Parent Directory | Revision Log
Revision 1.11.2.1 - (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 : | PMV_HALFPELREFINE16 | PMV_HALFPELREFINE8 | PMV_EXTSEARCH16 | PMV_USESQUARES16, | ||
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 : | long *frametype); | ||
144 : | edgomez | 1.2 | |
145 : | /***************************************************************************** | ||
146 : | * Main function | ||
147 : | ****************************************************************************/ | ||
148 : | |||
149 : | int main(int argc, char *argv[]) | ||
150 : | { | ||
151 : | |||
152 : | unsigned char *mp4_buffer = NULL; | ||
153 : | unsigned char *in_buffer = NULL; | ||
154 : | unsigned char *out_buffer = NULL; | ||
155 : | |||
156 : | double enctime; | ||
157 : | double totalenctime=0.; | ||
158 : | |||
159 : | long totalsize; | ||
160 : | int status; | ||
161 : | edgomez | 1.6 | long frame_type; |
162 : | edgomez | 1.2 | |
163 : | edgomez | 1.6 | long m4v_size; |
164 : | edgomez | 1.2 | int use_assembler=0; |
165 : | |||
166 : | char filename[256]; | ||
167 : | |||
168 : | FILE *in_file = stdin; | ||
169 : | FILE *out_file = NULL; | ||
170 : | |||
171 : | edgomez | 1.7 | printf("xvid_encraw - raw mpeg4 bitstream encoder "); |
172 : | edgomez | 1.10 | printf("written by Christoph Lampert 2002-2003\n\n"); |
173 : | chl | 1.1 | |
174 : | edgomez | 1.2 | /***************************************************************************** |
175 : | * Command line parsing | ||
176 : | ****************************************************************************/ | ||
177 : | chl | 1.1 | |
178 : | edgomez | 1.2 | for (i=1; i< argc; i++) { |
179 : | |||
180 : | if (strcmp("-asm", argv[i]) == 0 ) { | ||
181 : | use_assembler = 1; | ||
182 : | } | ||
183 : | else if (strcmp("-w", argv[i]) == 0 && i < argc - 1 ) { | ||
184 : | i++; | ||
185 : | XDIM = atoi(argv[i]); | ||
186 : | } | ||
187 : | else if (strcmp("-h", argv[i]) == 0 && i < argc - 1 ) { | ||
188 : | i++; | ||
189 : | YDIM = atoi(argv[i]); | ||
190 : | } | ||
191 : | else if (strcmp("-b", argv[i]) == 0 && i < argc - 1 ) { | ||
192 : | i++; | ||
193 : | ARG_BITRATE = atoi(argv[i]); | ||
194 : | } | ||
195 : | edgomez | 1.10 | else if (strcmp("-bn", argv[i]) == 0 && i < argc - 1 ) { |
196 : | i++; | ||
197 : | ARG_MAXBFRAMES = atoi(argv[i]); | ||
198 : | } | ||
199 : | else if (strcmp("-bqr", argv[i]) == 0 && i < argc - 1 ) { | ||
200 : | i++; | ||
201 : | ARG_BQRATIO = atoi(argv[i]); | ||
202 : | } | ||
203 : | else if (strcmp("-bqo", argv[i]) == 0 && i < argc - 1 ) { | ||
204 : | i++; | ||
205 : | ARG_BQOFFSET = atoi(argv[i]); | ||
206 : | } | ||
207 : | edgomez | 1.2 | else if (strcmp("-q", argv[i]) == 0 && i < argc - 1 ) { |
208 : | i++; | ||
209 : | ARG_QUALITY = atoi(argv[i]); | ||
210 : | } | ||
211 : | else if (strcmp("-f", argv[i]) == 0 && i < argc - 1 ) { | ||
212 : | i++; | ||
213 : | ARG_FRAMERATE = (float)atof(argv[i]); | ||
214 : | } | ||
215 : | else if (strcmp("-i", argv[i]) == 0 && i < argc - 1 ) { | ||
216 : | i++; | ||
217 : | ARG_INPUTFILE = argv[i]; | ||
218 : | } | ||
219 : | else if (strcmp("-t", argv[i]) == 0 && i < argc - 1 ) { | ||
220 : | i++; | ||
221 : | ARG_INPUTTYPE = atoi(argv[i]); | ||
222 : | } | ||
223 : | else if(strcmp("-n", argv[i]) == 0 && i < argc - 1 ) { | ||
224 : | i++; | ||
225 : | ARG_MAXFRAMENR = atoi(argv[i]); | ||
226 : | } | ||
227 : | else if (strcmp("-quant", argv[i]) == 0 && i < argc - 1 ) { | ||
228 : | i++; | ||
229 : | ARG_QUANTI = atoi(argv[i]); | ||
230 : | } | ||
231 : | else if (strcmp("-m", argv[i]) == 0 && i < argc - 1 ) { | ||
232 : | i++; | ||
233 : | ARG_SAVEMPEGSTREAM = atoi(argv[i]); | ||
234 : | } | ||
235 : | else if (strcmp("-o", argv[i]) == 0 && i < argc - 1 ) { | ||
236 : | i++; | ||
237 : | ARG_OUTPUTFILE = argv[i]; | ||
238 : | } | ||
239 : | else if (strcmp("-help", argv[i])) { | ||
240 : | usage(); | ||
241 : | return(0); | ||
242 : | } | ||
243 : | else { | ||
244 : | usage(); | ||
245 : | exit(-1); | ||
246 : | } | ||
247 : | |||
248 : | } | ||
249 : | |||
250 : | /***************************************************************************** | ||
251 : | * Arguments checking | ||
252 : | ****************************************************************************/ | ||
253 : | |||
254 : | if (XDIM <= 0 || XDIM >= 2048 || YDIM <=0 || YDIM >= 2048 ) { | ||
255 : | fprintf(stderr, "Trying to retreive width and height from PGM header\n"); | ||
256 : | ARG_INPUTTYPE = 1; /* pgm */ | ||
257 : | } | ||
258 : | |||
259 : | edgomez | 1.11.2.1 | if ( ARG_QUALITY < 0 || ARG_QUALITY > 5) { |
260 : | edgomez | 1.2 | fprintf(stderr,"Wrong Quality\n"); |
261 : | edgomez | 1.11.2.1 | return(-1); |
262 : | edgomez | 1.2 | } |
263 : | chl | 1.1 | |
264 : | edgomez | 1.2 | if ( ARG_BITRATE <= 0 && ARG_QUANTI == 0) { |
265 : | fprintf(stderr,"Wrong Bitrate\n"); | ||
266 : | edgomez | 1.11.2.1 | return(-1); |
267 : | edgomez | 1.2 | } |
268 : | chl | 1.1 | |
269 : | edgomez | 1.2 | if ( ARG_FRAMERATE <= 0) { |
270 : | fprintf(stderr,"Wrong Framerate %s \n",argv[5]); | ||
271 : | edgomez | 1.11.2.1 | return(-1); |
272 : | edgomez | 1.2 | } |
273 : | chl | 1.1 | |
274 : | edgomez | 1.2 | if ( ARG_MAXFRAMENR <= 0) { |
275 : | fprintf(stderr,"Wrong number of frames\n"); | ||
276 : | edgomez | 1.11.2.1 | return(-1); |
277 : | edgomez | 1.6 | } |
278 : | |||
279 : | edgomez | 1.2 | if ( ARG_INPUTFILE == NULL || strcmp(ARG_INPUTFILE, "stdin") == 0) { |
280 : | in_file = stdin; | ||
281 : | } | ||
282 : | else { | ||
283 : | chl | 1.1 | |
284 : | edgomez | 1.2 | in_file = fopen(ARG_INPUTFILE, "rb"); |
285 : | if (in_file == NULL) { | ||
286 : | fprintf(stderr, "Error opening input file %s\n", ARG_INPUTFILE); | ||
287 : | edgomez | 1.11.2.1 | return(-1); |
288 : | edgomez | 1.2 | } |
289 : | } | ||
290 : | |||
291 : | if (ARG_INPUTTYPE) { | ||
292 : | if (read_pgmheader(in_file)) { | ||
293 : | fprintf(stderr, "Wrong input format, I want YUV encapsulated in PGM\n"); | ||
294 : | edgomez | 1.11.2.1 | return(-1); |
295 : | edgomez | 1.2 | } |
296 : | } | ||
297 : | |||
298 : | /* now we know the sizes, so allocate memory */ | ||
299 : | in_buffer = (unsigned char *) malloc(IMAGE_SIZE(XDIM,YDIM)); | ||
300 : | if (!in_buffer) | ||
301 : | goto free_all_memory; | ||
302 : | |||
303 : | /* this should really be enough memory ! */ | ||
304 : | mp4_buffer = (unsigned char *) malloc(IMAGE_SIZE(XDIM,YDIM)*2); | ||
305 : | if (!mp4_buffer) | ||
306 : | goto free_all_memory; | ||
307 : | |||
308 : | /***************************************************************************** | ||
309 : | * XviD PART Start | ||
310 : | ****************************************************************************/ | ||
311 : | |||
312 : | |||
313 : | status = enc_init(use_assembler); | ||
314 : | if (status) | ||
315 : | { | ||
316 : | fprintf(stderr, "Encore INIT problem, return value %d\n", status); | ||
317 : | goto release_all; | ||
318 : | } | ||
319 : | |||
320 : | /***************************************************************************** | ||
321 : | * Main loop | ||
322 : | ****************************************************************************/ | ||
323 : | |||
324 : | edgomez | 1.10 | if (ARG_SAVEMPEGSTREAM && ARG_OUTPUTFILE) { |
325 : | edgomez | 1.2 | |
326 : | if((out_file = fopen(ARG_OUTPUTFILE, "w+b")) == NULL) { | ||
327 : | fprintf(stderr, "Error opening output file %s\n", ARG_OUTPUTFILE); | ||
328 : | goto release_all; | ||
329 : | } | ||
330 : | |||
331 : | } | ||
332 : | else { | ||
333 : | out_file = NULL; | ||
334 : | } | ||
335 : | |||
336 : | /***************************************************************************** | ||
337 : | * Encoding loop | ||
338 : | ****************************************************************************/ | ||
339 : | |||
340 : | totalsize = 0; | ||
341 : | |||
342 : | do { | ||
343 : | |||
344 : | if (ARG_INPUTTYPE) | ||
345 : | edgomez | 1.5 | status = read_pgmdata(in_file, in_buffer); /* read PGM data (YUV-format) */ |
346 : | edgomez | 1.2 | else |
347 : | edgomez | 1.5 | status = read_yuvdata(in_file, in_buffer); /* read raw data (YUV-format) */ |
348 : | edgomez | 1.2 | |
349 : | if (status) | ||
350 : | { | ||
351 : | /* Couldn't read image, most likely end-of-file */ | ||
352 : | continue; | ||
353 : | } | ||
354 : | |||
355 : | /***************************************************************************** | ||
356 : | * Encode and decode this frame | ||
357 : | ****************************************************************************/ | ||
358 : | |||
359 : | enctime = msecond(); | ||
360 : | edgomez | 1.11.2.1 | m4v_size = enc_main(in_buffer, mp4_buffer, &frame_type); |
361 : | edgomez | 1.2 | enctime = msecond() - enctime; |
362 : | |||
363 : | edgomez | 1.11.2.1 | /* Not coded frames return 0 */ |
364 : | if(m4v_size == 0) goto next_frame; | ||
365 : | edgomez | 1.10 | |
366 : | { | ||
367 : | edgomez | 1.11.2.1 | char *type; |
368 : | edgomez | 1.10 | |
369 : | edgomez | 1.11.2.1 | switch(frame_type) { |
370 : | case XVID_TYPE_IVOP: | ||
371 : | type = "I"; | ||
372 : | break; | ||
373 : | case XVID_TYPE_PVOP: | ||
374 : | type = "P"; | ||
375 : | break; | ||
376 : | case XVID_TYPE_BVOP: | ||
377 : | type = "B"; | ||
378 : | break; | ||
379 : | case XVID_TYPE_SVOP: | ||
380 : | type = "S"; | ||
381 : | break; | ||
382 : | default: | ||
383 : | type = "Unknown"; | ||
384 : | break; | ||
385 : | } | ||
386 : | edgomez | 1.10 | |
387 : | printf("Frame %5d: type = %s, enctime(ms) =%6.1f, length(bytes) =%7d\n", | ||
388 : | edgomez | 1.11.2.1 | (int)filenr, type, (float)enctime, (int)m4v_size); |
389 : | edgomez | 1.10 | |
390 : | } | ||
391 : | |||
392 : | /* Update encoding time stats */ | ||
393 : | edgomez | 1.2 | totalenctime += enctime; |
394 : | totalsize += m4v_size; | ||
395 : | |||
396 : | edgomez | 1.6 | /***************************************************************************** |
397 : | * Save stream to file | ||
398 : | ****************************************************************************/ | ||
399 : | |||
400 : | edgomez | 1.2 | if (ARG_SAVEMPEGSTREAM) |
401 : | { | ||
402 : | /* Save single files */ | ||
403 : | if (out_file == NULL) { | ||
404 : | sprintf(filename, "%sframe%05d.m4v", filepath, filenr); | ||
405 : | out_file = fopen(filename, "wb"); | ||
406 : | fwrite(mp4_buffer, m4v_size, 1, out_file); | ||
407 : | fclose(out_file); | ||
408 : | out_file = NULL; | ||
409 : | } | ||
410 : | else { | ||
411 : | |||
412 : | /* Write mp4 data */ | ||
413 : | edgomez | 1.10 | fwrite(mp4_buffer, 1, m4v_size, out_file); |
414 : | edgomez | 1.2 | |
415 : | } | ||
416 : | } | ||
417 : | |||
418 : | edgomez | 1.10 | next_frame: |
419 : | edgomez | 1.2 | /* Read the header if it's pgm stream */ |
420 : | if (ARG_INPUTTYPE) | ||
421 : | status = read_pgmheader(in_file); | ||
422 : | |||
423 : | edgomez | 1.10 | if(frame_type != 5) filenr++; |
424 : | chl | 1.1 | |
425 : | edgomez | 1.2 | } while ( (!status) && (filenr<ARG_MAXFRAMENR) ); |
426 : | chl | 1.1 | |
427 : | |||
428 : | edgomez | 1.2 | |
429 : | /***************************************************************************** | ||
430 : | * Calculate totals and averages for output, print results | ||
431 : | ****************************************************************************/ | ||
432 : | chl | 1.1 | |
433 : | edgomez | 1.2 | totalsize /= filenr; |
434 : | totalenctime /= filenr; | ||
435 : | |||
436 : | edgomez | 1.10 | printf("Avg: enctime(ms) =%7.2f, fps =%7.2f, length(bytes) = %7d\n", |
437 : | edgomez | 1.2 | totalenctime, 1000/totalenctime, (int)totalsize); |
438 : | |||
439 : | /***************************************************************************** | ||
440 : | * XviD PART Stop | ||
441 : | ****************************************************************************/ | ||
442 : | |||
443 : | release_all: | ||
444 : | |||
445 : | if (enc_handle) | ||
446 : | { | ||
447 : | status = enc_stop(); | ||
448 : | if (status) | ||
449 : | fprintf(stderr, "Encore RELEASE problem return value %d\n", status); | ||
450 : | } | ||
451 : | chl | 1.1 | |
452 : | edgomez | 1.6 | if(in_file) |
453 : | fclose(in_file); | ||
454 : | edgomez | 1.2 | if(out_file) |
455 : | fclose(out_file); | ||
456 : | chl | 1.1 | |
457 : | edgomez | 1.2 | free_all_memory: |
458 : | free(out_buffer); | ||
459 : | free(mp4_buffer); | ||
460 : | free(in_buffer); | ||
461 : | chl | 1.1 | |
462 : | edgomez | 1.11.2.1 | return(0); |
463 : | edgomez | 1.2 | |
464 : | } | ||
465 : | chl | 1.1 | |
466 : | |||
467 : | edgomez | 1.2 | /***************************************************************************** |
468 : | * "statistical" functions | ||
469 : | * | ||
470 : | * these are not needed for encoding or decoding, but for measuring | ||
471 : | * time and quality, there in nothing specific to XviD in these | ||
472 : | * | ||
473 : | *****************************************************************************/ | ||
474 : | |||
475 : | /* Return time elapsed time in miliseconds since the program started */ | ||
476 : | static double msecond() | ||
477 : | chl | 1.1 | { |
478 : | edgomez | 1.8 | #ifndef WIN32 |
479 : | chl | 1.1 | struct timeval tv; |
480 : | gettimeofday(&tv, 0); | ||
481 : | edgomez | 1.11.2.1 | return(tv.tv_sec*1.0e3 + tv.tv_usec * 1.0e-3); |
482 : | edgomez | 1.2 | #else |
483 : | clock_t clk; | ||
484 : | clk = clock(); | ||
485 : | edgomez | 1.11.2.1 | return(clk * 1000 / CLOCKS_PER_SEC); |
486 : | edgomez | 1.2 | #endif |
487 : | chl | 1.1 | } |
488 : | |||
489 : | edgomez | 1.2 | /***************************************************************************** |
490 : | * Usage message | ||
491 : | *****************************************************************************/ | ||
492 : | |||
493 : | static void usage() | ||
494 : | { | ||
495 : | |||
496 : | edgomez | 1.10 | fprintf(stderr, "Usage : xvid_stat [OPTIONS]\n"); |
497 : | edgomez | 1.2 | fprintf(stderr, "Options :\n"); |
498 : | fprintf(stderr, " -w integer : frame width ([1.2048])\n"); | ||
499 : | fprintf(stderr, " -h integer : frame height ([1.2048])\n"); | ||
500 : | fprintf(stderr, " -b integer : target bitrate (>0 | default=900kbit)\n"); | ||
501 : | edgomez | 1.10 | fprintf(stderr, " -bn integer : max bframes (default=0)\n"); |
502 : | fprintf(stderr, " -bqr integer : bframe quantizer ratio (default=150)\n"); | ||
503 : | fprintf(stderr, " -bqo integer : bframe quantizer offset (default=100)\n"); | ||
504 : | edgomez | 1.2 | fprintf(stderr, " -f float : target framerate (>0)\n"); |
505 : | fprintf(stderr, " -i string : input filename (default=stdin)\n"); | ||
506 : | fprintf(stderr, " -t integer : input data type (yuv=0, pgm=1)\n"); | ||
507 : | fprintf(stderr, " -n integer : number of frames to encode\n"); | ||
508 : | fprintf(stderr, " -q integer : quality ([0..5])\n"); | ||
509 : | edgomez | 1.10 | fprintf(stderr, " -d boolean : save decoder output (0 False*, !=0 True)\n"); |
510 : | edgomez | 1.2 | fprintf(stderr, " -m boolean : save mpeg4 raw stream (0 False*, !=0 True)\n"); |
511 : | fprintf(stderr, " -o string : output container filename (only usefull when -m 1 is used) :\n"); | ||
512 : | fprintf(stderr, " When this option is not used : one file per encoded frame\n"); | ||
513 : | edgomez | 1.11.2.1 | fprintf(stderr, " When this option is used : save to 'string' file\n"); |
514 : | edgomez | 1.2 | fprintf(stderr, " -help : prints this help message\n"); |
515 : | fprintf(stderr, " -quant integer : fixed quantizer (disables -b setting)\n"); | ||
516 : | fprintf(stderr, " (* means default)\n"); | ||
517 : | chl | 1.1 | |
518 : | edgomez | 1.2 | } |
519 : | |||
520 : | /***************************************************************************** | ||
521 : | * Input and output functions | ||
522 : | * | ||
523 : | * the are small and simple routines to read and write PGM and YUV | ||
524 : | * image. It's just for convenience, again nothing specific to XviD | ||
525 : | * | ||
526 : | *****************************************************************************/ | ||
527 : | |||
528 : | static int read_pgmheader(FILE* handle) | ||
529 : | chl | 1.1 | { |
530 : | int bytes,xsize,ysize,depth; | ||
531 : | char dummy[2]; | ||
532 : | |||
533 : | bytes = fread(dummy,1,2,handle); | ||
534 : | |||
535 : | if ( (bytes < 2) || (dummy[0] != 'P') || (dummy[1] != '5' )) | ||
536 : | edgomez | 1.11.2.1 | return(1); |
537 : | edgomez | 1.2 | |
538 : | chl | 1.1 | fscanf(handle,"%d %d %d",&xsize,&ysize,&depth); |
539 : | if ( (xsize > 1440) || (ysize > 2880 ) || (depth != 255) ) | ||
540 : | { | ||
541 : | edgomez | 1.2 | fprintf(stderr,"%d %d %d\n",xsize,ysize,depth); |
542 : | edgomez | 1.11.2.1 | return(2); |
543 : | chl | 1.1 | } |
544 : | if ( (XDIM==0) || (YDIM==0) ) | ||
545 : | edgomez | 1.2 | { |
546 : | XDIM=xsize; | ||
547 : | YDIM=ysize*2/3; | ||
548 : | chl | 1.1 | } |
549 : | |||
550 : | edgomez | 1.11.2.1 | return(0); |
551 : | chl | 1.1 | } |
552 : | |||
553 : | edgomez | 1.2 | static int read_pgmdata(FILE* handle, unsigned char *image) |
554 : | chl | 1.1 | { |
555 : | edgomez | 1.2 | int i; |
556 : | chl | 1.1 | char dummy; |
557 : | edgomez | 1.2 | |
558 : | unsigned char *y = image; | ||
559 : | unsigned char *u = image + XDIM*YDIM; | ||
560 : | unsigned char *v = image + XDIM*YDIM + XDIM/2*YDIM/2; | ||
561 : | chl | 1.1 | |
562 : | edgomez | 1.2 | /* read Y component of picture */ |
563 : | fread(y, 1, XDIM*YDIM, handle); | ||
564 : | chl | 1.1 | |
565 : | edgomez | 1.2 | for (i=0;i<YDIM/2;i++) |
566 : | chl | 1.1 | { |
567 : | edgomez | 1.2 | /* read U */ |
568 : | fread(u, 1, XDIM/2, handle); | ||
569 : | |||
570 : | /* read V */ | ||
571 : | fread(v, 1, XDIM/2, handle); | ||
572 : | |||
573 : | /* Update pointers */ | ||
574 : | u += XDIM/2; | ||
575 : | v += XDIM/2; | ||
576 : | chl | 1.1 | } |
577 : | edgomez | 1.2 | |
578 : | /* I don't know why, but this seems needed */ | ||
579 : | fread(&dummy, 1, 1, handle); | ||
580 : | |||
581 : | edgomez | 1.11.2.1 | return(0); |
582 : | chl | 1.1 | } |
583 : | |||
584 : | edgomez | 1.2 | static int read_yuvdata(FILE* handle, unsigned char *image) |
585 : | { | ||
586 : | chl | 1.1 | |
587 : | edgomez | 1.2 | if (fread(image, 1, IMAGE_SIZE(XDIM, YDIM), handle) != (unsigned int)IMAGE_SIZE(XDIM, YDIM)) |
588 : | edgomez | 1.11.2.1 | return(1); |
589 : | chl | 1.1 | else |
590 : | edgomez | 1.11.2.1 | return(0); |
591 : | chl | 1.1 | } |
592 : | |||
593 : | edgomez | 1.2 | /***************************************************************************** |
594 : | * Routines for encoding: init encoder, frame step, release encoder | ||
595 : | ****************************************************************************/ | ||
596 : | chl | 1.1 | |
597 : | #define FRAMERATE_INCR 1001 | ||
598 : | |||
599 : | edgomez | 1.2 | /* Initialize encoder for first use, pass all needed parameters to the codec */ |
600 : | static int enc_init(int use_assembler) | ||
601 : | { | ||
602 : | chl | 1.1 | int xerr; |
603 : | |||
604 : | edgomez | 1.11.2.1 | xvid_gbl_init_t xvid_gbl_init; |
605 : | xvid_enc_create_t xvid_enc_create; | ||
606 : | |||
607 : | /*------------------------------------------------------------------------ | ||
608 : | * XviD core initialization | ||
609 : | *----------------------------------------------------------------------*/ | ||
610 : | chl | 1.1 | |
611 : | edgomez | 1.11.2.1 | /* Set version -- version checking will done by xvidcore*/ |
612 : | xvid_gbl_init.version = XVID_VERSION; | ||
613 : | |||
614 : | /* Do we have to enable ASM optimizations ? */ | ||
615 : | edgomez | 1.2 | if(use_assembler) { |
616 : | chl | 1.1 | |
617 : | suxen_drol | 1.11 | #ifdef ARCH_IS_IA64 |
618 : | edgomez | 1.11.2.1 | xvid_gbl_init.cpu_flags = XVID_CPU_FORCE | XVID_CPU_IA64; |
619 : | chl | 1.1 | #else |
620 : | edgomez | 1.11.2.1 | xvid_gbl_init.cpu_flags = 0; |
621 : | chl | 1.1 | #endif |
622 : | edgomez | 1.2 | } |
623 : | else { | ||
624 : | edgomez | 1.11.2.1 | xvid_gbl_init.cpu_flags = XVID_CPU_FORCE; |
625 : | edgomez | 1.2 | } |
626 : | chl | 1.1 | |
627 : | edgomez | 1.11.2.1 | /* Initialize XviD core -- Should be done once per __process__ */ |
628 : | xvid_global(NULL, XVID_GBL_INIT, &xvid_gbl_init, NULL); | ||
629 : | chl | 1.1 | |
630 : | edgomez | 1.11.2.1 | /*------------------------------------------------------------------------ |
631 : | * XviD encoder initialization | ||
632 : | *----------------------------------------------------------------------*/ | ||
633 : | |||
634 : | /* Version again */ | ||
635 : | xvid_enc_create.version = XVID_VERSION; | ||
636 : | |||
637 : | /* Width and Height of input frames */ | ||
638 : | xvid_enc_create.width = XDIM; | ||
639 : | xvid_enc_create.height = YDIM; | ||
640 : | |||
641 : | /* No fancy thread tests */ | ||
642 : | xvid_enc_create.num_threads = 0; | ||
643 : | |||
644 : | /* Frame rate - Do some quick float fps = fincr/fbase hack */ | ||
645 : | if ((ARG_FRAMERATE - (int)ARG_FRAMERATE) < SMALL_EPS) { | ||
646 : | xvid_enc_create.fincr = 1; | ||
647 : | xvid_enc_create.fbase = (int)ARG_FRAMERATE; | ||
648 : | } else { | ||
649 : | xvid_enc_create.fincr = FRAMERATE_INCR; | ||
650 : | xvid_enc_create.fbase = (int)(FRAMERATE_INCR * ARG_FRAMERATE); | ||
651 : | chl | 1.1 | } |
652 : | edgomez | 1.11.2.1 | |
653 : | /* Maximum key frame interval */ | ||
654 : | xvid_enc_create.max_key_interval = (int)ARG_FRAMERATE*10; | ||
655 : | |||
656 : | /* Bframes settings */ | ||
657 : | xvid_enc_create.max_bframes = ARG_MAXBFRAMES; | ||
658 : | xvid_enc_create.bquant_ratio = ARG_BQRATIO; | ||
659 : | xvid_enc_create.bquant_offset = ARG_BQOFFSET; | ||
660 : | |||
661 : | /* Dropping ratio frame -- we don't need that */ | ||
662 : | xvid_enc_create.frame_drop_ratio = 0; | ||
663 : | |||
664 : | /* Global encoder options */ | ||
665 : | xvid_enc_create.global = 0; | ||
666 : | chl | 1.1 | |
667 : | edgomez | 1.2 | /* I use a small value here, since will not encode whole movies, but short clips */ |
668 : | edgomez | 1.11.2.1 | xerr = xvid_encore(NULL, XVID_ENC_CREATE, &xvid_enc_create, NULL); |
669 : | chl | 1.1 | |
670 : | edgomez | 1.11.2.1 | /* Retrieve the encoder instance from the structure */ |
671 : | enc_handle = xvid_enc_create.handle; | ||
672 : | chl | 1.1 | |
673 : | edgomez | 1.11.2.1 | return(xerr); |
674 : | chl | 1.1 | } |
675 : | |||
676 : | edgomez | 1.2 | static int enc_stop() |
677 : | { | ||
678 : | int xerr; | ||
679 : | chl | 1.1 | |
680 : | edgomez | 1.11.2.1 | /* Destroy the encoder instance */ |
681 : | chl | 1.1 | xerr = xvid_encore(enc_handle, XVID_ENC_DESTROY, NULL, NULL); |
682 : | edgomez | 1.2 | |
683 : | edgomez | 1.11.2.1 | return(xerr); |
684 : | chl | 1.1 | } |
685 : | |||
686 : | edgomez | 1.11.2.1 | static int enc_main(unsigned char* image, |
687 : | unsigned char* bitstream, | ||
688 : | long *frametype) | ||
689 : | edgomez | 1.2 | { |
690 : | edgomez | 1.11.2.1 | int ret; |
691 : | chl | 1.1 | |
692 : | edgomez | 1.11.2.1 | xvid_enc_frame_t xvid_enc_frame; |
693 : | xvid_enc_stats_t xvid_enc_stats[2]; | ||
694 : | chl | 1.1 | |
695 : | edgomez | 1.11.2.1 | /* Version for the frame and the stats */ |
696 : | xvid_enc_frame.version = XVID_VERSION; | ||
697 : | xvid_enc_stats[0].version = XVID_VERSION; | ||
698 : | xvid_enc_stats[1].version = XVID_VERSION; | ||
699 : | chl | 1.1 | |
700 : | edgomez | 1.11.2.1 | /* Bind output buffer */ |
701 : | xvid_enc_frame.bitstream = bitstream; | ||
702 : | xvid_enc_frame.length = -1; | ||
703 : | chl | 1.1 | |
704 : | edgomez | 1.11.2.1 | /* Initialize input image fields */ |
705 : | xvid_enc_frame.input.plane[0] = image; | ||
706 : | xvid_enc_frame.input.csp = XVID_CSP_I420; | ||
707 : | xvid_enc_frame.input.stride[0] = XDIM; | ||
708 : | chl | 1.1 | |
709 : | edgomez | 1.11.2.1 | /* Set up core's general features */ |
710 : | xvid_enc_frame.vol_flags = vol_presets[ARG_QUALITY]; | ||
711 : | chl | 1.1 | |
712 : | edgomez | 1.11.2.1 | /* Set up core's general features */ |
713 : | xvid_enc_frame.vop_flags = vop_presets[ARG_QUALITY]; | ||
714 : | edgomez | 1.6 | |
715 : | edgomez | 1.11.2.1 | /* Frame type -- let core decide for us */ |
716 : | xvid_enc_frame.type = XVID_TYPE_AUTO; | ||
717 : | edgomez | 1.6 | |
718 : | edgomez | 1.11.2.1 | /* Force the right quantizer */ |
719 : | xvid_enc_frame.quant = ARG_QUANTI; | ||
720 : | xvid_enc_frame.bquant = 0; | ||
721 : | |||
722 : | /* Set up motion estimation flags */ | ||
723 : | xvid_enc_frame.motion = motion_presets[ARG_QUALITY]; | ||
724 : | edgomez | 1.6 | |
725 : | edgomez | 1.11.2.1 | /* We don't use special matrices */ |
726 : | xvid_enc_frame.quant_intra_matrix = NULL; | ||
727 : | xvid_enc_frame.quant_inter_matrix = NULL; | ||
728 : | edgomez | 1.6 | |
729 : | edgomez | 1.11.2.1 | ret = xvid_encore(enc_handle, XVID_ENC_ENCODE, &xvid_enc_frame, &xvid_enc_stats); |
730 : | chl | 1.1 | |
731 : | edgomez | 1.11.2.1 | *frametype = xvid_enc_stats[0].type; |
732 : | chl | 1.1 | |
733 : | edgomez | 1.11.2.1 | return(ret); |
734 : | chl | 1.1 | } |
No admin address has been configured | ViewVC Help |
Powered by ViewVC 1.0.4 |