--- codec.c 2003/02/22 08:24:01 1.1.2.1 +++ codec.c 2004/01/24 13:36:00 1.1.2.32 @@ -49,18 +49,25 @@ #include #include +#include +#include "vfwext.h" +#include +#include "debug.h" #include "codec.h" -#include "2pass.h" +#include "status.h" -int pmvfast_presets[7] = { + +static const int pmvfast_presets[7] = { 0, 0, 0, 0, - 0 | PMV_HALFPELREFINE16 | 0, - 0 | PMV_HALFPELREFINE16 | 0 | - PMV_ADVANCEDDIAMOND16, PMV_HALFPELREFINE16 | PMV_EXTSEARCH16 | - PMV_HALFPELREFINE8 | 0 | PMV_USESQUARES16 + 0 | XVID_ME_HALFPELREFINE16 | 0, + 0 | XVID_ME_HALFPELREFINE16 | 0 | + XVID_ME_ADVANCEDDIAMOND16, XVID_ME_HALFPELREFINE16 | XVID_ME_EXTSEARCH16 | + XVID_ME_HALFPELREFINE8 | 0 | XVID_ME_USESQUARES16 }; + + /* return xvid compatbile colorspace, or XVID_CSP_NULL if failure */ @@ -75,21 +82,21 @@ case BI_RGB : if (hdr->biBitCount == 16) { - DEBUG("RGB16 (RGB555)"); + DPRINTF("RGB16 (RGB555)"); return rgb_flip | XVID_CSP_RGB555; } if (hdr->biBitCount == 24) { - DEBUG("RGB24"); + DPRINTF("RGB24"); return rgb_flip | XVID_CSP_BGR; } if (hdr->biBitCount == 32) { - DEBUG("RGB32"); + DPRINTF("RGB32"); return rgb_flip | XVID_CSP_BGRA; } - DEBUG1("unsupported BI_RGB biBitCount", hdr->biBitCount); + DPRINTF("unsupported BI_RGB biBitCount=%i", hdr->biBitCount); return XVID_CSP_NULL; case BI_BITFIELDS : @@ -102,7 +109,7 @@ hdr4->bV4GreenMask == 0x3e0 && hdr4->bV4BlueMask == 0x1f) { - DEBUG("RGB555"); + DPRINTF("RGB555"); return rgb_flip | XVID_CSP_RGB555; } @@ -111,41 +118,45 @@ hdr4->bV4GreenMask == 0x7e0 && hdr4->bV4BlueMask == 0x1f) { - DEBUG("RGB565"); + DPRINTF("RGB565"); return rgb_flip | XVID_CSP_RGB565; } - DEBUG("unsupported BI_BITFIELDS mode"); + DPRINTF("unsupported BI_BITFIELDS mode"); return XVID_CSP_NULL; } - DEBUG("unsupported BI_BITFIELDS/BITMAPHEADER combination"); + DPRINTF("unsupported BI_BITFIELDS/BITMAPHEADER combination"); return XVID_CSP_NULL; case FOURCC_I420 : case FOURCC_IYUV : - DEBUG("IYUY"); + DPRINTF("IYUY"); return XVID_CSP_I420; case FOURCC_YV12 : - DEBUG("YV12"); + DPRINTF("YV12"); return XVID_CSP_YV12; case FOURCC_YUYV : case FOURCC_YUY2 : - DEBUG("YUY2"); + DPRINTF("YUY2"); return XVID_CSP_YUY2; case FOURCC_YVYU : - DEBUG("YVYU"); + DPRINTF("YVYU"); return XVID_CSP_YVYU; case FOURCC_UYVY : - DEBUG("UYVY"); + DPRINTF("UYVY"); return XVID_CSP_UYVY; default : - DEBUGFOURCC("unsupported colorspace", hdr->biCompression); + DPRINTF("unsupported colorspace %c%c%c%c", + hdr->biCompression&0xff, + (hdr->biCompression>>8)&0xff, + (hdr->biCompression>>16)&0xff, + (hdr->biCompression>>24)&0xff); return XVID_CSP_NULL; } } @@ -161,6 +172,11 @@ BITMAPINFOHEADER * inhdr = &lpbiInput->bmiHeader; BITMAPINFOHEADER * outhdr = &lpbiOutput->bmiHeader; + /* VFWEXT detection */ + if (inhdr->biCompression == VFWEXT_FOURCC) { + return (ICM_USER+0x0fff); + } + if (get_colorspace(inhdr) == XVID_CSP_NULL) { return ICERR_BADFORMAT; @@ -193,7 +209,7 @@ if (lpbiOutput == NULL) { - return sizeof(BITMAPV4HEADER); + return sizeof(BITMAPINFOHEADER); } memcpy(outhdr, inhdr, sizeof(BITMAPINFOHEADER)); @@ -229,100 +245,302 @@ LRESULT compress_frames_info(CODEC * codec, ICCOMPRESSFRAMES * icf) { - // DEBUG2("frate fscale", codec->frate, codec->fscale); +#if 0 + DPRINTF("%i %i", icf->lStartFrame, icf->lFrameCount); +#endif codec->fincr = icf->dwScale; codec->fbase = icf->dwRate; return ICERR_OK; } +const char type2char(int type) +{ + if (type==XVID_TYPE_IVOP) + return 'I'; + if (type==XVID_TYPE_PVOP) + return 'P'; + if (type==XVID_TYPE_BVOP) + return 'B'; + return 'S'; +} + +int vfw_debug(void *handle, + int opt, + void *param1, + void *param2) +{ + switch (opt) { + case XVID_PLG_CREATE: + *((void**)param2) = NULL; + case XVID_PLG_INFO: + case XVID_PLG_DESTROY: + case XVID_PLG_BEFORE: + return 0; + + case XVID_PLG_AFTER: + { + xvid_plg_data_t *data = (xvid_plg_data_t *) param1; + + /* We don't use DPRINTF here because it's active only for _DEBUG + * builds and that activates lot of other debug printfs. We only + * want these all the time */ + char buf[1024]; + sprintf(buf, "[%6i] type=%c Q:%2i length:%6i", + data->frame_num, + type2char(data->type), + data->quant, + data->length); + OutputDebugString(buf); + + return 0; + } + } + + return XVID_ERR_FAIL; +} + +#define XVID_DLL_NAME "xvidcore.dll" + +static int init_dll() +{ + if (m_hdll != NULL) return 0; + + DPRINTF("init_dll"); + m_hdll = LoadLibrary(XVID_DLL_NAME); + if (m_hdll == NULL) { + DPRINTF("dll load failed"); + MessageBox(0, XVID_DLL_NAME " not found!","Error!", MB_ICONEXCLAMATION|MB_OK); + return XVID_ERR_FAIL; + } + + xvid_global_func = (int (__cdecl *)(void *, int, void *, void *))GetProcAddress(m_hdll, "xvid_global"); + if (xvid_global_func == NULL) { + MessageBox(0, "xvid_global() not found", "Error", 0); + return XVID_ERR_FAIL; + } + + xvid_encore_func = (int (__cdecl *)(void *, int, void *, void *))GetProcAddress(m_hdll, "xvid_encore"); + if (xvid_encore_func == NULL) { + MessageBox(0, "xvid_encore() not found", "Error", 0); + return XVID_ERR_FAIL; + } + + xvid_decore_func = (int (__cdecl *)(void *, int, void *, void *))GetProcAddress(m_hdll, "xvid_decore"); + if (xvid_decore_func == NULL) { + MessageBox(0, "xvid_decore() not found", "Error", 0); + return XVID_ERR_FAIL; + } + + xvid_plugin_single_func = + (int (__cdecl *)(void *, int, void *, void *))(GetProcAddress(m_hdll, "xvid_plugin_single")); + xvid_plugin_2pass1_func = + (int (__cdecl *)(void *, int, void *, void *))(GetProcAddress(m_hdll, "xvid_plugin_2pass1")); + xvid_plugin_2pass2_func = + (int (__cdecl *)(void *, int, void *, void *))(GetProcAddress(m_hdll, "xvid_plugin_2pass2")); + xvid_plugin_lumimasking_func = + (int (__cdecl *)(void *, int, void *, void *))(GetProcAddress(m_hdll, "xvid_plugin_lumimasking")); + xvid_plugin_psnr_func = + (int (__cdecl *)(void *, int, void *, void *))(GetProcAddress(m_hdll, "xvid_plugin_psnr")); + + return 0; +} + +void +sort_zones(zone_t * zones, int zone_num, int * sel); + +static void +prepare_cquant_zones(CONFIG * config) { + + int i = 0; + if (config->num_zones == 0 || config->zones[0].frame != 0) { + /* first zone does not start at frame 0 or doesn't exist */ + + if (config->num_zones >= MAX_ZONES) config->num_zones--; /* we scrifice last zone */ + + config->zones[config->num_zones].frame = 0; + config->zones[config->num_zones].mode = RC_ZONE_QUANT; + config->zones[config->num_zones].weight = 100; + config->zones[config->num_zones].quant = config->desired_quant; + config->zones[config->num_zones].type = XVID_TYPE_AUTO; + config->zones[config->num_zones].greyscale = 0; + config->zones[config->num_zones].chroma_opt = 0; + config->zones[config->num_zones].bvop_threshold = 0; + config->num_zones++; + + sort_zones(config->zones, config->num_zones, &i); + } + + /* step 2: let's change all weight zones into quant zones */ + + for(i = 0; i < config->num_zones; i++) + if (config->zones[i].mode == RC_ZONE_WEIGHT) { + config->zones[i].mode = RC_ZONE_QUANT; + config->zones[i].quant = (100*config->desired_quant) / config->zones[i].weight; + } +} + + LRESULT compress_begin(CODEC * codec, BITMAPINFO * lpbiInput, BITMAPINFO * lpbiOutput) { xvid_gbl_init_t init; xvid_enc_create_t create; - xvid_enc_rc_t rc; + xvid_enc_plugin_t plugins[3]; + xvid_plugin_single_t single; + xvid_plugin_2pass1_t pass1; + xvid_plugin_2pass2_t pass2; + int i; + HANDLE hFile; + + CONFIG tmpCfg; /* if we want to alter config to suit our needs, it shouldn't be visible to user later */ + memcpy(&tmpCfg, &codec->config, sizeof(CONFIG)); + + if (init_dll() != 0) return ICERR_ERROR; + /* destroy previously created codec */ + if(codec->ehandle) { + xvid_encore_func(codec->ehandle, XVID_ENC_DESTROY, NULL, NULL); + codec->ehandle = NULL; + } - memset(&rc, 0, sizeof(rc)); - rc.version = XVID_VERSION; + memset(&init, 0, sizeof(init)); + init.version = XVID_VERSION; + init.cpu_flags = codec->config.cpu; + init.debug = codec->config.debug; + xvid_global_func(0, XVID_GBL_INIT, &init, NULL); + memset(&create, 0, sizeof(create)); + create.version = XVID_VERSION; + + /* plugins */ + create.plugins = plugins; switch (codec->config.mode) { - case DLG_MODE_CBR : - //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; + case RC_MODE_1PASS : + memset(&single, 0, sizeof(single)); + single.version = XVID_VERSION; + single.bitrate = codec->config.bitrate * CONFIG_KBPS; + single.reaction_delay_factor = codec->config.rc_reaction_delay_factor; + single.averaging_period = codec->config.rc_averaging_period; + single.buffer = codec->config.rc_buffer; + plugins[create.num_plugins].func = xvid_plugin_single_func; + plugins[create.num_plugins].param = &single; + create.num_plugins++; + if (!codec->config.use_2pass_bitrate) /* constant-quant mode */ + prepare_cquant_zones(&tmpCfg); + break; + + case RC_MODE_2PASS1 : + memset(&pass1, 0, sizeof(pass1)); + pass1.version = XVID_VERSION; + pass1.filename = codec->config.stats; + + plugins[create.num_plugins].func = xvid_plugin_2pass1_func; + plugins[create.num_plugins].param = &pass1; + create.num_plugins++; + break; + + case RC_MODE_2PASS2 : + memset(&pass2, 0, sizeof(pass2)); + pass2.version = XVID_VERSION; + if (codec->config.use_2pass_bitrate) { + pass2.bitrate = codec->config.bitrate * CONFIG_KBPS; + } else { + pass2.bitrate = -codec->config.desired_size; /* kilobytes */ + } + pass2.filename = codec->config.stats; - case DLG_MODE_VBR_QUAL : - codec->config.fquant = 0; - //param.rc_bitrate = 0; - break; + hFile = CreateFile(pass2.filename, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL); + if (hFile == INVALID_HANDLE_VALUE) + { + MessageBox(0, "Statsfile not found!","Error!", MB_ICONEXCLAMATION|MB_OK); + return XVID_ERR_FAIL; + } else + { + CloseHandle(hFile); + } - case DLG_MODE_VBR_QUANT : - codec->config.fquant = (float) codec->config.quant; - //param.rc_bitrate = 0; - break; + pass2.keyframe_boost = codec->config.keyframe_boost; /* keyframe boost percentage: [0..100...]; */ + pass2.curve_compression_high = codec->config.curve_compression_high; + pass2.curve_compression_low = codec->config.curve_compression_low; + pass2.overflow_control_strength = codec->config.overflow_control_strength; + pass2.max_overflow_improvement = codec->config.twopass_max_overflow_improvement; + pass2.max_overflow_degradation = codec->config.twopass_max_overflow_degradation; + pass2.kfreduction = codec->config.kfreduction; + pass2.kfthreshold = codec->config.kfthreshold; + pass2.container_frame_overhead = 24; /* AVI */ - 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)); + plugins[create.num_plugins].func = xvid_plugin_2pass2_func; + plugins[create.num_plugins].param = &pass2; + create.num_plugins++; break; - case DLG_MODE_NULL : + case RC_MODE_NULL : return ICERR_OK; default : break; } - /* destroy previously created codec */ - if(codec->ehandle) - { - xvid_encore(codec->ehandle, XVID_ENC_DESTROY, NULL, NULL); - codec->ehandle = NULL; - } - - memset(&init, 0, sizeof(init)); - init.version = XVID_VERSION; - init.cpu_flags = codec->config.cpu; - xvid_global(0, XVID_GBL_INIT, &init, NULL); + /* zones - copy from tmpCfg in case we automatically altered them above */ + create.zones = malloc(sizeof(xvid_enc_zone_t) * tmpCfg.num_zones); + create.num_zones = tmpCfg.num_zones; + for (i=0; i < create.num_zones; i++) { + create.zones[i].frame = tmpCfg.zones[i].frame; + if (tmpCfg.zones[i].mode == RC_ZONE_QUANT) { + create.zones[i].mode = XVID_ZONE_QUANT; + create.zones[i].increment = tmpCfg.zones[i].quant; + }else{ + create.zones[i].mode = XVID_ZONE_WEIGHT; + create.zones[i].increment = tmpCfg.zones[i].weight; + } + create.zones[i].base = 100; + } + + /* lumimasking plugin */ + if ((profiles[codec->config.profile].flags & PROFILE_ADAPTQUANT) && codec->config.lum_masking) { + plugins[create.num_plugins].func = xvid_plugin_lumimasking_func; + plugins[create.num_plugins].param = NULL; + create.num_plugins++; + } + + plugins[create.num_plugins].func = vfw_debug; + plugins[create.num_plugins].param = NULL; + create.num_plugins++; - memset(&create, 0, sizeof(create)); - create.version = XVID_VERSION; + create.profile = profiles[codec->config.profile].id; create.width = lpbiInput->bmiHeader.biWidth; create.height = lpbiInput->bmiHeader.biHeight; create.fincr = codec->fincr; create.fbase = codec->fbase; - if (codec->config.packed) - create.global |= XVID_PACKED; - if (codec->config.dx50bvop) - create.global |= XVID_CLOSED_GOP; - - create.max_key_interval = codec->config.max_key_interval; - /* XXX: param.min_quantizer = codec->config.min_pquant; - param.max_quantizer = codec->config.max_pquant; */ + create.max_key_interval = codec->config.max_key_interval; - create.max_bframes = codec->config.max_bframes; - create.frame_drop_ratio = codec->config.frame_drop_ratio; + create.min_quant[0] = codec->config.min_iquant; + create.max_quant[0] = codec->config.max_iquant; + create.min_quant[1] = codec->config.min_pquant; + create.max_quant[1] = codec->config.max_pquant; + create.min_quant[2] = codec->config.min_bquant; + create.max_quant[2] = codec->config.max_bquant; + + if ((profiles[codec->config.profile].flags & PROFILE_BVOP) && codec->config.use_bvop) { + create.max_bframes = codec->config.max_bframes; + create.bquant_ratio = codec->config.bquant_ratio; + create.bquant_offset = codec->config.bquant_offset; - create.bquant_ratio = codec->config.bquant_ratio; - create.bquant_offset = codec->config.bquant_offset; + if (codec->config.packed) + create.global |= XVID_GLOBAL_PACKED; + + if (codec->config.closed_gov) + create.global |= XVID_GLOBAL_CLOSED_GOP; + + } + + create.frame_drop_ratio = codec->config.frame_drop_ratio; create.num_threads = codec->config.num_threads; - 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)) { case XVID_ERR_FAIL : return ICERR_ERROR; @@ -341,8 +559,10 @@ codec->framenum = 0; codec->keyspacing = 0; - codec->twopass.hints = codec->twopass.stats1 = codec->twopass.stats2 = INVALID_HANDLE_VALUE; - codec->twopass.hintstream = NULL; + if (codec->config.display_status) { + status_destroy_always(&codec->status); + status_create(&codec->status, codec->fincr, codec->fbase); + } return ICERR_OK; } @@ -350,35 +570,47 @@ LRESULT compress_end(CODEC * codec) { - if (codec->ehandle != 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); + if (m_hdll != NULL) { + if (codec->ehandle != NULL) { + xvid_encore_func(codec->ehandle, XVID_ENC_DESTROY, NULL, NULL); + codec->ehandle = NULL; } + FreeLibrary(m_hdll); + m_hdll = NULL; + } - codec->ehandle = NULL; + if (codec->config.display_status) + status_destroy(&codec->status); + + return ICERR_OK; +} + + +static void apply_zone_modifiers(xvid_enc_frame_t * frame, CONFIG * config, int framenum) +{ + int i; + + for (i=0; inum_zones && config->zones[i].frame <= framenum; i++) ; + + if (--i < 0) return; /* there are no zones, or we're before the first zone */ - codec_2pass_finish(codec); + if (framenum == config->zones[i].frame) + frame->type = config->zones[i].type; + + if (config->zones[i].greyscale) { + frame->vop_flags |= XVID_VOP_GREYSCALE; } - return ICERR_OK; + if (config->zones[i].chroma_opt) { + frame->vop_flags |= XVID_VOP_CHROMAOPT; + } + + if ((profiles[config->profile].flags & PROFILE_BVOP) && config->use_bvop) { + frame->bframe_threshold = config->zones[i].bvop_threshold; + } } + LRESULT compress(CODEC * codec, ICCOMPRESS * icc) { BITMAPINFOHEADER * inhdr = icc->lpbiInput; @@ -387,150 +619,120 @@ xvid_enc_stats_t stats; int length; - // 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; - } - } - memset(&frame, 0, sizeof(frame)); frame.version = XVID_VERSION; - frame.type = XVID_TYPE_AUTO; + frame.type = XVID_TYPE_AUTO; /* vol stuff */ - if (codec->config.quant_type != QUANT_MODE_H263) + if ((profiles[codec->config.profile].flags & PROFILE_MPEGQUANT) && + codec->config.quant_type != QUANT_MODE_H263) { - frame.vol_flags |= XVID_MPEGQUANT; + frame.vol_flags |= XVID_VOL_MPEGQUANT; - // we actually need "default/custom" selectbox for both inter/intra - // this will do for now - if (codec->config.quant_type == QUANT_MODE_CUSTOM) - { + if (codec->config.quant_type == QUANT_MODE_CUSTOM) { frame.quant_intra_matrix = codec->config.qmatrix_intra; frame.quant_inter_matrix = codec->config.qmatrix_inter; - } - else - { + }else{ frame.quant_intra_matrix = NULL; frame.quant_inter_matrix = NULL; } } - if (codec->config.reduced_resolution) { - frame.vol_flags |= XVID_REDUCED_ENABLE; - frame.vop_flags |= XVID_REDUCED; /* XXX: need auto decion mode */ + if ((profiles[codec->config.profile].flags & PROFILE_REDUCED) && + codec->config.reduced_resolution) { + frame.vol_flags |= XVID_VOL_REDUCED_ENABLE; + frame.vop_flags |= XVID_VOP_REDUCED; /* XXX: need auto decion mode */ } - if (codec->config.qpel) { - frame.vol_flags |= XVID_QUARTERPEL; - frame.motion |= PMV_QUARTERPELREFINE16 | PMV_QUARTERPELREFINE8; + if ((profiles[codec->config.profile].flags & PROFILE_QPEL) && codec->config.qpel) { + frame.vol_flags |= XVID_VOL_QUARTERPEL; + frame.motion |= XVID_ME_QUARTERPELREFINE16 | XVID_ME_QUARTERPELREFINE8; } - if (codec->config.gmc) - frame.vol_flags |= XVID_GMC; - - if (codec->config.interlacing) - frame.vol_flags |= XVID_INTERLACING; - - /* vop stuff */ - - frame.vop_flags |= XVID_HALFPEL; - frame.vop_flags |= XVID_HQACPRED; + if ((profiles[codec->config.profile].flags & PROFILE_GMC) && codec->config.gmc) { + frame.vol_flags |= XVID_VOL_GMC; + frame.motion |= XVID_ME_GME_REFINE; + } - if (codec->config.debug) - frame.vop_flags |= XVID_DEBUG; + if ((profiles[codec->config.profile].flags & PROFILE_INTERLACE) && codec->config.interlacing) + frame.vol_flags |= XVID_VOL_INTERLACING; - if (codec->config.motion_search > 4) - frame.vop_flags |= XVID_INTER4V; + if (codec->config.ar_mode == 0) { /* PAR */ + if (codec->config.display_aspect_ratio != 5) { + frame.par = codec->config.display_aspect_ratio + 1; + } else { + frame.par = XVID_PAR_EXT; + frame.par_width = codec->config.par_x; + frame.par_height= codec->config.par_y; + } + } else { /* AR */ + /* custom pixel aspect ratio -> calculated from DAR */ + frame.par = XVID_PAR_EXT; + frame.par_width = (100 * inhdr->biHeight) / codec->config.ar_y; + frame.par_height= (100 * inhdr->biWidth) / codec->config.ar_x; + } - if (codec->config.lum_masking) - frame.vop_flags |= XVID_LUMIMASKING; + /* vop stuff */ - if (codec->config.chromame) - frame.vop_flags |= PMV_CHROMA16 + PMV_CHROMA8; + frame.vop_flags |= XVID_VOP_HALFPEL; + frame.vop_flags |= XVID_VOP_HQACPRED; - if (codec->config.chroma_opt) - frame.vop_flags |= XVID_CHROMAOPT; + if (codec->config.vop_debug) + frame.vop_flags |= XVID_VOP_DEBUG; - check_greyscale_mode(&codec->config, &frame, codec->framenum); + if (codec->config.trellis_quant) { + frame.vop_flags |= XVID_VOP_TRELLISQUANT; + } -/* XXX: hinted me + if (codec->config.motion_search > 4) + frame.vop_flags |= XVID_VOP_INTER4V; -// 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.chromame) + frame.motion |= XVID_ME_CHROMA_PVOP + XVID_ME_CHROMA_BVOP; - 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; + if (codec->config.cartoon_mode) { + frame.vop_flags |= XVID_VOP_CARTOON; + frame.motion |= XVID_ME_DETECT_STATIC_MOTION; } - 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; - } - } */ + if (codec->config.turbo) + frame.motion |= XVID_ME_FASTREFINE16 | XVID_ME_FASTREFINE8 | + XVID_ME_SKIP_DELTASEARCH | XVID_ME_FAST_MODEINTERPOLATE | + XVID_ME_BFRAME_EARLYSTOP; frame.motion |= pmvfast_presets[codec->config.motion_search]; switch (codec->config.vhq_mode) { case VHQ_MODE_DECISION : - frame.vop_flags |= XVID_MODEDECISION_BITS; + frame.vop_flags |= XVID_VOP_MODEDECISION_RD; break; case VHQ_LIMITED_SEARCH : - frame.vop_flags |= XVID_MODEDECISION_BITS; - frame.motion |= HALFPELREFINE16_BITS; - frame.motion |= QUARTERPELREFINE16_BITS; + frame.vop_flags |= XVID_VOP_MODEDECISION_RD; + frame.motion |= XVID_ME_HALFPELREFINE16_RD; + frame.motion |= XVID_ME_QUARTERPELREFINE16_RD; break; case VHQ_MEDIUM_SEARCH : - frame.vop_flags |= XVID_MODEDECISION_BITS; - frame.motion |= HALFPELREFINE16_BITS; - frame.motion |= HALFPELREFINE8_BITS; - frame.motion |= QUARTERPELREFINE16_BITS; - frame.motion |= QUARTERPELREFINE8_BITS; - frame.motion |= CHECKPREDICTION_BITS; + frame.vop_flags |= XVID_VOP_MODEDECISION_RD; + frame.motion |= XVID_ME_HALFPELREFINE16_RD; + frame.motion |= XVID_ME_HALFPELREFINE8_RD; + frame.motion |= XVID_ME_QUARTERPELREFINE16_RD; + frame.motion |= XVID_ME_QUARTERPELREFINE8_RD; + frame.motion |= XVID_ME_CHECKPREDICTION_RD; break; case VHQ_WIDE_SEARCH : - frame.vop_flags |= XVID_MODEDECISION_BITS; - frame.motion |= HALFPELREFINE16_BITS; - frame.motion |= HALFPELREFINE8_BITS; - frame.motion |= QUARTERPELREFINE16_BITS; - frame.motion |= QUARTERPELREFINE8_BITS; - frame.motion |= CHECKPREDICTION_BITS; - frame.motion |= EXTSEARCH_BITS; + frame.vop_flags |= XVID_VOP_MODEDECISION_RD; + frame.motion |= XVID_ME_HALFPELREFINE16_RD; + frame.motion |= XVID_ME_HALFPELREFINE8_RD; + frame.motion |= XVID_ME_QUARTERPELREFINE16_RD; + frame.motion |= XVID_ME_QUARTERPELREFINE8_RD; + frame.motion |= XVID_ME_CHECKPREDICTION_RD; + frame.motion |= XVID_ME_EXTSEARCH_RD; break; default : @@ -549,63 +751,27 @@ frame.bitstream = icc->lpOutput; frame.length = icc->lpbiOutput->biSizeImage; - switch (codec->config.mode) - { - 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; - - case DLG_MODE_2PASS_2_EXT : - 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; + frame.quant = 0; - case DLG_MODE_NULL : + if (codec->config.mode == RC_MODE_NULL) { outhdr->biSizeImage = 0; *icc->lpdwFlags = AVIIF_KEYFRAME; return ICERR_OK; - - default : - DEBUGERR("Invalid encoding mode"); - return ICERR_ERROR; } - // force keyframe spacing in 2-pass 1st pass if (codec->config.motion_search == 0) - { frame.type = XVID_TYPE_IVOP; - } - else if (codec->keyspacing < codec->config.min_key_interval && codec->framenum) - { - DEBUG("current frame forced to p-frame"); - frame.type = XVID_TYPE_PVOP; - } - frame.bquant = 0; + /* frame-based stuff */ + apply_zone_modifiers(&frame, &codec->config, codec->framenum); + + /* call encore */ memset(&stats, 0, sizeof(stats)); stats.version = XVID_VERSION; - length = xvid_encore(codec->ehandle, XVID_ENC_ENCODE, &frame, &stats); + length = xvid_encore_func(codec->ehandle, XVID_ENC_ENCODE, &frame, &stats); switch (length) { case XVID_ERR_FAIL : @@ -621,13 +787,13 @@ return ICERR_UNSUPPORTED; } - { /* XXX: debug text */ - char tmp[100]; - wsprintf(tmp, " {type=%i len=%i} length=%i", stats.type, stats.length, length); - OutputDebugString(tmp); + if (codec->config.display_status && stats.type>0) { + status_update(&codec->status, stats.type, stats.length, stats.quant); } - if (length == 0) /* no encoder output */ + DPRINTF("{type=%i len=%i} length=%i", stats.type, stats.length, length); + + if (length == 0) /* no encoder output */ { *icc->lpdwFlags = 0; ((char*)icc->lpOutput)[0] = 0x7f; /* virtual dub skip frame */ @@ -635,54 +801,25 @@ }else{ if (frame.out_flags & XVID_KEYFRAME) - { - codec->keyspacing = 0; - *icc->lpdwFlags = AVIIF_KEYFRAME; - } - else - { - *icc->lpdwFlags = 0; - } - - outhdr->biSizeImage = length; - - if (codec->config.mode == DLG_MODE_2PASS_1 && codec->config.discard1pass) - { - outhdr->biSizeImage = 0; - } - - /* 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); - } - } + { + codec->keyspacing = 0; + *icc->lpdwFlags = AVIIF_KEYFRAME; + } + else + { + *icc->lpdwFlags = 0; + } + + outhdr->biSizeImage = length; - ++codec->framenum; - ++codec->keyspacing; + if (codec->config.mode == RC_MODE_2PASS1 && codec->config.discard1pass) + { + outhdr->biSizeImage = 0; + } + } + + codec->framenum++; + codec->keyspacing++; return ICERR_OK; } @@ -737,7 +874,7 @@ if (get_colorspace(inhdr) != XVID_CSP_NULL) { memcpy(outhdr, inhdr, sizeof(BITMAPINFOHEADER)); - // XXX: should we set outhdr->biSize ?? + /* XXX: should we set outhdr->biSize ?? */ return ICERR_OK; } /* --- yv12 --- */ @@ -763,25 +900,35 @@ return ICERR_OK; } +#define REG_GET_N(X, Y, Z) \ +{ \ + DWORD size = sizeof(int); \ + if (RegQueryValueEx(hKey, X, 0, 0, (LPBYTE)&Y, &size) != ERROR_SUCCESS) { \ + Y=Z; \ + } \ +}while(0) LRESULT decompress_begin(CODEC * codec, BITMAPINFO * lpbiInput, BITMAPINFO * lpbiOutput) { xvid_gbl_init_t init; xvid_dec_create_t create; + HKEY hKey; + + if (init_dll() != 0) return ICERR_ERROR; memset(&init, 0, sizeof(init)); init.version = XVID_VERSION; init.cpu_flags = codec->config.cpu; - xvid_global(0, XVID_GBL_INIT, &init, NULL); + xvid_global_func(0, XVID_GBL_INIT, &init, NULL); memset(&create, 0, sizeof(create)); create.version = XVID_VERSION; create.width = lpbiInput->bmiHeader.biWidth; create.height = lpbiInput->bmiHeader.biHeight; - switch(xvid_decore(0, XVID_DEC_CREATE, &create, NULL)) + switch(xvid_decore_func(0, XVID_DEC_CREATE, &create, NULL)) { - case XVID_ERR_FAIL : + case XVID_ERR_FAIL : return ICERR_ERROR; case XVID_ERR_MEMORY : @@ -796,17 +943,30 @@ codec->dhandle = create.handle; + RegOpenKeyEx(XVID_REG_KEY, XVID_REG_PARENT "\\" XVID_REG_CHILD, 0, KEY_READ, &hKey); + + REG_GET_N("Deblock_Y", pp_dy, 0) + REG_GET_N("Deblock_UV", pp_duv, 0) + REG_GET_N("Dering", pp_dr, 0) + REG_GET_N("FilmEffect", pp_fe, 0) + + RegCloseKey(hKey); + return ICERR_OK; } LRESULT decompress_end(CODEC * codec) { - if (codec->dhandle != NULL) - { - xvid_decore(codec->dhandle, XVID_DEC_DESTROY, NULL, NULL); - codec->dhandle = NULL; + if (m_hdll != NULL) { + if (codec->dhandle != NULL) { + xvid_decore_func(codec->dhandle, XVID_DEC_DESTROY, NULL, NULL); + codec->dhandle = NULL; + } + FreeLibrary(m_hdll); + m_hdll = NULL; } + return ICERR_OK; } @@ -822,8 +982,15 @@ { xvid_gbl_convert_t convert; - DEBUGFOURCC("input", icd->lpbiInput->biCompression); - DEBUGFOURCC("output", icd->lpbiOutput->biCompression); + DPRINTF("input=%c%c%c%c output=%c%c%c%c", + icd->lpbiInput->biCompression&0xff, + (icd->lpbiInput->biCompression>>8)&0xff, + (icd->lpbiInput->biCompression>>16)&0xff, + (icd->lpbiInput->biCompression>>24)&0xff, + icd->lpbiOutput->biCompression&0xff, + (icd->lpbiOutput->biCompression>>8)&0xff, + (icd->lpbiOutput->biCompression>>16)&0xff, + (icd->lpbiOutput->biCompression>>24)&0xff); memset(&convert, 0, sizeof(convert)); convert.version = XVID_VERSION; @@ -845,7 +1012,7 @@ convert.interlacing = 0; if (convert.input.csp == XVID_CSP_NULL || convert.output.csp == XVID_CSP_NULL || - xvid_global(0, XVID_GBL_CONVERT, &convert, NULL) < 0) + xvid_global_func(0, XVID_GBL_CONVERT, &convert, NULL) < 0) { return ICERR_BADFORMAT; } @@ -853,9 +1020,9 @@ } /* --- yv12 --- */ - memset(&frame, 0, sizeof(frame)); - frame.version = XVID_VERSION; - frame.general = XVID_LOWDELAY; /* force low_delay_default mode */ + memset(&frame, 0, sizeof(frame)); + frame.version = XVID_VERSION; + frame.general = XVID_LOWDELAY; /* force low_delay_default mode */ frame.bitstream = icd->lpInput; frame.length = icd->lpbiInput->biSizeImage; @@ -875,7 +1042,12 @@ frame.output.csp = XVID_CSP_NULL; } - switch (xvid_decore(codec->dhandle, XVID_DEC_DECODE, &frame, NULL)) + if (pp_dy)frame.general |= XVID_DEBLOCKY; + if (pp_duv) frame.general |= XVID_DEBLOCKUV; +/* if (pp_dr) frame.general |= XVID_DERING; */ + if (pp_fe) frame.general |= XVID_FILMEFFECT; + + switch (xvid_decore_func(codec->dhandle, XVID_DEC_DECODE, &frame, NULL)) { case XVID_ERR_FAIL : return ICERR_ERROR; @@ -893,230 +1065,3 @@ return ICERR_OK; } -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_GREYSCALE)) // use only if not already in greyscale - - frame->vop_flags |= XVID_GREYSCALE; - - } else { - - if (!(frame->vop_flags && XVID_GREYSCALE)) // if movie is in greyscale, switch back - - frame->vop_flags |= XVID_GREYSCALE; - - } - - } else { - - if (config->greyscale) - - { - - if ((frame->vop_flags && XVID_GREYSCALE)) // use only if not already in greyscale - - frame->vop_flags |= XVID_GREYSCALE; - - } else { - - if (!(frame->vop_flags && XVID_GREYSCALE)) // if credits is in greyscale, switch back - - frame->vop_flags |= XVID_GREYSCALE; - - } - - } - - return 0; - -} - -// end of koepi's addition -