From 32f9ae5db038134aed2f847e18139c5b9a779ca3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Fri, 4 Sep 2026 14:23:40 +0300 Subject: [PATCH] perf(av1): dequantize into a reused buffer, with the loop invariants hoisted `dequant_wh` was a `map`/`collect` building a fresh `Vec` per transform unit (7.8% self time, the third-hottest symbol in the decode profile), and inside it every coefficient repeated the whole per-coefficient spec function: two quantizer-table lookups with their own clamps, the area denominator's match, and the bound shift -- all of which depend on the transform, not on the coefficient. `dequant_wh_into` hoists them and writes a caller-owned buffer, which the one hot caller (`dequant_and_inverse_typed_wh`) keeps thread-local. A zero level dequantizes to zero for every parameter set, so the all-but-EOB tail of a grid now costs a compare rather than a 64-bit multiply and divide. The arithmetic on a non-zero level is untouched; `dequant_wh` keeps its `Vec` signature for the other callers. `buffered_dequant_matches_the_per_coefficient_form` checks the buffered form against `dequant_coeff_wh` over six transform shapes, three bit depths, three quantizer/delta sets and both level signs. Byte-exact: `EC_PROBE_OUT16` dumps cmp IDENTICAL against the lane's baseline binary on 1080p 10-bit, 2160p HDR and the 8-bit palette fixture. Interleaved medians against the previous commit: 1080p 13.46 s -> 12.15 s, 4K 67.11 s -> 63.38 s. --- crates/ec-av1/src/quant.rs | 67 +++++++++++++++++++++++++++++++--- crates/ec-av1/src/transform.rs | 15 +++++++- 2 files changed, 75 insertions(+), 7 deletions(-) diff --git a/crates/ec-av1/src/quant.rs b/crates/ec-av1/src/quant.rs index a42166c5..8da4c346 100644 --- a/crates/ec-av1/src/quant.rs +++ b/crates/ec-av1/src/quant.rs @@ -226,11 +226,45 @@ pub fn dequant(levels: &[i32], side: usize, bit_depth: u8, q_idx: i32) -> Vec Vec { - levels - .iter() - .enumerate() - .map(|(i, &l)| dequant_coeff_wh(l, i == 0, bit_depth, q_idx, dc_delta, ac_delta, w, h)) - .collect() + let mut out = Vec::new(); + dequant_wh_into(levels, &mut out, w, h, bit_depth, q_idx, dc_delta, ac_delta); + out +} + +/// [`dequant_wh`] into a caller-owned buffer, with the two quantizer-table +/// lookups, the area denominator and the clamp bound hoisted out of the +/// per-coefficient loop -- they depend only on the transform, and this loop +/// runs once per coefficient of every transform unit in the stream (7.8% +/// self time). A zero level dequantizes to zero for every parameter set, so +/// the common all-but-EOB tail costs a compare instead of a 64-bit multiply +/// and divide. The arithmetic on a non-zero level is the spec's, unchanged. +#[allow(clippy::too_many_arguments)] +pub fn dequant_wh_into( + levels: &[i32], + out: &mut Vec, + w: usize, + h: usize, + bit_depth: u8, + q_idx: i32, + dc_delta: i32, + ac_delta: i32, +) { + let dcq = i64::from(dc_q(bit_depth, q_idx + dc_delta)); + let acq = i64::from(ac_q(bit_depth, q_idx + ac_delta)); + let denom = dq_denom_area(w * h); + let bound = 1i64 << (7 + u32::from(bit_depth)); + let apply = |l: i32, q: i64| -> i32 { + let dq = i64::from(l) * q; + let sign = if dq < 0 { -1 } else { 1 }; + let dq2 = sign * ((dq.abs() & 0xFF_FFFF) / denom); + dq2.clamp(-bound, bound - 1) as i32 + }; + out.clear(); + out.reserve(levels.len()); + out.extend(levels.iter().map(|&l| if l == 0 { 0 } else { apply(l, acq) })); + if let (Some(dst), Some(&l)) = (out.first_mut(), levels.first()) { + *dst = if l == 0 { 0 } else { apply(l, dcq) }; + } } #[cfg(test)] @@ -254,6 +288,29 @@ mod tests { assert_eq!([4, 8, 16, 32, 64].map(dq_denom), [1, 1, 1, 2, 4]); } + /// The hoisted buffer form must agree with the per-coefficient spec + /// function it replaced, at every level sign, both quantizer roles and + /// every denominator band. + #[test] + fn buffered_dequant_matches_the_per_coefficient_form() { + let levels: Vec = (0..64).map(|i| (i as i32 - 32) * 37).collect(); + let mut out = Vec::new(); + for (w, h) in [(4, 4), (16, 16), (32, 32), (64, 64), (8, 32), (16, 4)] { + for bit_depth in [8u8, 10, 12] { + for (q_idx, dc, ac) in [(20, 0, 0), (140, -8, 5), (255, 12, -12)] { + let n = (w * h).min(levels.len()); + dequant_wh_into(&levels[..n], &mut out, w, h, bit_depth, q_idx, dc, ac); + for (i, &got) in out.iter().enumerate() { + let want = dequant_coeff_wh( + levels[i], i == 0, bit_depth, q_idx, dc, ac, w, h, + ); + assert_eq!(got, want, "{w}x{h} depth {bit_depth} q {q_idx} coeff {i}"); + } + } + } + } + } + /// Negative levels round toward zero, so a level and its negation /// dequantize to values that differ only in sign. #[test] diff --git a/crates/ec-av1/src/transform.rs b/crates/ec-av1/src/transform.rs index 8c9bbd41..8cb14750 100644 --- a/crates/ec-av1/src/transform.rs +++ b/crates/ec-av1/src/transform.rs @@ -1003,8 +1003,19 @@ pub fn dequant_and_inverse_typed_wh( ac_delta: i32, tx_type: TxType, ) -> Vec { - let dq = crate::quant::dequant_wh(levels, w, h, bit_depth, q_idx, dc_delta, ac_delta); - inverse_transform_2d_typed_wh(&dq, w, h, bit_depth, tx_type) + DQ_SCRATCH.with(|cell| { + let mut dq = cell.borrow_mut(); + crate::quant::dequant_wh_into(levels, &mut dq, w, h, bit_depth, q_idx, dc_delta, ac_delta); + inverse_transform_2d_typed_wh(&dq, w, h, bit_depth, tx_type) + }) +} + +thread_local! { + /// The dequantized grid, reused per thread: it lives only until the + /// inverse transform has read it, so it was a `Vec` allocated and dropped + /// once per transform unit. + static DQ_SCRATCH: std::cell::RefCell> = + const { std::cell::RefCell::new(Vec::new()) }; } /// [`dequant_and_inverse_typed`] at `DCT_DCT`, no per-plane quantizer delta.