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();