[svn] / branches / dev-api-3 / xvidcore / examples / xvid_bstat.c Repository:
ViewVC logotype

Annotation of /branches/dev-api-3/xvidcore/examples/xvid_bstat.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 841 - (view) (download)

1 : chl 571 /**************************************************************************
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 : chl 751 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 : chl 571 };
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 : chl 841 xparam.bquant_offset= 0;
327 : chl 571
328 :     xparam.frame_drop_ratio = 0;
329 :    
330 :     xerr = xvid_encore(NULL, XVID_ENC_CREATE, &xparam, NULL);
331 :     enc_handle=xparam.handle;
332 :    
333 :     return xerr;
334 :     }
335 :    
336 :     int enc_stop()
337 :     { int xerr;
338 :    
339 :     xerr = xvid_encore(enc_handle, XVID_ENC_DESTROY, NULL, NULL);
340 :     return xerr;
341 :     }
342 :    
343 :     int enc_main(unsigned char* image, unsigned char* bitstream, int *streamlength, int* frametype)
344 :     { int xerr;
345 :    
346 :     XVID_ENC_FRAME xframe;
347 :     XVID_ENC_STATS xstats;
348 :    
349 :     xframe.bitstream = bitstream;
350 :     xframe.length = -1; // this is written by the routine
351 :    
352 :     xframe.image = image;
353 : chl 841 xframe.stride = XDIM;
354 : chl 571 xframe.colorspace = XVID_CSP_YV12; // defined in <xvid.h>
355 :    
356 :     xframe.intra = -1; // let the codec decide between I-frame (1) and P-frame (0)
357 :    
358 :     xframe.quant = ARG_QUANTI; // if quant != 0, use a fixed quant (and ignore bitrate)
359 :     xframe.bquant = 0;
360 :    
361 :    
362 :     xframe.motion = motion_presets[ARG_QUALITY];
363 :     xframe.general = general_presets[ARG_QUALITY];
364 :     xframe.quant_intra_matrix = xframe.quant_inter_matrix = NULL;
365 :    
366 :     xerr = xvid_encore(enc_handle, XVID_ENC_ENCODE, &xframe, &xstats);
367 :    
368 :     /* enc_result->is_key_frame = xframe.intra;
369 :     enc_result->quantizer = xframe.quant;
370 :     enc_result->total_bits = xframe.length * 8;
371 :     enc_result->motion_bits = xstats.hlength * 8;
372 :     enc_result->texture_bits = enc_result->total_bits - enc_result->motion_bits;
373 :     */
374 :    
375 :     /* This is statictical data, e.g. for 2-pass.
376 :     If you are not interested in any of this, you can use NULL instead of &xstats
377 :     */
378 :     *frametype = xframe.intra;
379 :     *streamlength = xframe.length;
380 :    
381 :     return xerr;
382 :     }
383 :    
384 :    
385 :     /*********************************************************************/
386 :     /* Routines for decoding: init encoder, frame step, release encoder */
387 :     /*********************************************************************/
388 :    
389 :     int dec_init(int use_assembler) /* init decoder before first run */
390 :     {
391 :     int xerr;
392 :    
393 :     XVID_INIT_PARAM xinit;
394 :     XVID_DEC_PARAM xparam;
395 :    
396 :     if(use_assembler)
397 :    
398 :     #ifdef ARCH_IA64
399 :     xinit.cpu_flags = XVID_CPU_FORCE | XVID_CPU_IA64;
400 :     #else
401 :     xinit.cpu_flags = 0;
402 :     #endif
403 :    
404 :     else
405 :     xinit.cpu_flags = XVID_CPU_FORCE;
406 :    
407 :     xvid_init(NULL, 0, &xinit, NULL);
408 :     xparam.width = XDIM;
409 :     xparam.height = YDIM;
410 :    
411 :     xerr = xvid_decore(NULL, XVID_DEC_CREATE, &xparam, NULL);
412 :     dec_handle = xparam.handle;
413 :    
414 :     return xerr;
415 :     }
416 :    
417 :     int dec_main(unsigned char *m4v_buffer, unsigned char *out_buffer, int *m4v_size)
418 :     { /* decode one frame */
419 :    
420 :     int xerr;
421 :     XVID_DEC_FRAME xframe;
422 :    
423 :     xframe.bitstream = m4v_buffer;
424 :     xframe.length = 1234; // *m4v_size;
425 :     xframe.image = out_buffer;
426 :     xframe.stride = XDIM;
427 :     xframe.colorspace = XVID_CSP_YV12; // XVID_CSP_USER is fastest (no memcopy involved)
428 :    
429 :     xerr = xvid_decore(dec_handle, XVID_DEC_DECODE, &xframe, NULL);
430 :    
431 :     *m4v_size = xframe.length;
432 :     return xerr;
433 :     }
434 :    
435 :     int dec_stop() /* close decoder to release resources */
436 :     {
437 :     int xerr;
438 :     xerr = xvid_decore(dec_handle, XVID_DEC_DESTROY, NULL, NULL);
439 :    
440 :     return xerr;
441 :     }
442 :    
443 :    
444 :     /*********************************************************************/
445 :     /* Main program */
446 :     /*********************************************************************/
447 :    
448 :     int main(int argc, char *argv[])
449 :     {
450 :     unsigned char *divx_buffer = NULL;
451 :     unsigned char *divx_read = NULL;
452 :     unsigned char *divx_write = NULL;
453 :     unsigned char **in_buffer;
454 :     unsigned char *out_buffer = NULL;
455 :     unsigned char *divx_pos;
456 :    
457 :     int k;
458 :    
459 :     double enctime,dectime;
460 :     double totalenctime=0.;
461 :     double totaldectime=0.;
462 :    
463 :     long totalsize=0;
464 :     int status;
465 :    
466 :     int loop;
467 :     int m4v_size=0;
468 :     int enc_size=0;
469 :     int frame_type[ABS_MAXFRAMENR];
470 :     int Iframes=0, Pframes=0, Bframes=0, use_assembler=0;
471 :     double framepsnr[ABS_MAXFRAMENR];
472 :    
473 :     double Ipsnr=0.,Imaxpsnr=0.,Iminpsnr=999.,Ivarpsnr=0.;
474 :     double Ppsnr=0.,Pmaxpsnr=0.,Pminpsnr=999.,Pvarpsnr=0.;
475 :     double Bpsnr=0.,Bmaxpsnr=0.,Bminpsnr=999.,Bvarpsnr=0.;
476 :    
477 :     char filename[256];
478 :    
479 :     FILE *filehandle;
480 :    
481 :     /* read YUV in pgm format from stdin */
482 :     if (!pgmflag)
483 :     {
484 :     pgmflag = 1;
485 :    
486 :     if (argc==2 && !strcmp(argv[1],"-asm"))
487 :     use_assembler = 1;
488 :     if (argc>=3)
489 :     { XDIM = atoi(argv[1]);
490 :     YDIM = atoi(argv[2]);
491 :     if ( (XDIM <= 0) || (XDIM >= 2048) || (YDIM <=0) || (YDIM >= 2048) )
492 :     { fprintf(stderr,"Wrong frames size %d %d, trying PGM \n",XDIM, YDIM);
493 :     }
494 :     else
495 :     {
496 :     YDIM = YDIM*3/2; /* for YUV */
497 :     pgmflag = 0;
498 :     }
499 :     }
500 :     }
501 :    
502 :     if (pgmflag)
503 :     { if (read_pgmheader(stdin))
504 :     {
505 :     printf("Wrong input format, I want YUV encapsulated in PGM\n");
506 :     return 1;
507 :     }
508 :     }
509 :    
510 :     if (argc>=4)
511 :     { ARG_QUALITY = atoi(argv[3]);
512 :     if ( (ARG_QUALITY < 0) || (ARG_QUALITY > 6) )
513 :     { fprintf(stderr,"Wrong Quality\n"); return -1; }
514 :     else
515 :     printf("Quality %d\n",ARG_QUALITY);
516 :     }
517 :     if (argc>=5)
518 :     { ARG_BITRATE = atoi(argv[4]);
519 :     if ( (ARG_BITRATE <= 0) )
520 :     { fprintf(stderr,"Wrong Bitrate\n"); return -1; }
521 :     if ( (ARG_BITRATE <= 31) )
522 :     { ARG_QUANTI = ARG_BITRATE;
523 :     ARG_BITRATE=0;
524 :     printf("Quantizer %d\n",ARG_QUANTI);
525 :     }
526 :     else
527 :     printf("Bitrate %d kbps\n",ARG_BITRATE);
528 :     }
529 :     if (argc>=6)
530 :     { ARG_FRAMERATE = (float)atof(argv[5]);
531 :     if ( (ARG_FRAMERATE <= 0) )
532 :     { fprintf(stderr,"Wrong Fraterate %s \n",argv[5]); return -1; }
533 :     printf("Framerate %6.3f fps\n",ARG_FRAMERATE);
534 :     }
535 :    
536 :     if (argc>=7)
537 :     { ARG_MAXFRAMENR = atoi(argv[6]);
538 :     if ( (ARG_MAXFRAMENR <= 0) )
539 :     { fprintf(stderr,"Wrong number of frames\n"); return -1; }
540 :     printf("max. Framenr. %d\n",ARG_MAXFRAMENR);
541 :     }
542 :    
543 :     if (argc>=8)
544 :     { ARG_MAX_BFRAMES = atoi(argv[7]);
545 :     if ( (ARG_MAX_BFRAMES < -1) )
546 :     { fprintf(stderr,"Wrong number of bframes\n"); return -1; }
547 :     printf("max. B-Frames %d\n",ARG_MAX_BFRAMES);
548 :     }
549 :     NUMBUFFERS = ARG_MAX_BFRAMES+2;
550 :    
551 :     if (argc>=9)
552 :     { ARG_BQUANT_RATIO = atoi(argv[8]);
553 :     if ( (ARG_BQUANT_RATIO <= 0) )
554 :     { fprintf(stderr,"Wrong bquant ratio\n"); return -1; }
555 :     printf("bquant ratio %d\n",ARG_BQUANT_RATIO);
556 :     }
557 :    
558 :     /* now we know the sizes, so allocate memory */
559 :    
560 :     in_buffer = malloc( NUMBUFFERS*sizeof(unsigned char*) );
561 :     if (!in_buffer)
562 :     goto free_all_memory;
563 :    
564 :     for (k=0;k<NUMBUFFERS;k++)
565 :     in_buffer[k] = NULL;
566 :    
567 :     for (k=0;k<NUMBUFFERS;k++)
568 :     {
569 :     in_buffer[k] = (unsigned char *) malloc(XDIM*YDIM);
570 :     if (!in_buffer[k])
571 :     goto free_all_memory; // goto is one of the most underestimated instructions in C !!!
572 :     }
573 :    
574 :     #define DIVX_SIZE (XDIM*YDIM*200)
575 :     divx_buffer = (unsigned char *) malloc(2*DIVX_SIZE); // this should really be enough memory!
576 :     if (!divx_buffer)
577 :     goto free_all_memory;
578 :    
579 :     divx_write = divx_buffer;
580 :     divx_read = divx_buffer;
581 :    
582 :     YDIM = YDIM*2/3; // PGM is YUV 4:2:0 format, so real image height is *2/3 of PGM picture
583 :    
584 :     out_buffer = (unsigned char *) malloc(XDIM*YDIM*4);
585 :     if (!out_buffer)
586 :     goto free_all_memory;
587 :    
588 :    
589 :    
590 :    
591 :    
592 :     Iframes=0, Pframes=0, Bframes=0, use_assembler=1;
593 :     filenr=0;
594 :    
595 :     /*********************************************************************/
596 :     /* XviD PART Start */
597 :     /*********************************************************************/
598 :    
599 :    
600 :     status = enc_init(use_assembler);
601 :     if (status)
602 :     {
603 :     printf("Encore INIT problem, return value %d\n", status);
604 :     goto release_all;
605 :     }
606 :    
607 :     status = dec_init(use_assembler);
608 :     if (status)
609 :     {
610 :     printf("Decore INIT problem, return value %d\n", status);
611 :     goto release_all;
612 :     }
613 :    
614 :    
615 :     /*********************************************************************/
616 :     /* Main loop */
617 :     /*********************************************************************/
618 :    
619 :     do
620 :     {
621 :     if (pgmflag)
622 :     status = read_pgmdata(stdin, in_buffer[filenr%NUMBUFFERS]); // read PGM data (YUV-format)
623 :     else
624 :     status = read_yuvdata(stdin, in_buffer[filenr%NUMBUFFERS]); // read raw data (YUV-format)
625 :    
626 :     if (status)
627 :     {
628 :     // Couldn't read image, most likely end-of-file
629 :     continue;
630 :     }
631 :    
632 :    
633 :     if (save_ref_flag)
634 :     {
635 :     sprintf(filename, "%s%05d.pgm", filepath, filenr);
636 :     write_pgm(filename,in_buffer[filenr%NUMBUFFERS]);
637 :     }
638 :    
639 :    
640 :     /*********************************************************************/
641 :     /* analyse this frame before encoding */
642 :     /*********************************************************************/
643 :    
644 :     // nothing is done here at the moment, but you could e.g. create
645 :     // histograms or measure entropy or apply preprocessing filters...
646 :    
647 :     /*********************************************************************/
648 :     /* encode and decode this frame */
649 :     /*********************************************************************/
650 :    
651 :     enctime = -msecond();
652 :     status = enc_main(in_buffer[filenr%NUMBUFFERS], divx_write, &enc_size, &frame_type[filenr]);
653 :     enctime += msecond();
654 :    
655 :     if (status)
656 :     printf("Enc_main status: %d\n",status);
657 :     totalenctime += enctime;
658 :     totalsize += enc_size;
659 :    
660 :     printf("Frame %5d: intra %d, enctime =%6.1f ms length=%7d bytes \n",
661 :     filenr, frame_type[filenr], enctime*1000, enc_size);
662 :    
663 :     if (save_m4v_flag)
664 :     {
665 :     sprintf(filename, "%sframe%05d.m4v", filepath, filenr);
666 :     filehandle = fopen(filename, "wb");
667 :     fwrite(divx_write, enc_size, 1, filehandle);
668 :     fclose(filehandle);
669 :     }
670 :     divx_write += enc_size;
671 :     *divx_write=0; /* some kind of padding */
672 :     *(divx_write+1)=0;
673 :     *(divx_write+2)=0;
674 :     *(divx_write+3)=0;
675 :    
676 :     if (filenr<ARG_MAX_BFRAMES)
677 :     goto nextfile;
678 :    
679 :     do {
680 :     dectime = -msecond();
681 :     status = dec_main(divx_read, out_buffer, &m4v_size);
682 :     dectime += msecond();
683 :     divx_read += m4v_size;
684 :     printf("DecFrame %5d: length=%7d bytes (left: %7d)\n", filenr-(ARG_MAX_BFRAMES), m4v_size,
685 :     (divx_write-divx_read));
686 :    
687 :     } while (m4v_size<=7);
688 :    
689 :     if (status)
690 :     printf("dec_main Status =%d\n",status);
691 :     totaldectime += dectime;
692 :    
693 :     /*********************************************************************/
694 :     /* analyse the decoded frame and compare to original */
695 :     /*********************************************************************/
696 :    
697 :     if (filenr>ARG_MAX_BFRAMES) {
698 :     framepsnr[filenr-(ARG_MAX_BFRAMES+1)] = PSNR(XDIM,YDIM, in_buffer[(filenr-(ARG_MAX_BFRAMES+1))%NUMBUFFERS], XDIM, out_buffer, XDIM );
699 :    
700 :     printf("dectime =%6.1f ms PSNR %5.2f\n",dectime*1000, framepsnr[filenr-(ARG_MAX_BFRAMES+1)]);
701 :     }
702 :    
703 :     if (filenr>ARG_MAX_BFRAMES)
704 :     if (save_dec_flag)
705 :     {
706 :     sprintf(filename, "%sdec%05d.pgm", filepath, filenr-(ARG_MAX_BFRAMES+1));
707 :     write_pgm(filename,out_buffer);
708 :     }
709 :    
710 :     nextfile:
711 :    
712 :     filenr++;
713 :    
714 :     if ((int)(divx_read - divx_buffer) > DIVX_SIZE)
715 :     {
716 :     memmove(divx_buffer, divx_read, (int)(divx_buffer+2*DIVX_SIZE-divx_read));
717 :     divx_write -= (int)(divx_read-divx_buffer);
718 :     divx_read = divx_buffer;
719 :     fprintf(stderr,"DIVX-buffer copied\n");
720 :    
721 :     }
722 :    
723 :     if (pgmflag)
724 :     status = read_pgmheader(stdin); // because if this was the last PGM, stop now
725 :    
726 :     } while ( (!status) && (filenr<ARG_MAXFRAMENR) );
727 :    
728 :     printf("Status=%d filenr=%d\n",status,filenr);
729 :     filenr--;
730 :    
731 :    
732 :     /*********************************************************************/
733 :     /* calculate totals and averages for output, print results */
734 :     /*********************************************************************/
735 :    
736 :     if (filenr)
737 :     { totalsize /= filenr;
738 :     totalenctime /= filenr;
739 :     totaldectime /= filenr;
740 :     }
741 :    
742 :     for (i=0;i<filenr-(ARG_MAX_BFRAMES+1);i++)
743 :     {
744 :     switch (frame_type[i])
745 :     {
746 :     case 0:
747 :     Pframes++;
748 :     Ppsnr += framepsnr[i];
749 :     break;
750 :     case 1:
751 :     Iframes++;
752 :     Ipsnr += framepsnr[i];
753 :     break;
754 :     case 2:
755 :     default:
756 :     Bframes++;
757 :     Bpsnr += framepsnr[i];
758 :     break;
759 :     }
760 :     }
761 :    
762 :     if (Pframes)
763 :     Ppsnr /= Pframes;
764 :     if (Iframes)
765 :     Ipsnr /= Iframes;
766 :     if (Bframes)
767 :     Bpsnr /= Bframes;
768 :    
769 :    
770 :     for (i=0;i<filenr-(ARG_MAX_BFRAMES+1);i++) // calculate statistics for every frametype: P,I (and B)
771 :     {
772 :     switch (frame_type[i])
773 :     {
774 :     case 0:
775 :     if (framepsnr[i] > Pmaxpsnr)
776 :     Pmaxpsnr = framepsnr[i];
777 :     if (framepsnr[i] < Pminpsnr)
778 :     Pminpsnr = framepsnr[i];
779 :     Pvarpsnr += (framepsnr[i] - Ppsnr)*(framepsnr[i] - Ppsnr) /Pframes;
780 :     break;
781 :     case 1:
782 :     if (framepsnr[i] > Imaxpsnr)
783 :     Imaxpsnr = framepsnr[i];
784 :     if (framepsnr[i] < Pminpsnr)
785 :     Iminpsnr = framepsnr[i];
786 :     Ivarpsnr += (framepsnr[i] - Ipsnr)*(framepsnr[i] - Ipsnr) /Iframes;
787 :     break;
788 :     case 2:
789 :     if (framepsnr[i] > Bmaxpsnr)
790 :     Bmaxpsnr = framepsnr[i];
791 :     if (framepsnr[i] < Pminpsnr)
792 :     Bminpsnr = framepsnr[i];
793 :     Bvarpsnr += (framepsnr[i] - Bpsnr)*(framepsnr[i] - Bpsnr) /Bframes;
794 :     break;
795 :     }
796 :     }
797 :    
798 :     printf("Avg. Q%1d %2s ",ARG_QUALITY, (ARG_QUANTI ? " q" : "br"));
799 :     printf("%04d ",MAX(ARG_QUANTI,ARG_BITRATE));
800 :     printf("mbf %01d ",ARG_MAX_BFRAMES);
801 :     printf("bqr %03d ",ARG_MAX_BFRAMES>0 ? ARG_BQUANT_RATIO : 0);
802 :     printf("( %.3f bpp) ", (double)ARG_BITRATE*1000/XDIM/YDIM/ARG_FRAMERATE);
803 :     printf("size %6d ",totalsize);
804 :     printf("( %4d kbps ",(int)(totalsize*8*ARG_FRAMERATE/1000));
805 :     printf("/ %.3f bpp) ",(double)totalsize*8/XDIM/YDIM);
806 :     printf("enc: %6.1f fps, dec: %6.1f fps ",1/totalenctime, 1/totaldectime);
807 :     printf("PSNR P(%d): %5.2f ( %5.2f , %5.2f ; %5.4f ) ",Pframes,Ppsnr,Pminpsnr,Pmaxpsnr,sqrt(Pvarpsnr/filenr));
808 :     printf("I(%d): %5.2f ( %5.2f , %5.2f ; %5.4f ) ",Iframes,Ipsnr,Iminpsnr,Imaxpsnr,sqrt(Ivarpsnr/filenr));
809 :     if (Bframes)
810 :     printf("B(%d): %5.2f ( %5.2f , %5.2f ; %5.4f ) ",Bframes,Bpsnr,Bminpsnr,Bmaxpsnr,sqrt(Bvarpsnr/filenr));
811 :     printf("\n");
812 :    
813 :     /*********************************************************************/
814 :     /* XviD PART Stop */
815 :     /*********************************************************************/
816 :    
817 :     release_all:
818 :    
819 :     if (enc_handle)
820 :     {
821 :     status = enc_stop();
822 :     if (status)
823 :     printf("Encore RELEASE problem return value %d\n", status);
824 :     }
825 :    
826 :     if (dec_handle)
827 :     {
828 :     status = dec_stop();
829 :     if (status)
830 :     printf("Decore RELEASE problem return value %d\n", status);
831 :     }
832 :    
833 :    
834 :     free_all_memory:
835 :     free(out_buffer);
836 :     free(divx_buffer);
837 :     if (in_buffer)
838 :     for (k=0;k<NUMBUFFERS;k++)
839 :     free(in_buffer[k]);
840 :    
841 :     return 0;
842 :     }

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