Refactor av1_temporal_filter()

Pass update_type and is_forward_keyframe to av1_temporal_filter.

When --limit=150 --cpu-used=3 --end-usage=q, the performance improves
slightly
             AVG-PSNR SSIM
    lowres  -0.100%  -0.020%
    midres  -0.076%  -0.028%
    hdres   -0.069%  -0.108%
    ugc360p -0.091%  -0.040%

Bug: webm:1715

STATS_CHANGED

Change-Id: I6aecf2c49648c1c48f7efc2d8184a174fd5e51b9
diff --git a/av1/encoder/encode_strategy.c b/av1/encoder/encode_strategy.c
index dc51d55..051c3b2 100644
--- a/av1/encoder/encode_strategy.c
+++ b/av1/encoder/encode_strategy.c
@@ -873,6 +873,7 @@
   const AV1EncoderConfig *const oxcf = &cpi->oxcf;
   AV1_COMMON *const cm = &cpi->common;
   const GF_GROUP *const gf_group = &cpi->gf_group;
+  FRAME_UPDATE_TYPE update_type = get_frame_update_type(&cpi->gf_group);
 
   // Decide whether to apply temporal filtering to the source frame.
   int apply_filtering = 0;
@@ -895,8 +896,7 @@
     if (apply_filtering) {
       av1_setup_past_independence(cm);
     }
-  } else if (get_frame_update_type(&cpi->gf_group) == ARF_UPDATE ||
-             get_frame_update_type(&cpi->gf_group) == INTNL_ARF_UPDATE) {
+  } else if (update_type == ARF_UPDATE || update_type == INTNL_ARF_UPDATE) {
     // ARF
     apply_filtering = oxcf->algo_cfg.arnr_max_frames > 0;
   }
@@ -912,12 +912,15 @@
     // 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];
+    int is_forward_keyframe = 0;
     if (!frame_params->show_frame && cpi->no_show_fwd_kf) {
+      // TODO(angiebird): Figure out why this condition yields forward keyframe.
       // fwd kf
-      arf_src_index = -1 * gf_group->arf_src_offset[gf_group->index];
+      is_forward_keyframe = 1;
     }
     const int code_arf =
-        av1_temporal_filter(cpi, arf_src_index, &show_existing_alt_ref);
+        av1_temporal_filter(cpi, arf_src_index, update_type,
+                            is_forward_keyframe, &show_existing_alt_ref);
     if (code_arf) {
       aom_extend_frame_borders(&cpi->alt_ref_buffer, av1_num_planes(cm));
       frame_input->source = &cpi->alt_ref_buffer;
@@ -925,8 +928,7 @@
                                         source_buffer->metadata);
     }
     // Currently INTNL_ARF_UPDATE only do show_existing.
-    if (get_frame_update_type(&cpi->gf_group) == ARF_UPDATE &&
-        !cpi->no_show_fwd_kf) {
+    if (update_type == ARF_UPDATE && !cpi->no_show_fwd_kf) {
       cpi->show_existing_alt_ref = show_existing_alt_ref;
     }
   }
diff --git a/av1/encoder/pass2_strategy.c b/av1/encoder/pass2_strategy.c
index 173b884..f31dc70 100644
--- a/av1/encoder/pass2_strategy.c
+++ b/av1/encoder/pass2_strategy.c
@@ -3622,7 +3622,11 @@
             (rc->frames_since_key > 0 && gf_group->arf_index > -1);
         if (is_temporal_filter_enabled) {
           int arf_src_index = gf_group->arf_src_offset[gf_group->arf_index];
-          av1_temporal_filter(cpi, arf_src_index, NULL);
+          FRAME_UPDATE_TYPE arf_update_type =
+              gf_group->update_type[gf_group->arf_index];
+          int is_forward_keyframe = 0;
+          av1_temporal_filter(cpi, arf_src_index, arf_update_type,
+                              is_forward_keyframe, NULL);
           aom_extend_frame_borders(&cpi->alt_ref_buffer,
                                    av1_num_planes(&cpi->common));
         }
diff --git a/av1/encoder/temporal_filter.c b/av1/encoder/temporal_filter.c
index 3a0c64a..88f28c6 100644
--- a/av1/encoder/temporal_filter.c
+++ b/av1/encoder/temporal_filter.c
@@ -917,13 +917,18 @@
  * \param[in]   is_second_arf   Whether the to-filter frame is the second ARF.
  *                              This field will affect the number of frames
  *                              used for filtering.
+ * \param[in]   update_type     This frame's update type.
+ *
+ * \param[in]   is_forward_keyframe Indicate whether this is a forward keyframe.
  *
  * \return Nothing will be returned. But the fields `frames`, `num_frames`,
  *         `filter_frame_idx` and `noise_levels` will be updated in cpi->tf_ctx.
  */
 static void tf_setup_filtering_buffer(AV1_COMP *cpi,
                                       const int filter_frame_lookahead_idx,
-                                      const int is_second_arf) {
+                                      const int is_second_arf,
+                                      FRAME_UPDATE_TYPE update_type,
+                                      int is_forward_keyframe) {
   TemporalFilterCtx *tf_ctx = &cpi->tf_ctx;
   YV12_BUFFER_CONFIG **frames = tf_ctx->frames;
   // Number of frames used for filtering. Set `arnr_max_frames` as 1 to disable
@@ -934,31 +939,23 @@
   const int lookahead_depth =
       av1_lookahead_depth(cpi->lookahead, cpi->compressor_stage);
 
+  int arf_src_offset = cpi->gf_group.arf_src_offset[cpi->gf_group.index];
+
   // Temporal filtering should not go beyond key frames
   const int key_to_curframe =
-      AOMMAX(cpi->rc.frames_since_key +
-                 cpi->gf_group.arf_src_offset[cpi->gf_group.index],
-             0);
+      AOMMAX(cpi->rc.frames_since_key + arf_src_offset, 0);
   const int curframe_to_key =
-      AOMMAX(cpi->rc.frames_to_key -
-                 cpi->gf_group.arf_src_offset[cpi->gf_group.index] - 1,
-             0);
+      AOMMAX(cpi->rc.frames_to_key - arf_src_offset - 1, 0);
 
   // Number of buffered frames before the to-filter frame.
-  const int max_before =
-      AOMMIN(filter_frame_lookahead_idx < -1 ? -filter_frame_lookahead_idx + 1
-                                             : filter_frame_lookahead_idx + 1,
-             key_to_curframe);
+  const int max_before = AOMMIN(filter_frame_lookahead_idx, key_to_curframe);
+
   // Number of buffered frames after the to-filter frame.
   const int max_after = AOMMIN(lookahead_depth - max_before, curframe_to_key);
 
-  const int filter_frame_offset = filter_frame_lookahead_idx < -1
-                                      ? -filter_frame_lookahead_idx
-                                      : filter_frame_lookahead_idx;
-
   // Estimate noises for each plane.
   const struct lookahead_entry *to_filter_buf = av1_lookahead_peek(
-      cpi->lookahead, filter_frame_offset, cpi->compressor_stage);
+      cpi->lookahead, filter_frame_lookahead_idx, cpi->compressor_stage);
   assert(to_filter_buf != NULL);
   const YV12_BUFFER_CONFIG *to_filter_frame = &to_filter_buf->img;
   const int num_planes = av1_num_planes(&cpi->common);
@@ -973,8 +970,8 @@
   RATE_CONTROL *rc = &cpi->rc;
   const double *coeff = rc->cor_coeff;
   const int offset = rc->regions_offset;
-  int cur_frame_idx = filter_frame_offset + rc->frames_since_key - offset;
-  if (rc->frames_since_key == 0) cur_frame_idx++;
+  int cur_frame_idx =
+      filter_frame_lookahead_idx + rc->frames_since_key - offset;
 
   double accu_coeff0 = 1.0, accu_coeff1 = 1.0;
   for (int i = 1; i <= max_after; i++) {
@@ -997,16 +994,15 @@
   int adjust_num = 6;
   if (num_frames == 1) {  // `arnr_max_frames = 1` is used to disable filtering.
     adjust_num = 0;
-  } else if (filter_frame_lookahead_idx < 0 && q <= 10) {
+  } else if ((update_type == KF_UPDATE || is_forward_keyframe) && q <= 10) {
     adjust_num = 0;
   }
   num_frames = AOMMIN(num_frames + adjust_num, lookahead_depth);
 
-  if (filter_frame_lookahead_idx == -1 ||
-      filter_frame_lookahead_idx == 0) {  // Key frame.
+  if (update_type == KF_UPDATE && !is_forward_keyframe) {
     num_before = 0;
     num_after = AOMMIN(num_frames - 1, max_after);
-  } else if (filter_frame_lookahead_idx < -1) {  // Key frame in one-pass mode.
+  } else if (is_forward_keyframe) {  // Key frame in one-pass mode.
     num_before = AOMMIN(num_frames - 1, max_before);
     num_after = 0;
   } else {
@@ -1042,7 +1038,7 @@
 
   // Setup the frame buffer.
   for (int frame = 0; frame < num_frames; ++frame) {
-    const int lookahead_idx = frame - num_before + filter_frame_offset;
+    const int lookahead_idx = frame - num_before + filter_frame_lookahead_idx;
     struct lookahead_entry *buf = av1_lookahead_peek(
         cpi->lookahead, lookahead_idx, cpi->compressor_stage);
     assert(buf != NULL);
@@ -1118,20 +1114,23 @@
 // Returns:
 //   Nothing will be returned. But the contents of cpi->tf_ctx will be modified.
 static void init_tf_ctx(AV1_COMP *cpi, int filter_frame_lookahead_idx,
-                        int is_second_arf) {
+                        int is_second_arf, FRAME_UPDATE_TYPE update_type,
+                        int is_forward_keyframe) {
   TemporalFilterCtx *tf_ctx = &cpi->tf_ctx;
   // Setup frame buffer for filtering.
   YV12_BUFFER_CONFIG **frames = tf_ctx->frames;
   tf_ctx->num_frames = 0;
   tf_ctx->filter_frame_idx = -1;
-  tf_setup_filtering_buffer(cpi, filter_frame_lookahead_idx, is_second_arf);
+  tf_setup_filtering_buffer(cpi, filter_frame_lookahead_idx, is_second_arf,
+                            update_type, is_forward_keyframe);
   assert(tf_ctx->num_frames > 0);
   assert(tf_ctx->filter_frame_idx < tf_ctx->num_frames);
 
   // Check show existing condition for non-keyframes. For KFs, only check when
   // KF overlay is enabled.
-  tf_ctx->check_show_existing = !(filter_frame_lookahead_idx <= 0) ||
-                                cpi->oxcf.kf_cfg.enable_keyframe_filtering > 1;
+  tf_ctx->check_show_existing =
+      !(is_forward_keyframe && update_type == KF_UPDATE) ||
+      cpi->oxcf.kf_cfg.enable_keyframe_filtering > 1;
 
   // Setup scaling factors. Scaling on each of the arnr frames is not
   // supported.
@@ -1169,19 +1168,19 @@
 }
 
 int av1_temporal_filter(AV1_COMP *cpi, const int filter_frame_lookahead_idx,
+                        FRAME_UPDATE_TYPE update_type, int is_forward_keyframe,
                         int *show_existing_arf) {
   MultiThreadInfo *const mt_info = &cpi->mt_info;
   // Basic informaton of the current frame.
   const GF_GROUP *const gf_group = &cpi->gf_group;
   const uint8_t group_idx = gf_group->index;
-  const FRAME_UPDATE_TYPE update_type = gf_group->update_type[group_idx];
   TemporalFilterCtx *tf_ctx = &cpi->tf_ctx;
   TemporalFilterData *tf_data = &cpi->td.tf_data;
   // Filter one more ARF if the lookahead index is leq 7 (w.r.t. 9-th frame).
   // This frame is ALWAYS a show existing frame.
-  const int is_second_arf = (update_type == INTNL_ARF_UPDATE) &&
-                            (filter_frame_lookahead_idx >= 7) &&
-                            cpi->sf.hl_sf.second_alt_ref_filtering;
+  const int is_second_arf =
+      (update_type == INTNL_ARF_UPDATE) && (filter_frame_lookahead_idx >= 7) &&
+      (is_forward_keyframe == 0) && cpi->sf.hl_sf.second_alt_ref_filtering;
   // TODO(anyone): Currently, we enforce the filtering strength on internal
   // ARFs except the second ARF to be zero. We should investigate in which case
   // it is more beneficial to use non-zero strength filtering.
@@ -1201,10 +1200,11 @@
                     cpi->common.features.allow_high_precision_mv, mv_costs);
 
   // Initialize temporal filter context structure.
-  init_tf_ctx(cpi, filter_frame_lookahead_idx, is_second_arf);
+  init_tf_ctx(cpi, filter_frame_lookahead_idx, is_second_arf, update_type,
+              is_forward_keyframe);
 
   // Set showable frame.
-  if (filter_frame_lookahead_idx >= 0) {
+  if (is_forward_keyframe == 0 && update_type != KF_UPDATE) {
     cpi->common.showable_frame = tf_ctx->num_frames == 1 || is_second_arf ||
                                  (cpi->oxcf.algo_cfg.enable_overlay == 0);
   }
diff --git a/av1/encoder/temporal_filter.h b/av1/encoder/temporal_filter.h
index 461e445..8ecc774 100644
--- a/av1/encoder/temporal_filter.h
+++ b/av1/encoder/temporal_filter.h
@@ -205,6 +205,9 @@
  * \param[in]   cpi                        Top level encoder instance structure
  * \param[in]   filter_frame_lookahead_idx The index of the to-filter frame in
  *                                         the lookahead buffer cpi->lookahead.
+ * \param[in]   update_type                This frame's update type.
+ * \param[in]   is_forward_keyframe        Indicate whether this is a forward
+ *                                         keyframe.
  * \param[in,out]   show_existing_arf      Whether to show existing ARF. This
  *                                         field is updated in this function.
  *
@@ -212,6 +215,7 @@
  */
 int av1_temporal_filter(struct AV1_COMP *cpi,
                         const int filter_frame_lookahead_idx,
+                        FRAME_UPDATE_TYPE update_type, int is_forward_keyframe,
                         int *show_existing_arf);
 
 /*!\cond */