Commit 7e7258ffc3 for aom

commit 7e7258ffc3319a6c3d95e6e4a8d7903c3782c105
Author: Alejandro Pacheco <alepacheco99@gmail.com>
Date:   Wed Oct 14 16:04:11 2026 +0200

    av1_cx_iface: reject raising kf_max_dist from 0

    With kf_max_dist 0 every frame is a key frame, and the encoder does not
    allocate what only inter frames use:
    - on the main thread, the motion vector costs (alloc_compressor_data()),
      the simple motion search tree (av1_setup_sms_tree()) and the OBMC
      buffer (av1_create_compressor());
    - on each worker thread (av1_init_tile_thread_data()), the simple motion
      search tree and the OBMC, compound type RD and inter prediction
      buffers;
    - in the lookahead, the slots for past source frames
      (av1_lookahead_init()).

    aom_codec_enc_config_set() accepted a later non-zero kf_max_dist, but of
    these av1_change_config() allocates only the main thread's compound type
    RD and inter prediction buffers, so the first inter frames used the
    rest:
    - real time: init_mv_cost_params() leaves the cost table pointers of the
      full-pixel search uninitialized because x->mv_costs is NULL, and
      mv_cost() reads through them;
    - good quality: init_simple_motion_search_mvs() dereferences the NULL
      simple motion search tree, and with lag_in_frames > 0 the lookahead's
      first pass reads the NULL past source frame (unscaled_last_source) in
      firstpass_inter_prediction();
    - g_threads > 1: prepare_enc_workers() copies the main thread's NULL
      motion vector costs to each worker.

    Reject the change in encoder_set_config(), as is already done for
    leaving monochrome, and document it at kf_max_dist in aom_encoder.h. The
    check uses the current kf_max_dist, so an encoder that was reconfigured
    to kf_max_dist 0 cannot go back either.

    Bug: 573427857
    Change-Id: I35b461edaaf745387c75b08f9384dc370400d1da

diff --git a/aom/aom_encoder.h b/aom/aom_encoder.h
index 3669f5fcd9..124def7686 100644
--- a/aom/aom_encoder.h
+++ b/aom/aom_encoder.h
@@ -795,6 +795,11 @@ typedef struct aom_codec_enc_cfg {
    * a keyframe if one has not been coded in the last kf_max_dist frames.
    * A value of 0 implies all frames will be keyframes. Set kf_min_dist
    * equal to kf_max_dist for a fixed interval.
+   *
+   * With a value of 0 the encoder does not allocate the buffers that only
+   * inter frames use, so once kf_max_dist is 0 (at initialization or after
+   * aom_codec_enc_config_set()), aom_codec_enc_config_set() rejects a
+   * nonzero kf_max_dist with #AOM_CODEC_INVALID_PARAM.
    */
   unsigned int kf_max_dist;

diff --git a/av1/av1_cx_iface.c b/av1/av1_cx_iface.c
index 6ec3e27e21..3c509066f8 100644
--- a/av1/av1_cx_iface.c
+++ b/av1/av1_cx_iface.c
@@ -1726,6 +1726,15 @@ static aom_codec_err_t encoder_set_config(aom_codec_alg_priv_t *ctx,
     ERROR("Cannot change to monochrome = 0 after init with monochrome");
   }

+  // With kf_max_dist == 0 every frame is a key frame, so the encoder does not
+  // allocate what only inter frames use: the motion vector costs, the simple
+  // motion search tree and the inter prediction buffers of the main and worker
+  // threads, and the lookahead's past source frames. Of these, a later
+  // reconfiguration allocates only some of the main thread's inter
+  // prediction buffers, so inter frames cannot be enabled after that.
+  if (ctx->cfg.kf_max_dist == 0 && cfg->kf_max_dist != 0)
+    ERROR("Cannot change kf_max_dist from 0");
+
   // Prevent increasing lag_in_frames. This check is stricter than it needs
   // to be -- the limit is not increasing past the first lag_in_frames
   // value, but we don't track the initial config, only the last successful
diff --git a/test/encode_api_test.cc b/test/encode_api_test.cc
index fe185b6315..c0eb48f936 100644
--- a/test/encode_api_test.cc
+++ b/test/encode_api_test.cc
@@ -6039,4 +6039,83 @@ const IntraBCTilesParams kIntraBCTilesParams[] = {
 INSTANTIATE_TEST_SUITE_P(AV1, IntraBCTilesTest,
                          ::testing::ValuesIn(kIntraBCTilesParams));

+// With kf_max_dist 0 every frame is a key frame, and the encoder does not
+// allocate what only inter frames use, so aom_codec_enc_config_set() must
+// reject a nonzero kf_max_dist, whether kf_max_dist was 0 at init or was set
+// to 0 later. Otherwise the first inter frame reads the missing motion vector
+// costs (real-time), simple motion search tree (good quality) or past source
+// frame (good quality with lag_in_frames > 0), or copies the missing motion
+// vector costs to the worker threads (g_threads > 1).
+void ConfigSetKeyFrameDistanceFromZeroTest(unsigned int usage, int speed,
+                                           unsigned int threads,
+                                           unsigned int lag_in_frames) {
+  for (const unsigned int initial_kf_max_dist : { 0u, 30u }) {
+    SCOPED_TRACE(initial_kf_max_dist);
+    aom_codec_iface_t *iface = aom_codec_av1_cx();
+    aom_codec_enc_cfg_t cfg;
+    ASSERT_EQ(aom_codec_enc_config_default(iface, &cfg, usage), AOM_CODEC_OK);
+    cfg.g_w = 176;
+    cfg.g_h = 144;
+    cfg.g_threads = threads;
+    cfg.g_lag_in_frames = lag_in_frames;
+    cfg.kf_max_dist = initial_kf_max_dist;
+    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, speed), AOM_CODEC_OK);
+
+    aom_image_t *img =
+        aom_img_alloc(nullptr, AOM_IMG_FMT_I420, cfg.g_w, cfg.g_h, 1);
+    ASSERT_NE(img, nullptr);
+    int pts = 0;
+    for (; pts < 2; ++pts) {
+      FillImageRandom(img);
+      ASSERT_EQ(aom_codec_encode(&enc, img, pts, 1, 0), AOM_CODEC_OK);
+    }
+    if (cfg.kf_max_dist != 0) {
+      cfg.kf_max_dist = 0;
+      ASSERT_EQ(aom_codec_enc_config_set(&enc, &cfg), AOM_CODEC_OK);
+      FillImageRandom(img);
+      ASSERT_EQ(aom_codec_encode(&enc, img, pts++, 1, 0), AOM_CODEC_OK);
+    }
+
+    cfg.kf_max_dist = 30;
+    EXPECT_EQ(aom_codec_enc_config_set(&enc, &cfg), AOM_CODEC_INVALID_PARAM);
+    for (const int end = pts + 4; pts < end; ++pts) {
+      FillImageRandom(img);
+      ASSERT_EQ(aom_codec_encode(&enc, img, pts, 1, 0), AOM_CODEC_OK);
+    }
+    ASSERT_EQ(aom_codec_encode(&enc, nullptr, 0, 1, 0), AOM_CODEC_OK);
+    aom_img_free(img);
+    ASSERT_EQ(aom_codec_destroy(&enc), AOM_CODEC_OK);
+  }
+}
+
+TEST(EncodeAPI, ConfigSetKeyFrameDistanceFromZeroRealtime) {
+  for (const int speed : { 7, 10 }) {
+    SCOPED_TRACE(speed);
+    ConfigSetKeyFrameDistanceFromZeroTest(AOM_USAGE_REALTIME, speed,
+                                          /*threads=*/1, /*lag_in_frames=*/0);
+  }
+}
+
+TEST(EncodeAPI, ConfigSetKeyFrameDistanceFromZeroRealtimeMultiThread) {
+  ConfigSetKeyFrameDistanceFromZeroTest(AOM_USAGE_REALTIME, /*speed=*/7,
+                                        /*threads=*/4, /*lag_in_frames=*/0);
+}
+
+#if !CONFIG_REALTIME_ONLY
+TEST(EncodeAPI, ConfigSetKeyFrameDistanceFromZeroGoodQuality) {
+  for (const unsigned int lag_in_frames : { 0u, 10u }) {
+    SCOPED_TRACE(lag_in_frames);
+    ConfigSetKeyFrameDistanceFromZeroTest(AOM_USAGE_GOOD_QUALITY, /*speed=*/6,
+                                          /*threads=*/1, lag_in_frames);
+  }
+}
+
+TEST(EncodeAPI, ConfigSetKeyFrameDistanceFromZeroGoodQualityMultiThread) {
+  ConfigSetKeyFrameDistanceFromZeroTest(AOM_USAGE_GOOD_QUALITY, /*speed=*/6,
+                                        /*threads=*/4, /*lag_in_frames=*/0);
+}
+#endif  // !CONFIG_REALTIME_ONLY
+
 }  // namespace