Parent Directory | Revision Log
Revision 1.11.2.2 - (view) (download)
1 : | edgomez | 1.2 | /***************************************************************************** |
2 : | chl | 1.1 | * |
3 : | edgomez | 1.2 | * XVID MPEG-4 VIDEO CODEC |
4 : | * - Console based test application - | ||
5 : | chl | 1.1 | * |
6 : | edgomez | 1.10 | * Copyright(C) 2002-2003 Christoph Lampert |
7 : | chl | 1.1 | * |
8 : | edgomez | 1.2 | * This program is free software; you can redistribute it and/or modify |
9 : | * it under the terms of the GNU General Public License as published by | ||
10 : | * the Free Software Foundation; either version 2 of the License, or | ||
11 : | * (at your option) any later version. | ||
12 : | chl | 1.1 | * |
13 : | edgomez | 1.2 | * This program is distributed in the hope that it will be useful, |
14 : | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
15 : | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
16 : | * GNU General Public License for more details. | ||
17 : | chl | 1.1 | * |
18 : | edgomez | 1.2 | * You should have received a copy of the GNU General Public License |
19 : | * along with this program; if not, write to the Free Software | ||
20 : | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
21 : | * | ||
22 : | edgomez | 1.11.2.1 | * $Id: xvid_encraw.c,v 1.11 2003/02/16 05:11:39 suxen_drol Exp $ |
23 : | edgomez | 1.2 | * |
24 : | ****************************************************************************/ | ||
25 : | chl | 1.1 | |
26 : | edgomez | 1.2 | /***************************************************************************** |
27 : | * Application notes : | ||
28 : | chl | 1.1 | * |
29 : | edgomez | 1.2 | * A sequence of YUV pics in PGM file format is encoded and decoded |
30 : | * The speed is measured and PSNR of decoded picture is calculated. | ||
31 : | chl | 1.1 | * |
32 : | * The program is plain C and needs no libraries except for libxvidcore, | ||
33 : | edgomez | 1.2 | * and maths-lib. |
34 : | chl | 1.1 | * |
35 : | ************************************************************************/ | ||
36 : | |||
37 : | #include <stdio.h> | ||
38 : | #include <stdlib.h> | ||
39 : | edgomez | 1.4 | #include <string.h> |
40 : | edgomez | 1.2 | #include <math.h> |
41 : | edgomez | 1.10 | #ifndef WIN32 |
42 : | edgomez | 1.2 | #include <sys/time.h> |
43 : | #else | ||
44 : | #include <time.h> | ||
45 : | #endif | ||
46 : | chl | 1.1 | |
47 : | edgomez | 1.2 | #include "xvid.h" |
48 : | chl | 1.1 | |
49 : | edgomez | 1.2 | /***************************************************************************** |
50 : | * Quality presets | ||
51 : | ****************************************************************************/ | ||
52 : | |||
53 : | edgomez | 1.11.2.1 | static xvid_motion_t const motion_presets[] = { |
54 : | 0, | ||
55 : | PMV_HALFPELREFINE16, | ||
56 : | PMV_HALFPELREFINE16, | ||
57 : | PMV_HALFPELREFINE16 | PMV_HALFPELREFINE8, | ||
58 : | PMV_HALFPELREFINE16 | PMV_HALFPELREFINE8 | PMV_EXTSEARCH16 | PMV_USESQUARES16, | ||
59 : | edgomez | 1.11.2.2 | PMV_HALFPELREFINE16 | PMV_HALFPELREFINE8 | PMV_EXTSEARCH16 | PMV_USESQUARES16 | PMV_CHROMA16 | PMV_CHROMA8, |
60 : | edgomez | 1.2 | }; |
61 : | |||
62 : | edgomez | 1.11.2.1 | static xvid_vol_t const vol_presets[] = { |
63 : | XVID_MPEGQUANT, | ||
64 : | 0, | ||
65 : | 0, | ||
66 : | XVID_QUARTERPEL, | ||
67 : | XVID_QUARTERPEL | XVID_GMC, | ||
68 : | XVID_QUARTERPEL | XVID_GMC | ||
69 : | }; | ||
70 : | |||
71 : | static xvid_vop_t const vop_presets[] = { | ||
72 : | XVID_DYNAMIC_BFRAMES, | ||
73 : | XVID_DYNAMIC_BFRAMES, | ||
74 : | XVID_DYNAMIC_BFRAMES | XVID_HALFPEL, | ||
75 : | XVID_DYNAMIC_BFRAMES | XVID_HALFPEL | XVID_INTER4V, | ||
76 : | XVID_DYNAMIC_BFRAMES | XVID_HALFPEL | XVID_INTER4V | XVID_HQACPRED, | ||
77 : | XVID_DYNAMIC_BFRAMES | XVID_HALFPEL | XVID_INTER4V | XVID_HQACPRED | XVID_MODEDECISION_BITS | ||
78 : | edgomez | 1.2 | }; |
79 : | chl | 1.1 | |
80 : | edgomez | 1.2 | /***************************************************************************** |
81 : | * Command line global variables | ||
82 : | ****************************************************************************/ | ||
83 : | |||
84 : | /* Maximum number of frames to encode */ | ||
85 : | #define ABS_MAXFRAMENR 9999 | ||
86 : | |||
87 : | static int ARG_BITRATE = 900; | ||
88 : | static int ARG_QUANTI = 0; | ||
89 : | edgomez | 1.11.2.1 | static int ARG_QUALITY = 5; |
90 : | edgomez | 1.2 | static int ARG_MINQUANT = 1; |
91 : | static int ARG_MAXQUANT = 31; | ||
92 : | static float ARG_FRAMERATE = 25.00f; | ||
93 : | static int ARG_MAXFRAMENR = ABS_MAXFRAMENR; | ||
94 : | static char *ARG_INPUTFILE = NULL; | ||
95 : | static int ARG_INPUTTYPE = 0; | ||
96 : | static int ARG_SAVEMPEGSTREAM = 0; | ||
97 : | static char *ARG_OUTPUTFILE = NULL; | ||
98 : | static int XDIM = 0; | ||
99 : | static int YDIM = 0; | ||
100 : | edgomez | 1.11.2.1 | static int ARG_BQRATIO = 150; |
101 : | static int ARG_BQOFFSET = 100; | ||
102 : | edgomez | 1.10 | static int ARG_MAXBFRAMES = 0; |
103 : | edgomez | 1.2 | #define IMAGE_SIZE(x,y) ((x)*(y)*3/2) |
104 : | |||
105 : | #define MAX(A,B) ( ((A)>(B)) ? (A) : (B) ) | ||
106 : | edgomez | 1.11.2.1 | #define SMALL_EPS (1e-10) |
107 : | edgomez | 1.2 | |
108 : | #define SWAP(a) ( (((a)&0x000000ff)<<24) | (((a)&0x0000ff00)<<8) | \ | ||
109 : | (((a)&0x00ff0000)>>8) | (((a)&0xff000000)>>24) ) | ||
110 : | |||
111 : | /**************************************************************************** | ||
112 : | * Nasty global vars ;-) | ||
113 : | ***************************************************************************/ | ||
114 : | |||
115 : | static int i,filenr = 0; | ||
116 : | |||
117 : | /* the path where to save output */ | ||
118 : | static char filepath[256] = "./"; | ||
119 : | |||
120 : | /* Internal structures (handles) for encoding and decoding */ | ||
121 : | static void *enc_handle = NULL; | ||
122 : | |||
123 : | /***************************************************************************** | ||
124 : | * Local prototypes | ||
125 : | ****************************************************************************/ | ||
126 : | |||
127 : | /* Prints program usage message */ | ||
128 : | static void usage(); | ||
129 : | |||
130 : | /* Statistical functions */ | ||
131 : | static double msecond(); | ||
132 : | |||
133 : | /* PGM related functions */ | ||
134 : | static int read_pgmheader(FILE* handle); | ||
135 : | static int read_pgmdata(FILE* handle, unsigned char *image); | ||
136 : | static int read_yuvdata(FILE* handle, unsigned char *image); | ||
137 : | |||
138 : | /* Encoder related functions */ | ||
139 : | static int enc_init(int use_assembler); | ||
140 : | static int enc_stop(); | ||
141 : | edgomez | 1.11.2.1 | static int enc_main(unsigned char* image, |
142 : | unsigned char* bitstream, | ||
143 : | edgomez | 1.11.2.2 | long *frametype, |
144 : | int stats[3]); | ||
145 : | edgomez | 1.2 | |
146 : | /***************************************************************************** | ||
147 : | * Main function | ||
148 : | ****************************************************************************/ | ||
149 : | |||
150 : | int main(int argc, char *argv[]) | ||
151 : | { | ||
152 : | |||
153 : | unsigned char *mp4_buffer = NULL; | ||
154 : | unsigned char *in_buffer = NULL; | ||
155 : | unsigned char *out_buffer = NULL; | ||
156 : | |||
157 : | double enctime; | ||
158 : | double totalenctime=0.; | ||
159 : | |||
160 : | long totalsize; | ||
161 : | int status; | ||
162 : | edgomez | 1.6 | long frame_type; |
163 : | edgomez | 1.2 | |
164 : | edgomez | 1.6 | long m4v_size; |
165 : | edgomez | 1.2 | int use_assembler=0; |
166 : | |||
167 : | char filename[256]; | ||
168 : | |||
169 : | FILE *in_file = stdin; | ||
170 : | FILE *out_file = NULL; | ||
171 : | |||
172 : | edgomez | 1.7 | printf("xvid_encraw - raw mpeg4 bitstream encoder "); |
173 : | edgomez | 1.10 | printf("written by Christoph Lampert 2002-2003\n\n"); |
174 : | chl | 1.1 | |
175 : | edgomez | 1.2 | /***************************************************************************** |
176 : | * Command line parsing | ||
177 : | ****************************************************************************/ | ||
178 : | chl | 1.1 | |
179 : | edgomez | 1.2 | for (i=1; i< argc; i++) { |
180 : | |||
181 : | if (strcmp("-asm", argv[i]) == 0 ) { | ||
182 : | use_assembler = 1; | ||
183 : | } | ||
184 : | else if (strcmp("-w", argv[i]) == 0 && i < argc - 1 ) { | ||
185 : | i++; | ||
186 : | XDIM = atoi(argv[i]); | ||
187 : | } | ||
188 : | else if (strcmp("-h", argv[i]) == 0 && i < argc - 1 ) { | ||
189 : | i++; | ||
190 : | YDIM = atoi(argv[i]); | ||
191 : | } | ||
192 : | else if (strcmp("-b", argv[i]) == 0 && i < argc - 1 ) { | ||
193 : | i++; | ||
194 : | ARG_BITRATE = atoi(argv[i]); | ||
195 : | } | ||
196 : | edgomez | 1.10 | else if (strcmp("-bn", argv[i]) == 0 && i < argc - 1 ) { |
197 : | i++; | ||
198 : | ARG_MAXBFRAMES = atoi(argv[i]); | ||
199 : | } | ||
200 : | else if (strcmp("-bqr", argv[i]) == 0 && i < argc - 1 ) { | ||
201 : | i++; | ||
202 : | ARG_BQRATIO = atoi(argv[i]); | ||
203 : | } | ||
204 : | else if (strcmp("-bqo", argv[i]) == 0 && i < argc - 1 ) { | ||
205 : | i++; | ||
206 : | ARG_BQOFFSET = atoi(argv[i]); | ||
207 : | } | ||
208 : | edgomez | 1.2 | else if (strcmp("-q", argv[i]) == 0 && i < argc - 1 ) { |
209 : | i++; | ||
210 : | ARG_QUALITY = atoi(argv[i]); | ||
211 : | } | ||
212 : | else if (strcmp("-f", argv[i]) == 0 && i < argc - 1 ) { | ||
213 : | i++; | ||
214 : | ARG_FRAMERATE = (float)atof(argv[i]); | ||
215 : | } | ||
216 : | else if (strcmp("-i", argv[i]) == 0 && i < argc - 1 ) { | ||
217 : | i++; | ||
218 : | ARG_INPUTFILE = argv[i]; | ||
219 : | } | ||
220 : | else if (strcmp("-t", argv[i]) == 0 && i < argc - 1 ) { | ||
221 : | i++; | ||
222 : | ARG_INPUTTYPE = atoi(argv[i]); | ||
223 : | } | ||
224 : | else if(strcmp("-n", argv[i]) == 0 && i < argc - 1 ) { | ||
225 : | i++; | ||
226 : | ARG_MAXFRAMENR = atoi(argv[i]); | ||
227 : | } | ||
228 : | else if (strcmp("-quant", argv[i]) == 0 && i < argc - 1 ) { | ||
229 : | i++; | ||
230 : | ARG_QUANTI = atoi(argv[i]); | ||
231 : | } | ||
232 : | else if (strcmp("-m", argv[i]) == 0 && i < argc - 1 ) { | ||
233 : | i++; | ||
234 : | ARG_SAVEMPEGSTREAM = atoi(argv[i]); | ||
235 : | } | ||
236 : | else if (strcmp("-o", argv[i]) == 0 && i < argc - 1 ) { | ||
237 : | i++; | ||
238 : | ARG_OUTPUTFILE = argv[i]; | ||
239 : | } | ||
240 : | else if (strcmp("-help", argv[i])) { | ||
241 : | usage(); | ||
242 : | return(0); | ||
243 : | } | ||
244 : | else { | ||
245 : | usage(); | ||
246 : | exit(-1); | ||
247 : | } | ||
248 : | |||
249 : | } | ||
250 : | |||
251 : | /***************************************************************************** | ||
252 : | * Arguments checking | ||
253 : | ****************************************************************************/ | ||
254 : | |||
255 : | if (XDIM <= 0 || XDIM >= 2048 || YDIM <=0 || YDIM >= 2048 ) { | ||
256 : | fprintf(stderr, "Trying to retreive width and height from PGM header\n"); | ||
257 : | ARG_INPUTTYPE = 1; /* pgm */ | ||
258 : | } | ||
259 : | |||
260 : | edgomez | 1.11.2.1 | if ( ARG_QUALITY < 0 || ARG_QUALITY > 5) { |
261 : | edgomez | 1.2 | fprintf(stderr,"Wrong Quality\n"); |
262 : | edgomez | 1.11.2.1 | return(-1); |
263 : | edgomez | 1.2 | } |
264 : | chl | 1.1 | |
265 : | edgomez | 1.2 | if ( ARG_BITRATE <= 0 && ARG_QUANTI == 0) { |
266 : | fprintf(stderr,"Wrong Bitrate\n"); | ||
267 : | edgomez | 1.11.2.1 | return(-1); |
268 : | edgomez | 1.2 | } |
269 : | chl | 1.1 | |
270 : | edgomez | 1.2 | if ( ARG_FRAMERATE <= 0) { |
271 : | fprintf(stderr,"Wrong Framerate %s \n",argv[5]); | ||
272 : | edgomez | 1.11.2.1 | return(-1); |
273 : | edgomez | 1.2 | } |
274 : | chl | 1.1 | |
275 : | edgomez | 1.2 | if ( ARG_MAXFRAMENR <= 0) { |
276 : | fprintf(stderr,"Wrong number of frames\n"); | ||
277 : | edgomez | 1.11.2.1 | return(-1); |
278 : | edgomez | 1.6 | } |
279 : | |||
280 : | edgomez | 1.2 | if ( ARG_INPUTFILE == NULL || strcmp(ARG_INPUTFILE, "stdin") == 0) { |
281 : | in_file = stdin; | ||
282 : | } | ||
283 : | else { | ||
284 : | chl | 1.1 | |
285 : | edgomez | 1.2 | in_file = fopen(ARG_INPUTFILE, "rb"); |
286 : | if (in_file == NULL) { | ||
287 : | fprintf(stderr, "Error opening input file %s\n", ARG_INPUTFILE); | ||
288 : | edgomez | 1.11.2.1 | return(-1); |
289 : | edgomez | 1.2 | } |
290 : | } | ||
291 : | |||
292 : | if (ARG_INPUTTYPE) { | ||
293 : | if (read_pgmheader(in_file)) { | ||
294 : | fprintf(stderr, "Wrong input format, I want YUV encapsulated in PGM\n"); | ||
295 : | edgomez | 1.11.2.1 | return(-1); |
296 : | edgomez | 1.2 | } |
297 : | } | ||
298 : | |||
299 : | /* now we know the sizes, so allocate memory */ | ||
300 : | in_buffer = (unsigned char *) malloc(IMAGE_SIZE(XDIM,YDIM)); | ||
301 : | if (!in_buffer) | ||
302 : | goto free_all_memory; | ||
303 : | |||
304 : | /* this should really be enough memory ! */ | ||
305 : | mp4_buffer = (unsigned char *) malloc(IMAGE_SIZE(XDIM,YDIM)*2); | ||
306 : | if (!mp4_buffer) | ||
307 : | goto free_all_memory; | ||
308 : | |||
309 : | /***************************************************************************** | ||
310 : | * XviD PART Start | ||
311 : | ****************************************************************************/ | ||
312 : | |||
313 : | |||
314 : | status = enc_init(use_assembler); | ||
315 : | if (status) | ||
316 : | { | ||
317 : | fprintf(stderr, "Encore INIT problem, return value %d\n", status); | ||
318 : | goto release_all; | ||
319 : | } | ||
320 : | |||
321 : | /***************************************************************************** | ||
322 : | * Main loop | ||
323 : | ****************************************************************************/ | ||
324 : | |||
325 : | edgomez | 1.10 | if (ARG_SAVEMPEGSTREAM && ARG_OUTPUTFILE) { |
326 : | edgomez | 1.2 | |
327 : | if((out_file = fopen(ARG_OUTPUTFILE, "w+b")) == NULL) { | ||
328 : | fprintf(stderr, "Error opening output file %s\n", ARG_OUTPUTFILE); | ||
329 : | goto release_all; | ||
330 : | } | ||
331 : | |||
332 : | } | ||
333 : | else { | ||
334 : | out_file = NULL; | ||
335 : | } | ||
336 : | |||
337 : | /***************************************************************************** | ||
338 : | * Encoding loop | ||
339 : | ****************************************************************************/ | ||
340 : | |||
341 : | totalsize = 0; | ||
342 : | |||
343 : | do { | ||
344 : | |||
345 : | edgomez | 1.11.2.2 | char *type; |
346 : | int stats[3]; | ||
347 : | |||
348 : | if(ARG_INPUTTYPE) { | ||
349 : | /* read PGM data (YUV-format) */ | ||
350 : | status = read_pgmdata(in_file, in_buffer); | ||
351 : | } else { | ||
352 : | /* read raw data (YUV-format) */ | ||
353 : | status = read_yuvdata(in_file, in_buffer); | ||
354 : | } | ||
355 : | edgomez | 1.2 | |
356 : | edgomez | 1.11.2.2 | if(status) { |
357 : | edgomez | 1.2 | /* Couldn't read image, most likely end-of-file */ |
358 : | continue; | ||
359 : | } | ||
360 : | |||
361 : | /***************************************************************************** | ||
362 : | * Encode and decode this frame | ||
363 : | ****************************************************************************/ | ||
364 : | |||
365 : | enctime = msecond(); | ||
366 : | edgomez | 1.11.2.2 | m4v_size = enc_main(in_buffer, mp4_buffer, &frame_type, stats); |
367 : | edgomez | 1.2 | enctime = msecond() - enctime; |
368 : | |||
369 : | edgomez | 1.11.2.1 | /* Not coded frames return 0 */ |
370 : | if(m4v_size == 0) goto next_frame; | ||
371 : | edgomez | 1.10 | |
372 : | edgomez | 1.11.2.2 | /* Write the Frame statistics */ |
373 : | switch(frame_type) { | ||
374 : | case XVID_TYPE_IVOP: | ||
375 : | type = "I"; | ||
376 : | break; | ||
377 : | case XVID_TYPE_PVOP: | ||
378 : | type = "P"; | ||
379 : | break; | ||
380 : | case XVID_TYPE_BVOP: | ||
381 : | type = "B"; | ||
382 : | break; | ||
383 : | case XVID_TYPE_SVOP: | ||
384 : | type = "S"; | ||
385 : | break; | ||
386 : | case XVID_TYPE_NOTHING: | ||
387 : | type = "N"; | ||
388 : | break; | ||
389 : | default: | ||
390 : | type = "U"; | ||
391 : | break; | ||
392 : | } | ||
393 : | |||
394 : | printf("Frame %5d: type = %s, enctime(ms) =%6.1f, length(bytes) =%7d, " | ||
395 : | "psnr y = %2.2f, psnr u = %2.2f, psnr v = %2.2f\n", | ||
396 : | (int)filenr, | ||
397 : | type, | ||
398 : | (float)enctime, | ||
399 : | (int)m4v_size, | ||
400 : | (stats[0] == 0)? 0.0f: 48.131f - 10*(float)log10((float)stats[0]/((float)(XDIM)*(YDIM))), | ||
401 : | (stats[1] == 0)? 0.0f: 48.131f - 10*(float)log10((float)stats[1]/((float)(XDIM)*(YDIM)/4)), | ||
402 : | (stats[2] == 0)? 0.0f: 48.131f - 10*(float)log10((float)stats[2]/((float)(XDIM)*(YDIM)/4))); | ||
403 : | edgomez | 1.10 | |
404 : | /* Update encoding time stats */ | ||
405 : | edgomez | 1.2 | totalenctime += enctime; |
406 : | totalsize += m4v_size; | ||
407 : | |||
408 : | edgomez | 1.6 | /***************************************************************************** |
409 : | * Save stream to file | ||
410 : | ****************************************************************************/ | ||
411 : | |||
412 : | edgomez | 1.2 | if (ARG_SAVEMPEGSTREAM) |
413 : | { | ||
414 : | /* Save single files */ | ||
415 : | if (out_file == NULL) { | ||
416 : | sprintf(filename, "%sframe%05d.m4v", filepath, filenr); | ||
417 : | out_file = fopen(filename, "wb"); | ||
418 : | fwrite(mp4_buffer, m4v_size, 1, out_file); | ||
419 : | fclose(out_file); | ||
420 : | out_file = NULL; | ||
421 : | } | ||
422 : | else { | ||
423 : | |||
424 : | /* Write mp4 data */ | ||
425 : | edgomez | 1.10 | fwrite(mp4_buffer, 1, m4v_size, out_file); |
426 : | edgomez | 1.2 | |
427 : | } | ||
428 : | } | ||
429 : | |||
430 : | edgomez | 1.10 | next_frame: |
431 : | edgomez | 1.2 | /* Read the header if it's pgm stream */ |
432 : | if (ARG_INPUTTYPE) | ||
433 : | status = read_pgmheader(in_file); | ||
434 : | |||
435 : | edgomez | 1.11.2.2 | filenr++; |
436 : | chl | 1.1 | |
437 : | edgomez | 1.2 | } while ( (!status) && (filenr<ARG_MAXFRAMENR) ); |
438 : | chl | 1.1 | |
439 : | |||
440 : | edgomez | 1.2 | |
441 : | /***************************************************************************** | ||
442 : | * Calculate totals and averages for output, print results | ||
443 : | ****************************************************************************/ | ||
444 : | chl | 1.1 | |
445 : | edgomez | 1.2 | totalsize /= filenr; |
446 : | totalenctime /= filenr; | ||
447 : | |||
448 : | edgomez | 1.10 | printf("Avg: enctime(ms) =%7.2f, fps =%7.2f, length(bytes) = %7d\n", |
449 : | edgomez | 1.2 | totalenctime, 1000/totalenctime, (int)totalsize); |
450 : | |||
451 : | edgomez | 1.11.2.2 | |
452 : | edgomez | 1.2 | /***************************************************************************** |
453 : | * XviD PART Stop | ||
454 : | ****************************************************************************/ | ||
455 : | |||
456 : | release_all: | ||
457 : | |||
458 : | if (enc_handle) | ||
459 : | { | ||
460 : | status = enc_stop(); | ||
461 : | if (status) | ||
462 : | fprintf(stderr, "Encore RELEASE problem return value %d\n", status); | ||
463 : | } | ||
464 : | chl | 1.1 | |
465 : | edgomez | 1.6 | if(in_file) |
466 : | fclose(in_file); | ||
467 : | edgomez | 1.2 | if(out_file) |
468 : | fclose(out_file); | ||
469 : | chl | 1.1 | |
470 : | edgomez | 1.2 | free_all_memory: |
471 : | free(out_buffer); | ||
472 : | free(mp4_buffer); | ||
473 : | free(in_buffer); | ||
474 : | chl | 1.1 | |
475 : | edgomez | 1.11.2.1 | return(0); |
476 : | edgomez | 1.2 | |
477 : | } | ||
478 : | chl | 1.1 | |
479 : | |||
480 : | edgomez | 1.2 | /***************************************************************************** |
481 : | * "statistical" functions | ||
482 : | * | ||
483 : | * these are not needed for encoding or decoding, but for measuring | ||
484 : | * time and quality, there in nothing specific to XviD in these | ||
485 : | * | ||
486 : | *****************************************************************************/ | ||
487 : | |||
488 : | /* Return time elapsed time in miliseconds since the program started */ | ||
489 : | static double msecond() | ||
490 : | chl | 1.1 | { |
491 : | edgomez | 1.8 | #ifndef WIN32 |
492 : | chl | 1.1 | struct timeval tv; |
493 : | gettimeofday(&tv, 0); | ||
494 : | edgomez | 1.11.2.1 | return(tv.tv_sec*1.0e3 + tv.tv_usec * 1.0e-3); |
495 : | edgomez | 1.2 | #else |
496 : | clock_t clk; | ||
497 : | clk = clock(); | ||
498 : | edgomez | 1.11.2.1 | return(clk * 1000 / CLOCKS_PER_SEC); |
499 : | edgomez | 1.2 | #endif |
500 : | chl | 1.1 | } |
501 : | |||
502 : | edgomez | 1.2 | /***************************************************************************** |
503 : | * Usage message | ||
504 : | *****************************************************************************/ | ||
505 : | |||
506 : | static void usage() | ||
507 : | { | ||
508 : | |||
509 : | edgomez | 1.10 | fprintf(stderr, "Usage : xvid_stat [OPTIONS]\n"); |
510 : | edgomez | 1.2 | fprintf(stderr, "Options :\n"); |
511 : | fprintf(stderr, " -w integer : frame width ([1.2048])\n"); | ||
512 : | fprintf(stderr, " -h integer : frame height ([1.2048])\n"); | ||
513 : | fprintf(stderr, " -b integer : target bitrate (>0 | default=900kbit)\n"); | ||
514 : | edgomez | 1.10 | fprintf(stderr, " -bn integer : max bframes (default=0)\n"); |
515 : | fprintf(stderr, " -bqr integer : bframe quantizer ratio (default=150)\n"); | ||
516 : | fprintf(stderr, " -bqo integer : bframe quantizer offset (default=100)\n"); | ||
517 : | edgomez | 1.2 | fprintf(stderr, " -f float : target framerate (>0)\n"); |
518 : | fprintf(stderr, " -i string : input filename (default=stdin)\n"); | ||
519 : | fprintf(stderr, " -t integer : input data type (yuv=0, pgm=1)\n"); | ||
520 : | fprintf(stderr, " -n integer : number of frames to encode\n"); | ||
521 : | fprintf(stderr, " -q integer : quality ([0..5])\n"); | ||
522 : | edgomez | 1.10 | fprintf(stderr, " -d boolean : save decoder output (0 False*, !=0 True)\n"); |
523 : | edgomez | 1.2 | fprintf(stderr, " -m boolean : save mpeg4 raw stream (0 False*, !=0 True)\n"); |
524 : | fprintf(stderr, " -o string : output container filename (only usefull when -m 1 is used) :\n"); | ||
525 : | fprintf(stderr, " When this option is not used : one file per encoded frame\n"); | ||
526 : | edgomez | 1.11.2.1 | fprintf(stderr, " When this option is used : save to 'string' file\n"); |
527 : | edgomez | 1.2 | fprintf(stderr, " -help : prints this help message\n"); |
528 : | fprintf(stderr, " -quant integer : fixed quantizer (disables -b setting)\n"); | ||
529 : | fprintf(stderr, " (* means default)\n"); | ||
530 : | chl | 1.1 | |
531 : | edgomez | 1.2 | } |
532 : | |||
533 : | /***************************************************************************** | ||
534 : | * Input and output functions | ||
535 : | * | ||
536 : | * the are small and simple routines to read and write PGM and YUV | ||
537 : | * image. It's just for convenience, again nothing specific to XviD | ||
538 : | * | ||
539 : | *****************************************************************************/ | ||
540 : | |||
541 : | static int read_pgmheader(FILE* handle) | ||
542 : | chl | 1.1 | { |
543 : | int bytes,xsize,ysize,depth; | ||
544 : | char dummy[2]; | ||
545 : | |||
546 : | bytes = fread(dummy,1,2,handle); | ||
547 : | |||
548 : | if ( (bytes < 2) || (dummy[0] != 'P') || (dummy[1] != '5' )) | ||
549 : | edgomez | 1.11.2.1 | return(1); |
550 : | edgomez | 1.2 | |
551 : | chl | 1.1 | fscanf(handle,"%d %d %d",&xsize,&ysize,&depth); |
552 : | if ( (xsize > 1440) || (ysize > 2880 ) || (depth != 255) ) | ||
553 : | { | ||
554 : | edgomez | 1.2 | fprintf(stderr,"%d %d %d\n",xsize,ysize,depth); |
555 : | edgomez | 1.11.2.1 | return(2); |
556 : | chl | 1.1 | } |
557 : | if ( (XDIM==0) || (YDIM==0) ) | ||
558 : | edgomez | 1.2 | { |
559 : | XDIM=xsize; | ||
560 : | YDIM=ysize*2/3; | ||
561 : | chl | 1.1 | } |
562 : | |||
563 : | edgomez | 1.11.2.1 | return(0); |
564 : | chl | 1.1 | } |
565 : | |||
566 : | edgomez | 1.2 | static int read_pgmdata(FILE* handle, unsigned char *image) |
567 : | chl | 1.1 | { |
568 : | edgomez | 1.2 | int i; |
569 : | chl | 1.1 | char dummy; |
570 : | edgomez | 1.2 | |
571 : | unsigned char *y = image; | ||
572 : | unsigned char *u = image + XDIM*YDIM; | ||
573 : | unsigned char *v = image + XDIM*YDIM + XDIM/2*YDIM/2; | ||
574 : | chl | 1.1 | |
575 : | edgomez | 1.2 | /* read Y component of picture */ |
576 : | fread(y, 1, XDIM*YDIM, handle); | ||
577 : | chl | 1.1 | |
578 : | edgomez | 1.2 | for (i=0;i<YDIM/2;i++) |
579 : | chl | 1.1 | { |
580 : | edgomez | 1.2 | /* read U */ |
581 : | fread(u, 1, XDIM/2, handle); | ||
582 : | |||
583 : | /* read V */ | ||
584 : | fread(v, 1, XDIM/2, handle); | ||
585 : | |||
586 : | /* Update pointers */ | ||
587 : | u += XDIM/2; | ||
588 : | v += XDIM/2; | ||
589 : | chl | 1.1 | } |
590 : | edgomez | 1.2 | |
591 : | /* I don't know why, but this seems needed */ | ||
592 : | fread(&dummy, 1, 1, handle); | ||
593 : | |||
594 : | edgomez | 1.11.2.1 | return(0); |
595 : | chl | 1.1 | } |
596 : | |||
597 : | edgomez | 1.2 | static int read_yuvdata(FILE* handle, unsigned char *image) |
598 : | { | ||
599 : | chl | 1.1 | |
600 : | edgomez | 1.2 | if (fread(image, 1, IMAGE_SIZE(XDIM, YDIM), handle) != (unsigned int)IMAGE_SIZE(XDIM, YDIM)) |
601 : | edgomez | 1.11.2.1 | return(1); |
602 : | chl | 1.1 | else |
603 : | edgomez | 1.11.2.1 | return(0); |
604 : | chl | 1.1 | } |
605 : | |||
606 : | edgomez | 1.2 | /***************************************************************************** |
607 : | * Routines for encoding: init encoder, frame step, release encoder | ||
608 : | ****************************************************************************/ | ||
609 : | chl | 1.1 | |
610 : | #define FRAMERATE_INCR 1001 | ||
611 : | |||
612 : | edgomez | 1.2 | /* Initialize encoder for first use, pass all needed parameters to the codec */ |
613 : | static int enc_init(int use_assembler) | ||
614 : | { | ||
615 : | chl | 1.1 | int xerr; |
616 : | |||
617 : | edgomez | 1.11.2.1 | xvid_gbl_init_t xvid_gbl_init; |
618 : | xvid_enc_create_t xvid_enc_create; | ||
619 : | |||
620 : | /*------------------------------------------------------------------------ | ||
621 : | * XviD core initialization | ||
622 : | *----------------------------------------------------------------------*/ | ||
623 : | chl | 1.1 | |
624 : | edgomez | 1.11.2.1 | /* Set version -- version checking will done by xvidcore*/ |
625 : | xvid_gbl_init.version = XVID_VERSION; | ||
626 : | |||
627 : | /* Do we have to enable ASM optimizations ? */ | ||
628 : | edgomez | 1.2 | if(use_assembler) { |
629 : | chl | 1.1 | |
630 : | suxen_drol | 1.11 | #ifdef ARCH_IS_IA64 |
631 : | edgomez | 1.11.2.1 | xvid_gbl_init.cpu_flags = XVID_CPU_FORCE | XVID_CPU_IA64; |
632 : | chl | 1.1 | #else |
633 : | edgomez | 1.11.2.1 | xvid_gbl_init.cpu_flags = 0; |
634 : | chl | 1.1 | #endif |
635 : | edgomez | 1.2 | } |
636 : | else { | ||
637 : | edgomez | 1.11.2.1 | xvid_gbl_init.cpu_flags = XVID_CPU_FORCE; |
638 : | edgomez | 1.2 | } |
639 : | chl | 1.1 | |
640 : | edgomez | 1.11.2.1 | /* Initialize XviD core -- Should be done once per __process__ */ |
641 : | xvid_global(NULL, XVID_GBL_INIT, &xvid_gbl_init, NULL); | ||
642 : | chl | 1.1 | |
643 : | edgomez | 1.11.2.1 | /*------------------------------------------------------------------------ |
644 : | * XviD encoder initialization | ||
645 : | *----------------------------------------------------------------------*/ | ||
646 : | |||
647 : | /* Version again */ | ||
648 : | xvid_enc_create.version = XVID_VERSION; | ||
649 : | |||
650 : | /* Width and Height of input frames */ | ||
651 : | xvid_enc_create.width = XDIM; | ||
652 : | xvid_enc_create.height = YDIM; | ||
653 : | |||
654 : | /* No fancy thread tests */ | ||
655 : | xvid_enc_create.num_threads = 0; | ||
656 : | |||
657 : | /* Frame rate - Do some quick float fps = fincr/fbase hack */ | ||
658 : | if ((ARG_FRAMERATE - (int)ARG_FRAMERATE) < SMALL_EPS) { | ||
659 : | xvid_enc_create.fincr = 1; | ||
660 : | xvid_enc_create.fbase = (int)ARG_FRAMERATE; | ||
661 : | } else { | ||
662 : | xvid_enc_create.fincr = FRAMERATE_INCR; | ||
663 : | xvid_enc_create.fbase = (int)(FRAMERATE_INCR * ARG_FRAMERATE); | ||
664 : | chl | 1.1 | } |
665 : | edgomez | 1.11.2.1 | |
666 : | /* Maximum key frame interval */ | ||
667 : | xvid_enc_create.max_key_interval = (int)ARG_FRAMERATE*10; | ||
668 : | |||
669 : | /* Bframes settings */ | ||
670 : | xvid_enc_create.max_bframes = ARG_MAXBFRAMES; | ||
671 : | xvid_enc_create.bquant_ratio = ARG_BQRATIO; | ||
672 : | xvid_enc_create.bquant_offset = ARG_BQOFFSET; | ||
673 : | |||
674 : | /* Dropping ratio frame -- we don't need that */ | ||
675 : | xvid_enc_create.frame_drop_ratio = 0; | ||
676 : | |||
677 : | /* Global encoder options */ | ||
678 : | edgomez | 1.11.2.2 | xvid_enc_create.global = XVID_EXTRASTATS_ENABLE; |
679 : | chl | 1.1 | |
680 : | edgomez | 1.2 | /* I use a small value here, since will not encode whole movies, but short clips */ |
681 : | edgomez | 1.11.2.1 | xerr = xvid_encore(NULL, XVID_ENC_CREATE, &xvid_enc_create, NULL); |
682 : | chl | 1.1 | |
683 : | edgomez | 1.11.2.1 | /* Retrieve the encoder instance from the structure */ |
684 : | enc_handle = xvid_enc_create.handle; | ||
685 : | chl | 1.1 | |
686 : | edgomez | 1.11.2.1 | return(xerr); |
687 : | chl | 1.1 | } |
688 : | |||
689 : | edgomez | 1.2 | static int enc_stop() |
690 : | { | ||
691 : | int xerr; | ||
692 : | chl | 1.1 | |
693 : | edgomez | 1.11.2.1 | /* Destroy the encoder instance */ |
694 : | chl | 1.1 | xerr = xvid_encore(enc_handle, XVID_ENC_DESTROY, NULL, NULL); |
695 : | edgomez | 1.2 | |
696 : | edgomez | 1.11.2.1 | return(xerr); |
697 : | chl | 1.1 | } |
698 : | |||
699 : | edgomez | 1.11.2.1 | static int enc_main(unsigned char* image, |
700 : | unsigned char* bitstream, | ||
701 : | edgomez | 1.11.2.2 | long *frametype, |
702 : | int stats[3]) | ||
703 : | edgomez | 1.2 | { |
704 : | edgomez | 1.11.2.1 | int ret; |
705 : | chl | 1.1 | |
706 : | edgomez | 1.11.2.1 | xvid_enc_frame_t xvid_enc_frame; |
707 : | xvid_enc_stats_t xvid_enc_stats[2]; | ||
708 : | chl | 1.1 | |
709 : | edgomez | 1.11.2.1 | /* Version for the frame and the stats */ |
710 : | xvid_enc_frame.version = XVID_VERSION; | ||
711 : | xvid_enc_stats[0].version = XVID_VERSION; | ||
712 : | xvid_enc_stats[1].version = XVID_VERSION; | ||
713 : | chl | 1.1 | |
714 : | edgomez | 1.11.2.1 | /* Bind output buffer */ |
715 : | xvid_enc_frame.bitstream = bitstream; | ||
716 : | xvid_enc_frame.length = -1; | ||
717 : | chl | 1.1 | |
718 : | edgomez | 1.11.2.1 | /* Initialize input image fields */ |
719 : | xvid_enc_frame.input.plane[0] = image; | ||
720 : | xvid_enc_frame.input.csp = XVID_CSP_I420; | ||
721 : | xvid_enc_frame.input.stride[0] = XDIM; | ||
722 : | chl | 1.1 | |
723 : | edgomez | 1.11.2.1 | /* Set up core's general features */ |
724 : | xvid_enc_frame.vol_flags = vol_presets[ARG_QUALITY]; | ||
725 : | chl | 1.1 | |
726 : | edgomez | 1.11.2.1 | /* Set up core's general features */ |
727 : | xvid_enc_frame.vop_flags = vop_presets[ARG_QUALITY]; | ||
728 : | edgomez | 1.6 | |
729 : | edgomez | 1.11.2.1 | /* Frame type -- let core decide for us */ |
730 : | xvid_enc_frame.type = XVID_TYPE_AUTO; | ||
731 : | edgomez | 1.6 | |
732 : | edgomez | 1.11.2.1 | /* Force the right quantizer */ |
733 : | xvid_enc_frame.quant = ARG_QUANTI; | ||
734 : | xvid_enc_frame.bquant = 0; | ||
735 : | |||
736 : | /* Set up motion estimation flags */ | ||
737 : | xvid_enc_frame.motion = motion_presets[ARG_QUALITY]; | ||
738 : | edgomez | 1.6 | |
739 : | edgomez | 1.11.2.1 | /* We don't use special matrices */ |
740 : | xvid_enc_frame.quant_intra_matrix = NULL; | ||
741 : | xvid_enc_frame.quant_inter_matrix = NULL; | ||
742 : | edgomez | 1.6 | |
743 : | edgomez | 1.11.2.2 | /* Foll proof */ |
744 : | xvid_enc_stats[0].sse_y = 0; | ||
745 : | xvid_enc_stats[0].sse_v = 0; | ||
746 : | xvid_enc_stats[0].sse_u = 0; | ||
747 : | |||
748 : | /* Encode the frame */ | ||
749 : | xvid_enc_frame.vop_flags |= XVID_EXTRASTATS; | ||
750 : | edgomez | 1.11.2.1 | ret = xvid_encore(enc_handle, XVID_ENC_ENCODE, &xvid_enc_frame, &xvid_enc_stats); |
751 : | chl | 1.1 | |
752 : | edgomez | 1.11.2.1 | *frametype = xvid_enc_stats[0].type; |
753 : | edgomez | 1.11.2.2 | stats[0] = xvid_enc_stats[0].sse_y; |
754 : | stats[1] = xvid_enc_stats[0].sse_u; | ||
755 : | stats[2] = xvid_enc_stats[0].sse_v; | ||
756 : | chl | 1.1 | |
757 : | edgomez | 1.11.2.1 | return(ret); |
758 : | chl | 1.1 | } |
No admin address has been configured | ViewVC Help |
Powered by ViewVC 1.0.4 |