forge
Explore
Log in

Don't you dare go Hollow

The loop-restoration stage replays the decoder's own kernel instead of decoding the re-coded tile

Browse files at this commit

34dfd1e2d87598ae4c7560112d51d0764b3a7c4fAhmet Kaan GümüşParent: 5bde5fc
  • perf
    • the loop-restoration stage replays the decoder's own kernel instead of decoding the re-coded tile
Show full message
perf(av1): the loop-restoration stage replays the decoder's own kernel instead of decoding the re-coded tile

The chosen Wiener units used to be realised by DECODING the re-coded tile
(5.6% of a 4K frame), purely to reach the restored reference frame. Loop
restoration reads nothing of that tile beyond the per-unit filters the
stage just chose, so the final replay now keeps its post-CDEF luma plus
the TRUE post-deblock stripe-border rows (plane_snapshot, exactly the
rows restoration::lr_src_row substitutes) and runs
apply_loop_restoration_plane on them. Chroma restores nothing, so only
luma moves; the wide-margin stash's luma is refreshed in place.

Pinned by encoder::tests::filter_replay_final_matches_the_capture_decode,
which now also decodes the re-coded tile and asserts picture, chroma and
wide-margin luma identical (fired on 8 frames of the gate clip).
crates/ec-av1/src/decode.rsmodified+90 -0
@@ -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 });
@@ -19957,6 +19964,15 @@ fn replay_inner(
1995719964 pre,
1995819965 );
1995919966 drop(ct);
19967+ if final_pass.is_some_and(|(_, want)| want) {
19968+ // The loop-restoration search runs on the stages above (where
19969+ // post-CDEF stands in for post-deblock, as the capture decode
19970+ // had it); the FILTER this frame ends up applying reads the
19971+ // real post-deblock rows, which is what the decoder does.
19972+ let snapshot = r.deblocked.as_ref().map(|(_, dy, ..)| plane_snapshot(dy, 0));
19973+ let scratch = &mut r.scratch;
19974+ r.final_luma = snapshot.map(|d| (pooled(&y, scratch), d));
19975+ }
1996019976 let stages = final_pass.and_then(|(_, want)| {
1996119977 want.then(|| FilterStages {
1996219978 deblocked: [y.data.to_vec(), u.data.to_vec(), v.data.to_vec()],
@@ -20022,6 +20038,80 @@ fn replay_inner(
2002220038 })
2002320039 }
2002420040
20041+/// lane-av1cap2: the loop-restoration half of the final replay. Once the
20042+/// units are chosen the tile is coded a second time to carry them, and that
20043+/// re-coded tile used to be DECODED to reach the restored reference frame
20044+/// (5.6% of a 4K frame's encode). Loop restoration reads nothing of that
20045+/// tile but the per-unit filters the encoder just picked: it is the
20046+/// decoder's own [`crate::restoration::apply_loop_restoration_plane`] on
20047+/// the post-CDEF plane the final replay produced, with the TRUE
20048+/// post-deblock stripe-border rows -- the same call the decoder's
20049+/// `apply_loop_restoration` makes, on the same input. Only luma moves: the
20050+/// encoder never writes a chroma restoration type other than
20051+/// `RESTORE_NONE`, whose kernel hands its input straight back.
20052+///
20053+/// Returns the cropped luma plane and refreshes the wide-margin stash's own
20054+/// luma (restoration writes strictly inside `frame_width x frame_height`,
20055+/// so the margin columns past it stand). `None` when nothing was captured
20056+/// -- the caller then decodes for real.
20057+pub(crate) fn replay_restoration(
20058+ lr: &LoopRestorationParams,
20059+ units: &[Option<crate::restoration::WienerInfo>],
20060+ fctx: &FrameCtx,
20061+) -> Option<Vec<u16>> {
20062+ #[cfg(test)]
20063+ if REPLAY_DISABLED.load(std::sync::atomic::Ordering::Relaxed) {
20064+ return None;
20065+ }
20066+ REPLAY.with_borrow_mut(|slot| {
20067+ let r = slot.as_mut()?;
20068+ let (cdefed, deblocked) = r.final_luma.as_ref()?;
20069+ let (fw, fh) = (r.frame_width, r.frame_height);
20070+ let mut grid = crate::restoration::RestorationGrid::new(lr, fw as u32, fh as u32);
20071+ let (horz, vert) = (grid.horz_units[0], grid.vert_units[0]);
20072+ if horz == 0 || units.len() != horz * vert {
20073+ return None;
20074+ }
20075+ for (i, filter) in units.iter().enumerate() {
20076+ if let Some(info) = filter {
20077+ grid.set(0, i / horz, i % horz, crate::restoration::UnitFilter::Wiener(*info));
20078+ }
20079+ }
20080+ let stride = cdefed.width;
20081+ let mut out = scratch_empty();
20082+ crate::restoration::apply_loop_restoration_plane(
20083+ &cdefed.data,
20084+ &deblocked.data,
20085+ stride,
20086+ fw,
20087+ fh,
20088+ 0,
20089+ lr.frame_restoration_type[0],
20090+ lr.loop_restoration_size[0],
20091+ &grid,
20092+ 0,
20093+ &mut out,
20094+ None,
20095+ fctx,
20096+ );
20097+ fctx.last_frame_wide_margin.with(|m| {
20098+ if let Some(p) = m.borrow_mut().as_mut() {
20099+ let tw = p.width;
20100+ for row in 0..p.height.min(fh) {
20101+ let n = tw.min(fw);
20102+ p.y[row * tw..][..n].copy_from_slice(&out[row * stride..][..n]);
20103+ }
20104+ }
20105+ });
20106+ let mut y = Vec::with_capacity(fw * fh);
20107+ for row in 0..fh {
20108+ y.extend(out[row * stride..][..fw].iter().copied());
20109+ }
20110+ scratch_return(out);
20111+ Some(y)
20112+ })
20113+}
20114+
2002520115 /// The output crop, taking the plane by value: a frame whose width is
2002620116 /// already superblock-aligned (only its height is padded) has a padded
2002720117 /// 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));