Support forward key frame

Support the use of forward key frame in the flexible frame
scheduling system.

BUG=aomedia:3079

Change-Id: I8d6bf78b99ef2b9c787b3552ee2c1085665b5433
diff --git a/av1/encoder/encoder.c b/av1/encoder/encoder.c
index a21a10b..6bafa34 100644
--- a/av1/encoder/encoder.c
+++ b/av1/encoder/encoder.c
@@ -4138,6 +4138,7 @@
   if (cpi->common.show_frame && cpi->rc.frames_to_key) {
     cpi->rc.frames_since_key++;
     cpi->rc.frames_to_key--;
+    cpi->rc.frames_to_fwd_kf--;
   }
 }
 
diff --git a/av1/encoder/gop_structure.c b/av1/encoder/gop_structure.c
index 8b12570..577f5cd 100644
--- a/av1/encoder/gop_structure.c
+++ b/av1/encoder/gop_structure.c
@@ -607,7 +607,8 @@
 
   // ALTREF.
   const int use_altref = gf_group->max_layer_depth_allowed > 0;
-  int is_fwd_kf = (gf_interval == cpi->rc.frames_to_key);
+  int is_fwd_kf = rc->frames_to_fwd_kf == gf_interval;
+
   if (use_altref) {
     gf_group->update_type[frame_index] = ARF_UPDATE;
     gf_group->arf_src_offset[frame_index] = gf_interval - cur_frame_index;
diff --git a/av1/encoder/pass2_strategy.c b/av1/encoder/pass2_strategy.c
index 5010a8c..fccd948 100644
--- a/av1/encoder/pass2_strategy.c
+++ b/av1/encoder/pass2_strategy.c
@@ -3224,6 +3224,8 @@
   else
     rc->frames_to_key = kf_cfg->key_freq_max;
 
+  rc->frames_to_fwd_kf = kf_cfg->fwd_kf_dist;
+
   if (cpi->ppi->lap_enabled) correct_frames_to_key(cpi);
 
   // If there is a max kf interval set by the user we must obey it.
@@ -3722,6 +3724,7 @@
   // Keyframe and section processing.
   FIRSTPASS_STATS this_frame_copy;
   this_frame_copy = this_frame;
+
   int is_overlay_forward_kf =
       rc->frames_to_key == 0 &&
       gf_group->update_type[cpi->gf_frame_index] == OVERLAY_UPDATE;
@@ -3773,6 +3776,9 @@
     }
   }
 
+  if (rc->frames_to_fwd_kf <= 0)
+    rc->frames_to_fwd_kf = oxcf->kf_cfg.fwd_kf_dist;
+
   // Define a new GF/ARF group. (Should always enter here for key frames).
   if (cpi->gf_frame_index == gf_group->size) {
     const FIRSTPASS_STATS *const start_position = twopass->stats_in;
diff --git a/av1/encoder/ratectrl.h b/av1/encoder/ratectrl.h
index a6273df..c88d5c0 100644
--- a/av1/encoder/ratectrl.h
+++ b/av1/encoder/ratectrl.h
@@ -186,6 +186,7 @@
   int frames_to_key;
   /*!\cond */
   int frames_since_key;
+  int frames_to_fwd_kf;
   int is_src_frame_alt_ref;
   int sframe_due;