The filter search decodes its tile once instead of once per candidate
Browse files at this commit675eebbf2a223b66ea8314f60b09ec5f5a65de29Parent: 7947a2d- perf
- the filter search decodes its tile once instead of once per candidate
2 changed files +175 -3
- crates/ec-av1/src/decode.rs +157 -0
- crates/ec-av1/src/encode.rs +18 -3
crates/ec-av1/src/decode.rsmodified+157 -0
| @@ -6588,6 +6588,7 @@ struct BlkCell { | ||
| 6588 | 6588 | /// contexts read (spec `av1_set_entropy_contexts`) — which is what lets a |
| 6589 | 6589 | /// chroma transform straddling the true, odd-sized frame edge see its |
| 6590 | 6590 | /// neighbour's state clamped at its own subsampled edge rather than luma's. |
| 6591 | +#[derive(Clone)] | |
| 6591 | 6592 | struct Neighbours { |
| 6592 | 6593 | above: Vec<[Neighbour; 3]>, |
| 6593 | 6594 | left: Vec<[Neighbour; 3]>, |
| @@ -19674,6 +19675,119 @@ fn plane_snapshot(p: &PlaneBuf<'_>, ss_y: u32) -> PlaneBuf<'static> { | ||
| 19674 | 19675 | PlaneBuf { data: std::borrow::Cow::Owned(data), ..*p } |
| 19675 | 19676 | } |
| 19676 | 19677 | |
| 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 | + | |
| 19677 | 19791 | /// The output crop, taking the plane by value: a frame whose width is |
| 19678 | 19792 | /// already superblock-aligned (only its height is padded) has a padded |
| 19679 | 19793 | /// 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( | ||
| 22847 | 22961 | let _ = f.write_all(&crop_wide(&v)); |
| 22848 | 22962 | } |
| 22849 | 22963 | } |
| 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 | + } | |
| 22850 | 22986 | apply_deblock( |
| 22851 | 22987 | &mut y, |
| 22852 | 22988 | &mut u, |
| @@ -35446,6 +35582,27 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( | ||
| 35446 | 35582 | p.finish(); |
| 35447 | 35583 | } |
| 35448 | 35584 | 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 | + } | |
| 35449 | 35606 | if pipelined { |
| 35450 | 35607 | cdef_scan_counters(&neighbours); |
| 35451 | 35608 | } else { |
crates/ec-av1/src/encode.rsmodified+18 -3
| @@ -3042,6 +3042,7 @@ pub(crate) fn encode_key_frame_inner( | ||
| 3042 | 3042 | &mut chroma, |
| 3043 | 3043 | [&picture_y8, &picture_u8, &picture_v8], |
| 3044 | 3044 | true, |
| 3045 | + fctx, | |
| 3045 | 3046 | |lf, cdef, h| { |
| 3046 | 3047 | crate::decode::decode_key_frame_tile( |
| 3047 | 3048 | &tile, |
| @@ -3107,6 +3108,7 @@ fn pick_and_apply_filters( | ||
| 3107 | 3108 | chroma: &mut [Plane<'_>; 2], |
| 3108 | 3109 | source: [&[u8]; 3], |
| 3109 | 3110 | search: bool, |
| 3111 | + fctx: &crate::decode::FrameCtx, | |
| 3110 | 3112 | decode: impl Fn(&LoopFilterParams, &CdefParams, &FrameHeader) -> Result<Picture>, |
| 3111 | 3113 | ) -> Result<()> { |
| 3112 | 3114 | if !search { |
| @@ -3117,13 +3119,25 @@ fn pick_and_apply_filters( | ||
| 3117 | 3119 | // libaom `av1_pick_filter_level`'s sibling `av1_cdef_search`: |
| 3118 | 3120 | // `cdef_damping = 3 + (base_qindex >> 6)`, never searched. |
| 3119 | 3121 | 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 | + }, | |
| 3122 | 3134 | source, |
| 3123 | 3135 | luma.width, |
| 3124 | 3136 | (fw, fh), |
| 3125 | 3137 | damping, |
| 3126 | - )?; | |
| 3138 | + ); | |
| 3139 | + crate::decode::clear_filter_replay(); | |
| 3140 | + let (lf, cdef, filtered) = search_result?; | |
| 3127 | 3141 | header.loop_filter = lf; |
| 3128 | 3142 | header.cdef = cdef; |
| 3129 | 3143 | let dec_cw = filtered.width.div_ceil(2); |
| @@ -4234,6 +4248,7 @@ pub(crate) fn encode_inter_frame( | ||
| 4234 | 4248 | // `TxMode::Select` too (it used to skip and leave every inter frame |
| 4235 | 4249 | // unfiltered once Select became the default). |
| 4236 | 4250 | true, |
| 4251 | + fctx, | |
| 4237 | 4252 | |lf, cdef, h| { |
| 4238 | 4253 | crate::decode::decode_inter_frame_tile( |
| 4239 | 4254 | &tile, |