Unify motion estimation process

Unify the motion estimation process used in first and second pass
encoding paths.

Change-Id: I486c3be695fb8ce7c7a204767868a22cd735f65e
diff --git a/av1/encoder/firstpass.c b/av1/encoder/firstpass.c
index 42c28fa3..590523e 100644
--- a/av1/encoder/firstpass.c
+++ b/av1/encoder/firstpass.c
@@ -218,62 +218,28 @@
                                                 const MV *ref_mv, MV *best_mv,
                                                 int *best_motion_err) {
   MACROBLOCKD *const xd = &x->e_mbd;
-  MV tmp_mv = kZeroMv;
   MV ref_mv_full = { ref_mv->row >> 3, ref_mv->col >> 3 };
-  int num00, tmp_err, n;
+  int tmp_err;
   const BLOCK_SIZE bsize = xd->mi[0]->sb_type;
   aom_variance_fn_ptr_t v_fn_ptr = cpi->fn_ptr[bsize];
   const int new_mv_mode_penalty = NEW_MV_MODE_PENALTY;
-
-  int step_param = 3;
-  int further_steps = (MAX_MVSEARCH_STEPS - 1) - step_param;
   const int sr = get_search_range(cpi);
-  step_param += sr;
-  further_steps -= sr;
+  int step_param = 3 + sr;
+  int cost_list[5];
 
-  // Override the default variance function to use MSE.
-  v_fn_ptr.vf = get_block_variance_fn(bsize);
-#if CONFIG_AV1_HIGHBITDEPTH
-  if (is_cur_buf_hbd(xd)) {
-    v_fn_ptr.vf = highbd_get_block_variance_fn(bsize, xd->bd);
-  }
-#endif
-  // Center the initial step/diamond search on best mv.
-  tmp_err = av1_diamond_search_sad_c(
-      x, &cpi->ss_cfg[SS_CFG_SRC], &ref_mv_full, &tmp_mv, step_param,
-      x->sadperbit16, &num00, &v_fn_ptr, ref_mv, NULL, NULL, 0, 0);
+  tmp_err = av1_full_pixel_search(
+      cpi, x, bsize, &ref_mv_full, step_param, 0, NSTEP, 0, x->sadperbit16,
+      cond_cost_list(cpi, cost_list), ref_mv, INT_MAX, 0,
+      (MI_SIZE * xd->mi_col), (MI_SIZE * xd->mi_row), 0,
+      &cpi->ss_cfg[SS_CFG_SRC], 0);
+
   if (tmp_err < INT_MAX)
-    tmp_err = av1_get_mvpred_var(x, &tmp_mv, ref_mv, &v_fn_ptr, 1);
-  if (tmp_err < INT_MAX - new_mv_mode_penalty) tmp_err += new_mv_mode_penalty;
+    tmp_err = av1_get_mvpred_var(x, &x->best_mv.as_mv, ref_mv, &v_fn_ptr, 0) +
+              new_mv_mode_penalty;
 
   if (tmp_err < *best_motion_err) {
     *best_motion_err = tmp_err;
-    *best_mv = tmp_mv;
-  }
-
-  // Carry out further step/diamond searches as necessary.
-  n = num00;
-  num00 = 0;
-
-  while (n < further_steps) {
-    ++n;
-
-    if (num00) {
-      --num00;
-    } else {
-      tmp_err = av1_diamond_search_sad_c(
-          x, &cpi->ss_cfg[SS_CFG_SRC], &ref_mv_full, &tmp_mv, step_param + n,
-          x->sadperbit16, &num00, &v_fn_ptr, ref_mv, NULL, NULL, 0, 0);
-      if (tmp_err < INT_MAX)
-        tmp_err = av1_get_mvpred_var(x, &tmp_mv, ref_mv, &v_fn_ptr, 1);
-      if (tmp_err < INT_MAX - new_mv_mode_penalty)
-        tmp_err += new_mv_mode_penalty;
-
-      if (tmp_err < *best_motion_err) {
-        *best_motion_err = tmp_err;
-        *best_mv = tmp_mv;
-      }
-    }
+    *best_mv = x->best_mv.as_mv;
   }
 }
 
diff --git a/av1/encoder/mcomp.c b/av1/encoder/mcomp.c
index 8776747..5ac02a8 100644
--- a/av1/encoder/mcomp.c
+++ b/av1/encoder/mcomp.c
@@ -1416,18 +1416,20 @@
 
 int av1_get_mvpred_var(const MACROBLOCK *x, const MV *best_mv,
                        const MV *center_mv, const aom_variance_fn_ptr_t *vfp,
-                       int use_mvcost) {
+                       int use_var) {
   const MACROBLOCKD *const xd = &x->e_mbd;
   const struct buf_2d *const what = &x->plane[0].src;
   const struct buf_2d *const in_what = &xd->plane[0].pre[0];
   const MV mv = { best_mv->row * 8, best_mv->col * 8 };
-  unsigned int unused;
+  unsigned int sse, var;
 
-  return vfp->vf(what->buf, what->stride, get_buf_from_mv(in_what, best_mv),
-                 in_what->stride, &unused) +
-         (use_mvcost ? mv_err_cost(&mv, center_mv, x->nmv_vec_cost,
-                                   x->mv_cost_stack, x->errorperbit)
-                     : 0);
+  var = vfp->vf(what->buf, what->stride, get_buf_from_mv(in_what, best_mv),
+                in_what->stride, &sse);
+
+  if (!use_var) var = sse;
+
+  return var + mv_err_cost(&mv, center_mv, x->nmv_vec_cost, x->mv_cost_stack,
+                           x->errorperbit);
 }
 
 int av1_get_mvpred_av_var(const MACROBLOCK *x, const MV *best_mv,
@@ -1798,7 +1800,8 @@
               point as the best match, we will do a final 1-away diamond
               refining search  */
 static int full_pixel_diamond(MACROBLOCK *x, MV *mvp_full, int step_param,
-                              int sadpb, int further_steps, int *cost_list,
+                              int use_var, int sadpb, int further_steps,
+                              int *cost_list,
                               const aom_variance_fn_ptr_t *fn_ptr,
                               const MV *ref_mv, const search_site_config *cfg,
                               uint8_t *second_pred, uint8_t *mask,
@@ -1809,7 +1812,7 @@
                                          sadpb, &n, fn_ptr, ref_mv, second_pred,
                                          mask, mask_stride, inv_mask);
   if (bestsme < INT_MAX)
-    bestsme = av1_get_mvpred_var(x, &temp_mv, ref_mv, fn_ptr, 1);
+    bestsme = av1_get_mvpred_var(x, &temp_mv, ref_mv, fn_ptr, use_var);
   x->best_mv.as_mv = temp_mv;
 
   // If there won't be more n-step search, check to see if refining search is
@@ -1824,7 +1827,7 @@
           x, cfg, mvp_full, &temp_mv, step_param + n, sadpb, &num00, fn_ptr,
           ref_mv, second_pred, mask, mask_stride, inv_mask);
       if (thissme < INT_MAX)
-        thissme = av1_get_mvpred_var(x, &temp_mv, ref_mv, fn_ptr, 1);
+        thissme = av1_get_mvpred_var(x, &temp_mv, ref_mv, fn_ptr, use_var);
 
       if (thissme < bestsme) {
         bestsme = thissme;
@@ -2218,7 +2221,7 @@
 }
 
 int av1_full_pixel_search(const AV1_COMP *cpi, MACROBLOCK *x, BLOCK_SIZE bsize,
-                          MV *mvp_full, int step_param, int method,
+                          MV *mvp_full, int step_param, int use_var, int method,
                           int run_mesh_search, int error_per_bit,
                           int *cost_list, const MV *ref_mv, int var_max, int rd,
                           int x_pos, int y_pos, int intra,
@@ -2261,7 +2264,7 @@
                           fn_ptr, 1, ref_mv);
       break;
     case NSTEP:
-      var = full_pixel_diamond(x, mvp_full, step_param, error_per_bit,
+      var = full_pixel_diamond(x, mvp_full, step_param, use_var, error_per_bit,
                                cfg->ss_count - 1 - step_param, cost_list,
                                fn_ptr, ref_mv, cfg, NULL, NULL, 0, 0);
       break;
@@ -2274,7 +2277,7 @@
   const int max_exhaustive_pct = use_intrabc_mesh_pattern
                                      ? sf->intrabc_max_exhaustive_pct
                                      : sf->max_exhaustive_pct;
-  if (!run_mesh_search && method == NSTEP &&
+  if (!run_mesh_search && method == NSTEP && use_var &&
       is_exhaustive_allowed(cpi, x, max_exhaustive_pct)) {
     int exhuastive_thr = sf->exhaustive_searches_thresh;
     exhuastive_thr >>=
@@ -2993,9 +2996,9 @@
   // This overwrites the mv_limits so we will need to restore it later.
   av1_set_mv_search_range(&x->mv_limits, &ref_mv);
   var = av1_full_pixel_search(
-      cpi, x, bsize, &ref_mv_full, step_param, search_methods, do_mesh_search,
-      sadpb, cond_cost_list(cpi, cost_list), &ref_mv, INT_MAX, 1,
-      mi_col * MI_SIZE, mi_row * MI_SIZE, 0, &cpi->ss_cfg[SS_CFG_SRC], 0);
+      cpi, x, bsize, &ref_mv_full, step_param, 1, search_methods,
+      do_mesh_search, sadpb, cond_cost_list(cpi, cost_list), &ref_mv, INT_MAX,
+      1, mi_col * MI_SIZE, mi_row * MI_SIZE, 0, &cpi->ss_cfg[SS_CFG_SRC], 0);
   // Restore
   x->mv_limits = tmp_mv_limits;
 
diff --git a/av1/encoder/mcomp.h b/av1/encoder/mcomp.h
index c19ecfa..0104b8d 100644
--- a/av1/encoder/mcomp.h
+++ b/av1/encoder/mcomp.h
@@ -66,7 +66,7 @@
 // Utility to compute variance + MV rate cost for a given MV
 int av1_get_mvpred_var(const MACROBLOCK *x, const MV *best_mv,
                        const MV *center_mv, const aom_variance_fn_ptr_t *vfp,
-                       int use_mvcost);
+                       int use_var);
 int av1_get_mvpred_av_var(const MACROBLOCK *x, const MV *best_mv,
                           const MV *center_mv, const uint8_t *second_pred,
                           const aom_variance_fn_ptr_t *vfp,
@@ -135,9 +135,9 @@
 
 int av1_full_pixel_search(const struct AV1_COMP *cpi, MACROBLOCK *x,
                           BLOCK_SIZE bsize, MV *mvp_full, int step_param,
-                          int method, int run_mesh_search, int error_per_bit,
-                          int *cost_list, const MV *ref_mv, int var_max, int rd,
-                          int x_pos, int y_pos, int intra,
+                          int use_var, int method, int run_mesh_search,
+                          int error_per_bit, int *cost_list, const MV *ref_mv,
+                          int var_max, int rd, int x_pos, int y_pos, int intra,
                           const search_site_config *cfg,
                           int use_intrabc_mesh_pattern);
 
diff --git a/av1/encoder/nonrd_pickmode.c b/av1/encoder/nonrd_pickmode.c
index a796a84..39598da 100644
--- a/av1/encoder/nonrd_pickmode.c
+++ b/av1/encoder/nonrd_pickmode.c
@@ -160,8 +160,8 @@
     center_mv = tmp_mv->as_mv;
 
   av1_full_pixel_search(
-      cpi, x, bsize, &mvp_full, step_param, cpi->sf.mv.search_method, 0, sadpb,
-      cond_cost_list(cpi, cost_list), &center_mv, INT_MAX, 0,
+      cpi, x, bsize, &mvp_full, step_param, 1, cpi->sf.mv.search_method, 0,
+      sadpb, cond_cost_list(cpi, cost_list), &center_mv, INT_MAX, 0,
       (MI_SIZE * mi_col), (MI_SIZE * mi_row), 0, &cpi->ss_cfg[SS_CFG_SRC], 0);
 
   x->mv_limits = tmp_mv_limits;
diff --git a/av1/encoder/rdopt.c b/av1/encoder/rdopt.c
index 8cb7520..9b24f40 100644
--- a/av1/encoder/rdopt.c
+++ b/av1/encoder/rdopt.c
@@ -7793,7 +7793,7 @@
   switch (mbmi->motion_mode) {
     case SIMPLE_TRANSLATION:
       bestsme = av1_full_pixel_search(
-          cpi, x, bsize, &mvp_full, step_param, cpi->sf.mv.search_method, 0,
+          cpi, x, bsize, &mvp_full, step_param, 1, cpi->sf.mv.search_method, 0,
           sadpb, cond_cost_list(cpi, cost_list), &ref_mv, INT_MAX, 1,
           (MI_SIZE * mi_col), (MI_SIZE * mi_row), 0, &cpi->ss_cfg[SS_CFG_SRC],
           0);
@@ -11628,7 +11628,7 @@
     const int sadpb = x->sadperbit16;
     int cost_list[5];
     const int bestsme = av1_full_pixel_search(
-        cpi, x, bsize, &mvp_full, step_param, cpi->sf.mv.search_method, 0,
+        cpi, x, bsize, &mvp_full, step_param, 1, cpi->sf.mv.search_method, 0,
         sadpb, cond_cost_list(cpi, cost_list), &dv_ref.as_mv, INT_MAX, 1,
         (MI_SIZE * mi_col), (MI_SIZE * mi_row), 1,
         &cpi->ss_cfg[SS_CFG_LOOKAHEAD], 1);
diff --git a/av1/encoder/temporal_filter.c b/av1/encoder/temporal_filter.c
index 493c621..7b8f8b3 100644
--- a/av1/encoder/temporal_filter.c
+++ b/av1/encoder/temporal_filter.c
@@ -915,9 +915,10 @@
   // av1_full_pixel_search() parameters: best_ref_mv1_full is the start mv, and
   // ref_mv is for mv rate calculation. The search result is stored in
   // x->best_mv.
-  av1_full_pixel_search(cpi, x, TF_BLOCK, &best_ref_mv1_full, step_param, NSTEP,
-                        1, sadpb, cond_cost_list(cpi, cost_list), &ref_mv, 0, 0,
-                        x_pos, y_pos, 0, &cpi->ss_cfg[SS_CFG_LOOKAHEAD], 0);
+  av1_full_pixel_search(cpi, x, TF_BLOCK, &best_ref_mv1_full, step_param, 1,
+                        NSTEP, 1, sadpb, cond_cost_list(cpi, cost_list),
+                        &ref_mv, 0, 0, x_pos, y_pos, 0,
+                        &cpi->ss_cfg[SS_CFG_LOOKAHEAD], 0);
   x->mv_limits = tmp_mv_limits;
 
   // Ignore mv costing by sending NULL pointer instead of cost array
@@ -972,7 +973,7 @@
 
       av1_set_mv_search_range(&x->mv_limits, &ref_mv);
       av1_full_pixel_search(cpi, x, TF_SUB_BLOCK, &best_ref_mv1_full,
-                            step_param, NSTEP, 1, sadpb,
+                            step_param, 1, NSTEP, 1, sadpb,
                             cond_cost_list(cpi, cost_list), &ref_mv, 0, 0,
                             x_pos, y_pos, 0, &cpi->ss_cfg[SS_CFG_LOOKAHEAD], 0);
       x->mv_limits = tmp_mv_limits;
diff --git a/av1/encoder/tpl_model.c b/av1/encoder/tpl_model.c
index 3dde8cc..da66fcc 100644
--- a/av1/encoder/tpl_model.c
+++ b/av1/encoder/tpl_model.c
@@ -139,7 +139,7 @@
   av1_set_mv_search_range(&x->mv_limits, &best_ref_mv1);
 
   av1_init3smotion_compensation(&ss_cfg, stride_ref);
-  av1_full_pixel_search(cpi, x, bsize, &best_ref_mv1_full, step_param,
+  av1_full_pixel_search(cpi, x, bsize, &best_ref_mv1_full, step_param, 1,
                         search_method, 0, sadpb, cond_cost_list(cpi, cost_list),
                         &best_ref_mv1, INT_MAX, 0, (MI_SIZE * mi_col),
                         (MI_SIZE * mi_row), 0, &ss_cfg, 0);