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