[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 824, Sun Feb 9 19:32:52 2003 UTC revision 851, Sat Feb 15 15:22:19 2003 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 Christoph Lampert   *  Copyright(C) 2002-2003 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 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.5 2003-02-09 19:32:52 edgomez Exp $   * $Id: xvid_decraw.c,v 1.6 2003-02-15 15:22:17 edgomez Exp $
23   *   *
24   ****************************************************************************/   ****************************************************************************/
25    
# 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;
 static int ARG_STREAMTYPE = 0;  
78  static char *ARG_INPUTFILE = NULL;  static char *ARG_INPUTFILE = NULL;
79    
80    
# Line 84  Line 83 
83    
84  # define BUFFER_SIZE 10*XDIM*YDIM  # define BUFFER_SIZE 10*XDIM*YDIM
85    
 #define LONG_PACK(a,b,c,d) ((long) (((long)(a))<<24) | (((long)(b))<<16) | \  
                                    (((long)(c))<<8)  |((long)(d)))  
   
 #define SWAP(a) ( (((a)&0x000000ff)<<24) | (((a)&0x0000ff00)<<8) | \  
                   (((a)&0x00ff0000)>>8)  | (((a)&0xff000000)>>24) )  
   
86  /*****************************************************************************  /*****************************************************************************
87   *               Local prototypes   *               Local prototypes
88   ****************************************************************************/   ****************************************************************************/
# Line 101  Line 94 
94  static int dec_main(unsigned char *istream,  static int dec_main(unsigned char *istream,
95                      unsigned char *ostream,                      unsigned char *ostream,
96                      int istream_size,                      int istream_size,
97                      int *ostream_size);                                          int *ostream_size,
98                                            int *isframe);
99  static int dec_stop();  static int dec_stop();
100  static void usage();  static void usage();
101    
# Line 115  Line 109 
109          unsigned char *mp4_ptr    = NULL;          unsigned char *mp4_ptr    = NULL;
110          unsigned char *out_buffer = NULL;          unsigned char *out_buffer = NULL;
111          int bigendian = 0;          int bigendian = 0;
112            int still_left_in_buffer;
113            int delayed_frames;
114    
115          double totaldectime;          double totaldectime;
116    
# Line 130  Line 126 
126          int i;          int i;
127    
128          printf("xvid_decraw - raw mpeg4 bitstream decoder ");          printf("xvid_decraw - raw mpeg4 bitstream decoder ");
129          printf("written by Christoph Lampert 2002\n\n");          printf("written by Christoph Lampert 2002-2003\n\n");
130    
131  /*****************************************************************************  /*****************************************************************************
132   * Command line parsing   * Command line parsing
# Line 161  Line 157 
157                          i++;                          i++;
158                          ARG_SAVEMPEGSTREAM = atoi(argv[i]);                          ARG_SAVEMPEGSTREAM = atoi(argv[i]);
159                  }                  }
                 else if (strcmp("-t", argv[i]) == 0 && i < argc - 1 ) {  
                         i++;  
                         ARG_STREAMTYPE = atoi(argv[i]);  
                 }  
160                  else if (strcmp("-help", argv[i])) {                  else if (strcmp("-help", argv[i])) {
161                          usage();                          usage();
162                          return(0);                          return(0);
# Line 229  Line 221 
221   *                               Main loop   *                               Main loop
222   ****************************************************************************/   ****************************************************************************/
223    
224          totalsize = LONG_PACK('M','P','4','U');          /* Fill the buffer */
225          mp4_ptr = (unsigned char *)&totalsize;          still_left_in_buffer = fread(mp4_buffer, 1, BUFFER_SIZE, in_file);
         if(*mp4_ptr == 'M')  
                 bigendian = 1;  
         else  
                 bigendian = 0;  
   
         if(ARG_STREAMTYPE) {  
   
                 unsigned char header[4];  
   
                 /* MP4U format  : read header */  
                 if(feof(in_file))  
                         goto release_all;  
                 fread(header, 4, 1, in_file);  
   
                 if(header[0] != 'M' || header[1] != 'P' ||  
                    header[2] != '4' || header[3] != 'U') {  
                         fprintf(stderr, "Error, this not a mp4u container file\n");  
                         goto release_all;  
                 }  
   
         }  
         else {  
                 fread(mp4_buffer, BUFFER_SIZE, 1, in_file);  
         }  
226    
227          totaldectime = 0;          totaldectime = 0;
228          totalsize = 0;          totalsize = 0;
229          filenr = 0;          filenr = 0;
230            delayed_frames = 0;
231          mp4_ptr = mp4_buffer;          mp4_ptr = mp4_buffer;
232    
233          do {          do {
# Line 266  Line 235 
235                  int mp4_size = (mp4_buffer + BUFFER_SIZE - mp4_ptr);                  int mp4_size = (mp4_buffer + BUFFER_SIZE - mp4_ptr);
236                  int used_bytes = 0;                  int used_bytes = 0;
237                  double dectime;                  double dectime;
238                    int notification;
                 /* Read data from input file */  
                 if(ARG_STREAMTYPE) {  
   
                         /* MP4U container */  
   
                         /* Read stream size first */  
                         if(feof(in_file))  
                                 break;  
                         fread(&mp4_size, sizeof(long), 1, in_file);  
   
                         /* Mp4U container is big endian */  
                         if(!bigendian)  
                                 mp4_size = SWAP(mp4_size);  
   
                         /* Read mp4_size_bytes */  
                         if(feof(in_file))  
                                 break;  
                         fread(mp4_buffer, mp4_size, 1, in_file);  
239    
240                          /*                          /*
241                           * When reading mp4u, we don't have to care about buffer                   * If the buffer is half empty or there are no more bytes in it
242                           * filling as we know exactly how much bytes there are in                   * then fill it.
                          * next frame  
243                           */                           */
244                          mp4_ptr = mp4_buffer;                  if (mp4_ptr > mp4_buffer + BUFFER_SIZE/2 ||
245                            still_left_in_buffer <= 0) {
                 }  
                 else {  
   
                         /* Real raw stream */  
   
                         /* buffer more than half empty -> Fill it */  
                         if (mp4_ptr > mp4_buffer + BUFFER_SIZE/2) {  
246                                  int rest = (mp4_buffer + BUFFER_SIZE - mp4_ptr);                                  int rest = (mp4_buffer + BUFFER_SIZE - mp4_ptr);
247    
248                                  /* Move data if needed */                                  /* Move data if needed */
# Line 312  Line 255 
255                                  /* read new data */                                  /* read new data */
256                                  if(feof(in_file))                                  if(feof(in_file))
257                                          break;                                          break;
                                 fread(mp4_buffer + rest, BUFFER_SIZE - rest, 1, in_file);  
258    
259                          }                          still_left_in_buffer = fread(mp4_buffer + rest,
260                                                                                     1,
261                                                                                     BUFFER_SIZE - rest,
262                                                                                     in_file);
263    
264                  }                  }
265    
266    
267                    /* This loop flushes N_VOPS (with vop_coded bit set to 0) */
268                    do {
269    
270                  /* Decode frame */                  /* Decode frame */
271                  dectime = msecond();                  dectime = msecond();
272                  status = dec_main(mp4_ptr, out_buffer, mp4_size, &used_bytes);                          status = dec_main(mp4_ptr, out_buffer, mp4_size, &used_bytes, &notification);
273                  dectime = msecond() - dectime;                  dectime = msecond() - dectime;
274    
275                  if (status) {                  if (status) {
276                          break;                          break;
277                  }                  }
278    
279                  /*                          /* Update buffer pointers */
                  * Only needed for real raw stream, mp4u uses  
                  * mp4_ptr = mp4_buffer for each frame  
                  */  
280                  mp4_ptr += used_bytes;                  mp4_ptr += used_bytes;
281                            still_left_in_buffer -= used_bytes;
282    
283                  /* Updated data */                          /* Total size */
284                  totalsize += used_bytes;                  totalsize += used_bytes;
285    
286                    }while(used_bytes <= 7 && still_left_in_buffer > 0); /* <= 7 bytes is a NVOPS */
287    
288                    /* Negative buffer would mean we went too far */
289                    if(still_left_in_buffer < 0) break;
290    
291                    /* Skip when decoder is buffering images or decoding VOL information */
292                    if(notification != XVID_DEC_VOP) {
293                            /* It's a delay only if it notifies NOTHING */
294                            if(notification == XVID_DEC_NOTHING)
295                                    delayed_frames++;
296                            continue;
297                    }
298    
299                    /* Updated data - Count only usefull decode time */
300                  totaldectime += dectime;                  totaldectime += dectime;
301    
302                  /* Prints some decoding stats */                  /* Prints some decoding stats */
303                  printf("Frame %5d: dectime =%6.1f ms length=%7d bytes \n",                  printf("Frame %5d: dectime(ms) =%6.1f, length(bytes) =%7d\n",
304                         filenr, dectime, used_bytes);                         filenr, dectime, used_bytes);
305    
306                  /* Save individual mpeg4 strean if required */                  /* Save individual mpeg4 stream if required */
307                  if (ARG_SAVEMPEGSTREAM) {                  if (ARG_SAVEMPEGSTREAM) {
308                          FILE *filehandle = NULL;                          FILE *filehandle = NULL;
309    
# Line 353  Line 315 
315                                          filename);                                          filename);
316                          }                          }
317                          else {                          else {
318                                  fwrite(mp4_buffer, used_bytes, 1, filehandle);                                  fwrite(mp4_buffer, 1, used_bytes, filehandle);
319                                  fclose(filehandle);                                  fclose(filehandle);
320                          }                          }
321                  }                  }
322    
   
323                  /* Save output frame if required */                  /* Save output frame if required */
324                  if (ARG_SAVEDECOUTPUT) {                  if (ARG_SAVEDECOUTPUT) {
325                          sprintf(filename, "%sdec%05d.pgm", filepath, filenr);                          sprintf(filename, "%sdec%05d.pgm", filepath, filenr);
# Line 373  Line 334 
334    
335          } while ( (status>=0) && (filenr<ABS_MAXFRAMENR) );          } while ( (status>=0) && (filenr<ABS_MAXFRAMENR) );
336    
337    /*****************************************************************************
338     *     Flush decoder buffers
339     ****************************************************************************/
340            while(delayed_frames--) {
341    
342                    /* Fake vars */
343                    int used_bytes, isframe;
344                    double dectime;
345    
346                    /* Decode frame */
347                    dectime = msecond();
348                    status = dec_main(NULL, out_buffer, -1, &used_bytes, &isframe);
349                    dectime = msecond() - dectime;
350    
351                    if (status) {
352                            break;
353                    }
354    
355                    /* Updated data - Count only usefull decode time */
356                    totaldectime += dectime;
357    
358                    /* Prints some decoding stats */
359                    printf("Frame %5d: dectime(ms) =%6.1f, length(bytes) =%7d\n",
360                               filenr, dectime, used_bytes);
361    
362                    /* Save output frame if required */
363                    if (ARG_SAVEDECOUTPUT) {
364                            sprintf(filename, "%sdec%05d.pgm", filepath, filenr);
365                            if(write_pgm(filename,out_buffer)) {
366                                    fprintf(stderr,
367                                                    "Error writing decoded PGM frame %s\n",
368                                                    filename);
369                            }
370                    }
371    
372                    filenr++;
373    
374            }
375    
376  /*****************************************************************************  /*****************************************************************************
377   *     Calculate totals and averages for output, print results   *     Calculate totals and averages for output, print results
# Line 381  Line 380 
380          totalsize    /= filenr;          totalsize    /= filenr;
381          totaldectime /= filenr;          totaldectime /= filenr;
382    
383          printf("Avg: dectime %5.2f ms, %5.2f fps, mp4 stream size =%d\n",          printf("Avg: dectime(ms) =%7.2f, fps =%7.2f, length(bytes) =%7d\n",
384                  totaldectime, 1000/totaldectime, (int)totalsize);                  totaldectime, 1000/totaldectime, (int)totalsize);
385    
386  /*****************************************************************************  /*****************************************************************************
# Line 516  Line 515 
515          xparam.height = YDIM;          xparam.height = YDIM;
516    
517          xerr = xvid_decore(NULL, XVID_DEC_CREATE, &xparam, NULL);          xerr = xvid_decore(NULL, XVID_DEC_CREATE, &xparam, NULL);
518    
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,
530                     int *notification)
531  {  {
532    
533          int xerr;          int xerr;
534          XVID_DEC_FRAME xframe;          XVID_DEC_FRAME xframe;
535                    XVID_DEC_STATS xstats;
536    
537            xframe.general    = 0;
538          xframe.bitstream = istream;          xframe.bitstream = istream;
539          xframe.length = istream_size;          xframe.length = istream_size;
540          xframe.image = ostream;          xframe.image = ostream;
541          xframe.stride = XDIM;          xframe.stride = XDIM;
542          xframe.colorspace = XVID_CSP_YV12;          xframe.colorspace = XVID_CSP_YV12;
543    
544          xerr = xvid_decore(dec_handle, XVID_DEC_DECODE, &xframe, NULL);                  xerr = xvid_decore(dec_handle, XVID_DEC_DECODE, &xframe, &xstats);
545    
546          *ostream_size = xframe.length;          *ostream_size = xframe.length;
547    
548                    *notification = xstats.notify;
549    
550          return xerr;          return xerr;
551  }  }
552    

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

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