Commit 2f56451048 for aom
commit 2f56451048a95f45446dc83ca6cfa125a1be344a
Author: Frank Galligan <fgalligan@google.com>
Date: Mon Nov 23 13:12:07 2026 -0800
av1: Avoid int overflow in TF partition decision
In tf_motion_search(), 32x32 mid-block and 16x16 sub-block motion
searches are skipped when force_integer_mv is true, leaving
midblock_mses and subblock_mses at their INT_MAX sentinel values.
Calling tf_determine_block_partition() (updated in commit 6a64bb332e) in
that case multiplied INT_MAX by 15 in 32-bit signed int.
Skip tf_determine_block_partition() when force_integer_mv is true so the
whole-block MV and MSE are copied directly to all sub-blocks, widen MSE
differences and products in tf_determine_block_partition() to int64_t
for defense in depth, and add an EncodeAPI regression test. This change
does not affect encoder output.
Bug: 589789465
Fixed: 589789465
TAG=agy
CONV=89c75cd5-0385-479f-9080-cae000df05a4
Change-Id: Ibdb22d46b9553ec851ad62265fcc4ccab47b4c7b
diff --git a/av1/encoder/temporal_filter.c b/av1/encoder/temporal_filter.c
index fd9731f6ba..8e4cc67377 100644
--- a/av1/encoder/temporal_filter.c
+++ b/av1/encoder/temporal_filter.c
@@ -259,7 +259,7 @@ static void tf_motion_search(AV1_COMP *cpi, MACROBLOCK *mb,
const int step_param = av1_init_search_range(
AOMMAX(frame_to_filter->y_crop_width, frame_to_filter->y_crop_height));
const SUBPEL_SEARCH_TYPE subpel_search_type = USE_8_TAPS;
- const int force_integer_mv = cpi->common.features.cur_frame_force_integer_mv;
+ const bool force_integer_mv = cpi->common.features.cur_frame_force_integer_mv;
const MV_COST_TYPE mv_cost_type =
min_frame_size >= 720
? MV_COST_L1_HDRES
@@ -351,7 +351,7 @@ static void tf_motion_search(AV1_COMP *cpi, MACROBLOCK *mb,
cond_cost_list(cpi, cost_list, SUBPEL_SEARCH_TF),
&best_mv.as_fullmv, &best_mv_stats, NULL);
- if (force_integer_mv == 1) { // Only do full search on the entire block.
+ if (force_integer_mv) { // Only do full search on the entire block.
const int mv_row = best_mv.as_mv.row;
const int mv_col = best_mv.as_mv.col;
best_mv.as_mv.row = GET_MV_SUBPEL(mv_row);
@@ -459,8 +459,9 @@ static void tf_motion_search(AV1_COMP *cpi, MACROBLOCK *mb,
mb->plane[0].src = ori_src_buf;
mbd->plane[0].pre[0] = ori_pre_buf;
- // Make partition decision.
- if (allow_me_for_sub_blks) {
+ // Make partition decision. When force_integer_mv is set, mid-block and
+ // sub-block motion searches are skipped above.
+ if (!force_integer_mv && allow_me_for_sub_blks) {
tf_determine_block_partition(block_mv, block_mse, midblock_mvs,
midblock_mses, subblock_mvs, subblock_mses);
} else {
@@ -516,10 +517,11 @@ static void tf_determine_block_partition(const MV block_mv, const int block_mse,
max_subblock_mse = AOMMAX(max_subblock_mse, subblock_mses[i]);
}
- if (((midblock_mses[idx] * 15 <= sum_subblock_mse * 4) &&
- max_subblock_mse - min_subblock_mse < 48) ||
- ((midblock_mses[idx] * 14 <= sum_subblock_mse * 4) &&
- max_subblock_mse - min_subblock_mse < 24)) { // No split.
+ const int64_t mid_diff_mse = (int64_t)max_subblock_mse - min_subblock_mse;
+ if ((((int64_t)midblock_mses[idx] * 15 <= sum_subblock_mse * 4) &&
+ mid_diff_mse < 48) ||
+ (((int64_t)midblock_mses[idx] * 14 <= sum_subblock_mse * 4) &&
+ mid_diff_mse < 24)) { // No split.
for (i = sub_idx; i < sub_idx + 4; ++i) {
subblock_mvs[i] = midblock_mvs[idx];
subblock_mses[i] = midblock_mses[idx];
@@ -536,11 +538,11 @@ static void tf_determine_block_partition(const MV block_mv, const int block_mse,
max_subblock_mse = AOMMAX(max_subblock_mse, subblock_mses[i]);
}
- if (((block_mse * 15 <= sum_subblock_mse) &&
- (max_subblock_mse - min_subblock_mse) * 16 < sum_subblock_mse * 3) ||
- ((block_mse * 14 <= sum_subblock_mse) &&
- (max_subblock_mse - min_subblock_mse) * 8 <
- sum_subblock_mse)) { // No split.
+ const int64_t diff_mse = (int64_t)max_subblock_mse - min_subblock_mse;
+ if ((((int64_t)block_mse * 15 <= sum_subblock_mse) &&
+ diff_mse * 16 < sum_subblock_mse * 3) ||
+ (((int64_t)block_mse * 14 <= sum_subblock_mse) &&
+ diff_mse * 8 < sum_subblock_mse)) { // No split.
for (i = 0; i < NUM_16X16; ++i) {
subblock_mvs[i] = block_mv;
subblock_mses[i] = block_mse;
diff --git a/test/encode_api_test.cc b/test/encode_api_test.cc
index 2598c192e5..d7eead9e64 100644
--- a/test/encode_api_test.cc
+++ b/test/encode_api_test.cc
@@ -6366,4 +6366,94 @@ TEST(EncodeAPI, ConfigSetAllKeyFramesAndLargerSize) {
ASSERT_EQ(aom_codec_destroy(&enc), AOM_CODEC_OK);
}
}
+
+#if !CONFIG_REALTIME_ONLY
+// Regression test for bug 589789465: exercise temporal filtering on high-
+// contrast screen content when cur_frame_force_integer_mv is active (where
+// sub-block TF motion search is skipped while allow_me_for_sub_blks is true).
+TEST(EncodeAPI, ScreenContentForceIntegerMvTemporalFilter) {
+ constexpr int kWidth = 258;
+ constexpr int kHeight = 146;
+ constexpr int kFrames = 30;
+
+ 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_timebase.num = 1;
+ cfg.g_timebase.den = 30;
+ cfg.g_pass = AOM_RC_ONE_PASS;
+
+ aom_codec_ctx_t enc_ctx;
+ ASSERT_EQ(aom_codec_enc_init(&enc_ctx, iface, &cfg, 0), AOM_CODEC_OK);
+ struct CodecDeleter {
+ void operator()(aom_codec_ctx_t *c) const { aom_codec_destroy(c); }
+ };
+ std::unique_ptr<aom_codec_ctx_t, CodecDeleter> enc(&enc_ctx);
+ ASSERT_EQ(aom_codec_control(enc.get(), AOME_SET_CPUUSED, 5), AOM_CODEC_OK);
+
+ std::unique_ptr<aom_image_t, decltype(&aom_img_free)> img(
+ aom_img_alloc(nullptr, AOM_IMG_FMT_I420, kWidth, kHeight, 16),
+ &aom_img_free);
+ ASSERT_NE(img.get(), nullptr);
+ // Static SMPTE-like color bars with a ramp and contrast step trigger screen-
+ // content integer MVs on the first GF group and sub-block temporal filtering
+ // on the following group.
+ static constexpr uint8_t kBarY[7] = { 180, 168, 145, 133, 63, 51, 28 };
+ static constexpr uint8_t kBarU[7] = { 128, 44, 147, 63, 193, 109, 212 };
+ static constexpr uint8_t kBarV[7] = { 128, 136, 44, 52, 204, 212, 120 };
+ for (int r = 0; r < kHeight; ++r) {
+ uint8_t *row = img->planes[0] + r * img->stride[0];
+ for (int c = 0; c < kWidth; ++c) {
+ if (r >= 96 && r < 112) {
+ row[c] = (c < 60) ? ((c < 32) ? 219 : 44)
+ : static_cast<uint8_t>(((c - 60) * 255) / 198);
+ } else if (r >= 112) {
+ row[c] = (c >= 74 && c < 130) ? 235 : 16;
+ } else {
+ row[c] = kBarY[(c * 7) / kWidth];
+ }
+ }
+ }
+ const int uv_h = aom_img_plane_height(img.get(), 1);
+ const int uv_w = aom_img_plane_width(img.get(), 1);
+ for (int r = 0; r < uv_h; ++r) {
+ uint8_t *u_row = img->planes[1] + r * img->stride[1];
+ uint8_t *v_row = img->planes[2] + r * img->stride[2];
+ const bool bottom = r >= (uv_h * 3) / 4;
+ for (int c = 0; c < uv_w; ++c) {
+ const int idx = bottom ? 0 : ((c * 7) / uv_w);
+ u_row[c] = kBarU[idx];
+ v_row[c] = kBarV[idx];
+ }
+ }
+
+ int encoded_frames = 0;
+ for (int f = 0; f < kFrames; ++f) {
+ ASSERT_EQ(aom_codec_encode(enc.get(), img.get(), f, 1, 0), AOM_CODEC_OK);
+ aom_codec_iter_t iter = nullptr;
+ while (const aom_codec_cx_pkt_t *pkt =
+ aom_codec_get_cx_data(enc.get(), &iter)) {
+ if (pkt->kind == AOM_CODEC_CX_FRAME_PKT) ++encoded_frames;
+ }
+ }
+ img.reset();
+ bool got_pkts = false;
+ do {
+ got_pkts = false;
+ ASSERT_EQ(aom_codec_encode(enc.get(), nullptr, -1, 0, 0), AOM_CODEC_OK);
+ aom_codec_iter_t iter = nullptr;
+ while (const aom_codec_cx_pkt_t *pkt =
+ aom_codec_get_cx_data(enc.get(), &iter)) {
+ got_pkts = true;
+ if (pkt->kind == AOM_CODEC_CX_FRAME_PKT) ++encoded_frames;
+ }
+ } while (got_pkts);
+ EXPECT_EQ(encoded_frames, kFrames);
+ ASSERT_EQ(aom_codec_destroy(enc.release()), AOM_CODEC_OK);
+}
+#endif // !CONFIG_REALTIME_ONLY
+
} // namespace