57 |
#include "codec.h" |
#include "codec.h" |
58 |
#include "status.h" |
#include "status.h" |
59 |
|
|
60 |
|
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] = { |
static const int pmvfast_presets[7] = { |
74 |
0, 0, 0, 0, |
0, 0, 0, 0, |
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); |
266 |
} |
} |
267 |
|
|
268 |
|
|
269 |
const char type2char(int type) |
static char type2char(int type) |
270 |
{ |
{ |
271 |
if (type==XVID_TYPE_IVOP) |
if (type==XVID_TYPE_IVOP) |
272 |
return 'I'; |
return 'I'; |
277 |
return 'S'; |
return 'S'; |
278 |
} |
} |
279 |
|
|
280 |
int vfw_debug(void *handle, |
static int vfw_debug(void *handle, |
281 |
int opt, |
int opt, |
282 |
void *param1, |
void *param1, |
283 |
void *param2) |
void *param2) |
284 |
{ |
{ |
285 |
switch (opt) { |
switch (opt) { |
|
case XVID_PLG_INFO: |
|
286 |
case XVID_PLG_CREATE: |
case XVID_PLG_CREATE: |
287 |
|
*((void**)param2) = NULL; |
288 |
|
case XVID_PLG_INFO: |
289 |
case XVID_PLG_DESTROY: |
case XVID_PLG_DESTROY: |
290 |
case XVID_PLG_BEFORE: |
case XVID_PLG_BEFORE: |
291 |
return 0; |
return 0; |
316 |
|
|
317 |
static int init_dll() |
static int init_dll() |
318 |
{ |
{ |
319 |
/*if (m_hdll != NULL) return;*/ |
if (m_hdll != NULL) return 0; |
320 |
|
|
321 |
DPRINTF("init_dll"); |
DPRINTF("init_dll"); |
322 |
m_hdll = LoadLibrary(XVID_DLL_NAME); |
m_hdll = LoadLibrary(XVID_DLL_NAME); |
323 |
if (m_hdll == NULL) { |
if (m_hdll == NULL) { |
324 |
DPRINTF("dll load failed"); |
DPRINTF("dll load failed"); |
325 |
MessageBox(0, XVID_DLL_NAME " not found","Error", 0); |
MessageBox(0, XVID_DLL_NAME " not found!","Error!", MB_ICONEXCLAMATION|MB_OK); |
326 |
return XVID_ERR_FAIL; |
return XVID_ERR_FAIL; |
327 |
} |
} |
328 |
|
|
358 |
return 0; |
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 |
{ |
{ |
432 |
xvid_plugin_2pass1_t pass1; |
xvid_plugin_2pass1_t pass1; |
433 |
xvid_plugin_2pass2_t pass2; |
xvid_plugin_2pass2_t pass2; |
434 |
int i; |
int i; |
435 |
|
HANDLE hFile; |
436 |
|
|
437 |
|
CONFIG tmpCfg; /* if we want to alter config to suit our needs, it shouldn't be visible to user later */ |
438 |
|
memcpy(&tmpCfg, &codec->config, sizeof(CONFIG)); |
439 |
|
|
440 |
if (init_dll() != 0) return ICERR_ERROR; |
if (init_dll() != 0) return ICERR_ERROR; |
441 |
/* destroy previously created codec */ |
/* destroy previously created codec */ |
453 |
memset(&create, 0, sizeof(create)); |
memset(&create, 0, sizeof(create)); |
454 |
create.version = XVID_VERSION; |
create.version = XVID_VERSION; |
455 |
|
|
|
/* zones */ |
|
|
create.zones = malloc(sizeof(xvid_enc_zone_t) * codec->config.num_zones); |
|
|
create.num_zones = codec->config.num_zones; |
|
|
for (i=0; i < create.num_zones; i++) { |
|
|
create.zones[i].frame = codec->config.zones[i].frame; |
|
|
if (codec->config.zones[i].mode == RC_ZONE_QUANT) { |
|
|
create.zones[i].mode = XVID_ZONE_QUANT; |
|
|
create.zones[i].increment = codec->config.zones[i].quant; |
|
|
}else{ |
|
|
create.zones[i].mode = XVID_ZONE_WEIGHT; |
|
|
create.zones[i].increment = codec->config.zones[i].weight; |
|
|
} |
|
|
create.zones[i].base = 100; |
|
|
} |
|
|
|
|
456 |
/* plugins */ |
/* plugins */ |
457 |
create.plugins = plugins; |
create.plugins = plugins; |
458 |
switch (codec->config.mode) |
switch (codec->config.mode) |
467 |
plugins[create.num_plugins].func = xvid_plugin_single_func; |
plugins[create.num_plugins].func = xvid_plugin_single_func; |
468 |
plugins[create.num_plugins].param = &single; |
plugins[create.num_plugins].param = &single; |
469 |
create.num_plugins++; |
create.num_plugins++; |
470 |
|
if (!codec->config.use_2pass_bitrate) /* constant-quant mode */ |
471 |
|
prepare_cquant_zones(&tmpCfg); |
472 |
break; |
break; |
473 |
|
|
474 |
case RC_MODE_2PASS1 : |
case RC_MODE_2PASS1 : |
475 |
memset(&pass1, 0, sizeof(pass1)); |
memset(&pass1, 0, sizeof(pass1)); |
476 |
pass1.version = XVID_VERSION; |
pass1.version = XVID_VERSION; |
477 |
pass1.filename = codec->config.stats; |
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; |
plugins[create.num_plugins].func = xvid_plugin_2pass1_func; |
481 |
plugins[create.num_plugins].param = &pass1; |
plugins[create.num_plugins].param = &pass1; |
482 |
create.num_plugins++; |
create.num_plugins++; |
492 |
} |
} |
493 |
pass2.filename = codec->config.stats; |
pass2.filename = codec->config.stats; |
494 |
|
|
495 |
|
hFile = CreateFile(pass2.filename, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL); |
496 |
|
if (hFile == INVALID_HANDLE_VALUE) |
497 |
|
{ |
498 |
|
MessageBox(0, "Statsfile not found!","Error!", MB_ICONEXCLAMATION|MB_OK); |
499 |
|
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...]; */ |
pass2.keyframe_boost = codec->config.keyframe_boost; /* keyframe boost percentage: [0..100...]; */ |
506 |
pass2.curve_compression_high = codec->config.curve_compression_high; |
pass2.curve_compression_high = codec->config.curve_compression_high; |
507 |
pass2.curve_compression_low = codec->config.curve_compression_low; |
pass2.curve_compression_low = codec->config.curve_compression_low; |
524 |
break; |
break; |
525 |
} |
} |
526 |
|
|
527 |
|
/* zones - copy from tmpCfg in case we automatically altered them above */ |
528 |
|
create.zones = malloc(sizeof(xvid_enc_zone_t) * tmpCfg.num_zones); |
529 |
|
create.num_zones = tmpCfg.num_zones; |
530 |
|
for (i=0; i < create.num_zones; i++) { |
531 |
|
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 |
|
/* lumimasking plugin */ |
543 |
if ((profiles[codec->config.profile].flags & PROFILE_ADAPTQUANT) && codec->config.lum_masking) { |
if ((profiles[codec->config.profile].flags & PROFILE_ADAPTQUANT) && codec->config.lum_masking) { |
544 |
plugins[create.num_plugins].func = xvid_plugin_lumimasking_func; |
plugins[create.num_plugins].func = xvid_plugin_lumimasking_func; |
545 |
plugins[create.num_plugins].param = NULL; |
plugins[create.num_plugins].param = NULL; |
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; |
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; |
if (init_dll() != 0) return ICERR_ERROR; |
963 |
|
|
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 |
|
|
1087 |
frame.output.csp = XVID_CSP_NULL; |
frame.output.csp = XVID_CSP_NULL; |
1088 |
} |
} |
1089 |
|
|
1090 |
|
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)) |
switch (xvid_decore_func(codec->dhandle, XVID_DEC_DECODE, &frame, NULL)) |
1096 |
{ |
{ |
1097 |
case XVID_ERR_FAIL : |
case XVID_ERR_FAIL : |