diff --git a/encoder/ratecontrol.c b/encoder/ratecontrol.c index 103e73b..4003e7b 100644 --- a/encoder/ratecontrol.c +++ b/encoder/ratecontrol.c @@ -1348,21 +1348,6 @@ static float rate_estimate_qscale( x264_t *h ) + h->stat.i_slice_size[SLICE_TYPE_P] + h->stat.i_slice_size[SLICE_TYPE_B]); - if( h->param.i_threads > 1 ) - { - int j = h->rc - h->thread[0]->rc; - int i; - for( i=1; iparam.i_threads; i++ ) - { - x264_t *t = h->thread[ (j+i)%h->param.i_threads ]; - double bits = t->rc->frame_size_planned; - if( !t->b_thread_active ) - continue; - bits = X264_MAX(bits, x264_ratecontrol_get_estimated_size(t)); - total_bits += (int64_t)bits; - } - } - if( rcc->b_2pass ) { rce = *rcc->rce; @@ -1415,10 +1400,15 @@ static float rate_estimate_qscale( x264_t *h ) if( rcc->b_2pass ) { //FIXME adjust abr_buffer based on distance to the end of the video - int64_t diff = total_bits - (int64_t)rce.expected_bits; + int64_t diff; + + if( h->fenc->i_frame < h->param.i_threads ) + diff = total_bits + (int64_t)h->fenc->i_frame * rcc->bitrate / rcc->fps - (int64_t)rce.expected_bits; + else + diff = total_bits + (int64_t)(h->param.i_threads - 1) * rcc->bitrate / rcc->fps - (int64_t)rce.expected_bits; q = rce.new_qscale; q /= x264_clip3f((double)(abr_buffer - diff) / abr_buffer, .5, 2); - if( h->fenc->i_frame > 30 ) + if( h->fenc->i_frame + 1 - h->param.i_threads >= rcc->fps && rcc->expected_bits_sum > 0) { /* Adjust quant based on the difference between * achieved and expected bitrate so far */ @@ -1491,7 +1481,7 @@ static float rate_estimate_qscale( x264_t *h ) wanted_bits = i_frame_done * rcc->bitrate / rcc->fps; if( wanted_bits > 0 ) { - abr_buffer *= X264_MAX( 1, sqrt(i_frame_done/25) ); + abr_buffer *= X264_MAX( 1, sqrt(i_frame_done / rcc->fps) ); overflow = x264_clip3f( 1.0 + (total_bits - wanted_bits) / abr_buffer, .5, 2 ); q *= overflow; }