From 636712fa9c1c09b493428d6cfe0b048501d43e79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Sun, 6 Sep 2026 06:09:25 +0300 Subject: [PATCH] perf(av1): ten headroom bits under the forward network, measured on BD Six bits of headroom left the fixed-point network's rounding worth 3.9 BD points on screen capture against libaom. Ten put the coefficient resolution at 1/256 of a unit of the `8 * orthonormal` scale a level is formed on, which is where the BD gate stops moving -- 13 bits measures identical -- and stays under 2^27 for a 12-bit residual through two 64-point passes. BD-rate vs libaom 216.6/233.5/289.8 (base 216.4/235.5/289.2), vs rav1e 157.4/175.9/138.1 (base 157.8/178.3/141.1), 1080p/2160p/screen: six of the eight numbers improve, screen-vs-libaom is +0.6 and 1080p-vs-libaom +0.2. Also measured and REVERTED: the row pass's `Vec` in a thread-local scratch, as the inverse transform's is, +5.0% instructions -- reusing the buffer needs a `memset` where a fresh allocation is already zeroed, and that zeroing is what the all-zero-row skip reads. Noted at the function. --- crates/ec-av1/src/transform.rs | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/crates/ec-av1/src/transform.rs b/crates/ec-av1/src/transform.rs index 2945bb49..8a7429d1 100644 --- a/crates/ec-av1/src/transform.rs +++ b/crates/ec-av1/src/transform.rs @@ -1736,11 +1736,12 @@ const FORWARD_RANGE: usize = 32; /// The network rounds to an integer at every butterfly, so its output LSB is /// what the quantizer's input resolution costs: unshifted, one LSB at 4x4 is /// four whole units of the `8 * orthonormal` scale the levels are formed on -/// -- coarser than a fine quantizer's own step. Six bits put that back at -/// 1/16 of a unit and cost nothing: the widest case (a 13-bit residual -/// through two 64-point passes, gain 32) stays under 2^25, well inside `i32` -/// and far inside [`FORWARD_RANGE`]. -const FORWARD_PRECISION_BITS: u32 = 6; +/// -- coarser than a fine quantizer's own step, and worth 3.9 BD points on +/// screen capture. Ten bits put it at 1/256 of a unit, which is where the BD +/// gate stops moving (13 bits measured identical), and cost nothing: the +/// widest case, a 12-bit residual through two 64-point passes (gain 32), +/// stays under 2^27 -- inside `i32` and far inside [`FORWARD_RANGE`]. +const FORWARD_PRECISION_BITS: u32 = 10; /// [`forward_dct_n`]'s uniform gain at one size, measured from the network /// itself rather than derived: an impulse through it is `g` times @@ -1795,6 +1796,12 @@ pub fn forward_transform_2d(residual: &[i32], side: usize) -> Vec { /// The row pass, in fixed point: each of `side` residual rows through /// [`forward_dct_fixed`], at [`FORWARD_PRECISION_BITS`] extra precision and /// the network's own gain (both of which [`forward_scale`] divides back out). +/// +/// The `Vec` is per call on purpose: holding it in a thread-local scratch, as +/// the inverse transform's row pass does, was MEASURED +5.0% instructions on +/// the sequence bench -- a fresh zeroed allocation costs less here than the +/// `memset` reusing one needs plus the thread-local access, because the +/// zeroing is what the all-zero-row skip below reads. fn forward_rows(residual: &[i32], side: usize) -> Vec { assert_eq!( residual.len(),