Consolidate use_interp_filter code

Consolidated and cleaned up use_interp_filter related code.

Change-Id: I40b028c46ce93eb419b28ce40c4985ac3078ef51
diff --git a/av1/encoder/rdopt.c b/av1/encoder/rdopt.c
index 10b1377..2effddb 100644
--- a/av1/encoder/rdopt.c
+++ b/av1/encoder/rdopt.c
@@ -8940,38 +8940,27 @@
   }
 }
 
-// check if there is saved result match with this search
-static INLINE int is_interp_filter_exact_match(
-    const INTERPOLATION_FILTER_STATS *st, MB_MODE_INFO *const mi) {
-  for (int i = 0; i < 2; ++i) {
-    if ((st->ref_frames[i] != mi->ref_frame[i]) ||
-        (st->mv[i].as_int != mi->mv[i].as_int)) {
-      return 0;
-    }
-  }
-  if (has_second_ref(mi) && st->comp_type != mi->interinter_comp.type) return 0;
-  if (has_second_ref(mi) && st->compound_idx != mi->compound_idx) return 0;
-  return 1;
-}
-
 // return mv_diff
 static INLINE int is_interp_filter_good_match(
-    const INTERPOLATION_FILTER_STATS *st, MB_MODE_INFO *const mi) {
+    const INTERPOLATION_FILTER_STATS *st, MB_MODE_INFO *const mi,
+    int skip_level) {
   const int is_comp = has_second_ref(mi);
   int i;
 
   for (i = 0; i < 1 + is_comp; ++i) {
-    if (st->ref_frames[i] != mi->ref_frame[i]) return -1;
+    if (st->ref_frames[i] != mi->ref_frame[i]) return INT_MAX;
   }
 
-  const int thr = is_comp ? 7 : 3;
+  if (skip_level == 1 && is_comp) {
+    if (st->comp_type != mi->interinter_comp.type) return INT_MAX;
+    if (st->compound_idx != mi->compound_idx) return INT_MAX;
+  }
+
   int mv_diff = 0;
   for (i = 0; i < 1 + is_comp; ++i) {
     mv_diff += abs(st->mv[i].as_mv.row - mi->mv[i].as_mv.row) +
                abs(st->mv[i].as_mv.col - mi->mv[i].as_mv.col);
   }
-  if (mv_diff > thr) return -1;
-
   return mv_diff;
 }
 
@@ -9024,35 +9013,31 @@
 static INLINE int find_interp_filter_in_stats(
     MB_MODE_INFO *const mbmi, INTERPOLATION_FILTER_STATS *interp_filter_stats,
     int interp_filter_stats_idx, int skip_level) {
-  if (skip_level < 2) {
-    // Find exact match.
-    // TODO(yunqing): Use a small threshold instead. Combine this with finding
-    // good enough match.
-    for (int j = 0; j < interp_filter_stats_idx; ++j) {
-      const INTERPOLATION_FILTER_STATS *st = &interp_filter_stats[j];
-      if (is_interp_filter_exact_match(st, mbmi)) {
-        mbmi->interp_filters = st->filters;
-        return j;
-      }
+  // [skip_levels][single or comp]
+  const int thr[2][2] = { { 0, 0 }, { 3, 7 } };
+  const int is_comp = has_second_ref(mbmi);
+
+  // Find good enough match.
+  // TODO(yunqing): Separate single-ref mode and comp mode stats for fast
+  // search.
+  int best = INT_MAX;
+  int match = -1;
+  for (int j = 0; j < interp_filter_stats_idx; ++j) {
+    const INTERPOLATION_FILTER_STATS *st = &interp_filter_stats[j];
+    const int mv_diff = is_interp_filter_good_match(st, mbmi, skip_level);
+    // Exact match is found.
+    if (mv_diff == 0) {
+      match = j;
+      break;
+    } else if (mv_diff < best && mv_diff <= thr[skip_level - 1][is_comp]) {
+      best = mv_diff;
+      match = j;
     }
-  } else {
-    // Find good enough match.
-    // TODO(yunqing): Separate single-ref mode and comp mode stats for fast
-    // search.
-    int best = INT_MAX;
-    int match = -1;
-    for (int j = 0; j < interp_filter_stats_idx; ++j) {
-      const INTERPOLATION_FILTER_STATS *st = &interp_filter_stats[j];
-      const int mv_diff = is_interp_filter_good_match(st, mbmi);
-      if (mv_diff != -1 && mv_diff < best) {
-        best = mv_diff;
-        match = j;
-      }
-    }
-    if (match != -1) {
-      mbmi->interp_filters = interp_filter_stats[match].filters;
-      return match;
-    }
+  }
+
+  if (match != -1) {
+    mbmi->interp_filters = interp_filter_stats[match].filters;
+    return match;
   }
   return -1;  // no match result found
 }
@@ -9125,10 +9110,10 @@
     INTERPOLATION_FILTER_STATS *interp_filter_stats,
     int interp_filter_stats_idx) {
   int match_found_idx = -1;
-  if (cpi->sf.skip_repeat_interpolation_filter_search && need_search)
-    match_found_idx = find_interp_filter_in_stats(
-        mbmi, interp_filter_stats, interp_filter_stats_idx,
-        cpi->sf.skip_repeat_interpolation_filter_search);
+  if (cpi->sf.use_interp_filter && need_search)
+    match_found_idx = find_interp_filter_in_stats(mbmi, interp_filter_stats,
+                                                  interp_filter_stats_idx,
+                                                  cpi->sf.use_interp_filter);
 
   if (!need_search || match_found_idx == -1)
     set_default_interp_filters(mbmi, assign_filter);
@@ -9325,7 +9310,7 @@
   x->pred_sse[ref_frame] = (unsigned int)(rd_stats_luma.sse >> 4);
 
   // save search results
-  if (cpi->sf.skip_repeat_interpolation_filter_search) {
+  if (cpi->sf.use_interp_filter) {
     assert(match_found_idx == -1);
     args->interp_filter_stats_idx = save_interp_filter_search_stat(
         mbmi, *rd, x->pred_sse[ref_frame], args->interp_filter_stats,
diff --git a/av1/encoder/speed_features.c b/av1/encoder/speed_features.c
index 2b432c1..4979c95 100644
--- a/av1/encoder/speed_features.c
+++ b/av1/encoder/speed_features.c
@@ -300,7 +300,7 @@
     sf->prune_ref_frame_for_gm_search = boosted ? 0 : 1;
     sf->intra_tx_size_search_init_depth_rect = 1;
 
-    sf->skip_repeat_interpolation_filter_search = 1;
+    sf->use_interp_filter = 1;
     sf->tx_type_search.skip_tx_search = 1;
     sf->tx_type_search.ml_tx_split_thresh = 4000;
     sf->adaptive_txb_search_level = 2;
@@ -463,7 +463,7 @@
     // sf->tx_domain_dist_level = 2;
     sf->tx_domain_dist_thres_level = 2;
     sf->simple_motion_search_prune_agg = 2;
-    sf->skip_repeat_interpolation_filter_search = 2;
+    sf->use_interp_filter = 2;
     sf->prune_ref_mv_idx_search = 1;
   }
 
@@ -533,7 +533,7 @@
     sf->intra_tx_size_search_init_depth_rect = 1;
     sf->tx_size_search_lgr_block = 1;
     sf->prune_ext_partition_types_search_level = 2;
-    sf->skip_repeat_interpolation_filter_search = 1;
+    sf->use_interp_filter = 1;
     sf->tx_type_search.skip_tx_search = 1;
     sf->tx_type_search.ml_tx_split_thresh = 4000;
     sf->adaptive_txb_search_level = 2;
@@ -898,7 +898,7 @@
   sf->prune_ref_frame_for_gm_search = 0;
   sf->use_fast_interpolation_filter_search = 0;
   sf->disable_dual_filter = 0;
-  sf->skip_repeat_interpolation_filter_search = 0;
+  sf->use_interp_filter = 0;
   sf->use_hash_based_trellis = 0;
   sf->prune_comp_search_by_single_result = 0;
   sf->skip_repeated_newmv = 0;
diff --git a/av1/encoder/speed_features.h b/av1/encoder/speed_features.h
index 7433eb5..d4e9070 100644
--- a/av1/encoder/speed_features.h
+++ b/av1/encoder/speed_features.h
@@ -539,9 +539,9 @@
   int disable_dual_filter;
 
   // Save results of interpolation_filter_search for a block
-  // Check mv and ref_frames before search, if they are same with previous
-  // saved results, it can be skipped.
-  int skip_repeat_interpolation_filter_search;
+  // Check mv and ref_frames before search, if they are very close with previous
+  // saved results, filter search can be skipped.
+  int use_interp_filter;
 
   // Use a hash table to store previously computed optimized qcoeffs from
   // expensive calls to optimize_txb.