Correct frame buf used in init_gop_frames_for_tpl

Add display_idx to lookahead_entry

When --limit=150 --cpu-used=3 --end-usage=q, the performance remains neutral
         AVG-PSNR SSIM
lowres   0.018%   0.083%
midres   0.007%  -0.008%
hdres    0.049%   0.069%
ugc360p -0.007%   0.040%

When --limit=17 --cpu-used=3 --end-usage=q, the compression efficiency improves.
         AVG-PSNR   SSIM
lowres   -0.737%   -2.247%
midres   -0.762%   -2.371%
hdres    -1.575%   -2.922%
ugc360p  -0.563%   -2.026%

Bug: webm:1715

STATS_CHANGED

Change-Id: I0e19ba22b04f3bf81d1e863d145a31a1cea97b68
diff --git a/av1/encoder/encode_strategy.c b/av1/encoder/encode_strategy.c
index 0190b28..dc51d55 100644
--- a/av1/encoder/encode_strategy.c
+++ b/av1/encoder/encode_strategy.c
@@ -368,7 +368,8 @@
 // temporal_filtered, flush, and frame_update_type are outputs.
 // Return the frame source, or NULL if we couldn't find one
 static struct lookahead_entry *choose_frame_source(
-    AV1_COMP *const cpi, int *const flush, struct lookahead_entry **last_source,
+    AV1_COMP *const cpi, int *const flush, int *pop_lookahead,
+    struct lookahead_entry **last_source,
     EncodeFrameParams *const frame_params) {
   AV1_COMMON *const cm = &cpi->common;
   const GF_GROUP *const gf_group = &cpi->gf_group;
@@ -390,21 +391,21 @@
   // buffer. If the current frame is not arf, then pop it. This assumes the
   // first frame in the GF group is not arf. May need to change if it is not
   // true.
-  int pop_lookahead = (src_index == 0);
+  *pop_lookahead = (src_index == 0);
   // If this is a key frame and keyframe filtering is enabled with overlay,
   // then do not pop.
-  if (pop_lookahead && cpi->oxcf.kf_cfg.enable_keyframe_filtering > 1 &&
+  if (*pop_lookahead && cpi->oxcf.kf_cfg.enable_keyframe_filtering > 1 &&
       gf_group->update_type[gf_group->index] == ARF_UPDATE &&
       !is_stat_generation_stage(cpi) && cpi->lookahead) {
     if (cpi->lookahead->read_ctxs[cpi->compressor_stage].sz &&
         (*flush ||
          cpi->lookahead->read_ctxs[cpi->compressor_stage].sz ==
              cpi->lookahead->read_ctxs[cpi->compressor_stage].pop_sz)) {
-      pop_lookahead = 0;
+      *pop_lookahead = 0;
     }
   }
-  frame_params->show_frame = pop_lookahead;
-  if (pop_lookahead) {
+  frame_params->show_frame = *pop_lookahead;
+  if (*pop_lookahead) {
     // show frame, pop from buffer
     // Get last frame source.
     if (cm->current_frame.frame_number > 0) {
@@ -412,7 +413,7 @@
           av1_lookahead_peek(cpi->lookahead, -1, cpi->compressor_stage);
     }
     // Read in the source frame.
-    source = av1_lookahead_pop(cpi->lookahead, *flush, cpi->compressor_stage);
+    source = av1_lookahead_peek(cpi->lookahead, 0, cpi->compressor_stage);
   } else {
     // no show frames are arf frames
     source =
@@ -875,7 +876,6 @@
 
   // Decide whether to apply temporal filtering to the source frame.
   int apply_filtering = 0;
-  int arf_src_index = -1;
   if (frame_params->frame_type == KEY_FRAME) {
     // Decide whether it is allowed to perform key frame filtering
     int allow_kf_filtering =
@@ -894,22 +894,11 @@
     // If we are doing kf filtering, set up a few things.
     if (apply_filtering) {
       av1_setup_past_independence(cm);
-      if (!frame_params->show_frame && cpi->no_show_fwd_kf) {
-        // fwd kf
-        arf_src_index = -1 * gf_group->arf_src_offset[gf_group->index];
-      } else if (!frame_params->show_frame) {
-        arf_src_index = 0;
-      } else {
-        arf_src_index = -1;
-      }
     }
   } else if (get_frame_update_type(&cpi->gf_group) == ARF_UPDATE ||
              get_frame_update_type(&cpi->gf_group) == INTNL_ARF_UPDATE) {
     // ARF
     apply_filtering = oxcf->algo_cfg.arnr_max_frames > 0;
-    if (apply_filtering) {
-      arf_src_index = gf_group->arf_src_offset[gf_group->index];
-    }
   }
 
 #if CONFIG_COLLECT_COMPONENT_TIMING
@@ -922,6 +911,11 @@
     int show_existing_alt_ref = 0;
     // TODO(bohanli): figure out why we need frame_type in cm here.
     cm->current_frame.frame_type = frame_params->frame_type;
+    int arf_src_index = gf_group->arf_src_offset[gf_group->index];
+    if (!frame_params->show_frame && cpi->no_show_fwd_kf) {
+      // fwd kf
+      arf_src_index = -1 * gf_group->arf_src_offset[gf_group->index];
+    }
     const int code_arf =
         av1_temporal_filter(cpi, arf_src_index, &show_existing_alt_ref);
     if (code_arf) {
@@ -1171,11 +1165,14 @@
 
   struct lookahead_entry *source = NULL;
   struct lookahead_entry *last_source = NULL;
+  int pop_lookahead = 0;
   if (frame_params.show_existing_frame) {
-    source = av1_lookahead_pop(cpi->lookahead, flush, cpi->compressor_stage);
+    source = av1_lookahead_peek(cpi->lookahead, 0, cpi->compressor_stage);
+    pop_lookahead = 1;
     frame_params.show_frame = 1;
   } else {
-    source = choose_frame_source(cpi, &flush, &last_source, &frame_params);
+    source = choose_frame_source(cpi, &flush, &pop_lookahead, &last_source,
+                                 &frame_params);
   }
 
   if (source == NULL) {  // If no source was found, we can't encode a frame.
@@ -1397,6 +1394,9 @@
     av1_update_vmaf_curve(cpi);
   }
 #endif
+  if (pop_lookahead == 1) {
+    av1_lookahead_pop(cpi->lookahead, flush, cpi->compressor_stage);
+  }
 
   if (!is_stat_generation_stage(cpi)) {
     update_fb_of_context_type(cpi, &frame_params, cpi->fb_of_context_type);
diff --git a/av1/encoder/encoder.c b/av1/encoder/encoder.c
index 4e7de26..5edc072 100644
--- a/av1/encoder/encoder.c
+++ b/av1/encoder/encoder.c
@@ -815,6 +815,17 @@
   frame_info->subsampling_y = seq_params->subsampling_y;
 }
 
+static INLINE void init_frame_index_set(FRAME_INDEX_SET *frame_index_set) {
+  frame_index_set->show_frame_count = 0;
+}
+
+static INLINE void update_frame_index_set(FRAME_INDEX_SET *frame_index_set,
+                                          int is_show_frame) {
+  if (is_show_frame) {
+    frame_index_set->show_frame_count++;
+  }
+}
+
 AV1_COMP *av1_create_compressor(AV1EncoderConfig *oxcf, BufferPool *const pool,
                                 FIRSTPASS_STATS *frame_stats_buf,
                                 COMPRESSOR_STAGE stage, int num_lap_buffers,
@@ -885,6 +896,7 @@
     }
   }
   init_frame_info(&cpi->frame_info, cm);
+  init_frame_index_set(&cpi->frame_index_set);
 
   cm->current_frame.frame_number = 0;
   cm->current_frame_id = -1;
@@ -2963,6 +2975,7 @@
     }
 
     ++current_frame->frame_number;
+    update_frame_index_set(&cpi->frame_index_set, cm->show_frame);
     return AOM_CODEC_OK;
   }
 
@@ -3178,7 +3191,10 @@
   // A droppable frame might not be shown but it always
   // takes a space in the gf group. Therefore, even when
   // it is not shown, we still need update the count down.
-  if (cm->show_frame) ++current_frame->frame_number;
+  if (cm->show_frame) {
+    update_frame_index_set(&cpi->frame_index_set, cm->show_frame);
+    ++current_frame->frame_number;
+  }
 
 #if CONFIG_COLLECT_COMPONENT_TIMING
   end_timing(cpi, encode_frame_to_data_rate_time);
@@ -3214,8 +3230,9 @@
   memcpy(&cpi->refresh_frame, &frame_params->refresh_frame,
          sizeof(cpi->refresh_frame));
 
-  if (current_frame->frame_type == KEY_FRAME && !cpi->no_show_fwd_kf)
+  if (current_frame->frame_type == KEY_FRAME && !cpi->no_show_fwd_kf) {
     current_frame->frame_number = 0;
+  }
 
   current_frame->order_hint =
       current_frame->frame_number + frame_params->order_offset;
diff --git a/av1/encoder/encoder.h b/av1/encoder/encoder.h
index f7d9d6b..c8e934a 100644
--- a/av1/encoder/encoder.h
+++ b/av1/encoder/encoder.h
@@ -1983,6 +1983,13 @@
   int subsampling_y;
 } FRAME_INFO;
 
+/*!
+ * \brief This structure stores different types of frame indices.
+ */
+typedef struct {
+  int show_frame_count;
+} FRAME_INDEX_SET;
+
 /*!\endcond */
 
 /*!
@@ -2392,6 +2399,11 @@
   FRAME_INFO frame_info;
 
   /*!
+   * Stores different types of frame indices.
+   */
+  FRAME_INDEX_SET frame_index_set;
+
+  /*!
    * Structure to store the dimensions of current frame.
    */
   InitialDimensions initial_dimensions;
diff --git a/av1/encoder/lookahead.c b/av1/encoder/lookahead.c
index f2bcb27..78e2e08 100644
--- a/av1/encoder/lookahead.c
+++ b/av1/encoder/lookahead.c
@@ -61,6 +61,7 @@
   if (ctx) {
     unsigned int i;
     ctx->max_sz = depth;
+    ctx->push_frame_count = 0;
     ctx->read_ctxs[ENCODE_STAGE].pop_sz = ctx->max_sz - MAX_PRE_FRAMES;
     ctx->read_ctxs[ENCODE_STAGE].valid = 1;
     if (num_lap_buffers) {
@@ -136,7 +137,9 @@
 
   buf->ts_start = ts_start;
   buf->ts_end = ts_end;
+  buf->display_idx = ctx->push_frame_count;
   buf->flags = flags;
+  ++ctx->push_frame_count;
   aom_remove_metadata_from_frame_buffer(&buf->img);
   aom_copy_metadata_to_frame_buffer(&buf->img, src->metadata);
   return 0;
diff --git a/av1/encoder/lookahead.h b/av1/encoder/lookahead.h
index 8861fd1..635857f 100644
--- a/av1/encoder/lookahead.h
+++ b/av1/encoder/lookahead.h
@@ -32,6 +32,7 @@
   YV12_BUFFER_CONFIG img;
   int64_t ts_start;
   int64_t ts_end;
+  int display_idx;
   aom_enc_frame_flags_t flags;
 };
 
@@ -52,6 +53,7 @@
   int write_idx;                         /* Write index */
   struct read_ctx read_ctxs[MAX_STAGES]; /* Read context */
   struct lookahead_entry *buf;           /* Buffer list */
+  int push_frame_count; /* Number of frames has been pushed in the queue*/
 };
 /*!\endcond */
 
diff --git a/av1/encoder/temporal_filter.c b/av1/encoder/temporal_filter.c
index 73d9cb3..3a0c64a 100644
--- a/av1/encoder/temporal_filter.c
+++ b/av1/encoder/temporal_filter.c
@@ -1000,7 +1000,7 @@
   } else if (filter_frame_lookahead_idx < 0 && q <= 10) {
     adjust_num = 0;
   }
-  num_frames = AOMMIN(num_frames + adjust_num, lookahead_depth + 1);
+  num_frames = AOMMIN(num_frames + adjust_num, lookahead_depth);
 
   if (filter_frame_lookahead_idx == -1 ||
       filter_frame_lookahead_idx == 0) {  // Key frame.
diff --git a/av1/encoder/tpl_model.c b/av1/encoder/tpl_model.c
index c0d54e1..f2fbda2 100644
--- a/av1/encoder/tpl_model.c
+++ b/av1/encoder/tpl_model.c
@@ -1253,14 +1253,14 @@
     if (frame_update_type == LF_UPDATE)
       *pframe_qindex = gf_group->q_val[gf_index];
 
+    struct lookahead_entry *buf;
     if (gf_index == cur_frame_idx) {
-      struct lookahead_entry *buf = av1_lookahead_peek(
-          cpi->lookahead, lookahead_index, cpi->compressor_stage);
+      buf = av1_lookahead_peek(cpi->lookahead, lookahead_index,
+                               cpi->compressor_stage);
       tpl_frame->gf_picture = gop_eval ? &buf->img : frame_input->source;
     } else {
-      struct lookahead_entry *buf = av1_lookahead_peek(
-          cpi->lookahead, lookahead_index, cpi->compressor_stage);
-
+      buf = av1_lookahead_peek(cpi->lookahead, lookahead_index,
+                               cpi->compressor_stage);
       if (buf == NULL) break;
       tpl_frame->gf_picture = &buf->img;
     }
@@ -1278,6 +1278,8 @@
     // is the display index of the frame.
     tpl_frame->frame_display_index =
         frame_display_index + cm->current_frame.frame_number - anc_frame_offset;
+    assert(buf->display_idx == cpi->frame_index_set.show_frame_count -
+                                   anc_frame_offset + frame_display_index);
 
     if (frame_update_type != OVERLAY_UPDATE &&
         frame_update_type != INTNL_OVERLAY_UPDATE) {