Streamline motion vector to address mapping

Unify the motion vector to buffer address mapping for both scaled
and non-scaled reference frames.

Under rare cases, this might cause slight compression performance
change.

STATS_CHANGED

Change-Id: I209b1a7ce2367f042e3a29ea448759d8f357a939
diff --git a/av1/common/scale.c b/av1/common/scale.c
index 50d75cb..3b14c0a 100644
--- a/av1/common/scale.c
+++ b/av1/common/scale.c
@@ -37,7 +37,7 @@
 // Note: Expect val to be in q4 precision
 static int unscaled_value(int val, const struct scale_factors *sf) {
   (void)sf;
-  return val << SCALE_EXTRA_BITS;
+  return val * (1 << SCALE_EXTRA_BITS);
 }
 
 static int get_fixed_point_scale_factor(int other_size, int this_size) {
diff --git a/av1/encoder/rdopt.c b/av1/encoder/rdopt.c
index 0df6c44..b0e580e 100644
--- a/av1/encoder/rdopt.c
+++ b/av1/encoder/rdopt.c
@@ -6998,8 +6998,8 @@
     ref_yv12[0] = xd->plane[plane].pre[0];
     ref_yv12[1] = xd->plane[plane].pre[1];
 
-    av1_init_inter_params(&inter_pred_params, pw, ph, mi_col * MI_SIZE,
-                          mi_row * MI_SIZE, 0, 0, xd->bd, is_cur_buf_hbd(xd), 0,
+    av1_init_inter_params(&inter_pred_params, pw, ph, mi_row * MI_SIZE,
+                          mi_col * MI_SIZE, 0, 0, xd->bd, is_cur_buf_hbd(xd), 0,
                           &cm->sf_identity, &ref_yv12[!id], interp_filters);
     inter_pred_params.conv_params = get_conv_params(0, 0, xd->bd);
 
@@ -7546,7 +7546,7 @@
 
   InterPredParams inter_pred_params;
 
-  av1_init_inter_params(&inter_pred_params, pw, ph, p_col, p_row,
+  av1_init_inter_params(&inter_pred_params, pw, ph, p_row, p_col,
                         pd->subsampling_x, pd->subsampling_y, xd->bd,
                         is_cur_buf_hbd(xd), 0, &sf, &ref_yv12,
                         mbmi->interp_filters);
diff --git a/av1/encoder/reconinter_enc.c b/av1/encoder/reconinter_enc.c
index 06154f6..1c1d4f7 100644
--- a/av1/encoder/reconinter_enc.c
+++ b/av1/encoder/reconinter_enc.c
@@ -279,55 +279,40 @@
 }
 
 void av1_build_inter_predictor(const uint8_t *src, int src_stride, uint8_t *dst,
-                               int dst_stride, const MV *src_mv, int x, int y,
+                               int dst_stride, const MV *src_mv, int pix_col,
+                               int pix_row,
                                InterPredParams *inter_pred_params) {
-  const MV mv_q4 = {
-    inter_pred_params->subsampling_y ? src_mv->row : src_mv->row * 2,
-    inter_pred_params->subsampling_x ? src_mv->col : src_mv->col * 2
-  };
-
-  MV32 mv = av1_scale_mv(&mv_q4, x, y, inter_pred_params->scale_factors);
-  mv.col += SCALE_EXTRA_OFF;
-  mv.row += SCALE_EXTRA_OFF;
-
-  SubpelParams subpel_params = { inter_pred_params->scale_factors->x_step_q4,
-                                 inter_pred_params->scale_factors->y_step_q4,
-                                 mv.col & SCALE_SUBPEL_MASK,
-                                 mv.row & SCALE_SUBPEL_MASK };
-  src += (mv.row >> SCALE_SUBPEL_BITS) * src_stride +
-         (mv.col >> SCALE_SUBPEL_BITS);
-
+  SubpelParams subpel_params;
   const struct scale_factors *sf = inter_pred_params->scale_factors;
-  const int is_scaled = av1_is_scaled(sf);
-  // Re-compute the address when the reference frame is scaled.
-  if (is_scaled) {
-    struct buf_2d *pre_buf = &inter_pred_params->ref_frame_buf;
-    int ssx = inter_pred_params->subsampling_x;
-    int ssy = inter_pred_params->subsampling_y;
-    int orig_pos_y = (inter_pred_params->pix_row >> ssy) << SUBPEL_BITS;
-    orig_pos_y += src_mv->row * (1 << (1 - ssy));
-    int orig_pos_x = (inter_pred_params->pix_col >> ssx) << SUBPEL_BITS;
-    orig_pos_x += src_mv->col * (1 << (1 - ssx));
-    int pos_y = sf->scale_value_y(orig_pos_y, sf);
-    int pos_x = sf->scale_value_x(orig_pos_x, sf);
-    pos_x += SCALE_EXTRA_OFF;
-    pos_y += SCALE_EXTRA_OFF;
 
-    const int top = -AOM_LEFT_TOP_MARGIN_SCALED(ssy);
-    const int left = -AOM_LEFT_TOP_MARGIN_SCALED(ssx);
-    const int bottom = (pre_buf->height + AOM_INTERP_EXTEND)
-                       << SCALE_SUBPEL_BITS;
-    const int right = (pre_buf->width + AOM_INTERP_EXTEND) << SCALE_SUBPEL_BITS;
-    pos_y = clamp(pos_y, top, bottom);
-    pos_x = clamp(pos_x, left, right);
+  (void)pix_row;
+  (void)pix_col;
 
-    src = pre_buf->buf0 + (pos_y >> SCALE_SUBPEL_BITS) * pre_buf->stride +
-          (pos_x >> SCALE_SUBPEL_BITS);
-    subpel_params.subpel_x = pos_x & SCALE_SUBPEL_MASK;
-    subpel_params.subpel_y = pos_y & SCALE_SUBPEL_MASK;
-    subpel_params.xs = sf->x_step_q4;
-    subpel_params.ys = sf->y_step_q4;
-  }
+  struct buf_2d *pre_buf = &inter_pred_params->ref_frame_buf;
+  int ssx = inter_pred_params->subsampling_x;
+  int ssy = inter_pred_params->subsampling_y;
+  int orig_pos_y = inter_pred_params->pix_row << SUBPEL_BITS;
+  orig_pos_y += src_mv->row * (1 << (1 - ssy));
+  int orig_pos_x = inter_pred_params->pix_col << SUBPEL_BITS;
+  orig_pos_x += src_mv->col * (1 << (1 - ssx));
+  int pos_y = sf->scale_value_y(orig_pos_y, sf);
+  int pos_x = sf->scale_value_x(orig_pos_x, sf);
+  pos_x += SCALE_EXTRA_OFF;
+  pos_y += SCALE_EXTRA_OFF;
+
+  const int top = -AOM_LEFT_TOP_MARGIN_SCALED(ssy);
+  const int left = -AOM_LEFT_TOP_MARGIN_SCALED(ssx);
+  const int bottom = (pre_buf->height + AOM_INTERP_EXTEND) << SCALE_SUBPEL_BITS;
+  const int right = (pre_buf->width + AOM_INTERP_EXTEND) << SCALE_SUBPEL_BITS;
+  pos_y = clamp(pos_y, top, bottom);
+  pos_x = clamp(pos_x, left, right);
+
+  src = pre_buf->buf0 + (pos_y >> SCALE_SUBPEL_BITS) * pre_buf->stride +
+        (pos_x >> SCALE_SUBPEL_BITS);
+  subpel_params.subpel_x = pos_x & SCALE_SUBPEL_MASK;
+  subpel_params.subpel_y = pos_y & SCALE_SUBPEL_MASK;
+  subpel_params.xs = sf->x_step_q4;
+  subpel_params.ys = sf->y_step_q4;
 
   av1_make_inter_predictor(src, src_stride, dst, dst_stride, inter_pred_params,
                            &subpel_params);
@@ -369,10 +354,11 @@
     const struct buf_2d *const pre_buf = &pd->pre[0];
     const MV mv = above_mbmi->mv[0].as_mv;
 
-    av1_init_inter_params(&inter_pred_params, bw, bh, mi_y, mi_x,
-                          pd->subsampling_x, pd->subsampling_y, xd->bd,
-                          is_cur_buf_hbd(xd), 0, xd->block_ref_scale_factors[0],
-                          pre_buf, above_mbmi->interp_filters);
+    av1_init_inter_params(&inter_pred_params, bw, bh, mi_y >> pd->subsampling_y,
+                          mi_x >> pd->subsampling_x, pd->subsampling_x,
+                          pd->subsampling_y, xd->bd, is_cur_buf_hbd(xd), 0,
+                          xd->block_ref_scale_factors[0], pre_buf,
+                          above_mbmi->interp_filters);
     inter_pred_params.conv_params = get_conv_params(0, 0, xd->bd);
 
     av1_build_inter_predictor(pre_buf->buf, pre_buf->stride, pd->dst.buf,
@@ -502,10 +488,11 @@
 
     InterPredParams inter_pred_params;
 
-    av1_init_inter_params(
-        &inter_pred_params, bw, bh, mi_y, mi_x, pd->subsampling_x,
-        pd->subsampling_y, xd->bd, is_cur_buf_hbd(xd), 0,
-        xd->block_ref_scale_factors[ref], &pd->pre[ref], mi->interp_filters);
+    av1_init_inter_params(&inter_pred_params, bw, bh, mi_y >> pd->subsampling_y,
+                          mi_x >> pd->subsampling_x, pd->subsampling_x,
+                          pd->subsampling_y, xd->bd, is_cur_buf_hbd(xd), 0,
+                          xd->block_ref_scale_factors[ref], &pd->pre[ref],
+                          mi->interp_filters);
     inter_pred_params.conv_params = get_conv_params(0, plane, xd->bd);
     av1_init_warp_params(&inter_pred_params, &pd->pre[ref], &warp_types, ref,
                          xd, mi);
diff --git a/av1/encoder/temporal_filter.c b/av1/encoder/temporal_filter.c
index 0eee01b..b32239f 100644
--- a/av1/encoder/temporal_filter.c
+++ b/av1/encoder/temporal_filter.c
@@ -74,7 +74,7 @@
   struct buf_2d ref_buf_y = { NULL, ref_frame->y_buffer, ref_frame->y_width,
                               ref_frame->y_height, ref_frame->y_stride };
 
-  av1_init_inter_params(&inter_pred_params, BW, BH, x, y, 0, 0, xd->bd,
+  av1_init_inter_params(&inter_pred_params, BW, BH, y, x, 0, 0, xd->bd,
                         is_cur_buf_hbd(xd), 0, scale, &ref_buf_y,
                         interp_filters);
   inter_pred_params.conv_params = get_conv_params(0, 0, xd->bd);
@@ -93,7 +93,8 @@
                                    ref_frame->uv_stride };
 
       av1_init_inter_params(
-          &inter_pred_params, uv_block_width, uv_block_height, x, y,
+          &inter_pred_params, uv_block_width, uv_block_height,
+          y >> xd->plane[1].subsampling_y, x >> xd->plane[1].subsampling_x,
           xd->plane[1].subsampling_x, xd->plane[1].subsampling_y, xd->bd,
           is_cur_buf_hbd(xd), 0, scale, &ref_buf_uv, interp_filters);
       inter_pred_params.conv_params = get_conv_params(0, 1, xd->bd);
@@ -102,7 +103,8 @@
 
       ref_buf_uv.buf0 = ref_frame->v_buffer;
       av1_init_inter_params(
-          &inter_pred_params, uv_block_width, uv_block_height, x, y,
+          &inter_pred_params, uv_block_width, uv_block_height,
+          y >> xd->plane[1].subsampling_y, x >> xd->plane[1].subsampling_x,
           xd->plane[1].subsampling_x, xd->plane[1].subsampling_y, xd->bd,
           is_cur_buf_hbd(xd), 0, scale, &ref_buf_uv, interp_filters);
       inter_pred_params.conv_params = get_conv_params(0, 2, xd->bd);
@@ -117,16 +119,17 @@
   // predictors.
   int i, j, k = 0, ys = (BH >> 1), xs = (BW >> 1);
   // Y predictor
-  av1_init_inter_params(&inter_pred_params, xs, ys, x, y, 0, 0, xd->bd,
-                        is_cur_buf_hbd(xd), 0, scale, &ref_buf_y,
-                        interp_filters);
-  inter_pred_params.conv_params = get_conv_params(0, 0, xd->bd);
   for (i = 0; i < BH; i += ys) {
     for (j = 0; j < BW; j += xs) {
       const MV mv = blk_mvs[k];
       const int y_offset = i * stride + j;
       const int p_offset = i * BW + j;
 
+      av1_init_inter_params(&inter_pred_params, xs, ys, y + i, x + j, 0, 0,
+                            xd->bd, is_cur_buf_hbd(xd), 0, scale, &ref_buf_y,
+                            interp_filters);
+      inter_pred_params.conv_params = get_conv_params(0, 0, xd->bd);
+
       av1_build_inter_predictor(y_mb_ptr + y_offset, stride, &pred[p_offset],
                                 BW, &mv, x, y, &inter_pred_params);
       k++;
@@ -150,7 +153,8 @@
                                      ref_frame->uv_stride };
 
         av1_init_inter_params(
-            &inter_pred_params, xs, ys, x, y, xd->plane[1].subsampling_x,
+            &inter_pred_params, xs, ys, (y >> xd->plane[1].subsampling_y) + i,
+            (x >> xd->plane[1].subsampling_x) + j, xd->plane[1].subsampling_x,
             xd->plane[1].subsampling_y, xd->bd, is_cur_buf_hbd(xd), 0, scale,
             &ref_buf_uv, interp_filters);
         inter_pred_params.conv_params = get_conv_params(0, 1, xd->bd);
@@ -160,7 +164,8 @@
 
         ref_buf_uv.buf0 = ref_frame->v_buffer;
         av1_init_inter_params(
-            &inter_pred_params, xs, ys, x, y, xd->plane[1].subsampling_x,
+            &inter_pred_params, xs, ys, (y >> xd->plane[1].subsampling_y) + i,
+            (x >> xd->plane[1].subsampling_x) + j, xd->plane[1].subsampling_x,
             xd->plane[1].subsampling_y, xd->bd, is_cur_buf_hbd(xd), 0, scale,
             &ref_buf_uv, interp_filters);
 
diff --git a/av1/encoder/tpl_model.c b/av1/encoder/tpl_model.c
index 749ce1e..714e63f 100644
--- a/av1/encoder/tpl_model.c
+++ b/av1/encoder/tpl_model.c
@@ -266,8 +266,8 @@
                               ref_frame_ptr->y_width, ref_frame_ptr->y_height,
                               ref_frame_ptr->y_stride };
     InterPredParams inter_pred_params;
-    av1_init_inter_params(&inter_pred_params, bw, bh, mi_col * MI_SIZE,
-                          mi_row * MI_SIZE, 0, 0, xd->bd, is_cur_buf_hbd(xd), 0,
+    av1_init_inter_params(&inter_pred_params, bw, bh, mi_row * MI_SIZE,
+                          mi_col * MI_SIZE, 0, 0, xd->bd, is_cur_buf_hbd(xd), 0,
                           sf, &ref_buf, kernel);
     inter_pred_params.conv_params = get_conv_params(0, 0, xd->bd);
 
@@ -322,8 +322,8 @@
     struct buf_2d ref_buf = { NULL, ref_frame_ptr->y_buffer,
                               ref_frame_ptr->y_width, ref_frame_ptr->y_height,
                               ref_frame_ptr->y_stride };
-    av1_init_inter_params(&inter_pred_params, bw, bh, mi_col * MI_SIZE,
-                          mi_row * MI_SIZE, 0, 0, xd->bd, is_cur_buf_hbd(xd), 0,
+    av1_init_inter_params(&inter_pred_params, bw, bh, mi_row * MI_SIZE,
+                          mi_col * MI_SIZE, 0, 0, xd->bd, is_cur_buf_hbd(xd), 0,
                           sf, &ref_buf, kernel);
     inter_pred_params.conv_params = get_conv_params(0, 0, xd->bd);
 
@@ -1087,8 +1087,8 @@
       struct buf_2d ref_buf = { NULL, ref->y_buffer, ref->y_width,
                                 ref->y_height, ref->y_stride };
       InterPredParams inter_pred_params;
-      av1_init_inter_params(&inter_pred_params, bw, bh, mi_col * MI_SIZE,
-                            mi_row * MI_SIZE, 0, 0, xd->bd, is_cur_buf_hbd(xd),
+      av1_init_inter_params(&inter_pred_params, bw, bh, mi_row * MI_SIZE,
+                            mi_col * MI_SIZE, 0, 0, xd->bd, is_cur_buf_hbd(xd),
                             0, &sf, &ref_buf, kernel);
       inter_pred_params.conv_params = get_conv_params(0, 0, xd->bd);