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+260 -138
@@ -17561,26 +17561,105 @@ fn lf_level(
17561 /// edge" suppression (which only ever fires at a non-PU-edge TU boundary)17561 /// edge" suppression (which only ever fires at a non-PU-edge TU boundary)
17562 /// never applies here, letting this skip the `skip`/`skip_txfm` lookup17562 /// never applies here, letting this skip the `skip`/`skip_txfm` lookup
17563 /// entirely. Returns `None` when this 4-pixel edge group is not filtered.17563 /// entirely. Returns `None` when this 4-pixel edge group is not filtered.
17564-#[inline(always)]17564+/// One plane pass's worth of [`edge_params`] constants (lane-deblock2): the
17565-fn edge_params(17565+/// seven values that used to be re-passed per 4-pixel edge group -- half of
17566- lf: &LoopFilterParams,17566+/// them on the stack, into an `inline(never)` body -- plus libaom's
17567- n: &Neighbours,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,
17568 plane_idx: usize,17574 plane_idx: usize,
17569 dir: usize,17575 dir: usize,
17570 chroma: bool,17576 chroma: bool,
17571- x0: usize,17577+ seg_lf_active: bool,
17572- y0: usize,17578+ /// `delta_lf_grid`'s column for this plane and direction.
17573- // lane-perf9: `segmentation_enabled`, read once per plane pass instead of17579+ delta_idx: usize,
17574- // four thread-local reads (`SEG_MI_DIMS`, `SEG_IDS`, twice each) plus two17580+ /// `mode_step` (`set_lpf_parameters`): one mi for luma, `1 << ss` for
17575- // `SEG` borrows per 4x4 edge. With it clear, every segment id is 0 and no17581+ /// chroma.
17576- // `SEG_LVL_ALT_LF_*` feature can be active, so the levels are unchanged.17582+ step: usize,
17577- seg_lf_active: bool, fctx: &crate::decode::FrameCtx,17583+ /// This pass's own `lf.level[..]`, the LUT's base.
17578-) -> Option<(u8, i32)> {17584+ base0: i32,
17579- let (mi_r, mi_c) = (plane_to_mi(chroma, y0), plane_to_mi(chroma, x0));17585+ /// `lf_level` at `[base level][ref frame + 7]`, with no segment
17580- let cur_tx = if dir == 0 {17586+ /// override -- the whole domain, since a level is `0..=63` and a ref
17581- tx_px_at(n, chroma, mi_r, mi_c)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]
17582 } else {17606 } else {
17583- tx_h_px_at(n, chroma, mi_r, mi_c)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)
17661+ } else {
17662+ tx_h_px_at(c.n, c.chroma, mi_r, mi_c)
17584 };17663 };
17585 // lane-deblock: `cur_tx` is a power-of-two pixel size, so the alignment17664 // lane-deblock: `cur_tx` is a power-of-two pixel size, so the alignment
17586 // test is a mask, not the hardware `divl` this used to emit (10.7% of17665 // test is a mask, not the hardware `divl` this used to emit (10.7% of
@@ -17590,49 +17669,41 @@ fn edge_params(
17590 // the covering transform does not start at), so this head is inlined17669 // the covering transform does not start at), so this head is inlined
17591 // into the deblock loops and only survivors pay the call and the17670 // into the deblock loops and only survivors pay the call and the
17592 // register-spilling prologue of the body below.17671 // 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 {
17594 return None;17673 return None;
17595 }17674 }
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)
17597 }17676 }
17598 17677
17599 /// [`edge_params`] past its transform-alignment gate.17678 /// [`edge_params`] past its transform-alignment gate.
17600-#[allow(clippy::too_many_arguments)]
17601 #[inline(never)]17679 #[inline(never)]
17602 fn edge_params_body(17680 fn edge_params_body(
17603- lf: &LoopFilterParams,17681+ c: &EdgeCtx,
17604- n: &Neighbours,
17605- plane_idx: usize,
17606- dir: usize,
17607- chroma: bool,
17608 x0: usize,17682 x0: usize,
17609 y0: usize,17683 y0: usize,
17610 mi_r: usize,17684 mi_r: usize,
17611 mi_c: usize,17685 mi_c: usize,
17612 cur_tx: u32,17686 cur_tx: u32,
17613- seg_lf_active: bool,
17614- fctx: &crate::decode::FrameCtx,
17615 ) -> Option<(u8, i32)> {17687 ) -> Option<(u8, i32)> {
17688+ let n = c.n;
17616 let cur_ref = n.ref_grid[mi_r * n.skip_grid_cols_mi + mi_c];17689 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` for17690+ // The previous cell must keep the odd parity `plane_to_mi` just
17618- // chroma -- the previous cell must keep the odd parity `plane_to_mi`17691+ // established, i.e. the bottom/right mi of the PREVIOUS 8x8 luma group,
17619- // just established, i.e. the bottom/right mi of the PREVIOUS 8x8 luma17692+ // not the other half of this one.
17620- // group, not the other half of this one.17693+ let (pv_mi_r, pv_mi_c) = if c.dir == 0 {
17621- let step = if chroma { 2 } else { 1 };17694+ (mi_r, mi_c - c.step)
17622- let (pv_mi_r, pv_mi_c) = if dir == 0 {
17623- (mi_r, mi_c - step)
17624 } else {17695 } else {
17625- (mi_r - step, mi_c)17696+ (mi_r - c.step, mi_c)
17626 };17697 };
17627- let pv_tx = if dir == 0 {17698+ let pv_tx = if c.dir == 0 {
17628- tx_px_at(n, chroma, pv_mi_r, pv_mi_c)17699+ tx_px_at(n, c.chroma, pv_mi_r, pv_mi_c)
17629 } else {17700 } 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)
17631 };17702 };
17632 let pv_ref = n.ref_grid[pv_mi_r * n.skip_grid_cols_mi + pv_mi_c];17703 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 };17704+ let cur_delta_lf = i32::from(n.delta_lf_grid[mi_r * n.skip_grid_cols_mi + mi_c][c.delta_idx]);
17634- let cur_delta_lf = i32::from(n.delta_lf_grid[mi_r * n.skip_grid_cols_mi + mi_c][delta_idx]);17705+ let pv_delta_lf =
17635- let pv_delta_lf = i32::from(n.delta_lf_grid[pv_mi_r * n.skip_grid_cols_mi + pv_mi_c][delta_idx]);17706+ i32::from(n.delta_lf_grid[pv_mi_r * n.skip_grid_cols_mi + pv_mi_c][c.delta_idx]);
17636 17707
17637 // spec 7.14.2 / libaom `set_lpf_parameters`: a transform edge between two17708 // spec 7.14.2 / libaom `set_lpf_parameters`: a transform edge between two
17638 // SKIPPED INTER blocks is filtered only when it is also a prediction17709 // SKIPPED INTER blocks is filtered only when it is also a prediction
@@ -17650,16 +17721,16 @@ fn edge_params_body(
17650 // size, which a 4x4 luma block read at the `| 1` mi is not.17721 // size, which a 4x4 luma block read at the `| 1` mi is not.
17651 let cur_org = n.blk_org_grid[mi_r * n.skip_grid_cols_mi + mi_c];17722 let cur_org = n.blk_org_grid[mi_r * n.skip_grid_cols_mi + mi_c];
17652 let cur_dim = n.blk_dim_grid[mi_r * n.skip_grid_cols_mi + mi_c];17723 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 {
17654 (usize::from(cur_org[1]), usize::from(cur_dim[0]))17725 (usize::from(cur_org[1]), usize::from(cur_dim[0]))
17655 } else {17726 } else {
17656 (usize::from(cur_org[0]), usize::from(cur_dim[1]))17727 (usize::from(cur_org[0]), usize::from(cur_dim[1]))
17657 };17728 };
17658- let pu_edge = if chroma {17729+ let pu_edge = if c.chroma {
17659 let plane_dim = (dim_mi * MI / 2).max(4);17730 let plane_dim = (dim_mi * MI / 2).max(4);
17660- (if dir == 0 { x0 } else { y0 }) % plane_dim == 017731+ (if c.dir == 0 { x0 } else { y0 }) % plane_dim == 0
17661 } else {17732 } else {
17662- plane_to_mi(false, if dir == 0 { x0 } else { y0 }) == org_px17733+ plane_to_mi(false, if c.dir == 0 { x0 } else { y0 }) == org_px
17663 };17734 };
17664 if !pu_edge17735 if !pu_edge
17665 && cur_ref != 017736 && cur_ref != 0
@@ -17669,18 +17740,18 @@ fn edge_params_body(
17669 {17740 {
17670 return None;17741 return None;
17671 }17742 }
17672- let (cur_seg, pv_seg) = if seg_lf_active {17743+ let (cur_seg, pv_seg) = if c.seg_lf_active {
17673- (segment_id_at(mi_r, mi_c, fctx), segment_id_at(pv_mi_r, pv_mi_c, fctx))17744+ (segment_id_at(mi_r, mi_c, c.fctx), segment_id_at(pv_mi_r, pv_mi_c, c.fctx))
17674 } else {17745 } else {
17675 (0, 0)17746 (0, 0)
17676 };17747 };
17677- let cur_level = lf_level(lf, plane_idx, dir, cur_ref, cur_delta_lf, cur_seg, fctx);17748+ let cur_level = c.level(cur_ref, cur_delta_lf, cur_seg);
17678- let pv_level = lf_level(lf, plane_idx, dir, pv_ref, pv_delta_lf, pv_seg, fctx);17749+ let pv_level = c.level(pv_ref, pv_delta_lf, pv_seg);
17679 if cur_level == 0 && pv_level == 0 {17750 if cur_level == 0 && pv_level == 0 {
17680 return None;17751 return None;
17681 }17752 }
17682 let dim = unit_log2(cur_tx).min(unit_log2(pv_tx)).clamp(0, 4) as usize;17753 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 {
17684 if dim == 0 { 4 } else { 6 }17755 if dim == 0 { 4 } else { 6 }
17685 } else {17756 } else {
17686 [4, 8, 14, 14, 14][dim]17757 [4, 8, 14, 14, 14][dim]
@@ -18141,39 +18212,41 @@ mod deblock_simd {
18141 }18212 }
18142 }18213 }
18143 18214
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-
18154 /// [`super::filter_edge`]'s body for 16 lines held as `t[0..16]`, `q0` in18215 /// [`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.
18156 ///18219 ///
18157 /// # Safety18220 /// # Safety
18158 /// Requires AVX2.18221 /// 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).
18159 #[target_feature(enable = "avx2")]18228 #[target_feature(enable = "avx2")]
18160 #[allow(clippy::too_many_arguments)]18229 #[allow(clippy::too_many_arguments)]
18161- unsafe fn filter16(18230+ unsafe fn filter16<const LEN: u8>(
18162 t: &mut [__m256i; 16],18231 t: &mut [__m256i; 16],
18163- len: u8,
18164 blimit: i32,18232 blimit: i32,
18165 limit: i32,18233 limit: i32,
18166 hev_thr: i32,18234 hev_thr: i32,
18167 flat_thresh: i32,18235 flat_thresh: i32,
18168 scale: i32,18236 scale: i32,
18169 smax: i32,18237 smax: i32,
18170- ) {18238+ ) -> bool {
18171 let lim = _mm256_set1_epi16(limit as i16);18239 let lim = _mm256_set1_epi16(limit as i16);
18172 let blim = _mm256_set1_epi16(blimit as i16);18240 let blim = _mm256_set1_epi16(blimit as i16);
18173 let hthr = _mm256_set1_epi16(hev_thr as i16);18241 let hthr = _mm256_set1_epi16(hev_thr as i16);
18174 let fthr = _mm256_set1_epi16(flat_thresh as i16);18242 let fthr = _mm256_set1_epi16(flat_thresh as i16);
18175 let zero = _mm256_setzero_si256();18243 let zero = _mm256_setzero_si256();
18176 let smaxv = _mm256_set1_epi16(smax as i16);18244 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+ }
18177 let (p6, p5, p4, p3, p2, p1, p0) = (t[1], t[2], t[3], t[4], t[5], t[6], t[7]);18250 let (p6, p5, p4, p3, p2, p1, p0) = (t[1], t[2], t[3], t[4], t[5], t[6], t[7]);
18178 let (q0, q1, q2, q3, q4, q5, q6) = (t[8], t[9], t[10], t[11], t[12], t[13], t[14]);18251 let (q0, q1, q2, q3, q4, q5, q6) = (t[8], t[9], t[10], t[11], t[12], t[13], t[14]);
18179 18252
@@ -18181,10 +18254,10 @@ mod deblock_simd {
18181 // reaches, plus the shared `blimit` term.18254 // reaches, plus the shared `blimit` term.
18182 let d1 = _mm256_max_epi16(ad(p1, p0), ad(q1, q0));18255 let d1 = _mm256_max_epi16(ad(p1, p0), ad(q1, q0));
18183 let mut md = d1;18256 let mut md = d1;
18184- if len >= 6 {18257+ if LEN >= 6 {
18185 md = _mm256_max_epi16(md, _mm256_max_epi16(ad(p2, p1), ad(q2, q1)));18258 md = _mm256_max_epi16(md, _mm256_max_epi16(ad(p2, p1), ad(q2, q1)));
18186 }18259 }
18187- if len >= 8 {18260+ if LEN >= 8 {
18188 md = _mm256_max_epi16(md, _mm256_max_epi16(ad(p3, p2), ad(q3, q2)));18261 md = _mm256_max_epi16(md, _mm256_max_epi16(ad(p3, p2), ad(q3, q2)));
18189 }18262 }
18190 let bl = _mm256_add_epi16(18263 let bl = _mm256_add_epi16(
@@ -18192,6 +18265,14 @@ mod deblock_simd {
18192 _mm256_srli_epi16(ad(p1, q1), 1),18265 _mm256_srli_epi16(ad(p1, q1), 1),
18193 );18266 );
18194 let mask = _mm256_and_si256(le(md, lim), le(bl, blim));18267 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+ }
18195 let hev = _mm256_cmpgt_epi16(d1, hthr);18276 let hev = _mm256_cmpgt_epi16(d1, hthr);
18196 18277
18197 // `filter4`, in the centred domain.18278 // `filter4`, in the centred domain.
@@ -18221,38 +18302,73 @@ mod deblock_simd {
18221 let n_p0 = _mm256_add_epi16(sclamp(_mm256_add_epi16(ps0, f2), lo, hi), centre);18302 let n_p0 = _mm256_add_epi16(sclamp(_mm256_add_epi16(ps0, f2), lo, hi), centre);
18222 let n_q0 = _mm256_add_epi16(sclamp(_mm256_sub_epi16(qs0, f1), lo, hi), centre);18303 let n_q0 = _mm256_add_epi16(sclamp(_mm256_sub_epi16(qs0, f1), lo, hi), centre);
18223 let n_q1 = _mm256_add_epi16(sclamp(_mm256_sub_epi16(qs1, tap), lo, hi), centre);18304 let n_q1 = _mm256_add_epi16(sclamp(_mm256_sub_epi16(qs1, tap), lo, hi), centre);
18224- let mut r = [18305+ let b_p1 = _mm256_blendv_epi8(p1, n_p1, mask);
18225- p5,18306+ let b_p0 = _mm256_blendv_epi8(p0, n_p0, mask);
18226- p4,18307+ let b_q0 = _mm256_blendv_epi8(q0, n_q0, mask);
18227- p3,18308+ let b_q1 = _mm256_blendv_epi8(q1, n_q1, mask);
18228- p2,18309+ if LEN == 4 {
18229- _mm256_blendv_epi8(p1, n_p1, mask),18310+ put!(6, b_p1);
18230- _mm256_blendv_epi8(p0, n_p0, mask),18311+ put!(7, b_p0);
18231- _mm256_blendv_epi8(q0, n_q0, mask),18312+ put!(8, b_q0);
18232- _mm256_blendv_epi8(q1, n_q1, mask),18313+ put!(9, b_q1);
18233- q2,18314+ return true;
18234- q3,18315+ }
18235- q4,
18236- q5,
18237- ];
18238 18316
18239- if len >= 6 {
18240 // `highbd_flat_mask4`, over the same reach as this length's mask.18317 // `highbd_flat_mask4`, over the same reach as this length's mask.
18241 let mut fl = _mm256_max_epi16(18318 let mut fl = _mm256_max_epi16(
18242 _mm256_max_epi16(ad(p1, p0), ad(q1, q0)),18319 _mm256_max_epi16(ad(p1, p0), ad(q1, q0)),
18243 _mm256_max_epi16(ad(p2, p0), ad(q2, q0)),18320 _mm256_max_epi16(ad(p2, p0), ad(q2, q0)),
18244 );18321 );
18245- if len >= 8 {18322+ if LEN >= 8 {
18246 fl = _mm256_max_epi16(fl, _mm256_max_epi16(ad(p3, p0), ad(q3, q0)));18323 fl = _mm256_max_epi16(fl, _mm256_max_epi16(ad(p3, p0), ad(q3, q0)));
18247 }18324 }
18248 let flat = _mm256_and_si256(le(fl, fthr), mask);18325 let flat = _mm256_and_si256(le(fl, fthr), mask);
18249- if len == 6 {18326+ let any_flat = _mm256_testz_si256(flat, flat) == 0;
18327+ if LEN == 6 {
18328+ if any_flat {
18250 let taps = [p2, p1, p0, q0, q1, q2];18329 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));
18252 } else {18334 } else {
18335+ put!(6, b_p1);
18336+ put!(7, b_p0);
18337+ put!(8, b_q0);
18338+ put!(9, b_q1);
18339+ }
18340+ return true;
18341+ }
18342+
18343+ // `filter8`'s six rows over `p2..q2`, the wide kernel's base.
18344+ let (o0, o1, o2, o3, o4, o5) = if any_flat {
18253 let taps = [p3, p2, p1, p0, q0, q1, q2, q3];18345 let taps = [p3, p2, p1, p0, q0, q1, q2, q3];
18254- wrows!(r, 3, W8, taps, 3, flat, 0, 1, 2, 3, 4, 5);18346+ (
18255- if len > 8 {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)
18356+ };
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+
18256 let fl2 = _mm256_max_epi16(18372 let fl2 = _mm256_max_epi16(
18257 _mm256_max_epi16(18373 _mm256_max_epi16(
18258 _mm256_max_epi16(ad(p4, p0), ad(q4, q0)),18374 _mm256_max_epi16(ad(p4, p0), ad(q4, q0)),
@@ -18261,17 +18377,21 @@ mod deblock_simd {
18261 _mm256_max_epi16(ad(p6, p0), ad(q6, q0)),18377 _mm256_max_epi16(ad(p6, p0), ad(q6, q0)),
18262 );18378 );
18263 let flat2 = _mm256_and_si256(le(fl2, fthr), flat);18379 let flat2 = _mm256_and_si256(le(fl2, fthr), flat);
18264- // lane-deblock: `filter14`'s twelve rows as a RUNNING18380+ if _mm256_testz_si256(flat2, flat2) != 0 {
18265- // sum. Consecutive `W14` rows differ by three or four18381+ // No line takes the wide rows: `t[2..5]`/`t[11..14]` keep the
18266- // taps (row j+1 - row j is a constant vector with two18382+ // taps they were loaded with, exactly as the blend would.
18267- // negative and two positive entries), so each output18383+ put8!();
18268- // costs one add, one sub and the rounding shift instead18384+ return true;
18269- // of a fresh 14-tap weighted sum -- 168 tap operations18385+ }
18270- // become 44. Exact: every partial sum is taken mod18386+ // lane-deblock: `filter14`'s twelve rows as a RUNNING sum.
18271- // 2^16 by `add/sub_epi16` and every ROW sum is a real18387+ // Consecutive `W14` rows differ by three or four taps (row j+1 - row
18272- // `W14` row, at most `16 * 4095 + 8 < 2^16`, so the18388+ // j is a constant vector with two negative and two positive
18273- // unsigned rounding shift sees the true value.18389+ // entries), so each output costs one add, one sub and the rounding
18274- // `deblock_simd_kernel_matches_the_scalar_filter` pins18390+ // 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
18275 // it against `filter_edge` at 8/10/12-bit.18395 // it against `filter_edge` at 8/10/12-bit.
18276 let mut s = _mm256_add_epi16(18396 let mut s = _mm256_add_epi16(
18277 _mm256_add_epi16(18397 _mm256_add_epi16(
@@ -18283,48 +18403,36 @@ mod deblock_simd {
18283 q0,18403 q0,
18284 ),18404 ),
18285 );18405 );
18406+ let add2 = |a, b| _mm256_add_epi16(a, b);
18286 macro_rules! row14 {18407 macro_rules! row14 {
18287- ($idx:literal) => {18408+ ($idx:literal, $base:expr) => {
18288- r[$idx] = _mm256_blendv_epi8(18409+ put!(
18289- r[$idx],18410+ $idx,
18290- _mm256_srli_epi16(18411+ _mm256_blendv_epi8(
18291- _mm256_add_epi16(s, _mm256_set1_epi16(8)),18412+ $base,
18292- 4,18413+ _mm256_srli_epi16(_mm256_add_epi16(s, _mm256_set1_epi16(8)), 4),
18293- ),
18294 flat2,18414 flat2,
18415+ )
18295 );18416 );
18296 };18417 };
18297- ($idx:literal, $plus:expr, $minus:expr) => {18418+ ($idx:literal, $base:expr, $plus:expr, $minus:expr) => {
18298 s = _mm256_sub_epi16(_mm256_add_epi16(s, $plus), $minus);18419 s = _mm256_sub_epi16(_mm256_add_epi16(s, $plus), $minus);
18299- row14!($idx);18420+ row14!($idx, $base);
18300- };18421+ };
18301- }18422+ }
18302- let add2 = |a, b| _mm256_add_epi16(a, b);18423+ row14!(2, p5);
18303- row14!(0);18424+ row14!(3, p4, add2(p3, q1), _mm256_slli_epi16(p6, 1));
18304- row14!(1, add2(p3, q1), _mm256_slli_epi16(p6, 1));18425+ row14!(4, p3, add2(p2, q2), add2(p6, p5));
18305- row14!(2, add2(p2, q2), add2(p6, p5));18426+ row14!(5, o0, add2(p1, q3), add2(p6, p4));
18306- row14!(3, add2(p1, q3), add2(p6, p4));18427+ row14!(6, o1, add2(p0, q4), add2(p6, p3));
18307- row14!(4, add2(p0, q4), add2(p6, p3));18428+ row14!(7, o2, add2(q0, q5), add2(p6, p2));
18308- row14!(5, add2(q0, q5), add2(p6, p2));18429+ row14!(8, o3, add2(q1, q6), add2(p6, p1));
18309- row14!(6, add2(q1, q6), add2(p6, p1));18430+ row14!(9, o4, add2(q2, q6), add2(p5, p0));
18310- row14!(7, add2(q2, q6), add2(p5, p0));18431+ row14!(10, o5, add2(q3, q6), add2(p4, q0));
18311- row14!(8, add2(q3, q6), add2(p4, q0));18432+ row14!(11, q3, add2(q4, q6), add2(p3, q1));
18312- row14!(9, add2(q4, q6), add2(p3, q1));18433+ row14!(12, q4, add2(q5, q6), add2(p2, q2));
18313- row14!(10, add2(q5, q6), add2(p2, q2));18434+ row14!(13, q5, _mm256_slli_epi16(q6, 1), add2(p1, q3));
18314- row14!(11, _mm256_slli_epi16(q6, 1), add2(p1, q3));18435+ true
18315- }
18316- }
18317- }
18318-
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),
18324- };
18325- for j in a..b {
18326- t[j + 2] = _mm256_min_epi16(_mm256_max_epi16(r[j], zero), smaxv);
18327- }
18328 }18436 }
18329 18437
18330 /// 16 edge lines at `base`. `vertical` names the edge: a vertical edge's18438 /// 16 edge lines at `base`. `vertical` names the edge: a vertical edge's
@@ -18372,7 +18480,17 @@ mod deblock_simd {
18372 t[i] = _mm256_loadu_si256(p.add(top + i * stride).cast());18480 t[i] = _mm256_loadu_si256(p.add(top + i * stride).cast());
18373 }18481 }
18374 }18482 }
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+ }
18376 if vertical {18494 if vertical {
18377 transpose16(&mut t);18495 transpose16(&mut t);
18378 for j in 0..16 {18496 for j in 0..16 {
@@ -18512,13 +18630,16 @@ fn deblock_plane(
18512 // no other band touches (see `Shared`'s own corner-cut note).18630 // no other band touches (see `Shared`'s own corner-cut note).
18513 #[allow(unsafe_code)]18631 #[allow(unsafe_code)]
18514 let plane = unsafe { sp.get() };18632 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);
18515 let mut x0 = 4usize;18636 let mut x0 = 4usize;
18516 while x0 < tw {18637 while x0 < tw {
18517 let mut y0 = y_lo;18638 let mut y0 = y_lo;
18518 while y0 < y_hi {18639 while y0 < y_hi {
18519 if y0 + 16 <= y_hi && x0 >= 8 && x0 + 8 <= stride {18640 if y0 + 16 <= y_hi && x0 >= 8 && x0 + 8 <= stride {
18520 let ps: [Option<(u8, i32)>; 4] =18641 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));
18522 if let Some((len, level)) = ps[0] {18643 if let Some((len, level)) = ps[0] {
18523 if ps.iter().all(|p| *p == ps[0])18644 if ps.iter().all(|p| *p == ps[0])
18524 && filter_edge16(18645 && filter_edge16(
@@ -18551,7 +18672,7 @@ fn deblock_plane(
18551 y0 += 16;18672 y0 += 16;
18552 continue;18673 continue;
18553 }18674 }
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) {
18555 if crate::envflags::env_flag!("EC_AV1_DEBLOCK_TRACE_V") && plane_idx == 0 {18676 if crate::envflags::env_flag!("EC_AV1_DEBLOCK_TRACE_V") && plane_idx == 0 {
18556 eprintln!("VEDGE x0={x0} y0={y0} len={len} level={level}");18677 eprintln!("VEDGE x0={x0} y0={y0} len={len} level={level}");
18557 }18678 }
@@ -18580,13 +18701,14 @@ fn deblock_plane(
18580 // SAFETY: this band writes only columns `x_lo..x_hi`.18701 // SAFETY: this band writes only columns `x_lo..x_hi`.
18581 #[allow(unsafe_code)]18702 #[allow(unsafe_code)]
18582 let plane = unsafe { sp.get() };18703 let plane = unsafe { sp.get() };
18704+ let hctx = EdgeCtx::new(lf, n, plane_idx, 1, chroma, seg_lf_active, fctx);
18583 let mut y0 = 4usize;18705 let mut y0 = 4usize;
18584 while y0 < th {18706 while y0 < th {
18585 let mut x0 = x_lo;18707 let mut x0 = x_lo;
18586 while x0 < x_hi {18708 while x0 < x_hi {
18587 if x0 + 16 <= x_hi && y0 >= 8 && y0 + 7 <= rows {18709 if x0 + 16 <= x_hi && y0 >= 8 && y0 + 7 <= rows {
18588 let ps: [Option<(u8, i32)>; 4] =18710 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));
18590 if let Some((len, level)) = ps[0] {18712 if let Some((len, level)) = ps[0] {
18591 if ps.iter().all(|p| *p == ps[0])18713 if ps.iter().all(|p| *p == ps[0])
18592 && filter_edge16(18714 && filter_edge16(
@@ -18619,7 +18741,7 @@ fn deblock_plane(
18619 x0 += 16;18741 x0 += 16;
18620 continue;18742 continue;
18621 }18743 }
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) {
18623 if crate::envflags::env_flag!("EC_AV1_DEBLOCK_TRACE")18745 if crate::envflags::env_flag!("EC_AV1_DEBLOCK_TRACE")
18624 && plane_idx == 018746 && plane_idx == 0
18625 && x0 == 4418747 && x0 == 44