26 |
/***************************************************************************** |
/***************************************************************************** |
27 |
* Application notes : |
* Application notes : |
28 |
* |
* |
29 |
* A sequence of raw YUV I420 pics or YUV I420 PGM file format is encoded |
* A sequence of YUV pics in PGM file format is encoded and decoded |
30 |
* The speed is measured and frames' PSNR are taken from core. |
* 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. |
* and maths-lib. |
46 |
|
|
47 |
#include "xvid.h" |
#include "xvid.h" |
48 |
|
|
|
|
|
49 |
/***************************************************************************** |
/***************************************************************************** |
50 |
* Quality presets |
* Quality presets |
51 |
****************************************************************************/ |
****************************************************************************/ |
|
static xvid_motion_t const motion_presets[] = { |
|
|
/* quality 0 */ |
|
|
0, |
|
|
|
|
|
/* quality 1 */ |
|
|
XVID_ME_ADVANCEDDIAMOND16, |
|
|
|
|
|
/* quality 2 */ |
|
|
XVID_ME_ADVANCEDDIAMOND16 | XVID_ME_HALFPELREFINE16, |
|
|
|
|
|
/* quality 3 */ |
|
|
XVID_ME_ADVANCEDDIAMOND16 | XVID_ME_HALFPELREFINE16 | |
|
|
XVID_ME_ADVANCEDDIAMOND8 | XVID_ME_HALFPELREFINE8, |
|
|
|
|
|
/* quality 4 */ |
|
|
XVID_ME_ADVANCEDDIAMOND16 | XVID_ME_HALFPELREFINE16 | |
|
|
XVID_ME_ADVANCEDDIAMOND8 | XVID_ME_HALFPELREFINE8 | |
|
|
XVID_ME_CHROMA16 | XVID_ME_CHROMA8, |
|
|
|
|
|
/* quality 5 */ |
|
|
XVID_ME_ADVANCEDDIAMOND16 | XVID_ME_HALFPELREFINE16 | |
|
|
XVID_ME_ADVANCEDDIAMOND8 | XVID_ME_HALFPELREFINE8 | |
|
|
XVID_ME_CHROMA16 | XVID_ME_CHROMA8, |
|
|
|
|
|
/* quality 6 */ |
|
|
XVID_ME_ADVANCEDDIAMOND16 | XVID_ME_HALFPELREFINE16 | XVID_ME_EXTSEARCH16 | |
|
|
XVID_ME_ADVANCEDDIAMOND8 | XVID_ME_HALFPELREFINE8 | XVID_ME_EXTSEARCH8 | |
|
|
XVID_ME_CHROMA16 | XVID_ME_CHROMA8 , |
|
52 |
|
|
53 |
|
static int const motion_presets[7] = { |
54 |
|
0, /* Q 0 */ |
55 |
|
PMV_EARLYSTOP16, /* Q 1 */ |
56 |
|
PMV_EARLYSTOP16, /* Q 2 */ |
57 |
|
PMV_EARLYSTOP16 | PMV_HALFPELREFINE16, /* Q 3 */ |
58 |
|
PMV_EARLYSTOP16 | PMV_HALFPELREFINE16, /* Q 4 */ |
59 |
|
PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 | PMV_EARLYSTOP8 | /* Q 5 */ |
60 |
|
PMV_HALFPELREFINE8, |
61 |
|
PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 | PMV_EXTSEARCH16 | /* Q 6 */ |
62 |
|
PMV_USESQUARES16 | PMV_EARLYSTOP8 | PMV_HALFPELREFINE8 |
63 |
}; |
}; |
|
#define ME_ELEMENTS (sizeof(motion_presets)/sizeof(motion_presets[0])) |
|
|
|
|
|
static xvid_vop_t const vop_presets[] = { |
|
|
/* quality 0 */ |
|
|
0, |
|
|
|
|
|
/* quality 1 */ |
|
|
XVID_VOP_DYNAMIC_BFRAMES, |
|
|
|
|
|
/* quality 2 */ |
|
|
XVID_VOP_DYNAMIC_BFRAMES | XVID_VOP_HALFPEL, |
|
|
|
|
|
/* quality 3 */ |
|
|
XVID_VOP_DYNAMIC_BFRAMES | XVID_VOP_HALFPEL | |
|
|
XVID_VOP_INTER4V, |
|
|
|
|
|
/* quality 4 */ |
|
|
XVID_VOP_DYNAMIC_BFRAMES | XVID_VOP_HALFPEL | |
|
|
XVID_VOP_INTER4V, |
|
|
|
|
|
/* quality 5 */ |
|
|
XVID_VOP_DYNAMIC_BFRAMES | XVID_VOP_HALFPEL | |
|
|
XVID_VOP_INTER4V | XVID_VOP_TRELLISQUANT, |
|
|
|
|
|
/* quality 6 */ |
|
|
XVID_VOP_DYNAMIC_BFRAMES | XVID_VOP_HALFPEL | |
|
|
XVID_VOP_INTER4V | XVID_VOP_TRELLISQUANT | |
|
|
XVID_VOP_HQACPRED, |
|
64 |
|
|
65 |
|
static int const general_presets[7] = { |
66 |
|
XVID_H263QUANT, /* Q 0 */ |
67 |
|
XVID_MPEGQUANT, /* Q 1 */ |
68 |
|
XVID_H263QUANT, /* Q 2 */ |
69 |
|
XVID_H263QUANT | XVID_HALFPEL, /* Q 3 */ |
70 |
|
XVID_H263QUANT | XVID_HALFPEL | XVID_INTER4V, /* Q 4 */ |
71 |
|
XVID_H263QUANT | XVID_HALFPEL | XVID_INTER4V, /* Q 5 */ |
72 |
|
XVID_H263QUANT | XVID_HALFPEL | XVID_INTER4V /* Q 6 */ |
73 |
}; |
}; |
74 |
#define VOP_ELEMENTS (sizeof(vop_presets)/sizeof(vop_presets[0])) |
|
75 |
|
|
76 |
/***************************************************************************** |
/***************************************************************************** |
77 |
* Command line global variables |
* Command line global variables |
78 |
****************************************************************************/ |
****************************************************************************/ |
79 |
|
|
|
#define MAX_ZONES 64 |
|
|
|
|
|
static xvid_enc_zone_t ZONES[MAX_ZONES]; |
|
|
static int NUM_ZONES = 0; |
|
|
|
|
80 |
/* Maximum number of frames to encode */ |
/* Maximum number of frames to encode */ |
81 |
#define ABS_MAXFRAMENR 9999 |
#define ABS_MAXFRAMENR 9999 |
82 |
|
|
83 |
static int ARG_STATS = 0; |
/* HINTMODEs */ |
84 |
static int ARG_DUMP = 0; |
#define HINT_MODE_NONE 0 |
85 |
static int ARG_LUMIMASKING = 0; |
#define HINT_MODE_GET 1 |
86 |
static int ARG_BITRATE = 0; |
#define HINT_MODE_SET 2 |
87 |
static int ARG_SINGLE = 0; |
#define HINT_FILE "hints.mv" |
88 |
static char *ARG_PASS1 = 0; |
|
89 |
static char *ARG_PASS2 = 0; |
static int ARG_BITRATE = 900; |
90 |
static int ARG_QUALITY = ME_ELEMENTS - 1; |
static int ARG_QUANTI = 0; |
91 |
|
static int ARG_QUALITY = 6; |
92 |
|
static int ARG_MINQUANT = 1; |
93 |
|
static int ARG_MAXQUANT = 31; |
94 |
static float ARG_FRAMERATE = 25.00f; |
static float ARG_FRAMERATE = 25.00f; |
95 |
static int ARG_MAXFRAMENR = ABS_MAXFRAMENR; |
static int ARG_MAXFRAMENR = ABS_MAXFRAMENR; |
96 |
static char *ARG_INPUTFILE = NULL; |
static char *ARG_INPUTFILE = NULL; |
97 |
static int ARG_INPUTTYPE = 0; |
static int ARG_INPUTTYPE = 0; |
98 |
static int ARG_SAVEMPEGSTREAM = 0; |
static int ARG_SAVEMPEGSTREAM = 0; |
99 |
static char *ARG_OUTPUTFILE = NULL; |
static char *ARG_OUTPUTFILE = NULL; |
100 |
|
static int ARG_HINTMODE = HINT_MODE_NONE; |
101 |
static int XDIM = 0; |
static int XDIM = 0; |
102 |
static int YDIM = 0; |
static int YDIM = 0; |
103 |
static int ARG_BQRATIO = 150; |
static int ARG_BQRATIO = 120; |
104 |
static int ARG_BQOFFSET = 100; |
static int ARG_BQOFFSET = 0; |
105 |
static int ARG_MAXBFRAMES = 0; |
static int ARG_MAXBFRAMES = 0; |
|
static int ARG_PACKED = 0; |
|
|
static int ARG_DEBUG = 0; |
|
|
|
|
106 |
#define IMAGE_SIZE(x,y) ((x)*(y)*3/2) |
#define IMAGE_SIZE(x,y) ((x)*(y)*3/2) |
107 |
|
|
108 |
#define MAX(A,B) ( ((A)>(B)) ? (A) : (B) ) |
#define MAX(A,B) ( ((A)>(B)) ? (A) : (B) ) |
109 |
#define SMALL_EPS (1e-10) |
#define SMALL_EPS 1e-10 |
110 |
|
|
111 |
#define SWAP(a) ( (((a)&0x000000ff)<<24) | (((a)&0x0000ff00)<<8) | \ |
#define SWAP(a) ( (((a)&0x000000ff)<<24) | (((a)&0x0000ff00)<<8) | \ |
112 |
(((a)&0x00ff0000)>>8) | (((a)&0xff000000)>>24) ) |
(((a)&0x00ff0000)>>8) | (((a)&0xff000000)>>24) ) |
115 |
* Nasty global vars ;-) |
* Nasty global vars ;-) |
116 |
***************************************************************************/ |
***************************************************************************/ |
117 |
|
|
118 |
static int i; |
static int i,filenr = 0; |
119 |
|
|
120 |
/* the path where to save output */ |
/* the path where to save output */ |
121 |
static char filepath[256] = "./"; |
static char filepath[256] = "./"; |
135 |
|
|
136 |
/* PGM related functions */ |
/* PGM related functions */ |
137 |
static int read_pgmheader(FILE * handle); |
static int read_pgmheader(FILE * handle); |
138 |
static int read_pgmdata(FILE * handle, |
static int read_pgmdata(FILE* handle, unsigned char *image); |
139 |
unsigned char *image); |
static int read_yuvdata(FILE* handle, unsigned char *image); |
|
static int read_yuvdata(FILE * handle, |
|
|
unsigned char *image); |
|
140 |
|
|
141 |
/* Encoder related functions */ |
/* Encoder related functions */ |
142 |
static int enc_init(int use_assembler); |
static int enc_init(int use_assembler); |
143 |
static int enc_stop(); |
static int enc_stop(); |
144 |
static int enc_main(unsigned char *image, |
static int enc_main(unsigned char* image, unsigned char* bitstream, |
145 |
unsigned char *bitstream, |
unsigned char *hints_buffer, |
146 |
int *key, |
long *streamlength, long* frametype, long *hints_size); |
|
int *stats_type, |
|
|
int *stats_quant, |
|
|
int *stats_length, |
|
|
int stats[3]); |
|
147 |
|
|
148 |
/***************************************************************************** |
/***************************************************************************** |
149 |
* Main function |
* Main function |
150 |
****************************************************************************/ |
****************************************************************************/ |
151 |
|
|
152 |
int |
int main(int argc, char *argv[]) |
|
main(int argc, |
|
|
char *argv[]) |
|
153 |
{ |
{ |
154 |
|
|
155 |
unsigned char *mp4_buffer = NULL; |
unsigned char *mp4_buffer = NULL; |
156 |
unsigned char *in_buffer = NULL; |
unsigned char *in_buffer = NULL; |
157 |
unsigned char *out_buffer = NULL; |
unsigned char *out_buffer = NULL; |
158 |
|
unsigned char *hints_buffer = NULL; |
159 |
|
|
160 |
double enctime; |
double enctime; |
161 |
double totalenctime = 0.; |
double totalenctime = 0.; |
|
float totalPSNR[3] = {0., 0., 0.}; |
|
162 |
|
|
163 |
int totalsize; |
long totalsize; |
164 |
int result; |
long hints_size; |
165 |
int m4v_size; |
int status; |
166 |
int key; |
long frame_type; |
167 |
int stats_type; |
long bigendian; |
|
int stats_quant; |
|
|
int stats_length; |
|
|
int use_assembler = 0; |
|
168 |
|
|
169 |
int input_num; |
long m4v_size; |
170 |
int output_num; |
int use_assembler=0; |
171 |
|
|
172 |
char filename[256]; |
char filename[256]; |
173 |
|
|
174 |
FILE *in_file = stdin; |
FILE *in_file = stdin; |
175 |
FILE *out_file = NULL; |
FILE *out_file = NULL; |
176 |
|
FILE *hints_file = NULL; |
177 |
|
|
178 |
printf("xvid_encraw - raw mpeg4 bitstream encoder "); |
printf("xvid_encraw - raw mpeg4 bitstream encoder "); |
179 |
printf("written by Christoph Lampert 2002-2003\n\n"); |
printf("written by Christoph Lampert 2002-2003\n\n"); |
180 |
|
|
|
/* Is there a dumb XviD coder ? */ |
|
|
if(ME_ELEMENTS != VOP_ELEMENTS) { |
|
|
fprintf(stderr, "Presets' arrays should have the same number of elements -- Please fill a bug to xvid-devel@xvid.org\n"); |
|
|
return(-1); |
|
|
} |
|
|
|
|
181 |
/***************************************************************************** |
/***************************************************************************** |
182 |
* Command line parsing |
* Command line parsing |
183 |
****************************************************************************/ |
****************************************************************************/ |
186 |
|
|
187 |
if (strcmp("-asm", argv[i]) == 0) { |
if (strcmp("-asm", argv[i]) == 0) { |
188 |
use_assembler = 1; |
use_assembler = 1; |
189 |
} else if (strcmp("-w", argv[i]) == 0 && i < argc - 1) { |
} |
190 |
|
else if (strcmp("-w", argv[i]) == 0 && i < argc - 1 ) { |
191 |
i++; |
i++; |
192 |
XDIM = atoi(argv[i]); |
XDIM = atoi(argv[i]); |
193 |
} else if (strcmp("-h", argv[i]) == 0 && i < argc - 1) { |
} |
194 |
|
else if (strcmp("-h", argv[i]) == 0 && i < argc - 1 ) { |
195 |
i++; |
i++; |
196 |
YDIM = atoi(argv[i]); |
YDIM = atoi(argv[i]); |
197 |
} else if (strcmp("-bitrate", argv[i]) == 0 && i < argc - 1) { |
} |
198 |
|
else if (strcmp("-b", argv[i]) == 0 && i < argc - 1 ) { |
199 |
i++; |
i++; |
200 |
ARG_BITRATE = atoi(argv[i]); |
ARG_BITRATE = atoi(argv[i]); |
201 |
} else if (strcmp("-single", argv[i]) == 0) { |
} |
202 |
ARG_SINGLE = 1; |
else if (strcmp("-bn", argv[i]) == 0 && i < argc - 1 ) { |
|
} else if (strcmp("-pass1", argv[i]) == 0 && i < argc - 1) { |
|
|
i++; |
|
|
ARG_PASS1 = argv[i]; |
|
|
} else if (strcmp("-pass2", argv[i]) == 0 && i < argc - 1) { |
|
|
i++; |
|
|
ARG_PASS2 = argv[i]; |
|
|
} else if (strcmp("-max_bframes", argv[i]) == 0 && i < argc - 1) { |
|
203 |
i++; |
i++; |
204 |
ARG_MAXBFRAMES = atoi(argv[i]); |
ARG_MAXBFRAMES = atoi(argv[i]); |
205 |
} else if (strcmp("-packed", argv[i]) == 0) { |
} |
206 |
ARG_PACKED = 1; |
else if (strcmp("-bqr", argv[i]) == 0 && i < argc - 1 ) { |
|
} else if (strcmp("-bquant_ratio", argv[i]) == 0 && i < argc - 1) { |
|
207 |
i++; |
i++; |
208 |
ARG_BQRATIO = atoi(argv[i]); |
ARG_BQRATIO = atoi(argv[i]); |
209 |
} else if (strcmp("-bquant_offset", argv[i]) == 0 && i < argc - 1) { |
} |
210 |
|
else if (strcmp("-bqo", argv[i]) == 0 && i < argc - 1 ) { |
211 |
i++; |
i++; |
212 |
ARG_BQOFFSET = atoi(argv[i]); |
ARG_BQOFFSET = atoi(argv[i]); |
|
|
|
|
} else if ((strcmp("-zq", argv[i]) == 0 || strcmp("-zw", argv[i]) == 0) && i < argc - 2) { |
|
|
|
|
|
if (NUM_ZONES >= MAX_ZONES) { |
|
|
fprintf(stderr,"warning: too many zones; zone ignored\n"); |
|
|
continue; |
|
213 |
} |
} |
214 |
ZONES[NUM_ZONES].mode = strcmp("-zq", argv[i])==0 ? XVID_ZONE_QUANT : XVID_ZONE_WEIGHT; |
else if (strcmp("-q", argv[i]) == 0 && i < argc - 1 ) { |
|
i++; |
|
|
ZONES[NUM_ZONES].frame = atoi(argv[i]); |
|
|
i++; |
|
|
ZONES[NUM_ZONES].increment = (int)(atof(argv[i]) * 100); |
|
|
ZONES[NUM_ZONES].base = 100; |
|
|
NUM_ZONES++; |
|
|
|
|
|
} else if (strcmp("-quality", argv[i]) == 0 && i < argc - 1) { |
|
215 |
i++; |
i++; |
216 |
ARG_QUALITY = atoi(argv[i]); |
ARG_QUALITY = atoi(argv[i]); |
217 |
} else if (strcmp("-framerate", argv[i]) == 0 && i < argc - 1) { |
} |
218 |
|
else if (strcmp("-f", argv[i]) == 0 && i < argc - 1 ) { |
219 |
i++; |
i++; |
220 |
ARG_FRAMERATE = (float) atof(argv[i]); |
ARG_FRAMERATE = (float) atof(argv[i]); |
221 |
} else if (strcmp("-i", argv[i]) == 0 && i < argc - 1) { |
} |
222 |
|
else if (strcmp("-i", argv[i]) == 0 && i < argc - 1 ) { |
223 |
i++; |
i++; |
224 |
ARG_INPUTFILE = argv[i]; |
ARG_INPUTFILE = argv[i]; |
225 |
} else if (strcmp("-stats", argv[i]) == 0) { |
} |
226 |
ARG_STATS = 1; |
else if (strcmp("-t", argv[i]) == 0 && i < argc - 1 ) { |
|
} else if (strcmp("-dump", argv[i]) == 0) { |
|
|
ARG_DUMP = 1; |
|
|
} else if (strcmp("-lumimasking", argv[i]) == 0) { |
|
|
ARG_LUMIMASKING = 1; |
|
|
} else if (strcmp("-type", argv[i]) == 0 && i < argc - 1) { |
|
227 |
i++; |
i++; |
228 |
ARG_INPUTTYPE = atoi(argv[i]); |
ARG_INPUTTYPE = atoi(argv[i]); |
229 |
} else if (strcmp("-nframes", argv[i]) == 0 && i < argc - 1) { |
} |
230 |
|
else if(strcmp("-n", argv[i]) == 0 && i < argc - 1 ) { |
231 |
i++; |
i++; |
232 |
ARG_MAXFRAMENR = atoi(argv[i]); |
ARG_MAXFRAMENR = atoi(argv[i]); |
233 |
} else if (strcmp("-save", argv[i]) == 0) { |
} |
234 |
ARG_SAVEMPEGSTREAM = 1; |
else if (strcmp("-quant", argv[i]) == 0 && i < argc - 1 ) { |
235 |
} else if (strcmp("-debug", argv[i]) == 0) { |
i++; |
236 |
ARG_DEBUG = 1; |
ARG_QUANTI = atoi(argv[i]); |
237 |
} else if (strcmp("-o", argv[i]) == 0 && i < argc - 1) { |
} |
238 |
|
else if (strcmp("-m", argv[i]) == 0 && i < argc - 1 ) { |
239 |
|
i++; |
240 |
|
ARG_SAVEMPEGSTREAM = atoi(argv[i]); |
241 |
|
} |
242 |
|
else if (strcmp("-mv", argv[i]) == 0 && i < argc - 1 ) { |
243 |
|
i++; |
244 |
|
ARG_HINTMODE = atoi(argv[i]); |
245 |
|
} |
246 |
|
else if (strcmp("-o", argv[i]) == 0 && i < argc - 1 ) { |
247 |
i++; |
i++; |
248 |
ARG_OUTPUTFILE = argv[i]; |
ARG_OUTPUTFILE = argv[i]; |
249 |
} else if (strcmp("-help", argv[i])) { |
} |
250 |
|
else if (strcmp("-help", argv[i])) { |
251 |
usage(); |
usage(); |
252 |
return (0); |
return (0); |
253 |
} else { |
} |
254 |
|
else { |
255 |
usage(); |
usage(); |
256 |
exit(-1); |
exit(-1); |
257 |
} |
} |
263 |
****************************************************************************/ |
****************************************************************************/ |
264 |
|
|
265 |
if (XDIM <= 0 || XDIM >= 2048 || YDIM <= 0 || YDIM >= 2048) { |
if (XDIM <= 0 || XDIM >= 2048 || YDIM <= 0 || YDIM >= 2048) { |
266 |
fprintf(stderr, |
fprintf(stderr, "Trying to retreive width and height from PGM header\n"); |
|
"Trying to retreive width and height from PGM header\n"); |
|
267 |
ARG_INPUTTYPE = 1; /* pgm */ |
ARG_INPUTTYPE = 1; /* pgm */ |
268 |
} |
} |
269 |
|
|
270 |
if (ARG_QUALITY < 0 ) { |
if ( ARG_QUALITY < 0 || ARG_QUALITY > 6) { |
271 |
ARG_QUALITY = 0; |
fprintf(stderr,"Wrong Quality\n"); |
272 |
} else if (ARG_QUALITY >= ME_ELEMENTS) { |
return -1; |
273 |
ARG_QUALITY = ME_ELEMENTS - 1; |
} |
274 |
|
|
275 |
|
if ( ARG_BITRATE <= 0 && ARG_QUANTI == 0) { |
276 |
|
fprintf(stderr,"Wrong Bitrate\n"); |
277 |
|
return -1; |
278 |
} |
} |
279 |
|
|
280 |
if (ARG_FRAMERATE <= 0) { |
if (ARG_FRAMERATE <= 0) { |
281 |
fprintf(stderr, "Wrong Framerate %s \n", argv[5]); |
fprintf(stderr, "Wrong Framerate %s \n", argv[5]); |
282 |
return (-1); |
return -1; |
283 |
} |
} |
284 |
|
|
285 |
if (ARG_MAXFRAMENR <= 0) { |
if (ARG_MAXFRAMENR <= 0) { |
286 |
fprintf(stderr, "Wrong number of frames\n"); |
fprintf(stderr, "Wrong number of frames\n"); |
287 |
return (-1); |
return -1; |
288 |
|
} |
289 |
|
|
290 |
|
if ( ARG_HINTMODE != HINT_MODE_NONE && |
291 |
|
ARG_HINTMODE != HINT_MODE_GET && |
292 |
|
ARG_HINTMODE != HINT_MODE_SET) |
293 |
|
ARG_HINTMODE = HINT_MODE_NONE; |
294 |
|
|
295 |
|
if( ARG_HINTMODE != HINT_MODE_NONE) { |
296 |
|
char *rights = "rb"; |
297 |
|
|
298 |
|
/* |
299 |
|
* If we are getting hints from core, we will have to write them to |
300 |
|
* hint file |
301 |
|
*/ |
302 |
|
if(ARG_HINTMODE == HINT_MODE_GET) |
303 |
|
rights = "w+b"; |
304 |
|
|
305 |
|
/* Open the hint file */ |
306 |
|
hints_file = fopen(HINT_FILE, rights); |
307 |
|
if(hints_file == NULL) { |
308 |
|
fprintf(stderr, "Error opening input file %s\n", HINT_FILE); |
309 |
|
return -1; |
310 |
|
} |
311 |
|
|
312 |
|
/* Allocate hint memory space, we will be using rawhints */ |
313 |
|
/* NB : Hope 1Mb is enough */ |
314 |
|
if((hints_buffer = malloc(1024*1024)) == NULL) { |
315 |
|
fprintf(stderr, "Memory allocation error\n"); |
316 |
|
return -1; |
317 |
|
} |
318 |
|
|
319 |
} |
} |
320 |
|
|
321 |
if (ARG_INPUTFILE == NULL || strcmp(ARG_INPUTFILE, "stdin") == 0) { |
if (ARG_INPUTFILE == NULL || strcmp(ARG_INPUTFILE, "stdin") == 0) { |
322 |
in_file = stdin; |
in_file = stdin; |
323 |
} else { |
} |
324 |
|
else { |
325 |
|
|
326 |
in_file = fopen(ARG_INPUTFILE, "rb"); |
in_file = fopen(ARG_INPUTFILE, "rb"); |
327 |
if (in_file == NULL) { |
if (in_file == NULL) { |
328 |
fprintf(stderr, "Error opening input file %s\n", ARG_INPUTFILE); |
fprintf(stderr, "Error opening input file %s\n", ARG_INPUTFILE); |
329 |
return (-1); |
return -1; |
330 |
} |
} |
331 |
} |
} |
332 |
|
|
333 |
if (ARG_INPUTTYPE) { |
if (ARG_INPUTTYPE) { |
334 |
if (read_pgmheader(in_file)) { |
if (read_pgmheader(in_file)) { |
335 |
fprintf(stderr, |
fprintf(stderr, "Wrong input format, I want YUV encapsulated in PGM\n"); |
336 |
"Wrong input format, I want YUV encapsulated in PGM\n"); |
return -1; |
|
return (-1); |
|
337 |
} |
} |
338 |
} |
} |
339 |
|
|
352 |
****************************************************************************/ |
****************************************************************************/ |
353 |
|
|
354 |
|
|
355 |
result = enc_init(use_assembler); |
status = enc_init(use_assembler); |
356 |
if (result) { |
if (status) |
357 |
fprintf(stderr, "Encore INIT problem, return value %d\n", result); |
{ |
358 |
|
fprintf(stderr, "Encore INIT problem, return value %d\n", status); |
359 |
goto release_all; |
goto release_all; |
360 |
} |
} |
361 |
|
|
370 |
goto release_all; |
goto release_all; |
371 |
} |
} |
372 |
|
|
373 |
} else { |
} |
374 |
|
else { |
375 |
out_file = NULL; |
out_file = NULL; |
376 |
} |
} |
377 |
|
|
381 |
|
|
382 |
totalsize = 0; |
totalsize = 0; |
383 |
|
|
|
result = 0; |
|
|
|
|
|
input_num = 0; /* input frame counter */ |
|
|
output_num = 0; /* output frame counter */ |
|
|
|
|
384 |
do { |
do { |
385 |
|
|
386 |
char *type; |
if (ARG_INPUTTYPE) |
387 |
int sse[3]; |
status = read_pgmdata(in_file, in_buffer); /* read PGM data (YUV-format) */ |
388 |
|
else |
389 |
|
status = read_yuvdata(in_file, in_buffer); /* read raw data (YUV-format) */ |
390 |
|
|
391 |
if (input_num >= ARG_MAXFRAMENR) { |
if (status) |
392 |
result = 1; |
{ |
393 |
|
/* Couldn't read image, most likely end-of-file */ |
394 |
|
continue; |
395 |
} |
} |
396 |
|
|
397 |
if (!result) { |
/***************************************************************************** |
398 |
if (ARG_INPUTTYPE) { |
* Read hints from file |
399 |
/* read PGM data (YUV-format) */ |
****************************************************************************/ |
400 |
result = read_pgmdata(in_file, in_buffer); |
|
401 |
} else { |
if(ARG_HINTMODE == HINT_MODE_SET) { |
402 |
/* read raw data (YUV-format) */ |
fread(&hints_size, 1, sizeof(long), hints_file); |
403 |
result = read_yuvdata(in_file, in_buffer); |
hints_size = (!bigendian)?SWAP(hints_size):hints_size; |
404 |
} |
fread(hints_buffer, 1, hints_size, hints_file); |
405 |
} |
} |
406 |
|
|
407 |
/***************************************************************************** |
/***************************************************************************** |
409 |
****************************************************************************/ |
****************************************************************************/ |
410 |
|
|
411 |
enctime = msecond(); |
enctime = msecond(); |
412 |
m4v_size = |
status = enc_main(in_buffer, mp4_buffer, hints_buffer, |
413 |
enc_main(!result ? in_buffer : 0, mp4_buffer, &key, &stats_type, |
&m4v_size, &frame_type, &hints_size); |
|
&stats_quant, &stats_length, sse); |
|
414 |
enctime = msecond() - enctime; |
enctime = msecond() - enctime; |
415 |
|
|
416 |
/* Write the Frame statistics */ |
/* if it's a not coded VOP (aka NVOP) then we write nothing */ |
417 |
|
if(frame_type == 5) goto next_frame; |
|
printf("%5d: key=%i, time= %6.0f, length= %7d", !result ? input_num : -1, |
|
|
key, (float) enctime, (int) m4v_size); |
|
|
|
|
|
if (stats_type > 0) { /* !XVID_TYPE_NOTHING */ |
|
|
|
|
|
switch (stats_type) { |
|
|
case XVID_TYPE_IVOP: |
|
|
type = "I"; |
|
|
break; |
|
|
case XVID_TYPE_PVOP: |
|
|
type = "P"; |
|
|
break; |
|
|
case XVID_TYPE_BVOP: |
|
|
type = "B"; |
|
|
break; |
|
|
case XVID_TYPE_SVOP: |
|
|
type = "S"; |
|
|
break; |
|
|
default: |
|
|
type = "U"; |
|
|
break; |
|
|
} |
|
418 |
|
|
419 |
printf(" | type=%s, quant= %2d, length= %7d", type, stats_quant, |
{ |
420 |
stats_length); |
char *type[] = {"P", "I", "B", "S", "Packed", "N", "Unknown"}; |
|
|
|
|
#define SSE2PSNR(sse, width, height) ((!(sse))?0.0f : 48.131f - 10*(float)log10((float)(sse)/((float)((width)*(height))))) |
|
|
|
|
|
if (ARG_STATS) { |
|
|
printf(", psnr y = %2.2f, psnr u = %2.2f, psnr v = %2.2f", |
|
|
SSE2PSNR(sse[0], XDIM, YDIM), SSE2PSNR(sse[1], XDIM / 2, |
|
|
YDIM / 2), |
|
|
SSE2PSNR(sse[2], XDIM / 2, YDIM / 2)); |
|
|
|
|
|
totalPSNR[0] += SSE2PSNR(sse[0], XDIM, YDIM); |
|
|
totalPSNR[1] += SSE2PSNR(sse[1], XDIM/2, YDIM/2); |
|
|
totalPSNR[2] += SSE2PSNR(sse[2], XDIM/2, YDIM/2); |
|
|
} |
|
421 |
|
|
422 |
} |
if(frame_type<0 || frame_type>5) frame_type = 6; |
|
#undef SSE2PSNR |
|
423 |
|
|
424 |
printf("\n"); |
printf("Frame %5d: type = %s, enctime(ms) =%6.1f, length(bytes) =%7d\n", |
425 |
|
(int)filenr, type[frame_type], (float)enctime, (int)m4v_size); |
426 |
|
|
|
if (m4v_size < 0) { |
|
|
break; |
|
427 |
} |
} |
428 |
|
|
429 |
/* Update encoding time stats */ |
/* Update encoding time stats */ |
431 |
totalsize += m4v_size; |
totalsize += m4v_size; |
432 |
|
|
433 |
/***************************************************************************** |
/***************************************************************************** |
434 |
|
* Save hints to file |
435 |
|
****************************************************************************/ |
436 |
|
|
437 |
|
if(ARG_HINTMODE == HINT_MODE_GET) { |
438 |
|
hints_size = (!bigendian)?SWAP(hints_size):hints_size; |
439 |
|
fwrite(&hints_size, 1, sizeof(long), hints_file); |
440 |
|
hints_size = (!bigendian)?SWAP(hints_size):hints_size; |
441 |
|
fwrite(hints_buffer, 1, hints_size, hints_file); |
442 |
|
} |
443 |
|
|
444 |
|
/***************************************************************************** |
445 |
* Save stream to file |
* Save stream to file |
446 |
****************************************************************************/ |
****************************************************************************/ |
447 |
|
|
448 |
if (m4v_size > 0 && ARG_SAVEMPEGSTREAM) { |
if (ARG_SAVEMPEGSTREAM) |
449 |
|
{ |
450 |
/* Save single files */ |
/* Save single files */ |
451 |
if (out_file == NULL) { |
if (out_file == NULL) { |
452 |
sprintf(filename, "%sframe%05d.m4v", filepath, output_num); |
sprintf(filename, "%sframe%05d.m4v", filepath, filenr); |
453 |
out_file = fopen(filename, "wb"); |
out_file = fopen(filename, "wb"); |
454 |
fwrite(mp4_buffer, m4v_size, 1, out_file); |
fwrite(mp4_buffer, m4v_size, 1, out_file); |
455 |
fclose(out_file); |
fclose(out_file); |
456 |
out_file = NULL; |
out_file = NULL; |
457 |
output_num++; |
} |
458 |
} else { |
else { |
459 |
|
|
460 |
/* Write mp4 data */ |
/* Write mp4 data */ |
461 |
fwrite(mp4_buffer, 1, m4v_size, out_file); |
fwrite(mp4_buffer, 1, m4v_size, out_file); |
463 |
} |
} |
464 |
} |
} |
465 |
|
|
466 |
input_num++; |
next_frame: |
|
|
|
467 |
/* Read the header if it's pgm stream */ |
/* Read the header if it's pgm stream */ |
468 |
if (!result && ARG_INPUTTYPE) |
if (ARG_INPUTTYPE) |
469 |
result = read_pgmheader(in_file); |
status = read_pgmheader(in_file); |
470 |
|
|
471 |
|
if(frame_type != 5) filenr++; |
472 |
|
|
473 |
} while (1); |
} while ( (!status) && (filenr<ARG_MAXFRAMENR) ); |
474 |
|
|
475 |
|
|
476 |
|
|
478 |
* Calculate totals and averages for output, print results |
* Calculate totals and averages for output, print results |
479 |
****************************************************************************/ |
****************************************************************************/ |
480 |
|
|
481 |
printf("Tot: enctime(ms) =%7.2f, length(bytes) = %7d\n", |
totalsize /= filenr; |
482 |
totalenctime, (int) totalsize); |
totalenctime /= filenr; |
483 |
|
|
484 |
if (input_num > 0) { |
printf("Avg: enctime(ms) =%7.2f, fps =%7.2f, length(bytes) = %7d\n", |
|
totalsize /= input_num; |
|
|
totalenctime /= input_num; |
|
|
totalPSNR[0] /= input_num; |
|
|
totalPSNR[1] /= input_num; |
|
|
totalPSNR[2] /= input_num; |
|
|
} else { |
|
|
totalsize = -1; |
|
|
totalenctime = -1; |
|
|
} |
|
|
|
|
|
printf("Avg: enctime(ms) =%7.2f, fps =%7.2f, length(bytes) = %7d", |
|
485 |
totalenctime, 1000 / totalenctime, (int) totalsize); |
totalenctime, 1000 / totalenctime, (int) totalsize); |
|
if (ARG_STATS) { |
|
|
printf(", psnr y = %2.2f, psnr u = %2.2f, psnr v = %2.2f", |
|
|
totalPSNR[0],totalPSNR[1],totalPSNR[2]); |
|
|
} |
|
|
printf("\n"); |
|
|
|
|
486 |
|
|
487 |
/***************************************************************************** |
/***************************************************************************** |
488 |
* XviD PART Stop |
* XviD PART Stop |
490 |
|
|
491 |
release_all: |
release_all: |
492 |
|
|
493 |
if (enc_handle) { |
if (enc_handle) |
494 |
result = enc_stop(); |
{ |
495 |
if (result) |
status = enc_stop(); |
496 |
fprintf(stderr, "Encore RELEASE problem return value %d\n", |
if (status) |
497 |
result); |
fprintf(stderr, "Encore RELEASE problem return value %d\n", status); |
498 |
} |
} |
499 |
|
|
500 |
if (in_file) |
if (in_file) |
501 |
fclose(in_file); |
fclose(in_file); |
502 |
if (out_file) |
if (out_file) |
503 |
fclose(out_file); |
fclose(out_file); |
504 |
|
if(hints_file) |
505 |
|
fclose(hints_file); |
506 |
|
|
507 |
free_all_memory: |
free_all_memory: |
508 |
free(out_buffer); |
free(out_buffer); |
509 |
free(mp4_buffer); |
free(mp4_buffer); |
510 |
free(in_buffer); |
free(in_buffer); |
511 |
|
if(hints_buffer) free(hints_buffer); |
512 |
|
|
513 |
return (0); |
return 0; |
514 |
|
|
515 |
} |
} |
516 |
|
|
524 |
*****************************************************************************/ |
*****************************************************************************/ |
525 |
|
|
526 |
/* Return time elapsed time in miliseconds since the program started */ |
/* Return time elapsed time in miliseconds since the program started */ |
527 |
static double |
static double msecond() |
|
msecond() |
|
528 |
{ |
{ |
529 |
#ifndef WIN32 |
#ifndef WIN32 |
530 |
struct timeval tv; |
struct timeval tv; |
|
|
|
531 |
gettimeofday(&tv, 0); |
gettimeofday(&tv, 0); |
532 |
return (tv.tv_sec * 1.0e3 + tv.tv_usec * 1.0e-3); |
return tv.tv_sec*1.0e3 + tv.tv_usec * 1.0e-3; |
533 |
#else |
#else |
534 |
clock_t clk; |
clock_t clk; |
|
|
|
535 |
clk = clock(); |
clk = clock(); |
536 |
return (clk * 1000 / CLOCKS_PER_SEC); |
return clk * 1000 / CLOCKS_PER_SEC; |
537 |
#endif |
#endif |
538 |
} |
} |
539 |
|
|
541 |
* Usage message |
* Usage message |
542 |
*****************************************************************************/ |
*****************************************************************************/ |
543 |
|
|
544 |
static void |
static void usage() |
|
usage() |
|
545 |
{ |
{ |
546 |
fprintf(stderr, "Usage : xvid_stat [OPTIONS]\n\n"); |
|
547 |
fprintf(stderr, "Input options:\n"); |
fprintf(stderr, "Usage : xvid_stat [OPTIONS]\n"); |
548 |
fprintf(stderr, " -i string : input filename (default=stdin)\n"); |
fprintf(stderr, "Options :\n"); |
|
fprintf(stderr, " -type integer: input data type (yuv=0, pgm=1)\n"); |
|
549 |
fprintf(stderr, " -w integer: frame width ([1.2048])\n"); |
fprintf(stderr, " -w integer: frame width ([1.2048])\n"); |
550 |
fprintf(stderr, " -h integer: frame height ([1.2048])\n"); |
fprintf(stderr, " -h integer: frame height ([1.2048])\n"); |
551 |
fprintf(stderr, " -nframes integer: number of frames to encode\n"); |
fprintf(stderr, " -b integer : target bitrate (>0 | default=900kbit)\n"); |
552 |
fprintf(stderr, "\n"); |
fprintf(stderr, " -b integer : target bitrate (>0 | default=900kbit)\n"); |
553 |
fprintf(stderr, "Output options:\n"); |
fprintf(stderr, " -bn integer : max bframes (default=0)\n"); |
554 |
fprintf(stderr, " -dump : save decoder output\n"); |
fprintf(stderr, " -bqr integer : bframe quantizer ratio (default=150)\n"); |
555 |
fprintf(stderr, " -save : save mpeg4 raw stream\n"); |
fprintf(stderr, " -bqo integer : bframe quantizer offset (default=100)\n"); |
556 |
fprintf(stderr, " -o string: output filename\n"); |
fprintf(stderr, " -f float : target framerate (>0)\n"); |
557 |
fprintf(stderr, "\n"); |
fprintf(stderr, " -i string : input filename (default=stdin)\n"); |
558 |
fprintf(stderr, "BFrames options:\n"); |
fprintf(stderr, " -t integer : input data type (yuv=0, pgm=1)\n"); |
559 |
fprintf(stderr, " -max_bframes integer: max bframes (default=0)\n"); |
fprintf(stderr, " -n integer : number of frames to encode\n"); |
560 |
fprintf(stderr, " -bquant_ratio integer: bframe quantizer ratio (default=150)\n"); |
fprintf(stderr, " -q integer : quality ([0..5])\n"); |
561 |
fprintf(stderr, " -bquant_offset integer: bframe quantizer offset (default=100)\n"); |
fprintf(stderr, " -d boolean : save decoder output (0 False*, !=0 True)\n"); |
562 |
fprintf(stderr, "\n"); |
fprintf(stderr, " -m boolean : save mpeg4 raw stream (0 False*, !=0 True)\n"); |
563 |
fprintf(stderr, "Rate control options:\n"); |
fprintf(stderr, " -o string : output container filename (only usefull when -m 1 is used) :\n"); |
564 |
fprintf(stderr, " -framerate float : target framerate (>0 | default=25.0)\n"); |
fprintf(stderr, " When this option is not used : one file per encoded frame\n"); |
565 |
fprintf(stderr, " -bitrate integer : target bitrate\n"); |
fprintf(stderr, " When this option is used : save to 'string' (default=stream.m4v)\n"); |
566 |
fprintf(stderr, " -single : single pass mode\n"); |
fprintf(stderr, " -mt integer : output type (m4v=0, mp4u=1)\n"); |
567 |
fprintf(stderr, " -pass1 filename : twopass mode (first pass)\n"); |
fprintf(stderr, " -mv integer : Use motion vector hints (no hints=0, get hints=1, set hints=2)\n"); |
|
fprintf(stderr, " -pass2 filename : twopass mode (2nd pass)\n"); |
|
|
fprintf(stderr, " -zq starting_frame float : bitrate zone; quant\n"); |
|
|
fprintf(stderr, " -zw starting_frame float : bitrate zone; weight\n"); |
|
|
fprintf(stderr, "\n"); |
|
|
fprintf(stderr, "Other options\n"); |
|
|
fprintf(stderr, " -asm : use assembly optmized code\n"); |
|
|
fprintf(stderr, " -quality integer: quality ([0..%d])\n", ME_ELEMENTS - 1); |
|
|
fprintf(stderr, " -packed : packed mode\n"); |
|
|
fprintf(stderr, " -lumimasking : use lumimasking algorithm\n"); |
|
|
fprintf(stderr, " -stats : print stats about encoded frames\n"); |
|
|
fprintf(stderr, " -debug : print all MB dquants\n"); |
|
568 |
fprintf(stderr, " -help : prints this help message\n"); |
fprintf(stderr, " -help : prints this help message\n"); |
569 |
fprintf(stderr, "\n"); |
fprintf(stderr, " -quant integer : fixed quantizer (disables -b setting)\n"); |
570 |
fprintf(stderr, "NB: You can define %d zones repeating the -z[qw] option as many times as needed.\n", MAX_ZONES); |
fprintf(stderr, " (* means default)\n"); |
571 |
fprintf(stderr, "\n"); |
|
572 |
} |
} |
573 |
|
|
574 |
/***************************************************************************** |
/***************************************************************************** |
579 |
* |
* |
580 |
*****************************************************************************/ |
*****************************************************************************/ |
581 |
|
|
582 |
static int |
static int read_pgmheader(FILE* handle) |
|
read_pgmheader(FILE * handle) |
|
583 |
{ |
{ |
584 |
int bytes, xsize, ysize, depth; |
int bytes, xsize, ysize, depth; |
585 |
char dummy[2]; |
char dummy[2]; |
587 |
bytes = fread(dummy, 1, 2, handle); |
bytes = fread(dummy, 1, 2, handle); |
588 |
|
|
589 |
if ((bytes < 2) || (dummy[0] != 'P') || (dummy[1] != '5')) |
if ((bytes < 2) || (dummy[0] != 'P') || (dummy[1] != '5')) |
590 |
return (1); |
return 1; |
591 |
|
|
592 |
fscanf(handle, "%d %d %d", &xsize, &ysize, &depth); |
fscanf(handle, "%d %d %d", &xsize, &ysize, &depth); |
593 |
if ((xsize > 1440) || (ysize > 2880) || (depth != 255)) { |
if ( (xsize > 1440) || (ysize > 2880 ) || (depth != 255) ) |
594 |
|
{ |
595 |
fprintf(stderr, "%d %d %d\n", xsize, ysize, depth); |
fprintf(stderr, "%d %d %d\n", xsize, ysize, depth); |
596 |
return (2); |
return 2; |
597 |
} |
} |
598 |
if ((XDIM == 0) || (YDIM == 0)) { |
if ( (XDIM==0) || (YDIM==0) ) |
599 |
|
{ |
600 |
XDIM = xsize; |
XDIM = xsize; |
601 |
YDIM = ysize * 2 / 3; |
YDIM = ysize * 2 / 3; |
602 |
} |
} |
603 |
|
|
604 |
return (0); |
return 0; |
605 |
} |
} |
606 |
|
|
607 |
static int |
static int read_pgmdata(FILE* handle, unsigned char *image) |
|
read_pgmdata(FILE * handle, |
|
|
unsigned char *image) |
|
608 |
{ |
{ |
609 |
int i; |
int i; |
610 |
char dummy; |
char dummy; |
616 |
/* read Y component of picture */ |
/* read Y component of picture */ |
617 |
fread(y, 1, XDIM * YDIM, handle); |
fread(y, 1, XDIM * YDIM, handle); |
618 |
|
|
619 |
for (i = 0; i < YDIM / 2; i++) { |
for (i=0;i<YDIM/2;i++) |
620 |
|
{ |
621 |
/* read U */ |
/* read U */ |
622 |
fread(u, 1, XDIM / 2, handle); |
fread(u, 1, XDIM / 2, handle); |
623 |
|
|
632 |
/* I don't know why, but this seems needed */ |
/* I don't know why, but this seems needed */ |
633 |
fread(&dummy, 1, 1, handle); |
fread(&dummy, 1, 1, handle); |
634 |
|
|
635 |
return (0); |
return 0; |
636 |
} |
} |
637 |
|
|
638 |
static int |
static int read_yuvdata(FILE* handle, unsigned char *image) |
|
read_yuvdata(FILE * handle, |
|
|
unsigned char *image) |
|
639 |
{ |
{ |
640 |
|
|
641 |
if (fread(image, 1, IMAGE_SIZE(XDIM, YDIM), handle) != |
if (fread(image, 1, IMAGE_SIZE(XDIM, YDIM), handle) != (unsigned int)IMAGE_SIZE(XDIM, YDIM)) |
642 |
(unsigned int) IMAGE_SIZE(XDIM, YDIM)) |
return 1; |
|
return (1); |
|
643 |
else |
else |
644 |
return (0); |
return 0; |
645 |
} |
} |
646 |
|
|
647 |
/***************************************************************************** |
/***************************************************************************** |
648 |
* Routines for encoding: init encoder, frame step, release encoder |
* Routines for encoding: init encoder, frame step, release encoder |
649 |
****************************************************************************/ |
****************************************************************************/ |
650 |
|
|
|
/* sample plugin */ |
|
|
|
|
|
int |
|
|
rawenc_debug(void *handle, |
|
|
int opt, |
|
|
void *param1, |
|
|
void *param2) |
|
|
{ |
|
|
switch (opt) { |
|
|
case XVID_PLG_INFO: |
|
|
{ |
|
|
xvid_plg_info_t *info = (xvid_plg_info_t *) param1; |
|
|
|
|
|
info->flags = XVID_REQDQUANTS; |
|
|
return 0; |
|
|
} |
|
|
|
|
|
case XVID_PLG_CREATE: |
|
|
case XVID_PLG_DESTROY: |
|
|
case XVID_PLG_BEFORE: |
|
|
return 0; |
|
|
|
|
|
case XVID_PLG_AFTER: |
|
|
{ |
|
|
xvid_plg_data_t *data = (xvid_plg_data_t *) param1; |
|
|
int i, j; |
|
|
|
|
|
printf("---[ frame: %5i quant: %2i length: %6i ]---\n", |
|
|
data->frame_num, data->quant, data->length); |
|
|
for (j = 0; j < data->mb_height; j++) { |
|
|
for (i = 0; i < data->mb_width; i++) |
|
|
printf("%2i ", data->dquant[j * data->dquant_stride + i]); |
|
|
printf("\n"); |
|
|
} |
|
|
|
|
|
return 0; |
|
|
} |
|
|
} |
|
|
|
|
|
return XVID_ERR_FAIL; |
|
|
} |
|
|
|
|
|
|
|
651 |
#define FRAMERATE_INCR 1001 |
#define FRAMERATE_INCR 1001 |
652 |
|
|
|
|
|
653 |
/* Initialize encoder for first use, pass all needed parameters to the codec */ |
/* Initialize encoder for first use, pass all needed parameters to the codec */ |
654 |
static int |
static int enc_init(int use_assembler) |
|
enc_init(int use_assembler) |
|
655 |
{ |
{ |
656 |
int xerr; |
int xerr; |
|
//xvid_plugin_cbr_t cbr; |
|
|
xvid_plugin_single_t single; |
|
|
xvid_plugin_2pass1_t rc2pass1; |
|
|
xvid_plugin_2pass2_t rc2pass2; |
|
|
//xvid_plugin_fixed_t rcfixed; |
|
|
xvid_enc_plugin_t plugins[7]; |
|
|
xvid_gbl_init_t xvid_gbl_init; |
|
|
xvid_enc_create_t xvid_enc_create; |
|
|
|
|
|
/*------------------------------------------------------------------------ |
|
|
* XviD core initialization |
|
|
*----------------------------------------------------------------------*/ |
|
|
|
|
|
/* Set version -- version checking will done by xvidcore */ |
|
|
memset(&xvid_gbl_init, 0, sizeof(xvid_gbl_init)); |
|
|
xvid_gbl_init.version = XVID_VERSION; |
|
657 |
|
|
658 |
|
XVID_INIT_PARAM xinit; |
659 |
|
XVID_ENC_PARAM xparam; |
660 |
|
|
|
/* Do we have to enable ASM optimizations ? */ |
|
661 |
if (use_assembler) { |
if (use_assembler) { |
662 |
|
|
663 |
#ifdef ARCH_IS_IA64 |
#ifdef ARCH_IS_IA64 |
664 |
xvid_gbl_init.cpu_flags = XVID_CPU_FORCE | XVID_CPU_IA64; |
xinit.cpu_flags = XVID_CPU_FORCE | XVID_CPU_IA64; |
665 |
#else |
#else |
666 |
xvid_gbl_init.cpu_flags = 0; |
xinit.cpu_flags = 0; |
667 |
#endif |
#endif |
|
} else { |
|
|
xvid_gbl_init.cpu_flags = XVID_CPU_FORCE; |
|
668 |
} |
} |
669 |
|
else { |
670 |
/* Initialize XviD core -- Should be done once per __process__ */ |
xinit.cpu_flags = XVID_CPU_FORCE; |
|
xvid_global(NULL, XVID_GBL_INIT, &xvid_gbl_init, NULL); |
|
|
|
|
|
/*------------------------------------------------------------------------ |
|
|
* XviD encoder initialization |
|
|
*----------------------------------------------------------------------*/ |
|
|
|
|
|
/* Version again */ |
|
|
memset(&xvid_enc_create, 0, sizeof(xvid_enc_create)); |
|
|
xvid_enc_create.version = XVID_VERSION; |
|
|
|
|
|
/* Width and Height of input frames */ |
|
|
xvid_enc_create.width = XDIM; |
|
|
xvid_enc_create.height = YDIM; |
|
|
|
|
|
/* init plugins */ |
|
|
xvid_enc_create.zones = ZONES; |
|
|
xvid_enc_create.num_zones = NUM_ZONES; |
|
|
|
|
|
xvid_enc_create.plugins = plugins; |
|
|
xvid_enc_create.num_plugins = 0; |
|
|
|
|
|
if (ARG_SINGLE) { |
|
|
memset(&single, 0, sizeof(xvid_plugin_single_t)); |
|
|
single.version = XVID_VERSION; |
|
|
single.bitrate = ARG_BITRATE; |
|
|
|
|
|
plugins[xvid_enc_create.num_plugins].func = xvid_plugin_single; |
|
|
plugins[xvid_enc_create.num_plugins].param = &single; |
|
|
xvid_enc_create.num_plugins++; |
|
671 |
} |
} |
672 |
|
|
673 |
if (ARG_PASS2) { |
xvid_init(NULL, 0, &xinit, NULL); |
|
memset(&rc2pass2, 0, sizeof(xvid_plugin_2pass2_t)); |
|
|
rc2pass2.version = XVID_VERSION; |
|
|
rc2pass2.filename = ARG_PASS2; |
|
|
rc2pass2.bitrate = ARG_BITRATE; |
|
674 |
|
|
675 |
plugins[xvid_enc_create.num_plugins].func = xvid_plugin_2pass2; |
xparam.width = XDIM; |
676 |
plugins[xvid_enc_create.num_plugins].param = &rc2pass2; |
xparam.height = YDIM; |
677 |
xvid_enc_create.num_plugins++; |
if ((ARG_FRAMERATE - (int)ARG_FRAMERATE) < SMALL_EPS) |
678 |
|
{ |
679 |
|
xparam.fincr = 1; |
680 |
|
xparam.fbase = (int)ARG_FRAMERATE; |
681 |
} |
} |
682 |
|
else |
683 |
if (ARG_PASS1) { |
{ |
684 |
memset(&rc2pass1, 0, sizeof(xvid_plugin_2pass1_t)); |
xparam.fincr = FRAMERATE_INCR; |
685 |
rc2pass1.version = XVID_VERSION; |
xparam.fbase = (int)(FRAMERATE_INCR * ARG_FRAMERATE); |
|
rc2pass1.filename = ARG_PASS1; |
|
|
|
|
|
plugins[xvid_enc_create.num_plugins].func = xvid_plugin_2pass1; |
|
|
plugins[xvid_enc_create.num_plugins].param = &rc2pass1; |
|
|
xvid_enc_create.num_plugins++; |
|
686 |
} |
} |
687 |
|
xparam.rc_reaction_delay_factor = 16; |
688 |
|
xparam.rc_averaging_period = 100; |
689 |
|
xparam.rc_buffer = 10; |
690 |
|
xparam.rc_bitrate = ARG_BITRATE*1000; |
691 |
|
xparam.min_quantizer = ARG_MINQUANT; |
692 |
|
xparam.max_quantizer = ARG_MAXQUANT; |
693 |
|
xparam.max_key_interval = (int)ARG_FRAMERATE*10; |
694 |
|
xparam.bquant_ratio = ARG_BQRATIO; |
695 |
|
xparam.bquant_offset = ARG_BQOFFSET; |
696 |
|
xparam.max_bframes = ARG_MAXBFRAMES; |
697 |
|
xparam.frame_drop_ratio = 0; |
698 |
|
xparam.global = 0; |
699 |
|
|
700 |
if (ARG_LUMIMASKING) { |
/* I use a small value here, since will not encode whole movies, but short clips */ |
|
plugins[xvid_enc_create.num_plugins].func = xvid_plugin_lumimasking; |
|
|
plugins[xvid_enc_create.num_plugins].param = NULL; |
|
|
xvid_enc_create.num_plugins++; |
|
|
} |
|
701 |
|
|
702 |
if (ARG_DUMP) { |
xerr = xvid_encore(NULL, XVID_ENC_CREATE, &xparam, NULL); |
703 |
plugins[xvid_enc_create.num_plugins].func = xvid_plugin_dump; |
enc_handle=xparam.handle; |
|
plugins[xvid_enc_create.num_plugins].param = NULL; |
|
|
xvid_enc_create.num_plugins++; |
|
|
} |
|
704 |
|
|
705 |
if (ARG_DEBUG) { |
return xerr; |
|
plugins[xvid_enc_create.num_plugins].func = rawenc_debug; |
|
|
plugins[xvid_enc_create.num_plugins].param = NULL; |
|
|
xvid_enc_create.num_plugins++; |
|
706 |
} |
} |
707 |
|
|
708 |
/* No fancy thread tests */ |
static int enc_stop() |
709 |
xvid_enc_create.num_threads = 0; |
{ |
710 |
|
int xerr; |
711 |
|
|
712 |
|
xerr = xvid_encore(enc_handle, XVID_ENC_DESTROY, NULL, NULL); |
713 |
|
return xerr; |
714 |
|
|
|
/* Frame rate - Do some quick float fps = fincr/fbase hack */ |
|
|
if ((ARG_FRAMERATE - (int) ARG_FRAMERATE) < SMALL_EPS) { |
|
|
xvid_enc_create.fincr = 1; |
|
|
xvid_enc_create.fbase = (int) ARG_FRAMERATE; |
|
|
} else { |
|
|
xvid_enc_create.fincr = FRAMERATE_INCR; |
|
|
xvid_enc_create.fbase = (int) (FRAMERATE_INCR * ARG_FRAMERATE); |
|
715 |
} |
} |
716 |
|
|
717 |
/* Maximum key frame interval */ |
static int enc_main(unsigned char* image, unsigned char* bitstream, |
718 |
xvid_enc_create.max_key_interval = (int) ARG_FRAMERATE *10; |
unsigned char* hints_buffer, |
719 |
|
long *streamlength, long *frametype, long *hints_size) |
720 |
|
{ |
721 |
|
int xerr; |
722 |
|
|
723 |
/* Bframes settings */ |
XVID_ENC_FRAME xframe; |
724 |
xvid_enc_create.max_bframes = ARG_MAXBFRAMES; |
XVID_ENC_STATS xstats; |
|
xvid_enc_create.bquant_ratio = ARG_BQRATIO; |
|
|
xvid_enc_create.bquant_offset = ARG_BQOFFSET; |
|
725 |
|
|
726 |
/* Dropping ratio frame -- we don't need that */ |
xframe.bitstream = bitstream; |
727 |
xvid_enc_create.frame_drop_ratio = 0; |
xframe.length = -1; /* this is written by the routine */ |
728 |
|
|
729 |
/* Global encoder options */ |
xframe.image = image; |
730 |
xvid_enc_create.global = 0; |
xframe.colorspace = XVID_CSP_I420; /* defined in <xvid.h> */ |
731 |
|
|
732 |
if (ARG_PACKED) |
xframe.intra = -1; /* let the codec decide between I-frame (1) and P-frame (0) */ |
|
xvid_enc_create.global |=XVID_GLOBAL_PACKED; |
|
733 |
|
|
734 |
if (ARG_STATS) |
xframe.quant = ARG_QUANTI; /* is quant != 0, use a fixed quant (and ignore bitrate) */ |
735 |
xvid_enc_create.global |=XVID_GLOBAL_EXTRASTATS_ENABLE; |
xframe.bquant = 0; |
736 |
|
|
737 |
/* I use a small value here, since will not encode whole movies, but short clips */ |
xframe.motion = motion_presets[ARG_QUALITY]; |
738 |
xerr = xvid_encore(NULL, XVID_ENC_CREATE, &xvid_enc_create, NULL); |
xframe.general = general_presets[ARG_QUALITY]; |
739 |
|
xframe.quant_intra_matrix = xframe.quant_inter_matrix = NULL; |
740 |
|
xframe.stride = XDIM; |
741 |
|
|
742 |
/* Retrieve the encoder instance from the structure */ |
xframe.hint.hintstream = hints_buffer; |
|
enc_handle = xvid_enc_create.handle; |
|
743 |
|
|
744 |
return (xerr); |
if(ARG_HINTMODE == HINT_MODE_SET) { |
745 |
|
xframe.hint.hintlength = *hints_size; |
746 |
|
xframe.hint.rawhints = 0; |
747 |
|
xframe.general |= XVID_HINTEDME_SET; |
748 |
} |
} |
749 |
|
|
750 |
static int |
if(ARG_HINTMODE == HINT_MODE_GET) { |
751 |
enc_stop() |
xframe.hint.rawhints = 0; |
752 |
{ |
xframe.general |= XVID_HINTEDME_GET; |
753 |
int xerr; |
} |
754 |
|
|
755 |
/* Destroy the encoder instance */ |
xerr = xvid_encore(enc_handle, XVID_ENC_ENCODE, &xframe, &xstats); |
|
xerr = xvid_encore(enc_handle, XVID_ENC_DESTROY, NULL, NULL); |
|
756 |
|
|
757 |
return (xerr); |
if(ARG_HINTMODE == HINT_MODE_GET) |
758 |
} |
*hints_size = xframe.hint.hintlength; |
759 |
|
|
760 |
static int |
/* |
761 |
enc_main(unsigned char *image, |
* This is statictical data, e.g. for 2-pass. If you are not |
762 |
unsigned char *bitstream, |
* interested in any of this, you can use NULL instead of &xstats |
763 |
int *key, |
*/ |
764 |
int *stats_type, |
*frametype = xframe.intra; |
765 |
int *stats_quant, |
*streamlength = xframe.length; |
|
int *stats_length, |
|
|
int sse[3]) |
|
|
{ |
|
|
int ret; |
|
|
|
|
|
xvid_enc_frame_t xvid_enc_frame; |
|
|
xvid_enc_stats_t xvid_enc_stats; |
|
|
|
|
|
/* Version for the frame and the stats */ |
|
|
memset(&xvid_enc_frame, 0, sizeof(xvid_enc_frame)); |
|
|
xvid_enc_frame.version = XVID_VERSION; |
|
|
|
|
|
memset(&xvid_enc_stats, 0, sizeof(xvid_enc_stats)); |
|
|
xvid_enc_stats.version = XVID_VERSION; |
|
|
|
|
|
/* Bind output buffer */ |
|
|
xvid_enc_frame.bitstream = bitstream; |
|
|
xvid_enc_frame.length = -1; |
|
|
|
|
|
/* Initialize input image fields */ |
|
|
if (image) { |
|
|
xvid_enc_frame.input.plane[0] = image; |
|
|
xvid_enc_frame.input.csp = XVID_CSP_I420; |
|
|
xvid_enc_frame.input.stride[0] = XDIM; |
|
|
} else { |
|
|
xvid_enc_frame.input.csp = XVID_CSP_NULL; |
|
|
} |
|
|
|
|
|
/* Set up core's general features */ |
|
|
xvid_enc_frame.vol_flags = 0; |
|
|
if (ARG_STATS) |
|
|
xvid_enc_frame.vol_flags |= XVID_VOL_EXTRASTATS; |
|
|
|
|
|
/* Set up core's general features */ |
|
|
xvid_enc_frame.vop_flags = vop_presets[ARG_QUALITY]; |
|
|
|
|
|
/* Frame type -- let core decide for us */ |
|
|
xvid_enc_frame.type = XVID_TYPE_AUTO; |
|
|
|
|
|
/* Force the right quantizer -- It is internally managed by RC plugins */ |
|
|
xvid_enc_frame.quant = 0; |
|
|
|
|
|
/* Set up motion estimation flags */ |
|
|
xvid_enc_frame.motion = motion_presets[ARG_QUALITY]; |
|
|
|
|
|
/* We don't use special matrices */ |
|
|
xvid_enc_frame.quant_intra_matrix = NULL; |
|
|
xvid_enc_frame.quant_inter_matrix = NULL; |
|
|
|
|
|
/* Encode the frame */ |
|
|
ret = |
|
|
xvid_encore(enc_handle, XVID_ENC_ENCODE, &xvid_enc_frame, |
|
|
&xvid_enc_stats); |
|
|
|
|
|
*key = (xvid_enc_frame.out_flags & XVID_KEYFRAME); |
|
|
*stats_type = xvid_enc_stats.type; |
|
|
*stats_quant = xvid_enc_stats.quant; |
|
|
*stats_length = xvid_enc_stats.length; |
|
|
sse[0] = xvid_enc_stats.sse_y; |
|
|
sse[1] = xvid_enc_stats.sse_u; |
|
|
sse[2] = xvid_enc_stats.sse_v; |
|
766 |
|
|
767 |
return (ret); |
return xerr; |
768 |
} |
} |