5 |
* |
* |
6 |
* Copyright(C) 2002 Christoph Lampert |
* Copyright(C) 2002 Christoph Lampert |
7 |
* |
* |
|
* This program is an implementation of a part of one or more MPEG-4 |
|
|
* Video tools as specified in ISO/IEC 14496-2 standard. Those intending |
|
|
* to use this software module in hardware or software products are |
|
|
* advised that its use may infringe existing patents or copyrights, and |
|
|
* any such use would be at such party's own risk. The original |
|
|
* developer of this software module and his/her company, and subsequent |
|
|
* editors and their companies, will have no liability for use of this |
|
|
* software or modifications or derivatives thereof. |
|
|
* |
|
8 |
* This program is free software; you can redistribute it and/or modify |
* 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 |
* 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 |
* the Free Software Foundation; either version 2 of the License, or |
19 |
* along with this program; if not, write to the Free Software |
* along with this program; if not, write to the Free Software |
20 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
21 |
* |
* |
22 |
* $Id: xvid_stat.c,v 1.5 2002-09-12 19:18:12 chl Exp $ |
* $Id: xvid_stat.c,v 1.11 2002-09-21 11:55:53 edgomez Exp $ |
23 |
* |
* |
24 |
****************************************************************************/ |
****************************************************************************/ |
25 |
|
|
26 |
/************************************************************************ |
/***************************************************************************** |
27 |
* |
* Application notes : |
|
* PSNR and Speed test routine for XviD using the XviD-API |
|
|
* (C) Christoph Lampert, 2002/04/13 |
|
28 |
* |
* |
29 |
* A sequence of YUV pics in PGM file format is encoded and decoded |
* 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. |
* 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, |
* The program is plain C and needs no libraries except for libxvidcore, |
33 |
* and maths-lib ,so with UN*X you simply compile by |
* and maths-lib. |
|
* |
|
|
* gcc xvid_stat.c -lxvidcore -lm -o xvid_stat |
|
|
* |
|
|
* Run without or with illegal parameters, then PGM input input is read |
|
|
* from stdin. |
|
|
* |
|
|
* Parameters are: xvid_stat XDIM YDIM QUALITY BITRATE/QUANTIZER FRAMERATE |
|
34 |
* |
* |
35 |
* if XDIM or YDIM are illegal (e.g. 0), they are ignored and input is |
* Usage : xvid_stat [OPTIONS] |
36 |
* considered to be PGM. Otherwise (X and Y both greater than 0) raw YUV |
* Options : |
37 |
* is expected, as e.g. the standard MPEG test-files, like "foreman" |
* -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 |
|
* -h, -help : prints this help message |
48 |
|
* -quant integer : fixed quantizer (disables -b setting) |
49 |
|
* (* means default) |
50 |
|
* |
51 |
|
* An input file named "stdin" is treated as standard input stream. |
52 |
|
* |
53 |
|
* |
54 |
|
* PGM input must be in a very specific format, basically it pgm file must |
55 |
|
* contain Y plane first just like usual P5 pgm files, and then U and V |
56 |
|
* planes are stored just after Y plane so y dimension is y*3/2 in reality |
57 |
* |
* |
58 |
* 0 <= QUALITY <= 6 (default 5) |
* See read_pgmheader for more details. |
59 |
* |
* |
60 |
* BITRATE is in kbps (default 900), |
* Such a PGM file can be generated from MPEG2 by # mpeg2dec -o pgmpipe |
|
* if BITRATE<32, then value is taken is fixed QUANTIZER |
|
61 |
* |
* |
62 |
* FRAMERATE is a float (with or without decimal dot), default is 25.00 |
****************************************************************************/ |
|
* |
|
|
* input/output and m4v-output is saved, if corresponding flags are set |
|
|
* |
|
|
* PGM input must in a very specific format, see read_pgmheader |
|
|
* it can be generated e.g. from MPEG2 by mpeg2dec -o pgmpipe |
|
|
* |
|
|
************************************************************************/ |
|
|
|
|
|
/************************************************************************ |
|
|
* |
|
|
* For EXAMPLES how to use this, see the seperate file xvid_stat.examples |
|
|
* |
|
|
************************************************************************/ |
|
63 |
|
|
64 |
#include <stdio.h> |
#include <stdio.h> |
65 |
#include <stdlib.h> |
#include <stdlib.h> |
66 |
#include <math.h> // needed for log10 |
#include <string.h> |
67 |
#include <sys/time.h> // only needed for gettimeofday |
#include <math.h> |
68 |
|
#include <time.h> |
69 |
|
|
70 |
|
#include "../src/xvid.h" |
71 |
|
|
72 |
|
/**************************************************************************** |
73 |
|
* Prototypes |
74 |
|
***************************************************************************/ |
75 |
|
|
76 |
|
/* Prints program usage message */ |
77 |
|
static void usage(); |
78 |
|
|
79 |
|
/* Statistical functions */ |
80 |
|
static double msecond(); |
81 |
|
static double absdistq(int x, int y, |
82 |
|
unsigned char* buf1, int stride1, |
83 |
|
unsigned char* buf2, int stride2); |
84 |
|
static double PSNR(int x, int y, |
85 |
|
unsigned char* buf1, int stride1, |
86 |
|
unsigned char* buf2, int stride2); |
87 |
|
|
88 |
|
/* PGM related functions */ |
89 |
|
static int read_pgmheader(FILE* handle); |
90 |
|
static int read_pgmdata(FILE* handle, unsigned char *image); |
91 |
|
static int read_yuvdata(FILE* handle, unsigned char *image); |
92 |
|
static int write_pgm(char *filename, unsigned char *image); |
93 |
|
|
94 |
|
/* Encoder related functions */ |
95 |
|
static int enc_init(int use_assembler); |
96 |
|
static int enc_stop(); |
97 |
|
static int enc_main(unsigned char* image, unsigned char* bitstream, |
98 |
|
int *streamlength, int* frametype); |
99 |
|
|
100 |
|
/* Decoder related functions */ |
101 |
|
static int dec_stop(); |
102 |
|
static int dec_main(unsigned char *m4v_buffer, unsigned char *out_buffer, |
103 |
|
int m4v_size); |
104 |
|
static int dec_init(int use_assembler); |
105 |
|
|
106 |
#include "../src/xvid.h" /* comes with XviD */ |
/***************************************************************************** |
107 |
|
* Quality presets |
108 |
|
****************************************************************************/ |
109 |
|
|
110 |
int motion_presets[7] = { |
static int const motion_presets[7] = { |
111 |
0, // Q 0 |
0, // Q 0 |
112 |
PMV_EARLYSTOP16, // Q 1 |
PMV_EARLYSTOP16, // Q 1 |
113 |
PMV_EARLYSTOP16, // Q 2 |
PMV_EARLYSTOP16, // Q 2 |
114 |
PMV_EARLYSTOP16 | PMV_HALFPELREFINE16, // Q 3 |
PMV_EARLYSTOP16 | PMV_HALFPELREFINE16, // Q 3 |
115 |
PMV_EARLYSTOP16 | PMV_HALFPELREFINE16, // Q 4 |
PMV_EARLYSTOP16 | PMV_HALFPELREFINE16, // Q 4 |
116 |
PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 | PMV_EARLYSTOP8 // Q 5 |
PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 | PMV_EARLYSTOP8 | // Q 5 |
117 |
| PMV_HALFPELREFINE8, |
PMV_HALFPELREFINE8, |
118 |
PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 | PMV_EXTSEARCH16 // Q 6 |
PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 | PMV_EXTSEARCH16 | // Q 6 |
119 |
| PMV_USESQUARES16 | PMV_EARLYSTOP8 | PMV_HALFPELREFINE8 |
PMV_USESQUARES16 | PMV_EARLYSTOP8 | PMV_HALFPELREFINE8 |
120 |
}; |
}; |
121 |
|
|
122 |
int general_presets[7] = { |
static int const general_presets[7] = { |
123 |
XVID_H263QUANT, /* or use XVID_MPEGQUANT */ // Q 0 |
XVID_H263QUANT, // Q 0 |
124 |
XVID_MPEGQUANT, // Q 1 |
XVID_MPEGQUANT, // Q 1 |
125 |
XVID_H263QUANT, // Q 2 |
XVID_H263QUANT, // Q 2 |
126 |
XVID_H263QUANT | XVID_HALFPEL, // Q 3 |
XVID_H263QUANT | XVID_HALFPEL, // Q 3 |
127 |
XVID_H263QUANT | XVID_HALFPEL | XVID_INTER4V, // Q 4 |
XVID_H263QUANT | XVID_HALFPEL | XVID_INTER4V, // Q 4 |
128 |
XVID_H263QUANT | XVID_HALFPEL | XVID_INTER4V, // Q 5 |
XVID_H263QUANT | XVID_HALFPEL | XVID_INTER4V, // Q 5 |
129 |
XVID_H263QUANT | XVID_HALFPEL | XVID_INTER4V }; // Q 6 |
XVID_H263QUANT | XVID_HALFPEL | XVID_INTER4V // Q 6 |
130 |
|
}; |
131 |
|
|
132 |
|
|
133 |
|
/***************************************************************************** |
134 |
|
* Command line global variables |
135 |
|
****************************************************************************/ |
136 |
|
|
137 |
|
/* Maximum number of frames to encode */ |
138 |
|
#define ABS_MAXFRAMENR 9999 |
139 |
|
|
140 |
/* my default values for encoding */ |
static int ARG_BITRATE = 900; |
141 |
|
static int ARG_QUANTI = 0; |
142 |
|
static int ARG_QUALITY = 6; |
143 |
|
static int ARG_MINQUANT = 1; |
144 |
|
static int ARG_MAXQUANT = 31; |
145 |
|
static float ARG_FRAMERATE = 25.00f; |
146 |
|
static int ARG_MAXFRAMENR = ABS_MAXFRAMENR; |
147 |
|
static char *ARG_INPUTFILE = NULL; |
148 |
|
static int ARG_INPUTTYPE = 0; |
149 |
|
static int ARG_SAVEDECOUTPUT = 0; |
150 |
|
static int ARG_SAVEMPEGSTREAM = 0; |
151 |
|
static int XDIM = 0; |
152 |
|
static int YDIM = 0; |
153 |
|
#define IMAGE_SIZE(x,y) ((x)*(y)*3/2) |
154 |
|
|
155 |
#define ABS_MAXFRAMENR 9999 // max number of frames |
#define MAX(A,B) ( ((A)>(B)) ? (A) : (B) ) |
156 |
|
#define SMALL_EPS 1e-10 |
157 |
|
|
158 |
int ARG_BITRATE=900; |
/**************************************************************************** |
159 |
int ARG_QUANTI=0; |
* Nasty global vars ;-) |
160 |
|
***************************************************************************/ |
161 |
|
|
162 |
|
static int i,filenr = 0; |
163 |
|
static int save_ref_flag = 0; |
164 |
|
|
165 |
|
/* the path where to save output */ |
166 |
|
static char filepath[256] = "./"; |
167 |
|
|
168 |
|
/* Internal structures (handles) for encoding and decoding */ |
169 |
|
static void *enc_handle = NULL; |
170 |
|
static void *dec_handle = NULL; |
171 |
|
|
172 |
int ARG_QUALITY =6; |
/***************************************************************************** |
173 |
int ARG_MINQUANT=1; |
* Main program |
174 |
int ARG_MAXQUANT=31; |
****************************************************************************/ |
|
float ARG_FRAMERATE=25.00; |
|
175 |
|
|
176 |
int ARG_MAXFRAMENR=ABS_MAXFRAMENR; |
int main(int argc, char *argv[]) |
177 |
|
{ |
178 |
|
|
179 |
|
unsigned char *divx_buffer = NULL; |
180 |
|
unsigned char *in_buffer = NULL; |
181 |
|
unsigned char *out_buffer = NULL; |
182 |
|
|
183 |
#define MAX(A,B) ( ((A)>(B)) ? (A) : (B) ) |
double enctime,dectime; |
184 |
#define SMALL_EPS 1e-10 |
double totalenctime=0.; |
185 |
|
double totaldectime=0.; |
186 |
|
|
187 |
|
long totalsize=0; |
188 |
|
int status; |
189 |
|
|
190 |
|
int m4v_size; |
191 |
|
int frame_type[ABS_MAXFRAMENR]; |
192 |
|
int Iframes=0, Pframes=0, use_assembler=0; |
193 |
|
double framepsnr[ABS_MAXFRAMENR]; |
194 |
|
|
195 |
|
double Ipsnr=0.,Imaxpsnr=0.,Iminpsnr=999.,Ivarpsnr=0.; |
196 |
|
double Ppsnr=0.,Pmaxpsnr=0.,Pminpsnr=999.,Pvarpsnr=0.; |
197 |
|
double Bpsnr=0.,Bmaxpsnr=0.,Bminpsnr=999.,Bvarpsnr=0.; |
198 |
|
|
199 |
|
char filename[256]; |
200 |
|
|
201 |
|
FILE *filehandle; |
202 |
|
FILE *in_file = stdin; |
203 |
|
|
204 |
|
printf("xvid_stat - XviD core library test program "); |
205 |
|
printf("written by Christoph Lampert 2002\n\n"); |
206 |
|
|
207 |
|
/***************************************************************************** |
208 |
|
* Command line parsing |
209 |
|
****************************************************************************/ |
210 |
|
|
211 |
|
for (i=1; i< argc; i++) { |
212 |
|
|
213 |
|
if (strcmp("-asm", argv[i]) == 0 ) { |
214 |
|
use_assembler = 1; |
215 |
|
} |
216 |
|
else if (strcmp("-w", argv[i]) == 0 && i < argc - 1 ) { |
217 |
|
i++; |
218 |
|
XDIM = atoi(argv[i]); |
219 |
|
} |
220 |
|
else if (strcmp("-h", argv[i]) == 0 && i < argc - 1 ) { |
221 |
|
i++; |
222 |
|
YDIM = atoi(argv[i]); |
223 |
|
} |
224 |
|
else if (strcmp("-b", argv[i]) == 0 && i < argc - 1 ) { |
225 |
|
i++; |
226 |
|
ARG_BITRATE = atoi(argv[i]); |
227 |
|
} |
228 |
|
else if (strcmp("-q", argv[i]) == 0 && i < argc - 1 ) { |
229 |
|
i++; |
230 |
|
ARG_QUALITY = atoi(argv[i]); |
231 |
|
} |
232 |
|
else if (strcmp("-f", argv[i]) == 0 && i < argc - 1 ) { |
233 |
|
i++; |
234 |
|
ARG_FRAMERATE = (float)atof(argv[i]); |
235 |
|
} |
236 |
|
else if (strcmp("-i", argv[i]) == 0 && i < argc - 1 ) { |
237 |
|
i++; |
238 |
|
ARG_INPUTFILE = argv[i]; |
239 |
|
} |
240 |
|
else if (strcmp("-t", argv[i]) == 0 && i < argc - 1 ) { |
241 |
|
i++; |
242 |
|
ARG_INPUTTYPE = atoi(argv[i]); |
243 |
|
} |
244 |
|
else if(strcmp("-n", argv[i]) == 0 && i < argc - 1 ) { |
245 |
|
i++; |
246 |
|
ARG_MAXFRAMENR = atoi(argv[i]); |
247 |
|
} |
248 |
|
else if (strcmp("-quant", argv[i]) == 0 && i < argc - 1 ) { |
249 |
|
i++; |
250 |
|
ARG_QUANTI = atoi(argv[i]); |
251 |
|
} |
252 |
|
else if (strcmp("-d", argv[i]) == 0 && i < argc - 1 ) { |
253 |
|
i++; |
254 |
|
ARG_SAVEDECOUTPUT = atoi(argv[i]); |
255 |
|
} |
256 |
|
else if (strcmp("-m", argv[i]) == 0 && i < argc - 1 ) { |
257 |
|
i++; |
258 |
|
ARG_SAVEMPEGSTREAM = atoi(argv[i]); |
259 |
|
} |
260 |
|
else if (strcmp("-h", argv[i]) == 0 || strcmp("-help", argv[i])) { |
261 |
|
usage(); |
262 |
|
return(0); |
263 |
|
} |
264 |
|
else { |
265 |
|
usage(); |
266 |
|
exit(-1); |
267 |
|
} |
268 |
|
|
269 |
|
} |
270 |
|
|
271 |
|
/***************************************************************************** |
272 |
|
* Arguments checking |
273 |
|
****************************************************************************/ |
274 |
|
|
275 |
|
if (XDIM <= 0 || XDIM >= 2048 || YDIM <=0 || YDIM >= 2048 ) { |
276 |
|
fprintf(stderr, |
277 |
|
"Trying to retreive width and height from PGM header\n", |
278 |
|
XDIM, |
279 |
|
YDIM); |
280 |
|
ARG_INPUTTYPE = 1; /* pgm */ |
281 |
|
} |
282 |
|
|
283 |
|
if ( ARG_QUALITY < 0 || ARG_QUALITY > 6) { |
284 |
|
fprintf(stderr,"Wrong Quality\n"); |
285 |
|
return -1; |
286 |
|
} |
287 |
|
|
288 |
|
if ( ARG_BITRATE <= 0 && ARG_QUANTI == 0) { |
289 |
|
fprintf(stderr,"Wrong Bitrate\n"); |
290 |
|
return -1; |
291 |
|
} |
292 |
|
|
293 |
|
if ( ARG_FRAMERATE <= 0) { |
294 |
|
fprintf(stderr,"Wrong Fraterate %s \n",argv[5]); |
295 |
|
return -1; |
296 |
|
} |
297 |
|
|
298 |
|
if ( ARG_MAXFRAMENR <= 0) { |
299 |
|
fprintf(stderr,"Wrong number of frames\n"); |
300 |
|
return -1; |
301 |
|
} |
302 |
|
|
303 |
|
if ( ARG_INPUTFILE == NULL || strcmp(ARG_INPUTFILE, "stdin") == 0) { |
304 |
|
in_file = stdin; |
305 |
|
} |
306 |
|
else { |
307 |
|
|
308 |
|
in_file = fopen(ARG_INPUTFILE, "rb"); |
309 |
|
if (in_file == NULL) { |
310 |
|
fprintf(stderr, "Error opening input file %s\n", ARG_INPUTFILE); |
311 |
|
return -1; |
312 |
|
} |
313 |
|
} |
314 |
|
|
315 |
|
if (ARG_INPUTTYPE) { |
316 |
|
if (read_pgmheader(in_file)) { |
317 |
|
fprintf(stderr, "Wrong input format, I want YUV encapsulated in PGM\n"); |
318 |
|
return -1; |
319 |
|
} |
320 |
|
} |
321 |
|
|
322 |
|
/* now we know the sizes, so allocate memory */ |
323 |
|
|
324 |
|
in_buffer = (unsigned char *) malloc(IMAGE_SIZE(XDIM,YDIM)); |
325 |
|
if (!in_buffer) |
326 |
|
goto free_all_memory; |
327 |
|
|
328 |
|
/* this should really be enough memory ! */ |
329 |
|
divx_buffer = (unsigned char *) malloc(IMAGE_SIZE(XDIM,YDIM)*2); |
330 |
|
if (!divx_buffer) |
331 |
|
goto free_all_memory; |
332 |
|
|
333 |
|
out_buffer = (unsigned char *) malloc(IMAGE_SIZE(XDIM,YDIM)*4); |
334 |
|
if (!out_buffer) |
335 |
|
goto free_all_memory; |
336 |
|
|
337 |
|
|
338 |
|
/***************************************************************************** |
339 |
|
* XviD PART Start |
340 |
|
****************************************************************************/ |
341 |
|
|
342 |
|
|
343 |
|
status = enc_init(use_assembler); |
344 |
|
if (status) |
345 |
|
{ |
346 |
|
fprintf(stderr, "Encore INIT problem, return value %d\n", status); |
347 |
|
goto release_all; |
348 |
|
} |
349 |
|
|
350 |
|
status = dec_init(use_assembler); |
351 |
|
if (status) |
352 |
|
{ |
353 |
|
fprintf(stderr, "Decore INIT problem, return value %d\n", status); |
354 |
|
goto release_all; |
355 |
|
} |
356 |
|
|
357 |
|
|
358 |
|
/***************************************************************************** |
359 |
|
* Main loop |
360 |
|
****************************************************************************/ |
361 |
|
|
362 |
|
do { |
363 |
|
|
364 |
|
if (ARG_INPUTTYPE) |
365 |
|
status = read_pgmdata(in_file, in_buffer); // read PGM data (YUV-format) |
366 |
|
else |
367 |
|
status = read_yuvdata(in_file, in_buffer); // read raw data (YUV-format) |
368 |
|
|
369 |
|
if (status) |
370 |
|
{ |
371 |
|
/* Couldn't read image, most likely end-of-file */ |
372 |
|
continue; |
373 |
|
} |
374 |
|
|
375 |
|
|
376 |
|
if (save_ref_flag) |
377 |
|
{ |
378 |
|
sprintf(filename, "%s%05d.pgm", filepath, filenr); |
379 |
|
write_pgm(filename,in_buffer); |
380 |
|
} |
381 |
|
|
382 |
|
|
383 |
|
/***************************************************************************** |
384 |
|
* Analyse this frame before encoding |
385 |
|
****************************************************************************/ |
386 |
|
|
387 |
/* these are global variables. Not very elegant, but easy, and this is an easy program */ |
/* |
388 |
|
* nothing is done here at the moment, but you could e.g. create |
389 |
|
* histograms or measure entropy or apply preprocessing filters... |
390 |
|
*/ |
391 |
|
|
392 |
|
/***************************************************************************** |
393 |
|
* Encode and decode this frame |
394 |
|
****************************************************************************/ |
395 |
|
|
396 |
|
enctime = msecond(); |
397 |
|
status = enc_main(in_buffer, divx_buffer, &m4v_size, &frame_type[filenr]); |
398 |
|
enctime = msecond() - enctime; |
399 |
|
|
400 |
|
totalenctime += enctime; |
401 |
|
totalsize += m4v_size; |
402 |
|
|
403 |
|
printf("Frame %5d: intra %1d, enctime=%6.1f ms, size=%6d bytes ", |
404 |
|
(int)filenr, (int)frame_type[filenr], (float)enctime, (int)m4v_size); |
405 |
|
|
406 |
|
if (ARG_SAVEMPEGSTREAM) |
407 |
|
{ |
408 |
|
sprintf(filename, "%sframe%05d.m4v", filepath, filenr); |
409 |
|
filehandle = fopen(filename, "wb"); |
410 |
|
fwrite(divx_buffer, m4v_size, 1, filehandle); |
411 |
|
fclose(filehandle); |
412 |
|
} |
413 |
|
|
414 |
|
dectime = msecond(); |
415 |
|
status = dec_main(divx_buffer, out_buffer, m4v_size); |
416 |
|
dectime = msecond() - dectime; |
417 |
|
|
418 |
|
totaldectime += dectime; |
419 |
|
|
420 |
|
|
421 |
|
/***************************************************************************** |
422 |
|
* Analyse the decoded frame and compare to original |
423 |
|
****************************************************************************/ |
424 |
|
|
425 |
|
framepsnr[filenr] = PSNR(XDIM,YDIM*3/2, in_buffer, XDIM, out_buffer, XDIM); |
426 |
|
|
427 |
|
printf("dectime =%6.1f ms PSNR %5.2f\n",dectime, framepsnr[filenr]); |
428 |
|
|
429 |
|
if (ARG_SAVEDECOUTPUT) |
430 |
|
{ |
431 |
|
sprintf(filename, "%sdec%05d.pgm", filepath, filenr); |
432 |
|
write_pgm(filename, out_buffer); |
433 |
|
} |
434 |
|
|
435 |
|
/* Read the header if it's pgm stream */ |
436 |
|
if (ARG_INPUTTYPE) |
437 |
|
status = read_pgmheader(in_file); |
438 |
|
|
439 |
|
filenr++; |
440 |
|
|
441 |
|
} while ( (!status) && (filenr<ARG_MAXFRAMENR) ); |
442 |
|
|
443 |
|
|
444 |
|
|
445 |
|
/***************************************************************************** |
446 |
|
* Calculate totals and averages for output, print results |
447 |
|
****************************************************************************/ |
448 |
|
|
449 |
|
totalsize /= filenr; |
450 |
|
totalenctime /= filenr; |
451 |
|
totaldectime /= filenr; |
452 |
|
|
453 |
|
for (i=0;i<filenr;i++) |
454 |
|
{ |
455 |
|
switch (frame_type[i]) |
456 |
|
{ |
457 |
|
case 0: |
458 |
|
Pframes++; |
459 |
|
Ppsnr += framepsnr[i]; |
460 |
|
break; |
461 |
|
case 1: |
462 |
|
Iframes++; |
463 |
|
Ipsnr += framepsnr[i]; |
464 |
|
break; |
465 |
|
default: |
466 |
|
break; |
467 |
|
} |
468 |
|
} |
469 |
|
|
470 |
|
if (Pframes) |
471 |
|
Ppsnr /= Pframes; |
472 |
|
if (Iframes) |
473 |
|
Ipsnr /= Iframes; |
474 |
|
|
475 |
|
/* calculate statistics for every frametype: P,I */ |
476 |
|
for (i=0;i<filenr;i++) |
477 |
|
{ |
478 |
|
switch (frame_type[i]) |
479 |
|
{ |
480 |
|
case 0: |
481 |
|
if (framepsnr[i] > Pmaxpsnr) |
482 |
|
Pmaxpsnr = framepsnr[i]; |
483 |
|
if (framepsnr[i] < Pminpsnr) |
484 |
|
Pminpsnr = framepsnr[i]; |
485 |
|
Pvarpsnr += (framepsnr[i] - Ppsnr)*(framepsnr[i] - Ppsnr) /Pframes; |
486 |
|
break; |
487 |
|
case 1: |
488 |
|
if (framepsnr[i] > Imaxpsnr) |
489 |
|
Imaxpsnr = framepsnr[i]; |
490 |
|
if (framepsnr[i] < Pminpsnr) |
491 |
|
Iminpsnr = framepsnr[i]; |
492 |
|
Ivarpsnr += (framepsnr[i] - Ipsnr)*(framepsnr[i] - Ipsnr) /Iframes; |
493 |
|
default: |
494 |
|
break; |
495 |
|
} |
496 |
|
} |
497 |
|
|
498 |
|
/* Print all statistics */ |
499 |
|
printf("Avg. Q%1d %2s ",ARG_QUALITY, (ARG_QUANTI ? " q" : "br")); |
500 |
|
printf("%04d ",(ARG_QUANTI)?ARG_QUANTI:ARG_BITRATE); |
501 |
|
printf("( %.2f bpp) ", (double)ARG_BITRATE*1000/XDIM/YDIM/ARG_FRAMERATE); |
502 |
|
printf("size %6d ",totalsize); |
503 |
|
printf("( %4d kbps ",(int)(totalsize*8*ARG_FRAMERATE/1000)); |
504 |
|
printf("/ %.2f bpp) ",(double)totalsize*8/XDIM/YDIM); |
505 |
|
printf("enc: %6.1f fps, dec: %6.1f fps \n",CLOCKS_PER_SEC/(totalenctime*1000), CLOCKS_PER_SEC/(totaldectime*1000)); |
506 |
|
printf("PSNR P(%d): %5.2f ( %5.2f , %5.2f ; %5.4f ) ",Pframes,Ppsnr,Pminpsnr,Pmaxpsnr,sqrt(Pvarpsnr/filenr)); |
507 |
|
printf("I(%d): %5.2f ( %5.2f , %5.2f ; %5.4f ) ",Iframes,Ipsnr,Iminpsnr,Imaxpsnr,sqrt(Ivarpsnr/filenr)); |
508 |
|
printf("\n"); |
509 |
|
|
510 |
|
/***************************************************************************** |
511 |
|
* XviD PART Stop |
512 |
|
****************************************************************************/ |
513 |
|
|
514 |
|
release_all: |
515 |
|
|
516 |
|
if (enc_handle) |
517 |
|
{ |
518 |
|
status = enc_stop(); |
519 |
|
if (status) |
520 |
|
fprintf(stderr, "Encore RELEASE problem return value %d\n", status); |
521 |
|
} |
522 |
|
|
523 |
int XDIM=0; |
if (dec_handle) |
524 |
int YDIM=0; // will be set when reading first image |
{ |
525 |
int i,filenr = 0; |
status = dec_stop(); |
526 |
|
if (status) |
527 |
|
fprintf(stderr, "Decore RELEASE problem return value %d\n", status); |
528 |
|
} |
529 |
|
|
530 |
int save_m4v_flag = 0; // save MPEG4-bytestream? |
fclose(in_file); |
|
int save_dec_flag = 1; // save decompressed bytestream? |
|
|
int save_ref_flag = 0; // |
|
531 |
|
|
532 |
int pgmflag = 0; // a flag, if input is in PGM format, overwritten in init-phase |
free_all_memory: |
533 |
char filepath[256] = "./"; // the path where to save output |
free(out_buffer); |
534 |
|
free(divx_buffer); |
535 |
|
free(in_buffer); |
536 |
|
|
537 |
|
return 0; |
538 |
|
|
539 |
|
} |
540 |
|
|
541 |
void *enc_handle = NULL; // internal structures (handles) for encoding |
/***************************************************************************** |
542 |
void *dec_handle = NULL; // and decoding |
* "statistical" functions |
543 |
|
* |
544 |
|
* these are not needed for encoding or decoding, but for measuring |
545 |
|
* time and quality, there in nothing specific to XviD in these |
546 |
|
* |
547 |
|
*****************************************************************************/ |
548 |
|
|
549 |
|
|
|
/*********************************************************************/ |
|
|
/* "statistical" functions */ |
|
|
/* */ |
|
|
/* these are not needed for encoding or decoding, but for measuring */ |
|
|
/* time and quality, there in nothing specific to XviD in these */ |
|
|
/* */ |
|
|
/*********************************************************************/ |
|
550 |
|
|
551 |
double msecond() |
/* Return time elapsed time in miliseconds since the program started */ |
552 |
/* return the current time in seconds(!) */ |
static double msecond() |
553 |
{ |
{ |
554 |
struct timeval tv; |
clock_t clk; |
555 |
gettimeofday(&tv, 0); |
|
556 |
return tv.tv_sec + tv.tv_usec * 1.0e-6; |
clk = clock(); |
557 |
|
|
558 |
|
return clk * 1000 / CLOCKS_PER_SEC; |
559 |
|
|
560 |
} |
} |
561 |
|
|
562 |
|
|
563 |
double absdistq(int x,int y, unsigned char* buf1, int stride1, unsigned char* buf2, int stride2) |
/* |
564 |
/* returns the sum of squared distances (SSD) between two images of dimensions x times y */ |
* Returns the sum of squared distances (SSD) between two images of dimensions |
565 |
|
* x times y |
566 |
|
*/ |
567 |
|
static double absdistq(int x, int y, |
568 |
|
unsigned char* buf1, int stride1, |
569 |
|
unsigned char* buf2, int stride2) |
570 |
{ |
{ |
571 |
double dist=0.; |
double dist=0.; |
572 |
int i,j,val; |
int i,j,val; |
585 |
} |
} |
586 |
|
|
587 |
|
|
588 |
double PSNR(int x,int y, unsigned char* buf1, int stride1, unsigned char* buf2, int stride2 ) |
/* |
589 |
/* return the PSNR between to images */ |
* Returns the PSNR between to images. |
590 |
/* this is a logarithmic measure for "quality" from the world of signal processing */ |
* |
591 |
/* if you don't know what it is, simply accept that higher values are better */ |
* This is a common logarithmic measure for "quality" from the world of signal |
592 |
{ |
* processing if you don't know what it is, simply accept that higher values |
593 |
return 10*(log10(255*255)-log10( absdistq(x, y, buf1, stride1, buf2, stride2) )); |
* are better. |
594 |
|
* |
595 |
|
* PSNR represents the ratio of useful signal over noise signal. In our case, |
596 |
|
* useful signal is refernce image, noise signal is the difference between |
597 |
|
* reference and decoded frame from encoded bitstream. |
598 |
|
* |
599 |
|
* The problem is this type of value is dependant of image source and so, is |
600 |
|
* not reliable as a common "quality" indicator. |
601 |
|
* So PSNR computes the ratio of maximum/noise. Maximum being set to 2^bpp/channel |
602 |
|
* This way, PSNR is not dependant anymore of image data type. |
603 |
|
* |
604 |
|
*/ |
605 |
|
static double PSNR(int x, int y, |
606 |
|
unsigned char* buf1, int stride1, |
607 |
|
unsigned char* buf2, int stride2) |
608 |
|
{ |
609 |
|
return 10*(log10(255*255)-log10( absdistq(x, y, buf1, stride1, buf2, stride2) )); |
610 |
|
} |
611 |
|
|
612 |
|
/***************************************************************************** |
613 |
|
* Usage message |
614 |
|
*****************************************************************************/ |
615 |
|
|
616 |
|
static void usage() |
617 |
|
{ |
618 |
|
|
619 |
|
fprintf(stderr, "Usage : xvid_stat [OPTIONS]\n"); |
620 |
|
fprintf(stderr, "Options :\n"); |
621 |
|
fprintf(stderr, " -w integer : frame width ([1.2048])\n"); |
622 |
|
fprintf(stderr, " -h integer : frame height ([1.2048])\n"); |
623 |
|
fprintf(stderr, " -b integer : target bitrate (>0 | default=900kbit)\n"); |
624 |
|
fprintf(stderr, " -f float : target framerate (>0)\n"); |
625 |
|
fprintf(stderr, " -i string : input filename (default=stdin)\n"); |
626 |
|
fprintf(stderr, " -t integer : input data type (yuv=0, pgm=1)\n"); |
627 |
|
fprintf(stderr, " -n integer : number of frames to encode\n"); |
628 |
|
fprintf(stderr, " -q integer : quality ([0..5])\n"); |
629 |
|
fprintf(stderr, " -d boolean : save decoder output (0 False*, !=0 True)\n"); |
630 |
|
fprintf(stderr, " -m boolean : save mpeg4 raw stream (0 False*, !=0 True)\n"); |
631 |
|
fprintf(stderr, " -h, -help : prints this help message\n"); |
632 |
|
fprintf(stderr, " -quant integer : fixed quantizer (disables -b setting)\n"); |
633 |
|
fprintf(stderr, " (* means default)\n"); |
634 |
|
|
635 |
} |
} |
636 |
|
|
637 |
|
/***************************************************************************** |
638 |
|
* Input and output functions |
639 |
|
* |
640 |
|
* the are small and simple routines to read and write PGM and YUV |
641 |
|
* image. It's just for convenience, again nothing specific to XviD |
642 |
|
* |
643 |
|
*****************************************************************************/ |
644 |
|
|
645 |
/*********************************************************************/ |
static int read_pgmheader(FILE* handle) |
|
/* input and output functions */ |
|
|
/* */ |
|
|
/* the are small and simple routines to read and write PGM and YUV */ |
|
|
/* image. It's just for convenience, again nothing specific to XviD */ |
|
|
/* */ |
|
|
/*********************************************************************/ |
|
|
|
|
|
int read_pgmheader(FILE* handle) |
|
646 |
{ |
{ |
647 |
int bytes,xsize,ysize,depth; |
int bytes,xsize,ysize,depth; |
648 |
char dummy[2]; |
char dummy[2]; |
651 |
|
|
652 |
if ( (bytes < 2) || (dummy[0] != 'P') || (dummy[1] != '5' )) |
if ( (bytes < 2) || (dummy[0] != 'P') || (dummy[1] != '5' )) |
653 |
return 1; |
return 1; |
654 |
|
|
655 |
fscanf(handle,"%d %d %d",&xsize,&ysize,&depth); |
fscanf(handle,"%d %d %d",&xsize,&ysize,&depth); |
656 |
if ( (xsize > 1440) || (ysize > 2880 ) || (depth != 255) ) |
if ( (xsize > 1440) || (ysize > 2880 ) || (depth != 255) ) |
657 |
{ |
{ |
659 |
return 2; |
return 2; |
660 |
} |
} |
661 |
if ( (XDIM==0) || (YDIM==0) ) |
if ( (XDIM==0) || (YDIM==0) ) |
662 |
{ XDIM=xsize; |
{ |
663 |
YDIM=ysize; |
XDIM=xsize; |
664 |
|
YDIM=ysize*2/3; |
665 |
} |
} |
666 |
|
|
667 |
return 0; |
return 0; |
668 |
} |
} |
669 |
|
|
670 |
int read_pgmdata(FILE* handle, unsigned char *image) |
static int read_pgmdata(FILE* handle, unsigned char *image) |
671 |
{ |
{ |
672 |
int i,status; |
int i; |
673 |
char dummy; |
char dummy; |
674 |
|
|
675 |
unsigned char* buff1_ptr2 = image + XDIM*YDIM; |
unsigned char *y = image; |
676 |
unsigned char* buff1_ptr3 = image + XDIM*YDIM + XDIM/2*YDIM/2; |
unsigned char *u = image + XDIM*YDIM; |
677 |
|
unsigned char *v = image + XDIM*YDIM + XDIM/2*YDIM/2; |
678 |
|
|
679 |
fread(image,XDIM*YDIM,1,stdin); // read Y component of picture |
/* read Y component of picture */ |
680 |
|
fread(y, 1, XDIM*YDIM, handle); |
681 |
|
|
682 |
for (i=0;i<YDIM/2;i++) |
for (i=0;i<YDIM/2;i++) |
683 |
{ |
{ |
684 |
fread(buff1_ptr2,XDIM/2,1,stdin); // read U |
/* read U */ |
685 |
buff1_ptr2 += XDIM/2; |
fread(u, 1, XDIM/2, handle); |
686 |
fread(buff1_ptr3,XDIM/2,1,stdin); // read V |
|
687 |
buff1_ptr3 += XDIM/2; |
/* read V */ |
688 |
|
fread(v, 1, XDIM/2, handle); |
689 |
|
|
690 |
|
/* Update pointers */ |
691 |
|
u += XDIM/2; |
692 |
|
v += XDIM/2; |
693 |
} |
} |
694 |
fread(&dummy,1,1,handle); // I don't know why, but this seems needed |
|
695 |
|
/* I don't know why, but this seems needed */ |
696 |
|
fread(&dummy, 1, 1, handle); |
697 |
|
|
698 |
return 0; |
return 0; |
699 |
} |
} |
700 |
|
|
701 |
int read_yuvdata(FILE* handle, unsigned char *image) |
static int read_yuvdata(FILE* handle, unsigned char *image) |
702 |
{ int i; |
{ |
|
char dummy; |
|
|
|
|
|
unsigned char* buff1_ptr2 = image + XDIM*YDIM; |
|
|
unsigned char* buff1_ptr3 = image + XDIM*YDIM + XDIM/2*YDIM/2; |
|
703 |
|
|
704 |
if (fread(image,XDIM,YDIM*3/2,stdin) != YDIM*3/2) |
if (fread(image, 1, IMAGE_SIZE(XDIM, YDIM), handle) != IMAGE_SIZE(XDIM, YDIM)) |
705 |
return 1; |
return 1; |
706 |
else |
else |
707 |
return 0; |
return 0; |
708 |
} |
} |
709 |
|
|
710 |
int write_pgm(char *filename, unsigned char *image) |
static int write_pgm(char *filename, unsigned char *image) |
711 |
{ |
{ |
712 |
|
int loop; |
713 |
|
|
714 |
|
unsigned char *y = image; |
715 |
|
unsigned char *u = image + XDIM*YDIM; |
716 |
|
unsigned char *v = image + XDIM*YDIM + XDIM/2*YDIM/2; |
717 |
|
|
718 |
FILE *filehandle; |
FILE *filehandle; |
719 |
filehandle=fopen(filename,"wb"); |
filehandle=fopen(filename,"w+b"); |
720 |
if (filehandle) |
if (filehandle) |
721 |
{ |
{ |
722 |
fprintf(filehandle,"P5\n\n"); // |
/* Write header */ |
723 |
fprintf(filehandle,"%d %d 255\n",XDIM,YDIM*3/2); |
fprintf(filehandle,"P5\n\n%d %d 255\n", XDIM,YDIM*3/2); |
724 |
fwrite(image,XDIM,YDIM*3/2,filehandle); |
|
725 |
|
/* Write Y data */ |
726 |
|
fwrite(y, 1, XDIM*YDIM, filehandle); |
727 |
|
|
728 |
|
for(loop=0; loop<YDIM/2; loop++) |
729 |
|
{ |
730 |
|
/* Write U scanline */ |
731 |
|
fwrite(u, 1, XDIM/2, filehandle); |
732 |
|
|
733 |
|
/* Write V scanline */ |
734 |
|
fwrite(v, 1, XDIM/2, filehandle); |
735 |
|
|
736 |
|
/* Update pointers */ |
737 |
|
u += XDIM/2; |
738 |
|
v += XDIM/2; |
739 |
|
|
740 |
|
} |
741 |
|
|
742 |
|
/* Close file */ |
743 |
fclose(filehandle); |
fclose(filehandle); |
744 |
|
|
745 |
return 0; |
return 0; |
746 |
} |
} |
747 |
else |
else |
748 |
return 1; |
return 1; |
749 |
} |
} |
750 |
|
|
751 |
|
/***************************************************************************** |
752 |
|
* Routines for encoding: init encoder, frame step, release encoder |
753 |
/*********************************************************************/ |
****************************************************************************/ |
|
/* Routines for encoding: init encoder, frame step, release encoder */ |
|
|
/*********************************************************************/ |
|
754 |
|
|
755 |
#define FRAMERATE_INCR 1001 |
#define FRAMERATE_INCR 1001 |
756 |
|
|
757 |
|
/* Initialize encoder for first use, pass all needed parameters to the codec */ |
758 |
int enc_init(int use_assembler) |
static int enc_init(int use_assembler) |
759 |
{ /* initialize encoder for first use, pass all needed parameters to the codec */ |
{ |
760 |
int xerr; |
int xerr; |
761 |
|
|
762 |
XVID_INIT_PARAM xinit; |
XVID_INIT_PARAM xinit; |
763 |
XVID_ENC_PARAM xparam; |
XVID_ENC_PARAM xparam; |
764 |
|
|
765 |
if(use_assembler) |
if(use_assembler) { |
766 |
|
|
767 |
#ifdef ARCH_IA64 |
#ifdef ARCH_IA64 |
768 |
xinit.cpu_flags = XVID_CPU_FORCE | XVID_CPU_IA64; |
xinit.cpu_flags = XVID_CPU_FORCE | XVID_CPU_IA64; |
769 |
#else |
#else |
770 |
xinit.cpu_flags = 0; |
xinit.cpu_flags = 0; |
771 |
#endif |
#endif |
772 |
|
} |
773 |
else |
else { |
774 |
xinit.cpu_flags = XVID_CPU_FORCE; |
xinit.cpu_flags = XVID_CPU_FORCE; |
775 |
|
} |
776 |
|
|
777 |
xvid_init(NULL, 0, &xinit, NULL); |
xvid_init(NULL, 0, &xinit, NULL); |
778 |
|
|
792 |
xparam.rc_averaging_period = 100; |
xparam.rc_averaging_period = 100; |
793 |
xparam.rc_buffer = 10; |
xparam.rc_buffer = 10; |
794 |
xparam.rc_bitrate = ARG_BITRATE*1000; |
xparam.rc_bitrate = ARG_BITRATE*1000; |
795 |
xparam.min_quantizer = 1; |
xparam.min_quantizer = ARG_MINQUANT; |
796 |
xparam.max_quantizer = 31; |
xparam.max_quantizer = ARG_MAXQUANT; |
797 |
xparam.max_key_interval = (int)ARG_FRAMERATE*10; |
xparam.max_key_interval = (int)ARG_FRAMERATE*10; |
798 |
|
|
799 |
/* I use a small value here, since will not encode whole movies, but short clips */ |
/* I use a small value here, since will not encode whole movies, but short clips */ |
804 |
return xerr; |
return xerr; |
805 |
} |
} |
806 |
|
|
807 |
int enc_stop() |
static int enc_stop() |
808 |
{ int xerr; |
{ |
809 |
|
int xerr; |
810 |
|
|
811 |
xerr = xvid_encore(enc_handle, XVID_ENC_DESTROY, NULL, NULL); |
xerr = xvid_encore(enc_handle, XVID_ENC_DESTROY, NULL, NULL); |
812 |
return xerr; |
return xerr; |
813 |
|
|
814 |
} |
} |
815 |
|
|
816 |
int enc_main(unsigned char* image, unsigned char* bitstream, int *streamlength, int* frametype) |
static int enc_main(unsigned char* image, unsigned char* bitstream, |
817 |
{ int xerr; |
int *streamlength, int* frametype) |
818 |
|
{ |
819 |
|
int xerr; |
820 |
|
|
821 |
XVID_ENC_FRAME xframe; |
XVID_ENC_FRAME xframe; |
822 |
XVID_ENC_STATS xstats; |
XVID_ENC_STATS xstats; |
837 |
|
|
838 |
xerr = xvid_encore(enc_handle, XVID_ENC_ENCODE, &xframe, &xstats); |
xerr = xvid_encore(enc_handle, XVID_ENC_ENCODE, &xframe, &xstats); |
839 |
|
|
840 |
/* enc_result->is_key_frame = xframe.intra; |
/* |
841 |
enc_result->quantizer = xframe.quant; |
* This is statictical data, e.g. for 2-pass. If you are not |
842 |
enc_result->total_bits = xframe.length * 8; |
* interested in any of this, you can use NULL instead of &xstats |
|
enc_result->motion_bits = xstats.hlength * 8; |
|
|
enc_result->texture_bits = enc_result->total_bits - enc_result->motion_bits; |
|
|
*/ |
|
|
|
|
|
/* This is statictical data, e.g. for 2-pass. |
|
|
If you are not interested in any of this, you can use NULL instead of &xstats |
|
843 |
*/ |
*/ |
844 |
*frametype = xframe.intra; |
*frametype = xframe.intra; |
845 |
*streamlength = xframe.length; |
*streamlength = xframe.length; |
847 |
return xerr; |
return xerr; |
848 |
} |
} |
849 |
|
|
850 |
|
/***************************************************************************** |
851 |
|
* Routines for decoding: init encoder, frame step, release encoder |
852 |
|
****************************************************************************/ |
853 |
|
|
854 |
/*********************************************************************/ |
/* init decoder before first run */ |
855 |
/* Routines for decoding: init encoder, frame step, release encoder */ |
static int dec_init(int use_assembler) |
|
/*********************************************************************/ |
|
|
|
|
|
int dec_init(int use_assembler) /* init decoder before first run */ |
|
856 |
{ |
{ |
857 |
int xerr; |
int xerr; |
858 |
|
|
880 |
return xerr; |
return xerr; |
881 |
} |
} |
882 |
|
|
883 |
int dec_main(unsigned char *m4v_buffer, unsigned char *out_buffer, int m4v_size) |
/* decode one frame */ |
884 |
{ /* decode one frame */ |
static int dec_main(unsigned char *m4v_buffer, unsigned char *out_buffer, |
885 |
|
int m4v_size) |
886 |
|
{ |
887 |
int xerr; |
int xerr; |
888 |
XVID_DEC_FRAME xframe; |
XVID_DEC_FRAME xframe; |
889 |
|
|
898 |
return xerr; |
return xerr; |
899 |
} |
} |
900 |
|
|
901 |
int dec_stop() /* close decoder to release resources */ |
/* close decoder to release resources */ |
902 |
|
static int dec_stop() |
903 |
{ |
{ |
904 |
int xerr; |
int xerr; |
905 |
xerr = xvid_decore(dec_handle, XVID_DEC_DESTROY, NULL, NULL); |
xerr = xvid_decore(dec_handle, XVID_DEC_DESTROY, NULL, NULL); |
907 |
return xerr; |
return xerr; |
908 |
} |
} |
909 |
|
|
910 |
|
/* EOF */ |
|
/*********************************************************************/ |
|
|
/* Main program */ |
|
|
/*********************************************************************/ |
|
|
|
|
|
int main(int argc, char *argv[]) |
|
|
{ |
|
|
unsigned char *divx_buffer = NULL; |
|
|
unsigned char *in_buffer = NULL; |
|
|
unsigned char *out_buffer = NULL; |
|
|
|
|
|
double enctime,dectime; |
|
|
double totalenctime=0.; |
|
|
double totaldectime=0.; |
|
|
|
|
|
long totalsize=0; |
|
|
int status; |
|
|
|
|
|
int m4v_size; |
|
|
int frame_type[ABS_MAXFRAMENR]; |
|
|
int Iframes=0, Pframes=0, use_assembler=0; |
|
|
double framepsnr[ABS_MAXFRAMENR]; |
|
|
|
|
|
double Ipsnr=0.,Imaxpsnr=0.,Iminpsnr=999.,Ivarpsnr=0.; |
|
|
double Ppsnr=0.,Pmaxpsnr=0.,Pminpsnr=999.,Pvarpsnr=0.; |
|
|
double Bpsnr=0.,Bmaxpsnr=0.,Bminpsnr=999.,Bvarpsnr=0.; |
|
|
|
|
|
char filename[256]; |
|
|
|
|
|
FILE *filehandle; |
|
|
|
|
|
/* read YUV in pgm format from stdin */ |
|
|
if (!pgmflag) |
|
|
{ |
|
|
pgmflag = 1; |
|
|
|
|
|
if (argc==2 && !strcmp(argv[1],"-asm")) |
|
|
use_assembler = 1; |
|
|
if (argc>=3) |
|
|
{ XDIM = atoi(argv[1]); |
|
|
YDIM = atoi(argv[2]); |
|
|
if ( (XDIM <= 0) || (XDIM >= 2048) || (YDIM <=0) || (YDIM >= 2048) ) |
|
|
{ fprintf(stderr,"Wrong frames size %d %d, trying PGM \n",XDIM, YDIM); |
|
|
} |
|
|
else |
|
|
{ |
|
|
YDIM = YDIM*3/2; /* for YUV */ |
|
|
pgmflag = 0; |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
if (pgmflag) |
|
|
{ if (read_pgmheader(stdin)) |
|
|
{ |
|
|
printf("Wrong input format, I want YUV encapsulated in PGM\n"); |
|
|
return 1; |
|
|
} |
|
|
} |
|
|
if (argc>=4) |
|
|
{ ARG_QUALITY = atoi(argv[3]); |
|
|
if ( (ARG_QUALITY < 0) || (ARG_QUALITY > 6) ) |
|
|
{ fprintf(stderr,"Wrong Quality\n"); return -1; } |
|
|
else |
|
|
printf("Quality %d\n",ARG_QUALITY); |
|
|
} |
|
|
if (argc>=5) |
|
|
{ ARG_BITRATE = atoi(argv[4]); |
|
|
if ( (ARG_BITRATE <= 0) ) |
|
|
{ fprintf(stderr,"Wrong Bitrate\n"); return -1; } |
|
|
if ( (ARG_BITRATE <= 32) ) |
|
|
{ ARG_QUANTI = ARG_BITRATE; |
|
|
ARG_BITRATE=0; |
|
|
printf("Quantizer %d\n",ARG_QUANTI); |
|
|
} |
|
|
else |
|
|
printf("Bitrate %d kbps\n",ARG_BITRATE); |
|
|
} |
|
|
if (argc>=6) |
|
|
{ ARG_FRAMERATE = (float)atof(argv[5]); |
|
|
if ( (ARG_FRAMERATE <= 0) ) |
|
|
{ fprintf(stderr,"Wrong Fraterate %s \n",argv[5]); return -1; } |
|
|
printf("Framerate %6.3f fps\n",ARG_FRAMERATE); |
|
|
} |
|
|
|
|
|
if (argc>=7) |
|
|
{ ARG_MAXFRAMENR = atoi(argv[6]); |
|
|
if ( (ARG_MAXFRAMENR <= 0) ) |
|
|
{ fprintf(stderr,"Wrong number of frames\n"); return -1; } |
|
|
printf("max. Framenr. %d\n",ARG_MAXFRAMENR); |
|
|
} |
|
|
|
|
|
/* now we know the sizes, so allocate memory */ |
|
|
|
|
|
in_buffer = (unsigned char *) malloc(XDIM*YDIM); |
|
|
if (!in_buffer) |
|
|
goto free_all_memory; // goto is one of the most underestimated instructions in C !!! |
|
|
|
|
|
divx_buffer = (unsigned char *) malloc(XDIM*YDIM*2); // this should really be enough memory! |
|
|
if (!divx_buffer) |
|
|
goto free_all_memory; |
|
|
|
|
|
YDIM = YDIM*2/3; // PGM is YUV 4:2:0 format, so real image height is *2/3 of PGM picture |
|
|
|
|
|
out_buffer = (unsigned char *) malloc(XDIM*YDIM*4); |
|
|
if (!out_buffer) |
|
|
goto free_all_memory; |
|
|
|
|
|
|
|
|
/*********************************************************************/ |
|
|
/* XviD PART Start */ |
|
|
/*********************************************************************/ |
|
|
|
|
|
|
|
|
status = enc_init(use_assembler); |
|
|
if (status) |
|
|
{ |
|
|
printf("Encore INIT problem, return value %d\n", status); |
|
|
goto release_all; |
|
|
} |
|
|
|
|
|
status = dec_init(use_assembler); |
|
|
if (status) |
|
|
{ |
|
|
printf("Decore INIT problem, return value %d\n", status); |
|
|
goto release_all; |
|
|
} |
|
|
|
|
|
|
|
|
/*********************************************************************/ |
|
|
/* Main loop */ |
|
|
/*********************************************************************/ |
|
|
|
|
|
do |
|
|
{ |
|
|
if (pgmflag) |
|
|
status = read_pgmdata(stdin, in_buffer); // read PGM data (YUV-format) |
|
|
else |
|
|
status = read_yuvdata(stdin, in_buffer); // read raw data (YUV-format) |
|
|
|
|
|
if (status) |
|
|
{ |
|
|
// Couldn't read image, most likely end-of-file |
|
|
continue; |
|
|
} |
|
|
|
|
|
|
|
|
if (save_ref_flag) |
|
|
{ |
|
|
sprintf(filename, "%s%05d.pgm", filepath, filenr); |
|
|
write_pgm(filename,in_buffer); |
|
|
} |
|
|
|
|
|
|
|
|
/*********************************************************************/ |
|
|
/* analyse this frame before encoding */ |
|
|
/*********************************************************************/ |
|
|
|
|
|
// nothing is done here at the moment, but you could e.g. create |
|
|
// histograms or measure entropy or apply preprocessing filters... |
|
|
|
|
|
/*********************************************************************/ |
|
|
/* encode and decode this frame */ |
|
|
/*********************************************************************/ |
|
|
|
|
|
enctime = -msecond(); |
|
|
status = enc_main(in_buffer, divx_buffer, &m4v_size, &frame_type[filenr]); |
|
|
enctime += msecond(); |
|
|
|
|
|
totalenctime += enctime; |
|
|
totalsize += m4v_size; |
|
|
|
|
|
printf("Frame %5d: intra %d, enctime =%6.1f ms length=%7d bytes ", |
|
|
filenr, frame_type[filenr], enctime*1000, m4v_size); |
|
|
|
|
|
if (save_m4v_flag) |
|
|
{ |
|
|
sprintf(filename, "%sframe%05d.m4v", filepath, filenr); |
|
|
filehandle = fopen(filename, "wb"); |
|
|
fwrite(divx_buffer, m4v_size, 1, filehandle); |
|
|
fclose(filehandle); |
|
|
} |
|
|
|
|
|
dectime = -msecond(); |
|
|
status = dec_main(divx_buffer, out_buffer, m4v_size); |
|
|
dectime += msecond(); |
|
|
|
|
|
totaldectime += dectime; |
|
|
|
|
|
|
|
|
/*********************************************************************/ |
|
|
/* analyse the decoded frame and compare to original */ |
|
|
/*********************************************************************/ |
|
|
|
|
|
framepsnr[filenr] = PSNR(XDIM,YDIM, in_buffer, XDIM, out_buffer, XDIM ); |
|
|
|
|
|
printf("dectime =%6.1f ms PSNR %5.2f\n",dectime*1000, framepsnr[filenr]); |
|
|
|
|
|
if (save_dec_flag) |
|
|
{ |
|
|
sprintf(filename, "%sdec%05d.pgm", filepath, filenr); |
|
|
write_pgm(filename,out_buffer); |
|
|
} |
|
|
|
|
|
if (pgmflag) |
|
|
status = read_pgmheader(stdin); // because if this was the last PGM, stop now |
|
|
|
|
|
filenr++; |
|
|
|
|
|
} while ( (!status) && (filenr<ARG_MAXFRAMENR) ); |
|
|
|
|
|
|
|
|
|
|
|
/*********************************************************************/ |
|
|
/* calculate totals and averages for output, print results */ |
|
|
/*********************************************************************/ |
|
|
|
|
|
totalsize /= filenr; |
|
|
totalenctime /= filenr; |
|
|
totaldectime /= filenr; |
|
|
|
|
|
for (i=0;i<filenr;i++) |
|
|
{ |
|
|
switch (frame_type[i]) |
|
|
{ |
|
|
case 0: |
|
|
Pframes++; |
|
|
Ppsnr += framepsnr[i]; |
|
|
break; |
|
|
case 1: |
|
|
Iframes++; |
|
|
Ipsnr += framepsnr[i]; |
|
|
break; |
|
|
default: |
|
|
break; |
|
|
} |
|
|
} |
|
|
|
|
|
if (Pframes) |
|
|
Ppsnr /= Pframes; |
|
|
if (Iframes) |
|
|
Ipsnr /= Iframes; |
|
|
|
|
|
for (i=0;i<filenr;i++) // calculate statistics for every frametype: P,I (and B) |
|
|
{ |
|
|
switch (frame_type[i]) |
|
|
{ |
|
|
case 0: |
|
|
if (framepsnr[i] > Pmaxpsnr) |
|
|
Pmaxpsnr = framepsnr[i]; |
|
|
if (framepsnr[i] < Pminpsnr) |
|
|
Pminpsnr = framepsnr[i]; |
|
|
Pvarpsnr += (framepsnr[i] - Ppsnr)*(framepsnr[i] - Ppsnr) /Pframes; |
|
|
break; |
|
|
case 1: |
|
|
if (framepsnr[i] > Imaxpsnr) |
|
|
Imaxpsnr = framepsnr[i]; |
|
|
if (framepsnr[i] < Pminpsnr) |
|
|
Iminpsnr = framepsnr[i]; |
|
|
Ivarpsnr += (framepsnr[i] - Ipsnr)*(framepsnr[i] - Ipsnr) /Iframes; |
|
|
default: |
|
|
break; |
|
|
} |
|
|
} |
|
|
|
|
|
printf("Avg. Q%1d %2s ",ARG_QUALITY, (ARG_QUANTI ? " q" : "br")); |
|
|
printf("%04d ",MAX(ARG_QUANTI,ARG_BITRATE)); |
|
|
printf("( %.2f bpp) ", (double)ARG_BITRATE*1000/XDIM/YDIM/ARG_FRAMERATE); |
|
|
printf("size %6d ",totalsize); |
|
|
printf("( %4d kbps ",(int)(totalsize*8*ARG_FRAMERATE/1000)); |
|
|
printf("/ %.2f bpp) ",(double)totalsize*8/XDIM/YDIM); |
|
|
printf("enc: %6.1f fps, dec: %6.1f fps \n",1/totalenctime, 1/totaldectime); |
|
|
printf("PSNR P(%d): %5.2f ( %5.2f , %5.2f ; %5.4f ) ",Pframes,Ppsnr,Pminpsnr,Pmaxpsnr,sqrt(Pvarpsnr/filenr)); |
|
|
printf("I(%d): %5.2f ( %5.2f , %5.2f ; %5.4f ) ",Iframes,Ipsnr,Iminpsnr,Imaxpsnr,sqrt(Ivarpsnr/filenr)); |
|
|
printf("\n"); |
|
|
|
|
|
/*********************************************************************/ |
|
|
/* XviD PART Stop */ |
|
|
/*********************************************************************/ |
|
|
|
|
|
release_all: |
|
|
|
|
|
if (enc_handle) |
|
|
{ |
|
|
status = enc_stop(); |
|
|
if (status) |
|
|
printf("Encore RELEASE problem return value %d\n", status); |
|
|
} |
|
|
|
|
|
if (dec_handle) |
|
|
{ |
|
|
status = dec_stop(); |
|
|
if (status) |
|
|
printf("Decore RELEASE problem return value %d\n", status); |
|
|
} |
|
|
|
|
|
|
|
|
free_all_memory: |
|
|
free(out_buffer); |
|
|
free(divx_buffer); |
|
|
free(in_buffer); |
|
|
|
|
|
return 0; |
|
|
} |
|