Revert 92652ac7 and commit new fix for tip mismatch
diff --git a/av1/common/mv.h b/av1/common/mv.h
index cdb09ec..22e407b 100644
--- a/av1/common/mv.h
+++ b/av1/common/mv.h
@@ -348,12 +348,6 @@
   mv->row = clamp(mv->row, mv_limits->row_min, mv_limits->row_max);
 }
 
-static INLINE int av1_is_subpelmv_in_range(const SubpelMvLimits *mv_limits,
-                                           MV mv) {
-  return (mv.col >= mv_limits->col_min) && (mv.col <= mv_limits->col_max) &&
-         (mv.row >= mv_limits->row_min) && (mv.row <= mv_limits->row_max);
-}
-
 #ifdef __cplusplus
 }  // extern "C"
 #endif
diff --git a/av1/common/tip.h b/av1/common/tip.h
index 02268c0..6c87e1e 100644
--- a/av1/common/tip.h
+++ b/av1/common/tip.h
@@ -18,7 +18,6 @@
 #endif
 
 #include "av1/common/av1_common_int.h"
-#include "av1/common/mv.h"
 #include "av1/common/mvref_common.h"
 #include "av1/common/reconinter.h"
 
@@ -142,68 +141,6 @@
   return clamped_mv;
 }
 
-// Set TIP mv fullpel range constraint
-static INLINE void av1_set_tip_mv_range(
-    FullMvLimits *mv_limits, const MACROBLOCKD *const xd, BLOCK_SIZE bsize,
-    const CommonModeInfoParams *const mi_params) {
-  const int mi_row = xd->mi_row;
-  const int mi_col = xd->mi_col;
-  const int mi_width = mi_size_wide[bsize];
-  const int mi_height = mi_size_high[bsize];
-  const int mi_rows = mi_params->mi_rows;
-  const int mi_cols = mi_params->mi_cols;
-  const int tmvp_mv = (TIP_MV_SEARCH_RANGE << TMVP_MI_SZ_LOG2);
-
-  int col_min = -tmvp_mv;
-  int row_min = -tmvp_mv;
-  int col_max = tmvp_mv;
-  int row_max = tmvp_mv;
-
-  mv_limits->col_min = AOMMAX(col_min, -(mi_col * MI_SIZE));
-  mv_limits->col_max = AOMMIN(col_max, (mi_cols - mi_col - mi_width) * MI_SIZE);
-  mv_limits->row_min = AOMMAX(row_min, -(mi_row * MI_SIZE));
-  mv_limits->row_max =
-      AOMMIN(row_max, (mi_rows - mi_row - mi_height) * MI_SIZE);
-}
-
-// Set TIP mv subpel range constraint
-static INLINE void av1_set_tip_subpel_mv_range(
-    SubpelMvLimits *subpel_limits, const MACROBLOCKD *const xd,
-    BLOCK_SIZE bsize, const CommonModeInfoParams *const mi_params) {
-  const int mi_row = xd->mi_row;
-  const int mi_col = xd->mi_col;
-  const int mi_width = mi_size_wide[bsize];
-  const int mi_height = mi_size_high[bsize];
-  const int mi_rows = mi_params->mi_rows;
-  const int mi_cols = mi_params->mi_cols;
-  const int tmvp_mv = GET_MV_SUBPEL(TIP_MV_SEARCH_RANGE << TMVP_MI_SZ_LOG2);
-
-  const int col_min = -tmvp_mv;
-  const int row_min = -tmvp_mv;
-  const int col_max = tmvp_mv;
-  const int row_max = tmvp_mv;
-
-  subpel_limits->col_min = AOMMAX(col_min, GET_MV_SUBPEL(-(mi_col * MI_SIZE)));
-  subpel_limits->col_max =
-      AOMMIN(col_max, GET_MV_SUBPEL((mi_cols - mi_col - mi_width) * MI_SIZE));
-  subpel_limits->row_min = AOMMAX(row_min, GET_MV_SUBPEL(-(mi_row * MI_SIZE)));
-  subpel_limits->row_max =
-      AOMMIN(row_max, GET_MV_SUBPEL((mi_rows - mi_row - mi_height) * MI_SIZE));
-}
-
-// Clamp refmv for TIP nearmv mode
-static INLINE int tip_clamp_and_check_mv(int_mv *out_mv, int_mv in_mv,
-                                         BLOCK_SIZE bsize, const AV1_COMMON *cm,
-                                         const MACROBLOCKD *const xd) {
-  *out_mv = in_mv;
-  lower_mv_precision(&out_mv->as_mv, cm->features.allow_high_precision_mv,
-                     cm->features.cur_frame_force_integer_mv);
-  SubpelMvLimits subpel_mv_limits;
-  av1_set_tip_subpel_mv_range(&subpel_mv_limits, xd, bsize, &cm->mi_params);
-  clamp_mv(&out_mv->as_mv, &subpel_mv_limits);
-  return av1_is_subpelmv_in_range(&subpel_mv_limits, out_mv->as_mv);
-}
-
 #ifdef __cplusplus
 }  // extern "C"
 #endif
diff --git a/av1/decoder/decodeframe.c b/av1/decoder/decodeframe.c
index 62ad1fa..0e73e4b 100644
--- a/av1/decoder/decodeframe.c
+++ b/av1/decoder/decodeframe.c
@@ -848,50 +848,73 @@
       ROUND_POWER_OF_TWO(cm->mi_params.mi_rows, TMVP_SHIFT_BITS);
   const int mvs_cols =
       ROUND_POWER_OF_TWO(cm->mi_params.mi_cols, TMVP_SHIFT_BITS);
+  const int extra_pixel = 1 << TMVP_MI_SZ_LOG2;
 
   const int mi_row = xd->mi_row;
   const int mi_col = xd->mi_col;
   const int x_mis = xd->width;
   const int y_mis = xd->height;
 
+  // define the block start and end pixel locations
+  FULLPEL_MV start_mv = get_fullmv_from_mv(mv);
   const int bw = (x_mis << MI_SIZE_LOG2);
   const int bh = (y_mis << MI_SIZE_LOG2);
-  int pixel_row = (mi_row << MI_SIZE_LOG2);
-  int pixel_col = (mi_col << MI_SIZE_LOG2);
+  int start_pixel_row = (mi_row << MI_SIZE_LOG2) + start_mv.row;
+  int start_pixel_col = (mi_col << MI_SIZE_LOG2) + start_mv.col;
+  int end_pixel_row = start_pixel_row + bh;
+  int end_pixel_col = start_pixel_col + bw;
 
-  FULLPEL_MV start_mv = get_fullmv_from_mv(mv);
-
-  pixel_row += start_mv.row;
-  pixel_col += start_mv.col;
-  pixel_row = AOMMAX(0, pixel_row);
-  pixel_col = AOMMAX(0, pixel_col);
-  int end_pixel_row = pixel_row + bh;
-  int end_pixel_col = pixel_col + bw;
-
-  int tpl_start_row = pixel_row >> TMVP_MI_SZ_LOG2;
-  int tpl_end_row = (end_pixel_row + TMVP_MI_SIZE - 1) >> TMVP_MI_SZ_LOG2;
-  int tpl_start_col = pixel_col >> TMVP_MI_SZ_LOG2;
-  int tpl_end_col = (end_pixel_col + TMVP_MI_SIZE - 1) >> TMVP_MI_SZ_LOG2;
-
-  const int extra_order = 1;
+  // extend for handling interpolation
   if (mv->row != 0) {
-    tpl_start_row = AOMMAX(0, tpl_start_row - extra_order);
-    tpl_end_row = AOMMIN(mvs_rows, tpl_end_row + extra_order);
+    start_pixel_row -= extra_pixel;
+    end_pixel_row += extra_pixel;
   }
 
   if (mv->col != 0) {
-    tpl_start_col = AOMMAX(0, tpl_start_col - extra_order);
-    tpl_end_col = AOMMIN(mvs_cols, tpl_end_col + extra_order);
+    start_pixel_col -= extra_pixel;
+    end_pixel_col += extra_pixel;
   }
 
+  // clamp block start and end locations to make sure the block is in the frame
+  start_pixel_row = AOMMAX(0, start_pixel_row);
+  start_pixel_col = AOMMAX(0, start_pixel_col);
+  end_pixel_row = AOMMAX(0, end_pixel_row);
+  end_pixel_col = AOMMAX(0, end_pixel_col);
+  start_pixel_row = AOMMIN(cm->mi_params.mi_rows * MI_SIZE, start_pixel_row);
+  start_pixel_col = AOMMIN(cm->mi_params.mi_cols * MI_SIZE, start_pixel_col);
+  end_pixel_row = AOMMIN(cm->mi_params.mi_rows * MI_SIZE, end_pixel_row);
+  end_pixel_col = AOMMIN(cm->mi_params.mi_cols * MI_SIZE, end_pixel_col);
+
+  // convert the pixel block location to MV field grid location
+  int tpl_start_row = start_pixel_row >> TMVP_MI_SZ_LOG2;
+  int tpl_end_row = (end_pixel_row + TMVP_MI_SIZE - 1) >> TMVP_MI_SZ_LOG2;
+  int tpl_start_col = start_pixel_col >> TMVP_MI_SZ_LOG2;
+  int tpl_end_col = (end_pixel_col + TMVP_MI_SIZE - 1) >> TMVP_MI_SZ_LOG2;
+
+  // handling the boundary case when start and end locations are the same
+  if (tpl_start_row == tpl_end_row) {
+    if (tpl_start_row > 0) {
+      tpl_start_row -= 1;
+    } else {
+      tpl_end_row += 1;
+    }
+  }
+
+  if (tpl_start_col == tpl_end_col) {
+    if (tpl_start_col > 0) {
+      tpl_start_col -= 1;
+    } else {
+      tpl_end_col += 1;
+    }
+  }
+
+  // handle SIMD alignment for the chroma case
   tpl_start_row = (tpl_start_row >> 1) << 1;
   tpl_start_col = (tpl_start_col >> 1) << 1;
   tpl_end_row = ((tpl_end_row + 1) >> 1) << 1;
   tpl_end_col = ((tpl_end_col + 1) >> 1) << 1;
   tpl_end_row = AOMMIN(mvs_rows, tpl_end_row);
   tpl_end_col = AOMMIN(mvs_cols, tpl_end_col);
-  assert(tpl_start_row != tpl_end_row);
-  assert(tpl_start_col != tpl_end_col);
 
   av1_setup_tip_on_the_fly(cm, xd, tpl_start_row, tpl_start_col, tpl_end_row,
                            tpl_end_col, mvs_cols, mc_buf, conv_dst,
diff --git a/av1/decoder/decodemv.c b/av1/decoder/decodemv.c
index 914fa89..fcddc28 100644
--- a/av1/decoder/decodemv.c
+++ b/av1/decoder/decodemv.c
@@ -22,9 +22,6 @@
 #include "av1/common/reconinter.h"
 #include "av1/common/reconintra.h"
 #include "av1/common/seg_common.h"
-#if CONFIG_TIP
-#include "av1/common/tip.h"
-#endif  // CONFIG_TIP
 #include "av1/common/warped_motion.h"
 
 #include "av1/decoder/decodeframe.h"
@@ -1730,15 +1727,7 @@
       break;
     }
     case NEARMV: {
-#if CONFIG_TIP
-      int_mv this_mv = ref_mv[0];
-      if (is_tip_ref_frame(ref_frame[0])) {
-        tip_clamp_and_check_mv(&this_mv, this_mv, bsize, cm, xd);
-      }
-      mv[0].as_int = this_mv.as_int;
-#else
-      mv[0].as_int = near_mv[0].as_int;
-#endif  // CONFIG_TIP
+      mv[0].as_int = ref_mv[0].as_int;
       break;
     }
     case GLOBALMV: {
diff --git a/av1/encoder/mcomp.c b/av1/encoder/mcomp.c
index 7b5fcaa..140b115 100644
--- a/av1/encoder/mcomp.c
+++ b/av1/encoder/mcomp.c
@@ -26,9 +26,6 @@
 #include "av1/common/filter.h"
 #include "av1/common/mvref_common.h"
 #include "av1/common/reconinter.h"
-#if CONFIG_TIP
-#include "av1/common/tip.h"
-#endif  // CONFIG_TIP
 
 #include "av1/encoder/encoder.h"
 #include "av1/encoder/encodemv.h"
@@ -149,8 +146,7 @@
   ms_params->mv_limits = x->mv_limits;
 #if CONFIG_TIP
   if (is_tip_ref_frame(mbmi->ref_frame[0])) {
-    av1_set_tip_mv_range(&ms_params->mv_limits, &x->e_mbd, bsize,
-                         &cpi->common.mi_params);
+    av1_set_tip_mv_search_range(&ms_params->mv_limits);
   } else {
 #endif  // CONFIG_TIP
     av1_set_mv_search_range(&ms_params->mv_limits, ref_mv);
@@ -186,8 +182,7 @@
 
 #if CONFIG_TIP
   if (is_tip_ref_frame(mbmi->ref_frame[0])) {
-    av1_set_tip_subpel_mv_range(&ms_params->mv_limits, &x->e_mbd, bsize,
-                                &cm->mi_params);
+    av1_set_tip_subpel_mv_search_range(&ms_params->mv_limits, &x->mv_limits);
   } else {
 #endif  // CONFIG_TIP
     av1_set_subpel_mv_search_range(&ms_params->mv_limits, &x->mv_limits,
@@ -244,6 +239,23 @@
   if (mv_limits->row_max > row_max) mv_limits->row_max = row_max;
 }
 
+#if CONFIG_TIP
+void av1_set_tip_mv_search_range(FullMvLimits *mv_limits) {
+  const int tmvp_mv = (TIP_MV_SEARCH_RANGE << TMVP_MI_SZ_LOG2);
+  const int col_min = -tmvp_mv;
+  const int row_min = -tmvp_mv;
+  const int col_max = tmvp_mv;
+  const int row_max = tmvp_mv;
+
+  // Get intersection of UMV window and valid MV window to reduce # of checks
+  // in diamond search.
+  if (mv_limits->col_min < col_min) mv_limits->col_min = col_min;
+  if (mv_limits->col_max > col_max) mv_limits->col_max = col_max;
+  if (mv_limits->row_min < row_min) mv_limits->row_min = row_min;
+  if (mv_limits->row_max > row_max) mv_limits->row_max = row_max;
+}
+#endif  // CONFIG_TIP
+
 int av1_init_search_range(int size) {
   int sr = 0;
   // Minimum search size no matter what the passed in value.
@@ -2453,8 +2465,7 @@
   SubpelMvLimits subpel_mv_limits;
 #if CONFIG_TIP
   if (is_tip_ref_frame(mi->ref_frame[0])) {
-    av1_set_tip_subpel_mv_range(&subpel_mv_limits, &x->e_mbd, bsize,
-                                &cpi->common.mi_params);
+    av1_set_tip_subpel_mv_search_range(&subpel_mv_limits, &x->mv_limits);
   } else {
 #endif  // CONFIG_TIP
     av1_set_subpel_mv_search_range(&subpel_mv_limits, &x->mv_limits, ref_mv);
diff --git a/av1/encoder/mcomp.h b/av1/encoder/mcomp.h
index 6be8e9b..4671d92 100644
--- a/av1/encoder/mcomp.h
+++ b/av1/encoder/mcomp.h
@@ -294,6 +294,10 @@
 
 void av1_set_mv_search_range(FullMvLimits *mv_limits, const MV *mv);
 
+#if CONFIG_TIP
+void av1_set_tip_mv_search_range(FullMvLimits *mv_limits);
+#endif  // CONFIG_TIP
+
 int av1_init_search_range(int size);
 
 unsigned int av1_int_pro_motion_estimation(const struct AV1_COMP *cpi,
@@ -434,6 +438,24 @@
   subpel_limits->row_max = AOMMIN(MV_UPP - 1, maxr);
 }
 
+#if CONFIG_TIP
+static INLINE void av1_set_tip_subpel_mv_search_range(
+    SubpelMvLimits *subpel_limits, const FullMvLimits *mv_limits) {
+  const int tmvp_mv = GET_MV_SUBPEL(TIP_MV_SEARCH_RANGE << TMVP_MI_SZ_LOG2);
+
+  subpel_limits->col_min = AOMMAX(GET_MV_SUBPEL(mv_limits->col_min), -tmvp_mv);
+  subpel_limits->col_max = AOMMIN(GET_MV_SUBPEL(mv_limits->col_max), tmvp_mv);
+  subpel_limits->row_min = AOMMAX(GET_MV_SUBPEL(mv_limits->row_min), -tmvp_mv);
+  subpel_limits->row_max = AOMMIN(GET_MV_SUBPEL(mv_limits->row_max), tmvp_mv);
+}
+#endif  // CONFIG_TIP
+
+static INLINE int av1_is_subpelmv_in_range(const SubpelMvLimits *mv_limits,
+                                           MV mv) {
+  return (mv.col >= mv_limits->col_min) && (mv.col <= mv_limits->col_max) &&
+         (mv.row >= mv_limits->row_min) && (mv.row <= mv_limits->row_max);
+}
+
 #if CONFIG_BVP_IMPROVEMENT
 // Returns the cost of using the current mv during the motion search
 int av1_get_mv_err_cost(const MV *mv, const MV_COST_PARAMS *mv_cost_params);
diff --git a/av1/encoder/rdopt.c b/av1/encoder/rdopt.c
index 79e3668..de24562 100644
--- a/av1/encoder/rdopt.c
+++ b/av1/encoder/rdopt.c
@@ -40,9 +40,6 @@
 #include "av1/common/reconintra.h"
 #include "av1/common/scan.h"
 #include "av1/common/seg_common.h"
-#if CONFIG_TIP
-#include "av1/common/tip.h"
-#endif  // CONFIG_TIP
 #include "av1/common/txb_common.h"
 #include "av1/common/warped_motion.h"
 
@@ -2125,9 +2122,6 @@
 // This function update the non-new mv for the current prediction mode
 static INLINE int build_cur_mv(int_mv *cur_mv, PREDICTION_MODE this_mode,
                                const AV1_COMMON *cm, const MACROBLOCK *x,
-#if CONFIG_TIP
-                               BLOCK_SIZE bsize,
-#endif  // CONFIG_TIP
                                int skip_repeated_ref_mv) {
   const MACROBLOCKD *xd = &x->e_mbd;
   const MB_MODE_INFO *mbmi = xd->mi[0];
@@ -2149,16 +2143,7 @@
                    : x->mbmi_ext->ref_mv_stack[ref_frame_type][mbmi->ref_mv_idx]
                          .comp_mv;
     } else {
-#if CONFIG_TIP
-      if (is_tip_ref_frame(mbmi->ref_frame[0])) {
-        ret &=
-            tip_clamp_and_check_mv(cur_mv + i, this_mv, bsize, cm, &x->e_mbd);
-      } else {
-#endif  // CONFIG_TIP
-        ret &= clamp_and_check_mv(cur_mv + i, this_mv, cm, x);
-#if CONFIG_TIP
-      }
-#endif  // CONFIG_TIP
+      ret &= clamp_and_check_mv(cur_mv + i, this_mv, cm, x);
     }
   }
   return ret;
@@ -2341,11 +2326,7 @@
   mode_info[ref_mv_idx].drl_cost = drl_cost;
 
   int_mv cur_mv[2];
-  if (!build_cur_mv(cur_mv, mbmi->mode, cm, x,
-#if CONFIG_TIP
-                    bsize,
-#endif  // CONFIG_TIP
-                    0)) {
+  if (!build_cur_mv(cur_mv, mbmi->mode, cm, x, 0)) {
     return INT64_MAX;
   }
   assert(have_nearmv_in_inter_mode(mbmi->mode));
@@ -3195,11 +3176,7 @@
       int skip_repeated_ref_mv =
           is_comp_pred ? 0 : cpi->sf.inter_sf.skip_repeated_ref_mv;
       // Generate the current mv according to the prediction mode
-      if (!build_cur_mv(cur_mv, this_mode, cm, x,
-#if CONFIG_TIP
-                        bsize,
-#endif  // CONFIG_TIP
-                        skip_repeated_ref_mv)) {
+      if (!build_cur_mv(cur_mv, this_mode, cm, x, skip_repeated_ref_mv)) {
         continue;
       }
 
@@ -4089,11 +4066,7 @@
   assert(mbmi->mode == NEAR_NEARMV);
 #endif
   assert(mbmi->ref_mv_idx == 0);
-  if (!build_cur_mv(mbmi->mv, this_mode, cm, x,
-#if CONFIG_TIP
-                    bsize,
-#endif  // CONFIG_TIP
-                    0)) {
+  if (!build_cur_mv(mbmi->mv, this_mode, cm, x, 0)) {
     assert(av1_check_newmv_joint_nonzero(cm, x));
     return;
   }
@@ -4147,11 +4120,7 @@
   for (int ref_mv_idx = 0; ref_mv_idx < ref_set; ref_mv_idx++) {
     mbmi->ref_mv_idx = ref_mv_idx;
 
-    if (!build_cur_mv(mbmi->mv, this_mode, cm, x,
-#if CONFIG_TIP
-                      bsize,
-#endif  // CONFIG_TIP
-                      0)) {
+    if (!build_cur_mv(mbmi->mv, this_mode, cm, x, 0)) {
       assert(av1_check_newmv_joint_nonzero(cm, x));
       continue;
     }
@@ -4345,11 +4314,7 @@
   assert(this_mode == NEAR_NEARMV);
   assert(mbmi->mode == NEAR_NEARMV);
   assert(mbmi->ref_mv_idx == 0);
-  if (!build_cur_mv(mbmi->mv, this_mode, cm, x,
-#if CONFIG_TIP
-                    bsize,
-#endif  // CONFIG_TIP
-                    0)) {
+  if (!build_cur_mv(mbmi->mv, this_mode, cm, x, 0)) {
     assert(av1_check_newmv_joint_nonzero(cm, x));
     return;
   }