forge
Explore
Log in

Don't you dare go Hollow

Merge branch 'lane-av1cap2' (74e32723): the LR-recode decode is replaced by a restoration replay on the final replay's planes; all-zero CDEF candidates keep their deblocked planes as the cache

Browse files at this commit

8497549a2411935d29259d260e33ed2af3ed20b1Ahmet Kaan GümüşParent: 037459c3a4b874
Byte-identical on all three gate clips x 4 q; 4K 4x2/8 threads 1.43 ->
1.51 fps (capture-decode bucket 152 -> 7 ms, plane copies 235 -> 204 ms).
A per-unit CDEF scratch is not provably exact (write coverage is per-unit
variance dependent) and stays deferred with its unblocker.
crates/ec-av1/src/decode.rsmodified+138 -7
@@ -19735,6 +19735,12 @@ pub(crate) struct FilterReplay {
1973519735 /// 1080p frame's wall in allocation, not in filtering. Recycled here
1973619736 /// instead; the pool holds at most the six planes one candidate has live.
1973719737 scratch: Vec<Vec<u16>>,
19738+ /// lane-av1cap2: the final replay's post-CDEF luma plane and the TRUE
19739+ /// post-deblock rows loop restoration substitutes at its stripe
19740+ /// borders ([`plane_snapshot`], exactly `restoration::lr_src_row`'s
19741+ /// rows) -- what [`replay_restoration`] filters, instead of decoding
19742+ /// the re-coded tile a second time.
19743+ final_luma: Option<(PlaneBuf<'static>, PlaneBuf<'static>)>,
1973819744 /// The last deblocked planes and the levels that produced them.
1973919745 /// Deblocking depends on nothing else the search moves, and the search
1974019746 /// fixes the levels before it walks the CDEF strengths, so every
@@ -19824,6 +19830,7 @@ fn capture_filter_replay(
1982419830 true_width,
1982519831 true_height,
1982619832 scratch: Vec::new(),
19833+ final_luma: None,
1982719834 deblocked: None,
1982819835 });
1982919836 });
@@ -19908,6 +19915,10 @@ fn replay_inner(
1990819915 REPLAY.with_borrow_mut(|slot| {
1990919916 let r = slot.as_mut()?;
1991019917 let hit = matches!(&r.deblocked, Some((lf, ..)) if lf == loop_filter);
19918+ // Set when the working planes ARE the deblocked planes (see below):
19919+ // they go into the cache at the end, and the picture is cropped out
19920+ // of them rather than taking their buffers.
19921+ let mut share = false;
1991119922 let cpt = crate::par::timer(crate::par::S_COPY);
1991219923 let (mut y, mut u, mut v) = if hit {
1991319924 let (deblocked, scratch) = (&r.deblocked, &mut r.scratch);
@@ -19935,9 +19946,23 @@ fn replay_inner(
1993519946 r.frame_height,
1993619947 fctx,
1993719948 );
19938- let scratch = &mut r.scratch;
19939- r.deblocked =
19940- Some((*loop_filter, pooled(&y, scratch), pooled(&u, scratch), pooled(&v, scratch)));
19949+ // lane-av1cap2: an all-zero-strength CDEF writes no sample at
19950+ // all ([`cdef_all_zero`]'s own early return), so THESE planes
19951+ // are already this candidate's output -- they become the
19952+ // deblock cache at the end instead of being copied into it
19953+ // here, which is three ~6 MB copies less per candidate that
19954+ // moves the levels (the whole deblocking half of the search:
19955+ // its CDEF strengths are still (0,0)).
19956+ share = cdef_all_zero(cdef);
19957+ if !share {
19958+ let scratch = &mut r.scratch;
19959+ r.deblocked = Some((
19960+ *loop_filter,
19961+ pooled(&y, scratch),
19962+ pooled(&u, scratch),
19963+ pooled(&v, scratch),
19964+ ));
19965+ }
1994119966 (y, u, v)
1994219967 };
1994319968 drop(cpt);
@@ -19957,6 +19982,21 @@ fn replay_inner(
1995719982 pre,
1995819983 );
1995919984 drop(ct);
19985+ if final_pass.is_some_and(|(_, want)| want) {
19986+ // The loop-restoration search runs on the stages above (where
19987+ // post-CDEF stands in for post-deblock, as the capture decode
19988+ // had it); the FILTER this frame ends up applying reads the
19989+ // real post-deblock rows, which is what the decoder does.
19990+ let snapshot = if share {
19991+ // Nothing filtered these planes after the deblock, so they
19992+ // are the post-deblock picture themselves.
19993+ Some(plane_snapshot(&y, 0))
19994+ } else {
19995+ r.deblocked.as_ref().map(|(_, dy, ..)| plane_snapshot(dy, 0))
19996+ };
19997+ let scratch = &mut r.scratch;
19998+ r.final_luma = snapshot.map(|d| (pooled(&y, scratch), d));
19999+ }
1996020000 let stages = final_pass.and_then(|(_, want)| {
1996120001 want.then(|| FilterStages {
1996220002 deblocked: [y.data.to_vec(), u.data.to_vec(), v.data.to_vec()],
@@ -19977,6 +20017,17 @@ fn replay_inner(
1997720017 let (fw, fh) = (r.frame_width, r.frame_height);
1997820018 if fw == r.width && fh == r.height {
1997920019 fctx.last_frame_wide_margin.with(|m| *m.borrow_mut() = None);
20020+ if share {
20021+ let picture = Picture {
20022+ width: r.width,
20023+ height: r.height,
20024+ y: y.data.to_vec(),
20025+ u: u.data.to_vec(),
20026+ v: v.data.to_vec(),
20027+ };
20028+ r.deblocked = Some((*loop_filter, y, u, v));
20029+ return Some((picture, stages));
20030+ }
1998020031 return Some((
1998120032 Picture {
1998220033 width: r.width,
@@ -20012,16 +20063,96 @@ fn replay_inner(
2001220063 u: crop(&u, fw.div_ceil(2), fh.div_ceil(2)),
2001320064 v: crop(&v, fw.div_ceil(2), fh.div_ceil(2)),
2001420065 };
20015- // The working planes outlive the crop, so they go back to the pool.
20016- for plane in [y, u, v] {
20017- if r.scratch.len() < 6 {
20018- r.scratch.push(plane.data.into_owned());
20066+ // The working planes outlive the crop: either they ARE the
20067+ // deblocked planes this candidate's successors reuse, or they go
20068+ // back to the pool.
20069+ if share {
20070+ r.deblocked = Some((*loop_filter, y, u, v));
20071+ } else {
20072+ for plane in [y, u, v] {
20073+ if r.scratch.len() < 6 {
20074+ r.scratch.push(plane.data.into_owned());
20075+ }
2001920076 }
2002020077 }
2002120078 Some((out, stages))
2002220079 })
2002320080 }
2002420081
20082+/// lane-av1cap2: the loop-restoration half of the final replay. Once the
20083+/// units are chosen the tile is coded a second time to carry them, and that
20084+/// re-coded tile used to be DECODED to reach the restored reference frame
20085+/// (5.6% of a 4K frame's encode). Loop restoration reads nothing of that
20086+/// tile but the per-unit filters the encoder just picked: it is the
20087+/// decoder's own [`crate::restoration::apply_loop_restoration_plane`] on
20088+/// the post-CDEF plane the final replay produced, with the TRUE
20089+/// post-deblock stripe-border rows -- the same call the decoder's
20090+/// `apply_loop_restoration` makes, on the same input. Only luma moves: the
20091+/// encoder never writes a chroma restoration type other than
20092+/// `RESTORE_NONE`, whose kernel hands its input straight back.
20093+///
20094+/// Returns the cropped luma plane and refreshes the wide-margin stash's own
20095+/// luma (restoration writes strictly inside `frame_width x frame_height`,
20096+/// so the margin columns past it stand). `None` when nothing was captured
20097+/// -- the caller then decodes for real.
20098+pub(crate) fn replay_restoration(
20099+ lr: &LoopRestorationParams,
20100+ units: &[Option<crate::restoration::WienerInfo>],
20101+ fctx: &FrameCtx,
20102+) -> Option<Vec<u16>> {
20103+ #[cfg(test)]
20104+ if REPLAY_DISABLED.load(std::sync::atomic::Ordering::Relaxed) {
20105+ return None;
20106+ }
20107+ REPLAY.with_borrow_mut(|slot| {
20108+ let r = slot.as_mut()?;
20109+ let (cdefed, deblocked) = r.final_luma.as_ref()?;
20110+ let (fw, fh) = (r.frame_width, r.frame_height);
20111+ let mut grid = crate::restoration::RestorationGrid::new(lr, fw as u32, fh as u32);
20112+ let (horz, vert) = (grid.horz_units[0], grid.vert_units[0]);
20113+ if horz == 0 || units.len() != horz * vert {
20114+ return None;
20115+ }
20116+ for (i, filter) in units.iter().enumerate() {
20117+ if let Some(info) = filter {
20118+ grid.set(0, i / horz, i % horz, crate::restoration::UnitFilter::Wiener(*info));
20119+ }
20120+ }
20121+ let stride = cdefed.width;
20122+ let mut out = scratch_empty();
20123+ crate::restoration::apply_loop_restoration_plane(
20124+ &cdefed.data,
20125+ &deblocked.data,
20126+ stride,
20127+ fw,
20128+ fh,
20129+ 0,
20130+ lr.frame_restoration_type[0],
20131+ lr.loop_restoration_size[0],
20132+ &grid,
20133+ 0,
20134+ &mut out,
20135+ None,
20136+ fctx,
20137+ );
20138+ fctx.last_frame_wide_margin.with(|m| {
20139+ if let Some(p) = m.borrow_mut().as_mut() {
20140+ let tw = p.width;
20141+ for row in 0..p.height.min(fh) {
20142+ let n = tw.min(fw);
20143+ p.y[row * tw..][..n].copy_from_slice(&out[row * stride..][..n]);
20144+ }
20145+ }
20146+ });
20147+ let mut y = Vec::with_capacity(fw * fh);
20148+ for row in 0..fh {
20149+ y.extend(out[row * stride..][..fw].iter().copied());
20150+ }
20151+ scratch_return(out);
20152+ Some(y)
20153+ })
20154+}
20155+
2002520156 /// The output crop, taking the plane by value: a frame whose width is
2002620157 /// already superblock-aligned (only its height is padded) has a padded
2002720158 /// buffer whose stride equals the visible width, so the crop is a pure
crates/ec-av1/src/encode.rsmodified+43 -2
@@ -6135,7 +6135,9 @@ fn pick_and_apply_filters(
61356135 _ => panic!("one path captured filter stages and the other did not"),
61366136 }
61376137 }
6138- crate::decode::clear_filter_replay();
6138+ // lane-av1cap2: the capture stays live past the restoration search --
6139+ // `crate::decode::replay_restoration` filters the same post-CDEF plane
6140+ // rather than decoding the re-coded tile. Cleared at the end instead.
61396141 let mut lr = LoopRestorationParams::default();
61406142 if lr_on {
61416143 let mut want = LoopRestorationParams {
@@ -6165,11 +6167,50 @@ fn pick_and_apply_filters(
61656167 if units.iter().any(Option::is_some) {
61666168 let coded = recode(cdef.bits, sb_cols, &idx_grid, &units)?;
61676169 let _t = crate::par::timer(crate::par::S_CAPTURE);
6168- filtered = decode(&lf, &cdef, &hdr, &coded, &want)?;
6170+ // lane-av1cap2: that re-coded tile used to be DECODED here to
6171+ // reach the restored reference frame -- 5.6% of a 4K frame,
6172+ // for a filter that reads nothing of the tile but the per-unit
6173+ // Wiener filters this stage just chose. Run the decoder's own
6174+ // restoration kernel on the replay's post-CDEF plane instead
6175+ // (`crate::decode::replay_restoration`), with the real
6176+ // post-deblock stripe borders.
6177+ match crate::decode::replay_restoration(&want, &units, fctx) {
6178+ Some(y) => {
6179+ #[cfg(test)]
6180+ let margin = fctx
6181+ .last_frame_wide_margin
6182+ .with(|m| m.borrow().as_ref().map(|p| p.y.clone()));
6183+ #[cfg(test)]
6184+ if crate::decode::verify_final_replay() {
6185+ let want_pic = decode(&lf, &cdef, &hdr, &coded, &want)?;
6186+ assert!(
6187+ (want_pic.width, want_pic.height) == (filtered.width, filtered.height)
6188+ && want_pic.u == filtered.u
6189+ && want_pic.v == filtered.v,
6190+ "the restoration replay moved a chroma plane or the picture size",
6191+ );
6192+ assert!(
6193+ want_pic.y == y,
6194+ "the restoration replay is not the decode of the re-coded tile",
6195+ );
6196+ assert!(
6197+ fctx.last_frame_wide_margin
6198+ .with(|m| m.borrow().as_ref().map(|p| p.y.clone()))
6199+ == margin,
6200+ "the restoration replay's wide-margin luma is not the decode's",
6201+ );
6202+ }
6203+ filtered.y = y;
6204+ }
6205+ // Nothing captured: the winner came out of a full decode,
6206+ // so this one does too.
6207+ None => filtered = decode(&lf, &cdef, &hdr, &coded, &want)?,
6208+ }
61696209 *tiles = coded;
61706210 lr = want;
61716211 }
61726212 }
6213+ crate::decode::clear_filter_replay();
61736214 header.loop_restoration = lr;
61746215 let dec_cw = filtered.width.div_ceil(2);
61756216 let (cw, ch) = (fw.div_ceil(2), fh.div_ceil(2));