forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

inter_intra_arm.rs1409 lines, 60.3 KiBHistoryRawDownload
1//! Intra arm of `decode_inter_block` (the `else` of its `is_inter` test), moved out (av1split D8a).
2//! The body is unchanged apart from the signature, which carries the locals it reads and
3//! returns the ones the caller reads after the branch. `Ok(None)` is the arm's early return.
4
5use super::*;
6
7/// The outputs the intra arm hands back to `decode_inter_block`, in the order it destructures them.
8type IntraArmOut = (
9 usize,
10 usize,
11 [u8; 2],
12 i8,
13 bool,
14 Grid,
15 Grid,
16 Grid,
17 Option<Vec<(usize, usize, usize, usize)>>,
18 (usize, [u16; 8], usize, [u16; 8]),
19 bool,
20);
21
22// the arm takes what decode_inter_block's inline branch read; its outputs are declared late, as they were inline
23#[allow(clippy::too_many_arguments, clippy::needless_late_init)]
24pub(super) fn decode_inter_block_intra_arm(
25 dec: &mut SymbolDecoder,
26 cdfs: &mut Cdfs,
27 neighbours: &mut Neighbours,
28 grid: &mut MiGrid,
29 at: (usize, usize),
30 side: usize,
31 mi_cols: u32,
32 mi_rows: u32,
33 y: &mut PlaneBuf<'static>,
34 u: &mut PlaneBuf<'static>,
35 v: &mut PlaneBuf<'static>,
36 base_q_idx: u8,
37 luma_set_intra: TxbSet,
38 luma_tx: usize,
39 scan_luma: &[u16],
40 write_w: usize,
41 write_h: usize,
42 allow_screen_content_tools: bool,
43 fctx: &crate::decode::FrameCtx,
44 at_mi: (usize, usize),
45 px: usize,
46 py: usize,
47 r: usize,
48 c: usize,
49 rmi: usize,
50 cmi: usize,
51 cpx: usize,
52 cpy: usize,
53 chroma_side: usize,
54 chroma_stride: usize,
55 chroma_buf_h: usize,
56 chroma_set: TxbSet,
57 chroma_tx: usize,
58 scan_chroma: &[u16],
59 strip_chroma: Option<InterStripChroma>,
60 skip: bool,
61 skip_mode: bool,
62 mut mu_chroma: bool,
63) -> Result<Option<IntraArmOut>> {
64 let mode_for_tx;
65 let uv_predict_mode;
66 let block_filter;
67 let ref_frame_for_lf;
68 let globalmv_for_lf;
69 let vartx_leaves;
70 let (luma_grid, u_grid, v_grid);
71 let palette_record: (usize, [u16; 8], usize, [u16; 8]);
72 // lane-partitions r1: intra prediction (`PlaneBuf::reconstruct`) has
73 // no rectangular counterpart -- an intra-coded HORZ/VERT strip needs
74 // its own true-width/height edge/predict math, unlike the inter
75 // skip path above (which only needed a clipped write of an
76 // already-square-predicted buffer). Named refusal instead of a
77 // silently wrong square-shaped intra prediction.
78 // lane-inter16ab r2: `decode_intra_rect_in_inter` has no chroma-pair
79 // path (it predicts and codes chroma at the strip's own halved
80 // footprint), so an INTRA 16x4 / 4x16 strip inside a 1:4 partition is
81 // refused by name rather than decoded with a 8x2 chroma transform
82 // libaom never coded.
83 // lane-intra16x4 r1: the strip DECODES through the key frame's own
84 // per-strip body, [`decode_rect4_16_strip`].
85 // lane-sub8x4 r3: pixel-proven, so the decode opt-in was lifted. r1
86 // and r2 found no witness because every firing film frame ALSO carried
87 // a sub-8x8 intra block, which refused until r2 lifted it; on this tip
88 // the first 50 frame OBUs of the 10-bit 3840x1608 stream's head decode
89 // all 33 frames pixel-exact while this arm fires 31x 16x4 + 17x 4x16
90 // (23 of them chroma-paired). Pinned as
91 // `fixtures/reelb_rect64_intra16x4_witness.obu` -- MEASURED WRONG, see
92 // the lane-wit16x4 note below.
93 // The refusal below is NARROWED, not deleted: it still stands for a
94 // 16x4/4x16 strip with no chroma-pair record and for every other
95 // sub-8 shape.
96 //
97 // lane-wit16x4 r1: RE-WIDENED to the whole shape, because r3's witness
98 // was a PHANTOM (class `counter-from-refused-stream`): the pinned
99 // fixture was measured while decode-order frame 33 was desynced, and
100 // on the merged tip (lane-frame36 r2 fixed that desync) the very same
101 // fixture decodes all 33 frames pixel-exact with `intra16x4_in_inter`
102 // 16x4=0 4x16=0 chroma_ref=0 -- the 31/17/23 were counted out of a
103 // wrong decode. Re-measured this round: NO stream anywhere decodes
104 // exactly while this arm fires. Two 2 s 10-bit film cuts (3840x1608 at
105 // 0 s: OK, 48 frames exact, 0 hits; at 300 s: 138/559 hits but frames
106 // 1 and 2 mismatch ffmpeg by 14105680 / 12470780 bytes), four 1920x792
107 // 128-SB cuts (hits only inside frames that refuse or are never shown
108 // -- the 6300 s cut's key frame is exact with 0 hits and its no-show
109 // frame 1 carries 169/445 with no display frame reachable before the
110 // 128x64-intra-on-inter refusal), and the aomenc gate
111 // `a_real_aomenc_inter_sequence_with_intra_16x4_strips_in_1to4_partitions_decodes_pixel_exact`
112 // (run `--include-ignored` on this tip: FAILED, 0 pixel-exact attempts
113 // carrying the strip, per-arm [0, 0, 0]). A lifted refusal with no
114 // witness is a capability claim (class `refusal-lifted-without-a-gate`),
115 // so the decode path stayed behind an opt-in for the round that found
116 // a witness.
117 //
118 // lane-t900 r6: THAT WITNESS EXISTS, so the opt-in was lifted (the path decodes by default)
119 // and the refusal is narrowed back to "no chroma-pair record". The
120 // witness is a REAL 10-bit 1920x792 128-superblock film stream, not an
121 // aomenc recipe: `fixtures/reelc_sb128_inter_witness.obu` (177773 bytes,
122 // 23 frame-carrying OBUs, 16 decode-order frames, extended from 14 in
123 // lane-t900 r7) decodes all 16 decode-order frames byte-exact against
124 // instrumented aomdec
125 // (`EC_AV1_FINAL_DUMP`) and all 15 shown frames pixel-exact against
126 // ffmpeg, while this arm fires 78x 16x4 + 149x 4x16 (108 of them
127 // chroma-paired). Unlike lane-sub8x4 r3's phantom (class
128 // `counter-from-refused-stream`) every one of those hits lies inside a
129 // frame that was decoded AND compared exact. Gate
130 // `a_10bit_128sb_film_frames_with_warp_cdef_and_interintra_decode_pixel_exact`.
131 let strip16 = strip_chroma
132 .filter(|_| write_w.min(write_h) == 4 && write_w.max(write_h) == 16)
133 .map(|s| (s.horz, s.has_chroma));
134 if write_w.min(write_h) < 8 && strip16.is_none() {
135 if crate::envflags::env_flag!("EC_INTRA16X4") {
136 let (idx, hc, prev_inter) = match strip_chroma {
137 Some(s) => (
138 if s.horz {
139 rmi - s.pair_mi.0
140 } else {
141 cmi - s.pair_mi.1
142 },
143 s.has_chroma,
144 s.prev.is_some(),
145 ),
146 None => (9, true, false),
147 };
148 eprintln!(
149 "EC_INTRA16X4 w={write_w} h={write_h} pair_parity={idx} has_chroma={hc} prev_single_inter={prev_inter} skip={skip} mi={rmi},{cmi} tx_select={} scr={}",
150 fctx.tx_select_inter.with(std::cell::Cell::get),
151 allow_screen_content_tools,
152 );
153 }
154 return Err(unsupported(
155 "an intra 16x4/4x16 strip inside an inter 16x16-level 1:4 partition (its 4:2:0 chroma pair is coded once for two strips; only the inter path implements that pairing)",
156 ));
157 }
158 if write_w != side || write_h != side {
159 // lane-intrarect r1: the strip decodes through its own rect
160 // machinery ([`decode_intra_rect_in_inter`] -> the key-frame
161 // paths' [`decode_rect_split`]) instead of refusing. That call
162 // already writes everything the shared tail below would for a
163 // square block EXCEPT the inter-side bands and the mi map, so
164 // this arm returns rather than falling through (a second
165 // `record_rect`/`fill_lf_grid_rect` at block granularity would
166 // overwrite the per-transform-unit records it just made).
167 decode_intra_rect_in_inter(
168 dec,
169 cdfs,
170 neighbours,
171 at,
172 write_w,
173 write_h,
174 skip,
175 allow_screen_content_tools,
176 strip16,
177 y,
178 u,
179 v,
180 base_q_idx,
181 fctx,
182 )?;
183 // merge b51719f+lane-intrarect: `at` is in MI units on this path
184 // (lane-inter4), so the strip's mi origin is `at` itself -- the
185 // lane's `r * SUB_MI` re-derivation would snap a sub-SUB strip to
186 // its enclosing 16-px cell.
187 let (mi_r0, mi_c0) = (rmi, cmi);
188 grid.fill_rect(
189 mi_r0,
190 mi_c0,
191 write_h / MI,
192 write_w / MI,
193 MiInfo {
194 is_inter: false,
195 ref_frame: -1,
196 ref_frame1: NO_REF1,
197 mv1: (0, 0),
198 mv: (0, 0),
199 is_new_mv: false,
200 size: (write_w / MI) as u8,
201 size_h: (write_h / MI) as u8,
202 is_global_mv0: false,
203 is_global_mv1: false,
204 },
205 );
206 neighbours.record_inter_rect_mi(
207 at,
208 write_w / MI,
209 write_h / MI,
210 skip,
211 false,
212 0,
213 [3, 3],
214 skip_mode,
215 );
216 return Ok(None);
217 }
218 crate::msac::SymbolDecoder::set_symr_cdf("y_mode");
219 let mode = dec.symbol(&mut cdfs.y_mode[size_group_wh(write_w, write_h)]);
220 if crate::envflags::env_flag!("EC_IIS") {
221 eprintln!(
222 "TRACE iis px={px} py={py} side={side} w={write_w} h={write_h} sg={} mode={mode} skip={skip}",
223 size_group_wh(write_w, write_h)
224 );
225 }
226 if mode >= 13 {
227 return Err(unsupported(
228 "an intra mode this decoder does not code (round 2)",
229 ));
230 }
231 // lane-angleinter r1: `read_intra_angle_info` (libaom `decodemv.c`,
232 // inside `read_intra_block_mode_info`) -- an intra block in an inter
233 // frame reads `angle_delta_y` off the same
234 // `angle_delta_cdf[mode - V_PRED]` row a key-frame block does, and
235 // the delta feeds the directional predictor (and the edge
236 // filter/upsample decision derived from the delta'd angle) unchanged.
237 // The symbol was read and then refused before this round even though
238 // aomenc enables `--enable-angle-delta` by default.
239 let angle_delta_y = if (V_PRED..=D67_PRED).contains(&mode) {
240 read_angle_delta(dec, &mut cdfs.angle_delta[mode - V_PRED])
241 } else {
242 0
243 };
244 if angle_delta_y != 0 {
245 hit!(INTRA_IN_INTER_ANGLE_DELTA_Y_HITS);
246 }
247 // The delta'd angle decides whether the intra edge filter runs at
248 // all, so this block needs the same luma edge-filter-type neighbour
249 // check a key-frame block does (`get_intra_edge_filter_type`, spec
250 // 7.11.2) -- this arm passed a hardcoded `false` while every
251 // directional intra-in-inter block was refused above.
252 let (nb_above_mode, nb_left_mode) = neighbours.modes_above_left(r, c);
253 let smooth_neighbor = is_smooth_mode(nb_above_mode) || is_smooth_mode(nb_left_mode);
254 // lane-sbrect10 r1 (class wrong-alphabet-same-value, second instance
255 // after lane-sbpart r3's rect strips): `is_cfl_allowed` (libaom
256 // `blockd.h` -- `block_size_wide[bsize] <= 32 && block_size_high[bsize]
257 // <= 32`, spec 5.11.5) caps CFL at 32x32, so a 64x64 intra block inside
258 // an INTER frame reads the 13-symbol `uv_mode_no_cfl` alphabet. Reading
259 // the 14-symbol CFL one gave the same DC_PRED VALUE but narrowed the
260 // arithmetic coder by the wrong interval, desyncing the tile from this
261 // block on.
262 // lane-av1mono: a monochrome block codes no chroma mode/CFL symbol
263 // (libaom `decodemv.c:933`).
264 let has_chroma = !mono(fctx);
265 let cfl_allowed = has_chroma && cfl_allowed_px(write_w, write_h, fctx);
266 if !cfl_allowed {
267 hit!(NOCFL_UV_MODE_HITS);
268 }
269 let uv_mode = if !has_chroma {
270 DC_PRED
271 } else if cfl_allowed {
272 crate::msac::SymbolDecoder::set_symr_cdf("uv_mode");
273 dec.symbol(&mut cdfs.uv_mode_cfl[mode])
274 } else {
275 crate::msac::SymbolDecoder::set_symr_cdf("uv_mode");
276 dec.symbol(&mut cdfs.uv_mode_no_cfl[mode])
277 };
278 if (9..=12).contains(&uv_mode) {
279 hit!(SMOOTH_UV_HITS);
280 }
281 let alpha = if cfl_allowed && uv_mode == UV_CFL_PRED {
282 Some(read_cfl_alphas(dec, cdfs))
283 } else {
284 None
285 };
286 // `get_uv_mode` (spec 9.3): `UV_CFL_PRED` predicts as `DC_PRED` for
287 // the angle-delta question, same as [`read_intra_mode`]'s own copy
288 // -- `uv_mode` (13) already falls outside `V_PRED..=D67_PRED` so the
289 // `else` branch below is exact for it either way.
290 let angle_delta_uv = if (V_PRED..=D67_PRED).contains(&uv_mode) {
291 hit!(DIRECTIONAL_UV_HITS);
292 read_angle_delta(dec, &mut cdfs.angle_delta[uv_mode - V_PRED])
293 } else {
294 0
295 };
296 if angle_delta_uv != 0 {
297 hit!(UV_ANGLE_DELTA_HITS);
298 hit!(INTRA_IN_INTER_ANGLE_DELTA_UV_HITS);
299 }
300 uv_predict_mode = if uv_mode == UV_CFL_PRED {
301 DC_PRED
302 } else {
303 uv_mode
304 };
305 // lane-chroma r4: same chroma edge-filter-strength neighbour check
306 // as [`read_intra_mode`]/[`read_intra_mode_rect`] -- the CHROMA
307 // neighbour's own `uv_mode`, not the luma one.
308 let smooth_neighbor_uv =
309 neighbours.smooth_uv_neighbour(r * (SUB / MI), c * (SUB / MI), r, c, fctx);
310 // `read_palette_mode_info` (decodemv.c:567, called from
311 // `read_intra_block_mode_info` right after `xd->cfl.store_y`, same
312 // gating and same corner-cut as `read_intra_mode`'s own copy above
313 // -- `palette_mode_ctx`/`palette_uv_mode_ctx` hardcoded 0, provably
314 // safe since a nonzero neighbour `palette_size` would already have
315 // refused the decode that produced it.
316 // lane-inter16ab r4 (class sweep): `av1_get_palette_bsize_ctx` is keyed
317 // by the block's TRUE footprint -- a rect strip's row is not its
318 // enclosing square's, and a 16x4 strip (64 pixels) reads no palette
319 // symbol at all.
320 // lane-t900 r22: reconstructed, not refused. Same syntax and same
321 // deferral of the colour-index maps past `filter_intra`
322 // (`av1_visit_palette` runs from `parse_decode_block`, after the whole
323 // mode-info read) as the key frame's [`read_intra_mode`]; the mode
324 // context and colour cache are this block's real above/left ones, at
325 // its own mi position (`av1_get_palette_mode_ctx`/
326 // `av1_get_palette_cache`), not the hardcoded 0/empty pair the refusal
327 // could get away with while no neighbour could ever be a palette.
328 let (palette_ctx, palette_cache) = neighbours.palette_ctx_and_cache_mi((rmi, cmi));
329 let palette_uv_cache = neighbours.palette_uv_cache_mi((rmi, cmi));
330 let mut palette_y_pending: Option<(usize, [u16; 8])> = None;
331 let mut palette_uv_pending: Option<(usize, [u16; 8], [u16; 8])> = None;
332 if allow_screen_content_tools && let Some(bsize_ctx) = palette_bsize_ctx_wh(write_w, write_h) {
333 let use_palette_y =
334 mode == DC_PRED && dec.symbol(&mut cdfs.palette_y_mode[bsize_ctx][palette_ctx]) != 0;
335 if use_palette_y {
336 let n = 2 + dec.symbol(&mut cdfs.palette_y_size[bsize_ctx]);
337 palette_y_pending = Some((n, read_palette_colors_y(dec, n, &palette_cache, fctx)));
338 }
339 if crate::envflags::env_flag!("EC_PALSYN") {
340 eprintln!(
341 "EC_PALSYN_PRE sq mi={rmi},{cmi} bw={write_w} bh={write_h} mode={mode} uv={uv_mode} ctx={palette_ctx} y={} rng={}",
342 palette_y_pending.map_or(0, |(n, _)| n),
343 dec.debug_state().0
344 );
345 }
346 let use_palette_uv = has_chroma
347 && uv_mode == DC_PRED
348 && dec.symbol(&mut cdfs.palette_uv_mode[usize::from(use_palette_y)]) != 0;
349 if crate::envflags::env_flag!("EC_PALSYN") {
350 eprintln!(
351 "EC_PALSYN sq mi={rmi},{cmi} bw={write_w} bh={write_h} mode={mode} uv={uv_mode} ctx={palette_ctx} y={} uv={} rng={}",
352 use_palette_y as i32,
353 use_palette_uv as i32,
354 dec.debug_state().0
355 );
356 }
357 if use_palette_uv {
358 let n = 2 + dec.symbol(&mut cdfs.palette_uv_size[bsize_ctx]);
359 let (u_colors, v_colors) = read_palette_colors_uv(dec, n, &palette_uv_cache, fctx);
360 palette_uv_pending = Some((n, u_colors, v_colors));
361 }
362 }
363 // lane-fiinter r1: `read_filter_intra_mode_info` (libaom `decodemv.c`,
364 // the tail of `read_intra_block_mode_info`) -- an intra block inside
365 // an INTER frame reads `use_filter_intra` in exactly the same place a
366 // key-frame block does, right after the palette syntax. Nothing read
367 // it here before this round, and aomenc enables filter intra by
368 // default, so every inter frame carrying one desynced at this symbol.
369 // `av1_filter_intra_allowed`'s `palette_size[0] == 0` term is implied:
370 // a real palette-Y block already returned above.
371 let mut filter_intra = None;
372 if mode == DC_PRED
373 && fctx.enable_filter_intra_inter.with(std::cell::Cell::get)
374 // `av1_filter_intra_allowed` (reconintra.h:77) excludes a
375 // palette-Y block, so no `use_filter_intra` symbol exists there.
376 && palette_y_pending.is_none()
377 // lane-inter16ab r4 (class sweep): `cdf::FILTER_INTRA`'s row is the
378 // true `BLOCK_SIZES_ALL` class of this strip, not its square side's.
379 && let Some(class) = filter_intra_size_class_rect(write_w, write_h)
380 && dec.symbol(&mut cdfs.filter_intra[class]) != 0
381 {
382 hit!(FILTER_INTRA_HITS);
383 hit!(INTRA_IN_INTER_FILTER_INTRA_HITS);
384 filter_intra = Some(dec.symbol(&mut cdfs.filter_intra_mode));
385 }
386 // `av1_visit_palette`: the two colour-index maps, Y then the shared
387 // chroma one, after the WHOLE mode-info read.
388 let palette_y = palette_y_pending.map(|(n, colors)| {
389 let map = decode_color_index_map_wh(
390 dec,
391 cdfs,
392 n,
393 write_w,
394 write_h,
395 palette_onscreen(rmi, cmi, write_w, write_h, fctx),
396 false,
397 );
398 hit!(PALETTE_HITS);
399 hit!(INTRA_IN_INTER_PALETTE_Y_HITS);
400 PaletteY {
401 size: n,
402 colors,
403 map,
404 }
405 });
406 let palette_uv = palette_uv_pending.map(|(n, u_colors, v_colors)| {
407 // `av1_get_plane_block_size(bsize, 1, 1)` at the sequence's own
408 // subsampling, floored at the 4-px transform -- the rect reader's
409 // own copy; a hardcoded `/ 2` sized a 4:4:4 chroma map at 4:2:0
410 // and the full-size reconstruct panicked on it (lane-av1-444).
411 let (cw, ch) = (
412 (write_w >> ss_x(fctx)).max(4),
413 (write_h >> ss_y(fctx)).max(4),
414 );
415 let on = palette_onscreen_uv(
416 (write_w, write_h),
417 palette_onscreen(rmi, cmi, write_w, write_h, fctx),
418 (cw, ch),
419 (ss_x(fctx), ss_y(fctx)),
420 );
421 let map = decode_color_index_map_wh(dec, cdfs, n, cw, ch, on, true);
422 hit!(PALETTE_UV_HITS);
423 hit!(INTRA_IN_INTER_PALETTE_UV_HITS);
424 PaletteUv {
425 size: n,
426 u_colors,
427 v_colors,
428 map,
429 }
430 });
431 palette_record = (
432 palette_y.as_ref().map_or(0, |p| p.size),
433 palette_y.as_ref().map_or([0u16; 8], |p| p.colors),
434 palette_uv.as_ref().map_or(0, |p| p.size),
435 palette_uv.as_ref().map_or([0u16; 8], |p| p.u_colors),
436 );
437 // A palette block predicts from its own colour-index map, never from
438 // the block edge (spec 7.11.2/7.11.4) -- these ride the [`PALETTE_PRED`]
439 // slot into each `reconstruct`/`read_plane` call below, exactly as the
440 // key-frame paths do.
441 let palette_y_buf: Option<Vec<u16>> = palette_y
442 .as_ref()
443 .map(|p| p.map.iter().map(|&i| p.colors[i as usize]).collect());
444 let palette_uv_bufs: Option<(Vec<u16>, Vec<u16>)> = palette_uv.as_ref().map(|p| {
445 (
446 p.map.iter().map(|&i| p.u_colors[i as usize]).collect(),
447 p.map.iter().map(|&i| p.v_colors[i as usize]).collect(),
448 )
449 });
450 mode_for_tx = mode;
451 block_filter = [3, 3];
452 ref_frame_for_lf = 0;
453 globalmv_for_lf = false;
454 let (mi_row, mi_col) = (rmi, cmi);
455 grid.fill_rect(
456 mi_row,
457 mi_col,
458 side / 4,
459 side / 4,
460 MiInfo {
461 is_inter: false,
462 ref_frame: -1,
463 ref_frame1: NO_REF1,
464 mv1: (0, 0),
465 mv: (0, 0),
466 is_new_mv: false,
467 size: (side / 4) as u8,
468 size_h: (side / 4) as u8,
469 is_global_mv0: false,
470 is_global_mv1: false,
471 },
472 );
473
474 // lane-intrainter r1: an intra block in an inter frame reads the same
475 // `tx_depth` symbol a key-frame block does, and when it splits, the
476 // block codes one intra prediction + one coefficient set PER
477 // transform unit (`decode_block`'s multi-TU branch), not one for the
478 // whole block. The leaf list rides the existing `vartx_leaves` slot,
479 // whose tail already does the split-block neighbour write-back
480 // (`record_split_luma`) and the per-TU deblock edges.
481 vartx_leaves = read_block_tx_size(
482 dec,
483 cdfs,
484 neighbours,
485 at_mi,
486 side,
487 (mi_cols as usize, mi_rows as usize),
488 false,
489 skip,
490 fctx,
491 )?
492 .1;
493 let reach = Reach::of(side, px, py, y.width, y.height, fctx);
494 let split_luma = vartx_leaves.is_some();
495 // lane-sb128c r3: a 128x128 INTRA block inside an inter frame codes
496 // its chroma per 64x64 mu chunk, exactly as the inter half above --
497 // `decode_token_recon_block` walks `mu_blocks_wide/high` and codes
498 // every plane of a chunk before the next chunk's luma. These collect
499 // the four TX_32X32 units per plane for the block-level write-back.
500 let mut u_units: Vec<i32> = Vec::new();
501 let mut v_units: Vec<i32> = Vec::new();
502 if let Some(leaves) = vartx_leaves.as_ref() {
503 let reduced = fctx.reduced_tx_set_inter.with(std::cell::Cell::get);
504 // lane-av1h5: chunk-major token order at 128 (see [`mu_chunk_order`]).
505 let (order, _mu_reordered) = mu_chunk_order(leaves, side);
506 for (pos, &idx) in order.iter().enumerate() {
507 let (row, col, tx_px, _) = leaves[idx];
508 let tu_mi = (at_mi.0 + row, at_mi.1 + col);
509 let (tu_px, tu_py) = (px + col * MI, py + row * MI);
510 // A transform unit inside a block is not a block at that
511 // position (class from lane-palette2 r12): top-right /
512 // bottom-left availability is the block's, narrowed by the
513 // unit's own offset inside it.
514 let tu_reach = Reach::of_tu(side, side, col * MI, row * MI, tx_px, tx_px, reach);
515 // This unit's own window on the block's palette prediction
516 // (the whole-block buffer would be indexed at the wrong
517 // stride by the first unit).
518 if let Some(buf) = &palette_y_buf {
519 hit!(PALETTE_SPLIT_TX_HITS);
520 let window: Vec<u16> = (0..tx_px)
521 .flat_map(|i| {
522 let base = (row * MI + i) * side + col * MI;
523 buf[base..base + tx_px].iter().copied()
524 })
525 .collect();
526 set_palette_pred(window, fctx);
527 }
528 let tu_grid = if skip {
529 push_intra(
530 y,
531 u,
532 v,
533 0,
534 tu_px,
535 tu_py,
536 tx_px,
537 mode,
538 angle_delta_y,
539 tu_reach,
540 &ZERO_RESIDUAL[..tx_px * tx_px],
541 None,
542 filter_intra,
543 smooth_neighbor,
544 fctx,
545 );
546 Grid::Zero(tx_px * tx_px)
547 } else {
548 // The unit's bsize is smaller than the block it sits in,
549 // so `txb_skip_ctx` is the neighbour-magnitude table, not
550 // the lone-TU 0 (`get_txb_ctx_general`).
551 let tu_skip_ctx = neighbours.luma_skip_ctx(tu_mi, tx_px / MI);
552 read_plane(
553 dec,
554 cdfs,
555 txbset_for(tx_px, reduced),
556 // lane-sb128c r5: a TX_64X64 unit codes only its
557 // top-left 32x32 of frequencies (spec 5.11.40), so
558 // the scan and `tx_side` are the CODED corner while
559 // `side` stays the transform's true 64.
560 default_scan(tx_px.min(32)),
561 0,
562 neighbours.around_mi(tu_mi, tx_px)[0],
563 mode,
564 mode,
565 angle_delta_y,
566 tu_reach,
567 y,
568 u,
569 v,
570 tu_px,
571 tu_py,
572 tx_px,
573 tx_px.min(32),
574 base_q_idx,
575 None,
576 filter_intra,
577 Some(tu_skip_ctx),
578 smooth_neighbor,
579 fctx,
580 )?
581 };
582 neighbours.record_mi_luma(tu_mi, tx_px, &tu_grid);
583 if side > 64 {
584 let chunk = (row / 16, col / 16);
585 let done = pos + 1 == order.len()
586 || (leaves[order[pos + 1]].0 / 16, leaves[order[pos + 1]].1 / 16) != chunk;
587 if !done {
588 continue;
589 }
590 let (cr, cc) = chunk;
591 hit!(INTRA_128_IN_INTER_MU_CHROMA_HITS);
592 // The unit's CHROMA size, its LUMA footprint, the
593 // CHUNK's chroma extent and the unit's ORIGIN inside the
594 // chunk are four different quantities; this arm had the
595 // last three as one 4:2:0 literal and read ONE unit per
596 // mu chunk at every subsampling.
597 //
598 // `cu_tx` is TX_32X32 at EVERY subsampling.
599 // `av1_get_max_uv_txsize` (blockd.h:1371) is
600 // `av1_get_adjusted_tx_size(max_txsize_rect_lookup[
601 // get_plane_block_size(bsize, ss_x, ss_y)])`, and
602 // `av1_get_adjusted_tx_size` (blockd.h:1361) takes NO
603 // subsampling argument: it maps TX_64X64 / TX_64X32 /
604 // TX_32X64 to TX_32X32 UNCONDITIONALLY, so even at ss (0,0)
605 // (where `get_plane_block_size` returns BLOCK_128X128 and
606 // the lookup entry is TX_64X64) the answer is TX_32X32.
607 let cu_tx = 32usize;
608 // The unit's own footprint on the LUMA grid, per axis
609 // (`cu_tx << ss`): 64 luma px (16 mi) at 4:2:0, 32 luma px
610 // (8 mi) at 4:4:4. This is the ss-aware form the repaired
611 // siblings at decode.rs:17598 / 17685 use (`ox <<
612 // ss_x(fctx)`, never `ox * 2`), and it is the size
613 // `around_mi` / `record_mi_chroma` want.
614 let unit_luma_w = cu_tx << ss_x(fctx);
615 let unit_luma_h = cu_tx << ss_y(fctx);
616 // The CHUNK's own extent, per axis: a mu chunk is a
617 // 64x64 LUMA region at every subsampling, so on the luma
618 // grid this is a literal 64, and its CHROMA plane block is
619 // `64 >> ss` square. At 4:2:0 that is 32x32 = the one unit
620 // this arm always coded; at 4:4:4 it is 64x64 = FOUR
621 // TX_32X32 units. Measured on the gate's own 4:4:4 stream
622 // against the oracle's `EC_TRACE_COEFF`: every chroma read
623 // of a 128x128 intra-in-inter root is `tx_size=3`
624 // (TX_32X32), and each 64x64 mu chunk contributes FOUR per
625 // plane at (row,col) (0,0) (0,8) (8,0) (8,8) -- never a
626 // TX_64X64 chroma unit.
627 let chunk_luma = 64usize;
628 let chunk_chroma_w = chunk_luma >> ss_x(fctx);
629 let chunk_chroma_h = chunk_luma >> ss_y(fctx);
630 let units_w = chunk_chroma_w / cu_tx;
631 let units_h = chunk_chroma_h / cu_tx;
632 let cu_scan = default_scan(cu_tx);
633 if lossless(fctx) {
634 // lane-lossless2: TX_4x4 units over this chunk's
635 // chroma, not TX_32X32 ones.
636 read_intra_chroma_lossless(
637 y,
638 u,
639 v,
640 dec,
641 cdfs,
642 neighbours,
643 at_mi,
644 (px, py),
645 (cpx, cpy),
646 side,
647 // lane-av1422lpf site 6 (the intra-in-inter twin of
648 // site 5 at 41572): the plane block's own per-axis
649 // shape and stride, never the enclosing square.
650 // `palette_uv_bufs` is the decoded colour-index map
651 // at `(write_w >> ss_x, write_h >> ss_y)` (44144),
652 // i.e. per-axis already, and the lossless walk
653 // windows it at this `stride` -- at 4:2:2 a
654 // 16-wide square stride over an 8-wide map put the
655 // third unit row at index 180 of 128 samples
656 // (`palette_window`, decode.rs:2946; measured on
657 // `Y_intrabc_10b.obu`).
658 (chroma_stride, chroma_buf_h),
659 (cc * chunk_chroma_w, cr * chunk_chroma_h),
660 (chunk_chroma_w, chunk_chroma_h),
661 chroma_stride,
662 reach,
663 (y.width, y.height),
664 mode,
665 uv_predict_mode,
666 angle_delta_uv,
667 palette_uv_bufs.as_ref().map(|(a, b)| (&a[..], &b[..])),
668 smooth_neighbor_uv,
669 base_q_idx,
670 &mut u_units,
671 &mut v_units,
672 fctx,
673 )?;
674 mu_chroma = true;
675 continue;
676 }
677 // Plane-major per chunk (libaom's plane loop is outermost
678 // inside the mu walk); within a plane the units walk raster.
679 for plane_idx in 1..3 {
680 for ur in 0..units_h {
681 for uc in 0..units_w {
682 // The unit's own mi origin: the chunk's
683 // (16x16 mi) plus the unit's offset inside it.
684 let unit_mi = (
685 at_mi.0 + cr * 16 + ur * (unit_luma_h / MI),
686 at_mi.1 + cc * 16 + uc * (unit_luma_w / MI),
687 );
688 // The unit's own entropy-cell span, so the
689 // NEXT unit of this chunk reads THIS one's
690 // state and the next chunk reads the last.
691 //
692 // lane-av1muchunk422iis: the intra-in-inter twin
693 // of the compound mu-chunk gather. At ss (1, 0)
694 // one CHROMA column spans two luma mi columns,
695 // so libaom's `get_txb_ctx_general` reads its
696 // above votes over `txb_w_unit` chroma cells
697 // while a plain luma `around_mi` gathers
698 // `unit_luma_w / MI` -- twice as many -- and
699 // `dc_sign_ctx`'s signum flips whenever the
700 // doubled above vote cancels the left one. The
701 // LEFT extent is unsubsampled at ss_y 0, so the
702 // per-axis footprint is what the pair-aware
703 // helper needs; 4:2:0 / 4:4:4 have square
704 // footprints and keep the plain gather verbatim.
705 let cu_around = if ss_x(fctx) == 1 && ss_y(fctx) == 0 {
706 neighbours.around_mi_422_chroma(unit_mi, unit_luma_w, unit_luma_h)
707 } else {
708 neighbours.around_mi(unit_mi, unit_luma_w)
709 };
710 let (cu_x, cu_y) = (
711 cpx + cc * chunk_chroma_w + uc * cu_tx,
712 cpy + cr * chunk_chroma_h + ur * cu_tx,
713 );
714 let cu_reach = crate::decode::tu_reach(
715 side,
716 side,
717 cc * chunk_luma + uc * unit_luma_w,
718 cr * chunk_luma + ur * unit_luma_h,
719 unit_luma_w,
720 reach,
721 px,
722 py,
723 y.width,
724 y.height,
725 fctx,
726 );
727 let cu_grid = if skip {
728 push_intra(
729 y,
730 u,
731 v,
732 plane_idx,
733 cu_x,
734 cu_y,
735 cu_tx,
736 uv_predict_mode,
737 angle_delta_uv,
738 cu_reach,
739 &ZERO_RESIDUAL[..cu_tx * cu_tx],
740 None,
741 None,
742 smooth_neighbor_uv,
743 fctx,
744 );
745 Grid::Zero(cu_tx * cu_tx)
746 } else {
747 read_plane(
748 dec,
749 cdfs,
750 chroma_set,
751 cu_scan,
752 plane_idx,
753 cu_around[plane_idx],
754 mode,
755 uv_predict_mode,
756 angle_delta_uv,
757 cu_reach,
758 y,
759 u,
760 v,
761 cu_x,
762 cu_y,
763 cu_tx,
764 cu_tx,
765 base_q_idx,
766 // CfL is refused above 32x32
767 // (`is_cfl_allowed`), so never here.
768 None,
769 None,
770 // The uv plane block is larger than
771 // the unit: `get_txb_ctx`'s offset-10
772 // rows, +3 on the `txb_skip_chroma_32`
773 // table (lane-sb128b r3).
774 Some(3),
775 smooth_neighbor_uv,
776 fctx,
777 )?
778 };
779 // Immediately, so the NEXT unit reads this
780 // one's coefficient context.
781 neighbours.record_mi_chroma(
782 unit_mi,
783 unit_luma_w,
784 unit_luma_h,
785 plane_idx,
786 &cu_grid,
787 );
788 hit!(CHROMA_SPLIT_TX_HITS);
789 // lane-dpm1: the two INTER mu-chunk sites set
790 // this and this INTRA-in-inter one did not, so
791 // the whole-block chroma record below re-stamped
792 // all four 32x32 units with the TOP-LEFT unit's
793 // level and the next block read a txb_skip row
794 // libaom never selects (class
795 // `override-slot-on-one-arm`).
796 mu_chroma = true;
797 // lane-av1muchunk422b: the allocation of the intra-in-inter
798 // mu-chunk walk (site 8), per TX_32X32 unit.
799 hit!(MU_CHUNK_INTRA_IN_INTER_UNITS);
800 let dst = mu_units(
801 if plane_idx == 1 {
802 &mut u_units
803 } else {
804 &mut v_units
805 },
806 chroma_side * chroma_side,
807 );
808 // The ASSEMBLED grid stays at the square
809 // `chroma_side` stride the block tail's
810 // re-stamp indexes; the unit's true per-axis
811 // position inside its chunk is what changes.
812 for rr in 0..cu_tx {
813 let start = (cr * chunk_chroma_h + ur * cu_tx + rr) * chroma_side
814 + cc * chunk_chroma_w
815 + uc * cu_tx;
816 dst[start..start + cu_tx]
817 .copy_from_slice(&cu_grid[rr * cu_tx..][..cu_tx]);
818 }
819 }
820 }
821 }
822 }
823 }
824 }
825 // lane-av1-422k: the chroma PREDICTION shape is the plane block
826 // (`side >> ss_x`, `side >> ss_y`) -- square at 4:2:0/4:4:4, a
827 // (side/2, side) rect at 4:2:2. `chroma_side` (the 422j enclosing
828 // square of that rect) is the right size for BOOKKEEPING buffers but
829 // the wrong SHAPE to predict and reconstruct at: an intra-in-inter
830 // block's chroma prediction indexes its CFL signal and residual
831 // `row * chroma_w + col` over the plane block, and a 16x16 luma
832 // block's 8x16 chroma CFL signal is 128 samples -- predicting at the
833 // 16x16 square read `ac[128]` out of it (frame 1, px=192 py=96).
834 let chroma_w = side >> ss_x(fctx);
835 let chroma_h = side >> ss_y(fctx);
836 if side > 64 {
837 // Chroma is already coded and reconstructed per mu chunk above.
838 luma_grid = Grid::Zero(side * side);
839 u_grid = if u_units.is_empty() {
840 Grid::Zero(chroma_stride * chroma_buf_h)
841 } else {
842 Grid::Own(std::mem::take(&mut u_units))
843 };
844 v_grid = if v_units.is_empty() {
845 Grid::Zero(chroma_stride * chroma_buf_h)
846 } else {
847 Grid::Own(std::mem::take(&mut v_units))
848 };
849 hit!(INTRA_128_IN_INTER_HITS);
850 } else if skip {
851 if !split_luma {
852 if let Some(buf) = &palette_y_buf {
853 set_palette_pred(buf.clone(), fctx);
854 }
855 push_intra(
856 y,
857 u,
858 v,
859 0,
860 px,
861 py,
862 side,
863 mode,
864 angle_delta_y,
865 reach,
866 &ZERO_RESIDUAL[..side * side],
867 None,
868 filter_intra,
869 smooth_neighbor,
870 fctx,
871 );
872 }
873 let ac = alpha.map(|_| cfl_src(px, py, side));
874 if let Some((ub, _)) = &palette_uv_bufs {
875 set_palette_pred(ub.clone(), fctx);
876 }
877 push_intra_unit(
878 y,
879 u,
880 v,
881 1,
882 cpx,
883 cpy,
884 chroma_w,
885 chroma_h,
886 uv_predict_mode,
887 angle_delta_uv,
888 reach,
889 &ZERO_RESIDUAL[..chroma_w * chroma_h],
890 alpha.zip(ac).map(|((au, _), ac)| (au, ac)),
891 None,
892 smooth_neighbor_uv,
893 fctx,
894 );
895 if let Some((_, vb)) = &palette_uv_bufs {
896 set_palette_pred(vb.clone(), fctx);
897 }
898 push_intra_unit(
899 y,
900 u,
901 v,
902 2,
903 cpx,
904 cpy,
905 chroma_w,
906 chroma_h,
907 uv_predict_mode,
908 angle_delta_uv,
909 reach,
910 &ZERO_RESIDUAL[..chroma_w * chroma_h],
911 alpha.zip(ac).map(|((_, av), ac)| (av, ac)),
912 None,
913 smooth_neighbor_uv,
914 fctx,
915 );
916 luma_grid = Grid::Zero(side * side);
917 u_grid = Grid::Zero(chroma_w * chroma_h);
918 v_grid = Grid::Zero(chroma_w * chroma_h);
919 } else {
920 let around = neighbours.around_mi(at, side);
921 if split_luma {
922 // Already coded and reconstructed unit by unit above; the
923 // block-level grid is only the neighbour write-back's input,
924 // which `record_split_luma` ignores for plane 0.
925 luma_grid = Grid::Zero(side * side);
926 } else {
927 if let Some(buf) = &palette_y_buf {
928 set_palette_pred(buf.clone(), fctx);
929 }
930 luma_grid = read_plane(
931 dec,
932 cdfs,
933 luma_set_intra,
934 scan_luma,
935 0,
936 around[0],
937 mode,
938 mode,
939 angle_delta_y,
940 reach,
941 y,
942 u,
943 v,
944 px,
945 py,
946 side,
947 luma_tx,
948 base_q_idx,
949 None,
950 filter_intra,
951 None,
952 smooth_neighbor,
953 fctx,
954 )?;
955 }
956 let ac = alpha.map(|_| cfl_src(px, py, side));
957 if lossless(fctx) {
958 // lane-lossless2: the block's chroma plane is a raster of 4x4
959 // units, plane-major (`read_intra_chroma_lossless`).
960 let (mut uo, mut vo) = (Vec::new(), Vec::new());
961 read_intra_chroma_lossless(
962 y,
963 u,
964 v,
965 dec,
966 cdfs,
967 neighbours,
968 at_mi,
969 (px, py),
970 (cpx, cpy),
971 side,
972 // lane-av1422lpf site 6 (the whole-block twin of the mu
973 // chunk fix at 44374): the plane block's own per-axis shape
974 // and its own stride, never the enclosing square
975 // `chroma_side`. At 4:2:0 and 4:4:4 all three are the same
976 // number.
977 (chroma_stride, chroma_buf_h),
978 (0, 0),
979 (chroma_stride, chroma_buf_h),
980 chroma_stride,
981 reach,
982 (y.width, y.height),
983 mode,
984 uv_predict_mode,
985 angle_delta_uv,
986 palette_uv_bufs.as_ref().map(|(a, b)| (&a[..], &b[..])),
987 smooth_neighbor_uv,
988 base_q_idx,
989 &mut uo,
990 &mut vo,
991 fctx,
992 )?;
993 let zero = chroma_side * chroma_side;
994 u_grid = if uo.is_empty() {
995 Grid::Zero(zero)
996 } else {
997 Grid::Own(uo)
998 };
999 v_grid = if vo.is_empty() {
1000 Grid::Zero(zero)
1001 } else {
1002 Grid::Own(vo)
1003 };
1004 mu_chroma = true;
1005 } else if ss_x(fctx) == 0 && ss_y(fctx) == 0 && chroma_side > chroma_tx {
1006 // lane-av1-rect128recon: at ss 0/0 the uv plane block of a
1007 // 64x64 intra block inside an inter frame is 64x64 while
1008 // `av1_get_max_uv_txsize` caps the unit at TX_32X32 -- FOUR
1009 // plane-major TX_32X32 units (libaom
1010 // `decode_token_recon_block`'s plane loop), each with its own
1011 // neighbour context and `get_txb_ctx`'s offset-10 rows (+3 on
1012 // the `txb_skip_chroma_32` table, lane-sb128b r3), each
1013 // stamped immediately so the next unit reads it. The old arm
1014 // read ONE whole-plane unit with the block-level context and
1015 // no offset: three quarters of the plane's residual was never
1016 // read, the unit's `txb_skip` came off the offset-7 row (444
1017 // witness f2, right-half intra 64x64: U unit (0,0) ctx 0
1018 // where aom reads 10), and the coded corner was even
1019 // dequantized at the TX_64X64 dqDenom.
1020 let mu_units_n = chroma_side / chroma_tx;
1021 let mut u_acc = vec![0i32; chroma_side * chroma_side];
1022 let mut v_acc = vec![0i32; chroma_side * chroma_side];
1023 for plane_idx in 1..=2 {
1024 for cu_row in 0..mu_units_n {
1025 for cu_col in 0..mu_units_n {
1026 let cu_mi = (
1027 at_mi.0 + cu_row * (chroma_tx / MI),
1028 at_mi.1 + cu_col * (chroma_tx / MI),
1029 );
1030 let cu_around = neighbours.around_mi(cu_mi, chroma_tx);
1031 let cu_reach = crate::decode::tu_reach(
1032 side,
1033 side,
1034 cu_col * chroma_tx,
1035 cu_row * chroma_tx,
1036 chroma_tx,
1037 reach,
1038 px,
1039 py,
1040 y.width,
1041 y.height,
1042 fctx,
1043 );
1044 let cu_grid = read_plane(
1045 dec,
1046 cdfs,
1047 chroma_set,
1048 scan_chroma,
1049 plane_idx,
1050 cu_around[plane_idx],
1051 mode,
1052 uv_predict_mode,
1053 angle_delta_uv,
1054 cu_reach,
1055 y,
1056 u,
1057 v,
1058 cpx + cu_col * chroma_tx,
1059 cpy + cu_row * chroma_tx,
1060 chroma_tx,
1061 chroma_tx,
1062 base_q_idx,
1063 // CfL is capped at 32x32 (`is_cfl_allowed`),
1064 // so a block this size never carries alpha.
1065 None,
1066 None,
1067 Some(3),
1068 smooth_neighbor_uv,
1069 fctx,
1070 )?;
1071 // Immediately, so the NEXT unit reads this one's
1072 // coefficient context.
1073 neighbours
1074 .record_mi_chroma(cu_mi, chroma_tx, chroma_tx, plane_idx, &cu_grid);
1075 hit!(CHROMA_SPLIT_TX_HITS);
1076 let acc = if plane_idx == 1 {
1077 &mut u_acc
1078 } else {
1079 &mut v_acc
1080 };
1081 for rr in 0..chroma_tx {
1082 let start =
1083 (cu_row * chroma_tx + rr) * chroma_side + cu_col * chroma_tx;
1084 acc[start..start + chroma_tx]
1085 .copy_from_slice(&cu_grid[rr * chroma_tx..][..chroma_tx]);
1086 }
1087 }
1088 }
1089 }
1090 mu_chroma = true;
1091 u_grid = Grid::Own(u_acc);
1092 v_grid = Grid::Own(v_acc);
1093 } else {
1094 // lane-av1-422k: the chroma plane block is (`chroma_w`,
1095 // `chroma_h`) -- the square read above at 4:2:0/4:4:4, one rect
1096 // TX_{side/2}X{side} unit through [`read_rect_chroma_unit`] at
1097 // 4:2:2 (the key-frame path's proven set/scan rows, and the
1098 // per-chroma-cell context read [`around_mi_422_chroma`]).
1099 let chroma_around = if chroma_w == chroma_h {
1100 around
1101 } else {
1102 neighbours.around_mi_422_chroma(at, side, side)
1103 };
1104 if let Some((ub, _)) = &palette_uv_bufs {
1105 set_palette_pred(ub.clone(), fctx);
1106 }
1107 u_grid = if chroma_w == chroma_h {
1108 read_plane(
1109 dec,
1110 cdfs,
1111 chroma_set,
1112 scan_chroma,
1113 1,
1114 chroma_around[1],
1115 mode,
1116 uv_predict_mode,
1117 angle_delta_uv,
1118 reach,
1119 y,
1120 u,
1121 v,
1122 cpx,
1123 cpy,
1124 chroma_side,
1125 chroma_tx,
1126 base_q_idx,
1127 alpha.zip(ac).map(|((au, _), ac)| (au, ac)),
1128 None,
1129 None,
1130 smooth_neighbor_uv,
1131 fctx,
1132 )?
1133 } else if side == 64 {
1134 // lane-av1-422inter: `av1_get_max_uv_txsize(BLOCK_64X64)` at
1135 // ss (1,0) adjusts `max_txsize_rect_lookup[BLOCK_32X64]` =
1136 // TX_32X64 down to TX_32X32 (`av1_get_adjusted_tx_size`), so
1137 // the (side/2, side) = (32, 64) plane block codes TWO stacked
1138 // SQUARE TX_32X32 units per plane, plane-major -- the
1139 // key-frame path's own model (`decode_block`'s 4:2:2
1140 // chroma_tx table, "64 and 128: TX_32X64 / TX_64X64 adjust
1141 // down to TX_32X32", proven byte-exact on this fixture's
1142 // frame 0) and the twin of this function's own side>64
1143 // mu-chunk walk above. The whole-plane-block rect read this
1144 // arm replaced handed [`read_coeffs_rect`] a 36x68 levels
1145 // frame it never had (the frame-9 panic) and would have read
1146 // a full TX_32X64 scan libaom never codes.
1147 let cu_scan = default_scan(32);
1148 let (cu_w, cu_h) = (32usize, 32usize);
1149 let luma_span_x = cu_w << ss_x(fctx);
1150 let luma_span_y = cu_h << ss_y(fctx);
1151 let mut assembled = vec![0i32; chroma_side * chroma_side];
1152 for cu_row in 0..chroma_h / cu_h {
1153 let cu_mi = (at_mi.0 + cu_row * (luma_span_y / MI), at_mi.1);
1154 let cu_around =
1155 neighbours.around_mi_422_chroma(cu_mi, luma_span_x, luma_span_y);
1156 let cu_reach = tu_reach_rect(
1157 side,
1158 side,
1159 0,
1160 cu_row * luma_span_y,
1161 luma_span_x,
1162 luma_span_y,
1163 reach,
1164 px,
1165 py,
1166 y.width,
1167 y.height,
1168 fctx,
1169 );
1170 // The 4:2:2 plane block is `(chroma_w, chroma_h)` --
1171 // 32x64 -- but codes TWO stacked TX_32X32 units, so
1172 // each unit needs its OWN window on the block-sized
1173 // chroma palette prediction. Armed once above, the
1174 // first unit consumed the whole 2048-entry buffer and
1175 // the second found none pending, silently falling back
1176 // to the edge DC prediction (s422_384x240 decode
1177 // frame 1, U/V rows 170..189 cols 23..31).
1178 if let Some((ub, _)) = &palette_uv_bufs {
1179 set_palette_pred(
1180 palette_window(ub, chroma_w, 0, cu_row * cu_h, cu_w, cu_h),
1181 fctx,
1182 );
1183 }
1184 hit!(PALETTE_422_UNIT_WINDOW_HITS);
1185 let cu_grid = read_plane(
1186 dec,
1187 cdfs,
1188 TxbSet::Chroma32,
1189 cu_scan,
1190 1,
1191 cu_around[1],
1192 mode,
1193 uv_predict_mode,
1194 angle_delta_uv,
1195 cu_reach,
1196 y,
1197 u,
1198 v,
1199 cpx,
1200 cpy + cu_row * cu_h,
1201 cu_w,
1202 cu_w,
1203 base_q_idx,
1204 alpha.zip(ac).map(|((au, _), ac)| (au, ac)),
1205 None,
1206 // The uv plane block (32x64) is larger than the unit
1207 // (32x32): `get_txb_ctx`'s offset-10 rows, +3 on the
1208 // `txb_skip_chroma_32` table (lane-sb128b r3).
1209 Some(3),
1210 smooth_neighbor_uv,
1211 fctx,
1212 )?;
1213 hit!(CHROMA_SPLIT_TX_HITS);
1214 hit!(CHROMA422_SQUARE_HITS);
1215 // Immediately, so the next unit of this plane reads this
1216 // one's coefficient context; the whole-block record below
1217 // re-stamps the block over these and the tail's
1218 // `mu_chroma_units` replay puts the per-unit state back
1219 // (lane-dpm1).
1220 neighbours.record_mi_chroma(cu_mi, luma_span_x, luma_span_y, 1, &cu_grid);
1221 for rr in 0..cu_h {
1222 let start = (cu_row * cu_h + rr) * chroma_side;
1223 assembled[start..start + cu_w]
1224 .copy_from_slice(&cu_grid[rr * cu_w..][..cu_w]);
1225 }
1226 }
1227 mu_chroma = true;
1228 Grid::Own(assembled)
1229 } else {
1230 let (rect_set, rect_scan): (TxbSet, &[u16]) = match side {
1231 4 => (TxbSet::Chroma4, default_scan(4)),
1232 8 => (TxbSet::ChromaRect8x4, &SCAN_4X8[..]),
1233 16 => (TxbSet::ChromaRect16x8, &SCAN_8X16[..]),
1234 32 => (TxbSet::ChromaRect32x16, &SCAN_16X32[..]),
1235 _ => (TxbSet::Chroma32, default_scan(32)),
1236 };
1237 read_rect_chroma_unit(
1238 dec,
1239 cdfs,
1240 rect_set,
1241 rect_scan,
1242 chroma_w,
1243 chroma_h,
1244 1,
1245 chroma_around[1],
1246 uv_predict_mode,
1247 angle_delta_uv,
1248 reach,
1249 y,
1250 u,
1251 v,
1252 cpx,
1253 cpy,
1254 base_q_idx,
1255 None,
1256 alpha.zip(ac).map(|((au, _), ac)| (au, ac)),
1257 smooth_neighbor_uv,
1258 fctx,
1259 )?
1260 };
1261 if let Some((_, vb)) = &palette_uv_bufs {
1262 set_palette_pred(vb.clone(), fctx);
1263 }
1264 v_grid = if chroma_w == chroma_h {
1265 read_plane(
1266 dec,
1267 cdfs,
1268 chroma_set,
1269 scan_chroma,
1270 2,
1271 chroma_around[2],
1272 mode,
1273 uv_predict_mode,
1274 angle_delta_uv,
1275 reach,
1276 y,
1277 u,
1278 v,
1279 cpx,
1280 cpy,
1281 chroma_side,
1282 chroma_tx,
1283 base_q_idx,
1284 alpha.zip(ac).map(|((_, av), ac)| (av, ac)),
1285 None,
1286 None,
1287 smooth_neighbor_uv,
1288 fctx,
1289 )?
1290 } else if side == 64 {
1291 // lane-av1-422inter: plane 2 of the TX_32X32-adjusted walk
1292 // above -- same two stacked square units, plane-major (every
1293 // U unit, then every V unit, libaom `decode_token_recon_block`'s
1294 // per-mu-chunk plane loop).
1295 let cu_scan = default_scan(32);
1296 let (cu_w, cu_h) = (32usize, 32usize);
1297 let luma_span_x = cu_w << ss_x(fctx);
1298 let luma_span_y = cu_h << ss_y(fctx);
1299 let mut assembled = vec![0i32; chroma_side * chroma_side];
1300 for cu_row in 0..chroma_h / cu_h {
1301 let cu_mi = (at_mi.0 + cu_row * (luma_span_y / MI), at_mi.1);
1302 let cu_around =
1303 neighbours.around_mi_422_chroma(cu_mi, luma_span_x, luma_span_y);
1304 let cu_reach = tu_reach_rect(
1305 side,
1306 side,
1307 0,
1308 cu_row * luma_span_y,
1309 luma_span_x,
1310 luma_span_y,
1311 reach,
1312 px,
1313 py,
1314 y.width,
1315 y.height,
1316 fctx,
1317 );
1318 // The V twin of the U arm's per-unit window above.
1319 if let Some((_, vb)) = &palette_uv_bufs {
1320 set_palette_pred(
1321 palette_window(vb, chroma_w, 0, cu_row * cu_h, cu_w, cu_h),
1322 fctx,
1323 );
1324 }
1325 hit!(PALETTE_422_UNIT_WINDOW_HITS);
1326 let cu_grid = read_plane(
1327 dec,
1328 cdfs,
1329 TxbSet::Chroma32,
1330 cu_scan,
1331 2,
1332 cu_around[2],
1333 mode,
1334 uv_predict_mode,
1335 angle_delta_uv,
1336 cu_reach,
1337 y,
1338 u,
1339 v,
1340 cpx,
1341 cpy + cu_row * cu_h,
1342 cu_w,
1343 cu_w,
1344 base_q_idx,
1345 alpha.zip(ac).map(|((_, av), ac)| (av, ac)),
1346 None,
1347 Some(3),
1348 smooth_neighbor_uv,
1349 fctx,
1350 )?;
1351 hit!(CHROMA_SPLIT_TX_HITS);
1352 hit!(CHROMA422_SQUARE_HITS);
1353 neighbours.record_mi_chroma(cu_mi, luma_span_x, luma_span_y, 2, &cu_grid);
1354 for rr in 0..cu_h {
1355 let start = (cu_row * cu_h + rr) * chroma_side;
1356 assembled[start..start + cu_w]
1357 .copy_from_slice(&cu_grid[rr * cu_w..][..cu_w]);
1358 }
1359 }
1360 mu_chroma = true;
1361 Grid::Own(assembled)
1362 } else {
1363 let (rect_set, rect_scan): (TxbSet, &[u16]) = match side {
1364 4 => (TxbSet::Chroma4, default_scan(4)),
1365 8 => (TxbSet::ChromaRect8x4, &SCAN_4X8[..]),
1366 16 => (TxbSet::ChromaRect16x8, &SCAN_8X16[..]),
1367 32 => (TxbSet::ChromaRect32x16, &SCAN_16X32[..]),
1368 _ => (TxbSet::Chroma32, default_scan(32)),
1369 };
1370 read_rect_chroma_unit(
1371 dec,
1372 cdfs,
1373 rect_set,
1374 rect_scan,
1375 chroma_w,
1376 chroma_h,
1377 2,
1378 chroma_around[2],
1379 uv_predict_mode,
1380 angle_delta_uv,
1381 reach,
1382 y,
1383 u,
1384 v,
1385 cpx,
1386 cpy,
1387 base_q_idx,
1388 None,
1389 alpha.zip(ac).map(|((_, av), ac)| (av, ac)),
1390 smooth_neighbor_uv,
1391 fctx,
1392 )?
1393 };
1394 }
1395 }
1396 Ok(Some((
1397 mode_for_tx,
1398 uv_predict_mode,
1399 block_filter,
1400 ref_frame_for_lf,
1401 globalmv_for_lf,
1402 luma_grid,
1403 u_grid,
1404 v_grid,
1405 vartx_leaves,
1406 palette_record,
1407 mu_chroma,
1408 )))
1409}