Use ref_frame_map vice active_ref_idx on the encoder

This patch makes the encoder's use of ref_frame_map and active_ref_idx
consistent with the decoder. ref_frame_map[] maps a reference buffer
index to its actual location in the yv12_fb array, since many
references may share an underlying buffer. active_ref_idx[] mirrors
cpi->{lst,gld,alt}_fb_idx, holding the active references in each
slot.

This also fixes a bug in setup_buffer_inter() where the incorrect
reference was used to populate the scaling factors.

Change-Id: Id3728f6d77cffcd27c248903bf51f9c3e594287e
diff --git a/vp9/encoder/vp9_encodeframe.c b/vp9/encoder/vp9_encodeframe.c
index eaed1a9..c0fe5ac 100644
--- a/vp9/encoder/vp9_encodeframe.c
+++ b/vp9/encoder/vp9_encodeframe.c
@@ -1187,7 +1187,7 @@
 
   // Copy data over into macro block data structures.
   x->src = *cpi->Source;
-  xd->pre = cm->yv12_fb[cm->active_ref_idx[cpi->lst_fb_idx]];
+  xd->pre = cm->yv12_fb[cm->ref_frame_map[cpi->lst_fb_idx]];
   xd->dst = cm->yv12_fb[cm->new_fb_idx];
 
   // set up frame for intra coded blocks
@@ -2089,11 +2089,11 @@
     assert(cm->frame_type != KEY_FRAME);
 
     if (mbmi->ref_frame == LAST_FRAME)
-      ref_fb_idx = cpi->common.active_ref_idx[cpi->lst_fb_idx];
+      ref_fb_idx = cpi->common.ref_frame_map[cpi->lst_fb_idx];
     else if (mbmi->ref_frame == GOLDEN_FRAME)
-      ref_fb_idx = cpi->common.active_ref_idx[cpi->gld_fb_idx];
+      ref_fb_idx = cpi->common.ref_frame_map[cpi->gld_fb_idx];
     else
-      ref_fb_idx = cpi->common.active_ref_idx[cpi->alt_fb_idx];
+      ref_fb_idx = cpi->common.ref_frame_map[cpi->alt_fb_idx];
 
     setup_pred_block(&xd->pre,
                      &cpi->common.yv12_fb[ref_fb_idx],
@@ -2104,11 +2104,11 @@
       int second_ref_fb_idx;
 
       if (mbmi->second_ref_frame == LAST_FRAME)
-        second_ref_fb_idx = cpi->common.active_ref_idx[cpi->lst_fb_idx];
+        second_ref_fb_idx = cpi->common.ref_frame_map[cpi->lst_fb_idx];
       else if (mbmi->second_ref_frame == GOLDEN_FRAME)
-        second_ref_fb_idx = cpi->common.active_ref_idx[cpi->gld_fb_idx];
+        second_ref_fb_idx = cpi->common.ref_frame_map[cpi->gld_fb_idx];
       else
-        second_ref_fb_idx = cpi->common.active_ref_idx[cpi->alt_fb_idx];
+        second_ref_fb_idx = cpi->common.ref_frame_map[cpi->alt_fb_idx];
 
       setup_pred_block(&xd->second_pre,
                        &cpi->common.yv12_fb[second_ref_fb_idx],
@@ -2319,11 +2319,11 @@
     assert(cm->frame_type != KEY_FRAME);
 
     if (xd->mode_info_context->mbmi.ref_frame == LAST_FRAME)
-      ref_fb_idx = cpi->common.active_ref_idx[cpi->lst_fb_idx];
+      ref_fb_idx = cpi->common.ref_frame_map[cpi->lst_fb_idx];
     else if (xd->mode_info_context->mbmi.ref_frame == GOLDEN_FRAME)
-      ref_fb_idx = cpi->common.active_ref_idx[cpi->gld_fb_idx];
+      ref_fb_idx = cpi->common.ref_frame_map[cpi->gld_fb_idx];
     else
-      ref_fb_idx = cpi->common.active_ref_idx[cpi->alt_fb_idx];
+      ref_fb_idx = cpi->common.ref_frame_map[cpi->alt_fb_idx];
 
     setup_pred_block(&xd->pre,
                      &cpi->common.yv12_fb[ref_fb_idx],
@@ -2334,11 +2334,11 @@
       int second_ref_fb_idx;
 
       if (xd->mode_info_context->mbmi.second_ref_frame == LAST_FRAME)
-        second_ref_fb_idx = cpi->common.active_ref_idx[cpi->lst_fb_idx];
+        second_ref_fb_idx = cpi->common.ref_frame_map[cpi->lst_fb_idx];
       else if (xd->mode_info_context->mbmi.second_ref_frame == GOLDEN_FRAME)
-        second_ref_fb_idx = cpi->common.active_ref_idx[cpi->gld_fb_idx];
+        second_ref_fb_idx = cpi->common.ref_frame_map[cpi->gld_fb_idx];
       else
-        second_ref_fb_idx = cpi->common.active_ref_idx[cpi->alt_fb_idx];
+        second_ref_fb_idx = cpi->common.ref_frame_map[cpi->alt_fb_idx];
 
       setup_pred_block(&xd->second_pre,
                        &cpi->common.yv12_fb[second_ref_fb_idx],
@@ -2548,11 +2548,11 @@
     assert(cm->frame_type != KEY_FRAME);
 
     if (xd->mode_info_context->mbmi.ref_frame == LAST_FRAME)
-      ref_fb_idx = cpi->common.active_ref_idx[cpi->lst_fb_idx];
+      ref_fb_idx = cpi->common.ref_frame_map[cpi->lst_fb_idx];
     else if (xd->mode_info_context->mbmi.ref_frame == GOLDEN_FRAME)
-      ref_fb_idx = cpi->common.active_ref_idx[cpi->gld_fb_idx];
+      ref_fb_idx = cpi->common.ref_frame_map[cpi->gld_fb_idx];
     else
-      ref_fb_idx = cpi->common.active_ref_idx[cpi->alt_fb_idx];
+      ref_fb_idx = cpi->common.ref_frame_map[cpi->alt_fb_idx];
 
     setup_pred_block(&xd->pre,
                      &cpi->common.yv12_fb[ref_fb_idx],
@@ -2563,11 +2563,11 @@
       int second_ref_fb_idx;
 
       if (xd->mode_info_context->mbmi.second_ref_frame == LAST_FRAME)
-        second_ref_fb_idx = cpi->common.active_ref_idx[cpi->lst_fb_idx];
+        second_ref_fb_idx = cpi->common.ref_frame_map[cpi->lst_fb_idx];
       else if (xd->mode_info_context->mbmi.second_ref_frame == GOLDEN_FRAME)
-        second_ref_fb_idx = cpi->common.active_ref_idx[cpi->gld_fb_idx];
+        second_ref_fb_idx = cpi->common.ref_frame_map[cpi->gld_fb_idx];
       else
-        second_ref_fb_idx = cpi->common.active_ref_idx[cpi->alt_fb_idx];
+        second_ref_fb_idx = cpi->common.ref_frame_map[cpi->alt_fb_idx];
 
       setup_pred_block(&xd->second_pre,
                        &cpi->common.yv12_fb[second_ref_fb_idx],
diff --git a/vp9/encoder/vp9_firstpass.c b/vp9/encoder/vp9_firstpass.c
index 05a0f6f..4d0a299 100644
--- a/vp9/encoder/vp9_firstpass.c
+++ b/vp9/encoder/vp9_firstpass.c
@@ -436,10 +436,10 @@
 
   int recon_yoffset, recon_uvoffset;
   YV12_BUFFER_CONFIG *lst_yv12 =
-      &cm->yv12_fb[cm->active_ref_idx[cpi->lst_fb_idx]];
+      &cm->yv12_fb[cm->ref_frame_map[cpi->lst_fb_idx]];
   YV12_BUFFER_CONFIG *new_yv12 = &cm->yv12_fb[cm->new_fb_idx];
   YV12_BUFFER_CONFIG *gld_yv12 =
-      &cm->yv12_fb[cm->active_ref_idx[cpi->gld_fb_idx]];
+      &cm->yv12_fb[cm->ref_frame_map[cpi->gld_fb_idx]];
   int recon_y_stride = lst_yv12->y_stride;
   int recon_uv_stride = lst_yv12->uv_stride;
   int64_t intra_error = 0;
diff --git a/vp9/encoder/vp9_mbgraph.c b/vp9/encoder/vp9_mbgraph.c
index bc06c94..d6644c2 100644
--- a/vp9/encoder/vp9_mbgraph.c
+++ b/vp9/encoder/vp9_mbgraph.c
@@ -445,7 +445,7 @@
   VP9_COMMON *const cm = &cpi->common;
   int i, n_frames = vp9_lookahead_depth(cpi->lookahead);
   YV12_BUFFER_CONFIG *golden_ref =
-      &cm->yv12_fb[cm->active_ref_idx[cpi->gld_fb_idx]];
+      &cm->yv12_fb[cm->ref_frame_map[cpi->gld_fb_idx]];
 
   // we need to look ahead beyond where the ARF transitions into
   // being a GF - so exit if we don't look ahead beyond that
diff --git a/vp9/encoder/vp9_onyx_if.c b/vp9/encoder/vp9_onyx_if.c
index 45ab6cd..ced6edd 100644
--- a/vp9/encoder/vp9_onyx_if.c
+++ b/vp9/encoder/vp9_onyx_if.c
@@ -833,7 +833,7 @@
   }
 
   {
-    int y_stride = cm->yv12_fb[cm->active_ref_idx[cpi->lst_fb_idx]].y_stride;
+    int y_stride = cm->yv12_fb[cm->ref_frame_map[cpi->lst_fb_idx]].y_stride;
 
     if (cpi->sf.search_method == NSTEP) {
       vp9_init3smotion_compensation(&cpi->mb, y_stride);
@@ -1754,7 +1754,7 @@
 #endif
       if (cpi->b_calculate_psnr) {
         YV12_BUFFER_CONFIG *lst_yv12 =
-            &cpi->common.yv12_fb[cpi->common.active_ref_idx[cpi->lst_fb_idx]];
+            &cpi->common.yv12_fb[cpi->common.ref_frame_map[cpi->lst_fb_idx]];
         double samples = 3.0 / 2 * cpi->count * lst_yv12->y_width * lst_yv12->y_height;
         double total_psnr = vp9_mse2psnr(samples, 255.0, cpi->total_sq_error);
         double total_psnr2 = vp9_mse2psnr(samples, 255.0, cpi->total_sq_error2);
@@ -2099,11 +2099,11 @@
   int ref_fb_idx;
 
   if (ref_frame_flag == VP9_LAST_FLAG)
-    ref_fb_idx = cm->active_ref_idx[cpi->lst_fb_idx];
+    ref_fb_idx = cm->ref_frame_map[cpi->lst_fb_idx];
   else if (ref_frame_flag == VP9_GOLD_FLAG)
-    ref_fb_idx = cm->active_ref_idx[cpi->gld_fb_idx];
+    ref_fb_idx = cm->ref_frame_map[cpi->gld_fb_idx];
   else if (ref_frame_flag == VP9_ALT_FLAG)
-    ref_fb_idx = cm->active_ref_idx[cpi->alt_fb_idx];
+    ref_fb_idx = cm->ref_frame_map[cpi->alt_fb_idx];
   else
     return -1;
 
@@ -2120,11 +2120,11 @@
   int ref_fb_idx;
 
   if (ref_frame_flag == VP9_LAST_FLAG)
-    ref_fb_idx = cm->active_ref_idx[cpi->lst_fb_idx];
+    ref_fb_idx = cm->ref_frame_map[cpi->lst_fb_idx];
   else if (ref_frame_flag == VP9_GOLD_FLAG)
-    ref_fb_idx = cm->active_ref_idx[cpi->gld_fb_idx];
+    ref_fb_idx = cm->ref_frame_map[cpi->gld_fb_idx];
   else if (ref_frame_flag == VP9_ALT_FLAG)
-    ref_fb_idx = cm->active_ref_idx[cpi->alt_fb_idx];
+    ref_fb_idx = cm->ref_frame_map[cpi->alt_fb_idx];
   else
     return -1;
 
@@ -2480,9 +2480,9 @@
   // If any buffer copy / swapping is signaled it should be done here.
   if (cm->frame_type == KEY_FRAME) {
     ref_cnt_fb(cm->fb_idx_ref_cnt,
-               &cm->active_ref_idx[cpi->gld_fb_idx], cm->new_fb_idx);
+               &cm->ref_frame_map[cpi->gld_fb_idx], cm->new_fb_idx);
     ref_cnt_fb(cm->fb_idx_ref_cnt,
-               &cm->active_ref_idx[cpi->alt_fb_idx], cm->new_fb_idx);
+               &cm->ref_frame_map[cpi->alt_fb_idx], cm->new_fb_idx);
   } else if (cpi->refresh_golden_frame && !cpi->refresh_alt_ref_frame) {
     /* Preserve the previously existing golden frame and update the frame in
      * the alt ref slot instead. This is highly specific to the current use of
@@ -2496,7 +2496,7 @@
     int tmp;
 
     ref_cnt_fb(cm->fb_idx_ref_cnt,
-               &cm->active_ref_idx[cpi->alt_fb_idx], cm->new_fb_idx);
+               &cm->ref_frame_map[cpi->alt_fb_idx], cm->new_fb_idx);
 
     tmp = cpi->alt_fb_idx;
     cpi->alt_fb_idx = cpi->gld_fb_idx;
@@ -2504,18 +2504,18 @@
   } else { /* For non key/golden frames */
     if (cpi->refresh_alt_ref_frame) {
       ref_cnt_fb(cm->fb_idx_ref_cnt,
-                 &cm->active_ref_idx[cpi->alt_fb_idx], cm->new_fb_idx);
+                 &cm->ref_frame_map[cpi->alt_fb_idx], cm->new_fb_idx);
     }
 
     if (cpi->refresh_golden_frame) {
       ref_cnt_fb(cm->fb_idx_ref_cnt,
-                 &cm->active_ref_idx[cpi->gld_fb_idx], cm->new_fb_idx);
+                 &cm->ref_frame_map[cpi->gld_fb_idx], cm->new_fb_idx);
     }
   }
 
   if (cpi->refresh_last_frame) {
     ref_cnt_fb(cm->fb_idx_ref_cnt,
-               &cm->active_ref_idx[cpi->lst_fb_idx], cm->new_fb_idx);
+               &cm->ref_frame_map[cpi->lst_fb_idx], cm->new_fb_idx);
   }
 }
 
@@ -2604,7 +2604,7 @@
   int i;
 
   for (i = 0; i < 3; i++) {
-    YV12_BUFFER_CONFIG *ref = &cm->yv12_fb[cm->active_ref_idx[i]];
+    YV12_BUFFER_CONFIG *ref = &cm->yv12_fb[cm->ref_frame_map[i]];
 
     if (ref->y_width != cm->Width || ref->y_height != cm->Height) {
       int new_fb = get_free_fb(cm);
@@ -2616,8 +2616,8 @@
       scale_and_extend_frame(ref, &cm->yv12_fb[new_fb]);
       cpi->scaled_ref_idx[i] = new_fb;
     } else {
-      cpi->scaled_ref_idx[i] = cm->active_ref_idx[i];
-      cm->fb_idx_ref_cnt[cm->active_ref_idx[i]]++;
+      cpi->scaled_ref_idx[i] = cm->ref_frame_map[i];
+      cm->fb_idx_ref_cnt[cm->ref_frame_map[i]]++;
     }
   }
 }
@@ -3644,8 +3644,8 @@
     FILE *recon_file;
     sprintf(filename, "enc%04d.yuv", (int) cm->current_video_frame);
     recon_file = fopen(filename, "wb");
-    fwrite(cm->yv12_fb[cm->active_ref_idx[cpi->lst_fb_idx]].buffer_alloc,
-           cm->yv12_fb[cm->active_ref_idx[cpi->lst_fb_idx]].frame_size,
+    fwrite(cm->yv12_fb[cm->ref_frame_map[cpi->lst_fb_idx]].buffer_alloc,
+           cm->yv12_fb[cm->ref_frame_map[cpi->lst_fb_idx]].frame_size,
            1, recon_file);
     fclose(recon_file);
   }
@@ -3867,6 +3867,11 @@
   cm->fb_idx_ref_cnt[cm->new_fb_idx]--;
   cm->new_fb_idx = get_free_fb(cm);
 
+  /* Get the mapping of L/G/A to the reference buffer pool */
+  cm->active_ref_idx[0] = cm->ref_frame_map[cpi->lst_fb_idx];
+  cm->active_ref_idx[1] = cm->ref_frame_map[cpi->gld_fb_idx];
+  cm->active_ref_idx[2] = cm->ref_frame_map[cpi->alt_fb_idx];
+
   /* Reset the frame pointers to the current frame size */
   vp8_yv12_realloc_frame_buffer(&cm->yv12_fb[cm->new_fb_idx],
                                 cm->mb_cols * 16, cm->mb_rows * 16,
diff --git a/vp9/encoder/vp9_rdopt.c b/vp9/encoder/vp9_rdopt.c
index bc075d9..4f84300 100644
--- a/vp9/encoder/vp9_rdopt.c
+++ b/vp9/encoder/vp9_rdopt.c
@@ -3112,7 +3112,7 @@
                                YV12_BUFFER_CONFIG yv12_mb[4],
                                struct scale_factors scale[MAX_REF_FRAMES]) {
   VP9_COMMON *cm = &cpi->common;
-  YV12_BUFFER_CONFIG *yv12 = &cm->yv12_fb[cpi->common.active_ref_idx[idx]];
+  YV12_BUFFER_CONFIG *yv12 = &cm->yv12_fb[cpi->common.ref_frame_map[idx]];
   MACROBLOCKD *const xd = &x->e_mbd;
   MB_MODE_INFO *const mbmi = &xd->mode_info_context->mbmi;
   int use_prev_in_find_mv_refs, use_prev_in_find_best_ref;
@@ -4084,7 +4084,7 @@
         fb = cpi->alt_fb_idx;
       }
 
-      if (cpi->scaled_ref_idx[fb] != cm->active_ref_idx[fb])
+      if (cpi->scaled_ref_idx[fb] != cm->ref_frame_map[fb])
         scaled_ref_frame = &cm->yv12_fb[cpi->scaled_ref_idx[fb]];
     }
 
@@ -5177,7 +5177,7 @@
         fb = cpi->alt_fb_idx;
       }
 
-      if (cpi->scaled_ref_idx[fb] != cm->active_ref_idx[fb])
+      if (cpi->scaled_ref_idx[fb] != cm->ref_frame_map[fb])
         scaled_ref_frame = &cm->yv12_fb[cpi->scaled_ref_idx[fb]];
 
 #if CONFIG_COMP_INTERINTRA_PRED