Separate allocation of src_diff buffer for ST and MT encode paths

In fp_prepare_enc_workers(), during the copy of MACROBLOCK structure
from main thread (cpi->td.mb) to worker threads (thread_data->td->mb),
the thread-specific "src_diff" buffers get initialized with that of
the main thread. If the allocation of thread-specific "src_diff" buffer
fails, it will result in a double free error in free_thread_data() as
the buffer of main thread would have already been freed in
dealloc_compressor_data() during encoder_destroy().

This CL fixes this issue by allocating the 'src_diff' buffer
separately for ST encode in first_pass_tiles() and MT encode
in fp_prepare_enc_workers().

Bug: aomedia:3276
Change-Id: I4f7d99c814a87fade140a460914dea2d367666eb
diff --git a/av1/encoder/encodeframe_utils.c b/av1/encoder/encodeframe_utils.c
index 7654864..9498371 100644
--- a/av1/encoder/encodeframe_utils.c
+++ b/av1/encoder/encodeframe_utils.c
@@ -1758,6 +1758,11 @@
 
 void av1_alloc_src_diff_buf(const struct AV1Common *cm, struct macroblock *mb) {
   const int num_planes = av1_num_planes(cm);
+#ifndef NDEBUG
+  for (int plane = 0; plane < num_planes; ++plane) {
+    assert(!mb->plane[plane].src_diff);
+  }
+#endif
   for (int plane = 0; plane < num_planes; ++plane) {
     const int subsampling_xy =
         plane ? cm->seq_params->subsampling_x + cm->seq_params->subsampling_y
diff --git a/av1/encoder/ethread.c b/av1/encoder/ethread.c
index 8bf73e7..acd2436 100644
--- a/av1/encoder/ethread.c
+++ b/av1/encoder/ethread.c
@@ -1645,13 +1645,10 @@
       thread_data->td = &cpi->td;
     } else {
       thread_data->td = thread_data->original_td;
-    }
-
-    if (thread_data->td != &cpi->td) {
       // Before encoding a frame, copy the thread data from cpi.
       thread_data->td->mb = cpi->td.mb;
-      av1_alloc_src_diff_buf(cm, &thread_data->td->mb);
     }
+    av1_alloc_src_diff_buf(cm, &thread_data->td->mb);
   }
 }
 #endif
diff --git a/av1/encoder/firstpass.c b/av1/encoder/firstpass.c
index 7ddc3e3..50e6ecb 100644
--- a/av1/encoder/firstpass.c
+++ b/av1/encoder/firstpass.c
@@ -1105,6 +1105,7 @@
   const int tile_cols = cm->tiles.cols;
   const int tile_rows = cm->tiles.rows;
 
+  av1_alloc_src_diff_buf(cm, &cpi->td.mb);
   for (int tile_row = 0; tile_row < tile_rows; ++tile_row) {
     for (int tile_col = 0; tile_col < tile_cols; ++tile_col) {
       TileDataEnc *const tile_data =
@@ -1390,7 +1391,6 @@
   av1_init_mode_probs(cm->fc);
   av1_init_mv_probs(cm);
   av1_initialize_rd_consts(cpi);
-  av1_alloc_src_diff_buf(cm, &cpi->td.mb);
 
   enc_row_mt->sync_read_ptr = av1_row_mt_sync_read_dummy;
   enc_row_mt->sync_write_ptr = av1_row_mt_sync_write_dummy;