From 119f80054a34ccee267888237602af6b328af2ab Mon Sep 17 00:00:00 2001 From: Joose Sainio Date: Tue, 30 Mar 2021 10:19:35 +0300 Subject: [PATCH] Update get_ic_rate --- src/rdo.c | 53 +++++++++++++++++++++++++++++++++++++++++++---------- src/rdo.h | 2 +- 2 files changed, 44 insertions(+), 11 deletions(-) diff --git a/src/rdo.c b/src/rdo.c index be77560b..1470dbca 100644 --- a/src/rdo.c +++ b/src/rdo.c @@ -311,7 +311,8 @@ INLINE int32_t kvz_get_ic_rate(encoder_state_t * const state, uint16_t ctx_num_par, uint16_t abs_go_rice, uint32_t reg_bins, - int8_t type) + int8_t type, + int use_limited_prefix_length) { cabac_data_t * const cabac = &state->cabac; int32_t rate = 1 << CTX_FRAC_BITS; // cost of sign bit @@ -320,6 +321,7 @@ INLINE int32_t kvz_get_ic_rate(encoder_state_t * const state, cabac_ctx_t *base_gt1_ctx = (type == 0) ? &(cabac->ctx.cu_gtx_flag_model_luma[0][0]) : &(cabac->ctx.cu_gtx_flag_model_chroma[0][0]); cabac_ctx_t* base_gt2_ctx = (type == 0) ? &(cabac->ctx.cu_gtx_flag_model_luma[1][0]) : &(cabac->ctx.cu_gtx_flag_model_chroma[1][0]); uint16_t go_rice_zero = 1 << abs_go_rice; + int maxLog2TrDynamicRange = 15; if (reg_bins < 4) { @@ -330,7 +332,22 @@ INLINE int32_t kvz_get_ic_rate(encoder_state_t * const state, { length = symbol >> abs_go_rice; rate += (length + 1 + abs_go_rice) << CTX_FRAC_BITS; - } else { + } else if(use_limited_prefix_length) { + const uint32_t maximumPrefixLength = (32 - (COEF_REMAIN_BIN_REDUCTION + maxLog2TrDynamicRange)); + + uint32_t prefixLength = 0; + uint32_t suffix = (symbol >> abs_go_rice) - COEF_REMAIN_BIN_REDUCTION; + + while ((prefixLength < maximumPrefixLength) && (suffix > ((2 << prefixLength) - 2))) + { + prefixLength++; + } + + const uint32_t suffixLength = (prefixLength == maximumPrefixLength) ? (maxLog2TrDynamicRange - abs_go_rice) : (prefixLength + 1/*separator*/); + + rate += (COEF_REMAIN_BIN_REDUCTION + prefixLength + suffixLength + abs_go_rice) << CTX_FRAC_BITS; + } + else { length = abs_go_rice; symbol = symbol - (threshold << abs_go_rice); while (symbol >= (1 << length)) @@ -348,7 +365,23 @@ INLINE int32_t kvz_get_ic_rate(encoder_state_t * const state, if (symbol < (COEF_REMAIN_BIN_REDUCTION << abs_go_rice)) { length = symbol>>abs_go_rice; rate += (length + 1 + abs_go_rice) << CTX_FRAC_BITS; - } else { + } + else if (use_limited_prefix_length) { + const uint32_t maximumPrefixLength = (32 - (COEF_REMAIN_BIN_REDUCTION + maxLog2TrDynamicRange)); + + uint32_t prefixLength = 0; + uint32_t suffix = (symbol >> abs_go_rice) - COEF_REMAIN_BIN_REDUCTION; + + while ((prefixLength < maximumPrefixLength) && (suffix > ((2 << prefixLength) - 2))) + { + prefixLength++; + } + + const uint32_t suffixLength = (prefixLength == maximumPrefixLength) ? (maxLog2TrDynamicRange - abs_go_rice) : (prefixLength + 1/*separator*/); + + rate += (COEF_REMAIN_BIN_REDUCTION + prefixLength + suffixLength + abs_go_rice) << CTX_FRAC_BITS; + } + else { length = abs_go_rice; symbol = symbol - ( COEF_REMAIN_BIN_REDUCTION << abs_go_rice); while (symbol >= (1<ctx.sig_coeff_group_model[type]); + cabac_ctx_t *base_coeff_group_ctx = &(cabac->ctx.sig_coeff_group_model[type ? 2 : 0]); cabac_ctx_t *baseCtx = (type == 0) ? &(cabac->ctx.cu_sig_model_luma[0][0]) : &(cabac->ctx.cu_sig_model_chroma[0][0]); cabac_ctx_t* base_gt1_ctx = (type == 0) ? &(cabac->ctx.cu_gtx_flag_model_luma[0][0]) : &(cabac->ctx.cu_gtx_flag_model_chroma[0][0]); @@ -863,13 +896,13 @@ void kvz_rdoq(encoder_state_t * const state, coeff_t *coef, coeff_t *dest_coeff, if (encoder->cfg.signhide_enable) { sh_rates.quant_delta[blkpos] = (level_double - level * (1 << q_bits)) >> (q_bits - 8); if (level > 0) { - int32_t rate_now = kvz_get_ic_rate(state, level, gt1_ctx, gt2_ctx, par_ctx, go_rice_param, reg_bins, type); - sh_rates.inc[blkpos] = kvz_get_ic_rate(state, level + 1, gt1_ctx, gt2_ctx, par_ctx, go_rice_param, reg_bins, type) - rate_now; - sh_rates.dec[blkpos] = kvz_get_ic_rate(state, level - 1, gt1_ctx, gt2_ctx, par_ctx, go_rice_param, reg_bins, type) - rate_now; + int32_t rate_now = kvz_get_ic_rate(state, level, gt1_ctx, gt2_ctx, par_ctx, go_rice_param, reg_bins, type, false); + sh_rates.inc[blkpos] = kvz_get_ic_rate(state, level + 1, gt1_ctx, gt2_ctx, par_ctx, go_rice_param, reg_bins, type, false) - rate_now; + sh_rates.dec[blkpos] = kvz_get_ic_rate(state, level - 1, gt1_ctx, gt2_ctx, par_ctx, go_rice_param, reg_bins, type, false) - rate_now; } else { // level == 0 if (reg_bins < 4) { - int32_t rate_now = kvz_get_ic_rate(state, level, gt1_ctx, gt2_ctx, par_ctx, go_rice_param, reg_bins, type); - sh_rates.inc[blkpos] = kvz_get_ic_rate(state, level + 1, gt1_ctx, gt2_ctx, par_ctx, go_rice_param, reg_bins, type) - rate_now; + int32_t rate_now = kvz_get_ic_rate(state, level, gt1_ctx, gt2_ctx, par_ctx, go_rice_param, reg_bins, type, false); + sh_rates.inc[blkpos] = kvz_get_ic_rate(state, level + 1, gt1_ctx, gt2_ctx, par_ctx, go_rice_param, reg_bins, type, false) - rate_now; } else { sh_rates.inc[blkpos] = CTX_ENTROPY_BITS(&base_gt1_ctx[gt1_ctx], 0); } diff --git a/src/rdo.h b/src/rdo.h index d63864d5..ad627ab5 100644 --- a/src/rdo.h +++ b/src/rdo.h @@ -46,7 +46,7 @@ uint32_t kvz_get_coeff_cost(const encoder_state_t * const state, int8_t scan_mode); int32_t kvz_get_ic_rate(encoder_state_t *state, uint32_t abs_level, uint16_t ctx_num_gt1, uint16_t ctx_num_gt2, uint16_t ctx_num_par, - uint16_t abs_go_rice, uint32_t reg_bins, int8_t type); + uint16_t abs_go_rice, uint32_t reg_bins, int8_t type, int use_limited_prefix_length); uint32_t kvz_get_coded_level(encoder_state_t * state, double* coded_cost, double* coded_cost0, double* coded_cost_sig, int32_t level_double, uint32_t max_abs_level, uint16_t ctx_num_sig, uint16_t ctx_num_gt1, uint16_t ctx_num_gt2, uint16_t ctx_num_par,