forge
Explore
Log in

Don't you dare go Hollow

The two rect nz_map ctx tables were transposed, and the 512-chroma eob CDF was the flat 1D row

Browse files at this commit

6906e60853a43f4b152a9b092138befe84f46c4aAhmet Kaan GümüşParent: 85bf4d6
  • fix
    • the two rect nz_map ctx tables were transposed, and the 512-chroma eob CDF was the flat 1D row
Show full message
fix(av1): the two rect nz_map ctx tables were transposed, and the 512-chroma eob CDF was the flat 1D row

The merged superblock HORZ/VERT gate's seed 43 decoded garbage from the
first 64x32 strip onward; base fad4d5a "passed" it only because the
desynced stream then produced a 16x16 partition symbol the AB refusal
caught (the gate's own recipe spells --enable-ab-partitions=0, so aomenc
never wrote one). Two transcription defects, found by comparing our
EC_TRACE_COEFF against the instrumented aomdec's msac range element by
element:

- NZ_MAP_CTX_OFFSET_32X64 / _64X32 were stored [col][row] and read
  [row][col]. libaom indexes av1_nz_map_ctx_offset by
  coeff_idx = (col << bhl) | row -- column-major, bhl = log2 of the
  transform's HEIGHT -- so a straight transcription is transposed. It is
  invisible on the square table (symmetric) and at row, col < 2 (both
  halves agree), which is every position lane-sbpart r8's gate reached.
- EOB_PT_512_CHROMA carried av1_default_eob_multi512_cdfs' SECOND inner
  index, which is eob_multi_ctx (the flat 1D-class row, flat for both
  planes), not the plane. The chroma 2D rows vary per q-context exactly
  like luma's; all four are spelled out and picked by q_ctx now.

Two unit tests pin both: the offset tables are re-derived from libaom's
own generating rule and cross-checked against base_ctx_rect, which
spells that rule out instead of reading a table (those two disagreed
silently before), and the chroma eob constant is pinned unequal to the
flat row.

Seed 43 now decodes bit-exact vs aomdec (36864/36864 bytes; 20494 luma
bytes differed before). cargo test -p ec-av1 --lib: 274 passed, 0 failed.
crates/ec-av1/src/cdf.rsmodified+40 -15
@@ -309,11 +309,28 @@ pub const EOB_PT_32_CHROMA_Q3: [u16; 7] = [22086, 24282, 27010, 29770, 31743, 32
309309 /// coefficients truncate to the 64x64-superblock's own 32x32 corner-scan
310310 /// (`TxbSet::Luma64`) but whose *chroma* plane, at 32x16, is small enough
311311 /// that no truncation applies). Unlike every other `EOB_PT_*` table here,
312-/// libaom's default is the exact same flat distribution at all four
313-/// q-contexts and both `eob_multi_size` selectors (`token_cdfs.h` rows), so
314-/// one constant covers every q-context -- no `_Q0`/`_Q1`/`_Q3` siblings.
312+/// `av1_default_eob_multi512_cdfs` is
313+/// `[q][plane][eob_multi_ctx][10]`: the flat 3277/6554/... row this constant
314+/// used to carry is the SECOND inner index (`eob_multi_ctx == 1`, the 1D
315+/// `TX_CLASS_HORIZ`/`VERT` row that is flat for BOTH planes), not the chroma
316+/// plane -- reading it here made every 32x16/16x32 chroma transform decode a
317+/// wrong end-of-block group and desync the tile (lane-ab16 r2, the second
318+/// defect under the merged SB gate's seed 43). Chroma's real 2D rows vary
319+/// per q-context exactly like luma's, so all four are spelled out.
315320 pub const EOB_PT_512_CHROMA: [u16; 11] = [
316- 3277, 6554, 9830, 13107, 16384, 19661, 22938, 26214, 29491, 32768, 0,
321+ 12015, 14769, 19588, 22052, 24222, 25812, 27300, 29219, 32114, 32768, 0,
322+];
323+/// [`EOB_PT_512_CHROMA`], q-context 0 (`base_q_idx` 0..=20).
324+pub const EOB_PT_512_CHROMA_Q0: [u16; 11] = [
325+ 5095, 6446, 9996, 13354, 16017, 17986, 20919, 26129, 29140, 32768, 0,
326+];
327+/// [`EOB_PT_512_CHROMA`], q-context 1 (`base_q_idx` 21..=60).
328+pub const EOB_PT_512_CHROMA_Q1: [u16; 11] = [
329+ 7265, 9979, 15819, 19250, 21780, 23846, 26478, 28396, 31811, 32768, 0,
330+];
331+/// [`EOB_PT_512_CHROMA`], q-context 3 (`base_q_idx` 121..=255).
332+pub const EOB_PT_512_CHROMA_Q3: [u16; 11] = [
333+ 21093, 23043, 25742, 27658, 29097, 29716, 30073, 30820, 31956, 32768, 0,
317334 ];
318335
319336 /// `Default_Dc_Sign_Cdf[2][0]` (spec 9.4): the sign of a luma DC coefficient,
@@ -813,23 +830,31 @@ pub const NZ_MAP_CTX_OFFSET_32: [[u8; 5]; 5] = [
813830 /// resolves the CDF *set* to `TX_64X64`, but `get_nz_map_ctx_from_stats` still
814831 /// indexes this position table by the raw, un-adjusted `tx_size` -- lane-sbpart
815832 /// r8 root cause) is genuinely rectangular, not square. Distinct from
816-/// [`NZ_MAP_CTX_OFFSET_32`] starting at `(row=1, col=0)`.
833+/// [`NZ_MAP_CTX_OFFSET_32`] starting at `(row=2, col=0)`.
834+///
835+/// `[row][col]`, which is libaom's own array TRANSPOSED: its `coeff_idx` is
836+/// `(col << bhl) | row` (`txb_common.h`, `bhl` = log2 of the transform's
837+/// HEIGHT), so `av1_nz_map_ctx_offset_32x64[i]` runs down a column, not
838+/// across a row. Both rect tables were transcribed in that column-major
839+/// order and then read `[row][col]` -- invisible on the square tables (they
840+/// are symmetric) and at `row, col < 2` (both halves agree there), which is
841+/// every position the r8 gate reached (lane-ab16 r2).
817842 pub const NZ_MAP_CTX_OFFSET_32X64: [[u8; 5]; 5] = [
818- [0, 11, 6, 6, 21],
819- [11, 11, 6, 21, 21],
820- [11, 11, 21, 21, 21],
821- [11, 11, 21, 21, 21],
822- [11, 11, 21, 21, 21],
843+ [0, 11, 11, 11, 11],
844+ [11, 11, 11, 11, 11],
845+ [6, 6, 21, 21, 21],
846+ [6, 21, 21, 21, 21],
847+ [21, 21, 21, 21, 21],
823848 ];
824849
825850 /// [`NZ_MAP_CTX_OFFSET_32X64`]'s `TX_64X32` counterpart (libaom
826851 /// `av1_nz_map_ctx_offset_64x32`): a `PARTITION_HORZ` strip's luma corner.
827852 pub const NZ_MAP_CTX_OFFSET_64X32: [[u8; 5]; 5] = [
828- [0, 16, 16, 16, 16],
829- [16, 16, 16, 16, 16],
830- [6, 6, 21, 21, 21],
831- [6, 21, 21, 21, 21],
832- [21, 21, 21, 21, 21],
853+ [0, 16, 6, 6, 21],
854+ [16, 16, 6, 21, 21],
855+ [16, 16, 21, 21, 21],
856+ [16, 16, 21, 21, 21],
857+ [16, 16, 21, 21, 21],
833858 ];
834859
835860 /// `Default_Txb_Skip_Cdf[2][2][7..10]` (spec 9.4): the all-zero flag of a
crates/ec-av1/src/cdf_state.rsmodified+7 -3
@@ -1068,9 +1068,13 @@ impl Cdfs {
10681068 cdf::EOB_PT_32_CHROMA,
10691069 cdf::EOB_PT_32_CHROMA_Q3,
10701070 ),
1071- // Flat default at every q-context (see the constant's own doc
1072- // comment) -- no `pick()` needed.
1073- eob_pt_512_chroma: cdf::EOB_PT_512_CHROMA,
1071+ eob_pt_512_chroma: pick(
1072+ q_ctx,
1073+ cdf::EOB_PT_512_CHROMA_Q0,
1074+ cdf::EOB_PT_512_CHROMA_Q1,
1075+ cdf::EOB_PT_512_CHROMA,
1076+ cdf::EOB_PT_512_CHROMA_Q3,
1077+ ),
10741078 eob_extra_luma_32: pick(
10751079 q_ctx,
10761080 cdf::EOB_EXTRA_LUMA_32_Q0,
crates/ec-av1/src/decode.rsmodified+93 -0
@@ -13697,6 +13697,99 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs(
1369713697
1369813698 #[cfg(test)]
1369913699 mod tests {
13700+
13701+ /// The rect `av1_nz_map_ctx_offset` tables, against libaom's own
13702+ /// generating rule (`txb_common.h`'s comment in
13703+ /// `get_nz_map_ctx_from_stats`) AND against [`base_ctx_rect`], which
13704+ /// spells that rule out instead of reading a table. libaom's array is
13705+ /// indexed by `coeff_idx = (col << bhl) | row` -- COLUMN-major, `bhl` =
13706+ /// log2 of the transform's height -- so a straight transcription of it
13707+ /// is `[col][row]`; ours is read `[row][col]`, and both tables were
13708+ /// carrying the transposed values. Invisible on the square table (it is
13709+ /// symmetric) and at `row, col < 2` (both halves agree there), which is
13710+ /// every position the gate that introduced them reached: it desynced the
13711+ /// first 64x32 strip of the merged superblock gate's seed 43
13712+ /// (lane-ab16 r2).
13713+ #[test]
13714+ fn rect_nz_map_ctx_offset_tables_are_row_major() {
13715+ // libaom, for a 2D class at (row, col) with ctx == 0.
13716+ fn libaom_offset(w: usize, h: usize, row: usize, col: usize) -> usize {
13717+ if row == 0 && col == 0 {
13718+ return 0;
13719+ }
13720+ if w < h && row < 2 {
13721+ return 11;
13722+ }
13723+ if w > h && col < 2 {
13724+ return 16;
13725+ }
13726+ if row + col < 2 {
13727+ 1
13728+ } else if row + col < 4 {
13729+ 6
13730+ } else {
13731+ 21
13732+ }
13733+ }
13734+ for row in 0..5 {
13735+ for col in 0..5 {
13736+ assert_eq!(
13737+ usize::from(crate::cdf::NZ_MAP_CTX_OFFSET_32X64[row][col]),
13738+ libaom_offset(32, 64, row, col),
13739+ "NZ_MAP_CTX_OFFSET_32X64[{row}][{col}]"
13740+ );
13741+ assert_eq!(
13742+ usize::from(crate::cdf::NZ_MAP_CTX_OFFSET_64X32[row][col]),
13743+ libaom_offset(64, 32, row, col),
13744+ "NZ_MAP_CTX_OFFSET_64X32[{row}][{col}]"
13745+ );
13746+ assert_eq!(
13747+ usize::from(crate::cdf::NZ_MAP_CTX_OFFSET_32[row][col]),
13748+ libaom_offset(32, 32, row, col),
13749+ "NZ_MAP_CTX_OFFSET_32[{row}][{col}]"
13750+ );
13751+ // The table path and the spelled-out path must agree: an
13752+ // empty grid gives ctx 0, so what is left is the offset.
13753+ for (w, h) in [(64usize, 32usize), (32, 64)] {
13754+ let grid = vec![0i32; w * h];
13755+ assert_eq!(
13756+ base_ctx(
13757+ &grid,
13758+ 32,
13759+ row,
13760+ col,
13761+ TxClass::TwoD,
13762+ Some((w, h)),
13763+ ),
13764+ base_ctx_rect(&grid, w, h, row, col),
13765+ "{w}x{h} at ({row}, {col}): table vs rule"
13766+ );
13767+ }
13768+ }
13769+ }
13770+ }
13771+
13772+ /// The 512-position chroma end-of-block CDF is the CHROMA 2D row, not
13773+ /// the flat 1D one. `av1_default_eob_multi512_cdfs` is
13774+ /// `[q][plane][eob_multi_ctx]`; taking the second inner index for the
13775+ /// plane picks a distribution that is flat for both planes, which is
13776+ /// what made every 32x16/16x32 chroma transform read a wrong
13777+ /// end-of-block group (lane-ab16 r2).
13778+ #[test]
13779+ fn the_chroma_512_eob_cdf_is_not_the_flat_one_dimensional_row() {
13780+ for table in [
13781+ crate::cdf::EOB_PT_512_CHROMA_Q0,
13782+ crate::cdf::EOB_PT_512_CHROMA_Q1,
13783+ crate::cdf::EOB_PT_512_CHROMA,
13784+ crate::cdf::EOB_PT_512_CHROMA_Q3,
13785+ ] {
13786+ assert_ne!(
13787+ table,
13788+ [3277, 6554, 9830, 13107, 16384, 19661, 22938, 26214, 29491, 32768, 0],
13789+ "the flat eob_multi_ctx == 1 row is not any q-context's chroma 2D row"
13790+ );
13791+ }
13792+ }
1370013793 use super::*;
1370113794
1370213795 // `crate::tile`'s `flat_key_frame_tile`/`dc_key_frame_tile_levels`/
lanes/ab16-r2.report.mdmodified+85 -47
@@ -64,53 +64,91 @@ The gate's doc no longer claims a `--tile-columns=1` attempt (there is none; r1'
6464 for the base tile defect stands) and now states why tx-size search is left at aomenc's
6565 default.
6666
67-## (2) Seed 43 of the merged SB gate: NOT the AB arms — a 64x32 strip coefficient desync
68-
69-`a_real_aomenc_stream_with_a_superblock_level_horz_vert_partition_decodes_pixel_exact` still
70-FAILS on this branch at seed 43, frame 0 luma. Bisected against the instrumented oracle
71-(`EC_TRACE_MODE_STEP` both sides, ranges compared element by element):
72-
73-```
74-first divergent element: mi_row=8 mi_col=16 name=skip oracle rng=60402 ours rng=58410
75-last matching element: mi_row=0 mi_col=16 name=angle_uv val=3 rng=49760 (identical)
76-```
77-
78-Everything up to and including the mode info of the **first 64x32 strip** at mi (0,16) —
79-pixel (64,0), `mode=7` (D203), `angle_y=3`, `uv_mode=7`, `angle_uv=3` — is bit-identical.
80-The streams part inside that block's **coefficients** (between its `angle_uv` and the next
81-block's `skip`), i.e. in the TX_64X32 residual path, and never resync: the oracle codes 16
82-mode-info elements for the frame, we code 44.
83-
84-This is upstream of every AB symbol in the stream. The stream's partition trace is
85-`w64: NONE, HORZ, SPLIT | ... | w16 mi=(2,11) value=4 (HORZ_A)` — the single AB16 block sits
86-in the third superblock, decoded long after the divergence. The luma error map confirms it:
87-superblock 0 is exact except its right/bottom 7-pixel deblock spill, and everything from
88-x >= 64 is wrong.
89-
90-Why it was green at base: base `fad4d5a` refuses seed 43 — but that refusal is *itself* a
91-consequence of this desync (it decodes a garbage partition value once the arithmetic decoder
92-has drifted). Lifting the AB16 refusal did not break seed 43; it removed the mask
93-([[refusal-hides-a-defect]]). The defect lives in lane-rectx's 64x32 strip coefficient path,
94-not in this lane's arms, and this branch changes nothing on that path (`Reach` answers only
95-affect prediction, never symbol consumption).
96-
97-Disposition: **deferred(lane-rectx / a 64x32-strip coefficient lane — this lane owns no code
98-on that path)**. Merge blocker either way: the gate is red on this branch until it is fixed,
99-and it is red for a real defect, so it must not be papered over.
100-
101-EVIDENCE: `<scratchpad>/ab16/{seed43.obu,o.all,u.all,ours.yuv,ff.yuv}` | `EC_AV1_GATE_DUMP`
102-+ `EC_TRACE_MODE_STEP=1` on `aomdec` and `decode_probe`, `diff` of the element traces, and a
103-per-8x8 luma error map vs `ffmpeg -f rawvideo` | first divergent element
104-`mi(8,16) skip rng 60402 vs 58410`; 20494/24576 luma bytes differ, all at x >= 56.
105-
106-## (4) The chroma-only residue (mandelbrot 64x64 start_x=-0.6 cq30)
107-
108-Not re-measured this round (turn budget went to (1) and (2)); r1's characterisation stands:
109-670 differing bytes, first at byte 4247 (U plane), luma exact. Whether AB blocks are present
110-in that stream is still unanswered.
111-Disposition: **deferred(a chroma/SMOOTH prediction lane — r1 already scoped it)**.
67+## (2) Seed 43 of the merged SB gate: TWO real defects, both fixed -- it is GREEN now
68+
69+The r2 first pass deferred this as "lane-rectx's problem". That deferral was wrong on the
70+disposition and right on the location: the defects are on the 64x32-strip path, and they are
71+two table transcriptions this lane could fix in one round.
72+
73+The gate's own recipe spells `--enable-ab-partitions=0` (stream.rs:9663), so aomenc wrote no AB
74+partition into seed 43 at all -- which means the refusal base `fad4d5a` stopped at ("a
75+HORZ_A/HORZ_B/VERT_A partition below 16x16") was decoded out of an ALREADY desynced stream. The
76+base pass was vacuous ([[refusal-hides-a-defect]]); lifting the refusal only removed the mask.
77+
78+Bisection, element by element, our `EC_TRACE_COEFF` against the instrumented `aomdec`'s
79+(`EC_COEFF`/`EC_COEFF_STEP`, msac RANGE compared, never `tell()`):
80+
81+**Defect A -- the two rect `av1_nz_map_ctx_offset` tables were transposed.**
82+First divergence: the luma TU of the first 64x32 strip (mi 0,16), `tx_size=12` (`TX_64X32`),
83+at scan index 7, libaom position 65 = (row 1, col 2), where the oracle reads ctx 6 and we read
84+16. libaom's array is indexed `coeff_idx = (col << bhl) | row` -- COLUMN-major, `bhl` = log2 of
85+the transform's HEIGHT (`txb_common.h` `get_nz_map_ctx_from_stats`) -- so a straight
86+transcription of it is `[col][row]`, and `base_ctx` reads `[row][col]`.
87+`crates/ec-av1/src/cdf.rs` `NZ_MAP_CTX_OFFSET_32X64` / `NZ_MAP_CTX_OFFSET_64X32`: both
88+transposed into true `[row][col]`. Invisible on the square table (symmetric) and at
89+`row, col < 2` (both halves agree there), which is every position lane-sbpart r8's gate ever
90+reached -- class [[reference-layout-not-spec]] / [[scan-weights-cross-axis]].
91+
92+**Defect B -- the 512-position CHROMA end-of-block CDF was the flat 1D row.**
93+With A fixed the stream ran on to the same strip's chroma TU (`tx_size=10`, `TX_32X16`), where
94+the oracle reads `eob=3` and we read `eob=103` off `EOB_PT_512_CHROMA`.
95+`av1_default_eob_multi512_cdfs` is `[q][plane][eob_multi_ctx][10]` (`token_cdfs.h:874`); the
96+constant carried the SECOND inner index (`eob_multi_ctx == 1`, the 1D `TX_CLASS_HORIZ`/`VERT`
97+row, which is the flat 3277/6554/... distribution for BOTH planes) instead of the chroma
98+plane's 2D row -- and its doc comment had rationalised the flatness as "libaom's default is the
99+same at all four q-contexts". `crates/ec-av1/src/cdf.rs`: the real chroma rows, all four
100+q-contexts (`EOB_PT_512_CHROMA{,_Q0,_Q1,_Q3}`); `crates/ec-av1/src/cdf_state.rs:1073`: `pick()`
101+by q-context like every sibling table. Class [[cdf-row-held-constant]].
102+
103+Both are pinned by unit tests that do not need a stream (`decode.rs` tests):
104+`rect_nz_map_ctx_offset_tables_are_row_major` re-derives every entry of all three tables from
105+libaom's own generating rule AND asserts the table path (`base_ctx`) equals the spelled-out
106+rule path (`base_ctx_rect`) at every (row, col) for both shapes -- those two disagreed
107+silently before; `the_chroma_512_eob_cdf_is_not_the_flat_one_dimensional_row` pins the
108+mis-indexing itself.
109+
110+EVIDENCE: <scratchpad>/ab16/{seed43.obu,ref.yuv,ours3.yuv,oc.raw,uc2.raw} | seed-43 stream
111+regenerated from the gate recipe (`gradients` seed 43, 192x128, cq45), decoded by
112+`~/.cache/aom-oracle/build/aomdec --rawvideo` and by `decode_probe`, coefficient traces
113+compared element by element | before: 20494/24576 luma bytes differ, first divergent element
114+`TX_64X32` luma scan idx 7 (rng 57584 vs ours); after defect A: first divergence moves to the
115+chroma `TX_32X16` eob (oracle 3, ours 103); after both: `cmp ours3.yuv ref.yuv` -> identical,
116+36864/36864 bytes.
117+
118+## (4) The chroma-only residue (mandelbrot 64x64 start_x=-0.6 cq30) -- re-measured, AB blocks ARE in it
119+
120+Unchanged by this round: 670 differing bytes, all of them chroma, first at offset 4246 (U
121+plane starts at 4096), luma bit-exact. And the stream does contain all four AB arms: the
122+16x16 partition symbols are `value=0 x2, 1 x2, 2, 3 x7, 4, 5, 6, 7` -- one HORZ_A, one HORZ_B,
123+one VERT_A, one VERT_B. Luma-exact with chroma-wrong means the entropy stream is fine through
124+the whole frame, so this is chroma PREDICTION/reconstruction, not a table or a desync
125+(a green entropy trace plus byte-exact luma exonerates the coefficient path -- the
126+`h265-chroma`/`gmaffine` class note in memory).
127+Disposition: **deferred(a chroma-prediction lane -- 4-wide/8-wide intra MC at these sizes; this
128+lane's tables are now proven exact by the seed-43 bisection)**.
129+
130+EVIDENCE: <scratchpad>/ab16/{m30.obu,m_ref.yuv,m_ours.yuv} | aomenc gate recipe at cq30 on
131+`mandelbrot=size=64x64:start_x=-0.6`, decoded by aomdec and decode_probe, byte diff by plane;
132+`EC_AV1_TRACE=1 ... | grep partition_w16` for the arm census | 670 diffs, 0 of them luma,
133+first at 4246; AB arm values 4/5/6/7 each present once.
112134
113135 ## Test totals
114136
115-`EC_AV1_REQUIRE_AOMENC=1 cargo test -p ec-av1 --lib` (log: `~/.cache/ab16-r2-suite.log`):
116-see the HANDOFF line — the only failure is the seed-43 SB gate above.
137+`EC_AV1_REQUIRE_AOMENC=1 CARGO_TARGET_DIR=$HOME/.cache/cargo-target-ab16 cargo test -p ec-av1 --lib`
138+(log `$HOME/.cache/ab16-suite.log`): **274 passed; 0 failed; 24 ignored**, 619 s. That run
139+includes both gates this lane owns and every sibling the fixes touch --
140+`a_real_aomenc_stream_with_a_superblock_level_horz_vert_partition_decodes_pixel_exact` (seed 43
141+included, 40 seeds) is GREEN, `a_real_aomenc_stream_with_ab_partitions_below_16x16_decodes_pixel_exact`
142+is green with `--enable-tx-size-search` at aomenc's default on four of its ten attempts, and the
143+rectx / tiny / rectwire / sbpart gates that share the two corrected tables all pass.
144+
145+EVIDENCE: $HOME/.cache/ab16-suite.log | `cargo test -p ec-av1 --lib` with the aomenc oracle
146+required | `test result: ok. 274 passed; 0 failed; 24 ignored`.
147+
148+## Residue
149+
150+- deferred(a chroma-prediction lane): the mandelbrot cq30 chroma-only 670-byte residue above.
151+- deferred(base tile defect, r1 reproducer stands): no `--tile-columns=1` arm in the AB gate.
152+- deferred(lane-rectx): the tx-size-search AB attempts still stop at the "HORZ/VERT intra strip
153+ with a split transform" refusal, so they prove the reach lookup, not pixels.
154+- accepted: 10-bit proves HORZ_A + VERT_A only (r1).