Allocate rst_tmpbuf conditionally

The buffer rst_tmpbuf is used in loop restoration search
and filtering. Hence, the memory for the same is allocated
based on loop restoration sf in multi-thread encoding mode.

Change-Id: Ie74fbc3f4d6aab7fa3b181c5d7697d9b4728f186
diff --git a/av1/encoder/encoder.c b/av1/encoder/encoder.c
index 750e97c..4b657c8 100644
--- a/av1/encoder/encoder.c
+++ b/av1/encoder/encoder.c
@@ -2079,10 +2079,7 @@
   if (!is_stat_generation_stage(cpi)) av1_init_cdef_worker(cpi);
 
 #if !CONFIG_REALTIME_ONLY
-  const int use_restoration = cm->seq_params->enable_restoration &&
-                              !cm->features.all_lossless &&
-                              !cm->tiles.large_scale;
-  if (use_restoration) {
+  if (is_restoration_used(cm)) {
     const int frame_width = cm->superres_upscaled_width;
     const int frame_height = cm->superres_upscaled_height;
     set_restoration_unit_size(frame_width, frame_height,
@@ -2214,9 +2211,7 @@
       !cm->features.coded_lossless && !cm->tiles.large_scale;
   const int use_cdef = cm->seq_params->enable_cdef &&
                        !cm->features.coded_lossless && !cm->tiles.large_scale;
-  const int use_restoration = cm->seq_params->enable_restoration &&
-                              !cm->features.all_lossless &&
-                              !cm->tiles.large_scale;
+  const int use_restoration = is_restoration_used(cm);
   const int cur_width = cm->cur_frame->width;
   const int cur_height = cm->cur_frame->height;
   const int cur_width_mib = cm->mi_params.mi_cols * MI_SIZE;
diff --git a/av1/encoder/encoder.h b/av1/encoder/encoder.h
index 83c8bd1..26a66db 100644
--- a/av1/encoder/encoder.h
+++ b/av1/encoder/encoder.h
@@ -3843,6 +3843,12 @@
            cpi->common.height != resize_pending_params->height));
 }
 
+// Check if loop restoration filter is used.
+static INLINE int is_restoration_used(const AV1_COMMON *const cm) {
+  return cm->seq_params->enable_restoration && !cm->features.all_lossless &&
+         !cm->tiles.large_scale;
+}
+
 #if CONFIG_AV1_TEMPORAL_DENOISING
 static INLINE int denoise_svc(const struct AV1_COMP *const cpi) {
   return (!cpi->ppi->use_svc ||
diff --git a/av1/encoder/encoder_alloc.h b/av1/encoder/encoder_alloc.h
index 2ee89b4..22406d9 100644
--- a/av1/encoder/encoder_alloc.h
+++ b/av1/encoder/encoder_alloc.h
@@ -360,10 +360,7 @@
 
   // The frame buffer trial_frame_rst is used during loop restoration filter
   // search. Hence it is allocated only when loop restoration is used.
-  const int use_restoration = seq_params->enable_restoration &&
-                              !cm->features.all_lossless &&
-                              !cm->tiles.large_scale;
-  if (use_restoration) {
+  if (is_restoration_used(cm)) {
     if (aom_realloc_frame_buffer(
             &cpi->trial_frame_rst, cm->superres_upscaled_width,
             cm->superres_upscaled_height, seq_params->subsampling_x,
diff --git a/av1/encoder/ethread.c b/av1/encoder/ethread.c
index b6e2c2b..98638bc 100644
--- a/av1/encoder/ethread.c
+++ b/av1/encoder/ethread.c
@@ -638,21 +638,23 @@
     }
 
 #if !CONFIG_REALTIME_ONLY
-    // Initialize loop restoration MT object.
-    AV1LrSync *lr_sync = &mt_info->lr_row_sync;
-    int rst_unit_size;
-    if (cm->width * cm->height > 352 * 288)
-      rst_unit_size = RESTORATION_UNITSIZE_MAX;
-    else
-      rst_unit_size = (RESTORATION_UNITSIZE_MAX >> 1);
-    int num_rows_lr = av1_lr_count_units_in_tile(rst_unit_size, cm->height);
-    int num_lr_workers = av1_get_num_mod_workers_for_alloc(p_mt_info, MOD_LR);
-    if (!lr_sync->sync_range || num_rows_lr > lr_sync->rows ||
-        num_lr_workers > lr_sync->num_workers ||
-        MAX_MB_PLANE > lr_sync->num_planes) {
-      av1_loop_restoration_dealloc(lr_sync, num_lr_workers);
-      av1_loop_restoration_alloc(lr_sync, cm, num_lr_workers, num_rows_lr,
-                                 MAX_MB_PLANE, cm->width);
+    if (is_restoration_used(cm)) {
+      // Initialize loop restoration MT object.
+      AV1LrSync *lr_sync = &mt_info->lr_row_sync;
+      int rst_unit_size;
+      if (cm->width * cm->height > 352 * 288)
+        rst_unit_size = RESTORATION_UNITSIZE_MAX;
+      else
+        rst_unit_size = (RESTORATION_UNITSIZE_MAX >> 1);
+      int num_rows_lr = av1_lr_count_units_in_tile(rst_unit_size, cm->height);
+      int num_lr_workers = av1_get_num_mod_workers_for_alloc(p_mt_info, MOD_LR);
+      if (!lr_sync->sync_range || num_rows_lr > lr_sync->rows ||
+          num_lr_workers > lr_sync->num_workers ||
+          MAX_MB_PLANE > lr_sync->num_planes) {
+        av1_loop_restoration_dealloc(lr_sync, num_lr_workers);
+        av1_loop_restoration_alloc(lr_sync, cm, num_lr_workers, num_rows_lr,
+                                   MAX_MB_PLANE, cm->width);
+      }
     }
 #endif
 
@@ -925,7 +927,8 @@
         &p_mt_info->workers[i];
     AV1_COMP *cur_cpi = ppi->parallel_cpi[frame_idx];
     MultiThreadInfo *mt_info = &cur_cpi->mt_info;
-    const int num_planes = av1_num_planes(&cur_cpi->common);
+    AV1_COMMON *const cm = &cur_cpi->common;
+    const int num_planes = av1_num_planes(cm);
 
     // Assign start of level 2 worker pool
     mt_info->workers = &p_mt_info->workers[i];
@@ -947,24 +950,25 @@
             mt_info->cdef_worker->colbuf[plane];
     }
 #if !CONFIG_REALTIME_ONLY
-    // Back up the original LR buffers before update.
-    int idx = i + mt_info->num_workers - 1;
-    mt_info->restore_state_buf.rst_tmpbuf =
-        mt_info->lr_row_sync.lrworkerdata[idx].rst_tmpbuf;
-    mt_info->restore_state_buf.rlbs =
-        mt_info->lr_row_sync.lrworkerdata[idx].rlbs;
+    if (is_restoration_used(cm)) {
+      // Back up the original LR buffers before update.
+      int idx = i + mt_info->num_workers - 1;
+      mt_info->restore_state_buf.rst_tmpbuf =
+          mt_info->lr_row_sync.lrworkerdata[idx].rst_tmpbuf;
+      mt_info->restore_state_buf.rlbs =
+          mt_info->lr_row_sync.lrworkerdata[idx].rlbs;
 
-    // Update LR buffers.
-    mt_info->lr_row_sync.lrworkerdata[idx].rst_tmpbuf =
-        cur_cpi->common.rst_tmpbuf;
-    mt_info->lr_row_sync.lrworkerdata[idx].rlbs = cur_cpi->common.rlbs;
+      // Update LR buffers.
+      mt_info->lr_row_sync.lrworkerdata[idx].rst_tmpbuf = cm->rst_tmpbuf;
+      mt_info->lr_row_sync.lrworkerdata[idx].rlbs = cm->rlbs;
+    }
 #endif
 
     // At this stage, the thread specific CDEF buffers for the current frame's
     // 'common' and 'cdef_sync' only need to be allocated. 'cdef_worker' has
     // already been allocated across parallel frames.
-    av1_alloc_cdef_buffers(&cur_cpi->common, &p_mt_info->cdef_worker,
-                           &mt_info->cdef_sync, p_mt_info->num_workers, 0);
+    av1_alloc_cdef_buffers(cm, &p_mt_info->cdef_worker, &mt_info->cdef_sync,
+                           p_mt_info->num_workers, 0);
 
     frame_worker->hook = hook;
     frame_worker->data1 = cur_cpi;
@@ -1027,7 +1031,8 @@
   while (i < num_workers) {
     AV1_COMP *cur_cpi = ppi->parallel_cpi[frame_idx];
     MultiThreadInfo *mt_info = &cur_cpi->mt_info;
-    const int num_planes = av1_num_planes(&cur_cpi->common);
+    const AV1_COMMON *const cm = &cur_cpi->common;
+    const int num_planes = av1_num_planes(cm);
 
     // Restore the original cdef_worker pointers.
     if (ppi->p_mt_info.cdef_worker != NULL) {
@@ -1037,12 +1042,14 @@
             mt_info->restore_state_buf.cdef_colbuf[plane];
     }
 #if !CONFIG_REALTIME_ONLY
-    // Restore the original LR buffers.
-    int idx = i + mt_info->num_workers - 1;
-    mt_info->lr_row_sync.lrworkerdata[idx].rst_tmpbuf =
-        mt_info->restore_state_buf.rst_tmpbuf;
-    mt_info->lr_row_sync.lrworkerdata[idx].rlbs =
-        mt_info->restore_state_buf.rlbs;
+    if (is_restoration_used(cm)) {
+      // Restore the original LR buffers.
+      int idx = i + mt_info->num_workers - 1;
+      mt_info->lr_row_sync.lrworkerdata[idx].rst_tmpbuf =
+          mt_info->restore_state_buf.rst_tmpbuf;
+      mt_info->lr_row_sync.lrworkerdata[idx].rlbs =
+          mt_info->restore_state_buf.rlbs;
+    }
 #endif
 
     frame_idx++;