forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

key_tile.rs3059 lines, 162.4 KiBHistoryRawDownload
1//! Key-frame tile decoders: the per-tile walks with loop restoration, the multi-tile driver and the CDF-carrying key tile. Moved out of `decode/mod.rs` (av1split D10); the items are unchanged apart from `pub(super)` visibility.
2
3use super::*;
4
5/// [`decode_key_frame_tile`] with this frame header's own `lr_params` (spec
6/// 5.9.20) instead of the no-restoration default -- the per-unit filters
7/// themselves live in the tile payload, so the header is all this needs.
8#[allow(clippy::too_many_arguments)]
9pub(crate) fn decode_key_frame_tile_lr(
10 data: &[u8],
11 mi_cols: u32,
12 mi_rows: u32,
13 base_q_idx: u8,
14 frame_width: u32,
15 frame_height: u32,
16 enable_filter_intra: bool,
17 cdef: &CdefParams,
18 loop_filter: &LoopFilterParams,
19 tx_select: bool,
20 reduced_tx_set: bool,
21 allow_screen_content_tools: bool,
22 allow_intrabc: bool,
23 lr: &LoopRestorationParams,
24 fctx: &crate::decode::FrameCtx,
25) -> Result<Picture> {
26 let single_tile = TileInfo {
27 mi_col_starts: vec![0, mi_cols],
28 mi_row_starts: vec![0, mi_rows],
29 ..TileInfo::default()
30 };
31 decode_key_frame_tiles_lr(
32 &[data],
33 &single_tile,
34 mi_cols,
35 mi_rows,
36 base_q_idx,
37 frame_width,
38 frame_height,
39 enable_filter_intra,
40 cdef,
41 loop_filter,
42 tx_select,
43 reduced_tx_set,
44 allow_screen_content_tools,
45 allow_intrabc,
46 lr,
47 fctx,
48 )
49}
50
51/// [`decode_key_frame_tile_lr`] for a frame coded as SEVERAL tiles: the
52/// encoder's own filter search hands back one payload per tile plus the
53/// `tile_info` its header carries, and the per-tile CDF/LR/neighbour resets
54/// are the decoder's own (`decode_key_frame_tile_with_cdfs`).
55#[allow(clippy::too_many_arguments)]
56pub(crate) fn decode_key_frame_tiles_lr(
57 tiles: &[&[u8]],
58 tile_info: &TileInfo,
59 mi_cols: u32,
60 mi_rows: u32,
61 base_q_idx: u8,
62 frame_width: u32,
63 frame_height: u32,
64 enable_filter_intra: bool,
65 cdef: &CdefParams,
66 loop_filter: &LoopFilterParams,
67 tx_select: bool,
68 reduced_tx_set: bool,
69 allow_screen_content_tools: bool,
70 allow_intrabc: bool,
71 lr: &LoopRestorationParams,
72 fctx: &crate::decode::FrameCtx,
73) -> Result<Picture> {
74 let delta = DeltaParams::default();
75 decode_key_frame_tile_with_cdfs(
76 tiles,
77 tile_info,
78 mi_cols,
79 mi_rows,
80 base_q_idx,
81 crate::quant::QuantDeltas::default(),
82 frame_width,
83 frame_height,
84 enable_filter_intra,
85 false,
86 cdef,
87 loop_filter,
88 lr,
89 None,
90 tx_select,
91 reduced_tx_set,
92 allow_screen_content_tools,
93 allow_intrabc,
94 delta,
95 fctx,
96 )
97 .map(|(picture, _)| picture)
98}
99
100/// lane-superres r3: the real decoded margin beyond `frame_width` up to
101/// the mi-aligned `true_width`, set by the most recent
102/// [`decode_key_frame_tile_with_cdfs`] OR (lane-superres r10)
103/// [`decode_inter_frame_tile_with_cdfs`] call -- both frame kinds crop
104/// their reconstructed buffer down from the same mi-aligned extent, so
105/// they share this one slot. See each function's own comment at its
106/// write site. `None` when there was no margin to save.
107/// The margin the most recent key- or inter-frame tile decode stashed
108/// (see [`LAST_FRAME_WIDE_MARGIN`]) -- `stream.rs`'s superres path
109/// reads this immediately after the call, before any other frame
110/// decode can overwrite it.
111#[allow(dead_code)] // read only from the `#[cfg(test)]` gates
112pub(crate) fn take_last_frame_wide_margin(fctx: &crate::decode::FrameCtx) -> Option<Picture> {
113 fctx.last_frame_wide_margin.with(|m| m.borrow_mut().take())
114}
115
116/// [`decode_key_frame_tile`], threading a cross-frame CDF forward (spec
117/// 7.20's `load_cdfs`): `initial_cdfs` is the caller's forwarded state (from
118/// a prior frame's saved reference slot) when set, or the spec 8.4 defaults
119/// when `None` (the only case [`decode_key_frame_tile`] itself ever needs,
120/// since a key frame's own header always forces `primary_ref_frame ==
121/// PRIMARY_REF_NONE`). Returns the tile's own end-of-tile adapted table
122/// alongside the picture, for the caller to save into whichever reference
123/// slots this frame's `refresh_frame_flags` names.
124/// The order the 64x64 superblock quadrants of a tile are decoded in.
125///
126/// At `sb_size == 64` this is plain raster order over the tile's own
127/// superblock grid. At `sb_size == 128` libaom's `decode_partition` recurses
128/// each 128x128 superblock in raster order and visits its four 64x64
129/// quadrants (TL, TR, BL, BR) before moving to the next superblock, so the
130/// 64-level bodies must run in that nested order. `row_start` marks the first
131/// item of each *superblock* row (where the left neighbour context resets),
132/// `sb_start` the first item of each superblock (where the 128 root's own
133/// loop-restoration units and partition symbol are read).
134pub(super) fn sb_visit_order(
135 r0: u32,
136 r1: u32,
137 c0: u32,
138 c1: u32,
139 sb128: bool,
140) -> Vec<(u32, u32, bool, bool)> {
141 let mut out = Vec::new();
142 if !sb128 {
143 for r in r0..r1 {
144 for c in c0..c1 {
145 out.push((r, c, c == c0, true));
146 }
147 }
148 return out;
149 }
150 let mut r = r0;
151 while r < r1 {
152 let mut first_in_row = true;
153 let mut c = c0;
154 while c < c1 {
155 let mut first_in_sb = true;
156 for qr in 0..2 {
157 for qc in 0..2 {
158 let (rr, cc) = (r + qr, c + qc);
159 if rr >= r1 || cc >= c1 {
160 continue;
161 }
162 out.push((rr, cc, first_in_row, first_in_sb));
163 first_in_row = false;
164 first_in_sb = false;
165 }
166 }
167 c += 2;
168 }
169 r += 2;
170 }
171 out
172}
173
174thread_local! {
175 /// lane-av1muchunk422b: how many TX_32X32 chroma units the **compound**
176 /// `side > 64` mu-chunk walk allocated (`decode_inter_block`'s compound
177 /// arm, `mu_units(.., chroma_side * chroma_side)`). This is the walk
178 /// `lanes/av1muchunk422.report.md` called site 2 and could not reach at
179 /// 4:2:2; `CHROMA_SPLIT_TX_HITS` fires at all seventeen unit sites and
180 /// cannot attribute a bump to this arm. The single-reference twin (that
181 /// report's site 5) shares this exact source text, so the bump is routed
182 /// through `is_compound` and only this counter names site 2.
183 ///
184 /// Counted at the ALLOCATION, not at the `side > 64` guard: the arm's
185 /// lossless branch returns before the unit loop, so `W_intrabc.obu`
186 /// reaches the guard at ss (1,0) four times and allocates nothing -- a
187 /// guard-level counter would call that stream a witness of this walk.
188 pub(crate) static MU_CHUNK_COMPOUND_UNITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
189}
190
191thread_local! {
192 /// lane-av1muchunk422b: the intra-in-inter twin of
193 /// [`MU_CHUNK_COMPOUND_UNITS`] -- TX_32X32 chroma units allocated by the
194 /// intra-in-inter `side > 64` mu-chunk walk (site 8 of the same report).
195 /// Same allocation-vs-guard argument: `INTRA_128_IN_INTER_MU_CHROMA_HITS`
196 /// is bumped once per mu chunk BEFORE this arm's own lossless branch, so
197 /// it is not a witness of the per-unit walk, and `W_intrabc.obu` fires it
198 /// eight times at ss (1,0) with zero allocations.
199 pub(crate) static MU_CHUNK_INTRA_IN_INTER_UNITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
200}
201
202/// Current value of [`MU_CHUNK_COMPOUND_UNITS`], for a gate's before/after
203/// delta.
204pub fn mu_chunk_compound_units() -> usize {
205 MU_CHUNK_COMPOUND_UNITS.with(std::cell::Cell::get)
206}
207
208/// Current value of [`MU_CHUNK_INTRA_IN_INTER_UNITS`], for a gate's
209/// before/after delta.
210pub fn mu_chunk_intra_in_inter_units() -> usize {
211 MU_CHUNK_INTRA_IN_INTER_UNITS.with(std::cell::Cell::get)
212}
213
214thread_local! {
215 /// lane-av1h5: chunk-major chroma WALKS run for a 128-root plane block --
216 /// one per 64x64 mu chunk. libaom codes exactly 4 per unsplit 128x128 root
217 /// (`decode_token_recon_block`'s chunk loop), so a stream whose 128 roots
218 /// can be counted pins this exactly; the pre-fix redundancy read 64.
219 pub(crate) static MU_CHUNK_WALKS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
220 /// lane-av1h5: chroma transform units those walks read. On the pinned
221 /// 128x96 4:4:4 lossless stream, 1536 per inter frame (768 per plane),
222 /// against the oracle's 1536.
223 pub(crate) static MU_CHUNK_WALK_UNITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
224 /// lane-av1lm444loss: lossless blocks whose transform-unit grid was
225 /// CLIPPED at the frame edge -- `max_block_high`/`max_block_wide` cut the
226 /// grid short of the block's own `side / MI`, so libaom codes fewer TX_4X4
227 /// units than the block is wide/tall. Zero on any frame whose height and
228 /// width are multiples of the superblock, so a gate cannot look armed by
229 /// merely decoding.
230 pub(crate) static LOSSLESS_EDGE_CLIPPED: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
231 /// lane-av1lm444loss: the transform units that clip REMOVED -- the
232 /// `(side / MI - clipped) * (side / MI)` overflow the pre-fix walk read
233 /// past the frame bottom/right edge. On the 128x96 4:4:4 lossless pin this
234 /// is 256 per inter frame (8 unclipped luma rows x 32 columns), and 0 on a
235 /// frame that fits.
236 pub(crate) static LOSSLESS_EDGE_CLIP_UNITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
237 /// lane-av1mixllfork: sub-8 (4x8 / 8x4) inter leaves whose var-tx tree
238 /// was SUPPRESSED because their own segment is lossless -- libaom's
239 /// `parse_decode_block` carries `!xd->lossless[mbmi->segment_id]` as a
240 /// conjunct of the tree condition (`decodeframe.c:1237`), so a mixed-
241 /// lossless frame codes no `tx_size_cat1` symbol there. Zero on any frame
242 /// whose sub-8 leaves all sit in lossy segments, so a gate cannot look
243 /// armed by merely decoding.
244 ///
245 /// CORRECTED 2026-09-30 by the refutation pass (`lanes/refute-av1mixllconj`):
246 /// the hit was UNCONDITIONAL inside the else-if, which also serves leaves
247 /// whose `tx_select_inter` is off -- the pass measured **33 hits on a build
248 /// with the conjunct REMOVED**, i.e. it was counting a population the
249 /// conjunct does not touch. It is now guarded on `tx_select_inter`, the same
250 /// term the conjunct uses, so a hit means the suppression is the conjunct's
251 /// doing.
252 pub(crate) static SUB8_LOSSLESS_NO_VARTX: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
253}
254
255/// [`MU_CHUNK_WALKS`], for a gate's own before/after delta.
256#[allow(dead_code)] // read only from the `#[cfg(test)]` gates
257pub(crate) fn mu_chunk_walks() -> usize {
258 MU_CHUNK_WALKS.with(std::cell::Cell::get)
259}
260
261/// [`MU_CHUNK_WALK_UNITS`], for a gate's own before/after delta.
262#[allow(dead_code)] // read only from the `#[cfg(test)]` gates
263pub(crate) fn mu_chunk_walk_units() -> usize {
264 MU_CHUNK_WALK_UNITS.with(std::cell::Cell::get)
265}
266
267/// [`LOSSLESS_EDGE_CLIPPED`] -- lossless blocks whose TX grid the frame-edge
268/// clip shortened, for a gate's own before/after delta.
269#[allow(dead_code)] // read only from the `#[cfg(test)]` gates
270pub(crate) fn lossless_edge_clipped() -> usize {
271 LOSSLESS_EDGE_CLIPPED.with(std::cell::Cell::get)
272}
273
274/// [`LOSSLESS_EDGE_CLIP_UNITS`] -- transform units the clip removed, i.e. the
275/// ones the unclipped pre-fix walk read past the frame edge.
276#[allow(dead_code)] // read only from the `#[cfg(test)]` gates
277pub(crate) fn lossless_edge_clip_units() -> usize {
278 LOSSLESS_EDGE_CLIP_UNITS.with(std::cell::Cell::get)
279}
280/// [`SUB8_LOSSLESS_NO_VARTX`] -- sub-8 inter leaves on a lossless segment that
281/// read no var-tx symbol, for a gate's own before/after delta.
282#[allow(dead_code)] // read only from the `#[cfg(test)]` gates
283pub(crate) fn sub8_lossless_no_vartx() -> usize {
284 SUB8_LOSSLESS_NO_VARTX.with(std::cell::Cell::get)
285}
286
287// Signature mirrors the spec's per-call inputs; a parameter struct would only move them.
288#[allow(clippy::too_many_arguments)]
289pub(super) fn read_sb128_root(
290 dec: &mut SymbolDecoder,
291 cdfs: &mut Cdfs,
292 neighbours: &Neighbours,
293 lr: &LoopRestorationParams,
294 lr_grid: &mut crate::restoration::RestorationGrid,
295 lr_reference: &mut [(
296 crate::restoration::WienerInfo,
297 crate::restoration::SgrprojInfo,
298 ); 3],
299 sb_r: u32,
300 sb_c: u32,
301 mi_cols: u32,
302 mi_rows: u32,
303 fctx: &crate::decode::FrameCtx,
304) -> Result<usize> {
305 let (mi_r128, mi_c128) = ((sb_r & !1) * SB_MI, (sb_c & !1) * SB_MI);
306 crate::restoration::read_lr(
307 dec,
308 cdfs,
309 lr,
310 lr_grid,
311 lr_reference,
312 // `read_lr` is the ONE consumer that turns this superblock's
313 // mode-info origin into ABSOLUTE frame space -- it divides by the
314 // restoration-unit size, which is a real pixel count. The partition
315 // decode below is self-consistent with `mi_r128` as written (it
316 // only ever uses relative offsets from it), which is why a 128x128
317 // stream decoded correctly for 4:2:0 and 4:4:4 while placing its
318 // loop-restoration units at double the offset. `sb_r`/`sb_c` index
319 // 64px superblocks, so a 128px superblock's origin is half a
320 // `SB_MI` per 64px step -- `(sb_r & !1) * SB_MI` is twice the true
321 // mi row. The extent is `SB_MI`: a 128px superblock is 16 mi.
322 (sb_r & !1) * (SB_MI / 2),
323 (sb_c & !1) * (SB_MI / 2),
324 SB_MI,
325 fctx,
326 );
327 let at128 = ((mi_r128 / SUB_MI) as usize, (mi_c128 / SUB_MI) as usize);
328 let ctx128 = neighbours.partition_ctx(at128, 128);
329 let (has_cols128, has_rows128) = (mi_c128 + SB_MI < mi_cols, mi_r128 + SB_MI < mi_rows);
330 let part128 = if has_cols128 && has_rows128 {
331 hit!(PART128_SYMBOLS);
332 let p = dec.symbol(&mut cdfs.partition_w128[ctx128]);
333 if crate::envflags::env_flag!("EC_AV1_TRACE") {
334 eprintln!("TRACE partition_w128 ctx={ctx128} value={p}");
335 }
336 p
337 } else {
338 // lane-sb128 r4: the gathered half-out-of-frame reads DECIDE the
339 // partition, they are not a forced split -- exactly the discarded-bit
340 // defect the 64 root carries a comment about at `partition_w64`'s
341 // gathered arm. Value 1 is SPLIT; 0 is HORZ (rows out of frame) or
342 // VERT (cols out of frame), which the refusal below then names
343 // instead of silently reconstructing a split.
344 hit!(PART128_GATHERED_SYMBOLS);
345 match (has_cols128, has_rows128) {
346 (true, false) => {
347 if dec.symbol_fixed(&gather_of(&cdfs.partition_w128[ctx128], &VERT_ALIKE128)) == 1 {
348 PARTITION_SPLIT
349 } else {
350 hit!(PART128_EDGE_HORZ_HITS);
351 PARTITION_HORZ
352 }
353 }
354 (false, true) => {
355 if dec.symbol_fixed(&gather_of(&cdfs.partition_w128[ctx128], &HORZ_ALIKE128)) == 1 {
356 PARTITION_SPLIT
357 } else {
358 hit!(PART128_EDGE_VERT_HITS);
359 PARTITION_VERT
360 }
361 }
362 _ => {
363 PART128_GATHERED_SYMBOLS.with(|c| c.set(c.get() - 1));
364 bump_edge_both_cut(3);
365 PARTITION_SPLIT
366 }
367 }
368 };
369 // lane-sb128b r1: `PARTITION_NONE` -- one 128x128 block -- is decoded by
370 // the key-frame caller below. The six remaining arms (HORZ/VERT and the
371 // four AB shapes) still need the rect/AB machinery at this size.
372 // lane-sb128c r7: HORZ (two 128x64) and VERT (two 64x128) now decode on
373 // both tile paths. The four AB shapes (libaom `decode_partition`'s
374 // PARTITION_HORZ_A..VERT_B, decodeframe.c) still need the mixed
375 // 128x64 + 64x64 + 64x64 visit order and stay refused by name; a 128
376 // root has no 1:4 arm at all (`PARTITION_HORZ_4`/`VERT_4` are excluded
377 // for BLOCK_128X128 by `av1_get_partition_cdf`'s 8-symbol alphabet,
378 // which stops at VERT_B).
379 // lane-av1unwritten r4: guard the subsize THIS SYMBOL selected, never the
380 // root's own plane block. VERT on a 128x128 root selects BLOCK_64X128,
381 // whose 4:2:2 plane block is BLOCK_INVALID; the AB arms select 64x64
382 // quadrants (valid), so PARTITION_HORZ / PARTITION_NONE / SPLIT keep
383 // decoding untouched.
384 refuse_invalid_subsize((128, 128), part128, fctx)?;
385 match part128 {
386 PARTITION_SPLIT => hit!(PART128_SPLIT_HITS),
387 PARTITION_NONE => hit!(PART128_NONE_HITS),
388 PARTITION_HORZ => hit!(PART128_HORZ_HITS),
389 PARTITION_VERT => hit!(PART128_VERT_HITS),
390 // lane-sb128c r9: the four AB shapes at the 128 root -- one
391 // 128x64/64x128 half plus two 64x64 quadrants, decoded by both tile
392 // paths below in libaom `decode_partition`'s own visit order.
393 p if (PARTITION_HORZ_A..=PARTITION_VERT_B).contains(&p) => {
394 hit_do! {
395 PART128_AB_HITS.with(|c| {
396 let mut h = c.get();
397 h[p - PARTITION_HORZ_A] += 1;
398 c.set(h);
399 });
400 }
401 }
402 _ => {
403 return Err(unsupported(
404 "a 128x128 superblock partition value outside the 8-symbol alphabet",
405 ));
406 }
407 }
408 Ok(part128)
409}
410
411// Signature mirrors the spec's per-call inputs; a parameter struct would only move them.
412#[allow(clippy::too_many_arguments)]
413pub(crate) fn decode_key_frame_tile_with_cdfs(
414 tiles: &[&[u8]],
415 tile_info: &TileInfo,
416 mi_cols: u32,
417 mi_rows: u32,
418 base_q_idx: u8,
419 // lane-sbpart r11: this frame header's own per-plane DC/AC quantizer
420 // deltas (spec 5.9.12), threaded to [`QUANT_DELTAS`] at the top of every
421 // tile alongside [`CURRENT_Q_IDX`].
422 deltas: crate::quant::QuantDeltas,
423 frame_width: u32,
424 frame_height: u32,
425 enable_filter_intra: bool,
426 enable_edge_filter: bool,
427 cdef: &CdefParams,
428 loop_filter: &LoopFilterParams,
429 lr: &LoopRestorationParams,
430 initial_cdfs: Option<Cdfs>,
431 tx_select: bool,
432 reduced_tx_set: bool,
433 allow_screen_content_tools: bool,
434 allow_intrabc: bool,
435 // lane-realworld r4: this frame header's own `delta` (spec 5.9.17/5.9.18)
436 // -- only `q_present`/`q_res` are actually read ([`maybe_read_delta_q`]);
437 // `stream.rs` still refuses a frame with `lf_present` set before this
438 // function is ever called.
439 delta: DeltaParams,
440 fctx: &crate::decode::FrameCtx,
441) -> Result<(Picture, Cdfs)> {
442 // lane-frame80: [`TX_SELECT_INTER`] is set by the INTER tile decoder
443 // only, so a key frame decoded after an inter frame on this thread would
444 // otherwise inherit it and read its `tx_depth` context off the inter
445 // frame's stale `TXFM_CONTEXT` bands.
446 fctx.tx_select_inter.with(|c| c.set(false));
447 fctx.tx_select_frame.with(|c| c.set(tx_select));
448 // lane-av1-rect14: an intrabc block is `is_inter_block`, so its
449 // `tx_type` CDF is the INTER set selected by this frame's
450 // `reduced_tx_set`. Left at the cell default, the first coded
451 // 16x4 intrabc strip reads the wrong alphabet.
452 fctx.reduced_tx_set_inter.with(|c| c.set(reduced_tx_set));
453 // Every block of an intra-only frame publishes its resolved transform into
454 // the `TXFM_CONTEXT` bands exactly like an inter frame's does -- see
455 // [`Neighbours::fill_lf_grid_rect`].
456 fctx.intra_only.with(|c| c.set(true));
457 if mi_cols == 0 || mi_rows == 0 {
458 return Err(unsupported("a frame with no mode-info grid"));
459 }
460 fctx.enable_edge_filter.with(|f| f.set(enable_edge_filter));
461 let sb128 = sb128(fctx);
462 let (sb_cols, sb_rows) = (mi_cols.div_ceil(SB_MI), mi_rows.div_ceil(SB_MI));
463 fctx.intrabc_mi_grid.with(|g| {
464 *g.borrow_mut() = allow_intrabc.then(|| {
465 (
466 crate::mvstack::MiGrid::new(mi_cols as usize, mi_rows as usize),
467 mi_cols as usize,
468 mi_rows as usize,
469 )
470 });
471 });
472 fctx.intrabc_dv.with(|c| c.set(None));
473 fctx.cdef_bits.with(|c| c.set(cdef.bits));
474 fctx.cdef_sb_cols.with(|c| c.set(sb_cols as usize));
475 fctx.cdef_idx_grid
476 .with(|g| *g.borrow_mut() = vec![0u8; sb_cols as usize * sb_rows as usize]);
477 fctx.delta_q_present.with(|c| c.set(delta.q_present));
478 fctx.delta_q_res.with(|c| c.set(1i32 << delta.q_res));
479 fctx.delta_lf_present.with(|c| c.set(delta.lf_present));
480 fctx.delta_lf_res.with(|c| c.set(1i32 << delta.lf_res));
481 fctx.delta_lf_multi.with(|c| c.set(delta.lf_multi));
482 let (cols32, rows32) = block_grid(mi_cols, mi_rows);
483 let (true_width, true_height) = ((mi_cols * 4) as usize, (mi_rows * 4) as usize);
484 let (width, height) = (cols32 as usize * BLOCK, rows32 as usize * BLOCK);
485
486 let mut y = PlaneBuf {
487 data: std::borrow::Cow::Owned(fresh_plane(
488 width * height,
489 true_width,
490 true_height,
491 width,
492 height,
493 )),
494 width,
495 height,
496 true_width,
497 true_height,
498 tile_x0: 0,
499 tile_y0: 0,
500 tile_x1: width,
501 tile_y1: height,
502 plane: 0,
503 };
504 let (cw, ch) = (width >> ss_x(fctx), height >> ss_y(fctx));
505 note_frame_chroma_alloc((width, height), (cw, ch), (ss_x(fctx), ss_y(fctx)));
506 let (ctw, cth) = (true_width >> ss_x(fctx), true_height >> ss_y(fctx));
507 let mut u = PlaneBuf {
508 data: std::borrow::Cow::Owned(fresh_plane(cw * ch, ctw, cth, cw, ch)),
509 width: cw,
510 height: ch,
511 true_width: ctw,
512 true_height: cth,
513 tile_x0: 0,
514 tile_y0: 0,
515 tile_x1: cw,
516 tile_y1: ch,
517 plane: 1,
518 };
519 let mut v = PlaneBuf {
520 data: std::borrow::Cow::Owned(fresh_plane(cw * ch, ctw, cth, cw, ch)),
521 width: cw,
522 height: ch,
523 true_width: ctw,
524 true_height: cth,
525 tile_x0: 0,
526 tile_y0: 0,
527 tile_x1: cw,
528 tile_y1: ch,
529 plane: 2,
530 };
531
532 let scan32 = default_scan(TX32);
533 let scan16 = default_scan(TX16);
534 let scan8 = default_scan(TX8);
535 let scan4 = default_scan(TX4);
536
537 let base_cdfs = initial_cdfs.unwrap_or_else(|| Cdfs::new(q_ctx_of(base_q_idx)));
538 let mut result_cdfs = base_cdfs.clone();
539 let mut neighbours = Neighbours::new(
540 cols32 as usize * 2,
541 rows32 as usize * 2,
542 mi_cols as usize,
543 mi_rows as usize,
544 fctx,
545 );
546 // spec 7.16/libaom `av1_alloc_restoration_struct`: the unit grid is
547 // sized from `av1_get_upsampled_plane_size`, i.e. the UPSCALED width --
548 // loop restoration runs after the superres upscale.
549 let mut lr_grid = crate::restoration::RestorationGrid::with_ss(
550 lr,
551 superres(fctx).map_or(frame_width, |(w, _)| w),
552 frame_height,
553 fctx.subsampling_x.get(),
554 fctx.subsampling_y.get(),
555 );
556
557 // lane-tiles r2: each tile gets its own fresh `SymbolDecoder` over its
558 // own byte range and its own fresh copy of the frame's initial CDFs
559 // (spec 5.11.2 `decode_tile`'s `init_symbol`); only the tile named by
560 // `context_update_tile_id` has its end-of-tile adapted table kept as the
561 // frame's own output (spec `exit_symbol`) -- every other tile's
562 // adaptation is discarded, matching the refusal this replaces.
563 for (tile_idx, &tile_bytes) in tiles.iter().enumerate() {
564 let tile_num = tile_idx as u32;
565 let (trow, tcol) = (tile_num / tile_info.cols, tile_num % tile_info.cols);
566 let mi_row0 = tile_info.mi_row_starts[trow as usize];
567 let mi_row1 = tile_info.mi_row_starts[trow as usize + 1];
568 let mi_col0 = tile_info.mi_col_starts[tcol as usize];
569 let mi_col1 = tile_info.mi_col_starts[tcol as usize + 1];
570 // Tile boundaries are always superblock-aligned in mi units (spec
571 // 5.9.15's uniform/non-uniform derivation both only ever emit
572 // multiples of the superblock's own mi size), so plain division is
573 // exact here -- no rounding needed.
574 // `mi_row0`/`mi_col0` (this tile's own start, `0` or an interior
575 // tile boundary) are always superblock-aligned by spec 5.9.15; the
576 // frame's own true final edge (`mi_row_starts`/`mi_col_starts`'
577 // last entry) is not, so the upper bound needs `div_ceil` the same
578 // way the whole-frame `sb_rows`/`sb_cols` above does, to keep the
579 // trailing partial superblock row/column in scope.
580 let (sb_r0, sb_r1) = (
581 (mi_row0 / SB_MI).min(sb_rows),
582 mi_row1.div_ceil(SB_MI).min(sb_rows),
583 );
584 let (sb_c0, sb_c1) = (
585 (mi_col0 / SB_MI).min(sb_cols),
586 mi_col1.div_ceil(SB_MI).min(sb_cols),
587 );
588
589 // lane-t900 r19: spec 5.11.2 `decode_tile` resets `RefLrWiener` /
590 // `RefSgrXqd` to the midpoint filter at the top of EVERY tile, not
591 // once per frame. Carried across a tile boundary, the loop-restoration
592 // coefficients of the next tile decode off the previous tile's
593 // reference -- same symbol count, so entropy and reconstruction stay
594 // exact and only the filtered pixels of every tile after the first
595 // are wrong.
596 let mut lr_reference = [(
597 crate::restoration::WienerInfo::default(),
598 crate::restoration::SgrprojInfo::default(),
599 ); 3];
600 let mut cdfs = base_cdfs.clone();
601 // spec `decode_tile`: `CurrentQIndex` resets to the frame's own
602 // `base_q_idx` at the top of every tile, not once per frame.
603 fctx.current_q_idx.with(|c| c.set(i32::from(base_q_idx)));
604 fctx.quant_deltas.with(|c| c.set(deltas));
605 fctx.current_delta_lf.with(|c| c.set([0; 4]));
606 if crate::envflags::env_flag!("EC_AV1_TRACE") {
607 eprintln!(
608 "TRACE key_tile_bytes tile={tile_num} len={} first8={:02x?} base_q_idx={base_q_idx} mi_cols={mi_cols} mi_rows={mi_rows}",
609 tile_bytes.len(),
610 &tile_bytes[..tile_bytes.len().min(8)]
611 );
612 }
613 let mut dec = SymbolDecoder::new(tile_bytes);
614 // lane-av1txr: spec 5.9.2's `disable_cdf_update` -- a frame that sets
615 // it codes every symbol against the tables it started with.
616 dec.set_cdf_update(!cdf_update_disabled(fctx));
617 crate::census::register_cdfs(&cdfs);
618 neighbours.start_tile(mi_row0 as usize, mi_col0 as usize, mi_col1 as usize, fctx);
619 y.set_tile_origin(
620 mi_col0 as usize * 4,
621 mi_row0 as usize * 4,
622 (mi_col1 as usize * 4).min(y.width),
623 (mi_row1 as usize * 4).min(y.height),
624 );
625 u.set_tile_origin(
626 (mi_col0 as usize * 4) >> ss_x(fctx),
627 (mi_row0 as usize * 4) >> ss_y(fctx),
628 ((mi_col1 as usize * 4) >> ss_x(fctx)).min(u.width),
629 ((mi_row1 as usize * 4) >> ss_y(fctx)).min(u.height),
630 );
631 v.set_tile_origin(
632 (mi_col0 as usize * 4) >> ss_x(fctx),
633 (mi_row0 as usize * 4) >> ss_y(fctx),
634 ((mi_col1 as usize * 4) >> ss_x(fctx)).min(v.width),
635 ((mi_row1 as usize * 4) >> ss_y(fctx)).min(v.height),
636 );
637 hit!(TILE_HITS);
638
639 // lane-sb128b r1: set when the 128 root resolved to `PARTITION_NONE` --
640 // its one block covers all four 64x64 quadrants, so the remaining three
641 // visits of this superblock code nothing at all.
642 let mut sb128_none = false;
643 for (sb_r, sb_c, row_start, sb_start) in sb_visit_order(sb_r0, sb_r1, sb_c0, sb_c1, sb128) {
644 if row_start {
645 neighbours.start_row();
646 }
647 if sb_start {
648 sb128_none = false;
649 }
650 if sb128_none {
651 continue;
652 }
653 {
654 // lane-sb128 r1: the 128 root. Its loop-restoration units are read
655 // once for the whole 128x128 (spec 5.11.57 is called with the
656 // superblock's own size), then one partition symbol off the
657 // `BLOCK_128X128` CDF (8 symbols) decides the root -- only SPLIT
658 // recurses into the four 64x64 quadrants this decoder codes.
659 if sb128 && sb_start {
660 let part128 = read_sb128_root(
661 &mut dec,
662 &mut cdfs,
663 &neighbours,
664 lr,
665 &mut lr_grid,
666 &mut lr_reference,
667 sb_r,
668 sb_c,
669 mi_cols,
670 mi_rows,
671 fctx,
672 )?;
673 if (PARTITION_HORZ_A..=PARTITION_VERT_B).contains(&part128) {
674 // lane-sb128c r9: the four AB shapes at the 128 root.
675 // libaom `decode_partition` (decodeframe.c) codes each as
676 // two `bsize2` (64x64) squares plus one `subsize`
677 // (128x64 / 64x128) half, visited HORZ_A = TL, TR, bottom
678 // half; HORZ_B = top half, BL, BR; VERT_A = TL, BL, right
679 // half; VERT_B = left half, TR, BR. AB values only come
680 // from the FULL alphabet symbol, which is only read when
681 // both halves of the root are inside the frame, so no
682 // piece needs libaom's `i > 0` frame-edge break (a piece
683 // may still overhang the bottom/right, which
684 // `PlaneBuf::reconstruct`'s r8 clip handles).
685 sb128_none = true;
686 let at128 = ((sb_r & !1) as usize * 4, (sb_c & !1) as usize * 4);
687 let (br, bc) = (at128.0 + 4, at128.1 + 4);
688 let (q_r, q_c) = ((sb_r & !1) as usize, (sb_c & !1) as usize);
689 // Only the VERTICAL arms reorder the squares (TL, BL, TR,
690 // BR), so only they switch libaom's `has_tr_vert_*` /
691 // `has_bl_vert_*` tables (lane-sb128c r6 visit order).
692 let _vert_ab = (part128 == PARTITION_VERT_A || part128 == PARTITION_VERT_B)
693 .then(crate::encode::Reach::vert_ab_partition);
694 // One `cdef_idx` literal per CODED BLOCK, at the first
695 // non-skip one of each 64x64 CDEF unit; the 128x64 /
696 // 64x128 half covers two units and its read lands in the
697 // unit holding its origin, so that value is copied to the
698 // other one (libaom: every mi of the block points at the
699 // same `MB_MODE_INFO`).
700 let cdef_copy = |src: (usize, usize), dst: (usize, usize)| {
701 fctx.cdef_idx_grid.with(|g| {
702 let mut g = g.borrow_mut();
703 let cols = fctx.cdef_sb_cols.with(|c| c.get());
704 if cols == 0 {
705 return;
706 }
707 let (si, di) = (
708 (q_r + src.0) * cols + q_c + src.1,
709 (q_r + dst.0) * cols + q_c + dst.1,
710 );
711 if q_c + src.1 < cols
712 && q_c + dst.1 < cols
713 && si < g.len()
714 && di < g.len()
715 {
716 g[di] = g[si];
717 }
718 });
719 };
720 macro_rules! square64 {
721 ($at:expr) => {{
722 fctx.cdef_transmitted.with(|c| c.set(false));
723 decode_block(
724 &mut dec,
725 &mut cdfs,
726 &mut neighbours,
727 $at,
728 SB,
729 TxbSet::Luma64,
730 TxbSet::Chroma32,
731 TX32,
732 TX32,
733 (&scan32, &scan32),
734 false,
735 &mut y,
736 &mut u,
737 &mut v,
738 base_q_idx,
739 enable_filter_intra,
740 allow_screen_content_tools,
741 allow_intrabc,
742 &scan32,
743 &scan16,
744 &scan8,
745 &scan4,
746 tx_select,
747 reduced_tx_set,
748 fctx,
749 )?
750 }};
751 }
752 macro_rules! half128 {
753 ($at:expr, $bw:expr, $bh:expr, $src:expr, $dst:expr) => {{
754 fctx.cdef_transmitted.with(|c| c.set(false));
755 decode_block_128rect(
756 &mut dec,
757 &mut cdfs,
758 &mut neighbours,
759 $at,
760 $bw,
761 $bh,
762 &mut y,
763 &mut u,
764 &mut v,
765 base_q_idx,
766 enable_filter_intra,
767 allow_screen_content_tools,
768 tx_select,
769 reduced_tx_set,
770 fctx,
771 )?;
772 cdef_copy($src, $dst);
773 }};
774 }
775 match part128 {
776 PARTITION_HORZ_A => {
777 square64!(at128);
778 square64!((at128.0, bc));
779 half128!((br, at128.1), 128, 64, (1, 0), (1, 1));
780 }
781 PARTITION_HORZ_B => {
782 half128!(at128, 128, 64, (0, 0), (0, 1));
783 square64!((br, at128.1));
784 square64!((br, bc));
785 }
786 PARTITION_VERT_A => {
787 square64!(at128);
788 square64!((br, at128.1));
789 half128!((at128.0, bc), 64, 128, (0, 1), (1, 1));
790 }
791 _ => {
792 half128!(at128, 64, 128, (0, 0), (1, 0));
793 square64!((at128.0, bc));
794 square64!((br, bc));
795 }
796 }
797 continue;
798 }
799 if matches!(part128, PARTITION_HORZ | PARTITION_VERT) {
800 // lane-sb128c r8: libaom `decode_partition`
801 // (decodeframe.c:1554) codes PARTITION_HORZ at the 128
802 // root as `subsize = BLOCK_128X64` at (mi_row, mi_col)
803 // plus a second one at (mi_row + hbs, mi_col) only
804 // `if (mi_row + hbs < mi_rows)`; PARTITION_VERT mirrors it
805 // on the column. `hbs` is `mi_size_high[BLOCK_128X128]/2`
806 // = 16 mi. Both halves together cover the whole root, so
807 // the remaining three 64x64 quadrant visits code nothing
808 // (same flag the NONE arm sets).
809 sb128_none = true;
810 let horz = part128 == PARTITION_HORZ;
811 let at128 = ((sb_r & !1) as usize * 4, (sb_c & !1) as usize * 4);
812 let (bw, bh) = if horz { (128usize, 64usize) } else { (64, 128) };
813 for i in 0..2 {
814 let at = if horz {
815 (at128.0 + i * 4, at128.1)
816 } else {
817 (at128.0, at128.1 + i * 4)
818 };
819 let (blk_mi_r, blk_mi_c) = (at.0 * (SUB / MI), at.1 * (SUB / MI));
820 if i > 0
821 && (blk_mi_r >= mi_rows as usize || blk_mi_c >= mi_cols as usize)
822 {
823 break;
824 }
825 // Each half reads its OWN `cdef_idx` literal
826 // (libaom `read_cdef` keys the `-1` test on the
827 // block's own `MB_MODE_INFO`, and the two halves have
828 // different ones).
829 fctx.cdef_transmitted.with(|c| c.set(false));
830 decode_block_128rect(
831 &mut dec,
832 &mut cdfs,
833 &mut neighbours,
834 at,
835 bw,
836 bh,
837 &mut y,
838 &mut u,
839 &mut v,
840 base_q_idx,
841 enable_filter_intra,
842 allow_screen_content_tools,
843 tx_select,
844 reduced_tx_set,
845 fctx,
846 )?;
847 }
848 // One `cdef_idx` per BLOCK, copied to every 64x64 CDEF
849 // unit it covers (the inter path's own fan-out).
850 let (q_r, q_c) = ((sb_r & !1) as usize, (sb_c & !1) as usize);
851 fctx.cdef_idx_grid.with(|g| {
852 let mut g = g.borrow_mut();
853 let cols = fctx.cdef_sb_cols.with(|c| c.get());
854 if cols == 0 {
855 return;
856 }
857 for (src, dst) in if horz {
858 [((0, 0), (0, 1)), ((1, 0), (1, 1))]
859 } else {
860 [((0, 0), (1, 0)), ((0, 1), (1, 1))]
861 } {
862 let (si, di) = (
863 (q_r + src.0) * cols + q_c + src.1,
864 (q_r + dst.0) * cols + q_c + dst.1,
865 );
866 if q_c + src.1 < cols
867 && q_c + dst.1 < cols
868 && si < g.len()
869 && di < g.len()
870 {
871 g[di] = g[si];
872 }
873 }
874 });
875 continue;
876 }
877 if part128 == PARTITION_NONE {
878 // One 128x128 intra block: four TX_64X64 luma units and a
879 // single 64x64 chroma unit per plane (`av1_get_max_uv_txsize`
880 // of a 128x128 block is TX_64X64 over its 64x64 uv plane
881 // block). No CfL, no filter intra, no palette at this size --
882 // [`read_intra_mode`]'s own size gates already say so.
883 sb128_none = true;
884 fctx.cdef_transmitted.with(|c| c.set(false));
885 let at128 = ((sb_r & !1) as usize * 4, (sb_c & !1) as usize * 4);
886 decode_block(
887 &mut dec,
888 &mut cdfs,
889 &mut neighbours,
890 at128,
891 SB * 2,
892 TxbSet::Luma64,
893 TxbSet::Chroma32,
894 TX32,
895 TX32,
896 (scan32, scan32),
897 false,
898 &mut y,
899 &mut u,
900 &mut v,
901 base_q_idx,
902 enable_filter_intra,
903 allow_screen_content_tools,
904 allow_intrabc,
905 scan32,
906 scan16,
907 scan8,
908 scan4,
909 tx_select,
910 reduced_tx_set,
911 fctx,
912 )?;
913 // libaom writes one `cdef_strength` into the block's own
914 // `MB_MODE_INFO`, which every mi cell of the block --
915 // hence all four 64x64 CDEF units -- points at; our grid
916 // is per-unit, so the read (which only ever lands in the
917 // top-left unit, `read_cdef`'s index 0) is copied across.
918 let (q_r, q_c) = ((sb_r & !1) as usize, (sb_c & !1) as usize);
919 fctx.cdef_idx_grid.with(|g| {
920 let mut g = g.borrow_mut();
921 let cols = fctx.cdef_sb_cols.with(|c| c.get());
922 if cols > 0 && q_r * cols + q_c < g.len() {
923 let idx = g[q_r * cols + q_c];
924 for (dr, dc) in [(0, 1), (1, 0), (1, 1)] {
925 let i = (q_r + dr) * cols + q_c + dc;
926 if q_c + dc < cols && i < g.len() {
927 g[i] = idx;
928 }
929 }
930 }
931 });
932 continue;
933 }
934 }
935 if !sb128 {
936 crate::restoration::read_lr(
937 &mut dec,
938 &mut cdfs,
939 lr,
940 &mut lr_grid,
941 &mut lr_reference,
942 // True mode-info units: a 64px superblock is 8, and `SB_MI` is 16.
943 // `read_lr` divides by the restoration-unit size (a real pixel count),
944 // so it is the one consumer that needs the absolute origin in real mi.
945 // The half-mi convention these arguments used to carry only worked
946 // while `read_lr` also counted in half-mi -- the two cancelled.
947 sb_r * (SB_MI / 2),
948 sb_c * (SB_MI / 2),
949 SB_MI / 2,
950 fctx,
951 );
952 }
953 fctx.cdef_transmitted.with(|c| c.set(false));
954 let at = (sb_r as usize * 4, sb_c as usize * 4);
955 let ctx = neighbours.partition_ctx(at, SB);
956 let (has_cols, has_rows) = (
957 sb_c * SB_MI + SB_MI / 2 < mi_cols,
958 sb_r * SB_MI + SB_MI / 2 < mi_rows,
959 );
960 // Recomputed at this superblock's own half (spec
961 // `decode_partition`): a full alphabet symbol when both halves are
962 // inside the true frame, a single gathered bit when just one is
963 // (the superblock cannot be left whole, so this is forced split),
964 // and nothing at all (SPLIT inferred, no bits) when neither is —
965 // mirroring [`crate::tile::sb_coeff_key_frame_tile`]'s own
966 // three-way write exactly.
967 let part = if has_cols && has_rows {
968 if crate::envflags::env_flag!("EC_TRACE") {
969 eprintln!(
970 "EC_PART mi_row={} mi_col={} bsize=12 ctx={} tell={} rng={}",
971 sb_r * SB_MI,
972 sb_c * SB_MI,
973 ctx,
974 dec.debug_bitpos(),
975 dec.debug_state().0
976 );
977 }
978 let p = dec.symbol(&mut cdfs.partition_w64[ctx]);
979 if crate::envflags::env_flag!("EC_TRACE") {
980 eprintln!(
981 "EC_PART_VAL mi_row={} mi_col={} bsize=12 value={}",
982 sb_r * SB_MI,
983 sb_c * SB_MI,
984 p
985 );
986 }
987 if crate::envflags::env_flag!("EC_AV1_TRACE") {
988 eprintln!("TRACE partition_w64 ctx={ctx} value={p}");
989 }
990 p
991 } else {
992 // lane-hgkf r1 (class `parsed-then-discarded`, same finding as
993 // the unmerged lane-golomb r1): the gathered bit IS the
994 // partition -- libaom `ec_read_partition_impl` returns
995 // PARTITION_HORZ (resp. VERT) for 0 and only SPLIT for 1.
996 // Forcing SPLIT desynced the last superblock row of every
997 // frame whose height is not a multiple of 64: the ReelB key frames (3840x1608) diverge at mi_row=400 mi_col=0,
998 // the first block of that row.
999 match (has_cols, has_rows) {
1000 (true, false) => {
1001 trace_edge(
1002 (sb_r * SB_MI) as usize,
1003 (sb_c * SB_MI) as usize,
1004 12,
1005 ctx,
1006 dec.debug_state().0,
1007 );
1008 let b = dec.symbol_fixed(&gather(&cdfs.partition_w64[ctx], VERT_ALIKE));
1009 bump_edge32(4 + b.min(1));
1010 bump_edge_part(b.min(1));
1011 if b == 1 {
1012 PARTITION_SPLIT
1013 } else {
1014 PARTITION_HORZ
1015 }
1016 }
1017 (false, true) => {
1018 trace_edge(
1019 (sb_r * SB_MI) as usize,
1020 (sb_c * SB_MI) as usize,
1021 12,
1022 ctx,
1023 dec.debug_state().0,
1024 );
1025 let b = dec.symbol_fixed(&gather(&cdfs.partition_w64[ctx], HORZ_ALIKE));
1026 bump_edge32(4 + b.min(1));
1027 bump_edge_part(b.min(1));
1028 if b == 1 {
1029 PARTITION_SPLIT
1030 } else {
1031 PARTITION_VERT
1032 }
1033 }
1034 _ => {
1035 bump_edge_both_cut(2);
1036 PARTITION_SPLIT
1037 }
1038 }
1039 };
1040 // lane-av1unwritten r5: the partition symbol is RESOLVED (a full CDF read or
1041 // an edge `gather`) and about to be dispatched. `partition_subsize_dims` is
1042 // libaom's `get_partition_subsize` (common_data.h:71-90) reduced to
1043 // dimensions, so this is `decode_partition`'s pre-descend check
1044 // (decodeframe.c:1449-1458) applied to EVERY arm at once -- VERT,
1045 // VERT_A / VERT_B, HORZ_4 and VERT_4 included. The HORZ siblings keep
1046 // decoding because the helper is exact, not a heuristic. the 64x64 level binds its symbol to `part`.
1047 refuse_invalid_subsize((64, 64), part, fctx)?;
1048 match part {
1049 PARTITION_NONE => {
1050 decode_block(
1051 &mut dec,
1052 &mut cdfs,
1053 &mut neighbours,
1054 at,
1055 SB,
1056 TxbSet::Luma64,
1057 TxbSet::Chroma32,
1058 TX32,
1059 TX32,
1060 (scan32, scan32),
1061 false,
1062 &mut y,
1063 &mut u,
1064 &mut v,
1065 base_q_idx,
1066 enable_filter_intra,
1067 allow_screen_content_tools,
1068 allow_intrabc,
1069 scan32,
1070 scan16,
1071 scan8,
1072 scan4,
1073 tx_select,
1074 reduced_tx_set,
1075 fctx,
1076 )?;
1077 }
1078 PARTITION_SPLIT => {
1079 for q in 0..4 {
1080 let (r32, c32) = (sb_r * 2 + q / 2, sb_c * 2 + q % 2);
1081 if r32 >= rows32 || c32 >= cols32 {
1082 continue;
1083 }
1084 let at32 = (r32 as usize * 2, c32 as usize * 2);
1085 let ctx32 = neighbours.partition_ctx(at32, BLOCK);
1086 let (has_cols32, has_rows32) = (
1087 has_half(c32 * BLOCK_MI, BLOCK_MI, mi_cols),
1088 has_half(r32 * BLOCK_MI, BLOCK_MI, mi_rows),
1089 );
1090 let part32 = if has_cols32 && has_rows32 {
1091 if crate::envflags::env_flag!("EC_TRACE") {
1092 eprintln!(
1093 "EC_PART mi_row={} mi_col={} bsize=9 ctx={} tell={} rng={}",
1094 r32 * BLOCK_MI,
1095 c32 * BLOCK_MI,
1096 ctx32,
1097 dec.debug_bitpos(),
1098 dec.debug_state().0
1099 );
1100 }
1101 let p = dec.symbol(&mut cdfs.partition_w32[ctx32]);
1102 if crate::envflags::env_flag!("EC_TRACE") {
1103 eprintln!(
1104 "EC_PART_VAL mi_row={} mi_col={} bsize=9 value={}",
1105 r32 * BLOCK_MI,
1106 c32 * BLOCK_MI,
1107 p
1108 );
1109 }
1110 if crate::envflags::env_flag!("EC_AV1_TRACE") {
1111 let (rng, _) = dec.debug_state();
1112 eprintln!(
1113 "TRACE partition_w32 mi=({},{}) ctx={ctx32} value={p} rng={rng}",
1114 r32 * BLOCK_MI,
1115 c32 * BLOCK_MI
1116 );
1117 }
1118 p
1119 } else {
1120 // lane-hgkf r1: the 32-level frame-edge bit is the
1121 // partition too (same libaom reader as the
1122 // superblock level above).
1123 match (has_cols32, has_rows32) {
1124 (true, false) => {
1125 trace_edge(
1126 (r32 * BLOCK_MI) as usize,
1127 (c32 * BLOCK_MI) as usize,
1128 9,
1129 ctx32,
1130 dec.debug_state().0,
1131 );
1132 let b = dec.symbol_fixed(&gather(
1133 &cdfs.partition_w32[ctx32],
1134 VERT_ALIKE,
1135 ));
1136 bump_edge32(b.min(1));
1137 bump_edge_part(2 + b.min(1));
1138 if b == 1 {
1139 PARTITION_SPLIT
1140 } else {
1141 PARTITION_HORZ
1142 }
1143 }
1144 (false, true) => {
1145 trace_edge(
1146 (r32 * BLOCK_MI) as usize,
1147 (c32 * BLOCK_MI) as usize,
1148 9,
1149 ctx32,
1150 dec.debug_state().0,
1151 );
1152 let b = dec.symbol_fixed(&gather(
1153 &cdfs.partition_w32[ctx32],
1154 HORZ_ALIKE,
1155 ));
1156 bump_edge32(b.min(1));
1157 bump_edge_part(2 + b.min(1));
1158 if b == 1 {
1159 PARTITION_SPLIT
1160 } else {
1161 PARTITION_VERT
1162 }
1163 }
1164 _ => {
1165 bump_edge_both_cut(1);
1166 PARTITION_SPLIT
1167 }
1168 }
1169 };
1170 // lane-av1unwritten r4: VERT on a 32x32 block selects
1171 // BLOCK_16X32 -- INVALID at 4:2:2. HORZ on the same
1172 // block selects BLOCK_32X16, whose (1,0) cell is
1173 // BLOCK_16X16 (valid) and MUST keep decoding, so the
1174 // check is on the symbol's subsize, not the parent's.
1175 refuse_invalid_subsize((32, 32), part32, fctx)?;
1176 match part32 {
1177 PARTITION_NONE => {
1178 decode_block(
1179 &mut dec,
1180 &mut cdfs,
1181 &mut neighbours,
1182 at32,
1183 BLOCK,
1184 TxbSet::Luma32,
1185 TxbSet::Chroma16,
1186 TX32,
1187 TX16,
1188 (scan32, scan16),
1189 true,
1190 &mut y,
1191 &mut u,
1192 &mut v,
1193 base_q_idx,
1194 enable_filter_intra,
1195 allow_screen_content_tools,
1196 allow_intrabc,
1197 scan32,
1198 scan16,
1199 scan8,
1200 scan4,
1201 tx_select,
1202 reduced_tx_set,
1203 fctx,
1204 )?;
1205 }
1206 PARTITION_SPLIT => {
1207 for sub in 0..4 {
1208 let (sr, sc) = (
1209 r32 as usize * 2 + sub / 2,
1210 c32 as usize * 2 + sub % 2,
1211 );
1212 if (sr as u32) * SUB_MI >= mi_rows
1213 || (sc as u32) * SUB_MI >= mi_cols
1214 {
1215 continue;
1216 }
1217 let (has_cols16, has_rows16) = (
1218 has_half(sc as u32 * SUB_MI, SUB_MI, mi_cols),
1219 has_half(sr as u32 * SUB_MI, SUB_MI, mi_rows),
1220 );
1221 let at16 = (sr, sc);
1222 let ctx16 = neighbours.partition_ctx(at16, SUB);
1223 if has_cols16 && has_rows16 {
1224 if crate::envflags::env_flag!("EC_TRACE") {
1225 eprintln!(
1226 "EC_PART mi_row={} mi_col={} bsize=6 ctx={} tell={} rng={}",
1227 sr * SUB_MI as usize,
1228 sc * SUB_MI as usize,
1229 ctx16,
1230 dec.debug_bitpos(),
1231 dec.debug_state().0
1232 );
1233 }
1234 let part16 = dec.symbol(&mut cdfs.partition_w16[ctx16]);
1235 // lane-av1unwritten r6: the partition symbol is RESOLVED and about to be
1236 // dispatched. `partition_subsize_dims` is libaom's `get_partition_subsize`
1237 // (common_data.h:71-90) reduced to dimensions, so this is
1238 // `decode_partition`'s pre-descend check (decodeframe.c:1451-1460) on EVERY
1239 // arm -- VERT, VERT_A / VERT_B, HORZ_4 and VERT_4 -- and the HORZ siblings
1240 // keep decoding because the helper is exact, not a heuristic. key-frame 16x16 level.
1241 refuse_invalid_subsize((16, 16), part16, fctx)?;
1242
1243 if crate::envflags::env_flag!("EC_TRACE") {
1244 eprintln!(
1245 "EC_PART_VAL mi_row={} mi_col={} bsize=6 value={}",
1246 sr * SUB_MI as usize,
1247 sc * SUB_MI as usize,
1248 part16
1249 );
1250 }
1251 if crate::envflags::env_flag!("EC_AV1_TRACE") {
1252 eprintln!(
1253 "TRACE partition_w16 mi=({},{}) ctx={ctx16} value={part16}",
1254 sr * SUB_MI as usize,
1255 sc * SUB_MI as usize
1256 );
1257 }
1258 if part16 == PARTITION_HORZ || part16 == PARTITION_VERT
1259 {
1260 let (bw, bh) = if part16 == PARTITION_HORZ {
1261 (16, 8)
1262 } else {
1263 (8, 16)
1264 };
1265 if !chroma_plane_block_codable(
1266 bw,
1267 bh,
1268 ss_x(fctx),
1269 ss_y(fctx),
1270 ) {
1271 return Err(refuse_invalid_plane_block(
1272 bw, bh, fctx,
1273 ));
1274 }
1275 }
1276 if part16 == PARTITION_NONE {
1277 decode_block(
1278 &mut dec,
1279 &mut cdfs,
1280 &mut neighbours,
1281 at16,
1282 SUB,
1283 TxbSet::Luma16,
1284 TxbSet::Chroma8,
1285 TX16,
1286 TX8,
1287 (scan16, scan8),
1288 true,
1289 &mut y,
1290 &mut u,
1291 &mut v,
1292 base_q_idx,
1293 enable_filter_intra,
1294 allow_screen_content_tools,
1295 allow_intrabc,
1296 scan32,
1297 scan16,
1298 scan8,
1299 scan4,
1300 tx_select,
1301 reduced_tx_set,
1302 fctx,
1303 )?;
1304 continue;
1305 }
1306 if (PARTITION_HORZ_A..=PARTITION_VERT_B)
1307 .contains(&part16)
1308 {
1309 // lane-ab16 r1: the four AB (T-shaped) partitions at the 16x16
1310 // level -- two 8x8 squares plus one 16x8/8x16 strip. libaom's
1311 // `decode_partition` visit order (decodeframe.c):
1312 // HORZ_A: TL 8x8, TR 8x8, bottom 16x8
1313 // HORZ_B: top 16x8, BL 8x8, BR 8x8
1314 // VERT_A: TL 8x8, BL 8x8, right 8x16
1315 // VERT_B: left 8x16, TR 8x8, BR 8x8
1316 // The squares go through `decode_leaf8`, the strip through
1317 // `decode_leaf_rect` (real, non-skip 16x8/8x16 coefficients --
1318 // lane-rectx r2-r4; the older `decode_block_rect` this VERT_B arm
1319 // used refuses the non-skip case at this size). Both writers keep
1320 // mi-granular mode/skip/palette records, so a later block's
1321 // `above_mi`/`left_mi` sees the real sub-block, not the coarse
1322 // 16x16 cell.
1323 //
1324 // VERT_A/VERT_B visit TL,BL,TR,BR, so the bottom-left square's
1325 // top-right neighbour is the partition's own not-yet-decoded
1326 // rectangle: libaom switches those squares onto `has_tr_vert_*`/
1327 // `has_bl_vert_*` (`reconintra.c` `get_has_tr_table`), which is
1328 // `Reach::vert_ab_partition()` (lane-part32 r5, cherry-picked here).
1329 let arm = part16 - PARTITION_HORZ_A;
1330 bump_ab16(arm);
1331 if part16 == PARTITION_VERT_B {
1332 hit!(VERT_B_INTRA_HITS);
1333 }
1334 let _vert_guard = if part16 == PARTITION_VERT_A
1335 || part16 == PARTITION_VERT_B
1336 {
1337 Some(crate::encode::Reach::vert_ab_partition())
1338 } else {
1339 None
1340 };
1341 let (mi_row0, mi_col0) =
1342 (sr * SUB_MI as usize, sc * SUB_MI as usize);
1343 macro_rules! ab_leaf8 {
1344 ($mi:expr, $prev:expr) => {
1345 decode_leaf8(
1346 &mut dec,
1347 &mut cdfs,
1348 &mut neighbours,
1349 at16,
1350 $mi,
1351 (&scan8, &scan4),
1352 $prev,
1353 &mut y,
1354 &mut u,
1355 &mut v,
1356 base_q_idx,
1357 enable_filter_intra,
1358 allow_screen_content_tools,
1359 allow_intrabc,
1360 tx_select,
1361 reduced_tx_set,
1362 fctx,
1363 )?
1364 };
1365 }
1366 macro_rules! ab_strip {
1367 ($mi:expr, $bw:expr, $bh:expr, $prev:expr) => {
1368 decode_leaf_rect(
1369 &mut dec,
1370 &mut cdfs,
1371 &mut neighbours,
1372 at16,
1373 $mi,
1374 $bw,
1375 $bh,
1376 $prev,
1377 &mut y,
1378 &mut u,
1379 &mut v,
1380 enable_filter_intra,
1381 allow_screen_content_tools,
1382 base_q_idx,
1383 tx_select,
1384 reduced_tx_set,
1385 fctx,
1386 )?
1387 };
1388 }
1389 let tl = (mi_row0, mi_col0);
1390 let tr = (mi_row0, mi_col0 + 2);
1391 let bl = (mi_row0 + 2, mi_col0);
1392 let br = (mi_row0 + 2, mi_col0 + 2);
1393 let mut done: Vec<((usize, usize), usize)> =
1394 Vec::new();
1395 match part16 {
1396 PARTITION_HORZ_A => {
1397 let m_tl =
1398 ab_leaf8!(tl, done.last().copied());
1399 done.push((tl, m_tl));
1400 let m_tr =
1401 ab_leaf8!(tr, done.last().copied());
1402 done.push((tr, m_tr));
1403 let m_bot =
1404 ab_strip!(bl, 16, 8, Some((tl, m_tl)));
1405 done.push((bl, m_bot));
1406 }
1407 PARTITION_HORZ_B => {
1408 let m_top = ab_strip!(tl, 16, 8, None);
1409 done.push((tl, m_top));
1410 let m_bl =
1411 ab_leaf8!(bl, done.last().copied());
1412 done.push((bl, m_bl));
1413 let m_br =
1414 ab_leaf8!(br, done.last().copied());
1415 done.push((br, m_br));
1416 }
1417 PARTITION_VERT_A => {
1418 let m_tl =
1419 ab_leaf8!(tl, done.last().copied());
1420 done.push((tl, m_tl));
1421 let m_bl =
1422 ab_leaf8!(bl, done.last().copied());
1423 done.push((bl, m_bl));
1424 let m_right =
1425 ab_strip!(tr, 8, 16, Some((tl, m_tl)));
1426 done.push((tr, m_right));
1427 }
1428 _ => {
1429 let m_left = ab_strip!(tl, 8, 16, None);
1430 done.push((tl, m_left));
1431 let m_tr =
1432 ab_leaf8!(tr, done.last().copied());
1433 done.push((tr, m_tr));
1434 let m_br =
1435 ab_leaf8!(br, done.last().copied());
1436 done.push((br, m_br));
1437 }
1438 }
1439 // Same coarse-slot write-back the real SPLIT path takes: the
1440 // 16x16 cell's single `above_mode`/`left_mode` entry gets the
1441 // sub-block on the parent's bottom / right edge (the mi-exact map
1442 // above wins wherever it has an entry).
1443 write_back_split_modes(
1444 &mut neighbours,
1445 &done,
1446 (mi_row0, mi_col0),
1447 (sr, sc),
1448 );
1449 continue;
1450 }
1451 if part16 == PARTITION_HORZ {
1452 hit!(HORZ_VERT_INTRA_HITS);
1453 let (mi_row0, mi_col0) =
1454 (sr as u32 * SUB_MI, sc as u32 * SUB_MI);
1455 let top = (mi_row0 as usize, mi_col0 as usize);
1456 let bot = (mi_row0 as usize + 2, mi_col0 as usize);
1457 let mode_top = decode_leaf_rect(
1458 &mut dec,
1459 &mut cdfs,
1460 &mut neighbours,
1461 at16,
1462 top,
1463 16,
1464 8,
1465 None,
1466 &mut y,
1467 &mut u,
1468 &mut v,
1469 enable_filter_intra,
1470 allow_screen_content_tools,
1471 base_q_idx,
1472 tx_select,
1473 reduced_tx_set,
1474 fctx,
1475 )?;
1476 let mode_bot = decode_leaf_rect(
1477 &mut dec,
1478 &mut cdfs,
1479 &mut neighbours,
1480 at16,
1481 bot,
1482 16,
1483 8,
1484 Some((top, mode_top)),
1485 &mut y,
1486 &mut u,
1487 &mut v,
1488 enable_filter_intra,
1489 allow_screen_content_tools,
1490 base_q_idx,
1491 tx_select,
1492 reduced_tx_set,
1493 fctx,
1494 )?;
1495 neighbours.above_mode[sc] = mode_bot;
1496 neighbours.left_mode[sr] = mode_bot;
1497 continue;
1498 }
1499 if part16 == PARTITION_VERT {
1500 hit!(HORZ_VERT_INTRA_HITS);
1501 let (mi_row0, mi_col0) =
1502 (sr as u32 * SUB_MI, sc as u32 * SUB_MI);
1503 let left = (mi_row0 as usize, mi_col0 as usize);
1504 let right =
1505 (mi_row0 as usize, mi_col0 as usize + 2);
1506 let mode_left = decode_leaf_rect(
1507 &mut dec,
1508 &mut cdfs,
1509 &mut neighbours,
1510 at16,
1511 left,
1512 8,
1513 16,
1514 None,
1515 &mut y,
1516 &mut u,
1517 &mut v,
1518 enable_filter_intra,
1519 allow_screen_content_tools,
1520 base_q_idx,
1521 tx_select,
1522 reduced_tx_set,
1523 fctx,
1524 )?;
1525 let mode_right = decode_leaf_rect(
1526 &mut dec,
1527 &mut cdfs,
1528 &mut neighbours,
1529 at16,
1530 right,
1531 8,
1532 16,
1533 Some((left, mode_left)),
1534 &mut y,
1535 &mut u,
1536 &mut v,
1537 enable_filter_intra,
1538 allow_screen_content_tools,
1539 base_q_idx,
1540 tx_select,
1541 reduced_tx_set,
1542 fctx,
1543 )?;
1544 neighbours.above_mode[sc] = mode_right;
1545 neighbours.left_mode[sr] = mode_right;
1546 continue;
1547 }
1548 if part16 == PARTITION_HORZ_4
1549 || part16 == PARTITION_VERT_4
1550 {
1551 // lane-av1unwritten r6: arm census --
1552 // which partition values reach here.
1553 note_subsize_arm(
1554 part16,
1555 chroma_plane_block_codable(
1556 partition_subsize_dims((16, 16), part16).0,
1557 partition_subsize_dims((16, 16), part16).1,
1558 ss_x(fctx),
1559 ss_y(fctx),
1560 ),
1561 );
1562 decode_rect4_16(
1563 &mut dec,
1564 &mut cdfs,
1565 &mut neighbours,
1566 at16,
1567 part16 == PARTITION_HORZ_4,
1568 &mut y,
1569 &mut u,
1570 &mut v,
1571 enable_filter_intra,
1572 allow_screen_content_tools,
1573 base_q_idx,
1574 tx_select,
1575 reduced_tx_set,
1576 fctx,
1577 )?;
1578 continue;
1579 }
1580 // lane-r14 r2 (verifier note): every
1581 // one of the 10 symbols this CDF can
1582 // produce is handled above, so SPLIT
1583 // is the only value that reaches
1584 // here -- the refusal that used to
1585 // stand here was unreachable and is
1586 // gone from `refusal_inventory`.
1587 debug_assert_eq!(part16, PARTITION_SPLIT);
1588 // A real (non-straddle) SPLIT of a
1589 // whole 16x16 into four 8x8 leaves --
1590 // the same recursion the straddle
1591 // path below already runs when the
1592 // true frame edge forces it, just
1593 // over all four positions instead of
1594 // the one or two the edge leaves.
1595 let (mi_row0, mi_col0) =
1596 (sr as u32 * SUB_MI, sc as u32 * SUB_MI);
1597 // Each 8x8 leaf's intra-mode context
1598 // must see its own siblings: the
1599 // bottom-left leaf's ABOVE neighbour
1600 // is leaf 0, not the previously
1601 // decoded leaf 1 (and the bottom-right
1602 // leaf's above is leaf 1, while
1603 // `prev_leaf` only ever carried the
1604 // immediately preceding one). The
1605 // coarse `above_mode`/`left_mode`
1606 // arrays hold one slot per 16x16, so
1607 // the sibling modes are swapped in
1608 // around each leaf call and restored
1609 // after; the loop tail writes the last
1610 // leaf's mode as before.
1611 let mut sib_modes: [Option<(usize, usize)>; 4] =
1612 [None; 4];
1613 for i in 0..4 {
1614 let (mr, mc) =
1615 (mi_row0 + (i / 2) * 2, mi_col0 + (i % 2) * 2);
1616 let leaf_mi = (mr as usize, mc as usize);
1617 let li = i as usize;
1618 let saved_modes = (
1619 neighbours.above_mode[sc],
1620 neighbours.left_mode[sr],
1621 );
1622 if li >= 2
1623 && let Some((below, _)) = sib_modes[li - 2]
1624 {
1625 neighbours.above_mode[sc] = below;
1626 }
1627 if li % 2 == 1
1628 && let Some((_, right)) = sib_modes[li - 1]
1629 {
1630 neighbours.left_mode[sr] = right;
1631 }
1632 let leaf_ctx =
1633 neighbours.partition_ctx_mi(leaf_mi, 8);
1634 if crate::envflags::env_flag!("EC_TRACE") {
1635 eprintln!(
1636 "EC_PART mi_row={} mi_col={} bsize=3 ctx={} tell={} rng={}",
1637 leaf_mi.0,
1638 leaf_mi.1,
1639 leaf_ctx,
1640 dec.debug_bitpos(),
1641 dec.debug_state().0
1642 );
1643 }
1644 let part8 =
1645 dec.symbol(&mut cdfs.partition_w8[leaf_ctx]);
1646 // lane-av1unwritten r9: the
1647 // SECOND key-frame 8x8 resolution.
1648 // Found by the structural scan, which
1649 // r9 made sensitive to a second
1650 // site at an already covered level.
1651 refuse_invalid_subsize((8, 8), part8, fctx)?;
1652 if crate::envflags::env_flag!("EC_TRACE") {
1653 eprintln!(
1654 "EC_PART_VAL mi_row={} mi_col={} bsize=3 value={}",
1655 leaf_mi.0, leaf_mi.1, part8
1656 );
1657 }
1658 if crate::envflags::env_flag!("EC_AV1_TRACE") {
1659 eprintln!(
1660 "TRACE partition_w8 mi=({mr},{mc}) ctx={leaf_ctx} value={part8}"
1661 );
1662 }
1663 if part8 == PARTITION_HORZ
1664 || part8 == PARTITION_VERT
1665 {
1666 // libaom's `subsize_lookup`: HORZ on an 8x8 is
1667 // BLOCK_8X4, VERT is BLOCK_4X8.
1668 let (bw, bh) = if part8 == PARTITION_HORZ {
1669 (8, 4)
1670 } else {
1671 (4, 8)
1672 };
1673 if !chroma_plane_block_codable(
1674 bw,
1675 bh,
1676 ss_x(fctx),
1677 ss_y(fctx),
1678 ) {
1679 return Err(refuse_invalid_plane_block(
1680 bw, bh, fctx,
1681 ));
1682 }
1683 }
1684 let leaf_mode = if part8 == PARTITION_NONE {
1685 decode_leaf8(
1686 &mut dec,
1687 &mut cdfs,
1688 &mut neighbours,
1689 at16,
1690 leaf_mi,
1691 (scan8, scan4),
1692 None,
1693 &mut y,
1694 &mut u,
1695 &mut v,
1696 base_q_idx,
1697 enable_filter_intra,
1698 allow_screen_content_tools,
1699 allow_intrabc,
1700 tx_select,
1701 reduced_tx_set,
1702 fctx,
1703 )
1704 .map(|m| (m, m))?
1705 } else if part8 == PARTITION_SPLIT {
1706 decode_leaf_split4(
1707 &mut dec,
1708 &mut cdfs,
1709 &mut neighbours,
1710 at16,
1711 leaf_mi,
1712 scan4,
1713 None,
1714 &mut y,
1715 &mut u,
1716 &mut v,
1717 base_q_idx,
1718 enable_filter_intra,
1719 allow_intrabc,
1720 reduced_tx_set,
1721 fctx,
1722 )?
1723 } else if part8 == PARTITION_HORZ
1724 || part8 == PARTITION_VERT
1725 {
1726 decode_leaf_rect8(
1727 &mut dec,
1728 &mut cdfs,
1729 &mut neighbours,
1730 at16,
1731 leaf_mi,
1732 part8 == PARTITION_VERT,
1733 scan4,
1734 &mut y,
1735 &mut u,
1736 &mut v,
1737 base_q_idx,
1738 enable_filter_intra,
1739 allow_intrabc,
1740 tx_select,
1741 reduced_tx_set,
1742 fctx,
1743 )?
1744 } else {
1745 unreachable!(
1746 "partition_w8 is a 4-symbol CDF: NONE/HORZ/VERT/SPLIT only"
1747 );
1748 };
1749 neighbours.above_mode[sc] = saved_modes.0;
1750 neighbours.left_mode[sr] = saved_modes.1;
1751 sib_modes[li] = Some(leaf_mode);
1752 }
1753 if let Some((below, _)) =
1754 sib_modes[2].or(sib_modes[3]).or(sib_modes[0])
1755 {
1756 neighbours.above_mode[sc] = below;
1757 }
1758 if let Some((_, right)) =
1759 sib_modes[1].or(sib_modes[3]).or(sib_modes[0])
1760 {
1761 neighbours.left_mode[sr] = right;
1762 }
1763 continue;
1764 }
1765 // The true edge falls inside this 16x16
1766 // leaf itself (mod-32==8 target sizes,
1767 // lane-av1-rect): one axis only -- an 8x8
1768 // leaf never itself straddles, so the
1769 // block splits cleanly along whichever
1770 // axis is short.
1771 // lane-edgeboth r1: when the true frame
1772 // edge cuts BOTH axes libaom's
1773 // `decode_partition` takes
1774 // `else if (!has_rows && !has_cols)
1775 // partition = PARTITION_SPLIT;` -- no
1776 // symbol of any kind is read, and the four
1777 // 8x8 children are then visited by the
1778 // same loop below, whose
1779 // `mr < mi_rows && mc < mi_cols` filter IS
1780 // libaom's own
1781 // `if (mi_row >= mi_rows || mi_col >= mi_cols) return;`
1782 // early return, so the three out-of-frame
1783 // quadrants get no neighbour or tx-grid
1784 // write.
1785 if !has_cols16 && !has_rows16 {
1786 bump_edge_both_cut(0);
1787 } else {
1788 // lane-golomb r1 (class
1789 // `parsed-then-discarded`): the gathered
1790 // bit IS the partition -- 0 names
1791 // PARTITION_HORZ (resp. VERT), only 1 is
1792 // SPLIT (libaom `ec_read_partition_impl`).
1793 // This path can only walk the four SPLIT
1794 // leaves, so a 0 refuses by name instead
1795 // of desyncing.
1796 trace_edge(
1797 sr * SUB_MI as usize,
1798 sc * SUB_MI as usize,
1799 6,
1800 ctx16,
1801 dec.debug_state().0,
1802 );
1803 let edge_split = if has_cols16 {
1804 dec.symbol_fixed(&gather(
1805 &cdfs.partition_w16[ctx16],
1806 VERT_ALIKE,
1807 ))
1808 } else {
1809 dec.symbol_fixed(&gather(
1810 &cdfs.partition_w16[ctx16],
1811 HORZ_ALIKE,
1812 ))
1813 };
1814 // lane-hgkf r1: slots 2/3 of
1815 // [`EDGE_PART_HITS`], the gate's proof the
1816 // non-SPLIT edge arm actually decoded.
1817 bump_edge_part(if edge_split == 1 { 3 } else { 2 });
1818 if edge_split == 0 {
1819 // lane-oddh r1: the strip IS decodable
1820 // -- libaom's `decode_partition` codes
1821 // only the FIRST half of an edge
1822 // HORZ/VERT (the second call is
1823 // guarded by the same has_rows/
1824 // has_cols test that made the
1825 // partition non-square), so one
1826 // 16x8 (resp. 8x16) strip covers the
1827 // whole visible band of a frame whose
1828 // height (resp. width) is 8 mod 16 --
1829 // the user's 3840x1608 film.
1830 hit!(HORZ_VERT_INTRA_HITS);
1831 hit!(EDGE_RECT_STRIP_HITS);
1832 // lane-av1subsizesweep: this arm is the ONE
1833 // frame-edge partition read in the file that
1834 // dispatches on its own symbol instead of
1835 // resolving it into a `partN` the level's guard
1836 // covers. The 128-root edge arms
1837 // (decode.rs:35559/35567), the 64-level ones
1838 // (36247/36264) and the 32-level ones
1839 // (36371/36391) all end in `PARTITION_HORZ` /
1840 // `PARTITION_VERT` / `PARTITION_SPLIT`, which
1841 // the guard after their `match` sees -- the
1842 // inter-frame 16-level edge arms (54777/54790)
1843 // resolve into `part16` for the same reason.
1844 // Here `edge_split` goes straight into
1845 // `decode_leaf_rect`, and the shape it carries is
1846 // `BLOCK_8X16` when the COLUMN is short --
1847 // whose (1,0) chroma plane block is
1848 // BLOCK_INVALID (`av1_ss_size_lookup`,
1849 // common_data.c:20), so at 4:2:2 this walks a
1850 // subsize libaom refuses at decodeframe.c:1456.
1851 //
1852 // The check names the LEVEL, not a hand-picked
1853 // shape: the gathered bit resolves to
1854 // PARTITION_HORZ when the columns are full and
1855 // PARTITION_VERT when they are not, and
1856 // `refuse_invalid_subsize` reduces that through
1857 // the same `partition_subsize_dims` every other
1858 // site uses. So the 16x8 strip -- BLOCK_8X16's
1859 // mirror, whose (1,0) cell is BLOCK_8X8 and MUST
1860 // keep decoding -- is untouched, and the guard
1861 // reds on the cell list, not a shape list.
1862 let edge_part = if has_cols16 {
1863 PARTITION_HORZ
1864 } else {
1865 PARTITION_VERT
1866 };
1867 refuse_invalid_subsize((16, 16), edge_part, fctx)?;
1868 // lane-av1unwritten r6: arm census -- which
1869 // partition value reaches here.
1870 note_subsize_arm(
1871 edge_part,
1872 chroma_plane_block_codable(
1873 partition_subsize_dims((16, 16), edge_part)
1874 .0,
1875 partition_subsize_dims((16, 16), edge_part)
1876 .1,
1877 ss_x(fctx),
1878 ss_y(fctx),
1879 ),
1880 );
1881 let (mi_row0, mi_col0) =
1882 (sr as u32 * SUB_MI, sc as u32 * SUB_MI);
1883 let (bw, bh) =
1884 if has_cols16 { (16, 8) } else { (8, 16) };
1885 let mode = decode_leaf_rect(
1886 &mut dec,
1887 &mut cdfs,
1888 &mut neighbours,
1889 at16,
1890 (mi_row0 as usize, mi_col0 as usize),
1891 bw,
1892 bh,
1893 None,
1894 &mut y,
1895 &mut u,
1896 &mut v,
1897 enable_filter_intra,
1898 allow_screen_content_tools,
1899 base_q_idx,
1900 tx_select,
1901 reduced_tx_set,
1902 fctx,
1903 )?;
1904 neighbours.above_mode[sc] = mode;
1905 neighbours.left_mode[sr] = mode;
1906 continue;
1907 }
1908 }
1909 let (mi_row0, mi_col0) =
1910 (sr as u32 * SUB_MI, sc as u32 * SUB_MI);
1911 let leaf_positions: Vec<(u32, u32)> = (0..4)
1912 .map(|i| (mi_row0 + (i / 2) * 2, mi_col0 + (i % 2) * 2))
1913 .filter(|&(mr, mc)| mr < mi_rows && mc < mi_cols)
1914 .collect();
1915 // See the sibling-mode note in the clean
1916 // SPLIT path above.
1917 let mut sib_modes: [Option<(usize, usize)>; 4] = [None; 4];
1918 for (mr, mc) in leaf_positions {
1919 let leaf_mi = (mr as usize, mc as usize);
1920 let li = (((mr - mi_row0) / 2) * 2 + (mc - mi_col0) / 2)
1921 as usize;
1922 let saved_modes = (
1923 neighbours.above_mode[sc],
1924 neighbours.left_mode[sr],
1925 );
1926 if li >= 2
1927 && let Some((below, _)) = sib_modes[li - 2]
1928 {
1929 neighbours.above_mode[sc] = below;
1930 }
1931 if li % 2 == 1
1932 && let Some((_, right)) = sib_modes[li - 1]
1933 {
1934 neighbours.left_mode[sr] = right;
1935 }
1936 let leaf_ctx = neighbours.partition_ctx_mi(leaf_mi, 8);
1937 let pre8 = dec.debug_state().0;
1938 let part8 =
1939 dec.symbol(&mut cdfs.partition_w8[leaf_ctx]);
1940 // lane-av1unwritten r9: key-frame 8x8
1941 // level. A VERT here dispatches
1942 // `decode_leaf_rect8` on a raw `part8`
1943 // with no guard anywhere above it --
1944 // `BLOCK_4X8`, which is INVALID at
1945 // 4:2:2 (the shape r5 and r6 each
1946 // flagged). Not corpus-reachable at
1947 // ss(1,0) today, so it is residue rather
1948 // than live pixels, and it is guarded
1949 // anyway.
1950 refuse_invalid_subsize((8, 8), part8, fctx)?;
1951 if crate::envflags::env_flag!("EC_AV1_TRACE") {
1952 eprintln!(
1953 "TRACE partition_w8 mi=({mr},{mc}) ctx={leaf_ctx} value={part8} pre_rng={pre8}"
1954 );
1955 }
1956 let leaf_mode = if part8 == PARTITION_NONE {
1957 decode_leaf8(
1958 &mut dec,
1959 &mut cdfs,
1960 &mut neighbours,
1961 at16,
1962 leaf_mi,
1963 (scan8, scan4),
1964 None,
1965 &mut y,
1966 &mut u,
1967 &mut v,
1968 base_q_idx,
1969 enable_filter_intra,
1970 allow_screen_content_tools,
1971 allow_intrabc,
1972 tx_select,
1973 reduced_tx_set,
1974 fctx,
1975 )
1976 .map(|m| (m, m))?
1977 } else if part8 == PARTITION_SPLIT {
1978 decode_leaf_split4(
1979 &mut dec,
1980 &mut cdfs,
1981 &mut neighbours,
1982 at16,
1983 leaf_mi,
1984 scan4,
1985 None,
1986 &mut y,
1987 &mut u,
1988 &mut v,
1989 base_q_idx,
1990 enable_filter_intra,
1991 allow_intrabc,
1992 reduced_tx_set,
1993 fctx,
1994 )?
1995 } else if part8 == PARTITION_HORZ
1996 || part8 == PARTITION_VERT
1997 {
1998 decode_leaf_rect8(
1999 &mut dec,
2000 &mut cdfs,
2001 &mut neighbours,
2002 at16,
2003 leaf_mi,
2004 part8 == PARTITION_VERT,
2005 scan4,
2006 &mut y,
2007 &mut u,
2008 &mut v,
2009 base_q_idx,
2010 enable_filter_intra,
2011 allow_intrabc,
2012 tx_select,
2013 reduced_tx_set,
2014 fctx,
2015 )?
2016 } else {
2017 unreachable!(
2018 "partition_w8 is a 4-symbol CDF: NONE/HORZ/VERT/SPLIT only"
2019 );
2020 };
2021 neighbours.above_mode[sc] = saved_modes.0;
2022 neighbours.left_mode[sr] = saved_modes.1;
2023 sib_modes[li] = Some(leaf_mode);
2024 }
2025 // `record()`'s `above_mode`/`left_mode`
2026 // write is a no-op at an 8x8 leaf's own
2027 // side, so force the write once the whole
2028 // 16x16 slot's leaves are done, from the
2029 // last leaf (mirrors the writer's r15
2030 // fix).
2031 if let Some((below, _)) =
2032 sib_modes[2].or(sib_modes[3]).or(sib_modes[0])
2033 {
2034 neighbours.above_mode[sc] = below;
2035 }
2036 if let Some((_, right)) =
2037 sib_modes[1].or(sib_modes[3]).or(sib_modes[0])
2038 {
2039 neighbours.left_mode[sr] = right;
2040 }
2041 }
2042 }
2043 PARTITION_HORZ => {
2044 // lane-intradisp r1: two true 32x16 strips.
2045 decode_block_rect(
2046 &mut dec,
2047 &mut cdfs,
2048 &mut neighbours,
2049 at32,
2050 32,
2051 16,
2052 &mut y,
2053 &mut u,
2054 &mut v,
2055 enable_filter_intra,
2056 allow_screen_content_tools,
2057 base_q_idx,
2058 tx_select,
2059 reduced_tx_set,
2060 fctx,
2061 )?;
2062 if !has_rows32 {
2063 bump_edge32(2);
2064 }
2065 // lane-hgkf r1: at the frame edge only the first half of the strip is
2066 // coded (libaom `decode_partition` guards the second half with the same
2067 // test that made this partition non-square).
2068 if has_rows32 {
2069 decode_block_rect(
2070 &mut dec,
2071 &mut cdfs,
2072 &mut neighbours,
2073 (at32.0 + 1, at32.1),
2074 32,
2075 16,
2076 &mut y,
2077 &mut u,
2078 &mut v,
2079 enable_filter_intra,
2080 allow_screen_content_tools,
2081 base_q_idx,
2082 tx_select,
2083 reduced_tx_set,
2084 fctx,
2085 )?;
2086 }
2087 }
2088 PARTITION_VERT => {
2089 // lane-intradisp r1: mirror of PARTITION_HORZ
2090 // above with width/height swapped.
2091 decode_block_rect(
2092 &mut dec,
2093 &mut cdfs,
2094 &mut neighbours,
2095 at32,
2096 16,
2097 32,
2098 &mut y,
2099 &mut u,
2100 &mut v,
2101 enable_filter_intra,
2102 allow_screen_content_tools,
2103 base_q_idx,
2104 tx_select,
2105 reduced_tx_set,
2106 fctx,
2107 )?;
2108 if !has_cols32 {
2109 bump_edge32(3);
2110 }
2111 // lane-hgkf r1: at the frame edge only the first half of the strip is
2112 // coded (libaom `decode_partition` guards the second half with the same
2113 // test that made this partition non-square).
2114 if has_cols32 {
2115 decode_block_rect(
2116 &mut dec,
2117 &mut cdfs,
2118 &mut neighbours,
2119 (at32.0, at32.1 + 1),
2120 16,
2121 32,
2122 &mut y,
2123 &mut u,
2124 &mut v,
2125 enable_filter_intra,
2126 allow_screen_content_tools,
2127 base_q_idx,
2128 tx_select,
2129 reduced_tx_set,
2130 fctx,
2131 )?;
2132 }
2133 }
2134 PARTITION_HORZ_A => {
2135 // lane-part32 r1: two 16x16 squares on top +
2136 // a true 32x16 strip below (mirrors
2137 // decode_inter_block's cd6cb6d HORZ_A: TL,
2138 // TR, bottom strip).
2139 hit!(INTRA_HORZ_A_HITS);
2140 decode_block(
2141 &mut dec,
2142 &mut cdfs,
2143 &mut neighbours,
2144 at32,
2145 SUB,
2146 TxbSet::Luma16,
2147 TxbSet::Chroma8,
2148 TX16,
2149 TX8,
2150 (scan16, scan8),
2151 true,
2152 &mut y,
2153 &mut u,
2154 &mut v,
2155 base_q_idx,
2156 enable_filter_intra,
2157 allow_screen_content_tools,
2158 allow_intrabc,
2159 scan32,
2160 scan16,
2161 scan8,
2162 scan4,
2163 tx_select,
2164 reduced_tx_set,
2165 fctx,
2166 )?;
2167 decode_block(
2168 &mut dec,
2169 &mut cdfs,
2170 &mut neighbours,
2171 (at32.0, at32.1 + 1),
2172 SUB,
2173 TxbSet::Luma16,
2174 TxbSet::Chroma8,
2175 TX16,
2176 TX8,
2177 (scan16, scan8),
2178 true,
2179 &mut y,
2180 &mut u,
2181 &mut v,
2182 base_q_idx,
2183 enable_filter_intra,
2184 allow_screen_content_tools,
2185 allow_intrabc,
2186 scan32,
2187 scan16,
2188 scan8,
2189 scan4,
2190 tx_select,
2191 reduced_tx_set,
2192 fctx,
2193 )?;
2194 decode_block_rect(
2195 &mut dec,
2196 &mut cdfs,
2197 &mut neighbours,
2198 (at32.0 + 1, at32.1),
2199 32,
2200 16,
2201 &mut y,
2202 &mut u,
2203 &mut v,
2204 enable_filter_intra,
2205 allow_screen_content_tools,
2206 base_q_idx,
2207 tx_select,
2208 reduced_tx_set,
2209 fctx,
2210 )?;
2211 }
2212 PARTITION_HORZ_B => {
2213 // lane-part32 r1: a true 32x16 strip on top +
2214 // two 16x16 squares below (mirrors HORZ_A
2215 // with the strip/squares order flipped, same
2216 // shape as libaom decode_partition's HORZ_B).
2217 hit!(INTRA_HORZ_B_HITS);
2218 decode_block_rect(
2219 &mut dec,
2220 &mut cdfs,
2221 &mut neighbours,
2222 at32,
2223 32,
2224 16,
2225 &mut y,
2226 &mut u,
2227 &mut v,
2228 enable_filter_intra,
2229 allow_screen_content_tools,
2230 base_q_idx,
2231 tx_select,
2232 reduced_tx_set,
2233 fctx,
2234 )?;
2235 decode_block(
2236 &mut dec,
2237 &mut cdfs,
2238 &mut neighbours,
2239 (at32.0 + 1, at32.1),
2240 SUB,
2241 TxbSet::Luma16,
2242 TxbSet::Chroma8,
2243 TX16,
2244 TX8,
2245 (scan16, scan8),
2246 true,
2247 &mut y,
2248 &mut u,
2249 &mut v,
2250 base_q_idx,
2251 enable_filter_intra,
2252 allow_screen_content_tools,
2253 allow_intrabc,
2254 scan32,
2255 scan16,
2256 scan8,
2257 scan4,
2258 tx_select,
2259 reduced_tx_set,
2260 fctx,
2261 )?;
2262 decode_block(
2263 &mut dec,
2264 &mut cdfs,
2265 &mut neighbours,
2266 (at32.0 + 1, at32.1 + 1),
2267 SUB,
2268 TxbSet::Luma16,
2269 TxbSet::Chroma8,
2270 TX16,
2271 TX8,
2272 (scan16, scan8),
2273 true,
2274 &mut y,
2275 &mut u,
2276 &mut v,
2277 base_q_idx,
2278 enable_filter_intra,
2279 allow_screen_content_tools,
2280 allow_intrabc,
2281 scan32,
2282 scan16,
2283 scan8,
2284 scan4,
2285 tx_select,
2286 reduced_tx_set,
2287 fctx,
2288 )?;
2289 }
2290 PARTITION_VERT_A => {
2291 // lane-part32 r1: mirror of HORZ_A with
2292 // width/height swapped (TL, BL, right 16x32
2293 // strip).
2294 hit!(INTRA_VERT_A_HITS);
2295 let _vert_ab = crate::encode::Reach::vert_ab_partition();
2296 decode_block(
2297 &mut dec,
2298 &mut cdfs,
2299 &mut neighbours,
2300 at32,
2301 SUB,
2302 TxbSet::Luma16,
2303 TxbSet::Chroma8,
2304 TX16,
2305 TX8,
2306 (scan16, scan8),
2307 true,
2308 &mut y,
2309 &mut u,
2310 &mut v,
2311 base_q_idx,
2312 enable_filter_intra,
2313 allow_screen_content_tools,
2314 allow_intrabc,
2315 scan32,
2316 scan16,
2317 scan8,
2318 scan4,
2319 tx_select,
2320 reduced_tx_set,
2321 fctx,
2322 )?;
2323 decode_block(
2324 &mut dec,
2325 &mut cdfs,
2326 &mut neighbours,
2327 (at32.0 + 1, at32.1),
2328 SUB,
2329 TxbSet::Luma16,
2330 TxbSet::Chroma8,
2331 TX16,
2332 TX8,
2333 (scan16, scan8),
2334 true,
2335 &mut y,
2336 &mut u,
2337 &mut v,
2338 base_q_idx,
2339 enable_filter_intra,
2340 allow_screen_content_tools,
2341 allow_intrabc,
2342 scan32,
2343 scan16,
2344 scan8,
2345 scan4,
2346 tx_select,
2347 reduced_tx_set,
2348 fctx,
2349 )?;
2350 decode_block_rect(
2351 &mut dec,
2352 &mut cdfs,
2353 &mut neighbours,
2354 (at32.0, at32.1 + 1),
2355 16,
2356 32,
2357 &mut y,
2358 &mut u,
2359 &mut v,
2360 enable_filter_intra,
2361 allow_screen_content_tools,
2362 base_q_idx,
2363 tx_select,
2364 reduced_tx_set,
2365 fctx,
2366 )?;
2367 }
2368 PARTITION_VERT_B => {
2369 // lane-part32 r1: a true 16x32 strip on the
2370 // left + two 16x16 squares on the right
2371 // (libaom decode_partition VERT_B: left
2372 // strip, TR, BR).
2373 hit!(INTRA_VERT_B_HITS);
2374 let _vert_ab = crate::encode::Reach::vert_ab_partition();
2375 decode_block_rect(
2376 &mut dec,
2377 &mut cdfs,
2378 &mut neighbours,
2379 at32,
2380 16,
2381 32,
2382 &mut y,
2383 &mut u,
2384 &mut v,
2385 enable_filter_intra,
2386 allow_screen_content_tools,
2387 base_q_idx,
2388 tx_select,
2389 reduced_tx_set,
2390 fctx,
2391 )?;
2392 decode_block(
2393 &mut dec,
2394 &mut cdfs,
2395 &mut neighbours,
2396 (at32.0, at32.1 + 1),
2397 SUB,
2398 TxbSet::Luma16,
2399 TxbSet::Chroma8,
2400 TX16,
2401 TX8,
2402 (scan16, scan8),
2403 true,
2404 &mut y,
2405 &mut u,
2406 &mut v,
2407 base_q_idx,
2408 enable_filter_intra,
2409 allow_screen_content_tools,
2410 allow_intrabc,
2411 scan32,
2412 scan16,
2413 scan8,
2414 scan4,
2415 tx_select,
2416 reduced_tx_set,
2417 fctx,
2418 )?;
2419 decode_block(
2420 &mut dec,
2421 &mut cdfs,
2422 &mut neighbours,
2423 (at32.0 + 1, at32.1 + 1),
2424 SUB,
2425 TxbSet::Luma16,
2426 TxbSet::Chroma8,
2427 TX16,
2428 TX8,
2429 (scan16, scan8),
2430 true,
2431 &mut y,
2432 &mut u,
2433 &mut v,
2434 base_q_idx,
2435 enable_filter_intra,
2436 allow_screen_content_tools,
2437 allow_intrabc,
2438 scan32,
2439 scan16,
2440 scan8,
2441 scan4,
2442 tx_select,
2443 reduced_tx_set,
2444 fctx,
2445 )?;
2446 }
2447 PARTITION_HORZ_4 | PARTITION_VERT_4 => {
2448 // lane-tx64x16 r3: the 1:4 pair at the 32x32
2449 // level -- four 32x8 (or 8x32) strips in
2450 // raster order, `quarter_step` = 2 mi, with
2451 // libaom `decode_partition`'s own `i > 0`
2452 // frame-edge break.
2453 let horz = part32 == PARTITION_HORZ_4;
2454 let base_mi = (at32.0 * (SUB / MI), at32.1 * (SUB / MI));
2455 let mut prev: Option<((usize, usize), usize)> = None;
2456 for i in 0..4 {
2457 let strip_mi = if horz {
2458 (base_mi.0 + i * 2, base_mi.1)
2459 } else {
2460 (base_mi.0, base_mi.1 + i * 2)
2461 };
2462 if i > 0
2463 && (strip_mi.0 >= mi_rows as usize
2464 || strip_mi.1 >= mi_cols as usize)
2465 {
2466 break;
2467 }
2468 let (bw, bh) = if horz { (32, 8) } else { (8, 32) };
2469 let (mode, _uv) = decode_block_rect4(
2470 &mut dec,
2471 &mut cdfs,
2472 &mut neighbours,
2473 strip_mi,
2474 bw,
2475 bh,
2476 prev,
2477 &mut y,
2478 &mut u,
2479 &mut v,
2480 enable_filter_intra,
2481 allow_screen_content_tools,
2482 base_q_idx,
2483 tx_select,
2484 fctx,
2485 )?;
2486 prev = Some((strip_mi, mode));
2487 }
2488 }
2489 _ => {
2490 return Err(unsupported(format!(
2491 "a 32x32 partition type this decoder does not code (value={part32})"
2492 )));
2493 }
2494 }
2495 }
2496 }
2497 PARTITION_HORZ => {
2498 // lane-sbpart r2: two true 64x32 strips.
2499 decode_block_rect64(
2500 &mut dec,
2501 &mut cdfs,
2502 &mut neighbours,
2503 at,
2504 64,
2505 32,
2506 &mut y,
2507 &mut u,
2508 &mut v,
2509 enable_filter_intra,
2510 allow_screen_content_tools,
2511 base_q_idx,
2512 tx_select,
2513 reduced_tx_set,
2514 fctx,
2515 )?;
2516 if !has_rows {
2517 bump_edge32(6);
2518 }
2519 // lane-golomb r1: at the frame edge only the first half of the
2520 // strip is coded (libaom `decode_partition` guards the second
2521 // with the same has_rows test that made this partition non-square).
2522 if has_rows {
2523 decode_block_rect64(
2524 &mut dec,
2525 &mut cdfs,
2526 &mut neighbours,
2527 (at.0 + 2, at.1),
2528 64,
2529 32,
2530 &mut y,
2531 &mut u,
2532 &mut v,
2533 enable_filter_intra,
2534 allow_screen_content_tools,
2535 base_q_idx,
2536 tx_select,
2537 reduced_tx_set,
2538 fctx,
2539 )?;
2540 }
2541 }
2542 PARTITION_VERT => {
2543 // lane-sbpart r2: mirror of PARTITION_HORZ above with
2544 // width/height swapped.
2545 decode_block_rect64(
2546 &mut dec,
2547 &mut cdfs,
2548 &mut neighbours,
2549 at,
2550 32,
2551 64,
2552 &mut y,
2553 &mut u,
2554 &mut v,
2555 enable_filter_intra,
2556 allow_screen_content_tools,
2557 base_q_idx,
2558 tx_select,
2559 reduced_tx_set,
2560 fctx,
2561 )?;
2562 if !has_cols {
2563 bump_edge32(7);
2564 }
2565 // lane-golomb r1: at the frame edge only the first half of the
2566 // strip is coded (libaom `decode_partition` guards the second
2567 // with the same has_cols test that made this partition non-square).
2568 if has_cols {
2569 decode_block_rect64(
2570 &mut dec,
2571 &mut cdfs,
2572 &mut neighbours,
2573 (at.0, at.1 + 2),
2574 32,
2575 64,
2576 &mut y,
2577 &mut u,
2578 &mut v,
2579 enable_filter_intra,
2580 allow_screen_content_tools,
2581 base_q_idx,
2582 tx_select,
2583 reduced_tx_set,
2584 fctx,
2585 )?;
2586 }
2587 }
2588 PARTITION_HORZ_A | PARTITION_HORZ_B | PARTITION_VERT_A | PARTITION_VERT_B => {
2589 // lane-part32 r4: the four superblock-level AB arms, each
2590 // two 32x32 squares plus one 64x32/32x64 strip, in
2591 // libaom's own `decode_partition` order (decodeframe.c:
2592 // HORZ_A = TL, TR, bottom strip; HORZ_B = top strip, BL,
2593 // BR; VERT_A = TL, BL, right strip; VERT_B = left strip,
2594 // TR, BR). The pieces are exactly the ones already proven
2595 // by the `PARTITION_NONE`-under-SPLIT (32x32 square) and
2596 // `PARTITION_HORZ`/`VERT` (rect64 strip) arms above.
2597 hit_do! {
2598 SB_AB_HITS.with(|c| {
2599 let mut h = c.get();
2600 h[part - PARTITION_HORZ_A] += 1;
2601 c.set(h);
2602 });
2603 }
2604 // lane-part32 r5: only the two VERTICAL arms reorder the
2605 // square sub-blocks (TL, BL, TR, BR), so only they switch
2606 // libaom's `has_tr`/`has_bl` tables.
2607 let _vert_ab = (part == PARTITION_VERT_A || part == PARTITION_VERT_B)
2608 .then(crate::encode::Reach::vert_ab_partition);
2609 macro_rules! square32 {
2610 ($at:expr) => {
2611 decode_block(
2612 &mut dec,
2613 &mut cdfs,
2614 &mut neighbours,
2615 $at,
2616 BLOCK,
2617 TxbSet::Luma32,
2618 TxbSet::Chroma16,
2619 TX32,
2620 TX16,
2621 (&scan32, &scan16),
2622 true,
2623 &mut y,
2624 &mut u,
2625 &mut v,
2626 base_q_idx,
2627 enable_filter_intra,
2628 allow_screen_content_tools,
2629 allow_intrabc,
2630 &scan32,
2631 &scan16,
2632 &scan8,
2633 &scan4,
2634 tx_select,
2635 reduced_tx_set,
2636 fctx,
2637 )?
2638 };
2639 }
2640 macro_rules! strip64 {
2641 ($at:expr, $bw:expr, $bh:expr) => {
2642 decode_block_rect64(
2643 &mut dec,
2644 &mut cdfs,
2645 &mut neighbours,
2646 $at,
2647 $bw,
2648 $bh,
2649 &mut y,
2650 &mut u,
2651 &mut v,
2652 enable_filter_intra,
2653 allow_screen_content_tools,
2654 base_q_idx,
2655 tx_select,
2656 reduced_tx_set,
2657 fctx,
2658 )?
2659 };
2660 }
2661 let (br, bc) = (at.0 + 2, at.1 + 2);
2662 match part {
2663 PARTITION_HORZ_A => {
2664 square32!(at);
2665 square32!((at.0, bc));
2666 strip64!((br, at.1), 64, 32);
2667 }
2668 PARTITION_HORZ_B => {
2669 strip64!(at, 64, 32);
2670 square32!((br, at.1));
2671 square32!((br, bc));
2672 }
2673 PARTITION_VERT_A => {
2674 square32!(at);
2675 square32!((br, at.1));
2676 strip64!((at.0, bc), 32, 64);
2677 }
2678 _ => {
2679 strip64!(at, 32, 64);
2680 square32!((at.0, bc));
2681 square32!((br, bc));
2682 }
2683 }
2684 }
2685 PARTITION_HORZ_4 | PARTITION_VERT_4 => {
2686 // lane-tx64x16: the 1:4 pair at 64x64 -- four 64x16 (or
2687 // 16x64) strips in raster order. `decode_partition`
2688 // (`decodeframe.c`) steps by `quarter_step = mi_size / 4`
2689 // (4 mi = 16 px = one SUB unit here) and BREAKS at the
2690 // frame edge for `i > 0`, so a partial superblock codes
2691 // only the strips whose origin is inside the frame.
2692 let horz = part == PARTITION_HORZ_4;
2693 for i in 0..4 {
2694 let step_mi = i * (SB_MI as usize / 4);
2695 let (this_r_mi, this_c_mi) = if horz {
2696 (
2697 sb_r as usize * SB_MI as usize + step_mi,
2698 sb_c as usize * SB_MI as usize,
2699 )
2700 } else {
2701 (
2702 sb_r as usize * SB_MI as usize,
2703 sb_c as usize * SB_MI as usize + step_mi,
2704 )
2705 };
2706 if i > 0
2707 && (this_r_mi >= mi_rows as usize || this_c_mi >= mi_cols as usize)
2708 {
2709 break;
2710 }
2711 let strip_at = if horz {
2712 (at.0 + i, at.1)
2713 } else {
2714 (at.0, at.1 + i)
2715 };
2716 let (bw, bh) = if horz { (64, 16) } else { (16, 64) };
2717 decode_block_rect64(
2718 &mut dec,
2719 &mut cdfs,
2720 &mut neighbours,
2721 strip_at,
2722 bw,
2723 bh,
2724 &mut y,
2725 &mut u,
2726 &mut v,
2727 enable_filter_intra,
2728 allow_screen_content_tools,
2729 base_q_idx,
2730 tx_select,
2731 reduced_tx_set,
2732 fctx,
2733 )?;
2734 }
2735 }
2736 _ => {
2737 return Err(unsupported(
2738 "a superblock-level partition value outside PARTITION_NONE..PARTITION_VERT_4",
2739 ));
2740 }
2741 }
2742 }
2743 }
2744 // lane-timeline: this tile's last symbol is read here.
2745 crate::timeline::tile_done();
2746
2747 if tile_num == tile_info.context_update_tile_id {
2748 result_cdfs = cdfs;
2749 }
2750 }
2751 crate::timeline::recon_done();
2752
2753 // lane-comppin r4: pre-loop-filter decode-order dump, matching aomdec's
2754 // own EC_AV1_PREFILT_DUMP shape (decodeframe.c ~5451) -- diffs against
2755 // that isolate whether a decode-order frame's mismatch already exists
2756 // in reconstruction (this dump) vs is introduced by the loop filter
2757 // (EC_AV1_DECODE_ORDER_DUMP's post-filter dump).
2758 // Decode-order frame marker for the EC_OBMC / EC_TRACE_MODE ladders.
2759 if crate::envflags::env_flag!("EC_OBMC")
2760 || crate::envflags::env_flag!("EC_TRACE_MODE")
2761 || crate::envflags::env_flag!("EC_PRED")
2762 {
2763 eprintln!(
2764 "EC_PICT idx={}",
2765 PREFILT_PICTURE_IDX.load(std::sync::atomic::Ordering::SeqCst)
2766 );
2767 }
2768 dump_stage16(
2769 "EC_AV1_PREFILT_DUMP16",
2770 &y,
2771 &u,
2772 &v,
2773 frame_width as usize,
2774 frame_height as usize,
2775 ss_x(fctx),
2776 ss_y(fctx),
2777 );
2778 census_unwritten(&y, &u, &v);
2779 if let Ok(path) = crate::envflags::var("EC_AV1_PREFILT_DUMP") {
2780 // lane-av1dumploud: loud on a short write (three discarded
2781 // `write_all` results before). Expected length = the three planes' own
2782 // data lengths: this dump is the PADDED plane, not a crop, so the
2783 // writer's extent is `p.data` itself.
2784 let idx = PREFILT_PICTURE_IDX.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
2785 let narrow = |p: &PlaneBuf<'_>| -> Vec<u8> { p.data.iter().map(|&s| s as u8).collect() };
2786 crate::dumpio::write_planes(
2787 "EC_AV1_PREFILT_DUMP",
2788 format!("{path}.f{idx}"),
2789 &[&narrow(&y), &narrow(&u), &narrow(&v)],
2790 );
2791 } else {
2792 PREFILT_PICTURE_IDX.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
2793 }
2794 dump_prefilter_wide(&y, &u, &v);
2795 // lane-av1fwd: the encoder's filter search re-decodes this same key
2796 // frame tile per candidate -- capture it once (see [`FilterReplay`]),
2797 // on the shape whose tail is the two filter calls and nothing else.
2798 if superres(fctx).is_none()
2799 && !(0..3).any(|p| {
2800 lr.frame_restoration_type[p] != ec_av1_syntax::RestorationType::None
2801 && lr.loop_restoration_size[p] != 0
2802 })
2803 {
2804 capture_filter_replay(
2805 &y,
2806 &u,
2807 &v,
2808 &neighbours,
2809 frame_width as usize,
2810 frame_height as usize,
2811 width,
2812 height,
2813 true_width,
2814 true_height,
2815 );
2816 }
2817 apply_deblock(
2818 &mut y,
2819 &mut u,
2820 &mut v,
2821 loop_filter,
2822 &neighbours,
2823 frame_width as usize,
2824 frame_height as usize,
2825 fctx,
2826 );
2827 // lane-perf4: loop restoration is the only reader of the pre-CDEF planes,
2828 // and a frame that restores nothing copied every plane twice for nothing
2829 // (three `PlaneBuf` clones here plus the `cdef.to_vec()` inside
2830 // [`crate::restoration::apply_loop_restoration_plane`], which returns its
2831 // input unchanged when the plane's type is `None`). Clone only when some
2832 // plane actually restores.
2833 let lr_active = (0..3).any(|p| {
2834 lr.frame_restoration_type[p] != ec_av1_syntax::RestorationType::None
2835 && lr.loop_restoration_size[p] != 0
2836 });
2837 let deblocked = lr_active.then(|| {
2838 (
2839 plane_snapshot(&y, 0),
2840 // lane-av1422filter: the bands are the LR stripe-boundary context
2841 // (`RESTORATION_UNIT_OFFSET >> ss_y` above/below each 64-luma-row
2842 // stripe). Chroma stripes are half-height ONLY under 4:2:0; a
2843 // 4:2:2 frame's chroma (ss_y = 0) stripes on the luma grid, so
2844 // the hardcoded `1` saved bands at rows 26/60/92/124 where LR
2845 // reads rows 54..57/118..121 -- every 4:2:2 boundary
2846 // substitution read uninitialised zeros (frame-0 V (48,0)).
2847 plane_snapshot(&u, ss_y(fctx) as u32),
2848 plane_snapshot(&v, ss_y(fctx) as u32),
2849 )
2850 });
2851 // lane-tiny r4: post-deblock / post-CDEF dumps mirroring aomdec's own
2852 // EC_AV1_POSTDEBLOCK_DUMP (decodeframe.c ~5404) -- with the pre-filter
2853 // dump above these bisect WHICH filter stage introduced a mismatch.
2854 dump_stage("EC_AV1_POSTDEBLOCK_DUMP", &y, &u, &v);
2855 dump_stage16(
2856 "EC_AV1_POSTDEBLOCK_DUMP16",
2857 &y,
2858 &u,
2859 &v,
2860 frame_width as usize,
2861 frame_height as usize,
2862 ss_x(fctx),
2863 ss_y(fctx),
2864 );
2865 apply_cdef(
2866 &mut y,
2867 &mut u,
2868 &mut v,
2869 cdef,
2870 &neighbours,
2871 (frame_width as usize, frame_height as usize),
2872 fctx,
2873 None,
2874 None,
2875 );
2876 dump_stage("EC_AV1_POSTCDEF_DUMP", &y, &u, &v);
2877 dump_stage16(
2878 "EC_AV1_POSTCDEF_DUMP16",
2879 &y,
2880 &u,
2881 &v,
2882 frame_width as usize,
2883 frame_height as usize,
2884 ss_x(fctx),
2885 ss_y(fctx),
2886 );
2887 capture_stages(&y, &u, &v, deblocked.as_ref(), fctx);
2888 // spec 7.16 (libaom `superres_post_decode`, decodeframe.c: called
2889 // between `av1_cdef_frame` and `av1_loop_restoration_filter_frame`):
2890 // the horizontal upscale runs HERE -- after CDEF, before loop
2891 // restoration, not after the whole filter chain. Both the post-CDEF
2892 // frame and the post-deblock frame that LR substitutes at its stripe
2893 // boundaries are upscaled (`save_deblock_boundary_lines` runs
2894 // `av1_upscale_normative_rows` over its saved rows when the frame is
2895 // scaled), so every LR coordinate from here on is in upscaled space.
2896 let mut deblocked = deblocked;
2897 let mut lr_w = frame_width as usize;
2898 if let Some((upscaled_w, _)) = superres(fctx) {
2899 let bd = u32::from(bit_depth(fctx));
2900 let (uw, fw, fh) = (
2901 upscaled_w as usize,
2902 frame_width as usize,
2903 frame_height as usize,
2904 );
2905 let up = |p: &PlaneBuf<'_>, in_w: usize, in_h: usize, out_w: usize| PlaneBuf {
2906 data: std::borrow::Cow::Owned(crate::superres::upscale_plane_strided(
2907 &p.data,
2908 p.width,
2909 in_w,
2910 in_h,
2911 p.true_width,
2912 out_w,
2913 bd,
2914 )),
2915 width: out_w,
2916 height: in_h,
2917 true_width: out_w,
2918 true_height: in_h,
2919 tile_x0: 0,
2920 tile_y0: 0,
2921 tile_x1: out_w,
2922 tile_y1: in_h,
2923 plane: p.plane,
2924 };
2925 let (cw, ch, cuw) = (
2926 round_ss(fw, ss_x(fctx)),
2927 round_ss(fh, ss_y(fctx)),
2928 round_ss(uw, ss_x(fctx)),
2929 );
2930 y = up(&y, fw, fh, uw);
2931 u = up(&u, cw, ch, cuw);
2932 v = up(&v, cw, ch, cuw);
2933 if let Some((dy, du, dv)) = deblocked.take() {
2934 deblocked = Some((
2935 up(&dy, fw, fh, uw),
2936 up(&du, cw, ch, cuw),
2937 up(&dv, cw, ch, cuw),
2938 ));
2939 }
2940 lr_w = uw;
2941 crate::superres::note_upscaled_picture();
2942 }
2943 if let Some((deblocked_y, deblocked_u, deblocked_v)) = &deblocked {
2944 apply_loop_restoration(
2945 &mut y,
2946 &mut u,
2947 &mut v,
2948 deblocked_y,
2949 deblocked_u,
2950 deblocked_v,
2951 lr,
2952 &lr_grid,
2953 (lr_w, frame_height as usize),
2954 fctx,
2955 );
2956 }
2957 if let Some((db_y, db_u, db_v)) = deblocked {
2958 // LIFO pool: return in reverse so the next frame's `y`/`u`/`v`
2959 // snapshots pop buffers of their own size and never resize.
2960 scratch_return(db_v.data.into_owned());
2961 scratch_return(db_u.data.into_owned());
2962 scratch_return(db_y.data.into_owned());
2963 }
2964 crate::timeline::filters_done();
2965
2966 let (fw, fh) = (lr_w, frame_height as usize);
2967 // An upscaled frame is already at its final size with no mi-aligned
2968 // margin beyond it (the upscale consumed those columns), so the
2969 // wide-margin stash below has nothing left to hand out.
2970 let (true_width, true_height) = if superres(fctx).is_some() {
2971 (fw, fh)
2972 } else {
2973 (true_width, true_height)
2974 };
2975 if fw == width && fh == height {
2976 fctx.last_frame_wide_margin.with(|m| *m.borrow_mut() = None);
2977 return Ok((
2978 Picture {
2979 width,
2980 height,
2981 y: y.data.into_owned(),
2982 u: u.data.into_owned(),
2983 v: v.data.into_owned(),
2984 },
2985 result_cdfs,
2986 ));
2987 }
2988 let crop = |plane: &PlaneBuf<'_>, w: usize, h: usize| -> Vec<u16> {
2989 let mut out = Vec::with_capacity(w * h);
2990 for row in 0..h {
2991 out.extend(plane.data[row * plane.width..][..w].iter().copied());
2992 }
2993 out
2994 };
2995 // lane-superres r3: `true_width`/`true_height` (`mi_cols`/`mi_rows` * 4)
2996 // is the real reconstructed extent -- columns `[fw, true_width)` hold
2997 // genuine decoded samples (the last coding block straddles the frame
2998 // edge whenever `frame_width` isn't 8-sample aligned), not padding.
2999 // libaom's superres upscale runs on that buffer's own border-extended
3000 // margin, so it reads those real columns, not a synthetic replicate of
3001 // column `fw - 1`. Stash them for `stream.rs`'s superres path (a real
3002 // aomenc 43->64 stream pinned column-by-column against libaom's own
3003 // `av1_convolve_horiz_rs_c` via `scripts/superres-pin-harness.c` --
3004 // `row6-realedgeval` proved the replicate-of-`fw-1` padding this
3005 // decoder used before was off by 1 at the columns the tap window
3006 // reaches into that real margin; `None` when there is no margin (this
3007 // frame's `true_width`/`true_height` already equal `fw`/`fh`, e.g. a
3008 // non-superres frame whose width just isn't 32-block aligned).
3009 fctx.last_frame_wide_margin.with(|m| {
3010 *m.borrow_mut() = if true_width > fw || true_height > fh {
3011 let yc = crop(&y, true_width, true_height);
3012 if let Ok(path) = crate::envflags::var("EC_AV1_MARGIN_DUMP") {
3013 static IDX: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
3014 let idx = IDX.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
3015 // corner-cut: this diagnostic dump stays 8-bit-narrowed
3016 // regardless of the stream's real bit depth (debug-only,
3017 // never read by a gate); widen if a >8-bit margin dump
3018 // is ever needed.
3019 //
3020 // lane-av1dumploud: loud on a short write. Expected length is
3021 // `true_width * true_height` -- the crop this dump is exactly,
3022 // not a re-derived geometry.
3023 let narrow: Vec<u8> = yc.iter().map(|&s| s as u8).collect();
3024 crate::dumpio::write_planes(
3025 "EC_AV1_MARGIN_DUMP",
3026 format!("{path}.f{idx}"),
3027 &[&narrow],
3028 );
3029 }
3030 Some(Picture {
3031 width: true_width,
3032 height: true_height,
3033 y: yc,
3034 u: crop(
3035 &u,
3036 round_ss(true_width, ss_x(fctx)),
3037 round_ss(true_height, ss_y(fctx)),
3038 ),
3039 v: crop(
3040 &v,
3041 round_ss(true_width, ss_x(fctx)),
3042 round_ss(true_height, ss_y(fctx)),
3043 ),
3044 })
3045 } else {
3046 None
3047 }
3048 });
3049 Ok((
3050 Picture {
3051 width: fw,
3052 height: fh,
3053 y: crop_owned(y, fw, fh),
3054 u: crop_owned(u, round_ss(fw, ss_x(fctx)), round_ss(fh, ss_y(fctx))),
3055 v: crop_owned(v, round_ss(fw, ss_x(fctx)), round_ss(fh, ss_y(fctx))),
3056 },
3057 result_cdfs,
3058 ))
3059}