[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.1.2.4, Sun Feb 9 06:46:54 2003 UTC revision 1.4, Sat Sep 28 14:27:16 2002 UTC
# Line 3  Line 3 
3   *  XVID MPEG-4 VIDEO CODEC   *  XVID MPEG-4 VIDEO CODEC
4   *  - Console based decoding test application  -   *  - Console based decoding test application  -
5   *   *
6   *  Copyright(C) 2002-2003 Christoph Lampert   *  Copyright(C) 2002 Christoph Lampert
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 56  Line 56 
56  #include <stdlib.h>  #include <stdlib.h>
57  #include <string.h>  #include <string.h>
58  #include <math.h>  #include <math.h>
59  #ifndef WIN32  #ifndef _MSC_VER
60  #include <sys/time.h>  #include <sys/time.h>
61  #else  #else
62  #include <time.h>  #include <time.h>
# Line 75  Line 75 
75  static int YDIM = 0;  static int YDIM = 0;
76  static int ARG_SAVEDECOUTPUT = 0;  static int ARG_SAVEDECOUTPUT = 0;
77  static int ARG_SAVEMPEGSTREAM = 0;  static int ARG_SAVEMPEGSTREAM = 0;
78    static int ARG_STREAMTYPE = 0;
79  static char *ARG_INPUTFILE = NULL;  static char *ARG_INPUTFILE = NULL;
80    
81    
# Line 83  Line 84 
84    
85  # define BUFFER_SIZE 10*XDIM*YDIM  # define BUFFER_SIZE 10*XDIM*YDIM
86    
87    #define LONG_PACK(a,b,c,d) ((long) (((long)(a))<<24) | (((long)(b))<<16) | \
88                                       (((long)(c))<<8)  |((long)(d)))
89    
90    #define SWAP(a) ( (((a)&0x000000ff)<<24) | (((a)&0x0000ff00)<<8) | \
91                      (((a)&0x00ff0000)>>8)  | (((a)&0xff000000)>>24) )
92    
93  /*****************************************************************************  /*****************************************************************************
94   *               Local prototypes   *               Local prototypes
95   ****************************************************************************/   ****************************************************************************/
# Line 94  Line 101 
101  static int dec_main(unsigned char *istream,  static int dec_main(unsigned char *istream,
102                                          unsigned char *ostream,                                          unsigned char *ostream,
103                                          int istream_size,                                          int istream_size,
104                                          int *ostream_size,                      int *ostream_size);
                                         int *isframe);  
105  static int dec_stop();  static int dec_stop();
106  static void usage();  static void usage();
107    
# Line 109  Line 115 
115          unsigned char *mp4_ptr    = NULL;          unsigned char *mp4_ptr    = NULL;
116          unsigned char *out_buffer = NULL;          unsigned char *out_buffer = NULL;
117          int bigendian = 0;          int bigendian = 0;
         int still_left_in_buffer;  
         int delayed_frames;  
118    
119          double totaldectime;          double totaldectime;
120    
# Line 126  Line 130 
130          int i;          int i;
131    
132          printf("xvid_decraw - raw mpeg4 bitstream decoder ");          printf("xvid_decraw - raw mpeg4 bitstream decoder ");
133          printf("written by Christoph Lampert 2002-2003\n\n");          printf("written by Christoph Lampert 2002\n\n");
134    
135  /*****************************************************************************  /*****************************************************************************
136   * Command line parsing   * Command line parsing
# Line 157  Line 161 
161                          i++;                          i++;
162                          ARG_SAVEMPEGSTREAM = atoi(argv[i]);                          ARG_SAVEMPEGSTREAM = atoi(argv[i]);
163                  }                  }
164                    else if (strcmp("-t", argv[i]) == 0 && i < argc - 1 ) {
165                            i++;
166                            ARG_STREAMTYPE = atoi(argv[i]);
167                    }
168                  else if (strcmp("-help", argv[i])) {                  else if (strcmp("-help", argv[i])) {
169                          usage();                          usage();
170                          return(0);                          return(0);
# Line 221  Line 229 
229   *                               Main loop   *                               Main loop
230   ****************************************************************************/   ****************************************************************************/
231    
232          /* Fill the buffer */          totalsize = LONG_PACK('M','P','4','U');
233          still_left_in_buffer = fread(mp4_buffer, 1, BUFFER_SIZE, in_file);          mp4_ptr = (unsigned char *)&totalsize;
234            if(*mp4_ptr == 'M')
235                    bigendian = 1;
236            else
237                    bigendian = 0;
238    
239            if(ARG_STREAMTYPE) {
240    
241                    unsigned char header[4];
242    
243                    /* MP4U format  : read header */
244                    if(feof(in_file))
245                            goto release_all;
246                    fread(header, 4, 1, in_file);
247    
248                    if(header[0] != 'M' || header[1] != 'P' ||
249                       header[2] != '4' || header[3] != 'U') {
250                            fprintf(stderr, "Error, this not a mp4u container file\n");
251                            goto release_all;
252                    }
253    
254            }
255            else {
256                    fread(mp4_buffer, BUFFER_SIZE, 1, in_file);
257            }
258    
259          totaldectime = 0;          totaldectime = 0;
260          totalsize = 0;          totalsize = 0;
261          filenr = 0;          filenr = 0;
         delayed_frames = 0;  
262          mp4_ptr = mp4_buffer;          mp4_ptr = mp4_buffer;
263    
264          do {          do {
# Line 235  Line 266 
266                  int mp4_size = (mp4_buffer + BUFFER_SIZE - mp4_ptr);                  int mp4_size = (mp4_buffer + BUFFER_SIZE - mp4_ptr);
267                  int used_bytes = 0;                  int used_bytes = 0;
268                  double dectime;                  double dectime;
269                  int notification;  
270                    /* Read data from input file */
271                    if(ARG_STREAMTYPE) {
272    
273                            /* MP4U container */
274    
275                            /* Read stream size first */
276                            if(feof(in_file))
277                                    break;
278                            fread(&mp4_size, sizeof(long), 1, in_file);
279    
280                            /* Mp4U container is big endian */
281                            if(!bigendian)
282                                    mp4_size = SWAP(mp4_size);
283    
284                            /* Read mp4_size_bytes */
285                            if(feof(in_file))
286                                    break;
287                            fread(mp4_buffer, mp4_size, 1, in_file);
288    
289                  /*                  /*
290                   * If the buffer is half empty or there are no more bytes in it                           * When reading mp4u, we don't have to care about buffer
291                   * then fill it.                           * filling as we know exactly how much bytes there are in
292                             * next frame
293                   */                   */
294                  if (mp4_ptr > mp4_buffer + BUFFER_SIZE/2 ||                          mp4_ptr = mp4_buffer;
295                          still_left_in_buffer <= 0) {  
296                    }
297                    else {
298    
299                            /* Real raw stream */
300    
301                            /* buffer more than half empty -> Fill it */
302                            if (mp4_ptr > mp4_buffer + BUFFER_SIZE/2) {
303                          int rest = (mp4_buffer + BUFFER_SIZE - mp4_ptr);                          int rest = (mp4_buffer + BUFFER_SIZE - mp4_ptr);
304    
305                          /* Move data if needed */                          /* Move data if needed */
# Line 255  Line 312 
312                          /* read new data */                          /* read new data */
313                          if(feof(in_file))                          if(feof(in_file))
314                                  break;                                  break;
315                                    fread(mp4_buffer + rest, BUFFER_SIZE - rest, 1, in_file);
                         still_left_in_buffer = fread(mp4_buffer + rest,  
                                                                                  1,  
                                                                                  BUFFER_SIZE - rest,  
                                                                                  in_file);  
316    
317                  }                  }
318    
319                    }
                 /* This loop flushes N_VOPS (with vop_coded bit set to 0) */  
                 do {  
320    
321                          /* Decode frame */                          /* Decode frame */
322                          dectime = msecond();                          dectime = msecond();
323                          status = dec_main(mp4_ptr, out_buffer, mp4_size, &used_bytes, &notification);                  status = dec_main(mp4_ptr, out_buffer, mp4_size, &used_bytes);
324                          dectime = msecond() - dectime;                          dectime = msecond() - dectime;
325    
326                          if (status) {                          if (status) {
327                                  break;                                  break;
328                          }                          }
329    
330                          /* Update buffer pointers */                  /*
331                     * Only needed for real raw stream, mp4u uses
332                     * mp4_ptr = mp4_buffer for each frame
333                     */
334                          mp4_ptr += used_bytes;                          mp4_ptr += used_bytes;
                         still_left_in_buffer -= used_bytes;  
335    
336                          /* Total size */                  /* Updated data */
337                          totalsize += used_bytes;                          totalsize += used_bytes;
   
                 }while(used_bytes <= 7 && still_left_in_buffer > 0); /* <= 7 bytes is a NVOPS */  
   
                 /* Negative buffer would mean we went too far */  
                 if(still_left_in_buffer < 0) 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;  
                 }  
   
                 /* Updated data - Count only usefull decode time */  
338                  totaldectime += dectime;                  totaldectime += dectime;
339    
340                  /* Prints some decoding stats */                  /* Prints some decoding stats */
341                  printf("Frame %5d: dectime(ms) =%6.1f, length(bytes) =%7d\n",                  printf("Frame %5d: dectime =%6.1f ms length=%7d bytes \n",
342                             filenr, dectime, used_bytes);                             filenr, dectime, used_bytes);
343    
344                  /* Save individual mpeg4 stream if required */                  /* Save individual mpeg4 strean if required */
345                  if (ARG_SAVEMPEGSTREAM) {                  if (ARG_SAVEMPEGSTREAM) {
346                          FILE *filehandle = NULL;                          FILE *filehandle = NULL;
347    
# Line 315  Line 353 
353                                                  filename);                                                  filename);
354                          }                          }
355                          else {                          else {
356                                  fwrite(mp4_buffer, 1, used_bytes, filehandle);                                  fwrite(mp4_buffer, used_bytes, 1, filehandle);
357                                  fclose(filehandle);                                  fclose(filehandle);
358                          }                          }
359                  }                  }
360    
361    
362                  /* Save output frame if required */                  /* Save output frame if required */
363                  if (ARG_SAVEDECOUTPUT) {                  if (ARG_SAVEDECOUTPUT) {
364                          sprintf(filename, "%sdec%05d.pgm", filepath, filenr);                          sprintf(filename, "%sdec%05d.pgm", filepath, filenr);
# Line 334  Line 373 
373    
374          } while ( (status>=0) && (filenr<ABS_MAXFRAMENR));          } while ( (status>=0) && (filenr<ABS_MAXFRAMENR));
375    
 /*****************************************************************************  
  *     Flush decoder buffers  
  ****************************************************************************/  
         while(delayed_frames--) {  
   
                 /* Fake vars */  
                 int used_bytes, isframe;  
                 double dectime;  
   
                 /* Decode frame */  
                 dectime = msecond();  
                 status = dec_main(NULL, out_buffer, -1, &used_bytes, &isframe);  
                 dectime = msecond() - dectime;  
   
                 if (status) {  
                         break;  
                 }  
   
                 /* Updated data - Count only usefull decode time */  
                 totaldectime += dectime;  
   
                 /* Prints some decoding stats */  
                 printf("Frame %5d: dectime(ms) =%6.1f, length(bytes) =%7d\n",  
                            filenr, dectime, used_bytes);  
   
                 /* Save output frame if required */  
                 if (ARG_SAVEDECOUTPUT) {  
                         sprintf(filename, "%sdec%05d.pgm", filepath, filenr);  
                         if(write_pgm(filename,out_buffer)) {  
                                 fprintf(stderr,  
                                                 "Error writing decoded PGM frame %s\n",  
                                                 filename);  
                         }  
                 }  
   
                 filenr++;  
   
         }  
376    
377  /*****************************************************************************  /*****************************************************************************
378   *     Calculate totals and averages for output, print results   *     Calculate totals and averages for output, print results
# Line 380  Line 381 
381          totalsize    /= filenr;          totalsize    /= filenr;
382          totaldectime /= filenr;          totaldectime /= filenr;
383    
384          printf("Avg: dectime(ms) =%7.2f, fps =%7.2f, length(bytes) =%7d\n",          printf("Avg: dectime %5.2f ms, %5.2f fps, mp4 stream size =%d\n",
385                  totaldectime, 1000/totaldectime, (int)totalsize);                  totaldectime, 1000/totaldectime, (int)totalsize);
386    
387  /*****************************************************************************  /*****************************************************************************
# Line 430  Line 431 
431  static double  static double
432  msecond()  msecond()
433  {  {
434  #ifndef WIN32  #ifndef _MSC_VER
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;
# Line 515  Line 516 
516          xparam.height = YDIM;          xparam.height = YDIM;
517    
518          xerr = xvid_decore(NULL, XVID_DEC_CREATE, &xparam, NULL);          xerr = xvid_decore(NULL, XVID_DEC_CREATE, &xparam, NULL);
   
519          dec_handle = xparam.handle;          dec_handle = xparam.handle;
520    
521          return xerr;          return xerr;
# Line 526  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                   int *ostream_size,           int *ostream_size)
                  int *notification)  
530  {  {
531    
532          int xerr;          int xerr;
533          XVID_DEC_FRAME xframe;          XVID_DEC_FRAME xframe;
                 XVID_DEC_STATS xstats;  
534    
         xframe.general    = 0;  
535                  xframe.bitstream  = istream;                  xframe.bitstream  = istream;
536          xframe.length     = istream_size;          xframe.length     = istream_size;
537                  xframe.image      = ostream;                  xframe.image      = ostream;
538          xframe.stride     = XDIM;          xframe.stride     = XDIM;
539                  xframe.colorspace = XVID_CSP_YV12;                  xframe.colorspace = XVID_CSP_YV12;
540    
541                  xerr = xvid_decore(dec_handle, XVID_DEC_DECODE, &xframe, &xstats);          xerr = xvid_decore(dec_handle, XVID_DEC_DECODE, &xframe, NULL);
542    
543                  *ostream_size = xframe.length;                  *ostream_size = xframe.length;
544    
                 *notification = xstats.notify;  
   
545          return xerr;          return xerr;
546  }  }
547    

Legend:
Removed from v.1.1.2.4  
changed lines
  Added in v.1.4

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