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 {
19735 /// 1080p frame's wall in allocation, not in filtering. Recycled here19735 /// 1080p frame's wall in allocation, not in filtering. Recycled here
19736 /// instead; the pool holds at most the six planes one candidate has live.19736 /// instead; the pool holds at most the six planes one candidate has live.
19737 scratch: Vec<Vec<u16>>,19737 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>)>,
19738 /// The last deblocked planes and the levels that produced them.19744 /// The last deblocked planes and the levels that produced them.
19739 /// Deblocking depends on nothing else the search moves, and the search19745 /// Deblocking depends on nothing else the search moves, and the search
19740 /// fixes the levels before it walks the CDEF strengths, so every19746 /// fixes the levels before it walks the CDEF strengths, so every
@@ -19824,6 +19830,7 @@ fn capture_filter_replay(
19824 true_width,19830 true_width,
19825 true_height,19831 true_height,
19826 scratch: Vec::new(),19832 scratch: Vec::new(),
19833+ final_luma: None,
19827 deblocked: None,19834 deblocked: None,
19828 });19835 });
19829 });19836 });
@@ -19908,6 +19915,10 @@ fn replay_inner(
19908 REPLAY.with_borrow_mut(|slot| {19915 REPLAY.with_borrow_mut(|slot| {
19909 let r = slot.as_mut()?;19916 let r = slot.as_mut()?;
19910 let hit = matches!(&r.deblocked, Some((lf, ..)) if lf == loop_filter);19917 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;
19911 let cpt = crate::par::timer(crate::par::S_COPY);19922 let cpt = crate::par::timer(crate::par::S_COPY);
19912 let (mut y, mut u, mut v) = if hit {19923 let (mut y, mut u, mut v) = if hit {
19913 let (deblocked, scratch) = (&r.deblocked, &mut r.scratch);19924 let (deblocked, scratch) = (&r.deblocked, &mut r.scratch);
@@ -19935,9 +19946,23 @@ fn replay_inner(
19935 r.frame_height,19946 r.frame_height,
19936 fctx,19947 fctx,
19937 );19948 );
19938- let scratch = &mut r.scratch;19949+ // lane-av1cap2: an all-zero-strength CDEF writes no sample at
19939- r.deblocked =19950+ // all ([`cdef_all_zero`]'s own early return), so THESE planes
19940- Some((*loop_filter, pooled(&y, scratch), pooled(&u, scratch), pooled(&v, scratch)));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+ }
19941 (y, u, v)19966 (y, u, v)
19942 };19967 };
19943 drop(cpt);19968 drop(cpt);
@@ -19957,6 +19982,21 @@ fn replay_inner(
19957 pre,19982 pre,
19958 );19983 );
19959 drop(ct);19984 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+ }
19960 let stages = final_pass.and_then(|(_, want)| {20000 let stages = final_pass.and_then(|(_, want)| {
19961 want.then(|| FilterStages {20001 want.then(|| FilterStages {
19962 deblocked: [y.data.to_vec(), u.data.to_vec(), v.data.to_vec()],20002 deblocked: [y.data.to_vec(), u.data.to_vec(), v.data.to_vec()],
@@ -19977,6 +20017,17 @@ fn replay_inner(
19977 let (fw, fh) = (r.frame_width, r.frame_height);20017 let (fw, fh) = (r.frame_width, r.frame_height);
19978 if fw == r.width && fh == r.height {20018 if fw == r.width && fh == r.height {
19979 fctx.last_frame_wide_margin.with(|m| *m.borrow_mut() = None);20019 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+ }
19980 return Some((20031 return Some((
19981 Picture {20032 Picture {
19982 width: r.width,20033 width: r.width,
@@ -20012,16 +20063,96 @@ fn replay_inner(
20012 u: crop(&u, fw.div_ceil(2), fh.div_ceil(2)),20063 u: crop(&u, fw.div_ceil(2), fh.div_ceil(2)),
20013 v: crop(&v, fw.div_ceil(2), fh.div_ceil(2)),20064 v: crop(&v, fw.div_ceil(2), fh.div_ceil(2)),
20014 };20065 };
20015- // The working planes outlive the crop, so they go back to the pool.20066+ // The working planes outlive the crop: either they ARE the
20016- for plane in [y, u, v] {20067+ // deblocked planes this candidate's successors reuse, or they go
20017- if r.scratch.len() < 6 {20068+ // back to the pool.
20018- r.scratch.push(plane.data.into_owned());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+ }
20019 }20076 }
20020 }20077 }
20021 Some((out, stages))20078 Some((out, stages))
20022 })20079 })
20023 }20080 }
20024 20081
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+
20025 /// The output crop, taking the plane by value: a frame whose width is20156 /// The output crop, taking the plane by value: a frame whose width is
20026 /// already superblock-aligned (only its height is padded) has a padded20157 /// already superblock-aligned (only its height is padded) has a padded
20027 /// buffer whose stride equals the visible width, so the crop is a pure20158 /// 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(
6135 _ => panic!("one path captured filter stages and the other did not"),6135 _ => panic!("one path captured filter stages and the other did not"),
6136 }6136 }
6137 }6137 }
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.
6139 let mut lr = LoopRestorationParams::default();6141 let mut lr = LoopRestorationParams::default();
6140 if lr_on {6142 if lr_on {
6141 let mut want = LoopRestorationParams {6143 let mut want = LoopRestorationParams {
@@ -6165,11 +6167,50 @@ fn pick_and_apply_filters(
6165 if units.iter().any(Option::is_some) {6167 if units.iter().any(Option::is_some) {
6166 let coded = recode(cdef.bits, sb_cols, &idx_grid, &units)?;6168 let coded = recode(cdef.bits, sb_cols, &idx_grid, &units)?;
6167 let _t = crate::par::timer(crate::par::S_CAPTURE);6169 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+ }
6169 *tiles = coded;6209 *tiles = coded;
6170 lr = want;6210 lr = want;
6171 }6211 }
6172 }6212 }
6213+ crate::decode::clear_filter_replay();
6173 header.loop_restoration = lr;6214 header.loop_restoration = lr;
6174 let dec_cw = filtered.width.div_ceil(2);6215 let dec_cw = filtered.width.div_ceil(2);
6175 let (cw, ch) = (fw.div_ceil(2), fh.div_ceil(2));6216 let (cw, ch) = (fw.div_ceil(2), fh.div_ceil(2));