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