Commit 5b44ef0c authored by paulwilkins's avatar paulwilkins

Respond more rapidly to excessive local overshoot.

This patch attempts to address a bug reported for 4K video.
https://b.corp.google.com/issues/62215394

In this instance a perfect storm of a moderate complexity section
followed by a much easier section where a CGI overlay helped to
suppress film grain noise, followed by a much harder and very grainy
section at the end, cause a massive local rate spike that pushed a chunk
over the upper allowed rate limit.

This patch detects cases where the rate for a frame is much higher than
expected and allows, in this special case, for rapid adjustment of the active
Q range.

For the example chunk in the bug report the target rate was 18Mb/s and the
observed rate was over 37 Mb/s with a surge for the last few frames to over
100Mb/s. This patch brings the overall chunk rate right back down to ~18.2 Mbit/s
and  almost completely eliminates the rate spike at the end. (See graphs appended
to bug report)

Also see  I108da7ca42f3bc95c5825dd33c9d84583227dac1 which fixes a bug
unearthed during testing of this patch and also has a bearing on high rate
encodes such as 4K.

This patch does have a negative impact on some metrics. Most notably there are
clips in our standard test set where it hurts global psnr (though in many cases it
conversely helps SSIM, FAST SSIM and PSNR-HVS). It is also worth noting that
the clips (and data rates) where there is a big metric impact, are almost all cases
where there is currently a significant overshoot vs the target rate and overall rate
accuracy is greatly improved.

Change-Id: I692311a709ccdb6003e705103de9d05b59bf840a
parent 7d526c16
......@@ -73,7 +73,7 @@ TEST_P(LevelTest, TestTargetLevel11Large) {
target_level_ = 11;
cfg_.rc_target_bitrate = 150;
ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
ASSERT_EQ(target_level_, level_);
ASSERT_GE(target_level_, level_);
}
TEST_P(LevelTest, TestTargetLevel20Large) {
......@@ -83,7 +83,7 @@ TEST_P(LevelTest, TestTargetLevel20Large) {
target_level_ = 20;
cfg_.rc_target_bitrate = 1200;
ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
ASSERT_EQ(target_level_, level_);
ASSERT_GE(target_level_, level_);
}
TEST_P(LevelTest, TestTargetLevel31Large) {
......@@ -103,11 +103,11 @@ TEST_P(LevelTest, TestTargetLevel0) {
target_level_ = 0;
min_gf_internal_ = 4;
ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
ASSERT_EQ(11, level_);
ASSERT_GE(11, level_);
cfg_.rc_target_bitrate = 1600;
ASSERT_NO_FATAL_FAILURE(RunLoop(&video));
ASSERT_EQ(20, level_);
ASSERT_GE(20, level_);
}
// Test for level control being turned off
......
......@@ -2756,11 +2756,33 @@ static int scale_down(VP9_COMP *cpi, int q) {
return scale;
}
static int big_rate_miss(VP9_COMP *cpi, int high_limit, int low_limit) {
static int big_rate_miss_high_threshold(VP9_COMP *cpi) {
const RATE_CONTROL *const rc = &cpi->rc;
int big_miss_high;
return (rc->projected_frame_size > ((high_limit * 3) / 2)) ||
(rc->projected_frame_size < (low_limit / 2));
if (frame_is_kf_gf_arf(cpi))
big_miss_high = rc->this_frame_target * 3 / 2;
else
big_miss_high = rc->this_frame_target * 2;
return big_miss_high;
}
static int big_rate_miss(VP9_COMP *cpi) {
const RATE_CONTROL *const rc = &cpi->rc;
int big_miss_high;
int big_miss_low;
// Ignore for overlay frames
if (rc->is_src_frame_alt_ref) {
return 0;
} else {
big_miss_low = (rc->this_frame_target / 2);
big_miss_high = big_rate_miss_high_threshold(cpi);
return (rc->projected_frame_size > big_miss_high) ||
(rc->projected_frame_size < big_miss_low);
}
}
// test in two pass for the first
......@@ -2785,8 +2807,7 @@ static int recode_loop_test(VP9_COMP *cpi, int high_limit, int low_limit, int q,
int force_recode = 0;
if ((rc->projected_frame_size >= rc->max_frame_bandwidth) ||
big_rate_miss(cpi, high_limit, low_limit) ||
(cpi->sf.recode_loop == ALLOW_RECODE) ||
big_rate_miss(cpi) || (cpi->sf.recode_loop == ALLOW_RECODE) ||
(two_pass_first_group_inter(cpi) &&
(cpi->sf.recode_loop == ALLOW_RECODE_FIRST)) ||
(frame_is_kfgfarf && (cpi->sf.recode_loop >= ALLOW_RECODE_KFARFGF))) {
......@@ -2796,8 +2817,12 @@ static int recode_loop_test(VP9_COMP *cpi, int high_limit, int low_limit, int q,
cpi->resize_pending = 1;
return 1;
}
// Force recode if projected_frame_size > max_frame_bandwidth
if (rc->projected_frame_size >= rc->max_frame_bandwidth) return 1;
// Force recode for extreme overshoot.
if ((rc->projected_frame_size >= rc->max_frame_bandwidth) ||
(rc->projected_frame_size >= big_rate_miss_high_threshold(cpi))) {
return 1;
}
// TODO(agrange) high_limit could be greater than the scale-down threshold.
if ((rc->projected_frame_size > high_limit && q < maxq) ||
......@@ -3849,11 +3874,16 @@ static void encode_with_recode_loop(VP9_COMP *cpi, size_t *size,
// Frame is too large
if (rc->projected_frame_size > rc->this_frame_target) {
// Special case if the projected size is > the max allowed.
if (rc->projected_frame_size >= rc->max_frame_bandwidth) {
if ((q == q_high) &&
((rc->projected_frame_size >= rc->max_frame_bandwidth) ||
(rc->projected_frame_size >=
big_rate_miss_high_threshold(cpi)))) {
int max_rate = VPXMAX(1, VPXMIN(rc->max_frame_bandwidth,
big_rate_miss_high_threshold(cpi)));
double q_val_high;
q_val_high = vp9_convert_qindex_to_q(q_high, cm->bit_depth);
q_val_high = q_val_high * ((double)rc->projected_frame_size /
rc->max_frame_bandwidth);
q_val_high =
q_val_high * ((double)rc->projected_frame_size / max_rate);
q_high = vp9_convert_q_to_qindex(q_val_high, cm->bit_depth);
q_high = clamp(q_high, rc->best_quality, rc->worst_quality);
}
......@@ -3862,7 +3892,6 @@ static void encode_with_recode_loop(VP9_COMP *cpi, size_t *size,
qstep =
get_qstep_adj(rc->projected_frame_size, rc->this_frame_target);
q_low = VPXMIN(q + qstep, q_high);
// q_low = q < q_high ? q + 1 : q_high;
if (undershoot_seen || loop_at_this_size > 1) {
// Update rate_correction_factor unless
......@@ -3890,15 +3919,14 @@ static void encode_with_recode_loop(VP9_COMP *cpi, size_t *size,
qstep =
get_qstep_adj(rc->this_frame_target, rc->projected_frame_size);
q_high = VPXMAX(q - qstep, q_low);
// q_high = q > q_low ? q - 1 : q_low;
if (overshoot_seen || loop_at_this_size > 1) {
vp9_rc_update_rate_correction_factors(cpi);
q = (q_high + q_low) / 2;
} else {
vp9_rc_update_rate_correction_factors(cpi);
q = vp9_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
top_index);
q = vp9_rc_regulate_q(cpi, rc->this_frame_target,
VPXMIN(q_low, bottom_index), top_index);
// Special case reset for qlow for constrained quality.
// This should only trigger where there is very substantial
// undershoot on a frame and the auto cq level is above
......@@ -3909,12 +3937,11 @@ static void encode_with_recode_loop(VP9_COMP *cpi, size_t *size,
while (q > q_high && retries < 10) {
vp9_rc_update_rate_correction_factors(cpi);
q = vp9_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
top_index);
q = vp9_rc_regulate_q(cpi, rc->this_frame_target,
VPXMIN(q_low, bottom_index), top_index);
retries++;
}
}
undershoot_seen = 1;
}
......@@ -3950,6 +3977,14 @@ static void encode_with_recode_loop(VP9_COMP *cpi, size_t *size,
cpi->twopass.active_worst_quality =
VPXMIN(q + qrange_adj, cpi->oxcf.worst_allowed_q);
}
#else
if (!frame_is_kf_gf_arf(cpi)) {
// Have we been forced to adapt Q outside the expected range by an extreme
// rate miss. If so adjust the active maxQ for the subsequent frames.
if (q > cpi->twopass.active_worst_quality) {
cpi->twopass.active_worst_quality = q;
}
}
#endif
if (enable_acl) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment