Refactor av1_build_inter_predictor

Make av1_build_inter_predictor() independent of coding state. The
new function will take motion vector and buffer address as input
and produce the inter prediction.

Change-Id: I0580bd0c22e09d76ceb59093b1cd0ef314d1b93d
diff --git a/av1/common/blockd.h b/av1/common/blockd.h
index 1a0544b..4c57628 100644
--- a/av1/common/blockd.h
+++ b/av1/common/blockd.h
@@ -192,8 +192,11 @@
   int zero_rate;
 #if CONFIG_RD_DEBUG
   int txb_coeff_cost[MAX_MB_PLANE];
-  int txb_coeff_cost_map[MAX_MB_PLANE][TXB_COEFF_COST_MAP_SIZE]
-                        [TXB_COEFF_COST_MAP_SIZE];
+  // TODO(jingning): Temporary solution to silence stack over-size warning
+  // in handle_inter_mode. This should be fixed after rate-distortion
+  // optimization refactoring.
+  int16_t txb_coeff_cost_map[MAX_MB_PLANE][TXB_COEFF_COST_MAP_SIZE]
+                            [TXB_COEFF_COST_MAP_SIZE];
 #endif  // CONFIG_RD_DEBUG
 } RD_STATS;
 
diff --git a/av1/encoder/rd.h b/av1/encoder/rd.h
index 6080804..eaae19e 100644
--- a/av1/encoder/rd.h
+++ b/av1/encoder/rd.h
@@ -122,7 +122,7 @@
       int r, c;
       for (r = 0; r < TXB_COEFF_COST_MAP_SIZE; ++r)
         for (c = 0; c < TXB_COEFF_COST_MAP_SIZE; ++c)
-          rd_stats->txb_coeff_cost_map[plane][r][c] = INT_MAX;
+          rd_stats->txb_coeff_cost_map[plane][r][c] = INT16_MAX;
     }
   }
 #endif
diff --git a/av1/encoder/rdopt.c b/av1/encoder/rdopt.c
index 7ebef47..bafc308 100644
--- a/av1/encoder/rdopt.c
+++ b/av1/encoder/rdopt.c
@@ -6918,12 +6918,7 @@
   const int refs[2] = { mbmi->ref_frame[0], mbmi->ref_frame[1] };
   int_mv ref_mv[2];
   int ite, ref;
-  struct macroblockd_plane *const pd = &xd->plane[0];
-  const int p_col = ((mi_col * MI_SIZE) >> pd->subsampling_x);
-  const int p_row = ((mi_row * MI_SIZE) >> pd->subsampling_y);
 
-  ConvolveParams conv_params = get_conv_params(0, plane, xd->bd);
-  conv_params.use_dist_wtd_comp_avg = 0;
   WarpTypesAllowed warp_types[2];
   for (ref = 0; ref < 2; ++ref) {
     const WarpedMotionParams *const wm =
@@ -6933,6 +6928,19 @@
     warp_types[ref].local_warp_allowed = mbmi->motion_mode == WARPED_CAUSAL;
   }
 
+  // Get the prediction block from the 'other' reference frame.
+  const int_interpfilters interp_filters =
+      av1_broadcast_interp_filter(EIGHTTAP_REGULAR);
+
+  (void)warp_types;
+
+  InterPredParams inter_pred_params;
+
+  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,
+                        &cm->sf_identity, interp_filters);
+  inter_pred_params.conv_params = get_conv_params(0, 0, xd->bd);
+
   // Do joint motion search in compound mode to get more accurate mv.
   struct buf_2d backup_yv12[2][MAX_MB_PLANE];
   int last_besterr[2] = { INT_MAX, INT_MAX };
@@ -6996,18 +7004,12 @@
     ref_yv12[0] = xd->plane[plane].pre[0];
     ref_yv12[1] = xd->plane[plane].pre[1];
 
-    // Get the prediction block from the 'other' reference frame.
-    const int_interpfilters interp_filters =
-        av1_broadcast_interp_filter(EIGHTTAP_REGULAR);
-
     // Since we have scaled the reference frames to match the size of the
     // current frame we must use a unit scaling factor during mode selection.
     av1_build_inter_predictor(ref_yv12[!id].buf, ref_yv12[!id].stride,
                               second_pred, pw, &cur_mv[!id].as_mv,
-                              &cm->sf_identity, pw, ph, &conv_params,
-                              interp_filters, &warp_types[!id], p_col, p_row,
-                              plane, !id, MV_PRECISION_Q3, mi_col * MI_SIZE,
-                              mi_row * MI_SIZE, xd, cm->allow_warped_motion);
+                              mi_col * MI_SIZE, mi_row * MI_SIZE,
+                              &inter_pred_params);
 
     const int order_idx = id != 0;
     av1_dist_wtd_comp_weight_assign(
@@ -7550,17 +7552,23 @@
   av1_setup_scale_factors_for_frame(&sf, ref_yv12.width, ref_yv12.height,
                                     cm->width, cm->height);
 
-  ConvolveParams conv_params = get_conv_params(0, plane, xd->bd);
   WarpTypesAllowed warp_types;
   warp_types.global_warp_allowed = is_global;
   warp_types.local_warp_allowed = mbmi->motion_mode == WARPED_CAUSAL;
 
+  (void)warp_types;
+
+  InterPredParams inter_pred_params;
+
+  av1_init_inter_params(&inter_pred_params, pw, ph, p_col, p_row,
+                        pd->subsampling_x, pd->subsampling_y, xd->bd,
+                        is_cur_buf_hbd(xd), 0, &sf, mbmi->interp_filters);
+  inter_pred_params.conv_params = get_conv_params(0, 0, xd->bd);
+
   // Get the prediction block from the 'other' reference frame.
   av1_build_inter_predictor(ref_yv12.buf, ref_yv12.stride, second_pred, pw,
-                            other_mv, &sf, pw, ph, &conv_params,
-                            mbmi->interp_filters, &warp_types, p_col, p_row,
-                            plane, !ref_idx, MV_PRECISION_Q3, mi_col * MI_SIZE,
-                            mi_row * MI_SIZE, xd, cm->allow_warped_motion);
+                            other_mv, mi_col * MI_SIZE, mi_row * MI_SIZE,
+                            &inter_pred_params);
 
   av1_dist_wtd_comp_weight_assign(cm, mbmi, 0, &xd->jcp_param.fwd_offset,
                                   &xd->jcp_param.bck_offset,
@@ -10754,7 +10762,6 @@
                   (mbmi->ref_frame[1] < 0 ? 0 : mbmi->ref_frame[1]) };
   int rate_mv = 0;
   int64_t rd = INT64_MAX;
-
   // do first prediction into the destination buffer. Do the next
   // prediction into a temporary buffer. Then keep track of which one
   // of these currently holds the best predictor, and use the other
diff --git a/av1/encoder/reconinter_enc.c b/av1/encoder/reconinter_enc.c
index 979a667..b7a8300 100644
--- a/av1/encoder/reconinter_enc.c
+++ b/av1/encoder/reconinter_enc.c
@@ -277,47 +277,27 @@
   }
 }
 
-// TODO(sarahparker):
-// av1_build_inter_predictor should be combined with
-// av1_make_inter_predictor
 void av1_build_inter_predictor(const uint8_t *src, int src_stride, uint8_t *dst,
-                               int dst_stride, const MV *src_mv,
-                               const struct scale_factors *sf, int w, int h,
-                               ConvolveParams *conv_params,
-                               int_interpfilters interp_filters,
-                               const WarpTypesAllowed *warp_types, int p_col,
-                               int p_row, int plane, int ref,
-                               mv_precision precision, int x, int y,
-                               const MACROBLOCKD *xd, int can_use_previous) {
-  const struct macroblockd_plane *pd = &xd->plane[conv_params->plane];
-  const MV mv_q4 = { pd->subsampling_y ? src_mv->row : src_mv->row * 2,
-                     pd->subsampling_x ? src_mv->col : src_mv->col * 2 };
+                               int dst_stride, const MV *src_mv, int x, int y,
+                               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
+  };
 
-  (void)plane;
-  (void)precision;
-  (void)warp_types;
-  (void)ref;
-  (void)can_use_previous;
-
-  MV32 mv = av1_scale_mv(&mv_q4, x, y, sf);
+  MV32 mv = av1_scale_mv(&mv_q4, x, y, inter_pred_params->scale_factors);
   mv.col += SCALE_EXTRA_OFF;
   mv.row += SCALE_EXTRA_OFF;
 
-  InterPredParams inter_pred_params;
-  const SubpelParams subpel_params = { sf->x_step_q4, sf->y_step_q4,
-                                       mv.col & SCALE_SUBPEL_MASK,
-                                       mv.row & SCALE_SUBPEL_MASK };
+  const 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);
 
-  inter_pred_params.conv_params = *conv_params;
-
-  av1_init_inter_params(&inter_pred_params, w, h, p_col, p_row,
-                        pd->subsampling_x, pd->subsampling_y, xd->bd,
-                        is_cur_buf_hbd(xd), xd->mi[0]->use_intrabc, sf,
-                        interp_filters);
-
-  av1_make_inter_predictor(src, src_stride, dst, dst_stride, &inter_pred_params,
+  av1_make_inter_predictor(src, src_stride, dst, dst_stride, inter_pred_params,
                            &subpel_params);
 }
 
diff --git a/av1/encoder/reconinter_enc.h b/av1/encoder/reconinter_enc.h
index 6a374a7..a2c580ef 100644
--- a/av1/encoder/reconinter_enc.h
+++ b/av1/encoder/reconinter_enc.h
@@ -18,6 +18,7 @@
 #include "av1/common/onyxc_int.h"
 #include "av1/common/convolve.h"
 #include "av1/common/warped_motion.h"
+#include "av1/common/reconinter.h"
 
 #ifdef __cplusplus
 extern "C" {
@@ -29,14 +30,8 @@
                                    int plane_from, int plane_to);
 
 void av1_build_inter_predictor(const uint8_t *src, int src_stride, uint8_t *dst,
-                               int dst_stride, const MV *src_mv,
-                               const struct scale_factors *sf, int w, int h,
-                               ConvolveParams *conv_params,
-                               int_interpfilters interp_filters,
-                               const WarpTypesAllowed *warp_types, int p_col,
-                               int p_row, int plane, int ref,
-                               mv_precision precision, int x, int y,
-                               const MACROBLOCKD *xd, int can_use_previous);
+                               int dst_stride, const MV *src_mv, int x, int y,
+                               InterPredParams *inter_pred_params);
 
 // Detect if the block have sub-pixel level motion vectors
 // per component.
diff --git a/av1/encoder/temporal_filter.c b/av1/encoder/temporal_filter.c
index 83fc112..2307996 100644
--- a/av1/encoder/temporal_filter.c
+++ b/av1/encoder/temporal_filter.c
@@ -54,9 +54,8 @@
 static void temporal_filter_predictors_mb_c(
     MACROBLOCKD *xd, uint8_t *y_mb_ptr, uint8_t *u_mb_ptr, uint8_t *v_mb_ptr,
     int stride, int uv_block_width, int uv_block_height, int mv_row, int mv_col,
-    uint8_t *pred, struct scale_factors *scale, int x, int y,
-    int can_use_previous, int num_planes, MV *blk_mvs, int use_32x32) {
-  mv_precision mv_precision_uv;
+    uint8_t *pred, struct scale_factors *scale, int x, int y, int num_planes,
+    MV *blk_mvs, int use_32x32) {
   int uv_stride;
   const int_interpfilters interp_filters =
       av1_broadcast_interp_filter(MULTITAP_SHARP);
@@ -66,34 +65,36 @@
   const int ssx = (uv_block_width == (BW >> 1)) ? 1 : 0;
   if (ssx) {
     uv_stride = (stride + 1) >> 1;
-    mv_precision_uv = MV_PRECISION_Q4;
   } else {
     uv_stride = stride;
-    mv_precision_uv = MV_PRECISION_Q3;
   }
 
-  ConvolveParams conv_params = get_conv_params(0, 0, xd->bd);
+  InterPredParams inter_pred_params;
+
+  av1_init_inter_params(&inter_pred_params, BW, BH, x, y, 0, 0, xd->bd,
+                        is_cur_buf_hbd(xd), 0, scale, interp_filters);
+  inter_pred_params.conv_params = get_conv_params(0, 0, xd->bd);
+
   if (use_32x32) {
     assert(mv_row >= INT16_MIN && mv_row <= INT16_MAX && mv_col >= INT16_MIN &&
            mv_col <= INT16_MAX);
     const MV mv = { (int16_t)mv_row, (int16_t)mv_col };
 
-    av1_build_inter_predictor(y_mb_ptr, stride, &pred[0], BW, &mv, scale, BW,
-                              BH, &conv_params, interp_filters, &warp_types, x,
-                              y, 0, 0, MV_PRECISION_Q3, x, y, xd,
-                              can_use_previous);
-    if (num_planes > 1) {
-      conv_params = get_conv_params(0, 1, xd->bd);
-      av1_build_inter_predictor(
-          u_mb_ptr, uv_stride, &pred[BLK_PELS], uv_block_width, &mv, scale,
-          uv_block_width, uv_block_height, &conv_params, interp_filters,
-          &warp_types, x, y, 1, 0, mv_precision_uv, x, y, xd, can_use_previous);
+    av1_build_inter_predictor(y_mb_ptr, stride, &pred[0], BW, &mv, x, y,
+                              &inter_pred_params);
 
-      conv_params = get_conv_params(0, 2, xd->bd);
-      av1_build_inter_predictor(
-          v_mb_ptr, uv_stride, &pred[(BLK_PELS << 1)], uv_block_width, &mv,
-          scale, uv_block_width, uv_block_height, &conv_params, interp_filters,
-          &warp_types, x, y, 2, 0, mv_precision_uv, x, y, xd, can_use_previous);
+    if (num_planes > 1) {
+      av1_init_inter_params(&inter_pred_params, uv_block_width, uv_block_height,
+                            x, y, xd->plane[1].subsampling_x,
+                            xd->plane[1].subsampling_y, xd->bd,
+                            is_cur_buf_hbd(xd), 0, scale, interp_filters);
+      inter_pred_params.conv_params = get_conv_params(0, 1, xd->bd);
+      av1_build_inter_predictor(u_mb_ptr, uv_stride, &pred[BLK_PELS],
+                                uv_block_width, &mv, x, y, &inter_pred_params);
+
+      inter_pred_params.conv_params = get_conv_params(0, 2, xd->bd);
+      av1_build_inter_predictor(v_mb_ptr, uv_stride, &pred[(BLK_PELS << 1)],
+                                uv_block_width, &mv, x, y, &inter_pred_params);
     }
 
     return;
@@ -103,7 +104,9 @@
   // predictors.
   int i, j, k = 0, ys = (BH >> 1), xs = (BW >> 1);
   // Y predictor
-  conv_params = get_conv_params(0, 0, xd->bd);
+  av1_init_inter_params(&inter_pred_params, xs, ys, x, y, 0, 0, xd->bd,
+                        is_cur_buf_hbd(xd), 0, scale, 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];
@@ -111,9 +114,7 @@
       const int p_offset = i * BW + j;
 
       av1_build_inter_predictor(y_mb_ptr + y_offset, stride, &pred[p_offset],
-                                BW, &mv, scale, xs, ys, &conv_params,
-                                interp_filters, &warp_types, x, y, 0, 0,
-                                MV_PRECISION_Q3, x, y, xd, can_use_previous);
+                                BW, &mv, x, y, &inter_pred_params);
       k++;
     }
   }
@@ -130,19 +131,20 @@
         const int uv_offset = i * uv_stride + j;
         const int p_offset = i * uv_block_width + j;
 
-        conv_params = get_conv_params(0, 1, xd->bd);
+        av1_init_inter_params(&inter_pred_params, xs, ys, x, y,
+                              xd->plane[1].subsampling_x,
+                              xd->plane[1].subsampling_y, xd->bd,
+                              is_cur_buf_hbd(xd), 0, scale, interp_filters);
+        inter_pred_params.conv_params = get_conv_params(0, 1, xd->bd);
+
         av1_build_inter_predictor(u_mb_ptr + uv_offset, uv_stride,
                                   &pred[BLK_PELS + p_offset], uv_block_width,
-                                  &mv, scale, xs, ys, &conv_params,
-                                  interp_filters, &warp_types, x, y, 1, 0,
-                                  mv_precision_uv, x, y, xd, can_use_previous);
+                                  &mv, x, y, &inter_pred_params);
 
-        conv_params = get_conv_params(0, 2, xd->bd);
+        inter_pred_params.conv_params = get_conv_params(0, 1, xd->bd);
         av1_build_inter_predictor(
             v_mb_ptr + uv_offset, uv_stride, &pred[(BLK_PELS << 1) + p_offset],
-            uv_block_width, &mv, scale, xs, ys, &conv_params, interp_filters,
-            &warp_types, x, y, 2, 0, mv_precision_uv, x, y, xd,
-            can_use_previous);
+            uv_block_width, &mv, x, y, &inter_pred_params);
         k++;
       }
     }
@@ -1162,7 +1164,7 @@
               frames[frame]->y_stride, mb_uv_width, mb_uv_height,
               mbd->mi[0]->mv[0].as_mv.row, mbd->mi[0]->mv[0].as_mv.col,
               predictor, ref_scale_factors, mb_col * BW, mb_row * BH,
-              cm->allow_warped_motion, num_planes, blk_mvs, use_32x32);
+              num_planes, blk_mvs, use_32x32);
 
           // Apply the filter (YUV)
           if (frame == alt_ref_index) {
diff --git a/av1/encoder/tpl_model.c b/av1/encoder/tpl_model.c
index 025a215..6f342c7 100644
--- a/av1/encoder/tpl_model.c
+++ b/av1/encoder/tpl_model.c
@@ -240,14 +240,15 @@
     motion_estimation(cpi, x, src_mb_buffer, ref_mb, src_stride, ref_stride,
                       bsize, mi_row, mi_col);
 
-    ConvolveParams conv_params = get_conv_params(0, 0, xd->bd);
-    WarpTypesAllowed warp_types;
-    memset(&warp_types, 0, sizeof(WarpTypesAllowed));
+    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,
+                          sf, kernel);
+    inter_pred_params.conv_params = get_conv_params(0, 0, xd->bd);
 
-    av1_build_inter_predictor(
-        ref_mb, ref_stride, predictor, bw, &x->best_mv.as_mv, sf, bw, bh,
-        &conv_params, kernel, &warp_types, mi_col * MI_SIZE, mi_row * MI_SIZE,
-        0, 0, MV_PRECISION_Q3, mi_col * MI_SIZE, mi_row * MI_SIZE, xd, 0);
+    av1_build_inter_predictor(ref_mb, ref_stride, predictor, bw,
+                              &x->best_mv.as_mv, mi_col * MI_SIZE,
+                              mi_row * MI_SIZE, &inter_pred_params);
 
 #if CONFIG_AV1_HIGHBITDEPTH
     if (is_cur_buf_hbd(xd)) {
@@ -290,9 +291,11 @@
 
   // Final encode
   if (is_inter_mode(best_mode)) {
-    ConvolveParams conv_params = get_conv_params(0, 0, xd->bd);
-    WarpTypesAllowed warp_types;
-    memset(&warp_types, 0, sizeof(WarpTypesAllowed));
+    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,
+                          sf, kernel);
+    inter_pred_params.conv_params = get_conv_params(0, 0, xd->bd);
 
     const YV12_BUFFER_CONFIG *ref_frame_ptr = ref_frame[best_rf_idx];
     const int ref_mb_offset =
@@ -301,10 +304,8 @@
     int ref_stride = ref_frame_ptr->y_stride;
 
     av1_build_inter_predictor(ref_mb, ref_stride, dst_buffer, dst_buffer_stride,
-                              &best_mv.as_mv, sf, bw, bh, &conv_params, kernel,
-                              &warp_types, mi_col * MI_SIZE, mi_row * MI_SIZE,
-                              0, 0, MV_PRECISION_Q3, mi_col * MI_SIZE,
-                              mi_row * MI_SIZE, xd, 0);
+                              &best_mv.as_mv, mi_col * MI_SIZE,
+                              mi_row * MI_SIZE, &inter_pred_params);
   } else {
     av1_predict_intra_block(cm, xd, block_size_wide[bsize],
                             block_size_high[bsize], tx_size, best_mode, 0, 0,
@@ -966,9 +967,6 @@
   struct scale_factors sf;
   av1_setup_scale_factors_for_frame(&sf, ref->y_crop_width, ref->y_crop_height,
                                     src->y_crop_width, src->y_crop_height);
-  ConvolveParams conv_params = get_conv_params(0, 0, xd->bd);
-  WarpTypesAllowed warp_types;
-  memset(&warp_types, 0, sizeof(WarpTypesAllowed));
   const int_interpfilters kernel =
       av1_broadcast_interp_filter(EIGHTTAP_REGULAR);
   xd->above_mbmi = NULL;
@@ -1055,11 +1053,16 @@
                         ref->y_buffer + mb_y_offset_ref, src->y_stride,
                         ref->y_stride, bsize, mi_row, mi_col);
 
-      av1_build_inter_predictor(
-          ref->y_buffer + mb_y_offset_ref, ref->y_stride, predictor, bw,
-          &x->best_mv.as_mv, &sf, bw, bh, &conv_params, kernel, &warp_types,
-          mi_col * MI_SIZE, mi_row * MI_SIZE, 0, 0, MV_PRECISION_Q3,
-          mi_col * MI_SIZE, mi_row * MI_SIZE, xd, 0);
+      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, &sf, kernel);
+      inter_pred_params.conv_params = get_conv_params(0, 0, xd->bd);
+
+      av1_build_inter_predictor(ref->y_buffer + mb_y_offset_ref, ref->y_stride,
+                                predictor, bw, &x->best_mv.as_mv,
+                                mi_col * MI_SIZE, mi_row * MI_SIZE,
+                                &inter_pred_params);
       if (use_satd) {
 #if CONFIG_AV1_HIGHBITDEPTH
         if (is_cur_buf_hbd(xd)) {