Make buffers in MACROBLOCK dynamically allocated

src_diff, seg_mask and winner_mode_stats are part of MACROBLOCK
structure. In multithread path of real time encoding, the
structure copy adds overhead while preparing workers.
Allocating this dynamically helps to reduce the overhead.

Change-Id: Ie9f4b4b6c1b06de0ea1cb2d7e0ba3cf89a5b2be4
diff --git a/av1/common/blockd.h b/av1/common/blockd.h
index 1d1c381..8bb7daa 100644
--- a/av1/common/blockd.h
+++ b/av1/common/blockd.h
@@ -893,7 +893,7 @@
   /*!
    * Mask for this block used for compound prediction.
    */
-  DECLARE_ALIGNED(16, uint8_t, seg_mask[2 * MAX_SB_SQUARE]);
+  uint8_t *seg_mask;
 
   /*!
    * CFL (chroma from luma) related parameters.
diff --git a/av1/decoder/decodeframe.c b/av1/decoder/decodeframe.c
index 8866817..58629e0 100644
--- a/av1/decoder/decodeframe.c
+++ b/av1/decoder/decodeframe.c
@@ -2803,6 +2803,10 @@
   if (pbi->tile_data == NULL || n_tiles != pbi->allocated_tiles) {
     decoder_alloc_tile_data(pbi, n_tiles);
   }
+  if (pbi->dcb.xd.seg_mask == NULL)
+    CHECK_MEM_ERROR(cm, pbi->dcb.xd.seg_mask,
+                    (uint8_t *)aom_memalign(
+                        16, 2 * MAX_SB_SQUARE * sizeof(*pbi->dcb.xd.seg_mask)));
 #if CONFIG_ACCOUNTING
   if (pbi->acct_enabled) {
     aom_accounting_reset(&pbi->accounting);
@@ -3359,6 +3363,8 @@
 
   aom_free(thread_data->tmp_conv_dst);
   thread_data->tmp_conv_dst = NULL;
+  aom_free(thread_data->seg_mask);
+  thread_data->seg_mask = NULL;
   for (int i = 0; i < 2; ++i) {
     aom_free(thread_data->tmp_obmc_bufs[i]);
     thread_data->tmp_obmc_bufs[i] = NULL;
@@ -3391,6 +3397,10 @@
   CHECK_MEM_ERROR(cm, thread_data->tmp_conv_dst,
                   aom_memalign(32, MAX_SB_SIZE * MAX_SB_SIZE *
                                        sizeof(*thread_data->tmp_conv_dst)));
+  CHECK_MEM_ERROR(cm, thread_data->seg_mask,
+                  (uint8_t *)aom_memalign(
+                      16, 2 * MAX_SB_SQUARE * sizeof(*thread_data->seg_mask)));
+
   for (int i = 0; i < 2; ++i) {
     CHECK_MEM_ERROR(
         cm, thread_data->tmp_obmc_bufs[i],
@@ -3413,6 +3423,8 @@
     thread_data->td->dcb.mc_buf[0] = thread_data->td->mc_buf[0];
     thread_data->td->dcb.mc_buf[1] = thread_data->td->mc_buf[1];
     thread_data->td->dcb.xd.tmp_conv_dst = thread_data->td->tmp_conv_dst;
+    if (worker_idx)
+      thread_data->td->dcb.xd.seg_mask = thread_data->td->seg_mask;
     for (int j = 0; j < 2; ++j) {
       thread_data->td->dcb.xd.tmp_obmc_bufs[j] =
           thread_data->td->tmp_obmc_bufs[j];
@@ -3592,6 +3604,10 @@
   if (pbi->tile_data == NULL || n_tiles != pbi->allocated_tiles) {
     decoder_alloc_tile_data(pbi, n_tiles);
   }
+  if (pbi->dcb.xd.seg_mask == NULL)
+    CHECK_MEM_ERROR(cm, pbi->dcb.xd.seg_mask,
+                    (uint8_t *)aom_memalign(
+                        16, 2 * MAX_SB_SQUARE * sizeof(*pbi->dcb.xd.seg_mask)));
 
   for (int row = 0; row < tile_rows; row++) {
     for (int col = 0; col < tile_cols; col++) {
@@ -3778,6 +3794,10 @@
     }
     decoder_alloc_tile_data(pbi, n_tiles);
   }
+  if (pbi->dcb.xd.seg_mask == NULL)
+    CHECK_MEM_ERROR(cm, pbi->dcb.xd.seg_mask,
+                    (uint8_t *)aom_memalign(
+                        16, 2 * MAX_SB_SQUARE * sizeof(*pbi->dcb.xd.seg_mask)));
 
   for (int row = 0; row < tile_rows; row++) {
     for (int col = 0; col < tile_cols; col++) {
diff --git a/av1/decoder/decoder.c b/av1/decoder/decoder.c
index 1680734..48d0e56 100644
--- a/av1/decoder/decoder.c
+++ b/av1/decoder/decoder.c
@@ -194,6 +194,7 @@
     }
     aom_free(pbi->thread_data);
   }
+  aom_free(pbi->dcb.xd.seg_mask);
 
   for (i = 0; i < pbi->num_workers; ++i) {
     AVxWorker *const worker = &pbi->tile_workers[i];
diff --git a/av1/decoder/decoder.h b/av1/decoder/decoder.h
index b20e9c1..61147f9 100644
--- a/av1/decoder/decoder.h
+++ b/av1/decoder/decoder.h
@@ -112,6 +112,8 @@
   // Motion compensation buffer used to get a prediction buffer with extended
   // borders. One buffer for each of the two possible references.
   uint8_t *mc_buf[2];
+  // Mask for this block used for compound prediction.
+  uint8_t *seg_mask;
   // Allocated size of 'mc_buf'.
   int32_t mc_buf_size;
   // If true, the pointers in 'mc_buf' were converted from highbd pointers.
diff --git a/av1/encoder/block.h b/av1/encoder/block.h
index 9ddd19b..91df25d 100644
--- a/av1/encoder/block.h
+++ b/av1/encoder/block.h
@@ -102,7 +102,7 @@
  */
 typedef struct macroblock_plane {
   //! Stores source - pred so the txfm can be computed later
-  DECLARE_ALIGNED(32, int16_t, src_diff[MAX_SB_SQUARE]);
+  int16_t *src_diff;
   //! Dequantized coefficients
   tran_low_t *dqcoeff;
   //! Quantized coefficients
@@ -1114,8 +1114,7 @@
    * In the second pass, we retry the winner modes with more thorough txfm
    * options.
    */
-  WinnerModeStats winner_mode_stats[AOMMAX(MAX_WINNER_MODE_COUNT_INTRA,
-                                           MAX_WINNER_MODE_COUNT_INTER)];
+  WinnerModeStats *winner_mode_stats;
   //! Tracks how many winner modes there are.
   int winner_mode_count;
 
diff --git a/av1/encoder/compound_type.c b/av1/encoder/compound_type.c
index aacb7fc..69062e3 100644
--- a/av1/encoder/compound_type.c
+++ b/av1/encoder/compound_type.c
@@ -1342,7 +1342,8 @@
               &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;
-          memset(xd->seg_mask, mask_value, sizeof(xd->seg_mask));
+          memset(xd->seg_mask, mask_value,
+                 sizeof(xd->seg_mask[0]) * 2 * MAX_SB_SQUARE);
           tmp_rate_mv = av1_interinter_compound_motion_search(cpi, x, cur_mv,
                                                               bsize, this_mode);
         }
@@ -1485,7 +1486,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));
+          memset(xd->seg_mask, mask_value,
+                 sizeof(xd->seg_mask[0]) * 2 * MAX_SB_SQUARE);
           tmp_rate_mv = av1_interinter_compound_motion_search(cpi, x, cur_mv,
                                                               bsize, this_mode);
         }
@@ -1522,7 +1524,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));
+        memset(xd->seg_mask, mask_value,
+               sizeof(xd->seg_mask[0]) * 2 * MAX_SB_SQUARE);
 
         if (have_newmv_in_inter_mode(this_mode)) {
           tmp_rate_mv = av1_interinter_compound_motion_search(cpi, x, cur_mv,
diff --git a/av1/encoder/encodeframe.c b/av1/encoder/encodeframe.c
index 515081d..6716bc2 100644
--- a/av1/encoder/encodeframe.c
+++ b/av1/encoder/encodeframe.c
@@ -1031,6 +1031,7 @@
   const int tile_rows = cm->tiles.rows;
   int tile_col, tile_row;
 
+  MACROBLOCK *const mb = &cpi->td.mb;
   assert(IMPLIES(cpi->tile_data == NULL,
                  cpi->allocated_tiles < tile_cols * tile_rows));
   if (cpi->allocated_tiles < tile_cols * tile_rows) av1_alloc_tile_data(cpi);
@@ -1040,6 +1041,24 @@
     cpi->td.mb.txfm_search_info.txb_rd_records =
         (TxbRdRecords *)aom_malloc(sizeof(TxbRdRecords));
   }
+  const int num_planes = av1_num_planes(&cpi->common);
+  for (int plane = 0; plane < num_planes; plane++) {
+    const int subsampling_xy =
+        plane ? cm->seq_params.subsampling_x + cm->seq_params.subsampling_y : 0;
+    const int sb_size = MAX_SB_SQUARE >> subsampling_xy;
+    CHECK_MEM_ERROR(cm, mb->plane[plane].src_diff,
+                    (int16_t *)aom_memalign(
+                        32, sizeof(*mb->plane[plane].src_diff) * sb_size));
+  }
+  CHECK_MEM_ERROR(cm, mb->e_mbd.seg_mask,
+                  (uint8_t *)aom_memalign(
+                      16, 2 * MAX_SB_SQUARE * sizeof(mb->e_mbd.seg_mask[0])));
+  const int winner_mode_count = frame_is_intra_only(cm)
+                                    ? MAX_WINNER_MODE_COUNT_INTRA
+                                    : MAX_WINNER_MODE_COUNT_INTER;
+  CHECK_MEM_ERROR(cm, mb->winner_mode_stats,
+                  (WinnerModeStats *)aom_malloc(
+                      winner_mode_count * sizeof(mb->winner_mode_stats[0])));
 
   for (tile_row = 0; tile_row < tile_rows; ++tile_row) {
     for (tile_col = 0; tile_col < tile_cols; ++tile_col) {
@@ -1067,6 +1086,16 @@
     aom_free(cpi->td.mb.txfm_search_info.txb_rd_records);
     cpi->td.mb.txfm_search_info.txb_rd_records = NULL;
   }
+  for (int plane = 0; plane < num_planes; plane++) {
+    if (cpi->td.mb.plane[plane].src_diff) {
+      aom_free(cpi->td.mb.plane[plane].src_diff);
+      cpi->td.mb.plane[plane].src_diff = NULL;
+    }
+  }
+  aom_free(cpi->td.mb.e_mbd.seg_mask);
+  cpi->td.mb.e_mbd.seg_mask = NULL;
+  aom_free(cpi->td.mb.winner_mode_stats);
+  cpi->td.mb.winner_mode_stats = NULL;
 }
 
 // Set the relative distance of a reference frame w.r.t. current frame
diff --git a/av1/encoder/ethread.c b/av1/encoder/ethread.c
index efa5ffc..d5816bf 100644
--- a/av1/encoder/ethread.c
+++ b/av1/encoder/ethread.c
@@ -792,6 +792,17 @@
         aom_free(thread_data->td->mb.dv_costs);
       }
     }
+    const int num_planes = av1_num_planes(&cpi->common);
+    for (int plane = 0; plane < num_planes; plane++) {
+      if (thread_data->td->mb.plane[plane].src_diff) {
+        aom_free(thread_data->td->mb.plane[plane].src_diff);
+        thread_data->td->mb.plane[plane].src_diff = NULL;
+      }
+    }
+    aom_free(thread_data->td->mb.e_mbd.seg_mask);
+    thread_data->td->mb.e_mbd.seg_mask = NULL;
+    aom_free(thread_data->td->mb.winner_mode_stats);
+    thread_data->td->mb.winner_mode_stats = NULL;
 
     // Accumulate counters.
     if (i > 0) {
@@ -857,6 +868,29 @@
                sizeof(IntraBCMVCosts));
       }
     }
+    const int num_planes = av1_num_planes(&cpi->common);
+    for (int plane = 0; plane < num_planes; plane++) {
+      const int subsampling_xy =
+          plane ? cm->seq_params.subsampling_x + cm->seq_params.subsampling_y
+                : 0;
+      const int sb_size = MAX_SB_SQUARE >> subsampling_xy;
+      CHECK_MEM_ERROR(
+          cm, thread_data->td->mb.plane[plane].src_diff,
+          (int16_t *)aom_memalign(
+              32,
+              sizeof(*thread_data->td->mb.plane[plane].src_diff) * sb_size));
+    }
+    CHECK_MEM_ERROR(cm, thread_data->td->mb.e_mbd.seg_mask,
+                    (uint8_t *)aom_memalign(
+                        16, 2 * MAX_SB_SQUARE *
+                                sizeof(thread_data->td->mb.e_mbd.seg_mask[0])));
+    const int winner_mode_count = frame_is_intra_only(cm)
+                                      ? MAX_WINNER_MODE_COUNT_INTRA
+                                      : MAX_WINNER_MODE_COUNT_INTER;
+    CHECK_MEM_ERROR(cm, thread_data->td->mb.winner_mode_stats,
+                    (WinnerModeStats *)aom_malloc(
+                        winner_mode_count *
+                        sizeof(thread_data->td->mb.winner_mode_stats[0])));
     // Reset cyclic refresh counters.
     av1_init_cyclic_refresh_counters(&thread_data->td->mb);
 
@@ -921,6 +955,30 @@
                sizeof(IntraBCMVCosts));
       }
     }
+
+    const int num_planes = av1_num_planes(&cpi->common);
+    for (int plane = 0; plane < num_planes; plane++) {
+      const int subsampling_xy =
+          plane ? cm->seq_params.subsampling_x + cm->seq_params.subsampling_y
+                : 0;
+      const int sb_size = MAX_SB_SQUARE >> subsampling_xy;
+      CHECK_MEM_ERROR(
+          cm, thread_data->td->mb.plane[plane].src_diff,
+          (int16_t *)aom_memalign(
+              32,
+              sizeof(*thread_data->td->mb.plane[plane].src_diff) * sb_size));
+    }
+    CHECK_MEM_ERROR(cm, thread_data->td->mb.e_mbd.seg_mask,
+                    (uint8_t *)aom_memalign(
+                        16, 2 * MAX_SB_SQUARE *
+                                sizeof(thread_data->td->mb.e_mbd.seg_mask[0])));
+    const int winner_mode_count = frame_is_intra_only(cm)
+                                      ? MAX_WINNER_MODE_COUNT_INTRA
+                                      : MAX_WINNER_MODE_COUNT_INTER;
+    CHECK_MEM_ERROR(cm, thread_data->td->mb.winner_mode_stats,
+                    (WinnerModeStats *)aom_malloc(
+                        winner_mode_count *
+                        sizeof(thread_data->td->mb.winner_mode_stats[0])));
     if (!cpi->sf.rt_sf.use_nonrd_pick_mode) {
       CHECK_MEM_ERROR(cm, thread_data->td->mb.txfm_search_info.txb_rd_records,
                       (TxbRdRecords *)aom_malloc(sizeof(TxbRdRecords)));
@@ -1218,6 +1276,17 @@
     if (thread_data->td->mb.txfm_search_info.txb_rd_records) {
       aom_free(thread_data->td->mb.txfm_search_info.txb_rd_records);
     }
+    const int num_planes = av1_num_planes(&cpi->common);
+    for (int plane = 0; plane < num_planes; plane++) {
+      if (thread_data->td->mb.plane[plane].src_diff) {
+        aom_free(thread_data->td->mb.plane[plane].src_diff);
+        thread_data->td->mb.plane[plane].src_diff = NULL;
+      }
+    }
+    aom_free(thread_data->td->mb.e_mbd.seg_mask);
+    thread_data->td->mb.e_mbd.seg_mask = NULL;
+    aom_free(thread_data->td->mb.winner_mode_stats);
+    thread_data->td->mb.winner_mode_stats = NULL;
   }
 }
 
diff --git a/av1/encoder/firstpass.c b/av1/encoder/firstpass.c
index c5fad6f..08fc498 100644
--- a/av1/encoder/firstpass.c
+++ b/av1/encoder/firstpass.c
@@ -984,6 +984,16 @@
   AV1_COMMON *const cm = &cpi->common;
   const int tile_cols = cm->tiles.cols;
   const int tile_rows = cm->tiles.rows;
+  const int num_planes = av1_num_planes(&cpi->common);
+  for (int plane = 0; plane < num_planes; plane++) {
+    const int subsampling_xy =
+        plane ? cm->seq_params.subsampling_x + cm->seq_params.subsampling_y : 0;
+    const int sb_size = MAX_SB_SQUARE >> subsampling_xy;
+    CHECK_MEM_ERROR(
+        cm, cpi->td.mb.plane[plane].src_diff,
+        (int16_t *)aom_memalign(
+            32, sizeof(*cpi->td.mb.plane[plane].src_diff) * sb_size));
+  }
   for (int tile_row = 0; tile_row < tile_rows; ++tile_row) {
     for (int tile_col = 0; tile_col < tile_cols; ++tile_col) {
       TileDataEnc *const tile_data =
@@ -991,6 +1001,12 @@
       first_pass_tile(cpi, &cpi->td, tile_data, fp_block_size);
     }
   }
+  for (int plane = 0; plane < num_planes; plane++) {
+    if (cpi->td.mb.plane[plane].src_diff) {
+      aom_free(cpi->td.mb.plane[plane].src_diff);
+      cpi->td.mb.plane[plane].src_diff = NULL;
+    }
+  }
 }
 
 void av1_first_pass_row(AV1_COMP *cpi, ThreadData *td, TileDataEnc *tile_data,
diff --git a/av1/encoder/intra_mode_search.c b/av1/encoder/intra_mode_search.c
index 0fddd65..8b310a7 100644
--- a/av1/encoder/intra_mode_search.c
+++ b/av1/encoder/intra_mode_search.c
@@ -1117,7 +1117,7 @@
   set_mode_eval_params(cpi, x, MODE_EVAL);
 
   MB_MODE_INFO best_mbmi = *mbmi;
-  av1_zero(x->winner_mode_stats);
+  av1_zero_array(x->winner_mode_stats, MAX_WINNER_MODE_COUNT_INTRA);
   x->winner_mode_count = 0;
 
   // Searches the intra-modes except for intrabc, palette, and filter_intra.
diff --git a/av1/encoder/rdopt.c b/av1/encoder/rdopt.c
index 81c57da..8cea21b 100644
--- a/av1/encoder/rdopt.c
+++ b/av1/encoder/rdopt.c
@@ -5336,7 +5336,7 @@
 #endif  // !CONFIG_REALTIME_ONLY
 
   // Initialize best mode stats for winner mode processing
-  av1_zero(x->winner_mode_stats);
+  av1_zero_array(x->winner_mode_stats, MAX_WINNER_MODE_COUNT_INTER);
   x->winner_mode_count = 0;
   store_winner_mode_stats(&cpi->common, x, mbmi, NULL, NULL, NULL, THR_INVALID,
                           NULL, bsize, best_rd_so_far,