forge
Explore
Log in

Don't you dare go Hollow

PARTITION_VERT_A/_B square sub-blocks must read libaom's _vert_ has_tr/has_bl tables

Browse files at this commit

02e331ff70ff7fd1c44cf0873d07af4949f395eaAhmet Kaan GümüşParent: ad9ad14
  • fix
    • PARTITION_VERT_A/_B square sub-blocks must read libaom's _vert_ has_tr/has_bl tables
Show full message
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.rsmodified+26 -5
@@ -764,13 +764,21 @@ pub(crate) fn sb_rect_hits() -> usize {
764764 // `--enable-ab-partitions=0` (lane-sbpart r2's own measurement), so this is
765765 // the counter the gate hard-asserts.
766766 thread_local! {
767- static SB_AB_HITS: std::cell::Cell<usize> =
768- const { std::cell::Cell::new(0) };
767+ static SB_AB_HITS: std::cell::Cell<[usize; 4]> =
768+ const { std::cell::Cell::new([0; 4]) };
769769 }
770770
771-/// Current value of [`SB_AB_HITS`].
771+/// Current value of [`SB_AB_HITS`], summed over the four arms.
772772 pub(crate) fn sb_ab_hits() -> usize {
773- SB_AB_HITS.with(|c| c.get())
773+ SB_AB_HITS.with(|c| c.get().iter().sum())
774+}
775+
776+/// [`SB_AB_HITS`] split per arm, in `PARTITION_HORZ_A`, `_HORZ_B`,
777+/// `_VERT_A`, `_VERT_B` order -- what the gate hard-asserts arm by arm, so a
778+/// run where aomenc's RD only ever picked (say) `HORZ_A` cannot pass off as
779+/// proof of all four.
780+pub(crate) fn sb_ab_hits_by_arm() -> [usize; 4] {
781+ SB_AB_HITS.with(std::cell::Cell::get)
774782 }
775783
776784 // 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(
75327540 // width/height swapped (TL, BL, right 16x32
75337541 // strip).
75347542 INTRA_VERT_A_HITS.with(|c| c.set(c.get() + 1));
7543+ let _vert_ab = crate::encode::Reach::vert_ab_partition();
75357544 decode_block(
75367545 &mut dec,
75377546 &mut cdfs,
@@ -7606,6 +7615,7 @@ pub(crate) fn decode_key_frame_tile_with_cdfs(
76067615 // (libaom decode_partition VERT_B: left
76077616 // strip, TR, BR).
76087617 INTRA_VERT_B_HITS.with(|c| c.set(c.get() + 1));
7618+ let _vert_ab = crate::encode::Reach::vert_ab_partition();
76097619 decode_block_rect(
76107620 &mut dec,
76117621 &mut cdfs,
@@ -7758,7 +7768,16 @@ pub(crate) fn decode_key_frame_tile_with_cdfs(
77587768 // TR, BR). The pieces are exactly the ones already proven
77597769 // by the `PARTITION_NONE`-under-SPLIT (32x32 square) and
77607770 // `PARTITION_HORZ`/`VERT` (rect64 strip) arms above.
7761- SB_AB_HITS.with(|c| c.set(c.get() + 1));
7771+ SB_AB_HITS.with(|c| {
7772+ let mut h = c.get();
7773+ h[part - PARTITION_HORZ_A] += 1;
7774+ c.set(h);
7775+ });
7776+ // lane-part32 r5: only the two VERTICAL arms reorder the
7777+ // square sub-blocks (TL, BL, TR, BR), so only they switch
7778+ // libaom's `has_tr`/`has_bl` tables.
7779+ let _vert_ab = (part == PARTITION_VERT_A || part == PARTITION_VERT_B)
7780+ .then(crate::encode::Reach::vert_ab_partition);
77627781 macro_rules! square32 {
77637782 ($at:expr) => {
77647783 decode_block(
@@ -13427,6 +13446,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs(
1342713446 // lane-partab r1: mirror of PARTITION_HORZ_A with
1342813447 // width/height swapped (TL, BL, right 16x32 strip).
1342913448 PARTAB_HITS.with(|c| c.set(c.get() + 1));
13449+ let _vert_ab = crate::encode::Reach::vert_ab_partition();
1343013450 decode_inter_block(
1343113451 &mut dec,
1343213452 &mut cdfs,
@@ -13588,6 +13608,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs(
1358813608 // 16x16 squares on the right (libaom decode_partition
1358913609 // PARTITION_VERT_B: left strip, TR, BR).
1359013610 PARTAB_HITS.with(|c| c.set(c.get() + 1));
13611+ let _vert_ab = crate::encode::Reach::vert_ab_partition();
1359113612 decode_inter_block(
1359213613 &mut dec,
1359313614 &mut cdfs,
crates/ec-av1/src/encode.rsmodified+72 -2
@@ -1043,6 +1043,59 @@ const HAS_BOTTOM_LEFT: [&[u8]; 3] = [
10431043 ],
10441044 ];
10451045
1046+/// `has_tr_vert_*` (libaom `reconintra.c`, `has_tr_vert_tables`), indexed by
1047+/// [`Reach::table`] exactly like [`HAS_TOP_RIGHT`]: inside a
1048+/// `PARTITION_VERT_A`/`_B` the square sub-blocks are visited TL, BL, TR, BR
1049+/// rather than in raster order, so the bottom-left square's top-right
1050+/// neighbour is the partition's own right-hand rectangle -- not yet decoded.
1051+/// libaom switches tables on the partition type (`get_has_tr_table`); the
1052+/// ordinary table says 1 where this one says 0 (32x32 at row 1 col 0:
1053+/// `95` bit 4 = 1 vs `15` bit 4 = 0), which is exactly the case
1054+/// lane-part32 r5 caught reading undecoded samples.
1055+const HAS_TOP_RIGHT_VERT: [&[u8]; 3] = [
1056+ // has_tr_vert_16x16
1057+ &[255, 0, 119, 0, 127, 0, 119, 0],
1058+ // has_tr_vert_32x32
1059+ &[15, 7],
1060+ // has_tr_vert_8x8
1061+ &[
1062+ 255, 255, 0, 0, 119, 119, 0, 0, 127, 127, 0, 0, 119, 119, 0, 0, 255, 127, 0, 0, 119, 119,
1063+ 0, 0, 127, 127, 0, 0, 119, 119, 0, 0,
1064+ ],
1065+];
1066+
1067+/// [`HAS_TOP_RIGHT_VERT`]'s sibling, `has_bl_vert_tables`.
1068+const HAS_BOTTOM_LEFT_VERT: [&[u8]; 3] = [
1069+ // has_bl_vert_16x16
1070+ &[254, 16, 254, 0, 254, 16, 254, 0],
1071+ // has_bl_vert_32x32
1072+ &[14, 14],
1073+ // has_bl_vert_8x8
1074+ &[
1075+ 254, 255, 16, 17, 254, 255, 0, 1, 254, 255, 16, 17, 254, 255, 0, 0, 254, 255, 16, 17, 254,
1076+ 255, 0, 1, 254, 255, 16, 17, 254, 255, 0, 0,
1077+ ],
1078+];
1079+
1080+thread_local! {
1081+ /// Set for the duration of a `PARTITION_VERT_A`/`_B`'s own sub-blocks
1082+ /// (see [`Reach::vert_ab_partition`]) -- libaom carries the partition
1083+ /// type down into `has_top_right`/`has_bottom_left` as an argument, and
1084+ /// threading one bool through this crate's already twenty-argument
1085+ /// `decode_block` would touch every call site instead of the two arms
1086+ /// that need it.
1087+ static VERT_AB_PARTITION: std::cell::Cell<bool> = const { std::cell::Cell::new(false) };
1088+}
1089+
1090+/// Restores [`VERT_AB_PARTITION`] to what it was when the guard was made.
1091+pub(crate) struct VertAbGuard(bool);
1092+
1093+impl Drop for VertAbGuard {
1094+ fn drop(&mut self) {
1095+ VERT_AB_PARTITION.with(|c| c.set(self.0));
1096+ }
1097+}
1098+
10461099 /// `has_tr_32x16`/`has_tr_16x32` (libaom `reconintra.c`) -- lane-intradisp
10471100 /// r1: the two rect strips PARTITION_HORZ/VERT ever produce at the 32x32
10481101 /// level, transcribed verbatim (not derived): `[0]` is 32-wide, `[1]` is
@@ -1113,6 +1166,15 @@ impl Reach {
11131166 Self::bit(table, (row * Self::table_stride(bw) + col) % (table.len() * 8))
11141167 }
11151168
1169+ /// Marks every [`Reach::of`] made until the returned guard drops as being
1170+ /// inside a `PARTITION_VERT_A`/`_B` (libaom's `get_has_tr_table` /
1171+ /// `get_has_bl_table` partition argument). Rectangular sub-blocks are
1172+ /// unaffected: libaom's own comment says vertical rectangles keep their
1173+ /// non-vert table, which is what [`Reach::of_rect`] already reads.
1174+ pub(crate) fn vert_ab_partition() -> VertAbGuard {
1175+ VertAbGuard(VERT_AB_PARTITION.with(|c| c.replace(true)))
1176+ }
1177+
11161178 /// Neither, which is all a mode that reads no further than its own edges
11171179 /// needs.
11181180 pub(crate) fn none() -> Self {
@@ -1145,7 +1207,11 @@ impl Reach {
11451207 // reference pixels), never a stream desync (Reach never gates a
11461208 // symbol read). Ceiling: transcribe `has_tr_4x4`/`has_bl_4x4`
11471209 // verbatim and give TX4 its own `HAS_TOP_RIGHT`/`HAS_BOTTOM_LEFT` row.
1148- let table = HAS_TOP_RIGHT[Self::table(side)];
1210+ let table = if VERT_AB_PARTITION.with(std::cell::Cell::get) {
1211+ HAS_TOP_RIGHT_VERT[Self::table(side)]
1212+ } else {
1213+ HAS_TOP_RIGHT[Self::table(side)]
1214+ };
11491215 Self::bit(
11501216 table,
11511217 (row * Self::table_stride(side) + col) % (table.len() * 8),
@@ -1165,7 +1231,11 @@ impl Reach {
11651231 return false;
11661232 }
11671233 // corner-cut: see `top_right`'s note -- same clamp, same ceiling.
1168- let table = HAS_BOTTOM_LEFT[Self::table(side)];
1234+ let table = if VERT_AB_PARTITION.with(std::cell::Cell::get) {
1235+ HAS_BOTTOM_LEFT_VERT[Self::table(side)]
1236+ } else {
1237+ HAS_BOTTOM_LEFT[Self::table(side)]
1238+ };
11691239 Self::bit(
11701240 table,
11711241 (row * Self::table_stride(side) + col) % (table.len() * 8),
crates/ec-av1/src/stream.rsmodified+28 -28
@@ -9460,17 +9460,15 @@ mod tests {
94609460 msg.contains("unsupported"),
94619461 "{NAME} failed outright, not a named refusal (seed {seed}): {msg}"
94629462 );
9463- // r2: `--enable-ab-partitions=0` does NOT stop aomenc
9464- // choosing an SB-level HORZ_A/HORZ_B/VERT_A/VERT_B
9465- // (`partition_w64` value 4-7) even with
9466- // `--min/max-partition-size=32/64` -- a real, separate
9467- // encoder quirk (AB-at-64 is a different, unlanded
9468- // capability, lane-partab's territory is 32x32 and
9469- // below only) discovered live this round, not this
9470- // lane's HORZ/VERT gap. Only NONE/SPLIT/HORZ/VERT
9471- // (values 0-3) are this gate's territory; a real
9472- // HORZ/VERT (1/2) must still decode pixel-exact, which
9473- // `sb_rect_hits() > 0` below hard-proves.
9463+ // r5: the four SB-level AB arms (`partition_w64` 4-7)
9464+ // decode since this round, so the only partition refusal
9465+ // left at 64x64 is the 1:4 pair (values 8/9, four
9466+ // 64x16/16x64 strips, whose luma TX_64X16 this decoder
9467+ // has no transform for); the refusals this loop actually
9468+ // counts on these fixtures are the pre-existing
9469+ // screen-content-palette strip one. Every refusal is
9470+ // named, so a genuine decode failure still fails the
9471+ // assert above rather than skipping.
94749472 named_refusals += 1;
94759473 eprintln!("seed {seed} refusal: {msg}");
94769474 continue;
@@ -9621,17 +9619,15 @@ mod tests {
96219619 msg.contains("unsupported"),
96229620 "{NAME} failed outright, not a named refusal (seed {seed}): {msg}"
96239621 );
9624- // r2: `--enable-ab-partitions=0` does NOT stop aomenc
9625- // choosing an SB-level HORZ_A/HORZ_B/VERT_A/VERT_B
9626- // (`partition_w64` value 4-7) even with
9627- // `--min/max-partition-size=32/64` -- a real, separate
9628- // encoder quirk (AB-at-64 is a different, unlanded
9629- // capability, lane-partab's territory is 32x32 and
9630- // below only) discovered live this round, not this
9631- // lane's HORZ/VERT gap. Only NONE/SPLIT/HORZ/VERT
9632- // (values 0-3) are this gate's territory; a real
9633- // HORZ/VERT (1/2) must still decode pixel-exact, which
9634- // `sb_rect_hits() > 0` below hard-proves.
9622+ // r5: the four SB-level AB arms (`partition_w64` 4-7)
9623+ // decode since this round, so the only partition refusal
9624+ // left at 64x64 is the 1:4 pair (values 8/9, four
9625+ // 64x16/16x64 strips, whose luma TX_64X16 this decoder
9626+ // has no transform for); the refusals this loop actually
9627+ // counts on these fixtures are the pre-existing
9628+ // screen-content-palette strip one. Every refusal is
9629+ // named, so a genuine decode failure still fails the
9630+ // assert above rather than skipping.
96359631 named_refusals += 1;
96369632 eprintln!("seed {seed} refusal: {msg}");
96379633 continue;
@@ -9661,14 +9657,18 @@ mod tests {
96619657 matched > 0,
96629658 "{NAME}: every attempt refused; the gate never decoded a stream"
96639659 );
9664- assert!(
9665- crate::decode::sb_ab_hits() > 0,
9666- "{NAME}: zero superblock-level AB blocks fired ({matched} matches, \
9667- {named_refusals} refusals out of {n_attempts}) -- gate proved nothing this run"
9668- );
9660+ let arms = crate::decode::sb_ab_hits_by_arm();
9661+ for (i, name) in ["HORZ_A", "HORZ_B", "VERT_A", "VERT_B"].iter().enumerate() {
9662+ assert!(
9663+ arms[i] > 0,
9664+ "{NAME}: zero superblock-level PARTITION_{name} blocks fired (arms={arms:?}, \
9665+ {matched} matches, {named_refusals} refusals out of {n_attempts}) -- that arm \
9666+ proved nothing this run"
9667+ );
9668+ }
96699669 eprintln!(
96709670 "{NAME}: {named_refusals} named refusals, {matched} pixel-exact matches out of \
9671- {n_attempts}, sb_ab_hits={}",
9671+ {n_attempts}, sb_ab_hits={} arms(HORZ_A,HORZ_B,VERT_A,VERT_B)={arms:?}",
96729672 crate::decode::sb_ab_hits()
96739673 );
96749674 }