Remove twopass->kfgroup_inter_fraction

I believe that the intended function of this factor was to take account
of the overall level of predictability in estimating the active max q.
This assumes that for better predicted content the key frame will have
a higher boost and will consume a larger proportion of the total bits for
the KF group. In fact this number does not vary that much for most longer
KF groups and is dominated instead by the length of the key frame group.

In this patch I remove the factor and work in a value that is similar to the
average seen when coding 150 frame groups in out tests.

This patch also separates the calculation of bits per MB used for estimating
maxq from that used in the normal per frame rate control as the correction
mechanisms are substantially different.

Metrics wise there is a mixed picture with NET gains in overall psnr and ssim
but average psnr and psnr-hvs are down a bit.

The exception is the HD set where there are gains on all metrics. This is
because 3 of the clips have a small KF group at the start and see gains of
5-12%, which is enough to distort the averages for the whole set. This
also illustrates how kfgroup_inter_fraction can go wrong.

Quite large changes to the unit test thresholds expected because the unit
tests have very short kf groups.

STATS_CHANGED

Change-Id: I71391f2a0d06ba9ce82f13a8f09366297bec14fc
diff --git a/av1/encoder/firstpass.h b/av1/encoder/firstpass.h
index 71037a8..6ca1215 100644
--- a/av1/encoder/firstpass.h
+++ b/av1/encoder/firstpass.h
@@ -157,9 +157,6 @@
   // Error score of frames still to be coded in kf group
   int64_t kf_group_error_left;
 
-  // The fraction for a kf groups total bits allocated to the inter frames
-  double kfgroup_inter_fraction;
-
   // Over time correction for bits per macro block estimation
   double bpm_factor;
 
diff --git a/av1/encoder/pass2_strategy.c b/av1/encoder/pass2_strategy.c
index 197d910..bbc66f3 100644
--- a/av1/encoder/pass2_strategy.c
+++ b/av1/encoder/pass2_strategy.c
@@ -163,9 +163,8 @@
 // Similar to find_qindex_by_rate() function in ratectrl.c, but includes
 // calculation of a correction_factor.
 static int find_qindex_by_rate_with_correction(
-    int desired_bits_per_mb, aom_bit_depth_t bit_depth, FRAME_TYPE frame_type,
-    double error_per_mb, double group_weight_factor, int best_qindex,
-    int worst_qindex) {
+    int desired_bits_per_mb, aom_bit_depth_t bit_depth, double error_per_mb,
+    double group_weight_factor, int best_qindex, int worst_qindex) {
   assert(best_qindex <= worst_qindex);
   int low = best_qindex;
   int high = worst_qindex;
@@ -173,8 +172,11 @@
   while (low < high) {
     const int mid = (low + high) >> 1;
     const double mid_factor = calc_correction_factor(error_per_mb, mid);
-    const int mid_bits_per_mb = av1_rc_bits_per_mb(
-        frame_type, mid, mid_factor * group_weight_factor, bit_depth);
+    const double q = av1_convert_qindex_to_q(mid, bit_depth);
+    const int enumerator = 1600000 + ((int)(1600000 * q) >> 12);
+    const int mid_bits_per_mb =
+        (int)((enumerator * mid_factor * group_weight_factor) / q);
+
     if (mid_bits_per_mb > desired_bits_per_mb) {
       low = mid + 1;
     } else {
@@ -209,7 +211,7 @@
     // Try and pick a max Q that will be high enough to encode the
     // content at the given rate.
     int q = find_qindex_by_rate_with_correction(
-        target_norm_bits_per_mb, cpi->common.seq_params.bit_depth, INTER_FRAME,
+        target_norm_bits_per_mb, cpi->common.seq_params.bit_depth,
         av_err_per_mb, group_weight_factor, rc->best_quality,
         rc->worst_quality);
 
@@ -1206,7 +1208,7 @@
     }
     tmp_q = get_twopass_worst_quality(
         cpi, group_av_err, (group_av_skip_pct + group_av_inactive_zone),
-        vbr_group_bits_per_frame, twopass->kfgroup_inter_fraction * rc_factor);
+        vbr_group_bits_per_frame, rc_factor);
     rc->active_worst_quality = AOMMAX(tmp_q, rc->active_worst_quality >> 1);
   }
 #endif
@@ -1611,16 +1613,6 @@
   kf_bits = adjust_boost_bits_for_target_level(cpi, kf_bits,
                                                twopass->kf_group_bits, 0);
 
-  // Work out the fraction of the kf group bits reserved for the inter frames
-  // within the group after discounting the bits for the kf itself.
-  if (twopass->kf_group_bits) {
-    twopass->kfgroup_inter_fraction =
-        (double)(twopass->kf_group_bits - kf_bits) /
-        (double)twopass->kf_group_bits;
-  } else {
-    twopass->kfgroup_inter_fraction = 1.0;
-  }
-
   twopass->kf_group_bits -= kf_bits;
 
   // Save the bits to spend on the key frame.
@@ -1961,18 +1953,20 @@
     AV1_COMMON *cm = &cpi->common;
     FILE *fpfile;
     fpfile = fopen("details.stt", "a");
-    fprintf(fpfile, "%10d %10d %10d %10"PRId64" %10"PRId64" %10d %10d %10d %10.4lf %10.4lf %10.4lf %10.4lf\n",
-            cm->current_frame.frame_number,
-            rc->base_frame_target, rc->projected_frame_size,
-            rc->total_actual_bits, rc->vbr_bits_off_target,
-            rc->rate_error_estimate,
+    fprintf(fpfile,
+            "%10d %10d %10d %10" PRId64 " %10" PRId64
+            " %10d %10d %10d %10.4lf %10.4lf %10.4lf %10.4lf\n",
+            cm->current_frame.frame_number, rc->base_frame_target,
+            rc->projected_frame_size, rc->total_actual_bits,
+            rc->vbr_bits_off_target, rc->rate_error_estimate,
             twopass->rolling_arf_group_target_bits,
             twopass->rolling_arf_group_actual_bits,
             (double)twopass->rolling_arf_group_actual_bits /
                 (double)twopass->rolling_arf_group_target_bits,
             twopass->bpm_factor,
             av1_convert_qindex_to_q(cm->base_qindex, cm->seq_params.bit_depth),
-            av1_convert_qindex_to_q(rc->active_worst_quality, cm->seq_params.bit_depth));
+            av1_convert_qindex_to_q(rc->active_worst_quality,
+                                    cm->seq_params.bit_depth));
     fclose(fpfile);
   }
 #endif
diff --git a/test/fwd_kf_test.cc b/test/fwd_kf_test.cc
index bc85285..6543d85 100644
--- a/test/fwd_kf_test.cc
+++ b/test/fwd_kf_test.cc
@@ -23,8 +23,8 @@
 } FwdKfTestParam;
 
 const FwdKfTestParam kTestParams[] = {
-  { 4, 36.6 },  { 6, 35.7 },  { 8, 35.0 },
-  { 12, 33.6 }, { 16, 33.5 }, { 18, 33.1 }
+  { 4, 34.1 },  { 6, 33.4 },  { 8, 33.0 },
+  { 12, 32.4 }, { 16, 32.3 }, { 18, 32.1 }
 };
 
 class ForwardKeyTest
diff --git a/test/gf_max_pyr_height_test.cc b/test/gf_max_pyr_height_test.cc
index 43cf09c..7f4c764 100644
--- a/test/gf_max_pyr_height_test.cc
+++ b/test/gf_max_pyr_height_test.cc
@@ -21,7 +21,7 @@
   int gf_max_pyr_height;
   double psnr_thresh;
 } kTestParams[] = {
-  { 0, 34.2 }, { 1, 34.35 }, { 2, 34.9 }, { 3, 35.1 }, { 4, 35.2 },
+  { 0, 33.4 }, { 1, 33.50 }, { 2, 34.0 }, { 3, 34.2 }, { 4, 34.3 },
 };
 
 // Compiler may decide to add some padding to the struct above for alignment,
diff --git a/test/horz_superres_test.cc b/test/horz_superres_test.cc
index 9a90ffb..dbdb088 100644
--- a/test/horz_superres_test.cc
+++ b/test/horz_superres_test.cc
@@ -41,7 +41,7 @@
 } TestVideoParam;
 
 const TestVideoParam kTestVideoVectors[] = {
-  { "park_joy_90p_8_420.y4m", AOM_IMG_FMT_I420, AOM_BITS_8, 0, 5, 0, 26.0 },
+  { "park_joy_90p_8_420.y4m", AOM_IMG_FMT_I420, AOM_BITS_8, 0, 5, 0, 25.8 },
 #if CONFIG_AV1_HIGHBITDEPTH
   { "park_joy_90p_10_444.y4m", AOM_IMG_FMT_I44416, AOM_BITS_10, 1, 5, 0, 28.0 },
 #endif