From 001d30598c75d9bbc3aa80f67f9bdac17692437d Mon Sep 17 00:00:00 2001 From: Anton Mitrofanov Date: Tue, 22 Sep 2015 19:26:25 +0300 Subject: [PATCH] Fix high bit depth lookahead cost compensation algorithm Now high bit depth VBV should act more like 8-bit depth one. --- encoder/ratecontrol.c | 8 ++++---- encoder/slicetype.c | 10 +++------- 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/encoder/ratecontrol.c b/encoder/ratecontrol.c index c692691e..8b8e76e6 100644 --- a/encoder/ratecontrol.c +++ b/encoder/ratecontrol.c @@ -201,11 +201,11 @@ static void update_predictor( predictor_t *p, float q, float var, float bits ); */ static inline float qp2qscale( float qp ) { - return 0.85f * powf( 2.0f, ( qp - 12.0f ) / 6.0f ); + return 0.85f * powf( 2.0f, ( qp - (12.0f + QP_BD_OFFSET) ) / 6.0f ); } static inline float qscale2qp( float qscale ) { - return 12.0f + 6.0f * log2f( qscale/0.85f ); + return (12.0f + QP_BD_OFFSET) + 6.0f * log2f( qscale/0.85f ); } /* Texture bitrate is not quite inversely proportional to qscale, @@ -828,7 +828,7 @@ int x264_ratecontrol_new( x264_t *h ) h->mb.ip_offset = rc->ip_offset + 0.5; rc->lstep = pow( 2, h->param.rc.i_qp_step / 6.0 ); - rc->last_qscale = qp2qscale( 26 ); + rc->last_qscale = qp2qscale( 26 + QP_BD_OFFSET ); int num_preds = h->param.b_sliced_threads * h->param.i_threads + 1; CHECKED_MALLOC( rc->pred, 5 * sizeof(predictor_t) * num_preds ); CHECKED_MALLOC( rc->pred_b_from_p, sizeof(predictor_t) ); @@ -1024,7 +1024,7 @@ int x264_ratecontrol_new( x264_t *h ) { ratecontrol_entry_t *rce = &rc->entry[i]; rce->pict_type = SLICE_TYPE_P; - rce->qscale = rce->new_qscale = qp2qscale( 20 ); + rce->qscale = rce->new_qscale = qp2qscale( 20 + QP_BD_OFFSET ); rce->misc_bits = rc->nmb + 10; rce->new_qp = 0; } diff --git a/encoder/slicetype.c b/encoder/slicetype.c index 99007654..51e94bf9 100644 --- a/encoder/slicetype.c +++ b/encoder/slicetype.c @@ -740,7 +740,7 @@ lowres_intra_mb: } } - i_icost += intra_penalty + lowres_penalty; + i_icost = ((i_icost + intra_penalty) >> (BIT_DEPTH - 8)) + lowres_penalty; fenc->i_intra_cost[i_mb_xy] = i_icost; int i_icost_aq = i_icost; if( h->param.rc.i_aq_mode ) @@ -752,7 +752,7 @@ lowres_intra_mb: output_intra[COST_EST_AQ] += i_icost_aq; } } - i_bcost += lowres_penalty; + i_bcost = (i_bcost >> (BIT_DEPTH - 8)) + lowres_penalty; /* forbid intra-mbs in B-frames, because it's rare and not worth checking */ /* FIXME: Should we still forbid them now that we cache intra scores? */ @@ -2069,9 +2069,5 @@ int x264_rc_analyse_slice( x264_t *h ) } } - if( BIT_DEPTH > 8 ) - for( int y = 0; y < h->mb.i_mb_height; y++ ) - h->fdec->i_row_satd[y] >>= (BIT_DEPTH - 8); - - return cost >> (BIT_DEPTH - 8); + return cost; } -- 2.39.2