Enable full pipeline in qmode rc encoder

Change-Id: I529a579480556fce80928275e5c0b9722b38bc9b
diff --git a/apps/qmode_rc_encoder.cc b/apps/qmode_rc_encoder.cc
index 8e7c3a6..b4a4263 100644
--- a/apps/qmode_rc_encoder.cc
+++ b/apps/qmode_rc_encoder.cc
@@ -24,7 +24,7 @@
   std::string input_file = "/export/hda3/Videos/derf/bus_cif.y4m";
   aom_rational_t frame_rate = { 30, 1 };
   aom::VideoInfo input_video = { 352, 288,       frame_rate, AOM_IMG_FMT_I420,
-                                 55,  input_file };
+                                 25,  input_file };
   aom::DuckyEncode ducky_encode(input_video);
   aom::AV1RateControlQMode qmode_rc;
   aom::RateControlParam rc_param = {};
@@ -33,6 +33,7 @@
   rc_param.max_gop_show_frame_count = 16;
   rc_param.min_gop_show_frame_count = 4;
   rc_param.ref_frame_table_size = 7;
+  rc_param.base_q_index = 128;
 
   const aom::Status status = qmode_rc.SetRcParam(rc_param);
 
@@ -66,25 +67,21 @@
 
     // TODO(jingning): Extract the tpl stats through ducky_encode and make
     // frame encoding decisions.
-    aom::GopEncodeInfo gop_encode_info;
-    // = qmode_rc.GetGopEncodeInfo(gop_struct, tpl_gop_stats, ref_frame_table);
+    aom::GopEncodeInfo gop_encode_info =
+        qmode_rc.GetGopEncodeInfo(gop_struct, tpl_gop_stats, ref_frame_table);
     ref_frame_table = gop_encode_info.final_snapshot;
     gop_encode_info_list.push_back(gop_encode_info);
   }
-
   ducky_encode.EndEncode();
 
   fprintf(stderr, "tpl stats completed.\n");
 
-  // TODO(jingning): Re-enable the next final encoding stage once the TPL stats
-  // collection is done.
-  return 0;
-
   // Full encoding of the video sequence.
   // Do binary search with rc_param.base_q_index around this block.
   ducky_encode.StartEncode(frame_stats);
   ducky_encode.EncodeVideo(gop_list, gop_encode_info_list);
   ducky_encode.EndEncode();
 
+  fprintf(stderr, "final encoding completed.\n");
   return 0;
 }
diff --git a/av1/ducky_encode.cc b/av1/ducky_encode.cc
index 8919531..449c349 100644
--- a/av1/ducky_encode.cc
+++ b/av1/ducky_encode.cc
@@ -356,13 +356,15 @@
   const uint8_t block_mis_log2 = ppi->tpl_data.tpl_stats_block_mis_log2;
 
   for (size_t idx = 0; idx < gop_struct.gop_frame_list.size(); ++idx) {
-    TplFrameStats tpl_frame_stats;
+    TplFrameStats tpl_frame_stats = {};
     TplDepFrame *tpl_frame = &ppi->tpl_data.tpl_frame[idx];
 
     if (gop_struct.gop_frame_list[idx].update_type == GopFrameType::kOverlay ||
         gop_struct.gop_frame_list[idx].update_type ==
-            GopFrameType::kIntermediateOverlay)
+            GopFrameType::kIntermediateOverlay) {
+      tpl_gop_stats.frame_stats_list.push_back(tpl_frame_stats);
       continue;
+    }
 
     const int mi_rows = tpl_frame->mi_rows;
     const int mi_cols = tpl_frame->mi_cols;
diff --git a/av1/ratectrl_qmode.cc b/av1/ratectrl_qmode.cc
index 18b3d62..02824d3 100644
--- a/av1/ratectrl_qmode.cc
+++ b/av1/ratectrl_qmode.cc
@@ -1091,10 +1091,12 @@
 
 TplGopDepStats ComputeTplGopDepStats(
     const TplGopStats &tpl_gop_stats,
-    const std::vector<RefFrameTable> &ref_frame_table_list,
-    const int frame_count) {
+    const std::vector<RefFrameTable> &ref_frame_table_list) {
+  const int frame_count =
+      static_cast<int>(tpl_gop_stats.frame_stats_list.size());
   // Create the struct to store TPL dependency stats
   TplGopDepStats tpl_gop_dep_stats;
+
   for (int coding_idx = 0; coding_idx < frame_count; coding_idx++) {
     tpl_gop_dep_stats.frame_dep_stats_list.push_back(
         CreateTplFrameDepStatsWithoutPropagation(
@@ -1135,29 +1137,37 @@
 
   GopEncodeInfo gop_encode_info;
   gop_encode_info.final_snapshot = ref_frame_table_list.back();
-  TplGopDepStats gop_dep_stats = ComputeTplGopDepStats(
-      tpl_gop_stats, ref_frame_table_list, gop_struct.show_frame_count);
+  TplGopDepStats gop_dep_stats =
+      ComputeTplGopDepStats(tpl_gop_stats, ref_frame_table_list);
   const int frame_count =
       static_cast<int>(tpl_gop_stats.frame_stats_list.size());
   for (int i = 0; i < frame_count; i++) {
-    const TplFrameDepStats &frame_dep_stats =
-        gop_dep_stats.frame_dep_stats_list[i];
-    const double cost_without_propagation =
-        TplFrameDepStatsAccumulateIntraCost(frame_dep_stats);
-    const double cost_with_propagation =
-        TplFrameDepStatsAccumulate(frame_dep_stats);
-    const double frame_importance =
-        cost_with_propagation / cost_without_propagation;
-    // Imitate the behavior of av1_tpl_get_qstep_ratio()
-    const double qstep_ratio = sqrt(1 / frame_importance);
     FrameEncodeParameters param;
-    param.q_index = av1_get_q_index_from_qstep_ratio(rc_param_.base_q_index,
-                                                     qstep_ratio, AOM_BITS_8);
     const GopFrame &gop_frame = gop_struct.gop_frame_list[i];
-    if (gop_frame.is_key_frame) {
-      // TODO(jianj): QP for key frame could be 0 when base q index is set very
-      // low. Tune the calculation for frame_importance. Cap it at 1 for now.
-      param.q_index = AOMMAX(param.q_index, 1);
+
+    if (gop_frame.update_type == GopFrameType::kOverlay ||
+        gop_frame.update_type == GopFrameType::kIntermediateOverlay) {
+      param.q_index = rc_param_.base_q_index;
+    } else {
+      const TplFrameDepStats &frame_dep_stats =
+          gop_dep_stats.frame_dep_stats_list[i];
+      const double cost_without_propagation =
+          TplFrameDepStatsAccumulateIntraCost(frame_dep_stats);
+      const double cost_with_propagation =
+          TplFrameDepStatsAccumulate(frame_dep_stats);
+      const double frame_importance =
+          cost_with_propagation / cost_without_propagation;
+      // Imitate the behavior of av1_tpl_get_qstep_ratio()
+      const double qstep_ratio = sqrt(1 / frame_importance);
+      param.q_index = av1_get_q_index_from_qstep_ratio(rc_param_.base_q_index,
+                                                       qstep_ratio, AOM_BITS_8);
+
+      if (gop_frame.is_key_frame) {
+        // TODO(jianj): QP for key frame could be 0 when base q index is set
+        // very low. Tune the calculation for frame_importance. Cap it at 1 for
+        // now.
+        param.q_index = AOMMAX(param.q_index, 1);
+      }
     }
     param.rdmult = GetRDMult(gop_frame, param.q_index);
     gop_encode_info.param_list.push_back(param);
diff --git a/av1/ratectrl_qmode.h b/av1/ratectrl_qmode.h
index cceb217..4517d2d 100644
--- a/av1/ratectrl_qmode.h
+++ b/av1/ratectrl_qmode.h
@@ -81,8 +81,7 @@
 
 TplGopDepStats ComputeTplGopDepStats(
     const TplGopStats &tpl_gop_stats,
-    const std::vector<RefFrameTable> &ref_frame_table_list,
-    const int frame_count);
+    const std::vector<RefFrameTable> &ref_frame_table_list);
 
 class AV1RateControlQMode : public AV1RateControlQModeInterface {
  public:
diff --git a/test/ratectrl_qmode_test.cc b/test/ratectrl_qmode_test.cc
index 5b9aa99..d2eb25c 100644
--- a/test/ratectrl_qmode_test.cc
+++ b/test/ratectrl_qmode_test.cc
@@ -626,8 +626,8 @@
 
     ref_frame_table_list.push_back(CreateToyRefFrameTable(i));
   }
-  const TplGopDepStats &gop_dep_stats = ComputeTplGopDepStats(
-      tpl_gop_stats, ref_frame_table_list, gop_struct.show_frame_count);
+  const TplGopDepStats &gop_dep_stats =
+      ComputeTplGopDepStats(tpl_gop_stats, ref_frame_table_list);
 
   double expected_sum = 0;
   for (int i = 2; i >= 0; i--) {