Commit d23e3392 authored by Deb Mukherjee's avatar Deb Mukherjee

Removes active_worst_quality feedback

Removes certain cases of feedback of active_worst_quality,
and removes it from the RATE_CONTROL structure. Now active
worst quality is expected to be computed locally in the
q picking function during the encode.
Making temporal filter strength depend on avg_frame_qindex
rather than on active_worst_quality actually improves
performance esp. for yt.
derf: +0.038%
yt: +0.359%

Change-Id: I1fe5a343034b55af9322289165321f00ac0827b1
parent 992500b8
...@@ -2275,15 +2275,14 @@ void vp9_rc_get_second_pass_params(VP9_COMP *cpi) { ...@@ -2275,15 +2275,14 @@ void vp9_rc_get_second_pass_params(VP9_COMP *cpi) {
vp9_clear_system_state(); vp9_clear_system_state();
if (cpi->oxcf.end_usage == USAGE_CONSTANT_QUALITY) { if (cpi->oxcf.end_usage == USAGE_CONSTANT_QUALITY) {
rc->active_worst_quality = cpi->oxcf.cq_level; twopass->active_worst_quality = cpi->oxcf.cq_level;
} else if (cm->current_video_frame == 0) { } else if (cm->current_video_frame == 0) {
// Special case code for first frame. // Special case code for first frame.
const int section_target_bandwidth = (int)(twopass->bits_left / const int section_target_bandwidth = (int)(twopass->bits_left /
frames_left); frames_left);
const int tmp_q = vp9_twopass_worst_quality(cpi, &twopass->total_left_stats, const int tmp_q = vp9_twopass_worst_quality(cpi, &twopass->total_left_stats,
section_target_bandwidth); section_target_bandwidth);
twopass->active_worst_quality = tmp_q;
rc->active_worst_quality = tmp_q;
rc->ni_av_qi = tmp_q; rc->ni_av_qi = tmp_q;
rc->avg_q = vp9_convert_qindex_to_q(tmp_q); rc->avg_q = vp9_convert_qindex_to_q(tmp_q);
......
...@@ -76,6 +76,8 @@ struct twopass_rc { ...@@ -76,6 +76,8 @@ struct twopass_rc {
int kf_zeromotion_pct; int kf_zeromotion_pct;
int gf_zeromotion_pct; int gf_zeromotion_pct;
int active_worst_quality;
}; };
struct VP9_COMP; struct VP9_COMP;
......
...@@ -1155,10 +1155,9 @@ static void init_layer_context(VP9_COMP *const cpi) { ...@@ -1155,10 +1155,9 @@ static void init_layer_context(VP9_COMP *const cpi) {
++temporal_layer) { ++temporal_layer) {
LAYER_CONTEXT *const lc = &cpi->svc.layer_context[temporal_layer]; LAYER_CONTEXT *const lc = &cpi->svc.layer_context[temporal_layer];
RATE_CONTROL *const lrc = &lc->rc; RATE_CONTROL *const lrc = &lc->rc;
lrc->active_worst_quality = q_trans[oxcf->worst_allowed_q]; lrc->avg_frame_qindex[INTER_FRAME] = q_trans[oxcf->worst_allowed_q];
lrc->avg_frame_qindex[INTER_FRAME] = lrc->active_worst_quality; lrc->last_q[INTER_FRAME] = q_trans[oxcf->worst_allowed_q];
lrc->last_q[INTER_FRAME] = lrc->active_worst_quality; lrc->ni_av_qi = q_trans[oxcf->worst_allowed_q];
lrc->ni_av_qi = lrc->active_worst_quality;
lrc->total_actual_bits = 0; lrc->total_actual_bits = 0;
lrc->total_target_vs_actual = 0; lrc->total_target_vs_actual = 0;
lrc->ni_tot_qi = 0; lrc->ni_tot_qi = 0;
...@@ -1203,7 +1202,6 @@ static void update_layer_context_change_config(VP9_COMP *const cpi, ...@@ -1203,7 +1202,6 @@ static void update_layer_context_change_config(VP9_COMP *const cpi,
// Update qp-related quantities. // Update qp-related quantities.
lrc->worst_quality = rc->worst_quality; lrc->worst_quality = rc->worst_quality;
lrc->best_quality = rc->best_quality; lrc->best_quality = rc->best_quality;
lrc->active_worst_quality = rc->active_worst_quality;
} }
} }
...@@ -1302,8 +1300,6 @@ static void init_config(VP9_PTR ptr, VP9_CONFIG *oxcf) { ...@@ -1302,8 +1300,6 @@ static void init_config(VP9_PTR ptr, VP9_CONFIG *oxcf) {
vp9_change_config(ptr, oxcf); vp9_change_config(ptr, oxcf);
// Initialize active best and worst q and average q values. // Initialize active best and worst q and average q values.
cpi->rc.active_worst_quality = cpi->oxcf.worst_allowed_q;
if (cpi->pass == 0 && cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) { if (cpi->pass == 0 && cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER) {
cpi->rc.avg_frame_qindex[0] = cpi->oxcf.worst_allowed_q; cpi->rc.avg_frame_qindex[0] = cpi->oxcf.worst_allowed_q;
cpi->rc.avg_frame_qindex[1] = cpi->oxcf.worst_allowed_q; cpi->rc.avg_frame_qindex[1] = cpi->oxcf.worst_allowed_q;
...@@ -1449,9 +1445,6 @@ void vp9_change_config(VP9_PTR ptr, VP9_CONFIG *oxcf) { ...@@ -1449,9 +1445,6 @@ void vp9_change_config(VP9_PTR ptr, VP9_CONFIG *oxcf) {
cpi->rc.best_quality = cpi->oxcf.best_allowed_q; cpi->rc.best_quality = cpi->oxcf.best_allowed_q;
// active values should only be modified if out of new range // active values should only be modified if out of new range
cpi->rc.active_worst_quality = clamp(cpi->rc.active_worst_quality,
cpi->rc.best_quality,
cpi->rc.worst_quality);
cpi->cq_target_quality = cpi->oxcf.cq_level; cpi->cq_target_quality = cpi->oxcf.cq_level;
...@@ -2727,7 +2720,7 @@ static void output_frame_level_debug_stats(VP9_COMP *cpi) { ...@@ -2727,7 +2720,7 @@ static void output_frame_level_debug_stats(VP9_COMP *cpi) {
if (cpi->twopass.total_left_stats.coded_error != 0.0) if (cpi->twopass.total_left_stats.coded_error != 0.0)
fprintf(f, "%10u %10d %10d %10d %10d %10d " fprintf(f, "%10u %10d %10d %10d %10d %10d "
"%10"PRId64" %10"PRId64" %10d " "%10"PRId64" %10"PRId64" %10d "
"%7.2lf %7.2lf %7.2lf %7.2lf %7.2lf %7.2lf" "%7.2lf %7.2lf %7.2lf %7.2lf %7.2lf"
"%6d %6d %5d %5d %5d " "%6d %6d %5d %5d %5d "
"%10"PRId64" %10.3lf" "%10"PRId64" %10.3lf"
"%10lf %8u %10d %10d %10d\n", "%10lf %8u %10d %10d %10d\n",
...@@ -2740,7 +2733,7 @@ static void output_frame_level_debug_stats(VP9_COMP *cpi) { ...@@ -2740,7 +2733,7 @@ static void output_frame_level_debug_stats(VP9_COMP *cpi) {
cpi->rc.total_actual_bits, cm->base_qindex, cpi->rc.total_actual_bits, cm->base_qindex,
vp9_convert_qindex_to_q(cm->base_qindex), vp9_convert_qindex_to_q(cm->base_qindex),
(double)vp9_dc_quant(cm->base_qindex, 0) / 4.0, (double)vp9_dc_quant(cm->base_qindex, 0) / 4.0,
vp9_convert_qindex_to_q(cpi->rc.active_worst_quality), cpi->rc.avg_q, cpi->rc.avg_q,
vp9_convert_qindex_to_q(cpi->rc.ni_av_qi), vp9_convert_qindex_to_q(cpi->rc.ni_av_qi),
vp9_convert_qindex_to_q(cpi->cq_target_quality), vp9_convert_qindex_to_q(cpi->cq_target_quality),
cpi->refresh_last_frame, cpi->refresh_golden_frame, cpi->refresh_last_frame, cpi->refresh_golden_frame,
...@@ -3209,9 +3202,7 @@ static void encode_frame_to_data_rate(VP9_COMP *cpi, ...@@ -3209,9 +3202,7 @@ static void encode_frame_to_data_rate(VP9_COMP *cpi,
#endif #endif
// Decide q and q bounds. // Decide q and q bounds.
q = vp9_rc_pick_q_and_adjust_q_bounds(cpi, q = vp9_rc_pick_q_and_bounds(cpi, &bottom_index, &top_index);
&bottom_index,
&top_index);
if (!frame_is_intra_only(cm)) { if (!frame_is_intra_only(cm)) {
cm->interp_filter = DEFAULT_INTERP_FILTER; cm->interp_filter = DEFAULT_INTERP_FILTER;
......
...@@ -460,14 +460,206 @@ static int get_active_quality(int q, int gfu_boost, int low, int high, ...@@ -460,14 +460,206 @@ static int get_active_quality(int q, int gfu_boost, int low, int high,
} }
} }
static int rc_pick_q_and_adjust_q_bounds_one_pass(const VP9_COMP *cpi, static int calc_active_worst_quality_one_pass_vbr(const VP9_COMP *cpi) {
int *bottom_index, int active_worst_quality;
int *top_index) { if (cpi->common.frame_type == KEY_FRAME) {
if (cpi->common.current_video_frame == 0) {
active_worst_quality = cpi->rc.worst_quality;
} else {
// Choose active worst quality twice as large as the last q.
active_worst_quality = cpi->rc.last_q[KEY_FRAME] * 2;
}
} else if (!cpi->rc.is_src_frame_alt_ref &&
(cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) {
if (cpi->common.current_video_frame == 1) {
active_worst_quality = cpi->rc.last_q[KEY_FRAME] * 5 / 4;
} else {
// Choose active worst quality twice as large as the last q.
active_worst_quality = cpi->rc.last_q[INTER_FRAME];
}
} else {
if (cpi->common.current_video_frame == 1) {
active_worst_quality = cpi->rc.last_q[KEY_FRAME] * 2;
} else {
// Choose active worst quality twice as large as the last q.
active_worst_quality = cpi->rc.last_q[INTER_FRAME] * 2;
}
}
if (active_worst_quality > cpi->rc.worst_quality)
active_worst_quality = cpi->rc.worst_quality;
return active_worst_quality;
}
// Adjust active_worst_quality level based on buffer level.
static int calc_active_worst_quality_one_pass_cbr(const VP9_COMP *cpi) {
// Adjust active_worst_quality: If buffer is above the optimal/target level,
// bring active_worst_quality down depending on fullness of buffer.
// If buffer is below the optimal level, let the active_worst_quality go from
// ambient Q (at buffer = optimal level) to worst_quality level
// (at buffer = critical level).
const VP9_CONFIG *oxcf = &cpi->oxcf;
const RATE_CONTROL *rc = &cpi->rc;
// int active_worst_quality = rc->active_worst_quality;
// Maximum limit for down adjustment, ~20%.
// Buffer level below which we push active_worst to worst_quality.
int critical_level = oxcf->optimal_buffer_level >> 2;
int adjustment = 0;
int buff_lvl_step = 0;
int active_worst_quality;
if (cpi->common.frame_type == KEY_FRAME)
return rc->worst_quality;
if (cpi->common.current_video_frame > 1)
active_worst_quality = MIN(rc->worst_quality,
rc->avg_frame_qindex[INTER_FRAME] * 5 / 4);
else
active_worst_quality = MIN(rc->worst_quality,
rc->avg_frame_qindex[KEY_FRAME] * 3 / 2);
if (rc->buffer_level > oxcf->optimal_buffer_level) {
// Adjust down.
int max_adjustment_down = active_worst_quality / 3;
if (max_adjustment_down) {
buff_lvl_step = (int)((oxcf->maximum_buffer_size -
oxcf->optimal_buffer_level) / max_adjustment_down);
if (buff_lvl_step)
adjustment = (int)((rc->buffer_level - oxcf->optimal_buffer_level) /
buff_lvl_step);
active_worst_quality -= adjustment;
}
} else if (rc->buffer_level > critical_level) {
// Adjust up from ambient Q.
if (critical_level) {
buff_lvl_step = (oxcf->optimal_buffer_level - critical_level);
if (buff_lvl_step) {
adjustment = (rc->worst_quality - rc->avg_frame_qindex[INTER_FRAME]) *
(oxcf->optimal_buffer_level - rc->buffer_level) /
buff_lvl_step;
}
active_worst_quality = rc->avg_frame_qindex[INTER_FRAME] + adjustment;
}
} else {
// Set to worst_quality if buffer is below critical level.
active_worst_quality = rc->worst_quality;
}
return active_worst_quality;
}
static int rc_pick_q_and_bounds_one_pass_cbr(const VP9_COMP *cpi,
int *bottom_index,
int *top_index) {
const VP9_COMMON *const cm = &cpi->common;
const RATE_CONTROL *const rc = &cpi->rc;
int active_best_quality;
int active_worst_quality = calc_active_worst_quality_one_pass_cbr(cpi);
int q;
if (frame_is_intra_only(cm)) {
active_best_quality = rc->best_quality;
// Handle the special case for key frames forced when we have75 reached
// the maximum key frame interval. Here force the Q to a range
// based on the ambient Q to reduce the risk of popping.
if (rc->this_key_frame_forced) {
int qindex = rc->last_boosted_qindex;
double last_boosted_q = vp9_convert_qindex_to_q(qindex);
int delta_qindex = vp9_compute_qdelta(cpi, last_boosted_q,
(last_boosted_q * 0.75));
active_best_quality = MAX(qindex + delta_qindex, rc->best_quality);
} else if (cm->current_video_frame > 0) {
// not first frame of one pass and kf_boost is set
double q_adj_factor = 1.0;
double q_val;
active_best_quality = get_active_quality(rc->avg_frame_qindex[KEY_FRAME],
rc->kf_boost,
kf_low, kf_high,
kf_low_motion_minq,
kf_high_motion_minq);
// Allow somewhat lower kf minq with small image formats.
if ((cm->width * cm->height) <= (352 * 288)) {
q_adj_factor -= 0.25;
}
// Convert the adjustment factor to a qindex delta
// on active_best_quality.
q_val = vp9_convert_qindex_to_q(active_best_quality);
active_best_quality += vp9_compute_qdelta(cpi, q_val, q_val *
q_adj_factor);
}
} else if (!rc->is_src_frame_alt_ref &&
(cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) {
// Use the lower of active_worst_quality and recent
// average Q as basis for GF/ARF best Q limit unless last frame was
// a key frame.
if (rc->frames_since_key > 1 &&
rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality) {
q = rc->avg_frame_qindex[INTER_FRAME];
} else {
q = active_worst_quality;
}
active_best_quality = get_active_quality(
q, rc->gfu_boost, gf_low, gf_high,
gf_low_motion_minq, gf_high_motion_minq);
} else {
// Use the lower of active_worst_quality and recent/average Q.
if (cm->current_video_frame > 1) {
if (rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality)
active_best_quality = inter_minq[rc->avg_frame_qindex[INTER_FRAME]];
else
active_best_quality = inter_minq[active_worst_quality];
} else {
if (rc->avg_frame_qindex[KEY_FRAME] < active_worst_quality)
active_best_quality = inter_minq[rc->avg_frame_qindex[KEY_FRAME]];
else
active_best_quality = inter_minq[active_worst_quality];
}
}
// Clip the active best and worst quality values to limits
active_best_quality = clamp(active_best_quality,
rc->best_quality, rc->worst_quality);
active_worst_quality = clamp(active_worst_quality,
active_best_quality, rc->worst_quality);
*top_index = active_worst_quality;
*bottom_index = active_best_quality;
#if LIMIT_QRANGE_FOR_ALTREF_AND_KEY
// Limit Q range for the adaptive loop.
if (cm->frame_type == KEY_FRAME && !rc->this_key_frame_forced) {
if (!(cm->current_video_frame == 0))
*top_index = (active_worst_quality + active_best_quality * 3) / 4;
}
#endif
// Special case code to try and match quality with forced key frames
if (cm->frame_type == KEY_FRAME && rc->this_key_frame_forced) {
q = rc->last_boosted_qindex;
} else {
q = vp9_rc_regulate_q(cpi, rc->this_frame_target,
active_best_quality, active_worst_quality);
if (q > *top_index) {
// Special case when we are targeting the max allowed rate
if (cpi->rc.this_frame_target >= cpi->rc.max_frame_bandwidth)
*top_index = q;
else
q = *top_index;
}
}
assert(*top_index <= rc->worst_quality &&
*top_index >= rc->best_quality);
assert(*bottom_index <= rc->worst_quality &&
*bottom_index >= rc->best_quality);
assert(q <= rc->worst_quality && q >= rc->best_quality);
return q;
}
static int rc_pick_q_and_bounds_one_pass_vbr(const VP9_COMP *cpi,
int *bottom_index,
int *top_index) {
const VP9_COMMON *const cm = &cpi->common; const VP9_COMMON *const cm = &cpi->common;
const RATE_CONTROL *const rc = &cpi->rc; const RATE_CONTROL *const rc = &cpi->rc;
const VP9_CONFIG *const oxcf = &cpi->oxcf; const VP9_CONFIG *const oxcf = &cpi->oxcf;
int active_best_quality; int active_best_quality;
int active_worst_quality = rc->active_worst_quality; int active_worst_quality = calc_active_worst_quality_one_pass_vbr(cpi);
int q; int q;
if (frame_is_intra_only(cm)) { if (frame_is_intra_only(cm)) {
...@@ -520,8 +712,7 @@ static int rc_pick_q_and_adjust_q_bounds_one_pass(const VP9_COMP *cpi, ...@@ -520,8 +712,7 @@ static int rc_pick_q_and_adjust_q_bounds_one_pass(const VP9_COMP *cpi,
rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality) { rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality) {
q = rc->avg_frame_qindex[INTER_FRAME]; q = rc->avg_frame_qindex[INTER_FRAME];
} else { } else {
q = (oxcf->end_usage == USAGE_STREAM_FROM_SERVER) ? q = rc->avg_frame_qindex[KEY_FRAME];
active_worst_quality : rc->avg_frame_qindex[KEY_FRAME];
} }
// For constrained quality dont allow Q less than the cq level // For constrained quality dont allow Q less than the cq level
if (oxcf->end_usage == USAGE_CONSTRAINED_QUALITY) { if (oxcf->end_usage == USAGE_CONSTRAINED_QUALITY) {
...@@ -565,24 +756,10 @@ static int rc_pick_q_and_adjust_q_bounds_one_pass(const VP9_COMP *cpi, ...@@ -565,24 +756,10 @@ static int rc_pick_q_and_adjust_q_bounds_one_pass(const VP9_COMP *cpi,
active_best_quality = cpi->cq_target_quality; active_best_quality = cpi->cq_target_quality;
} else { } else {
// Use the lower of active_worst_quality and recent/average Q. // Use the lower of active_worst_quality and recent/average Q.
if (oxcf->end_usage == USAGE_STREAM_FROM_SERVER) { if (cm->current_video_frame > 1)
if (cm->current_video_frame > 1) { active_best_quality = inter_minq[rc->avg_frame_qindex[INTER_FRAME]];
if (rc->avg_frame_qindex[INTER_FRAME] < active_worst_quality) else
active_best_quality = inter_minq[rc->avg_frame_qindex[INTER_FRAME]]; active_best_quality = inter_minq[rc->avg_frame_qindex[KEY_FRAME]];
else
active_best_quality = inter_minq[active_worst_quality];
} else {
if (rc->avg_frame_qindex[KEY_FRAME] < active_worst_quality)
active_best_quality = inter_minq[rc->avg_frame_qindex[KEY_FRAME]];
else
active_best_quality = inter_minq[active_worst_quality];
}
} else {
if (cm->current_video_frame > 1)
active_best_quality = inter_minq[rc->avg_frame_qindex[INTER_FRAME]];
else
active_best_quality = inter_minq[rc->avg_frame_qindex[KEY_FRAME]];
}
// For the constrained quality mode we don't want // For the constrained quality mode we don't want
// q to fall below the cq level. // q to fall below the cq level.
if ((oxcf->end_usage == USAGE_CONSTRAINED_QUALITY) && if ((oxcf->end_usage == USAGE_CONSTRAINED_QUALITY) &&
...@@ -613,7 +790,6 @@ static int rc_pick_q_and_adjust_q_bounds_one_pass(const VP9_COMP *cpi, ...@@ -613,7 +790,6 @@ static int rc_pick_q_and_adjust_q_bounds_one_pass(const VP9_COMP *cpi,
if (!(cm->current_video_frame == 0)) if (!(cm->current_video_frame == 0))
*top_index = (active_worst_quality + active_best_quality * 3) / 4; *top_index = (active_worst_quality + active_best_quality * 3) / 4;
} else if (!rc->is_src_frame_alt_ref && } else if (!rc->is_src_frame_alt_ref &&
(oxcf->end_usage != USAGE_STREAM_FROM_SERVER) &&
(cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) { (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) {
*top_index = (active_worst_quality + active_best_quality) / 2; *top_index = (active_worst_quality + active_best_quality) / 2;
} }
...@@ -659,14 +835,14 @@ static int rc_pick_q_and_adjust_q_bounds_one_pass(const VP9_COMP *cpi, ...@@ -659,14 +835,14 @@ static int rc_pick_q_and_adjust_q_bounds_one_pass(const VP9_COMP *cpi,
return q; return q;
} }
static int rc_pick_q_and_adjust_q_bounds_two_pass(const VP9_COMP *cpi, static int rc_pick_q_and_bounds_two_pass(const VP9_COMP *cpi,
int *bottom_index, int *bottom_index,
int *top_index) { int *top_index) {
const VP9_COMMON *const cm = &cpi->common; const VP9_COMMON *const cm = &cpi->common;
const RATE_CONTROL *const rc = &cpi->rc; const RATE_CONTROL *const rc = &cpi->rc;
const VP9_CONFIG *const oxcf = &cpi->oxcf; const VP9_CONFIG *const oxcf = &cpi->oxcf;
int active_best_quality; int active_best_quality;
int active_worst_quality = rc->active_worst_quality; int active_worst_quality = cpi->twopass.active_worst_quality;
int q; int q;
if (frame_is_intra_only(cm)) { if (frame_is_intra_only(cm)) {
...@@ -850,16 +1026,18 @@ static int rc_pick_q_and_adjust_q_bounds_two_pass(const VP9_COMP *cpi, ...@@ -850,16 +1026,18 @@ static int rc_pick_q_and_adjust_q_bounds_two_pass(const VP9_COMP *cpi,
return q; return q;
} }
int vp9_rc_pick_q_and_adjust_q_bounds(const VP9_COMP *cpi, int vp9_rc_pick_q_and_bounds(const VP9_COMP *cpi,
int *bottom_index, int *bottom_index,
int *top_index) { int *top_index) {
int q; int q;
if (cpi->pass == 0) if (cpi->pass == 0) {
q = rc_pick_q_and_adjust_q_bounds_one_pass( if (cpi->oxcf.end_usage == USAGE_STREAM_FROM_SERVER)
cpi, bottom_index, top_index); q = rc_pick_q_and_bounds_one_pass_cbr(cpi, bottom_index, top_index);
else else
q = rc_pick_q_and_adjust_q_bounds_two_pass( q = rc_pick_q_and_bounds_one_pass_vbr(cpi, bottom_index, top_index);
cpi, bottom_index, top_index); } else {
q = rc_pick_q_and_bounds_two_pass(cpi, bottom_index, top_index);
}
// JBB : This is realtime mode. In real time mode the first frame // JBB : This is realtime mode. In real time mode the first frame
// should be larger. Q of 0 is disabled because we force tx size to be // should be larger. Q of 0 is disabled because we force tx size to be
...@@ -1094,36 +1272,6 @@ static int calc_iframe_target_size_one_pass_vbr(const VP9_COMP *const cpi) { ...@@ -1094,36 +1272,6 @@ static int calc_iframe_target_size_one_pass_vbr(const VP9_COMP *const cpi) {
return vp9_rc_clamp_iframe_target_size(cpi, target); return vp9_rc_clamp_iframe_target_size(cpi, target);
} }
static int calc_active_worst_quality_one_pass_vbr(const VP9_COMP *cpi) {
int active_worst_quality;
if (cpi->common.frame_type == KEY_FRAME) {
if (cpi->common.current_video_frame == 0) {
active_worst_quality = cpi->rc.worst_quality;
} else {
// Choose active worst quality twice as large as the last q.
active_worst_quality = cpi->rc.last_q[KEY_FRAME] * 2;
}
} else if (!cpi->rc.is_src_frame_alt_ref &&
(cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame)) {
if (cpi->common.current_video_frame == 1) {
active_worst_quality = cpi->rc.last_q[KEY_FRAME] * 5 / 4;
} else {
// Choose active worst quality twice as large as the last q.
active_worst_quality = cpi->rc.last_q[INTER_FRAME];
}
} else {
if (cpi->common.current_video_frame == 1) {
active_worst_quality = cpi->rc.last_q[KEY_FRAME] * 2;
} else {
// Choose active worst quality twice as large as the last q.
active_worst_quality = cpi->rc.last_q[INTER_FRAME] * 2;
}
}
if (active_worst_quality > cpi->rc.worst_quality)
active_worst_quality = cpi->rc.worst_quality;
return active_worst_quality;
}
void vp9_rc_get_one_pass_vbr_params(VP9_COMP *cpi) { void vp9_rc_get_one_pass_vbr_params(VP9_COMP *cpi) {
VP9_COMMON *const cm = &cpi->common; VP9_COMMON *const cm = &cpi->common;
RATE_CONTROL *const rc = &cpi->rc; RATE_CONTROL *const rc = &cpi->rc;
...@@ -1152,7 +1300,6 @@ void vp9_rc_get_one_pass_vbr_params(VP9_COMP *cpi) { ...@@ -1152,7 +1300,6 @@ void vp9_rc_get_one_pass_vbr_params(VP9_COMP *cpi) {
rc->source_alt_ref_pending = USE_ALTREF_FOR_ONE_PASS; rc->source_alt_ref_pending = USE_ALTREF_FOR_ONE_PASS;
rc->gfu_boost = DEFAULT_GF_BOOST; rc->gfu_boost = DEFAULT_GF_BOOST;
} }
cpi->rc.active_worst_quality = calc_active_worst_quality_one_pass_vbr(cpi);
if (cm->frame_type == KEY_FRAME) if (cm->frame_type == KEY_FRAME)
target = calc_iframe_target_size_one_pass_vbr(cpi); target = calc_iframe_target_size_one_pass_vbr(cpi);
else else
...@@ -1160,52 +1307,6 @@ void vp9_rc_get_one_pass_vbr_params(VP9_COMP *cpi) { ...@@ -1160,52 +1307,6 @@ void vp9_rc_get_one_pass_vbr_params(VP9_COMP *cpi) {
vp9_rc_set_frame_target(cpi, target); vp9_rc_set_frame_target(cpi, target);
} }
// Adjust active_worst_quality level based on buffer level.
static int calc_active_worst_quality_one_pass_cbr(const VP9_COMP *cpi) {
// Adjust active_worst_quality: If buffer is above the optimal/target level,
// bring active_worst_quality down depending on fullness of buffer.
// If buffer is below the optimal level, let the active_worst_quality go from
// ambient Q (at buffer = optimal level) to worst_quality level
// (at buffer = critical level).
const VP9_CONFIG *oxcf = &cpi->oxcf;
const RATE_CONTROL *rc = &cpi->rc;
int active_worst_quality = rc->active_worst_quality;
// Maximum limit for down adjustment, ~20%.
// Buffer level below which we push active_worst to worst_quality.
int critical_level = oxcf->optimal_buffer_level >> 2;
int adjustment = 0;
int buff_lvl_step = 0;
if (cpi->common.frame_type == KEY_FRAME)
return rc->worst_quality;
if (rc->buffer_level > oxcf->optimal_buffer_level) {
// Adjust down.
int max_adjustment_down = active_worst_quality / 5;
if (max_adjustment_down) {
buff_lvl_step = (int)((oxcf->maximum_buffer_size -
oxcf->optimal_buffer_level) / max_adjustment_down);
if (buff_lvl_step)
adjustment = (int)((rc->buffer_level - oxcf->optimal_buffer_level) /
buff_lvl_step);
active_worst_quality -= adjustment;
}
} else if (rc->buffer_level > critical_level) {
// Adjust up from ambient Q.
if (critical_level) {
buff_lvl_step = (oxcf->optimal_buffer_level - critical_level);
if (buff_lvl_step) {
adjustment = (rc->worst_quality - rc->avg_frame_qindex[INTER_FRAME]) *
(oxcf->optimal_buffer_level - rc->buffer_level) /
buff_lvl_step;
}
active_worst_quality = rc->avg_frame_qindex[INTER_FRAME] + adjustment;
}
} else {
// Set to worst_quality if buffer is below critical level.
active_worst_quality = rc->worst_quality;
}
return active_worst_quality;
}
static int calc_pframe_target_size_one_pass_cbr(const VP9_COMP *cpi) { static int calc_pframe_target_size_one_pass_cbr(const VP9_COMP *cpi) {
const VP9_CONFIG *oxcf = &cpi->oxcf; const VP9_CONFIG *oxcf = &cpi->oxcf;
const RATE_CONTROL *rc = &cpi->rc; const RATE_CONTROL *rc = &cpi->rc;
...@@ -1269,8 +1370,6 @@ void vp9_rc_get_svc_params(VP9_COMP *cpi) { ...@@ -1269,8 +1370,6 @@ void vp9_rc_get_svc_params(VP9_COMP *cpi) {
target = calc_pframe_target_size_one_pass_cbr(cpi); target = calc_pframe_target_size_one_pass_cbr(cpi);
} }
} }
cpi->rc.active_worst_quality =
calc_active_worst_quality_one_pass_cbr(cpi);
vp9_rc_set_frame_target(cpi, target); vp9_rc_set_frame_target(cpi, target);
cpi->rc.frames_till_gf_update_due = INT_MAX; cpi->rc.frames_till_gf_update_due = INT_MAX;
cpi->rc.baseline_gf_interval = INT_MAX; cpi->rc.baseline_gf_interval = INT_MAX;
...@@ -1295,8 +1394,6 @@ void vp9_rc_get_one_pass_cbr_params(VP9_COMP *cpi) { ...@@ -1295,8 +1394,6 @@ void vp9_rc_get_one_pass_cbr_params(VP9_COMP *cpi) {
cm->frame_type = INTER_FRAME; cm->frame_type = INTER_FRAME;
target = calc_pframe_target_size_one_pass_cbr(cpi);