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