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