Abstract malloc/dealloc calls of members in MACROBLOCK struct

src_diff, seg_mask, winner_mode_stats and TxbRdRecords are part
of MACROBLOCK structure. This patch will abstract allocation and
deallocation of these buffers into a function.

Change-Id: I44fbcb7c3604e83e17a807e97300d4662bf59ef3
diff --git a/av1/encoder/encodeframe.c b/av1/encoder/encodeframe.c
index 6a68b86..63e3158 100644
--- a/av1/encoder/encodeframe.c
+++ b/av1/encoder/encodeframe.c
@@ -1044,28 +1044,7 @@
   if (cpi->allocated_tiles < tile_cols * tile_rows) av1_alloc_tile_data(cpi);
 
   av1_init_tile_data(cpi);
-  if (!cpi->sf.rt_sf.use_nonrd_pick_mode) {
-    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])));
+  av1_alloc_mb_data(cm, mb, cpi->sf.rt_sf.use_nonrd_pick_mode);
 
   for (tile_row = 0; tile_row < tile_rows; ++tile_row) {
     for (tile_col = 0; tile_col < tile_cols; ++tile_col) {
@@ -1089,20 +1068,7 @@
     }
   }
 
-  if (cpi->td.mb.txfm_search_info.txb_rd_records) {
-    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;
+  av1_dealloc_mb_data(cm, mb);
 }
 
 // Set the relative distance of a reference frame w.r.t. current frame
diff --git a/av1/encoder/encodeframe_utils.h b/av1/encoder/encodeframe_utils.h
index 7bdfad5..cd024f2 100644
--- a/av1/encoder/encodeframe_utils.h
+++ b/av1/encoder/encodeframe_utils.h
@@ -13,6 +13,7 @@
 #define AOM_AV1_ENCODER_ENCODEFRAME_UTILS_H_
 
 #include "aom_ports/aom_timer.h"
+#include "aom_ports/system_state.h"
 
 #include "av1/common/reconinter.h"
 
@@ -335,6 +336,56 @@
                            const TileInfo *const tile_info, const int mi_row,
                            const int mi_col);
 
+static AOM_INLINE void av1_dealloc_mb_data(struct AV1Common *cm,
+                                           struct macroblock *mb) {
+  if (mb->txfm_search_info.txb_rd_records) {
+    aom_free(mb->txfm_search_info.txb_rd_records);
+    mb->txfm_search_info.txb_rd_records = NULL;
+  }
+  const int num_planes = av1_num_planes(cm);
+  for (int plane = 0; plane < num_planes; plane++) {
+    if (mb->plane[plane].src_diff) {
+      aom_free(mb->plane[plane].src_diff);
+      mb->plane[plane].src_diff = NULL;
+    }
+  }
+  if (mb->e_mbd.seg_mask) {
+    aom_free(mb->e_mbd.seg_mask);
+    mb->e_mbd.seg_mask = NULL;
+  }
+  if (mb->winner_mode_stats) {
+    aom_free(mb->winner_mode_stats);
+    mb->winner_mode_stats = NULL;
+  }
+}
+
+static AOM_INLINE void av1_alloc_mb_data(struct AV1Common *cm,
+                                         struct macroblock *mb,
+                                         int use_nonrd_pick_mode) {
+  if (!use_nonrd_pick_mode) {
+    mb->txfm_search_info.txb_rd_records =
+        (TxbRdRecords *)aom_malloc(sizeof(TxbRdRecords));
+  }
+  const int num_planes = av1_num_planes(cm);
+  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])));
+}
+
 // This function will compute the number of reference frames to be disabled
 // based on selective_ref_frame speed feature.
 static AOM_INLINE unsigned int get_num_refs_to_disable(
diff --git a/av1/encoder/ethread.c b/av1/encoder/ethread.c
index d5816bf..0d5d383 100644
--- a/av1/encoder/ethread.c
+++ b/av1/encoder/ethread.c
@@ -14,6 +14,7 @@
 #include "av1/encoder/encodeframe.h"
 #include "av1/encoder/encoder.h"
 #include "av1/encoder/encoder_alloc.h"
+#include "av1/encoder/encodeframe_utils.h"
 #include "av1/encoder/ethread.h"
 #if !CONFIG_REALTIME_ONLY
 #include "av1/encoder/firstpass.h"
@@ -780,10 +781,6 @@
         !frame_is_intra_only(&cpi->common))
       av1_accumulate_cyclic_refresh_counters(cpi->cyclic_refresh,
                                              &thread_data->td->mb);
-    if (thread_data->td->mb.txfm_search_info.txb_rd_records) {
-      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) {
       if (cpi->oxcf.cost_upd_freq.mv < COST_UPD_OFF) {
         aom_free(thread_data->td->mb.mv_costs);
@@ -792,17 +789,7 @@
         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;
+    av1_dealloc_mb_data(&cpi->common, &thread_data->td->mb);
 
     // Accumulate counters.
     if (i > 0) {
@@ -868,37 +855,12 @@
                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])));
+    av1_alloc_mb_data(cm, &thread_data->td->mb,
+                      cpi->sf.rt_sf.use_nonrd_pick_mode);
+
     // Reset cyclic refresh counters.
     av1_init_cyclic_refresh_counters(&thread_data->td->mb);
 
-    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)));
-    }
-
     if (thread_data->td->counts != &cpi->counts) {
       memcpy(thread_data->td->counts, &cpi->counts, sizeof(cpi->counts));
     }
@@ -956,33 +918,8 @@
       }
     }
 
-    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)));
-    }
+    av1_alloc_mb_data(cm, &thread_data->td->mb,
+                      cpi->sf.rt_sf.use_nonrd_pick_mode);
   }
 }
 #endif
@@ -1273,20 +1210,7 @@
         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);
-    }
-    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;
+    av1_dealloc_mb_data(cm, &thread_data->td->mb);
   }
 }