Consolidate trellis optimization related speed features

This patch combines perform_coeff_opt and
perform_coeff_opt_based_on_satd speed features

Change-Id: I11cec3dbc7a7b4befd950d475f9fb86875f4acf1
diff --git a/av1/encoder/block.h b/av1/encoder/block.h
index d77c281..099ba2e 100644
--- a/av1/encoder/block.h
+++ b/av1/encoder/block.h
@@ -442,16 +442,14 @@
    *
    * See the documentation for \ref WinnerModeParams for more detail.
    */
-  unsigned int coeff_opt_dist_threshold;
-  //! \copydoc coeff_opt_dist_threshold
-  unsigned int coeff_opt_satd_threshold;
-  //! \copydoc coeff_opt_dist_threshold
+  unsigned int coeff_opt_thresholds[2];
+  /*! \copydoc coeff_opt_thresholds */
   unsigned int tx_domain_dist_threshold;
-  //! \copydoc coeff_opt_dist_threshold
+  /*! \copydoc coeff_opt_thresholds */
   TX_SIZE_SEARCH_METHOD tx_size_search_method;
-  //! \copydoc coeff_opt_dist_threshold
+  /*! \copydoc coeff_opt_thresholds */
   unsigned int use_transform_domain_distortion;
-  //! \copydoc coeff_opt_dist_threshold
+  /*! \copydoc coeff_opt_thresholds */
   unsigned int skip_txfm_level;
 
   /*! \brief How to search for the optimal tx_size
diff --git a/av1/encoder/encoder.h b/av1/encoder/encoder.h
index 2ce143d..6baa5ca 100644
--- a/av1/encoder/encoder.h
+++ b/av1/encoder/encoder.h
@@ -1815,18 +1815,13 @@
  */
 typedef struct {
   /*!
-   * Threshold to determine the best number of transform coefficients to keep
-   * using trellis optimization.
-   * Corresponds to enable_winner_mode_for_coeff_opt speed feature.
-   */
-  unsigned int coeff_opt_dist_threshold[MODE_EVAL_TYPES];
-
-  /*!
    * Threshold to determine if trellis optimization is to be enabled
-   * based on SATD.
+   * based on :
+   * 0 : dist threshold
+   * 1 : satd threshold
    * Corresponds to enable_winner_mode_for_coeff_opt speed feature.
    */
-  unsigned int coeff_opt_satd_threshold[MODE_EVAL_TYPES];
+  unsigned int coeff_opt_thresholds[MODE_EVAL_TYPES][2];
 
   /*!
    * Determines the tx size search method during rdopt.
diff --git a/av1/encoder/rd.h b/av1/encoder/rd.h
index 73c575f..3a567c1 100644
--- a/av1/encoder/rd.h
+++ b/av1/encoder/rd.h
@@ -287,25 +287,33 @@
 
 // Get the threshold for R-D optimization of coefficients depending upon mode
 // decision/winner mode processing
-static INLINE uint32_t get_rd_opt_coeff_thresh(
-    const uint32_t *const coeff_opt_dist_threshold,
-    int enable_winner_mode_for_coeff_opt, int is_winner_mode) {
-  // Default initialization of threshold
-  uint32_t coeff_opt_thresh = coeff_opt_dist_threshold[DEFAULT_EVAL];
+static INLINE void get_rd_opt_coeff_thresh(
+    const uint32_t (*const coeff_opt_threshold)[2],
+    TxfmSearchParams *txfm_params, int enable_winner_mode_for_coeff_opt,
+    int is_winner_mode) {
+  if (!enable_winner_mode_for_coeff_opt) {
+    // Default initialization of threshold
+    txfm_params->coeff_opt_thresholds[0] = coeff_opt_threshold[DEFAULT_EVAL][0];
+    txfm_params->coeff_opt_thresholds[1] = coeff_opt_threshold[DEFAULT_EVAL][1];
+    return;
+  }
   // TODO(any): Experiment with coeff_opt_dist_threshold values when
   // enable_winner_mode_for_coeff_opt is ON
   // TODO(any): Skip the winner mode processing for blocks with lower residual
   // energy as R-D optimization of coefficients would have been enabled during
   // mode decision
-  if (enable_winner_mode_for_coeff_opt) {
-    // Use conservative threshold during mode decision and perform R-D
-    // optimization of coeffs always for winner modes
-    if (is_winner_mode)
-      coeff_opt_thresh = coeff_opt_dist_threshold[WINNER_MODE_EVAL];
-    else
-      coeff_opt_thresh = coeff_opt_dist_threshold[MODE_EVAL];
+
+  // Use conservative threshold during mode decision and perform R-D
+  // optimization of coeffs always for winner modes
+  if (is_winner_mode) {
+    txfm_params->coeff_opt_thresholds[0] =
+        coeff_opt_threshold[WINNER_MODE_EVAL][0];
+    txfm_params->coeff_opt_thresholds[1] =
+        coeff_opt_threshold[WINNER_MODE_EVAL][1];
+  } else {
+    txfm_params->coeff_opt_thresholds[0] = coeff_opt_threshold[MODE_EVAL][0];
+    txfm_params->coeff_opt_thresholds[1] = coeff_opt_threshold[MODE_EVAL][1];
   }
-  return coeff_opt_thresh;
 }
 
 // Used to reset the state of tx/mb rd hash information
diff --git a/av1/encoder/rdopt_utils.h b/av1/encoder/rdopt_utils.h
index 4063889..a130ffe 100644
--- a/av1/encoder/rdopt_utils.h
+++ b/av1/encoder/rdopt_utils.h
@@ -485,10 +485,8 @@
       set_tx_domain_dist_params(winner_mode_params, txfm_params, 0, 0);
 
       // Get default threshold for R-D optimization of coefficients
-      txfm_params->coeff_opt_dist_threshold = get_rd_opt_coeff_thresh(
-          winner_mode_params->coeff_opt_dist_threshold, 0, 0);
-      txfm_params->coeff_opt_satd_threshold = get_rd_opt_coeff_thresh(
-          winner_mode_params->coeff_opt_satd_threshold, 0, 0);
+      get_rd_opt_coeff_thresh(winner_mode_params->coeff_opt_thresholds,
+                              txfm_params, 0, 0);
 
       // Set default transform size search method
       set_tx_size_search_method(cm, winner_mode_params, txfm_params, 0, 0);
@@ -512,11 +510,8 @@
 
       // Get threshold for R-D optimization of coefficients during mode
       // evaluation
-      txfm_params->coeff_opt_dist_threshold = get_rd_opt_coeff_thresh(
-          winner_mode_params->coeff_opt_dist_threshold,
-          sf->winner_mode_sf.enable_winner_mode_for_coeff_opt, 0);
-      txfm_params->coeff_opt_satd_threshold = get_rd_opt_coeff_thresh(
-          winner_mode_params->coeff_opt_satd_threshold,
+      get_rd_opt_coeff_thresh(
+          winner_mode_params->coeff_opt_thresholds, txfm_params,
           sf->winner_mode_sf.enable_winner_mode_for_coeff_opt, 0);
 
       // Set the transform size search method for mode evaluation
@@ -543,11 +538,8 @@
 
       // Get threshold for R-D optimization of coefficients for winner mode
       // evaluation
-      txfm_params->coeff_opt_dist_threshold = get_rd_opt_coeff_thresh(
-          winner_mode_params->coeff_opt_dist_threshold,
-          sf->winner_mode_sf.enable_winner_mode_for_coeff_opt, 1);
-      txfm_params->coeff_opt_satd_threshold = get_rd_opt_coeff_thresh(
-          winner_mode_params->coeff_opt_satd_threshold,
+      get_rd_opt_coeff_thresh(
+          winner_mode_params->coeff_opt_thresholds, txfm_params,
           sf->winner_mode_sf.enable_winner_mode_for_coeff_opt, 1);
 
       // Set the transform size search method for winner mode evaluation
diff --git a/av1/encoder/speed_features.c b/av1/encoder/speed_features.c
index b88c7c2..b9d0f44 100644
--- a/av1/encoder/speed_features.c
+++ b/av1/encoder/speed_features.c
@@ -66,6 +66,7 @@
 // Threshold values to be used for disabling coeff RD-optimization
 // based on block MSE / qstep^2.
 // TODO(any): Experiment the threshold logic based on variance metric.
+// Table has satd and dist threshold value index 0 : dist,index 1: satd
 // For each row, the indices are as follows.
 // Index 0: Default mode evaluation, Winner mode processing is not applicable
 // (Eg : IntraBc)
@@ -74,20 +75,16 @@
 // Index 1 and 2 are applicable when enable_winner_mode_for_coeff_opt speed
 // feature is ON
 // There are 7 levels with increasing speed, mapping to vertical indices.
-static unsigned int coeff_opt_dist_thresholds[7][MODE_EVAL_TYPES] = {
-  { UINT_MAX, UINT_MAX, UINT_MAX },
-  { 3200, 250, UINT_MAX },
-  { 1728, 142, UINT_MAX },
-  { 864, 142, UINT_MAX },
-  { 432, 86, UINT_MAX },
-  { 216, 86, UINT_MAX },
-  { 216, 0, UINT_MAX }
-};
-
-static unsigned int coeff_opt_satd_thresholds[3][MODE_EVAL_TYPES] = {
-  { UINT_MAX, UINT_MAX, UINT_MAX },
-  { 97, 16, UINT_MAX },
-  { 25, 10, UINT_MAX },
+static unsigned int coeff_opt_thresholds[9][MODE_EVAL_TYPES][2] = {
+  { { UINT_MAX, UINT_MAX }, { UINT_MAX, UINT_MAX }, { UINT_MAX, UINT_MAX } },
+  { { 3200, UINT_MAX }, { 250, UINT_MAX }, { UINT_MAX, UINT_MAX } },
+  { { 1728, UINT_MAX }, { 142, UINT_MAX }, { UINT_MAX, UINT_MAX } },
+  { { 864, UINT_MAX }, { 142, UINT_MAX }, { UINT_MAX, UINT_MAX } },
+  { { 432, UINT_MAX }, { 86, UINT_MAX }, { UINT_MAX, UINT_MAX } },
+  { { 864, 97 }, { 142, 16 }, { UINT_MAX, UINT_MAX } },
+  { { 432, 97 }, { 86, 16 }, { UINT_MAX, UINT_MAX } },
+  { { 216, 25 }, { 86, 10 }, { UINT_MAX, UINT_MAX } },
+  { { 216, 25 }, { 0, 10 }, { UINT_MAX, UINT_MAX } }
 };
 
 // Transform size to be used for default, mode and winner mode evaluation
@@ -645,9 +642,7 @@
     // is reset during winner mode processing
     sf->tx_sf.use_intra_txb_hash = 0;
 
-    sf->rd_sf.perform_coeff_opt = is_boosted_arf2_bwd_type ? 3 : 5;
-    sf->rd_sf.perform_coeff_opt_based_on_satd =
-        is_boosted_arf2_bwd_type ? 1 : 2;
+    sf->rd_sf.perform_coeff_opt = is_boosted_arf2_bwd_type ? 5 : 7;
     sf->rd_sf.tx_domain_dist_thres_level = 2;
 
     // TODO(any): Extend multi-winner mode processing support for inter frames
@@ -719,7 +714,7 @@
     sf->tx_sf.use_intra_txb_hash = 1;
     sf->tx_sf.tx_type_search.prune_tx_type_est_rd = 0;
 
-    sf->rd_sf.perform_coeff_opt = is_boosted_arf2_bwd_type ? 4 : 6;
+    sf->rd_sf.perform_coeff_opt = is_boosted_arf2_bwd_type ? 6 : 8;
 
     sf->winner_mode_sf.dc_blk_pred_level = 2;
     sf->winner_mode_sf.multi_winner_mode_type = MULTI_WINNER_MODE_OFF;
@@ -1253,7 +1248,6 @@
   rd_sf->tx_domain_dist_level = 0;
   rd_sf->tx_domain_dist_thres_level = 0;
   rd_sf->perform_coeff_opt = 0;
-  rd_sf->perform_coeff_opt_based_on_satd = 0;
 }
 
 static AOM_INLINE void init_winner_mode_sf(
@@ -1424,20 +1418,12 @@
          tx_domain_dist_types[cpi->sf.rd_sf.tx_domain_dist_level],
          sizeof(winner_mode_params->use_transform_domain_distortion));
 
-  // assert ensures that coeff_opt_dist_thresholds is accessed correctly
+  // assert ensures that coeff_opt_thresholds is accessed correctly
   assert(cpi->sf.rd_sf.perform_coeff_opt >= 0 &&
-         cpi->sf.rd_sf.perform_coeff_opt < 7);
-  memcpy(winner_mode_params->coeff_opt_dist_threshold,
-         coeff_opt_dist_thresholds[cpi->sf.rd_sf.perform_coeff_opt],
-         sizeof(winner_mode_params->coeff_opt_dist_threshold));
-
-  // assert ensures that coeff_opt_satd_thresholds is accessed correctly
-  assert(cpi->sf.rd_sf.perform_coeff_opt_based_on_satd >= 0 &&
-         cpi->sf.rd_sf.perform_coeff_opt_based_on_satd < 3);
-  memcpy(
-      winner_mode_params->coeff_opt_satd_threshold,
-      coeff_opt_satd_thresholds[cpi->sf.rd_sf.perform_coeff_opt_based_on_satd],
-      sizeof(winner_mode_params->coeff_opt_satd_threshold));
+         cpi->sf.rd_sf.perform_coeff_opt < 9);
+  memcpy(winner_mode_params->coeff_opt_thresholds,
+         &coeff_opt_thresholds[cpi->sf.rd_sf.perform_coeff_opt],
+         sizeof(winner_mode_params->coeff_opt_thresholds));
 
   // assert ensures that predict_skip_levels is accessed correctly
   assert(cpi->sf.tx_sf.tx_type_search.use_skip_flag_prediction >= 0 &&
@@ -1497,9 +1483,9 @@
 
     if (is_720p_or_larger && cm->quant_params.base_qindex <= 128) {
       sf->rd_sf.perform_coeff_opt = 2 + is_1080p_or_larger;
-      memcpy(winner_mode_params->coeff_opt_dist_threshold,
-             coeff_opt_dist_thresholds[sf->rd_sf.perform_coeff_opt],
-             sizeof(winner_mode_params->coeff_opt_dist_threshold));
+      memcpy(winner_mode_params->coeff_opt_thresholds,
+             &coeff_opt_thresholds[sf->rd_sf.perform_coeff_opt],
+             sizeof(winner_mode_params->coeff_opt_thresholds));
       sf->part_sf.simple_motion_search_split =
           cm->features.allow_screen_content_tools ? 1 : 2;
       sf->tx_sf.inter_tx_size_search_init_depth_rect = 1;
diff --git a/av1/encoder/speed_features.h b/av1/encoder/speed_features.h
index 4b7583a..12ae704 100644
--- a/av1/encoder/speed_features.h
+++ b/av1/encoder/speed_features.h
@@ -879,11 +879,6 @@
 
   // Flag used to control the extent of coeff R-D optimization
   int perform_coeff_opt;
-
-  // Enable coeff R-D optimization based on SATD values.
-  // 0    : Do not disable coeff R-D opt.
-  // 1, 2 : Disable coeff R-D opt with progressively increasing aggressiveness.
-  int perform_coeff_opt_based_on_satd;
 } RD_CALC_SPEED_FEATURES;
 
 typedef struct WINNER_MODE_SPEED_FEATURES {
diff --git a/av1/encoder/tx_search.c b/av1/encoder/tx_search.c
index 1428c1f..749bca6 100644
--- a/av1/encoder/tx_search.c
+++ b/av1/encoder/tx_search.c
@@ -2345,7 +2345,7 @@
   // TODO(any): Experiment with variance and mean based thresholds
   const int perform_block_coeff_opt =
       ((uint64_t)block_mse_q8 <=
-       (uint64_t)txfm_params->coeff_opt_dist_threshold * qstep * qstep);
+       (uint64_t)txfm_params->coeff_opt_thresholds[0] * qstep * qstep);
   skip_trellis |= !perform_block_coeff_opt;
 
   // Flag to indicate if distortion should be calculated in transform domain or
@@ -2403,8 +2403,7 @@
 
     skip_trellis_based_on_satd[tx_type] = skip_trellis_opt_based_on_satd(
         x, &quant_param, plane, block, tx_size, cpi->oxcf.q_cfg.quant_b_adapt,
-        qstep, txfm_params->coeff_opt_satd_threshold, skip_trellis,
-        dc_only_blk);
+        qstep, txfm_params->coeff_opt_thresholds[1], skip_trellis, dc_only_blk);
 
     av1_quant(x, plane, block, &txfm_param, &quant_param);