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