forge
Explore
Log in

Don't you dare go Hollow

Run the pipelined loop restoration as its own worker

Browse files at this commit

eca8ad93c3c01f6905ae1ae95ea1d1363b644064Ahmet Kaan GümüşParent: 013daf3
  • perf
    • run the pipelined loop restoration as its own worker
Show full message
perf(av1): run the pipelined loop restoration as its own worker

Serialised as a fourth step inside pipe_run's band loop, LR measured ~1%
slower than the whole-frame form (which at least spread its RU rows over
the filter pool). A second batch task, chained one CDEF band behind the
first through its own PipeSync, keeps it off the deblock/CDEF critical
path instead.
crates/ec-av1/src/decode.rsmodified+137 -87
@@ -19645,14 +19645,6 @@ struct PipeCfg<'a> {
19645 nbands: usize,19645 nbands: usize,
19646 cdef_on: bool,19646 cdef_on: bool,
19647 snap_on: bool,19647 snap_on: bool,
19648- /// lane-pipefilt2: loop restoration runs in the pipeline too, one RU row
19649- /// band behind CDEF (see [`pipe_run`]'s LR step). `lr_grid` is the
19650- /// frame's unit-filter grid, whose `Vec`s are sized once by
19651- /// `RestorationGrid::new` and only ever written at units the parse
19652- /// thread is many bands ahead of.
19653- lr: &'a LoopRestorationParams,
19654- lr_grid: *const crate::restoration::RestorationGrid,
19655- lr_on: bool,
19656 }19648 }
19657 19649
19658 // SAFETY: the `*const u8` is a read-only view of a Vec that is allocated once19650 // SAFETY: the `*const u8` is a read-only view of a Vec that is allocated once
@@ -19671,6 +19663,7 @@ fn pipe_run(
19671 sn: crate::par::Shared<'_, Neighbours>,19663 sn: crate::par::Shared<'_, Neighbours>,
19672 cfg: PipeCfg<'_>,19664 cfg: PipeCfg<'_>,
19673 snap: &mut Option<(PlaneBuf<'static>, PlaneBuf<'static>, PlaneBuf<'static>)>,19665 snap: &mut Option<(PlaneBuf<'static>, PlaneBuf<'static>, PlaneBuf<'static>)>,
19666+ lr_sync: Option<&PipeSync>,
19674 fctx: &crate::decode::FrameCtx,19667 fctx: &crate::decode::FrameCtx,
19675 ) {19668 ) {
19676 // SAFETY: the frame thread parses (and reconstructs) bands the pipeline19669 // SAFETY: the frame thread parses (and reconstructs) bands the pipeline
@@ -19684,45 +19677,11 @@ fn pipe_run(
19684 let damping_uv = (damping - 1).max(0);19677 let damping_uv = (damping - 1).max(0);
19685 let (fw, fh) = (cfg.frame_w, cfg.frame_h);19678 let (fw, fh) = (cfg.frame_w, cfg.frame_h);
19686 let nb = cfg.nbands;19679 let nb = cfg.nbands;
19687- // lane-pipefilt2: loop restoration's RU row `r` writes `out` rows
19688- // `v_start..v_end`; it reads post-CDEF rows only inside that span and
19689- // the post-deblock snapshot at most two rows past `v_end` (see
19690- // `restoration::lr_src_row`). CDEF band `b` covers luma rows
19691- // `64b..64b+64` and runs at iteration `b + 3`, the snapshot band at
19692- // `b + 2`, so the row is ready at iteration `(v_end - 1) / 64 + 3`: the
19693- // snapshot condition `(v_end + 1) / 64 <= k - 2` is implied by it, the
19694- // two row indices being at most one band apart. LR therefore runs in
19695- // the SAME iteration as the CDEF band that finishes its last row,
19696- // right after it.
19697- let mut lr_planes: Vec<LrPlane> = Vec::new();
19698- if cfg.lr_on {
19699- let dims = [(fw, fh, 0u32, y.width, y.data.len()),
19700- (fw.div_ceil(2), fh.div_ceil(2), 1, u.width, u.data.len()),
19701- (fw.div_ceil(2), fh.div_ceil(2), 1, v.width, v.data.len())];
19702- for (p, &(pw, ph, ss, stride, len)) in dims.iter().enumerate() {
19703- let rows = crate::restoration::lr_unit_rows(
19704- ph,
19705- cfg.lr.loop_restoration_size[p],
19706- ss,
19707- cfg.lr.frame_restoration_type[p],
19708- );
19709- if rows.is_empty() {
19710- continue;
19711- }
19712- lr_planes.push(LrPlane {
19713- p,
19714- out: scratch_sized(len),
19715- rows,
19716- next: 0,
19717- pw,
19718- ph,
19719- ss,
19720- stride,
19721- });
19722- }
19723- }
19724 for k in 0..nb + 3 {19680 for k in 0..nb + 3 {
19725 if !sync.wait_for((k + 2).min(nb)) {19681 if !sync.wait_for((k + 2).min(nb)) {
19682+ if let Some(ls) = lr_sync {
19683+ ls.close(nb, true);
19684+ }
19726 return;19685 return;
19727 }19686 }
19728 if k < nb {19687 if k < nb {
@@ -19748,49 +19707,104 @@ fn pipe_run(
19748 );19707 );
19749 }19708 }
19750 }19709 }
19751- if !lr_planes.is_empty() && k >= 3 && k - 3 < nb {19710+ // lane-pipefilt2: CDEF bands `0..=k-3` are now complete -- release
19752- let band = k - 3;19711+ // them to the loop restoration worker.
19753- // SAFETY: sized once per frame by `RestorationGrid::new`.19712+ if let Some(ls) = lr_sync {
19754- let grid = unsafe { &*cfg.lr_grid };19713+ ls.note(k.saturating_sub(2));
19755- let Some((sy2, su2, sv2)) = snap.as_ref() else { continue };19714+ }
19756- for lp in &mut lr_planes {19715+ }
19757- let stripe = 64usize >> lp.ss;19716+ if let Some(ls) = lr_sync {
19758- let r_lo = lp.next;19717+ ls.close(nb, false);
19759- while lp.next < lp.rows.len()19718+ }
19760- && (lp.rows[lp.next].1 as usize - 1) / stripe <= band19719+}
19761- {19720+
19762- lp.next += 1;19721+/// The pipeline's loop restoration worker, a second task alongside
19763- }19722+/// [`pipe_run`]: restoration-unit row `r` writes `out` rows
19764- if lp.next == r_lo {19723+/// `v_start..v_end`, reads post-CDEF rows only inside that span and the
19765- continue;19724+/// post-deblock snapshot at most two rows past `v_end` (see
19766- }19725+/// `restoration::lr_src_row`), so it is ready as soon as CDEF band
19767- let (src, deb) = match lp.p {19726+/// `(v_end - 1) / 64` is done -- `pipe_run` runs that band at its own
19768- 0 => (&y.data, &sy2.data),19727+/// iteration `band + 3` and the snapshot band one iteration earlier, so the
19769- 1 => (&u.data, &su2.data),19728+/// snapshot condition `(v_end + 1) / 64 <= band + 1` is implied, the two row
19770- _ => (&v.data, &sv2.data),19729+/// indices being at most one band apart. Running it as its own worker (not
19771- };19730+/// as a fourth step inside `pipe_run`'s loop) keeps it OFF the deblock/CDEF
19772- crate::restoration::apply_loop_restoration_plane(19731+/// critical path: serialised into that loop it measured ~1% slower than the
19773- src,19732+/// whole-frame form, which at least ran its RU rows across the filter pool.
19774- deb,19733+#[allow(unsafe_code)]
19775- lp.stride,19734+fn pipe_run_lr(
19776- lp.pw,19735+ sync: &PipeSync,
19777- lp.ph,19736+ sy: crate::par::Shared<'_, PlaneBuf<'static>>,
19778- lp.ss,19737+ su: crate::par::Shared<'_, PlaneBuf<'static>>,
19779- cfg.lr.frame_restoration_type[lp.p],19738+ sv: crate::par::Shared<'_, PlaneBuf<'static>>,
19780- cfg.lr.loop_restoration_size[lp.p],19739+ snap: crate::par::Shared<'_, Option<(PlaneBuf<'static>, PlaneBuf<'static>, PlaneBuf<'static>)>>,
19781- grid,19740+ cfg: LrCfg<'_>,
19782- lp.p,19741+ fctx: &crate::decode::FrameCtx,
19783- &mut lp.out,19742+) {
19784- Some((r_lo, lp.next)),19743+ // SAFETY: this worker only reads bands `pipe_run` has finished filtering
19785- fctx,19744+ // and writes its own `out` buffers; the final swap happens after the
19786- );19745+ // last CDEF band, which is `pipe_run`'s last plane write.
19746+ let (y, u, v) = unsafe { (sy.get(), su.get(), sv.get()) };
19747+ let snap = unsafe { snap.get() };
19748+ let grid = unsafe { &*cfg.lr_grid };
19749+ let (fw, fh) = (cfg.frame_w, cfg.frame_h);
19750+ let dims = [
19751+ (fw, fh, 0u32, y.width, y.data.len()),
19752+ (fw.div_ceil(2), fh.div_ceil(2), 1, u.width, u.data.len()),
19753+ (fw.div_ceil(2), fh.div_ceil(2), 1, v.width, v.data.len()),
19754+ ];
19755+ let mut planes: Vec<LrPlane> = Vec::new();
19756+ for (p, &(pw, ph, ss, stride, len)) in dims.iter().enumerate() {
19757+ let rows = crate::restoration::lr_unit_rows(
19758+ ph,
19759+ cfg.lr.loop_restoration_size[p],
19760+ ss,
19761+ cfg.lr.frame_restoration_type[p],
19762+ );
19763+ if rows.is_empty() {
19764+ continue;
19765+ }
19766+ planes.push(LrPlane { p, out: scratch_sized(len), rows, next: 0, pw, ph, ss, stride });
19767+ }
19768+ for band in 0..cfg.nbands {
19769+ if !sync.wait_for(band + 1) {
19770+ return;
19771+ }
19772+ let Some((sy2, su2, sv2)) = snap.as_ref() else { return };
19773+ for lp in &mut planes {
19774+ let stripe = 64usize >> lp.ss;
19775+ let r_lo = lp.next;
19776+ while lp.next < lp.rows.len() && (lp.rows[lp.next].1 as usize - 1) / stripe <= band {
19777+ lp.next += 1;
19778+ }
19779+ if lp.next == r_lo {
19780+ continue;
19787 }19781 }
19782+ let (src, deb) = match lp.p {
19783+ 0 => (&y.data, &sy2.data),
19784+ 1 => (&u.data, &su2.data),
19785+ _ => (&v.data, &sv2.data),
19786+ };
19787+ crate::restoration::apply_loop_restoration_plane(
19788+ src,
19789+ deb,
19790+ lp.stride,
19791+ lp.pw,
19792+ lp.ph,
19793+ lp.ss,
19794+ cfg.lr.frame_restoration_type[lp.p],
19795+ cfg.lr.loop_restoration_size[lp.p],
19796+ grid,
19797+ lp.p,
19798+ &mut lp.out,
19799+ Some((r_lo, lp.next)),
19800+ fctx,
19801+ );
19788 }19802 }
19789 }19803 }
19790 // The RU rows partition `0..plane_h`; the mi-rounded rows below it are19804 // The RU rows partition `0..plane_h`; the mi-rounded rows below it are
19791 // untouched by restoration and copied here, then the restored plane19805 // untouched by restoration and copied here, then the restored plane
19792 // takes the picture's place (the whole-frame path's own final swap).19806 // takes the picture's place (the whole-frame path's own final swap).
19793- for mut lp in lr_planes {19807+ for mut lp in planes {
19794 let buf: &mut PlaneBuf<'static> = match lp.p {19808 let buf: &mut PlaneBuf<'static> = match lp.p {
19795 0 => &mut *y,19809 0 => &mut *y,
19796 1 => &mut *u,19810 1 => &mut *u,
@@ -19806,6 +19820,21 @@ fn pipe_run(
19806 }19820 }
19807 }19821 }
19808 19822
19823+/// Everything [`pipe_run_lr`] needs beyond the planes.
19824+struct LrCfg<'a> {
19825+ lr: &'a LoopRestorationParams,
19826+ /// Sized once per frame by `RestorationGrid::new`; the parse thread is
19827+ /// many bands ahead of every unit this worker reads.
19828+ lr_grid: *const crate::restoration::RestorationGrid,
19829+ frame_w: usize,
19830+ frame_h: usize,
19831+ nbands: usize,
19832+}
19833+
19834+// SAFETY: as `PipeCfg`'s -- a read-only view of a per-frame allocation.
19835+#[allow(unsafe_code)]
19836+unsafe impl Send for LrCfg<'_> {}
19837+
19809 /// One plane's pipelined loop restoration state (see [`pipe_run`]).19838 /// One plane's pipelined loop restoration state (see [`pipe_run`]).
19810 struct LrPlane {19839 struct LrPlane {
19811 p: usize,19840 p: usize,
@@ -19828,6 +19857,8 @@ struct PipeGuard<'a> {
19828 sync: std::sync::Arc<PipeSync>,19857 sync: std::sync::Arc<PipeSync>,
19829 batch: Option<crate::par::Batch<'a>>,19858 batch: Option<crate::par::Batch<'a>>,
19830 nbands: usize,19859 nbands: usize,
19860+ /// The loop restoration worker's own gate, aborted with the first one.
19861+ lr_sync: Option<std::sync::Arc<PipeSync>>,
19831 }19862 }
19832 19863
19833 impl PipeGuard<'_> {19864 impl PipeGuard<'_> {
@@ -19844,6 +19875,9 @@ impl PipeGuard<'_> {
19844 impl Drop for PipeGuard<'_> {19875 impl Drop for PipeGuard<'_> {
19845 fn drop(&mut self) {19876 fn drop(&mut self) {
19846 self.sync.close(self.nbands, true);19877 self.sync.close(self.nbands, true);
19878+ if let Some(ls) = &self.lr_sync {
19879+ ls.close(self.nbands, true);
19880+ }
19847 }19881 }
19848 }19882 }
19849 19883
@@ -32207,15 +32241,31 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs(
32207 nbands,32241 nbands,
32208 cdef_on,32242 cdef_on,
32209 snap_on,32243 snap_on,
32210- lr,
32211- lr_grid: std::ptr::from_mut(&mut lr_grid),
32212- lr_on: lr_active,
32213 };32244 };
32214 let ctx = crate::decode::filter_ctx_copy(fctx);32245 let ctx = crate::decode::filter_ctx_copy(fctx);
32215 let sync2 = std::sync::Arc::clone(&sync);32246 let sync2 = std::sync::Arc::clone(&sync);
32247+ // lane-pipefilt2: loop restoration is a SECOND worker, chained one
32248+ // CDEF band behind the first (see `pipe_run_lr`).
32249+ let lr_sync = lr_active.then(|| std::sync::Arc::new(PipeSync::new()));
32250+ let lr_sync2 = lr_sync.clone();
32251+ #[allow(unsafe_code)]
32252+ let ssnap = unsafe { crate::par::Shared::from_ptr(std::ptr::from_mut(&mut pipe_snap)) };
32216 let snap = &mut pipe_snap;32253 let snap = &mut pipe_snap;
32217- batch.submit(move || pipe_run(&sync2, sy, su, sv, sn, cfg, snap, &ctx));32254+ batch.submit(move || {
32218- PipeGuard { sync, batch: Some(batch), nbands }32255+ pipe_run(&sync2, sy, su, sv, sn, cfg, snap, lr_sync2.as_deref(), &ctx)
32256+ });
32257+ if let Some(ls) = lr_sync.clone() {
32258+ let lcfg = LrCfg {
32259+ lr,
32260+ lr_grid: std::ptr::from_mut(&mut lr_grid),
32261+ frame_w: frame_width as usize,
32262+ frame_h: frame_height as usize,
32263+ nbands,
32264+ };
32265+ let ctx2 = crate::decode::filter_ctx_copy(fctx);
32266+ batch.submit(move || pipe_run_lr(&ls, sy, su, sv, ssnap, lcfg, &ctx2));
32267+ }
32268+ PipeGuard { sync, batch: Some(batch), nbands, lr_sync }
32219 });32269 });
32220 if !pipe_on && wave_stats() {32270 if !pipe_on && wave_stats() {
32221 PIPE_WHOLE.fetch_add(1, Relaxed);32271 PIPE_WHOLE.fetch_add(1, Relaxed);