Refactor motion field mv buffer

Reduce the needed memory space for motion field estimation.

Change-Id: I9caa228aa804a06370a34715a1c3463564d8d86b
diff --git a/av1/av1_dx_iface.c b/av1/av1_dx_iface.c
index c2f433d..1f0bb65 100644
--- a/av1/av1_dx_iface.c
+++ b/av1/av1_dx_iface.c
@@ -135,6 +135,10 @@
       pthread_mutex_destroy(&frame_worker_data->stats_mutex);
       pthread_cond_destroy(&frame_worker_data->stats_cond);
 #endif
+#if CONFIG_MFMV
+      aom_free(frame_worker_data->pbi->common.tpl_mvs);
+      frame_worker_data->pbi->common.tpl_mvs = NULL;
+#endif
       aom_free(frame_worker_data);
     }
 #if CONFIG_MULTITHREAD
diff --git a/av1/common/alloccommon.c b/av1/common/alloccommon.c
index fd63568..cc7b665 100644
--- a/av1/common/alloccommon.c
+++ b/av1/common/alloccommon.c
@@ -134,10 +134,6 @@
     }
     aom_free(pool->frame_bufs[i].mvs);
     pool->frame_bufs[i].mvs = NULL;
-#if CONFIG_MFMV
-    aom_free(pool->frame_bufs[i].tpl_mvs);
-    pool->frame_bufs[i].tpl_mvs = NULL;
-#endif
     aom_free_frame_buffer(&pool->frame_bufs[i].buf);
 #if CONFIG_HASH_ME
     av1_hash_table_destroy(&pool->frame_bufs[i].hash_table);
diff --git a/av1/common/mvref_common.c b/av1/common/mvref_common.c
index 108b70f..354c3b6 100644
--- a/av1/common/mvref_common.c
+++ b/av1/common/mvref_common.c
@@ -544,8 +544,7 @@
     return coll_blk_count;
 
   const TPL_MV_REF *prev_frame_mvs =
-      cm->cur_frame->tpl_mvs +
-      ((mi_row + mi_pos.row) >> 1) * (cm->mi_stride >> 1) +
+      cm->tpl_mvs + ((mi_row + mi_pos.row) >> 1) * (cm->mi_stride >> 1) +
       ((mi_col + mi_pos.col) >> 1);
 
   MV_REFERENCE_FRAME rf[2];
@@ -1648,7 +1647,7 @@
   int lst2_frame_index = 0, lst3_frame_index = 0;
   int bwd_frame_index = 0, alt2_frame_index = 0;
 #endif
-  TPL_MV_REF *tpl_mvs_base = cm->cur_frame->tpl_mvs;
+  TPL_MV_REF *tpl_mvs_base = cm->tpl_mvs;
 
   for (int ref_frame = 0; ref_frame < INTER_REFS_PER_FRAME; ++ref_frame) {
     int size = ((cm->mi_rows + MAX_MIB_SIZE) >> 1) * (cm->mi_stride >> 1);
diff --git a/av1/common/onyxc_int.h b/av1/common/onyxc_int.h
index 3627d1a..614f0d4 100644
--- a/av1/common/onyxc_int.h
+++ b/av1/common/onyxc_int.h
@@ -141,9 +141,6 @@
 #endif
 #endif  // CONFIG_FRAME_MARKER
 
-#if CONFIG_MFMV
-  TPL_MV_REF *tpl_mvs;
-#endif
   MV_REF *mvs;
   int mi_rows;
   int mi_cols;
@@ -547,6 +544,9 @@
 #if CONFIG_ADAPT_SCAN
   int use_adapt_scan;
 #endif
+#if CONFIG_MFMV
+  TPL_MV_REF *tpl_mvs;
+#endif
 } AV1_COMMON;
 
 // TODO(hkuang): Don't need to lock the whole pool after implementing atomic
@@ -642,15 +642,18 @@
         cm, buf->mvs,
         (MV_REF *)aom_calloc(cm->mi_rows * cm->mi_cols, sizeof(*buf->mvs)));
 #endif  // CONFIG_TMV
+  }
 
 #if CONFIG_MFMV
-    aom_free(buf->tpl_mvs);
-    CHECK_MEM_ERROR(cm, buf->tpl_mvs, (TPL_MV_REF *)aom_calloc(
-                                          ((cm->mi_rows + MAX_MIB_SIZE) >> 1) *
-                                              (cm->mi_stride >> 1),
-                                          sizeof(*buf->tpl_mvs)));
-#endif
+  if (cm->tpl_mvs == NULL || buf->mi_rows < cm->mi_rows ||
+      buf->mi_cols < cm->mi_cols) {
+    aom_free(cm->tpl_mvs);
+    CHECK_MEM_ERROR(cm, cm->tpl_mvs, (TPL_MV_REF *)aom_calloc(
+                                         ((cm->mi_rows + MAX_MIB_SIZE) >> 1) *
+                                             (cm->mi_stride >> 1),
+                                         sizeof(*cm->tpl_mvs)));
   }
+#endif
 }
 
 #if CONFIG_VAR_REFS
diff --git a/av1/encoder/encoder.c b/av1/encoder/encoder.c
index 856ef10..ecb41ff 100644
--- a/av1/encoder/encoder.c
+++ b/av1/encoder/encoder.c
@@ -552,6 +552,11 @@
   cpi->td.mb.mask_buf = NULL;
 #endif
 
+#if CONFIG_MFMV
+  aom_free(cm->tpl_mvs);
+  cm->tpl_mvs = NULL;
+#endif
+
   av1_free_ref_frame_buffers(cm->buffer_pool);
 #if CONFIG_LV_MAP
   av1_free_txb_buf(cpi);