From ed3b10eb9cffcc346b5a070ce47f5a2beaf9efb6 Mon Sep 17 00:00:00 2001 From: Anton Mitrofanov Date: Mon, 6 Jun 2011 13:54:44 +0400 Subject: [PATCH] Fix MBAFF row VBV ratecontrol Reverts most of r1984 and implements a much simpler solution. --- encoder/encoder.c | 12 ++---------- encoder/ratecontrol.c | 14 ++++++++------ 2 files changed, 10 insertions(+), 16 deletions(-) diff --git a/encoder/encoder.c b/encoder/encoder.c index d6bcd51b..f8050e0e 100644 --- a/encoder/encoder.c +++ b/encoder/encoder.c @@ -2023,7 +2023,6 @@ static int x264_slice_write( x264_t *h ) i_mb_y = h->sh.i_first_mb / h->mb.i_mb_width; i_mb_x = h->sh.i_first_mb % h->mb.i_mb_width; i_skip = 0; - int mb_size[2]; while( 1 ) { @@ -2131,7 +2130,7 @@ reencode: } int total_bits = bs_pos(&h->out.bs) + x264_cabac_pos(&h->cabac); - mb_size[i_mb_y&1] = total_bits - mb_spos; + int mb_size = total_bits - mb_spos; if( slice_max_size ) { @@ -2253,14 +2252,7 @@ reencode: if( b_deblock ) x264_macroblock_deblock_strength( h ); - if( SLICE_MBAFF ) - { - /* update ratecontrol per-mbpair in MBAFF */ - if( i_mb_y&1 ) - x264_ratecontrol_mb( h, mb_size[0]+mb_size[1] ); - } - else - x264_ratecontrol_mb( h, mb_size[i_mb_y&1] ); + x264_ratecontrol_mb( h, mb_size ); if( mb_xy == h->sh.i_last_mb ) break; diff --git a/encoder/ratecontrol.c b/encoder/ratecontrol.c index 87264bfe..97b5ee88 100644 --- a/encoder/ratecontrol.c +++ b/encoder/ratecontrol.c @@ -1331,7 +1331,7 @@ static float predict_row_size( x264_t *h, int y, float qscale ) static int row_bits_so_far( x264_t *h, int y ) { int bits = 0; - for( int i = h->i_threadslice_start+SLICE_MBAFF; i <= y; i+=(SLICE_MBAFF+1) ) + for( int i = h->i_threadslice_start; i <= y; i++ ) bits += h->fdec->i_row_bits[i]; return bits; } @@ -1340,7 +1340,7 @@ static float predict_row_size_sum( x264_t *h, int y, float qp ) { float qscale = qp2qscale( qp ); float bits = row_bits_so_far( h, y ); - for( int i = y+1+SLICE_MBAFF; i < h->i_threadslice_end; i+=(1+SLICE_MBAFF) ) + for( int i = y+1; i < h->i_threadslice_end; i++ ) bits += predict_row_size( h, i, qscale ); return bits; } @@ -1356,14 +1356,12 @@ void x264_ratecontrol_mb( x264_t *h, int bits ) h->fdec->i_row_bits[y] += bits; rc->qpa_aq += h->mb.i_qp; - if( SLICE_MBAFF ) - rc->qpa_aq += h->mb.i_last_qp; if( h->mb.i_mb_x != h->mb.i_mb_width - 1 ) return; x264_emms(); - rc->qpa_rc += rc->qpm * (h->mb.i_mb_width << SLICE_MBAFF); + rc->qpa_rc += rc->qpm * h->mb.i_mb_width; if( !rc->b_vbv ) return; @@ -1376,6 +1374,10 @@ void x264_ratecontrol_mb( x264_t *h, int bits ) if( h->sh.i_type == SLICE_TYPE_P && rc->qpm < h->fref[0][0]->f_row_qp[y] ) update_predictor( rc->row_pred[1], qscale, h->fdec->i_row_satds[0][0][y], h->fdec->i_row_bits[y] ); + /* update ratecontrol per-mbpair in MBAFF */ + if( SLICE_MBAFF && !(y&1) ) + return; + /* tweak quality based on difference from predicted size */ if( y < h->i_threadslice_end-1 ) { @@ -1390,7 +1392,7 @@ void x264_ratecontrol_mb( x264_t *h, int bits ) /* B-frames shouldn't use lower QP than their reference frames. */ if( h->sh.i_type == SLICE_TYPE_B ) { - qp_min = X264_MAX( qp_min, X264_MAX( h->fref[0][0]->f_row_qp[y+1+SLICE_MBAFF], h->fref[1][0]->f_row_qp[y+1+SLICE_MBAFF] ) ); + qp_min = X264_MAX( qp_min, X264_MAX( h->fref[0][0]->f_row_qp[y+1], h->fref[1][0]->f_row_qp[y+1] ) ); rc->qpm = X264_MAX( rc->qpm, qp_min ); } -- 2.39.2