forge
Explore
Log in

Don't you dare go Hollow

Film grain chroma noise walks contiguous rows

Browse files at this commit

8567ed51a9d89685301ff546a1a9dd90540a9235Ahmet Kaan GümüşParent: bea828c
  • perf
    • film grain chroma noise walks contiguous rows
Show full message
perf(av1): film grain chroma noise walks contiguous rows

`add_noise_to_block`'s chroma half recomputed `(ly * y_stride + lx)`,
`(cy0 + i) * c_stride + (cx0 + j)` and the grain index per sample, and
indexed four planes with a bounds check each time. Every one of those
operands is contiguous along a row, so the loop now slices one row of
luma, U, V and both grain planes per `i` and zips them: the 4:2:0 average
(spec 7.18.3.5) is exactly `chunks_exact(2)` over the block's own luma
row, `(l[0] + l[1] + 1) >> 1` as before. The whole loop is skipped when
neither chroma plane takes noise.

Byte-exact: EC_PROBE_OUT16 sha256 unchanged on a 1080p and a 4K segment
and on `palette_screen_witness.obu`. Interleaved medians vs the tip
before this lane (3 pairs 1080p, one pair 4K): 11.96 s -> 11.39 s at
1080p, 60.71 s -> 58.87 s at 4K.
crates/ec-av1/src/film_grain.rsmodified+45 -29
@@ -387,35 +387,51 @@ fn add_noise_to_block(
387 387
388 let cy0 = py0 / 2;388 let cy0 = py0 / 2;
389 let cx0 = px0 / 2;389 let cx0 = px0 / 2;
390- for i in 0..half_h {390+ if apply_cb || apply_cr {
391- for j in 0..half_w {391+ // lane-perf4: one row of each operand at a time. The 4:2:0 luma
392- let ly = py0 + i * 2;392+ // average (spec 7.18.3.5) reads a horizontally adjacent PAIR of the
393- let lx = px0 + j * 2;393+ // block's own luma row, so `chunks_exact(2)` walks exactly the pairs
394- let l0 = (ly * y_stride + lx) as usize;394+ // the per-sample `(ly * y_stride + lx)` form used to re-derive, and
395- let average_luma = (picture.y[l0] as i32 + picture.y[l0 + 1] as i32 + 1) >> 1;395+ // the U/V/grain rows advance in lockstep with it -- same integers,
396- let cidx = ((cy0 + i) * c_stride + (cx0 + j)) as usize;396+ // same rounding, no per-sample index arithmetic or bounds check.
397- let gidx = ((cg_row0 + i) * cg_stride + (cg_col0 + j)) as usize;397+ let cw = half_w as usize;
398- if apply_cb {398+ // U and V are distinct fields, so a disjoint borrow of each is
399- let scaled = (((average_luma * cb_luma_mult + cb_mult * picture.u[cidx] as i32)399+ // needed alongside the immutable read of Y.
400- >> 6)400+ let Picture { y: ref luma_plane, u: ref mut u_plane, v: ref mut v_plane, .. } =
401- + cb_offset)401+ *picture;
402- .clamp(0, index_max);402+ for i in 0..half_h {
403- let delta =403+ let lrow = ((py0 + i * 2) * y_stride + px0) as usize;
404- (scaling_cb[scaled as usize] * cb_grain[gidx] + rounding_offset)404+ let crow = ((cy0 + i) * c_stride + cx0) as usize;
405- >> scaling_shift;405+ let grow = ((cg_row0 + i) * cg_stride + cg_col0) as usize;
406- picture.u[cidx] =406+ let luma = &luma_plane[lrow..lrow + cw * 2];
407- ((picture.u[cidx] as i32 + delta).clamp(min_chroma, max_chroma)) as u16;407+ let gcb = &cb_grain[grow..grow + cw];
408- }408+ let gcr = &cr_grain[grow..grow + cw];
409- if apply_cr {409+ let urow = &mut u_plane[crow..crow + cw];
410- let scaled = (((average_luma * cr_luma_mult + cr_mult * picture.v[cidx] as i32)410+ let vrow = &mut v_plane[crow..crow + cw];
411- >> 6)411+ for ((((lpair, u), v), &gb), &gr) in luma
412- + cr_offset)412+ .chunks_exact(2)
413- .clamp(0, index_max);413+ .zip(urow.iter_mut())
414- let delta =414+ .zip(vrow.iter_mut())
415- (scaling_cr[scaled as usize] * cr_grain[gidx] + rounding_offset)415+ .zip(gcb.iter())
416- >> scaling_shift;416+ .zip(gcr.iter())
417- picture.v[cidx] =417+ {
418- ((picture.v[cidx] as i32 + delta).clamp(min_chroma, max_chroma)) as u16;418+ let average_luma = (lpair[0] as i32 + lpair[1] as i32 + 1) >> 1;
419+ if apply_cb {
420+ let scaled = (((average_luma * cb_luma_mult + cb_mult * *u as i32) >> 6)
421+ + cb_offset)
422+ .clamp(0, index_max);
423+ let delta =
424+ (scaling_cb[scaled as usize] * gb + rounding_offset) >> scaling_shift;
425+ *u = ((*u as i32 + delta).clamp(min_chroma, max_chroma)) as u16;
426+ }
427+ if apply_cr {
428+ let scaled = (((average_luma * cr_luma_mult + cr_mult * *v as i32) >> 6)
429+ + cr_offset)
430+ .clamp(0, index_max);
431+ let delta =
432+ (scaling_cr[scaled as usize] * gr + rounding_offset) >> scaling_shift;
433+ *v = ((*v as i32 + delta).clamp(min_chroma, max_chroma)) as u16;
434+ }
419 }435 }
420 }436 }
421 }437 }