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 commit2 changed files +115 -105
- crates/ec-av1/src/decode.rs +114 -104
- crates/ec-av1/src/transform.rs +1 -1
crates/ec-av1/src/decode.rsmodified+114 -104
| @@ -2140,7 +2140,7 @@ impl Residual { | ||
| 2140 | 2140 | /// the empty marker, so neither the grid nor the residual is cloned and |
| 2141 | 2141 | /// the worker never runs the transform (60% of the 4K segment's units). |
| 2142 | 2142 | 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) { | |
| 2144 | 2144 | return Self::Done(0, 0); |
| 2145 | 2145 | } |
| 2146 | 2146 | 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 { | ||
| 2197 | 2197 | /// and the reconstruction loops below read this instead of a per-unit |
| 2198 | 2198 | /// `vec![0; w * h]`. Sized for the largest stride a recorded unit can carry |
| 2199 | 2199 | /// (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]; | |
| 2201 | 2201 | |
| 2202 | 2202 | /// lane-blockscratch: one plane's coefficient levels as the block-tail |
| 2203 | 2203 | /// neighbour writes want them. A plane that coded nothing (a skipped block, a |
| 2204 | 2204 | /// strip with no chroma, luma under a var-tx split whose units already wrote |
| 2205 | 2205 | /// themselves) names the shared [`ZERO_RESIDUAL`] instead of allocating and |
| 2206 | 2206 | /// zeroing up to 64 KB of its own per block. |
| 2207 | +#[derive(Clone)] | |
| 2207 | 2208 | enum Grid { |
| 2208 | 2209 | Zero(usize), |
| 2209 | 2210 | Own(Vec<i32>), |
| @@ -2230,6 +2231,13 @@ impl std::ops::Deref for Grid { | ||
| 2230 | 2231 | } |
| 2231 | 2232 | } |
| 2232 | 2233 | |
| 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 | + | |
| 2233 | 2241 | /// `residual`, or `n` zeros when it is the empty all-zero marker. |
| 2234 | 2242 | fn dense_residual(residual: &[i32], n: usize) -> &[i32] { |
| 2235 | 2243 | if residual.is_empty() { &ZERO_RESIDUAL[..n] } else { residual } |
| @@ -5897,10 +5905,9 @@ fn read_coeffs( | ||
| 5897 | 5905 | // root cause, see [`base_ctx`]). `None` everywhere else -- every other |
| 5898 | 5906 | // caller's block genuinely is `side` x `side`. |
| 5899 | 5907 | rect_shape: Option<(usize, usize)>, |
| 5900 | -) -> Result<(Vec<i32>, TxType)> { | |
| 5908 | +) -> Result<(Grid, TxType)> { | |
| 5901 | 5909 | let trace = crate::envflags::env_flag!("EC_AV1_TRACE"); |
| 5902 | 5910 | let side = coding.side; |
| 5903 | - let mut grid = vec![0i32; side * side]; | |
| 5904 | 5911 | if crate::envflags::env_flag!("EC_AV1_EOBPT_CDF") { |
| 5905 | 5912 | let (range, value) = dec.debug_state(); |
| 5906 | 5913 | eprintln!("EC_AV1_STATE_BEFORE_TXBSKIP range={range} value={value}"); |
| @@ -5933,8 +5940,11 @@ fn read_coeffs( | ||
| 5933 | 5940 | eprintln!("EC_AV1_STATE_AFTER_TXBSKIP range={range} value={value}"); |
| 5934 | 5941 | } |
| 5935 | 5942 | 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)); | |
| 5937 | 5946 | } |
| 5947 | + let mut grid = vec![0i32; side * side]; | |
| 5938 | 5948 | let mut tx_type = default_tx_type; |
| 5939 | 5949 | if let Some(tx_type_cdf) = coding.tx_type.as_deref_mut() { |
| 5940 | 5950 | // The CDF row's own width names its set (lane-cdffwd2: extended |
| @@ -6116,7 +6126,7 @@ fn read_coeffs( | ||
| 6116 | 6126 | } |
| 6117 | 6127 | grid[pos] = if negative { -level } else { level }; |
| 6118 | 6128 | } |
| 6119 | - Ok((grid, tx_type)) | |
| 6129 | + Ok((Grid::Own(grid), tx_type)) | |
| 6120 | 6130 | } |
| 6121 | 6131 | |
| 6122 | 6132 | /// [`read_coeffs`] widened to `(w, h)` (lane-rectwire; lane-rectx added the |
| @@ -6136,9 +6146,8 @@ fn read_coeffs_rect( | ||
| 6136 | 6146 | skip_ctx: usize, |
| 6137 | 6147 | sign_ctx: usize, |
| 6138 | 6148 | default_tx_type: TxType, |
| 6139 | -) -> Result<(Vec<i32>, TxType)> { | |
| 6149 | +) -> Result<(Grid, TxType)> { | |
| 6140 | 6150 | let rect_trace = coeff_trace_on(); |
| 6141 | - let mut grid = vec![0i32; w * h]; | |
| 6142 | 6151 | let entry_rng = dec.debug_state().0; |
| 6143 | 6152 | let all_zero = dec.symbol(&mut coding.txb_skip[skip_ctx]) == 1; |
| 6144 | 6153 | if rect_trace { |
| @@ -6149,8 +6158,9 @@ fn read_coeffs_rect( | ||
| 6149 | 6158 | ); |
| 6150 | 6159 | } |
| 6151 | 6160 | if all_zero { |
| 6152 | - return Ok((grid, TxType::DctDct)); | |
| 6161 | + return Ok((Grid::Zero(w * h), TxType::DctDct)); | |
| 6153 | 6162 | } |
| 6163 | + let mut grid = vec![0i32; w * h]; | |
| 6154 | 6164 | RECT_COEFF_TU_HITS.with(|c| c.set(c.get() + 1)); |
| 6155 | 6165 | // lane-tx4x8: a 4x8/8x4 luma TU DOES carry a `tx_type` symbol (its set is |
| 6156 | 6166 | // `EXT_TX_SET_DTT4_IDTX(_1DDCT)`, `tx_size_sqr_up == TX_8X8`), including |
| @@ -6288,7 +6298,7 @@ fn read_coeffs_rect( | ||
| 6288 | 6298 | }; |
| 6289 | 6299 | grid[pos] = if negative { -level } else { level }; |
| 6290 | 6300 | } |
| 6291 | - Ok((grid, tx_type)) | |
| 6301 | + Ok((Grid::Own(grid), tx_type)) | |
| 6292 | 6302 | } |
| 6293 | 6303 | |
| 6294 | 6304 | /// 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) { | ||
| 6324 | 6334 | } |
| 6325 | 6335 | |
| 6326 | 6336 | 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 | + } | |
| 6327 | 6343 | let cul: u32 = grid.iter().map(|&l| l.unsigned_abs()).sum(); |
| 6328 | 6344 | Neighbour { |
| 6329 | 6345 | level: cul.min(7) as u8, |
| @@ -9223,7 +9239,7 @@ fn decode_rect_split( | ||
| 9223 | 9239 | let ac = m.alpha.map(|_| cfl_src_rect(px, py, bw, bh)); |
| 9224 | 9240 | let reach = block_reach; |
| 9225 | 9241 | 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); | |
| 9227 | 9243 | if let Some((ub, _)) = &m.palette_uv { |
| 9228 | 9244 | set_palette_pred(ub.clone(), fctx); |
| 9229 | 9245 | } |
| @@ -9250,7 +9266,7 @@ fn decode_rect_split( | ||
| 9250 | 9266 | default_intra_tx_type(m.uv_predict_mode as u8) |
| 9251 | 9267 | }; |
| 9252 | 9268 | 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); | |
| 9254 | 9270 | for plane_idx in 1..=2 { |
| 9255 | 9271 | let skip_ctx = |
| 9256 | 9272 | usize::from(around[plane_idx].0) + usize::from(around[plane_idx].1); |
| @@ -10719,9 +10735,9 @@ fn decode_leaf_rect( | ||
| 10719 | 10735 | let (chroma_w, chroma_h) = (bw / 2, bh / 2); |
| 10720 | 10736 | let (luma_levels, u_levels, v_levels); |
| 10721 | 10737 | 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); | |
| 10725 | 10741 | if let Some(b) = &palette_y_buf { |
| 10726 | 10742 | set_palette_pred(b.clone(), fctx); |
| 10727 | 10743 | } |
| @@ -11647,7 +11663,7 @@ fn decode_rect4_16_strip( | ||
| 11647 | 11663 | } |
| 11648 | 11664 | } else { |
| 11649 | 11665 | let levels = if skip { |
| 11650 | - let zeros = vec![0i32; bw * bh]; | |
| 11666 | + let zeros = Grid::Zero(bw * bh); | |
| 11651 | 11667 | if let Some(b) = &palette_y_buf { |
| 11652 | 11668 | set_palette_pred(b.clone(), fctx); |
| 11653 | 11669 | } |
| @@ -11748,7 +11764,7 @@ fn decode_rect4_16_strip( | ||
| 11748 | 11764 | RECT4_16_UV_PAIR_FILT_HITS.with(|h| h.set(h.get() + 1)); |
| 11749 | 11765 | } |
| 11750 | 11766 | let (u_levels, v_levels) = if skip { |
| 11751 | - let zeros = vec![0i32; cw * ch]; | |
| 11767 | + let zeros = Grid::Zero(cw * ch); | |
| 11752 | 11768 | // libaom `predict_and_reconstruct_intra_block` (decodeframe.c |
| 11753 | 11769 | // ~1000) predicts EVERY plane and only guards the residual on |
| 11754 | 11770 | // `mbmi->skip_txfm`, so a skipped `UV_CFL_PRED` block is still |
| @@ -13350,7 +13366,7 @@ fn read_plane( | ||
| 13350 | 13366 | luma_skip_ctx: Option<usize>, |
| 13351 | 13367 | // See [`PlaneBuf::reconstruct`]'s own doc: caller's neighbour-smooth check. |
| 13352 | 13368 | smooth_neighbor: bool, fctx: &crate::decode::FrameCtx, |
| 13353 | -) -> Result<Vec<i32>> { | |
| 13369 | +) -> Result<Grid> { | |
| 13354 | 13370 | let skip_ctx = if plane_idx == 0 { |
| 13355 | 13371 | luma_skip_ctx.unwrap_or(0) |
| 13356 | 13372 | } else { |
| @@ -13410,19 +13426,11 @@ fn read_plane( | ||
| 13410 | 13426 | // top-left 32x32 of frequencies are coded (spec 5.11.40); the rest of the |
| 13411 | 13427 | // dequantized grid stays zero, which `inverse_transform_2d`'s own `< 32` |
| 13412 | 13428 | // 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); | |
| 13426 | 13434 | let (dc_delta, ac_delta) = plane_q_delta(plane_idx, fctx); |
| 13427 | 13435 | let tx = TxParams { |
| 13428 | 13436 | w: side, h: side, bit_depth: bit_depth(fctx), q_idx: block_q_idx(fctx), dc_delta, |
| @@ -13431,7 +13439,7 @@ fn read_plane( | ||
| 13431 | 13439 | if crate::envflags::env_flag!("EC_AV1_TRACE") { |
| 13432 | 13440 | let residual = tx.run(&levels); |
| 13433 | 13441 | 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, | |
| 13435 | 13443 | ); |
| 13436 | 13444 | } |
| 13437 | 13445 | push_intra_tx( |
| @@ -13945,8 +13953,8 @@ fn decode_block( | ||
| 13945 | 13953 | let chroma_ctx_offset = if cn > 1 { Some(3) } else { None }; |
| 13946 | 13954 | let tpc = (n_axis / nchunk).max(1); |
| 13947 | 13955 | 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(); | |
| 13950 | 13958 | for ch_row in 0..nchunk { |
| 13951 | 13959 | for ch_col in 0..nchunk { |
| 13952 | 13960 | for tu_row in ch_row * tpc..(ch_row + 1) * tpc { |
| @@ -14308,8 +14316,8 @@ fn decode_block_128rect( | ||
| 14308 | 14316 | let tpc = 64 / logical_tx; |
| 14309 | 14317 | let zero_tu = vec![0i32; logical_tx * logical_tx]; |
| 14310 | 14318 | 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(); | |
| 14313 | 14321 | for ch_row in 0..nch_y { |
| 14314 | 14322 | for ch_col in 0..nch_x { |
| 14315 | 14323 | for tu_row in ch_row * tpc..(ch_row + 1) * tpc { |
| @@ -14829,9 +14837,9 @@ fn decode_leaf8( | ||
| 14829 | 14837 | None, |
| 14830 | 14838 | smooth_neighbor_uv, fctx, |
| 14831 | 14839 | ); |
| 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); | |
| 14835 | 14843 | } else if resolved != 4 { |
| 14836 | 14844 | let around = neighbours.around_mi(leaf_mi, 8); |
| 14837 | 14845 | if let Some(buf) = &palette_y_buf { |
| @@ -15369,7 +15377,7 @@ fn decode_leaf_split4( | ||
| 15369 | 15377 | neighbours.above_uv_mode[c] = uv_predict_mode; |
| 15370 | 15378 | neighbours.left_uv_mode[r] = uv_predict_mode; |
| 15371 | 15379 | 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] { | |
| 15373 | 15381 | // A skipped chroma block still gets its CfL contribution: libaom's |
| 15374 | 15382 | // `predict_and_reconstruct_intra_block` calls `cfl_predict_block` |
| 15375 | 15383 | // from `av1_predict_intra_block_facade` (av1/common/reconintra.c:1719) |
| @@ -15385,7 +15393,7 @@ fn decode_leaf_split4( | ||
| 15385 | 15393 | } |
| 15386 | 15394 | 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); |
| 15387 | 15395 | 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)) | |
| 15389 | 15397 | } else { |
| 15390 | 15398 | let ac = alpha.map(|_| cfl_src(gpx, gpy, 8)); |
| 15391 | 15399 | let chroma_around = neighbours.around_mi(leaf_mi, 8); |
| @@ -15661,7 +15669,7 @@ fn decode_leaf_rect8( | ||
| 15661 | 15669 | continue; |
| 15662 | 15670 | } |
| 15663 | 15671 | let grid = if skip { |
| 15664 | - let zeros = vec![0i32; bw * bh]; | |
| 15672 | + let zeros = Grid::Zero(bw * bh); | |
| 15665 | 15673 | push_intra_rect(0, |
| 15666 | 15674 | px, py, bw, bh, mode, angle_delta_y, reach, &zeros, None, filter_intra, |
| 15667 | 15675 | smooth_neighbor, fctx, |
| @@ -15741,7 +15749,7 @@ fn decode_leaf_rect8( | ||
| 15741 | 15749 | neighbours.above_uv_mode[c] = uv_predict_mode; |
| 15742 | 15750 | neighbours.left_uv_mode[r] = uv_predict_mode; |
| 15743 | 15751 | 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] { | |
| 15745 | 15753 | // A skipped chroma block still gets its CfL contribution: libaom's |
| 15746 | 15754 | // `predict_and_reconstruct_intra_block` calls `cfl_predict_block` |
| 15747 | 15755 | // from `av1_predict_intra_block_facade` (av1/common/reconintra.c:1719) |
| @@ -15757,7 +15765,7 @@ fn decode_leaf_rect8( | ||
| 15757 | 15765 | } |
| 15758 | 15766 | 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); |
| 15759 | 15767 | 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)) | |
| 15761 | 15769 | } else { |
| 15762 | 15770 | let ac = alpha.map(|_| cfl_src(gpx, gpy, 8)); |
| 15763 | 15771 | let chroma_around = neighbours.around_mi(leaf_mi, 8); |
| @@ -17675,7 +17683,7 @@ fn read_inter_plane_rect( | ||
| 17675 | 17683 | prediction: Pred<'_>, |
| 17676 | 17684 | inherited_luma_tx_type: Option<TxType>, |
| 17677 | 17685 | luma_skip_ctx: Option<usize>, fctx: &crate::decode::FrameCtx, |
| 17678 | -) -> Result<(Vec<i32>, TxType)> { | |
| 17686 | +) -> Result<(Grid, TxType)> { | |
| 17679 | 17687 | let skip_ctx = if plane_idx == 0 { |
| 17680 | 17688 | // `get_txb_ctx_general`: 0 only when this unit IS the whole plane |
| 17681 | 17689 | // block; a var-tx leaf passes its own neighbour-table context. |
| @@ -17732,15 +17740,7 @@ fn read_inter_plane_rect( | ||
| 17732 | 17740 | default_tx_type, |
| 17733 | 17741 | )? |
| 17734 | 17742 | }; |
| 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); | |
| 17744 | 17744 | let (dc_delta, ac_delta) = plane_q_delta(plane_idx, fctx); |
| 17745 | 17745 | if crate::envflags::env_flag!("EC_TRACE_TXTYPE") { |
| 17746 | 17746 | 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( | ||
| 17756 | 17756 | Ok((grid, tx_type)) |
| 17757 | 17757 | } |
| 17758 | 17758 | |
| 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 | + | |
| 17759 | 17777 | /// A rect coefficient grid re-laid into the square `stride x stride` grid the |
| 17760 | 17778 | /// neighbour/deblock stamps downstream of the residual expect. |
| 17761 | 17779 | fn embed_rect_grid(grid: &[i32], w: usize, h: usize, stride: usize) -> Vec<i32> { |
| @@ -17790,7 +17808,7 @@ fn read_inter_luma8( | ||
| 17790 | 17808 | scan4: &[u16], |
| 17791 | 17809 | reduced_tx_set: bool, |
| 17792 | 17810 | split: bool, fctx: &crate::decode::FrameCtx, |
| 17793 | -) -> Result<(Vec<i32>, TxType)> { | |
| 17811 | +) -> Result<(Grid, TxType)> { | |
| 17794 | 17812 | if !split { |
| 17795 | 17813 | return read_inter_plane( |
| 17796 | 17814 | dec, |
| @@ -17847,7 +17865,7 @@ fn read_inter_luma8( | ||
| 17847 | 17865 | neighbours.record_mi_luma(tu_mi, 4, &tu_grid); |
| 17848 | 17866 | } |
| 17849 | 17867 | } |
| 17850 | - Ok((vec![0i32; 64], first)) | |
| 17868 | + Ok((Grid::Zero(64), first)) | |
| 17851 | 17869 | } |
| 17852 | 17870 | |
| 17853 | 17871 | /// The [`TxbSet`] one leaf of an inter block's var-tx tree reads its |
| @@ -23084,7 +23102,7 @@ fn read_inter_plane( | ||
| 23084 | 23102 | // transform every non-var-tx caller reads, whose plane-0 context is the |
| 23085 | 23103 | // lone-transform-unit 0. |
| 23086 | 23104 | luma_skip_ctx: Option<usize>, fctx: &crate::decode::FrameCtx, |
| 23087 | -) -> Result<(Vec<i32>, TxType)> { | |
| 23105 | +) -> Result<(Grid, TxType)> { | |
| 23088 | 23106 | let skip_ctx = if plane_idx == 0 { |
| 23089 | 23107 | luma_skip_ctx.unwrap_or(0) |
| 23090 | 23108 | } else { |
| @@ -23122,15 +23140,7 @@ fn read_inter_plane( | ||
| 23122 | 23140 | // [`read_plane`]'s own `tx_side != side` branch. Every other transform |
| 23123 | 23141 | // this decoder codes has `tx_side == side`, so this is inert below 64. |
| 23124 | 23142 | 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); | |
| 23134 | 23144 | let (dc_delta, ac_delta) = plane_q_delta(plane_idx, fctx); |
| 23135 | 23145 | let tx = TxParams { |
| 23136 | 23146 | 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( | ||
| 26018 | 26028 | None, |
| 26019 | 26029 | None, fctx, |
| 26020 | 26030 | )?; |
| 26021 | - luma_grid = Grid::Own(coded.0); | |
| 26031 | + luma_grid = coded.0; | |
| 26022 | 26032 | luma_tx_type = coded.1; |
| 26023 | 26033 | } |
| 26024 | 26034 | } |
| @@ -26087,7 +26097,7 @@ fn decode_inter_block( | ||
| 26087 | 26097 | chroma_side, |
| 26088 | 26098 | )); |
| 26089 | 26099 | } else { |
| 26090 | - u_grid = Grid::Own(read_inter_plane( | |
| 26100 | + u_grid = read_inter_plane( | |
| 26091 | 26101 | dec, |
| 26092 | 26102 | cdfs, |
| 26093 | 26103 | chroma_set, |
| @@ -26104,8 +26114,8 @@ fn decode_inter_block( | ||
| 26104 | 26114 | Some(luma_tx_type), |
| 26105 | 26115 | None, fctx, |
| 26106 | 26116 | )? |
| 26107 | - .0); | |
| 26108 | - v_grid = Grid::Own(read_inter_plane( | |
| 26117 | + .0; | |
| 26118 | + v_grid = read_inter_plane( | |
| 26109 | 26119 | dec, |
| 26110 | 26120 | cdfs, |
| 26111 | 26121 | chroma_set, |
| @@ -26122,7 +26132,7 @@ fn decode_inter_block( | ||
| 26122 | 26132 | Some(luma_tx_type), |
| 26123 | 26133 | None, fctx, |
| 26124 | 26134 | )? |
| 26125 | - .0); | |
| 26135 | + .0; | |
| 26126 | 26136 | } |
| 26127 | 26137 | } |
| 26128 | 26138 | } else { |
| @@ -27277,7 +27287,7 @@ fn decode_inter_block( | ||
| 27277 | 27287 | None, |
| 27278 | 27288 | None, fctx, |
| 27279 | 27289 | )?; |
| 27280 | - luma_grid = Grid::Own(coded.0); | |
| 27290 | + luma_grid = coded.0; | |
| 27281 | 27291 | luma_tx_type = coded.1; |
| 27282 | 27292 | } |
| 27283 | 27293 | } |
| @@ -27355,7 +27365,7 @@ fn decode_inter_block( | ||
| 27355 | 27365 | chroma_side, |
| 27356 | 27366 | )); |
| 27357 | 27367 | } else { |
| 27358 | - u_grid = Grid::Own(read_inter_plane( | |
| 27368 | + u_grid = read_inter_plane( | |
| 27359 | 27369 | dec, |
| 27360 | 27370 | cdfs, |
| 27361 | 27371 | chroma_set, |
| @@ -27372,8 +27382,8 @@ fn decode_inter_block( | ||
| 27372 | 27382 | Some(luma_tx_type), |
| 27373 | 27383 | None, fctx, |
| 27374 | 27384 | )? |
| 27375 | - .0); | |
| 27376 | - v_grid = Grid::Own(read_inter_plane( | |
| 27385 | + .0; | |
| 27386 | + v_grid = read_inter_plane( | |
| 27377 | 27387 | dec, |
| 27378 | 27388 | cdfs, |
| 27379 | 27389 | chroma_set, |
| @@ -27390,7 +27400,7 @@ fn decode_inter_block( | ||
| 27390 | 27400 | Some(luma_tx_type), |
| 27391 | 27401 | None, fctx, |
| 27392 | 27402 | )? |
| 27393 | - .0); | |
| 27403 | + .0; | |
| 27394 | 27404 | } |
| 27395 | 27405 | } |
| 27396 | 27406 | } |
| @@ -27816,7 +27826,7 @@ fn decode_inter_block( | ||
| 27816 | 27826 | // so `txb_skip_ctx` is the neighbour-magnitude table, not |
| 27817 | 27827 | // the lone-TU 0 (`get_txb_ctx_general`). |
| 27818 | 27828 | let tu_skip_ctx = neighbours.luma_skip_ctx(tu_mi, tx_px / MI); |
| 27819 | - Grid::Own(read_plane( | |
| 27829 | + read_plane( | |
| 27820 | 27830 | dec, |
| 27821 | 27831 | cdfs, |
| 27822 | 27832 | txbset_for(tx_px, reduced), |
| @@ -27841,7 +27851,7 @@ fn decode_inter_block( | ||
| 27841 | 27851 | filter_intra, |
| 27842 | 27852 | Some(tu_skip_ctx), |
| 27843 | 27853 | smooth_neighbor, fctx, |
| 27844 | - )?) | |
| 27854 | + )? | |
| 27845 | 27855 | }; |
| 27846 | 27856 | neighbours.record_mi_luma(tu_mi, tx_px, &tu_grid); |
| 27847 | 27857 | if side > 64 { |
| @@ -27882,7 +27892,7 @@ fn decode_inter_block( | ||
| 27882 | 27892 | ); |
| 27883 | 27893 | Grid::Zero(cu_tx * cu_tx) |
| 27884 | 27894 | } else { |
| 27885 | - Grid::Own(read_plane( | |
| 27895 | + read_plane( | |
| 27886 | 27896 | dec, |
| 27887 | 27897 | cdfs, |
| 27888 | 27898 | chroma_set, |
| @@ -27909,7 +27919,7 @@ fn decode_inter_block( | ||
| 27909 | 27919 | // (lane-sb128b r3). |
| 27910 | 27920 | Some(3), |
| 27911 | 27921 | smooth_neighbor_uv, fctx, |
| 27912 | - )?) | |
| 27922 | + )? | |
| 27913 | 27923 | }; |
| 27914 | 27924 | // Immediately, so the NEXT chunk's unit reads this |
| 27915 | 27925 | // one's coefficient context. |
| @@ -28007,7 +28017,7 @@ fn decode_inter_block( | ||
| 28007 | 28017 | if let Some(buf) = &palette_y_buf { |
| 28008 | 28018 | set_palette_pred(buf.clone(), fctx); |
| 28009 | 28019 | } |
| 28010 | - luma_grid = Grid::Own(read_plane( | |
| 28020 | + luma_grid = read_plane( | |
| 28011 | 28021 | dec, |
| 28012 | 28022 | cdfs, |
| 28013 | 28023 | luma_set_intra, |
| @@ -28028,13 +28038,13 @@ fn decode_inter_block( | ||
| 28028 | 28038 | filter_intra, |
| 28029 | 28039 | None, |
| 28030 | 28040 | smooth_neighbor, fctx, |
| 28031 | - )?); | |
| 28041 | + )?; | |
| 28032 | 28042 | } |
| 28033 | 28043 | let ac = alpha.map(|_| cfl_src(px, py, side)); |
| 28034 | 28044 | if let Some((ub, _)) = &palette_uv_bufs { |
| 28035 | 28045 | set_palette_pred(ub.clone(), fctx); |
| 28036 | 28046 | } |
| 28037 | - u_grid = Grid::Own(read_plane( | |
| 28047 | + u_grid = read_plane( | |
| 28038 | 28048 | dec, |
| 28039 | 28049 | cdfs, |
| 28040 | 28050 | chroma_set, |
| @@ -28055,11 +28065,11 @@ fn decode_inter_block( | ||
| 28055 | 28065 | None, |
| 28056 | 28066 | None, |
| 28057 | 28067 | smooth_neighbor_uv, fctx, |
| 28058 | - )?); | |
| 28068 | + )?; | |
| 28059 | 28069 | if let Some((_, vb)) = &palette_uv_bufs { |
| 28060 | 28070 | set_palette_pred(vb.clone(), fctx); |
| 28061 | 28071 | } |
| 28062 | - v_grid = Grid::Own(read_plane( | |
| 28072 | + v_grid = read_plane( | |
| 28063 | 28073 | dec, |
| 28064 | 28074 | cdfs, |
| 28065 | 28075 | chroma_set, |
| @@ -28080,7 +28090,7 @@ fn decode_inter_block( | ||
| 28080 | 28090 | None, |
| 28081 | 28091 | None, |
| 28082 | 28092 | smooth_neighbor_uv, fctx, |
| 28083 | - )?); | |
| 28093 | + )?; | |
| 28084 | 28094 | } |
| 28085 | 28095 | } |
| 28086 | 28096 | // lane-comppin r9: end-of-block checkpoint -- everything between |
| @@ -28112,7 +28122,7 @@ fn decode_inter_block( | ||
| 28112 | 28122 | // the partial right superblock read row 1 where libaom reads row 2). The |
| 28113 | 28123 | // units are collected here and re-stamped after that record, in mu-chunk |
| 28114 | 28124 | // 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 { | |
| 28116 | 28126 | let cu = 32usize; |
| 28117 | 28127 | let mut units = Vec::new(); |
| 28118 | 28128 | for cr in 0..(write_h / 64).max(1) { |
| @@ -28124,7 +28134,7 @@ fn decode_inter_block( | ||
| 28124 | 28134 | let start = (cr * cu + rr) * chroma_side + cc * cu; |
| 28125 | 28135 | unit.extend_from_slice(&grid[start..start + cu]); |
| 28126 | 28136 | } |
| 28127 | - units.push((cu_mi, plane, unit)); | |
| 28137 | + units.push((cu_mi, plane, Grid::Own(unit))); | |
| 28128 | 28138 | } |
| 28129 | 28139 | } |
| 28130 | 28140 | } |
| @@ -28801,7 +28811,7 @@ fn decode_inter_sub8_split4( | ||
| 28801 | 28811 | let (u_grid, v_grid) = if last_skip { |
| 28802 | 28812 | push_mc(1, cpx, cpy, B4, Pred::dense(&pred_u), &ZERO_RESIDUAL[..B4 * B4], fctx); |
| 28803 | 28813 | 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)) | |
| 28805 | 28815 | } else { |
| 28806 | 28816 | let around = neighbours.around_mi(group_mi, 8); |
| 28807 | 28817 | let ug = read_inter_plane( |
| @@ -29108,7 +29118,7 @@ fn decode_intra_sub8_leaf( | ||
| 29108 | 29118 | } |
| 29109 | 29119 | } else { |
| 29110 | 29120 | let grid_levels = if skip { |
| 29111 | - let zeros = vec![0i32; bw * bh]; | |
| 29121 | + let zeros = Grid::Zero(bw * bh); | |
| 29112 | 29122 | push_intra_rect(0, |
| 29113 | 29123 | px, py, bw, bh, mode, angle_delta_y, reach, &zeros, None, filter_intra, |
| 29114 | 29124 | smooth_neighbor, fctx, |
| @@ -29238,14 +29248,14 @@ fn decode_intra_sub8_leaf( | ||
| 29238 | 29248 | record_strip_palette(neighbours, (gr, gc), 8, 8, 0, [0u16; 8], 0, [0u16; 8]); |
| 29239 | 29249 | |
| 29240 | 29250 | 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 { | |
| 29242 | 29252 | if alpha.is_some() { |
| 29243 | 29253 | CHROMA_SKIP_CFL_HITS.with(|h| h.set(h.get() + 1)); |
| 29244 | 29254 | SKIP_CFL_HITS.with(|c| c.set(c.get() + 1)); |
| 29245 | 29255 | } |
| 29246 | 29256 | 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); |
| 29247 | 29257 | 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)) | |
| 29249 | 29259 | } else { |
| 29250 | 29260 | let chroma_around = neighbours.around_mi(group_mi, 8); |
| 29251 | 29261 | let ug = read_plane( |
| @@ -29820,7 +29830,7 @@ fn decode_inter_sub8_rect2( | ||
| 29820 | 29830 | let (u_grid, v_grid) = if last_skip { |
| 29821 | 29831 | push_mc(1, cpx, cpy, B4, Pred::dense(&pred_u), &ZERO_RESIDUAL[..B4 * B4], fctx); |
| 29822 | 29832 | 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)) | |
| 29824 | 29834 | } else { |
| 29825 | 29835 | let around = neighbours.around_mi(group_mi, 8); |
| 29826 | 29836 | let ug = read_inter_plane( |
| @@ -30663,9 +30673,9 @@ fn decode_inter_block8( | ||
| 30663 | 30673 | Pred::dense(&pred_v), |
| 30664 | 30674 | &ZERO_RESIDUAL[..CHROMA_SIDE * CHROMA_SIDE], fctx, |
| 30665 | 30675 | ); |
| 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); | |
| 30669 | 30679 | } else { |
| 30670 | 30680 | let around = neighbours.around_mi(leaf_mi, 8); |
| 30671 | 30681 | let (grid8, luma_tx_type) = read_inter_luma8( |
| @@ -31308,9 +31318,9 @@ fn decode_inter_block8( | ||
| 31308 | 31318 | Pred::dense(&pred_v), |
| 31309 | 31319 | &ZERO_RESIDUAL[..CHROMA_SIDE * CHROMA_SIDE], fctx, |
| 31310 | 31320 | ); |
| 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); | |
| 31314 | 31324 | } else { |
| 31315 | 31325 | let around = neighbours.around_mi(leaf_mi, 8); |
| 31316 | 31326 | let (grid8, luma_tx_type) = read_inter_luma8( |
| @@ -31674,7 +31684,7 @@ fn decode_inter_block8( | ||
| 31674 | 31684 | filter_intra, |
| 31675 | 31685 | smooth_neighbor, fctx, |
| 31676 | 31686 | ); |
| 31677 | - vec![0i32; tx_px * tx_px] | |
| 31687 | + Grid::Zero(tx_px * tx_px) | |
| 31678 | 31688 | } else { |
| 31679 | 31689 | // The unit is smaller than the block it sits in, so |
| 31680 | 31690 | // `txb_skip_ctx` is the neighbour-magnitude table, not |
| @@ -31761,16 +31771,16 @@ fn decode_inter_block8( | ||
| 31761 | 31771 | None, |
| 31762 | 31772 | smooth_neighbor_uv, fctx, |
| 31763 | 31773 | ); |
| 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); | |
| 31767 | 31777 | } else { |
| 31768 | 31778 | let around = neighbours.around_mi(leaf_mi, 8); |
| 31769 | 31779 | luma_grid = if split8 { |
| 31770 | 31780 | // Already coded and reconstructed unit by unit above; the |
| 31771 | 31781 | // block-level grid only feeds the neighbour write-back, |
| 31772 | 31782 | // which the `saved_luma_ctx` restore below keeps per-TU. |
| 31773 | - vec![0i32; SIDE * SIDE] | |
| 31783 | + Grid::Zero(SIDE * SIDE) | |
| 31774 | 31784 | } else { |
| 31775 | 31785 | if let Some(buf) = &palette_y_buf { |
| 31776 | 31786 | 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( | ||
| 1340 | 1340 | // `decode.rs`), which also drops the `vec![0; w * h]` memset here and the |
| 1341 | 1341 | // `to_vec` clone the recorded [`crate::decode::Residual`] made of it -- |
| 1342 | 1342 | // 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) { | |
| 1344 | 1344 | return Vec::new(); |
| 1345 | 1345 | } |
| 1346 | 1346 | DQ_SCRATCH.with(|cell| { |