Add display frame index tracker in gf_group

Control the lookahead offset according to the number of frames
displayed. The coding performance results are mixed:

         avg PSNR     overall PSNR    SSIM
lowres   0.030% 	0.092%        0.025%
midres  -0.203%	       -0.064%	     -0.211%
ugc360   0.148%	        0.215%	      0.153%
hdres   -0.143%	       -0.199%	     -0.198%

STATS_CHANGED

Change-Id: I6b5e15b6961876e62c4e3ec504982cc6e13991c5
diff --git a/av1/encoder/firstpass.h b/av1/encoder/firstpass.h
index cf48007..266e5fe 100644
--- a/av1/encoder/firstpass.h
+++ b/av1/encoder/firstpass.h
@@ -112,6 +112,9 @@
   unsigned char index;
   FRAME_UPDATE_TYPE update_type[MAX_STATIC_GF_GROUP_LENGTH];
   unsigned char arf_src_offset[MAX_STATIC_GF_GROUP_LENGTH];
+  // The number of frames displayed so far within the GOP at a given coding
+  // frame.
+  unsigned char cur_frame_idx[MAX_STATIC_GF_GROUP_LENGTH];
   unsigned char frame_disp_idx[MAX_STATIC_GF_GROUP_LENGTH];
   int ref_frame_disp_idx[MAX_STATIC_GF_GROUP_LENGTH][REF_FRAMES];
   int ref_frame_gop_idx[MAX_STATIC_GF_GROUP_LENGTH][REF_FRAMES];
diff --git a/av1/encoder/gop_structure.c b/av1/encoder/gop_structure.c
index 4f79e32..6889c83 100644
--- a/av1/encoder/gop_structure.c
+++ b/av1/encoder/gop_structure.c
@@ -29,8 +29,8 @@
 static void set_multi_layer_params(const TWO_PASS *twopass,
                                    GF_GROUP *const gf_group, RATE_CONTROL *rc,
                                    FRAME_INFO *frame_info, int start, int end,
-                                   int *frame_ind, int arf_ind,
-                                   int layer_depth) {
+                                   int *cur_frame_idx, int *frame_ind,
+                                   int arf_ind, int layer_depth) {
   const int num_frames_to_process = end - start - 1;
   assert(num_frames_to_process >= 0);
   if (num_frames_to_process == 0) return;
@@ -43,6 +43,8 @@
     while (++start < end) {
       gf_group->update_type[*frame_ind] = LF_UPDATE;
       gf_group->arf_src_offset[*frame_ind] = 0;
+      ++*cur_frame_idx;
+      gf_group->cur_frame_idx[*frame_ind] = *cur_frame_idx;
       gf_group->frame_disp_idx[*frame_ind] = start;
       gf_group->layer_depth[*frame_ind] = MAX_ARF_LAYERS;
       gf_group->arf_boost[*frame_ind] =
@@ -57,6 +59,7 @@
     // Internal ARF.
     gf_group->update_type[*frame_ind] = INTNL_ARF_UPDATE;
     gf_group->arf_src_offset[*frame_ind] = m - start - 1;
+    gf_group->cur_frame_idx[*frame_ind] = *cur_frame_idx;
     gf_group->frame_disp_idx[*frame_ind] = m;
     gf_group->layer_depth[*frame_ind] = layer_depth;
 
@@ -67,19 +70,20 @@
 
     // Frames displayed before this internal ARF.
     set_multi_layer_params(twopass, gf_group, rc, frame_info, start, m,
-                           frame_ind, 1, layer_depth + 1);
+                           cur_frame_idx, frame_ind, 1, layer_depth + 1);
 
     // Overlay for internal ARF.
     gf_group->update_type[*frame_ind] = INTNL_OVERLAY_UPDATE;
     gf_group->arf_src_offset[*frame_ind] = 0;
+    gf_group->cur_frame_idx[*frame_ind] = *cur_frame_idx;
     gf_group->frame_disp_idx[*frame_ind] = m;
     gf_group->arf_boost[*frame_ind] = 0;
     gf_group->layer_depth[*frame_ind] = layer_depth;
     ++(*frame_ind);
 
     // Frames displayed after this internal ARF.
-    set_multi_layer_params(twopass, gf_group, rc, frame_info, m, end, frame_ind,
-                           arf_ind, layer_depth + 1);
+    set_multi_layer_params(twopass, gf_group, rc, frame_info, m, end,
+                           cur_frame_idx, frame_ind, arf_ind, layer_depth + 1);
   }
 }
 
@@ -97,6 +101,7 @@
 
   gf_group->update_type[frame_index] = first_frame_update_type;
   gf_group->arf_src_offset[frame_index] = 0;
+  gf_group->cur_frame_idx[frame_index] = 0;
   gf_group->layer_depth[frame_index] =
       first_frame_update_type == OVERLAY_UPDATE ? MAX_ARF_LAYERS + 1 : 0;
   gf_group->max_layer_depth = 0;
@@ -107,6 +112,7 @@
   if (use_altref) {
     gf_group->update_type[frame_index] = ARF_UPDATE;
     gf_group->arf_src_offset[frame_index] = gf_interval - 1;
+    gf_group->cur_frame_idx[frame_index] = 0;
     gf_group->frame_disp_idx[frame_index] = gf_interval;
     gf_group->layer_depth[frame_index] = 1;
     gf_group->arf_boost[frame_index] = cpi->rc.gfu_boost;
@@ -114,9 +120,10 @@
     ++frame_index;
   }
 
+  int cur_frame_index = 0;
   // Rest of the frames.
   set_multi_layer_params(twopass, gf_group, rc, frame_info, 0, gf_interval,
-                         &frame_index, 0, use_altref + 1);
+                         &cur_frame_index, &frame_index, 0, 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.
diff --git a/av1/encoder/tpl_model.c b/av1/encoder/tpl_model.c
index 0b95f29..4aa7383 100644
--- a/av1/encoder/tpl_model.c
+++ b/av1/encoder/tpl_model.c
@@ -837,7 +837,7 @@
 
   int gf_index;
   int use_arf = gf_group->update_type[1] == ARF_UPDATE;
-  int anc_frame_offset = !gop_eval;
+  int anc_frame_offset = gf_group->cur_frame_idx[cur_frame_idx] + 1;
   const int gop_length =
       AOMMIN(gf_group->size - 1 + use_arf, MAX_LENGTH_TPL_FRAME_STATS - 1);
   for (gf_index = cur_frame_idx; gf_index <= gop_length; ++gf_index) {