Address comments related to updating intrabc cost table

Change-Id: I1e933e5310e7783725c5ff75a52a8ebbefa9d79a
diff --git a/av1/encoder/block.h b/av1/encoder/block.h
index cb6fa97..9ddd19b 100644
--- a/av1/encoder/block.h
+++ b/av1/encoder/block.h
@@ -785,8 +785,8 @@
   int joint_mv[MV_JOINTS];
 
   /*! \brief Cost of transmitting the actual motion vector.
-   *  mv_costs_alloc[0][i] is the cost of motion vector with horizontal
-   * component (mv_row) equal to i - MV_MAX. mv_costs_alloc[1][i] is the cost of
+   *  dv_costs_alloc[0][i] is the cost of motion vector with horizontal
+   * component (mv_row) equal to i - MV_MAX. dv_costs_alloc[1][i] is the cost of
    * motion vector with vertical component (mv_col) equal to i - MV_MAX.
    */
   int dv_costs_alloc[2][MV_VALS];
diff --git a/av1/encoder/encodeframe_utils.c b/av1/encoder/encodeframe_utils.c
index 8ded505..232db44 100644
--- a/av1/encoder/encodeframe_utils.c
+++ b/av1/encoder/encodeframe_utils.c
@@ -1294,33 +1294,32 @@
 #endif  // CONFIG_INTERNAL_STATS
 }
 
+/*! Checks whether to skip updating the entropy cost based on tile info.
+ *
+ * This function contains codes common to both \ref skip_mv_cost_update and
+ * \ref skip_dv_cost_update.
+ */
 static int skip_cost_update(const SequenceHeader *seq_params,
                             const TileInfo *const tile_info, const int mi_row,
-                            const int mi_col, int upd_level) {
-  // upd_level=0: update happens at each sb,
-  //              so return skip status as 0.
-  // upd_level=1: update happens once for each sb row,
-  //              so return skip status as 1 for
-  //              mi_col != tile_info->mi_col_start.
-  // upd_level=2: update happens once for a set of rows,
-  //                      so return skip status as 1 appropriately.
-  // upd_level=3: don't update, so return 1 immediately.
-  if (!upd_level) return 0;
+                            const int mi_col,
+                            INTERNAL_COST_UPDATE_TYPE upd_level) {
+  if (upd_level == INTERNAL_COST_UPD_SB) return 0;
+  if (upd_level == INTERNAL_COST_UPD_OFF) return 1;
+
+  // upd_level is at most as frequent as each sb_row in a tile.
   if (mi_col != tile_info->mi_col_start) return 1;
-  if (upd_level == 3) {
-    return 1;
-  }
-  if (upd_level == 2) {
+
+  if (upd_level == INTERNAL_COST_UPD_SBROW_SET) {
     const int mib_size_log2 = seq_params->mib_size_log2;
     const int sb_row = (mi_row - tile_info->mi_row_start) >> mib_size_log2;
     const int sb_size = seq_params->mib_size * MI_SIZE;
     const int tile_height =
         (tile_info->mi_row_end - tile_info->mi_row_start) * MI_SIZE;
-    // When upd_level = 2, the cost update happens once for 2, 4 sb
-    // rows for sb size 128, sb size 64 respectively. However, as the update
-    // will not be equally spaced in smaller resolutions making it equally
-    // spaced by calculating (mv_num_rows_cost_update) the number of rows
-    // after which the cost update should happen.
+    // When upd_level = INTERNAL_COST_UPD_SBROW_SET, the cost update happens
+    // once for 2, 4 sb rows for sb size 128, sb size 64 respectively. However,
+    // as the update will not be equally spaced in smaller resolutions making
+    // it equally spaced by calculating (mv_num_rows_cost_update) the number of
+    // rows after which the cost update should happen.
     const int sb_size_update_freq_map[2] = { 2, 4 };
     const int update_freq_sb_rows =
         sb_size_update_freq_map[sb_size != MAX_SB_SIZE];
@@ -1353,8 +1352,8 @@
 static int skip_dv_cost_update(AV1_COMP *cpi, const TileInfo *const tile_info,
                                const int mi_row, const int mi_col) {
   const AV1_COMMON *cm = &cpi->common;
-  // Intrabc is only applicable to intra frames. So skip if current frame isn't
-  // intra frame.
+  // Intrabc is only applicable to intra frames. So skip if intrabc is not
+  // allowed.
   if (!av1_allow_intrabc(cm) || is_stat_generation_stage(cpi)) {
     return 1;
   }
@@ -1425,7 +1424,7 @@
     case COST_UPD_SB:  // SB level
       // Checks for skip status of dv cost update.
       if (skip_dv_cost_update(cpi, tile_info, mi_row, mi_col)) break;
-      av1_fill_dv_costs(x->dv_costs, &xd->tile_ctx->ndvc);
+      av1_fill_dv_costs(&xd->tile_ctx->ndvc, x->dv_costs);
       break;
     default: assert(0);
   }
diff --git a/av1/encoder/encoder.h b/av1/encoder/encoder.h
index fd553b8..adee9b9 100644
--- a/av1/encoder/encoder.h
+++ b/av1/encoder/encoder.h
@@ -159,13 +159,6 @@
 /*!\cond */
 
 typedef enum {
-  COST_UPD_SB,
-  COST_UPD_SBROW,
-  COST_UPD_TILE,
-  COST_UPD_OFF,
-} COST_UPDATE_TYPE;
-
-typedef enum {
   MOD_FP,           // First pass
   MOD_TF,           // Temporal filtering
   MOD_TPL,          // TPL
@@ -179,6 +172,16 @@
 
 /*!\endcond */
 
+/*!\enum COST_UPDATE_TYPE
+ * \brief This enum controls how often the entropy costs should be updated.
+ */
+typedef enum {
+  COST_UPD_SB,    /*!< Update every sb. */
+  COST_UPD_SBROW, /*!< Update every sb rows inside a tile. */
+  COST_UPD_TILE,  /*!< Update every tile. */
+  COST_UPD_OFF,   /*!< Turn off cost updates. */
+} COST_UPDATE_TYPE;
+
 /*!
  * \brief Encoder config related to resize.
  */
diff --git a/av1/encoder/ethread.c b/av1/encoder/ethread.c
index ec92885..efa5ffc 100644
--- a/av1/encoder/ethread.c
+++ b/av1/encoder/ethread.c
@@ -784,13 +784,13 @@
       aom_free(thread_data->td->mb.txfm_search_info.txb_rd_records);
       thread_data->td->mb.txfm_search_info.txb_rd_records = NULL;
     }
-    if (thread_data->td != &cpi->td &&
-        cpi->oxcf.cost_upd_freq.mv < COST_UPD_OFF) {
-      aom_free(thread_data->td->mb.mv_costs);
-    }
-    if (thread_data->td != &cpi->td &&
-        cpi->oxcf.cost_upd_freq.dv < COST_UPD_OFF) {
-      aom_free(thread_data->td->mb.dv_costs);
+    if (thread_data->td != &cpi->td) {
+      if (cpi->oxcf.cost_upd_freq.mv < COST_UPD_OFF) {
+        aom_free(thread_data->td->mb.mv_costs);
+      }
+      if (cpi->oxcf.cost_upd_freq.dv < COST_UPD_OFF) {
+        aom_free(thread_data->td->mb.dv_costs);
+      }
     }
 
     // Accumulate counters.
@@ -852,10 +852,9 @@
       }
       if (cpi->oxcf.cost_upd_freq.dv < COST_UPD_OFF) {
         CHECK_MEM_ERROR(cm, thread_data->td->mb.dv_costs,
-                        (IntraBCMVCosts *)aom_malloc(
-                            sizeof(*thread_data->td->mb.dv_costs)));
+                        (IntraBCMVCosts *)aom_malloc(sizeof(IntraBCMVCosts)));
         memcpy(thread_data->td->mb.dv_costs, cpi->td.mb.dv_costs,
-               sizeof(*thread_data->td->mb.dv_costs));
+               sizeof(IntraBCMVCosts));
       }
     }
     // Reset cyclic refresh counters.
@@ -917,10 +916,9 @@
       }
       if (cpi->oxcf.cost_upd_freq.dv < COST_UPD_OFF) {
         CHECK_MEM_ERROR(cm, thread_data->td->mb.dv_costs,
-                        (IntraBCMVCosts *)aom_malloc(
-                            sizeof(*thread_data->td->mb.dv_costs)));
+                        (IntraBCMVCosts *)aom_malloc(sizeof(IntraBCMVCosts)));
         memcpy(thread_data->td->mb.dv_costs, cpi->td.mb.dv_costs,
-               sizeof(*thread_data->td->mb.dv_costs));
+               sizeof(IntraBCMVCosts));
       }
     }
     if (!cpi->sf.rt_sf.use_nonrd_pick_mode) {
@@ -1209,13 +1207,13 @@
   sync_enc_workers(&cpi->mt_info, cm, num_workers);
   for (int i = num_workers - 1; i >= 0; i--) {
     EncWorkerData *const thread_data = &cpi->mt_info.tile_thr_data[i];
-    if (thread_data->td != &cpi->td &&
-        cpi->oxcf.cost_upd_freq.mv < COST_UPD_OFF) {
-      aom_free(thread_data->td->mb.mv_costs);
-    }
-    if (thread_data->td != &cpi->td &&
-        cpi->oxcf.cost_upd_freq.dv < COST_UPD_OFF) {
-      aom_free(thread_data->td->mb.dv_costs);
+    if (thread_data->td != &cpi->td) {
+      if (cpi->oxcf.cost_upd_freq.mv < COST_UPD_OFF) {
+        aom_free(thread_data->td->mb.mv_costs);
+      }
+      if (cpi->oxcf.cost_upd_freq.dv < COST_UPD_OFF) {
+        aom_free(thread_data->td->mb.dv_costs);
+      }
     }
     if (thread_data->td->mb.txfm_search_info.txb_rd_records) {
       aom_free(thread_data->td->mb.txfm_search_info.txb_rd_records);
diff --git a/av1/encoder/partition_search.c b/av1/encoder/partition_search.c
index ae0c7f6..17cbc3a 100644
--- a/av1/encoder/partition_search.c
+++ b/av1/encoder/partition_search.c
@@ -804,7 +804,7 @@
     const int is_intrabc = is_intrabc_block(mbmi);
     update_cdf(fc->intrabc_cdf, is_intrabc, 2);
 #if CONFIG_ENTROPY_STATS
-    ++td->counts->intrabc[is_intrabc_block(mbmi)];
+    ++td->counts->intrabc[is_intrabc];
 #endif  // CONFIG_ENTROPY_STATS
     if (is_intrabc) {
       const int_mv dv_ref = x->mbmi_ext_frame->ref_mv_stack[0].this_mv;
diff --git a/av1/encoder/rd.c b/av1/encoder/rd.c
index 54c1c3e..aedb10e 100644
--- a/av1/encoder/rd.c
+++ b/av1/encoder/rd.c
@@ -577,7 +577,7 @@
   }
 }
 
-void av1_fill_dv_costs(IntraBCMVCosts *dv_costs, const nmv_context *ndvc) {
+void av1_fill_dv_costs(const nmv_context *ndvc, IntraBCMVCosts *dv_costs) {
   dv_costs->dv_costs[0] = &dv_costs->dv_costs_alloc[0][MV_MAX];
   dv_costs->dv_costs[1] = &dv_costs->dv_costs_alloc[1][MV_MAX];
   av1_build_nmv_cost_table(dv_costs->joint_mv, dv_costs->dv_costs, ndvc,
@@ -617,10 +617,9 @@
       cost_upd_freq.mode == COST_UPD_TILE || fill_costs)
     av1_fill_mode_rates(cm, &x->mode_costs, cm->fc);
 
-  if (!use_nonrd_pick_mode && frame_is_intra_only(cm) &&
-      cm->features.allow_screen_content_tools &&
+  if (!use_nonrd_pick_mode && av1_allow_intrabc(cm) &&
       !is_stat_generation_stage(cpi)) {
-    av1_fill_dv_costs(x->dv_costs, &cm->fc->ndvc);
+    av1_fill_dv_costs(&cm->fc->ndvc, x->dv_costs);
   }
 }
 
diff --git a/av1/encoder/rd.h b/av1/encoder/rd.h
index 0231c2b..8af31b2 100644
--- a/av1/encoder/rd.h
+++ b/av1/encoder/rd.h
@@ -361,7 +361,7 @@
 void av1_fill_mv_costs(const nmv_context *nmvc, int integer_mv, int usehp,
                        MvCosts *mv_costs);
 
-void av1_fill_dv_costs(IntraBCMVCosts *dv_costs, const nmv_context *ndvc);
+void av1_fill_dv_costs(const nmv_context *ndvc, IntraBCMVCosts *dv_costs);
 
 int av1_get_adaptive_rdmult(const struct AV1_COMP *cpi, double beta);
 
diff --git a/av1/encoder/speed_features.c b/av1/encoder/speed_features.c
index 3336b6e..4b87012 100644
--- a/av1/encoder/speed_features.c
+++ b/av1/encoder/speed_features.c
@@ -291,7 +291,7 @@
   sf->part_sf.ml_predict_breakout_level = use_hbd ? 1 : 3;
 
   sf->intra_sf.intra_pruning_with_hog = 1;
-  sf->intra_sf.dv_cost_upd_level = 3;
+  sf->intra_sf.dv_cost_upd_level = INTERNAL_COST_UPD_OFF;
 
   sf->tx_sf.adaptive_txb_search_level = 1;
   sf->tx_sf.intra_tx_size_search_init_depth_sqr = 1;
@@ -632,7 +632,7 @@
     }
 
     if (!is_720p_or_larger) {
-      sf->inter_sf.mv_cost_upd_level = 2;
+      sf->inter_sf.mv_cost_upd_level = INTERNAL_COST_UPD_SBROW_SET;
     }
 
     if (is_720p_or_larger) {
@@ -834,7 +834,7 @@
     sf->mv_sf.search_method = DIAMOND;
     sf->mv_sf.disable_second_mv = 2;
 
-    sf->inter_sf.mv_cost_upd_level = 1;
+    sf->inter_sf.mv_cost_upd_level = INTERNAL_COST_UPD_SBROW;
     sf->inter_sf.disable_onesided_comp = 1;
     // TODO(yunqing): evaluate this speed feature for speed 1 & 2, and combine
     // it with cpi->sf.disable_wedge_search_var_thresh.
@@ -1106,7 +1106,7 @@
 
   sf->interp_sf.use_fast_interpolation_filter_search = 1;
 
-  sf->intra_sf.dv_cost_upd_level = 3;
+  sf->intra_sf.dv_cost_upd_level = INTERNAL_COST_UPD_OFF;
   sf->intra_sf.intra_pruning_with_hog = 1;
 
   sf->mv_sf.full_pixel_search_level = 1;
@@ -1206,7 +1206,7 @@
     // sf->mv_sf.adaptive_motion_search = 1;
 
     sf->inter_sf.adaptive_rd_thresh = 2;
-    sf->inter_sf.mv_cost_upd_level = 1;
+    sf->inter_sf.mv_cost_upd_level = INTERNAL_COST_UPD_SBROW;
     // TODO(yunqing): evaluate this speed feature for speed 1 & 2, and combine
     // it with cpi->sf.disable_wedge_search_var_thresh.
     sf->inter_sf.disable_interintra_wedge_var_thresh = UINT_MAX;
@@ -1492,7 +1492,7 @@
   inter_sf->fast_wedge_sign_estimate = 0;
   inter_sf->use_dist_wtd_comp_flag = DIST_WTD_COMP_ENABLED;
   inter_sf->reuse_inter_intra_mode = 0;
-  inter_sf->mv_cost_upd_level = 0;
+  inter_sf->mv_cost_upd_level = INTERNAL_COST_UPD_SB;
   inter_sf->prune_inter_modes_based_on_tpl = 0;
   inter_sf->prune_nearmv_using_neighbors = 0;
   inter_sf->prune_comp_search_by_single_result = 0;
@@ -1533,8 +1533,7 @@
 }
 
 static AOM_INLINE void init_intra_sf(INTRA_MODE_SPEED_FEATURES *intra_sf) {
-  intra_sf->chroma_intra_pruning_with_hog = 0;
-  intra_sf->dv_cost_upd_level = 0;
+  intra_sf->dv_cost_upd_level = INTERNAL_COST_UPD_SB;
   intra_sf->skip_intra_in_interframe = 1;
   intra_sf->intra_pruning_with_hog = 0;
   intra_sf->chroma_intra_pruning_with_hog = 0;
@@ -1668,9 +1667,9 @@
     cpi->mv_search_params.find_fractional_mv_step = av1_return_min_sub_pixel_mv;
 
   if ((cpi->oxcf.row_mt == 1) && (cpi->oxcf.max_threads > 1)) {
-    if (sf->inter_sf.mv_cost_upd_level > 1) {
+    if (sf->inter_sf.mv_cost_upd_level < INTERNAL_COST_UPD_SBROW) {
       // Set mv_cost_upd_level to use row level update.
-      sf->inter_sf.mv_cost_upd_level = 1;
+      sf->inter_sf.mv_cost_upd_level = INTERNAL_COST_UPD_SBROW;
     }
   }
 }
diff --git a/av1/encoder/speed_features.h b/av1/encoder/speed_features.h
index 53c6898..1a4d75b 100644
--- a/av1/encoder/speed_features.h
+++ b/av1/encoder/speed_features.h
@@ -287,8 +287,23 @@
   SUPERRES_AUTO_DUAL,  // Tries no superres and q-based superres ratios
   SUPERRES_AUTO_SOLO,  // Only apply the q-based superres ratio
 } UENUM1BYTE(SUPERRES_AUTO_SEARCH_TYPE);
-
 /*!\endcond */
+
+/*!\enum INTERNAL_COST_UPDATE_TYPE
+ * \brief This enum decides internally how often to update the entropy costs
+ *
+ * INTERNAL_COST_UPD_TYPE is similar to \ref COST_UPDATE_TYPE but has slightly
+ * more flexibility in update frequency. This enum is separate from \ref
+ * COST_UPDATE_TYPE because although \ref COST_UPDATE_TYPE is not exposed, its
+ * values are public so it cannot be modified without breaking public API.
+ */
+typedef enum {
+  INTERNAL_COST_UPD_OFF,       /*!< Turn off cost updates. */
+  INTERNAL_COST_UPD_SBROW_SET, /*!< Update every row_set of height 256 pixs. */
+  INTERNAL_COST_UPD_SBROW,     /*!< Update every sb rows inside a tile. */
+  INTERNAL_COST_UPD_SB,        /*!< Update every sb. */
+} INTERNAL_COST_UPDATE_TYPE;
+
 /*!
  * \brief Sequence/frame level speed vs quality features
  */
@@ -726,13 +741,9 @@
   // Decide when and how to use joint_comp.
   DIST_WTD_COMP_FLAG use_dist_wtd_comp_flag;
 
-  // To skip cost update for mv.
-  // mv_cost_upd_level indicates the aggressiveness of skipping.
-  // 0: update happens at each sb level.
-  // 1: update happens once for each sb row.
-  // 2: update happens once for a set of rows.
-  // 3: skips updates
-  int mv_cost_upd_level;
+  // Clip the frequency of updating the mv cost.
+  INTERNAL_COST_UPDATE_TYPE mv_cost_upd_level;
+
   // Prune inter modes based on tpl stats
   // 0 : no pruning
   // 1 - 3 indicate increasing aggressiveness in order.
@@ -827,13 +838,8 @@
   // UV_CFL_PRED and the mode that corresponds to luma intra mode winner.
   int prune_chroma_modes_using_luma_winner;
 
-  // To skip cost update for mv.
-  // dv_cost_upd_level indicates the aggressiveness of skipping.
-  // 0: update happens at each sb level.
-  // 1: update happens once for each sb row.
-  // 2: update happens once for a set of rows.
-  // 3: skips updates
-  int dv_cost_upd_level;
+  // Clip the frequency of updating the mv cost for intrabc.
+  INTERNAL_COST_UPDATE_TYPE dv_cost_upd_level;
 } INTRA_MODE_SPEED_FEATURES;
 
 typedef struct TX_SPEED_FEATURES {