forge
Explore
Log in

Don't you dare go Hollow

CDEF filters a gathered window with a resolved damping shift

Browse files at this commit

126001f283f3529a8a9f459cd655ab390de5669fAhmet Kaan GümüşParent: 2baca5a
  • perf
    • CDEF filters a gathered window with a resolved damping shift
Show full message
perf(av1): CDEF filters a gathered window with a resolved damping shift

Every tap read went through a closure that re-derived the frame-bounds
test (four compares plus the stride multiply) for each of a pixel's ~17
reads, and `constrain` recomputed its damping shift -- a `leading_zeros`
and a branch -- twice per tap. `cdef_gather` now lifts a unit's whole
footprint (its samples plus the two-pixel `CDEF_DIRECTIONS` ring) into a
reused scratch window once, contiguously for the interior case, and both
the direction search and `cdef_filter_block` index that; the shift is
resolved once per block by `cdef_constrain_shift`. A unit whose four
strengths are all zero now skips the gather and the direction search
outright -- `dir`/`var` are only ever consumed through a strength.

Byte-exact on seg1080/seg4k/seg4k_lr/palette; 4K 59.40 s -> 51.37 s
against the same-session base (interleaved medians of 3, 1.156x vs the
previous step's 1.140x), 1080p 10.72 s.
crates/ec-av1/src/decode.rsmodified+165 -36
@@ -14086,8 +14086,75 @@ fn cdef_constrain(diff: i32, threshold: i32, damping: i32) -> i32 {
14086 if threshold == 0 {14086 if threshold == 0 {
14087 return 0;14087 return 0;
14088 }14088 }
14089- let shift = (damping - cdef_msb(threshold)).max(0);14089+ cdef_constrain_shifted(diff, threshold, cdef_constrain_shift(threshold, damping))
14090- diff.signum() * (threshold - (diff.abs() >> shift)).clamp(0, diff.abs())14090+}
14091+
14092+/// `constrain`'s damping shift: a function of the block's threshold and
14093+/// damping alone, so the filter loop computes it once per block instead of
14094+/// the `leading_zeros` + branch it used to do twice per tap read.
14095+fn cdef_constrain_shift(threshold: i32, damping: i32) -> i32 {
14096+ (damping - cdef_msb(threshold)).max(0)
14097+}
14098+
14099+/// [`cdef_constrain`]'s body with the shift already resolved; `threshold` is
14100+/// non-zero on every call (the caller only filters when its strength is).
14101+fn cdef_constrain_shifted(diff: i32, threshold: i32, shift: i32) -> i32 {
14102+ let a = diff.abs();
14103+ diff.signum() * (threshold - (a >> shift)).clamp(0, a)
14104+}
14105+
14106+/// The widest `CDEF_DIRECTIONS` offset in either axis: every tap a block reads
14107+/// lies inside its own samples grown by this much.
14108+const CDEF_BORDER: usize = 2;
14109+
14110+/// Copies one block's whole CDEF footprint -- its `bh` x `bw` samples plus the
14111+/// [`CDEF_BORDER`] tap ring -- out of the *unfiltered* plane into `buf` (row
14112+/// stride `bw + 2 * CDEF_BORDER`), so the direction search and the tap loops
14113+/// index a small contiguous window instead of re-deriving the frame-bounds
14114+/// test on every one of a pixel's ~17 tap reads. Out-of-frame positions get
14115+/// `CDEF_VERY_LARGE`, exactly as the per-sample closure did.
14116+fn cdef_gather(
14117+ src: &[u16],
14118+ stride: usize,
14119+ true_w: usize,
14120+ true_h: usize,
14121+ ox: usize,
14122+ oy: usize,
14123+ bw: usize,
14124+ bh: usize,
14125+ buf: &mut [i32],
14126+) {
14127+ let bstride = bw + 2 * CDEF_BORDER;
14128+ let rows = bh + 2 * CDEF_BORDER;
14129+ if oy >= CDEF_BORDER
14130+ && ox >= CDEF_BORDER
14131+ && oy + bh + CDEF_BORDER <= true_h
14132+ && ox + bw + CDEF_BORDER <= true_w
14133+ {
14134+ // Interior block: every tap is inside the frame, so the whole window
14135+ // is `rows` contiguous runs.
14136+ for r in 0..rows {
14137+ let s0 = (oy - CDEF_BORDER + r) * stride + (ox - CDEF_BORDER);
14138+ let src_row = &src[s0..s0 + bstride];
14139+ let dst_row = &mut buf[r * bstride..r * bstride + bstride];
14140+ for (d, &v) in dst_row.iter_mut().zip(src_row) {
14141+ *d = i32::from(v);
14142+ }
14143+ }
14144+ return;
14145+ }
14146+ for r in 0..rows {
14147+ let ny = oy as isize + r as isize - CDEF_BORDER as isize;
14148+ for c in 0..bstride {
14149+ let nx = ox as isize + c as isize - CDEF_BORDER as isize;
14150+ buf[r * bstride + c] =
14151+ if ny < 0 || nx < 0 || ny as usize >= true_h || nx as usize >= true_w {
14152+ CDEF_VERY_LARGE
14153+ } else {
14154+ i32::from(src[ny as usize * stride + nx as usize])
14155+ };
14156+ }
14157+ }
14091 }14158 }
14092 14159
14093 /// Spec 7.15.3's direction search (libaom `cdef_find_dir_c`): the dominant14160 /// Spec 7.15.3's direction search (libaom `cdef_find_dir_c`): the dominant
@@ -14168,7 +14235,7 @@ fn cdef_adjust_strength(strength: i32, var: i32) -> i32 {
14168 /// previously-filtered neighbour must never feed this block's own sum).14235 /// previously-filtered neighbour must never feed this block's own sum).
14169 #[allow(clippy::too_many_arguments)]14236 #[allow(clippy::too_many_arguments)]
14170 fn cdef_filter_block(14237 fn cdef_filter_block(
14171- sample: impl Fn(i32, i32) -> i32,14238+ src: &[i32],
14172 dst: &mut PlaneBuf,14239 dst: &mut PlaneBuf,
14173 ox: usize,14240 ox: usize,
14174 oy: usize,14241 oy: usize,
@@ -14189,19 +14256,33 @@ fn cdef_filter_block(
14189 // derivative) carries the bit-depth shift, so the tap-set parity bit14256 // derivative) carries the bit-depth shift, so the tap-set parity bit
14190 // must be read back above that shift, not off the raw low bit.14257 // must be read back above that shift, not off the raw low bit.
14191 let pri_taps = CDEF_PRI_TAPS[((pri_strength >> coeff_shift) & 1) as usize];14258 let pri_taps = CDEF_PRI_TAPS[((pri_strength >> coeff_shift) & 1) as usize];
14259+ // Both damping shifts depend only on this block's strengths (spec 7.15.3's
14260+ // `constrain`), so they are resolved here rather than per tap read.
14261+ let pri_shift = cdef_constrain_shift(pri_strength.max(1), pri_damping);
14262+ let sec_shift = cdef_constrain_shift(sec_strength.max(1), sec_damping);
14263+ let bstride = bw + 2 * CDEF_BORDER;
14264+ let smax = sample_max();
14265+ // `src` is [`cdef_gather`]'s window: (i, j) in block coordinates, taps
14266+ // reaching +-CDEF_BORDER, so every index below is in range by construction.
14267+ let at = |i: i32, j: i32| -> i32 {
14268+ src[(i + CDEF_BORDER as i32) as usize * bstride + (j + CDEF_BORDER as i32) as usize]
14269+ };
14270+ let (pri_dir, sec_dir0, sec_dir1) =
14271+ (CDEF_DIRECTIONS[dir], CDEF_DIRECTIONS[(dir + 2) & 7], CDEF_DIRECTIONS[(dir + 6) & 7]);
14192 for i in 0..bh {14272 for i in 0..bh {
14193 for j in 0..bw {14273 for j in 0..bw {
14194- let x = sample(i as i32, j as i32);14274+ let (i32i, i32j) = (i as i32, j as i32);
14275+ let x = at(i32i, i32j);
14195 let mut sum = 0i32;14276 let mut sum = 0i32;
14196 let mut max = x;14277 let mut max = x;
14197 let mut min = x;14278 let mut min = x;
14198 for k in 0..2usize {14279 for k in 0..2usize {
14199 if enable_primary {14280 if enable_primary {
14200- let (tr, tc) = CDEF_DIRECTIONS[dir][k];14281+ let (tr, tc) = pri_dir[k];
14201- let p0 = sample(i as i32 + tr, j as i32 + tc);14282+ let p0 = at(i32i + tr, i32j + tc);
14202- let p1 = sample(i as i32 - tr, j as i32 - tc);14283+ let p1 = at(i32i - tr, i32j - tc);
14203- sum += pri_taps[k] * cdef_constrain(p0 - x, pri_strength, pri_damping);14284+ sum += pri_taps[k] * cdef_constrain_shifted(p0 - x, pri_strength, pri_shift);
14204- sum += pri_taps[k] * cdef_constrain(p1 - x, pri_strength, pri_damping);14285+ sum += pri_taps[k] * cdef_constrain_shifted(p1 - x, pri_strength, pri_shift);
14205 if clipping_required {14286 if clipping_required {
14206 if p0 != CDEF_VERY_LARGE {14287 if p0 != CDEF_VERY_LARGE {
14207 max = max.max(p0);14288 max = max.max(p0);
@@ -14213,12 +14294,12 @@ fn cdef_filter_block(
14213 }14294 }
14214 }14295 }
14215 if enable_secondary {14296 if enable_secondary {
14216- let (tr0, tc0) = CDEF_DIRECTIONS[(dir + 2) & 7][k];14297+ let (tr0, tc0) = sec_dir0[k];
14217- let (tr1, tc1) = CDEF_DIRECTIONS[(dir + 6) & 7][k];14298+ let (tr1, tc1) = sec_dir1[k];
14218- let s0 = sample(i as i32 + tr0, j as i32 + tc0);14299+ let s0 = at(i32i + tr0, i32j + tc0);
14219- let s1 = sample(i as i32 - tr0, j as i32 - tc0);14300+ let s1 = at(i32i - tr0, i32j - tc0);
14220- let s2 = sample(i as i32 + tr1, j as i32 + tc1);14301+ let s2 = at(i32i + tr1, i32j + tc1);
14221- let s3 = sample(i as i32 - tr1, j as i32 - tc1);14302+ let s3 = at(i32i - tr1, i32j - tc1);
14222 if clipping_required {14303 if clipping_required {
14223 for s in [s0, s1, s2, s3] {14304 for s in [s0, s1, s2, s3] {
14224 if s != CDEF_VERY_LARGE {14305 if s != CDEF_VERY_LARGE {
@@ -14228,17 +14309,17 @@ fn cdef_filter_block(
14228 min = min.min(s0).min(s1).min(s2).min(s3);14309 min = min.min(s0).min(s1).min(s2).min(s3);
14229 }14310 }
14230 sum += CDEF_SEC_TAPS[k]14311 sum += CDEF_SEC_TAPS[k]
14231- * (cdef_constrain(s0 - x, sec_strength, sec_damping)14312+ * (cdef_constrain_shifted(s0 - x, sec_strength, sec_shift)
14232- + cdef_constrain(s1 - x, sec_strength, sec_damping)14313+ + cdef_constrain_shifted(s1 - x, sec_strength, sec_shift)
14233- + cdef_constrain(s2 - x, sec_strength, sec_damping)14314+ + cdef_constrain_shifted(s2 - x, sec_strength, sec_shift)
14234- + cdef_constrain(s3 - x, sec_strength, sec_damping));14315+ + cdef_constrain_shifted(s3 - x, sec_strength, sec_shift));
14235 }14316 }
14236 }14317 }
14237 let mut y = x + ((8 + sum - i32::from(sum < 0)) >> 4);14318 let mut y = x + ((8 + sum - i32::from(sum < 0)) >> 4);
14238 if clipping_required {14319 if clipping_required {
14239 y = y.clamp(min, max);14320 y = y.clamp(min, max);
14240 }14321 }
14241- dst.data[(oy + i) * dst.width + (ox + j)] = y.clamp(0, sample_max()) as u16;14322+ dst.data[(oy + i) * dst.width + (ox + j)] = y.clamp(0, smax) as u16;
14242 }14323 }
14243 }14324 }
14244 }14325 }
@@ -16362,6 +16443,10 @@ fn apply_cdef(
16362 let coeff_shift = i32::from(bit_depth()) - 8;16443 let coeff_shift = i32::from(bit_depth()) - 8;
16363 let damping = i32::from(cdef.damping) + coeff_shift;16444 let damping = i32::from(cdef.damping) + coeff_shift;
16364 let damping_uv = (damping - 1).max(0);16445 let damping_uv = (damping - 1).max(0);
16446+ // One scratch window per plane kind, reused by every unit (8x8 luma and
16447+ // 4x4 chroma, each grown by the [`CDEF_BORDER`] tap ring).
16448+ let mut win_y = [0i32; (8 + 2 * CDEF_BORDER) * (8 + 2 * CDEF_BORDER)];
16449+ let mut win_uv = [0i32; (4 + 2 * CDEF_BORDER) * (4 + 2 * CDEF_BORDER)];
16365 let mut mi_r = 0usize;16450 let mut mi_r = 0usize;
16366 while mi_r < skip_grid.mi_rows {16451 while mi_r < skip_grid.mi_rows {
16367 let mut mi_c = 0usize;16452 let mut mi_c = 0usize;
@@ -16372,14 +16457,22 @@ fn apply_cdef(
16372 if !skip_grid.is_skip_txfm(mi_r, mi_c) {16457 if !skip_grid.is_skip_txfm(mi_r, mi_c) {
16373 let sidx = strength_idx(mi_r, mi_c);16458 let sidx = strength_idx(mi_r, mi_c);
16374 let (ox, oy) = (mi_c * 4, mi_r * 4);16459 let (ox, oy) = (mi_c * 4, mi_r * 4);
16460+ // Nothing to do for this unit at all: the direction search is
16461+ // only ever consumed through a strength, so a unit whose four
16462+ // strengths are zero never needs its 8x8 window gathered.
16463+ if cdef.y_pri_strength[sidx] == 0
16464+ && cdef.y_sec_strength[sidx] == 0
16465+ && cdef.uv_pri_strength[sidx] == 0
16466+ && cdef.uv_sec_strength[sidx] == 0
16467+ {
16468+ mi_c += 2;
16469+ continue;
16470+ }
16375 let (y_stride, y_true_w, y_true_h) = (y.width, y.true_width, y.true_height);16471 let (y_stride, y_true_w, y_true_h) = (y.width, y.true_width, y.true_height);
16472+ cdef_gather(&src_y, y_stride, y_true_w, y_true_h, ox, oy, 8, 8, &mut win_y);
16376 let sample_y = |r: i32, c: i32| -> i32 {16473 let sample_y = |r: i32, c: i32| -> i32 {
16377- let (ny, nx) = (oy as i32 + r, ox as i32 + c);16474+ win_y[(r + CDEF_BORDER as i32) as usize * (8 + 2 * CDEF_BORDER)
16378- if ny < 0 || nx < 0 || ny as usize >= y_true_h || nx as usize >= y_true_w {16475+ + (c + CDEF_BORDER as i32) as usize]
16379- CDEF_VERY_LARGE
16380- } else {
16381- i32::from(src_y[ny as usize * y_stride + nx as usize])
16382- }
16383 };16476 };
16384 // lane-cdef r1: the direction search reads the SAME padded16477 // lane-cdef r1: the direction search reads the SAME padded
16385 // block the taps do -- libaom runs `cdef_find_dir` on the16478 // block the taps do -- libaom runs `cdef_find_dir` on the
@@ -16417,7 +16510,7 @@ fn apply_cdef(
16417 }16510 }
16418 if enable_primary || enable_secondary {16511 if enable_primary || enable_secondary {
16419 cdef_filter_block(16512 cdef_filter_block(
16420- sample_y,16513+ &win_y,
16421 y,16514 y,
16422 ox,16515 ox,
16423 oy,16516 oy,
@@ -16444,16 +16537,9 @@ fn apply_cdef(
16444 for (plane, src_p) in [(&mut *u, &src_u), (&mut *v, &src_v)] {16537 for (plane, src_p) in [(&mut *u, &src_u), (&mut *v, &src_v)] {
16445 let (tw, th) = (plane.true_width, plane.true_height);16538 let (tw, th) = (plane.true_width, plane.true_height);
16446 let stride = plane.width;16539 let stride = plane.width;
16447- let sample_uv = |r: i32, c: i32| -> i32 {16540+ cdef_gather(src_p, stride, tw, th, cox, coy, 4, 4, &mut win_uv);
16448- let (ny, nx) = (coy as i32 + r, cox as i32 + c);
16449- if ny < 0 || nx < 0 || ny as usize >= th || nx as usize >= tw {
16450- CDEF_VERY_LARGE
16451- } else {
16452- i32::from(src_p[ny as usize * stride + nx as usize])
16453- }
16454- };
16455 cdef_filter_block(16541 cdef_filter_block(
16456- sample_uv,16542+ &win_uv,
16457 plane,16543 plane,
16458 cox,16544 cox,
16459 coy,16545 coy,
@@ -31029,6 +31115,49 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs(
31029 #[cfg(test)]31115 #[cfg(test)]
31030 mod tests {31116 mod tests {
31031 31117
31118+ /// lane-perf6 step 2: the CDEF kernel now reads a pre-gathered window and
31119+ /// a pre-resolved damping shift instead of a bounds-testing closure and a
31120+ /// per-tap `msb`. Both replacements must be value-identical to the spec
31121+ /// forms they came from, over the whole strength/damping/diff domain.
31122+ #[test]
31123+ fn cdef_gather_and_shifted_constrain_match_the_spec_forms() {
31124+ for damping in 3..=9i32 {
31125+ for threshold in 1..=64i32 {
31126+ let shift = super::cdef_constrain_shift(threshold, damping);
31127+ for diff in -300..=300i32 {
31128+ assert_eq!(
31129+ super::cdef_constrain(diff, threshold, damping),
31130+ super::cdef_constrain_shifted(diff, threshold, shift),
31131+ "diff={diff} threshold={threshold} damping={damping}"
31132+ );
31133+ }
31134+ }
31135+ }
31136+ // `cdef_gather`'s interior fast path and its bounds-testing slow path
31137+ // must agree with the per-sample closure the filter used to call, at
31138+ // every block position including the three that straddle the crop.
31139+ let (stride, tw, th) = (24usize, 21usize, 19usize);
31140+ let src: Vec<u16> = (0..stride * 24).map(|i| (i * 7 % 1024) as u16).collect();
31141+ for &(ox, oy, bw, bh) in &[(0, 0, 8, 8), (8, 8, 8, 8), (16, 16, 8, 8), (4, 12, 4, 4)] {
31142+ let bstride = bw + 2 * super::CDEF_BORDER;
31143+ let mut win = vec![0i32; bstride * (bh + 2 * super::CDEF_BORDER)];
31144+ super::cdef_gather(&src, stride, tw, th, ox, oy, bw, bh, &mut win);
31145+ for r in -(super::CDEF_BORDER as i32)..(bh + super::CDEF_BORDER) as i32 {
31146+ for c in -(super::CDEF_BORDER as i32)..(bw + super::CDEF_BORDER) as i32 {
31147+ let (ny, nx) = (oy as i32 + r, ox as i32 + c);
31148+ let want = if ny < 0 || nx < 0 || ny as usize >= th || nx as usize >= tw {
31149+ super::CDEF_VERY_LARGE
31150+ } else {
31151+ i32::from(src[ny as usize * stride + nx as usize])
31152+ };
31153+ let got = win[(r + super::CDEF_BORDER as i32) as usize * bstride
31154+ + (c + super::CDEF_BORDER as i32) as usize];
31155+ assert_eq!(got, want, "({ox},{oy}) r={r} c={c}");
31156+ }
31157+ }
31158+ }
31159+ }
31160+
31032 /// lane-golomb r7: [`reduce_inherited_chroma_tx_type`] must reproduce31161 /// lane-golomb r7: [`reduce_inherited_chroma_tx_type`] must reproduce
31033 /// `av1_ext_tx_used[av1_get_ext_tx_set_type(uv_tx, 1, reduced)]`31162 /// `av1_ext_tx_used[av1_get_ext_tx_set_type(uv_tx, 1, reduced)]`
31034 /// (libaom `av1/common/blockd.h:1036`/`1097`) exactly -- the g35 residue31163 /// (libaom `av1/common/blockd.h:1036`/`1097`) exactly -- the g35 residue