Refactor memory alloc/dealloc of global motion buffers

This CL refactors the memory allocations and deallocations of
global motion buffers to be consistent with other modules like
temporal filter and tpl.

Bug: aomedia:3276

Change-Id: I43bedfa1ece6401e8ad9643f81be49d0e6590b2e
diff --git a/av1/encoder/ethread.c b/av1/encoder/ethread.c
index 22679e9..fcc1023 100644
--- a/av1/encoder/ethread.c
+++ b/av1/encoder/ethread.c
@@ -2413,19 +2413,6 @@
   get_next_gm_job(cpi, frame_idx, *(cur_dir));
 }
 
-// Initializes inliers, num_inliers and segment_map.
-static AOM_INLINE void init_gm_thread_data(
-    const GlobalMotionInfo *gm_info, GlobalMotionThreadData *thread_data) {
-  for (int m = 0; m < RANSAC_NUM_MOTIONS; m++) {
-    MotionModel motion_params = thread_data->motion_models[m];
-    av1_zero(motion_params.params);
-    motion_params.num_inliers = 0;
-  }
-
-  av1_zero_array(thread_data->segment_map,
-                 gm_info->segment_map_w * gm_info->segment_map_h);
-}
-
 // Hook function for each thread in global motion multi-threading.
 static int gm_mt_worker_hook(void *arg1, void *unused) {
   (void)unused;
@@ -2436,8 +2423,7 @@
   MultiThreadInfo *mt_info = &cpi->mt_info;
   JobInfo *job_info = &mt_info->gm_sync.job_info;
   int thread_id = thread_data->thread_id;
-  GlobalMotionThreadData *gm_thread_data =
-      &mt_info->gm_sync.thread_data[thread_id];
+  GlobalMotionData *gm_thread_data = &thread_data->td->gm_data;
   int cur_dir = job_info->thread_id_to_dir[thread_id];
 #if CONFIG_MULTITHREAD
   pthread_mutex_t *gm_mt_mutex_ = mt_info->gm_sync.mutex_;
@@ -2464,8 +2450,6 @@
 
     if (ref_buf_idx == -1) break;
 
-    init_gm_thread_data(gm_info, gm_thread_data);
-
     // Compute global motion for the given ref_buf_idx.
     av1_compute_gm_for_valid_ref_frames(
         cpi, gm_info->ref_buf, ref_buf_idx, gm_thread_data->motion_models,
@@ -2511,6 +2495,9 @@
     } else {
       thread_data->td = thread_data->original_td;
     }
+
+    if (thread_data->td != &cpi->td)
+      gm_alloc_data(cpi, &thread_data->td->gm_data);
   }
 }
 
@@ -2537,69 +2524,28 @@
 }
 
 // Frees the memory allocated for each worker in global motion multi-threading.
-void av1_gm_dealloc(AV1GlobalMotionSync *gm_sync_data) {
-  if (gm_sync_data->thread_data != NULL) {
-    for (int j = 0; j < gm_sync_data->allocated_workers; j++) {
-      GlobalMotionThreadData *thread_data = &gm_sync_data->thread_data[j];
-      aom_free(thread_data->segment_map);
-
-      for (int m = 0; m < RANSAC_NUM_MOTIONS; m++)
-        aom_free(thread_data->motion_models[m].inliers);
-    }
-    aom_free(gm_sync_data->thread_data);
-  }
-}
-
-// Allocates memory for inliers and segment_map for each worker in global motion
-// multi-threading.
-static AOM_INLINE void gm_alloc(AV1_COMP *cpi, int num_workers) {
-  AV1_COMMON *cm = &cpi->common;
-  AV1GlobalMotionSync *gm_sync = &cpi->mt_info.gm_sync;
-  GlobalMotionInfo *gm_info = &cpi->gm_info;
-
-  gm_sync->allocated_workers = num_workers;
-  gm_sync->allocated_width = cpi->source->y_width;
-  gm_sync->allocated_height = cpi->source->y_height;
-
-  CHECK_MEM_ERROR(cm, gm_sync->thread_data,
-                  aom_malloc(sizeof(*gm_sync->thread_data) * num_workers));
-
-  for (int i = 0; i < num_workers; i++) {
-    GlobalMotionThreadData *thread_data = &gm_sync->thread_data[i];
-    CHECK_MEM_ERROR(
-        cm, thread_data->segment_map,
-        aom_malloc(sizeof(*thread_data->segment_map) * gm_info->segment_map_w *
-                   gm_info->segment_map_h));
-
-    for (int m = 0; m < RANSAC_NUM_MOTIONS; m++) {
-      CHECK_MEM_ERROR(
-          cm, thread_data->motion_models[m].inliers,
-          aom_malloc(sizeof(*thread_data->motion_models[m].inliers) * 2 *
-                     MAX_CORNERS));
-    }
+static AOM_INLINE void gm_dealloc_thread_data(AV1_COMP *cpi, int num_workers) {
+  MultiThreadInfo *mt_info = &cpi->mt_info;
+  for (int j = 0; j < num_workers; j++) {
+    EncWorkerData *thread_data = &mt_info->tile_thr_data[j];
+    ThreadData *td = thread_data->td;
+    if (td != &cpi->td) gm_dealloc_data(&td->gm_data);
   }
 }
 
 // Implements multi-threading for global motion.
 void av1_global_motion_estimation_mt(AV1_COMP *cpi) {
-  AV1GlobalMotionSync *gm_sync = &cpi->mt_info.gm_sync;
-  JobInfo *job_info = &gm_sync->job_info;
+  JobInfo *job_info = &cpi->mt_info.gm_sync.job_info;
 
   av1_zero(*job_info);
 
   int num_workers = compute_gm_workers(cpi);
 
-  if (num_workers > gm_sync->allocated_workers ||
-      cpi->source->y_width != gm_sync->allocated_width ||
-      cpi->source->y_height != gm_sync->allocated_height) {
-    av1_gm_dealloc(gm_sync);
-    gm_alloc(cpi, num_workers);
-  }
-
   assign_thread_to_dir(job_info->thread_id_to_dir, num_workers);
   prepare_gm_workers(cpi, gm_mt_worker_hook, num_workers);
   launch_workers(&cpi->mt_info, num_workers);
   sync_enc_workers(&cpi->mt_info, &cpi->common, num_workers);
+  gm_dealloc_thread_data(cpi, num_workers);
 }
 #endif  // !CONFIG_REALTIME_ONLY