De-duplicate references to current frame buffer

There are a number of explicit (as pointers) and implicit (as indices
into the reference buffer array) references to the current frame's
buffer.  This patch removes some of this redundancy, using the single
RefCntBuffer pointer cur_frame in AV1_COMMON.

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

BUG=aomedia:2244

Change-Id: Ic0c6e831e19cb587c8aa830338f654926ff4a1d8
diff --git a/av1/av1_dx_iface.c b/av1/av1_dx_iface.c
index ff77289..08da650 100644
--- a/av1/av1_dx_iface.c
+++ b/av1/av1_dx_iface.c
@@ -318,6 +318,7 @@
     BufferPool *const pool = cm->buffer_pool;
 
     cm->new_fb_idx = INVALID_IDX;
+    cm->cur_frame = NULL;
     cm->byte_alignment = ctx->byte_alignment;
     cm->skip_loop_filter = ctx->skip_loop_filter;
     cm->skip_film_grain = ctx->skip_film_grain;
@@ -356,7 +357,7 @@
 
   if (result != 0) {
     // Check decode result in serial decode.
-    frame_worker_data->pbi->cur_buf->buf.corrupted = 1;
+    frame_worker_data->pbi->common.cur_frame->buf.corrupted = 1;
     frame_worker_data->pbi->need_resync = 1;
   }
   return !result;
@@ -693,6 +694,9 @@
       AVxWorker *const worker = &ctx->frame_workers[ctx->next_output_worker_id];
       FrameWorkerData *const frame_worker_data =
           (FrameWorkerData *)worker->data1;
+      AV1Decoder *const pbi = frame_worker_data->pbi;
+      AV1_COMMON *const cm = &pbi->common;
+      RefCntBuffer *const frame_bufs = cm->buffer_pool->frame_bufs;
       ctx->next_output_worker_id =
           (ctx->next_output_worker_id + 1) % ctx->num_frame_workers;
       // Wait for the frame from worker thread.
@@ -705,9 +709,6 @@
         aom_film_grain_t *grain_params;
         if (av1_get_raw_frame(frame_worker_data->pbi, *index, &sd,
                               &grain_params) == 0) {
-          AV1Decoder *const pbi = frame_worker_data->pbi;
-          AV1_COMMON *const cm = &pbi->common;
-          RefCntBuffer *const frame_bufs = cm->buffer_pool->frame_bufs;
           const int buf_idx = pbi->output_frame_index[*index];
           ctx->last_show_frame = buf_idx;
           if (ctx->need_resync) return NULL;
diff --git a/av1/common/onyxc_int.h b/av1/common/onyxc_int.h
index 2a91167..ccd5ebb 100644
--- a/av1/common/onyxc_int.h
+++ b/av1/common/onyxc_int.h
@@ -379,7 +379,6 @@
 
   int show_frame;
   int showable_frame;  // frame can be used as show existing frame in future
-  int last_show_frame;
   int show_existing_frame;
   // Flag for a frame used as a reference - not written to the bitstream
   int is_reference_frame;
@@ -614,11 +613,6 @@
   return &cm->buffer_pool->frame_bufs[cm->ref_frame_map[index]].buf;
 }
 
-static INLINE YV12_BUFFER_CONFIG *get_frame_new_buffer(
-    const AV1_COMMON *const cm) {
-  return &cm->buffer_pool->frame_bufs[cm->new_fb_idx].buf;
-}
-
 static INLINE int get_free_fb(AV1_COMMON *cm) {
   RefCntBuffer *const frame_bufs = cm->buffer_pool->frame_bufs;
   int i;
diff --git a/av1/common/resize.c b/av1/common/resize.c
index 5d4448a..8b24ed0 100644
--- a/av1/common/resize.c
+++ b/av1/common/resize.c
@@ -1362,7 +1362,7 @@
   YV12_BUFFER_CONFIG copy_buffer;
   memset(&copy_buffer, 0, sizeof(copy_buffer));
 
-  YV12_BUFFER_CONFIG *const frame_to_show = get_frame_new_buffer(cm);
+  YV12_BUFFER_CONFIG *const frame_to_show = &cm->cur_frame->buf;
 
   const int aligned_width = ALIGN_POWER_OF_TWO(cm->width, 3);
   if (aom_alloc_frame_buffer(
@@ -1382,8 +1382,7 @@
   // Realloc the current frame buffer at a higher resolution in place.
   if (pool != NULL) {
     // Use callbacks if on the decoder.
-    aom_codec_frame_buffer_t *fb =
-        &pool->frame_bufs[cm->new_fb_idx].raw_frame_buffer;
+    aom_codec_frame_buffer_t *fb = &cm->cur_frame->raw_frame_buffer;
     aom_release_frame_buffer_cb_fn_t release_fb_cb = pool->release_fb_cb;
     aom_get_frame_buffer_cb_fn_t cb = pool->get_fb_cb;
     void *cb_priv = pool->cb_priv;
diff --git a/av1/decoder/decodeframe.c b/av1/decoder/decodeframe.c
index f9de800..661d136 100644
--- a/av1/decoder/decodeframe.c
+++ b/av1/decoder/decodeframe.c
@@ -368,8 +368,8 @@
   // as they are always compared to values that are in 1/8th pel units
   set_mi_row_col(xd, tile, mi_row, bh, mi_col, bw, cm->mi_rows, cm->mi_cols);
 
-  av1_setup_dst_planes(xd->plane, bsize, get_frame_new_buffer(cm), mi_row,
-                       mi_col, 0, num_planes);
+  av1_setup_dst_planes(xd->plane, bsize, &cm->cur_frame->buf, mi_row, mi_col, 0,
+                       num_planes);
 }
 
 static void decode_mbmi_block(AV1Decoder *const pbi, MACROBLOCKD *const xd,
@@ -1039,7 +1039,7 @@
                                       dst_width1, dst_height1, dst_stride1);
   dec_build_prediction_by_left_preds(cm, xd, mi_row, mi_col, dst_buf2,
                                      dst_width2, dst_height2, dst_stride2);
-  av1_setup_dst_planes(xd->plane, xd->mi[0]->sb_type, get_frame_new_buffer(cm),
+  av1_setup_dst_planes(xd->plane, xd->mi[0]->sb_type, &cm->cur_frame->buf,
                        mi_row, mi_col, 0, num_planes);
   av1_build_obmc_inter_prediction(cm, xd, mi_row, mi_col, dst_buf1, dst_stride1,
                                   dst_buf2, dst_stride2);
@@ -1686,8 +1686,8 @@
   // as they are always compared to values that are in 1/8th pel units
   set_mi_row_col(xd, tile, mi_row, bh, mi_col, bw, cm->mi_rows, cm->mi_cols);
 
-  av1_setup_dst_planes(xd->plane, bsize, get_frame_new_buffer(cm), mi_row,
-                       mi_col, 0, num_planes);
+  av1_setup_dst_planes(xd->plane, bsize, &cm->cur_frame->buf, mi_row, mi_col, 0,
+                       num_planes);
 }
 
 static void decode_block(AV1Decoder *const pbi, ThreadData *const td,
@@ -2372,32 +2372,27 @@
 
   lock_buffer_pool(pool);
   if (aom_realloc_frame_buffer(
-          get_frame_new_buffer(cm), cm->width, cm->height,
-          seq_params->subsampling_x, seq_params->subsampling_y,
-          seq_params->use_highbitdepth, AOM_BORDER_IN_PIXELS,
-          cm->byte_alignment,
-          &pool->frame_bufs[cm->new_fb_idx].raw_frame_buffer, pool->get_fb_cb,
-          pool->cb_priv)) {
+          &cm->cur_frame->buf, cm->width, cm->height, seq_params->subsampling_x,
+          seq_params->subsampling_y, seq_params->use_highbitdepth,
+          AOM_BORDER_IN_PIXELS, cm->byte_alignment,
+          &cm->cur_frame->raw_frame_buffer, pool->get_fb_cb, pool->cb_priv)) {
     unlock_buffer_pool(pool);
     aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
                        "Failed to allocate frame buffer");
   }
   unlock_buffer_pool(pool);
 
-  pool->frame_bufs[cm->new_fb_idx].buf.bit_depth =
-      (unsigned int)seq_params->bit_depth;
-  pool->frame_bufs[cm->new_fb_idx].buf.color_primaries =
-      seq_params->color_primaries;
-  pool->frame_bufs[cm->new_fb_idx].buf.transfer_characteristics =
+  cm->cur_frame->buf.bit_depth = (unsigned int)seq_params->bit_depth;
+  cm->cur_frame->buf.color_primaries = seq_params->color_primaries;
+  cm->cur_frame->buf.transfer_characteristics =
       seq_params->transfer_characteristics;
-  pool->frame_bufs[cm->new_fb_idx].buf.matrix_coefficients =
-      seq_params->matrix_coefficients;
-  pool->frame_bufs[cm->new_fb_idx].buf.monochrome = seq_params->monochrome;
-  pool->frame_bufs[cm->new_fb_idx].buf.chroma_sample_position =
+  cm->cur_frame->buf.matrix_coefficients = seq_params->matrix_coefficients;
+  cm->cur_frame->buf.monochrome = seq_params->monochrome;
+  cm->cur_frame->buf.chroma_sample_position =
       seq_params->chroma_sample_position;
-  pool->frame_bufs[cm->new_fb_idx].buf.color_range = seq_params->color_range;
-  pool->frame_bufs[cm->new_fb_idx].buf.render_width = cm->render_width;
-  pool->frame_bufs[cm->new_fb_idx].buf.render_height = cm->render_height;
+  cm->cur_frame->buf.color_range = seq_params->color_range;
+  cm->cur_frame->buf.render_width = cm->render_width;
+  cm->cur_frame->buf.render_height = cm->render_height;
 }
 
 static void setup_frame_size(AV1_COMMON *cm, int frame_size_override_flag,
@@ -4690,7 +4685,7 @@
   for (int mask = pbi->refresh_frame_flags; mask; mask >>= 1) {
     if (mask & 1) {
       cm->next_ref_frame_map[ref_index] = cm->new_fb_idx;
-      ++frame_bufs[cm->new_fb_idx].ref_count;
+      ++cm->cur_frame->ref_count;
     } else {
       cm->next_ref_frame_map[ref_index] = cm->ref_frame_map[ref_index];
     }
@@ -4769,7 +4764,7 @@
 
   lock_buffer_pool(cm->buffer_pool);
   reset_ref_frame_map(cm);
-  assert(frame_bufs[cm->new_fb_idx].ref_count == 1);
+  assert(cm->cur_frame->ref_count == 1);
   for (i = 0; i < FRAME_BUFFERS; ++i) {
     // Reset all unreferenced frame buffers. We can also reset cm->new_fb_idx
     // because we are the sole owner of cm->new_fb_idx.
@@ -4850,15 +4845,17 @@
                            "Buffer %d does not contain a decoded frame",
                            frame_to_show);
       }
-      // cm->new_fb_idx should be the return value of the get_free_fb() call
-      // in av1_receive_compressed_data(), and generate_next_ref_frame_map()
-      // has not been called, so ref_count should still be 1.
-      assert(frame_bufs[cm->new_fb_idx].ref_count == 1);
+      // cm->cur_frame should be the buffer referenced by the return value
+      // of the get_free_fb() call in av1_receive_compressed_data(), and
+      // generate_next_ref_frame_map() has not been called, so ref_count
+      // should still be 1.
+      assert(cm->cur_frame->ref_count == 1);
       // ref_cnt_fb() decrements ref_count directly rather than call
-      // decrease_ref_count(). If frame_bufs[cm->new_fb_idx].raw_frame_buffer
+      // decrease_ref_count(). If cm->cur_frame->raw_frame_buffer
       // has already been allocated, it will not be released by ref_cnt_fb()!
-      assert(!frame_bufs[cm->new_fb_idx].raw_frame_buffer.data);
+      assert(!cm->cur_frame->raw_frame_buffer.data);
       assign_frame_buffer(frame_bufs, &cm->new_fb_idx, frame_to_show);
+      cm->cur_frame = &cm->buffer_pool->frame_bufs[cm->new_fb_idx];
       cm->reset_decoder_state =
           frame_bufs[frame_to_show].frame_type == KEY_FRAME;
       unlock_buffer_pool(pool);
@@ -5266,18 +5263,17 @@
     cm->refresh_frame_context = REFRESH_FRAME_CONTEXT_DISABLED;
   }
 
-  get_frame_new_buffer(cm)->bit_depth = seq_params->bit_depth;
-  get_frame_new_buffer(cm)->color_primaries = seq_params->color_primaries;
-  get_frame_new_buffer(cm)->transfer_characteristics =
+  cm->cur_frame->buf.bit_depth = seq_params->bit_depth;
+  cm->cur_frame->buf.color_primaries = seq_params->color_primaries;
+  cm->cur_frame->buf.transfer_characteristics =
       seq_params->transfer_characteristics;
-  get_frame_new_buffer(cm)->matrix_coefficients =
-      seq_params->matrix_coefficients;
-  get_frame_new_buffer(cm)->monochrome = seq_params->monochrome;
-  get_frame_new_buffer(cm)->chroma_sample_position =
+  cm->cur_frame->buf.matrix_coefficients = seq_params->matrix_coefficients;
+  cm->cur_frame->buf.monochrome = seq_params->monochrome;
+  cm->cur_frame->buf.chroma_sample_position =
       seq_params->chroma_sample_position;
-  get_frame_new_buffer(cm)->color_range = seq_params->color_range;
-  get_frame_new_buffer(cm)->render_width = cm->render_width;
-  get_frame_new_buffer(cm)->render_height = cm->render_height;
+  cm->cur_frame->buf.color_range = seq_params->color_range;
+  cm->cur_frame->buf.render_width = cm->render_width;
+  cm->cur_frame->buf.render_height = cm->render_height;
 
   if (pbi->need_resync) {
     aom_internal_error(&cm->error, AOM_CODEC_CORRUPT_FRAME,
@@ -5480,7 +5476,7 @@
 
   const uint32_t uncomp_hdr_size =
       (uint32_t)aom_rb_bytes_read(rb);  // Size of the uncompressed header
-  YV12_BUFFER_CONFIG *new_fb = get_frame_new_buffer(cm);
+  YV12_BUFFER_CONFIG *new_fb = &cm->cur_frame->buf;
   xd->cur_buf = new_fb;
   if (av1_allow_intrabc(cm)) {
     av1_setup_scale_factors_for_frame(
@@ -5578,13 +5574,13 @@
     if (cm->lf.filter_level[0] || cm->lf.filter_level[1]) {
       if (pbi->num_workers > 1) {
         av1_loop_filter_frame_mt(
-            get_frame_new_buffer(cm), cm, &pbi->mb, 0, num_planes, 0,
+            &cm->cur_frame->buf, cm, &pbi->mb, 0, num_planes, 0,
 #if LOOP_FILTER_BITMASK
             1,
 #endif
             pbi->tile_workers, pbi->num_workers, &pbi->lf_row_sync);
       } else {
-        av1_loop_filter_frame(get_frame_new_buffer(cm), cm, &pbi->mb,
+        av1_loop_filter_frame(&cm->cur_frame->buf, cm, &pbi->mb,
 #if LOOP_FILTER_BITMASK
                               1,
 #endif
@@ -5605,14 +5601,16 @@
 
     if (!optimized_loop_restoration) {
       if (do_loop_restoration)
-        av1_loop_restoration_save_boundary_lines(&pbi->cur_buf->buf, cm, 0);
+        av1_loop_restoration_save_boundary_lines(&pbi->common.cur_frame->buf,
+                                                 cm, 0);
 
-      if (do_cdef) av1_cdef_frame(&pbi->cur_buf->buf, cm, &pbi->mb);
+      if (do_cdef) av1_cdef_frame(&pbi->common.cur_frame->buf, cm, &pbi->mb);
 
       superres_post_decode(pbi);
 
       if (do_loop_restoration) {
-        av1_loop_restoration_save_boundary_lines(&pbi->cur_buf->buf, cm, 1);
+        av1_loop_restoration_save_boundary_lines(&pbi->common.cur_frame->buf,
+                                                 cm, 1);
         if (pbi->num_workers > 1) {
           av1_loop_restoration_filter_frame_mt(
               (YV12_BUFFER_CONFIG *)xd->cur_buf, cm, optimized_loop_restoration,
diff --git a/av1/decoder/decoder.c b/av1/decoder/decoder.c
index 00f7916..019db7e 100644
--- a/av1/decoder/decoder.c
+++ b/av1/decoder/decoder.c
@@ -400,13 +400,14 @@
     }
 
     if (cm->show_existing_frame || cm->show_frame) {
-      YV12_BUFFER_CONFIG *cur_frame = get_frame_new_buffer(cm);
+      YV12_BUFFER_CONFIG *cur_frame = &cm->cur_frame->buf;
       if (pbi->output_all_layers) {
         // Append this frame to the output queue
         if (pbi->num_output_frames >= MAX_NUM_SPATIAL_LAYERS) {
           // We can't store the new frame anywhere, so drop it and return an
           // error
           decrease_ref_count(cm->new_fb_idx, frame_bufs, pool);
+          cm->cur_frame = NULL;
           cm->error.error_code = AOM_CODEC_UNSUP_BITSTREAM;
         } else {
           pbi->output_frames[pbi->num_output_frames] = cur_frame;
@@ -425,6 +426,7 @@
       }
     } else {
       decrease_ref_count(cm->new_fb_idx, frame_bufs, pool);
+      cm->cur_frame = NULL;
     }
 
     unlock_buffer_pool(pool);
@@ -436,6 +438,7 @@
     // Nothing was decoded, so just drop this frame buffer
     lock_buffer_pool(pool);
     decrease_ref_count(cm->new_fb_idx, frame_bufs, pool);
+    cm->cur_frame = NULL;
     unlock_buffer_pool(pool);
   }
 
@@ -453,7 +456,6 @@
                                 const uint8_t **psource) {
   AV1_COMMON *volatile const cm = &pbi->common;
   BufferPool *volatile const pool = cm->buffer_pool;
-  RefCntBuffer *volatile const frame_bufs = cm->buffer_pool->frame_bufs;
   const uint8_t *source = *psource;
   cm->error.error_code = AOM_CODEC_OK;
   cm->error.has_detail = 0;
@@ -487,8 +489,6 @@
 
   if (!pbi->camera_frame_header_ready) pbi->hold_ref_buf = 0;
 
-  pbi->cur_buf = &frame_bufs[cm->new_fb_idx];
-
   // The jmp_buf is valid only for the duration of the function that calls
   // setjmp(). Therefore, this function must reset the 'setjmp' field to 0
   // before it returns.
@@ -547,8 +547,6 @@
   aom_clear_system_state();
 
   if (!cm->show_existing_frame) {
-    cm->last_show_frame = cm->show_frame;
-
     if (cm->seg.enabled) {
       if (cm->prev_frame && (cm->mi_rows == cm->prev_frame->mi_rows) &&
           (cm->mi_cols == cm->prev_frame->mi_cols)) {
diff --git a/av1/decoder/decoder.h b/av1/decoder/decoder.h
index f8cc3ab..6ca28e7 100644
--- a/av1/decoder/decoder.h
+++ b/av1/decoder/decoder.h
@@ -165,10 +165,6 @@
 
   int refresh_frame_flags;
 
-  // TODO(hkuang): Combine this with cur_buf in macroblockd as they are
-  // the same.
-  RefCntBuffer *cur_buf;  //  Current decoding frame buffer.
-
   AVxWorker lf_worker;
   AV1LfSync lf_row_sync;
   AV1LrSync lr_row_sync;
diff --git a/av1/decoder/obu.c b/av1/decoder/obu.c
index 3221eab..d892dc4 100644
--- a/av1/decoder/obu.c
+++ b/av1/decoder/obu.c
@@ -421,7 +421,7 @@
   const int ssx = cm->seq_params.subsampling_x;
   const int num_planes = av1_num_planes(cm);
 
-  YV12_BUFFER_CONFIG *cur_frame = get_frame_new_buffer(cm);
+  YV12_BUFFER_CONFIG *cur_frame = &cm->cur_frame->buf;
   const int tr = tile_idx / (pbi->output_frame_width_in_tiles_minus_1 + 1);
   const int tc = tile_idx % (pbi->output_frame_width_in_tiles_minus_1 + 1);
   int plane;
diff --git a/av1/encoder/bitstream.c b/av1/encoder/bitstream.c
index a462fc7..34511dc 100644
--- a/av1/encoder/bitstream.c
+++ b/av1/encoder/bitstream.c
@@ -2948,6 +2948,7 @@
                            frame_to_show);
       }
       assign_frame_buffer(frame_bufs, &cm->new_fb_idx, frame_to_show);
+      cm->cur_frame = &frame_bufs[cm->new_fb_idx];
 
       aom_wb_write_bit(wb, 1);  // show_existing_frame
       aom_wb_write_literal(wb, cpi->existing_fb_idx_to_show, 3);
diff --git a/av1/encoder/encodeframe.c b/av1/encoder/encodeframe.c
index 04b9ac9..5f81fd5 100644
--- a/av1/encoder/encodeframe.c
+++ b/av1/encoder/encodeframe.c
@@ -241,8 +241,8 @@
       xd->left_txfm_context_buffer + (mi_row & MAX_MIB_MASK);
 
   // Set up destination pointers.
-  av1_setup_dst_planes(xd->plane, bsize, get_frame_new_buffer(cm), mi_row,
-                       mi_col, 0, num_planes);
+  av1_setup_dst_planes(xd->plane, bsize, &cm->cur_frame->buf, mi_row, mi_col, 0,
+                       num_planes);
 
   // Set up limit values for MV components.
   // Mv beyond the range do not produce new/different prediction block.
diff --git a/av1/encoder/encoder.c b/av1/encoder/encoder.c
index 9423e18..ef98c3f 100644
--- a/av1/encoder/encoder.c
+++ b/av1/encoder/encoder.c
@@ -3988,6 +3988,7 @@
   int i;
   BufferPool *const pool = cm->buffer_pool;
   cm->new_fb_idx = INVALID_IDX;
+  cm->cur_frame = NULL;
   for (i = 0; i < REF_FRAMES; ++i) {
     cm->ref_frame_map[i] = INVALID_IDX;
   }
@@ -4084,10 +4085,9 @@
 
   // Reset the frame pointers to the current frame size.
   if (aom_realloc_frame_buffer(
-          get_frame_new_buffer(cm), cm->width, cm->height,
-          seq_params->subsampling_x, seq_params->subsampling_y,
-          seq_params->use_highbitdepth, AOM_BORDER_IN_PIXELS,
-          cm->byte_alignment, NULL, NULL, NULL))
+          &cm->cur_frame->buf, cm->width, cm->height, seq_params->subsampling_x,
+          seq_params->subsampling_y, seq_params->use_highbitdepth,
+          AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL, NULL, NULL))
     aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
                        "Failed to allocate frame buffer");
 
@@ -4654,9 +4654,9 @@
         int64_t low_err_target = cpi->ambient_err >> 1;
 
         if (cm->seq_params.use_highbitdepth) {
-          kf_err = aom_highbd_get_y_sse(cpi->source, get_frame_new_buffer(cm));
+          kf_err = aom_highbd_get_y_sse(cpi->source, &cm->cur_frame->buf);
         } else {
-          kf_err = aom_get_y_sse(cpi->source, get_frame_new_buffer(cm));
+          kf_err = aom_get_y_sse(cpi->source, &cm->cur_frame->buf);
         }
         // Prevent possible divide by zero error below for perfect KF
         kf_err += !kf_err;
@@ -5082,11 +5082,10 @@
     cpi->seq_params_locked = 1;
 
     // Set up frame to show to get ready for stats collection.
-    cm->frame_to_show = get_frame_new_buffer(cm);
+    cm->frame_to_show = &cm->cur_frame->buf;
 
     // Update current frame offset.
-    current_frame->order_hint =
-        cm->buffer_pool->frame_bufs[cm->new_fb_idx].order_hint;
+    current_frame->order_hint = cm->cur_frame->order_hint;
 
 #if DUMP_RECON_FRAMES == 1
     // NOTE(zoeliu): For debug - Output the filtered reconstructed video.
@@ -5240,10 +5239,9 @@
   // the force key frame
   if (cpi->rc.next_key_frame_forced && cpi->rc.frames_to_key == 1) {
     if (seq_params->use_highbitdepth) {
-      cpi->ambient_err =
-          aom_highbd_get_y_sse(cpi->source, get_frame_new_buffer(cm));
+      cpi->ambient_err = aom_highbd_get_y_sse(cpi->source, &cm->cur_frame->buf);
     } else {
-      cpi->ambient_err = aom_get_y_sse(cpi->source, get_frame_new_buffer(cm));
+      cpi->ambient_err = aom_get_y_sse(cpi->source, &cm->cur_frame->buf);
     }
   }
 
@@ -5253,7 +5251,7 @@
     cpi->refresh_last_frame = 1;
   }
 
-  cm->frame_to_show = get_frame_new_buffer(cm);
+  cm->frame_to_show = &cm->cur_frame->buf;
   cm->frame_to_show->color_primaries = seq_params->color_primaries;
   cm->frame_to_show->transfer_characteristics =
       seq_params->transfer_characteristics;
@@ -5390,9 +5388,6 @@
     // keep track of the last coded dimensions
     cm->last_width = cm->width;
     cm->last_height = cm->height;
-
-    // reset to normal state now that we are done.
-    cm->last_show_frame = cm->show_frame;
   }
 
   return AOM_CODEC_OK;
@@ -6607,9 +6602,11 @@
     if (cm->new_fb_idx != INVALID_IDX) {
       --pool->frame_bufs[cm->new_fb_idx].ref_count;
     }
-    cm->new_fb_idx = get_free_fb(cm);
 
+    cm->cur_frame = NULL;
+    cm->new_fb_idx = get_free_fb(cm);
     if (cm->new_fb_idx == INVALID_IDX) return -1;
+    cm->cur_frame = &pool->frame_bufs[cm->new_fb_idx];
 
     // Clear down mmx registers
     aom_clear_system_state();
diff --git a/av1/encoder/firstpass.c b/av1/encoder/firstpass.c
index b68fdbb..5117c67 100644
--- a/av1/encoder/firstpass.c
+++ b/av1/encoder/firstpass.c
@@ -525,7 +525,7 @@
 
   YV12_BUFFER_CONFIG *const lst_yv12 = get_ref_frame_buffer(cpi, LAST_FRAME);
   YV12_BUFFER_CONFIG *gld_yv12 = get_ref_frame_buffer(cpi, GOLDEN_FRAME);
-  YV12_BUFFER_CONFIG *const new_yv12 = get_frame_new_buffer(cm);
+  YV12_BUFFER_CONFIG *const new_yv12 = &cm->cur_frame->buf;
   const YV12_BUFFER_CONFIG *first_ref_buf = lst_yv12;
   double intra_factor;
   double brightness_factor;
diff --git a/av1/encoder/mbgraph.c b/av1/encoder/mbgraph.c
index f0b537a..cc50458 100644
--- a/av1/encoder/mbgraph.c
+++ b/av1/encoder/mbgraph.c
@@ -179,8 +179,8 @@
   x->plane[0].src.buf = buf->y_buffer + mb_y_offset;
   x->plane[0].src.stride = buf->y_stride;
 
-  xd->plane[0].dst.buf = get_frame_new_buffer(cm)->y_buffer + mb_y_offset;
-  xd->plane[0].dst.stride = get_frame_new_buffer(cm)->y_stride;
+  xd->plane[0].dst.buf = cm->cur_frame->buf.y_buffer + mb_y_offset;
+  xd->plane[0].dst.stride = cm->cur_frame->buf.y_stride;
 
   // do intra 16x16 prediction
   intra_error = find_best_16x16_intra(cpi, &stats->ref[INTRA_FRAME].m.mode);
diff --git a/av1/encoder/rdopt.c b/av1/encoder/rdopt.c
index a963863..d06e66a 100644
--- a/av1/encoder/rdopt.c
+++ b/av1/encoder/rdopt.c
@@ -10711,8 +10711,8 @@
     av1_build_prediction_by_left_preds(cm, xd, mi_row, mi_col,
                                        args->left_pred_buf, dst_width2,
                                        dst_height2, args->left_pred_stride);
-    av1_setup_dst_planes(xd->plane, bsize, get_frame_new_buffer(cm), mi_row,
-                         mi_col, 0, num_planes);
+    av1_setup_dst_planes(xd->plane, bsize, &cm->cur_frame->buf, mi_row, mi_col,
+                         0, num_planes);
     calc_target_weighted_pred(
         cm, x, xd, mi_row, mi_col, args->above_pred_buf[0],
         args->above_pred_stride[0], args->left_pred_buf[0],
diff --git a/av1/encoder/reconinter_enc.c b/av1/encoder/reconinter_enc.c
index 87f5132..9652621 100644
--- a/av1/encoder/reconinter_enc.c
+++ b/av1/encoder/reconinter_enc.c
@@ -476,7 +476,7 @@
                                       dst_width1, dst_height1, dst_stride1);
   av1_build_prediction_by_left_preds(cm, xd, mi_row, mi_col, dst_buf2,
                                      dst_width2, dst_height2, dst_stride2);
-  av1_setup_dst_planes(xd->plane, xd->mi[0]->sb_type, get_frame_new_buffer(cm),
+  av1_setup_dst_planes(xd->plane, xd->mi[0]->sb_type, &cm->cur_frame->buf,
                        mi_row, mi_col, 0, num_planes);
   av1_build_obmc_inter_prediction(cm, xd, mi_row, mi_col, dst_buf1, dst_stride1,
                                   dst_buf2, dst_stride2);