[svn] / trunk / xvidcore / examples / xvid_encraw.c Repository:
ViewVC logotype

Annotation of /trunk/xvidcore/examples/xvid_encraw.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1623 - (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 1382 * Copyright(C) 2002-2003 Christoph Lampert <gruel@web.de>
7 :     * 2002-2003 Edouard Gomez <ed.gomez@free.fr>
8 :     * 2003 Peter Ross <pross@xvid.org>
9 : chl 376 *
10 : edgomez 558 * This program is free software; you can redistribute it and/or modify
11 :     * it under the terms of the GNU General Public License as published by
12 :     * the Free Software Foundation; either version 2 of the License, or
13 :     * (at your option) any later version.
14 : chl 376 *
15 : edgomez 558 * This program is distributed in the hope that it will be useful,
16 :     * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 :     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 :     * GNU General Public License for more details.
19 : chl 376 *
20 : edgomez 558 * You should have received a copy of the GNU General Public License
21 :     * along with this program; if not, write to the Free Software
22 :     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 :     *
24 : chl 1623 * $Id: xvid_encraw.c,v 1.18 2005-07-05 20:39:52 chl Exp $
25 : edgomez 558 *
26 :     ****************************************************************************/
27 : chl 376
28 : edgomez 558 /*****************************************************************************
29 :     * Application notes :
30 : chl 376 *
31 : edgomez 1382 * A sequence of raw YUV I420 pics or YUV I420 PGM file format is encoded
32 :     * The speed is measured and frames' PSNR are taken from core.
33 : chl 376 *
34 :     * The program is plain C and needs no libraries except for libxvidcore,
35 : edgomez 558 * and maths-lib.
36 : edgomez 1382 *
37 :     * Use ./xvid_encraw -help for a list of options
38 : chl 376 *
39 :     ************************************************************************/
40 :    
41 :     #include <stdio.h>
42 :     #include <stdlib.h>
43 : edgomez 566 #include <string.h>
44 : edgomez 558 #include <math.h>
45 : edgomez 851 #ifndef WIN32
46 : edgomez 558 #include <sys/time.h>
47 :     #else
48 :     #include <time.h>
49 :     #endif
50 : chl 376
51 : edgomez 558 #include "xvid.h"
52 : chl 376
53 : edgomez 1382 #undef READ_PNM
54 :    
55 : edgomez 558 /*****************************************************************************
56 :     * Quality presets
57 :     ****************************************************************************/
58 : chl 376
59 : edgomez 1382 static const int motion_presets[] = {
60 :     /* quality 0 */
61 :     0,
62 :    
63 :     /* quality 1 */
64 :     XVID_ME_ADVANCEDDIAMOND16,
65 :    
66 :     /* quality 2 */
67 :     XVID_ME_ADVANCEDDIAMOND16 | XVID_ME_HALFPELREFINE16,
68 :    
69 :     /* quality 3 */
70 :     XVID_ME_ADVANCEDDIAMOND16 | XVID_ME_HALFPELREFINE16 |
71 :     XVID_ME_ADVANCEDDIAMOND8 | XVID_ME_HALFPELREFINE8,
72 :    
73 :     /* quality 4 */
74 :     XVID_ME_ADVANCEDDIAMOND16 | XVID_ME_HALFPELREFINE16 |
75 :     XVID_ME_ADVANCEDDIAMOND8 | XVID_ME_HALFPELREFINE8 |
76 :     XVID_ME_CHROMA_PVOP | XVID_ME_CHROMA_BVOP,
77 :    
78 :     /* quality 5 */
79 :     XVID_ME_ADVANCEDDIAMOND16 | XVID_ME_HALFPELREFINE16 |
80 :     XVID_ME_ADVANCEDDIAMOND8 | XVID_ME_HALFPELREFINE8 |
81 :     XVID_ME_CHROMA_PVOP | XVID_ME_CHROMA_BVOP,
82 :    
83 :     /* quality 6 */
84 :     XVID_ME_ADVANCEDDIAMOND16 | XVID_ME_HALFPELREFINE16 | XVID_ME_EXTSEARCH16 |
85 :     XVID_ME_ADVANCEDDIAMOND8 | XVID_ME_HALFPELREFINE8 | XVID_ME_EXTSEARCH8 |
86 :     XVID_ME_CHROMA_PVOP | XVID_ME_CHROMA_BVOP,
87 :    
88 : edgomez 558 };
89 : edgomez 1382 #define ME_ELEMENTS (sizeof(motion_presets)/sizeof(motion_presets[0]))
90 : edgomez 558
91 : edgomez 1382 static const int vop_presets[] = {
92 :     /* quality 0 */
93 :     0,
94 :    
95 :     /* quality 1 */
96 :     0,
97 :    
98 :     /* quality 2 */
99 :     XVID_VOP_HALFPEL,
100 :    
101 :     /* quality 3 */
102 :     XVID_VOP_HALFPEL | XVID_VOP_INTER4V,
103 :    
104 :     /* quality 4 */
105 :     XVID_VOP_HALFPEL | XVID_VOP_INTER4V,
106 :    
107 :     /* quality 5 */
108 :     XVID_VOP_HALFPEL | XVID_VOP_INTER4V |
109 :     XVID_VOP_TRELLISQUANT,
110 :    
111 :     /* quality 6 */
112 :     XVID_VOP_HALFPEL | XVID_VOP_INTER4V |
113 :     XVID_VOP_TRELLISQUANT | XVID_VOP_HQACPRED,
114 :    
115 : edgomez 558 };
116 : edgomez 1382 #define VOP_ELEMENTS (sizeof(vop_presets)/sizeof(vop_presets[0]))
117 : chl 376
118 : edgomez 558 /*****************************************************************************
119 :     * Command line global variables
120 :     ****************************************************************************/
121 : chl 376
122 : edgomez 1382 #define MAX_ZONES 64
123 :    
124 :     static xvid_enc_zone_t ZONES[MAX_ZONES];
125 :     static int NUM_ZONES = 0;
126 :    
127 : edgomez 558 /* Maximum number of frames to encode */
128 :     #define ABS_MAXFRAMENR 9999
129 : chl 376
130 : edgomez 1382 static int ARG_STATS = 0;
131 :     static int ARG_DUMP = 0;
132 :     static int ARG_LUMIMASKING = 0;
133 :     static int ARG_BITRATE = 0;
134 :     static int ARG_SINGLE = 0;
135 :     static char *ARG_PASS1 = 0;
136 :     static char *ARG_PASS2 = 0;
137 :     static int ARG_QUALITY = ME_ELEMENTS - 1;
138 : edgomez 558 static float ARG_FRAMERATE = 25.00f;
139 : edgomez 1382 static int ARG_MAXFRAMENR = ABS_MAXFRAMENR;
140 :     static int ARG_MAXKEYINTERVAL = 0;
141 : edgomez 558 static char *ARG_INPUTFILE = NULL;
142 : edgomez 1382 static int ARG_INPUTTYPE = 0;
143 :     static int ARG_SAVEMPEGSTREAM = 0;
144 :     static int ARG_SAVEINDIVIDUAL = 0;
145 : edgomez 558 static char *ARG_OUTPUTFILE = NULL;
146 : edgomez 1382 static int XDIM = 0;
147 :     static int YDIM = 0;
148 :     static int ARG_BQRATIO = 150;
149 :     static int ARG_BQOFFSET = 100;
150 :     static int ARG_MAXBFRAMES = 0;
151 :     static int ARG_PACKED = 0;
152 :     static int ARG_DEBUG = 0;
153 :     static int ARG_VOPDEBUG = 0;
154 : Skal 1622 static int ARG_GREYSCALE = 0;
155 : edgomez 1382 static int ARG_GMC = 0;
156 : chl 1559 static int ARG_INTERLACING = 0;
157 : edgomez 1382 static int ARG_QPEL = 0;
158 :     static int ARG_CLOSED_GOP = 0;
159 :    
160 :     #ifndef READ_PNM
161 : edgomez 558 #define IMAGE_SIZE(x,y) ((x)*(y)*3/2)
162 : edgomez 1382 #else
163 :     #define IMAGE_SIZE(x,y) ((x)*(y)*3)
164 :     #endif
165 : chl 376
166 : edgomez 558 #define MAX(A,B) ( ((A)>(B)) ? (A) : (B) )
167 : edgomez 1382 #define SMALL_EPS (1e-10)
168 : chl 376
169 : edgomez 558 #define SWAP(a) ( (((a)&0x000000ff)<<24) | (((a)&0x0000ff00)<<8) | \
170 :     (((a)&0x00ff0000)>>8) | (((a)&0xff000000)>>24) )
171 : chl 376
172 : edgomez 558 /****************************************************************************
173 :     * Nasty global vars ;-)
174 :     ***************************************************************************/
175 : chl 376
176 : edgomez 1382 static int i;
177 : chl 376
178 : edgomez 558 /* the path where to save output */
179 :     static char filepath[256] = "./";
180 : chl 376
181 : edgomez 558 /* Internal structures (handles) for encoding and decoding */
182 :     static void *enc_handle = NULL;
183 :    
184 :     /*****************************************************************************
185 :     * Local prototypes
186 :     ****************************************************************************/
187 :    
188 :     /* Prints program usage message */
189 :     static void usage();
190 :    
191 :     /* Statistical functions */
192 :     static double msecond();
193 :    
194 :     /* PGM related functions */
195 : edgomez 1382 #ifndef READ_PNM
196 :     static int read_pgmheader(FILE * handle);
197 :     static int read_pgmdata(FILE * handle,
198 :     unsigned char *image);
199 :     #else
200 :     static int read_pnmheader(FILE * handle);
201 :     static int read_pnmdata(FILE * handle,
202 :     unsigned char *image);
203 :     #endif
204 :     static int read_yuvdata(FILE * handle,
205 :     unsigned char *image);
206 : edgomez 558
207 :     /* Encoder related functions */
208 :     static int enc_init(int use_assembler);
209 :     static int enc_stop();
210 : edgomez 1382 static int enc_main(unsigned char *image,
211 :     unsigned char *bitstream,
212 :     int *key,
213 :     int *stats_type,
214 :     int *stats_quant,
215 :     int *stats_length,
216 :     int stats[3]);
217 : edgomez 558
218 :     /*****************************************************************************
219 :     * Main function
220 :     ****************************************************************************/
221 :    
222 : edgomez 1382 int
223 :     main(int argc,
224 :     char *argv[])
225 : edgomez 558 {
226 :    
227 :     unsigned char *mp4_buffer = NULL;
228 :     unsigned char *in_buffer = NULL;
229 :     unsigned char *out_buffer = NULL;
230 :    
231 :     double enctime;
232 : edgomez 1382 double totalenctime = 0.;
233 :     float totalPSNR[3] = {0., 0., 0.};
234 :    
235 :     int totalsize;
236 :     int result;
237 :     int m4v_size;
238 :     int key;
239 :     int stats_type;
240 :     int stats_quant;
241 :     int stats_length;
242 : chl 1623 int use_assembler = 1; // this default changed!
243 : edgomez 1382
244 :     int input_num;
245 :     int output_num;
246 :    
247 : edgomez 558 char filename[256];
248 : edgomez 1382
249 : edgomez 558 FILE *in_file = stdin;
250 :     FILE *out_file = NULL;
251 :    
252 : edgomez 741 printf("xvid_encraw - raw mpeg4 bitstream encoder ");
253 : edgomez 851 printf("written by Christoph Lampert 2002-2003\n\n");
254 : edgomez 558
255 : edgomez 1382 /* Is there a dumb XviD coder ? */
256 :     if(ME_ELEMENTS != VOP_ELEMENTS) {
257 :     fprintf(stderr, "Presets' arrays should have the same number of elements -- Please fill a bug to xvid-devel@xvid.org\n");
258 :     return(-1);
259 :     }
260 :    
261 : edgomez 558 /*****************************************************************************
262 :     * Command line parsing
263 :     ****************************************************************************/
264 :    
265 : edgomez 1382 for (i = 1; i < argc; i++) {
266 :    
267 :     if (strcmp("-asm", argv[i]) == 0) {
268 : edgomez 558 use_assembler = 1;
269 : chl 1623 } else if (strcmp("-noasm", argv[i]) == 0) {
270 :     use_assembler = 0;
271 : edgomez 1382 } else if (strcmp("-w", argv[i]) == 0 && i < argc - 1) {
272 : edgomez 558 i++;
273 :     XDIM = atoi(argv[i]);
274 : edgomez 1382 } else if (strcmp("-h", argv[i]) == 0 && i < argc - 1) {
275 : edgomez 558 i++;
276 :     YDIM = atoi(argv[i]);
277 : edgomez 1382 } else if (strcmp("-bitrate", argv[i]) == 0 && i < argc - 1) {
278 : edgomez 558 i++;
279 :     ARG_BITRATE = atoi(argv[i]);
280 : edgomez 1382 } else if (strcmp("-single", argv[i]) == 0) {
281 :     ARG_SINGLE = 1;
282 :     } else if (strcmp("-pass1", argv[i]) == 0 && i < argc - 1) {
283 : edgomez 851 i++;
284 : edgomez 1382 ARG_PASS1 = argv[i];
285 :     } else if (strcmp("-pass2", argv[i]) == 0 && i < argc - 1) {
286 :     i++;
287 :     ARG_PASS2 = argv[i];
288 :     } else if (strcmp("-max_bframes", argv[i]) == 0 && i < argc - 1) {
289 :     i++;
290 : edgomez 851 ARG_MAXBFRAMES = atoi(argv[i]);
291 : edgomez 1382 } else if (strcmp("-packed", argv[i]) == 0) {
292 :     ARG_PACKED = 1;
293 :     } else if (strcmp("-bquant_ratio", argv[i]) == 0 && i < argc - 1) {
294 : edgomez 851 i++;
295 :     ARG_BQRATIO = atoi(argv[i]);
296 : edgomez 1382 } else if (strcmp("-bquant_offset", argv[i]) == 0 && i < argc - 1) {
297 : edgomez 851 i++;
298 :     ARG_BQOFFSET = atoi(argv[i]);
299 : edgomez 1382
300 :     } else if ((strcmp("-zq", argv[i]) == 0 || strcmp("-zw", argv[i]) == 0) && i < argc - 2) {
301 :    
302 :     if (NUM_ZONES >= MAX_ZONES) {
303 :     fprintf(stderr,"warning: too many zones; zone ignored\n");
304 :     continue;
305 :     }
306 :     ZONES[NUM_ZONES].mode = strcmp("-zq", argv[i])==0 ? XVID_ZONE_QUANT : XVID_ZONE_WEIGHT;
307 : edgomez 558 i++;
308 : edgomez 1382 ZONES[NUM_ZONES].frame = atoi(argv[i]);
309 :     i++;
310 :     ZONES[NUM_ZONES].increment = (int)(atof(argv[i]) * 100);
311 :     ZONES[NUM_ZONES].base = 100;
312 :     NUM_ZONES++;
313 :    
314 :     } else if (strcmp("-quality", argv[i]) == 0 && i < argc - 1) {
315 :     i++;
316 : edgomez 558 ARG_QUALITY = atoi(argv[i]);
317 : edgomez 1382 } else if (strcmp("-framerate", argv[i]) == 0 && i < argc - 1) {
318 : edgomez 558 i++;
319 : edgomez 1382 ARG_FRAMERATE = (float) atof(argv[i]);
320 :     } else if (strcmp("-max_key_interval", argv[i]) == 0 && i < argc - 1) {
321 : edgomez 558 i++;
322 : edgomez 1382 ARG_MAXKEYINTERVAL = atoi(argv[i]);
323 :     } else if (strcmp("-i", argv[i]) == 0 && i < argc - 1) {
324 :     i++;
325 : edgomez 558 ARG_INPUTFILE = argv[i];
326 : edgomez 1382 } else if (strcmp("-stats", argv[i]) == 0) {
327 :     ARG_STATS = 1;
328 :     } else if (strcmp("-dump", argv[i]) == 0) {
329 :     ARG_DUMP = 1;
330 :     } else if (strcmp("-lumimasking", argv[i]) == 0) {
331 :     ARG_LUMIMASKING = 1;
332 :     } else if (strcmp("-type", argv[i]) == 0 && i < argc - 1) {
333 : edgomez 558 i++;
334 :     ARG_INPUTTYPE = atoi(argv[i]);
335 : edgomez 1382 } else if (strcmp("-frames", argv[i]) == 0 && i < argc - 1) {
336 : edgomez 558 i++;
337 : edgomez 1382 ARG_MAXFRAMENR = atoi(argv[i]);
338 :     } else if (strcmp("-save", argv[i]) == 0) {
339 :     ARG_SAVEMPEGSTREAM = 1;
340 :     ARG_SAVEINDIVIDUAL = 1;
341 :     } else if (strcmp("-debug", argv[i]) == 0) {
342 : edgomez 558 i++;
343 : edgomez 1382 if (sscanf(argv[i],"0x%x", &ARG_DEBUG) || sscanf(argv[i],"%d", &ARG_DEBUG)) ;
344 :     } else if (strcmp("-o", argv[i]) == 0 && i < argc - 1) {
345 :     ARG_SAVEMPEGSTREAM = 1;
346 : edgomez 558 i++;
347 :     ARG_OUTPUTFILE = argv[i];
348 : edgomez 1382 } else if (strcmp("-vop_debug", argv[i]) == 0) {
349 :     ARG_VOPDEBUG = 1;
350 : Skal 1622 }
351 :     else if (strcmp("-grey", argv[i]) == 0) {
352 :     ARG_GREYSCALE = 1;
353 :     }
354 :     else if (strcmp("-qpel", argv[i]) == 0) {
355 : edgomez 1382 ARG_QPEL = 1;
356 :     } else if (strcmp("-gmc", argv[i]) == 0) {
357 :     ARG_GMC = 1;
358 : chl 1559 } else if (strcmp("-interlaced", argv[i]) == 0) {
359 :     ARG_INTERLACING = 1;
360 : edgomez 1382 } else if (strcmp("-closed_gop", argv[i]) == 0) {
361 :     ARG_CLOSED_GOP = 1;
362 :     } else if (strcmp("-help", argv[i])) {
363 : edgomez 558 usage();
364 : edgomez 1382 return (0);
365 :     } else {
366 : edgomez 558 usage();
367 :     exit(-1);
368 :     }
369 : edgomez 1382
370 : edgomez 558 }
371 : edgomez 1382
372 : edgomez 558 /*****************************************************************************
373 :     * Arguments checking
374 :     ****************************************************************************/
375 :    
376 : edgomez 1382 if (XDIM <= 0 || XDIM >= 4096 || YDIM <= 0 || YDIM >= 4096) {
377 :     fprintf(stderr,
378 :     "Trying to retreive width and height from PGM header\n");
379 :     ARG_INPUTTYPE = 1; /* pgm */
380 : edgomez 558 }
381 :    
382 : edgomez 1382 if (ARG_QUALITY < 0 ) {
383 :     ARG_QUALITY = 0;
384 :     } else if (ARG_QUALITY >= ME_ELEMENTS) {
385 :     ARG_QUALITY = ME_ELEMENTS - 1;
386 : edgomez 558 }
387 :    
388 : edgomez 1382 if (ARG_FRAMERATE <= 0) {
389 :     fprintf(stderr, "Wrong Framerate %s \n", argv[5]);
390 :     return (-1);
391 : edgomez 558 }
392 :    
393 : edgomez 1382 if (ARG_MAXFRAMENR <= 0) {
394 :     fprintf(stderr, "Wrong number of frames\n");
395 :     return (-1);
396 : edgomez 558 }
397 :    
398 : edgomez 1382 if (ARG_INPUTFILE == NULL || strcmp(ARG_INPUTFILE, "stdin") == 0) {
399 : edgomez 558 in_file = stdin;
400 : edgomez 1382 } else {
401 : edgomez 558
402 :     in_file = fopen(ARG_INPUTFILE, "rb");
403 :     if (in_file == NULL) {
404 :     fprintf(stderr, "Error opening input file %s\n", ARG_INPUTFILE);
405 : edgomez 1382 return (-1);
406 : edgomez 558 }
407 :     }
408 :    
409 :     if (ARG_INPUTTYPE) {
410 : edgomez 1382 #ifndef READ_PNM
411 : edgomez 558 if (read_pgmheader(in_file)) {
412 : edgomez 1382 #else
413 :     if (read_pnmheader(in_file)) {
414 :     #endif
415 :     fprintf(stderr,
416 :     "Wrong input format, I want YUV encapsulated in PGM\n");
417 :     return (-1);
418 : edgomez 558 }
419 :     }
420 :    
421 :     /* now we know the sizes, so allocate memory */
422 : edgomez 1382 in_buffer = (unsigned char *) malloc(IMAGE_SIZE(XDIM, YDIM));
423 : edgomez 558 if (!in_buffer)
424 :     goto free_all_memory;
425 :    
426 :     /* this should really be enough memory ! */
427 : edgomez 1382 mp4_buffer = (unsigned char *) malloc(IMAGE_SIZE(XDIM, YDIM) * 2);
428 : edgomez 558 if (!mp4_buffer)
429 : edgomez 1382 goto free_all_memory;
430 : edgomez 558
431 :     /*****************************************************************************
432 :     * XviD PART Start
433 :     ****************************************************************************/
434 :    
435 :    
436 : edgomez 1382 result = enc_init(use_assembler);
437 :     if (result) {
438 :     fprintf(stderr, "Encore INIT problem, return value %d\n", result);
439 : edgomez 558 goto release_all;
440 :     }
441 :    
442 :     /*****************************************************************************
443 :     * Main loop
444 :     ****************************************************************************/
445 :    
446 : edgomez 851 if (ARG_SAVEMPEGSTREAM && ARG_OUTPUTFILE) {
447 : edgomez 561
448 : edgomez 1382 if ((out_file = fopen(ARG_OUTPUTFILE, "w+b")) == NULL) {
449 : edgomez 558 fprintf(stderr, "Error opening output file %s\n", ARG_OUTPUTFILE);
450 :     goto release_all;
451 :     }
452 :    
453 : edgomez 1382 } else {
454 : edgomez 558 out_file = NULL;
455 :     }
456 :    
457 :     /*****************************************************************************
458 :     * Encoding loop
459 :     ****************************************************************************/
460 :    
461 :     totalsize = 0;
462 :    
463 : edgomez 1382 result = 0;
464 :    
465 :     input_num = 0; /* input frame counter */
466 :     output_num = 0; /* output frame counter */
467 :    
468 : edgomez 558 do {
469 :    
470 : edgomez 1382 char *type;
471 :     int sse[3];
472 :    
473 :     if (input_num >= ARG_MAXFRAMENR) {
474 :     result = 1;
475 : edgomez 558 }
476 :    
477 : edgomez 1382 if (!result) {
478 :     if (ARG_INPUTTYPE) {
479 :     /* read PGM data (YUV-format) */
480 :     #ifndef READ_PNM
481 :     result = read_pgmdata(in_file, in_buffer);
482 :     #else
483 :     result = read_pnmdata(in_file, in_buffer);
484 :     #endif
485 :     } else {
486 :     /* read raw data (YUV-format) */
487 :     result = read_yuvdata(in_file, in_buffer);
488 :     }
489 : edgomez 728 }
490 :    
491 :     /*****************************************************************************
492 : edgomez 558 * Encode and decode this frame
493 :     ****************************************************************************/
494 :    
495 :     enctime = msecond();
496 : edgomez 1382 m4v_size =
497 :     enc_main(!result ? in_buffer : 0, mp4_buffer, &key, &stats_type,
498 :     &stats_quant, &stats_length, sse);
499 : edgomez 558 enctime = msecond() - enctime;
500 :    
501 : edgomez 1382 /* Write the Frame statistics */
502 : edgomez 851
503 : edgomez 1382 printf("%5d: key=%i, time= %6.0f, len= %7d", !result ? input_num : -1,
504 :     key, (float) enctime, (int) m4v_size);
505 : edgomez 851
506 : edgomez 1382 if (stats_type > 0) { /* !XVID_TYPE_NOTHING */
507 : edgomez 851
508 : edgomez 1382 switch (stats_type) {
509 :     case XVID_TYPE_IVOP:
510 :     type = "I";
511 :     break;
512 :     case XVID_TYPE_PVOP:
513 :     type = "P";
514 :     break;
515 :     case XVID_TYPE_BVOP:
516 :     type = "B";
517 :     break;
518 :     case XVID_TYPE_SVOP:
519 :     type = "S";
520 :     break;
521 :     default:
522 :     type = "U";
523 :     break;
524 :     }
525 : edgomez 851
526 : edgomez 1382 printf(" | type=%s, quant= %2d, len= %7d", type, stats_quant,
527 :     stats_length);
528 :    
529 :     #define SSE2PSNR(sse, width, height) ((!(sse))?0.0f : 48.131f - 10*(float)log10((float)(sse)/((float)((width)*(height)))))
530 :    
531 :     if (ARG_STATS) {
532 :     printf(", psnr y = %2.2f, psnr u = %2.2f, psnr v = %2.2f",
533 :     SSE2PSNR(sse[0], XDIM, YDIM), SSE2PSNR(sse[1], XDIM / 2,
534 :     YDIM / 2),
535 :     SSE2PSNR(sse[2], XDIM / 2, YDIM / 2));
536 :    
537 :     totalPSNR[0] += SSE2PSNR(sse[0], XDIM, YDIM);
538 :     totalPSNR[1] += SSE2PSNR(sse[1], XDIM/2, YDIM/2);
539 :     totalPSNR[2] += SSE2PSNR(sse[2], XDIM/2, YDIM/2);
540 :     }
541 :    
542 : edgomez 851 }
543 : edgomez 1382 #undef SSE2PSNR
544 : edgomez 851
545 : edgomez 1382 printf("\n");
546 :    
547 :     if (m4v_size < 0) {
548 :     break;
549 :     }
550 :    
551 : edgomez 851 /* Update encoding time stats */
552 : edgomez 558 totalenctime += enctime;
553 :     totalsize += m4v_size;
554 :    
555 : edgomez 728 /*****************************************************************************
556 : edgomez 1382 * Save stream to file
557 : edgomez 728 ****************************************************************************/
558 :    
559 : edgomez 1382 if (m4v_size > 0 && ARG_SAVEMPEGSTREAM) {
560 : edgomez 728
561 : edgomez 558 /* Save single files */
562 : edgomez 1382 if (ARG_SAVEINDIVIDUAL) {
563 :     FILE *out;
564 :     sprintf(filename, "%sframe%05d.m4v", filepath, output_num);
565 :     out = fopen(filename, "w+b");
566 :     fwrite(mp4_buffer, m4v_size, 1, out);
567 :     fclose(out);
568 :     output_num++;
569 : edgomez 558 }
570 :    
571 : edgomez 1382 /* Save ES stream */
572 :     if (ARG_OUTPUTFILE && out_file)
573 : edgomez 851 fwrite(mp4_buffer, 1, m4v_size, out_file);
574 : edgomez 558 }
575 :    
576 : edgomez 1382 input_num++;
577 : edgomez 558
578 : edgomez 1382 /* Read the header if it's pgm stream */
579 :     if (!result && ARG_INPUTTYPE)
580 :     #ifndef READ_PNM
581 :     result = read_pgmheader(in_file);
582 :     #else
583 :     result = read_pnmheader(in_file);
584 :     #endif
585 :     } while (1);
586 : edgomez 558
587 :    
588 : edgomez 1382
589 : edgomez 558 /*****************************************************************************
590 :     * Calculate totals and averages for output, print results
591 :     ****************************************************************************/
592 : chl 376
593 : edgomez 1382 printf("Tot: enctime(ms) =%7.2f, length(bytes) = %7d\n",
594 :     totalenctime, (int) totalsize);
595 : chl 376
596 : edgomez 1382 if (input_num > 0) {
597 :     totalsize /= input_num;
598 :     totalenctime /= input_num;
599 :     totalPSNR[0] /= input_num;
600 :     totalPSNR[1] /= input_num;
601 :     totalPSNR[2] /= input_num;
602 :     } else {
603 :     totalsize = -1;
604 :     totalenctime = -1;
605 :     }
606 : chl 376
607 : edgomez 1382 printf("Avg: enctime(ms) =%7.2f, fps =%7.2f, length(bytes) = %7d",
608 :     totalenctime, 1000 / totalenctime, (int) totalsize);
609 :     if (ARG_STATS) {
610 :     printf(", psnr y = %2.2f, psnr u = %2.2f, psnr v = %2.2f",
611 :     totalPSNR[0],totalPSNR[1],totalPSNR[2]);
612 :     }
613 :     printf("\n");
614 :    
615 :    
616 : edgomez 558 /*****************************************************************************
617 :     * XviD PART Stop
618 :     ****************************************************************************/
619 : chl 376
620 : edgomez 1382 release_all:
621 : chl 376
622 : edgomez 1382 if (enc_handle) {
623 :     result = enc_stop();
624 :     if (result)
625 :     fprintf(stderr, "Encore RELEASE problem return value %d\n",
626 :     result);
627 : edgomez 558 }
628 : chl 376
629 : edgomez 1382 if (in_file)
630 : edgomez 728 fclose(in_file);
631 : edgomez 1382 if (out_file)
632 : edgomez 558 fclose(out_file);
633 :    
634 : edgomez 1382 free_all_memory:
635 : edgomez 558 free(out_buffer);
636 :     free(mp4_buffer);
637 :     free(in_buffer);
638 :    
639 : edgomez 1382 return (0);
640 : edgomez 558
641 :     }
642 :    
643 :    
644 :     /*****************************************************************************
645 :     * "statistical" functions
646 :     *
647 :     * these are not needed for encoding or decoding, but for measuring
648 :     * time and quality, there in nothing specific to XviD in these
649 :     *
650 :     *****************************************************************************/
651 :    
652 :     /* Return time elapsed time in miliseconds since the program started */
653 : edgomez 1382 static double
654 :     msecond()
655 :     {
656 : edgomez 824 #ifndef WIN32
657 : edgomez 1382 struct timeval tv;
658 :    
659 : chl 376 gettimeofday(&tv, 0);
660 : edgomez 1382 return (tv.tv_sec * 1.0e3 + tv.tv_usec * 1.0e-3);
661 : edgomez 558 #else
662 :     clock_t clk;
663 : edgomez 1382
664 : edgomez 558 clk = clock();
665 : edgomez 1382 return (clk * 1000 / CLOCKS_PER_SEC);
666 : edgomez 558 #endif
667 : chl 376 }
668 :    
669 : edgomez 558 /*****************************************************************************
670 :     * Usage message
671 :     *****************************************************************************/
672 : chl 376
673 : edgomez 1382 static void
674 :     usage()
675 : edgomez 558 {
676 : edgomez 1382 fprintf(stderr, "Usage : xvid_stat [OPTIONS]\n\n");
677 :     fprintf(stderr, "Input options:\n");
678 :     fprintf(stderr, " -i string : input filename (default=stdin)\n");
679 :     fprintf(stderr, " -type integer: input data type (yuv=0, pgm=1)\n");
680 :     fprintf(stderr, " -w integer: frame width ([1.2048])\n");
681 :     fprintf(stderr, " -h integer: frame height ([1.2048])\n");
682 :     fprintf(stderr, " -frames integer: number of frames to encode\n");
683 :     fprintf(stderr, "\n");
684 :     fprintf(stderr, "Output options:\n");
685 :     fprintf(stderr, " -dump : save decoder output\n");
686 :     fprintf(stderr, " -save : save an Elementary Stream file per frame\n");
687 :     fprintf(stderr, " -o string: save an Elementary Stream for the complete sequence\n");
688 :     fprintf(stderr, "\n");
689 :     fprintf(stderr, "BFrames options:\n");
690 :     fprintf(stderr, " -max_bframes integer: max bframes (default=0)\n");
691 :     fprintf(stderr, " -bquant_ratio integer: bframe quantizer ratio (default=150)\n");
692 :     fprintf(stderr, " -bquant_offset integer: bframe quantizer offset (default=100)\n");
693 :     fprintf(stderr, "\n");
694 :     fprintf(stderr, "Rate control options:\n");
695 :     fprintf(stderr, " -framerate float : target framerate (>0 | default=25.0)\n");
696 :     fprintf(stderr, " -bitrate integer : target bitrate\n");
697 :     fprintf(stderr, " -single : single pass mode\n");
698 :     fprintf(stderr, " -pass1 filename : twopass mode (first pass)\n");
699 :     fprintf(stderr, " -pass2 filename : twopass mode (2nd pass)\n");
700 :     fprintf(stderr, " -zq starting_frame float : bitrate zone; quant\n");
701 :     fprintf(stderr, " -zw starting_frame float : bitrate zone; weight\n");
702 :     fprintf(stderr, " -max_key_interval integer : maximum keyframe interval\n");
703 :     fprintf(stderr, "\n");
704 :     fprintf(stderr, "Other options\n");
705 :     fprintf(stderr, " -asm : use assembly optmized code\n");
706 : chl 1623 fprintf(stderr, " -noasm : do not use assembly optmized code\n");
707 : edgomez 1382 fprintf(stderr, " -quality integer: quality ([0..%d])\n", ME_ELEMENTS - 1);
708 :     fprintf(stderr, " -qpel : use quarter pixel ME\n");
709 :     fprintf(stderr, " -gmc : use global motion compensation\n");
710 : chl 1559 fprintf(stderr, " -interlaced : use interlaced encoding (this is NOT a deinterlacer!)\n");
711 : edgomez 1382 fprintf(stderr, " -packed : packed mode\n");
712 :     fprintf(stderr, " -closed_gop : closed GOP mode\n");
713 : Skal 1622 fprintf(stderr, " -grey : grey scale coding (chroma is discarded)\n");
714 : edgomez 1382 fprintf(stderr, " -lumimasking : use lumimasking algorithm\n");
715 :     fprintf(stderr, " -stats : print stats about encoded frames\n");
716 :     fprintf(stderr, " -debug : activates xvidcore internal debugging output\n");
717 :     fprintf(stderr, " -vop_debug : print some info directly into encoded frames\n");
718 :     fprintf(stderr, " -help : prints this help message\n");
719 :     fprintf(stderr, "\n");
720 :     fprintf(stderr, "NB: You can define %d zones repeating the -z[qw] option as many times as needed.\n", MAX_ZONES);
721 :     fprintf(stderr, "\n");
722 : edgomez 558 }
723 :    
724 :     /*****************************************************************************
725 :     * Input and output functions
726 :     *
727 :     * the are small and simple routines to read and write PGM and YUV
728 :     * image. It's just for convenience, again nothing specific to XviD
729 :     *
730 :     *****************************************************************************/
731 :    
732 : edgomez 1382 #ifndef READ_PNM
733 :     static int
734 :     read_pgmheader(FILE * handle)
735 :     {
736 :     int bytes, xsize, ysize, depth;
737 : chl 376 char dummy[2];
738 :    
739 : edgomez 1382 bytes = fread(dummy, 1, 2, handle);
740 : edgomez 558
741 : edgomez 1382 if ((bytes < 2) || (dummy[0] != 'P') || (dummy[1] != '5'))
742 :     return (1);
743 :    
744 :     fscanf(handle, "%d %d %d", &xsize, &ysize, &depth);
745 : edgomez 1398 if ((xsize > 4096) || (ysize > 4096*3/2) || (depth != 255)) {
746 : edgomez 1382 fprintf(stderr, "%d %d %d\n", xsize, ysize, depth);
747 :     return (2);
748 : chl 376 }
749 : edgomez 1382 if ((XDIM == 0) || (YDIM == 0)) {
750 :     XDIM = xsize;
751 :     YDIM = ysize * 2 / 3;
752 : chl 376 }
753 :    
754 : edgomez 1382 return (0);
755 : chl 376 }
756 :    
757 : edgomez 1382 static int
758 :     read_pgmdata(FILE * handle,
759 :     unsigned char *image)
760 :     {
761 : edgomez 558 int i;
762 : chl 376 char dummy;
763 : edgomez 1382
764 : edgomez 558 unsigned char *y = image;
765 : edgomez 1382 unsigned char *u = image + XDIM * YDIM;
766 :     unsigned char *v = image + XDIM * YDIM + XDIM / 2 * YDIM / 2;
767 : chl 376
768 : edgomez 558 /* read Y component of picture */
769 : edgomez 1382 fread(y, 1, XDIM * YDIM, handle);
770 :    
771 :     for (i = 0; i < YDIM / 2; i++) {
772 : edgomez 558 /* read U */
773 : edgomez 1382 fread(u, 1, XDIM / 2, handle);
774 : edgomez 558
775 :     /* read V */
776 : edgomez 1382 fread(v, 1, XDIM / 2, handle);
777 : edgomez 558
778 :     /* Update pointers */
779 : edgomez 1382 u += XDIM / 2;
780 :     v += XDIM / 2;
781 : chl 376 }
782 : edgomez 558
783 : edgomez 1382 /* I don't know why, but this seems needed */
784 : edgomez 558 fread(&dummy, 1, 1, handle);
785 :    
786 : edgomez 1382 return (0);
787 : chl 376 }
788 : edgomez 1382 #else
789 :     static int
790 :     read_pnmheader(FILE * handle)
791 :     {
792 :     int bytes, xsize, ysize, depth;
793 :     char dummy[2];
794 : chl 376
795 : edgomez 1382 bytes = fread(dummy, 1, 2, handle);
796 :    
797 :     if ((bytes < 2) || (dummy[0] != 'P') || (dummy[1] != '6'))
798 :     return (1);
799 :    
800 :     fscanf(handle, "%d %d %d", &xsize, &ysize, &depth);
801 :     if ((xsize > 1440) || (ysize > 2880) || (depth != 255)) {
802 :     fprintf(stderr, "%d %d %d\n", xsize, ysize, depth);
803 :     return (2);
804 :     }
805 :    
806 :     XDIM = xsize;
807 :     YDIM = ysize;
808 :    
809 :     return (0);
810 :     }
811 :    
812 :     static int
813 :     read_pnmdata(FILE * handle,
814 :     unsigned char *image)
815 : edgomez 558 {
816 : edgomez 1382 int i;
817 :     char dummy;
818 :    
819 :     /* read Y component of picture */
820 :     fread(image, 1, XDIM * YDIM * 3, handle);
821 :    
822 :     /* I don't know why, but this seems needed */
823 :     fread(&dummy, 1, 1, handle);
824 :    
825 :     return (0);
826 : chl 376 }
827 : edgomez 1382 #endif
828 : chl 376
829 : edgomez 1382 static int
830 :     read_yuvdata(FILE * handle,
831 :     unsigned char *image)
832 :     {
833 :    
834 :     if (fread(image, 1, IMAGE_SIZE(XDIM, YDIM), handle) !=
835 :     (unsigned int) IMAGE_SIZE(XDIM, YDIM))
836 :     return (1);
837 :     else
838 :     return (0);
839 :     }
840 :    
841 : edgomez 558 /*****************************************************************************
842 :     * Routines for encoding: init encoder, frame step, release encoder
843 :     ****************************************************************************/
844 : chl 376
845 : edgomez 1382 /* sample plugin */
846 :    
847 :     int
848 :     rawenc_debug(void *handle,
849 :     int opt,
850 :     void *param1,
851 :     void *param2)
852 :     {
853 :     switch (opt) {
854 :     case XVID_PLG_INFO:
855 :     {
856 :     xvid_plg_info_t *info = (xvid_plg_info_t *) param1;
857 :    
858 :     info->flags = XVID_REQDQUANTS;
859 :     return 0;
860 :     }
861 :    
862 :     case XVID_PLG_CREATE:
863 :     case XVID_PLG_DESTROY:
864 :     case XVID_PLG_BEFORE:
865 :     return 0;
866 :    
867 :     case XVID_PLG_AFTER:
868 :     {
869 :     xvid_plg_data_t *data = (xvid_plg_data_t *) param1;
870 :     int i, j;
871 :    
872 :     printf("---[ frame: %5i quant: %2i length: %6i ]---\n",
873 :     data->frame_num, data->quant, data->length);
874 :     for (j = 0; j < data->mb_height; j++) {
875 :     for (i = 0; i < data->mb_width; i++)
876 :     printf("%2i ", data->dquant[j * data->dquant_stride + i]);
877 :     printf("\n");
878 :     }
879 :    
880 :     return 0;
881 :     }
882 :     }
883 :    
884 :     return XVID_ERR_FAIL;
885 :     }
886 :    
887 :    
888 : chl 376 #define FRAMERATE_INCR 1001
889 :    
890 : edgomez 1382
891 : edgomez 558 /* Initialize encoder for first use, pass all needed parameters to the codec */
892 : edgomez 1382 static int
893 :     enc_init(int use_assembler)
894 : edgomez 558 {
895 : chl 376 int xerr;
896 : edgomez 1382 //xvid_plugin_cbr_t cbr;
897 :     xvid_plugin_single_t single;
898 :     xvid_plugin_2pass1_t rc2pass1;
899 :     xvid_plugin_2pass2_t rc2pass2;
900 :     //xvid_plugin_fixed_t rcfixed;
901 :     xvid_enc_plugin_t plugins[7];
902 :     xvid_gbl_init_t xvid_gbl_init;
903 :     xvid_enc_create_t xvid_enc_create;
904 : chl 376
905 : edgomez 1382 /*------------------------------------------------------------------------
906 :     * XviD core initialization
907 :     *----------------------------------------------------------------------*/
908 : chl 376
909 : edgomez 1382 /* Set version -- version checking will done by xvidcore */
910 :     memset(&xvid_gbl_init, 0, sizeof(xvid_gbl_init));
911 :     xvid_gbl_init.version = XVID_VERSION;
912 :     xvid_gbl_init.debug = ARG_DEBUG;
913 :    
914 :    
915 :     /* Do we have to enable ASM optimizations ? */
916 :     if (use_assembler) {
917 :    
918 : suxen_drol 860 #ifdef ARCH_IS_IA64
919 : edgomez 1382 xvid_gbl_init.cpu_flags = XVID_CPU_FORCE | XVID_CPU_ASM;
920 : chl 376 #else
921 : edgomez 1382 xvid_gbl_init.cpu_flags = 0;
922 : chl 376 #endif
923 : edgomez 1382 } else {
924 :     xvid_gbl_init.cpu_flags = XVID_CPU_FORCE;
925 : edgomez 558 }
926 : edgomez 1382
927 :     /* Initialize XviD core -- Should be done once per __process__ */
928 :     xvid_global(NULL, XVID_GBL_INIT, &xvid_gbl_init, NULL);
929 :    
930 :     /*------------------------------------------------------------------------
931 :     * XviD encoder initialization
932 :     *----------------------------------------------------------------------*/
933 :    
934 :     /* Version again */
935 :     memset(&xvid_enc_create, 0, sizeof(xvid_enc_create));
936 :     xvid_enc_create.version = XVID_VERSION;
937 :    
938 :     /* Width and Height of input frames */
939 :     xvid_enc_create.width = XDIM;
940 :     xvid_enc_create.height = YDIM;
941 :     xvid_enc_create.profile = XVID_PROFILE_AS_L4;
942 :    
943 :     /* init plugins */
944 :     xvid_enc_create.zones = ZONES;
945 :     xvid_enc_create.num_zones = NUM_ZONES;
946 :    
947 :     xvid_enc_create.plugins = plugins;
948 :     xvid_enc_create.num_plugins = 0;
949 :    
950 :     if (ARG_SINGLE) {
951 :     memset(&single, 0, sizeof(xvid_plugin_single_t));
952 :     single.version = XVID_VERSION;
953 :     single.bitrate = ARG_BITRATE;
954 :    
955 :     plugins[xvid_enc_create.num_plugins].func = xvid_plugin_single;
956 :     plugins[xvid_enc_create.num_plugins].param = &single;
957 :     xvid_enc_create.num_plugins++;
958 : edgomez 558 }
959 : chl 376
960 : edgomez 1382 if (ARG_PASS2) {
961 :     memset(&rc2pass2, 0, sizeof(xvid_plugin_2pass2_t));
962 :     rc2pass2.version = XVID_VERSION;
963 :     rc2pass2.filename = ARG_PASS2;
964 :     rc2pass2.bitrate = ARG_BITRATE;
965 : chl 376
966 : chl 1470 /* An example of activating VBV could look like this
967 :     rc2pass2.vbv_size = 3145728;
968 :     rc2pass2.vbv_initial = 2359296;
969 :     rc2pass2.vbv_maxrate = 4000000;
970 :     rc2pass2.vbv_peakrate = 10000000;
971 :     */
972 :    
973 : edgomez 1382 plugins[xvid_enc_create.num_plugins].func = xvid_plugin_2pass2;
974 :     plugins[xvid_enc_create.num_plugins].param = &rc2pass2;
975 :     xvid_enc_create.num_plugins++;
976 : chl 376 }
977 : edgomez 1382
978 :     if (ARG_PASS1) {
979 :     memset(&rc2pass1, 0, sizeof(xvid_plugin_2pass1_t));
980 :     rc2pass1.version = XVID_VERSION;
981 :     rc2pass1.filename = ARG_PASS1;
982 :    
983 :     plugins[xvid_enc_create.num_plugins].func = xvid_plugin_2pass1;
984 :     plugins[xvid_enc_create.num_plugins].param = &rc2pass1;
985 :     xvid_enc_create.num_plugins++;
986 : chl 376 }
987 :    
988 : edgomez 1382 if (ARG_LUMIMASKING) {
989 :     plugins[xvid_enc_create.num_plugins].func = xvid_plugin_lumimasking;
990 :     plugins[xvid_enc_create.num_plugins].param = NULL;
991 :     xvid_enc_create.num_plugins++;
992 :     }
993 :    
994 :     if (ARG_DUMP) {
995 :     plugins[xvid_enc_create.num_plugins].func = xvid_plugin_dump;
996 :     plugins[xvid_enc_create.num_plugins].param = NULL;
997 :     xvid_enc_create.num_plugins++;
998 :     }
999 :    
1000 :     #if 0
1001 :     if (ARG_DEBUG) {
1002 :     plugins[xvid_enc_create.num_plugins].func = rawenc_debug;
1003 :     plugins[xvid_enc_create.num_plugins].param = NULL;
1004 :     xvid_enc_create.num_plugins++;
1005 :     }
1006 :     #endif
1007 :    
1008 :     /* No fancy thread tests */
1009 :     xvid_enc_create.num_threads = 0;
1010 :    
1011 :     /* Frame rate - Do some quick float fps = fincr/fbase hack */
1012 :     if ((ARG_FRAMERATE - (int) ARG_FRAMERATE) < SMALL_EPS) {
1013 :     xvid_enc_create.fincr = 1;
1014 :     xvid_enc_create.fbase = (int) ARG_FRAMERATE;
1015 :     } else {
1016 :     xvid_enc_create.fincr = FRAMERATE_INCR;
1017 :     xvid_enc_create.fbase = (int) (FRAMERATE_INCR * ARG_FRAMERATE);
1018 :     }
1019 :    
1020 :     /* Maximum key frame interval */
1021 :     if (ARG_MAXKEYINTERVAL > 0) {
1022 :     xvid_enc_create.max_key_interval = ARG_MAXKEYINTERVAL;
1023 :     }else {
1024 :     xvid_enc_create.max_key_interval = (int) ARG_FRAMERATE *10;
1025 :     }
1026 :    
1027 :     /* Bframes settings */
1028 :     xvid_enc_create.max_bframes = ARG_MAXBFRAMES;
1029 :     xvid_enc_create.bquant_ratio = ARG_BQRATIO;
1030 :     xvid_enc_create.bquant_offset = ARG_BQOFFSET;
1031 :    
1032 :     /* Dropping ratio frame -- we don't need that */
1033 :     xvid_enc_create.frame_drop_ratio = 0;
1034 :    
1035 :     /* Global encoder options */
1036 :     xvid_enc_create.global = 0;
1037 :    
1038 :     if (ARG_PACKED)
1039 :     xvid_enc_create.global |= XVID_GLOBAL_PACKED;
1040 :    
1041 :     if (ARG_CLOSED_GOP)
1042 :     xvid_enc_create.global |= XVID_GLOBAL_CLOSED_GOP;
1043 :    
1044 :     if (ARG_STATS)
1045 :     xvid_enc_create.global |= XVID_GLOBAL_EXTRASTATS_ENABLE;
1046 :    
1047 : edgomez 558 /* I use a small value here, since will not encode whole movies, but short clips */
1048 : edgomez 1382 xerr = xvid_encore(NULL, XVID_ENC_CREATE, &xvid_enc_create, NULL);
1049 : chl 376
1050 : edgomez 1382 /* Retrieve the encoder instance from the structure */
1051 :     enc_handle = xvid_enc_create.handle;
1052 : chl 376
1053 : edgomez 1382 return (xerr);
1054 : chl 376 }
1055 :    
1056 : edgomez 1382 static int
1057 :     enc_stop()
1058 : edgomez 558 {
1059 :     int xerr;
1060 : chl 376
1061 : edgomez 1382 /* Destroy the encoder instance */
1062 : chl 376 xerr = xvid_encore(enc_handle, XVID_ENC_DESTROY, NULL, NULL);
1063 : edgomez 558
1064 : edgomez 1382 return (xerr);
1065 : chl 376 }
1066 :    
1067 : edgomez 1382 static int
1068 :     enc_main(unsigned char *image,
1069 :     unsigned char *bitstream,
1070 :     int *key,
1071 :     int *stats_type,
1072 :     int *stats_quant,
1073 :     int *stats_length,
1074 :     int sse[3])
1075 : edgomez 558 {
1076 : edgomez 1382 int ret;
1077 : chl 376
1078 : edgomez 1382 xvid_enc_frame_t xvid_enc_frame;
1079 :     xvid_enc_stats_t xvid_enc_stats;
1080 : chl 376
1081 : edgomez 1382 /* Version for the frame and the stats */
1082 :     memset(&xvid_enc_frame, 0, sizeof(xvid_enc_frame));
1083 :     xvid_enc_frame.version = XVID_VERSION;
1084 : chl 376
1085 : edgomez 1382 memset(&xvid_enc_stats, 0, sizeof(xvid_enc_stats));
1086 :     xvid_enc_stats.version = XVID_VERSION;
1087 : chl 376
1088 : edgomez 1382 /* Bind output buffer */
1089 :     xvid_enc_frame.bitstream = bitstream;
1090 :     xvid_enc_frame.length = -1;
1091 : chl 376
1092 : edgomez 1382 /* Initialize input image fields */
1093 :     if (image) {
1094 :     xvid_enc_frame.input.plane[0] = image;
1095 :     #ifndef READ_PNM
1096 :     xvid_enc_frame.input.csp = XVID_CSP_I420;
1097 :     xvid_enc_frame.input.stride[0] = XDIM;
1098 :     #else
1099 :     xvid_enc_frame.input.csp = XVID_CSP_BGR;
1100 :     xvid_enc_frame.input.stride[0] = XDIM*3;
1101 :     #endif
1102 :     } else {
1103 :     xvid_enc_frame.input.csp = XVID_CSP_NULL;
1104 :     }
1105 : chl 376
1106 : edgomez 1382 /* Set up core's general features */
1107 :     xvid_enc_frame.vol_flags = 0;
1108 :     if (ARG_STATS)
1109 :     xvid_enc_frame.vol_flags |= XVID_VOL_EXTRASTATS;
1110 :     if (ARG_QPEL)
1111 :     xvid_enc_frame.vol_flags |= XVID_VOL_QUARTERPEL;
1112 :     if (ARG_GMC)
1113 :     xvid_enc_frame.vol_flags |= XVID_VOL_GMC;
1114 : chl 1559 if (ARG_INTERLACING)
1115 :     xvid_enc_frame.vol_flags |= XVID_VOL_INTERLACING;
1116 : edgomez 728
1117 : edgomez 1382 /* Set up core's general features */
1118 :     xvid_enc_frame.vop_flags = vop_presets[ARG_QUALITY];
1119 : edgomez 728
1120 : edgomez 1382 if (ARG_VOPDEBUG) {
1121 :     xvid_enc_frame.vop_flags |= XVID_VOP_DEBUG;
1122 :     }
1123 : edgomez 728
1124 : Skal 1622 if (ARG_GREYSCALE) {
1125 :     xvid_enc_frame.vop_flags |= XVID_VOP_GREYSCALE;
1126 :     }
1127 :    
1128 : edgomez 1382 /* Frame type -- let core decide for us */
1129 :     xvid_enc_frame.type = XVID_TYPE_AUTO;
1130 : chl 376
1131 : edgomez 1382 /* Force the right quantizer -- It is internally managed by RC plugins */
1132 :     xvid_enc_frame.quant = 0;
1133 : edgomez 728
1134 : edgomez 1382 /* Set up motion estimation flags */
1135 :     xvid_enc_frame.motion = motion_presets[ARG_QUALITY];
1136 : chl 376
1137 : edgomez 1382 if (ARG_GMC)
1138 :     xvid_enc_frame.motion |= XVID_ME_GME_REFINE;
1139 :    
1140 :     if (ARG_QPEL)
1141 :     xvid_enc_frame.motion |= XVID_ME_QUARTERPELREFINE16;
1142 :     if (ARG_QPEL && (xvid_enc_frame.vop_flags & XVID_VOP_INTER4V))
1143 :     xvid_enc_frame.motion |= XVID_ME_QUARTERPELREFINE8;
1144 :    
1145 :     /* We don't use special matrices */
1146 :     xvid_enc_frame.quant_intra_matrix = NULL;
1147 :     xvid_enc_frame.quant_inter_matrix = NULL;
1148 :    
1149 :     /* Encode the frame */
1150 :     ret = xvid_encore(enc_handle, XVID_ENC_ENCODE, &xvid_enc_frame,
1151 :     &xvid_enc_stats);
1152 :    
1153 :     *key = (xvid_enc_frame.out_flags & XVID_KEYFRAME);
1154 :     *stats_type = xvid_enc_stats.type;
1155 :     *stats_quant = xvid_enc_stats.quant;
1156 :     *stats_length = xvid_enc_stats.length;
1157 :     sse[0] = xvid_enc_stats.sse_y;
1158 :     sse[1] = xvid_enc_stats.sse_u;
1159 :     sse[2] = xvid_enc_stats.sse_v;
1160 :    
1161 :     return (ret);
1162 : chl 376 }

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