/*****************************************************************************
* ratecontrol.c: ratecontrol
*****************************************************************************
- * Copyright (C) 2005-2014 x264 project
+ * Copyright (C) 2005-2015 x264 project
*
* Authors: Loren Merritt <lorenm@u.washington.edu>
* Michael Niedermayer <michaelni@gmx.at>
*/
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,
void x264_adaptive_quant_frame( x264_t *h, x264_frame_t *frame, float *quant_offsets )
{
- /* constants chosen to result in approximately the same overall bitrate as without AQ.
- * FIXME: while they're written in 5 significant digits, they're only tuned to 2. */
- float strength;
- float avg_adj = 0.f;
/* Initialize frame stats */
for( int i = 0; i < 3; i++ )
{
/* Actual adaptive quantization */
else
{
- if( h->param.rc.i_aq_mode == X264_AQ_AUTOVARIANCE )
+ /* constants chosen to result in approximately the same overall bitrate as without AQ.
+ * FIXME: while they're written in 5 significant digits, they're only tuned to 2. */
+ float strength;
+ float avg_adj = 0.f;
+ float bias_strength = 0.f;
+
+ if( h->param.rc.i_aq_mode == X264_AQ_AUTOVARIANCE || h->param.rc.i_aq_mode == X264_AQ_AUTOVARIANCE_BIASED )
{
- float bit_depth_correction = powf(1 << (BIT_DEPTH-8), 0.5f);
+ float bit_depth_correction = 1.f / (1 << (2*(BIT_DEPTH-8)));
float avg_adj_pow2 = 0.f;
for( int mb_y = 0; mb_y < h->mb.i_mb_height; mb_y++ )
for( int mb_x = 0; mb_x < h->mb.i_mb_width; mb_x++ )
{
uint32_t energy = x264_ac_energy_mb( h, mb_x, mb_y, frame );
- float qp_adj = powf( energy + 1, 0.125f );
+ float qp_adj = powf( energy * bit_depth_correction + 1, 0.125f );
frame->f_qp_offset[mb_x + mb_y*h->mb.i_mb_stride] = qp_adj;
avg_adj += qp_adj;
avg_adj_pow2 += qp_adj * qp_adj;
}
avg_adj /= h->mb.i_mb_count;
avg_adj_pow2 /= h->mb.i_mb_count;
- strength = h->param.rc.f_aq_strength * avg_adj / bit_depth_correction;
- avg_adj = avg_adj - 0.5f * (avg_adj_pow2 - (14.f * bit_depth_correction)) / avg_adj;
+ strength = h->param.rc.f_aq_strength * avg_adj;
+ avg_adj = avg_adj - 0.5f * (avg_adj_pow2 - 14.f) / avg_adj;
+ bias_strength = h->param.rc.f_aq_strength;
}
else
strength = h->param.rc.f_aq_strength * 1.0397f;
{
float qp_adj;
int mb_xy = mb_x + mb_y*h->mb.i_mb_stride;
- if( h->param.rc.i_aq_mode == X264_AQ_AUTOVARIANCE )
+ if( h->param.rc.i_aq_mode == X264_AQ_AUTOVARIANCE_BIASED )
+ {
+ qp_adj = frame->f_qp_offset[mb_xy];
+ qp_adj = strength * (qp_adj - avg_adj) + bias_strength * (1.f - 14.f / (qp_adj * qp_adj));
+ }
+ else if( h->param.rc.i_aq_mode == X264_AQ_AUTOVARIANCE )
{
qp_adj = frame->f_qp_offset[mb_xy];
qp_adj = strength * (qp_adj - avg_adj);
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) );
+ static const float pred_coeff_table[3] = { 1.0, 1.0, 1.5 };
for( int i = 0; i < 3; i++ )
{
rc->last_qscale_for[i] = qp2qscale( ABR_INIT_QP );
rc->lmax[i] = qp2qscale( h->param.rc.i_qp_max );
for( int j = 0; j < num_preds; j++ )
{
- rc->pred[i+j*5].coeff_min = 2.0 / 4;
- rc->pred[i+j*5].coeff = 2.0;
+ rc->pred[i+j*5].coeff_min = pred_coeff_table[i] / 2;
+ rc->pred[i+j*5].coeff = pred_coeff_table[i];
rc->pred[i+j*5].count = 1.0;
rc->pred[i+j*5].decay = 0.5;
rc->pred[i+j*5].offset = 0.0;
rc->row_preds[i][j].offset = 0.0;
}
}
- *rc->pred_b_from_p = rc->pred[0];
+ rc->pred_b_from_p->coeff_min = 0.5 / 2;
+ rc->pred_b_from_p->coeff = 0.5;
+ rc->pred_b_from_p->count = 1.0;
+ rc->pred_b_from_p->decay = 0.5;
+ rc->pred_b_from_p->offset = 0.0;
if( parse_zones( h ) < 0 )
{
{
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;
}
h->fdec->f_row_qscale[y] = qscale;
update_predictor( &rc->row_pred[0], qscale, h->fdec->i_row_satd[y], h->fdec->i_row_bits[y] );
- if( h->sh.i_type == SLICE_TYPE_P && rc->qpm < h->fref[0][0]->f_row_qp[y] )
+ if( h->sh.i_type != SLICE_TYPE_I && 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( var < 10 )
return;
float old_coeff = p->coeff / p->count;
- float new_coeff = X264_MAX( bits*q / var, p->coeff_min );
+ float old_offset = p->offset / p->count;
+ float new_coeff = X264_MAX( (bits*q - old_offset) / var, p->coeff_min );
float new_coeff_clipped = x264_clip3f( new_coeff, old_coeff/range, old_coeff*range );
float new_offset = bits*q - new_coeff_clipped * var;
if( new_offset >= 0 )