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