49 |
|
|
50 |
#include <windows.h> |
#include <windows.h> |
51 |
#include <vfw.h> |
#include <vfw.h> |
52 |
|
#include <stdio.h> |
53 |
|
#include "vfwext.h" |
54 |
|
|
55 |
|
#include <xvid.h> |
56 |
|
#include "debug.h" |
57 |
#include "codec.h" |
#include "codec.h" |
58 |
#include "2pass.h" |
#include "status.h" |
59 |
|
|
60 |
int pmvfast_presets[7] = { |
HINSTANCE m_hdll; |
61 |
|
int (*xvid_global_func)(void *handle, int opt, void *param1, void *param2); |
62 |
|
int (*xvid_encore_func)(void *handle, int opt, void *param1, void *param2); |
63 |
|
int (*xvid_decore_func)(void *handle, int opt, void *param1, void *param2); |
64 |
|
|
65 |
|
xvid_plugin_func *xvid_plugin_single_func, |
66 |
|
*xvid_plugin_2pass1_func, |
67 |
|
*xvid_plugin_2pass2_func, |
68 |
|
*xvid_plugin_lumimasking_func, |
69 |
|
*xvid_plugin_psnr_func; |
70 |
|
|
71 |
|
|
72 |
|
|
73 |
|
static const int pmvfast_presets[7] = { |
74 |
0, 0, 0, 0, |
0, 0, 0, 0, |
75 |
0 | XVID_ME_HALFPELREFINE16 | 0, |
0 | XVID_ME_HALFPELREFINE16 | 0, |
76 |
0 | XVID_ME_HALFPELREFINE16 | 0 | |
0 | XVID_ME_HALFPELREFINE16 | 0 | |
78 |
XVID_ME_HALFPELREFINE8 | 0 | XVID_ME_USESQUARES16 |
XVID_ME_HALFPELREFINE8 | 0 | XVID_ME_USESQUARES16 |
79 |
}; |
}; |
80 |
|
|
81 |
|
|
82 |
|
|
83 |
/* return xvid compatbile colorspace, |
/* return xvid compatbile colorspace, |
84 |
or XVID_CSP_NULL if failure |
or XVID_CSP_NULL if failure |
85 |
*/ |
*/ |
86 |
|
|
87 |
int get_colorspace(BITMAPINFOHEADER * hdr) |
static int get_colorspace(BITMAPINFOHEADER * hdr) |
88 |
{ |
{ |
89 |
/* rgb only: negative height specifies top down image */ |
/* rgb only: negative height specifies top down image */ |
90 |
int rgb_flip = (hdr->biHeight < 0 ? 0 : XVID_CSP_VFLIP); |
int rgb_flip = (hdr->biHeight < 0 ? 0 : XVID_CSP_VFLIP); |
94 |
case BI_RGB : |
case BI_RGB : |
95 |
if (hdr->biBitCount == 16) |
if (hdr->biBitCount == 16) |
96 |
{ |
{ |
97 |
DEBUG("RGB16 (RGB555)"); |
DPRINTF("RGB16 (RGB555)"); |
98 |
return rgb_flip | XVID_CSP_RGB555; |
return rgb_flip | XVID_CSP_RGB555; |
99 |
} |
} |
100 |
if (hdr->biBitCount == 24) |
if (hdr->biBitCount == 24) |
101 |
{ |
{ |
102 |
DEBUG("RGB24"); |
DPRINTF("RGB24"); |
103 |
return rgb_flip | XVID_CSP_BGR; |
return rgb_flip | XVID_CSP_BGR; |
104 |
} |
} |
105 |
if (hdr->biBitCount == 32) |
if (hdr->biBitCount == 32) |
106 |
{ |
{ |
107 |
DEBUG("RGB32"); |
DPRINTF("RGB32"); |
108 |
return rgb_flip | XVID_CSP_BGRA; |
return rgb_flip | XVID_CSP_BGRA; |
109 |
} |
} |
110 |
|
|
111 |
DEBUG1("unsupported BI_RGB biBitCount", hdr->biBitCount); |
DPRINTF("unsupported BI_RGB biBitCount=%i", hdr->biBitCount); |
112 |
return XVID_CSP_NULL; |
return XVID_CSP_NULL; |
113 |
|
|
114 |
case BI_BITFIELDS : |
case BI_BITFIELDS : |
121 |
hdr4->bV4GreenMask == 0x3e0 && |
hdr4->bV4GreenMask == 0x3e0 && |
122 |
hdr4->bV4BlueMask == 0x1f) |
hdr4->bV4BlueMask == 0x1f) |
123 |
{ |
{ |
124 |
DEBUG("RGB555"); |
DPRINTF("RGB555"); |
125 |
return rgb_flip | XVID_CSP_RGB555; |
return rgb_flip | XVID_CSP_RGB555; |
126 |
} |
} |
127 |
|
|
130 |
hdr4->bV4GreenMask == 0x7e0 && |
hdr4->bV4GreenMask == 0x7e0 && |
131 |
hdr4->bV4BlueMask == 0x1f) |
hdr4->bV4BlueMask == 0x1f) |
132 |
{ |
{ |
133 |
DEBUG("RGB565"); |
DPRINTF("RGB565"); |
134 |
return rgb_flip | XVID_CSP_RGB565; |
return rgb_flip | XVID_CSP_RGB565; |
135 |
} |
} |
136 |
|
|
137 |
DEBUG("unsupported BI_BITFIELDS mode"); |
DPRINTF("unsupported BI_BITFIELDS mode"); |
138 |
return XVID_CSP_NULL; |
return XVID_CSP_NULL; |
139 |
} |
} |
140 |
|
|
141 |
DEBUG("unsupported BI_BITFIELDS/BITMAPHEADER combination"); |
DPRINTF("unsupported BI_BITFIELDS/BITMAPHEADER combination"); |
142 |
return XVID_CSP_NULL; |
return XVID_CSP_NULL; |
143 |
|
|
144 |
case FOURCC_I420 : |
case FOURCC_I420 : |
145 |
case FOURCC_IYUV : |
case FOURCC_IYUV : |
146 |
DEBUG("IYUY"); |
DPRINTF("IYUY"); |
147 |
return XVID_CSP_I420; |
return XVID_CSP_I420; |
148 |
|
|
149 |
case FOURCC_YV12 : |
case FOURCC_YV12 : |
150 |
DEBUG("YV12"); |
DPRINTF("YV12"); |
151 |
return XVID_CSP_YV12; |
return XVID_CSP_YV12; |
152 |
|
|
153 |
case FOURCC_YUYV : |
case FOURCC_YUYV : |
154 |
case FOURCC_YUY2 : |
case FOURCC_YUY2 : |
155 |
DEBUG("YUY2"); |
DPRINTF("YUY2"); |
156 |
return XVID_CSP_YUY2; |
return XVID_CSP_YUY2; |
157 |
|
|
158 |
case FOURCC_YVYU : |
case FOURCC_YVYU : |
159 |
DEBUG("YVYU"); |
DPRINTF("YVYU"); |
160 |
return XVID_CSP_YVYU; |
return XVID_CSP_YVYU; |
161 |
|
|
162 |
case FOURCC_UYVY : |
case FOURCC_UYVY : |
163 |
DEBUG("UYVY"); |
DPRINTF("UYVY"); |
164 |
return XVID_CSP_UYVY; |
return XVID_CSP_UYVY; |
165 |
|
|
166 |
default : |
default : |
167 |
DEBUGFOURCC("unsupported colorspace", hdr->biCompression); |
DPRINTF("unsupported colorspace %c%c%c%c", |
168 |
|
hdr->biCompression&0xff, |
169 |
|
(hdr->biCompression>>8)&0xff, |
170 |
|
(hdr->biCompression>>16)&0xff, |
171 |
|
(hdr->biCompression>>24)&0xff); |
172 |
return XVID_CSP_NULL; |
return XVID_CSP_NULL; |
173 |
} |
} |
174 |
} |
} |
184 |
BITMAPINFOHEADER * inhdr = &lpbiInput->bmiHeader; |
BITMAPINFOHEADER * inhdr = &lpbiInput->bmiHeader; |
185 |
BITMAPINFOHEADER * outhdr = &lpbiOutput->bmiHeader; |
BITMAPINFOHEADER * outhdr = &lpbiOutput->bmiHeader; |
186 |
|
|
187 |
|
/* VFWEXT detection */ |
188 |
|
if (inhdr->biCompression == VFWEXT_FOURCC) { |
189 |
|
return (ICM_USER+0x0fff); |
190 |
|
} |
191 |
|
|
192 |
if (get_colorspace(inhdr) == XVID_CSP_NULL) |
if (get_colorspace(inhdr) == XVID_CSP_NULL) |
193 |
{ |
{ |
194 |
return ICERR_BADFORMAT; |
return ICERR_BADFORMAT; |
221 |
|
|
222 |
if (lpbiOutput == NULL) |
if (lpbiOutput == NULL) |
223 |
{ |
{ |
224 |
return sizeof(BITMAPV4HEADER); |
return sizeof(BITMAPINFOHEADER); |
225 |
} |
} |
226 |
|
|
227 |
memcpy(outhdr, inhdr, sizeof(BITMAPINFOHEADER)); |
memcpy(outhdr, inhdr, sizeof(BITMAPINFOHEADER)); |
257 |
|
|
258 |
LRESULT compress_frames_info(CODEC * codec, ICCOMPRESSFRAMES * icf) |
LRESULT compress_frames_info(CODEC * codec, ICCOMPRESSFRAMES * icf) |
259 |
{ |
{ |
260 |
// DEBUG2("frate fscale", codec->frate, codec->fscale); |
#if 0 |
261 |
|
DPRINTF("%i %i", icf->lStartFrame, icf->lFrameCount); |
262 |
|
#endif |
263 |
codec->fincr = icf->dwScale; |
codec->fincr = icf->dwScale; |
264 |
codec->fbase = icf->dwRate; |
codec->fbase = icf->dwRate; |
265 |
return ICERR_OK; |
return ICERR_OK; |
266 |
} |
} |
267 |
|
|
268 |
|
|
269 |
|
static char type2char(int type) |
270 |
|
{ |
271 |
|
if (type==XVID_TYPE_IVOP) |
272 |
|
return 'I'; |
273 |
|
if (type==XVID_TYPE_PVOP) |
274 |
|
return 'P'; |
275 |
|
if (type==XVID_TYPE_BVOP) |
276 |
|
return 'B'; |
277 |
|
return 'S'; |
278 |
|
} |
279 |
|
|
280 |
|
static int vfw_debug(void *handle, |
281 |
|
int opt, |
282 |
|
void *param1, |
283 |
|
void *param2) |
284 |
|
{ |
285 |
|
switch (opt) { |
286 |
|
case XVID_PLG_CREATE: |
287 |
|
*((void**)param2) = NULL; |
288 |
|
case XVID_PLG_INFO: |
289 |
|
case XVID_PLG_DESTROY: |
290 |
|
case XVID_PLG_BEFORE: |
291 |
|
return 0; |
292 |
|
|
293 |
|
case XVID_PLG_AFTER: |
294 |
|
{ |
295 |
|
xvid_plg_data_t *data = (xvid_plg_data_t *) param1; |
296 |
|
|
297 |
|
/* We don't use DPRINTF here because it's active only for _DEBUG |
298 |
|
* builds and that activates lot of other debug printfs. We only |
299 |
|
* want these all the time */ |
300 |
|
char buf[1024]; |
301 |
|
sprintf(buf, "[%6i] type=%c Q:%2i length:%6i", |
302 |
|
data->frame_num, |
303 |
|
type2char(data->type), |
304 |
|
data->quant, |
305 |
|
data->length); |
306 |
|
OutputDebugString(buf); |
307 |
|
|
308 |
|
return 0; |
309 |
|
} |
310 |
|
} |
311 |
|
|
312 |
|
return XVID_ERR_FAIL; |
313 |
|
} |
314 |
|
|
315 |
|
#define XVID_DLL_NAME "xvidcore.dll" |
316 |
|
|
317 |
|
static int init_dll() |
318 |
|
{ |
319 |
|
if (m_hdll != NULL) return 0; |
320 |
|
|
321 |
|
DPRINTF("init_dll"); |
322 |
|
m_hdll = LoadLibrary(XVID_DLL_NAME); |
323 |
|
if (m_hdll == NULL) { |
324 |
|
DPRINTF("dll load failed"); |
325 |
|
MessageBox(0, XVID_DLL_NAME " not found!","Error!", MB_ICONEXCLAMATION|MB_OK); |
326 |
|
return XVID_ERR_FAIL; |
327 |
|
} |
328 |
|
|
329 |
|
xvid_global_func = (int (__cdecl *)(void *, int, void *, void *))GetProcAddress(m_hdll, "xvid_global"); |
330 |
|
if (xvid_global_func == NULL) { |
331 |
|
MessageBox(0, "xvid_global() not found", "Error", 0); |
332 |
|
return XVID_ERR_FAIL; |
333 |
|
} |
334 |
|
|
335 |
|
xvid_encore_func = (int (__cdecl *)(void *, int, void *, void *))GetProcAddress(m_hdll, "xvid_encore"); |
336 |
|
if (xvid_encore_func == NULL) { |
337 |
|
MessageBox(0, "xvid_encore() not found", "Error", 0); |
338 |
|
return XVID_ERR_FAIL; |
339 |
|
} |
340 |
|
|
341 |
|
xvid_decore_func = (int (__cdecl *)(void *, int, void *, void *))GetProcAddress(m_hdll, "xvid_decore"); |
342 |
|
if (xvid_decore_func == NULL) { |
343 |
|
MessageBox(0, "xvid_decore() not found", "Error", 0); |
344 |
|
return XVID_ERR_FAIL; |
345 |
|
} |
346 |
|
|
347 |
|
xvid_plugin_single_func = |
348 |
|
(int (__cdecl *)(void *, int, void *, void *))(GetProcAddress(m_hdll, "xvid_plugin_single")); |
349 |
|
xvid_plugin_2pass1_func = |
350 |
|
(int (__cdecl *)(void *, int, void *, void *))(GetProcAddress(m_hdll, "xvid_plugin_2pass1")); |
351 |
|
xvid_plugin_2pass2_func = |
352 |
|
(int (__cdecl *)(void *, int, void *, void *))(GetProcAddress(m_hdll, "xvid_plugin_2pass2")); |
353 |
|
xvid_plugin_lumimasking_func = |
354 |
|
(int (__cdecl *)(void *, int, void *, void *))(GetProcAddress(m_hdll, "xvid_plugin_lumimasking")); |
355 |
|
xvid_plugin_psnr_func = |
356 |
|
(int (__cdecl *)(void *, int, void *, void *))(GetProcAddress(m_hdll, "xvid_plugin_psnr")); |
357 |
|
|
358 |
|
return 0; |
359 |
|
} |
360 |
|
|
361 |
|
/* constant-quant zones for fixed quant encoding */ |
362 |
|
static void |
363 |
|
prepare_cquant_zones(CONFIG * config) { |
364 |
|
|
365 |
|
int i = 0; |
366 |
|
if (config->num_zones == 0 || config->zones[0].frame != 0) { |
367 |
|
/* first zone does not start at frame 0 or doesn't exist */ |
368 |
|
|
369 |
|
if (config->num_zones >= MAX_ZONES) config->num_zones--; /* we scrifice last zone */ |
370 |
|
|
371 |
|
config->zones[config->num_zones].frame = 0; |
372 |
|
config->zones[config->num_zones].mode = RC_ZONE_QUANT; |
373 |
|
config->zones[config->num_zones].weight = 100; |
374 |
|
config->zones[config->num_zones].quant = config->desired_quant; |
375 |
|
config->zones[config->num_zones].type = XVID_TYPE_AUTO; |
376 |
|
config->zones[config->num_zones].greyscale = 0; |
377 |
|
config->zones[config->num_zones].chroma_opt = 0; |
378 |
|
config->zones[config->num_zones].bvop_threshold = 0; |
379 |
|
config->num_zones++; |
380 |
|
|
381 |
|
sort_zones(config->zones, config->num_zones, &i); |
382 |
|
} |
383 |
|
|
384 |
|
/* step 2: let's change all weight zones into quant zones */ |
385 |
|
|
386 |
|
for(i = 0; i < config->num_zones; i++) |
387 |
|
if (config->zones[i].mode == RC_ZONE_WEIGHT) { |
388 |
|
config->zones[i].mode = RC_ZONE_QUANT; |
389 |
|
config->zones[i].quant = (100*config->desired_quant) / config->zones[i].weight; |
390 |
|
} |
391 |
|
} |
392 |
|
|
393 |
|
/* full first pass zones */ |
394 |
|
static void |
395 |
|
prepare_full1pass_zones(CONFIG * config) { |
396 |
|
|
397 |
|
int i = 0; |
398 |
|
if (config->num_zones == 0 || config->zones[0].frame != 0) { |
399 |
|
/* first zone does not start at frame 0 or doesn't exist */ |
400 |
|
|
401 |
|
if (config->num_zones >= MAX_ZONES) config->num_zones--; /* we scrifice last zone */ |
402 |
|
|
403 |
|
config->zones[config->num_zones].frame = 0; |
404 |
|
config->zones[config->num_zones].mode = RC_ZONE_QUANT; |
405 |
|
config->zones[config->num_zones].weight = 100; |
406 |
|
config->zones[config->num_zones].quant = 200; |
407 |
|
config->zones[config->num_zones].type = XVID_TYPE_AUTO; |
408 |
|
config->zones[config->num_zones].greyscale = 0; |
409 |
|
config->zones[config->num_zones].chroma_opt = 0; |
410 |
|
config->zones[config->num_zones].bvop_threshold = 0; |
411 |
|
config->num_zones++; |
412 |
|
|
413 |
|
sort_zones(config->zones, config->num_zones, &i); |
414 |
|
} |
415 |
|
|
416 |
|
/* step 2: let's change all weight zones into quant zones */ |
417 |
|
|
418 |
|
for(i = 0; i < config->num_zones; i++) |
419 |
|
if (config->zones[i].mode == RC_ZONE_WEIGHT) { |
420 |
|
config->zones[i].mode = RC_ZONE_QUANT; |
421 |
|
config->zones[i].quant = 200; |
422 |
|
} |
423 |
|
} |
424 |
|
|
425 |
|
|
426 |
LRESULT compress_begin(CODEC * codec, BITMAPINFO * lpbiInput, BITMAPINFO * lpbiOutput) |
LRESULT compress_begin(CODEC * codec, BITMAPINFO * lpbiInput, BITMAPINFO * lpbiOutput) |
427 |
{ |
{ |
428 |
xvid_gbl_init_t init; |
xvid_gbl_init_t init; |
429 |
xvid_enc_create_t create; |
xvid_enc_create_t create; |
430 |
xvid_enc_rc_t rc; |
xvid_enc_plugin_t plugins[3]; |
431 |
xvid_enc_plugin_t plugins[1]; |
xvid_plugin_single_t single; |
432 |
|
xvid_plugin_2pass1_t pass1; |
433 |
|
xvid_plugin_2pass2_t pass2; |
434 |
|
int i; |
435 |
|
HANDLE hFile; |
436 |
|
|
437 |
memset(&rc, 0, sizeof(rc)); |
CONFIG tmpCfg; /* if we want to alter config to suit our needs, it shouldn't be visible to user later */ |
438 |
rc.version = XVID_VERSION; |
memcpy(&tmpCfg, &codec->config, sizeof(CONFIG)); |
439 |
|
|
440 |
|
if (init_dll() != 0) return ICERR_ERROR; |
441 |
|
/* destroy previously created codec */ |
442 |
|
if(codec->ehandle) { |
443 |
|
xvid_encore_func(codec->ehandle, XVID_ENC_DESTROY, NULL, NULL); |
444 |
|
codec->ehandle = NULL; |
445 |
|
} |
446 |
|
|
447 |
|
memset(&init, 0, sizeof(init)); |
448 |
|
init.version = XVID_VERSION; |
449 |
|
init.cpu_flags = codec->config.cpu; |
450 |
|
init.debug = codec->config.debug; |
451 |
|
xvid_global_func(0, XVID_GBL_INIT, &init, NULL); |
452 |
|
|
453 |
|
memset(&create, 0, sizeof(create)); |
454 |
|
create.version = XVID_VERSION; |
455 |
|
|
456 |
|
/* plugins */ |
457 |
|
create.plugins = plugins; |
458 |
switch (codec->config.mode) |
switch (codec->config.mode) |
459 |
{ |
{ |
460 |
case DLG_MODE_CBR : |
case RC_MODE_1PASS : |
461 |
//rc.type = XVID_RC_CBR; |
memset(&single, 0, sizeof(single)); |
462 |
rc.min_iquant = codec->config.min_iquant; |
single.version = XVID_VERSION; |
463 |
rc.max_iquant = codec->config.max_iquant; |
single.bitrate = codec->config.bitrate * CONFIG_KBPS; |
464 |
rc.min_pquant = codec->config.min_pquant; |
single.reaction_delay_factor = codec->config.rc_reaction_delay_factor; |
465 |
rc.max_pquant = codec->config.max_pquant; |
single.averaging_period = codec->config.rc_averaging_period; |
466 |
rc.min_bquant = 0; /* XXX: todo */ |
single.buffer = codec->config.rc_buffer; |
467 |
rc.max_bquant = 0; /* XXX: todo */ |
plugins[create.num_plugins].func = xvid_plugin_single_func; |
468 |
rc.data.cbr.bitrate = codec->config.rc_bitrate; |
plugins[create.num_plugins].param = &single; |
469 |
rc.data.cbr.reaction_delay_factor = codec->config.rc_reaction_delay_factor; |
create.num_plugins++; |
470 |
rc.data.cbr.averaging_period = codec->config.rc_averaging_period; |
if (!codec->config.use_2pass_bitrate) /* constant-quant mode */ |
471 |
rc.data.cbr.buffer = codec->config.rc_buffer; |
prepare_cquant_zones(&tmpCfg); |
472 |
break; |
break; |
473 |
|
|
474 |
case DLG_MODE_VBR_QUAL : |
case RC_MODE_2PASS1 : |
475 |
codec->config.fquant = 0; |
memset(&pass1, 0, sizeof(pass1)); |
476 |
//param.rc_bitrate = 0; |
pass1.version = XVID_VERSION; |
477 |
|
pass1.filename = codec->config.stats; |
478 |
|
if (codec->config.full1pass) |
479 |
|
prepare_full1pass_zones(&tmpCfg); |
480 |
|
plugins[create.num_plugins].func = xvid_plugin_2pass1_func; |
481 |
|
plugins[create.num_plugins].param = &pass1; |
482 |
|
create.num_plugins++; |
483 |
break; |
break; |
484 |
|
|
485 |
case DLG_MODE_VBR_QUANT : |
case RC_MODE_2PASS2 : |
486 |
codec->config.fquant = (float) codec->config.quant; |
memset(&pass2, 0, sizeof(pass2)); |
487 |
//param.rc_bitrate = 0; |
pass2.version = XVID_VERSION; |
488 |
break; |
if (codec->config.use_2pass_bitrate) { |
489 |
|
pass2.bitrate = codec->config.bitrate * CONFIG_KBPS; |
490 |
|
} else { |
491 |
|
pass2.bitrate = -codec->config.desired_size; /* kilobytes */ |
492 |
|
} |
493 |
|
pass2.filename = codec->config.stats; |
494 |
|
|
495 |
case DLG_MODE_2PASS_1 : |
hFile = CreateFile(pass2.filename, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL); |
496 |
case DLG_MODE_2PASS_2_INT : |
if (hFile == INVALID_HANDLE_VALUE) |
497 |
case DLG_MODE_2PASS_2_EXT : |
{ |
498 |
//param.rc_bitrate = 0; |
MessageBox(0, "Statsfile not found!","Error!", MB_ICONEXCLAMATION|MB_OK); |
499 |
codec->twopass.max_framesize = (int)((double)codec->config.twopass_max_bitrate / 8.0 / ((double)codec->fbase / (double)codec->fincr)); |
return XVID_ERR_FAIL; |
500 |
|
} else |
501 |
|
{ |
502 |
|
CloseHandle(hFile); |
503 |
|
} |
504 |
|
|
505 |
|
pass2.keyframe_boost = codec->config.keyframe_boost; /* keyframe boost percentage: [0..100...]; */ |
506 |
|
pass2.curve_compression_high = codec->config.curve_compression_high; |
507 |
|
pass2.curve_compression_low = codec->config.curve_compression_low; |
508 |
|
pass2.overflow_control_strength = codec->config.overflow_control_strength; |
509 |
|
pass2.max_overflow_improvement = codec->config.twopass_max_overflow_improvement; |
510 |
|
pass2.max_overflow_degradation = codec->config.twopass_max_overflow_degradation; |
511 |
|
pass2.kfreduction = codec->config.kfreduction; |
512 |
|
pass2.kfthreshold = codec->config.kfthreshold; |
513 |
|
pass2.container_frame_overhead = 24; /* AVI */ |
514 |
|
|
515 |
|
plugins[create.num_plugins].func = xvid_plugin_2pass2_func; |
516 |
|
plugins[create.num_plugins].param = &pass2; |
517 |
|
create.num_plugins++; |
518 |
break; |
break; |
519 |
|
|
520 |
case DLG_MODE_NULL : |
case RC_MODE_NULL : |
521 |
return ICERR_OK; |
return ICERR_OK; |
522 |
|
|
523 |
default : |
default : |
524 |
break; |
break; |
525 |
} |
} |
526 |
|
|
527 |
/* destroy previously created codec */ |
/* zones - copy from tmpCfg in case we automatically altered them above */ |
528 |
if(codec->ehandle) |
create.zones = malloc(sizeof(xvid_enc_zone_t) * tmpCfg.num_zones); |
529 |
{ |
create.num_zones = tmpCfg.num_zones; |
530 |
xvid_encore(codec->ehandle, XVID_ENC_DESTROY, NULL, NULL); |
for (i=0; i < create.num_zones; i++) { |
531 |
codec->ehandle = NULL; |
create.zones[i].frame = tmpCfg.zones[i].frame; |
532 |
|
if (tmpCfg.zones[i].mode == RC_ZONE_QUANT) { |
533 |
|
create.zones[i].mode = XVID_ZONE_QUANT; |
534 |
|
create.zones[i].increment = tmpCfg.zones[i].quant; |
535 |
|
}else{ |
536 |
|
create.zones[i].mode = XVID_ZONE_WEIGHT; |
537 |
|
create.zones[i].increment = tmpCfg.zones[i].weight; |
538 |
|
} |
539 |
|
create.zones[i].base = 100; |
540 |
} |
} |
541 |
|
|
542 |
memset(&init, 0, sizeof(init)); |
/* lumimasking plugin */ |
543 |
init.version = XVID_VERSION; |
if ((profiles[codec->config.profile].flags & PROFILE_ADAPTQUANT) && codec->config.lum_masking) { |
544 |
init.cpu_flags = codec->config.cpu; |
plugins[create.num_plugins].func = xvid_plugin_lumimasking_func; |
545 |
xvid_global(0, XVID_GBL_INIT, &init, NULL); |
plugins[create.num_plugins].param = NULL; |
546 |
|
create.num_plugins++; |
547 |
|
} |
548 |
|
|
549 |
memset(&create, 0, sizeof(create)); |
plugins[create.num_plugins].func = vfw_debug; |
550 |
create.version = XVID_VERSION; |
plugins[create.num_plugins].param = NULL; |
551 |
|
create.num_plugins++; |
552 |
|
|
553 |
|
create.profile = profiles[codec->config.profile].id; |
554 |
|
|
555 |
create.width = lpbiInput->bmiHeader.biWidth; |
create.width = lpbiInput->bmiHeader.biWidth; |
556 |
create.height = lpbiInput->bmiHeader.biHeight; |
create.height = lpbiInput->bmiHeader.biHeight; |
557 |
create.fincr = codec->fincr; |
create.fincr = codec->fincr; |
558 |
create.fbase = codec->fbase; |
create.fbase = codec->fbase; |
559 |
|
|
560 |
create.plugins = plugins; |
create.max_key_interval = codec->config.max_key_interval; |
561 |
create.num_plugins = 0; |
|
562 |
if (codec->config.lum_masking) { |
create.min_quant[0] = codec->config.min_iquant; |
563 |
plugins[create.num_plugins].func = xvid_plugin_lumimasking; |
create.max_quant[0] = codec->config.max_iquant; |
564 |
plugins[create.num_plugins].param = NULL; |
create.min_quant[1] = codec->config.min_pquant; |
565 |
create.num_plugins++; |
create.max_quant[1] = codec->config.max_pquant; |
566 |
} |
create.min_quant[2] = codec->config.min_bquant; |
567 |
|
create.max_quant[2] = codec->config.max_bquant; |
568 |
|
|
569 |
|
if ((profiles[codec->config.profile].flags & PROFILE_BVOP) && codec->config.use_bvop) { |
570 |
|
create.max_bframes = codec->config.max_bframes; |
571 |
|
create.bquant_ratio = codec->config.bquant_ratio; |
572 |
|
create.bquant_offset = codec->config.bquant_offset; |
573 |
|
|
574 |
if (codec->config.packed) |
if (codec->config.packed) |
575 |
create.global |= XVID_GLOBAL_PACKED; |
create.global |= XVID_GLOBAL_PACKED; |
576 |
if (codec->config.dx50bvop) |
|
577 |
|
if (codec->config.closed_gov) |
578 |
create.global |= XVID_GLOBAL_CLOSED_GOP; |
create.global |= XVID_GLOBAL_CLOSED_GOP; |
579 |
|
|
580 |
create.max_key_interval = codec->config.max_key_interval; |
} |
|
/* XXX: param.min_quantizer = codec->config.min_pquant; |
|
|
param.max_quantizer = codec->config.max_pquant; */ |
|
581 |
|
|
|
create.max_bframes = codec->config.max_bframes; |
|
582 |
create.frame_drop_ratio = codec->config.frame_drop_ratio; |
create.frame_drop_ratio = codec->config.frame_drop_ratio; |
583 |
|
|
|
create.bquant_ratio = codec->config.bquant_ratio; |
|
|
create.bquant_offset = codec->config.bquant_offset; |
|
|
|
|
584 |
create.num_threads = codec->config.num_threads; |
create.num_threads = codec->config.num_threads; |
585 |
|
|
586 |
switch(xvid_encore(0, XVID_ENC_CREATE, &create, (codec->config.mode==DLG_MODE_CBR)?&rc:NULL )) |
switch(xvid_encore_func(0, XVID_ENC_CREATE, &create, NULL)) |
587 |
{ |
{ |
588 |
case XVID_ERR_FAIL : |
case XVID_ERR_FAIL : |
589 |
return ICERR_ERROR; |
return ICERR_ERROR; |
602 |
codec->framenum = 0; |
codec->framenum = 0; |
603 |
codec->keyspacing = 0; |
codec->keyspacing = 0; |
604 |
|
|
605 |
codec->twopass.hints = codec->twopass.stats1 = codec->twopass.stats2 = INVALID_HANDLE_VALUE; |
if (codec->config.display_status) { |
606 |
codec->twopass.hintstream = NULL; |
status_destroy_always(&codec->status); |
607 |
|
status_create(&codec->status, codec->fincr, codec->fbase); |
608 |
|
} |
609 |
|
|
610 |
return ICERR_OK; |
return ICERR_OK; |
611 |
} |
} |
613 |
|
|
614 |
LRESULT compress_end(CODEC * codec) |
LRESULT compress_end(CODEC * codec) |
615 |
{ |
{ |
616 |
if (codec->ehandle != NULL) |
if (m_hdll != NULL) { |
617 |
{ |
if (codec->ehandle != NULL) { |
618 |
xvid_encore(codec->ehandle, XVID_ENC_DESTROY, NULL, NULL); |
xvid_encore_func(codec->ehandle, XVID_ENC_DESTROY, NULL, NULL); |
619 |
|
codec->ehandle = NULL; |
|
if (codec->twopass.hints != INVALID_HANDLE_VALUE) |
|
|
{ |
|
|
CloseHandle(codec->twopass.hints); |
|
620 |
} |
} |
621 |
if (codec->twopass.stats1 != INVALID_HANDLE_VALUE) |
FreeLibrary(m_hdll); |
622 |
{ |
m_hdll = NULL; |
|
CloseHandle(codec->twopass.stats1); |
|
623 |
} |
} |
624 |
if (codec->twopass.stats2 != INVALID_HANDLE_VALUE) |
|
625 |
{ |
if (codec->config.display_status) |
626 |
CloseHandle(codec->twopass.stats2); |
status_destroy(&codec->status); |
627 |
|
|
628 |
|
return ICERR_OK; |
629 |
} |
} |
630 |
if (codec->twopass.hintstream != NULL) |
|
631 |
|
|
632 |
|
static void apply_zone_modifiers(xvid_enc_frame_t * frame, CONFIG * config, int framenum) |
633 |
{ |
{ |
634 |
free(codec->twopass.hintstream); |
int i; |
|
} |
|
635 |
|
|
636 |
codec->ehandle = NULL; |
for (i=0; i<config->num_zones && config->zones[i].frame <= framenum; i++) ; |
637 |
|
|
638 |
|
if (--i < 0) return; /* there are no zones, or we're before the first zone */ |
639 |
|
|
640 |
|
if (framenum == config->zones[i].frame) |
641 |
|
frame->type = config->zones[i].type; |
642 |
|
|
643 |
codec_2pass_finish(codec); |
if (config->zones[i].greyscale) { |
644 |
|
frame->vop_flags |= XVID_VOP_GREYSCALE; |
645 |
} |
} |
646 |
|
|
647 |
return ICERR_OK; |
if (config->zones[i].chroma_opt) { |
648 |
|
frame->vop_flags |= XVID_VOP_CHROMAOPT; |
649 |
} |
} |
650 |
|
|
651 |
|
if ((profiles[config->profile].flags & PROFILE_BVOP) && config->use_bvop) { |
652 |
|
frame->bframe_threshold = config->zones[i].bvop_threshold; |
653 |
|
} |
654 |
|
} |
655 |
|
|
656 |
|
|
657 |
LRESULT compress(CODEC * codec, ICCOMPRESS * icc) |
LRESULT compress(CODEC * codec, ICCOMPRESS * icc) |
658 |
{ |
{ |
659 |
BITMAPINFOHEADER * inhdr = icc->lpbiInput; |
BITMAPINFOHEADER * inhdr = icc->lpbiInput; |
662 |
xvid_enc_stats_t stats; |
xvid_enc_stats_t stats; |
663 |
int length; |
int length; |
664 |
|
|
|
// 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; |
|
|
} |
|
|
} |
|
|
|
|
665 |
memset(&frame, 0, sizeof(frame)); |
memset(&frame, 0, sizeof(frame)); |
666 |
frame.version = XVID_VERSION; |
frame.version = XVID_VERSION; |
667 |
|
|
669 |
|
|
670 |
/* vol stuff */ |
/* vol stuff */ |
671 |
|
|
672 |
if (codec->config.quant_type != QUANT_MODE_H263) |
if ((profiles[codec->config.profile].flags & PROFILE_MPEGQUANT) && |
673 |
|
codec->config.quant_type != QUANT_MODE_H263) |
674 |
{ |
{ |
675 |
frame.vol_flags |= XVID_VOL_MPEGQUANT; |
frame.vol_flags |= XVID_VOL_MPEGQUANT; |
676 |
|
|
677 |
// we actually need "default/custom" selectbox for both inter/intra |
if (codec->config.quant_type == QUANT_MODE_CUSTOM) { |
|
// this will do for now |
|
|
if (codec->config.quant_type == QUANT_MODE_CUSTOM) |
|
|
{ |
|
678 |
frame.quant_intra_matrix = codec->config.qmatrix_intra; |
frame.quant_intra_matrix = codec->config.qmatrix_intra; |
679 |
frame.quant_inter_matrix = codec->config.qmatrix_inter; |
frame.quant_inter_matrix = codec->config.qmatrix_inter; |
680 |
} |
}else{ |
|
else |
|
|
{ |
|
681 |
frame.quant_intra_matrix = NULL; |
frame.quant_intra_matrix = NULL; |
682 |
frame.quant_inter_matrix = NULL; |
frame.quant_inter_matrix = NULL; |
683 |
} |
} |
684 |
} |
} |
685 |
|
|
686 |
if (codec->config.reduced_resolution) { |
if ((profiles[codec->config.profile].flags & PROFILE_REDUCED) && |
687 |
|
codec->config.reduced_resolution) { |
688 |
frame.vol_flags |= XVID_VOL_REDUCED_ENABLE; |
frame.vol_flags |= XVID_VOL_REDUCED_ENABLE; |
689 |
frame.vop_flags |= XVID_VOP_REDUCED; /* XXX: need auto decion mode */ |
frame.vop_flags |= XVID_VOP_REDUCED; /* XXX: need auto decion mode */ |
690 |
} |
} |
691 |
|
|
692 |
if (codec->config.qpel) { |
if ((profiles[codec->config.profile].flags & PROFILE_QPEL) && codec->config.qpel) { |
693 |
frame.vol_flags |= XVID_VOL_QUARTERPEL; |
frame.vol_flags |= XVID_VOL_QUARTERPEL; |
694 |
frame.motion |= XVID_ME_QUARTERPELREFINE16 | XVID_ME_QUARTERPELREFINE8; |
frame.motion |= XVID_ME_QUARTERPELREFINE16 | XVID_ME_QUARTERPELREFINE8; |
695 |
} |
} |
696 |
|
|
697 |
if (codec->config.gmc) |
if ((profiles[codec->config.profile].flags & PROFILE_GMC) && codec->config.gmc) { |
698 |
frame.vol_flags |= XVID_VOL_GMC; |
frame.vol_flags |= XVID_VOL_GMC; |
699 |
|
frame.motion |= XVID_ME_GME_REFINE; |
700 |
|
} |
701 |
|
|
702 |
if (codec->config.interlacing) |
if ((profiles[codec->config.profile].flags & PROFILE_INTERLACE) && codec->config.interlacing) |
703 |
frame.vol_flags |= XVID_VOL_INTERLACING; |
frame.vol_flags |= XVID_VOL_INTERLACING; |
704 |
|
|
705 |
|
if (codec->config.ar_mode == 0) { /* PAR */ |
706 |
|
if (codec->config.display_aspect_ratio != 5) { |
707 |
|
frame.par = codec->config.display_aspect_ratio + 1; |
708 |
|
} else { |
709 |
|
frame.par = XVID_PAR_EXT; |
710 |
|
frame.par_width = codec->config.par_x; |
711 |
|
frame.par_height= codec->config.par_y; |
712 |
|
} |
713 |
|
} else { /* AR */ |
714 |
|
/* custom pixel aspect ratio -> calculated from DAR */ |
715 |
|
frame.par = XVID_PAR_EXT; |
716 |
|
frame.par_width = (100 * inhdr->biHeight) / codec->config.ar_y; |
717 |
|
frame.par_height= (100 * inhdr->biWidth) / codec->config.ar_x; |
718 |
|
} |
719 |
|
|
720 |
/* vop stuff */ |
/* vop stuff */ |
721 |
|
|
722 |
frame.vop_flags |= XVID_VOP_HALFPEL; |
frame.vop_flags |= XVID_VOP_HALFPEL; |
723 |
frame.vop_flags |= XVID_VOP_HQACPRED; |
frame.vop_flags |= XVID_VOP_HQACPRED; |
724 |
|
|
725 |
if (codec->config.debug) |
if (codec->config.vop_debug) |
726 |
frame.vop_flags |= XVID_VOP_DEBUG; |
frame.vop_flags |= XVID_VOP_DEBUG; |
727 |
|
|
728 |
|
if (codec->config.trellis_quant) { |
729 |
|
frame.vop_flags |= XVID_VOP_TRELLISQUANT; |
730 |
|
} |
731 |
|
|
732 |
if (codec->config.motion_search > 4) |
if (codec->config.motion_search > 4) |
733 |
frame.vop_flags |= XVID_VOP_INTER4V; |
frame.vop_flags |= XVID_VOP_INTER4V; |
734 |
|
|
735 |
if (codec->config.chromame) |
if (codec->config.chromame) |
736 |
frame.vop_flags |= XVID_ME_CHROMA16 + XVID_ME_CHROMA8; |
frame.motion |= XVID_ME_CHROMA_PVOP + XVID_ME_CHROMA_BVOP; |
|
|
|
|
if (codec->config.chroma_opt) |
|
|
frame.vop_flags |= XVID_VOP_CHROMAOPT; |
|
|
|
|
|
check_greyscale_mode(&codec->config, &frame, codec->framenum); |
|
|
|
|
|
/* XXX: hinted me |
|
737 |
|
|
738 |
// fix 1pass modes/hinted MV by koepi |
if (codec->config.cartoon_mode) { |
739 |
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)) |
frame.vop_flags |= XVID_VOP_CARTOON; |
740 |
{ |
frame.motion |= XVID_ME_DETECT_STATIC_MOTION; |
|
codec->config.hinted_me = 0; |
|
741 |
} |
} |
|
// 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; |
|
742 |
|
|
743 |
frame.hint.hintstream = codec->twopass.hintstream; |
if (codec->config.turbo) |
744 |
frame.hint.rawhints = 0; |
frame.motion |= XVID_ME_FASTREFINE16 | XVID_ME_FASTREFINE8 | |
745 |
frame.general |= XVID_HINTEDME_SET; |
XVID_ME_SKIP_DELTASEARCH | XVID_ME_FAST_MODEINTERPOLATE | |
746 |
|
XVID_ME_BFRAME_EARLYSTOP; |
|
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; |
|
|
} |
|
|
} */ |
|
747 |
|
|
748 |
frame.motion |= pmvfast_presets[codec->config.motion_search]; |
frame.motion |= pmvfast_presets[codec->config.motion_search]; |
749 |
|
|
750 |
switch (codec->config.vhq_mode) |
switch (codec->config.vhq_mode) |
751 |
{ |
{ |
752 |
case VHQ_MODE_DECISION : |
case VHQ_MODE_DECISION : |
753 |
frame.vop_flags |= XVID_VOP_MODEDECISION_BITS; |
frame.vop_flags |= XVID_VOP_MODEDECISION_RD; |
754 |
break; |
break; |
755 |
|
|
756 |
case VHQ_LIMITED_SEARCH : |
case VHQ_LIMITED_SEARCH : |
757 |
frame.vop_flags |= XVID_VOP_MODEDECISION_BITS; |
frame.vop_flags |= XVID_VOP_MODEDECISION_RD; |
758 |
frame.motion |= XVID_ME_HALFPELREFINE16_BITS; |
frame.motion |= XVID_ME_HALFPELREFINE16_RD; |
759 |
frame.motion |= XVID_ME_QUARTERPELREFINE16_BITS; |
frame.motion |= XVID_ME_QUARTERPELREFINE16_RD; |
760 |
break; |
break; |
761 |
|
|
762 |
case VHQ_MEDIUM_SEARCH : |
case VHQ_MEDIUM_SEARCH : |
763 |
frame.vop_flags |= XVID_VOP_MODEDECISION_BITS; |
frame.vop_flags |= XVID_VOP_MODEDECISION_RD; |
764 |
frame.motion |= XVID_ME_HALFPELREFINE16_BITS; |
frame.motion |= XVID_ME_HALFPELREFINE16_RD; |
765 |
frame.motion |= XVID_ME_HALFPELREFINE8_BITS; |
frame.motion |= XVID_ME_HALFPELREFINE8_RD; |
766 |
frame.motion |= XVID_ME_QUARTERPELREFINE16_BITS; |
frame.motion |= XVID_ME_QUARTERPELREFINE16_RD; |
767 |
frame.motion |= XVID_ME_QUARTERPELREFINE8_BITS; |
frame.motion |= XVID_ME_QUARTERPELREFINE8_RD; |
768 |
frame.motion |= XVID_ME_CHECKPREDICTION_BITS; |
frame.motion |= XVID_ME_CHECKPREDICTION_RD; |
769 |
break; |
break; |
770 |
|
|
771 |
case VHQ_WIDE_SEARCH : |
case VHQ_WIDE_SEARCH : |
772 |
frame.vop_flags |= XVID_VOP_MODEDECISION_BITS; |
frame.vop_flags |= XVID_VOP_MODEDECISION_RD; |
773 |
frame.motion |= XVID_ME_HALFPELREFINE16_BITS; |
frame.motion |= XVID_ME_HALFPELREFINE16_RD; |
774 |
frame.motion |= XVID_ME_HALFPELREFINE8_BITS; |
frame.motion |= XVID_ME_HALFPELREFINE8_RD; |
775 |
frame.motion |= XVID_ME_QUARTERPELREFINE16_BITS; |
frame.motion |= XVID_ME_QUARTERPELREFINE16_RD; |
776 |
frame.motion |= XVID_ME_QUARTERPELREFINE8_BITS; |
frame.motion |= XVID_ME_QUARTERPELREFINE8_RD; |
777 |
frame.motion |= XVID_ME_CHECKPREDICTION_BITS; |
frame.motion |= XVID_ME_CHECKPREDICTION_RD; |
778 |
frame.motion |= XVID_ME_EXTSEARCH_BITS; |
frame.motion |= XVID_ME_EXTSEARCH_RD; |
779 |
break; |
break; |
780 |
|
|
781 |
default : |
default : |
788 |
if ((frame.input.csp = get_colorspace(inhdr)) == XVID_CSP_NULL) |
if ((frame.input.csp = get_colorspace(inhdr)) == XVID_CSP_NULL) |
789 |
return ICERR_BADFORMAT; |
return ICERR_BADFORMAT; |
790 |
|
|
791 |
if (frame.input.csp == XVID_CSP_I420 || frame.input.csp == XVID_CSP_YV12) |
if (frame.input.csp == XVID_CSP_I420 || frame.input.csp == XVID_CSP_YV12) { |
792 |
frame.input.stride[0] = (frame.input.stride[0]*2)/3; |
frame.input.stride[0] = (4 * icc->lpbiInput->biWidth + 3) / 4; |
793 |
|
frame.input.stride[1] = frame.input.stride[2] = frame.input.stride[0] / 2 ; |
794 |
|
} |
795 |
|
|
796 |
frame.bitstream = icc->lpOutput; |
frame.bitstream = icc->lpOutput; |
797 |
frame.length = icc->lpbiOutput->biSizeImage; |
frame.length = icc->lpbiOutput->biSizeImage; |
798 |
|
|
799 |
switch (codec->config.mode) |
frame.quant = 0; |
|
{ |
|
|
case DLG_MODE_CBR : |
|
|
frame.quant = 0; /* use xvidcore cbr rate control */ |
|
|
break; |
|
|
|
|
|
case DLG_MODE_VBR_QUAL : |
|
|
case DLG_MODE_VBR_QUANT : |
|
|
case DLG_MODE_2PASS_1 : |
|
|
if (codec_get_quant(codec, &frame) == ICERR_ERROR) |
|
|
{ |
|
|
return ICERR_ERROR; |
|
|
} |
|
|
break; |
|
800 |
|
|
801 |
case DLG_MODE_2PASS_2_EXT : |
if (codec->config.mode == RC_MODE_NULL) { |
|
case DLG_MODE_2PASS_2_INT : |
|
|
if (codec_2pass_get_quant(codec, &frame) == ICERR_ERROR) |
|
|
{ |
|
|
return ICERR_ERROR; |
|
|
} |
|
|
if (codec->config.dummy2pass) |
|
|
{ |
|
|
outhdr->biSizeImage = codec->twopass.bytes2; |
|
|
*icc->lpdwFlags = (codec->twopass.nns1.quant & NNSTATS_KEYFRAME) ? AVIIF_KEYFRAME : 0; |
|
|
return ICERR_OK; |
|
|
} |
|
|
break; |
|
|
|
|
|
case DLG_MODE_NULL : |
|
802 |
outhdr->biSizeImage = 0; |
outhdr->biSizeImage = 0; |
803 |
*icc->lpdwFlags = AVIIF_KEYFRAME; |
*icc->lpdwFlags = AVIIF_KEYFRAME; |
804 |
return ICERR_OK; |
return ICERR_OK; |
|
|
|
|
default : |
|
|
DEBUGERR("Invalid encoding mode"); |
|
|
return ICERR_ERROR; |
|
805 |
} |
} |
806 |
|
|
|
|
|
807 |
// force keyframe spacing in 2-pass 1st pass |
// force keyframe spacing in 2-pass 1st pass |
808 |
if (codec->config.motion_search == 0) |
if (codec->config.motion_search == 0) |
|
{ |
|
809 |
frame.type = XVID_TYPE_IVOP; |
frame.type = XVID_TYPE_IVOP; |
810 |
} |
|
811 |
else if (codec->keyspacing < codec->config.min_key_interval && codec->framenum) |
/* frame-based stuff */ |
812 |
{ |
apply_zone_modifiers(&frame, &codec->config, codec->framenum); |
813 |
DEBUG("current frame forced to p-frame"); |
|
814 |
frame.type = XVID_TYPE_PVOP; |
/* call encore */ |
|
} |
|
815 |
|
|
816 |
memset(&stats, 0, sizeof(stats)); |
memset(&stats, 0, sizeof(stats)); |
817 |
stats.version = XVID_VERSION; |
stats.version = XVID_VERSION; |
818 |
|
|
819 |
length = xvid_encore(codec->ehandle, XVID_ENC_ENCODE, &frame, &stats); |
length = xvid_encore_func(codec->ehandle, XVID_ENC_ENCODE, &frame, &stats); |
820 |
switch (length) |
switch (length) |
821 |
{ |
{ |
822 |
case XVID_ERR_FAIL : |
case XVID_ERR_FAIL : |
832 |
return ICERR_UNSUPPORTED; |
return ICERR_UNSUPPORTED; |
833 |
} |
} |
834 |
|
|
835 |
{ /* XXX: debug text */ |
if (codec->config.display_status && stats.type>0) { |
836 |
char tmp[100]; |
status_update(&codec->status, stats.type, stats.length, stats.quant); |
|
wsprintf(tmp, " {type=%i len=%i} length=%i", stats.type, stats.length, length); |
|
|
OutputDebugString(tmp); |
|
837 |
} |
} |
838 |
|
|
839 |
|
DPRINTF("{type=%i len=%i} length=%i", stats.type, stats.length, length); |
840 |
|
|
841 |
if (length == 0) /* no encoder output */ |
if (length == 0) /* no encoder output */ |
842 |
{ |
{ |
843 |
*icc->lpdwFlags = 0; |
*icc->lpdwFlags = 0; |
857 |
|
|
858 |
outhdr->biSizeImage = length; |
outhdr->biSizeImage = length; |
859 |
|
|
860 |
if (codec->config.mode == DLG_MODE_2PASS_1 && codec->config.discard1pass) |
if (codec->config.mode == RC_MODE_2PASS1 && codec->config.discard1pass) |
861 |
{ |
{ |
862 |
outhdr->biSizeImage = 0; |
outhdr->biSizeImage = 0; |
863 |
} |
} |
|
|
|
|
/* 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); |
|
|
} |
|
864 |
} |
} |
865 |
|
|
866 |
++codec->framenum; |
codec->framenum++; |
867 |
++codec->keyspacing; |
codec->keyspacing++; |
868 |
|
|
869 |
return ICERR_OK; |
return ICERR_OK; |
870 |
} |
} |
919 |
|
|
920 |
if (get_colorspace(inhdr) != XVID_CSP_NULL) { |
if (get_colorspace(inhdr) != XVID_CSP_NULL) { |
921 |
memcpy(outhdr, inhdr, sizeof(BITMAPINFOHEADER)); |
memcpy(outhdr, inhdr, sizeof(BITMAPINFOHEADER)); |
922 |
// XXX: should we set outhdr->biSize ?? |
/* XXX: should we set outhdr->biSize ?? */ |
923 |
return ICERR_OK; |
return ICERR_OK; |
924 |
} |
} |
925 |
/* --- yv12 --- */ |
/* --- yv12 --- */ |
945 |
return ICERR_OK; |
return ICERR_OK; |
946 |
} |
} |
947 |
|
|
948 |
|
#define REG_GET_N(X, Y, Z) \ |
949 |
|
{ \ |
950 |
|
DWORD size = sizeof(int); \ |
951 |
|
if (RegQueryValueEx(hKey, X, 0, 0, (LPBYTE)&Y, &size) != ERROR_SUCCESS) { \ |
952 |
|
Y=Z; \ |
953 |
|
} \ |
954 |
|
}while(0) |
955 |
|
|
956 |
LRESULT decompress_begin(CODEC * codec, BITMAPINFO * lpbiInput, BITMAPINFO * lpbiOutput) |
LRESULT decompress_begin(CODEC * codec, BITMAPINFO * lpbiInput, BITMAPINFO * lpbiOutput) |
957 |
{ |
{ |
958 |
xvid_gbl_init_t init; |
xvid_gbl_init_t init; |
959 |
xvid_dec_create_t create; |
xvid_dec_create_t create; |
960 |
|
HKEY hKey; |
961 |
|
|
962 |
|
if (init_dll() != 0) return ICERR_ERROR; |
963 |
|
|
964 |
memset(&init, 0, sizeof(init)); |
memset(&init, 0, sizeof(init)); |
965 |
init.version = XVID_VERSION; |
init.version = XVID_VERSION; |
966 |
init.cpu_flags = codec->config.cpu; |
init.cpu_flags = codec->config.cpu; |
967 |
xvid_global(0, XVID_GBL_INIT, &init, NULL); |
xvid_global_func(0, XVID_GBL_INIT, &init, NULL); |
968 |
|
|
969 |
memset(&create, 0, sizeof(create)); |
memset(&create, 0, sizeof(create)); |
970 |
create.version = XVID_VERSION; |
create.version = XVID_VERSION; |
971 |
create.width = lpbiInput->bmiHeader.biWidth; |
create.width = lpbiInput->bmiHeader.biWidth; |
972 |
create.height = lpbiInput->bmiHeader.biHeight; |
create.height = lpbiInput->bmiHeader.biHeight; |
973 |
|
|
974 |
switch(xvid_decore(0, XVID_DEC_CREATE, &create, NULL)) |
switch(xvid_decore_func(0, XVID_DEC_CREATE, &create, NULL)) |
975 |
{ |
{ |
976 |
case XVID_ERR_FAIL : |
case XVID_ERR_FAIL : |
977 |
return ICERR_ERROR; |
return ICERR_ERROR; |
988 |
|
|
989 |
codec->dhandle = create.handle; |
codec->dhandle = create.handle; |
990 |
|
|
991 |
|
RegOpenKeyEx(XVID_REG_KEY, XVID_REG_PARENT "\\" XVID_REG_CHILD, 0, KEY_READ, &hKey); |
992 |
|
|
993 |
|
REG_GET_N("Deblock_Y", pp_dy, 0) |
994 |
|
REG_GET_N("Deblock_UV", pp_duv, 0) |
995 |
|
REG_GET_N("Dering", pp_dr, 0) |
996 |
|
REG_GET_N("FilmEffect", pp_fe, 0) |
997 |
|
|
998 |
|
RegCloseKey(hKey); |
999 |
|
|
1000 |
return ICERR_OK; |
return ICERR_OK; |
1001 |
} |
} |
1002 |
|
|
1003 |
|
|
1004 |
LRESULT decompress_end(CODEC * codec) |
LRESULT decompress_end(CODEC * codec) |
1005 |
{ |
{ |
1006 |
if (codec->dhandle != NULL) |
if (m_hdll != NULL) { |
1007 |
{ |
if (codec->dhandle != NULL) { |
1008 |
xvid_decore(codec->dhandle, XVID_DEC_DESTROY, NULL, NULL); |
xvid_decore_func(codec->dhandle, XVID_DEC_DESTROY, NULL, NULL); |
1009 |
codec->dhandle = NULL; |
codec->dhandle = NULL; |
1010 |
} |
} |
1011 |
|
FreeLibrary(m_hdll); |
1012 |
|
m_hdll = NULL; |
1013 |
|
} |
1014 |
|
|
1015 |
return ICERR_OK; |
return ICERR_OK; |
1016 |
} |
} |
1017 |
|
|
1027 |
{ |
{ |
1028 |
xvid_gbl_convert_t convert; |
xvid_gbl_convert_t convert; |
1029 |
|
|
1030 |
DEBUGFOURCC("input", icd->lpbiInput->biCompression); |
DPRINTF("input=%c%c%c%c output=%c%c%c%c", |
1031 |
DEBUGFOURCC("output", icd->lpbiOutput->biCompression); |
icd->lpbiInput->biCompression&0xff, |
1032 |
|
(icd->lpbiInput->biCompression>>8)&0xff, |
1033 |
|
(icd->lpbiInput->biCompression>>16)&0xff, |
1034 |
|
(icd->lpbiInput->biCompression>>24)&0xff, |
1035 |
|
icd->lpbiOutput->biCompression&0xff, |
1036 |
|
(icd->lpbiOutput->biCompression>>8)&0xff, |
1037 |
|
(icd->lpbiOutput->biCompression>>16)&0xff, |
1038 |
|
(icd->lpbiOutput->biCompression>>24)&0xff); |
1039 |
|
|
1040 |
memset(&convert, 0, sizeof(convert)); |
memset(&convert, 0, sizeof(convert)); |
1041 |
convert.version = XVID_VERSION; |
convert.version = XVID_VERSION; |
1057 |
convert.interlacing = 0; |
convert.interlacing = 0; |
1058 |
if (convert.input.csp == XVID_CSP_NULL || |
if (convert.input.csp == XVID_CSP_NULL || |
1059 |
convert.output.csp == XVID_CSP_NULL || |
convert.output.csp == XVID_CSP_NULL || |
1060 |
xvid_global(0, XVID_GBL_CONVERT, &convert, NULL) < 0) |
xvid_global_func(0, XVID_GBL_CONVERT, &convert, NULL) < 0) |
1061 |
{ |
{ |
1062 |
return ICERR_BADFORMAT; |
return ICERR_BADFORMAT; |
1063 |
} |
} |
1087 |
frame.output.csp = XVID_CSP_NULL; |
frame.output.csp = XVID_CSP_NULL; |
1088 |
} |
} |
1089 |
|
|
1090 |
switch (xvid_decore(codec->dhandle, XVID_DEC_DECODE, &frame, NULL)) |
if (pp_dy)frame.general |= XVID_DEBLOCKY; |
1091 |
|
if (pp_duv) frame.general |= XVID_DEBLOCKUV; |
1092 |
|
/* if (pp_dr) frame.general |= XVID_DERING; */ |
1093 |
|
if (pp_fe) frame.general |= XVID_FILMEFFECT; |
1094 |
|
|
1095 |
|
switch (xvid_decore_func(codec->dhandle, XVID_DEC_DECODE, &frame, NULL)) |
1096 |
{ |
{ |
1097 |
case XVID_ERR_FAIL : |
case XVID_ERR_FAIL : |
1098 |
return ICERR_ERROR; |
return ICERR_ERROR; |
1110 |
return ICERR_OK; |
return ICERR_OK; |
1111 |
} |
} |
1112 |
|
|
|
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 |
|
|
|
|