[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, Sat Aug 17 20:03:36 2002 UTC revision 1.25, Tue Jan 5 09:25:19 2010 UTC
# Line 1  Line 1 
1  /**************************************************************************  /*****************************************************************************
2   *   *
3   *      XVID MPEG-4 VIDEO CODEC - Example for encoding and decoding   *  XVID MPEG-4 VIDEO CODEC
4     *  - Console based decoding test application  -
5     *
6     *  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 14  Line 18 
18   *   *
19   *      You should have received a copy of the GNU General Public License   *      You should have received a copy of the GNU General Public License
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., 675 Mass Ave, Cambridge, MA 02139, USA.   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
22     *
23     * $Id$
24   *   *
25   *************************************************************************/   ****************************************************************************/
26    
27  /************************************************************************  /*****************************************************************************
28   *   *
29   *  Test routine for XviD decoding   *  Application notes :
  *  (C) Christoph Lampert, 2002/08/17  
30   *   *
31   *  An MPEG-4 bitstream is read from stdin and decoded,   *  An MPEG-4 bitstream is read from an input file (or stdin) and decoded,
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.
  *  
  *   gcc xvid_decraw.c -lxvidcore -lm -o xvid_decraw  
36   *   *
37   *  You have to specify the image dimensions (until the add the feature   *  Use ./xvid_decraw -help for a list of options
  *  to read this from the bitstream)  
38   *   *
39   *  Parameters are: xvid_stat XDIM YDIM   ****************************************************************************/
  *  
  *  output and indivual m4v-files are saved, if corresponding flags are set  
  *  
  ************************************************************************/  
40    
41  #include <stdio.h>  #include <stdio.h>
42  #include <stdlib.h>  #include <stdlib.h>
43  #include <math.h>               // needed for log10  #include <string.h>
44  #include <sys/time.h>           // only needed for gettimeofday  #include <math.h>
45    #ifndef WIN32
46    #include <sys/time.h>
47    #else
48    #include <time.h>
49    #endif
50    
51  #include "../src/xvid.h"                /* comes with XviD */  #include "xvid.h"
52    
53  #define ABS_MAXFRAMENR 9999               // max number of frames  /*****************************************************************************
54     *               Global vars in module and constants
55     ****************************************************************************/
56    
57    #define USE_PNM 0
58    #define USE_TGA 1
59    #define USE_YUV 2
60    
61    static int XDIM = 0;
62    static int YDIM = 0;
63    static int ARG_SAVEDECOUTPUT = 0;
64    static int ARG_SAVEMPEGSTREAM = 0;
65    static char *ARG_INPUTFILE = NULL;
66    static int CSP = XVID_CSP_I420;
67    static int BPP = 1;
68    static int FORMAT = USE_PNM;
69    
70    static char filepath[256] = "./";
71    static void *dec_handle = NULL;
72    
73    #define BUFFER_SIZE (2*1024*1024)
74    
75    static const int display_buffer_bytes = 0;
76    
77    #define MIN_USEFUL_BYTES 1
78    
79    /*****************************************************************************
80     *               Local prototypes
81     ****************************************************************************/
82    
83    static double msecond();
84    static int dec_init(int use_assembler, int debug_level);
85    static int dec_main(unsigned char *istream,
86                                            unsigned char *ostream,
87                                            int istream_size,
88                                            xvid_dec_stats_t *xvid_dec_stats);
89    static int dec_stop();
90    static void usage();
91    static int write_image(char *prefix, unsigned char *image);
92    static int write_pnm(char *filename, unsigned char *image);
93    static int write_tga(char *filename, unsigned char *image);
94    static int write_yuv(char *filename, unsigned char *image);
95    
96    const char * type2str(int type)
97    {
98        if (type==XVID_TYPE_IVOP)
99            return "I";
100        if (type==XVID_TYPE_PVOP)
101            return "P";
102        if (type==XVID_TYPE_BVOP)
103            return "B";
104        return "S";
105    }
106    
107    /*****************************************************************************
108     *        Main program
109     ****************************************************************************/
110    
111  int XDIM=720;  int main(int argc, char *argv[])
112  int YDIM=576;  {
113  int i,filenr = 0;          unsigned char *mp4_buffer = NULL;
114            unsigned char *mp4_ptr    = NULL;
115            unsigned char *out_buffer = NULL;
116            int useful_bytes;
117            int chunk;
118            xvid_dec_stats_t xvid_dec_stats;
119    
120  int save_dec_flag = 1;          // save decompressed bytestream?          double totaldectime;
 int save_m4v_flag = 0;          // save bytestream itself?  
121    
122  char filepath[256] = "./";      // the path where to save output          long totalsize;
123            int status;
124    
125  void *dec_handle = NULL;                // handle for decoding          int use_assembler = 0;
126            int debug_level = 0;
127    
128  /*********************************************************************/          char filename[256];
 /*                     "statistical" functions                               */  
 /*                                                                   */  
 /*  these are not needed for decoding itself, but for measuring      */  
 /*  time (and maybe later quality), there in nothing specific to     */  
 /*  XviD in these                                                    */  
 /*                                                                   */  
 /*********************************************************************/  
129    
130  double msecond()          FILE *in_file;
131  /* return the current time in seconds(!)  */          int filenr;
132  {          int i;
133          struct timeval  tv;  
134          gettimeofday(&tv, 0);          printf("xvid_decraw - raw mpeg4 bitstream decoder ");
135          return tv.tv_sec + tv.tv_usec * 1.0e-6;          printf("written by Christoph Lampert 2002-2003\n\n");
136    
137    /*****************************************************************************
138     * Command line parsing
139     ****************************************************************************/
140    
141            for (i=1; i< argc; i++) {
142    
143                    if (strcmp("-asm", argv[i]) == 0 ) {
144                            use_assembler = 1;
145                    } else if (strcmp("-debug", argv[i]) == 0 && i < argc - 1 ) {
146                            i++;
147                            if (sscanf(argv[i], "0x%x", &debug_level) != 1) {
148                                    debug_level = atoi(argv[i]);
149                            }
150                    } else if (strcmp("-d", argv[i]) == 0) {
151                            ARG_SAVEDECOUTPUT = 1;
152                    } else if (strcmp("-i", argv[i]) == 0 && i < argc - 1 ) {
153                            i++;
154                            ARG_INPUTFILE = argv[i];
155                    } else if (strcmp("-m", argv[i]) == 0) {
156                            ARG_SAVEMPEGSTREAM = 1;
157                    } else if (strcmp("-c", argv[i]) == 0  && i < argc - 1 ) {
158                            i++;
159                            if (strcmp(argv[i], "rgb16") == 0) {
160                                    CSP = XVID_CSP_RGB555;
161                                    BPP = 2;
162                            } else if (strcmp(argv[i], "rgb24") == 0) {
163                                    CSP = XVID_CSP_BGR;
164                                    BPP = 3;
165                            } else if (strcmp(argv[i], "rgb32") == 0) {
166                                    CSP = XVID_CSP_BGRA;
167                                    BPP = 4;
168                            } else if (strcmp(argv[i], "yv12") == 0) {
169                                    CSP = XVID_CSP_YV12;
170                                    BPP = 1;
171                            } else {
172                                    CSP = XVID_CSP_I420;
173                                    BPP = 1;
174                            }
175                    } else if (strcmp("-f", argv[i]) == 0 && i < argc -1) {
176                            i++;
177                            if (strcmp(argv[i], "tga") == 0) {
178                                    FORMAT = USE_TGA;
179                            } else if (strcmp(argv[i], "yuv") == 0) {
180                                    FORMAT = USE_YUV;
181                            } else {
182                                    FORMAT = USE_PNM;
183                            }
184                    } else if (strcmp("-help", argv[i]) == 0) {
185                            usage();
186                            return(0);
187                    } else {
188                            usage();
189                            exit(-1);
190                    }
191            }
192    
193    #if defined(_MSC_VER)
194            if (ARG_INPUTFILE==NULL) {
195                    fprintf(stderr, "Warning: MSVC build does not read EOF correctly from stdin. Use the -i switch.\n\n");
196  }  }
197    #endif
198    
199    /*****************************************************************************
200     * Values checking
201     ****************************************************************************/
202    
203  /*********************************************************************/          if ( ARG_INPUTFILE == NULL || strcmp(ARG_INPUTFILE, "stdin") == 0) {
204  /*                    input and output functions                         */                  in_file = stdin;
205  /*                                                                   */          }
206  /* the are small and simple routines for writing image               */          else {
 /* image. It's just for convenience, again nothing specific to XviD  */  
 /*                                                                   */  
 /*********************************************************************/  
207    
208  int write_pgm(char *filename, unsigned char *image)                  in_file = fopen(ARG_INPUTFILE, "rb");
209  {                  if (in_file == NULL) {
210          FILE *filehandle;                          fprintf(stderr, "Error opening input file %s\n", ARG_INPUTFILE);
211          filehandle=fopen(filename,"wb");                          return(-1);
212          if (filehandle)                  }
213          {          }
                 fprintf(filehandle,"P5\n\n");           //  
                 fprintf(filehandle,"%d %d 255\n",XDIM,YDIM*3/2);  
                 fwrite(image,XDIM,YDIM*3/2,filehandle);  
214    
215                  fclose(filehandle);          /* PNM/PGM format can't handle 16/32 bit data */
216                  return 0;          if (BPP != 1 && BPP != 3 && FORMAT == USE_PNM) {
217                    FORMAT = USE_TGA;
218          }          }
219          else          if (BPP != 1 && FORMAT == USE_YUV) {
220                  return 1;                  FORMAT = USE_TGA;
221  }  }
222    
223    /*****************************************************************************
224     *        Memory allocation
225     ****************************************************************************/
226    
227  int write_ppm(char *filename, unsigned char *image)          /* Memory for encoded mp4 stream */
228  {          mp4_buffer = (unsigned char *) malloc(BUFFER_SIZE);
229          FILE *filehandle;          mp4_ptr = mp4_buffer;
230          filehandle=fopen(filename,"wb");          if (!mp4_buffer)
231          if (filehandle)                  goto free_all_memory;
         {  
                 fprintf(filehandle,"P6\n\n");           //  
                 fprintf(filehandle,"%d %d 255\n",XDIM,YDIM);  
                 fwrite(image,XDIM,YDIM*3,filehandle);  
232    
233                  fclose(filehandle);  /*****************************************************************************
234                  return 0;   *        XviD PART  Start
235          }   ****************************************************************************/
236          else  
237                  return 1;          status = dec_init(use_assembler, debug_level);
238            if (status) {
239                    fprintf(stderr,
240                                    "Decore INIT problem, return value %d\n", status);
241                    goto release_all;
242  }  }
243    
 /*********************************************************************/  
 /* Routines for decoding: init encoder, frame step, release encoder  */  
 /*********************************************************************/  
244    
245  int dec_init(int use_assembler) /* init decoder before first run */  /*****************************************************************************
246  {   *                               Main loop
247          int xerr;   ****************************************************************************/
248    
249            /* Fill the buffer */
250            useful_bytes = fread(mp4_buffer, 1, BUFFER_SIZE, in_file);
251    
252            totaldectime = 0;
253            totalsize = 0;
254            filenr = 0;
255            mp4_ptr = mp4_buffer;
256            chunk = 0;
257    
258          XVID_INIT_PARAM xinit;          do {
259          XVID_DEC_PARAM xparam;                  int used_bytes = 0;
260                    double dectime;
261    
262                  if(use_assembler)                  /*
263  #ifdef ARCH_IA64                   * If the buffer is half empty or there are no more bytes in it
264                          xinit.cpu_flags = XVID_CPU_FORCE | XVID_CPU_IA64;                   * then fill it.
265  #else                   */
266                          xinit.cpu_flags = 0;                  if (mp4_ptr > mp4_buffer + BUFFER_SIZE/2) {
267  #endif                          int already_in_buffer = (mp4_buffer + BUFFER_SIZE - mp4_ptr);
                 else  
                         xinit.cpu_flags = XVID_CPU_FORCE;  
268    
269          xvid_init(NULL, 0, &xinit, NULL);                          /* Move data if needed */
270          xparam.width = XDIM;                          if (already_in_buffer > 0)
271          xparam.height = YDIM;                                  memcpy(mp4_buffer, mp4_ptr, already_in_buffer);
272    
273          xerr = xvid_decore(NULL, XVID_DEC_CREATE, &xparam, NULL);                          /* Update mp4_ptr */
274          dec_handle = xparam.handle;                          mp4_ptr = mp4_buffer;
275    
276          return xerr;                          /* read new data */
277                if(!feof(in_file)) {
278                                    useful_bytes += fread(mp4_buffer + already_in_buffer,
279                                                                              1, BUFFER_SIZE - already_in_buffer,
280                                                                              in_file);
281                            }
282  }  }
283    
 int dec_main(unsigned char *m4v_buffer, unsigned char *out_buffer,int *m4v_size)  
 {       /* decode one frame  */  
284    
285          int xerr;                  /* This loop is needed to handle VOL/NVOP reading */
286          XVID_DEC_FRAME xframe;                  do {
287    
288                            /* Decode frame */
289                            dectime = msecond();
290                            used_bytes = dec_main(mp4_ptr, out_buffer, useful_bytes, &xvid_dec_stats);
291                            dectime = msecond() - dectime;
292    
293                            /* Resize image buffer if needed */
294                            if(xvid_dec_stats.type == XVID_TYPE_VOL) {
295    
296                                    /* Check if old buffer is smaller */
297                                    if(XDIM*YDIM < xvid_dec_stats.data.vol.width*xvid_dec_stats.data.vol.height) {
298    
299                                            /* Copy new witdh and new height from the vol structure */
300                                            XDIM = xvid_dec_stats.data.vol.width;
301                                            YDIM = xvid_dec_stats.data.vol.height;
302    
303                                            /* Free old output buffer*/
304                                            if(out_buffer) free(out_buffer);
305    
306                                            /* Allocate the new buffer */
307                                            out_buffer = (unsigned char*)malloc(XDIM*YDIM*4);
308                                            if(out_buffer == NULL)
309                                                    goto free_all_memory;
310    
311                                            fprintf(stderr, "Resized frame buffer to %dx%d\n", XDIM, YDIM);
312                                    }
313    
314          xframe.bitstream = m4v_buffer;                                  /* Save individual mpeg4 stream if required */
315          xframe.length = 9999;                                  if(ARG_SAVEMPEGSTREAM) {
316              xframe.image = out_buffer;                                          FILE *filehandle = NULL;
         xframe.stride = XDIM;  
             xframe.colorspace = XVID_CSP_YV12;  
317    
318                  do {                                          sprintf(filename, "%svolhdr.m4v", filepath);
319                  xerr = xvid_decore(dec_handle, XVID_DEC_DECODE, &xframe, NULL);                                          filehandle = fopen(filename, "wb");
320                                            if(!filehandle) {
321                                                    fprintf(stderr,
322                                                                    "Error writing vol header mpeg4 stream to file %s\n",
323                                                                    filename);
324                                            } else {
325                                                    fwrite(mp4_ptr, 1, used_bytes, filehandle);
326                                                    fclose(filehandle);
327                                            }
328                                    }
329                            }
330    
331                          *m4v_size = xframe.length;                          /* Update buffer pointers */
332                  xframe.bitstream += *m4v_size;                          if(used_bytes > 0) {
333                                    mp4_ptr += used_bytes;
334                                    useful_bytes -= used_bytes;
335    
336                  } while (*m4v_size<7);  /* this skips N-VOPs */                                  /* Total size */
337                                    totalsize += used_bytes;
338                            }
339    
340          return xerr;                          if (display_buffer_bytes) {
341                                    printf("Data chunk %d: %d bytes consumed, %d bytes in buffer\n", chunk++, used_bytes, useful_bytes);
342  }  }
343                    } while (xvid_dec_stats.type <= 0 && useful_bytes > MIN_USEFUL_BYTES);
344    
345  int dec_stop()  /* close decoder to release resources */                  /* Check if there is a negative number of useful bytes left in buffer
346  {                   * This means we went too far */
347          int xerr;          if(useful_bytes < 0)
348          xerr = xvid_decore(dec_handle, XVID_DEC_DESTROY, NULL, NULL);              break;
349    
350            /* Updated data - Count only usefull decode time */
351                    totaldectime += dectime;
352    
353          return xerr;  
354                    if (!display_buffer_bytes) {
355                            printf("Frame %5d: type = %s, dectime(ms) =%6.1f, length(bytes) =%7d\n",
356                                            filenr, type2str(xvid_dec_stats.type), dectime, used_bytes);
357  }  }
358    
359                    /* Save individual mpeg4 stream if required */
360                    if(ARG_SAVEMPEGSTREAM) {
361                            FILE *filehandle = NULL;
362    
363  /*********************************************************************/                          sprintf(filename, "%sframe%05d.m4v", filepath, filenr);
364  /*                          Main program                             */                          filehandle = fopen(filename, "wb");
365  /*********************************************************************/                          if(!filehandle) {
366                                    fprintf(stderr,
367                                                    "Error writing single mpeg4 stream to file %s\n",
368                                                    filename);
369                            }
370                            else {
371                                    fwrite(mp4_ptr-used_bytes, 1, used_bytes, filehandle);
372                                    fclose(filehandle);
373                            }
374                    }
375    
376  int main(int argc, char *argv[])                  /* Save output frame if required */
377  {                  if (ARG_SAVEDECOUTPUT) {
378    unsigned char *divx_buffer = NULL;                          if (FORMAT == USE_YUV) {
379    unsigned char *divx_ptr = NULL;                                  sprintf(filename, "%sdec", filepath);
380    unsigned char *out_buffer = NULL;                          }
381                            else {
382                                    sprintf(filename, "%sdec%05d", filepath, filenr);
383                            }
384                            if(write_image(filename, out_buffer)) {
385                                    fprintf(stderr,
386                                                    "Error writing decoded frame %s\n",
387                                                    filename);
388                            }
389                    }
390    
391    double dectime;                  filenr++;
   double totaldectime=0.;  
392    
393    long totalsize=0;          } while (useful_bytes>MIN_USEFUL_BYTES || !feof(in_file));
   int status;  
394    
395    int m4v_size;          useful_bytes = 0; /* Empty buffer */
   int frame_type[ABS_MAXFRAMENR];  
   int use_assembler=1;  
396    
397    char filename[256];  /*****************************************************************************
398     *     Flush decoder buffers
399     ****************************************************************************/
400    
401    FILE *filehandle;          do {
402    
403                    /* Fake vars */
404                    int used_bytes;
405                    double dectime;
406    
407          if (argc>=3)          do {
408          {       XDIM = atoi(argv[1]);                      dectime = msecond();
409                  YDIM = atoi(argv[2]);                      used_bytes = dec_main(NULL, out_buffer, -1, &xvid_dec_stats);
410                  if ( (XDIM <= 0) || (XDIM >= 2048) || (YDIM <=0) || (YDIM >= 2048) )                      dectime = msecond() - dectime;
411                  {       fprintf(stderr,"Wrong frames size %d %d, trying PGM \n",XDIM, YDIM);                          if (display_buffer_bytes) {
412                                    printf("Data chunk %d: %d bytes consumed, %d bytes in buffer\n", chunk++, used_bytes, useful_bytes);
413                  }                  }
414            } while(used_bytes>=0 && xvid_dec_stats.type <= 0);
415    
416            if (used_bytes < 0) {   /* XVID_ERR_END */
417                break;
418          }          }
         if (argc>=4 && !strcmp(argv[3],"noasm"))  
           use_assembler = 0;  
419    
420                    /* Updated data - Count only usefull decode time */
421                    totaldectime += dectime;
422    
423  /* allocate memory */                  /* Prints some decoding stats */
424                    if (!display_buffer_bytes) {
425                            printf("Frame %5d: type = %s, dectime(ms) =%6.1f, length(bytes) =%7d\n",
426                                            filenr, type2str(xvid_dec_stats.type), dectime, used_bytes);
427                    }
428    
429                    /* Save output frame if required */
430                    if (ARG_SAVEDECOUTPUT) {
431                            if (FORMAT == USE_YUV) {
432                                    sprintf(filename, "%sdec", filepath);
433                            }
434                            else {
435                                    sprintf(filename, "%sdec%05d", filepath, filenr);
436                            }
437                            if(write_image(filename, out_buffer)) {
438                                    fprintf(stderr,
439                                                    "Error writing decoded frame %s\n",
440                                                    filename);
441                            }
442                    }
443    
444    divx_buffer = (unsigned char *) malloc(10*XDIM*YDIM);                  filenr++;
   // this should really be enough memory!  
   if (!divx_buffer)  
     goto free_all_memory;  
   divx_ptr = divx_buffer+10*XDIM*YDIM;  
445    
446    out_buffer = (unsigned char *) malloc(4*XDIM*YDIM);   /* YUV needs less */          }while(1);
   if (!out_buffer)  
     goto free_all_memory;  
447    
448    /*****************************************************************************
449     *     Calculate totals and averages for output, print results
450     ****************************************************************************/
451    
452            if (filenr>0) {
453                    totalsize    /= filenr;
454                    totaldectime /= filenr;
455                    printf("Avg: dectime(ms) =%7.2f, fps =%7.2f, length(bytes) =%7d\n",
456                               totaldectime, 1000/totaldectime, (int)totalsize);
457            }else{
458                    printf("Nothing was decoded!\n");
459            }
460    
461  /*********************************************************************/  /*****************************************************************************
462  /*                         XviD PART  Start                          */   *      XviD PART  Stop
463  /*********************************************************************/   ****************************************************************************/
464    
465          status = dec_init(use_assembler);   release_all:
466            if (dec_handle) {
467                    status = dec_stop();
468          if (status)          if (status)
469          {                          fprintf(stderr, "decore RELEASE problem return value %d\n", status);
                 printf("Decore INIT problem, return value %d\n", status);  
                 goto release_all;  
470          }          }
471    
472     free_all_memory:
473            free(out_buffer);
474            free(mp4_buffer);
475    
476  /*********************************************************************/          return(0);
477  /*                               Main loop                           */  }
 /*********************************************************************/  
478    
479    do  /*****************************************************************************
480     *               Usage function
481     ****************************************************************************/
482    
483    static void usage()
484      {      {
485    
486          if (divx_ptr > divx_buffer+5*XDIM*YDIM) /* buffer more than half empty */          fprintf(stderr, "Usage : xvid_decraw [OPTIONS]\n");
487          {       int rest=(divx_buffer+10*XDIM*YDIM-divx_ptr);          fprintf(stderr, "Options :\n");
488                  if (rest)          fprintf(stderr, " -asm           : use assembly optimizations (default=disabled)\n");
489                          memcpy(divx_buffer, divx_ptr, rest);          fprintf(stderr, " -debug         : debug level (debug=0)\n");
490                  divx_ptr = divx_buffer;          fprintf(stderr, " -i string      : input filename (default=stdin)\n");
491                  fread(divx_buffer+rest, 1, 5*XDIM*YDIM, stdin); /* read new data */          fprintf(stderr, " -d             : save decoder output\n");
492            fprintf(stderr, " -c csp         : choose colorspace output (rgb16, rgb24, rgb32, yv12, i420)\n");
493            fprintf(stderr, " -f format      : choose output file format (tga, pnm, pgm, yuv)\n");
494            fprintf(stderr, " -m             : save mpeg4 raw stream to individual files\n");
495            fprintf(stderr, " -help          : This help message\n");
496            fprintf(stderr, " (* means default)\n");
497    
498          }          }
499    
500          dectime = -msecond();  /*****************************************************************************
501          status = dec_main(divx_ptr, out_buffer, &m4v_size);   *               "helper" functions
502     ****************************************************************************/
503    
504          if (status)  /* return the current time in milli seconds */
505    static double
506    msecond()
507          {          {
508                  break;  #ifndef WIN32
509            struct timeval  tv;
510            gettimeofday(&tv, 0);
511            return((double)tv.tv_sec*1.0e3 + (double)tv.tv_usec*1.0e-3);
512    #else
513            clock_t clk;
514            clk = clock();
515            return(clk * 1000.0 / CLOCKS_PER_SEC);
516    #endif
517    }
518    
519    /*****************************************************************************
520     *              output functions
521     ****************************************************************************/
522    
523    static int write_image(char *prefix, unsigned char *image)
524    {
525            char filename[1024];
526            char *ext;
527            int ret;
528    
529            if (FORMAT == USE_PNM && BPP == 1) {
530                    ext = "pgm";
531            } else if (FORMAT == USE_PNM && BPP == 3) {
532                    ext = "pnm";
533            } else if (FORMAT == USE_YUV) {
534                    ext = "yuv";
535            } else if (FORMAT == USE_TGA) {
536                    ext = "tga";
537            } else {
538                    fprintf(stderr, "Bug: should not reach this path code -- please report to xvid-devel@xvid.org with command line options used");
539                    exit(-1);
540            }
541    
542            sprintf(filename, "%s.%s", prefix, ext);
543    
544            if (FORMAT == USE_PNM) {
545                    ret = write_pnm(filename, image);
546            } else if (FORMAT == USE_YUV) {
547                    ret = write_yuv(filename, image);
548            } else {
549                    ret = write_tga(filename, image);
550            }
551    
552            return(ret);
553    }
554    
555    static int write_tga(char *filename, unsigned char *image)
556    {
557            FILE * f;
558            char hdr[18];
559    
560            f = fopen(filename, "wb");
561            if ( f == NULL) {
562                    return -1;
563            }
564    
565            hdr[0]  = 0; /* ID length */
566            hdr[1]  = 0; /* Color map type */
567            hdr[2]  = (BPP>1)?2:3; /* Uncompressed true color (2) or greymap (3) */
568            hdr[3]  = 0; /* Color map specification (not used) */
569            hdr[4]  = 0; /* Color map specification (not used) */
570            hdr[5]  = 0; /* Color map specification (not used) */
571            hdr[6]  = 0; /* Color map specification (not used) */
572            hdr[7]  = 0; /* Color map specification (not used) */
573            hdr[8]  = 0; /* LSB X origin */
574            hdr[9]  = 0; /* MSB X origin */
575            hdr[10] = 0; /* LSB Y origin */
576            hdr[11] = 0; /* MSB Y origin */
577            hdr[12] = (XDIM>>0)&0xff; /* LSB Width */
578            hdr[13] = (XDIM>>8)&0xff; /* MSB Width */
579            if (BPP > 1) {
580                    hdr[14] = (YDIM>>0)&0xff; /* LSB Height */
581                    hdr[15] = (YDIM>>8)&0xff; /* MSB Height */
582            } else {
583                    hdr[14] = ((YDIM*3)>>1)&0xff; /* LSB Height */
584                    hdr[15] = ((YDIM*3)>>9)&0xff; /* MSB Height */
585            }
586            hdr[16] = BPP*8;
587            hdr[17] = 0x00 | (1<<5) /* Up to down */ | (0<<4); /* Image descriptor */
588    
589            /* Write header */
590            fwrite(hdr, 1, sizeof(hdr), f);
591    
592    #ifdef ARCH_IS_LITTLE_ENDIAN
593            /* write first plane */
594            fwrite(image, 1, XDIM*YDIM*BPP, f);
595    #else
596            {
597                    int i;
598                    for (i=0; i<XDIM*YDIM*BPP;i+=BPP) {
599                            if (BPP == 1) {
600                                    fputc(*(image+i), f);
601                            } else if (BPP == 2) {
602                                    fputc(*(image+i+1), f);
603                                    fputc(*(image+i+0), f);
604                            } else if (BPP == 3) {
605                                    fputc(*(image+i+2), f);
606                                    fputc(*(image+i+1), f);
607                                    fputc(*(image+i+0), f);
608                            } else if (BPP == 4) {
609                                    fputc(*(image+i+3), f);
610                                    fputc(*(image+i+2), f);
611                                    fputc(*(image+i+1), f);
612                                    fputc(*(image+i+0), f);
613                            }
614                    }
615            }
616    #endif
617    
618            /* Write Y and V planes for YUV formats */
619            if (BPP == 1) {
620                    int i;
621    
622                    /* Write the two chrominance planes */
623                    for (i=0; i<YDIM/2; i++) {
624                            fwrite(image+XDIM*YDIM + i*XDIM/2, 1, XDIM/2, f);
625                            fwrite(image+5*XDIM*YDIM/4 + i*XDIM/2, 1, XDIM/2, f);
626                    }
627          }          }
         dectime += msecond();  
         divx_ptr += m4v_size;  
628    
         totalsize += m4v_size;  
629    
630          printf("Frame %5d: dectime =%6.1f ms length=%7d bytes \n",          /* Close the file */
631                          filenr, dectime*1000, m4v_size);          fclose(f);
632    
633          if (save_m4v_flag)          return(0);
634    }
635    
636    static int write_pnm(char *filename, unsigned char *image)
637          {          {
638                  sprintf(filename, "%sframe%05d.m4v", filepath, filenr);          FILE * f;
639                  filehandle = fopen(filename, "wb");  
640                  fwrite(divx_buffer, m4v_size, 1, filehandle);          f = fopen(filename, "wb");
641                  fclose(filehandle);          if ( f == NULL) {
642                    return -1;
643          }          }
         totaldectime += dectime;  
644    
645            if (BPP == 1) {
646                    int i;
647                    fprintf(f, "P5\n%i %i\n255\n", XDIM, YDIM*3/2);
648    
649                    fwrite(image, 1, XDIM*YDIM, f);
650    
651  /*********************************************************************/                  for (i=0; i<YDIM/2;i++) {
652  /*        analyse the decoded frame and compare to original          */                          fwrite(image+XDIM*YDIM + i*XDIM/2, 1, XDIM/2, f);
653  /*********************************************************************/                          fwrite(image+5*XDIM*YDIM/4 + i*XDIM/2, 1, XDIM/2, f);
654                    }
655            } else if (BPP == 3) {
656                    int i;
657                    fprintf(f, "P6\n#xvid\n%i %i\n255\n", XDIM, YDIM);
658                    for (i=0; i<XDIM*YDIM*3; i+=3) {
659    #ifdef ARCH_IS_LITTLE_ENDIAN
660                            fputc(image[i+2], f);
661                            fputc(image[i+1], f);
662                            fputc(image[i+0], f);
663    #else
664                            fputc(image[i+0], f);
665                            fputc(image[i+1], f);
666                            fputc(image[i+2], f);
667    #endif
668                    }
669            }
670    
671            fclose(f);
672    
673            return 0;
674    }
675    
676          if (save_dec_flag)  static int write_yuv(char *filename, unsigned char *image)
677          {          {
678                  sprintf(filename, "%sdec%05d.pgm", filepath, filenr);          FILE * f;
679                  write_pgm(filename,out_buffer);  
680            f = fopen(filename, "ab+");
681            if ( f == NULL) {
682                    return -1;
683          }          }
684    
685          filenr++;          if (BPP == 1) {
686                    int i;
687    
688     } while ( (status>=0) && (filenr<ABS_MAXFRAMENR) );                  fwrite(image, 1, XDIM*YDIM, f);
689    
690                    for (i=0; i<YDIM/2;i++) {
691                            fwrite(image+XDIM*YDIM + i*XDIM/2, 1, XDIM/2, f);
692                            fwrite(image+5*XDIM*YDIM/4 + i*XDIM/2, 1, XDIM/2, f);
693                    }
694            }
695    
696  /*********************************************************************/          fclose(f);
 /*     calculate totals and averages for output, print results       */  
 /*********************************************************************/  
697    
698          totalsize    /= filenr;          return 0;
699          totaldectime /= filenr;  }
700    
701          fprintf(stderr,"Avg: dectime %5.2f ms, %5.2f fps, filesize =%d\n",  /*****************************************************************************
702                  1000*totaldectime, 1./totaldectime, totalsize);   * Routines for decoding: init decoder, use, and stop decoder
703     ****************************************************************************/
704    
705  /*********************************************************************/  /* init decoder before first run */
706  /*                         XviD PART  Stop                           */  static int
707  /*********************************************************************/  dec_init(int use_assembler, int debug_level)
708    {
709            int ret;
710    
711  release_all:          xvid_gbl_init_t   xvid_gbl_init;
712            xvid_dec_create_t xvid_dec_create;
713    
714            /* Reset the structure with zeros */
715            memset(&xvid_gbl_init, 0, sizeof(xvid_gbl_init_t));
716            memset(&xvid_dec_create, 0, sizeof(xvid_dec_create_t));
717    
718            /*------------------------------------------------------------------------
719             * XviD core initialization
720             *----------------------------------------------------------------------*/
721    
722            /* Version */
723            xvid_gbl_init.version = XVID_VERSION;
724    
725            /* Assembly setting */
726            if(use_assembler)
727    #ifdef ARCH_IS_IA64
728                    xvid_gbl_init.cpu_flags = XVID_CPU_FORCE | XVID_CPU_IA64;
729    #else
730            xvid_gbl_init.cpu_flags = 0;
731    #endif
732            else
733                    xvid_gbl_init.cpu_flags = XVID_CPU_FORCE;
734    
735            xvid_gbl_init.debug = debug_level;
736    
737            xvid_global(NULL, 0, &xvid_gbl_init, NULL);
738    
739            /*------------------------------------------------------------------------
740             * XviD encoder initialization
741             *----------------------------------------------------------------------*/
742    
743          if (dec_handle)          /* Version */
744            xvid_dec_create.version = XVID_VERSION;
745    
746            /*
747             * Image dimensions -- set to 0, xvidcore will resize when ever it is
748             * needed
749             */
750            xvid_dec_create.width = 0;
751            xvid_dec_create.height = 0;
752    
753            ret = xvid_decore(NULL, XVID_DEC_CREATE, &xvid_dec_create, NULL);
754    
755            dec_handle = xvid_dec_create.handle;
756    
757            return(ret);
758    }
759    
760    /* decode one frame  */
761    static int
762    dec_main(unsigned char *istream,
763                     unsigned char *ostream,
764                     int istream_size,
765                     xvid_dec_stats_t *xvid_dec_stats)
766          {          {
767                  status = dec_stop();  
768                  if (status)          int ret;
769                          printf("Decore RELEASE problem return value %d\n", status);  
770            xvid_dec_frame_t xvid_dec_frame;
771    
772            /* Reset all structures */
773            memset(&xvid_dec_frame, 0, sizeof(xvid_dec_frame_t));
774            memset(xvid_dec_stats, 0, sizeof(xvid_dec_stats_t));
775    
776            /* Set version */
777            xvid_dec_frame.version = XVID_VERSION;
778            xvid_dec_stats->version = XVID_VERSION;
779    
780            /* No general flags to set */
781            xvid_dec_frame.general          = 0;
782    
783            /* Input stream */
784            xvid_dec_frame.bitstream        = istream;
785            xvid_dec_frame.length           = istream_size;
786    
787            /* Output frame structure */
788            xvid_dec_frame.output.plane[0]  = ostream;
789            xvid_dec_frame.output.stride[0] = XDIM*BPP;
790            xvid_dec_frame.output.csp = CSP;
791    
792            ret = xvid_decore(dec_handle, XVID_DEC_DECODE, &xvid_dec_frame, xvid_dec_stats);
793    
794            return(ret);
795          }          }
796    
797  free_all_memory:  /* close decoder to release resources */
798          free(out_buffer);  static int
799          free(divx_buffer);  dec_stop()
800    {
801            int ret;
802    
803    return 0;          ret = xvid_decore(dec_handle, XVID_DEC_DESTROY, NULL, NULL);
804    
805            return(ret);
806  }  }

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.25

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