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> {
18606 v18606 v
18607 }18607 }
18608 18608
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+
18609 /// A plane's post-deblock snapshot, its samples taken from the pool (every18622 /// A plane's post-deblock snapshot, its samples taken from the pool (every
18610 /// other field is `usize`).18623 /// other field is `usize`).
18611-fn plane_snapshot(p: &PlaneBuf<'_>) -> PlaneBuf<'static> {18624+///
18612- PlaneBuf { data: std::borrow::Cow::Owned(scratch_from(&p.data)), ..*p }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
18613 }18668 }
18614 18669
18615 /// Hands a buffer back to the pool; the capacity is what is worth keeping.18670 /// 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(
21324 && lr.loop_restoration_size[p] != 021379 && lr.loop_restoration_size[p] != 0
21325 });21380 });
21326 let deblocked =21381 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)));
21328 // lane-tiny r4: post-deblock / post-CDEF dumps mirroring aomdec's own21383 // lane-tiny r4: post-deblock / post-CDEF dumps mirroring aomdec's own
21329 // EC_AV1_POSTDEBLOCK_DUMP (decodeframe.c ~5404) -- with the pre-filter21384 // EC_AV1_POSTDEBLOCK_DUMP (decodeframe.c ~5404) -- with the pre-filter
21330 // dump above these bisect WHICH filter stage introduced a mismatch.21385 // dump above these bisect WHICH filter stage introduced a mismatch.
@@ -21383,9 +21438,11 @@ pub(crate) fn decode_key_frame_tile_with_cdfs(
21383 );21438 );
21384 }21439 }
21385 if let Some((db_y, db_u, db_v)) = deblocked {21440 if let Some((db_y, db_u, db_v)) = deblocked {
21386- scratch_return(db_y.data.into_owned());21441+ // LIFO pool: return in reverse so the next frame's `y`/`u`/`v`
21387- scratch_return(db_u.data.into_owned());21442+ // snapshots pop buffers of their own size and never resize.
21388 scratch_return(db_v.data.into_owned());21443 scratch_return(db_v.data.into_owned());
21444+ scratch_return(db_u.data.into_owned());
21445+ scratch_return(db_y.data.into_owned());
21389 }21446 }
21390 21447
21391 let (fw, fh) = (lr_w, frame_height as usize);21448 let (fw, fh) = (lr_w, frame_height as usize);
@@ -21461,9 +21518,9 @@ pub(crate) fn decode_key_frame_tile_with_cdfs(
21461 Picture {21518 Picture {
21462 width: fw,21519 width: fw,
21463 height: fh,21520 height: fh,
21464- y: crop(&y, fw, fh),21521+ y: crop_owned(y, fw, fh),
21465- u: crop(&u, fw.div_ceil(2), fh.div_ceil(2)),21522+ u: crop_owned(u, fw.div_ceil(2), fh.div_ceil(2)),
21466- v: crop(&v, fw.div_ceil(2), fh.div_ceil(2)),21523+ v: crop_owned(v, fw.div_ceil(2), fh.div_ceil(2)),
21467 },21524 },
21468 result_cdfs,21525 result_cdfs,
21469 ))21526 ))
@@ -33476,7 +33533,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs(
33476 && lr.loop_restoration_size[p] != 033533 && lr.loop_restoration_size[p] != 0
33477 });33534 });
33478 let deblocked =33535 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)));
33480 // lane-tiny r4: post-deblock / post-CDEF dumps mirroring aomdec's own33537 // lane-tiny r4: post-deblock / post-CDEF dumps mirroring aomdec's own
33481 // EC_AV1_POSTDEBLOCK_DUMP (decodeframe.c ~5404) -- with the pre-filter33538 // EC_AV1_POSTDEBLOCK_DUMP (decodeframe.c ~5404) -- with the pre-filter
33482 // dump above these bisect WHICH filter stage introduced a mismatch.33539 // dump above these bisect WHICH filter stage introduced a mismatch.
@@ -33535,9 +33592,11 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs(
33535 );33592 );
33536 }33593 }
33537 if let Some((db_y, db_u, db_v)) = deblocked {33594 if let Some((db_y, db_u, db_v)) = deblocked {
33538- scratch_return(db_y.data.into_owned());33595+ // LIFO pool: return in reverse so the next frame's `y`/`u`/`v`
33539- scratch_return(db_u.data.into_owned());33596+ // snapshots pop buffers of their own size and never resize.
33540 scratch_return(db_v.data.into_owned());33597 scratch_return(db_v.data.into_owned());
33598+ scratch_return(db_u.data.into_owned());
33599+ scratch_return(db_y.data.into_owned());
33541 }33600 }
33542 33601
33543 let motion_field = build_motion_field(33602 let motion_field = build_motion_field(
@@ -33607,9 +33666,9 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs(
33607 Picture {33666 Picture {
33608 width: fw,33667 width: fw,
33609 height: fh,33668 height: fh,
33610- y: crop(&y, fw, fh),33669+ y: crop_owned(y, fw, fh),
33611- u: crop(&u, fw.div_ceil(2), fh.div_ceil(2)),33670+ u: crop_owned(u, fw.div_ceil(2), fh.div_ceil(2)),
33612- v: crop(&v, fw.div_ceil(2), fh.div_ceil(2)),33671+ v: crop_owned(v, fw.div_ceil(2), fh.div_ceil(2)),
33613 },33672 },
33614 result_cdfs,33673 result_cdfs,
33615 motion_field,33674 motion_field,
crates/ec-av1/src/film_grain.rsmodified+4 -0
@@ -466,6 +466,7 @@ fn add_noise_to_block(
466 /// multiply, `Round2` and clip; every other CPU runs the scalar loop, which466 /// multiply, `Round2` and clip; every other CPU runs the scalar loop, which
467 /// stays the reference (`simd_matches_scalar_luma_noise_row` pins the pair).467 /// stays the reference (`simd_matches_scalar_luma_noise_row` pins the pair).
468 #[allow(unsafe_code)]468 #[allow(unsafe_code)]
469+#[cfg(test)]
469 fn luma_noise_row(470 fn luma_noise_row(
470 pixels: &mut [u16],471 pixels: &mut [u16],
471 grain: &[i32],472 grain: &[i32],
@@ -551,6 +552,7 @@ struct ChromaNoise {
551 /// stays the reference (`simd_matches_scalar_chroma_noise_row` pins the pair).552 /// stays the reference (`simd_matches_scalar_chroma_noise_row` pins the pair).
552 #[allow(unsafe_code)]553 #[allow(unsafe_code)]
553 #[allow(clippy::too_many_arguments)]554 #[allow(clippy::too_many_arguments)]
555+#[cfg(test)]
554 fn chroma_noise_row(556 fn chroma_noise_row(
555 chroma: &mut [u16],557 chroma: &mut [u16],
556 luma: &[u16],558 luma: &[u16],
@@ -670,6 +672,7 @@ mod simd {
670 /// `scaling` non-empty.672 /// `scaling` non-empty.
671 #[target_feature(enable = "avx2")]673 #[target_feature(enable = "avx2")]
672 #[allow(clippy::too_many_arguments)]674 #[allow(clippy::too_many_arguments)]
675+ #[cfg(test)]
673 pub unsafe fn luma_noise_row_avx2(676 pub unsafe fn luma_noise_row_avx2(
674 pixels: &mut [u16],677 pixels: &mut [u16],
675 grain: &[i32],678 grain: &[i32],
@@ -773,6 +776,7 @@ mod simd {
773 /// `grain` at least `chroma.len()`, and `scaling` non-empty.776 /// `grain` at least `chroma.len()`, and `scaling` non-empty.
774 #[target_feature(enable = "avx2")]777 #[target_feature(enable = "avx2")]
775 #[allow(clippy::too_many_arguments)]778 #[allow(clippy::too_many_arguments)]
779+ #[cfg(test)]
776 pub unsafe fn chroma_noise_row_avx2(780 pub unsafe fn chroma_noise_row_avx2(
777 chroma: &mut [u16],781 chroma: &mut [u16],
778 luma: &[u16],782 luma: &[u16],