Merge "Allow OVERLAY frames to use the show_exsiting_frame flag" into nextgenv2
diff --git a/vp10/encoder/bitstream.c b/vp10/encoder/bitstream.c
index 9743367..6430a710 100644
--- a/vp10/encoder/bitstream.c
+++ b/vp10/encoder/bitstream.c
@@ -2678,7 +2678,6 @@
     vpx_wb_write_bit(wb, cm->log2_tile_rows != 1);
 #endif  // CONFIG_EXT_TILE
 }
-
 static int get_refresh_mask(VP10_COMP *cpi) {
   int refresh_mask = 0;
 
diff --git a/vp10/encoder/bitstream.h b/vp10/encoder/bitstream.h
index cacdb43..24934fe 100644
--- a/vp10/encoder/bitstream.h
+++ b/vp10/encoder/bitstream.h
@@ -23,8 +23,12 @@
 void vp10_encode_token_init(void);
 
 static INLINE int vp10_preserve_existing_gf(VP10_COMP *cpi) {
+#if CONFIG_EXT_REFS
+  return !cpi->multi_arf_allowed && cpi->rc.is_src_frame_alt_ref;
+#else
   return !cpi->multi_arf_allowed && cpi->refresh_golden_frame &&
          cpi->rc.is_src_frame_alt_ref;
+#endif
 }
 
 #ifdef __cplusplus
diff --git a/vp10/encoder/encoder.c b/vp10/encoder/encoder.c
index 5adba4c..0620b88 100644
--- a/vp10/encoder/encoder.c
+++ b/vp10/encoder/encoder.c
@@ -85,6 +85,7 @@
 #endif
 #ifdef OUTPUT_YUV_REC
 FILE *yuv_rec_file;
+#define FILE_NAME_LEN 80
 #endif
 
 #if 0
@@ -3061,9 +3062,36 @@
 }
 #endif
 
+#if CONFIG_EXT_REFS
+static void check_show_existing_frame(VP10_COMP *cpi) {
+  const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
+  VP10_COMMON *const cm = &cpi->common;
+  const FRAME_UPDATE_TYPE next_frame_update_type =
+      gf_group->update_type[gf_group->index];
+
+  if (cpi->rc.is_last_bipred_frame) {
+    // NOTE(zoeliu): If the current 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 = cpi->lst_fb_idxes[0];
+  } else if (next_frame_update_type == OVERLAY_UPDATE &&
+      cpi->is_arf_filter_off) {
+    // Other parameters related to OVERLAY_UPDATE will be taken care of
+    // in vp10_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 = cpi->alt_fb_idx;
+    cpi->is_arf_filter_off = 0;
+  } else {
+    cm->show_existing_frame = 0;
+  }
+}
+#endif
+
 #ifdef OUTPUT_YUV_REC
-void vp10_write_yuv_rec_frame(VP10_COMMON *cm) {
-  YV12_BUFFER_CONFIG *s = cm->frame_to_show;
+void vp10_write_one_yuv_frame(VP10_COMMON *cm, YV12_BUFFER_CONFIG *s) {
   uint8_t *src = s->y_buffer;
   int h = cm->height;
 
@@ -3120,7 +3148,7 @@
 
   fflush(yuv_rec_file);
 }
-#endif
+#endif  // OUTPUT_YUV_REC
 
 #if CONFIG_VP9_HIGHBITDEPTH
 static void scale_and_extend_frame_nonnormative(const YV12_BUFFER_CONFIG *src,
@@ -4849,6 +4877,15 @@
     // Update the frame type
     cm->last_frame_type = cm->frame_type;
 
+#if CONFIG_EXT_REFS
+    // Since we allocate a spot for the OVERLAY frame in the gf group, we need
+    // to do post-encoding update accordingly.
+    if (cpi->rc.is_src_frame_alt_ref) {
+      vp10_set_target_rate(cpi);
+      vp10_rc_postencode_update(cpi, *size);
+    }
+#endif
+
     cm->last_width = cm->width;
     cm->last_height = cm->height;
 
@@ -4976,14 +5013,6 @@
     dump_filtered_recon_frames(cpi);
 #endif  // DUMP_RECON_FRAMES
 
-#if CONFIG_EXT_REFS
-  if (cpi->rc.is_last_bipred_frame) {
-    // NOTE: If the current frame is a LAST_BIPRED_FRAME, next it is needed
-    //       to show the BWDREF_FRAME.
-    cpi->existing_fb_idx_to_show = cpi->bwd_fb_idx;
-  }
-#endif  // CONFIG_EXT_REFS
-
   if (cm->seg.update_map)
     update_reference_segmentation_map(cpi);
 
@@ -5097,13 +5126,20 @@
 static void Pass2Encode(VP10_COMP *cpi, size_t *size,
                         uint8_t *dest, unsigned int *frame_flags) {
   cpi->allow_encode_breakout = ENCODE_BREAKOUT_ENABLED;
+
   encode_frame_to_data_rate(cpi, size, dest, frame_flags);
 
 #if CONFIG_EXT_REFS
-  // Donot do the post-encoding update for show_existing_frame==1.
-  if (!cpi->common.show_existing_frame)
-#endif  // CONFIG_EXT_REFS
+  // Do not do post-encoding update for those frames that do not have a spot in
+  // a gf group, but note that an OVERLAY frame always has a spot in a gf group,
+  // even when show_existing_frame is used.
+  if (!cpi->common.show_existing_frame || cpi->rc.is_src_frame_alt_ref) {
     vp10_twopass_postencode_update(cpi);
+  }
+  check_show_existing_frame(cpi);
+#else
+  vp10_twopass_postencode_update(cpi);
+#endif  // CONFIG_EXT_REFS
 }
 
 static void init_ref_frame_bufs(VP10_COMMON *cm) {
@@ -5500,6 +5536,11 @@
     *time_stamp = source->ts_start;
     *time_end = source->ts_end;
 
+    // We need to adjust frame rate for an overlay frame
+    if (cpi->rc.is_src_frame_alt_ref) {
+      adjust_frame_rate(cpi, source);
+    }
+
     // Find a free buffer for the new frame, releasing the reference previously
     // held.
     if (cm->new_fb_idx != INVALID_IDX) {
@@ -5516,6 +5557,11 @@
     // Start with a 0 size frame.
     *size = 0;
 
+    // We need to update the gf_group for show_existing overlay frame
+    if (cpi->rc.is_src_frame_alt_ref) {
+      vp10_rc_get_second_pass_params(cpi);
+    }
+
     Pass2Encode(cpi, size, dest, frame_flags);
 
     if (cpi->b_calculate_psnr)
@@ -5717,17 +5763,6 @@
 
   vpx_clear_system_state();
 
-#if CONFIG_EXT_REFS
-  if (cpi->rc.is_last_bipred_frame) {
-    // NOTE(zoeliu): If the current frame is a last bi-predictive frame, it is
-    //               needed next to show the BWDREF_FRAME.
-    cpi->rc.is_last_bipred_frame = 0;
-    cm->show_existing_frame = 1;
-  } else {
-    cm->show_existing_frame = 0;
-  }
-#endif  // CONFIG_EXT_REFS
-
   return 0;
 }
 
diff --git a/vp10/encoder/encoder.h b/vp10/encoder/encoder.h
index 5b07c39..63a1669 100644
--- a/vp10/encoder/encoder.h
+++ b/vp10/encoder/encoder.h
@@ -614,6 +614,7 @@
 #if CONFIG_EXT_REFS
   int refresh_frame_mask;
   int existing_fb_idx_to_show;
+  int is_arf_filter_off;
 #endif  // CONFIG_EXT_REFS
 #if CONFIG_GLOBAL_MOTION
   int global_motion_used[MAX_REF_FRAMES];
diff --git a/vp10/encoder/firstpass.c b/vp10/encoder/firstpass.c
index add2510..6bb7165 100644
--- a/vp10/encoder/firstpass.c
+++ b/vp10/encoder/firstpass.c
@@ -2519,6 +2519,9 @@
 static void configure_buffer_updates(VP10_COMP *cpi) {
   TWO_PASS *const twopass = &cpi->twopass;
 
+  // Wei-Ting: Should we define another function to take care of
+  // cpi->rc.is_$Source_Type to make this function as it is in the comment?
+
   cpi->rc.is_src_frame_alt_ref = 0;
 #if CONFIG_EXT_REFS
   cpi->rc.is_bwd_ref_frame = 0;
@@ -2739,6 +2742,7 @@
   }
 
   target_rate = gf_group->bit_allocation[gf_group->index];
+
   if (cpi->common.frame_type == KEY_FRAME)
     target_rate = vp10_rc_clamp_iframe_target_size(cpi, target_rate);
   else
diff --git a/vp10/encoder/ratectrl.c b/vp10/encoder/ratectrl.c
index 5dd42d4..2f2f2b7 100644
--- a/vp10/encoder/ratectrl.c
+++ b/vp10/encoder/ratectrl.c
@@ -203,7 +203,11 @@
                                       rc->avg_frame_bandwidth >> 5);
   if (target < min_frame_target)
     target = min_frame_target;
+#if CONFIG_EXT_REFS
+  if (cpi->rc.is_src_frame_alt_ref) {
+#else
   if (cpi->refresh_golden_frame && rc->is_src_frame_alt_ref) {
+#endif
     // 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
@@ -1232,8 +1236,16 @@
 static void update_golden_frame_stats(VP10_COMP *cpi) {
   RATE_CONTROL *const rc = &cpi->rc;
 
+#if CONFIG_EXT_REFS
   // Update the Golden frame usage counts.
+  // Wei-Ting: If we use show_existing_frame for an OVERLAY frame, only the
+  //           virtual indices for the reference frame will be updated and
+  //           cpi->refresh_golden_frame will still be zero.
+  if (cpi->refresh_golden_frame || rc->is_src_frame_alt_ref) {
+#else
+    // Update the Golden frame usage counts.
   if (cpi->refresh_golden_frame) {
+#endif
     // this frame refreshes means next frames don't unless specified by user
     rc->frames_since_golden = 0;
 
diff --git a/vp10/encoder/temporal_filter.c b/vp10/encoder/temporal_filter.c
index d125dae..707de96 100644
--- a/vp10/encoder/temporal_filter.c
+++ b/vp10/encoder/temporal_filter.c
@@ -733,6 +733,12 @@
 
   // Apply context specific adjustments to the arnr filter parameters.
   adjust_arnr_filter(cpi, distance, rc->gfu_boost, &frames_to_blur, &strength);
+#if CONFIG_EXT_REFS
+  if (strength == 0 && frames_to_blur == 1)
+    cpi->is_arf_filter_off = 1;
+  else
+    cpi->is_arf_filter_off = 0;
+#endif
   frames_to_blur_backward = (frames_to_blur / 2);
   frames_to_blur_forward = ((frames_to_blur - 1) / 2);
   start_frame = distance + frames_to_blur_forward;