forge
Explore
Log in

Don't you dare go Hollow

Merge branch 'lane-wave1' (22e1ab44): dequantisation and inverse transform run on the reconstruction workers from recorded coefficients; Mc ops carry only their transform-unit window; byte-exact at recon 1, 4 and the composed setting; 4K 21.3 fps at (6,2,4)

Browse files at this commit

11ef8cafec62d1e1f2ad7d2a486078eb6276dcb1Ahmet Kaan GümüşParent: f97a50b89f1aa3
crates/ec-av1/src/decode.rsmodified+160 -32
@@ -204,7 +204,7 @@ use crate::mvstack::{
204204 single_ref_p6_ctx, uni_comp_ref_p1_ctx,
205205 };
206206 use crate::tile::{INTRA_MODE_CTX, block_grid, has_half};
207-use crate::transform::{TxType, dequant_and_inverse_typed, dequant_and_inverse_typed_wh};
207+use crate::transform::{TxType, dequant_and_inverse_typed_wh};
208208
209209 const PARTITION_NONE: usize = 0;
210210 const PARTITION_HORZ: usize = 1;
@@ -2057,6 +2057,61 @@ fn cfl_src_rect(px: usize, py: usize, bw: usize, bh: usize) -> CflSrc {
20572057 /// entropy parsing no longer touches the frame's own planes (except the two
20582058 /// sites named in [`flush_recon`]'s doc), which is what lets step (ii) run
20592059 /// the replay on a superblock-row wavefront worker.
2060+/// The dequantization + inverse transform of one transform unit, kept as its
2061+/// INPUTS so that a recorded write carries the work instead of its result
2062+/// (lane-wave3): at `EC_AV1_RECON_THREADS > 1` the transform then runs on the
2063+/// recon worker, not on the parsing thread. `stride == 0` means the dense
2064+/// `w x h` residual; otherwise it is re-laid into the `stride x stride` grid
2065+/// `reconstruct_mc_rect` reads both of its buffers at.
2066+#[derive(Clone, Copy)]
2067+pub(crate) struct TxParams {
2068+ pub(crate) w: usize,
2069+ pub(crate) h: usize,
2070+ pub(crate) bit_depth: u8,
2071+ pub(crate) q_idx: i32,
2072+ pub(crate) dc_delta: i32,
2073+ pub(crate) ac_delta: i32,
2074+ pub(crate) tx_type: TxType,
2075+ pub(crate) stride: usize,
2076+}
2077+
2078+impl TxParams {
2079+ /// The one copy of the produce-the-residual code: the inline path calls it
2080+ /// on the parse thread, the wavefront worker calls it on its own, so both
2081+ /// are the same function on the same inputs and byte-identical.
2082+ fn run(self, grid: &[i32]) -> Vec<i32> {
2083+ let residual = dequant_and_inverse_typed_wh(
2084+ grid, self.w, self.h, self.bit_depth, self.q_idx, self.dc_delta, self.ac_delta,
2085+ self.tx_type,
2086+ );
2087+ if self.stride == 0 {
2088+ return residual;
2089+ }
2090+ let mut strided = vec![0i32; self.stride * self.stride];
2091+ for row in 0..self.h {
2092+ strided[row * self.stride..][..self.w]
2093+ .copy_from_slice(&residual[row * self.w..][..self.w]);
2094+ }
2095+ strided
2096+ }
2097+}
2098+
2099+/// A recorded write's residual: already transformed, or the coefficients and
2100+/// [`TxParams`] the worker transforms itself.
2101+pub(crate) enum Residual {
2102+ Done(Vec<i32>),
2103+ Coeffs(TxParams, Vec<i32>),
2104+}
2105+
2106+impl Residual {
2107+ fn resolve(self) -> Vec<i32> {
2108+ match self {
2109+ Self::Done(v) => v,
2110+ Self::Coeffs(p, grid) => p.run(&grid),
2111+ }
2112+ }
2113+}
2114+
20602115 pub(crate) enum ReconOp {
20612116 Intra {
20622117 plane: usize,
@@ -2070,7 +2125,7 @@ pub(crate) enum ReconOp {
20702125 mode: usize,
20712126 angle_delta: i32,
20722127 reach: Reach,
2073- residual: Vec<i32>,
2128+ residual: Residual,
20742129 cfl: Option<(i32, CflSrc)>,
20752130 filter_intra: Option<usize>,
20762131 smooth_neighbor: bool,
@@ -2087,7 +2142,7 @@ pub(crate) enum ReconOp {
20872142 w: usize,
20882143 h: usize,
20892144 prediction: Vec<u16>,
2090- residual: Vec<i32>,
2145+ residual: Residual,
20912146 },
20922147 }
20932148
@@ -2423,7 +2478,7 @@ fn push_intra(
24232478 let pred = take_palette_pred(fctx);
24242479 push_op(fctx, ReconOp::Intra {
24252480 plane, x, y, bw: side, bh: side, rect: false, mode, angle_delta, reach,
2426- residual: residual.to_vec(), cfl, filter_intra, smooth_neighbor, pred,
2481+ residual: Residual::Done(residual.to_vec()), cfl, filter_intra, smooth_neighbor, pred,
24272482 });
24282483 }
24292484
@@ -2452,7 +2507,7 @@ fn push_intra_owned(
24522507 let pred = take_palette_pred(fctx);
24532508 push_op(fctx, ReconOp::Intra {
24542509 plane, x, y, bw: side, bh: side, rect: false, mode, angle_delta, reach,
2455- residual, cfl, filter_intra, smooth_neighbor, pred,
2510+ residual: Residual::Done(residual), cfl, filter_intra, smooth_neighbor, pred,
24562511 });
24572512 }
24582513
@@ -2472,7 +2527,8 @@ fn push_mc_rect_owned(
24722527 return;
24732528 }
24742529 push_op(fctx, ReconOp::Mc {
2475- plane, x, y, stride, w, h, prediction: prediction.to_vec(), residual,
2530+ plane, x, y, stride, w, h, prediction: prediction.to_vec(),
2531+ residual: Residual::Done(residual),
24762532 });
24772533 }
24782534
@@ -2501,7 +2557,7 @@ fn push_intra_rect(
25012557 let pred = take_palette_pred(fctx);
25022558 push_op(fctx, ReconOp::Intra {
25032559 plane, x, y, bw, bh, rect: true, mode, angle_delta, reach,
2504- residual: residual.to_vec(), cfl, filter_intra, smooth_neighbor, pred,
2560+ residual: Residual::Done(residual.to_vec()), cfl, filter_intra, smooth_neighbor, pred,
25052561 });
25062562 }
25072563
@@ -2534,7 +2590,78 @@ fn push_mc_rect(
25342590 }
25352591 push_op(fctx, ReconOp::Mc {
25362592 plane, x, y, stride, w, h,
2537- prediction: prediction.to_vec(), residual: residual.to_vec(),
2593+ prediction: prediction.to_vec(), residual: Residual::Done(residual.to_vec()),
2594+ });
2595+}
2596+
2597+/// [`push_intra_rect`] carrying the transform's INPUTS: at one reconstruction
2598+/// thread the transform runs here, exactly as before; on the wavefront it
2599+/// travels with the op and runs on the worker (lane-wave3).
2600+#[allow(clippy::too_many_arguments)]
2601+fn push_intra_tx(
2602+ plane: usize,
2603+ x: usize,
2604+ y: usize,
2605+ bw: usize,
2606+ bh: usize,
2607+ rect: bool,
2608+ mode: usize,
2609+ angle_delta: i32,
2610+ reach: Reach,
2611+ tx: TxParams,
2612+ grid: &[i32],
2613+ cfl: Option<(i32, CflSrc)>,
2614+ filter_intra: Option<usize>,
2615+ smooth_neighbor: bool,
2616+ fctx: &crate::decode::FrameCtx,
2617+) {
2618+ if fctx.recon_planes.with(std::cell::Cell::get).is_some() {
2619+ inline_intra(
2620+ plane, x, y, bw, bh, rect, mode, angle_delta, reach, &tx.run(grid), cfl, filter_intra,
2621+ smooth_neighbor, fctx,
2622+ );
2623+ return;
2624+ }
2625+ let pred = take_palette_pred(fctx);
2626+ push_op(fctx, ReconOp::Intra {
2627+ plane, x, y, bw, bh, rect, mode, angle_delta, reach,
2628+ residual: Residual::Coeffs(tx, grid.to_vec()), cfl, filter_intra, smooth_neighbor, pred,
2629+ });
2630+}
2631+
2632+/// [`push_mc_rect`] carrying the transform's inputs -- see [`push_intra_tx`].
2633+#[allow(clippy::too_many_arguments)]
2634+fn push_mc_rect_tx(
2635+ plane: usize,
2636+ x: usize,
2637+ y: usize,
2638+ stride: usize,
2639+ w: usize,
2640+ h: usize,
2641+ prediction: &[u16],
2642+ tx: TxParams,
2643+ grid: &[i32],
2644+ fctx: &crate::decode::FrameCtx,
2645+) {
2646+ if fctx.recon_planes.with(std::cell::Cell::get).is_some() {
2647+ inline_mc(plane, x, y, stride, w, h, prediction, &tx.run(grid), fctx);
2648+ return;
2649+ }
2650+ // `reconstruct_mc_rect` reads exactly rows `0..h`, columns `0..w` of the
2651+ // two buffers at `stride`, so the record carries that window densely
2652+ // rather than the whole block's prediction: a 4x4 unit of a 64x64 block
2653+ // copied 64*64 samples on the PARSING thread before. The residual is then
2654+ // produced dense too (`TxParams::stride = 0`), which is the same numbers
2655+ // in the same places at stride `w`.
2656+ let mut window = Vec::with_capacity(w * h);
2657+ for row in 0..h {
2658+ window.extend_from_slice(&prediction[row * stride..][..w]);
2659+ }
2660+ let mut tx = tx;
2661+ tx.stride = 0;
2662+ push_op(fctx, ReconOp::Mc {
2663+ plane, x, y, stride: w, w, h, prediction: window,
2664+ residual: Residual::Coeffs(tx, grid.to_vec()),
25382665 });
25392666 }
25402667
@@ -2577,11 +2704,11 @@ fn exec_op(
25772704 plane, x, y: oy, bw, bh, rect, mode, angle_delta, reach, residual, cfl,
25782705 filter_intra, smooth_neighbor, pred,
25792706 } => exec_intra(
2580- y, u, v, plane, x, oy, bw, bh, rect, mode, angle_delta, reach, &residual, cfl,
2581- filter_intra, smooth_neighbor, pred, fctx,
2707+ y, u, v, plane, x, oy, bw, bh, rect, mode, angle_delta, reach, &residual.resolve(),
2708+ cfl, filter_intra, smooth_neighbor, pred, fctx,
25822709 ),
25832710 ReconOp::Mc { plane, x, y: oy, stride, w, h, prediction, residual } => {
2584- exec_mc(y, u, v, plane, x, oy, stride, w, h, &prediction, &residual, fctx);
2711+ exec_mc(y, u, v, plane, x, oy, stride, w, h, &prediction, &residual.resolve(), fctx);
25852712 }
25862713 }
25872714 }
@@ -12672,21 +12799,28 @@ fn read_plane(
1267212799 full
1267312800 };
1267412801 let (dc_delta, ac_delta) = plane_q_delta(plane_idx, fctx);
12675- let residual = dequant_and_inverse_typed(&levels, side, bit_depth(fctx), block_q_idx(fctx), dc_delta, ac_delta, tx_type);
12802+ let tx = TxParams {
12803+ w: side, h: side, bit_depth: bit_depth(fctx), q_idx: block_q_idx(fctx), dc_delta,
12804+ ac_delta, tx_type, stride: 0,
12805+ };
1267612806 if crate::envflags::env_flag!("EC_AV1_TRACE") {
12807+ let residual = tx.run(&levels);
1267712808 eprintln!(
1267812809 "TRACE dequant plane={plane_idx} base_q_idx={base_q_idx} tx_type={tx_type:?} side={side} levels={levels:?} residual={residual:?}",
1267912810 );
1268012811 }
12681- push_intra_owned(
12812+ push_intra_tx(
1268212813 plane_idx,
1268312814 x,
1268412815 y,
1268512816 side,
12817+ side,
12818+ false,
1268612819 predict_mode,
1268712820 angle_delta,
1268812821 reach,
12689- residual,
12822+ tx,
12823+ &levels,
1269012824 cfl,
1269112825 filter_intra,
1269212826 smooth_neighbor, fctx,
@@ -16877,26 +17011,17 @@ fn read_inter_plane_rect(
1687717011 full
1687817012 };
1687917013 let (dc_delta, ac_delta) = plane_q_delta(plane_idx, fctx);
16880- let residual = dequant_and_inverse_typed_wh(
16881- &grid,
16882- w,
16883- h,
16884- bit_depth(fctx),
16885- block_q_idx(fctx),
16886- dc_delta,
16887- ac_delta,
16888- tx_type,
16889- );
1689017014 if crate::envflags::env_flag!("EC_TRACE_TXTYPE") {
1689117015 eprintln!("OUR_TXTYPE plane={plane_idx} x={x} y={y} w={w} h={h} tx_type={tx_type:?} inh={inherited_luma_tx_type:?}");
1689217016 }
1689317017 // `reconstruct_mc_rect` reads both buffers at the square prediction
16894- // stride, so the dense `w x h` residual is re-laid at that stride first.
16895- let mut strided = vec![0i32; stride * stride];
16896- for row in 0..h {
16897- strided[row * stride..][..w].copy_from_slice(&residual[row * w..][..w]);
16898- }
16899- push_mc_rect_owned(plane_idx, x, y, stride, w, h, prediction, strided, fctx);
17018+ // stride, so the dense `w x h` residual is re-laid at that stride
17019+ // (`TxParams::stride`) after the inverse transform.
17020+ let tx = TxParams {
17021+ w, h, bit_depth: bit_depth(fctx), q_idx: block_q_idx(fctx), dc_delta, ac_delta, tx_type,
17022+ stride,
17023+ };
17024+ push_mc_rect_tx(plane_idx, x, y, stride, w, h, prediction, tx, &grid, fctx);
1690017025 Ok((grid, tx_type))
1690117026 }
1690217027
@@ -21465,8 +21590,11 @@ fn read_inter_plane(
2146521590 full
2146621591 };
2146721592 let (dc_delta, ac_delta) = plane_q_delta(plane_idx, fctx);
21468- let residual = dequant_and_inverse_typed(&grid, side, bit_depth(fctx), block_q_idx(fctx), dc_delta, ac_delta, tx_type);
21469- push_mc_rect_owned(plane_idx, x, y, side, side, side, prediction, residual, fctx);
21593+ let tx = TxParams {
21594+ w: side, h: side, bit_depth: bit_depth(fctx), q_idx: block_q_idx(fctx), dc_delta,
21595+ ac_delta, tx_type, stride: 0,
21596+ };
21597+ push_mc_rect_tx(plane_idx, x, y, side, side, side, prediction, tx, &grid, fctx);
2147021598 Ok((grid, tx_type))
2147121599 }
2147221600