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

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