From b0db8e99a119d75fb309fd3bc9189b61b672a31f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Mon, 7 Sep 2026 14:19:22 +0300 Subject: [PATCH] feat(av1): a lossless key frame decodes sample-exact The rect strip readers take the per-transform-unit walk on a lossless frame (depth_to_tx_wh returns TX_4X4 before it looks at the block), their chroma is coded as 4x4 units plane-major with get_txb_ctx's +10 offset rows, and the Walsh-Hadamard orientation is pinned by measurement. A 64x64 libaom -crf 0 key frame now decodes sample-exact against ffmpeg (6488 entropy steps identical to aomdec's EC_TRACE_COEFF, 0 sample diffs). --- crates/ec-av1/src/decode.rs | 62 +++++++++++++++++++++++++++++++--- crates/ec-av1/src/transform.rs | 39 ++++++++++++--------- 2 files changed, 80 insertions(+), 21 deletions(-) diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index 6b98746d..d5b8127f 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -9565,6 +9565,58 @@ fn decode_rect_split( m.alpha.zip(ac).map(|((_, av), ac)| (av, ac)), None, m.smooth_neighbor_uv, fctx, ); (zero.clone(), zero) + } else if lossless(fctx) { + // lane-lossless: the strip's chroma plane block is coded as 4x4 + // transform units too (libaom `read_tx_size` returns TX_4X4 for every + // plane), PLANE-MAJOR inside the block -- U's units first, then V's + // (`decode_token_recon_block`'s mu-chunk loop, measured against + // aomdec's `EC_TRACE_COEFF` on a `-crf 0` stream: plane=1, plane=1, + // plane=2, plane=2 for a 16x8 strip). `get_txb_ctx` adds its `+10` + // offset (our `+3` row base) whenever the plane block is bigger than + // the transform. + let scan4 = default_scan(4); + let offset = (chroma_w > 4 || chroma_h > 4).then_some(3); + let (cw_n, ch_n) = (chroma_w / 4, chroma_h / 4); + let mut last = [Grid::Zero(16), Grid::Zero(16)]; + for plane_idx in 1..=2 { + for cu_row in 0..ch_n { + for cu_col in 0..cw_n { + let luma_span = 8; + let cu_mi = ( + mi_r + cu_row * (luma_span / MI), + mi_c + cu_col * (luma_span / MI), + ); + let cu_around = neighbours.around_mi(cu_mi, luma_span); + let cu_reach = tu_reach( + bw, bh, + cu_col * luma_span, cu_row * luma_span, luma_span, + block_reach, px, py, y.width, y.height, fctx, + ); + let (cu_x, cu_y) = (cpx + cu_col * 4, cpy + cu_row * 4); + if let Some((ub, vb)) = &m.palette_uv { + set_palette_pred( + if plane_idx == 1 { ub.clone() } else { vb.clone() }, + fctx, + ); + } + let plane = if plane_idx == 1 { &mut *u } else { &mut *v }; + let grid = read_plane( + dec, cdfs, TxbSet::Chroma4, &scan4, plane_idx, + cu_around[plane_idx], m.mode, m.uv_predict_mode, + m.angle_delta_uv, cu_reach, plane, cu_x, cu_y, 4, 4, + base_q_idx, + m.alpha.zip(ac).map(|((au, av), ac)| { + (if plane_idx == 1 { au } else { av }, ac) + }), + None, offset, m.smooth_neighbor_uv, fctx, + )?; + neighbours.record_mi_chroma(cu_mi, luma_span, luma_span, plane_idx, &grid); + last[plane_idx - 1] = grid; + } + } + } + let [ug, vg] = last; + (ug, vg) } else { let (chroma_set, chroma_scan) = chroma.expect("refused above when None"); // `av1_get_ext_tx_set_type`: a chroma transform whose square-up size @@ -10281,7 +10333,7 @@ fn decode_intra_rect_in_inter( } else { 0 }; - if depth != 0 { + if depth != 0 || lossless(fctx) { hit!(TX_DEPTH_HITS); // lane-intrasplit r1: a split intra strip in an inter frame walks the // key frame's own per-TU path ([`decode_rect_split`] below); what was @@ -10494,7 +10546,7 @@ fn decode_block_rect( } else { 0 }; - if depth != 0 { + if depth != 0 || lossless(fctx) { hit!(TX_DEPTH_HITS); // lane-rectsplit r1: a split transform is predicted and reconstructed // per transform unit, each unit taking its edges from the previous @@ -11000,7 +11052,7 @@ fn decode_leaf_rect( } else { 0 }; - if depth != 0 { + if depth != 0 || lossless(fctx) { // The same per-transform-unit walk the bigger strips take // (`depth_to_tx_wh`: TX_16X8 -> TX_8X8 at depth 1, -> TX_4X4 at // depth 2), addressed at this leaf's own mi position. @@ -11342,7 +11394,7 @@ fn decode_block_rect4( depth_to_tx_wh(bw, bh, depth, fctx), (bw, bh), fctx, ); - if depth != 0 { + if depth != 0 || lossless(fctx) { // lane-rectsplitx r1: the per-transform-unit walk, at this strip's // real mi position (strips 1 and 3 of a HORZ_4 start 8 px into a // 16-px [`SUB`] cell). depth 1 is the RECT unit TX_16X8/TX_8X16 @@ -12358,7 +12410,7 @@ fn decode_block_rect64( depth_to_tx_wh(bw, bh, depth, fctx), (bw, bh), fctx, ); - if depth != 0 { + if depth != 0 || lossless(fctx) { hit!(TX_DEPTH_HITS); // lane-rectsplit r1/r4: the superblock-level strip splits its // transform through the very same per-unit path as its 32x32-level diff --git a/crates/ec-av1/src/transform.rs b/crates/ec-av1/src/transform.rs index 1c69ebeb..d6c58ea9 100644 --- a/crates/ec-av1/src/transform.rs +++ b/crates/ec-av1/src/transform.rs @@ -2849,10 +2849,16 @@ mod tests { /// dequantization exactly -- that is what makes the round trip lossless. pub fn inverse_wht4x4(dq: &[i32]) -> Vec { debug_assert_eq!(dq.len(), 16); + // libaom runs pass 1 down the COLUMNS (`ip[4 * k]`, `ip++`) and pass 2 + // along the intermediate's ROWS writing dest COLUMNS (`dest[stride * k]`, + // `dest++`); transposing both passes -- rows first, then columns, output + // in place -- is the same map and is what a raster `dq`/residual pair + // wants here. MEASURED: the other orientation reconstructs sample 0 and + // the DC-only units correctly and drifts by +-1 everywhere else. let mut out = [0i32; 16]; for i in 0..4 { - let ip = &dq[i * 4..]; - let (mut a1, mut c1, mut d1, mut b1) = (ip[0] >> 2, ip[1] >> 2, ip[2] >> 2, ip[3] >> 2); + let (mut a1, mut c1, mut d1, mut b1) = + (dq[i * 4] >> 2, dq[i * 4 + 1] >> 2, dq[i * 4 + 2] >> 2, dq[i * 4 + 3] >> 2); a1 += c1; d1 -= b1; let e1 = (a1 - d1) >> 1; @@ -2867,7 +2873,8 @@ pub fn inverse_wht4x4(dq: &[i32]) -> Vec { } let mut res = vec![0i32; 16]; for i in 0..4 { - let (mut a1, mut c1, mut d1, mut b1) = (out[i], out[4 + i], out[8 + i], out[12 + i]); + let (mut a1, mut c1, mut d1, mut b1) = + (out[i], out[4 + i], out[8 + i], out[12 + i]); a1 += c1; d1 -= b1; let e1 = (a1 - d1) >> 1; @@ -2925,23 +2932,23 @@ mod lossless_tx_tests { (a1 - c, b, c, d1 + b) } fn fwht(residual: &[i32]) -> Vec { - let mut t = residual.to_vec(); + let mut t = [0i32; 16]; for i in 0..4 { - let (a, b, c, d) = undo(t[i], t[4 + i], t[8 + i], t[12 + i]); - t[i] = a; - t[4 + i] = c; - t[8 + i] = d; - t[12 + i] = b; + let (a, b, c, d) = undo(residual[i], residual[4 + i], residual[8 + i], residual[12 + i]); + t[i * 4] = a; + t[i * 4 + 1] = c; + t[i * 4 + 2] = d; + t[i * 4 + 3] = b; } - let mut out = vec![0i32; 16]; + let mut dq = vec![0i32; 16]; for i in 0..4 { - let (a, b, c, d) = undo(t[i * 4], t[i * 4 + 1], t[i * 4 + 2], t[i * 4 + 3]); - out[i * 4] = a * 4; - out[i * 4 + 1] = c * 4; - out[i * 4 + 2] = d * 4; - out[i * 4 + 3] = b * 4; + let (a, b, c, d) = undo(t[i], t[4 + i], t[8 + i], t[12 + i]); + dq[i] = a * 4; + dq[4 + i] = c * 4; + dq[8 + i] = d * 4; + dq[12 + i] = b * 4; } - out + dq } let mut state = 0x1234_5678u32; for _ in 0..200 {