diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index 62fbad77..2ac3a6df 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -2140,7 +2140,7 @@ impl Residual { /// the empty marker, so neither the grid nor the residual is cloned and /// the worker never runs the transform (60% of the 4K segment's units). fn coeffs(tx: TxParams, grid: &[i32], fctx: &crate::decode::FrameCtx) -> Self { - if grid.iter().all(|&c| c == 0) { + if is_zero_grid(grid) { return Self::Done(0, 0); } 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 { /// and the reconstruction loops below read this instead of a per-unit /// `vec![0; w * h]`. Sized for the largest stride a recorded unit can carry /// (a 128-wide inter block's prediction stride). -static ZERO_RESIDUAL: [i32; 128 * 128] = [0; 128 * 128]; +pub(crate) static ZERO_RESIDUAL: [i32; 128 * 128] = [0; 128 * 128]; /// lane-blockscratch: one plane's coefficient levels as the block-tail /// neighbour writes want them. A plane that coded nothing (a skipped block, a /// strip with no chroma, luma under a var-tx split whose units already wrote /// themselves) names the shared [`ZERO_RESIDUAL`] instead of allocating and /// zeroing up to 64 KB of its own per block. +#[derive(Clone)] enum Grid { Zero(usize), Own(Vec), @@ -2230,6 +2231,13 @@ impl std::ops::Deref for Grid { } } +/// Whether a coefficient grid codes nothing: a [`Grid::Zero`] unit IS the +/// shared [`ZERO_RESIDUAL`], so one pointer compare answers the 60% of +/// transform units that code no coefficient without the scan (lane-coefgrid). +pub(crate) fn is_zero_grid(grid: &[i32]) -> bool { + std::ptr::eq(grid.as_ptr(), ZERO_RESIDUAL.as_ptr()) || grid.iter().all(|&c| c == 0) +} + /// `residual`, or `n` zeros when it is the empty all-zero marker. fn dense_residual(residual: &[i32], n: usize) -> &[i32] { if residual.is_empty() { &ZERO_RESIDUAL[..n] } else { residual } @@ -5897,10 +5905,9 @@ fn read_coeffs( // root cause, see [`base_ctx`]). `None` everywhere else -- every other // caller's block genuinely is `side` x `side`. rect_shape: Option<(usize, usize)>, -) -> Result<(Vec, TxType)> { +) -> Result<(Grid, TxType)> { let trace = crate::envflags::env_flag!("EC_AV1_TRACE"); let side = coding.side; - let mut grid = vec![0i32; side * side]; if crate::envflags::env_flag!("EC_AV1_EOBPT_CDF") { let (range, value) = dec.debug_state(); eprintln!("EC_AV1_STATE_BEFORE_TXBSKIP range={range} value={value}"); @@ -5933,8 +5940,11 @@ fn read_coeffs( eprintln!("EC_AV1_STATE_AFTER_TXBSKIP range={range} value={value}"); } if all_zero { - return Ok((grid, TxType::DctDct)); + // lane-coefgrid: the all-zero unit (60% of the 4K segment's) names the + // shared zero grid instead of allocating and zeroing its own. + return Ok((Grid::Zero(side * side), TxType::DctDct)); } + let mut grid = vec![0i32; side * side]; let mut tx_type = default_tx_type; if let Some(tx_type_cdf) = coding.tx_type.as_deref_mut() { // The CDF row's own width names its set (lane-cdffwd2: extended @@ -6116,7 +6126,7 @@ fn read_coeffs( } grid[pos] = if negative { -level } else { level }; } - Ok((grid, tx_type)) + Ok((Grid::Own(grid), tx_type)) } /// [`read_coeffs`] widened to `(w, h)` (lane-rectwire; lane-rectx added the @@ -6136,9 +6146,8 @@ fn read_coeffs_rect( skip_ctx: usize, sign_ctx: usize, default_tx_type: TxType, -) -> Result<(Vec, TxType)> { +) -> Result<(Grid, TxType)> { let rect_trace = coeff_trace_on(); - let mut grid = vec![0i32; w * h]; let entry_rng = dec.debug_state().0; let all_zero = dec.symbol(&mut coding.txb_skip[skip_ctx]) == 1; if rect_trace { @@ -6149,8 +6158,9 @@ fn read_coeffs_rect( ); } if all_zero { - return Ok((grid, TxType::DctDct)); + return Ok((Grid::Zero(w * h), TxType::DctDct)); } + let mut grid = vec![0i32; w * h]; RECT_COEFF_TU_HITS.with(|c| c.set(c.get() + 1)); // lane-tx4x8: a 4x8/8x4 luma TU DOES carry a `tx_type` symbol (its set is // `EXT_TX_SET_DTT4_IDTX(_1DDCT)`, `tx_size_sqr_up == TX_8X8`), including @@ -6288,7 +6298,7 @@ fn read_coeffs_rect( }; grid[pos] = if negative { -level } else { level }; } - Ok((grid, tx_type)) + Ok((Grid::Own(grid), tx_type)) } /// What one coded block leaves behind for the blocks that read it as a @@ -6324,6 +6334,12 @@ fn fill_span(dst: &mut [T], start: usize, n: usize, v: T) { } fn neighbour_state(grid: &[i32]) -> Neighbour { + // lane-coefgrid: the shared zero grid sums to zero by construction; this + // sum has no early exit, so the 60% of units that code nothing used to + // walk their whole grid here. + if is_zero_grid(grid) { + return Neighbour::default(); + } let cul: u32 = grid.iter().map(|&l| l.unsigned_abs()).sum(); Neighbour { level: cul.min(7) as u8, @@ -9223,7 +9239,7 @@ fn decode_rect_split( 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]; + let zero = Grid::Zero(chroma_w * chroma_h); if let Some((ub, _)) = &m.palette_uv { set_palette_pred(ub.clone(), fctx); } @@ -9250,7 +9266,7 @@ fn decode_rect_split( default_intra_tx_type(m.uv_predict_mode as u8) }; let around = neighbours.around_mi_rect(at_mi, bw, bh); - let mut grids: Vec> = Vec::with_capacity(2); + let mut grids: Vec = Vec::with_capacity(2); for plane_idx in 1..=2 { let skip_ctx = usize::from(around[plane_idx].0) + usize::from(around[plane_idx].1); @@ -10719,9 +10735,9 @@ fn decode_leaf_rect( let (chroma_w, chroma_h) = (bw / 2, bh / 2); let (luma_levels, u_levels, v_levels); if skip { - luma_levels = vec![0i32; bw * bh]; - u_levels = vec![0i32; chroma_w * chroma_h]; - v_levels = vec![0i32; chroma_w * chroma_h]; + luma_levels = Grid::Zero(bw * bh); + u_levels = Grid::Zero(chroma_w * chroma_h); + v_levels = Grid::Zero(chroma_w * chroma_h); if let Some(b) = &palette_y_buf { set_palette_pred(b.clone(), fctx); } @@ -11647,7 +11663,7 @@ fn decode_rect4_16_strip( } } else { let levels = if skip { - let zeros = vec![0i32; bw * bh]; + let zeros = Grid::Zero(bw * bh); if let Some(b) = &palette_y_buf { set_palette_pred(b.clone(), fctx); } @@ -11748,7 +11764,7 @@ fn decode_rect4_16_strip( RECT4_16_UV_PAIR_FILT_HITS.with(|h| h.set(h.get() + 1)); } let (u_levels, v_levels) = if skip { - let zeros = vec![0i32; cw * ch]; + let zeros = Grid::Zero(cw * ch); // libaom `predict_and_reconstruct_intra_block` (decodeframe.c // ~1000) predicts EVERY plane and only guards the residual on // `mbmi->skip_txfm`, so a skipped `UV_CFL_PRED` block is still @@ -13350,7 +13366,7 @@ fn read_plane( luma_skip_ctx: Option, // See [`PlaneBuf::reconstruct`]'s own doc: caller's neighbour-smooth check. smooth_neighbor: bool, fctx: &crate::decode::FrameCtx, -) -> Result> { +) -> Result { let skip_ctx = if plane_idx == 0 { luma_skip_ctx.unwrap_or(0) } else { @@ -13410,19 +13426,11 @@ fn read_plane( // top-left 32x32 of frequencies are coded (spec 5.11.40); the rest of the // dequantized grid stays zero, which `inverse_transform_2d`'s own `< 32` // guard also assumes. - let levels = if tx_side == side { - grid - } else { - // `grid` is `tx_side x tx_side` (the coded corner); `dequant_and_inverse` - // wants the true `side x side` transform, with dqDenom (spec 7.12.3) - // keyed by that true size, not the coded corner's -- so the corner - // goes into the top-left of a `side`-sized grid, not the reverse. - let mut full = vec![0i32; side * side]; - for row in 0..tx_side { - full[row * side..][..tx_side].copy_from_slice(&grid[row * tx_side..][..tx_side]); - } - full - }; + // `grid` is `tx_side x tx_side` (the coded corner); `dequant_and_inverse` + // wants the true `side x side` transform, with dqDenom (spec 7.12.3) + // keyed by that true size, not the coded corner's -- so the corner + // goes into the top-left of a `side`-sized grid, not the reverse. + let levels = extend_corner(grid, tx_side, tx_side, side, side); let (dc_delta, ac_delta) = plane_q_delta(plane_idx, fctx); let tx = TxParams { w: side, h: side, bit_depth: bit_depth(fctx), q_idx: block_q_idx(fctx), dc_delta, @@ -13431,7 +13439,7 @@ fn read_plane( if crate::envflags::env_flag!("EC_AV1_TRACE") { let residual = tx.run(&levels); eprintln!( - "TRACE dequant plane={plane_idx} base_q_idx={base_q_idx} tx_type={tx_type:?} side={side} levels={levels:?} residual={residual:?}", + "TRACE dequant plane={plane_idx} base_q_idx={base_q_idx} tx_type={tx_type:?} side={side} levels={:?} residual={residual:?}", &*levels, ); } push_intra_tx( @@ -13945,8 +13953,8 @@ fn decode_block( let chroma_ctx_offset = if cn > 1 { Some(3) } else { None }; let tpc = (n_axis / nchunk).max(1); let cpc = (cn / nchunk).max(1); - let mut last_grids = [Vec::new(), Vec::new()]; - let mut units: Vec<((usize, usize), usize, Vec, Vec)> = Vec::new(); + let mut last_grids = [Grid::Zero(0), Grid::Zero(0)]; + let mut units: Vec<((usize, usize), usize, Grid, Grid)> = Vec::new(); for ch_row in 0..nchunk { for ch_col in 0..nchunk { for tu_row in ch_row * tpc..(ch_row + 1) * tpc { @@ -14308,8 +14316,8 @@ fn decode_block_128rect( let tpc = 64 / logical_tx; let zero_tu = vec![0i32; logical_tx * logical_tx]; let zero_cu = vec![0i32; chroma_tx * chroma_tx]; - let mut last_grids = [Vec::new(), Vec::new()]; - let mut units: Vec<((usize, usize), Vec, Vec)> = Vec::new(); + let mut last_grids = [Grid::Zero(0), Grid::Zero(0)]; + let mut units: Vec<((usize, usize), Grid, Grid)> = Vec::new(); for ch_row in 0..nch_y { for ch_col in 0..nch_x { for tu_row in ch_row * tpc..(ch_row + 1) * tpc { @@ -14829,9 +14837,9 @@ fn decode_leaf8( None, smooth_neighbor_uv, fctx, ); - luma_grid = vec![0i32; 64]; - u_grid = vec![0i32; 16]; - v_grid = vec![0i32; 16]; + luma_grid = Grid::Zero(64); + u_grid = Grid::Zero(16); + v_grid = Grid::Zero(16); } else if resolved != 4 { let around = neighbours.around_mi(leaf_mi, 8); if let Some(buf) = &palette_y_buf { @@ -15369,7 +15377,7 @@ fn decode_leaf_split4( neighbours.above_uv_mode[c] = uv_predict_mode; neighbours.left_uv_mode[r] = uv_predict_mode; neighbours.record_uv_mode_mi(leaf_mi.0, leaf_mi.1, 2, 2, uv_predict_mode); - let (u_grid, v_grid): (Vec, Vec) = if leaf_skips[3] { + let (u_grid, v_grid): (Grid, Grid) = if leaf_skips[3] { // A skipped chroma block still gets its CfL contribution: libaom's // `predict_and_reconstruct_intra_block` calls `cfl_predict_block` // from `av1_predict_intra_block_facade` (av1/common/reconintra.c:1719) @@ -15385,7 +15393,7 @@ fn decode_leaf_split4( } 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); 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); - (vec![0i32; 16], vec![0i32; 16]) + (Grid::Zero(16), Grid::Zero(16)) } else { let ac = alpha.map(|_| cfl_src(gpx, gpy, 8)); let chroma_around = neighbours.around_mi(leaf_mi, 8); @@ -15661,7 +15669,7 @@ fn decode_leaf_rect8( continue; } let grid = if skip { - let zeros = vec![0i32; bw * bh]; + let zeros = Grid::Zero(bw * bh); push_intra_rect(0, px, py, bw, bh, mode, angle_delta_y, reach, &zeros, None, filter_intra, smooth_neighbor, fctx, @@ -15741,7 +15749,7 @@ fn decode_leaf_rect8( neighbours.above_uv_mode[c] = uv_predict_mode; neighbours.left_uv_mode[r] = uv_predict_mode; neighbours.record_uv_mode_mi(leaf_mi.0, leaf_mi.1, 2, 2, uv_predict_mode); - let (u_grid, v_grid): (Vec, Vec) = if leaf_skips[1] { + let (u_grid, v_grid): (Grid, Grid) = if leaf_skips[1] { // A skipped chroma block still gets its CfL contribution: libaom's // `predict_and_reconstruct_intra_block` calls `cfl_predict_block` // from `av1_predict_intra_block_facade` (av1/common/reconintra.c:1719) @@ -15757,7 +15765,7 @@ fn decode_leaf_rect8( } 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); 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); - (vec![0i32; 16], vec![0i32; 16]) + (Grid::Zero(16), Grid::Zero(16)) } else { let ac = alpha.map(|_| cfl_src(gpx, gpy, 8)); let chroma_around = neighbours.around_mi(leaf_mi, 8); @@ -17675,7 +17683,7 @@ fn read_inter_plane_rect( prediction: Pred<'_>, inherited_luma_tx_type: Option, luma_skip_ctx: Option, fctx: &crate::decode::FrameCtx, -) -> Result<(Vec, TxType)> { +) -> Result<(Grid, TxType)> { let skip_ctx = if plane_idx == 0 { // `get_txb_ctx_general`: 0 only when this unit IS the whole plane // block; a var-tx leaf passes its own neighbour-table context. @@ -17732,15 +17740,7 @@ fn read_inter_plane_rect( default_tx_type, )? }; - let grid = if (cw, ch) == (w, h) { - corner - } else { - let mut full = vec![0i32; w * h]; - for row in 0..ch { - full[row * w..][..cw].copy_from_slice(&corner[row * cw..][..cw]); - } - full - }; + let grid = extend_corner(corner, cw, ch, w, h); let (dc_delta, ac_delta) = plane_q_delta(plane_idx, fctx); if crate::envflags::env_flag!("EC_TRACE_TXTYPE") { 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( Ok((grid, tx_type)) } +/// The coded `cw x ch` corner of a transform zero-extended into its true +/// `w x h` grid (a 64-length axis codes only its low 32 coefficients). A +/// corner that coded nothing stays the shared zero grid -- no allocation +/// (lane-coefgrid). +fn extend_corner(corner: Grid, cw: usize, ch: usize, w: usize, h: usize) -> Grid { + if (cw, ch) == (w, h) { + return corner; + } + if let Grid::Zero(_) = corner { + return Grid::Zero(w * h); + } + let mut full = vec![0i32; w * h]; + for row in 0..ch { + full[row * w..][..cw].copy_from_slice(&corner[row * cw..][..cw]); + } + Grid::Own(full) +} + /// A rect coefficient grid re-laid into the square `stride x stride` grid the /// neighbour/deblock stamps downstream of the residual expect. fn embed_rect_grid(grid: &[i32], w: usize, h: usize, stride: usize) -> Vec { @@ -17790,7 +17808,7 @@ fn read_inter_luma8( scan4: &[u16], reduced_tx_set: bool, split: bool, fctx: &crate::decode::FrameCtx, -) -> Result<(Vec, TxType)> { +) -> Result<(Grid, TxType)> { if !split { return read_inter_plane( dec, @@ -17847,7 +17865,7 @@ fn read_inter_luma8( neighbours.record_mi_luma(tu_mi, 4, &tu_grid); } } - Ok((vec![0i32; 64], first)) + Ok((Grid::Zero(64), first)) } /// The [`TxbSet`] one leaf of an inter block's var-tx tree reads its @@ -23084,7 +23102,7 @@ fn read_inter_plane( // transform every non-var-tx caller reads, whose plane-0 context is the // lone-transform-unit 0. luma_skip_ctx: Option, fctx: &crate::decode::FrameCtx, -) -> Result<(Vec, TxType)> { +) -> Result<(Grid, TxType)> { let skip_ctx = if plane_idx == 0 { luma_skip_ctx.unwrap_or(0) } else { @@ -23122,15 +23140,7 @@ fn read_inter_plane( // [`read_plane`]'s own `tx_side != side` branch. Every other transform // this decoder codes has `tx_side == side`, so this is inert below 64. let tx_side = side.min(32); - let grid = if tx_side == side { - grid - } else { - let mut full = vec![0i32; side * side]; - for row in 0..tx_side { - full[row * side..][..tx_side].copy_from_slice(&grid[row * tx_side..][..tx_side]); - } - full - }; + let grid = extend_corner(grid, tx_side, tx_side, side, side); let (dc_delta, ac_delta) = plane_q_delta(plane_idx, fctx); let tx = TxParams { 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( None, None, fctx, )?; - luma_grid = Grid::Own(coded.0); + luma_grid = coded.0; luma_tx_type = coded.1; } } @@ -26087,7 +26097,7 @@ fn decode_inter_block( chroma_side, )); } else { - u_grid = Grid::Own(read_inter_plane( + u_grid = read_inter_plane( dec, cdfs, chroma_set, @@ -26104,8 +26114,8 @@ fn decode_inter_block( Some(luma_tx_type), None, fctx, )? - .0); - v_grid = Grid::Own(read_inter_plane( + .0; + v_grid = read_inter_plane( dec, cdfs, chroma_set, @@ -26122,7 +26132,7 @@ fn decode_inter_block( Some(luma_tx_type), None, fctx, )? - .0); + .0; } } } else { @@ -27277,7 +27287,7 @@ fn decode_inter_block( None, None, fctx, )?; - luma_grid = Grid::Own(coded.0); + luma_grid = coded.0; luma_tx_type = coded.1; } } @@ -27355,7 +27365,7 @@ fn decode_inter_block( chroma_side, )); } else { - u_grid = Grid::Own(read_inter_plane( + u_grid = read_inter_plane( dec, cdfs, chroma_set, @@ -27372,8 +27382,8 @@ fn decode_inter_block( Some(luma_tx_type), None, fctx, )? - .0); - v_grid = Grid::Own(read_inter_plane( + .0; + v_grid = read_inter_plane( dec, cdfs, chroma_set, @@ -27390,7 +27400,7 @@ fn decode_inter_block( Some(luma_tx_type), None, fctx, )? - .0); + .0; } } } @@ -27816,7 +27826,7 @@ fn decode_inter_block( // so `txb_skip_ctx` is the neighbour-magnitude table, not // the lone-TU 0 (`get_txb_ctx_general`). let tu_skip_ctx = neighbours.luma_skip_ctx(tu_mi, tx_px / MI); - Grid::Own(read_plane( + read_plane( dec, cdfs, txbset_for(tx_px, reduced), @@ -27841,7 +27851,7 @@ fn decode_inter_block( filter_intra, Some(tu_skip_ctx), smooth_neighbor, fctx, - )?) + )? }; neighbours.record_mi_luma(tu_mi, tx_px, &tu_grid); if side > 64 { @@ -27882,7 +27892,7 @@ fn decode_inter_block( ); Grid::Zero(cu_tx * cu_tx) } else { - Grid::Own(read_plane( + read_plane( dec, cdfs, chroma_set, @@ -27909,7 +27919,7 @@ fn decode_inter_block( // (lane-sb128b r3). Some(3), smooth_neighbor_uv, fctx, - )?) + )? }; // Immediately, so the NEXT chunk's unit reads this // one's coefficient context. @@ -28007,7 +28017,7 @@ fn decode_inter_block( if let Some(buf) = &palette_y_buf { set_palette_pred(buf.clone(), fctx); } - luma_grid = Grid::Own(read_plane( + luma_grid = read_plane( dec, cdfs, luma_set_intra, @@ -28028,13 +28038,13 @@ fn decode_inter_block( filter_intra, None, smooth_neighbor, fctx, - )?); + )?; } let ac = alpha.map(|_| cfl_src(px, py, side)); if let Some((ub, _)) = &palette_uv_bufs { set_palette_pred(ub.clone(), fctx); } - u_grid = Grid::Own(read_plane( + u_grid = read_plane( dec, cdfs, chroma_set, @@ -28055,11 +28065,11 @@ fn decode_inter_block( None, None, smooth_neighbor_uv, fctx, - )?); + )?; if let Some((_, vb)) = &palette_uv_bufs { set_palette_pred(vb.clone(), fctx); } - v_grid = Grid::Own(read_plane( + v_grid = read_plane( dec, cdfs, chroma_set, @@ -28080,7 +28090,7 @@ fn decode_inter_block( None, None, smooth_neighbor_uv, fctx, - )?); + )?; } } // lane-comppin r9: end-of-block checkpoint -- everything between @@ -28112,7 +28122,7 @@ fn decode_inter_block( // the partial right superblock read row 1 where libaom reads row 2). The // units are collected here and re-stamped after that record, in mu-chunk // order so a later unit still wins over an earlier one. - let mu_chroma_units: Vec<((usize, usize), usize, Vec)> = if mu_chroma { + let mu_chroma_units: Vec<((usize, usize), usize, Grid)> = if mu_chroma { let cu = 32usize; let mut units = Vec::new(); for cr in 0..(write_h / 64).max(1) { @@ -28124,7 +28134,7 @@ fn decode_inter_block( let start = (cr * cu + rr) * chroma_side + cc * cu; unit.extend_from_slice(&grid[start..start + cu]); } - units.push((cu_mi, plane, unit)); + units.push((cu_mi, plane, Grid::Own(unit))); } } } @@ -28801,7 +28811,7 @@ fn decode_inter_sub8_split4( let (u_grid, v_grid) = if last_skip { push_mc(1, cpx, cpy, B4, Pred::dense(&pred_u), &ZERO_RESIDUAL[..B4 * B4], fctx); push_mc(2, cpx, cpy, B4, Pred::dense(&pred_v), &ZERO_RESIDUAL[..B4 * B4], fctx); - (vec![0i32; B4 * B4], vec![0i32; B4 * B4]) + (Grid::Zero(B4 * B4), Grid::Zero(B4 * B4)) } else { let around = neighbours.around_mi(group_mi, 8); let ug = read_inter_plane( @@ -29108,7 +29118,7 @@ fn decode_intra_sub8_leaf( } } else { let grid_levels = if skip { - let zeros = vec![0i32; bw * bh]; + let zeros = Grid::Zero(bw * bh); push_intra_rect(0, px, py, bw, bh, mode, angle_delta_y, reach, &zeros, None, filter_intra, smooth_neighbor, fctx, @@ -29238,14 +29248,14 @@ fn decode_intra_sub8_leaf( record_strip_palette(neighbours, (gr, gc), 8, 8, 0, [0u16; 8], 0, [0u16; 8]); let ac = alpha.map(|_| cfl_src(gpx, gpy, 8)); - let (u_grid, v_grid): (Vec, Vec) = if skip { + let (u_grid, v_grid): (Grid, Grid) = 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)); } 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); 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); - (vec![0i32; 16], vec![0i32; 16]) + (Grid::Zero(16), Grid::Zero(16)) } else { let chroma_around = neighbours.around_mi(group_mi, 8); let ug = read_plane( @@ -29820,7 +29830,7 @@ fn decode_inter_sub8_rect2( let (u_grid, v_grid) = if last_skip { push_mc(1, cpx, cpy, B4, Pred::dense(&pred_u), &ZERO_RESIDUAL[..B4 * B4], fctx); push_mc(2, cpx, cpy, B4, Pred::dense(&pred_v), &ZERO_RESIDUAL[..B4 * B4], fctx); - (vec![0i32; B4 * B4], vec![0i32; B4 * B4]) + (Grid::Zero(B4 * B4), Grid::Zero(B4 * B4)) } else { let around = neighbours.around_mi(group_mi, 8); let ug = read_inter_plane( @@ -30663,9 +30673,9 @@ fn decode_inter_block8( Pred::dense(&pred_v), &ZERO_RESIDUAL[..CHROMA_SIDE * CHROMA_SIDE], fctx, ); - luma_grid = vec![0i32; SIDE * SIDE]; - u_grid = vec![0i32; CHROMA_SIDE * CHROMA_SIDE]; - v_grid = vec![0i32; CHROMA_SIDE * CHROMA_SIDE]; + luma_grid = Grid::Zero(SIDE * SIDE); + u_grid = Grid::Zero(CHROMA_SIDE * CHROMA_SIDE); + v_grid = Grid::Zero(CHROMA_SIDE * CHROMA_SIDE); } else { let around = neighbours.around_mi(leaf_mi, 8); let (grid8, luma_tx_type) = read_inter_luma8( @@ -31308,9 +31318,9 @@ fn decode_inter_block8( Pred::dense(&pred_v), &ZERO_RESIDUAL[..CHROMA_SIDE * CHROMA_SIDE], fctx, ); - luma_grid = vec![0i32; SIDE * SIDE]; - u_grid = vec![0i32; CHROMA_SIDE * CHROMA_SIDE]; - v_grid = vec![0i32; CHROMA_SIDE * CHROMA_SIDE]; + luma_grid = Grid::Zero(SIDE * SIDE); + u_grid = Grid::Zero(CHROMA_SIDE * CHROMA_SIDE); + v_grid = Grid::Zero(CHROMA_SIDE * CHROMA_SIDE); } else { let around = neighbours.around_mi(leaf_mi, 8); let (grid8, luma_tx_type) = read_inter_luma8( @@ -31674,7 +31684,7 @@ fn decode_inter_block8( filter_intra, smooth_neighbor, fctx, ); - vec![0i32; tx_px * tx_px] + Grid::Zero(tx_px * tx_px) } else { // The unit is smaller than the block it sits in, so // `txb_skip_ctx` is the neighbour-magnitude table, not @@ -31761,16 +31771,16 @@ fn decode_inter_block8( None, smooth_neighbor_uv, fctx, ); - luma_grid = vec![0i32; SIDE * SIDE]; - u_grid = vec![0i32; CHROMA_SIDE * CHROMA_SIDE]; - v_grid = vec![0i32; CHROMA_SIDE * CHROMA_SIDE]; + luma_grid = Grid::Zero(SIDE * SIDE); + u_grid = Grid::Zero(CHROMA_SIDE * CHROMA_SIDE); + v_grid = Grid::Zero(CHROMA_SIDE * CHROMA_SIDE); } else { let around = neighbours.around_mi(leaf_mi, 8); luma_grid = if split8 { // Already coded and reconstructed unit by unit above; the // block-level grid only feeds the neighbour write-back, // which the `saved_luma_ctx` restore below keeps per-TU. - vec![0i32; SIDE * SIDE] + Grid::Zero(SIDE * SIDE) } else { if let Some(buf) = &palette_y_buf { set_palette_pred(buf.clone(), fctx); diff --git a/crates/ec-av1/src/transform.rs b/crates/ec-av1/src/transform.rs index b9b2429c..6e4546bd 100644 --- a/crates/ec-av1/src/transform.rs +++ b/crates/ec-av1/src/transform.rs @@ -1340,7 +1340,7 @@ pub fn dequant_and_inverse_typed_wh( // `decode.rs`), which also drops the `vec![0; w * h]` memset here and the // `to_vec` clone the recorded [`crate::decode::Residual`] made of it -- // together 6.5% of the 4K segment's profile (memset + memmove). - if levels.iter().all(|&l| l == 0) { + if crate::decode::is_zero_grid(levels) { return Vec::new(); } DQ_SCRATCH.with(|cell| {