[svn] / branches / dev-api-4 / xvidcore / examples / xvid_decraw.c Repository:
ViewVC logotype

Diff of /branches/dev-api-4/xvidcore/examples/xvid_decraw.c

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

trunk/xvidcore/examples/xvid_decraw.c revision 851, Sat Feb 15 15:22:19 2003 UTC branches/dev-api-4/xvidcore/examples/xvid_decraw.c revision 945, Wed Mar 26 10:29:51 2003 UTC
# Line 19  Line 19 
19   *  along with this program; if not, write to the Free Software   *  along with this program; if not, write to the Free Software
20   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
21   *   *
22   * $Id: xvid_decraw.c,v 1.6 2003-02-15 15:22:17 edgomez Exp $   * $Id: xvid_decraw.c,v 1.7.2.2 2003-03-26 10:29:10 suxen_drol Exp $
23   *   *
24   ****************************************************************************/   ****************************************************************************/
25    
# Line 41  Line 41 
41   * Usage : xvid_decraw <-w width> <-h height> [OPTIONS]   * Usage : xvid_decraw <-w width> <-h height> [OPTIONS]
42   * Options :   * Options :
43   *  -asm           : use assembly optimizations (default=disabled)   *  -asm           : use assembly optimizations (default=disabled)
  *  -w integer     : frame width ([1.2048])  
  *  -h integer     : frame height ([1.2048])  
44   *  -i string      : input filename (default=stdin)   *  -i string      : input filename (default=stdin)
45   *  -t integer     : input data type (raw=0, mp4u=1)   *  -t integer     : input data type (raw=0, mp4u=1)
46   *  -d boolean     : save decoder output (0 False*, !=0 True)   *  -d             : save decoder output (0 False*, !=0 True)
47   *  -m boolean     : save mpeg4 raw stream to single files (0 False*, !=0 True)   *  -m             : save mpeg4 raw stream to single files (0 False*, !=0 True)
48   *  -help          : This help message   *  -help          : This help message
49   * (* means default)   * (* means default)
50   *   *
# Line 81  Line 79 
79  static char filepath[256] = "./";  static char filepath[256] = "./";
80  static void *dec_handle = NULL;  static void *dec_handle = NULL;
81    
82  # define BUFFER_SIZE 10*XDIM*YDIM  # define BUFFER_SIZE (2*1024*1024)
83    
84  /*****************************************************************************  /*****************************************************************************
85   *               Local prototypes   *               Local prototypes
# Line 94  Line 92 
92  static int dec_main(unsigned char *istream,  static int dec_main(unsigned char *istream,
93                                          unsigned char *ostream,                                          unsigned char *ostream,
94                                          int istream_size,                                          int istream_size,
95                                          int *ostream_size,                                          xvid_dec_stats_t *xvid_dec_stats);
                                         int *isframe);  
96  static int dec_stop();  static int dec_stop();
97  static void usage();  static void usage();
98    
99    
100    const char * type2str(int type)
101    {
102        if (type==XVID_TYPE_IVOP)
103            return "I";
104        if (type==XVID_TYPE_PVOP)
105            return "P";
106        if (type==XVID_TYPE_BVOP)
107            return "B";
108        return "S";
109    }
110    
111  /*****************************************************************************  /*****************************************************************************
112   *        Main program   *        Main program
113   ****************************************************************************/   ****************************************************************************/
# Line 108  Line 117 
117          unsigned char *mp4_buffer = NULL;          unsigned char *mp4_buffer = NULL;
118          unsigned char *mp4_ptr    = NULL;          unsigned char *mp4_ptr    = NULL;
119          unsigned char *out_buffer = NULL;          unsigned char *out_buffer = NULL;
         int bigendian = 0;  
120          int still_left_in_buffer;          int still_left_in_buffer;
121          int delayed_frames;          xvid_dec_stats_t xvid_dec_stats;
122    
123          double totaldectime;          double totaldectime;
124    
# Line 137  Line 145 
145                  if (strcmp("-asm", argv[i]) == 0 ) {                  if (strcmp("-asm", argv[i]) == 0 ) {
146                          use_assembler = 1;                          use_assembler = 1;
147                  }                  }
148                  else if (strcmp("-w", argv[i]) == 0 && i < argc - 1 ) {                  else if (strcmp("-d", argv[i]) == 0) {
149                          i++;                          ARG_SAVEDECOUTPUT = 1;
                         XDIM = atoi(argv[i]);  
                 }  
                 else if (strcmp("-h", argv[i]) == 0 && i < argc - 1 ) {  
                         i++;  
                         YDIM = atoi(argv[i]);  
                 }  
                 else if (strcmp("-d", argv[i]) == 0 && i < argc - 1 ) {  
                         i++;  
                         ARG_SAVEDECOUTPUT = atoi(argv[i]);  
150                  }                  }
151                  else if (strcmp("-i", argv[i]) == 0 && i < argc - 1 ) {                  else if (strcmp("-i", argv[i]) == 0 && i < argc - 1 ) {
152                          i++;                          i++;
153                          ARG_INPUTFILE = argv[i];                          ARG_INPUTFILE = argv[i];
154                  }                  }
155                  else if (strcmp("-m", argv[i]) == 0 && i < argc - 1 ) {                  else if (strcmp("-m", argv[i]) == 0) {
156                          i++;                          ARG_SAVEMPEGSTREAM = 1;
                         ARG_SAVEMPEGSTREAM = atoi(argv[i]);  
157                  }                  }
158                  else if (strcmp("-help", argv[i])) {                  else if (strcmp("-help", argv[i])) {
159                          usage();                          usage();
# Line 172  Line 170 
170   * Values checking   * Values checking
171   ****************************************************************************/   ****************************************************************************/
172    
         if(XDIM <= 0 || XDIM > 2048 || YDIM <= 0 || YDIM > 2048) {  
                 usage();  
                 return -1;  
         }  
   
173          if ( ARG_INPUTFILE == NULL || strcmp(ARG_INPUTFILE, "stdin") == 0) {          if ( ARG_INPUTFILE == NULL || strcmp(ARG_INPUTFILE, "stdin") == 0) {
174                  in_file = stdin;                  in_file = stdin;
175          }          }
# Line 185  Line 178 
178                  in_file = fopen(ARG_INPUTFILE, "rb");                  in_file = fopen(ARG_INPUTFILE, "rb");
179                  if (in_file == NULL) {                  if (in_file == NULL) {
180                          fprintf(stderr, "Error opening input file %s\n", ARG_INPUTFILE);                          fprintf(stderr, "Error opening input file %s\n", ARG_INPUTFILE);
181                          return -1;                          return(-1);
182                  }                  }
183          }          }
184    
# Line 199  Line 192 
192          if (!mp4_buffer)          if (!mp4_buffer)
193                  goto free_all_memory;                  goto free_all_memory;
194    
         /* Memory for frame output */  
         out_buffer = (unsigned char *) malloc(XDIM*YDIM*4);  
         if (!out_buffer)  
                 goto free_all_memory;  
   
   
195  /*****************************************************************************  /*****************************************************************************
196   *        XviD PART  Start   *        XviD PART  Start
197   ****************************************************************************/   ****************************************************************************/
# Line 227  Line 214 
214          totaldectime = 0;          totaldectime = 0;
215          totalsize = 0;          totalsize = 0;
216          filenr = 0;          filenr = 0;
         delayed_frames = 0;  
217          mp4_ptr = mp4_buffer;          mp4_ptr = mp4_buffer;
218    
219          do {          do {
# Line 235  Line 221 
221                  int mp4_size = (mp4_buffer + BUFFER_SIZE - mp4_ptr);                  int mp4_size = (mp4_buffer + BUFFER_SIZE - mp4_ptr);
222                  int used_bytes = 0;                  int used_bytes = 0;
223                  double dectime;                  double dectime;
                 int notification;  
224    
225                  /*                  /*
226                   * 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
# Line 264  Line 249 
249                  }                  }
250    
251    
252                  /* This loop flushes N_VOPS (with vop_coded bit set to 0) */                  /* This loop is needed to handle VOL/NVOP reading */
253                  do {                  do {
254    
255                          /* Decode frame */                          /* Decode frame */
256                          dectime = msecond();                          dectime = msecond();
257                          status = dec_main(mp4_ptr, out_buffer, mp4_size, &used_bytes, &notification);                          used_bytes = dec_main(mp4_ptr, out_buffer, mp4_size, &xvid_dec_stats);
258                          dectime = msecond() - dectime;                          dectime = msecond() - dectime;
259    
260                          if (status) {                          /* Resize image buffer if needed */
261                                  break;                          if(xvid_dec_stats.type == XVID_TYPE_VOL) {
262    
263                                    /* Free old output buffer*/
264                                    if(out_buffer) free(out_buffer);
265    
266                                    /* Copy witdh and height from the vol structure */
267                                    XDIM = xvid_dec_stats.data.vol.width;
268                                    YDIM = xvid_dec_stats.data.vol.height;
269    
270                                    /* Allocate the new buffer */
271                                    if((out_buffer = (unsigned char*)malloc(XDIM*YDIM*4)) == NULL)
272                                            goto free_all_memory;
273                          }                          }
274    
275                          /* Update buffer pointers */                          /* Update buffer pointers */
276                            if(used_bytes > 0) {
277                          mp4_ptr += used_bytes;                          mp4_ptr += used_bytes;
278                          still_left_in_buffer -= used_bytes;                          still_left_in_buffer -= used_bytes;
279    
280                          /* Total size */                          /* Total size */
281                          totalsize += used_bytes;                          totalsize += used_bytes;
282                            }
283    
284                  }while(used_bytes <= 7 && still_left_in_buffer > 0); /* <= 7 bytes is a NVOPS */                  }while(xvid_dec_stats.type <= 0 && still_left_in_buffer > 0);
285    
286                  /* Negative buffer would mean we went too far */                  /* Negative buffer would mean we went too far */
287                  if(still_left_in_buffer < 0) break;          if(still_left_in_buffer <= 0)
288                break;
                 /* Skip when decoder is buffering images or decoding VOL information */  
                 if(notification != XVID_DEC_VOP) {  
                         /* It's a delay only if it notifies NOTHING */  
                         if(notification == XVID_DEC_NOTHING)  
                                 delayed_frames++;  
                         continue;  
                 }  
289    
290                  /* Updated data - Count only usefull decode time */                  /* Updated data - Count only usefull decode time */
291                  totaldectime += dectime;                  totaldectime += dectime;
292    
293                  /* Prints some decoding stats */  
294                  printf("Frame %5d: dectime(ms) =%6.1f, length(bytes) =%7d\n",          printf("Frame %5d: type = %s, dectime(ms) =%6.1f, length(bytes) =%7d\n",
295                             filenr, dectime, used_bytes);                             filenr, type2str(xvid_dec_stats.type), dectime, used_bytes);
296    
297                  /* Save individual mpeg4 stream if required */                  /* Save individual mpeg4 stream if required */
298                  if (ARG_SAVEMPEGSTREAM) {                  if (ARG_SAVEMPEGSTREAM) {
# Line 337  Line 328 
328  /*****************************************************************************  /*****************************************************************************
329   *     Flush decoder buffers   *     Flush decoder buffers
330   ****************************************************************************/   ****************************************************************************/
331          while(delayed_frames--) {  
332            do {
333    
334                  /* Fake vars */                  /* Fake vars */
335                  int used_bytes, isframe;                  int used_bytes;
336                  double dectime;                  double dectime;
337    
338                  /* Decode frame */          do {
339                  dectime = msecond();                  dectime = msecond();
340                  status = dec_main(NULL, out_buffer, -1, &used_bytes, &isframe);                      used_bytes = dec_main(NULL, out_buffer, -1, &xvid_dec_stats);
341                  dectime = msecond() - dectime;                  dectime = msecond() - dectime;
342            }while(used_bytes>=0 && xvid_dec_stats.type <= 0);
343    
344                  if (status) {          if (used_bytes < 0) {   /* XVID_ERR_END */
345                          break;                          break;
346                  }                  }
347    
# Line 356  Line 349 
349                  totaldectime += dectime;                  totaldectime += dectime;
350    
351                  /* Prints some decoding stats */                  /* Prints some decoding stats */
352                  printf("Frame %5d: dectime(ms) =%6.1f, length(bytes) =%7d\n",          printf("Frame %5d: type = %s, dectime(ms) =%6.1f, length(bytes) =%7d\n",
353                             filenr, dectime, used_bytes);                             filenr, type2str(xvid_dec_stats.type), dectime, used_bytes);
354    
355                  /* Save output frame if required */                  /* Save output frame if required */
356                  if (ARG_SAVEDECOUTPUT) {                  if (ARG_SAVEDECOUTPUT) {
# Line 371  Line 364 
364    
365                  filenr++;                  filenr++;
366    
367          }          }while(1);
368    
369  /*****************************************************************************  /*****************************************************************************
370   *     Calculate totals and averages for output, print results   *     Calculate totals and averages for output, print results
# Line 398  Line 391 
391          free(out_buffer);          free(out_buffer);
392          free(mp4_buffer);          free(mp4_buffer);
393    
394          return 0;          return(0);
395  }  }
396    
397  /*****************************************************************************  /*****************************************************************************
# Line 411  Line 404 
404          fprintf(stderr, "Usage : xvid_decraw <-w width> <-h height> [OPTIONS]\n");          fprintf(stderr, "Usage : xvid_decraw <-w width> <-h height> [OPTIONS]\n");
405          fprintf(stderr, "Options :\n");          fprintf(stderr, "Options :\n");
406          fprintf(stderr, " -asm           : use assembly optimizations (default=disabled)\n");          fprintf(stderr, " -asm           : use assembly optimizations (default=disabled)\n");
         fprintf(stderr, " -w integer     : frame width ([1.2048])\n");  
         fprintf(stderr, " -h integer     : frame height ([1.2048])\n");  
407          fprintf(stderr, " -i string      : input filename (default=stdin)\n");          fprintf(stderr, " -i string      : input filename (default=stdin)\n");
408          fprintf(stderr, " -t integer     : input data type (raw=0, mp4u=1)\n");          fprintf(stderr, " -t integer     : input data type (raw=0, mp4u=1)\n");
409          fprintf(stderr, " -d boolean     : save decoder output (0 False*, !=0 True)\n");          fprintf(stderr, " -d             : save decoder output\n");
410          fprintf(stderr, " -m boolean     : save mpeg4 raw stream to individual files (0 False*, !=0 True)\n");          fprintf(stderr, " -m             : save mpeg4 raw stream to individual files\n");
411          fprintf(stderr, " -help          : This help message\n");          fprintf(stderr, " -help          : This help message\n");
412          fprintf(stderr, " (* means default)\n");          fprintf(stderr, " (* means default)\n");
413    
# Line 433  Line 424 
424  #ifndef WIN32  #ifndef WIN32
425          struct timeval  tv;          struct timeval  tv;
426          gettimeofday(&tv, 0);          gettimeofday(&tv, 0);
427          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);
428  #else  #else
429          clock_t clk;          clock_t clk;
430          clk = clock();          clk = clock();
431          return clk * 1000 / CLOCKS_PER_SEC;          return(clk * 1000 / CLOCKS_PER_SEC);
432  #endif  #endif
433  }  }
434    
# Line 482  Line 473 
473                  /* Close file */                  /* Close file */
474                  fclose(filehandle);                  fclose(filehandle);
475    
476                  return 0;                  return(0);
477          }          }
478          else          else
479                  return 1;                  return(1);
480  }  }
481    
482  /*****************************************************************************  /*****************************************************************************
# Line 496  Line 487 
487  static int  static int
488  dec_init(int use_assembler)  dec_init(int use_assembler)
489  {  {
490          int xerr;          int ret;
491    
492            xvid_gbl_init_t   xvid_gbl_init;
493            xvid_dec_create_t xvid_dec_create;
494    
495          XVID_INIT_PARAM xinit;          /*------------------------------------------------------------------------
496          XVID_DEC_PARAM xparam;           * XviD core initialization
497             *----------------------------------------------------------------------*/
498    
499            /* Version */
500            xvid_gbl_init.version = XVID_VERSION;
501    
502            /* Assembly setting */
503                  if(use_assembler)                  if(use_assembler)
504  #ifdef ARCH_IA64  #ifdef ARCH_IS_IA64
505                          xinit.cpu_flags = XVID_CPU_FORCE | XVID_CPU_IA64;                  xvid_gbl_init.cpu_flags = XVID_CPU_FORCE | XVID_CPU_IA64;
506  #else  #else
507                          xinit.cpu_flags = 0;          xvid_gbl_init.cpu_flags = 0;
508  #endif  #endif
509                  else                  else
510                          xinit.cpu_flags = XVID_CPU_FORCE;                  xvid_gbl_init.cpu_flags = XVID_CPU_FORCE;
511    
512          xvid_init(NULL, 0, &xinit, NULL);          xvid_global(NULL, 0, &xvid_gbl_init, NULL);
         xparam.width = XDIM;  
         xparam.height = YDIM;  
513    
514          xerr = xvid_decore(NULL, XVID_DEC_CREATE, &xparam, NULL);          /*------------------------------------------------------------------------
515             * XviD encoder initialization
516             *----------------------------------------------------------------------*/
517    
518          dec_handle = xparam.handle;          /* Version */
519            xvid_dec_create.version = XVID_VERSION;
520    
521          return xerr;          /*
522             * Image dimensions -- set to 0, xvidcore will resize when ever it is
523             * needed
524             */
525            xvid_dec_create.width = 0;
526            xvid_dec_create.height = 0;
527    
528            ret = xvid_decore(NULL, XVID_DEC_CREATE, &xvid_dec_create, NULL);
529    
530            dec_handle = xvid_dec_create.handle;
531    
532            return(ret);
533  }  }
534    
535  /* decode one frame  */  /* decode one frame  */
# Line 526  Line 537 
537  dec_main(unsigned char *istream,  dec_main(unsigned char *istream,
538                   unsigned char *ostream,                   unsigned char *ostream,
539                   int istream_size,                   int istream_size,
540                   int *ostream_size,                   xvid_dec_stats_t *xvid_dec_stats)
                  int *notification)  
541  {  {
542    
543          int xerr;          int ret;
544          XVID_DEC_FRAME xframe;  
545                  XVID_DEC_STATS xstats;          xvid_dec_frame_t xvid_dec_frame;
546    
547            /* Set version */
548            xvid_dec_frame.version = XVID_VERSION;
549            xvid_dec_stats->version = XVID_VERSION;
550    
551          xframe.general    = 0;          /* No general flags to set */
552                  xframe.bitstream  = istream;          xvid_dec_frame.general          = 0;
         xframe.length     = istream_size;  
                 xframe.image      = ostream;  
         xframe.stride     = XDIM;  
                 xframe.colorspace = XVID_CSP_YV12;  
553    
554                  xerr = xvid_decore(dec_handle, XVID_DEC_DECODE, &xframe, &xstats);          /* Input stream */
555            xvid_dec_frame.bitstream        = istream;
556            xvid_dec_frame.length           = istream_size;
557    
558                  *ostream_size = xframe.length;          /* Output frame structure */
559            xvid_dec_frame.output.plane[0]  = ostream;
560            xvid_dec_frame.output.stride[0] = XDIM;
561            xvid_dec_frame.output.csp       = XVID_CSP_I420;
562    
563                  *notification = xstats.notify;          ret = xvid_decore(dec_handle, XVID_DEC_DECODE, &xvid_dec_frame, xvid_dec_stats);
564    
565          return xerr;          return(ret);
566  }  }
567    
568  /* close decoder to release resources */  /* close decoder to release resources */
569  static int  static int
570  dec_stop()  dec_stop()
571  {  {
572          int xerr;          int ret;
573    
574          xerr = xvid_decore(dec_handle, XVID_DEC_DESTROY, NULL, NULL);          ret = xvid_decore(dec_handle, XVID_DEC_DESTROY, NULL, NULL);
575    
576          return xerr;          return(ret);
577  }  }

Legend:
Removed from v.851  
changed lines
  Added in v.945

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