Loop restoration joins the per-superblock-row filter pipeline
Browse files at this commit013daf3277171916bb35874bbbe6c8a06dd9a311Parent: 551e13e- perf
- loop restoration joins the per-superblock-row filter pipeline
2 changed files +201 -23
- crates/ec-av1/src/decode.rs +125 -3
- crates/ec-av1/src/restoration.rs +76 -20
crates/ec-av1/src/decode.rsmodified+125 -3
| @@ -19645,6 +19645,14 @@ 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, | ||
| 19648 | } | 19656 | } |
| 19649 | 19657 | ||
| 19650 | // SAFETY: the `*const u8` is a read-only view of a Vec that is allocated once | 19658 | // SAFETY: the `*const u8` is a read-only view of a Vec that is allocated once |
| @@ -19676,6 +19684,43 @@ fn pipe_run( | |||
| 19676 | let damping_uv = (damping - 1).max(0); | 19684 | let damping_uv = (damping - 1).max(0); |
| 19677 | let (fw, fh) = (cfg.frame_w, cfg.frame_h); | 19685 | let (fw, fh) = (cfg.frame_w, cfg.frame_h); |
| 19678 | let nb = cfg.nbands; | 19686 | 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 | + } | ||
| 19679 | for k in 0..nb + 3 { | 19724 | for k in 0..nb + 3 { |
| 19680 | if !sync.wait_for((k + 2).min(nb)) { | 19725 | if !sync.wait_for((k + 2).min(nb)) { |
| 19681 | return; | 19726 | return; |
| @@ -19703,7 +19748,76 @@ fn pipe_run( | |||
| 19703 | ); | 19748 | ); |
| 19704 | } | 19749 | } |
| 19705 | } | 19750 | } |
| 19751 | + if !lr_planes.is_empty() && k >= 3 && k - 3 < nb { | ||
| 19752 | + let band = k - 3; | ||
| 19753 | + // SAFETY: sized once per frame by `RestorationGrid::new`. | ||
| 19754 | + let grid = unsafe { &*cfg.lr_grid }; | ||
| 19755 | + let Some((sy2, su2, sv2)) = snap.as_ref() else { continue }; | ||
| 19756 | + for lp in &mut lr_planes { | ||
| 19757 | + let stripe = 64usize >> lp.ss; | ||
| 19758 | + let r_lo = lp.next; | ||
| 19759 | + while lp.next < lp.rows.len() | ||
| 19760 | + && (lp.rows[lp.next].1 as usize - 1) / stripe <= band | ||
| 19761 | + { | ||
| 19762 | + lp.next += 1; | ||
| 19763 | + } | ||
| 19764 | + if lp.next == r_lo { | ||
| 19765 | + continue; | ||
| 19766 | + } | ||
| 19767 | + let (src, deb) = match lp.p { | ||
| 19768 | + 0 => (&y.data, &sy2.data), | ||
| 19769 | + 1 => (&u.data, &su2.data), | ||
| 19770 | + _ => (&v.data, &sv2.data), | ||
| 19771 | + }; | ||
| 19772 | + crate::restoration::apply_loop_restoration_plane( | ||
| 19773 | + src, | ||
| 19774 | + deb, | ||
| 19775 | + lp.stride, | ||
| 19776 | + lp.pw, | ||
| 19777 | + lp.ph, | ||
| 19778 | + lp.ss, | ||
| 19779 | + cfg.lr.frame_restoration_type[lp.p], | ||
| 19780 | + cfg.lr.loop_restoration_size[lp.p], | ||
| 19781 | + grid, | ||
| 19782 | + lp.p, | ||
| 19783 | + &mut lp.out, | ||
| 19784 | + Some((r_lo, lp.next)), | ||
| 19785 | + fctx, | ||
| 19786 | + ); | ||
| 19787 | + } | ||
| 19788 | + } | ||
| 19789 | + } | ||
| 19790 | + // The RU rows partition `0..plane_h`; the mi-rounded rows below it are | ||
| 19791 | + // untouched by restoration and copied here, then the restored plane | ||
| 19792 | + // takes the picture's place (the whole-frame path's own final swap). | ||
| 19793 | + for mut lp in lr_planes { | ||
| 19794 | + let buf: &mut PlaneBuf<'static> = match lp.p { | ||
| 19795 | + 0 => &mut *y, | ||
| 19796 | + 1 => &mut *u, | ||
| 19797 | + _ => &mut *v, | ||
| 19798 | + }; | ||
| 19799 | + let tail = lp.ph * lp.stride; | ||
| 19800 | + if tail < buf.data.len() { | ||
| 19801 | + lp.out[tail..].copy_from_slice(&buf.data[tail..]); | ||
| 19802 | + } | ||
| 19803 | + scratch_return( | ||
| 19804 | + std::mem::replace(&mut buf.data, std::borrow::Cow::Owned(lp.out)).into_owned(), | ||
| 19805 | + ); | ||
| 19806 | + } | ||
| 19706 | } | 19807 | } |
| 19808 | + | ||
| 19809 | +/// One plane's pipelined loop restoration state (see [`pipe_run`]). | ||
| 19810 | +struct LrPlane { | ||
| 19811 | + p: usize, | ||
| 19812 | + /// The restored plane, built band by band and swapped in at the end. | ||
| 19813 | + out: Vec<u16>, | ||
| 19814 | + rows: Vec<(u32, u32)>, | ||
| 19815 | + /// The first RU row not yet restored. | ||
| 19816 | + next: usize, | ||
| 19817 | + pw: usize, | ||
| 19818 | + ph: usize, | ||
| 19819 | + ss: u32, | ||
| 19820 | + stride: usize, | ||
| 19707 | } | 19821 | } |
| 19708 | 19822 | ||
| 19709 | /// The pipeline's handle on the frame thread: [`PipeGuard::note`] releases | 19823 | /// The pipeline's handle on the frame thread: [`PipeGuard::note`] releases |
| @@ -20002,7 +20116,7 @@ fn apply_loop_restoration( | |||
| 20002 | lr.loop_restoration_size[0], | 20116 | lr.loop_restoration_size[0], |
| 20003 | grid, | 20117 | grid, |
| 20004 | 0, | 20118 | 0, |
| 20005 | - &mut out, fctx, | 20119 | + &mut out, None, fctx, |
| 20006 | ); | 20120 | ); |
| 20007 | scratch_return(std::mem::replace(&mut y.data, std::borrow::Cow::Owned(out)).into_owned()); | 20121 | scratch_return(std::mem::replace(&mut y.data, std::borrow::Cow::Owned(out)).into_owned()); |
| 20008 | let mut out = scratch_empty(); | 20122 | let mut out = scratch_empty(); |
| @@ -20017,7 +20131,7 @@ fn apply_loop_restoration( | |||
| 20017 | lr.loop_restoration_size[1], | 20131 | lr.loop_restoration_size[1], |
| 20018 | grid, | 20132 | grid, |
| 20019 | 1, | 20133 | 1, |
| 20020 | - &mut out, fctx, | 20134 | + &mut out, None, fctx, |
| 20021 | ); | 20135 | ); |
| 20022 | scratch_return(std::mem::replace(&mut u.data, std::borrow::Cow::Owned(out)).into_owned()); | 20136 | scratch_return(std::mem::replace(&mut u.data, std::borrow::Cow::Owned(out)).into_owned()); |
| 20023 | let mut out = scratch_empty(); | 20137 | let mut out = scratch_empty(); |
| @@ -20032,7 +20146,7 @@ fn apply_loop_restoration( | |||
| 20032 | lr.loop_restoration_size[2], | 20146 | lr.loop_restoration_size[2], |
| 20033 | grid, | 20147 | grid, |
| 20034 | 2, | 20148 | 2, |
| 20035 | - &mut out, fctx, | 20149 | + &mut out, None, fctx, |
| 20036 | ); | 20150 | ); |
| 20037 | scratch_return(std::mem::replace(&mut v.data, std::borrow::Cow::Owned(out)).into_owned()); | 20151 | scratch_return(std::mem::replace(&mut v.data, std::borrow::Cow::Owned(out)).into_owned()); |
| 20038 | } | 20152 | } |
| @@ -32093,6 +32207,9 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( | |||
| 32093 | nbands, | 32207 | nbands, |
| 32094 | cdef_on, | 32208 | cdef_on, |
| 32095 | snap_on, | 32209 | snap_on, |
| 32210 | + lr, | ||
| 32211 | + lr_grid: std::ptr::from_mut(&mut lr_grid), | ||
| 32212 | + lr_on: lr_active, | ||
| 32096 | }; | 32213 | }; |
| 32097 | let ctx = crate::decode::filter_ctx_copy(fctx); | 32214 | let ctx = crate::decode::filter_ctx_copy(fctx); |
| 32098 | let sync2 = std::sync::Arc::clone(&sync); | 32215 | let sync2 = std::sync::Arc::clone(&sync); |
| @@ -34716,11 +34833,16 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( | |||
| 34716 | crate::superres::note_upscaled_picture(); | 34833 | crate::superres::note_upscaled_picture(); |
| 34717 | } | 34834 | } |
| 34718 | if let Some((deblocked_y, deblocked_u, deblocked_v)) = &deblocked { | 34835 | if let Some((deblocked_y, deblocked_u, deblocked_v)) = &deblocked { |
| 34836 | + // lane-pipefilt2: the pipeline already restored every RU row on its | ||
| 34837 | + // way past it (its LR step runs in the same iteration as the CDEF | ||
| 34838 | + // band that completes the row's last line). | ||
| 34839 | + if !pipelined { | ||
| 34719 | apply_loop_restoration( | 34840 | apply_loop_restoration( |
| 34720 | &mut y, &mut u, &mut v, deblocked_y, deblocked_u, deblocked_v, lr, &lr_grid, | 34841 | &mut y, &mut u, &mut v, deblocked_y, deblocked_u, deblocked_v, lr, &lr_grid, |
| 34721 | (lr_w, frame_height as usize), fctx, | 34842 | (lr_w, frame_height as usize), fctx, |
| 34722 | ); | 34843 | ); |
| 34723 | } | 34844 | } |
| 34845 | + } | ||
| 34724 | if let Some((db_y, db_u, db_v)) = deblocked { | 34846 | if let Some((db_y, db_u, db_v)) = deblocked { |
| 34725 | // LIFO pool: return in reverse so the next frame's `y`/`u`/`v` | 34847 | // LIFO pool: return in reverse so the next frame's `y`/`u`/`v` |
| 34726 | // snapshots pop buffers of their own size and never resize. | 34848 | // snapshots pop buffers of their own size and never resize. |
crates/ec-av1/src/restoration.rsmodified+76 -20
| @@ -1237,21 +1237,67 @@ pub(crate) fn apply_loop_restoration_plane( | |||
| 1237 | // lane-perf6: the destination buffer, refilled from `cdef` here. It used | 1237 | // lane-perf6: the destination buffer, refilled from `cdef` here. It used |
| 1238 | // to be a fresh `cdef.to_vec()` per plane per frame -- a multi-megabyte | 1238 | // to be a fresh `cdef.to_vec()` per plane per frame -- a multi-megabyte |
| 1239 | // allocation whose every page then faulted in on first touch. | 1239 | // allocation whose every page then faulted in on first touch. |
| 1240 | - out: &mut Vec<u16>, fctx: &crate::decode::FrameCtx, | 1240 | + out: &mut Vec<u16>, |
| 1241 | + // lane-pipefilt2: `Some((r_lo, r_hi))` restricts the call to those RU | ||
| 1242 | + // ROWS, for the pipeline, which runs loop restoration band by band | ||
| 1243 | + // behind CDEF. `out` is then the caller's, already sized, and only this | ||
| 1244 | + // range's rows are refilled from `cdef` (the rows below `plane_h` are | ||
| 1245 | + // the caller's own tail copy). `None` is the whole-plane form. | ||
| 1246 | + rrows: Option<(usize, usize)>, | ||
| 1247 | + fctx: &crate::decode::FrameCtx, | ||
| 1241 | ) { | 1248 | ) { |
| 1249 | + let rows = lr_unit_rows(plane_h, unit_size, ss_y, ftype); | ||
| 1250 | + let Some((r_lo, r_hi)) = rrows else { | ||
| 1242 | out.clear(); | 1251 | out.clear(); |
| 1243 | out.extend_from_slice(cdef); | 1252 | out.extend_from_slice(cdef); |
| 1244 | - if ftype == RestorationType::None || unit_size == 0 { | 1253 | + if rows.is_empty() { |
| 1254 | + return; | ||
| 1255 | + } | ||
| 1256 | + let so = crate::par::Shared::new(out); | ||
| 1257 | + let rbands = crate::par::bands(rows.len(), crate::par::filter_threads()); | ||
| 1258 | + crate::par::run_bands(&rbands, fctx, |r_lo, r_hi, fctx| { | ||
| 1259 | + // SAFETY: this band writes only the `out` rows its own RU rows cover. | ||
| 1260 | + #[allow(unsafe_code)] | ||
| 1261 | + let out = unsafe { so.get() }; | ||
| 1262 | + for rrow in r_lo..r_hi { | ||
| 1263 | + lr_unit_row( | ||
| 1264 | + out, cdef, deblocked, stride, plane_w, plane_h, ss_y, unit_size, grid, | ||
| 1265 | + plane, rrow, &rows, fctx, | ||
| 1266 | + ); | ||
| 1267 | + } | ||
| 1268 | + }); | ||
| 1269 | + debug_assert_eq!(rows.len(), grid.vert_units[plane], "RU row walk must match RestorationGrid::new's count_units"); | ||
| 1245 | return; | 1270 | return; |
| 1271 | + }; | ||
| 1272 | + for rrow in r_lo..r_hi.min(rows.len()) { | ||
| 1273 | + let (v_start, v_end) = rows[rrow]; | ||
| 1274 | + let (a, b) = (v_start as usize * stride, v_end as usize * stride); | ||
| 1275 | + out[a..b].copy_from_slice(&cdef[a..b]); | ||
| 1276 | + lr_unit_row( | ||
| 1277 | + out, cdef, deblocked, stride, plane_w, plane_h, ss_y, unit_size, grid, plane, rrow, | ||
| 1278 | + &rows, fctx, | ||
| 1279 | + ); | ||
| 1280 | + } | ||
| 1281 | +} | ||
| 1282 | + | ||
| 1283 | +/// One plane's restoration-unit ROW walk, resolved to each row's `out` write | ||
| 1284 | +/// span `v_start..v_end`. The walk is data-dependent (a short remainder is | ||
| 1285 | +/// swallowed by the previous unit row), so both callers resolve it into this | ||
| 1286 | +/// list first: the next row's `v_start` is this one's `v_end`, so the spans | ||
| 1287 | +/// partition `0..plane_h` and row bands are disjoint. Empty when the plane | ||
| 1288 | +/// restores nothing. | ||
| 1289 | +pub(crate) fn lr_unit_rows( | ||
| 1290 | + plane_h: usize, | ||
| 1291 | + unit_size: u32, | ||
| 1292 | + ss_y: u32, | ||
| 1293 | + ftype: RestorationType, | ||
| 1294 | +) -> Vec<(u32, u32)> { | ||
| 1295 | + let mut rows: Vec<(u32, u32)> = Vec::new(); | ||
| 1296 | + if ftype == RestorationType::None || unit_size == 0 { | ||
| 1297 | + return rows; | ||
| 1246 | } | 1298 | } |
| 1247 | let voffset = 8u32 >> ss_y; | 1299 | let voffset = 8u32 >> ss_y; |
| 1248 | let ext_size = (unit_size * 3) / 2; | 1300 | let ext_size = (unit_size * 3) / 2; |
| 1249 | - // lane-filt1: the RU row walk is data-dependent (a short remainder is | ||
| 1250 | - // swallowed by the previous unit row), so resolve it into a row list | ||
| 1251 | - // first. Each row writes exactly `v_start..v_end` of `out` and the next | ||
| 1252 | - // row's `v_start` is this one's `v_end`, so row bands are disjoint; the | ||
| 1253 | - // reads are all from `cdef`/`deblocked`, immutable snapshots. | ||
| 1254 | - let mut rows: Vec<(u32, u32)> = Vec::new(); | ||
| 1255 | let mut y0 = 0u32; | 1301 | let mut y0 = 0u32; |
| 1256 | while y0 < plane_h as u32 { | 1302 | while y0 < plane_h as u32 { |
| 1257 | let remaining_h = plane_h as u32 - y0; | 1303 | let remaining_h = plane_h as u32 - y0; |
| @@ -1264,14 +1310,28 @@ pub(crate) fn apply_loop_restoration_plane( | |||
| 1264 | rows.push((v_start, v_end)); | 1310 | rows.push((v_start, v_end)); |
| 1265 | y0 += uh; | 1311 | y0 += uh; |
| 1266 | } | 1312 | } |
| 1267 | - let rrow_count = rows.len(); | 1313 | + rows |
| 1268 | - let so = crate::par::Shared::new(out); | 1314 | +} |
| 1269 | - let rbands = crate::par::bands(rrow_count, crate::par::filter_threads()); | 1315 | + |
| 1270 | - crate::par::run_bands(&rbands, fctx, |r_lo, r_hi, fctx| { | 1316 | +/// The restoration units of one RU row, filtered into `out`. Reads only |
| 1271 | - // SAFETY: this band writes only the `out` rows its own RU rows cover. | 1317 | +/// `cdef`/`deblocked`, writes only this row's `v_start..v_end` span. |
| 1272 | - #[allow(unsafe_code)] | 1318 | +#[allow(clippy::too_many_arguments)] |
| 1273 | - let out = unsafe { so.get() }; | 1319 | +fn lr_unit_row( |
| 1274 | - for rrow in r_lo..r_hi { | 1320 | + out: &mut [u16], |
| 1321 | + cdef: &[u16], | ||
| 1322 | + deblocked: &[u16], | ||
| 1323 | + stride: usize, | ||
| 1324 | + plane_w: usize, | ||
| 1325 | + plane_h: usize, | ||
| 1326 | + ss_y: u32, | ||
| 1327 | + unit_size: u32, | ||
| 1328 | + grid: &RestorationGrid, | ||
| 1329 | + plane: usize, | ||
| 1330 | + rrow: usize, | ||
| 1331 | + rows: &[(u32, u32)], | ||
| 1332 | + fctx: &crate::decode::FrameCtx, | ||
| 1333 | +) { | ||
| 1334 | + let ext_size = (unit_size * 3) / 2; | ||
| 1275 | let (v_start, v_end) = rows[rrow]; | 1335 | let (v_start, v_end) = rows[rrow]; |
| 1276 | let mut x0 = 0u32; | 1336 | let mut x0 = 0u32; |
| 1277 | let mut rcol = 0usize; | 1337 | let mut rcol = 0usize; |
| @@ -1299,10 +1359,6 @@ pub(crate) fn apply_loop_restoration_plane( | |||
| 1299 | rcol += 1; | 1359 | rcol += 1; |
| 1300 | } | 1360 | } |
| 1301 | } | 1361 | } |
| 1302 | - }); | ||
| 1303 | - let rrow = rrow_count; | ||
| 1304 | - debug_assert_eq!(rrow, grid.vert_units[plane], "RU row walk must match RestorationGrid::new's count_units"); | ||
| 1305 | -} | ||
| 1306 | 1362 | ||
| 1307 | #[cfg(test)] | 1363 | #[cfg(test)] |
| 1308 | mod tests { | 1364 | mod tests { |