[svn] / tags / release-0_9_2 / xvidcore / examples / xvid_bstat.c Repository:
ViewVC logotype

Annotation of /tags/release-0_9_2/xvidcore/examples/xvid_bstat.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1104 - (view) (download)

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

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