av1: Reuse skip-RD SSE and trim comp buffers

Cache Y-plane SSE from prune_mode_by_skip_rd() for model_rd reuse,
size diffwtd seg_mask clears to block pixel count, reorder fast
is_comp_rd_match() keys, reuse interintra preds, and prune masks
early below rd_thresh in a single bit-exact compound-RD pass.

Speedup: -0.71% weighted 5-case MT 2-pass encode time on AMD EPYC 7B13
(1080p -2.73% / paired ABBA -1.97%, 4K paired ABBA -0.66%; 5/5
bit-exact SHA-256).
Binary size (x86_64 -O3): libaom.a .text +1.7 KiB (+0.02%),
  .rodata +0.0 KiB (+0.00%); stripped aomenc +0.0 KiB (+0.00%).

TAG=agy
CONV=181013d1-8c6e-44c8-b7ad-7781bba16c12
Change-Id: I7c74aa8dcd16194dbedae6e319c510aec3948b11
diff --git a/av1/encoder/compound_type.c b/av1/encoder/compound_type.c
index eb6ee9e..ce10f2f 100644
--- a/av1/encoder/compound_type.c
+++ b/av1/encoder/compound_type.c
@@ -9,6 +9,8 @@
  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
  */
 
+#include "config/aom_dsp_rtcd.h"
+
 #include "av1/common/pred_common.h"
 #include "av1/encoder/compound_type.h"
 #include "av1/encoder/encoder_alloc.h"
@@ -42,18 +44,20 @@
                                    int32_t *comp_rate, int64_t *comp_dist,
                                    int32_t *comp_model_rate,
                                    int64_t *comp_model_dist, int *comp_rs2) {
+  // Match MV and reference indices first as they discriminate fastest.
+  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;
+    }
+  }
   // TODO(ranjit): Ensure that compound type search use regular filter always
   // and check if following check can be removed
   // Check if interp filter matches with previous case
   if (st->filter.as_int != mi->interp_filters.as_int) return 0;
 
   const MACROBLOCKD *const xd = &x->e_mbd;
-  // Match MV and reference indices
   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;
-    }
     const WarpedMotionParams *const wm = &xd->global_motion[mi->ref_frame[i]];
     if (is_global_mv_block(mi, wm->wmtype) != st->is_global[i]) return 0;
   }
@@ -239,7 +243,8 @@
 
     wedge_sign = av1_wedge_sign_from_residuals(ds, mask, N, sign_limit);
 
-    mask = av1_get_contiguous_soft_mask(wedge_index, wedge_sign, bsize);
+    if (wedge_sign)
+      mask = av1_get_contiguous_soft_mask(wedge_index, wedge_sign, bsize);
     sse = av1_wedge_sse_from_residuals(residual1, diff10, mask, N);
     sse = ROUND_POWER_OF_TWO(sse, bd_round);
 
@@ -353,7 +358,7 @@
   DIFFWTD_MASK_TYPE best_mask_type = 0;
   const int hbd = is_cur_buf_hbd(xd);
   const int bd_round = hbd ? (xd->bd - 8) * 2 : 0;
-  DECLARE_ALIGNED(16, uint8_t, seg_mask[2 * MAX_SB_SQUARE]);
+  DECLARE_ALIGNED(16, uint8_t, seg_mask[MAX_SB_SQUARE]);
   uint8_t *tmp_mask[2] = { xd->seg_mask, seg_mask };
   // try each mask type and its inverse
   for (cur_mask_type = 0; cur_mask_type < DIFFWTD_MASK_TYPES; cur_mask_type++) {
@@ -389,7 +394,7 @@
   }
   mbmi->interinter_comp.mask_type = best_mask_type;
   if (best_mask_type == DIFFWTD_38_INV) {
-    memcpy(xd->seg_mask, seg_mask, N * 2);
+    memcpy(xd->seg_mask, seg_mask, N);
   }
   return best_rd;
 }
@@ -826,6 +831,24 @@
 
   const AV1_COMMON *const cm = &cpi->common;
   MACROBLOCKD *xd = &x->e_mbd;
+  INTERINTRA_MODE best_interintra_mode =
+      args->inter_intra_mode[mbmi->ref_frame[0]];
+  // When smooth interintra is enabled and the reused mode already exceeds
+  // ref_best_rd, handle_smooth_inter_intra_mode() returns IGNORE_MODE before
+  // wedge search runs, so return early before building Y predictors.
+  if (try_smooth_interintra && ref_best_rd < INT64_MAX &&
+      cpi->sf.inter_sf.reuse_inter_intra_mode &&
+      best_interintra_mode != INTERINTRA_MODES) {
+    const ModeCosts *mode_costs = &x->mode_costs;
+    const int rmode =
+        mode_costs->interintra_mode_cost[size_group_lookup[bsize]]
+                                        [best_interintra_mode] +
+        (is_wedge_used ? mode_costs->wedge_interintra_cost[bsize][0] : 0);
+    if (compute_rd_thresh(x, rmode + *rate_mv, ref_best_rd) < 0) {
+      return IGNORE_MODE;
+    }
+  }
+
   const int bw = block_size_wide[bsize];
   DECLARE_ALIGNED(16, uint8_t, tmp_buf_[2 * MAX_INTERINTRA_SB_SQUARE]);
   DECLARE_ALIGNED(16, uint8_t, intrapred_[2 * MAX_INTERINTRA_SB_SQUARE]);
@@ -845,8 +868,6 @@
   // Restore the buffers for intra prediction
   restore_dst_buf(xd, *orig_dst, num_planes);
   mbmi->ref_frame[1] = INTRA_FRAME;
-  INTERINTRA_MODE best_interintra_mode =
-      args->inter_intra_mode[mbmi->ref_frame[0]];
 
   // Compute smooth_interintra
   int64_t best_interintra_rd_nowedge = INT64_MAX;
@@ -906,13 +927,21 @@
     *rate_mv = tmp_rate_mv;
     best_mode_rate = rate_overhead;
   } else if (try_smooth_interintra && try_wedge_interintra) {
-    // If smooth was best, but we over-wrote the values when evaluating the
-    // wedge mode, we need to recompute the smooth values.
+    // If smooth was best, restore its settings. With
+    // fast_interintra_wedge_search enabled, handle_wedge_inter_intra_mode()
+    // keeps tmp_buf and intrapred for best_interintra_mode intact so plane Y
+    // can just be re-combined; otherwise intrapred was overwritten and plane Y
+    // must be rebuilt.
     mbmi->use_wedge_interintra = 0;
     mbmi->interintra_mode = best_interintra_mode;
     mbmi->mv[0].as_int = mv0.as_int;
-    av1_enc_build_inter_predictor(cm, xd, mi_row, mi_col, orig_dst, bsize,
-                                  AOM_PLANE_Y, AOM_PLANE_Y);
+    if (cpi->sf.inter_sf.fast_interintra_wedge_search) {
+      av1_combine_interintra(xd, bsize, AOM_PLANE_Y, tmp_buf, bw, intrapred,
+                             bw);
+    } else {
+      av1_enc_build_inter_predictor(cm, xd, mi_row, mi_col, orig_dst, bsize,
+                                    AOM_PLANE_Y, AOM_PLANE_Y);
+    }
   }
   *tmp_rate2 += best_mode_rate;
 
@@ -1111,18 +1140,33 @@
     return 0;
 }
 
+// Sets *out_sse_y to -1 when skip-RD is skipped; callers guard reuse with >= 0.
 static inline int prune_mode_by_skip_rd(const AV1_COMP *const cpi,
                                         MACROBLOCK *x, MACROBLOCKD *xd,
                                         const BLOCK_SIZE bsize,
-                                        int64_t ref_skip_rd, int mode_rate) {
+                                        int64_t ref_skip_rd, int mode_rate,
+                                        int64_t *out_sse_y, int *out_samples) {
   int eval_txfm = 1;
+  if (out_sse_y) *out_sse_y = -1;
+  if (out_samples) *out_samples = 0;
   const int txfm_rd_gate_level =
       get_txfm_rd_gate_level(cpi->common.seq_params->enable_masked_compound,
                              cpi->sf.inter_sf.txfm_rd_gate_level, bsize,
                              TX_SEARCH_COMP_TYPE_MODE, /*eval_motion_mode=*/0);
   // Check if the mode is good enough based on skip rd
   if (txfm_rd_gate_level) {
-    int64_t sse_y = compute_sse_plane(cpi, x, xd, PLANE_TYPE_Y, bsize);
+    int bw, bh;
+    const int block_width = block_size_wide[bsize];
+    const int block_height = block_size_high[bsize];
+    const struct macroblock_plane *const p = &x->plane[0];
+    const struct macroblockd_plane *const pd = &xd->plane[0];
+    get_visible_dimensions(x, 0, bsize, 0, 0, block_width, block_height,
+                           /*clip_dims=*/true, &bw, &bh);
+    int64_t sse_y = pixel_dist_visible_only(cpi, x, p->src.buf, p->src.stride,
+                                            pd->dst.buf, pd->dst.stride, bsize,
+                                            block_height, block_width, bh, bw);
+    if (out_sse_y) *out_sse_y = sse_y;
+    if (out_samples) *out_samples = bw * bh;
     int64_t skip_rd = RDCOST(x->rdmult, mode_rate, (sse_y << 4));
     eval_txfm =
         check_txfm_eval(x, bsize, ref_skip_rd, skip_rd, txfm_rd_gate_level, 1);
@@ -1130,6 +1174,65 @@
   return eval_txfm;
 }
 
+static inline void model_rd_plane0_with_cached_sse(
+    const AV1_COMP *const cpi, MACROBLOCK *x, MACROBLOCKD *xd, BLOCK_SIZE bsize,
+    int64_t cached_sse_y, int cached_samples, bool skip_model_curve,
+    int *rate_sum, int64_t *dist_sum) {
+#if MODELRD_TYPE_MASKED_COMPOUND == 1
+  if (cached_sse_y >= 0) {
+    x->pred_sse[xd->mi[0]->ref_frame[0]] =
+        (unsigned int)AOMMIN(cached_sse_y, UINT_MAX);
+    if (skip_model_curve) {
+      *rate_sum = 0;
+      *dist_sum = 0;
+      return;
+    }
+    model_rd_with_curvfit(cpi, x, bsize, 0, cached_sse_y, cached_samples,
+                          rate_sum, dist_sum);
+    return;
+  }
+#else
+  (void)cached_sse_y;
+  (void)cached_samples;
+  (void)skip_model_curve;
+#endif
+  uint8_t tmp_skip_txfm_sb;
+  int64_t tmp_skip_sse_sb;
+  model_rd_sb_fn[MODELRD_TYPE_MASKED_COMPOUND](
+      cpi, bsize, x, xd, 0, 0, rate_sum, dist_sum, &tmp_skip_txfm_sb,
+      &tmp_skip_sse_sb, NULL, NULL, NULL);
+}
+
+// Reuses pick_interinter_seg()'s xd->seg_mask (or contiguous wedge mask)
+// instead of recomputing DIFFWTD masks for plane 0.
+static inline void build_masked_compound_pred_plane0(MACROBLOCKD *xd,
+                                                     BLOCK_SIZE bsize,
+                                                     const uint8_t *src0,
+                                                     const uint8_t *src1,
+                                                     int src_stride) {
+  MB_MODE_INFO *const mbmi = xd->mi[0];
+  struct buf_2d *const dst_buf = &xd->plane[0].dst;
+  const int bw = block_size_wide[bsize];
+  const int bh = block_size_high[bsize];
+  mbmi->interinter_comp.seg_mask = xd->seg_mask;
+  const INTERINTER_COMPOUND_DATA *const comp_data = &mbmi->interinter_comp;
+  const uint8_t *const mask =
+      comp_data->type == COMPOUND_WEDGE
+          ? av1_get_contiguous_soft_mask(comp_data->wedge_index,
+                                         comp_data->wedge_sign, bsize)
+          : comp_data->seg_mask;
+#if CONFIG_AV1_HIGHBITDEPTH
+  if (is_cur_buf_hbd(xd)) {
+    aom_highbd_blend_a64_mask(
+        dst_buf->buf, dst_buf->stride, CONVERT_TO_BYTEPTR(src0), src_stride,
+        CONVERT_TO_BYTEPTR(src1), src_stride, mask, bw, bw, bh, 0, 0, xd->bd);
+    return;
+  }
+#endif  // CONFIG_AV1_HIGHBITDEPTH
+  aom_blend_a64_mask(dst_buf->buf, dst_buf->stride, src0, src_stride, src1,
+                     src_stride, mask, bw, bw, bh, 0, 0);
+}
+
 static int64_t masked_compound_type_rd(
     const AV1_COMP *const cpi, MACROBLOCK *x, const int_mv *const cur_mv,
     const BLOCK_SIZE bsize, const PREDICTION_MODE this_mode, int *rs2,
@@ -1153,6 +1256,15 @@
   pick_interinter_mask_type pick_interinter_mask[2] = { pick_interinter_wedge,
                                                         pick_interinter_seg };
 
+  const int min_mask_rate =
+      (compound_type == COMPOUND_DIFFWTD || av1_is_wedge_used(bsize))
+          ? av1_cost_literal(1)
+          : 0;
+  if (RDCOST(x->rdmult, *rs2 + min_mask_rate + mode_rate, 0) > rd_thresh) {
+    *comp_model_rd_cur = INT64_MAX;
+    return INT64_MAX;
+  }
+
   // TODO(any): Save pred and mask calculation as well into records. However
   // this may increase memory requirements as compound segment mask needs to be
   // stored in each record.
@@ -1215,8 +1327,8 @@
                                     AOM_PLANE_Y, AOM_PLANE_Y);
     } else {
       *out_rate_mv = rate_mv;
-      av1_build_wedge_inter_predictor_from_buf(xd, bsize, 0, 0, preds0, strides,
-                                               preds1, strides);
+      build_masked_compound_pred_plane0(xd, bsize, *preds0, *preds1,
+                                        strides[0]);
     }
     // Get the RD cost from model RD
     model_rd_sb_fn[MODELRD_TYPE_MASKED_COMPOUND](
@@ -1225,15 +1337,20 @@
     rd = RDCOST(x->rdmult, *rs2 + *out_rate_mv + rate_sum, dist_sum);
     *comp_model_rd_cur = rd;
     // Override with best if current is worse than best for new MV
-    if (wedge_newmv_search) {
-      if (rd >= best_rd_cur) {
-        mbmi->mv[0].as_int = cur_mv[0].as_int;
-        mbmi->mv[1].as_int = cur_mv[1].as_int;
-        *out_rate_mv = rate_mv;
-        av1_build_wedge_inter_predictor_from_buf(xd, bsize, 0, 0, preds0,
-                                                 strides, preds1, strides);
-        *comp_model_rd_cur = best_rd_cur;
+    if (wedge_newmv_search && rd >= best_rd_cur) {
+      if (cpi->sf.inter_sf.prune_comp_type_by_model_rd &&
+          best_rd_cur > comp_best_model_rd && comp_best_model_rd != INT64_MAX) {
+        // av1_compound_type_rd() ignores *out_rate_mv on INT64_MAX and resets
+        // mbmi->mv at the end of each loop iteration.
+        *comp_model_rd_cur = INT64_MAX;
+        return INT64_MAX;
       }
+      mbmi->mv[0].as_int = cur_mv[0].as_int;
+      mbmi->mv[1].as_int = cur_mv[1].as_int;
+      *out_rate_mv = rate_mv;
+      build_masked_compound_pred_plane0(xd, bsize, *preds0, *preds1,
+                                        strides[0]);
+      *comp_model_rd_cur = best_rd_cur;
     }
     if (cpi->sf.inter_sf.prune_comp_type_by_model_rd &&
         (*comp_model_rd_cur > comp_best_model_rd) &&
@@ -1326,9 +1443,18 @@
   const int match_found =
       find_comp_rd_in_stats(cpi, x, mbmi, comp_rate, comp_dist, comp_model_rate,
                             comp_model_dist, comp_rs2, &match_index);
+  *rd = INT64_MAX;
+
+  // If the match is found, calculate the rd cost using the
+  // stored stats and update the mbmi appropriately.
+  if (match_found && cpi->sf.inter_sf.reuse_compound_type_decision) {
+    return populate_reuse_comp_type_data(x, mbmi, &best_type_stats, cur_mv,
+                                         comp_rate, comp_dist, comp_rs2,
+                                         rate_mv, rd, match_index);
+  }
+
   best_mv[0].as_int = cur_mv[0].as_int;
   best_mv[1].as_int = cur_mv[1].as_int;
-  *rd = INT64_MAX;
 
   // Local array to store the valid compound types to be evaluated in the core
   // loop
@@ -1355,14 +1481,6 @@
   const int mi_row = xd->mi_row;
   const int mi_col = xd->mi_col;
 
-  // If the match is found, calculate the rd cost using the
-  // stored stats and update the mbmi appropriately.
-  if (match_found && cpi->sf.inter_sf.reuse_compound_type_decision) {
-    return populate_reuse_comp_type_data(x, mbmi, &best_type_stats, cur_mv,
-                                         comp_rate, comp_dist, comp_rs2,
-                                         rate_mv, rd, match_index);
-  }
-
   // If COMPOUND_AVERAGE is not valid, use the spare buffer
   if (valid_comp_types[0] != COMPOUND_AVERAGE) restore_dst_buf(xd, *tmp_dst, 1);
 
@@ -1395,37 +1513,47 @@
     if (cur_type < COMPOUND_WEDGE) {
       if (skip_mv_refinement_for_avg_distwtd) {
         int rate_sum;
-        uint8_t tmp_skip_txfm_sb;
-        int64_t dist_sum, tmp_skip_sse_sb;
+        int64_t dist_sum;
 
         // Reuse data if matching record is found
         if (comp_rate[cur_type] == INT_MAX) {
-          av1_enc_build_inter_predictor(cm, xd, mi_row, mi_col, orig_dst, bsize,
-                                        AOM_PLANE_Y, AOM_PLANE_Y);
-          if (cur_type == COMPOUND_AVERAGE) *is_luma_interp_done = 1;
-          // Compute RD cost for the current type
-          RD_STATS est_rd_stats;
           const int64_t tmp_rd_thresh = AOMMIN(*rd, rd_thresh) - mode_rd;
-          int64_t est_rd = INT64_MAX;
-          int eval_txfm = prune_mode_by_skip_rd(cpi, x, xd, bsize, ref_skip_rd,
-                                                rs2 + *rate_mv);
-          // Evaluate further if skip rd is low enough
-          if (eval_txfm) {
-            est_rd = estimate_yrd_for_sb(cpi, bsize, x, tmp_rd_thresh,
-                                         &est_rd_stats);
-          }
-          if (est_rd != INT64_MAX) {
-            best_rd_cur = RDCOST(x->rdmult, rs2 + *rate_mv + est_rd_stats.rate,
-                                 est_rd_stats.dist);
-            model_rd_sb_fn[MODELRD_TYPE_MASKED_COMPOUND](
-                cpi, bsize, x, xd, 0, 0, &rate_sum, &dist_sum,
-                &tmp_skip_txfm_sb, &tmp_skip_sse_sb, NULL, NULL, NULL);
-            comp_model_rd_cur =
-                RDCOST(x->rdmult, rs2 + *rate_mv + rate_sum, dist_sum);
-            // Backup rate and distortion for future reuse
-            backup_stats(cur_type, comp_rate, comp_dist, comp_model_rate,
-                         comp_model_dist, rate_sum, dist_sum, &est_rd_stats,
-                         comp_rs2, rs2);
+          if (tmp_rd_thresh >= 0) {
+            av1_enc_build_inter_predictor(cm, xd, mi_row, mi_col, orig_dst,
+                                          bsize, AOM_PLANE_Y, AOM_PLANE_Y);
+            if (cur_type == COMPOUND_AVERAGE) *is_luma_interp_done = 1;
+            // Compute RD cost for the current type
+            RD_STATS est_rd_stats;
+            int64_t est_rd = INT64_MAX;
+            int64_t sse_y;
+            int vis_samples;
+            int eval_txfm =
+                prune_mode_by_skip_rd(cpi, x, xd, bsize, ref_skip_rd,
+                                      rs2 + *rate_mv, &sse_y, &vis_samples);
+            // Evaluate further if skip rd is low enough
+            if (eval_txfm) {
+              est_rd = estimate_yrd_for_sb(cpi, bsize, x, tmp_rd_thresh,
+                                           &est_rd_stats);
+            }
+            if (est_rd != INT64_MAX) {
+              best_rd_cur =
+                  RDCOST(x->rdmult, rs2 + *rate_mv + est_rd_stats.rate,
+                         est_rd_stats.dist);
+              // Single-type search never compares model RD and reuse hits
+              // return via populate_reuse_comp_type_data().
+              const bool skip_model_curve =
+                  valid_type_count == 1 &&
+                  cpi->sf.inter_sf.reuse_compound_type_decision;
+              model_rd_plane0_with_cached_sse(cpi, x, xd, bsize, sse_y,
+                                              vis_samples, skip_model_curve,
+                                              &rate_sum, &dist_sum);
+              comp_model_rd_cur =
+                  RDCOST(x->rdmult, rs2 + *rate_mv + rate_sum, dist_sum);
+              // Backup rate and distortion for future reuse
+              backup_stats(cur_type, comp_rate, comp_dist, comp_model_rate,
+                           comp_model_dist, rate_sum, dist_sum, &est_rd_stats,
+                           comp_rs2, rs2);
+            }
           }
         } else {
           // Calculate RD cost based on stored stats
@@ -1447,8 +1575,9 @@
                 &inter_pred_params.conv_params.bck_offset,
                 &inter_pred_params.conv_params.use_dist_wtd_comp_avg, 1);
             int mask_value = inter_pred_params.conv_params.fwd_offset * 4;
+            const int num_pels = 1 << num_pels_log2_lookup[bsize];
             memset(xd->seg_mask, mask_value,
-                   sizeof(xd->seg_mask[0]) * 2 * MAX_SB_SQUARE);
+                   sizeof(xd->seg_mask[0]) * num_pels);
           }
           tmp_rate_mv = av1_interinter_compound_motion_search(cpi, x, cur_mv,
                                                               bsize, this_mode);
@@ -1457,8 +1586,11 @@
                                       AOM_PLANE_Y, AOM_PLANE_Y);
         if (cur_type == COMPOUND_AVERAGE) *is_luma_interp_done = 1;
 
-        int eval_txfm = prune_mode_by_skip_rd(cpi, x, xd, bsize, ref_skip_rd,
-                                              rs2 + *rate_mv);
+        int64_t sse_y;
+        int vis_samples;
+        int eval_txfm =
+            prune_mode_by_skip_rd(cpi, x, xd, bsize, ref_skip_rd,
+                                  rs2 + *rate_mv, &sse_y, &vis_samples);
         int64_t est_rd = INT64_MAX;
         RD_STATS est_rd_stats;
         if (eval_txfm) {
@@ -1468,11 +1600,15 @@
           best_rd_cur = RDCOST(x->rdmult, rs2 + tmp_rate_mv + est_rd_stats.rate,
                                est_rd_stats.dist);
           int rate_sum;
-          uint8_t tmp_skip_txfm_sb;
-          int64_t dist_sum, tmp_skip_sse_sb;
-          model_rd_sb_fn[MODELRD_TYPE_MASKED_COMPOUND](
-              cpi, bsize, x, xd, 0, 0, &rate_sum, &dist_sum, &tmp_skip_txfm_sb,
-              &tmp_skip_sse_sb, NULL, NULL, NULL);
+          int64_t dist_sum;
+          // Single-type search never compares model RD and reuse hits
+          // return via populate_reuse_comp_type_data().
+          const bool skip_model_curve =
+              valid_type_count == 1 &&
+              cpi->sf.inter_sf.reuse_compound_type_decision;
+          model_rd_plane0_with_cached_sse(cpi, x, xd, bsize, sse_y, vis_samples,
+                                          skip_model_curve, &rate_sum,
+                                          &dist_sum);
           comp_model_rd_cur =
               RDCOST(x->rdmult, rs2 + tmp_rate_mv + rate_sum, dist_sum);
           // Backup rate and distortion for future reuse
@@ -1547,7 +1683,7 @@
           RD_STATS est_rd_stats;
           int64_t this_rd_cur = INT64_MAX;
           int eval_txfm = prune_mode_by_skip_rd(cpi, x, xd, bsize, ref_skip_rd,
-                                                rs2 + *rate_mv);
+                                                rs2 + *rate_mv, NULL, NULL);
           if (eval_txfm) {
             this_rd_cur = estimate_yrd_for_sb(
                 cpi, bsize, x, AOMMIN(best_rd_cur, ref_best_rd), &est_rd_stats);
@@ -1623,7 +1759,7 @@
         av1_enc_build_inter_predictor(cm, xd, mi_row, mi_col, orig_dst, bsize,
                                       AOM_PLANE_Y, AOM_PLANE_Y);
         int eval_txfm = prune_mode_by_skip_rd(cpi, x, xd, bsize, ref_skip_rd,
-                                              best_rs2 + *rate_mv);
+                                              best_rs2 + *rate_mv, NULL, NULL);
         if (eval_txfm) {
           RD_STATS est_rd_stats;
           estimate_yrd_for_sb(cpi, bsize, x, INT64_MAX, &est_rd_stats);
@@ -1654,8 +1790,8 @@
         if (have_newmv_in_inter_mode(this_mode)) {
           // hard coded number for diff wtd
           int mask_value = mask_index == 0 ? 38 : 26;
-          memset(xd->seg_mask, mask_value,
-                 sizeof(xd->seg_mask[0]) * 2 * MAX_SB_SQUARE);
+          const int num_pels = 1 << num_pels_log2_lookup[bsize];
+          memset(xd->seg_mask, mask_value, sizeof(xd->seg_mask[0]) * num_pels);
           tmp_rate_mv = av1_interinter_compound_motion_search(cpi, x, cur_mv,
                                                               bsize, this_mode);
         }
@@ -1664,7 +1800,7 @@
         RD_STATS est_rd_stats;
         int64_t this_rd_cur = INT64_MAX;
         int eval_txfm = prune_mode_by_skip_rd(cpi, x, xd, bsize, ref_skip_rd,
-                                              rs2 + *rate_mv);
+                                              rs2 + *rate_mv, NULL, NULL);
         if (eval_txfm) {
           this_rd_cur =
               estimate_yrd_for_sb(cpi, bsize, x, ref_best_rd, &est_rd_stats);
@@ -1691,8 +1827,8 @@
         rs2 += get_interinter_compound_mask_rate(&x->mode_costs, mbmi);
 
         int mask_value = mbmi->interinter_comp.mask_type == 0 ? 38 : 26;
-        memset(xd->seg_mask, mask_value,
-               sizeof(xd->seg_mask[0]) * 2 * MAX_SB_SQUARE);
+        const int num_pels = 1 << num_pels_log2_lookup[bsize];
+        memset(xd->seg_mask, mask_value, sizeof(xd->seg_mask[0]) * num_pels);
 
         if (have_newmv_in_inter_mode(this_mode)) {
           tmp_rate_mv = av1_interinter_compound_motion_search(cpi, x, cur_mv,
@@ -1706,7 +1842,7 @@
         RD_STATS est_rd_stats;
         int64_t this_rd_cur = INT64_MAX;
         int eval_txfm = prune_mode_by_skip_rd(cpi, x, xd, bsize, ref_skip_rd,
-                                              rs2 + *rate_mv);
+                                              rs2 + *rate_mv, NULL, NULL);
         if (eval_txfm) {
           this_rd_cur =
               estimate_yrd_for_sb(cpi, bsize, x, ref_best_rd, &est_rd_stats);