Make show_existing_frame control only depend on update_type

Refactor the GOP structure control logic. Make the show existing
frame flag purely depend on the frame update type provided by
the gf_group.

Change-Id: I1b4bf636c5d414a7db0688922ae53f9473033cb2
diff --git a/av1/encoder/encode_strategy.c b/av1/encoder/encode_strategy.c
index 006bc36..2f2b3c7 100644
--- a/av1/encoder/encode_strategy.c
+++ b/av1/encoder/encode_strategy.c
@@ -199,29 +199,6 @@
   return gf_group->update_type[gf_group->index];
 }
 
-// Note: the parameters related to OVERLAY_UPDATE will be taken care of in
-// av1_get_second_pass_params().
-static void check_show_existing_frame(AV1_COMP *const cpi,
-                                      EncodeFrameParams *const frame_params) {
-  const GF_GROUP *const gf_group = &cpi->gf_group;
-  assert(gf_group->index <= gf_group->size);
-  (void)gf_group;
-  AV1_COMMON *const cm = &cpi->common;
-  if (cm->show_existing_frame == 1) {
-    frame_params->show_existing_frame = 0;
-  } else {
-    const FRAME_UPDATE_TYPE frame_update_type = get_frame_update_type(cpi);
-    if (frame_update_type == INTNL_OVERLAY_UPDATE) {
-      frame_params->show_existing_frame = 1;
-      frame_params->existing_fb_idx_to_show =
-          get_ref_frame_map_idx(cm, BWDREF_FRAME);
-
-    } else {
-      frame_params->show_existing_frame = 0;
-    }
-  }
-}
-
 static void set_ext_overrides(AV1_COMP *const cpi,
                               EncodeFrameParams *const frame_params) {
   // Overrides the defaults with the externally supplied values with
@@ -1158,6 +1135,7 @@
                         int flush) {
   const AV1EncoderConfig *const oxcf = &cpi->oxcf;
   AV1_COMMON *const cm = &cpi->common;
+  GF_GROUP *gf_group = &cpi->gf_group;
 
   EncodeFrameInput frame_input;
   EncodeFrameParams frame_params;
@@ -1171,7 +1149,8 @@
     cpi->oxcf.gf_max_pyr_height = USE_ALTREF_FOR_ONE_PASS;
 
   if (oxcf->pass == 0 || oxcf->pass == 2) {
-    check_show_existing_frame(cpi, &frame_params);
+    frame_params.show_existing_frame =
+        gf_group->update_type[gf_group->index] == INTNL_OVERLAY_UPDATE;
     frame_params.show_existing_frame &= allow_show_existing(cpi, *frame_flags);
   } else {
     frame_params.show_existing_frame = 0;
@@ -1322,6 +1301,11 @@
 
     frame_params.refresh_frame_flags = av1_get_refresh_frame_flags(
         cpi, &frame_params, frame_update_type, &cpi->ref_buffer_stack);
+
+    frame_params.existing_fb_idx_to_show =
+        frame_params.show_existing_frame
+            ? get_ref_frame_map_idx(cm, BWDREF_FRAME)
+            : INVALID_IDX;
   }
 
   // The way frame_params->remapped_ref_idx is setup is a placeholder.
diff --git a/av1/encoder/gop_structure.c b/av1/encoder/gop_structure.c
index df0001b..c694a14 100644
--- a/av1/encoder/gop_structure.c
+++ b/av1/encoder/gop_structure.c
@@ -132,6 +132,11 @@
   assert(next_height >= MIN_PYRAMID_LVL);
   set_multi_layer_params(cpi, gf_group, 0, gf_interval, &frame_index, 0,
                          next_height, use_altref + 1);
+
+  // The end frame will be Overlay frame for an ARF GOP; otherwise set it to
+  // be GF, for consistency, which will be updated in the next GOP.
+  gf_group->update_type[frame_index] = use_altref ? OVERLAY_UPDATE : GF_UPDATE;
+  gf_group->arf_src_offset[frame_index] = 0;
   return frame_index;
 }
 
diff --git a/av1/encoder/tpl_model.c b/av1/encoder/tpl_model.c
index 2fe8800..9756328 100644
--- a/av1/encoder/tpl_model.c
+++ b/av1/encoder/tpl_model.c
@@ -635,11 +635,6 @@
   for (gf_index = cur_frame_idx; gf_index <= gop_length; ++gf_index) {
     TplDepFrame *tpl_frame = &cpi->tpl_frame[gf_index];
     FRAME_UPDATE_TYPE frame_update_type = gf_group->update_type[gf_index];
-    if (gf_index == gf_group->size) {
-      frame_update_type = INTNL_OVERLAY_UPDATE;
-      gf_group->update_type[gf_index] = frame_update_type;
-      gf_group->q_val[gf_index] = gf_group->q_val[1];
-    }
 
     frame_params.show_frame = frame_update_type != ARF_UPDATE &&
                               frame_update_type != INTNL_ARF_UPDATE;
@@ -768,14 +763,11 @@
     // Backward propagation from tpl_group_frames to 1.
     for (int frame_idx = cpi->tpl_gf_group_frames - 1;
          frame_idx >= gf_group->index; --frame_idx) {
-      int is_process = 1;
+      if (gf_group->update_type[frame_idx] == INTNL_OVERLAY_UPDATE ||
+          gf_group->update_type[frame_idx] == OVERLAY_UPDATE)
+        continue;
 
-      if (frame_idx == gf_group->size) is_process = 0;
-      if (frame_idx < gf_group->size)
-        if (gf_group->update_type[frame_idx] == INTNL_OVERLAY_UPDATE)
-          is_process = 0;
-
-      if (is_process) mc_flow_dispenser(cpi, frame_idx);
+      mc_flow_dispenser(cpi, frame_idx);
     }
   }