forge
Explore
Log in

Don't you dare go Hollow

Reach's has_top_right/has_bottom_left table stride was 64-relative

Browse files at this commit

d1d8e104445dabbab03182d33b130bfec7bbdb27Ahmet Kaan GümüşParent: f1da657
  • fix
    • Reach's has_top_right/has_bottom_left table stride was 64-relative
Show full message
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.rsmodified+126 -45
@@ -725,7 +725,8 @@ struct Reach {
725725 /// libaom): for a block that is neither in the superblock's top row nor its
726726 /// rightmost column, whether the block above and to its right is coded before
727727 /// it. Indexed by the block's position in the superblock, in blocks of its own
728-/// size, as `row * (16 >> log2 mi width) + col`, bit by bit from the low end.
728+/// size, as `row * (128 / side) + col` (`Reach::table_stride`) -- libaom's
729+/// 128, not this crate's 64 superblock -- bit by bit from the low end.
729730 const HAS_TOP_RIGHT: [&[u8]; 2] = [&[255, 85, 119, 85, 127, 85, 119, 85], &[95, 87]];
730731
731732 /// `has_bl_16x16` / `has_bl_32x32`: the same, for the block below and to the
@@ -763,7 +764,10 @@ impl Reach {
763764 if col + 1 == per_side {
764765 return false;
765766 }
766- Self::bit(HAS_TOP_RIGHT[Self::table(side)], row * per_side + col)
767+ Self::bit(
768+ HAS_TOP_RIGHT[Self::table(side)],
769+ row * Self::table_stride(side) + col,
770+ )
767771 }
768772
769773 /// `has_bottom_left` for such a block: the leftmost column of a superblock
@@ -778,7 +782,10 @@ impl Reach {
778782 if row + 1 == per_side {
779783 return false;
780784 }
781- Self::bit(HAS_BOTTOM_LEFT[Self::table(side)], row * per_side + col)
785+ Self::bit(
786+ HAS_BOTTOM_LEFT[Self::table(side)],
787+ row * Self::table_stride(side) + col,
788+ )
782789 }
783790
784791 /// Where a block sits inside its superblock, in blocks of its own size,
@@ -791,6 +798,18 @@ impl Reach {
791798 )
792799 }
793800
801+ /// The row stride `HAS_TOP_RIGHT`/`HAS_BOTTOM_LEFT` index by: libaom pins
802+ /// these tables to its compile-time maximum superblock (128, spec
803+ /// `MAX_MIB_SIZE_LOG2` = 5 in 4-pixel units), not to whichever superblock
804+ /// size an encode actually uses, so a block's row in the bit index steps
805+ /// by 128 / `side` even though this crate only ever codes a 64
806+ /// superblock -- using `per_side` (`SUPERBLOCK` / `side`, 64-relative)
807+ /// here instead indexes the wrong bit for every block size but the one
808+ /// where 64 and 128 give the same stride.
809+ fn table_stride(side: usize) -> usize {
810+ 128 / side
811+ }
812+
794813 /// Which of the two block sizes the encoder codes a table row is for.
795814 fn table(side: usize) -> usize {
796815 usize::from(side == BLOCK)
@@ -2127,9 +2146,9 @@ mod tests {
21272146 eprintln!("SKIP a_frame_round_trips_at_its_own_size: no ffmpeg");
21282147 return;
21292148 }
2130- // 854x480 is deliberately not in this list -- it hits an open,
2131- // content-dependent desync unrelated to padding wiring; see
2132- // `open_defect_854x480_padded_key_frame_desyncs`.
2149+ // 854x480 is covered separately by
2150+ // `an_854x480_picture_round_trips_through_its_padding`, which its own
2151+ // doc comment explains was worth keeping as its own test.
21332152 for &(width, height) in &[(1920usize, 1080usize), (640, 352), (1280, 720)] {
21342153 let picture = test_card(width, height);
21352154 let encoded = encode_key_frame(&picture, 100, 0.5).unwrap();
@@ -2158,46 +2177,25 @@ mod tests {
21582177 }
21592178 }
21602179
2161- /// Reproduces an open defect this lane's sweep found: 854x480 (padded to
2162- /// 864x480, an *edge-replication* padded key frame -- not a preexisting
2163- /// bug, since 864x480 coded directly with no padding involved is
2164- /// bit-exact) diverges from ffmpeg's decode starting at luma (row 161,
2165- /// col 369), well inside the real (non-padded) region, not at the
2166- /// padding edge.
2167- ///
2168- /// NOT an arithmetic-coder desync: an EC_RNG-per-symbol trace against a
2169- /// debug libaom decoder (od_ec_decode_cdf_q15/od_ec_decode_bool_q15
2170- /// instrumented) showed the encoder's and the decoder's rng_before/
2171- /// rng_after/nsyms/symbol sequences are byte-for-byte identical across
2172- /// all 51188 symbols of this frame -- the bitstream itself decodes
2173- /// exactly as written. The divergence is a reconstruction/prediction
2174- /// mismatch: our own `Encoded::reconstruction` reports 147 at (161,369)
2175- /// where both a debug `aomdec` decode of our own bitstream and ffmpeg
2176- /// agree on 148, an exact off-by-one that then cascades rightward
2177- /// (diff dump: every mismatch in the frame is `ours == ffmpeg - 1`,
2178- /// starting at (161,369) and spreading through the rest of that 16x16/
2179- /// 32x32 block and everything predicted from it). That points at
2180- /// `encode.rs`'s `Reach::of`/`Reach::top_right`/`Reach::bottom_left`
2181- /// (the `HAS_TOP_RIGHT`/`HAS_BOTTOM_LEFT` pinned tables used only to
2182- /// decide how far a directional intra predictor may read past a block's
2183- /// own edges) computing a different above-right/below-left availability
2184- /// than the real decoder does for this specific block position, so our
2185- /// own search-time prediction differs from the decoder's by one preset
2186- /// intensity step -- not the partition/column-edge superblock code this
2187- /// lane suspected first (`sb_coeff_key_frame_tile`'s `(has_cols,
2188- /// has_rows)` match at tile.rs:783-795 is exercised correctly by this
2189- /// same test: the rng trace over that exact superblock matches
2190- /// libaom's). 640x352, 1280x720, 854x480's own height (480, already
2191- /// 32-aligned), 1920x1080 and 864x480 coded directly all round-trip
2192- /// clean, so this needs a specific directional-mode block position
2193- /// (content-dependent) to fire. Left `#[ignore]`d -- next lane should
2194- /// start from `Reach::top_right`/`bottom_left` in `encode.rs` against
2195- /// libaom's `has_tr_16x16`/`has_tr_32x32`/`has_bl_*` source tables,
2196- /// checked at the exact position `Reach::position` computes for the
2197- /// block covering (row 160ish, col 368ish) in this test's superblock.
2180+ /// 854x480 (padded to 864x480, an edge-replication padded key frame) used
2181+ /// to diverge from ffmpeg's decode at luma (row 161, col 369): a debug
2182+ /// `aomdec` trace of `has_top_right`/`has_bottom_left` at that exact
2183+ /// block (a 16x16 at mi (40, 88)) showed libaom indexing its pinned
2184+ /// `has_bl_16x16` table at bit 18 (`row * 8 + col`, row=2 col=2), while
2185+ /// `Reach::bottom_left` indexed the same table at bit 10 (`row * 4 +
2186+ /// col`) -- different bits of an 8-byte table, one true, one false. The
2187+ /// stride libaom indexes by is fixed at its compile-time maximum
2188+ /// superblock (128px, `MAX_MIB_SIZE_LOG2` = 5), not the actual superblock
2189+ /// size a stream uses, so the right stride for a 16x16 or 32x32 block is
2190+ /// `128 / side`, not `SUPERBLOCK / side` (64-relative) -- `Reach::of`'s
2191+ /// row/col position within the (correctly 64-relative) superblock stayed
2192+ /// right, only the table's own row stride was wrong. Fixed in
2193+ /// `Reach::table_stride`; not an arithmetic-coder desync (an
2194+ /// EC_RNG-per-symbol trace against the same debug decoder found the
2195+ /// bitstream byte-for-byte identical to libaom's across all 51188
2196+ /// symbols of this frame before this bug was found).
21982197 #[test]
2199- #[ignore = "open defect: content-dependent desync at 854x480 padded, not padding wiring -- see doc comment"]
2200- fn open_defect_854x480_padded_key_frame_desyncs() {
2198+ fn an_854x480_picture_round_trips_through_its_padding() {
22012199 if !have_ffmpeg() {
22022200 return;
22032201 }
@@ -2213,6 +2211,89 @@ mod tests {
22132211 assert_eq!(cropped.y, encoded.reconstruction.y, "854x480: luma");
22142212 }
22152213
2214+ /// `Reach::top_right`/`bottom_left` against a from-scratch transcription
2215+ /// of libaom's `has_top_right`/`has_bottom_left` (`av1/common/
2216+ /// reconintra.c`), for every 16x16 and 32x32 block position across three
2217+ /// superblocks square -- interior and every superblock edge (top row,
2218+ /// left column, right column, bottom row) both sizes reach. Written to
2219+ /// catch the class the `table_stride` bug was: an index stride silently
2220+ /// wrong for one block size while looking plausible for the other.
2221+ #[test]
2222+ fn reach_matches_libaom_has_top_right_and_has_bottom_left() {
2223+ // Transcribed from has_top_right/has_bottom_left's row_off==0,
2224+ // col_off==0 (whole-transform) path, with MAX_MIB_SIZE_LOG2 = 5 (a
2225+ // 128px reference grid) pinned as libaom pins it, independent of the
2226+ // 64px superblock this crate actually codes.
2227+ fn libaom_top_right(side: usize, x: usize, y: usize, width: usize, height: usize) -> bool {
2228+ if y == 0 || x + side >= width {
2229+ return false;
2230+ }
2231+ let (row, col, per_side) = (
2232+ (y % SUPERBLOCK) / side,
2233+ (x % SUPERBLOCK) / side,
2234+ SUPERBLOCK / side,
2235+ );
2236+ if row == 0 {
2237+ return true;
2238+ }
2239+ if col + 1 == per_side {
2240+ return false;
2241+ }
2242+ let stride = 128 / side;
2243+ let index = row * stride + col;
2244+ let table = if side == BLOCK {
2245+ [95u8, 87].to_vec()
2246+ } else {
2247+ vec![255, 85, 119, 85, 127, 85, 119, 85]
2248+ };
2249+ (table[index / 8] >> (index % 8)) & 1 != 0
2250+ }
2251+
2252+ fn libaom_bottom_left(side: usize, x: usize, y: usize, height: usize) -> bool {
2253+ if x == 0 || y + side >= height {
2254+ return false;
2255+ }
2256+ let (row, col, per_side) = (
2257+ (y % SUPERBLOCK) / side,
2258+ (x % SUPERBLOCK) / side,
2259+ SUPERBLOCK / side,
2260+ );
2261+ if col == 0 {
2262+ return row * side + side < SUPERBLOCK;
2263+ }
2264+ if row + 1 == per_side {
2265+ return false;
2266+ }
2267+ let stride = 128 / side;
2268+ let index = row * stride + col;
2269+ let table = if side == BLOCK {
2270+ [4u8, 4].to_vec()
2271+ } else {
2272+ vec![84, 16, 84, 0, 84, 16, 84, 0]
2273+ };
2274+ (table[index / 8] >> (index % 8)) & 1 != 0
2275+ }
2276+
2277+ let (width, height) = (SUPERBLOCK * 3, SUPERBLOCK * 3);
2278+ for side in [16usize, BLOCK] {
2279+ for y in (0..height).step_by(side) {
2280+ for x in (0..width).step_by(side) {
2281+ let reach = Reach::of(side, x, y, width, height);
2282+ assert_eq!(
2283+ reach.above_right,
2284+ libaom_top_right(side, x, y, width, height),
2285+ "side={side} x={x} y={y}: above_right"
2286+ );
2287+ assert_eq!(
2288+ reach.below_left,
2289+ libaom_bottom_left(side, x, y, height),
2290+ "side={side} x={x} y={y}: below_left"
2291+ );
2292+ }
2293+ }
2294+ }
2295+ }
2296+
22162297 /// An odd width or height is refused by name, not by however the padder
22172298 /// or the block coder would happen to fail on it.
22182299 #[test]