Add scale factor to vbr_rc_info.

The scale factor is used to normalize the estimated bit rate in
accordance to the actual bit rate.
Setting the scale factor to 1 has no effect. This value will be changed
in a future CL.

BUG=aomedia:3045

Change-Id: I88c32c1535a35ad163de6a4ff9aed5e37a312798
diff --git a/av1/encoder/encode_strategy.c b/av1/encoder/encode_strategy.c
index 52dba95..3da9959 100644
--- a/av1/encoder/encode_strategy.c
+++ b/av1/encoder/encode_strategy.c
@@ -1042,10 +1042,10 @@
         // Use the gop_bit_budget to determine gf_group->q_val.
         const double arf_qstep_ratio =
             av1_tpl_get_qstep_ratio(&cpi->ppi->tpl_data, cpi->gf_frame_index);
-        av1_q_mode_estimate_base_q(&cpi->ppi->gf_group,
-                                   cpi->ppi->tpl_data.txfm_stats_list,
-                                   gop_bit_budget, cpi->gf_frame_index,
-                                   arf_qstep_ratio, cm->seq_params->bit_depth);
+        av1_q_mode_estimate_base_q(
+            &cpi->ppi->gf_group, cpi->ppi->tpl_data.txfm_stats_list,
+            gop_bit_budget, cpi->gf_frame_index, arf_qstep_ratio,
+            cm->seq_params->bit_depth, cpi->vbr_rc_info.scale_factor);
       }
 #endif
     }
diff --git a/av1/encoder/pass2_strategy.c b/av1/encoder/pass2_strategy.c
index c81fa23..c32830b 100644
--- a/av1/encoder/pass2_strategy.c
+++ b/av1/encoder/pass2_strategy.c
@@ -1074,7 +1074,7 @@
           av1_q_mode_estimate_base_q(
               &cpi->ppi->gf_group, cpi->ppi->tpl_data.txfm_stats_list,
               gop_bit_budget, cpi->gf_frame_index, arf_qstep_ratio,
-              cpi->common.seq_params->bit_depth);
+              cpi->common.seq_params->bit_depth, cpi->vbr_rc_info.scale_factor);
         }
 #endif  // CONFIG_BITRATE_ACCURACY
       }
diff --git a/av1/encoder/tpl_model.c b/av1/encoder/tpl_model.c
index aee3828..0ca74ac 100644
--- a/av1/encoder/tpl_model.c
+++ b/av1/encoder/tpl_model.c
@@ -1965,7 +1965,7 @@
                                const TplTxfmStats *txfm_stats_list,
                                double bit_budget, int gf_frame_index,
                                double arf_qstep_ratio,
-                               aom_bit_depth_t bit_depth) {
+                               aom_bit_depth_t bit_depth, double scale_factor) {
   int q_max = 255;  // Maximum q value.
   int q_min = 0;    // Minimum q value.
   int q = (q_max + q_min) / 2;
@@ -1986,6 +1986,8 @@
     double estimate = av1_estimate_gop_bitrate(gf_group->q_val, gf_group->size,
                                                txfm_stats_list);
 
+    estimate *= scale_factor;
+
     // We want to find the lowest q that satisfies the bit budget constraint.
     // A binary search narrows the result down to two values: q_min and q_max.
     if (q_max <= q_min + 1 || estimate == bit_budget) {
diff --git a/av1/encoder/tpl_model.h b/av1/encoder/tpl_model.h
index b5aabf8..fec81cf 100644
--- a/av1/encoder/tpl_model.h
+++ b/av1/encoder/tpl_model.h
@@ -237,7 +237,8 @@
   int show_frame_count;     // Number of show frames in the entire video
 
   int gop_showframe_count;  // The number of show frames in the current gop
-  double gop_bit_budget;    // The bitbudger for the current gop
+  double gop_bit_budget;    // The bitbudget for the current gop
+  double scale_factor;      // Scale factor to improve the budget estimation
 } VBR_RATECTRL_INFO;
 
 static INLINE void vbr_rc_init(VBR_RATECTRL_INFO *vbr_rc_info,
@@ -245,6 +246,7 @@
   vbr_rc_info->total_bit_budget = total_bit_budget;
   vbr_rc_info->show_frame_count = show_frame_count;
   vbr_rc_info->keyframe_bitrate = 0;
+  vbr_rc_info->scale_factor = 1.0;
 }
 
 static INLINE void vbr_rc_set_gop_bit_budget(VBR_RATECTRL_INFO *vbr_rc_info,
@@ -489,6 +491,7 @@
  * \param[in]       gf_frame_index    current frame in the GOP
  * \param[in]       arf_qstep_ratio   ARF q step ratio
  * \param[in]       bit_depth         bit depth
+ * \param[in]       scale_factor      Used to improve budget estimation
  *
  * \return Returns the optimal base q index to use.
  */
@@ -496,7 +499,7 @@
                                const TplTxfmStats *txfm_stats_list,
                                double bit_budget, int gf_frame_index,
                                double arf_qstep_ratio,
-                               aom_bit_depth_t bit_depth);
+                               aom_bit_depth_t bit_depth, double scale_factor);
 
 /*!\brief Get current frame's q_index from tpl stats and leaf_qindex
  *
diff --git a/test/tpl_model_test.cc b/test/tpl_model_test.cc
index 720804e..bd6f4cc 100644
--- a/test/tpl_model_test.cc
+++ b/test/tpl_model_test.cc
@@ -271,6 +271,7 @@
   const int gf_frame_index = 0;
   const double arf_qstep_ratio = 2;
   const aom_bit_depth_t bit_depth = AOM_BITS_8;
+  const double scale_factor = 1.0;
 
   for (int i = 0; i < gf_group.size; i++) {
     gf_group.update_type[i] = gf_group_update_types[i];
@@ -288,9 +289,9 @@
 
   for (double bit_budget : bit_budgets) {
     // Binary search method to find the optimal q.
-    const int result =
-        av1_q_mode_estimate_base_q(&gf_group, stats_list, bit_budget,
-                                   gf_frame_index, arf_qstep_ratio, bit_depth);
+    const int result = av1_q_mode_estimate_base_q(
+        &gf_group, stats_list, bit_budget, gf_frame_index, arf_qstep_ratio,
+        bit_depth, scale_factor);
 
     const int test_result =
         find_gop_q_iterative(bit_budget, arf_qstep_ratio, gf_group, stats_list,