Further Lambda scaling tweaks.

Change I5592b2aa adjusted the scaling of the rd lambda to be based
on layer depth not frame type.

Some gains were reported for low res and midres sets in ssim though
the results were mixed in opsnr.

However there was a significant regression in rate accuracy.

This change tweaks the layer numbers for ARFs a little further and gets
some additional metrics gains of ~0.2 to 0.25% (lowres2 and ugc360) up
to -0.4 to -0.55% for midres2 at typical streaming rates. To counter
balance the impact of these changes on rate there is also a small
change to the calculation used to define n allowed Q range.  In effect
the RD lambda will tend to reduce the number of bits spent for a given Q
by reducing bits spent in the prediction signal. This is countered by
lowering Q slightly which will shift the bit allocation balance slightly
in favor of the residual signal.

The main testing was done using versions of the test sets that have
 a denser pattern of points at typical streaming rates. For the default
lowres set which has some very extreme data points, there was a slight
regression but there were still gains for the other sets.

Some refactoring and elimination of duplication.

STATS_CHANGED

Change-Id: Ie1cc0f911cb9f413f7ac7e41d829e8e3cfef61f9
diff --git a/av1/encoder/pass2_strategy.c b/av1/encoder/pass2_strategy.c
index 3a27a7d..51b378e 100644
--- a/av1/encoder/pass2_strategy.c
+++ b/av1/encoder/pass2_strategy.c
@@ -191,7 +191,7 @@
 }
 
 static int qbpm_enumerator(int rate_err_tol) {
-  return 1250000 + ((300000 * AOMMIN(75, AOMMAX(rate_err_tol - 25, 0))) / 75);
+  return 1200000 + ((300000 * AOMMIN(75, AOMMAX(rate_err_tol - 25, 0))) / 75);
 }
 
 // Similar to find_qindex_by_rate() function in ratectrl.c, but includes
diff --git a/av1/encoder/rd.c b/av1/encoder/rd.c
index f054114..c053048 100644
--- a/av1/encoder/rd.c
+++ b/av1/encoder/rd.c
@@ -349,14 +349,15 @@
 
 static const int rd_boost_factor[16] = { 64, 32, 32, 32, 24, 16, 12, 12,
                                          8,  8,  4,  4,  2,  2,  1,  0 };
-static const int rd_layer_depth_factor[6] = {
-  128, 128, 144, 160, 160, 180,
+
+static const int rd_layer_depth_factor[7] = {
+  160, 160, 160, 160, 192, 208, 224
 };
 
 int av1_compute_rd_mult_based_on_qindex(const AV1_COMP *cpi, int qindex) {
   const int q = av1_dc_quant_QTX(qindex, 0, cpi->common.seq_params.bit_depth);
-  int rdmult = q * q;
-  rdmult = rdmult * 3 + (rdmult * 2 / 3);
+  int rdmult = (int)(((int64_t)88 * q * q) / 24);
+
   switch (cpi->common.seq_params.bit_depth) {
     case AOM_BITS_8: break;
     case AOM_BITS_10: rdmult = ROUND_POWER_OF_TWO(rdmult, 4); break;
@@ -374,9 +375,12 @@
       (cpi->common.current_frame.frame_type != KEY_FRAME)) {
     const GF_GROUP *const gf_group = &cpi->gf_group;
     const int boost_index = AOMMIN(15, (cpi->rc.gfu_boost / 100));
-    const int layer_depth = AOMMIN(gf_group->layer_depth[gf_group->index], 5);
+    const int layer_depth = AOMMIN(gf_group->layer_depth[gf_group->index], 6);
 
+    // Layer depth adjustment
     rdmult = (rdmult * rd_layer_depth_factor[layer_depth]) >> 7;
+
+    // ARF boost adjustment
     rdmult += ((rdmult * rd_boost_factor[boost_index]) >> 7);
   }
   return (int)rdmult;
@@ -404,33 +408,10 @@
 int av1_get_adaptive_rdmult(const AV1_COMP *cpi, double beta) {
   assert(beta > 0.0);
   const AV1_COMMON *cm = &cpi->common;
-  int64_t q = av1_dc_quant_QTX(cm->quant_params.base_qindex, 0,
-                               cm->seq_params.bit_depth);
-  int64_t rdmult = 0;
+  int q = av1_dc_quant_QTX(cm->quant_params.base_qindex, 0,
+                           cm->seq_params.bit_depth);
 
-  switch (cm->seq_params.bit_depth) {
-    case AOM_BITS_8: rdmult = (int)((88 * q * q / beta) / 24); break;
-    case AOM_BITS_10:
-      rdmult = ROUND_POWER_OF_TWO((int)((88 * q * q / beta) / 24), 4);
-      break;
-    default:
-      assert(cm->seq_params.bit_depth == AOM_BITS_12);
-      rdmult = ROUND_POWER_OF_TWO((int)((88 * q * q / beta) / 24), 8);
-      break;
-  }
-
-  if (is_stat_consumption_stage(cpi) &&
-      (cm->current_frame.frame_type != KEY_FRAME)) {
-    const GF_GROUP *const gf_group = &cpi->gf_group;
-    const int boost_index = AOMMIN(15, (cpi->rc.gfu_boost / 100));
-
-    const int layer_depth = AOMMIN(gf_group->layer_depth[gf_group->index], 5);
-    rdmult = (rdmult * rd_layer_depth_factor[layer_depth]) >> 7;
-
-    rdmult += ((rdmult * rd_boost_factor[boost_index]) >> 7);
-  }
-  if (rdmult < 1) rdmult = 1;
-  return (int)rdmult;
+  return (int)(av1_compute_rd_mult(cpi, q) / beta);
 }
 
 static int compute_rd_thresh_factor(int qindex, aom_bit_depth_t bit_depth) {