Free mv_costs and dv_costs in encoder_destroy()

This CL frees 'mv_costs' and 'dv_costs' in
free_thread_data() to prevent memory leaks in case of
early termination. In order to avoid double free error
in case of 'x->mv_costs' and 'x->dv_costs', the allocation
and freeing of the same is done using newly introduced
'mv_costs_alloc' and 'dv_costs_alloc' pointers in ThreadData
structure. 'x->mv_costs'and 'x->dv_costs' are initialized
appropriately.

Bug: aomedia:3276
Change-Id: I0954513c9c0bdb216cd4fbeaf75533552472132f
diff --git a/av1/encoder/encoder.h b/av1/encoder/encoder.h
index 881cdc3..735668c 100644
--- a/av1/encoder/encoder.h
+++ b/av1/encoder/encoder.h
@@ -1442,6 +1442,8 @@
 
 typedef struct ThreadData {
   MACROBLOCK mb;
+  MvCosts *mv_costs_alloc;
+  IntraBCMVCosts *dv_costs_alloc;
   RD_COUNTS rd_counts;
   FRAME_COUNTS *counts;
   PC_TREE_SHARED_BUFFERS shared_coeff_buf;
diff --git a/av1/encoder/encoder_alloc.h b/av1/encoder/encoder_alloc.h
index 15fd075..30c3b86 100644
--- a/av1/encoder/encoder_alloc.h
+++ b/av1/encoder/encoder_alloc.h
@@ -65,12 +65,14 @@
 
   if (!is_stat_generation_stage(cpi)) av1_alloc_txb_buf(cpi);
 
-  aom_free(cpi->td.mb.mv_costs);
-  cpi->td.mb.mv_costs = NULL;
-  // Avoid the memory allocation of 'mv_costs' for allintra encoding mode.
+  aom_free(cpi->td.mv_costs_alloc);
+  cpi->td.mv_costs_alloc = NULL;
+  // Avoid the memory allocation of 'mv_costs_alloc' for allintra encoding
+  // mode.
   if (cpi->oxcf.kf_cfg.key_freq_max != 0) {
-    CHECK_MEM_ERROR(cm, cpi->td.mb.mv_costs,
-                    (MvCosts *)aom_calloc(1, sizeof(MvCosts)));
+    CHECK_MEM_ERROR(cm, cpi->td.mv_costs_alloc,
+                    (MvCosts *)aom_calloc(1, sizeof(*cpi->td.mv_costs_alloc)));
+    cpi->td.mb.mv_costs = cpi->td.mv_costs_alloc;
   }
 
   av1_setup_shared_coeff_buffer(cm->seq_params, &cpi->td.shared_coeff_buf,
@@ -222,10 +224,10 @@
 
   release_obmc_buffers(&cpi->td.mb.obmc_buffer);
 
-  aom_free(cpi->td.mb.mv_costs);
-  cpi->td.mb.mv_costs = NULL;
-  aom_free(cpi->td.mb.dv_costs);
-  cpi->td.mb.dv_costs = NULL;
+  aom_free(cpi->td.mv_costs_alloc);
+  cpi->td.mv_costs_alloc = NULL;
+  aom_free(cpi->td.dv_costs_alloc);
+  cpi->td.dv_costs_alloc = NULL;
 
   for (int i = 0; i < 2; i++)
     for (int j = 0; j < 2; j++) {
@@ -449,6 +451,10 @@
         thread_data->td->hash_value_buffer[x][y] = NULL;
       }
     }
+    aom_free(thread_data->td->mv_costs_alloc);
+    thread_data->td->mv_costs_alloc = NULL;
+    aom_free(thread_data->td->dv_costs_alloc);
+    thread_data->td->dv_costs_alloc = NULL;
     aom_free(thread_data->td->counts);
     av1_free_pmc(thread_data->td->firstpass_ctx, num_planes);
     thread_data->td->firstpass_ctx = NULL;
diff --git a/av1/encoder/ethread.c b/av1/encoder/ethread.c
index b1c3ac8..24538b4 100644
--- a/av1/encoder/ethread.c
+++ b/av1/encoder/ethread.c
@@ -1470,10 +1470,12 @@
       // Keep these conditional expressions in sync with the corresponding ones
       // in prepare_enc_workers().
       if (cpi->sf.inter_sf.mv_cost_upd_level != INTERNAL_COST_UPD_OFF) {
-        aom_free(thread_data->td->mb.mv_costs);
+        aom_free(thread_data->td->mv_costs_alloc);
+        thread_data->td->mv_costs_alloc = NULL;
       }
       if (cpi->sf.intra_sf.dv_cost_upd_level != INTERNAL_COST_UPD_OFF) {
-        aom_free(thread_data->td->mb.dv_costs);
+        aom_free(thread_data->td->dv_costs_alloc);
+        thread_data->td->dv_costs_alloc = NULL;
       }
     }
     av1_dealloc_mb_data(&thread_data->td->mb, av1_num_planes(&cpi->common));
@@ -1540,8 +1542,10 @@
       // Keep these conditional expressions in sync with the corresponding ones
       // in accumulate_counters_enc_workers().
       if (cpi->sf.inter_sf.mv_cost_upd_level != INTERNAL_COST_UPD_OFF) {
-        CHECK_MEM_ERROR(cm, thread_data->td->mb.mv_costs,
-                        (MvCosts *)aom_malloc(sizeof(MvCosts)));
+        CHECK_MEM_ERROR(
+            cm, thread_data->td->mv_costs_alloc,
+            (MvCosts *)aom_malloc(sizeof(*thread_data->td->mv_costs_alloc)));
+        thread_data->td->mb.mv_costs = thread_data->td->mv_costs_alloc;
         memcpy(thread_data->td->mb.mv_costs, cpi->td.mb.mv_costs,
                sizeof(MvCosts));
       }
@@ -1551,8 +1555,10 @@
         // aom_free() call for the same.
         thread_data->td->mb.dv_costs = NULL;
         if (av1_need_dv_costs(cpi)) {
-          CHECK_MEM_ERROR(cm, thread_data->td->mb.dv_costs,
-                          (IntraBCMVCosts *)aom_malloc(sizeof(IntraBCMVCosts)));
+          CHECK_MEM_ERROR(cm, thread_data->td->dv_costs_alloc,
+                          (IntraBCMVCosts *)aom_malloc(
+                              sizeof(*thread_data->td->dv_costs_alloc)));
+          thread_data->td->mb.dv_costs = thread_data->td->dv_costs_alloc;
           memcpy(thread_data->td->mb.dv_costs, cpi->td.mb.dv_costs,
                  sizeof(IntraBCMVCosts));
         }
diff --git a/av1/encoder/rd.c b/av1/encoder/rd.c
index 8bc7d1b..c2d76e7 100644
--- a/av1/encoder/rd.c
+++ b/av1/encoder/rd.c
@@ -809,10 +809,11 @@
 
   // Frame level dv cost update
   if (av1_need_dv_costs(cpi)) {
-    if (cpi->td.mb.dv_costs == NULL) {
+    if (cpi->td.dv_costs_alloc == NULL) {
       CHECK_MEM_ERROR(
-          cm, cpi->td.mb.dv_costs,
-          (IntraBCMVCosts *)aom_malloc(sizeof(*cpi->td.mb.dv_costs)));
+          cm, cpi->td.dv_costs_alloc,
+          (IntraBCMVCosts *)aom_malloc(sizeof(*cpi->td.dv_costs_alloc)));
+      cpi->td.mb.dv_costs = cpi->td.dv_costs_alloc;
     }
     av1_fill_dv_costs(&cm->fc->ndvc, x->dv_costs);
   }