Apply temporal filter to fwd kf

This requires filtering backwards rather than forwards,
since the forward keyframe is the last frame in the lookahead
buffer.

Change-Id: I5fe15b695a5e5e4755030e8a6752a03b56312f27
diff --git a/av1/encoder/encode_strategy.c b/av1/encoder/encode_strategy.c
index e9bc75a..0d7973e 100644
--- a/av1/encoder/encode_strategy.c
+++ b/av1/encoder/encode_strategy.c
@@ -933,7 +933,7 @@
   }
   const int apply_filtering =
       oxcf->pass == 2 && frame_params->frame_type == KEY_FRAME &&
-      frame_params->show_frame &&
+      !frame_params->show_existing_frame &&
       cpi->rc.frames_to_key > NUM_KEY_FRAME_DENOISING && noise_level > 0 &&
       !is_lossless_requested(oxcf) && oxcf->arnr_max_frames > 0;
   // Save the pointer to the original source image.
@@ -958,7 +958,12 @@
     av1_frame_init_quantizer(cpi);
     av1_setup_past_independence(cm);
 
-    av1_temporal_filter(cpi, -1, NULL);
+    if (!frame_params->show_frame) {
+      int arf_src_index = get_arf_src_index(&cpi->gf_group, cpi->oxcf.pass);
+      av1_temporal_filter(cpi, -1 * arf_src_index, NULL);
+    } else {
+      av1_temporal_filter(cpi, -1, NULL);
+    }
     aom_extend_frame_borders(&cpi->alt_ref_buffer, num_planes);
     // Use the filtered frame for encoding.
     frame_input->source = &cpi->alt_ref_buffer;
diff --git a/av1/encoder/temporal_filter.c b/av1/encoder/temporal_filter.c
index 5f2af82..f8a318c1 100644
--- a/av1/encoder/temporal_filter.c
+++ b/av1/encoder/temporal_filter.c
@@ -1572,14 +1572,24 @@
     return 0;
   }
 
-  if (distance == -1) {
-    // Apply temporal filtering on key frame.
-    strength = estimate_strength(cpi, distance, rc->gfu_boost, &sigma);
-    // Number of frames for temporal filtering, could be tuned.
+  if (distance < 0) {
     frames_to_blur = NUM_KEY_FRAME_DENOISING;
-    frames_to_blur_backward = 0;
-    frames_to_blur_forward = frames_to_blur - 1;
-    start_frame = distance + frames_to_blur_forward;
+    if (distance == -1) {
+      // Apply temporal filtering on key frame.
+      strength = estimate_strength(cpi, distance, rc->gfu_boost, &sigma);
+      // Number of frames for temporal filtering, could be tuned.
+      frames_to_blur_backward = 0;
+      frames_to_blur_forward = frames_to_blur - 1;
+      start_frame = distance + frames_to_blur_forward;
+    } else {
+      // Apply temporal filtering on forward key frame. This requires filtering
+      // backwards rather than forwards.
+      strength = estimate_strength(cpi, -1 * distance, rc->gfu_boost, &sigma);
+      // Number of frames for temporal filtering, could be tuned.
+      frames_to_blur_backward = frames_to_blur - 1;
+      frames_to_blur_forward = 0;
+      start_frame = -1 * distance;
+    }
   } else {
     adjust_arnr_filter(cpi, distance, rc->gfu_boost, &frames_to_blur, &strength,
                        &sigma, &frames_to_blur_backward,
@@ -1613,9 +1623,9 @@
 
   FRAME_DIFF diff = temporal_filter_iterate_c(cpi, frames, frames_to_blur,
                                               frames_to_blur_backward, strength,
-                                              sigma, distance == -1, &sf);
+                                              sigma, distance < 0, &sf);
 
-  if (distance == -1) return 1;
+  if (distance < 0) return 1;
 
   if (show_existing_alt_ref != NULL && cpi->sf.adaptive_overlay_encoding) {
     AV1_COMMON *const cm = &cpi->common;