Allocate tpl buffers conditionally

When lag_in_frames <= 1, tpl analysis is not performed.
Hence relevant tpl buffer allocations are avoided.

BUG=aomedia:2872

Change-Id: I1f4f291413e0896417113413f7e5457295a0d89f
diff --git a/av1/encoder/encoder.c b/av1/encoder/encoder.c
index 672c3bb..07f3072 100644
--- a/av1/encoder/encoder.c
+++ b/av1/encoder/encoder.c
@@ -1051,7 +1051,7 @@
 #endif
 
   if (!is_stat_generation_stage(cpi)) {
-    setup_tpl_buffers(cm, &cpi->tpl_data);
+    setup_tpl_buffers(cm, &cpi->tpl_data, cpi->oxcf.gf_cfg.lag_in_frames);
   }
 
 #if CONFIG_COLLECT_PARTITION_STATS == 2
diff --git a/av1/encoder/encoder_alloc.h b/av1/encoder/encoder_alloc.h
index 0dd2b33..32eeb6b 100644
--- a/av1/encoder/encoder_alloc.h
+++ b/av1/encoder/encoder_alloc.h
@@ -110,7 +110,8 @@
 }
 
 static AOM_INLINE void setup_tpl_buffers(AV1_COMMON *const cm,
-                                         TplParams *const tpl_data) {
+                                         TplParams *const tpl_data,
+                                         int lag_in_frames) {
   CommonModeInfoParams *const mi_params = &cm->mi_params;
   set_tpl_stats_block_size(&tpl_data->tpl_stats_block_mis_log2,
                            &tpl_data->tpl_bsize_1d);
@@ -132,7 +133,14 @@
     tpl_data->tpl_stats_buffer[frame].mi_rows = mi_params->mi_rows;
     tpl_data->tpl_stats_buffer[frame].mi_cols = mi_params->mi_cols;
   }
+  tpl_data->tpl_frame = &tpl_data->tpl_stats_buffer[REF_FRAMES + 1];
 
+  // If lag_in_frames <= 1, TPL module is not invoked. Hence tpl recon and
+  // stats buffers are not allocated.
+  if (lag_in_frames <= 1) return;
+
+  // TODO(aomedia:2873): Explore the allocation of tpl buffers based on
+  // lag_in_frames.
   for (int frame = 0; frame < MAX_LAG_BUFFERS; ++frame) {
     CHECK_MEM_ERROR(
         cm, tpl_data->tpl_stats_pool[frame],
@@ -147,8 +155,6 @@
       aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
                          "Failed to allocate frame buffer");
   }
-
-  tpl_data->tpl_frame = &tpl_data->tpl_stats_buffer[REF_FRAMES + 1];
 }
 
 static AOM_INLINE void alloc_obmc_buffers(OBMCBuffer *obmc_buffer,