forge
Explore
Log in

Don't you dare go Hollow

Coefficient readers return the shared zero grid instead of a fresh Vec

Browse files at this commit

f969486e2bb4ec91bb31175dcd49cf64172145b6Ahmet Kaan GümüşParent: c870931
  • perf
    • coefficient readers return the shared zero grid instead of a fresh Vec
Show full message
perf(av1): coefficient readers return the shared zero grid instead of a fresh Vec

read_coeffs/read_coeffs_rect now return the existing Grid enum: an all-zero
transform unit (60% of the 4K segment's) names ZERO_RESIDUAL instead of
allocating and memsetting its own side*side Vec, and the coded corner's
zero-extension (extend_corner) keeps that when nothing was coded. read_plane,
read_inter_plane, read_inter_plane_rect and read_inter_luma8 return Grid, so
nine Grid::Own(read_*(..)) wrappers and every skipped-arm vec![0i32; ..]
collapse to Grid::Zero. Residual::coeffs recognises the shared grid by pointer
instead of scanning it.
crates/ec-av1/src/decode.rsmodified+104 -102
@@ -2140,6 +2140,11 @@ 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+ // lane-coefgrid: a [`Grid::Zero`] unit IS the shared zero grid -- one
2144+ // pointer compare instead of scanning up to 16 K coefficients.
2145+ if std::ptr::eq(grid.as_ptr(), ZERO_RESIDUAL.as_ptr()) {
2146+ return Self::Done(0, 0);
2147+ }
21432148 if grid.iter().all(|&c| c == 0) {
21442149 return Self::Done(0, 0);
21452150 }
@@ -2204,6 +2209,7 @@ static ZERO_RESIDUAL: [i32; 128 * 128] = [0; 128 * 128];
22042209 /// strip with no chroma, luma under a var-tx split whose units already wrote
22052210 /// themselves) names the shared [`ZERO_RESIDUAL`] instead of allocating and
22062211 /// zeroing up to 64 KB of its own per block.
2212+#[derive(Clone)]
22072213 enum Grid {
22082214 Zero(usize),
22092215 Own(Vec<i32>),
@@ -5897,10 +5903,9 @@ fn read_coeffs(
58975903 // root cause, see [`base_ctx`]). `None` everywhere else -- every other
58985904 // caller's block genuinely is `side` x `side`.
58995905 rect_shape: Option<(usize, usize)>,
5900-) -> Result<(Vec<i32>, TxType)> {
5906+) -> Result<(Grid, TxType)> {
59015907 let trace = crate::envflags::env_flag!("EC_AV1_TRACE");
59025908 let side = coding.side;
5903- let mut grid = vec![0i32; side * side];
59045909 if crate::envflags::env_flag!("EC_AV1_EOBPT_CDF") {
59055910 let (range, value) = dec.debug_state();
59065911 eprintln!("EC_AV1_STATE_BEFORE_TXBSKIP range={range} value={value}");
@@ -5933,8 +5938,11 @@ fn read_coeffs(
59335938 eprintln!("EC_AV1_STATE_AFTER_TXBSKIP range={range} value={value}");
59345939 }
59355940 if all_zero {
5936- return Ok((grid, TxType::DctDct));
5941+ // lane-coefgrid: the all-zero unit (60% of the 4K segment's) names the
5942+ // shared zero grid instead of allocating and zeroing its own.
5943+ return Ok((Grid::Zero(side * side), TxType::DctDct));
59375944 }
5945+ let mut grid = vec![0i32; side * side];
59385946 let mut tx_type = default_tx_type;
59395947 if let Some(tx_type_cdf) = coding.tx_type.as_deref_mut() {
59405948 // The CDF row's own width names its set (lane-cdffwd2: extended
@@ -6116,7 +6124,7 @@ fn read_coeffs(
61166124 }
61176125 grid[pos] = if negative { -level } else { level };
61186126 }
6119- Ok((grid, tx_type))
6127+ Ok((Grid::Own(grid), tx_type))
61206128 }
61216129
61226130 /// [`read_coeffs`] widened to `(w, h)` (lane-rectwire; lane-rectx added the
@@ -6136,9 +6144,8 @@ fn read_coeffs_rect(
61366144 skip_ctx: usize,
61376145 sign_ctx: usize,
61386146 default_tx_type: TxType,
6139-) -> Result<(Vec<i32>, TxType)> {
6147+) -> Result<(Grid, TxType)> {
61406148 let rect_trace = coeff_trace_on();
6141- let mut grid = vec![0i32; w * h];
61426149 let entry_rng = dec.debug_state().0;
61436150 let all_zero = dec.symbol(&mut coding.txb_skip[skip_ctx]) == 1;
61446151 if rect_trace {
@@ -6149,8 +6156,9 @@ fn read_coeffs_rect(
61496156 );
61506157 }
61516158 if all_zero {
6152- return Ok((grid, TxType::DctDct));
6159+ return Ok((Grid::Zero(w * h), TxType::DctDct));
61536160 }
6161+ let mut grid = vec![0i32; w * h];
61546162 RECT_COEFF_TU_HITS.with(|c| c.set(c.get() + 1));
61556163 // lane-tx4x8: a 4x8/8x4 luma TU DOES carry a `tx_type` symbol (its set is
61566164 // `EXT_TX_SET_DTT4_IDTX(_1DDCT)`, `tx_size_sqr_up == TX_8X8`), including
@@ -6288,7 +6296,7 @@ fn read_coeffs_rect(
62886296 };
62896297 grid[pos] = if negative { -level } else { level };
62906298 }
6291- Ok((grid, tx_type))
6299+ Ok((Grid::Own(grid), tx_type))
62926300 }
62936301
62946302 /// What one coded block leaves behind for the blocks that read it as a
@@ -9223,7 +9231,7 @@ fn decode_rect_split(
92239231 let ac = m.alpha.map(|_| cfl_src_rect(px, py, bw, bh));
92249232 let reach = block_reach;
92259233 let (u_grid, v_grid) = if m.skip {
9226- let zero = vec![0i32; chroma_w * chroma_h];
9234+ let zero = Grid::Zero(chroma_w * chroma_h);
92279235 if let Some((ub, _)) = &m.palette_uv {
92289236 set_palette_pred(ub.clone(), fctx);
92299237 }
@@ -9250,7 +9258,7 @@ fn decode_rect_split(
92509258 default_intra_tx_type(m.uv_predict_mode as u8)
92519259 };
92529260 let around = neighbours.around_mi_rect(at_mi, bw, bh);
9253- let mut grids: Vec<Vec<i32>> = Vec::with_capacity(2);
9261+ let mut grids: Vec<Grid> = Vec::with_capacity(2);
92549262 for plane_idx in 1..=2 {
92559263 let skip_ctx =
92569264 usize::from(around[plane_idx].0) + usize::from(around[plane_idx].1);
@@ -10719,9 +10727,9 @@ fn decode_leaf_rect(
1071910727 let (chroma_w, chroma_h) = (bw / 2, bh / 2);
1072010728 let (luma_levels, u_levels, v_levels);
1072110729 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];
10730+ luma_levels = Grid::Zero(bw * bh);
10731+ u_levels = Grid::Zero(chroma_w * chroma_h);
10732+ v_levels = Grid::Zero(chroma_w * chroma_h);
1072510733 if let Some(b) = &palette_y_buf {
1072610734 set_palette_pred(b.clone(), fctx);
1072710735 }
@@ -11647,7 +11655,7 @@ fn decode_rect4_16_strip(
1164711655 }
1164811656 } else {
1164911657 let levels = if skip {
11650- let zeros = vec![0i32; bw * bh];
11658+ let zeros = Grid::Zero(bw * bh);
1165111659 if let Some(b) = &palette_y_buf {
1165211660 set_palette_pred(b.clone(), fctx);
1165311661 }
@@ -11748,7 +11756,7 @@ fn decode_rect4_16_strip(
1174811756 RECT4_16_UV_PAIR_FILT_HITS.with(|h| h.set(h.get() + 1));
1174911757 }
1175011758 let (u_levels, v_levels) = if skip {
11751- let zeros = vec![0i32; cw * ch];
11759+ let zeros = Grid::Zero(cw * ch);
1175211760 // libaom `predict_and_reconstruct_intra_block` (decodeframe.c
1175311761 // ~1000) predicts EVERY plane and only guards the residual on
1175411762 // `mbmi->skip_txfm`, so a skipped `UV_CFL_PRED` block is still
@@ -13351,7 +13359,7 @@ fn read_plane(
1335113359 luma_skip_ctx: Option<usize>,
1335213360 // See [`PlaneBuf::reconstruct`]'s own doc: caller's neighbour-smooth check.
1335313361 smooth_neighbor: bool, fctx: &crate::decode::FrameCtx,
13354-) -> Result<Vec<i32>> {
13362+) -> Result<Grid> {
1335513363 let skip_ctx = if plane_idx == 0 {
1335613364 luma_skip_ctx.unwrap_or(0)
1335713365 } else {
@@ -13411,19 +13419,11 @@ fn read_plane(
1341113419 // top-left 32x32 of frequencies are coded (spec 5.11.40); the rest of the
1341213420 // dequantized grid stays zero, which `inverse_transform_2d`'s own `< 32`
1341313421 // guard also assumes.
13414- let levels = if tx_side == side {
13415- grid
13416- } else {
13417- // `grid` is `tx_side x tx_side` (the coded corner); `dequant_and_inverse`
13418- // wants the true `side x side` transform, with dqDenom (spec 7.12.3)
13419- // keyed by that true size, not the coded corner's -- so the corner
13420- // goes into the top-left of a `side`-sized grid, not the reverse.
13421- let mut full = vec![0i32; side * side];
13422- for row in 0..tx_side {
13423- full[row * side..][..tx_side].copy_from_slice(&grid[row * tx_side..][..tx_side]);
13424- }
13425- full
13426- };
13422+ // `grid` is `tx_side x tx_side` (the coded corner); `dequant_and_inverse`
13423+ // wants the true `side x side` transform, with dqDenom (spec 7.12.3)
13424+ // keyed by that true size, not the coded corner's -- so the corner
13425+ // goes into the top-left of a `side`-sized grid, not the reverse.
13426+ let levels = extend_corner(grid, tx_side, tx_side, side, side);
1342713427 let (dc_delta, ac_delta) = plane_q_delta(plane_idx, fctx);
1342813428 let tx = TxParams {
1342913429 w: side, h: side, bit_depth: bit_depth(fctx), q_idx: block_q_idx(fctx), dc_delta,
@@ -13432,7 +13432,7 @@ fn read_plane(
1343213432 if crate::envflags::env_flag!("EC_AV1_TRACE") {
1343313433 let residual = tx.run(&levels);
1343413434 eprintln!(
13435- "TRACE dequant plane={plane_idx} base_q_idx={base_q_idx} tx_type={tx_type:?} side={side} levels={levels:?} residual={residual:?}",
13435+ "TRACE dequant plane={plane_idx} base_q_idx={base_q_idx} tx_type={tx_type:?} side={side} levels={:?} residual={residual:?}", &*levels,
1343613436 );
1343713437 }
1343813438 push_intra_tx(
@@ -13946,8 +13946,8 @@ fn decode_block(
1394613946 let chroma_ctx_offset = if cn > 1 { Some(3) } else { None };
1394713947 let tpc = (n_axis / nchunk).max(1);
1394813948 let cpc = (cn / nchunk).max(1);
13949- let mut last_grids = [Vec::new(), Vec::new()];
13950- let mut units: Vec<((usize, usize), usize, Vec<i32>, Vec<i32>)> = Vec::new();
13949+ let mut last_grids = [Grid::Zero(0), Grid::Zero(0)];
13950+ let mut units: Vec<((usize, usize), usize, Grid, Grid)> = Vec::new();
1395113951 for ch_row in 0..nchunk {
1395213952 for ch_col in 0..nchunk {
1395313953 for tu_row in ch_row * tpc..(ch_row + 1) * tpc {
@@ -14309,8 +14309,8 @@ fn decode_block_128rect(
1430914309 let tpc = 64 / logical_tx;
1431014310 let zero_tu = vec![0i32; logical_tx * logical_tx];
1431114311 let zero_cu = vec![0i32; chroma_tx * chroma_tx];
14312- let mut last_grids = [Vec::new(), Vec::new()];
14313- let mut units: Vec<((usize, usize), Vec<i32>, Vec<i32>)> = Vec::new();
14312+ let mut last_grids = [Grid::Zero(0), Grid::Zero(0)];
14313+ let mut units: Vec<((usize, usize), Grid, Grid)> = Vec::new();
1431414314 for ch_row in 0..nch_y {
1431514315 for ch_col in 0..nch_x {
1431614316 for tu_row in ch_row * tpc..(ch_row + 1) * tpc {
@@ -14830,9 +14830,9 @@ fn decode_leaf8(
1483014830 None,
1483114831 smooth_neighbor_uv, fctx,
1483214832 );
14833- luma_grid = vec![0i32; 64];
14834- u_grid = vec![0i32; 16];
14835- v_grid = vec![0i32; 16];
14833+ luma_grid = Grid::Zero(64);
14834+ u_grid = Grid::Zero(16);
14835+ v_grid = Grid::Zero(16);
1483614836 } else if resolved != 4 {
1483714837 let around = neighbours.around_mi(leaf_mi, 8);
1483814838 if let Some(buf) = &palette_y_buf {
@@ -15370,7 +15370,7 @@ fn decode_leaf_split4(
1537015370 neighbours.above_uv_mode[c] = uv_predict_mode;
1537115371 neighbours.left_uv_mode[r] = uv_predict_mode;
1537215372 neighbours.record_uv_mode_mi(leaf_mi.0, leaf_mi.1, 2, 2, uv_predict_mode);
15373- let (u_grid, v_grid): (Vec<i32>, Vec<i32>) = if leaf_skips[3] {
15373+ let (u_grid, v_grid): (Grid, Grid) = if leaf_skips[3] {
1537415374 // A skipped chroma block still gets its CfL contribution: libaom's
1537515375 // `predict_and_reconstruct_intra_block` calls `cfl_predict_block`
1537615376 // from `av1_predict_intra_block_facade` (av1/common/reconintra.c:1719)
@@ -15386,7 +15386,7 @@ fn decode_leaf_split4(
1538615386 }
1538715387 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);
1538815388 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);
15389- (vec![0i32; 16], vec![0i32; 16])
15389+ (Grid::Zero(16), Grid::Zero(16))
1539015390 } else {
1539115391 let ac = alpha.map(|_| cfl_src(gpx, gpy, 8));
1539215392 let chroma_around = neighbours.around_mi(leaf_mi, 8);
@@ -15662,7 +15662,7 @@ fn decode_leaf_rect8(
1566215662 continue;
1566315663 }
1566415664 let grid = if skip {
15665- let zeros = vec![0i32; bw * bh];
15665+ let zeros = Grid::Zero(bw * bh);
1566615666 push_intra_rect(0,
1566715667 px, py, bw, bh, mode, angle_delta_y, reach, &zeros, None, filter_intra,
1566815668 smooth_neighbor, fctx,
@@ -15742,7 +15742,7 @@ fn decode_leaf_rect8(
1574215742 neighbours.above_uv_mode[c] = uv_predict_mode;
1574315743 neighbours.left_uv_mode[r] = uv_predict_mode;
1574415744 neighbours.record_uv_mode_mi(leaf_mi.0, leaf_mi.1, 2, 2, uv_predict_mode);
15745- let (u_grid, v_grid): (Vec<i32>, Vec<i32>) = if leaf_skips[1] {
15745+ let (u_grid, v_grid): (Grid, Grid) = if leaf_skips[1] {
1574615746 // A skipped chroma block still gets its CfL contribution: libaom's
1574715747 // `predict_and_reconstruct_intra_block` calls `cfl_predict_block`
1574815748 // from `av1_predict_intra_block_facade` (av1/common/reconintra.c:1719)
@@ -15758,7 +15758,7 @@ fn decode_leaf_rect8(
1575815758 }
1575915759 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);
1576015760 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);
15761- (vec![0i32; 16], vec![0i32; 16])
15761+ (Grid::Zero(16), Grid::Zero(16))
1576215762 } else {
1576315763 let ac = alpha.map(|_| cfl_src(gpx, gpy, 8));
1576415764 let chroma_around = neighbours.around_mi(leaf_mi, 8);
@@ -17676,7 +17676,7 @@ fn read_inter_plane_rect(
1767617676 prediction: Pred<'_>,
1767717677 inherited_luma_tx_type: Option<TxType>,
1767817678 luma_skip_ctx: Option<usize>, fctx: &crate::decode::FrameCtx,
17679-) -> Result<(Vec<i32>, TxType)> {
17679+) -> Result<(Grid, TxType)> {
1768017680 let skip_ctx = if plane_idx == 0 {
1768117681 // `get_txb_ctx_general`: 0 only when this unit IS the whole plane
1768217682 // block; a var-tx leaf passes its own neighbour-table context.
@@ -17733,15 +17733,7 @@ fn read_inter_plane_rect(
1773317733 default_tx_type,
1773417734 )?
1773517735 };
17736- let grid = if (cw, ch) == (w, h) {
17737- corner
17738- } else {
17739- let mut full = vec![0i32; w * h];
17740- for row in 0..ch {
17741- full[row * w..][..cw].copy_from_slice(&corner[row * cw..][..cw]);
17742- }
17743- full
17744- };
17736+ let grid = extend_corner(corner, cw, ch, w, h);
1774517737 let (dc_delta, ac_delta) = plane_q_delta(plane_idx, fctx);
1774617738 if crate::envflags::env_flag!("EC_TRACE_TXTYPE") {
1774717739 eprintln!("OUR_TXTYPE plane={plane_idx} x={x} y={y} w={w} h={h} tx_type={tx_type:?} inh={inherited_luma_tx_type:?}");
@@ -17757,6 +17749,24 @@ fn read_inter_plane_rect(
1775717749 Ok((grid, tx_type))
1775817750 }
1775917751
17752+/// The coded `cw x ch` corner of a transform zero-extended into its true
17753+/// `w x h` grid (a 64-length axis codes only its low 32 coefficients). A
17754+/// corner that coded nothing stays the shared zero grid -- no allocation
17755+/// (lane-coefgrid).
17756+fn extend_corner(corner: Grid, cw: usize, ch: usize, w: usize, h: usize) -> Grid {
17757+ if (cw, ch) == (w, h) {
17758+ return corner;
17759+ }
17760+ if let Grid::Zero(_) = corner {
17761+ return Grid::Zero(w * h);
17762+ }
17763+ let mut full = vec![0i32; w * h];
17764+ for row in 0..ch {
17765+ full[row * w..][..cw].copy_from_slice(&corner[row * cw..][..cw]);
17766+ }
17767+ Grid::Own(full)
17768+}
17769+
1776017770 /// A rect coefficient grid re-laid into the square `stride x stride` grid the
1776117771 /// neighbour/deblock stamps downstream of the residual expect.
1776217772 fn embed_rect_grid(grid: &[i32], w: usize, h: usize, stride: usize) -> Vec<i32> {
@@ -17791,7 +17801,7 @@ fn read_inter_luma8(
1779117801 scan4: &[u16],
1779217802 reduced_tx_set: bool,
1779317803 split: bool, fctx: &crate::decode::FrameCtx,
17794-) -> Result<(Vec<i32>, TxType)> {
17804+) -> Result<(Grid, TxType)> {
1779517805 if !split {
1779617806 return read_inter_plane(
1779717807 dec,
@@ -17848,7 +17858,7 @@ fn read_inter_luma8(
1784817858 neighbours.record_mi_luma(tu_mi, 4, &tu_grid);
1784917859 }
1785017860 }
17851- Ok((vec![0i32; 64], first))
17861+ Ok((Grid::Zero(64), first))
1785217862 }
1785317863
1785417864 /// The [`TxbSet`] one leaf of an inter block's var-tx tree reads its
@@ -23085,7 +23095,7 @@ fn read_inter_plane(
2308523095 // transform every non-var-tx caller reads, whose plane-0 context is the
2308623096 // lone-transform-unit 0.
2308723097 luma_skip_ctx: Option<usize>, fctx: &crate::decode::FrameCtx,
23088-) -> Result<(Vec<i32>, TxType)> {
23098+) -> Result<(Grid, TxType)> {
2308923099 let skip_ctx = if plane_idx == 0 {
2309023100 luma_skip_ctx.unwrap_or(0)
2309123101 } else {
@@ -23123,15 +23133,7 @@ fn read_inter_plane(
2312323133 // [`read_plane`]'s own `tx_side != side` branch. Every other transform
2312423134 // this decoder codes has `tx_side == side`, so this is inert below 64.
2312523135 let tx_side = side.min(32);
23126- let grid = if tx_side == side {
23127- grid
23128- } else {
23129- let mut full = vec![0i32; side * side];
23130- for row in 0..tx_side {
23131- full[row * side..][..tx_side].copy_from_slice(&grid[row * tx_side..][..tx_side]);
23132- }
23133- full
23134- };
23136+ let grid = extend_corner(grid, tx_side, tx_side, side, side);
2313523137 let (dc_delta, ac_delta) = plane_q_delta(plane_idx, fctx);
2313623138 let tx = TxParams {
2313723139 w: side, h: side, bit_depth: bit_depth(fctx), q_idx: block_q_idx(fctx), dc_delta,
@@ -25928,7 +25930,7 @@ fn decode_inter_block(
2592825930 None,
2592925931 None, fctx,
2593025932 )?;
25931- luma_grid = Grid::Own(coded.0);
25933+ luma_grid = coded.0;
2593225934 luma_tx_type = coded.1;
2593325935 }
2593425936 }
@@ -25997,7 +25999,7 @@ fn decode_inter_block(
2599725999 chroma_side,
2599826000 ));
2599926001 } else {
26000- u_grid = Grid::Own(read_inter_plane(
26002+ u_grid = read_inter_plane(
2600126003 dec,
2600226004 cdfs,
2600326005 chroma_set,
@@ -26014,8 +26016,8 @@ fn decode_inter_block(
2601426016 Some(luma_tx_type),
2601526017 None, fctx,
2601626018 )?
26017- .0);
26018- v_grid = Grid::Own(read_inter_plane(
26019+ .0;
26020+ v_grid = read_inter_plane(
2601926021 dec,
2602026022 cdfs,
2602126023 chroma_set,
@@ -26032,7 +26034,7 @@ fn decode_inter_block(
2603226034 Some(luma_tx_type),
2603326035 None, fctx,
2603426036 )?
26035- .0);
26037+ .0;
2603626038 }
2603726039 }
2603826040 } else {
@@ -27202,7 +27204,7 @@ fn decode_inter_block(
2720227204 None,
2720327205 None, fctx,
2720427206 )?;
27205- luma_grid = Grid::Own(coded.0);
27207+ luma_grid = coded.0;
2720627208 luma_tx_type = coded.1;
2720727209 }
2720827210 }
@@ -27280,7 +27282,7 @@ fn decode_inter_block(
2728027282 chroma_side,
2728127283 ));
2728227284 } else {
27283- u_grid = Grid::Own(read_inter_plane(
27285+ u_grid = read_inter_plane(
2728427286 dec,
2728527287 cdfs,
2728627288 chroma_set,
@@ -27297,8 +27299,8 @@ fn decode_inter_block(
2729727299 Some(luma_tx_type),
2729827300 None, fctx,
2729927301 )?
27300- .0);
27301- v_grid = Grid::Own(read_inter_plane(
27302+ .0;
27303+ v_grid = read_inter_plane(
2730227304 dec,
2730327305 cdfs,
2730427306 chroma_set,
@@ -27315,7 +27317,7 @@ fn decode_inter_block(
2731527317 Some(luma_tx_type),
2731627318 None, fctx,
2731727319 )?
27318- .0);
27320+ .0;
2731927321 }
2732027322 }
2732127323 }
@@ -27741,7 +27743,7 @@ fn decode_inter_block(
2774127743 // so `txb_skip_ctx` is the neighbour-magnitude table, not
2774227744 // the lone-TU 0 (`get_txb_ctx_general`).
2774327745 let tu_skip_ctx = neighbours.luma_skip_ctx(tu_mi, tx_px / MI);
27744- Grid::Own(read_plane(
27746+ read_plane(
2774527747 dec,
2774627748 cdfs,
2774727749 txbset_for(tx_px, reduced),
@@ -27766,7 +27768,7 @@ fn decode_inter_block(
2776627768 filter_intra,
2776727769 Some(tu_skip_ctx),
2776827770 smooth_neighbor, fctx,
27769- )?)
27771+ )?
2777027772 };
2777127773 neighbours.record_mi_luma(tu_mi, tx_px, &tu_grid);
2777227774 if side > 64 {
@@ -27807,7 +27809,7 @@ fn decode_inter_block(
2780727809 );
2780827810 Grid::Zero(cu_tx * cu_tx)
2780927811 } else {
27810- Grid::Own(read_plane(
27812+ read_plane(
2781127813 dec,
2781227814 cdfs,
2781327815 chroma_set,
@@ -27834,7 +27836,7 @@ fn decode_inter_block(
2783427836 // (lane-sb128b r3).
2783527837 Some(3),
2783627838 smooth_neighbor_uv, fctx,
27837- )?)
27839+ )?
2783827840 };
2783927841 // Immediately, so the NEXT chunk's unit reads this
2784027842 // one's coefficient context.
@@ -27932,7 +27934,7 @@ fn decode_inter_block(
2793227934 if let Some(buf) = &palette_y_buf {
2793327935 set_palette_pred(buf.clone(), fctx);
2793427936 }
27935- luma_grid = Grid::Own(read_plane(
27937+ luma_grid = read_plane(
2793627938 dec,
2793727939 cdfs,
2793827940 luma_set_intra,
@@ -27953,13 +27955,13 @@ fn decode_inter_block(
2795327955 filter_intra,
2795427956 None,
2795527957 smooth_neighbor, fctx,
27956- )?);
27958+ )?;
2795727959 }
2795827960 let ac = alpha.map(|_| cfl_src(px, py, side));
2795927961 if let Some((ub, _)) = &palette_uv_bufs {
2796027962 set_palette_pred(ub.clone(), fctx);
2796127963 }
27962- u_grid = Grid::Own(read_plane(
27964+ u_grid = read_plane(
2796327965 dec,
2796427966 cdfs,
2796527967 chroma_set,
@@ -27980,11 +27982,11 @@ fn decode_inter_block(
2798027982 None,
2798127983 None,
2798227984 smooth_neighbor_uv, fctx,
27983- )?);
27985+ )?;
2798427986 if let Some((_, vb)) = &palette_uv_bufs {
2798527987 set_palette_pred(vb.clone(), fctx);
2798627988 }
27987- v_grid = Grid::Own(read_plane(
27989+ v_grid = read_plane(
2798827990 dec,
2798927991 cdfs,
2799027992 chroma_set,
@@ -28005,7 +28007,7 @@ fn decode_inter_block(
2800528007 None,
2800628008 None,
2800728009 smooth_neighbor_uv, fctx,
28008- )?);
28010+ )?;
2800928011 }
2801028012 }
2801128013 // lane-comppin r9: end-of-block checkpoint -- everything between
@@ -28037,7 +28039,7 @@ fn decode_inter_block(
2803728039 // the partial right superblock read row 1 where libaom reads row 2). The
2803828040 // units are collected here and re-stamped after that record, in mu-chunk
2803928041 // order so a later unit still wins over an earlier one.
28040- let mu_chroma_units: Vec<((usize, usize), usize, Vec<i32>)> = if mu_chroma {
28042+ let mu_chroma_units: Vec<((usize, usize), usize, Grid)> = if mu_chroma {
2804128043 let cu = 32usize;
2804228044 let mut units = Vec::new();
2804328045 for cr in 0..(write_h / 64).max(1) {
@@ -28049,7 +28051,7 @@ fn decode_inter_block(
2804928051 let start = (cr * cu + rr) * chroma_side + cc * cu;
2805028052 unit.extend_from_slice(&grid[start..start + cu]);
2805128053 }
28052- units.push((cu_mi, plane, unit));
28054+ units.push((cu_mi, plane, Grid::Own(unit)));
2805328055 }
2805428056 }
2805528057 }
@@ -28729,7 +28731,7 @@ fn decode_inter_sub8_split4(
2872928731 let (u_grid, v_grid) = if last_skip {
2873028732 push_mc(1, cpx, cpy, B4, Pred::dense(&pred_u), &ZERO_RESIDUAL[..B4 * B4], fctx);
2873128733 push_mc(2, cpx, cpy, B4, Pred::dense(&pred_v), &ZERO_RESIDUAL[..B4 * B4], fctx);
28732- (vec![0i32; B4 * B4], vec![0i32; B4 * B4])
28734+ (Grid::Zero(B4 * B4), Grid::Zero(B4 * B4))
2873328735 } else {
2873428736 let around = neighbours.around_mi(group_mi, 8);
2873528737 let ug = read_inter_plane(
@@ -29036,7 +29038,7 @@ fn decode_intra_sub8_leaf(
2903629038 }
2903729039 } else {
2903829040 let grid_levels = if skip {
29039- let zeros = vec![0i32; bw * bh];
29041+ let zeros = Grid::Zero(bw * bh);
2904029042 push_intra_rect(0,
2904129043 px, py, bw, bh, mode, angle_delta_y, reach, &zeros, None, filter_intra,
2904229044 smooth_neighbor, fctx,
@@ -29166,14 +29168,14 @@ fn decode_intra_sub8_leaf(
2916629168 record_strip_palette(neighbours, (gr, gc), 8, 8, 0, [0u16; 8], 0, [0u16; 8]);
2916729169
2916829170 let ac = alpha.map(|_| cfl_src(gpx, gpy, 8));
29169- let (u_grid, v_grid): (Vec<i32>, Vec<i32>) = if skip {
29171+ let (u_grid, v_grid): (Grid, Grid) = if skip {
2917029172 if alpha.is_some() {
2917129173 CHROMA_SKIP_CFL_HITS.with(|h| h.set(h.get() + 1));
2917229174 SKIP_CFL_HITS.with(|c| c.set(c.get() + 1));
2917329175 }
2917429176 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);
2917529177 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);
29176- (vec![0i32; 16], vec![0i32; 16])
29178+ (Grid::Zero(16), Grid::Zero(16))
2917729179 } else {
2917829180 let chroma_around = neighbours.around_mi(group_mi, 8);
2917929181 let ug = read_plane(
@@ -29751,7 +29753,7 @@ fn decode_inter_sub8_rect2(
2975129753 let (u_grid, v_grid) = if last_skip {
2975229754 push_mc(1, cpx, cpy, B4, Pred::dense(&pred_u), &ZERO_RESIDUAL[..B4 * B4], fctx);
2975329755 push_mc(2, cpx, cpy, B4, Pred::dense(&pred_v), &ZERO_RESIDUAL[..B4 * B4], fctx);
29754- (vec![0i32; B4 * B4], vec![0i32; B4 * B4])
29756+ (Grid::Zero(B4 * B4), Grid::Zero(B4 * B4))
2975529757 } else {
2975629758 let around = neighbours.around_mi(group_mi, 8);
2975729759 let ug = read_inter_plane(
@@ -30597,9 +30599,9 @@ fn decode_inter_block8(
3059730599 Pred::dense(&pred_v),
3059830600 &ZERO_RESIDUAL[..CHROMA_SIDE * CHROMA_SIDE], fctx,
3059930601 );
30600- luma_grid = vec![0i32; SIDE * SIDE];
30601- u_grid = vec![0i32; CHROMA_SIDE * CHROMA_SIDE];
30602- v_grid = vec![0i32; CHROMA_SIDE * CHROMA_SIDE];
30602+ luma_grid = Grid::Zero(SIDE * SIDE);
30603+ u_grid = Grid::Zero(CHROMA_SIDE * CHROMA_SIDE);
30604+ v_grid = Grid::Zero(CHROMA_SIDE * CHROMA_SIDE);
3060330605 } else {
3060430606 let around = neighbours.around_mi(leaf_mi, 8);
3060530607 let (grid8, luma_tx_type) = read_inter_luma8(
@@ -31245,9 +31247,9 @@ fn decode_inter_block8(
3124531247 Pred::dense(&pred_v),
3124631248 &ZERO_RESIDUAL[..CHROMA_SIDE * CHROMA_SIDE], fctx,
3124731249 );
31248- luma_grid = vec![0i32; SIDE * SIDE];
31249- u_grid = vec![0i32; CHROMA_SIDE * CHROMA_SIDE];
31250- v_grid = vec![0i32; CHROMA_SIDE * CHROMA_SIDE];
31250+ luma_grid = Grid::Zero(SIDE * SIDE);
31251+ u_grid = Grid::Zero(CHROMA_SIDE * CHROMA_SIDE);
31252+ v_grid = Grid::Zero(CHROMA_SIDE * CHROMA_SIDE);
3125131253 } else {
3125231254 let around = neighbours.around_mi(leaf_mi, 8);
3125331255 let (grid8, luma_tx_type) = read_inter_luma8(
@@ -31611,7 +31613,7 @@ fn decode_inter_block8(
3161131613 filter_intra,
3161231614 smooth_neighbor, fctx,
3161331615 );
31614- vec![0i32; tx_px * tx_px]
31616+ Grid::Zero(tx_px * tx_px)
3161531617 } else {
3161631618 // The unit is smaller than the block it sits in, so
3161731619 // `txb_skip_ctx` is the neighbour-magnitude table, not
@@ -31698,16 +31700,16 @@ fn decode_inter_block8(
3169831700 None,
3169931701 smooth_neighbor_uv, fctx,
3170031702 );
31701- luma_grid = vec![0i32; SIDE * SIDE];
31702- u_grid = vec![0i32; CHROMA_SIDE * CHROMA_SIDE];
31703- v_grid = vec![0i32; CHROMA_SIDE * CHROMA_SIDE];
31703+ luma_grid = Grid::Zero(SIDE * SIDE);
31704+ u_grid = Grid::Zero(CHROMA_SIDE * CHROMA_SIDE);
31705+ v_grid = Grid::Zero(CHROMA_SIDE * CHROMA_SIDE);
3170431706 } else {
3170531707 let around = neighbours.around_mi(leaf_mi, 8);
3170631708 luma_grid = if split8 {
3170731709 // Already coded and reconstructed unit by unit above; the
3170831710 // block-level grid only feeds the neighbour write-back,
3170931711 // which the `saved_luma_ctx` restore below keeps per-TU.
31710- vec![0i32; SIDE * SIDE]
31712+ Grid::Zero(SIDE * SIDE)
3171131713 } else {
3171231714 if let Some(buf) = &palette_y_buf {
3171331715 set_palette_pred(buf.clone(), fctx);