Commit 69982f8660 for aom

commit 69982f866095a24cfb5c47c43a3dde205c89bf9f
Author: Alejandro Pacheco <alepacheco99@gmail.com>
Date:   Wed Oct 14 10:08:51 2026 +0200

    temporal_filter: keep motion search within the frame's border

    In realtime mode the lookahead frames get a small border
    (get_src_border_in_pixels()), sized only for extending the frame to
    a superblock multiple. With lag_in_frames > 0 the temporal filter
    runs motion search on the lookahead frames, with mv limits based on
    the encoder border, so the search read past the end of the frame
    buffer.

    Limit the search range to the border of the frame searched. Nothing
    changes when the lookahead frames have the encoder border. Add a
    test.

    Bug: 573427857
    Change-Id: Ia656eeb125fced8bd900a9c6a21539d9633f55f4

diff --git a/av1/encoder/temporal_filter.c b/av1/encoder/temporal_filter.c
index 6ec8c04a0c..d775b3f1b5 100644
--- a/av1/encoder/temporal_filter.c
+++ b/av1/encoder/temporal_filter.c
@@ -83,14 +83,15 @@ static void subblock_motion_search(
   int distortion;
   int cost_list[5];

+  // Keep the search within the border of the frame searched: a realtime
+  // lookahead frame may have a smaller border than the encoder's.
+  const int border = AOMMIN(cpi->oxcf.border_in_pixels, ref_frame->border);
   av1_set_mv_row_limits(&cpi->common.mi_params, &mb->mv_limits,
                         (mb_row << mi_h) + (subblock_ofst_i >> MI_SIZE_LOG2),
-                        (subblock_height >> MI_SIZE_LOG2),
-                        cpi->oxcf.border_in_pixels);
+                        (subblock_height >> MI_SIZE_LOG2), border);
   av1_set_mv_col_limits(&cpi->common.mi_params, &mb->mv_limits,
                         (mb_col << mi_w) + (subblock_ofst_j >> MI_SIZE_LOG2),
-                        (subblock_width >> MI_SIZE_LOG2),
-                        cpi->oxcf.border_in_pixels);
+                        (subblock_width >> MI_SIZE_LOG2), border);
   const int boffset = subblock_ofst_i * y_stride + subblock_ofst_j;
   mb->plane[0].src.buf = frame_to_filter->y_buffer + y_offset + boffset;
   mbd->plane[0].pre[0].buf = ref_frame->y_buffer + y_offset + boffset;
@@ -321,13 +322,15 @@ static void tf_motion_search(AV1_COMP *cpi, MACROBLOCK *mb,
   // 32x32 block motion search results.
   int midblock_mses[4] = { INT_MAX, INT_MAX, INT_MAX, INT_MAX };
   MV midblock_mvs[4] = { kZeroMv, kZeroMv, kZeroMv, kZeroMv };
+  // Keep the search within the border of the frame searched: a realtime
+  // lookahead frame may have a smaller border than the encoder's.
+  const int border = AOMMIN(cpi->oxcf.border_in_pixels, ref_frame->border);
   av1_set_mv_row_limits(&cpi->common.mi_params, &mb->mv_limits,
                         (mb_row << mi_size_high_log2[block_size]),
-                        (mb_height >> MI_SIZE_LOG2),
-                        cpi->oxcf.border_in_pixels);
+                        (mb_height >> MI_SIZE_LOG2), border);
   av1_set_mv_col_limits(&cpi->common.mi_params, &mb->mv_limits,
                         (mb_col << mi_size_wide_log2[block_size]),
-                        (mb_width >> MI_SIZE_LOG2), cpi->oxcf.border_in_pixels);
+                        (mb_width >> MI_SIZE_LOG2), border);

   const int q = get_q(cpi);

diff --git a/test/encode_api_test.cc b/test/encode_api_test.cc
index cb28bde03a..20d928a3a7 100644
--- a/test/encode_api_test.cc
+++ b/test/encode_api_test.cc
@@ -4200,6 +4200,60 @@ TEST(EncodeAPI, NonrdBlockYrdRateOverflow) {
   ASSERT_EQ(aom_codec_destroy(&enc), AOM_CODEC_OK);
 }

+// Realtime with lag_in_frames > 0 runs the temporal filter on the lookahead
+// frames, whose border is smaller than the encoder's: its motion search must
+// stay within it.
+TEST(EncodeAPI, RealtimeLagTemporalFilterBorder) {
+  constexpr int kWidth = 320;
+  constexpr int kHeight = 240;
+  aom_codec_iface_t *const iface = aom_codec_av1_cx();
+  aom_codec_enc_cfg_t cfg;
+  ASSERT_EQ(aom_codec_enc_config_default(iface, &cfg, AOM_USAGE_REALTIME),
+            AOM_CODEC_OK);
+  cfg.g_w = kWidth;
+  cfg.g_h = kHeight;
+  cfg.g_lag_in_frames = 48;
+  cfg.rc_end_usage = AOM_VBR;
+
+  aom_codec_ctx_t enc;
+  ASSERT_EQ(aom_codec_enc_init(&enc, iface, &cfg, 0), AOM_CODEC_OK);
+  ASSERT_EQ(aom_codec_control(&enc, AOME_SET_CPUUSED, 7), AOM_CODEC_OK);
+  // Use internal ARFs, which are temporally filtered.
+  ASSERT_EQ(aom_codec_control(&enc, AV1E_SET_GF_MIN_PYRAMID_HEIGHT, 3),
+            AOM_CODEC_OK);
+
+  aom_image_t *img =
+      aom_img_alloc(nullptr, AOM_IMG_FMT_I420, kWidth, kHeight, 32);
+  ASSERT_NE(img, nullptr);
+  // A static textured picture, so that the ARFs are kept.
+  for (int plane = 0; plane < 3; ++plane) {
+    const int w = plane ? (kWidth + 1) >> 1 : kWidth;
+    const int h = plane ? (kHeight + 1) >> 1 : kHeight;
+    for (int r = 0; r < h; ++r) {
+      for (int c = 0; c < w; ++c) {
+        const uint64_t v =
+            static_cast<uint64_t>((c * 7) ^ (r * 13)) * 2654435761u;
+        img->planes[plane][r * img->stride[plane] + c] =
+            static_cast<unsigned char>(v >> 24);
+      }
+    }
+  }
+  for (int i = 0; i < 70; ++i) {
+    ASSERT_EQ(aom_codec_encode(&enc, img, i, 1, 0), AOM_CODEC_OK);
+  }
+  // Flush.
+  bool got_data;
+  do {
+    ASSERT_EQ(aom_codec_encode(&enc, nullptr, 0, 0, 0), AOM_CODEC_OK);
+    got_data = false;
+    aom_codec_iter_t iter = nullptr;
+    while (aom_codec_get_cx_data(&enc, &iter) != nullptr) got_data = true;
+  } while (got_data);
+
+  aom_img_free(img);
+  ASSERT_EQ(aom_codec_destroy(&enc), AOM_CODEC_OK);
+}
+
 // Regression test for webrtc:496266459: Enabling compound prediction via
 // AV1E_SET_SVC_REF_FRAME_COMP_PRED must not affect encoding of frames that do
 // not have any active compound reference pair (e.g., single-reference delta