Commit 24040af58b for aom

commit 24040af58bd174658afcacf7307548dc58192e10
Author: Jerome Jiang <jianj@google.com>
Date:   Tue Nov 24 14:33:05 2026 -0500

    av1: Fix OOB mv_cost read for compound NEWMV

    - Use av1_get_ref_mv() in build_cur_mv() for NEWMV components so
      NEAR_NEWMV and NEW_NEARMV read ref_mv_idx + 1 instead of
      ref_mv_idx.
    - Run ref_mv_idx_early_breakout() before returning early for
      ref_set == 1 in ref_mv_idx_to_search() so invalid single_newmv
      candidates are skipped instead of reaching handle_newmv().
    - Add MV_MAX range assertions in mv_cost() and av1_mv_bit_cost()
      and a regression test in EncodeAPI.NearNewmvFarProjectedRefMv.

    STATS_CHANGED

    Bug: 585490691
    Fixed: 585490691
    Change-Id: Idf6a9027ee61124bb004c4962b267306f915a061

diff --git a/av1/encoder/mcomp.c b/av1/encoder/mcomp.c
index 7ad9951620..ad875dab62 100644
--- a/av1/encoder/mcomp.c
+++ b/av1/encoder/mcomp.c
@@ -295,6 +295,8 @@ int av1_init_search_range(int size) {
 // vector.
 static inline int mv_cost(const MV *mv, const int *joint_cost,
                           const int *const comp_cost[2]) {
+  assert(abs(mv->row) <= MV_MAX);
+  assert(abs(mv->col) <= MV_MAX);
   return joint_cost[av1_get_mv_joint(mv)] + comp_cost[0][mv->row] +
          comp_cost[1][mv->col];
 }
@@ -306,6 +308,8 @@ static inline int mv_cost(const MV *mv, const int *joint_cost,
 // This is NOT used during motion compensation.
 int av1_mv_bit_cost(const MV *mv, const MV *ref_mv, const int *mvjcost,
                     int *const mvcost[2], int weight) {
+  assert(abs(mv->row - ref_mv->row) <= MV_MAX);
+  assert(abs(mv->col - ref_mv->col) <= MV_MAX);
   const MV diff = { mv->row - ref_mv->row, mv->col - ref_mv->col };
   return ROUND_POWER_OF_TWO(
       mv_cost(&diff, mvjcost, CONVERT_TO_CONST_MVCOST(mvcost)) * weight, 7);
diff --git a/av1/encoder/rdopt.c b/av1/encoder/rdopt.c
index a5dde009a9..80514081c9 100644
--- a/av1/encoder/rdopt.c
+++ b/av1/encoder/rdopt.c
@@ -2238,12 +2238,8 @@ static inline int build_cur_mv(int_mv *cur_mv, PREDICTION_MODE this_mode,
     if (!ret) return 0;
     const PREDICTION_MODE single_mode = get_single_mode(this_mode, i);
     if (single_mode == NEWMV) {
-      const uint8_t ref_frame_type = av1_ref_frame_type(mbmi->ref_frame);
-      cur_mv[i] =
-          (i == 0) ? x->mbmi_ext.ref_mv_stack[ref_frame_type][mbmi->ref_mv_idx]
-                         .this_mv
-                   : x->mbmi_ext.ref_mv_stack[ref_frame_type][mbmi->ref_mv_idx]
-                         .comp_mv;
+      assert(mbmi->mode == this_mode);
+      cur_mv[i] = av1_get_ref_mv(x, i);
     } else {
       ret &= clamp_and_check_mv(cur_mv + i, this_mv, cm, x);
     }
@@ -2473,9 +2469,6 @@ static int ref_mv_idx_to_search(AV1_COMP *const cpi, MACROBLOCK *x,
                                 HandleInterModeArgs *const args,
                                 int64_t ref_best_rd, BLOCK_SIZE bsize,
                                 const int ref_set) {
-  // If the number of ref mv count is equal to 1, do not prune the same. It
-  // is better to evaluate the same than to prune it.
-  if (ref_set == 1) return 1;
   AV1_COMMON *const cm = &cpi->common;
   const MACROBLOCKD *const xd = &x->e_mbd;
   const MB_MODE_INFO *const mbmi = xd->mi[0];
@@ -2491,6 +2484,10 @@ static int ref_mv_idx_to_search(AV1_COMP *const cpi, MACROBLOCK *x,
     mask_set_bit(&good_indices, i);
   }

+  // If there is only 1 ref mv candidate, or if all candidates were pruned by
+  // ref_mv_idx_early_breakout(), skip the simple translation pruning below.
+  if (ref_set == 1 || good_indices == 0) return good_indices;
+
   // Only prune in NEARMV mode, if the speed feature is set, and the block size
   // is large enough. If these conditions are not met, return all good indices
   // found so far.
diff --git a/test/encode_api_test.cc b/test/encode_api_test.cc
index f58fc72257..2598c192e5 100644
--- a/test/encode_api_test.cc
+++ b/test/encode_api_test.cc
@@ -4262,6 +4262,159 @@ TEST(EncodeAPI, Buganizer580372713) {
   aom_img_free(img_large);
   ASSERT_EQ(aom_codec_destroy(&enc), AOM_CODEC_OK);
 }
+
+// Regression test for b/585490691: When opposite temporal motion vectors
+// project to a compound ref_mv_stack with |stack[0] - stack[1]| > MV_MAX and
+// ref_mv_count == 2 (ref_set == 1 for NEAR_NEWMV / NEW_NEARMV), bypassing
+// ref_mv_idx_early_breakout() entered handle_newmv() with single_newmv_valid
+// == 0 and build_cur_mv()'s off-by-one stack index caused av1_mv_bit_cost() to
+// read out of bounds past MvCosts::nmv_cost_hp_alloc.
+TEST(EncodeAPI, NearNewmvFarProjectedRefMv) {
+  constexpr int kWidth = 2304;
+  constexpr int kHeight = 24;
+  constexpr int kTexWidth = 1024;
+  constexpr int kGfLen = 28;
+  constexpr int kStripStart = 1024;
+  constexpr int kStripEnd = 1280;
+  constexpr int kShift = 42;
+
+  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_GOOD_QUALITY),
+            AOM_CODEC_OK);
+  cfg.g_w = kWidth;
+  cfg.g_h = kHeight;
+  cfg.g_pass = AOM_RC_ONE_PASS;
+  cfg.rc_end_usage = AOM_Q;
+  cfg.g_lag_in_frames = 35;
+  cfg.g_threads = 1;
+  cfg.kf_min_dist = 100;
+  cfg.kf_max_dist = 100;
+
+  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, 0), AOM_CODEC_OK);
+  ASSERT_EQ(aom_codec_control(&enc, AOME_SET_CQ_LEVEL, 24), AOM_CODEC_OK);
+  ASSERT_EQ(aom_codec_control(&enc, AOME_SET_SHARPNESS, 1), AOM_CODEC_OK);
+  ASSERT_EQ(aom_codec_control(&enc, AOME_SET_ENABLEAUTOALTREF, 1),
+            AOM_CODEC_OK);
+  ASSERT_EQ(aom_codec_control(&enc, AV1E_SET_MIN_GF_INTERVAL, kGfLen),
+            AOM_CODEC_OK);
+  ASSERT_EQ(aom_codec_control(&enc, AV1E_SET_MAX_GF_INTERVAL, kGfLen),
+            AOM_CODEC_OK);
+  ASSERT_EQ(aom_codec_control(&enc, AV1E_SET_GF_MAX_PYRAMID_HEIGHT, 1),
+            AOM_CODEC_OK);
+  ASSERT_EQ(aom_codec_control(&enc, AV1E_SET_MIN_PARTITION_SIZE, 8),
+            AOM_CODEC_OK);
+  ASSERT_EQ(aom_codec_control(&enc, AV1E_SET_MAX_PARTITION_SIZE, 8),
+            AOM_CODEC_OK);
+  ASSERT_EQ(aom_codec_control(&enc, AV1E_SET_ENABLE_TPL_MODEL, 0),
+            AOM_CODEC_OK);
+  ASSERT_EQ(aom_codec_control(&enc, AV1E_SET_ENABLE_REF_FRAME_MVS, 1),
+            AOM_CODEC_OK);
+  ASSERT_EQ(aom_codec_control(&enc, AV1E_SET_ALLOW_REF_FRAME_MVS, 1),
+            AOM_CODEC_OK);
+  ASSERT_EQ(aom_codec_control(&enc, AV1E_SET_ENABLE_ORDER_HINT, 1),
+            AOM_CODEC_OK);
+  ASSERT_EQ(aom_codec_control(&enc, AV1E_SET_ENABLE_GLOBAL_MOTION, 0),
+            AOM_CODEC_OK);
+  ASSERT_EQ(aom_codec_control(&enc, AV1E_SET_ENABLE_WARPED_MOTION, 0),
+            AOM_CODEC_OK);
+  ASSERT_EQ(aom_codec_control(&enc, AV1E_SET_ENABLE_OBMC, 0), AOM_CODEC_OK);
+  ASSERT_EQ(aom_codec_control(&enc, AV1E_SET_ENABLE_RESTORATION, 0),
+            AOM_CODEC_OK);
+  ASSERT_EQ(aom_codec_control(&enc, AV1E_SET_ENABLE_CDEF, 0), AOM_CODEC_OK);
+  ASSERT_EQ(aom_codec_control(&enc, AV1E_SET_ENABLE_DIFF_WTD_COMP, 0),
+            AOM_CODEC_OK);
+  ASSERT_EQ(aom_codec_control(&enc, AV1E_SET_ENABLE_DIST_WTD_COMP, 0),
+            AOM_CODEC_OK);
+  ASSERT_EQ(aom_codec_control(&enc, AV1E_SET_ENABLE_INTERINTRA_COMP, 0),
+            AOM_CODEC_OK);
+  ASSERT_EQ(aom_codec_control(&enc, AV1E_SET_ENABLE_ONESIDED_COMP, 1),
+            AOM_CODEC_OK);
+  ASSERT_EQ(aom_codec_control(&enc, AV1E_SET_ENABLE_DUAL_FILTER, 0),
+            AOM_CODEC_OK);
+  ASSERT_EQ(aom_codec_control(&enc, AV1E_SET_ENABLE_TX64, 0), AOM_CODEC_OK);
+
+  std::vector<uint8_t> tex(kHeight * kTexWidth);
+  for (int y = 0; y < kHeight; ++y) {
+    for (int x = 0; x < kTexWidth; ++x) {
+      const double v = 128.0 + 30.0 * std::sin(x * 0.031 + y * 0.13) +
+                       16.0 * std::cos(x * 0.097 - y * 0.21) +
+                       10.0 * std::sin(x * 0.37 + y * 0.41);
+      tex[y * kTexWidth + x] = static_cast<uint8_t>(v);
+    }
+  }
+
+  auto copy_strip = [&](aom_image_t *img, int y0, int y1, int shift_x, int x0,
+                        int x1) {
+    for (int y = y0; y < y1; ++y) {
+      uint8_t *row = img->planes[AOM_PLANE_Y] + y * img->stride[AOM_PLANE_Y];
+      for (int x = x0; x < x1; ++x) {
+        int sx = (x - shift_x) % kTexWidth;
+        if (sx < 0) sx += kTexWidth;
+        row[x] = tex[y * kTexWidth + sx];
+      }
+    }
+  };
+
+#if CONFIG_AV1_DECODER
+  aom_codec_ctx_t dec;
+  ASSERT_EQ(aom_codec_dec_init(&dec, aom_codec_av1_dx(), nullptr, 0),
+            AOM_CODEC_OK);
+#endif
+  auto drain = [&]() {
+    aom_codec_iter_t iter = nullptr;
+    const aom_codec_cx_pkt_t *pkt;
+    while ((pkt = aom_codec_get_cx_data(&enc, &iter)) != nullptr) {
+#if CONFIG_AV1_DECODER
+      if (pkt->kind == AOM_CODEC_CX_FRAME_PKT) {
+        ASSERT_EQ(aom_codec_decode(
+                      &dec, static_cast<const uint8_t *>(pkt->data.frame.buf),
+                      pkt->data.frame.sz, nullptr),
+                  AOM_CODEC_OK);
+      }
+#endif
+    }
+  };
+
+  aom_image_t *img =
+      aom_img_alloc(nullptr, AOM_IMG_FMT_I420, kWidth, kHeight, 16);
+  ASSERT_NE(img, nullptr);
+
+  for (int f = 0; f <= kGfLen; ++f) {
+    FillImage(img, 128);
+    if (f == 1) {
+      copy_strip(img, 0, kHeight, 0, kStripStart - 64, kStripEnd + 64);
+    } else if (f == 2 || f == 3) {
+      // Opposite horizontal shifts in neighboring 8px bands so frame 3
+      // projects opposite high-magnitude MVs onto ALTREF (frame kGfLen).
+      copy_strip(img, 0, 8, kShift, kStripStart, kStripEnd);
+      copy_strip(img, 8, kHeight, -kShift, kStripStart, kStripEnd);
+    } else if (f == kGfLen) {
+      for (int y = 0; y < kHeight; ++y) {
+        memset(img->planes[AOM_PLANE_Y] + y * img->stride[AOM_PLANE_Y], 200,
+               kWidth);
+      }
+    }
+    ASSERT_EQ(aom_codec_encode(&enc, img, f, 1, 0), AOM_CODEC_OK);
+    ASSERT_NO_FATAL_FAILURE(drain());
+  }
+
+  // Flush through frame 3 (plus keyframe and ALTREF) to hit the projected
+  // compound ref_mv_stack on frame 3 without encoding all remaining flat
+  // frames.
+  for (int i = 0; i < 4; ++i) {
+    ASSERT_EQ(aom_codec_encode(&enc, nullptr, 0, 0, 0), AOM_CODEC_OK);
+    ASSERT_NO_FATAL_FAILURE(drain());
+  }
+
+  aom_img_free(img);
+#if CONFIG_AV1_DECODER
+  ASSERT_EQ(aom_codec_destroy(&dec), AOM_CODEC_OK);
+#endif
+  ASSERT_EQ(aom_codec_destroy(&enc), AOM_CODEC_OK);
+}
 #endif  // !CONFIG_REALTIME_ONLY

 // Regression test for b/568565869: Undefined left-shift overflow of