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