From 95de47fc4b4ef45aea807025014d3bb28c48fa2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Sun, 6 Sep 2026 10:30:48 +0300 Subject: [PATCH] perf(av1): the filter replay deblocks once per level pair The search fixes the deblocking levels in its first three stages and then walks 18 CDEF strength candidates that all replay the same deblock. The replay now keeps the deblocked planes with the levels that produced them and copies those instead when the levels match -- the same copy the replay was already making, with the deblock skipped. Byte-identical on the three gate clips; ladder wall 4.64/4.21/4.91 -> 4.51/4.08/4.80 s. --- crates/ec-av1/src/decode.rs | 40 +++++++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index 7b33b083..08ecd8fc 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -19725,6 +19725,11 @@ pub(crate) struct FilterReplay { frame_height: usize, width: usize, height: usize, + /// The last deblocked planes and the levels that produced them. + /// Deblocking depends on nothing else the search moves, and the search + /// fixes the levels before it walks the CDEF strengths, so every + /// candidate after the first of a level pair replays CDEF alone. + deblocked: Option<(LoopFilterParams, PlaneBuf<'static>, PlaneBuf<'static>, PlaneBuf<'static>)>, } thread_local! { @@ -19772,6 +19777,7 @@ fn capture_filter_replay( frame_height, width, height, + deblocked: None, }); }); } @@ -19785,23 +19791,31 @@ pub(crate) fn replay_filters( cdef: &CdefParams, fctx: &FrameCtx, ) -> Option { - REPLAY.with_borrow(|slot| { - let r = slot.as_ref()?; + REPLAY.with_borrow_mut(|slot| { + let r = slot.as_mut()?; let copy = |p: &PlaneBuf<'_>| PlaneBuf { data: std::borrow::Cow::Owned(p.data.to_vec()), ..*p }; - let (mut y, mut u, mut v) = (copy(&r.y), copy(&r.u), copy(&r.v)); - apply_deblock( - &mut y, - &mut u, - &mut v, - loop_filter, - &r.neighbours, - r.frame_width, - r.frame_height, - fctx, - ); + let hit = matches!(&r.deblocked, Some((lf, ..)) if lf == loop_filter); + let (mut y, mut u, mut v) = if hit { + let (_, dy, du, dv) = r.deblocked.as_ref().expect("just matched"); + (copy(dy), copy(du), copy(dv)) + } else { + let (mut y, mut u, mut v) = (copy(&r.y), copy(&r.u), copy(&r.v)); + apply_deblock( + &mut y, + &mut u, + &mut v, + loop_filter, + &r.neighbours, + r.frame_width, + r.frame_height, + fctx, + ); + r.deblocked = Some((*loop_filter, copy(&y), copy(&u), copy(&v))); + (y, u, v) + }; apply_cdef(&mut y, &mut u, &mut v, cdef, &r.neighbours, (r.frame_width, r.frame_height), fctx); // The capture only happens on the shape the decoder's own tail // returns whole (no superres, no loop restoration, no crop), so this