Simplify buffer update decision code

* Remove check_src_altref() which was pointless in pass 2 (the
  parameters are reset by av1_rc_get_second_pass_params())
* Decide on FRAME_UPDATE_TYPE and only call
  av1_configure_buffer_updates() once
* Remove direct reads and writes to the refresh_*_frame flags from
  av1_rc_get_one_pass_{cbr,vbr}_params().
* Remove overrides of rc.is_src_frame_alt_ref and rc.is_src_frame_ext_arf
  from check_show_existing_frame(): these will be handled by
  av1_configure_buffer_updates()
* Move overriding refresh flags from set_ext_overrides() to
  av1_configure_buffer_updates()
* Move refresh_*_frame flag overrides from encode_frame_to_data_rate()
  and setup_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: Idf1e75ac59319e16cbefbba56038a21042666d33
diff --git a/av1/encoder/encode_strategy.c b/av1/encoder/encode_strategy.c
index 0e74f47..e4952db 100644
--- a/av1/encoder/encode_strategy.c
+++ b/av1/encoder/encode_strategy.c
@@ -31,17 +31,13 @@
 #include "av1/encoder/temporal_filter.h"
 #include "av1/encoder/tpl_model.h"
 
-// Define the reference buffers that will be updated post encode.
 void av1_configure_buffer_updates(AV1_COMP *const cpi,
-                                  const FRAME_UPDATE_TYPE type) {
+                                  const FRAME_UPDATE_TYPE type,
+                                  int force_refresh_all) {
   // NOTE(weitinglin): Should we define another function to take care of
   // cpi->rc.is_$Source_Type to make this function as it is in the comment?
 
-  // show_existing_frame is a flag left set from the end of encoding the
-  // previous frame.  Alongside it, is_src_frame_alt_ref may also be left
-  // set so shouldn't be cleared in this case.
-  if (!cpi->common.show_existing_frame) cpi->rc.is_src_frame_alt_ref = 0;
-
+  cpi->rc.is_src_frame_alt_ref = 0;
   cpi->rc.is_bwd_ref_frame = 0;
   cpi->rc.is_last_bipred_frame = 0;
   cpi->rc.is_bipred_frame = 0;
@@ -149,6 +145,23 @@
 
     default: assert(0); break;
   }
+
+  if (cpi->ext_refresh_frame_flags_pending &&
+      (cpi->oxcf.pass == 0 || cpi->oxcf.pass == 2)) {
+    cpi->refresh_last_frame = cpi->ext_refresh_last_frame;
+    cpi->refresh_golden_frame = cpi->ext_refresh_golden_frame;
+    cpi->refresh_alt_ref_frame = cpi->ext_refresh_alt_ref_frame;
+    cpi->refresh_bwd_ref_frame = cpi->ext_refresh_bwd_ref_frame;
+    cpi->refresh_alt2_ref_frame = cpi->ext_refresh_alt2_ref_frame;
+  }
+
+  if (force_refresh_all) {
+    cpi->refresh_last_frame = 1;
+    cpi->refresh_golden_frame = 1;
+    cpi->refresh_bwd_ref_frame = 1;
+    cpi->refresh_alt2_ref_frame = 1;
+    cpi->refresh_alt_ref_frame = 1;
+  }
 }
 
 static void set_additional_frame_flags(const AV1_COMMON *const cm,
@@ -238,7 +251,6 @@
     // 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 =
         (frame_update_type == OVERLAY_UPDATE)
             ? get_ref_frame_map_idx(cm, ALTREF_FRAME)
@@ -247,7 +259,6 @@
       cpi->is_arf_filter_off[which_arf] = 0;
     }
   }
-  cpi->rc.is_src_frame_ext_arf = 0;
 }
 
 static void set_ext_overrides(AV1_COMP *const cpi,
@@ -267,14 +278,6 @@
     cm->refresh_frame_context = cpi->ext_refresh_frame_context;
     cpi->ext_refresh_frame_context_pending = 0;
   }
-  if (cpi->ext_refresh_frame_flags_pending) {
-    cpi->refresh_last_frame = cpi->ext_refresh_last_frame;
-    cpi->refresh_golden_frame = cpi->ext_refresh_golden_frame;
-    cpi->refresh_alt_ref_frame = cpi->ext_refresh_alt_ref_frame;
-    cpi->refresh_bwd_ref_frame = cpi->ext_refresh_bwd_ref_frame;
-    cpi->refresh_alt2_ref_frame = cpi->ext_refresh_alt2_ref_frame;
-    cpi->ext_refresh_frame_flags_pending = 0;
-  }
   cm->allow_ref_frame_mvs = cpi->ext_use_ref_frame_mvs;
 
   frame_params->error_resilient_mode = cpi->ext_use_error_resilient;
@@ -508,42 +511,6 @@
   cpi->last_end_time_stamp_seen = source->ts_end;
 }
 
-static void check_src_altref(AV1_COMP *cpi,
-                             const struct lookahead_entry *source) {
-  RATE_CONTROL *const rc = &cpi->rc;
-
-  // If pass == 2, the parameters set here will be reset in
-  // av1_rc_get_second_pass_params()
-
-  if (cpi->oxcf.pass == 2) {
-    const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
-    rc->is_src_frame_alt_ref =
-        (gf_group->update_type[gf_group->index] == INTNL_OVERLAY_UPDATE) ||
-        (gf_group->update_type[gf_group->index] == OVERLAY_UPDATE);
-    rc->is_src_frame_ext_arf =
-        gf_group->update_type[gf_group->index] == INTNL_OVERLAY_UPDATE;
-  } else {
-    rc->is_src_frame_alt_ref =
-        cpi->alt_ref_source && (source == cpi->alt_ref_source);
-  }
-
-  if (rc->is_src_frame_alt_ref) {
-    // Current frame is an ARF overlay frame.
-    cpi->alt_ref_source = NULL;
-
-    if (rc->is_src_frame_ext_arf && !cpi->common.show_existing_frame) {
-      // For INTNL_OVERLAY, when show_existing_frame == 0, they do need to
-      // refresh the LAST_FRAME, i.e. LAST3 gets retired, LAST2 becomes LAST3,
-      // LAST becomes LAST2, and INTNL_OVERLAY becomes LAST.
-      cpi->refresh_last_frame = 1;
-    } else {
-      // Don't refresh the last buffer for an ARF overlay frame. It will
-      // become the GF so preserve last as an alternative prediction option.
-      cpi->refresh_last_frame = 0;
-    }
-  }
-}
-
 // Returns 0 if this is not an alt ref else the offset of the source frame
 // used as the arf midpoint.
 static int get_arf_src_index(AV1_COMP *cpi) {
@@ -633,12 +600,6 @@
       }
     }
     frame_params->show_frame = 0;
-
-    if (oxcf->pass < 2) {
-      // In second pass, the buffer updates configure will be set
-      // in the function av1_rc_get_second_pass_params
-      av1_configure_buffer_updates(cpi, arf2 ? INTNL_ARF_UPDATE : ARF_UPDATE);
-    }
   }
   rc->source_alt_ref_pending = 0;
   return source;
@@ -664,10 +625,11 @@
 
 // Check if we should encode an ARF, ARF2 or BRF.  If not, try a LAST
 // Do some setup associated with the chosen source
+// temporal_filtered, flush, and frame_update_type are outputs.
 // Return the frame source, or NULL if we couldn't find one
 struct lookahead_entry *choose_frame_source(
     AV1_COMP *const cpi, int *const temporal_filtered, int *const flush,
-    struct lookahead_entry **last_source,
+    struct lookahead_entry **last_source, FRAME_UPDATE_TYPE *frame_update_type,
     EncodeFrameParams *const frame_params) {
   AV1_COMMON *const cm = &cpi->common;
   struct lookahead_entry *source = NULL;
@@ -684,6 +646,7 @@
   if (arf_src_index) {
     source = setup_arf_or_arf2(cpi, arf_src_index, 0, temporal_filtered,
                                frame_params);
+    *frame_update_type = ARF_UPDATE;
   }
 
   // Should we encode an arf2 frame (mutually exclusive to ARF)
@@ -697,6 +660,7 @@
   if (arf_src_index) {
     source = setup_arf_or_arf2(cpi, arf_src_index, 1, temporal_filtered,
                                frame_params);
+    *frame_update_type = INTNL_ARF_UPDATE;
   }
 
   int brf_src_index = get_brf_src_index(cpi);
@@ -705,12 +669,7 @@
     if ((source = av1_lookahead_peek(cpi->lookahead, brf_src_index)) != NULL) {
       cm->showable_frame = 1;
       frame_params->show_frame = 0;
-
-      if (cpi->oxcf.pass < 2) {
-        // In second pass, the buffer updates configure will be set
-        // in the function av1_rc_get_second_pass_params
-        av1_configure_buffer_updates(cpi, BRF_UPDATE);
-      }
+      *frame_update_type = BRF_UPDATE;
     }
   }
 
@@ -721,12 +680,14 @@
     }
     // Read in the source frame.
     source = av1_lookahead_pop(cpi->lookahead, *flush);
+    if (source == NULL) return NULL;
+    *frame_update_type = LF_UPDATE;  // Default update type
+    frame_params->show_frame = 1;
 
-    if (source != NULL) {
-      frame_params->show_frame = 1;
-
-      // Check to see if the frame should be encoded as an arf overlay.
-      check_src_altref(cpi, source);
+    // Check to see if the frame should be encoded as an arf overlay.
+    if (cpi->alt_ref_source == source) {
+      *frame_update_type = OVERLAY_UPDATE;
+      cpi->alt_ref_source = NULL;
     }
   }
   return source;
@@ -776,11 +737,19 @@
   int temporal_filtered = 0;
   struct lookahead_entry *source = NULL;
   struct lookahead_entry *last_source = NULL;
+  FRAME_UPDATE_TYPE frame_update_type;
   if (cm->show_existing_frame) {
     source = av1_lookahead_pop(cpi->lookahead, flush);
+    frame_update_type = LF_UPDATE;
   } else {
     source = choose_frame_source(cpi, &temporal_filtered, &flush, &last_source,
-                                 &frame_params);
+                                 &frame_update_type, &frame_params);
+  }
+
+  // In pass 2 we get the frame_update_type from gf_group
+  if (oxcf->pass == 2) {
+    frame_update_type =
+        cpi->twopass.gf_group.update_type[cpi->twopass.gf_group.index];
   }
 
   if (source == NULL) {  // If no source was found, we can't encode a frame.
@@ -807,8 +776,10 @@
     *frame_flags = (source->flags & AOM_EFLAG_FORCE_KF) ? FRAMEFLAGS_KEY : 0;
   cpi->frame_flags = *frame_flags;
 
-  if (frame_params.show_frame ||
-      (cm->show_existing_frame && cpi->rc.is_src_frame_alt_ref)) {
+  const int is_overlay =
+      cm->show_existing_frame && (frame_update_type == OVERLAY_UPDATE ||
+                                  frame_update_type == INTNL_OVERLAY_UPDATE);
+  if (frame_params.show_frame || is_overlay) {
     // Shown frames and arf-overlay frames need frame-rate considering
     adjust_frame_rate(cpi, source);
   }
@@ -835,26 +806,18 @@
     cpi->common.frame_presentation_time = (uint32_t)pts64;
   }
 
-  if (oxcf->pass == 2 &&
-      (!cm->show_existing_frame || cpi->rc.is_src_frame_alt_ref)) {
+  if (oxcf->pass == 2 && (!cm->show_existing_frame || is_overlay)) {
     // GF_GROUP needs updating for arf overlays as well as non-show-existing
     av1_rc_get_second_pass_params(cpi, &frame_params);
+    frame_update_type =
+        cpi->twopass.gf_group.update_type[cpi->twopass.gf_group.index];
   }
+
   if (cm->show_existing_frame && frame_params.frame_type != KEY_FRAME) {
     // Force show-existing frames to be INTER, except forward keyframes
     frame_params.frame_type = INTER_FRAME;
   }
 
-  if (!cm->show_existing_frame) {
-    cm->using_qmatrix = cpi->oxcf.using_qm;
-    cm->min_qmlevel = cpi->oxcf.qm_minlevel;
-    cm->max_qmlevel = cpi->oxcf.qm_maxlevel;
-    if (cpi->twopass.gf_group.index == 1 && cpi->oxcf.enable_tpl_model) {
-      av1_set_frame_size(cpi, cm->width, cm->height);
-      av1_tpl_setup_stats(cpi, &frame_input);
-    }
-  }
-
   frame_params.frame_flags = frame_flags;
 
   // TODO(david.turner@argondesign.com): Move all the encode strategy
@@ -868,18 +831,30 @@
   // parameter should be used with caution.
   frame_params.speed = oxcf->speed;
 
+  if (!cm->show_existing_frame) {
+    cm->using_qmatrix = cpi->oxcf.using_qm;
+    cm->min_qmlevel = cpi->oxcf.qm_minlevel;
+    cm->max_qmlevel = cpi->oxcf.qm_maxlevel;
+    if (cpi->twopass.gf_group.index == 1 && cpi->oxcf.enable_tpl_model) {
+      av1_configure_buffer_updates(cpi, frame_update_type, 0);
+      av1_set_frame_size(cpi, cm->width, cm->height);
+      av1_tpl_setup_stats(cpi, &frame_input);
+    }
+  }
+
   // Work out some encoding parameters specific to the pass:
   if (oxcf->pass == 0) {
     if (cpi->oxcf.rc_mode == AOM_CBR) {
-      av1_rc_get_one_pass_cbr_params(cpi, &frame_params);
+      av1_rc_get_one_pass_cbr_params(cpi, &frame_update_type, &frame_params);
     } else {
-      av1_rc_get_one_pass_vbr_params(cpi, &frame_params);
+      av1_rc_get_one_pass_vbr_params(cpi, &frame_update_type, &frame_params);
     }
   } else if (oxcf->pass == 1) {
-    av1_setup_frame_size(cpi);
     cpi->td.mb.e_mbd.lossless[0] = is_lossless_requested(&cpi->oxcf);
-    if (!cpi->refresh_alt_ref_frame && (cm->current_frame.frame_number == 0 ||
-                                        (cpi->frame_flags & FRAMEFLAGS_KEY))) {
+    const int kf_requested = (cm->current_frame.frame_number == 0 ||
+                              (cpi->frame_flags & FRAMEFLAGS_KEY));
+    if (kf_requested && frame_update_type != OVERLAY_UPDATE &&
+        frame_update_type != INTNL_OVERLAY_UPDATE) {
       frame_params.frame_type = KEY_FRAME;
     } else {
       frame_params.frame_type = INTER_FRAME;
@@ -894,15 +869,20 @@
 #endif
   }
 
-  if (oxcf->pass == 0 || oxcf->pass == 2) {
-    // Apply external override flags
-    set_ext_overrides(cpi, &frame_params);
+  if (oxcf->pass == 0 || oxcf->pass == 2) set_ext_overrides(cpi, &frame_params);
 
+  // Shown keyframes and S frames refresh all reference buffers
+  const int force_refresh_all =
+      ((frame_params.frame_type == KEY_FRAME && frame_params.show_frame) ||
+       frame_params.frame_type == S_FRAME) &&
+      !cm->show_existing_frame;
+
+  av1_configure_buffer_updates(cpi, frame_update_type, force_refresh_all);
+
+  if (oxcf->pass == 0 || oxcf->pass == 2) {
     // Work out which reference frame slots may be used.
     frame_params.ref_frame_flags = get_ref_frame_flags(cpi);
-  }
 
-  if (oxcf->pass == 0 || oxcf->pass == 2) {
     frame_params.primary_ref_frame =
         choose_primary_ref_frame(cpi, &frame_params);
     frame_params.order_offset = get_order_offset(cpi, &frame_params);
diff --git a/av1/encoder/encode_strategy.h b/av1/encoder/encode_strategy.h
index 1671473..da09de1 100644
--- a/av1/encoder/encode_strategy.h
+++ b/av1/encoder/encode_strategy.h
@@ -31,9 +31,12 @@
                         int64_t *const time_stamp, int64_t *const time_end,
                         const aom_rational_t *const timebase, int flush);
 
-// Set individual buffer update flags based on frame reference type
+// Set individual buffer update flags based on frame reference type.
+// force_refresh_all is used when we have a KEY_FRAME or S_FRAME.  It forces all
+// refresh_*_frame flags to be set, because we refresh all buffers in this case.
 void av1_configure_buffer_updates(AV1_COMP *const cpi,
-                                  const FRAME_UPDATE_TYPE type);
+                                  const FRAME_UPDATE_TYPE type,
+                                  int force_refresh_all);
 
 #ifdef __cplusplus
 }  // extern "C"
diff --git a/av1/encoder/encoder.c b/av1/encoder/encoder.c
index 4499f9e..fa43c7c 100644
--- a/av1/encoder/encoder.c
+++ b/av1/encoder/encoder.c
@@ -381,13 +381,9 @@
   }
 
   if (cm->current_frame.frame_type == KEY_FRAME && cm->show_frame) {
-    cpi->refresh_golden_frame = 1;
-    cpi->refresh_alt_ref_frame = 1;
     av1_zero(cpi->interp_filter_selected);
     set_sb_size(&cm->seq_params, select_sb_size(cpi));
   } else if (frame_is_sframe(cm)) {
-    cpi->refresh_golden_frame = 1;
-    cpi->refresh_alt_ref_frame = 1;
     av1_zero(cpi->interp_filter_selected);
     set_sb_size(&cm->seq_params, select_sb_size(cpi));
   } else {
@@ -5307,12 +5303,6 @@
     }
   }
 
-  // If the encoder forced a KEY_FRAME decision or if frame is an S_FRAME
-  if ((current_frame->frame_type == KEY_FRAME && cm->show_frame) ||
-      frame_is_sframe(cm)) {
-    cpi->refresh_last_frame = 1;
-  }
-
   cm->cur_frame->buf.color_primaries = seq_params->color_primaries;
   cm->cur_frame->buf.transfer_characteristics =
       seq_params->transfer_characteristics;
@@ -5734,9 +5724,6 @@
   if (oxcf->large_scale_tile)
     cm->refresh_frame_context = REFRESH_FRAME_CONTEXT_DISABLED;
 
-  // default reference buffers update config
-  av1_configure_buffer_updates(cpi, LF_UPDATE);
-
   // Initialize fields related to forward keyframes
   cpi->no_show_kf = 0;
 
@@ -5959,6 +5946,8 @@
     cpi->ext_refresh_bwd_ref_frame = (upd & AOM_BWD_FLAG) != 0;
     cpi->ext_refresh_alt2_ref_frame = (upd & AOM_ALT2_FLAG) != 0;
     cpi->ext_refresh_frame_flags_pending = 1;
+  } else {
+    cpi->ext_refresh_frame_flags_pending = 0;
   }
 
   cpi->ext_use_ref_frame_mvs = cpi->oxcf.allow_ref_frame_mvs &
diff --git a/av1/encoder/firstpass.c b/av1/encoder/firstpass.c
index eefce39..f66e388 100644
--- a/av1/encoder/firstpass.c
+++ b/av1/encoder/firstpass.c
@@ -537,6 +537,7 @@
   }
 #endif
 
+  av1_setup_frame_size(cpi);
   aom_clear_system_state();
 
   xd->mi = cm->mi_grid_visible;
@@ -2978,9 +2979,9 @@
   // advance the input pointer as we already have what we need.
   if (gf_group->update_type[gf_group->index] == ARF_UPDATE ||
       gf_group->update_type[gf_group->index] == INTNL_ARF_UPDATE) {
-    av1_configure_buffer_updates(cpi, gf_group->update_type[gf_group->index]);
     target_rate = gf_group->bit_allocation[gf_group->index];
-    target_rate = av1_rc_clamp_pframe_target_size(cpi, target_rate);
+    target_rate = av1_rc_clamp_pframe_target_size(
+        cpi, target_rate, gf_group->update_type[gf_group->index]);
     rc->base_frame_target = target_rate;
 
     if (cpi->no_show_kf) {
@@ -3070,8 +3071,6 @@
 #endif
   }
 
-  av1_configure_buffer_updates(cpi, gf_group->update_type[gf_group->index]);
-
   // Do the firstpass stats indicate that this frame is skippable for the
   // partition search?
   if (cpi->sf.allow_partition_search_skip && cpi->oxcf.pass == 2) {
@@ -3080,10 +3079,12 @@
 
   target_rate = gf_group->bit_allocation[gf_group->index];
 
-  if (frame_params->frame_type == KEY_FRAME)
+  if (frame_params->frame_type == KEY_FRAME) {
     target_rate = av1_rc_clamp_iframe_target_size(cpi, target_rate);
-  else
-    target_rate = av1_rc_clamp_pframe_target_size(cpi, target_rate);
+  } else {
+    target_rate = av1_rc_clamp_pframe_target_size(
+        cpi, target_rate, gf_group->update_type[gf_group->index]);
+  }
 
   rc->base_frame_target = target_rate;
 
diff --git a/av1/encoder/ratectrl.c b/av1/encoder/ratectrl.c
index 6a76371..74bd4a3 100644
--- a/av1/encoder/ratectrl.c
+++ b/av1/encoder/ratectrl.c
@@ -175,13 +175,15 @@
                 (int)((uint64_t)bpm * mbs) >> BPER_MB_NORMBITS);
 }
 
-int av1_rc_clamp_pframe_target_size(const AV1_COMP *const cpi, int target) {
+int av1_rc_clamp_pframe_target_size(const AV1_COMP *const cpi, int target,
+                                    FRAME_UPDATE_TYPE frame_update_type) {
   const RATE_CONTROL *rc = &cpi->rc;
   const AV1EncoderConfig *oxcf = &cpi->oxcf;
   const int min_frame_target =
       AOMMAX(rc->min_frame_bandwidth, rc->avg_frame_bandwidth >> 5);
   // Clip the frame target to the minimum setup value.
-  if (cpi->rc.is_src_frame_alt_ref) {
+  if (frame_update_type == OVERLAY_UPDATE ||
+      frame_update_type == INTNL_OVERLAY_UPDATE) {
     // If there is an active ARF at this location use the minimum
     // bits on this frame even if it is a constructed arf.
     // The active maximum quantizer insures that an appropriate
@@ -1320,7 +1322,7 @@
     cpi->twopass.gf_group.index = idx;
     rc_set_frame_target(cpi, target_rate, cm->width, cm->height);
     av1_configure_buffer_updates(
-        cpi, cpi->twopass.gf_group.update_type[cpi->twopass.gf_group.index]);
+        cpi, cpi->twopass.gf_group.update_type[cpi->twopass.gf_group.index], 0);
     tpl_frame->base_qindex = rc_pick_q_and_bounds_two_pass(
         cpi, cm->width, cm->height, &bottom_index, &top_index, &arf_q);
     tpl_frame->base_qindex = AOMMAX(tpl_frame->base_qindex, 1);
@@ -1328,7 +1330,7 @@
   // Reset the actual index and frame update
   cpi->twopass.gf_group.index = gf_index;
   av1_configure_buffer_updates(
-      cpi, cpi->twopass.gf_group.update_type[cpi->twopass.gf_group.index]);
+      cpi, cpi->twopass.gf_group.update_type[cpi->twopass.gf_group.index], 0);
 }
 
 void av1_rc_postencode_update(AV1_COMP *cpi, uint64_t bytes_used) {
@@ -1447,22 +1449,24 @@
 // Use this macro to turn on/off use of alt-refs in one-pass mode.
 #define USE_ALTREF_FOR_ONE_PASS 1
 
-static int calc_pframe_target_size_one_pass_vbr(const AV1_COMP *const cpi) {
+static int calc_pframe_target_size_one_pass_vbr(
+    const AV1_COMP *const cpi, FRAME_UPDATE_TYPE frame_update_type) {
   static const int af_ratio = 10;
   const RATE_CONTROL *const rc = &cpi->rc;
   int target;
 #if USE_ALTREF_FOR_ONE_PASS
-  target =
-      (!rc->is_src_frame_alt_ref &&
-       (cpi->refresh_golden_frame || cpi->refresh_alt_ref_frame))
-          ? (rc->avg_frame_bandwidth * rc->baseline_gf_interval * af_ratio) /
-                (rc->baseline_gf_interval + af_ratio - 1)
-          : (rc->avg_frame_bandwidth * rc->baseline_gf_interval) /
-                (rc->baseline_gf_interval + af_ratio - 1);
+  if (frame_update_type == KF_UPDATE || frame_update_type == GF_UPDATE ||
+      frame_update_type == ARF_UPDATE) {
+    target = (rc->avg_frame_bandwidth * rc->baseline_gf_interval * af_ratio) /
+             (rc->baseline_gf_interval + af_ratio - 1);
+  } else {
+    target = (rc->avg_frame_bandwidth * rc->baseline_gf_interval) /
+             (rc->baseline_gf_interval + af_ratio - 1);
+  }
 #else
   target = rc->avg_frame_bandwidth;
 #endif
-  return av1_rc_clamp_pframe_target_size(cpi, target);
+  return av1_rc_clamp_pframe_target_size(cpi, target, frame_update_type);
 }
 
 static int calc_iframe_target_size_one_pass_vbr(const AV1_COMP *const cpi) {
@@ -1473,6 +1477,7 @@
 }
 
 void av1_rc_get_one_pass_vbr_params(AV1_COMP *cpi,
+                                    FRAME_UPDATE_TYPE *const frame_update_type,
                                     EncodeFrameParams *const frame_params) {
   AV1_COMMON *const cm = &cpi->common;
   RATE_CONTROL *const rc = &cpi->rc;
@@ -1483,7 +1488,7 @@
   int sframe_mode = cpi->oxcf.sframe_mode;
   int sframe_enabled = cpi->oxcf.sframe_enabled;
   // TODO(yaowu): replace the "auto_key && 0" below with proper decision logic.
-  if (!cpi->refresh_alt_ref_frame &&
+  if (*frame_update_type != ARF_UPDATE &&
       (current_frame->frame_number == 0 ||
        (cpi->frame_flags & FRAMEFLAGS_KEY) || rc->frames_to_key == 0 ||
        (cpi->oxcf.auto_key && 0))) {
@@ -1501,7 +1506,8 @@
           // sframe_mode == 1: insert sframe if it matches altref frame.
 
           if (current_frame->frame_number % sframe_dist == 0 &&
-              current_frame->frame_number != 0 && cpi->refresh_alt_ref_frame) {
+              current_frame->frame_number != 0 &&
+              *frame_update_type == ARF_UPDATE) {
             frame_params->frame_type = S_FRAME;
           }
         } else {
@@ -1513,7 +1519,7 @@
             rc->sframe_due = 1;
           }
 
-          if (rc->sframe_due && cpi->refresh_alt_ref_frame) {
+          if (rc->sframe_due && *frame_update_type == ARF_UPDATE) {
             frame_params->frame_type = S_FRAME;
             rc->sframe_due = 0;
           }
@@ -1536,7 +1542,7 @@
     } else {
       rc->constrained_gf_group = 0;
     }
-    cpi->refresh_golden_frame = 1;
+    if (*frame_update_type == LF_UPDATE) *frame_update_type = GF_UPDATE;
     rc->source_alt_ref_pending = USE_ALTREF_FOR_ONE_PASS;
     rc->gfu_boost = DEFAULT_GF_BOOST;
   }
@@ -1547,11 +1553,12 @@
   if (frame_params->frame_type == KEY_FRAME)
     target = calc_iframe_target_size_one_pass_vbr(cpi);
   else
-    target = calc_pframe_target_size_one_pass_vbr(cpi);
+    target = calc_pframe_target_size_one_pass_vbr(cpi, *frame_update_type);
   rc_set_frame_target(cpi, target, cm->width, cm->height);
 }
 
-static int calc_pframe_target_size_one_pass_cbr(const AV1_COMP *cpi) {
+static int calc_pframe_target_size_one_pass_cbr(
+    const AV1_COMP *cpi, FRAME_UPDATE_TYPE frame_update_type) {
   const AV1EncoderConfig *oxcf = &cpi->oxcf;
   const RATE_CONTROL *rc = &cpi->rc;
   const int64_t diff = rc->optimal_buffer_level - rc->buffer_level;
@@ -1562,12 +1569,14 @@
 
   if (oxcf->gf_cbr_boost_pct) {
     const int af_ratio_pct = oxcf->gf_cbr_boost_pct + 100;
-    target = cpi->refresh_golden_frame
-                 ? (rc->avg_frame_bandwidth * rc->baseline_gf_interval *
-                    af_ratio_pct) /
-                       (rc->baseline_gf_interval * 100 + af_ratio_pct - 100)
-                 : (rc->avg_frame_bandwidth * rc->baseline_gf_interval * 100) /
-                       (rc->baseline_gf_interval * 100 + af_ratio_pct - 100);
+    if (frame_update_type == GF_UPDATE || frame_update_type == OVERLAY_UPDATE) {
+      target =
+          (rc->avg_frame_bandwidth * rc->baseline_gf_interval * af_ratio_pct) /
+          (rc->baseline_gf_interval * 100 + af_ratio_pct - 100);
+    } else {
+      target = (rc->avg_frame_bandwidth * rc->baseline_gf_interval * 100) /
+               (rc->baseline_gf_interval * 100 + af_ratio_pct - 100);
+    }
   } else {
     target = rc->avg_frame_bandwidth;
   }
@@ -1611,6 +1620,7 @@
 }
 
 void av1_rc_get_one_pass_cbr_params(AV1_COMP *cpi,
+                                    FRAME_UPDATE_TYPE *const frame_update_type,
                                     EncodeFrameParams *const frame_params) {
   AV1_COMMON *const cm = &cpi->common;
   RATE_CONTROL *const rc = &cpi->rc;
@@ -1639,7 +1649,7 @@
     // NOTE: frames_till_gf_update_due must be <= frames_to_key.
     if (rc->frames_till_gf_update_due > rc->frames_to_key)
       rc->frames_till_gf_update_due = rc->frames_to_key;
-    cpi->refresh_golden_frame = 1;
+    if (*frame_update_type == LF_UPDATE) *frame_update_type = GF_UPDATE;
     rc->gfu_boost = DEFAULT_GF_BOOST;
   }
 
@@ -1651,7 +1661,7 @@
   if (frame_params->frame_type == KEY_FRAME)
     target = calc_iframe_target_size_one_pass_cbr(cpi);
   else
-    target = calc_pframe_target_size_one_pass_cbr(cpi);
+    target = calc_pframe_target_size_one_pass_cbr(cpi, *frame_update_type);
 
   rc_set_frame_target(cpi, target, cm->width, cm->height);
   // TODO(afergs): Decide whether to scale up, down, or not at all
diff --git a/av1/encoder/ratectrl.h b/av1/encoder/ratectrl.h
index 36c738c..3f2dfb1 100644
--- a/av1/encoder/ratectrl.h
+++ b/av1/encoder/ratectrl.h
@@ -15,6 +15,8 @@
 #include "aom/aom_codec.h"
 #include "aom/aom_integer.h"
 
+#include "aom_ports/mem.h"
+
 #include "av1/common/blockd.h"
 #include "av1/common/onyxc_int.h"
 
@@ -201,9 +203,11 @@
 // encode_frame_to_data_rate() function.
 struct EncodeFrameParams;
 void av1_rc_get_one_pass_vbr_params(
-    struct AV1_COMP *cpi, struct EncodeFrameParams *const frame_params);
+    struct AV1_COMP *cpi, uint8_t *const frame_update_type,
+    struct EncodeFrameParams *const frame_params);
 void av1_rc_get_one_pass_cbr_params(
-    struct AV1_COMP *cpi, struct EncodeFrameParams *const frame_params);
+    struct AV1_COMP *cpi, uint8_t *const frame_update_type,
+    struct EncodeFrameParams *const frame_params);
 
 // Post encode update of the rate control parameters based
 // on bytes used
@@ -243,7 +247,7 @@
 int av1_rc_clamp_iframe_target_size(const struct AV1_COMP *const cpi,
                                     int target);
 int av1_rc_clamp_pframe_target_size(const struct AV1_COMP *const cpi,
-                                    int target);
+                                    int target, uint8_t frame_update_type);
 
 // Computes a q delta (in "q index" terms) to get from a starting q value
 // to a target q value