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

Diff of /trunk/xvidcore/examples/xvid_decraw.c

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

revision 902, Mon Mar 3 11:18:53 2003 UTC revision 1416, Sat Apr 10 04:25:31 2004 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
7     *               2002-2003 Edouard Gomez <ed.gomez@free.fr>
8   *   *
9   *  This program is free software; you can redistribute it and/or modify   *  This program is free software; you can redistribute it and/or modify
10   *  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 19  Line 20 
20   *  along with this program; if not, write to the Free Software   *  along with this program; if not, write to the Free Software
21   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
22   *   *
23   * $Id: xvid_decraw.c,v 1.9 2003-03-03 11:18:53 chl Exp $   * $Id: xvid_decraw.c,v 1.11 2004-04-10 04:25:31 suxen_drol Exp $
24   *   *
25   ****************************************************************************/   ****************************************************************************/
26    
# Line 31  Line 32 
32   *  the speed for this is measured.   *  the speed for this is measured.
33   *   *
34   *  The program is plain C and needs no libraries except for libxvidcore,   *  The program is plain C and needs no libraries except for libxvidcore,
35   *  and maths-lib, so with UN*X you simply compile by   *  and maths-lib.
36   *   *
37   *   gcc xvid_decraw.c -lxvidcore -lm -o xvid_decraw   *  Use ./xvid_decraw -help for a list of options
  *  
  *  You have to specify the image dimensions (until the add the feature  
  *  to read this from the bitstream)  
  *  
  * Usage : xvid_decraw <-w width> <-h height> [OPTIONS]  
  * Options :  
  *  -asm           : use assembly optimizations (default=disabled)  
  *  -w integer     : frame width ([1.2048])  
  *  -h integer     : frame height ([1.2048])  
  *  -i string      : input filename (default=stdin)  
  *  -t integer     : input data type (raw=0, mp4u=1)  
  *  -d boolean     : save decoder output (0 False*, !=0 True)  
  *  -m boolean     : save mpeg4 raw stream to single files (0 False*, !=0 True)  
  *  -help          : This help message  
  * (* means default)  
38   *   *
39   ****************************************************************************/   ****************************************************************************/
40    
# Line 81  Line 67 
67  static char filepath[256] = "./";  static char filepath[256] = "./";
68  static void *dec_handle = NULL;  static void *dec_handle = NULL;
69    
70  # define BUFFER_SIZE 10*XDIM*YDIM  #define BUFFER_SIZE (2*1024*1024)
71    
72  /*****************************************************************************  /*****************************************************************************
73   *               Local prototypes   *               Local prototypes
# Line 90  Line 76 
76  static double msecond();  static double msecond();
77  static int write_pgm(char *filename,  static int write_pgm(char *filename,
78                       unsigned char *image);                       unsigned char *image);
79  static int dec_init(int use_assembler);  static int dec_init(int use_assembler, int debug_level);
80  static int dec_main(unsigned char *istream,  static int dec_main(unsigned char *istream,
81                                          unsigned char *ostream,                                          unsigned char *ostream,
82                                          int istream_size,                                          int istream_size,
83                                          int *ostream_size,                                          xvid_dec_stats_t *xvid_dec_stats);
                                         int *isframe);  
84  static int dec_stop();  static int dec_stop();
85  static void usage();  static void usage();
86    
87    
88    const char * type2str(int type)
89    {
90        if (type==XVID_TYPE_IVOP)
91            return "I";
92        if (type==XVID_TYPE_PVOP)
93            return "P";
94        if (type==XVID_TYPE_BVOP)
95            return "B";
96        return "S";
97    }
98    
99  /*****************************************************************************  /*****************************************************************************
100   *        Main program   *        Main program
101   ****************************************************************************/   ****************************************************************************/
# Line 108  Line 105 
105          unsigned char *mp4_buffer = NULL;          unsigned char *mp4_buffer = NULL;
106          unsigned char *mp4_ptr    = NULL;          unsigned char *mp4_ptr    = NULL;
107          unsigned char *out_buffer = NULL;          unsigned char *out_buffer = NULL;
108          int bigendian = 0;          int useful_bytes;
109          int still_left_in_buffer;          xvid_dec_stats_t xvid_dec_stats;
         int delayed_frames;  
110    
111          double totaldectime;          double totaldectime;
112    
# Line 118  Line 114 
114          int status;          int status;
115    
116          int use_assembler = 0;          int use_assembler = 0;
117            int debug_level = 0;
118    
119          char filename[256];          char filename[256];
120    
# Line 136  Line 133 
133    
134                  if (strcmp("-asm", argv[i]) == 0 ) {                  if (strcmp("-asm", argv[i]) == 0 ) {
135                          use_assembler = 1;                          use_assembler = 1;
136                  }                  } else if (strcmp("-debug", argv[i]) == 0 && i < argc - 1 ) {
                 else if (strcmp("-w", argv[i]) == 0 && i < argc - 1 ) {  
137                          i++;                          i++;
138                          XDIM = atoi(argv[i]);                          if (sscanf(argv[i], "0x%x", &debug_level) != 1) {
139                                    debug_level = atoi(argv[i]);
140                  }                  }
141                  else if (strcmp("-h", argv[i]) == 0 && i < argc - 1 ) {                  } else if (strcmp("-d", argv[i]) == 0) {
142                          i++;                          ARG_SAVEDECOUTPUT = 1;
143                          YDIM = atoi(argv[i]);                  } else if (strcmp("-i", argv[i]) == 0 && i < argc - 1 ) {
                 }  
                 else if (strcmp("-d", argv[i]) == 0 && i < argc - 1 ) {  
                         i++;  
                         ARG_SAVEDECOUTPUT = atoi(argv[i]);  
                 }  
                 else if (strcmp("-i", argv[i]) == 0 && i < argc - 1 ) {  
144                          i++;                          i++;
145                          ARG_INPUTFILE = argv[i];                          ARG_INPUTFILE = argv[i];
146                  }                  } else if (strcmp("-m", argv[i]) == 0) {
147                  else if (strcmp("-m", argv[i]) == 0 && i < argc - 1 ) {                          ARG_SAVEMPEGSTREAM = 1;
148                          i++;                  } else if (strcmp("-help", argv[i]) == 0) {
                         ARG_SAVEMPEGSTREAM = atoi(argv[i]);  
                 }  
                 else if (strcmp("-help", argv[i])) {  
149                          usage();                          usage();
150                          return(0);                          return(0);
151                  }                  } else {
                 else {  
152                          usage();                          usage();
153                          exit(-1);                          exit(-1);
154                  }                  }
   
155          }          }
156    
157  /*****************************************************************************  /*****************************************************************************
158   * Values checking   * Values checking
159   ****************************************************************************/   ****************************************************************************/
160    
         if(XDIM <= 0 || XDIM > 2048 || YDIM <= 0 || YDIM > 2048) {  
                 usage();  
                 return -1;  
         }  
   
161          if ( ARG_INPUTFILE == NULL || strcmp(ARG_INPUTFILE, "stdin") == 0) {          if ( ARG_INPUTFILE == NULL || strcmp(ARG_INPUTFILE, "stdin") == 0) {
162                  in_file = stdin;                  in_file = stdin;
163          }          }
# Line 185  Line 166 
166                  in_file = fopen(ARG_INPUTFILE, "rb");                  in_file = fopen(ARG_INPUTFILE, "rb");
167                  if (in_file == NULL) {                  if (in_file == NULL) {
168                          fprintf(stderr, "Error opening input file %s\n", ARG_INPUTFILE);                          fprintf(stderr, "Error opening input file %s\n", ARG_INPUTFILE);
169                          return -1;                          return(-1);
170                  }                  }
171          }          }
172    
# Line 199  Line 180 
180          if (!mp4_buffer)          if (!mp4_buffer)
181                  goto free_all_memory;                  goto free_all_memory;
182    
         /* Memory for frame output */  
         out_buffer = (unsigned char *) malloc(XDIM*YDIM*4);  
         if (!out_buffer)  
                 goto free_all_memory;  
   
   
183  /*****************************************************************************  /*****************************************************************************
184   *        XviD PART  Start   *        XviD PART  Start
185   ****************************************************************************/   ****************************************************************************/
186    
187          status = dec_init(use_assembler);          status = dec_init(use_assembler, debug_level);
188          if (status) {          if (status) {
189                  fprintf(stderr,                  fprintf(stderr,
190                          "Decore INIT problem, return value %d\n", status);                          "Decore INIT problem, return value %d\n", status);
# Line 222  Line 197 
197   ****************************************************************************/   ****************************************************************************/
198    
199          /* Fill the buffer */          /* Fill the buffer */
200          still_left_in_buffer = fread(mp4_buffer, 1, BUFFER_SIZE, in_file);          useful_bytes = fread(mp4_buffer, 1, BUFFER_SIZE, in_file);
201    
202          totaldectime = 0;          totaldectime = 0;
203          totalsize = 0;          totalsize = 0;
204          filenr = 0;          filenr = 0;
         delayed_frames = 0;  
205          mp4_ptr = mp4_buffer;          mp4_ptr = mp4_buffer;
206    
207          do {          do {
   
                 int mp4_size = (mp4_buffer + BUFFER_SIZE - mp4_ptr);  
208                  int used_bytes = 0;                  int used_bytes = 0;
209                  double dectime;                  double dectime;
                 int notification;  
210    
211                  /*                  /*
212                   * 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
213                   * then fill it.                   * then fill it.
214                   */                   */
215                  if (mp4_ptr > mp4_buffer + BUFFER_SIZE/2 ||                  if (mp4_ptr > mp4_buffer + BUFFER_SIZE/2) {
216                          still_left_in_buffer <= 0) {                          int already_in_buffer = (mp4_buffer + BUFFER_SIZE - mp4_ptr);
                         int rest = (mp4_buffer + BUFFER_SIZE - mp4_ptr);  
217    
218                          /* Move data if needed */                          /* Move data if needed */
219                          if (rest)                          if (already_in_buffer > 0)
220                                  memcpy(mp4_buffer, mp4_ptr, rest);                                  memcpy(mp4_buffer, mp4_ptr, already_in_buffer);
221    
222                          /* Update mp4_ptr */                          /* Update mp4_ptr */
223                          mp4_ptr = mp4_buffer;                          mp4_ptr = mp4_buffer;
# Line 256  Line 226 
226                          if(feof(in_file))                          if(feof(in_file))
227                                  break;                                  break;
228    
229                          still_left_in_buffer = rest + fread(mp4_buffer + rest,                          useful_bytes += fread(mp4_buffer + already_in_buffer,
230                                                                                   1,                                                                    1, BUFFER_SIZE - already_in_buffer,
                                                                                  BUFFER_SIZE - rest,  
231                                                                                   in_file);                                                                                   in_file);
232    
233                  }                  }
234    
235    
236                  /* This loop flushes N_VOPS (with vop_coded bit set to 0) */                  /* This loop is needed to handle VOL/NVOP reading */
237                  do {                  do {
238    
239                          /* Decode frame */                          /* Decode frame */
240                          dectime = msecond();                          dectime = msecond();
241                          status = dec_main(mp4_ptr, out_buffer, mp4_size, &used_bytes, &notification);                          used_bytes = dec_main(mp4_ptr, out_buffer, useful_bytes, &xvid_dec_stats);
242                          dectime = msecond() - dectime;                          dectime = msecond() - dectime;
243    
244                          if (status) {                          /* Resize image buffer if needed */
245                                  break;                          if(xvid_dec_stats.type == XVID_TYPE_VOL) {
246    
247                                    /* Check if old buffer is smaller */
248                                    if(XDIM*YDIM < xvid_dec_stats.data.vol.width*xvid_dec_stats.data.vol.height) {
249    
250                                            /* Copy new witdh and new height from the vol structure */
251                                            XDIM = xvid_dec_stats.data.vol.width;
252                                            YDIM = xvid_dec_stats.data.vol.height;
253    
254                                            /* Free old output buffer*/
255                                            if(out_buffer) free(out_buffer);
256    
257                                            /* Allocate the new buffer */
258                                            out_buffer = (unsigned char*)malloc(XDIM*YDIM*4);
259                                            if(out_buffer == NULL)
260                                                    goto free_all_memory;
261    
262                                            fprintf(stderr, "Resized frame buffer to %dx%d\n", XDIM, YDIM);
263                                    }
264                          }                          }
265    
266                          /* Update buffer pointers */                          /* Update buffer pointers */
267                            if(used_bytes > 0) {
268                          mp4_ptr += used_bytes;                          mp4_ptr += used_bytes;
269                          still_left_in_buffer -= used_bytes;                                  useful_bytes -= used_bytes;
270    
271                          /* Total size */                          /* Total size */
272                          totalsize += used_bytes;                          totalsize += used_bytes;
273                            }
274    
275                  }while(used_bytes <= 7 && still_left_in_buffer > 0); /* <= 7 bytes is a NVOPS */                  }while(xvid_dec_stats.type <= 0 && useful_bytes > 0);
   
                 /* Negative buffer would mean we went too far */  
                 if(still_left_in_buffer < 0) break;  
276    
277                  /* Skip when decoder is buffering images or decoding VOL information */                  /* Check if there is a negative number of useful bytes left in buffer
278                  if(notification != XVID_DEC_VOP) {                   * This means we went too far */
279                          /* It's a delay only if it notifies NOTHING */          if(useful_bytes < 0)
280                          if(notification == XVID_DEC_NOTHING)              break;
                                 delayed_frames++;  
                         continue;  
                 }  
281    
282                  /* Updated data - Count only usefull decode time */                  /* Updated data - Count only usefull decode time */
283                  totaldectime += dectime;                  totaldectime += dectime;
284    
285                  /* Prints some decoding stats */  
286                  printf("Frame %5d: dectime(ms) =%6.1f, length(bytes) =%7d\n",          printf("Frame %5d: type = %s, dectime(ms) =%6.1f, length(bytes) =%7d\n",
287                             filenr, dectime, used_bytes);                             filenr, type2str(xvid_dec_stats.type), dectime, used_bytes);
288    
289                  /* Save individual mpeg4 stream if required */                  /* Save individual mpeg4 stream if required */
290                  if (ARG_SAVEMPEGSTREAM) {                  if (ARG_SAVEMPEGSTREAM) {
# Line 315  Line 298 
298                                                  filename);                                                  filename);
299                          }                          }
300                          else {                          else {
301                                  fwrite(mp4_buffer, 1, used_bytes, filehandle);                                  fwrite(mp4_ptr-used_bytes, 1, used_bytes, filehandle);
302                                  fclose(filehandle);                                  fclose(filehandle);
303                          }                          }
304                  }                  }
# Line 337  Line 320 
320  /*****************************************************************************  /*****************************************************************************
321   *     Flush decoder buffers   *     Flush decoder buffers
322   ****************************************************************************/   ****************************************************************************/
323          while(delayed_frames--) {  
324            do {
325    
326                  /* Fake vars */                  /* Fake vars */
327                  int used_bytes, isframe;                  int used_bytes;
328                  double dectime;                  double dectime;
329    
330                  /* Decode frame */          do {
331                  dectime = msecond();                  dectime = msecond();
332                  status = dec_main(NULL, out_buffer, -1, &used_bytes, &isframe);                      used_bytes = dec_main(NULL, out_buffer, -1, &xvid_dec_stats);
333                  dectime = msecond() - dectime;                  dectime = msecond() - dectime;
334            }while(used_bytes>=0 && xvid_dec_stats.type <= 0);
335    
336                  if (status) {          if (used_bytes < 0) {   /* XVID_ERR_END */
337                          break;                          break;
338                  }                  }
339    
# Line 356  Line 341 
341                  totaldectime += dectime;                  totaldectime += dectime;
342    
343                  /* Prints some decoding stats */                  /* Prints some decoding stats */
344                  printf("Frame %5d: dectime(ms) =%6.1f, length(bytes) =%7d\n",          printf("Frame %5d: type = %s, dectime(ms) =%6.1f, length(bytes) =%7d\n",
345                             filenr, dectime, used_bytes);                             filenr, type2str(xvid_dec_stats.type), dectime, used_bytes);
346    
347                  /* Save output frame if required */                  /* Save output frame if required */
348                  if (ARG_SAVEDECOUTPUT) {                  if (ARG_SAVEDECOUTPUT) {
# Line 371  Line 356 
356    
357                  filenr++;                  filenr++;
358    
359          }          }while(1);
360    
361  /*****************************************************************************  /*****************************************************************************
362   *     Calculate totals and averages for output, print results   *     Calculate totals and averages for output, print results
# Line 398  Line 383 
383          free(out_buffer);          free(out_buffer);
384          free(mp4_buffer);          free(mp4_buffer);
385    
386          return 0;          return(0);
387  }  }
388    
389  /*****************************************************************************  /*****************************************************************************
# Line 408  Line 393 
393  static void usage()  static void usage()
394  {  {
395    
396          fprintf(stderr, "Usage : xvid_decraw <-w width> <-h height> [OPTIONS]\n");          fprintf(stderr, "Usage : xvid_decraw [OPTIONS]\n");
397          fprintf(stderr, "Options :\n");          fprintf(stderr, "Options :\n");
398          fprintf(stderr, " -asm           : use assembly optimizations (default=disabled)\n");          fprintf(stderr, " -asm           : use assembly optimizations (default=disabled)\n");
399          fprintf(stderr, " -w integer     : frame width ([1.2048])\n");          fprintf(stderr, " -debug         : debug level (debug=0)\n");
         fprintf(stderr, " -h integer     : frame height ([1.2048])\n");  
400          fprintf(stderr, " -i string      : input filename (default=stdin)\n");          fprintf(stderr, " -i string      : input filename (default=stdin)\n");
401          fprintf(stderr, " -t integer     : input data type (raw=0, mp4u=1)\n");          fprintf(stderr, " -d             : save decoder output\n");
402          fprintf(stderr, " -d boolean     : save decoder output (0 False*, !=0 True)\n");          fprintf(stderr, " -m             : save mpeg4 raw stream to individual files\n");
         fprintf(stderr, " -m boolean     : save mpeg4 raw stream to individual files (0 False*, !=0 True)\n");  
403          fprintf(stderr, " -help          : This help message\n");          fprintf(stderr, " -help          : This help message\n");
404          fprintf(stderr, " (* means default)\n");          fprintf(stderr, " (* means default)\n");
405    
# Line 433  Line 416 
416  #ifndef WIN32  #ifndef WIN32
417          struct timeval  tv;          struct timeval  tv;
418          gettimeofday(&tv, 0);          gettimeofday(&tv, 0);
419          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);
420  #else  #else
421          clock_t clk;          clock_t clk;
422          clk = clock();          clk = clock();
423          return clk * 1000 / CLOCKS_PER_SEC;          return(clk * 1000 / CLOCKS_PER_SEC);
424  #endif  #endif
425  }  }
426    
# Line 482  Line 465 
465                  /* Close file */                  /* Close file */
466                  fclose(filehandle);                  fclose(filehandle);
467    
468                  return 0;                  return(0);
469          }          }
470          else          else
471                  return 1;                  return(1);
472  }  }
473    
474  /*****************************************************************************  /*****************************************************************************
# Line 494  Line 477 
477    
478  /* init decoder before first run */  /* init decoder before first run */
479  static int  static int
480  dec_init(int use_assembler)  dec_init(int use_assembler, int debug_level)
481  {  {
482          int xerr;          int ret;
483    
484          XVID_INIT_PARAM xinit;          xvid_gbl_init_t   xvid_gbl_init;
485          XVID_DEC_PARAM xparam;          xvid_dec_create_t xvid_dec_create;
486    
487            /*------------------------------------------------------------------------
488             * XviD core initialization
489             *----------------------------------------------------------------------*/
490    
491            /* Version */
492            xvid_gbl_init.version = XVID_VERSION;
493    
494            /* Assembly setting */
495                  if(use_assembler)                  if(use_assembler)
496  #ifdef ARCH_IS_IA64  #ifdef ARCH_IS_IA64
497                          xinit.cpu_flags = XVID_CPU_FORCE | XVID_CPU_IA64;                  xvid_gbl_init.cpu_flags = XVID_CPU_FORCE | XVID_CPU_IA64;
498  #else  #else
499                          xinit.cpu_flags = 0;          xvid_gbl_init.cpu_flags = 0;
500  #endif  #endif
501                  else                  else
502                          xinit.cpu_flags = XVID_CPU_FORCE;                  xvid_gbl_init.cpu_flags = XVID_CPU_FORCE;
503    
504            xvid_gbl_init.debug = debug_level;
505    
506          xvid_init(NULL, 0, &xinit, NULL);          xvid_global(NULL, 0, &xvid_gbl_init, NULL);
         xparam.width = XDIM;  
         xparam.height = YDIM;  
507    
508          xerr = xvid_decore(NULL, XVID_DEC_CREATE, &xparam, NULL);          /*------------------------------------------------------------------------
509             * XviD encoder initialization
510             *----------------------------------------------------------------------*/
511    
512          dec_handle = xparam.handle;          /* Version */
513            xvid_dec_create.version = XVID_VERSION;
514    
515          return xerr;          /*
516             * Image dimensions -- set to 0, xvidcore will resize when ever it is
517             * needed
518             */
519            xvid_dec_create.width = 0;
520            xvid_dec_create.height = 0;
521    
522            ret = xvid_decore(NULL, XVID_DEC_CREATE, &xvid_dec_create, NULL);
523    
524            dec_handle = xvid_dec_create.handle;
525    
526            return(ret);
527  }  }
528    
529  /* decode one frame  */  /* decode one frame  */
# Line 526  Line 531 
531  dec_main(unsigned char *istream,  dec_main(unsigned char *istream,
532                   unsigned char *ostream,                   unsigned char *ostream,
533                   int istream_size,                   int istream_size,
534                   int *ostream_size,                   xvid_dec_stats_t *xvid_dec_stats)
                  int *notification)  
535  {  {
536    
537          int xerr;          int ret;
538          XVID_DEC_FRAME xframe;  
539                  XVID_DEC_STATS xstats;          xvid_dec_frame_t xvid_dec_frame;
540    
541            /* Set version */
542            xvid_dec_frame.version = XVID_VERSION;
543            xvid_dec_stats->version = XVID_VERSION;
544    
545          xframe.general    = 0;          /* No general flags to set */
546                  xframe.bitstream  = istream;          xvid_dec_frame.general          = 0;
         xframe.length     = istream_size;  
                 xframe.image      = ostream;  
         xframe.stride     = XDIM;  
                 xframe.colorspace = XVID_CSP_I420;  
547    
548                  xerr = xvid_decore(dec_handle, XVID_DEC_DECODE, &xframe, &xstats);          /* Input stream */
549            xvid_dec_frame.bitstream        = istream;
550            xvid_dec_frame.length           = istream_size;
551    
552                  *ostream_size = xframe.length;          /* Output frame structure */
553            xvid_dec_frame.output.plane[0]  = ostream;
554            xvid_dec_frame.output.stride[0] = XDIM;
555            xvid_dec_frame.output.csp       = XVID_CSP_I420;
556    
557                  *notification = xstats.notify;          ret = xvid_decore(dec_handle, XVID_DEC_DECODE, &xvid_dec_frame, xvid_dec_stats);
558    
559          return xerr;          return(ret);
560  }  }
561    
562  /* close decoder to release resources */  /* close decoder to release resources */
563  static int  static int
564  dec_stop()  dec_stop()
565  {  {
566          int xerr;          int ret;
567    
568          xerr = xvid_decore(dec_handle, XVID_DEC_DESTROY, NULL, NULL);          ret = xvid_decore(dec_handle, XVID_DEC_DESTROY, NULL, NULL);
569    
570          return xerr;          return(ret);
571  }  }

Legend:
Removed from v.902  
changed lines
  Added in v.1416

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