Rework qp estimate for tpl model

Rework GOP QP estimate for tpl model build.

STATS_CHANGED

Change-Id: Id977bab6b28f767dafc6e95e9223e1c4b85b82f3
diff --git a/av1/encoder/ratectrl.c b/av1/encoder/ratectrl.c
index cd70e38..b8c3fa6 100644
--- a/av1/encoder/ratectrl.c
+++ b/av1/encoder/ratectrl.c
@@ -805,9 +805,9 @@
   return q;
 }
 
-static int gf_group_pyramid_level(const AV1_COMP *cpi) {
+static int gf_group_pyramid_level(const AV1_COMP *cpi, int gf_index) {
   const GF_GROUP *gf_group = &cpi->gf_group;
-  int this_height = gf_group->pyramid_level[gf_group->index];
+  int this_height = gf_group->pyramid_level[gf_index];
   return this_height;
 }
 
@@ -1377,7 +1377,7 @@
       } else if (is_intrl_arf_boost) {
         assert(rc->arf_q >= 0);  // Ensure it is set to a valid value.
         active_best_quality = rc->arf_q;
-        int this_height = gf_group_pyramid_level(cpi);
+        int this_height = gf_group_pyramid_level(cpi, gf_index);
         while (this_height < gf_group->pyramid_height) {
           active_best_quality = (active_best_quality + cq_level + 1) / 2;
           ++this_height;
@@ -1398,7 +1398,7 @@
           assert(rc->arf_q >= 0);  // Ensure it is set to a valid value.
           assert(is_intrl_arf_boost);
           active_best_quality = rc->arf_q;
-          int this_height = gf_group_pyramid_level(cpi);
+          int this_height = gf_group_pyramid_level(cpi, gf_index);
           while (this_height < gf_group->pyramid_height) {
             active_best_quality = (active_best_quality + cq_level + 1) / 2;
             ++this_height;
@@ -1412,7 +1412,7 @@
 
       active_best_quality = min_boost - (int)(boost * rc->arf_boost_factor);
       if (is_intrl_arf_boost) {
-        int this_height = gf_group_pyramid_level(cpi);
+        int this_height = gf_group_pyramid_level(cpi, gf_index);
         while (this_height < gf_group->pyramid_height) {
           active_best_quality =
               (active_best_quality + active_worst_quality + 1) / 2;
@@ -1444,6 +1444,7 @@
   assert(*bottom_index <= rc->worst_quality &&
          *bottom_index >= rc->best_quality);
   assert(q <= rc->worst_quality && q >= rc->best_quality);
+
   return q;
 }
 
diff --git a/av1/encoder/tpl_model.c b/av1/encoder/tpl_model.c
index 54598c1..5b92f4d 100644
--- a/av1/encoder/tpl_model.c
+++ b/av1/encoder/tpl_model.c
@@ -706,7 +706,26 @@
 void av1_tpl_setup_stats(AV1_COMP *cpi,
                          const EncodeFrameParams *const frame_params,
                          const EncodeFrameInput *const frame_input) {
+  AV1_COMMON *cm = &cpi->common;
   GF_GROUP *gf_group = &cpi->gf_group;
+  int bottom_index, top_index;
+  EncodeFrameParams this_frame_params = *frame_params;
+
+  for (int gf_index = gf_group->index; gf_index < gf_group->size; ++gf_index) {
+    av1_configure_buffer_updates(cpi, &this_frame_params,
+                                 gf_group->update_type[gf_index], 0);
+
+    cpi->refresh_last_frame = this_frame_params.refresh_last_frame;
+    cpi->refresh_golden_frame = this_frame_params.refresh_golden_frame;
+    cpi->refresh_bwd_ref_frame = this_frame_params.refresh_bwd_ref_frame;
+    cpi->refresh_alt2_ref_frame = this_frame_params.refresh_alt2_ref_frame;
+    cpi->refresh_alt_ref_frame = this_frame_params.refresh_alt_ref_frame;
+
+    gf_group->q_val[gf_index] = av1_rc_pick_q_and_bounds(
+        cpi, cm->width, cm->height, gf_index, &bottom_index, &top_index);
+
+    cm->current_frame.frame_type = INTER_FRAME;
+  }
 
   init_gop_frames_for_tpl(cpi, frame_params, gf_group,
                           &cpi->tpl_gf_group_frames, frame_input);
@@ -727,6 +746,10 @@
       if (is_process) mc_flow_dispenser(cpi, frame_idx);
     }
   }
+
+  av1_configure_buffer_updates(cpi, &this_frame_params,
+                               gf_group->update_type[gf_group->index], 0);
+  cm->current_frame.frame_type = frame_params->frame_type;
 }
 
 static void get_tpl_forward_stats(AV1_COMP *cpi, MACROBLOCK *x, MACROBLOCKD *xd,