[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

revision 886, Fri Feb 21 14:49:29 2003 UTC revision 918, Tue Mar 11 23:39:47 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.7 2003-02-16 05:11:39 suxen_drol Exp $   * $Id: xvid_decraw.c,v 1.7.2.1 2003-03-11 23:39:47 edgomez 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    
# 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;          unsigned char *type       = NULL;
109          int still_left_in_buffer;          int still_left_in_buffer;
110          int delayed_frames;          int delayed_frames;
111            xvid_dec_stats_t xvid_dec_stats;
112    
113          double totaldectime;          double totaldectime;
114    
# Line 137  Line 135 
135                  if (strcmp("-asm", argv[i]) == 0 ) {                  if (strcmp("-asm", argv[i]) == 0 ) {
136                          use_assembler = 1;                          use_assembler = 1;
137                  }                  }
138                  else if (strcmp("-w", argv[i]) == 0 && i < argc - 1 ) {                  else if (strcmp("-d", argv[i]) == 0) {
139                          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]);  
140                  }                  }
141                  else if (strcmp("-i", argv[i]) == 0 && i < argc - 1 ) {                  else if (strcmp("-i", argv[i]) == 0 && i < argc - 1 ) {
142                          i++;                          i++;
143                          ARG_INPUTFILE = argv[i];                          ARG_INPUTFILE = argv[i];
144                  }                  }
145                  else if (strcmp("-m", argv[i]) == 0 && i < argc - 1 ) {                  else if (strcmp("-m", argv[i]) == 0) {
146                          i++;                          ARG_SAVEMPEGSTREAM = 1;
                         ARG_SAVEMPEGSTREAM = atoi(argv[i]);  
147                  }                  }
148                  else if (strcmp("-help", argv[i])) {                  else if (strcmp("-help", argv[i])) {
149                          usage();                          usage();
# Line 172  Line 160 
160   * Values checking   * Values checking
161   ****************************************************************************/   ****************************************************************************/
162    
         if(XDIM <= 0 || XDIM > 2048 || YDIM <= 0 || YDIM > 2048) {  
                 usage();  
                 return -1;  
         }  
   
163          if ( ARG_INPUTFILE == NULL || strcmp(ARG_INPUTFILE, "stdin") == 0) {          if ( ARG_INPUTFILE == NULL || strcmp(ARG_INPUTFILE, "stdin") == 0) {
164                  in_file = stdin;                  in_file = stdin;
165          }          }
# Line 185  Line 168 
168                  in_file = fopen(ARG_INPUTFILE, "rb");                  in_file = fopen(ARG_INPUTFILE, "rb");
169                  if (in_file == NULL) {                  if (in_file == NULL) {
170                          fprintf(stderr, "Error opening input file %s\n", ARG_INPUTFILE);                          fprintf(stderr, "Error opening input file %s\n", ARG_INPUTFILE);
171                          return -1;                          return(-1);
172                  }                  }
173          }          }
174    
# Line 199  Line 182 
182          if (!mp4_buffer)          if (!mp4_buffer)
183                  goto free_all_memory;                  goto free_all_memory;
184    
         /* Memory for frame output */  
         out_buffer = (unsigned char *) malloc(XDIM*YDIM*4);  
         if (!out_buffer)  
                 goto free_all_memory;  
   
   
185  /*****************************************************************************  /*****************************************************************************
186   *        XviD PART  Start   *        XviD PART  Start
187   ****************************************************************************/   ****************************************************************************/
# Line 235  Line 212 
212                  int mp4_size = (mp4_buffer + BUFFER_SIZE - mp4_ptr);                  int mp4_size = (mp4_buffer + BUFFER_SIZE - mp4_ptr);
213                  int used_bytes = 0;                  int used_bytes = 0;
214                  double dectime;                  double dectime;
                 int notification;  
215    
216                  /*                  /*
217                   * 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 240 
240                  }                  }
241    
242    
243                  /* This loop flushes N_VOPS (with vop_coded bit set to 0) */                  /* This loop is needed to handle VOL/NVOP reading */
244                  do {                  do {
245    
246                          /* Decode frame */                          /* Decode frame */
247                          dectime = msecond();                          dectime = msecond();
248                          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);
249                          dectime = msecond() - dectime;                          dectime = msecond() - dectime;
250    
251                          if (status) {                          /* Resize image buffer if needed */
252                                  break;                          if(xvid_dec_stats.type == XVID_TYPE_VOL) {
253    
254                                    /* Free old output buffer*/
255                                    if(out_buffer) free(out_buffer);
256    
257                                    /* Copy witdh and height from the vol structure */
258                                    XDIM = xvid_dec_stats.data.vol.width;
259                                    YDIM = xvid_dec_stats.data.vol.height;
260    
261                                    /* Allocate the new buffer */
262                                    if((out_buffer = (unsigned char*)malloc(XDIM*YDIM*4)) == NULL)
263                                            goto free_all_memory;
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;                          still_left_in_buffer -= 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 != XVID_TYPE_IVOP &&
276                               xvid_dec_stats.type != XVID_TYPE_PVOP &&
277                               xvid_dec_stats.type != XVID_TYPE_BVOP &&
278                               xvid_dec_stats.type != XVID_TYPE_SVOP &&
279                               still_left_in_buffer > 0);
280    
281                  /* Negative buffer would mean we went too far */                  /* Negative buffer would mean we went too far */
282                  if(still_left_in_buffer < 0) break;                  if(still_left_in_buffer < 0) break;
283    
284                  /* Skip when decoder is buffering images or decoding VOL information */                  /* Skip when decoder is buffering images because of bframes */
285                  if(notification != XVID_DEC_VOP) {                  if(xvid_dec_stats.type == XVID_TYPE_NOTHING) {
                         /* It's a delay only if it notifies NOTHING */  
                         if(notification == XVID_DEC_NOTHING)  
286                                  delayed_frames++;                                  delayed_frames++;
287                          continue;                          continue;
288                  }                  }
# Line 300  Line 291 
291                  totaldectime += dectime;                  totaldectime += dectime;
292    
293                  /* Prints some decoding stats */                  /* Prints some decoding stats */
294                  printf("Frame %5d: dectime(ms) =%6.1f, length(bytes) =%7d\n",                  switch(xvid_dec_stats.type) {
295                             filenr, dectime, used_bytes);                  case XVID_TYPE_IVOP:
296                            type = "I";
297                            break;
298                    case XVID_TYPE_PVOP:
299                            type = "P";
300                            break;
301                    case XVID_TYPE_BVOP:
302                            type = "B";
303                            break;
304                    case XVID_TYPE_SVOP:
305                            type = "S";
306                            break;
307                    }
308    
309                    printf("Frame %5d: type = %s, dectime(ms) =%6.1f, length(bytes) =%7d\n",
310                               filenr, type, dectime, used_bytes);
311    
312                  /* Save individual mpeg4 stream if required */                  /* Save individual mpeg4 stream if required */
313                  if (ARG_SAVEMPEGSTREAM) {                  if (ARG_SAVEMPEGSTREAM) {
# Line 337  Line 343 
343  /*****************************************************************************  /*****************************************************************************
344   *     Flush decoder buffers   *     Flush decoder buffers
345   ****************************************************************************/   ****************************************************************************/
346    
347          while(delayed_frames--) {          while(delayed_frames--) {
348    
349                  /* Fake vars */                  /* Fake vars */
350                  int used_bytes, isframe;                  int used_bytes;
351                  double dectime;                  double dectime;
352    
353                  /* Decode frame */                  /* Decode frame */
354                  dectime = msecond();                  dectime = msecond();
355                  status = dec_main(NULL, out_buffer, -1, &used_bytes, &isframe);                  used_bytes = dec_main(NULL, out_buffer, -1, &xvid_dec_stats);
356                  dectime = msecond() - dectime;                  dectime = msecond() - dectime;
357    
                 if (status) {  
                         break;  
                 }  
   
358                  /* Updated data - Count only usefull decode time */                  /* Updated data - Count only usefull decode time */
359                  totaldectime += dectime;                  totaldectime += dectime;
360    
# Line 398  Line 401 
401          free(out_buffer);          free(out_buffer);
402          free(mp4_buffer);          free(mp4_buffer);
403    
404          return 0;          return(0);
405  }  }
406    
407  /*****************************************************************************  /*****************************************************************************
# Line 411  Line 414 
414          fprintf(stderr, "Usage : xvid_decraw <-w width> <-h height> [OPTIONS]\n");          fprintf(stderr, "Usage : xvid_decraw <-w width> <-h height> [OPTIONS]\n");
415          fprintf(stderr, "Options :\n");          fprintf(stderr, "Options :\n");
416          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");  
417          fprintf(stderr, " -i string      : input filename (default=stdin)\n");          fprintf(stderr, " -i string      : input filename (default=stdin)\n");
418          fprintf(stderr, " -t integer     : input data type (raw=0, mp4u=1)\n");          fprintf(stderr, " -t integer     : input data type (raw=0, mp4u=1)\n");
419          fprintf(stderr, " -d boolean     : save decoder output (0 False*, !=0 True)\n");          fprintf(stderr, " -d             : save decoder output\n");
420          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");
421          fprintf(stderr, " -help          : This help message\n");          fprintf(stderr, " -help          : This help message\n");
422          fprintf(stderr, " (* means default)\n");          fprintf(stderr, " (* means default)\n");
423    
# Line 433  Line 434 
434  #ifndef WIN32  #ifndef WIN32
435          struct timeval  tv;          struct timeval  tv;
436          gettimeofday(&tv, 0);          gettimeofday(&tv, 0);
437          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);
438  #else  #else
439          clock_t clk;          clock_t clk;
440          clk = clock();          clk = clock();
441          return clk * 1000 / CLOCKS_PER_SEC;          return(clk * 1000 / CLOCKS_PER_SEC);
442  #endif  #endif
443  }  }
444    
# Line 482  Line 483 
483                  /* Close file */                  /* Close file */
484                  fclose(filehandle);                  fclose(filehandle);
485    
486                  return 0;                  return(0);
487          }          }
488          else          else
489                  return 1;                  return(1);
490  }  }
491    
492  /*****************************************************************************  /*****************************************************************************
# Line 496  Line 497 
497  static int  static int
498  dec_init(int use_assembler)  dec_init(int use_assembler)
499  {  {
500          int xerr;          int ret;
501    
502            xvid_gbl_init_t   xvid_gbl_init;
503            xvid_dec_create_t xvid_dec_create;
504    
505            /*------------------------------------------------------------------------
506             * XviD core initialization
507             *----------------------------------------------------------------------*/
508    
509          XVID_INIT_PARAM xinit;          /* Version */
510          XVID_DEC_PARAM xparam;          xvid_gbl_init.version = XVID_VERSION;
511    
512            /* Assembly setting */
513                  if(use_assembler)                  if(use_assembler)
514  #ifdef ARCH_IS_IA64  #ifdef ARCH_IS_IA64
515                          xinit.cpu_flags = XVID_CPU_FORCE | XVID_CPU_IA64;                  xvid_gbl_init.cpu_flags = XVID_CPU_FORCE | XVID_CPU_IA64;
516  #else  #else
517                          xinit.cpu_flags = 0;          xvid_gbl_init.cpu_flags = 0;
518  #endif  #endif
519                  else                  else
520                          xinit.cpu_flags = XVID_CPU_FORCE;                  xvid_gbl_init.cpu_flags = XVID_CPU_FORCE;
521    
522            xvid_global(NULL, 0, &xvid_gbl_init, NULL);
523    
524            /*------------------------------------------------------------------------
525             * XviD encoder initialization
526             *----------------------------------------------------------------------*/
527    
528          xvid_init(NULL, 0, &xinit, NULL);          /* Version */
529          xparam.width = XDIM;          xvid_dec_create.version = XVID_VERSION;
         xparam.height = YDIM;  
530    
531          xerr = xvid_decore(NULL, XVID_DEC_CREATE, &xparam, NULL);          /*
532             * Image dimensions -- set to 0, xvidcore will resize when ever it is
533             * needed
534             */
535            xvid_dec_create.width = 0;
536            xvid_dec_create.height = 0;
537    
538            ret = xvid_decore(NULL, XVID_DEC_CREATE, &xvid_dec_create, NULL);
539    
540          dec_handle = xparam.handle;          dec_handle = xvid_dec_create.handle;
541    
542          return xerr;          return(ret);
543  }  }
544    
545  /* decode one frame  */  /* decode one frame  */
# Line 526  Line 547 
547  dec_main(unsigned char *istream,  dec_main(unsigned char *istream,
548                   unsigned char *ostream,                   unsigned char *ostream,
549                   int istream_size,                   int istream_size,
550                   int *ostream_size,                   xvid_dec_stats_t *xvid_dec_stats)
                  int *notification)  
551  {  {
552    
553          int xerr;          int ret;
554          XVID_DEC_FRAME xframe;  
555                  XVID_DEC_STATS xstats;          xvid_dec_frame_t xvid_dec_frame;
556    
557            /* Set version */
558            xvid_dec_frame.version = XVID_VERSION;
559            xvid_dec_stats->version = XVID_VERSION;
560    
561          xframe.general    = 0;          /* No general flags to set */
562                  xframe.bitstream  = istream;          xvid_dec_frame.general          = 0;
         xframe.length     = istream_size;  
                 xframe.image      = ostream;  
         xframe.stride     = XDIM;  
                 xframe.colorspace = XVID_CSP_YV12;  
563    
564                  xerr = xvid_decore(dec_handle, XVID_DEC_DECODE, &xframe, &xstats);          /* Input stream */
565            xvid_dec_frame.bitstream        = istream;
566            xvid_dec_frame.length           = istream_size;
567    
568                  *ostream_size = xframe.length;          /* Output frame structure */
569            xvid_dec_frame.output.plane[0]  = ostream;
570            xvid_dec_frame.output.stride[0] = XDIM;
571            xvid_dec_frame.output.csp       = XVID_CSP_I420;
572    
573                  *notification = xstats.notify;          ret = xvid_decore(dec_handle, XVID_DEC_DECODE, &xvid_dec_frame, xvid_dec_stats);
574    
575          return xerr;          return(ret);
576  }  }
577    
578  /* close decoder to release resources */  /* close decoder to release resources */
579  static int  static int
580  dec_stop()  dec_stop()
581  {  {
582          int xerr;          int ret;
583    
584          xerr = xvid_decore(dec_handle, XVID_DEC_DESTROY, NULL, NULL);          ret = xvid_decore(dec_handle, XVID_DEC_DESTROY, NULL, NULL);
585    
586          return xerr;          return(ret);
587  }  }

Legend:
Removed from v.886  
changed lines
  Added in v.918

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