forge
Explore
Log in

Don't you dare go Hollow

The filter search decodes its tile once instead of once per candidate

Browse files at this commit

675eebbf2a223b66ea8314f60b09ec5f5a65de29Ahmet Kaan GümüşParent: 7947a2d
  • perf
    • the filter search decodes its tile once instead of once per candidate
Show full message
perf(av1): the filter search decodes its tile once instead of once per candidate

`filter_search::pick_filters` scores ~25 deblock/CDEF candidates by handing
the coded tile back to the decoder under each one. The entropy decode and
reconstruction it repeated are identical every time -- both filters run
after them -- and that re-parse was 24% of the encoder's profile.

The decoder now captures its pre-filter frame plus the neighbour state the
filters read (`decode::FilterReplay`, armed for exactly one decode), and
`replay_filters` re-runs `apply_deblock` + `apply_cdef` on a copy of it.
Capture only happens on the shape whose decode tail is those two calls and
nothing else (no superres, no loop restoration, no crop); anything else
decodes for real as before.

Byte-exact: the four-point ladder of the 1080p gate clip is unchanged
(17643/32317/53048/76330 bytes, same FNV-1a per stream); 8.24s -> 6.61s.
crates/ec-av1/src/decode.rsmodified+157 -0
@@ -6588,6 +6588,7 @@ struct BlkCell {
65886588 /// contexts read (spec `av1_set_entropy_contexts`) — which is what lets a
65896589 /// chroma transform straddling the true, odd-sized frame edge see its
65906590 /// neighbour's state clamped at its own subsampled edge rather than luma's.
6591+#[derive(Clone)]
65916592 struct Neighbours {
65926593 above: Vec<[Neighbour; 3]>,
65936594 left: Vec<[Neighbour; 3]>,
@@ -19674,6 +19675,119 @@ fn plane_snapshot(p: &PlaneBuf<'_>, ss_y: u32) -> PlaneBuf<'static> {
1967419675 PlaneBuf { data: std::borrow::Cow::Owned(data), ..*p }
1967519676 }
1967619677
19678+/// The pre-filter reconstruction of one coded frame, plus the per-block state
19679+/// the loop filters read.
19680+///
19681+/// The encoder's filter search ([`crate::filter_search::pick_filters`])
19682+/// decodes the same coded tile under ~25 candidate deblock/CDEF parameter
19683+/// sets to score each one, and the entropy decode and reconstruction it
19684+/// repeats are identical every time -- both filters run after it, on the
19685+/// finished planes. That re-parse was 24% of the encoder's profile. A caller
19686+/// arms [`arm_filter_replay`] around ONE decode and then re-runs the filters
19687+/// alone through [`replay_filters`]; the result is the same picture the full
19688+/// decode would have returned, because it is the same two calls on a copy of
19689+/// the same planes.
19690+pub(crate) struct FilterReplay {
19691+ y: PlaneBuf<'static>,
19692+ u: PlaneBuf<'static>,
19693+ v: PlaneBuf<'static>,
19694+ neighbours: Neighbours,
19695+ frame_width: usize,
19696+ frame_height: usize,
19697+ width: usize,
19698+ height: usize,
19699+}
19700+
19701+thread_local! {
19702+ /// Set by [`arm_filter_replay`] for the span of one decode.
19703+ static REPLAY_ARMED: std::cell::Cell<bool> = const { std::cell::Cell::new(false) };
19704+ /// What that decode captured, until [`clear_filter_replay`].
19705+ static REPLAY: std::cell::RefCell<Option<FilterReplay>> =
19706+ const { std::cell::RefCell::new(None) };
19707+}
19708+
19709+/// Capture this thread's next decode's pre-filter frame (see [`FilterReplay`]).
19710+pub(crate) fn arm_filter_replay() {
19711+ REPLAY_ARMED.set(true);
19712+}
19713+
19714+/// Drop the armed flag and anything captured: the tile it belongs to is done.
19715+pub(crate) fn clear_filter_replay() {
19716+ REPLAY_ARMED.set(false);
19717+ REPLAY.with_borrow_mut(|r| *r = None);
19718+}
19719+
19720+/// The capture itself, called from the decoder's own pre-filter point. Owns
19721+/// copies of the planes, since the caller filters them in place.
19722+fn capture_filter_replay(
19723+ y: &PlaneBuf<'_>,
19724+ u: &PlaneBuf<'_>,
19725+ v: &PlaneBuf<'_>,
19726+ neighbours: &Neighbours,
19727+ frame_width: usize,
19728+ frame_height: usize,
19729+ width: usize,
19730+ height: usize,
19731+) {
19732+ if !REPLAY_ARMED.replace(false) {
19733+ return;
19734+ }
19735+ let own = |p: &PlaneBuf<'_>| PlaneBuf { data: std::borrow::Cow::Owned(p.data.to_vec()), ..*p };
19736+ REPLAY.with_borrow_mut(|slot| {
19737+ *slot = Some(FilterReplay {
19738+ y: own(y),
19739+ u: own(u),
19740+ v: own(v),
19741+ neighbours: neighbours.clone(),
19742+ frame_width,
19743+ frame_height,
19744+ width,
19745+ height,
19746+ });
19747+ });
19748+}
19749+
19750+/// The filter half of a captured decode, under new parameters: exactly the
19751+/// [`apply_deblock`] / [`apply_cdef`] pair the decoder's own tail runs, on a
19752+/// copy of the captured planes. `None` when nothing is captured (the caller
19753+/// then decodes for real).
19754+pub(crate) fn replay_filters(
19755+ loop_filter: &LoopFilterParams,
19756+ cdef: &CdefParams,
19757+ fctx: &FrameCtx,
19758+) -> Option<Picture> {
19759+ REPLAY.with_borrow(|slot| {
19760+ let r = slot.as_ref()?;
19761+ let copy = |p: &PlaneBuf<'_>| PlaneBuf {
19762+ data: std::borrow::Cow::Owned(p.data.to_vec()),
19763+ ..*p
19764+ };
19765+ let (mut y, mut u, mut v) = (copy(&r.y), copy(&r.u), copy(&r.v));
19766+ apply_deblock(
19767+ &mut y,
19768+ &mut u,
19769+ &mut v,
19770+ loop_filter,
19771+ &r.neighbours,
19772+ r.frame_width,
19773+ r.frame_height,
19774+ fctx,
19775+ );
19776+ apply_cdef(&mut y, &mut u, &mut v, cdef, &r.neighbours, (r.frame_width, r.frame_height), fctx);
19777+ // The capture only happens on the shape the decoder's own tail
19778+ // returns whole (no superres, no loop restoration, no crop), so this
19779+ // is that tail.
19780+ fctx.last_frame_wide_margin.with(|m| *m.borrow_mut() = None);
19781+ Some(Picture {
19782+ width: r.width,
19783+ height: r.height,
19784+ y: y.data.into_owned(),
19785+ u: u.data.into_owned(),
19786+ v: v.data.into_owned(),
19787+ })
19788+ })
19789+}
19790+
1967719791 /// The output crop, taking the plane by value: a frame whose width is
1967819792 /// already superblock-aligned (only its height is padded) has a padded
1967919793 /// buffer whose stride equals the visible width, so the crop is a pure
@@ -22847,6 +22961,28 @@ pub(crate) fn decode_key_frame_tile_with_cdfs(
2284722961 let _ = f.write_all(&crop_wide(&v));
2284822962 }
2284922963 }
22964+ // lane-av1fwd: the encoder's filter search re-decodes this same key
22965+ // frame tile per candidate -- capture it once (see [`FilterReplay`]),
22966+ // on the shape whose tail is the two filter calls and nothing else.
22967+ if superres(fctx).is_none()
22968+ && frame_width as usize == width
22969+ && frame_height as usize == height
22970+ && !(0..3).any(|p| {
22971+ lr.frame_restoration_type[p] != ec_av1_syntax::RestorationType::None
22972+ && lr.loop_restoration_size[p] != 0
22973+ })
22974+ {
22975+ capture_filter_replay(
22976+ &y,
22977+ &u,
22978+ &v,
22979+ &neighbours,
22980+ frame_width as usize,
22981+ frame_height as usize,
22982+ width,
22983+ height,
22984+ );
22985+ }
2285022986 apply_deblock(
2285122987 &mut y,
2285222988 &mut u,
@@ -35446,6 +35582,27 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs(
3544635582 p.finish();
3544735583 }
3544835584 drop(pipe);
35585+ // lane-av1fwd: the encoder scores ~25 filter candidates against this
35586+ // same coded tile ([`FilterReplay`]). Capture the reconstruction here,
35587+ // before either filter has touched it -- but only on the shape whose
35588+ // tail is exactly `apply_deblock` + `apply_cdef` and nothing else.
35589+ if !pipelined
35590+ && !lr_active
35591+ && superres(fctx).is_none()
35592+ && frame_width as usize == width
35593+ && frame_height as usize == height
35594+ {
35595+ capture_filter_replay(
35596+ &y,
35597+ &u,
35598+ &v,
35599+ &neighbours,
35600+ frame_width as usize,
35601+ frame_height as usize,
35602+ width,
35603+ height,
35604+ );
35605+ }
3544935606 if pipelined {
3545035607 cdef_scan_counters(&neighbours);
3545135608 } else {
crates/ec-av1/src/encode.rsmodified+18 -3
@@ -3042,6 +3042,7 @@ pub(crate) fn encode_key_frame_inner(
30423042 &mut chroma,
30433043 [&picture_y8, &picture_u8, &picture_v8],
30443044 true,
3045+ fctx,
30453046 |lf, cdef, h| {
30463047 crate::decode::decode_key_frame_tile(
30473048 &tile,
@@ -3107,6 +3108,7 @@ fn pick_and_apply_filters(
31073108 chroma: &mut [Plane<'_>; 2],
31083109 source: [&[u8]; 3],
31093110 search: bool,
3111+ fctx: &crate::decode::FrameCtx,
31103112 decode: impl Fn(&LoopFilterParams, &CdefParams, &FrameHeader) -> Result<Picture>,
31113113 ) -> Result<()> {
31123114 if !search {
@@ -3117,13 +3119,25 @@ fn pick_and_apply_filters(
31173119 // libaom `av1_pick_filter_level`'s sibling `av1_cdef_search`:
31183120 // `cdef_damping = 3 + (base_qindex >> 6)`, never searched.
31193121 let damping = 3 + (header.quantization.base_q_idx >> 6);
3120- let (lf, cdef, filtered) = crate::filter_search::pick_filters(
3121- |lf, cdef| decode(lf, cdef, &hdr),
3122+ // Every candidate scores the same coded tile: decode it once and re-run
3123+ // the filters alone for the rest (`crate::decode::FilterReplay`, 24% of
3124+ // the encoder's profile before this).
3125+ crate::decode::clear_filter_replay();
3126+ let search_result = crate::filter_search::pick_filters(
3127+ |lf, cdef| {
3128+ if let Some(picture) = crate::decode::replay_filters(lf, cdef, fctx) {
3129+ return Ok(picture);
3130+ }
3131+ crate::decode::arm_filter_replay();
3132+ decode(lf, cdef, &hdr)
3133+ },
31223134 source,
31233135 luma.width,
31243136 (fw, fh),
31253137 damping,
3126- )?;
3138+ );
3139+ crate::decode::clear_filter_replay();
3140+ let (lf, cdef, filtered) = search_result?;
31273141 header.loop_filter = lf;
31283142 header.cdef = cdef;
31293143 let dec_cw = filtered.width.div_ceil(2);
@@ -4234,6 +4248,7 @@ pub(crate) fn encode_inter_frame(
42344248 // `TxMode::Select` too (it used to skip and leave every inter frame
42354249 // unfiltered once Select became the default).
42364250 true,
4251+ fctx,
42374252 |lf, cdef, h| {
42384253 crate::decode::decode_inter_frame_tile(
42394254 &tile,