--- trunk/xvidcore/vfw/src/codec.c 2004/07/11 08:54:38 1487 +++ trunk/xvidcore/vfw/src/codec.c 2010/12/02 06:46:07 1910 @@ -3,6 +3,8 @@ * XVID VFW FRONTEND * codec * + * Copyright(C) Peter Ross + * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or @@ -17,33 +19,7 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * - *************************************************************************/ - -/************************************************************************** - * - * History: - * - * 12.07.2002 num_threads - * 23.06.2002 XVID_CPU_CHKONLY; loading speed up - * 25.04.2002 ICDECOMPRESS_PREROLL - * 17.04.2002 re-enabled lumi masking for 1st pass - * 15.04.2002 updated cbr support - * 04.04.2002 separated 2-pass code to 2pass.c - * interlacing support - * hinted ME support - * 23.03.2002 daniel smith - * changed inter4v to only be in modes 5 or 6 - * fixed null mode crash ? - * merged foxer's alternative 2-pass code - * added DEBUGERR output on errors instead of returning - * 16.03.2002 daniel smith - * changed BITMAPV4HEADER to BITMAPINFOHEADER - * - prevents memcpy crash in compress_get_format() - * credits are processed in external 2pass mode - * motion search precision = 0 now effective in 2-pass - * modulated quantization - * added DX50 fourcc - * 01.12.2001 inital version; (c)2001 peter ross + * $Id: codec.c,v 1.27 2010-12-02 06:46:07 Isibaar Exp $ * *************************************************************************/ @@ -221,7 +197,7 @@ outhdr->biClrUsed = 0; outhdr->biClrImportant = 0; - if (codec->config.fourcc_used == 0) + if ((codec->config.fourcc_used == 0) || (profiles[codec->config.profile].flags & PROFILE_XVID)) { outhdr->biCompression = FOURCC_XVID; } @@ -421,8 +397,12 @@ xvid_plugin_single_t single; xvid_plugin_2pass1_t pass1; xvid_plugin_2pass2_t pass2; + xvid_plugin_lumimasking_t masking; + xvid_gbl_info_t info; int i; HANDLE hFile; + const quality_t* quality_preset = (codec->config.quality==quality_table_num) ? + &codec->config.quality_user : &quality_table[codec->config.quality]; 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)); @@ -440,6 +420,10 @@ init.debug = codec->config.debug; codec->xvid_global_func(0, XVID_GBL_INIT, &init, NULL); + memset(&info, 0, sizeof(info)); + info.version = XVID_VERSION; + codec->xvid_global_func(0, XVID_GBL_INFO, &info, NULL); + memset(&create, 0, sizeof(create)); create.version = XVID_VERSION; @@ -502,6 +486,12 @@ pass2.kfthreshold = codec->config.kfthreshold; pass2.container_frame_overhead = 24; /* AVI */ + /* VBV */ + pass2.vbv_size = profiles[codec->config.profile].max_vbv_size; + pass2.vbv_initial = (profiles[codec->config.profile].max_vbv_size*3)/4; /* 75% */ + pass2.vbv_maxrate = profiles[codec->config.profile].max_bitrate; + pass2.vbv_peakrate = profiles[codec->config.profile].vbv_peakrate; + plugins[create.num_plugins].func = codec->xvid_plugin_2pass2_func; plugins[create.num_plugins].param = &pass2; create.num_plugins++; @@ -530,9 +520,11 @@ } /* lumimasking plugin */ - if ((profiles[codec->config.profile].flags & PROFILE_ADAPTQUANT) && codec->config.lum_masking) { + if ((profiles[codec->config.profile].flags & PROFILE_ADAPTQUANT) && (codec->config.lum_masking>0)) { + memset(&masking, 0, sizeof(masking)); + masking.method = (codec->config.lum_masking==2); plugins[create.num_plugins].func = codec->xvid_plugin_lumimasking_func; - plugins[create.num_plugins].param = NULL; + plugins[create.num_plugins].param = &masking; create.num_plugins++; } @@ -547,31 +539,53 @@ create.fincr = codec->fincr; create.fbase = codec->fbase; - create.max_key_interval = codec->config.max_key_interval; + create.max_key_interval = quality_preset->max_key_interval; - 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; + create.min_quant[0] = quality_preset->min_iquant; + create.max_quant[0] = quality_preset->max_iquant; + create.min_quant[1] = quality_preset->min_pquant; + create.max_quant[1] = quality_preset->max_pquant; + create.min_quant[2] = quality_preset->min_bquant; + create.max_quant[2] = quality_preset->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; - - 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; + /* dxn: prevent bframes usage if interlacing is selected */ + if (!((profiles[codec->config.profile].flags & PROFILE_EXTRA) && codec->config.interlacing)) { + create.max_bframes = codec->config.max_bframes; + create.bquant_ratio = codec->config.bquant_ratio; + create.bquant_offset = codec->config.bquant_offset; + + if (codec->config.packed) + create.global |= XVID_GLOBAL_PACKED; + + create.global |= XVID_GLOBAL_CLOSED_GOP; + + /* restrict max bframes */ + if ((create.max_bframes > profiles[codec->config.profile].xvid_max_bframes) && (profiles[codec->config.profile].xvid_max_bframes >= 0)) + create.max_bframes = profiles[codec->config.profile].xvid_max_bframes; + + /* DXN: enable packed bframes */ + if ((profiles[codec->config.profile].flags & PROFILE_PACKED)) { + create.global |= XVID_GLOBAL_PACKED; + } + } + } + + /* dxn: always write divx5 userdata */ + if ((profiles[codec->config.profile].flags & PROFILE_EXTRA)) + create.global |= XVID_GLOBAL_DIVX5_USERDATA; + + create.frame_drop_ratio = quality_preset->frame_drop_ratio; + + /* Encoder threads */ + if (codec->config.num_threads == 0) + create.num_threads = info.num_threads; /* Autodetect */ + else if (codec->config.num_threads == 1) + create.num_threads = -1; /* Single-threaded, disable SMP */ + else + create.num_threads = codec->config.num_threads; - create.num_threads = codec->config.num_threads; switch(codec->xvid_encore_func(0, XVID_ENC_CREATE, &create, NULL)) { @@ -604,6 +618,9 @@ LRESULT compress_end(CODEC * codec) { + if (codec==NULL) + return ICERR_OK; + if (codec->m_hdll != NULL) { if (codec->ehandle != NULL) { codec->xvid_encore_func(codec->ehandle, XVID_ENC_DESTROY, NULL, NULL); @@ -637,12 +654,19 @@ frame->vop_flags |= XVID_VOP_CHROMAOPT; } + if (config->zones[i].cartoon_mode) { + frame->vop_flags |= XVID_VOP_CARTOON; + frame->motion |= XVID_ME_DETECT_STATIC_MOTION; + } + if ((profiles[config->profile].flags & PROFILE_BVOP) && config->use_bvop) { frame->bframe_threshold = config->zones[i].bvop_threshold; } } +#define CALC_BI_STRIDE(width,bitcount) ((((width * bitcount) + 31) & ~31) >> 3) + LRESULT compress(CODEC * codec, ICCOMPRESS * icc) { BITMAPINFOHEADER * inhdr = icc->lpbiInput; @@ -650,6 +674,8 @@ xvid_enc_frame_t frame; xvid_enc_stats_t stats; int length; + const quality_t* quality_preset = (codec->config.quality==quality_table_num) ? + &codec->config.quality_user : &quality_table[codec->config.quality]; memset(&frame, 0, sizeof(frame)); frame.version = XVID_VERSION; @@ -672,12 +698,6 @@ } } - 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 ((profiles[codec->config.profile].flags & PROFILE_QPEL) && codec->config.qpel) { frame.vol_flags |= XVID_VOL_QUARTERPEL; frame.motion |= XVID_ME_QUARTERPELREFINE16 | XVID_ME_QUARTERPELREFINE8; @@ -691,7 +711,10 @@ if ((profiles[codec->config.profile].flags & PROFILE_INTERLACE) && codec->config.interlacing) frame.vol_flags |= XVID_VOL_INTERLACING; - if (codec->config.ar_mode == 0) { /* PAR */ + /* dxn: force 1:1 picture aspect ration */ + if ((profiles[codec->config.profile].flags & PROFILE_EXTRA)) { + frame.par = XVID_PAR_11_VGA; + } else if (codec->config.ar_mode == 0) { /* PAR */ if (codec->config.display_aspect_ratio != 5) { frame.par = codec->config.display_aspect_ratio + 1; } else { @@ -718,29 +741,29 @@ if (codec->config.vop_debug) frame.vop_flags |= XVID_VOP_DEBUG; - if (codec->config.trellis_quant) { + if (quality_preset->trellis_quant) { frame.vop_flags |= XVID_VOP_TRELLISQUANT; } - if (codec->config.motion_search > 4) - frame.vop_flags |= XVID_VOP_INTER4V; + if ((profiles[codec->config.profile].flags & PROFILE_4MV)) { + if (quality_preset->motion_search > 4) + frame.vop_flags |= XVID_VOP_INTER4V; + } - if (codec->config.chromame) + if (quality_preset->chromame) 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) + if (quality_preset->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]; + frame.motion |= pmvfast_presets[quality_preset->motion_search]; + + if (quality_preset->vhq_bframe) frame.vop_flags |= XVID_VOP_RD_BVOP; + - switch (codec->config.vhq_mode) + switch (quality_preset->vhq_mode) { case VHQ_MODE_DECISION : frame.vop_flags |= XVID_VOP_MODEDECISION_RD; @@ -776,7 +799,7 @@ } frame.input.plane[0] = icc->lpInput; - frame.input.stride[0] = (((icc->lpbiInput->biWidth * icc->lpbiInput->biBitCount) + 31) & ~31) >> 3; + frame.input.stride[0] = CALC_BI_STRIDE(icc->lpbiInput->biWidth, icc->lpbiInput->biBitCount); if ((frame.input.csp = get_colorspace(inhdr)) == XVID_CSP_NULL) return ICERR_BADFORMAT; @@ -798,7 +821,7 @@ } // force keyframe spacing in 2-pass 1st pass - if (codec->config.motion_search == 0) + if (quality_preset->motion_search == 0) frame.type = XVID_TYPE_IVOP; /* frame-based stuff */ @@ -929,7 +952,8 @@ outhdr->biPlanes = 1; outhdr->biBitCount = 24; outhdr->biCompression = BI_RGB; /* sonic foundry vegas video v3 only supports BI_RGB */ - outhdr->biSizeImage = outhdr->biWidth * outhdr->biHeight * outhdr->biBitCount / 8; + outhdr->biSizeImage = outhdr->biHeight * CALC_BI_STRIDE(outhdr->biWidth, outhdr->biBitCount); + outhdr->biXPelsPerMeter = 0; outhdr->biYPelsPerMeter = 0; outhdr->biClrUsed = 0; @@ -948,6 +972,7 @@ LRESULT decompress_begin(CODEC * codec, BITMAPINFO * lpbiInput, BITMAPINFO * lpbiOutput) { + BITMAPINFOHEADER * inhdr = &lpbiInput->bmiHeader; xvid_gbl_init_t init; xvid_dec_create_t create; HKEY hKey; @@ -957,12 +982,14 @@ memset(&init, 0, sizeof(init)); init.version = XVID_VERSION; init.cpu_flags = codec->config.cpu; + init.debug = codec->config.debug; codec->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; + create.fourcc = inhdr->biCompression; switch(codec->xvid_decore_func(0, XVID_DEC_CREATE, &create, NULL)) { @@ -984,11 +1011,11 @@ RegOpenKeyEx(XVID_REG_KEY, XVID_REG_PARENT "\\" XVID_REG_CHILD, 0, KEY_READ, &hKey); REG_GET_N("Brightness", pp_brightness, 0); - REG_GET_N("Deblock_Y", pp_dy, 0) - REG_GET_N("Deblock_UV", pp_duv, 0) - REG_GET_N("Dering_Y", pp_dry, 0) - REG_GET_N("Dering_UV", pp_druv, 0) - REG_GET_N("FilmEffect", pp_fe, 0) + REG_GET_N("Deblock_Y", pp_dy, 0); + REG_GET_N("Deblock_UV", pp_duv, 0); + REG_GET_N("Dering_Y", pp_dry, 0); + REG_GET_N("Dering_UV", pp_druv, 0); + REG_GET_N("FilmEffect", pp_fe, 0); RegCloseKey(hKey); @@ -1035,13 +1062,13 @@ convert.input.csp = get_colorspace(icd->lpbiInput); convert.input.plane[0] = icd->lpInput; - convert.input.stride[0] = (((icd->lpbiInput->biWidth *icd->lpbiInput->biBitCount) + 31) & ~31) >> 3; + convert.input.stride[0] = CALC_BI_STRIDE(icd->lpbiInput->biWidth, icd->lpbiInput->biBitCount); if (convert.input.csp == XVID_CSP_I420 || convert.input.csp == XVID_CSP_YV12) convert.input.stride[0] = (convert.input.stride[0]*2)/3; convert.output.csp = get_colorspace(icd->lpbiOutput); convert.output.plane[0] = icd->lpOutput; - convert.output.stride[0] = (((icd->lpbiOutput->biWidth *icd->lpbiOutput->biBitCount) + 31) & ~31) >> 3; + convert.output.stride[0] = CALC_BI_STRIDE(icd->lpbiOutput->biWidth, icd->lpbiOutput->biBitCount); if (convert.output.csp == XVID_CSP_I420 || convert.output.csp == XVID_CSP_YV12) convert.output.stride[0] = (convert.output.stride[0]*2)/3; @@ -1071,9 +1098,9 @@ return ICERR_BADFORMAT; } frame.output.plane[0] = icd->lpOutput; - frame.output.stride[0] = (((icd->lpbiOutput->biWidth * icd->lpbiOutput->biBitCount) + 31) & ~31) >> 3; + frame.output.stride[0] = CALC_BI_STRIDE(icd->lpbiOutput->biWidth, icd->lpbiOutput->biBitCount); if (frame.output.csp == XVID_CSP_I420 || frame.output.csp == XVID_CSP_YV12) - frame.output.stride[0] = (frame.output.stride[0]*2)/3; + frame.output.stride[0] = CALC_BI_STRIDE(icd->lpbiOutput->biWidth, 8); } else {