--- branches/dev-api-4/xvidcore/vfw/src/codec.c 2003/05/17 13:37:49 1032 +++ branches/dev-api-4/xvidcore/vfw/src/codec.c 2004/01/03 12:06:11 1305 @@ -49,11 +49,13 @@ #include #include +#include #include "vfwext.h" #include #include "debug.h" #include "codec.h" +#include "status.h" static const int pmvfast_presets[7] = { @@ -207,7 +209,7 @@ if (lpbiOutput == NULL) { - return sizeof(BITMAPV4HEADER); + return sizeof(BITMAPINFOHEADER); } memcpy(outhdr, inhdr, sizeof(BITMAPINFOHEADER)); @@ -243,7 +245,9 @@ LRESULT compress_frames_info(CODEC * codec, ICCOMPRESSFRAMES * icf) { - //DPRINTF("%i %i", icf->lStartFrame, icf->lFrameCount); +#if 0 + DPRINTF("%i %i", icf->lStartFrame, icf->lFrameCount); +#endif codec->fincr = icf->dwScale; codec->fbase = icf->dwRate; return ICERR_OK; @@ -277,11 +281,17 @@ { xvid_plg_data_t *data = (xvid_plg_data_t *) param1; - DPRINTF("[%5i] type=%c Q:%2i length:%6i", - data->frame_num, - type2char(data->type), - data->quant, - data->length); + /* 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; } } @@ -289,6 +299,51 @@ 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", 0); + 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; +} LRESULT compress_begin(CODEC * codec, BITMAPINFO * lpbiInput, BITMAPINFO * lpbiOutput) @@ -301,9 +356,10 @@ xvid_plugin_2pass2_t pass2; int i; + if (init_dll() != 0) return ICERR_ERROR; /* destroy previously created codec */ if(codec->ehandle) { - xvid_encore(codec->ehandle, XVID_ENC_DESTROY, NULL, NULL); + xvid_encore_func(codec->ehandle, XVID_ENC_DESTROY, NULL, NULL); codec->ehandle = NULL; } @@ -311,12 +367,12 @@ init.version = XVID_VERSION; init.cpu_flags = codec->config.cpu; init.debug = codec->config.debug; - 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; - // zones + /* 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++) { @@ -331,7 +387,7 @@ create.zones[i].base = 100; } - // plugins + /* plugins */ create.plugins = plugins; switch (codec->config.mode) { @@ -342,7 +398,7 @@ 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; + plugins[create.num_plugins].func = xvid_plugin_single_func; plugins[create.num_plugins].param = &single; create.num_plugins++; break; @@ -352,7 +408,7 @@ pass1.version = XVID_VERSION; pass1.filename = codec->config.stats; - plugins[create.num_plugins].func = xvid_plugin_2pass1; + plugins[create.num_plugins].func = xvid_plugin_2pass1_func; plugins[create.num_plugins].param = &pass1; create.num_plugins++; break; @@ -360,10 +416,24 @@ case RC_MODE_2PASS2 : memset(&pass2, 0, sizeof(pass2)); pass2.version = XVID_VERSION; - pass2.bitrate = codec->config.bitrate * CONFIG_KBPS; + 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; - plugins[create.num_plugins].func = xvid_plugin_2pass2; + 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 */ + + plugins[create.num_plugins].func = xvid_plugin_2pass2_func; plugins[create.num_plugins].param = &pass2; create.num_plugins++; break; @@ -376,7 +446,7 @@ } if ((profiles[codec->config.profile].flags & PROFILE_ADAPTQUANT) && codec->config.lum_masking) { - plugins[create.num_plugins].func = xvid_plugin_lumimasking; + plugins[create.num_plugins].func = xvid_plugin_lumimasking_func; plugins[create.num_plugins].param = NULL; create.num_plugins++; } @@ -418,7 +488,7 @@ create.num_threads = codec->config.num_threads; - switch(xvid_encore(0, XVID_ENC_CREATE, &create, NULL)) + switch(xvid_encore_func(0, XVID_ENC_CREATE, &create, NULL)) { case XVID_ERR_FAIL : return ICERR_ERROR; @@ -437,18 +507,29 @@ codec->framenum = 0; codec->keyspacing = 0; + if (codec->config.display_status) { + status_destroy_always(&codec->status); + status_create(&codec->status, codec->fincr, codec->fbase); + } + return ICERR_OK; } LRESULT compress_end(CODEC * codec) { - if (codec->ehandle != NULL) - { - xvid_encore(codec->ehandle, XVID_ENC_DESTROY, NULL, NULL); - codec->ehandle = NULL; + 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; } + if (codec->config.display_status) + status_destroy(&codec->status); + return ICERR_OK; } @@ -458,7 +539,11 @@ int i; for (i=0; inum_zones && config->zones[i].frame <= framenum; i++) ; - i--; + + if (--i < 0) return; /* there are no zones, or we're before the first zone */ + + if (framenum == config->zones[i].frame) + frame->type = config->zones[i].type; if (config->zones[i].greyscale) { frame->vop_flags |= XVID_VOP_GREYSCALE; @@ -503,7 +588,7 @@ } } - if ((profiles[codec->config.profile].flags & PROFILE_REDUCED) && + 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 */ @@ -514,12 +599,29 @@ frame.motion |= XVID_ME_QUARTERPELREFINE16 | XVID_ME_QUARTERPELREFINE8; } - if ((profiles[codec->config.profile].flags & PROFILE_GMC) && codec->config.gmc) + if ((profiles[codec->config.profile].flags & PROFILE_GMC) && codec->config.gmc) { frame.vol_flags |= XVID_VOL_GMC; + frame.motion |= XVID_ME_GME_REFINE; + } if ((profiles[codec->config.profile].flags & PROFILE_INTERLACE) && codec->config.interlacing) frame.vol_flags |= XVID_VOL_INTERLACING; + 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; + } + /* vop stuff */ frame.vop_flags |= XVID_VOP_HALFPEL; @@ -536,39 +638,49 @@ frame.vop_flags |= XVID_VOP_INTER4V; if (codec->config.chromame) - frame.vop_flags |= XVID_ME_CHROMA16 + XVID_ME_CHROMA8; + frame.motion |= XVID_ME_CHROMA_PVOP + XVID_ME_CHROMA_BVOP; + + if (codec->config.cartoon_mode) { + frame.vop_flags |= XVID_VOP_CARTOON; + frame.motion |= XVID_ME_DETECT_STATIC_MOTION; + } + + 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_VOP_MODEDECISION_BITS; + frame.vop_flags |= XVID_VOP_MODEDECISION_RD; break; case VHQ_LIMITED_SEARCH : - frame.vop_flags |= XVID_VOP_MODEDECISION_BITS; - frame.motion |= XVID_ME_HALFPELREFINE16_BITS; - frame.motion |= XVID_ME_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_VOP_MODEDECISION_BITS; - frame.motion |= XVID_ME_HALFPELREFINE16_BITS; - frame.motion |= XVID_ME_HALFPELREFINE8_BITS; - frame.motion |= XVID_ME_QUARTERPELREFINE16_BITS; - frame.motion |= XVID_ME_QUARTERPELREFINE8_BITS; - frame.motion |= XVID_ME_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_VOP_MODEDECISION_BITS; - frame.motion |= XVID_ME_HALFPELREFINE16_BITS; - frame.motion |= XVID_ME_HALFPELREFINE8_BITS; - frame.motion |= XVID_ME_QUARTERPELREFINE16_BITS; - frame.motion |= XVID_ME_QUARTERPELREFINE8_BITS; - frame.motion |= XVID_ME_CHECKPREDICTION_BITS; - frame.motion |= XVID_ME_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 : @@ -597,14 +709,7 @@ // 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) - { - DPRINTF("current frame forced to p-frame"); - frame.type = XVID_TYPE_PVOP; - } /* frame-based stuff */ apply_zone_modifiers(&frame, &codec->config, codec->framenum); @@ -614,7 +719,7 @@ 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 : @@ -630,6 +735,10 @@ return ICERR_UNSUPPORTED; } + if (codec->config.display_status && stats.type>0) { + status_update(&codec->status, stats.type, stats.length, stats.quant); + } + DPRINTF("{type=%i len=%i} length=%i", stats.type, stats.length, length); if (length == 0) /* no encoder output */ @@ -713,7 +822,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 --- */ @@ -745,17 +854,19 @@ xvid_gbl_init_t init; xvid_dec_create_t create; + 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 : return ICERR_ERROR; @@ -778,11 +889,15 @@ 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; } @@ -828,7 +943,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; } @@ -858,7 +973,7 @@ frame.output.csp = XVID_CSP_NULL; } - switch (xvid_decore(codec->dhandle, XVID_DEC_DECODE, &frame, NULL)) + switch (xvid_decore_func(codec->dhandle, XVID_DEC_DECODE, &frame, NULL)) { case XVID_ERR_FAIL : return ICERR_ERROR;