Move check_show_existing_frame to next frame

Before this patch, check_show_existing_frame runs at the end of encoding
a frame to check if the *next* frame should be a show_existing_frame.
This patch changes the code so that check_show_existing_frame() runs in
the strategy *before* encoding a frame to check if *this* frame should
be a show_existing_frame.  As well as being less confusing this will
make it a lot easier for show_existing_frame to be moved to
EncodeFrameParams.

Also, remove effectless assignments to rc.is_bwd_ref_frame and
rc.is_last_bipred_frame

This forms part of wider restructuring and refactoring in order to
achieve a clean API separation at the entry to the low-level encoder and
to make the high-level encoding strategy easier to modify.

BUG=aomedia:2244

Change-Id: I9fab1c547bf681c321901f7e3548d811ee8306f8
diff --git a/av1/encoder/encode_strategy.c b/av1/encoder/encode_strategy.c
index 9fe4543..0e74f47 100644
--- a/av1/encoder/encode_strategy.c
+++ b/av1/encoder/encode_strategy.c
@@ -211,9 +211,9 @@
 static void check_show_existing_frame(AV1_COMP *cpi) {
   const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
   AV1_COMMON *const cm = &cpi->common;
-  const FRAME_UPDATE_TYPE next_frame_update_type =
+  const FRAME_UPDATE_TYPE frame_update_type =
       gf_group->update_type[gf_group->index];
-  const int which_arf = (cpi->new_bwdref_update_rule == 1)
+  const int which_arf = cpi->new_bwdref_update_rule
                             ? gf_group->arf_update_idx[gf_group->index] > 0
                             : gf_group->arf_update_idx[gf_group->index];
 
@@ -223,28 +223,27 @@
     // NOTE: When new structure is used, every bwdref will have one overlay
     //       frame. Therefore, there is no need to find out which frame to
     //       show in advance.
-    if (cpi->new_bwdref_update_rule == 0) {
-      // NOTE: If the current frame is a last bi-predictive frame, it is
+    if (!cpi->new_bwdref_update_rule) {
+      // NOTE: If the last frame is a last bi-predictive frame, it is
       //       needed next to show the BWDREF_FRAME, which is pointed by
       //       the last_fb_idxes[0] after reference frame buffer update
-      cpi->rc.is_last_bipred_frame = 0;
       cm->show_existing_frame = 1;
       cpi->existing_fb_idx_to_show = cm->remapped_ref_idx[0];
     }
   } else if (cpi->is_arf_filter_off[which_arf] &&
-             (next_frame_update_type == OVERLAY_UPDATE ||
-              next_frame_update_type == INTNL_OVERLAY_UPDATE)) {
+             (frame_update_type == OVERLAY_UPDATE ||
+              frame_update_type == INTNL_OVERLAY_UPDATE)) {
     const int bwdref_to_show =
-        (cpi->new_bwdref_update_rule == 1) ? BWDREF_FRAME : ALTREF2_FRAME;
+        cpi->new_bwdref_update_rule ? BWDREF_FRAME : ALTREF2_FRAME;
     // Other parameters related to OVERLAY_UPDATE will be taken care of
     // in av1_rc_get_second_pass_params(cpi)
     cm->show_existing_frame = 1;
     cpi->rc.is_src_frame_alt_ref = 1;
     cpi->existing_fb_idx_to_show =
-        (next_frame_update_type == OVERLAY_UPDATE)
+        (frame_update_type == OVERLAY_UPDATE)
             ? get_ref_frame_map_idx(cm, ALTREF_FRAME)
             : get_ref_frame_map_idx(cm, bwdref_to_show);
-    if (cpi->new_bwdref_update_rule == 0) {
+    if (!cpi->new_bwdref_update_rule) {
       cpi->is_arf_filter_off[which_arf] = 0;
     }
   }
@@ -700,7 +699,6 @@
                                frame_params);
   }
 
-  cpi->rc.is_bwd_ref_frame = 0;
   int brf_src_index = get_brf_src_index(cpi);
   if (brf_src_index) {
     assert(brf_src_index <= cpi->rc.frames_to_key);
@@ -734,6 +732,26 @@
   return source;
 }
 
+// Don't allow a show_existing_frame to coincide with an error resilient or
+// S-Frame. An exception can be made in the case of a keyframe, since it does
+// not depend on any previous frames.
+static int allow_show_existing(const AV1_COMP *const cpi) {
+  if (cpi->common.current_frame.frame_number == 0) return 0;
+
+  const struct lookahead_entry *lookahead_src =
+      av1_lookahead_peek(cpi->lookahead, 0);
+  if (lookahead_src == NULL) return 1;
+
+  const int is_error_resilient =
+      cpi->oxcf.error_resilient_mode ||
+      (lookahead_src->flags & AOM_EFLAG_ERROR_RESILIENT);
+  const int is_s_frame =
+      cpi->oxcf.s_frame_mode || (lookahead_src->flags & AOM_EFLAG_SET_S_FRAME);
+  const int is_key_frame =
+      (cpi->rc.frames_to_key == 0) || (cpi->frame_flags & FRAMEFLAGS_KEY);
+  return !(is_error_resilient || is_s_frame) || is_key_frame;
+}
+
 int av1_encode_strategy(AV1_COMP *const cpi, size_t *const size,
                         uint8_t *const dest, unsigned int *frame_flags,
                         int64_t *const time_stamp, int64_t *const time_end,
@@ -748,6 +766,13 @@
   memset(&frame_params, 0, sizeof(frame_params));
   memset(&frame_results, 0, sizeof(frame_results));
 
+  if (oxcf->pass == 0 || oxcf->pass == 2) {
+    check_show_existing_frame(cpi);
+    cm->show_existing_frame &= allow_show_existing(cpi);
+  } else {
+    cm->show_existing_frame = 0;
+  }
+
   int temporal_filtered = 0;
   struct lookahead_entry *source = NULL;
   struct lookahead_entry *last_source = NULL;
@@ -904,7 +929,6 @@
     update_fb_of_context_type(cpi, &frame_params, cpi->fb_of_context_type);
     set_additional_frame_flags(cm, frame_params.frame_flags);
     update_rc_counts(cpi);
-    check_show_existing_frame(cpi);  // Is next frame a show_existing frame?
   }
 
   // Unpack frame_results:
diff --git a/av1/encoder/encoder.c b/av1/encoder/encoder.c
index 57cce12..4499f9e 100644
--- a/av1/encoder/encoder.c
+++ b/av1/encoder/encoder.c
@@ -5701,26 +5701,6 @@
 }
 #endif  // CONFIG_INTERNAL_STATS
 
-// Don't allow a show_existing_frame to coincide with an error resilient or
-// S-Frame. An exception can be made in the case of a keyframe, since it does
-// not depend on any previous frames.
-static int allow_show_existing(const AV1_COMP *const cpi) {
-  if (cpi->common.current_frame.frame_number == 0) return 0;
-
-  const struct lookahead_entry *lookahead_src =
-      av1_lookahead_peek(cpi->lookahead, 0);
-  if (lookahead_src == NULL) return 1;
-
-  const int is_error_resilient =
-      cpi->oxcf.error_resilient_mode ||
-      (lookahead_src->flags & AOM_EFLAG_ERROR_RESILIENT);
-  const int is_s_frame =
-      cpi->oxcf.s_frame_mode || (lookahead_src->flags & AOM_EFLAG_SET_S_FRAME);
-  const int is_key_frame =
-      (cpi->rc.frames_to_key == 0) || (cpi->frame_flags & FRAMEFLAGS_KEY);
-  return !(is_error_resilient || is_s_frame) || is_key_frame;
-}
-
 int av1_get_compressed_data(AV1_COMP *cpi, unsigned int *frame_flags,
                             size_t *size, uint8_t *dest, int64_t *time_stamp,
                             int64_t *time_end, int flush,
@@ -5760,8 +5740,6 @@
   // Initialize fields related to forward keyframes
   cpi->no_show_kf = 0;
 
-  cm->show_existing_frame &= allow_show_existing(cpi);
-
   if (assign_cur_frame_new_fb(cm) == NULL) return AOM_CODEC_ERROR;
 
   const int result = av1_encode_strategy(cpi, size, dest, frame_flags,