From 5ec09409a507fe40c3c4e1aa432b56d012167eb8 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:40:09 +0300 Subject: [PATCH] fix(av1): eob_pt CDF kept no TX_CLASS_HORIZ/VERT row above 8x8 -- a V_DCT 16x16 var-tx leaf read the 2D table and desynced libaom picks eob_multi_ctx = (tx_class == TX_CLASS_2D) ? 0 : 1 at EVERY eob_pt table size (decodetxb.c:200); this crate carried the class-1 sibling only for 4x4 and 8x8 luma. TxMode::Select made the hole reachable: a 32x32 inter block's var-tx leaf is a 16x16 luma TU, whose inter tx set EXT_TX_SET_DTT9_IDTX_1DDCT is the only 16x16 set holding V_DCT/H_DCT. Adds eob_pt_256_luma_class1 and the three chroma siblings (an inter block's chroma TU inherits its luma unit's tx_type, so 1D classes reach chroma too), their defaults from av1_default_eob_multi{256,64,16}_cdfs[q][plane][1], and their per-frame counter resets. 32-point and larger need none (DCT_IDTX / DCTONLY hold no 1D type). Found by msac range ladder vs instrumented aomdec on r1's failing seed-43 stream: first divergent element is the eob of the 16x16 leaf at mi (4,8), ref eob=248 rng=65032 vs ours eob=247 rng=56584 with identical all_zero and tx_type ranges. All four frames of that stream are now pre-filter bit-exact. Gate a_real_aomenc_inter_sequence_with_tx_select_decodes_pixel_exact is green and gains a 10-bit arm (-b 10 --input-bit-depth=10); suite 269 passed / 0 failed / 23 ignored. --- crates/ec-av1/src/cdf.rs | 41 ++++++++++++ crates/ec-av1/src/cdf_state.rs | 55 ++++++++++++++-- crates/ec-av1/src/stream.rs | 40 ++++++++++-- lanes/txselect-r2.report.md | 114 +++++++++++++++++++++++++++++++++ 4 files changed, 239 insertions(+), 11 deletions(-) create mode 100644 lanes/txselect-r2.report.md diff --git a/crates/ec-av1/src/cdf.rs b/crates/ec-av1/src/cdf.rs index da3ad71b..a14bdb37 100644 --- a/crates/ec-av1/src/cdf.rs +++ b/crates/ec-av1/src/cdf.rs @@ -5032,3 +5032,44 @@ pub const INTRA_TX_TYPE_SET1_4: [[u16; 8]; 13] = [ [277, 4369, 5255, 8905, 16465, 22271, 32768, 0], [3409, 5436, 10599, 15599, 19687, 24040, 32768, 0], ]; + +/// The `TX_CLASS_HORIZ`/`TX_CLASS_VERT` siblings of [`EOB_PT_256_LUMA`], +/// [`EOB_PT_256_CHROMA`], [`EOB_PT_64_CHROMA`] and [`EOB_PT_16_CHROMA`] +/// (`av1_default_eob_multi{256,64,16}_cdfs[q][plane][1]`, `token_cdfs.h`) -- +/// lane-txselect r2: the class dimension libaom keeps on *every* eob_pt +/// table. Only 4x4/8x8 luma carried it here, so the first `V_DCT`/`H_DCT` +/// 16x16 luma TU (a var-tx leaf of a 32x32 inter block, `EXT_TX_SET_ +/// DTT9_IDTX_1DDCT`) read the 2D table and desynced at its `eob`. 32-point +/// and larger transforms need no sibling: their tx sets (`DCT_IDTX`, +/// `DCTONLY`) hold no 1D type. +pub const EOB_PT_256_LUMA_CLASS1_Q0: [u16; 10] = + [998, 1850, 2998, 5604, 17341, 19888, 22899, 25583, 32768, 0]; +pub const EOB_PT_256_LUMA_CLASS1_Q1: [u16; 10] = + [399, 1019, 1749, 3038, 10444, 15546, 22739, 27294, 32768, 0]; +pub const EOB_PT_256_LUMA_CLASS1: [u16; 10] = + [1084, 2358, 3488, 5122, 11483, 18103, 26023, 29799, 32768, 0]; +pub const EOB_PT_256_LUMA_CLASS1_Q3: [u16; 10] = + [2453, 4474, 6307, 8777, 16474, 22975, 29000, 31547, 32768, 0]; + +pub const EOB_PT_256_CHROMA_CLASS1_Q0: [u16; 10] = + [2203, 4130, 7435, 10739, 20652, 23681, 25609, 27261, 32768, 0]; +pub const EOB_PT_256_CHROMA_CLASS1_Q1: [u16; 10] = + [1674, 3252, 5734, 10159, 22397, 23802, 24821, 30940, 32768, 0]; +pub const EOB_PT_256_CHROMA_CLASS1: [u16; 10] = + [6571, 9610, 15516, 21826, 29092, 30829, 31842, 32708, 32768, 0]; +pub const EOB_PT_256_CHROMA_CLASS1_Q3: [u16; 10] = + [9998, 17661, 25178, 28097, 31308, 32038, 32403, 32695, 32768, 0]; + +pub const EOB_PT_64_CHROMA_CLASS1_Q0: [u16; 8] = + [1563, 2700, 4876, 10911, 14706, 22480, 32768, 0]; +pub const EOB_PT_64_CHROMA_CLASS1_Q1: [u16; 8] = + [1923, 3127, 5867, 9703, 14277, 27100, 32768, 0]; +pub const EOB_PT_64_CHROMA_CLASS1: [u16; 8] = + [4034, 6290, 10235, 14982, 21214, 28491, 32768, 0]; +pub const EOB_PT_64_CHROMA_CLASS1_Q3: [u16; 8] = + [8726, 12378, 19409, 26450, 30038, 32462, 32768, 0]; + +pub const EOB_PT_16_CHROMA_CLASS1_Q0: [u16; 6] = [1904, 3354, 7763, 14647, 32768, 0]; +pub const EOB_PT_16_CHROMA_CLASS1_Q1: [u16; 6] = [2497, 4096, 8866, 16993, 32768, 0]; +pub const EOB_PT_16_CHROMA_CLASS1: [u16; 6] = [3192, 5032, 10297, 19755, 32768, 0]; +pub const EOB_PT_16_CHROMA_CLASS1_Q3: [u16; 6] = [7297, 10767, 19273, 28194, 32768, 0]; diff --git a/crates/ec-av1/src/cdf_state.rs b/crates/ec-av1/src/cdf_state.rs index 0a79d714..eaf09f96 100644 --- a/crates/ec-av1/src/cdf_state.rs +++ b/crates/ec-av1/src/cdf_state.rs @@ -214,6 +214,17 @@ pub(crate) struct Cdfs { pub eob_pt_16_luma_class1: [u16; 6], /// `eob_pt_64_luma`'s class-1 sibling (r5), see `eob_pt_16_luma_class1`. pub eob_pt_64_luma_class1: [u16; 8], + /// `eob_pt_256_luma`'s class-1 sibling (lane-txselect r2): a 16x16 luma + /// TU reaches `V_DCT`/`H_DCT` through the inter tx set + /// `EXT_TX_SET_DTT9_IDTX_1DDCT`, and libaom keeps the eob class + /// dimension at every size. + pub eob_pt_256_luma_class1: [u16; 10], + /// The chroma class-1 siblings (lane-txselect r2): an *inter* block's + /// chroma TU inherits its luma unit's `tx_type`, so 1D classes reach + /// chroma too. + pub eob_pt_256_chroma_class1: [u16; 10], + pub eob_pt_64_chroma_class1: [u16; 8], + pub eob_pt_16_chroma_class1: [u16; 6], /// The end-of-block offset bit of a 16x16 luma transform. pub eob_extra_luma_16: [[u16; 3]; 9], /// The same, for an 8x8 luma transform. @@ -612,6 +623,10 @@ impl Cdfs { reset1(&mut self.eob_pt_16_luma); reset1(&mut self.eob_pt_16_luma_class1); reset1(&mut self.eob_pt_64_luma_class1); + reset1(&mut self.eob_pt_256_luma_class1); + reset1(&mut self.eob_pt_256_chroma_class1); + reset1(&mut self.eob_pt_64_chroma_class1); + reset1(&mut self.eob_pt_16_chroma_class1); reset2(&mut self.eob_extra_luma_16); reset2(&mut self.eob_extra_luma_8); reset2(&mut self.eob_extra_luma_4); @@ -827,6 +842,34 @@ impl Cdfs { cdf::EOB_PT_64_LUMA_CLASS1, cdf::EOB_PT_64_LUMA_CLASS1_Q3, ), + eob_pt_256_luma_class1: pick( + q_ctx, + cdf::EOB_PT_256_LUMA_CLASS1_Q0, + cdf::EOB_PT_256_LUMA_CLASS1_Q1, + cdf::EOB_PT_256_LUMA_CLASS1, + cdf::EOB_PT_256_LUMA_CLASS1_Q3, + ), + eob_pt_256_chroma_class1: pick( + q_ctx, + cdf::EOB_PT_256_CHROMA_CLASS1_Q0, + cdf::EOB_PT_256_CHROMA_CLASS1_Q1, + cdf::EOB_PT_256_CHROMA_CLASS1, + cdf::EOB_PT_256_CHROMA_CLASS1_Q3, + ), + eob_pt_64_chroma_class1: pick( + q_ctx, + cdf::EOB_PT_64_CHROMA_CLASS1_Q0, + cdf::EOB_PT_64_CHROMA_CLASS1_Q1, + cdf::EOB_PT_64_CHROMA_CLASS1, + cdf::EOB_PT_64_CHROMA_CLASS1_Q3, + ), + eob_pt_16_chroma_class1: pick( + q_ctx, + cdf::EOB_PT_16_CHROMA_CLASS1_Q0, + cdf::EOB_PT_16_CHROMA_CLASS1_Q1, + cdf::EOB_PT_16_CHROMA_CLASS1, + cdf::EOB_PT_16_CHROMA_CLASS1_Q3, + ), eob_extra_luma_16: pick( q_ctx, cdf::EOB_EXTRA_LUMA_16_Q0, @@ -1264,7 +1307,7 @@ impl Cdfs { br: &mut self.br_luma_16, dc_sign: &mut self.dc_sign_luma, tx_type: Some(self.intra_tx_type_16[mode].as_mut_slice()), - eob_pt_class1: None, + eob_pt_class1: Some(&mut self.eob_pt_256_luma_class1), }, TxbSet::Luma16Inter => TxbTables { side: 16, @@ -1276,7 +1319,7 @@ impl Cdfs { br: &mut self.br_luma_16, dc_sign: &mut self.dc_sign_luma, tx_type: Some(self.inter_tx_type_16.as_mut_slice()), - eob_pt_class1: None, + eob_pt_class1: Some(&mut self.eob_pt_256_luma_class1), }, TxbSet::Luma16InterSet1 => TxbTables { side: 16, @@ -1288,7 +1331,7 @@ impl Cdfs { br: &mut self.br_luma_16, dc_sign: &mut self.dc_sign_luma, tx_type: Some(self.inter_tx_type_16_set2.as_mut_slice()), - eob_pt_class1: None, + eob_pt_class1: Some(&mut self.eob_pt_256_luma_class1), }, TxbSet::Luma8 => TxbTables { side: 8, @@ -1396,7 +1439,7 @@ impl Cdfs { br: &mut self.br_chroma_8, dc_sign: &mut self.dc_sign_chroma, tx_type: None, - eob_pt_class1: None, + eob_pt_class1: Some(&mut self.eob_pt_64_chroma_class1), }, TxbSet::Chroma4 => TxbTables { side: 4, @@ -1408,7 +1451,7 @@ impl Cdfs { br: &mut self.br_chroma_4, dc_sign: &mut self.dc_sign_chroma, tx_type: None, - eob_pt_class1: None, + eob_pt_class1: Some(&mut self.eob_pt_16_chroma_class1), }, TxbSet::Chroma16 => TxbTables { side: 16, @@ -1420,7 +1463,7 @@ impl Cdfs { br: &mut self.br_chroma_16, dc_sign: &mut self.dc_sign_chroma, tx_type: None, - eob_pt_class1: None, + eob_pt_class1: Some(&mut self.eob_pt_256_chroma_class1), }, TxbSet::Chroma32 => TxbTables { side: 32, diff --git a/crates/ec-av1/src/stream.rs b/crates/ec-av1/src/stream.rs index 6c78187b..a2573b9c 100644 --- a/crates/ec-av1/src/stream.rs +++ b/crates/ec-av1/src/stream.rs @@ -2366,6 +2366,19 @@ mod tests { /// FAILURE, not a SKIP -- an unfired gate is a vacuous gate. #[test] fn a_real_aomenc_inter_sequence_with_tx_select_decodes_pixel_exact() { + tx_select_inter_gate(8); + } + + /// The 10-bit arm of the gate above (lane-txselect r2): both of the + /// user's films are `yuv420p10le` and var-tx is exactly the tool they + /// use, so an 8-bit-only proof would not reach them. Same recipe plus + /// `-b 10 --input-bit-depth=10`. + #[test] + fn a_real_aomenc_inter_sequence_with_tx_select_decodes_pixel_exact_10bit() { + tx_select_inter_gate(10); + } + + fn tx_select_inter_gate(bit_depth: u32) { if !have_ffmpeg() { eprintln!( "SKIP a_real_aomenc_inter_sequence_with_tx_select_decodes_pixel_exact: no ffmpeg" @@ -2405,11 +2418,17 @@ mod tests { "-vf", "hue=s=0", "-pix_fmt", - "yuv420p", + if bit_depth == 10 { "yuv420p10le" } else { "yuv420p" }, + // y4m carries no official 10-bit tag; ffmpeg writes one + // only under `-strict -1` (a no-op at 8 bit). + "-strict", + "-1", "-t", "0.16", "-f", "yuv4mpegpipe", + "-strict", + "-1", "-", ]) .stdin(Stdio::null()) @@ -2422,7 +2441,13 @@ mod tests { "ffmpeg fixture: {}", String::from_utf8_lossy(&y4m.stderr) ); + let depth_args: &[&str] = if bit_depth == 10 { + &["-b", "10", "--input-bit-depth=10"] + } else { + &[] + }; let mut child = Command::new(aomenc_path()) + .args(depth_args) .args([ "--codec=av1", "--passes=1", @@ -2512,9 +2537,15 @@ mod tests { )); continue; } - let ffmpeg_frames = ffmpeg_decode_sequence(&stream, width, height, frame_count); + let ffmpeg_frames = if bit_depth == 10 { + ffmpeg_decode_sequence_10bit(&stream, width, height, frame_count) + } else { + ffmpeg_decode_sequence(&stream, width, height, frame_count) + }; assert_eq!(frames.len(), frame_count); - eprintln!("txselect gate seed={seed}: txfm_split reads={reads} splits taken={hits}"); + eprintln!( + "txselect gate {bit_depth}-bit seed={seed}: txfm_split reads={reads} splits taken={hits}" + ); // Name the first differing sample before the assert fires: a // 64x64 plane's `assert_eq!` output is 4096 numbers, useless for // locating a desync. @@ -2555,8 +2586,7 @@ mod tests { return; } panic!( - "a_real_aomenc_inter_sequence_with_tx_select_decodes_pixel_exact never observed a \ - taken txfm_split:\n{}", + "tx_select_inter_gate({bit_depth}) never observed a taken txfm_split:\n{}", refusals.join("\n") ); } diff --git a/lanes/txselect-r2.report.md b/lanes/txselect-r2.report.md new file mode 100644 index 00000000..e527c9dd --- /dev/null +++ b/lanes/txselect-r2.report.md @@ -0,0 +1,114 @@ +# lane-txselect r2 — the frame-2 divergence: a missing eob_pt class dimension + +Branch `lane-txselect`, on top of `fb6b74f` (r1's report commit, off main `3808cf8`). + +## Root cause + +`read_eob` picks between the 2D and the `TX_CLASS_HORIZ`/`TX_CLASS_VERT` +eob_pt CDF (`decode.rs:1384`, libaom `decodetxb.c:200` +`eob_multi_ctx = (tx_class == TX_CLASS_2D) ? 0 : 1`). Only the 4x4 and 8x8 +LUMA sets ever carried a class-1 sibling (`eob_pt_16_luma_class1`, +`eob_pt_64_luma_class1`, added by lane-av1tx4 r5); every other `TxbSet` had +`eob_pt_class1: None`, so a 1D-class TU silently read (and adapted) the +class-0 table. Instance of [[cdf-row-held-constant]] / the class dimension +half of [[table-indexed-by-raw-size]]. + +`TxMode::Select` is what made it reachable: a 32x32 inter block's var-tx +leaf is a **16x16** luma TU, whose inter tx set is +`EXT_TX_SET_DTT9_IDTX_1DDCT` — the only 16x16 set holding `V_DCT`/`H_DCT`. +(16x16 intra reads `TX_SET_INTRA_2`, all-2D; 32x32 and up read +`DCT_IDTX`/`DCTONLY`, also all-2D — so 16x16 inter is exactly the hole.) + +Sweep of the same shape: every reachable size now carries its sibling — +`eob_pt_256_luma_class1` plus the three chroma ones +(`eob_pt_256/64/16_chroma_class1`; an *inter* block's chroma TU inherits its +luma unit's `tx_type`, so 1D classes reach chroma too). 32-point and larger +need none, by their tx sets. + +## What changed + +- `crates/ec-av1/src/cdf.rs` (EOF): `EOB_PT_256_LUMA_CLASS1*`, + `EOB_PT_256_CHROMA_CLASS1*`, `EOB_PT_64_CHROMA_CLASS1*`, + `EOB_PT_16_CHROMA_CLASS1*` — `av1_default_eob_multi{256,64,16}_cdfs[q][plane][1]`, + `token_cdfs.h:773/811/851`, all four q-contexts. +- `crates/ec-av1/src/cdf_state.rs`: the four fields, their `pick(q_ctx, …)` + defaults, their lines in the per-frame counter reset (class + [[cdf-counter-not-reset]]), and `eob_pt_class1: Some(…)` on + `Luma16`/`Luma16Inter`/`Luma16InterSet1`/`Chroma16`/`Chroma8`/`Chroma4`. +- `crates/ec-av1/src/stream.rs`: the gate body becomes + `tx_select_inter_gate(bit_depth)` with two `#[test]` arms — the existing + 8-bit one and a new `…_decodes_pixel_exact_10bit` (`-b 10 + --input-bit-depth=10`, `yuv420p10le` fixture under `-strict -1`, compared + through `ffmpeg_decode_sequence_10bit`). + +No refusal string changed this round (r1 lifted the blanket +`TxMode::Select` one; it is now actually proven). + +## Method (what found it, in order) + +1. Pinned the r1 failing stream: seed-43 recipe → `s43.obu`, + sha256 `d0ba79bbdd34317fa3864c824026e0b905dea0d9add540e646281a1ae23e3d57`. +2. `EC_AV1_PREFILT_DUMP` ours vs instrumented aomdec: frame 2 already + differed BEFORE the loop filter (1721 luma samples, chroma clean) — so + not the deblock tx grid, which was r1's leading suspect. +3. `EC_TRACE_MODE_STEP` matched everywhere (it only instruments the intra + path); `EC_TRACE_COEFF` on both, compared by msac RANGE: the first + divergent element is the **`eob` of the 16x16 luma var-tx leaf at + mi (4,8)** of the 32x32 block at mi (0,8) — entry `rng=42600` + (`all_zero`) and `rng=33264` (`tx_type`) identical, then ref + `eob=248 rng=65032` vs ours `eob=247 rng=56584`. +4. `EC_EOBPT_DUMP` on aomdec gave `tx_size=2 eob_multi_size=4 txs_ctx=2`; + ours read `tx_type value=1 len=13` = `V_DCT` (class VERT) yet took the + class-0 CDF. `eob_pt_class1: None` on `Luma16InterSet1` was the line. + +## Gate — GREEN, both arms + +``` +CARGO_TARGET_DIR=$HOME/.cache/cargo-target-txselect EC_AV1_REQUIRE_AOMENC=1 \ + cargo test -p ec-av1 --lib a_real_aomenc_inter_sequence_with_tx_select -- --nocapture +``` + +EVIDENCE: cargo test stderr | 4-frame 64x64 mandelbrot inter sequence, aomenc +default tx-mode (`--enable-tx-size-search` deliberately absent), decode_stream +vs ffmpeg per frame, 8-bit and 10-bit arms | `txselect gate 8-bit seed=42: +txfm_split reads=22 splits taken=3`, `txselect gate 10-bit seed=42: +txfm_split reads=25 splits taken=6`, `test result: ok. 2 passed; 0 failed`. + +EVIDENCE: /tmp/…/scratchpad/{ref,ours}.f0..f3 | `EC_AV1_PREFILT_DUMP` on +instrumented aomdec and on `decode_probe s43.obu` (r1's own failing seed-43 +stream) | `cmp -l` 0 differing bytes on all four frames' Y+U+V pre-filter +planes (was 0 / 0 / 1721 / 3647 before the fix). + +10-bit note: the 10-bit arm passes, so this lane owes lane-hbdinter nothing. + +## Suite + +`EC_AV1_REQUIRE_AOMENC=1 cargo test -p ec-av1 --lib` (full, this branch): +**269 passed / 0 failed / 23 ignored**, 877.86s. The earlier run of the same +command before the 10-bit arm was added was +**268 passed / 0 failed / 23 ignored** (r1: 267/1/23, the one failure being +this lane's gate). + +## Film probe (0.4 s, `-c:v copy -f obu`) + +- ReelC: `REFUSED: unsupported: AV1 tile (a 32x32 partition type this decoder + does not code (value=4))` +- ReelB: `REFUSED: unsupported: AV1 tile (a partition below 8x8 (this + decoder codes no leaf smaller than 8x8))` + +Both are other lanes' refusals, reached in the first tile's partition tree — +strictly upstream of anything this round touched, so the "before" value is +identical by construction (not separately rebuilt: `deferred(nothing)` — +measuring it would mean a second full build of `fb6b74f` for a string that +cannot move). + +## Residue + +- accepted (documented corner-cut, unchanged from r1, `decode.rs:11447`): a + var-tx block's **chroma** deblock edges follow `tx_h_grid / 2`, i.e. the + luma leaves, though the block's uv transform is not split. Ceiling: a + chroma deblock edge at a luma-only transform seam. Not observable in this + gate (`--enable-cdef=0`, and both arms are chroma-exact through 4 frames); + upgrade path = a per-plane tx grid, not a scalar. +- accepted: the two narrow refusals r1 added stay (a var-tx leaf >32x32; an + intra block in an inter frame whose `tx_depth` splits).