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