From 6906e60853a43f4b152a9b092138befe84f46c4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 2 Sep 2026 02:49:21 +0300 Subject: [PATCH] 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.rs | 55 ++++++++++---- crates/ec-av1/src/cdf_state.rs | 10 ++- crates/ec-av1/src/decode.rs | 93 +++++++++++++++++++++++ lanes/ab16-r2.report.md | 132 +++++++++++++++++++++------------ 4 files changed, 225 insertions(+), 65 deletions(-) diff --git a/crates/ec-av1/src/cdf.rs b/crates/ec-av1/src/cdf.rs index 466f5513..02c5092b 100644 --- a/crates/ec-av1/src/cdf.rs +++ b/crates/ec-av1/src/cdf.rs @@ -309,11 +309,28 @@ pub const EOB_PT_32_CHROMA_Q3: [u16; 7] = [22086, 24282, 27010, 29770, 31743, 32 /// coefficients truncate to the 64x64-superblock's own 32x32 corner-scan /// (`TxbSet::Luma64`) but whose *chroma* plane, at 32x16, is small enough /// that no truncation applies). Unlike every other `EOB_PT_*` table here, -/// libaom's default is the exact same flat distribution at all four -/// q-contexts and both `eob_multi_size` selectors (`token_cdfs.h` rows), so -/// one constant covers every q-context -- no `_Q0`/`_Q1`/`_Q3` siblings. +/// `av1_default_eob_multi512_cdfs` is +/// `[q][plane][eob_multi_ctx][10]`: the flat 3277/6554/... row this constant +/// used to carry is the SECOND inner index (`eob_multi_ctx == 1`, the 1D +/// `TX_CLASS_HORIZ`/`VERT` row that is flat for BOTH planes), not the chroma +/// plane -- reading it here made every 32x16/16x32 chroma transform decode a +/// wrong end-of-block group and desync the tile (lane-ab16 r2, the second +/// defect under the merged SB gate's seed 43). Chroma's real 2D rows vary +/// per q-context exactly like luma's, so all four are spelled out. pub const EOB_PT_512_CHROMA: [u16; 11] = [ - 3277, 6554, 9830, 13107, 16384, 19661, 22938, 26214, 29491, 32768, 0, + 12015, 14769, 19588, 22052, 24222, 25812, 27300, 29219, 32114, 32768, 0, +]; +/// [`EOB_PT_512_CHROMA`], q-context 0 (`base_q_idx` 0..=20). +pub const EOB_PT_512_CHROMA_Q0: [u16; 11] = [ + 5095, 6446, 9996, 13354, 16017, 17986, 20919, 26129, 29140, 32768, 0, +]; +/// [`EOB_PT_512_CHROMA`], q-context 1 (`base_q_idx` 21..=60). +pub const EOB_PT_512_CHROMA_Q1: [u16; 11] = [ + 7265, 9979, 15819, 19250, 21780, 23846, 26478, 28396, 31811, 32768, 0, +]; +/// [`EOB_PT_512_CHROMA`], q-context 3 (`base_q_idx` 121..=255). +pub const EOB_PT_512_CHROMA_Q3: [u16; 11] = [ + 21093, 23043, 25742, 27658, 29097, 29716, 30073, 30820, 31956, 32768, 0, ]; /// `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] = [ /// resolves the CDF *set* to `TX_64X64`, but `get_nz_map_ctx_from_stats` still /// indexes this position table by the raw, un-adjusted `tx_size` -- lane-sbpart /// r8 root cause) is genuinely rectangular, not square. Distinct from -/// [`NZ_MAP_CTX_OFFSET_32`] starting at `(row=1, col=0)`. +/// [`NZ_MAP_CTX_OFFSET_32`] starting at `(row=2, col=0)`. +/// +/// `[row][col]`, which is libaom's own array TRANSPOSED: its `coeff_idx` is +/// `(col << bhl) | row` (`txb_common.h`, `bhl` = log2 of the transform's +/// HEIGHT), so `av1_nz_map_ctx_offset_32x64[i]` runs down a column, not +/// across a row. Both rect tables were transcribed in that column-major +/// order and then read `[row][col]` -- invisible on the square tables (they +/// are symmetric) and at `row, col < 2` (both halves agree there), which is +/// every position the r8 gate reached (lane-ab16 r2). pub const NZ_MAP_CTX_OFFSET_32X64: [[u8; 5]; 5] = [ - [0, 11, 6, 6, 21], - [11, 11, 6, 21, 21], - [11, 11, 21, 21, 21], - [11, 11, 21, 21, 21], - [11, 11, 21, 21, 21], + [0, 11, 11, 11, 11], + [11, 11, 11, 11, 11], + [6, 6, 21, 21, 21], + [6, 21, 21, 21, 21], + [21, 21, 21, 21, 21], ]; /// [`NZ_MAP_CTX_OFFSET_32X64`]'s `TX_64X32` counterpart (libaom /// `av1_nz_map_ctx_offset_64x32`): a `PARTITION_HORZ` strip's luma corner. pub const NZ_MAP_CTX_OFFSET_64X32: [[u8; 5]; 5] = [ - [0, 16, 16, 16, 16], - [16, 16, 16, 16, 16], - [6, 6, 21, 21, 21], - [6, 21, 21, 21, 21], - [21, 21, 21, 21, 21], + [0, 16, 6, 6, 21], + [16, 16, 6, 21, 21], + [16, 16, 21, 21, 21], + [16, 16, 21, 21, 21], + [16, 16, 21, 21, 21], ]; /// `Default_Txb_Skip_Cdf[2][2][7..10]` (spec 9.4): the all-zero flag of a diff --git a/crates/ec-av1/src/cdf_state.rs b/crates/ec-av1/src/cdf_state.rs index 1f4a4196..3454016b 100644 --- a/crates/ec-av1/src/cdf_state.rs +++ b/crates/ec-av1/src/cdf_state.rs @@ -1068,9 +1068,13 @@ impl Cdfs { cdf::EOB_PT_32_CHROMA, cdf::EOB_PT_32_CHROMA_Q3, ), - // Flat default at every q-context (see the constant's own doc - // comment) -- no `pick()` needed. - eob_pt_512_chroma: cdf::EOB_PT_512_CHROMA, + eob_pt_512_chroma: pick( + q_ctx, + cdf::EOB_PT_512_CHROMA_Q0, + cdf::EOB_PT_512_CHROMA_Q1, + cdf::EOB_PT_512_CHROMA, + cdf::EOB_PT_512_CHROMA_Q3, + ), eob_extra_luma_32: pick( q_ctx, cdf::EOB_EXTRA_LUMA_32_Q0, diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index 42aef227..8dc833c0 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -13697,6 +13697,99 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( #[cfg(test)] mod tests { + + /// The rect `av1_nz_map_ctx_offset` tables, against libaom's own + /// generating rule (`txb_common.h`'s comment in + /// `get_nz_map_ctx_from_stats`) AND against [`base_ctx_rect`], which + /// spells that rule out instead of reading a table. libaom's array is + /// indexed by `coeff_idx = (col << bhl) | row` -- COLUMN-major, `bhl` = + /// log2 of the transform's height -- so a straight transcription of it + /// is `[col][row]`; ours is read `[row][col]`, and both tables were + /// carrying the transposed values. Invisible on the square table (it is + /// symmetric) and at `row, col < 2` (both halves agree there), which is + /// every position the gate that introduced them reached: it desynced the + /// first 64x32 strip of the merged superblock gate's seed 43 + /// (lane-ab16 r2). + #[test] + fn rect_nz_map_ctx_offset_tables_are_row_major() { + // libaom, for a 2D class at (row, col) with ctx == 0. + fn libaom_offset(w: usize, h: usize, row: usize, col: usize) -> usize { + if row == 0 && col == 0 { + return 0; + } + if w < h && row < 2 { + return 11; + } + if w > h && col < 2 { + return 16; + } + if row + col < 2 { + 1 + } else if row + col < 4 { + 6 + } else { + 21 + } + } + for row in 0..5 { + for col in 0..5 { + assert_eq!( + usize::from(crate::cdf::NZ_MAP_CTX_OFFSET_32X64[row][col]), + libaom_offset(32, 64, row, col), + "NZ_MAP_CTX_OFFSET_32X64[{row}][{col}]" + ); + assert_eq!( + usize::from(crate::cdf::NZ_MAP_CTX_OFFSET_64X32[row][col]), + libaom_offset(64, 32, row, col), + "NZ_MAP_CTX_OFFSET_64X32[{row}][{col}]" + ); + assert_eq!( + usize::from(crate::cdf::NZ_MAP_CTX_OFFSET_32[row][col]), + libaom_offset(32, 32, row, col), + "NZ_MAP_CTX_OFFSET_32[{row}][{col}]" + ); + // The table path and the spelled-out path must agree: an + // empty grid gives ctx 0, so what is left is the offset. + for (w, h) in [(64usize, 32usize), (32, 64)] { + let grid = vec![0i32; w * h]; + assert_eq!( + base_ctx( + &grid, + 32, + row, + col, + TxClass::TwoD, + Some((w, h)), + ), + base_ctx_rect(&grid, w, h, row, col), + "{w}x{h} at ({row}, {col}): table vs rule" + ); + } + } + } + } + + /// The 512-position chroma end-of-block CDF is the CHROMA 2D row, not + /// the flat 1D one. `av1_default_eob_multi512_cdfs` is + /// `[q][plane][eob_multi_ctx]`; taking the second inner index for the + /// plane picks a distribution that is flat for both planes, which is + /// what made every 32x16/16x32 chroma transform read a wrong + /// end-of-block group (lane-ab16 r2). + #[test] + fn the_chroma_512_eob_cdf_is_not_the_flat_one_dimensional_row() { + for table in [ + crate::cdf::EOB_PT_512_CHROMA_Q0, + crate::cdf::EOB_PT_512_CHROMA_Q1, + crate::cdf::EOB_PT_512_CHROMA, + crate::cdf::EOB_PT_512_CHROMA_Q3, + ] { + assert_ne!( + table, + [3277, 6554, 9830, 13107, 16384, 19661, 22938, 26214, 29491, 32768, 0], + "the flat eob_multi_ctx == 1 row is not any q-context's chroma 2D row" + ); + } + } use super::*; // `crate::tile`'s `flat_key_frame_tile`/`dc_key_frame_tile_levels`/ diff --git a/lanes/ab16-r2.report.md b/lanes/ab16-r2.report.md index 7488caff..f51226a0 100644 --- a/lanes/ab16-r2.report.md +++ b/lanes/ab16-r2.report.md @@ -64,53 +64,91 @@ The gate's doc no longer claims a `--tile-columns=1` attempt (there is none; r1' for the base tile defect stands) and now states why tx-size search is left at aomenc's default. -## (2) Seed 43 of the merged SB gate: NOT the AB arms — a 64x32 strip coefficient desync - -`a_real_aomenc_stream_with_a_superblock_level_horz_vert_partition_decodes_pixel_exact` still -FAILS on this branch at seed 43, frame 0 luma. Bisected against the instrumented oracle -(`EC_TRACE_MODE_STEP` both sides, ranges compared element by element): - -``` -first divergent element: mi_row=8 mi_col=16 name=skip oracle rng=60402 ours rng=58410 -last matching element: mi_row=0 mi_col=16 name=angle_uv val=3 rng=49760 (identical) -``` - -Everything up to and including the mode info of the **first 64x32 strip** at mi (0,16) — -pixel (64,0), `mode=7` (D203), `angle_y=3`, `uv_mode=7`, `angle_uv=3` — is bit-identical. -The streams part inside that block's **coefficients** (between its `angle_uv` and the next -block's `skip`), i.e. in the TX_64X32 residual path, and never resync: the oracle codes 16 -mode-info elements for the frame, we code 44. - -This is upstream of every AB symbol in the stream. The stream's partition trace is -`w64: NONE, HORZ, SPLIT | ... | w16 mi=(2,11) value=4 (HORZ_A)` — the single AB16 block sits -in the third superblock, decoded long after the divergence. The luma error map confirms it: -superblock 0 is exact except its right/bottom 7-pixel deblock spill, and everything from -x >= 64 is wrong. - -Why it was green at base: base `fad4d5a` refuses seed 43 — but that refusal is *itself* a -consequence of this desync (it decodes a garbage partition value once the arithmetic decoder -has drifted). Lifting the AB16 refusal did not break seed 43; it removed the mask -([[refusal-hides-a-defect]]). The defect lives in lane-rectx's 64x32 strip coefficient path, -not in this lane's arms, and this branch changes nothing on that path (`Reach` answers only -affect prediction, never symbol consumption). - -Disposition: **deferred(lane-rectx / a 64x32-strip coefficient lane — this lane owns no code -on that path)**. Merge blocker either way: the gate is red on this branch until it is fixed, -and it is red for a real defect, so it must not be papered over. - -EVIDENCE: `/ab16/{seed43.obu,o.all,u.all,ours.yuv,ff.yuv}` | `EC_AV1_GATE_DUMP` -+ `EC_TRACE_MODE_STEP=1` on `aomdec` and `decode_probe`, `diff` of the element traces, and a -per-8x8 luma error map vs `ffmpeg -f rawvideo` | first divergent element -`mi(8,16) skip rng 60402 vs 58410`; 20494/24576 luma bytes differ, all at x >= 56. - -## (4) The chroma-only residue (mandelbrot 64x64 start_x=-0.6 cq30) - -Not re-measured this round (turn budget went to (1) and (2)); r1's characterisation stands: -670 differing bytes, first at byte 4247 (U plane), luma exact. Whether AB blocks are present -in that stream is still unanswered. -Disposition: **deferred(a chroma/SMOOTH prediction lane — r1 already scoped it)**. +## (2) Seed 43 of the merged SB gate: TWO real defects, both fixed -- it is GREEN now + +The r2 first pass deferred this as "lane-rectx's problem". That deferral was wrong on the +disposition and right on the location: the defects are on the 64x32-strip path, and they are +two table transcriptions this lane could fix in one round. + +The gate's own recipe spells `--enable-ab-partitions=0` (stream.rs:9663), so aomenc wrote no AB +partition into seed 43 at all -- which means the refusal base `fad4d5a` stopped at ("a +HORZ_A/HORZ_B/VERT_A partition below 16x16") was decoded out of an ALREADY desynced stream. The +base pass was vacuous ([[refusal-hides-a-defect]]); lifting the refusal only removed the mask. + +Bisection, element by element, our `EC_TRACE_COEFF` against the instrumented `aomdec`'s +(`EC_COEFF`/`EC_COEFF_STEP`, msac RANGE compared, never `tell()`): + +**Defect A -- the two rect `av1_nz_map_ctx_offset` tables were transposed.** +First divergence: the luma TU of the first 64x32 strip (mi 0,16), `tx_size=12` (`TX_64X32`), +at scan index 7, libaom position 65 = (row 1, col 2), where the oracle reads ctx 6 and we read +16. libaom's array is indexed `coeff_idx = (col << bhl) | row` -- COLUMN-major, `bhl` = log2 of +the transform's HEIGHT (`txb_common.h` `get_nz_map_ctx_from_stats`) -- so a straight +transcription of it is `[col][row]`, and `base_ctx` reads `[row][col]`. +`crates/ec-av1/src/cdf.rs` `NZ_MAP_CTX_OFFSET_32X64` / `NZ_MAP_CTX_OFFSET_64X32`: both +transposed into true `[row][col]`. Invisible on the square table (symmetric) and at +`row, col < 2` (both halves agree there), which is every position lane-sbpart r8's gate ever +reached -- class [[reference-layout-not-spec]] / [[scan-weights-cross-axis]]. + +**Defect B -- the 512-position CHROMA end-of-block CDF was the flat 1D row.** +With A fixed the stream ran on to the same strip's chroma TU (`tx_size=10`, `TX_32X16`), where +the oracle reads `eob=3` and we read `eob=103` off `EOB_PT_512_CHROMA`. +`av1_default_eob_multi512_cdfs` is `[q][plane][eob_multi_ctx][10]` (`token_cdfs.h:874`); the +constant carried the SECOND inner index (`eob_multi_ctx == 1`, the 1D `TX_CLASS_HORIZ`/`VERT` +row, which is the flat 3277/6554/... distribution for BOTH planes) instead of the chroma +plane's 2D row -- and its doc comment had rationalised the flatness as "libaom's default is the +same at all four q-contexts". `crates/ec-av1/src/cdf.rs`: the real chroma rows, all four +q-contexts (`EOB_PT_512_CHROMA{,_Q0,_Q1,_Q3}`); `crates/ec-av1/src/cdf_state.rs:1073`: `pick()` +by q-context like every sibling table. Class [[cdf-row-held-constant]]. + +Both are pinned by unit tests that do not need a stream (`decode.rs` tests): +`rect_nz_map_ctx_offset_tables_are_row_major` re-derives every entry of all three tables from +libaom's own generating rule AND asserts the table path (`base_ctx`) equals the spelled-out +rule path (`base_ctx_rect`) at every (row, col) for both shapes -- those two disagreed +silently before; `the_chroma_512_eob_cdf_is_not_the_flat_one_dimensional_row` pins the +mis-indexing itself. + +EVIDENCE: /ab16/{seed43.obu,ref.yuv,ours3.yuv,oc.raw,uc2.raw} | seed-43 stream +regenerated from the gate recipe (`gradients` seed 43, 192x128, cq45), decoded by +`~/.cache/aom-oracle/build/aomdec --rawvideo` and by `decode_probe`, coefficient traces +compared element by element | before: 20494/24576 luma bytes differ, first divergent element +`TX_64X32` luma scan idx 7 (rng 57584 vs ours); after defect A: first divergence moves to the +chroma `TX_32X16` eob (oracle 3, ours 103); after both: `cmp ours3.yuv ref.yuv` -> identical, +36864/36864 bytes. + +## (4) The chroma-only residue (mandelbrot 64x64 start_x=-0.6 cq30) -- re-measured, AB blocks ARE in it + +Unchanged by this round: 670 differing bytes, all of them chroma, first at offset 4246 (U +plane starts at 4096), luma bit-exact. And the stream does contain all four AB arms: the +16x16 partition symbols are `value=0 x2, 1 x2, 2, 3 x7, 4, 5, 6, 7` -- one HORZ_A, one HORZ_B, +one VERT_A, one VERT_B. Luma-exact with chroma-wrong means the entropy stream is fine through +the whole frame, so this is chroma PREDICTION/reconstruction, not a table or a desync +(a green entropy trace plus byte-exact luma exonerates the coefficient path -- the +`h265-chroma`/`gmaffine` class note in memory). +Disposition: **deferred(a chroma-prediction lane -- 4-wide/8-wide intra MC at these sizes; this +lane's tables are now proven exact by the seed-43 bisection)**. + +EVIDENCE: /ab16/{m30.obu,m_ref.yuv,m_ours.yuv} | aomenc gate recipe at cq30 on +`mandelbrot=size=64x64:start_x=-0.6`, decoded by aomdec and decode_probe, byte diff by plane; +`EC_AV1_TRACE=1 ... | grep partition_w16` for the arm census | 670 diffs, 0 of them luma, +first at 4246; AB arm values 4/5/6/7 each present once. ## Test totals -`EC_AV1_REQUIRE_AOMENC=1 cargo test -p ec-av1 --lib` (log: `~/.cache/ab16-r2-suite.log`): -see the HANDOFF line — the only failure is the seed-43 SB gate above. +`EC_AV1_REQUIRE_AOMENC=1 CARGO_TARGET_DIR=$HOME/.cache/cargo-target-ab16 cargo test -p ec-av1 --lib` +(log `$HOME/.cache/ab16-suite.log`): **274 passed; 0 failed; 24 ignored**, 619 s. That run +includes both gates this lane owns and every sibling the fixes touch -- +`a_real_aomenc_stream_with_a_superblock_level_horz_vert_partition_decodes_pixel_exact` (seed 43 +included, 40 seeds) is GREEN, `a_real_aomenc_stream_with_ab_partitions_below_16x16_decodes_pixel_exact` +is green with `--enable-tx-size-search` at aomenc's default on four of its ten attempts, and the +rectx / tiny / rectwire / sbpart gates that share the two corrected tables all pass. + +EVIDENCE: $HOME/.cache/ab16-suite.log | `cargo test -p ec-av1 --lib` with the aomenc oracle +required | `test result: ok. 274 passed; 0 failed; 24 ignored`. + +## Residue + +- deferred(a chroma-prediction lane): the mandelbrot cq30 chroma-only 670-byte residue above. +- deferred(base tile defect, r1 reproducer stands): no `--tile-columns=1` arm in the AB gate. +- deferred(lane-rectx): the tx-size-search AB attempts still stop at the "HORZ/VERT intra strip + with a split transform" refusal, so they prove the reach lookup, not pixels. +- accepted: 10-bit proves HORZ_A + VERT_A only (r1).