Commit 9c42d63951 for aom
commit 9c42d639511a0479fec10bc58dec44edbc6df537
Author: Ihor Buhaiov <freedbrt@gmail.com>
Date: Fri Nov 13 12:59:29 2026 +0100
rtc: Use the best reference for the IDTX uv check
After the inter mode loop, av1_nonrd_pick_inter_mode_sb() restores the
best mode, but xd->plane[].pre[] and the block scale factors still
point at the reference of the last mode searched. The IDTX test in
handle_screen_content_mode_nonrd() builds its U and V prediction from
them. When GOLDEN was searched last and LAST won, the uv model measures
GOLDEN at the LAST motion vector. If GOLDEN happens to match the source
chroma there, the model gives rate 0 and skip, IDTX wins, and the block
is coded as skip with the chroma of LAST. That chroma can be far off.
On screen content this is one block with the wrong color, and it stays
while the block keeps skipping.
Point the prediction planes and scale factors at the best references
once the best mode is restored, if U or V color sensitivity is set. The
setup moves into a helper that the mode loop uses too. mi->ref_frame[1]
is now also restored for a single reference best mode, so a compound
mode searched last can't make that prediction compound when the intra
search is skipped.
The IDTX test only runs at 8 bit, so 10 bit output doesn't change.
Speed 11, tune screen, VBR q 9 to 15, ncpeer tool settings, Apple M3
Pro unless noted. A 19x94 crop of a screen capture, 4:2:0, 1 thread,
active map: worst U block 8.86 to 33.25 dB, bytes +0.73%. On 4 screen
recordings of 40 to 60 frames (1 and 4 threads with the map, 4 threads
without):
bytes PSNR change
4:2:0 -0.22% to +0.18% 0.02 dB or less
4:4:4 -0.12% to +0.06% 0.04 dB or less
One V block in a scroll clip moves between about 28 and 37 dB with
either build, depending on threads and map. It is not an IDTX block.
Encode time, release -O3, base and fix alternating with a second base
run for A/A. Mac: sum over the 4 recordings of the median of 9 runs.
x86 (WSL gcc 13.3): sum over 2 of them of the median of 7 runs.
1 thread 4 threads 4 threads no map
Mac 4:2:0 -0.63% -0.53% -0.04%
4:4:4 +0.11% -0.37% -0.02%
A/A range -1.45% to +0.45%
x86 4:2:0 +0.81% +0.71%
4:4:4 +0.45% +0.70%
A/A range +0.00% to +0.50%
Test: EncodeAPI.ScreenIdtxChromaOfBestReference fails without the fix
(chroma off by 53 in 4:2:0 and 55 in 4:4:4 at speeds 7 to 11) and
passes with it, in a default and a realtime only build. EncodeAPI,
ActiveMap, InactiveArea, ROIMap, ScreenContent and IntraBC tiles tests
pass in both builds. 10 bit: park_joy_90p and screendata 1080p, 4:2:0
and 4:4:4, give the same streams, no recon mismatch. On x86 with gcc and
clang the same 4 recordings and 2 10 bit clips decode without recon or
dav1d mismatch and gcc and clang give the same streams.
Change-Id: Id59f85bb8dfd779e385c84ae1c04a21b95a8646c
diff --git a/av1/encoder/nonrd_pickmode.c b/av1/encoder/nonrd_pickmode.c
index 0277d73da2..0e7540fc3f 100644
--- a/av1/encoder/nonrd_pickmode.c
+++ b/av1/encoder/nonrd_pickmode.c
@@ -2671,6 +2671,32 @@ static AOM_FORCE_INLINE void set_params_nonrd_pick_inter_mode(
}
}
+// Points the prediction planes and scale factors of the block at the given
+// reference frames.
+static inline void set_pred_ref_frames(
+ const AV1_COMMON *cm, MACROBLOCKD *xd,
+ const InterModeSearchStateNonrd *search_state,
+ const struct scale_factors *sf_no_scale, MV_REFERENCE_FRAME ref_frame,
+ MV_REFERENCE_FRAME ref_frame2) {
+ const int is_single_pred = ref_frame2 <= INTRA_FRAME;
+ for (int plane = 0; plane < MAX_MB_PLANE; plane++) {
+ xd->plane[plane].pre[0] = search_state->yv12_mb[ref_frame][plane];
+ if (!is_single_pred)
+ xd->plane[plane].pre[1] = search_state->yv12_mb[ref_frame2][plane];
+ }
+ set_ref_ptrs(cm, xd, ref_frame, ref_frame2);
+
+ // Check if the scaled reference frame should be used. This is set in the
+ // find_predictors() for each usable reference. If so, set the
+ // block_ref_scale_factors[] to no reference scaling.
+ if (search_state->use_scaled_ref_frame[ref_frame]) {
+ xd->block_ref_scale_factors[0] = sf_no_scale;
+ }
+ if (!is_single_pred && search_state->use_scaled_ref_frame[ref_frame2]) {
+ xd->block_ref_scale_factors[1] = sf_no_scale;
+ }
+}
+
// Function to check the inter mode can be skipped based on mode statistics and
// speed features settings.
static AOM_FORCE_INLINE bool skip_inter_mode_nonrd(
@@ -3823,25 +3849,10 @@ void av1_nonrd_pick_inter_mode_sb(AV1_COMP *cpi, TileDataEnc *tile_data,
continue;
// Select prediction reference frames.
- for (int plane = 0; plane < MAX_MB_PLANE; plane++) {
- xd->plane[plane].pre[0] = search_state.yv12_mb[ref_frame][plane];
- if (!is_single_pred)
- xd->plane[plane].pre[1] = search_state.yv12_mb[ref_frame2][plane];
- }
-
mi->ref_frame[0] = ref_frame;
mi->ref_frame[1] = ref_frame2;
- set_ref_ptrs(cm, xd, ref_frame, ref_frame2);
-
- // Check if the scaled reference frame should be used. This is set in the
- // find_predictors() for each usable reference. If so, set the
- // block_ref_scale_factors[] to no reference scaling.
- if (search_state.use_scaled_ref_frame[ref_frame]) {
- xd->block_ref_scale_factors[0] = &sf_no_scale;
- }
- if (!is_single_pred && search_state.use_scaled_ref_frame[ref_frame2]) {
- xd->block_ref_scale_factors[1] = &sf_no_scale;
- }
+ set_pred_ref_frames(cm, xd, &search_state, &sf_no_scale, ref_frame,
+ ref_frame2);
// Perform inter mode evaluation for non-rd
if (!handle_inter_mode_nonrd(
@@ -3868,18 +3879,26 @@ void av1_nonrd_pick_inter_mode_sb(AV1_COMP *cpi, TileDataEnc *tile_data,
mi->tx_size = best_pickmode->best_tx_size;
memset(mi->inter_tx_size, mi->tx_size, sizeof(mi->inter_tx_size));
mi->ref_frame[0] = best_pickmode->best_ref_frame;
+ mi->ref_frame[1] = best_pickmode->best_second_ref_frame;
mi->mv[0].as_int = search_state
.frame_mv_best[best_pickmode->best_mode]
[best_pickmode->best_ref_frame]
.as_int;
mi->mv[1].as_int = 0;
if (best_pickmode->best_second_ref_frame > INTRA_FRAME) {
- mi->ref_frame[1] = best_pickmode->best_second_ref_frame;
mi->mv[1].as_int = search_state
.frame_mv_best[best_pickmode->best_mode]
[best_pickmode->best_second_ref_frame]
.as_int;
}
+ // The chroma checks below predict from the planes of the best mode.
+ if (search_state.best_rdc.rdcost != INT64_MAX &&
+ (x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_U)] ||
+ x->color_sensitivity[COLOR_SENS_IDX(AOM_PLANE_V)])) {
+ set_pred_ref_frames(cm, xd, &search_state, &sf_no_scale,
+ best_pickmode->best_ref_frame,
+ best_pickmode->best_second_ref_frame);
+ }
// Perform intra prediction search, if the best SAD is above a certain
// threshold.
mi->angle_delta[PLANE_TYPE_Y] = 0;
diff --git a/test/encode_api_test.cc b/test/encode_api_test.cc
index b72872b913..b134d46491 100644
--- a/test/encode_api_test.cc
+++ b/test/encode_api_test.cc
@@ -4794,6 +4794,139 @@ TEST(EncodeAPI, SvcRefFrameConfigUnusedRefIdxWithResolutionChange) {
}
#endif // CONFIG_AV1_DECODER
+#if CONFIG_AV1_DECODER
+// Two level noise that is the same for the same seed and position.
+static uint8_t NoisePixel(int seed, int r, int c) {
+ // 64 bit math, since the integer sanitizer reports unsigned wraparound.
+ uint64_t h = (static_cast<uint64_t>(seed) * 2654435761u ^
+ static_cast<uint64_t>(r / 2) * 40503u ^
+ static_cast<uint64_t>(c / 2) * 2246822519u) &
+ 0xffffffffu;
+ h ^= h >> 13;
+ h = (h * 0x5bd1e995u) & 0xffffffffu;
+ h ^= h >> 15;
+ return (h & 1) ? 220 : 40;
+}
+
+// Fills a 64x64 frame. Frame 0 becomes GOLDEN and frame 1 LAST. In frame 2
+// the text in the left half has moved up by 8 rows since frame 1 and has a
+// new color, which GOLDEN has at the same place.
+static void FillMovedTextNewColor(aom_image_t *img, int frame) {
+ constexpr int kShift = 8;
+ constexpr int kText = 7;
+ const int background = frame == 1 ? 2 : 1;
+ for (int r = 0; r < 64; ++r) {
+ uint8_t *const row = img->planes[AOM_PLANE_Y] + r * img->stride[0];
+ for (int c = 0; c < 64; ++c) {
+ uint8_t v = NoisePixel(background, r, c);
+ if (c < 32) {
+ if (frame == 0 && r < 32) v = NoisePixel(3, r, c);
+ if (frame == 1 && r >= kShift && r < 32 + kShift) {
+ v = NoisePixel(kText, r - kShift, c);
+ }
+ if (frame == 2 && r < 32) v = NoisePixel(kText, r, c);
+ }
+ row[c] = v;
+ }
+ }
+ for (int plane = AOM_PLANE_U; plane <= AOM_PLANE_V; ++plane) {
+ const uint8_t old_color = plane == AOM_PLANE_U ? 100 : 150;
+ const uint8_t new_color = plane == AOM_PLANE_U ? 150 : 100;
+ for (int r = 0; r < aom_img_plane_height(img, plane); ++r) {
+ uint8_t *const row = img->planes[plane] + r * img->stride[plane];
+ const int luma_r = r << img->y_chroma_shift;
+ for (int c = 0; c < aom_img_plane_width(img, plane); ++c) {
+ uint8_t v = 128;
+ if ((c << img->x_chroma_shift) < 32) {
+ if (frame == 0 && luma_r < 32 + 2 * kShift) v = new_color;
+ if (frame == 1 && luma_r >= kShift && luma_r < 32 + kShift) {
+ v = old_color;
+ }
+ if (frame == 2 && luma_r < 32) v = new_color;
+ }
+ row[c] = v;
+ }
+ }
+ }
+}
+
+// LAST is the best reference for the moved text. The identity transform test
+// must measure the chroma of LAST, not of the reference searched last.
+TEST(EncodeAPI, ScreenIdtxChromaOfBestReference) {
+ for (const bool is_444 : { false, true }) {
+ for (int speed = 7; speed <= 11; ++speed) {
+ SCOPED_TRACE(testing::Message()
+ << (is_444 ? "4:4:4" : "4:2:0") << ", speed " << speed);
+ 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 = 64;
+ cfg.g_h = 64;
+ cfg.g_profile = is_444 ? 1 : 0;
+ cfg.g_lag_in_frames = 0;
+ cfg.rc_end_usage = AOM_CBR;
+ cfg.rc_target_bitrate = 1000;
+ 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);
+ ASSERT_EQ(
+ aom_codec_control(&enc, AV1E_SET_TUNE_CONTENT, AOM_CONTENT_SCREEN),
+ AOM_CODEC_OK);
+ ASSERT_EQ(aom_codec_control(&enc, AV1E_SET_AQ_MODE, 0), AOM_CODEC_OK);
+ // The identity transform test needs the inter prediction kept.
+ ASSERT_EQ(aom_codec_control(&enc, AV1E_SET_ENABLE_WARPED_MOTION, 0),
+ AOM_CODEC_OK);
+ // Decode to 8 bit buffers even when FORCE_HIGHBITDEPTH_DECODING is on.
+ aom_codec_dec_cfg_t dec_cfg = {};
+ dec_cfg.allow_lowbitdepth = 1;
+ aom_codec_ctx_t dec;
+ ASSERT_EQ(aom_codec_dec_init(&dec, aom_codec_av1_dx(), &dec_cfg, 0),
+ AOM_CODEC_OK);
+ aom_image_t *img =
+ aom_img_alloc(nullptr, is_444 ? AOM_IMG_FMT_I444 : AOM_IMG_FMT_I420,
+ cfg.g_w, cfg.g_h, 1);
+ ASSERT_NE(img, nullptr);
+
+ int max_diff = 0;
+ for (int frame = 0; frame < 3; ++frame) {
+ FillMovedTextNewColor(img, frame);
+ ASSERT_EQ(aom_codec_encode(&enc, img, frame, 1, 0), AOM_CODEC_OK);
+ aom_codec_iter_t iter = nullptr;
+ const aom_codec_cx_pkt_t *pkt = aom_codec_get_cx_data(&enc, &iter);
+ ASSERT_NE(pkt, nullptr);
+ ASSERT_EQ(aom_codec_decode(
+ &dec, static_cast<const uint8_t *>(pkt->data.frame.buf),
+ pkt->data.frame.sz, nullptr),
+ AOM_CODEC_OK);
+ aom_codec_iter_t dec_iter = nullptr;
+ const aom_image_t *decoded = aom_codec_get_frame(&dec, &dec_iter);
+ ASSERT_NE(decoded, nullptr);
+ if (frame < 2) continue;
+ for (int plane = AOM_PLANE_U; plane <= AOM_PLANE_V; ++plane) {
+ for (int r = 0; r < aom_img_plane_height(img, plane); ++r) {
+ const uint8_t *const src =
+ img->planes[plane] + r * img->stride[plane];
+ const uint8_t *const out =
+ decoded->planes[plane] + r * decoded->stride[plane];
+ for (int c = 0; c < aom_img_plane_width(img, plane); ++c) {
+ const int diff = std::abs(src[c] - out[c]);
+ if (diff > max_diff) max_diff = diff;
+ }
+ }
+ }
+ }
+ // The new color is 50 away from the old one.
+ EXPECT_LT(max_diff, 25);
+
+ aom_img_free(img);
+ ASSERT_EQ(aom_codec_destroy(&dec), AOM_CODEC_OK);
+ ASSERT_EQ(aom_codec_destroy(&enc), AOM_CODEC_OK);
+ }
+ }
+}
+#endif // CONFIG_AV1_DECODER
+
// Verify that when handle_inter_mode_nonrd() reuses the superblock motion
// vector (x->sb_me_mv, searched relative to zero) for NEWMV on screen content,
// the vector is clamped to the valid subpel range relative to the block's