From 06a3123d3b70afd4af57a0f75764cda00d9b41d5 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 05:34:47 +0300 Subject: [PATCH] perf(av1): integer SAD in the motion search, a priced-once symbol table Two more bit-exact costs off the RD search's inner loops: - the motion search accumulated its SAD as one f64 conversion and dependent add per sample; a 32x32 block of 8-bit samples sums exactly in u32, and that sum converts exactly to f64, so the value is unchanged; - `symbol_bits` called libm's log2 for every symbol of every candidate the search prices, but a CDF interval is a 15-bit width, so there are only 32769 prices; they are now computed once into a table (each entry exactly the expression the call site used to evaluate). instructions:u on the 24-frame 1280x720 sequence bench: 231.8G -> 209.0G (-9.8%); output bytes unchanged (64931), BD-rate unchanged on all three gate clips, gate wall 7.0/6.7/6.7 -> 6.3/6.0/6.2 s. --- crates/ec-av1/src/encode.rs | 15 +++++++++++++-- crates/ec-av1/src/motion.rs | 11 ++++++++--- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/crates/ec-av1/src/encode.rs b/crates/ec-av1/src/encode.rs index a6221cb7..55cf82c7 100644 --- a/crates/ec-av1/src/encode.rs +++ b/crates/ec-av1/src/encode.rs @@ -1610,8 +1610,19 @@ impl Reach { /// What one symbol costs against a CDF, in bits. pub(crate) fn symbol_bits(cdf: &[u16], symbol: usize) -> f64 { let low = if symbol == 0 { 0 } else { cdf[symbol - 1] }; - let probability = f64::from(cdf[symbol] - low) / 32768.0; - -probability.log2() + let width = cdf[symbol] - low; + // lane-av1speed: a CDF interval is a 15-bit width, so there are only + // 32769 prices a symbol can carry; the `log2` behind each is computed + // once instead of per call (libm's `__log2_fma` was 5% of the encoder's + // profile -- this function is called for every symbol of every candidate + // the RD search prices). Each entry is exactly the expression this used + // to evaluate, so every price is bit-identical. + static PRICES: std::sync::OnceLock> = std::sync::OnceLock::new(); + PRICES.get_or_init(|| { + (0..=32768u32) + .map(|w| -(f64::from(w) / 32768.0).log2()) + .collect() + })[usize::from(width)] } /// What the tile writer spends to say a block is coded in each of the thirteen diff --git a/crates/ec-av1/src/motion.rs b/crates/ec-av1/src/motion.rs index 57515a25..e54094d6 100644 --- a/crates/ec-av1/src/motion.rs +++ b/crates/ec-av1/src/motion.rs @@ -276,13 +276,18 @@ fn search_traced_from_step( reference, stride, ref_width, ref_height, x_q4, y_q4, block_w, block_h, &mut dst16, fctx, ); - let sad: f64 = source + // lane-av1speed: accumulated as an integer (a block is at most + // 32x32 samples of at most 255 each, nowhere near `u32`, and an + // integer sum this small is exact in `f64` whatever order it is + // summed in) instead of one `f64` conversion and dependent add per + // sample. Same value, several times the throughput. + let sad: u32 = source .iter() .zip(dst16.iter()) - .map(|(&a, &b)| f64::from((i32::from(a) - i32::from(b)).unsigned_abs())) + .map(|(&a, &b)| (i32::from(a) - i32::from(b)).unsigned_abs()) .sum(); let diff = (mv.0 - pred_mv.0, mv.1 - pred_mv.1); - sad + lambda * mv_bits(diff) + f64::from(sad) + lambda * mv_bits(diff) }; let mut trace = Vec::new();