diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index 9886a692..e54cbc99 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -79,6 +79,20 @@ pub(crate) struct FrameCtx { pub(crate) last_frame_wide_margin: std::cell::RefCell>, pub(crate) reach_sb_px: std::cell::Cell, pub(crate) grain_bit_depth: std::cell::Cell, + /// lane-wave1 step (i): this superblock's recorded pixel writes, see + /// [`ReconOp`]. Pushed by parse, replayed by [`flush_recon`]. + pub(crate) recon_ops: std::cell::RefCell>, + /// The queue buffer [`flush_recon`] just drained, kept for its capacity + /// so a superblock's records do not regrow the vector from nothing. + pub(crate) recon_spare: std::cell::RefCell>, + /// lane-wave1 step (ii): the tile decoder's own planes, registered by + /// [`ReconInline`] while reconstruction runs INLINE (one recon thread). + /// `None` while the queue is in use (wavefront workers) and outside a + /// tile. See [`push_op`]. + pub(crate) recon_planes: std::cell::Cell>, + /// lane-wave1 step (ii): the superblock wavefront this tile's recorded + /// writes are handed to, at more than one reconstruction thread. + pub(crate) wave: std::cell::RefCell>>, } impl std::fmt::Debug for FrameCtx { @@ -136,6 +150,10 @@ impl FrameCtx { last_frame_wide_margin: std::cell::RefCell::new(None), reach_sb_px: std::cell::Cell::new(crate::encode::SUPERBLOCK), grain_bit_depth: std::cell::Cell::new(8), + recon_ops: std::cell::RefCell::new(Vec::new()), + recon_spare: std::cell::RefCell::new(Vec::new()), + recon_planes: std::cell::Cell::new(None), + wave: std::cell::RefCell::new(None), } } } @@ -2010,6 +2028,635 @@ fn take_palette_pred(fctx: &crate::decode::FrameCtx) -> Option> { got } +/// The luma window a `UV_CFL_PRED` chroma block averages its AC signal over +/// (lane-wave1 step (i)). Parse records the window; [`flush_recon`] reads the +/// pixels, because at parse time this block's own luma is still queued. +#[derive(Clone, Copy, Debug)] +pub(crate) struct CflSrc { + px: usize, + py: usize, + bw: usize, + bh: usize, + /// Which averager the parse site asked for -- [`cfl_ac_q3_rect`] and + /// [`cfl_ac_q3`] are not the same function at `bw == bh`. + rect: bool, +} + +fn cfl_src(px: usize, py: usize, side: usize) -> CflSrc { + CflSrc { px, py, bw: side, bh: side, rect: false } +} + +fn cfl_src_rect(px: usize, py: usize, bw: usize, bh: usize) -> CflSrc { + CflSrc { px, py, bw, bh, rect: true } +} + +/// lane-wave1 step (i): the pixel-writing half of block decoding, recorded +/// instead of executed. Each variant is the resolved argument tuple of the +/// `PlaneBuf::reconstruct*` call it replaces, so replaying the queue in push +/// order performs exactly the same writes in exactly the same order -- +/// entropy parsing no longer touches the frame's own planes (except the two +/// sites named in [`flush_recon`]'s doc), which is what lets step (ii) run +/// the replay on a superblock-row wavefront worker. +pub(crate) enum ReconOp { + Intra { + plane: usize, + x: usize, + y: usize, + bw: usize, + bh: usize, + /// `true` when the parse site called `reconstruct_rect` (whose + /// `edges_rect` differs from `edges` even at `bw == bh`). + rect: bool, + mode: usize, + angle_delta: i32, + reach: Reach, + residual: Vec, + cfl: Option<(i32, CflSrc)>, + filter_intra: Option, + smooth_neighbor: bool, + /// The palette/intrabc prediction override, taken off + /// [`FrameCtx::palette_pred`] at PUSH time (parse order), never read + /// from that slot by the replay. + pred: Option>, + }, + Mc { + plane: usize, + x: usize, + y: usize, + stride: usize, + w: usize, + h: usize, + prediction: Vec, + residual: Vec, + }, +} + +/// The three planes of the tile currently being decoded, as raw pointers. +/// +/// corner-cut: at one reconstruction thread a recorded write must cost +/// nothing -- deferring it to the end of the superblock is +1.2% +/// instructions and +4.5% wall on 4K, because the residual is written at +/// parse and read back cache-cold at replay. So the recorded op is performed +/// at the push site instead, through the pointers the tile decoder registers +/// once ([`ReconInline`]), which is the same thread, the same order and the +/// same writes as before lane-wave1 -- but the `&mut PlaneBuf` the parse +/// stack still holds aliases them, exactly the [`crate::par::Shared`] +/// trade-off (Miri's stacked borrows would flag both). The upgrade path is +/// threading the three planes down every `read_plane`/`read_inter_plane` +/// call site (~40) so the push site owns the borrow it writes through. +#[derive(Clone, Copy)] +pub(crate) struct ReconPlanes( + *mut PlaneBuf<'static>, + *mut PlaneBuf<'static>, + *mut PlaneBuf<'static>, +); + +// SAFETY: a `FrameCtx` sent to another thread (a frame worker, or +// [`filter_ctx_copy`]) is always one whose guard is not installed, so the +// value travelling is `None`; the pointers never leave the tile decoder's +// own thread. +#[allow(unsafe_code)] +unsafe impl Send for ReconPlanes {} +// SAFETY: the wavefront's workers share one `ReconPlanes` and write provably +// disjoint superblocks of it (see [`WaveState`]) -- the [`crate::par::Shared`] +// contract, one superblock per band instead of one row band. +#[allow(unsafe_code)] +unsafe impl Sync for ReconPlanes {} + +/// Registers the tile's planes for inline reconstruction and unregisters them +/// on every exit from the tile decoder, including the `?` ones -- a stale +/// pointer here would outlive the planes, since [`FrameCtx`] outlives the +/// tile. +pub(crate) struct ReconInline<'a>(&'a crate::decode::FrameCtx); + +#[allow(unsafe_code)] +impl<'a> ReconInline<'a> { + /// Installs the guard for this tile when reconstruction is inline + /// (`EC_AV1_RECON_THREADS == 1`, the default); `None` hands every write + /// to the queue instead, for the wavefront workers to replay. + fn install( + fctx: &'a crate::decode::FrameCtx, + y: &mut PlaneBuf<'static>, + u: &mut PlaneBuf<'static>, + v: &mut PlaneBuf<'static>, + ) -> Option { + if crate::par::recon_threads() != 1 { + return None; + } + // SAFETY: the planes are locals of the tile decoder and outlive the + // guard, which is dropped on every exit from it (including `?`); see + // [`ReconPlanes`] for the aliasing trade-off. + let planes = ReconPlanes( + std::ptr::from_mut(y), + std::ptr::from_mut(u), + std::ptr::from_mut(v), + ); + fctx.recon_planes.with(|c| c.set(Some(planes))); + Some(Self(fctx)) + } +} + +impl Drop for ReconInline<'_> { + fn drop(&mut self) { + self.0.recon_planes.with(|c| c.set(None)); + } +} + +#[allow(unsafe_code)] +fn push_op(fctx: &crate::decode::FrameCtx, op: ReconOp) { + if let Some(ReconPlanes(y, u, v)) = fctx.recon_planes.with(std::cell::Cell::get) { + // SAFETY: see [`ReconPlanes`] -- one thread, and the write is the one + // the parse site would have performed itself before lane-wave1. + unsafe { exec_op(op, &mut *y, &mut *u, &mut *v, fctx) }; + return; + } + fctx.recon_ops.with(|c| c.borrow_mut().push(op)); +} + +/// lane-wave1 step (ii): the superblock-row wavefront a tile's recorded +/// writes are reconstructed on when `EC_AV1_RECON_THREADS > 1`. +/// +/// The parser keeps parsing and never touches the planes; each superblock's +/// drained `Vec` becomes one job, keyed by the superblock's own +/// `(row, column)` inside the tile (128x128 superblocks are one job for all +/// four of their 64x64 visits, since a 128 block writes across quadrants). +/// A worker may start job `(r, c)` once `(r - 1, min(c + 1, last_col))` and +/// `(r, c - 1)` are complete -- the classic wavefront rule, which covers +/// every pixel an intra predictor can read (its edges reach one block right +/// along the row above and one block down the column to the left, both +/// inside those two neighbours) and every CfL AC read (this block's own +/// luma, written by this same job). Jobs are handed out in raster order, so +/// the oldest outstanding one always has its dependencies met: no deadlock. +/// +/// Gate counters incremented inside `reconstruct*`/`intra`/`mc` are +/// `thread_local!` and miss every worker, which is why the default is 1. +pub(crate) struct WaveState { + q: std::sync::Mutex, + cv: std::sync::Condvar, + planes: ReconPlanes, + /// The two [`FrameCtx`] fields reconstruction reads (`sample_max` is + /// `bit_depth`'s; everything else a `reconstruct*` needs is in the op). + bit_depth: u8, + enable_edge_filter: bool, + last_col: usize, +} + +struct WaveQueue { + jobs: std::collections::VecDeque<(usize, usize, Vec)>, + /// Per superblock row, the last column reconstructed (-1 = none yet). + done: Vec, + /// Jobs popped but not finished -- what [`wave_barrier`] waits out. + active: usize, + closed: bool, +} + +#[allow(unsafe_code)] +fn wave_worker(st: &WaveState) { + let fctx = FrameCtx::new(); + fctx.bit_depth.set(st.bit_depth); + fctx.enable_edge_filter.set(st.enable_edge_filter); + loop { + let mut q = st.q.lock().unwrap(); + let (r, c, ops) = loop { + if let Some(job) = q.jobs.pop_front() { + break job; + } + if q.closed { + return; + } + q = st.cv.wait(q).unwrap(); + }; + q.active += 1; + loop { + let above = r == 0 || q.done[r - 1] >= (c + 1).min(st.last_col) as isize; + let left = c == 0 || q.done[r] >= c as isize - 1; + if above && left { + break; + } + q = st.cv.wait(q).unwrap(); + } + drop(q); + let ReconPlanes(y, u, v) = st.planes; + // SAFETY: this job writes only superblock (r, c)'s own pixels, and + // every superblock that could have written or be reading them is + // complete by the wavefront rule above. + unsafe { + for op in ops { + exec_op(op, &mut *y, &mut *u, &mut *v, &fctx); + } + } + let mut q = st.q.lock().unwrap(); + q.done[r] = c as isize; + q.active -= 1; + st.cv.notify_all(); + } +} + +/// Installs the wavefront for one tile and joins its workers on every exit +/// from the tile decoder, including the `?` ones -- the jobs hold raw +/// pointers into the tile's planes, so no worker may outlive them. +pub(crate) struct WaveGuard<'a>(&'a crate::decode::FrameCtx, Vec>); + +#[allow(unsafe_code)] +impl<'a> WaveGuard<'a> { + fn install( + fctx: &'a crate::decode::FrameCtx, + y: &mut PlaneBuf<'static>, + u: &mut PlaneBuf<'static>, + v: &mut PlaneBuf<'static>, + rows: usize, + cols: usize, + ) -> Option { + let threads = crate::par::recon_threads(); + if threads == 1 || rows == 0 || cols == 0 { + return None; + } + let st = std::sync::Arc::new(WaveState { + q: std::sync::Mutex::new(WaveQueue { + jobs: std::collections::VecDeque::new(), + done: vec![-1; rows], + active: 0, + closed: false, + }), + cv: std::sync::Condvar::new(), + // SAFETY: the planes are locals of the tile decoder and outlive + // this guard, which joins every worker in `drop`. + planes: ReconPlanes( + std::ptr::from_mut(y), + std::ptr::from_mut(u), + std::ptr::from_mut(v), + ), + bit_depth: fctx.bit_depth.with(std::cell::Cell::get), + enable_edge_filter: fctx.enable_edge_filter.with(std::cell::Cell::get), + last_col: cols - 1, + }); + let workers = (0..threads) + .map(|_| { + let st = std::sync::Arc::clone(&st); + std::thread::spawn(move || wave_worker(&st)) + }) + .collect(); + fctx.wave.with(|w| *w.borrow_mut() = Some(st)); + Some(Self(fctx, workers)) + } +} + +impl Drop for WaveGuard<'_> { + fn drop(&mut self) { + let st = self.0.wave.with(|w| w.borrow_mut().take()); + if let Some(st) = st { + st.q.lock().unwrap().closed = true; + st.cv.notify_all(); + } + for h in self.1.drain(..) { + let _ = h.join(); + } + } +} + +/// Hands superblock `(r, c)`'s recorded writes to the wavefront. `false` +/// when there is no wavefront (one reconstruction thread), in which case the +/// caller replays them itself. +fn wave_submit(fctx: &crate::decode::FrameCtx, r: usize, c: usize) -> bool { + let st = fctx.wave.with(|w| w.borrow().clone()); + let Some(st) = st else { return false }; + let ops = fctx.recon_ops.with(|c| std::mem::take(&mut *c.borrow_mut())); + if !ops.is_empty() { + st.q.lock().unwrap().jobs.push_back((r, c, ops)); + st.cv.notify_all(); + } + true +} + +/// Waits until every submitted job is reconstructed, so the parser may read +/// the planes itself (the intrabc source fetch and the interintra intra +/// predictor -- see [`flush_recon`]). +fn wave_barrier(fctx: &crate::decode::FrameCtx) { + let st = fctx.wave.with(|w| w.borrow().clone()); + let Some(st) = st else { return }; + let mut q = st.q.lock().unwrap(); + while !q.jobs.is_empty() || q.active > 0 { + q = st.cv.wait(q).unwrap(); + } +} + +/// Performs an intra write at its push site when the tile registered its +/// planes for inline reconstruction ([`ReconPlanes`]); `false` when the +/// caller must record it for the wavefront workers instead. +#[allow(unsafe_code)] +#[allow(clippy::too_many_arguments)] +fn inline_intra( + plane: usize, + x: usize, + oy: usize, + bw: usize, + bh: usize, + rect: bool, + mode: usize, + angle_delta: i32, + reach: Reach, + residual: &[i32], + cfl: Option<(i32, CflSrc)>, + filter_intra: Option, + smooth_neighbor: bool, + fctx: &crate::decode::FrameCtx, +) -> bool { + let Some(ReconPlanes(y, u, v)) = fctx.recon_planes.with(std::cell::Cell::get) else { + return false; + }; + let pred = take_palette_pred(fctx); + // SAFETY: see [`ReconPlanes`]. + unsafe { + exec_intra( + &mut *y, &mut *u, &mut *v, plane, x, oy, bw, bh, rect, mode, angle_delta, reach, + residual, cfl, filter_intra, smooth_neighbor, pred, fctx, + ); + } + true +} + +/// [`inline_intra`] for a motion-compensated write. +#[allow(unsafe_code)] +#[allow(clippy::too_many_arguments)] +fn inline_mc( + plane: usize, + x: usize, + oy: usize, + stride: usize, + w: usize, + h: usize, + prediction: &[u16], + residual: &[i32], + fctx: &crate::decode::FrameCtx, +) -> bool { + let Some(ReconPlanes(y, u, v)) = fctx.recon_planes.with(std::cell::Cell::get) else { + return false; + }; + // SAFETY: see [`ReconPlanes`]. + unsafe { + exec_mc(&mut *y, &mut *u, &mut *v, plane, x, oy, stride, w, h, prediction, residual, fctx); + } + true +} + +/// [`PlaneBuf::reconstruct`], recorded (lane-wave1 step (i)). +#[allow(clippy::too_many_arguments)] +fn push_intra( + plane: usize, + x: usize, + y: usize, + side: usize, + mode: usize, + angle_delta: i32, + reach: Reach, + residual: &[i32], + cfl: Option<(i32, CflSrc)>, + filter_intra: Option, + smooth_neighbor: bool, fctx: &crate::decode::FrameCtx, +) { + if inline_intra( + plane, x, y, side, side, false, mode, angle_delta, reach, residual, cfl, filter_intra, + smooth_neighbor, fctx, + ) { + return; + } + let pred = take_palette_pred(fctx); + push_op(fctx, ReconOp::Intra { + plane, x, y, bw: side, bh: side, rect: false, mode, angle_delta, reach, + residual: residual.to_vec(), cfl, filter_intra, smooth_neighbor, pred, + }); +} + +/// [`push_intra`] for a caller whose residual is dead after the record -- +/// the three fused coefficient readers, i.e. nearly every recorded write. +#[allow(clippy::too_many_arguments)] +fn push_intra_owned( + plane: usize, + x: usize, + y: usize, + side: usize, + mode: usize, + angle_delta: i32, + reach: Reach, + residual: Vec, + cfl: Option<(i32, CflSrc)>, + filter_intra: Option, + smooth_neighbor: bool, fctx: &crate::decode::FrameCtx, +) { + if inline_intra( + plane, x, y, side, side, false, mode, angle_delta, reach, &residual, cfl, filter_intra, + smooth_neighbor, fctx, + ) { + return; + } + let pred = take_palette_pred(fctx); + push_op(fctx, ReconOp::Intra { + plane, x, y, bw: side, bh: side, rect: false, mode, angle_delta, reach, + residual, cfl, filter_intra, smooth_neighbor, pred, + }); +} + +/// [`push_mc_rect`] with the residual moved in -- see [`push_intra_owned`]. +#[allow(clippy::too_many_arguments)] +fn push_mc_rect_owned( + plane: usize, + x: usize, + y: usize, + stride: usize, + w: usize, + h: usize, + prediction: &[u16], + residual: Vec, fctx: &crate::decode::FrameCtx, +) { + if inline_mc(plane, x, y, stride, w, h, prediction, &residual, fctx) { + return; + } + push_op(fctx, ReconOp::Mc { + plane, x, y, stride, w, h, prediction: prediction.to_vec(), residual, + }); +} + +/// [`PlaneBuf::reconstruct_rect`], recorded (lane-wave1 step (i)). +#[allow(clippy::too_many_arguments)] +fn push_intra_rect( + plane: usize, + x: usize, + y: usize, + bw: usize, + bh: usize, + mode: usize, + angle_delta: i32, + reach: Reach, + residual: &[i32], + cfl: Option<(i32, CflSrc)>, + filter_intra: Option, + smooth_neighbor: bool, fctx: &crate::decode::FrameCtx, +) { + if inline_intra( + plane, x, y, bw, bh, true, mode, angle_delta, reach, residual, cfl, filter_intra, + smooth_neighbor, fctx, + ) { + return; + } + let pred = take_palette_pred(fctx); + push_op(fctx, ReconOp::Intra { + plane, x, y, bw, bh, rect: true, mode, angle_delta, reach, + residual: residual.to_vec(), cfl, filter_intra, smooth_neighbor, pred, + }); +} + +/// [`PlaneBuf::reconstruct_mc_rect`] at `stride == w == h == side`, recorded. +fn push_mc( + plane: usize, + x: usize, + y: usize, + side: usize, + prediction: &[u16], + residual: &[i32], fctx: &crate::decode::FrameCtx, +) { + push_mc_rect(plane, x, y, side, side, side, prediction, residual, fctx); +} + +/// [`PlaneBuf::reconstruct_mc_rect`], recorded (lane-wave1 step (i)). +#[allow(clippy::too_many_arguments)] +fn push_mc_rect( + plane: usize, + x: usize, + y: usize, + stride: usize, + w: usize, + h: usize, + prediction: &[u16], + residual: &[i32], fctx: &crate::decode::FrameCtx, +) { + if inline_mc(plane, x, y, stride, w, h, prediction, residual, fctx) { + return; + } + push_op(fctx, ReconOp::Mc { + plane, x, y, stride, w, h, + prediction: prediction.to_vec(), residual: residual.to_vec(), + }); +} + +/// Replays every recorded [`ReconOp`] into the frame's planes, in push order, +/// and empties the queue (lane-wave1 step (i)). +/// +/// Called at the top of each superblock visit and at the end of each tile, so +/// a whole superblock's parsing runs with no plane access at all; the two +/// parse-time pixel READS that survive -- the intrabc source fetch and the +/// interintra intra predictor -- flush ahead of themselves instead, which +/// keeps them byte-exact and names exactly what step (ii) still has to move. +fn flush_recon( + fctx: &crate::decode::FrameCtx, + y: &mut PlaneBuf<'static>, + u: &mut PlaneBuf<'static>, + v: &mut PlaneBuf<'static>, +) { + // lane-wave1 step (ii): a parse-time pixel READ needs every worker's + // writes in place, not just this superblock's queue. + wave_barrier(fctx); + let mut ops = fctx.recon_spare.with(|c| std::mem::take(&mut *c.borrow_mut())); + fctx.recon_ops.with(|c| std::mem::swap(&mut *c.borrow_mut(), &mut ops)); + for op in ops.drain(..) { + exec_op(op, y, u, v, fctx); + } + fctx.recon_spare.with(|c| *c.borrow_mut() = ops); +} + +/// Performs one recorded write: the body [`flush_recon`] replays, and the +/// body [`push_op`] runs immediately when reconstruction is inline. +fn exec_op( + op: ReconOp, + y: &mut PlaneBuf<'static>, + u: &mut PlaneBuf<'static>, + v: &mut PlaneBuf<'static>, + fctx: &crate::decode::FrameCtx, +) { + match op { + ReconOp::Intra { + plane, x, y: oy, bw, bh, rect, mode, angle_delta, reach, residual, cfl, + filter_intra, smooth_neighbor, pred, + } => exec_intra( + y, u, v, plane, x, oy, bw, bh, rect, mode, angle_delta, reach, &residual, cfl, + filter_intra, smooth_neighbor, pred, fctx, + ), + ReconOp::Mc { plane, x, y: oy, stride, w, h, prediction, residual } => { + exec_mc(y, u, v, plane, x, oy, stride, w, h, &prediction, &residual, fctx); + } + } +} + +/// [`ReconOp::Intra`] performed. Its arguments are borrowed, so the inline +/// path ([`push_op`]'s registered planes) reaches it without ever building +/// the record -- which is what keeps one reconstruction thread at the cost of +/// the pre-lane-wave1 decoder. +#[allow(clippy::too_many_arguments)] +fn exec_intra( + y: &mut PlaneBuf<'static>, + u: &mut PlaneBuf<'static>, + v: &mut PlaneBuf<'static>, + plane: usize, + x: usize, + oy: usize, + bw: usize, + bh: usize, + rect: bool, + mode: usize, + angle_delta: i32, + reach: Reach, + residual: &[i32], + cfl: Option<(i32, CflSrc)>, + filter_intra: Option, + smooth_neighbor: bool, + pred: Option>, + fctx: &crate::decode::FrameCtx, +) { + // The CfL AC signal is read HERE, off the luma that has already been + // written for this block -- at parse time it would have been the previous + // frame's samples. + let ac = cfl.map(|(alpha, src)| { + let sig = if src.rect { + cfl_ac_q3_rect(&*y, src.px, src.py, src.bw, src.bh) + } else { + cfl_ac_q3(&*y, src.px, src.py, src.bw) + }; + (alpha, sig) + }); + let cfl = ac.as_ref().map(|(a, sig)| (*a, sig.as_slice())); + let target: &mut PlaneBuf<'static> = match plane { 0 => &mut *y, 1 => &mut *u, _ => &mut *v }; + if rect { + target.reconstruct_rect( + x, oy, bw, bh, mode, angle_delta, reach, residual, cfl, filter_intra, + smooth_neighbor, pred, fctx, + ); + } else { + target.reconstruct( + x, oy, bw, mode, angle_delta, reach, residual, cfl, filter_intra, + smooth_neighbor, pred, fctx, + ); + } +} + +/// [`ReconOp::Mc`] performed -- see [`exec_intra`]. +#[allow(clippy::too_many_arguments)] +fn exec_mc( + y: &mut PlaneBuf<'static>, + u: &mut PlaneBuf<'static>, + v: &mut PlaneBuf<'static>, + plane: usize, + x: usize, + oy: usize, + stride: usize, + w: usize, + h: usize, + prediction: &[u16], + residual: &[i32], + fctx: &crate::decode::FrameCtx, +) { + let target: &mut PlaneBuf<'static> = match plane { 0 => &mut *y, 1 => &mut *u, _ => &mut *v }; + target.reconstruct_mc_rect(x, oy, stride, w, h, prediction, residual, fctx); +} + thread_local! { /// The current frame's `mi` grid, kept only while a frame header set /// `allow_intrabc` -- an intrabc block's DV predictor is the ordinary @@ -7693,7 +8340,7 @@ fn decode_rect_split( set_palette_pred(tu_buf, fctx); } if m.skip { - y.reconstruct_rect( + push_intra_rect(0, tu_px, tu_py, tx_w, @@ -7751,7 +8398,7 @@ fn decode_rect_split( plane_q_delta(0, fctx).1, tu_tx_type, ); - y.reconstruct_rect( + push_intra_rect(0, tu_px, tu_py, tx_w, @@ -7807,7 +8454,7 @@ fn decode_rect_split( plane_q_delta(0, fctx).1, tu_tx_type, ); - y.reconstruct_rect( + push_intra_rect(0, tu_px, tu_py, tx_w, @@ -7854,23 +8501,23 @@ fn decode_rect_split( } } // CHROMA: one un-split rect transform for the whole strip. - let ac = m.alpha.map(|_| cfl_ac_q3_rect(y, px, py, bw, bh)); + let ac = m.alpha.map(|_| cfl_src_rect(px, py, bw, bh)); let reach = block_reach; let (u_grid, v_grid) = if m.skip { let zero = vec![0i32; chroma_w * chroma_h]; if let Some((ub, _)) = &m.palette_uv { set_palette_pred(ub.clone(), fctx); } - u.reconstruct_rect( + push_intra_rect(1, cpx, cpy, chroma_w, chroma_h, m.uv_predict_mode, m.angle_delta_uv, reach, &zero, - m.alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), None, m.smooth_neighbor_uv, fctx, + m.alpha.zip(ac).map(|((au, _), ac)| (au, ac)), None, m.smooth_neighbor_uv, fctx, ); if let Some((_, vb)) = &m.palette_uv { set_palette_pred(vb.clone(), fctx); } - v.reconstruct_rect( + push_intra_rect(2, cpx, cpy, chroma_w, chroma_h, m.uv_predict_mode, m.angle_delta_uv, reach, &zero, - m.alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), None, m.smooth_neighbor_uv, fctx, + m.alpha.zip(ac).map(|((_, av), ac)| (av, ac)), None, m.smooth_neighbor_uv, fctx, ); (zero.clone(), zero) } else { @@ -7918,14 +8565,14 @@ fn decode_rect_split( ); let cfl = m .alpha - .zip(ac.as_deref()) + .zip(ac) .map(|((au, av), ac)| (if plane_idx == 1 { au } else { av }, ac)); - let plane = if plane_idx == 1 { &mut *u } else { &mut *v }; + let _plane = if plane_idx == 1 { &mut *u } else { &mut *v }; if let Some((ub, vb)) = &m.palette_uv { let buf = if plane_idx == 1 { ub } else { vb }; set_palette_pred(buf.clone(), fctx); } - plane.reconstruct_rect( + push_intra_rect(plane_idx, cpx, cpy, chroma_w, @@ -8888,7 +9535,7 @@ fn decode_block_rect( ) }); if skip { - y.reconstruct_rect( + push_intra_rect(0, px, py, bw, @@ -8901,11 +9548,11 @@ fn decode_block_rect( filter_intra, smooth_neighbor, fctx, ); - let ac = alpha.map(|_| cfl_ac_q3_rect(y, px, py, bw, bh)); + let ac = alpha.map(|_| cfl_src_rect(px, py, bw, bh)); if let Some((ub, _)) = &palette_uv_bufs { set_palette_pred(ub.clone(), fctx); } - u.reconstruct_rect( + push_intra_rect(1, cpx, cpy, chroma_w, @@ -8914,14 +9561,14 @@ fn decode_block_rect( angle_delta_uv, reach, &vec![0i32; chroma_w * chroma_h], - alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), + alpha.zip(ac).map(|((au, _), ac)| (au, ac)), None, smooth_neighbor_uv, fctx, ); if let Some((_, vb)) = &palette_uv_bufs { set_palette_pred(vb.clone(), fctx); } - v.reconstruct_rect( + push_intra_rect(2, cpx, cpy, chroma_w, @@ -8930,7 +9577,7 @@ fn decode_block_rect( angle_delta_uv, reach, &vec![0i32; chroma_w * chroma_h], - alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), + alpha.zip(ac).map(|((_, av), ac)| (av, ac)), None, smooth_neighbor_uv, fctx, ); @@ -9018,7 +9665,7 @@ fn decode_block_rect( plane_q_delta(0, fctx).1, luma_tx_type, ); - y.reconstruct_rect( + push_intra_rect(0, px, py, bw, @@ -9031,7 +9678,7 @@ fn decode_block_rect( filter_intra, smooth_neighbor, fctx, ); - let ac = alpha.map(|_| cfl_ac_q3_rect(y, px, py, bw, bh)); + let ac = alpha.map(|_| cfl_src_rect(px, py, bw, bh)); if let Some((ub, _)) = &palette_uv_bufs { set_palette_pred(ub.clone(), fctx); } @@ -9062,7 +9709,7 @@ fn decode_block_rect( plane_q_delta(1, fctx).1, u_tx_type, ); - u.reconstruct_rect( + push_intra_rect(1, cpx, cpy, chroma_w, @@ -9071,7 +9718,7 @@ fn decode_block_rect( angle_delta_uv, reach, &u_residual, - alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), + alpha.zip(ac).map(|((au, _), ac)| (au, ac)), None, smooth_neighbor_uv, fctx, ); @@ -9105,7 +9752,7 @@ fn decode_block_rect( plane_q_delta(2, fctx).1, v_tx_type, ); - v.reconstruct_rect( + push_intra_rect(2, cpx, cpy, chroma_w, @@ -9114,7 +9761,7 @@ fn decode_block_rect( angle_delta_uv, reach, &v_residual, - alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), + alpha.zip(ac).map(|((_, av), ac)| (av, ac)), None, smooth_neighbor_uv, fctx, ); @@ -9359,24 +10006,24 @@ fn decode_leaf_rect( if let Some(b) = &palette_y_buf { set_palette_pred(b.clone(), fctx); } - y.reconstruct_rect( + push_intra_rect(0, px, py, bw, bh, mode, angle_delta_y, reach, &luma_levels, None, filter_intra, smooth_neighbor, fctx, ); - let ac = alpha.map(|_| cfl_ac_q3_rect(y, px, py, bw, bh)); + let ac = alpha.map(|_| cfl_src_rect(px, py, bw, bh)); if let Some((ub, _)) = &palette_uv_bufs { set_palette_pred(ub.clone(), fctx); } - u.reconstruct_rect( + push_intra_rect(1, cpx, cpy, chroma_w, chroma_h, uv_predict_mode, angle_delta_uv, reach, &u_levels, - alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), None, smooth_neighbor_uv, fctx, + alpha.zip(ac).map(|((au, _), ac)| (au, ac)), None, smooth_neighbor_uv, fctx, ); if let Some((_, vb)) = &palette_uv_bufs { set_palette_pred(vb.clone(), fctx); } - v.reconstruct_rect( + push_intra_rect(2, cpx, cpy, chroma_w, chroma_h, uv_predict_mode, angle_delta_uv, reach, &v_levels, - alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), None, smooth_neighbor_uv, fctx, + alpha.zip(ac).map(|((_, av), ac)| (av, ac)), None, smooth_neighbor_uv, fctx, ); } else { // lane-rectx: a genuine 16x8/8x16 luma transform (`TxbSet::LumaRect16x8`, @@ -9412,11 +10059,11 @@ fn decode_leaf_rect( if let Some(b) = &palette_y_buf { set_palette_pred(b.clone(), fctx); } - y.reconstruct_rect( + push_intra_rect(0, px, py, bw, bh, mode, angle_delta_y, reach, &luma_residual, None, filter_intra, smooth_neighbor, fctx, ); - let ac = alpha.map(|_| cfl_ac_q3_rect(y, px, py, bw, bh)); + let ac = alpha.map(|_| cfl_src_rect(px, py, bw, bh)); let u_default_tx = default_intra_tx_type(uv_predict_mode as u8); let u_skip_ctx = usize::from(around[1].0) + usize::from(around[1].1); let mut u_coding = cdfs.txb(TxbSet::ChromaRect8x4, uv_predict_mode); @@ -9432,9 +10079,9 @@ fn decode_leaf_rect( if let Some((ub, _)) = &palette_uv_bufs { set_palette_pred(ub.clone(), fctx); } - u.reconstruct_rect( + push_intra_rect(1, cpx, cpy, chroma_w, chroma_h, uv_predict_mode, angle_delta_uv, reach, &u_residual, - alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), None, smooth_neighbor_uv, fctx, + alpha.zip(ac).map(|((au, _), ac)| (au, ac)), None, smooth_neighbor_uv, fctx, ); let v_default_tx = default_intra_tx_type(uv_predict_mode as u8); let v_skip_ctx = usize::from(around[2].0) + usize::from(around[2].1); @@ -9451,9 +10098,9 @@ fn decode_leaf_rect( if let Some((_, vb)) = &palette_uv_bufs { set_palette_pred(vb.clone(), fctx); } - v.reconstruct_rect( + push_intra_rect(2, cpx, cpy, chroma_w, chroma_h, uv_predict_mode, angle_delta_uv, reach, &v_residual, - alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), None, smooth_neighbor_uv, fctx, + alpha.zip(ac).map(|((_, av), ac)| (av, ac)), None, smooth_neighbor_uv, fctx, ); RECT_LEAF_COEFF_HITS.with(|c| c.set(c.get() + 1)); } @@ -9798,7 +10445,7 @@ fn decode_block_rect4( if let Some(b) = &palette_y_buf { set_palette_pred(b.clone(), fctx); } - y.reconstruct_rect( + push_intra_rect(0, px, py, bw, @@ -9811,8 +10458,8 @@ fn decode_block_rect4( filter_intra, smooth_neighbor, fctx, ); - let ac = alpha.map(|_| cfl_ac_q3_rect(y, px, py, bw, bh)); - for (plane, buf, levels, tx_type) in [ + let ac = alpha.map(|_| cfl_src_rect(px, py, bw, bh)); + for (plane, _buf, levels, tx_type) in [ (1usize, &mut *u, &u_levels, u_tx_type), (2, &mut *v, &v_levels, v_tx_type), ] { @@ -9829,7 +10476,7 @@ fn decode_block_rect4( if let Some((ub, vb)) = &palette_uv_bufs { set_palette_pred(if plane == 1 { ub.clone() } else { vb.clone() }, fctx); } - buf.reconstruct_rect( + push_intra_rect(plane, cpx, cpy, chroma_w, @@ -9839,7 +10486,7 @@ fn decode_block_rect4( reach, &residual, alpha - .zip(ac.as_deref()) + .zip(ac) .map(|((au, av), ac)| (if plane == 1 { au } else { av }, ac)), None, smooth_neighbor_uv, fctx, @@ -9874,7 +10521,7 @@ fn decode_block_rect4( if let Some(b) = &palette_y_buf { set_palette_pred(b.clone(), fctx); } - y.reconstruct_rect( + push_intra_rect(0, px, py, bw, @@ -9887,11 +10534,11 @@ fn decode_block_rect4( filter_intra, smooth_neighbor, fctx, ); - let ac = alpha.map(|_| cfl_ac_q3_rect(y, px, py, bw, bh)); + let ac = alpha.map(|_| cfl_src_rect(px, py, bw, bh)); if let Some((ub, _)) = &palette_uv_bufs { set_palette_pred(ub.clone(), fctx); } - u.reconstruct_rect( + push_intra_rect(1, cpx, cpy, chroma_w, @@ -9900,14 +10547,14 @@ fn decode_block_rect4( angle_delta_uv, reach, &u_levels, - alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), + alpha.zip(ac).map(|((au, _), ac)| (au, ac)), None, smooth_neighbor_uv, fctx, ); if let Some((_, vb)) = &palette_uv_bufs { set_palette_pred(vb.clone(), fctx); } - v.reconstruct_rect( + push_intra_rect(2, cpx, cpy, chroma_w, @@ -9916,7 +10563,7 @@ fn decode_block_rect4( angle_delta_uv, reach, &v_levels, - alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), + alpha.zip(ac).map(|((_, av), ac)| (av, ac)), None, smooth_neighbor_uv, fctx, ); @@ -10200,7 +10847,7 @@ fn decode_rect4_16_strip( set_palette_pred(tu_buf, fctx); } if skip { - y.reconstruct_rect( + push_intra_rect(0, tu_px, tu_py, tx_w, tx_h, mode, angle_delta_y, tu_reach, &zero_tu, None, filter_intra, smooth_neighbor, fctx, ); @@ -10239,7 +10886,7 @@ fn decode_rect4_16_strip( plane_q_delta(0, fctx).1, tu_tx_type, ); - y.reconstruct_rect( + push_intra_rect(0, tu_px, tu_py, tx_w, tx_h, mode, angle_delta_y, tu_reach, &residual, None, filter_intra, smooth_neighbor, fctx, ); @@ -10285,7 +10932,7 @@ fn decode_rect4_16_strip( if let Some(b) = &palette_y_buf { set_palette_pred(b.clone(), fctx); } - y.reconstruct_rect( + push_intra_rect(0, px, py, bw, bh, mode, angle_delta_y, reach, &zeros, None, filter_intra, smooth_neighbor, fctx, ); @@ -10324,7 +10971,7 @@ fn decode_rect4_16_strip( if let Some(b) = &palette_y_buf { set_palette_pred(b.clone(), fctx); } - y.reconstruct_rect( + push_intra_rect(0, px, py, bw, bh, mode, angle_delta_y, reach, &residual, None, filter_intra, smooth_neighbor, fctx, ); @@ -10388,15 +11035,15 @@ fn decode_rect4_16_strip( // `mbmi->skip_txfm`, so a skipped `UV_CFL_PRED` block is still // DC + alpha * luma-AC. Dropping the CfL term here left a // chroma-only bounded box on ReelC's key frames. - let ac = alpha.map(|_| cfl_ac_q3_rect(y, ppx, ppy, pw, ph)); + let ac = alpha.map(|_| cfl_src_rect(ppx, ppy, pw, ph)); if alpha.is_some() { CHROMA_SKIP_CFL_HITS.with(|h| h.set(h.get() + 1)); } - for (buf, plane) in [(&mut *u, 1usize), (&mut *v, 2)] { + for (_buf, plane) in [(&mut *u, 1usize), (&mut *v, 2)] { if let Some((ub, vb)) = &palette_uv_bufs { set_palette_pred(if plane == 1 { ub.clone() } else { vb.clone() }, fctx); } - buf.reconstruct_rect( + push_intra_rect(plane, cpx, cpy, cw, @@ -10406,7 +11053,7 @@ fn decode_rect4_16_strip( pair_reach, &zeros, alpha - .zip(ac.as_deref()) + .zip(ac) .map(|((au, av), ac)| (if plane == 1 { au } else { av }, ac)), None, smooth_neighbor_uv, fctx, @@ -10436,8 +11083,8 @@ fn decode_rect4_16_strip( let (u_levels, u_tx_type) = planes.pop().expect("plane 1"); // CfL averages the luma of the WHOLE pair, the region this one // chroma transform covers. - let ac = alpha.map(|_| cfl_ac_q3_rect(y, ppx, ppy, pw, ph)); - for (plane, buf, levels, tx_type) in [ + let ac = alpha.map(|_| cfl_src_rect(ppx, ppy, pw, ph)); + for (plane, _buf, levels, tx_type) in [ (1usize, &mut *u, &u_levels, u_tx_type), (2, &mut *v, &v_levels, v_tx_type), ] { @@ -10454,7 +11101,7 @@ fn decode_rect4_16_strip( if let Some((ub, vb)) = &palette_uv_bufs { set_palette_pred(if plane == 1 { ub.clone() } else { vb.clone() }, fctx); } - buf.reconstruct_rect( + push_intra_rect(plane, cpx, cpy, cw, @@ -10464,7 +11111,7 @@ fn decode_rect4_16_strip( pair_reach, &residual, alpha - .zip(ac.as_deref()) + .zip(ac) .map(|((au, av), ac)| (if plane == 1 { au } else { av }, ac)), None, smooth_neighbor_uv, fctx, @@ -10757,7 +11404,7 @@ fn decode_block_rect64( ) }); if skip { - y.reconstruct_rect( + push_intra_rect(0, px, py, bw, @@ -10770,11 +11417,11 @@ fn decode_block_rect64( filter_intra, smooth_neighbor, fctx, ); - let ac = alpha.map(|_| cfl_ac_q3_rect(y, px, py, bw, bh)); + let ac = alpha.map(|_| cfl_src_rect(px, py, bw, bh)); if let Some((ub, _)) = &palette_uv_bufs { set_palette_pred(ub.clone(), fctx); } - u.reconstruct_rect( + push_intra_rect(1, cpx, cpy, chroma_w, @@ -10783,14 +11430,14 @@ fn decode_block_rect64( angle_delta_uv, reach, &vec![0i32; chroma_w * chroma_h], - alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), + alpha.zip(ac).map(|((au, _), ac)| (au, ac)), None, smooth_neighbor_uv, fctx, ); if let Some((_, vb)) = &palette_uv_bufs { set_palette_pred(vb.clone(), fctx); } - v.reconstruct_rect( + push_intra_rect(2, cpx, cpy, chroma_w, @@ -10799,7 +11446,7 @@ fn decode_block_rect64( angle_delta_uv, reach, &vec![0i32; chroma_w * chroma_h], - alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), + alpha.zip(ac).map(|((_, av), ac)| (av, ac)), None, smooth_neighbor_uv, fctx, ); @@ -10896,7 +11543,7 @@ fn decode_block_rect64( plane_q_delta(0, fctx).1, luma_tx_type, ); - y.reconstruct_rect( + push_intra_rect(0, px, py, bw, @@ -10925,7 +11572,7 @@ fn decode_block_rect64( } // CHROMA: a real, untruncated chroma_w x chroma_h transform -- no // corner crop needed (see doc comment). - let ac = alpha.map(|_| cfl_ac_q3_rect(y, px, py, bw, bh)); + let ac = alpha.map(|_| cfl_src_rect(px, py, bw, bh)); // The chroma plane is never truncated (both axes <= 32 after // subsampling). Under a 1:4 strip it is a true 32x8/8x32: // `get_txsize_entropy_ctx(TX_32X8)` = (TX_8X8 + TX_32X32 + 1) >> 1 = @@ -10989,7 +11636,7 @@ fn decode_block_rect64( if let Some((ub, _)) = &palette_uv_bufs { set_palette_pred(ub.clone(), fctx); } - u.reconstruct_rect( + push_intra_rect(1, cpx, cpy, chroma_w, @@ -10998,7 +11645,7 @@ fn decode_block_rect64( angle_delta_uv, reach, &u_residual, - alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), + alpha.zip(ac).map(|((au, _), ac)| (au, ac)), None, smooth_neighbor_uv, fctx, ); @@ -11046,7 +11693,7 @@ fn decode_block_rect64( if let Some((_, vb)) = &palette_uv_bufs { set_palette_pred(vb.clone(), fctx); } - v.reconstruct_rect( + push_intra_rect(2, cpx, cpy, chroma_w, @@ -11055,7 +11702,7 @@ fn decode_block_rect64( angle_delta_uv, reach, &v_residual, - alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), + alpha.zip(ac).map(|((_, av), ac)| (av, ac)), None, smooth_neighbor_uv, fctx, ); @@ -11719,12 +12366,16 @@ impl PlaneBuf<'_> { residual: &[i32], cfl: Option<(i32, &[i32])>, filter_intra: Option, - smooth_neighbor: bool, fctx: &crate::decode::FrameCtx, + smooth_neighbor: bool, + // lane-wave1 step (i): the palette/intrabc prediction override, taken + // off `FrameCtx::palette_pred` by the PUSH site (parse order) rather + // than by this replay. + pred: Option>, fctx: &crate::decode::FrameCtx, ) { // As [`Self::reconstruct`]'s own [`PALETTE_PRED`] override // (lane-palette2 r4): a palette block's prediction is the // reconstructed colour-index map, needing no edge pixels at all. - let prediction = if let Some(buf) = take_palette_pred(fctx) { + let prediction = if let Some(buf) = pred { buf } else { let (above, left, corner) = self.edges_rect(x, y, bw, bh, reach); @@ -11823,12 +12474,16 @@ impl PlaneBuf<'_> { // callers until a smooth/paeth `uv_mode` can reach this call at all // (still refused in `read_intra_mode`/`read_intra_mode_rect`), which // keeps `false` exact for chroma without this fn needing to know why. - smooth_neighbor: bool, fctx: &crate::decode::FrameCtx, + smooth_neighbor: bool, + // lane-wave1 step (i): the palette/intrabc prediction override, taken + // off `FrameCtx::palette_pred` by the PUSH site (parse order) rather + // than by this replay. + pred: Option>, fctx: &crate::decode::FrameCtx, ) { // A palette block's own [`PALETTE_PRED`] override (lane-palette r2) // takes priority over `predict()`/`predict_filter_intra` entirely -- // palette prediction needs no edge pixels at all. - let prediction = if let Some(buf) = take_palette_pred(fctx) { + let prediction = if let Some(buf) = pred { buf } else { let (above, left, corner) = self.edges(x, y, side, reach); @@ -11922,13 +12577,17 @@ fn read_plane( predict_mode: usize, angle_delta: i32, reach: Reach, - plane: &mut PlaneBuf<'static>, + // lane-wave1 step (i): unread -- this reader records its write + // ([`push_intra`]/[`push_mc`]) instead of performing it; the borrow + // stays in the signature so every call site still proves the plane is + // exclusively held while its coefficients are parsed. + _plane: &mut PlaneBuf<'static>, x: usize, y: usize, side: usize, tx_side: usize, base_q_idx: u8, - cfl: Option<(i32, &[i32])>, + cfl: Option<(i32, CflSrc)>, filter_intra: Option, // Only meaningful for `plane_idx == 0`: `Some` when this transform unit is // smaller than its own block (spec `get_txb_ctx_general`'s @@ -12019,14 +12678,15 @@ fn read_plane( "TRACE dequant plane={plane_idx} base_q_idx={base_q_idx} tx_type={tx_type:?} side={side} levels={levels:?} residual={residual:?}", ); } - plane.reconstruct( + push_intra_owned( + plane_idx, x, y, side, predict_mode, angle_delta, reach, - &residual, + residual, cfl, filter_intra, smooth_neighbor, fctx, @@ -12215,6 +12875,7 @@ fn decode_block( // block vector, with the BILINEAR kernel libaom forces for intrabc -- // luma is a straight copy, chroma lands half-pel whenever the luma DV is // odd, which is exactly what the bilinear taps then interpolate. + flush_recon(fctx, y, u, v); let intrabc_bufs = intrabc_dv.map(|(dv_row, dv_col)| { let mut yb = vec![0u16; side * side]; mc::predict_with_filter( @@ -12333,7 +12994,7 @@ fn decode_block( y.width, y.height, fctx, ); - y.reconstruct( + push_intra(0, tu_px, tu_py, logical_tx, @@ -12348,7 +13009,7 @@ fn decode_block( } } } else { - y.reconstruct( + push_intra(0, px, py, side, @@ -12361,7 +13022,7 @@ fn decode_block( smooth_neighbor, fctx, ); } - let ac = alpha.map(|_| cfl_ac_q3(y, px, py, side)); + let ac = alpha.map(|_| cfl_src(px, py, side)); let cn = (chroma_side / chroma_tx).max(1); for cu_row in 0..cn { for cu_col in 0..cn { @@ -12379,7 +13040,7 @@ fn decode_block( if let Some((ub, _)) = &palette_uv_bufs { set_palette_pred(ub.clone(), fctx); } - u.reconstruct( + push_intra(1, cu_x, cu_y, chroma_tx, @@ -12387,14 +13048,14 @@ fn decode_block( angle_delta_uv, cu_r, &vec![0i32; chroma_tx * chroma_tx], - alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), + alpha.zip(ac).map(|((au, _), ac)| (au, ac)), None, smooth_neighbor_uv, fctx, ); if let Some((_, vb)) = &palette_uv_bufs { set_palette_pred(vb.clone(), fctx); } - v.reconstruct( + push_intra(2, cu_x, cu_y, chroma_tx, @@ -12402,7 +13063,7 @@ fn decode_block( angle_delta_uv, cu_r, &vec![0i32; chroma_tx * chroma_tx], - alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), + alpha.zip(ac).map(|((_, av), ac)| (av, ac)), None, smooth_neighbor_uv, fctx, ); @@ -12444,7 +13105,7 @@ fn decode_block( None, smooth_neighbor, fctx, )?; - let ac = alpha.map(|_| cfl_ac_q3(y, px, py, side)); + let ac = alpha.map(|_| cfl_src(px, py, side)); // Armed for exactly this block's two chroma reads (see // [`INTRABC_CHROMA_TX`]); the luma call above just set // [`LUMA_TX_TYPE`] to the type it coded. @@ -12471,7 +13132,7 @@ fn decode_block( chroma_side, chroma_tx, base_q_idx, - alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), + alpha.zip(ac).map(|((au, _), ac)| (au, ac)), None, None, smooth_neighbor_uv, fctx, @@ -12502,7 +13163,7 @@ fn decode_block( chroma_side, chroma_tx, base_q_idx, - alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), + alpha.zip(ac).map(|((_, av), ac)| (av, ac)), None, None, smooth_neighbor_uv, fctx, @@ -12622,7 +13283,7 @@ fn decode_block( } // CfL is only allowed up to 32x32, i.e. only at `nchunk == 1`, where // this still runs after every luma unit of the block. - let ac = alpha.map(|_| cfl_ac_q3(y, px, py, side)); + let ac = alpha.map(|_| cfl_src(px, py, side)); // The block's uv plane block is `chroma_side` across but its uv // transform is `chroma_tx` (`av1_get_max_uv_txsize` = // `av1_get_adjusted_tx_size(max_txsize_rect_lookup[uv_plane_bsize])`): @@ -12657,7 +13318,7 @@ fn decode_block( dec, cdfs, chroma_set, scans.1, 1, cu_around[1], mode, uv_predict_mode, angle_delta_uv, cu_reach, u, cu_x, cu_y, chroma_tx, chroma_tx, base_q_idx, - alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), + alpha.zip(ac).map(|((au, _), ac)| (au, ac)), None, chroma_ctx_offset, smooth_neighbor_uv, fctx, )?; if let Some((_, vb)) = &palette_uv_bufs { @@ -12667,7 +13328,7 @@ fn decode_block( dec, cdfs, chroma_set, scans.1, 2, cu_around[2], mode, uv_predict_mode, angle_delta_uv, cu_reach, v, cu_x, cu_y, chroma_tx, chroma_tx, base_q_idx, - alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), + alpha.zip(ac).map(|((_, av), ac)| (av, ac)), None, chroma_ctx_offset, smooth_neighbor_uv, fctx, )?; if cn > 1 { @@ -12930,7 +13591,7 @@ fn decode_block_128rect( y.height, fctx, ); if skip { - y.reconstruct( + push_intra(0, tu_px, tu_py, logical_tx, mode, angle_delta_y, tu_r, &zero_tu, None, None, smooth_neighbor, fctx, ); @@ -12987,11 +13648,11 @@ fn decode_block_128rect( ); let (cu_x, cu_y) = (cpx + ch_col * chroma_tx, cpy + ch_row * chroma_tx); if skip { - u.reconstruct( + push_intra(1, cu_x, cu_y, chroma_tx, uv_predict_mode, angle_delta_uv, cu_r, &zero_cu, None, None, smooth_neighbor_uv, fctx, ); - v.reconstruct( + push_intra(2, cu_x, cu_y, chroma_tx, uv_predict_mode, angle_delta_uv, cu_r, &zero_cu, None, None, smooth_neighbor_uv, fctx, ); @@ -13287,6 +13948,7 @@ fn decode_leaf8( // the CURRENT frame's own pre-loop-filter reconstruction (every loop // filter is off whenever `allow_intrabc`, frame.rs:194/210/252/272) at // the full-pel block vector, with the BILINEAR kernel libaom forces. + flush_recon(fctx, y, u, v); let intrabc_bufs = intrabc_dv.map(|(dv_row, dv_col)| { let mut yb = vec![0u16; 8 * 8]; mc::predict_with_filter( @@ -13349,7 +14011,7 @@ fn decode_leaf8( y.width, y.height, fctx, ); - y.reconstruct( + push_intra(0, tu_px, tu_py, 4, @@ -13367,7 +14029,7 @@ fn decode_leaf8( if let Some(buf) = &palette_y_buf { set_palette_pred(buf.clone(), fctx); } - y.reconstruct( + push_intra(0, px, py, 8, @@ -13380,13 +14042,13 @@ fn decode_leaf8( smooth_neighbor, fctx, ); } - let ac = alpha.map(|_| cfl_ac_q3(y, px, py, 8)); + let ac = alpha.map(|_| cfl_src(px, py, 8)); if let Some((_, ub, _)) = &intrabc_bufs { set_palette_pred(ub.clone(), fctx); } else if let Some((ub, _)) = &palette_uv_bufs { set_palette_pred(ub.clone(), fctx); } - u.reconstruct( + push_intra(1, cpx, cpy, 4, @@ -13394,7 +14056,7 @@ fn decode_leaf8( angle_delta_uv, reach, &vec![0i32; 16], - alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), + alpha.zip(ac).map(|((au, _), ac)| (au, ac)), None, smooth_neighbor_uv, fctx, ); @@ -13403,7 +14065,7 @@ fn decode_leaf8( } else if let Some((_, vb)) = &palette_uv_bufs { set_palette_pred(vb.clone(), fctx); } - v.reconstruct( + push_intra(2, cpx, cpy, 4, @@ -13411,7 +14073,7 @@ fn decode_leaf8( angle_delta_uv, reach, &vec![0i32; 16], - alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), + alpha.zip(ac).map(|((_, av), ac)| (av, ac)), None, smooth_neighbor_uv, fctx, ); @@ -13457,7 +14119,7 @@ fn decode_leaf8( if intrabc_dv.is_some() { fctx.intrabc_chroma_tx.with(|c| c.set(Some(fctx.luma_tx_type.with(std::cell::Cell::get)))); } - let ac = alpha.map(|_| cfl_ac_q3(y, px, py, 8)); + let ac = alpha.map(|_| cfl_src(px, py, 8)); if let Some((_, ub, _)) = &intrabc_bufs { set_palette_pred(ub.clone(), fctx); } else if let Some((ub, _)) = &palette_uv_bufs { @@ -13480,7 +14142,7 @@ fn decode_leaf8( 4, TX4, base_q_idx, - alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), + alpha.zip(ac).map(|((au, _), ac)| (au, ac)), None, None, smooth_neighbor_uv, fctx, @@ -13507,7 +14169,7 @@ fn decode_leaf8( 4, TX4, base_q_idx, - alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), + alpha.zip(ac).map(|((_, av), ac)| (av, ac)), None, None, smooth_neighbor_uv, fctx, @@ -13597,7 +14259,7 @@ fn decode_leaf8( neighbours.record_mi_luma(tu_mi, 4, &tu_grid); } } - let ac = alpha.map(|_| cfl_ac_q3(y, px, py, 8)); + let ac = alpha.map(|_| cfl_src(px, py, 8)); let chroma_around = neighbours.around_mi(leaf_mi, 8); if let Some((ub, _)) = &palette_uv_bufs { set_palette_pred(ub.clone(), fctx); @@ -13619,7 +14281,7 @@ fn decode_leaf8( 4, TX4, base_q_idx, - alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), + alpha.zip(ac).map(|((au, _), ac)| (au, ac)), None, None, smooth_neighbor_uv, fctx, @@ -13644,7 +14306,7 @@ fn decode_leaf8( 4, TX4, base_q_idx, - alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), + alpha.zip(ac).map(|((_, av), ac)| (av, ac)), None, None, smooth_neighbor_uv, fctx, @@ -13907,7 +14569,7 @@ fn decode_leaf_split4( let (px, py) = (lmi.1 * MI, lmi.0 * MI); let reach = Reach::of(4, px, py, y.width, y.height, fctx); if skip { - y.reconstruct(px, py, 4, mode, angle_delta_y, reach, &vec![0i32; 16], None, filter_intra, smooth_neighbor, fctx); + push_intra(0, px, py, 4, mode, angle_delta_y, reach, &vec![0i32; 16], None, filter_intra, smooth_neighbor, fctx); neighbours.record_mi_luma(lmi, 4, &vec![0i32; 16]); } else { let tu_around = neighbours.around_mi(lmi, 4)[0]; @@ -13964,26 +14626,26 @@ fn decode_leaf_split4( // lane-reelckf and lane-kf1200 fixed THIS SAME arm independently; the // merge keeps one fix and feeds both lanes' counters so neither of // their gates goes vacuous. - let ac = alpha.map(|_| cfl_ac_q3(y, gpx, gpy, 8)); + let ac = alpha.map(|_| cfl_src(gpx, gpy, 8)); if alpha.is_some() { CHROMA_SKIP_CFL_HITS.with(|h| h.set(h.get() + 1)); SKIP_CFL_HITS.with(|c| c.set(c.get() + 1)); } - u.reconstruct(cpx, cpy, 4, uv_predict_mode, angle_delta_uv, group_reach, &vec![0i32; 16], alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), None, smooth_neighbor_uv, fctx); - v.reconstruct(cpx, cpy, 4, uv_predict_mode, angle_delta_uv, group_reach, &vec![0i32; 16], alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), None, smooth_neighbor_uv, fctx); + push_intra(1, cpx, cpy, 4, uv_predict_mode, angle_delta_uv, group_reach, &vec![0i32; 16], alpha.zip(ac).map(|((au, _), ac)| (au, ac)), None, smooth_neighbor_uv, fctx); + push_intra(2, cpx, cpy, 4, uv_predict_mode, angle_delta_uv, group_reach, &vec![0i32; 16], alpha.zip(ac).map(|((_, av), ac)| (av, ac)), None, smooth_neighbor_uv, fctx); (vec![0i32; 16], vec![0i32; 16]) } else { - let ac = alpha.map(|_| cfl_ac_q3(y, gpx, gpy, 8)); + let ac = alpha.map(|_| cfl_src(gpx, gpy, 8)); let chroma_around = neighbours.around_mi(leaf_mi, 8); let ug = read_plane( dec, cdfs, TxbSet::Chroma4, scan4, 1, chroma_around[1], uv_mode, uv_predict_mode, angle_delta_uv, group_reach, u, cpx, cpy, 4, TX4, base_q_idx, - alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), None, None, smooth_neighbor_uv, fctx, + alpha.zip(ac).map(|((au, _), ac)| (au, ac)), None, None, smooth_neighbor_uv, fctx, )?; let vg = read_plane( dec, cdfs, TxbSet::Chroma4, scan4, 2, chroma_around[2], uv_mode, uv_predict_mode, angle_delta_uv, group_reach, v, cpx, cpy, 4, TX4, base_q_idx, - alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), None, None, smooth_neighbor_uv, fctx, + alpha.zip(ac).map(|((_, av), ac)| (av, ac)), None, None, smooth_neighbor_uv, fctx, )?; (ug, vg) }; @@ -14194,7 +14856,7 @@ fn decode_leaf_rect8( // one's reconstruction, which is NOT what a single 4x8 // prediction of the same mode produces. let zeros = vec![0i32; 16]; - y.reconstruct( + push_intra(0, tu_px, tu_py, 4, mode, angle_delta_y, tu_reach, &zeros, None, filter_intra, smooth_neighbor, fctx, ); @@ -14248,7 +14910,7 @@ fn decode_leaf_rect8( } let grid = if skip { let zeros = vec![0i32; bw * bh]; - y.reconstruct_rect( + push_intra_rect(0, px, py, bw, bh, mode, angle_delta_y, reach, &zeros, None, filter_intra, smooth_neighbor, fctx, ); @@ -14291,7 +14953,7 @@ fn decode_leaf_rect8( plane_q_delta(0, fctx).1, tx_type, ); - y.reconstruct_rect( + push_intra_rect(0, px, py, bw, bh, mode, angle_delta_y, reach, &residual, None, filter_intra, smooth_neighbor, fctx, ); @@ -14336,26 +14998,26 @@ fn decode_leaf_rect8( // lane-reelckf and lane-kf1200 fixed THIS SAME arm independently; the // merge keeps one fix and feeds both lanes' counters so neither of // their gates goes vacuous. - let ac = alpha.map(|_| cfl_ac_q3(y, gpx, gpy, 8)); + let ac = alpha.map(|_| cfl_src(gpx, gpy, 8)); if alpha.is_some() { CHROMA_SKIP_CFL_HITS.with(|h| h.set(h.get() + 1)); SKIP_CFL_HITS.with(|c| c.set(c.get() + 1)); } - u.reconstruct(cpx, cpy, 4, uv_predict_mode, angle_delta_uv, group_reach, &vec![0i32; 16], alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), None, smooth_neighbor_uv, fctx); - v.reconstruct(cpx, cpy, 4, uv_predict_mode, angle_delta_uv, group_reach, &vec![0i32; 16], alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), None, smooth_neighbor_uv, fctx); + push_intra(1, cpx, cpy, 4, uv_predict_mode, angle_delta_uv, group_reach, &vec![0i32; 16], alpha.zip(ac).map(|((au, _), ac)| (au, ac)), None, smooth_neighbor_uv, fctx); + push_intra(2, cpx, cpy, 4, uv_predict_mode, angle_delta_uv, group_reach, &vec![0i32; 16], alpha.zip(ac).map(|((_, av), ac)| (av, ac)), None, smooth_neighbor_uv, fctx); (vec![0i32; 16], vec![0i32; 16]) } else { - let ac = alpha.map(|_| cfl_ac_q3(y, gpx, gpy, 8)); + let ac = alpha.map(|_| cfl_src(gpx, gpy, 8)); let chroma_around = neighbours.around_mi(leaf_mi, 8); let ug = read_plane( dec, cdfs, TxbSet::Chroma4, scan4, 1, chroma_around[1], uv_mode, uv_predict_mode, angle_delta_uv, group_reach, u, cpx, cpy, 4, TX4, base_q_idx, - alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), None, None, smooth_neighbor_uv, fctx, + alpha.zip(ac).map(|((au, _), ac)| (au, ac)), None, None, smooth_neighbor_uv, fctx, )?; let vg = read_plane( dec, cdfs, TxbSet::Chroma4, scan4, 2, chroma_around[2], uv_mode, uv_predict_mode, angle_delta_uv, group_reach, v, cpx, cpy, 4, TX4, base_q_idx, - alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), None, None, smooth_neighbor_uv, fctx, + alpha.zip(ac).map(|((_, av), ac)| (av, ac)), None, None, smooth_neighbor_uv, fctx, )?; (ug, vg) }; @@ -16138,7 +16800,11 @@ fn read_inter_plane_rect( plane_idx: usize, around: (bool, bool, i32), tx_mode: usize, - plane: &mut PlaneBuf<'static>, + // lane-wave1 step (i): unread -- this reader records its write + // ([`push_intra`]/[`push_mc`]) instead of performing it; the borrow + // stays in the signature so every call site still proves the plane is + // exclusively held while its coefficients are parsed. + _plane: &mut PlaneBuf<'static>, x: usize, y: usize, prediction: &[u16], @@ -16230,7 +16896,7 @@ fn read_inter_plane_rect( for row in 0..h { strided[row * stride..][..w].copy_from_slice(&residual[row * w..][..w]); } - plane.reconstruct_mc_rect(x, y, stride, w, h, prediction, &strided, fctx); + push_mc_rect_owned(plane_idx, x, y, stride, w, h, prediction, strided, fctx); Ok((grid, tx_type)) } @@ -18364,7 +19030,39 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( // its one block covers all four 64x64 quadrants, so the remaining three // visits of this superblock code nothing at all. let mut sb128_none = false; + // lane-wave1 step (ii): at one reconstruction thread the recorded writes + // are performed at their push sites (nothing reaches the queue below). + let _recon_inline = ReconInline::install(fctx, &mut y, &mut u, &mut v); + // lane-wave1 step (ii): one job per superblock (per 128x128 superblock + // when `sb128`, since a 128 block writes across its four 64x64 visits). + let wave_shift = u32::from(sb128); + let (wave_r0, wave_c0) = ((sb_r0 >> wave_shift) as usize, (sb_c0 >> wave_shift) as usize); + let (wave_rows, wave_cols) = if sb_r1 > sb_r0 && sb_c1 > sb_c0 { + ( + ((sb_r1 - 1) >> wave_shift) as usize - wave_r0 + 1, + ((sb_c1 - 1) >> wave_shift) as usize - wave_c0 + 1, + ) + } else { + (0, 0) + }; + let _wave = WaveGuard::install(fctx, &mut y, &mut u, &mut v, wave_rows, wave_cols); + let waving = fctx.wave.with(|w| w.borrow().is_some()); + let mut wave_job = (0usize, 0usize); for (sb_r, sb_c, row_start, sb_start) in sb_visit_order(sb_r0, sb_r1, sb_c0, sb_c1, sb128) { + // lane-wave1: the previous superblock's recorded writes -- handed to + // the wavefront workers, or replayed here at one recon thread (where + // the queue is empty, every write having run at its push site). + if waving { + if sb_start { + wave_submit(fctx, wave_job.0, wave_job.1); + wave_job = ( + (sb_r >> wave_shift) as usize - wave_r0, + (sb_c >> wave_shift) as usize - wave_c0, + ); + } + } else { + flush_recon(fctx, &mut y, &mut u, &mut v); + } if row_start { neighbours.start_row(); } @@ -20105,6 +20803,10 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( } } } + if waving { + wave_submit(fctx, wave_job.0, wave_job.1); + } + flush_recon(fctx, &mut y, &mut u, &mut v); if tile_num == tile_info.context_update_tile_id { result_cdfs = cdfs; @@ -20589,18 +21291,7 @@ impl PlaneBuf<'_> { /// compensation's output, rather than [`predict`]'s intra one), writing /// the clamped reconstruction into the plane at `(x, y)` -- the inter /// counterpart of [`PlaneBuf::reconstruct`]. - fn reconstruct_mc( - &mut self, - x: usize, - y: usize, - side: usize, - prediction: &[u16], - residual: &[i32], fctx: &crate::decode::FrameCtx, - ) { - self.reconstruct_mc_rect(x, y, side, side, side, prediction, residual, fctx); - } - - /// [`Self::reconstruct_mc`], writing only a `w`x`h` sub-rectangle of the + /// [`Self::reconstruct_mc_rect`], writing only a `w`x`h` sub-rectangle of the /// `side`x`side` `prediction`/`residual` buffers (which stay `side`-square /// for stride/indexing) -- lane-partitions r1: a true rectangular HORZ/ /// VERT strip is predicted at its enclosing square `side` (matching @@ -20656,7 +21347,11 @@ fn read_inter_plane( plane_idx: usize, around: (bool, bool, i32), tx_mode: usize, - plane: &mut PlaneBuf<'static>, + // lane-wave1 step (i): unread -- this reader records its write + // ([`push_intra`]/[`push_mc`]) instead of performing it; the borrow + // stays in the signature so every call site still proves the plane is + // exclusively held while its coefficients are parsed. + _plane: &mut PlaneBuf<'static>, x: usize, y: usize, side: usize, @@ -20727,7 +21422,7 @@ fn read_inter_plane( }; let (dc_delta, ac_delta) = plane_q_delta(plane_idx, fctx); let residual = dequant_and_inverse_typed(&grid, side, bit_depth(fctx), block_q_idx(fctx), dc_delta, ac_delta, tx_type); - plane.reconstruct_mc(x, y, side, prediction, &residual, fctx); + push_mc_rect_owned(plane_idx, x, y, side, side, side, prediction, residual, fctx); Ok((grid, tx_type)) } @@ -23244,7 +23939,7 @@ fn decode_inter_block( .1 }; if skip { - y.reconstruct_mc_rect( + push_mc_rect(0, px, py, side, @@ -23253,7 +23948,7 @@ fn decode_inter_block( &pred_y, &vec![0i32; side * side], fctx, ); - u.reconstruct_mc_rect( + push_mc_rect(1, cpx, cpy, chroma_side, @@ -23262,7 +23957,7 @@ fn decode_inter_block( &pred_u, &vec![0i32; chroma_side * chroma_side], fctx, ); - v.reconstruct_mc_rect( + push_mc_rect(2, cpx, cpy, chroma_side, @@ -24367,6 +25062,7 @@ fn decode_inter_block( // result, all planes (reconinter.c av1_build_interintra_predictor // plane loop). if let Some(ii) = interintra_mode { + flush_recon(fctx, y, u, v); if write_w != write_h { INTERINTRA_RECT_HITS.with(|c| c.set(c.get() + 1)); } @@ -24454,7 +25150,7 @@ fn decode_inter_block( .1 }; if skip { - y.reconstruct_mc_rect( + push_mc_rect(0, px, py, side, @@ -24464,7 +25160,7 @@ fn decode_inter_block( &vec![0i32; side * side], fctx, ); if has_chroma { - u.reconstruct_mc_rect( + push_mc_rect(1, cpx, cpy, chroma_side, @@ -24473,7 +25169,7 @@ fn decode_inter_block( &pred_u, &vec![0i32; chroma_side * chroma_side], fctx, ); - v.reconstruct_mc_rect( + push_mc_rect(2, cpx, cpy, chroma_side, @@ -25216,7 +25912,7 @@ fn decode_inter_block( set_palette_pred(window, fctx); } let tu_grid = if skip { - y.reconstruct( + push_intra(0, tu_px, tu_py, tx_px, @@ -25286,7 +25982,7 @@ fn decode_inter_block( for plane_idx in 1..3 { let buf: &mut PlaneBuf<'static> = if plane_idx == 1 { &mut *u } else { &mut *v }; let cu_grid = if skip { - buf.reconstruct( + push_intra(plane_idx, cu_x, cu_y, cu_tx, @@ -25356,7 +26052,7 @@ fn decode_inter_block( if let Some(buf) = &palette_y_buf { set_palette_pred(buf.clone(), fctx); } - y.reconstruct( + push_intra(0, px, py, side, @@ -25369,11 +26065,11 @@ fn decode_inter_block( smooth_neighbor, fctx, ); } - let ac = alpha.map(|_| cfl_ac_q3(y, px, py, side)); + let ac = alpha.map(|_| cfl_src(px, py, side)); if let Some((ub, _)) = &palette_uv_bufs { set_palette_pred(ub.clone(), fctx); } - u.reconstruct( + push_intra(1, cpx, cpy, chroma_side, @@ -25381,14 +26077,14 @@ fn decode_inter_block( angle_delta_uv, reach, &vec![0i32; chroma_side * chroma_side], - alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), + alpha.zip(ac).map(|((au, _), ac)| (au, ac)), None, smooth_neighbor_uv, fctx, ); if let Some((_, vb)) = &palette_uv_bufs { set_palette_pred(vb.clone(), fctx); } - v.reconstruct( + push_intra(2, cpx, cpy, chroma_side, @@ -25396,7 +26092,7 @@ fn decode_inter_block( angle_delta_uv, reach, &vec![0i32; chroma_side * chroma_side], - alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), + alpha.zip(ac).map(|((_, av), ac)| (av, ac)), None, smooth_neighbor_uv, fctx, ); @@ -25437,7 +26133,7 @@ fn decode_inter_block( smooth_neighbor, fctx, )? }; - let ac = alpha.map(|_| cfl_ac_q3(y, px, py, side)); + let ac = alpha.map(|_| cfl_src(px, py, side)); if let Some((ub, _)) = &palette_uv_bufs { set_palette_pred(ub.clone(), fctx); } @@ -25458,7 +26154,7 @@ fn decode_inter_block( chroma_side, chroma_tx, base_q_idx, - alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), + alpha.zip(ac).map(|((au, _), ac)| (au, ac)), None, None, smooth_neighbor_uv, fctx, @@ -25483,7 +26179,7 @@ fn decode_inter_block( chroma_side, chroma_tx, base_q_idx, - alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), + alpha.zip(ac).map(|((_, av), ac)| (av, ac)), None, None, smooth_neighbor_uv, fctx, @@ -26094,7 +26790,7 @@ fn decode_inter_sub8_split4( // txsize_to_bsize[tx_size]` branch (`txb_skip_ctx = 0`, which // `read_inter_plane`'s `plane_idx == 0` arm already assumes). if skip { - y.reconstruct_mc(px, py, B4, &pred_y, &vec![0i32; B4 * B4], fctx); + push_mc(0, px, py, B4, &pred_y, &vec![0i32; B4 * B4], fctx); neighbours.record_mi_luma_rect(lmi, B4, B4, &vec![0i32; B4 * B4]); } else { let around = neighbours.around_mi(lmi, B4); @@ -26209,8 +26905,8 @@ fn decode_inter_sub8_split4( } } let (u_grid, v_grid) = if last_skip { - u.reconstruct_mc(cpx, cpy, B4, &pred_u, &vec![0i32; B4 * B4], fctx); - v.reconstruct_mc(cpx, cpy, B4, &pred_v, &vec![0i32; B4 * B4], fctx); + push_mc(1, cpx, cpy, B4, &pred_u, &vec![0i32; B4 * B4], fctx); + push_mc(2, cpx, cpy, B4, &pred_v, &vec![0i32; B4 * B4], fctx); (vec![0i32; B4 * B4], vec![0i32; B4 * B4]) } else { let around = neighbours.around_mi(group_mi, 8); @@ -26439,7 +27135,7 @@ fn decode_intra_sub8_leaf( ); if skip { let zeros = vec![0i32; 16]; - y.reconstruct( + push_intra(0, tu_px, tu_py, 4, mode, angle_delta_y, tu_reach, &zeros, None, filter_intra, smooth_neighbor, fctx, ); @@ -26481,7 +27177,7 @@ fn decode_intra_sub8_leaf( // fixes `txb_skip_ctx` at 0 (`luma_skip_ctx: None`) -- the same square // reader [`decode_leaf_split4`] uses. if skip { - y.reconstruct( + push_intra(0, px, py, 4, mode, angle_delta_y, reach, &vec![0i32; 16], None, filter_intra, smooth_neighbor, fctx, ); @@ -26519,7 +27215,7 @@ fn decode_intra_sub8_leaf( } else { let grid_levels = if skip { let zeros = vec![0i32; bw * bh]; - y.reconstruct_rect( + push_intra_rect(0, px, py, bw, bh, mode, angle_delta_y, reach, &zeros, None, filter_intra, smooth_neighbor, fctx, ); @@ -26554,7 +27250,7 @@ fn decode_intra_sub8_leaf( plane_q_delta(0, fctx).1, tx_type, ); - y.reconstruct_rect( + push_intra_rect(0, px, py, bw, bh, mode, angle_delta_y, reach, &residual, None, filter_intra, smooth_neighbor, fctx, ); @@ -26649,26 +27345,26 @@ fn decode_intra_sub8_leaf( // needs `bsize >= BLOCK_8X8`), so the group always clears. record_strip_palette(neighbours, (gr, gc), 8, 8, 0, [0u16; 8], 0, [0u16; 8]); - let ac = alpha.map(|_| cfl_ac_q3(y, gpx, gpy, 8)); + let ac = alpha.map(|_| cfl_src(gpx, gpy, 8)); let (u_grid, v_grid): (Vec, Vec) = if skip { if alpha.is_some() { CHROMA_SKIP_CFL_HITS.with(|h| h.set(h.get() + 1)); SKIP_CFL_HITS.with(|c| c.set(c.get() + 1)); } - u.reconstruct(cpx, cpy, 4, uv_predict_mode, angle_delta_uv, group_reach, &vec![0i32; 16], alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), None, smooth_neighbor_uv, fctx); - v.reconstruct(cpx, cpy, 4, uv_predict_mode, angle_delta_uv, group_reach, &vec![0i32; 16], alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), None, smooth_neighbor_uv, fctx); + push_intra(1, cpx, cpy, 4, uv_predict_mode, angle_delta_uv, group_reach, &vec![0i32; 16], alpha.zip(ac).map(|((au, _), ac)| (au, ac)), None, smooth_neighbor_uv, fctx); + push_intra(2, cpx, cpy, 4, uv_predict_mode, angle_delta_uv, group_reach, &vec![0i32; 16], alpha.zip(ac).map(|((_, av), ac)| (av, ac)), None, smooth_neighbor_uv, fctx); (vec![0i32; 16], vec![0i32; 16]) } else { let chroma_around = neighbours.around_mi(group_mi, 8); let ug = read_plane( dec, cdfs, TxbSet::Chroma4, scan4, 1, chroma_around[1], uv_mode, uv_predict_mode, angle_delta_uv, group_reach, u, cpx, cpy, 4, TX4, base_q_idx, - alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), None, None, smooth_neighbor_uv, fctx, + alpha.zip(ac).map(|((au, _), ac)| (au, ac)), None, None, smooth_neighbor_uv, fctx, )?; let vg = read_plane( dec, cdfs, TxbSet::Chroma4, scan4, 2, chroma_around[2], uv_mode, uv_predict_mode, angle_delta_uv, group_reach, v, cpx, cpy, 4, TX4, base_q_idx, - alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), None, None, smooth_neighbor_uv, fctx, + alpha.zip(ac).map(|((_, av), ac)| (av, ac)), None, None, smooth_neighbor_uv, fctx, )?; (ug, vg) }; @@ -27067,7 +27763,7 @@ fn decode_inter_sub8_rect2( } } if skip { - y.reconstruct_mc_rect(px, py, SIDE, bw, bh, &pred_y, &vec![0i32; SIDE * SIDE], fctx); + push_mc_rect(0, px, py, SIDE, bw, bh, &pred_y, &vec![0i32; SIDE * SIDE], fctx); neighbours.record_mi_luma_rect(lmi, bw, bh, &vec![0i32; bw * bh]); neighbours.fill_lf_grid_rect(lmi, w_mi, h_mi, bw as u8, bh as u8, ref_frame.max(LAST_FRAME), fctx); } else { @@ -27233,8 +27929,8 @@ fn decode_inter_sub8_rect2( } } let (u_grid, v_grid) = if last_skip { - u.reconstruct_mc(cpx, cpy, B4, &pred_u, &vec![0i32; B4 * B4], fctx); - v.reconstruct_mc(cpx, cpy, B4, &pred_v, &vec![0i32; B4 * B4], fctx); + push_mc(1, cpx, cpy, B4, &pred_u, &vec![0i32; B4 * B4], fctx); + push_mc(2, cpx, cpy, B4, &pred_v, &vec![0i32; B4 * B4], fctx); (vec![0i32; B4 * B4], vec![0i32; B4 * B4]) } else { let around = neighbours.around_mi(group_mi, 8); @@ -28070,15 +28766,15 @@ fn decode_inter_block8( .1 .is_some(); if skip { - y.reconstruct_mc(px, py, SIDE, &pred_y, &vec![0i32; SIDE * SIDE], fctx); - u.reconstruct_mc( + push_mc(0, px, py, SIDE, &pred_y, &vec![0i32; SIDE * SIDE], fctx); + push_mc(1, cpx, cpy, CHROMA_SIDE, &pred_u, &vec![0i32; CHROMA_SIDE * CHROMA_SIDE], fctx, ); - v.reconstruct_mc( + push_mc(2, cpx, cpy, CHROMA_SIDE, @@ -28697,6 +29393,7 @@ fn decode_inter_block8( // Non-wedge interintra blend, mutually exclusive with OBMC (the // motion_mode symbol is not read for an interintra block). if let Some(ii) = interintra_mode { + flush_recon(fctx, y, u, v); interintra_blend(y, px, py, SIDE, SIDE, SIDE, ii, wedge_mask, &mut pred_y, fctx); interintra_blend( u, cpx, cpy, CHROMA_SIDE, CHROMA_SIDE, CHROMA_SIDE, ii, wedge_mask, &mut pred_u, fctx, @@ -28719,15 +29416,15 @@ fn decode_inter_block8( .1 .is_some(); if skip { - y.reconstruct_mc(px, py, SIDE, &pred_y, &vec![0i32; SIDE * SIDE], fctx); - u.reconstruct_mc( + push_mc(0, px, py, SIDE, &pred_y, &vec![0i32; SIDE * SIDE], fctx); + push_mc(1, cpx, cpy, CHROMA_SIDE, &pred_u, &vec![0i32; CHROMA_SIDE * CHROMA_SIDE], fctx, ); - v.reconstruct_mc( + push_mc(2, cpx, cpy, CHROMA_SIDE, @@ -29090,7 +29787,7 @@ fn decode_inter_block8( set_palette_pred(window, fctx); } let tu_grid = if skip { - y.reconstruct( + push_intra(0, tu_px, tu_py, tx_px, @@ -29143,7 +29840,7 @@ fn decode_inter_block8( if let Some(buf) = &palette_y_buf { set_palette_pred(buf.clone(), fctx); } - y.reconstruct( + push_intra(0, px, py, SIDE, @@ -29158,11 +29855,11 @@ fn decode_inter_block8( } // CfL's AC contribution is the luma this leaf just reconstructed, // averaged over the 4x4 chroma block (`cfl_ac_q3`, spec 7.11.5). - let ac = alpha.map(|_| cfl_ac_q3(y, px, py, SIDE)); + let ac = alpha.map(|_| cfl_src(px, py, SIDE)); if let Some((ub, _)) = &palette_uv_bufs { set_palette_pred(ub.clone(), fctx); } - u.reconstruct( + push_intra(1, cpx, cpy, CHROMA_SIDE, @@ -29170,14 +29867,14 @@ fn decode_inter_block8( angle_delta_uv, reach, &vec![0i32; CHROMA_SIDE * CHROMA_SIDE], - alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), + alpha.zip(ac).map(|((au, _), ac)| (au, ac)), None, smooth_neighbor_uv, fctx, ); if let Some((_, vb)) = &palette_uv_bufs { set_palette_pred(vb.clone(), fctx); } - v.reconstruct( + push_intra(2, cpx, cpy, CHROMA_SIDE, @@ -29185,7 +29882,7 @@ fn decode_inter_block8( angle_delta_uv, reach, &vec![0i32; CHROMA_SIDE * CHROMA_SIDE], - alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), + alpha.zip(ac).map(|((_, av), ac)| (av, ac)), None, smooth_neighbor_uv, fctx, ); @@ -29234,7 +29931,7 @@ fn decode_inter_block8( smooth_neighbor, fctx, )? }; - let ac = alpha.map(|_| cfl_ac_q3(y, px, py, SIDE)); + let ac = alpha.map(|_| cfl_src(px, py, SIDE)); if let Some((ub, _)) = &palette_uv_bufs { set_palette_pred(ub.clone(), fctx); } @@ -29255,7 +29952,7 @@ fn decode_inter_block8( CHROMA_SIDE, TX4, base_q_idx, - alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), + alpha.zip(ac).map(|((au, _), ac)| (au, ac)), None, None, smooth_neighbor_uv, fctx, @@ -29280,7 +29977,7 @@ fn decode_inter_block8( CHROMA_SIDE, TX4, base_q_idx, - alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), + alpha.zip(ac).map(|((_, av), ac)| (av, ac)), None, None, smooth_neighbor_uv, fctx, @@ -29818,7 +30515,39 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( // three items of this superblock carry no syntax at all (same skip the // key-frame tile loop runs). let mut sb128_none = false; + // lane-wave1 step (ii): at one reconstruction thread the recorded writes + // are performed at their push sites (nothing reaches the queue below). + let _recon_inline = ReconInline::install(fctx, &mut y, &mut u, &mut v); + // lane-wave1 step (ii): one job per superblock (per 128x128 superblock + // when `sb128`, since a 128 block writes across its four 64x64 visits). + let wave_shift = u32::from(sb128); + let (wave_r0, wave_c0) = ((sb_r0 >> wave_shift) as usize, (sb_c0 >> wave_shift) as usize); + let (wave_rows, wave_cols) = if sb_r1 > sb_r0 && sb_c1 > sb_c0 { + ( + ((sb_r1 - 1) >> wave_shift) as usize - wave_r0 + 1, + ((sb_c1 - 1) >> wave_shift) as usize - wave_c0 + 1, + ) + } else { + (0, 0) + }; + let _wave = WaveGuard::install(fctx, &mut y, &mut u, &mut v, wave_rows, wave_cols); + let waving = fctx.wave.with(|w| w.borrow().is_some()); + let mut wave_job = (0usize, 0usize); for (sb_r, sb_c, row_start, sb_start) in sb_visit_order(sb_r0, sb_r1, sb_c0, sb_c1, sb128) { + // lane-wave1: the previous superblock's recorded writes -- handed to + // the wavefront workers, or replayed here at one recon thread (where + // the queue is empty, every write having run at its push site). + if waving { + if sb_start { + wave_submit(fctx, wave_job.0, wave_job.1); + wave_job = ( + (sb_r >> wave_shift) as usize - wave_r0, + (sb_c >> wave_shift) as usize - wave_c0, + ); + } + } else { + flush_recon(fctx, &mut y, &mut u, &mut v); + } if row_start { neighbours.start_row(); } @@ -32162,6 +32891,10 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs( } } } + if waving { + wave_submit(fctx, wave_job.0, wave_job.1); + } + flush_recon(fctx, &mut y, &mut u, &mut v); if tile_num == tile_info.context_update_tile_id { result_cdfs = cdfs; @@ -33827,6 +34560,10 @@ mod tests { width, fctx, ) .unwrap(); + // lane-wave1: a direct caller of the block decoder RECORDS its writes + // (the tile decoder is what replays them), so this gate replays its + // own before reading the plane back. + flush_recon(fctx, &mut y, &mut u, &mut v); let (px, py) = (c * SUB, r * SUB); let mut want_near = vec![0u16; side * side]; diff --git a/crates/ec-av1/src/par.rs b/crates/ec-av1/src/par.rs index 76419670..f77dde51 100644 --- a/crates/ec-av1/src/par.rs +++ b/crates/ec-av1/src/par.rs @@ -133,3 +133,31 @@ where } } } + +/// `EC_AV1_RECON_THREADS`, the number of worker threads a tile's +/// reconstruction (lane-wave1) is spread across, in superblock-row wavefront +/// order (default 1 = reconstruct inline at the parse site, i.e. exactly the +/// pre-wavefront decoder). Read once per process, same shape as +/// [`filter_threads`]. +static RECON_THREADS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0); + +pub(crate) fn recon_threads() -> usize { + match RECON_THREADS.load(std::sync::atomic::Ordering::Relaxed) { + 0 => { + let n = crate::envflags::var("EC_AV1_RECON_THREADS") + .ok() + .and_then(|v| v.trim().parse::().ok()) + .unwrap_or(1) + .clamp(1, 64); + RECON_THREADS.store(n, std::sync::atomic::Ordering::Relaxed); + n + } + n => n, + } +} + +/// Test-only override of [`recon_threads`] -- see [`set_filter_threads`]. +#[cfg(test)] +pub(crate) fn set_recon_threads(n: usize) { + RECON_THREADS.store(n.clamp(1, 64), std::sync::atomic::Ordering::Relaxed); +} diff --git a/crates/ec-av1/src/stream.rs b/crates/ec-av1/src/stream.rs index 8e932b5b..6cb22e04 100644 --- a/crates/ec-av1/src/stream.rs +++ b/crates/ec-av1/src/stream.rs @@ -6106,6 +6106,50 @@ mod tests { /// `--denoise-noise-level=25` stream whose frames carry film grain /// parameters (small enough that 4 bands is one block row each, which is /// what exercises the grain band's one-row `prelude` replay). + /// lane-wave1 step (ii): the superblock-row wavefront + /// (`EC_AV1_RECON_THREADS`) reconstructs every output frame byte for + /// byte as the inline path does, on a hidden-ARF 10-bit stream (no-show + /// frames, CfL, 128 superblocks) and on the two screen-content streams + /// whose palette/intrabc blocks READ the current frame's pixels while + /// parsing -- the sites that make the workers barrier. + #[test] + fn a_real_stream_reconstructs_identically_with_one_and_four_recon_threads() { + const NAME: &str = "a_real_stream_reconstructs_identically_with_one_and_four_recon_threads"; + let _gate_lock = lock_gate_counters(); + for fixture in [ + "reelb_arf_witness.obu", + "palette_screen_witness.obu", + "palette_screen_witness_10bit.obu", + ] { + let path = std::path::Path::new(env!("CARGO_MANIFEST_DIR")) + .join("fixtures") + .join(fixture); + let stream = std::fs::read(&path) + .unwrap_or_else(|e| panic!("{NAME}: reading {}: {e}", path.display())); + crate::par::set_recon_threads(1); + let one = match decode_stream(&stream) { + Ok(frames) => frames, + Err(e) => panic!("{NAME}: {fixture} refused at 1 recon thread: {e}"), + }; + crate::par::set_recon_threads(4); + let four = match decode_stream(&stream) { + Ok(frames) => frames, + Err(e) => { + crate::par::set_recon_threads(1); + panic!("{NAME}: {fixture} refused at 4 recon threads: {e}") + } + }; + crate::par::set_recon_threads(1); + assert!(!one.is_empty(), "{NAME}: {fixture} decoded nothing"); + assert_eq!(one.len(), four.len(), "{NAME}: {fixture} frame count"); + for (i, (a, b)) in one.iter().zip(four.iter()).enumerate() { + assert_eq!(a.y, b.y, "{NAME}: {fixture} frame {i} luma differs at 4 recon threads"); + assert_eq!(a.u, b.u, "{NAME}: {fixture} frame {i} U differs at 4 recon threads"); + assert_eq!(a.v, b.v, "{NAME}: {fixture} frame {i} V differs at 4 recon threads"); + } + } + } + #[test] fn a_real_stream_filters_identically_with_one_and_four_filter_threads() { const NAME: &str = "a_real_stream_filters_identically_with_one_and_four_filter_threads";