forge
Explore
Log in

Don't you dare go Hollow

Deblock kernel per length, level table per plane pass

Browse files at this commit

b2477330fbad1f11acf74e12768fcb36bbe88aaaAhmet Kaan GümüşParent: e27b2d5
  • perf
    • deblock kernel per length, level table per plane pass
Show full message
perf(av1): deblock kernel per length, level table per plane pass

The AVX2 edge kernel is monomorphised on the edge length, so each of the
four lengths holds only its own taps live and every wide row is clamped
straight into the tile instead of into a shared twelve-entry result array
the register allocator spilled (35% -> 18% of the kernel's samples on
stack moves). An all-empty filter mask now returns without any write-back
at all, which also drops a vertical edge's second transpose.

edge_params' seven per-plane-pass arguments -- half of them passed on the
stack into the inline(never) body -- collapse into one EdgeCtx built once
per band, carrying libaom's av1_loop_filter_frame_init idea in level
form: get_filter_level for every (base level, ref frame) pair, tabulated
once instead of re-derived per 4-pixel edge group.

Byte-exact: all four gate streams hash identically at 1, 4 and 16
threads.
crates/ec-av1/src/decode.rsmodified+288 -166
@@ -17561,26 +17561,105 @@ fn lf_level(
1756117561 /// edge" suppression (which only ever fires at a non-PU-edge TU boundary)
1756217562 /// never applies here, letting this skip the `skip`/`skip_txfm` lookup
1756317563 /// entirely. Returns `None` when this 4-pixel edge group is not filtered.
17564-#[inline(always)]
17565-fn edge_params(
17566- lf: &LoopFilterParams,
17567- n: &Neighbours,
17564+/// One plane pass's worth of [`edge_params`] constants (lane-deblock2): the
17565+/// seven values that used to be re-passed per 4-pixel edge group -- half of
17566+/// them on the stack, into an `inline(never)` body -- plus libaom's
17567+/// `lfi->lfthr` idea in level form: `av1_loop_filter_frame_init` builds the
17568+/// filter level for every `(base level, ref frame)` pair once per frame
17569+/// instead of running `av1_get_filter_level`'s delta arithmetic per edge.
17570+struct EdgeCtx<'a> {
17571+ lf: &'a LoopFilterParams,
17572+ n: &'a Neighbours,
17573+ fctx: &'a crate::decode::FrameCtx,
1756817574 plane_idx: usize,
1756917575 dir: usize,
1757017576 chroma: bool,
17571- x0: usize,
17572- y0: usize,
17573- // lane-perf9: `segmentation_enabled`, read once per plane pass instead of
17574- // four thread-local reads (`SEG_MI_DIMS`, `SEG_IDS`, twice each) plus two
17575- // `SEG` borrows per 4x4 edge. With it clear, every segment id is 0 and no
17576- // `SEG_LVL_ALT_LF_*` feature can be active, so the levels are unchanged.
17577- seg_lf_active: bool, fctx: &crate::decode::FrameCtx,
17578-) -> Option<(u8, i32)> {
17579- let (mi_r, mi_c) = (plane_to_mi(chroma, y0), plane_to_mi(chroma, x0));
17580- let cur_tx = if dir == 0 {
17581- tx_px_at(n, chroma, mi_r, mi_c)
17577+ seg_lf_active: bool,
17578+ /// `delta_lf_grid`'s column for this plane and direction.
17579+ delta_idx: usize,
17580+ /// `mode_step` (`set_lpf_parameters`): one mi for luma, `1 << ss` for
17581+ /// chroma.
17582+ step: usize,
17583+ /// This pass's own `lf.level[..]`, the LUT's base.
17584+ base0: i32,
17585+ /// `lf_level` at `[base level][ref frame + 7]`, with no segment
17586+ /// override -- the whole domain, since a level is `0..=63` and a ref
17587+ /// frame `-7..=7` (`ref_deltas` holds `TOTAL_REFS_PER_FRAME` = 8
17588+ /// entries and is indexed by the ref frame's magnitude).
17589+ lvl: [[u8; 15]; 64],
17590+}
17591+
17592+impl<'a> EdgeCtx<'a> {
17593+ fn new(
17594+ lf: &'a LoopFilterParams,
17595+ n: &'a Neighbours,
17596+ plane_idx: usize,
17597+ dir: usize,
17598+ chroma: bool,
17599+ seg_lf_active: bool,
17600+ fctx: &'a crate::decode::FrameCtx,
17601+ ) -> Self {
17602+ let base0 = i32::from(if plane_idx == 0 {
17603+ lf.level[dir]
17604+ } else if plane_idx == 1 {
17605+ lf.level[2]
17606+ } else {
17607+ lf.level[3]
17608+ });
17609+ // `lf_level`'s own first step is `(base0 + delta_lf).clamp(0, 63)`,
17610+ // so passing `b - base0` pins its base at `b` and the rest of the
17611+ // function is a pure function of `(b, ref_frame)`.
17612+ let mut lvl = [[0u8; 15]; 64];
17613+ for (b, row) in lvl.iter_mut().enumerate() {
17614+ for (r, cell) in row.iter_mut().enumerate() {
17615+ *cell =
17616+ lf_level(lf, plane_idx, dir, (r as i32 - 7) as i8, b as i32 - base0, 0, fctx)
17617+ as u8;
17618+ }
17619+ }
17620+ Self {
17621+ lf,
17622+ n,
17623+ fctx,
17624+ plane_idx,
17625+ dir,
17626+ chroma,
17627+ seg_lf_active,
17628+ delta_idx: if plane_idx == 0 { dir } else { plane_idx + 1 },
17629+ step: if chroma { 2 } else { 1 },
17630+ base0,
17631+ lvl,
17632+ }
17633+ }
17634+
17635+ /// Spec 7.14.4's `get_filter_level`, off the table when no segment can
17636+ /// override it (which is every frame that codes no segmentation).
17637+ #[inline(always)]
17638+ fn level(&self, ref_frame: i8, delta_lf: i32, seg: u8) -> i32 {
17639+ if self.seg_lf_active {
17640+ return lf_level(self.lf, self.plane_idx, self.dir, ref_frame, delta_lf, seg, self.fctx);
17641+ }
17642+ let b = (self.base0 + delta_lf).clamp(0, 63) as usize;
17643+ debug_assert!((-7..=7).contains(&ref_frame));
17644+ let r = (i32::from(ref_frame) + 7).clamp(0, 14) as usize;
17645+ i32::from(self.lvl[b][r])
17646+ }
17647+}
17648+
17649+/// Spec 7.14.2's `set_lpf_parameters`, restricted to the uniform-level,
17650+/// no-segmentation, no-delta-LF path and to this decoder's own invariant
17651+/// that a coded block's transform is always its own full size -- so a TU
17652+/// edge is always also a PU edge, and the "both sides skipped, not a PU
17653+/// edge" suppression (which only ever fires at a non-PU-edge TU boundary)
17654+/// never applies here, letting this skip the `skip`/`skip_txfm` lookup
17655+/// entirely. Returns `None` when this 4-pixel edge group is not filtered.
17656+#[inline(always)]
17657+fn edge_params(c: &EdgeCtx, x0: usize, y0: usize) -> Option<(u8, i32)> {
17658+ let (mi_r, mi_c) = (plane_to_mi(c.chroma, y0), plane_to_mi(c.chroma, x0));
17659+ let cur_tx = if c.dir == 0 {
17660+ tx_px_at(c.n, c.chroma, mi_r, mi_c)
1758217661 } else {
17583- tx_h_px_at(n, chroma, mi_r, mi_c)
17662+ tx_h_px_at(c.n, c.chroma, mi_r, mi_c)
1758417663 };
1758517664 // lane-deblock: `cur_tx` is a power-of-two pixel size, so the alignment
1758617665 // test is a mask, not the hardware `divl` this used to emit (10.7% of
@@ -17590,49 +17669,41 @@ fn edge_params(
1759017669 // the covering transform does not start at), so this head is inlined
1759117670 // into the deblock loops and only survivors pay the call and the
1759217671 // register-spilling prologue of the body below.
17593- if cur_tx == 0 || (if dir == 0 { x0 } else { y0 }) & (cur_tx as usize - 1) != 0 {
17672+ if cur_tx == 0 || (if c.dir == 0 { x0 } else { y0 }) & (cur_tx as usize - 1) != 0 {
1759417673 return None;
1759517674 }
17596- edge_params_body(lf, n, plane_idx, dir, chroma, x0, y0, mi_r, mi_c, cur_tx, seg_lf_active, fctx)
17675+ edge_params_body(c, x0, y0, mi_r, mi_c, cur_tx)
1759717676 }
1759817677
1759917678 /// [`edge_params`] past its transform-alignment gate.
17600-#[allow(clippy::too_many_arguments)]
1760117679 #[inline(never)]
1760217680 fn edge_params_body(
17603- lf: &LoopFilterParams,
17604- n: &Neighbours,
17605- plane_idx: usize,
17606- dir: usize,
17607- chroma: bool,
17681+ c: &EdgeCtx,
1760817682 x0: usize,
1760917683 y0: usize,
1761017684 mi_r: usize,
1761117685 mi_c: usize,
1761217686 cur_tx: u32,
17613- seg_lf_active: bool,
17614- fctx: &crate::decode::FrameCtx,
1761517687 ) -> Option<(u8, i32)> {
17688+ let n = c.n;
1761617689 let cur_ref = n.ref_grid[mi_r * n.skip_grid_cols_mi + mi_c];
17617- // `mode_step` (`set_lpf_parameters`): one mi for luma, `1 << ss` for
17618- // chroma -- the previous cell must keep the odd parity `plane_to_mi`
17619- // just established, i.e. the bottom/right mi of the PREVIOUS 8x8 luma
17620- // group, not the other half of this one.
17621- let step = if chroma { 2 } else { 1 };
17622- let (pv_mi_r, pv_mi_c) = if dir == 0 {
17623- (mi_r, mi_c - step)
17690+ // The previous cell must keep the odd parity `plane_to_mi` just
17691+ // established, i.e. the bottom/right mi of the PREVIOUS 8x8 luma group,
17692+ // not the other half of this one.
17693+ let (pv_mi_r, pv_mi_c) = if c.dir == 0 {
17694+ (mi_r, mi_c - c.step)
1762417695 } else {
17625- (mi_r - step, mi_c)
17696+ (mi_r - c.step, mi_c)
1762617697 };
17627- let pv_tx = if dir == 0 {
17628- tx_px_at(n, chroma, pv_mi_r, pv_mi_c)
17698+ let pv_tx = if c.dir == 0 {
17699+ tx_px_at(n, c.chroma, pv_mi_r, pv_mi_c)
1762917700 } else {
17630- tx_h_px_at(n, chroma, pv_mi_r, pv_mi_c)
17701+ tx_h_px_at(n, c.chroma, pv_mi_r, pv_mi_c)
1763117702 };
1763217703 let pv_ref = n.ref_grid[pv_mi_r * n.skip_grid_cols_mi + pv_mi_c];
17633- let delta_idx = if plane_idx == 0 { dir } else { plane_idx + 1 };
17634- let cur_delta_lf = i32::from(n.delta_lf_grid[mi_r * n.skip_grid_cols_mi + mi_c][delta_idx]);
17635- let pv_delta_lf = i32::from(n.delta_lf_grid[pv_mi_r * n.skip_grid_cols_mi + pv_mi_c][delta_idx]);
17704+ let cur_delta_lf = i32::from(n.delta_lf_grid[mi_r * n.skip_grid_cols_mi + mi_c][c.delta_idx]);
17705+ let pv_delta_lf =
17706+ i32::from(n.delta_lf_grid[pv_mi_r * n.skip_grid_cols_mi + pv_mi_c][c.delta_idx]);
1763617707
1763717708 // spec 7.14.2 / libaom `set_lpf_parameters`: a transform edge between two
1763817709 // SKIPPED INTER blocks is filtered only when it is also a prediction
@@ -17650,16 +17721,16 @@ fn edge_params_body(
1765017721 // size, which a 4x4 luma block read at the `| 1` mi is not.
1765117722 let cur_org = n.blk_org_grid[mi_r * n.skip_grid_cols_mi + mi_c];
1765217723 let cur_dim = n.blk_dim_grid[mi_r * n.skip_grid_cols_mi + mi_c];
17653- let (org_px, dim_mi) = if dir == 0 {
17724+ let (org_px, dim_mi) = if c.dir == 0 {
1765417725 (usize::from(cur_org[1]), usize::from(cur_dim[0]))
1765517726 } else {
1765617727 (usize::from(cur_org[0]), usize::from(cur_dim[1]))
1765717728 };
17658- let pu_edge = if chroma {
17729+ let pu_edge = if c.chroma {
1765917730 let plane_dim = (dim_mi * MI / 2).max(4);
17660- (if dir == 0 { x0 } else { y0 }) % plane_dim == 0
17731+ (if c.dir == 0 { x0 } else { y0 }) % plane_dim == 0
1766117732 } else {
17662- plane_to_mi(false, if dir == 0 { x0 } else { y0 }) == org_px
17733+ plane_to_mi(false, if c.dir == 0 { x0 } else { y0 }) == org_px
1766317734 };
1766417735 if !pu_edge
1766517736 && cur_ref != 0
@@ -17669,18 +17740,18 @@ fn edge_params_body(
1766917740 {
1767017741 return None;
1767117742 }
17672- let (cur_seg, pv_seg) = if seg_lf_active {
17673- (segment_id_at(mi_r, mi_c, fctx), segment_id_at(pv_mi_r, pv_mi_c, fctx))
17743+ let (cur_seg, pv_seg) = if c.seg_lf_active {
17744+ (segment_id_at(mi_r, mi_c, c.fctx), segment_id_at(pv_mi_r, pv_mi_c, c.fctx))
1767417745 } else {
1767517746 (0, 0)
1767617747 };
17677- let cur_level = lf_level(lf, plane_idx, dir, cur_ref, cur_delta_lf, cur_seg, fctx);
17678- let pv_level = lf_level(lf, plane_idx, dir, pv_ref, pv_delta_lf, pv_seg, fctx);
17748+ let cur_level = c.level(cur_ref, cur_delta_lf, cur_seg);
17749+ let pv_level = c.level(pv_ref, pv_delta_lf, pv_seg);
1767917750 if cur_level == 0 && pv_level == 0 {
1768017751 return None;
1768117752 }
1768217753 let dim = unit_log2(cur_tx).min(unit_log2(pv_tx)).clamp(0, 4) as usize;
17683- let len: u8 = if chroma {
17754+ let len: u8 = if c.chroma {
1768417755 if dim == 0 { 4 } else { 6 }
1768517756 } else {
1768617757 [4, 8, 14, 14, 14][dim]
@@ -18141,39 +18212,41 @@ mod deblock_simd {
1814118212 }
1814218213 }
1814318214
18144- /// One wide-kernel row set, unrolled so each `wsum` sees its weight row
18145- /// as a constant.
18146- macro_rules! wrows {
18147- ($r:expr, $off:expr, $tbl:ident, $taps:expr, $shift:literal, $flat:expr, $($j:literal),*) => {
18148- $(
18149- $r[$off + $j] = _mm256_blendv_epi8($r[$off + $j], wsum::<_, $shift>(&$taps, &$tbl[$j]), $flat);
18150- )*
18151- };
18152- }
18153-
1815418215 /// [`super::filter_edge`]'s body for 16 lines held as `t[0..16]`, `q0` in
18155- /// `t[8]`; the filtered taps are written back in place.
18216+ /// `t[8]`; the filtered taps are written back in place. Returns `false`
18217+ /// when the filter mask is empty on every one of the 16 lines -- nothing
18218+ /// was written and the caller can skip the whole write-back.
1815618219 ///
1815718220 /// # Safety
1815818221 /// Requires AVX2.
18222+ ///
18223+ /// lane-deblock2: `LEN` is a const parameter, so each edge length gets
18224+ /// its own kernel holding only its own taps, and every wide row is
18225+ /// clamped straight into `t` instead of into a shared twelve-entry
18226+ /// result array (which the register allocator spilled -- 35% of this
18227+ /// kernel's samples were `vmovdqa` to the stack).
1815918228 #[target_feature(enable = "avx2")]
1816018229 #[allow(clippy::too_many_arguments)]
18161- unsafe fn filter16(
18230+ unsafe fn filter16<const LEN: u8>(
1816218231 t: &mut [__m256i; 16],
18163- len: u8,
1816418232 blimit: i32,
1816518233 limit: i32,
1816618234 hev_thr: i32,
1816718235 flat_thresh: i32,
1816818236 scale: i32,
1816918237 smax: i32,
18170- ) {
18238+ ) -> bool {
1817118239 let lim = _mm256_set1_epi16(limit as i16);
1817218240 let blim = _mm256_set1_epi16(blimit as i16);
1817318241 let hthr = _mm256_set1_epi16(hev_thr as i16);
1817418242 let fthr = _mm256_set1_epi16(flat_thresh as i16);
1817518243 let zero = _mm256_setzero_si256();
1817618244 let smaxv = _mm256_set1_epi16(smax as i16);
18245+ macro_rules! put {
18246+ ($i:expr, $v:expr) => {
18247+ t[$i] = _mm256_min_epi16(_mm256_max_epi16($v, zero), smaxv);
18248+ };
18249+ }
1817718250 let (p6, p5, p4, p3, p2, p1, p0) = (t[1], t[2], t[3], t[4], t[5], t[6], t[7]);
1817818251 let (q0, q1, q2, q3, q4, q5, q6) = (t[8], t[9], t[10], t[11], t[12], t[13], t[14]);
1817918252
@@ -18181,10 +18254,10 @@ mod deblock_simd {
1818118254 // reaches, plus the shared `blimit` term.
1818218255 let d1 = _mm256_max_epi16(ad(p1, p0), ad(q1, q0));
1818318256 let mut md = d1;
18184- if len >= 6 {
18257+ if LEN >= 6 {
1818518258 md = _mm256_max_epi16(md, _mm256_max_epi16(ad(p2, p1), ad(q2, q1)));
1818618259 }
18187- if len >= 8 {
18260+ if LEN >= 8 {
1818818261 md = _mm256_max_epi16(md, _mm256_max_epi16(ad(p3, p2), ad(q3, q2)));
1818918262 }
1819018263 let bl = _mm256_add_epi16(
@@ -18192,6 +18265,14 @@ mod deblock_simd {
1819218265 _mm256_srli_epi16(ad(p1, q1), 1),
1819318266 );
1819418267 let mask = _mm256_and_si256(le(md, lim), le(bl, blim));
18268+ // lane-deblock2: an empty mask leaves every tap at its loaded value
18269+ // (the flat masks are `& mask`, so they are empty too, and the final
18270+ // clamp is the identity on samples that came out of the plane), so
18271+ // the whole write-back -- including the vertical edge's second
18272+ // transpose -- is dead.
18273+ if _mm256_testz_si256(mask, mask) != 0 {
18274+ return false;
18275+ }
1819518276 let hev = _mm256_cmpgt_epi16(d1, hthr);
1819618277
1819718278 // `filter4`, in the centred domain.
@@ -18221,110 +18302,137 @@ mod deblock_simd {
1822118302 let n_p0 = _mm256_add_epi16(sclamp(_mm256_add_epi16(ps0, f2), lo, hi), centre);
1822218303 let n_q0 = _mm256_add_epi16(sclamp(_mm256_sub_epi16(qs0, f1), lo, hi), centre);
1822318304 let n_q1 = _mm256_add_epi16(sclamp(_mm256_sub_epi16(qs1, tap), lo, hi), centre);
18224- let mut r = [
18225- p5,
18226- p4,
18227- p3,
18228- p2,
18229- _mm256_blendv_epi8(p1, n_p1, mask),
18230- _mm256_blendv_epi8(p0, n_p0, mask),
18231- _mm256_blendv_epi8(q0, n_q0, mask),
18232- _mm256_blendv_epi8(q1, n_q1, mask),
18233- q2,
18234- q3,
18235- q4,
18236- q5,
18237- ];
18305+ let b_p1 = _mm256_blendv_epi8(p1, n_p1, mask);
18306+ let b_p0 = _mm256_blendv_epi8(p0, n_p0, mask);
18307+ let b_q0 = _mm256_blendv_epi8(q0, n_q0, mask);
18308+ let b_q1 = _mm256_blendv_epi8(q1, n_q1, mask);
18309+ if LEN == 4 {
18310+ put!(6, b_p1);
18311+ put!(7, b_p0);
18312+ put!(8, b_q0);
18313+ put!(9, b_q1);
18314+ return true;
18315+ }
1823818316
18239- if len >= 6 {
18240- // `highbd_flat_mask4`, over the same reach as this length's mask.
18241- let mut fl = _mm256_max_epi16(
18242- _mm256_max_epi16(ad(p1, p0), ad(q1, q0)),
18243- _mm256_max_epi16(ad(p2, p0), ad(q2, q0)),
18244- );
18245- if len >= 8 {
18246- fl = _mm256_max_epi16(fl, _mm256_max_epi16(ad(p3, p0), ad(q3, q0)));
18247- }
18248- let flat = _mm256_and_si256(le(fl, fthr), mask);
18249- if len == 6 {
18317+ // `highbd_flat_mask4`, over the same reach as this length's mask.
18318+ let mut fl = _mm256_max_epi16(
18319+ _mm256_max_epi16(ad(p1, p0), ad(q1, q0)),
18320+ _mm256_max_epi16(ad(p2, p0), ad(q2, q0)),
18321+ );
18322+ if LEN >= 8 {
18323+ fl = _mm256_max_epi16(fl, _mm256_max_epi16(ad(p3, p0), ad(q3, q0)));
18324+ }
18325+ let flat = _mm256_and_si256(le(fl, fthr), mask);
18326+ let any_flat = _mm256_testz_si256(flat, flat) == 0;
18327+ if LEN == 6 {
18328+ if any_flat {
1825018329 let taps = [p2, p1, p0, q0, q1, q2];
18251- wrows!(r, 4, W6, taps, 3, flat, 0, 1, 2, 3);
18330+ put!(6, _mm256_blendv_epi8(b_p1, wsum::<_, 3>(&taps, &W6[0]), flat));
18331+ put!(7, _mm256_blendv_epi8(b_p0, wsum::<_, 3>(&taps, &W6[1]), flat));
18332+ put!(8, _mm256_blendv_epi8(b_q0, wsum::<_, 3>(&taps, &W6[2]), flat));
18333+ put!(9, _mm256_blendv_epi8(b_q1, wsum::<_, 3>(&taps, &W6[3]), flat));
1825218334 } else {
18253- let taps = [p3, p2, p1, p0, q0, q1, q2, q3];
18254- wrows!(r, 3, W8, taps, 3, flat, 0, 1, 2, 3, 4, 5);
18255- if len > 8 {
18256- let fl2 = _mm256_max_epi16(
18257- _mm256_max_epi16(
18258- _mm256_max_epi16(ad(p4, p0), ad(q4, q0)),
18259- _mm256_max_epi16(ad(p5, p0), ad(q5, q0)),
18260- ),
18261- _mm256_max_epi16(ad(p6, p0), ad(q6, q0)),
18262- );
18263- let flat2 = _mm256_and_si256(le(fl2, fthr), flat);
18264- // lane-deblock: `filter14`'s twelve rows as a RUNNING
18265- // sum. Consecutive `W14` rows differ by three or four
18266- // taps (row j+1 - row j is a constant vector with two
18267- // negative and two positive entries), so each output
18268- // costs one add, one sub and the rounding shift instead
18269- // of a fresh 14-tap weighted sum -- 168 tap operations
18270- // become 44. Exact: every partial sum is taken mod
18271- // 2^16 by `add/sub_epi16` and every ROW sum is a real
18272- // `W14` row, at most `16 * 4095 + 8 < 2^16`, so the
18273- // unsigned rounding shift sees the true value.
18274- // `deblock_simd_kernel_matches_the_scalar_filter` pins
18275- // it against `filter_edge` at 8/10/12-bit.
18276- let mut s = _mm256_add_epi16(
18277- _mm256_add_epi16(
18278- _mm256_mullo_epi16(p6, _mm256_set1_epi16(7)),
18279- _mm256_slli_epi16(_mm256_add_epi16(p5, p4), 1),
18280- ),
18281- _mm256_add_epi16(
18282- _mm256_add_epi16(_mm256_add_epi16(p3, p2), _mm256_add_epi16(p1, p0)),
18283- q0,
18284- ),
18285- );
18286- macro_rules! row14 {
18287- ($idx:literal) => {
18288- r[$idx] = _mm256_blendv_epi8(
18289- r[$idx],
18290- _mm256_srli_epi16(
18291- _mm256_add_epi16(s, _mm256_set1_epi16(8)),
18292- 4,
18293- ),
18294- flat2,
18295- );
18296- };
18297- ($idx:literal, $plus:expr, $minus:expr) => {
18298- s = _mm256_sub_epi16(_mm256_add_epi16(s, $plus), $minus);
18299- row14!($idx);
18300- };
18301- }
18302- let add2 = |a, b| _mm256_add_epi16(a, b);
18303- row14!(0);
18304- row14!(1, add2(p3, q1), _mm256_slli_epi16(p6, 1));
18305- row14!(2, add2(p2, q2), add2(p6, p5));
18306- row14!(3, add2(p1, q3), add2(p6, p4));
18307- row14!(4, add2(p0, q4), add2(p6, p3));
18308- row14!(5, add2(q0, q5), add2(p6, p2));
18309- row14!(6, add2(q1, q6), add2(p6, p1));
18310- row14!(7, add2(q2, q6), add2(p5, p0));
18311- row14!(8, add2(q3, q6), add2(p4, q0));
18312- row14!(9, add2(q4, q6), add2(p3, q1));
18313- row14!(10, add2(q5, q6), add2(p2, q2));
18314- row14!(11, _mm256_slli_epi16(q6, 1), add2(p1, q3));
18315- }
18335+ put!(6, b_p1);
18336+ put!(7, b_p0);
18337+ put!(8, b_q0);
18338+ put!(9, b_q1);
1831618339 }
18340+ return true;
1831718341 }
1831818342
18319- // `r[j]` holds the sample at tap `j - 6`, i.e. `t[j + 2]`.
18320- let (a, b) = match len {
18321- 4 | 6 => (4, 8),
18322- 8 => (3, 9),
18323- _ => (0, 12),
18343+ // `filter8`'s six rows over `p2..q2`, the wide kernel's base.
18344+ let (o0, o1, o2, o3, o4, o5) = if any_flat {
18345+ let taps = [p3, p2, p1, p0, q0, q1, q2, q3];
18346+ (
18347+ _mm256_blendv_epi8(p2, wsum::<_, 3>(&taps, &W8[0]), flat),
18348+ _mm256_blendv_epi8(b_p1, wsum::<_, 3>(&taps, &W8[1]), flat),
18349+ _mm256_blendv_epi8(b_p0, wsum::<_, 3>(&taps, &W8[2]), flat),
18350+ _mm256_blendv_epi8(b_q0, wsum::<_, 3>(&taps, &W8[3]), flat),
18351+ _mm256_blendv_epi8(b_q1, wsum::<_, 3>(&taps, &W8[4]), flat),
18352+ _mm256_blendv_epi8(q2, wsum::<_, 3>(&taps, &W8[5]), flat),
18353+ )
18354+ } else {
18355+ (p2, b_p1, b_p0, b_q0, b_q1, q2)
1832418356 };
18325- for j in a..b {
18326- t[j + 2] = _mm256_min_epi16(_mm256_max_epi16(r[j], zero), smaxv);
18357+ macro_rules! put8 {
18358+ () => {
18359+ put!(5, o0);
18360+ put!(6, o1);
18361+ put!(7, o2);
18362+ put!(8, o3);
18363+ put!(9, o4);
18364+ put!(10, o5);
18365+ };
18366+ }
18367+ if LEN == 8 {
18368+ put8!();
18369+ return true;
18370+ }
18371+
18372+ let fl2 = _mm256_max_epi16(
18373+ _mm256_max_epi16(
18374+ _mm256_max_epi16(ad(p4, p0), ad(q4, q0)),
18375+ _mm256_max_epi16(ad(p5, p0), ad(q5, q0)),
18376+ ),
18377+ _mm256_max_epi16(ad(p6, p0), ad(q6, q0)),
18378+ );
18379+ let flat2 = _mm256_and_si256(le(fl2, fthr), flat);
18380+ if _mm256_testz_si256(flat2, flat2) != 0 {
18381+ // No line takes the wide rows: `t[2..5]`/`t[11..14]` keep the
18382+ // taps they were loaded with, exactly as the blend would.
18383+ put8!();
18384+ return true;
1832718385 }
18386+ // lane-deblock: `filter14`'s twelve rows as a RUNNING sum.
18387+ // Consecutive `W14` rows differ by three or four taps (row j+1 - row
18388+ // j is a constant vector with two negative and two positive
18389+ // entries), so each output costs one add, one sub and the rounding
18390+ // shift instead of a fresh 14-tap weighted sum -- 168 tap operations
18391+ // become 44. Exact: every partial sum is taken mod 2^16 by
18392+ // `add/sub_epi16` and every ROW sum is a real `W14` row, at most
18393+ // `16 * 4095 + 8 < 2^16`, so the unsigned rounding shift sees the
18394+ // true value. `deblock_simd_kernel_matches_the_scalar_filter` pins
18395+ // it against `filter_edge` at 8/10/12-bit.
18396+ let mut s = _mm256_add_epi16(
18397+ _mm256_add_epi16(
18398+ _mm256_mullo_epi16(p6, _mm256_set1_epi16(7)),
18399+ _mm256_slli_epi16(_mm256_add_epi16(p5, p4), 1),
18400+ ),
18401+ _mm256_add_epi16(
18402+ _mm256_add_epi16(_mm256_add_epi16(p3, p2), _mm256_add_epi16(p1, p0)),
18403+ q0,
18404+ ),
18405+ );
18406+ let add2 = |a, b| _mm256_add_epi16(a, b);
18407+ macro_rules! row14 {
18408+ ($idx:literal, $base:expr) => {
18409+ put!(
18410+ $idx,
18411+ _mm256_blendv_epi8(
18412+ $base,
18413+ _mm256_srli_epi16(_mm256_add_epi16(s, _mm256_set1_epi16(8)), 4),
18414+ flat2,
18415+ )
18416+ );
18417+ };
18418+ ($idx:literal, $base:expr, $plus:expr, $minus:expr) => {
18419+ s = _mm256_sub_epi16(_mm256_add_epi16(s, $plus), $minus);
18420+ row14!($idx, $base);
18421+ };
18422+ }
18423+ row14!(2, p5);
18424+ row14!(3, p4, add2(p3, q1), _mm256_slli_epi16(p6, 1));
18425+ row14!(4, p3, add2(p2, q2), add2(p6, p5));
18426+ row14!(5, o0, add2(p1, q3), add2(p6, p4));
18427+ row14!(6, o1, add2(p0, q4), add2(p6, p3));
18428+ row14!(7, o2, add2(q0, q5), add2(p6, p2));
18429+ row14!(8, o3, add2(q1, q6), add2(p6, p1));
18430+ row14!(9, o4, add2(q2, q6), add2(p5, p0));
18431+ row14!(10, o5, add2(q3, q6), add2(p4, q0));
18432+ row14!(11, q3, add2(q4, q6), add2(p3, q1));
18433+ row14!(12, q4, add2(q5, q6), add2(p2, q2));
18434+ row14!(13, q5, _mm256_slli_epi16(q6, 1), add2(p1, q3));
18435+ true
1832818436 }
1832918437
1833018438 /// 16 edge lines at `base`. `vertical` names the edge: a vertical edge's
@@ -18372,7 +18480,17 @@ mod deblock_simd {
1837218480 t[i] = _mm256_loadu_si256(p.add(top + i * stride).cast());
1837318481 }
1837418482 }
18375- filter16(&mut t, len, blimit, limit, hev_thr, flat_thresh, scale, smax);
18483+ // lane-deblock2: one monomorphised kernel per edge length, and an
18484+ // empty filter mask returns without any write-back at all.
18485+ let modified = match len {
18486+ 4 => filter16::<4>(&mut t, blimit, limit, hev_thr, flat_thresh, scale, smax),
18487+ 6 => filter16::<6>(&mut t, blimit, limit, hev_thr, flat_thresh, scale, smax),
18488+ 8 => filter16::<8>(&mut t, blimit, limit, hev_thr, flat_thresh, scale, smax),
18489+ _ => filter16::<14>(&mut t, blimit, limit, hev_thr, flat_thresh, scale, smax),
18490+ };
18491+ if !modified {
18492+ return;
18493+ }
1837618494 if vertical {
1837718495 transpose16(&mut t);
1837818496 for j in 0..16 {
@@ -18512,13 +18630,16 @@ fn deblock_plane(
1851218630 // no other band touches (see `Shared`'s own corner-cut note).
1851318631 #[allow(unsafe_code)]
1851418632 let plane = unsafe { sp.get() };
18633+ // lane-deblock2: this band's own level table and pass constants,
18634+ // built once per band instead of re-derived per 4-pixel edge.
18635+ let vctx = EdgeCtx::new(lf, n, plane_idx, 0, chroma, seg_lf_active, fctx);
1851518636 let mut x0 = 4usize;
1851618637 while x0 < tw {
1851718638 let mut y0 = y_lo;
1851818639 while y0 < y_hi {
1851918640 if y0 + 16 <= y_hi && x0 >= 8 && x0 + 8 <= stride {
1852018641 let ps: [Option<(u8, i32)>; 4] =
18521- std::array::from_fn(|k| edge_params(lf, n, plane_idx, 0, chroma, x0, y0 + 4 * k, seg_lf_active, fctx));
18642+ std::array::from_fn(|k| edge_params(&vctx, x0, y0 + 4 * k));
1852218643 if let Some((len, level)) = ps[0] {
1852318644 if ps.iter().all(|p| *p == ps[0])
1852418645 && filter_edge16(
@@ -18551,7 +18672,7 @@ fn deblock_plane(
1855118672 y0 += 16;
1855218673 continue;
1855318674 }
18554- if let Some((len, level)) = edge_params(lf, n, plane_idx, 0, chroma, x0, y0, seg_lf_active, fctx) {
18675+ if let Some((len, level)) = edge_params(&vctx, x0, y0) {
1855518676 if crate::envflags::env_flag!("EC_AV1_DEBLOCK_TRACE_V") && plane_idx == 0 {
1855618677 eprintln!("VEDGE x0={x0} y0={y0} len={len} level={level}");
1855718678 }
@@ -18580,13 +18701,14 @@ fn deblock_plane(
1858018701 // SAFETY: this band writes only columns `x_lo..x_hi`.
1858118702 #[allow(unsafe_code)]
1858218703 let plane = unsafe { sp.get() };
18704+ let hctx = EdgeCtx::new(lf, n, plane_idx, 1, chroma, seg_lf_active, fctx);
1858318705 let mut y0 = 4usize;
1858418706 while y0 < th {
1858518707 let mut x0 = x_lo;
1858618708 while x0 < x_hi {
1858718709 if x0 + 16 <= x_hi && y0 >= 8 && y0 + 7 <= rows {
1858818710 let ps: [Option<(u8, i32)>; 4] =
18589- std::array::from_fn(|k| edge_params(lf, n, plane_idx, 1, chroma, x0 + 4 * k, y0, seg_lf_active, fctx));
18711+ std::array::from_fn(|k| edge_params(&hctx, x0 + 4 * k, y0));
1859018712 if let Some((len, level)) = ps[0] {
1859118713 if ps.iter().all(|p| *p == ps[0])
1859218714 && filter_edge16(
@@ -18619,7 +18741,7 @@ fn deblock_plane(
1861918741 x0 += 16;
1862018742 continue;
1862118743 }
18622- if let Some((len, level)) = edge_params(lf, n, plane_idx, 1, chroma, x0, y0, seg_lf_active, fctx) {
18744+ if let Some((len, level)) = edge_params(&hctx, x0, y0) {
1862318745 if crate::envflags::env_flag!("EC_AV1_DEBLOCK_TRACE")
1862418746 && plane_idx == 0
1862518747 && x0 == 44