393
void x264_ratecontrol_init_reconfigurable( x264_t *h, int b_init )
395
x264_ratecontrol_t *rc = h->rc;
396
if( !b_init && rc->b_2pass )
399
if( h->param.rc.i_vbv_max_bitrate > 0 && h->param.rc.i_vbv_buffer_size > 0 )
401
if( h->param.rc.i_vbv_buffer_size < (int)(h->param.rc.i_vbv_max_bitrate / rc->fps) )
403
h->param.rc.i_vbv_buffer_size = h->param.rc.i_vbv_max_bitrate / rc->fps;
404
x264_log( h, X264_LOG_WARNING, "VBV buffer size cannot be smaller than one frame, using %d kbit\n",
405
h->param.rc.i_vbv_buffer_size );
408
/* We don't support changing the ABR bitrate right now,
409
so if the stream starts as CBR, keep it CBR. */
410
if( rc->b_vbv_min_rate )
411
h->param.rc.i_vbv_max_bitrate = h->param.rc.i_bitrate;
412
rc->buffer_rate = h->param.rc.i_vbv_max_bitrate * 1000. / rc->fps;
413
rc->buffer_size = h->param.rc.i_vbv_buffer_size * 1000.;
414
rc->single_frame_vbv = rc->buffer_rate * 1.1 > rc->buffer_size;
415
rc->cbr_decay = 1.0 - rc->buffer_rate / rc->buffer_size
416
* 0.5 * X264_MAX(0, 1.5 - rc->buffer_rate * rc->fps / rc->bitrate);
419
if( h->param.rc.f_vbv_buffer_init > 1. )
420
h->param.rc.f_vbv_buffer_init = x264_clip3f( h->param.rc.f_vbv_buffer_init / h->param.rc.i_vbv_buffer_size, 0, 1 );
421
h->param.rc.f_vbv_buffer_init = X264_MAX( h->param.rc.f_vbv_buffer_init, rc->buffer_rate / rc->buffer_size );
422
rc->buffer_fill_final = rc->buffer_size * h->param.rc.f_vbv_buffer_init;
424
rc->b_vbv_min_rate = !rc->b_2pass
425
&& h->param.rc.i_rc_method == X264_RC_ABR
426
&& h->param.rc.i_vbv_max_bitrate <= h->param.rc.i_bitrate;
429
if( h->param.rc.i_rc_method == X264_RC_CRF )
431
/* Arbitrary rescaling to make CRF somewhat similar to QP.
432
* Try to compensate for MB-tree's effects as well. */
433
double base_cplx = h->mb.i_mb_count * (h->param.i_bframe ? 120 : 80);
434
double mbtree_offset = h->param.rc.b_mb_tree ? (1.0-h->param.rc.f_qcompress)*13.5 : 0;
435
rc->rate_factor_constant = pow( base_cplx, 1 - rc->qcompress )
436
/ qp2qscale( h->param.rc.f_rf_constant + mbtree_offset );
391
440
int x264_ratecontrol_new( x264_t *h )
393
442
x264_ratecontrol_t *rc;
426
475
x264_log(h, X264_LOG_ERROR, "constant rate-factor is incompatible with 2pass.\n");
429
if( h->param.rc.i_vbv_buffer_size )
431
if( h->param.rc.i_rc_method == X264_RC_CQP )
433
x264_log(h, X264_LOG_WARNING, "VBV is incompatible with constant QP, ignored.\n");
434
h->param.rc.i_vbv_max_bitrate = 0;
435
h->param.rc.i_vbv_buffer_size = 0;
437
else if( h->param.rc.i_vbv_max_bitrate == 0 )
439
if( h->param.rc.i_rc_method == X264_RC_ABR )
441
x264_log( h, X264_LOG_INFO, "VBV maxrate unspecified, assuming CBR\n" );
442
h->param.rc.i_vbv_max_bitrate = h->param.rc.i_bitrate;
446
x264_log( h, X264_LOG_INFO, "VBV bufsize set but maxrate unspecified, ignored\n" );
447
h->param.rc.i_vbv_buffer_size = 0;
451
if( h->param.rc.i_vbv_max_bitrate < h->param.rc.i_bitrate &&
452
h->param.rc.i_vbv_max_bitrate > 0)
453
x264_log(h, X264_LOG_WARNING, "max bitrate less than average bitrate, ignored.\n");
454
else if( h->param.rc.i_vbv_max_bitrate > 0 &&
455
h->param.rc.i_vbv_buffer_size > 0 )
457
if( h->param.rc.i_vbv_buffer_size < (int)(h->param.rc.i_vbv_max_bitrate / rc->fps) )
459
h->param.rc.i_vbv_buffer_size = h->param.rc.i_vbv_max_bitrate / rc->fps;
460
x264_log( h, X264_LOG_WARNING, "VBV buffer size cannot be smaller than one frame, using %d kbit\n",
461
h->param.rc.i_vbv_buffer_size );
463
if( h->param.rc.f_vbv_buffer_init > 1. )
464
h->param.rc.f_vbv_buffer_init = x264_clip3f( h->param.rc.f_vbv_buffer_init / h->param.rc.i_vbv_buffer_size, 0, 1 );
465
rc->buffer_rate = h->param.rc.i_vbv_max_bitrate * 1000. / rc->fps;
466
rc->buffer_size = h->param.rc.i_vbv_buffer_size * 1000.;
467
rc->single_frame_vbv = rc->buffer_rate * 1.1 > rc->buffer_size;
468
h->param.rc.f_vbv_buffer_init = X264_MAX( h->param.rc.f_vbv_buffer_init, rc->buffer_rate / rc->buffer_size );
469
rc->buffer_fill_final = rc->buffer_size * h->param.rc.f_vbv_buffer_init;
470
rc->cbr_decay = 1.0 - rc->buffer_rate / rc->buffer_size
471
* 0.5 * X264_MAX(0, 1.5 - rc->buffer_rate * rc->fps / rc->bitrate);
473
rc->b_vbv_min_rate = !rc->b_2pass
474
&& h->param.rc.i_rc_method == X264_RC_ABR
475
&& h->param.rc.i_vbv_max_bitrate <= h->param.rc.i_bitrate;
477
else if( h->param.rc.i_vbv_max_bitrate )
479
x264_log(h, X264_LOG_WARNING, "VBV maxrate specified, but no bufsize.\n");
480
h->param.rc.i_vbv_max_bitrate = 0;
482
if(rc->rate_tolerance < 0.01)
479
x264_ratecontrol_init_reconfigurable( h, 1 );
481
if( rc->rate_tolerance < 0.01 )
484
483
x264_log(h, X264_LOG_WARNING, "bitrate tolerance too small, using .01\n");
485
484
rc->rate_tolerance = 0.01;
499
498
rc->last_non_b_pict_type = SLICE_TYPE_I;
502
if( h->param.rc.i_rc_method == X264_RC_CRF )
504
/* Arbitrary rescaling to make CRF somewhat similar to QP.
505
* Try to compensate for MB-tree's effects as well. */
506
double base_cplx = h->mb.i_mb_count * (h->param.i_bframe ? 120 : 80);
507
double mbtree_offset = h->param.rc.b_mb_tree ? (1.0-h->param.rc.f_qcompress)*13.5 : 0;
508
rc->rate_factor_constant = pow( base_cplx, 1 - rc->qcompress )
509
/ qp2qscale( h->param.rc.f_rf_constant + mbtree_offset );
512
501
rc->ip_offset = 6.0 * log(h->param.rc.f_ip_factor) / log(2.0);
513
502
rc->pb_offset = 6.0 * log(h->param.rc.f_pb_factor) / log(2.0);
514
503
rc->qp_constant[SLICE_TYPE_P] = h->param.rc.i_qp_constant;
519
508
rc->lstep = pow( 2, h->param.rc.i_qp_step / 6.0 );
520
509
rc->last_qscale = qp2qscale(26);
521
CHECKED_MALLOC( rc->pred, 5*sizeof(predictor_t) );
510
int num_preds = h->param.b_sliced_threads * h->param.i_threads + 1;
511
CHECKED_MALLOC( rc->pred, 5 * sizeof(predictor_t) * num_preds );
522
512
CHECKED_MALLOC( rc->pred_b_from_p, sizeof(predictor_t) );
523
513
for( i = 0; i < 5; i++ )
525
515
rc->last_qscale_for[i] = qp2qscale( ABR_INIT_QP );
526
516
rc->lmin[i] = qp2qscale( h->param.rc.i_qp_min );
527
517
rc->lmax[i] = qp2qscale( h->param.rc.i_qp_max );
528
rc->pred[i].coeff= 2.0;
529
rc->pred[i].count= 1.0;
530
rc->pred[i].decay= 0.5;
531
rc->pred[i].offset= 0.0;
518
for( j = 0; j < num_preds; j++ )
520
rc->pred[i+j*5].coeff= 2.0;
521
rc->pred[i+j*5].count= 1.0;
522
rc->pred[i+j*5].decay= 0.5;
523
rc->pred[i+j*5].offset= 0.0;
532
525
for( j = 0; j < 2; j++ )
534
527
rc->row_preds[i][j].coeff= .25;
1200
1178
float buffer_left_planned = rc->buffer_fill - rc->frame_size_planned;
1201
1179
float slice_size_planned = h->param.b_sliced_threads ? rc->slice_size_planned : rc->frame_size_planned;
1202
float size_of_other_slices = rc->frame_size_planned - slice_size_planned;
1180
float size_of_other_slices = 0;
1181
if( h->param.b_sliced_threads )
1183
for( i = 0; i < h->param.i_threads; i++ )
1184
if( h != h->thread[i] )
1185
size_of_other_slices += h->thread[i]->rc->frame_size_estimated;
1188
rc->max_frame_error = X264_MAX( 0.05, 1.0 / (h->sps->i_mb_width) );
1203
1190
/* More threads means we have to be more cautious in letting ratecontrol use up extra bits. */
1204
1191
float rc_tol = buffer_left_planned / h->param.i_threads * rc->rate_tolerance;
1205
float max_frame_error = X264_MAX( 0.05, 1.0 / h->sps->i_mb_height );
1206
int b1 = predict_row_size_sum( h, y, rc->qpm );
1208
/* Assume that if this slice has become larger than expected,
1209
* the other slices will have gotten equally larger. */
1210
b1 += X264_MAX( size_of_other_slices * b1 / slice_size_planned, size_of_other_slices );
1192
int b1 = predict_row_size_sum( h, y, rc->qpm ) + size_of_other_slices;
1212
1194
/* Don't modify the row QPs until a sufficent amount of the bits of the frame have been processed, in case a flat */
1213
1195
/* area at the top of the frame was measured inaccurately. */
1214
if( row_bits_so_far(h,y) < 0.05 * (rc->frame_size_planned-size_of_other_slices) )
1196
if( row_bits_so_far( h, y ) < 0.05 * slice_size_planned )
1217
1199
if( h->sh.i_type != SLICE_TYPE_I )
1236
1217
|| b1 < (rc->buffer_fill - rc->buffer_size + rc->buffer_rate) * 1.1) )
1239
b1 = predict_row_size_sum( h, y, rc->qpm );
1240
b1 += X264_MAX( size_of_other_slices * b1 / slice_size_planned, size_of_other_slices );
1220
b1 = predict_row_size_sum( h, y, rc->qpm ) + size_of_other_slices;
1243
1223
/* avoid VBV underflow */
1244
1224
while( (rc->qpm < h->param.rc.i_qp_max)
1245
&& (rc->buffer_fill - b1 < rc->buffer_rate * max_frame_error) )
1225
&& (rc->buffer_fill - b1 < rc->buffer_rate * rc->max_frame_error) )
1248
b1 = predict_row_size_sum( h, y, rc->qpm );
1249
b1 += X264_MAX( size_of_other_slices * b1 / slice_size_planned, size_of_other_slices );
1228
b1 = predict_row_size_sum( h, y, rc->qpm ) + size_of_other_slices;
1252
x264_ratecontrol_set_estimated_size(h, b1);
1231
h->rc->frame_size_estimated = predict_row_size_sum( h, y, rc->qpm );
1255
1234
/* loses the fractional part of the frame-wise qp */
1577
1557
if( rct->buffer_fill_final < 0 )
1578
1558
x264_log( h, X264_LOG_WARNING, "VBV underflow (frame %d, %.0f bits)\n", h->i_frame, rct->buffer_fill_final );
1579
1559
rct->buffer_fill_final = X264_MAX( rct->buffer_fill_final, 0 );
1580
rct->buffer_fill_final += rct->buffer_rate;
1581
rct->buffer_fill_final = X264_MIN( rct->buffer_fill_final, rct->buffer_size );
1560
rct->buffer_fill_final += rcc->buffer_rate;
1561
rct->buffer_fill_final = X264_MIN( rct->buffer_fill_final, rcc->buffer_size );
1584
1564
// provisionally update VBV according to the planned size of all frames currently in progress
1585
1565
static void update_vbv_plan( x264_t *h, int overhead )
1587
1567
x264_ratecontrol_t *rcc = h->rc;
1588
rcc->buffer_fill = h->thread[0]->rc->buffer_fill_final - overhead;
1568
rcc->buffer_fill = h->thread[0]->rc->buffer_fill_final;
1589
1569
if( h->i_thread_frames > 1 )
1591
1571
int j = h->rc - h->thread[0]->rc;
1596
1576
double bits = t->rc->frame_size_planned;
1597
1577
if( !t->b_thread_active )
1599
bits = X264_MAX(bits, x264_ratecontrol_get_estimated_size(t));
1579
bits = X264_MAX(bits, t->rc->frame_size_estimated);
1600
1580
rcc->buffer_fill -= bits;
1601
1581
rcc->buffer_fill = X264_MAX( rcc->buffer_fill, 0 );
1602
1582
rcc->buffer_fill += rcc->buffer_rate;
1603
1583
rcc->buffer_fill = X264_MIN( rcc->buffer_fill, rcc->buffer_size );
1586
rcc->buffer_fill = X264_MIN( rcc->buffer_fill, rcc->buffer_size );
1587
rcc->buffer_fill -= overhead;
1608
1590
// apply VBV constraints and clip qscale to between lmin and lmax
1963
1947
/* Always use up the whole VBV in this case. */
1964
1948
if( rcc->single_frame_vbv )
1965
1949
rcc->frame_size_planned = rcc->buffer_rate;
1966
x264_ratecontrol_set_estimated_size(h, rcc->frame_size_planned);
1950
h->rc->frame_size_estimated = rcc->frame_size_planned;
1955
void x264_threads_normalize_predictors( x264_t *h )
1958
double totalsize = 0;
1959
for( i = 0; i < h->param.i_threads; i++ )
1960
totalsize += h->thread[i]->rc->slice_size_planned;
1961
double factor = h->rc->frame_size_planned / totalsize;
1962
for( i = 0; i < h->param.i_threads; i++ )
1963
h->thread[i]->rc->slice_size_planned *= factor;
1971
1966
void x264_threads_distribute_ratecontrol( x264_t *h )
1973
int i, row, totalsize = 0;
1975
for( row = 0; row < h->sps->i_mb_height; row++ )
1976
totalsize += h->fdec->i_row_satd[row];
1969
x264_ratecontrol_t *rc = h->rc;
1971
/* Initialize row predictors */
1972
if( h->i_frame == 0 )
1973
for( i = 0; i < h->param.i_threads; i++ )
1975
x264_ratecontrol_t *t = h->thread[i]->rc;
1976
memcpy( t->row_preds, rc->row_preds, sizeof(rc->row_preds) );
1977
1979
for( i = 0; i < h->param.i_threads; i++ )
1979
1981
x264_t *t = h->thread[i];
1980
x264_ratecontrol_t *rc = h->rc;
1981
memcpy( t->rc, rc, sizeof(x264_ratecontrol_t) );
1982
memcpy( t->rc, rc, offsetof(x264_ratecontrol_t, row_pred) );
1983
t->rc->row_pred = &t->rc->row_preds[h->sh.i_type];
1982
1984
/* Calculate the planned slice size. */
1983
if( h->rc->b_vbv && rc->frame_size_planned )
1985
if( rc->b_vbv && rc->frame_size_planned )
1986
1988
for( row = t->i_threadslice_start; row < t->i_threadslice_end; row++ )
1987
1989
size += h->fdec->i_row_satd[row];
1988
t->rc->slice_size_planned = size * rc->frame_size_planned / totalsize;
1990
t->rc->slice_size_planned = predict_size( &rc->pred[h->sh.i_type + (i+1)*5], rc->qpm, size );
1991
1993
t->rc->slice_size_planned = 0;
1995
if( rc->b_vbv && rc->frame_size_planned )
1997
x264_threads_normalize_predictors( h );
1999
if( rc->single_frame_vbv )
2001
/* Compensate for our max frame error threshold: give more bits (proportionally) to smaller slices. */
2002
for( i = 0; i < h->param.i_threads; i++ )
2004
x264_t *t = h->thread[i];
2005
t->rc->max_frame_error = X264_MAX( 0.05, 1.0 / (t->i_threadslice_end - t->i_threadslice_start) );
2006
t->rc->slice_size_planned += 2 * t->rc->max_frame_error * rc->frame_size_planned;
2008
x264_threads_normalize_predictors( h );
2011
for( i = 0; i < h->param.i_threads; i++ )
2012
h->thread[i]->rc->frame_size_estimated = h->thread[i]->rc->slice_size_planned;
1995
2016
void x264_threads_merge_ratecontrol( x264_t *h )
1998
2019
x264_ratecontrol_t *rc = h->rc;
2001
for( i = 1; i < h->param.i_threads; i++ )
2022
for( i = 0; i < h->param.i_threads; i++ )
2003
x264_ratecontrol_t *t = h->thread[i]->rc;
2004
rc->qpa_rc += t->qpa_rc;
2005
rc->qpa_aq += t->qpa_aq;
2006
for( j = 0; j < 5; j++ )
2007
for( k = 0; k < 2; k++ )
2009
rc->row_preds[j][k].coeff += t->row_preds[j][k].coeff;
2010
rc->row_preds[j][k].offset += t->row_preds[j][k].offset;
2011
rc->row_preds[j][k].count += t->row_preds[j][k].count;
2014
for( j = 0; j < 5; j++ )
2015
for( k = 0; k < 2; k++ )
2024
x264_t *t = h->thread[i];
2025
x264_ratecontrol_t *rct = h->thread[i]->rc;
2026
if( h->param.rc.i_vbv_buffer_size )
2017
rc->row_preds[j][k].coeff /= h->param.i_threads;
2018
rc->row_preds[j][k].offset /= h->param.i_threads;
2019
rc->row_preds[j][k].count /= h->param.i_threads;
2029
for( row = t->i_threadslice_start; row < t->i_threadslice_end; row++ )
2030
size += h->fdec->i_row_satd[row];
2031
int bits = t->stat.frame.i_mv_bits + t->stat.frame.i_tex_bits + t->stat.frame.i_misc_bits;
2032
int mb_count = (t->i_threadslice_end - t->i_threadslice_start) * h->sps->i_mb_width;
2033
update_predictor( &rc->pred[h->sh.i_type+5*i], qp2qscale(rct->qpa_rc/mb_count), size, bits );
2037
rc->qpa_rc += rct->qpa_rc;
2038
rc->qpa_aq += rct->qpa_aq;
2023
2042
void x264_thread_sync_ratecontrol( x264_t *cur, x264_t *prev, x264_t *next )