Further tweaks to rate adaption.

This is a further experimental change to the rate adaptation.
The rate error factor is based on the bits off target error
measured against the larger of the actual bits used so far and
the projected number of bits left. This tends to naturally damp
the factor so that it is likely to be less extreme at the two ends
of the clip.

A larger adjustment is allowed at the end of the first GOP.
The specified undershoot and overshoot limits are used to
define the maximum amount of adjustment allowed.

This gives a small improvement in average psnr and vmaf scores
but a  small drop in ssim.  However, the average absolute
rate error improves by almost 2%.

STATS_CHANGED

Change-Id: Ia7d2bc833a4266ec92e9c4e78caa4d80ed72f3af
diff --git a/av1/encoder/pass2_strategy.c b/av1/encoder/pass2_strategy.c
index d067d50e8..173b884 100644
--- a/av1/encoder/pass2_strategy.c
+++ b/av1/encoder/pass2_strategy.c
@@ -180,23 +180,46 @@
   return fclamp(pow(error_term, power_term), 0.05, 5.0);
 }
 
-static void twopass_update_bpm_factor(TWO_PASS *twopass, int err_estimate,
-                                      int rate_err_tol) {
+// Based on history adjust expectations of bits per macroblock.
+static void twopass_update_bpm_factor(AV1_COMP *cpi, int rate_err_tol) {
+  TWO_PASS *twopass = &cpi->twopass;
+  const RATE_CONTROL *const rc = &cpi->rc;
+  int err_estimate = rc->rate_error_estimate;
+
   // Based on recent history adjust expectations of bits per macroblock.
-  double last_group_rate_err =
-      (double)twopass->rolling_arf_group_actual_bits /
-      DOUBLE_DIVIDE_CHECK((double)twopass->rolling_arf_group_target_bits);
   double damp_fac = AOMMAX(5.0, rate_err_tol / 10.0);
+  double rate_err_factor = 1.0;
+  const double adj_limit = AOMMAX(0.20, (double)(100 - rate_err_tol) / 200.0);
+  const double min_fac = 1.0 - adj_limit;
+  const double max_fac = 1.0 + adj_limit;
 
-  last_group_rate_err = AOMMAX(0.75, AOMMIN(1.25, last_group_rate_err));
-  last_group_rate_err = 1.0 + ((last_group_rate_err - 1.0) / damp_fac);
+  if (rc->vbr_bits_off_target && rc->total_actual_bits > 0) {
+    if (cpi->lap_enabled) {
+      rate_err_factor =
+          (double)twopass->rolling_arf_group_actual_bits /
+          DOUBLE_DIVIDE_CHECK((double)twopass->rolling_arf_group_target_bits);
+    } else {
+      rate_err_factor =
+          1.0 - ((double)(rc->vbr_bits_off_target) /
+                 AOMMAX(rc->total_actual_bits, cpi->twopass.bits_left));
+    }
 
-  // Is the last GOP error making the total error worse or better? Only make
+    rate_err_factor = AOMMAX(min_fac, AOMMIN(max_fac, rate_err_factor));
+
+    // Adjustment is damped if this is 1 pass with look ahead processing
+    // (as there are only ever a few frames of data) and for all but the first
+    // GOP in normal two pass.
+    if ((twopass->bpm_factor != 1.0) || cpi->lap_enabled) {
+      rate_err_factor = 1.0 + ((rate_err_factor - 1.0) / damp_fac);
+    }
+  }
+
+  // Is the rate control trending in the right direction. Only make
   // an adjustment if things are getting worse.
-  if ((last_group_rate_err < 1.0 && err_estimate > 0) ||
-      (last_group_rate_err > 1.0 && err_estimate < 0)) {
-    twopass->bpm_factor *= last_group_rate_err;
-    twopass->bpm_factor = AOMMAX(0.75, AOMMIN(1.25, twopass->bpm_factor));
+  if ((rate_err_factor < 1.0 && err_estimate > 0) ||
+      (rate_err_factor > 1.0 && err_estimate < 0)) {
+    twopass->bpm_factor *= rate_err_factor;
+    twopass->bpm_factor = AOMMAX(min_fac, AOMMIN(max_fac, twopass->bpm_factor));
   }
 }
 
@@ -274,8 +297,7 @@
     int rate_err_tol = AOMMIN(rc_cfg->under_shoot_pct, rc_cfg->over_shoot_pct);
 
     // Update bpm correction factor based on previous GOP rate error.
-    twopass_update_bpm_factor(&cpi->twopass, rc->rate_error_estimate,
-                              rate_err_tol);
+    twopass_update_bpm_factor(cpi, rate_err_tol);
 
     // Try and pick a max Q that will be high enough to encode the
     // content at the given rate.