diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index 9886a692..07748bf7 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -17549,117 +17549,39 @@ fn scratch_return(v: Vec) { PLANE_SCRATCH.with(|p| p.borrow_mut().push(v)); } -fn apply_cdef( +/// One CDEF mi-row band, as a real function rather than the body of the +/// `run_bands` closure: inside the closure the destination planes are +/// `&mut` references derived from a `par::Shared` raw pointer in the same +/// scope as the `src_*` snapshot reads, so LLVM cannot tell the two apart and +/// re-loads through them on every tap. Reference parameters restore `noalias` +/// (lane-perf11 (a): -x% instructions at one filter thread). +#[allow(clippy::too_many_arguments)] +fn cdef_band( + r_lo: usize, + r_hi: usize, y: &mut PlaneBuf<'static>, u: &mut PlaneBuf<'static>, v: &mut PlaneBuf<'static>, + src_y: &[u16], + src_u: &[u16], + src_v: &[u16], cdef: &CdefParams, skip_grid: &Neighbours, - // This frame header's own CROPPED luma size, for the straddling-unit - // counter only -- CDEF itself walks the mi-rounded plane. - (frame_w, frame_h): (usize, usize), fctx: &crate::decode::FrameCtx, + idx_grid: &[u8], + sb_cols: usize, + coeff_shift: i32, + damping: i32, + damping_uv: i32, + (frame_w, frame_h): (usize, usize), + fctx: &crate::decode::FrameCtx, ) { - // lane-part32 r2 debug rung, env-gated: see `apply_deblock`'s sibling. - if crate::envflags::env_flag!("EC_AV1_DEBUG_SKIP_CDEF") { - return; - } - // lane-cdefstrip r1 instrument, BEFORE either early return so it is valid - // for every frame: an 8x8 CDEF unit none of whose four mi cells a coded - // block wrote this frame. `Neighbours` seeds `skip_grid` all-false, so a - // decode arm that forgets `fill_skip_grid_rect` reads "not skip" and CDEF - // filters a unit libaom's `is_8x8_block_skip` (`cdef.c:29-38`) may exclude. - // Always 0 on every stream measured; a gate asserts it stays 0. - { - let mut r = 0usize; - while r < skip_grid.mi_rows { - let mut c = 0usize; - while c < skip_grid.mi_cols { - if (0..2).any(|rr| { - (0..2).any(|cc| { - !skip_grid - .skip_written - .get((r + rr) * skip_grid.skip_grid_cols_mi + (c + cc)) - .copied() - .unwrap_or(true) - }) - }) { - CDEF_UNWRITTEN_SKIP_UNITS.with(|c2| c2.set(c2.get() + 1)); - if crate::envflags::env_flag!("EC_AV1_DEBUG_CDEF_BAND") { - eprintln!("cdef band unwritten at mi ({r},{c}) px ({},{})", r * 4, c * 4); - } - } - let cell = |rr: usize, cc: usize| { - skip_grid - .skip_grid - .get((r + rr) * skip_grid.skip_grid_cols_mi + (c + cc)) - .copied() - .unwrap_or(false) - }; - let first = cell(0, 0); - if (0..2).any(|rr| (0..2).any(|cc| cell(rr, cc) != first)) { - CDEF_MIXED_SKIP_UNITS.with(|c2| c2.set(c2.get() + 1)); - } - c += 2; - } - r += 2; - } - } - // `cdef.bits == 0` still means "no filtering at all" whenever the single - // (index 0) strength pair is all-zero -- the fast path every existing - // gate stream takes. A `bits > 0` frame can still be all-zero-strength - // at every index, but that is rare enough not to special-case; the - // per-superblock lookup below degrades to the same index-0 read anyway - // when [`CDEF_IDX_GRID`] was never populated (`bits == 0`). - if cdef.bits == 0 - && cdef.y_pri_strength[0] == 0 - && cdef.y_sec_strength[0] == 0 - && cdef.uv_pri_strength[0] == 0 - && cdef.uv_sec_strength[0] == 0 - { - return; - } - let sb_cols = fctx.cdef_sb_cols.with(|c| c.get()); - // One entry per superblock (a few hundred at 4K), so this clone is not - // one of the per-frame copies lane-perf6 pooled below. - let idx_grid = fctx.cdef_idx_grid.with(|g| g.borrow().clone()); let strength_idx = |mi_r: usize, mi_c: usize| -> usize { if sb_cols == 0 { return 0; } let (sb_r, sb_c) = (mi_r / SB_MI as usize, mi_c / SB_MI as usize); - idx_grid - .get(sb_r * sb_cols + sb_c) - .copied() - .unwrap_or(0) as usize + idx_grid.get(sb_r * sb_cols + sb_c).copied().unwrap_or(0) as usize }; - let src_y = scratch_from(&y.data); - let src_u = scratch_from(&u.data); - let src_v = scratch_from(&v.data); - // libaom `av1_cdef_filter_fb`: `pri_strength = level << coeff_shift`, - // `sec_strength <<= coeff_shift`, `damping += coeff_shift - (pli != 0)`. - let coeff_shift = i32::from(bit_depth(fctx)) - 8; - let damping = i32::from(cdef.damping) + coeff_shift; - let damping_uv = (damping - 1).max(0); - // lane-filt1: every CDEF unit reads ONLY the pre-CDEF snapshots - // (`src_y`/`src_u`/`src_v`, immutable here) and writes its own 8x8 luma / - // 4x4 chroma square, so units are independent. Bands are mi ROW bands (2 - // mi = 8 luma rows, 4 chroma rows), which makes each band's writes a - // disjoint row range of all three planes. - let (sy, su, sv) = ( - crate::par::Shared::new(y), - crate::par::Shared::new(u), - crate::par::Shared::new(v), - ); - let mi_rows = skip_grid.mi_rows; - let cbands: Vec<(usize, usize)> = crate::par::bands(mi_rows.div_ceil(2), crate::par::filter_threads()) - .into_iter() - .map(|(a, b)| (a * 2, (b * 2).min(mi_rows))) - .collect(); - crate::par::run_bands(&cbands, fctx, |r_lo, r_hi, fctx| { - // SAFETY: this band writes only the plane rows its own mi rows cover, - // disjoint from every other band's (see `Shared`). - #[allow(unsafe_code)] - let (y, u, v) = unsafe { (sy.get(), su.get(), sv.get()) }; // One scratch window per plane kind, reused by every unit (8x8 luma and // 4x4 chroma, each grown by the [`CDEF_BORDER`] tap ring). let mut win_y = [0i32; (8 + 2 * CDEF_BORDER) * (8 + 2 * CDEF_BORDER)]; @@ -17686,7 +17608,7 @@ fn apply_cdef( continue; } let (y_stride, y_true_w, y_true_h) = (y.width, y.true_width, y.true_height); - cdef_gather(&src_y, y_stride, y_true_w, y_true_h, ox, oy, 8, 8, &mut win_y); + cdef_gather(src_y, y_stride, y_true_w, y_true_h, ox, oy, 8, 8, &mut win_y); let sample_y = |r: i32, c: i32| -> i32 { win_y[(r + CDEF_BORDER as i32) as usize * (8 + 2 * CDEF_BORDER) + (c + CDEF_BORDER as i32) as usize] @@ -17751,7 +17673,7 @@ fn apply_cdef( let enable_secondary_uv = uv_sec_strength != 0; if enable_primary_uv || enable_secondary_uv { let (cox, coy) = (ox / 2, oy / 2); - for (plane, src_p) in [(&mut *u, &src_u), (&mut *v, &src_v)] { + for (plane, src_p) in [(&mut *u, src_u), (&mut *v, src_v)] { let (tw, th) = (plane.true_width, plane.true_height); let stride = plane.width; cdef_gather(src_p, stride, tw, th, cox, coy, 4, 4, &mut win_uv); @@ -17778,6 +17700,128 @@ fn apply_cdef( } mi_r += 2; } +} + +fn apply_cdef( + y: &mut PlaneBuf<'static>, + u: &mut PlaneBuf<'static>, + v: &mut PlaneBuf<'static>, + cdef: &CdefParams, + skip_grid: &Neighbours, + // This frame header's own CROPPED luma size, for the straddling-unit + // counter only -- CDEF itself walks the mi-rounded plane. + (frame_w, frame_h): (usize, usize), fctx: &crate::decode::FrameCtx, +) { + // lane-part32 r2 debug rung, env-gated: see `apply_deblock`'s sibling. + if crate::envflags::env_flag!("EC_AV1_DEBUG_SKIP_CDEF") { + return; + } + // lane-cdefstrip r1 instrument, BEFORE either early return so it is valid + // for every frame: an 8x8 CDEF unit none of whose four mi cells a coded + // block wrote this frame. `Neighbours` seeds `skip_grid` all-false, so a + // decode arm that forgets `fill_skip_grid_rect` reads "not skip" and CDEF + // filters a unit libaom's `is_8x8_block_skip` (`cdef.c:29-38`) may exclude. + // Always 0 on every stream measured; a gate asserts it stays 0. + { + let mut r = 0usize; + while r < skip_grid.mi_rows { + let mut c = 0usize; + while c < skip_grid.mi_cols { + if (0..2).any(|rr| { + (0..2).any(|cc| { + !skip_grid + .skip_written + .get((r + rr) * skip_grid.skip_grid_cols_mi + (c + cc)) + .copied() + .unwrap_or(true) + }) + }) { + CDEF_UNWRITTEN_SKIP_UNITS.with(|c2| c2.set(c2.get() + 1)); + if crate::envflags::env_flag!("EC_AV1_DEBUG_CDEF_BAND") { + eprintln!("cdef band unwritten at mi ({r},{c}) px ({},{})", r * 4, c * 4); + } + } + let cell = |rr: usize, cc: usize| { + skip_grid + .skip_grid + .get((r + rr) * skip_grid.skip_grid_cols_mi + (c + cc)) + .copied() + .unwrap_or(false) + }; + let first = cell(0, 0); + if (0..2).any(|rr| (0..2).any(|cc| cell(rr, cc) != first)) { + CDEF_MIXED_SKIP_UNITS.with(|c2| c2.set(c2.get() + 1)); + } + c += 2; + } + r += 2; + } + } + // `cdef.bits == 0` still means "no filtering at all" whenever the single + // (index 0) strength pair is all-zero -- the fast path every existing + // gate stream takes. A `bits > 0` frame can still be all-zero-strength + // at every index, but that is rare enough not to special-case; the + // per-superblock lookup below degrades to the same index-0 read anyway + // when [`CDEF_IDX_GRID`] was never populated (`bits == 0`). + if cdef.bits == 0 + && cdef.y_pri_strength[0] == 0 + && cdef.y_sec_strength[0] == 0 + && cdef.uv_pri_strength[0] == 0 + && cdef.uv_sec_strength[0] == 0 + { + return; + } + let sb_cols = fctx.cdef_sb_cols.with(|c| c.get()); + // One entry per superblock (a few hundred at 4K), so this clone is not + // one of the per-frame copies lane-perf6 pooled below. + let idx_grid = fctx.cdef_idx_grid.with(|g| g.borrow().clone()); + let src_y = scratch_from(&y.data); + let src_u = scratch_from(&u.data); + let src_v = scratch_from(&v.data); + // libaom `av1_cdef_filter_fb`: `pri_strength = level << coeff_shift`, + // `sec_strength <<= coeff_shift`, `damping += coeff_shift - (pli != 0)`. + let coeff_shift = i32::from(bit_depth(fctx)) - 8; + let damping = i32::from(cdef.damping) + coeff_shift; + let damping_uv = (damping - 1).max(0); + // lane-filt1: every CDEF unit reads ONLY the pre-CDEF snapshots + // (`src_y`/`src_u`/`src_v`, immutable here) and writes its own 8x8 luma / + // 4x4 chroma square, so units are independent. Bands are mi ROW bands (2 + // mi = 8 luma rows, 4 chroma rows), which makes each band's writes a + // disjoint row range of all three planes. + let (sy, su, sv) = ( + crate::par::Shared::new(y), + crate::par::Shared::new(u), + crate::par::Shared::new(v), + ); + let mi_rows = skip_grid.mi_rows; + let cbands: Vec<(usize, usize)> = crate::par::bands(mi_rows.div_ceil(2), crate::par::filter_threads()) + .into_iter() + .map(|(a, b)| (a * 2, (b * 2).min(mi_rows))) + .collect(); + crate::par::run_bands(&cbands, fctx, |r_lo, r_hi, fctx| { + // SAFETY: this band writes only the plane rows its own mi rows cover, + // disjoint from every other band's (see `Shared`). + #[allow(unsafe_code)] + let (y, u, v) = unsafe { (sy.get(), su.get(), sv.get()) }; + cdef_band( + r_lo, + r_hi, + y, + u, + v, + &src_y, + &src_u, + &src_v, + cdef, + skip_grid, + &idx_grid, + sb_cols, + coeff_shift, + damping, + damping_uv, + (frame_w, frame_h), + fctx, + ); }); scratch_return(src_y); scratch_return(src_u);