Refactor cdef buffers allocation

Refactor cdef buffers allocation logic for better readability

BUG=aomedia:3037

Change-Id: Ib1a1fccad8a14f4955b439e37d5505089b3ffb5e
diff --git a/av1/common/alloccommon.c b/av1/common/alloccommon.c
index fc8b8bb..8624255 100644
--- a/av1/common/alloccommon.c
+++ b/av1/common/alloccommon.c
@@ -53,6 +53,35 @@
   }
 }
 
+static INLINE void free_cdef_linebuf_conditional(
+    AV1_COMMON *const cm, const size_t *new_linebuf_size) {
+  CdefInfo *cdef_info = &cm->cdef_info;
+  for (int plane = 0; plane < MAX_MB_PLANE; plane++) {
+    if (new_linebuf_size[plane] != cdef_info->allocated_linebuf_size[plane]) {
+      aom_free(cdef_info->linebuf[plane]);
+      cdef_info->linebuf[plane] = NULL;
+    }
+  }
+}
+
+static INLINE void free_cdef_bufs_conditional(AV1_COMMON *const cm,
+                                              uint16_t **colbuf,
+                                              uint16_t **srcbuf,
+                                              const size_t *new_colbuf_size,
+                                              const size_t new_srcbuf_size) {
+  CdefInfo *cdef_info = &cm->cdef_info;
+  if (new_srcbuf_size != cdef_info->allocated_srcbuf_size) {
+    aom_free(*srcbuf);
+    *srcbuf = NULL;
+  }
+  for (int plane = 0; plane < MAX_MB_PLANE; plane++) {
+    if (new_colbuf_size[plane] != cdef_info->allocated_colbuf_size[plane]) {
+      aom_free(colbuf[plane]);
+      colbuf[plane] = NULL;
+    }
+  }
+}
+
 static INLINE void free_cdef_bufs(uint16_t **colbuf, uint16_t **srcbuf) {
   aom_free(*srcbuf);
   *srcbuf = NULL;
@@ -104,21 +133,27 @@
   free_cdef_row_sync(&cdef_sync->cdef_row_mt, num_mi_rows);
 }
 
+static INLINE void alloc_cdef_linebuf(AV1_COMMON *const cm, uint16_t **linebuf,
+                                      const int num_planes) {
+  CdefInfo *cdef_info = &cm->cdef_info;
+  for (int plane = 0; plane < num_planes; plane++) {
+    if (linebuf[plane] == NULL)
+      CHECK_MEM_ERROR(cm, linebuf[plane],
+                      aom_malloc(cdef_info->allocated_linebuf_size[plane]));
+  }
+}
+
 static INLINE void alloc_cdef_bufs(AV1_COMMON *const cm, uint16_t **colbuf,
                                    uint16_t **srcbuf, const int num_planes) {
+  CdefInfo *cdef_info = &cm->cdef_info;
   if (*srcbuf == NULL)
     CHECK_MEM_ERROR(cm, *srcbuf,
-                    aom_memalign(16, sizeof(**srcbuf) * CDEF_INBUF_SIZE));
+                    aom_memalign(16, cdef_info->allocated_srcbuf_size));
 
   for (int plane = 0; plane < num_planes; plane++) {
-    const int shift = plane == AOM_PLANE_Y ? 0 : cm->seq_params->subsampling_x;
-    const int block_height =
-        (CDEF_BLOCKSIZE << (MI_SIZE_LOG2 - shift)) * 2 * CDEF_VBORDER;
-
     if (colbuf[plane] == NULL)
-      CHECK_MEM_ERROR(
-          cm, colbuf[plane],
-          aom_malloc(sizeof(*colbuf[plane]) * block_height * CDEF_HBORDER));
+      CHECK_MEM_ERROR(cm, colbuf[plane],
+                      aom_malloc(cdef_info->allocated_colbuf_size[plane]));
   }
 }
 
@@ -148,24 +183,18 @@
                             AV1CdefWorkerData **cdef_worker,
                             AV1CdefSync *cdef_sync, int num_workers) {
   const int num_planes = av1_num_planes(cm);
-  const int luma_stride =
-      ALIGN_POWER_OF_TWO(cm->mi_params.mi_cols << MI_SIZE_LOG2, 4);
-  CdefInfo *cdef_info = &cm->cdef_info;
+  size_t new_linebuf_size[MAX_MB_PLANE] = { 0 };
+  size_t new_colbuf_size[MAX_MB_PLANE] = { 0 };
+  size_t new_srcbuf_size = 0;
+  CdefInfo *const cdef_info = &cm->cdef_info;
   // Check for configuration change
-  const int is_sub_sampling_changed =
-      (cdef_info->allocated_subsampling_x != cm->seq_params->subsampling_x ||
-       cdef_info->allocated_subsampling_y != cm->seq_params->subsampling_y);
-  const int is_frame_scaled =
-      cdef_info->allocated_mi_cols != cm->mi_params.mi_cols;
-  const int is_cdef_flag_changed =
-      cdef_info->prev_cdef_enable_flag != cm->seq_params->enable_cdef;
-  const int is_large_scale_tile_changed =
-      cdef_info->prev_large_scale_tile_flag != cm->tiles.large_scale;
-  const int is_num_planes_changed = cdef_info->prev_num_planes != num_planes;
   const int num_mi_rows =
       (cm->mi_params.mi_rows + MI_SIZE_64X64 - 1) / MI_SIZE_64X64;
   const int is_num_workers_changed =
       cdef_info->allocated_num_workers != num_workers;
+  const int is_cdef_enabled =
+      cm->seq_params->enable_cdef && !cm->tiles.large_scale;
+
   // num-bufs=3 represents ping-pong buffers for top linebuf,
   // followed by bottom linebuf.
   // ping-pong is to avoid top linebuf over-write by consecutive row.
@@ -173,41 +202,62 @@
   if (num_workers > 1)
     num_bufs = (cm->mi_params.mi_rows + MI_SIZE_64X64 - 1) / MI_SIZE_64X64;
 
-  // TODO(vishnu): Simplify the below conditional logic based on linebuf_size.
-  if (is_frame_scaled || is_sub_sampling_changed || is_cdef_flag_changed ||
-      is_large_scale_tile_changed || is_num_planes_changed ||
-      is_num_workers_changed) {
-    av1_free_cdef_buffers(cm, cdef_worker, cdef_sync,
-                          cdef_info->allocated_num_workers);
-  } else if (cdef_info->allocated_mi_rows != num_mi_rows) {
-    free_cdef_row_sync(&cdef_sync->cdef_row_mt, cdef_info->allocated_mi_rows);
-  }
-
-  // Store configuration to check change in configuration
-  cdef_info->allocated_mi_cols = cm->mi_params.mi_cols;
-  cdef_info->allocated_mi_rows = num_mi_rows;
-  cdef_info->allocated_subsampling_x = cm->seq_params->subsampling_x;
-  cdef_info->allocated_subsampling_y = cm->seq_params->subsampling_y;
-  cdef_info->prev_cdef_enable_flag = cm->seq_params->enable_cdef;
-  cdef_info->prev_large_scale_tile_flag = cm->tiles.large_scale;
-  cdef_info->prev_num_planes = num_planes;
-  cdef_info->allocated_num_workers = num_workers;
-
-  if (!cm->seq_params->enable_cdef && cm->tiles.large_scale) return;
-
-  for (int plane = 0; plane < num_planes; plane++) {
-    if (cdef_info->linebuf[plane] == NULL) {
-      const int stride =
-          luma_stride >>
-          (plane == AOM_PLANE_Y ? 0 : cm->seq_params->subsampling_x);
-      CHECK_MEM_ERROR(cm, cdef_info->linebuf[plane],
-                      aom_malloc(sizeof(*cdef_info->linebuf) * num_bufs *
-                                 (CDEF_VBORDER << 1) * stride));
+  if (is_cdef_enabled) {
+    // Calculate src buffer size
+    new_srcbuf_size = sizeof(*cdef_info->srcbuf) * CDEF_INBUF_SIZE;
+    for (int plane = 0; plane < num_planes; plane++) {
+      const int shift =
+          plane == AOM_PLANE_Y ? 0 : cm->seq_params->subsampling_x;
+      // Calculate top and bottom line buffer size
+      const int luma_stride =
+          ALIGN_POWER_OF_TWO(cm->mi_params.mi_cols << MI_SIZE_LOG2, 4);
+      new_linebuf_size[plane] = sizeof(*cdef_info->linebuf) * num_bufs *
+                                (CDEF_VBORDER << 1) * (luma_stride >> shift);
+      // Calculate column buffer size
+      const int block_height =
+          (CDEF_BLOCKSIZE << (MI_SIZE_LOG2 - shift)) * 2 * CDEF_VBORDER;
+      new_colbuf_size[plane] =
+          sizeof(*cdef_info->colbuf[plane]) * block_height * CDEF_HBORDER;
     }
   }
 
+  // Free src, line and column buffers for worker 0 in case of reallocation
+  free_cdef_linebuf_conditional(cm, new_linebuf_size);
+  free_cdef_bufs_conditional(cm, cdef_info->colbuf, &cdef_info->srcbuf,
+                             new_colbuf_size, new_srcbuf_size);
+
+  if (*cdef_worker != NULL) {
+    if (is_num_workers_changed) {
+      // Free src and column buffers for remaining workers in case of change in
+      // num_workers
+      for (int idx = cdef_info->allocated_num_workers - 1; idx >= 1; idx--)
+        free_cdef_bufs((*cdef_worker)[idx].colbuf, &(*cdef_worker)[idx].srcbuf);
+    } else if (num_workers > 1) {
+      // Free src and column buffers for remaining workers in case of
+      // reallocation
+      for (int idx = num_workers - 1; idx >= 1; idx--)
+        free_cdef_bufs_conditional(cm, (*cdef_worker)[idx].colbuf,
+                                   &(*cdef_worker)[idx].srcbuf, new_colbuf_size,
+                                   new_srcbuf_size);
+    }
+  }
+
+  if (cdef_info->allocated_mi_rows != num_mi_rows)
+    free_cdef_row_sync(&cdef_sync->cdef_row_mt, cdef_info->allocated_mi_rows);
+
+  // Store allocated sizes for reallocation
+  cdef_info->allocated_srcbuf_size = new_srcbuf_size;
+  av1_copy(cdef_info->allocated_colbuf_size, new_colbuf_size);
+  av1_copy(cdef_info->allocated_linebuf_size, new_linebuf_size);
+  // Store configuration to check change in configuration
+  cdef_info->allocated_mi_rows = num_mi_rows;
+  cdef_info->allocated_num_workers = num_workers;
+
+  if (!is_cdef_enabled) return;
+
   // Memory allocation of column buffer & source buffer (worker_0).
   alloc_cdef_bufs(cm, cdef_info->colbuf, &cdef_info->srcbuf, num_planes);
+  alloc_cdef_linebuf(cm, cdef_info->linebuf, num_planes);
 
   if (num_workers < 2) return;
 
@@ -215,11 +265,10 @@
     CHECK_MEM_ERROR(cm, *cdef_worker,
                     aom_calloc(num_workers, sizeof(**cdef_worker)));
 
-  for (int idx = num_workers - 1; idx >= 1; idx--) {
-    // Memory allocation of column buffer & source buffer for remaining workers.
+  // Memory allocation of column buffer & source buffer for remaining workers.
+  for (int idx = num_workers - 1; idx >= 1; idx--)
     alloc_cdef_bufs(cm, (*cdef_worker)[idx].colbuf, &(*cdef_worker)[idx].srcbuf,
                     num_planes);
-  }
 
   alloc_cdef_row_sync(cm, &cdef_sync->cdef_row_mt,
                       cdef_info->allocated_mi_rows);
diff --git a/av1/common/av1_common_int.h b/av1/common/av1_common_int.h
index f0970f9..9970332 100644
--- a/av1/common/av1_common_int.h
+++ b/av1/common/av1_common_int.h
@@ -192,23 +192,32 @@
 
 /*!\brief Parameters related to CDEF */
 typedef struct {
-  uint16_t *colbuf[MAX_MB_PLANE];         /*!< CDEF column line buffer */
-  uint16_t *linebuf[MAX_MB_PLANE];        /*!< CDEF top & bottom line buffer */
-  uint16_t *srcbuf;                       /*!< CDEF intermediate buffer */
-  int cdef_damping;                       /*!< CDEF damping factor */
-  int nb_cdef_strengths;                  /*!< Number of CDEF strength values */
-  int cdef_strengths[CDEF_MAX_STRENGTHS]; /*!< CDEF strength values for luma */
-  int cdef_uv_strengths[CDEF_MAX_STRENGTHS]; /*!< CDEF strength values for
-                                                chroma */
-  int cdef_bits;                 /*!< Number of CDEF strength values in bits */
-  int allocated_mi_cols;         /*!< Number of cols in the frame in 4 pixel */
-  int allocated_mi_rows;         /*!< Number of rows in the frame in 4 pixel */
-  int allocated_subsampling_x;   /*!< Chroma subsampling for x */
-  int allocated_subsampling_y;   /*!< Chroma subsampling for y */
-  uint8_t prev_cdef_enable_flag; /*!< CDEF on/off flag */
-  unsigned int prev_large_scale_tile_flag; /*!< Large scale tile on/off flag */
-  int prev_num_planes;                     /*!< Number of planes */
-  int allocated_num_workers;               /*!< Number of CDEF workers */
+  //! CDEF column line buffer
+  uint16_t *colbuf[MAX_MB_PLANE];
+  //! CDEF top & bottom line buffer
+  uint16_t *linebuf[MAX_MB_PLANE];
+  //! CDEF intermediate buffer
+  uint16_t *srcbuf;
+  //! CDEF column line buffer sizes
+  size_t allocated_colbuf_size[MAX_MB_PLANE];
+  //! CDEF top and bottom line buffer sizes
+  size_t allocated_linebuf_size[MAX_MB_PLANE];
+  //! CDEF intermediate buffer size
+  size_t allocated_srcbuf_size;
+  //! CDEF damping factor
+  int cdef_damping;
+  //! Number of CDEF strength values
+  int nb_cdef_strengths;
+  //! CDEF strength values for luma
+  int cdef_strengths[CDEF_MAX_STRENGTHS];
+  //! CDEF strength values for chroma
+  int cdef_uv_strengths[CDEF_MAX_STRENGTHS];
+  //! Number of CDEF strength values in bits
+  int cdef_bits;
+  //! Number of rows in the frame in 4 pixel
+  int allocated_mi_rows;
+  //! Number of CDEF workers
+  int allocated_num_workers;
 } CdefInfo;
 
 /*!\cond */