[cvs] / xvidcore / examples / xvid_bstat.c Repository:
ViewVC logotype

Diff of /xvidcore/examples/xvid_bstat.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.1, Sun Sep 29 16:38:06 2002 UTC revision 1.1.2.2, Sun Dec 29 21:09:09 2002 UTC
# Line 0  Line 1 
1    /**************************************************************************
2     *
3     *      XVID MPEG-4 VIDEO CODEC - Example for encoding and decoding
4     *
5     *      This program is free software; you can redistribute it and/or modify
6     *      it under the terms of the GNU General Public License as published by
7     *      the Free Software Foundation; either version 2 of the License, or
8     *      (at your option) any later version.
9     *
10     *      This program is distributed in the hope that it will be useful,
11     *      but WITHOUT ANY WARRANTY; without even the implied warranty of
12     *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     *      GNU General Public License for more details.
14     *
15     *      You should have received a copy of the GNU General Public License
16     *      along with this program; if not, write to the Free Software
17     *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18     *
19     *************************************************************************/
20    
21    /************************************************************************
22     *
23     *  PSNR and Speed test routine for XviD using the XviD-API
24     *  (C) Christoph Lampert, 2002/04/13
25     *
26     *  This is programs isn't finished... it's just a quick hack because people
27     *  asked for a version of xvid_bstat with bframe-support
28     *
29     *  A sequence of YUV pics in PGM file format is encoded and decoded
30     *  The speed is measured and PSNR of decoded picture is calculated.
31     *
32     *  The program is plain C and needs no libraries except for libxvidcore,
33     *  and maths-lib ,so with UN*X you simply compile by
34     *
35     *   gcc xvid_bstat.c -lxvidcore -lm -o xvid_bstat
36     *
37     *  Run without or with illegal parameters, then PGM input input is read
38     *  from stdin.
39     *
40     *  Parameters are: xvid_bstat XDIM YDIM QUALITY BITRATE/QUANTIZER FRAMERATE
41     *                            MAX_FRAMES  MAX_BFRAMES  BFRAME_QUANT
42     *
43     *  if XDIM or YDIM are illegal (e.g. 0), they are ignored and input is
44     *  considered to be PGM. Otherwise (X and Y both greater than 0) raw YUV
45     *  is expected, as e.g. the standard MPEG test-files, like "foreman"
46     *
47     *  0 <= QUALITY <= 6  (default 5)
48     *
49     *  BITRATE is in kbps (default 900),
50     *      if BITRATE<32, then value is taken is fixed QUANTIZER
51     *
52     *  FRAMERATE is a float (with or without decimal dot), default is 25.00
53     *
54     *  MAX_FRAMEs is the total number of frames to be encoded (minimum 1)
55     *
56     *  MAX_BFRAMES is the maximum number of bframes in a row (or -1)
57     *
58     *  BFRAME_QUANT is the quantization ratio for B-frames (in percent)
59     *
60     *  input/output and m4v-output is saved, if corresponding flags are set
61     *
62     *  PGM input must in a very specific format, see read_pgmheader
63     *  it can be generated e.g. from MPEG2 by    mpeg2dec -o pgmpipe
64     *
65     ************************************************************************/
66    
67    /************************************************************************
68     *
69     *  For EXAMPLES how to use this, see the seperate file xvid_bstat.examples
70     *
71     ************************************************************************/
72    
73    #define BFRAMES
74    
75    #include <stdio.h>
76    #include <stdlib.h>
77    #include <math.h>               // needed for log10
78    #include <sys/time.h>           // only needed for gettimeofday
79    
80    #include "xvid.h"               /* comes with XviD */
81    
82    int motion_presets[7] = {
83            0,                                                              // Q 0
84            0,                                                // Q 1
85            0,                                              // Q 2
86            PMV_HALFPELREFINE16,                            // Q 3
87            PMV_HALFPELREFINE16,                            // Q 4
88            PMV_HALFPELREFINE16 | PMV_HALFPELREFINE8,               // Q 5
89            PMV_HALFPELREFINE16 | PMV_EXTSEARCH16   // Q 6
90                            | PMV_USESQUARES8 | PMV_USESQUARES16 | PMV_HALFPELREFINE8
91            };
92    
93    int general_presets[7] = {
94            XVID_H263QUANT, /* or use XVID_MPEGQUANT */             // Q 0
95            XVID_H263QUANT,                                         // Q 1
96            XVID_H263QUANT | XVID_HALFPEL,                                 // Q 2
97            XVID_H263QUANT | XVID_HALFPEL,                          // Q 3
98            XVID_H263QUANT | XVID_HALFPEL | XVID_INTER4V,           // Q 4
99            XVID_H263QUANT | XVID_HALFPEL | XVID_INTER4V,           // Q 5
100            XVID_H263QUANT | XVID_HALFPEL | XVID_INTER4V };         // Q 6
101    
102    
103    /* my default values for encoding */
104    
105    #define ABS_MAXFRAMENR 9999               // max number of frames
106    
107    int ARG_BITRATE=800;
108    int ARG_QUANTI=0;
109    
110    int ARG_QUALITY =6;
111    int ARG_MINQUANT=1;
112    int ARG_MAXQUANT=31;
113    float ARG_FRAMERATE=25.00;
114    
115    int ARG_MAX_BFRAMES=2;
116    int ARG_BQUANT_RATIO=200;
117    int NUMBUFFERS;
118    
119    int ARG_MAXFRAMENR=ABS_MAXFRAMENR;
120    
121    
122    #define MAX(A,B) ( ((A)>(B)) ? (A) : (B) )
123    #define SMALL_EPS 1e-10
124    
125    /* these are global variables. Not very elegant, but easy, and this is an easy program */
126    
127    int XDIM=0;
128    int YDIM=0;     // will be set when reading first image
129    int i,filenr = 0;
130    
131    int save_m4v_flag = 0;          // save MPEG4-bytestream?
132    int save_dec_flag = 1;          // save decompressed bytestream?
133    int save_ref_flag = 0;          //
134    
135    int pgmflag = 0;                // a flag, if input is in PGM format, overwritten in init-phase
136    char filepath[256] = "./";      // the path where to save output
137    
138    void *enc_handle = NULL;                // internal structures (handles) for encoding
139    void *dec_handle = NULL;                // and decoding
140    
141    
142    /*********************************************************************/
143    /*                     "statistical" functions                       */
144    /*                                                                   */
145    /*  these are not needed for encoding or decoding, but for measuring */
146    /*  time and quality, there in nothing specific to XviD in these     */
147    /*                                                                   */
148    /*********************************************************************/
149    
150    double msecond()
151    /* return the current time in seconds(!)  */
152    {
153            struct timeval  tv;
154            gettimeofday(&tv, 0);
155            return tv.tv_sec + tv.tv_usec * 1.0e-6;
156    }
157    
158    
159    
160    double absdistq(int x,int y, unsigned char* buf1, int stride1, unsigned char* buf2, int stride2)
161    /* returns the sum of squared distances (SSD) between two images of dimensions x times y */
162    {
163            double dist=0.;
164            int i,j,val;
165    
166            for (i=0;i<y;i++)
167            {
168                    val=0;
169                    for (j=0;j<x;j++)
170                            val+= ((int)buf1[j]-(int)buf2[j])*((int)buf1[j]-(int)buf2[j]);
171    
172                    dist += (double)val;
173                    buf1 += stride1;
174                    buf2 += stride2;
175            }
176       return dist/(x*y);
177    }
178    
179    
180    double PSNR(int x,int y, unsigned char* buf1, int stride1, unsigned char* buf2, int stride2 )
181    /* return the PSNR between to images                                               */
182    /* this is a logarithmic measure for "quality" from the world of signal processing */
183    /* if you don't know what it is, simply accept that higher values are better       */
184    {
185       return 10*(log10(255*255)-log10( absdistq(x, y, buf1, stride1, buf2, stride2) ));
186    }
187    
188    
189    /*********************************************************************/
190    /*                    input and output functions                     */
191    /*                                                                   */
192    /* the are small and simple routines to read and write PGM and YUV   */
193    /* image. It's just for convenience, again nothing specific to XviD  */
194    /*                                                                   */
195    /*********************************************************************/
196    
197    int read_pgmheader(FILE* handle)
198    {
199            int bytes,xsize,ysize,depth;
200            char dummy[2];
201    
202            bytes = fread(dummy,1,2,handle);
203    
204            if ( (bytes < 2) || (dummy[0] != 'P') || (dummy[1] != '5' ))
205                    return 1;
206            fscanf(handle,"%d %d %d",&xsize,&ysize,&depth);
207            if ( (xsize > 1440) || (ysize > 2880 ) || (depth != 255) )
208            {
209                    fprintf(stderr,"%d %d %d\n",xsize,ysize,depth);
210                    return 2;
211            }
212            if ( (XDIM==0) || (YDIM==0) )
213            {       XDIM=xsize;
214                    YDIM=ysize;
215            }
216    
217            return 0;
218    }
219    
220    int read_pgmdata(FILE* handle, unsigned char *image)
221    {
222            int i,status;
223            char dummy;
224    
225            unsigned char* buff1_ptr2 = image + XDIM*YDIM;
226            unsigned char* buff1_ptr3 = image + XDIM*YDIM + XDIM/2*YDIM/2;
227    
228            fread(image,XDIM*YDIM,1,stdin); // read Y component of picture
229    
230            for (i=0;i<YDIM/2;i++)
231            {
232                    fread(buff1_ptr2,XDIM/2,1,stdin);        // read U
233                    buff1_ptr2 += XDIM/2;
234                    fread(buff1_ptr3,XDIM/2,1,stdin);        // read V
235                    buff1_ptr3 += XDIM/2;
236            }
237            fread(&dummy,1,1,handle);       //  I don't know why, but this seems needed
238            return 0;
239    }
240    
241    int read_yuvdata(FILE* handle, unsigned char *image)
242    {       int i;
243            char dummy;
244    
245            unsigned char* buff1_ptr2 = image + XDIM*YDIM;
246            unsigned char* buff1_ptr3 = image + XDIM*YDIM + XDIM/2*YDIM/2;
247    
248            if (fread(image,XDIM,YDIM*3/2,stdin) != YDIM*3/2)
249                    return 1;
250            else
251                    return 0;
252    }
253    
254    int write_pgm(char *filename, unsigned char *image)
255    {
256            FILE *filehandle;
257            filehandle=fopen(filename,"wb");
258            if (filehandle)
259            {
260                    fprintf(filehandle,"P5\n\n");           //
261                    fprintf(filehandle,"%d %d 255\n",XDIM,YDIM*3/2);
262                    fwrite(image,XDIM,YDIM*3/2,filehandle);
263                    fclose(filehandle);
264                    return 0;
265            }
266            else
267                    return 1;
268    }
269    
270    
271    
272    /*********************************************************************/
273    /* Routines for encoding: init encoder, frame step, release encoder  */
274    /*********************************************************************/
275    
276    #define FRAMERATE_INCR 1001
277    
278    
279    int enc_init(int use_assembler)
280    {       /* initialize encoder for first use, pass all needed parameters to the codec */
281            int xerr;
282    
283            XVID_INIT_PARAM xinit;
284            XVID_ENC_PARAM xparam;
285    
286            if(use_assembler)
287    
288    #ifdef ARCH_IA64
289                    xinit.cpu_flags = XVID_CPU_FORCE | XVID_CPU_IA64;
290    #else
291                    xinit.cpu_flags = 0;
292    #endif
293    
294            else
295                    xinit.cpu_flags = XVID_CPU_FORCE;
296    
297            xvid_init(NULL, 0, &xinit, NULL);
298    
299            xparam.width = XDIM;
300            xparam.height = YDIM;
301            if ((ARG_FRAMERATE - (int)ARG_FRAMERATE) < SMALL_EPS)
302            {
303                    xparam.fincr = 1;
304                    xparam.fbase = (int)ARG_FRAMERATE;
305            }
306            else
307            {
308                    xparam.fincr = FRAMERATE_INCR;
309                    xparam.fbase = (int)(FRAMERATE_INCR * ARG_FRAMERATE);
310            }
311            xparam.rc_reaction_delay_factor = 16;
312        xparam.rc_averaging_period = 100;
313        xparam.rc_buffer = 10;
314    
315                    /* I use small values here, since we will not
316                       encode whole movies, but short clips */
317    
318            xparam.rc_bitrate = ARG_BITRATE*1000;
319            xparam.min_quantizer = 1;
320            xparam.max_quantizer = 31;
321            xparam.max_key_interval = (int)ARG_FRAMERATE*10;
322    
323            xparam.global = XVID_GLOBAL_DX50BVOP;//XVID_GLOBAL_DEBUG; //XVID_GLOBAL_PACKED|XVID_GLOBAL_DX50BVOP;//|XVID_GLOBAL_DEBUG;
324            xparam.max_bframes = ARG_MAX_BFRAMES;
325            xparam.bquant_ratio = ARG_BQUANT_RATIO;
326    
327            xparam.frame_drop_ratio = 0;
328    
329            xerr = xvid_encore(NULL, XVID_ENC_CREATE, &xparam, NULL);
330            enc_handle=xparam.handle;
331    
332            return xerr;
333    }
334    
335    int  enc_stop()
336    {       int xerr;
337    
338            xerr = xvid_encore(enc_handle, XVID_ENC_DESTROY, NULL, NULL);
339            return xerr;
340    }
341    
342    int  enc_main(unsigned char* image, unsigned char* bitstream, int *streamlength, int* frametype)
343    {       int xerr;
344    
345            XVID_ENC_FRAME xframe;
346            XVID_ENC_STATS xstats;
347    
348            xframe.bitstream = bitstream;
349            xframe.length = -1;     // this is written by the routine
350    
351            xframe.image = image;
352            xframe.colorspace = XVID_CSP_YV12;      // defined in <xvid.h>
353    
354            xframe.intra = -1; // let the codec decide between I-frame (1) and P-frame (0)
355    
356            xframe.quant = ARG_QUANTI;      // if quant != 0, use a fixed quant (and ignore bitrate)
357            xframe.bquant = 0;
358    
359    
360            xframe.motion = motion_presets[ARG_QUALITY];
361            xframe.general = general_presets[ARG_QUALITY];
362            xframe.quant_intra_matrix = xframe.quant_inter_matrix = NULL;
363    
364            xerr = xvid_encore(enc_handle, XVID_ENC_ENCODE, &xframe, &xstats);
365    
366    /*              enc_result->is_key_frame = xframe.intra;
367                    enc_result->quantizer = xframe.quant;
368                    enc_result->total_bits = xframe.length * 8;
369                    enc_result->motion_bits = xstats.hlength * 8;
370                    enc_result->texture_bits = enc_result->total_bits - enc_result->motion_bits;
371    */
372    
373    /*  This is statictical data, e.g. for 2-pass.
374        If you are not interested in any of this, you can use NULL instead of &xstats
375    */
376            *frametype = xframe.intra;
377            *streamlength = xframe.length;
378    
379            return xerr;
380    }
381    
382    
383    /*********************************************************************/
384    /* Routines for decoding: init encoder, frame step, release encoder  */
385    /*********************************************************************/
386    
387    int dec_init(int use_assembler) /* init decoder before first run */
388    {
389            int xerr;
390    
391            XVID_INIT_PARAM xinit;
392            XVID_DEC_PARAM xparam;
393    
394                    if(use_assembler)
395    
396    #ifdef ARCH_IA64
397                            xinit.cpu_flags = XVID_CPU_FORCE | XVID_CPU_IA64;
398    #else
399                            xinit.cpu_flags = 0;
400    #endif
401    
402                    else
403                            xinit.cpu_flags = XVID_CPU_FORCE;
404    
405            xvid_init(NULL, 0, &xinit, NULL);
406            xparam.width = XDIM;
407            xparam.height = YDIM;
408    
409            xerr = xvid_decore(NULL, XVID_DEC_CREATE, &xparam, NULL);
410            dec_handle = xparam.handle;
411    
412            return xerr;
413    }
414    
415    int dec_main(unsigned char *m4v_buffer, unsigned char *out_buffer, int *m4v_size)
416    {       /* decode one frame  */
417    
418            int xerr;
419            XVID_DEC_FRAME xframe;
420    
421            xframe.bitstream = m4v_buffer;
422            xframe.length = 1234; // *m4v_size;
423            xframe.image = out_buffer;
424            xframe.stride = XDIM;
425            xframe.colorspace = XVID_CSP_YV12;             // XVID_CSP_USER is fastest (no memcopy involved)
426    
427            xerr = xvid_decore(dec_handle, XVID_DEC_DECODE, &xframe, NULL);
428    
429                    *m4v_size = xframe.length;
430            return xerr;
431    }
432    
433    int dec_stop()  /* close decoder to release resources */
434    {
435            int xerr;
436            xerr = xvid_decore(dec_handle, XVID_DEC_DESTROY, NULL, NULL);
437    
438            return xerr;
439    }
440    
441    
442    /*********************************************************************/
443    /*                          Main program                             */
444    /*********************************************************************/
445    
446    int main(int argc, char *argv[])
447    {
448      unsigned char *divx_buffer = NULL;
449      unsigned char *divx_read = NULL;
450      unsigned char *divx_write = NULL;
451      unsigned char **in_buffer;
452      unsigned char *out_buffer = NULL;
453      unsigned char *divx_pos;
454    
455            int k;
456    
457      double enctime,dectime;
458      double totalenctime=0.;
459      double totaldectime=0.;
460    
461      long totalsize=0;
462      int status;
463    
464      int loop;
465      int m4v_size=0;
466      int enc_size=0;
467      int frame_type[ABS_MAXFRAMENR];
468      int Iframes=0, Pframes=0, Bframes=0, use_assembler=0;
469      double framepsnr[ABS_MAXFRAMENR];
470    
471      double Ipsnr=0.,Imaxpsnr=0.,Iminpsnr=999.,Ivarpsnr=0.;
472      double Ppsnr=0.,Pmaxpsnr=0.,Pminpsnr=999.,Pvarpsnr=0.;
473      double Bpsnr=0.,Bmaxpsnr=0.,Bminpsnr=999.,Bvarpsnr=0.;
474    
475      char filename[256];
476    
477      FILE *filehandle;
478    
479    /* read YUV in pgm format from stdin */
480      if (!pgmflag)
481      {
482            pgmflag = 1;
483    
484            if (argc==2 && !strcmp(argv[1],"-asm"))
485              use_assembler = 1;
486            if (argc>=3)
487            {       XDIM = atoi(argv[1]);
488                    YDIM = atoi(argv[2]);
489                    if ( (XDIM <= 0) || (XDIM >= 2048) || (YDIM <=0) || (YDIM >= 2048) )
490                    {       fprintf(stderr,"Wrong frames size %d %d, trying PGM \n",XDIM, YDIM);
491                    }
492                    else
493                    {
494                            YDIM = YDIM*3/2; /* for YUV */
495                            pgmflag = 0;
496                    }
497            }
498      }
499    
500      if (pgmflag)
501      {     if (read_pgmheader(stdin))
502                {
503                  printf("Wrong input format, I want YUV encapsulated in PGM\n");
504                  return 1;
505                }
506      }
507    
508      if (argc>=4)
509      {     ARG_QUALITY = atoi(argv[3]);
510            if ( (ARG_QUALITY < 0) || (ARG_QUALITY > 6) )
511                    { fprintf(stderr,"Wrong Quality\n"); return -1; }
512            else
513                      printf("Quality %d\n",ARG_QUALITY);
514      }
515      if (argc>=5)
516      {     ARG_BITRATE = atoi(argv[4]);
517            if ( (ARG_BITRATE <= 0) )
518                    { fprintf(stderr,"Wrong Bitrate\n"); return -1; }
519            if ( (ARG_BITRATE <= 31) )
520                    { ARG_QUANTI = ARG_BITRATE;
521                      ARG_BITRATE=0;
522                      printf("Quantizer %d\n",ARG_QUANTI);
523                    }
524            else
525                      printf("Bitrate %d kbps\n",ARG_BITRATE);
526      }
527      if (argc>=6)
528      {     ARG_FRAMERATE = (float)atof(argv[5]);
529            if ( (ARG_FRAMERATE <= 0) )
530                    { fprintf(stderr,"Wrong Fraterate %s \n",argv[5]); return -1; }
531            printf("Framerate %6.3f fps\n",ARG_FRAMERATE);
532      }
533    
534      if (argc>=7)
535      {     ARG_MAXFRAMENR = atoi(argv[6]);
536            if ( (ARG_MAXFRAMENR <= 0) )
537             { fprintf(stderr,"Wrong number of frames\n"); return -1; }
538            printf("max. Framenr. %d\n",ARG_MAXFRAMENR);
539      }
540    
541      if (argc>=8)
542      {     ARG_MAX_BFRAMES = atoi(argv[7]);
543            if ( (ARG_MAX_BFRAMES < -1) )
544             { fprintf(stderr,"Wrong number of bframes\n"); return -1; }
545            printf("max. B-Frames %d\n",ARG_MAX_BFRAMES);
546      }
547      NUMBUFFERS = ARG_MAX_BFRAMES+2;
548    
549      if (argc>=9)
550      {     ARG_BQUANT_RATIO = atoi(argv[8]);
551            if ( (ARG_BQUANT_RATIO <= 0) )
552             { fprintf(stderr,"Wrong bquant ratio\n"); return -1; }
553            printf("bquant ratio %d\n",ARG_BQUANT_RATIO);
554      }
555    
556    /* now we know the sizes, so allocate memory */
557    
558      in_buffer = malloc( NUMBUFFERS*sizeof(unsigned char*) );
559      if (!in_buffer)
560            goto free_all_memory;
561    
562      for (k=0;k<NUMBUFFERS;k++)
563            in_buffer[k] = NULL;
564    
565      for (k=0;k<NUMBUFFERS;k++)
566      {
567              in_buffer[k] = (unsigned char *) malloc(XDIM*YDIM);
568              if (!in_buffer[k])
569            goto free_all_memory;   // goto is one of the most underestimated instructions in C !!!
570      }
571    
572    #define DIVX_SIZE (XDIM*YDIM*200)
573      divx_buffer = (unsigned char *) malloc(2*DIVX_SIZE);  // this should really be enough memory!
574      if (!divx_buffer)
575        goto free_all_memory;
576    
577      divx_write = divx_buffer;
578      divx_read = divx_buffer;
579    
580      YDIM = YDIM*2/3; // PGM is YUV 4:2:0 format, so real image height is *2/3 of PGM picture
581    
582      out_buffer = (unsigned char *) malloc(XDIM*YDIM*4);
583      if (!out_buffer)
584        goto free_all_memory;
585    
586    
587    
588    
589    
590            Iframes=0, Pframes=0, Bframes=0, use_assembler=1;
591            filenr=0;
592    
593    /*********************************************************************/
594    /*                         XviD PART  Start                          */
595    /*********************************************************************/
596    
597    
598      status = enc_init(use_assembler);
599            if (status)
600            {
601                    printf("Encore INIT problem, return value %d\n", status);
602                    goto release_all;
603            }
604    
605            status = dec_init(use_assembler);
606            if (status)
607            {
608                    printf("Decore INIT problem, return value %d\n", status);
609                    goto release_all;
610            }
611    
612    
613    /*********************************************************************/
614    /*                               Main loop                           */
615    /*********************************************************************/
616    
617      do
618        {
619            if (pgmflag)
620                  status = read_pgmdata(stdin, in_buffer[filenr%NUMBUFFERS]);       // read PGM data (YUV-format)
621            else
622                  status = read_yuvdata(stdin, in_buffer[filenr%NUMBUFFERS]);       // read raw data (YUV-format)
623    
624        if (status)
625            {
626              // Couldn't read image, most likely end-of-file
627              continue;
628            }
629    
630    
631        if (save_ref_flag)
632            {
633                    sprintf(filename, "%s%05d.pgm", filepath, filenr);
634                    write_pgm(filename,in_buffer[filenr%NUMBUFFERS]);
635            }
636    
637    
638    /*********************************************************************/
639    /*               analyse this frame before encoding                  */
640    /*********************************************************************/
641    
642    //      nothing is done here at the moment, but you could e.g. create
643    //      histograms or measure entropy or apply preprocessing filters...
644    
645    /*********************************************************************/
646    /*               encode and decode this frame                        */
647    /*********************************************************************/
648    
649            enctime = -msecond();
650            status = enc_main(in_buffer[filenr%NUMBUFFERS], divx_write, &enc_size, &frame_type[filenr]);
651            enctime += msecond();
652    
653            if (status)
654                    printf("Enc_main status: %d\n",status);
655            totalenctime += enctime;
656            totalsize += enc_size;
657    
658            printf("Frame %5d: intra %d, enctime =%6.1f ms length=%7d bytes \n",
659                     filenr, frame_type[filenr], enctime*1000, enc_size);
660    
661            if (save_m4v_flag)
662            {
663                    sprintf(filename, "%sframe%05d.m4v", filepath, filenr);
664                    filehandle = fopen(filename, "wb");
665                    fwrite(divx_write, enc_size, 1, filehandle);
666                    fclose(filehandle);
667            }
668            divx_write += enc_size;
669            *divx_write=0;                  /* some kind of padding */
670            *(divx_write+1)=0;
671            *(divx_write+2)=0;
672            *(divx_write+3)=0;
673    
674            if (filenr<ARG_MAX_BFRAMES)
675                    goto nextfile;
676    
677            do {
678                    dectime = -msecond();
679                    status = dec_main(divx_read, out_buffer, &m4v_size);
680                    dectime += msecond();
681                    divx_read += m4v_size;
682                    printf("DecFrame %5d: length=%7d bytes (left: %7d)\n", filenr-(ARG_MAX_BFRAMES), m4v_size,
683                            (divx_write-divx_read));
684    
685            } while (m4v_size<=7);
686    
687            if (status)
688                    printf("dec_main Status =%d\n",status);
689            totaldectime += dectime;
690    
691    /*********************************************************************/
692    /*        analyse the decoded frame and compare to original          */
693    /*********************************************************************/
694    
695        if (filenr>ARG_MAX_BFRAMES) {
696                    framepsnr[filenr-(ARG_MAX_BFRAMES+1)] = PSNR(XDIM,YDIM, in_buffer[(filenr-(ARG_MAX_BFRAMES+1))%NUMBUFFERS], XDIM, out_buffer, XDIM );
697    
698                    printf("dectime =%6.1f ms PSNR %5.2f\n",dectime*1000, framepsnr[filenr-(ARG_MAX_BFRAMES+1)]);
699            }
700    
701        if (filenr>ARG_MAX_BFRAMES)
702            if (save_dec_flag)
703            {
704                    sprintf(filename, "%sdec%05d.pgm", filepath, filenr-(ARG_MAX_BFRAMES+1));
705                    write_pgm(filename,out_buffer);
706            }
707    
708    nextfile:
709    
710            filenr++;
711    
712            if ((int)(divx_read - divx_buffer) > DIVX_SIZE)
713            {
714                    memmove(divx_buffer, divx_read, (int)(divx_buffer+2*DIVX_SIZE-divx_read));
715                    divx_write -= (int)(divx_read-divx_buffer);
716                    divx_read = divx_buffer;
717                    fprintf(stderr,"DIVX-buffer copied\n");
718    
719            }
720    
721            if (pgmflag)
722                    status = read_pgmheader(stdin);         // because if this was the last PGM, stop now
723    
724       } while ( (!status) && (filenr<ARG_MAXFRAMENR) );
725    
726            printf("Status=%d filenr=%d\n",status,filenr);
727            filenr--;
728    
729    
730    /*********************************************************************/
731    /*     calculate totals and averages for output, print results       */
732    /*********************************************************************/
733    
734            if (filenr)
735            {       totalsize    /= filenr;
736                    totalenctime /= filenr;
737                    totaldectime /= filenr;
738            }
739    
740            for (i=0;i<filenr-(ARG_MAX_BFRAMES+1);i++)
741            {
742                    switch (frame_type[i])
743                    {
744                    case 0:
745                            Pframes++;
746                            Ppsnr += framepsnr[i];
747                            break;
748                    case 1:
749                            Iframes++;
750                            Ipsnr += framepsnr[i];
751                            break;
752                    case 2:
753                    default:
754                            Bframes++;
755                            Bpsnr += framepsnr[i];
756                            break;
757                    }
758            }
759    
760            if (Pframes)
761                    Ppsnr /= Pframes;
762            if (Iframes)
763                    Ipsnr /= Iframes;
764            if (Bframes)
765                    Bpsnr /= Bframes;
766    
767    
768            for (i=0;i<filenr-(ARG_MAX_BFRAMES+1);i++)      // calculate statistics for every frametype: P,I (and B)
769            {
770                    switch (frame_type[i])
771                    {
772                    case 0:
773                            if (framepsnr[i] > Pmaxpsnr)
774                                    Pmaxpsnr = framepsnr[i];
775                            if (framepsnr[i] < Pminpsnr)
776                                    Pminpsnr = framepsnr[i];
777                            Pvarpsnr += (framepsnr[i] - Ppsnr)*(framepsnr[i] - Ppsnr) /Pframes;
778                            break;
779                    case 1:
780                            if (framepsnr[i] > Imaxpsnr)
781                                    Imaxpsnr = framepsnr[i];
782                            if (framepsnr[i] < Pminpsnr)
783                                    Iminpsnr = framepsnr[i];
784                            Ivarpsnr += (framepsnr[i] - Ipsnr)*(framepsnr[i] - Ipsnr) /Iframes;
785                            break;
786                    case 2:
787                            if (framepsnr[i] > Bmaxpsnr)
788                                    Bmaxpsnr = framepsnr[i];
789                            if (framepsnr[i] < Pminpsnr)
790                                    Bminpsnr = framepsnr[i];
791                            Bvarpsnr += (framepsnr[i] - Bpsnr)*(framepsnr[i] - Bpsnr) /Bframes;
792                            break;
793                    }
794            }
795    
796            printf("Avg. Q%1d %2s ",ARG_QUALITY, (ARG_QUANTI ? " q" : "br"));
797            printf("%04d ",MAX(ARG_QUANTI,ARG_BITRATE));
798            printf("mbf %01d ",ARG_MAX_BFRAMES);
799            printf("bqr %03d ",ARG_MAX_BFRAMES>0 ? ARG_BQUANT_RATIO : 0);
800            printf("( %.3f bpp) ", (double)ARG_BITRATE*1000/XDIM/YDIM/ARG_FRAMERATE);
801            printf("size %6d ",totalsize);
802            printf("( %4d kbps ",(int)(totalsize*8*ARG_FRAMERATE/1000));
803            printf("/ %.3f bpp) ",(double)totalsize*8/XDIM/YDIM);
804            printf("enc: %6.1f fps, dec: %6.1f fps ",1/totalenctime, 1/totaldectime);
805            printf("PSNR P(%d): %5.2f ( %5.2f , %5.2f ; %5.4f ) ",Pframes,Ppsnr,Pminpsnr,Pmaxpsnr,sqrt(Pvarpsnr/filenr));
806            printf("I(%d): %5.2f ( %5.2f , %5.2f ; %5.4f ) ",Iframes,Ipsnr,Iminpsnr,Imaxpsnr,sqrt(Ivarpsnr/filenr));
807            if (Bframes)
808                    printf("B(%d): %5.2f ( %5.2f , %5.2f ; %5.4f ) ",Bframes,Bpsnr,Bminpsnr,Bmaxpsnr,sqrt(Bvarpsnr/filenr));
809            printf("\n");
810    
811    /*********************************************************************/
812    /*                         XviD PART  Stop                           */
813    /*********************************************************************/
814    
815    release_all:
816    
817            if (enc_handle)
818            {
819                    status = enc_stop();
820                    if (status)
821                            printf("Encore RELEASE problem return value %d\n", status);
822            }
823    
824            if (dec_handle)
825            {
826                    status = dec_stop();
827                    if (status)
828                            printf("Decore RELEASE problem return value %d\n", status);
829            }
830    
831    
832    free_all_memory:
833            free(out_buffer);
834            free(divx_buffer);
835            if (in_buffer)
836                    for (k=0;k<NUMBUFFERS;k++)
837                            free(in_buffer[k]);
838    
839      return 0;
840    }

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

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