From 555df44328181b04f53dde8f73daca15423dd764 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 08:19:47 +0300 Subject: [PATCH] fix(av1): an inter block's chroma tx_type is the colocated luma type NARROWED to the chroma size's ext-tx set, not DCT_DCT at every side >= 32 av1_get_tx_type (libaom av1/common/blockd.h:1278-1309) resolves an inter block's chroma transform type in three steps: DCT_DCT only when txsize_sqr_up_map[tx_size] is STRICTLY greater than TX_32X32; otherwise the colocated luma unit's coded type from xd->tx_type_map; then narrowed by av1_ext_tx_used[av1_get_ext_tx_set_type(uv_tx, is_inter, reduced)] (blockd.h:1036 / 1097). This decoder had the first step as "chroma side >= 32 -> DCT_DCT" and no third step at all. At exactly TX_32X32 an inter block reads EXT_TX_SET_DCT_IDTX, which contains IDTX -- so a 64x64 inter superblock whose first var-tx luma leaf coded IDTX must inverse-transform its 32x32 chroma units with IDTX, and we used DCT_DCT. IDTX and DCT_DCT are both TX_CLASS_2D, so the scan, the coefficient contexts and every msac range stayed identical: the stream never desynced, luma stayed bit-exact, and one 64x64 superblock of chroma came out as high-frequency noise. The missing third step was also wrong the other way for a 16x16 chroma unit under 8x8 luma leaves (EXT_TX_SET_DTT9_IDTX_1DDCT excludes the four 1D ADST types). New reduce_inherited_chroma_tx_type covers all four inter sets and both inter-chroma callers (read_inter_plane, read_inter_plane_rect) -- the only two sites in the crate that derive a chroma tx_type for an inter block. Gate a_real_aomenc_stream_with_a_32x32_frame_edge_rect_partition goes "192x80 cq35 frames=5 10bit=false frame 3 plane U: 978 pixels differ" -> ok, 38 pixel-exact attempts over 5 arms x 8 cq levels, 8-bit and 10-bit. Plus a unit test enumerating the set-membership table against libaom's. --- crates/ec-av1/src/decode.rs | 84 ++++++++++++++++++++++++++++---- crates/ec-av1/src/stream.rs | 13 +++++ lanes/golomb-r7.report.md | 96 +++++++++++++++++++++++++++++++++++++ 3 files changed, 185 insertions(+), 8 deletions(-) create mode 100644 lanes/golomb-r7.report.md diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index 1ac35d46..83e7408f 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -11750,8 +11750,8 @@ fn read_inter_plane_rect( // `av1_get_ext_tx_set_type`'s DCT-only clamp is on `tx_size_sqr_up`, i.e. // the *longer* side of this rect transform. let default_tx_type = match inherited_luma_tx_type { - Some(t) if w.max(h) < 32 => t, - _ => TxType::DctDct, + Some(t) => reduce_inherited_chroma_tx_type(t, w, h), + None => TxType::DctDct, }; // lane-r14 r2: a 64-length axis codes only its low 32 coefficients // (`av1_get_max_eob`), so the coded unit is the `min(w,32) x min(h,32)` @@ -11918,6 +11918,45 @@ fn read_inter_luma8( /// The [`TxbSet`] one leaf of an inter block's var-tx tree reads its /// coefficient tables from -- the inter counterpart of [`txbset_for`]. +/// `av1_get_tx_type`'s chroma tail (libaom `av1/common/blockd.h:1291-1305`): +/// an inter block's chroma transform inherits the *colocated luma* unit's +/// coded `tx_type`, then drops back to `DCT_DCT` unless that type is a member +/// of the extended set the CHROMA transform size selects +/// (`av1_ext_tx_used[av1_get_ext_tx_set_type(uv_tx, 1, reduced)][tx_type]`, +/// `blockd.h:1036`/`1097`). lane-golomb r7: we used to force `DCT_DCT` at +/// every chroma side >= 32, but `av1_get_ext_tx_set_type`'s DCT-only rung is +/// `tx_size_sqr_up > TX_32X32`; at exactly TX_32X32 an inter block reads +/// `EXT_TX_SET_DCT_IDTX`, so a 64x64 inter block whose first luma leaf coded +/// `IDTX` inherits IDTX on its 32x32 chroma units. `IDTX` and `DCT_DCT` are +/// both `TX_CLASS_2D`, so that mistake left the entropy stream perfectly in +/// sync and only corrupted the inverse transform (one 64x64 superblock of +/// high-frequency chroma noise, luma bit-exact). +fn reduce_inherited_chroma_tx_type(t: TxType, w: usize, h: usize) -> TxType { + use TxType::*; + let sqr_up = w.max(h); + let sqr = w.min(h); + let reduced = REDUCED_TX_SET_INTER.with(std::cell::Cell::get); + // `av1_get_ext_tx_set_type(tx_size, is_inter = 1, reduced)`. + let allowed: &[TxType] = if sqr_up > 32 { + // EXT_TX_SET_DCTONLY + &[DctDct] + } else if sqr_up == 32 || reduced { + // EXT_TX_SET_DCT_IDTX + &[DctDct, Idtx] + } else if sqr == 16 { + // EXT_TX_SET_DTT9_IDTX_1DDCT -- TX_TYPES 0..=11, i.e. everything but + // the four 1D ADST/FLIPADST types. + &[ + DctDct, AdstDct, DctAdst, AdstAdst, FlipAdstDct, DctFlipAdst, FlipAdstFlipAdst, + AdstFlipAdst, FlipAdstAdst, Idtx, VDct, HDct, + ] + } else { + // EXT_TX_SET_ALL16 + return t; + }; + if allowed.contains(&t) { t } else { DctDct } +} + fn inter_txbset_for(tx_px: usize, reduced_tx_set: bool) -> TxbSet { match tx_px { 32 => TxbSet::Luma32Inter, @@ -14972,10 +15011,9 @@ fn read_inter_plane( // `tx_type` -- `av1_get_tx_type` (libaom `blockd.h`): an inter block's // chroma plane never codes its own `tx_type` symbol, but (unlike an // intra block, which falls back to `Intra_Mode_To_Tx_Type`) it inherits - // the colocated luma transform's coded type verbatim, clamped back to - // `DCT_DCT` at the chroma tx sizes the spec forces DCT-only - // (`av1_get_ext_tx_set_type`: `tx_size_sqr_up >= TX_32X32`, i.e. this - // function's own `side >= 32`). `None` for the luma call itself + // the colocated luma transform's coded type, narrowed to the extended + // set the CHROMA size selects ([`reduce_inherited_chroma_tx_type`]). + // `None` for the luma call itself // (`plane_idx == 0`), where the `TxbSet`'s own symbol (or the true // `DCT_DCT` default at 32-point+) already resolves it without help. inherited_luma_tx_type: Option, @@ -14993,8 +15031,8 @@ fn read_inter_plane( }; let mut coding = cdfs.txb(set, tx_mode); let default_tx_type = match inherited_luma_tx_type { - Some(t) if side < 32 => t, - _ => TxType::DctDct, + Some(t) => reduce_inherited_chroma_tx_type(t, side, side), + None => TxType::DctDct, }; let (grid, tx_type) = read_coeffs( dec, @@ -22472,6 +22510,36 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( #[cfg(test)] mod tests { + /// lane-golomb r7: [`reduce_inherited_chroma_tx_type`] must reproduce + /// `av1_ext_tx_used[av1_get_ext_tx_set_type(uv_tx, 1, reduced)]` + /// (libaom `av1/common/blockd.h:1036`/`1097`) exactly -- the g35 residue + /// was one 64x64 inter superblock whose 32x32 chroma units are + /// `EXT_TX_SET_DCT_IDTX`, where IDTX survives and we forced `DCT_DCT`. + #[test] + fn an_inter_chroma_transform_narrows_its_inherited_tx_type_to_its_own_set() { + use TxType::*; + // EXT_TX_SET_DCTONLY: `tx_size_sqr_up > TX_32X32`. + set_inter_tx_mode(true, false, true); + assert_eq!(reduce_inherited_chroma_tx_type(Idtx, 64, 64), DctDct); + // EXT_TX_SET_DCT_IDTX at exactly TX_32X32 -- the residue's case. + assert_eq!(reduce_inherited_chroma_tx_type(Idtx, 32, 32), Idtx); + assert_eq!(reduce_inherited_chroma_tx_type(AdstAdst, 32, 32), DctDct); + // EXT_TX_SET_DTT9_IDTX_1DDCT at `tx_size_sqr == TX_16X16`: the four + // 1D ADST/FLIPADST types (TX_TYPES 12..15) drop out, the rest stay. + assert_eq!(reduce_inherited_chroma_tx_type(HDct, 16, 16), HDct); + assert_eq!(reduce_inherited_chroma_tx_type(FlipAdstAdst, 16, 16), FlipAdstAdst); + assert_eq!(reduce_inherited_chroma_tx_type(HAdst, 16, 16), DctDct); + // EXT_TX_SET_ALL16 below that (`tx_size_sqr < TX_16X16`, so a 16x8 + // chroma unit too). + assert_eq!(reduce_inherited_chroma_tx_type(HAdst, 16, 8), HAdst); + assert_eq!(reduce_inherited_chroma_tx_type(VFlipAdst, 8, 8), VFlipAdst); + // `reduced_tx_set` collapses every inter set to DCT_IDTX. + set_inter_tx_mode(true, true, true); + assert_eq!(reduce_inherited_chroma_tx_type(Idtx, 8, 8), Idtx); + assert_eq!(reduce_inherited_chroma_tx_type(HAdst, 8, 8), DctDct); + set_inter_tx_mode(true, false, true); + } + /// lane-fistrip r1 (class `enumerate-the-table-domain`): every block /// shape this decoder can hand [`filter_intra_size_class_rect`], against /// `av1_filter_intra_allowed_bsize` (`block_size_wide <= 32 && diff --git a/crates/ec-av1/src/stream.rs b/crates/ec-av1/src/stream.rs index 241a3b89..db159fee 100644 --- a/crates/ec-av1/src/stream.rs +++ b/crates/ec-av1/src/stream.rs @@ -18439,6 +18439,19 @@ mod tests { // The inter arm: >= 4 frames so the 32-level edge path in the INTER // tile walk is exercised too. arms.push((192, 80, 5, false)); + // lane-golomb r7 MEASURED, arm NOT added (it would be vacuous or red): + // a 68-px axis leaves the last superblock straddling by 4 px, the + // shape `read_var_tx_size`'s `blk_row >= max_h_mi || blk_col >= + // max_w_mi` early return exists for. With + // `arms.push((192, 68, 5, false))` all EIGHT cq attempts refuse by + // name -- "a reference picture whose height does not match this + // frame's own true size" -- so the arm compares nothing (class + // `counter-from-refused-stream`). With + // `arms.push((68, 192, 5, false))` the gate goes RED on a real, + // separate defect: `68x192 cq35 frames=5 10bit=false frame 1 plane Y: + // 141 pixels differ, first at row 0 col 64 (ours 167 vs ffmpeg 166) + // [edge32=[0,34,0,0,1,17,0,1]]` -- LUMA, inside the 4-px straddling + // COLUMN. Both are unrelated to this round's chroma tx_type fix. let cqs: [u32; 8] = [35, 40, 45, 50, 55, 57, 59, 61]; let mut totals = [0usize; 8]; let mut compared = 0usize; diff --git a/lanes/golomb-r7.report.md b/lanes/golomb-r7.report.md new file mode 100644 index 00000000..802cd183 --- /dev/null +++ b/lanes/golomb-r7.report.md @@ -0,0 +1,96 @@ +# lane-golomb r7 REPORT -- GREEN (the r6 chroma residue is closed at its root cause) + +## What changed +* `crates/ec-av1/src/decode.rs` -- new `fn reduce_inherited_chroma_tx_type(t, w, h)` + (inserted just above `fn inter_txbset_for`), and both inter-chroma callers now use it: + * `read_inter_plane_rect`'s `default_tx_type` (was `Some(t) if w.max(h) < 32 => t`) + * `read_inter_plane`'s `default_tx_type` (was `Some(t) if side < 32 => t`) + Those two are the ONLY sites in the crate that derive a chroma `tx_type` for an INTER + block (`rg inherited_luma_tx_type`); the intra chroma sites (`decode.rs:6056`, `9195`) + take `default_intra_tx_type` and are already correct -- every value that table can + produce is a member of both intra sets below 32, and both already force `DCT_DCT` at + `>= 32`, which for INTRA is right (`av1_get_ext_tx_set_type` returns `EXT_TX_SET_DCTONLY` + for intra at `tx_size_sqr_up == TX_32X32`). +* `crates/ec-av1/src/decode.rs` -- new unit test + `decode::tests::an_inter_chroma_transform_narrows_its_inherited_tx_type_to_its_own_set`. +* `crates/ec-av1/src/stream.rs` -- comment only: the measured reason the 192x68 / 68x192 + straddling arms are NOT in `edge32_gate` (below). + +## ROOT CAUSE (libaom citation) +`av1_get_tx_type` (`av1/common/blockd.h:1278-1309`) for a chroma plane of an INTER block: +1. `if (txsize_sqr_up_map[tx_size] > TX_32X32) return DCT_DCT;` -- **strictly greater**; +2. `tx_type = xd->tx_type_map[(blk_row << ss_y) * stride + (blk_col << ss_x)]` -- the + colocated luma unit's coded type (for a chroma unit at (0,0), the FIRST var-tx leaf); +3. `if (!av1_ext_tx_used[av1_get_ext_tx_set_type(tx_size, 1, reduced)][tx_type]) + tx_type = DCT_DCT;` -- narrowed by the set the **chroma** size selects + (`av1_ext_tx_used` `blockd.h:1036`, `av1_get_ext_tx_set_type` `blockd.h:1097`). + +We had step 1 as `chroma side >= 32 -> DCT_DCT` and no step 3 at all. At exactly +`TX_32X32` an inter block reads `EXT_TX_SET_DCT_IDTX`, which **contains IDTX**. So a +64x64 inter superblock, whose chroma units are 32x32 (`av1_get_max_uv_txsize(BLOCK_64X64)`), +whose first luma var-tx leaf coded `IDTX`, must inverse-transform its chroma with IDTX -- +we used DCT_DCT. + +`IDTX` and `DCT_DCT` are both `TX_CLASS_2D`: same scan, same coefficient contexts, same +msac ranges. That is why the whole stream stayed in entropy sync and only one superblock +of chroma came out as high-frequency noise with luma bit-exact -- the r6 handoff's exact +signature. (Class for the ledger: a tx_type set-membership bug is invisible to every +range ladder; only pixels see it.) + +The old rule was also wrong the other way for chroma 16x16 under 8x8 luma leaves +(`EXT_TX_SET_DTT9_IDTX_1DDCT` excludes V_ADST/H_ADST/V_FLIPADST/H_FLIPADST, which we would +have inherited verbatim). The new helper covers all four sets. + +## Localization (how the tx_type was identified, no aomdec rebuild) +`EC_TRACE_MODE=1 EC_TRACE_COEFF=1` on `g35.obu` (md5 `9037f5b21db95d35e71f91f040fc33e1`, +`scratchpad/gl-gen.sh 35`), split into frames on `EC_MODE mi_row=0 mi_col=0`: decode-order +picture 4 is segment 4. Its block at `mi(0,16)` (luma x=64,y=0) runs to the next +`EC_MODE mi_row=0 mi_col=32`, i.e. it is a **64x64** block (not 32x32 as r6 recorded), +var-tx split into 16x16 leaves. Its last two coefficient units are +`eob=1022 tx=DctDct` and `eob=1020 tx=DctDct` with `ctx=0` -- eob > 256 proves 32x32 +chroma units -- while its FIRST luma leaf is `eob=254 tx=Idtx`. libaom inherits that +`Idtx`; we produced `DctDct`. + +## Gate +`cargo test -p ec-av1 --lib -- a_real_aomenc_stream_with_a_32x32_frame_edge_rect_partition --nocapture` +(under `systemd-run --user --unit=golomb-gate-r7-... -p MemoryMax=10G`, +`EC_AV1_REQUIRE_AOMENC=1`, `CARGO_TARGET_DIR=$HOME/.cache/cargo-target-golomb`) + +BEFORE (r6 handoff): `192x80 cq35 frames=5 10bit=false frame 3 plane U: 978 pixels differ, +first at row 0 col 32 (ours 33 vs ffmpeg 8) [edge32=[0,36,0,0,0,18,0,0]]`. +AFTER: `ok` -- 5 arms x cq {35,40,45,50,55,57,59,61}, 8-bit and 10-bit, +`38 pixel-exact attempts, 32-level edge bits [horz_or_vert=0 split=394], 64-level edge +bits [horz_or_vert=7 split=197] right-VERT=7, 2 named refusals` (both the inter SB-level +AB partition another lane owns). + +EVIDENCE: $HOME/.cache/golomb-gate-r7.log | cargo test -- a_real_aomenc_stream_with_a_32x32_frame_edge_rect_partition --nocapture, 40 aomenc encodes decoded and compared plane-by-plane vs ffmpeg | 38/38 compared attempts pixel-exact, 0 differing pixels (was 978 U pixels on the 192x80 cq35 5-frame arm) +EVIDENCE: $HOME/.cache/golomb-gate-r7b.log | same gate + arms (192,68,5) and (68,192,5) | 68x192 cq35 frame 1 plane Y 141 px differ first at row 0 col 64 -- separate OPEN defect, arm not kept +EVIDENCE: scratchpad/r7-ours.err | EC_TRACE_MODE=1 EC_TRACE_COEFF=1 decode_probe g35.obu, segment 4, block mi(0,16) | first luma leaf tx=Idtx, the two 32x32 chroma units (eob 1022/1020) took tx=DctDct + +Unit check: `cargo test -p ec-av1 --lib -- an_inter_chroma_transform_narrows` => `ok. 1 passed`. + +## Refusals lifted +None this round -- this is a wrong-pixels fix, not a refusal lift. `refusal_inventory` / +`gate_coverage` untouched. + +## The 192x68 / 68x192 straddling arm (r3 verifier ask) -- NOT ADDED, measured why +* `(192, 68, 5, false)`: all EIGHT cq attempts refuse by name, `a reference picture whose + height does not match this frame's own true size` -> the arm compares nothing (vacuous, + class `counter-from-refused-stream`). +* `(68, 192, 5, false)`: RED on a real separate defect -- + `68x192 cq35 frames=5 10bit=false frame 1 plane Y: 141 pixels differ, first at row 0 + col 64 (ours 167 vs ffmpeg 166) [edge32=[0,34,0,0,1,17,0,1]]`. LUMA, inside the 4-px + straddling COLUMN; the straddling ROW twin (192x68) never gets that far. + disposition: deferred(the right-edge straddling-column luma defect above, and the + reference-size refusal) -- both are outside this lane's chroma tx_type cause. + +## Test totals +`$HOME/.cache/golomb-suite-r7.log` (unit `golomb-suite-r7`) -- see the `test result:` line. +NOTE: `$HOME/.cache/golomb-suite-r6.log` never produced a `test result:` line: it was still +`active` (254/404 tests, stuck in the long superres/encoder tests) and holding +`CARGO_TARGET_DIR`, so it was stopped -- it was measuring the PRE-fix tree and is superseded +by r7. Every `test` line it did produce was `ok` or a documented `ignored`. + +## Open / residue +* `main` has advanced to `85887c7` (we merged `18bf7dc`); this branch is NOT re-merged. +* the two defects the 68-px arms found (above), deferred.