forge
Explore
Log in

Don't you dare go Hollow

Tabulate the film-grain scaling curve once per frame

Browse files at this commit

60817696d2e70f653e554360dab7a3c1720dd41eAhmet Kaan GümüşParent: 32f9ae5
  • perf
    • tabulate the film-grain scaling curve once per frame
Show full message
perf(av1): tabulate the film-grain scaling curve once per frame

`add_noise_to_block` (9.0% self time) evaluated `scale_lut` per sample -- a
shift, a branch on the bit depth, two table loads and an interpolation --
once per luma sample and once per chroma sample per plane. The scaling LUT is
fixed for the frame and `scale_lut` is a pure function of the sample value, so
its whole index domain (1024 entries at 10 bit, 256 at 8) is now tabulated
once per frame by `expand_scaling_lut`, which calls `scale_lut` itself: the
spec function stays the source of truth and the per-sample work becomes one
load.

The luma loop also walks the picture row and the grain row as slices rather
than recomputing `row * stride + col` per sample.

Byte-exact: `EC_PROBE_OUT16` dumps cmp IDENTICAL against the lane's baseline
binary on 1080p 10-bit, 2160p HDR and the 8-bit palette fixture. Interleaved
medians against the previous commit: 1080p 12.76 s -> 12.40 s, 4K 65.47 s ->
65.12 s (the 4K run is the noisiest on this box; five 1080p pairs are the
readable number).
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 {
297297 }
298298 }
299299
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+
300314 /// `add_noise_to_block` (spec 7.18.3.5), 4:2:0 only. `(py0, px0)` is
301315 /// the luma-plane pixel origin; the matching chroma origin is `(py0/2,
302316 /// 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 {
306320 fn add_noise_to_block(
307321 fg: &FilmGrainParams,
308322 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],
312326 picture: &mut Picture,
313327 py0: i32,
314328 px0: i32,
@@ -386,9 +400,9 @@ fn add_noise_to_block(
386400 >> 6)
387401 + cb_offset)
388402 .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;
392406 picture.u[cidx] =
393407 ((picture.u[cidx] as i32 + delta).clamp(min_chroma, max_chroma)) as u16;
394408 }
@@ -397,25 +411,27 @@ fn add_noise_to_block(
397411 >> 6)
398412 + cr_offset)
399413 .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;
403417 picture.v[cidx] =
404418 ((picture.v[cidx] as i32 + delta).clamp(min_chroma, max_chroma)) as u16;
405419 }
406420 }
407421 }
408422 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;
409426 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;
419435 }
420436 }
421437 }
@@ -609,6 +625,11 @@ pub(crate) fn apply_grain(
609625 )
610626 };
611627
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+
612633 let overlap = fg.overlap_flag;
613634
614635 let mut y_line_buf = vec![0i32; (y_stride * 2).max(1) as usize];