The loop-restoration stage replays the decoder's own kernel instead of decoding the re-coded tile
Browse files at this commit34dfd1e2d87598ae4c7560112d51d0764b3a7c4fParent: 5bde5fc- perf
- the loop-restoration stage replays the decoder's own kernel instead of decoding the re-coded tile
2 changed files +133 -2
- crates/ec-av1/src/decode.rs +90 -0
- crates/ec-av1/src/encode.rs +43 -2
crates/ec-av1/src/decode.rsmodified+90 -0
| @@ -19735,6 +19735,12 @@ pub(crate) struct FilterReplay { | ||
| 19735 | 19735 | /// 1080p frame's wall in allocation, not in filtering. Recycled here |
| 19736 | 19736 | /// instead; the pool holds at most the six planes one candidate has live. |
| 19737 | 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 | 19744 | /// The last deblocked planes and the levels that produced them. |
| 19739 | 19745 | /// Deblocking depends on nothing else the search moves, and the search |
| 19740 | 19746 | /// fixes the levels before it walks the CDEF strengths, so every |
| @@ -19824,6 +19830,7 @@ fn capture_filter_replay( | ||
| 19824 | 19830 | true_width, |
| 19825 | 19831 | true_height, |
| 19826 | 19832 | scratch: Vec::new(), |
| 19833 | + final_luma: None, | |
| 19827 | 19834 | deblocked: None, |
| 19828 | 19835 | }); |
| 19829 | 19836 | }); |
| @@ -19957,6 +19964,15 @@ fn replay_inner( | ||
| 19957 | 19964 | pre, |
| 19958 | 19965 | ); |
| 19959 | 19966 | 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 | + } | |
| 19960 | 19976 | let stages = final_pass.and_then(|(_, want)| { |
| 19961 | 19977 | want.then(|| FilterStages { |
| 19962 | 19978 | deblocked: [y.data.to_vec(), u.data.to_vec(), v.data.to_vec()], |
| @@ -20022,6 +20038,80 @@ fn replay_inner( | ||
| 20022 | 20038 | }) |
| 20023 | 20039 | } |
| 20024 | 20040 | |
| 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 | + | |
| 20025 | 20115 | /// The output crop, taking the plane by value: a frame whose width is |
| 20026 | 20116 | /// already superblock-aligned (only its height is padded) has a padded |
| 20027 | 20117 | /// 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 | 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 | 6141 | let mut lr = LoopRestorationParams::default(); |
| 6140 | 6142 | if lr_on { |
| 6141 | 6143 | let mut want = LoopRestorationParams { |
| @@ -6165,11 +6167,50 @@ fn pick_and_apply_filters( | ||
| 6165 | 6167 | if units.iter().any(Option::is_some) { |
| 6166 | 6168 | let coded = recode(cdef.bits, sb_cols, &idx_grid, &units)?; |
| 6167 | 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 | 6209 | *tiles = coded; |
| 6170 | 6210 | lr = want; |
| 6171 | 6211 | } |
| 6172 | 6212 | } |
| 6213 | + crate::decode::clear_filter_replay(); | |
| 6173 | 6214 | header.loop_restoration = lr; |
| 6174 | 6215 | let dec_cw = filtered.width.div_ceil(2); |
| 6175 | 6216 | let (cw, ch) = (fw.div_ceil(2), fh.div_ceil(2)); |