forge
Explore
Log in

Don't you dare go Hollow

Merge branch 'lane-deblock2' (8587f4c7): deblock kernel monomorphised on edge length with a mask early-out, EdgeCtx replaces seven per-pass args, filter level tabulated per band over (base level, ref frame); byte-exact, -0.74% instructions on the 4K segment

Browse files at this commit

6d124e382ef6679068ff829a3929142470d41910Ahmet Kaan GümüşParent: d35cd3ab247733
crates/ec-av1/src/decode.rsmodified+288 -166
@@ -17673,26 +17673,105 @@ fn lf_level(
17673 /// edge" suppression (which only ever fires at a non-PU-edge TU boundary)17673 /// edge" suppression (which only ever fires at a non-PU-edge TU boundary)
17674 /// never applies here, letting this skip the `skip`/`skip_txfm` lookup17674 /// never applies here, letting this skip the `skip`/`skip_txfm` lookup
17675 /// entirely. Returns `None` when this 4-pixel edge group is not filtered.17675 /// entirely. Returns `None` when this 4-pixel edge group is not filtered.
17676-#[inline(always)]17676+/// One plane pass's worth of [`edge_params`] constants (lane-deblock2): the
17677-fn edge_params(17677+/// seven values that used to be re-passed per 4-pixel edge group -- half of
17678- lf: &LoopFilterParams,17678+/// them on the stack, into an `inline(never)` body -- plus libaom's
17679- n: &Neighbours,17679+/// `lfi->lfthr` idea in level form: `av1_loop_filter_frame_init` builds the
17680+/// filter level for every `(base level, ref frame)` pair once per frame
17681+/// instead of running `av1_get_filter_level`'s delta arithmetic per edge.
17682+struct EdgeCtx<'a> {
17683+ lf: &'a LoopFilterParams,
17684+ n: &'a Neighbours,
17685+ fctx: &'a crate::decode::FrameCtx,
17680 plane_idx: usize,17686 plane_idx: usize,
17681 dir: usize,17687 dir: usize,
17682 chroma: bool,17688 chroma: bool,
17683- x0: usize,17689+ seg_lf_active: bool,
17684- y0: usize,17690+ /// `delta_lf_grid`'s column for this plane and direction.
17685- // lane-perf9: `segmentation_enabled`, read once per plane pass instead of17691+ delta_idx: usize,
17686- // four thread-local reads (`SEG_MI_DIMS`, `SEG_IDS`, twice each) plus two17692+ /// `mode_step` (`set_lpf_parameters`): one mi for luma, `1 << ss` for
17687- // `SEG` borrows per 4x4 edge. With it clear, every segment id is 0 and no17693+ /// chroma.
17688- // `SEG_LVL_ALT_LF_*` feature can be active, so the levels are unchanged.17694+ step: usize,
17689- seg_lf_active: bool, fctx: &crate::decode::FrameCtx,17695+ /// This pass's own `lf.level[..]`, the LUT's base.
17690-) -> Option<(u8, i32)> {17696+ base0: i32,
17691- let (mi_r, mi_c) = (plane_to_mi(chroma, y0), plane_to_mi(chroma, x0));17697+ /// `lf_level` at `[base level][ref frame + 7]`, with no segment
17692- let cur_tx = if dir == 0 {17698+ /// override -- the whole domain, since a level is `0..=63` and a ref
17693- tx_px_at(n, chroma, mi_r, mi_c)17699+ /// frame `-7..=7` (`ref_deltas` holds `TOTAL_REFS_PER_FRAME` = 8
17700+ /// entries and is indexed by the ref frame's magnitude).
17701+ lvl: [[u8; 15]; 64],
17702+}
17703+
17704+impl<'a> EdgeCtx<'a> {
17705+ fn new(
17706+ lf: &'a LoopFilterParams,
17707+ n: &'a Neighbours,
17708+ plane_idx: usize,
17709+ dir: usize,
17710+ chroma: bool,
17711+ seg_lf_active: bool,
17712+ fctx: &'a crate::decode::FrameCtx,
17713+ ) -> Self {
17714+ let base0 = i32::from(if plane_idx == 0 {
17715+ lf.level[dir]
17716+ } else if plane_idx == 1 {
17717+ lf.level[2]
17718+ } else {
17719+ lf.level[3]
17720+ });
17721+ // `lf_level`'s own first step is `(base0 + delta_lf).clamp(0, 63)`,
17722+ // so passing `b - base0` pins its base at `b` and the rest of the
17723+ // function is a pure function of `(b, ref_frame)`.
17724+ let mut lvl = [[0u8; 15]; 64];
17725+ for (b, row) in lvl.iter_mut().enumerate() {
17726+ for (r, cell) in row.iter_mut().enumerate() {
17727+ *cell =
17728+ lf_level(lf, plane_idx, dir, (r as i32 - 7) as i8, b as i32 - base0, 0, fctx)
17729+ as u8;
17730+ }
17731+ }
17732+ Self {
17733+ lf,
17734+ n,
17735+ fctx,
17736+ plane_idx,
17737+ dir,
17738+ chroma,
17739+ seg_lf_active,
17740+ delta_idx: if plane_idx == 0 { dir } else { plane_idx + 1 },
17741+ step: if chroma { 2 } else { 1 },
17742+ base0,
17743+ lvl,
17744+ }
17745+ }
17746+
17747+ /// Spec 7.14.4's `get_filter_level`, off the table when no segment can
17748+ /// override it (which is every frame that codes no segmentation).
17749+ #[inline(always)]
17750+ fn level(&self, ref_frame: i8, delta_lf: i32, seg: u8) -> i32 {
17751+ if self.seg_lf_active {
17752+ return lf_level(self.lf, self.plane_idx, self.dir, ref_frame, delta_lf, seg, self.fctx);
17753+ }
17754+ let b = (self.base0 + delta_lf).clamp(0, 63) as usize;
17755+ debug_assert!((-7..=7).contains(&ref_frame));
17756+ let r = (i32::from(ref_frame) + 7).clamp(0, 14) as usize;
17757+ i32::from(self.lvl[b][r])
17758+ }
17759+}
17760+
17761+/// Spec 7.14.2's `set_lpf_parameters`, restricted to the uniform-level,
17762+/// no-segmentation, no-delta-LF path and to this decoder's own invariant
17763+/// that a coded block's transform is always its own full size -- so a TU
17764+/// edge is always also a PU edge, and the "both sides skipped, not a PU
17765+/// edge" suppression (which only ever fires at a non-PU-edge TU boundary)
17766+/// never applies here, letting this skip the `skip`/`skip_txfm` lookup
17767+/// entirely. Returns `None` when this 4-pixel edge group is not filtered.
17768+#[inline(always)]
17769+fn edge_params(c: &EdgeCtx, x0: usize, y0: usize) -> Option<(u8, i32)> {
17770+ let (mi_r, mi_c) = (plane_to_mi(c.chroma, y0), plane_to_mi(c.chroma, x0));
17771+ let cur_tx = if c.dir == 0 {
17772+ tx_px_at(c.n, c.chroma, mi_r, mi_c)
17694 } else {17773 } else {
17695- tx_h_px_at(n, chroma, mi_r, mi_c)17774+ tx_h_px_at(c.n, c.chroma, mi_r, mi_c)
17696 };17775 };
17697 // lane-deblock: `cur_tx` is a power-of-two pixel size, so the alignment17776 // lane-deblock: `cur_tx` is a power-of-two pixel size, so the alignment
17698 // test is a mask, not the hardware `divl` this used to emit (10.7% of17777 // test is a mask, not the hardware `divl` this used to emit (10.7% of
@@ -17702,49 +17781,41 @@ fn edge_params(
17702 // the covering transform does not start at), so this head is inlined17781 // the covering transform does not start at), so this head is inlined
17703 // into the deblock loops and only survivors pay the call and the17782 // into the deblock loops and only survivors pay the call and the
17704 // register-spilling prologue of the body below.17783 // register-spilling prologue of the body below.
17705- if cur_tx == 0 || (if dir == 0 { x0 } else { y0 }) & (cur_tx as usize - 1) != 0 {17784+ if cur_tx == 0 || (if c.dir == 0 { x0 } else { y0 }) & (cur_tx as usize - 1) != 0 {
17706 return None;17785 return None;
17707 }17786 }
17708- edge_params_body(lf, n, plane_idx, dir, chroma, x0, y0, mi_r, mi_c, cur_tx, seg_lf_active, fctx)17787+ edge_params_body(c, x0, y0, mi_r, mi_c, cur_tx)
17709 }17788 }
17710 17789
17711 /// [`edge_params`] past its transform-alignment gate.17790 /// [`edge_params`] past its transform-alignment gate.
17712-#[allow(clippy::too_many_arguments)]
17713 #[inline(never)]17791 #[inline(never)]
17714 fn edge_params_body(17792 fn edge_params_body(
17715- lf: &LoopFilterParams,17793+ c: &EdgeCtx,
17716- n: &Neighbours,
17717- plane_idx: usize,
17718- dir: usize,
17719- chroma: bool,
17720 x0: usize,17794 x0: usize,
17721 y0: usize,17795 y0: usize,
17722 mi_r: usize,17796 mi_r: usize,
17723 mi_c: usize,17797 mi_c: usize,
17724 cur_tx: u32,17798 cur_tx: u32,
17725- seg_lf_active: bool,
17726- fctx: &crate::decode::FrameCtx,
17727 ) -> Option<(u8, i32)> {17799 ) -> Option<(u8, i32)> {
17800+ let n = c.n;
17728 let cur_ref = n.ref_grid[mi_r * n.skip_grid_cols_mi + mi_c];17801 let cur_ref = n.ref_grid[mi_r * n.skip_grid_cols_mi + mi_c];
17729- // `mode_step` (`set_lpf_parameters`): one mi for luma, `1 << ss` for17802+ // The previous cell must keep the odd parity `plane_to_mi` just
17730- // chroma -- the previous cell must keep the odd parity `plane_to_mi`17803+ // established, i.e. the bottom/right mi of the PREVIOUS 8x8 luma group,
17731- // just established, i.e. the bottom/right mi of the PREVIOUS 8x8 luma17804+ // not the other half of this one.
17732- // group, not the other half of this one.17805+ let (pv_mi_r, pv_mi_c) = if c.dir == 0 {
17733- let step = if chroma { 2 } else { 1 };17806+ (mi_r, mi_c - c.step)
17734- let (pv_mi_r, pv_mi_c) = if dir == 0 {
17735- (mi_r, mi_c - step)
17736 } else {17807 } else {
17737- (mi_r - step, mi_c)17808+ (mi_r - c.step, mi_c)
17738 };17809 };
17739- let pv_tx = if dir == 0 {17810+ let pv_tx = if c.dir == 0 {
17740- tx_px_at(n, chroma, pv_mi_r, pv_mi_c)17811+ tx_px_at(n, c.chroma, pv_mi_r, pv_mi_c)
17741 } else {17812 } else {
17742- tx_h_px_at(n, chroma, pv_mi_r, pv_mi_c)17813+ tx_h_px_at(n, c.chroma, pv_mi_r, pv_mi_c)
17743 };17814 };
17744 let pv_ref = n.ref_grid[pv_mi_r * n.skip_grid_cols_mi + pv_mi_c];17815 let pv_ref = n.ref_grid[pv_mi_r * n.skip_grid_cols_mi + pv_mi_c];
17745- let delta_idx = if plane_idx == 0 { dir } else { plane_idx + 1 };17816+ let cur_delta_lf = i32::from(n.delta_lf_grid[mi_r * n.skip_grid_cols_mi + mi_c][c.delta_idx]);
17746- let cur_delta_lf = i32::from(n.delta_lf_grid[mi_r * n.skip_grid_cols_mi + mi_c][delta_idx]);17817+ let pv_delta_lf =
17747- let pv_delta_lf = i32::from(n.delta_lf_grid[pv_mi_r * n.skip_grid_cols_mi + pv_mi_c][delta_idx]);17818+ i32::from(n.delta_lf_grid[pv_mi_r * n.skip_grid_cols_mi + pv_mi_c][c.delta_idx]);
17748 17819
17749 // spec 7.14.2 / libaom `set_lpf_parameters`: a transform edge between two17820 // spec 7.14.2 / libaom `set_lpf_parameters`: a transform edge between two
17750 // SKIPPED INTER blocks is filtered only when it is also a prediction17821 // SKIPPED INTER blocks is filtered only when it is also a prediction
@@ -17762,16 +17833,16 @@ fn edge_params_body(
17762 // size, which a 4x4 luma block read at the `| 1` mi is not.17833 // size, which a 4x4 luma block read at the `| 1` mi is not.
17763 let cur_org = n.blk_org_grid[mi_r * n.skip_grid_cols_mi + mi_c];17834 let cur_org = n.blk_org_grid[mi_r * n.skip_grid_cols_mi + mi_c];
17764 let cur_dim = n.blk_dim_grid[mi_r * n.skip_grid_cols_mi + mi_c];17835 let cur_dim = n.blk_dim_grid[mi_r * n.skip_grid_cols_mi + mi_c];
17765- let (org_px, dim_mi) = if dir == 0 {17836+ let (org_px, dim_mi) = if c.dir == 0 {
17766 (usize::from(cur_org[1]), usize::from(cur_dim[0]))17837 (usize::from(cur_org[1]), usize::from(cur_dim[0]))
17767 } else {17838 } else {
17768 (usize::from(cur_org[0]), usize::from(cur_dim[1]))17839 (usize::from(cur_org[0]), usize::from(cur_dim[1]))
17769 };17840 };
17770- let pu_edge = if chroma {17841+ let pu_edge = if c.chroma {
17771 let plane_dim = (dim_mi * MI / 2).max(4);17842 let plane_dim = (dim_mi * MI / 2).max(4);
17772- (if dir == 0 { x0 } else { y0 }) % plane_dim == 017843+ (if c.dir == 0 { x0 } else { y0 }) % plane_dim == 0
17773 } else {17844 } else {
17774- plane_to_mi(false, if dir == 0 { x0 } else { y0 }) == org_px17845+ plane_to_mi(false, if c.dir == 0 { x0 } else { y0 }) == org_px
17775 };17846 };
17776 if !pu_edge17847 if !pu_edge
17777 && cur_ref != 017848 && cur_ref != 0
@@ -17781,18 +17852,18 @@ fn edge_params_body(
17781 {17852 {
17782 return None;17853 return None;
17783 }17854 }
17784- let (cur_seg, pv_seg) = if seg_lf_active {17855+ let (cur_seg, pv_seg) = if c.seg_lf_active {
17785- (segment_id_at(mi_r, mi_c, fctx), segment_id_at(pv_mi_r, pv_mi_c, fctx))17856+ (segment_id_at(mi_r, mi_c, c.fctx), segment_id_at(pv_mi_r, pv_mi_c, c.fctx))
17786 } else {17857 } else {
17787 (0, 0)17858 (0, 0)
17788 };17859 };
17789- let cur_level = lf_level(lf, plane_idx, dir, cur_ref, cur_delta_lf, cur_seg, fctx);17860+ let cur_level = c.level(cur_ref, cur_delta_lf, cur_seg);
17790- let pv_level = lf_level(lf, plane_idx, dir, pv_ref, pv_delta_lf, pv_seg, fctx);17861+ let pv_level = c.level(pv_ref, pv_delta_lf, pv_seg);
17791 if cur_level == 0 && pv_level == 0 {17862 if cur_level == 0 && pv_level == 0 {
17792 return None;17863 return None;
17793 }17864 }
17794 let dim = unit_log2(cur_tx).min(unit_log2(pv_tx)).clamp(0, 4) as usize;17865 let dim = unit_log2(cur_tx).min(unit_log2(pv_tx)).clamp(0, 4) as usize;
17795- let len: u8 = if chroma {17866+ let len: u8 = if c.chroma {
17796 if dim == 0 { 4 } else { 6 }17867 if dim == 0 { 4 } else { 6 }
17797 } else {17868 } else {
17798 [4, 8, 14, 14, 14][dim]17869 [4, 8, 14, 14, 14][dim]
@@ -18253,39 +18324,41 @@ mod deblock_simd {
18253 }18324 }
18254 }18325 }
18255 18326
18256- /// One wide-kernel row set, unrolled so each `wsum` sees its weight row
18257- /// as a constant.
18258- macro_rules! wrows {
18259- ($r:expr, $off:expr, $tbl:ident, $taps:expr, $shift:literal, $flat:expr, $($j:literal),*) => {
18260- $(
18261- $r[$off + $j] = _mm256_blendv_epi8($r[$off + $j], wsum::<_, $shift>(&$taps, &$tbl[$j]), $flat);
18262- )*
18263- };
18264- }
18265-
18266 /// [`super::filter_edge`]'s body for 16 lines held as `t[0..16]`, `q0` in18327 /// [`super::filter_edge`]'s body for 16 lines held as `t[0..16]`, `q0` in
18267- /// `t[8]`; the filtered taps are written back in place.18328+ /// `t[8]`; the filtered taps are written back in place. Returns `false`
18329+ /// when the filter mask is empty on every one of the 16 lines -- nothing
18330+ /// was written and the caller can skip the whole write-back.
18268 ///18331 ///
18269 /// # Safety18332 /// # Safety
18270 /// Requires AVX2.18333 /// Requires AVX2.
18334+ ///
18335+ /// lane-deblock2: `LEN` is a const parameter, so each edge length gets
18336+ /// its own kernel holding only its own taps, and every wide row is
18337+ /// clamped straight into `t` instead of into a shared twelve-entry
18338+ /// result array (which the register allocator spilled -- 35% of this
18339+ /// kernel's samples were `vmovdqa` to the stack).
18271 #[target_feature(enable = "avx2")]18340 #[target_feature(enable = "avx2")]
18272 #[allow(clippy::too_many_arguments)]18341 #[allow(clippy::too_many_arguments)]
18273- unsafe fn filter16(18342+ unsafe fn filter16<const LEN: u8>(
18274 t: &mut [__m256i; 16],18343 t: &mut [__m256i; 16],
18275- len: u8,
18276 blimit: i32,18344 blimit: i32,
18277 limit: i32,18345 limit: i32,
18278 hev_thr: i32,18346 hev_thr: i32,
18279 flat_thresh: i32,18347 flat_thresh: i32,
18280 scale: i32,18348 scale: i32,
18281 smax: i32,18349 smax: i32,
18282- ) {18350+ ) -> bool {
18283 let lim = _mm256_set1_epi16(limit as i16);18351 let lim = _mm256_set1_epi16(limit as i16);
18284 let blim = _mm256_set1_epi16(blimit as i16);18352 let blim = _mm256_set1_epi16(blimit as i16);
18285 let hthr = _mm256_set1_epi16(hev_thr as i16);18353 let hthr = _mm256_set1_epi16(hev_thr as i16);
18286 let fthr = _mm256_set1_epi16(flat_thresh as i16);18354 let fthr = _mm256_set1_epi16(flat_thresh as i16);
18287 let zero = _mm256_setzero_si256();18355 let zero = _mm256_setzero_si256();
18288 let smaxv = _mm256_set1_epi16(smax as i16);18356 let smaxv = _mm256_set1_epi16(smax as i16);
18357+ macro_rules! put {
18358+ ($i:expr, $v:expr) => {
18359+ t[$i] = _mm256_min_epi16(_mm256_max_epi16($v, zero), smaxv);
18360+ };
18361+ }
18289 let (p6, p5, p4, p3, p2, p1, p0) = (t[1], t[2], t[3], t[4], t[5], t[6], t[7]);18362 let (p6, p5, p4, p3, p2, p1, p0) = (t[1], t[2], t[3], t[4], t[5], t[6], t[7]);
18290 let (q0, q1, q2, q3, q4, q5, q6) = (t[8], t[9], t[10], t[11], t[12], t[13], t[14]);18363 let (q0, q1, q2, q3, q4, q5, q6) = (t[8], t[9], t[10], t[11], t[12], t[13], t[14]);
18291 18364
@@ -18293,10 +18366,10 @@ mod deblock_simd {
18293 // reaches, plus the shared `blimit` term.18366 // reaches, plus the shared `blimit` term.
18294 let d1 = _mm256_max_epi16(ad(p1, p0), ad(q1, q0));18367 let d1 = _mm256_max_epi16(ad(p1, p0), ad(q1, q0));
18295 let mut md = d1;18368 let mut md = d1;
18296- if len >= 6 {18369+ if LEN >= 6 {
18297 md = _mm256_max_epi16(md, _mm256_max_epi16(ad(p2, p1), ad(q2, q1)));18370 md = _mm256_max_epi16(md, _mm256_max_epi16(ad(p2, p1), ad(q2, q1)));
18298 }18371 }
18299- if len >= 8 {18372+ if LEN >= 8 {
18300 md = _mm256_max_epi16(md, _mm256_max_epi16(ad(p3, p2), ad(q3, q2)));18373 md = _mm256_max_epi16(md, _mm256_max_epi16(ad(p3, p2), ad(q3, q2)));
18301 }18374 }
18302 let bl = _mm256_add_epi16(18375 let bl = _mm256_add_epi16(
@@ -18304,6 +18377,14 @@ mod deblock_simd {
18304 _mm256_srli_epi16(ad(p1, q1), 1),18377 _mm256_srli_epi16(ad(p1, q1), 1),
18305 );18378 );
18306 let mask = _mm256_and_si256(le(md, lim), le(bl, blim));18379 let mask = _mm256_and_si256(le(md, lim), le(bl, blim));
18380+ // lane-deblock2: an empty mask leaves every tap at its loaded value
18381+ // (the flat masks are `& mask`, so they are empty too, and the final
18382+ // clamp is the identity on samples that came out of the plane), so
18383+ // the whole write-back -- including the vertical edge's second
18384+ // transpose -- is dead.
18385+ if _mm256_testz_si256(mask, mask) != 0 {
18386+ return false;
18387+ }
18307 let hev = _mm256_cmpgt_epi16(d1, hthr);18388 let hev = _mm256_cmpgt_epi16(d1, hthr);
18308 18389
18309 // `filter4`, in the centred domain.18390 // `filter4`, in the centred domain.
@@ -18333,110 +18414,137 @@ mod deblock_simd {
18333 let n_p0 = _mm256_add_epi16(sclamp(_mm256_add_epi16(ps0, f2), lo, hi), centre);18414 let n_p0 = _mm256_add_epi16(sclamp(_mm256_add_epi16(ps0, f2), lo, hi), centre);
18334 let n_q0 = _mm256_add_epi16(sclamp(_mm256_sub_epi16(qs0, f1), lo, hi), centre);18415 let n_q0 = _mm256_add_epi16(sclamp(_mm256_sub_epi16(qs0, f1), lo, hi), centre);
18335 let n_q1 = _mm256_add_epi16(sclamp(_mm256_sub_epi16(qs1, tap), lo, hi), centre);18416 let n_q1 = _mm256_add_epi16(sclamp(_mm256_sub_epi16(qs1, tap), lo, hi), centre);
18336- let mut r = [18417+ let b_p1 = _mm256_blendv_epi8(p1, n_p1, mask);
18337- p5,18418+ let b_p0 = _mm256_blendv_epi8(p0, n_p0, mask);
18338- p4,18419+ let b_q0 = _mm256_blendv_epi8(q0, n_q0, mask);
18339- p3,18420+ let b_q1 = _mm256_blendv_epi8(q1, n_q1, mask);
18340- p2,18421+ if LEN == 4 {
18341- _mm256_blendv_epi8(p1, n_p1, mask),18422+ put!(6, b_p1);
18342- _mm256_blendv_epi8(p0, n_p0, mask),18423+ put!(7, b_p0);
18343- _mm256_blendv_epi8(q0, n_q0, mask),18424+ put!(8, b_q0);
18344- _mm256_blendv_epi8(q1, n_q1, mask),18425+ put!(9, b_q1);
18345- q2,18426+ return true;
18346- q3,18427+ }
18347- q4,
18348- q5,
18349- ];
18350 18428
18351- if len >= 6 {18429+ // `highbd_flat_mask4`, over the same reach as this length's mask.
18352- // `highbd_flat_mask4`, over the same reach as this length's mask.18430+ let mut fl = _mm256_max_epi16(
18353- let mut fl = _mm256_max_epi16(18431+ _mm256_max_epi16(ad(p1, p0), ad(q1, q0)),
18354- _mm256_max_epi16(ad(p1, p0), ad(q1, q0)),18432+ _mm256_max_epi16(ad(p2, p0), ad(q2, q0)),
18355- _mm256_max_epi16(ad(p2, p0), ad(q2, q0)),18433+ );
18356- );18434+ if LEN >= 8 {
18357- if len >= 8 {18435+ fl = _mm256_max_epi16(fl, _mm256_max_epi16(ad(p3, p0), ad(q3, q0)));
18358- fl = _mm256_max_epi16(fl, _mm256_max_epi16(ad(p3, p0), ad(q3, q0)));18436+ }
18359- }18437+ let flat = _mm256_and_si256(le(fl, fthr), mask);
18360- let flat = _mm256_and_si256(le(fl, fthr), mask);18438+ let any_flat = _mm256_testz_si256(flat, flat) == 0;
18361- if len == 6 {18439+ if LEN == 6 {
18440+ if any_flat {
18362 let taps = [p2, p1, p0, q0, q1, q2];18441 let taps = [p2, p1, p0, q0, q1, q2];
18363- wrows!(r, 4, W6, taps, 3, flat, 0, 1, 2, 3);18442+ put!(6, _mm256_blendv_epi8(b_p1, wsum::<_, 3>(&taps, &W6[0]), flat));
18443+ put!(7, _mm256_blendv_epi8(b_p0, wsum::<_, 3>(&taps, &W6[1]), flat));
18444+ put!(8, _mm256_blendv_epi8(b_q0, wsum::<_, 3>(&taps, &W6[2]), flat));
18445+ put!(9, _mm256_blendv_epi8(b_q1, wsum::<_, 3>(&taps, &W6[3]), flat));
18364 } else {18446 } else {
18365- let taps = [p3, p2, p1, p0, q0, q1, q2, q3];18447+ put!(6, b_p1);
18366- wrows!(r, 3, W8, taps, 3, flat, 0, 1, 2, 3, 4, 5);18448+ put!(7, b_p0);
18367- if len > 8 {18449+ put!(8, b_q0);
18368- let fl2 = _mm256_max_epi16(18450+ put!(9, b_q1);
18369- _mm256_max_epi16(
18370- _mm256_max_epi16(ad(p4, p0), ad(q4, q0)),
18371- _mm256_max_epi16(ad(p5, p0), ad(q5, q0)),
18372- ),
18373- _mm256_max_epi16(ad(p6, p0), ad(q6, q0)),
18374- );
18375- let flat2 = _mm256_and_si256(le(fl2, fthr), flat);
18376- // lane-deblock: `filter14`'s twelve rows as a RUNNING
18377- // sum. Consecutive `W14` rows differ by three or four
18378- // taps (row j+1 - row j is a constant vector with two
18379- // negative and two positive entries), so each output
18380- // costs one add, one sub and the rounding shift instead
18381- // of a fresh 14-tap weighted sum -- 168 tap operations
18382- // become 44. Exact: every partial sum is taken mod
18383- // 2^16 by `add/sub_epi16` and every ROW sum is a real
18384- // `W14` row, at most `16 * 4095 + 8 < 2^16`, so the
18385- // unsigned rounding shift sees the true value.
18386- // `deblock_simd_kernel_matches_the_scalar_filter` pins
18387- // it against `filter_edge` at 8/10/12-bit.
18388- let mut s = _mm256_add_epi16(
18389- _mm256_add_epi16(
18390- _mm256_mullo_epi16(p6, _mm256_set1_epi16(7)),
18391- _mm256_slli_epi16(_mm256_add_epi16(p5, p4), 1),
18392- ),
18393- _mm256_add_epi16(
18394- _mm256_add_epi16(_mm256_add_epi16(p3, p2), _mm256_add_epi16(p1, p0)),
18395- q0,
18396- ),
18397- );
18398- macro_rules! row14 {
18399- ($idx:literal) => {
18400- r[$idx] = _mm256_blendv_epi8(
18401- r[$idx],
18402- _mm256_srli_epi16(
18403- _mm256_add_epi16(s, _mm256_set1_epi16(8)),
18404- 4,
18405- ),
18406- flat2,
18407- );
18408- };
18409- ($idx:literal, $plus:expr, $minus:expr) => {
18410- s = _mm256_sub_epi16(_mm256_add_epi16(s, $plus), $minus);
18411- row14!($idx);
18412- };
18413- }
18414- let add2 = |a, b| _mm256_add_epi16(a, b);
18415- row14!(0);
18416- row14!(1, add2(p3, q1), _mm256_slli_epi16(p6, 1));
18417- row14!(2, add2(p2, q2), add2(p6, p5));
18418- row14!(3, add2(p1, q3), add2(p6, p4));
18419- row14!(4, add2(p0, q4), add2(p6, p3));
18420- row14!(5, add2(q0, q5), add2(p6, p2));
18421- row14!(6, add2(q1, q6), add2(p6, p1));
18422- row14!(7, add2(q2, q6), add2(p5, p0));
18423- row14!(8, add2(q3, q6), add2(p4, q0));
18424- row14!(9, add2(q4, q6), add2(p3, q1));
18425- row14!(10, add2(q5, q6), add2(p2, q2));
18426- row14!(11, _mm256_slli_epi16(q6, 1), add2(p1, q3));
18427- }
18428 }18451 }
18452+ return true;
18429 }18453 }
18430 18454
18431- // `r[j]` holds the sample at tap `j - 6`, i.e. `t[j + 2]`.18455+ // `filter8`'s six rows over `p2..q2`, the wide kernel's base.
18432- let (a, b) = match len {18456+ let (o0, o1, o2, o3, o4, o5) = if any_flat {
18433- 4 | 6 => (4, 8),18457+ let taps = [p3, p2, p1, p0, q0, q1, q2, q3];
18434- 8 => (3, 9),18458+ (
18435- _ => (0, 12),18459+ _mm256_blendv_epi8(p2, wsum::<_, 3>(&taps, &W8[0]), flat),
18460+ _mm256_blendv_epi8(b_p1, wsum::<_, 3>(&taps, &W8[1]), flat),
18461+ _mm256_blendv_epi8(b_p0, wsum::<_, 3>(&taps, &W8[2]), flat),
18462+ _mm256_blendv_epi8(b_q0, wsum::<_, 3>(&taps, &W8[3]), flat),
18463+ _mm256_blendv_epi8(b_q1, wsum::<_, 3>(&taps, &W8[4]), flat),
18464+ _mm256_blendv_epi8(q2, wsum::<_, 3>(&taps, &W8[5]), flat),
18465+ )
18466+ } else {
18467+ (p2, b_p1, b_p0, b_q0, b_q1, q2)
18436 };18468 };
18437- for j in a..b {18469+ macro_rules! put8 {
18438- t[j + 2] = _mm256_min_epi16(_mm256_max_epi16(r[j], zero), smaxv);18470+ () => {
18471+ put!(5, o0);
18472+ put!(6, o1);
18473+ put!(7, o2);
18474+ put!(8, o3);
18475+ put!(9, o4);
18476+ put!(10, o5);
18477+ };
18478+ }
18479+ if LEN == 8 {
18480+ put8!();
18481+ return true;
18482+ }
18483+
18484+ let fl2 = _mm256_max_epi16(
18485+ _mm256_max_epi16(
18486+ _mm256_max_epi16(ad(p4, p0), ad(q4, q0)),
18487+ _mm256_max_epi16(ad(p5, p0), ad(q5, q0)),
18488+ ),
18489+ _mm256_max_epi16(ad(p6, p0), ad(q6, q0)),
18490+ );
18491+ let flat2 = _mm256_and_si256(le(fl2, fthr), flat);
18492+ if _mm256_testz_si256(flat2, flat2) != 0 {
18493+ // No line takes the wide rows: `t[2..5]`/`t[11..14]` keep the
18494+ // taps they were loaded with, exactly as the blend would.
18495+ put8!();
18496+ return true;
18439 }18497 }
18498+ // lane-deblock: `filter14`'s twelve rows as a RUNNING sum.
18499+ // Consecutive `W14` rows differ by three or four taps (row j+1 - row
18500+ // j is a constant vector with two negative and two positive
18501+ // entries), so each output costs one add, one sub and the rounding
18502+ // shift instead of a fresh 14-tap weighted sum -- 168 tap operations
18503+ // become 44. Exact: every partial sum is taken mod 2^16 by
18504+ // `add/sub_epi16` and every ROW sum is a real `W14` row, at most
18505+ // `16 * 4095 + 8 < 2^16`, so the unsigned rounding shift sees the
18506+ // true value. `deblock_simd_kernel_matches_the_scalar_filter` pins
18507+ // it against `filter_edge` at 8/10/12-bit.
18508+ let mut s = _mm256_add_epi16(
18509+ _mm256_add_epi16(
18510+ _mm256_mullo_epi16(p6, _mm256_set1_epi16(7)),
18511+ _mm256_slli_epi16(_mm256_add_epi16(p5, p4), 1),
18512+ ),
18513+ _mm256_add_epi16(
18514+ _mm256_add_epi16(_mm256_add_epi16(p3, p2), _mm256_add_epi16(p1, p0)),
18515+ q0,
18516+ ),
18517+ );
18518+ let add2 = |a, b| _mm256_add_epi16(a, b);
18519+ macro_rules! row14 {
18520+ ($idx:literal, $base:expr) => {
18521+ put!(
18522+ $idx,
18523+ _mm256_blendv_epi8(
18524+ $base,
18525+ _mm256_srli_epi16(_mm256_add_epi16(s, _mm256_set1_epi16(8)), 4),
18526+ flat2,
18527+ )
18528+ );
18529+ };
18530+ ($idx:literal, $base:expr, $plus:expr, $minus:expr) => {
18531+ s = _mm256_sub_epi16(_mm256_add_epi16(s, $plus), $minus);
18532+ row14!($idx, $base);
18533+ };
18534+ }
18535+ row14!(2, p5);
18536+ row14!(3, p4, add2(p3, q1), _mm256_slli_epi16(p6, 1));
18537+ row14!(4, p3, add2(p2, q2), add2(p6, p5));
18538+ row14!(5, o0, add2(p1, q3), add2(p6, p4));
18539+ row14!(6, o1, add2(p0, q4), add2(p6, p3));
18540+ row14!(7, o2, add2(q0, q5), add2(p6, p2));
18541+ row14!(8, o3, add2(q1, q6), add2(p6, p1));
18542+ row14!(9, o4, add2(q2, q6), add2(p5, p0));
18543+ row14!(10, o5, add2(q3, q6), add2(p4, q0));
18544+ row14!(11, q3, add2(q4, q6), add2(p3, q1));
18545+ row14!(12, q4, add2(q5, q6), add2(p2, q2));
18546+ row14!(13, q5, _mm256_slli_epi16(q6, 1), add2(p1, q3));
18547+ true
18440 }18548 }
18441 18549
18442 /// 16 edge lines at `base`. `vertical` names the edge: a vertical edge's18550 /// 16 edge lines at `base`. `vertical` names the edge: a vertical edge's
@@ -18484,7 +18592,17 @@ mod deblock_simd {
18484 t[i] = _mm256_loadu_si256(p.add(top + i * stride).cast());18592 t[i] = _mm256_loadu_si256(p.add(top + i * stride).cast());
18485 }18593 }
18486 }18594 }
18487- filter16(&mut t, len, blimit, limit, hev_thr, flat_thresh, scale, smax);18595+ // lane-deblock2: one monomorphised kernel per edge length, and an
18596+ // empty filter mask returns without any write-back at all.
18597+ let modified = match len {
18598+ 4 => filter16::<4>(&mut t, blimit, limit, hev_thr, flat_thresh, scale, smax),
18599+ 6 => filter16::<6>(&mut t, blimit, limit, hev_thr, flat_thresh, scale, smax),
18600+ 8 => filter16::<8>(&mut t, blimit, limit, hev_thr, flat_thresh, scale, smax),
18601+ _ => filter16::<14>(&mut t, blimit, limit, hev_thr, flat_thresh, scale, smax),
18602+ };
18603+ if !modified {
18604+ return;
18605+ }
18488 if vertical {18606 if vertical {
18489 transpose16(&mut t);18607 transpose16(&mut t);
18490 for j in 0..16 {18608 for j in 0..16 {
@@ -18624,13 +18742,16 @@ fn deblock_plane(
18624 // no other band touches (see `Shared`'s own corner-cut note).18742 // no other band touches (see `Shared`'s own corner-cut note).
18625 #[allow(unsafe_code)]18743 #[allow(unsafe_code)]
18626 let plane = unsafe { sp.get() };18744 let plane = unsafe { sp.get() };
18745+ // lane-deblock2: this band's own level table and pass constants,
18746+ // built once per band instead of re-derived per 4-pixel edge.
18747+ let vctx = EdgeCtx::new(lf, n, plane_idx, 0, chroma, seg_lf_active, fctx);
18627 let mut x0 = 4usize;18748 let mut x0 = 4usize;
18628 while x0 < tw {18749 while x0 < tw {
18629 let mut y0 = y_lo;18750 let mut y0 = y_lo;
18630 while y0 < y_hi {18751 while y0 < y_hi {
18631 if y0 + 16 <= y_hi && x0 >= 8 && x0 + 8 <= stride {18752 if y0 + 16 <= y_hi && x0 >= 8 && x0 + 8 <= stride {
18632 let ps: [Option<(u8, i32)>; 4] =18753 let ps: [Option<(u8, i32)>; 4] =
18633- std::array::from_fn(|k| edge_params(lf, n, plane_idx, 0, chroma, x0, y0 + 4 * k, seg_lf_active, fctx));18754+ std::array::from_fn(|k| edge_params(&vctx, x0, y0 + 4 * k));
18634 if let Some((len, level)) = ps[0] {18755 if let Some((len, level)) = ps[0] {
18635 if ps.iter().all(|p| *p == ps[0])18756 if ps.iter().all(|p| *p == ps[0])
18636 && filter_edge16(18757 && filter_edge16(
@@ -18663,7 +18784,7 @@ fn deblock_plane(
18663 y0 += 16;18784 y0 += 16;
18664 continue;18785 continue;
18665 }18786 }
18666- if let Some((len, level)) = edge_params(lf, n, plane_idx, 0, chroma, x0, y0, seg_lf_active, fctx) {18787+ if let Some((len, level)) = edge_params(&vctx, x0, y0) {
18667 if crate::envflags::env_flag!("EC_AV1_DEBLOCK_TRACE_V") && plane_idx == 0 {18788 if crate::envflags::env_flag!("EC_AV1_DEBLOCK_TRACE_V") && plane_idx == 0 {
18668 eprintln!("VEDGE x0={x0} y0={y0} len={len} level={level}");18789 eprintln!("VEDGE x0={x0} y0={y0} len={len} level={level}");
18669 }18790 }
@@ -18692,13 +18813,14 @@ fn deblock_plane(
18692 // SAFETY: this band writes only columns `x_lo..x_hi`.18813 // SAFETY: this band writes only columns `x_lo..x_hi`.
18693 #[allow(unsafe_code)]18814 #[allow(unsafe_code)]
18694 let plane = unsafe { sp.get() };18815 let plane = unsafe { sp.get() };
18816+ let hctx = EdgeCtx::new(lf, n, plane_idx, 1, chroma, seg_lf_active, fctx);
18695 let mut y0 = 4usize;18817 let mut y0 = 4usize;
18696 while y0 < th {18818 while y0 < th {
18697 let mut x0 = x_lo;18819 let mut x0 = x_lo;
18698 while x0 < x_hi {18820 while x0 < x_hi {
18699 if x0 + 16 <= x_hi && y0 >= 8 && y0 + 7 <= rows {18821 if x0 + 16 <= x_hi && y0 >= 8 && y0 + 7 <= rows {
18700 let ps: [Option<(u8, i32)>; 4] =18822 let ps: [Option<(u8, i32)>; 4] =
18701- std::array::from_fn(|k| edge_params(lf, n, plane_idx, 1, chroma, x0 + 4 * k, y0, seg_lf_active, fctx));18823+ std::array::from_fn(|k| edge_params(&hctx, x0 + 4 * k, y0));
18702 if let Some((len, level)) = ps[0] {18824 if let Some((len, level)) = ps[0] {
18703 if ps.iter().all(|p| *p == ps[0])18825 if ps.iter().all(|p| *p == ps[0])
18704 && filter_edge16(18826 && filter_edge16(
@@ -18731,7 +18853,7 @@ fn deblock_plane(
18731 x0 += 16;18853 x0 += 16;
18732 continue;18854 continue;
18733 }18855 }
18734- if let Some((len, level)) = edge_params(lf, n, plane_idx, 1, chroma, x0, y0, seg_lf_active, fctx) {18856+ if let Some((len, level)) = edge_params(&hctx, x0, y0) {
18735 if crate::envflags::env_flag!("EC_AV1_DEBLOCK_TRACE")18857 if crate::envflags::env_flag!("EC_AV1_DEBLOCK_TRACE")
18736 && plane_idx == 018858 && plane_idx == 0
18737 && x0 == 4418859 && x0 == 44