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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

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