Dequantize into a reused buffer, with the loop invariants hoisted
Browse files at this commit32f9ae5db038134aed2f847e18139c5b9a779ca3Parent: ee4e975- perf
- dequantize into a reused buffer, with the loop invariants hoisted
2 changed files +75 -7
- crates/ec-av1/src/quant.rs +62 -5
- crates/ec-av1/src/transform.rs +13 -2
crates/ec-av1/src/quant.rsmodified+62 -5
| @@ -226,11 +226,45 @@ pub fn dequant(levels: &[i32], side: usize, bit_depth: u8, q_idx: i32) -> Vec<i3 | ||
| 226 | 226 | /// [`QuantDeltas`]-shaped `dc_delta`/`ac_delta` pair as [`dequant_coeff_wh`]. |
| 227 | 227 | #[allow(clippy::too_many_arguments)] |
| 228 | 228 | pub fn dequant_wh(levels: &[i32], w: usize, h: usize, bit_depth: u8, q_idx: i32, dc_delta: i32, ac_delta: i32) -> Vec<i32> { |
| 229 | - levels | |
| 230 | - .iter() | |
| 231 | - .enumerate() | |
| 232 | - .map(|(i, &l)| dequant_coeff_wh(l, i == 0, bit_depth, q_idx, dc_delta, ac_delta, w, h)) | |
| 233 | - .collect() | |
| 229 | + let mut out = Vec::new(); | |
| 230 | + dequant_wh_into(levels, &mut out, w, h, bit_depth, q_idx, dc_delta, ac_delta); | |
| 231 | + out | |
| 232 | +} | |
| 233 | + | |
| 234 | +/// [`dequant_wh`] into a caller-owned buffer, with the two quantizer-table | |
| 235 | +/// lookups, the area denominator and the clamp bound hoisted out of the | |
| 236 | +/// per-coefficient loop -- they depend only on the transform, and this loop | |
| 237 | +/// runs once per coefficient of every transform unit in the stream (7.8% | |
| 238 | +/// self time). A zero level dequantizes to zero for every parameter set, so | |
| 239 | +/// the common all-but-EOB tail costs a compare instead of a 64-bit multiply | |
| 240 | +/// and divide. The arithmetic on a non-zero level is the spec's, unchanged. | |
| 241 | +#[allow(clippy::too_many_arguments)] | |
| 242 | +pub fn dequant_wh_into( | |
| 243 | + levels: &[i32], | |
| 244 | + out: &mut Vec<i32>, | |
| 245 | + w: usize, | |
| 246 | + h: usize, | |
| 247 | + bit_depth: u8, | |
| 248 | + q_idx: i32, | |
| 249 | + dc_delta: i32, | |
| 250 | + ac_delta: i32, | |
| 251 | +) { | |
| 252 | + let dcq = i64::from(dc_q(bit_depth, q_idx + dc_delta)); | |
| 253 | + let acq = i64::from(ac_q(bit_depth, q_idx + ac_delta)); | |
| 254 | + let denom = dq_denom_area(w * h); | |
| 255 | + let bound = 1i64 << (7 + u32::from(bit_depth)); | |
| 256 | + let apply = |l: i32, q: i64| -> i32 { | |
| 257 | + let dq = i64::from(l) * q; | |
| 258 | + let sign = if dq < 0 { -1 } else { 1 }; | |
| 259 | + let dq2 = sign * ((dq.abs() & 0xFF_FFFF) / denom); | |
| 260 | + dq2.clamp(-bound, bound - 1) as i32 | |
| 261 | + }; | |
| 262 | + out.clear(); | |
| 263 | + out.reserve(levels.len()); | |
| 264 | + out.extend(levels.iter().map(|&l| if l == 0 { 0 } else { apply(l, acq) })); | |
| 265 | + if let (Some(dst), Some(&l)) = (out.first_mut(), levels.first()) { | |
| 266 | + *dst = if l == 0 { 0 } else { apply(l, dcq) }; | |
| 267 | + } | |
| 234 | 268 | } |
| 235 | 269 | |
| 236 | 270 | #[cfg(test)] |
| @@ -254,6 +288,29 @@ mod tests { | ||
| 254 | 288 | assert_eq!([4, 8, 16, 32, 64].map(dq_denom), [1, 1, 1, 2, 4]); |
| 255 | 289 | } |
| 256 | 290 | |
| 291 | + /// The hoisted buffer form must agree with the per-coefficient spec | |
| 292 | + /// function it replaced, at every level sign, both quantizer roles and | |
| 293 | + /// every denominator band. | |
| 294 | + #[test] | |
| 295 | + fn buffered_dequant_matches_the_per_coefficient_form() { | |
| 296 | + let levels: Vec<i32> = (0..64).map(|i| (i as i32 - 32) * 37).collect(); | |
| 297 | + let mut out = Vec::new(); | |
| 298 | + for (w, h) in [(4, 4), (16, 16), (32, 32), (64, 64), (8, 32), (16, 4)] { | |
| 299 | + for bit_depth in [8u8, 10, 12] { | |
| 300 | + for (q_idx, dc, ac) in [(20, 0, 0), (140, -8, 5), (255, 12, -12)] { | |
| 301 | + let n = (w * h).min(levels.len()); | |
| 302 | + dequant_wh_into(&levels[..n], &mut out, w, h, bit_depth, q_idx, dc, ac); | |
| 303 | + for (i, &got) in out.iter().enumerate() { | |
| 304 | + let want = dequant_coeff_wh( | |
| 305 | + levels[i], i == 0, bit_depth, q_idx, dc, ac, w, h, | |
| 306 | + ); | |
| 307 | + assert_eq!(got, want, "{w}x{h} depth {bit_depth} q {q_idx} coeff {i}"); | |
| 308 | + } | |
| 309 | + } | |
| 310 | + } | |
| 311 | + } | |
| 312 | + } | |
| 313 | + | |
| 257 | 314 | /// Negative levels round toward zero, so a level and its negation |
| 258 | 315 | /// dequantize to values that differ only in sign. |
| 259 | 316 | #[test] |
crates/ec-av1/src/transform.rsmodified+13 -2
| @@ -1003,8 +1003,19 @@ pub fn dequant_and_inverse_typed_wh( | ||
| 1003 | 1003 | ac_delta: i32, |
| 1004 | 1004 | tx_type: TxType, |
| 1005 | 1005 | ) -> Vec<i32> { |
| 1006 | - let dq = crate::quant::dequant_wh(levels, w, h, bit_depth, q_idx, dc_delta, ac_delta); | |
| 1007 | - inverse_transform_2d_typed_wh(&dq, w, h, bit_depth, tx_type) | |
| 1006 | + DQ_SCRATCH.with(|cell| { | |
| 1007 | + let mut dq = cell.borrow_mut(); | |
| 1008 | + crate::quant::dequant_wh_into(levels, &mut dq, w, h, bit_depth, q_idx, dc_delta, ac_delta); | |
| 1009 | + inverse_transform_2d_typed_wh(&dq, w, h, bit_depth, tx_type) | |
| 1010 | + }) | |
| 1011 | +} | |
| 1012 | + | |
| 1013 | +thread_local! { | |
| 1014 | + /// The dequantized grid, reused per thread: it lives only until the | |
| 1015 | + /// inverse transform has read it, so it was a `Vec` allocated and dropped | |
| 1016 | + /// once per transform unit. | |
| 1017 | + static DQ_SCRATCH: std::cell::RefCell<Vec<i32>> = | |
| 1018 | + const { std::cell::RefCell::new(Vec::new()) }; | |
| 1008 | 1019 | } |
| 1009 | 1020 | |
| 1010 | 1021 | /// [`dequant_and_inverse_typed`] at `DCT_DCT`, no per-plane quantizer delta. |