From bb7bd46bfd8a9effa323aeb7ab9f7a43a42b26d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 30 Sep 2026 14:12:06 +0300 Subject: [PATCH] ec-av1: the lossless OUR_DQW twin, plane-tagged, with the raw levels beside dq Re-derives lane-av1loss444mm r3/r6/r7 (6dfd827e, 7586358f, c8ed05f3) onto current main. The open defect it instruments is the 4:4:4 lossless KEY FRAME reconstruction-only divergence (21 U + 28 V samples off by up to +/-63, luma exact, entropy bit-identical), named in lanes/av1loss444mm.report.md r10 as item (a) with a three-file search space. The oracle's `EC_DQCOEFF` rung printed thousands of lines on a lossless stream while ours printed NOTHING, because `dequant_and_inverse_typed_wh` is the LOSSY route -- a lossless 4x4 unit goes to `dequant_and_inverse_wht4x4`. That silence reads as "no coefficients" rather than "wrong function". The twin prints the same `pos:value` nonzero form under the same `EC_DQCOEFF` env var, plus: - `plane=`, carried on a new `TxParams.plane` set by the reader that OWNS the unit. The per-unit census plane could not supply it: the callers that route a lossless 4x4 unit into the WHT are not all the ones that stamp the census plane (measured: 1663 of 1663 chroma units paired by count, 1660 against an empty grid). Without `plane=` a 4x4 chroma grid is indistinguishable from a luma one and an order-paired ladder collapses -- equal counts, value ratios of exactly 1.0, and only 16 of 2053 units matched. - `lv=`, the RAW level grid the dequant consumed. If `nz` is empty while `lv` is not, `dequant_wh_into` is what zeroed it, and the line now says so instead of leaving the next lane to infer it (this is what retracted r6's "1650 units dequantized to empty"). Behaviour-free: `plane` reaches nothing but the env-gated eprintln, and it is threaded through `dequant_and_inverse_wht` (which main has since generalised to any shape, lane-whtshape) rather than only the 4x4 entry, so the raster path's tiles carry it too. `census_plane()` from 7586358f is NOT landed: on the branch's own tip it is defined and never called, dead weight next to the field that replaced it. --- crates/ec-av1/src/decode.rs | 13 ++++++++ crates/ec-av1/src/transform.rs | 58 +++++++++++++++++++++++++++++----- 2 files changed, 63 insertions(+), 8 deletions(-) diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index 713ecf0a..a662e91d 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -3028,6 +3028,14 @@ fn cfl_src_rect(px: usize, py: usize, bw: usize, bh: usize) -> CflSrc { /// `reconstruct_mc_rect` reads both of its buffers at. #[derive(Clone, Copy)] pub(crate) struct TxParams { + /// The plane this unit belongs to (0 luma, 1 U, 2 V), carried from the + /// reader that OWNS the unit. `dequant_and_inverse_wht4x4`'s `EC_DQCOEFF` + /// twin prints it so its ladder pairs with the oracle's: the per-unit + /// census plane could not, because the callers that route a lossless 4x4 + /// unit into the WHT are not all the ones that stamp the census plane + /// (measured on `ll444-lossless-key.obu`: 1663 of 1663 chroma units paired + /// by count, 1660 of them against an empty grid). + pub(crate) plane: usize, pub(crate) w: usize, pub(crate) h: usize, pub(crate) bit_depth: u8, @@ -3118,6 +3126,7 @@ impl TxParams { } crate::transform::dequant_and_inverse_wht( grid, + self.plane, self.w, self.h, self.bit_depth, @@ -21556,6 +21565,7 @@ fn read_plane( let levels = extend_corner(grid, tx_side, tx_side, side, side); let (dc_delta, ac_delta) = plane_q_delta(plane_idx, fctx); let tx = TxParams { + plane: plane_idx, w: side, h: side, bit_depth: bit_depth(fctx), @@ -27206,6 +27216,7 @@ fn decode_leaf_rect8( // fixture: the DCT route left ±1..30 sample errors on every // walked leaf's luma. let tx = TxParams { + plane: 0, w: tw, h: th, bit_depth: crate::decode::bit_depth(fctx), @@ -30608,6 +30619,7 @@ fn read_inter_plane_rect( // stride, so the dense `w x h` residual is re-laid at that stride // (`TxParams::stride`) after the inverse transform. let tx = TxParams { + plane: plane_idx, w, h, bit_depth: bit_depth(fctx), @@ -37712,6 +37724,7 @@ fn read_inter_plane( let grid = extend_corner(grid, tx_side, tx_side, side, side); let (dc_delta, ac_delta) = plane_q_delta(plane_idx, fctx); let tx = TxParams { + plane: plane_idx, w: side, h: side, bit_depth: bit_depth(fctx), diff --git a/crates/ec-av1/src/transform.rs b/crates/ec-av1/src/transform.rs index dce1830d..4e8b1d4b 100644 --- a/crates/ec-av1/src/transform.rs +++ b/crates/ec-av1/src/transform.rs @@ -1381,6 +1381,15 @@ pub fn dequant_and_inverse_typed_wh( // the twin of the oracle's `EC_DQCOEFF` rung (whose own layout is // column-major `input[c * h + r]`), so a residual mismatch whose // levels already agree is separable into dequant vs transform. + // + // LOSSY PATH ONLY (lane-av1loss444mm): a LOSSLESS 4x4 unit never + // reaches this function -- `decode.rs` routes it to + // `dequant_and_inverse_wht4x4` -- so on a lossless stream this rung + // printed NOTHING while the oracle's printed thousands of lines, and + // that silence reads as "no coefficients" rather than "wrong function". + // The lossless twin is `OUR_DQW` in `dequant_and_inverse_wht4x4`, + // below; both carry `plane=` so a chroma grid can be told from a luma + // one (the oracle's line carries it too). if crate::envflags::env_flag!("EC_DQCOEFF") { let nz: Vec = dq .iter() @@ -3144,6 +3153,7 @@ pub fn inverse_wht4x4(dq: &[i32]) -> Vec { /// marker contract). pub fn dequant_and_inverse_wht4x4( levels: &[i32], + plane: usize, bit_depth: u8, q_idx: i32, dc_delta: i32, @@ -3160,6 +3170,35 @@ pub fn dequant_and_inverse_wht4x4( crate::quant::dequant_wh_into( levels, &mut dq, 4, 4, bit_depth, q_idx, dc_delta, ac_delta, None, ); + // lane-av1loss444mm: the LOSSLESS half of the oracle's `EC_DQCOEFF` + // twin. Same env var, same `pos:value` nonzero form, PLUS `plane=` + // -- which is what makes the two ladders pairable: without it a 4x4 + // chroma grid is indistinguishable from a luma one and an + // order-paired comparison of the two sides collapses (measured on + // `ll444-lossless-key.obu`: equal unit counts and value ratios of + // exactly 1.0, yet 16 of 2053 units matched). + if crate::envflags::env_flag!("EC_DQCOEFF") { + let nz: Vec = dq[..16] + .iter() + .enumerate() + .filter(|&(_, &v)| v != 0) + .map(|(i, &v)| format!("{i}:{v}")) + .collect(); + // `lv=` is the RAW level grid the dequant consumed: if `nz` is + // empty while `lv` is not, `dequant_wh_into` is what zeroed it, and + // this line says so instead of leaving the next lane to infer it. + let lv: Vec = levels[..16] + .iter() + .enumerate() + .filter(|&(_, &v)| v != 0) + .map(|(i, &v)| format!("{i}:{v}")) + .collect(); + eprintln!( + "OUR_DQW plane={plane} w=4 h=4 q={q_idx} dcd={dc_delta} acd={ac_delta} nz={} lv={}", + nz.join(","), + lv.join(",") + ); + } inverse_wht4x4(&dq[..16]) }) } @@ -3200,6 +3239,7 @@ pub fn dequant_and_inverse_wht4x4( /// Returns the same empty-grid marker as [`dequant_and_inverse_wht4x4`]. pub fn dequant_and_inverse_wht( levels: &[i32], + plane: usize, w: usize, h: usize, bit_depth: u8, @@ -3208,7 +3248,7 @@ pub fn dequant_and_inverse_wht( ac_delta: i32, ) -> Vec { if w == 4 && h == 4 { - return dequant_and_inverse_wht4x4(levels, bit_depth, q_idx, dc_delta, ac_delta); + return dequant_and_inverse_wht4x4(levels, plane, bit_depth, q_idx, dc_delta, ac_delta); } if crate::decode::is_zero_grid(levels) { return Vec::new(); @@ -3227,7 +3267,8 @@ pub fn dequant_and_inverse_wht( }; } } - let res = dequant_and_inverse_wht4x4(&tile, bit_depth, q_idx, dc_delta, ac_delta); + let res = + dequant_and_inverse_wht4x4(&tile, plane, bit_depth, q_idx, dc_delta, ac_delta); // An all-zero tile dequantizes to the same empty marker a 4x4 // caller passes straight through, and `out` is already zero -- // copying out of it would index an empty slice. @@ -3342,12 +3383,13 @@ mod lossless_tx_tests { }; for &(w, h) in &[(8usize, 4usize), (4, 8), (8, 8), (16, 4), (4, 16), (16, 16)] { let levels: Vec = (0..w * h).map(|_| next()).collect(); - let got = super::dequant_and_inverse_wht(&levels, w, h, 10, 0, 0, 0); + let got = super::dequant_and_inverse_wht(&levels, 0, w, h, 10, 0, 0, 0); let mut want = vec![0i32; w * h]; for ty in 0..h / 4 { for tx in 0..w / 4 { let unit = super::dequant_and_inverse_wht4x4( &tile_of(&levels, w, tx, ty), + 0, 10, 0, 0, @@ -3382,7 +3424,7 @@ mod lossless_tx_tests { levels[0] = 400; levels[1] = -240; levels[2] = 88; - let got = super::dequant_and_inverse_wht(&levels, 8, 4, 10, 0, 0, 0); + let got = super::dequant_and_inverse_wht(&levels, 0, 8, 4, 10, 0, 0, 0); assert_eq!(got.len(), 32, "8x4 residual length"); let mut want = vec![0i32; 32]; for (tx, ty) in [(0usize, 0usize), (1, 0)] { @@ -3392,7 +3434,7 @@ mod lossless_tx_tests { t[r * 4 + c] = levels[(ty * 4 + r) * 8 + tx * 4 + c]; } } - let unit = super::dequant_and_inverse_wht4x4(&t, 10, 0, 0, 0); + let unit = super::dequant_and_inverse_wht4x4(&t, 0, 10, 0, 0, 0); if unit.is_empty() { continue; } @@ -3422,8 +3464,8 @@ mod lossless_tx_tests { }) .collect(); assert_eq!( - super::dequant_and_inverse_wht(&levels, 4, 4, 8, 0, 0, 0), - super::dequant_and_inverse_wht4x4(&levels, 8, 0, 0, 0), + super::dequant_and_inverse_wht(&levels, 0, 4, 4, 8, 0, 0, 0), + super::dequant_and_inverse_wht4x4(&levels, 0, 8, 0, 0, 0), "levels={levels:?}" ); } @@ -3437,7 +3479,7 @@ mod lossless_tx_tests { #[test] fn a_non_4x4_all_zero_lossless_unit_keeps_the_empty_marker() { assert!( - super::dequant_and_inverse_wht(&[0i32; 32], 8, 4, 10, 0, 0, 0).is_empty(), + super::dequant_and_inverse_wht(&[0i32; 32], 0, 8, 4, 10, 0, 0, 0).is_empty(), "an all-zero 8x4 lossless unit must stay the empty marker" ); }