Revert "Allocate TPL buffers only when required"

This reverts commit 6b150b79125ca411ed2533a735e69f373485da49.

This causes some failures with monochrome test vectors.

Bug: b/228634712
Change-Id: I9681cffd8b04fedcd369c5976427ae8e1b7dccde
diff --git a/aom_scale/generic/yv12config.c b/aom_scale/generic/yv12config.c
index b425781..dedfc02 100644
--- a/aom_scale/generic/yv12config.c
+++ b/aom_scale/generic/yv12config.c
@@ -122,14 +122,11 @@
     ybf->y_height = aligned_height;
     ybf->y_stride = y_stride;
 
-    ybf->monochrome = (uv_width == 0 || uv_height == 0) ? 1 : 0;
-    if (!ybf->monochrome) {
-      ybf->uv_crop_width = (width + ss_x) >> ss_x;
-      ybf->uv_crop_height = (height + ss_y) >> ss_y;
-      ybf->uv_width = uv_width;
-      ybf->uv_height = uv_height;
-      ybf->uv_stride = uv_stride;
-    }
+    ybf->uv_crop_width = (width + ss_x) >> ss_x;
+    ybf->uv_crop_height = (height + ss_y) >> ss_y;
+    ybf->uv_width = uv_width;
+    ybf->uv_height = uv_height;
+    ybf->uv_stride = uv_stride;
 
     ybf->border = border;
     ybf->frame_size = (size_t)frame_size;
@@ -147,16 +144,13 @@
 
     ybf->y_buffer = (uint8_t *)aom_align_addr(
         buf + (border * y_stride) + border, aom_byte_align);
-    ybf->u_buffer = ybf->v_buffer = NULL;
-    if (!ybf->monochrome) {
-      ybf->u_buffer = (uint8_t *)aom_align_addr(
-          buf + yplane_size + (uv_border_h * uv_stride) + uv_border_w,
-          aom_byte_align);
-      ybf->v_buffer =
-          (uint8_t *)aom_align_addr(buf + yplane_size + uvplane_size +
-                                        (uv_border_h * uv_stride) + uv_border_w,
-                                    aom_byte_align);
-    }
+    ybf->u_buffer = (uint8_t *)aom_align_addr(
+        buf + yplane_size + (uv_border_h * uv_stride) + uv_border_w,
+        aom_byte_align);
+    ybf->v_buffer =
+        (uint8_t *)aom_align_addr(buf + yplane_size + uvplane_size +
+                                      (uv_border_h * uv_stride) + uv_border_w,
+                                  aom_byte_align);
 
     ybf->use_external_reference_buffers = 0;
 
@@ -178,11 +172,13 @@
   return AOM_CODEC_MEM_ERROR;
 }
 
-static int calc_stride_and_planesize(
-    const int ss_x, const int ss_y, const int aligned_width,
-    const int aligned_height, const int border, const int byte_alignment,
-    int is_monochrome, int *y_stride, int *uv_stride, uint64_t *yplane_size,
-    uint64_t *uvplane_size, const int uv_height) {
+static int calc_stride_and_planesize(const int ss_x, const int ss_y,
+                                     const int aligned_width,
+                                     const int aligned_height, const int border,
+                                     const int byte_alignment, int *y_stride,
+                                     int *uv_stride, uint64_t *yplane_size,
+                                     uint64_t *uvplane_size,
+                                     const int uv_height) {
   /* Only support allocating buffers that have a border that's a multiple
    * of 32. The border restriction is required to get 16-byte alignment of
    * the start of the chroma rows without introducing an arbitrary gap
@@ -193,15 +189,9 @@
   *yplane_size =
       (aligned_height + 2 * border) * (uint64_t)(*y_stride) + byte_alignment;
 
-  *uv_stride = 0;
-  *uvplane_size = 0;
-  if (is_monochrome == 0) {
-    *uv_stride = *y_stride >> ss_x;
-    *uvplane_size =
-        (uv_height + 2 * (border >> ss_y)) * (uint64_t)(*uv_stride) +
-        byte_alignment;
-  }
-
+  *uv_stride = *y_stride >> ss_x;
+  *uvplane_size = (uv_height + 2 * (border >> ss_y)) * (uint64_t)(*uv_stride) +
+                  byte_alignment;
   return 0;
 }
 
@@ -210,7 +200,7 @@
                              int border, int byte_alignment,
                              aom_codec_frame_buffer_t *fb,
                              aom_get_frame_buffer_cb_fn_t cb, void *cb_priv,
-                             int alloc_y_buffer_8bit, int is_monochrome) {
+                             int alloc_y_buffer_8bit) {
 #if CONFIG_SIZE_LIMIT
   if (width > DECODE_WIDTH_LIMIT || height > DECODE_HEIGHT_LIMIT)
     return AOM_CODEC_MEM_ERROR;
@@ -223,15 +213,14 @@
     uint64_t uvplane_size = 0;
     const int aligned_width = (width + 7) & ~7;
     const int aligned_height = (height + 7) & ~7;
-    const int uv_width = is_monochrome == 0 ? aligned_width >> ss_x : 0;
-    const int uv_height = is_monochrome == 0 ? aligned_height >> ss_y : 0;
-    const int uv_border_w = is_monochrome == 0 ? border >> ss_x : 0;
-    const int uv_border_h = is_monochrome == 0 ? border >> ss_y : 0;
+    const int uv_width = aligned_width >> ss_x;
+    const int uv_height = aligned_height >> ss_y;
+    const int uv_border_w = border >> ss_x;
+    const int uv_border_h = border >> ss_y;
 
     int error = calc_stride_and_planesize(
         ss_x, ss_y, aligned_width, aligned_height, border, byte_alignment,
-        is_monochrome, &y_stride, &uv_stride, &yplane_size, &uvplane_size,
-        uv_height);
+        &y_stride, &uv_stride, &yplane_size, &uvplane_size, uv_height);
     if (error) return error;
     return realloc_frame_buffer_aligned(
         ybf, width, height, ss_x, ss_y, use_highbitdepth, border,
@@ -244,12 +233,12 @@
 
 int aom_alloc_frame_buffer(YV12_BUFFER_CONFIG *ybf, int width, int height,
                            int ss_x, int ss_y, int use_highbitdepth, int border,
-                           int byte_alignment, int is_monochrome) {
+                           int byte_alignment) {
   if (ybf) {
     aom_free_frame_buffer(ybf);
     return aom_realloc_frame_buffer(ybf, width, height, ss_x, ss_y,
                                     use_highbitdepth, border, byte_alignment,
-                                    NULL, NULL, NULL, 0, is_monochrome);
+                                    NULL, NULL, NULL, 0);
   }
   return AOM_CODEC_MEM_ERROR;
 }
diff --git a/aom_scale/generic/yv12extend.c b/aom_scale/generic/yv12extend.c
index 5d797c8..a32aee5 100644
--- a/aom_scale/generic/yv12extend.c
+++ b/aom_scale/generic/yv12extend.c
@@ -460,7 +460,7 @@
     const int error = aom_alloc_frame_buffer(
         &new_buf, ybf->y_crop_width, ybf->y_crop_height, ybf->subsampling_x,
         ybf->subsampling_y, ybf->flags & YV12_FLAG_HIGHBITDEPTH, new_border,
-        byte_alignment, 0);
+        byte_alignment);
     if (error) return error;
     // Copy image buffer
     aom_yv12_copy_frame(ybf, &new_buf, num_planes);
diff --git a/aom_scale/yv12config.h b/aom_scale/yv12config.h
index d8db98a..31af69c 100644
--- a/aom_scale/yv12config.h
+++ b/aom_scale/yv12config.h
@@ -123,7 +123,7 @@
 
 int aom_alloc_frame_buffer(YV12_BUFFER_CONFIG *ybf, int width, int height,
                            int ss_x, int ss_y, int use_highbitdepth, int border,
-                           int byte_alignment, int is_monochrome);
+                           int byte_alignment);
 
 // Updates the yv12 buffer config with the frame buffer. |byte_alignment| must
 // be a power of 2, from 32 to 1024. 0 sets legacy alignment. If cb is not
@@ -137,7 +137,7 @@
                              int border, int byte_alignment,
                              aom_codec_frame_buffer_t *fb,
                              aom_get_frame_buffer_cb_fn_t cb, void *cb_priv,
-                             int alloc_y_buffer_8bit, int is_monochrome);
+                             int alloc_y_buffer_8bit);
 
 int aom_free_frame_buffer(YV12_BUFFER_CONFIG *ybf);
 
diff --git a/av1/common/resize.c b/av1/common/resize.c
index 8591f98..a3c3c0e 100644
--- a/av1/common/resize.c
+++ b/av1/common/resize.c
@@ -1362,7 +1362,7 @@
             scaled, scaled_width, scaled_height, seq_params->subsampling_x,
             seq_params->subsampling_y, seq_params->use_highbitdepth,
             border_in_pixels, cm->features.byte_alignment, NULL, NULL, NULL,
-            alloc_y_buffer_8bit, 0))
+            alloc_y_buffer_8bit))
       aom_internal_error(cm->error, AOM_CODEC_MEM_ERROR,
                          "Failed to allocate scaled buffer");
 
@@ -1457,7 +1457,7 @@
   if (aom_alloc_frame_buffer(
           &copy_buffer, aligned_width, cm->height, seq_params->subsampling_x,
           seq_params->subsampling_y, seq_params->use_highbitdepth,
-          AOM_BORDER_IN_PIXELS, byte_alignment, 0))
+          AOM_BORDER_IN_PIXELS, byte_alignment))
     aom_internal_error(cm->error, AOM_CODEC_MEM_ERROR,
                        "Failed to allocate copy buffer for superres upscaling");
 
@@ -1489,7 +1489,7 @@
             frame_to_show, cm->superres_upscaled_width,
             cm->superres_upscaled_height, seq_params->subsampling_x,
             seq_params->subsampling_y, seq_params->use_highbitdepth,
-            AOM_BORDER_IN_PIXELS, byte_alignment, fb, cb, cb_priv, 0, 0)) {
+            AOM_BORDER_IN_PIXELS, byte_alignment, fb, cb, cb_priv, 0)) {
       unlock_buffer_pool(pool);
       aom_internal_error(
           cm->error, AOM_CODEC_MEM_ERROR,
@@ -1506,7 +1506,7 @@
             frame_to_show, cm->superres_upscaled_width,
             cm->superres_upscaled_height, seq_params->subsampling_x,
             seq_params->subsampling_y, seq_params->use_highbitdepth,
-            AOM_BORDER_IN_PIXELS, byte_alignment, 0))
+            AOM_BORDER_IN_PIXELS, byte_alignment))
       aom_internal_error(
           cm->error, AOM_CODEC_MEM_ERROR,
           "Failed to reallocate current frame buffer for superres upscaling");
diff --git a/av1/common/restoration.c b/av1/common/restoration.c
index dbfd1cc..202953c 100644
--- a/av1/common/restoration.c
+++ b/av1/common/restoration.c
@@ -1117,7 +1117,7 @@
   if (aom_realloc_frame_buffer(
           lr_ctxt->dst, frame_width, frame_height, seq_params->subsampling_x,
           seq_params->subsampling_y, highbd, AOM_RESTORATION_FRAME_BORDER,
-          cm->features.byte_alignment, NULL, NULL, NULL, 0, 0) < 0)
+          cm->features.byte_alignment, NULL, NULL, NULL, 0) < 0)
     aom_internal_error(cm->error, AOM_CODEC_MEM_ERROR,
                        "Failed to allocate restoration dst buffer");
 
diff --git a/av1/decoder/decodeframe.c b/av1/decoder/decodeframe.c
index e0c7b74..df66961 100644
--- a/av1/decoder/decodeframe.c
+++ b/av1/decoder/decodeframe.c
@@ -1951,7 +1951,7 @@
           &cm->cur_frame->buf, cm->width, cm->height, seq_params->subsampling_x,
           seq_params->subsampling_y, seq_params->use_highbitdepth,
           AOM_DEC_BORDER_IN_PIXELS, cm->features.byte_alignment,
-          &cm->cur_frame->raw_frame_buffer, pool->get_fb_cb, pool->cb_priv, 0,
+          &cm->cur_frame->raw_frame_buffer, pool->get_fb_cb, pool->cb_priv,
           0)) {
     unlock_buffer_pool(pool);
     aom_internal_error(cm->error, AOM_CODEC_MEM_ERROR,
@@ -4790,8 +4790,7 @@
                   seq_params->max_frame_height, seq_params->subsampling_x,
                   seq_params->subsampling_y, seq_params->use_highbitdepth,
                   AOM_BORDER_IN_PIXELS, features->byte_alignment,
-                  &buf->raw_frame_buffer, pool->get_fb_cb, pool->cb_priv, 0,
-                  0)) {
+                  &buf->raw_frame_buffer, pool->get_fb_cb, pool->cb_priv, 0)) {
             decrease_ref_count(buf, pool);
             unlock_buffer_pool(pool);
             aom_internal_error(&pbi->error, AOM_CODEC_MEM_ERROR,
diff --git a/av1/decoder/obu.c b/av1/decoder/obu.c
index d589f00..76abf6b 100644
--- a/av1/decoder/obu.c
+++ b/av1/decoder/obu.c
@@ -396,7 +396,7 @@
                              cm->seq_params->subsampling_y,
                              (cm->seq_params->use_highbitdepth &&
                               (cm->seq_params->bit_depth > AOM_BITS_8)),
-                             0, cm->features.byte_alignment, 0))
+                             0, cm->features.byte_alignment))
     aom_internal_error(&pbi->error, AOM_CODEC_MEM_ERROR,
                        "Failed to allocate the tile list output buffer");
 }
diff --git a/av1/encoder/allintra_vis.c b/av1/encoder/allintra_vis.c
index 5634d50..ef6dd70 100644
--- a/av1/encoder/allintra_vis.c
+++ b/av1/encoder/allintra_vis.c
@@ -382,7 +382,7 @@
           &cm->cur_frame->buf, cm->width, cm->height, seq_params->subsampling_x,
           seq_params->subsampling_y, seq_params->use_highbitdepth,
           cpi->oxcf.border_in_pixels, cm->features.byte_alignment, NULL, NULL,
-          NULL, cpi->oxcf.tool_cfg.enable_global_motion, 0))
+          NULL, cpi->oxcf.tool_cfg.enable_global_motion))
     aom_internal_error(cm->error, AOM_CODEC_MEM_ERROR,
                        "Failed to allocate frame buffer");
 
diff --git a/av1/encoder/encode_strategy.c b/av1/encoder/encode_strategy.c
index ef0ee72..d60aa9e 100644
--- a/av1/encoder/encode_strategy.c
+++ b/av1/encoder/encode_strategy.c
@@ -1262,11 +1262,6 @@
   cpi->twopass_frame.this_frame = NULL;
   const int use_one_pass_rt_params = is_one_pass_rt_params(cpi);
   if (!use_one_pass_rt_params && !is_stat_generation_stage(cpi)) {
-    TplParams *const tpl_data = &cpi->ppi->tpl_data;
-    if (tpl_data->tpl_stats_pool[0] == NULL && oxcf->gf_cfg.lag_in_frames > 1 &&
-        oxcf->algo_cfg.enable_tpl_model) {
-      av1_setup_tpl_buffers(cpi->ppi, &cpi->common, oxcf->gf_cfg.lag_in_frames);
-    }
 #if CONFIG_COLLECT_COMPONENT_TIMING
     start_timing(cpi, av1_get_second_pass_params_time);
 #endif
diff --git a/av1/encoder/encodeframe.c b/av1/encoder/encodeframe.c
index 8e792a1..418c907 100644
--- a/av1/encoder/encodeframe.c
+++ b/av1/encoder/encodeframe.c
@@ -590,7 +590,7 @@
       }
 
       // TODO(jingning): revisit this function.
-      if (is_frame_tpl_valid(cpi, cpi->gf_frame_index) && 0) {
+      if (cpi->oxcf.algo_cfg.enable_tpl_model && 0) {
         adjust_rdmult_tpl_model(cpi, x, mi_row, mi_col);
       }
     }
diff --git a/av1/encoder/encodeframe_utils.c b/av1/encoder/encodeframe_utils.c
index 798c573..0128156 100644
--- a/av1/encoder/encodeframe_utils.c
+++ b/av1/encoder/encodeframe_utils.c
@@ -989,10 +989,10 @@
 
   if (tpl_idx >= MAX_TPL_FRAME_IDX) return base_qindex;
 
-  if (!is_frame_tpl_valid(cpi, tpl_idx)) return base_qindex;
   TplDepFrame *tpl_frame = &tpl_data->tpl_frame[tpl_idx];
   TplDepStats *tpl_stats = tpl_frame->tpl_stats_ptr;
   int tpl_stride = tpl_frame->stride;
+  if (!tpl_frame->is_valid) return base_qindex;
 
 #ifndef NDEBUG
   int mi_count = 0;
diff --git a/av1/encoder/encoder.c b/av1/encoder/encoder.c
index 835a341..039dc80 100644
--- a/av1/encoder/encoder.c
+++ b/av1/encoder/encoder.c
@@ -1167,6 +1167,14 @@
         aom_calloc(num_rows * num_cols,
                    sizeof(*ppi->tpl_sb_rdmult_scaling_factors)));
 
+#if !CONFIG_REALTIME_ONLY
+    if (oxcf->pass != AOM_RC_FIRST_PASS) {
+      av1_setup_tpl_buffers(ppi, &mi_params, oxcf->frm_dim_cfg.width,
+                            oxcf->frm_dim_cfg.height, 0,
+                            oxcf->gf_cfg.lag_in_frames);
+    }
+#endif
+
 #if CONFIG_INTERNAL_STATS
     ppi->b_calculate_blockiness = 1;
     ppi->b_calculate_consistency = 1;
@@ -1516,7 +1524,6 @@
   for (int frame = 0; frame < MAX_LAG_BUFFERS; ++frame) {
     aom_free(tpl_data->tpl_stats_pool[frame]);
     aom_free_frame_buffer(&tpl_data->tpl_rec_pool[frame]);
-    tpl_data->tpl_stats_pool[frame] = NULL;
   }
 
 #if !CONFIG_REALTIME_ONLY
@@ -2091,7 +2098,7 @@
           &cm->cur_frame->buf, cm->width, cm->height, seq_params->subsampling_x,
           seq_params->subsampling_y, seq_params->use_highbitdepth,
           cpi->oxcf.border_in_pixels, cm->features.byte_alignment, NULL, NULL,
-          NULL, cpi->oxcf.tool_cfg.enable_global_motion, 0))
+          NULL, cpi->oxcf.tool_cfg.enable_global_motion))
     aom_internal_error(cm->error, AOM_CODEC_MEM_ERROR,
                        "Failed to allocate frame buffer");
 
@@ -2456,7 +2463,7 @@
               &cpi->orig_source, cpi->oxcf.frm_dim_cfg.width,
               cpi->oxcf.frm_dim_cfg.height, seq_params->subsampling_x,
               seq_params->subsampling_y, seq_params->use_highbitdepth,
-              cpi->oxcf.border_in_pixels, cm->features.byte_alignment, 0))
+              cpi->oxcf.border_in_pixels, cm->features.byte_alignment))
         aom_internal_error(cm->error, AOM_CODEC_MEM_ERROR,
                            "Failed to allocate scaled buffer");
     }
diff --git a/av1/encoder/encoder.h b/av1/encoder/encoder.h
index 3796a39..3d9d479 100644
--- a/av1/encoder/encoder.h
+++ b/av1/encoder/encoder.h
@@ -3941,12 +3941,6 @@
 #define MAX_GFUBOOST_FACTOR 10.0
 #define MIN_GFUBOOST_FACTOR 4.0
 
-static INLINE int is_frame_tpl_valid(const AV1_COMP *cpi, uint8_t gf_index) {
-  return cpi->oxcf.gf_cfg.lag_in_frames > 1 &&
-         cpi->oxcf.algo_cfg.enable_tpl_model &&
-         cpi->ppi->tpl_data.tpl_frame[gf_index].is_valid;
-}
-
 static INLINE int is_frame_tpl_eligible(const GF_GROUP *const gf_group,
                                         uint8_t index) {
   const FRAME_UPDATE_TYPE update_type = gf_group->update_type[index];
diff --git a/av1/encoder/encoder_alloc.h b/av1/encoder/encoder_alloc.h
index a94a572..ae99aee 100644
--- a/av1/encoder/encoder_alloc.h
+++ b/av1/encoder/encoder_alloc.h
@@ -378,7 +378,7 @@
           cm->seq_params->subsampling_x, cm->seq_params->subsampling_y,
           cm->seq_params->use_highbitdepth, AOM_BORDER_IN_PIXELS,
           cm->features.byte_alignment, NULL, NULL, NULL,
-          cpi->oxcf.tool_cfg.enable_global_motion, 0))
+          cpi->oxcf.tool_cfg.enable_global_motion))
     aom_internal_error(cm->error, AOM_CODEC_MEM_ERROR,
                        "Failed to reallocate scaled source buffer");
   assert(cpi->scaled_source.y_crop_width == scaled_width);
diff --git a/av1/encoder/encoder_utils.c b/av1/encoder/encoder_utils.c
index e6cf957..fd8be7b 100644
--- a/av1/encoder/encoder_utils.c
+++ b/av1/encoder/encoder_utils.c
@@ -541,7 +541,7 @@
 
 #if !CONFIG_REALTIME_ONLY
   GF_GROUP *gf_group = &cpi->ppi->gf_group;
-  if (is_frame_tpl_valid(cpi, cpi->gf_frame_index) &&
+  if (cpi->oxcf.algo_cfg.enable_tpl_model &&
       av1_tpl_stats_ready(&cpi->ppi->tpl_data, cpi->gf_frame_index)) {
     process_tpl_stats_frame(cpi);
     av1_tpl_rdmult_setup(cpi);
@@ -554,7 +554,7 @@
 
 #if !CONFIG_REALTIME_ONLY
   if (cpi->oxcf.rc_cfg.mode == AOM_Q &&
-      is_frame_tpl_valid(cpi, cpi->gf_frame_index) &&
+      cpi->ppi->tpl_data.tpl_frame[cpi->gf_frame_index].is_valid &&
       !is_lossless_requested(&cpi->oxcf.rc_cfg)) {
     const RateControlCfg *const rc_cfg = &cpi->oxcf.rc_cfg;
     const int tpl_q = av1_tpl_get_q_index(
@@ -725,7 +725,7 @@
                   &new_fb->buf, cm->width, cm->height,
                   cm->seq_params->subsampling_x, cm->seq_params->subsampling_y,
                   cm->seq_params->use_highbitdepth, AOM_BORDER_IN_PIXELS,
-                  cm->features.byte_alignment, NULL, NULL, NULL, 0, 0)) {
+                  cm->features.byte_alignment, NULL, NULL, NULL, 0)) {
             if (force_scaling) {
               // Release the reference acquired in the get_free_fb() call above.
               --new_fb->ref_count;
diff --git a/av1/encoder/lookahead.c b/av1/encoder/lookahead.c
index fe513ce..e6d7213 100644
--- a/av1/encoder/lookahead.c
+++ b/av1/encoder/lookahead.c
@@ -82,7 +82,7 @@
       if (aom_realloc_frame_buffer(
               &ctx->buf[i].img, width, height, subsampling_x, subsampling_y,
               use_highbitdepth, border_in_pixels, byte_alignment, NULL, NULL,
-              NULL, enable_global_motion, 0))
+              NULL, enable_global_motion))
         goto fail;
     }
   }
@@ -126,7 +126,7 @@
     memset(&new_img, 0, sizeof(new_img));
     if (aom_alloc_frame_buffer(&new_img, width, height, subsampling_x,
                                subsampling_y, use_highbitdepth,
-                               AOM_BORDER_IN_PIXELS, 0, 0))
+                               AOM_BORDER_IN_PIXELS, 0))
       return 1;
     aom_free_frame_buffer(&buf->img);
     buf->img = new_img;
diff --git a/av1/encoder/picklpf.c b/av1/encoder/picklpf.c
index 377f069..925c269 100644
--- a/av1/encoder/picklpf.c
+++ b/av1/encoder/picklpf.c
@@ -298,7 +298,7 @@
             &cpi->last_frame_uf, cm->width, cm->height,
             seq_params->subsampling_x, seq_params->subsampling_y,
             seq_params->use_highbitdepth, cpi->oxcf.border_in_pixels,
-            cm->features.byte_alignment, NULL, NULL, NULL, 0, 0))
+            cm->features.byte_alignment, NULL, NULL, NULL, 0))
       aom_internal_error(cm->error, AOM_CODEC_MEM_ERROR,
                          "Failed to allocate last frame buffer");
 
diff --git a/av1/encoder/pickrst.c b/av1/encoder/pickrst.c
index 246b0aa..467fb1e 100644
--- a/av1/encoder/pickrst.c
+++ b/av1/encoder/pickrst.c
@@ -1768,7 +1768,7 @@
           cm->superres_upscaled_height, seq_params->subsampling_x,
           seq_params->subsampling_y, seq_params->use_highbitdepth,
           AOM_RESTORATION_FRAME_BORDER, cm->features.byte_alignment, NULL, NULL,
-          NULL, 0, 0))
+          NULL, 0))
     aom_internal_error(cm->error, AOM_CODEC_MEM_ERROR,
                        "Failed to allocate trial restored frame buffer");
 
diff --git a/av1/encoder/rdopt.c b/av1/encoder/rdopt.c
index a8e24ad..b947043 100644
--- a/av1/encoder/rdopt.c
+++ b/av1/encoder/rdopt.c
@@ -2220,9 +2220,7 @@
                  cpi->gf_frame_index < cpi->ppi->gf_group.size));
   const int tpl_idx = cpi->gf_frame_index;
   TplParams *const tpl_data = &cpi->ppi->tpl_data;
-  if (!is_frame_tpl_valid(cpi, tpl_idx) ||
-      !av1_tpl_stats_ready(tpl_data, tpl_idx))
-    return;
+  if (!av1_tpl_stats_ready(tpl_data, tpl_idx)) return;
   const TplDepFrame *tpl_frame = &tpl_data->tpl_frame[tpl_idx];
   const TplDepStats *tpl_stats = tpl_frame->tpl_stats_ptr;
   const int mi_wide = mi_size_wide[bsize];
@@ -5829,7 +5827,7 @@
   const int do_pruning =
       (AOMMIN(cm->width, cm->height) > 480 && cpi->speed <= 1) ? 0 : 1;
   if (do_pruning && sf->intra_sf.skip_intra_in_interframe &&
-      cpi->oxcf.gf_cfg.lag_in_frames > 1 && cpi->oxcf.algo_cfg.enable_tpl_model)
+      cpi->oxcf.algo_cfg.enable_tpl_model)
     calculate_cost_from_tpl_data(cpi, x, bsize, mi_row, mi_col, &inter_cost,
                                  &intra_cost);
 #endif  // !CONFIG_REALTIME_ONLY
diff --git a/av1/encoder/temporal_filter.c b/av1/encoder/temporal_filter.c
index dbdd66d..c876679 100644
--- a/av1/encoder/temporal_filter.c
+++ b/av1/encoder/temporal_filter.c
@@ -1278,7 +1278,7 @@
         seq_params->subsampling_x, seq_params->subsampling_y,
         seq_params->use_highbitdepth, cpi->oxcf.border_in_pixels,
         cm->features.byte_alignment, NULL, NULL, NULL,
-        cpi->oxcf.tool_cfg.enable_global_motion, 0);
+        cpi->oxcf.tool_cfg.enable_global_motion);
     if (ret) {
       aom_internal_error(cm->error, AOM_CODEC_MEM_ERROR,
                          "Failed to allocate tf_info");
@@ -1290,7 +1290,7 @@
       oxcf->frm_dim_cfg.height, seq_params->subsampling_x,
       seq_params->subsampling_y, seq_params->use_highbitdepth,
       cpi->oxcf.border_in_pixels, cm->features.byte_alignment, NULL, NULL, NULL,
-      cpi->oxcf.tool_cfg.enable_global_motion, 0);
+      cpi->oxcf.tool_cfg.enable_global_motion);
   if (ret) {
     aom_internal_error(cm->error, AOM_CODEC_MEM_ERROR,
                        "Failed to allocate tf_info");
diff --git a/av1/encoder/tpl_model.c b/av1/encoder/tpl_model.c
index 9d794fa..764de09 100644
--- a/av1/encoder/tpl_model.c
+++ b/av1/encoder/tpl_model.c
@@ -143,9 +143,9 @@
   assert(*tpl_bsize_1d >= 16);
 }
 
-void av1_setup_tpl_buffers(AV1_PRIMARY *const ppi, AV1_COMMON *const cm,
-                           int lag_in_frames) {
-  CommonModeInfoParams *const mi_params = &cm->mi_params;
+void av1_setup_tpl_buffers(AV1_PRIMARY *const ppi,
+                           CommonModeInfoParams *const mi_params, int width,
+                           int height, int byte_alignment, int lag_in_frames) {
   SequenceHeader *const seq_params = &ppi->seq_params;
   TplParams *const tpl_data = &ppi->tpl_data;
   set_tpl_stats_block_size(&tpl_data->tpl_stats_block_mis_log2,
@@ -184,12 +184,11 @@
                        tpl_data->tpl_stats_buffer[frame].height,
                    sizeof(*tpl_data->tpl_stats_buffer[frame].tpl_stats_ptr)));
 
-    if (aom_alloc_frame_buffer(
-            &tpl_data->tpl_rec_pool[frame], cm->width, cm->height,
-            seq_params->subsampling_x, seq_params->subsampling_y,
-            seq_params->use_highbitdepth, tpl_data->border_in_pixels,
-            cm->features.byte_alignment,
-            ppi->cpi->sf.tpl_sf.use_y_only_rate_distortion))
+    if (aom_alloc_frame_buffer(&tpl_data->tpl_rec_pool[frame], width, height,
+                               seq_params->subsampling_x,
+                               seq_params->subsampling_y,
+                               seq_params->use_highbitdepth,
+                               tpl_data->border_in_pixels, byte_alignment))
       aom_internal_error(&ppi->error, AOM_CODEC_MEM_ERROR,
                          "Failed to allocate frame buffer");
   }
@@ -1676,8 +1675,6 @@
                     cm->features.allow_high_precision_mv, cpi->td.mb.mv_costs);
 
   const int gop_length = get_gop_length(gf_group);
-  const int num_planes =
-      cpi->sf.tpl_sf.use_y_only_rate_distortion ? 1 : av1_num_planes(cm);
   // Backward propagation from tpl_group_frames to 1.
   for (int frame_idx = cpi->gf_frame_index; frame_idx < tpl_gf_group_frames;
        ++frame_idx) {
@@ -1703,7 +1700,7 @@
     av1_tpl_store_txfm_stats(tpl_data, &cpi->td.tpl_txfm_stats, frame_idx);
 
     aom_extend_frame_borders(tpl_data->tpl_frame[frame_idx].rec_picture,
-                             num_planes);
+                             av1_num_planes(cm));
   }
 
   for (int frame_idx = tpl_gf_group_frames - 1;
@@ -1813,7 +1810,8 @@
   const int tpl_idx = cpi->gf_frame_index;
 
   if (tpl_idx >= MAX_TPL_FRAME_IDX) return;
-  if (!is_frame_tpl_valid(cpi, tpl_idx)) return;
+  TplDepFrame *tpl_frame = &cpi->ppi->tpl_data.tpl_frame[tpl_idx];
+  if (!tpl_frame->is_valid) return;
   if (!is_frame_tpl_eligible(gf_group, cpi->gf_frame_index)) return;
   if (cpi->oxcf.q_cfg.aq_mode != NO_AQ) return;
 
diff --git a/av1/encoder/tpl_model.h b/av1/encoder/tpl_model.h
index 7ed1dac..a974f98 100644
--- a/av1/encoder/tpl_model.h
+++ b/av1/encoder/tpl_model.h
@@ -420,8 +420,9 @@
  * \param[out]   tpl_data  tpl data structure
  */
 
-void av1_setup_tpl_buffers(struct AV1_PRIMARY *const ppi, AV1_COMMON *const cm,
-                           int lag_in_frames);
+void av1_setup_tpl_buffers(struct AV1_PRIMARY *const ppi,
+                           CommonModeInfoParams *const mi_params, int width,
+                           int height, int byte_alignment, int lag_in_frames);
 
 /*!\brief Implements temporal dependency modelling for a GOP (GF/ARF
  * group) and selects between 16 and 32 frame GOP structure.
diff --git a/test/hbd_metrics_test.cc b/test/hbd_metrics_test.cc
index 6c9fe55..39c2b4c 100644
--- a/test/hbd_metrics_test.cc
+++ b/test/hbd_metrics_test.cc
@@ -112,10 +112,10 @@
     memset(&hbd_src, 0, sizeof(hbd_src));
     memset(&hbd_dst, 0, sizeof(hbd_dst));
 
-    aom_alloc_frame_buffer(&lbd_src, width, height, 1, 1, 0, 32, 16, 0);
-    aom_alloc_frame_buffer(&lbd_dst, width, height, 1, 1, 0, 32, 16, 0);
-    aom_alloc_frame_buffer(&hbd_src, width, height, 1, 1, 1, 32, 16, 0);
-    aom_alloc_frame_buffer(&hbd_dst, width, height, 1, 1, 1, 32, 16, 0);
+    aom_alloc_frame_buffer(&lbd_src, width, height, 1, 1, 0, 32, 16);
+    aom_alloc_frame_buffer(&lbd_dst, width, height, 1, 1, 0, 32, 16);
+    aom_alloc_frame_buffer(&hbd_src, width, height, 1, 1, 1, 32, 16);
+    aom_alloc_frame_buffer(&hbd_dst, width, height, 1, 1, 1, 32, 16);
 
     memset(lbd_src.buffer_alloc, kPixFiller, lbd_src.buffer_alloc_sz);
     while (i < lbd_src.buffer_alloc_sz) {