Parent Directory | Revision Log
Revision 1.8 - (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.2 | * Copyright(C) 2002 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.8 | * $Id: xvid_encraw.c,v 1.7 2002/12/26 14:36:06 edgomez 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 : | #ifndef _MSC_VER | ||
42 : | #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 : | static int const motion_presets[7] = { | ||
54 : | edgomez | 1.5 | 0, /* Q 0 */ |
55 : | PMV_EARLYSTOP16, /* Q 1 */ | ||
56 : | PMV_EARLYSTOP16, /* Q 2 */ | ||
57 : | PMV_EARLYSTOP16 | PMV_HALFPELREFINE16, /* Q 3 */ | ||
58 : | PMV_EARLYSTOP16 | PMV_HALFPELREFINE16, /* Q 4 */ | ||
59 : | PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 | PMV_EARLYSTOP8 | /* Q 5 */ | ||
60 : | edgomez | 1.2 | PMV_HALFPELREFINE8, |
61 : | edgomez | 1.5 | PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 | PMV_EXTSEARCH16 | /* Q 6 */ |
62 : | edgomez | 1.2 | PMV_USESQUARES16 | PMV_EARLYSTOP8 | PMV_HALFPELREFINE8 |
63 : | }; | ||
64 : | |||
65 : | static int const general_presets[7] = { | ||
66 : | edgomez | 1.5 | XVID_H263QUANT, /* Q 0 */ |
67 : | XVID_MPEGQUANT, /* Q 1 */ | ||
68 : | XVID_H263QUANT, /* Q 2 */ | ||
69 : | XVID_H263QUANT | XVID_HALFPEL, /* Q 3 */ | ||
70 : | XVID_H263QUANT | XVID_HALFPEL | XVID_INTER4V, /* Q 4 */ | ||
71 : | XVID_H263QUANT | XVID_HALFPEL | XVID_INTER4V, /* Q 5 */ | ||
72 : | XVID_H263QUANT | XVID_HALFPEL | XVID_INTER4V /* Q 6 */ | ||
73 : | edgomez | 1.2 | }; |
74 : | chl | 1.1 | |
75 : | |||
76 : | edgomez | 1.2 | /***************************************************************************** |
77 : | * Command line global variables | ||
78 : | ****************************************************************************/ | ||
79 : | |||
80 : | /* Maximum number of frames to encode */ | ||
81 : | #define ABS_MAXFRAMENR 9999 | ||
82 : | |||
83 : | edgomez | 1.6 | /* HINTMODEs */ |
84 : | #define HINT_MODE_NONE 0 | ||
85 : | #define HINT_MODE_GET 1 | ||
86 : | #define HINT_MODE_SET 2 | ||
87 : | #define HINT_FILE "hints.mv" | ||
88 : | |||
89 : | edgomez | 1.2 | static int ARG_BITRATE = 900; |
90 : | static int ARG_QUANTI = 0; | ||
91 : | static int ARG_QUALITY = 6; | ||
92 : | static int ARG_MINQUANT = 1; | ||
93 : | static int ARG_MAXQUANT = 31; | ||
94 : | static float ARG_FRAMERATE = 25.00f; | ||
95 : | static int ARG_MAXFRAMENR = ABS_MAXFRAMENR; | ||
96 : | static char *ARG_INPUTFILE = NULL; | ||
97 : | static int ARG_INPUTTYPE = 0; | ||
98 : | static int ARG_SAVEMPEGSTREAM = 0; | ||
99 : | static int ARG_OUTPUTTYPE = 0; | ||
100 : | static char *ARG_OUTPUTFILE = NULL; | ||
101 : | edgomez | 1.6 | static int ARG_HINTMODE = HINT_MODE_NONE; |
102 : | edgomez | 1.2 | static int XDIM = 0; |
103 : | static int YDIM = 0; | ||
104 : | #define IMAGE_SIZE(x,y) ((x)*(y)*3/2) | ||
105 : | |||
106 : | #define MAX(A,B) ( ((A)>(B)) ? (A) : (B) ) | ||
107 : | #define SMALL_EPS 1e-10 | ||
108 : | |||
109 : | #define LONG_PACK(a,b,c,d) ((long) (((long)(a))<<24) | (((long)(b))<<16) | \ | ||
110 : | (((long)(c))<<8) |((long)(d))) | ||
111 : | |||
112 : | #define SWAP(a) ( (((a)&0x000000ff)<<24) | (((a)&0x0000ff00)<<8) | \ | ||
113 : | (((a)&0x00ff0000)>>8) | (((a)&0xff000000)>>24) ) | ||
114 : | |||
115 : | /**************************************************************************** | ||
116 : | * Nasty global vars ;-) | ||
117 : | ***************************************************************************/ | ||
118 : | |||
119 : | static int i,filenr = 0; | ||
120 : | |||
121 : | /* the path where to save output */ | ||
122 : | static char filepath[256] = "./"; | ||
123 : | |||
124 : | /* 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 : | static int enc_main(unsigned char* image, unsigned char* bitstream, | ||
146 : | edgomez | 1.6 | unsigned char *hints_buffer, |
147 : | long *streamlength, long* frametype, long *hints_size); | ||
148 : | edgomez | 1.2 | |
149 : | /***************************************************************************** | ||
150 : | * Main function | ||
151 : | ****************************************************************************/ | ||
152 : | |||
153 : | int main(int argc, char *argv[]) | ||
154 : | { | ||
155 : | |||
156 : | unsigned char *mp4_buffer = NULL; | ||
157 : | unsigned char *in_buffer = NULL; | ||
158 : | unsigned char *out_buffer = NULL; | ||
159 : | edgomez | 1.6 | unsigned char *hints_buffer = NULL; |
160 : | edgomez | 1.2 | |
161 : | double enctime; | ||
162 : | double totalenctime=0.; | ||
163 : | |||
164 : | long totalsize; | ||
165 : | edgomez | 1.6 | long hints_size; |
166 : | edgomez | 1.2 | int status; |
167 : | edgomez | 1.6 | long frame_type; |
168 : | long bigendian; | ||
169 : | edgomez | 1.2 | |
170 : | edgomez | 1.6 | long m4v_size; |
171 : | edgomez | 1.2 | int use_assembler=0; |
172 : | |||
173 : | char filename[256]; | ||
174 : | |||
175 : | FILE *in_file = stdin; | ||
176 : | FILE *out_file = NULL; | ||
177 : | edgomez | 1.6 | FILE *hints_file = NULL; |
178 : | edgomez | 1.2 | |
179 : | edgomez | 1.7 | printf("xvid_encraw - raw mpeg4 bitstream encoder "); |
180 : | edgomez | 1.2 | printf("written by Christoph Lampert 2002\n\n"); |
181 : | chl | 1.1 | |
182 : | edgomez | 1.2 | /***************************************************************************** |
183 : | * Command line parsing | ||
184 : | ****************************************************************************/ | ||
185 : | chl | 1.1 | |
186 : | edgomez | 1.2 | for (i=1; i< argc; i++) { |
187 : | |||
188 : | if (strcmp("-asm", argv[i]) == 0 ) { | ||
189 : | use_assembler = 1; | ||
190 : | } | ||
191 : | else if (strcmp("-w", argv[i]) == 0 && i < argc - 1 ) { | ||
192 : | i++; | ||
193 : | XDIM = atoi(argv[i]); | ||
194 : | } | ||
195 : | else if (strcmp("-h", argv[i]) == 0 && i < argc - 1 ) { | ||
196 : | i++; | ||
197 : | YDIM = atoi(argv[i]); | ||
198 : | } | ||
199 : | else if (strcmp("-b", argv[i]) == 0 && i < argc - 1 ) { | ||
200 : | i++; | ||
201 : | ARG_BITRATE = atoi(argv[i]); | ||
202 : | } | ||
203 : | else if (strcmp("-q", argv[i]) == 0 && i < argc - 1 ) { | ||
204 : | i++; | ||
205 : | ARG_QUALITY = atoi(argv[i]); | ||
206 : | } | ||
207 : | else if (strcmp("-f", argv[i]) == 0 && i < argc - 1 ) { | ||
208 : | i++; | ||
209 : | ARG_FRAMERATE = (float)atof(argv[i]); | ||
210 : | } | ||
211 : | else if (strcmp("-i", argv[i]) == 0 && i < argc - 1 ) { | ||
212 : | i++; | ||
213 : | ARG_INPUTFILE = argv[i]; | ||
214 : | } | ||
215 : | else if (strcmp("-t", argv[i]) == 0 && i < argc - 1 ) { | ||
216 : | i++; | ||
217 : | ARG_INPUTTYPE = atoi(argv[i]); | ||
218 : | } | ||
219 : | else if(strcmp("-n", argv[i]) == 0 && i < argc - 1 ) { | ||
220 : | i++; | ||
221 : | ARG_MAXFRAMENR = atoi(argv[i]); | ||
222 : | } | ||
223 : | else if (strcmp("-quant", argv[i]) == 0 && i < argc - 1 ) { | ||
224 : | i++; | ||
225 : | ARG_QUANTI = atoi(argv[i]); | ||
226 : | } | ||
227 : | else if (strcmp("-m", argv[i]) == 0 && i < argc - 1 ) { | ||
228 : | i++; | ||
229 : | ARG_SAVEMPEGSTREAM = atoi(argv[i]); | ||
230 : | } | ||
231 : | else if (strcmp("-mt", argv[i]) == 0 && i < argc - 1 ) { | ||
232 : | i++; | ||
233 : | ARG_OUTPUTTYPE = atoi(argv[i]); | ||
234 : | } | ||
235 : | edgomez | 1.6 | else if (strcmp("-mv", argv[i]) == 0 && i < argc - 1 ) { |
236 : | i++; | ||
237 : | ARG_HINTMODE = atoi(argv[i]); | ||
238 : | } | ||
239 : | edgomez | 1.2 | else if (strcmp("-o", argv[i]) == 0 && i < argc - 1 ) { |
240 : | i++; | ||
241 : | ARG_OUTPUTFILE = argv[i]; | ||
242 : | } | ||
243 : | else if (strcmp("-help", argv[i])) { | ||
244 : | usage(); | ||
245 : | return(0); | ||
246 : | } | ||
247 : | else { | ||
248 : | usage(); | ||
249 : | exit(-1); | ||
250 : | } | ||
251 : | |||
252 : | } | ||
253 : | |||
254 : | /***************************************************************************** | ||
255 : | * Arguments checking | ||
256 : | ****************************************************************************/ | ||
257 : | |||
258 : | if (XDIM <= 0 || XDIM >= 2048 || YDIM <=0 || YDIM >= 2048 ) { | ||
259 : | fprintf(stderr, "Trying to retreive width and height from PGM header\n"); | ||
260 : | ARG_INPUTTYPE = 1; /* pgm */ | ||
261 : | } | ||
262 : | |||
263 : | if ( ARG_QUALITY < 0 || ARG_QUALITY > 6) { | ||
264 : | fprintf(stderr,"Wrong Quality\n"); | ||
265 : | return -1; | ||
266 : | } | ||
267 : | chl | 1.1 | |
268 : | edgomez | 1.2 | if ( ARG_BITRATE <= 0 && ARG_QUANTI == 0) { |
269 : | fprintf(stderr,"Wrong Bitrate\n"); | ||
270 : | return -1; | ||
271 : | } | ||
272 : | chl | 1.1 | |
273 : | edgomez | 1.2 | if ( ARG_FRAMERATE <= 0) { |
274 : | fprintf(stderr,"Wrong Framerate %s \n",argv[5]); | ||
275 : | return -1; | ||
276 : | } | ||
277 : | chl | 1.1 | |
278 : | edgomez | 1.2 | if ( ARG_MAXFRAMENR <= 0) { |
279 : | fprintf(stderr,"Wrong number of frames\n"); | ||
280 : | return -1; | ||
281 : | } | ||
282 : | chl | 1.1 | |
283 : | edgomez | 1.6 | if ( ARG_HINTMODE != HINT_MODE_NONE && |
284 : | ARG_HINTMODE != HINT_MODE_GET && | ||
285 : | ARG_HINTMODE != HINT_MODE_SET) | ||
286 : | ARG_HINTMODE = HINT_MODE_NONE; | ||
287 : | |||
288 : | if( ARG_HINTMODE != HINT_MODE_NONE) { | ||
289 : | char *rights = "rb"; | ||
290 : | |||
291 : | /* | ||
292 : | * If we are getting hints from core, we will have to write them to | ||
293 : | * hint file | ||
294 : | */ | ||
295 : | if(ARG_HINTMODE == HINT_MODE_GET) | ||
296 : | rights = "w+b"; | ||
297 : | |||
298 : | /* Open the hint file */ | ||
299 : | hints_file = fopen(HINT_FILE, rights); | ||
300 : | if(hints_file == NULL) { | ||
301 : | fprintf(stderr, "Error opening input file %s\n", HINT_FILE); | ||
302 : | return -1; | ||
303 : | } | ||
304 : | |||
305 : | /* Allocate hint memory space, we will be using rawhints */ | ||
306 : | /* NB : Hope 1Mb is enough */ | ||
307 : | if((hints_buffer = malloc(1024*1024)) == NULL) { | ||
308 : | fprintf(stderr, "Memory allocation error\n"); | ||
309 : | return -1; | ||
310 : | } | ||
311 : | |||
312 : | } | ||
313 : | |||
314 : | edgomez | 1.2 | if ( ARG_INPUTFILE == NULL || strcmp(ARG_INPUTFILE, "stdin") == 0) { |
315 : | in_file = stdin; | ||
316 : | } | ||
317 : | else { | ||
318 : | chl | 1.1 | |
319 : | edgomez | 1.2 | in_file = fopen(ARG_INPUTFILE, "rb"); |
320 : | if (in_file == NULL) { | ||
321 : | fprintf(stderr, "Error opening input file %s\n", ARG_INPUTFILE); | ||
322 : | return -1; | ||
323 : | } | ||
324 : | } | ||
325 : | |||
326 : | if (ARG_INPUTTYPE) { | ||
327 : | if (read_pgmheader(in_file)) { | ||
328 : | fprintf(stderr, "Wrong input format, I want YUV encapsulated in PGM\n"); | ||
329 : | return -1; | ||
330 : | } | ||
331 : | } | ||
332 : | |||
333 : | /* now we know the sizes, so allocate memory */ | ||
334 : | |||
335 : | in_buffer = (unsigned char *) malloc(IMAGE_SIZE(XDIM,YDIM)); | ||
336 : | if (!in_buffer) | ||
337 : | goto free_all_memory; | ||
338 : | |||
339 : | /* this should really be enough memory ! */ | ||
340 : | mp4_buffer = (unsigned char *) malloc(IMAGE_SIZE(XDIM,YDIM)*2); | ||
341 : | if (!mp4_buffer) | ||
342 : | goto free_all_memory; | ||
343 : | |||
344 : | /***************************************************************************** | ||
345 : | * XviD PART Start | ||
346 : | ****************************************************************************/ | ||
347 : | |||
348 : | |||
349 : | status = enc_init(use_assembler); | ||
350 : | if (status) | ||
351 : | { | ||
352 : | fprintf(stderr, "Encore INIT problem, return value %d\n", status); | ||
353 : | goto release_all; | ||
354 : | } | ||
355 : | |||
356 : | /***************************************************************************** | ||
357 : | * Main loop | ||
358 : | ****************************************************************************/ | ||
359 : | |||
360 : | edgomez | 1.3 | totalsize = LONG_PACK('M','P','4','U'); |
361 : | if(*((char *)(&totalsize)) == 'M') | ||
362 : | bigendian = 1; | ||
363 : | else | ||
364 : | bigendian = 0; | ||
365 : | |||
366 : | edgomez | 1.2 | if (ARG_SAVEMPEGSTREAM && (ARG_OUTPUTTYPE || ARG_OUTPUTFILE)) { |
367 : | |||
368 : | if (ARG_OUTPUTFILE == NULL && ARG_OUTPUTTYPE) | ||
369 : | ARG_OUTPUTFILE = "stream.mp4u"; | ||
370 : | else if(ARG_OUTPUTFILE == NULL && !ARG_OUTPUTTYPE) | ||
371 : | ARG_OUTPUTFILE = "stream.m4v"; | ||
372 : | |||
373 : | if((out_file = fopen(ARG_OUTPUTFILE, "w+b")) == NULL) { | ||
374 : | fprintf(stderr, "Error opening output file %s\n", ARG_OUTPUTFILE); | ||
375 : | goto release_all; | ||
376 : | } | ||
377 : | |||
378 : | /* Write header */ | ||
379 : | if (ARG_OUTPUTTYPE) { | ||
380 : | |||
381 : | edgomez | 1.3 | long test = LONG_PACK('M','P','4','U'); |
382 : | edgomez | 1.2 | |
383 : | test = (!bigendian)?SWAP(test):test; | ||
384 : | |||
385 : | fwrite(&test, sizeof(test), 1, out_file); | ||
386 : | |||
387 : | } | ||
388 : | |||
389 : | } | ||
390 : | else { | ||
391 : | out_file = NULL; | ||
392 : | } | ||
393 : | |||
394 : | /***************************************************************************** | ||
395 : | * Encoding loop | ||
396 : | ****************************************************************************/ | ||
397 : | |||
398 : | totalsize = 0; | ||
399 : | |||
400 : | do { | ||
401 : | |||
402 : | if (ARG_INPUTTYPE) | ||
403 : | edgomez | 1.5 | status = read_pgmdata(in_file, in_buffer); /* read PGM data (YUV-format) */ |
404 : | edgomez | 1.2 | else |
405 : | edgomez | 1.5 | status = read_yuvdata(in_file, in_buffer); /* read raw data (YUV-format) */ |
406 : | edgomez | 1.2 | |
407 : | if (status) | ||
408 : | { | ||
409 : | /* Couldn't read image, most likely end-of-file */ | ||
410 : | continue; | ||
411 : | } | ||
412 : | |||
413 : | /***************************************************************************** | ||
414 : | edgomez | 1.6 | * Read hints from file |
415 : | ****************************************************************************/ | ||
416 : | |||
417 : | if(ARG_HINTMODE == HINT_MODE_SET) { | ||
418 : | fread(&hints_size, 1, sizeof(long), hints_file); | ||
419 : | hints_size = (!bigendian)?SWAP(hints_size):hints_size; | ||
420 : | fread(hints_buffer, 1, hints_size, hints_file); | ||
421 : | } | ||
422 : | |||
423 : | /***************************************************************************** | ||
424 : | edgomez | 1.2 | * Encode and decode this frame |
425 : | ****************************************************************************/ | ||
426 : | |||
427 : | enctime = msecond(); | ||
428 : | edgomez | 1.6 | status = enc_main(in_buffer, mp4_buffer, hints_buffer, |
429 : | &m4v_size, &frame_type, &hints_size); | ||
430 : | edgomez | 1.2 | enctime = msecond() - enctime; |
431 : | |||
432 : | totalenctime += enctime; | ||
433 : | totalsize += m4v_size; | ||
434 : | |||
435 : | printf("Frame %5d: intra %1d, enctime=%6.1f ms, size=%6dbytes\n", | ||
436 : | (int)filenr, (int)frame_type, (float)enctime, (int)m4v_size); | ||
437 : | |||
438 : | edgomez | 1.6 | /***************************************************************************** |
439 : | * Save hints to file | ||
440 : | ****************************************************************************/ | ||
441 : | |||
442 : | if(ARG_HINTMODE == HINT_MODE_GET) { | ||
443 : | hints_size = (!bigendian)?SWAP(hints_size):hints_size; | ||
444 : | fwrite(&hints_size, 1, sizeof(long), hints_file); | ||
445 : | hints_size = (!bigendian)?SWAP(hints_size):hints_size; | ||
446 : | fwrite(hints_buffer, 1, hints_size, hints_file); | ||
447 : | } | ||
448 : | |||
449 : | /***************************************************************************** | ||
450 : | * Save stream to file | ||
451 : | ****************************************************************************/ | ||
452 : | |||
453 : | edgomez | 1.2 | if (ARG_SAVEMPEGSTREAM) |
454 : | { | ||
455 : | /* Save single files */ | ||
456 : | if (out_file == NULL) { | ||
457 : | sprintf(filename, "%sframe%05d.m4v", filepath, filenr); | ||
458 : | out_file = fopen(filename, "wb"); | ||
459 : | fwrite(mp4_buffer, m4v_size, 1, out_file); | ||
460 : | fclose(out_file); | ||
461 : | out_file = NULL; | ||
462 : | } | ||
463 : | else { | ||
464 : | /* Using mp4u container */ | ||
465 : | if (ARG_OUTPUTTYPE) { | ||
466 : | long size = m4v_size; | ||
467 : | size = (!bigendian)?SWAP(size):size; | ||
468 : | fwrite(&size, sizeof(size), 1, out_file); | ||
469 : | } | ||
470 : | |||
471 : | /* Write mp4 data */ | ||
472 : | fwrite(mp4_buffer, m4v_size, 1, out_file); | ||
473 : | |||
474 : | } | ||
475 : | } | ||
476 : | |||
477 : | /* Read the header if it's pgm stream */ | ||
478 : | if (ARG_INPUTTYPE) | ||
479 : | status = read_pgmheader(in_file); | ||
480 : | |||
481 : | filenr++; | ||
482 : | chl | 1.1 | |
483 : | edgomez | 1.2 | } while ( (!status) && (filenr<ARG_MAXFRAMENR) ); |
484 : | chl | 1.1 | |
485 : | |||
486 : | edgomez | 1.2 | |
487 : | /***************************************************************************** | ||
488 : | * Calculate totals and averages for output, print results | ||
489 : | ****************************************************************************/ | ||
490 : | chl | 1.1 | |
491 : | edgomez | 1.2 | totalsize /= filenr; |
492 : | totalenctime /= filenr; | ||
493 : | |||
494 : | printf("Avg: enctime %5.2f ms, %5.2f fps, filesize %7d bytes\n", | ||
495 : | totalenctime, 1000/totalenctime, (int)totalsize); | ||
496 : | |||
497 : | /***************************************************************************** | ||
498 : | * XviD PART Stop | ||
499 : | ****************************************************************************/ | ||
500 : | |||
501 : | release_all: | ||
502 : | |||
503 : | if (enc_handle) | ||
504 : | { | ||
505 : | status = enc_stop(); | ||
506 : | if (status) | ||
507 : | fprintf(stderr, "Encore RELEASE problem return value %d\n", status); | ||
508 : | } | ||
509 : | chl | 1.1 | |
510 : | edgomez | 1.6 | if(in_file) |
511 : | fclose(in_file); | ||
512 : | edgomez | 1.2 | if(out_file) |
513 : | fclose(out_file); | ||
514 : | edgomez | 1.6 | if(hints_file) |
515 : | fclose(hints_file); | ||
516 : | chl | 1.1 | |
517 : | edgomez | 1.2 | free_all_memory: |
518 : | free(out_buffer); | ||
519 : | free(mp4_buffer); | ||
520 : | free(in_buffer); | ||
521 : | edgomez | 1.6 | if(hints_buffer) free(hints_buffer); |
522 : | chl | 1.1 | |
523 : | edgomez | 1.2 | return 0; |
524 : | |||
525 : | } | ||
526 : | chl | 1.1 | |
527 : | |||
528 : | edgomez | 1.2 | /***************************************************************************** |
529 : | * "statistical" functions | ||
530 : | * | ||
531 : | * these are not needed for encoding or decoding, but for measuring | ||
532 : | * time and quality, there in nothing specific to XviD in these | ||
533 : | * | ||
534 : | *****************************************************************************/ | ||
535 : | |||
536 : | /* Return time elapsed time in miliseconds since the program started */ | ||
537 : | static double msecond() | ||
538 : | chl | 1.1 | { |
539 : | edgomez | 1.8 | #ifndef WIN32 |
540 : | chl | 1.1 | struct timeval tv; |
541 : | gettimeofday(&tv, 0); | ||
542 : | edgomez | 1.2 | return tv.tv_sec*1.0e3 + tv.tv_usec * 1.0e-3; |
543 : | #else | ||
544 : | clock_t clk; | ||
545 : | clk = clock(); | ||
546 : | return clk * 1000 / CLOCKS_PER_SEC; | ||
547 : | #endif | ||
548 : | chl | 1.1 | } |
549 : | |||
550 : | edgomez | 1.2 | /***************************************************************************** |
551 : | * Usage message | ||
552 : | *****************************************************************************/ | ||
553 : | |||
554 : | static void usage() | ||
555 : | { | ||
556 : | |||
557 : | fprintf(stderr, "Usage : xvid_stat [OPTIONS]\n"); | ||
558 : | fprintf(stderr, "Options :\n"); | ||
559 : | fprintf(stderr, " -w integer : frame width ([1.2048])\n"); | ||
560 : | fprintf(stderr, " -h integer : frame height ([1.2048])\n"); | ||
561 : | fprintf(stderr, " -b integer : target bitrate (>0 | default=900kbit)\n"); | ||
562 : | fprintf(stderr, " -f float : target framerate (>0)\n"); | ||
563 : | fprintf(stderr, " -i string : input filename (default=stdin)\n"); | ||
564 : | 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 : | fprintf(stderr, " -d boolean : save decoder output (0 False*, !=0 True)\n"); | ||
568 : | fprintf(stderr, " -m boolean : save mpeg4 raw stream (0 False*, !=0 True)\n"); | ||
569 : | 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 : | fprintf(stderr, " When this option is used :\n"); | ||
572 : | fprintf(stderr, " + stream.m4v with -mt 0\n"); | ||
573 : | fprintf(stderr, " + stream.mp4u with -mt 1\n"); | ||
574 : | fprintf(stderr, " -mt integer : output type (m4v=0, mp4u=1)\n"); | ||
575 : | edgomez | 1.6 | fprintf(stderr, " -mv integer : Use motion vector hints (no hints=0, get hints=1, set hints=2)\n"); |
576 : | edgomez | 1.2 | fprintf(stderr, " -help : prints this help message\n"); |
577 : | fprintf(stderr, " -quant integer : fixed quantizer (disables -b setting)\n"); | ||
578 : | fprintf(stderr, " (* means default)\n"); | ||
579 : | chl | 1.1 | |
580 : | edgomez | 1.2 | } |
581 : | |||
582 : | /***************************************************************************** | ||
583 : | * Input and output functions | ||
584 : | * | ||
585 : | * the are small and simple routines to read and write PGM and YUV | ||
586 : | * image. It's just for convenience, again nothing specific to XviD | ||
587 : | * | ||
588 : | *****************************************************************************/ | ||
589 : | |||
590 : | static int read_pgmheader(FILE* handle) | ||
591 : | chl | 1.1 | { |
592 : | int bytes,xsize,ysize,depth; | ||
593 : | char dummy[2]; | ||
594 : | |||
595 : | bytes = fread(dummy,1,2,handle); | ||
596 : | |||
597 : | if ( (bytes < 2) || (dummy[0] != 'P') || (dummy[1] != '5' )) | ||
598 : | return 1; | ||
599 : | edgomez | 1.2 | |
600 : | chl | 1.1 | fscanf(handle,"%d %d %d",&xsize,&ysize,&depth); |
601 : | if ( (xsize > 1440) || (ysize > 2880 ) || (depth != 255) ) | ||
602 : | { | ||
603 : | edgomez | 1.2 | fprintf(stderr,"%d %d %d\n",xsize,ysize,depth); |
604 : | chl | 1.1 | return 2; |
605 : | } | ||
606 : | if ( (XDIM==0) || (YDIM==0) ) | ||
607 : | edgomez | 1.2 | { |
608 : | XDIM=xsize; | ||
609 : | YDIM=ysize*2/3; | ||
610 : | chl | 1.1 | } |
611 : | |||
612 : | return 0; | ||
613 : | } | ||
614 : | |||
615 : | edgomez | 1.2 | static int read_pgmdata(FILE* handle, unsigned char *image) |
616 : | chl | 1.1 | { |
617 : | edgomez | 1.2 | int i; |
618 : | chl | 1.1 | char dummy; |
619 : | edgomez | 1.2 | |
620 : | unsigned char *y = image; | ||
621 : | unsigned char *u = image + XDIM*YDIM; | ||
622 : | unsigned char *v = image + XDIM*YDIM + XDIM/2*YDIM/2; | ||
623 : | chl | 1.1 | |
624 : | edgomez | 1.2 | /* read Y component of picture */ |
625 : | fread(y, 1, XDIM*YDIM, handle); | ||
626 : | chl | 1.1 | |
627 : | edgomez | 1.2 | for (i=0;i<YDIM/2;i++) |
628 : | chl | 1.1 | { |
629 : | edgomez | 1.2 | /* read U */ |
630 : | fread(u, 1, XDIM/2, handle); | ||
631 : | |||
632 : | /* read V */ | ||
633 : | fread(v, 1, XDIM/2, handle); | ||
634 : | |||
635 : | /* Update pointers */ | ||
636 : | u += XDIM/2; | ||
637 : | v += XDIM/2; | ||
638 : | chl | 1.1 | } |
639 : | edgomez | 1.2 | |
640 : | /* I don't know why, but this seems needed */ | ||
641 : | fread(&dummy, 1, 1, handle); | ||
642 : | |||
643 : | chl | 1.1 | return 0; |
644 : | } | ||
645 : | |||
646 : | edgomez | 1.2 | static int read_yuvdata(FILE* handle, unsigned char *image) |
647 : | { | ||
648 : | chl | 1.1 | |
649 : | edgomez | 1.2 | if (fread(image, 1, IMAGE_SIZE(XDIM, YDIM), handle) != (unsigned int)IMAGE_SIZE(XDIM, YDIM)) |
650 : | chl | 1.1 | return 1; |
651 : | else | ||
652 : | return 0; | ||
653 : | } | ||
654 : | |||
655 : | edgomez | 1.2 | /***************************************************************************** |
656 : | * Routines for encoding: init encoder, frame step, release encoder | ||
657 : | ****************************************************************************/ | ||
658 : | chl | 1.1 | |
659 : | #define FRAMERATE_INCR 1001 | ||
660 : | |||
661 : | edgomez | 1.2 | /* Initialize encoder for first use, pass all needed parameters to the codec */ |
662 : | static int enc_init(int use_assembler) | ||
663 : | { | ||
664 : | chl | 1.1 | int xerr; |
665 : | |||
666 : | XVID_INIT_PARAM xinit; | ||
667 : | XVID_ENC_PARAM xparam; | ||
668 : | |||
669 : | edgomez | 1.2 | if(use_assembler) { |
670 : | chl | 1.1 | |
671 : | #ifdef ARCH_IA64 | ||
672 : | xinit.cpu_flags = XVID_CPU_FORCE | XVID_CPU_IA64; | ||
673 : | #else | ||
674 : | xinit.cpu_flags = 0; | ||
675 : | #endif | ||
676 : | edgomez | 1.2 | } |
677 : | else { | ||
678 : | chl | 1.1 | xinit.cpu_flags = XVID_CPU_FORCE; |
679 : | edgomez | 1.2 | } |
680 : | chl | 1.1 | |
681 : | xvid_init(NULL, 0, &xinit, NULL); | ||
682 : | |||
683 : | xparam.width = XDIM; | ||
684 : | xparam.height = YDIM; | ||
685 : | if ((ARG_FRAMERATE - (int)ARG_FRAMERATE) < SMALL_EPS) | ||
686 : | { | ||
687 : | edgomez | 1.2 | xparam.fincr = 1; |
688 : | xparam.fbase = (int)ARG_FRAMERATE; | ||
689 : | chl | 1.1 | } |
690 : | else | ||
691 : | { | ||
692 : | edgomez | 1.2 | xparam.fincr = FRAMERATE_INCR; |
693 : | xparam.fbase = (int)(FRAMERATE_INCR * ARG_FRAMERATE); | ||
694 : | chl | 1.1 | } |
695 : | xparam.rc_reaction_delay_factor = 16; | ||
696 : | edgomez | 1.2 | xparam.rc_averaging_period = 100; |
697 : | xparam.rc_buffer = 10; | ||
698 : | chl | 1.1 | xparam.rc_bitrate = ARG_BITRATE*1000; |
699 : | edgomez | 1.2 | xparam.min_quantizer = ARG_MINQUANT; |
700 : | xparam.max_quantizer = ARG_MAXQUANT; | ||
701 : | chl | 1.1 | xparam.max_key_interval = (int)ARG_FRAMERATE*10; |
702 : | |||
703 : | edgomez | 1.2 | /* I use a small value here, since will not encode whole movies, but short clips */ |
704 : | chl | 1.1 | |
705 : | xerr = xvid_encore(NULL, XVID_ENC_CREATE, &xparam, NULL); | ||
706 : | enc_handle=xparam.handle; | ||
707 : | |||
708 : | return xerr; | ||
709 : | } | ||
710 : | |||
711 : | edgomez | 1.2 | static int enc_stop() |
712 : | { | ||
713 : | int xerr; | ||
714 : | chl | 1.1 | |
715 : | xerr = xvid_encore(enc_handle, XVID_ENC_DESTROY, NULL, NULL); | ||
716 : | edgomez | 1.2 | return xerr; |
717 : | |||
718 : | chl | 1.1 | } |
719 : | |||
720 : | edgomez | 1.2 | static int enc_main(unsigned char* image, unsigned char* bitstream, |
721 : | edgomez | 1.6 | unsigned char* hints_buffer, |
722 : | long *streamlength, long *frametype, long *hints_size) | ||
723 : | edgomez | 1.2 | { |
724 : | int xerr; | ||
725 : | chl | 1.1 | |
726 : | XVID_ENC_FRAME xframe; | ||
727 : | XVID_ENC_STATS xstats; | ||
728 : | |||
729 : | xframe.bitstream = bitstream; | ||
730 : | edgomez | 1.5 | xframe.length = -1; /* this is written by the routine */ |
731 : | chl | 1.1 | |
732 : | xframe.image = image; | ||
733 : | edgomez | 1.5 | xframe.colorspace = XVID_CSP_YV12; /* defined in <xvid.h> */ |
734 : | chl | 1.1 | |
735 : | edgomez | 1.5 | xframe.intra = -1; /* let the codec decide between I-frame (1) and P-frame (0) */ |
736 : | chl | 1.1 | |
737 : | edgomez | 1.5 | xframe.quant = ARG_QUANTI; /* is quant != 0, use a fixed quant (and ignore bitrate) */ |
738 : | chl | 1.1 | |
739 : | xframe.motion = motion_presets[ARG_QUALITY]; | ||
740 : | xframe.general = general_presets[ARG_QUALITY]; | ||
741 : | xframe.quant_intra_matrix = xframe.quant_inter_matrix = NULL; | ||
742 : | |||
743 : | edgomez | 1.6 | xframe.hint.hintstream = hints_buffer; |
744 : | |||
745 : | if(ARG_HINTMODE == HINT_MODE_SET) { | ||
746 : | xframe.hint.hintlength = *hints_size; | ||
747 : | xframe.hint.rawhints = 0; | ||
748 : | xframe.general |= XVID_HINTEDME_SET; | ||
749 : | } | ||
750 : | |||
751 : | if(ARG_HINTMODE == HINT_MODE_GET) { | ||
752 : | xframe.hint.rawhints = 0; | ||
753 : | xframe.general |= XVID_HINTEDME_GET; | ||
754 : | } | ||
755 : | |||
756 : | chl | 1.1 | xerr = xvid_encore(enc_handle, XVID_ENC_ENCODE, &xframe, &xstats); |
757 : | edgomez | 1.6 | |
758 : | if(ARG_HINTMODE == HINT_MODE_GET) | ||
759 : | *hints_size = xframe.hint.hintlength; | ||
760 : | chl | 1.1 | |
761 : | edgomez | 1.2 | /* |
762 : | * This is statictical data, e.g. for 2-pass. If you are not | ||
763 : | * interested in any of this, you can use NULL instead of &xstats | ||
764 : | */ | ||
765 : | chl | 1.1 | *frametype = xframe.intra; |
766 : | *streamlength = xframe.length; | ||
767 : | |||
768 : | return xerr; | ||
769 : | } |
No admin address has been configured | ViewVC Help |
Powered by ViewVC 1.0.4 |