Save arf index in gf_group.

This removes hard-coded reference to the arf frame in gf group and
makes it easier to check whether arf is enabled in a gf group.

In the future if arf is not the second frame in a gf group, this also
avoids potential problems.

Change-Id: Ic723a651559191ba567078ad9ba0f22667e005a7
diff --git a/av1/encoder/encode_strategy.c b/av1/encoder/encode_strategy.c
index c9acf72..0e9414d 100644
--- a/av1/encoder/encode_strategy.c
+++ b/av1/encoder/encode_strategy.c
@@ -1076,8 +1076,10 @@
 
   if (!is_stat_generation_stage(cpi)) {
     // If this is a forward keyframe, mark as a show_existing_frame
+    // TODO(bohanli): find a consistent condition for fwd keyframes
     if (oxcf->kf_cfg.fwd_kf_enabled && (gf_group->index == gf_group->size) &&
-        gf_group->update_type[1] == ARF_UPDATE && cpi->rc.frames_to_key == 0) {
+        gf_group->update_type[gf_group->index] == OVERLAY_UPDATE &&
+        gf_group->arf_index >= 0 && cpi->rc.frames_to_key == 0) {
       frame_params.show_existing_frame = 1;
     } else {
       frame_params.show_existing_frame =
@@ -1274,8 +1276,19 @@
   if (!frame_params.show_existing_frame) {
     cm->quant_params.using_qmatrix = oxcf->q_cfg.using_qm;
 #if !CONFIG_REALTIME_ONLY
+    // Perform TPL when finished with arf filtering if arf is used.
+    // Otherwise perform it at the start of the gf group.
     if (gf_cfg->lag_in_frames > 0 && !is_stat_generation_stage(cpi)) {
-      if (cpi->gf_group.index == 1 && oxcf->algo_cfg.enable_tpl_model) {
+      // Try performing tpl after arf is initialized.
+      int which_frame_tpl = cpi->gf_group.arf_index;
+      // TODO(any): If no arf in the GF group, we can do tpl at the first
+      // frame.
+      // TODO(bohanli): Unify it with tpl for kf.
+      if (which_frame_tpl < 0) {
+        which_frame_tpl = 1;
+      }
+      if (cpi->gf_group.index == which_frame_tpl &&
+          oxcf->algo_cfg.enable_tpl_model) {
         av1_configure_buffer_updates(cpi, &frame_params.refresh_frame,
                                      frame_update_type, 0);
         av1_set_frame_size(cpi, cm->width, cm->height);
diff --git a/av1/encoder/encoder.c b/av1/encoder/encoder.c
index 867c1c7..649071e 100644
--- a/av1/encoder/encoder.c
+++ b/av1/encoder/encoder.c
@@ -2828,6 +2828,7 @@
   if (has_no_stats_stage(cpi) && oxcf->rc_cfg.mode == AOM_CBR &&
       current_frame->frame_type != KEY_FRAME) {
     if (av1_rc_drop_frame(cpi)) {
+      av1_setup_frame_size(cpi);
       av1_rc_postencode_update_drop_frame(cpi);
       release_scaled_references(cpi);
       return AOM_CODEC_OK;
diff --git a/av1/encoder/firstpass.h b/av1/encoder/firstpass.h
index 11a9f90..e0e2876 100644
--- a/av1/encoder/firstpass.h
+++ b/av1/encoder/firstpass.h
@@ -191,6 +191,7 @@
   // This is currently only populated for AOM_Q mode
   unsigned char q_val[MAX_STATIC_GF_GROUP_LENGTH];
   int bit_allocation[MAX_STATIC_GF_GROUP_LENGTH];
+  int arf_index;  // the index in the gf group of ARF, if no arf, then -1
   int size;
   /*!\endcond */
 } GF_GROUP;
diff --git a/av1/encoder/gop_structure.c b/av1/encoder/gop_structure.c
index 9ed418c..511bc86 100644
--- a/av1/encoder/gop_structure.c
+++ b/av1/encoder/gop_structure.c
@@ -120,7 +120,10 @@
     gf_group->layer_depth[frame_index] = 1;
     gf_group->arf_boost[frame_index] = cpi->rc.gfu_boost;
     gf_group->max_layer_depth = 1;
+    gf_group->arf_index = frame_index;
     ++frame_index;
+  } else {
+    gf_group->arf_index = -1;
   }
 
   // Rest of the frames.
diff --git a/av1/encoder/tpl_model.c b/av1/encoder/tpl_model.c
index 72c6bec..628ad60 100644
--- a/av1/encoder/tpl_model.c
+++ b/av1/encoder/tpl_model.c
@@ -903,7 +903,7 @@
   *tpl_group_frames = cur_frame_idx;
 
   int gf_index;
-  int use_arf = gf_group->update_type[1] == ARF_UPDATE;
+  int use_arf = gf_group->arf_index >= 0;
   int anc_frame_offset = gf_group->cur_frame_idx[cur_frame_idx];
   int process_frame_count = 0;
   const int gop_length =