diff --git a/crates/ec-av1/src/cdf.rs b/crates/ec-av1/src/cdf.rs index 4a8beedc..fda4e1ae 100644 --- a/crates/ec-av1/src/cdf.rs +++ b/crates/ec-av1/src/cdf.rs @@ -843,11 +843,11 @@ pub const NZ_MAP_CTX_OFFSET_32: [[u8; 5]; 5] = [ // `pos = col*32+row` raster (same convention [`SCAN_32X16`]'s doc comment // already documents this decoder transcribes out of). pub const NZ_MAP_CTX_OFFSET_32X64: [[u8; 5]; 5] = [ - [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], + [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], ]; /// [`NZ_MAP_CTX_OFFSET_32X64`]'s `TX_64X32` counterpart (libaom @@ -856,11 +856,11 @@ pub const NZ_MAP_CTX_OFFSET_32X64: [[u8; 5]; 5] = [ /// regardless of row -- transpose of [`NZ_MAP_CTX_OFFSET_32X64`]'s `row < 2` /// branch, same r3 fix (see that table's doc comment). pub const NZ_MAP_CTX_OFFSET_64X32: [[u8; 5]; 5] = [ - [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], + [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], ]; /// `Default_Txb_Skip_Cdf[2][2][7..10]` (spec 9.4): the all-zero flag of a diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index 827cfa8d..31ad73c2 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -1814,33 +1814,30 @@ fn base_ctx( // (lane-sbpart r8 root cause: `(row=1, col=0)` reads ctx 2 under the // square table vs the real ctx 13 the rect table gives). TxClass::TwoD => { - // OPEN (lane-rectsplit r3, measured): the two rect - // transcriptions in [`cdf`] are packed `[col][row]` -- libaom's - // `av1_nz_map_ctx_offset_32x64` is a FLAT 1024-entry array whose - // `coeff_idx` is COLUMN-major with stride 32 (`TX_CLASS_HORIZ` - // reads its column back as `coeff_idx >> bhl`), and entries - // 0..5 / 32..35 match the generating rule (txb_common.h:199-209) - // only under `flat[col * 32 + row]`; - // `nz_map_ctx_offset_tables_match_the_rect_rule` pins that. - // Yet reading `[col][row]` here BREAKS - // `a_real_aomenc_stream_with_a_superblock_level_horz_vert_partition_decodes_pixel_exact` - // (seed 43) while `[row][col]` breaks the split-transform SB gate - // (seed 50); swapping the shape test as well fails BOTH ways. So - // this read is not the defect -- our (row, col) here come from a - // square 32x32-corner scan whose orientation relative to libaom's - // column-major `coeff_idx` is what is really unproven, and each - // "fix" only moves which rare corner position desyncs. Left at - // the r1 reading, the one every merged gate is green under. - // `EC_NZOFF_DUMP=1` prints every rect-shaped read. - let table = match rect_shape { - Some((w, h)) if w < h => &cdf::NZ_MAP_CTX_OFFSET_32X64, - Some((w, h)) if w > h => &cdf::NZ_MAP_CTX_OFFSET_64X32, - _ => &cdf::NZ_MAP_CTX_OFFSET_32, - }; - if std::env::var_os("EC_NZOFF_DUMP").is_some() { - eprintln!("NZOFF side={side} shape={rect_shape:?} row={row} col={col}"); + // lane-rectsplit r4: apply libaom's GENERATING RULE + // (`txb_common.h:199-209`) directly instead of indexing the + // transcribed 5x5 tables. `av1_nz_map_ctx_offset[tx_size]` is a + // FLAT array read at `coeff_idx`, and `coeff_idx` is COLUMN-major + // (libaom decomposes it as `col = coeff_idx >> bhl`, `bhl` the + // ADJUSTED height's log2, 5 for both `TX_64X32` and `TX_32X64`), + // so 32 consecutive entries are one COLUMN: the [`cdf`] + // transcriptions are `[col][row]` and this read had them as + // `[row][col]` -- the transpose, i.e. the other shape's rule with + // 11 and 16 swapped. Same values as [`base_ctx_rect`] now, stated + // once; `base_ctx_rect_offsets_match_the_transcribed_tables_over_the_whole_domain` + // pins the rule against the tables over their whole 5x5 domain. + if let Some((w, h)) = rect_shape { + if std::env::var_os("EC_NZOFF_DUMP").is_some() { + eprintln!("NZOFF side={side} shape={w}x{h} row={row} col={col}"); + } + if w < h && row < 2 { + return ctx + 11; + } + if w > h && col < 2 { + return ctx + 16; + } } - ctx + table[row.min(4)][col.min(4)] as usize + ctx + cdf::NZ_MAP_CTX_OFFSET_32[row.min(4)][col.min(4)] as usize } TxClass::Horiz => ctx + nz_map_ctx_offset_1d(col.min(31)), TxClass::Vert => ctx + nz_map_ctx_offset_1d(row.min(31)), @@ -2257,6 +2254,15 @@ fn read_coeffs( } else { let ctx = base_ctx(&levels, side, row, col, class, rect_shape); let v = dec.symbol(&mut coding.base[ctx]) as i32; + if ec_trace_coeff { + let (rng, _) = dec.debug_state(); + // `pos` in libaom's column-major `coeff_idx` convention, so + // the ladder lines up with instrumented `aomdec`. + let apos = col * side + row; + eprintln!( + "EC_COEFF_STEP tag=base c={scan_idx} pos={apos} ctx={ctx} level={v} rng={rng}" + ); + } if trace { eprintln!( "TRACE base scan_idx={scan_idx} pos={pos} row={row} col={col} ctx={ctx} value={v}" @@ -4209,6 +4215,13 @@ fn decode_rect_split( dc_sign_ctx(around[plane_idx].2), default_tx, )?; + // The same delta_q proof the unsplit rect64 path records + // (lane-rectsplit r4: lifting the split-transform refusal moved + // superblock strips onto this path, so only counting there would + // leave the delta_q gate's counter silent for them). + if bw.max(bh) == 64 && CURRENT_Q_IDX.with(|c| c.get()) != i32::from(base_q_idx) { + RECT64_QIDX_DRIFT_HITS.with(|c| c.set(c.get() + 1)); + } let residual = dequant_and_inverse_typed_wh( &levels, chroma_w, @@ -4872,20 +4885,34 @@ fn decode_block_rect64( }; if depth != 0 { TX_DEPTH_HITS.with(|c| c.set(c.get() + 1)); - // lane-rectsplit r1/r3: [`decode_rect_split`] handles this shape too - // and the per-unit port is wired here, but the gate - // (`a_real_aomenc_stream_with_a_split_transform_superblock_strip_decodes_pixel_exact`, - // `#[ignore]`d with its measurement) is still RED at seed 50 (luma - // (171,56), ours 147 vs ffmpeg 148). r2 believed the cause was a - // transposed `av1_nz_map_ctx_offset` read; r3 measured that - // transposing it turns THIS gate green and the merged SB-level - // HORZ/VERT partition gate RED (seed 43), so the cause is still - // open. A refusal this lane cannot lift is not one it may ship wrong - // pixels past. - return Err(unsupported( - "a superblock-level HORZ/VERT strip with a split transform (per-unit rect \ - prediction is not ported)", - )); + // lane-rectsplit r1/r4: the superblock-level strip splits its + // transform through the very same per-unit path as its 32x32-level + // sibling (`sub_tx_size_map[TX_64X32] == TX_32X32`, square from the + // first step on, so `bw.min(bh) >> (depth - 1)` names the unit). + let tx = bw.min(bh) >> (depth - 1); + if std::env::var_os("EC_SBPART_DUMP64").is_some() { + eprintln!( + "DUMP64SPLIT mi_r={mi_r} mi_c={mi_c} px={px} py={py} bw={bw} bh={bh} \ + depth={depth} tx={tx} mode={mode} uv={uv_predict_mode} skip={skip} \ + angle_y={angle_delta_y}" + ); + } + let modes = RectStripModes { + skip, + mode, + angle_delta_y, + uv_predict_mode, + angle_delta_uv, + alpha, + filter_intra, + smooth_neighbor, + smooth_neighbor_uv, + }; + decode_rect_split( + dec, cdfs, neighbours, at, bw, bh, tx, &modes, y, u, v, base_q_idx, reduced_tx_set, + )?; + RECT_PARTITION_HITS.with(|c| c.set(c.get() + 1)); + return Ok(()); } let (tx_w, tx_h) = (bw, bh); let (cpx, cpy) = (px / 2, py / 2); @@ -16556,33 +16583,34 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( mod tests { use super::*; - /// lane-rectsplit r2: `av1_nz_map_ctx_offset` is packed COLUMN-major, so - /// the two rect transcriptions in [`cdf`] must be read `[col][row]`. - /// [`base_ctx_rect`] states libaom's generating rule - /// (`txb_common.h:199-209`) in display coordinates; this pins the tables - /// against it, which is what a transposed read would break. + /// lane-rectsplit r4 (class `enumerate-table-domain`): [`base_ctx`]'s + /// `TX_CLASS_2D` arm must, over the WHOLE 5x5 domain of both rect shapes + /// and the square one, return exactly what libaom's + /// `av1_nz_map_ctx_offset[tx_size][coeff_idx]` holds -- the flat tables + /// transcribed in [`cdf`], read at libaom's own COLUMN-major + /// `coeff_idx = col * 32 + row`, i.e. `table[col][row]`. r1..r3 read them + /// `[row][col]`, which is the transpose (the OTHER shape's rule, with 11 + /// and 16 swapped): it desynced the first superblock strip whose luma + /// corner held a coefficient off the first row/column. #[test] - fn nz_map_ctx_offset_tables_match_the_rect_rule() { - for (w, h, table) in [ - (32usize, 64usize, &cdf::NZ_MAP_CTX_OFFSET_32X64), - (64, 32, &cdf::NZ_MAP_CTX_OFFSET_64X32), + fn base_ctx_rect_offsets_match_the_transcribed_tables_over_the_whole_domain() { + let grid = [0i32; 32 * 32]; + for (shape, table) in [ + (Some((64usize, 32usize)), &cdf::NZ_MAP_CTX_OFFSET_64X32), + (Some((32, 64)), &cdf::NZ_MAP_CTX_OFFSET_32X64), + (None, &cdf::NZ_MAP_CTX_OFFSET_32), ] { for row in 0..5usize { for col in 0..5usize { + // An all-zero neighbourhood makes the magnitude term 0, + // so `base_ctx` returns the offset itself. + let got = base_ctx(&grid, 32, row, col, TxClass::TwoD, shape); let want = if row == 0 && col == 0 { 0 - } else if w < h && row < 2 { - 11 - } else if w > h && col < 2 { - 16 } else { - cdf::NZ_MAP_CTX_OFFSET_32[row][col] + table[col][row] as usize }; - assert_eq!( - table[col][row], - want, - "{w}x{h} nz_map offset at display (row {row}, col {col})" - ); + assert_eq!(got, want, "{shape:?} at (row {row}, col {col})"); } } } diff --git a/crates/ec-av1/src/refusal_inventory.rs b/crates/ec-av1/src/refusal_inventory.rs index 5302e7c7..c5e9b5a9 100644 --- a/crates/ec-av1/src/refusal_inventory.rs +++ b/crates/ec-av1/src/refusal_inventory.rs @@ -43,7 +43,6 @@ const REFUSALS: &[&str] = &[ "a 16x16 inter block whose true edge cuts through both axes needs a rectangular transform this decoder does not code yet", "a 32x32 partition type this decoder does not code (value={part32})", "a Golomb tail longer than this decoder reads", - "a superblock-level HORZ/VERT strip with a split transform (per-unit rect prediction is not ported)", "a tx_type symbol outside its CDF's own set: {t}", "an INTER 32x32 partition type this decoder does not code (value={part32})", "a superblock-level 1:4 partition (PARTITION_HORZ_4/VERT_4 at 64x64, four 64x16/16x64 strips)", diff --git a/crates/ec-av1/src/stream.rs b/crates/ec-av1/src/stream.rs index 5ef01ce6..df5e746b 100644 --- a/crates/ec-av1/src/stream.rs +++ b/crates/ec-av1/src/stream.rs @@ -13186,14 +13186,20 @@ mod tests { /// which is the per-unit availability rule this port exists for, and no /// gate fired one before this round. /// - /// This gate was `#[ignore]`d RED through r1 (seed 50 luma (171,56)). - /// Root cause, r2: not availability at all -- `av1_nz_map_ctx_offset` is - /// packed COLUMN-major, so [`crate::decode::base_ctx`] read the - /// `TX_32X64`/`TX_64X32` offset tables transposed and desynced the first - /// SB-level strip whose luma corner held a coefficient below row 1 (see - /// that function's comment). The strip's own per-unit prediction was - /// already exact. - #[ignore = "lane-rectsplit r3: RED at seed 50; the nz_map transposition r2 blamed turns the merged SB HORZ/VERT partition gate RED at seed 43, cause still open"] + /// This gate was `#[ignore]`d RED through r3 (seed 50 luma (171,56)). + /// Root cause, r4, TWO defects, neither of them availability -- the + /// strip's own per-unit prediction was exact all along: + /// 1. `av1_nz_map_ctx_offset` is indexed by libaom's COLUMN-major + /// `coeff_idx = col * 32 + row`, so [`crate::decode::base_ctx`] read + /// the `TX_32X64`/`TX_64X32` offset tables transposed (r2 saw this and + /// r3 reverted it, because "fixing" it turned the merged SB HORZ/VERT + /// gate RED at seed 43); + /// 2. that seed-43 RED was a SECOND defect the first one had been hiding + /// behind a refusal: `EOB_PT_512_CHROMA` was transcribed from + /// `av1_default_eob_multi512_cdfs`'s `eob_multi_ctx = 1` row (the flat + /// `V_DCT`/`H_DCT` distribution), so the first real 32x16 chroma + /// transform read `eob_pt` 7 where aomdec read 2. With both fixed, + /// seeds 43 and 50 are pixel-exact. #[test] fn a_real_aomenc_stream_with_a_split_transform_superblock_strip_decodes_pixel_exact() { const NAME: &str = "a_real_aomenc_stream_with_a_split_transform_superblock_strip_decodes_pixel_exact"; diff --git a/lanes/rectsplit-r4.report.md b/lanes/rectsplit-r4.report.md new file mode 100644 index 00000000..c9f4d376 --- /dev/null +++ b/lanes/rectsplit-r4.report.md @@ -0,0 +1,97 @@ +# lane-rectsplit r4 — SB-level split-transform strips: TWO root causes, refusal lifted + +Branch `lane-rectsplit` off `4c3389a` (r3's safe state). No rebase needed: `main` is still +`3808cf8`, the branch's merge base. + +## Headline + +r3's four-way table was not a contradiction — it was two defects stacked, the first hiding +the second behind a refusal: + +1. **`base_ctx` read the rect `av1_nz_map_ctx_offset` tables transposed.** libaom indexes + them by its own `coeff_idx`, and `get_nz_map_ctx_from_stats` decomposes that as + `col = coeff_idx >> bhl`, `bhl` = log2 of the *adjusted* height (5 for both `TX_64X32` + and `TX_32X64`): `coeff_idx = col * 32 + row`, so 32 consecutive flat entries are one + COLUMN and the `cdf.rs` 5x5 transcriptions are `[col][row]`. `decode.rs:1162` read them + `[row][col]` = the transpose = the other shape's rule with 11 and 16 swapped. + Fixed by stating libaom's generating rule (`txb_common.h:199-209`) once, exactly as + `base_ctx_rect` already did, instead of indexing the tables at all. +2. **`EOB_PT_512_CHROMA` was transcribed from the wrong row of + `av1_default_eob_multi512_cdfs[q][plane][2]`.** The last index is `eob_multi_ctx` + (0 = `TX_CLASS_2D`, 1 = `V_DCT`/`H_DCT`), not a q-context: the flat + `3277, 6554, ...` distribution our table held is the 1D-class row, identical for both + planes at every q, and the real chroma 2D row has four q-context variants like every + other coefficient table. The FIRST real 32x16 chroma transform read `eob_pt` 7 where + aomdec read 2 — that is what made seed 43 RED once defect 1 was fixed. Under r3's + shipped code seed 43 never reached it: it desynced 13 elements EARLIER and exited + through a named refusal, which the gate counts as a pass (class `refusal-hides-a-defect`). + +## Changed + +- `crates/ec-av1/src/decode.rs:1162` `base_ctx` — the `TX_CLASS_2D` rect arm now applies + libaom's generating rule (`row < 2 -> 11` when w 16` when w>h, else the + square table), no transposable table read left. +- `crates/ec-av1/src/cdf.rs:269` — `EOB_PT_512_CHROMA` corrected to the 2D chroma row plus + new `_Q0`/`_Q1`/`_Q3` siblings; `cdf_state.rs:1015` now `pick()`s by q-context. +- `crates/ec-av1/src/decode.rs:4102` `decode_block_rect64` — the split-transform refusal is + LIFTED, wired to `decode_rect_split` (r1's per-unit port), `RECT_PARTITION_HITS` bumped. +- `crates/ec-av1/src/decode.rs:3428` `decode_rect_split` — records `RECT64_QIDX_DRIFT_HITS` + for 64-level strips, which now reach this path. +- `crates/ec-av1/src/refusal_inventory.rs:49` — refusal string removed. +- `crates/ec-av1/src/stream.rs:9778` — gate (b) un-`#[ignore]`d, its doc records both causes. +- Instruments kept: `read_coeffs`/`read_coeffs_rect` print an `EC_COEFF_STEP` ladder under + `EC_TRACE_COEFF` in instrumented-aomdec format, with `pos` converted to libaom's + column-major `coeff_idx` so the two ladders `diff` line by line. +- New unit test `decode::tests::base_ctx_rect_offsets_match_the_transcribed_tables_over_the_whole_domain` + — evaluates `base_ctx` over the WHOLE 5x5 domain of 64x32 / 32x64 / square against + `table[col][row]` (class `enumerate-table-domain`). + +## Refusals lifted + +- `"a superblock-level HORZ/VERT strip with a split transform (per-unit rect prediction is not ported)"` + — with `a_real_aomenc_stream_with_a_split_transform_superblock_strip_decodes_pixel_exact` + green, hard-asserting `rect_split_sb_interior_tu_hits` (SB-level AND tx depth >= 2) sampled + per compared attempt. + +## Gates and evidence + +EVIDENCE: msac range ladders `scratchpad/n_ours43.txt` vs `scratchpad/n_aom43.txt` | `EC_TRACE_COEFF=1 decode_probe seed43.obu` vs `EC_TRACE_COEFF=1 aomdec` on the gate-dumped seed-43 stream | first divergent element = the chroma `eob` of the 64x32 strip at mi (0,16): ours `eob_pt` group 8 (eob 103), aomdec group 3 (eob 3), at identical range 44680 — i.e. a CDF-content defect, not a context one +EVIDENCE: `scratchpad/r4_ours43.yuv` vs `scratchpad/r4_aom43.yuv` | `decode_probe seed43.obu out.yuv`; `aomdec --rawvideo` | `cmp` byte-identical (was: frame-0 luma mismatch) +EVIDENCE: `EC_AV1_REQUIRE_AOMENC=1 cargo test -p ec-av1 --lib -j3 -- --test-threads=1 --include-ignored superblock_level_horz_vert_partition split_transform_horz_vert_strip filter_intra_on_a_horz split_transform_superblock_strip refusal_inventory gate_coverage nz_map` | gates (a) (b) (c) + merged SB HORZ/VERT + inventory/coverage/table tests | 10 passed, 1 failed (81.41s) — the failure is the already-`#[ignore]`d `..._and_delta_q_...` gate, which fails its own drift counter, not a pixel compare (27/40 pixel-exact, 13 named refusals) +EVIDENCE: third leg from lane-tx64x16 — `scratchpad` sweep seeds 42..81, 192x128 gradients, merged-gate recipe with `--min-partition-size=16 --enable-1to4-partitions=1` | encode with the oracle aomenc, `decode_probe` vs `aomdec --rawvideo`, `cmp` | 33 EXACT, 7 named refusals (6x SB-level AB partition, 1x screen-content strip), 0 mismatches — seed 55 (reported 3944 wrong luma samples, first at (154,59)) is now byte-exact +EVIDENCE: full `EC_AV1_REQUIRE_AOMENC=1 cargo test -p ec-av1 --lib -j3` | whole crate lib | **272 passed, 0 failed, 23 ignored**, 490.90s (r3's expectation was 270/0/24; +1 un-ignored gate, +1 new unit test) + +## Film check (ReelB, the lane's critical path) + +`decode_probe` on the coordinator's mid-film extract (`ffmpeg -ss 600 -t 0.5 ... -f obu`, +11.5 MB, 10-bit 2160p, `.../cdc46329-.../scratchpad/reelb5.obu`), which stopped at exactly this +lane's refusal on main: + +``` +before (main 06d856d): REFUSED: a superblock-level HORZ/VERT strip with a split transform ... +after (this branch): REFUSED: AV1 tile (a 32x32 partition type this decoder does not code (value=8)) +``` + +EVIDENCE: `.../cdc46329-.../scratchpad/reelb5.obu` | `cargo run -p ec-av1 --example decode_probe` on `lane-rectsplit` r4 | the film advances past this lane's refusal to `PARTITION_HORZ_4` (value 8), a 1:4-partition gap owned by lane-part32/tx64x16 + +## Sibling sweep (defect class) + +- Class `table-transcribed-from-the-wrong-row-of-a-multi-dimensional-default`: every other + coefficient CDF in `Cdfs::new` already goes through `pick(q_ctx, ...)`; a grep of + `cdf.rs` for libaom's flat 1D rows (`3277, 6554` / `2979, 5958` / ...) finds no second + instance. `EOB_PT_512_CHROMA` was the only coefficient table initialised without `pick`. +- Class `reference-layout-not-spec` (transposed table): the only other rect offset consumer, + `base_ctx_rect`, already stated the rule and is unchanged; the new domain test covers both. + +## Residue + +- deferred(lane-part32 / lane-tx64x16 — 1:4 partitions): both films and 7/40 seeds of the + third-leg sweep now stop at `PARTITION_HORZ_4/VERT_4` or SB-level AB partitions. +- deferred(lane-rect64q): `a_real_aomenc_stream_with_a_superblock_level_horz_vert_partition_and_delta_q_decodes_pixel_exact` + stays `#[ignore]`d as r3 left it — its 40 attempts pixel-match (27 compared, 13 named + refusals) but `deltaq-mode=1` never drifts `CURRENT_Q_IDX` inside a 64-level strip in + that recipe. r4 added the missing counter bump on the split path; the gate still needs a + recipe that actually drifts. +- accepted: the third-leg recipe (`--min-partition-size=16 --enable-1to4-partitions=1`) is + measured here but NOT added as a gate arm — its remaining refusals are 1:4/AB partitions + owned by other lanes, so the arm would be RED for reasons outside this lane.