forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

inter_block.rs4457 lines, 227.4 KiBHistoryRawDownload
1//! Inter block decoder: `decode_inter_block`, the per-block inter path. Moved out of `decode/mod.rs` (av1split D8b); its intra else-arm is in `inter_intra_arm.rs` (D8a). The items are unchanged apart from visibility.
2
3use super::*;
4
5// Signature mirrors the spec's per-call inputs; a parameter struct would only move them.
6#[allow(clippy::too_many_arguments)]
7pub(super) fn decode_inter_block(
8 dec: &mut SymbolDecoder,
9 cdfs: &mut Cdfs,
10 neighbours: &mut Neighbours,
11 grid: &mut MiGrid,
12 at: (usize, usize),
13 side: usize,
14 mi_cols: u32,
15 mi_rows: u32,
16 y: &mut PlaneBuf<'static>,
17 u: &mut PlaneBuf<'static>,
18 v: &mut PlaneBuf<'static>,
19 refpix: &RefPix<'_>,
20 // lane-av1refs: every non-`LAST_FRAME` reference this frame header's own
21 // `ref_frame_idx` names a live DPB slot for, indexed `[ref_frame]`
22 // (`LAST2_FRAME`=2 .. `ALTREF_FRAME`=7; index 0/1 unused, `LAST_FRAME`
23 // stays on `ref_y`/`u`/`v` above) -- `None` at a live index means the
24 // slot's still empty (an error-resilient stream, or too early in the
25 // GOP), matching `GOLDEN_FRAME`'s existing empty-slot refusal below.
26 sign_bias_table: &SignBiasTable,
27 // lane-gm r2: this frame header's own `global_motion` table (spec
28 // 5.9.24), indexed `[ref_frame - LAST_FRAME]` same as `sign_bias_table`
29 // -- feeds `warp::gm_get_motion_vector` for `GLOBALMV`/`GLOBAL_GLOBALMV`
30 // assignment and the mv-stack's neighbour substitution.
31 global_motion: &[ec_av1_syntax::WarpParams; 7],
32 base_q_idx: u8,
33 luma_set_intra: TxbSet,
34 luma_set_inter: TxbSet,
35 chroma_set: TxbSet,
36 luma_tx: usize,
37 chroma_tx: usize,
38 scan_luma: &[u16],
39 scan_chroma: &[u16],
40 // lane-inter4 r3: superseded by `size_group_wh(write_w, write_h)` at the
41 // one site that read it (`y_mode`) -- a rect strip's group comes from its
42 // true footprint, not the caller's square `side`. Kept in the signature
43 // rather than editing twenty call sites.
44 _size_group: usize,
45 allow_high_precision_mv: bool,
46 force_integer_mv: bool,
47 interp_fixed: Option<mc::InterpFilterKind>,
48 enable_dual_filter: bool,
49 tpl_frame: Option<&TplFrameArgs>,
50 // lane-av1comp: this frame header's own `reference_select` bit -- `false`
51 // means every block is `SINGLE_REFERENCE` by construction and `comp_mode`
52 // is never read (spec 5.11.25); `true` reads `comp_mode` per block and
53 // refuses (rather than silently misdecoding) the ones that pick
54 // `COMPOUND_REFERENCE`, since two-reference motion compensation is not
55 // wired yet.
56 reference_select: bool,
57 // lane-av1comp: `seq_params`' own masked-compound/distance-weighted-
58 // compound enable bits, gating `comp_group_idx`/`compound_idx`
59 // (spec 5.11.25) -- always present (unlike `tpl_frame`), since the
60 // compound blend weights need this frame's own order hint regardless
61 // of `use_ref_frame_mvs`.
62 enable_masked_compound: bool,
63 // lane-sb128 r4: this sequence header's own `enable_interintra_compound`
64 // bit (spec 5.11.24's `interintra` read, libaom `decodemv.c`
65 // read_inter_block_mode_info ~1490-1510) -- read right after `assign_mv`
66 // and before `read_motion_mode` on the single-ref path below, gated by
67 // `is_interintra_allowed` (single-ref, `BLOCK_8X8..=BLOCK_32X32`, and
68 // every mode this single-ref branch can produce is already in
69 // `SINGLE_INTER_MODE_START..END`); `interintra == 1` is a named refusal
70 // (inter+intra blended prediction unimplemented) rather than a
71 // mis-decode.
72 enable_interintra_compound: bool,
73 enable_jnt_comp: bool,
74 order_hint_bits: u32,
75 order_hint: u32,
76 ref_order_hints: [u32; 7],
77 // lane-av1comp round 14: this frame header's own `skip_mode_present` bit
78 // and `skip_mode_frame` reference pair (spec 5.9.22, `frame.rs`'s
79 // `read_skip_mode_params`, already 1-based `MV_REFERENCE_FRAME`) --
80 // `skip_mode` (spec 5.11.23 `read_skip_mode`) is read *before* `skip`
81 // when this is `true` (every block this decoder reaches is >= 8x8, so
82 // `is_comp_ref_allowed` never gates it further); a `true` result forces
83 // `skip_txfm`, `is_inter`, `NEAREST_NEARESTMV` of this pair with no mode/
84 // DRL symbol read, and a plain average blend with no `comp_group_idx`/
85 // `compound_idx` read (libaom `decodemv.c` 1604-1619, 1382-1384, 1509).
86 skip_mode_present: bool,
87 skip_mode_frame: [u8; 2],
88 // lane-motionmode round 1: this frame header's own `is_motion_mode_switchable`/
89 // `allow_warped_motion` bits (spec 5.11.24's `read_motion_mode`).
90 switchable_motion_mode: bool,
91 allow_warped_motion: bool,
92 // lane-warp r5: set by the HORZ_B arm -- the 32x16 top strip is decoded
93 // as a square 32x32 block, which over-reads a residual unless coded
94 // `skip`; a non-skip strip must refuse cleanly instead of desyncing.
95 reject_residual: bool,
96 // lane-partitions r1: the strip's own true width/height in pixels --
97 // `side` keeps governing every syntax/CDF/prediction-buffer decision
98 // (matching `HORZ_B`'s already-accepted square-context corner-cut), but
99 // the final pixel write and this block's own neighbour-context stamp
100 // (`record*`/`fill_*_grid`) use `write_w`/`write_h` instead, so a
101 // PARTITION_HORZ/VERT strip only claims its own true footprint. `side`
102 // for every existing (square) caller.
103 write_w: usize,
104 write_h: usize,
105 // lane-screen r2: this frame header's own `allow_screen_content_tools`
106 // bit, threaded to the intra-sub-block branch below -- libaom's
107 // `read_intra_block_mode_info` (decodemv.c:1065-1107, the inter-frame
108 // counterpart of `read_intra_frame_mode_info`) reads `y_mode_cdf`
109 // (never `kf_y_mode_cdf`) and has NO `use_intrabc` call at all (that
110 // symbol is intra-frame-only, spec `av1_read_mode_info` dispatches on
111 // `frame_is_intra_only`), but it still calls `read_palette_mode_info`
112 // under the same `av1_allow_palette` gate as the key-frame path.
113 allow_screen_content_tools: bool,
114 // lane-superres r9: this frame's own coded luma width (spec 7.11.3.3) --
115 // compared against each reference's stored picture width to decide
116 // whether a scaled-MC path (`mc::predict_scaled`) or the ordinary
117 // stride-1 one (`mc::predict_with_filters`) applies per reference, and to
118 // refuse (rather than mis-decode) the warp/OBMC/interintra/compound
119 // combinations that don't implement scaled MC yet.
120 frame_size: (usize, usize),
121 fctx: &crate::decode::FrameCtx,
122) -> Result<()> {
123 // Round 27: the per-read trace is the only instrument that reaches the inter
124 // path (the coefficient rungs stop well before the frame-3 divergence),
125 // so publish the phase and the block's mi for every read it makes.
126 crate::msac::SymbolDecoder::set_symr_phase("inter");
127 crate::msac::SymbolDecoder::set_symr_mi(at.0 as i64, at.1 as i64);
128 // lane-inter4 r1: `at` is in MI units (4 px) so a 32-level 1:4 strip can
129 // name an 8-px offset; `(r, c)` stays the enclosing 16-px cell for the
130 // few 16-px-granular uses below.
131 let (r, c) = (at.0 / (SUB / MI), at.1 / (SUB / MI));
132 // lane-inter8 r2: the inter side bands are mi-granular; this block's own
133 // above/left neighbour is the mi cell at its top-left corner (libaom
134 // `above_mbmi` = mi(mi_row - 1, mi_col)), so a SUB-aligned block reads
135 // exactly what it read before.
136 let (rmi, cmi) = at;
137 let (px, py) = (at.1 * MI, at.0 * MI);
138 census_inter_block_shape(side, write_w, write_h);
139 // lane-inter16ab r2: a 16x4 / 4x16 inter strip's chroma is the PAIR's
140 // (8x4 / 4x8 at the even strip's origin) and is coded only by the odd
141 // strip (`is_chroma_reference`); every other block keeps its own.
142 let strip_chroma = fctx.inter_strip_chroma.with(std::cell::Cell::take);
143 fctx.inter_last_mc.with(|c| c.set(None));
144 let has_chroma = strip_chroma.is_none_or(|s| s.has_chroma);
145 let (cpx, cpy) = match strip_chroma {
146 Some(s) if s.has_chroma => (
147 (s.pair_mi.1 * MI) >> ss_x(fctx),
148 (s.pair_mi.0 * MI) >> ss_y(fctx),
149 ),
150 _ => (px >> ss_x(fctx), py >> ss_y(fctx)),
151 };
152 // lane-av1-422j: the chroma prediction/residual buffers are
153 // `chroma_side`-square and every consumer indexes them
154 // `row * chroma_side + col` over the WRITE rect (`write_chroma_w` x
155 // `write_chroma_h`). At ss (1,0) that rect is `side/2 x side` (an 8x16
156 // strip's 4x16 chroma), so a `side >> ss_x` square is shorter than
157 // `row * chroma_side + write_chroma_w` on rows `>= side/2` -- frame 1's
158 // skipped VERT_B strip read `prediction[64..68]` out of the 64-sample
159 // buffer (reconstruct_mc_rect panic, decode.rs:29094). The enclosing
160 // square of the chroma rect is the max: unchanged at 4:2:0 (side/2) and
161 // 4:4:4 (side), so only 4:2:2 changes shape.
162 let chroma_side = (side >> ss_x(fctx)).max(side >> ss_y(fctx));
163 let (write_chroma_w, write_chroma_h) = match strip_chroma {
164 Some(s) if s.has_chroma => {
165 // 4:2:0: the PAIR's 8x4 / 4x8. 4:4:4: the strip's own 16x4 /
166 // 4x16 (lane-av1-444; no pair at ss 0/0).
167 //
168 // lane-av1422lpf site 4: the VERT arm's third shape. A
169 // `PARTITION_VERT_4` strip is 4x16 luma and pairs on COLUMNS
170 // while `ss_x == 1` (`is_chroma_reference` on `mi_col` parity,
171 // av1_common_int.h:1454), so the pair is 8x16 luma -- and 4:2:2
172 // subsamples X ONLY (`subsampling_x = 1, subsampling_y = 0`,
173 // spec 5.5.2's `color_config`), which leaves the pair's chroma
174 // plane block at 8>>1 x 16>>0 = **4x16**, twice as tall as the
175 // 4:2:0 pair's 4x8. The old `else` handed 4:2:2 the 4:2:0 shape,
176 // halving `write_chroma_h` for the WHOLE of `decode_inter_block`
177 // at ss (1,0) -- `chroma_stride`/`chroma_buf_h` at 40017 and so
178 // every prediction buffer, every grid and every mu-chunk replay
179 // below. Measured on `W_intrabc.obu` frame 1: a 4x16 vert strip
180 // got `chroma_stride = 4`, `chroma_buf_h = 8`, a 32-sample grid,
181 // and the lossless TX_4x4 replay's last unit row read index 64 of
182 // it (`range end index 36 out of range for slice of length 32`
183 // at the composed-grid read).
184 //
185 // The HORZ arm needs no third shape at 4:2:2 and gets none: a
186 // HORZ_4 strip pairs on ROWS only while `ss_y == 1`, and 4:2:2
187 // has `ss_y == 0`, so each 16x4 strip is its own chroma reference
188 // and its own 8x4 chroma block -- the number `(8, 4)` already
189 // returns. At 4:2:0 (`ss_x == ss_y == 1`) the pair halves on both
190 // axes and at 4:4:4 there is no pair at all, so both of those
191 // arms are untouched by this line.
192 if ss_x(fctx) == 0 && ss_y(fctx) == 0 {
193 if s.horz { (16, 4) } else { (4, 16) }
194 } else if s.horz {
195 (8, 4)
196 } else if ss_y(fctx) == 0 {
197 (4, 16)
198 } else {
199 (4, 8)
200 }
201 }
202 _ => (write_w >> ss_x(fctx), write_h >> ss_y(fctx)),
203 };
204 // lane-av1-422blockhalf: the chroma PREDICTION buffers take the PLANE
205 // BLOCK's own per-axis shape at 4:2:2 -- (side/2, side) -- instead of the
206 // enclosing square `chroma_side` the pre-port model indexed, and the
207 // masked-compound blend reads its luma mask through the plane's real
208 // subsampling shifts (aom_dsp/blend-a64-mask.c's four branches; the
209 // stop-note this lane owns). Every consumer below strides with
210 // `chroma_stride`. At 4:2:0 and 4:4:4 both values reduce to the square
211 // `chroma_side` verbatim, so only 4:2:2 changes shape.
212 let chroma_422 = ss_x(fctx) == 1 && ss_y(fctx) == 0;
213 let (chroma_stride, chroma_buf_h) = if chroma_422 {
214 (write_chroma_w, write_chroma_h)
215 } else {
216 (chroma_side, chroma_side)
217 };
218 let (chroma_set, chroma_tx, scan_chroma) =
219 if ss_x(fctx) == 0 && ss_y(fctx) == 0 && strip_chroma.is_none() {
220 let tx = if side >= 64 { 32 } else { side };
221 let set = match tx {
222 32 => TxbSet::Chroma32,
223 16 => TxbSet::Chroma16,
224 8 => TxbSet::Chroma8,
225 _ => TxbSet::Chroma4,
226 };
227 (set, tx, scan_luma)
228 } else {
229 (chroma_set, chroma_tx, scan_chroma)
230 };
231 // lane-midcut r3 (same rule as lane-t900 r5's `warp_plane_allowed`, one
232 // copy kept at the merge): `av1_init_warp_params` (libaom reconinter.c)
233 // bails per PLANE at `block_width < 8 || block_height < 8` -- on the
234 // block's own dims, never on the square prediction buffer this reader
235 // allocates.
236 if crate::envflags::env_flag!("EC_MC_TRACE") {
237 eprintln!("EC_IB px={px} py={py} side={side} w={write_w} h={write_h}");
238 }
239
240 if crate::envflags::env_flag!("EC_AV1_TELL") {
241 eprintln!(
242 "TELL mi_row={} mi_col={} label=block_entry side={side} tell={} range={}",
243 rmi,
244 cmi,
245 dec.debug_bitpos(),
246 dec.debug_state().0
247 );
248 }
249 let skip_mode_ctx =
250 usize::from(neighbours.above_skip_mode[cmi]) + usize::from(neighbours.left_skip_mode[rmi]);
251 // lane-comppin r3: skip_mode desync isolation -- dump ctx + the pre-read
252 // MSAC tell so it can be diffed against an equivalent hook in libaom's
253 // own `read_skip_mode`/`av1_get_skip_mode_context`.
254 if crate::envflags::env_flag!("EC_AV1_SKIPMODE_DUMP") {
255 eprintln!(
256 "EC_SKIPMODE r={r} c={c} ctx={skip_mode_ctx} skip_mode_present={skip_mode_present} tell_before={}",
257 dec.debug_bitpos()
258 );
259 }
260 // spec 5.11.5 `inter_frame_mode_info`: `inter_segment_id(1)` before
261 // `skip_mode`/`skip`, `inter_segment_id(0)` after them.
262 let (seg_mi_r, seg_mi_c) = (rmi, cmi);
263 let (seg_w_mi, seg_h_mi) = (write_w / 4, write_h / 4);
264 inter_segment_id(
265 dec, cdfs, seg_mi_r, seg_mi_c, seg_w_mi, seg_h_mi, false, true, fctx,
266 );
267 // libaom `read_skip_mode` (decodemv.c): `if (!is_comp_ref_allowed(bsize))
268 // return 0` -- a 16x4 / 4x16 strip (min(bw, bh) == 4, blockd.h:65) reads
269 // no `skip_mode` symbol at all.
270 let comp_allowed = write_w.min(write_h) >= 8;
271 let skip_mode =
272 skip_mode_present && comp_allowed && dec.symbol(&mut cdfs.skip_mode[skip_mode_ctx]) == 1;
273 if crate::envflags::env_flag!("EC_AV1_SKIPMODE_DUMP") {
274 eprintln!(
275 "EC_SKIPMODE r={r} c={c} result={skip_mode} tell_after={}",
276 dec.debug_bitpos()
277 );
278 }
279 if skip_mode {
280 hit!(SKIP_MODE_HITS);
281 }
282
283 let skip_ctx = usize::from(neighbours.above_skip[cmi]) + usize::from(neighbours.left_skip[rmi]);
284 let skip = skip_mode || dec.symbol(&mut cdfs.skip[skip_ctx]) == 1;
285 inter_segment_id(
286 dec, cdfs, seg_mi_r, seg_mi_c, seg_w_mi, seg_h_mi, skip, false, fctx,
287 );
288 maybe_read_cdef_idx(dec, rmi, cmi, (write_w / MI, write_h / MI), skip, fctx);
289 // lane-inter4 r1: spec 5.11.6's `bSize == sbSize` -- a 64x32/64x16 strip
290 // is NOT the whole superblock, so its `skip` never suppresses the read.
291 // lane-sb128c r7: `sbSize` is 128 under `--sb-size=128`, so the whole-block
292 // test is against the sequence's own superblock side, not a hardcoded 64.
293 let sb_px = sb_mi_cur(fctx) as usize * 4;
294 let whole_sb = side == sb_px && write_w == sb_px && write_h == sb_px;
295 maybe_read_delta_q(dec, cdfs, rmi, cmi, whole_sb, skip, fctx);
296 maybe_read_delta_lf(dec, cdfs, rmi, cmi, whole_sb, skip, fctx);
297 let (has_above, has_left) = (rmi > neighbours.tile_row0_mi, cmi > neighbours.tile_col0_mi);
298 let (above_inter, left_inter) = (neighbours.above_inter[cmi], neighbours.left_inter[rmi]);
299 let ii_ctx = intra_inter_ctx(has_above, has_left, above_inter, left_inter);
300 let is_inter = skip_mode || dec.symbol(&mut cdfs.intra_inter[ii_ctx]) == 1;
301 // lane-sb128c r1: this round wires the INTER half of a 128x128 block (four
302 // TX_64X64 luma units and four TX_32X32 chroma units interleaved per mu
303 // chunk). An INTRA block at that size inside an inter frame takes the
304 // intra tail below, which still codes one whole-block chroma unit and one
305 // `read_tx_size` at 128 -- refuse by name instead of mis-decoding it.
306 if crate::envflags::env_flag!("EC_TRACE_MODE") {
307 eprintln!(
308 "EC_MODE mi_row={} mi_col={} rng={} is_inter={} skip={} side={side}",
309 rmi,
310 cmi,
311 dec.debug_state().0,
312 u8::from(is_inter),
313 u8::from(skip)
314 );
315 }
316 // lane-warp r5: see `reject_residual` -- a square-block decode of a
317 // HORZ_B strip is symbol-exact only for `skip` (no residual, no
318 // motion_mode/warp symbol). lane-intrarect r1 MOVED this gate from just
319 // after the `skip` read to here, past `is_inter` (class
320 // [[refusal-short-circuits-its-own-code]]: at the old place it refused
321 // 24/24 attempts of this lane's own gate before the intra arm below
322 // could run). An INTRA strip with its true `write_w`x`write_h` footprint
323 // now codes its residual rectangularly ([`decode_intra_rect_in_inter`]);
324 // an INTER one, and a strip decoded with square dimensions anyway, still
325 // cannot.
326 // merge b51719f+lane-intrarect: main's `rect_inter_residual_supported`
327 // term stays -- lane-inter4's 16x8/8x16 inter leaves DO code a
328 // rectangular residual, and the lane's condition (written before that
329 // landed) refused every one of them.
330 if reject_residual
331 && !skip
332 && !rect_inter_residual_supported(write_w, write_h)
333 && (is_inter || (write_w == side && write_h == side))
334 {
335 if crate::envflags::env_flag!("EC_RECTRES") {
336 eprintln!(
337 "EC_RECTRES w={write_w} h={write_h} side={side} skip={} is_inter={} mi_row={rmi} mi_col={cmi}",
338 u8::from(skip),
339 u8::from(is_inter)
340 );
341 }
342 return Err(unsupported(
343 "a non-skip rectangular (HORZ/VERT/HORZ_B) strip needs rectangular residual coding",
344 ));
345 }
346 if crate::envflags::env_flag!("EC_AV1_TELL") {
347 eprintln!(
348 "TELL mi_row={} mi_col={} label=post_is_inter skip_mode={skip_mode} skip={skip} is_inter={is_inter} tell={} range={}",
349 rmi,
350 cmi,
351 dec.debug_bitpos(),
352 dec.debug_state().0
353 );
354 }
355
356 let mode_for_tx;
357 let uv_predict_mode;
358 let (luma_grid, u_grid, v_grid);
359 // The rect_tu arms' chroma helper returns its TRUE per-unit coefficient
360 // context stamps (see `read_inter_rect_chroma`); they are appended to
361 // `mu_chroma_units` below so the block tail replays them after the
362 // whole-block record. `mu_chroma`'s own square-mu derivation cannot
363 // express a non-square strip's units.
364 let mut rect_chroma_units: Vec<((usize, usize), usize, Grid, usize, usize)> = Vec::new();
365 let block_filter;
366 let ref_frame_for_lf;
367 let globalmv_for_lf;
368 // lane-av1comp: `Some((comp_group_idx, compound_idx))` only for a real
369 // `COMPOUND_REFERENCE` block -- applied to `neighbours` *after* the
370 // common `record_inter` call below (which would otherwise stamp the
371 // single-ref ALTREF-special-case default over it).
372 let mut compound_ctx: Option<(i8, u8, u8)> = None;
373 // lane-txselect: `read_block_tx_size`'s leaf transform units (spec
374 // 5.11.17), `Some` only when this block's var-tx tree resolved to more
375 // than one luma transform -- read back by the residual loop, the
376 // coefficient-context write-back and the loop-filter grid fill below.
377 let vartx_leaves: Option<Vec<(usize, usize, usize, usize)>>;
378 // lane-t900 r22: this block's own palette state, stamped into the
379 // mi-granular neighbour bands at the tail below for EVERY block --
380 // libaom's `MB_MODE_INFO.palette_size` is 0 on an inter block, and
381 // `av1_get_palette_cache`/`av1_get_palette_mode_ctx` read those cells, so
382 // leaving a previous palette block's colours standing here would feed the
383 // next intra block a cache libaom never builds.
384 let mut palette_record: (usize, [u16; 8], usize, [u16; 8]) = (0, [0u16; 8], 0, [0u16; 8]);
385 let at_mi = at;
386 // lane-vert46 r1: set when this block's chroma was coded as one 32x32 unit
387 // per 64x64 "mu" chunk (any side above 64), so the end-of-block record
388 // below must NOT re-stamp the chroma neighbour context from the assembled
389 // whole-block grid.
390 let mut mu_chroma = false;
391 if is_inter {
392 let above_nbr = has_above.then(|| NeighbourRef {
393 is_inter: above_inter,
394 ref0: if above_inter {
395 neighbours.above_ref[cmi]
396 } else {
397 0
398 },
399 ref1: neighbours.above_ref1[cmi],
400 uni: neighbours.above_ref1[cmi]
401 .is_some_and(|r1| is_uni_comp_ref(neighbours.above_ref[cmi], r1)),
402 });
403 let left_nbr = has_left.then(|| NeighbourRef {
404 is_inter: left_inter,
405 ref0: if left_inter {
406 neighbours.left_ref[rmi]
407 } else {
408 0
409 },
410 ref1: neighbours.left_ref1[rmi],
411 uni: neighbours.left_ref1[rmi]
412 .is_some_and(|r1| is_uni_comp_ref(neighbours.left_ref[rmi], r1)),
413 });
414 if crate::envflags::env_flag!("EC_AV1_COMPIDX_DUMP") {
415 eprintln!(
416 "EC_PRECOMP r={r} c={c} skip_mode={skip_mode} skip={skip} reference_select={reference_select} tell={}",
417 dec.debug_bitpos()
418 );
419 }
420 let is_compound = skip_mode
421 || (reference_select
422 // `is_comp_ref_allowed` (blockd.h:65) gates the
423 // `comp_mode` read exactly as it gates `skip_mode`.
424 && comp_allowed
425 && read_comp_mode(dec, cdfs, above_nbr, left_nbr));
426 if is_compound {
427 let (ref0, ref1) = if skip_mode {
428 (skip_mode_frame[0] as i8, skip_mode_frame[1] as i8)
429 } else {
430 read_compound_ref_frames(
431 dec,
432 cdfs,
433 above_nbr,
434 left_nbr,
435 neighbours.above_ref[cmi],
436 neighbours.left_ref[rmi],
437 )
438 };
439 let (mi_row, mi_col) = (rmi, cmi);
440 // lane-rect r2: true footprint, mirroring the single-ref path below.
441 let bw4 = write_w / 4;
442 let bh4 = write_h / 4;
443 // spec 7.10.2.8, doubled per side (`CompoundTplArgs`): same
444 // `use_ref_frame_mvs` gating as the single-ref `tpl` build
445 // below, just one `get_relative_dist` per reference.
446 let comp_tpl = tpl_frame.map(|t| crate::mvstack::CompoundTplArgs {
447 field: t.field,
448 cur_offset_0: crate::motion_field::get_relative_dist(
449 t.order_hint_bits,
450 t.order_hint,
451 t.ref_order_hints[(ref0 - LAST_FRAME) as usize],
452 ),
453 cur_offset_1: crate::motion_field::get_relative_dist(
454 t.order_hint_bits,
455 t.order_hint,
456 t.ref_order_hints[(ref1 - LAST_FRAME) as usize],
457 ),
458 allow_high_precision_mv,
459 force_integer_mv,
460 });
461 let gm_table = build_gm_mv_table(
462 global_motion,
463 mi_row,
464 mi_col,
465 bw4,
466 bh4,
467 allow_high_precision_mv,
468 force_integer_mv,
469 );
470 let comp_stack = crate::mvstack::find_mv_stack_compound(
471 grid,
472 mi_row,
473 mi_col,
474 bw4,
475 bh4,
476 (ref0, ref1),
477 mi_cols as usize,
478 mi_rows as usize,
479 sign_bias_table,
480 &gm_table,
481 comp_tpl,
482 );
483 let compound_mode = if skip_mode {
484 0 // NEAREST_NEARESTMV, forced -- no mode symbol read
485 } else {
486 read_inter_compound_mode(dec, cdfs, comp_stack.new_mv_ctx, comp_stack.ref_mv_ctx)
487 };
488 let (mv0, mv1) = assign_compound_mv(
489 dec,
490 cdfs,
491 &comp_stack,
492 compound_mode,
493 allow_high_precision_mv,
494 force_integer_mv,
495 (
496 gm_table[(ref0 - LAST_FRAME) as usize],
497 gm_table[(ref1 - LAST_FRAME) as usize],
498 ),
499 fctx,
500 );
501 if crate::envflags::env_flag!("EC_TRACE_MODE") {
502 eprintln!(
503 "EC_MODE_VAL mi_row={mi_row} mi_col={mi_col} mode={} ref0={ref0} ref1={ref1} mv0=({},{}) stack={} rng={}",
504 compound_mode + 17,
505 mv0.0,
506 mv0.1,
507 comp_stack.entries.len(),
508 dec.debug_state().0
509 );
510 for (i, e) in comp_stack.entries.iter().enumerate() {
511 eprintln!(
512 "EC_STACK mi_row={mi_row} mi_col={mi_col} i={i} this=({},{}) comp=({},{}) w={}",
513 e.mv0.0, e.mv0.1, e.mv1.0, e.mv1.1, e.weight
514 );
515 }
516 }
517 let is_globalmv = compound_mode == 6; // GLOBAL_GLOBALMV
518 // be non-TRANSLATION (IDENTITY counts) for the compound block's
519 // interp-filter read to be suppressed.
520 let gm_nontrans_models = is_globalmv
521 && global_motion[(ref0 - LAST_FRAME) as usize].model
522 != ec_av1_syntax::WarpModel::Translation
523 && global_motion[(ref1 - LAST_FRAME) as usize].model
524 != ec_av1_syntax::WarpModel::Translation;
525 // lane-t900 r9: `is_nontrans_global_motion` bails at
526 // `AOMMIN(mi_size_wide, mi_size_high) < 2` before it looks at any
527 // model, so a 4-px-sided block still reads its interp filter.
528 let gm_nontrans = gm_nontrans_models && bw4.min(bh4) >= 2;
529 if gm_nontrans_models && !gm_nontrans {
530 hit!(GM_NONTRANS_SMALL_SIDE_HITS);
531 }
532 // lane-defer8: both slots' pictures are resolved inside the
533 // prediction closure; the parse only needs their SIZES.
534 // lane-cwarp r1: `is_global_mv_block` (blockd.h:421-429) is
535 // PER REFERENCE SLOT -- mode GLOBAL_GLOBALMV, block >= 8x8, and
536 // THAT slot's own gm model > TRANSLATION. `bw4`/`bh4` come from
537 // the write extent below, so recompute the size bound from
538 // `side` here (the block's own size, what libaom's `bsize`
539 // is).
540 let is_global_mv0 = is_globalmv
541 && side >= 8
542 && global_motion[(ref0 - LAST_FRAME) as usize].model as u8 > 1;
543 let is_global_mv1 = is_globalmv
544 && side >= 8
545 && global_motion[(ref1 - LAST_FRAME) as usize].model as u8 > 1;
546 // lane-av1422warp r33 (reviewer P3): the top-half census is
547 // placed HERE, after `is_global_mv0`/`is_global_mv1`, so it
548 // counts the same blocks libaom's `is_global_mv_block` does --
549 // mode GLOBAL_GLOBALMV AND block >= 8x8 AND that slot's own gm
550 // model > TRANSLATION. An earlier placement tested the model term
551 // alone, which counts blocks that are not global-mv blocks at
552 // all, so its number was an upper bound dressed up as an
553 // engagement figure.
554 if mi_row < mi_rows as usize / 2 {
555 if is_globalmv {
556 TOP_HALF_COMPOUND_HITS.with(|c| c.set(c.get() + 1));
557 }
558 if is_global_mv0 || is_global_mv1 {
559 TOP_HALF_WARP_HITS.with(|c| c.set(c.get() + 1));
560 }
561 }
562 // libaom `av1_init_warp_params` + `allow_warp`'s
563 // `global_warp_allowed` branch, run once per reference for a
564 // compound block exactly as for a single-ref one (reconinter.c
565 // `build_inter_predictors_8x8_and_bigger` loops over `ref` and
566 // calls `av1_init_warp_params` inside the loop) -- WARPED_CAUSAL
567 // never reaches here (spec 5.11.27 / libaom `read_motion_mode`:
568 // `has_second_ref` returns SIMPLE_TRANSLATION), so the local-warp
569 // arm of `allow_warp` is dead on this path and only the global
570 // one applies. `force_integer_mv` short-circuits
571 // `av1_init_warp_params` before `allow_warp`.
572 let compound_warp = |ref_frame: i8, eligible: bool| {
573 if eligible
574 && !force_integer_mv
575 && !global_motion[(ref_frame - LAST_FRAME) as usize].invalid
576 {
577 crate::warp::global_warp_params(
578 global_motion[(ref_frame - LAST_FRAME) as usize].params,
579 )
580 } else {
581 None
582 }
583 };
584 // lane-t900 r28 (class branch-dropped-as-unreachable, swept out
585 // of the scaled-ref lift): libaom `allow_warp`
586 // (av1/common/reconinter.c:41) opens with `if (av1_is_scaled(sf))
587 // return 0;` PER SLOT, so a compound tap whose own reference is
588 // scaled (superres) is predicted translationally while its
589 // sibling may still warp.
590 let ref_unscaled = |rf: i8| {
591 refpix
592 .dims(rf)
593 .is_some_and(|d| scale_of(d, frame_size).is_unscaled())
594 };
595 let warp0 = compound_warp(ref0, is_global_mv0).filter(|_| ref_unscaled(ref0));
596 let warp1 = compound_warp(ref1, is_global_mv1).filter(|_| ref_unscaled(ref1));
597 if warp0.is_some() || warp1.is_some() {
598 hit!(COMPOUND_WARP_HITS);
599 // lane-av112bitw: the 12-bit refusal that stood here is
600 // LIFTED with the compound-warp witness: the rotating-
601 // mandelbrot 12-bit recipe refused exactly here pre-lift and
602 // now decodes byte-exact vs ffmpeg through the same bumped
603 // `warp::warp_round_0` shifts
604 // (`a_real_compound_global_warp_12bit_stream_decodes_pixel_exact`).
605 }
606 // spec `get_ref_filter_type`: matches when EITHER of the
607 // neighbour's two references equals this block's own ref0 --
608 // `above_ref1`/`left_ref1` is the neighbour's real second
609 // reference (`record_compound_ctx`) when it was itself a
610 // compound block; checking `above_ref`/`left_ref` alone drops a
611 // real filter match down to the "no neighbour" sentinel
612 // whenever the shared reference sits in the neighbour's SECOND
613 // slot (lane-av1idx r2).
614 let above_filter_ctx =
615 if neighbours.above_ref[cmi] == ref0 || neighbours.above_ref1[cmi] == Some(ref0) {
616 neighbours.above_filter[cmi]
617 } else {
618 [3, 3]
619 };
620 let left_filter_ctx =
621 if neighbours.left_ref[rmi] == ref0 || neighbours.left_ref1[rmi] == Some(ref0) {
622 neighbours.left_filter[rmi]
623 } else {
624 [3, 3]
625 };
626 globalmv_for_lf = is_globalmv;
627 ref_frame_for_lf = ref0;
628 mode_for_tx = 0;
629 uv_predict_mode = DC_PRED;
630 // lane-rect r2: `grid` (mvstack.rs's own MiGrid, distinct from
631 // `neighbours`) must be stamped with the block's true footprint
632 // too -- a square `bw4`x`bw4` stamp here claims a rect strip's
633 // NEXT strip's own rows/cols before it decodes, corrupting
634 // every later mvstack scan that reads this cell back.
635 grid.fill_rect(
636 mi_row,
637 mi_col,
638 bh4,
639 bw4,
640 MiInfo {
641 is_inter: true,
642 ref_frame: ref0,
643 ref_frame1: ref1,
644 mv1: mv16(mv1),
645 mv: mv16(mv0),
646 is_new_mv: matches!(compound_mode, 2 | 3 | 4 | 5 | 7),
647 size: bw4 as u8,
648 size_h: bh4 as u8,
649 // libaom `is_global_mv_block`: per-ref-slot, mode
650 // GLOBAL_GLOBALMV, block >= 8x8, and THAT slot's
651 // own gm model > TRANSLATION (IDENTITY excluded
652 // too -- the two-predicate trap, distinct from
653 // `gm_nontrans` above which allows IDENTITY).
654 is_global_mv0,
655 is_global_mv1,
656 },
657 );
658 // spec 5.11.25's `comp_group_idx`/`compound_idx`: only read when
659 // masked compound / distance-weighted compound are actually
660 // enabled for this stream -- `is_any_masked_compound_used_here`
661 // is `true` for every block size this decoder ever reaches
662 // (`min(side,side) >= 8`, `COMPOUND_DIFFWTD` always eligible
663 // there), so the gate reduces to the sequence header bit; kept
664 // as an explicit call so a future smaller block size is not
665 // silently wrong (libaom `reconinter.h::is_any_masked_compound_used`).
666 let group_ctx = get_comp_group_idx_context(neighbours, (rmi, cmi), side);
667 let comp_group_idx = if !skip_mode
668 && enable_masked_compound
669 && is_any_masked_compound_used_here(write_w, write_h)
670 {
671 crate::msac::SymbolDecoder::set_symr_cdf("comp_group_idx");
672 dec.symbol(&mut cdfs.comp_group_idx[group_ctx])
673 } else {
674 0
675 };
676 if crate::envflags::env_flag!("EC_TRACE_MODE") {
677 eprintln!(
678 "EC_CGI mi_row={rmi} mi_col={cmi} ctx={group_ctx} val={comp_group_idx} rng={}",
679 dec.debug_state().0
680 );
681 }
682 // lane-maskcomp r2 / lane-wedge r3: `Some(mask_type)` for a
683 // `COMPOUND_DIFFWTD` block, `Some(mask)` (wedge codebook lookup)
684 // for a `COMPOUND_WEDGE` block -- exactly one of the two is set
685 // when `comp_group_idx == 1`.
686 let mut diffwtd_mask_type: Option<u8> = None;
687 let mut wedge_mask: Option<&'static [u8]> = None;
688 if comp_group_idx == 1 {
689 // lane-maskcomp r1: `read_compound_type` (spec 5.11.25,
690 // libaom `decodemv.c` 1634-1656). `is_interinter_compound_used
691 // (COMPOUND_WEDGE, bsize)` (`av1_is_wedge_used`,
692 // `av1_wedge_params_lookup`) is true for every square bsize
693 // this leaf reaches (8x8/16x16/32x32 all have
694 // `wedge_types > 0`), so `compound_type` always reads a real
695 // symbol here -- no alphabet collapse to worry about at this
696 // block-size family.
697 // lane-mcomp64 r1: `av1_is_wedge_used` is false at 64x64
698 // (`av1_wedge_params_lookup[BLOCK_64X64].wedge_types == 0`),
699 // so libaom's `read_compound_type` (decodemv.c 1634-1656)
700 // writes NO `compound_type` symbol there and INFERS
701 // `COMPOUND_DIFFWTD` -- only the 1-bit `mask_type` literal
702 // follows. `None` here is exactly that inferred arm.
703 // lane-inter16ab r3: the row is this block's TRUE
704 // `BLOCK_SIZES_ALL` index, not the enclosing square `side`'s
705 // -- a 16x8 strip reads `compound_type_cdf[BLOCK_16X8]`.
706 let wedge_bsize = bsize_all_index(write_w, write_h)
707 .filter(|_| wedge_used_bsize(write_w, write_h));
708 let compound_type = match wedge_bsize {
709 Some(b) => dec.symbol(&mut cdfs.compound_type[b]),
710 None => 1,
711 };
712 if let Some(wedge_bsize) = wedge_bsize.filter(|_| compound_type == 0) {
713 // COMPOUND_WEDGE: lane-wedge r3, codebook checksum-
714 // verified vs independent C dump (wedge.rs).
715 let wedge_index = dec.symbol(&mut cdfs.wedge_idx[wedge_bsize]);
716 let wedge_sign = dec.literal(1);
717 hit!(WEDGE_HITS);
718 if let Some(k) = rect_wedge_slot(write_w, write_h) {
719 hit_do! {
720 RECT_WEDGE_HITS.with(|c| {
721 let mut a = c.get();
722 a[k] += 1;
723 c.set(a);
724 });
725 }
726 }
727 // lane-inter16ab r5: the codebook row is this block's TRUE
728 // footprint (`av1_wedge_params_lookup[bsize]`: hgtw for
729 // h>w, hltw for h<w, plus that bsize's own signflip row).
730 // Masks are padded to stride `max(bw,bh) == side`, so the
731 // square blend below indexes them unchanged and the
732 // padding never reaches the frame (only `write_w x
733 // write_h` is written out).
734 wedge_mask = Some(
735 crate::wedge::wedge_masks()
736 .codebook(write_w, write_h)
737 .mask(wedge_sign as usize, wedge_index),
738 );
739 } else {
740 // COMPOUND_DIFFWTD: `mask_type`, MAX_DIFFWTD_MASK_BITS==1.
741 let mask_type = dec.literal(1);
742 diffwtd_mask_type = Some(mask_type as u8);
743 if wedge_bsize.is_none() {
744 hit!(DIFFWTD_INFERRED_HITS);
745 }
746 }
747 hit!(MASKED_COMPOUND_HITS);
748 if write_w != write_h {
749 hit!(RECT_MASKED_COMPOUND_HITS);
750 }
751 }
752 // corner-cut (lane-av1comp r16/r17, lane-av1blend r1): r16/r17
753 // called this a `predict_compound_intermediate`/`combine_compound`
754 // reconstruction defect. r1 FALSIFIED that hypothesis outright:
755 // unmasking and running the real gates
756 // (a_real_aomenc_stream_with_reference_select_reads_comp_mode_correctly,
757 // a_real_aomenc_stream_with_compound_references_decodes_pixel_exact)
758 // shows the plain-average blend is bit-exact *most of the time*
759 // -- `combine_compound`'s two-shift-in-one round2 was proven
760 // algebraically identical to libaom's separate
761 // `>>DIST_PRECISION_BITS`-then-`ROUND_POWER_OF_TWO` sequence
762 // (av1_dist_wtd_convolve_2d_c, convolve.c:402-415) for every
763 // input, and a whole-pel zero-MV compound block (mv0=mv1=(0,0),
764 // the common case a lag-in-frames GOP's hidden altref frames
765 // hit) reproduces aomdec's own pre-deblock dump byte-for-byte
766 // for several consecutive frames.
767 //
768 // The real defect a live gate run isolated (seed 49,
769 // `fixtures/av1blend-r1-mismatch.obu`): decode-order frame 0
770 // (keyframe) and frame 1 (first hidden altref, `ref1 ==
771 // ALTREF_FRAME`) match aomdec's own pre-deblock dump byte for
772 // byte; decode-order frame 2 -- same shape, same zero MV, only
773 // difference `ref1 == BWDREF_FRAME` instead of `ALTREF_FRAME` --
774 // is the *first* frame to diverge (881/6144 luma+chroma bytes).
775 // Every later frame up through decode-order 9 also mismatches,
776 // *including* frame 8, whose four blocks are back to
777 // `ref1 == ALTREF_FRAME` only -- ruling out "BWDREF_FRAME's MC
778 // math is wrong" (that block's own math is proven exact) in
779 // favour of "the picture sitting in some DPB slot is already
780 // wrong by the time frame 2 reads it, and the corruption
781 // propagates through refresh_frame_flags into every later
782 // reference regardless of which named ref a later block picks".
783 // Frames 10 through 18 (once the GOP's lineage stops tracing
784 // back through the bad slot) are bit-exact again. This points at
785 // reference-slot bookkeeping (`ref_slots`/`refresh_frame_flags`
786 // in `stream.rs`'s `decode_stream`, or `ref_order_hints`/DPB
787 // indexing feeding `dist_wtd_comp_weight_assign`) around
788 // BWDREF_FRAME specifically, not at the MC/blend arithmetic in
789 // mc.rs. Not isolated further within lane-av1blend's budget --
790 // refuse by name rather than ship a blend that is right only
791 // sometimes. Ceiling: r13/r14's MC plumbing above this line
792 // stays -- only the plain-average reconstruct is masked off
793 // again. Upgrade: instrument `ref_slots`/`refresh_frame_flags`
794 // per decode-order frame (which slot each `Frame` OBU refreshes,
795 // and which slot each `ref_frame_idx[BWDREF_FRAME - LAST_FRAME]`
796 // reads) against aomdec's own `RefCntBuffer` bookkeeping on
797 // `fixtures/av1blend-r1-mismatch.obu`, starting at decode-order
798 // frame 2.
799 //
800 // r2 (fresh fixture, seed 45, same recipe -- the original r1
801 // fixture is gitignored and could not be regenerated byte-exact):
802 // static review of `stream.rs`'s `ref_slots`/`refresh_frame_flags`/
803 // `cdf_slots` bookkeeping and the `show_existing_frame` refresh
804 // arm (spec 7.21: only refreshes on a shown KEY frame, which the
805 // parser already enforces at `frame.rs:791/815` -- `refresh_frame_flags`
806 // is forced 0 on every non-key `show_existing_frame`) turned up
807 // nothing wrong; `other_refs`/`ref_planes` slot indexing for
808 // `BWDREF_FRAME` is spec-correct too. A live unmasked run
809 // isolated a *different, smaller* shape than r1's: decode-order
810 // frame 3 (a `ref1 == BWDREF_FRAME`, zero-MV, simple-average
811 // compound block) is the first to diverge from ffmpeg (worst
812 // delta 1, luma only, clustered at 32-pixel block edges) even
813 // though BOTH its compound inputs -- the key frame and
814 // decode-order frame 2's own picture (`ref1 == ALTREF_FRAME`,
815 // itself proven bit-exact: it is later re-shown via
816 // `show_existing_frame` and matches ffmpeg exactly) -- are
817 // independently pixel-exact, and the blend arithmetic is the
818 // same proven-exact formula. That rules out "wrong slot"/"stale
819 // picture" for this fixture: the inputs are right, so the defect
820 // is in decode-order frame 3's *own* per-block decode (entropy
821 // desync from compound-CDF adaptation carried across a slot, or
822 // the compound mode/MV read itself) -- not in `ref_slots`/
823 // `refresh_frame_flags`. Not isolated further within budget.
824 // Upgrade: bisect decode-order frame 3's tile decode against a
825 // synced `EC_TOK`/`EC_PART` trace from the instrumented aomdec
826 // build (`/tmp/libaom-src/build/decoder-debug/aomdec`) on
827 // `fixtures/av1blend-r1-mismatch.obu` (seed 45 now, not seed 49)
828 // -- the pre-deblock dump frame-index alignment between this
829 // crate and that aomdec build is NOT 1:1 on this fixture (23
830 // dumps here vs 24 there), so calibrate that first.
831 // r3 (this round): pre-deblock buffers were bisected byte-for-byte
832 // against an instrumented aomdec build (frame-index alignment
833 // recalibrated via content-hash matching rather than the raw
834 // dump count -- the keyframe never calls a pre-deblock dump at
835 // all, since only `decode_inter_frame_tile` does, which is the
836 // whole "23 vs 24" gap, not a real bug).
837 // Decode-order frame 3 (`fixtures/av1blend-r1-mismatch.obu`,
838 // seed 45) is still the first divergence: PRE-deblock differs
839 // from aomdec's own pre-deblock dump by up to 1, on ~20% of
840 // luma pixels, clustered where its two compound inputs' pixel
841 // values disagree by more than a couple of levels -- both
842 // inputs (the keyframe and decode-order frame 2's own picture)
843 // are independently proven bit-exact (pre- and post-deblock)
844 // against aomdec, and `decodemv`'s own EC_TRACE (mode/mv/skip)
845 // matches this decoder's block loop exactly for every block in
846 // this frame. Solving `aomval = (key*w0 + bwd*w1 + 8) >> 4` for
847 // every mismatching pixel against `key`/`bwd` (both known-exact)
848 // picks out `w0=9, w1=7` for the majority (525/846) with zero
849 // matches for the plain average this decoder actually emits or
850 // the reversed `w0=7, w1=9` -- i.e. `compound_idx` is being
851 // decoded as `1` (simple average, spec `COMPOUND_AVERAGE`) when
852 // the real bitstream encoded `0` (`COMPOUND_DISTWTD`,
853 // `dist_wtd_comp_weight_assign`'s own weights for this frame's
854 // order-hint distances 2/3 land on exactly `(9, 7)` -- this
855 // decoder's own `dist_wtd_comp_weight_assign` was independently
856 // re-verified against `QUANT_DIST_WEIGHT`/`QUANT_DIST_LOOKUP_TABLE`
857 // by hand for these distances and gives the same `(9, 7)`).
858 // `get_comp_index_context` was checked line-for-line against
859 // libaom's `pred_common.h` (the `fwd`/`bck` distance halves, the
860 // `above_mi`/`left_mi` `has_second_ref`/`ALTREF_FRAME` fallback,
861 // the `3 * offset` term) and matches; `Default_Compound_Idx_Cdf`
862 // (`cdf.rs`'s `COMPOUND_IDX`) matches `entropymode.c`'s
863 // `default_compound_idx_cdfs` value-for-value; the read is
864 // gated on `!skip_mode && enable_jnt_comp`, matching libaom's
865 // `has_second_ref(mbmi) && !mbmi->skip_mode` outer guard plus
866 // `order_hint_info.enable_dist_wtd_comp` inner guard. The
867 // remaining 321/846 mismatched pixels do NOT fit `(9, 7)`
868 // either -- some blocks in this frame plausibly decode a
869 // genuinely different (correct, context-dependent) compound_idx
870 // than block (0,0)'s own -- so this is not simply "always wrong
871 // the same way": table/context/gate are all individually
872 // verified correct, yet the decoded VALUE at this specific
873 // symbol is wrong at least once in this frame. Not isolated
874 // further within budget. Upgrade: instrument `dec.symbol`'s own
875 // range/bit state (`rng`/`tell`, matching aomdec's own EC_PART
876 // trace fields) immediately before and after the `compound_idx`
877 // read specifically, not just the surrounding mode/mv/skip
878 // trace which does match -- the desync (if any) is local to
879 // this one read, not a wider stream desync.
880 //
881 // r5 (this round): the `resolve_interp_filter` call used to sit
882 // right here, ahead of `comp_group_idx`/`compound_idx` -- spec
883 // 5.11.19/libaom `decodemv.c:1575` reads `interp_filter` AFTER
884 // `read_compound_type`, not before, so that was a genuine
885 // ordering bug for a Switchable-filter frame (moved below, past
886 // the `compound_ctx = Some(...)` line). It does NOT explain this
887 // fixture's own mismatch, though: `av1blend-r1-mismatch.obu`'s
888 // header carries a *fixed* interpolation filter
889 // (`interp_fixed = Some(Regular)`, confirmed live via a
890 // one-off dump), so `resolve_interp_filter` never read a symbol
891 // in either position -- zero bits moved, decoded values
892 // unchanged before and after the reorder. Kept anyway: it is a
893 // real spec-order fix for the next Switchable-filter compound
894 // stream, independently correct regardless of this fixture.
895 //
896 // Also ruled out this round: comparing this crate's own
897 // `SymbolDecoder::debug_bitpos()` against aomdec's
898 // `aom_reader_tell()` at the block boundary is NOT a valid
899 // desync probe -- they are different quantities (raw consumed
900 // input bits vs. an entropy-cost estimate that advances without
901 // a literal byte fetch), so equal/unequal bitpos proves nothing
902 // either way. Do not repeat that comparison; instead trace
903 // `range`/`value` (this crate) against libaom's own internal
904 // `dif`/`rng` (needs new instrumentation in both) immediately
905 // after each read *upstream* of this one in the same block:
906 // `read_comp_mode`, `read_compound_ref_frames` (comp_ref_type/
907 // uni_comp_ref/comp_ref/comp_bwdref), `read_inter_compound_mode`,
908 // and the DRL loop. All of those independently decoded the SAME
909 // values this crate and aomdec both show (ref0=1, ref1=5, mode
910 // NEAREST_NEARESTMV, mv=(0,0)) -- but an upstream *context*
911 // miscomputation can still decode the identical symbol value off
912 // a slightly different CDF, narrowing `range` by a different
913 // amount than aomdec without changing the decoded value itself,
914 // and only surface as a wrong decode a few reads later exactly
915 // like this. `get_comp_index_context` and the `COMPOUND_IDX`
916 // table were independently re-verified again this round and are
917 // still correct; the four candidates above were not re-audited
918 // this round. Refusing here (rather than shipping a value-exact-
919 // most-of-the-time blend) until one of them is found wrong.
920 // r6/r7: found and fixed one real bug (mvstack.rs ref_ctx family
921 // undercounted compound neighbours in the single_ref_p*/comp_ref*/
922 // comp_bwdref* context reads -- kept, real fix). Unmasking on
923 // that fix alone and running the wide gate sweep (many seeds,
924 // `a_real_aomenc_stream_with_compound_references_decodes_pixel_exact`/
925 // `a_real_aomenc_stream_with_reference_select_reads_comp_mode_correctly`,
926 // cq-level=45) still mismatches roughly half the time, at
927 // different frames each run for the SAME seed -- aomenc's own
928 // cpu-used=0 RD search is run-to-run nondeterministic (noted
929 // above), so this is a second, still-unfound compound_idx defect,
930 // not the one r6/r7 fixed. r6's own residual-mismatch note (321/
931 // 846 pixels not fitting the (9,7)-vs-plain-average hypothesis
932 // either) already flagged more than one bug here. Re-masking:
933 // do not ship a blend that is right only sometimes.
934 // lane-av1idx r1: fixed one real bug this round
935 // (`switchable_interp_ctx` never folded in libaom's
936 // `INTER_FILTER_COMP_OFFSET` for a compound block's own
937 // interp_filter context -- kept). Pinned
938 // `fixtures/av1idx-refsel-pin.obu` (seed 61, reference_select
939 // gate) still mismatches after that fix (decode-order frame 8
940 // first, small deltas, worst 1-2, propagating through later
941 // frames via DPB reference) -- a second, still-unfound bug.
942 // Re-masking: see that fixture + `scratch_isolate_pinned_mismatch`
943 // (`EC_AV1_PIN=fixtures/av1idx-refsel-pin.obu EC_AV1_PIN_N=20`)
944 // for the next round's starting point.
945 // lane-maskcomp r2: `compound_idx` is only read when
946 // `comp_group_idx == 0` (libaom `decodemv.c:1606`) -- a masked
947 // (wedge/diffwtd) block never also carries a distance-weighted
948 // split. This gate was missing before r2 (dead code: the masked
949 // path always refused earlier, so it never executed with
950 // `comp_group_idx == 1` in practice).
951 let (fwd_offset, bck_offset, compound_idx) =
952 if comp_group_idx == 0 && !skip_mode && enable_jnt_comp {
953 let idx_ctx = get_comp_index_context(
954 neighbours,
955 (rmi, cmi),
956 side,
957 order_hint_bits,
958 order_hint,
959 ref_order_hints[(ref0 - LAST_FRAME) as usize],
960 ref_order_hints[(ref1 - LAST_FRAME) as usize],
961 );
962 crate::msac::SymbolDecoder::set_symr_cdf("compound_idx");
963 let idx = dec.symbol(&mut cdfs.compound_idx[idx_ctx]);
964 if crate::envflags::env_flag!("EC_TRACE_MODE") {
965 eprintln!(
966 "EC_CIDX mi_row={rmi} mi_col={cmi} ctx={idx_ctx} val={idx} rng={}",
967 dec.debug_state().0
968 );
969 }
970 if idx == 1 {
971 (8, 8, 1u8)
972 } else {
973 // lane-av1distwtd: `idx == 0` is the
974 // distance-weighted arm -- the only branch whose
975 // weights are NOT the constant (8, 8) split.
976 hit!(DIST_WTD_COMP_HITS);
977 let (fwd, bck) = crate::compound::dist_wtd_comp_weight_assign(
978 order_hint_bits,
979 order_hint,
980 ref_order_hints[(ref0 - LAST_FRAME) as usize],
981 ref_order_hints[(ref1 - LAST_FRAME) as usize],
982 );
983 (fwd, bck, 0u8)
984 }
985 } else {
986 (8, 8, 1u8)
987 };
988 compound_ctx = Some((ref1, comp_group_idx as u8, compound_idx));
989 if crate::envflags::env_flag!("EC_AV1_COMPIDX_DUMP") {
990 eprintln!(
991 "EC_COMPIDX mi_row={mi_row} mi_col={mi_col} bsize={side} mode={compound_mode} mv0=({},{}) mv1=({},{}) ref0={ref0} ref1={ref1} comp_group_idx={comp_group_idx} compound_idx={compound_idx} tell={}",
992 mv0.0,
993 mv0.1,
994 mv1.0,
995 mv1.1,
996 dec.debug_bitpos()
997 );
998 }
999
1000 // spec 5.11.19/libaom `decodemv.c` 1575: `read_mb_interp_filter`
1001 // comes AFTER `read_compound_type` (the `comp_group_idx`/
1002 // `compound_idx` reads just above), not before -- moved here
1003 // (lane-av1blend r5) from its old position ahead of those reads,
1004 // which stole their bits for a Switchable-filter compound block
1005 // and desynced every symbol read after it in the tile.
1006 // `av1_is_interp_needed` is also 0 for `skip_mode` blocks: the
1007 // filter is derived (Regular), never read -- same
1008 // conditional-read contract as the WARPED_CAUSAL suppression on
1009 // the single-ref path below.
1010 let (h_filter, v_filter, resolved_filter) = resolve_interp_filter(
1011 dec,
1012 cdfs,
1013 interp_fixed,
1014 enable_dual_filter,
1015 gm_nontrans || skip_mode,
1016 above_filter_ctx,
1017 left_filter_ctx,
1018 true,
1019 );
1020 block_filter = resolved_filter;
1021
1022 // lane-scaledref r1: each compound tap scales INDEPENDENTLY off
1023 // its own stored reference's luma width (spec 7.11.3.3 derives
1024 // x_scale_fp per reference; libaom `av1_setup_pre_planes` builds
1025 // one `scale_factors` per `ref_frame`), so the two taps can carry
1026 // different ratios in the same block. `REF_NO_SCALE` reduces
1027 // `predict_compound_intermediate`'s walk to the ordinary
1028 // stride-1 one, so the unscaled case is unchanged.
1029 let scale0 = scale_of(ref_dims(ref0, refpix)?, frame_size);
1030 let scale1 = scale_of(ref_dims(ref1, refpix)?, frame_size);
1031 if !scale0.is_unscaled() || !scale1.is_unscaled() {
1032 hit!(SCALED_COMPOUND_HITS);
1033 }
1034 // lane-scaledref r2: the MIXED case -- one tap scaled, the other
1035 // not -- is the one an all-frames-scaled recipe never produces,
1036 // and the one a single shared scale factor would get wrong.
1037 if (scale0.is_unscaled()) != (scale1.is_unscaled()) {
1038 hit!(MIXED_SCALE_COMPOUND_HITS);
1039 }
1040
1041 // lane-wave4 (the compound arm's half): this block's whole
1042 // prediction -- both references, the wedge/diffwtd/distance
1043 // blend, every plane -- as a closure, run here (see the
1044 // single-reference arm below). A compound block reads
1045 // neither `motion_mode` nor `interintra`, so nothing but the
1046 // blend itself lives in here.
1047 let build = move |_y: &mut PlaneBuf<'static>,
1048 _u: &mut PlaneBuf<'static>,
1049 _v: &mut PlaneBuf<'static>,
1050 refpix: &RefPix<'_>,
1051 fctx: &crate::decode::FrameCtx,
1052 out: &mut [Vec<u16>; 3]| {
1053 let (Some((py0, pu0, pv0)), Some((py1, pu1, pv1))) = (
1054 ref_planes_deferred(ref0, refpix),
1055 ref_planes_deferred(ref1, refpix),
1056 ) else {
1057 return;
1058 };
1059 let mut inter0_y = vec![0i32; side * side];
1060 mc::predict_compound_intermediate_kern(
1061 &py0.data,
1062 py0.width,
1063 py0.true_width,
1064 py0.true_height,
1065 mv_to_q4(px, mv0.1, 0),
1066 mv_to_q4(py, mv0.0, 0),
1067 scale0,
1068 side,
1069 side,
1070 write_w,
1071 write_h,
1072 h_filter,
1073 v_filter,
1074 &mut inter0_y,
1075 fctx,
1076 );
1077 // lane-cwarp r1: this reference's own GLOBAL warp replaces the
1078 // translational tap (libaom `av1_warp_plane` with
1079 // `conv_params->is_compound`); the blend below is unchanged.
1080 if let Some(wp) = warp0
1081 .as_ref()
1082 .filter(|_| warp_plane_allowed(write_w, write_h))
1083 {
1084 crate::warp::warp_affine_compound(
1085 wp,
1086 &py0.data,
1087 py0.true_width as i32,
1088 py0.true_height as i32,
1089 py0.width as i32,
1090 &mut inter0_y,
1091 px as i32,
1092 py as i32,
1093 side as i32,
1094 side as i32,
1095 side as i32,
1096 0,
1097 0,
1098 fctx,
1099 );
1100 }
1101 let mut inter1_y = vec![0i32; side * side];
1102 mc::predict_compound_intermediate_kern(
1103 &py1.data,
1104 py1.width,
1105 py1.true_width,
1106 py1.true_height,
1107 mv_to_q4(px, mv1.1, 0),
1108 mv_to_q4(py, mv1.0, 0),
1109 scale1,
1110 side,
1111 side,
1112 write_w,
1113 write_h,
1114 h_filter,
1115 v_filter,
1116 &mut inter1_y,
1117 fctx,
1118 );
1119 // lane-cwarp r1: this reference's own GLOBAL warp replaces the
1120 // translational tap (libaom `av1_warp_plane` with
1121 // `conv_params->is_compound`); the blend below is unchanged.
1122 if let Some(wp) = warp1
1123 .as_ref()
1124 .filter(|_| warp_plane_allowed(write_w, write_h))
1125 {
1126 crate::warp::warp_affine_compound(
1127 wp,
1128 &py1.data,
1129 py1.true_width as i32,
1130 py1.true_height as i32,
1131 py1.width as i32,
1132 &mut inter1_y,
1133 px as i32,
1134 py as i32,
1135 side as i32,
1136 side as i32,
1137 side as i32,
1138 0,
1139 0,
1140 fctx,
1141 );
1142 }
1143 let mut pred_y = refill(std::mem::take(&mut out[0]), side * side);
1144 let mut diffwtd_mask_y;
1145 // lane-wedge r3: `mask_y` is the DIFFWTD buffer just computed OR
1146 // the wedge codebook lookup (comp_group_idx==1's two mutually
1147 // exclusive branches) -- same [`mc::blend_masked_compound`] call
1148 // either way, only the mask source differs.
1149 let mask_y: Option<&[u8]> = if let Some(mask_type) = diffwtd_mask_type {
1150 diffwtd_mask_y = vec![0u8; side * side];
1151 hit!(DIFFWTD_HITS);
1152 mc::diffwtd_mask(
1153 &inter0_y,
1154 &inter1_y,
1155 mask_type == 1,
1156 &mut diffwtd_mask_y,
1157 fctx,
1158 );
1159 Some(diffwtd_mask_y.as_slice())
1160 } else {
1161 wedge_mask
1162 };
1163 if let Some(mask_y) = mask_y {
1164 mc::blend_masked_compound(
1165 &inter0_y,
1166 &inter1_y,
1167 mask_y,
1168 side,
1169 side,
1170 side,
1171 0,
1172 0,
1173 &mut pred_y,
1174 fctx,
1175 );
1176 } else {
1177 mc::combine_compound(
1178 &inter0_y,
1179 &inter1_y,
1180 fwd_offset,
1181 bck_offset,
1182 &mut pred_y,
1183 fctx,
1184 );
1185 }
1186
1187 let mut inter0_u = vec![0i32; chroma_stride * chroma_buf_h];
1188 mc::predict_compound_intermediate_kern(
1189 &pu0.data,
1190 pu0.width,
1191 pu0.true_width,
1192 pu0.true_height,
1193 mv_to_q4(cpx, mv0.1, ss_x(fctx)),
1194 mv_to_q4(cpy, mv0.0, ss_y(fctx)),
1195 scale0,
1196 chroma_stride,
1197 chroma_buf_h,
1198 write_chroma_w,
1199 write_chroma_h,
1200 h_filter,
1201 v_filter,
1202 &mut inter0_u,
1203 fctx,
1204 );
1205 // `av1_init_warp_params` bails when the PLANE's block is
1206 // narrower/shorter than 8 (`block_width < 8`), so a 4x4 chroma
1207 // block of an 8x8 luma block stays translational.
1208 if let Some(wp) = &warp0
1209 && warp_plane_allowed(write_chroma_w, write_chroma_h)
1210 {
1211 crate::warp::warp_affine_compound(
1212 wp,
1213 &pu0.data,
1214 pu0.true_width as i32,
1215 pu0.true_height as i32,
1216 pu0.width as i32,
1217 &mut inter0_u,
1218 cpx as i32,
1219 cpy as i32,
1220 chroma_stride as i32,
1221 chroma_buf_h as i32,
1222 chroma_stride as i32,
1223 ss_x(fctx) as i32,
1224 ss_y(fctx) as i32,
1225 fctx,
1226 );
1227 }
1228 let mut inter1_u = vec![0i32; chroma_stride * chroma_buf_h];
1229 mc::predict_compound_intermediate_kern(
1230 &pu1.data,
1231 pu1.width,
1232 pu1.true_width,
1233 pu1.true_height,
1234 mv_to_q4(cpx, mv1.1, ss_x(fctx)),
1235 mv_to_q4(cpy, mv1.0, ss_y(fctx)),
1236 scale1,
1237 chroma_stride,
1238 chroma_buf_h,
1239 write_chroma_w,
1240 write_chroma_h,
1241 h_filter,
1242 v_filter,
1243 &mut inter1_u,
1244 fctx,
1245 );
1246 // `av1_init_warp_params` bails when the PLANE's block is
1247 // narrower/shorter than 8 (`block_width < 8`), so a 4x4 chroma
1248 // block of an 8x8 luma block stays translational.
1249 if let Some(wp) = &warp1
1250 && warp_plane_allowed(write_chroma_w, write_chroma_h)
1251 {
1252 crate::warp::warp_affine_compound(
1253 wp,
1254 &pu1.data,
1255 pu1.true_width as i32,
1256 pu1.true_height as i32,
1257 pu1.width as i32,
1258 &mut inter1_u,
1259 cpx as i32,
1260 cpy as i32,
1261 chroma_stride as i32,
1262 chroma_buf_h as i32,
1263 chroma_stride as i32,
1264 ss_x(fctx) as i32,
1265 ss_y(fctx) as i32,
1266 fctx,
1267 );
1268 }
1269 let mut pred_u = refill(std::mem::take(&mut out[1]), chroma_stride * chroma_buf_h);
1270 if let Some(mask_y) = mask_y {
1271 // lane-av1-422blockhalf: the blend walks this plane's own
1272 // (chroma_stride x chroma_buf_h) rect through the plane's
1273 // real subsampling shifts -- the mask read is aom_dsp/
1274 // blend-a64-mask.c's `(1, 0)` branch at 4:2:2, the direct
1275 // luma sample at 4:2:0/4:4:4.
1276 mc::blend_masked_compound(
1277 &inter0_u,
1278 &inter1_u,
1279 mask_y,
1280 side,
1281 chroma_stride,
1282 chroma_buf_h,
1283 ss_x(fctx),
1284 ss_y(fctx),
1285 &mut pred_u,
1286 fctx,
1287 );
1288 } else {
1289 mc::combine_compound(
1290 &inter0_u,
1291 &inter1_u,
1292 fwd_offset,
1293 bck_offset,
1294 &mut pred_u,
1295 fctx,
1296 );
1297 }
1298
1299 let mut inter0_v = vec![0i32; chroma_stride * chroma_buf_h];
1300 mc::predict_compound_intermediate_kern(
1301 &pv0.data,
1302 pv0.width,
1303 pv0.true_width,
1304 pv0.true_height,
1305 mv_to_q4(cpx, mv0.1, ss_x(fctx)),
1306 mv_to_q4(cpy, mv0.0, ss_y(fctx)),
1307 scale0,
1308 chroma_stride,
1309 chroma_buf_h,
1310 write_chroma_w,
1311 write_chroma_h,
1312 h_filter,
1313 v_filter,
1314 &mut inter0_v,
1315 fctx,
1316 );
1317 // `av1_init_warp_params` bails when the PLANE's block is
1318 // narrower/shorter than 8 (`block_width < 8`), so a 4x4 chroma
1319 // block of an 8x8 luma block stays translational.
1320 if let Some(wp) = &warp0
1321 && warp_plane_allowed(write_chroma_w, write_chroma_h)
1322 {
1323 crate::warp::warp_affine_compound(
1324 wp,
1325 &pv0.data,
1326 pv0.true_width as i32,
1327 pv0.true_height as i32,
1328 pv0.width as i32,
1329 &mut inter0_v,
1330 cpx as i32,
1331 cpy as i32,
1332 chroma_stride as i32,
1333 chroma_buf_h as i32,
1334 chroma_stride as i32,
1335 ss_x(fctx) as i32,
1336 ss_y(fctx) as i32,
1337 fctx,
1338 );
1339 }
1340 let mut inter1_v = vec![0i32; chroma_stride * chroma_buf_h];
1341 mc::predict_compound_intermediate_kern(
1342 &pv1.data,
1343 pv1.width,
1344 pv1.true_width,
1345 pv1.true_height,
1346 mv_to_q4(cpx, mv1.1, ss_x(fctx)),
1347 mv_to_q4(cpy, mv1.0, ss_y(fctx)),
1348 scale1,
1349 chroma_stride,
1350 chroma_buf_h,
1351 write_chroma_w,
1352 write_chroma_h,
1353 h_filter,
1354 v_filter,
1355 &mut inter1_v,
1356 fctx,
1357 );
1358 // `av1_init_warp_params` bails when the PLANE's block is
1359 // narrower/shorter than 8 (`block_width < 8`), so a 4x4 chroma
1360 // block of an 8x8 luma block stays translational.
1361 if let Some(wp) = &warp1
1362 && warp_plane_allowed(write_chroma_w, write_chroma_h)
1363 {
1364 crate::warp::warp_affine_compound(
1365 wp,
1366 &pv1.data,
1367 pv1.true_width as i32,
1368 pv1.true_height as i32,
1369 pv1.width as i32,
1370 &mut inter1_v,
1371 cpx as i32,
1372 cpy as i32,
1373 chroma_stride as i32,
1374 chroma_buf_h as i32,
1375 chroma_stride as i32,
1376 ss_x(fctx) as i32,
1377 ss_y(fctx) as i32,
1378 fctx,
1379 );
1380 }
1381 let mut pred_v = refill(std::mem::take(&mut out[2]), chroma_stride * chroma_buf_h);
1382 if let Some(mask_y) = mask_y {
1383 // lane-av1-422blockhalf: the blend walks this plane's own
1384 // (chroma_stride x chroma_buf_h) rect through the plane's
1385 // real subsampling shifts -- the mask read is aom_dsp/
1386 // blend-a64-mask.c's `(1, 0)` branch at 4:2:2, the direct
1387 // luma sample at 4:2:0/4:4:4.
1388 mc::blend_masked_compound(
1389 &inter0_v,
1390 &inter1_v,
1391 mask_y,
1392 side,
1393 chroma_stride,
1394 chroma_buf_h,
1395 ss_x(fctx),
1396 ss_y(fctx),
1397 &mut pred_v,
1398 fctx,
1399 );
1400 } else {
1401 mc::combine_compound(
1402 &inter0_v,
1403 &inter1_v,
1404 fwd_offset,
1405 bck_offset,
1406 &mut pred_v,
1407 fctx,
1408 );
1409 }
1410
1411 // lane-inter4 r2: a rectangular strip's transform tree starts from
1412 // its own rect `max_txsize_rect_lookup` entry, not a square one.
1413 *out = [pred_y, pred_u, pred_v];
1414 };
1415 let (pred_y, pred_u, pred_v): (Vec<u16>, Vec<u16>, Vec<u16>);
1416 let (sy, su, sv) = if pred_deferred(refpix) {
1417 push_pred(build);
1418 (Pred::Cur { plane: 0, stride: side, off: 0 }, Pred::Cur { plane: 1, stride: chroma_stride, off: 0 }, Pred::Cur { plane: 2, stride: chroma_stride, off: 0 })
1419 } else {
1420 let mut built: [Vec<u16>; 3] = Default::default();
1421 build(y, u, v, refpix, fctx, &mut built);
1422 let [by, bu, bv] = built;
1423 (pred_y, pred_u, pred_v) = (by, bu, bv);
1424 (
1425 Pred::Inline(&pred_y, side),
1426 Pred::Inline(&pred_u, chroma_stride),
1427 Pred::Inline(&pred_v, chroma_stride),
1428 )
1429 };
1430
1431 let rect_tu = (write_w, write_h) != (side, side);
1432 vartx_leaves = if rect_tu {
1433 read_block_tx_size_rect(
1434 dec,
1435 cdfs,
1436 neighbours,
1437 at_mi,
1438 (write_w, write_h),
1439 (mi_cols as usize, mi_rows as usize),
1440 skip,
1441 fctx,
1442 )?
1443 } else {
1444 read_block_tx_size(
1445 dec,
1446 cdfs,
1447 neighbours,
1448 at_mi,
1449 side,
1450 (mi_cols as usize, mi_rows as usize),
1451 true,
1452 skip,
1453 fctx,
1454 )?
1455 .1
1456 };
1457 if skip {
1458 push_mc_rect(
1459 y,
1460 u,
1461 v,
1462 0,
1463 px,
1464 py,
1465 side,
1466 write_w,
1467 write_h,
1468 sy,
1469 &ZERO_RESIDUAL[..side * side],
1470 fctx,
1471 );
1472 push_mc_rect(
1473 y,
1474 u,
1475 v,
1476 1,
1477 cpx,
1478 cpy,
1479 chroma_stride,
1480 write_chroma_w,
1481 write_chroma_h,
1482 su,
1483 &ZERO_RESIDUAL[..chroma_stride * chroma_buf_h],
1484 fctx,
1485 );
1486 push_mc_rect(
1487 y,
1488 u,
1489 v,
1490 2,
1491 cpx,
1492 cpy,
1493 chroma_stride,
1494 write_chroma_w,
1495 write_chroma_h,
1496 sv,
1497 &ZERO_RESIDUAL[..chroma_stride * chroma_buf_h],
1498 fctx,
1499 );
1500 luma_grid = Grid::Zero(side * side);
1501 u_grid = Grid::Zero(chroma_stride * chroma_buf_h);
1502 v_grid = Grid::Zero(chroma_stride * chroma_buf_h);
1503 } else {
1504 // lane-inter4 r2: a rect strip's coefficient context spans its
1505 // OWN width above and height left -- reading `side` cells both
1506 // ways indexes past the tile's last mi row for a bottom strip.
1507 let around = if rect_tu {
1508 // lane-av1422mi24: at ss (1,0) the rect strip's chroma
1509 // above span is `write_w >> 1` chroma cells spread over
1510 // `write_w` luma mi columns -- the per-mi gather counts
1511 // every chroma column's whole-unit dc sign twice (the
1512 // t422 frame-1 mi(26,60) strip's U dc_sign read one row
1513 // high). Luma keeps the per-mi extents; chroma takes
1514 // [`Neighbours::around_mi_422_chroma`]'s every-second-
1515 // above-cell sampling.
1516 let mut a = neighbours.around_mi_rect(at_mi, write_w, write_h);
1517 if ss_x(fctx) == 1 && ss_y(fctx) == 0 {
1518 let c = neighbours.around_mi_422_chroma(at_mi, write_w, write_h);
1519 a[1] = c[1];
1520 a[2] = c[2];
1521 }
1522 a
1523 } else if ss_x(fctx) == 1 && ss_y(fctx) == 0 {
1524 // lane-av1422mi24: at ss (1,0) a square block's chroma
1525 // plane block is (side/2, side) -- `side` chroma 4-px
1526 // above cells spread over `side` luma mi columns, one
1527 // chroma column per PAIR of luma mi cells. The plain
1528 // per-mi gather samples every second chroma column, and
1529 // its dc-vote sum lands one sign row off (t422 frame 1,
1530 // mi(24,56) U dc_sign: ours read dc_sign_cdf[0] where
1531 // the oracle read row 1). Luma keeps the per-mi gather
1532 // verbatim; only the two chroma planes take
1533 // [`Neighbours::around_mi_422_chroma`]'s every-second-
1534 // above-cell sampling (the same measured rule the strip
1535 // and chunk arms already run).
1536 let mut a = neighbours.around_mi(at, side);
1537 let c = neighbours.around_mi_422_chroma(at, write_w, write_h);
1538 a[1] = c[1];
1539 a[2] = c[2];
1540 a
1541 } else {
1542 neighbours.around_mi(at, side)
1543 };
1544 let luma_tx_type;
1545 // lane-sb128c r1: a 128x128 inter block's chroma is read
1546 // INSIDE the luma leaf loop (each 64x64 mu chunk's own units:
1547 // one per chunk at 4:2:0, FOUR TX_32X32 at ss 0/0); these
1548 // collect them for the block-level neighbour write-back.
1549 let mut u_units: Vec<i32> = Vec::new();
1550 let mut v_units: Vec<i32> = Vec::new();
1551 // lane-av1-444-128: every luma leaf's coded tx_type, so the
1552 // chunk tail's chroma units can each inherit the type of the
1553 // leaf covering their own quadrant (`av1_get_tx_type`,
1554 // blockd.h:1291, scales the CHROMA 4x4 position back to luma).
1555 let mut leaf_tx_types: Vec<(usize, usize, usize, usize, TxType)> = Vec::new();
1556 if let Some(leaves) = vartx_leaves.as_ref() {
1557 // spec 5.11.17's transform tree, leaf by leaf in the order
1558 // `read_var_tx_size` coded it: each unit reads its own
1559 // coefficients against the neighbour magnitudes the
1560 // earlier units already wrote, and reconstructs over its
1561 // own slice of the block's motion-compensated predictor.
1562 let reduced = fctx.reduced_tx_set_inter.with(std::cell::Cell::get);
1563 let mut first_tx_type = TxType::DctDct;
1564 // lane-av1h5: libaom's `decode_token_recon_block` walks
1565 // 64x64 "mu" chunks with the PLANE loop inside, so at 128
1566 // the token order is CHUNK-major; the transform tree hands
1567 // us a BLOCK-wide raster whose chunk members are not
1568 // contiguous. Below 128 a block IS one chunk.
1569 let (order, _mu_reordered) = mu_chunk_order(leaves, side);
1570 for (pos, &idx) in order.iter().enumerate() {
1571 let (row, col, tw, th) = leaves[idx];
1572 let tu_mi = (at_mi.0 + row, at_mi.1 + col);
1573 let (tu_px, tu_py) = (px + col * MI, py + row * MI);
1574 let (tu_grid, tu_tx_type) = if tw == th {
1575 let tx_px = tw;
1576 let tu_pred = sy.offset(row * MI * side + col * MI);
1577 let scan = default_scan(tx_px.min(32));
1578 let tu_skip_ctx = neighbours.luma_skip_ctx(tu_mi, tx_px / MI);
1579 read_inter_plane(
1580 dec,
1581 cdfs,
1582 inter_txbset_for(tx_px, reduced),
1583 scan,
1584 0,
1585 neighbours.around_mi(tu_mi, tx_px)[0],
1586 mode_for_tx,
1587 y,
1588 u,
1589 v,
1590 tu_px,
1591 tu_py,
1592 tx_px,
1593 base_q_idx,
1594 tu_pred,
1595 None,
1596 Some(tu_skip_ctx),
1597 fctx,
1598 )?
1599 } else {
1600 // lane-r14 r3: a 1:4 strip's `sub_tx_size_map`
1601 // sub-transform is RECTANGULAR (TX_64X16 ->
1602 // TX_32X16, TX_16X64 -> TX_16X32), so this leaf is
1603 // read with the rect coefficient machinery, at its
1604 // own `txb_skip_ctx`/neighbour spans -- the unit is
1605 // smaller than its block, so `get_txb_ctx_general`'s
1606 // neighbour table applies, not the lone-TU 0.
1607 vartx_rect_leaf_hit(tw, th);
1608 let tu_skip_ctx =
1609 neighbours.luma_skip_ctx_rect(tu_mi, tw / MI, th / MI);
1610 let start = row * MI * side + col * MI;
1611 read_inter_plane_rect(
1612 dec,
1613 cdfs,
1614 rect_inter_luma_set(tw, th, fctx)?,
1615 (tw, th),
1616 side,
1617 0,
1618 neighbours.around_mi_rect(tu_mi, tw, th)[0],
1619 mode_for_tx,
1620 y,
1621 u,
1622 v,
1623 tu_px,
1624 tu_py,
1625 sy.offset(start),
1626 None,
1627 Some(tu_skip_ctx),
1628 fctx,
1629 )?
1630 };
1631 if idx == 0 {
1632 first_tx_type = tu_tx_type;
1633 }
1634 // lane-av1444chr: EVERY luma leaf's coded type, at
1635 // every block size (see the single-reference twin
1636 // below) -- the chroma units resolve their own
1637 // quadrant's leaf, and a leaf that coded NOTHING is
1638 // `DctDct` exactly as libaom's `decodetxb.c:199-203`
1639 // stamps `DCT_DCT` into that leaf's map cell.
1640 leaf_tx_types.push((row, col, tw, th, tu_tx_type));
1641 neighbours.record_mi_luma_rect(tu_mi, tw, th, &tu_grid);
1642 // lane-sb128c r1: libaom's `decode_token_recon_block`
1643 // walks 64x64 "mu" chunks (`mu_blocks_wide/high =
1644 // mi_size_wide[BLOCK_64X64]`) and codes EVERY PLANE of
1645 // a chunk before the next one, so a 128x128 block
1646 // interleaves Y,U,V per quadrant -- the same order the
1647 // intra 128 path reads (lane-sb128b r3, measured
1648 // against aomdec's `EC_TRACE_COEFF`). Below 128 a block
1649 // is a single chunk and this never fires.
1650 if side > 64 {
1651 let chunk = (row / 16, col / 16);
1652 let done = pos + 1 == order.len()
1653 || (leaves[order[pos + 1]].0 / 16, leaves[order[pos + 1]].1 / 16)
1654 != chunk;
1655 if done {
1656 let (cr, cc) = chunk;
1657 // `av1_get_max_uv_txsize(BLOCK_128X128)` =
1658 // `av1_get_adjusted_tx_size(TX_64X64)` =
1659 // TX_32X32. The mu walk
1660 // (`decode_token_recon_block`, decodeframe.c)
1661 // then enumerates, per plane, EVERY TX_32X32
1662 // unit of the chunk's chroma in raster order:
1663 // at ss 0/0 the chunk's chroma is 64x64 and
1664 // that is FOUR units; at 4:2:0 it is 32x32
1665 // and the loop degenerates to the one unit
1666 // this arm has always coded. `chroma_side`
1667 // already carries the subsampling.
1668 let cu_tx = 32usize;
1669 // lane-av1-422bigblock: a 64x64 mu chunk's
1670 // chroma plane block is PER-AXIS
1671 // (64 >> ss_x) x (64 >> ss_y) -- 32x64 at
1672 // ss (1,0), i.e. TWO stacked TX_32X32 units
1673 // per plane (`av1_get_max_uv_txsize` caps the
1674 // unit at 32 on each axis). The old square-cut
1675 // model walked `chroma_side`-square chunks:
1676 // twice the units the oracle codes at 4:2:2,
1677 // prediction windows indexed at the square
1678 // `chroma_side` instead of the prediction
1679 // buffer's own `chroma_stride`, and chunk
1680 // columns at 64.. of a 64-wide chroma plane.
1681 let chunk_chroma_w = (64usize) >> ss_x(fctx);
1682 let chunk_chroma_h = (64usize) >> ss_y(fctx);
1683 let units_w = chunk_chroma_w / cu_tx;
1684 let units_h = chunk_chroma_h / cu_tx;
1685 // The unit's own footprint on the LUMA mi
1686 // grid, per axis (32 << ss) px: 64x64 (16x16
1687 // mi) at 4:2:0, 32x32 (8x8 mi) at 4:4:4,
1688 // 64x32 (16x8 mi) at 4:2:2. The context
1689 // gather and `record_mi_chroma`'s stamp span
1690 // exactly the unit's own cells -- `get_txb_ctx`
1691 // reads the 8 context cells of the TX_32X32
1692 // itself, and `av1_set_entropy_contexts` memsets
1693 // the same 8, so the unit over never sees this
1694 // unit's state and the unit below always does.
1695 // At ss (1, 0) those 8 are CHROMA cells, so the
1696 // gather routes through the 4:2:2 pair rule
1697 // (lane-av1muchunk422sym, see the walk below).
1698 let unit_luma_w = cu_tx << ss_x(fctx);
1699 let unit_luma_h = cu_tx << ss_y(fctx);
1700 let cu_scan = default_scan(cu_tx);
1701 if lossless(fctx) {
1702 // lane-lossless2: TX_4X4 units over this
1703 // chunk's chroma, not one TX_32X32 unit.
1704 read_inter_chroma_lossless(
1705 y,
1706 u,
1707 v,
1708 dec,
1709 cdfs,
1710 neighbours,
1711 su,
1712 sv,
1713 at_mi,
1714 (cpx, cpy),
1715 (cc * chunk_chroma_w, cr * chunk_chroma_h),
1716 (chunk_chroma_w, chunk_chroma_h),
1717 (write_chroma_w, write_chroma_h),
1718 // lane-av1422lpf site 5: the stride
1719 // the walk indexes `su`/`sv` at is the
1720 // PREDICTION BUFFER's own
1721 // `chroma_stride` -- the same number
1722 // `su`/`sv` were built at, and the one
1723 // the composed `u_units`/`v_units`
1724 // grid is sized and written at (site
1725 // 1's `stride * blk_h`). `chroma_side`
1726 // is the ENCLOSING SQUARE
1727 // (`max(side >> ss_x, side >> ss_y)`)
1728 // and at 4:2:2 it is the LUMA side: a
1729 // 128x128 block's chroma plane block is
1730 // 64x128, so the walk addressed
1731 // `oy * 128 + ox` on a 64-wide, 8192-
1732 // sample buffer and the last unit row
1733 // ran off the end
1734 // (`offset 8192 .. 8580 of 8192`,
1735 // X_intrabc_tiled frame 1, at
1736 // `push_mc_rect_tx`).
1737 //
1738 // At 4:2:0 `chroma_stride ==
1739 // chroma_side == side >> 1` and at
1740 // 4:4:4 both are `side`, so this
1741 // argument is the same number on every
1742 // shape this decoder admits except
1743 // 4:2:2.
1744 chroma_stride,
1745 mode_for_tx,
1746 base_q_idx,
1747 &mut u_units,
1748 &mut v_units,
1749 fctx,
1750 )?;
1751 mu_chroma = true;
1752 continue;
1753 }
1754 for plane_idx in 1..3 {
1755 let src = if plane_idx == 1 { su } else { sv };
1756 // Plane-major per chunk (libaom's plane
1757 // loop is outermost inside the mu walk);
1758 // within a plane the units walk raster.
1759 for ur in 0..units_h {
1760 for uc in 0..units_w {
1761 let unit_mi = (
1762 at_mi.0 + cr * 16 + ur * (unit_luma_h / MI),
1763 at_mi.1 + cc * 16 + uc * (unit_luma_w / MI),
1764 );
1765 // Per unit, AFTER the earlier
1766 // units' stamps: this unit's
1767 // coefficient context must see
1768 // them.
1769 //
1770 // lane-av1muchunk422sym: at ss
1771 // (1, 0) one CHROMA column spans
1772 // two luma mi columns, so the plain
1773 // rect gather sums this unit's
1774 // above dc sign TWICE where
1775 // libaom's `get_txb_ctx_general`
1776 // reads `txb_w_unit` CHROMA cells --
1777 // one per column. `dc_sign_ctx`
1778 // only sees the vote's signum, so
1779 // the doubled vote flips the row
1780 // exactly when above and left
1781 // cancel; `around_mi_422_chroma`
1782 // samples every second above cell,
1783 // which IS libaom's sum. Same guard
1784 // as the intra twin (13169) and the
1785 // block-8 arm (19810).
1786 let cu_around = if ss_x(fctx) == 1 && ss_y(fctx) == 0 {
1787 neighbours.around_mi_422_chroma(
1788 unit_mi,
1789 unit_luma_w,
1790 unit_luma_h,
1791 )
1792 } else {
1793 neighbours.around_mi_rect(
1794 unit_mi,
1795 unit_luma_w,
1796 unit_luma_h,
1797 )
1798 };
1799 let (cu_x, cu_y) = (
1800 cpx + cc * chunk_chroma_w + uc * cu_tx,
1801 cpy + cr * chunk_chroma_h + ur * cu_tx,
1802 );
1803 // `av1_get_tx_type` (blockd.h:
1804 // 1291): an inter block's chroma
1805 // unit never codes a tx_type; it
1806 // inherits the coded type of the
1807 // luma leaf covering its own
1808 // quadrant (all of this chunk's
1809 // leaves are read by now).
1810 let unit_rel_mi =
1811 (unit_mi.0 - at_mi.0, unit_mi.1 - at_mi.1);
1812 let covering =
1813 covering_leaf_tx_type(&leaf_tx_types, unit_rel_mi);
1814 // The prediction buffer is
1815 // `chroma_stride`-strided (the
1816 // plane block's own width at
1817 // 4:2:2), not the square
1818 // `chroma_side`.
1819 let cu_pred = src.offset(
1820 (cr * chunk_chroma_h + ur * cu_tx) * chroma_stride
1821 + cc * chunk_chroma_w
1822 + uc * cu_tx,
1823 );
1824 let cu_grid = read_inter_plane(
1825 dec,
1826 cdfs,
1827 chroma_set,
1828 cu_scan,
1829 plane_idx,
1830 cu_around[plane_idx],
1831 mode_for_tx,
1832 y,
1833 u,
1834 v,
1835 cu_x,
1836 cu_y,
1837 cu_tx,
1838 base_q_idx,
1839 cu_pred,
1840 Some(covering.unwrap_or(TxType::DctDct)),
1841 Some(3),
1842 fctx,
1843 )?
1844 .0;
1845 // Immediately, so the NEXT unit
1846 // reads this one's coefficient
1847 // context.
1848 neighbours.record_mi_chroma(
1849 unit_mi,
1850 unit_luma_w,
1851 unit_luma_h,
1852 plane_idx,
1853 &cu_grid,
1854 );
1855 hit!(CHROMA_SPLIT_TX_HITS);
1856 mu_chroma = true;
1857 // lane-av1muchunk422b: the allocation of this
1858 // mu-chunk walk, counted per TX_32X32 UNIT.
1859 // The compound and single-reference arms share
1860 // this source text, so `is_compound` routes the
1861 // bump: `MU_CHUNK_COMPOUND_UNITS` names site 2
1862 // and the single-reference twin (site 5) stays
1863 // out of it.
1864 if is_compound {
1865 hit!(MU_CHUNK_COMPOUND_UNITS);
1866 }
1867 let dst = mu_units(
1868 if plane_idx == 1 {
1869 &mut u_units
1870 } else {
1871 &mut v_units
1872 },
1873 chroma_side * chroma_side,
1874 );
1875 for rr in 0..cu_tx {
1876 // The ASSEMBLED grid stays at
1877 // the square `chroma_side`
1878 // stride the block tail's
1879 // `mu_chroma_units` re-stamp
1880 // indexes; the unit's true
1881 // per-axis position is what
1882 // changes.
1883 let start = (cr * chunk_chroma_h + ur * cu_tx + rr)
1884 * chroma_side
1885 + cc * chunk_chroma_w
1886 + uc * cu_tx;
1887 dst[start..start + cu_tx].copy_from_slice(
1888 &cu_grid[rr * cu_tx..][..cu_tx],
1889 );
1890 }
1891 }
1892 }
1893 }
1894 }
1895 }
1896 }
1897 // The block-level `luma_tx_type` stays the first
1898 // (top-left) luma unit's coded type; the PER-UNIT chroma
1899 // inheritance in the chunk tail above resolves each unit's
1900 // own covering leaf instead (`av1_get_tx_type`,
1901 // blockd.h:1291).
1902 luma_tx_type = first_tx_type;
1903 luma_grid = Grid::Zero(side * side);
1904 } else if rect_tu {
1905 // The whole-block rectangular transform unit
1906 // (`txfm_partition` said 0 at depth 0).
1907 let (grid, tx_type) = read_inter_plane_rect(
1908 dec,
1909 cdfs,
1910 rect_inter_luma_set(write_w, write_h, fctx)?,
1911 (write_w, write_h),
1912 side,
1913 0,
1914 around[0],
1915 mode_for_tx,
1916 y,
1917 u,
1918 v,
1919 px,
1920 py,
1921 sy,
1922 None,
1923 None,
1924 fctx,
1925 )?;
1926 luma_tx_type = tx_type;
1927 luma_grid = Grid::Own(embed_rect_grid(&grid, write_w, write_h, side));
1928 } else {
1929 {
1930 let coded = read_inter_plane(
1931 dec,
1932 cdfs,
1933 luma_set_inter,
1934 scan_luma,
1935 0,
1936 around[0],
1937 mode_for_tx,
1938 y,
1939 u,
1940 v,
1941 px,
1942 py,
1943 side,
1944 base_q_idx,
1945 sy,
1946 None,
1947 None,
1948 fctx,
1949 )?;
1950 luma_grid = coded.0;
1951 luma_tx_type = coded.1;
1952 }
1953 }
1954 if side > 64 {
1955 // Already read inside the mu-chunk loop above -- at the 128
1956 // root a rect (128x64/64x128) block's chroma is per mu
1957 // chunk too, so this test comes BEFORE the `rect_tu` one.
1958 u_grid = if u_units.is_empty() {
1959 Grid::Zero(chroma_stride * chroma_buf_h)
1960 } else {
1961 Grid::Own(std::mem::take(&mut u_units))
1962 };
1963 v_grid = if v_units.is_empty() {
1964 Grid::Zero(chroma_stride * chroma_buf_h)
1965 } else {
1966 Grid::Own(std::mem::take(&mut v_units))
1967 };
1968 } else if lossless(fctx) {
1969 // lane-lossless2: TX_4X4 chroma units, plane-major
1970 // (libaom `read_tx_size` + `decode_token_recon_block`).
1971 let (mut uo, mut vo) = (Vec::new(), Vec::new());
1972 read_inter_chroma_lossless(
1973 y,
1974 u,
1975 v,
1976 dec,
1977 cdfs,
1978 neighbours,
1979 su,
1980 sv,
1981 at_mi,
1982 (cpx, cpy),
1983 (0, 0),
1984 (write_chroma_w, write_chroma_h),
1985 (write_chroma_w, write_chroma_h),
1986 chroma_stride,
1987 mode_for_tx,
1988 base_q_idx,
1989 &mut uo,
1990 &mut vo,
1991 fctx,
1992 )?;
1993 let zero = chroma_side * chroma_side;
1994 u_grid = if uo.is_empty() {
1995 Grid::Zero(zero)
1996 } else {
1997 Grid::Own(uo)
1998 };
1999 v_grid = if vo.is_empty() {
2000 Grid::Zero(zero)
2001 } else {
2002 Grid::Own(vo)
2003 };
2004 // Every unit stamped its own neighbour context above; the
2005 // whole-block record must not overwrite them with the
2006 // assembled grid (class `override-slot-on-one-arm`).
2007 mu_chroma = true;
2008 } else if rect_tu || (write_chroma_w, write_chroma_h) != (chroma_side, chroma_side)
2009 {
2010 // One chroma read for the whole strip at 4:2:0, two
2011 // 32-capped units at 4:4:4 -- see the helper.
2012 // lane-av1-422j: a 4:2:2 square block's chroma plane is the
2013 // (side/2 x side) rect (a 32x32 block's 16x32), never
2014 // `chroma_side`-square -- the square read below would pair
2015 // a `chroma_side`-square unit with the 4:2:0 unit scan
2016 // (one-coefficient-per-position assert in
2017 // inverse_transform_2d_typed_wh). The rect reader codes
2018 // the exact `av1_get_max_uv_txsize` unit (TX_16X32,
2019 // `rect_inter_chroma_set`'s tables).
2020 (u_grid, v_grid, rect_chroma_units) = read_inter_rect_chroma(
2021 y,
2022 u,
2023 v,
2024 dec,
2025 cdfs,
2026 neighbours,
2027 around,
2028 su,
2029 sv,
2030 at_mi,
2031 (cpx, cpy),
2032 (write_chroma_w, write_chroma_h),
2033 chroma_stride,
2034 mode_for_tx,
2035 &leaf_tx_types,
2036 luma_tx_type,
2037 fctx,
2038 )?;
2039 } else if ss_x(fctx) == 0 && ss_y(fctx) == 0 && chroma_side > chroma_tx {
2040 // lane-av1-444: at ss 0/0 this block's chroma plane is
2041 // `chroma_side` square while `av1_get_max_uv_txsize` caps
2042 // the units at TX_32X32 -- each plane codes FOUR TX_32X32
2043 // units, plane-major inside the block's one 64x64 mu chunk
2044 // (libaom `decode_token_recon_block`'s plane loop). Each
2045 // unit's `txb_skip_ctx` carries the +10
2046 // `get_txb_ctx_general` offset (plane block larger than the
2047 // transform); our `txb_skip_chroma_32` holds those rows at
2048 // +3, so `luma_skip_ctx: Some(3)` (lane-sb128b r3's
2049 // convention, as the 128 mu path above).
2050 let cu_tx = chroma_tx;
2051 let mu_units_n = chroma_side / cu_tx;
2052 let mut u_acc: Vec<i32> = Vec::new();
2053 let mut v_acc: Vec<i32> = Vec::new();
2054 for plane_idx in 1..3 {
2055 let (src, acc) = if plane_idx == 1 {
2056 (su, &mut u_acc)
2057 } else {
2058 (sv, &mut v_acc)
2059 };
2060 for cr in 0..mu_units_n {
2061 for cc in 0..mu_units_n {
2062 let cu_mi = (rmi + cr * (cu_tx / MI), cmi + cc * (cu_tx / MI));
2063 let cu_around = neighbours.around_mi(cu_mi, cu_tx);
2064 // Per-quadrant inheritance, as in the
2065 // single-reference twin: the covering luma
2066 // leaf's OWN coded type, `DctDct` where that
2067 // leaf coded nothing.
2068 let cu_rel_mi = (cr * (cu_tx / MI), cc * (cu_tx / MI));
2069 let cu_tx_type = covering_leaf_tx_type(&leaf_tx_types, cu_rel_mi)
2070 .unwrap_or(luma_tx_type);
2071 if covering_leaf_tx_type(&leaf_tx_types, cu_rel_mi).is_some() {
2072 hit!(CHROMA_QUAD_LEAF_TX_HITS);
2073 if cu_tx_type != luma_tx_type {
2074 hit!(CHROMA_QUAD_LEAF_TX_DIFF_HITS);
2075 }
2076 }
2077 let cu_grid = read_inter_plane(
2078 dec,
2079 cdfs,
2080 chroma_set,
2081 scan_chroma,
2082 plane_idx,
2083 cu_around[plane_idx],
2084 mode_for_tx,
2085 y,
2086 u,
2087 v,
2088 cpx + cc * cu_tx,
2089 cpy + cr * cu_tx,
2090 cu_tx,
2091 base_q_idx,
2092 src.offset(cr * cu_tx * chroma_side + cc * cu_tx),
2093 Some(cu_tx_type),
2094 Some(3),
2095 fctx,
2096 )?
2097 .0;
2098 // Immediately, so the next unit's context sees
2099 // this one's coefficients.
2100 neighbours
2101 .record_mi_chroma(cu_mi, cu_tx, cu_tx, plane_idx, &cu_grid);
2102 hit!(CHROMA_SPLIT_TX_HITS);
2103 // lane-av1-444 c8: the unit's levels must ALSO
2104 // land in the block grid -- the block-tail mu
2105 // re-stamp slices the per-unit states back out
2106 // of it. This arm used to hand it a ZERO grid,
2107 // so the re-stamp (and `record_rect_mi`'s
2108 // whole-block write under it) painted the whole
2109 // block's chroma context cells 0 AFTER the
2110 // correct per-unit stamps above: the next block
2111 // read `txb_skip_ctx` base 0 where aom gathers
2112 // 1 (f52 mi(52,16) U all_zero, ctx 0 vs 8).
2113 let dst = mu_units(acc, chroma_side * chroma_side);
2114 for rr in 0..cu_tx {
2115 let start = (cr * cu_tx + rr) * chroma_side + cc * cu_tx;
2116 dst[start..start + cu_tx]
2117 .copy_from_slice(&cu_grid[rr * cu_tx..][..cu_tx]);
2118 }
2119 }
2120 }
2121 }
2122 mu_chroma = true;
2123 u_grid = Grid::Own(u_acc);
2124 v_grid = Grid::Own(v_acc);
2125 } else {
2126 u_grid = read_inter_plane(
2127 dec,
2128 cdfs,
2129 chroma_set,
2130 scan_chroma,
2131 1,
2132 around[1],
2133 mode_for_tx,
2134 y,
2135 u,
2136 v,
2137 cpx,
2138 cpy,
2139 chroma_side,
2140 base_q_idx,
2141 su,
2142 Some(luma_tx_type),
2143 None,
2144 fctx,
2145 )?
2146 .0;
2147 v_grid = read_inter_plane(
2148 dec,
2149 cdfs,
2150 chroma_set,
2151 scan_chroma,
2152 2,
2153 around[2],
2154 mode_for_tx,
2155 y,
2156 u,
2157 v,
2158 cpx,
2159 cpy,
2160 chroma_side,
2161 base_q_idx,
2162 sv,
2163 Some(luma_tx_type),
2164 None,
2165 fctx,
2166 )?
2167 .0;
2168 }
2169 }
2170 } else {
2171 let ref_frame = read_single_ref(
2172 dec,
2173 cdfs,
2174 neighbours.above_ref[cmi],
2175 neighbours.above_ref1[cmi],
2176 neighbours.left_ref[rmi],
2177 neighbours.left_ref1[rmi],
2178 );
2179 ref_frame_for_lf = ref_frame;
2180 if crate::envflags::env_flag!("EC_AV1_TELL") {
2181 let (mi_row, mi_col) = (rmi, cmi);
2182 eprintln!(
2183 "TELL mi_row={mi_row} mi_col={mi_col} label=post_ref_frame ref={ref_frame} tell={} range={}",
2184 dec.debug_bitpos(),
2185 dec.debug_state().0
2186 );
2187 }
2188 // round 4-9 (lane-av1golden..lane-av1golden7): GOLDEN_FRAME's own
2189 // stacked defects (lf_level ref/mode-delta forwarding, the
2190 // switchable_interp ref-frame ctx gate, and a film-grain synthesis
2191 // gap that looked like a GOLDEN MC bug by survivor bias -- see the
2192 // ledger) are all closed; GOLDEN_FRAME decodes live via `other_refs`
2193 // below same as every other non-`LAST_FRAME` reference. lane-av1refs
2194 // widens this from GOLDEN alone to every reference `read_single_ref`
2195 // can name.
2196 // lane-defer8: SIZE here (spec 7.11.3.3's scale factor), pixels
2197 // inside the prediction closure -- the parse thread of a threaded
2198 // decode must never wait on a reference picture.
2199 let (ref_width, ref_height) = ref_dims(ref_frame, refpix)?;
2200
2201 let (mi_row, mi_col) = (rmi, cmi);
2202 // lane-rect r2: the mv stack must be queried with the block's
2203 // REAL extent (libaom `av1_get_mv_refs` gets bw4/bh4 from
2204 // xd->width/xd->height, not a square guess) -- `write_w`/
2205 // `write_h` already name the true HORZ/VERT/HORZ_B strip
2206 // footprint (r1's plumbing), so derive bw4/bh4 from those
2207 // instead of the old `side`-square (HORZ_B-only) heuristic.
2208 let bw4 = write_w / 4;
2209 let bh4 = write_h / 4;
2210 // spec 7.10.2.8: only reached when the frame header set
2211 // `use_ref_frame_mvs` (`tpl_frame` is `Some` then, `None`
2212 // otherwise) -- `cur_offset_0` is this query block's own resolved
2213 // `ref_frame`'s distance from the current frame (spec 7.9.3),
2214 // recomputed per block since different blocks pick different refs.
2215 let tpl = tpl_frame.map(|t| crate::mvstack::TplArgs {
2216 field: t.field,
2217 cur_offset_0: crate::motion_field::get_relative_dist(
2218 t.order_hint_bits,
2219 t.order_hint,
2220 t.ref_order_hints[(ref_frame - LAST_FRAME) as usize],
2221 ),
2222 allow_high_precision_mv,
2223 force_integer_mv,
2224 });
2225 let gm_table = build_gm_mv_table(
2226 global_motion,
2227 mi_row,
2228 mi_col,
2229 bw4,
2230 bh4,
2231 allow_high_precision_mv,
2232 force_integer_mv,
2233 );
2234 let stack = find_mv_stack_with_sign_bias(
2235 grid,
2236 mi_row,
2237 mi_col,
2238 bw4,
2239 bh4,
2240 ref_frame,
2241 mi_cols as usize,
2242 mi_rows as usize,
2243 sign_bias_table,
2244 &gm_table,
2245 tpl,
2246 );
2247 let not_new = dec.symbol(&mut cdfs.new_mv[stack.new_mv_ctx]) == 1;
2248 if !not_new {
2249 crate::census::mode(0);
2250 }
2251 let mut is_globalmv = false;
2252 // lane-interbis r1: which single-ref mode this block resolved to,
2253 // in the oracle's own numbering (NEARESTMV=13 .. NEWMV=16), for
2254 // the `EC_MODE_VAL` value ladder below.
2255 let mut dbg_mode = 16u32;
2256 let (mv, is_new_mv) = if !not_new {
2257 // NEWMV (spec 5.11.24's `read_drl_idx`, `RefMvIdx` starting at 0):
2258 // read at most two `drl_mode` bits, one per stack entry past the
2259 // first, stopping at the first `0`. The chosen index selects
2260 // which stack entry `read_mv`'s base predictor comes from
2261 // (spec 7.10.2.10 `assign_mv`'s `PredMv = RefStackMv[RefMvIdx]`)
2262 // -- entry 0 (`stack.pred_mv`) only when the loop never advances.
2263 let mut idx = 0usize;
2264 while idx < 2 && stack.entries.len() > idx + 1 {
2265 if dec.symbol(&mut cdfs.drl_mode[stack.drl_ctx[idx]]) == 0 {
2266 break;
2267 }
2268 idx += 1;
2269 }
2270 let base_mv = stack.entries.get(idx).map_or(stack.pred_mv, |e| e.mv);
2271 (
2272 read_mv(
2273 dec,
2274 &mut cdfs.mv_comp,
2275 &mut cdfs.mv_joint,
2276 base_mv,
2277 allow_high_precision_mv,
2278 force_integer_mv,
2279 fctx,
2280 ),
2281 true,
2282 )
2283 } else {
2284 let not_zero = dec.symbol(&mut cdfs.zero_mv[stack.zero_mv_ctx]) == 1;
2285 if !not_zero {
2286 crate::census::mode(1);
2287 }
2288 is_globalmv = !not_zero;
2289 let mv = if !not_zero {
2290 // GLOBALMV: `gm_get_motion_vector` (spec 7.10.2.1), already
2291 // computed at this block's own position in `gm_table`.
2292 dbg_mode = 15;
2293 gm_table[(ref_frame - LAST_FRAME) as usize]
2294 } else {
2295 let nearest = dec.symbol(&mut cdfs.ref_mv[stack.ref_mv_ctx]) == 0;
2296 crate::census::mode(if nearest { 2 } else { 3 });
2297 dbg_mode = if nearest { 13 } else { 14 };
2298 if nearest {
2299 stack.nearest_mv
2300 } else {
2301 // NEARMV (spec 5.11.24's `read_drl_idx`, `RefMvIdx` starting
2302 // at 1): read at most two more `drl_mode` bits, one per stack
2303 // entry past the second, stopping at the first `0`.
2304 // `drl_ctx[idx]` is the context between `entries[idx]` and
2305 // `entries[idx + 1]` (`MvStack::drl_ctx`'s own doc), which is
2306 // exactly the pair this index is choosing between.
2307 let mut idx = 1usize;
2308 while idx < 3 && stack.entries.len() > idx + 1 {
2309 if dec.symbol(&mut cdfs.drl_mode[stack.drl_ctx[idx]]) == 0 {
2310 break;
2311 }
2312 idx += 1;
2313 }
2314 stack.entries.get(idx).map_or(stack.near_mv, |e| e.mv)
2315 }
2316 };
2317 (mv, false)
2318 };
2319 if crate::msac::symtrace::dir().is_some() {
2320 crate::msac::symtrace::note(&format!(
2321 " MODE mi=({mi_row},{mi_col}) dbg={dbg_mode} mv={mv:?}"
2322 ));
2323 }
2324 if crate::envflags::env_flag!("EC_TRACE_MODE") {
2325 for (i, e) in stack.entries.iter().enumerate() {
2326 eprintln!(
2327 "EC_STACK mi_row={mi_row} mi_col={mi_col} ref={ref_frame} i={i} this=({},{}) comp=(0,0) w={}",
2328 e.mv.0, e.mv.1, e.weight
2329 );
2330 }
2331 eprintln!(
2332 "EC_MODE_VAL mi_row={mi_row} mi_col={mi_col} mode={dbg_mode} ref0={ref_frame} ref1=-1 mv0=({},{}) stack={} rng={}",
2333 mv.0,
2334 mv.1,
2335 stack.entries.len(),
2336 dec.debug_state().0
2337 );
2338 }
2339 // lane-gm r2: the two libaom predicates single-ref GLOBALMV
2340 // blocks need -- `is_global_mv_block` (blockd.h:421-429, mode
2341 // GLOBAL* AND model > TRANSLATION AND min(bw,bh) >= 8px, gating
2342 // `motion_mode_eligible`/`MiInfo::is_global_mv0` below) and
2343 // `is_nontrans_global_motion` (reconinter.h:420-425, model !=
2344 // TRANSLATION -- IDENTITY counts here -- gating the interp
2345 // filter suppress below). Do NOT unify these.
2346 let gm_model = global_motion[(ref_frame - LAST_FRAME) as usize].model;
2347 let min_bw_bh4 = bw4.min(bh4);
2348 let is_global_mv_block = is_globalmv && gm_model as u8 > 1 && min_bw_bh4 >= 2;
2349 let gm_nontrans =
2350 is_globalmv && gm_model != ec_av1_syntax::WarpModel::Translation && min_bw_bh4 >= 2;
2351 if crate::envflags::env_flag!("EC_AV1_TELL") {
2352 eprintln!(
2353 "TELL mi_row={mi_row} mi_col={mi_col} label=post_assign_mv tell={} range={}",
2354 dec.debug_bitpos(),
2355 dec.debug_state().0
2356 );
2357 }
2358 // lane-sb128 r4: `interintra` (spec 5.11.24; libaom
2359 // `decodemv.c` read_inter_block_mode_info ~1490-1510), placed
2360 // right after `assign_mv` and before `read_motion_mode` below --
2361 // libaom's own sequential order. `is_interintra_allowed_bsize`
2362 // (`BLOCK_8X8..=BLOCK_32X32`) is the only real gate on this
2363 // single-ref path: `is_interintra_allowed_ref` always holds here
2364 // (single ref, `ref_frame[1]` not yet `INTRA_FRAME`) and
2365 // `is_interintra_allowed_mode` always holds (every mode this
2366 // branch produces -- NEARESTMV/NEARMV/GLOBALMV/NEWMV -- is
2367 // `SINGLE_INTER_MODE_START..END` by construction).
2368 // `size_group_lookup`: BLOCK_16X16 -> 2, BLOCK_32X32 -> 3.
2369 let mut interintra_mode: Option<u8> = None;
2370 let mut wedge_mask: Option<(&'static [u8], usize)> = None;
2371 // lane-inter4 r1: `is_interintra_allowed_bsize` is
2372 // `BLOCK_8X8..=BLOCK_32X32` in libaom's enum ORDER, which excludes
2373 // every 1:4 shape (BLOCK_8X32 = 18, BLOCK_32X8 = 19) even though
2374 // both dimensions are in range -- reading this symbol on a 1:4
2375 // strip is a symbol the reference never wrote.
2376 let interintra_shape =
2377 write_w.max(write_h) <= 32 && write_w.max(write_h) / write_w.min(write_h) <= 2;
2378 if enable_interintra_compound
2379 && !skip_mode
2380 && (side == 16 || side == 32)
2381 && interintra_shape
2382 {
2383 // lane-inter4 r3: `size_group_lookup` reads the block's
2384 // TRUE footprint -- a 16x8 strip is group 1, a 32x16 strip
2385 // group 2, not their square `side`'s 2/3.
2386 let bsize_group = size_group_wh(write_w, write_h);
2387 crate::msac::SymbolDecoder::set_symr_cdf("interintra");
2388 let interintra = dec.symbol(&mut cdfs.interintra[bsize_group]) == 1;
2389 if interintra {
2390 // lane-interintra r1 (decodemv.c 1540-1555): interintra_mode,
2391 // then -- gated on block size ALONE, never the
2392 // `enable_interintra_wedge` seq bit -- the wedge flag
2393 // (`av1_is_wedge_used(bsize)` holds for 16x16/32x32).
2394 let ii = dec.symbol(&mut cdfs.interintra_mode[bsize_group]) as u8;
2395 // lane-inter16ab r3 (same class as the compound
2396 // `compound_type` row below): the row is the block's TRUE
2397 // `BLOCK_SIZES_ALL` index -- a 16x8 / 32x16 interintra
2398 // strip is BLOCK_16X8 / BLOCK_32X16, not its square
2399 // `side`'s BLOCK_16X16 / BLOCK_32X32.
2400 let wedge_bsize = bsize_all_index(write_w, write_h)
2401 .expect("interintra shape is a BLOCK_SIZES_ALL size");
2402 let wedge = dec.symbol(&mut cdfs.wedge_interintra[wedge_bsize]) == 1;
2403 if wedge {
2404 // lane-wii r2 (spec 5.11.25): `wedge_index` is an
2405 // ADAPTING CDF symbol over the same `wedge_bsize` row;
2406 // NO sign symbol follows -- libaom fixes
2407 // INTERINTRA_WEDGE_SIGN 0 (blockd.h).
2408 let wedge_index = dec.symbol(&mut cdfs.wedge_idx[wedge_bsize]);
2409 hit!(WII_HITS);
2410 if let Some(k) = rect_wedge_slot(write_w, write_h) {
2411 hit_do! {
2412 RECT_WII_HITS.with(|c| {
2413 let mut a = c.get();
2414 a[k] += 1;
2415 c.set(a);
2416 });
2417 }
2418 }
2419 // lane-inter16ab r5: true-footprint codebook row,
2420 // mask padded to stride `max(bw,bh) == side` -- see
2421 // the compound site's own comment.
2422 wedge_mask = Some((
2423 crate::wedge::wedge_masks()
2424 .codebook(write_w, write_h)
2425 .mask(0, wedge_index),
2426 side,
2427 ));
2428 } else {
2429 hit!(INTERINTRA_HITS);
2430 }
2431 interintra_mode = Some(ii);
2432 // The inter-intra predictor reads this block's neighbours as already
2433 // reconstructed: a deferred wavefront must finish first.
2434 super::wave::finish_tile(fctx, refpix, [&mut *y, &mut *u, &mut *v]);
2435 }
2436 }
2437 if crate::envflags::env_flag!("EC_AV1_TELL") {
2438 eprintln!(
2439 "TELL mi_row={mi_row} mi_col={mi_col} label=post_interintra tell={} range={}",
2440 dec.debug_bitpos(),
2441 dec.debug_state().0
2442 );
2443 }
2444 // lane-motionmode round 1: `read_motion_mode` (spec 5.11.24;
2445 // libaom `decodemv.c` read_inter_block_mode_info, ~1520-1528),
2446 // placed right after MV assignment and before the switchable
2447 // interp filter read below (`read_mb_interp_filter`, decodemv.c
2448 // ~1600) -- libaom's own sequential order. `motion_mode_allowed`
2449 // (`reconinter.h`) reduces, for every block this single-ref path
2450 // reaches, to "does an inter neighbour border the above row or
2451 // left column": `is_motion_variation_allowed_bsize` always holds
2452 // (`side` is never below 16px on this path), `ref_frame[1] !=
2453 // INTRA_FRAME` always holds (no interintra reader here),
2454 // `is_motion_variation_allowed_compound` always holds (this is
2455 // the single-ref branch), and `is_global_mv_block` never
2456 // triggers (this decoder's global motion is always `IDENTITY`,
2457 // so its warp `type` is never `> TRANSLATION`). A stream with
2458 // `allow_warped_motion=1` is refused right at an eligible block
2459 // rather than guessed at: `av1_findSamples`/`num_proj_ref` is
2460 // not ported, so this decoder cannot tell a real
2461 // `WARPED_CAUSAL`-eligible block (3-symbol `motion_mode_cdf`)
2462 // from an `OBMC_CAUSAL`-only one (2-symbol `obmc_cdf`) whenever
2463 // the header allows warp -- reading the wrong alphabet desyncs
2464 // the tile.
2465 // An interintra block has `ref_frame[1] == INTRA_FRAME`, which
2466 // fails libaom's `is_motion_variation_allowed_compound`: the
2467 // motion_mode symbol is NOT read (SIMPLE_TRANSLATION implied).
2468 let motion_mode_eligible = switchable_motion_mode
2469 && !skip_mode
2470 // `is_motion_variation_allowed_bsize` (blockd.h:1455):
2471 // min(bw, bh) >= 8, false for a 16x4 / 4x16 strip.
2472 && write_w.min(write_h) >= 8
2473 && interintra_mode.is_none()
2474 && has_overlappable_neighbour(
2475 grid, mi_row, mi_col, bw4, bh4, mi_cols as usize, mi_rows as usize,
2476 )
2477 // libaom `motion_mode_allowed`: a GLOBALMV block whose model
2478 // is non-IDENTITY-non-TRANSLATION under free (non-integer)
2479 // mv reads no motion_mode/obmc symbol at all -- implicit
2480 // SIMPLE_TRANSLATION.
2481 && !(is_global_mv_block && !force_integer_mv);
2482 // lane-t900 r9: the eligibility terms themselves, on the same rung
2483 // as aomdec's `EC_ISTEP2 name=motion_mode` -- an eligibility
2484 // mismatch is otherwise invisible (a skipped symbol prints
2485 // nothing).
2486 if crate::envflags::env_flag!("EC_TRACE_MODE_STEP") {
2487 eprintln!(
2488 "EC_MM mi_row={mi_row} mi_col={mi_col} w={write_w} h={write_h} elig={} \
2489 ab={} lf={} sw={} skipmode={} ii={} gmb={} rng={}",
2490 motion_mode_eligible as u8,
2491 overlappable_above(grid, mi_row, mi_col, bw4, mi_cols as usize, 1).len(),
2492 overlappable_left(grid, mi_row, mi_col, bh4, mi_rows as usize, 1).len(),
2493 switchable_motion_mode as u8,
2494 skip_mode as u8,
2495 interintra_mode.is_some() as u8,
2496 is_global_mv_block as u8,
2497 dec.debug_state().0,
2498 );
2499 }
2500 let mut obmc_selected = false;
2501 // lane-warp round 2: `Some` when motion_mode == WARPED_CAUSAL
2502 // *and* the local warp estimate is valid (`!wm_params.invalid`,
2503 // `av1_find_projection`) -- an invalid estimate falls back to
2504 // this block's own translational mv, same as any other block
2505 // (spec `allow_warp`/`reconinter.c` -- global warp is never the
2506 // fallback here since this decoder's global motion is always
2507 // IDENTITY).
2508 let mut warp_params: Option<crate::warp::WarpParams> = None;
2509 // Tracks the SYMBOL value, not projection validity: libaom's
2510 // `av1_is_interp_needed` suppresses the interp-filter read for
2511 // `motion_mode == WARPED_CAUSAL` even when the projection later
2512 // falls back to translation. Its third suppressor,
2513 // `is_nontrans_global_motion`, matches our unconditional
2514 // `is_globalmv` only because this decoder codes global motion as
2515 // IDENTITY (non-TRANSLATION) and never reads switchable filters
2516 // below 8x8 -- porting TRANSLATION global motion must add the
2517 // wmtype check here.
2518 let mut warped_selected = false;
2519 if motion_mode_eligible {
2520 // `default_obmc_cdf`'s own index: square bsize 8/16/32/64 ->
2521 // 0/1/2/3; lane-rect r2: a rect strip reads its OWN bsize row
2522 // (libaom `motion_mode_cdf[mbmi->bsize]`: BLOCK_16X32 -> 4,
2523 // BLOCK_32X16 -> 5 in our packed table) -- rect-flake-1's
2524 // strip motion_mode read diverged on the square row.
2525 // lane-inter4 r1: every rect shape this decoder can now
2526 // reach reads its OWN libaom bsize row (the packed order is
2527 // documented on `cdf::MOTION_MODE`); a wrong row is a wrong
2528 // interval narrowing, not a wrong value (class
2529 // wrong-alphabet-same-value).
2530 let bsize_idx = motion_mode_cdf_row(write_w, write_h).ok_or_else(|| {
2531 unsupported("a motion_mode symbol for a block shape with no CDF row here")
2532 })?;
2533 // `motion_mode_allowed` reads the 3-symbol `motion_mode_cdf`
2534 // instead of the 2-symbol `obmc_cdf` exactly when
2535 // `num_proj_ref >= 1` under `allow_warped_motion`.
2536 // lane-scaledref r2 + lane-fimv r1: libaom
2537 // `motion_mode_allowed` (`blockd.h:1484`) requires BOTH
2538 // `!av1_is_scaled(block_ref_scale_factors[0])` and
2539 // `!xd->cur_frame_force_integer_mv` for WARPED_CAUSAL --
2540 // under a scaled reference (superres) OR on a frame whose
2541 // header set `force_integer_mv` (screen content) the block
2542 // reads the 2-symbol `obmc_cdf`, never the 3-symbol
2543 // `motion_mode_cdf`. Reading the wrong alphabet here narrows
2544 // the arithmetic coder by the wrong amount and predicts the
2545 // rest of the tile off a diverged state: silently wrong
2546 // pixels, not a desync error. `force_integer_mv` is the
2547 // per-frame value (spec 5.9.2: coded per frame only when
2548 // `seq_force_integer_mv == SELECT_INTEGER_MV`, threaded from
2549 // `FrameHeader::force_integer_mv`).
2550 // libaom `av1_is_scaled` is `x_scale_fp != REF_NO_SCALE ||
2551 // y_scale_fp != REF_NO_SCALE`, which `RefScale::is_unscaled`
2552 // reads on both axes.
2553 let ref_is_scaled = !scale_of((ref_width, ref_height), frame_size).is_unscaled();
2554 let proj_ok = num_proj_ref(
2555 grid,
2556 mi_row,
2557 mi_col,
2558 bw4,
2559 bh4,
2560 mi_cols as usize,
2561 mi_rows as usize,
2562 ref_frame,
2563 fctx,
2564 ) >= 1;
2565 let warp_eligible =
2566 allow_warped_motion && !ref_is_scaled && !force_integer_mv && proj_ok;
2567 if allow_warped_motion && proj_ok && ref_is_scaled {
2568 hit!(SCALED_WARP_SUPPRESSED_HITS);
2569 }
2570 if allow_warped_motion && proj_ok && !ref_is_scaled && force_integer_mv {
2571 hit!(FIMV_ALPHABET_HITS);
2572 }
2573 if warp_eligible {
2574 crate::msac::SymbolDecoder::set_symr_cdf("motion_mode");
2575 let mode = dec.symbol(&mut cdfs.motion_mode[bsize_idx]);
2576 match mode {
2577 0 => {}
2578 1 => {
2579 obmc_selected = true;
2580 hit!(OBMC_HITS);
2581 }
2582 _ => {
2583 warped_selected = true;
2584 hit!(WARP_SELECTED_HITS);
2585 let mut samples = find_samples(
2586 grid,
2587 mi_row,
2588 mi_col,
2589 bw4,
2590 bh4,
2591 mi_cols as usize,
2592 mi_rows as usize,
2593 ref_frame,
2594 fctx,
2595 );
2596 if crate::envflags::env_flag!("EC_WARP_DEBUG") {
2597 eprintln!(
2598 "EC_WARP_DEBUG findSamples mi_row={mi_row} mi_col={mi_col} bsize={side} num_proj_ref={}",
2599 samples.len()
2600 );
2601 for (i, s) in samples.iter().enumerate() {
2602 eprintln!(
2603 "EC_WARP_DEBUG sample[{i}] pts={:?} pts_inref={:?}",
2604 s.pts1, s.pts2
2605 );
2606 }
2607 }
2608 if samples.len() > 1 {
2609 crate::warp::select_samples(
2610 mv,
2611 &mut samples,
2612 side as i32,
2613 side as i32,
2614 );
2615 }
2616 warp_params = crate::warp::find_projection(
2617 &samples,
2618 // lane-rect r2: the block's TRUE dims -- a
2619 // 32x16 strip's model center is not a
2620 // 32x32's (aom av1_find_projection bw/bh).
2621 write_w as i32,
2622 write_h as i32,
2623 mv.1,
2624 mv.0,
2625 mi_row as i32,
2626 mi_col as i32,
2627 );
2628 if crate::envflags::env_flag!("EC_WARP_DEBUG") {
2629 eprintln!(
2630 "EC_WARP_DEBUG projection mi_row={mi_row} mi_col={mi_col} num_proj_ref(final)={} mv=({},{}) params={:?}",
2631 samples.len(),
2632 mv.0,
2633 mv.1,
2634 warp_params
2635 );
2636 for (i, s) in samples.iter().enumerate() {
2637 eprintln!(
2638 "EC_WARP_DEBUG sample_used[{i}] pts={:?} pts_inref={:?}",
2639 s.pts1, s.pts2
2640 );
2641 }
2642 }
2643 }
2644 }
2645 } else {
2646 obmc_selected = dec.symbol(&mut cdfs.obmc[bsize_idx]) == 1;
2647 if obmc_selected {
2648 hit!(OBMC_HITS);
2649 }
2650 }
2651 // lane-t900 r9: which ALPHABET this block read (3-symbol
2652 // motion_mode vs 2-symbol obmc) and the range after -- the
2653 // two give the same value with a different narrowing (class
2654 // wrong-alphabet-same-value).
2655 if crate::envflags::env_flag!("EC_TRACE_MODE_STEP") {
2656 eprintln!(
2657 "EC_MM2 mi_row={mi_row} mi_col={mi_col} row={bsize_idx} warp_elig={} \
2658 scaled={} fimv={} obmc={} warped={} rng={}",
2659 warp_eligible as u8,
2660 ref_is_scaled as u8,
2661 force_integer_mv as u8,
2662 obmc_selected as u8,
2663 warped_selected as u8,
2664 dec.debug_state().0,
2665 );
2666 }
2667 }
2668 // lane-gm r4: `allow_warp`'s `global_warp_allowed` branch
2669 // (`reconinter.c:33-55`), gated INDEPENDENTLY of `motion_mode` --
2670 // libaom's `av1_init_warp_params` runs for every inter
2671 // predictor build, not just a `WARPED_CAUSAL`-selected one. Only
2672 // reachable when `warp_params` is still `None` here: a block
2673 // that read (and got) local `WARPED_CAUSAL` above already has
2674 // `local_warp_allowed && !wm_params.invalid`, which `allow_warp`
2675 // checks FIRST and short-circuits on -- the global branch is an
2676 // `else if`, never layered on top. `is_global_mv_block` already
2677 // encodes `allow_warp`'s size bound (`min(bw4,bh4) >= 2`, i.e.
2678 // >=8px both dims, matching `av1_init_warp_params`'s
2679 // `block_height < 8 || block_width < 8` early return) and, via
2680 // `motion_mode_eligible`'s own suppression above, the
2681 // `force_integer_mv` case never reads a motion_mode symbol at
2682 // all -- checked again here directly since this branch fires
2683 // independent of `motion_mode_eligible`.
2684 let gm_ref = &global_motion[(ref_frame - LAST_FRAME) as usize];
2685 if warp_params.is_none() && is_global_mv_block && !force_integer_mv && !gm_ref.invalid {
2686 warp_params = crate::warp::global_warp_params(gm_ref.params);
2687 if warp_params.is_some() && gm_ref.model == ec_av1_syntax::WarpModel::Affine {
2688 hit!(AFFINE_GM_HITS);
2689 }
2690 // lane-av1gwarp12: the ROTZOOM sibling -- the only
2691 // >TRANSLATION model vanilla aomenc can emit for a single-ref
2692 // global-motion block (encoder search pinned to ROTZOOM).
2693 if warp_params.is_some() && gm_ref.model == ec_av1_syntax::WarpModel::Rotzoom {
2694 hit!(ROTZOOM_GM_WARP_HITS);
2695 }
2696 }
2697 // lane-av112bitw: a local `WARPED_CAUSAL` projection (set above)
2698 // or a global warp model both land here with `warp_params` set --
2699 // the 12-bit refusal that used to stand here is LIFTED: warp.rs's
2700 // rounding is parameterised on the bumped `conv_params->round_0`
2701 // (`warp::warp_round_0`, 5 at 12 bits) and the six-frame 12-bit
2702 // warp stream decodes byte-exact vs ffmpeg
2703 // (`a_12bit_warped_motion_stream_decodes_pixel_exact`).
2704 if crate::envflags::env_flag!("EC_AV1_TELL") {
2705 eprintln!(
2706 "TELL mi_row={mi_row} mi_col={mi_col} label=post_motion_mode eligible={} tell={} range={}",
2707 motion_mode_eligible as u8,
2708 dec.debug_bitpos(),
2709 dec.debug_state().0
2710 );
2711 }
2712 // spec `get_ref_filter_type`: a neighbour's own filter only feeds
2713 // this block's switchable_interp context when that neighbour coded
2714 // the SAME reference frame this block is about to read -- a
2715 // different-ref neighbour reads as "no neighbour" ([3, 3], the
2716 // sentinel `Neighbours::new` already seeds unset slots with),
2717 // exactly like an intra neighbour. Without this gate, a GOLDEN_FRAME
2718 // block next to a LAST_FRAME one inherits that neighbour's filter
2719 // choice into its own context, corrupting the symbol it reads.
2720 // lane-av1idx r2: also match on the neighbour's SECOND reference
2721 // (`above_ref1`/`left_ref1`, real for a compound neighbour) --
2722 // see the 16x16 leaf's own comment above for the spec citation.
2723 let above_filter_ctx = if neighbours.above_ref[cmi] == ref_frame
2724 || neighbours.above_ref1[cmi] == Some(ref_frame)
2725 {
2726 neighbours.above_filter[cmi]
2727 } else {
2728 [3, 3]
2729 };
2730 let left_filter_ctx = if neighbours.left_ref[rmi] == ref_frame
2731 || neighbours.left_ref1[rmi] == Some(ref_frame)
2732 {
2733 neighbours.left_filter[rmi]
2734 } else {
2735 [3, 3]
2736 };
2737 let (h_filter, v_filter, resolved_filter) = resolve_interp_filter(
2738 dec,
2739 cdfs,
2740 interp_fixed,
2741 enable_dual_filter,
2742 gm_nontrans || warped_selected,
2743 above_filter_ctx,
2744 left_filter_ctx,
2745 false,
2746 );
2747 block_filter = resolved_filter;
2748 globalmv_for_lf = is_globalmv;
2749 if crate::envflags::env_flag!("EC_AV1_TELL") {
2750 eprintln!(
2751 "TELL mi_row={mi_row} mi_col={mi_col} label=post_interp_filter tell={} range={}",
2752 dec.debug_bitpos(),
2753 dec.debug_state().0
2754 );
2755 }
2756 if crate::envflags::env_flag!("EC_AV1_TRACE") {
2757 eprintln!(
2758 "EC_TRACE mi_row={mi_row} mi_col={mi_col} skip={} is_inter=1 mv=({},{}) is_new_mv={is_new_mv} bsize={side} ref={ref_frame} filter={:?} motion_mode_eligible={} obmc_selected={} warped={} tell={}",
2759 skip as u8,
2760 mv.0,
2761 mv.1,
2762 block_filter,
2763 motion_mode_eligible as u8,
2764 obmc_selected as u8,
2765 warp_params.is_some() as u8,
2766 dec.debug_bitpos()
2767 );
2768 }
2769 // lane-rect r2: see the compound path's matching comment above --
2770 // `grid` must be stamped with the block's true bh4/bw4 span, not
2771 // a square `bw4`x`bw4` guess.
2772 grid.fill_rect(
2773 mi_row,
2774 mi_col,
2775 bh4,
2776 bw4,
2777 MiInfo {
2778 is_inter: true,
2779 ref_frame,
2780 // An interintra block records ref_frame[1] ==
2781 // INTRA_FRAME (0): warp-sample gathering
2782 // (libaom av1_findSamples, mvref_common.c:1155)
2783 // requires ref_frame[1] == NONE_FRAME, so such a
2784 // neighbour must not donate samples or count in
2785 // num_proj_ref.
2786 ref_frame1: if interintra_mode.is_some() {
2787 0
2788 } else {
2789 NO_REF1
2790 },
2791 mv1: (0, 0),
2792 mv: mv16(mv),
2793 is_new_mv,
2794 size: bw4 as u8,
2795 size_h: bh4 as u8,
2796 is_global_mv0: is_global_mv_block,
2797 is_global_mv1: false,
2798 },
2799 );
2800 mode_for_tx = 0;
2801 uv_predict_mode = DC_PRED;
2802
2803 // lane-superres r9: spec 7.11.3.3's x_scale_fp comes from LUMA
2804 // widths only (r8's derivation) and applies unchanged to the
2805 // chroma calls below (their x_q4 is already in the chroma
2806 // plane's own pixel units). `mc::predict_scaled` has no warp/OBMC
2807 // /interintra counterpart -- those three combinations are refused
2808 // by name instead of silently sampling a scaled reference wrong
2809 // (warp_params/obmc_selected/interintra_mode are all resolved by
2810 // this point, every symbol for this block already read).
2811 let luma_scale = scale_of((ref_width, ref_height), frame_size);
2812 if !luma_scale.is_unscaled() {
2813 // lane-scaledref r1: libaom `allow_warp`
2814 // (av1/common/reconinter.c:41) suppresses BOTH local and
2815 // global warp under a scaled reference and predicts
2816 // translationally instead -- the fallback below. lane-t900
2817 // r28 LIFTED the refusal that sat here: the mismatch r1
2818 // measured was the 16x4/4x16 strip's sub-8x8 chroma piece,
2819 // which this file used to SKIP under a scaled reference
2820 // (fixed in the same commit), not the warp fallback.
2821 if warp_params.is_some() {
2822 hit!(SCALED_WARP_FALLBACK_HITS);
2823 }
2824 if obmc_selected {
2825 hit!(SCALED_OBMC_HITS);
2826 }
2827 if interintra_mode.is_some() {
2828 hit!(SCALED_INTERINTRA_HITS);
2829 }
2830 }
2831
2832 // lane-inter16ab r2: `is_sub8x8_inter`'s neighbour walk reads the
2833 // PREVIOUS strip's `mv`/`ref_frame`/`interp_filters`
2834 // (reconinter_template.inc:75-82); record them for the next 1:4
2835 // strip, which is the only reader.
2836 fctx.inter_last_mc
2837 .with(|c| c.set(Some((ref_frame, mv, h_filter, v_filter))));
2838 // lane-defer8: the sub-8x8 chroma piece's own reference resolved to
2839 // a SIZE here and to pixels inside the closure, like the block's own.
2840 let sub8 = match strip_chroma
2841 .filter(|s| s.has_chroma)
2842 .and_then(|s| s.prev.map(|p| (s.horz, p)))
2843 {
2844 Some((horz, (prev_ref, prev_mv, prev_h, prev_v))) => {
2845 let prev_dims = ref_dims(prev_ref, refpix)?;
2846 hit!(INTER16_SUB8_PIECE_HITS);
2847 Some((
2848 horz,
2849 prev_ref,
2850 prev_mv,
2851 prev_h,
2852 prev_v,
2853 scale_of(prev_dims, frame_size),
2854 ))
2855 }
2856 None => None,
2857 };
2858 // The whole-block prediction (motion compensation + warp + the
2859 // interintra blend) runs here, inline. OBMC and the sub-8x8 chroma
2860 // piece are built here too.
2861 // lane-defer8: OBMC's neighbour walk reads the mode-info grid and
2862 // the neighbour bands, both of which the parse keeps rewriting --
2863 // so the walk happens here and the blend inside the closure.
2864 let obmc = if obmc_selected {
2865 Some(obmc_plan(
2866 grid,
2867 &neighbours.above_filter,
2868 &neighbours.left_filter,
2869 mi_row,
2870 mi_col,
2871 bw4,
2872 bh4,
2873 mi_rows as usize,
2874 mi_cols as usize,
2875 side,
2876 write_w,
2877 write_h,
2878 ss_x(fctx),
2879 ss_y(fctx),
2880 chroma_stride,
2881 px,
2882 py,
2883 cpx,
2884 cpy,
2885 refpix,
2886 interp_fixed,
2887 frame_size,
2888 )?)
2889 } else {
2890 None
2891 };
2892 let build = move |y: &mut PlaneBuf<'static>,
2893 u: &mut PlaneBuf<'static>,
2894 v: &mut PlaneBuf<'static>,
2895 refpix: &RefPix<'_>,
2896 fctx: &crate::decode::FrameCtx,
2897 out: &mut [Vec<u16>; 3]| {
2898 let Some((py_ref, pu_ref, pv_ref)) = ref_planes_deferred(ref_frame, refpix) else {
2899 return;
2900 };
2901 let mut pred_y = refill(std::mem::take(&mut out[0]), side * side);
2902 let mut pred_u = refill(std::mem::take(&mut out[1]), chroma_stride * chroma_buf_h);
2903 let mut pred_v = refill(std::mem::take(&mut out[2]), chroma_stride * chroma_buf_h);
2904 if luma_scale.is_unscaled() {
2905 mc::predict_with_filters_kern(
2906 &py_ref.data,
2907 py_ref.width,
2908 py_ref.true_width,
2909 py_ref.true_height,
2910 mv_to_q4(px, mv.1, 0),
2911 mv_to_q4(py, mv.0, 0),
2912 side,
2913 side,
2914 write_w,
2915 write_h,
2916 h_filter,
2917 v_filter,
2918 &mut pred_y,
2919 fctx,
2920 );
2921 mc::predict_with_filters_kern(
2922 &pu_ref.data,
2923 pu_ref.width,
2924 pu_ref.true_width,
2925 pu_ref.true_height,
2926 mv_to_q4(cpx, mv.1, ss_x(fctx)),
2927 mv_to_q4(cpy, mv.0, ss_y(fctx)),
2928 chroma_stride,
2929 chroma_buf_h,
2930 write_chroma_w,
2931 write_chroma_h,
2932 h_filter,
2933 v_filter,
2934 &mut pred_u,
2935 fctx,
2936 );
2937 mc::predict_with_filters_kern(
2938 &pv_ref.data,
2939 pv_ref.width,
2940 pv_ref.true_width,
2941 pv_ref.true_height,
2942 mv_to_q4(cpx, mv.1, ss_x(fctx)),
2943 mv_to_q4(cpy, mv.0, ss_y(fctx)),
2944 chroma_stride,
2945 chroma_buf_h,
2946 write_chroma_w,
2947 write_chroma_h,
2948 h_filter,
2949 v_filter,
2950 &mut pred_v,
2951 fctx,
2952 );
2953 } else {
2954 mc::predict_scaled_kern(
2955 &py_ref.data,
2956 py_ref.width,
2957 py_ref.true_width,
2958 py_ref.true_height,
2959 mv_to_q4(px, mv.1, 0),
2960 mv_to_q4(py, mv.0, 0),
2961 luma_scale,
2962 side,
2963 side,
2964 write_w,
2965 write_h,
2966 h_filter,
2967 v_filter,
2968 &mut pred_y,
2969 fctx,
2970 );
2971 mc::predict_scaled_kern(
2972 &pu_ref.data,
2973 pu_ref.width,
2974 pu_ref.true_width,
2975 pu_ref.true_height,
2976 mv_to_q4(cpx, mv.1, ss_x(fctx)),
2977 mv_to_q4(cpy, mv.0, ss_y(fctx)),
2978 luma_scale,
2979 chroma_stride,
2980 chroma_buf_h,
2981 write_chroma_w,
2982 write_chroma_h,
2983 h_filter,
2984 v_filter,
2985 &mut pred_u,
2986 fctx,
2987 );
2988 mc::predict_scaled_kern(
2989 &pv_ref.data,
2990 pv_ref.width,
2991 pv_ref.true_width,
2992 pv_ref.true_height,
2993 mv_to_q4(cpx, mv.1, ss_x(fctx)),
2994 mv_to_q4(cpy, mv.0, ss_y(fctx)),
2995 luma_scale,
2996 chroma_stride,
2997 chroma_buf_h,
2998 write_chroma_w,
2999 write_chroma_h,
3000 h_filter,
3001 v_filter,
3002 &mut pred_v,
3003 fctx,
3004 );
3005 }
3006
3007 // lane-scaledref r1: libaom `allow_warp` (av1/common/reconinter.c:41)
3008 // opens with `if (av1_is_scaled(sf)) return 0;` -- BOTH local
3009 // (`wm_params`) and global warp fall back to the translational
3010 // prediction above when the reference is scaled, even though the
3011 // motion_mode symbol was still read and warp_params still
3012 // resolved. Suppress the warp, keep every symbol read.
3013 if let Some(params) = warp_params.as_ref().filter(|_| luma_scale.is_unscaled()) {
3014 // `av1_init_warp_params` is per PLANE: the luma plane's
3015 // own block is `write_w`x`write_h` (a PARTITION_HORZ/VERT
3016 // strip is not `side`x`side`), so a strip under 8 px in
3017 // either direction keeps the translational prediction too.
3018 if warp_plane_allowed(write_w, write_h) {
3019 crate::warp::warp_affine(
3020 params,
3021 &py_ref.data,
3022 py_ref.true_width as i32,
3023 py_ref.true_height as i32,
3024 py_ref.width as i32,
3025 &mut pred_y,
3026 px as i32,
3027 py as i32,
3028 side as i32,
3029 side as i32,
3030 side as i32,
3031 0,
3032 0,
3033 fctx,
3034 );
3035 }
3036 // libaom `av1_init_warp_params` (reconinter.c): warp is
3037 // per PLANE and bails out at `block_width < 8 ||
3038 // block_height < 8`, so a plane whose own block is under
3039 // 8x8 keeps the translational prediction built above --
3040 // in 420 that is every chroma plane whose own width or
3041 // height is under 8 -- `write_chroma_w`/`write_chroma_h`,
3042 // NOT the square `chroma_side` (lane-t900 r5: an 8x16 luma
3043 // block has `chroma_side == 8` but a 4x8 chroma block, so
3044 // the square side warped chroma libaom predicts
3045 // translationally; lane-gmaffine r4 for the 8x8 leaf).
3046 if warp_plane_allowed(write_chroma_w, write_chroma_h) {
3047 crate::warp::warp_affine(
3048 params,
3049 &pu_ref.data,
3050 pu_ref.true_width as i32,
3051 pu_ref.true_height as i32,
3052 pu_ref.width as i32,
3053 &mut pred_u,
3054 cpx as i32,
3055 cpy as i32,
3056 chroma_stride as i32,
3057 chroma_buf_h as i32,
3058 chroma_stride as i32,
3059 ss_x(fctx) as i32,
3060 ss_y(fctx) as i32,
3061 fctx,
3062 );
3063 crate::warp::warp_affine(
3064 params,
3065 &pv_ref.data,
3066 pv_ref.true_width as i32,
3067 pv_ref.true_height as i32,
3068 pv_ref.width as i32,
3069 &mut pred_v,
3070 cpx as i32,
3071 cpy as i32,
3072 chroma_stride as i32,
3073 chroma_buf_h as i32,
3074 chroma_stride as i32,
3075 ss_x(fctx) as i32,
3076 ss_y(fctx) as i32,
3077 fctx,
3078 );
3079 }
3080 }
3081 // Mutually exclusive with warp/OBMC (motion_mode is not read for
3082 // an interintra block): blend the intra predictor over the MC
3083 // result, all planes (reconinter.c av1_build_interintra_predictor
3084 // plane loop).
3085 if let Some(ii) = interintra_mode {
3086 if write_w != write_h {
3087 hit!(INTERINTRA_RECT_HITS);
3088 }
3089 interintra_blend(
3090 y,
3091 px,
3092 py,
3093 write_w,
3094 write_h,
3095 side,
3096 0,
3097 0,
3098 ii,
3099 wedge_mask,
3100 &mut pred_y,
3101 fctx,
3102 );
3103 interintra_blend(
3104 u,
3105 cpx,
3106 cpy,
3107 write_chroma_w,
3108 write_chroma_h,
3109 chroma_stride,
3110 ss_x(fctx),
3111 ss_y(fctx),
3112 ii,
3113 wedge_mask,
3114 &mut pred_u,
3115 fctx,
3116 );
3117 interintra_blend(
3118 v,
3119 cpx,
3120 cpy,
3121 write_chroma_w,
3122 write_chroma_h,
3123 chroma_stride,
3124 ss_x(fctx),
3125 ss_y(fctx),
3126 ii,
3127 wedge_mask,
3128 &mut pred_v,
3129 fctx,
3130 );
3131 }
3132 if let Some(plan) = &obmc {
3133 obmc_run(plan, refpix, &mut pred_y, &mut pred_u, &mut pred_v, fctx);
3134 }
3135 // lane-inter16ab r2: libaom `build_inter_predictors_sub8x8`
3136 // (reconinter_template.inc:87-160). A 4:2:0 chroma block under a
3137 // 16x4 (4x16) luma pair covers TWO luma blocks, so it is built in
3138 // `b4_w x b4_h` = 8x2 (2x8) pieces, each from its own strip's
3139 // mv/ref/filters. The bottom (right) piece is this strip's own mv
3140 // over its own rows, which the whole-block prediction above
3141 // already wrote there (MC is position-invariant for a fixed mv),
3142 // so only the FIRST piece has to be rebuilt -- and only when
3143 // `is_sub8x8_inter` holds. lane-t900 r28: it scales off the
3144 // PREVIOUS strip's own reference, not this block's.
3145 if let Some((horz, prev_ref, prev_mv, prev_h, prev_v, prev_scale)) = sub8 {
3146 let Some((_, prev_u, prev_v_plane)) = ref_planes_deferred(prev_ref, refpix)
3147 else {
3148 return;
3149 };
3150 let (piece_w, piece_h) = if horz {
3151 (8usize, 2usize)
3152 } else {
3153 (2usize, 8usize)
3154 };
3155 for (src, dst) in [(prev_u, &mut pred_u), (prev_v_plane, &mut pred_v)] {
3156 let mut piece = vec![0u16; piece_w * piece_h];
3157 mc::predict_maybe_scaled(
3158 &src.data,
3159 src.width,
3160 src.true_width,
3161 src.true_height,
3162 mv_to_q4(cpx, prev_mv.1, ss_x(fctx)),
3163 mv_to_q4(cpy, prev_mv.0, ss_y(fctx)),
3164 prev_scale,
3165 piece_w,
3166 piece_h,
3167 prev_h,
3168 prev_v,
3169 &mut piece,
3170 fctx,
3171 );
3172 for row in 0..piece_h {
3173 dst[row * chroma_stride..row * chroma_stride + piece_w]
3174 .copy_from_slice(&piece[row * piece_w..(row + 1) * piece_w]);
3175 }
3176 }
3177 }
3178 *out = [pred_y, pred_u, pred_v];
3179 };
3180 // OBMC and interintra are mutually exclusive (`motion_mode` is
3181 // never read for an interintra block), so blending interintra
3182 // inside `build` and OBMC after it is still the coded order.
3183 debug_assert!(!(obmc_selected && interintra_mode.is_some()));
3184 // lane-defer8: OBMC and the sub-8x8 piece now live inside `build`,
3185 // so nothing about this block keeps its prediction on this thread.
3186 let (pred_y, pred_u, pred_v): (Vec<u16>, Vec<u16>, Vec<u16>);
3187 let (sy, su, sv) = if pred_deferred(refpix) {
3188 push_pred(build);
3189 (Pred::Cur { plane: 0, stride: side, off: 0 }, Pred::Cur { plane: 1, stride: chroma_stride, off: 0 }, Pred::Cur { plane: 2, stride: chroma_stride, off: 0 })
3190 } else {
3191 let mut built: [Vec<u16>; 3] = Default::default();
3192 build(y, u, v, refpix, fctx, &mut built);
3193 let [by, bu, bv] = built;
3194 (pred_y, pred_u, pred_v) = (by, bu, bv);
3195
3196 // lane-inter16ab r2: libaom `build_inter_predictors_sub8x8`
3197 // (reconinter_template.inc:87-160). A 4:2:0 chroma block under a
3198 // 16x4 (4x16) luma pair covers TWO luma blocks, so it is built in
3199 // `b4_w x b4_h` = 8x2 (2x8) pieces, each from its own strip's
3200 // mv/ref/filters. The bottom (right) piece is this strip's own mv
3201 // over its own rows, which the whole-block prediction above
3202 // already wrote there (MC is position-invariant for a fixed mv),
3203 // so only the FIRST piece has to be rebuilt -- and only when
3204 // `is_sub8x8_inter` holds, i.e. the previous strip was itself a
3205 // single-reference inter block (`prev`), else libaom falls
3206 // through to the ordinary whole-block predictor.
3207 (
3208 Pred::Inline(&pred_y, side),
3209 Pred::Inline(&pred_u, chroma_stride),
3210 Pred::Inline(&pred_v, chroma_stride),
3211 )
3212 };
3213
3214 // lane-inter4 r2: a rectangular strip's transform tree starts from
3215 // its own rect `max_txsize_rect_lookup` entry, not a square one.
3216 let rect_tu = (write_w, write_h) != (side, side);
3217 vartx_leaves = if rect_tu {
3218 read_block_tx_size_rect(
3219 dec,
3220 cdfs,
3221 neighbours,
3222 at_mi,
3223 (write_w, write_h),
3224 (mi_cols as usize, mi_rows as usize),
3225 skip,
3226 fctx,
3227 )?
3228 } else {
3229 read_block_tx_size(
3230 dec,
3231 cdfs,
3232 neighbours,
3233 at_mi,
3234 side,
3235 (mi_cols as usize, mi_rows as usize),
3236 true,
3237 skip,
3238 fctx,
3239 )?
3240 .1
3241 };
3242 if skip {
3243 push_mc_rect(
3244 y,
3245 u,
3246 v,
3247 0,
3248 px,
3249 py,
3250 side,
3251 write_w,
3252 write_h,
3253 sy,
3254 &ZERO_RESIDUAL[..side * side],
3255 fctx,
3256 );
3257 if has_chroma {
3258 push_mc_rect(
3259 y,
3260 u,
3261 v,
3262 1,
3263 cpx,
3264 cpy,
3265 chroma_stride,
3266 write_chroma_w,
3267 write_chroma_h,
3268 su,
3269 &ZERO_RESIDUAL[..chroma_stride * chroma_buf_h],
3270 fctx,
3271 );
3272 push_mc_rect(
3273 y,
3274 u,
3275 v,
3276 2,
3277 cpx,
3278 cpy,
3279 chroma_stride,
3280 write_chroma_w,
3281 write_chroma_h,
3282 sv,
3283 &ZERO_RESIDUAL[..chroma_stride * chroma_buf_h],
3284 fctx,
3285 );
3286 }
3287 luma_grid = Grid::Zero(side * side);
3288 u_grid = Grid::Zero(chroma_stride * chroma_buf_h);
3289 v_grid = Grid::Zero(chroma_stride * chroma_buf_h);
3290 } else {
3291 // lane-inter4 r2: a rect strip's coefficient context spans its
3292 // OWN width above and height left -- reading `side` cells both
3293 // ways indexes past the tile's last mi row for a bottom strip.
3294 let around = if rect_tu {
3295 // lane-av1422mi24: same 4:2:2 rect-strip chroma gather
3296 // rule as the single-TU path above -- luma per-mi,
3297 // chroma every-second above cell.
3298 let mut a = neighbours.around_mi_rect(at_mi, write_w, write_h);
3299 if ss_x(fctx) == 1 && ss_y(fctx) == 0 {
3300 let c = neighbours.around_mi_422_chroma(at_mi, write_w, write_h);
3301 a[1] = c[1];
3302 a[2] = c[2];
3303 }
3304 a
3305 } else if ss_x(fctx) == 1 && ss_y(fctx) == 0 {
3306 // lane-av1422mi24: same 4:2:2 square-block chroma gather
3307 // rule as the single-TU path above -- luma per-mi, chroma
3308 // every-second above cell.
3309 let mut a = neighbours.around_mi(at, side);
3310 let c = neighbours.around_mi_422_chroma(at, write_w, write_h);
3311 a[1] = c[1];
3312 a[2] = c[2];
3313 a
3314 } else {
3315 neighbours.around_mi(at, side)
3316 };
3317 // lane-inter16ab r2: the pair's chroma transform sits at the
3318 // EVEN strip's mi origin and spans both strips, so its
3319 // above/left coefficient context is the pair's, not this
3320 // strip's row.
3321 // lane-av1422ctxgather: at 4:2:2 (ss 1,0) a HORZ strip is
3322 // its own chroma reference (`is_chroma_reference`'s
3323 // `!(subsampling_y)` row clause) -- pair_mi is the strip
3324 // itself and the context reads the strip's own 8x4, ONE
3325 // luma-mi row tall, with the 4:2:2 above sampling
3326 // ([`Neighbours::around_mi_422_chroma`]) -- and the same
3327 // reroute catches 4:2:2 VERT: its (8, 16) pair span's
3328 // above side covers one chroma column per two luma mi
3329 // columns, the identical double-count.
3330 //
3331 // lane-av1444rect (H1, MEASURED): the (16, 8) / (8, 16) pair
3332 // extent was reached at ss (0,0) too, where no pair exists.
3333 // At 4:4:4 `is_chroma_reference` (av1_common_int.h:1454)
3334 // reduces BOTH parity clauses to `!subsampling_y` /
3335 // `!subsampling_x`, so every block is its own chroma
3336 // reference: a HORZ_4 16x4 strip codes its OWN 16x4 chroma
3337 // and the setter above already gave it `pair_mi == at_mi`.
3338 // Its coefficient context is therefore the strip's own,
3339 // which is exactly the `around` gathered one screen above
3340 // as `around_mi_rect(at_mi, write_w, write_h)`. The pair
3341 // gather read one EXTRA luma mi row of LEFT chroma context
3342 // (`ph = 8` covers left[8] and left[9], a 4-px strip only
3343 // left[8]), so the U unit's `get_txb_ctx` saw a neighbour
3344 // libaom does not have and forked the arithmetic coder: the
3345 // 6441-byte 4:4:4 lossy cq-20 stream, frame 1, mi(8,16)
3346 // read our `txb_skip_ctx` 1 (0 above + 1 left) where the
3347 // oracle read offset-7 row 7 (ctx_base 0).
3348 let around_c = match strip_chroma {
3349 // lane-av1444rect: the arm `is_chroma_reference` makes the
3350 // ONLY correct choice at ss_x 0 -- no pair exists, the
3351 // extent is the strip's own. Counted so a 4:4:4 1:4-strip
3352 // gate can prove it exercised this route (a 4:2:0 stream
3353 // of the same content reads 0).
3354 Some(_) if ss_x(fctx) == 0 => {
3355 hit!(RECT4_INTER_OWN_CHROMA444_HITS);
3356 around
3357 }
3358 Some(s) if s.has_chroma => {
3359 let (pw, ph) = if s.horz {
3360 if ss_y(fctx) == 0 { (16, 4) } else { (16, 8) }
3361 } else {
3362 (8, 16)
3363 };
3364 if ss_x(fctx) == 1 && ss_y(fctx) == 0 {
3365 neighbours.around_mi_422_chroma(s.pair_mi, pw, ph)
3366 } else {
3367 neighbours.around_mi_rect(s.pair_mi, pw, ph)
3368 }
3369 }
3370 _ => around,
3371 };
3372 let luma_tx_type;
3373 // lane-sb128c r1: a 128x128 inter block's chroma is read
3374 // INSIDE the luma leaf loop (each 64x64 mu chunk's own units:
3375 // one per chunk at 4:2:0, FOUR TX_32X32 at ss 0/0); these
3376 // collect them for the block-level neighbour write-back.
3377 let mut u_units: Vec<i32> = Vec::new();
3378 let mut v_units: Vec<i32> = Vec::new();
3379 // lane-av1-444-128: every luma leaf's coded tx_type, so the
3380 // chunk tail's chroma units can each inherit the type of the
3381 // leaf covering their own quadrant (`av1_get_tx_type`,
3382 // blockd.h:1291, scales the CHROMA 4x4 position back to luma).
3383 let mut leaf_tx_types: Vec<(usize, usize, usize, usize, TxType)> = Vec::new();
3384 if let Some(leaves) = vartx_leaves.as_ref() {
3385 // spec 5.11.17's transform tree, leaf by leaf in the order
3386 // `read_var_tx_size` coded it: each unit reads its own
3387 // coefficients against the neighbour magnitudes the
3388 // earlier units already wrote, and reconstructs over its
3389 // own slice of the block's motion-compensated predictor.
3390 let reduced = fctx.reduced_tx_set_inter.with(std::cell::Cell::get);
3391 let mut first_tx_type = TxType::DctDct;
3392 // lane-av1h5: libaom's `decode_token_recon_block` walks the
3393 // block in 64x64 "mu" CHUNKS and codes EVERY PLANE of a
3394 // chunk before the next one starts, so at 128 the token
3395 // order is CHUNK-major. The transform tree hands us a
3396 // BLOCK-wide raster (32 mi across here), whose chunk
3397 // members are not contiguous: `leaves` 0..15 are chunk
3398 // (0,0) row 0, but chunk (0,0) rows 1..15 are leaves
3399 // 32..47, 64..79, ... Walking it as given closed the
3400 // "chunk" every 16 leaves and coded each chunk's chroma
3401 // sixteen times -- 64 chroma walks per 128 root where
3402 // libaom codes 4 (measured against aomdec's `EC_DBGCTX`:
3403 // 24576 chroma units per inter frame vs 1536). Below 128 a
3404 // block IS one chunk and the two orders coincide.
3405 let (order, _mu_reordered) = mu_chunk_order(leaves, side);
3406 for (pos, &idx) in order.iter().enumerate() {
3407 let (row, col, tw, th) = leaves[idx];
3408 let tu_mi = (at_mi.0 + row, at_mi.1 + col);
3409 let (tu_px, tu_py) = (px + col * MI, py + row * MI);
3410 let (tu_grid, tu_tx_type) = if tw == th {
3411 let tx_px = tw;
3412 let tu_pred = sy.offset(row * MI * side + col * MI);
3413 let scan = default_scan(tx_px.min(32));
3414 let tu_skip_ctx = neighbours.luma_skip_ctx(tu_mi, tx_px / MI);
3415 read_inter_plane(
3416 dec,
3417 cdfs,
3418 inter_txbset_for(tx_px, reduced),
3419 scan,
3420 0,
3421 neighbours.around_mi(tu_mi, tx_px)[0],
3422 mode_for_tx,
3423 y,
3424 u,
3425 v,
3426 tu_px,
3427 tu_py,
3428 tx_px,
3429 base_q_idx,
3430 tu_pred,
3431 None,
3432 Some(tu_skip_ctx),
3433 fctx,
3434 )?
3435 } else {
3436 // lane-r14 r3: a 1:4 strip's `sub_tx_size_map`
3437 // sub-transform is RECTANGULAR (TX_64X16 ->
3438 // TX_32X16, TX_16X64 -> TX_16X32), so this leaf is
3439 // read with the rect coefficient machinery, at its
3440 // own `txb_skip_ctx`/neighbour spans -- the unit is
3441 // smaller than its block, so `get_txb_ctx_general`'s
3442 // neighbour table applies, not the lone-TU 0.
3443 vartx_rect_leaf_hit(tw, th);
3444 let tu_skip_ctx =
3445 neighbours.luma_skip_ctx_rect(tu_mi, tw / MI, th / MI);
3446 let start = row * MI * side + col * MI;
3447 read_inter_plane_rect(
3448 dec,
3449 cdfs,
3450 rect_inter_luma_set(tw, th, fctx)?,
3451 (tw, th),
3452 side,
3453 0,
3454 neighbours.around_mi_rect(tu_mi, tw, th)[0],
3455 mode_for_tx,
3456 y,
3457 u,
3458 v,
3459 tu_px,
3460 tu_py,
3461 sy.offset(start),
3462 None,
3463 Some(tu_skip_ctx),
3464 fctx,
3465 )?
3466 };
3467 if idx == 0 {
3468 first_tx_type = tu_tx_type;
3469 }
3470 // lane-av1444chr: EVERY luma leaf's coded type, at
3471 // every block size -- the chroma units below resolve
3472 // their own quadrant's leaf, and at 64x64 that is
3473 // this very loop's leaves (`av1_get_tx_type`,
3474 // blockd.h:1287-1297, scales the CHROMA 4x4 position
3475 // back to luma). It used to be collected only for
3476 // `side > 64`, so the 64x64 four-unit arm below had
3477 // nothing to resolve from and handed all four units
3478 // the block-level (top-left leaf) type. A leaf that
3479 // coded NOTHING is `DctDct` here exactly as libaom's
3480 // `decodetxb.c:199-203` stamps `DCT_DCT` into that
3481 // leaf's map cell on the `all_zero` path.
3482 leaf_tx_types.push((row, col, tw, th, tu_tx_type));
3483 neighbours.record_mi_luma_rect(tu_mi, tw, th, &tu_grid);
3484 // lane-sb128c r1: libaom's `decode_token_recon_block`
3485 // walks 64x64 "mu" chunks (`mu_blocks_wide/high =
3486 // mi_size_wide[BLOCK_64X64]`) and codes EVERY PLANE of
3487 // a chunk before the next one, so a 128x128 block
3488 // interleaves Y,U,V per quadrant -- the same order the
3489 // intra 128 path reads (lane-sb128b r3, measured
3490 // against aomdec's `EC_TRACE_COEFF`). Below 128 a block
3491 // is a single chunk and this never fires.
3492 if side > 64 {
3493 let chunk = (row / 16, col / 16);
3494 let done = pos + 1 == order.len()
3495 || (leaves[order[pos + 1]].0 / 16, leaves[order[pos + 1]].1 / 16)
3496 != chunk;
3497 if done {
3498 let (cr, cc) = chunk;
3499 // `av1_get_max_uv_txsize(BLOCK_128X128)` =
3500 // `av1_get_adjusted_tx_size(TX_64X64)` =
3501 // TX_32X32. The mu walk
3502 // (`decode_token_recon_block`, decodeframe.c)
3503 // then enumerates, per plane, EVERY TX_32X32
3504 // unit of the chunk's chroma in raster order:
3505 // at ss 0/0 the chunk's chroma is 64x64 and
3506 // that is FOUR units; at 4:2:0 it is 32x32
3507 // and the loop degenerates to the one unit
3508 // this arm has always coded. `chroma_side`
3509 // already carries the subsampling.
3510 let cu_tx = 32usize;
3511 // lane-av1-422bigblock: a 64x64 mu chunk's
3512 // chroma plane block is PER-AXIS
3513 // (64 >> ss_x) x (64 >> ss_y) -- 32x64 at
3514 // ss (1,0), i.e. TWO stacked TX_32X32 units
3515 // per plane (`av1_get_max_uv_txsize` caps the
3516 // unit at 32 on each axis). The old square-cut
3517 // model walked `chroma_side`-square chunks:
3518 // twice the units the oracle codes at 4:2:2,
3519 // prediction windows indexed at the square
3520 // `chroma_side` instead of the prediction
3521 // buffer's own `chroma_stride`, and chunk
3522 // columns at 64.. of a 64-wide chroma plane.
3523 let chunk_chroma_w = (64usize) >> ss_x(fctx);
3524 let chunk_chroma_h = (64usize) >> ss_y(fctx);
3525 let units_w = chunk_chroma_w / cu_tx;
3526 let units_h = chunk_chroma_h / cu_tx;
3527 // The unit's own footprint on the LUMA mi
3528 // grid, per axis (32 << ss) px: 64x64 (16x16
3529 // mi) at 4:2:0, 32x32 (8x8 mi) at 4:4:4,
3530 // 64x32 (16x8 mi) at 4:2:2. The context
3531 // gather and `record_mi_chroma`'s stamp span
3532 // exactly the unit's own cells -- `get_txb_ctx`
3533 // reads the 8 context cells of the TX_32X32
3534 // itself, and `av1_set_entropy_contexts` memsets
3535 // the same 8, so the unit over never sees this
3536 // unit's state and the unit below always does.
3537 // At ss (1, 0) those 8 are CHROMA cells, so the
3538 // gather routes through the 4:2:2 pair rule
3539 // (lane-av1muchunk422sym, see the walk below).
3540 let unit_luma_w = cu_tx << ss_x(fctx);
3541 let unit_luma_h = cu_tx << ss_y(fctx);
3542 let cu_scan = default_scan(cu_tx);
3543 if lossless(fctx) {
3544 // lane-lossless2: TX_4X4 units over this
3545 // chunk's chroma, not one TX_32X32 unit.
3546 read_inter_chroma_lossless(
3547 y,
3548 u,
3549 v,
3550 dec,
3551 cdfs,
3552 neighbours,
3553 su,
3554 sv,
3555 at_mi,
3556 (cpx, cpy),
3557 (cc * chunk_chroma_w, cr * chunk_chroma_h),
3558 (chunk_chroma_w, chunk_chroma_h),
3559 (write_chroma_w, write_chroma_h),
3560 // lane-av1422lpf site 5 (the compound
3561 // twin of the single-reference fix at
3562 // 41572): the prediction buffer's own
3563 // `chroma_stride`, never the enclosing
3564 // square `chroma_side`. Identical
3565 // number at 4:2:0 and 4:4:4.
3566 chroma_stride,
3567 mode_for_tx,
3568 base_q_idx,
3569 &mut u_units,
3570 &mut v_units,
3571 fctx,
3572 )?;
3573 mu_chroma = true;
3574 continue;
3575 }
3576 for plane_idx in 1..3 {
3577 let src = if plane_idx == 1 { su } else { sv };
3578 // Plane-major per chunk (libaom's plane
3579 // loop is outermost inside the mu walk);
3580 // within a plane the units walk raster.
3581 for ur in 0..units_h {
3582 for uc in 0..units_w {
3583 let unit_mi = (
3584 at_mi.0 + cr * 16 + ur * (unit_luma_h / MI),
3585 at_mi.1 + cc * 16 + uc * (unit_luma_w / MI),
3586 );
3587 // Per unit, AFTER the earlier
3588 // units' stamps: this unit's
3589 // coefficient context must see
3590 // them.
3591 //
3592 // lane-av1muchunk422sym: at ss
3593 // (1, 0) one CHROMA column spans
3594 // two luma mi columns, so the plain
3595 // rect gather sums this unit's
3596 // above dc sign TWICE where
3597 // libaom's `get_txb_ctx_general`
3598 // reads `txb_w_unit` CHROMA cells --
3599 // one per column. `dc_sign_ctx`
3600 // only sees the vote's signum, so
3601 // the doubled vote flips the row
3602 // exactly when above and left
3603 // cancel; `around_mi_422_chroma`
3604 // samples every second above cell,
3605 // which IS libaom's sum. Same guard
3606 // as the intra twin (13169) and the
3607 // block-8 arm (19810).
3608 let cu_around = if ss_x(fctx) == 1 && ss_y(fctx) == 0 {
3609 neighbours.around_mi_422_chroma(
3610 unit_mi,
3611 unit_luma_w,
3612 unit_luma_h,
3613 )
3614 } else {
3615 neighbours.around_mi_rect(
3616 unit_mi,
3617 unit_luma_w,
3618 unit_luma_h,
3619 )
3620 };
3621 let (cu_x, cu_y) = (
3622 cpx + cc * chunk_chroma_w + uc * cu_tx,
3623 cpy + cr * chunk_chroma_h + ur * cu_tx,
3624 );
3625 // `av1_get_tx_type` (blockd.h:
3626 // 1291): an inter block's chroma
3627 // unit never codes a tx_type; it
3628 // inherits the coded type of the
3629 // luma leaf covering its own
3630 // quadrant (all of this chunk's
3631 // leaves are read by now).
3632 let unit_rel_mi =
3633 (unit_mi.0 - at_mi.0, unit_mi.1 - at_mi.1);
3634 let covering =
3635 covering_leaf_tx_type(&leaf_tx_types, unit_rel_mi);
3636 // The prediction buffer is
3637 // `chroma_stride`-strided (the
3638 // plane block's own width at
3639 // 4:2:2), not the square
3640 // `chroma_side`.
3641 let cu_pred = src.offset(
3642 (cr * chunk_chroma_h + ur * cu_tx) * chroma_stride
3643 + cc * chunk_chroma_w
3644 + uc * cu_tx,
3645 );
3646 let cu_grid = read_inter_plane(
3647 dec,
3648 cdfs,
3649 chroma_set,
3650 cu_scan,
3651 plane_idx,
3652 cu_around[plane_idx],
3653 mode_for_tx,
3654 y,
3655 u,
3656 v,
3657 cu_x,
3658 cu_y,
3659 cu_tx,
3660 base_q_idx,
3661 cu_pred,
3662 Some(covering.unwrap_or(TxType::DctDct)),
3663 Some(3),
3664 fctx,
3665 )?
3666 .0;
3667 // Immediately, so the NEXT unit
3668 // reads this one's coefficient
3669 // context.
3670 neighbours.record_mi_chroma(
3671 unit_mi,
3672 unit_luma_w,
3673 unit_luma_h,
3674 plane_idx,
3675 &cu_grid,
3676 );
3677 hit!(CHROMA_SPLIT_TX_HITS);
3678 mu_chroma = true;
3679 // lane-av1muchunk422b: the allocation of this
3680 // mu-chunk walk, counted per TX_32X32 UNIT.
3681 // The compound and single-reference arms share
3682 // this source text, so `is_compound` routes the
3683 // bump: `MU_CHUNK_COMPOUND_UNITS` names site 2
3684 // and the single-reference twin (site 5) stays
3685 // out of it.
3686 if is_compound {
3687 hit!(MU_CHUNK_COMPOUND_UNITS);
3688 }
3689 let dst = mu_units(
3690 if plane_idx == 1 {
3691 &mut u_units
3692 } else {
3693 &mut v_units
3694 },
3695 chroma_side * chroma_side,
3696 );
3697 for rr in 0..cu_tx {
3698 // The ASSEMBLED grid stays at
3699 // the square `chroma_side`
3700 // stride the block tail's
3701 // `mu_chroma_units` re-stamp
3702 // indexes; the unit's true
3703 // per-axis position is what
3704 // changes.
3705 let start = (cr * chunk_chroma_h + ur * cu_tx + rr)
3706 * chroma_side
3707 + cc * chunk_chroma_w
3708 + uc * cu_tx;
3709 dst[start..start + cu_tx].copy_from_slice(
3710 &cu_grid[rr * cu_tx..][..cu_tx],
3711 );
3712 }
3713 }
3714 }
3715 }
3716 }
3717 }
3718 }
3719 // The block-level `luma_tx_type` stays the first
3720 // (top-left) luma unit's coded type; the PER-UNIT chroma
3721 // inheritance in the chunk tail above resolves each unit's
3722 // own covering leaf instead (`av1_get_tx_type`,
3723 // blockd.h:1291).
3724 luma_tx_type = first_tx_type;
3725 luma_grid = Grid::Zero(side * side);
3726 } else if rect_tu {
3727 // The whole-block rectangular transform unit
3728 // (`txfm_partition` said 0 at depth 0).
3729 let (grid, tx_type) = read_inter_plane_rect(
3730 dec,
3731 cdfs,
3732 rect_inter_luma_set(write_w, write_h, fctx)?,
3733 (write_w, write_h),
3734 side,
3735 0,
3736 around[0],
3737 mode_for_tx,
3738 y,
3739 u,
3740 v,
3741 px,
3742 py,
3743 sy,
3744 None,
3745 None,
3746 fctx,
3747 )?;
3748 luma_tx_type = tx_type;
3749 luma_grid = Grid::Own(embed_rect_grid(&grid, write_w, write_h, side));
3750 } else {
3751 {
3752 let coded = read_inter_plane(
3753 dec,
3754 cdfs,
3755 luma_set_inter,
3756 scan_luma,
3757 0,
3758 around[0],
3759 mode_for_tx,
3760 y,
3761 u,
3762 v,
3763 px,
3764 py,
3765 side,
3766 base_q_idx,
3767 sy,
3768 None,
3769 None,
3770 fctx,
3771 )?;
3772 luma_grid = coded.0;
3773 luma_tx_type = coded.1;
3774 }
3775 }
3776 if !has_chroma {
3777 // `is_chroma_reference` is false for this strip: libaom's
3778 // plane loop (decodeframe.c
3779 // `predict_and_reconstruct_intra_block`/
3780 // `reconstruct_inter_block`'s `for (plane...)` guarded by
3781 // `xd->is_chroma_ref`) codes NO chroma transform here, so
3782 // neither symbol nor context exists.
3783 u_grid = Grid::Zero(chroma_stride * chroma_buf_h);
3784 v_grid = Grid::Zero(chroma_stride * chroma_buf_h);
3785 } else if side > 64 {
3786 // Already read inside the mu-chunk loop above -- at the 128
3787 // root a rect (128x64/64x128) block's chroma is per mu
3788 // chunk too, so this test comes BEFORE the `rect_tu` one.
3789 u_grid = if u_units.is_empty() {
3790 Grid::Zero(chroma_stride * chroma_buf_h)
3791 } else {
3792 Grid::Own(std::mem::take(&mut u_units))
3793 };
3794 v_grid = if v_units.is_empty() {
3795 Grid::Zero(chroma_stride * chroma_buf_h)
3796 } else {
3797 Grid::Own(std::mem::take(&mut v_units))
3798 };
3799 } else if lossless(fctx) {
3800 // lane-lossless2: TX_4X4 chroma units, plane-major
3801 // (libaom `read_tx_size` + `decode_token_recon_block`).
3802 let (mut uo, mut vo) = (Vec::new(), Vec::new());
3803 read_inter_chroma_lossless(
3804 y,
3805 u,
3806 v,
3807 dec,
3808 cdfs,
3809 neighbours,
3810 su,
3811 sv,
3812 at_mi,
3813 (cpx, cpy),
3814 (0, 0),
3815 (write_chroma_w, write_chroma_h),
3816 (write_chroma_w, write_chroma_h),
3817 chroma_stride,
3818 mode_for_tx,
3819 base_q_idx,
3820 &mut uo,
3821 &mut vo,
3822 fctx,
3823 )?;
3824 let zero = chroma_side * chroma_side;
3825 u_grid = if uo.is_empty() {
3826 Grid::Zero(zero)
3827 } else {
3828 Grid::Own(uo)
3829 };
3830 v_grid = if vo.is_empty() {
3831 Grid::Zero(zero)
3832 } else {
3833 Grid::Own(vo)
3834 };
3835 // Every unit stamped its own neighbour context above; the
3836 // whole-block record must not overwrite them with the
3837 // assembled grid (class `override-slot-on-one-arm`).
3838 mu_chroma = true;
3839 } else if rect_tu || (write_chroma_w, write_chroma_h) != (chroma_side, chroma_side)
3840 {
3841 // One chroma read for the whole strip at 4:2:0, two
3842 // 32-capped units at 4:4:4 -- see the helper.
3843 // lane-av1-422j: same rule as the compound arm above -- a
3844 // 4:2:2 square block's chroma plane is the
3845 // (side/2 x side) rect, so the rect reader takes it
3846 // instead of pairing a `chroma_side`-square unit with the
3847 // 4:2:0 scan.
3848 (u_grid, v_grid, rect_chroma_units) = read_inter_rect_chroma(
3849 y,
3850 u,
3851 v,
3852 dec,
3853 cdfs,
3854 neighbours,
3855 around_c,
3856 su,
3857 sv,
3858 at_mi,
3859 (cpx, cpy),
3860 (write_chroma_w, write_chroma_h),
3861 chroma_stride,
3862 mode_for_tx,
3863 &leaf_tx_types,
3864 luma_tx_type,
3865 fctx,
3866 )?;
3867 } else if ss_x(fctx) == 0 && ss_y(fctx) == 0 && chroma_side > chroma_tx {
3868 // lane-av1-444: at ss 0/0 this block's chroma plane is
3869 // `chroma_side` square while `av1_get_max_uv_txsize` caps
3870 // the units at TX_32X32 -- each plane codes FOUR TX_32X32
3871 // units, plane-major inside the block's one 64x64 mu chunk
3872 // (libaom `decode_token_recon_block`'s plane loop). Each
3873 // unit's `txb_skip_ctx` carries the +10
3874 // `get_txb_ctx_general` offset (plane block larger than the
3875 // transform); our `txb_skip_chroma_32` holds those rows at
3876 // +3, so `luma_skip_ctx: Some(3)` (lane-sb128b r3's
3877 // convention, as the 128 mu path above).
3878 let cu_tx = chroma_tx;
3879 let mu_units_n = chroma_side / cu_tx;
3880 let mut u_acc: Vec<i32> = Vec::new();
3881 let mut v_acc: Vec<i32> = Vec::new();
3882 for plane_idx in 1..3 {
3883 let (src, acc) = if plane_idx == 1 {
3884 (su, &mut u_acc)
3885 } else {
3886 (sv, &mut v_acc)
3887 };
3888 for cr in 0..mu_units_n {
3889 for cc in 0..mu_units_n {
3890 let cu_mi = (rmi + cr * (cu_tx / MI), cmi + cc * (cu_tx / MI));
3891 let cu_around = neighbours.around_mi(cu_mi, cu_tx);
3892 // `av1_get_tx_type` (blockd.h:1287-1297): an
3893 // inter block's chroma unit never codes a
3894 // `tx_type`; it reads the map cell at its OWN
3895 // position scaled back to luma
3896 // (`blk_row << ss_y`, `blk_col << ss_x`),
3897 // which is the coded type of the luma leaf
3898 // covering this unit's top-left mi cell --
3899 // `DctDct` when that leaf coded nothing
3900 // (`decodetxb.c:199-203`). The block-level
3901 // `luma_tx_type` is only the TOP-LEFT leaf's
3902 // type, so handing it to all four units
3903 // mistyped every unit sitting over a skipped
3904 // or differently-typed luma quadrant.
3905 let cu_rel_mi = (cr * (cu_tx / MI), cc * (cu_tx / MI));
3906 let cu_tx_type = covering_leaf_tx_type(&leaf_tx_types, cu_rel_mi)
3907 .unwrap_or(luma_tx_type);
3908 if covering_leaf_tx_type(&leaf_tx_types, cu_rel_mi).is_some() {
3909 hit!(CHROMA_QUAD_LEAF_TX_HITS);
3910 hit!(CHROMA_QUAD_LEAF_TX_SINGLEREF_HITS);
3911 // lane-av1f9singleland: this arm's own
3912 // non-vacuity number -- the shared
3913 // DIFFERS counter below cannot say which
3914 // arm changed an answer.
3915 if cu_tx_type != luma_tx_type {
3916 hit!(CHROMA_QUAD_LEAF_TX_DIFF_HITS);
3917 hit!(CHROMA_QUAD_LEAF_TX_SINGLEREF_DIFF_HITS);
3918 }
3919 }
3920 let cu_grid = read_inter_plane(
3921 dec,
3922 cdfs,
3923 chroma_set,
3924 scan_chroma,
3925 plane_idx,
3926 cu_around[plane_idx],
3927 mode_for_tx,
3928 y,
3929 u,
3930 v,
3931 cpx + cc * cu_tx,
3932 cpy + cr * cu_tx,
3933 cu_tx,
3934 base_q_idx,
3935 src.offset(cr * cu_tx * chroma_side + cc * cu_tx),
3936 Some(cu_tx_type),
3937 Some(3),
3938 fctx,
3939 )?
3940 .0;
3941 // Immediately, so the next unit's context sees
3942 // this one's coefficients.
3943 neighbours
3944 .record_mi_chroma(cu_mi, cu_tx, cu_tx, plane_idx, &cu_grid);
3945 hit!(CHROMA_SPLIT_TX_HITS);
3946 // lane-av1-444 c8: the unit's levels must ALSO
3947 // land in the block grid -- the block-tail mu
3948 // re-stamp slices the per-unit states back out
3949 // of it. This arm used to hand it a ZERO grid,
3950 // so the re-stamp (and `record_rect_mi`'s
3951 // whole-block write under it) painted the whole
3952 // block's chroma context cells 0 AFTER the
3953 // correct per-unit stamps above: the next block
3954 // read `txb_skip_ctx` base 0 where aom gathers
3955 // 1 (f52 mi(52,16) U all_zero, ctx 0 vs 8).
3956 let dst = mu_units(acc, chroma_side * chroma_side);
3957 for rr in 0..cu_tx {
3958 let start = (cr * cu_tx + rr) * chroma_side + cc * cu_tx;
3959 dst[start..start + cu_tx]
3960 .copy_from_slice(&cu_grid[rr * cu_tx..][..cu_tx]);
3961 }
3962 }
3963 }
3964 }
3965 mu_chroma = true;
3966 u_grid = Grid::Own(u_acc);
3967 v_grid = Grid::Own(v_acc);
3968 } else {
3969 u_grid = read_inter_plane(
3970 dec,
3971 cdfs,
3972 chroma_set,
3973 scan_chroma,
3974 1,
3975 around[1],
3976 mode_for_tx,
3977 y,
3978 u,
3979 v,
3980 cpx,
3981 cpy,
3982 chroma_side,
3983 base_q_idx,
3984 su,
3985 Some(luma_tx_type),
3986 None,
3987 fctx,
3988 )?
3989 .0;
3990 v_grid = read_inter_plane(
3991 dec,
3992 cdfs,
3993 chroma_set,
3994 scan_chroma,
3995 2,
3996 around[2],
3997 mode_for_tx,
3998 y,
3999 u,
4000 v,
4001 cpx,
4002 cpy,
4003 chroma_side,
4004 base_q_idx,
4005 sv,
4006 Some(luma_tx_type),
4007 None,
4008 fctx,
4009 )?
4010 .0;
4011 }
4012 }
4013 }
4014 } else {
4015 let Some(arm) = decode_inter_block_intra_arm(
4016 dec,
4017 cdfs,
4018 neighbours,
4019 grid,
4020 at,
4021 side,
4022 mi_cols,
4023 mi_rows,
4024 y,
4025 u,
4026 v,
4027 base_q_idx,
4028 luma_set_intra,
4029 luma_tx,
4030 scan_luma,
4031 write_w,
4032 write_h,
4033 allow_screen_content_tools,
4034 fctx,
4035 at_mi,
4036 px,
4037 py,
4038 r,
4039 c,
4040 rmi,
4041 cmi,
4042 cpx,
4043 cpy,
4044 chroma_side,
4045 chroma_stride,
4046 chroma_buf_h,
4047 chroma_set,
4048 chroma_tx,
4049 scan_chroma,
4050 strip_chroma,
4051 skip,
4052 skip_mode,
4053 mu_chroma,
4054 )?
4055 else {
4056 return Ok(());
4057 };
4058 (
4059 mode_for_tx,
4060 uv_predict_mode,
4061 block_filter,
4062 ref_frame_for_lf,
4063 globalmv_for_lf,
4064 luma_grid,
4065 u_grid,
4066 v_grid,
4067 vartx_leaves,
4068 palette_record,
4069 mu_chroma,
4070 ) = arm;
4071 }
4072 // lane-comppin r9: end-of-block checkpoint -- everything between
4073 // post_assign_mv and here (interintra, motion_mode, interp_filter, then
4074 // every plane's coeffs) is unproven; a range mismatch here vs aomdec's
4075 // equivalent post-content point (added at the matching call site)
4076 // narrows the ladder to this block's own tail instead of the next
4077 // block's partition read.
4078 if crate::envflags::env_flag!("EC_AV1_TELL") {
4079 eprintln!(
4080 "TELL mi_row={} mi_col={} label=block_end tell={} range={}",
4081 rmi,
4082 cmi,
4083 dec.debug_bitpos(),
4084 dec.debug_state().0
4085 );
4086 }
4087 // lane-inter16ab r2: the pair's chroma coefficient context spans BOTH
4088 // strips (mirroring the intra `decode_rect4_16` path), and the even
4089 // strip's own record wrote empty chroma state over those cells -- this
4090 // rewrite is what libaom's single `av1_set_contexts` call for the 8x4
4091 // (4x8) chroma unit leaves behind.
4092 // lane-av1-444interband: 4:2:0 ONLY. At ss 0/0 every strip IS its own
4093 // chroma reference (`is_chroma_reference`, no mi parity test), its own
4094 // `record_rect_mi` above already stamps exactly the strip's 16x4 (4x16)
4095 // chroma span -- libaom `av1_set_entropy_contexts`, or
4096 // `av1_reset_entropy_context` for a skipped strip (decodeframe.c:1180)
4097 // -- and the pair-form 16x8 (8x16) span anchored at `pair_mi` (== the
4098 // strip's own mi at ss 0/0) smeared the strip's state over one extra
4099 // row/col of cells: a skipped strip wrote zero over `left[r+1][1..2]`,
4100 // erasing the 16x16-intra block to its left's U/V stamps, and the strip
4101 // below read its chroma `txb_skip` off the wrong CDF row (t6 frame 1,
4102 // U unit (2,8): our base 0 vs aom's base 1, `left[2][1]` 0 vs 23 --
4103 // `INTER16_CHROMA_PAIR_HITS` fired 31x on a stream that has no pairs).
4104 let pair_chroma = strip_chroma
4105 .filter(|s| s.has_chroma)
4106 .filter(|_| ss_x(fctx) == 1 && ss_y(fctx) == 1)
4107 .map(|s| (s, neighbour_state(&u_grid), neighbour_state(&v_grid)));
4108 // lane-vert46 r1: libaom stamps the chroma entropy context PER TRANSFORM
4109 // UNIT (`av1_set_entropy_contexts` inside `decode_reconstruct_tx`'s mu-chunk
4110 // loop). Above 64 a block's chroma is two or four 32x32 units, so the
4111 // whole-block re-stamp in the record below overwrote every unit's own DC
4112 // sign and level with the TOP-LEFT unit's -- the next block then read a
4113 // stale `dc_sign_ctx` (192x256 --sb-size=128 stream: the 64x128 block at
4114 // the partial right superblock read row 1 where libaom reads row 2). The
4115 // units are collected here and re-stamped after that record, in mu-chunk
4116 // order so a later unit still wins over an earlier one.
4117 let mut mu_chroma_units: Vec<((usize, usize), usize, Grid, usize, usize)> = if mu_chroma {
4118 // lane-lossless2: the unit is TX_32X32 (one per 64x64 mu chunk)
4119 // normally and TX_4X4 on a lossless frame, where the whole chroma
4120 // plane block is a raster of them.
4121 let cu = if lossless(fctx) { 4usize } else { 32usize };
4122 let mut units = Vec::new();
4123 if lossless(fctx) {
4124 // lane-av1-llinter: a TX_4X4 chroma unit covers `4 << ss` LUMA
4125 // pixels (8, x2-per-unit, at 4:2:0; 4, x1, at 4:4:4) -- the
4126 // composed plane grid then holds a full cu-row per chroma row and
4127 // each unit's own origin sits `(cu << ss) / MI` mi cells out.
4128 // The hardcoded 4:2:0 /2-and-x2 math stamped half the plane with
4129 // double spans at 4:4:4 and corrupted the next block's
4130 // `txb_skip_ctx` (dodge2 frame 1, U unit mi (4,16) read row 11
4131 // where the oracle reads row 10).
4132 let (rows, cols) = (
4133 (write_h >> ss_y(fctx)).div_ceil(cu),
4134 (write_w >> ss_x(fctx)).div_ceil(cu),
4135 );
4136 for cr in 0..rows {
4137 for cc in 0..cols {
4138 let cu_mi = (
4139 at.0 + cr * ((cu << ss_y(fctx)) / MI),
4140 at.1 + cc * ((cu << ss_x(fctx)) / MI),
4141 );
4142 for (plane, grid) in [(1usize, &u_grid), (2usize, &v_grid)] {
4143 let mut unit = Vec::with_capacity(cu * cu);
4144 for rr in 0..cu {
4145 // lane-av1422lpf site 2: the composed grid this
4146 // replays is the block's CHROMA PLANE BLOCK at
4147 // `chroma_stride` -- the same per-axis stride
4148 // site 1 sized `u_out`/`v_out` at and the same
4149 // `read_inter_chroma_lossless` wrote them at
4150 // (`src.offset(..)` + `(oy + row) * stride + ox`).
4151 // `chroma_side` is the ENCLOSING SQUARE
4152 // (`max(side >> ss_x, side >> ss_y)`), which is
4153 // the plane block's own width only when
4154 // `ss_x == ss_y`: at 4:2:2 it is the full luma
4155 // side, twice a strip block's plane-block width,
4156 // and the last unit row ran off the end
4157 // (`range end index 68 out of range for slice of
4158 // length 64`, W_intrabc frame 1).
4159 //
4160 // At 4:2:0 (`chroma_stride == chroma_side ==
4161 // side >> 1`) and 4:4:4 (`side`) the two are the
4162 // same number and this line is unchanged.
4163 let start = (cr * cu + rr) * chroma_stride + cc * cu;
4164 unit.extend_from_slice(&grid[start..start + cu]);
4165 }
4166 units.push((
4167 cu_mi,
4168 plane,
4169 Grid::Own(unit),
4170 cu << ss_x(fctx),
4171 cu << ss_y(fctx),
4172 ));
4173 }
4174 }
4175 }
4176 } else {
4177 // lane-av1-444 c8: a 64x64 mu chunk's chroma plane block is
4178 // (64 >> ss) square -- ONE TX_32X32 unit per chunk at 4:2:0 but
4179 // FOUR at ss 0/0 (`av1_get_max_uv_txsize` caps the unit at 32
4180 // either way). Enumerate every unit at its own mi origin, its
4181 // own (32 << ss)-px entropy-cell span, and its own 32x32
4182 // quadrant of the assembled plane grid; stamping one quadrant's
4183 // state over the whole chunk desynced the next block's
4184 // `txb_skip_ctx` (f52 mi(52,16) U read base 0, aom 1).
4185 let (ur, uc) = (
4186 ((64usize) >> ss_y(fctx)) / cu,
4187 ((64usize) >> ss_x(fctx)) / cu,
4188 );
4189 let (rows, cols) = ((write_h / 64).max(1), (write_w / 64).max(1));
4190 for cr in 0..rows {
4191 for cc in 0..cols {
4192 for kr in 0..ur {
4193 for kc in 0..uc {
4194 let cu_mi = (
4195 at.0 + (cr * 64 + kr * (cu << ss_y(fctx))) / MI,
4196 at.1 + (cc * 64 + kc * (cu << ss_x(fctx))) / MI,
4197 );
4198 let cy = cr * ((64usize) >> ss_y(fctx)) + kr * cu;
4199 let cx = cc * ((64usize) >> ss_x(fctx)) + kc * cu;
4200 for (plane, grid) in [(1usize, &u_grid), (2usize, &v_grid)] {
4201 let mut unit = Vec::with_capacity(cu * cu);
4202 for rr in 0..cu {
4203 let start = (cy + rr) * chroma_side + cx;
4204 unit.extend_from_slice(&grid[start..start + cu]);
4205 }
4206 units.push((
4207 cu_mi,
4208 plane,
4209 Grid::Own(unit),
4210 cu << ss_x(fctx),
4211 cu << ss_y(fctx),
4212 ));
4213 }
4214 }
4215 }
4216 }
4217 }
4218 }
4219 units
4220 } else {
4221 Vec::new()
4222 };
4223 // The rect_tu strips' true units (`read_inter_rect_chroma`'s return):
4224 // replayed by the same loop below, after the whole-block record.
4225 mu_chroma_units.extend(rect_chroma_units);
4226 // libaom `av1_reset_entropy_context` / `av1_set_entropy_contexts`:
4227 // `nplanes = 1 + (num_planes - 1) * xd->is_chroma_ref` -- a block that is
4228 // NOT the chroma reference of its 4:2:0 pair codes no chroma transform at
4229 // all, so it leaves its neighbours' CHROMA coefficient contexts exactly as
4230 // it found them. `record_rect_mi`/`record_split_luma_rect_mi` below write
4231 // all three planes unconditionally, and for such a strip the chroma grids
4232 // are all-zero (see the `!has_chroma` arm above) -- that wiped the real
4233 // left-neighbour chroma state, so the PAIR's own `all_zero` then read
4234 // `txb_skip_ctx` one row low (lane-t900 r3, 10-bit 1920x792 stream,
4235 // mi(40,220): base 1 where libaom gathers 2). Snapshot and restore is
4236 // equivalent to not writing them, without threading a flag through both
4237 // recorders.
4238 // lane-t900 r22: every block stamps its palette state (all-zero for an
4239 // inter block, real for a palette one) into the mi-granular neighbour
4240 // bands, so the next intra block's `av1_get_palette_mode_ctx` and colour
4241 // cache see exactly what libaom's mode-info grid holds.
4242 record_strip_palette(
4243 neighbours,
4244 at,
4245 write_w,
4246 write_h,
4247 palette_record.0,
4248 palette_record.1,
4249 palette_record.2,
4250 palette_record.3,
4251 );
4252 let saved_chroma_ctx = (!has_chroma).then(|| {
4253 hit!(NON_CHROMA_REF_CTX_SKIP_HITS);
4254 let (w_mi, h_mi) = (write_w / MI, write_h / MI);
4255 let left: Vec<[Neighbour; 2]> = (0..h_mi)
4256 .filter_map(|k| neighbours.left.get(at.0 + k).map(|s| [s[1], s[2]]))
4257 .collect();
4258 let above: Vec<[Neighbour; 2]> = (0..w_mi)
4259 .filter_map(|k| neighbours.above.get(at.1 + k).map(|s| [s[1], s[2]]))
4260 .collect();
4261 (left, above)
4262 });
4263 if vartx_leaves.is_some() {
4264 // lane-t900 r17 root cause (class early-return-skips-tail): the
4265 // "plane 0 is already correct per transform unit" premise below holds
4266 // only when the per-TU luma loop RAN. A SKIP inter block takes the
4267 // `if skip` arm above, which reconstructs straight from the predictor
4268 // and never enters that loop -- yet `read_block_tx_size` still hands
4269 // back four TX_64X64 leaves for a 128 root under `TX_MODE_LARGEST`
4270 // (and for any block under it), so `vartx_leaves` is `Some` and the
4271 // luma coefficient contexts were left at the PREVIOUS block's values.
4272 // libaom calls `av1_reset_entropy_context` for every skipped block,
4273 // zeroing all three planes over its own footprint; `luma_grid` is the
4274 // all-zero grid on this arm, so publishing it is exactly that reset.
4275 // Witnessed on a 10-bit 512x512 --sb-size=128 stream: the 128x128
4276 // block at mi(32,64) of decode-order frame 1 read `txb_skip_ctx` 2
4277 // where aomdec reads 1, because the skipped 128x128 block at
4278 // mi(32,32) never cleared `left[32..48]`.
4279 if skip {
4280 hit!(SKIP_VARTX_LUMA_RESET_HITS);
4281 neighbours.record_mi_luma_rect(at, write_w, write_h, &luma_grid);
4282 }
4283 // Plane 0 is already correct per transform unit
4284 // ([`Neighbours::record_mi_luma`] above); this writes everything else
4285 // [`Neighbours::record_rect`] would.
4286 //
4287 // lane-sbab r1: this used to pass `side, side` on the premise that "a
4288 // var-tx block is always square". That stopped being true the moment
4289 // the SB-level rect strips landed (lane-inter4): a 64x32 / 32x64
4290 // strip keeps `side = SB` for syntax but is NOT square, and stamping
4291 // its neighbour bands 64x64 wrote `above_side_mi = 64` where libaom's
4292 // `update_partition_context` writes `partition_context_lookup[
4293 // BLOCK_32X64]`. The next superblock row then read the partition
4294 // context one value low (ctx 2 where aomdec says 3) and desynced.
4295 // The true footprint is `write_w`/`write_h`, exactly as the
4296 // non-var-tx arm below uses.
4297 // Plane 0 is already correct per transform unit
4298 // ([`Neighbours::record_mi_luma`] above); this writes everything else
4299 // [`Neighbours::record_rect`] would. lane-vert46 r1: the block's OWN
4300 // width/height, not `side` -- var-tx is no longer square-only (the 128
4301 // root's HORZ/VERT arm decodes 128x64 / 64x128 var-tx blocks), and
4302 // `side` there stamped `above_side`/`left_side` = 128, so the NEXT
4303 // superblock's 128-root partition context read 16 where libaom reads 19.
4304 neighbours.record_split_luma_rect_mi(
4305 at,
4306 write_w,
4307 write_h,
4308 mode_for_tx,
4309 uv_predict_mode,
4310 [&u_grid, &v_grid],
4311 );
4312 } else {
4313 neighbours.record_rect_mi(
4314 at,
4315 write_w,
4316 write_h,
4317 mode_for_tx,
4318 uv_predict_mode,
4319 &[&luma_grid[..], &u_grid[..], &v_grid[..]],
4320 );
4321 }
4322 if let Some((left, above)) = saved_chroma_ctx {
4323 for (k, v) in left.into_iter().enumerate() {
4324 if let Some(slot) = neighbours.left.get_mut(at.0 + k) {
4325 slot[1] = v[0];
4326 slot[2] = v[1];
4327 }
4328 }
4329 for (k, v) in above.into_iter().enumerate() {
4330 if let Some(slot) = neighbours.above.get_mut(at.1 + k) {
4331 slot[1] = v[0];
4332 slot[2] = v[1];
4333 }
4334 }
4335 }
4336 if let Some((s, u_state, v_state)) = pair_chroma {
4337 // lane-av1422ctxgather: at 4:2:2 (ss 1,0) a horz strip is its own
4338 // chroma reference -- no pair merge ever happened, so the write
4339 // covers the strip's own 8x4: ONE luma-mi left row (libaom's
4340 // `av1_set_contexts` for the TX_8X4 at ss_y 0 stamps
4341 // `tx_size_high_unit` = 1 cell). The 4:2:0 pair's two-row (16, 8)
4342 // span would overwrite the NEXT row's leaf-stamped level state
4343 // (measured: t422 frame 1, the strip at mi(8,4) zeroed
4344 // left[9][1] under the mi(8,2) 8x8's level 5 before the mi(9,4)
4345 // strip's own all_zero read it one ctx row low).
4346 let (pw, ph) = if s.horz {
4347 if ss_x(fctx) == 1 && ss_y(fctx) == 0 {
4348 (16usize, 4usize)
4349 } else {
4350 (16usize, 8usize)
4351 }
4352 } else {
4353 (8, 16)
4354 };
4355 let round_up_even = |n: usize| n.div_ceil(2) * 2;
4356 let (bound_h, bound_w) = (
4357 round_up_even(neighbours.mi_rows),
4358 round_up_even(neighbours.mi_cols),
4359 );
4360 for cell in 0..(ph / MI) {
4361 if s.pair_mi.0 + cell < bound_h
4362 && let Some(slot) = neighbours.left.get_mut(s.pair_mi.0 + cell)
4363 {
4364 slot[1] = u_state;
4365 slot[2] = v_state;
4366 }
4367 }
4368 for cell in 0..(pw / MI) {
4369 if s.pair_mi.1 + cell < bound_w
4370 && let Some(slot) = neighbours.above.get_mut(s.pair_mi.1 + cell)
4371 {
4372 slot[1] = u_state;
4373 slot[2] = v_state;
4374 }
4375 }
4376 neighbours.record_uv_mode_mi(s.pair_mi.0, s.pair_mi.1, pw / MI, ph / MI, uv_predict_mode);
4377 hit!(INTER16_CHROMA_PAIR_HITS);
4378 }
4379 // lane-av1-444 c8: each unit stamps its OWN span (32 << ss px at
4380 // TX_32X32, 8 px lossless) -- a 64x64 mu chunk at ss 0/0 holds four
4381 // TX_32X32 units, so the old whole-chunk 64-px span smeared one unit's
4382 // state over its three neighbours' cells too.
4383 for (cu_mi, plane, unit, span_w, span_h) in &mu_chroma_units {
4384 neighbours.record_mi_chroma(*cu_mi, *span_w, *span_h, *plane, unit);
4385 }
4386 neighbours.record_inter_rect_mi(
4387 at,
4388 write_w / MI,
4389 write_h / MI,
4390 skip,
4391 is_inter,
4392 ref_frame_for_lf,
4393 block_filter,
4394 skip_mode,
4395 );
4396 if let Some((ref1, group_idx, idx)) = compound_ctx {
4397 neighbours.record_compound_ctx_rect_mi(
4398 at,
4399 write_w / MI,
4400 write_h / MI,
4401 ref1,
4402 group_idx,
4403 idx,
4404 );
4405 }
4406 neighbours.fill_skip_grid_rect((rmi, cmi), write_w / MI, write_h / MI, skip);
4407 neighbours.fill_lf_grid_rect(
4408 (rmi, cmi),
4409 write_w / MI,
4410 write_h / MI,
4411 // lane-rect r2: the strip's true TX_32X16/TX_16X32 dims -- the
4412 // single-scalar corner-cut deblocked the strip seam with the wrong
4413 // edge length (1-px chroma drift on rect-flake-1 f17+).
4414 write_w as u8,
4415 write_h as u8,
4416 // ref_grid's packed convention (lf_level): sign carries GLOBALMV,
4417 // whose mode_lf_lut row is 0, not the 1 every other inter mode gets.
4418 if globalmv_for_lf {
4419 -ref_frame_for_lf
4420 } else {
4421 ref_frame_for_lf
4422 },
4423 fctx,
4424 );
4425 // lane-txselect: a var-tx block's deblock edges follow its own transform
4426 // units, not the block -- for LUMA only (lane-golomb r6): the chroma
4427 // grids keep the block's own `av1_get_max_uv_txsize`, which is what
4428 // libaom's `get_transform_size` reads for `plane != AOM_PLANE_Y`.
4429 // lane-sb128c r4: libaom `set_one_param_for_line_luma` suppresses every
4430 // edge that is a TU edge but not a PU edge when
4431 // `curr_skipped = mbmi->skip_txfm && is_inter_block(mbmi)`, so a SKIP
4432 // inter block's internal transform edges are never filtered even though
4433 // `get_transform_size` still reports TX_64X64 for it. The only block that
4434 // reaches here skipped is the 128 root (`read_block_tx_size` hands back
4435 // four TX_64X64 leaves for `side > 64` regardless of `skip`); leaving its
4436 // block-level 128 publication in place is exactly that suppression.
4437 // lane-sb128c r6: narrowed to the 128 root. Below it `read_block_tx_size`
4438 // hands a skipped inter block a single leaf equal to the block, so the
4439 // per-leaf write is the block-level one and this branch is a no-op either
4440 // way; only the 128 root gets four TX_64X64 leaves regardless of `skip`.
4441 let skip_inter_128 = skip && is_inter && write_w.max(write_h) > 64;
4442 if skip_inter_128 {
4443 neighbours.suppress_internal_lf_edges((rmi, cmi), write_w / MI, write_h / MI, fctx);
4444 }
4445 if let Some(leaves) = (!skip_inter_128).then_some(()).and(vartx_leaves.as_ref()) {
4446 for &(row, col, tw, th) in leaves {
4447 neighbours.fill_lf_grid_leaf_luma(
4448 (at_mi.0 + row, at_mi.1 + col),
4449 tw / MI,
4450 th / MI,
4451 tw as u8,
4452 th as u8,
4453 );
4454 }
4455 }
4456 Ok(())
4457}