Correct frames_to_key in single_pass for last GOP

Correct the value of frames_to_key when flush condition
is reached(towards the last GOP). Because flush has begun
we can be sure that no more input frames remain, and we can
use the depth of the lookahead buffer to determine frames_to_key.

Change-Id: I6afbdee1a795f0d286437e4abb08d84322f8ef1e
(cherry picked from commit 8f7c0acf538fbbcb90873c8b3f3d45630237f21b)
diff --git a/av1/encoder/lookahead.c b/av1/encoder/lookahead.c
index df05ade..ee90bc0 100644
--- a/av1/encoder/lookahead.c
+++ b/av1/encoder/lookahead.c
@@ -200,3 +200,12 @@
   assert(read_ctx->valid == 1);
   return read_ctx->sz;
 }
+
+int av1_lookahead_pop_sz(struct lookahead_ctx *ctx, COMPRESSOR_STAGE stage) {
+  struct read_ctx *read_ctx = NULL;
+  assert(ctx != NULL);
+
+  read_ctx = &ctx->read_ctxs[stage];
+  assert(read_ctx->valid == 1);
+  return read_ctx->pop_sz;
+}
diff --git a/av1/encoder/lookahead.h b/av1/encoder/lookahead.h
index 44921c1a..03693d3 100644
--- a/av1/encoder/lookahead.h
+++ b/av1/encoder/lookahead.h
@@ -113,6 +113,8 @@
 unsigned int av1_lookahead_depth(struct lookahead_ctx *ctx,
                                  COMPRESSOR_STAGE stage);
 
+int av1_lookahead_pop_sz(struct lookahead_ctx *ctx, COMPRESSOR_STAGE stage);
+
 #ifdef __cplusplus
 }  // extern "C"
 #endif
diff --git a/av1/encoder/pass2_strategy.c b/av1/encoder/pass2_strategy.c
index 149de63..7bcd125 100644
--- a/av1/encoder/pass2_strategy.c
+++ b/av1/encoder/pass2_strategy.c
@@ -1233,6 +1233,15 @@
 #endif
 }
 
+static void correct_frames_to_key(AV1_COMP *cpi) {
+  int lookahead_size =
+      (int)av1_lookahead_depth(cpi->lookahead, cpi->compressor_stage) + 1;
+  if (lookahead_size <
+      av1_lookahead_pop_sz(cpi->lookahead, cpi->compressor_stage)) {
+    cpi->rc.frames_to_key = AOMMIN(cpi->rc.frames_to_key, lookahead_size);
+  }
+}
+
 static void define_gf_group_pass0(AV1_COMP *cpi,
                                   const EncodeFrameParams *const frame_params) {
   RATE_CONTROL *const rc = &cpi->rc;
@@ -1247,6 +1256,9 @@
     rc->cur_gf_index++;
   }
 
+  // correct frames_to_key when lookahead queue is flushing
+  correct_frames_to_key(cpi);
+
   if (rc->baseline_gf_interval > rc->frames_to_key)
     rc->baseline_gf_interval = rc->frames_to_key;
 
@@ -1346,6 +1358,11 @@
     return;
   }
 
+  // correct frames_to_key when lookahead queue is emptying
+  if (cpi->lap_enabled) {
+    correct_frames_to_key(cpi);
+  }
+
   // Load stats for the current frame.
   mod_frame_err = calculate_modified_err(frame_info, twopass, oxcf, this_frame);
 
@@ -1917,6 +1934,7 @@
       rc->frames_to_key = num_frames_to_app_forced_key;
     else
       rc->frames_to_key = AOMMAX(1, cpi->oxcf.key_freq);
+    correct_frames_to_key(cpi);
     rc->kf_boost = DEFAULT_KF_BOOST;
     rc->source_alt_ref_active = 0;
     gf_group->update_type[0] = KF_UPDATE;
@@ -2014,7 +2032,9 @@
       rc->frames_to_key = num_frames_to_app_forced_key;
     else
       rc->frames_to_key = AOMMAX(1, cpi->oxcf.key_freq);
+    correct_frames_to_key(cpi);
   }
+
   // If there is a max kf interval set by the user we must obey it.
   // We already breakout of the loop above at 2x max.
   // This code centers the extra kf if the actual natural interval