forge
Explore
Log in

Don't you dare go Hollow

Merge branch 'lane-coefgrid' (2e37f9aa): coefficient readers return the Grid enum so all-zero units alias the static zero residual (no per-unit Vec, no zero fill), pointer test replaces the full all-zero scan in the dequantiser and the neighbour-state sum; byte-exact, -1.2% instructions, -44% single-thread page faults

Browse files at this commit

14f4482798ce82c4e41c6a9c6a047c36d3ad3df0Ahmet Kaan GümüşParent: 1d9f0d37002e78
crates/ec-av1/src/decode.rsmodified+114 -104
@@ -2140,7 +2140,7 @@ impl Residual {
21402140 /// the empty marker, so neither the grid nor the residual is cloned and
21412141 /// the worker never runs the transform (60% of the 4K segment's units).
21422142 fn coeffs(tx: TxParams, grid: &[i32], fctx: &crate::decode::FrameCtx) -> Self {
2143- if grid.iter().all(|&c| c == 0) {
2143+ if is_zero_grid(grid) {
21442144 return Self::Done(0, 0);
21452145 }
21462146 Self::Coeffs(tx, push_coeffs(grid, fctx), grid.len() as u32)
@@ -2197,13 +2197,14 @@ fn push_pred_samples(v: &[u16], fctx: &crate::decode::FrameCtx) -> u32 {
21972197 /// and the reconstruction loops below read this instead of a per-unit
21982198 /// `vec![0; w * h]`. Sized for the largest stride a recorded unit can carry
21992199 /// (a 128-wide inter block's prediction stride).
2200-static ZERO_RESIDUAL: [i32; 128 * 128] = [0; 128 * 128];
2200+pub(crate) static ZERO_RESIDUAL: [i32; 128 * 128] = [0; 128 * 128];
22012201
22022202 /// lane-blockscratch: one plane's coefficient levels as the block-tail
22032203 /// neighbour writes want them. A plane that coded nothing (a skipped block, a
22042204 /// strip with no chroma, luma under a var-tx split whose units already wrote
22052205 /// themselves) names the shared [`ZERO_RESIDUAL`] instead of allocating and
22062206 /// zeroing up to 64 KB of its own per block.
2207+#[derive(Clone)]
22072208 enum Grid {
22082209 Zero(usize),
22092210 Own(Vec<i32>),
@@ -2230,6 +2231,13 @@ impl std::ops::Deref for Grid {
22302231 }
22312232 }
22322233
2234+/// Whether a coefficient grid codes nothing: a [`Grid::Zero`] unit IS the
2235+/// shared [`ZERO_RESIDUAL`], so one pointer compare answers the 60% of
2236+/// transform units that code no coefficient without the scan (lane-coefgrid).
2237+pub(crate) fn is_zero_grid(grid: &[i32]) -> bool {
2238+ std::ptr::eq(grid.as_ptr(), ZERO_RESIDUAL.as_ptr()) || grid.iter().all(|&c| c == 0)
2239+}
2240+
22332241 /// `residual`, or `n` zeros when it is the empty all-zero marker.
22342242 fn dense_residual(residual: &[i32], n: usize) -> &[i32] {
22352243 if residual.is_empty() { &ZERO_RESIDUAL[..n] } else { residual }
@@ -5897,10 +5905,9 @@ fn read_coeffs(
58975905 // root cause, see [`base_ctx`]). `None` everywhere else -- every other
58985906 // caller's block genuinely is `side` x `side`.
58995907 rect_shape: Option<(usize, usize)>,
5900-) -> Result<(Vec<i32>, TxType)> {
5908+) -> Result<(Grid, TxType)> {
59015909 let trace = crate::envflags::env_flag!("EC_AV1_TRACE");
59025910 let side = coding.side;
5903- let mut grid = vec![0i32; side * side];
59045911 if crate::envflags::env_flag!("EC_AV1_EOBPT_CDF") {
59055912 let (range, value) = dec.debug_state();
59065913 eprintln!("EC_AV1_STATE_BEFORE_TXBSKIP range={range} value={value}");
@@ -5933,8 +5940,11 @@ fn read_coeffs(
59335940 eprintln!("EC_AV1_STATE_AFTER_TXBSKIP range={range} value={value}");
59345941 }
59355942 if all_zero {
5936- return Ok((grid, TxType::DctDct));
5943+ // lane-coefgrid: the all-zero unit (60% of the 4K segment's) names the
5944+ // shared zero grid instead of allocating and zeroing its own.
5945+ return Ok((Grid::Zero(side * side), TxType::DctDct));
59375946 }
5947+ let mut grid = vec![0i32; side * side];
59385948 let mut tx_type = default_tx_type;
59395949 if let Some(tx_type_cdf) = coding.tx_type.as_deref_mut() {
59405950 // The CDF row's own width names its set (lane-cdffwd2: extended
@@ -6116,7 +6126,7 @@ fn read_coeffs(
61166126 }
61176127 grid[pos] = if negative { -level } else { level };
61186128 }
6119- Ok((grid, tx_type))
6129+ Ok((Grid::Own(grid), tx_type))
61206130 }
61216131
61226132 /// [`read_coeffs`] widened to `(w, h)` (lane-rectwire; lane-rectx added the
@@ -6136,9 +6146,8 @@ fn read_coeffs_rect(
61366146 skip_ctx: usize,
61376147 sign_ctx: usize,
61386148 default_tx_type: TxType,
6139-) -> Result<(Vec<i32>, TxType)> {
6149+) -> Result<(Grid, TxType)> {
61406150 let rect_trace = coeff_trace_on();
6141- let mut grid = vec![0i32; w * h];
61426151 let entry_rng = dec.debug_state().0;
61436152 let all_zero = dec.symbol(&mut coding.txb_skip[skip_ctx]) == 1;
61446153 if rect_trace {
@@ -6149,8 +6158,9 @@ fn read_coeffs_rect(
61496158 );
61506159 }
61516160 if all_zero {
6152- return Ok((grid, TxType::DctDct));
6161+ return Ok((Grid::Zero(w * h), TxType::DctDct));
61536162 }
6163+ let mut grid = vec![0i32; w * h];
61546164 RECT_COEFF_TU_HITS.with(|c| c.set(c.get() + 1));
61556165 // lane-tx4x8: a 4x8/8x4 luma TU DOES carry a `tx_type` symbol (its set is
61566166 // `EXT_TX_SET_DTT4_IDTX(_1DDCT)`, `tx_size_sqr_up == TX_8X8`), including
@@ -6288,7 +6298,7 @@ fn read_coeffs_rect(
62886298 };
62896299 grid[pos] = if negative { -level } else { level };
62906300 }
6291- Ok((grid, tx_type))
6301+ Ok((Grid::Own(grid), tx_type))
62926302 }
62936303
62946304 /// What one coded block leaves behind for the blocks that read it as a
@@ -6324,6 +6334,12 @@ fn fill_span<T: Copy>(dst: &mut [T], start: usize, n: usize, v: T) {
63246334 }
63256335
63266336 fn neighbour_state(grid: &[i32]) -> Neighbour {
6337+ // lane-coefgrid: the shared zero grid sums to zero by construction; this
6338+ // sum has no early exit, so the 60% of units that code nothing used to
6339+ // walk their whole grid here.
6340+ if is_zero_grid(grid) {
6341+ return Neighbour::default();
6342+ }
63276343 let cul: u32 = grid.iter().map(|&l| l.unsigned_abs()).sum();
63286344 Neighbour {
63296345 level: cul.min(7) as u8,
@@ -9223,7 +9239,7 @@ fn decode_rect_split(
92239239 let ac = m.alpha.map(|_| cfl_src_rect(px, py, bw, bh));
92249240 let reach = block_reach;
92259241 let (u_grid, v_grid) = if m.skip {
9226- let zero = vec![0i32; chroma_w * chroma_h];
9242+ let zero = Grid::Zero(chroma_w * chroma_h);
92279243 if let Some((ub, _)) = &m.palette_uv {
92289244 set_palette_pred(ub.clone(), fctx);
92299245 }
@@ -9250,7 +9266,7 @@ fn decode_rect_split(
92509266 default_intra_tx_type(m.uv_predict_mode as u8)
92519267 };
92529268 let around = neighbours.around_mi_rect(at_mi, bw, bh);
9253- let mut grids: Vec<Vec<i32>> = Vec::with_capacity(2);
9269+ let mut grids: Vec<Grid> = Vec::with_capacity(2);
92549270 for plane_idx in 1..=2 {
92559271 let skip_ctx =
92569272 usize::from(around[plane_idx].0) + usize::from(around[plane_idx].1);
@@ -10719,9 +10735,9 @@ fn decode_leaf_rect(
1071910735 let (chroma_w, chroma_h) = (bw / 2, bh / 2);
1072010736 let (luma_levels, u_levels, v_levels);
1072110737 if skip {
10722- luma_levels = vec![0i32; bw * bh];
10723- u_levels = vec![0i32; chroma_w * chroma_h];
10724- v_levels = vec![0i32; chroma_w * chroma_h];
10738+ luma_levels = Grid::Zero(bw * bh);
10739+ u_levels = Grid::Zero(chroma_w * chroma_h);
10740+ v_levels = Grid::Zero(chroma_w * chroma_h);
1072510741 if let Some(b) = &palette_y_buf {
1072610742 set_palette_pred(b.clone(), fctx);
1072710743 }
@@ -11647,7 +11663,7 @@ fn decode_rect4_16_strip(
1164711663 }
1164811664 } else {
1164911665 let levels = if skip {
11650- let zeros = vec![0i32; bw * bh];
11666+ let zeros = Grid::Zero(bw * bh);
1165111667 if let Some(b) = &palette_y_buf {
1165211668 set_palette_pred(b.clone(), fctx);
1165311669 }
@@ -11748,7 +11764,7 @@ fn decode_rect4_16_strip(
1174811764 RECT4_16_UV_PAIR_FILT_HITS.with(|h| h.set(h.get() + 1));
1174911765 }
1175011766 let (u_levels, v_levels) = if skip {
11751- let zeros = vec![0i32; cw * ch];
11767+ let zeros = Grid::Zero(cw * ch);
1175211768 // libaom `predict_and_reconstruct_intra_block` (decodeframe.c
1175311769 // ~1000) predicts EVERY plane and only guards the residual on
1175411770 // `mbmi->skip_txfm`, so a skipped `UV_CFL_PRED` block is still
@@ -13350,7 +13366,7 @@ fn read_plane(
1335013366 luma_skip_ctx: Option<usize>,
1335113367 // See [`PlaneBuf::reconstruct`]'s own doc: caller's neighbour-smooth check.
1335213368 smooth_neighbor: bool, fctx: &crate::decode::FrameCtx,
13353-) -> Result<Vec<i32>> {
13369+) -> Result<Grid> {
1335413370 let skip_ctx = if plane_idx == 0 {
1335513371 luma_skip_ctx.unwrap_or(0)
1335613372 } else {
@@ -13410,19 +13426,11 @@ fn read_plane(
1341013426 // top-left 32x32 of frequencies are coded (spec 5.11.40); the rest of the
1341113427 // dequantized grid stays zero, which `inverse_transform_2d`'s own `< 32`
1341213428 // guard also assumes.
13413- let levels = if tx_side == side {
13414- grid
13415- } else {
13416- // `grid` is `tx_side x tx_side` (the coded corner); `dequant_and_inverse`
13417- // wants the true `side x side` transform, with dqDenom (spec 7.12.3)
13418- // keyed by that true size, not the coded corner's -- so the corner
13419- // goes into the top-left of a `side`-sized grid, not the reverse.
13420- let mut full = vec![0i32; side * side];
13421- for row in 0..tx_side {
13422- full[row * side..][..tx_side].copy_from_slice(&grid[row * tx_side..][..tx_side]);
13423- }
13424- full
13425- };
13429+ // `grid` is `tx_side x tx_side` (the coded corner); `dequant_and_inverse`
13430+ // wants the true `side x side` transform, with dqDenom (spec 7.12.3)
13431+ // keyed by that true size, not the coded corner's -- so the corner
13432+ // goes into the top-left of a `side`-sized grid, not the reverse.
13433+ let levels = extend_corner(grid, tx_side, tx_side, side, side);
1342613434 let (dc_delta, ac_delta) = plane_q_delta(plane_idx, fctx);
1342713435 let tx = TxParams {
1342813436 w: side, h: side, bit_depth: bit_depth(fctx), q_idx: block_q_idx(fctx), dc_delta,
@@ -13431,7 +13439,7 @@ fn read_plane(
1343113439 if crate::envflags::env_flag!("EC_AV1_TRACE") {
1343213440 let residual = tx.run(&levels);
1343313441 eprintln!(
13434- "TRACE dequant plane={plane_idx} base_q_idx={base_q_idx} tx_type={tx_type:?} side={side} levels={levels:?} residual={residual:?}",
13442+ "TRACE dequant plane={plane_idx} base_q_idx={base_q_idx} tx_type={tx_type:?} side={side} levels={:?} residual={residual:?}", &*levels,
1343513443 );
1343613444 }
1343713445 push_intra_tx(
@@ -13945,8 +13953,8 @@ fn decode_block(
1394513953 let chroma_ctx_offset = if cn > 1 { Some(3) } else { None };
1394613954 let tpc = (n_axis / nchunk).max(1);
1394713955 let cpc = (cn / nchunk).max(1);
13948- let mut last_grids = [Vec::new(), Vec::new()];
13949- let mut units: Vec<((usize, usize), usize, Vec<i32>, Vec<i32>)> = Vec::new();
13956+ let mut last_grids = [Grid::Zero(0), Grid::Zero(0)];
13957+ let mut units: Vec<((usize, usize), usize, Grid, Grid)> = Vec::new();
1395013958 for ch_row in 0..nchunk {
1395113959 for ch_col in 0..nchunk {
1395213960 for tu_row in ch_row * tpc..(ch_row + 1) * tpc {
@@ -14308,8 +14316,8 @@ fn decode_block_128rect(
1430814316 let tpc = 64 / logical_tx;
1430914317 let zero_tu = vec![0i32; logical_tx * logical_tx];
1431014318 let zero_cu = vec![0i32; chroma_tx * chroma_tx];
14311- let mut last_grids = [Vec::new(), Vec::new()];
14312- let mut units: Vec<((usize, usize), Vec<i32>, Vec<i32>)> = Vec::new();
14319+ let mut last_grids = [Grid::Zero(0), Grid::Zero(0)];
14320+ let mut units: Vec<((usize, usize), Grid, Grid)> = Vec::new();
1431314321 for ch_row in 0..nch_y {
1431414322 for ch_col in 0..nch_x {
1431514323 for tu_row in ch_row * tpc..(ch_row + 1) * tpc {
@@ -14829,9 +14837,9 @@ fn decode_leaf8(
1482914837 None,
1483014838 smooth_neighbor_uv, fctx,
1483114839 );
14832- luma_grid = vec![0i32; 64];
14833- u_grid = vec![0i32; 16];
14834- v_grid = vec![0i32; 16];
14840+ luma_grid = Grid::Zero(64);
14841+ u_grid = Grid::Zero(16);
14842+ v_grid = Grid::Zero(16);
1483514843 } else if resolved != 4 {
1483614844 let around = neighbours.around_mi(leaf_mi, 8);
1483714845 if let Some(buf) = &palette_y_buf {
@@ -15369,7 +15377,7 @@ fn decode_leaf_split4(
1536915377 neighbours.above_uv_mode[c] = uv_predict_mode;
1537015378 neighbours.left_uv_mode[r] = uv_predict_mode;
1537115379 neighbours.record_uv_mode_mi(leaf_mi.0, leaf_mi.1, 2, 2, uv_predict_mode);
15372- let (u_grid, v_grid): (Vec<i32>, Vec<i32>) = if leaf_skips[3] {
15380+ let (u_grid, v_grid): (Grid, Grid) = if leaf_skips[3] {
1537315381 // A skipped chroma block still gets its CfL contribution: libaom's
1537415382 // `predict_and_reconstruct_intra_block` calls `cfl_predict_block`
1537515383 // from `av1_predict_intra_block_facade` (av1/common/reconintra.c:1719)
@@ -15385,7 +15393,7 @@ fn decode_leaf_split4(
1538515393 }
1538615394 push_intra(1, cpx, cpy, 4, uv_predict_mode, angle_delta_uv, group_reach, &ZERO_RESIDUAL[..16], alpha.zip(ac).map(|((au, _), ac)| (au, ac)), None, smooth_neighbor_uv, fctx);
1538715395 push_intra(2, cpx, cpy, 4, uv_predict_mode, angle_delta_uv, group_reach, &ZERO_RESIDUAL[..16], alpha.zip(ac).map(|((_, av), ac)| (av, ac)), None, smooth_neighbor_uv, fctx);
15388- (vec![0i32; 16], vec![0i32; 16])
15396+ (Grid::Zero(16), Grid::Zero(16))
1538915397 } else {
1539015398 let ac = alpha.map(|_| cfl_src(gpx, gpy, 8));
1539115399 let chroma_around = neighbours.around_mi(leaf_mi, 8);
@@ -15661,7 +15669,7 @@ fn decode_leaf_rect8(
1566115669 continue;
1566215670 }
1566315671 let grid = if skip {
15664- let zeros = vec![0i32; bw * bh];
15672+ let zeros = Grid::Zero(bw * bh);
1566515673 push_intra_rect(0,
1566615674 px, py, bw, bh, mode, angle_delta_y, reach, &zeros, None, filter_intra,
1566715675 smooth_neighbor, fctx,
@@ -15741,7 +15749,7 @@ fn decode_leaf_rect8(
1574115749 neighbours.above_uv_mode[c] = uv_predict_mode;
1574215750 neighbours.left_uv_mode[r] = uv_predict_mode;
1574315751 neighbours.record_uv_mode_mi(leaf_mi.0, leaf_mi.1, 2, 2, uv_predict_mode);
15744- let (u_grid, v_grid): (Vec<i32>, Vec<i32>) = if leaf_skips[1] {
15752+ let (u_grid, v_grid): (Grid, Grid) = if leaf_skips[1] {
1574515753 // A skipped chroma block still gets its CfL contribution: libaom's
1574615754 // `predict_and_reconstruct_intra_block` calls `cfl_predict_block`
1574715755 // from `av1_predict_intra_block_facade` (av1/common/reconintra.c:1719)
@@ -15757,7 +15765,7 @@ fn decode_leaf_rect8(
1575715765 }
1575815766 push_intra(1, cpx, cpy, 4, uv_predict_mode, angle_delta_uv, group_reach, &ZERO_RESIDUAL[..16], alpha.zip(ac).map(|((au, _), ac)| (au, ac)), None, smooth_neighbor_uv, fctx);
1575915767 push_intra(2, cpx, cpy, 4, uv_predict_mode, angle_delta_uv, group_reach, &ZERO_RESIDUAL[..16], alpha.zip(ac).map(|((_, av), ac)| (av, ac)), None, smooth_neighbor_uv, fctx);
15760- (vec![0i32; 16], vec![0i32; 16])
15768+ (Grid::Zero(16), Grid::Zero(16))
1576115769 } else {
1576215770 let ac = alpha.map(|_| cfl_src(gpx, gpy, 8));
1576315771 let chroma_around = neighbours.around_mi(leaf_mi, 8);
@@ -17675,7 +17683,7 @@ fn read_inter_plane_rect(
1767517683 prediction: Pred<'_>,
1767617684 inherited_luma_tx_type: Option<TxType>,
1767717685 luma_skip_ctx: Option<usize>, fctx: &crate::decode::FrameCtx,
17678-) -> Result<(Vec<i32>, TxType)> {
17686+) -> Result<(Grid, TxType)> {
1767917687 let skip_ctx = if plane_idx == 0 {
1768017688 // `get_txb_ctx_general`: 0 only when this unit IS the whole plane
1768117689 // block; a var-tx leaf passes its own neighbour-table context.
@@ -17732,15 +17740,7 @@ fn read_inter_plane_rect(
1773217740 default_tx_type,
1773317741 )?
1773417742 };
17735- let grid = if (cw, ch) == (w, h) {
17736- corner
17737- } else {
17738- let mut full = vec![0i32; w * h];
17739- for row in 0..ch {
17740- full[row * w..][..cw].copy_from_slice(&corner[row * cw..][..cw]);
17741- }
17742- full
17743- };
17743+ let grid = extend_corner(corner, cw, ch, w, h);
1774417744 let (dc_delta, ac_delta) = plane_q_delta(plane_idx, fctx);
1774517745 if crate::envflags::env_flag!("EC_TRACE_TXTYPE") {
1774617746 eprintln!("OUR_TXTYPE plane={plane_idx} x={x} y={y} w={w} h={h} tx_type={tx_type:?} inh={inherited_luma_tx_type:?}");
@@ -17756,6 +17756,24 @@ fn read_inter_plane_rect(
1775617756 Ok((grid, tx_type))
1775717757 }
1775817758
17759+/// The coded `cw x ch` corner of a transform zero-extended into its true
17760+/// `w x h` grid (a 64-length axis codes only its low 32 coefficients). A
17761+/// corner that coded nothing stays the shared zero grid -- no allocation
17762+/// (lane-coefgrid).
17763+fn extend_corner(corner: Grid, cw: usize, ch: usize, w: usize, h: usize) -> Grid {
17764+ if (cw, ch) == (w, h) {
17765+ return corner;
17766+ }
17767+ if let Grid::Zero(_) = corner {
17768+ return Grid::Zero(w * h);
17769+ }
17770+ let mut full = vec![0i32; w * h];
17771+ for row in 0..ch {
17772+ full[row * w..][..cw].copy_from_slice(&corner[row * cw..][..cw]);
17773+ }
17774+ Grid::Own(full)
17775+}
17776+
1775917777 /// A rect coefficient grid re-laid into the square `stride x stride` grid the
1776017778 /// neighbour/deblock stamps downstream of the residual expect.
1776117779 fn embed_rect_grid(grid: &[i32], w: usize, h: usize, stride: usize) -> Vec<i32> {
@@ -17790,7 +17808,7 @@ fn read_inter_luma8(
1779017808 scan4: &[u16],
1779117809 reduced_tx_set: bool,
1779217810 split: bool, fctx: &crate::decode::FrameCtx,
17793-) -> Result<(Vec<i32>, TxType)> {
17811+) -> Result<(Grid, TxType)> {
1779417812 if !split {
1779517813 return read_inter_plane(
1779617814 dec,
@@ -17847,7 +17865,7 @@ fn read_inter_luma8(
1784717865 neighbours.record_mi_luma(tu_mi, 4, &tu_grid);
1784817866 }
1784917867 }
17850- Ok((vec![0i32; 64], first))
17868+ Ok((Grid::Zero(64), first))
1785117869 }
1785217870
1785317871 /// The [`TxbSet`] one leaf of an inter block's var-tx tree reads its
@@ -23084,7 +23102,7 @@ fn read_inter_plane(
2308423102 // transform every non-var-tx caller reads, whose plane-0 context is the
2308523103 // lone-transform-unit 0.
2308623104 luma_skip_ctx: Option<usize>, fctx: &crate::decode::FrameCtx,
23087-) -> Result<(Vec<i32>, TxType)> {
23105+) -> Result<(Grid, TxType)> {
2308823106 let skip_ctx = if plane_idx == 0 {
2308923107 luma_skip_ctx.unwrap_or(0)
2309023108 } else {
@@ -23122,15 +23140,7 @@ fn read_inter_plane(
2312223140 // [`read_plane`]'s own `tx_side != side` branch. Every other transform
2312323141 // this decoder codes has `tx_side == side`, so this is inert below 64.
2312423142 let tx_side = side.min(32);
23125- let grid = if tx_side == side {
23126- grid
23127- } else {
23128- let mut full = vec![0i32; side * side];
23129- for row in 0..tx_side {
23130- full[row * side..][..tx_side].copy_from_slice(&grid[row * tx_side..][..tx_side]);
23131- }
23132- full
23133- };
23143+ let grid = extend_corner(grid, tx_side, tx_side, side, side);
2313423144 let (dc_delta, ac_delta) = plane_q_delta(plane_idx, fctx);
2313523145 let tx = TxParams {
2313623146 w: side, h: side, bit_depth: bit_depth(fctx), q_idx: block_q_idx(fctx), dc_delta,
@@ -26018,7 +26028,7 @@ fn decode_inter_block(
2601826028 None,
2601926029 None, fctx,
2602026030 )?;
26021- luma_grid = Grid::Own(coded.0);
26031+ luma_grid = coded.0;
2602226032 luma_tx_type = coded.1;
2602326033 }
2602426034 }
@@ -26087,7 +26097,7 @@ fn decode_inter_block(
2608726097 chroma_side,
2608826098 ));
2608926099 } else {
26090- u_grid = Grid::Own(read_inter_plane(
26100+ u_grid = read_inter_plane(
2609126101 dec,
2609226102 cdfs,
2609326103 chroma_set,
@@ -26104,8 +26114,8 @@ fn decode_inter_block(
2610426114 Some(luma_tx_type),
2610526115 None, fctx,
2610626116 )?
26107- .0);
26108- v_grid = Grid::Own(read_inter_plane(
26117+ .0;
26118+ v_grid = read_inter_plane(
2610926119 dec,
2611026120 cdfs,
2611126121 chroma_set,
@@ -26122,7 +26132,7 @@ fn decode_inter_block(
2612226132 Some(luma_tx_type),
2612326133 None, fctx,
2612426134 )?
26125- .0);
26135+ .0;
2612626136 }
2612726137 }
2612826138 } else {
@@ -27277,7 +27287,7 @@ fn decode_inter_block(
2727727287 None,
2727827288 None, fctx,
2727927289 )?;
27280- luma_grid = Grid::Own(coded.0);
27290+ luma_grid = coded.0;
2728127291 luma_tx_type = coded.1;
2728227292 }
2728327293 }
@@ -27355,7 +27365,7 @@ fn decode_inter_block(
2735527365 chroma_side,
2735627366 ));
2735727367 } else {
27358- u_grid = Grid::Own(read_inter_plane(
27368+ u_grid = read_inter_plane(
2735927369 dec,
2736027370 cdfs,
2736127371 chroma_set,
@@ -27372,8 +27382,8 @@ fn decode_inter_block(
2737227382 Some(luma_tx_type),
2737327383 None, fctx,
2737427384 )?
27375- .0);
27376- v_grid = Grid::Own(read_inter_plane(
27385+ .0;
27386+ v_grid = read_inter_plane(
2737727387 dec,
2737827388 cdfs,
2737927389 chroma_set,
@@ -27390,7 +27400,7 @@ fn decode_inter_block(
2739027400 Some(luma_tx_type),
2739127401 None, fctx,
2739227402 )?
27393- .0);
27403+ .0;
2739427404 }
2739527405 }
2739627406 }
@@ -27816,7 +27826,7 @@ fn decode_inter_block(
2781627826 // so `txb_skip_ctx` is the neighbour-magnitude table, not
2781727827 // the lone-TU 0 (`get_txb_ctx_general`).
2781827828 let tu_skip_ctx = neighbours.luma_skip_ctx(tu_mi, tx_px / MI);
27819- Grid::Own(read_plane(
27829+ read_plane(
2782027830 dec,
2782127831 cdfs,
2782227832 txbset_for(tx_px, reduced),
@@ -27841,7 +27851,7 @@ fn decode_inter_block(
2784127851 filter_intra,
2784227852 Some(tu_skip_ctx),
2784327853 smooth_neighbor, fctx,
27844- )?)
27854+ )?
2784527855 };
2784627856 neighbours.record_mi_luma(tu_mi, tx_px, &tu_grid);
2784727857 if side > 64 {
@@ -27882,7 +27892,7 @@ fn decode_inter_block(
2788227892 );
2788327893 Grid::Zero(cu_tx * cu_tx)
2788427894 } else {
27885- Grid::Own(read_plane(
27895+ read_plane(
2788627896 dec,
2788727897 cdfs,
2788827898 chroma_set,
@@ -27909,7 +27919,7 @@ fn decode_inter_block(
2790927919 // (lane-sb128b r3).
2791027920 Some(3),
2791127921 smooth_neighbor_uv, fctx,
27912- )?)
27922+ )?
2791327923 };
2791427924 // Immediately, so the NEXT chunk's unit reads this
2791527925 // one's coefficient context.
@@ -28007,7 +28017,7 @@ fn decode_inter_block(
2800728017 if let Some(buf) = &palette_y_buf {
2800828018 set_palette_pred(buf.clone(), fctx);
2800928019 }
28010- luma_grid = Grid::Own(read_plane(
28020+ luma_grid = read_plane(
2801128021 dec,
2801228022 cdfs,
2801328023 luma_set_intra,
@@ -28028,13 +28038,13 @@ fn decode_inter_block(
2802828038 filter_intra,
2802928039 None,
2803028040 smooth_neighbor, fctx,
28031- )?);
28041+ )?;
2803228042 }
2803328043 let ac = alpha.map(|_| cfl_src(px, py, side));
2803428044 if let Some((ub, _)) = &palette_uv_bufs {
2803528045 set_palette_pred(ub.clone(), fctx);
2803628046 }
28037- u_grid = Grid::Own(read_plane(
28047+ u_grid = read_plane(
2803828048 dec,
2803928049 cdfs,
2804028050 chroma_set,
@@ -28055,11 +28065,11 @@ fn decode_inter_block(
2805528065 None,
2805628066 None,
2805728067 smooth_neighbor_uv, fctx,
28058- )?);
28068+ )?;
2805928069 if let Some((_, vb)) = &palette_uv_bufs {
2806028070 set_palette_pred(vb.clone(), fctx);
2806128071 }
28062- v_grid = Grid::Own(read_plane(
28072+ v_grid = read_plane(
2806328073 dec,
2806428074 cdfs,
2806528075 chroma_set,
@@ -28080,7 +28090,7 @@ fn decode_inter_block(
2808028090 None,
2808128091 None,
2808228092 smooth_neighbor_uv, fctx,
28083- )?);
28093+ )?;
2808428094 }
2808528095 }
2808628096 // lane-comppin r9: end-of-block checkpoint -- everything between
@@ -28112,7 +28122,7 @@ fn decode_inter_block(
2811228122 // the partial right superblock read row 1 where libaom reads row 2). The
2811328123 // units are collected here and re-stamped after that record, in mu-chunk
2811428124 // order so a later unit still wins over an earlier one.
28115- let mu_chroma_units: Vec<((usize, usize), usize, Vec<i32>)> = if mu_chroma {
28125+ let mu_chroma_units: Vec<((usize, usize), usize, Grid)> = if mu_chroma {
2811628126 let cu = 32usize;
2811728127 let mut units = Vec::new();
2811828128 for cr in 0..(write_h / 64).max(1) {
@@ -28124,7 +28134,7 @@ fn decode_inter_block(
2812428134 let start = (cr * cu + rr) * chroma_side + cc * cu;
2812528135 unit.extend_from_slice(&grid[start..start + cu]);
2812628136 }
28127- units.push((cu_mi, plane, unit));
28137+ units.push((cu_mi, plane, Grid::Own(unit)));
2812828138 }
2812928139 }
2813028140 }
@@ -28801,7 +28811,7 @@ fn decode_inter_sub8_split4(
2880128811 let (u_grid, v_grid) = if last_skip {
2880228812 push_mc(1, cpx, cpy, B4, Pred::dense(&pred_u), &ZERO_RESIDUAL[..B4 * B4], fctx);
2880328813 push_mc(2, cpx, cpy, B4, Pred::dense(&pred_v), &ZERO_RESIDUAL[..B4 * B4], fctx);
28804- (vec![0i32; B4 * B4], vec![0i32; B4 * B4])
28814+ (Grid::Zero(B4 * B4), Grid::Zero(B4 * B4))
2880528815 } else {
2880628816 let around = neighbours.around_mi(group_mi, 8);
2880728817 let ug = read_inter_plane(
@@ -29108,7 +29118,7 @@ fn decode_intra_sub8_leaf(
2910829118 }
2910929119 } else {
2911029120 let grid_levels = if skip {
29111- let zeros = vec![0i32; bw * bh];
29121+ let zeros = Grid::Zero(bw * bh);
2911229122 push_intra_rect(0,
2911329123 px, py, bw, bh, mode, angle_delta_y, reach, &zeros, None, filter_intra,
2911429124 smooth_neighbor, fctx,
@@ -29238,14 +29248,14 @@ fn decode_intra_sub8_leaf(
2923829248 record_strip_palette(neighbours, (gr, gc), 8, 8, 0, [0u16; 8], 0, [0u16; 8]);
2923929249
2924029250 let ac = alpha.map(|_| cfl_src(gpx, gpy, 8));
29241- let (u_grid, v_grid): (Vec<i32>, Vec<i32>) = if skip {
29251+ let (u_grid, v_grid): (Grid, Grid) = if skip {
2924229252 if alpha.is_some() {
2924329253 CHROMA_SKIP_CFL_HITS.with(|h| h.set(h.get() + 1));
2924429254 SKIP_CFL_HITS.with(|c| c.set(c.get() + 1));
2924529255 }
2924629256 push_intra(1, cpx, cpy, 4, uv_predict_mode, angle_delta_uv, group_reach, &ZERO_RESIDUAL[..16], alpha.zip(ac).map(|((au, _), ac)| (au, ac)), None, smooth_neighbor_uv, fctx);
2924729257 push_intra(2, cpx, cpy, 4, uv_predict_mode, angle_delta_uv, group_reach, &ZERO_RESIDUAL[..16], alpha.zip(ac).map(|((_, av), ac)| (av, ac)), None, smooth_neighbor_uv, fctx);
29248- (vec![0i32; 16], vec![0i32; 16])
29258+ (Grid::Zero(16), Grid::Zero(16))
2924929259 } else {
2925029260 let chroma_around = neighbours.around_mi(group_mi, 8);
2925129261 let ug = read_plane(
@@ -29820,7 +29830,7 @@ fn decode_inter_sub8_rect2(
2982029830 let (u_grid, v_grid) = if last_skip {
2982129831 push_mc(1, cpx, cpy, B4, Pred::dense(&pred_u), &ZERO_RESIDUAL[..B4 * B4], fctx);
2982229832 push_mc(2, cpx, cpy, B4, Pred::dense(&pred_v), &ZERO_RESIDUAL[..B4 * B4], fctx);
29823- (vec![0i32; B4 * B4], vec![0i32; B4 * B4])
29833+ (Grid::Zero(B4 * B4), Grid::Zero(B4 * B4))
2982429834 } else {
2982529835 let around = neighbours.around_mi(group_mi, 8);
2982629836 let ug = read_inter_plane(
@@ -30663,9 +30673,9 @@ fn decode_inter_block8(
3066330673 Pred::dense(&pred_v),
3066430674 &ZERO_RESIDUAL[..CHROMA_SIDE * CHROMA_SIDE], fctx,
3066530675 );
30666- luma_grid = vec![0i32; SIDE * SIDE];
30667- u_grid = vec![0i32; CHROMA_SIDE * CHROMA_SIDE];
30668- v_grid = vec![0i32; CHROMA_SIDE * CHROMA_SIDE];
30676+ luma_grid = Grid::Zero(SIDE * SIDE);
30677+ u_grid = Grid::Zero(CHROMA_SIDE * CHROMA_SIDE);
30678+ v_grid = Grid::Zero(CHROMA_SIDE * CHROMA_SIDE);
3066930679 } else {
3067030680 let around = neighbours.around_mi(leaf_mi, 8);
3067130681 let (grid8, luma_tx_type) = read_inter_luma8(
@@ -31308,9 +31318,9 @@ fn decode_inter_block8(
3130831318 Pred::dense(&pred_v),
3130931319 &ZERO_RESIDUAL[..CHROMA_SIDE * CHROMA_SIDE], fctx,
3131031320 );
31311- luma_grid = vec![0i32; SIDE * SIDE];
31312- u_grid = vec![0i32; CHROMA_SIDE * CHROMA_SIDE];
31313- v_grid = vec![0i32; CHROMA_SIDE * CHROMA_SIDE];
31321+ luma_grid = Grid::Zero(SIDE * SIDE);
31322+ u_grid = Grid::Zero(CHROMA_SIDE * CHROMA_SIDE);
31323+ v_grid = Grid::Zero(CHROMA_SIDE * CHROMA_SIDE);
3131431324 } else {
3131531325 let around = neighbours.around_mi(leaf_mi, 8);
3131631326 let (grid8, luma_tx_type) = read_inter_luma8(
@@ -31674,7 +31684,7 @@ fn decode_inter_block8(
3167431684 filter_intra,
3167531685 smooth_neighbor, fctx,
3167631686 );
31677- vec![0i32; tx_px * tx_px]
31687+ Grid::Zero(tx_px * tx_px)
3167831688 } else {
3167931689 // The unit is smaller than the block it sits in, so
3168031690 // `txb_skip_ctx` is the neighbour-magnitude table, not
@@ -31761,16 +31771,16 @@ fn decode_inter_block8(
3176131771 None,
3176231772 smooth_neighbor_uv, fctx,
3176331773 );
31764- luma_grid = vec![0i32; SIDE * SIDE];
31765- u_grid = vec![0i32; CHROMA_SIDE * CHROMA_SIDE];
31766- v_grid = vec![0i32; CHROMA_SIDE * CHROMA_SIDE];
31774+ luma_grid = Grid::Zero(SIDE * SIDE);
31775+ u_grid = Grid::Zero(CHROMA_SIDE * CHROMA_SIDE);
31776+ v_grid = Grid::Zero(CHROMA_SIDE * CHROMA_SIDE);
3176731777 } else {
3176831778 let around = neighbours.around_mi(leaf_mi, 8);
3176931779 luma_grid = if split8 {
3177031780 // Already coded and reconstructed unit by unit above; the
3177131781 // block-level grid only feeds the neighbour write-back,
3177231782 // which the `saved_luma_ctx` restore below keeps per-TU.
31773- vec![0i32; SIDE * SIDE]
31783+ Grid::Zero(SIDE * SIDE)
3177431784 } else {
3177531785 if let Some(buf) = &palette_y_buf {
3177631786 set_palette_pred(buf.clone(), fctx);
crates/ec-av1/src/transform.rsmodified+1 -1
@@ -1340,7 +1340,7 @@ pub fn dequant_and_inverse_typed_wh(
13401340 // `decode.rs`), which also drops the `vec![0; w * h]` memset here and the
13411341 // `to_vec` clone the recorded [`crate::decode::Residual`] made of it --
13421342 // together 6.5% of the 4K segment's profile (memset + memmove).
1343- if levels.iter().all(|&l| l == 0) {
1343+ if crate::decode::is_zero_grid(levels) {
13441344 return Vec::new();
13451345 }
13461346 DQ_SCRATCH.with(|cell| {