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) {
1976619766 REPLAY_DISABLED.store(off, std::sync::atomic::Ordering::Relaxed);
1976719767 }
1976819768
19769-/// Test-only: run the capture decode [`replay_final`] replaced as well, and
19770-/// hold its picture and its [`FilterStages`] against the replay's
19771-/// (`encoder::tests::filter_replay_final_matches_the_capture_decode`).
19772-/// 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 encoding
19774-/// in parallel with this one through the extra decode too.
19769+// Test-only: run the capture decode `replay_final` replaced as well, and
19770+// hold its picture and its `FilterStages` against the replay's
19771+// (`encoder::tests::filter_replay_final_matches_the_capture_decode`).
19772+// 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 encoding
19774+// in parallel with this one through the extra decode too.
1977519775 #[cfg(test)]
1977619776 thread_local! {
1977719777 static VERIFY_FINAL: std::cell::Cell<bool> = const { std::cell::Cell::new(false) };
@@ -19949,7 +19949,13 @@ fn replay_inner(
1994919949 fctx.cdef_idx_grid.with(|g| *g.borrow_mut() = grid.to_vec());
1995019950 }
1995119951 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+ );
1995319959 drop(ct);
1995419960 let stages = final_pass.and_then(|(_, want)| {
1995519961 want.then(|| FilterStages {
@@ -20732,6 +20738,13 @@ fn apply_cdef(
2073220738 // This frame header's own CROPPED luma size, for the straddling-unit
2073320739 // counter only -- CDEF itself walks the mi-rounded plane.
2073420740 (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])>,
2073520748 ) {
2073620749 // lane-part32 r2 debug rung, env-gated: see `apply_deblock`'s sibling.
2073720750 if crate::envflags::env_flag!("EC_AV1_DEBUG_SKIP_CDEF") {
@@ -20751,9 +20764,15 @@ fn apply_cdef(
2075120764 // One entry per superblock (a few hundred at 4K), so this clone is not
2075220765 // one of the per-frame copies lane-perf6 pooled below.
2075320766 let idx_grid = fctx.cdef_idx_grid.with(|g| g.borrow().clone());
20754- let src_y = scratch_from(&y.data);
20755- let src_u = scratch_from(&u.data);
20756- let src_v = scratch_from(&v.data);
20767+ let owned = src.is_none();
20768+ let (src_y, src_u, src_v) = match src {
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+ };
2075720776 // libaom `av1_cdef_filter_fb`: `pri_strength = level << coeff_shift`,
2075820777 // `sec_strength <<= coeff_shift`, `damping += coeff_shift - (pli != 0)`.
2075920778 let coeff_shift = i32::from(bit_depth(fctx)) - 8;
@@ -20768,9 +20787,11 @@ fn apply_cdef(
2076820787 y, u, v, &src_y, &src_u, &src_v, cdef, skip_grid, &idx_grid, sb_cols, coeff_shift,
2076920788 damping, damping_uv, (frame_w, frame_h), 0, skip_grid.mi_rows, fctx,
2077020789 );
20771- scratch_return(src_y);
20772- scratch_return(src_u);
20773- scratch_return(src_v);
20790+ if owned {
20791+ scratch_return(src_y.into_owned());
20792+ scratch_return(src_u.into_owned());
20793+ scratch_return(src_v.into_owned());
20794+ }
2077420795 }
2077520796
2077620797 /// [`FrameCtx::capture_stages`]'s write side, called between CDEF and loop
@@ -23346,6 +23367,7 @@ pub(crate) fn decode_key_frame_tile_with_cdfs(
2334623367 cdef,
2334723368 &neighbours,
2334823369 (frame_width as usize, frame_height as usize), fctx,
23370+ None,
2334923371 );
2335023372 dump_stage("EC_AV1_POSTCDEF_DUMP", &y, &u, &v);
2335123373 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(
3616136183 cdef,
3616236184 &neighbours,
3616336185 (frame_width as usize, frame_height as usize), fctx,
36186+ None,
3616436187 );
3616536188 }
3616636189 dump_stage("EC_AV1_POSTCDEF_DUMP", &y, &u, &v);