50 |
#include <windows.h> |
#include <windows.h> |
51 |
#include <vfw.h> |
#include <vfw.h> |
52 |
|
|
53 |
|
#include <xvid.h> |
54 |
|
#include "debug.h" |
55 |
#include "codec.h" |
#include "codec.h" |
|
#include "2pass.h" |
|
56 |
|
|
57 |
int pmvfast_presets[7] = { |
|
58 |
|
static const int pmvfast_presets[7] = { |
59 |
0, 0, 0, 0, |
0, 0, 0, 0, |
60 |
0 | XVID_ME_HALFPELREFINE16 | 0, |
0 | XVID_ME_HALFPELREFINE16 | 0, |
61 |
0 | XVID_ME_HALFPELREFINE16 | 0 | |
0 | XVID_ME_HALFPELREFINE16 | 0 | |
63 |
XVID_ME_HALFPELREFINE8 | 0 | XVID_ME_USESQUARES16 |
XVID_ME_HALFPELREFINE8 | 0 | XVID_ME_USESQUARES16 |
64 |
}; |
}; |
65 |
|
|
66 |
|
|
67 |
|
|
68 |
/* return xvid compatbile colorspace, |
/* return xvid compatbile colorspace, |
69 |
or XVID_CSP_NULL if failure |
or XVID_CSP_NULL if failure |
70 |
*/ |
*/ |
79 |
case BI_RGB : |
case BI_RGB : |
80 |
if (hdr->biBitCount == 16) |
if (hdr->biBitCount == 16) |
81 |
{ |
{ |
82 |
DEBUG("RGB16 (RGB555)"); |
DPRINTF("RGB16 (RGB555)"); |
83 |
return rgb_flip | XVID_CSP_RGB555; |
return rgb_flip | XVID_CSP_RGB555; |
84 |
} |
} |
85 |
if (hdr->biBitCount == 24) |
if (hdr->biBitCount == 24) |
86 |
{ |
{ |
87 |
DEBUG("RGB24"); |
DPRINTF("RGB24"); |
88 |
return rgb_flip | XVID_CSP_BGR; |
return rgb_flip | XVID_CSP_BGR; |
89 |
} |
} |
90 |
if (hdr->biBitCount == 32) |
if (hdr->biBitCount == 32) |
91 |
{ |
{ |
92 |
DEBUG("RGB32"); |
DPRINTF("RGB32"); |
93 |
return rgb_flip | XVID_CSP_BGRA; |
return rgb_flip | XVID_CSP_BGRA; |
94 |
} |
} |
95 |
|
|
96 |
DEBUG1("unsupported BI_RGB biBitCount", hdr->biBitCount); |
DPRINTF("unsupported BI_RGB biBitCount=%i", hdr->biBitCount); |
97 |
return XVID_CSP_NULL; |
return XVID_CSP_NULL; |
98 |
|
|
99 |
case BI_BITFIELDS : |
case BI_BITFIELDS : |
106 |
hdr4->bV4GreenMask == 0x3e0 && |
hdr4->bV4GreenMask == 0x3e0 && |
107 |
hdr4->bV4BlueMask == 0x1f) |
hdr4->bV4BlueMask == 0x1f) |
108 |
{ |
{ |
109 |
DEBUG("RGB555"); |
DPRINTF("RGB555"); |
110 |
return rgb_flip | XVID_CSP_RGB555; |
return rgb_flip | XVID_CSP_RGB555; |
111 |
} |
} |
112 |
|
|
115 |
hdr4->bV4GreenMask == 0x7e0 && |
hdr4->bV4GreenMask == 0x7e0 && |
116 |
hdr4->bV4BlueMask == 0x1f) |
hdr4->bV4BlueMask == 0x1f) |
117 |
{ |
{ |
118 |
DEBUG("RGB565"); |
DPRINTF("RGB565"); |
119 |
return rgb_flip | XVID_CSP_RGB565; |
return rgb_flip | XVID_CSP_RGB565; |
120 |
} |
} |
121 |
|
|
122 |
DEBUG("unsupported BI_BITFIELDS mode"); |
DPRINTF("unsupported BI_BITFIELDS mode"); |
123 |
return XVID_CSP_NULL; |
return XVID_CSP_NULL; |
124 |
} |
} |
125 |
|
|
126 |
DEBUG("unsupported BI_BITFIELDS/BITMAPHEADER combination"); |
DPRINTF("unsupported BI_BITFIELDS/BITMAPHEADER combination"); |
127 |
return XVID_CSP_NULL; |
return XVID_CSP_NULL; |
128 |
|
|
129 |
case FOURCC_I420 : |
case FOURCC_I420 : |
130 |
case FOURCC_IYUV : |
case FOURCC_IYUV : |
131 |
DEBUG("IYUY"); |
DPRINTF("IYUY"); |
132 |
return XVID_CSP_I420; |
return XVID_CSP_I420; |
133 |
|
|
134 |
case FOURCC_YV12 : |
case FOURCC_YV12 : |
135 |
DEBUG("YV12"); |
DPRINTF("YV12"); |
136 |
return XVID_CSP_YV12; |
return XVID_CSP_YV12; |
137 |
|
|
138 |
case FOURCC_YUYV : |
case FOURCC_YUYV : |
139 |
case FOURCC_YUY2 : |
case FOURCC_YUY2 : |
140 |
DEBUG("YUY2"); |
DPRINTF("YUY2"); |
141 |
return XVID_CSP_YUY2; |
return XVID_CSP_YUY2; |
142 |
|
|
143 |
case FOURCC_YVYU : |
case FOURCC_YVYU : |
144 |
DEBUG("YVYU"); |
DPRINTF("YVYU"); |
145 |
return XVID_CSP_YVYU; |
return XVID_CSP_YVYU; |
146 |
|
|
147 |
case FOURCC_UYVY : |
case FOURCC_UYVY : |
148 |
DEBUG("UYVY"); |
DPRINTF("UYVY"); |
149 |
return XVID_CSP_UYVY; |
return XVID_CSP_UYVY; |
150 |
|
|
151 |
default : |
default : |
152 |
DEBUGFOURCC("unsupported colorspace", hdr->biCompression); |
DPRINTF("unsupported colorspace %c%c%c%c", |
153 |
|
hdr->biCompression&0xff, |
154 |
|
(hdr->biCompression>>8)&0xff, |
155 |
|
(hdr->biCompression>>16)&0xff, |
156 |
|
(hdr->biCompression>>24)&0xff); |
157 |
return XVID_CSP_NULL; |
return XVID_CSP_NULL; |
158 |
} |
} |
159 |
} |
} |
237 |
|
|
238 |
LRESULT compress_frames_info(CODEC * codec, ICCOMPRESSFRAMES * icf) |
LRESULT compress_frames_info(CODEC * codec, ICCOMPRESSFRAMES * icf) |
239 |
{ |
{ |
|
// DEBUG2("frate fscale", codec->frate, codec->fscale); |
|
240 |
codec->fincr = icf->dwScale; |
codec->fincr = icf->dwScale; |
241 |
codec->fbase = icf->dwRate; |
codec->fbase = icf->dwRate; |
242 |
return ICERR_OK; |
return ICERR_OK; |
243 |
} |
} |
244 |
|
|
245 |
|
|
246 |
LRESULT compress_begin(CODEC * codec, BITMAPINFO * lpbiInput, BITMAPINFO * lpbiOutput) |
const char type2char(int type) |
247 |
{ |
{ |
248 |
xvid_gbl_init_t init; |
if (type==XVID_TYPE_IVOP) |
249 |
xvid_enc_create_t create; |
return 'I'; |
250 |
xvid_enc_rc_t rc; |
if (type==XVID_TYPE_PVOP) |
251 |
xvid_enc_plugin_t plugins[1]; |
return 'P'; |
252 |
|
if (type==XVID_TYPE_BVOP) |
253 |
memset(&rc, 0, sizeof(rc)); |
return 'B'; |
254 |
rc.version = XVID_VERSION; |
return 'S'; |
255 |
|
} |
256 |
|
|
257 |
|
int vfw_debug(void *handle, |
258 |
|
int opt, |
259 |
|
void *param1, |
260 |
|
void *param2) |
261 |
|
{ |
262 |
|
switch (opt) { |
263 |
|
case XVID_PLG_INFO: |
264 |
|
case XVID_PLG_CREATE: |
265 |
|
case XVID_PLG_DESTROY: |
266 |
|
case XVID_PLG_BEFORE: |
267 |
|
return 0; |
268 |
|
|
269 |
switch (codec->config.mode) |
case XVID_PLG_AFTER: |
270 |
{ |
{ |
271 |
case DLG_MODE_CBR : |
xvid_plg_data_t *data = (xvid_plg_data_t *) param1; |
|
//rc.type = XVID_RC_CBR; |
|
|
rc.min_iquant = codec->config.min_iquant; |
|
|
rc.max_iquant = codec->config.max_iquant; |
|
|
rc.min_pquant = codec->config.min_pquant; |
|
|
rc.max_pquant = codec->config.max_pquant; |
|
|
rc.min_bquant = 0; /* XXX: todo */ |
|
|
rc.max_bquant = 0; /* XXX: todo */ |
|
|
rc.data.cbr.bitrate = codec->config.rc_bitrate; |
|
|
rc.data.cbr.reaction_delay_factor = codec->config.rc_reaction_delay_factor; |
|
|
rc.data.cbr.averaging_period = codec->config.rc_averaging_period; |
|
|
rc.data.cbr.buffer = codec->config.rc_buffer; |
|
|
break; |
|
272 |
|
|
273 |
case DLG_MODE_VBR_QUAL : |
DPRINTF("[%5i] type=%c Q:%2i length:%6i", |
274 |
codec->config.fquant = 0; |
data->frame_num, |
275 |
//param.rc_bitrate = 0; |
type2char(data->type), |
276 |
break; |
data->quant, |
277 |
|
data->length); |
278 |
|
return 0; |
279 |
|
} |
280 |
|
} |
281 |
|
|
282 |
case DLG_MODE_VBR_QUANT : |
return XVID_ERR_FAIL; |
283 |
codec->config.fquant = (float) codec->config.quant; |
} |
|
//param.rc_bitrate = 0; |
|
|
break; |
|
284 |
|
|
|
case DLG_MODE_2PASS_1 : |
|
|
case DLG_MODE_2PASS_2_INT : |
|
|
case DLG_MODE_2PASS_2_EXT : |
|
|
//param.rc_bitrate = 0; |
|
|
codec->twopass.max_framesize = (int)((double)codec->config.twopass_max_bitrate / 8.0 / ((double)codec->fbase / (double)codec->fincr)); |
|
|
break; |
|
285 |
|
|
|
case DLG_MODE_NULL : |
|
|
return ICERR_OK; |
|
286 |
|
|
287 |
default : |
LRESULT compress_begin(CODEC * codec, BITMAPINFO * lpbiInput, BITMAPINFO * lpbiOutput) |
288 |
break; |
{ |
289 |
} |
xvid_gbl_init_t init; |
290 |
|
xvid_enc_create_t create; |
291 |
|
xvid_enc_plugin_t plugins[3]; |
292 |
|
|
293 |
|
xvid_plugin_fixed_t fixed; |
294 |
|
xvid_plugin_cbr_t cbr; |
295 |
|
xvid_plugin_2pass1_t pass1; |
296 |
|
xvid_plugin_2pass2_t pass2; |
297 |
|
|
298 |
/* destroy previously created codec */ |
/* destroy previously created codec */ |
299 |
if(codec->ehandle) |
if(codec->ehandle) { |
|
{ |
|
300 |
xvid_encore(codec->ehandle, XVID_ENC_DESTROY, NULL, NULL); |
xvid_encore(codec->ehandle, XVID_ENC_DESTROY, NULL, NULL); |
301 |
codec->ehandle = NULL; |
codec->ehandle = NULL; |
302 |
} |
} |
308 |
|
|
309 |
memset(&create, 0, sizeof(create)); |
memset(&create, 0, sizeof(create)); |
310 |
create.version = XVID_VERSION; |
create.version = XVID_VERSION; |
311 |
|
create.plugins = plugins; |
312 |
|
|
313 |
create.width = lpbiInput->bmiHeader.biWidth; |
switch (codec->config.mode) |
314 |
create.height = lpbiInput->bmiHeader.biHeight; |
{ |
315 |
create.fincr = codec->fincr; |
case RC_MODE_CBR : |
316 |
create.fbase = codec->fbase; |
memset(&cbr, 0, sizeof(cbr)); |
317 |
|
cbr.version = XVID_VERSION; |
318 |
|
cbr.bitrate = codec->config.rc_bitrate; |
319 |
|
cbr.reaction_delay_factor = codec->config.rc_reaction_delay_factor; |
320 |
|
cbr.averaging_period = codec->config.rc_averaging_period; |
321 |
|
cbr.buffer = codec->config.rc_buffer; |
322 |
|
plugins[create.num_plugins].func = xvid_plugin_cbr; |
323 |
|
plugins[create.num_plugins].param = &cbr; |
324 |
|
create.num_plugins++; |
325 |
|
|
326 |
|
case RC_MODE_FIXED : |
327 |
|
memset(&fixed, 0, sizeof(fixed)); |
328 |
|
fixed.version = XVID_VERSION; |
329 |
|
fixed.quant_increment = codec->config.quant; |
330 |
|
fixed.quant_base = 1; |
331 |
|
plugins[create.num_plugins].func = xvid_plugin_fixed; |
332 |
|
plugins[create.num_plugins].param = &fixed; |
333 |
|
create.num_plugins++; |
334 |
|
break; |
335 |
|
|
336 |
|
case RC_MODE_2PASS1 : |
337 |
|
memset(&pass1, 0, sizeof(pass1)); |
338 |
|
pass1.version = XVID_VERSION; |
339 |
|
|
340 |
|
plugins[create.num_plugins].func = xvid_plugin_2pass1; |
341 |
|
plugins[create.num_plugins].param = &pass1; |
342 |
|
create.num_plugins++; |
343 |
|
break; |
344 |
|
|
345 |
|
|
346 |
|
case RC_MODE_2PASS2_INT : |
347 |
|
case RC_MODE_2PASS2_EXT : |
348 |
|
memset(&pass2, 0, sizeof(pass2)); |
349 |
|
pass2.version = XVID_VERSION; |
350 |
|
pass2.min_quant[0] = codec->config.min_iquant; |
351 |
|
pass2.max_quant[0] = codec->config.max_iquant; |
352 |
|
pass2.min_quant[1] = codec->config.min_pquant; |
353 |
|
pass2.max_quant[1] = codec->config.max_pquant; |
354 |
|
//pass2.min_quant[2] = codec->config.min_bquant; |
355 |
|
//pass2.max_quant[2] = codec->config.max_bquant; |
356 |
|
pass2.filename = codec->config.stats; |
357 |
|
if (codec->config.mode == RC_MODE_2PASS2_INT) { |
358 |
|
pass2.bitrate = 10000; /* xxx */ |
359 |
|
} |
360 |
|
plugins[create.num_plugins].func = xvid_plugin_2pass2; |
361 |
|
plugins[create.num_plugins].param = &pass2; |
362 |
|
create.num_plugins++; |
363 |
|
break; |
364 |
|
|
365 |
|
case RC_MODE_NULL : |
366 |
|
return ICERR_OK; |
367 |
|
|
368 |
|
default : |
369 |
|
break; |
370 |
|
} |
371 |
|
|
|
create.plugins = plugins; |
|
|
create.num_plugins = 0; |
|
372 |
if (codec->config.lum_masking) { |
if (codec->config.lum_masking) { |
373 |
plugins[create.num_plugins].func = xvid_plugin_lumimasking; |
plugins[create.num_plugins].func = xvid_plugin_lumimasking; |
374 |
plugins[create.num_plugins].param = NULL; |
plugins[create.num_plugins].param = NULL; |
375 |
create.num_plugins++; |
create.num_plugins++; |
376 |
} |
} |
377 |
|
|
378 |
|
plugins[create.num_plugins].func = vfw_debug; |
379 |
|
plugins[create.num_plugins].param = NULL; |
380 |
|
create.num_plugins++; |
381 |
|
|
382 |
|
create.width = lpbiInput->bmiHeader.biWidth; |
383 |
|
create.height = lpbiInput->bmiHeader.biHeight; |
384 |
|
create.fincr = codec->fincr; |
385 |
|
create.fbase = codec->fbase; |
386 |
|
|
387 |
if (codec->config.packed) |
if (codec->config.packed) |
388 |
create.global |= XVID_GLOBAL_PACKED; |
create.global |= XVID_GLOBAL_PACKED; |
389 |
if (codec->config.dx50bvop) |
if (codec->config.closed_gov) |
390 |
create.global |= XVID_GLOBAL_CLOSED_GOP; |
create.global |= XVID_GLOBAL_CLOSED_GOP; |
391 |
|
|
392 |
create.max_key_interval = codec->config.max_key_interval; |
create.max_key_interval = codec->config.max_key_interval; |
393 |
/* XXX: param.min_quantizer = codec->config.min_pquant; |
/* XXX: param.min_quantizer = codec->config.min_pquant; |
394 |
param.max_quantizer = codec->config.max_pquant; */ |
param.max_quantizer = codec->config.max_pquant; */ |
395 |
|
|
396 |
|
if (codec->config.use_bvop) |
397 |
create.max_bframes = codec->config.max_bframes; |
create.max_bframes = codec->config.max_bframes; |
398 |
create.frame_drop_ratio = codec->config.frame_drop_ratio; |
create.frame_drop_ratio = codec->config.frame_drop_ratio; |
399 |
|
|
402 |
|
|
403 |
create.num_threads = codec->config.num_threads; |
create.num_threads = codec->config.num_threads; |
404 |
|
|
405 |
switch(xvid_encore(0, XVID_ENC_CREATE, &create, (codec->config.mode==DLG_MODE_CBR)?&rc:NULL )) |
switch(xvid_encore(0, XVID_ENC_CREATE, &create, NULL)) |
406 |
{ |
{ |
407 |
case XVID_ERR_FAIL : |
case XVID_ERR_FAIL : |
408 |
return ICERR_ERROR; |
return ICERR_ERROR; |
421 |
codec->framenum = 0; |
codec->framenum = 0; |
422 |
codec->keyspacing = 0; |
codec->keyspacing = 0; |
423 |
|
|
|
codec->twopass.hints = codec->twopass.stats1 = codec->twopass.stats2 = INVALID_HANDLE_VALUE; |
|
|
codec->twopass.hintstream = NULL; |
|
|
|
|
424 |
return ICERR_OK; |
return ICERR_OK; |
425 |
} |
} |
426 |
|
|
430 |
if (codec->ehandle != NULL) |
if (codec->ehandle != NULL) |
431 |
{ |
{ |
432 |
xvid_encore(codec->ehandle, XVID_ENC_DESTROY, NULL, NULL); |
xvid_encore(codec->ehandle, XVID_ENC_DESTROY, NULL, NULL); |
|
|
|
|
if (codec->twopass.hints != INVALID_HANDLE_VALUE) |
|
|
{ |
|
|
CloseHandle(codec->twopass.hints); |
|
|
} |
|
|
if (codec->twopass.stats1 != INVALID_HANDLE_VALUE) |
|
|
{ |
|
|
CloseHandle(codec->twopass.stats1); |
|
|
} |
|
|
if (codec->twopass.stats2 != INVALID_HANDLE_VALUE) |
|
|
{ |
|
|
CloseHandle(codec->twopass.stats2); |
|
|
} |
|
|
if (codec->twopass.hintstream != NULL) |
|
|
{ |
|
|
free(codec->twopass.hintstream); |
|
|
} |
|
|
|
|
433 |
codec->ehandle = NULL; |
codec->ehandle = NULL; |
|
|
|
|
codec_2pass_finish(codec); |
|
434 |
} |
} |
435 |
|
|
436 |
return ICERR_OK; |
return ICERR_OK; |
445 |
int length; |
int length; |
446 |
|
|
447 |
// mpeg2avi yuv bug workaround (2 instances of CODEC) |
// mpeg2avi yuv bug workaround (2 instances of CODEC) |
|
if (codec->twopass.stats1 == INVALID_HANDLE_VALUE) |
|
|
{ |
|
|
if (codec_2pass_init(codec) == ICERR_ERROR) |
|
|
{ |
|
|
return ICERR_ERROR; |
|
|
} |
|
|
} |
|
448 |
|
|
449 |
memset(&frame, 0, sizeof(frame)); |
memset(&frame, 0, sizeof(frame)); |
450 |
frame.version = XVID_VERSION; |
frame.version = XVID_VERSION; |
504 |
if (codec->config.chroma_opt) |
if (codec->config.chroma_opt) |
505 |
frame.vop_flags |= XVID_VOP_CHROMAOPT; |
frame.vop_flags |= XVID_VOP_CHROMAOPT; |
506 |
|
|
|
check_greyscale_mode(&codec->config, &frame, codec->framenum); |
|
|
|
|
|
/* XXX: hinted me |
|
|
|
|
|
// fix 1pass modes/hinted MV by koepi |
|
|
if (codec->config.hinted_me && (codec->config.mode == DLG_MODE_CBR || codec->config.mode == DLG_MODE_VBR_QUAL || codec->config.mode == DLG_MODE_VBR_QUANT)) |
|
|
{ |
|
|
codec->config.hinted_me = 0; |
|
|
} |
|
|
// end of ugly hack |
|
|
|
|
|
if (codec->config.hinted_me && codec->config.mode == DLG_MODE_2PASS_1) |
|
|
{ |
|
|
frame.hint.hintstream = codec->twopass.hintstream; |
|
|
frame.hint.rawhints = 0; |
|
|
frame.general |= XVID_HINTEDME_GET; |
|
|
} |
|
|
else if (codec->config.hinted_me && (codec->config.mode == DLG_MODE_2PASS_2_EXT || codec->config.mode == DLG_MODE_2PASS_2_INT)) |
|
|
{ |
|
|
DWORD read; |
|
|
DWORD blocksize; |
|
|
|
|
|
frame.hint.hintstream = codec->twopass.hintstream; |
|
|
frame.hint.rawhints = 0; |
|
|
frame.general |= XVID_HINTEDME_SET; |
|
|
|
|
|
if (codec->twopass.hints == INVALID_HANDLE_VALUE) |
|
|
{ |
|
|
codec->twopass.hints = CreateFile(codec->config.hintfile, GENERIC_READ, 0, 0, OPEN_EXISTING, 0, 0); |
|
|
if (codec->twopass.hints == INVALID_HANDLE_VALUE) |
|
|
{ |
|
|
DEBUGERR("couldn't open hints file"); |
|
|
return ICERR_ERROR; |
|
|
} |
|
|
} |
|
|
if (!ReadFile(codec->twopass.hints, &blocksize, sizeof(DWORD), &read, 0) || read != sizeof(DWORD) || |
|
|
!ReadFile(codec->twopass.hints, frame.hint.hintstream, blocksize, &read, 0) || read != blocksize) |
|
|
{ |
|
|
DEBUGERR("couldn't read from hints file"); |
|
|
return ICERR_ERROR; |
|
|
} |
|
|
} */ |
|
|
|
|
507 |
frame.motion |= pmvfast_presets[codec->config.motion_search]; |
frame.motion |= pmvfast_presets[codec->config.motion_search]; |
508 |
|
|
509 |
switch (codec->config.vhq_mode) |
switch (codec->config.vhq_mode) |
541 |
break; |
break; |
542 |
} |
} |
543 |
|
|
544 |
|
frame.bframe_threshold = codec->config.bvop_threshold; |
545 |
|
|
546 |
frame.input.plane[0] = icc->lpInput; |
frame.input.plane[0] = icc->lpInput; |
547 |
frame.input.stride[0] = (((icc->lpbiInput->biWidth * icc->lpbiInput->biBitCount) + 31) & ~31) >> 3; |
frame.input.stride[0] = (((icc->lpbiInput->biWidth * icc->lpbiInput->biBitCount) + 31) & ~31) >> 3; |
548 |
|
|
557 |
|
|
558 |
switch (codec->config.mode) |
switch (codec->config.mode) |
559 |
{ |
{ |
560 |
case DLG_MODE_CBR : |
case RC_MODE_CBR : |
561 |
frame.quant = 0; /* use xvidcore cbr rate control */ |
frame.quant = 0; /* use xvidcore cbr rate control */ |
562 |
break; |
break; |
563 |
|
|
564 |
case DLG_MODE_VBR_QUAL : |
//case RC_MODE_VBR_QUAL : |
565 |
case DLG_MODE_VBR_QUANT : |
case RC_MODE_FIXED : |
566 |
case DLG_MODE_2PASS_1 : |
case RC_MODE_2PASS1 : |
567 |
if (codec_get_quant(codec, &frame) == ICERR_ERROR) |
/*if (codec_get_quant(codec, &frame) == ICERR_ERROR) |
568 |
{ |
{ |
569 |
return ICERR_ERROR; |
return ICERR_ERROR; |
570 |
} |
}*/ |
571 |
break; |
break; |
572 |
|
|
573 |
case DLG_MODE_2PASS_2_EXT : |
case RC_MODE_2PASS2_EXT : |
574 |
case DLG_MODE_2PASS_2_INT : |
case RC_MODE_2PASS2_INT : |
575 |
if (codec_2pass_get_quant(codec, &frame) == ICERR_ERROR) |
/*if (codec_2pass_get_quant(codec, &frame) == ICERR_ERROR) |
576 |
{ |
{ |
577 |
return ICERR_ERROR; |
return ICERR_ERROR; |
578 |
} |
}*/ |
|
if (codec->config.dummy2pass) |
|
|
{ |
|
|
outhdr->biSizeImage = codec->twopass.bytes2; |
|
|
*icc->lpdwFlags = (codec->twopass.nns1.quant & NNSTATS_KEYFRAME) ? AVIIF_KEYFRAME : 0; |
|
|
return ICERR_OK; |
|
|
} |
|
579 |
break; |
break; |
580 |
|
|
581 |
case DLG_MODE_NULL : |
case RC_MODE_NULL : |
582 |
outhdr->biSizeImage = 0; |
outhdr->biSizeImage = 0; |
583 |
*icc->lpdwFlags = AVIIF_KEYFRAME; |
*icc->lpdwFlags = AVIIF_KEYFRAME; |
584 |
return ICERR_OK; |
return ICERR_OK; |
585 |
|
|
586 |
default : |
default : |
587 |
DEBUGERR("Invalid encoding mode"); |
DPRINTF("Invalid encoding mode"); |
588 |
return ICERR_ERROR; |
return ICERR_ERROR; |
589 |
} |
} |
590 |
|
|
596 |
} |
} |
597 |
else if (codec->keyspacing < codec->config.min_key_interval && codec->framenum) |
else if (codec->keyspacing < codec->config.min_key_interval && codec->framenum) |
598 |
{ |
{ |
599 |
DEBUG("current frame forced to p-frame"); |
DPRINTF("current frame forced to p-frame"); |
600 |
frame.type = XVID_TYPE_PVOP; |
frame.type = XVID_TYPE_PVOP; |
601 |
} |
} |
602 |
|
|
619 |
return ICERR_UNSUPPORTED; |
return ICERR_UNSUPPORTED; |
620 |
} |
} |
621 |
|
|
622 |
{ /* XXX: debug text */ |
DPRINTF("{type=%i len=%i} length=%i", stats.type, stats.length, length); |
|
char tmp[100]; |
|
|
wsprintf(tmp, " {type=%i len=%i} length=%i", stats.type, stats.length, length); |
|
|
OutputDebugString(tmp); |
|
|
} |
|
623 |
|
|
624 |
if (length == 0) /* no encoder output */ |
if (length == 0) /* no encoder output */ |
625 |
{ |
{ |
640 |
|
|
641 |
outhdr->biSizeImage = length; |
outhdr->biSizeImage = length; |
642 |
|
|
643 |
if (codec->config.mode == DLG_MODE_2PASS_1 && codec->config.discard1pass) |
if (codec->config.mode == RC_MODE_2PASS1 && codec->config.discard1pass) |
644 |
{ |
{ |
645 |
outhdr->biSizeImage = 0; |
outhdr->biSizeImage = 0; |
646 |
} |
} |
|
|
|
|
/* XXX: hinted me |
|
|
if (frame.general & XVID_HINTEDME_GET) |
|
|
{ |
|
|
DWORD wrote; |
|
|
DWORD blocksize = frame.hint.hintlength; |
|
|
|
|
|
if (codec->twopass.hints == INVALID_HANDLE_VALUE) |
|
|
{ |
|
|
codec->twopass.hints = CreateFile(codec->config.hintfile, GENERIC_WRITE, 0, 0, CREATE_ALWAYS, 0, 0); |
|
|
if (codec->twopass.hints == INVALID_HANDLE_VALUE) |
|
|
{ |
|
|
DEBUGERR("couldn't create hints file"); |
|
|
return ICERR_ERROR; |
|
|
} |
|
|
} |
|
|
if (!WriteFile(codec->twopass.hints, &frame.hint.hintlength, sizeof(int), &wrote, 0) || wrote != sizeof(int) || |
|
|
!WriteFile(codec->twopass.hints, frame.hint.hintstream, blocksize, &wrote, 0) || wrote != blocksize) |
|
|
{ |
|
|
DEBUGERR("couldn't write to hints file"); |
|
|
return ICERR_ERROR; |
|
|
} |
|
|
} |
|
|
*/ |
|
|
|
|
|
if (stats.type > 0) |
|
|
{ |
|
|
codec_2pass_update(codec, &stats); |
|
|
} |
|
647 |
} |
} |
648 |
|
|
649 |
++codec->framenum; |
++codec->framenum; |
787 |
{ |
{ |
788 |
xvid_gbl_convert_t convert; |
xvid_gbl_convert_t convert; |
789 |
|
|
790 |
DEBUGFOURCC("input", icd->lpbiInput->biCompression); |
DPRINTF("input=%c%c%c%c output=%c%c%c%c", |
791 |
DEBUGFOURCC("output", icd->lpbiOutput->biCompression); |
icd->lpbiInput->biCompression&0xff, |
792 |
|
(icd->lpbiInput->biCompression>>8)&0xff, |
793 |
|
(icd->lpbiInput->biCompression>>16)&0xff, |
794 |
|
(icd->lpbiInput->biCompression>>24)&0xff, |
795 |
|
icd->lpbiOutput->biCompression&0xff, |
796 |
|
(icd->lpbiOutput->biCompression>>8)&0xff, |
797 |
|
(icd->lpbiOutput->biCompression>>16)&0xff, |
798 |
|
(icd->lpbiOutput->biCompression>>24)&0xff); |
799 |
|
|
800 |
memset(&convert, 0, sizeof(convert)); |
memset(&convert, 0, sizeof(convert)); |
801 |
convert.version = XVID_VERSION; |
convert.version = XVID_VERSION; |
865 |
return ICERR_OK; |
return ICERR_OK; |
866 |
} |
} |
867 |
|
|
|
int codec_get_quant(CODEC* codec, xvid_enc_frame_t* frame) |
|
|
{ |
|
|
switch (codec->config.mode) |
|
|
{ |
|
|
case DLG_MODE_VBR_QUAL : |
|
|
if (codec_is_in_credits(&codec->config, codec->framenum)) |
|
|
{ |
|
|
// added by koepi for credits greyscale |
|
|
|
|
|
check_greyscale_mode(&codec->config, frame, codec->framenum); |
|
|
|
|
|
// end of koepi's addition |
|
|
|
|
|
switch (codec->config.credits_mode) |
|
|
{ |
|
|
case CREDITS_MODE_RATE : |
|
|
frame->quant = codec_get_vbr_quant(&codec->config, codec->config.quality * codec->config.credits_rate / 100); |
|
|
break; |
|
|
|
|
|
case CREDITS_MODE_QUANT : |
|
|
frame->quant = codec->config.credits_quant_p; |
|
|
break; |
|
|
|
|
|
default : |
|
|
DEBUGERR("Can't use credits size mode in quality mode"); |
|
|
return ICERR_ERROR; |
|
|
} |
|
|
} |
|
|
else |
|
|
{ |
|
|
// added by koepi for credits greyscale |
|
|
|
|
|
check_greyscale_mode(&codec->config, frame, codec->framenum); |
|
|
|
|
|
// end of koepi's addition |
|
|
|
|
|
frame->quant = codec_get_vbr_quant(&codec->config, codec->config.quality); |
|
|
} |
|
|
return ICERR_OK; |
|
|
|
|
|
case DLG_MODE_VBR_QUANT : |
|
|
if (codec_is_in_credits(&codec->config, codec->framenum)) |
|
|
{ |
|
|
// added by koepi for credits greyscale |
|
|
|
|
|
check_greyscale_mode(&codec->config, frame, codec->framenum); |
|
|
|
|
|
// end of koepi's addition |
|
|
|
|
|
switch (codec->config.credits_mode) |
|
|
{ |
|
|
case CREDITS_MODE_RATE : |
|
|
frame->quant = |
|
|
codec->config.max_pquant - |
|
|
((codec->config.max_pquant - codec->config.quant) * codec->config.credits_rate / 100); |
|
|
break; |
|
|
|
|
|
case CREDITS_MODE_QUANT : |
|
|
frame->quant = codec->config.credits_quant_p; |
|
|
break; |
|
|
|
|
|
default : |
|
|
DEBUGERR("Can't use credits size mode in quantizer mode"); |
|
|
return ICERR_ERROR; |
|
|
} |
|
|
} |
|
|
else |
|
|
{ |
|
|
// added by koepi for credits greyscale |
|
|
|
|
|
check_greyscale_mode(&codec->config, frame, codec->framenum); |
|
|
|
|
|
// end of koepi's addition |
|
|
|
|
|
frame->quant = codec->config.quant; |
|
|
} |
|
|
return ICERR_OK; |
|
|
|
|
|
case DLG_MODE_2PASS_1 : |
|
|
// added by koepi for credits greyscale |
|
|
|
|
|
check_greyscale_mode(&codec->config, frame, codec->framenum); |
|
|
|
|
|
// end of koepi's addition |
|
|
|
|
|
if (codec->config.credits_mode == CREDITS_MODE_QUANT) |
|
|
{ |
|
|
if (codec_is_in_credits(&codec->config, codec->framenum)) |
|
|
{ |
|
|
frame->quant = codec->config.credits_quant_p; |
|
|
} |
|
|
else |
|
|
{ |
|
|
frame->quant = 2; |
|
|
} |
|
|
} |
|
|
else |
|
|
{ |
|
|
frame->quant = 2; |
|
|
} |
|
|
return ICERR_OK; |
|
|
|
|
|
default: |
|
|
DEBUGERR("get quant: invalid mode"); |
|
|
return ICERR_ERROR; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
int codec_is_in_credits(CONFIG* config, int framenum) |
|
|
{ |
|
|
if (config->credits_start) |
|
|
{ |
|
|
if (framenum >= config->credits_start_begin && |
|
|
framenum <= config->credits_start_end) |
|
|
{ |
|
|
return CREDITS_START; |
|
|
} |
|
|
} |
|
|
|
|
|
if (config->credits_end) |
|
|
{ |
|
|
if (framenum >= config->credits_end_begin && |
|
|
framenum <= config->credits_end_end) |
|
|
{ |
|
|
return CREDITS_END; |
|
|
} |
|
|
} |
|
|
|
|
|
return 0; |
|
|
} |
|
|
|
|
|
|
|
|
int codec_get_vbr_quant(CONFIG* config, int quality) |
|
|
{ |
|
|
static float fquant_running = 0; |
|
|
static int my_quality = -1; |
|
|
int quant; |
|
|
|
|
|
// if quality changes, recalculate fquant (credits) |
|
|
if (quality != my_quality) |
|
|
{ |
|
|
config->fquant = 0; |
|
|
} |
|
|
|
|
|
my_quality = quality; |
|
|
|
|
|
// desired quantiser = (maxQ-minQ)/100 * (100-qual) + minQ |
|
|
if (!config->fquant) |
|
|
{ |
|
|
config->fquant = |
|
|
((float) (config->max_pquant - config->min_pquant) / 100) * |
|
|
(100 - quality) + |
|
|
(float) config->min_pquant; |
|
|
|
|
|
fquant_running = config->fquant; |
|
|
} |
|
|
|
|
|
if (fquant_running < config->min_pquant) |
|
|
{ |
|
|
fquant_running = (float) config->min_pquant; |
|
|
} |
|
|
else if(fquant_running > config->max_pquant) |
|
|
{ |
|
|
fquant_running = (float) config->max_pquant; |
|
|
} |
|
|
|
|
|
quant = (int) fquant_running; |
|
|
|
|
|
// add error between fquant and quant to fquant_running |
|
|
fquant_running += config->fquant - quant; |
|
|
|
|
|
return quant; |
|
|
} |
|
|
|
|
|
// added by koepi for credits greyscale |
|
|
|
|
|
int check_greyscale_mode(CONFIG* config, xvid_enc_frame_t* frame, int framenum) |
|
|
|
|
|
{ |
|
|
|
|
|
if ((codec_is_in_credits(config, framenum)) && (config->mode!=DLG_MODE_CBR)) |
|
|
|
|
|
{ |
|
|
|
|
|
if (config->credits_greyscale) |
|
|
|
|
|
{ |
|
|
|
|
|
if ((frame->vop_flags && XVID_VOP_GREYSCALE)) // use only if not already in greyscale |
|
|
|
|
|
frame->vop_flags |= XVID_VOP_GREYSCALE; |
|
|
|
|
|
} else { |
|
|
|
|
|
if (!(frame->vop_flags && XVID_VOP_GREYSCALE)) // if movie is in greyscale, switch back |
|
|
|
|
|
frame->vop_flags |= XVID_VOP_GREYSCALE; |
|
|
|
|
|
} |
|
|
|
|
|
} else { |
|
|
|
|
|
if (config->greyscale) |
|
|
|
|
|
{ |
|
|
|
|
|
if ((frame->vop_flags && XVID_VOP_GREYSCALE)) // use only if not already in greyscale |
|
|
|
|
|
frame->vop_flags |= XVID_VOP_GREYSCALE; |
|
|
|
|
|
} else { |
|
|
|
|
|
if (!(frame->vop_flags && XVID_VOP_GREYSCALE)) // if credits is in greyscale, switch back |
|
|
|
|
|
frame->vop_flags |= XVID_VOP_GREYSCALE; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
return 0; |
|
|
|
|
|
} |
|
|
|
|
|
// end of koepi's addition |
|
|
|
|