Merge branch 'lane-pipefilt' (7cdd977a): deblock and CDEF pipelined per superblock row behind the wavefront (iteration k: deblock-V(k), deblock-H(k-1), snapshot(k-2), CDEF(k-3)), released at each SB row from per-row wavefront completion; whole-frame path kept for superres/segmentation/delta_lf/multi-column/key frames and EC_AV1_PIPE_FILTERS=0; byte-exact on 16 gate hashes + 6 film cuts; wall -2.3% at the composed setting
Browse files at this commit2 changed files +632 -99
- crates/ec-av1/src/decode.rs +619 -99
- crates/ec-av1/src/par.rs +13 -0
crates/ec-av1/src/decode.rsmodified+619 -99
| @@ -2537,6 +2537,13 @@ struct WaveQueue { | ||
| 2537 | 2537 | queued: usize, |
| 2538 | 2538 | /// Per superblock row, the last column reconstructed (-1 = none yet). |
| 2539 | 2539 | done: Vec<isize>, |
| 2540 | + /// Per superblock row, the last column PARSED (submitted, whether or not | |
| 2541 | + /// it recorded any write) and the last column actually pushed as a job | |
| 2542 | + /// (-1 = none). A row is fully reconstructed once it has been parsed to | |
| 2543 | + /// `last_col` and `done` has caught up with `last_push` -- lane-pipefilt | |
| 2544 | + /// reads that to release the row's filters (see [`wave_rows_complete`]). | |
| 2545 | + last_sub: Vec<isize>, | |
| 2546 | + last_push: Vec<isize>, | |
| 2540 | 2547 | /// Jobs popped but not finished -- what [`wave_barrier`] waits out. |
| 2541 | 2548 | active: usize, |
| 2542 | 2549 | closed: bool, |
| @@ -2633,6 +2640,11 @@ fn wave_stats_dump() { | ||
| 2633 | 2640 | WS_WAIT[i].load(Relaxed) as f64 / jobs as f64 / 1e3, |
| 2634 | 2641 | ); |
| 2635 | 2642 | } |
| 2643 | + s += &format!( | |
| 2644 | + " | pipe={}/{}", | |
| 2645 | + PIPE_FRAMES.load(Relaxed), | |
| 2646 | + PIPE_FRAMES.load(Relaxed) + PIPE_WHOLE.load(Relaxed), | |
| 2647 | + ); | |
| 2636 | 2648 | s += &format!( |
| 2637 | 2649 | " | parse={:.0}ms barrier={:.0}ms/{} drain={:.0}ms block_above={} block_left={}", |
| 2638 | 2650 | ms(WS_PARSE.load(Relaxed)), |
| @@ -2722,6 +2734,8 @@ impl<'a> WaveGuard<'a> { | ||
| 2722 | 2734 | rows: (0..rows).map(|_| std::collections::VecDeque::new()).collect(), |
| 2723 | 2735 | queued: 0, |
| 2724 | 2736 | done: vec![-1; rows], |
| 2737 | + last_sub: vec![-1; rows], | |
| 2738 | + last_push: vec![-1; rows], | |
| 2725 | 2739 | active: 0, |
| 2726 | 2740 | closed: false, |
| 2727 | 2741 | }), |
| @@ -2794,10 +2808,16 @@ fn wave_submit(fctx: &crate::decode::FrameCtx, r: usize, c: usize) -> bool { | ||
| 2794 | 2808 | let cap = c.len(); |
| 2795 | 2809 | std::mem::replace(&mut *c, Vec::with_capacity(cap)) |
| 2796 | 2810 | }); |
| 2797 | - if !ops.is_empty() { | |
| 2811 | + { | |
| 2798 | 2812 | let mut q = st.q.lock().unwrap(); |
| 2799 | - q.rows[r].push_back((c, ReconJob { ops, coeffs, preds })); | |
| 2800 | - q.queued += 1; | |
| 2813 | + if (c as isize) > q.last_sub[r] { | |
| 2814 | + q.last_sub[r] = c as isize; | |
| 2815 | + } | |
| 2816 | + if !ops.is_empty() { | |
| 2817 | + q.rows[r].push_back((c, ReconJob { ops, coeffs, preds })); | |
| 2818 | + q.queued += 1; | |
| 2819 | + q.last_push[r] = c as isize; | |
| 2820 | + } | |
| 2801 | 2821 | drop(q); |
| 2802 | 2822 | st.cv.notify_all(); |
| 2803 | 2823 | } |
| @@ -16415,6 +16435,36 @@ fn apply_deblock( | ||
| 16415 | 16435 | } |
| 16416 | 16436 | } |
| 16417 | 16437 | |
| 16438 | +/// [`apply_deblock`] for ONE pass over ONE band of luma rows | |
| 16439 | +/// (`y_lo..y_hi`), the form [`pipe_run`] calls it in. Running the three | |
| 16440 | +/// planes' vertical passes and then their three horizontal ones is the same | |
| 16441 | +/// output as `apply_deblock`'s plane-at-a-time order: the planes are | |
| 16442 | +/// separate buffers and neither pass reads another plane. | |
| 16443 | +#[allow(clippy::too_many_arguments)] | |
| 16444 | +fn deblock_span( | |
| 16445 | + y: &mut PlaneBuf<'static>, | |
| 16446 | + u: &mut PlaneBuf<'static>, | |
| 16447 | + v: &mut PlaneBuf<'static>, | |
| 16448 | + lf: &LoopFilterParams, | |
| 16449 | + n: &Neighbours, | |
| 16450 | + frame_width: usize, | |
| 16451 | + frame_height: usize, | |
| 16452 | + y_lo: usize, | |
| 16453 | + y_hi: usize, | |
| 16454 | + horz: bool, | |
| 16455 | + fctx: &crate::decode::FrameCtx, | |
| 16456 | +) { | |
| 16457 | + if lf.level[0] != 0 || lf.level[1] != 0 { | |
| 16458 | + deblock_plane_span(y, 0, lf, n, frame_width, frame_height, y_lo, y_hi, horz, fctx); | |
| 16459 | + } | |
| 16460 | + if lf.level[2] != 0 { | |
| 16461 | + deblock_plane_span(u, 1, lf, n, frame_width, frame_height, y_lo / 2, y_hi / 2, horz, fctx); | |
| 16462 | + } | |
| 16463 | + if lf.level[3] != 0 { | |
| 16464 | + deblock_plane_span(v, 2, lf, n, frame_width, frame_height, y_lo / 2, y_hi / 2, horz, fctx); | |
| 16465 | + } | |
| 16466 | +} | |
| 16467 | + | |
| 16418 | 16468 | /// Luma-mi-grid position (row or col) a plane-local pixel coordinate falls |
| 16419 | 16469 | /// in -- `Neighbours::tx_grid`/`ref_grid` are always indexed at the luma |
| 16420 | 16470 | /// 4x4-mi grain, even when `coord` is a chroma coordinate (2 chroma px per |
| @@ -17945,6 +17995,54 @@ fn lf_level( | ||
| 17945 | 17995 | lvl.clamp(0, 63) |
| 17946 | 17996 | } |
| 17947 | 17997 | |
| 17998 | +thread_local! { | |
| 17999 | + /// lane-pipefilt: [`EdgeCtx`]'s level LUT, memoised per thread. The table | |
| 18000 | + /// is a pure function of the frame's [`LoopFilterParams`], the plane, the | |
| 18001 | + /// pass and segment 0's own `SEG_LVL_ALT_LF_*` datum (the only thing | |
| 18002 | + /// [`lf_level`] reads out of the `FrameCtx` at `segment_id = 0`), so one | |
| 18003 | + /// build per plane and pass serves the whole frame. The pipeline builds | |
| 18004 | + /// an `EdgeCtx` per superblock-row band rather than per frame, which | |
| 18005 | + /// turned 20 builds a frame into ~680 -- 960 `lf_level` calls each. | |
| 18006 | + static LVL_CACHE: std::cell::RefCell< | |
| 18007 | + Vec<((usize, usize, LoopFilterParams, Option<i32>), [[u8; 15]; 64])>, | |
| 18008 | + > = const { std::cell::RefCell::new(Vec::new()) }; | |
| 18009 | +} | |
| 18010 | + | |
| 18011 | +/// `lf_level` over the whole `(base level, ref frame)` domain -- see | |
| 18012 | +/// [`LVL_CACHE`]. `lf_level`'s own first step is | |
| 18013 | +/// `(base0 + delta_lf).clamp(0, 63)`, so passing `b - base0` pins its base at | |
| 18014 | +/// `b` and the rest of the function is a pure function of `(b, ref_frame)`. | |
| 18015 | +fn lvl_table( | |
| 18016 | + lf: &LoopFilterParams, | |
| 18017 | + plane_idx: usize, | |
| 18018 | + dir: usize, | |
| 18019 | + base0: i32, | |
| 18020 | + fctx: &crate::decode::FrameCtx, | |
| 18021 | +) -> [[u8; 15]; 64] { | |
| 18022 | + let feature = if plane_idx == 0 { SEG_LVL_ALT_LF_Y_V + dir } else { plane_idx + 2 }; | |
| 18023 | + let key = (plane_idx, dir, *lf, seg_feature(0, feature, fctx)); | |
| 18024 | + if let Some(t) = LVL_CACHE.with(|c| { | |
| 18025 | + c.borrow().iter().find(|(k, _)| *k == key).map(|(_, t)| *t) | |
| 18026 | + }) { | |
| 18027 | + return t; | |
| 18028 | + } | |
| 18029 | + let mut lvl = [[0u8; 15]; 64]; | |
| 18030 | + for (b, row) in lvl.iter_mut().enumerate() { | |
| 18031 | + for (r, cell) in row.iter_mut().enumerate() { | |
| 18032 | + *cell = | |
| 18033 | + lf_level(lf, plane_idx, dir, (r as i32 - 7) as i8, b as i32 - base0, 0, fctx) as u8; | |
| 18034 | + } | |
| 18035 | + } | |
| 18036 | + LVL_CACHE.with(|c| { | |
| 18037 | + let mut c = c.borrow_mut(); | |
| 18038 | + if c.len() >= 8 { | |
| 18039 | + c.remove(0); | |
| 18040 | + } | |
| 18041 | + c.push((key, lvl)); | |
| 18042 | + }); | |
| 18043 | + lvl | |
| 18044 | +} | |
| 18045 | + | |
| 17948 | 18046 | /// Spec 7.14.2's `set_lpf_parameters`, restricted to the uniform-level, |
| 17949 | 18047 | /// no-segmentation, no-delta-LF path and to this decoder's own invariant |
| 17950 | 18048 | /// that a coded block's transform is always its own full size -- so a TU |
| @@ -17997,17 +18095,7 @@ impl<'a> EdgeCtx<'a> { | ||
| 17997 | 18095 | } else { |
| 17998 | 18096 | lf.level[3] |
| 17999 | 18097 | }); |
| 18000 | - // `lf_level`'s own first step is `(base0 + delta_lf).clamp(0, 63)`, | |
| 18001 | - // so passing `b - base0` pins its base at `b` and the rest of the | |
| 18002 | - // function is a pure function of `(b, ref_frame)`. | |
| 18003 | - let mut lvl = [[0u8; 15]; 64]; | |
| 18004 | - for (b, row) in lvl.iter_mut().enumerate() { | |
| 18005 | - for (r, cell) in row.iter_mut().enumerate() { | |
| 18006 | - *cell = | |
| 18007 | - lf_level(lf, plane_idx, dir, (r as i32 - 7) as i8, b as i32 - base0, 0, fctx) | |
| 18008 | - as u8; | |
| 18009 | - } | |
| 18010 | - } | |
| 18098 | + let lvl = lvl_table(lf, plane_idx, dir, base0, fctx); | |
| 18011 | 18099 | Self { |
| 18012 | 18100 | lf, |
| 18013 | 18101 | n, |
| @@ -18952,6 +19040,34 @@ fn deblock_plane( | ||
| 18952 | 19040 | n: &Neighbours, |
| 18953 | 19041 | frame_width: usize, |
| 18954 | 19042 | frame_height: usize, fctx: &crate::decode::FrameCtx, |
| 19043 | +) { | |
| 19044 | + // spec 7.14's order: this plane's whole vertical pass, then its whole | |
| 19045 | + // horizontal one. lane-pipefilt split the two into one row-ranged | |
| 19046 | + // function so the per-superblock-row pipeline can run them a band at a | |
| 19047 | + // time; `0..usize::MAX` is the whole plane, i.e. exactly this call. | |
| 19048 | + deblock_plane_span(plane, plane_idx, lf, n, frame_width, frame_height, 0, usize::MAX, false, fctx); | |
| 19049 | + deblock_plane_span(plane, plane_idx, lf, n, frame_width, frame_height, 0, usize::MAX, true, fctx); | |
| 19050 | +} | |
| 19051 | + | |
| 19052 | +/// One pass of [`deblock_plane`] -- vertical edges (`horz == false`) or | |
| 19053 | +/// horizontal ones -- restricted to the plane rows `span_lo..span_hi`. | |
| 19054 | +/// | |
| 19055 | +/// A row span is a legal cut of either pass because a band boundary is a | |
| 19056 | +/// multiple of 16 rows and every edge's kernel stays inside its own | |
| 19057 | +/// 4-sample group's reach; the pipeline's row lags (see [`pipe_run`]) are | |
| 19058 | +/// what keep the two passes in spec order across bands. | |
| 19059 | +#[allow(clippy::too_many_arguments)] | |
| 19060 | +fn deblock_plane_span( | |
| 19061 | + plane: &mut PlaneBuf<'static>, | |
| 19062 | + plane_idx: usize, | |
| 19063 | + lf: &LoopFilterParams, | |
| 19064 | + n: &Neighbours, | |
| 19065 | + frame_width: usize, | |
| 19066 | + frame_height: usize, | |
| 19067 | + span_lo: usize, | |
| 19068 | + span_hi: usize, | |
| 19069 | + horz: bool, | |
| 19070 | + fctx: &crate::decode::FrameCtx, | |
| 18955 | 19071 | ) { |
| 18956 | 19072 | let chroma = plane_idx != 0; |
| 18957 | 19073 | // r7: libaom clips the deblock loop to the CODED frame's own |
| @@ -19012,10 +19128,15 @@ fn deblock_plane( | ||
| 19012 | 19128 | plane.data.to_mut(); |
| 19013 | 19129 | let threads = crate::par::filter_threads(); |
| 19014 | 19130 | let sp = crate::par::Shared::new(plane); |
| 19015 | - let vbands: Vec<(usize, usize)> = crate::par::bands(th.div_ceil(16), threads) | |
| 19016 | - .into_iter() | |
| 19017 | - .map(|(a, b)| (a * 16, (b * 16).min(th))) | |
| 19018 | - .collect(); | |
| 19131 | + let (sp_lo, sp_hi) = (span_lo.min(th), span_hi.min(th)); | |
| 19132 | + let vbands: Vec<(usize, usize)> = if horz || sp_lo >= sp_hi { | |
| 19133 | + Vec::new() | |
| 19134 | + } else { | |
| 19135 | + crate::par::bands((sp_hi - sp_lo).div_ceil(16), threads) | |
| 19136 | + .into_iter() | |
| 19137 | + .map(|(a, b)| (sp_lo + a * 16, (sp_lo + b * 16).min(sp_hi))) | |
| 19138 | + .collect() | |
| 19139 | + }; | |
| 19019 | 19140 | crate::par::run_bands(&vbands, fctx, |y_lo, y_hi, fctx| { |
| 19020 | 19141 | // SAFETY: this band writes only rows `y_lo..y_hi` of the plane, which |
| 19021 | 19142 | // no other band touches (see `Shared`'s own corner-cut note). |
| @@ -19084,17 +19205,21 @@ fn deblock_plane( | ||
| 19084 | 19205 | }); |
| 19085 | 19206 | // The horizontal pass runs only after the WHOLE vertical pass of this |
| 19086 | 19207 | // plane (spec 7.14 order), which `run_bands` guarantees by joining. |
| 19087 | - let hbands: Vec<(usize, usize)> = crate::par::bands(tw.div_ceil(16), threads) | |
| 19088 | - .into_iter() | |
| 19089 | - .map(|(a, b)| (a * 16, (b * 16).min(tw))) | |
| 19090 | - .collect(); | |
| 19208 | + let hbands: Vec<(usize, usize)> = if !horz || sp_lo >= sp_hi { | |
| 19209 | + Vec::new() | |
| 19210 | + } else { | |
| 19211 | + crate::par::bands(tw.div_ceil(16), threads) | |
| 19212 | + .into_iter() | |
| 19213 | + .map(|(a, b)| (a * 16, (b * 16).min(tw))) | |
| 19214 | + .collect() | |
| 19215 | + }; | |
| 19091 | 19216 | crate::par::run_bands(&hbands, fctx, |x_lo, x_hi, fctx| { |
| 19092 | 19217 | // SAFETY: this band writes only columns `x_lo..x_hi`. |
| 19093 | 19218 | #[allow(unsafe_code)] |
| 19094 | 19219 | let plane = unsafe { sp.get() }; |
| 19095 | 19220 | let hctx = EdgeCtx::new(lf, n, plane_idx, 1, chroma, seg_lf_active, fctx); |
| 19096 | - let mut y0 = 4usize; | |
| 19097 | - while y0 < th { | |
| 19221 | + let mut y0 = sp_lo.max(4); | |
| 19222 | + while y0 < sp_hi { | |
| 19098 | 19223 | let mut x0 = x_lo; |
| 19099 | 19224 | while x0 < x_hi { |
| 19100 | 19225 | if x0 + 16 <= x_hi && y0 >= 8 && y0 + 7 <= rows { |
| @@ -19211,6 +19336,10 @@ fn scratch_sized(n: usize) -> Vec<u16> { | ||
| 19211 | 19336 | /// memmove per plane per frame into 4 rows in 64 (luma). Every other row of |
| 19212 | 19337 | /// the pooled buffer keeps whatever a previous frame left there -- an |
| 19213 | 19338 | /// initialised value that no reader ever reaches. |
| 19339 | +fn snap_alloc(p: &PlaneBuf<'_>) -> PlaneBuf<'static> { | |
| 19340 | + PlaneBuf { data: std::borrow::Cow::Owned(scratch_sized(p.data.len())), ..*p } | |
| 19341 | +} | |
| 19342 | + | |
| 19214 | 19343 | fn plane_snapshot(p: &PlaneBuf<'_>, ss_y: u32) -> PlaneBuf<'static> { |
| 19215 | 19344 | let mut data = scratch_sized(p.data.len()); |
| 19216 | 19345 | let rows = if p.width == 0 { 0 } else { p.data.len() / p.width }; |
| @@ -19402,27 +19531,272 @@ fn cdef_band( | ||
| 19402 | 19531 | } |
| 19403 | 19532 | } |
| 19404 | 19533 | |
| 19405 | -fn apply_cdef( | |
| 19406 | - y: &mut PlaneBuf<'static>, | |
| 19407 | - u: &mut PlaneBuf<'static>, | |
| 19408 | - v: &mut PlaneBuf<'static>, | |
| 19409 | - cdef: &CdefParams, | |
| 19410 | - skip_grid: &Neighbours, | |
| 19411 | - // This frame header's own CROPPED luma size, for the straddling-unit | |
| 19412 | - // counter only -- CDEF itself walks the mi-rounded plane. | |
| 19413 | - (frame_w, frame_h): (usize, usize), fctx: &crate::decode::FrameCtx, | |
| 19414 | -) { | |
| 19415 | - // lane-part32 r2 debug rung, env-gated: see `apply_deblock`'s sibling. | |
| 19416 | - if crate::envflags::env_flag!("EC_AV1_DEBUG_SKIP_CDEF") { | |
| 19534 | + | |
| 19535 | +// --- lane-pipefilt: the per-superblock-row loop filter pipeline ---------- | |
| 19536 | +// | |
| 19537 | +// DEPENDENCY SHAPE (derived from this decoder's own whole-frame pass order: | |
| 19538 | +// every plane's vertical deblock edges, then every plane's horizontal ones, | |
| 19539 | +// then CDEF, then -- unpipelined -- superres and loop restoration). | |
| 19540 | +// A band is 64 luma rows, one superblock row (a 128 superblock is two). | |
| 19541 | +// | |
| 19542 | +// * recon: intra prediction of band r+1 reads the UNFILTERED bottom rows of | |
| 19543 | +// band r, so no filter may touch band r before band r+1 is reconstructed. | |
| 19544 | +// Hence V(r) waits for `ready >= r + 2`. | |
| 19545 | +// * V(r) (vertical edges whose 4-sample groups live in band r's rows) then | |
| 19546 | +// H(r-1) (horizontal edges at `y` in band r-1, including the one at its | |
| 19547 | +// own top boundary): the edge at `y = r*64` reads/writes 6-7 rows either | |
| 19548 | +// side of the band boundary, so H(r-1)'s last edge reaches into band r, | |
| 19549 | +// which the whole-frame order has already vertical-filtered -- V(r) | |
| 19550 | +// before H(r-1) reproduces exactly that. | |
| 19551 | +// * post-deblock finality: band j's own bottom rows are still rewritten by | |
| 19552 | +// H(j+1)'s first edge, so band j's post-deblock snapshot (`snap`) may | |
| 19553 | +// only be taken after H(j+1). | |
| 19554 | +// * CDEF(j) reads the post-deblock picture 2-3 rows across its 64x64 | |
| 19555 | +// boundaries, i.e. bands j-1..j+1, so it runs after snap(j+1). | |
| 19556 | +// | |
| 19557 | +// One iteration k therefore runs V(k), H(k-1), snap(k-2), CDEF(k-3), and the | |
| 19558 | +// loop runs k in `0..nbands+3` so the tail drains. Loop restoration (and the | |
| 19559 | +// superres upscale, which is a whole-frame refusal for this path) stay in the | |
| 19560 | +// frame thread's tail; the snapshot the pipeline built is what LR reads. | |
| 19561 | +struct PipeQ { | |
| 19562 | + /// Bands whose reconstruction the frame thread has released. | |
| 19563 | + ready: usize, | |
| 19564 | + abort: bool, | |
| 19565 | +} | |
| 19566 | + | |
| 19567 | +pub(crate) struct PipeSync { | |
| 19568 | + m: std::sync::Mutex<PipeQ>, | |
| 19569 | + cv: std::sync::Condvar, | |
| 19570 | +} | |
| 19571 | + | |
| 19572 | +impl PipeSync { | |
| 19573 | + fn new() -> Self { | |
| 19574 | + Self { | |
| 19575 | + m: std::sync::Mutex::new(PipeQ { ready: 0, abort: false }), | |
| 19576 | + cv: std::sync::Condvar::new(), | |
| 19577 | + } | |
| 19578 | + } | |
| 19579 | + fn note(&self, ready: usize) { | |
| 19580 | + let mut q = self.m.lock().unwrap(); | |
| 19581 | + if ready > q.ready { | |
| 19582 | + q.ready = ready; | |
| 19583 | + self.cv.notify_all(); | |
| 19584 | + } | |
| 19585 | + } | |
| 19586 | + /// Releases every remaining band (`abort` = the tile decoder is bailing | |
| 19587 | + /// out, so the worker returns instead of finishing the frame). | |
| 19588 | + fn close(&self, all: usize, abort: bool) { | |
| 19589 | + let mut q = self.m.lock().unwrap(); | |
| 19590 | + q.ready = q.ready.max(all); | |
| 19591 | + q.abort |= abort; | |
| 19592 | + self.cv.notify_all(); | |
| 19593 | + } | |
| 19594 | + /// `false` when the frame was aborted. | |
| 19595 | + fn wait_for(&self, want: usize) -> bool { | |
| 19596 | + let mut q = self.m.lock().unwrap(); | |
| 19597 | + while q.ready < want && !q.abort { | |
| 19598 | + q = self.cv.wait(q).unwrap(); | |
| 19599 | + } | |
| 19600 | + !q.abort | |
| 19601 | + } | |
| 19602 | +} | |
| 19603 | + | |
| 19604 | +/// Band `j`'s rows of `src` copied into the pipeline's post-deblock | |
| 19605 | +/// snapshot. `full` mirrors [`apply_cdef`]'s whole-plane pre-CDEF copy; | |
| 19606 | +/// otherwise only the four rows around each stripe boundary that loop | |
| 19607 | +/// restoration reads are taken, exactly [`plane_snapshot`]'s rows. | |
| 19608 | +fn snap_band(dst: &mut [u16], src: &PlaneBuf<'_>, ss_y: u32, y_lo: usize, y_hi: usize, full: bool) { | |
| 19609 | + let w = src.width; | |
| 19610 | + if w == 0 { | |
| 19417 | 19611 | return; |
| 19418 | 19612 | } |
| 19419 | - // lane-cdefstrip r1 instrument, BEFORE either early return so it is valid | |
| 19420 | - // for every frame: an 8x8 CDEF unit none of whose four mi cells a coded | |
| 19421 | - // block wrote this frame. `Neighbours` seeds `skip_grid` all-false, so a | |
| 19422 | - // decode arm that forgets `fill_skip_grid_rect` reads "not skip" and CDEF | |
| 19423 | - // filters a unit libaom's `is_8x8_block_skip` (`cdef.c:29-38`) may exclude. | |
| 19424 | - // Always 0 on every stream measured; a gate asserts it stays 0. | |
| 19425 | - { | |
| 19613 | + let rows = src.data.len() / w; | |
| 19614 | + let (lo, hi) = (y_lo.min(rows), y_hi.min(rows)); | |
| 19615 | + if lo >= hi { | |
| 19616 | + return; | |
| 19617 | + } | |
| 19618 | + if full { | |
| 19619 | + dst[lo * w..hi * w].copy_from_slice(&src.data[lo * w..hi * w]); | |
| 19620 | + return; | |
| 19621 | + } | |
| 19622 | + let stripe_h = 64usize >> ss_y; | |
| 19623 | + let offset = 8usize >> ss_y; | |
| 19624 | + let mut b = stripe_h - offset; | |
| 19625 | + while b < rows { | |
| 19626 | + let (a, e) = ((b - 2).max(lo), (b + 2).min(rows).min(hi)); | |
| 19627 | + if a < e { | |
| 19628 | + dst[a * w..e * w].copy_from_slice(&src.data[a * w..e * w]); | |
| 19629 | + } | |
| 19630 | + b += stripe_h; | |
| 19631 | + } | |
| 19632 | +} | |
| 19633 | + | |
| 19634 | +/// Everything the pipeline worker needs that is not a plane or a grid. | |
| 19635 | +struct PipeCfg<'a> { | |
| 19636 | + lf: &'a LoopFilterParams, | |
| 19637 | + cdef: &'a CdefParams, | |
| 19638 | + /// `(cdef_idx_grid.as_ptr(), len)` -- the frame thread keeps filling the | |
| 19639 | + /// entries of the superblock rows the pipeline has not reached; the Vec | |
| 19640 | + /// itself is sized once per frame, so the pointer stays valid. | |
| 19641 | + idx_grid: (*const u8, usize), | |
| 19642 | + sb_cols: usize, | |
| 19643 | + frame_w: usize, | |
| 19644 | + frame_h: usize, | |
| 19645 | + nbands: usize, | |
| 19646 | + cdef_on: bool, | |
| 19647 | + snap_on: bool, | |
| 19648 | +} | |
| 19649 | + | |
| 19650 | +// SAFETY: the `*const u8` is a read-only view of a Vec that is allocated once | |
| 19651 | +// per frame and only ever written at indices the pipeline is three bands | |
| 19652 | +// behind (see this module's dependency note). | |
| 19653 | +#[allow(unsafe_code)] | |
| 19654 | +unsafe impl Send for PipeCfg<'_> {} | |
| 19655 | + | |
| 19656 | +#[allow(unsafe_code)] | |
| 19657 | +#[allow(clippy::too_many_arguments)] | |
| 19658 | +fn pipe_run( | |
| 19659 | + sync: &PipeSync, | |
| 19660 | + sy: crate::par::Shared<'_, PlaneBuf<'static>>, | |
| 19661 | + su: crate::par::Shared<'_, PlaneBuf<'static>>, | |
| 19662 | + sv: crate::par::Shared<'_, PlaneBuf<'static>>, | |
| 19663 | + sn: crate::par::Shared<'_, Neighbours>, | |
| 19664 | + cfg: PipeCfg<'_>, | |
| 19665 | + snap: &mut Option<(PlaneBuf<'static>, PlaneBuf<'static>, PlaneBuf<'static>)>, | |
| 19666 | + fctx: &crate::decode::FrameCtx, | |
| 19667 | +) { | |
| 19668 | + // SAFETY: the frame thread parses (and reconstructs) bands the pipeline | |
| 19669 | + // is at least two behind, and the two ranges never meet -- see the | |
| 19670 | + // dependency note above and `Shared`'s own corner-cut. | |
| 19671 | + let (y, u, v) = unsafe { (sy.get(), su.get(), sv.get()) }; | |
| 19672 | + let n: &Neighbours = unsafe { sn.get() }; | |
| 19673 | + let idx_grid = unsafe { std::slice::from_raw_parts(cfg.idx_grid.0, cfg.idx_grid.1) }; | |
| 19674 | + let coeff_shift = i32::from(bit_depth(fctx)) - 8; | |
| 19675 | + let damping = i32::from(cfg.cdef.damping) + coeff_shift; | |
| 19676 | + let damping_uv = (damping - 1).max(0); | |
| 19677 | + let (fw, fh) = (cfg.frame_w, cfg.frame_h); | |
| 19678 | + let nb = cfg.nbands; | |
| 19679 | + for k in 0..nb + 3 { | |
| 19680 | + if !sync.wait_for((k + 2).min(nb)) { | |
| 19681 | + return; | |
| 19682 | + } | |
| 19683 | + if k < nb { | |
| 19684 | + deblock_span(y, u, v, cfg.lf, n, fw, fh, k * 64, (k + 1) * 64, false, fctx); | |
| 19685 | + } | |
| 19686 | + if k >= 1 && k - 1 < nb { | |
| 19687 | + deblock_span(y, u, v, cfg.lf, n, fw, fh, (k - 1) * 64, k * 64, true, fctx); | |
| 19688 | + } | |
| 19689 | + if cfg.snap_on && k >= 2 && k - 2 < nb { | |
| 19690 | + let j = k - 2; | |
| 19691 | + if let Some((sy2, su2, sv2)) = snap.as_mut() { | |
| 19692 | + snap_band(sy2.data.to_mut(), y, 0, j * 64, (j + 1) * 64, cfg.cdef_on); | |
| 19693 | + snap_band(su2.data.to_mut(), u, 1, j * 32, (j + 1) * 32, cfg.cdef_on); | |
| 19694 | + snap_band(sv2.data.to_mut(), v, 1, j * 32, (j + 1) * 32, cfg.cdef_on); | |
| 19695 | + } | |
| 19696 | + } | |
| 19697 | + if cfg.cdef_on && k >= 3 && k - 3 < nb { | |
| 19698 | + let j = k - 3; | |
| 19699 | + if let Some((sy2, su2, sv2)) = snap.as_ref() { | |
| 19700 | + cdef_rows( | |
| 19701 | + y, u, v, &sy2.data, &su2.data, &sv2.data, cfg.cdef, n, idx_grid, cfg.sb_cols, | |
| 19702 | + coeff_shift, damping, damping_uv, (fw, fh), j * 16, (j + 1) * 16, fctx, | |
| 19703 | + ); | |
| 19704 | + } | |
| 19705 | + } | |
| 19706 | + } | |
| 19707 | +} | |
| 19708 | + | |
| 19709 | +/// The pipeline's handle on the frame thread: [`PipeGuard::note`] releases | |
| 19710 | +/// bands as reconstruction completes, [`PipeGuard::finish`] drains it, and | |
| 19711 | +/// `Drop` aborts it on any `?` out of the tile decoder (the worker holds raw | |
| 19712 | +/// pointers into the frame's planes, so it may not outlive them). | |
| 19713 | +struct PipeGuard<'a> { | |
| 19714 | + sync: std::sync::Arc<PipeSync>, | |
| 19715 | + batch: Option<crate::par::Batch<'a>>, | |
| 19716 | + nbands: usize, | |
| 19717 | +} | |
| 19718 | + | |
| 19719 | +impl PipeGuard<'_> { | |
| 19720 | + fn note(&self, bands: usize) { | |
| 19721 | + self.sync.note(bands); | |
| 19722 | + } | |
| 19723 | + fn finish(&mut self) { | |
| 19724 | + self.sync.close(self.nbands, false); | |
| 19725 | + // Dropping the batch waits the worker out. | |
| 19726 | + self.batch.take(); | |
| 19727 | + } | |
| 19728 | +} | |
| 19729 | + | |
| 19730 | +impl Drop for PipeGuard<'_> { | |
| 19731 | + fn drop(&mut self) { | |
| 19732 | + self.sync.close(self.nbands, true); | |
| 19733 | + } | |
| 19734 | +} | |
| 19735 | + | |
| 19736 | +/// Leading superblock rows of the live tile's wavefront that are fully | |
| 19737 | +/// reconstructed: parsed out to the last column and with every job that | |
| 19738 | +/// carried a write finished. 0 when reconstruction is inline (no wavefront), | |
| 19739 | +/// where the caller uses the parse position instead. | |
| 19740 | +fn wave_rows_complete(fctx: &crate::decode::FrameCtx) -> usize { | |
| 19741 | + let st = fctx.wave.with(|w| w.borrow().clone()); | |
| 19742 | + let Some(st) = st else { return 0 }; | |
| 19743 | + let q = st.q.lock().unwrap(); | |
| 19744 | + let mut n = 0; | |
| 19745 | + while n < q.rows.len() { | |
| 19746 | + if q.last_sub[n] < st.last_col as isize || q.done[n] < q.last_push[n] { | |
| 19747 | + break; | |
| 19748 | + } | |
| 19749 | + n += 1; | |
| 19750 | + } | |
| 19751 | + n | |
| 19752 | +} | |
| 19753 | + | |
| 19754 | +/// Frames whose loop filters ran pipelined, and frames that took the | |
| 19755 | +/// whole-frame path instead (`EC_AV1_WAVE_STATS`). | |
| 19756 | +static PIPE_FRAMES: AtomicU64 = AtomicU64::new(0); | |
| 19757 | +static PIPE_WHOLE: AtomicU64 = AtomicU64::new(0); | |
| 19758 | + | |
| 19759 | +/// Whether this frame's filters may run per superblock row. Everything that | |
| 19760 | +/// reads the picture out of band order, or the pipeline's own inputs out of | |
| 19761 | +/// row order, keeps the whole-frame path: superres (a between-CDEF-and-LR | |
| 19762 | +/// whole-plane resample), segmentation (the deblocker reads `seg_ids`, which | |
| 19763 | +/// the parse thread is still filling), delta_lf, more than one tile COLUMN (a | |
| 19764 | +/// frame row is only complete after the last tile column's own parse), and | |
| 19765 | +/// every pixel-dump / stage-skip debug flag, whose whole point is the | |
| 19766 | +/// unpipelined stage boundary. The pipeline also stays off unless some | |
| 19767 | +/// threading is on -- at (1, 1) the filters would run on a worker thread for | |
| 19768 | +/// nothing and the crate's `thread_local!` gate counters would miss them. | |
| 19769 | +fn pipe_filters_ok(fctx: &crate::decode::FrameCtx, tile_cols: u32, lf_present: bool) -> bool { | |
| 19770 | + if crate::envflags::var("EC_AV1_PIPE_FILTERS").map(|v| v.trim() == "0").unwrap_or(false) { | |
| 19771 | + return false; | |
| 19772 | + } | |
| 19773 | + if crate::par::filter_threads() == 1 && crate::par::recon_threads() == 1 { | |
| 19774 | + return false; | |
| 19775 | + } | |
| 19776 | + if tile_cols != 1 || lf_present || superres(fctx).is_some() { | |
| 19777 | + return false; | |
| 19778 | + } | |
| 19779 | + if fctx.seg.with(|s| s.borrow().enabled) { | |
| 19780 | + return false; | |
| 19781 | + } | |
| 19782 | + !(crate::envflags::is_set("EC_AV1_PREFILT_DUMP") | |
| 19783 | + || crate::envflags::is_set("EC_AV1_PREFILT_DUMP16") | |
| 19784 | + || crate::envflags::is_set("EC_AV1_PREFILT_WIDE_DUMP") | |
| 19785 | + || crate::envflags::is_set("EC_AV1_POSTDEBLOCK_DUMP") | |
| 19786 | + || crate::envflags::is_set("EC_AV1_POSTDEBLOCK_DUMP16") | |
| 19787 | + || crate::envflags::is_set("EC_AV1_POSTCDEF_DUMP") | |
| 19788 | + || crate::envflags::is_set("EC_AV1_POSTCDEF_DUMP16") | |
| 19789 | + || crate::envflags::is_set("EC_AV1_DEBUG_SKIP_DEBLOCK") | |
| 19790 | + || crate::envflags::is_set("EC_AV1_DEBUG_SKIP_CDEF") | |
| 19791 | + || crate::envflags::is_set("EC_AV1_DEBUG_CDEF_BAND") | |
| 19792 | + || crate::envflags::is_set("EC_AV1_DEBLOCK_TRACE") | |
| 19793 | + || crate::envflags::is_set("EC_AV1_DEBLOCK_TRACE_V")) | |
| 19794 | +} | |
| 19795 | + | |
| 19796 | +/// lane-cdefstrip r1 instrument, run for every frame BEFORE either of | |
| 19797 | +/// [`apply_cdef`]'s early returns (lane-pipefilt: the pipelined path | |
| 19798 | +/// never enters `apply_cdef`, so it calls this itself). | |
| 19799 | +fn cdef_scan_counters(skip_grid: &Neighbours) { | |
| 19426 | 19800 | let mut r = 0usize; |
| 19427 | 19801 | while r < skip_grid.mi_rows { |
| 19428 | 19802 | let mut c = 0usize; |
| @@ -19454,47 +19828,55 @@ fn apply_cdef( | ||
| 19454 | 19828 | } |
| 19455 | 19829 | r += 2; |
| 19456 | 19830 | } |
| 19457 | - } | |
| 19458 | - // `cdef.bits == 0` still means "no filtering at all" whenever the single | |
| 19459 | - // (index 0) strength pair is all-zero -- the fast path every existing | |
| 19460 | - // gate stream takes. A `bits > 0` frame can still be all-zero-strength | |
| 19461 | - // at every index, but that is rare enough not to special-case; the | |
| 19462 | - // per-superblock lookup below degrades to the same index-0 read anyway | |
| 19463 | - // when [`CDEF_IDX_GRID`] was never populated (`bits == 0`). | |
| 19464 | - if cdef.bits == 0 | |
| 19831 | +} | |
| 19832 | + | |
| 19833 | +/// `true` when this frame's CDEF cannot change a sample: `bits == 0` still | |
| 19834 | +/// means "no filtering at all" whenever the single (index 0) strength pair | |
| 19835 | +/// is all-zero, the fast path every existing gate stream takes. | |
| 19836 | +fn cdef_all_zero(cdef: &CdefParams) -> bool { | |
| 19837 | + cdef.bits == 0 | |
| 19465 | 19838 | && cdef.y_pri_strength[0] == 0 |
| 19466 | 19839 | && cdef.y_sec_strength[0] == 0 |
| 19467 | 19840 | && cdef.uv_pri_strength[0] == 0 |
| 19468 | 19841 | && cdef.uv_sec_strength[0] == 0 |
| 19469 | - { | |
| 19842 | +} | |
| 19843 | + | |
| 19844 | +/// [`apply_cdef`]'s band loop over the mi rows `r_lo0..r_hi0` only (the | |
| 19845 | +/// whole frame when `0..mi_rows`, which is what `apply_cdef` passes). | |
| 19846 | +/// `src_*` are the PRE-CDEF planes every unit reads; the writes are this | |
| 19847 | +/// row range's own pixels. | |
| 19848 | +#[allow(clippy::too_many_arguments)] | |
| 19849 | +fn cdef_rows( | |
| 19850 | + y: &mut PlaneBuf<'static>, | |
| 19851 | + u: &mut PlaneBuf<'static>, | |
| 19852 | + v: &mut PlaneBuf<'static>, | |
| 19853 | + src_y: &[u16], | |
| 19854 | + src_u: &[u16], | |
| 19855 | + src_v: &[u16], | |
| 19856 | + cdef: &CdefParams, | |
| 19857 | + skip_grid: &Neighbours, | |
| 19858 | + idx_grid: &[u8], | |
| 19859 | + sb_cols: usize, | |
| 19860 | + coeff_shift: i32, | |
| 19861 | + damping: i32, | |
| 19862 | + damping_uv: i32, | |
| 19863 | + (frame_w, frame_h): (usize, usize), | |
| 19864 | + r_lo0: usize, | |
| 19865 | + r_hi0: usize, fctx: &crate::decode::FrameCtx, | |
| 19866 | +) { | |
| 19867 | + let mi_rows = skip_grid.mi_rows; | |
| 19868 | + let (lo, hi) = (r_lo0.min(mi_rows), r_hi0.min(mi_rows)); | |
| 19869 | + if lo >= hi { | |
| 19470 | 19870 | return; |
| 19471 | 19871 | } |
| 19472 | - let sb_cols = fctx.cdef_sb_cols.with(|c| c.get()); | |
| 19473 | - // One entry per superblock (a few hundred at 4K), so this clone is not | |
| 19474 | - // one of the per-frame copies lane-perf6 pooled below. | |
| 19475 | - let idx_grid = fctx.cdef_idx_grid.with(|g| g.borrow().clone()); | |
| 19476 | - let src_y = scratch_from(&y.data); | |
| 19477 | - let src_u = scratch_from(&u.data); | |
| 19478 | - let src_v = scratch_from(&v.data); | |
| 19479 | - // libaom `av1_cdef_filter_fb`: `pri_strength = level << coeff_shift`, | |
| 19480 | - // `sec_strength <<= coeff_shift`, `damping += coeff_shift - (pli != 0)`. | |
| 19481 | - let coeff_shift = i32::from(bit_depth(fctx)) - 8; | |
| 19482 | - let damping = i32::from(cdef.damping) + coeff_shift; | |
| 19483 | - let damping_uv = (damping - 1).max(0); | |
| 19484 | - // lane-filt1: every CDEF unit reads ONLY the pre-CDEF snapshots | |
| 19485 | - // (`src_y`/`src_u`/`src_v`, immutable here) and writes its own 8x8 luma / | |
| 19486 | - // 4x4 chroma square, so units are independent. Bands are mi ROW bands (2 | |
| 19487 | - // mi = 8 luma rows, 4 chroma rows), which makes each band's writes a | |
| 19488 | - // disjoint row range of all three planes. | |
| 19489 | 19872 | let (sy, su, sv) = ( |
| 19490 | 19873 | crate::par::Shared::new(y), |
| 19491 | 19874 | crate::par::Shared::new(u), |
| 19492 | 19875 | crate::par::Shared::new(v), |
| 19493 | 19876 | ); |
| 19494 | - let mi_rows = skip_grid.mi_rows; | |
| 19495 | - let cbands: Vec<(usize, usize)> = crate::par::bands(mi_rows.div_ceil(2), crate::par::filter_threads()) | |
| 19877 | + let cbands: Vec<(usize, usize)> = crate::par::bands((hi - lo).div_ceil(2), crate::par::filter_threads()) | |
| 19496 | 19878 | .into_iter() |
| 19497 | - .map(|(a, b)| (a * 2, (b * 2).min(mi_rows))) | |
| 19879 | + .map(|(a, b)| (lo + a * 2, (lo + b * 2).min(hi))) | |
| 19498 | 19880 | .collect(); |
| 19499 | 19881 | crate::par::run_bands(&cbands, fctx, |r_lo, r_hi, fctx| { |
| 19500 | 19882 | // SAFETY: this band writes only the plane rows its own mi rows cover, |
| @@ -19507,12 +19889,12 @@ fn apply_cdef( | ||
| 19507 | 19889 | y, |
| 19508 | 19890 | u, |
| 19509 | 19891 | v, |
| 19510 | - &src_y, | |
| 19511 | - &src_u, | |
| 19512 | - &src_v, | |
| 19892 | + src_y, | |
| 19893 | + src_u, | |
| 19894 | + src_v, | |
| 19513 | 19895 | cdef, |
| 19514 | 19896 | skip_grid, |
| 19515 | - &idx_grid, | |
| 19897 | + idx_grid, | |
| 19516 | 19898 | sb_cols, |
| 19517 | 19899 | coeff_shift, |
| 19518 | 19900 | damping, |
| @@ -19521,6 +19903,53 @@ fn apply_cdef( | ||
| 19521 | 19903 | fctx, |
| 19522 | 19904 | ); |
| 19523 | 19905 | }); |
| 19906 | +} | |
| 19907 | + | |
| 19908 | +fn apply_cdef( | |
| 19909 | + y: &mut PlaneBuf<'static>, | |
| 19910 | + u: &mut PlaneBuf<'static>, | |
| 19911 | + v: &mut PlaneBuf<'static>, | |
| 19912 | + cdef: &CdefParams, | |
| 19913 | + skip_grid: &Neighbours, | |
| 19914 | + // This frame header's own CROPPED luma size, for the straddling-unit | |
| 19915 | + // counter only -- CDEF itself walks the mi-rounded plane. | |
| 19916 | + (frame_w, frame_h): (usize, usize), fctx: &crate::decode::FrameCtx, | |
| 19917 | +) { | |
| 19918 | + // lane-part32 r2 debug rung, env-gated: see `apply_deblock`'s sibling. | |
| 19919 | + if crate::envflags::env_flag!("EC_AV1_DEBUG_SKIP_CDEF") { | |
| 19920 | + return; | |
| 19921 | + } | |
| 19922 | + cdef_scan_counters(skip_grid); | |
| 19923 | + // `cdef.bits == 0` still means "no filtering at all" whenever the single | |
| 19924 | + // (index 0) strength pair is all-zero -- the fast path every existing | |
| 19925 | + // gate stream takes. A `bits > 0` frame can still be all-zero-strength | |
| 19926 | + // at every index, but that is rare enough not to special-case; the | |
| 19927 | + // per-superblock lookup below degrades to the same index-0 read anyway | |
| 19928 | + // when [`CDEF_IDX_GRID`] was never populated (`bits == 0`). | |
| 19929 | + if cdef_all_zero(cdef) { | |
| 19930 | + return; | |
| 19931 | + } | |
| 19932 | + let sb_cols = fctx.cdef_sb_cols.with(|c| c.get()); | |
| 19933 | + // One entry per superblock (a few hundred at 4K), so this clone is not | |
| 19934 | + // one of the per-frame copies lane-perf6 pooled below. | |
| 19935 | + let idx_grid = fctx.cdef_idx_grid.with(|g| g.borrow().clone()); | |
| 19936 | + let src_y = scratch_from(&y.data); | |
| 19937 | + let src_u = scratch_from(&u.data); | |
| 19938 | + let src_v = scratch_from(&v.data); | |
| 19939 | + // libaom `av1_cdef_filter_fb`: `pri_strength = level << coeff_shift`, | |
| 19940 | + // `sec_strength <<= coeff_shift`, `damping += coeff_shift - (pli != 0)`. | |
| 19941 | + let coeff_shift = i32::from(bit_depth(fctx)) - 8; | |
| 19942 | + let damping = i32::from(cdef.damping) + coeff_shift; | |
| 19943 | + let damping_uv = (damping - 1).max(0); | |
| 19944 | + // lane-filt1: every CDEF unit reads ONLY the pre-CDEF snapshots | |
| 19945 | + // (`src_y`/`src_u`/`src_v`, immutable here) and writes its own 8x8 luma / | |
| 19946 | + // 4x4 chroma square, so units are independent. Bands are mi ROW bands (2 | |
| 19947 | + // mi = 8 luma rows, 4 chroma rows), which makes each band's writes a | |
| 19948 | + // disjoint row range of all three planes. | |
| 19949 | + cdef_rows( | |
| 19950 | + y, u, v, &src_y, &src_u, &src_v, cdef, skip_grid, &idx_grid, sb_cols, coeff_shift, | |
| 19951 | + damping, damping_uv, (frame_w, frame_h), 0, skip_grid.mi_rows, fctx, | |
| 19952 | + ); | |
| 19524 | 19953 | scratch_return(src_y); |
| 19525 | 19954 | scratch_return(src_u); |
| 19526 | 19955 | scratch_return(src_v); |
| @@ -31619,6 +32048,62 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( | ||
| 31619 | 32048 | frame_height, |
| 31620 | 32049 | ); |
| 31621 | 32050 | |
| 32051 | + // lane-pipefilt: the loop filters of superblock row r run as soon as row | |
| 32052 | + // r+1 is reconstructed, on the filter pool, so the frame thread's latency | |
| 32053 | + // is the parse plus the last rows' filters instead of parse plus the | |
| 32054 | + // whole frame's filter chain. See `pipe_run` for the row lags. | |
| 32055 | + let lr_active = (0..3).any(|p| { | |
| 32056 | + lr.frame_restoration_type[p] != ec_av1_syntax::RestorationType::None | |
| 32057 | + && lr.loop_restoration_size[p] != 0 | |
| 32058 | + }); | |
| 32059 | + let pipe_on = pipe_filters_ok(fctx, tile_info.cols, delta.lf_present); | |
| 32060 | + let cdef_on = !cdef_all_zero(cdef); | |
| 32061 | + let snap_on = pipe_on && (cdef_on || lr_active); | |
| 32062 | + let mut pipe_snap = snap_on.then(|| (snap_alloc(&y), snap_alloc(&u), snap_alloc(&v))); | |
| 32063 | + let mut pipe = pipe_on.then(|| { | |
| 32064 | + if wave_stats() { | |
| 32065 | + PIPE_FRAMES.fetch_add(1, Relaxed); | |
| 32066 | + } | |
| 32067 | + let nbands = (mi_rows as usize).div_ceil(16); | |
| 32068 | + let sync = std::sync::Arc::new(PipeSync::new()); | |
| 32069 | + let batch = crate::par::Batch::new("ec-av1-filter"); | |
| 32070 | + // SAFETY (all four): the pipeline only ever touches superblock rows | |
| 32071 | + // the frame thread has finished reconstructing and will not revisit | |
| 32072 | + // -- the row-lag argument in `pipe_run`'s module note, checked | |
| 32073 | + // end to end by the byte-exact gate. | |
| 32074 | + #[allow(unsafe_code)] | |
| 32075 | + let (sy, su, sv, sn) = unsafe { | |
| 32076 | + ( | |
| 32077 | + crate::par::Shared::from_ptr(std::ptr::from_mut(&mut y)), | |
| 32078 | + crate::par::Shared::from_ptr(std::ptr::from_mut(&mut u)), | |
| 32079 | + crate::par::Shared::from_ptr(std::ptr::from_mut(&mut v)), | |
| 32080 | + crate::par::Shared::from_ptr(std::ptr::from_mut(&mut neighbours)), | |
| 32081 | + ) | |
| 32082 | + }; | |
| 32083 | + let cfg = PipeCfg { | |
| 32084 | + lf: loop_filter, | |
| 32085 | + cdef, | |
| 32086 | + idx_grid: fctx.cdef_idx_grid.with(|g| { | |
| 32087 | + let g = g.borrow(); | |
| 32088 | + (g.as_ptr(), g.len()) | |
| 32089 | + }), | |
| 32090 | + sb_cols: fctx.cdef_sb_cols.with(std::cell::Cell::get), | |
| 32091 | + frame_w: frame_width as usize, | |
| 32092 | + frame_h: frame_height as usize, | |
| 32093 | + nbands, | |
| 32094 | + cdef_on, | |
| 32095 | + snap_on, | |
| 32096 | + }; | |
| 32097 | + let ctx = crate::decode::filter_ctx_copy(fctx); | |
| 32098 | + let sync2 = std::sync::Arc::clone(&sync); | |
| 32099 | + let snap = &mut pipe_snap; | |
| 32100 | + batch.submit(move || pipe_run(&sync2, sy, su, sv, sn, cfg, snap, &ctx)); | |
| 32101 | + PipeGuard { sync, batch: Some(batch), nbands } | |
| 32102 | + }); | |
| 32103 | + if !pipe_on && wave_stats() { | |
| 32104 | + PIPE_WHOLE.fetch_add(1, Relaxed); | |
| 32105 | + } | |
| 32106 | + | |
| 31622 | 32107 | // lane-tiles r6: mirrors decode_key_frame_tile_with_cdfs's own per-tile |
| 31623 | 32108 | // loop (spec 5.11.2 decode_tile / 7.20 exit_symbol) -- each tile gets |
| 31624 | 32109 | // its own fresh SymbolDecoder over its own byte range and its own fresh |
| @@ -31743,6 +32228,17 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( | ||
| 31743 | 32228 | flush_recon(fctx, &mut y, &mut u, &mut v); |
| 31744 | 32229 | } |
| 31745 | 32230 | if row_start { |
| 32231 | + if let Some(p) = &pipe { | |
| 32232 | + // Reconstruction complete through this pixel row: the parse | |
| 32233 | + // position itself when reconstruction is inline, the | |
| 32234 | + // wavefront's own leading completed rows otherwise. | |
| 32235 | + let px = if waving { | |
| 32236 | + ((wave_r0 + wave_rows_complete(fctx)) << wave_shift) * 64 | |
| 32237 | + } else { | |
| 32238 | + sb_r as usize * 64 | |
| 32239 | + }; | |
| 32240 | + p.note(px / 64); | |
| 32241 | + } | |
| 31746 | 32242 | neighbours.start_row(); |
| 31747 | 32243 | } |
| 31748 | 32244 | if sb_start { |
| @@ -34123,40 +34619,64 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( | ||
| 34123 | 34619 | } else { |
| 34124 | 34620 | PREFILT_PICTURE_IDX.fetch_add(1, std::sync::atomic::Ordering::SeqCst); |
| 34125 | 34621 | } |
| 34126 | - apply_deblock( | |
| 34127 | - &mut y, | |
| 34128 | - &mut u, | |
| 34129 | - &mut v, | |
| 34130 | - loop_filter, | |
| 34131 | - &neighbours, | |
| 34132 | - frame_width as usize, | |
| 34133 | - frame_height as usize, fctx, | |
| 34134 | - ); | |
| 34622 | + // lane-pipefilt: release the rows still pending and wait the pipeline | |
| 34623 | + // out; from here the picture is deblocked and CDEF-filtered exactly as | |
| 34624 | + // the whole-frame path leaves it. | |
| 34625 | + let pipelined = pipe.is_some(); | |
| 34626 | + if let Some(p) = &mut pipe { | |
| 34627 | + p.finish(); | |
| 34628 | + } | |
| 34629 | + drop(pipe); | |
| 34630 | + if pipelined { | |
| 34631 | + cdef_scan_counters(&neighbours); | |
| 34632 | + } else { | |
| 34633 | + apply_deblock( | |
| 34634 | + &mut y, | |
| 34635 | + &mut u, | |
| 34636 | + &mut v, | |
| 34637 | + loop_filter, | |
| 34638 | + &neighbours, | |
| 34639 | + frame_width as usize, | |
| 34640 | + frame_height as usize, fctx, | |
| 34641 | + ); | |
| 34642 | + } | |
| 34135 | 34643 | // lane-perf4: loop restoration is the only reader of the pre-CDEF planes, |
| 34136 | 34644 | // and a frame that restores nothing copied every plane twice for nothing |
| 34137 | 34645 | // (three `PlaneBuf` clones here plus the `cdef.to_vec()` inside |
| 34138 | 34646 | // [`crate::restoration::apply_loop_restoration_plane`], which returns its |
| 34139 | 34647 | // input unchanged when the plane's type is `None`). Clone only when some |
| 34140 | 34648 | // plane actually restores. |
| 34141 | - let lr_active = (0..3).any(|p| { | |
| 34142 | - lr.frame_restoration_type[p] != ec_av1_syntax::RestorationType::None | |
| 34143 | - && lr.loop_restoration_size[p] != 0 | |
| 34144 | - }); | |
| 34145 | - let deblocked = | |
| 34146 | - lr_active.then(|| (plane_snapshot(&y, 0), plane_snapshot(&u, 1), plane_snapshot(&v, 1))); | |
| 34649 | + let deblocked = if pipelined { | |
| 34650 | + // The pipeline already snapshotted every band on its way past it. | |
| 34651 | + let snap = pipe_snap.take(); | |
| 34652 | + match (lr_active, snap) { | |
| 34653 | + (true, snap) => snap, | |
| 34654 | + (false, Some((a, b, c))) => { | |
| 34655 | + scratch_return(c.data.into_owned()); | |
| 34656 | + scratch_return(b.data.into_owned()); | |
| 34657 | + scratch_return(a.data.into_owned()); | |
| 34658 | + None | |
| 34659 | + } | |
| 34660 | + (false, None) => None, | |
| 34661 | + } | |
| 34662 | + } else { | |
| 34663 | + lr_active.then(|| (plane_snapshot(&y, 0), plane_snapshot(&u, 1), plane_snapshot(&v, 1))) | |
| 34664 | + }; | |
| 34147 | 34665 | // lane-tiny r4: post-deblock / post-CDEF dumps mirroring aomdec's own |
| 34148 | 34666 | // EC_AV1_POSTDEBLOCK_DUMP (decodeframe.c ~5404) -- with the pre-filter |
| 34149 | 34667 | // dump above these bisect WHICH filter stage introduced a mismatch. |
| 34150 | 34668 | dump_stage("EC_AV1_POSTDEBLOCK_DUMP", &y, &u, &v); |
| 34151 | 34669 | dump_stage16("EC_AV1_POSTDEBLOCK_DUMP16", &y, &u, &v, frame_width as usize, frame_height as usize); |
| 34152 | - apply_cdef( | |
| 34153 | - &mut y, | |
| 34154 | - &mut u, | |
| 34155 | - &mut v, | |
| 34156 | - cdef, | |
| 34157 | - &neighbours, | |
| 34158 | - (frame_width as usize, frame_height as usize), fctx, | |
| 34159 | - ); | |
| 34670 | + if !pipelined { | |
| 34671 | + apply_cdef( | |
| 34672 | + &mut y, | |
| 34673 | + &mut u, | |
| 34674 | + &mut v, | |
| 34675 | + cdef, | |
| 34676 | + &neighbours, | |
| 34677 | + (frame_width as usize, frame_height as usize), fctx, | |
| 34678 | + ); | |
| 34679 | + } | |
| 34160 | 34680 | dump_stage("EC_AV1_POSTCDEF_DUMP", &y, &u, &v); |
| 34161 | 34681 | dump_stage16("EC_AV1_POSTCDEF_DUMP16", &y, &u, &v, frame_width as usize, frame_height as usize); |
| 34162 | 34682 | // spec 7.16 (libaom `superres_post_decode`, decodeframe.c: called |
crates/ec-av1/src/par.rsmodified+13 -0
| @@ -101,6 +101,19 @@ impl<'a, T: ?Sized> Shared<'a, T> { | ||
| 101 | 101 | Self(v, std::marker::PhantomData) |
| 102 | 102 | } |
| 103 | 103 | |
| 104 | + /// [`Shared::new`] from a raw pointer, for a caller that keeps using the | |
| 105 | + /// `&mut T` itself (lane-pipefilt: the frame thread parses into the | |
| 106 | + /// planes and the mode-info grids while the filter pipeline reads the | |
| 107 | + /// superblock rows the parse is already three bands past). | |
| 108 | + /// | |
| 109 | + /// # Safety | |
| 110 | + /// `p` must stay valid and unaliased-in-practice for `'a` -- the same | |
| 111 | + /// band-disjointness contract as [`Shared::get`], proved by the caller's | |
| 112 | + /// row-lag arithmetic and the byte-exact gate. | |
| 113 | + pub(crate) unsafe fn from_ptr(p: *mut T) -> Self { | |
| 114 | + Self(p, std::marker::PhantomData) | |
| 115 | + } | |
| 116 | + | |
| 104 | 117 | /// # Safety |
| 105 | 118 | /// The caller must touch only its own band, disjoint from every other |
| 106 | 119 | /// worker's, for as long as the returned reference lives. |