From: Anton Mitrofanov Date: Thu, 21 Apr 2011 21:13:28 +0000 (+0400) Subject: Fix VFR MB-tree to work as intended X-Git-Url: http://www.zmx.ovh/git.videolan.org/?a=commitdiff_plain;h=6e33b51a6571dfc814053096c82538ad278f1868;p=x264.git Fix VFR MB-tree to work as intended Should improve quality with FPSs much larger or smaller than 25. --- diff --git a/encoder/ratecontrol.c b/encoder/ratecontrol.c index 6726fbd1..d28696f3 100644 --- a/encoder/ratecontrol.c +++ b/encoder/ratecontrol.c @@ -1688,7 +1688,14 @@ static double get_qscale(x264_t *h, ratecontrol_entry_t *rce, double rate_factor { x264_ratecontrol_t *rcc= h->rc; x264_zone_t *zone = get_zone( h, frame_num ); - double q = pow( rce->blurred_complexity, 1 - rcc->qcompress ); + double q; + if( h->param.rc.b_mb_tree ) + { + double timescale = (double)h->sps->vui.i_num_units_in_tick / h->sps->vui.i_time_scale; + q = pow( BASE_FRAME_DURATION / CLIP_DURATION(rce->i_duration * timescale), 1 - h->param.rc.f_qcompress ); + } + else + q = pow( rce->blurred_complexity, 1 - rcc->qcompress ); // avoid NaN's in the rc_eq if( !isfinite(q) || rce->tex_bits + rce->mv_bits == 0 ) @@ -2199,6 +2206,7 @@ static float rate_estimate_qscale( x264_t *h ) rce.s_count = 0; rce.qscale = 1; rce.pict_type = pict_type; + rce.i_duration = h->fenc->i_duration; if( h->param.rc.i_rc_method == X264_RC_CRF ) { diff --git a/encoder/slicetype.c b/encoder/slicetype.c index 0c9ba7a2..4ecdf31e 100644 --- a/encoder/slicetype.c +++ b/encoder/slicetype.c @@ -752,8 +752,7 @@ static int x264_slicetype_frame_cost_recalculate( x264_t *h, x264_frame_t **fram static void x264_macroblock_tree_finish( x264_t *h, x264_frame_t *frame, float average_duration, int ref0_distance ) { - int fps_factor_intra = round( CLIP_DURATION(frame->f_duration) / BASE_FRAME_DURATION * 256 ); - int fps_factor_propagate = round( CLIP_DURATION( average_duration) / BASE_FRAME_DURATION * 256 ); + int fps_factor = round( CLIP_DURATION(average_duration) / CLIP_DURATION(frame->f_duration) * 256 ); float weightdelta = 0.0; if( ref0_distance && frame->f_weighted_cost_delta[ref0_distance-1] > 0 ) weightdelta = (1.0 - frame->f_weighted_cost_delta[ref0_distance-1]); @@ -764,11 +763,10 @@ static void x264_macroblock_tree_finish( x264_t *h, x264_frame_t *frame, float a for( int mb_index = 0; mb_index < h->mb.i_mb_count; mb_index++ ) { int intra_cost = (frame->i_intra_cost[mb_index] * frame->i_inv_qscale_factor[mb_index] + 128) >> 8; - int intra_cost_scaled = (intra_cost * fps_factor_intra + 128) >> 8; if( intra_cost ) { - int propagate_cost = (frame->i_propagate_cost[mb_index] * fps_factor_propagate + 128) >> 8; - float log2_ratio = x264_log2(intra_cost_scaled + propagate_cost) - x264_log2(intra_cost) + weightdelta; + int propagate_cost = (frame->i_propagate_cost[mb_index] * fps_factor + 128) >> 8; + float log2_ratio = x264_log2(intra_cost + propagate_cost) - x264_log2(intra_cost) + weightdelta; frame->f_qp_offset[mb_index] = frame->f_qp_offset_aq[mb_index] - strength * log2_ratio; } }