--- branches/dev-api-4/xvidcore/vfw/src/codec.c 2003/03/26 14:56:49 949 +++ branches/dev-api-4/xvidcore/vfw/src/codec.c 2003/05/16 17:15:26 1028 @@ -49,11 +49,14 @@ #include #include +#include "vfwext.h" +#include +#include "debug.h" #include "codec.h" -#include "2pass.h" -int pmvfast_presets[7] = { + +static const int pmvfast_presets[7] = { 0, 0, 0, 0, 0 | XVID_ME_HALFPELREFINE16 | 0, 0 | XVID_ME_HALFPELREFINE16 | 0 | @@ -61,6 +64,8 @@ XVID_ME_HALFPELREFINE8 | 0 | XVID_ME_USESQUARES16 }; + + /* return xvid compatbile colorspace, or XVID_CSP_NULL if failure */ @@ -75,21 +80,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 +107,7 @@ hdr4->bV4GreenMask == 0x3e0 && hdr4->bV4BlueMask == 0x1f) { - DEBUG("RGB555"); + DPRINTF("RGB555"); return rgb_flip | XVID_CSP_RGB555; } @@ -111,41 +116,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 +170,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; @@ -229,70 +243,70 @@ LRESULT compress_frames_info(CODEC * codec, ICCOMPRESSFRAMES * icf) { - // DEBUG2("frate fscale", codec->frate, codec->fscale); + //DPRINTF("%i %i", icf->lStartFrame, icf->lFrameCount); codec->fincr = icf->dwScale; codec->fbase = icf->dwRate; return ICERR_OK; } -LRESULT compress_begin(CODEC * codec, BITMAPINFO * lpbiInput, BITMAPINFO * lpbiOutput) +const char type2char(int type) { - xvid_gbl_init_t init; - xvid_enc_create_t create; - xvid_enc_rc_t rc; - xvid_enc_plugin_t plugins[1]; + if (type==XVID_TYPE_IVOP) + return 'I'; + if (type==XVID_TYPE_PVOP) + return 'P'; + if (type==XVID_TYPE_BVOP) + return 'B'; + return 'S'; +} - memset(&rc, 0, sizeof(rc)); - rc.version = XVID_VERSION; +int vfw_debug(void *handle, + int opt, + void *param1, + void *param2) +{ + switch (opt) { + case XVID_PLG_INFO: + case XVID_PLG_CREATE: + case XVID_PLG_DESTROY: + case XVID_PLG_BEFORE: + return 0; - 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 XVID_PLG_AFTER: + { + xvid_plg_data_t *data = (xvid_plg_data_t *) param1; - case DLG_MODE_VBR_QUAL : - codec->config.fquant = 0; - //param.rc_bitrate = 0; - break; + DPRINTF("[%5i] type=%c Q:%2i length:%6i", + data->frame_num, + type2char(data->type), + data->quant, + data->length); + return 0; + } + } - case DLG_MODE_VBR_QUANT : - codec->config.fquant = (float) codec->config.quant; - //param.rc_bitrate = 0; - break; + return XVID_ERR_FAIL; +} - 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)); - break; - case DLG_MODE_NULL : - return ICERR_OK; - default : - break; - } +LRESULT compress_begin(CODEC * codec, BITMAPINFO * lpbiInput, BITMAPINFO * lpbiOutput) +{ + xvid_gbl_init_t init; + xvid_enc_create_t create; + xvid_enc_plugin_t plugins[3]; + xvid_plugin_single_t single; + xvid_plugin_2pass1_t pass1; + xvid_plugin_2pass2_t pass2; + int i; /* destroy previously created codec */ - if(codec->ehandle) - { + 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; @@ -301,37 +315,108 @@ memset(&create, 0, sizeof(create)); create.version = XVID_VERSION; - create.width = lpbiInput->bmiHeader.biWidth; - create.height = lpbiInput->bmiHeader.biHeight; - create.fincr = codec->fincr; - create.fbase = codec->fbase; + // 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 (create.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; + } + + // plugins + create.plugins = plugins; + switch (codec->config.mode) + { + case RC_MODE_1PASS : + memset(&single, 0, sizeof(single)); + single.version = XVID_VERSION; + single.bitrate = codec->config.bitrate; + 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].param = &single; + create.num_plugins++; + + 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; + plugins[create.num_plugins].param = &pass1; + create.num_plugins++; + break; + + case RC_MODE_2PASS2 : + memset(&pass2, 0, sizeof(pass2)); + pass2.version = XVID_VERSION; + pass2.bitrate = codec->config.bitrate; + pass2.filename = codec->config.stats; + + plugins[create.num_plugins].func = xvid_plugin_2pass2; + plugins[create.num_plugins].param = &pass2; + create.num_plugins++; + break; - create.plugins = plugins; - create.num_plugins = 0; - if (codec->config.lum_masking) { + case RC_MODE_NULL : + return ICERR_OK; + + default : + break; + } + + if ((profiles[codec->config.profile].flags & PROFILE_ADAPTQUANT) && codec->config.lum_masking) { plugins[create.num_plugins].func = xvid_plugin_lumimasking; plugins[create.num_plugins].param = NULL; create.num_plugins++; - } + } - if (codec->config.packed) - create.global |= XVID_GLOBAL_PACKED; - if (codec->config.dx50bvop) - create.global |= XVID_GLOBAL_CLOSED_GOP; + plugins[create.num_plugins].func = vfw_debug; + plugins[create.num_plugins].param = NULL; + create.num_plugins++; + + 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; 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_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; + + if (codec->config.packed) + create.global |= XVID_GLOBAL_PACKED; - create.bquant_ratio = codec->config.bquant_ratio; - create.bquant_offset = codec->config.bquant_offset; + if (codec->config.closed_gov) + create.global |= XVID_GLOBAL_CLOSED_GOP; - create.num_threads = codec->config.num_threads; + } + + 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(0, XVID_ENC_CREATE, &create, NULL)) { case XVID_ERR_FAIL : return ICERR_ERROR; @@ -350,9 +435,6 @@ codec->framenum = 0; codec->keyspacing = 0; - codec->twopass.hints = codec->twopass.stats1 = codec->twopass.stats2 = INVALID_HANDLE_VALUE; - codec->twopass.hintstream = NULL; - return ICERR_OK; } @@ -362,32 +444,34 @@ 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); - } - codec->ehandle = NULL; - - codec_2pass_finish(codec); } 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++) ; + i--; + + if (config->zones[i].greyscale) { + frame->vop_flags |= XVID_VOP_GREYSCALE; + } + + 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; @@ -396,15 +480,6 @@ 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; @@ -412,38 +487,35 @@ /* 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_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) { + 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) { + 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) + if ((profiles[codec->config.profile].flags & PROFILE_GMC) && codec->config.gmc) frame.vol_flags |= XVID_VOL_GMC; - if (codec->config.interlacing) + if ((profiles[codec->config.profile].flags & PROFILE_INTERLACE) && codec->config.interlacing) frame.vol_flags |= XVID_VOL_INTERLACING; /* vop stuff */ @@ -454,58 +526,16 @@ if (codec->config.debug) frame.vop_flags |= XVID_VOP_DEBUG; - if (codec->config.motion_search > 4) + if (codec->config.trellis_quant) { + frame.vop_flags |= XVID_VOP_TRELLISQUANT; + } + + if (codec->config.motion_search > 4) frame.vop_flags |= XVID_VOP_INTER4V; if (codec->config.chromame) frame.vop_flags |= XVID_ME_CHROMA16 + XVID_ME_CHROMA8; - if (codec->config.chroma_opt) - frame.vop_flags |= XVID_VOP_CHROMAOPT; - - check_greyscale_mode(&codec->config, &frame, codec->framenum); - -/* XXX: hinted me - -// 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.hinted_me && codec->config.mode == DLG_MODE_2PASS_1) - { - frame.hint.hintstream = codec->twopass.hintstream; - frame.hint.rawhints = 0; - frame.general |= XVID_HINTEDME_GET; - } - 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; - } - } */ - frame.motion |= pmvfast_presets[codec->config.motion_search]; switch (codec->config.vhq_mode) @@ -555,45 +585,13 @@ 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; + frame.quant = 0; - 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; - - 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) @@ -602,10 +600,15 @@ } else if (codec->keyspacing < codec->config.min_key_interval && codec->framenum) { - DEBUG("current frame forced to p-frame"); + DPRINTF("current frame forced to p-frame"); frame.type = XVID_TYPE_PVOP; } + /* frame-based stuff */ + apply_zone_modifiers(&frame, &codec->config, codec->framenum); + + /* call encore */ + memset(&stats, 0, sizeof(stats)); stats.version = XVID_VERSION; @@ -625,11 +628,7 @@ return ICERR_UNSUPPORTED; } - { /* XXX: debug text */ - char tmp[100]; - wsprintf(tmp, " {type=%i len=%i} length=%i", stats.type, stats.length, length); - OutputDebugString(tmp); - } + DPRINTF("{type=%i len=%i} length=%i", stats.type, stats.length, length); if (length == 0) /* no encoder output */ { @@ -650,43 +649,14 @@ outhdr->biSizeImage = length; - if (codec->config.mode == DLG_MODE_2PASS_1 && codec->config.discard1pass) + if (codec->config.mode == RC_MODE_2PASS1 && 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->framenum; - ++codec->keyspacing; + codec->framenum++; + codec->keyspacing++; return ICERR_OK; } @@ -826,8 +796,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; @@ -897,230 +874,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_VOP_GREYSCALE)) // use only if not already in greyscale - - frame->vop_flags |= XVID_VOP_GREYSCALE; - - } else { - - if (!(frame->vop_flags && XVID_VOP_GREYSCALE)) // if movie is in greyscale, switch back - - frame->vop_flags |= XVID_VOP_GREYSCALE; - - } - - } else { - - if (config->greyscale) - - { - - if ((frame->vop_flags && XVID_VOP_GREYSCALE)) // use only if not already in greyscale - - frame->vop_flags |= XVID_VOP_GREYSCALE; - - } else { - - if (!(frame->vop_flags && XVID_VOP_GREYSCALE)) // if credits is in greyscale, switch back - - frame->vop_flags |= XVID_VOP_GREYSCALE; - - } - - } - - return 0; - -} - -// end of koepi's addition -