[cvs] / xvidcore / examples / xvid_decraw.c Repository:
ViewVC logotype

Diff of /xvidcore/examples/xvid_decraw.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.7.2.5, Sat Aug 9 17:19:15 2003 UTC revision 1.8, Sat Feb 22 21:37:50 2003 UTC
# Line 4  Line 4 
4   *  - Console based decoding test application  -   *  - Console based decoding test application  -
5   *   *
6   *  Copyright(C) 2002-2003 Christoph Lampert   *  Copyright(C) 2002-2003 Christoph Lampert
  *               2002-2003 Edouard Gomez <ed.gomez@free.fr>  
7   *   *
8   *  This program is free software; you can redistribute it and/or modify   *  This program is free software; you can redistribute it and/or modify
9   *  it under the terms of the GNU General Public License as published by   *  it under the terms of the GNU General Public License as published by
# Line 32  Line 31 
31   *  the speed for this is measured.   *  the speed for this is measured.
32   *   *
33   *  The program is plain C and needs no libraries except for libxvidcore,   *  The program is plain C and needs no libraries except for libxvidcore,
34   *  and maths-lib.   *  and maths-lib, so with UN*X you simply compile by
35   *   *
36   *  Use ./xvid_decraw -help for a list of options   *   gcc xvid_decraw.c -lxvidcore -lm -o xvid_decraw
37     *
38     *  You have to specify the image dimensions (until the add the feature
39     *  to read this from the bitstream)
40     *
41     * Usage : xvid_decraw <-w width> <-h height> [OPTIONS]
42     * Options :
43     *  -asm           : use assembly optimizations (default=disabled)
44     *  -w integer     : frame width ([1.2048])
45     *  -h integer     : frame height ([1.2048])
46     *  -i string      : input filename (default=stdin)
47     *  -t integer     : input data type (raw=0, mp4u=1)
48     *  -d boolean     : save decoder output (0 False*, !=0 True)
49     *  -m boolean     : save mpeg4 raw stream to single files (0 False*, !=0 True)
50     *  -help          : This help message
51     * (* means default)
52   *   *
53   ****************************************************************************/   ****************************************************************************/
54    
# Line 67  Line 81 
81  static char filepath[256] = "./";  static char filepath[256] = "./";
82  static void *dec_handle = NULL;  static void *dec_handle = NULL;
83    
84  #define BUFFER_SIZE (2*1024*1024)  # define BUFFER_SIZE 10*XDIM*YDIM
85    
86  /*****************************************************************************  /*****************************************************************************
87   *               Local prototypes   *               Local prototypes
# Line 80  Line 94 
94  static int dec_main(unsigned char *istream,  static int dec_main(unsigned char *istream,
95                                          unsigned char *ostream,                                          unsigned char *ostream,
96                                          int istream_size,                                          int istream_size,
97                                          xvid_dec_stats_t *xvid_dec_stats);                                          int *ostream_size,
98                                            int *isframe);
99  static int dec_stop();  static int dec_stop();
100  static void usage();  static void usage();
101    
   
 const char * type2str(int type)  
 {  
     if (type==XVID_TYPE_IVOP)  
         return "I";  
     if (type==XVID_TYPE_PVOP)  
         return "P";  
     if (type==XVID_TYPE_BVOP)  
         return "B";  
     return "S";  
 }  
   
102  /*****************************************************************************  /*****************************************************************************
103   *        Main program   *        Main program
104   ****************************************************************************/   ****************************************************************************/
# Line 105  Line 108 
108          unsigned char *mp4_buffer = NULL;          unsigned char *mp4_buffer = NULL;
109          unsigned char *mp4_ptr    = NULL;          unsigned char *mp4_ptr    = NULL;
110          unsigned char *out_buffer = NULL;          unsigned char *out_buffer = NULL;
111          int useful_bytes;          int bigendian = 0;
112          xvid_dec_stats_t xvid_dec_stats;          int still_left_in_buffer;
113            int delayed_frames;
114    
115          double totaldectime;          double totaldectime;
116    
# Line 132  Line 136 
136    
137                  if (strcmp("-asm", argv[i]) == 0 ) {                  if (strcmp("-asm", argv[i]) == 0 ) {
138                          use_assembler = 1;                          use_assembler = 1;
139                  } else if (strcmp("-d", argv[i]) == 0) {                  }
140                          ARG_SAVEDECOUTPUT = 1;                  else if (strcmp("-w", argv[i]) == 0 && i < argc - 1 ) {
141                  } else if (strcmp("-i", argv[i]) == 0 && i < argc - 1 ) {                          i++;
142                            XDIM = atoi(argv[i]);
143                    }
144                    else if (strcmp("-h", argv[i]) == 0 && i < argc - 1 ) {
145                            i++;
146                            YDIM = atoi(argv[i]);
147                    }
148                    else if (strcmp("-d", argv[i]) == 0 && i < argc - 1 ) {
149                            i++;
150                            ARG_SAVEDECOUTPUT = atoi(argv[i]);
151                    }
152                    else if (strcmp("-i", argv[i]) == 0 && i < argc - 1 ) {
153                          i++;                          i++;
154                          ARG_INPUTFILE = argv[i];                          ARG_INPUTFILE = argv[i];
155                  } else if (strcmp("-m", argv[i]) == 0) {                  }
156                          ARG_SAVEMPEGSTREAM = 1;                  else if (strcmp("-m", argv[i]) == 0 && i < argc - 1 ) {
157                  } else if (strcmp("-help", argv[i]) == 0) {                          i++;
158                            ARG_SAVEMPEGSTREAM = atoi(argv[i]);
159                    }
160                    else if (strcmp("-help", argv[i])) {
161                          usage();                          usage();
162                          return(0);                          return(0);
163                  } else {                  }
164                    else {
165                          usage();                          usage();
166                          exit(-1);                          exit(-1);
167                  }                  }
168    
169          }          }
170    
171  /*****************************************************************************  /*****************************************************************************
172   * Values checking   * Values checking
173   ****************************************************************************/   ****************************************************************************/
174    
175            if(XDIM <= 0 || XDIM > 2048 || YDIM <= 0 || YDIM > 2048) {
176                    usage();
177                    return -1;
178            }
179    
180          if ( ARG_INPUTFILE == NULL || strcmp(ARG_INPUTFILE, "stdin") == 0) {          if ( ARG_INPUTFILE == NULL || strcmp(ARG_INPUTFILE, "stdin") == 0) {
181                  in_file = stdin;                  in_file = stdin;
182          }          }
# Line 160  Line 185 
185                  in_file = fopen(ARG_INPUTFILE, "rb");                  in_file = fopen(ARG_INPUTFILE, "rb");
186                  if (in_file == NULL) {                  if (in_file == NULL) {
187                          fprintf(stderr, "Error opening input file %s\n", ARG_INPUTFILE);                          fprintf(stderr, "Error opening input file %s\n", ARG_INPUTFILE);
188                          return(-1);                          return -1;
189                  }                  }
190          }          }
191    
# Line 174  Line 199 
199          if (!mp4_buffer)          if (!mp4_buffer)
200                  goto free_all_memory;                  goto free_all_memory;
201    
202            /* Memory for frame output */
203            out_buffer = (unsigned char *) malloc(XDIM*YDIM*4);
204            if (!out_buffer)
205                    goto free_all_memory;
206    
207    
208  /*****************************************************************************  /*****************************************************************************
209   *        XviD PART  Start   *        XviD PART  Start
210   ****************************************************************************/   ****************************************************************************/
# Line 191  Line 222 
222   ****************************************************************************/   ****************************************************************************/
223    
224          /* Fill the buffer */          /* Fill the buffer */
225          useful_bytes = fread(mp4_buffer, 1, BUFFER_SIZE, in_file);          still_left_in_buffer = fread(mp4_buffer, 1, BUFFER_SIZE, in_file);
226    
227          totaldectime = 0;          totaldectime = 0;
228          totalsize = 0;          totalsize = 0;
229          filenr = 0;          filenr = 0;
230            delayed_frames = 0;
231          mp4_ptr = mp4_buffer;          mp4_ptr = mp4_buffer;
232    
233          do {          do {
234    
235                    int mp4_size = (mp4_buffer + BUFFER_SIZE - mp4_ptr);
236                  int used_bytes = 0;                  int used_bytes = 0;
237                  double dectime;                  double dectime;
238                    int notification;
239    
240                  /*                  /*
241                   * If the buffer is half empty or there are no more bytes in it                   * If the buffer is half empty or there are no more bytes in it
242                   * then fill it.                   * then fill it.
243                   */                   */
244                  if (mp4_ptr > mp4_buffer + BUFFER_SIZE/2) {                  if (mp4_ptr > mp4_buffer + BUFFER_SIZE/2 ||
245                          int already_in_buffer = (mp4_buffer + BUFFER_SIZE - mp4_ptr);                          still_left_in_buffer <= 0) {
246                            int rest = (mp4_buffer + BUFFER_SIZE - mp4_ptr);
247    
248                          /* Move data if needed */                          /* Move data if needed */
249                          if (already_in_buffer > 0)                          if (rest)
250                                  memcpy(mp4_buffer, mp4_ptr, already_in_buffer);                                  memcpy(mp4_buffer, mp4_ptr, rest);
251    
252                          /* Update mp4_ptr */                          /* Update mp4_ptr */
253                          mp4_ptr = mp4_buffer;                          mp4_ptr = mp4_buffer;
# Line 220  Line 256 
256              if(feof(in_file))              if(feof(in_file))
257                                  break;                                  break;
258    
259                          useful_bytes += fread(mp4_buffer + already_in_buffer,                          still_left_in_buffer = fread(mp4_buffer + rest,
260                                                                    1, BUFFER_SIZE - already_in_buffer,                                                                                   1,
261                                                                                     BUFFER_SIZE - rest,
262                                                                    in_file);                                                                    in_file);
263    
264                  }                  }
265    
266    
267                  /* This loop is needed to handle VOL/NVOP reading */                  /* This loop flushes N_VOPS (with vop_coded bit set to 0) */
268                  do {                  do {
269    
270                          /* Decode frame */                          /* Decode frame */
271                          dectime = msecond();                          dectime = msecond();
272                          used_bytes = dec_main(mp4_ptr, out_buffer, useful_bytes, &xvid_dec_stats);                          status = dec_main(mp4_ptr, out_buffer, mp4_size, &used_bytes, &notification);
273                          dectime = msecond() - dectime;                          dectime = msecond() - dectime;
274    
275                          /* Resize image buffer if needed */                          if (status) {
276                          if(xvid_dec_stats.type == XVID_TYPE_VOL) {                                  break;
   
                                 /* Check if old buffer is smaller */  
                                 if(XDIM*YDIM < xvid_dec_stats.data.vol.width*xvid_dec_stats.data.vol.height) {  
   
                                         /* Copy new witdh and new height from the vol structure */  
                                         XDIM = xvid_dec_stats.data.vol.width;  
                                         YDIM = xvid_dec_stats.data.vol.height;  
   
                                         /* Free old output buffer*/  
                                         if(out_buffer) free(out_buffer);  
   
                                         /* Allocate the new buffer */  
                                         out_buffer = (unsigned char*)malloc(XDIM*YDIM*4);  
                                         if(out_buffer == NULL)  
                                                 goto free_all_memory;  
   
                                         fprintf(stderr, "Resized frame buffer to %dx%d\n", XDIM, YDIM);  
                                 }  
277                          }                          }
278    
279                          /* Update buffer pointers */                          /* Update buffer pointers */
                         if(used_bytes > 0) {  
280                                  mp4_ptr += used_bytes;                                  mp4_ptr += used_bytes;
281                                  useful_bytes -= used_bytes;                          still_left_in_buffer -= used_bytes;
282    
283                                  /* Total size */                                  /* Total size */
284                                  totalsize += used_bytes;                                  totalsize += used_bytes;
                         }  
285    
286                  }while(xvid_dec_stats.type <= 0 && useful_bytes > 0);                  }while(used_bytes <= 7 && still_left_in_buffer > 0); /* <= 7 bytes is a NVOPS */
287    
288                  /* Check if there is a negative number of useful bytes left in buffer                  /* Negative buffer would mean we went too far */
289                   * This means we went too far */                  if(still_left_in_buffer < 0) break;
290          if(useful_bytes < 0)  
291              break;                  /* Skip when decoder is buffering images or decoding VOL information */
292                    if(notification != XVID_DEC_VOP) {
293                            /* It's a delay only if it notifies NOTHING */
294                            if(notification == XVID_DEC_NOTHING)
295                                    delayed_frames++;
296                            continue;
297                    }
298    
299          /* Updated data - Count only usefull decode time */          /* Updated data - Count only usefull decode time */
300                  totaldectime += dectime;                  totaldectime += dectime;
301    
302                    /* Prints some decoding stats */
303          printf("Frame %5d: type = %s, dectime(ms) =%6.1f, length(bytes) =%7d\n",                  printf("Frame %5d: dectime(ms) =%6.1f, length(bytes) =%7d\n",
304                             filenr, type2str(xvid_dec_stats.type), dectime, used_bytes);                             filenr, dectime, used_bytes);
305    
306                  /* Save individual mpeg4 stream if required */                  /* Save individual mpeg4 stream if required */
307                  if(ARG_SAVEMPEGSTREAM) {                  if(ARG_SAVEMPEGSTREAM) {
# Line 314  Line 337 
337  /*****************************************************************************  /*****************************************************************************
338   *     Flush decoder buffers   *     Flush decoder buffers
339   ****************************************************************************/   ****************************************************************************/
340            while(delayed_frames--) {
         do {  
341    
342                  /* Fake vars */                  /* Fake vars */
343                  int used_bytes;                  int used_bytes, isframe;
344                  double dectime;                  double dectime;
345    
346          do {                  /* Decode frame */
347                      dectime = msecond();                      dectime = msecond();
348                      used_bytes = dec_main(NULL, out_buffer, -1, &xvid_dec_stats);                  status = dec_main(NULL, out_buffer, -1, &used_bytes, &isframe);
349                      dectime = msecond() - dectime;                      dectime = msecond() - dectime;
         }while(used_bytes>=0 && xvid_dec_stats.type <= 0);  
350    
351          if (used_bytes < 0) {   /* XVID_ERR_END */                  if (status) {
352              break;              break;
353          }          }
354    
# Line 335  Line 356 
356                  totaldectime += dectime;                  totaldectime += dectime;
357    
358                  /* Prints some decoding stats */                  /* Prints some decoding stats */
359          printf("Frame %5d: type = %s, dectime(ms) =%6.1f, length(bytes) =%7d\n",                  printf("Frame %5d: dectime(ms) =%6.1f, length(bytes) =%7d\n",
360                             filenr, type2str(xvid_dec_stats.type), dectime, used_bytes);                             filenr, dectime, used_bytes);
361    
362                  /* Save output frame if required */                  /* Save output frame if required */
363                  if (ARG_SAVEDECOUTPUT) {                  if (ARG_SAVEDECOUTPUT) {
# Line 350  Line 371 
371    
372                  filenr++;                  filenr++;
373    
374          }while(1);          }
375    
376  /*****************************************************************************  /*****************************************************************************
377   *     Calculate totals and averages for output, print results   *     Calculate totals and averages for output, print results
# Line 377  Line 398 
398          free(out_buffer);          free(out_buffer);
399          free(mp4_buffer);          free(mp4_buffer);
400    
401          return(0);          return 0;
402  }  }
403    
404  /*****************************************************************************  /*****************************************************************************
# Line 387  Line 408 
408  static void usage()  static void usage()
409  {  {
410    
411          fprintf(stderr, "Usage : xvid_decraw [OPTIONS]\n");          fprintf(stderr, "Usage : xvid_decraw <-w width> <-h height> [OPTIONS]\n");
412          fprintf(stderr, "Options :\n");          fprintf(stderr, "Options :\n");
413          fprintf(stderr, " -asm           : use assembly optimizations (default=disabled)\n");          fprintf(stderr, " -asm           : use assembly optimizations (default=disabled)\n");
414            fprintf(stderr, " -w integer     : frame width ([1.2048])\n");
415            fprintf(stderr, " -h integer     : frame height ([1.2048])\n");
416          fprintf(stderr, " -i string      : input filename (default=stdin)\n");          fprintf(stderr, " -i string      : input filename (default=stdin)\n");
417          fprintf(stderr, " -d             : save decoder output\n");          fprintf(stderr, " -t integer     : input data type (raw=0, mp4u=1)\n");
418          fprintf(stderr, " -m             : save mpeg4 raw stream to individual files\n");          fprintf(stderr, " -d boolean     : save decoder output (0 False*, !=0 True)\n");
419            fprintf(stderr, " -m boolean     : save mpeg4 raw stream to individual files (0 False*, !=0 True)\n");
420          fprintf(stderr, " -help          : This help message\n");          fprintf(stderr, " -help          : This help message\n");
421          fprintf(stderr, " (* means default)\n");          fprintf(stderr, " (* means default)\n");
422    
# Line 409  Line 433 
433  #ifndef WIN32  #ifndef WIN32
434          struct timeval  tv;          struct timeval  tv;
435          gettimeofday(&tv, 0);          gettimeofday(&tv, 0);
436          return((double)tv.tv_sec*1.0e3 + (double)tv.tv_usec*1.0e-3);          return (double)tv.tv_sec*1.0e3 + (double)tv.tv_usec*1.0e-3;
437  #else  #else
438          clock_t clk;          clock_t clk;
439          clk = clock();          clk = clock();
440          return(clk * 1000 / CLOCKS_PER_SEC);          return clk * 1000 / CLOCKS_PER_SEC;
441  #endif  #endif
442  }  }
443    
# Line 458  Line 482 
482                  /* Close file */                  /* Close file */
483                  fclose(filehandle);                  fclose(filehandle);
484    
485                  return(0);                  return 0;
486          }          }
487          else          else
488                  return(1);                  return 1;
489  }  }
490    
491  /*****************************************************************************  /*****************************************************************************
# Line 472  Line 496 
496  static int  static int
497  dec_init(int use_assembler)  dec_init(int use_assembler)
498  {  {
499          int ret;          int xerr;
   
         xvid_gbl_init_t   xvid_gbl_init;  
         xvid_dec_create_t xvid_dec_create;  
   
         /*------------------------------------------------------------------------  
          * XviD core initialization  
          *----------------------------------------------------------------------*/  
500    
501          /* Version */          XVID_INIT_PARAM xinit;
502          xvid_gbl_init.version = XVID_VERSION;          XVID_DEC_PARAM xparam;
503    
         /* Assembly setting */  
504          if(use_assembler)          if(use_assembler)
505  #ifdef ARCH_IS_IA64  #ifdef ARCH_IS_IA64
506                  xvid_gbl_init.cpu_flags = XVID_CPU_FORCE | XVID_CPU_IA64;                          xinit.cpu_flags = XVID_CPU_FORCE | XVID_CPU_IA64;
507  #else  #else
508          xvid_gbl_init.cpu_flags = 0;                          xinit.cpu_flags = 0;
509  #endif  #endif
510          else          else
511                  xvid_gbl_init.cpu_flags = XVID_CPU_FORCE;                          xinit.cpu_flags = XVID_CPU_FORCE;
512    
513          xvid_global(NULL, 0, &xvid_gbl_init, NULL);          xvid_init(NULL, 0, &xinit, NULL);
514            xparam.width = XDIM;
515            xparam.height = YDIM;
516    
517          /*------------------------------------------------------------------------          xerr = xvid_decore(NULL, XVID_DEC_CREATE, &xparam, NULL);
          * XviD encoder initialization  
          *----------------------------------------------------------------------*/  
518    
519          /* Version */          dec_handle = xparam.handle;
         xvid_dec_create.version = XVID_VERSION;  
520    
521          /*          return xerr;
          * Image dimensions -- set to 0, xvidcore will resize when ever it is  
          * needed  
          */  
         xvid_dec_create.width = 0;  
         xvid_dec_create.height = 0;  
   
         ret = xvid_decore(NULL, XVID_DEC_CREATE, &xvid_dec_create, NULL);  
   
         dec_handle = xvid_dec_create.handle;  
   
         return(ret);  
522  }  }
523    
524  /* decode one frame  */  /* decode one frame  */
# Line 522  Line 526 
526  dec_main(unsigned char *istream,  dec_main(unsigned char *istream,
527                   unsigned char *ostream,                   unsigned char *ostream,
528                   int istream_size,                   int istream_size,
529                   xvid_dec_stats_t *xvid_dec_stats)                   int *ostream_size,
530                     int *notification)
531  {  {
532    
533          int ret;          int xerr;
534            XVID_DEC_FRAME xframe;
535          xvid_dec_frame_t xvid_dec_frame;                  XVID_DEC_STATS xstats;
   
         /* Set version */  
         xvid_dec_frame.version = XVID_VERSION;  
         xvid_dec_stats->version = XVID_VERSION;  
536    
537          /* No general flags to set */          xframe.general    = 0;
538          xvid_dec_frame.general          = 0;                  xframe.bitstream  = istream;
539            xframe.length     = istream_size;
540                    xframe.image      = ostream;
541            xframe.stride     = XDIM;
542                    xframe.colorspace = XVID_CSP_I420;
543    
544          /* Input stream */                  xerr = xvid_decore(dec_handle, XVID_DEC_DECODE, &xframe, &xstats);
         xvid_dec_frame.bitstream        = istream;  
         xvid_dec_frame.length           = istream_size;  
545    
546          /* Output frame structure */                  *ostream_size = xframe.length;
         xvid_dec_frame.output.plane[0]  = ostream;  
         xvid_dec_frame.output.stride[0] = XDIM;  
         xvid_dec_frame.output.csp       = XVID_CSP_I420;  
547    
548          ret = xvid_decore(dec_handle, XVID_DEC_DECODE, &xvid_dec_frame, xvid_dec_stats);                  *notification = xstats.notify;
549    
550          return(ret);          return xerr;
551  }  }
552    
553  /* close decoder to release resources */  /* close decoder to release resources */
554  static int  static int
555  dec_stop()  dec_stop()
556  {  {
557          int ret;          int xerr;
558    
559          ret = xvid_decore(dec_handle, XVID_DEC_DESTROY, NULL, NULL);          xerr = xvid_decore(dec_handle, XVID_DEC_DESTROY, NULL, NULL);
560    
561          return(ret);          return xerr;
562  }  }

Legend:
Removed from v.1.7.2.5  
changed lines
  Added in v.1.8

No admin address has been configured
ViewVC Help
Powered by ViewVC 1.0.4