--- trunk/vfw/src/codec.c 2002/06/23 03:59:49 235 +++ branches/dev-api-3/vfw/src/codec.c 2002/11/19 13:23:17 660 @@ -23,6 +23,7 @@ * * 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 @@ -42,7 +43,7 @@ * motion search precision = 0 now effective in 2-pass * modulated quantization * added DX50 fourcc - * 01.12.2001 inital version; (c)2001 peter ross + * 01.12.2001 inital version; (c)2001 peter ross * *************************************************************************/ @@ -53,11 +54,11 @@ #include "2pass.h" int pmvfast_presets[7] = { - 0, PMV_QUICKSTOP16, PMV_EARLYSTOP16, PMV_EARLYSTOP16 | PMV_EARLYSTOP8, - PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 | PMV_EARLYSTOP8 | PMV_HALFPELDIAMOND8, - PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 | PMV_EARLYSTOP8 | PMV_HALFPELDIAMOND8, - PMV_EARLYSTOP16 | PMV_HALFPELREFINE16 | PMV_EXTSEARCH16 | - PMV_EARLYSTOP8 | PMV_HALFPELREFINE8 | PMV_HALFPELDIAMOND8 + 0, PMV_QUICKSTOP16, 0, 0, + 0 | PMV_HALFPELREFINE16 | PMV_HALFPELDIAMOND8, + 0 | PMV_HALFPELREFINE16 | PMV_HALFPELDIAMOND8 | + PMV_ADVANCEDDIAMOND16, PMV_HALFPELREFINE16 | PMV_EXTSEARCH16 | + PMV_HALFPELREFINE8 | PMV_HALFPELDIAMOND8 | PMV_USESQUARES16 }; /* return xvid compatbile colorspace, @@ -66,83 +67,87 @@ int get_colorspace(BITMAPINFOHEADER * hdr) { - if (hdr->biHeight < 0) - { - DEBUGERR("colorspace: inverted input format not supported"); - return XVID_CSP_NULL; - } - + /* rgb only: negative height specifies top down image */ + int rgb_flip = (hdr->biHeight < 0 ? 0 : XVID_CSP_VFLIP); + switch(hdr->biCompression) { case BI_RGB : if (hdr->biBitCount == 16) { DEBUG("RGB16 (RGB555)"); - return XVID_CSP_VFLIP | XVID_CSP_RGB555; + return rgb_flip | XVID_CSP_RGB555; } if (hdr->biBitCount == 24) { DEBUG("RGB24"); - return XVID_CSP_VFLIP | XVID_CSP_RGB24; + return rgb_flip | XVID_CSP_RGB24; } if (hdr->biBitCount == 32) { DEBUG("RGB32"); - return XVID_CSP_VFLIP | XVID_CSP_RGB32; + return rgb_flip | XVID_CSP_RGB32; } - DEBUG1("BI_RGB unsupported", hdr->biBitCount); + DEBUG1("unsupported BI_RGB biBitCount", hdr->biBitCount); return XVID_CSP_NULL; -// how do these work in BITMAPINFOHEADER ??? -/* case BI_BITFIELDS : - if (hdr->biBitCount == 16 - if(hdr->biBitCount == 16 && - hdr->bV4RedMask == 0x7c00 && - hdr->bV4GreenMask == 0x3e0 && - hdr->bV4BlueMask == 0x1f) - { - DEBUG("RGB555"); - return XVID_CSP_VFLIP | XVID_CSP_RGB555; - } - if(hdr->bV4BitCount == 16 && - hdr->bV4RedMask == 0xf800 && - hdr->bV4GreenMask == 0x7e0 && - hdr->bV4BlueMask == 0x1f) + case BI_BITFIELDS : + if (hdr->biSize >= sizeof(BITMAPV4HEADER)) { - DEBUG("RGB565"); - return XVID_CSP_VFLIP | XVID_CSP_RGB565; + BITMAPV4HEADER * hdr4 = (BITMAPV4HEADER *)hdr; + + if (hdr4->bV4BitCount == 16 && + hdr4->bV4RedMask == 0x7c00 && + hdr4->bV4GreenMask == 0x3e0 && + hdr4->bV4BlueMask == 0x1f) + { + DEBUG("RGB555"); + return rgb_flip | XVID_CSP_RGB555; + } + + if(hdr4->bV4BitCount == 16 && + hdr4->bV4RedMask == 0xf800 && + hdr4->bV4GreenMask == 0x7e0 && + hdr4->bV4BlueMask == 0x1f) + { + DEBUG("RGB565"); + return rgb_flip | XVID_CSP_RGB565; + } + + DEBUG("unsupported BI_BITFIELDS mode"); + return XVID_CSP_NULL; } - DEBUG1("BI_FIELDS unsupported", hdr->bV4BitCount); + DEBUG("unsupported BI_BITFIELDS/BITMAPHEADER combination"); return XVID_CSP_NULL; -*/ - case FOURCC_I420: - case FOURCC_IYUV: + + case FOURCC_I420 : + case FOURCC_IYUV : DEBUG("IYUY"); return XVID_CSP_I420; case FOURCC_YV12 : DEBUG("YV12"); return XVID_CSP_YV12; - + case FOURCC_YUYV : case FOURCC_YUY2 : - case FOURCC_V422 : DEBUG("YUY2"); return XVID_CSP_YUY2; - case FOURCC_YVYU: + case FOURCC_YVYU : DEBUG("YVYU"); return XVID_CSP_YVYU; - case FOURCC_UYVY: + case FOURCC_UYVY : DEBUG("UYVY"); return XVID_CSP_UYVY; + default : + DEBUGFOURCC("unsupported colorspace", hdr->biCompression); + return XVID_CSP_NULL; } - DEBUGFOURCC("colorspace: unknown", hdr->biCompression); - return XVID_CSP_NULL; } @@ -167,7 +172,7 @@ } if (inhdr->biWidth != outhdr->biWidth || inhdr->biHeight != outhdr->biHeight || - (outhdr->biCompression != FOURCC_XVID && outhdr->biCompression != FOURCC_DIVX)) + (outhdr->biCompression != FOURCC_XVID && outhdr->biCompression != FOURCC_DIVX && outhdr->biCompression != FOURCC_DX50)) { return ICERR_BADFORMAT; } @@ -193,7 +198,6 @@ memcpy(outhdr, inhdr, sizeof(BITMAPINFOHEADER)); outhdr->biSize = sizeof(BITMAPINFOHEADER); - outhdr->biBitCount = 24; // or 16 outhdr->biSizeImage = compress_get_size(codec, lpbiInput, lpbiOutput); outhdr->biXPelsPerMeter = 0; outhdr->biYPelsPerMeter = 0; @@ -294,6 +298,10 @@ param.max_quantizer = codec->config.max_pquant; param.max_key_interval = codec->config.max_key_interval; +#ifdef _SMP + param.num_threads = codec->config.num_threads; +#endif + #ifdef BFRAMES param.global = 0; if (codec->config.packed) param.global |= XVID_GLOBAL_PACKED; @@ -301,6 +309,8 @@ if (codec->config.debug) param.global |= XVID_GLOBAL_DEBUG; param.max_bframes = codec->config.max_bframes; param.bquant_ratio = codec->config.bquant_ratio; + param.bquant_offset = codec->config.bquant_offset; + param.frame_drop_ratio = codec->config.frame_drop_ratio; #endif switch(xvid_encore(0, XVID_ENC_CREATE, ¶m, NULL)) @@ -379,6 +389,8 @@ frame.intra = -1; frame.general |= XVID_HALFPEL; +// frame.general |= XVID_ME_EPZS; + if (codec->config.motion_search > 4) frame.general |= XVID_INTER4V; @@ -388,6 +400,26 @@ if (codec->config.interlacing) frame.general |= XVID_INTERLACING; + + if (codec->config.qpel) { + frame.general |= XVID_QUARTERPEL; + frame.motion |= PMV_QUARTERPELREFINE16 | PMV_QUARTERPELREFINE8; + + } + + if (codec->config.gmc) + frame.general |= XVID_GMC; + + if (codec->config.chromame) + frame.general |= XVID_ME_COLOUR; + +// added by koepi for credits greyscale + + check_greyscale_mode(&codec->config, &frame, codec->framenum); + +// end of koepi's addition + + // 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)) { @@ -427,9 +459,11 @@ } } - frame.motion = pmvfast_presets[codec->config.motion_search]; + frame.motion |= pmvfast_presets[codec->config.motion_search]; frame.image = icc->lpInput; + // dev-api-3 + frame.stride = (((icc->lpbiInput->biWidth * icc->lpbiInput->biBitCount) + 31) & ~31) >> 3; if ((frame.colorspace = get_colorspace(inhdr)) == XVID_CSP_NULL) return ICERR_BADFORMAT; @@ -510,7 +544,11 @@ frame.intra = 0; } - OutputDebugString(" "); +#ifdef BFRAMES + frame.bquant = 0; +#endif + +// OutputDebugString(" "); switch (xvid_encore(codec->ehandle, XVID_ENC_ENCODE, &frame, &stats)) { case XVID_ERR_FAIL : @@ -523,7 +561,7 @@ return ICERR_BADFORMAT; } - if (frame.intra) + if (frame.intra==1) { codec->keyspacing = 0; *icc->lpdwFlags = AVIIF_KEYFRAME; @@ -562,7 +600,9 @@ } } - codec_2pass_update(codec, &frame, &stats); +//quick fix for delayed frames +// if (frame.intra != 5) + codec_2pass_update(codec, &frame, &stats); ++codec->framenum; ++codec->keyspacing; @@ -584,7 +624,7 @@ return ICERR_ERROR; } - if (inhdr->biCompression != FOURCC_XVID && inhdr->biCompression != FOURCC_DIVX) + if (inhdr->biCompression != FOURCC_XVID && inhdr->biCompression != FOURCC_DIVX && inhdr->biCompression != FOURCC_DX50 && get_colorspace(inhdr) == XVID_CSP_NULL) { return ICERR_BADFORMAT; } @@ -616,6 +656,15 @@ return sizeof(BITMAPINFOHEADER); } + /* --- yv12 --- */ + + if (get_colorspace(inhdr) != XVID_CSP_NULL) { + memcpy(outhdr, inhdr, sizeof(BITMAPINFOHEADER)); + // XXX: should we set outhdr->biSize ?? + return ICERR_OK; + } + /* --- yv12 --- */ + result = decompress_query(codec, lpbiInput, lpbiOutput); if (result != ICERR_OK) { @@ -687,7 +736,37 @@ frame.length = icd->lpbiInput->biSizeImage; frame.image = icd->lpOutput; - frame.stride = icd->lpbiOutput->biWidth; +// frame.stride = icd->lpbiOutput->biWidth; + // dev-api-3: + frame.stride = (((icd->lpbiOutput->biWidth * icd->lpbiOutput->biBitCount) + 31) & ~31) >> 3; + + /* --- yv12 --- */ + if (icd->lpbiInput->biCompression != FOURCC_XVID && + icd->lpbiInput->biCompression != FOURCC_DIVX && + icd->lpbiInput->biCompression != FOURCC_DX50) + { + XVID_INIT_CONVERTINFO convert; + DEBUGFOURCC("input", icd->lpbiInput->biCompression); + DEBUGFOURCC("output", icd->lpbiOutput->biCompression); + convert.input.colorspace = get_colorspace(icd->lpbiInput); + convert.input.y = icd->lpInput; + convert.input.y_stride = (((icd->lpbiInput->biWidth *icd->lpbiInput->biBitCount) + 31) & ~31) >> 3; + convert.output.colorspace = get_colorspace(icd->lpbiOutput); + convert.output.y = icd->lpOutput; + convert.output.y_stride = (((icd->lpbiOutput->biWidth *icd->lpbiOutput->biBitCount) + 31) & ~31) >> 3; + convert.width = icd->lpbiInput->biWidth; + convert.height = icd->lpbiInput->biHeight; + convert.interlacing = 0; + if (convert.input.colorspace == XVID_CSP_NULL || + convert.output.colorspace == XVID_CSP_NULL || + xvid_init(NULL, XVID_INIT_CONVERT, &convert, NULL) != XVID_ERR_OK) + { + return ICERR_BADFORMAT; + } + return ICERR_OK; + } + /* --- yv12 --- */ + if (~((icd->dwFlags & ICDECOMPRESS_HURRYUP) | (icd->dwFlags & ICDECOMPRESS_UPDATE) | (icd->dwFlags & ICDECOMPRESS_PREROLL))) { @@ -723,6 +802,12 @@ 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 : @@ -740,6 +825,12 @@ } 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; @@ -747,6 +838,12 @@ 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 : @@ -766,11 +863,23 @@ } 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)) @@ -861,3 +970,55 @@ return quant; } +// added by koepi for credits greyscale + +int check_greyscale_mode(CONFIG* config, XVID_ENC_FRAME* frame, int framenum) + +{ + + if ((codec_is_in_credits(config, framenum)) && (config->mode!=DLG_MODE_CBR)) + + { + + if (config->credits_greyscale) + + { + + if ((frame->general && XVID_GREYSCALE)) // use only if not already in greyscale + + frame->general |= XVID_GREYSCALE; + + } else { + + if (!(frame->general && XVID_GREYSCALE)) // if movie is in greyscale, switch back + + frame->general |= XVID_GREYSCALE; + + } + + } else { + + if (config->greyscale) + + { + + if ((frame->general && XVID_GREYSCALE)) // use only if not already in greyscale + + frame->general |= XVID_GREYSCALE; + + } else { + + if (!(frame->general && XVID_GREYSCALE)) // if credits is in greyscale, switch back + + frame->general |= XVID_GREYSCALE; + + } + + } + + return 0; + +} + +// end of koepi's addition +