Refine early exit during compound type selection

Introduced a speed feature to gate wedge and compound segment
evaluation based on best approximate rd so far

For speed = 1, 2, 3 and 4 presets, BD-rate drop is seen by 0.03%, 0.04%, 0.05%
and 0.06% (as per AWCY runs) with encode time reduction of 1.51%, 2.87%, 3.12%
and 3.64% (averaged across multiple test cases)

STATS_CHANGED

Change-Id: I5f7f0264f79514b0ab810fe990382c28a7cdae01
diff --git a/av1/encoder/encoder.h b/av1/encoder/encoder.h
index 1ff2ef7..35567e2 100644
--- a/av1/encoder/encoder.h
+++ b/av1/encoder/encoder.h
@@ -847,6 +847,11 @@
   //   decoder side value obtained following the short signaling procedure.
   int ref_conv[REF_FRAMES];
 
+  // Factors to control gating of compound type selection based on best
+  // approximate rd so far
+  int max_comp_type_rd_threshold_mul;
+  int max_comp_type_rd_threshold_div;
+
   AV1LfSync lf_row_sync;
   AV1LrSync lr_row_sync;
   AV1LrStruct lr_ctxt;
diff --git a/av1/encoder/rdopt.c b/av1/encoder/rdopt.c
index b393e6f..8047f61 100644
--- a/av1/encoder/rdopt.c
+++ b/av1/encoder/rdopt.c
@@ -9524,7 +9524,10 @@
       masked_type_cost += x->comp_group_idx_cost[comp_group_idx_ctx][1];
       masked_type_cost += x->compound_type_cost[bsize][cur_type - 1];
       rs2 = masked_type_cost;
-      if (enable_wedge_search(x, cpi) && *rd / 3 < ref_best_rd) {
+
+      if (enable_wedge_search(x, cpi) &&
+          ((*rd / cpi->max_comp_type_rd_threshold_div) *
+           cpi->max_comp_type_rd_threshold_mul) < ref_best_rd) {
         best_rd_cur = build_and_cost_compound_type(
             cpi, x, cur_mv, bsize, this_mode, &rs2, *rate_mv, orig_dst,
             &tmp_rate_mv, preds0, preds1, buffers->residual1, buffers->diff10,
diff --git a/av1/encoder/speed_features.c b/av1/encoder/speed_features.c
index fd0368e..de4a906 100644
--- a/av1/encoder/speed_features.c
+++ b/av1/encoder/speed_features.c
@@ -49,6 +49,10 @@
 };
 static uint8_t intrabc_max_mesh_pct[MAX_MESH_SPEED + 1] = { 100, 100, 100,
                                                             25,  25,  10 };
+// scaling values to be used for gating wedge/compound segment based on best
+// approximate rd
+static int comp_type_rd_threshold_mul[3] = { 1, 10, 11 };
+static int comp_type_rd_threshold_div[3] = { 3, 16, 16 };
 
 // Intra only frames, golden frames (except alt ref overlays) and
 // alt ref frames tend to be coded at a higher than ambient quality
@@ -241,6 +245,7 @@
     sf->full_pixel_motion_search_based_split = 1;
     sf->disable_wedge_search_var_thresh = 0;
     sf->disable_wedge_search_edge_thresh = 0;
+    sf->prune_comp_type_by_comp_avg = 1;
   }
 
   if (speed >= 2) {
@@ -265,6 +270,7 @@
     sf->fast_wedge_sign_estimate = 1;
     sf->disable_dual_filter = 1;
     sf->use_jnt_comp_flag = JNT_COMP_DISABLED;
+    sf->prune_comp_type_by_comp_avg = 2;
   }
 
   if (speed >= 3) {
@@ -517,6 +523,7 @@
   sf->inter_mode_rd_model_estimation = 0;
   sf->obmc_full_pixel_search_level = 0;
   sf->skip_sharp_interp_filter_search = 0;
+  sf->prune_comp_type_by_comp_avg = 0;
 
   if (oxcf->mode == GOOD)
     set_good_speed_features_framesize_independent(cpi, sf, oxcf->speed);
@@ -592,6 +599,10 @@
     cpi->find_fractional_mv_step = av1_return_max_sub_pixel_mv;
   else if (cpi->oxcf.motion_vector_unit_test == 2)
     cpi->find_fractional_mv_step = av1_return_min_sub_pixel_mv;
+  cpi->max_comp_type_rd_threshold_mul =
+      comp_type_rd_threshold_mul[sf->prune_comp_type_by_comp_avg];
+  cpi->max_comp_type_rd_threshold_div =
+      comp_type_rd_threshold_div[sf->prune_comp_type_by_comp_avg];
 
 #if CONFIG_DIST_8X8
   if (sf->use_transform_domain_distortion > 0) cpi->oxcf.using_dist_8x8 = 0;
diff --git a/av1/encoder/speed_features.h b/av1/encoder/speed_features.h
index f71dcbf..9f0aac8 100644
--- a/av1/encoder/speed_features.h
+++ b/av1/encoder/speed_features.h
@@ -593,6 +593,10 @@
   // skip sharp_filter evaluation based on regular and smooth filter rd for
   // dual_filter=0 case
   int skip_sharp_interp_filter_search;
+
+  // prune wedge and compound segment approximate rd evaluation based on
+  // compound average rd/ref_best_rd
+  int prune_comp_type_by_comp_avg;
 } SPEED_FEATURES;
 
 struct AV1_COMP;