forge
Explore
Log in

Don't you dare go Hollow

Separable box sums in the sgrproj a/b pass

Browse files at this commit

ac194aa89102ebcac07f3d96ec3e2fdccbb147a7Ahmet Kaan GümüşParent: 801fe73
  • perf
    • separable box sums in the sgrproj a/b pass
Show full message
perf(av1): separable box sums in the sgrproj a/b pass

`compute_ab` summed every grid point's own (2r+1)x(2r+1) window from
scratch, so each source sample went through `lr_sample` -- whose
stripe-boundary substitution is the expensive part -- (2r+1) times more
often than necessary. One column-sum pass per grid row reads each sample
once and the point's box is then (2r+1) adds of those i64 column sums;
integer addition is exact and associative, so every total is unchanged.
The per-point bit-depth read is hoisted out with it.

Byte-exact on seg1080/seg4k/seg4k_lr/palette; 4K 59.37 s -> 46.15 s
(1.286x, from 1.252x at the previous step), LR-heavy 4K cut 57.21 s ->
50.47 s (1.134x).
crates/ec-av1/src/restoration.rsmodified+30 -10
@@ -674,28 +674,48 @@ fn compute_ab(
674 let gh = h + 2;674 let gh = h + 2;
675 let mut ab = vec![(0i32, 0i64); gw * gh];675 let mut ab = vec![(0i32, 0i64); gw * gh];
676 let n = ((2 * r + 1) * (2 * r + 1)) as i64;676 let n = ((2 * r + 1) * (2 * r + 1)) as i64;
677+ // lane-perf6: the box sums are separable. Summing every grid point's own
678+ // (2r+1)^2 window called `lr_sample` -- whose stripe-boundary test is the
679+ // expensive part -- (2r+1) times more often than needed; one column-sum
680+ // pass per grid row reads each source sample once per row instead, and
681+ // the horizontal sum is then (2r+1) adds of i64s. Integer addition is
682+ // exact and associative, so the totals are unchanged bit for bit.
683+ let ru = r as usize;
684+ let ncols = gw + 2 * ru;
685+ let mut col_a = vec![0i64; ncols];
686+ let mut col_b = vec![0i64; ncols];
687+ // Hoisted out of the per-point body: one thread-local read per call.
688+ let bd_shift = u32::from(crate::decode::bit_depth()).saturating_sub(8);
677 for gi in 0..gh {689 for gi in 0..gh {
678 let i = gi as i64 - 1;690 let i = gi as i64 - 1;
691+ let base_col = h_start as i64 - 1 - r as i64;
692+ for k in 0..ncols {
693+ let col = base_col + k as i64;
694+ let (mut ca, mut cb) = (0i64, 0i64);
695+ for dr in -r..=r {
696+ let row = v_start as i64 + i + dr as i64;
697+ let px = lr_sample(
698+ cdef, deblocked, stride, plane_w, plane_h, v_start, v_end, row, col,
699+ ) as i64;
700+ ca += px * px;
701+ cb += px;
702+ }
703+ col_a[k] = ca;
704+ col_b[k] = cb;
705+ }
679 for gj in 0..gw {706 for gj in 0..gw {
680 let j = gj as i64 - 1;707 let j = gj as i64 - 1;
681 let mut a = 0i64;708 let mut a = 0i64;
682 let mut b = 0i64;709 let mut b = 0i64;
683- for dr in -r..=r {710+ for k in gj..gj + 2 * ru + 1 {
684- let row = v_start as i64 + i + dr as i64;711+ a += col_a[k];
685- for dc in -r..=r {712+ b += col_b[k];
686- let col = h_start as i64 + j + dc as i64;
687- let px =
688- lr_sample(cdef, deblocked, stride, plane_w, plane_h, v_start, v_end, row, col) as i64;
689- a += px * px;
690- b += px;
691- }
692 }713 }
693 // libaom `restoration.c:660` -- the box sums are brought back714 // libaom `restoration.c:660` -- the box sums are brought back
694 // to the 8-bit scale before `p` is formed (`a` is a sum of715 // to the 8-bit scale before `p` is formed (`a` is a sum of
695 // squares, so it loses twice the shift `b` does); `B[k]` below716 // squares, so it loses twice the shift `b` does); `B[k]` below
696 // keeps the RAW `b`, only `p` uses the scaled pair. At 8-bit717 // keeps the RAW `b`, only `p` uses the scaled pair. At 8-bit
697 // both shifts are 0, which is why this was invisible until now.718 // both shifts are 0, which is why this was invisible until now.
698- let bd_shift = u32::from(crate::decode::bit_depth()).saturating_sub(8);
699 let a_s = round2(a, 2 * bd_shift);719 let a_s = round2(a, 2 * bd_shift);
700 let b_s = round2(b, bd_shift);720 let b_s = round2(b, bd_shift);
701 let p = if a_s * n < b_s * b_s { 0 } else { a_s * n - b_s * b_s };721 let p = if a_s * n < b_s * b_s { 0 } else { a_s * n - b_s * b_s };