forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

leaf_sub8.rs3176 lines, 124.6 KiBHistoryRawDownload
1//! Sub-8x8 chroma-format leaf decoders and the split4 / rect8 leaves. //! av1split: moved out of `decode/mod.rs`; the items are unchanged apart from visibility. (D15)
2
3use super::*;
4
5/// 4:4:4 sub-8x8 key-frame leaf chroma (lane-av1-444). At subsampling 0/0
6/// `is_chroma_reference` (av1_common_int.h:1459) is unconditional, so EVERY
7/// 4x4 / 4x8 / 8x4 leaf codes its own full-resolution chroma unit right
8/// after its own luma (`read_intra_mode_sub8` already read this leaf's own
9/// `uv_mode`), not the one shared 4x4 unit [`decode_leaf_split4`] /
10/// [`decode_leaf_rect8`]'s group tails decode at 4:2:0. Mirrors those tails
11/// arm for arm at leaf scale: a skipped leaf keeps its CfL contribution, an
12/// intrabc leaf predicts a frame copy at its DV with the luma `tx_type`
13/// inherited through [`INTRABC_CHROMA_TX`] (the caller arms it right after
14/// the luma read) -- and a SKIPPED intrabc leaf is still the DV copy, never
15/// an intra prediction (lane-av1-ibcskip2: libaom's
16/// `av1_build_inter_predictors_sb` runs for an intrabc block regardless of
17/// `skip`). The chroma position, reach and entropy-context span are
18/// the leaf's own (ss 0/0); the 4x8/8x4 unit reads the same
19/// `ChromaRect8x4` tables [`decode_rect_split`] proves for that shape.
20/// Records the leaf's own `uv_mode` map entry and chroma above/left state,
21/// and returns the (u, v) grids for the caller's coarse slots.
22#[allow(clippy::too_many_arguments)]
23pub(super) fn sub8_leaf_chroma444(
24 dec: &mut SymbolDecoder,
25 cdfs: &mut Cdfs,
26 neighbours: &mut Neighbours,
27 lmi: (usize, usize),
28 bw: usize,
29 bh: usize,
30 uv_mode: usize,
31 alpha: Option<(i32, i32)>,
32 skip: bool,
33 intrabc_dv: Option<(i32, i32)>,
34 y: &mut PlaneBuf<'static>,
35 u: &mut PlaneBuf<'static>,
36 v: &mut PlaneBuf<'static>,
37 scan4: &[u16],
38 base_q_idx: u8,
39 smooth_neighbor_uv: bool,
40 fctx: &crate::decode::FrameCtx,
41) -> Result<(Grid, Grid, usize, usize)> {
42 let uv_predict_mode = if uv_mode == UV_CFL_PRED {
43 DC_PRED
44 } else {
45 uv_mode
46 };
47 let (w_mi, h_mi) = (bw / MI, bh / MI);
48 neighbours.record_uv_mode_mi(lmi.0, lmi.1, w_mi, h_mi, uv_predict_mode);
49 let (px, py) = (lmi.1 * MI, lmi.0 * MI);
50 let square = (bw, bh) == (4, 4);
51 // `Reach::of_rect` has no 4x4 table (`rect_reach_tables` starts at 4x8);
52 // the square unit takes the leaf's own `Reach::of`, as its luma does.
53 let reach = if square {
54 Reach::of(4, px, py, y.width, y.height, fctx)
55 } else {
56 Reach::of_rect(bw, bh, px, py, y.width, y.height, fctx)
57 };
58 let grids = if let Some(dv) = intrabc_dv.filter(|_| skip) {
59 // lane-av1-ibcskip2: a SKIPPED intrabc leaf still predicts the frame
60 // copy at its DV -- libaom's `av1_build_inter_predictors_sb` runs for
61 // an intrabc block regardless of `skip` (only the residual read is
62 // dropped), so the reconstruction is the bare DV copy. The intra push
63 // in the next arm is the NON-intrabc route; predicting DC there
64 // decoded flat cells where this fixture's oracle copies frame bytes
65 // (class skipped-intrabc-predicts-intra,
66 // lanes/av1ibcskip2.report.md). Same armed-palette-slot route the
67 // leaf's own luma skip arm uses (`decode_leaf_split4` /
68 // `decode_leaf_rect8`); the luma DV copy reads current samples.
69 let mut ub = vec![0u16; bw * bh];
70 mc::predict_with_filter(
71 &u.data,
72 u.width,
73 u.true_width,
74 u.true_height,
75 mv_to_q4(px, dv.1, 0),
76 mv_to_q4(py, dv.0, 0),
77 bw,
78 bh,
79 mc::InterpFilterKind::Bilinear,
80 &mut ub,
81 fctx,
82 );
83 let mut vb = vec![0u16; bw * bh];
84 mc::predict_with_filter(
85 &v.data,
86 v.width,
87 v.true_width,
88 v.true_height,
89 mv_to_q4(px, dv.1, 0),
90 mv_to_q4(py, dv.0, 0),
91 bw,
92 bh,
93 mc::InterpFilterKind::Bilinear,
94 &mut vb,
95 fctx,
96 );
97 hit!(SKIPPED_INTRABC_DV_COPY_HITS);
98 let zeros = &ZERO_RESIDUAL[..bw * bh];
99 if square {
100 set_palette_pred(ub, fctx);
101 push_intra(
102 y,
103 u,
104 v,
105 1,
106 px,
107 py,
108 4,
109 DC_PRED,
110 0,
111 reach,
112 zeros,
113 None,
114 None,
115 smooth_neighbor_uv,
116 fctx,
117 );
118 set_palette_pred(vb, fctx);
119 push_intra(
120 y,
121 u,
122 v,
123 2,
124 px,
125 py,
126 4,
127 DC_PRED,
128 0,
129 reach,
130 zeros,
131 None,
132 None,
133 smooth_neighbor_uv,
134 fctx,
135 );
136 } else {
137 set_palette_pred(ub, fctx);
138 push_intra_rect(
139 y,
140 u,
141 v,
142 1,
143 px,
144 py,
145 bw,
146 bh,
147 DC_PRED,
148 0,
149 reach,
150 zeros,
151 None,
152 None,
153 smooth_neighbor_uv,
154 fctx,
155 );
156 set_palette_pred(vb, fctx);
157 push_intra_rect(
158 y,
159 u,
160 v,
161 2,
162 px,
163 py,
164 bw,
165 bh,
166 DC_PRED,
167 0,
168 reach,
169 zeros,
170 None,
171 None,
172 smooth_neighbor_uv,
173 fctx,
174 );
175 }
176 // The same arm-without-clear route the 4:2:0 tails count: a skipped
177 // leaf reads no coefficient, so the inherited chroma `tx_type` is
178 // dropped unconsumed.
179 hit!(SKIPPED_INTRABC_CHROMA_ARM_HITS);
180 fctx.intrabc_chroma_tx.with(|c| c.set(None));
181 (Grid::Zero(bw * bh), Grid::Zero(bw * bh), 4, 4)
182 } else if skip {
183 let ac = alpha.map(|_| cfl_src_rect(px, py, bw, bh));
184 if square {
185 push_intra(
186 y,
187 u,
188 v,
189 1,
190 px,
191 py,
192 4,
193 uv_predict_mode,
194 0,
195 reach,
196 &ZERO_RESIDUAL[..16],
197 alpha.zip(ac).map(|((au, _), ac)| (au, ac)),
198 None,
199 smooth_neighbor_uv,
200 fctx,
201 );
202 push_intra(
203 y,
204 u,
205 v,
206 2,
207 px,
208 py,
209 4,
210 uv_predict_mode,
211 0,
212 reach,
213 &ZERO_RESIDUAL[..16],
214 alpha.zip(ac).map(|((_, av), ac)| (av, ac)),
215 None,
216 smooth_neighbor_uv,
217 fctx,
218 );
219 } else {
220 push_intra_rect(
221 y,
222 u,
223 v,
224 1,
225 px,
226 py,
227 bw,
228 bh,
229 uv_predict_mode,
230 0,
231 reach,
232 &ZERO_RESIDUAL[..bw * bh],
233 alpha.zip(ac).map(|((au, _), ac)| (au, ac)),
234 None,
235 smooth_neighbor_uv,
236 fctx,
237 );
238 push_intra_rect(
239 y,
240 u,
241 v,
242 2,
243 px,
244 py,
245 bw,
246 bh,
247 uv_predict_mode,
248 0,
249 reach,
250 &ZERO_RESIDUAL[..bw * bh],
251 alpha.zip(ac).map(|((_, av), ac)| (av, ac)),
252 None,
253 smooth_neighbor_uv,
254 fctx,
255 );
256 }
257 // The same arm-without-clear route the 4:2:0 tails count: a skipped
258 // leaf reads no coefficient, so an armed intrabc slot is dropped.
259 if fctx.intrabc_chroma_tx.with(std::cell::Cell::get).is_some() {
260 hit!(SKIPPED_INTRABC_CHROMA_ARM_HITS);
261 fctx.intrabc_chroma_tx.with(|c| c.set(None));
262 }
263 (Grid::Zero(bw * bh), Grid::Zero(bw * bh), 4, 4)
264 } else if let Some(dv) = intrabc_dv {
265 // Frame copy at the DV; the residual still reads the ordinary chroma
266 // tables with the inherited luma `tx_type` (see the 4:2:0 tails).
267 let mut ub = vec![0u16; bw * bh];
268 mc::predict_with_filter(
269 &u.data,
270 u.width,
271 u.true_width,
272 u.true_height,
273 mv_to_q4(px, dv.1, 0),
274 mv_to_q4(py, dv.0, 0),
275 bw,
276 bh,
277 mc::InterpFilterKind::Bilinear,
278 &mut ub,
279 fctx,
280 );
281 let mut vb = vec![0u16; bw * bh];
282 mc::predict_with_filter(
283 &v.data,
284 v.width,
285 v.true_width,
286 v.true_height,
287 mv_to_q4(px, dv.1, 0),
288 mv_to_q4(py, dv.0, 0),
289 bw,
290 bh,
291 mc::InterpFilterKind::Bilinear,
292 &mut vb,
293 fctx,
294 );
295 let around = neighbours.around_mi_rect(lmi, bw, bh);
296 let pair = if square {
297 set_palette_pred(ub, fctx);
298 let ug = read_plane(
299 dec,
300 cdfs,
301 TxbSet::Chroma4,
302 scan4,
303 1,
304 around[1],
305 DC_PRED,
306 DC_PRED,
307 0,
308 reach,
309 y,
310 u,
311 v,
312 px,
313 py,
314 4,
315 TX4,
316 base_q_idx,
317 None,
318 None,
319 None,
320 smooth_neighbor_uv,
321 fctx,
322 )?;
323 set_palette_pred(vb, fctx);
324 let vg = read_plane(
325 dec,
326 cdfs,
327 TxbSet::Chroma4,
328 scan4,
329 2,
330 around[2],
331 DC_PRED,
332 DC_PRED,
333 0,
334 reach,
335 y,
336 u,
337 v,
338 px,
339 py,
340 4,
341 TX4,
342 base_q_idx,
343 None,
344 None,
345 None,
346 smooth_neighbor_uv,
347 fctx,
348 )?;
349 (ug, vg, 4, 4)
350 } else {
351 // lane-av1-ibc444rect: the unskipped lossless chroma walk this
352 // lane fixes (one rect unit -> per-4x4 Chroma4 units).
353 if lossless(fctx) {
354 hit!(INTRABC_RECT8_LOSSLESS_CHROMA_HITS);
355 }
356 if lossless(fctx) {
357 // lane-av1-ibc444rect: at lossless `read_tx_size` forces
358 // TX_4X4 on EVERY plane (`av1_get_tx_size`'s lossless
359 // first line), so this rect leaf's chroma is (bw/4)*(bh/4)
360 // TX_4X4 units at their own 4x4 origins -- not the single
361 // `ChromaRect8x4` rect unit the non-lossless arm below reads
362 // (`av1_get_max_uv_txsize(BLOCK_8X4)`'s rect, unreachable at
363 // lossless). Same walk the leaf's own non-intrabc lossless
364 // arm runs (lane-av1-llsub8's fix), plane-major per libaom
365 // `decode_token_recon_block`, with the intra predictor
366 // swapped for the CURRENT frame at the DV; CFL is never
367 // allowed at lossless and no alpha rides an intrabc leaf.
368 let mut out_u = Grid::Own(vec![0i32; bw * bh]);
369 let mut out_v = Grid::Own(vec![0i32; bw * bh]);
370 for plane_idx in [1usize, 2] {
371 for ur in 0..bh / 4usize {
372 for uc in 0..bw / 4usize {
373 let (umi_r, umi_c) = (lmi.0 + ur, lmi.1 + uc);
374 let (upx, upy) = (px + uc * 4, py + ur * 4);
375 let mut pb = vec![0u16; 16];
376 let buf: &PlaneBuf<'static> = if plane_idx == 1 { &*u } else { &*v };
377 mc::predict_with_filter(
378 &buf.data,
379 buf.width,
380 buf.true_width,
381 buf.true_height,
382 mv_to_q4(upx, dv.1, 0),
383 mv_to_q4(upy, dv.0, 0),
384 4,
385 4,
386 mc::InterpFilterKind::Bilinear,
387 &mut pb,
388 fctx,
389 );
390 set_palette_pred(pb, fctx);
391 let unit_around = neighbours.around_mi_rect((umi_r, umi_c), 4, 4);
392 // lane-av1loss444kf: libaom asks
393 // `has_top_right`/`has_bottom_left` with the
394 // LEAF's bsize, this unit's `(col_off, row_off)`
395 // and `tx_size` -- not as a standalone BLOCK_4X4
396 // at the unit's own position. A 4x8 leaf's FIRST
397 // unit has its bottom-left pixels inside the
398 // block's own second row (already reconstructed),
399 // which `has_bottom_left`'s
400 // `row_off + count < plane_bh_unit` early-out
401 // answers TRUE; the 4x4 table answered false, so
402 // `PlaneBuf::edges` replicated the last left
403 // sample and the unit predicted from a fabricated
404 // edge (the 4:4:4 lossless key frame's 49-sample
405 // chroma damage at (196,200)-(204,200)). The
406 // 4:2:2 twin of this arm was fixed the same way
407 // (lane-av1-422kf2); this is its 4:4:4 twin.
408 let unit_reach = Reach::of_tu(bw, bh, uc * 4, ur * 4, 4, 4, reach);
409 if !square
410 && unit_reach != Reach::of(4, upx, upy, y.width, y.height, fctx)
411 {
412 hit!(SUB8_CHROMA444_LOSSLESS_TU_REACH_HITS);
413 }
414 let grid = read_plane(
415 dec,
416 cdfs,
417 TxbSet::Chroma4,
418 scan4,
419 plane_idx,
420 unit_around[plane_idx],
421 DC_PRED,
422 DC_PRED,
423 0,
424 unit_reach,
425 y,
426 u,
427 v,
428 upx,
429 upy,
430 4,
431 TX4,
432 base_q_idx,
433 None,
434 None,
435 Some(3),
436 smooth_neighbor_uv,
437 fctx,
438 )?;
439 let cell = grid_unit_state(&grid, 4, 0, 0, 4, 4);
440 if umi_r < neighbours.mi_rows {
441 neighbours.left[umi_r][plane_idx] = cell;
442 }
443 if umi_c < neighbours.mi_cols {
444 neighbours.above[umi_c][plane_idx] = cell;
445 }
446 let out = if plane_idx == 1 {
447 &mut out_u
448 } else {
449 &mut out_v
450 };
451 if let Grid::Own(v) = out {
452 for r in 0..4usize {
453 v[(ur * 4 + r) * bw + uc * 4..][..4]
454 .copy_from_slice(&grid[r * 4..][..4]);
455 }
456 }
457 }
458 }
459 }
460 (out_u, out_v, 4, 4)
461 } else {
462 let scan: &[u16] = if bw > bh { &SCAN_8X4 } else { &SCAN_4X8 };
463 let default_tx = fctx
464 .intrabc_chroma_tx
465 .with(std::cell::Cell::get)
466 .unwrap_or(TxType::DctDct);
467 let mut out = (Grid::Zero(bw * bh), Grid::Zero(bw * bh), bw, bh);
468 for (plane_idx, buf) in [(1usize, ub), (2usize, vb)] {
469 let skip_ctx =
470 usize::from(around[plane_idx].0) + usize::from(around[plane_idx].1);
471 let mut coding = cdfs.txb(TxbSet::ChromaRect8x4, DC_PRED);
472 let (levels, tx_type) = read_coeffs_rect(
473 dec,
474 &mut coding,
475 scan,
476 bw,
477 bh,
478 skip_ctx,
479 dc_sign_ctx(around[plane_idx].2),
480 default_tx,
481 plane_idx,
482 )?;
483 let residual = dequant_and_inverse_typed_wh(
484 &levels,
485 bw,
486 bh,
487 crate::decode::bit_depth(fctx),
488 block_q_idx(fctx),
489 plane_q_delta(plane_idx, fctx).0,
490 plane_q_delta(plane_idx, fctx).1,
491 tx_type,
492 block_iqmatrix(fctx, plane_idx, bw, bh, tx_type),
493 );
494 set_palette_pred(buf, fctx);
495 push_intra_rect(
496 y,
497 u,
498 v,
499 plane_idx,
500 px,
501 py,
502 bw,
503 bh,
504 DC_PRED,
505 0,
506 reach,
507 &residual,
508 None,
509 None,
510 smooth_neighbor_uv,
511 fctx,
512 );
513 if plane_idx == 1 {
514 out.0 = levels;
515 } else {
516 out.1 = levels;
517 }
518 }
519 out
520 }
521 };
522 fctx.intrabc_chroma_tx.with(|c| c.set(None));
523 pair
524 } else {
525 let ac = alpha.map(|_| cfl_src_rect(px, py, bw, bh));
526 let around = neighbours.around_mi_rect(lmi, bw, bh);
527 if square {
528 let ug = read_plane(
529 dec,
530 cdfs,
531 TxbSet::Chroma4,
532 scan4,
533 1,
534 around[1],
535 uv_mode,
536 uv_predict_mode,
537 0,
538 reach,
539 y,
540 u,
541 v,
542 px,
543 py,
544 4,
545 TX4,
546 base_q_idx,
547 alpha.zip(ac).map(|((au, _), ac)| (au, ac)),
548 None,
549 None,
550 smooth_neighbor_uv,
551 fctx,
552 )?;
553 let vg = read_plane(
554 dec,
555 cdfs,
556 TxbSet::Chroma4,
557 scan4,
558 2,
559 around[2],
560 uv_mode,
561 uv_predict_mode,
562 0,
563 reach,
564 y,
565 u,
566 v,
567 px,
568 py,
569 4,
570 TX4,
571 base_q_idx,
572 alpha.zip(ac).map(|((_, av), ac)| (av, ac)),
573 None,
574 None,
575 smooth_neighbor_uv,
576 fctx,
577 )?;
578 (ug, vg, 4, 4)
579 } else if lossless(fctx) {
580 // lane-av1-llsub8: `read_tx_size` answers TX_4X4 for EVERY plane of
581 // a lossless block, so this rect leaf's chroma is (bw/4)*(bh/4)
582 // TX_4X4 units at their own 4x4 origins -- not the single
583 // TX_8X4/TX_4X8 rect unit the non-lossless arm below reads (that
584 // one is `av1_get_max_uv_txsize(BLOCK_8X4)`'s rect transform, which
585 // lossless never reaches). Measured on `ll444.obu`'s HORZ leaf at
586 // mi (8,16): the oracle codes U units at bc 0,1 with
587 // `get_txb_ctx`'s +10 rows (`txb_skip_cdf[0][10..12]`,
588 // plane pels 32 > tx pels 16) where the rect-unit read consumed
589 // `txb_skip_cdf[1][0]` -- the tile desynced at that U unit's
590 // all_zero symbol. Walk is plane-major like libaom
591 // `decode_token_recon_block`'s plane/row/col loop; each unit adds
592 // 3 to the chroma context (Chroma4's `big` rows, the same fold
593 // [`read_plane`]'s 128x128 caller uses); CFL is never allowed at
594 // lossless, so no alpha rides along.
595 let mut out_u = Grid::Own(vec![0i32; bw * bh]);
596 let mut out_v = Grid::Own(vec![0i32; bw * bh]);
597 for plane_idx in [1usize, 2] {
598 for ur in 0..bh / 4usize {
599 for uc in 0..bw / 4usize {
600 let (umi_r, umi_c) = (lmi.0 + ur, lmi.1 + uc);
601 let (upx, upy) = (px + uc * 4, py + ur * 4);
602 let unit_around = neighbours.around_mi_rect((umi_r, umi_c), 4, 4);
603 // lane-av1loss444kf: the block-relative answer, not
604 // a standalone BLOCK_4X4 lookup at the unit's own
605 // position -- see the intrabc twin above for the
606 // libaom citation and the measured damage.
607 let unit_reach = Reach::of_tu(bw, bh, uc * 4, ur * 4, 4, 4, reach);
608 if !square && unit_reach != Reach::of(4, upx, upy, y.width, y.height, fctx)
609 {
610 hit!(SUB8_CHROMA444_LOSSLESS_TU_REACH_HITS);
611 }
612 let grid = read_plane(
613 dec,
614 cdfs,
615 TxbSet::Chroma4,
616 scan4,
617 plane_idx,
618 unit_around[plane_idx],
619 uv_mode,
620 uv_predict_mode,
621 0,
622 unit_reach,
623 y,
624 u,
625 v,
626 upx,
627 upy,
628 4,
629 TX4,
630 base_q_idx,
631 None,
632 None,
633 Some(3),
634 smooth_neighbor_uv,
635 fctx,
636 )?;
637 let cell = grid_unit_state(&grid, 4, 0, 0, 4, 4);
638 if umi_r < neighbours.mi_rows {
639 neighbours.left[umi_r][plane_idx] = cell;
640 }
641 if umi_c < neighbours.mi_cols {
642 neighbours.above[umi_c][plane_idx] = cell;
643 }
644 let out = if plane_idx == 1 {
645 &mut out_u
646 } else {
647 &mut out_v
648 };
649 if let Grid::Own(v) = out {
650 for r in 0..4usize {
651 v[(ur * 4 + r) * bw + uc * 4..][..4]
652 .copy_from_slice(&grid[r * 4..][..4]);
653 }
654 }
655 }
656 }
657 }
658 (out_u, out_v, 4, 4)
659 } else {
660 // `av1_get_max_uv_txsize(BLOCK_4X8/8X4)` at ss 0/0 is the rect
661 // transform itself; its `tx_type` is mode-derived with the
662 // <32 set (`decode_rect_split`'s own rule for this shape).
663 let scan: &[u16] = if bw > bh { &SCAN_8X4 } else { &SCAN_4X8 };
664 let default_tx = default_intra_tx_type(uv_predict_mode as u8);
665 let mut out = (Grid::Zero(bw * bh), Grid::Zero(bw * bh), bw, bh);
666 // Index loop mirrors the spec's per-position form over several offsets; an iterator would obscure it.
667 #[allow(clippy::needless_range_loop)]
668 for plane_idx in 1..=2usize {
669 let skip_ctx = usize::from(around[plane_idx].0) + usize::from(around[plane_idx].1);
670 let mut coding = cdfs.txb(TxbSet::ChromaRect8x4, uv_predict_mode);
671 let (levels, tx_type) = read_coeffs_rect(
672 dec,
673 &mut coding,
674 scan,
675 bw,
676 bh,
677 skip_ctx,
678 dc_sign_ctx(around[plane_idx].2),
679 default_tx,
680 plane_idx,
681 )?;
682 let residual = dequant_and_inverse_typed_wh(
683 &levels,
684 bw,
685 bh,
686 crate::decode::bit_depth(fctx),
687 block_q_idx(fctx),
688 plane_q_delta(plane_idx, fctx).0,
689 plane_q_delta(plane_idx, fctx).1,
690 tx_type,
691 block_iqmatrix(fctx, plane_idx, bw, bh, tx_type),
692 );
693 let cfl = alpha
694 .zip(ac)
695 .map(|((au, av), ac)| (if plane_idx == 1 { au } else { av }, ac));
696 push_intra_rect(
697 y,
698 u,
699 v,
700 plane_idx,
701 px,
702 py,
703 bw,
704 bh,
705 uv_predict_mode,
706 0,
707 reach,
708 &residual,
709 cfl,
710 None,
711 smooth_neighbor_uv,
712 fctx,
713 );
714 if plane_idx == 1 {
715 out.0 = levels;
716 } else {
717 out.1 = levels;
718 }
719 }
720 out
721 }
722 };
723 // libaom `av1_set_entropy_contexts` stamps above/left PER TRANSFORM UNIT:
724 // the leaf's own chroma span, not the 8x8 group's. A rect leaf's lossless
725 // units (4x4) each carry their own state -- a 4:2:0-style whole-leaf smear
726 // gave one unit's state to its neighbour's cells (lane-av1-llsub8).
727 let (u_grid, v_grid, unit_w, unit_h) = grids;
728 // One unit covers `unit_w/MI` mi columns and `unit_h/MI` mi rows (a 4x4
729 // unit: one cell each; the non-lossless rect unit: the whole leaf).
730 let (uw_cells, uh_cells) = ((unit_w / MI).max(1), (unit_h / MI).max(1));
731 for ur in 0..h_mi / uh_cells {
732 for uc in 0..w_mi / uw_cells {
733 let (x0, y0) = (uc * unit_w, ur * unit_h);
734 let us = grid_unit_state(&u_grid, bw, x0, y0, unit_w, unit_h);
735 let vs = grid_unit_state(&v_grid, bw, x0, y0, unit_w, unit_h);
736 for cell in 0..uh_cells {
737 if lmi.0 + ur * uh_cells + cell < neighbours.mi_rows {
738 neighbours.left[lmi.0 + ur * uh_cells + cell][1] = us;
739 neighbours.left[lmi.0 + ur * uh_cells + cell][2] = vs;
740 }
741 }
742 for cell in 0..uw_cells {
743 if lmi.1 + uc * uw_cells + cell < neighbours.mi_cols {
744 neighbours.above[lmi.1 + uc * uw_cells + cell][1] = us;
745 neighbours.above[lmi.1 + uc * uw_cells + cell][2] = vs;
746 }
747 }
748 }
749 }
750 Ok((u_grid, v_grid, unit_w, unit_h))
751}
752
753/// [`neighbour_state`] over one `(unit_w x unit_h)` transform unit at offset
754/// `(x0, y0)` inside a `bw`-wide assembled chroma grid -- the per-unit stamp
755/// the rect arms need when a leaf carries more than one unit.
756fn grid_unit_state(
757 grid: &Grid,
758 bw: usize,
759 x0: usize,
760 y0: usize,
761 unit_w: usize,
762 unit_h: usize,
763) -> Neighbour {
764 let mut cul = 0u32;
765 let mut dc = None;
766 for r in 0..unit_h {
767 for c in 0..unit_w {
768 let v = grid[(y0 + r) * bw + x0 + c];
769 cul += v.unsigned_abs();
770 if r == 0 && c == 0 && v != 0 {
771 dc = Some(v < 0);
772 }
773 }
774 }
775 if cul == 0 {
776 return Neighbour::default();
777 }
778 Neighbour {
779 level: cul.min(7) as u8,
780 dc,
781 }
782}
783
784/// [`sub8_leaf_chroma444`] for 4:2:2 (lane-av1-422g): an even-mi-column 4x4
785/// leaf is its plane's own chroma reference (`is_chroma_reference`'s
786/// `!(mi_col & ss_x)` at ss_x 1 -- every even-column leaf, not 4:2:0's single
787/// bottom-right one) and codes ONE TX_4X4 chroma unit per plane right after
788/// its luma: `ss_size_lookup[BLOCK_4X4][1][0]` is BLOCK_4X4, so the (2, 4)
789/// plane area rounds up to the square 4x4 read. The unit sits at the chroma
790/// position (`px >> ss_x`), and its 4 chroma-px width spans TWO luma mi
791/// columns, so the context reads go through
792/// [`Neighbours::around_mi_422_chroma`] and the entropy stamp covers both
793/// columns with the unit's whole-unit state.
794#[allow(clippy::too_many_arguments)]
795pub(super) fn sub8_leaf_chroma422(
796 dec: &mut SymbolDecoder,
797 cdfs: &mut Cdfs,
798 neighbours: &mut Neighbours,
799 lmi: (usize, usize),
800 uv_mode: usize,
801 angle_delta_uv: i32,
802 alpha: Option<(i32, i32)>,
803 skip: bool,
804 intrabc_dv: Option<(i32, i32)>,
805 y: &mut PlaneBuf<'static>,
806 u: &mut PlaneBuf<'static>,
807 v: &mut PlaneBuf<'static>,
808 scan4: &[u16],
809 base_q_idx: u8,
810 smooth_neighbor_uv: bool,
811 // The owning luma leaf's own shape (`(4, 4)` for a split leaf, `(8, 4)` /
812 // `(4, 8)` for a rect pair's strip): libaom's `has_top_right` /
813 // `has_bottom_left` answer for this chroma TX_4X4 unit comes from the
814 // LEAF's own rect table row at the leaf's mi (`bsize` is the leaf, the
815 // unit covers it whole), never the standalone square-4x4 table the old
816 // `Reach::of(4, ..)` lookup used -- that said 1 where `has_tr_8x4` says 0
817 // at mi(27,44) (the oracle's own EC_PRED prints `n_tr=0` there), so the
818 // D45 unit read undecoded pixels to its upper right.
819 leaf_shape: (usize, usize),
820 fctx: &crate::decode::FrameCtx,
821) -> Result<(Grid, Grid)> {
822 let uv_predict_mode = if uv_mode == UV_CFL_PRED {
823 DC_PRED
824 } else {
825 uv_mode
826 };
827 // lane-av1-422kf2: libaom writes one MB_MODE_INFO per mi cell the LEAF
828 // covers, and `chroma_left_mbmi`/`chroma_above_mbmi` read whichever of
829 // those cells the next block's snap lands on -- an 8x4 strip's SM_V
830 // uv_mode must therefore be visible at BOTH of its mi columns, or the
831 // block to its right (mi (30,50): left neighbour cell (30,49)) missed the
832 // smooth neighbour and skipped libaom's strength-1 intra edge filter
833 // (class context-read-from-one-cell, the 4:2:2 sub-8 twin of the
834 // lane-kf900 snap fix).
835 neighbours.record_uv_mode_mi(
836 lmi.0,
837 lmi.1,
838 (leaf_shape.0 / MI).max(1),
839 (leaf_shape.1 / MI).max(1),
840 uv_predict_mode,
841 );
842 let (px, py) = (lmi.1 * MI, lmi.0 * MI);
843 // lane-av1-422kf2: the unit's WRITE/PREDICTION anchor is the same floored
844 // chroma cell as the entropy anchor below -- libaom positions the sub-8
845 // chroma plane block at `av1_setup_dst_planes`' mi-exact floor
846 // (`(mi_col & !1) * MI >> ss_x`), NOT at the odd leaf's own chroma x:
847 // the oracle's mi(18,55) TX_4X4 unit reconstructed chroma px 108..111
848 // (a pure V-copy of the row above at 108..111), while the leaf's own
849 // chroma x is 110 -- anchoring there left px 108..109 unwritten (zeros)
850 // and stepped px 112..113 with the unit's right columns.
851 let (cpx, cpy) = (((lmi.1 & !1) * MI) >> ss_x(fctx), py >> ss_y(fctx));
852 // lane-av1-422h: the entropy-context anchor is the unit's FLOOR chroma
853 // cell (`pd->above_entropy_context + (mi_col >> ss_x)`): an ODD-column
854 // leaf's 4-px unit starts at a chroma px that is NOT cell aligned, and
855 // both its ctx read and its ctx write live in the cell containing that
856 // start -- one luma mi column to the LEFT of the leaf's own. Even-column
857 // (cell-aligned) leaves keep their own column. The state write spans the
858 // anchor's two luma mi columns, so the second (odd) leaf of a split group
859 // overwrites exactly the cell the first one wrote, as libaom's
860 // `av1_set_entropy_contexts` per-TU writes do.
861 let ctx_mi = (lmi.0, lmi.1 & !1);
862 // lane-av1-422kf2: the square unit takes the leaf's OWN reach, but at the
863 // CHROMA plane's granularity, not the luma leaf's. libaom passes the leaf
864 // through `scale_chroma_bsize(bsize, ss_x, ss_y)` first
865 // (`reconintra.c:1823`, definition `:1649`) and only then calls
866 // `has_top_right`/`has_bottom_left` with it, so a rect strip's answer
867 // comes from `has_tr_8x4`/`has_tr_4x8` at the strip's mi, not from a
868 // 4x4-block lookup. At 4:2:2 (`ss (1,0)`) that remap is NOT the identity:
869 // a BLOCK_4X4 luma leaf is answered as BLOCK_8X4, TWO mi wide and one mi
870 // tall.
871 //
872 // MEASURED, `probe/ll422_allintra.obu` (320x240, 4:2:2 lossless
873 // all-intra) against the instrumented `aomdec`: the FIRST wrong chroma
874 // prediction in decode order is unit 2818, plane U, chroma (96,32) --
875 // the leaf at mi(8,49). The oracle's own `EC_PRED` prints `plane=1
876 // row_off=0 col_off=0 txw=4 txh=4 mode=7 p_angle=203 have_top=1
877 // have_left=1 n_top=4 n_left=4 n_tr=-1 n_bl=4 bsize=2`, and `bsize=2`
878 // is BLOCK_8X4 -- the remap, not the BLOCK_4X4 leaf libaom decoded. That
879 // `n_bl=4` is the "leftmost column of superblock" arm: `blk_col_in_sb =
880 // (mi_col & (sb_mi-1)) >> bw_in_mi_log2 = (49 & 15) >> 1 = 0`
881 // (`reconintra.c:421`), where the one-mi-wide luma BLOCK_4X4 reads column
882 // 1, and `blk_start_row_off + row_off + 1 < sb_height_unit` is
883 // `8 + 0 + 1 < 16 >> 0` (`reconintra.c:429-435`). Squared on the luma
884 // leaf, `Reach::of(4, ..)` instead read `has_bl_4x4[byte 32] bit 1`
885 // (`84 = 0b0101_0100`, bit 1 clear) and answered FALSE, so
886 // `PlaneBuf::edges` fed the predictor a 4-sample left column where
887 // libaom fed 8. `build_directional_and_filter_intra_predictors`
888 // (`reconintra.c:1150-1158`) copies `n_left_px` into `left_col[0..3]` and
889 // only `if (n_bottomleft_px > 0)` the next `n_bottomleft_px`, so with the
890 // denial `av1_dr_prediction_z3_c` reads its base term off a replicated
891 // tail -- interior-only, with row0/col0 and the coefficients untouched.
892 // 4:2:0 and 4:4:4 never reach this arm: the reader is 4:2:2-only (its
893 // sibling `sub8_leaf_chroma444` owns those), and the remap is the
894 // identity for a leaf of 8 or more at every subsampling.
895 let (mi_bw, mi_bh) =
896 crate::encode::chroma_mi_shape(leaf_shape.0, leaf_shape.1, ss_x(fctx), ss_y(fctx));
897 // libaom's two edge tests are TILE bounds over the TRANSFORM's plane
898 // extent and its `have_top`/`have_left` flags are `y0 > 0` / `x0 > 0`
899 // (`reconintra.c:1804-1806`, `:1815-1817`); the tile end is the frame end
900 // for every leaf this reader owns, and the unit is one TX_4X4 chroma
901 // transform wide and tall (`4 << ss` luma px per axis).
902 // lane-av1unwritten: at ss (1,0) a BLOCK_4X8 chroma reference's plane
903 // block is 4x8 -- `get_plane_block_size(BLOCK_4X8, 1, 0)` is BLOCK_4X8
904 // (`common_data.c`'s `av1_ss_size_lookup[BLOCK_4X8][1][0]` is
905 // BLOCK_INVALID, but `blockd.h:1372` takes the per-axis MIN of the two
906 // lookups) -- and `av1_get_max_uv_txsize` (blockd.h:1372-1379) resolves
907 // it through `max_txsize_rect_lookup[BLOCK_4X8]` == TX_4X8
908 // (`common_data.h:126`). So away from lossless the WHOLE 4x8 plane block
909 // is ONE rect transform unit, exactly the one [`leaf8_chroma422_unit`]
910 // already reads for a BLOCK_8X8 leaf. Emitting the single square TX_4X4
911 // this arm used to wrote left the block's bottom four chroma rows
912 // UNWRITTEN: on the pinned `440_request_is_422.obu` (a 4:2:2 header over
913 // a 4:2:0 tile, whose legal 8x8 `PARTITION_HORZ` groups make this shape
914 // legal syntax) the sentinel census found 112 unwritten samples per
915 // chroma plane, one 4x4 block per group, and the decode's output hash
916 // CHANGED RUN TO RUN because the unwritten samples were uninitialised
917 // memory (class `writer-gap-reaches-output`).
918 //
919 // At LOSSLESS the single-unit reading is wrong the other way:
920 // `av1_get_tx_size`'s first line returns TX_4X4 on every plane
921 // (blockd.h:1383), so the 4x8 plane block is walked as TWO stacked 4x4
922 // units -- which is exactly what the two chroma-reference leaves of the
923 // `decode_leaf_split4` route below already emit, so the square arms stay
924 // for lossless.
925 let tx_w = 4 << ss_x(fctx);
926 let tx_h = 4 << ss_y(fctx);
927 let reach = Reach::of_tu_chroma(
928 mi_bw,
929 mi_bh,
930 lmi.0,
931 lmi.1,
932 crate::encode::reach_sb_mi(fctx),
933 0,
934 0,
935 tx_w,
936 tx_h,
937 ss_x(fctx),
938 ss_y(fctx),
939 // `tx_w`/`tx_h` are already in luma samples (`4 << ss`), so the frame
940 // extent is compared with them directly. Shifting them by `ss` again
941 // doubled the step at 4:2:2 (`ss_x == 1`): a 4-wide chroma unit at
942 // luma x 336 of a 352-wide frame read `336 + 16 < 352` and lost its
943 // above-right, while libaom's `right_available`
944 // (`mi_col + ((col_off + txw) << ss_x) < mi_col_end`) and the spec's
945 // BlockDecoded test both keep it (`336 + 8 < 352`).
946 py > 0 && px + tx_w < y.width,
947 px > 0 && py + tx_h < y.height,
948 );
949 let chroma_around = neighbours.around_mi_422_chroma(ctx_mi, 8, 4);
950 // lane-av1422sub8pix: the CfL AC signal spans the UNIT's luma footprint,
951 // anchored at the same FLOORED luma x the write/prediction anchor below
952 // uses -- `px` (the odd leaf's own luma x) steps half the span into the
953 // RIGHT neighbour group (t422 frame-1 leaf (1,23): the unit reconstructs
954 // chroma px 44..47 from luma 88..95, while a `px`-anchored source
955 // averaged luma 92..99 and every CFL unit of the group mismatched).
956 let ac = alpha.map(|_| cfl_src_rect((lmi.1 & !1) * MI, py, 8, 4));
957 let grids: (Grid, Grid) = if let Some(dv) = intrabc_dv.filter(|_| skip) {
958 // lane-av1422ctattr: a SKIPPED intrabc chroma leaf still predicts the
959 // frame copy at its DV. libaom's `decode_token_recon_block` takes the
960 // INTER arm for an intrabc block (`is_inter_block` counts it,
961 // `blockd.h:373`) and `predict_inter_block_visit` runs
962 // `dec_build_inter_predictor` REGARDLESS of `skip_txfm` -- only the
963 // residual read is dropped. Measured on the 4:2:2 seed of
964 // `R422_320x242.obu` / `Q_odd320x242.obu` (chroma U(116,160)):
965 // oracle EC_DP mi_row=40 mi_col=58 bsize=2 inter=1 skip_txfm=1 is_intrabc=1
966 // EC_ISTEP mi_row=40 mi_col=58 name=intrabc val=1 rng=39264
967 // EC_DV mi_row=40 mi_col=58 dv_col=0 dv_row=-1024 rng=40116
968 // ours the SAME symbol and the SAME DV (parse in sync), then
969 // OUR_PRED x=116 y=160 plane=1 mode=0 row0=[182,182,182,182]
970 // i.e. this `if skip` arm was taken and the `else if let Some(dv)`
971 // frame copy below never ran. 202 = (808+2)/4 is not a `dc_top`: there
972 // is NO DC variant at all on the oracle side, because
973 // `av1_predict_intra_block` is never called for an intrabc block.
974 // `sub8_leaf_chroma444` already orders its arms this way
975 // (`intrabc_dv.filter(|_| skip)` first, lane-av1ibcskip2); this arm is
976 // that one, at 4:2:2's square 4x4 chroma unit.
977 let mut ub = vec![0u16; 16];
978 mc::predict_with_filter(
979 &u.data,
980 u.width,
981 u.true_width,
982 u.true_height,
983 mv_to_q4(cpx, dv.1, ss_x(fctx)),
984 mv_to_q4(cpy, dv.0, ss_y(fctx)),
985 4,
986 4,
987 mc::InterpFilterKind::Bilinear,
988 &mut ub,
989 fctx,
990 );
991 hit!(SKIPPED_INTRABC_DV_COPY_HITS);
992 let zeros = &ZERO_RESIDUAL[..16];
993 set_palette_pred(ub, fctx);
994 push_intra(
995 y,
996 u,
997 v,
998 1,
999 cpx,
1000 cpy,
1001 4,
1002 DC_PRED,
1003 0,
1004 reach,
1005 zeros,
1006 None,
1007 None,
1008 smooth_neighbor_uv,
1009 fctx,
1010 );
1011 let mut vb = vec![0u16; 16];
1012 mc::predict_with_filter(
1013 &v.data,
1014 v.width,
1015 v.true_width,
1016 v.true_height,
1017 mv_to_q4(cpx, dv.1, ss_x(fctx)),
1018 mv_to_q4(cpy, dv.0, ss_y(fctx)),
1019 4,
1020 4,
1021 mc::InterpFilterKind::Bilinear,
1022 &mut vb,
1023 fctx,
1024 );
1025 set_palette_pred(vb, fctx);
1026 push_intra(
1027 y,
1028 u,
1029 v,
1030 2,
1031 cpx,
1032 cpy,
1033 4,
1034 DC_PRED,
1035 0,
1036 reach,
1037 zeros,
1038 None,
1039 None,
1040 smooth_neighbor_uv,
1041 fctx,
1042 );
1043 // The same arm-without-clear route the 4:2:0 tails and
1044 // [`sub8_leaf_chroma444`] count: a skipped leaf reads no coefficient,
1045 // so the inherited chroma `tx_type` is dropped unconsumed.
1046 if fctx.intrabc_chroma_tx.with(std::cell::Cell::get).is_some() {
1047 hit!(SKIPPED_INTRABC_CHROMA_ARM_HITS);
1048 fctx.intrabc_chroma_tx.with(|c| c.set(None));
1049 }
1050 (Grid::Zero(16), Grid::Zero(16))
1051 } else if skip {
1052 // A skipped chroma-reference leaf still predicts (the CfL contribution
1053 // survives a skip); no coefficient symbol exists.
1054 if alpha.is_some() {
1055 hit!(CHROMA_SKIP_CFL_HITS);
1056 hit!(SKIP_CFL_HITS);
1057 }
1058 push_intra(
1059 y,
1060 u,
1061 v,
1062 1,
1063 cpx,
1064 cpy,
1065 4,
1066 uv_predict_mode,
1067 angle_delta_uv,
1068 reach,
1069 &ZERO_RESIDUAL[..16],
1070 alpha.zip(ac).map(|((au, _), ac)| (au, ac)),
1071 None,
1072 smooth_neighbor_uv,
1073 fctx,
1074 );
1075 push_intra(
1076 y,
1077 u,
1078 v,
1079 2,
1080 cpx,
1081 cpy,
1082 4,
1083 uv_predict_mode,
1084 angle_delta_uv,
1085 reach,
1086 &ZERO_RESIDUAL[..16],
1087 alpha.zip(ac).map(|((_, av), ac)| (av, ac)),
1088 None,
1089 smooth_neighbor_uv,
1090 fctx,
1091 );
1092 // The same arm-without-clear route the 4:2:0 tails count: a skipped
1093 // leaf reads no coefficient, so an armed intrabc slot is dropped.
1094 if fctx.intrabc_chroma_tx.with(std::cell::Cell::get).is_some() {
1095 hit!(SKIPPED_INTRABC_CHROMA_ARM_HITS);
1096 fctx.intrabc_chroma_tx.with(|c| c.set(None));
1097 }
1098 (Grid::Zero(16), Grid::Zero(16))
1099 } else if let Some(dv) = intrabc_dv {
1100 hit!(CHROMA422_SUB8_HITS);
1101 // Frame copy at the DV; the residual still reads the ordinary chroma
1102 // tables with the inherited luma `tx_type` (see the 4:2:0 tails).
1103 let mut ub = vec![0u16; 16];
1104 mc::predict_with_filter(
1105 &u.data,
1106 u.width,
1107 u.true_width,
1108 u.true_height,
1109 mv_to_q4(cpx, dv.1, ss_x(fctx)),
1110 mv_to_q4(cpy, dv.0, ss_y(fctx)),
1111 4,
1112 4,
1113 mc::InterpFilterKind::Bilinear,
1114 &mut ub,
1115 fctx,
1116 );
1117 let mut vb = vec![0u16; 16];
1118 mc::predict_with_filter(
1119 &v.data,
1120 v.width,
1121 v.true_width,
1122 v.true_height,
1123 mv_to_q4(cpx, dv.1, ss_x(fctx)),
1124 mv_to_q4(cpy, dv.0, ss_y(fctx)),
1125 4,
1126 4,
1127 mc::InterpFilterKind::Bilinear,
1128 &mut vb,
1129 fctx,
1130 );
1131 set_palette_pred(ub, fctx);
1132 let ug = read_plane(
1133 dec,
1134 cdfs,
1135 TxbSet::Chroma4,
1136 scan4,
1137 1,
1138 chroma_around[1],
1139 DC_PRED,
1140 DC_PRED,
1141 0,
1142 reach,
1143 y,
1144 u,
1145 v,
1146 cpx,
1147 cpy,
1148 4,
1149 TX4,
1150 base_q_idx,
1151 None,
1152 None,
1153 None,
1154 smooth_neighbor_uv,
1155 fctx,
1156 )?;
1157 set_palette_pred(vb, fctx);
1158 let vg = read_plane(
1159 dec,
1160 cdfs,
1161 TxbSet::Chroma4,
1162 scan4,
1163 2,
1164 chroma_around[2],
1165 DC_PRED,
1166 DC_PRED,
1167 0,
1168 reach,
1169 y,
1170 u,
1171 v,
1172 cpx,
1173 cpy,
1174 4,
1175 TX4,
1176 base_q_idx,
1177 None,
1178 None,
1179 None,
1180 smooth_neighbor_uv,
1181 fctx,
1182 )?;
1183 fctx.intrabc_chroma_tx.with(|c| c.set(None));
1184 (ug, vg)
1185 } else {
1186 hit!(CHROMA422_SUB8_HITS);
1187 let ug = read_plane(
1188 dec,
1189 cdfs,
1190 TxbSet::Chroma4,
1191 scan4,
1192 1,
1193 chroma_around[1],
1194 uv_mode,
1195 uv_predict_mode,
1196 angle_delta_uv,
1197 reach,
1198 y,
1199 u,
1200 v,
1201 cpx,
1202 cpy,
1203 4,
1204 TX4,
1205 base_q_idx,
1206 alpha.zip(ac).map(|((au, _), ac)| (au, ac)),
1207 None,
1208 None,
1209 smooth_neighbor_uv,
1210 fctx,
1211 )?;
1212 let vg = read_plane(
1213 dec,
1214 cdfs,
1215 TxbSet::Chroma4,
1216 scan4,
1217 2,
1218 chroma_around[2],
1219 uv_mode,
1220 uv_predict_mode,
1221 angle_delta_uv,
1222 reach,
1223 y,
1224 u,
1225 v,
1226 cpx,
1227 cpy,
1228 4,
1229 TX4,
1230 base_q_idx,
1231 alpha.zip(ac).map(|((_, av), ac)| (av, ac)),
1232 None,
1233 None,
1234 smooth_neighbor_uv,
1235 fctx,
1236 )?;
1237 (ug, vg)
1238 };
1239 // libaom `av1_set_entropy_contexts` stamps above/left PER TRANSFORM UNIT:
1240 // the unit's own chroma span -- 1 mi row tall, TWO luma mi columns wide
1241 // (one chroma column) -- not the 8x8 group's.
1242 let (u_grid, v_grid) = grids;
1243 let u_state = neighbour_state(&u_grid);
1244 let v_state = neighbour_state(&v_grid);
1245 if lmi.0 < neighbours.mi_rows {
1246 neighbours.left[ctx_mi.0][1] = u_state;
1247 neighbours.left[ctx_mi.0][2] = v_state;
1248 }
1249 for cell in 0..2 {
1250 if ctx_mi.1 + cell < neighbours.mi_cols {
1251 neighbours.above[ctx_mi.1 + cell][1] = u_state;
1252 neighbours.above[ctx_mi.1 + cell][2] = v_state;
1253 }
1254 }
1255 Ok((u_grid, v_grid))
1256}
1257
1258/// Decodes a `PARTITION_SPLIT` of one 8x8 block into four `BLOCK_4X4` leaves
1259/// (spec `decode_partition`'s recursion bottom), sharing [`decode_leaf8`]'s
1260/// own signature so its caller's `prev_leaf`/`above_mode`/`left_mode`
1261/// bookkeeping needs no change. Each leaf reads its own luma mode info,
1262/// neighboured off the *previously decoded leaf in this same group* on
1263/// whichever axis is adjacent (mirroring [`decode_leaf8`]'s own `prev_leaf`
1264/// pattern, one level deeper); chroma is read and reconstructed exactly
1265/// once, on the last (bottom-right) leaf, at the whole 8x8 group's own 4x4
1266/// chroma unit -- [`read_intra_mode_sub8`]'s doc has the `is_chroma_reference`
1267/// derivation.
1268#[allow(clippy::too_many_arguments)]
1269pub(super) fn decode_leaf_split4(
1270 dec: &mut SymbolDecoder,
1271 cdfs: &mut Cdfs,
1272 neighbours: &mut Neighbours,
1273 outer_at: (usize, usize),
1274 leaf_mi: (usize, usize),
1275 scan4: &[u16],
1276 prev_leaf: Option<((usize, usize), usize)>,
1277 y: &mut PlaneBuf<'static>,
1278 u: &mut PlaneBuf<'static>,
1279 v: &mut PlaneBuf<'static>,
1280 base_q_idx: u8,
1281 enable_filter_intra: bool,
1282 allow_intrabc: bool,
1283 reduced_tx_set: bool,
1284 // Returns `(below_mode, right_mode)`: the mode a neighbour BELOW this 8x8
1285 // group sees (its bottom-left 4x4, libaom reads mi(row-1, col)) and the
1286 // one a neighbour to its RIGHT sees (its top-right 4x4, mi(row, col-1))., fctx: &crate::decode::FrameCtx
1287 fctx: &crate::decode::FrameCtx,
1288) -> Result<(usize, usize)> {
1289 hit!(SUB8_SPLIT_HITS);
1290 let (r, c) = outer_at;
1291 let mut above_mode = neighbours.above_mode[c];
1292 let mut left_mode = neighbours.left_mode[r];
1293 if let Some(((pr, pc), pmode)) = prev_leaf {
1294 if pc == leaf_mi.1 && leaf_mi.0 == pr + 2 {
1295 above_mode = pmode;
1296 } else if pr == leaf_mi.0 && leaf_mi.1 == pc + 2 {
1297 left_mode = pmode;
1298 }
1299 }
1300 // See `decode_leaf8`'s own `smooth_neighbor_uv`: the chroma edge-filter
1301 // type is the chroma neighbours' `uv_mode`, mi-exact where recorded.
1302 let smooth_neighbor_uv = neighbours.smooth_uv_neighbour(leaf_mi.0, leaf_mi.1, r, c, fctx);
1303 let mut leaf_modes = [0usize; 4];
1304 let mut leaf_skips = [false; 4];
1305 let mut last_uv: Option<(usize, i32, Option<(i32, i32)>)> = None;
1306 // lane-av1txr: the chroma-reference (i==3) leaf's block vector when that
1307 // leaf is an intrabc block -- its prediction/reconstruction below is a
1308 // frame copy at this DV, not an intra one.
1309 let mut last_intrabc: Option<(i32, i32)> = None;
1310 // 4:4:4 (lane-av1-444): `is_chroma_reference` (av1_common_int.h:1459) is
1311 // unconditional at subsampling 0/0, so EVERY 4x4 leaf reads its own
1312 // `uv_mode` and codes its own full-resolution 4x4 chroma unit right
1313 // after its luma -- there is no shared group unit at all.
1314 let chroma_444 = ss_x(fctx) == 0 && ss_y(fctx) == 0;
1315 // lane-av1-422g: at ss (1,0) every even-mi-column leaf is its plane's own
1316 // chroma reference (`is_chroma_reference`'s `!(mi_col & ss_x)`) and codes
1317 // one TX_4X4 chroma unit per plane; odd-column leaves read no chroma.
1318 let chroma_422 = ss_x(fctx) == 1 && ss_y(fctx) == 0;
1319 // The last leaf's own `uv_predict_mode`, for the coarse above/left slots
1320 // the 4:2:0 tail fills from the group's single unit.
1321 let mut last_uv444 = DC_PRED;
1322 let mut last_uv422 = DC_PRED;
1323 for i in 0..4usize {
1324 let (dr, dc) = (i / 2, i % 2);
1325 let lmi = (leaf_mi.0 + dr, leaf_mi.1 + dc);
1326 // Neighbour modes at real mi granularity: inside the group the
1327 // sibling leaf, outside it the split-leaf map when the adjacent mi
1328 // was itself a 4x4 leaf, else the coarse 16x16 slot (see
1329 // `sub8_mode_col`'s doc).
1330 let leaf_above = if dr == 0 {
1331 neighbours.mode_above_mi(lmi.0, lmi.1).unwrap_or(above_mode)
1332 } else {
1333 leaf_modes[i - 2]
1334 };
1335 let leaf_left = if dc == 0 {
1336 neighbours.mode_left_mi(lmi.0, lmi.1).unwrap_or(left_mode)
1337 } else {
1338 leaf_modes[i - 1]
1339 };
1340 let has_chroma = if chroma_444 {
1341 true
1342 } else if chroma_422 {
1343 // lane-av1-422h: `is_chroma_reference`'s column clause at ss_x 1 is
1344 // `(mi_col & 1)` for a BLOCK_4X4 leaf -- the ODD-column leaf is the
1345 // chroma reference (oracle, mi(30,42) split group: uv_mode read at
1346 // mi(30,43) and mi(31,43) only). The even-column rule this
1347 // replaces came from 422f's pre-partition-trace misreading.
1348 (lmi.1 & 1) == 1
1349 } else {
1350 i == 3
1351 };
1352 let skip_ctx = neighbours.skip_txfm_ctx(lmi.0, lmi.1);
1353 let (skip, mode, angle_delta_y, chroma, filter_intra, intrabc) = read_intra_mode_sub8(
1354 dec,
1355 cdfs,
1356 leaf_above,
1357 leaf_left,
1358 has_chroma,
1359 enable_filter_intra,
1360 filter_intra_size_class(4).expect("BLOCK_4X4 has its own filter_intra row"),
1361 skip_ctx,
1362 allow_intrabc,
1363 lmi.0,
1364 lmi.1,
1365 1,
1366 1,
1367 fctx,
1368 )?;
1369 leaf_modes[i] = mode;
1370 leaf_skips[i] = skip;
1371 neighbours.record_mode_mi(lmi.0, lmi.1, 1, 1, mode);
1372 // lane-av1txr: libaom `parse_decode_block`'s `set_txfm_ctxs` for this
1373 // `BLOCK_4X4` leaf -- EVERY block publishes its transform size into
1374 // the TXFM context bands, and an intrabc block's later `txfm_split`
1375 // context is read off them (`txfm_partition_context`). A 4x4 never
1376 // signals a tx size, so it is always TX_4X4 (`set_txfm_ctxs`'s
1377 // `skip && is_inter` branch writes the block's own 4 pixels -- the
1378 // same value).
1379 set_txfm_ctxs(neighbours, lmi, 4, 1, 1, false);
1380 let smooth_neighbor = is_smooth_mode(leaf_above) || is_smooth_mode(leaf_left);
1381 if smooth_neighbor {
1382 hit!(SMOOTH_LUMA_HITS);
1383 }
1384 let (px, py) = (lmi.1 * MI, lmi.0 * MI);
1385 let reach = Reach::of(4, px, py, y.width, y.height, fctx);
1386 if let Some(dv) = intrabc {
1387 // lane-av1txr: an intrabc `BLOCK_4X4` leaf -- prediction is the
1388 // CURRENT frame at the block vector (`predict_inter_block`'s
1389 // intrabc arm, spec 7.11.3.4), residual the INTER coefficient set
1390 // (libaom reads `tx_type` off `inter_ext_tx_cdf` for
1391 // `is_inter_block`, which counts intrabc). Published into
1392 // `intrabc_dv` so the group's chroma arm (below, when the
1393 // chroma-reference leaf is the intrabc one) and
1394 // [`record_intrabc_mi`] see it.
1395 let mut yb = vec![0u16; 16];
1396 mc::predict_with_filter(
1397 &y.data,
1398 y.width,
1399 y.true_width,
1400 y.true_height,
1401 mv_to_q4(px, dv.1, 0),
1402 mv_to_q4(py, dv.0, 0),
1403 4,
1404 4,
1405 mc::InterpFilterKind::Bilinear,
1406 &mut yb,
1407 fctx,
1408 );
1409 set_palette_pred(yb.clone(), fctx);
1410 if skip {
1411 push_intra(
1412 y,
1413 u,
1414 v,
1415 0,
1416 px,
1417 py,
1418 4,
1419 DC_PRED,
1420 0,
1421 reach,
1422 &ZERO_RESIDUAL[..16],
1423 None,
1424 None,
1425 smooth_neighbor,
1426 fctx,
1427 );
1428 neighbours.record_mi_luma(lmi, 4, &ZERO_RESIDUAL[..16]);
1429 } else {
1430 let tu_around = neighbours.around_mi(lmi, 4)[0];
1431 let tu_grid = read_plane(
1432 dec,
1433 cdfs,
1434 if reduced_tx_set {
1435 TxbSet::Luma4Inter
1436 } else {
1437 TxbSet::Luma4InterSet1
1438 },
1439 scan4,
1440 0,
1441 tu_around,
1442 DC_PRED,
1443 DC_PRED,
1444 0,
1445 reach,
1446 y,
1447 u,
1448 v,
1449 px,
1450 py,
1451 4,
1452 4,
1453 base_q_idx,
1454 None,
1455 None,
1456 None,
1457 smooth_neighbor,
1458 fctx,
1459 )?;
1460 neighbours.record_mi_luma(lmi, 4, &tu_grid);
1461 }
1462 neighbours.fill_skip_grid(lmi, 1, skip);
1463 neighbours.fill_lf_grid(lmi, 1, 4, 0, fctx);
1464 record_intrabc_mi(lmi.0, lmi.1, 1, Some(dv), neighbours, fctx);
1465 if has_chroma {
1466 // An intrabc block is an INTER block for `av1_get_tx_type`, so
1467 // the chroma inherits the LUMA's coded transform type
1468 // (`xd->tx_type_map` at the co-located luma position) -- the
1469 // luma `read_plane` above just set [`LUMA_TX_TYPE`]. Armed for
1470 // exactly this leaf's two chroma reads.
1471 fctx.intrabc_chroma_tx
1472 .with(|c| c.set(Some(fctx.luma_tx_type.with(std::cell::Cell::get))));
1473 if chroma_444 {
1474 // lane-av1-444: this leaf codes its OWN 4x4 chroma unit
1475 // right here (frame copy at the DV); no group tail.
1476 sub8_leaf_chroma444(
1477 dec,
1478 cdfs,
1479 neighbours,
1480 lmi,
1481 4,
1482 4,
1483 DC_PRED,
1484 None,
1485 skip,
1486 Some(dv),
1487 y,
1488 u,
1489 v,
1490 scan4,
1491 base_q_idx,
1492 neighbours.smooth_uv_neighbour(lmi.0, lmi.1, r, c, fctx),
1493 fctx,
1494 )?;
1495 last_uv444 = DC_PRED;
1496 } else if chroma_422 {
1497 sub8_leaf_chroma422(
1498 dec,
1499 cdfs,
1500 neighbours,
1501 lmi,
1502 DC_PRED,
1503 0,
1504 None,
1505 skip,
1506 Some(dv),
1507 y,
1508 u,
1509 v,
1510 scan4,
1511 base_q_idx,
1512 neighbours.smooth_uv_neighbour(lmi.0, lmi.1, r, c, fctx),
1513 (4, 4),
1514 fctx,
1515 )?;
1516 last_uv422 = DC_PRED;
1517 } else {
1518 last_uv = Some((DC_PRED, 0, None));
1519 // lane-av1txbands: the group's chroma is the CHROMA-REFERENCE
1520 // leaf's own block -- libaom codes one chroma unit under the
1521 // last sub-block only (`is_chroma_reference`), so its prediction
1522 // is that leaf's `uv_mode` (CfL alpha and all) unless THAT leaf
1523 // is the intrabc one. Arming this from a non-chroma-reference
1524 // leaf's DV made `warped.obu`'s 8x8 group at mi(40,62) copy
1525 // frame bytes at mi(41,62)'s vector where aomdec predicts
1526 // `uv_mode=UV_CFL_PRED`: luma stayed byte-exact and only chroma
1527 // moved (class chroma-keyed-at-a-non-chroma-leaf). Mirrors
1528 // [`decode_leaf_rect8`]'s own placement.
1529 last_intrabc = Some(dv);
1530 }
1531 }
1532 continue;
1533 }
1534 if skip {
1535 push_intra(
1536 y,
1537 u,
1538 v,
1539 0,
1540 px,
1541 py,
1542 4,
1543 mode,
1544 angle_delta_y,
1545 reach,
1546 &ZERO_RESIDUAL[..16],
1547 None,
1548 filter_intra,
1549 smooth_neighbor,
1550 fctx,
1551 );
1552 neighbours.record_mi_luma(lmi, 4, &ZERO_RESIDUAL[..16]);
1553 } else {
1554 let tu_around = neighbours.around_mi(lmi, 4)[0];
1555 // A `BLOCK_4X4` leaf's luma TU *is* the whole block, so libaom's
1556 // `get_txb_ctx` takes its `plane_bsize == txsize_to_bsize[tx_size]`
1557 // branch: `txb_skip_ctx = 0`, never the neighbour-magnitude table
1558 // (`txb_common.h:400`). Reading the table here decoded `all_zero`
1559 // off the wrong CDF on the very first leaf and desynced the tile.
1560 let tu_grid = read_plane(
1561 dec,
1562 cdfs,
1563 if reduced_tx_set {
1564 TxbSet::Luma4
1565 } else {
1566 TxbSet::Luma4Set1
1567 },
1568 scan4,
1569 0,
1570 tu_around,
1571 mode,
1572 mode,
1573 angle_delta_y,
1574 reach,
1575 y,
1576 u,
1577 v,
1578 px,
1579 py,
1580 4,
1581 4,
1582 base_q_idx,
1583 None,
1584 filter_intra,
1585 None,
1586 smooth_neighbor,
1587 fctx,
1588 )?;
1589 neighbours.record_mi_luma(lmi, 4, &tu_grid);
1590 }
1591 neighbours.fill_skip_grid(lmi, 1, skip);
1592 neighbours.fill_lf_grid(lmi, 1, 4, 0, fctx);
1593 if has_chroma {
1594 if chroma_444 {
1595 // lane-av1-444: the leaf's own 4x4 chroma unit, coded right
1596 // after its luma (no shared group unit at ss 0/0).
1597 let (uv_mode, _, alpha) = chroma.expect("4:4:4 reads uv_mode on every leaf");
1598 sub8_leaf_chroma444(
1599 dec,
1600 cdfs,
1601 neighbours,
1602 lmi,
1603 4,
1604 4,
1605 uv_mode,
1606 alpha,
1607 skip,
1608 None,
1609 y,
1610 u,
1611 v,
1612 scan4,
1613 base_q_idx,
1614 neighbours.smooth_uv_neighbour(lmi.0, lmi.1, r, c, fctx),
1615 fctx,
1616 )?;
1617 last_uv444 = if uv_mode == UV_CFL_PRED {
1618 DC_PRED
1619 } else {
1620 uv_mode
1621 };
1622 } else if chroma_422 {
1623 let (uv_mode, angle_delta_uv, alpha) =
1624 chroma.expect("4:2:2 reads uv_mode on even-column leaves");
1625 sub8_leaf_chroma422(
1626 dec,
1627 cdfs,
1628 neighbours,
1629 lmi,
1630 uv_mode,
1631 angle_delta_uv,
1632 alpha,
1633 skip,
1634 None,
1635 y,
1636 u,
1637 v,
1638 scan4,
1639 base_q_idx,
1640 neighbours.smooth_uv_neighbour(lmi.0, lmi.1, r, c, fctx),
1641 (4, 4),
1642 fctx,
1643 )?;
1644 last_uv422 = if uv_mode == UV_CFL_PRED {
1645 DC_PRED
1646 } else {
1647 uv_mode
1648 };
1649 } else {
1650 last_uv = chroma;
1651 }
1652 }
1653 }
1654 // lane-av1mono: as [`decode_leaf_rect8`] -- a monochrome frame has no
1655 // chroma to reconstruct and no uv_mode was read.
1656 let (u_grid, v_grid): (Grid, Grid) = if mono(fctx) {
1657 (Grid::Zero(16), Grid::Zero(16))
1658 } else if chroma_444 {
1659 // lane-av1-444: every leaf coded and reconstructed its own chroma in
1660 // the loop above ([`sub8_leaf_chroma444`] stamped the mi-exact maps
1661 // and per-leaf entropy state); only the coarse slots remain, filled
1662 // from the bottom-right leaf like the 4:2:0 tail's single unit.
1663 neighbours.above_uv_mode[c] = last_uv444;
1664 neighbours.left_uv_mode[r] = last_uv444;
1665 (Grid::Zero(16), Grid::Zero(16))
1666 } else if chroma_422 {
1667 // lane-av1-422g: each even-column leaf coded and reconstructed its
1668 // own TX_4X4 chroma unit in the loop above (sub8_leaf_chroma422
1669 // stamped the per-leaf entropy state); only the coarse slots remain,
1670 // filled from the group's last chroma-reference leaf (bottom-left).
1671 neighbours.above_uv_mode[c] = last_uv422;
1672 neighbours.left_uv_mode[r] = last_uv422;
1673 (Grid::Zero(16), Grid::Zero(16))
1674 } else {
1675 let (gpx, gpy) = (leaf_mi.1 * MI, leaf_mi.0 * MI);
1676 let (cpx, cpy) = (gpx >> ss_x(fctx), gpy >> ss_y(fctx));
1677 let group_reach = Reach::of(8, gpx, gpy, y.width, y.height, fctx);
1678 let (uv_mode, angle_delta_uv, alpha) =
1679 last_uv.expect("i==3 always sets has_chroma, so chroma is always Some");
1680 let uv_predict_mode = if uv_mode == UV_CFL_PRED {
1681 DC_PRED
1682 } else {
1683 uv_mode
1684 };
1685 neighbours.above_uv_mode[c] = uv_predict_mode;
1686 neighbours.left_uv_mode[r] = uv_predict_mode;
1687 neighbours.record_uv_mode_mi(leaf_mi.0, leaf_mi.1, 2, 2, uv_predict_mode);
1688 let (u_grid, v_grid): (Grid, Grid) = if leaf_skips[3] {
1689 // A skipped chroma block still gets its CfL contribution: libaom's
1690 // `predict_and_reconstruct_intra_block` calls `cfl_predict_block`
1691 // from `av1_predict_intra_block_facade` (av1/common/reconintra.c:1719)
1692 // whenever `mbmi->uv_mode == UV_CFL_PRED`, before any residual is
1693 // added -- `skip_txfm` only drops the residual, never the alpha.
1694 // lane-reelckf and lane-kf1200 fixed THIS SAME arm independently; the
1695 // merge keeps one fix and feeds both lanes' counters so neither of
1696 // their gates goes vacuous.
1697 let ac = alpha.map(|_| cfl_src(gpx, gpy, 8));
1698 if alpha.is_some() {
1699 hit!(CHROMA_SKIP_CFL_HITS);
1700 hit!(SKIP_CFL_HITS);
1701 }
1702 push_intra(
1703 y,
1704 u,
1705 v,
1706 1,
1707 cpx,
1708 cpy,
1709 4,
1710 uv_predict_mode,
1711 angle_delta_uv,
1712 group_reach,
1713 &ZERO_RESIDUAL[..16],
1714 alpha.zip(ac).map(|((au, _), ac)| (au, ac)),
1715 None,
1716 smooth_neighbor_uv,
1717 fctx,
1718 );
1719 push_intra(
1720 y,
1721 u,
1722 v,
1723 2,
1724 cpx,
1725 cpy,
1726 4,
1727 uv_predict_mode,
1728 angle_delta_uv,
1729 group_reach,
1730 &ZERO_RESIDUAL[..16],
1731 alpha.zip(ac).map(|((_, av), ac)| (av, ac)),
1732 None,
1733 smooth_neighbor_uv,
1734 fctx,
1735 );
1736 // lane-av1txbands: a SKIPPED chroma-reference leaf reads no chroma
1737 // coefficient, so it never consults the inherited luma transform type
1738 // -- but an intrabc chroma-reference leaf did arm
1739 // [`INTRABC_CHROMA_TX`] above (`has_chroma`), and the two coefficient
1740 // branches below are the only ones that disarm it. Left armed, the slot
1741 // picks the NEXT block's chroma scan and inverse transform (class
1742 // arm-without-clear-on-a-skip-route). Counted so a gate can tell a
1743 // stream that reaches this route from one that never does.
1744 if fctx.intrabc_chroma_tx.with(std::cell::Cell::get).is_some() {
1745 hit!(SKIPPED_INTRABC_CHROMA_ARM_HITS);
1746 fctx.intrabc_chroma_tx.with(|c| c.set(None));
1747 }
1748 (Grid::Zero(16), Grid::Zero(16))
1749 } else if let Some(dv) = last_intrabc {
1750 // lane-av1txr: the chroma-reference leaf was an intrabc block, so the
1751 // group's one chroma unit is a frame copy at that DV too (its residual
1752 // still reads the ordinary `Chroma4` coefficient tables -- libaom
1753 // codes a chroma `tx_type` nowhere).
1754 let mut ub = vec![0u16; 16];
1755 mc::predict_with_filter(
1756 &u.data,
1757 u.width,
1758 u.true_width,
1759 u.true_height,
1760 mv_to_q4(cpx, dv.1, ss_x(fctx)),
1761 mv_to_q4(cpy, dv.0, ss_y(fctx)),
1762 4,
1763 4,
1764 mc::InterpFilterKind::Bilinear,
1765 &mut ub,
1766 fctx,
1767 );
1768 let mut vb = vec![0u16; 16];
1769 mc::predict_with_filter(
1770 &v.data,
1771 v.width,
1772 v.true_width,
1773 v.true_height,
1774 mv_to_q4(cpx, dv.1, ss_x(fctx)),
1775 mv_to_q4(cpy, dv.0, ss_y(fctx)),
1776 4,
1777 4,
1778 mc::InterpFilterKind::Bilinear,
1779 &mut vb,
1780 fctx,
1781 );
1782 let chroma_around = neighbours.around_mi(leaf_mi, 8);
1783 set_palette_pred(ub, fctx);
1784 let ug = read_plane(
1785 dec,
1786 cdfs,
1787 TxbSet::Chroma4,
1788 scan4,
1789 1,
1790 chroma_around[1],
1791 DC_PRED,
1792 DC_PRED,
1793 0,
1794 group_reach,
1795 y,
1796 u,
1797 v,
1798 cpx,
1799 cpy,
1800 4,
1801 TX4,
1802 base_q_idx,
1803 None,
1804 None,
1805 None,
1806 smooth_neighbor_uv,
1807 fctx,
1808 )?;
1809 set_palette_pred(vb, fctx);
1810 let vg = read_plane(
1811 dec,
1812 cdfs,
1813 TxbSet::Chroma4,
1814 scan4,
1815 2,
1816 chroma_around[2],
1817 DC_PRED,
1818 DC_PRED,
1819 0,
1820 group_reach,
1821 y,
1822 u,
1823 v,
1824 cpx,
1825 cpy,
1826 4,
1827 TX4,
1828 base_q_idx,
1829 None,
1830 None,
1831 None,
1832 smooth_neighbor_uv,
1833 fctx,
1834 )?;
1835 fctx.intrabc_chroma_tx.with(|c| c.set(None));
1836 (ug, vg)
1837 } else {
1838 let ac = alpha.map(|_| cfl_src(gpx, gpy, 8));
1839 let chroma_around = neighbours.around_mi(leaf_mi, 8);
1840 let ug = read_plane(
1841 dec,
1842 cdfs,
1843 TxbSet::Chroma4,
1844 scan4,
1845 1,
1846 chroma_around[1],
1847 uv_mode,
1848 uv_predict_mode,
1849 angle_delta_uv,
1850 group_reach,
1851 y,
1852 u,
1853 v,
1854 cpx,
1855 cpy,
1856 4,
1857 TX4,
1858 base_q_idx,
1859 alpha.zip(ac).map(|((au, _), ac)| (au, ac)),
1860 None,
1861 None,
1862 smooth_neighbor_uv,
1863 fctx,
1864 )?;
1865 let vg = read_plane(
1866 dec,
1867 cdfs,
1868 TxbSet::Chroma4,
1869 scan4,
1870 2,
1871 chroma_around[2],
1872 uv_mode,
1873 uv_predict_mode,
1874 angle_delta_uv,
1875 group_reach,
1876 y,
1877 u,
1878 v,
1879 cpx,
1880 cpy,
1881 4,
1882 TX4,
1883 base_q_idx,
1884 alpha.zip(ac).map(|((_, av), ac)| (av, ac)),
1885 None,
1886 None,
1887 smooth_neighbor_uv,
1888 fctx,
1889 )?;
1890 (ug, vg)
1891 };
1892 (u_grid, v_grid)
1893 };
1894 // Chroma neighbour bookkeeping for the whole 8x8 group -- mirrors
1895 // [`decode_leaf8`]'s own tx4-chroma section (`record_split_luma`'s
1896 // rescaling is wrong for an already-mi-unit position, so this inlines
1897 // its tail directly at `leaf_mi`, side_mi=2), EXCEPT for the partition
1898 // context value itself. Unlike `decode_leaf8`'s TX4 branch (still one
1899 // `BLOCK_8X8`, `PARTITION_NONE`, transform depth only), this group is
1900 // genuinely four `BLOCK_4X4` blocks (`PARTITION_SPLIT` at the 8x8
1901 // level, libaom `decode_partition`'s `bsize2`/`subsize` recursion
1902 // bottom). libaom's `update_ext_partition_context` writes
1903 // `partition_context_lookup[subsize]` at the 8x8 slot -- `{30,30}` for
1904 // `BLOCK_8X8` (bit0=0, what `decode_leaf8` already reproduces via
1905 // side_mi=8) but `{31,31}` for `BLOCK_4X4` (bit0=1) here, and
1906 // `partition_ctx_mi`'s `left_side_mi[mi_r]*2 <= side` needs
1907 // `left_side_mi <= 4` to read that bit as 1. Leaving this at 8 (this
1908 // round's bug) makes the very next sibling 8x8's `partition_w8` read
1909 // with the wrong context and desync immediately.
1910 let side_mi = 2;
1911 for cell in 0..side_mi {
1912 neighbours.left_side_mi[leaf_mi.0 + cell] = 4;
1913 neighbours.above_side_mi[leaf_mi.1 + cell] = 4;
1914 }
1915 // lane-av1-444: at 4:4:4 [`sub8_leaf_chroma444`] already stamped each
1916 // leaf's own span as it decoded; these placeholder grids carry no state.
1917 // lane-av1-422g: the 4:2:2 per-leaf units stamp the same way.
1918 if !chroma_444 && !chroma_422 {
1919 let round_up_even = |n: usize| n.div_ceil(2) * 2;
1920 let bound_h = round_up_even(neighbours.mi_rows);
1921 let bound_w = round_up_even(neighbours.mi_cols);
1922 let u_state = neighbour_state(&u_grid);
1923 let v_state = neighbour_state(&v_grid);
1924 for cell in 0..side_mi {
1925 if cell < side_mi.min(bound_h.saturating_sub(leaf_mi.0)) {
1926 neighbours.left[leaf_mi.0 + cell][1] = u_state;
1927 neighbours.left[leaf_mi.0 + cell][2] = v_state;
1928 }
1929 if cell < side_mi.min(bound_w.saturating_sub(leaf_mi.1)) {
1930 neighbours.above[leaf_mi.1 + cell][1] = u_state;
1931 neighbours.above[leaf_mi.1 + cell][2] = v_state;
1932 }
1933 }
1934 }
1935 // lane-dkey: a sub-8x8 leaf can never use palette (`av1_allow_palette`
1936 // needs BLOCK_8X8 and up), and libaom's mi grid says so -- its
1937 // `palette_size` is 0 in every one of these mi cells, so the NEXT block's
1938 // `av1_get_palette_mode_ctx` and colour cache read 0 here. This decoder
1939 // only ever WROTE palette state from the >=8x8 readers, so an 8x8 palette
1940 // block above a 4x4 group left its size/colours standing in the mi-granular
1941 // bands: the block below the group read `palette_y_mode` off CDF row 1
1942 // where libaom reads row 0 (class [[cdf-row-held-constant]]), decoded a
1943 // palette libaom never coded, and then skipped the `use_filter_intra`
1944 // symbol that a palette block has none of -- one symbol short, tile
1945 // desynced. Publishing the group's own zeros is the clear.
1946 neighbours.record_palette_y_rect(leaf_mi, 8, 8, 0, [0u16; 8]);
1947 neighbours.record_palette_uv_rect(leaf_mi, 8, 8, 0, [0u16; 8]);
1948 Ok((leaf_modes[2], leaf_modes[1]))
1949}
1950
1951/// Decodes a `PARTITION_VERT`/`PARTITION_HORZ` of one 8x8 block into two
1952/// `BLOCK_4X8`/`BLOCK_8X4` leaves (lane-tx4x8), the last partition shape
1953/// below 8x8 this decoder refused. Mirrors [`decode_leaf_split4`] element for
1954/// element -- same mode reads ([`read_intra_mode_sub8`]: no angle delta, no
1955/// tx-depth symbol below 8x8), same "chroma once, on the last sub-block, at
1956/// the whole 8x8 group's 4x4 unit" rule (`is_chroma_reference` is false for
1957/// every sub-block one mi wide or high but the last) -- differing only where
1958/// the shape does:
1959///
1960/// * the transform is a true `TX_4X8`/`TX_8X4`
1961/// ([`dequant_and_inverse_typed_wh`], whose `abs(rect_type) == 1` scale and
1962/// `av1_inv_txfm_shift_ls` row already cover this size pair);
1963/// * the CDF set is [`TxbSet::LumaRect4x8`] (`get_txsize_entropy_ctx` squares
1964/// up to `TX_8X8`) with a 32-position `eob_pt` and the `TX_4X4` `tx_type`
1965/// row -- see that variant's doc comment;
1966/// * the scan is [`SCAN_4X8`]/[`SCAN_8X4`], or their `mrow`/`mcol` siblings
1967/// ([`class_scan_table_wh`]) when the coded `tx_type` is `V_DCT`/`H_DCT`;
1968/// * `Reach` comes from libaom's own `has_tr_4x8`/`has_bl_8x4` rows
1969/// ([`Reach::of_rect`]);
1970/// * `update_partition_context` writes `partition_context_lookup[BLOCK_4X8]`
1971/// = `{31, 30}` (width 4 above, height 8 left), not the `{31, 31}` a
1972/// `BLOCK_4X4` split writes.
1973///
1974/// Filter intra on one of these leaves is decoded (lane-fi8 r1): the flag is
1975/// read from `BLOCK_4X8`/`BLOCK_8X4`'s own `cdf::FILTER_INTRA` row (classes
1976/// 8/9, NOT `BLOCK_4X4`'s), the predictor is [`crate::intra::predict_filter_intra`]
1977/// over the leaf's true `bw`x`bh` -- per 4x4 transform unit, with that unit's
1978/// own edge, when `tx_depth` splits it -- and the coded `tx_type` row is
1979/// [`fi_tx_row`]'s `fimode_to_intradir`, not the block's raw `DC_PRED`.
1980#[allow(clippy::too_many_arguments)]
1981pub(super) fn decode_leaf_rect8(
1982 dec: &mut SymbolDecoder,
1983 cdfs: &mut Cdfs,
1984 neighbours: &mut Neighbours,
1985 outer_at: (usize, usize),
1986 leaf_mi: (usize, usize),
1987 vert: bool,
1988 scan4: &[u16],
1989 y: &mut PlaneBuf<'static>,
1990 u: &mut PlaneBuf<'static>,
1991 v: &mut PlaneBuf<'static>,
1992 base_q_idx: u8,
1993 enable_filter_intra: bool,
1994 allow_intrabc: bool,
1995 tx_select: bool,
1996 reduced_tx_set: bool,
1997 fctx: &crate::decode::FrameCtx,
1998) -> Result<(usize, usize)> {
1999 let (r, c) = outer_at;
2000 // lane-modectx r1: availability-correct fallback pair (see
2001 // `modes_above_left`), not the raw coarse bands.
2002 let (above_mode, left_mode) = neighbours.modes_above_left(r, c);
2003 let smooth_neighbor_uv = neighbours.smooth_uv_neighbour(leaf_mi.0, leaf_mi.1, r, c, fctx);
2004 let (bw, bh) = if vert { (4, 8) } else { (8, 4) };
2005 let (w_mi, h_mi) = (bw / MI, bh / MI);
2006 let scan: &[u16] = if vert { &SCAN_4X8 } else { &SCAN_8X4 };
2007 let mut leaf_modes = [0usize; 2];
2008 let mut leaf_skips = [false; 2];
2009 let mut last_uv: Option<(usize, i32, Option<(i32, i32)>)> = None;
2010 // lane-av1txr: as [`decode_leaf_split4`]'s own `last_intrabc`.
2011 let mut last_intrabc: Option<(i32, i32)> = None;
2012 // 4:4:4 (lane-av1-444): as [`decode_leaf_split4`] -- at subsampling 0/0
2013 // EVERY 4x8/8x4 leaf reads its own `uv_mode` and codes its own
2014 // full-resolution rect chroma unit right after its luma.
2015 let chroma_444 = ss_x(fctx) == 0 && ss_y(fctx) == 0;
2016 let mut last_uv444 = DC_PRED;
2017 // lane-av1-422h: at ss (1,0) `is_chroma_reference`
2018 // (av1_common_int.h:1459) resolves per strip shape --
2019 // `((mi_row & 1) || !(bh_mi & 1) || !ss_y) && ((mi_col & 1) || !(bw_mi & 1) || !ss_x)`:
2020 // a HORZ pair is two BLOCK_8X4 strips at the group's even column, so
2021 // BOTH strips are chroma references (`!(bw & 1)`); a VERT pair is two
2022 // BLOCK_4X8 strips, and the column clause keeps only the ODD-column
2023 // strip (i == 1, the same leaf the 4:2:0 rule reads chroma on).
2024 let chroma_422 = ss_x(fctx) == 1 && ss_y(fctx) == 0;
2025 let mut last_uv422 = DC_PRED;
2026 for i in 0..2usize {
2027 let lmi = if vert {
2028 (leaf_mi.0, leaf_mi.1 + i)
2029 } else {
2030 (leaf_mi.0 + i, leaf_mi.1)
2031 };
2032 // Inside the pair the sibling is the neighbour on the split axis;
2033 // outside it the mi-granular map, else the coarse 16x16 slot.
2034 let leaf_above = if !vert && i == 1 {
2035 leaf_modes[0]
2036 } else {
2037 neighbours.mode_above_mi(lmi.0, lmi.1).unwrap_or(above_mode)
2038 };
2039 let leaf_left = if vert && i == 1 {
2040 leaf_modes[0]
2041 } else {
2042 neighbours.mode_left_mi(lmi.0, lmi.1).unwrap_or(left_mode)
2043 };
2044 let has_chroma = if chroma_444 {
2045 true
2046 } else if chroma_422 {
2047 !vert || i == 1
2048 } else {
2049 i == 1
2050 };
2051 let skip_ctx = neighbours.skip_txfm_ctx(lmi.0, lmi.1);
2052 let (skip, mode, angle_delta_y, chroma, filter_intra, intrabc) = read_intra_mode_sub8(
2053 dec,
2054 cdfs,
2055 leaf_above,
2056 leaf_left,
2057 has_chroma,
2058 enable_filter_intra,
2059 filter_intra_size_class_rect(bw, bh)
2060 .expect("BLOCK_4X8/BLOCK_8X4 are inside av1_filter_intra_allowed_bsize"),
2061 skip_ctx,
2062 allow_intrabc,
2063 lmi.0,
2064 lmi.1,
2065 bw / 4,
2066 bh / 4,
2067 fctx,
2068 )?;
2069 leaf_modes[i] = mode;
2070 leaf_skips[i] = skip;
2071 neighbours.record_mode_mi(lmi.0, lmi.1, w_mi, h_mi, mode);
2072 let smooth_neighbor = is_smooth_mode(leaf_above) || is_smooth_mode(leaf_left);
2073 if let Some(dv) = intrabc {
2074 // lane-av1-ibc444rect: the unskipped lossless luma walk this lane
2075 // fixes (one rect unit -> per-4x4 inter units).
2076 if !skip && lossless(fctx) {
2077 hit!(INTRABC_RECT8_LOSSLESS_LUMA_HITS);
2078 }
2079 // lane-av1txr: an intrabc `BLOCK_4X8`/`BLOCK_8X4` leaf. The
2080 // prediction is a frame copy at the DV and the residual reads the
2081 // INTER `LumaRect8x4` set -- but with `TxMode::Select` the leaf
2082 // reads the INTER var-tx tree, exactly like every other intrabc
2083 // block: libaom's `block_signals_txsize` is `bsize > BLOCK_4X4`
2084 // (`blockd.h:1027`), TRUE at `BLOCK_4X8`/`BLOCK_8X4`, so
2085 // `parse_decode_block` sends an unskipped `inter_block_tx` block
2086 // (intrabc counts, `is_inter_block`) of a signalling bsize into
2087 // `read_tx_size_vartx` over `max_txsize_rect_lookup[BLOCK_4X8] ==
2088 // TX_4X8`; only `BLOCK_4X4` reads no tx symbol at all. Measured on
2089 // `warped.obu` mi (37,62): reading NO symbol here left the
2090 // arithmetic decoder one symbol short and desynced the very next
2091 // block's `skip` (ours 60320 vs aomdec 48630, class
2092 // `symbol-consumption-gap`).
2093 let (px, py) = (lmi.1 * MI, lmi.0 * MI);
2094 let reach = Reach::of_rect(bw, bh, px, py, y.width, y.height, fctx);
2095 let mut luma_leaves: Vec<(usize, usize, usize, usize)> = Vec::new();
2096 // The non-var-tx band publish, deferred to AFTER the unit loop:
2097 // libaom's `set_txfm_ctxs` runs at `parse_decode_block`'s tail, so
2098 // a unit's own `txb_skip_ctx` read must see the PRE-block bands.
2099 let mut publish = Some((bw, bh));
2100 // lane-av1mixllfork: libaom `parse_decode_block` (decodeframe.c:1237)
2101 // gates the var-tx tree on `!xd->lossless[mbmi->segment_id]` as a
2102 // CONJUNCT of the tx_mode-select condition, not as the `else` arm --
2103 // `read_tx_size` (`decodeframe.c:1208`) answers TX_4X4 on the same
2104 // flag. A frame whose segments disagree (per-segment mixed
2105 // lossless: `--cq-level=0` with AQ) therefore reads NO tree symbol
2106 // on its lossless blocks. Ordering the test the other way round
2107 // reads a `tx_size_cat1` symbol the oracle never reads and forks
2108 // the very next block's `txb_skip`.
2109 if tx_select && !skip && !lossless(fctx) {
2110 // `max_block_wide`/`max_block_high`: the frame's own mi grid,
2111 // so a leaf hanging over the right/bottom edge reads no split
2112 // symbol for the units outside it (as in `decode_block`).
2113 let mi_cols = 2 * y.true_width.div_ceil(8);
2114 let mi_rows = 2 * y.true_height.div_ceil(8);
2115 let max_w_mi = w_mi.min(mi_cols.saturating_sub(lmi.1));
2116 let max_h_mi = h_mi.min(mi_rows.saturating_sub(lmi.0));
2117 read_var_tx_size(
2118 dec,
2119 cdfs,
2120 neighbours,
2121 lmi,
2122 bw.max(bh),
2123 (max_w_mi, max_h_mi),
2124 (bw, bh),
2125 0,
2126 0,
2127 0,
2128 &mut luma_leaves,
2129 );
2130 hit!(RECT_INTRABC_VARTX_HITS);
2131 publish = None;
2132 } else if lossless(fctx) && !skip {
2133 // lane-av1-ibc444rect: `read_tx_size`'s lossless first line
2134 // (`decodeframe.c:1203`) answers TX_4X4 for EVERY plane before
2135 // any tree, and a lossless frame's `TxMode` is `ONLY_4X4`
2136 // (spec 6.8.20) so no var-tx tree is read either -- the leaf's
2137 // coefficients are (bw/4)*(bh/4) inter TX_4X4 units at their
2138 // own origins, plane-major, NOT the single rect transform the
2139 // non-lossless arm reads (that one is
2140 // `av1_get_max_uv_txsize(BLOCK_8X4)`'s rect, unreachable at
2141 // lossless). Same unit structure the non-intrabc lossless
2142 // split below walks; each unit predicts from the CURRENT
2143 // frame at the DV. Measured on the lane's `e4` fixture
2144 // (`ll444_ibc_rect.obu`): the single-rect read desynced the
2145 // tile and tripped the lossless WHT assert `(4, 8) != (4, 4)`
2146 // at `TxParams::run`.
2147 for ur in 0..bh / 4usize {
2148 for uc in 0..bw / 4usize {
2149 luma_leaves.push((ur, uc, 4, 4));
2150 }
2151 }
2152 publish = Some((4, 4));
2153 } else {
2154 luma_leaves.push((0, 0, bw, bh));
2155 // The non-var-tx arm: a SKIPPED intrabc leaf (or a frame
2156 // outside `TxMode::Select`) publishes its own rect transform
2157 // (`set_txfm_ctxs`'s `skip && is_inter` branch writes the
2158 // block's own pixels -- the same size here).
2159 }
2160 // The transform type an intrabc block's CHROMA inherits is the
2161 // `tx_type_map` entry at the chroma unit's co-located luma
2162 // position (`av1_get_tx_type`, `blockd.h:1278`), i.e. the FIRST
2163 // luma unit the tree resolved.
2164 let single = luma_leaves.len() == 1;
2165 let mut chroma_tx = TxType::DctDct;
2166 let mut grid = Grid::Zero(bw * bh);
2167 for (li, &(tu_row, tu_col, tw, th)) in luma_leaves.iter().enumerate() {
2168 let tu_mi = (lmi.0 + tu_row, lmi.1 + tu_col);
2169 let (tu_px, tu_py) = (tu_mi.1 * MI, tu_mi.0 * MI);
2170 let tu_reach = if single {
2171 reach
2172 } else {
2173 tu_reach(
2174 bw,
2175 bh,
2176 tu_px - px,
2177 tu_py - py,
2178 tw,
2179 reach,
2180 px,
2181 py,
2182 y.width,
2183 y.height,
2184 fctx,
2185 )
2186 };
2187 let mut yb = vec![0u16; tw * th];
2188 mc::predict_with_filter(
2189 &y.data,
2190 y.width,
2191 y.true_width,
2192 y.true_height,
2193 mv_to_q4(tu_px, dv.1, 0),
2194 mv_to_q4(tu_py, dv.0, 0),
2195 tw,
2196 th,
2197 mc::InterpFilterKind::Bilinear,
2198 &mut yb,
2199 fctx,
2200 );
2201 set_palette_pred(yb, fctx);
2202 let tu_grid = if skip {
2203 let zeros = Grid::Zero(tw * th);
2204 if (tw, th) == (4, 4) {
2205 push_intra(
2206 y,
2207 u,
2208 v,
2209 0,
2210 tu_px,
2211 tu_py,
2212 4,
2213 DC_PRED,
2214 0,
2215 tu_reach,
2216 &zeros,
2217 None,
2218 None,
2219 smooth_neighbor,
2220 fctx,
2221 );
2222 } else {
2223 push_intra_rect(
2224 y,
2225 u,
2226 v,
2227 0,
2228 tu_px,
2229 tu_py,
2230 tw,
2231 th,
2232 DC_PRED,
2233 0,
2234 tu_reach,
2235 &zeros,
2236 None,
2237 None,
2238 smooth_neighbor,
2239 fctx,
2240 );
2241 }
2242 zeros
2243 } else {
2244 // A unit smaller than its own block (the var-tx split)
2245 // reads its `txb_skip_ctx` off the neighbours; the
2246 // whole-leaf unit's `plane_bsize == txsize_to_bsize` fixes
2247 // it at 0.
2248 let (set, tu_scan, around, skip_ctx) = if (tw, th) == (4, 4) {
2249 (
2250 inter_txbset_for(4, reduced_tx_set),
2251 scan4,
2252 neighbours.around_mi(tu_mi, 4)[0],
2253 neighbours.luma_skip_ctx(tu_mi, 1),
2254 )
2255 } else {
2256 (
2257 if reduced_tx_set {
2258 TxbSet::LumaRect8x4Inter
2259 } else {
2260 TxbSet::LumaRect8x4InterSet1
2261 },
2262 scan,
2263 neighbours.around_mi_rect(tu_mi, tw, th)[0],
2264 0,
2265 )
2266 };
2267 let mut coding = cdfs.txb(set, DC_PRED);
2268 // lane-av1-ibc444rect: `av1_read_tx_type` returns before any
2269 // symbol for a lossless block (qindex == 0, decodemv.c:681)
2270 // on EVERY plane and size -- the lossless units this arm now
2271 // walks must not read the set's tx_type either (`read_plane`
2272 // does the same for its own call sites). Measured on the
2273 // lane's `ll444_ibc_rect.obu`: with the symbol read, the
2274 // first unit's ladder diverged right after a matched
2275 // `all_zero` (ours +4096 rng on the unused tx_type).
2276 if lossless(fctx) {
2277 coding.tx_type = None;
2278 }
2279 let (levels, tx_type) = read_coeffs_rect(
2280 dec,
2281 &mut coding,
2282 tu_scan,
2283 tw,
2284 th,
2285 skip_ctx,
2286 dc_sign_ctx(around.2),
2287 TxType::DctDct,
2288 0,
2289 )?;
2290 if li == 0 {
2291 chroma_tx = tx_type;
2292 fctx.luma_tx_type.with(|c| c.set(tx_type));
2293 }
2294 // lane-av1-ibc444rect: the residual goes through [`TxParams`]
2295 // with the frame's lossless flag, exactly like
2296 // [`read_plane`]'s own dequant -- at lossless `TxParams::run`
2297 // is the Walsh-Hadamard inverse on every plane, and the
2298 // `dequant_and_inverse_typed_wh` shortcut the lossy var-tx
2299 // callers use has no lossless flag (it would run the DCT
2300 // inverse over the WHT-ordered levels). Measured on the lane
2301 // fixture: the DCT route left ±1..30 sample errors on every
2302 // walked leaf's luma.
2303 let tx = TxParams {
2304 plane: 0,
2305 w: tw,
2306 h: th,
2307 bit_depth: crate::decode::bit_depth(fctx),
2308 q_idx: block_q_idx(fctx),
2309 dc_delta: plane_q_delta(0, fctx).0,
2310 ac_delta: plane_q_delta(0, fctx).1,
2311 tx_type,
2312 qm: block_iqmatrix(fctx, 0, tw, th, tx_type),
2313 stride: 0,
2314 lossless: lossless(fctx),
2315 };
2316 let residual = tx.run(&levels);
2317 if (tw, th) == (4, 4) {
2318 push_intra(
2319 y,
2320 u,
2321 v,
2322 0,
2323 tu_px,
2324 tu_py,
2325 4,
2326 DC_PRED,
2327 0,
2328 tu_reach,
2329 &residual,
2330 None,
2331 None,
2332 smooth_neighbor,
2333 fctx,
2334 );
2335 } else {
2336 push_intra_rect(
2337 y,
2338 u,
2339 v,
2340 0,
2341 tu_px,
2342 tu_py,
2343 tw,
2344 th,
2345 DC_PRED,
2346 0,
2347 tu_reach,
2348 &residual,
2349 None,
2350 None,
2351 smooth_neighbor,
2352 fctx,
2353 );
2354 }
2355 levels
2356 };
2357 if single {
2358 grid = tu_grid;
2359 } else {
2360 // Per-unit neighbour state, the same shape the intra rect
2361 // leaf's own split path runs (`decode_leaf8`'s depth-1
2362 // loop): a transform unit inside a block is not a block at
2363 // that position.
2364 neighbours.record_mi_luma(tu_mi, 4, &tu_grid);
2365 }
2366 }
2367 // The deferred non-var-tx band publish (see `publish` above): the
2368 // units' `txb_skip_ctx` reads ran against the pre-block bands.
2369 if let Some((pub_w, pub_h)) = publish {
2370 txfm_partition_update_rect(neighbours, lmi, (pub_w, pub_h), (bw, bh));
2371 }
2372 if single {
2373 let state = neighbour_state(&grid);
2374 for cell in 0..h_mi {
2375 if lmi.0 + cell < neighbours.mi_rows {
2376 neighbours.left[lmi.0 + cell][0] = state;
2377 }
2378 }
2379 for cell in 0..w_mi {
2380 if lmi.1 + cell < neighbours.mi_cols {
2381 neighbours.above[lmi.1 + cell][0] = state;
2382 }
2383 }
2384 }
2385 neighbours.fill_skip_grid_rect(lmi, w_mi, h_mi, skip);
2386 neighbours.fill_lf_grid_rect(
2387 lmi,
2388 w_mi,
2389 h_mi,
2390 if single { bw as u8 } else { 4 },
2391 if single { bh as u8 } else { 4 },
2392 0,
2393 fctx,
2394 );
2395 // `record_intrabc_mi` publishes a square `n4`x`n4` footprint; a
2396 // 4x8/8x4 leaf needs its true `w_mi`x`h_mi` (lane-av1txr).
2397 record_intrabc_mi_rect(lmi.0, lmi.1, w_mi, h_mi, Some(dv), neighbours, fctx);
2398 if has_chroma {
2399 // See [`decode_leaf_split4`]: an intrabc block's chroma
2400 // inherits the luma transform type.
2401 fctx.intrabc_chroma_tx.with(|c| c.set(Some(chroma_tx)));
2402 if chroma_444 {
2403 // lane-av1-444: this leaf codes its OWN rect chroma unit
2404 // right here (frame copy at the DV); no group tail.
2405 sub8_leaf_chroma444(
2406 dec,
2407 cdfs,
2408 neighbours,
2409 lmi,
2410 bw,
2411 bh,
2412 DC_PRED,
2413 None,
2414 skip,
2415 Some(dv),
2416 y,
2417 u,
2418 v,
2419 scan4,
2420 base_q_idx,
2421 neighbours.smooth_uv_neighbour(lmi.0, lmi.1, r, c, fctx),
2422 fctx,
2423 )?;
2424 last_uv444 = DC_PRED;
2425 } else if chroma_422 {
2426 // lane-av1-422h: same at ss (1,0) -- the strip's own square
2427 // TX_4X4 chroma unit at its halved position.
2428 sub8_leaf_chroma422(
2429 dec,
2430 cdfs,
2431 neighbours,
2432 lmi,
2433 DC_PRED,
2434 0,
2435 None,
2436 skip,
2437 Some(dv),
2438 y,
2439 u,
2440 v,
2441 scan4,
2442 base_q_idx,
2443 neighbours.smooth_uv_neighbour(lmi.0, lmi.1, r, c, fctx),
2444 (bw, bh),
2445 fctx,
2446 )?;
2447 last_uv422 = DC_PRED;
2448 } else {
2449 last_uv = Some((DC_PRED, 0, None));
2450 last_intrabc = Some(dv);
2451 }
2452 }
2453 continue;
2454 }
2455 if filter_intra.is_some() {
2456 // lane-fi8 r1: `predict_filter_intra` walks its 4x2 patches over
2457 // a true `bw`x`bh` block, so a 4x8/8x4 leaf predicts through the
2458 // same `reconstruct_rect`/`reconstruct` calls below -- and under
2459 // a split transform, per 4x4 unit with that unit's own edge,
2460 // which is exactly what the depth-1 loop already does.
2461 hit!(FILTER_INTRA_RECT_HITS);
2462 hit!(FILTER_INTRA_RECT_SUB8_HITS);
2463 }
2464 // `TxMode::Select`'s `tx_depth` (spec 5.11.16) exists at a 4x8/8x4
2465 // leaf too, and is read for every intra block, skipped or not. Its
2466 // category is libaom's `bsize_to_tx_size_cat(BLOCK_4X8) == 0` --
2467 // `sub_tx_size_map[TX_4X8] == TX_4X4` is a single step, so it is the
2468 // same 2-symbol `tx_size_cat0` an 8x8 uses, not the 3-symbol
2469 // `tx_size_cat2` the 32x16 strips read -- with the rect context
2470 // (`get_tx_size_context`'s `max_tx_wide`/`max_tx_high` = bw/bh).
2471 // Missing this read consumed one symbol fewer than the encoder wrote
2472 // and desynced the tile at the very first rect leaf (class
2473 // `symbol-consumption-gap`).
2474 // lane-av1mix: libaom `read_tx_size`'s FIRST line
2475 // (`decodeframe.c:1203`, `if (xd->lossless[...]) return TX_4X4`)
2476 // fires before the `TX_MODE_SELECT` test, so a lossless-segment leaf
2477 // codes NO depth symbol even in `TxMode::Select` -- the mixed-lossless
2478 // frame's sub8 leaves forked exactly here (ours read the symbol at
2479 // (12477,50864) bit 3011, the oracle's all_zero used it).
2480 let depth = if tx_select && !lossless(fctx) {
2481 // lane-av1txbands: the `TXFM_CONTEXT` band read every other rect
2482 // reader already does.
2483 let ctx = tx_size_context_txfm_rect(neighbours, lmi, bw, bh, fctx);
2484 dec.symbol(&mut cdfs.tx_size_cat0[ctx])
2485 } else {
2486 0
2487 };
2488 if depth != 0 {
2489 hit!(TX_DEPTH_HITS);
2490 hit!(RECT8_SPLIT_TX_HITS);
2491 }
2492 if smooth_neighbor {
2493 hit!(SMOOTH_LUMA_HITS);
2494 }
2495 let (px, py) = (lmi.1 * MI, lmi.0 * MI);
2496 let reach = Reach::of_rect(bw, bh, px, py, y.width, y.height, fctx);
2497 // `sub_tx_size_map[TX_4X8] == TX_4X4`: depth 1 splits the leaf into
2498 // two 4x4 transform units along its long axis, each predicted from
2499 // the previous unit's reconstruction and reading its own `txb_skip`
2500 // context -- `decode_leaf8`'s own depth-1 loop, two units instead of
2501 // four. The per-TU path writes its own neighbour state, so the
2502 // block-level write below is skipped for it.
2503 let split = depth != 0 || lossless(fctx);
2504 // lane-av1txr: libaom `parse_decode_block`'s `set_txfm_ctxs` for this
2505 // leaf -- the size the `tx_depth` symbol resolved (depth 1 -> TX_4X4,
2506 // else the leaf's own rect transform), published over the leaf's own
2507 // mi span. An intrabc block's `txfm_partition_context` reads these
2508 // bands, and the key-frame sub-8x8 leaves had no write at all:
2509 // `warped.obu` mi (37,62) read a stale 8 where aomdec read 4 (ctx 18
2510 // vs 19) and the tile desynced (class `context-band-not-published`).
2511 let (ctx_tx_w, ctx_tx_h) = if split { (4, 4) } else { (bw, bh) };
2512 txfm_partition_update_rect(neighbours, lmi, (ctx_tx_w, ctx_tx_h), (bw, bh));
2513 if split {
2514 let mut done = 0usize;
2515 for tu in 0..2usize {
2516 let tu_mi = if vert {
2517 (lmi.0 + tu, lmi.1)
2518 } else {
2519 (lmi.0, lmi.1 + tu)
2520 };
2521 let (tu_px, tu_py) = (tu_mi.1 * MI, tu_mi.0 * MI);
2522 let tu_around = neighbours.around_mi(tu_mi, 4)[0];
2523 // Same per-TU rule as every other split-transform site
2524 // (lane-palette2 r12): a transform unit inside a block is not
2525 // a block at that position, so its reach comes from the
2526 // block's own row, offset by the unit's position.
2527 let tu_reach = tu_reach(
2528 bw,
2529 bh,
2530 tu_px - px,
2531 tu_py - py,
2532 4,
2533 reach,
2534 px,
2535 py,
2536 y.width,
2537 y.height,
2538 fctx,
2539 );
2540 let tu_skip_ctx = neighbours.luma_skip_ctx(tu_mi, 1);
2541 if skip {
2542 // A skipped leaf still predicts per transform unit: the
2543 // second 4x4 takes its above/left edge from the first
2544 // one's reconstruction, which is NOT what a single 4x8
2545 // prediction of the same mode produces.
2546 let zeros = vec![0i32; 16];
2547 push_intra(
2548 y,
2549 u,
2550 v,
2551 0,
2552 tu_px,
2553 tu_py,
2554 4,
2555 mode,
2556 angle_delta_y,
2557 tu_reach,
2558 &zeros,
2559 None,
2560 filter_intra,
2561 smooth_neighbor,
2562 fctx,
2563 );
2564 neighbours.record_mi_luma(tu_mi, 4, &zeros);
2565 continue;
2566 }
2567 let tu_grid = read_plane(
2568 dec,
2569 cdfs,
2570 if reduced_tx_set {
2571 TxbSet::Luma4
2572 } else {
2573 TxbSet::Luma4Set1
2574 },
2575 scan4,
2576 0,
2577 tu_around,
2578 mode,
2579 mode,
2580 angle_delta_y,
2581 tu_reach,
2582 y,
2583 u,
2584 v,
2585 tu_px,
2586 tu_py,
2587 4,
2588 TX4,
2589 base_q_idx,
2590 None,
2591 filter_intra,
2592 Some(tu_skip_ctx),
2593 smooth_neighbor,
2594 fctx,
2595 )?;
2596 if tu_grid.iter().any(|&l| l != 0) {
2597 done += 1;
2598 }
2599 neighbours.record_mi_luma(tu_mi, 4, &tu_grid);
2600 }
2601 if done > 0 {
2602 if vert {
2603 hit!(TX4X8_CODED_HITS);
2604 } else {
2605 hit!(TX8X4_CODED_HITS);
2606 }
2607 }
2608 neighbours.fill_skip_grid_rect(lmi, w_mi, h_mi, skip);
2609 neighbours.fill_lf_grid_rect(lmi, w_mi, h_mi, 4, 4, 0, fctx);
2610 if has_chroma {
2611 if chroma_444 {
2612 // lane-av1-444: the leaf's own rect chroma unit after its
2613 // split luma (no shared group unit at ss 0/0).
2614 let (uv_mode, _, alpha) = chroma.expect("4:4:4 reads uv_mode on every leaf");
2615 sub8_leaf_chroma444(
2616 dec,
2617 cdfs,
2618 neighbours,
2619 lmi,
2620 bw,
2621 bh,
2622 uv_mode,
2623 alpha,
2624 skip,
2625 None,
2626 y,
2627 u,
2628 v,
2629 scan4,
2630 base_q_idx,
2631 neighbours.smooth_uv_neighbour(lmi.0, lmi.1, r, c, fctx),
2632 fctx,
2633 )?;
2634 last_uv444 = if uv_mode == UV_CFL_PRED {
2635 DC_PRED
2636 } else {
2637 uv_mode
2638 };
2639 } else if chroma_422 {
2640 // lane-av1-422h: the strip's own square TX_4X4 chroma unit
2641 // per plane right after its split luma (one per ref strip).
2642 let (uv_mode, angle_delta_uv, alpha) =
2643 chroma.expect("4:2:2 chroma references read uv_mode");
2644 sub8_leaf_chroma422(
2645 dec,
2646 cdfs,
2647 neighbours,
2648 lmi,
2649 uv_mode,
2650 angle_delta_uv,
2651 alpha,
2652 skip,
2653 None,
2654 y,
2655 u,
2656 v,
2657 scan4,
2658 base_q_idx,
2659 neighbours.smooth_uv_neighbour(lmi.0, lmi.1, r, c, fctx),
2660 (bw, bh),
2661 fctx,
2662 )?;
2663 last_uv422 = if uv_mode == UV_CFL_PRED {
2664 DC_PRED
2665 } else {
2666 uv_mode
2667 };
2668 } else {
2669 last_uv = chroma;
2670 }
2671 }
2672 continue;
2673 }
2674 let grid = if skip {
2675 let zeros = Grid::Zero(bw * bh);
2676 push_intra_rect(
2677 y,
2678 u,
2679 v,
2680 0,
2681 px,
2682 py,
2683 bw,
2684 bh,
2685 mode,
2686 angle_delta_y,
2687 reach,
2688 &zeros,
2689 None,
2690 filter_intra,
2691 smooth_neighbor,
2692 fctx,
2693 );
2694 zeros
2695 } else {
2696 // `plane_bsize == txsize_to_bsize[tx_size]` here too (the leaf's
2697 // luma TU *is* the leaf), so `get_txb_ctx` fixes `txb_skip_ctx`
2698 // at 0 -- see `decode_leaf_split4`'s own note.
2699 let around = neighbours.around_mi_rect(lmi, bw, bh)[0];
2700 let set = if reduced_tx_set {
2701 TxbSet::LumaRect4x8
2702 } else {
2703 TxbSet::LumaRect4x8Set1
2704 };
2705 let mut coding = cdfs.txb(set, fi_tx_row(mode, filter_intra));
2706 let (levels, tx_type) = read_coeffs_rect(
2707 dec,
2708 &mut coding,
2709 scan,
2710 bw,
2711 bh,
2712 0,
2713 dc_sign_ctx(around.2),
2714 TxType::DctDct,
2715 0,
2716 )?;
2717 if levels.iter().any(|&l| l != 0) {
2718 if vert {
2719 hit!(TX4X8_CODED_HITS);
2720 } else {
2721 hit!(TX8X4_CODED_HITS);
2722 }
2723 }
2724 let residual = dequant_and_inverse_typed_wh(
2725 &levels,
2726 bw,
2727 bh,
2728 crate::decode::bit_depth(fctx),
2729 // lane-d792 sweep: the block's segment `SEG_LVL_ALT_Q`, as at
2730 // every other dequant site (class `parsed-then-discarded`).
2731 block_q_idx(fctx),
2732 plane_q_delta(0, fctx).0,
2733 plane_q_delta(0, fctx).1,
2734 tx_type,
2735 block_iqmatrix(fctx, 0, bw, bh, tx_type),
2736 );
2737 push_intra_rect(
2738 y,
2739 u,
2740 v,
2741 0,
2742 px,
2743 py,
2744 bw,
2745 bh,
2746 mode,
2747 angle_delta_y,
2748 reach,
2749 &residual,
2750 None,
2751 filter_intra,
2752 smooth_neighbor,
2753 fctx,
2754 );
2755 levels
2756 };
2757 // Plane 0's per-TU coefficient context over this leaf's own mi span
2758 // (`record_mi_luma`'s rect twin -- the chroma planes are written once
2759 // for the whole 8x8 group below).
2760 let state = neighbour_state(&grid);
2761 for cell in 0..h_mi {
2762 if lmi.0 + cell < neighbours.mi_rows {
2763 neighbours.left[lmi.0 + cell][0] = state;
2764 }
2765 }
2766 for cell in 0..w_mi {
2767 if lmi.1 + cell < neighbours.mi_cols {
2768 neighbours.above[lmi.1 + cell][0] = state;
2769 }
2770 }
2771 neighbours.fill_skip_grid_rect(lmi, w_mi, h_mi, skip);
2772 let (tx_w, tx_h) = if split { (4, 4) } else { (bw as u8, bh as u8) };
2773 neighbours.fill_lf_grid_rect(lmi, w_mi, h_mi, tx_w, tx_h, 0, fctx);
2774 if has_chroma {
2775 if chroma_444 {
2776 // lane-av1-444: the leaf's own rect chroma unit after its
2777 // luma (no shared group unit at ss 0/0).
2778 let (uv_mode, _, alpha) = chroma.expect("4:4:4 reads uv_mode on every leaf");
2779 sub8_leaf_chroma444(
2780 dec,
2781 cdfs,
2782 neighbours,
2783 lmi,
2784 bw,
2785 bh,
2786 uv_mode,
2787 alpha,
2788 skip,
2789 None,
2790 y,
2791 u,
2792 v,
2793 scan4,
2794 base_q_idx,
2795 neighbours.smooth_uv_neighbour(lmi.0, lmi.1, r, c, fctx),
2796 fctx,
2797 )?;
2798 last_uv444 = if uv_mode == UV_CFL_PRED {
2799 DC_PRED
2800 } else {
2801 uv_mode
2802 };
2803 } else if chroma_422 {
2804 // lane-av1-422h: the strip's own square TX_4X4 chroma unit
2805 // per plane right after its luma -- libaom codes each
2806 // BLOCK_8X4 strip's chroma under the strip itself (both
2807 // strips of a HORZ pair are chroma references), not one
2808 // shared group unit.
2809 let (uv_mode, angle_delta_uv, alpha) =
2810 chroma.expect("4:2:2 chroma references read uv_mode");
2811 sub8_leaf_chroma422(
2812 dec,
2813 cdfs,
2814 neighbours,
2815 lmi,
2816 uv_mode,
2817 angle_delta_uv,
2818 alpha,
2819 skip,
2820 None,
2821 y,
2822 u,
2823 v,
2824 scan4,
2825 base_q_idx,
2826 neighbours.smooth_uv_neighbour(lmi.0, lmi.1, r, c, fctx),
2827 (bw, bh),
2828 fctx,
2829 )?;
2830 last_uv422 = if uv_mode == UV_CFL_PRED {
2831 DC_PRED
2832 } else {
2833 uv_mode
2834 };
2835 } else {
2836 last_uv = chroma;
2837 }
2838 }
2839 }
2840 // lane-av1mono: a monochrome frame codes no chroma at all -- no
2841 // `uv_mode` was read ([`read_intra_mode_sub8`] folds the plane count in),
2842 // so there is no chroma to reconstruct and no chroma coefficient to read.
2843 let (u_grid, v_grid): (Grid, Grid) = if mono(fctx) {
2844 (Grid::Zero(16), Grid::Zero(16))
2845 } else if chroma_444 {
2846 // lane-av1-444: every leaf coded and reconstructed its own chroma in
2847 // the loop above; only the coarse slots remain (last leaf's mode).
2848 neighbours.above_uv_mode[c] = last_uv444;
2849 neighbours.left_uv_mode[r] = last_uv444;
2850 (Grid::Zero(16), Grid::Zero(16))
2851 } else if chroma_422 {
2852 // lane-av1-422h: every chroma-reference strip coded and reconstructed
2853 // its own chroma unit in the loop above (and stamped its own entropy
2854 // span), so the 4:2:0 shared group unit below must not run. Only the
2855 // coarse 16x16 slots remain: libaom's `above_uv_mode`/`left_uv_mode`
2856 // end up holding the LAST chroma reference's mode (decode order).
2857 neighbours.above_uv_mode[c] = last_uv422;
2858 neighbours.left_uv_mode[r] = last_uv422;
2859 (Grid::Zero(16), Grid::Zero(16))
2860 } else {
2861 let (gpx, gpy) = (leaf_mi.1 * MI, leaf_mi.0 * MI);
2862 let (cpx, cpy) = (gpx >> ss_x(fctx), gpy >> ss_y(fctx));
2863 let group_reach = Reach::of(8, gpx, gpy, y.width, y.height, fctx);
2864 let (uv_mode, angle_delta_uv, alpha) =
2865 last_uv.expect("i==1 always sets has_chroma, so chroma is always Some");
2866 let uv_predict_mode = if uv_mode == UV_CFL_PRED {
2867 DC_PRED
2868 } else {
2869 uv_mode
2870 };
2871 neighbours.above_uv_mode[c] = uv_predict_mode;
2872 neighbours.left_uv_mode[r] = uv_predict_mode;
2873 neighbours.record_uv_mode_mi(leaf_mi.0, leaf_mi.1, 2, 2, uv_predict_mode);
2874 let (u_grid, v_grid): (Grid, Grid) = if leaf_skips[1] && last_intrabc.is_none() {
2875 // A skipped chroma block still gets its CfL contribution: libaom's
2876 // `predict_and_reconstruct_intra_block` calls `cfl_predict_block`
2877 // from `av1_predict_intra_block_facade` (av1/common/reconintra.c:1719)
2878 // whenever `mbmi->uv_mode == UV_CFL_PRED`, before any residual is
2879 // added -- `skip_txfm` only drops the residual, never the alpha.
2880 // lane-reelckf and lane-kf1200 fixed THIS SAME arm independently; the
2881 // merge keeps one fix and feeds both lanes' counters so neither of
2882 // their gates goes vacuous.
2883 let ac = alpha.map(|_| cfl_src(gpx, gpy, 8));
2884 if alpha.is_some() {
2885 hit!(CHROMA_SKIP_CFL_HITS);
2886 hit!(SKIP_CFL_HITS);
2887 }
2888 push_intra(
2889 y,
2890 u,
2891 v,
2892 1,
2893 cpx,
2894 cpy,
2895 4,
2896 uv_predict_mode,
2897 angle_delta_uv,
2898 group_reach,
2899 &ZERO_RESIDUAL[..16],
2900 alpha.zip(ac).map(|((au, _), ac)| (au, ac)),
2901 None,
2902 smooth_neighbor_uv,
2903 fctx,
2904 );
2905 push_intra(
2906 y,
2907 u,
2908 v,
2909 2,
2910 cpx,
2911 cpy,
2912 4,
2913 uv_predict_mode,
2914 angle_delta_uv,
2915 group_reach,
2916 &ZERO_RESIDUAL[..16],
2917 alpha.zip(ac).map(|((_, av), ac)| (av, ac)),
2918 None,
2919 smooth_neighbor_uv,
2920 fctx,
2921 );
2922 // lane-av1txbands: a SKIPPED chroma-reference leaf reads no chroma
2923 // coefficient, so it never consults the inherited luma transform type
2924 // -- but an intrabc chroma-reference leaf did arm
2925 // [`INTRABC_CHROMA_TX`] above (`has_chroma`), and the two coefficient
2926 // branches below are the only ones that disarm it. Left armed, the slot
2927 // picks the NEXT block's chroma scan and inverse transform (class
2928 // arm-without-clear-on-a-skip-route). Counted so a gate can tell a
2929 // stream that reaches this route from one that never does.
2930 if fctx.intrabc_chroma_tx.with(std::cell::Cell::get).is_some() {
2931 hit!(SKIPPED_INTRABC_CHROMA_ARM_HITS);
2932 fctx.intrabc_chroma_tx.with(|c| c.set(None));
2933 }
2934 (Grid::Zero(16), Grid::Zero(16))
2935 } else if let Some(dv) = last_intrabc {
2936 // A 4:2:0 `BLOCK_4X8`/`BLOCK_8X4` leaf maps to a clamped chroma
2937 // `BLOCK_4X4` (`get_plane_block_size`, blockd.h:1184; `set_plane_n4`,
2938 // av1_common_int.h:1344). The chroma reference is still the whole
2939 // 8x8 group's 4x4 plane block at `(cpx, cpy)`, so prediction uses the
2940 // group's destination origin, not the odd-mi leaf's nominal 2x4/4x2
2941 // extent. The old route treated a skipped intrabc leaf as skipped
2942 // intra and predicted this block with DC, first differing at
2943 // mi(46,52), U(104,92), while consuming identical symbols.
2944 // `decode_reconstruct_tx` reads no chroma coefficients for a skipped
2945 // intrabc block, but an unskipped one reads the ordinary Chroma4
2946 // tables; chroma inherits no coded tx_type of its own.
2947 let mut ub = vec![0u16; 16];
2948 mc::predict_with_filter(
2949 &u.data,
2950 u.width,
2951 u.true_width,
2952 u.true_height,
2953 mv_to_q4(cpx, dv.1, ss_x(fctx)),
2954 mv_to_q4(cpy, dv.0, ss_y(fctx)),
2955 4,
2956 4,
2957 mc::InterpFilterKind::Bilinear,
2958 &mut ub,
2959 fctx,
2960 );
2961 let mut vb = vec![0u16; 16];
2962 mc::predict_with_filter(
2963 &v.data,
2964 v.width,
2965 v.true_width,
2966 v.true_height,
2967 mv_to_q4(cpx, dv.1, ss_x(fctx)),
2968 mv_to_q4(cpy, dv.0, ss_y(fctx)),
2969 4,
2970 4,
2971 mc::InterpFilterKind::Bilinear,
2972 &mut vb,
2973 fctx,
2974 );
2975 if leaf_skips[1] {
2976 // `skip_txfm` still reconstructs an intrabc prediction, but
2977 // libaom's `decode_reconstruct_tx` reads NO chroma coefficients.
2978 // The ordinary skipped-intra arm above is therefore not valid for
2979 // an intrabc leaf: use the copied, clamped 4x4 prediction directly.
2980 push_mc_rect(
2981 y,
2982 u,
2983 v,
2984 1,
2985 cpx,
2986 cpy,
2987 4,
2988 4,
2989 4,
2990 Pred::Inline(&ub, 4),
2991 &ZERO_RESIDUAL[..16],
2992 fctx,
2993 );
2994 push_mc_rect(
2995 y,
2996 u,
2997 v,
2998 2,
2999 cpx,
3000 cpy,
3001 4,
3002 4,
3003 4,
3004 Pred::Inline(&vb, 4),
3005 &ZERO_RESIDUAL[..16],
3006 fctx,
3007 );
3008 hit!(SKIPPED_INTRABC_CHROMA_ARM_HITS);
3009 fctx.intrabc_chroma_tx.with(|c| c.set(None));
3010 (Grid::Zero(16), Grid::Zero(16))
3011 } else {
3012 let chroma_around = neighbours.around_mi(leaf_mi, 8);
3013 set_palette_pred(ub, fctx);
3014 let ug = read_plane(
3015 dec,
3016 cdfs,
3017 TxbSet::Chroma4,
3018 scan4,
3019 1,
3020 chroma_around[1],
3021 DC_PRED,
3022 DC_PRED,
3023 0,
3024 group_reach,
3025 y,
3026 u,
3027 v,
3028 cpx,
3029 cpy,
3030 4,
3031 TX4,
3032 base_q_idx,
3033 None,
3034 None,
3035 None,
3036 smooth_neighbor_uv,
3037 fctx,
3038 )?;
3039 set_palette_pred(vb, fctx);
3040 let vg = read_plane(
3041 dec,
3042 cdfs,
3043 TxbSet::Chroma4,
3044 scan4,
3045 2,
3046 chroma_around[2],
3047 DC_PRED,
3048 DC_PRED,
3049 0,
3050 group_reach,
3051 y,
3052 u,
3053 v,
3054 cpx,
3055 cpy,
3056 4,
3057 TX4,
3058 base_q_idx,
3059 None,
3060 None,
3061 None,
3062 smooth_neighbor_uv,
3063 fctx,
3064 )?;
3065 fctx.intrabc_chroma_tx.with(|c| c.set(None));
3066 (ug, vg)
3067 }
3068 } else {
3069 let ac = alpha.map(|_| cfl_src(gpx, gpy, 8));
3070 let chroma_around = neighbours.around_mi(leaf_mi, 8);
3071 let ug = read_plane(
3072 dec,
3073 cdfs,
3074 TxbSet::Chroma4,
3075 scan4,
3076 1,
3077 chroma_around[1],
3078 uv_mode,
3079 uv_predict_mode,
3080 angle_delta_uv,
3081 group_reach,
3082 y,
3083 u,
3084 v,
3085 cpx,
3086 cpy,
3087 4,
3088 TX4,
3089 base_q_idx,
3090 alpha.zip(ac).map(|((au, _), ac)| (au, ac)),
3091 None,
3092 None,
3093 smooth_neighbor_uv,
3094 fctx,
3095 )?;
3096 let vg = read_plane(
3097 dec,
3098 cdfs,
3099 TxbSet::Chroma4,
3100 scan4,
3101 2,
3102 chroma_around[2],
3103 uv_mode,
3104 uv_predict_mode,
3105 angle_delta_uv,
3106 group_reach,
3107 y,
3108 u,
3109 v,
3110 cpx,
3111 cpy,
3112 4,
3113 TX4,
3114 base_q_idx,
3115 alpha.zip(ac).map(|((_, av), ac)| (av, ac)),
3116 None,
3117 None,
3118 smooth_neighbor_uv,
3119 fctx,
3120 )?;
3121 (ug, vg)
3122 };
3123 (u_grid, v_grid)
3124 };
3125 // `update_partition_context` at the 8x8 level writes
3126 // `partition_context_lookup[subsize]` over the whole 8x8 span: BLOCK_4X8
3127 // is `{31, 30}` (bit0 set on the above half only), BLOCK_8X4 `{30, 31}`
3128 // -- the mirror of `decode_leaf_split4`'s `{31, 31}`, which
3129 // `partition_ctx_mi` reads back off these side values.
3130 for cell in 0..2 {
3131 neighbours.above_side_mi[leaf_mi.1 + cell] = bw as u8;
3132 neighbours.left_side_mi[leaf_mi.0 + cell] = bh as u8;
3133 }
3134 // lane-av1-444: at 4:4:4 [`sub8_leaf_chroma444`] already stamped each
3135 // leaf's own span as it decoded; these placeholder grids carry no state.
3136 // lane-av1-422h: same at ss (1,0) -- [`sub8_leaf_chroma422`] stamped each
3137 // chroma-reference strip's own span in decode order.
3138 if !(chroma_444 || chroma_422) {
3139 let round_up_even = |n: usize| n.div_ceil(2) * 2;
3140 let bound_h = round_up_even(neighbours.mi_rows);
3141 let bound_w = round_up_even(neighbours.mi_cols);
3142 let u_state = neighbour_state(&u_grid);
3143 let v_state = neighbour_state(&v_grid);
3144 for cell in 0..2usize {
3145 if cell < 2usize.min(bound_h.saturating_sub(leaf_mi.0)) {
3146 neighbours.left[leaf_mi.0 + cell][1] = u_state;
3147 neighbours.left[leaf_mi.0 + cell][2] = v_state;
3148 }
3149 if cell < 2usize.min(bound_w.saturating_sub(leaf_mi.1)) {
3150 neighbours.above[leaf_mi.1 + cell][1] = u_state;
3151 neighbours.above[leaf_mi.1 + cell][2] = v_state;
3152 }
3153 }
3154 }
3155 // lane-dkey (same clear as `decode_leaf_split4`): a sub-8x8 leaf can never use palette (`av1_allow_palette`
3156 // needs BLOCK_8X8 and up), and libaom's mi grid says so -- its
3157 // `palette_size` is 0 in every one of these mi cells, so the NEXT block's
3158 // `av1_get_palette_mode_ctx` and colour cache read 0 here. This decoder
3159 // only ever WROTE palette state from the >=8x8 readers, so an 8x8 palette
3160 // block above a 4x4 group left its size/colours standing in the mi-granular
3161 // bands: the block below the group read `palette_y_mode` off CDF row 1
3162 // where libaom reads row 0 (class [[cdf-row-held-constant]]), decoded a
3163 // palette libaom never coded, and then skipped the `use_filter_intra`
3164 // symbol that a palette block has none of -- one symbol short, tile
3165 // desynced. Publishing the group's own zeros is the clear.
3166 neighbours.record_palette_y_rect(leaf_mi, 8, 8, 0, [0u16; 8]);
3167 neighbours.record_palette_uv_rect(leaf_mi, 8, 8, 0, [0u16; 8]);
3168 // `(below_mode, right_mode)`: the sub-block a neighbour below sees is the
3169 // bottom one (the second under HORZ, the left one under VERT), the one a
3170 // neighbour to the right sees is the top-right (the second under VERT).
3171 Ok(if vert {
3172 (leaf_modes[0], leaf_modes[1])
3173 } else {
3174 (leaf_modes[1], leaf_modes[0])
3175 })
3176}