From 02e331ff70ff7fd1c44cf0873d07af4949f395ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Tue, 1 Sep 2026 22:06:15 +0300 Subject: [PATCH] fix(av1): PARTITION_VERT_A/_B square sub-blocks must read libaom's _vert_ has_tr/has_bl tables lane-part32 r5. A VERT_A/VERT_B partition visits its square sub-blocks TL, BL, TR, BR -- not raster order -- so the bottom-left square's top-right neighbour is the partition's own right-hand rectangle, which is not decoded yet. libaom switches has_top_right/has_bottom_left onto has_tr_vert_*/has_bl_vert_* for exactly these two partition types (reconintra.c get_has_tr_table); this crate's `Reach` had only the raster tables, so the BL 32x32 of an SB-level VERT_A predicted from never-written samples (has_tr_32x32[0] bit 4 = 1 vs has_tr_vert_32x32[0] bit 4 = 0). Symptom on the pinned seed-63 stream: every symbol in sync with the oracle (aomdec EC_TRACE/EC_TRACE_MODE agree on partition=6 and on every mode, and the msac range at mi_col=40 matches ours exactly, 53240) yet the prefilter reconstruction diverges from x=157 of the BL square, and the 32x64 strip to its right then DC-predicts off that corrupted left column (107 instead of ~149) and the next superblock row's V_PRED copies it down -- 4683 luma pixels wrong from one availability bit. Also lifts the r4 refusal narrowing into a proven capability: the four SB-level AB arms now decode pixel-exact, hard-asserted arm by arm (sb_ab_hits_by_arm), and PARTITION_HORZ_4/VERT_4 at 64x64 remain the only refused 64x64 partition types. --- crates/ec-av1/src/decode.rs | 31 +++++++++++++--- crates/ec-av1/src/encode.rs | 74 ++++++++++++++++++++++++++++++++++++- crates/ec-av1/src/stream.rs | 56 ++++++++++++++-------------- 3 files changed, 126 insertions(+), 35 deletions(-) diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index ca717a46..985cbb54 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -764,13 +764,21 @@ pub(crate) fn sb_rect_hits() -> usize { // `--enable-ab-partitions=0` (lane-sbpart r2's own measurement), so this is // the counter the gate hard-asserts. thread_local! { - static SB_AB_HITS: std::cell::Cell = - const { std::cell::Cell::new(0) }; + static SB_AB_HITS: std::cell::Cell<[usize; 4]> = + const { std::cell::Cell::new([0; 4]) }; } -/// Current value of [`SB_AB_HITS`]. +/// Current value of [`SB_AB_HITS`], summed over the four arms. pub(crate) fn sb_ab_hits() -> usize { - SB_AB_HITS.with(|c| c.get()) + SB_AB_HITS.with(|c| c.get().iter().sum()) +} + +/// [`SB_AB_HITS`] split per arm, in `PARTITION_HORZ_A`, `_HORZ_B`, +/// `_VERT_A`, `_VERT_B` order -- what the gate hard-asserts arm by arm, so a +/// run where aomenc's RD only ever picked (say) `HORZ_A` cannot pass off as +/// proof of all four. +pub(crate) fn sb_ab_hits_by_arm() -> [usize; 4] { + SB_AB_HITS.with(std::cell::Cell::get) } // lane-rect64q r1: how many of [`decode_block_rect64`]'s three per-plane @@ -7532,6 +7540,7 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( // width/height swapped (TL, BL, right 16x32 // strip). INTRA_VERT_A_HITS.with(|c| c.set(c.get() + 1)); + let _vert_ab = crate::encode::Reach::vert_ab_partition(); decode_block( &mut dec, &mut cdfs, @@ -7606,6 +7615,7 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( // (libaom decode_partition VERT_B: left // strip, TR, BR). INTRA_VERT_B_HITS.with(|c| c.set(c.get() + 1)); + let _vert_ab = crate::encode::Reach::vert_ab_partition(); decode_block_rect( &mut dec, &mut cdfs, @@ -7758,7 +7768,16 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( // TR, BR). The pieces are exactly the ones already proven // by the `PARTITION_NONE`-under-SPLIT (32x32 square) and // `PARTITION_HORZ`/`VERT` (rect64 strip) arms above. - SB_AB_HITS.with(|c| c.set(c.get() + 1)); + SB_AB_HITS.with(|c| { + let mut h = c.get(); + h[part - PARTITION_HORZ_A] += 1; + c.set(h); + }); + // lane-part32 r5: only the two VERTICAL arms reorder the + // square sub-blocks (TL, BL, TR, BR), so only they switch + // libaom's `has_tr`/`has_bl` tables. + let _vert_ab = (part == PARTITION_VERT_A || part == PARTITION_VERT_B) + .then(crate::encode::Reach::vert_ab_partition); macro_rules! square32 { ($at:expr) => { decode_block( @@ -13427,6 +13446,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( // lane-partab r1: mirror of PARTITION_HORZ_A with // width/height swapped (TL, BL, right 16x32 strip). PARTAB_HITS.with(|c| c.set(c.get() + 1)); + let _vert_ab = crate::encode::Reach::vert_ab_partition(); decode_inter_block( &mut dec, &mut cdfs, @@ -13588,6 +13608,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( // 16x16 squares on the right (libaom decode_partition // PARTITION_VERT_B: left strip, TR, BR). PARTAB_HITS.with(|c| c.set(c.get() + 1)); + let _vert_ab = crate::encode::Reach::vert_ab_partition(); decode_inter_block( &mut dec, &mut cdfs, diff --git a/crates/ec-av1/src/encode.rs b/crates/ec-av1/src/encode.rs index b84fdf92..140e528b 100644 --- a/crates/ec-av1/src/encode.rs +++ b/crates/ec-av1/src/encode.rs @@ -1043,6 +1043,59 @@ const HAS_BOTTOM_LEFT: [&[u8]; 3] = [ ], ]; +/// `has_tr_vert_*` (libaom `reconintra.c`, `has_tr_vert_tables`), indexed by +/// [`Reach::table`] exactly like [`HAS_TOP_RIGHT`]: inside a +/// `PARTITION_VERT_A`/`_B` the square sub-blocks are visited TL, BL, TR, BR +/// rather than in raster order, so the bottom-left square's top-right +/// neighbour is the partition's own right-hand rectangle -- not yet decoded. +/// libaom switches tables on the partition type (`get_has_tr_table`); the +/// ordinary table says 1 where this one says 0 (32x32 at row 1 col 0: +/// `95` bit 4 = 1 vs `15` bit 4 = 0), which is exactly the case +/// lane-part32 r5 caught reading undecoded samples. +const HAS_TOP_RIGHT_VERT: [&[u8]; 3] = [ + // has_tr_vert_16x16 + &[255, 0, 119, 0, 127, 0, 119, 0], + // has_tr_vert_32x32 + &[15, 7], + // has_tr_vert_8x8 + &[ + 255, 255, 0, 0, 119, 119, 0, 0, 127, 127, 0, 0, 119, 119, 0, 0, 255, 127, 0, 0, 119, 119, + 0, 0, 127, 127, 0, 0, 119, 119, 0, 0, + ], +]; + +/// [`HAS_TOP_RIGHT_VERT`]'s sibling, `has_bl_vert_tables`. +const HAS_BOTTOM_LEFT_VERT: [&[u8]; 3] = [ + // has_bl_vert_16x16 + &[254, 16, 254, 0, 254, 16, 254, 0], + // has_bl_vert_32x32 + &[14, 14], + // has_bl_vert_8x8 + &[ + 254, 255, 16, 17, 254, 255, 0, 1, 254, 255, 16, 17, 254, 255, 0, 0, 254, 255, 16, 17, 254, + 255, 0, 1, 254, 255, 16, 17, 254, 255, 0, 0, + ], +]; + +thread_local! { + /// Set for the duration of a `PARTITION_VERT_A`/`_B`'s own sub-blocks + /// (see [`Reach::vert_ab_partition`]) -- libaom carries the partition + /// type down into `has_top_right`/`has_bottom_left` as an argument, and + /// threading one bool through this crate's already twenty-argument + /// `decode_block` would touch every call site instead of the two arms + /// that need it. + static VERT_AB_PARTITION: std::cell::Cell = const { std::cell::Cell::new(false) }; +} + +/// Restores [`VERT_AB_PARTITION`] to what it was when the guard was made. +pub(crate) struct VertAbGuard(bool); + +impl Drop for VertAbGuard { + fn drop(&mut self) { + VERT_AB_PARTITION.with(|c| c.set(self.0)); + } +} + /// `has_tr_32x16`/`has_tr_16x32` (libaom `reconintra.c`) -- lane-intradisp /// r1: the two rect strips PARTITION_HORZ/VERT ever produce at the 32x32 /// level, transcribed verbatim (not derived): `[0]` is 32-wide, `[1]` is @@ -1113,6 +1166,15 @@ impl Reach { Self::bit(table, (row * Self::table_stride(bw) + col) % (table.len() * 8)) } + /// Marks every [`Reach::of`] made until the returned guard drops as being + /// inside a `PARTITION_VERT_A`/`_B` (libaom's `get_has_tr_table` / + /// `get_has_bl_table` partition argument). Rectangular sub-blocks are + /// unaffected: libaom's own comment says vertical rectangles keep their + /// non-vert table, which is what [`Reach::of_rect`] already reads. + pub(crate) fn vert_ab_partition() -> VertAbGuard { + VertAbGuard(VERT_AB_PARTITION.with(|c| c.replace(true))) + } + /// Neither, which is all a mode that reads no further than its own edges /// needs. pub(crate) fn none() -> Self { @@ -1145,7 +1207,11 @@ impl Reach { // reference pixels), never a stream desync (Reach never gates a // symbol read). Ceiling: transcribe `has_tr_4x4`/`has_bl_4x4` // verbatim and give TX4 its own `HAS_TOP_RIGHT`/`HAS_BOTTOM_LEFT` row. - let table = HAS_TOP_RIGHT[Self::table(side)]; + let table = if VERT_AB_PARTITION.with(std::cell::Cell::get) { + HAS_TOP_RIGHT_VERT[Self::table(side)] + } else { + HAS_TOP_RIGHT[Self::table(side)] + }; Self::bit( table, (row * Self::table_stride(side) + col) % (table.len() * 8), @@ -1165,7 +1231,11 @@ impl Reach { return false; } // corner-cut: see `top_right`'s note -- same clamp, same ceiling. - let table = HAS_BOTTOM_LEFT[Self::table(side)]; + let table = if VERT_AB_PARTITION.with(std::cell::Cell::get) { + HAS_BOTTOM_LEFT_VERT[Self::table(side)] + } else { + HAS_BOTTOM_LEFT[Self::table(side)] + }; Self::bit( table, (row * Self::table_stride(side) + col) % (table.len() * 8), diff --git a/crates/ec-av1/src/stream.rs b/crates/ec-av1/src/stream.rs index 0f3aee67..29cfe8a8 100644 --- a/crates/ec-av1/src/stream.rs +++ b/crates/ec-av1/src/stream.rs @@ -9460,17 +9460,15 @@ mod tests { msg.contains("unsupported"), "{NAME} failed outright, not a named refusal (seed {seed}): {msg}" ); - // r2: `--enable-ab-partitions=0` does NOT stop aomenc - // choosing an SB-level HORZ_A/HORZ_B/VERT_A/VERT_B - // (`partition_w64` value 4-7) even with - // `--min/max-partition-size=32/64` -- a real, separate - // encoder quirk (AB-at-64 is a different, unlanded - // capability, lane-partab's territory is 32x32 and - // below only) discovered live this round, not this - // lane's HORZ/VERT gap. Only NONE/SPLIT/HORZ/VERT - // (values 0-3) are this gate's territory; a real - // HORZ/VERT (1/2) must still decode pixel-exact, which - // `sb_rect_hits() > 0` below hard-proves. + // r5: the four SB-level AB arms (`partition_w64` 4-7) + // decode since this round, so the only partition refusal + // left at 64x64 is the 1:4 pair (values 8/9, four + // 64x16/16x64 strips, whose luma TX_64X16 this decoder + // has no transform for); the refusals this loop actually + // counts on these fixtures are the pre-existing + // screen-content-palette strip one. Every refusal is + // named, so a genuine decode failure still fails the + // assert above rather than skipping. named_refusals += 1; eprintln!("seed {seed} refusal: {msg}"); continue; @@ -9621,17 +9619,15 @@ mod tests { msg.contains("unsupported"), "{NAME} failed outright, not a named refusal (seed {seed}): {msg}" ); - // r2: `--enable-ab-partitions=0` does NOT stop aomenc - // choosing an SB-level HORZ_A/HORZ_B/VERT_A/VERT_B - // (`partition_w64` value 4-7) even with - // `--min/max-partition-size=32/64` -- a real, separate - // encoder quirk (AB-at-64 is a different, unlanded - // capability, lane-partab's territory is 32x32 and - // below only) discovered live this round, not this - // lane's HORZ/VERT gap. Only NONE/SPLIT/HORZ/VERT - // (values 0-3) are this gate's territory; a real - // HORZ/VERT (1/2) must still decode pixel-exact, which - // `sb_rect_hits() > 0` below hard-proves. + // r5: the four SB-level AB arms (`partition_w64` 4-7) + // decode since this round, so the only partition refusal + // left at 64x64 is the 1:4 pair (values 8/9, four + // 64x16/16x64 strips, whose luma TX_64X16 this decoder + // has no transform for); the refusals this loop actually + // counts on these fixtures are the pre-existing + // screen-content-palette strip one. Every refusal is + // named, so a genuine decode failure still fails the + // assert above rather than skipping. named_refusals += 1; eprintln!("seed {seed} refusal: {msg}"); continue; @@ -9661,14 +9657,18 @@ mod tests { matched > 0, "{NAME}: every attempt refused; the gate never decoded a stream" ); - assert!( - crate::decode::sb_ab_hits() > 0, - "{NAME}: zero superblock-level AB blocks fired ({matched} matches, \ - {named_refusals} refusals out of {n_attempts}) -- gate proved nothing this run" - ); + let arms = crate::decode::sb_ab_hits_by_arm(); + for (i, name) in ["HORZ_A", "HORZ_B", "VERT_A", "VERT_B"].iter().enumerate() { + assert!( + arms[i] > 0, + "{NAME}: zero superblock-level PARTITION_{name} blocks fired (arms={arms:?}, \ + {matched} matches, {named_refusals} refusals out of {n_attempts}) -- that arm \ + proved nothing this run" + ); + } eprintln!( "{NAME}: {named_refusals} named refusals, {matched} pixel-exact matches out of \ - {n_attempts}, sb_ab_hits={}", + {n_attempts}, sb_ab_hits={} arms(HORZ_A,HORZ_B,VERT_A,VERT_B)={arms:?}", crate::decode::sb_ab_hits() ); }