[svn] / branches / dev-api-3 / xvidcore / examples / xvid_encraw.c Repository:
ViewVC logotype

Annotation of /branches/dev-api-3/xvidcore/examples/xvid_encraw.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 822 - (view) (download)

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

No admin address has been configured
ViewVC Help
Powered by ViewVC 1.0.4