forge
Explore
Log in

Don't you dare go Hollow

The filter replay's CDEF reads the deblock cache instead of copying

Browse files at this commit

1085bf233cb1ec4ae7037c43a7ffdccc8c20e1afAhmet Kaan GümüşParent: 2d19659
  • perf
    • the filter replay's CDEF reads the deblock cache instead of copying
Show full message
perf(av1): the filter replay's CDEF reads the deblock cache instead of copying

Every CDEF unit reads the pre-CDEF planes, so apply_cdef opened by copying
all three (~19 MB at 4K) into a scratch. The replay already holds an
untouched copy of exactly those planes -- the post-deblock cache the working
planes were copied FROM -- so it hands them in and the copy does not happen.
4K/8 threads: cdef replay 162 -> 124 ms.
crates/ec-av1/src/decode.rsmodified+36 -13
@@ -19766,12 +19766,12 @@ pub(crate) fn set_filter_replay_disabled(off: bool) {
19766 REPLAY_DISABLED.store(off, std::sync::atomic::Ordering::Relaxed);19766 REPLAY_DISABLED.store(off, std::sync::atomic::Ordering::Relaxed);
19767 }19767 }
19768 19768
19769-/// Test-only: run the capture decode [`replay_final`] replaced as well, and19769+// Test-only: run the capture decode `replay_final` replaced as well, and
19770-/// hold its picture and its [`FilterStages`] against the replay's19770+// hold its picture and its `FilterStages` against the replay's
19771-/// (`encoder::tests::filter_replay_final_matches_the_capture_decode`).19771+// (`encoder::tests::filter_replay_final_matches_the_capture_decode`).
19772-/// Thread-local, not a global: the filter stage runs on the frame thread the19772+// Thread-local, not a global: the filter stage runs on the frame thread the
19773-/// test itself drives, and a process-wide flag would put every test encoding19773+// test itself drives, and a process-wide flag would put every test encoding
19774-/// in parallel with this one through the extra decode too.19774+// in parallel with this one through the extra decode too.
19775 #[cfg(test)]19775 #[cfg(test)]
19776 thread_local! {19776 thread_local! {
19777 static VERIFY_FINAL: std::cell::Cell<bool> = const { std::cell::Cell::new(false) };19777 static VERIFY_FINAL: std::cell::Cell<bool> = const { std::cell::Cell::new(false) };
@@ -19949,7 +19949,13 @@ fn replay_inner(
19949 fctx.cdef_idx_grid.with(|g| *g.borrow_mut() = grid.to_vec());19949 fctx.cdef_idx_grid.with(|g| *g.borrow_mut() = grid.to_vec());
19950 }19950 }
19951 let ct = crate::par::timer(crate::par::S_CDEF);19951 let ct = crate::par::timer(crate::par::S_CDEF);
19952- apply_cdef(&mut y, &mut u, &mut v, cdef, &r.neighbours, (r.frame_width, r.frame_height), fctx);19952+ let pre = r.deblocked.as_ref().map(|(_, dy, du, dv)| {
19953+ (dy.data.as_ref(), du.data.as_ref(), dv.data.as_ref())
19954+ });
19955+ apply_cdef(
19956+ &mut y, &mut u, &mut v, cdef, &r.neighbours, (r.frame_width, r.frame_height), fctx,
19957+ pre,
19958+ );
19953 drop(ct);19959 drop(ct);
19954 let stages = final_pass.and_then(|(_, want)| {19960 let stages = final_pass.and_then(|(_, want)| {
19955 want.then(|| FilterStages {19961 want.then(|| FilterStages {
@@ -20732,6 +20738,13 @@ fn apply_cdef(
20732 // This frame header's own CROPPED luma size, for the straddling-unit20738 // This frame header's own CROPPED luma size, for the straddling-unit
20733 // counter only -- CDEF itself walks the mi-rounded plane.20739 // counter only -- CDEF itself walks the mi-rounded plane.
20734 (frame_w, frame_h): (usize, usize), fctx: &crate::decode::FrameCtx,20740 (frame_w, frame_h): (usize, usize), fctx: &crate::decode::FrameCtx,
20741+ // lane-av1cap: every CDEF unit reads the PRE-CDEF planes, so the filter
20742+ // starts by copying all three (~19 MB at 4K). The encoder's filter
20743+ // replay already holds an untouched copy of exactly those planes -- its
20744+ // post-deblock cache, which is where the working planes were copied FROM
20745+ // -- so it hands them straight in and the copy does not happen. `None`
20746+ // (every decode) copies as before.
20747+ src: Option<(&[u16], &[u16], &[u16])>,
20735 ) {20748 ) {
20736 // lane-part32 r2 debug rung, env-gated: see `apply_deblock`'s sibling.20749 // lane-part32 r2 debug rung, env-gated: see `apply_deblock`'s sibling.
20737 if crate::envflags::env_flag!("EC_AV1_DEBUG_SKIP_CDEF") {20750 if crate::envflags::env_flag!("EC_AV1_DEBUG_SKIP_CDEF") {
@@ -20751,9 +20764,15 @@ fn apply_cdef(
20751 // One entry per superblock (a few hundred at 4K), so this clone is not20764 // One entry per superblock (a few hundred at 4K), so this clone is not
20752 // one of the per-frame copies lane-perf6 pooled below.20765 // one of the per-frame copies lane-perf6 pooled below.
20753 let idx_grid = fctx.cdef_idx_grid.with(|g| g.borrow().clone());20766 let idx_grid = fctx.cdef_idx_grid.with(|g| g.borrow().clone());
20754- let src_y = scratch_from(&y.data);20767+ let owned = src.is_none();
20755- let src_u = scratch_from(&u.data);20768+ let (src_y, src_u, src_v) = match src {
20756- let src_v = scratch_from(&v.data);20769+ Some((a, b, c)) => (std::borrow::Cow::Borrowed(a), std::borrow::Cow::Borrowed(b), std::borrow::Cow::Borrowed(c)),
20770+ None => (
20771+ std::borrow::Cow::Owned(scratch_from(&y.data)),
20772+ std::borrow::Cow::Owned(scratch_from(&u.data)),
20773+ std::borrow::Cow::Owned(scratch_from(&v.data)),
20774+ ),
20775+ };
20757 // libaom `av1_cdef_filter_fb`: `pri_strength = level << coeff_shift`,20776 // libaom `av1_cdef_filter_fb`: `pri_strength = level << coeff_shift`,
20758 // `sec_strength <<= coeff_shift`, `damping += coeff_shift - (pli != 0)`.20777 // `sec_strength <<= coeff_shift`, `damping += coeff_shift - (pli != 0)`.
20759 let coeff_shift = i32::from(bit_depth(fctx)) - 8;20778 let coeff_shift = i32::from(bit_depth(fctx)) - 8;
@@ -20768,9 +20787,11 @@ fn apply_cdef(
20768 y, u, v, &src_y, &src_u, &src_v, cdef, skip_grid, &idx_grid, sb_cols, coeff_shift,20787 y, u, v, &src_y, &src_u, &src_v, cdef, skip_grid, &idx_grid, sb_cols, coeff_shift,
20769 damping, damping_uv, (frame_w, frame_h), 0, skip_grid.mi_rows, fctx,20788 damping, damping_uv, (frame_w, frame_h), 0, skip_grid.mi_rows, fctx,
20770 );20789 );
20771- scratch_return(src_y);20790+ if owned {
20772- scratch_return(src_u);20791+ scratch_return(src_y.into_owned());
20773- scratch_return(src_v);20792+ scratch_return(src_u.into_owned());
20793+ scratch_return(src_v.into_owned());
20794+ }
20774 }20795 }
20775 20796
20776 /// [`FrameCtx::capture_stages`]'s write side, called between CDEF and loop20797 /// [`FrameCtx::capture_stages`]'s write side, called between CDEF and loop
@@ -23346,6 +23367,7 @@ pub(crate) fn decode_key_frame_tile_with_cdfs(
23346 cdef,23367 cdef,
23347 &neighbours,23368 &neighbours,
23348 (frame_width as usize, frame_height as usize), fctx,23369 (frame_width as usize, frame_height as usize), fctx,
23370+ None,
23349 );23371 );
23350 dump_stage("EC_AV1_POSTCDEF_DUMP", &y, &u, &v);23372 dump_stage("EC_AV1_POSTCDEF_DUMP", &y, &u, &v);
23351 dump_stage16("EC_AV1_POSTCDEF_DUMP16", &y, &u, &v, frame_width as usize, frame_height as usize);23373 dump_stage16("EC_AV1_POSTCDEF_DUMP16", &y, &u, &v, frame_width as usize, frame_height as usize);
@@ -36161,6 +36183,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs(
36161 cdef,36183 cdef,
36162 &neighbours,36184 &neighbours,
36163 (frame_width as usize, frame_height as usize), fctx,36185 (frame_width as usize, frame_height as usize), fctx,
36186+ None,
36164 );36187 );
36165 }36188 }
36166 dump_stage("EC_AV1_POSTCDEF_DUMP", &y, &u, &v);36189 dump_stage("EC_AV1_POSTCDEF_DUMP", &y, &u, &v);