forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

leaf8.rs2155 lines, 88.9 KiBHistoryRawDownload
1//! 8x8 leaf decoder and the 128-rect and sub-8 intra mode readers. //! av1split: moved out of `decode/mod.rs`; the items are unchanged apart from visibility. (D16)
2
3use super::*;
4
5/// The 128 root's HORZ/VERT arm on an INTRA frame: ONE 128x64 (HORZ) or
6/// 64x128 (VERT) intra block (lane-sb128c r8; the inter twin is
7/// `decode_inter_block`'s own rect path).
8///
9/// libaom `decode_block` -> `predict_and_reconstruct_intra_block` per
10/// transform unit, with the 128-specific geometry:
11/// * `max_txsize_rect_lookup[BLOCK_128X64]` is TX_64X64, so every luma unit
12/// is SQUARE and there are at least two of them -- depth 0: 64x64 x2,
13/// depth 1: 32x32 x8, depth 2: 16x16 x32 (`bsize_to_tx_size_cat` counts
14/// the `sub_tx_size_map` chain TX_64X64->32->16->8->4, i.e. cat 3, the
15/// same CDF a 64x64 square block reads).
16/// * `av1_get_max_uv_txsize` = `av1_get_adjusted_tx_size(
17/// max_txsize_rect_lookup[BLOCK_64X32])` caps at TX_32X32, so the block's
18/// 64x32 uv plane block is TWO 32x32 chroma units per plane -- one per
19/// 64x64 luma "mu" chunk, and `decode_token_recon_block` codes Y, U, V of
20/// a chunk before moving to the next (the interleave lane-sb128b r3
21/// measured at the 128x128 NONE root).
22/// * `get_txb_ctx`: the uv plane block is bigger than the uv transform, so
23/// chroma `txb_skip_ctx` takes the offset-10 rows (7 + 3).
24/// No CfL, no filter intra (both cap at 32x32) and no palette
25/// (`av1_allow_palette` caps at 64x64) exist at this size -- the mode
26/// reader's own size gates already say so, and the guard below is defensive.
27///
28/// lane-lossless128: on a LOSSLESS frame every one of those sizes collapses to
29/// TX_4X4 -- `read_tx_mode` returns `ONLY_4X4` before the frame codes a tx-size
30/// symbol and `read_tx_size` returns `TX_4X4` before it looks at anything
31/// (`decodeframe.c:141`, `:1183`) -- so the block is tiled into 4x4 luma units
32/// and (because a lossless frame forces `TX_4X4` on CHROMA too) 4x4 chroma
33/// units, plane-major inside each 64x64 mu chunk exactly as
34/// `decode_token_recon_block` codes them. Reading the 64-point geometry there
35/// reconstructs into a `side`-sized plane with a 16-sample residual (the panic
36/// this lane closed); the geometry and the unit walks below are normalized on
37/// `lossless_frame`.
38#[allow(clippy::too_many_arguments)]
39pub(super) fn decode_block_128rect(
40 dec: &mut SymbolDecoder,
41 cdfs: &mut Cdfs,
42 neighbours: &mut Neighbours,
43 at: (usize, usize),
44 bw: usize,
45 bh: usize,
46 y: &mut PlaneBuf<'static>,
47 u: &mut PlaneBuf<'static>,
48 v: &mut PlaneBuf<'static>,
49 base_q_idx: u8,
50 enable_filter_intra: bool,
51 allow_screen_content_tools: bool,
52 tx_select: bool,
53 reduced_tx_set: bool,
54 fctx: &crate::decode::FrameCtx,
55) -> Result<()> {
56 let (r, c) = at;
57 let (px, py) = (c * SUB, r * SUB);
58 let (mi_r, mi_c) = (r * (SUB / MI), c * (SUB / MI));
59 let (nb_above_mode, nb_left_mode) = neighbours.modes_above_left(r, c);
60 let (palette_ctx, palette_cache) = neighbours.palette_ctx_and_cache(at);
61 let palette_uv_cache = neighbours.palette_uv_cache(at);
62 let (
63 skip,
64 mode,
65 angle_delta_y,
66 uv_mode,
67 angle_delta_uv,
68 alpha,
69 filter_intra,
70 palette_y,
71 palette_uv,
72 intrabc,
73 ) = read_intra_mode_rect(
74 dec,
75 cdfs,
76 nb_above_mode,
77 nb_left_mode,
78 // `is_cfl_allowed` caps CFL at 32x32: `uv_mode` comes off the
79 // 13-symbol no-CFL CDF here, never the 14-symbol one.
80 false,
81 true,
82 bw,
83 bh,
84 enable_filter_intra,
85 neighbours.skip_txfm_ctx(mi_r, mi_c),
86 allow_screen_content_tools,
87 Some((palette_ctx, &palette_cache)),
88 &palette_uv_cache,
89 mi_r,
90 mi_c,
91 fctx,
92 )?;
93 if let Some((dv_row, dv_col)) = intrabc {
94 // lane-av1-ibcrect: the reconstruction is ported -- an intrabc block
95 // is is_inter_block, so libaom predicts the whole 128x64/64x128 strip
96 // from this frame's own reconstruction and runs the INTER residual
97 // walk (`decode_token_recon_block`'s else arm) over it.
98 return decode_intrabc_128rect(
99 dec,
100 cdfs,
101 neighbours,
102 (mi_r, mi_c),
103 (bw, bh),
104 skip,
105 (dv_row, dv_col),
106 y,
107 u,
108 v,
109 base_q_idx,
110 fctx,
111 );
112 }
113 if alpha.is_some() || filter_intra.is_some() || palette_y.is_some() || palette_uv.is_some() {
114 return Err(unsupported(
115 "CfL, filter intra or a palette on a 128-root HORZ/VERT intra block (every one of \
116 their size gates caps at 64x64 or below, so none of these symbols exists there)",
117 ));
118 }
119 let uv_predict_mode = if uv_mode == UV_CFL_PRED {
120 DC_PRED
121 } else {
122 uv_mode
123 };
124 let smooth_neighbor = is_smooth_mode(nb_above_mode) || is_smooth_mode(nb_left_mode);
125 if smooth_neighbor {
126 hit!(SMOOTH_LUMA_HITS);
127 }
128 let smooth_neighbor_uv = neighbours.smooth_uv_neighbour(mi_r, mi_c, r, c, fctx);
129 // spec 5.11.16 `read_tx_size`: read for every intra block, skipped or
130 // not. `bsize_to_tx_size_cat(BLOCK_128X64)` is 3 (see this fn's doc).
131 // lane-lossless128: a LOSSLESS frame codes no tx-size symbol at all --
132 // libaom's `read_tx_mode` returns `ONLY_4X4` for a `coded_lossless` frame
133 // before the symbol is even considered (`decodeframe.c:141`) and
134 // `read_tx_size` returns `TX_4X4` before it looks at anything
135 // (`decodeframe.c:1183`). So `tx_select` is off, every plane is walked as
136 // TX_4X4 transform units, and no `tx_size_cat3` symbol may be consumed.
137 let lossless_frame = lossless(fctx);
138 let in_inter = fctx
139 .intra_in_inter_mode
140 .with(std::cell::Cell::get)
141 .is_some();
142 let depth = if tx_select && !lossless_frame {
143 // lane-inter128intra r1: `get_tx_size_context` compares the bands
144 // against `tx_size_wide/high[max_txsize_rect_lookup[bsize]]` --
145 // TX_64X64 for every 128-axis block, hence the `min(64)`.
146 let ctx = tx_size_context_txfm_rect(neighbours, (mi_r, mi_c), bw.min(64), bh.min(64), fctx);
147 dec.symbol(&mut cdfs.tx_size_cat3[ctx])
148 } else {
149 0
150 };
151 if depth != 0 {
152 hit!(TX_DEPTH_HITS);
153 }
154 hit_do! {
155 INTRA_SB128_DEPTH_HITS.with(|c| {
156 let mut h = c.get();
157 h[depth.min(2)] += 1;
158 c.set(h);
159 });
160 }
161 let logical_tx = if lossless_frame { 4 } else { 64 >> depth };
162 if lossless_frame {
163 hit!(INTRA128_LOSSLESS_HITS);
164 }
165 // A 64-point transform codes nothing past its low 32 coefficients.
166 let coeff_tx_side = logical_tx.min(32);
167 let luma_set = if lossless_frame {
168 TxbSet::Luma4
169 } else {
170 txbset_for(logical_tx, reduced_tx_set)
171 };
172 let luma_scan = default_scan(coeff_tx_side);
173 // `av1_get_max_uv_txsize` caps at TX_32X32 normally; a lossless frame
174 // forces every plane to TX_4X4 (its own `Chroma4` set and 4x4 scan).
175 let chroma_tx = if lossless_frame { 4 } else { 32 };
176 let chroma_set = if lossless_frame {
177 TxbSet::Chroma4
178 } else {
179 TxbSet::Chroma32
180 };
181 let chroma_scan = default_scan(chroma_tx);
182 let (cpx, cpy) = (px >> ss_x(fctx), py >> ss_y(fctx));
183 let (chroma_w, chroma_h) = (bw >> ss_x(fctx), bh >> ss_y(fctx));
184 let reach = Reach::of_rect(bw, bh, px, py, y.width, y.height, fctx);
185 let (nch_x, nch_y) = (bw / 64, bh / 64);
186 // The frame's own mi grid (`av1_calc_mi_size`, 8-px aligned): libaom's
187 // `decode_token_recon_block` clips each vector's walk to
188 // `max_block_wide/high`, so a chroma unit whose top-left is off the frame
189 // codes nothing.
190 let mi_cols = 2 * y.true_width.div_ceil(8);
191 let mi_rows = 2 * y.true_height.div_ceil(8);
192 let max_w_mi = (bw / MI).min(mi_cols.saturating_sub(mi_c));
193 let max_h_mi = (bh / MI).min(mi_rows.saturating_sub(mi_r));
194 // Non-lossless: per mu chunk, `(64 >> ss) / chroma_tx` chroma units per
195 // axis -- one 32x32 at 4:2:0, two stacked on the tall axis at 4:2:2
196 // (lane-av1-422bigblock: the chunk's chroma plane block is 32x64 there),
197 // four at 4:4:4 (lane-av1-444sb's own finding on this same walk).
198 debug_assert!(
199 lossless_frame
200 || (chroma_w / chroma_tx, chroma_h / chroma_tx)
201 == (
202 nch_x * ((64 >> ss_x(fctx)) / chroma_tx),
203 nch_y * ((64 >> ss_y(fctx)) / chroma_tx)
204 )
205 );
206 let tpc = 64 / logical_tx;
207 let zero_tu = vec![0i32; logical_tx * logical_tx];
208 let zero_cu = vec![0i32; chroma_tx * chroma_tx];
209 let mut last_grids = [Grid::Zero(0), Grid::Zero(0)];
210 let mut units: Vec<(usize, (usize, usize), Grid)> = Vec::new();
211 // The chroma context arrays are indexed in LUMA mi; every chroma unit of
212 // every chunk shares this luma span (2 chroma pixels per luma unit).
213 let luma_span = chroma_tx << ss_x(fctx);
214 let luma_span_h = chroma_tx << ss_y(fctx);
215 for ch_row in 0..nch_y {
216 for ch_col in 0..nch_x {
217 for tu_row in ch_row * tpc..(ch_row + 1) * tpc {
218 for tu_col in ch_col * tpc..(ch_col + 1) * tpc {
219 let tu_mi = (
220 mi_r + tu_row * (logical_tx / MI),
221 mi_c + tu_col * (logical_tx / MI),
222 );
223 let (tu_px, tu_py) = (px + tu_col * logical_tx, py + tu_row * logical_tx);
224 // libaom clips the transform loop to
225 // `max_blocks_wide/high`: a unit whose top-left sample is
226 // outside the frame is never coded.
227 if tu_px >= y.true_width || tu_py >= y.true_height {
228 continue;
229 }
230 // Edge availability of a unit INSIDE a block, with the
231 // 128 geometry -- `has_top_right` is false for a block
232 // whose largest side is > 64, which [`Reach::of_rect`]
233 // already answered for the block and [`tu_reach`] carries
234 // down to the units on its own top row.
235 let tu_r = tu_reach(
236 bw,
237 bh,
238 tu_col * logical_tx,
239 tu_row * logical_tx,
240 logical_tx,
241 reach,
242 px,
243 py,
244 y.width,
245 y.height,
246 fctx,
247 );
248 if skip {
249 push_intra(
250 y,
251 u,
252 v,
253 0,
254 tu_px,
255 tu_py,
256 logical_tx,
257 mode,
258 angle_delta_y,
259 tu_r,
260 &zero_tu,
261 None,
262 None,
263 smooth_neighbor,
264 fctx,
265 );
266 neighbours.record_mi_luma(tu_mi, logical_tx, &zero_tu);
267 continue;
268 }
269 // The unit is smaller than its block, so `txb_skip_ctx`
270 // is the neighbour-magnitude table, never the lone-TU 0.
271 let tu_skip_ctx = neighbours.luma_skip_ctx(tu_mi, logical_tx / MI);
272 let tu_around = neighbours.around_mi(tu_mi, logical_tx)[0];
273 let tu_grid = read_plane(
274 dec,
275 cdfs,
276 luma_set,
277 luma_scan,
278 0,
279 tu_around,
280 mode,
281 mode,
282 angle_delta_y,
283 tu_r,
284 y,
285 u,
286 v,
287 tu_px,
288 tu_py,
289 logical_tx,
290 coeff_tx_side,
291 base_q_idx,
292 None,
293 None,
294 Some(tu_skip_ctx),
295 smooth_neighbor,
296 fctx,
297 )?;
298 neighbours.record_mi_luma(tu_mi, logical_tx, &tu_grid);
299 }
300 }
301 // Transform units per axis inside this mu chunk's chroma span:
302 // a 64x64 luma chunk covers `64 >> ss` chroma pixels per axis,
303 // so it holds `(64 >> ss) / chroma_tx` units -- one 32x32 at
304 // 4:2:0, four at 4:4:4 (lane-av1-444sb), TWO STACKED TX_32X32 on
305 // the tall axis at 4:2:2 (lane-av1-422bigblock: the chunk's
306 // chroma plane block is 32x64 there, so the two axes' counts
307 // part ways -- 1 x 2); 8x8 TX_4X4 units when lossless.
308 let cn = ((64 >> ss_x(fctx)) / chroma_tx).max(1);
309 let cn_h = ((64 >> ss_y(fctx)) / chroma_tx).max(1);
310 // libaom's per-chunk `unit_width/height` (plane-mi units of the
311 // luma mi grid): `min(mu_blocks + col, max_blocks) >> ss`.
312 let unit_w = ((ch_col + 1) * 16).min(max_w_mi) >> ss_x(fctx);
313 let unit_h = ((ch_row + 1) * 16).min(max_h_mi) >> ss_y(fctx);
314 let stepc = (chroma_tx / 4).max(1);
315 // `decode_token_recon_block` codes a mu chunk PLANE-MAJOR: every U
316 // unit of the chunk, then every V unit. At one unit per plane per
317 // chunk (every non-lossless 128-axis block) that is the same
318 // symbol order the interleaved pair used to read; a lossless
319 // frame's 4x4 chroma units are where it becomes observable.
320 for plane_pass in 1..=2usize {
321 for ci_row in 0..cn_h {
322 for ci_col in 0..cn {
323 // The clamp strides are the CHUNK's chroma mi extent
324 // per axis: 16 luma mi >> the axis's own ss.
325 if ch_col * (16 >> ss_x(fctx)) + ci_col * stepc >= unit_w
326 || ch_row * (16 >> ss_y(fctx)) + ci_row * stepc >= unit_h
327 {
328 continue;
329 }
330 let cu_x = cpx + ch_col * (64 >> ss_x(fctx)) + ci_col * chroma_tx;
331 let cu_y = cpy + ch_row * (64 >> ss_y(fctx)) + ci_row * chroma_tx;
332 let cu_mi = (
333 mi_r + ch_row * 16 + ci_row * ((chroma_tx << ss_y(fctx)) / 4),
334 mi_c + ch_col * 16 + ci_col * ((chroma_tx << ss_x(fctx)) / 4),
335 );
336 let cu_r = tu_reach_rect(
337 bw,
338 bh,
339 ch_col * 64 + ci_col * luma_span,
340 ch_row * 64 + ci_row * luma_span_h,
341 luma_span,
342 luma_span_h,
343 reach,
344 px,
345 py,
346 y.width,
347 y.height,
348 fctx,
349 );
350 if skip {
351 push_intra(
352 y,
353 u,
354 v,
355 plane_pass,
356 cu_x,
357 cu_y,
358 chroma_tx,
359 uv_predict_mode,
360 angle_delta_uv,
361 cu_r,
362 &zero_cu,
363 None,
364 None,
365 smooth_neighbor_uv,
366 fctx,
367 );
368 continue;
369 }
370 // lane-av1422gather3: libaom's `get_txb_ctx_general`
371 // reads the unit's above votes over `txb_w_unit` =
372 // `chroma_tx / 4` CHROMA cells, and at ss_x 1 one
373 // chroma cell spans TWO luma mi columns -- the same
374 // pair rule the mu-chunk walks take
375 // (`around_mi_422_chroma`). Reachability measured on
376 // the pinned `422_intrabc_sb128_strip*.obu` (72 unit
377 // gathers each, `sb128rect_chroma_replay_hits().0`);
378 // output-inert on all 21 committed 4:2:2 cells, so
379 // the source-scan gate
380 // `the_422_block_128rect_chroma_chunk_gather_stays_per_axis`
381 // is what stops a revert.
382 let cu_around = if ss_x(fctx) == 1 && ss_y(fctx) == 0 {
383 neighbours.around_mi_422_chroma(cu_mi, luma_span, luma_span_h)
384 } else {
385 neighbours.around_mi_rect(cu_mi, luma_span, luma_span_h)
386 };
387 let grid = read_plane(
388 dec,
389 cdfs,
390 chroma_set,
391 chroma_scan,
392 plane_pass,
393 cu_around[plane_pass],
394 mode,
395 uv_predict_mode,
396 angle_delta_uv,
397 cu_r,
398 y,
399 u,
400 v,
401 cu_x,
402 cu_y,
403 chroma_tx,
404 chroma_tx,
405 base_q_idx,
406 None,
407 None,
408 Some(3),
409 smooth_neighbor_uv,
410 fctx,
411 )?;
412 last_grids[plane_pass - 1] = grid.clone();
413 // Immediately, so the NEXT unit of this same block
414 // reads this one's coefficient context; replayed after
415 // `record_split_luma_rect` below, which would otherwise
416 // clobber them whole-block.
417 neighbours.record_mi_chroma(
418 cu_mi,
419 luma_span,
420 luma_span_h,
421 plane_pass,
422 &grid,
423 );
424 units.push((plane_pass, cu_mi, grid));
425 }
426 }
427 }
428 if !skip {
429 hit!(CHROMA_SPLIT_TX_HITS);
430 }
431 }
432 }
433 if skip {
434 neighbours.record_rect(
435 at,
436 bw,
437 bh,
438 mode,
439 uv_predict_mode,
440 &[
441 &ZERO_RESIDUAL[..bw * bh],
442 &ZERO_RESIDUAL[..chroma_w * chroma_h],
443 &ZERO_RESIDUAL[..chroma_w * chroma_h],
444 ],
445 );
446 } else {
447 neighbours.record_split_luma_rect(
448 at,
449 bw,
450 bh,
451 mode,
452 uv_predict_mode,
453 [&last_grids[0], &last_grids[1]],
454 );
455 // lane-av1-128rectspan: the replay must re-stamp each unit over the
456 // SAME cells the walk above stamped it over -- `record_mi_chroma`'s
457 // height drives `left[]` and its width `above[]`, and the walk used
458 // the per-axis `luma_span_h`. Passing the width on both axes doubled
459 // the `left[]` run wherever the two differ (4:2:2), reaching 8 mi
460 // rows below the block on the last unit of a 128x64/64x128 strip and
461 // smearing a neighbour chunk's unit over its neighbour's rows.
462 //
463 // 4:2:2 (ss (1,0)) is the one format where the two spans differ, and
464 // it is ADMITTED since lane-av1422lift: this branch does fire there
465 // (`SB128RECT_REPLAY_SPAN_MISMATCH_HITS` reads 9 on each of the two
466 // pinned `422_intrabc_sb128_strip*.obu` cells, whose five frames are
467 // byte-exact against ffmpeg) -- that is the replay's own designed
468 // 4:2:2 case, not a defect signature. Nothing here asserts the
469 // mismatch is zero.
470 if luma_span != luma_span_h {
471 hit!(SB128RECT_REPLAY_SPAN_MISMATCH_HITS);
472 }
473 for (plane, cu_mi, grid) in &units {
474 neighbours.record_mi_chroma(*cu_mi, luma_span, luma_span_h, *plane, grid);
475 hit!(SB128RECT_CHROMA_REPLAY_HITS);
476 }
477 }
478 // No palette exists at this size; the unconditional stamp clears any
479 // stale neighbour palette state, as every other block path does.
480 neighbours.record_palette_y_rect(at, bw, bh, 0, [0u16; 8]);
481 neighbours.record_palette_uv_rect(at, bw, bh, 0, [0u16; 8]);
482 neighbours.fill_skip_grid_rect((mi_r, mi_c), bw / MI, bh / MI, skip);
483 neighbours.fill_lf_grid_rect(
484 (mi_r, mi_c),
485 bw / MI,
486 bh / MI,
487 logical_tx as u8,
488 logical_tx as u8,
489 0,
490 fctx,
491 );
492 if in_inter || allow_intrabc_frame(fctx) {
493 // `set_txfm_ctxs(tx_size, n4_w, n4_h, skip && is_inter, xd)`
494 // (libaom `decodeframe.c` `decode_block`) runs for EVERY block of an
495 // inter frame, intra ones included: an intra block stamps its
496 // TRANSFORM size over its own footprint in both TXFM_CONTEXT bands.
497 // A key frame reads those bands only when it allows intrabc
498 // ([`read_block_tx_size_rect`]'s var-tx context), and then libaom
499 // stamps here too -- lane-av1-intrabc r3.
500 txfm_partition_update_rect(neighbours, (mi_r, mi_c), (logical_tx, logical_tx), (bw, bh));
501 }
502 if in_inter {
503 hit_do! {
504 INTRA128_IN_INTER_HITS.with(|c| {
505 let mut h = c.get();
506 h[usize::from(bw < bh)] += 1;
507 c.set(h);
508 });
509 }
510 } else if bw > bh {
511 hit!(INTRA_SB128_HORZ_HITS);
512 } else {
513 hit!(INTRA_SB128_VERT_HITS);
514 }
515 Ok(())
516}
517
518/// Decodes one 8x8 leaf of a straddling 16x16 block (lane-av1-rect), mirroring
519/// [`crate::tile::write_leaf8`]: its intra-mode context comes from the
520/// *enclosing* 16x16 slot's `above_mode`/`left_mode` (`outer_at`, [`SUB`]-grid),
521/// overridden on whichever axis `prev_leaf` sits along -- the previous leaf's
522/// just-decoded mode, not the enclosing slot's stale one, is the true
523/// neighbour there. `leaf_mi` is this leaf's own position in 4x4 mode-info
524/// units, which is what its coefficient context (finer than [`SUB`]) is read
525/// at. Returns the mode this leaf decoded, for the caller's own
526/// `prev_leaf`/final mode-writeback bookkeeping.
527#[allow(clippy::too_many_arguments)]
528/// The coarse 16x16 `above_mode`/`left_mode` cells a four-way SPLIT of one
529/// 16x16 parent leaves behind (lane-rectx r3). The next block DOWN reads
530/// `above_mode[col]`, and its true above neighbour is the mi directly above
531/// its own top-left corner -- the parent's BOTTOM-LEFT leaf, not the last one
532/// decoded; the next block RIGHT reads `left_mode[row]`, whose true left
533/// neighbour is the parent's TOP-RIGHT leaf. Writing the z-order last leaf
534/// (bottom-right) into both, as this path did, is right only when all four
535/// leaves happen to share a mode. `done` may be short at the true frame edge
536/// (the straddle caller filters positions off the frame), so each side falls
537/// back to the last leaf actually decoded.
538pub(super) fn write_back_split_modes(
539 neighbours: &mut Neighbours,
540 done: &[((usize, usize), usize)],
541 parent_mi: (usize, usize),
542 slot: (usize, usize),
543) {
544 let (sr, sc) = slot;
545 let Some(&(_, last)) = done.last() else {
546 return;
547 };
548 let find = |mi: (usize, usize)| done.iter().find(|&&(at, _)| at == mi).map(|&(_, m)| m);
549 neighbours.above_mode[sc] = find((parent_mi.0 + 2, parent_mi.1)).unwrap_or(last);
550 neighbours.left_mode[sr] = find((parent_mi.0, parent_mi.1 + 2)).unwrap_or(last);
551}
552
553// Signature mirrors the spec's per-call inputs; a parameter struct would only move them.
554#[allow(clippy::too_many_arguments)]
555pub(super) fn decode_leaf8(
556 dec: &mut SymbolDecoder,
557 cdfs: &mut Cdfs,
558 neighbours: &mut Neighbours,
559 outer_at: (usize, usize),
560 leaf_mi: (usize, usize),
561 scans: (&[u16], &[u16]),
562 prev_leaf: Option<((usize, usize), usize)>,
563 y: &mut PlaneBuf<'static>,
564 u: &mut PlaneBuf<'static>,
565 v: &mut PlaneBuf<'static>,
566 base_q_idx: u8,
567 enable_filter_intra: bool,
568 allow_screen_content_tools: bool,
569 allow_intrabc: bool,
570 tx_select: bool,
571 reduced_tx_set: bool,
572 fctx: &crate::decode::FrameCtx,
573) -> Result<usize> {
574 let (r, c) = outer_at;
575 // lane-modectx r1: the group's own availability-correct fallback pair, not
576 // the raw coarse bands (which leak across a tile edge -- see
577 // `modes_above_left`).
578 let (mut above_mode, mut left_mode) = neighbours.modes_above_left(r, c);
579 if let Some(((pr, pc), pmode)) = prev_leaf {
580 if pc == leaf_mi.1 && leaf_mi.0 == pr + 2 {
581 above_mode = pmode;
582 } else if pr == leaf_mi.0 && leaf_mi.1 == pc + 2 {
583 left_mode = pmode;
584 }
585 }
586 // The mi-exact neighbour wins over both the coarse 16x16 slot and
587 // `prev_leaf`: one 16x16 slot cannot hold the two different modes its
588 // two 8x8 (or four 4x4) columns leave behind, so a leaf whose above/left
589 // neighbour sits in a split cell read the wrong `kf_y_mode` row.
590 // lane-rectsplitx r2 swept this arm into
591 // [`Neighbours::modes_above_left_mi`]: the mi-exact map ALSO has to answer
592 // "no neighbour" at a tile edge, where the coarse cell holds the block
593 // above/left of it inside the same 16x16.
594 let (above_mode, left_mode) =
595 neighbours.modes_above_left_mi(leaf_mi.0, leaf_mi.1, (above_mode, left_mode));
596 let smooth_neighbor = is_smooth_mode(above_mode) || is_smooth_mode(left_mode);
597 // Chroma's edge-filter type reads the CHROMA neighbour's `uv_mode`
598 // (`get_intra_edge_filter_type`, reconintra.c:974), never the luma mode
599 // this leaf's `smooth_neighbor` is built from -- passing `false` here
600 // filtered a directional chroma block's edge at the wrong strength
601 // (lane-sub8 r5: 4 pixels of one 8x8 leaf's chroma, deltas 1..3).
602 // The `uv_mode` neighbours are mi-exact (`uv_mode_col`/`uv_mode_row`),
603 // with the coarse [`SUB`] slot as the fallback when no block recorded
604 // that exact mi cell.
605 let smooth_neighbor_uv = neighbours.smooth_uv_neighbour(leaf_mi.0, leaf_mi.1, r, c, fctx);
606 if smooth_neighbor {
607 hit!(SMOOTH_LUMA_HITS);
608 }
609 // An 8x8 leaf is well within `is_cfl_allowed`'s <=32x32 bound (spec
610 // 5.11.5), so it reads the CFL-allowed `uv_mode_cfl` CDF, like every other
611 // `decode_block` caller at 16x16 and up.
612 // lane-pal8 r1: this leaf's own palette-Y mode ctx and colour cache
613 // (`av1_get_palette_mode_ctx` / `av1_get_palette_cache`, decodemv.c:567,
614 // `read_palette_mode_info`), at its REAL mi position -- passing
615 // `None`/`&[]` read `palette_y_mode` off CDF row 0 and refused by name the
616 // moment a real 8x8 palette block fired, which is where every 8-bit
617 // `--tune-content=screen --min-partition-size=8` stream stopped.
618 let (palette_ctx, palette_cache) = neighbours.palette_ctx_and_cache_mi(leaf_mi);
619 let palette_uv_cache = neighbours.palette_uv_cache_mi(leaf_mi);
620 let (
621 skip,
622 mode,
623 angle_delta_y,
624 uv_mode,
625 angle_delta_uv,
626 alpha,
627 filter_intra,
628 palette_y,
629 palette_uv,
630 ) = read_intra_mode(
631 dec,
632 cdfs,
633 above_mode,
634 left_mode,
635 true,
636 8,
637 enable_filter_intra,
638 neighbours.skip_txfm_ctx(leaf_mi.0, leaf_mi.1),
639 allow_screen_content_tools,
640 allow_intrabc,
641 Some((palette_ctx, &palette_cache)),
642 &palette_uv_cache,
643 leaf_mi.0,
644 leaf_mi.1,
645 fctx,
646 )?;
647 // lane-kf900 r7: an 8x8 leaf consumed `use_intrabc` (and its DV) through
648 // `read_intra_mode` and then decoded the block as if it were ordinary
649 // intra. libaom's `is_inter_block` counts an intrabc block
650 // (`blockd.h:373`), so its luma `tx_type` comes off the INTER ext-tx set
651 // (`av1_get_ext_tx_set_type(TX_8X8, is_inter=1, 0)` = `EXT_TX_SET_ALL16`,
652 // a 16-symbol CDF) while we read the 7-symbol `TX_SET_INTRA_1` row: the
653 // same value with a different interval, so the tile desynced at the very
654 // next symbol (class `wrong-alphabet-same-value`). Everything below
655 // mirrors `decode_block`'s own intrabc handling at 16x16 and up.
656 let intrabc_dv = fctx.intrabc_dv.with(|c| c.take());
657 if intrabc_dv.is_some() {
658 hit!(LEAF8_INTRABC_HITS);
659 }
660 // `av1_visit_palette`'s reconstruction (decodeframe.c:1135): the colour
661 // index map, mapped through the base colours, IS the block's prediction --
662 // carried to the next `reconstruct`/`read_plane` through the same
663 // [`PALETTE_PRED`] slot [`decode_block`] uses at 16x16 and up.
664 let palette_y_buf: Option<Vec<u16>> = palette_y
665 .as_ref()
666 .map(|p| p.map.iter().map(|&i| p.colors[i as usize]).collect());
667 let palette_uv_bufs: Option<(Vec<u16>, Vec<u16>)> = palette_uv.as_ref().map(|p| {
668 (
669 p.map.iter().map(|&i| p.u_colors[i as usize]).collect(),
670 p.map.iter().map(|&i| p.v_colors[i as usize]).collect(),
671 )
672 });
673 let (pal_y_size, pal_y_colors) = palette_y
674 .as_ref()
675 .map_or((0, [0u16; 8]), |p| (p.size, p.colors));
676 let (pal_uv_size, pal_uv_colors) = palette_uv
677 .as_ref()
678 .map_or((0, [0u16; 8]), |p| (p.size, p.u_colors));
679 if palette_y.is_some() || palette_uv.is_some() {
680 hit!(PALETTE_LEAF8_HITS);
681 }
682 let uv_predict_mode = if uv_mode == UV_CFL_PRED {
683 DC_PRED
684 } else {
685 uv_mode
686 };
687 neighbours.above_uv_mode[c] = uv_predict_mode;
688 neighbours.left_uv_mode[r] = uv_predict_mode;
689 neighbours.record_uv_mode_mi(leaf_mi.0, leaf_mi.1, 2, 2, uv_predict_mode);
690 // `TxMode::Select`'s `tx_depth` at an 8x8 leaf (spec 5.11.16): the only
691 // depths an 8x8 block offers are `TX8` (depth 0) and `TX4` (depth 1,
692 // which splits the leaf's own 8x8 prediction into a 2x2 grid of 4x4
693 // transform units, same raster-order per-TU pattern `decode_block`'s
694 // multi-TU branch runs).
695 // ... and an intrabc leaf reads no intra `tx_depth` symbol at all: libaom
696 // dispatches `read_block_tx_size` on `is_inter_block`, which counts an
697 // intrabc block, so a SKIPPED one takes the largest transform and an
698 // UNSKIPPED one reads the inter var-tx tree (lane-t900 r32). At 8x8 that
699 // tree is one `txfm_split` symbol: TX_8X8, or TX_4X4 over all four mi
700 // cells (`read_var_tx_size`'s `sub_txs == TX_4X4` early return).
701 // `parse_decode_block`'s tree condition carries
702 // `!xd->lossless[mbmi->segment_id]` (see [`decode_block`]'s own copy):
703 // a lossless segment's intrabc leaf reads no tree, TX_4X4 via
704 // `read_tx_size`'s first line (lane-av1mixloss).
705 let resolved = if tx_select && intrabc_dv.is_some() && !skip && !lossless(fctx) {
706 let mut leaves = Vec::new();
707 let mi_cols = 2 * y.true_width.div_ceil(8);
708 let mi_rows = 2 * y.true_height.div_ceil(8);
709 read_var_tx_size(
710 dec,
711 cdfs,
712 neighbours,
713 leaf_mi,
714 8,
715 (
716 2usize.min(mi_cols.saturating_sub(leaf_mi.1)),
717 2usize.min(mi_rows.saturating_sub(leaf_mi.0)),
718 ),
719 (8, 8),
720 0,
721 0,
722 0,
723 &mut leaves,
724 );
725 hit!(INTRABC_VARTX_HITS);
726 leaves.first().map_or(8, |l| l.2)
727 } else if tx_select && intrabc_dv.is_none() {
728 read_tx_size(dec, cdfs, neighbours, leaf_mi, 8, fctx)
729 } else {
730 // lane-lossless: TX_4X4 on a lossless frame, no symbol
731 // (libaom `read_tx_size`).
732 lossless_tx(8, fctx)
733 };
734 let (px, py) = (leaf_mi.1 * MI, leaf_mi.0 * MI);
735 let reach = Reach::of(8, px, py, y.width, y.height, fctx);
736 let (cpx, cpy) = (px >> ss_x(fctx), py >> ss_y(fctx));
737 // 4:4:4: this 8x8 block's chroma plane block is 8x8 (same scan as luma,
738 // `scans.0`). 4:2:0 stays the 4x4 unit this leaf was written for.
739 // lane-av1-422d: 4:2:2 (ss 1/0) halves only the width, so the plane block
740 // is 4x8 -- ONE rect transform unit per plane (`av1_get_max_uv_txsize(
741 // BLOCK_8X8)` at ss (1,0) is `max_txsize_rect_lookup[BLOCK_4X8]` ==
742 // TX_4X8): one txb_skip + one scan per plane in the
743 // [`TxbSet::ChromaRect8x4`] class [`decode_rect_split`] proves for the
744 // shape, read through [`leaf8_chroma422_unit`] in each arm below. The
745 // `(csz, cset, cscan)` selection keeps serving the square arms (444's
746 // 8x8, 420's 4x4); the rect arms branch on [`chroma_422`] instead.
747 let chroma_444 = ss_x(fctx) == 0 && ss_y(fctx) == 0;
748 let chroma_422 = ss_x(fctx) == 1 && ss_y(fctx) == 0;
749 let (csz, cset, cscan) = if chroma_444 {
750 (8usize, TxbSet::Chroma8, scans.0)
751 } else {
752 (4usize, TxbSet::Chroma4, scans.1)
753 };
754 // spec 7.11.3.4 with `use_intrabc`, same as `decode_block`: predict from
755 // the CURRENT frame's own pre-loop-filter reconstruction (every loop
756 // filter is off whenever `allow_intrabc`, frame.rs:194/210/252/272) at
757 // the full-pel block vector, with the BILINEAR kernel libaom forces.
758 let intrabc_bufs = intrabc_dv.map(|(dv_row, dv_col)| {
759 let mut yb = vec![0u16; 8 * 8];
760 mc::predict_with_filter(
761 &y.data,
762 y.width,
763 y.true_width,
764 y.true_height,
765 mv_to_q4(px, dv_col, 0),
766 mv_to_q4(py, dv_row, 0),
767 8,
768 8,
769 mc::InterpFilterKind::Bilinear,
770 &mut yb,
771 fctx,
772 );
773 // The chroma extent is THIS frame's subsampled size of the 8x8 luma
774 // leaf (`get_plane_block_size`): 4x4 at 4:2:0, 4x8 at 4:2:2, 8x8 at
775 // 4:4:4. The literal 4x4 this replaces hardcoded the 4:2:0 halving --
776 // at 4:4:4 the 16-sample override fed this leaf's own 8x8 chroma
777 // reconstruct, which indexed `prediction[16]` of a 16-sample buffer
778 // (len 16, index 16 at `PlaneBuf::reconstruct`), and at 4:2:2 it
779 // covered half the leaf's 4x8 chroma plane block (lane-av1-422d's
780 // `if chroma_422 { (4, 8) }` arm, which the ss-derived form subsumes:
781 // `8 >> ss_x` is 4 and `8 >> ss_y` is 8 there). The same buffers
782 // are windowed at stride 8 by `read_intra_chroma_lossless` (the TX4
783 // 4:4:4-lossless arm) and consumed whole by the TX8 and skip arms,
784 // so all three heal with this one sizing.
785 let (cw, ch) = (8 >> ss_x(fctx), 8 >> ss_y(fctx));
786 let mut ub = vec![0u16; cw * ch];
787 mc::predict_with_filter(
788 &u.data,
789 u.width,
790 u.true_width,
791 u.true_height,
792 mv_to_q4(cpx, dv_col, ss_x(fctx)),
793 mv_to_q4(cpy, dv_row, ss_y(fctx)),
794 cw,
795 ch,
796 mc::InterpFilterKind::Bilinear,
797 &mut ub,
798 fctx,
799 );
800 let mut vb = vec![0u16; cw * ch];
801 mc::predict_with_filter(
802 &v.data,
803 v.width,
804 v.true_width,
805 v.true_height,
806 mv_to_q4(cpx, dv_col, ss_x(fctx)),
807 mv_to_q4(cpy, dv_row, ss_y(fctx)),
808 cw,
809 ch,
810 mc::InterpFilterKind::Bilinear,
811 &mut vb,
812 fctx,
813 );
814 (yb, ub, vb)
815 });
816 // The per-plane prediction override rides the same [`PALETTE_PRED`] slot
817 // `decode_block` uses: set fresh right before each plane's own
818 // `reconstruct`/`read_plane` call (the slot is taken, not copied).
819 // lane-t900 r32: through `palette_y_buf`, so the 4x4 TU loop below windows
820 // it per unit (an intrabc leaf whose var-tx tree split to TX_4X4 is the
821 // first shape that reaches that loop with an override set).
822 let palette_y_buf = match &intrabc_bufs {
823 Some((yb, _, _)) => Some(yb.clone()),
824 None => palette_y_buf,
825 };
826 // lane-av1intrapred: the chroma twin of the `palette_y_buf` wrap above --
827 // `decode_block` does the same for its own `palette_uv_bufs`
828 // (decode.rs's single-TU and multi-TU chroma reads). Without it, an
829 // intrabc leaf whose var-tx tree split to TX_4X4 reached the TX4 arm's
830 // chroma reads with the override unset: the unit predicted INTRA from
831 // the UV mode (intrabc's is DC) instead of copying the frame at the DV,
832 // R5 first differing pixel U(184,4), block mi (2,92).
833 let palette_uv_bufs = match &intrabc_bufs {
834 Some((_, ub, vb)) => Some((ub.clone(), vb.clone())),
835 None => palette_uv_bufs,
836 };
837 let (luma_grid, u_grid, v_grid);
838 if skip {
839 // lane-hgkf r2 (same shape as `decode_block`'s own skip branch): an
840 // 8x8 leaf whose `tx_depth` resolved to TX_4X4 predicts FOUR 4x4
841 // transform units, each off the units already written left/above of
842 // it inside this same leaf -- an intra block's transform size is read
843 // whether or not it is skipped, and spec 7.11.2 predicts per unit.
844 // A palette block's prediction is the whole-block index map, never
845 // per-transform-unit edges ([`decode_block`]'s own `split_tx_skip`
846 // rule) -- so a palette leaf takes the single 8x8 reconstruct below
847 // even when `tx_depth` resolved to TX_4X4.
848 if resolved == 4 && palette_y_buf.is_none() {
849 hit!(SKIP_SPLIT_TX_HITS);
850 for tu_row in 0..2 {
851 for tu_col in 0..2 {
852 let (tu_px, tu_py) = (px + tu_col * 4, py + tu_row * 4);
853 if tu_px >= y.true_width || tu_py >= y.true_height {
854 continue;
855 }
856 let tu_reach = tu_reach(
857 8,
858 8,
859 tu_col * 4,
860 tu_row * 4,
861 4,
862 reach,
863 px,
864 py,
865 y.width,
866 y.height,
867 fctx,
868 );
869 push_intra(
870 y,
871 u,
872 v,
873 0,
874 tu_px,
875 tu_py,
876 4,
877 mode,
878 angle_delta_y,
879 tu_reach,
880 &ZERO_RESIDUAL[..16],
881 None,
882 filter_intra,
883 smooth_neighbor,
884 fctx,
885 );
886 }
887 }
888 } else {
889 if let Some(buf) = &palette_y_buf {
890 set_palette_pred(buf.clone(), fctx);
891 }
892 push_intra(
893 y,
894 u,
895 v,
896 0,
897 px,
898 py,
899 8,
900 mode,
901 angle_delta_y,
902 reach,
903 &ZERO_RESIDUAL[..64],
904 None,
905 filter_intra,
906 smooth_neighbor,
907 fctx,
908 );
909 }
910 let ac = alpha.map(|_| cfl_src(px, py, 8));
911 // Three skip-arm shapes, dispatched on THIS frame's subsampling.
912 // lane-av1-422d: at 4:2:2 a skipped leaf still predicts its 4x8 rect
913 // unit per plane (CfL keeps its contribution, [`decode_block`]'s own
914 // skip-arm rule); no coefficient symbol exists, so the entropy
915 // context never enters the rect read.
916 // lane-av1422llintra: NOT in a LOSSLESS frame -- `av1_get_tx_size`'s
917 // first line returns TX_4X4 on every plane there, so the 4x8 plane
918 // block is two 4x4 units and the `else` arm's per-unit walk is the
919 // right one.
920 if chroma_422 && !lossless(fctx) {
921 u_grid = leaf8_chroma422_unit(
922 dec,
923 cdfs,
924 (false, false, 0),
925 uv_predict_mode,
926 angle_delta_uv,
927 reach,
928 1,
929 alpha.zip(ac).map(|((au, _), ac)| (au, ac)),
930 true,
931 intrabc_bufs
932 .as_ref()
933 .map(|(_, ub, _)| ub.clone())
934 .or_else(|| palette_uv_bufs.as_ref().map(|(ub, _)| ub.clone())),
935 y,
936 u,
937 v,
938 cpx,
939 cpy,
940 smooth_neighbor_uv,
941 fctx,
942 )?;
943 v_grid = leaf8_chroma422_unit(
944 dec,
945 cdfs,
946 (false, false, 0),
947 uv_predict_mode,
948 angle_delta_uv,
949 reach,
950 2,
951 alpha.zip(ac).map(|((_, av), ac)| (av, ac)),
952 true,
953 intrabc_bufs
954 .as_ref()
955 .map(|(_, _, vb)| vb.clone())
956 .or_else(|| palette_uv_bufs.as_ref().map(|(_, vb)| vb.clone())),
957 y,
958 u,
959 v,
960 cpx,
961 cpy,
962 smooth_neighbor_uv,
963 fctx,
964 )?;
965 luma_grid = Grid::Zero(64);
966 } else {
967 // 4:4:4 AND 4:2:2 lossless: the leaf's chroma plane block is a
968 // raster of TX_4X4 units (`av1_get_tx_size` returns TX_4X4 for
969 // EVERY plane of a lossless block, so `av1_get_max_uv_txsize` --
970 // the TX_4X8 the 4:2:2 arm above reads -- is never reached), so a
971 // skipped leaf predicts per unit -- each from its own edges -- like
972 // the luma `SKIP_SPLIT_TX` loop above. At 4:2:2 that plane block is
973 // 4x8 (`av1_ss_size_lookup[BLOCK_8X8][1][0]`), so it is one 4x4
974 // unit per chroma row pair. CfL never fires here (`is_cfl_allowed`
975 // at lossless needs plane_bsize == BLOCK_4X4; this plane block is
976 // 8x8 at 4:4:4 and 4x8 at 4:2:2, neither of which is BLOCK_4X4),
977 // and intrabc/palette keep the whole-plane-block prediction they
978 // already run.
979 let chroma_units_ll = (chroma_444 || chroma_422)
980 && lossless(fctx)
981 && intrabc_bufs.is_none()
982 && palette_uv_bufs.is_none();
983 let cu_shape: (usize, usize) = if chroma_422 { (4, 8) } else { (8, 8) };
984 if let Some((_, ub, _)) = &intrabc_bufs {
985 set_palette_pred(ub.clone(), fctx);
986 } else if let Some((ub, _)) = &palette_uv_bufs {
987 set_palette_pred(ub.clone(), fctx);
988 }
989 if chroma_units_ll {
990 for cu_row in 0..cu_shape.1 / 4 {
991 for cu_col in 0..cu_shape.0 / 4 {
992 let cu_reach = tu_reach(
993 8,
994 8,
995 cu_col * 4,
996 cu_row * 4,
997 4,
998 reach,
999 px,
1000 py,
1001 y.width,
1002 y.height,
1003 fctx,
1004 );
1005 push_intra(
1006 y,
1007 u,
1008 v,
1009 1,
1010 cpx + cu_col * 4,
1011 cpy + cu_row * 4,
1012 4,
1013 uv_predict_mode,
1014 angle_delta_uv,
1015 cu_reach,
1016 &ZERO_RESIDUAL[..16],
1017 None,
1018 None,
1019 smooth_neighbor_uv,
1020 fctx,
1021 );
1022 }
1023 }
1024 } else {
1025 // lane-av1422mixll10: with a prediction override (intrabc's
1026 // frame copy at the DV, or a palette colour map), a skipped
1027 // leaf predicts its WHOLE chroma plane block, not one csz x
1028 // csz corner of it. At 4:2:2 that plane block is 4x8 (csz
1029 // stays 4), so the single 4x4 push below left rows
1030 // `cpy+4..cpy+8` unwritten -- sentinel-poisoned at frame 0
1031 // (cell5 U(140..143,188..191) read 0 against the oracle's
1032 // 664) because an intrabc+lossless leaf takes exactly this
1033 // arm (`chroma_units_ll` requires `intrabc_bufs.is_none()`).
1034 // Window the override per 4x4 unit off the cw-stride buffer,
1035 // the same slice `read_intra_chroma_lossless` feeds
1036 // `set_palette_pred` with.
1037 let (cw, ch) = (8 >> ss_x(fctx), 8 >> ss_y(fctx));
1038 let has_override = intrabc_bufs.is_some() || palette_uv_bufs.is_some();
1039 if chroma_422 && has_override && ch == 8 {
1040 let ub = intrabc_bufs
1041 .as_ref()
1042 .map(|(_, ub, _)| ub.clone())
1043 .or_else(|| palette_uv_bufs.as_ref().map(|(ub, _)| ub.clone()));
1044 let vb = intrabc_bufs
1045 .as_ref()
1046 .map(|(_, _, vb)| vb.clone())
1047 .or_else(|| palette_uv_bufs.as_ref().map(|(_, vb)| vb.clone()));
1048 for (buf, plane) in [(ub, 1usize), (vb, 2usize)] {
1049 if let Some(buf) = buf {
1050 for cu_row in 0..2 {
1051 let cu_reach = tu_reach(
1052 8,
1053 8,
1054 0,
1055 cu_row * 4,
1056 4,
1057 reach,
1058 px,
1059 py,
1060 y.width,
1061 y.height,
1062 fctx,
1063 );
1064 set_palette_pred(
1065 palette_window(&buf, cw, 0, cu_row * 4, 4, 4),
1066 fctx,
1067 );
1068 push_intra(
1069 y,
1070 u,
1071 v,
1072 plane,
1073 cpx,
1074 cpy + cu_row * 4,
1075 4,
1076 uv_predict_mode,
1077 angle_delta_uv,
1078 cu_reach,
1079 &ZERO_RESIDUAL[..16],
1080 None,
1081 None,
1082 smooth_neighbor_uv,
1083 fctx,
1084 );
1085 }
1086 }
1087 }
1088 } else {
1089 push_intra(
1090 y,
1091 u,
1092 v,
1093 1,
1094 cpx,
1095 cpy,
1096 csz,
1097 uv_predict_mode,
1098 angle_delta_uv,
1099 reach,
1100 &ZERO_RESIDUAL[..csz * csz],
1101 alpha.zip(ac).map(|((au, _), ac)| (au, ac)),
1102 None,
1103 smooth_neighbor_uv,
1104 fctx,
1105 );
1106 }
1107 }
1108 if let Some((_, _, vb)) = &intrabc_bufs {
1109 set_palette_pred(vb.clone(), fctx);
1110 } else if let Some((_, vb)) = &palette_uv_bufs {
1111 set_palette_pred(vb.clone(), fctx);
1112 }
1113 if chroma_units_ll {
1114 for cu_row in 0..cu_shape.1 / 4 {
1115 for cu_col in 0..cu_shape.0 / 4 {
1116 let cu_reach = tu_reach(
1117 8,
1118 8,
1119 cu_col * 4,
1120 cu_row * 4,
1121 4,
1122 reach,
1123 px,
1124 py,
1125 y.width,
1126 y.height,
1127 fctx,
1128 );
1129 push_intra(
1130 y,
1131 u,
1132 v,
1133 2,
1134 cpx + cu_col * 4,
1135 cpy + cu_row * 4,
1136 4,
1137 uv_predict_mode,
1138 angle_delta_uv,
1139 cu_reach,
1140 &ZERO_RESIDUAL[..16],
1141 None,
1142 None,
1143 smooth_neighbor_uv,
1144 fctx,
1145 );
1146 }
1147 }
1148 } else {
1149 // lane-av1422mixll10: same whole-plane-block rule as the U
1150 // plane above -- with an override, both 4x4 units of the 4x8
1151 // plane block predict, not just the first.
1152 let (cw, ch) = (8 >> ss_x(fctx), 8 >> ss_y(fctx));
1153 let has_override = intrabc_bufs.is_some() || palette_uv_bufs.is_some();
1154 if chroma_422 && has_override && ch == 8 {
1155 let vb = intrabc_bufs
1156 .as_ref()
1157 .map(|(_, _, vb)| vb.clone())
1158 .or_else(|| palette_uv_bufs.as_ref().map(|(_, vb)| vb.clone()));
1159 if let Some(vb) = vb {
1160 for cu_row in 0..2 {
1161 let cu_reach = tu_reach(
1162 8,
1163 8,
1164 0,
1165 cu_row * 4,
1166 4,
1167 reach,
1168 px,
1169 py,
1170 y.width,
1171 y.height,
1172 fctx,
1173 );
1174 set_palette_pred(palette_window(&vb, cw, 0, cu_row * 4, 4, 4), fctx);
1175 push_intra(
1176 y,
1177 u,
1178 v,
1179 2,
1180 cpx,
1181 cpy + cu_row * 4,
1182 4,
1183 uv_predict_mode,
1184 angle_delta_uv,
1185 cu_reach,
1186 &ZERO_RESIDUAL[..16],
1187 None,
1188 None,
1189 smooth_neighbor_uv,
1190 fctx,
1191 );
1192 }
1193 }
1194 } else {
1195 push_intra(
1196 y,
1197 u,
1198 v,
1199 2,
1200 cpx,
1201 cpy,
1202 csz,
1203 uv_predict_mode,
1204 angle_delta_uv,
1205 reach,
1206 &ZERO_RESIDUAL[..csz * csz],
1207 alpha.zip(ac).map(|((_, av), ac)| (av, ac)),
1208 None,
1209 smooth_neighbor_uv,
1210 fctx,
1211 );
1212 }
1213 }
1214 luma_grid = Grid::Zero(64);
1215 u_grid = Grid::Zero(csz * csz);
1216 v_grid = Grid::Zero(csz * csz);
1217 }
1218 } else if resolved != 4 {
1219 let around = neighbours.around_mi(leaf_mi, 8);
1220 // lane-av1-422h: the 4:2:2 chroma rect unit's `get_txb_ctx_general`
1221 // reads its above votes over CHROMA 4-px cells -- at ss_x 1 one chroma
1222 // cell spans TWO luma mi columns, so the per-luma-mi sum counts each
1223 // above chroma column twice and `dc_sign_ctx`'s signum flips whenever
1224 // the doubled sum crosses zero (the same class
1225 // [`Neighbours::around_mi_422_chroma`] fixes for the rect readers;
1226 // measured at mi(2,24) plane 2: ours dc_sign row 0, oracle row 2).
1227 // The left extent is unsubsquared at ss_y 0 and stays one-to-one.
1228 let chroma_around = if chroma_422 {
1229 neighbours.around_mi_422_chroma(leaf_mi, 8, 8)
1230 } else {
1231 around
1232 };
1233 if let Some(buf) = &palette_y_buf {
1234 set_palette_pred(buf.clone(), fctx);
1235 }
1236 luma_grid = read_plane(
1237 dec,
1238 cdfs,
1239 if intrabc_dv.is_some() {
1240 inter_txbset_for(8, reduced_tx_set)
1241 } else if reduced_tx_set {
1242 TxbSet::Luma8
1243 } else {
1244 TxbSet::Luma8Set1
1245 },
1246 scans.0,
1247 0,
1248 around[0],
1249 mode,
1250 mode,
1251 angle_delta_y,
1252 reach,
1253 y,
1254 u,
1255 v,
1256 px,
1257 py,
1258 8,
1259 TX8,
1260 base_q_idx,
1261 None,
1262 filter_intra,
1263 None,
1264 smooth_neighbor,
1265 fctx,
1266 )?;
1267 // `av1_get_tx_type` branches `PLANE_TYPE_UV` on `is_inter_block`, so
1268 // an intrabc block's chroma tx_type is the colocated LUMA transform's
1269 // coded type (lane-kf900 r6's own fix, `decode_block`'s line).
1270 if intrabc_dv.is_some() {
1271 fctx.intrabc_chroma_tx
1272 .with(|c| c.set(Some(fctx.luma_tx_type.with(std::cell::Cell::get))));
1273 }
1274 let ac = alpha.map(|_| cfl_src(px, py, 8));
1275 if chroma_422 {
1276 // lane-av1-422d: ONE 4x8 rect unit per plane (`av1_get_max_uv_txsize`
1277 // at ss (1,0) == TX_4X8), covering the plane block exactly, off the
1278 // leaf's own 8x8-span entropy context (`chroma_tiled`'s convention).
1279 u_grid = leaf8_chroma422_unit(
1280 dec,
1281 cdfs,
1282 chroma_around[1],
1283 uv_predict_mode,
1284 angle_delta_uv,
1285 reach,
1286 1,
1287 alpha.zip(ac).map(|((au, _), ac)| (au, ac)),
1288 false,
1289 intrabc_bufs
1290 .as_ref()
1291 .map(|(_, ub, _)| ub.clone())
1292 .or_else(|| palette_uv_bufs.as_ref().map(|(ub, _)| ub.clone())),
1293 y,
1294 u,
1295 v,
1296 cpx,
1297 cpy,
1298 smooth_neighbor_uv,
1299 fctx,
1300 )?;
1301 v_grid = leaf8_chroma422_unit(
1302 dec,
1303 cdfs,
1304 chroma_around[2],
1305 uv_predict_mode,
1306 angle_delta_uv,
1307 reach,
1308 2,
1309 alpha.zip(ac).map(|((_, av), ac)| (av, ac)),
1310 false,
1311 intrabc_bufs
1312 .as_ref()
1313 .map(|(_, _, vb)| vb.clone())
1314 .or_else(|| palette_uv_bufs.as_ref().map(|(_, vb)| vb.clone())),
1315 y,
1316 u,
1317 v,
1318 cpx,
1319 cpy,
1320 smooth_neighbor_uv,
1321 fctx,
1322 )?;
1323 } else {
1324 if let Some((_, ub, _)) = &intrabc_bufs {
1325 set_palette_pred(ub.clone(), fctx);
1326 } else if let Some((ub, _)) = &palette_uv_bufs {
1327 set_palette_pred(ub.clone(), fctx);
1328 }
1329 u_grid = read_plane(
1330 dec,
1331 cdfs,
1332 cset,
1333 cscan,
1334 1,
1335 around[1],
1336 mode,
1337 uv_predict_mode,
1338 angle_delta_uv,
1339 reach,
1340 y,
1341 u,
1342 v,
1343 cpx,
1344 cpy,
1345 csz,
1346 csz,
1347 base_q_idx,
1348 alpha.zip(ac).map(|((au, _), ac)| (au, ac)),
1349 None,
1350 None,
1351 smooth_neighbor_uv,
1352 fctx,
1353 )?;
1354 if let Some((_, _, vb)) = &intrabc_bufs {
1355 set_palette_pred(vb.clone(), fctx);
1356 } else if let Some((_, vb)) = &palette_uv_bufs {
1357 set_palette_pred(vb.clone(), fctx);
1358 }
1359 v_grid = read_plane(
1360 dec,
1361 cdfs,
1362 cset,
1363 cscan,
1364 2,
1365 around[2],
1366 mode,
1367 uv_predict_mode,
1368 angle_delta_uv,
1369 reach,
1370 y,
1371 u,
1372 v,
1373 cpx,
1374 cpy,
1375 csz,
1376 csz,
1377 base_q_idx,
1378 alpha.zip(ac).map(|((_, av), ac)| (av, ac)),
1379 None,
1380 None,
1381 smooth_neighbor_uv,
1382 fctx,
1383 )?;
1384 }
1385 // Disarm: `INTRABC_CHROMA_TX` is `Some` across exactly this block's
1386 // two chroma reads (`decode_block`'s own tail does the same) -- left
1387 // set, every later chroma block would inherit this luma tx_type,
1388 // which picks the scan and the inverse transform.
1389 fctx.intrabc_chroma_tx.with(|c| c.set(None));
1390 } else {
1391 // `tx_depth` resolved this leaf's luma to `TX4`: a 2x2 raster-order
1392 // grid of 4x4 transform units, each predicted from the leaf's own
1393 // 8x8 `mode` and reading its own fresh context off the earlier
1394 // units in this same leaf (the same per-TU pattern `decode_block`'s
1395 // multi-TU branch runs at bigger blocks).
1396 let mut first_leaf_tx: Option<TxType> = None;
1397 for tu_row in 0..2 {
1398 for tu_col in 0..2 {
1399 let tu_mi = (leaf_mi.0 + tu_row, leaf_mi.1 + tu_col);
1400 let tu_px = px + tu_col * 4;
1401 let tu_py = py + tu_row * 4;
1402 let tu_around = neighbours.around_mi(tu_mi, 4)[0];
1403 if crate::encode::Reach::in_vert_ab() {
1404 hit!(VERT_AB_TX4_HITS);
1405 }
1406 let tu_reach = tu_reach(
1407 8,
1408 8,
1409 tu_col * 4,
1410 tu_row * 4,
1411 4,
1412 reach,
1413 px,
1414 py,
1415 y.width,
1416 y.height,
1417 fctx,
1418 );
1419 let tu_skip_ctx = neighbours.luma_skip_ctx(tu_mi, 1);
1420 // Each 4x4 unit predicts from its own window of the block's
1421 // 8x8 colour-index map ([`decode_block`]'s per-TU slice at
1422 // 16x16 and up) -- the whole-block buffer would be taken by
1423 // the FIRST unit and indexed at the wrong stride.
1424 if let Some(buf) = &palette_y_buf {
1425 hit!(PALETTE_SPLIT_TX_HITS);
1426 let mut window = vec![0u16; 16];
1427 for row in 0..4 {
1428 let src = (tu_row * 4 + row) * 8 + tu_col * 4;
1429 window[row * 4..][..4].copy_from_slice(&buf[src..][..4]);
1430 }
1431 set_palette_pred(window, fctx);
1432 }
1433 let tu_grid = read_plane(
1434 dec,
1435 cdfs,
1436 if intrabc_dv.is_some() {
1437 inter_txbset_for(4, reduced_tx_set)
1438 } else if reduced_tx_set {
1439 TxbSet::Luma4
1440 } else {
1441 TxbSet::Luma4Set1
1442 },
1443 scans.1,
1444 0,
1445 tu_around,
1446 mode,
1447 mode,
1448 angle_delta_y,
1449 tu_reach,
1450 y,
1451 u,
1452 v,
1453 tu_px,
1454 tu_py,
1455 4,
1456 4,
1457 base_q_idx,
1458 None,
1459 filter_intra,
1460 Some(tu_skip_ctx),
1461 smooth_neighbor,
1462 fctx,
1463 )?;
1464 if crate::envflags::env_flag!("EC_AV1_TRACE") {
1465 eprintln!(
1466 "TRACE tu_bitpos mi=({},{}) tell={}",
1467 tu_mi.0,
1468 tu_mi.1,
1469 dec.debug_bitpos()
1470 );
1471 }
1472 neighbours.record_mi_luma(tu_mi, 4, &tu_grid);
1473 // The FIRST (top-left, co-located with this group's chroma)
1474 // TU's coded tx_type is what the group's chroma inherits --
1475 // the same capture [`decode_block`]'s multi-TU branch makes
1476 // for `first_leaf_tx` (libaom `av1_get_tx_type`, blockd.h:
1477 // an inter-classified block's `PLANE_TYPE_UV` reads
1478 // `tx_type_map`, and intrabc is inter-classified).
1479 if first_leaf_tx.is_none() {
1480 first_leaf_tx = Some(fctx.luma_tx_type.with(std::cell::Cell::get));
1481 }
1482 }
1483 }
1484 // lane-av1intrapred: an intrabc leaf whose var-tx tree split to
1485 // TX_4X4 never armed [`INTRABC_CHROMA_TX`], so its chroma reads fell
1486 // back to `default_intra_tx_type(DC_PRED)` = `DCT_DCT` and took the
1487 // 2D scan, the 2D eob row and the 2D nz contexts where libaom takes
1488 // the inherited luma type's (R5's first fork: mi (36,66), luma
1489 // (0,0) coded `H_ADST` off the 16-symbol inter set, chroma U then
1490 // decoded its `eob` from the wrong CDF row). Same arm the
1491 // `resolved != 4` branch above runs for its whole-leaf unit.
1492 if intrabc_dv.is_some()
1493 && let Some(t) = first_leaf_tx
1494 {
1495 fctx.intrabc_chroma_tx.with(|c| c.set(Some(t)));
1496 }
1497 let ac = alpha.map(|_| cfl_src(px, py, 8));
1498 // lane-av1-422h: same ss_x-1 dc-vote sampling as the whole-leaf arm
1499 // above -- the doubled above sum crosses zero at mi(2,24) plane 2.
1500 let chroma_around = if chroma_422 {
1501 neighbours.around_mi_422_chroma(leaf_mi, 8, 8)
1502 } else {
1503 neighbours.around_mi(leaf_mi, 8)
1504 };
1505 // lane-av1intrapred: the non-vacuity counter -- armed slot consulted
1506 // by this arm's chroma reads means the frame-copy + inherited-type
1507 // route ran; zero at gate time means the fixture stopped reaching it.
1508 if fctx.intrabc_chroma_tx.with(std::cell::Cell::get).is_some() {
1509 hit!(INTRABC_TX4_CHROMA_COPY_HITS);
1510 }
1511 if chroma_422 && !lossless(fctx) {
1512 // lane-av1-422d: the chroma unit does NOT follow the luma var-tx
1513 // split -- `uv_txsize` is `av1_get_max_uv_txsize` of the block,
1514 // still the one 4x8 rect unit per plane at ss (1,0).
1515 u_grid = leaf8_chroma422_unit(
1516 dec,
1517 cdfs,
1518 chroma_around[1],
1519 uv_predict_mode,
1520 angle_delta_uv,
1521 reach,
1522 1,
1523 alpha.zip(ac).map(|((au, _), ac)| (au, ac)),
1524 false,
1525 intrabc_bufs
1526 .as_ref()
1527 .map(|(_, ub, _)| ub.clone())
1528 .or_else(|| palette_uv_bufs.as_ref().map(|(ub, _)| ub.clone())),
1529 y,
1530 u,
1531 v,
1532 cpx,
1533 cpy,
1534 smooth_neighbor_uv,
1535 fctx,
1536 )?;
1537 v_grid = leaf8_chroma422_unit(
1538 dec,
1539 cdfs,
1540 chroma_around[2],
1541 uv_predict_mode,
1542 angle_delta_uv,
1543 reach,
1544 2,
1545 alpha.zip(ac).map(|((_, av), ac)| (av, ac)),
1546 false,
1547 intrabc_bufs
1548 .as_ref()
1549 .map(|(_, _, vb)| vb.clone())
1550 .or_else(|| palette_uv_bufs.as_ref().map(|(_, vb)| vb.clone())),
1551 y,
1552 u,
1553 v,
1554 cpx,
1555 cpy,
1556 smooth_neighbor_uv,
1557 fctx,
1558 )?;
1559 } else if (chroma_444 || chroma_422) && lossless(fctx) {
1560 // lane-av1422llintra: a LOSSLESS 4:2:2 leaf is the 4:2:2 twin of
1561 // the 4:4:4 arm below. libaom's `av1_get_tx_size` (`blockd.h:1381`)
1562 // returns TX_4X4 for EVERY plane of a lossless block, and its
1563 // FIRST line is the lossless test -- so `av1_get_max_uv_txsize`
1564 // (the `Tx_4X8` the `chroma_422` arm above reads) is never reached
1565 // in a lossless frame. At ss (1,0) the leaf's chroma PLANE BLOCK is
1566 // 4x8 (`av1_ss_size_lookup[BLOCK_8X8][1][0]`), so it is a
1567 // `(4, 8)` region walked as two 4x4 units per plane, PLANE-MAJOR
1568 // -- the same walk `read_intra_chroma_lossless` runs for whole
1569 // blocks, at this leaf's own 4x8 plane block.
1570 //
1571 // MEASURED: the pinned 4:2:2 all-intra LOSSLESS control
1572 // `ll422_allintra.obu` (43 255 B, sha256 below) against the
1573 // instrumented `aomdec`: its block at mi(0,2) is `EC_IMODE_VAL
1574 // mi_row=0 mi_col=2 bsize=3` (BLOCK_8X8) and the oracle's
1575 // `EC_COEFF` units are `plane=1 row=0`, `plane=1 row=1`,
1576 // `plane=2 row=0`, `plane=2 row=1` -- four 4x4 units, two per
1577 // plane. This arm read ONE `TxbSet::ChromaRect8x4` 4x8 unit per
1578 // plane instead (our `EC_PRED` trace:
1579 // `x=4 y=0 plane=1 bw=4 bh=8 mode=1`, the first line that
1580 // disagrees with the oracle's `EC_PREDOUT8
1581 // plane=1 row_off=0 txw=4 txh=4` + `row_off=1 txh=4` pair), so
1582 // the tile desynced at that block's second chroma symbol and
1583 // every later pixel decoded from the wrong place.
1584 //
1585 // CfL never rides here: `is_cfl_allowed` (`cfl.h:24`) allows it in
1586 // a lossless block only when the chroma PLANE BLOCK is
1587 // BLOCK_4X4, and a 4:2:2 8x8 leaf's is BLOCK_4X8 -- so
1588 // `read_intra_chroma_lossless`'s no-CfL `read_plane` is the
1589 // correct arm for this shape too.
1590 let (blk_w, blk_h) = if chroma_422 {
1591 (4usize, 8usize)
1592 } else {
1593 (8usize, 8usize)
1594 };
1595 let (mut uo, mut vo) = (Vec::new(), Vec::new());
1596 read_intra_chroma_lossless(
1597 y,
1598 u,
1599 v,
1600 dec,
1601 cdfs,
1602 neighbours,
1603 leaf_mi,
1604 (px, py),
1605 (cpx, cpy),
1606 8,
1607 (blk_w, blk_h),
1608 (0, 0),
1609 (blk_w, blk_h),
1610 blk_w,
1611 reach,
1612 (y.width, y.height),
1613 mode,
1614 uv_predict_mode,
1615 angle_delta_uv,
1616 palette_uv_bufs.as_ref().map(|(a, b)| (&a[..], &b[..])),
1617 smooth_neighbor_uv,
1618 base_q_idx,
1619 &mut uo,
1620 &mut vo,
1621 fctx,
1622 )?;
1623 u_grid = if uo.is_empty() {
1624 Grid::Zero(blk_w * blk_h)
1625 } else {
1626 Grid::Own(uo)
1627 };
1628 v_grid = if vo.is_empty() {
1629 Grid::Zero(blk_w * blk_h)
1630 } else {
1631 Grid::Own(vo)
1632 };
1633 hit!(CHROMA422_LOSSLESS_4X4_HITS);
1634 } else if chroma_444 && lossless(fctx) {
1635 // 4:4:4 lossless: the leaf's chroma plane block is a 2x2 raster
1636 // of TX_4X4 units per plane, PLANE-MAJOR (`av1_get_tx_size`:
1637 // lossless forces TX_4X4 on EVERY plane) -- the same walk
1638 // `read_intra_chroma_lossless` runs for whole blocks. Reading one
1639 // TX_8X8 unit here panicked before: the lossless inverse is the
1640 // 4x4-only WHT (`TxParams::run`'s own `(w, h) == (4, 4)`
1641 // invariant), and the unit count desyncs the tile either way.
1642 // Per-unit `record_mi_chroma` calls stamp the neighbour bands
1643 // inside, which is why the tail's whole-leaf chroma stamp below
1644 // is skipped for this shape.
1645 let (mut uo, mut vo) = (Vec::new(), Vec::new());
1646 read_intra_chroma_lossless(
1647 y,
1648 u,
1649 v,
1650 dec,
1651 cdfs,
1652 neighbours,
1653 leaf_mi,
1654 (px, py),
1655 (cpx, cpy),
1656 8,
1657 (8, 8),
1658 (0, 0),
1659 (8, 8),
1660 8,
1661 reach,
1662 (y.width, y.height),
1663 mode,
1664 uv_predict_mode,
1665 angle_delta_uv,
1666 palette_uv_bufs.as_ref().map(|(a, b)| (&a[..], &b[..])),
1667 smooth_neighbor_uv,
1668 base_q_idx,
1669 &mut uo,
1670 &mut vo,
1671 fctx,
1672 )?;
1673 u_grid = if uo.is_empty() {
1674 Grid::Zero(64)
1675 } else {
1676 Grid::Own(uo)
1677 };
1678 v_grid = if vo.is_empty() {
1679 Grid::Zero(64)
1680 } else {
1681 Grid::Own(vo)
1682 };
1683 } else {
1684 if let Some((ub, _)) = &palette_uv_bufs {
1685 set_palette_pred(ub.clone(), fctx);
1686 }
1687 u_grid = read_plane(
1688 dec,
1689 cdfs,
1690 cset,
1691 cscan,
1692 1,
1693 chroma_around[1],
1694 mode,
1695 uv_predict_mode,
1696 angle_delta_uv,
1697 reach,
1698 y,
1699 u,
1700 v,
1701 cpx,
1702 cpy,
1703 csz,
1704 csz,
1705 base_q_idx,
1706 alpha.zip(ac).map(|((au, _), ac)| (au, ac)),
1707 None,
1708 None,
1709 smooth_neighbor_uv,
1710 fctx,
1711 )?;
1712 if let Some((_, vb)) = &palette_uv_bufs {
1713 set_palette_pred(vb.clone(), fctx);
1714 }
1715 v_grid = read_plane(
1716 dec,
1717 cdfs,
1718 cset,
1719 cscan,
1720 2,
1721 chroma_around[2],
1722 mode,
1723 uv_predict_mode,
1724 angle_delta_uv,
1725 reach,
1726 y,
1727 u,
1728 v,
1729 cpx,
1730 cpy,
1731 csz,
1732 csz,
1733 base_q_idx,
1734 alpha.zip(ac).map(|((_, av), ac)| (av, ac)),
1735 None,
1736 None,
1737 smooth_neighbor_uv,
1738 fctx,
1739 )?;
1740 }
1741 // The two chroma reads above are the only consulters of this leaf's
1742 // armed [`INTRABC_CHROMA_TX`]; leaving it set would make the NEXT
1743 // leaf's chroma inherit this leaf's luma type instead of its own
1744 // derivation (class armed-slot-leak-into-next-leaf, seen live: the
1745 // regular-intra leaf after an intrabc TX4 leaf took the intrabc
1746 // luma's `V_ADST` for its chroma eob row).
1747 if intrabc_dv.is_some() {
1748 fctx.intrabc_chroma_tx.with(|c| c.set(None));
1749 }
1750 // `record_split_luma` expects a `record`-style `(r, c)` position in
1751 // `SUB`-grid units and rescales it by `SUB / MI`; `leaf_mi` is
1752 // already in mi units, so calling it here double-scaled the
1753 // position. Plane 0 is already correct per-TU from the
1754 // `record_mi_luma` calls above; this inlines `record_mi`'s tail
1755 // (spec `get_txb_ctx_general`'s neighbour-magnitude bookkeeping) for
1756 // the two chroma planes only, at `leaf_mi` directly, side_mi=2 (this
1757 // leaf's own 8x8 side over `MI`).
1758 let side_mi = 2;
1759 for cell in 0..side_mi {
1760 neighbours.left_side_mi[leaf_mi.0 + cell] = 8;
1761 neighbours.above_side_mi[leaf_mi.1 + cell] = 8;
1762 }
1763 let round_up_even = |n: usize| n.div_ceil(2) * 2;
1764 let bound_h = round_up_even(neighbours.mi_rows);
1765 let bound_w = round_up_even(neighbours.mi_cols);
1766 let u_state = neighbour_state(&u_grid);
1767 let v_state = neighbour_state(&v_grid);
1768 // The 4:4:4 AND 4:2:2 lossless walks above already stamped these
1769 // bands PER UNIT (their raster-order last writer per cell is exactly
1770 // libaom's per-entry last update); this whole-leaf aggregate would
1771 // clobber distinct unit states with one, so it runs only for the
1772 // shapes whose chroma really is one unit.
1773 if !((chroma_444 || chroma_422) && lossless(fctx)) {
1774 for cell in 0..side_mi {
1775 if cell < side_mi.min(bound_h.saturating_sub(leaf_mi.0)) {
1776 neighbours.left[leaf_mi.0 + cell][1] = u_state;
1777 neighbours.left[leaf_mi.0 + cell][2] = v_state;
1778 }
1779 if cell < side_mi.min(bound_w.saturating_sub(leaf_mi.1)) {
1780 neighbours.above[leaf_mi.1 + cell][1] = u_state;
1781 neighbours.above[leaf_mi.1 + cell][2] = v_state;
1782 }
1783 }
1784 }
1785 // This leaf's own palette state into the mi-granular above/left bands
1786 // (sizes 0 when it used none, which is what clears a previous block's
1787 // palette out of the next block's ctx and colour cache).
1788 {
1789 neighbours.record_palette_y_rect(leaf_mi, 8, 8, pal_y_size, pal_y_colors);
1790 neighbours.record_palette_uv_rect(leaf_mi, 8, 8, pal_uv_size, pal_uv_colors);
1791 }
1792 neighbours.fill_skip_grid(leaf_mi, 2, skip);
1793 neighbours.fill_lf_grid(leaf_mi, 2, 4, 0, fctx);
1794 // lane-t900 r32: `set_txfm_ctxs` for an 8x8 leaf -- see `decode_block`'s
1795 // own copy; only an intrabc var-tx read consumes these bands on the intra
1796 // path, so the write is armed by `allow_intrabc`.
1797 if allow_intrabc {
1798 set_txfm_ctxs(neighbours, leaf_mi, 4, 2, 2, skip && intrabc_dv.is_some());
1799 }
1800 neighbours.record_mode_mi(leaf_mi.0, leaf_mi.1, 2, 2, mode);
1801 record_intrabc_mi(leaf_mi.0, leaf_mi.1, 2, intrabc_dv, neighbours, fctx);
1802 return Ok(mode);
1803 }
1804 neighbours.record_mi(leaf_mi, 8, &[&luma_grid[..], &u_grid[..], &v_grid[..]]);
1805 // This leaf's own palette state into the mi-granular above/left bands
1806 // (sizes 0 when it used none, which is what clears a previous block's
1807 // palette out of the next block's ctx and colour cache).
1808 {
1809 neighbours.record_palette_y_rect(leaf_mi, 8, 8, pal_y_size, pal_y_colors);
1810 neighbours.record_palette_uv_rect(leaf_mi, 8, 8, pal_uv_size, pal_uv_colors);
1811 }
1812 hit!(INTER8_SKIP_BAND_HITS);
1813 neighbours.fill_skip_grid(leaf_mi, 2, skip);
1814 // lane-kf900 r1: the leaf's own RESOLVED transform size, never the literal
1815 // block side. libaom calls `set_txfm_ctxs(mbmi->tx_size, ..., skip_txfm &&
1816 // is_inter_block(mbmi), xd)` (decodemv.c) -- for an INTRA block that skip
1817 // term is always 0, so a skipped 8x8 leaf whose `tx_depth` resolved to
1818 // TX_4X4 still publishes 4 to the next block's `get_tx_size_context`
1819 // (pred_common.h:342). Writing 8 here made the 8x16 strip below the leaf
1820 // read `tx_size_cat1` row 2 where libaom reads row 1 -- same alphabet,
1821 // wrong CDF row, so the msac range diverged mid-frame with no refusal
1822 // (class `wrong-alphabet-same-value`; the ReelB key frame at
1823 // -ss 900, first bad luma sample row 186 col 1803).
1824 neighbours.fill_lf_grid(leaf_mi, 2, resolved as u8, 0, fctx);
1825 // lane-t900 r32 / lane-av1txbands: `set_txfm_ctxs` for an 8x8 leaf -- see
1826 // `decode_block`'s own copy for why this stays beside
1827 // [`Neighbours::fill_lf_grid_rect`]'s publish (the skipped-intrabc
1828 // BLOCK-size term).
1829 if allow_intrabc {
1830 set_txfm_ctxs(
1831 neighbours,
1832 leaf_mi,
1833 resolved,
1834 2,
1835 2,
1836 skip && intrabc_dv.is_some(),
1837 );
1838 }
1839 neighbours.record_mode_mi(leaf_mi.0, leaf_mi.1, 2, 2, mode);
1840 // The DV predictor of a LATER intrabc block is the ordinary `INTRA_FRAME`
1841 // MV stack over this grid (`av1_find_mv_refs`), so an 8x8 leaf has to
1842 // publish its own cells exactly like `decode_block` does.
1843 record_intrabc_mi(leaf_mi.0, leaf_mi.1, 2, intrabc_dv, neighbours, fctx);
1844 Ok(mode)
1845}
1846
1847/// lane-av1-422d: ONE 4:2:2 chroma transform unit of an 8x8 leaf
1848/// ([`decode_leaf8`]). At ss (1,0) the plane block is 4x8 and
1849/// `av1_get_max_uv_txsize(BLOCK_8X8)` resolves to
1850/// `max_txsize_rect_lookup[BLOCK_4X8]` == TX_4X8, so the WHOLE plane block is
1851/// a single rect unit -- one txb_skip + one scan per plane in the
1852/// [`TxbSet::ChromaRect8x4`] class [`decode_rect_split`] proves for the
1853/// shape. Mirrors [`sub8_leaf_chroma444`]'s rect arm at the leaf's halved
1854/// position: the entropy context is the leaf's own 8x8 luma-span
1855/// above/left state (the `chroma_tiled` arm's convention; no `+3`
1856/// plane-block-bigger term -- the unit covers the plane block exactly), the
1857/// CFL source is the leaf's whole 8x8 luma block, and the plane's predicted
1858/// mode supplies the mode-derived default `tx_type` unless an armed
1859/// [`INTRABC_CHROMA_TX`] slot carries the intrabc luma's own (the caller
1860/// arms and disarms it, exactly as it does around its square reads).
1861#[allow(clippy::too_many_arguments)]
1862pub(super) fn leaf8_chroma422_unit(
1863 dec: &mut SymbolDecoder,
1864 cdfs: &mut Cdfs,
1865 around: (bool, bool, i32),
1866 uv_predict_mode: usize,
1867 angle_delta_uv: i32,
1868 reach: Reach,
1869 plane_idx: usize,
1870 cfl: Option<(i32, CflSrc)>,
1871 skip: bool,
1872 // The prediction override the square arms feed through
1873 // `set_palette_pred`: an intrabc leaf's frame copy at the DV takes
1874 // priority over a palette block's colour-index map.
1875 pred_override: Option<Vec<u16>>,
1876 // Unread, like [`read_plane`]'s own: the borrow proves the caller holds
1877 // the plane exclusively while the unit's coefficients are parsed.
1878 y: &mut PlaneBuf<'static>,
1879 u: &mut PlaneBuf<'static>,
1880 v: &mut PlaneBuf<'static>,
1881 cpx: usize,
1882 cpy: usize,
1883 smooth_neighbor_uv: bool,
1884 fctx: &crate::decode::FrameCtx,
1885) -> Result<Grid> {
1886 // The unit's prediction runs whether or not a residual follows it -- a
1887 // skipped leaf still predicts (the CfL contribution survives a skip),
1888 // and a skipped intrabc leaf still copies the frame at its DV.
1889 if let Some(buf) = pred_override {
1890 set_palette_pred(buf, fctx);
1891 }
1892 if skip {
1893 push_intra_rect(
1894 y,
1895 u,
1896 v,
1897 plane_idx,
1898 cpx,
1899 cpy,
1900 4,
1901 8,
1902 uv_predict_mode,
1903 angle_delta_uv,
1904 reach,
1905 &ZERO_RESIDUAL[..4 * 8],
1906 cfl,
1907 None,
1908 smooth_neighbor_uv,
1909 fctx,
1910 );
1911 return Ok(Grid::Zero(4 * 8));
1912 }
1913 let default_tx = fctx
1914 .intrabc_chroma_tx
1915 .with(std::cell::Cell::get)
1916 .unwrap_or_else(|| default_intra_tx_type(uv_predict_mode as u8));
1917 let mut coding = cdfs.txb(TxbSet::ChromaRect8x4, uv_predict_mode);
1918 let (levels, tx_type) = read_coeffs_rect(
1919 dec,
1920 &mut coding,
1921 &SCAN_4X8,
1922 4,
1923 8,
1924 usize::from(around.0) + usize::from(around.1),
1925 dc_sign_ctx(around.2),
1926 default_tx,
1927 plane_idx,
1928 )?;
1929 let residual = dequant_and_inverse_typed_wh(
1930 &levels,
1931 4,
1932 8,
1933 crate::decode::bit_depth(fctx),
1934 block_q_idx(fctx),
1935 plane_q_delta(plane_idx, fctx).0,
1936 plane_q_delta(plane_idx, fctx).1,
1937 tx_type,
1938 block_iqmatrix(fctx, plane_idx, 4, 8, tx_type),
1939 );
1940 push_intra_rect(
1941 y,
1942 u,
1943 v,
1944 plane_idx,
1945 cpx,
1946 cpy,
1947 4,
1948 8,
1949 uv_predict_mode,
1950 angle_delta_uv,
1951 reach,
1952 &residual,
1953 cfl,
1954 None,
1955 smooth_neighbor_uv,
1956 fctx,
1957 );
1958 Ok(levels)
1959}
1960
1961/// `read_intra_frame_mode_info`'s mode-info read for one `BLOCK_4X4` leaf
1962/// below an 8x8 partition (spec `decode_partition`'s `bSize < BLOCK_8X8`
1963/// bottom, where no partition symbol is read at all -- `decode_block` runs
1964/// directly). No palette (`av1_allow_palette` needs both dims >= 8, spec
1965/// blockd.h) and no `read_tx_size` (a sub-8x8 block's transform is always
1966/// its own bsize, spec 5.11.16). `has_chroma` follows `is_chroma_reference`
1967/// (spec/libaom `av1_common_int.h`): for 4:2:0, only the bottom-right leaf
1968/// of the 2x2 4x4 group carries chroma syntax at all.
1969#[allow(clippy::too_many_arguments)]
1970pub(super) fn read_intra_mode_sub8(
1971 dec: &mut SymbolDecoder,
1972 cdfs: &mut Cdfs,
1973 above_mode: usize,
1974 left_mode: usize,
1975 has_chroma: bool,
1976 enable_filter_intra: bool,
1977 // [`filter_intra_size_class_rect`] for the LEAF's own shape: 0 for a
1978 // `BLOCK_4X4` split leaf, 8/9 for a 4x8/8x4 rect leaf. Reading the flag
1979 // from the wrong row is the same VALUE with a different interval, so it
1980 // desyncs a block or two later, not here (class `wrong-alphabet-same-value`).
1981 fi_class: usize,
1982 skip_ctx: usize,
1983 allow_intrabc: bool,
1984 mi_r: usize,
1985 mi_c: usize,
1986 // The leaf's own mi footprint for `set_segment_id`: 1x1 for a `BLOCK_4X4`
1987 // split leaf, 2x1/1x2 for an 8x4/4x8 one. Stamping 1x1 for a rect leaf
1988 // left the second mi column at segment 0, which reads back as a
1989 // neighbour's `cdf_index`/prediction two blocks later.
1990 seg_w_mi: usize,
1991 seg_h_mi: usize,
1992 fctx: &crate::decode::FrameCtx,
1993) -> Result<(
1994 bool,
1995 usize,
1996 i32,
1997 Option<(usize, i32, Option<(i32, i32)>)>,
1998 Option<usize>,
1999 Option<(i32, i32)>,
2000)> {
2001 // lane-av1mono: a monochrome leaf codes no chroma mode (libaom
2002 // `decodemv.c:933`), so fold `NumPlanes == 1` into `has_chroma`.
2003 let has_chroma = has_chroma && !mono(fctx);
2004 let trace = crate::envflags::env_flag!("EC_AV1_TRACE");
2005 let ec_istep = crate::envflags::env_flag!("EC_TRACE_MODE_STEP");
2006 crate::msac::SymbolDecoder::set_symr_mi(mi_r as i64, mi_c as i64);
2007 if ec_istep {
2008 eprintln!(
2009 "EC_IMODE mi_row={mi_r} mi_col={mi_c} fn=sub8 rng={}",
2010 dec.debug_state().0
2011 );
2012 }
2013 // Same `EC_ISTEP` ladder the square/rect readers print, so a cross-decoder
2014 // range diff lands on the ELEMENT inside a 4x4 leaf, not just the block.
2015 macro_rules! istep {
2016 ($name:literal, $val:expr) => {
2017 if ec_istep {
2018 eprintln!(
2019 "EC_ISTEP mi_row={mi_r} mi_col={mi_c} name={} val={} rng={}",
2020 $name,
2021 $val,
2022 dec.debug_state().0
2023 );
2024 }
2025 };
2026 }
2027 // lane-seg2: same `intra_segment_id` placement as the square and rect
2028 // readers (spec 5.11.6) -- a `BLOCK_4X4` leaf codes its own `segment_id`
2029 // like any other block, over its own 1x1 mi footprint. Dropping it here
2030 // both ate a symbol (the tile desynced at the frame's first sub-8x8
2031 // split) and left `cur_segment_id` on the previous block, so the leaf
2032 // dequantized with a neighbour's `SEG_LVL_ALT_Q`.
2033 if seg_id_pre_skip(fctx) {
2034 intra_segment_id(dec, cdfs, mi_r, mi_c, seg_w_mi, seg_h_mi, false, fctx);
2035 }
2036 let skip = dec.symbol(&mut cdfs.skip[skip_ctx]) != 0;
2037 if trace {
2038 eprintln!(
2039 "TRACE sub8 skip mi=({mi_r},{mi_c}) ctx={skip_ctx} value={} rng={}",
2040 skip as i32,
2041 dec.debug_state().0
2042 );
2043 }
2044 istep!("skip", skip as i32);
2045 if !seg_id_pre_skip(fctx) {
2046 intra_segment_id(dec, cdfs, mi_r, mi_c, seg_w_mi, seg_h_mi, skip, fctx);
2047 }
2048 maybe_read_cdef_idx(dec, mi_r, mi_c, (2, 2), skip, fctx);
2049 istep!("cdef", 0);
2050 maybe_read_delta_q(dec, cdfs, mi_r, mi_c, false, skip, fctx);
2051 maybe_read_delta_lf(dec, cdfs, mi_r, mi_c, false, skip, fctx);
2052 istep!("dq", 0);
2053 if allow_intrabc {
2054 let use_intrabc = dec.symbol(&mut cdfs.intrabc) != 0;
2055 if ec_istep {
2056 eprintln!(
2057 "EC_ISTEP mi_row={mi_r} mi_col={mi_c} name=intrabc val={} rng={}",
2058 u8::from(use_intrabc),
2059 dec.debug_state().0
2060 );
2061 }
2062 if use_intrabc {
2063 // lane-av1txr: `read_intrabc_info` (spec 5.11.13) reads the block
2064 // vector right after the flag and RETURNS -- no y/uv mode, angle
2065 // delta, palette, CfL or filter-intra syntax follows, and the
2066 // block is reconstructed by the INTER path (libaom's
2067 // `is_inter_block` counts an intrabc block), so the caller takes
2068 // the frame-copy prediction + inter residual.
2069 let dv = read_intrabc_dv(dec, cdfs, mi_r, mi_c, (seg_w_mi, seg_h_mi), fctx);
2070 // lane-av1txr: the DV travels back in the RETURN value -- it is
2071 // NOT published through `FrameCtx::intrabc_dv`. That slot is the
2072 // SQUARE paths' handoff (`decode_block`/`decode_leaf8` arm it,
2073 // then `take()` it in the same call), so a sub-8x8 leaf that
2074 // armed it left the vector standing for the NEXT >=8x8 block:
2075 // `warped.obu`'s (38,60) 8x8 intra block took it as its own
2076 // intrabc and read an inter var-tx `txfm_split` where libaom read
2077 // an intra `tx_depth` (class `stale-slot-handoff`).
2078 hit!(INTRABC_HITS);
2079 return Ok((skip, DC_PRED, 0, None, None, Some(dv)));
2080 }
2081 }
2082 let above_ctx = INTRA_MODE_CTX[above_mode];
2083 let left_ctx = INTRA_MODE_CTX[left_mode];
2084 let mode = dec.symbol(&mut cdfs.kf_y_mode[above_ctx][left_ctx]);
2085 istep!("mode", mode);
2086 if trace {
2087 eprintln!(
2088 "TRACE sub8 y_mode mi=({mi_r},{mi_c}) ctx=({above_ctx},{left_ctx}) value={mode} rng={}",
2089 dec.debug_state().0
2090 );
2091 }
2092 // No `angle_delta` at all below 8x8: spec 5.11.6's `intra_angle_info_y`/
2093 // `_uv` are gated on `MiSize >= BLOCK_8X8` (libaom `av1_use_angle_delta`,
2094 // blockd.h), so a directional mode on a `BLOCK_4X4` leaf carries no delta
2095 // symbol. Reading one consumed a symbol the encoder never wrote and
2096 // desynced the tile at the very next element.
2097 let angle_delta_y = 0;
2098 let chroma = if has_chroma {
2099 // `is_cfl_allowed` (blockd.h): at lossless CfL needs the CHROMA plane
2100 // block to be a single TX_4X4 (`cfl_allowed_px`). True for this leaf
2101 // at 4:2:0 (the group's plane block is 4x4) and for a 4:4:4
2102 // `BLOCK_4X4` split leaf, but FALSE for a 4:4:4 `BLOCK_8X4`/`BLOCK_4X8`
2103 // rect leaf, whose plane block is the rect itself -- that shape reads
2104 // the 13-symbol `uv_mode_no_cfl` CDF, and reading the CFL alphabet
2105 // there desynced the tile at the rect leaf's own uv_mode symbol (measured:
2106 // ours consumed the 14-symbol interval where the oracle read 13, first
2107 // divergence at the rect leaf mi (8,16) of `ll444.obu`; class
2108 // `wrong-alphabet-same-value`, lane-av1-llsub8).
2109 let uv_mode = if cfl_allowed_px(seg_w_mi * MI, seg_h_mi * MI, fctx) {
2110 crate::msac::SymbolDecoder::set_symr_cdf("uv_mode");
2111 dec.symbol(&mut cdfs.uv_mode_cfl[mode])
2112 } else {
2113 crate::msac::SymbolDecoder::set_symr_cdf("uv_mode");
2114 dec.symbol(&mut cdfs.uv_mode_no_cfl[mode])
2115 };
2116 if trace {
2117 eprintln!(
2118 "TRACE sub8 uv_mode value={uv_mode} rng={}",
2119 dec.debug_state().0
2120 );
2121 }
2122 if (9..=12).contains(&uv_mode) {
2123 hit!(SMOOTH_UV_HITS);
2124 }
2125 let alpha = if uv_mode == UV_CFL_PRED {
2126 Some(read_cfl_alphas(dec, cdfs))
2127 } else {
2128 None
2129 };
2130 if (V_PRED..=D67_PRED).contains(&uv_mode) {
2131 hit!(DIRECTIONAL_UV_HITS);
2132 }
2133 // See the luma comment above: no angle delta below 8x8.
2134 let angle_delta_uv = 0;
2135 Some((uv_mode, angle_delta_uv, alpha))
2136 } else {
2137 None
2138 };
2139 let mut filter_intra = None;
2140 if mode == DC_PRED && enable_filter_intra {
2141 let use_filter_intra = dec.symbol(&mut cdfs.filter_intra[fi_class]) != 0;
2142 if trace {
2143 eprintln!(
2144 "TRACE sub8 use_filter_intra value={}",
2145 use_filter_intra as i32
2146 );
2147 }
2148 if use_filter_intra {
2149 hit!(FILTER_INTRA_HITS);
2150 let fi_mode = dec.symbol(&mut cdfs.filter_intra_mode);
2151 filter_intra = Some(fi_mode);
2152 }
2153 }
2154 Ok((skip, mode, angle_delta_y, chroma, filter_intra, None))
2155}