forge
Explore
Log in

Don't you dare go Hollow

The output crop and the post-deblock snapshot stop copying whole planes

Browse files at this commit

36f0c532280bca2dd27bee8143cb879374b18d4fAhmet Kaan GümüşParent: 8cdbcf4
  • perf
    • the output crop and the post-deblock snapshot stop copying whole planes
Show full message
perf(av1): the output crop and the post-deblock snapshot stop copying whole planes

Two bulk copies on the frame thread's serial tail, both of them avoidable:

* The output crop rebuilt every plane row by row whenever the padded
  coding surface was bigger than the visible frame. A 4K 3840x1608 frame
  is superblock-aligned in WIDTH and padded only in HEIGHT, so its padded
  buffer's stride already equals the visible width and the "crop" is a
  pure truncation of the buffer we already own. `crop_owned` takes the
  plane by value and truncates in that case, falling back to the old
  row-by-row copy for a genuinely narrower plane.

* The post-deblock snapshot loop restoration reads cloned all three
  planes. Spec 7.17.2's stripe boundary substitution only ever reads the
  two rows above and the two below each interior stripe boundary
  (`restoration::lr_src_row` resolves every out-of-stripe row to
  `stripe_v_start-1/-2` or `stripe_v_end/+1`), and stripes are
  `64 >> ss_y` rows offset by `RESTORATION_UNIT_OFFSET`, so the snapshot
  now copies 4 rows in 64 into a pooled buffer whose other rows keep a
  previous frame's samples -- initialised values no reader reaches.

Byte-exact: seg1080/seg4k/seg4k_lr/palette hashes unchanged at 1, 4 and
16 frame threads (the last with 4 filter + 2 recon threads).
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 ))
@@ -33460,7 +33517,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs(
3346033517 && lr.loop_restoration_size[p] != 0
3346133518 });
3346233519 let deblocked =
33463- lr_active.then(|| (plane_snapshot(&y), plane_snapshot(&u), plane_snapshot(&v)));
33520+ lr_active.then(|| (plane_snapshot(&y, 0), plane_snapshot(&u, 1), plane_snapshot(&v, 1)));
3346433521 // lane-tiny r4: post-deblock / post-CDEF dumps mirroring aomdec's own
3346533522 // EC_AV1_POSTDEBLOCK_DUMP (decodeframe.c ~5404) -- with the pre-filter
3346633523 // dump above these bisect WHICH filter stage introduced a mismatch.
@@ -33519,9 +33576,11 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs(
3351933576 );
3352033577 }
3352133578 if let Some((db_y, db_u, db_v)) = deblocked {
33522- scratch_return(db_y.data.into_owned());
33523- scratch_return(db_u.data.into_owned());
33579+ // LIFO pool: return in reverse so the next frame's `y`/`u`/`v`
33580+ // snapshots pop buffers of their own size and never resize.
3352433581 scratch_return(db_v.data.into_owned());
33582+ scratch_return(db_u.data.into_owned());
33583+ scratch_return(db_y.data.into_owned());
3352533584 }
3352633585
3352733586 let motion_field = build_motion_field(
@@ -33591,9 +33650,9 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs(
3359133650 Picture {
3359233651 width: fw,
3359333652 height: fh,
33594- y: crop(&y, fw, fh),
33595- u: crop(&u, fw.div_ceil(2), fh.div_ceil(2)),
33596- v: crop(&v, fw.div_ceil(2), fh.div_ceil(2)),
33653+ y: crop_owned(y, fw, fh),
33654+ u: crop_owned(u, fw.div_ceil(2), fh.div_ceil(2)),
33655+ v: crop_owned(v, fw.div_ceil(2), fh.div_ceil(2)),
3359733656 },
3359833657 result_cdfs,
3359933658 motion_field,