]> Creatis software - gdcm.git/blobdiff - src/gdcmmpeg2/src/mpeg2dec/mpeg2dec.c
New method SerieHelper::AddSeriesDetail() to allow user to specifiy
[gdcm.git] / src / gdcmmpeg2 / src / mpeg2dec / mpeg2dec.c
index 09a4ae27cf80fa646f5bba04db445eb56306be73..f07432060e07bfdf73d41605bf596c0078841991 100644 (file)
  *
  */
 
-#include <stdio.h>
 #include <stdlib.h>
 #include <ctype.h>
 #include <fcntl.h>
-#include <unistd.h> // for lseek
 
 #define GLOBAL
 #include "config.h"
 
 /* private prototypes */
 static int  video_sequence _ANSI_ARGS_((int *framenum));
-static int Decode_Bitstream _ANSI_ARGS_((void));
+static int  Decode_Bitstream _ANSI_ARGS_((void));
 static int  Headers _ANSI_ARGS_((void));
 static void Initialize_Sequence _ANSI_ARGS_((void));
 static void Initialize_Decoder _ANSI_ARGS_((void));
+static void DeInitialize_Decoder _ANSI_ARGS_((void));
 static void Deinitialize_Sequence _ANSI_ARGS_((void));
 static void Process_Options _ANSI_ARGS_((int argc, char *argv[]));
 
@@ -59,11 +58,148 @@ static void Clear_Options();
 static void Print_Options();
 #endif
 
+
+int my_open(char *filename)
+{
+  abort();
+  return open(filename,O_RDONLY|O_BINARY);
+}
+
+off_t my_seek(istream *infile, off_t offset, int whence)
+{
+#ifdef FILESTAR
+  return fseek(infile->InFd,offset, whence);
+#else
+  return lseek(infile->InFd,offset, whence);
+#endif
+}
+ssize_t my_read(istream *infile, void *buf, size_t count)
+{
+#ifdef FILESTAR
+  return fread(buf,1,count, infile->InFd);
+#else
+  return read(infile->InFd,buf,count);
+#endif
+}
+
+int my_close(istream *infile)
+{
+#ifdef FILESTAR
+  return fclose(infile->InFd);
+#else
+  return close(infile->InFd);
+#endif
+}
+
+int my_fopenr(const char *path, const char *mode, istream *os)
+{
+  FILE *fd = fopen(path, mode);
+  if(fd)
+    {
+    os->InFd = fd;
+    return 1; /*success*/
+    }
+  else
+    os->InFd = NULL;
+  return 0;
+}
+
+int my_fopen(const char *path, const char *mode, ostream *os)
+{
+  FILE *fd = fopen(path, mode);
+  if(fd)
+    {
+    os->OutFd = fd;
+    return 1; /*success*/
+    }
+  else
+    os->OutFd = NULL;
+  return 0;
+}
+
+int my_fseek(ostream *stream, long offset, int whence)
+{
+  return fseek(stream->OutFd, offset, whence);
+}
+int my_fseekr(istream *stream, long offset, int whence)
+{
+  return fseek(stream->InFd, offset, whence);
+}
+
+size_t my_fread(void *ptr, size_t size, size_t nmemb, istream *stream)
+{
+  return fread(ptr, size, nmemb, stream->InFd);
+}
+
+size_t my_fwrite(const void *ptr, size_t size, size_t nmemb, ostream *stream)
+{
+  return fwrite(ptr, size, nmemb, stream->OutFd);
+}
+
+int my_fcloser(istream *fp)
+{
+  return fclose(fp->InFd);
+}
+int my_fclose(ostream *fp)
+{
+  return fclose(fp->OutFd);
+}
+#include <stdarg.h>
+
+int my_printf(const char *format, ...)
+{
+  va_list argptr;
+  int ret;
+
+  va_start(argptr, format);
+  ret = vprintf(format, argptr);
+  va_end(argptr);
+
+  return ret;
+}
+
+int my_sprintf(char *str, const char *format, ...)
+{
+  va_list argptr;
+  int ret;
+
+  va_start(argptr, format);
+  ret = vsprintf(str,format, argptr);
+  va_end(argptr);
+
+  return ret;
+}
+
+int my_fprintf(const char *format, ...)
+{
+  va_list argptr;
+  int ret;
+
+  va_start(argptr, format);
+  ret = vfprintf(stderr,format, argptr);
+  va_end(argptr);
+
+  return ret;
+}
+void my_exit(int status)
+{
+  exit(status);
+}
+
+
+#define GDCM_BUILD_MPEG2DEC
+
+#ifdef GDCM_BUILD_MPEG2DEC
 int main(argc,argv)
 int argc;
 char *argv[];
 {
   int ret, code;
+  istream bos,eos;
+  /*base.open_stream = my_open;*/
+  base.seek_stream = my_seek;
+  base.read_stream = my_read;
+  base.close_stream = my_close;
 
   Clear_Options();
 
@@ -78,7 +214,15 @@ char *argv[];
 
   /* open MPEG base layer bitstream file(s) */
   /* NOTE: this is either a base layer stream or a spatial enhancement stream */
-  if ((base.Infile=open(Main_Bitstream_Filename,O_RDONLY|O_BINARY))<0)
+/*  if ((base.Infile=open(Main_Bitstream_Filename,O_RDONLY|O_BINARY))<0) */
+  /*base.Infile = ld->open_stream(Main_Bitstream_Filename);*/
+  base.Infile = &bos;
+#ifdef FILESTAR
+  base.Infile->InFd = fopen(Main_Bitstream_Filename, "rb");
+#else
+  base.Infile->InFd = open(Main_Bitstream_Filename,O_RDONLY|O_BINARY );
+#endif
+  if( !base.Infile->InFd)
   {
     fprintf(stderr,"Base layer input file %s not found\n", Main_Bitstream_Filename);
     exit(1);
@@ -87,8 +231,8 @@ char *argv[];
 
   if(base.Infile != 0)
   {
-    Initialize_Buffer(); 
-  
+    Initialize_Buffer();
+
     if(Show_Bits(8)==0x47)
     {
       sprintf(Error_Text,"Decoder currently does not parse transport streams\n");
@@ -113,13 +257,15 @@ char *argv[];
       break;
     }
 
-    lseek(base.Infile, 0l, 0);
+    /*lseek(base.Infile, 0l, SEEK_SET);*/
+    ld->seek_stream(base.Infile,0l,SEEK_SET);
     Initialize_Buffer(); 
   }
 
   if(base.Infile!=0)
   {
-    lseek(base.Infile, 0l, 0);
+    /*lseek(base.Infile, 0l, SEEK_SET);*/
+    ld->seek_stream(base.Infile,0l,SEEK_SET);
   }
 
   Initialize_Buffer(); 
@@ -128,7 +274,15 @@ char *argv[];
   {
     ld = &enhan; /* select enhancement layer context */
 
-    if ((enhan.Infile = open(Enhancement_Layer_Bitstream_Filename,O_RDONLY|O_BINARY))<0)
+    /*if ((enhan.Infile = open(Enhancement_Layer_Bitstream_Filename,O_RDONLY|O_BINARY))<0)*/
+    /*enhan.Infile = ld->open_stream(Enhancement_Layer_Bitstream_Filename);*/
+    enhan.Infile = &eos;
+#ifdef FILESTAR
+    enhan.Infile->InFd = fopen(Main_Bitstream_Filename, "rb");
+#else
+    enhan.Infile->InFd = open(Enhancement_Layer_Bitstream_Filename,O_RDONLY|O_BINARY);
+#endif
+    if (enhan.Infile->InFd)
     {
       sprintf(Error_Text,"enhancment layer bitstream file %s not found\n",
         Enhancement_Layer_Bitstream_Filename);
@@ -144,15 +298,20 @@ char *argv[];
 
   ret = Decode_Bitstream();
 
-  close(base.Infile);
+  /*close(base.Infile);*/
+  ld->close_stream(base.Infile);
 
   if (Two_Streams)
-    close(enhan.Infile);
+    /*close(enhan.Infile);*/
+    ld->close_stream(enhan.Infile);
 
-  return 0;
+  DeInitialize_Decoder();
+
+  return ret;
 }
+#endif /*GDCM_BUILD_MPEG2DEC*/
 
-/* IMPLEMENTAION specific rouintes */
+/* IMPLEMENTATION specific routines */
 static void Initialize_Decoder()
 {
   int i;
@@ -174,7 +333,12 @@ static void Initialize_Decoder()
 
 }
 
-/* mostly IMPLEMENTAION specific rouintes */
+static void DeInitialize_Decoder()
+{
+  free(Clip-384); /* I love magic number */
+}
+
+/* mostly IMPLEMENTAION specific routines */
 static void Initialize_Sequence()
 {
   int cc, size;
@@ -263,7 +427,7 @@ static void Initialize_Sequence()
 }
 
 void Error(text)
-char *text;
+const char *text;
 {
   fprintf(stderr,text);
   exit(1);
@@ -291,27 +455,27 @@ char *argv[];              /* argument vector */
   if (argc<2)
   {
     printf("\n%s, %s\n",Version,Author);
-    printf("Usage:  mpeg2decode {options}\n\
-Options: -b  file  main bitstream (base or spatial enhancement layer)\n\
-         -cn file  conformance report (n: level)\n\
-         -e  file  enhancement layer bitstream (SNR or Data Partitioning)\n\
-         -f        store/display interlaced video in frame format\n\
-         -g        concatenated file format for substitution method (-x)\n\
-         -in file  information & statistics report  (n: level)\n\
-         -l  file  file name pattern for lower layer sequence\n\
-                   (for spatial scalability)\n\
-         -on file  output format (0:YUV 1:SIF 2:TGA 3:PPM 4:X11 5:X11HiQ)\n\
-         -q        disable warnings to stderr\n\
-         -r        use double precision reference IDCT\n\
-         -t        enable low level tracing to stdout\n\
-         -u  file  print user_data to stdio or file\n\
-         -vn       verbose output (n: level)\n\
-         -x  file  filename pattern of picture substitution sequence\n\n\
-File patterns:  for sequential filenames, \"printf\" style, e.g. rec%%d\n\
-                 or rec%%d%%c for fieldwise storage\n\
-Levels:        0:none 1:sequence 2:picture 3:slice 4:macroblock 5:block\n\n\
-Example:       mpeg2decode -b bitstream.mpg -f -r -o0 rec%%d\n\
-         \n");
+    printf("Usage:  mpeg2decode {options}\n"
+"Options: -b  file  main bitstream (base or spatial enhancement layer)\n"
+"         -cn file  conformance report (n: level)\n"
+"         -e  file  enhancement layer bitstream (SNR or Data Partitioning)\n"
+"         -f        store/display interlaced video in frame format\n"
+"         -g        concatenated file format for substitution method (-x)\n"
+"         -in file  information & statistics report  (n: level)\n"
+"         -l  file  file name pattern for lower layer sequence\n");
+printf("                   (for spatial scalability)\n"
+"         -on file  output format (0:YUV 1:SIF 2:TGA 3:PPM 4:X11 5:X11HiQ)\n"
+"         -q        disable warnings to stderr\n"
+"         -r        use double precision reference IDCT\n"
+"         -t        enable low level tracing to stdout\n"
+"         -u  file  print user_data to stdio or file\n"
+"         -vn       verbose output (n: level)\n"
+"         -x  file  filename pattern of picture substitution sequence\n\n");
+printf("File patterns:  for sequential filenames, \"printf\" style, e.g. rec%%d\n"
+"                 or rec%%d%%c for fieldwise storage\n"
+"Levels:        0:none 1:sequence 2:picture 3:slice 4:macroblock 5:block\n\n"
+"Example:       mpeg2decode -b bitstream.mpg -f -r -o0 rec%%d\n"
+"         \n");
     exit(0);
   }
 
@@ -606,21 +770,21 @@ static int Decode_Bitstream()
   Bitstream_Framenum = 0;
 
   for(;;)
-  {
+    {
 
 #ifdef VERIFY
     Clear_Verify_Headers();
 #endif /* VERIFY */
 
     ret = Headers();
-    
+
     if(ret==1)
-    {
-      ret = video_sequence(&Bitstream_Framenum);
-    }
+      {
+      /*ret =*/ video_sequence(&Bitstream_Framenum);
+      }
     else
       return(ret);
-  }
+    }
 
 }
 
@@ -629,6 +793,9 @@ static void Deinitialize_Sequence()
 {
   int i;
 
+  /* First cleanup the static buffer in store.c */
+  FreeStaticBuffer();
+
   /* clear flags */
   base.MPEG2_Flag=0;
 
@@ -764,3 +931,4 @@ static void Print_Options()
 
 }
 #endif
+