[svn] / trunk / xvidcore / examples / xvid_stat.c Repository:
ViewVC logotype

Annotation of /trunk/xvidcore/examples/xvid_stat.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 209 - (view) (download)

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

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