forge
Explore
Log in

Don't you dare go Hollow

Merge branch 'lane-copy' (2bbf188c): output crop is a truncation of the owned padded buffer when the width is SB-aligned; LR deblocked snapshot copies only the 4 rows per stripe boundary it can read; byte-exact. Also: grain row wrappers kept for the kernel-vs-scalar tests are cfg(test) (4 dead_code warnings from ab320fac)

Browse files at this commit

5a5f194970fe869722b81aa509afad2cf46d82b4Ahmet Kaan GümüşParent: 95d7ceb36f0c53
crates/ec-av1/src/decode.rsmodified+73 -14
@@ -18606,10 +18606,65 @@ fn scratch_empty() -> Vec<u16> {
1860618606 v
1860718607 }
1860818608
18609+/// A pooled buffer of exactly `n` samples whose contents are NOT cleared --
18610+/// for a caller that writes every element it later reads (lane-copy: the
18611+/// post-deblock snapshot writes 4 rows in 64 and reads no other).
18612+fn scratch_sized(n: usize) -> Vec<u16> {
18613+ let mut v = PLANE_SCRATCH.with(|p| p.borrow_mut().pop()).unwrap_or_default();
18614+ if v.len() > n {
18615+ v.truncate(n);
18616+ } else if v.len() < n {
18617+ v.resize(n, 0);
18618+ }
18619+ v
18620+}
18621+
1860918622 /// A plane's post-deblock snapshot, its samples taken from the pool (every
1861018623 /// other field is `usize`).
18611-fn plane_snapshot(p: &PlaneBuf<'_>) -> PlaneBuf<'static> {
18612- PlaneBuf { data: std::borrow::Cow::Owned(scratch_from(&p.data)), ..*p }
18624+///
18625+/// lane-copy: loop restoration is the only reader, and spec 7.17.2's stripe
18626+/// boundary substitution reads the post-deblock plane at just four rows
18627+/// around each interior stripe boundary (`row` outside
18628+/// `[stripe_v_start, stripe_v_end)` resolves to `stripe_v_start-1`,
18629+/// `stripe_v_start-2`, `stripe_v_end` or `stripe_v_end+1` in
18630+/// `restoration::lr_src_row`); stripes are `64 >> ss_y` rows offset by
18631+/// `RESTORATION_UNIT_OFFSET = 8 >> ss_y`, so the boundaries sit at
18632+/// `k*stripe_h - offset`. Copying only those rows turns a full-plane
18633+/// memmove per plane per frame into 4 rows in 64 (luma). Every other row of
18634+/// the pooled buffer keeps whatever a previous frame left there -- an
18635+/// initialised value that no reader ever reaches.
18636+fn plane_snapshot(p: &PlaneBuf<'_>, ss_y: u32) -> PlaneBuf<'static> {
18637+ let mut data = scratch_sized(p.data.len());
18638+ let rows = if p.width == 0 { 0 } else { p.data.len() / p.width };
18639+ let stripe_h = 64usize >> ss_y;
18640+ let offset = 8usize >> ss_y;
18641+ let mut b = stripe_h - offset;
18642+ while b < rows {
18643+ let (lo, hi) = (b - 2, (b + 2).min(rows));
18644+ data[lo * p.width..hi * p.width].copy_from_slice(&p.data[lo * p.width..hi * p.width]);
18645+ b += stripe_h;
18646+ }
18647+ PlaneBuf { data: std::borrow::Cow::Owned(data), ..*p }
18648+}
18649+
18650+/// The output crop, taking the plane by value: a frame whose width is
18651+/// already superblock-aligned (only its height is padded) has a padded
18652+/// buffer whose stride equals the visible width, so the crop is a pure
18653+/// truncation of the buffer we already own -- no full-plane copy at all
18654+/// (lane-copy: three ~6 MB memmoves per 4K frame on the frame thread's
18655+/// serial tail). Any other shape falls back to the row-by-row copy.
18656+fn crop_owned(plane: PlaneBuf<'_>, w: usize, h: usize) -> Vec<u16> {
18657+ let stride = plane.width;
18658+ let mut data = plane.data.into_owned();
18659+ if stride == w {
18660+ data.truncate(w * h);
18661+ return data;
18662+ }
18663+ let mut out = Vec::with_capacity(w * h);
18664+ for row in 0..h {
18665+ out.extend(data[row * stride..][..w].iter().copied());
18666+ }
18667+ out
1861318668 }
1861418669
1861518670 /// Hands a buffer back to the pool; the capacity is what is worth keeping.
@@ -21324,7 +21379,7 @@ pub(crate) fn decode_key_frame_tile_with_cdfs(
2132421379 && lr.loop_restoration_size[p] != 0
2132521380 });
2132621381 let deblocked =
21327- lr_active.then(|| (plane_snapshot(&y), plane_snapshot(&u), plane_snapshot(&v)));
21382+ lr_active.then(|| (plane_snapshot(&y, 0), plane_snapshot(&u, 1), plane_snapshot(&v, 1)));
2132821383 // lane-tiny r4: post-deblock / post-CDEF dumps mirroring aomdec's own
2132921384 // EC_AV1_POSTDEBLOCK_DUMP (decodeframe.c ~5404) -- with the pre-filter
2133021385 // dump above these bisect WHICH filter stage introduced a mismatch.
@@ -21383,9 +21438,11 @@ pub(crate) fn decode_key_frame_tile_with_cdfs(
2138321438 );
2138421439 }
2138521440 if let Some((db_y, db_u, db_v)) = deblocked {
21386- scratch_return(db_y.data.into_owned());
21387- scratch_return(db_u.data.into_owned());
21441+ // LIFO pool: return in reverse so the next frame's `y`/`u`/`v`
21442+ // snapshots pop buffers of their own size and never resize.
2138821443 scratch_return(db_v.data.into_owned());
21444+ scratch_return(db_u.data.into_owned());
21445+ scratch_return(db_y.data.into_owned());
2138921446 }
2139021447
2139121448 let (fw, fh) = (lr_w, frame_height as usize);
@@ -21461,9 +21518,9 @@ pub(crate) fn decode_key_frame_tile_with_cdfs(
2146121518 Picture {
2146221519 width: fw,
2146321520 height: fh,
21464- y: crop(&y, fw, fh),
21465- u: crop(&u, fw.div_ceil(2), fh.div_ceil(2)),
21466- v: crop(&v, fw.div_ceil(2), fh.div_ceil(2)),
21521+ y: crop_owned(y, fw, fh),
21522+ u: crop_owned(u, fw.div_ceil(2), fh.div_ceil(2)),
21523+ v: crop_owned(v, fw.div_ceil(2), fh.div_ceil(2)),
2146721524 },
2146821525 result_cdfs,
2146921526 ))
@@ -33476,7 +33533,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs(
3347633533 && lr.loop_restoration_size[p] != 0
3347733534 });
3347833535 let deblocked =
33479- lr_active.then(|| (plane_snapshot(&y), plane_snapshot(&u), plane_snapshot(&v)));
33536+ lr_active.then(|| (plane_snapshot(&y, 0), plane_snapshot(&u, 1), plane_snapshot(&v, 1)));
3348033537 // lane-tiny r4: post-deblock / post-CDEF dumps mirroring aomdec's own
3348133538 // EC_AV1_POSTDEBLOCK_DUMP (decodeframe.c ~5404) -- with the pre-filter
3348233539 // dump above these bisect WHICH filter stage introduced a mismatch.
@@ -33535,9 +33592,11 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs(
3353533592 );
3353633593 }
3353733594 if let Some((db_y, db_u, db_v)) = deblocked {
33538- scratch_return(db_y.data.into_owned());
33539- scratch_return(db_u.data.into_owned());
33595+ // LIFO pool: return in reverse so the next frame's `y`/`u`/`v`
33596+ // snapshots pop buffers of their own size and never resize.
3354033597 scratch_return(db_v.data.into_owned());
33598+ scratch_return(db_u.data.into_owned());
33599+ scratch_return(db_y.data.into_owned());
3354133600 }
3354233601
3354333602 let motion_field = build_motion_field(
@@ -33607,9 +33666,9 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs(
3360733666 Picture {
3360833667 width: fw,
3360933668 height: fh,
33610- y: crop(&y, fw, fh),
33611- u: crop(&u, fw.div_ceil(2), fh.div_ceil(2)),
33612- v: crop(&v, fw.div_ceil(2), fh.div_ceil(2)),
33669+ y: crop_owned(y, fw, fh),
33670+ u: crop_owned(u, fw.div_ceil(2), fh.div_ceil(2)),
33671+ v: crop_owned(v, fw.div_ceil(2), fh.div_ceil(2)),
3361333672 },
3361433673 result_cdfs,
3361533674 motion_field,
crates/ec-av1/src/film_grain.rsmodified+4 -0
@@ -466,6 +466,7 @@ fn add_noise_to_block(
466466 /// multiply, `Round2` and clip; every other CPU runs the scalar loop, which
467467 /// stays the reference (`simd_matches_scalar_luma_noise_row` pins the pair).
468468 #[allow(unsafe_code)]
469+#[cfg(test)]
469470 fn luma_noise_row(
470471 pixels: &mut [u16],
471472 grain: &[i32],
@@ -551,6 +552,7 @@ struct ChromaNoise {
551552 /// stays the reference (`simd_matches_scalar_chroma_noise_row` pins the pair).
552553 #[allow(unsafe_code)]
553554 #[allow(clippy::too_many_arguments)]
555+#[cfg(test)]
554556 fn chroma_noise_row(
555557 chroma: &mut [u16],
556558 luma: &[u16],
@@ -670,6 +672,7 @@ mod simd {
670672 /// `scaling` non-empty.
671673 #[target_feature(enable = "avx2")]
672674 #[allow(clippy::too_many_arguments)]
675+ #[cfg(test)]
673676 pub unsafe fn luma_noise_row_avx2(
674677 pixels: &mut [u16],
675678 grain: &[i32],
@@ -773,6 +776,7 @@ mod simd {
773776 /// `grain` at least `chroma.len()`, and `scaling` non-empty.
774777 #[target_feature(enable = "avx2")]
775778 #[allow(clippy::too_many_arguments)]
779+ #[cfg(test)]
776780 pub unsafe fn chroma_noise_row_avx2(
777781 chroma: &mut [u16],
778782 luma: &[u16],