From d1d8e104445dabbab03182d33b130bfec7bbdb27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 26 Aug 2026 23:27:23 +0300 Subject: [PATCH] fix(av1): Reach's has_top_right/has_bottom_left table stride was 64-relative Round-2 of the 854x480 padded key frame desync: a debug aomdec trace of has_top_right/has_bottom_left at the exact first-mismatch block (a 16x16 at mi (40, 88)) found libaom indexing its pinned has_bl_16x16 table at bit 18 (row * 8 + col), while Reach::bottom_left indexed the same table at bit 10 (row * 4 + col) -- different bits, one true one false. libaom's tables are pinned to its compile-time maximum superblock (128px, MAX_MIB_SIZE_LOG2 = 5), not the superblock size a stream actually uses, so the row stride for a 16x16/32x32 block is 128 / side, not SUPERBLOCK / side (this crate's 64px superblock) -- Reach::of's own row/col position within the superblock was already right; only the table's row stride was wrong. Un-ignores open_defect_854x480_padded_key_frame_desyncs, renamed an_854x480_picture_round_trips_through_its_padding, and adds reach_matches_libaom_has_top_right_and_has_bottom_left, a from-scratch transcription of libaom's has_top_right/has_bottom_left checked against Reach for every 16x16/32x32 position across three superblocks square, including every superblock edge -- the class this bug was (an index stride silently wrong for one block size, plausible for the other). --- crates/ec-av1/src/encode.rs | 171 ++++++++++++++++++++++++++---------- 1 file changed, 126 insertions(+), 45 deletions(-) diff --git a/crates/ec-av1/src/encode.rs b/crates/ec-av1/src/encode.rs index e558ff26..1ea54fb3 100644 --- a/crates/ec-av1/src/encode.rs +++ b/crates/ec-av1/src/encode.rs @@ -725,7 +725,8 @@ struct Reach { /// libaom): for a block that is neither in the superblock's top row nor its /// rightmost column, whether the block above and to its right is coded before /// it. Indexed by the block's position in the superblock, in blocks of its own -/// size, as `row * (16 >> log2 mi width) + col`, bit by bit from the low end. +/// size, as `row * (128 / side) + col` (`Reach::table_stride`) -- libaom's +/// 128, not this crate's 64 superblock -- bit by bit from the low end. const HAS_TOP_RIGHT: [&[u8]; 2] = [&[255, 85, 119, 85, 127, 85, 119, 85], &[95, 87]]; /// `has_bl_16x16` / `has_bl_32x32`: the same, for the block below and to the @@ -763,7 +764,10 @@ impl Reach { if col + 1 == per_side { return false; } - Self::bit(HAS_TOP_RIGHT[Self::table(side)], row * per_side + col) + Self::bit( + HAS_TOP_RIGHT[Self::table(side)], + row * Self::table_stride(side) + col, + ) } /// `has_bottom_left` for such a block: the leftmost column of a superblock @@ -778,7 +782,10 @@ impl Reach { if row + 1 == per_side { return false; } - Self::bit(HAS_BOTTOM_LEFT[Self::table(side)], row * per_side + col) + Self::bit( + HAS_BOTTOM_LEFT[Self::table(side)], + row * Self::table_stride(side) + col, + ) } /// Where a block sits inside its superblock, in blocks of its own size, @@ -791,6 +798,18 @@ impl Reach { ) } + /// The row stride `HAS_TOP_RIGHT`/`HAS_BOTTOM_LEFT` index by: libaom pins + /// these tables to its compile-time maximum superblock (128, spec + /// `MAX_MIB_SIZE_LOG2` = 5 in 4-pixel units), not to whichever superblock + /// size an encode actually uses, so a block's row in the bit index steps + /// by 128 / `side` even though this crate only ever codes a 64 + /// superblock -- using `per_side` (`SUPERBLOCK` / `side`, 64-relative) + /// here instead indexes the wrong bit for every block size but the one + /// where 64 and 128 give the same stride. + fn table_stride(side: usize) -> usize { + 128 / side + } + /// Which of the two block sizes the encoder codes a table row is for. fn table(side: usize) -> usize { usize::from(side == BLOCK) @@ -2127,9 +2146,9 @@ mod tests { eprintln!("SKIP a_frame_round_trips_at_its_own_size: no ffmpeg"); return; } - // 854x480 is deliberately not in this list -- it hits an open, - // content-dependent desync unrelated to padding wiring; see - // `open_defect_854x480_padded_key_frame_desyncs`. + // 854x480 is covered separately by + // `an_854x480_picture_round_trips_through_its_padding`, which its own + // doc comment explains was worth keeping as its own test. for &(width, height) in &[(1920usize, 1080usize), (640, 352), (1280, 720)] { let picture = test_card(width, height); let encoded = encode_key_frame(&picture, 100, 0.5).unwrap(); @@ -2158,46 +2177,25 @@ mod tests { } } - /// Reproduces an open defect this lane's sweep found: 854x480 (padded to - /// 864x480, an *edge-replication* padded key frame -- not a preexisting - /// bug, since 864x480 coded directly with no padding involved is - /// bit-exact) diverges from ffmpeg's decode starting at luma (row 161, - /// col 369), well inside the real (non-padded) region, not at the - /// padding edge. - /// - /// NOT an arithmetic-coder desync: an EC_RNG-per-symbol trace against a - /// debug libaom decoder (od_ec_decode_cdf_q15/od_ec_decode_bool_q15 - /// instrumented) showed the encoder's and the decoder's rng_before/ - /// rng_after/nsyms/symbol sequences are byte-for-byte identical across - /// all 51188 symbols of this frame -- the bitstream itself decodes - /// exactly as written. The divergence is a reconstruction/prediction - /// mismatch: our own `Encoded::reconstruction` reports 147 at (161,369) - /// where both a debug `aomdec` decode of our own bitstream and ffmpeg - /// agree on 148, an exact off-by-one that then cascades rightward - /// (diff dump: every mismatch in the frame is `ours == ffmpeg - 1`, - /// starting at (161,369) and spreading through the rest of that 16x16/ - /// 32x32 block and everything predicted from it). That points at - /// `encode.rs`'s `Reach::of`/`Reach::top_right`/`Reach::bottom_left` - /// (the `HAS_TOP_RIGHT`/`HAS_BOTTOM_LEFT` pinned tables used only to - /// decide how far a directional intra predictor may read past a block's - /// own edges) computing a different above-right/below-left availability - /// than the real decoder does for this specific block position, so our - /// own search-time prediction differs from the decoder's by one preset - /// intensity step -- not the partition/column-edge superblock code this - /// lane suspected first (`sb_coeff_key_frame_tile`'s `(has_cols, - /// has_rows)` match at tile.rs:783-795 is exercised correctly by this - /// same test: the rng trace over that exact superblock matches - /// libaom's). 640x352, 1280x720, 854x480's own height (480, already - /// 32-aligned), 1920x1080 and 864x480 coded directly all round-trip - /// clean, so this needs a specific directional-mode block position - /// (content-dependent) to fire. Left `#[ignore]`d -- next lane should - /// start from `Reach::top_right`/`bottom_left` in `encode.rs` against - /// libaom's `has_tr_16x16`/`has_tr_32x32`/`has_bl_*` source tables, - /// checked at the exact position `Reach::position` computes for the - /// block covering (row 160ish, col 368ish) in this test's superblock. + /// 854x480 (padded to 864x480, an edge-replication padded key frame) used + /// to diverge from ffmpeg's decode at luma (row 161, col 369): a debug + /// `aomdec` trace of `has_top_right`/`has_bottom_left` at that exact + /// block (a 16x16 at mi (40, 88)) showed libaom indexing its pinned + /// `has_bl_16x16` table at bit 18 (`row * 8 + col`, row=2 col=2), while + /// `Reach::bottom_left` indexed the same table at bit 10 (`row * 4 + + /// col`) -- different bits of an 8-byte table, one true, one false. The + /// stride libaom indexes by is fixed at its compile-time maximum + /// superblock (128px, `MAX_MIB_SIZE_LOG2` = 5), not the actual superblock + /// size a stream uses, so the right stride for a 16x16 or 32x32 block is + /// `128 / side`, not `SUPERBLOCK / side` (64-relative) -- `Reach::of`'s + /// row/col position within the (correctly 64-relative) superblock stayed + /// right, only the table's own row stride was wrong. Fixed in + /// `Reach::table_stride`; not an arithmetic-coder desync (an + /// EC_RNG-per-symbol trace against the same debug decoder found the + /// bitstream byte-for-byte identical to libaom's across all 51188 + /// symbols of this frame before this bug was found). #[test] - #[ignore = "open defect: content-dependent desync at 854x480 padded, not padding wiring -- see doc comment"] - fn open_defect_854x480_padded_key_frame_desyncs() { + fn an_854x480_picture_round_trips_through_its_padding() { if !have_ffmpeg() { return; } @@ -2213,6 +2211,89 @@ mod tests { assert_eq!(cropped.y, encoded.reconstruction.y, "854x480: luma"); } + /// `Reach::top_right`/`bottom_left` against a from-scratch transcription + /// of libaom's `has_top_right`/`has_bottom_left` (`av1/common/ + /// reconintra.c`), for every 16x16 and 32x32 block position across three + /// superblocks square -- interior and every superblock edge (top row, + /// left column, right column, bottom row) both sizes reach. Written to + /// catch the class the `table_stride` bug was: an index stride silently + /// wrong for one block size while looking plausible for the other. + #[test] + fn reach_matches_libaom_has_top_right_and_has_bottom_left() { + // Transcribed from has_top_right/has_bottom_left's row_off==0, + // col_off==0 (whole-transform) path, with MAX_MIB_SIZE_LOG2 = 5 (a + // 128px reference grid) pinned as libaom pins it, independent of the + // 64px superblock this crate actually codes. + fn libaom_top_right(side: usize, x: usize, y: usize, width: usize, height: usize) -> bool { + if y == 0 || x + side >= width { + return false; + } + let (row, col, per_side) = ( + (y % SUPERBLOCK) / side, + (x % SUPERBLOCK) / side, + SUPERBLOCK / side, + ); + if row == 0 { + return true; + } + if col + 1 == per_side { + return false; + } + let stride = 128 / side; + let index = row * stride + col; + let table = if side == BLOCK { + [95u8, 87].to_vec() + } else { + vec![255, 85, 119, 85, 127, 85, 119, 85] + }; + (table[index / 8] >> (index % 8)) & 1 != 0 + } + + fn libaom_bottom_left(side: usize, x: usize, y: usize, height: usize) -> bool { + if x == 0 || y + side >= height { + return false; + } + let (row, col, per_side) = ( + (y % SUPERBLOCK) / side, + (x % SUPERBLOCK) / side, + SUPERBLOCK / side, + ); + if col == 0 { + return row * side + side < SUPERBLOCK; + } + if row + 1 == per_side { + return false; + } + let stride = 128 / side; + let index = row * stride + col; + let table = if side == BLOCK { + [4u8, 4].to_vec() + } else { + vec![84, 16, 84, 0, 84, 16, 84, 0] + }; + (table[index / 8] >> (index % 8)) & 1 != 0 + } + + let (width, height) = (SUPERBLOCK * 3, SUPERBLOCK * 3); + for side in [16usize, BLOCK] { + for y in (0..height).step_by(side) { + for x in (0..width).step_by(side) { + let reach = Reach::of(side, x, y, width, height); + assert_eq!( + reach.above_right, + libaom_top_right(side, x, y, width, height), + "side={side} x={x} y={y}: above_right" + ); + assert_eq!( + reach.below_left, + libaom_bottom_left(side, x, y, height), + "side={side} x={x} y={y}: below_left" + ); + } + } + } + } + /// An odd width or height is refused by name, not by however the padder /// or the block coder would happen to fail on it. #[test]