[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 1710 - (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 : syskin 1710 * $Id: xvid_encraw.c,v 1.25 2006-06-16 10:08:28 syskin 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 : syskin 1710 #include <io.h>
43 : chl 376 #include <stdlib.h>
44 : edgomez 566 #include <string.h>
45 : edgomez 558 #include <math.h>
46 : edgomez 851 #ifndef WIN32
47 : edgomez 558 #include <sys/time.h>
48 :     #else
49 : Isibaar 1645 #include <windows.h>
50 :     #include <vfw.h>
51 : edgomez 558 #include <time.h>
52 : Isibaar 1645 #define XVID_AVI_INPUT
53 : syskin 1710 #define XVID_AVI_OUTPUT
54 : edgomez 558 #endif
55 : chl 376
56 : edgomez 558 #include "xvid.h"
57 : chl 376
58 : syskin 1710 #ifdef XVID_MKV_OUTPUT
59 :     #include "matroska.cpp"
60 :     #endif
61 :    
62 : edgomez 1382 #undef READ_PNM
63 :    
64 : edgomez 558 /*****************************************************************************
65 :     * Quality presets
66 :     ****************************************************************************/
67 : chl 376
68 : syskin 1710 // Equivalent to vfw's pmvfast_presets
69 : edgomez 1382 static const int motion_presets[] = {
70 :     /* quality 0 */
71 :     0,
72 :    
73 :     /* quality 1 */
74 : syskin 1710 0,
75 : edgomez 1382
76 :     /* quality 2 */
77 : syskin 1710 0,
78 : edgomez 1382
79 :     /* quality 3 */
80 : syskin 1710 0,
81 : edgomez 1382
82 :     /* quality 4 */
83 : syskin 1710 0 | XVID_ME_HALFPELREFINE16 | 0,
84 : edgomez 1382
85 :     /* quality 5 */
86 : syskin 1710 0 | XVID_ME_HALFPELREFINE16 | 0 | XVID_ME_ADVANCEDDIAMOND16,
87 : edgomez 1382
88 :     /* quality 6 */
89 : syskin 1710 XVID_ME_HALFPELREFINE16 | XVID_ME_EXTSEARCH16 | XVID_ME_HALFPELREFINE8 | 0 | XVID_ME_USESQUARES16
90 : edgomez 1382
91 : edgomez 558 };
92 : edgomez 1382 #define ME_ELEMENTS (sizeof(motion_presets)/sizeof(motion_presets[0]))
93 : edgomez 558
94 : edgomez 1382 static const int vop_presets[] = {
95 :     /* quality 0 */
96 :     0,
97 :    
98 :     /* quality 1 */
99 :     0,
100 :    
101 :     /* quality 2 */
102 : syskin 1710 0,
103 : edgomez 1382
104 :     /* quality 3 */
105 : syskin 1710 0,
106 : edgomez 1382
107 :     /* quality 4 */
108 : syskin 1710 0,
109 : edgomez 1382
110 :     /* quality 5 */
111 : syskin 1710 XVID_VOP_INTER4V,
112 : edgomez 1382
113 :     /* quality 6 */
114 : syskin 1710 XVID_VOP_INTER4V,
115 : edgomez 1382
116 : edgomez 558 };
117 : edgomez 1382 #define VOP_ELEMENTS (sizeof(vop_presets)/sizeof(vop_presets[0]))
118 : chl 376
119 : edgomez 558 /*****************************************************************************
120 :     * Command line global variables
121 :     ****************************************************************************/
122 : chl 376
123 : edgomez 1382 #define MAX_ZONES 64
124 :    
125 : syskin 1710 #define DEFAULT_QUANT 400
126 : edgomez 1382
127 : syskin 1710 typedef struct
128 :     {
129 :     int frame;
130 :    
131 :     int type;
132 :     int mode;
133 :     int modifier;
134 :    
135 :     unsigned int greyscale;
136 :     unsigned int chroma_opt;
137 :     unsigned int bvop_threshold;
138 :     unsigned int cartoon_mode;
139 :     } zone_t;
140 :    
141 :     typedef struct
142 :     {
143 :     int count;
144 :     int size;
145 :     int quants[32];
146 :     } frame_stats_t;
147 :    
148 : edgomez 558 /* Maximum number of frames to encode */
149 : Isibaar 1676 #define ABS_MAXFRAMENR -1 /* no limit */
150 : chl 376
151 : edgomez 1382 #ifndef READ_PNM
152 : edgomez 558 #define IMAGE_SIZE(x,y) ((x)*(y)*3/2)
153 : edgomez 1382 #else
154 :     #define IMAGE_SIZE(x,y) ((x)*(y)*3)
155 :     #endif
156 : chl 376
157 : edgomez 558 #define MAX(A,B) ( ((A)>(B)) ? (A) : (B) )
158 : edgomez 1382 #define SMALL_EPS (1e-10)
159 : chl 376
160 : edgomez 558 #define SWAP(a) ( (((a)&0x000000ff)<<24) | (((a)&0x0000ff00)<<8) | \
161 :     (((a)&0x00ff0000)>>8) | (((a)&0xff000000)>>24) )
162 : chl 376
163 : syskin 1710 static zone_t ZONES[MAX_ZONES];
164 :     static int NUM_ZONES = 0;
165 :     static frame_stats_t framestats[7];
166 :    
167 :     static int ARG_STATS = 0;
168 :     static int ARG_DUMP = 0;
169 :     static int ARG_LUMIMASKING = 0;
170 :     static int ARG_BITRATE = 0;
171 :     static int ARG_TARGETSIZE = 0;
172 :     static int ARG_SINGLE = 1;
173 :     static char *ARG_PASS1 = 0;
174 :     static char *ARG_PASS2 = 0;
175 :     //static int ARG_QUALITY = ME_ELEMENTS - 1;
176 :     static int ARG_QUALITY = 6;
177 :     static float ARG_FRAMERATE = 0.00f;
178 :     static int ARG_DWRATE = 25;
179 :     static int ARG_DWSCALE = 1;
180 :     static int ARG_MAXFRAMENR = ABS_MAXFRAMENR;
181 :     static int ARG_MAXKEYINTERVAL = 300;
182 :     static int ARG_STARTFRAMENR = 0;
183 :     static char *ARG_INPUTFILE = NULL;
184 :     static int ARG_INPUTTYPE = 0;
185 :     static int ARG_SAVEMPEGSTREAM = 0;
186 :     static int ARG_SAVEINDIVIDUAL = 0;
187 :     static char *ARG_OUTPUTFILE = NULL;
188 :     #ifdef XVID_AVI_OUTPUT
189 :     static char *ARG_AVIOUTPUTFILE = NULL;
190 :     #endif
191 :     #ifdef XVID_MKV_OUTPUT
192 :     static char *ARG_MKVOUTPUTFILE = NULL;
193 :     #endif
194 :     #ifdef XVID_AVI_INPUT
195 :     static PAVISTREAM avi_stream = NULL;
196 :     static PAVIFILE avi_file = NULL;
197 :     static LPBITMAPINFOHEADER info_header = NULL;
198 :     static PGETFRAME get_frame = NULL;
199 :     #endif
200 :     static char *ARG_TIMECODEFILE = NULL;
201 :     static int XDIM = 0;
202 :     static int YDIM = 0;
203 :     static int ARG_BQRATIO = 150;
204 :     static int ARG_BQOFFSET = 100;
205 :     static int ARG_MAXBFRAMES = 2;
206 :     static int ARG_PACKED = 1;
207 :     static int ARG_DEBUG = 0;
208 :     static int ARG_VOPDEBUG = 0;
209 :     static int ARG_TRELLIS = 1;
210 :     static int ARG_QTYPE = 0;
211 :     static int ARG_QMATRIX = 0;
212 :     static int ARG_GMC = 0;
213 :     static int ARG_INTERLACING = 0;
214 :     static int ARG_QPEL = 0;
215 :     static int ARG_TURBO = 0;
216 :     static int ARG_VHQMODE = 1;
217 :     static int ARG_BVHQ = 0;
218 :     static int ARG_CLOSED_GOP = 1;
219 :     static int ARG_CHROMAME = 1;
220 :     static int ARG_PAR = 1;
221 :     static int ARG_PARHEIGHT;
222 :     static int ARG_PARWIDTH;
223 :     static int ARG_QUANTS[6] = {2, 31, 2, 31, 2, 31};
224 :     static int ARG_FRAMEDROP = 0;
225 :     static double ARG_CQ = 0;
226 :     static int ARG_FULL1PASS = 0;
227 :     static int ARG_REACTION = 16;
228 :     static int ARG_AVERAGING = 100;
229 :     static int ARG_SMOOTHER = 100;
230 :     static int ARG_KBOOST = 10;
231 :     static int ARG_KREDUCTION = 20;
232 :     static int ARG_KTHRESH = 1;
233 :     static int ARG_CHIGH = 0;
234 :     static int ARG_CLOW = 0;
235 :     static int ARG_OVERSTRENGTH = 5;
236 :     static int ARG_OVERIMPROVE = 5;
237 :     static int ARG_OVERDEGRADE = 5;
238 :     static int ARG_OVERHEAD = 0;
239 :     static int ARG_VBVSIZE = 0;
240 :     static int ARG_VBVMAXRATE = 0;
241 :     static int ARG_VBVPEAKRATE = 0;
242 :     static int ARG_THREADS = 0;
243 :     static int ARG_VFR = 0;
244 :     static int ARG_PROGRESS = 0;
245 :     static int ARG_COLORSPACE = XVID_CSP_YV12;
246 :     /* the path where to save output */
247 :     static char filepath[256] = "./";
248 :     /* Internal structures (handles) for encoding and decoding */
249 :     static void *enc_handle = NULL;
250 :     static unsigned char qmatrix_intra[64];
251 :     static unsigned char qmatrix_inter[64];
252 :    
253 : edgomez 558 /****************************************************************************
254 :     * Nasty global vars ;-)
255 :     ***************************************************************************/
256 : chl 376
257 : syskin 1710 static const int height_ratios[] = {1, 1, 11, 11, 11, 33};
258 :     static const int width_ratios[] = {1, 1, 12, 10, 16, 40};
259 : chl 376
260 : syskin 1710 const char userdata_start_code[] = "\0\0\x01\xb2";
261 : chl 376
262 : edgomez 558
263 :     /*****************************************************************************
264 :     * Local prototypes
265 :     ****************************************************************************/
266 :    
267 :     /* Prints program usage message */
268 :     static void usage();
269 :    
270 :     /* Statistical functions */
271 :     static double msecond();
272 : syskin 1710 int gcd(int a, int b);
273 :     int minquant(int quants[32]);
274 :     int maxquant(int quants[32]);
275 :     double avgquant(frame_stats_t frame);
276 : edgomez 558
277 :     /* PGM related functions */
278 : edgomez 1382 #ifndef READ_PNM
279 :     static int read_pgmheader(FILE * handle);
280 :     static int read_pgmdata(FILE * handle,
281 :     unsigned char *image);
282 :     #else
283 :     static int read_pnmheader(FILE * handle);
284 :     static int read_pnmdata(FILE * handle,
285 :     unsigned char *image);
286 :     #endif
287 :     static int read_yuvdata(FILE * handle,
288 :     unsigned char *image);
289 : edgomez 558
290 :     /* Encoder related functions */
291 :     static int enc_init(int use_assembler);
292 : syskin 1710 static int enc_info();
293 : edgomez 558 static int enc_stop();
294 : edgomez 1382 static int enc_main(unsigned char *image,
295 :     unsigned char *bitstream,
296 :     int *key,
297 :     int *stats_type,
298 :     int *stats_quant,
299 :     int *stats_length,
300 : syskin 1710 int stats[3],
301 :     int framenum);
302 : edgomez 558
303 : syskin 1710 /* Zone Related Functions */
304 :     static void apply_zone_modifiers(xvid_enc_frame_t * frame, int framenum);
305 :     static void prepare_full1pass_zones();
306 :     static void prepare_cquant_zones();
307 :     void sort_zones(zone_t * zones, int zone_num, int * sel);
308 :    
309 :    
310 :     void removedivxp(char *buf, int size);
311 :    
312 : edgomez 558 /*****************************************************************************
313 :     * Main function
314 :     ****************************************************************************/
315 :    
316 : edgomez 1382 int
317 :     main(int argc,
318 :     char *argv[])
319 : edgomez 558 {
320 :    
321 :     unsigned char *mp4_buffer = NULL;
322 :     unsigned char *in_buffer = NULL;
323 :     unsigned char *out_buffer = NULL;
324 :    
325 :     double enctime;
326 : edgomez 1382 double totalenctime = 0.;
327 :     float totalPSNR[3] = {0., 0., 0.};
328 : syskin 1710 FILE *statsfile;
329 : edgomez 1382
330 :     int totalsize;
331 :     int result;
332 :     int m4v_size;
333 :     int key;
334 :     int stats_type;
335 :     int stats_quant;
336 :     int stats_length;
337 : Isibaar 1627 int use_assembler = 1;
338 : syskin 1710 int fakenvop = 0;
339 :     int i;
340 :     int nvop_counter;
341 : edgomez 1382
342 :     int input_num;
343 :     int output_num;
344 :    
345 : edgomez 558 char filename[256];
346 : edgomez 1382
347 : edgomez 558 FILE *in_file = stdin;
348 :     FILE *out_file = NULL;
349 : syskin 1710 FILE *time_file = NULL;
350 : edgomez 558
351 : syskin 1710 #ifdef XVID_AVI_OUTPUT
352 :     int avierr;
353 :     PAVIFILE myAVIFile=NULL;
354 :     PAVISTREAM myAVIStream=NULL;
355 :     AVISTREAMINFO myAVIStreamInfo;
356 :     BITMAPINFOHEADER myBitmapInfoHeader;
357 :     AVIFileInit();
358 :     #endif
359 :     #ifdef XVID_MKV_OUTPUT
360 :     PMKVFILE myMKVFile=NULL;
361 :     PMKVSTREAM myMKVStream=NULL;
362 :     MKVSTREAMINFO myMKVStreamInfo;
363 :     #endif
364 :    
365 : edgomez 741 printf("xvid_encraw - raw mpeg4 bitstream encoder ");
366 : edgomez 851 printf("written by Christoph Lampert 2002-2003\n\n");
367 : edgomez 558
368 : edgomez 1382 /* Is there a dumb XviD coder ? */
369 :     if(ME_ELEMENTS != VOP_ELEMENTS) {
370 :     fprintf(stderr, "Presets' arrays should have the same number of elements -- Please fill a bug to xvid-devel@xvid.org\n");
371 :     return(-1);
372 :     }
373 :    
374 : edgomez 558 /*****************************************************************************
375 :     * Command line parsing
376 :     ****************************************************************************/
377 :    
378 : edgomez 1382 for (i = 1; i < argc; i++) {
379 :    
380 :     if (strcmp("-asm", argv[i]) == 0) {
381 : edgomez 558 use_assembler = 1;
382 : chl 1623 } else if (strcmp("-noasm", argv[i]) == 0) {
383 :     use_assembler = 0;
384 : edgomez 1382 } else if (strcmp("-w", argv[i]) == 0 && i < argc - 1) {
385 : edgomez 558 i++;
386 :     XDIM = atoi(argv[i]);
387 : edgomez 1382 } else if (strcmp("-h", argv[i]) == 0 && i < argc - 1) {
388 : edgomez 558 i++;
389 :     YDIM = atoi(argv[i]);
390 : syskin 1710 } else if (strcmp("-bitrate", argv[i]) == 0) {
391 :     if (i < argc - 1)
392 :     ARG_BITRATE = atoi(argv[i+1]);
393 :     if (ARG_BITRATE) {
394 :     i++;
395 :     if (ARG_BITRATE <= 10000)
396 :     /* if given parameter is <= 10000, assume it means kbps */
397 :     ARG_BITRATE *= 1000;
398 :     }
399 :     else
400 :     ARG_BITRATE = 700000;
401 :     } else if (strcmp("-size", argv[i]) == 0 && i < argc - 1) {
402 : edgomez 558 i++;
403 : syskin 1710 ARG_TARGETSIZE = atoi(argv[i]);
404 :     } else if (strcmp("-cq", argv[i]) == 0 && i < argc - 1) {
405 :     i++;
406 :     ARG_CQ = atof(argv[i])*100;
407 : edgomez 1382 } else if (strcmp("-single", argv[i]) == 0) {
408 :     ARG_SINGLE = 1;
409 : syskin 1710 ARG_PASS1 = NULL;
410 :     ARG_PASS2 = NULL;
411 :     } else if (strcmp("-pass1", argv[i]) == 0) {
412 :     ARG_SINGLE = 0;
413 :     if ((i < argc - 1) && (*argv[i+1] != '-')) {
414 :     i++;
415 :     ARG_PASS1 = argv[i];
416 :     } else {
417 :     ARG_PASS1 = "xvid.stats";
418 :     }
419 :     } else if (strcmp("-full1pass", argv[i]) == 0) {
420 :     ARG_FULL1PASS = 1;
421 :     } else if (strcmp("-pass2", argv[i]) == 0) {
422 :     ARG_SINGLE = 0;
423 :     if ((i < argc - 1) && (*argv[i+1] != '-')) {
424 :     i++;
425 :     ARG_PASS2 = argv[i];
426 :     } else {
427 :     ARG_PASS2 = "xvid.stats";
428 :     }
429 : edgomez 1382 } else if (strcmp("-max_bframes", argv[i]) == 0 && i < argc - 1) {
430 :     i++;
431 : edgomez 851 ARG_MAXBFRAMES = atoi(argv[i]);
432 : syskin 1710 } else if (strcmp("-par", argv[i]) == 0 && i < argc - 1) {
433 :     i++;
434 :     if (sscanf(argv[i], "%d:%d", &(ARG_PARWIDTH), &(ARG_PARHEIGHT))!=2)
435 :     ARG_PAR = atoi(argv[i]);
436 :     else {
437 :     int div;
438 :     ARG_PAR = 0;
439 :     div = gcd(ARG_PARWIDTH, ARG_PARHEIGHT);
440 :     ARG_PARWIDTH /= div;
441 :     ARG_PARHEIGHT /= div;
442 :     }
443 :     } else if (strcmp("-nopacked", argv[i]) == 0) {
444 :     ARG_PACKED = 0;
445 : edgomez 1382 } else if (strcmp("-packed", argv[i]) == 0) {
446 : syskin 1710 ARG_PACKED = 2;
447 :     } else if (strcmp("-nochromame", argv[i]) == 0) {
448 :     ARG_CHROMAME = 0;
449 :     } else if (strcmp("-threads", argv[i]) == 0 && i < argc -1) {
450 :     i++;
451 :     ARG_THREADS = atoi(argv[i]);
452 : edgomez 1382 } else if (strcmp("-bquant_ratio", argv[i]) == 0 && i < argc - 1) {
453 : edgomez 851 i++;
454 :     ARG_BQRATIO = atoi(argv[i]);
455 : edgomez 1382 } else if (strcmp("-bquant_offset", argv[i]) == 0 && i < argc - 1) {
456 : edgomez 851 i++;
457 :     ARG_BQOFFSET = atoi(argv[i]);
458 : edgomez 1382
459 : syskin 1710 } else if (strcmp("-zones", argv[i]) == 0 && i < argc -1) {
460 :     char c;
461 :     char *frameoptions, *rem;
462 :     int startframe;
463 :     char options[40];
464 :    
465 :     i++;
466 : edgomez 1382
467 : syskin 1710 do {
468 :     rem = strrchr(argv[i], '/');
469 :     if (rem==NULL)
470 :     rem=argv[i];
471 :     else {
472 :     *rem = '\0';
473 :     rem++;
474 :     }
475 :     if (sscanf(rem, "%d,%c,%s", &startframe, &c, options)<3) {
476 :     fprintf(stderr, "Zone error, bad parameters %s\n", rem);
477 :     continue;
478 :     }
479 :     if (NUM_ZONES >= MAX_ZONES) {
480 :     fprintf(stderr, "warning: too many zones; zone ignored\n");
481 :     continue;
482 :     }
483 :     memset(&ZONES[NUM_ZONES], 0, sizeof(zone_t));
484 :    
485 :     ZONES[NUM_ZONES].frame = startframe;
486 :     ZONES[NUM_ZONES].modifier = atof(options)*100;
487 :     if (toupper(c)=='Q')
488 :     ZONES[NUM_ZONES].mode = XVID_ZONE_QUANT;
489 :     else if (toupper(c)=='W')
490 :     ZONES[NUM_ZONES].mode = XVID_ZONE_WEIGHT;
491 :     else {
492 :     fprintf(stderr, "Bad zone type %c\n", c);
493 :     continue;
494 :     }
495 :    
496 :     if ((frameoptions=strchr(options, ','))!=NULL) {
497 :     int readchar=0, count;
498 :     frameoptions++;
499 :     while (readchar<strlen(frameoptions)) {
500 :     if (sscanf(frameoptions+readchar, "%d%n", &(ZONES[NUM_ZONES].bvop_threshold), &count)==1) {
501 :     readchar += count;
502 :     }
503 :     else {
504 :     if (toupper(frameoptions[readchar])=='K')
505 :     ZONES[NUM_ZONES].type = XVID_TYPE_IVOP;
506 :     else if (toupper(frameoptions[readchar])=='G')
507 :     ZONES[NUM_ZONES].greyscale = 1;
508 :     else if (toupper(frameoptions[readchar])=='O')
509 :     ZONES[NUM_ZONES].chroma_opt = 1;
510 :     else if (toupper(frameoptions[readchar])=='C')
511 :     ZONES[NUM_ZONES].cartoon_mode = 1;
512 :     else {
513 :     fprintf(stderr, "Error in zone %s option %c\n", rem, frameoptions[readchar]);
514 :     break;
515 :     }
516 :     readchar++;
517 :     }
518 :     }
519 :     }
520 :     NUM_ZONES++;
521 :     } while (rem != argv[i]);
522 :    
523 :    
524 :     } else if ((strcmp("-zq", argv[i]) == 0 || strcmp("-zw", argv[i]) == 0) && i < argc - 2) {
525 :    
526 : edgomez 1382 if (NUM_ZONES >= MAX_ZONES) {
527 :     fprintf(stderr,"warning: too many zones; zone ignored\n");
528 :     continue;
529 :     }
530 : syskin 1710 memset(&ZONES[NUM_ZONES], 0, sizeof(zone_t));
531 :     if (strcmp("-zq", argv[i])== 0) {
532 :     ZONES[NUM_ZONES].mode = XVID_ZONE_QUANT;
533 :     }
534 :     else {
535 :     ZONES[NUM_ZONES].mode = XVID_ZONE_WEIGHT;
536 :     }
537 :     ZONES[NUM_ZONES].modifier = atof(argv[i+2])*100;
538 : edgomez 558 i++;
539 : syskin 1710 ZONES[NUM_ZONES].frame = atoi(argv[i]);
540 :     i++;
541 :     ZONES[NUM_ZONES].type = XVID_TYPE_AUTO;
542 :     ZONES[NUM_ZONES].greyscale = 0;
543 :     ZONES[NUM_ZONES].chroma_opt = 0;
544 :     ZONES[NUM_ZONES].bvop_threshold = 0;
545 :     ZONES[NUM_ZONES].cartoon_mode = 0;
546 :    
547 : edgomez 1382 NUM_ZONES++;
548 :     } else if (strcmp("-quality", argv[i]) == 0 && i < argc - 1) {
549 :     i++;
550 : edgomez 558 ARG_QUALITY = atoi(argv[i]);
551 : syskin 1710 } else if (strcmp("-start", argv[i]) == 0 && i < argc - 1) {
552 :     i++;
553 :     ARG_STARTFRAMENR = atoi(argv[i]);
554 : chl 1624 } else if (strcmp("-vhqmode", argv[i]) == 0 && i < argc - 1) {
555 :     i++;
556 :     ARG_VHQMODE = atoi(argv[i]);
557 : edgomez 1382 } else if (strcmp("-framerate", argv[i]) == 0 && i < argc - 1) {
558 : syskin 1710 int exponent;
559 : edgomez 558 i++;
560 : edgomez 1382 ARG_FRAMERATE = (float) atof(argv[i]);
561 : syskin 1710 exponent = strcspn(argv[i], ".");
562 :     if (exponent<strlen(argv[i]))
563 :     exponent=pow(10.0, (int)(strlen(argv[i])-1-exponent));
564 :     else
565 :     exponent=1;
566 :     ARG_DWRATE = atof(argv[i])*exponent;
567 :     ARG_DWSCALE = exponent;
568 :     exponent = gcd(ARG_DWRATE, ARG_DWSCALE);
569 :     ARG_DWRATE /= exponent;
570 :     ARG_DWSCALE /= exponent;
571 : edgomez 1382 } else if (strcmp("-max_key_interval", argv[i]) == 0 && i < argc - 1) {
572 : edgomez 558 i++;
573 : edgomez 1382 ARG_MAXKEYINTERVAL = atoi(argv[i]);
574 :     } else if (strcmp("-i", argv[i]) == 0 && i < argc - 1) {
575 :     i++;
576 : edgomez 558 ARG_INPUTFILE = argv[i];
577 : edgomez 1382 } else if (strcmp("-stats", argv[i]) == 0) {
578 :     ARG_STATS = 1;
579 : syskin 1710 } else if (strcmp("-timecode", argv[i]) == 0 && i < argc -1) {
580 :     i++;
581 :     ARG_TIMECODEFILE = argv[i];
582 : edgomez 1382 } else if (strcmp("-dump", argv[i]) == 0) {
583 :     ARG_DUMP = 1;
584 :     } else if (strcmp("-lumimasking", argv[i]) == 0) {
585 :     ARG_LUMIMASKING = 1;
586 :     } else if (strcmp("-type", argv[i]) == 0 && i < argc - 1) {
587 : edgomez 558 i++;
588 :     ARG_INPUTTYPE = atoi(argv[i]);
589 : edgomez 1382 } else if (strcmp("-frames", argv[i]) == 0 && i < argc - 1) {
590 : edgomez 558 i++;
591 : edgomez 1382 ARG_MAXFRAMENR = atoi(argv[i]);
592 : syskin 1710 } else if (strcmp("-drop", argv[i]) == 0 && i < argc - 1) {
593 :     i++;
594 :     ARG_FRAMEDROP = atoi(argv[i]);
595 :     } else if (strcmp("-imin", argv[i]) == 0 && i < argc - 1) {
596 :     i++;
597 :     ARG_QUANTS[0] = atoi(argv[i]);
598 :     } else if (strcmp("-imax", argv[i]) == 0 && i < argc - 1) {
599 :     i++;
600 :     ARG_QUANTS[1] = atoi(argv[i]);
601 :     } else if (strcmp("-pmin", argv[i]) == 0 && i < argc - 1) {
602 :     i++;
603 :     ARG_QUANTS[2] = atoi(argv[i]);
604 :     } else if (strcmp("-pmax", argv[i]) == 0 && i < argc - 1) {
605 :     i++;
606 :     ARG_QUANTS[3] = atoi(argv[i]);
607 :     } else if (strcmp("-bmin", argv[i]) == 0 && i < argc - 1) {
608 :     i++;
609 :     ARG_QUANTS[4] = atoi(argv[i]);
610 :     } else if (strcmp("-bmax", argv[i]) == 0 && i < argc - 1) {
611 :     i++;
612 :     ARG_QUANTS[5] = atoi(argv[i]);
613 : Isibaar 1645 } else if (strcmp("-qtype", argv[i]) == 0 && i < argc - 1) {
614 :     i++;
615 :     ARG_QTYPE = atoi(argv[i]);
616 :     } else if (strcmp("-qmatrix", argv[i]) == 0 && i < argc - 1) {
617 :     FILE *fp = fopen(argv[++i], "rb");
618 :     if (fp == NULL) {
619 :     fprintf(stderr, "Error opening input file %s\n", argv[i]);
620 :     return (-1);
621 :     }
622 :     fseek(fp, 0, SEEK_END);
623 :     if (ftell(fp) != 128) {
624 :     fprintf(stderr, "Unexpected size of input file %s\n", argv[i]);
625 :     return (-1);
626 :     }
627 :    
628 :     fseek(fp, 0, SEEK_SET);
629 :     fread(qmatrix_intra, 1, 64, fp);
630 :     fread(qmatrix_inter, 1, 64, fp);
631 :    
632 :     ARG_QMATRIX = 1;
633 : syskin 1710 ARG_QTYPE = 1;
634 : edgomez 1382 } else if (strcmp("-save", argv[i]) == 0) {
635 :     ARG_SAVEMPEGSTREAM = 1;
636 :     ARG_SAVEINDIVIDUAL = 1;
637 : syskin 1710 } else if (strcmp("-debug", argv[i]) == 0 && i < argc -1) {
638 : edgomez 558 i++;
639 : syskin 1710 if (!(sscanf(argv[i],"0x%x", &(ARG_DEBUG))))
640 :     sscanf(argv[i],"%d", &(ARG_DEBUG));
641 : edgomez 1382 } else if (strcmp("-o", argv[i]) == 0 && i < argc - 1) {
642 :     ARG_SAVEMPEGSTREAM = 1;
643 : edgomez 558 i++;
644 :     ARG_OUTPUTFILE = argv[i];
645 : syskin 1710 } else if (strcmp("-avi", argv[i]) == 0 && i < argc - 1) {
646 :     #ifdef XVID_AVI_OUTPUT
647 :     ARG_SAVEMPEGSTREAM = 1;
648 :     i++;
649 :     ARG_AVIOUTPUTFILE = argv[i];
650 :     #else
651 :     fprintf("Not compiled with AVI output support.\n");
652 :     return(-1);
653 :     #endif
654 :     } else if (strcmp("-mkv", argv[i]) == 0 && i < argc - 1) {
655 :     #ifdef XVID_MKV_OUTPUT
656 :     ARG_SAVEMPEGSTREAM = 1;
657 :     i++;
658 :     ARG_MKVOUTPUTFILE = argv[i];
659 :     #else
660 :     fprintf(stderr, "Not compiled with MKV output support.\n");
661 :     return(-1);
662 :     #endif
663 : edgomez 1382 } else if (strcmp("-vop_debug", argv[i]) == 0) {
664 :     ARG_VOPDEBUG = 1;
665 : syskin 1710 } else if (strcmp("-notrellis", argv[i]) == 0) {
666 :     ARG_TRELLIS = 0;
667 : Isibaar 1645 } else if (strcmp("-bvhq", argv[i]) == 0) {
668 :     ARG_BVHQ = 1;
669 : Isibaar 1627 } else if (strcmp("-qpel", argv[i]) == 0) {
670 : edgomez 1382 ARG_QPEL = 1;
671 : Isibaar 1645 } else if (strcmp("-turbo", argv[i]) == 0) {
672 :     ARG_TURBO = 1;
673 : edgomez 1382 } else if (strcmp("-gmc", argv[i]) == 0) {
674 :     ARG_GMC = 1;
675 : chl 1559 } else if (strcmp("-interlaced", argv[i]) == 0) {
676 : syskin 1710 if ((i < argc - 1) && (*argv[i+1] != '-')) {
677 :     i++;
678 :     ARG_INTERLACING = atoi(argv[i]);
679 :     } else {
680 :     ARG_INTERLACING = 1;
681 :     }
682 :     } else if (strcmp("-noclosed_gop", argv[i]) == 0) {
683 :     ARG_CLOSED_GOP = 0;
684 :     } else if (strcmp("-closed_gop", argv[i]) == 0) {
685 :     ARG_CLOSED_GOP = 2;
686 :     } else if (strcmp("-vbvsize", argv[i]) == 0 && i < argc -1) {
687 : syskin 1683 i++;
688 : syskin 1710 ARG_VBVSIZE = atoi(argv[i]);
689 :     } else if (strcmp("-vbvmax", argv[i]) == 0 && i < argc -1) {
690 :     i++;
691 :     ARG_VBVMAXRATE = atoi(argv[i]);
692 :     } else if (strcmp("-vbvpeak", argv[i]) == 0 && i < argc -1) {
693 :     i++;
694 :     ARG_VBVPEAKRATE = atoi(argv[i])*3;
695 :     } else if (strcmp("-reaction", argv[i]) == 0 && i < argc -1) {
696 :     i++;
697 :     ARG_REACTION = atoi(argv[i]);
698 :     } else if (strcmp("-averaging", argv[i]) == 0 && i < argc -1) {
699 :     i++;
700 :     ARG_AVERAGING = atoi(argv[i]);
701 :     } else if (strcmp("-smoother", argv[i]) == 0 && i < argc -1) {
702 :     i++;
703 :     ARG_SMOOTHER = atoi(argv[i]);
704 :     } else if (strcmp("-kboost", argv[i]) == 0 && i < argc -1) {
705 :     i++;
706 :     ARG_KBOOST = atoi(argv[i]);
707 :     } else if (strcmp("-kthresh", argv[i]) == 0 && i < argc -1) {
708 :     i++;
709 :     ARG_KTHRESH = atoi(argv[i]);
710 :     } else if (strcmp("-chigh", argv[i]) == 0 && i < argc -1) {
711 :     i++;
712 :     ARG_CHIGH = atoi(argv[i]);
713 :     } else if (strcmp("-clow", argv[i]) == 0 && i < argc -1) {
714 :     i++;
715 :     ARG_CLOW = atoi(argv[i]);
716 :     } else if (strcmp("-ostrength", argv[i]) == 0 && i < argc -1) {
717 :     i++;
718 :     ARG_OVERSTRENGTH = atoi(argv[i]);
719 :     } else if (strcmp("-oimprove", argv[i]) == 0 && i < argc -1) {
720 :     i++;
721 :     ARG_OVERIMPROVE = atoi(argv[i]);
722 :     } else if (strcmp("-odegrade", argv[i]) == 0 && i < argc -1) {
723 :     i++;
724 :     ARG_OVERDEGRADE = atoi(argv[i]);
725 :     } else if (strcmp("-overhead", argv[i]) == 0 && i < argc -1) {
726 :     i++;
727 :     ARG_OVERHEAD = atoi(argv[i]);
728 :     } else if (strcmp("-kreduction", argv[i]) == 0 && i < argc -1) {
729 :     i++;
730 :     ARG_KREDUCTION = atoi(argv[i]);
731 :     } else if (strcmp("-progress", argv[i]) == 0) {
732 :     if (i < argc - 1)
733 :     /* in kbps */
734 :     ARG_PROGRESS = atoi(argv[i+1]);
735 :     if (ARG_PROGRESS > 0)
736 :     i++;
737 :     else
738 :     ARG_PROGRESS = 10;
739 : edgomez 1382 } else if (strcmp("-help", argv[i])) {
740 : edgomez 558 usage();
741 : edgomez 1382 return (0);
742 :     } else {
743 : edgomez 558 usage();
744 :     exit(-1);
745 :     }
746 : edgomez 1382
747 : edgomez 558 }
748 : edgomez 1382
749 : edgomez 558 /*****************************************************************************
750 :     * Arguments checking
751 :     ****************************************************************************/
752 :    
753 : edgomez 1382 if (XDIM <= 0 || XDIM >= 4096 || YDIM <= 0 || YDIM >= 4096) {
754 :     fprintf(stderr,
755 : Isibaar 1645 "Trying to retrieve width and height from input header\n");
756 :     if (!ARG_INPUTTYPE)
757 :     ARG_INPUTTYPE = 1; /* pgm */
758 : edgomez 558 }
759 :    
760 : edgomez 1382 if (ARG_QUALITY < 0 ) {
761 :     ARG_QUALITY = 0;
762 :     } else if (ARG_QUALITY >= ME_ELEMENTS) {
763 :     ARG_QUALITY = ME_ELEMENTS - 1;
764 : edgomez 558 }
765 :    
766 : syskin 1710 if (ARG_STARTFRAMENR < 0) {
767 :     fprintf(stderr, "Bad starting frame number %d, cannot be negative\n", ARG_STARTFRAMENR);
768 :     return(-1);
769 : edgomez 558 }
770 :    
771 : syskin 1710 if (ARG_PASS2) {
772 :     if (ARG_PASS2 == ARG_PASS1) {
773 :     fprintf(stderr, "Can't use the same statsfile for pass1 and pass2: %s\n", ARG_PASS2);
774 :     return(-1);
775 :     }
776 :     statsfile = fopen(ARG_PASS2, "rb");
777 :     if (statsfile == NULL) {
778 :     fprintf(stderr, "Couldn't open statsfile '%s'!\n", ARG_PASS2);
779 :     return (-1);
780 :     }
781 :     fclose(statsfile);
782 :     }
783 :    
784 :     #ifdef XVID_AVI_OUTPUT
785 :     if (ARG_AVIOUTPUTFILE == NULL && ARG_PACKED <= 1)
786 :     ARG_PACKED = 0;
787 :     #endif
788 :    
789 :     if (ARG_BITRATE < 0) {
790 :     fprintf(stderr, "Bad bitrate %d, cannot be negative\n", ARG_BITRATE);
791 :     return(-1);
792 :     }
793 :    
794 :     if (NUM_ZONES) {
795 :     int i;
796 :     sort_zones(ZONES, NUM_ZONES, &i);
797 :     }
798 :    
799 :     if (ARG_PAR > 5) {
800 :     fprintf(stderr, "Bad PAR: %d. Must be [1..5] or width:height\n", ARG_PAR);
801 :     return(-1);
802 :     }
803 :    
804 : Isibaar 1676 if (ARG_MAXFRAMENR == 0) {
805 : edgomez 1382 fprintf(stderr, "Wrong number of frames\n");
806 :     return (-1);
807 : edgomez 558 }
808 :    
809 : edgomez 1382 if (ARG_INPUTFILE == NULL || strcmp(ARG_INPUTFILE, "stdin") == 0) {
810 : edgomez 558 in_file = stdin;
811 : edgomez 1382 } else {
812 : Isibaar 1645 #ifdef XVID_AVI_INPUT
813 :     if (strcmp(ARG_INPUTFILE+(strlen(ARG_INPUTFILE)-3), "avs")==0 ||
814 :     strcmp(ARG_INPUTFILE+(strlen(ARG_INPUTFILE)-3), "avi")==0 ||
815 :     ARG_INPUTTYPE==2)
816 :     {
817 :     AVISTREAMINFO avi_info;
818 : syskin 1710 #ifndef XVID_AVI_OUTPUT
819 :     AVIFileInit();
820 :     #endif
821 : Isibaar 1676 FILE *avi_fp = fopen(ARG_INPUTFILE, "rb");
822 :     if (avi_fp == NULL) {
823 :     fprintf(stderr, "Couldn't open file '%s'!\n", ARG_INPUTFILE);
824 :     return (-1);
825 :     }
826 :     fclose(avi_fp);
827 : edgomez 558
828 : syskin 1710 if (AVIFileOpen(&avi_file, ARG_INPUTFILE, OF_READ, NULL) != AVIERR_OK) {
829 :     fprintf(stderr, "Can't open avi/avs file %s\n", ARG_INPUTFILE);
830 :     AVIFileExit();
831 :     return(-1);
832 :     }
833 :    
834 :     if (AVIFileGetStream(avi_file, &avi_stream, streamtypeVIDEO, 0) != AVIERR_OK) {
835 : Isibaar 1645 fprintf(stderr, "Can't open stream from file '%s'!\n", ARG_INPUTFILE);
836 : syskin 1710 AVIFileRelease(avi_file);
837 : Isibaar 1645 AVIFileExit();
838 :     return (-1);
839 :     }
840 :    
841 : syskin 1710 AVIFileRelease(avi_file);
842 :    
843 : Isibaar 1645 if(AVIStreamInfo(avi_stream, &avi_info, sizeof(AVISTREAMINFO)) != AVIERR_OK) {
844 :     fprintf(stderr, "Can't get stream info from file '%s'!\n", ARG_INPUTFILE);
845 :     AVIStreamRelease(avi_stream);
846 :     AVIFileExit();
847 :     return (-1);
848 :     }
849 :    
850 :     if (avi_info.fccHandler != MAKEFOURCC('Y', 'V', '1', '2')) {
851 : syskin 1710 LONG size;
852 :     fprintf(stderr, "Non YV12 input colorspace %c%c%c%c! Attempting conversion...\n",
853 :     avi_info.fccHandler%256, (avi_info.fccHandler>>8)%256, (avi_info.fccHandler>>16)%256,
854 :     (avi_info.fccHandler>>24)%256);
855 :     size = sizeof(myBitmapInfoHeader);
856 :     AVIStreamReadFormat(avi_stream, 0, &myBitmapInfoHeader, &size);
857 :     if (size==0)
858 :     fprintf(stderr, "AVIStreamReadFormat read 0 bytes.\n");
859 :     else {
860 :     fprintf(stderr, "AVIStreamReadFormat read %d bytes.\n", size);
861 :     fprintf(stderr, "width = %d, height = %d, planes = %d\n", myBitmapInfoHeader.biWidth,
862 :     myBitmapInfoHeader.biHeight, myBitmapInfoHeader.biPlanes);
863 :     fprintf(stderr, "Compression = %c%c%c%c, %d\n",
864 :     myBitmapInfoHeader.biCompression%256, (myBitmapInfoHeader.biCompression>>8)%256,
865 :     (myBitmapInfoHeader.biCompression>>16)%256, (myBitmapInfoHeader.biCompression>>24)%256,
866 :     myBitmapInfoHeader.biCompression);
867 :     fprintf(stderr, "Bits Per Pixel = %d\n", myBitmapInfoHeader.biBitCount);
868 :     myBitmapInfoHeader.biCompression = MAKEFOURCC('Y', 'V', '1', '2');
869 :     myBitmapInfoHeader.biBitCount = 12;
870 :     myBitmapInfoHeader.biSizeImage = (myBitmapInfoHeader.biWidth*myBitmapInfoHeader.biHeight)*3/2;
871 :     get_frame = AVIStreamGetFrameOpen(avi_stream, &myBitmapInfoHeader);
872 :     }
873 :     if (get_frame == NULL) {
874 :     AVIStreamRelease(avi_stream);
875 :     AVIFileExit();
876 :     return (-1);
877 :     }
878 :     else {
879 :     unsigned char *temp;
880 :     fprintf(stderr, "AVIStreamGetFrameOpen successful.\n");
881 :     temp = (unsigned char*)AVIStreamGetFrame(get_frame, 0);
882 :     if (temp != NULL) {
883 :     int i;
884 :     for (i = 0; i < ((DWORD*)temp)[0]; i++) {
885 :     fprintf(stderr, "%2d ", temp[i]);
886 :     }
887 :     fprintf(stderr, "\n");
888 :     }
889 :     }
890 :     if (avi_info.fccHandler == MAKEFOURCC('D', 'I', 'B', ' ')) {
891 :     AVIStreamGetFrameClose(get_frame);
892 :     get_frame = NULL;
893 :     ARG_COLORSPACE = XVID_CSP_BGR | XVID_CSP_VFLIP;
894 :     }
895 : Isibaar 1645 }
896 : Isibaar 1676
897 :     if (ARG_MAXFRAMENR<0)
898 : syskin 1710 ARG_MAXFRAMENR = avi_info.dwLength-ARG_STARTFRAMENR;
899 : Isibaar 1676 else
900 : syskin 1710 ARG_MAXFRAMENR = min(ARG_MAXFRAMENR, avi_info.dwLength-ARG_STARTFRAMENR);
901 : suxen_drol 1646
902 : Isibaar 1645 XDIM = avi_info.rcFrame.right - avi_info.rcFrame.left;
903 :     YDIM = avi_info.rcFrame.bottom - avi_info.rcFrame.top;
904 : syskin 1710 if (ARG_FRAMERATE==0) {
905 :     ARG_FRAMERATE = (float) avi_info.dwRate / (float) avi_info.dwScale;
906 :     ARG_DWRATE = avi_info.dwRate;
907 :     ARG_DWSCALE = avi_info.dwScale;
908 :     }
909 : Isibaar 1645
910 :     ARG_INPUTTYPE = 2;
911 :     }
912 :     else
913 :     #endif
914 :     {
915 :     in_file = fopen(ARG_INPUTFILE, "rb");
916 :     if (in_file == NULL) {
917 :     fprintf(stderr, "Error opening input file %s\n", ARG_INPUTFILE);
918 :     return (-1);
919 :     }
920 : edgomez 558 }
921 :     }
922 :    
923 : syskin 1710 // This should be after the avi input opening stuff
924 :     if (ARG_TIMECODEFILE != NULL) {
925 :     time_file = fopen(ARG_TIMECODEFILE, "r");
926 :     if (time_file==NULL) {
927 :     fprintf(stderr, "Couldn't open timecode file '%s'!\n", ARG_TIMECODEFILE);
928 :     return(-1);
929 :     }
930 :     else {
931 :     fscanf(time_file, "# timecode format v2\n");
932 :     }
933 :     }
934 :    
935 :     if (ARG_FRAMERATE <= 0) {
936 :     fprintf(stderr, "Wrong Framerate %f\n", ARG_FRAMERATE);
937 :     return (-1);
938 :     }
939 :    
940 :     if (ARG_TARGETSIZE) {
941 :     if (ARG_MAXFRAMENR <= 0) {
942 :     fprintf(stderr, "Bad target size; number of input frames unknown\n");
943 :     goto release_all;
944 :     } else if (ARG_BITRATE) {
945 :     fprintf(stderr, "Parameter conflict: Do not specify both -bitrate and -size\n");
946 :     goto release_all;
947 :     } else
948 :     ARG_BITRATE = ((ARG_TARGETSIZE * 8) / (ARG_MAXFRAMENR / ARG_FRAMERATE)) * 1024;
949 :     }
950 :    
951 :     /* Set constant quant to default if no bitrate given for single pass */
952 :     if (ARG_SINGLE && (!ARG_BITRATE) && (!ARG_CQ))
953 :     ARG_CQ = DEFAULT_QUANT;
954 :    
955 : Isibaar 1645 if (ARG_INPUTTYPE==1) {
956 : edgomez 1382 #ifndef READ_PNM
957 : edgomez 558 if (read_pgmheader(in_file)) {
958 : edgomez 1382 #else
959 :     if (read_pnmheader(in_file)) {
960 :     #endif
961 :     fprintf(stderr,
962 :     "Wrong input format, I want YUV encapsulated in PGM\n");
963 :     return (-1);
964 : edgomez 558 }
965 :     }
966 :    
967 : syskin 1710 /* Jump to the starting frame */
968 :     if (ARG_INPUTTYPE < 2)
969 :     fseek(in_file, ARG_STARTFRAMENR*IMAGE_SIZE(XDIM, YDIM), SEEK_SET);
970 :    
971 : edgomez 558 /* now we know the sizes, so allocate memory */
972 : syskin 1710 if (get_frame == NULL) {
973 :     in_buffer = (unsigned char *) malloc(4*XDIM*YDIM);
974 :     if (!in_buffer)
975 :     goto free_all_memory;
976 :     }
977 : edgomez 558
978 :     /* this should really be enough memory ! */
979 : edgomez 1382 mp4_buffer = (unsigned char *) malloc(IMAGE_SIZE(XDIM, YDIM) * 2);
980 : edgomez 558 if (!mp4_buffer)
981 : edgomez 1382 goto free_all_memory;
982 : edgomez 558
983 :     /*****************************************************************************
984 :     * XviD PART Start
985 :     ****************************************************************************/
986 :    
987 :    
988 : edgomez 1382 result = enc_init(use_assembler);
989 :     if (result) {
990 :     fprintf(stderr, "Encore INIT problem, return value %d\n", result);
991 : edgomez 558 goto release_all;
992 :     }
993 :    
994 :     /*****************************************************************************
995 :     * Main loop
996 :     ****************************************************************************/
997 :    
998 : syskin 1710 if (ARG_SAVEMPEGSTREAM) {
999 : edgomez 561
1000 : syskin 1710 if (ARG_OUTPUTFILE) {
1001 :     if ((out_file = fopen(ARG_OUTPUTFILE, "w+b")) == NULL) {
1002 :     fprintf(stderr, "Error opening output file %s\n", ARG_OUTPUTFILE);
1003 :     goto release_all;
1004 :     }
1005 : edgomez 558 }
1006 :    
1007 : syskin 1710 #ifdef XVID_AVI_OUTPUT
1008 :     if (ARG_AVIOUTPUTFILE != NULL ) {
1009 :     {
1010 :     /* Open the .avi output then close it */
1011 :     /* Resets the file size to 0, which AVIFile doesn't seem to do */
1012 :     FILE *scrub;
1013 :     if ((scrub = fopen(ARG_AVIOUTPUTFILE, "w+b")) == NULL) {
1014 :     fprintf(stderr, "Error opening output file %s\n", ARG_AVIOUTPUTFILE);
1015 :     goto release_all;
1016 :     }
1017 :     else
1018 :     fclose(scrub);
1019 :     }
1020 :     memset(&myAVIStreamInfo, 0, sizeof(AVISTREAMINFO));
1021 :     myAVIStreamInfo.fccType = streamtypeVIDEO;
1022 :     myAVIStreamInfo.fccHandler = MAKEFOURCC('x', 'v', 'i', 'd');
1023 :     myAVIStreamInfo.dwScale = ARG_DWSCALE;
1024 :     myAVIStreamInfo.dwRate = ARG_DWRATE;
1025 :     myAVIStreamInfo.dwLength = ARG_MAXFRAMENR;
1026 :     myAVIStreamInfo.dwQuality = 10000;
1027 :     SetRect(&myAVIStreamInfo.rcFrame, 0, 0, YDIM, XDIM);
1028 :    
1029 :     if (avierr=AVIFileOpen(&myAVIFile, ARG_AVIOUTPUTFILE, OF_CREATE|OF_WRITE, NULL)) {
1030 :     fprintf(stderr, "AVIFileOpen failed opening output file %s, error code %d\n", ARG_AVIOUTPUTFILE, avierr);
1031 :     goto release_all;
1032 :     }
1033 :    
1034 :     if (avierr=AVIFileCreateStream(myAVIFile, &myAVIStream, &myAVIStreamInfo)) {
1035 :     fprintf(stderr, "AVIFileCreateStream failed, error code %d\n", avierr);
1036 :     goto release_all;
1037 :     }
1038 :    
1039 :     memset(&myBitmapInfoHeader, 0, sizeof(BITMAPINFOHEADER));
1040 :     myBitmapInfoHeader.biHeight = YDIM;
1041 :     myBitmapInfoHeader.biWidth = XDIM;
1042 :     myBitmapInfoHeader.biPlanes = 1;
1043 :     myBitmapInfoHeader.biSize = sizeof(BITMAPINFOHEADER);
1044 :     myBitmapInfoHeader.biCompression = MAKEFOURCC('X', 'V', 'I', 'D');
1045 :     myBitmapInfoHeader.biBitCount = 12;
1046 :     myBitmapInfoHeader.biSizeImage = 6*XDIM*YDIM;
1047 :     if (avierr=AVIStreamSetFormat(myAVIStream, 0, &myBitmapInfoHeader, sizeof(BITMAPINFOHEADER))) {
1048 :     fprintf(stderr, "AVIStreamSetFormat failed, error code %d\n", avierr);
1049 :     goto release_all;
1050 :     }
1051 :     }
1052 :     #endif
1053 :     #ifdef XVID_MKV_OUTPUT
1054 :     if (ARG_MKVOUTPUTFILE != NULL) {
1055 :     {
1056 :     /* Open the .mkv output then close it */
1057 :     /* Just to make sure we can write to it */
1058 :     FILE *scrub;
1059 :     if ((scrub = fopen(ARG_MKVOUTPUTFILE, "w+b")) == NULL) {
1060 :     fprintf(stderr, "Error opening output file %s\n", ARG_MKVOUTPUTFILE);
1061 :     goto release_all;
1062 :     }
1063 :     else
1064 :     fclose(scrub);
1065 :     }
1066 :    
1067 :     MKVFileOpen(&myMKVFile, ARG_MKVOUTPUTFILE, OF_CREATE|OF_WRITE, NULL);
1068 :     if (ARG_PAR) {
1069 :     myMKVStreamInfo.display_height = YDIM*height_ratios[ARG_PAR];
1070 :     myMKVStreamInfo.display_width = XDIM*width_ratios[ARG_PAR];
1071 :     }
1072 :     else {
1073 :     myMKVStreamInfo.display_height = YDIM*ARG_PARHEIGHT;
1074 :     myMKVStreamInfo.display_width = XDIM*ARG_PARWIDTH;
1075 :     }
1076 :     myMKVStreamInfo.height = YDIM;
1077 :     myMKVStreamInfo.width = XDIM;
1078 :     myMKVStreamInfo.framerate = ARG_DWRATE;
1079 :     myMKVStreamInfo.framescale = ARG_DWSCALE;
1080 :     myMKVStreamInfo.length = ARG_MAXFRAMENR;
1081 :     MKVFileCreateStream(myMKVFile, &myMKVStream, &myMKVStreamInfo);
1082 :     }
1083 :     #endif
1084 : edgomez 1382 } else {
1085 : edgomez 558 out_file = NULL;
1086 :     }
1087 :    
1088 : syskin 1710
1089 : edgomez 558 /*****************************************************************************
1090 :     * Encoding loop
1091 :     ****************************************************************************/
1092 :    
1093 :     totalsize = 0;
1094 :    
1095 : edgomez 1382 result = 0;
1096 :    
1097 :     input_num = 0; /* input frame counter */
1098 :     output_num = 0; /* output frame counter */
1099 :    
1100 : syskin 1710 nvop_counter = 0;
1101 :    
1102 : edgomez 558 do {
1103 :    
1104 : edgomez 1382 char *type;
1105 :     int sse[3];
1106 :    
1107 : Isibaar 1676 if (input_num >= ARG_MAXFRAMENR && ARG_MAXFRAMENR > 0) {
1108 : edgomez 1382 result = 1;
1109 : edgomez 558 }
1110 :    
1111 : edgomez 1382 if (!result) {
1112 : Isibaar 1645 #ifdef XVID_AVI_INPUT
1113 :     if (ARG_INPUTTYPE==2) {
1114 :     /* read avs/avi data (YUV-format) */
1115 : syskin 1710 if (get_frame != NULL) {
1116 :     in_buffer = (unsigned char*)AVIStreamGetFrame(get_frame, input_num+ARG_STARTFRAMENR);
1117 :     if (in_buffer == NULL)
1118 :     result = 1;
1119 :     else
1120 :     in_buffer += ((DWORD*)in_buffer)[0];
1121 :     } else {
1122 :     if(AVIStreamRead(avi_stream, input_num+ARG_STARTFRAMENR, 1, in_buffer, 4*XDIM*YDIM, NULL, NULL ) != AVIERR_OK)
1123 :     result = 1;
1124 :     }
1125 : Isibaar 1645 } else
1126 :     #endif
1127 :     if (ARG_INPUTTYPE==1) {
1128 : edgomez 1382 /* read PGM data (YUV-format) */
1129 :     #ifndef READ_PNM
1130 :     result = read_pgmdata(in_file, in_buffer);
1131 :     #else
1132 :     result = read_pnmdata(in_file, in_buffer);
1133 :     #endif
1134 :     } else {
1135 :     /* read raw data (YUV-format) */
1136 :     result = read_yuvdata(in_file, in_buffer);
1137 :     }
1138 : edgomez 728 }
1139 :    
1140 :     /*****************************************************************************
1141 : edgomez 558 * Encode and decode this frame
1142 :     ****************************************************************************/
1143 :    
1144 : syskin 1710 if (input_num >= (unsigned int)ARG_MAXFRAMENR-1 && ARG_MAXBFRAMES) {
1145 :     stats_type = XVID_TYPE_PVOP;
1146 :     }
1147 :     else
1148 :     stats_type = XVID_TYPE_AUTO;
1149 :    
1150 : edgomez 558 enctime = msecond();
1151 : edgomez 1382 m4v_size =
1152 :     enc_main(!result ? in_buffer : 0, mp4_buffer, &key, &stats_type,
1153 : syskin 1710 &stats_quant, &stats_length, sse, input_num);
1154 : edgomez 558 enctime = msecond() - enctime;
1155 :    
1156 : edgomez 1382 /* Write the Frame statistics */
1157 : edgomez 851
1158 : edgomez 1382 if (stats_type > 0) { /* !XVID_TYPE_NOTHING */
1159 :     switch (stats_type) {
1160 : syskin 1710 case XVID_TYPE_IVOP:
1161 :     type = "I";
1162 :     break;
1163 :     case XVID_TYPE_PVOP:
1164 :     type = "P";
1165 :     break;
1166 :     case XVID_TYPE_BVOP:
1167 :     type = "B";
1168 :     if (ARG_PACKED)
1169 :     fakenvop = 1;
1170 :     break;
1171 :     case XVID_TYPE_SVOP:
1172 :     type = "S";
1173 :     break;
1174 :     default:
1175 :     type = "U";
1176 :     break;
1177 : edgomez 1382 }
1178 : edgomez 851
1179 : syskin 1710 if (stats_length > 8) {
1180 :     framestats[stats_type].count++;
1181 :     framestats[stats_type].quants[stats_quant]++;
1182 :     framestats[stats_type].size += stats_length;
1183 :     }
1184 :     else {
1185 :     framestats[5].count++;
1186 :     framestats[5].quants[stats_quant]++;
1187 :     framestats[5].size += stats_length;
1188 :     }
1189 :    
1190 :    
1191 :     if (ARG_PROGRESS == 0) {
1192 :     printf("%5d: key=%i, time= %6.0f, len= %7d", !result ? input_num : -1,
1193 :     key, (float) enctime, (int) m4v_size);
1194 :     printf(" | type=%s, quant= %2d, len= %7d", type, stats_quant,
1195 : edgomez 1382 stats_length);
1196 :    
1197 :     #define SSE2PSNR(sse, width, height) ((!(sse))?0.0f : 48.131f - 10*(float)log10((float)(sse)/((float)((width)*(height)))))
1198 :    
1199 : syskin 1710 if (ARG_STATS) {
1200 :     printf(", psnr y = %2.2f, psnr u = %2.2f, psnr v = %2.2f",
1201 :     SSE2PSNR(sse[0], XDIM, YDIM), SSE2PSNR(sse[1], XDIM / 2,
1202 :     YDIM / 2),
1203 :     SSE2PSNR(sse[2], XDIM / 2, YDIM / 2));
1204 :    
1205 :     totalPSNR[0] += SSE2PSNR(sse[0], XDIM, YDIM);
1206 :     totalPSNR[1] += SSE2PSNR(sse[1], XDIM/2, YDIM/2);
1207 :     totalPSNR[2] += SSE2PSNR(sse[2], XDIM/2, YDIM/2);
1208 :     }
1209 :     printf("\n");
1210 :     } else {
1211 :     if (input_num % ARG_PROGRESS == 1) {
1212 :     if (ARG_MAXFRAMENR > 0) {
1213 :     fprintf(stderr, "\r%7d frames(%3d%%) encoded, %6.2f fps, Average Bitrate = %5.0fkbps", \
1214 :     input_num, input_num*100/ARG_MAXFRAMENR, input_num*1000/totalenctime, \
1215 :     (((totalsize/1000)*ARG_FRAMERATE)*8)/input_num);
1216 :     } else {
1217 :     fprintf(stderr, "\r%7d frames encoded, %6.2f fps, Average Bitrate = %5.0fkbps", \
1218 :     input_num, input_num*1000/totalenctime, \
1219 :     (((totalsize/1000)*ARG_FRAMERATE)*8)/input_num);
1220 :     }
1221 :     }
1222 : edgomez 1382 }
1223 :    
1224 : edgomez 851 }
1225 : edgomez 1382 #undef SSE2PSNR
1226 : edgomez 851
1227 : syskin 1710 if (m4v_size < 0)
1228 : edgomez 1382 break;
1229 :    
1230 : edgomez 851 /* Update encoding time stats */
1231 : edgomez 558 totalenctime += enctime;
1232 :     totalsize += m4v_size;
1233 :    
1234 : edgomez 728 /*****************************************************************************
1235 : edgomez 1382 * Save stream to file
1236 : edgomez 728 ****************************************************************************/
1237 :    
1238 : edgomez 1382 if (m4v_size > 0 && ARG_SAVEMPEGSTREAM) {
1239 : syskin 1710 char timecode[50];
1240 : edgomez 728
1241 : syskin 1710 if (time_file != NULL) {
1242 :     if (fscanf(time_file, "%s\n", timecode) != 1) {
1243 :     fprintf(stderr, "Error reading timecode file, frame %d\n", output_num);
1244 :     goto release_all;
1245 :     }
1246 :     }
1247 :     else
1248 :     sprintf(timecode, "%f", ((double)ARG_DWSCALE/ARG_DWRATE)*1000*output_num);
1249 :    
1250 : edgomez 558 /* Save single files */
1251 : edgomez 1382 if (ARG_SAVEINDIVIDUAL) {
1252 :     FILE *out;
1253 :     sprintf(filename, "%sframe%05d.m4v", filepath, output_num);
1254 :     out = fopen(filename, "w+b");
1255 :     fwrite(mp4_buffer, m4v_size, 1, out);
1256 :     fclose(out);
1257 : edgomez 558 }
1258 : syskin 1710 #ifdef XVID_AVI_OUTPUT
1259 :     if (ARG_AVIOUTPUTFILE && myAVIStream) {
1260 :     int output_frame;
1261 : edgomez 558
1262 : syskin 1710 if (time_file == NULL)
1263 :     output_frame = output_num;
1264 :     else {
1265 :     output_frame = (int)(atof(timecode)/1000/((double)ARG_DWSCALE/ARG_DWRATE)+.5);
1266 :     }
1267 :     if (AVIStreamWrite(myAVIStream, output_frame, 1, mp4_buffer, m4v_size, key ? AVIIF_KEYFRAME : 0, NULL, NULL)) {
1268 :     fprintf(stderr, "AVIStreamWrite failed writing frame %d\n", output_num);
1269 :     goto release_all;
1270 :     }
1271 :     }
1272 :     #endif
1273 :    
1274 :     if (key && ARG_PACKED)
1275 :     removedivxp((char*)mp4_buffer, m4v_size);
1276 :    
1277 :     /* Save ES stream */
1278 :     if (ARG_OUTPUTFILE && out_file && !(fakenvop && m4v_size <= 8)) {
1279 :     fwrite(mp4_buffer, 1, m4v_size, out_file);
1280 :     }
1281 :     #ifdef XVID_MKV_OUTPUT
1282 :     if (ARG_MKVOUTPUTFILE && myMKVStream) {
1283 :     MKVStreamWrite(myMKVStream, atof(timecode), 1, (ARG_PACKED && fakenvop && (m4v_size <= 8)) ? NULL : mp4_buffer, m4v_size, key ? AVIIF_KEYFRAME : 0, NULL, NULL);
1284 :     }
1285 :     #endif
1286 :    
1287 :     output_num++;
1288 :     if (stats_type != XVID_TYPE_BVOP)
1289 :     fakenvop=0;
1290 : edgomez 558 }
1291 :    
1292 : edgomez 1382 input_num++;
1293 : edgomez 558
1294 : edgomez 1382 /* Read the header if it's pgm stream */
1295 : Isibaar 1645 if (!result && (ARG_INPUTTYPE==1))
1296 : edgomez 1382 #ifndef READ_PNM
1297 :     result = read_pgmheader(in_file);
1298 :     #else
1299 :     result = read_pnmheader(in_file);
1300 :     #endif
1301 :     } while (1);
1302 : edgomez 558
1303 :    
1304 : edgomez 1382
1305 : edgomez 558 /*****************************************************************************
1306 :     * Calculate totals and averages for output, print results
1307 :     ****************************************************************************/
1308 : chl 376
1309 : syskin 1710 printf("\n");
1310 : edgomez 1382 printf("Tot: enctime(ms) =%7.2f, length(bytes) = %7d\n",
1311 :     totalenctime, (int) totalsize);
1312 : chl 376
1313 : edgomez 1382 if (input_num > 0) {
1314 :     totalsize /= input_num;
1315 :     totalenctime /= input_num;
1316 :     totalPSNR[0] /= input_num;
1317 :     totalPSNR[1] /= input_num;
1318 :     totalPSNR[2] /= input_num;
1319 :     } else {
1320 :     totalsize = -1;
1321 :     totalenctime = -1;
1322 :     }
1323 : chl 376
1324 : edgomez 1382 printf("Avg: enctime(ms) =%7.2f, fps =%7.2f, length(bytes) = %7d",
1325 :     totalenctime, 1000 / totalenctime, (int) totalsize);
1326 :     if (ARG_STATS) {
1327 :     printf(", psnr y = %2.2f, psnr u = %2.2f, psnr v = %2.2f",
1328 :     totalPSNR[0],totalPSNR[1],totalPSNR[2]);
1329 :     }
1330 :     printf("\n");
1331 : syskin 1710 if (framestats[XVID_TYPE_IVOP].count) {
1332 :     printf("I frames: %6d frames, size = %7d/%7d, quants = %2d / %.2f / %2d\n", \
1333 :     framestats[XVID_TYPE_IVOP].count, framestats[XVID_TYPE_IVOP].size/framestats[XVID_TYPE_IVOP].count, \
1334 :     framestats[XVID_TYPE_IVOP].size, minquant(framestats[XVID_TYPE_IVOP].quants), \
1335 :     avgquant(framestats[XVID_TYPE_IVOP]), maxquant(framestats[XVID_TYPE_IVOP].quants));
1336 :     }
1337 :     if (framestats[XVID_TYPE_PVOP].count) {
1338 :     printf("P frames: %6d frames, size = %7d/%7d, quants = %2d / %.2f / %2d\n", \
1339 :     framestats[XVID_TYPE_PVOP].count, framestats[XVID_TYPE_PVOP].size/framestats[XVID_TYPE_PVOP].count, \
1340 :     framestats[XVID_TYPE_PVOP].size, minquant(framestats[XVID_TYPE_PVOP].quants), \
1341 :     avgquant(framestats[XVID_TYPE_PVOP]), maxquant(framestats[XVID_TYPE_PVOP].quants));
1342 :     }
1343 :     if (framestats[XVID_TYPE_BVOP].count) {
1344 :     printf("B frames: %6d frames, size = %7d/%7d, quants = %2d / %.2f / %2d\n", \
1345 :     framestats[XVID_TYPE_BVOP].count, framestats[XVID_TYPE_BVOP].size/framestats[XVID_TYPE_BVOP].count, \
1346 :     framestats[XVID_TYPE_BVOP].size, minquant(framestats[XVID_TYPE_BVOP].quants), \
1347 :     avgquant(framestats[XVID_TYPE_BVOP]), maxquant(framestats[XVID_TYPE_BVOP].quants));
1348 :     }
1349 :     if (framestats[XVID_TYPE_SVOP].count) {
1350 :     printf("S frames: %6d frames, size = %7d/%7d, quants = %2d / %.2f / %2d\n", \
1351 :     framestats[XVID_TYPE_SVOP].count, framestats[XVID_TYPE_SVOP].size/framestats[XVID_TYPE_SVOP].count, \
1352 :     framestats[XVID_TYPE_SVOP].size, minquant(framestats[XVID_TYPE_SVOP].quants), \
1353 :     avgquant(framestats[XVID_TYPE_SVOP]), maxquant(framestats[XVID_TYPE_SVOP].quants));
1354 :     }
1355 :     if (framestats[5].count) {
1356 :     printf("N frames: %6d frames, size = %7d/%7d\n", \
1357 :     framestats[5].count, framestats[5].size/framestats[5].count, \
1358 :     framestats[5].size);
1359 :     }
1360 : edgomez 1382
1361 :    
1362 : edgomez 558 /*****************************************************************************
1363 :     * XviD PART Stop
1364 :     ****************************************************************************/
1365 : chl 376
1366 : edgomez 1382 release_all:
1367 : chl 376
1368 : Isibaar 1645 #ifdef XVID_AVI_INPUT
1369 : syskin 1710 if (get_frame) AVIStreamGetFrameClose(get_frame);
1370 :     if (avi_stream) AVIStreamRelease(avi_stream);
1371 :     #ifndef XVID_AVI_OUTPUT
1372 :     AVIFileExit();
1373 : Isibaar 1645 #endif
1374 : syskin 1710 #endif
1375 : Isibaar 1645
1376 : edgomez 1382 if (enc_handle) {
1377 :     result = enc_stop();
1378 :     if (result)
1379 :     fprintf(stderr, "Encore RELEASE problem return value %d\n",
1380 :     result);
1381 : edgomez 558 }
1382 : chl 376
1383 : edgomez 1382 if (in_file)
1384 : edgomez 728 fclose(in_file);
1385 : edgomez 1382 if (out_file)
1386 : edgomez 558 fclose(out_file);
1387 :    
1388 : syskin 1710 #ifdef XVID_AVI_OUTPUT
1389 :     if (myAVIStream) AVIStreamRelease(myAVIStream);
1390 :     if (myAVIFile) AVIFileRelease(myAVIFile);
1391 :     AVIFileExit();
1392 :     #endif
1393 :     #ifdef XVID_MKV_OUTPUT
1394 :     if (myMKVStream) MKVStreamRelease(myMKVStream);
1395 :     if (myMKVFile) MKVFileRelease(myMKVFile);
1396 :     #endif
1397 :    
1398 : edgomez 1382 free_all_memory:
1399 : edgomez 558 free(out_buffer);
1400 :     free(mp4_buffer);
1401 :     free(in_buffer);
1402 :    
1403 : edgomez 1382 return (0);
1404 : edgomez 558
1405 :     }
1406 :    
1407 :    
1408 :     /*****************************************************************************
1409 :     * "statistical" functions
1410 :     *
1411 :     * these are not needed for encoding or decoding, but for measuring
1412 :     * time and quality, there in nothing specific to XviD in these
1413 :     *
1414 :     *****************************************************************************/
1415 :    
1416 :     /* Return time elapsed time in miliseconds since the program started */
1417 : edgomez 1382 static double
1418 :     msecond()
1419 :     {
1420 : edgomez 824 #ifndef WIN32
1421 : edgomez 1382 struct timeval tv;
1422 :    
1423 : chl 376 gettimeofday(&tv, 0);
1424 : edgomez 1382 return (tv.tv_sec * 1.0e3 + tv.tv_usec * 1.0e-3);
1425 : edgomez 558 #else
1426 :     clock_t clk;
1427 : edgomez 1382
1428 : edgomez 558 clk = clock();
1429 : suxen_drol 1646 return (clk * 1000.0 / CLOCKS_PER_SEC);
1430 : edgomez 558 #endif
1431 : chl 376 }
1432 :    
1433 : syskin 1710 int
1434 :     gcd(int a, int b)
1435 :     {
1436 :     int r ;
1437 :    
1438 :     if (b > a) {
1439 :     r = a;
1440 :     a = b;
1441 :     b = r;
1442 :     }
1443 :    
1444 :     while ((r = a % b)) {
1445 :     a = b;
1446 :     b = r;
1447 :     }
1448 :     return b;
1449 :     }
1450 :    
1451 :     int minquant(int quants[32])
1452 :     {
1453 :     int i = 1;
1454 :     while (quants[i] == 0) {
1455 :     i++;
1456 :     }
1457 :     return i;
1458 :     }
1459 :    
1460 :     int maxquant(int quants[32])
1461 :     {
1462 :     int i = 31;
1463 :     while (quants[i] == 0) {
1464 :     i--;
1465 :     }
1466 :     return i;
1467 :     }
1468 :    
1469 :     double avgquant(frame_stats_t frame)
1470 :     {
1471 :     double avg=0;
1472 :     int i;
1473 :     for (i=1; i < 32; i++) {
1474 :     avg += frame.quants[i]*i;
1475 :     }
1476 :     avg /= frame.count;
1477 :     return avg;
1478 :     }
1479 :    
1480 : edgomez 558 /*****************************************************************************
1481 :     * Usage message
1482 :     *****************************************************************************/
1483 : chl 376
1484 : edgomez 1382 static void
1485 :     usage()
1486 : edgomez 558 {
1487 : syskin 1710 fprintf(stderr, "xvid_encraw built at %s on %s\n", __TIME__, __DATE__);
1488 :     fprintf(stderr, "Usage : xvid_encraw [OPTIONS]\n\n");
1489 : edgomez 1382 fprintf(stderr, "Input options:\n");
1490 : syskin 1710 fprintf(stderr, " -i string : input filename (stdin)\n");
1491 : Isibaar 1645 #ifdef XVID_AVI_INPUT
1492 :     fprintf(stderr, " -type integer: input data type (yuv=0, pgm=1, avi/avs=2)\n");
1493 :     #else
1494 : edgomez 1382 fprintf(stderr, " -type integer: input data type (yuv=0, pgm=1)\n");
1495 : Isibaar 1645 #endif
1496 : edgomez 1382 fprintf(stderr, " -w integer: frame width ([1.2048])\n");
1497 :     fprintf(stderr, " -h integer: frame height ([1.2048])\n");
1498 :     fprintf(stderr, " -frames integer: number of frames to encode\n");
1499 :     fprintf(stderr, "\n");
1500 :     fprintf(stderr, "Output options:\n");
1501 : syskin 1710 fprintf(stderr, " -dump : save decoder output\n");
1502 :     fprintf(stderr, " -save : save an Elementary Stream file per frame\n");
1503 :     fprintf(stderr, " -o string : save an Elementary Stream for the complete sequence\n");
1504 :     #ifdef XVID_AVI_OUTPUT
1505 :     fprintf(stderr, " -avi string: save an AVI file for the complete sequence\n");
1506 :     #endif
1507 :     fprintf(stderr, " -mkv string: save a MKV file for the complete sequence\n");
1508 : edgomez 1382 fprintf(stderr, "\n");
1509 :     fprintf(stderr, "BFrames options:\n");
1510 : syskin 1710 fprintf(stderr, " -max_bframes integer: max bframes (2)\n");
1511 :     fprintf(stderr, " -bquant_ratio integer: bframe quantizer ratio (150)\n");
1512 :     fprintf(stderr, " -bquant_offset integer: bframe quantizer offset (100)\n");
1513 : edgomez 1382 fprintf(stderr, "\n");
1514 :     fprintf(stderr, "Rate control options:\n");
1515 : syskin 1710 fprintf(stderr, " -framerate float : target framerate (25.0)\n");
1516 :     fprintf(stderr, " -bitrate [integer] : target bitrate in kbps (700)\n");
1517 :     fprintf(stderr, " -size integer : target size in kilobytes\n");
1518 :     fprintf(stderr, " -single : single pass mode (default)\n");
1519 :     fprintf(stderr, " -cq float : single pass constant quantizer\n");
1520 :     fprintf(stderr, " -pass1 [filename] : twopass mode (first pass)\n");
1521 :     fprintf(stderr, " -full1pass : perform full first pass\n");
1522 :     fprintf(stderr, " -pass2 [filename] : twopass mode (2nd pass)\n");
1523 : edgomez 1382 fprintf(stderr, " -zq starting_frame float : bitrate zone; quant\n");
1524 :     fprintf(stderr, " -zw starting_frame float : bitrate zone; weight\n");
1525 : syskin 1710 fprintf(stderr, " -max_key_interval integer : maximum keyframe interval (300)\n");
1526 : edgomez 1382 fprintf(stderr, "\n");
1527 : syskin 1710 fprintf(stderr, "Single Pass options:\n");
1528 :     fprintf(stderr, "-reaction integer : reaction delay factor (16)\n");
1529 :     fprintf(stderr, "-averaging integer : averaging period (100)\n");
1530 :     fprintf(stderr, "-smoother integer : smoothing buffer (100)\n");
1531 :     fprintf(stderr, "\n");
1532 :     fprintf(stderr, "Second Pass options:\n");
1533 :     fprintf(stderr, "-kboost integer : I frame boost (10)\n");
1534 :     fprintf(stderr, "-kthresh integer : I frame reduction threshold (1)\n");
1535 :     fprintf(stderr, "-kreduction integer : I frame reduction amount (20)\n");
1536 :     fprintf(stderr, "-ostrength integer : overflow control strength (5)\n");
1537 :     fprintf(stderr, "-oimprove integer : max overflow improvement (5)\n");
1538 :     fprintf(stderr, "-odegrade integer : max overflow degradation (5)\n");
1539 :     fprintf(stderr, "-chigh integer : high bitrate scenes degradation (0)\n");
1540 :     fprintf(stderr, "-clow integer : low bitrate scenes improvement (0)\n");
1541 :     fprintf(stderr, "-overhead integer : container frame overhead (24)\n");
1542 :     fprintf(stderr, "-vbvsize integer : use vbv buffer size\n");
1543 :     fprintf(stderr, "-vbvmax integer : vbv max bitrate\n");
1544 :     fprintf(stderr, "-vbvpeak integer : vbv peak bitrate over 1 second\n");
1545 :     fprintf(stderr, "\n");
1546 : edgomez 1382 fprintf(stderr, "Other options\n");
1547 : syskin 1710 fprintf(stderr, " -noasm : do not use assembly optmized code\n");
1548 :     fprintf(stderr, " -turbo : use turbo presets for higher encoding speed\n");
1549 :     fprintf(stderr, " -quality integer : quality ([0..%d]) (6)\n", ME_ELEMENTS - 1);
1550 :     fprintf(stderr, " -vhqmode integer : level of R-D optimizations ([0..4]) (1)\n");
1551 :     fprintf(stderr, " -bvhq : use R-D optimizations for B-frames\n");
1552 :     fprintf(stderr, " -qpel : use quarter pixel ME\n");
1553 :     fprintf(stderr, " -gmc : use global motion compensation\n");
1554 :     fprintf(stderr, " -qtype integer : quantization type (H263:0, MPEG4:1) (0)\n");
1555 :     fprintf(stderr, " -qmatrix filename : use custom MPEG4 quantization matrix\n");
1556 :     fprintf(stderr, " -interlaced [integer] : interlaced encoding (BFF:1, TFF:2) (1)\n");
1557 :     fprintf(stderr, " -nopacked : Disable packed mode\n");
1558 :     fprintf(stderr, " -noclosed_gop : Disable closed GOP mode\n");
1559 :     fprintf(stderr, " -lumimasking : use lumimasking algorithm\n");
1560 :     fprintf(stderr, " -stats : print stats about encoded frames\n");
1561 :     fprintf(stderr, " -debug : activates xvidcore internal debugging output\n");
1562 :     fprintf(stderr, " -vop_debug : print some info directly into encoded frames\n");
1563 :     fprintf(stderr, " -nochromame : Disable chroma motion estimation\n");
1564 :     fprintf(stderr, " -notrellis : Disable trellis quantization\n");
1565 :     fprintf(stderr, " -imin integer : Minimum I Quantizer (1..31) (2)\n");
1566 :     fprintf(stderr, " -imax integer : Maximum I quantizer (1..31) (31)\n");
1567 :     fprintf(stderr, " -bmin integer : Minimum B Quantizer (1..31) (2)\n");
1568 :     fprintf(stderr, " -bmax integer : Maximum B quantizer (1..31) (31)\n");
1569 :     fprintf(stderr, " -pmin integer : Minimum P Quantizer (1..31) (2)\n");
1570 :     fprintf(stderr, " -pmax integer : Maximum P quantizer (1..31) (31)\n");
1571 :     fprintf(stderr, " -drop integer : Frame Drop Ratio (0..100) (0)\n");
1572 :     fprintf(stderr, " -start integer : Starting frame number\n");
1573 :     fprintf(stderr, " -threads integer : Number of threads\n");
1574 :     fprintf(stderr, " -progress [integer] : Show progress updates every n frames (10)\n");
1575 :     fprintf(stderr, " -par integer[:integer] : Set Pixel Aspect Ratio.\n");
1576 :     fprintf(stderr, " 1 = 1:1\n");
1577 :     fprintf(stderr, " 2 = 12:11 (4:3 PAL)\n");
1578 :     fprintf(stderr, " 3 = 10:11 (4:3 NTSC)\n");
1579 :     fprintf(stderr, " 4 = 16:11 (16:9 PAL)\n");
1580 :     fprintf(stderr, " 5 = 40:33 (16:9 NTSC)\n");
1581 :     fprintf(stderr, " other = custom (width:height)\n");
1582 :     fprintf(stderr, " -help : prints this help message\n");
1583 : edgomez 1382 fprintf(stderr, "\n");
1584 : syskin 1710 fprintf(stderr, "NB: You can define %d zones repeating the -z[qw] option as needed.\n", MAX_ZONES);
1585 : edgomez 558 }
1586 :    
1587 :     /*****************************************************************************
1588 :     * Input and output functions
1589 :     *
1590 :     * the are small and simple routines to read and write PGM and YUV
1591 :     * image. It's just for convenience, again nothing specific to XviD
1592 :     *
1593 :     *****************************************************************************/
1594 :    
1595 : edgomez 1382 #ifndef READ_PNM
1596 :     static int
1597 :     read_pgmheader(FILE * handle)
1598 :     {
1599 :     int bytes, xsize, ysize, depth;
1600 : chl 376 char dummy[2];
1601 :    
1602 : edgomez 1382 bytes = fread(dummy, 1, 2, handle);
1603 : edgomez 558
1604 : edgomez 1382 if ((bytes < 2) || (dummy[0] != 'P') || (dummy[1] != '5'))
1605 :     return (1);
1606 :    
1607 :     fscanf(handle, "%d %d %d", &xsize, &ysize, &depth);
1608 : edgomez 1398 if ((xsize > 4096) || (ysize > 4096*3/2) || (depth != 255)) {
1609 : edgomez 1382 fprintf(stderr, "%d %d %d\n", xsize, ysize, depth);
1610 :     return (2);
1611 : chl 376 }
1612 : edgomez 1382 if ((XDIM == 0) || (YDIM == 0)) {
1613 :     XDIM = xsize;
1614 :     YDIM = ysize * 2 / 3;
1615 : chl 376 }
1616 :    
1617 : edgomez 1382 return (0);
1618 : chl 376 }
1619 :    
1620 : edgomez 1382 static int
1621 :     read_pgmdata(FILE * handle,
1622 :     unsigned char *image)
1623 :     {
1624 : edgomez 558 int i;
1625 : chl 376 char dummy;
1626 : edgomez 1382
1627 : edgomez 558 unsigned char *y = image;
1628 : edgomez 1382 unsigned char *u = image + XDIM * YDIM;
1629 :     unsigned char *v = image + XDIM * YDIM + XDIM / 2 * YDIM / 2;
1630 : chl 376
1631 : edgomez 558 /* read Y component of picture */
1632 : edgomez 1382 fread(y, 1, XDIM * YDIM, handle);
1633 :    
1634 :     for (i = 0; i < YDIM / 2; i++) {
1635 : edgomez 558 /* read U */
1636 : edgomez 1382 fread(u, 1, XDIM / 2, handle);
1637 : edgomez 558
1638 :     /* read V */
1639 : edgomez 1382 fread(v, 1, XDIM / 2, handle);
1640 : edgomez 558
1641 :     /* Update pointers */
1642 : edgomez 1382 u += XDIM / 2;
1643 :     v += XDIM / 2;
1644 : chl 376 }
1645 : edgomez 558
1646 : edgomez 1382 /* I don't know why, but this seems needed */
1647 : edgomez 558 fread(&dummy, 1, 1, handle);
1648 :    
1649 : edgomez 1382 return (0);
1650 : chl 376 }
1651 : edgomez 1382 #else
1652 :     static int
1653 :     read_pnmheader(FILE * handle)
1654 :     {
1655 :     int bytes, xsize, ysize, depth;
1656 :     char dummy[2];
1657 : chl 376
1658 : edgomez 1382 bytes = fread(dummy, 1, 2, handle);
1659 :    
1660 :     if ((bytes < 2) || (dummy[0] != 'P') || (dummy[1] != '6'))
1661 :     return (1);
1662 :    
1663 :     fscanf(handle, "%d %d %d", &xsize, &ysize, &depth);
1664 :     if ((xsize > 1440) || (ysize > 2880) || (depth != 255)) {
1665 :     fprintf(stderr, "%d %d %d\n", xsize, ysize, depth);
1666 :     return (2);
1667 :     }
1668 :    
1669 :     XDIM = xsize;
1670 :     YDIM = ysize;
1671 :    
1672 :     return (0);
1673 :     }
1674 :    
1675 :     static int
1676 :     read_pnmdata(FILE * handle,
1677 :     unsigned char *image)
1678 : edgomez 558 {
1679 : edgomez 1382 int i;
1680 :     char dummy;
1681 :    
1682 :     /* read Y component of picture */
1683 :     fread(image, 1, XDIM * YDIM * 3, handle);
1684 :    
1685 :     /* I don't know why, but this seems needed */
1686 :     fread(&dummy, 1, 1, handle);
1687 :    
1688 :     return (0);
1689 : chl 376 }
1690 : edgomez 1382 #endif
1691 : chl 376
1692 : edgomez 1382 static int
1693 :     read_yuvdata(FILE * handle,
1694 :     unsigned char *image)
1695 :     {
1696 :    
1697 :     if (fread(image, 1, IMAGE_SIZE(XDIM, YDIM), handle) !=
1698 :     (unsigned int) IMAGE_SIZE(XDIM, YDIM))
1699 :     return (1);
1700 :     else
1701 :     return (0);
1702 :     }
1703 :    
1704 : edgomez 558 /*****************************************************************************
1705 :     * Routines for encoding: init encoder, frame step, release encoder
1706 :     ****************************************************************************/
1707 : chl 376
1708 : edgomez 1382 /* sample plugin */
1709 :    
1710 :     int
1711 :     rawenc_debug(void *handle,
1712 :     int opt,
1713 :     void *param1,
1714 :     void *param2)
1715 :     {
1716 :     switch (opt) {
1717 :     case XVID_PLG_INFO:
1718 :     {
1719 :     xvid_plg_info_t *info = (xvid_plg_info_t *) param1;
1720 :    
1721 :     info->flags = XVID_REQDQUANTS;
1722 :     return 0;
1723 :     }
1724 :    
1725 :     case XVID_PLG_CREATE:
1726 :     case XVID_PLG_DESTROY:
1727 :     case XVID_PLG_BEFORE:
1728 :     return 0;
1729 :    
1730 :     case XVID_PLG_AFTER:
1731 :     {
1732 :     xvid_plg_data_t *data = (xvid_plg_data_t *) param1;
1733 :     int i, j;
1734 :    
1735 :     printf("---[ frame: %5i quant: %2i length: %6i ]---\n",
1736 :     data->frame_num, data->quant, data->length);
1737 :     for (j = 0; j < data->mb_height; j++) {
1738 :     for (i = 0; i < data->mb_width; i++)
1739 :     printf("%2i ", data->dquant[j * data->dquant_stride + i]);
1740 :     printf("\n");
1741 :     }
1742 :    
1743 :     return 0;
1744 :     }
1745 :     }
1746 :    
1747 :     return XVID_ERR_FAIL;
1748 :     }
1749 :    
1750 :    
1751 : chl 376 #define FRAMERATE_INCR 1001
1752 :    
1753 : edgomez 1382
1754 : edgomez 558 /* Initialize encoder for first use, pass all needed parameters to the codec */
1755 : edgomez 1382 static int
1756 :     enc_init(int use_assembler)
1757 : edgomez 558 {
1758 : chl 376 int xerr;
1759 : edgomez 1382 //xvid_plugin_cbr_t cbr;
1760 :     xvid_plugin_single_t single;
1761 :     xvid_plugin_2pass1_t rc2pass1;
1762 :     xvid_plugin_2pass2_t rc2pass2;
1763 :     //xvid_plugin_fixed_t rcfixed;
1764 :     xvid_enc_plugin_t plugins[7];
1765 :     xvid_gbl_init_t xvid_gbl_init;
1766 :     xvid_enc_create_t xvid_enc_create;
1767 : syskin 1710 int i;
1768 : chl 376
1769 : edgomez 1382 /*------------------------------------------------------------------------
1770 :     * XviD core initialization
1771 :     *----------------------------------------------------------------------*/
1772 : chl 376
1773 : edgomez 1382 /* Set version -- version checking will done by xvidcore */
1774 :     memset(&xvid_gbl_init, 0, sizeof(xvid_gbl_init));
1775 :     xvid_gbl_init.version = XVID_VERSION;
1776 :     xvid_gbl_init.debug = ARG_DEBUG;
1777 :    
1778 :    
1779 :     /* Do we have to enable ASM optimizations ? */
1780 :     if (use_assembler) {
1781 :    
1782 : suxen_drol 860 #ifdef ARCH_IS_IA64
1783 : edgomez 1382 xvid_gbl_init.cpu_flags = XVID_CPU_FORCE | XVID_CPU_ASM;
1784 : chl 376 #else
1785 : edgomez 1382 xvid_gbl_init.cpu_flags = 0;
1786 : chl 376 #endif
1787 : edgomez 1382 } else {
1788 :     xvid_gbl_init.cpu_flags = XVID_CPU_FORCE;
1789 : edgomez 558 }
1790 : edgomez 1382
1791 :     /* Initialize XviD core -- Should be done once per __process__ */
1792 :     xvid_global(NULL, XVID_GBL_INIT, &xvid_gbl_init, NULL);
1793 : syskin 1710 enc_info();
1794 : edgomez 1382
1795 :     /*------------------------------------------------------------------------
1796 :     * XviD encoder initialization
1797 :     *----------------------------------------------------------------------*/
1798 :    
1799 :     /* Version again */
1800 :     memset(&xvid_enc_create, 0, sizeof(xvid_enc_create));
1801 :     xvid_enc_create.version = XVID_VERSION;
1802 :    
1803 :     /* Width and Height of input frames */
1804 :     xvid_enc_create.width = XDIM;
1805 :     xvid_enc_create.height = YDIM;
1806 : syskin 1710 xvid_enc_create.profile = 0xf5; /* Unrestricted */
1807 : edgomez 1382
1808 :     /* init plugins */
1809 : syskin 1710 // xvid_enc_create.zones = ZONES;
1810 :     // xvid_enc_create.num_zones = NUM_ZONES;
1811 : edgomez 1382
1812 :     xvid_enc_create.plugins = plugins;
1813 :     xvid_enc_create.num_plugins = 0;
1814 :    
1815 :     if (ARG_SINGLE) {
1816 :     memset(&single, 0, sizeof(xvid_plugin_single_t));
1817 :     single.version = XVID_VERSION;
1818 :     single.bitrate = ARG_BITRATE;
1819 : syskin 1710 single.reaction_delay_factor = ARG_REACTION;
1820 :     single.averaging_period = ARG_AVERAGING;
1821 :     single.buffer = ARG_SMOOTHER;
1822 :    
1823 : edgomez 1382
1824 :     plugins[xvid_enc_create.num_plugins].func = xvid_plugin_single;
1825 :     plugins[xvid_enc_create.num_plugins].param = &single;
1826 :     xvid_enc_create.num_plugins++;
1827 : syskin 1710 if (!ARG_BITRATE)
1828 :     prepare_cquant_zones();
1829 : edgomez 558 }
1830 : chl 376
1831 : edgomez 1382 if (ARG_PASS2) {
1832 :     memset(&rc2pass2, 0, sizeof(xvid_plugin_2pass2_t));
1833 :     rc2pass2.version = XVID_VERSION;
1834 :     rc2pass2.filename = ARG_PASS2;
1835 :     rc2pass2.bitrate = ARG_BITRATE;
1836 : chl 376
1837 : syskin 1710 rc2pass2.keyframe_boost = ARG_KBOOST;
1838 :     rc2pass2.curve_compression_high = ARG_CHIGH;
1839 :     rc2pass2.curve_compression_low = ARG_CLOW;
1840 :     rc2pass2.overflow_control_strength = ARG_OVERSTRENGTH;
1841 :     rc2pass2.max_overflow_improvement = ARG_OVERIMPROVE;
1842 :     rc2pass2.max_overflow_degradation = ARG_OVERDEGRADE;
1843 :     rc2pass2.kfreduction = ARG_KREDUCTION;
1844 :     rc2pass2.kfthreshold = ARG_KTHRESH;
1845 :     rc2pass2.container_frame_overhead = ARG_OVERHEAD;
1846 : chl 1470
1847 : syskin 1710 // An example of activating VBV could look like this
1848 :     rc2pass2.vbv_size = ARG_VBVSIZE;
1849 :     rc2pass2.vbv_initial = (ARG_VBVSIZE*3)/4;
1850 :     rc2pass2.vbv_maxrate = ARG_VBVMAXRATE;
1851 :     rc2pass2.vbv_peakrate = ARG_VBVPEAKRATE*3;
1852 :    
1853 :    
1854 : edgomez 1382 plugins[xvid_enc_create.num_plugins].func = xvid_plugin_2pass2;
1855 :     plugins[xvid_enc_create.num_plugins].param = &rc2pass2;
1856 :     xvid_enc_create.num_plugins++;
1857 : chl 376 }
1858 : edgomez 1382
1859 :     if (ARG_PASS1) {
1860 :     memset(&rc2pass1, 0, sizeof(xvid_plugin_2pass1_t));
1861 :     rc2pass1.version = XVID_VERSION;
1862 :     rc2pass1.filename = ARG_PASS1;
1863 : syskin 1710 if (ARG_FULL1PASS)
1864 :     prepare_full1pass_zones();
1865 : edgomez 1382 plugins[xvid_enc_create.num_plugins].func = xvid_plugin_2pass1;
1866 :     plugins[xvid_enc_create.num_plugins].param = &rc2pass1;
1867 :     xvid_enc_create.num_plugins++;
1868 : chl 376 }
1869 :    
1870 : syskin 1710 /* Zones stuff */
1871 :     xvid_enc_create.zones = (xvid_enc_zone_t*)malloc(sizeof(xvid_enc_zone_t) * NUM_ZONES);
1872 :     xvid_enc_create.num_zones = NUM_ZONES;
1873 :     for (i=0; i < xvid_enc_create.num_zones; i++) {
1874 :     xvid_enc_create.zones[i].frame = ZONES[i].frame;
1875 :     xvid_enc_create.zones[i].base = 100;
1876 :     xvid_enc_create.zones[i].mode = ZONES[i].mode;
1877 :     xvid_enc_create.zones[i].increment = ZONES[i].modifier;
1878 :     }
1879 :    
1880 :    
1881 : edgomez 1382 if (ARG_LUMIMASKING) {
1882 :     plugins[xvid_enc_create.num_plugins].func = xvid_plugin_lumimasking;
1883 :     plugins[xvid_enc_create.num_plugins].param = NULL;
1884 :     xvid_enc_create.num_plugins++;
1885 :     }
1886 :    
1887 :     if (ARG_DUMP) {
1888 :     plugins[xvid_enc_create.num_plugins].func = xvid_plugin_dump;
1889 :     plugins[xvid_enc_create.num_plugins].param = NULL;
1890 :     xvid_enc_create.num_plugins++;
1891 :     }
1892 :    
1893 :     #if 0
1894 :     if (ARG_DEBUG) {
1895 :     plugins[xvid_enc_create.num_plugins].func = rawenc_debug;
1896 :     plugins[xvid_enc_create.num_plugins].param = NULL;
1897 :     xvid_enc_create.num_plugins++;
1898 :     }
1899 :     #endif
1900 :    
1901 : syskin 1683 xvid_enc_create.num_threads = ARG_THREADS;
1902 : edgomez 1382
1903 : syskin 1710 /* Frame rate */
1904 :     xvid_enc_create.fincr = ARG_DWSCALE;
1905 :     xvid_enc_create.fbase = ARG_DWRATE;
1906 : edgomez 1382
1907 :     /* Maximum key frame interval */
1908 :     if (ARG_MAXKEYINTERVAL > 0) {
1909 :     xvid_enc_create.max_key_interval = ARG_MAXKEYINTERVAL;
1910 :     }else {
1911 :     xvid_enc_create.max_key_interval = (int) ARG_FRAMERATE *10;
1912 :     }
1913 :    
1914 : syskin 1710 xvid_enc_create.min_quant[0]=ARG_QUANTS[0];
1915 :     xvid_enc_create.min_quant[1]=ARG_QUANTS[2];
1916 :     xvid_enc_create.min_quant[2]=ARG_QUANTS[4];
1917 :     xvid_enc_create.max_quant[0]=ARG_QUANTS[1];
1918 :     xvid_enc_create.max_quant[1]=ARG_QUANTS[3];
1919 :     xvid_enc_create.max_quant[2]=ARG_QUANTS[5];
1920 :    
1921 : edgomez 1382 /* Bframes settings */
1922 :     xvid_enc_create.max_bframes = ARG_MAXBFRAMES;
1923 :     xvid_enc_create.bquant_ratio = ARG_BQRATIO;
1924 :     xvid_enc_create.bquant_offset = ARG_BQOFFSET;
1925 :    
1926 : syskin 1710 /* Frame drop ratio */
1927 :     xvid_enc_create.frame_drop_ratio = ARG_FRAMEDROP;
1928 : edgomez 1382
1929 :     /* Global encoder options */
1930 :     xvid_enc_create.global = 0;
1931 :    
1932 :     if (ARG_PACKED)
1933 :     xvid_enc_create.global |= XVID_GLOBAL_PACKED;
1934 :    
1935 :     if (ARG_CLOSED_GOP)
1936 :     xvid_enc_create.global |= XVID_GLOBAL_CLOSED_GOP;
1937 :    
1938 :     if (ARG_STATS)
1939 :     xvid_enc_create.global |= XVID_GLOBAL_EXTRASTATS_ENABLE;
1940 :    
1941 : edgomez 558 /* I use a small value here, since will not encode whole movies, but short clips */
1942 : edgomez 1382 xerr = xvid_encore(NULL, XVID_ENC_CREATE, &xvid_enc_create, NULL);
1943 : chl 376
1944 : edgomez 1382 /* Retrieve the encoder instance from the structure */
1945 :     enc_handle = xvid_enc_create.handle;
1946 : chl 376
1947 : syskin 1710 free(xvid_enc_create.zones);
1948 :    
1949 : edgomez 1382 return (xerr);
1950 : chl 376 }
1951 :    
1952 : edgomez 1382 static int
1953 : syskin 1710 enc_info()
1954 :     {
1955 :     xvid_gbl_info_t xvid_gbl_info;
1956 :     int ret;
1957 :    
1958 :     memset(&xvid_gbl_info, 0, sizeof(xvid_gbl_info));
1959 :     xvid_gbl_info.version = XVID_VERSION;
1960 :     ret = xvid_global(NULL, XVID_GBL_INFO, &xvid_gbl_info, NULL);
1961 :     if (xvid_gbl_info.build != NULL) {
1962 :     fprintf(stderr, "xvidcore build version: %s\n", xvid_gbl_info.build);
1963 :     }
1964 :     fprintf(stderr, "Bitstream version: %d.%d.%d\n", XVID_VERSION_MAJOR(xvid_gbl_info.actual_version), XVID_VERSION_MINOR(xvid_gbl_info.actual_version), XVID_VERSION_PATCH(xvid_gbl_info.actual_version));
1965 :     fprintf(stderr, "Detected CPU flags: ");
1966 :     if (xvid_gbl_info.cpu_flags & XVID_CPU_ASM)
1967 :     fprintf(stderr, "ASM ");
1968 :     if (xvid_gbl_info.cpu_flags & XVID_CPU_MMX)
1969 :     fprintf(stderr, "MMX ");
1970 :     if (xvid_gbl_info.cpu_flags & XVID_CPU_MMXEXT)
1971 :     fprintf(stderr, "MMXEXT ");
1972 :     if (xvid_gbl_info.cpu_flags & XVID_CPU_SSE)
1973 :     fprintf(stderr, "SSE ");
1974 :     if (xvid_gbl_info.cpu_flags & XVID_CPU_SSE2)
1975 :     fprintf(stderr, "SSE2 ");
1976 :     if (xvid_gbl_info.cpu_flags & XVID_CPU_3DNOW)
1977 :     fprintf(stderr, "3DNOW ");
1978 :     if (xvid_gbl_info.cpu_flags & XVID_CPU_3DNOWEXT)
1979 :     fprintf(stderr, "3DNOWEXT ");
1980 :     if (xvid_gbl_info.cpu_flags & XVID_CPU_TSC)
1981 :     fprintf(stderr, "TSC ");
1982 :     fprintf(stderr, "\n");
1983 :     fprintf(stderr, "Detected %d cpus,", xvid_gbl_info.num_threads);
1984 :     if (!ARG_THREADS)
1985 :     ARG_THREADS = xvid_gbl_info.num_threads;
1986 :     fprintf(stderr, " using %d threads.\n", ARG_THREADS);
1987 :     return ret;
1988 :     }
1989 :    
1990 :     static int
1991 : edgomez 1382 enc_stop()
1992 : edgomez 558 {
1993 :     int xerr;
1994 : chl 376
1995 : edgomez 1382 /* Destroy the encoder instance */
1996 : chl 376 xerr = xvid_encore(enc_handle, XVID_ENC_DESTROY, NULL, NULL);
1997 : edgomez 558
1998 : edgomez 1382 return (xerr);
1999 : chl 376 }
2000 :    
2001 : edgomez 1382 static int
2002 :     enc_main(unsigned char *image,
2003 :     unsigned char *bitstream,
2004 :     int *key,
2005 :     int *stats_type,
2006 :     int *stats_quant,
2007 :     int *stats_length,
2008 : syskin 1710 int sse[3],
2009 :     int framenum)
2010 : edgomez 558 {
2011 : edgomez 1382 int ret;
2012 : chl 376
2013 : edgomez 1382 xvid_enc_frame_t xvid_enc_frame;
2014 :     xvid_enc_stats_t xvid_enc_stats;
2015 : chl 376
2016 : edgomez 1382 /* Version for the frame and the stats */
2017 :     memset(&xvid_enc_frame, 0, sizeof(xvid_enc_frame));
2018 :     xvid_enc_frame.version = XVID_VERSION;
2019 : chl 376
2020 : edgomez 1382 memset(&xvid_enc_stats, 0, sizeof(xvid_enc_stats));
2021 :     xvid_enc_stats.version = XVID_VERSION;
2022 : chl 376
2023 : edgomez 1382 /* Bind output buffer */
2024 :     xvid_enc_frame.bitstream = bitstream;
2025 :     xvid_enc_frame.length = -1;
2026 : chl 376
2027 : edgomez 1382 /* Initialize input image fields */
2028 :     if (image) {
2029 :     xvid_enc_frame.input.plane[0] = image;
2030 :     #ifndef READ_PNM
2031 : Isibaar 1645 if (ARG_INPUTTYPE==2)
2032 : syskin 1710 xvid_enc_frame.input.csp = ARG_COLORSPACE;
2033 : Isibaar 1645 else
2034 :     xvid_enc_frame.input.csp = XVID_CSP_I420;
2035 : edgomez 1382 xvid_enc_frame.input.stride[0] = XDIM;
2036 :     #else
2037 :     xvid_enc_frame.input.csp = XVID_CSP_BGR;
2038 :     xvid_enc_frame.input.stride[0] = XDIM*3;
2039 :     #endif
2040 :     } else {
2041 :     xvid_enc_frame.input.csp = XVID_CSP_NULL;
2042 :     }
2043 : chl 376
2044 : edgomez 1382 /* Set up core's general features */
2045 :     xvid_enc_frame.vol_flags = 0;
2046 :     if (ARG_STATS)
2047 :     xvid_enc_frame.vol_flags |= XVID_VOL_EXTRASTATS;
2048 : syskin 1710 if (ARG_QTYPE) {
2049 : Isibaar 1645 xvid_enc_frame.vol_flags |= XVID_VOL_MPEGQUANT;
2050 : syskin 1710 if (ARG_QMATRIX) {
2051 :     xvid_enc_frame.quant_intra_matrix = qmatrix_intra;
2052 :     xvid_enc_frame.quant_inter_matrix = qmatrix_inter;
2053 :     }
2054 :     else {
2055 :     /* We don't use special matrices */
2056 :     xvid_enc_frame.quant_intra_matrix = NULL;
2057 :     xvid_enc_frame.quant_inter_matrix = NULL;
2058 :     }
2059 :     }
2060 :    
2061 :     if (ARG_PAR)
2062 :     xvid_enc_frame.par = ARG_PAR;
2063 :     else {
2064 :     xvid_enc_frame.par = XVID_PAR_EXT;
2065 :     xvid_enc_frame.par_width = ARG_PARWIDTH;
2066 :     xvid_enc_frame.par_height = ARG_PARHEIGHT;
2067 :     }
2068 :    
2069 :    
2070 :     if (ARG_QPEL) {
2071 : edgomez 1382 xvid_enc_frame.vol_flags |= XVID_VOL_QUARTERPEL;
2072 : syskin 1710 xvid_enc_frame.motion |= XVID_ME_QUARTERPELREFINE16 | XVID_ME_QUARTERPELREFINE8;
2073 :     }
2074 :     if (ARG_GMC) {
2075 : edgomez 1382 xvid_enc_frame.vol_flags |= XVID_VOL_GMC;
2076 : syskin 1710 xvid_enc_frame.motion |= XVID_ME_GME_REFINE;
2077 :     }
2078 : edgomez 728
2079 : edgomez 1382 /* Set up core's general features */
2080 :     xvid_enc_frame.vop_flags = vop_presets[ARG_QUALITY];
2081 : edgomez 728
2082 : syskin 1710 if (ARG_INTERLACING) {
2083 :     xvid_enc_frame.vol_flags |= XVID_VOL_INTERLACING;
2084 :     if (ARG_INTERLACING == 2)
2085 :     xvid_enc_frame.vop_flags |= XVID_VOP_TOPFIELDFIRST;
2086 :     }
2087 :    
2088 :     xvid_enc_frame.vop_flags |= XVID_VOP_HALFPEL;
2089 :     xvid_enc_frame.vop_flags |= XVID_VOP_HQACPRED;
2090 :    
2091 : edgomez 1382 if (ARG_VOPDEBUG) {
2092 :     xvid_enc_frame.vop_flags |= XVID_VOP_DEBUG;
2093 :     }
2094 : edgomez 728
2095 : syskin 1710 if (ARG_TRELLIS) {
2096 :     xvid_enc_frame.vop_flags |= XVID_VOP_TRELLISQUANT;
2097 : Skal 1622 }
2098 :    
2099 : syskin 1710 /* Frame type -- taken from function call parameter */
2100 :     /* Sometimes we might want to force the last frame to be a P Frame */
2101 :     xvid_enc_frame.type = *stats_type;
2102 : chl 376
2103 : edgomez 1382 /* Force the right quantizer -- It is internally managed by RC plugins */
2104 :     xvid_enc_frame.quant = 0;
2105 : edgomez 728
2106 : syskin 1710 if (ARG_CHROMAME)
2107 :     xvid_enc_frame.motion |= XVID_ME_CHROMA_PVOP + XVID_ME_CHROMA_BVOP;
2108 :    
2109 : edgomez 1382 /* Set up motion estimation flags */
2110 : syskin 1710 xvid_enc_frame.motion |= motion_presets[ARG_QUALITY];
2111 : chl 376
2112 : Isibaar 1645 if (ARG_TURBO)
2113 :     xvid_enc_frame.motion |= XVID_ME_FASTREFINE16 | XVID_ME_FASTREFINE8 |
2114 :     XVID_ME_SKIP_DELTASEARCH | XVID_ME_FAST_MODEINTERPOLATE |
2115 :     XVID_ME_BFRAME_EARLYSTOP;
2116 :    
2117 :     if (ARG_BVHQ)
2118 :     xvid_enc_frame.vop_flags |= XVID_VOP_RD_BVOP;
2119 :    
2120 : chl 1624 switch (ARG_VHQMODE) /* this is the same code as for vfw */
2121 :     {
2122 :     case 1: /* VHQ_MODE_DECISION */
2123 :     xvid_enc_frame.vop_flags |= XVID_VOP_MODEDECISION_RD;
2124 :     break;
2125 :    
2126 :     case 2: /* VHQ_LIMITED_SEARCH */
2127 :     xvid_enc_frame.vop_flags |= XVID_VOP_MODEDECISION_RD;
2128 :     xvid_enc_frame.motion |= XVID_ME_HALFPELREFINE16_RD;
2129 :     xvid_enc_frame.motion |= XVID_ME_QUARTERPELREFINE16_RD;
2130 :     break;
2131 :    
2132 :     case 3: /* VHQ_MEDIUM_SEARCH */
2133 :     xvid_enc_frame.vop_flags |= XVID_VOP_MODEDECISION_RD;
2134 :     xvid_enc_frame.motion |= XVID_ME_HALFPELREFINE16_RD;
2135 :     xvid_enc_frame.motion |= XVID_ME_HALFPELREFINE8_RD;
2136 :     xvid_enc_frame.motion |= XVID_ME_QUARTERPELREFINE16_RD;
2137 :     xvid_enc_frame.motion |= XVID_ME_QUARTERPELREFINE8_RD;
2138 :     xvid_enc_frame.motion |= XVID_ME_CHECKPREDICTION_RD;
2139 :     break;
2140 :    
2141 :     case 4: /* VHQ_WIDE_SEARCH */
2142 :     xvid_enc_frame.vop_flags |= XVID_VOP_MODEDECISION_RD;
2143 :     xvid_enc_frame.motion |= XVID_ME_HALFPELREFINE16_RD;
2144 :     xvid_enc_frame.motion |= XVID_ME_HALFPELREFINE8_RD;
2145 :     xvid_enc_frame.motion |= XVID_ME_QUARTERPELREFINE16_RD;
2146 :     xvid_enc_frame.motion |= XVID_ME_QUARTERPELREFINE8_RD;
2147 :     xvid_enc_frame.motion |= XVID_ME_CHECKPREDICTION_RD;
2148 :     xvid_enc_frame.motion |= XVID_ME_EXTSEARCH_RD;
2149 :     break;
2150 :    
2151 :     default :
2152 :     break;
2153 :     }
2154 : edgomez 1382
2155 : syskin 1710 /* Not sure what this does */
2156 :     // force keyframe spacing in 2-pass 1st pass
2157 :     if (ARG_QUALITY == 0)
2158 :     xvid_enc_frame.type = XVID_TYPE_IVOP;
2159 :    
2160 :     /* frame-based stuff */
2161 :     apply_zone_modifiers(&xvid_enc_frame, framenum);
2162 :    
2163 :    
2164 : edgomez 1382 /* Encode the frame */
2165 :     ret = xvid_encore(enc_handle, XVID_ENC_ENCODE, &xvid_enc_frame,
2166 :     &xvid_enc_stats);
2167 :    
2168 :     *key = (xvid_enc_frame.out_flags & XVID_KEYFRAME);
2169 :     *stats_type = xvid_enc_stats.type;
2170 :     *stats_quant = xvid_enc_stats.quant;
2171 :     *stats_length = xvid_enc_stats.length;
2172 :     sse[0] = xvid_enc_stats.sse_y;
2173 :     sse[1] = xvid_enc_stats.sse_u;
2174 :     sse[2] = xvid_enc_stats.sse_v;
2175 :    
2176 :     return (ret);
2177 : chl 376 }
2178 : syskin 1710
2179 :     void
2180 :     sort_zones(zone_t * zones, int zone_num, int * sel)
2181 :     {
2182 :     int i, j;
2183 :     zone_t tmp;
2184 :     for (i = 0; i < zone_num; i++) {
2185 :     int cur = i;
2186 :     int min_f = zones[i].frame;
2187 :     for (j = i + 1; j < zone_num; j++) {
2188 :     if (zones[j].frame < min_f) {
2189 :     min_f = zones[j].frame;
2190 :     cur = j;
2191 :     }
2192 :     }
2193 :     if (cur != i) {
2194 :     tmp = zones[i];
2195 :     zones[i] = zones[cur];
2196 :     zones[cur] = tmp;
2197 :     if (i == *sel) *sel = cur;
2198 :     else if (cur == *sel) *sel = i;
2199 :     }
2200 :     }
2201 :     }
2202 :    
2203 :     /* constant-quant zones for fixed quant encoding */
2204 :     static void
2205 :     prepare_cquant_zones() {
2206 :    
2207 :     int i = 0;
2208 :     if (NUM_ZONES == 0 || ZONES[0].frame != 0) {
2209 :     /* first zone does not start at frame 0 or doesn't exist */
2210 :    
2211 :     if (NUM_ZONES >= MAX_ZONES) NUM_ZONES--; /* we sacrifice last zone */
2212 :    
2213 :     ZONES[NUM_ZONES].frame = 0;
2214 :     ZONES[NUM_ZONES].mode = XVID_ZONE_QUANT;
2215 :     ZONES[NUM_ZONES].modifier = ARG_CQ;
2216 :     ZONES[NUM_ZONES].type = XVID_TYPE_AUTO;
2217 :     ZONES[NUM_ZONES].greyscale = 0;
2218 :     ZONES[NUM_ZONES].chroma_opt = 0;
2219 :     ZONES[NUM_ZONES].bvop_threshold = 0;
2220 :     ZONES[NUM_ZONES].cartoon_mode = 0;
2221 :     NUM_ZONES++;
2222 :    
2223 :     sort_zones(ZONES, NUM_ZONES, &i);
2224 :     }
2225 :    
2226 :     /* step 2: let's change all weight zones into quant zones */
2227 :    
2228 :     for(i = 0; i < NUM_ZONES; i++)
2229 :     if (ZONES[i].mode == XVID_ZONE_WEIGHT) {
2230 :     ZONES[i].mode = XVID_ZONE_QUANT;
2231 :     ZONES[i].modifier = (100*ARG_CQ) / ZONES[i].modifier;
2232 :     }
2233 :     }
2234 :    
2235 :     /* full first pass zones */
2236 :     static void
2237 :     prepare_full1pass_zones() {
2238 :    
2239 :     int i = 0;
2240 :     if (NUM_ZONES == 0 || ZONES[0].frame != 0) {
2241 :     /* first zone does not start at frame 0 or doesn't exist */
2242 :    
2243 :     if (NUM_ZONES >= MAX_ZONES) NUM_ZONES--; /* we sacrifice last zone */
2244 :    
2245 :     ZONES[NUM_ZONES].frame = 0;
2246 :     ZONES[NUM_ZONES].mode = XVID_ZONE_QUANT;
2247 :     ZONES[NUM_ZONES].modifier = 200;
2248 :     ZONES[NUM_ZONES].type = XVID_TYPE_AUTO;
2249 :     ZONES[NUM_ZONES].greyscale = 0;
2250 :     ZONES[NUM_ZONES].chroma_opt = 0;
2251 :     ZONES[NUM_ZONES].bvop_threshold = 0;
2252 :     ZONES[NUM_ZONES].cartoon_mode = 0;
2253 :     NUM_ZONES++;
2254 :    
2255 :     sort_zones(ZONES, NUM_ZONES, &i);
2256 :     }
2257 :    
2258 :     /* step 2: let's change all weight zones into quant zones */
2259 :    
2260 :     for(i = 0; i < NUM_ZONES; i++)
2261 :     if (ZONES[i].mode == XVID_ZONE_WEIGHT) {
2262 :     ZONES[i].mode = XVID_ZONE_QUANT;
2263 :     ZONES[i].modifier = 200;
2264 :     }
2265 :     }
2266 :    
2267 :     static void apply_zone_modifiers(xvid_enc_frame_t * frame, int framenum)
2268 :     {
2269 :     int i;
2270 :    
2271 :     for (i=0; i<NUM_ZONES && ZONES[i].frame <= framenum; i++) ;
2272 :    
2273 :     if (--i < 0) return; /* there are no zones, or we're before the first zone */
2274 :    
2275 :     if (framenum == ZONES[i].frame)
2276 :     frame->type = ZONES[i].type;
2277 :    
2278 :     if (ZONES[i].greyscale) {
2279 :     frame->vop_flags |= XVID_VOP_GREYSCALE;
2280 :     }
2281 :    
2282 :     if (ZONES[i].chroma_opt) {
2283 :     frame->vop_flags |= XVID_VOP_CHROMAOPT;
2284 :     }
2285 :    
2286 :     if (ZONES[i].cartoon_mode) {
2287 :     frame->vop_flags |= XVID_VOP_CARTOON;
2288 :     frame->motion |= XVID_ME_DETECT_STATIC_MOTION;
2289 :     }
2290 :    
2291 :     if (ARG_MAXBFRAMES) {
2292 :     frame->bframe_threshold = ZONES[i].bvop_threshold;
2293 :     }
2294 :     }
2295 :    
2296 :     void removedivxp(char *buf, int bufsize) {
2297 :     int i;
2298 :     char* userdata;
2299 :    
2300 :     for (i=0; i <= (bufsize-sizeof(userdata_start_code)); i++) {
2301 :     if (memcmp((void*)userdata_start_code, (void*)(buf+i), strlen(userdata_start_code))==0) {
2302 :     if ((userdata = strstr(buf+i+4, "DivX"))!=NULL) {
2303 :     userdata[strlen(userdata)-1] = '\0';
2304 :     return;
2305 :     }
2306 :     }
2307 :     }
2308 :     }

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