Integer SAD in the motion search, a priced-once symbol table
Browse files at this commit06a3123d3b70afd4af57a0f75764cda00d9b41d5Parent: 12435b7- perf
- integer SAD in the motion search, a priced-once symbol table
2 changed files +21 -5
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 thirteen | 1628 | /// 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 = source | 279 | + // 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(); |