Fix memory leak in CDEF multi-threading

In multi-threaded scenario, corrected the
de-allocation of CDEF buffers to be based on
number of allocated workers.

BUG=chromium:1321388

Change-Id: I07579ea4d43875a470beb80316fbdf83a9df47ac
diff --git a/av1/av1_dx_iface.c b/av1/av1_dx_iface.c
index d6d147b..cb5c6e3 100644
--- a/av1/av1_dx_iface.c
+++ b/av1/av1_dx_iface.c
@@ -121,8 +121,7 @@
     aom_free(pbi->common.tpl_mvs);
     pbi->common.tpl_mvs = NULL;
     av1_remove_common(&frame_worker_data->pbi->common);
-    av1_free_cdef_buffers(&pbi->common, &pbi->cdef_worker, &pbi->cdef_sync,
-                          pbi->num_workers);
+    av1_free_cdef_buffers(&pbi->common, &pbi->cdef_worker, &pbi->cdef_sync);
     av1_free_cdef_sync(&pbi->cdef_sync);
     av1_free_restoration_buffers(&pbi->common);
     av1_decoder_remove(pbi);
diff --git a/av1/common/alloccommon.c b/av1/common/alloccommon.c
index cbeda00..5cf6c0f 100644
--- a/av1/common/alloccommon.c
+++ b/av1/common/alloccommon.c
@@ -110,7 +110,7 @@
 
 void av1_free_cdef_buffers(AV1_COMMON *const cm,
                            AV1CdefWorkerData **cdef_worker,
-                           AV1CdefSync *cdef_sync, int num_workers) {
+                           AV1CdefSync *cdef_sync) {
   CdefInfo *cdef_info = &cm->cdef_info;
   const int num_mi_rows = cdef_info->allocated_mi_rows;
 
@@ -121,16 +121,17 @@
   // De-allocation of column buffer & source buffer (worker_0).
   free_cdef_bufs(cdef_info->colbuf, &cdef_info->srcbuf);
 
-  if (num_workers < 2) return;
+  free_cdef_row_sync(&cdef_sync->cdef_row_mt, num_mi_rows);
+
+  if (cdef_info->allocated_num_workers < 2) return;
   if (*cdef_worker != NULL) {
-    for (int idx = num_workers - 1; idx >= 1; idx--) {
+    for (int idx = cdef_info->allocated_num_workers - 1; idx >= 1; idx--) {
       // De-allocation of column buffer & source buffer for remaining workers.
       free_cdef_bufs((*cdef_worker)[idx].colbuf, &(*cdef_worker)[idx].srcbuf);
     }
     aom_free(*cdef_worker);
     *cdef_worker = NULL;
   }
-  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,
@@ -237,6 +238,9 @@
       // 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);
+
+      aom_free(*cdef_worker);
+      *cdef_worker = NULL;
     } else if (num_workers > 1) {
       // Free src and column buffers for remaining workers in case of
       // reallocation
diff --git a/av1/common/alloccommon.h b/av1/common/alloccommon.h
index 6360c6f..fc4a8ba 100644
--- a/av1/common/alloccommon.h
+++ b/av1/common/alloccommon.h
@@ -47,7 +47,7 @@
                             int init_worker);
 void av1_free_cdef_buffers(struct AV1Common *const cm,
                            struct AV1CdefWorker **cdef_worker,
-                           struct AV1CdefSyncData *cdef_sync, int num_workers);
+                           struct AV1CdefSyncData *cdef_sync);
 void av1_alloc_restoration_buffers(struct AV1Common *cm);
 void av1_free_restoration_buffers(struct AV1Common *cm);
 
diff --git a/av1/encoder/encoder_alloc.h b/av1/encoder/encoder_alloc.h
index a94a572..eec0903 100644
--- a/av1/encoder/encoder_alloc.h
+++ b/av1/encoder/encoder_alloc.h
@@ -261,10 +261,8 @@
 #endif
 
   if (!is_stat_generation_stage(cpi)) {
-    int num_cdef_workers =
-        av1_get_num_mod_workers_for_alloc(&cpi->ppi->p_mt_info, MOD_CDEF);
     av1_free_cdef_buffers(cm, &cpi->ppi->p_mt_info.cdef_worker,
-                          &cpi->mt_info.cdef_sync, num_cdef_workers);
+                          &cpi->mt_info.cdef_sync);
   }
 
   aom_free_frame_buffer(&cpi->trial_frame_rst);