Tabulate the film-grain scaling curve once per frame
Browse files at this commit60817696d2e70f653e554360dab7a3c1720dd41eParent: 32f9ae5- perf
- tabulate the film-grain scaling curve once per frame
1 changed file +39 -18
- crates/ec-av1/src/film_grain.rs +39 -18
crates/ec-av1/src/film_grain.rsmodified+39 -18
| @@ -297,6 +297,20 @@ fn scale_lut(lut: &[i32; 256], index: i32, bit_depth: u32) -> i32 { | ||
| 297 | 297 | } |
| 298 | 298 | } |
| 299 | 299 | |
| 300 | +/// [`scale_lut`] evaluated over its whole index domain once per frame. | |
| 301 | +/// | |
| 302 | +/// The scaling LUT is fixed for the frame, so the interpolation the spec | |
| 303 | +/// writes per sample is a pure function of the sample value with at most | |
| 304 | +/// `256 << (bit_depth - 8)` distinct answers -- 1024 entries at 10 bit. This | |
| 305 | +/// is the same function, tabulated: `add_noise_to_block` was 9.0% self time | |
| 306 | +/// and ran that branchy interpolation once per luma sample and twice per | |
| 307 | +/// chroma sample. | |
| 308 | +fn expand_scaling_lut(lut: &[i32; 256], bit_depth: u32) -> Vec<i32> { | |
| 309 | + (0..(256i32 << (bit_depth - 8))) | |
| 310 | + .map(|i| scale_lut(lut, i, bit_depth)) | |
| 311 | + .collect() | |
| 312 | +} | |
| 313 | + | |
| 300 | 314 | /// `add_noise_to_block` (spec 7.18.3.5), 4:2:0 only. `(py0, px0)` is |
| 301 | 315 | /// the luma-plane pixel origin; the matching chroma origin is `(py0/2, |
| 302 | 316 | /// px0/2)`. Grain is read from `(grain, stride, row0, col0)` triples so the |
| @@ -306,9 +320,9 @@ fn scale_lut(lut: &[i32; 256], index: i32, bit_depth: u32) -> i32 { | ||
| 306 | 320 | fn add_noise_to_block( |
| 307 | 321 | fg: &FilmGrainParams, |
| 308 | 322 | mc_identity: bool, |
| 309 | - scaling_y: &[i32; 256], | |
| 310 | - scaling_cb: &[i32; 256], | |
| 311 | - scaling_cr: &[i32; 256], | |
| 323 | + scaling_y: &[i32], | |
| 324 | + scaling_cb: &[i32], | |
| 325 | + scaling_cr: &[i32], | |
| 312 | 326 | picture: &mut Picture, |
| 313 | 327 | py0: i32, |
| 314 | 328 | px0: i32, |
| @@ -386,9 +400,9 @@ fn add_noise_to_block( | ||
| 386 | 400 | >> 6) |
| 387 | 401 | + cb_offset) |
| 388 | 402 | .clamp(0, index_max); |
| 389 | - let delta = (scale_lut(scaling_cb, scaled, bit_depth) * cb_grain[gidx] | |
| 390 | - + rounding_offset) | |
| 391 | - >> scaling_shift; | |
| 403 | + let delta = | |
| 404 | + (scaling_cb[scaled as usize] * cb_grain[gidx] + rounding_offset) | |
| 405 | + >> scaling_shift; | |
| 392 | 406 | picture.u[cidx] = |
| 393 | 407 | ((picture.u[cidx] as i32 + delta).clamp(min_chroma, max_chroma)) as u16; |
| 394 | 408 | } |
| @@ -397,25 +411,27 @@ fn add_noise_to_block( | ||
| 397 | 411 | >> 6) |
| 398 | 412 | + cr_offset) |
| 399 | 413 | .clamp(0, index_max); |
| 400 | - let delta = (scale_lut(scaling_cr, scaled, bit_depth) * cr_grain[gidx] | |
| 401 | - + rounding_offset) | |
| 402 | - >> scaling_shift; | |
| 414 | + let delta = | |
| 415 | + (scaling_cr[scaled as usize] * cr_grain[gidx] + rounding_offset) | |
| 416 | + >> scaling_shift; | |
| 403 | 417 | picture.v[cidx] = |
| 404 | 418 | ((picture.v[cidx] as i32 + delta).clamp(min_chroma, max_chroma)) as u16; |
| 405 | 419 | } |
| 406 | 420 | } |
| 407 | 421 | } |
| 408 | 422 | if apply_y { |
| 423 | + // Both operands are contiguous along a row, so the row bases are | |
| 424 | + // computed once instead of a multiply-add per sample. | |
| 425 | + let width = (half_w * 2) as usize; | |
| 409 | 426 | for i in 0..half_h * 2 { |
| 410 | - for j in 0..half_w * 2 { | |
| 411 | - let pidx = ((py0 + i) * y_stride + (px0 + j)) as usize; | |
| 412 | - let gidx = ((lg_row0 + i) * lg_stride + (lg_col0 + j)) as usize; | |
| 413 | - let delta = (scale_lut(scaling_y, picture.y[pidx] as i32, bit_depth) | |
| 414 | - * luma_grain[gidx] | |
| 415 | - + rounding_offset) | |
| 416 | - >> scaling_shift; | |
| 417 | - picture.y[pidx] = | |
| 418 | - ((picture.y[pidx] as i32 + delta).clamp(min_luma, max_luma)) as u16; | |
| 427 | + let prow = ((py0 + i) * y_stride + px0) as usize; | |
| 428 | + let grow = ((lg_row0 + i) * lg_stride + lg_col0) as usize; | |
| 429 | + let pixels = &mut picture.y[prow..prow + width]; | |
| 430 | + let grain = &luma_grain[grow..grow + width]; | |
| 431 | + for (p, &g) in pixels.iter_mut().zip(grain) { | |
| 432 | + let delta = | |
| 433 | + (scaling_y[*p as usize] * g + rounding_offset) >> scaling_shift; | |
| 434 | + *p = ((*p as i32 + delta).clamp(min_luma, max_luma)) as u16; | |
| 419 | 435 | } |
| 420 | 436 | } |
| 421 | 437 | } |
| @@ -609,6 +625,11 @@ pub(crate) fn apply_grain( | ||
| 609 | 625 | ) |
| 610 | 626 | }; |
| 611 | 627 | |
| 628 | + let expand_bd = grain_bit_depth(); | |
| 629 | + let scaling_y = expand_scaling_lut(&scaling_y, expand_bd); | |
| 630 | + let scaling_cb = expand_scaling_lut(&scaling_cb, expand_bd); | |
| 631 | + let scaling_cr = expand_scaling_lut(&scaling_cr, expand_bd); | |
| 632 | + | |
| 612 | 633 | let overlap = fg.overlap_flag; |
| 613 | 634 | |
| 614 | 635 | let mut y_line_buf = vec![0i32; (y_stride * 2).max(1) as usize]; |