[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 728 - (view) (download)

1 : chl 479 /*****************************************************************************
2 : chl 119 *
3 : chl 479 * XVID MPEG-4 VIDEO CODEC
4 :     * - Console based test application -
5 : chl 119 *
6 : chl 479 * Copyright(C) 2002 Christoph Lampert
7 : chl 119 *
8 : chl 479 * This program is free software; you can redistribute it and/or modify
9 :     * it under the terms of the GNU General Public License as published by
10 :     * the Free Software Foundation; either version 2 of the License, or
11 :     * (at your option) any later version.
12 : chl 119 *
13 : chl 479 * This program is distributed in the hope that it will be useful,
14 :     * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 :     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 :     * GNU General Public License for more details.
17 :     *
18 :     * You should have received a copy of the GNU General Public License
19 :     * along with this program; if not, write to the Free Software
20 :     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 :     *
22 : edgomez 728 * $Id: xvid_stat.c,v 1.17 2002-12-18 20:48:25 edgomez Exp $
23 : chl 479 *
24 :     ****************************************************************************/
25 : chl 119
26 : edgomez 483 /*****************************************************************************
27 :     * Application notes :
28 : chl 119 *
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 : edgomez 483 * and maths-lib.
34 : chl 119 *
35 : edgomez 483 * Usage : xvid_stat [OPTIONS]
36 :     * Options :
37 :     * -w integer : frame width ([1.2048])
38 :     * -h integer : frame height ([1.2048])
39 :     * -b integer : target bitrate (>0 | default=900kbit)
40 :     * -f float : target framerate (>0)
41 :     * -i string : input filename (default=stdin)
42 :     * -t integer : input data type (yuv=0, pgm=1)
43 :     * -n integer : number of frames to encode
44 :     * -q integer : quality ([0..5])
45 :     * -d boolean : save decoder output (0 False*, !=0 True)
46 :     * -m boolean : save mpeg4 raw stream (0 False*, !=0 True)
47 : edgomez 728 * -mv integer : Hinted Motion Estimation (0 none, 1 get hints, 2 set hints)
48 : edgomez 548 * -help : prints this help message
49 : edgomez 483 * -quant integer : fixed quantizer (disables -b setting)
50 :     * (* means default)
51 : chl 119 *
52 : edgomez 483 * An input file named "stdin" is treated as standard input stream.
53 : chl 119 *
54 :     *
55 : edgomez 483 * PGM input must be in a very specific format, basically it pgm file must
56 :     * contain Y plane first just like usual P5 pgm files, and then U and V
57 :     * planes are stored just after Y plane so y dimension is y*3/2 in reality
58 :     *
59 :     * See read_pgmheader for more details.
60 :     *
61 :     * Such a PGM file can be generated from MPEG2 by # mpeg2dec -o pgmpipe
62 : chl 119 *
63 : edgomez 483 ****************************************************************************/
64 : chl 119
65 :     #include <stdio.h>
66 :     #include <stdlib.h>
67 : edgomez 483 #include <string.h>
68 :     #include <math.h>
69 : edgomez 548 #ifndef _MSC_VER
70 :     #include <sys/time.h>
71 :     #else
72 : edgomez 483 #include <time.h>
73 : edgomez 548 #endif
74 : chl 119
75 : edgomez 548 #include "xvid.h"
76 : chl 119
77 : edgomez 483 /****************************************************************************
78 :     * Prototypes
79 :     ***************************************************************************/
80 : chl 119
81 : edgomez 483 /* Prints program usage message */
82 :     static void usage();
83 : chl 119
84 : edgomez 483 /* Statistical functions */
85 :     static double msecond();
86 :     static double absdistq(int x, int y,
87 :     unsigned char* buf1, int stride1,
88 :     unsigned char* buf2, int stride2);
89 :     static double PSNR(int x, int y,
90 :     unsigned char* buf1, int stride1,
91 :     unsigned char* buf2, int stride2);
92 : chl 119
93 : edgomez 483 /* PGM related functions */
94 :     static int read_pgmheader(FILE* handle);
95 :     static int read_pgmdata(FILE* handle, unsigned char *image);
96 :     static int read_yuvdata(FILE* handle, unsigned char *image);
97 :     static int write_pgm(char *filename, unsigned char *image);
98 : chl 151
99 : edgomez 483 /* Encoder related functions */
100 :     static int enc_init(int use_assembler);
101 :     static int enc_stop();
102 :     static int enc_main(unsigned char* image, unsigned char* bitstream,
103 : edgomez 728 unsigned char* hints_buffer,
104 :     long *streamlength, long* frametype, long* hints_size);
105 : chl 119
106 : edgomez 483 /* Decoder related functions */
107 :     static int dec_stop();
108 :     static int dec_main(unsigned char *m4v_buffer, unsigned char *out_buffer,
109 :     int m4v_size);
110 :     static int dec_init(int use_assembler);
111 : chl 119
112 : edgomez 483 /*****************************************************************************
113 :     * Quality presets
114 :     ****************************************************************************/
115 : chl 119
116 : edgomez 483 static int const motion_presets[7] = {
117 : edgomez 684 0, /* Q 0 */
118 :     PMV_EARLYSTOP16, /* Q 1 */
119 :     PMV_EARLYSTOP16, /* Q 2 */
120 :     PMV_EARLYSTOP16 | PMV_HALFPELREFINE16, /* Q 3 */
121 :     PMV_EARLYSTOP16 | PMV_HALFPELREFINE16, /* Q 4 */
122 :     PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 | PMV_EARLYSTOP8 | /* Q 5 */
123 : edgomez 483 PMV_HALFPELREFINE8,
124 : edgomez 684 PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 | PMV_EXTSEARCH16 | /* Q 6 */
125 : edgomez 483 PMV_USESQUARES16 | PMV_EARLYSTOP8 | PMV_HALFPELREFINE8
126 :     };
127 : chl 151
128 : edgomez 483 static int const general_presets[7] = {
129 : edgomez 684 XVID_H263QUANT, /* Q 0 */
130 :     XVID_MPEGQUANT, /* Q 1 */
131 :     XVID_H263QUANT, /* Q 2 */
132 :     XVID_H263QUANT | XVID_HALFPEL, /* Q 3 */
133 :     XVID_H263QUANT | XVID_HALFPEL | XVID_INTER4V, /* Q 4 */
134 :     XVID_H263QUANT | XVID_HALFPEL | XVID_INTER4V, /* Q 5 */
135 :     XVID_H263QUANT | XVID_HALFPEL | XVID_INTER4V /* Q 6 */
136 : edgomez 483 };
137 :    
138 :    
139 :     /*****************************************************************************
140 :     * Command line global variables
141 :     ****************************************************************************/
142 :    
143 :     /* Maximum number of frames to encode */
144 :     #define ABS_MAXFRAMENR 9999
145 :    
146 : edgomez 728 /* HINTMODEs */
147 :     #define HINT_MODE_NONE 0
148 :     #define HINT_MODE_GET 1
149 :     #define HINT_MODE_SET 2
150 :     #define HINT_FILE "hints.mv"
151 :    
152 : edgomez 483 static int ARG_BITRATE = 900;
153 :     static int ARG_QUANTI = 0;
154 :     static int ARG_QUALITY = 6;
155 :     static int ARG_MINQUANT = 1;
156 :     static int ARG_MAXQUANT = 31;
157 :     static float ARG_FRAMERATE = 25.00f;
158 :     static int ARG_MAXFRAMENR = ABS_MAXFRAMENR;
159 :     static char *ARG_INPUTFILE = NULL;
160 :     static int ARG_INPUTTYPE = 0;
161 :     static int ARG_SAVEDECOUTPUT = 0;
162 :     static int ARG_SAVEMPEGSTREAM = 0;
163 : edgomez 728 static int ARG_HINTMODE = HINT_MODE_NONE;
164 : edgomez 483 static int XDIM = 0;
165 :     static int YDIM = 0;
166 : edgomez 484 #define IMAGE_SIZE(x,y) ((x)*(y)*3/2)
167 : edgomez 483
168 : chl 119 #define MAX(A,B) ( ((A)>(B)) ? (A) : (B) )
169 :     #define SMALL_EPS 1e-10
170 :    
171 : edgomez 728 #define LONG_PACK(a,b,c,d) ((long) (((long)(a))<<24) | (((long)(b))<<16) | \
172 :     (((long)(c))<<8) |((long)(d)))
173 :    
174 :     #define SWAP(a) ( (((a)&0x000000ff)<<24) | (((a)&0x0000ff00)<<8) | \
175 :     (((a)&0x00ff0000)>>8) | (((a)&0xff000000)>>24) )
176 :    
177 : edgomez 483 /****************************************************************************
178 :     * Nasty global vars ;-)
179 :     ***************************************************************************/
180 :    
181 :     static int i,filenr = 0;
182 :     static int save_ref_flag = 0;
183 :    
184 :     /* the path where to save output */
185 :     static char filepath[256] = "./";
186 :    
187 :     /* Internal structures (handles) for encoding and decoding */
188 :     static void *enc_handle = NULL;
189 :     static void *dec_handle = NULL;
190 :    
191 :     /*****************************************************************************
192 :     * Main program
193 :     ****************************************************************************/
194 :    
195 :     int main(int argc, char *argv[])
196 :     {
197 :    
198 :     unsigned char *divx_buffer = NULL;
199 :     unsigned char *in_buffer = NULL;
200 :     unsigned char *out_buffer = NULL;
201 : edgomez 728 unsigned char *hints_buffer = NULL;
202 : edgomez 483
203 :     double enctime,dectime;
204 :     double totalenctime=0.;
205 :     double totaldectime=0.;
206 :    
207 :     long totalsize=0;
208 : edgomez 728 long hints_size;
209 : edgomez 483 int status;
210 : edgomez 728 int bigendian;
211 :    
212 :     long m4v_size;
213 :     long frame_type[ABS_MAXFRAMENR];
214 : edgomez 483 int Iframes=0, Pframes=0, use_assembler=0;
215 :     double framepsnr[ABS_MAXFRAMENR];
216 :    
217 :     double Ipsnr=0.,Imaxpsnr=0.,Iminpsnr=999.,Ivarpsnr=0.;
218 :     double Ppsnr=0.,Pmaxpsnr=0.,Pminpsnr=999.,Pvarpsnr=0.;
219 :    
220 :     char filename[256];
221 :    
222 :     FILE *filehandle;
223 :     FILE *in_file = stdin;
224 : edgomez 728 FILE *hints_file = NULL;
225 : edgomez 483
226 :     printf("xvid_stat - XviD core library test program ");
227 :     printf("written by Christoph Lampert 2002\n\n");
228 :    
229 :     /*****************************************************************************
230 :     * Command line parsing
231 :     ****************************************************************************/
232 :    
233 :     for (i=1; i< argc; i++) {
234 :    
235 :     if (strcmp("-asm", argv[i]) == 0 ) {
236 :     use_assembler = 1;
237 :     }
238 :     else if (strcmp("-w", argv[i]) == 0 && i < argc - 1 ) {
239 :     i++;
240 :     XDIM = atoi(argv[i]);
241 :     }
242 :     else if (strcmp("-h", argv[i]) == 0 && i < argc - 1 ) {
243 :     i++;
244 :     YDIM = atoi(argv[i]);
245 :     }
246 :     else if (strcmp("-b", argv[i]) == 0 && i < argc - 1 ) {
247 :     i++;
248 :     ARG_BITRATE = atoi(argv[i]);
249 :     }
250 :     else if (strcmp("-q", argv[i]) == 0 && i < argc - 1 ) {
251 :     i++;
252 :     ARG_QUALITY = atoi(argv[i]);
253 :     }
254 :     else if (strcmp("-f", argv[i]) == 0 && i < argc - 1 ) {
255 :     i++;
256 :     ARG_FRAMERATE = (float)atof(argv[i]);
257 :     }
258 :     else if (strcmp("-i", argv[i]) == 0 && i < argc - 1 ) {
259 :     i++;
260 :     ARG_INPUTFILE = argv[i];
261 :     }
262 :     else if (strcmp("-t", argv[i]) == 0 && i < argc - 1 ) {
263 :     i++;
264 :     ARG_INPUTTYPE = atoi(argv[i]);
265 :     }
266 :     else if(strcmp("-n", argv[i]) == 0 && i < argc - 1 ) {
267 :     i++;
268 :     ARG_MAXFRAMENR = atoi(argv[i]);
269 :     }
270 :     else if (strcmp("-quant", argv[i]) == 0 && i < argc - 1 ) {
271 :     i++;
272 :     ARG_QUANTI = atoi(argv[i]);
273 :     }
274 :     else if (strcmp("-d", argv[i]) == 0 && i < argc - 1 ) {
275 :     i++;
276 :     ARG_SAVEDECOUTPUT = atoi(argv[i]);
277 :     }
278 :     else if (strcmp("-m", argv[i]) == 0 && i < argc - 1 ) {
279 :     i++;
280 :     ARG_SAVEMPEGSTREAM = atoi(argv[i]);
281 :     }
282 : edgomez 728 else if (strcmp("-mv", argv[i]) == 0 && i < argc - 1 ) {
283 :     i++;
284 :     ARG_HINTMODE = atoi(argv[i]);
285 :     }
286 : edgomez 548 else if (strcmp("-help", argv[i])) {
287 : edgomez 483 usage();
288 :     return(0);
289 :     }
290 :     else {
291 :     usage();
292 :     exit(-1);
293 :     }
294 :    
295 :     }
296 :    
297 :     /*****************************************************************************
298 :     * Arguments checking
299 :     ****************************************************************************/
300 :    
301 : edgomez 484 if (XDIM <= 0 || XDIM >= 2048 || YDIM <=0 || YDIM >= 2048 ) {
302 : edgomez 559 fprintf(stderr, "Trying to retreive width and height from PGM header\n");
303 : edgomez 483 ARG_INPUTTYPE = 1; /* pgm */
304 :     }
305 :    
306 :     if ( ARG_QUALITY < 0 || ARG_QUALITY > 6) {
307 :     fprintf(stderr,"Wrong Quality\n");
308 :     return -1;
309 :     }
310 :    
311 :     if ( ARG_BITRATE <= 0 && ARG_QUANTI == 0) {
312 :     fprintf(stderr,"Wrong Bitrate\n");
313 :     return -1;
314 :     }
315 :    
316 :     if ( ARG_FRAMERATE <= 0) {
317 : edgomez 559 fprintf(stderr,"Wrong Framerate %s \n",argv[5]);
318 : edgomez 483 return -1;
319 :     }
320 :    
321 :     if ( ARG_MAXFRAMENR <= 0) {
322 :     fprintf(stderr,"Wrong number of frames\n");
323 :     return -1;
324 :     }
325 :    
326 : edgomez 728 if ( ARG_HINTMODE != HINT_MODE_NONE &&
327 :     ARG_HINTMODE != HINT_MODE_GET &&
328 :     ARG_HINTMODE != HINT_MODE_SET)
329 :     ARG_HINTMODE = HINT_MODE_NONE;
330 :    
331 :     if( ARG_HINTMODE != HINT_MODE_NONE) {
332 :     char *rights = "rb";
333 :    
334 :     /*
335 :     * If we are getting hints from core, we will have to write them to
336 :     * hint file
337 :     */
338 :     if(ARG_HINTMODE == HINT_MODE_GET)
339 :     rights = "w+b";
340 :    
341 :     /* Open the hint file */
342 :     hints_file = fopen(HINT_FILE, rights);
343 :     if(hints_file == NULL) {
344 :     fprintf(stderr, "Error opening input file %s\n", HINT_FILE);
345 :     return -1;
346 :     }
347 :    
348 :     /* Allocate hint memory space, we will be using rawhints */
349 :     /* NB : Hope 1Mb is enough */
350 :     if((hints_buffer = malloc(1024*1024)) == NULL) {
351 :     fprintf(stderr, "Memory allocation error\n");
352 :     return -1;
353 :     }
354 :    
355 :     }
356 :    
357 : edgomez 483 if ( ARG_INPUTFILE == NULL || strcmp(ARG_INPUTFILE, "stdin") == 0) {
358 :     in_file = stdin;
359 :     }
360 :     else {
361 :    
362 :     in_file = fopen(ARG_INPUTFILE, "rb");
363 :     if (in_file == NULL) {
364 :     fprintf(stderr, "Error opening input file %s\n", ARG_INPUTFILE);
365 :     return -1;
366 :     }
367 :     }
368 :    
369 :     if (ARG_INPUTTYPE) {
370 :     if (read_pgmheader(in_file)) {
371 :     fprintf(stderr, "Wrong input format, I want YUV encapsulated in PGM\n");
372 : edgomez 484 return -1;
373 : edgomez 483 }
374 :     }
375 :    
376 :     /* now we know the sizes, so allocate memory */
377 :    
378 : edgomez 484 in_buffer = (unsigned char *) malloc(IMAGE_SIZE(XDIM,YDIM));
379 : edgomez 483 if (!in_buffer)
380 :     goto free_all_memory;
381 :    
382 :     /* this should really be enough memory ! */
383 : edgomez 484 divx_buffer = (unsigned char *) malloc(IMAGE_SIZE(XDIM,YDIM)*2);
384 : edgomez 483 if (!divx_buffer)
385 :     goto free_all_memory;
386 :    
387 : edgomez 484 out_buffer = (unsigned char *) malloc(IMAGE_SIZE(XDIM,YDIM)*4);
388 : edgomez 483 if (!out_buffer)
389 :     goto free_all_memory;
390 :    
391 :    
392 :     /*****************************************************************************
393 :     * XviD PART Start
394 :     ****************************************************************************/
395 :    
396 :    
397 :     status = enc_init(use_assembler);
398 :     if (status)
399 :     {
400 :     fprintf(stderr, "Encore INIT problem, return value %d\n", status);
401 :     goto release_all;
402 :     }
403 :    
404 :     status = dec_init(use_assembler);
405 :     if (status)
406 :     {
407 :     fprintf(stderr, "Decore INIT problem, return value %d\n", status);
408 :     goto release_all;
409 :     }
410 :    
411 : edgomez 728 totalsize = LONG_PACK('M','P','4','U');
412 :     if(*((char *)(&totalsize)) == 'M')
413 :     bigendian = 1;
414 :     else
415 :     bigendian = 0;
416 : edgomez 483
417 :     /*****************************************************************************
418 :     * Main loop
419 :     ****************************************************************************/
420 :    
421 :     do {
422 :    
423 :     if (ARG_INPUTTYPE)
424 : edgomez 684 status = read_pgmdata(in_file, in_buffer); /* read PGM data (YUV-format) */
425 : edgomez 483 else
426 : edgomez 684 status = read_yuvdata(in_file, in_buffer); /* read raw data (YUV-format) */
427 : edgomez 483
428 :     if (status)
429 :     {
430 :     /* Couldn't read image, most likely end-of-file */
431 :     continue;
432 :     }
433 :    
434 :    
435 :     if (save_ref_flag)
436 :     {
437 :     sprintf(filename, "%s%05d.pgm", filepath, filenr);
438 :     write_pgm(filename,in_buffer);
439 :     }
440 :    
441 :    
442 :     /*****************************************************************************
443 :     * Analyse this frame before encoding
444 :     ****************************************************************************/
445 :    
446 :     /*
447 :     * nothing is done here at the moment, but you could e.g. create
448 :     * histograms or measure entropy or apply preprocessing filters...
449 :     */
450 :    
451 :     /*****************************************************************************
452 : edgomez 728 * Read hints from file
453 :     ****************************************************************************/
454 :    
455 :     if(ARG_HINTMODE == HINT_MODE_SET) {
456 :     fread(&hints_size, 1, sizeof(long), hints_file);
457 :     hints_size = (!bigendian)?SWAP(hints_size):hints_size;
458 :     fread(hints_buffer, 1, hints_size, hints_file);
459 :     }
460 :    
461 :     /*****************************************************************************
462 : edgomez 483 * Encode and decode this frame
463 :     ****************************************************************************/
464 :    
465 :     enctime = msecond();
466 : edgomez 728 status = enc_main(in_buffer, divx_buffer, hints_buffer,
467 :     &m4v_size, &frame_type[filenr], &hints_size);
468 : edgomez 483 enctime = msecond() - enctime;
469 :    
470 :     totalenctime += enctime;
471 :     totalsize += m4v_size;
472 :    
473 :     printf("Frame %5d: intra %1d, enctime=%6.1f ms, size=%6d bytes ",
474 :     (int)filenr, (int)frame_type[filenr], (float)enctime, (int)m4v_size);
475 :    
476 : edgomez 728 /*****************************************************************************
477 :     * Save hints to file
478 :     ****************************************************************************/
479 :    
480 :     if(ARG_HINTMODE == HINT_MODE_GET) {
481 :     hints_size = (!bigendian)?SWAP(hints_size):hints_size;
482 :     fwrite(&hints_size, 1, sizeof(long), hints_file);
483 :     hints_size = (!bigendian)?SWAP(hints_size):hints_size;
484 :     fwrite(hints_buffer, 1, hints_size, hints_file);
485 :     }
486 :    
487 :     /*****************************************************************************
488 :     * Save stream to file
489 :     ****************************************************************************/
490 :    
491 : edgomez 483 if (ARG_SAVEMPEGSTREAM)
492 :     {
493 :     sprintf(filename, "%sframe%05d.m4v", filepath, filenr);
494 :     filehandle = fopen(filename, "wb");
495 :     fwrite(divx_buffer, m4v_size, 1, filehandle);
496 :     fclose(filehandle);
497 :     }
498 :    
499 :     dectime = msecond();
500 :     status = dec_main(divx_buffer, out_buffer, m4v_size);
501 :     dectime = msecond() - dectime;
502 :    
503 :     totaldectime += dectime;
504 :    
505 :    
506 :     /*****************************************************************************
507 :     * Analyse the decoded frame and compare to original
508 :     ****************************************************************************/
509 :    
510 : edgomez 485 framepsnr[filenr] = PSNR(XDIM,YDIM*3/2, in_buffer, XDIM, out_buffer, XDIM);
511 : chl 119
512 : edgomez 483 printf("dectime =%6.1f ms PSNR %5.2f\n",dectime, framepsnr[filenr]);
513 : chl 119
514 : edgomez 483 if (ARG_SAVEDECOUTPUT)
515 :     {
516 :     sprintf(filename, "%sdec%05d.pgm", filepath, filenr);
517 :     write_pgm(filename, out_buffer);
518 :     }
519 : chl 119
520 : edgomez 483 /* Read the header if it's pgm stream */
521 :     if (ARG_INPUTTYPE)
522 :     status = read_pgmheader(in_file);
523 : chl 119
524 : edgomez 483 filenr++;
525 : chl 119
526 : edgomez 483 } while ( (!status) && (filenr<ARG_MAXFRAMENR) );
527 : chl 119
528 : edgomez 483
529 :    
530 :     /*****************************************************************************
531 :     * Calculate totals and averages for output, print results
532 :     ****************************************************************************/
533 : chl 119
534 : edgomez 483 totalsize /= filenr;
535 :     totalenctime /= filenr;
536 :     totaldectime /= filenr;
537 :    
538 :     for (i=0;i<filenr;i++)
539 :     {
540 :     switch (frame_type[i])
541 :     {
542 :     case 0:
543 :     Pframes++;
544 :     Ppsnr += framepsnr[i];
545 :     break;
546 :     case 1:
547 :     Iframes++;
548 :     Ipsnr += framepsnr[i];
549 :     break;
550 :     default:
551 :     break;
552 :     }
553 :     }
554 :    
555 :     if (Pframes)
556 :     Ppsnr /= Pframes;
557 :     if (Iframes)
558 :     Ipsnr /= Iframes;
559 :    
560 :     /* calculate statistics for every frametype: P,I */
561 :     for (i=0;i<filenr;i++)
562 :     {
563 :     switch (frame_type[i])
564 :     {
565 :     case 0:
566 :     if (framepsnr[i] > Pmaxpsnr)
567 :     Pmaxpsnr = framepsnr[i];
568 :     if (framepsnr[i] < Pminpsnr)
569 :     Pminpsnr = framepsnr[i];
570 :     Pvarpsnr += (framepsnr[i] - Ppsnr)*(framepsnr[i] - Ppsnr) /Pframes;
571 :     break;
572 :     case 1:
573 :     if (framepsnr[i] > Imaxpsnr)
574 :     Imaxpsnr = framepsnr[i];
575 :     if (framepsnr[i] < Pminpsnr)
576 :     Iminpsnr = framepsnr[i];
577 :     Ivarpsnr += (framepsnr[i] - Ipsnr)*(framepsnr[i] - Ipsnr) /Iframes;
578 :     default:
579 :     break;
580 :     }
581 :     }
582 :    
583 :     /* Print all statistics */
584 :     printf("Avg. Q%1d %2s ",ARG_QUALITY, (ARG_QUANTI ? " q" : "br"));
585 : edgomez 501 printf("%04d ",(ARG_QUANTI)?ARG_QUANTI:ARG_BITRATE);
586 : edgomez 483 printf("( %.2f bpp) ", (double)ARG_BITRATE*1000/XDIM/YDIM/ARG_FRAMERATE);
587 : edgomez 561 printf("size %6d ", (int)totalsize);
588 : edgomez 483 printf("( %4d kbps ",(int)(totalsize*8*ARG_FRAMERATE/1000));
589 :     printf("/ %.2f bpp) ",(double)totalsize*8/XDIM/YDIM);
590 : edgomez 548 printf("enc: %6.1f fps, dec: %6.1f fps \n",1000/totalenctime, 1000/totaldectime);
591 : edgomez 483 printf("PSNR P(%d): %5.2f ( %5.2f , %5.2f ; %5.4f ) ",Pframes,Ppsnr,Pminpsnr,Pmaxpsnr,sqrt(Pvarpsnr/filenr));
592 :     printf("I(%d): %5.2f ( %5.2f , %5.2f ; %5.4f ) ",Iframes,Ipsnr,Iminpsnr,Imaxpsnr,sqrt(Ivarpsnr/filenr));
593 :     printf("\n");
594 :    
595 :     /*****************************************************************************
596 :     * XviD PART Stop
597 :     ****************************************************************************/
598 :    
599 :     release_all:
600 :    
601 :     if (enc_handle)
602 :     {
603 :     status = enc_stop();
604 :     if (status)
605 :     fprintf(stderr, "Encore RELEASE problem return value %d\n", status);
606 :     }
607 :    
608 :     if (dec_handle)
609 :     {
610 :     status = dec_stop();
611 :     if (status)
612 :     fprintf(stderr, "Decore RELEASE problem return value %d\n", status);
613 :     }
614 :    
615 :     fclose(in_file);
616 :    
617 :     free_all_memory:
618 :     free(out_buffer);
619 :     free(divx_buffer);
620 :     free(in_buffer);
621 :    
622 :     return 0;
623 :    
624 :     }
625 :    
626 :     /*****************************************************************************
627 :     * "statistical" functions
628 :     *
629 :     * these are not needed for encoding or decoding, but for measuring
630 :     * time and quality, there in nothing specific to XviD in these
631 :     *
632 :     *****************************************************************************/
633 :    
634 :    
635 :    
636 :     /* Return time elapsed time in miliseconds since the program started */
637 :     static double msecond()
638 : chl 119 {
639 : edgomez 548 #ifndef _MSC_VER
640 :     struct timeval tv;
641 :     gettimeofday(&tv, 0);
642 : edgomez 550 return tv.tv_sec*1.0e3 + tv.tv_usec * 1.0e-3;
643 : edgomez 548 #else
644 : edgomez 483 clock_t clk;
645 :     clk = clock();
646 :     return clk * 1000 / CLOCKS_PER_SEC;
647 : edgomez 548 #endif
648 : chl 119 }
649 :    
650 :    
651 : edgomez 483 /*
652 :     * Returns the sum of squared distances (SSD) between two images of dimensions
653 :     * x times y
654 :     */
655 :     static double absdistq(int x, int y,
656 :     unsigned char* buf1, int stride1,
657 :     unsigned char* buf2, int stride2)
658 : chl 119 {
659 :     double dist=0.;
660 :     int i,j,val;
661 :    
662 :     for (i=0;i<y;i++)
663 :     {
664 :     val=0;
665 :     for (j=0;j<x;j++)
666 :     val+= ((int)buf1[j]-(int)buf2[j])*((int)buf1[j]-(int)buf2[j]);
667 :    
668 :     dist += (double)val;
669 :     buf1 += stride1;
670 :     buf2 += stride2;
671 :     }
672 : edgomez 483 return dist/(x*y);
673 : chl 119 }
674 :    
675 :    
676 : edgomez 483 /*
677 :     * Returns the PSNR between to images.
678 :     *
679 :     * This is a common logarithmic measure for "quality" from the world of signal
680 :     * processing if you don't know what it is, simply accept that higher values
681 :     * are better.
682 :     *
683 :     * PSNR represents the ratio of useful signal over noise signal. In our case,
684 :     * useful signal is refernce image, noise signal is the difference between
685 :     * reference and decoded frame from encoded bitstream.
686 :     *
687 :     * The problem is this type of value is dependant of image source and so, is
688 :     * not reliable as a common "quality" indicator.
689 :     * So PSNR computes the ratio of maximum/noise. Maximum being set to 2^bpp/channel
690 :     * This way, PSNR is not dependant anymore of image data type.
691 :     *
692 :     */
693 :     static double PSNR(int x, int y,
694 :     unsigned char* buf1, int stride1,
695 :     unsigned char* buf2, int stride2)
696 : chl 119 {
697 : edgomez 483 return 10*(log10(255*255)-log10( absdistq(x, y, buf1, stride1, buf2, stride2) ));
698 : chl 119 }
699 :    
700 : edgomez 483 /*****************************************************************************
701 :     * Usage message
702 :     *****************************************************************************/
703 : chl 119
704 : edgomez 483 static void usage()
705 :     {
706 : chl 119
707 : edgomez 483 fprintf(stderr, "Usage : xvid_stat [OPTIONS]\n");
708 :     fprintf(stderr, "Options :\n");
709 :     fprintf(stderr, " -w integer : frame width ([1.2048])\n");
710 :     fprintf(stderr, " -h integer : frame height ([1.2048])\n");
711 :     fprintf(stderr, " -b integer : target bitrate (>0 | default=900kbit)\n");
712 :     fprintf(stderr, " -f float : target framerate (>0)\n");
713 :     fprintf(stderr, " -i string : input filename (default=stdin)\n");
714 :     fprintf(stderr, " -t integer : input data type (yuv=0, pgm=1)\n");
715 :     fprintf(stderr, " -n integer : number of frames to encode\n");
716 :     fprintf(stderr, " -q integer : quality ([0..5])\n");
717 :     fprintf(stderr, " -d boolean : save decoder output (0 False*, !=0 True)\n");
718 :     fprintf(stderr, " -m boolean : save mpeg4 raw stream (0 False*, !=0 True)\n");
719 : edgomez 548 fprintf(stderr, " -help : prints this help message\n");
720 : edgomez 483 fprintf(stderr, " -quant integer : fixed quantizer (disables -b setting)\n");
721 :     fprintf(stderr, " (* means default)\n");
722 :    
723 :     }
724 :    
725 :     /*****************************************************************************
726 :     * Input and output functions
727 :     *
728 :     * the are small and simple routines to read and write PGM and YUV
729 :     * image. It's just for convenience, again nothing specific to XviD
730 :     *
731 :     *****************************************************************************/
732 :    
733 :     static int read_pgmheader(FILE* handle)
734 : chl 119 {
735 :     int bytes,xsize,ysize,depth;
736 :     char dummy[2];
737 :    
738 :     bytes = fread(dummy,1,2,handle);
739 :    
740 :     if ( (bytes < 2) || (dummy[0] != 'P') || (dummy[1] != '5' ))
741 :     return 1;
742 : edgomez 484
743 : chl 119 fscanf(handle,"%d %d %d",&xsize,&ysize,&depth);
744 :     if ( (xsize > 1440) || (ysize > 2880 ) || (depth != 255) )
745 :     {
746 :     fprintf(stderr,"%d %d %d\n",xsize,ysize,depth);
747 :     return 2;
748 :     }
749 :     if ( (XDIM==0) || (YDIM==0) )
750 : edgomez 483 {
751 :     XDIM=xsize;
752 : edgomez 484 YDIM=ysize*2/3;
753 : chl 119 }
754 :    
755 :     return 0;
756 :     }
757 :    
758 : edgomez 483 static int read_pgmdata(FILE* handle, unsigned char *image)
759 : chl 119 {
760 : edgomez 483 int i;
761 : chl 119 char dummy;
762 : edgomez 483
763 : edgomez 484 unsigned char *y = image;
764 :     unsigned char *u = image + XDIM*YDIM;
765 :     unsigned char *v = image + XDIM*YDIM + XDIM/2*YDIM/2;
766 : chl 119
767 : edgomez 484 /* read Y component of picture */
768 :     fread(y, 1, XDIM*YDIM, handle);
769 : chl 119
770 : edgomez 483 for (i=0;i<YDIM/2;i++)
771 : chl 119 {
772 : edgomez 484 /* read U */
773 :     fread(u, 1, XDIM/2, handle);
774 :    
775 :     /* read V */
776 :     fread(v, 1, XDIM/2, handle);
777 :    
778 :     /* Update pointers */
779 :     u += XDIM/2;
780 :     v += XDIM/2;
781 : chl 119 }
782 : edgomez 484
783 :     /* I don't know why, but this seems needed */
784 :     fread(&dummy, 1, 1, handle);
785 :    
786 : chl 119 return 0;
787 :     }
788 :    
789 : edgomez 483 static int read_yuvdata(FILE* handle, unsigned char *image)
790 :     {
791 : chl 119
792 : edgomez 548 if (fread(image, 1, IMAGE_SIZE(XDIM, YDIM), handle) != (unsigned int)IMAGE_SIZE(XDIM, YDIM))
793 : chl 119 return 1;
794 :     else
795 :     return 0;
796 :     }
797 :    
798 : edgomez 483 static int write_pgm(char *filename, unsigned char *image)
799 : chl 119 {
800 : edgomez 484 int loop;
801 :    
802 :     unsigned char *y = image;
803 :     unsigned char *u = image + XDIM*YDIM;
804 :     unsigned char *v = image + XDIM*YDIM + XDIM/2*YDIM/2;
805 :    
806 : chl 119 FILE *filehandle;
807 : edgomez 484 filehandle=fopen(filename,"w+b");
808 : chl 119 if (filehandle)
809 : edgomez 483 {
810 : edgomez 484 /* Write header */
811 :     fprintf(filehandle,"P5\n\n%d %d 255\n", XDIM,YDIM*3/2);
812 :    
813 :     /* Write Y data */
814 :     fwrite(y, 1, XDIM*YDIM, filehandle);
815 :    
816 :     for(loop=0; loop<YDIM/2; loop++)
817 :     {
818 :     /* Write U scanline */
819 :     fwrite(u, 1, XDIM/2, filehandle);
820 :    
821 :     /* Write V scanline */
822 :     fwrite(v, 1, XDIM/2, filehandle);
823 :    
824 :     /* Update pointers */
825 :     u += XDIM/2;
826 :     v += XDIM/2;
827 :    
828 :     }
829 :    
830 :     /* Close file */
831 : chl 119 fclose(filehandle);
832 : edgomez 484
833 : chl 119 return 0;
834 :     }
835 :     else
836 :     return 1;
837 :     }
838 :    
839 : edgomez 483 /*****************************************************************************
840 :     * Routines for encoding: init encoder, frame step, release encoder
841 :     ****************************************************************************/
842 : chl 119
843 :     #define FRAMERATE_INCR 1001
844 :    
845 : edgomez 483 /* Initialize encoder for first use, pass all needed parameters to the codec */
846 :     static int enc_init(int use_assembler)
847 :     {
848 : chl 119 int xerr;
849 :    
850 :     XVID_INIT_PARAM xinit;
851 :     XVID_ENC_PARAM xparam;
852 :    
853 : edgomez 483 if(use_assembler) {
854 : Isibaar 209
855 :     #ifdef ARCH_IA64
856 :     xinit.cpu_flags = XVID_CPU_FORCE | XVID_CPU_IA64;
857 :     #else
858 :     xinit.cpu_flags = 0;
859 :     #endif
860 : edgomez 483 }
861 :     else {
862 : Isibaar 209 xinit.cpu_flags = XVID_CPU_FORCE;
863 : edgomez 483 }
864 : Isibaar 209
865 : chl 119 xvid_init(NULL, 0, &xinit, NULL);
866 :    
867 :     xparam.width = XDIM;
868 :     xparam.height = YDIM;
869 :     if ((ARG_FRAMERATE - (int)ARG_FRAMERATE) < SMALL_EPS)
870 :     {
871 : edgomez 483 xparam.fincr = 1;
872 :     xparam.fbase = (int)ARG_FRAMERATE;
873 : chl 119 }
874 :     else
875 :     {
876 : edgomez 483 xparam.fincr = FRAMERATE_INCR;
877 :     xparam.fbase = (int)(FRAMERATE_INCR * ARG_FRAMERATE);
878 : chl 119 }
879 : chl 151 xparam.rc_reaction_delay_factor = 16;
880 : chl 479 xparam.rc_averaging_period = 100;
881 :     xparam.rc_buffer = 10;
882 : chl 151 xparam.rc_bitrate = ARG_BITRATE*1000;
883 : edgomez 484 xparam.min_quantizer = ARG_MINQUANT;
884 :     xparam.max_quantizer = ARG_MAXQUANT;
885 : chl 119 xparam.max_key_interval = (int)ARG_FRAMERATE*10;
886 : chl 151
887 : edgomez 483 /* I use a small value here, since will not encode whole movies, but short clips */
888 : chl 119
889 :     xerr = xvid_encore(NULL, XVID_ENC_CREATE, &xparam, NULL);
890 :     enc_handle=xparam.handle;
891 :    
892 :     return xerr;
893 :     }
894 :    
895 : edgomez 483 static int enc_stop()
896 :     {
897 :     int xerr;
898 : chl 119
899 :     xerr = xvid_encore(enc_handle, XVID_ENC_DESTROY, NULL, NULL);
900 : edgomez 483 return xerr;
901 :    
902 : chl 119 }
903 :    
904 : edgomez 483 static int enc_main(unsigned char* image, unsigned char* bitstream,
905 : edgomez 728 unsigned char* hints_buffer,
906 :     long *streamlength, long* frametype, long* hints_size)
907 : edgomez 483 {
908 :     int xerr;
909 : chl 119
910 :     XVID_ENC_FRAME xframe;
911 :     XVID_ENC_STATS xstats;
912 :    
913 :     xframe.bitstream = bitstream;
914 : edgomez 684 xframe.length = -1; /* this is written by the routine */
915 : chl 119
916 :     xframe.image = image;
917 : edgomez 684 xframe.colorspace = XVID_CSP_YV12; /* defined in <xvid.h> */
918 : chl 119
919 : edgomez 684 xframe.intra = -1; /* let the codec decide between I-frame (1) and P-frame (0) */
920 : chl 119
921 : edgomez 684 xframe.quant = ARG_QUANTI; /* is quant != 0, use a fixed quant (and ignore bitrate) */
922 : chl 119
923 :     xframe.motion = motion_presets[ARG_QUALITY];
924 :     xframe.general = general_presets[ARG_QUALITY];
925 :     xframe.quant_intra_matrix = xframe.quant_inter_matrix = NULL;
926 :    
927 : edgomez 728 xframe.hint.hintstream = hints_buffer;
928 :    
929 :     if(ARG_HINTMODE == HINT_MODE_SET) {
930 :     xframe.hint.hintlength = *hints_size;
931 :     xframe.hint.rawhints = 0;
932 :     xframe.general |= XVID_HINTEDME_SET;
933 :     }
934 :    
935 :     if(ARG_HINTMODE == HINT_MODE_GET) {
936 :     xframe.hint.rawhints = 0;
937 :     xframe.general |= XVID_HINTEDME_GET;
938 :     }
939 :    
940 : chl 119 xerr = xvid_encore(enc_handle, XVID_ENC_ENCODE, &xframe, &xstats);
941 :    
942 : edgomez 728 if(ARG_HINTMODE == HINT_MODE_GET)
943 :     *hints_size = xframe.hint.hintlength;
944 :    
945 : edgomez 483 /*
946 :     * This is statictical data, e.g. for 2-pass. If you are not
947 :     * interested in any of this, you can use NULL instead of &xstats
948 :     */
949 : chl 119 *frametype = xframe.intra;
950 :     *streamlength = xframe.length;
951 :    
952 :     return xerr;
953 :     }
954 :    
955 : edgomez 483 /*****************************************************************************
956 :     * Routines for decoding: init encoder, frame step, release encoder
957 :     ****************************************************************************/
958 : chl 119
959 : edgomez 483 /* init decoder before first run */
960 :     static int dec_init(int use_assembler)
961 : chl 119 {
962 : edgomez 483 int xerr;
963 : chl 119
964 : edgomez 483 XVID_INIT_PARAM xinit;
965 :     XVID_DEC_PARAM xparam;
966 : chl 119
967 : edgomez 483 if(use_assembler)
968 : Isibaar 209
969 :     #ifdef ARCH_IA64
970 : edgomez 483 xinit.cpu_flags = XVID_CPU_FORCE | XVID_CPU_IA64;
971 : Isibaar 209 #else
972 : edgomez 483 xinit.cpu_flags = 0;
973 : Isibaar 209 #endif
974 :    
975 : edgomez 483 else
976 :     xinit.cpu_flags = XVID_CPU_FORCE;
977 : Isibaar 209
978 : edgomez 483 xvid_init(NULL, 0, &xinit, NULL);
979 :     xparam.width = XDIM;
980 :     xparam.height = YDIM;
981 : chl 119
982 : edgomez 483 xerr = xvid_decore(NULL, XVID_DEC_CREATE, &xparam, NULL);
983 :     dec_handle = xparam.handle;
984 : chl 119
985 : edgomez 483 return xerr;
986 : chl 119 }
987 :    
988 : edgomez 483 /* decode one frame */
989 :     static int dec_main(unsigned char *m4v_buffer, unsigned char *out_buffer,
990 :     int m4v_size)
991 :     {
992 :     int xerr;
993 :     XVID_DEC_FRAME xframe;
994 : chl 119
995 : edgomez 483 xframe.bitstream = m4v_buffer;
996 :     xframe.length = m4v_size;
997 :     xframe.image = out_buffer;
998 :     xframe.stride = XDIM;
999 : edgomez 684 xframe.colorspace = XVID_CSP_YV12; /* XVID_CSP_USER is fastest (no memcopy involved) */
1000 : chl 119
1001 : edgomez 483 xerr = xvid_decore(dec_handle, XVID_DEC_DECODE, &xframe, NULL);
1002 : chl 119
1003 : edgomez 483 return xerr;
1004 : chl 119 }
1005 :    
1006 : edgomez 483 /* close decoder to release resources */
1007 :     static int dec_stop()
1008 : chl 119 {
1009 : edgomez 483 int xerr;
1010 :     xerr = xvid_decore(dec_handle, XVID_DEC_DESTROY, NULL, NULL);
1011 : chl 119
1012 : edgomez 483 return xerr;
1013 : chl 119 }
1014 :    
1015 : edgomez 483 /* EOF */

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