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

Legend:
Removed from v.376  
changed lines
  Added in v.1882

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