forge
Explore
Log in

Don't you dare go Hollow

Integer SAD in the motion search, a priced-once symbol table

Browse files at this commit

06a3123d3b70afd4af57a0f75764cda00d9b41d5Ahmet Kaan GümüşParent: 12435b7
  • perf
    • integer SAD in the motion search, a priced-once symbol table
Show full message
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.rsmodified+13 -2
@@ -1610,8 +1610,19 @@ impl Reach {
1610 /// What one symbol costs against a CDF, in bits.1610 /// What one symbol costs against a CDF, in bits.
1611 pub(crate) fn symbol_bits(cdf: &[u16], symbol: usize) -> f64 {1611 pub(crate) fn symbol_bits(cdf: &[u16], symbol: usize) -> f64 {
1612 let low = if symbol == 0 { 0 } else { cdf[symbol - 1] };1612 let low = if symbol == 0 { 0 } else { cdf[symbol - 1] };
1613- let probability = f64::from(cdf[symbol] - low) / 32768.0;1613+ let width = cdf[symbol] - low;
1614- -probability.log2()1614+ // lane-av1speed: a CDF interval is a 15-bit width, so there are only
1615+ // 32769 prices a symbol can carry; the `log2` behind each is computed
1616+ // once instead of per call (libm's `__log2_fma` was 5% of the encoder's
1617+ // profile -- this function is called for every symbol of every candidate
1618+ // the RD search prices). Each entry is exactly the expression this used
1619+ // to evaluate, so every price is bit-identical.
1620+ static PRICES: std::sync::OnceLock<Vec<f64>> = std::sync::OnceLock::new();
1621+ PRICES.get_or_init(|| {
1622+ (0..=32768u32)
1623+ .map(|w| -(f64::from(w) / 32768.0).log2())
1624+ .collect()
1625+ })[usize::from(width)]
1615 }1626 }
1616 1627
1617 /// What the tile writer spends to say a block is coded in each of the thirteen1628 /// What the tile writer spends to say a block is coded in each of the thirteen
crates/ec-av1/src/motion.rsmodified+8 -3
@@ -276,13 +276,18 @@ fn search_traced_from_step(
276 reference, stride, ref_width, ref_height, x_q4, y_q4, block_w, block_h,276 reference, stride, ref_width, ref_height, x_q4, y_q4, block_w, block_h,
277 &mut dst16, fctx,277 &mut dst16, fctx,
278 );278 );
279- let sad: f64 = source279+ // lane-av1speed: accumulated as an integer (a block is at most
280+ // 32x32 samples of at most 255 each, nowhere near `u32`, and an
281+ // integer sum this small is exact in `f64` whatever order it is
282+ // summed in) instead of one `f64` conversion and dependent add per
283+ // sample. Same value, several times the throughput.
284+ let sad: u32 = source
280 .iter()285 .iter()
281 .zip(dst16.iter())286 .zip(dst16.iter())
282- .map(|(&a, &b)| f64::from((i32::from(a) - i32::from(b)).unsigned_abs()))287+ .map(|(&a, &b)| (i32::from(a) - i32::from(b)).unsigned_abs())
283 .sum();288 .sum();
284 let diff = (mv.0 - pred_mv.0, mv.1 - pred_mv.1);289 let diff = (mv.0 - pred_mv.0, mv.1 - pred_mv.1);
285- sad + lambda * mv_bits(diff)290+ f64::from(sad) + lambda * mv_bits(diff)
286 };291 };
287 292
288 let mut trace = Vec::new();293 let mut trace = Vec::new();