forge
Explore
Log in

Don't you dare go Hollow

ec-av1: continue 4:4:4 rect chroma port

Browse files at this commit

8b574e66322f6f3c6f18fe484fc10b9a728a2170Ahmet Kaan GümüşParent: a236cd3
crates/ec-av1/src/decode.rsmodified+487 -154
@@ -8285,7 +8285,14 @@ impl Neighbours {
8285 /// coded a smooth `uv_mode` -- libaom's `intra_edge_filter_type` for8285 /// coded a smooth `uv_mode` -- libaom's `intra_edge_filter_type` for
8286 /// planes 1/2. Falls back to the coarse [`SUB`] slot on whichever axis no8286 /// planes 1/2. Falls back to the coarse [`SUB`] slot on whichever axis no
8287 /// block has recorded the exact neighbouring mi.8287 /// block has recorded the exact neighbouring mi.
8288- fn smooth_uv_neighbour(&self, mi_r: usize, mi_c: usize, r: usize, c: usize) -> bool {8288+ fn smooth_uv_neighbour(
8289+ &self,
8290+ mi_r: usize,
8291+ mi_c: usize,
8292+ r: usize,
8293+ c: usize,
8294+ fctx: &crate::decode::FrameCtx,
8295+ ) -> bool {
8289 // lane-kf900 r2: libaom reads the chroma neighbours off the CHROMA8296 // lane-kf900 r2: libaom reads the chroma neighbours off the CHROMA
8290 // REFERENCE block, not off the mi that carries the chroma syntax.8297 // REFERENCE block, not off the mi that carries the chroma syntax.
8291 // `set_mi_row_col` (`blockd.h`) takes8298 // `set_mi_row_col` (`blockd.h`) takes
@@ -8302,7 +8309,14 @@ impl Neighbours {
8302 // false for type 1, TRUE for type 0) and upsampled an edge libaom8309 // false for type 1, TRUE for type 0) and upsampled an edge libaom
8303 // leaves alone -- +1 on rows 2/3 of that block and every chroma block8310 // leaves alone -- +1 on rows 2/3 of that block and every chroma block
8304 // predicting from it (class `context-read-from-one-cell`).8311 // predicting from it (class `context-read-from-one-cell`).
8305- self.smooth_uv_neighbour_unsnapped(mi_r & !1, mi_c & !1, r, c)8312+ // lane-av1-444: the snap is the SEQUENCE's subsampling, hardcoded
8313+ // `& !1` before -- at subsampling 0/0 libaom's `mi_row & 0` snaps
8314+ // nothing, and the hardcoded snap read a 4x4 leaf's smooth
8315+ // neighbour two mi away (frame 0 of `av1-profile1-444.ivf`, leaf
8316+ // mi(5,15): left neighbour SMOOTH_V at mi(5,14), read at mi(4,13)).
8317+ let snap_y = (1usize << ss_y(fctx)) - 1;
8318+ let snap_x = (1usize << ss_x(fctx)) - 1;
8319+ self.smooth_uv_neighbour_unsnapped(mi_r & !snap_y, mi_c & !snap_x, r, c)
8306 }8320 }
8307 8321
8308 /// [`Self::smooth_uv_neighbour`] without the chroma-reference snap, i.e.8322 /// [`Self::smooth_uv_neighbour`] without the chroma-reference snap, i.e.
@@ -9113,17 +9127,21 @@ pub(crate) fn palette_onscreen_dims(
9113 }9127 }
9114 9128
9115 /// [`palette_onscreen`] carried into the chroma plane: `>> subsampling` plus9129 /// [`palette_onscreen`] carried into the chroma plane: `>> subsampling` plus
9116-/// the SAME sub-8 bump the caller's own `(bw / 2).max(4)` applies to the block9130+/// the SAME sub-8 bump the caller's own `(bw >> ss).max(4)` applies to the
9117-/// dimensions (`is_chroma_sub8_x/y`, blockd.h:1533) -- both must move together9131+/// block dimensions (`is_chroma_sub8_x/y`, blockd.h:1533) -- both must move
9118-/// or a cut 8-wide block reads its chroma map at the wrong width.9132+/// together or a cut 8-wide block reads its chroma map at the wrong width.
9133+/// lane-av1-444: the shifts are the sequence's own subsampling (a 4:4:4
9134+/// intra-in-inter palette read its chroma map at the 4:2:0 extent and
9135+/// panicked feeding the 4:2:0-sized prediction to a full-size reconstruct).
9119 pub(crate) fn palette_onscreen_uv(9136 pub(crate) fn palette_onscreen_uv(
9120 luma: (usize, usize),9137 luma: (usize, usize),
9121 on: (usize, usize),9138 on: (usize, usize),
9122 chroma: (usize, usize),9139 chroma: (usize, usize),
9140+ ss: (usize, usize),
9123 ) -> (usize, usize) {9141 ) -> (usize, usize) {
9124 (9142 (
9125- (on.0 >> 1) + (chroma.0 - (luma.0 >> 1)),9143+ (on.0 >> ss.0) + (chroma.0 - (luma.0 >> ss.0)),
9126- (on.1 >> 1) + (chroma.1 - (luma.1 >> 1)),9144+ (on.1 >> ss.1) + (chroma.1 - (luma.1 >> ss.1)),
9127 )9145 )
9128 }9146 }
9129 9147
@@ -9479,7 +9497,7 @@ fn read_intra_mode_rect(
9479 // axis to 2 read HALF the colour-index symbols of a 1:4 strip's9497 // axis to 2 read HALF the colour-index symbols of a 1:4 strip's
9480 // chroma palette and desynced the tile from there.9498 // chroma palette and desynced the tile from there.
9481 let (cw, ch) = ((bw >> ss_x(fctx)).max(4), (bh >> ss_y(fctx)).max(4));9499 let (cw, ch) = ((bw >> ss_x(fctx)).max(4), (bh >> ss_y(fctx)).max(4));
9482- let on = palette_onscreen_uv((bw, bh), palette_onscreen(mi_r, mi_c, bw, bh, fctx), (cw, ch));9500+ let on = palette_onscreen_uv((bw, bh), palette_onscreen(mi_r, mi_c, bw, bh, fctx), (cw, ch), (ss_x(fctx), ss_y(fctx)));
9483 let map = decode_color_index_map_wh(dec, cdfs, n, cw, ch, on, true);9501 let map = decode_color_index_map_wh(dec, cdfs, n, cw, ch, on, true);
9484 hit!(PALETTE_UV_HITS);9502 hit!(PALETTE_UV_HITS);
9485 PaletteUv { size: n, u_colors, v_colors, map }9503 PaletteUv { size: n, u_colors, v_colors, map }
@@ -10844,7 +10862,7 @@ fn decode_intra_rect_in_inter(
10844 });10862 });
10845 let palette_uv = palette_uv_pending.map(|(n, u_colors, v_colors)| {10863 let palette_uv = palette_uv_pending.map(|(n, u_colors, v_colors)| {
10846 let (cw, ch) = ((bw >> ss_x(fctx)).max(4), (bh >> ss_y(fctx)).max(4));10864 let (cw, ch) = ((bw >> ss_x(fctx)).max(4), (bh >> ss_y(fctx)).max(4));
10847- let on = palette_onscreen_uv((bw, bh), palette_onscreen(mi_r, mi_c, bw, bh, fctx), (cw, ch));10865+ let on = palette_onscreen_uv((bw, bh), palette_onscreen(mi_r, mi_c, bw, bh, fctx), (cw, ch), (ss_x(fctx), ss_y(fctx)));
10848 let map = decode_color_index_map_wh(dec, cdfs, n, cw, ch, on, true);10866 let map = decode_color_index_map_wh(dec, cdfs, n, cw, ch, on, true);
10849 hit!(PALETTE_UV_HITS);10867 hit!(PALETTE_UV_HITS);
10850 hit!(INTRA_IN_INTER_PALETTE_UV_HITS);10868 hit!(INTRA_IN_INTER_PALETTE_UV_HITS);
@@ -10868,7 +10886,7 @@ fn decode_intra_rect_in_inter(
10868 if smooth_neighbor {10886 if smooth_neighbor {
10869 hit!(SMOOTH_LUMA_HITS);10887 hit!(SMOOTH_LUMA_HITS);
10870 }10888 }
10871- let smooth_neighbor_uv = neighbours.smooth_uv_neighbour(mi_r, mi_c, r, c);10889+ let smooth_neighbor_uv = neighbours.smooth_uv_neighbour(mi_r, mi_c, r, c, fctx);
10872 let depth = if fctx.tx_select_inter.with(std::cell::Cell::get) {10890 let depth = if fctx.tx_select_inter.with(std::cell::Cell::get) {
10873 // The frame's own `TXFM_CONTEXT` bands -- [`tx_size_context_txfm_rect`].10891 // The frame's own `TXFM_CONTEXT` bands -- [`tx_size_context_txfm_rect`].
10874 let ctx = tx_size_context_txfm_rect(neighbours, (mi_r, mi_c), bw.min(64), bh.min(64), fctx);10892 let ctx = tx_size_context_txfm_rect(neighbours, (mi_r, mi_c), bw.min(64), bh.min(64), fctx);
@@ -11054,7 +11072,7 @@ fn decode_block_rect(
11054 // (spec `get_intra_edge_filter_type`) reads the CHROMA neighbour's11072 // (spec `get_intra_edge_filter_type`) reads the CHROMA neighbour's
11055 // `uv_mode`, not the luma one.11073 // `uv_mode`, not the luma one.
11056 let smooth_neighbor_uv =11074 let smooth_neighbor_uv =
11057- neighbours.smooth_uv_neighbour(r * (SUB / MI), c * (SUB / MI), r, c);11075+ neighbours.smooth_uv_neighbour(r * (SUB / MI), c * (SUB / MI), r, c, fctx);
11058 // lane-rectsplit r1: `predict_filter_intra` now takes the block's own11076 // lane-rectsplit r1: `predict_filter_intra` now takes the block's own
11059 // `bw`x`bh` (`av1_filter_intra_predictor_c` walks its 4x2 patches over a11077 // `bw`x`bh` (`av1_filter_intra_predictor_c` walks its 4x2 patches over a
11060 // rectangle just as happily), so a `use_filter_intra` strip at this level11078 // rectangle just as happily), so a `use_filter_intra` strip at this level
@@ -11590,7 +11608,7 @@ fn decode_leaf_rect(
11590 // lane-cfl r1, same defect as [`decode_leaf_8x8`]: chroma's intra-edge11608 // lane-cfl r1, same defect as [`decode_leaf_8x8`]: chroma's intra-edge
11591 // filter type is the NEIGHBOUR's `uv_mode` (libaom `get_filt_type`), not11609 // filter type is the NEIGHBOUR's `uv_mode` (libaom `get_filt_type`), not
11592 // a constant `false`.11610 // a constant `false`.
11593- let smooth_neighbor_uv = neighbours.smooth_uv_neighbour(leaf_mi.0, leaf_mi.1, r, c);11611+ let smooth_neighbor_uv = neighbours.smooth_uv_neighbour(leaf_mi.0, leaf_mi.1, r, c, fctx);
11594 let depth = if tx_select {11612 let depth = if tx_select {
11595 // lane-t900 r2 (sibling sweep): the in-inter `TXFM_CONTEXT` band read11613 // lane-t900 r2 (sibling sweep): the in-inter `TXFM_CONTEXT` band read
11596 // every other rect reader now does -- a no-op on a key frame.11614 // every other rect reader now does -- a no-op on a key frame.
@@ -11918,7 +11936,7 @@ fn decode_block_rect4(
11918 // edge-filter strength was read from the wrong neighbour (measured:11936 // edge-filter strength was read from the wrong neighbour (measured:
11919 // seed 43, the last 8x32 strip at luma (152,32), chroma-only mismatch11937 // seed 43, the last 8x32 strip at luma (152,32), chroma-only mismatch
11920 // with luma bit-exact).11938 // with luma bit-exact).
11921- let smooth_neighbor_uv = neighbours.smooth_uv_neighbour(mi_r, mi_c, r, c);11939+ let smooth_neighbor_uv = neighbours.smooth_uv_neighbour(mi_r, mi_c, r, c, fctx);
11922 if filter_intra.is_some() {11940 if filter_intra.is_some() {
11923 hit!(FILTER_INTRA_RECT_HITS);11941 hit!(FILTER_INTRA_RECT_HITS);
11924 if bw > bh {11942 if bw > bh {
@@ -12714,7 +12732,7 @@ fn decode_rect4_16_strip(
12714 // `av1_use_intra_edge_upsample` on (blk_wh 12 <= 16) for a12732 // `av1_use_intra_edge_upsample` on (blk_wh 12 <= 16) for a
12715 // directional chroma strip, moving 4 samples by 1.12733 // directional chroma strip, moving 4 samples by 1.
12716 let smooth_neighbor_uv =12734 let smooth_neighbor_uv =
12717- neighbours.smooth_uv_neighbour(pair_mi.0, pair_mi.1, r, c);12735+ neighbours.smooth_uv_neighbour(pair_mi.0, pair_mi.1, r, c, fctx);
12718 if smooth_neighbor_uv != neighbours.smooth_uv_neighbour_unsnapped(lmi.0, lmi.1, r, c)12736 if smooth_neighbor_uv != neighbours.smooth_uv_neighbour_unsnapped(lmi.0, lmi.1, r, c)
12719 {12737 {
12720 hit!(RECT4_16_UV_PAIR_FILT_HITS);12738 hit!(RECT4_16_UV_PAIR_FILT_HITS);
@@ -12951,7 +12969,7 @@ fn decode_block_rect64(
12951 hit!(SMOOTH_LUMA_HITS);12969 hit!(SMOOTH_LUMA_HITS);
12952 }12970 }
12953 let smooth_neighbor_uv =12971 let smooth_neighbor_uv =
12954- neighbours.smooth_uv_neighbour(r * (SUB / MI), c * (SUB / MI), r, c);12972+ neighbours.smooth_uv_neighbour(r * (SUB / MI), c * (SUB / MI), r, c, fctx);
12955 if filter_intra.is_some() {12973 if filter_intra.is_some() {
12956 // `filter_intra_size_class_rect` already returns `None` for both12974 // `filter_intra_size_class_rect` already returns `None` for both
12957 // `(64, 32)` and `(32, 64)` (`av1_filter_intra_allowed_bsize` caps at12975 // `(64, 32)` and `(32, 64)` (`av1_filter_intra_allowed_bsize` caps at
@@ -13730,6 +13748,7 @@ fn read_intra_mode(
13730 (side, side),13748 (side, side),
13731 palette_onscreen(mi_r, mi_c, side, side, fctx),13749 palette_onscreen(mi_r, mi_c, side, side, fctx),
13732 (side >> ss_x(fctx), side >> ss_y(fctx)),13750 (side >> ss_x(fctx), side >> ss_y(fctx)),
13751+ (ss_x(fctx), ss_y(fctx)),
13733 );13752 );
13734 let map = decode_color_index_map(dec, cdfs, n, side >> ss_x(fctx), on, true);13753 let map = decode_color_index_map(dec, cdfs, n, side >> ss_x(fctx), on, true);
13735 hit!(PALETTE_UV_HITS);13754 hit!(PALETTE_UV_HITS);
@@ -14252,6 +14271,13 @@ impl PlaneBuf<'_> {
14252 buf14271 buf
14253 } else {14272 } else {
14254 let (above, left, corner) = self.edges(x, y, side, reach);14273 let (above, left, corner) = self.edges(x, y, side, reach);
14274+ if crate::envflags::env_flag!("EC_DEBUG_EDGES") {
14275+ eprintln!(
14276+ "EDGES_SQ x={x} y={y} side={side} mode={mode} above={:?} left={:?} corner={corner:?}",
14277+ above.as_deref(),
14278+ left.as_deref(),
14279+ );
14280+ }
14255 let mut prediction = vec![0u16; side * side];14281 let mut prediction = vec![0u16; side * side];
14256 if let Some(fi_mode) = filter_intra {14282 if let Some(fi_mode) = filter_intra {
14257 crate::intra::predict_filter_intra(14283 crate::intra::predict_filter_intra(
@@ -14571,7 +14597,7 @@ fn decode_block(
14571 hit!(SMOOTH_LUMA_HITS);14597 hit!(SMOOTH_LUMA_HITS);
14572 }14598 }
14573 let smooth_neighbor_uv =14599 let smooth_neighbor_uv =
14574- neighbours.smooth_uv_neighbour(r * (SUB / MI), c * (SUB / MI), r, c);14600+ neighbours.smooth_uv_neighbour(r * (SUB / MI), c * (SUB / MI), r, c, fctx);
14575 if crate::envflags::env_flag!("EC_AV1_TRACE") {14601 if crate::envflags::env_flag!("EC_AV1_TRACE") {
14576 eprintln!(14602 eprintln!(
14577 "TRACE block px={px} py={py} side={side} mode={mode} uv_mode={uv_mode} \14603 "TRACE block px={px} py={py} side={side} mode={mode} uv_mode={uv_mode} \
@@ -15448,7 +15474,7 @@ fn decode_block_128rect(
15448 if smooth_neighbor {15474 if smooth_neighbor {
15449 hit!(SMOOTH_LUMA_HITS);15475 hit!(SMOOTH_LUMA_HITS);
15450 }15476 }
15451- let smooth_neighbor_uv = neighbours.smooth_uv_neighbour(mi_r, mi_c, r, c);15477+ let smooth_neighbor_uv = neighbours.smooth_uv_neighbour(mi_r, mi_c, r, c, fctx);
15452 // spec 5.11.16 `read_tx_size`: read for every intra block, skipped or15478 // spec 5.11.16 `read_tx_size`: read for every intra block, skipped or
15453 // not. `bsize_to_tx_size_cat(BLOCK_128X64)` is 3 (see this fn's doc).15479 // not. `bsize_to_tx_size_cat(BLOCK_128X64)` is 3 (see this fn's doc).
15454 // lane-lossless128: a LOSSLESS frame codes no tx-size symbol at all --15480 // lane-lossless128: a LOSSLESS frame codes no tx-size symbol at all --
@@ -15812,7 +15838,7 @@ fn decode_leaf8(
15812 // The `uv_mode` neighbours are mi-exact (`uv_mode_col`/`uv_mode_row`),15838 // The `uv_mode` neighbours are mi-exact (`uv_mode_col`/`uv_mode_row`),
15813 // with the coarse [`SUB`] slot as the fallback when no block recorded15839 // with the coarse [`SUB`] slot as the fallback when no block recorded
15814 // that exact mi cell.15840 // that exact mi cell.
15815- let smooth_neighbor_uv = neighbours.smooth_uv_neighbour(leaf_mi.0, leaf_mi.1, r, c);15841+ let smooth_neighbor_uv = neighbours.smooth_uv_neighbour(leaf_mi.0, leaf_mi.1, r, c, fctx);
15816 if smooth_neighbor {15842 if smooth_neighbor {
15817 hit!(SMOOTH_LUMA_HITS);15843 hit!(SMOOTH_LUMA_HITS);
15818 }15844 }
@@ -16531,6 +16557,194 @@ fn read_intra_mode_sub8(
16531 }16557 }
16532 Ok((skip, mode, angle_delta_y, chroma, filter_intra, None))16558 Ok((skip, mode, angle_delta_y, chroma, filter_intra, None))
16533 }16559 }
16560+/// 4:4:4 sub-8x8 key-frame leaf chroma (lane-av1-444). At subsampling 0/0
16561+/// `is_chroma_reference` (av1_common_int.h:1459) is unconditional, so EVERY
16562+/// 4x4 / 4x8 / 8x4 leaf codes its own full-resolution chroma unit right
16563+/// after its own luma (`read_intra_mode_sub8` already read this leaf's own
16564+/// `uv_mode`), not the one shared 4x4 unit [`decode_leaf_split4`] /
16565+/// [`decode_leaf_rect8`]'s group tails decode at 4:2:0. Mirrors those tails
16566+/// arm for arm at leaf scale: a skipped leaf keeps its CfL contribution, an
16567+/// intrabc leaf predicts a frame copy at its DV with the luma `tx_type`
16568+/// inherited through [`INTRABC_CHROMA_TX`] (the caller arms it right after
16569+/// the luma read). The chroma position, reach and entropy-context span are
16570+/// the leaf's own (ss 0/0); the 4x8/8x4 unit reads the same
16571+/// `ChromaRect8x4` tables [`decode_rect_split`] proves for that shape.
16572+/// Records the leaf's own `uv_mode` map entry and chroma above/left state,
16573+/// and returns the (u, v) grids for the caller's coarse slots.
16574+#[allow(clippy::too_many_arguments)]
16575+fn sub8_leaf_chroma444(
16576+ dec: &mut SymbolDecoder,
16577+ cdfs: &mut Cdfs,
16578+ neighbours: &mut Neighbours,
16579+ lmi: (usize, usize),
16580+ bw: usize,
16581+ bh: usize,
16582+ uv_mode: usize,
16583+ alpha: Option<(i32, i32)>,
16584+ skip: bool,
16585+ intrabc_dv: Option<(i32, i32)>,
16586+ y: &PlaneBuf<'static>,
16587+ u: &mut PlaneBuf<'static>,
16588+ v: &mut PlaneBuf<'static>,
16589+ scan4: &[u16],
16590+ base_q_idx: u8,
16591+ smooth_neighbor_uv: bool,
16592+ fctx: &crate::decode::FrameCtx,
16593+) -> Result<(Grid, Grid)> {
16594+ let uv_predict_mode = if uv_mode == UV_CFL_PRED { DC_PRED } else { uv_mode };
16595+ let (w_mi, h_mi) = (bw / MI, bh / MI);
16596+ neighbours.record_uv_mode_mi(lmi.0, lmi.1, w_mi, h_mi, uv_predict_mode);
16597+ let (px, py) = (lmi.1 * MI, lmi.0 * MI);
16598+ let square = (bw, bh) == (4, 4);
16599+ // `Reach::of_rect` has no 4x4 table (`rect_reach_tables` starts at 4x8);
16600+ // the square unit takes the leaf's own `Reach::of`, as its luma does.
16601+ let reach = if square {
16602+ Reach::of(4, px, py, y.width, y.height, fctx)
16603+ } else {
16604+ Reach::of_rect(bw, bh, px, py, y.width, y.height, fctx)
16605+ };
16606+ let grids = if skip {
16607+ let ac = alpha.map(|_| cfl_src_rect(px, py, bw, bh));
16608+ if square {
16609+ push_intra(1, px, py, 4, uv_predict_mode, 0, reach, &ZERO_RESIDUAL[..16], alpha.zip(ac).map(|((au, _), ac)| (au, ac)), None, smooth_neighbor_uv, fctx);
16610+ push_intra(2, px, py, 4, uv_predict_mode, 0, reach, &ZERO_RESIDUAL[..16], alpha.zip(ac).map(|((_, av), ac)| (av, ac)), None, smooth_neighbor_uv, fctx);
16611+ } else {
16612+ push_intra_rect(1, px, py, bw, bh, uv_predict_mode, 0, reach, &ZERO_RESIDUAL[..bw * bh], alpha.zip(ac).map(|((au, _), ac)| (au, ac)), None, smooth_neighbor_uv, fctx);
16613+ push_intra_rect(2, px, py, bw, bh, uv_predict_mode, 0, reach, &ZERO_RESIDUAL[..bw * bh], alpha.zip(ac).map(|((_, av), ac)| (av, ac)), None, smooth_neighbor_uv, fctx);
16614+ }
16615+ // The same arm-without-clear route the 4:2:0 tails count: a skipped
16616+ // leaf reads no coefficient, so an armed intrabc slot is dropped.
16617+ if fctx.intrabc_chroma_tx.with(std::cell::Cell::get).is_some() {
16618+ hit!(SKIPPED_INTRABC_CHROMA_ARM_HITS);
16619+ fctx.intrabc_chroma_tx.with(|c| c.set(None));
16620+ }
16621+ (Grid::Zero(bw * bh), Grid::Zero(bw * bh))
16622+ } else if let Some(dv) = intrabc_dv {
16623+ // Frame copy at the DV; the residual still reads the ordinary chroma
16624+ // tables with the inherited luma `tx_type` (see the 4:2:0 tails).
16625+ let mut ub = vec![0u16; bw * bh];
16626+ mc::predict_with_filter(
16627+ &u.data, u.width, u.true_width, u.true_height,
16628+ mv_to_q4(px, dv.1, 0), mv_to_q4(py, dv.0, 0),
16629+ bw, bh, mc::InterpFilterKind::Bilinear, &mut ub, fctx,
16630+ );
16631+ let mut vb = vec![0u16; bw * bh];
16632+ mc::predict_with_filter(
16633+ &v.data, v.width, v.true_width, v.true_height,
16634+ mv_to_q4(px, dv.1, 0), mv_to_q4(py, dv.0, 0),
16635+ bw, bh, mc::InterpFilterKind::Bilinear, &mut vb, fctx,
16636+ );
16637+ let around = neighbours.around_mi_rect(lmi, bw, bh);
16638+ let pair = if square {
16639+ set_palette_pred(ub, fctx);
16640+ let ug = read_plane(
16641+ dec, cdfs, TxbSet::Chroma4, scan4, 1, around[1], DC_PRED, DC_PRED,
16642+ 0, reach, u, px, py, 4, TX4, base_q_idx,
16643+ None, None, None, smooth_neighbor_uv, fctx,
16644+ )?;
16645+ set_palette_pred(vb, fctx);
16646+ let vg = read_plane(
16647+ dec, cdfs, TxbSet::Chroma4, scan4, 2, around[2], DC_PRED, DC_PRED,
16648+ 0, reach, v, px, py, 4, TX4, base_q_idx,
16649+ None, None, None, smooth_neighbor_uv, fctx,
16650+ )?;
16651+ (ug, vg)
16652+ } else {
16653+ let scan: &[u16] = if bw > bh { &SCAN_8X4 } else { &SCAN_4X8 };
16654+ let default_tx =
16655+ fctx.intrabc_chroma_tx.with(std::cell::Cell::get).unwrap_or(TxType::DctDct);
16656+ let mut out = (Grid::Zero(bw * bh), Grid::Zero(bw * bh));
16657+ for (plane_idx, buf) in [(1usize, ub), (2usize, vb)] {
16658+ let skip_ctx =
16659+ usize::from(around[plane_idx].0) + usize::from(around[plane_idx].1);
16660+ let mut coding = cdfs.txb(TxbSet::ChromaRect8x4, DC_PRED);
16661+ let (levels, tx_type) = read_coeffs_rect(
16662+ dec, &mut coding, scan, bw, bh, skip_ctx,
16663+ dc_sign_ctx(around[plane_idx].2), default_tx,
16664+ )?;
16665+ let residual = dequant_and_inverse_typed_wh(
16666+ &levels, bw, bh, crate::decode::bit_depth(fctx), block_q_idx(fctx),
16667+ plane_q_delta(plane_idx, fctx).0, plane_q_delta(plane_idx, fctx).1,
16668+ tx_type,
16669+ );
16670+ set_palette_pred(buf, fctx);
16671+ push_intra_rect(
16672+ plane_idx, px, py, bw, bh, DC_PRED, 0, reach, &residual,
16673+ None, None, smooth_neighbor_uv, fctx,
16674+ );
16675+ if plane_idx == 1 { out.0 = levels; } else { out.1 = levels; }
16676+ }
16677+ out
16678+ };
16679+ fctx.intrabc_chroma_tx.with(|c| c.set(None));
16680+ pair
16681+ } else {
16682+ let ac = alpha.map(|_| cfl_src_rect(px, py, bw, bh));
16683+ let around = neighbours.around_mi_rect(lmi, bw, bh);
16684+ if square {
16685+ let ug = read_plane(
16686+ dec, cdfs, TxbSet::Chroma4, scan4, 1, around[1], uv_mode, uv_predict_mode,
16687+ 0, reach, u, px, py, 4, TX4, base_q_idx,
16688+ alpha.zip(ac).map(|((au, _), ac)| (au, ac)), None, None, smooth_neighbor_uv, fctx,
16689+ )?;
16690+ let vg = read_plane(
16691+ dec, cdfs, TxbSet::Chroma4, scan4, 2, around[2], uv_mode, uv_predict_mode,
16692+ 0, reach, v, px, py, 4, TX4, base_q_idx,
16693+ alpha.zip(ac).map(|((_, av), ac)| (av, ac)), None, None, smooth_neighbor_uv, fctx,
16694+ )?;
16695+ (ug, vg)
16696+ } else {
16697+ // `av1_get_max_uv_txsize(BLOCK_4X8/8X4)` at ss 0/0 is the rect
16698+ // transform itself; its `tx_type` is mode-derived with the
16699+ // <32 set (`decode_rect_split`'s own rule for this shape).
16700+ let scan: &[u16] = if bw > bh { &SCAN_8X4 } else { &SCAN_4X8 };
16701+ let default_tx = default_intra_tx_type(uv_predict_mode as u8);
16702+ let mut out = (Grid::Zero(bw * bh), Grid::Zero(bw * bh));
16703+ for plane_idx in 1..=2usize {
16704+ let skip_ctx =
16705+ usize::from(around[plane_idx].0) + usize::from(around[plane_idx].1);
16706+ let mut coding = cdfs.txb(TxbSet::ChromaRect8x4, uv_predict_mode);
16707+ let (levels, tx_type) = read_coeffs_rect(
16708+ dec, &mut coding, scan, bw, bh, skip_ctx,
16709+ dc_sign_ctx(around[plane_idx].2), default_tx,
16710+ )?;
16711+ let residual = dequant_and_inverse_typed_wh(
16712+ &levels, bw, bh, crate::decode::bit_depth(fctx), block_q_idx(fctx),
16713+ plane_q_delta(plane_idx, fctx).0, plane_q_delta(plane_idx, fctx).1,
16714+ tx_type,
16715+ );
16716+ let cfl = alpha
16717+ .zip(ac)
16718+ .map(|((au, av), ac)| (if plane_idx == 1 { au } else { av }, ac));
16719+ push_intra_rect(
16720+ plane_idx, px, py, bw, bh, uv_predict_mode, 0, reach, &residual,
16721+ cfl, None, smooth_neighbor_uv, fctx,
16722+ );
16723+ if plane_idx == 1 { out.0 = levels; } else { out.1 = levels; }
16724+ }
16725+ out
16726+ }
16727+ };
16728+ // libaom `av1_set_entropy_contexts` stamps above/left PER TRANSFORM UNIT:
16729+ // the leaf's own chroma span, not the 8x8 group's.
16730+ let (u_grid, v_grid) = grids;
16731+ let u_state = neighbour_state(&u_grid);
16732+ let v_state = neighbour_state(&v_grid);
16733+ for cell in 0..h_mi {
16734+ if lmi.0 + cell < neighbours.mi_rows {
16735+ neighbours.left[lmi.0 + cell][1] = u_state;
16736+ neighbours.left[lmi.0 + cell][2] = v_state;
16737+ }
16738+ }
16739+ for cell in 0..w_mi {
16740+ if lmi.1 + cell < neighbours.mi_cols {
16741+ neighbours.above[lmi.1 + cell][1] = u_state;
16742+ neighbours.above[lmi.1 + cell][2] = v_state;
16743+ }
16744+ }
16745+ Ok((u_grid, v_grid))
16746+}
16747+
16534 16748
16535 /// Decodes a `PARTITION_SPLIT` of one 8x8 block into four `BLOCK_4X4` leaves16749 /// Decodes a `PARTITION_SPLIT` of one 8x8 block into four `BLOCK_4X4` leaves
16536 /// (spec `decode_partition`'s recursion bottom), sharing [`decode_leaf8`]'s16750 /// (spec `decode_partition`'s recursion bottom), sharing [`decode_leaf8`]'s
@@ -16576,7 +16790,7 @@ fn decode_leaf_split4(
16576 }16790 }
16577 // See `decode_leaf8`'s own `smooth_neighbor_uv`: the chroma edge-filter16791 // See `decode_leaf8`'s own `smooth_neighbor_uv`: the chroma edge-filter
16578 // type is the chroma neighbours' `uv_mode`, mi-exact where recorded.16792 // type is the chroma neighbours' `uv_mode`, mi-exact where recorded.
16579- let smooth_neighbor_uv = neighbours.smooth_uv_neighbour(leaf_mi.0, leaf_mi.1, r, c);16793+ let smooth_neighbor_uv = neighbours.smooth_uv_neighbour(leaf_mi.0, leaf_mi.1, r, c, fctx);
16580 let mut leaf_modes = [0usize; 4];16794 let mut leaf_modes = [0usize; 4];
16581 let mut leaf_skips = [false; 4];16795 let mut leaf_skips = [false; 4];
16582 let mut last_uv: Option<(usize, i32, Option<(i32, i32)>)> = None;16796 let mut last_uv: Option<(usize, i32, Option<(i32, i32)>)> = None;
@@ -16584,6 +16798,14 @@ fn decode_leaf_split4(
16584 // leaf is an intrabc block -- its prediction/reconstruction below is a16798 // leaf is an intrabc block -- its prediction/reconstruction below is a
16585 // frame copy at this DV, not an intra one.16799 // frame copy at this DV, not an intra one.
16586 let mut last_intrabc: Option<(i32, i32)> = None;16800 let mut last_intrabc: Option<(i32, i32)> = None;
16801+ // 4:4:4 (lane-av1-444): `is_chroma_reference` (av1_common_int.h:1459) is
16802+ // unconditional at subsampling 0/0, so EVERY 4x4 leaf reads its own
16803+ // `uv_mode` and codes its own full-resolution 4x4 chroma unit right
16804+ // after its luma -- there is no shared group unit at all.
16805+ let chroma_444 = ss_x(fctx) == 0 && ss_y(fctx) == 0;
16806+ // The last leaf's own `uv_predict_mode`, for the coarse above/left slots
16807+ // the 4:2:0 tail fills from the group's single unit.
16808+ let mut last_uv444 = DC_PRED;
16587 for i in 0..4usize {16809 for i in 0..4usize {
16588 let (dr, dc) = (i / 2, i % 2);16810 let (dr, dc) = (i / 2, i % 2);
16589 let lmi = (leaf_mi.0 + dr, leaf_mi.1 + dc);16811 let lmi = (leaf_mi.0 + dr, leaf_mi.1 + dc);
@@ -16601,7 +16823,7 @@ fn decode_leaf_split4(
16601 } else {16823 } else {
16602 leaf_modes[i - 1]16824 leaf_modes[i - 1]
16603 };16825 };
16604- let has_chroma = i == 3;16826+ let has_chroma = chroma_444 || i == 3;
16605 let skip_ctx = neighbours.skip_txfm_ctx(lmi.0, lmi.1);16827 let skip_ctx = neighbours.skip_txfm_ctx(lmi.0, lmi.1);
16606 let (skip, mode, angle_delta_y, chroma, filter_intra, intrabc) = read_intra_mode_sub8(16828 let (skip, mode, angle_delta_y, chroma, filter_intra, intrabc) = read_intra_mode_sub8(
16607 dec, cdfs, leaf_above, leaf_left, has_chroma, enable_filter_intra,16829 dec, cdfs, leaf_above, leaf_left, has_chroma, enable_filter_intra,
@@ -16675,24 +16897,35 @@ fn decode_leaf_split4(
16675 neighbours.fill_lf_grid(lmi, 1, 4, 0, fctx);16897 neighbours.fill_lf_grid(lmi, 1, 4, 0, fctx);
16676 record_intrabc_mi(lmi.0, lmi.1, 1, Some(dv), neighbours, fctx);16898 record_intrabc_mi(lmi.0, lmi.1, 1, Some(dv), neighbours, fctx);
16677 if has_chroma {16899 if has_chroma {
16678- last_uv = Some((DC_PRED, 0, None));
16679 // An intrabc block is an INTER block for `av1_get_tx_type`, so16900 // An intrabc block is an INTER block for `av1_get_tx_type`, so
16680- // the group's chroma inherits the LUMA's coded transform type16901+ // the chroma inherits the LUMA's coded transform type
16681 // (`xd->tx_type_map` at the co-located luma position) -- the16902 // (`xd->tx_type_map` at the co-located luma position) -- the
16682 // luma `read_plane` above just set [`LUMA_TX_TYPE`]. Armed for16903 // luma `read_plane` above just set [`LUMA_TX_TYPE`]. Armed for
16683- // exactly this group's two chroma reads.16904+ // exactly this leaf's two chroma reads.
16684 fctx.intrabc_chroma_tx.with(|c| c.set(Some(fctx.luma_tx_type.with(std::cell::Cell::get))));16905 fctx.intrabc_chroma_tx.with(|c| c.set(Some(fctx.luma_tx_type.with(std::cell::Cell::get))));
16685- // lane-av1txbands: the group's chroma is the CHROMA-REFERENCE16906+ if chroma_444 {
16686- // leaf's own block -- libaom codes one chroma unit under the16907+ // lane-av1-444: this leaf codes its OWN 4x4 chroma unit
16687- // last sub-block only (`is_chroma_reference`), so its prediction16908+ // right here (frame copy at the DV); no group tail.
16688- // is that leaf's `uv_mode` (CfL alpha and all) unless THAT leaf16909+ sub8_leaf_chroma444(
16689- // is the intrabc one. Arming this from a non-chroma-reference16910+ dec, cdfs, neighbours, lmi, 4, 4, DC_PRED, None, skip,
16690- // leaf's DV made `warped.obu`'s 8x8 group at mi(40,62) copy16911+ Some(dv), y, u, v, scan4, base_q_idx,
16691- // frame bytes at mi(41,62)'s vector where aomdec predicts16912+ neighbours.smooth_uv_neighbour(lmi.0, lmi.1, r, c, fctx), fctx,
16692- // `uv_mode=UV_CFL_PRED`: luma stayed byte-exact and only chroma16913+ )?;
16693- // moved (class chroma-keyed-at-a-non-chroma-leaf). Mirrors16914+ last_uv444 = DC_PRED;
16694- // [`decode_leaf_rect8`]'s own placement.16915+ } else {
16695- last_intrabc = Some(dv);16916+ last_uv = Some((DC_PRED, 0, None));
16917+ // lane-av1txbands: the group's chroma is the CHROMA-REFERENCE
16918+ // leaf's own block -- libaom codes one chroma unit under the
16919+ // last sub-block only (`is_chroma_reference`), so its prediction
16920+ // is that leaf's `uv_mode` (CfL alpha and all) unless THAT leaf
16921+ // is the intrabc one. Arming this from a non-chroma-reference
16922+ // leaf's DV made `warped.obu`'s 8x8 group at mi(40,62) copy
16923+ // frame bytes at mi(41,62)'s vector where aomdec predicts
16924+ // `uv_mode=UV_CFL_PRED`: luma stayed byte-exact and only chroma
16925+ // moved (class chroma-keyed-at-a-non-chroma-leaf). Mirrors
16926+ // [`decode_leaf_rect8`]'s own placement.
16927+ last_intrabc = Some(dv);
16928+ }
16696 }16929 }
16697 continue;16930 continue;
16698 }16931 }
@@ -16733,13 +16966,34 @@ fn decode_leaf_split4(
16733 neighbours.fill_skip_grid(lmi, 1, skip);16966 neighbours.fill_skip_grid(lmi, 1, skip);
16734 neighbours.fill_lf_grid(lmi, 1, 4, 0, fctx);16967 neighbours.fill_lf_grid(lmi, 1, 4, 0, fctx);
16735 if has_chroma {16968 if has_chroma {
16736- last_uv = chroma;16969+ if chroma_444 {
16970+ // lane-av1-444: the leaf's own 4x4 chroma unit, coded right
16971+ // after its luma (no shared group unit at ss 0/0).
16972+ let (uv_mode, _, alpha) =
16973+ chroma.expect("4:4:4 reads uv_mode on every leaf");
16974+ sub8_leaf_chroma444(
16975+ dec, cdfs, neighbours, lmi, 4, 4, uv_mode, alpha, skip,
16976+ None, y, u, v, scan4, base_q_idx,
16977+ neighbours.smooth_uv_neighbour(lmi.0, lmi.1, r, c, fctx), fctx,
16978+ )?;
16979+ last_uv444 = if uv_mode == UV_CFL_PRED { DC_PRED } else { uv_mode };
16980+ } else {
16981+ last_uv = chroma;
16982+ }
16737 }16983 }
16738 }16984 }
16739 // lane-av1mono: as [`decode_leaf_rect8`] -- a monochrome frame has no16985 // lane-av1mono: as [`decode_leaf_rect8`] -- a monochrome frame has no
16740 // chroma to reconstruct and no uv_mode was read.16986 // chroma to reconstruct and no uv_mode was read.
16741 let (u_grid, v_grid): (Grid, Grid) = if mono(fctx) {16987 let (u_grid, v_grid): (Grid, Grid) = if mono(fctx) {
16742 (Grid::Zero(16), Grid::Zero(16))16988 (Grid::Zero(16), Grid::Zero(16))
16989+ } else if chroma_444 {
16990+ // lane-av1-444: every leaf coded and reconstructed its own chroma in
16991+ // the loop above ([`sub8_leaf_chroma444`] stamped the mi-exact maps
16992+ // and per-leaf entropy state); only the coarse slots remain, filled
16993+ // from the bottom-right leaf like the 4:2:0 tail's single unit.
16994+ neighbours.above_uv_mode[c] = last_uv444;
16995+ neighbours.left_uv_mode[r] = last_uv444;
16996+ (Grid::Zero(16), Grid::Zero(16))
16743 } else {16997 } else {
16744 let (gpx, gpy) = (leaf_mi.1 * MI, leaf_mi.0 * MI);16998 let (gpx, gpy) = (leaf_mi.1 * MI, leaf_mi.0 * MI);
16745 let (cpx, cpy) = (gpx >> ss_x(fctx), gpy >> ss_y(fctx));16999 let (cpx, cpy) = (gpx >> ss_x(fctx), gpy >> ss_y(fctx));
@@ -16849,19 +17103,23 @@ fn decode_leaf_split4(
16849 neighbours.left_side_mi[leaf_mi.0 + cell] = 4;17103 neighbours.left_side_mi[leaf_mi.0 + cell] = 4;
16850 neighbours.above_side_mi[leaf_mi.1 + cell] = 4;17104 neighbours.above_side_mi[leaf_mi.1 + cell] = 4;
16851 }17105 }
16852- let round_up_even = |n: usize| n.div_ceil(2) * 2;17106+ // lane-av1-444: at 4:4:4 [`sub8_leaf_chroma444`] already stamped each
16853- let bound_h = round_up_even(neighbours.mi_rows);17107+ // leaf's own span as it decoded; these placeholder grids carry no state.
16854- let bound_w = round_up_even(neighbours.mi_cols);17108+ if !chroma_444 {
16855- let u_state = neighbour_state(&u_grid);17109+ let round_up_even = |n: usize| n.div_ceil(2) * 2;
16856- let v_state = neighbour_state(&v_grid);17110+ let bound_h = round_up_even(neighbours.mi_rows);
16857- for cell in 0..side_mi {17111+ let bound_w = round_up_even(neighbours.mi_cols);
16858- if cell < side_mi.min(bound_h.saturating_sub(leaf_mi.0)) {17112+ let u_state = neighbour_state(&u_grid);
16859- neighbours.left[leaf_mi.0 + cell][1] = u_state;17113+ let v_state = neighbour_state(&v_grid);
16860- neighbours.left[leaf_mi.0 + cell][2] = v_state;17114+ for cell in 0..side_mi {
16861- }17115+ if cell < side_mi.min(bound_h.saturating_sub(leaf_mi.0)) {
16862- if cell < side_mi.min(bound_w.saturating_sub(leaf_mi.1)) {17116+ neighbours.left[leaf_mi.0 + cell][1] = u_state;
16863- neighbours.above[leaf_mi.1 + cell][1] = u_state;17117+ neighbours.left[leaf_mi.0 + cell][2] = v_state;
16864- neighbours.above[leaf_mi.1 + cell][2] = v_state;17118+ }
17119+ if cell < side_mi.min(bound_w.saturating_sub(leaf_mi.1)) {
17120+ neighbours.above[leaf_mi.1 + cell][1] = u_state;
17121+ neighbours.above[leaf_mi.1 + cell][2] = v_state;
17122+ }
16865 }17123 }
16866 }17124 }
16867 // lane-dkey: a sub-8x8 leaf can never use palette (`av1_allow_palette`17125 // lane-dkey: a sub-8x8 leaf can never use palette (`av1_allow_palette`
@@ -16931,7 +17189,7 @@ fn decode_leaf_rect8(
16931 // lane-modectx r1: availability-correct fallback pair (see17189 // lane-modectx r1: availability-correct fallback pair (see
16932 // `modes_above_left`), not the raw coarse bands.17190 // `modes_above_left`), not the raw coarse bands.
16933 let (above_mode, left_mode) = neighbours.modes_above_left(r, c);17191 let (above_mode, left_mode) = neighbours.modes_above_left(r, c);
16934- let smooth_neighbor_uv = neighbours.smooth_uv_neighbour(leaf_mi.0, leaf_mi.1, r, c);17192+ let smooth_neighbor_uv = neighbours.smooth_uv_neighbour(leaf_mi.0, leaf_mi.1, r, c, fctx);
16935 let (bw, bh) = if vert { (4, 8) } else { (8, 4) };17193 let (bw, bh) = if vert { (4, 8) } else { (8, 4) };
16936 let (w_mi, h_mi) = (bw / MI, bh / MI);17194 let (w_mi, h_mi) = (bw / MI, bh / MI);
16937 let scan: &[u16] = if vert { &SCAN_4X8 } else { &SCAN_8X4 };17195 let scan: &[u16] = if vert { &SCAN_4X8 } else { &SCAN_8X4 };
@@ -16940,6 +17198,11 @@ fn decode_leaf_rect8(
16940 let mut last_uv: Option<(usize, i32, Option<(i32, i32)>)> = None;17198 let mut last_uv: Option<(usize, i32, Option<(i32, i32)>)> = None;
16941 // lane-av1txr: as [`decode_leaf_split4`]'s own `last_intrabc`.17199 // lane-av1txr: as [`decode_leaf_split4`]'s own `last_intrabc`.
16942 let mut last_intrabc: Option<(i32, i32)> = None;17200 let mut last_intrabc: Option<(i32, i32)> = None;
17201+ // 4:4:4 (lane-av1-444): as [`decode_leaf_split4`] -- at subsampling 0/0
17202+ // EVERY 4x8/8x4 leaf reads its own `uv_mode` and codes its own
17203+ // full-resolution rect chroma unit right after its luma.
17204+ let chroma_444 = ss_x(fctx) == 0 && ss_y(fctx) == 0;
17205+ let mut last_uv444 = DC_PRED;
16943 for i in 0..2usize {17206 for i in 0..2usize {
16944 let lmi = if vert {17207 let lmi = if vert {
16945 (leaf_mi.0, leaf_mi.1 + i)17208 (leaf_mi.0, leaf_mi.1 + i)
@@ -16958,7 +17221,7 @@ fn decode_leaf_rect8(
16958 } else {17221 } else {
16959 neighbours.mode_left_mi(lmi.0, lmi.1).unwrap_or(left_mode)17222 neighbours.mode_left_mi(lmi.0, lmi.1).unwrap_or(left_mode)
16960 };17223 };
16961- let has_chroma = i == 1;17224+ let has_chroma = chroma_444 || i == 1;
16962 let skip_ctx = neighbours.skip_txfm_ctx(lmi.0, lmi.1);17225 let skip_ctx = neighbours.skip_txfm_ctx(lmi.0, lmi.1);
16963 let (skip, mode, angle_delta_y, chroma, filter_intra, intrabc) = read_intra_mode_sub8(17226 let (skip, mode, angle_delta_y, chroma, filter_intra, intrabc) = read_intra_mode_sub8(
16964 dec, cdfs, leaf_above, leaf_left, has_chroma, enable_filter_intra,17227 dec, cdfs, leaf_above, leaf_left, has_chroma, enable_filter_intra,
@@ -17132,11 +17395,22 @@ fn decode_leaf_rect8(
17132 // 4x8/8x4 leaf needs its true `w_mi`x`h_mi` (lane-av1txr).17395 // 4x8/8x4 leaf needs its true `w_mi`x`h_mi` (lane-av1txr).
17133 record_intrabc_mi_rect(lmi.0, lmi.1, w_mi, h_mi, Some(dv), neighbours, fctx);17396 record_intrabc_mi_rect(lmi.0, lmi.1, w_mi, h_mi, Some(dv), neighbours, fctx);
17134 if has_chroma {17397 if has_chroma {
17135- last_uv = Some((DC_PRED, 0, None));
17136- last_intrabc = Some(dv);
17137 // See [`decode_leaf_split4`]: an intrabc block's chroma17398 // See [`decode_leaf_split4`]: an intrabc block's chroma
17138 // inherits the luma transform type.17399 // inherits the luma transform type.
17139 fctx.intrabc_chroma_tx.with(|c| c.set(Some(chroma_tx)));17400 fctx.intrabc_chroma_tx.with(|c| c.set(Some(chroma_tx)));
17401+ if chroma_444 {
17402+ // lane-av1-444: this leaf codes its OWN rect chroma unit
17403+ // right here (frame copy at the DV); no group tail.
17404+ sub8_leaf_chroma444(
17405+ dec, cdfs, neighbours, lmi, bw, bh, DC_PRED, None, skip,
17406+ Some(dv), y, u, v, scan4, base_q_idx,
17407+ neighbours.smooth_uv_neighbour(lmi.0, lmi.1, r, c, fctx), fctx,
17408+ )?;
17409+ last_uv444 = DC_PRED;
17410+ } else {
17411+ last_uv = Some((DC_PRED, 0, None));
17412+ last_intrabc = Some(dv);
17413+ }
17140 }17414 }
17141 continue;17415 continue;
17142 }17416 }
@@ -17273,7 +17547,20 @@ fn decode_leaf_rect8(
17273 neighbours.fill_skip_grid_rect(lmi, w_mi, h_mi, skip);17547 neighbours.fill_skip_grid_rect(lmi, w_mi, h_mi, skip);
17274 neighbours.fill_lf_grid_rect(lmi, w_mi, h_mi, 4, 4, 0, fctx);17548 neighbours.fill_lf_grid_rect(lmi, w_mi, h_mi, 4, 4, 0, fctx);
17275 if has_chroma {17549 if has_chroma {
17276- last_uv = chroma;17550+ if chroma_444 {
17551+ // lane-av1-444: the leaf's own rect chroma unit after its
17552+ // split luma (no shared group unit at ss 0/0).
17553+ let (uv_mode, _, alpha) =
17554+ chroma.expect("4:4:4 reads uv_mode on every leaf");
17555+ sub8_leaf_chroma444(
17556+ dec, cdfs, neighbours, lmi, bw, bh, uv_mode, alpha, skip,
17557+ None, y, u, v, scan4, base_q_idx,
17558+ neighbours.smooth_uv_neighbour(lmi.0, lmi.1, r, c, fctx), fctx,
17559+ )?;
17560+ last_uv444 = if uv_mode == UV_CFL_PRED { DC_PRED } else { uv_mode };
17561+ } else {
17562+ last_uv = chroma;
17563+ }
17277 }17564 }
17278 continue;17565 continue;
17279 }17566 }
@@ -17348,7 +17635,20 @@ fn decode_leaf_rect8(
17348 let (tx_w, tx_h) = if split { (4, 4) } else { (bw as u8, bh as u8) };17635 let (tx_w, tx_h) = if split { (4, 4) } else { (bw as u8, bh as u8) };
17349 neighbours.fill_lf_grid_rect(lmi, w_mi, h_mi, tx_w, tx_h, 0, fctx);17636 neighbours.fill_lf_grid_rect(lmi, w_mi, h_mi, tx_w, tx_h, 0, fctx);
17350 if has_chroma {17637 if has_chroma {
17351- last_uv = chroma;17638+ if chroma_444 {
17639+ // lane-av1-444: the leaf's own rect chroma unit after its
17640+ // luma (no shared group unit at ss 0/0).
17641+ let (uv_mode, _, alpha) =
17642+ chroma.expect("4:4:4 reads uv_mode on every leaf");
17643+ sub8_leaf_chroma444(
17644+ dec, cdfs, neighbours, lmi, bw, bh, uv_mode, alpha, skip,
17645+ None, y, u, v, scan4, base_q_idx,
17646+ neighbours.smooth_uv_neighbour(lmi.0, lmi.1, r, c, fctx), fctx,
17647+ )?;
17648+ last_uv444 = if uv_mode == UV_CFL_PRED { DC_PRED } else { uv_mode };
17649+ } else {
17650+ last_uv = chroma;
17651+ }
17352 }17652 }
17353 }17653 }
17354 // lane-av1mono: a monochrome frame codes no chroma at all -- no17654 // lane-av1mono: a monochrome frame codes no chroma at all -- no
@@ -17356,6 +17656,12 @@ fn decode_leaf_rect8(
17356 // so there is no chroma to reconstruct and no chroma coefficient to read.17656 // so there is no chroma to reconstruct and no chroma coefficient to read.
17357 let (u_grid, v_grid): (Grid, Grid) = if mono(fctx) {17657 let (u_grid, v_grid): (Grid, Grid) = if mono(fctx) {
17358 (Grid::Zero(16), Grid::Zero(16))17658 (Grid::Zero(16), Grid::Zero(16))
17659+ } else if chroma_444 {
17660+ // lane-av1-444: every leaf coded and reconstructed its own chroma in
17661+ // the loop above; only the coarse slots remain (last leaf's mode).
17662+ neighbours.above_uv_mode[c] = last_uv444;
17663+ neighbours.left_uv_mode[r] = last_uv444;
17664+ (Grid::Zero(16), Grid::Zero(16))
17359 } else {17665 } else {
17360 let (gpx, gpy) = (leaf_mi.1 * MI, leaf_mi.0 * MI);17666 let (gpx, gpy) = (leaf_mi.1 * MI, leaf_mi.0 * MI);
17361 let (cpx, cpy) = (gpx >> ss_x(fctx), gpy >> ss_y(fctx));17667 let (cpx, cpy) = (gpx >> ss_x(fctx), gpy >> ss_y(fctx));
@@ -17453,19 +17759,23 @@ fn decode_leaf_rect8(
17453 neighbours.above_side_mi[leaf_mi.1 + cell] = bw as u8;17759 neighbours.above_side_mi[leaf_mi.1 + cell] = bw as u8;
17454 neighbours.left_side_mi[leaf_mi.0 + cell] = bh as u8;17760 neighbours.left_side_mi[leaf_mi.0 + cell] = bh as u8;
17455 }17761 }
17456- let round_up_even = |n: usize| n.div_ceil(2) * 2;17762+ // lane-av1-444: at 4:4:4 [`sub8_leaf_chroma444`] already stamped each
17457- let bound_h = round_up_even(neighbours.mi_rows);17763+ // leaf's own span as it decoded; these placeholder grids carry no state.
17458- let bound_w = round_up_even(neighbours.mi_cols);17764+ if !chroma_444 {
17459- let u_state = neighbour_state(&u_grid);17765+ let round_up_even = |n: usize| n.div_ceil(2) * 2;
17460- let v_state = neighbour_state(&v_grid);17766+ let bound_h = round_up_even(neighbours.mi_rows);
17461- for cell in 0..2usize {17767+ let bound_w = round_up_even(neighbours.mi_cols);
17462- if cell < 2usize.min(bound_h.saturating_sub(leaf_mi.0)) {17768+ let u_state = neighbour_state(&u_grid);
17463- neighbours.left[leaf_mi.0 + cell][1] = u_state;17769+ let v_state = neighbour_state(&v_grid);
17464- neighbours.left[leaf_mi.0 + cell][2] = v_state;17770+ for cell in 0..2usize {
17465- }17771+ if cell < 2usize.min(bound_h.saturating_sub(leaf_mi.0)) {
17466- if cell < 2usize.min(bound_w.saturating_sub(leaf_mi.1)) {17772+ neighbours.left[leaf_mi.0 + cell][1] = u_state;
17467- neighbours.above[leaf_mi.1 + cell][1] = u_state;17773+ neighbours.left[leaf_mi.0 + cell][2] = v_state;
17468- neighbours.above[leaf_mi.1 + cell][2] = v_state;17774+ }
17775+ if cell < 2usize.min(bound_w.saturating_sub(leaf_mi.1)) {
17776+ neighbours.above[leaf_mi.1 + cell][1] = u_state;
17777+ neighbours.above[leaf_mi.1 + cell][2] = v_state;
17778+ }
17469 }17779 }
17470 }17780 }
17471 // lane-dkey (same clear as `decode_leaf_split4`): a sub-8x8 leaf can never use palette (`av1_allow_palette`17781 // lane-dkey (same clear as `decode_leaf_split4`): a sub-8x8 leaf can never use palette (`av1_allow_palette`
@@ -30774,7 +31084,7 @@ fn decode_inter_block(
30774 // as [`read_intra_mode`]/[`read_intra_mode_rect`] -- the CHROMA31084 // as [`read_intra_mode`]/[`read_intra_mode_rect`] -- the CHROMA
30775 // neighbour's own `uv_mode`, not the luma one.31085 // neighbour's own `uv_mode`, not the luma one.
30776 let smooth_neighbor_uv =31086 let smooth_neighbor_uv =
30777- neighbours.smooth_uv_neighbour(r * (SUB / MI), c * (SUB / MI), r, c);31087+ neighbours.smooth_uv_neighbour(r * (SUB / MI), c * (SUB / MI), r, c, fctx);
30778 // `read_palette_mode_info` (decodemv.c:567, called from31088 // `read_palette_mode_info` (decodemv.c:567, called from
30779 // `read_intra_block_mode_info` right after `xd->cfl.store_y`, same31089 // `read_intra_block_mode_info` right after `xd->cfl.store_y`, same
30780 // gating and same corner-cut as `read_intra_mode`'s own copy above31090 // gating and same corner-cut as `read_intra_mode`'s own copy above
@@ -30856,13 +31166,16 @@ fn decode_inter_block(
30856 PaletteY { size: n, colors, map }31166 PaletteY { size: n, colors, map }
30857 });31167 });
30858 let palette_uv = palette_uv_pending.map(|(n, u_colors, v_colors)| {31168 let palette_uv = palette_uv_pending.map(|(n, u_colors, v_colors)| {
30859- // `av1_get_plane_block_size(bsize, 1, 1)` floors at the 4-px31169+ // `av1_get_plane_block_size(bsize, 1, 1)` at the sequence's own
30860- // transform, as the rect reader's own copy.31170+ // subsampling, floored at the 4-px transform -- the rect reader's
30861- let (cw, ch) = ((write_w / 2).max(4), (write_h / 2).max(4));31171+ // own copy; a hardcoded `/ 2` sized a 4:4:4 chroma map at 4:2:0
31172+ // and the full-size reconstruct panicked on it (lane-av1-444).
31173+ let (cw, ch) = ((write_w >> ss_x(fctx)).max(4), (write_h >> ss_y(fctx)).max(4));
30862 let on = palette_onscreen_uv(31174 let on = palette_onscreen_uv(
30863 (write_w, write_h),31175 (write_w, write_h),
30864 palette_onscreen(rmi, cmi, write_w, write_h, fctx),31176 palette_onscreen(rmi, cmi, write_w, write_h, fctx),
30865 (cw, ch),31177 (cw, ch),
31178+ (ss_x(fctx), ss_y(fctx)),
30866 );31179 );
30867 let map = decode_color_index_map_wh(dec, cdfs, n, cw, ch, on, true);31180 let map = decode_color_index_map_wh(dec, cdfs, n, cw, ch, on, true);
30868 hit!(PALETTE_UV_HITS);31181 hit!(PALETTE_UV_HITS);
@@ -31622,6 +31935,12 @@ fn decode_inter_sub8_split4(
31622 // Set when the chroma-reference (last) sub-block is INTRA: it coded the31935 // Set when the chroma-reference (last) sub-block is INTRA: it coded the
31623 // group's one chroma unit itself.31936 // group's one chroma unit itself.
31624 let mut intra_chroma = false;31937 let mut intra_chroma = false;
31938+ // 4:4:4 (lane-av1-444): `is_chroma_reference` is unconditional at
31939+ // subsampling 0/0, so EVERY 4x4 sub-block codes its own 4x4 chroma unit
31940+ // right after its own luma (aomdec EC_COEFF on `av1-profile1-444.ivf`
31941+ // frame 1: plane 0,1,2 per leaf in leaf order) -- there is no shared
31942+ // group unit and no 2x2-piece group prediction.
31943+ let chroma_444 = ss_x(fctx) == 0 && ss_y(fctx) == 0;
31625 for i in 0..4usize {31944 for i in 0..4usize {
31626 let (dr, dc) = (i / 2, i % 2);31945 let (dr, dc) = (i / 2, i % 2);
31627 let lmi = (gr + dr, gc + dc);31946 let lmi = (gr + dr, gc + dc);
@@ -31679,7 +31998,7 @@ fn decode_inter_sub8_split4(
31679 group_mi,31998 group_mi,
31680 lmi,31999 lmi,
31681 (B4, B4),32000 (B4, B4),
31682- i == 3,32001+ chroma_444 || i == 3,
31683 skip,32002 skip,
31684 scan4,32003 scan4,
31685 y,32004 y,
@@ -32522,7 +32841,7 @@ fn decode_intra_sub8_leaf(
32522 let (gpx, gpy) = (gc * MI, gr * MI);32841 let (gpx, gpy) = (gc * MI, gr * MI);
32523 let (cpx, cpy) = (gpx >> ss_x(fctx), gpy >> ss_y(fctx));32842 let (cpx, cpy) = (gpx >> ss_x(fctx), gpy >> ss_y(fctx));
32524 let group_reach = Reach::of(8, gpx, gpy, y.width, y.height, fctx);32843 let group_reach = Reach::of(8, gpx, gpy, y.width, y.height, fctx);
32525- let smooth_neighbor_uv = neighbours.smooth_uv_neighbour(gr, gc, r, c);32844+ let smooth_neighbor_uv = neighbours.smooth_uv_neighbour(gr, gc, r, c, fctx);
32526 let uv_predict_mode = if uv_mode == UV_CFL_PRED { DC_PRED } else { uv_mode };32845 let uv_predict_mode = if uv_mode == UV_CFL_PRED { DC_PRED } else { uv_mode };
32527 let angle_delta_uv = 0;32846 let angle_delta_uv = 0;
32528 neighbours.above_uv_mode[c] = uv_predict_mode;32847 neighbours.above_uv_mode[c] = uv_predict_mode;
@@ -33407,7 +33726,18 @@ fn decode_inter_block8(
33407 // bookkeeping. Always `false` outside a `TxMode::Select` inter frame.33726 // bookkeeping. Always `false` outside a `TxMode::Select` inter frame.
33408 let split8;33727 let split8;
33409 const SIDE: usize = 8;33728 const SIDE: usize = 8;
33410- const CHROMA_SIDE: usize = 4;33729+ // lane-av1-444: this 8x8 leaf's chroma plane block is `8 >> ss` a side --
33730+ // 4 at 4:2:0 (one TX_4X4 unit) and the full 8 at 4:4:4, where libaom
33731+ // codes ONE TX_8X8 unit (`av1_get_max_uv_txsize(BLOCK_8X8)` at ss 0/0,
33732+ // measured against aomdec's EC_COEFF on `av1-profile1-444.ivf` frame 1:
33733+ // plane=1 tx=TX_8X8 per 8x8 block, not a 4:2:0 4x4).
33734+ let chroma_side: usize = SIDE >> ss_x(fctx);
33735+ let (chroma_set, chroma_scan): (TxbSet, &[u16]) = if chroma_side == 8 {
33736+ (TxbSet::Chroma8, scan8)
33737+ } else {
33738+ (TxbSet::Chroma4, scan4)
33739+ };
33740+ let chroma_tx = if chroma_side == 8 { TX8 } else { TX4 };
33411 33741
33412 let (r, c) = outer_at;33742 let (r, c) = outer_at;
33413 // lane-inter8 r2: this leaf's OWN mi cell, not the enclosing 16x16's --33743 // lane-inter8 r2: this leaf's OWN mi cell, not the enclosing 16x16's --
@@ -33916,7 +34246,7 @@ fn decode_inter_block8(
33916 mc::combine_compound(&inter0_y, &inter1_y, fwd_offset, bck_offset, &mut pred_y, fctx);34246 mc::combine_compound(&inter0_y, &inter1_y, fwd_offset, bck_offset, &mut pred_y, fctx);
33917 }34247 }
33918 34248
33919- let mut inter0_u = vec![0i32; CHROMA_SIDE * CHROMA_SIDE];34249+ let mut inter0_u = vec![0i32; chroma_side * chroma_side];
33920 mc::predict_compound_intermediate(34250 mc::predict_compound_intermediate(
33921 &pu0.data,34251 &pu0.data,
33922 pu0.width,34252 pu0.width,
@@ -33925,13 +34255,13 @@ fn decode_inter_block8(
33925 mv_to_q4(cpx, mv0.1, ss_x(fctx)),34255 mv_to_q4(cpx, mv0.1, ss_x(fctx)),
33926 mv_to_q4(cpy, mv0.0, ss_x(fctx)),34256 mv_to_q4(cpy, mv0.0, ss_x(fctx)),
33927 scale0,34257 scale0,
33928- CHROMA_SIDE,34258+ chroma_side,
33929- CHROMA_SIDE,34259+ chroma_side,
33930 h_filter,34260 h_filter,
33931 v_filter,34261 v_filter,
33932 &mut inter0_u,34262 &mut inter0_u,
33933 );34263 );
33934- let mut inter1_u = vec![0i32; CHROMA_SIDE * CHROMA_SIDE];34264+ let mut inter1_u = vec![0i32; chroma_side * chroma_side];
33935 mc::predict_compound_intermediate(34265 mc::predict_compound_intermediate(
33936 &pu1.data,34266 &pu1.data,
33937 pu1.width,34267 pu1.width,
@@ -33940,21 +34270,21 @@ fn decode_inter_block8(
33940 mv_to_q4(cpx, mv1.1, ss_x(fctx)),34270 mv_to_q4(cpx, mv1.1, ss_x(fctx)),
33941 mv_to_q4(cpy, mv1.0, ss_x(fctx)),34271 mv_to_q4(cpy, mv1.0, ss_x(fctx)),
33942 scale1,34272 scale1,
33943- CHROMA_SIDE,34273+ chroma_side,
33944- CHROMA_SIDE,34274+ chroma_side,
33945 h_filter,34275 h_filter,
33946 v_filter,34276 v_filter,
33947 &mut inter1_u,34277 &mut inter1_u,
33948 );34278 );
33949- let mut pred_u = refill(std::mem::take(&mut out[1]), CHROMA_SIDE * CHROMA_SIDE);34279+ let mut pred_u = refill(std::mem::take(&mut out[1]), chroma_side * chroma_side);
33950 if let Some(mask_y) = mask_y {34280 if let Some(mask_y) = mask_y {
33951 mc::blend_masked_compound(34281 mc::blend_masked_compound(
33952 &inter0_u,34282 &inter0_u,
33953 &inter1_u,34283 &inter1_u,
33954 mask_y,34284 mask_y,
33955 SIDE,34285 SIDE,
33956- CHROMA_SIDE,34286+ chroma_side,
33957- CHROMA_SIDE,34287+ chroma_side,
33958 ss_x(fctx) == 1 && ss_y(fctx) == 1,34288 ss_x(fctx) == 1 && ss_y(fctx) == 1,
33959 &mut pred_u, fctx,34289 &mut pred_u, fctx,
33960 );34290 );
@@ -33962,7 +34292,7 @@ fn decode_inter_block8(
33962 mc::combine_compound(&inter0_u, &inter1_u, fwd_offset, bck_offset, &mut pred_u, fctx);34292 mc::combine_compound(&inter0_u, &inter1_u, fwd_offset, bck_offset, &mut pred_u, fctx);
33963 }34293 }
33964 34294
33965- let mut inter0_v = vec![0i32; CHROMA_SIDE * CHROMA_SIDE];34295+ let mut inter0_v = vec![0i32; chroma_side * chroma_side];
33966 mc::predict_compound_intermediate(34296 mc::predict_compound_intermediate(
33967 &pv0.data,34297 &pv0.data,
33968 pv0.width,34298 pv0.width,
@@ -33971,13 +34301,13 @@ fn decode_inter_block8(
33971 mv_to_q4(cpx, mv0.1, ss_x(fctx)),34301 mv_to_q4(cpx, mv0.1, ss_x(fctx)),
33972 mv_to_q4(cpy, mv0.0, ss_x(fctx)),34302 mv_to_q4(cpy, mv0.0, ss_x(fctx)),
33973 scale0,34303 scale0,
33974- CHROMA_SIDE,34304+ chroma_side,
33975- CHROMA_SIDE,34305+ chroma_side,
33976 h_filter,34306 h_filter,
33977 v_filter,34307 v_filter,
33978 &mut inter0_v,34308 &mut inter0_v,
33979 );34309 );
33980- let mut inter1_v = vec![0i32; CHROMA_SIDE * CHROMA_SIDE];34310+ let mut inter1_v = vec![0i32; chroma_side * chroma_side];
33981 mc::predict_compound_intermediate(34311 mc::predict_compound_intermediate(
33982 &pv1.data,34312 &pv1.data,
33983 pv1.width,34313 pv1.width,
@@ -33986,21 +34316,21 @@ fn decode_inter_block8(
33986 mv_to_q4(cpx, mv1.1, ss_x(fctx)),34316 mv_to_q4(cpx, mv1.1, ss_x(fctx)),
33987 mv_to_q4(cpy, mv1.0, ss_x(fctx)),34317 mv_to_q4(cpy, mv1.0, ss_x(fctx)),
33988 scale1,34318 scale1,
33989- CHROMA_SIDE,34319+ chroma_side,
33990- CHROMA_SIDE,34320+ chroma_side,
33991 h_filter,34321 h_filter,
33992 v_filter,34322 v_filter,
33993 &mut inter1_v,34323 &mut inter1_v,
33994 );34324 );
33995- let mut pred_v = refill(std::mem::take(&mut out[2]), CHROMA_SIDE * CHROMA_SIDE);34325+ let mut pred_v = refill(std::mem::take(&mut out[2]), chroma_side * chroma_side);
33996 if let Some(mask_y) = mask_y {34326 if let Some(mask_y) = mask_y {
33997 mc::blend_masked_compound(34327 mc::blend_masked_compound(
33998 &inter0_v,34328 &inter0_v,
33999 &inter1_v,34329 &inter1_v,
34000 mask_y,34330 mask_y,
34001 SIDE,34331 SIDE,
34002- CHROMA_SIDE,34332+ chroma_side,
34003- CHROMA_SIDE,34333+ chroma_side,
34004 ss_x(fctx) == 1 && ss_y(fctx) == 1,34334 ss_x(fctx) == 1 && ss_y(fctx) == 1,
34005 &mut pred_v, fctx,34335 &mut pred_v, fctx,
34006 );34336 );
@@ -34016,8 +34346,8 @@ fn decode_inter_block8(
34016 push_pred(fctx, build);34346 push_pred(fctx, build);
34017 (34347 (
34018 Pred::Cur { plane: 0, stride: SIDE, off: 0 },34348 Pred::Cur { plane: 0, stride: SIDE, off: 0 },
34019- Pred::Cur { plane: 1, stride: CHROMA_SIDE, off: 0 },34349+ Pred::Cur { plane: 1, stride: chroma_side, off: 0 },
34020- Pred::Cur { plane: 2, stride: CHROMA_SIDE, off: 0 },34350+ Pred::Cur { plane: 2, stride: chroma_side, off: 0 },
34021 )34351 )
34022 } else {34352 } else {
34023 let mut built: [Vec<u16>; 3] = Default::default();34353 let mut built: [Vec<u16>; 3] = Default::default();
@@ -34026,8 +34356,8 @@ fn decode_inter_block8(
34026 (pred_y, pred_u, pred_v) = (by, bu, bv);34356 (pred_y, pred_u, pred_v) = (by, bu, bv);
34027 (34357 (
34028 Pred::Inline(&pred_y, SIDE),34358 Pred::Inline(&pred_y, SIDE),
34029- Pred::Inline(&pred_u, CHROMA_SIDE),34359+ Pred::Inline(&pred_u, chroma_side),
34030- Pred::Inline(&pred_v, CHROMA_SIDE),34360+ Pred::Inline(&pred_v, chroma_side),
34031 )34361 )
34032 };34362 };
34033 split8 = read_block_tx_size(34363 split8 = read_block_tx_size(
@@ -34047,20 +34377,20 @@ fn decode_inter_block8(
34047 push_mc(1, 34377 push_mc(1,
34048 cpx,34378 cpx,
34049 cpy,34379 cpy,
34050- CHROMA_SIDE,34380+ chroma_side,
34051 su,34381 su,
34052- &ZERO_RESIDUAL[..CHROMA_SIDE * CHROMA_SIDE], fctx,34382+ &ZERO_RESIDUAL[..chroma_side * chroma_side], fctx,
34053 );34383 );
34054 push_mc(2, 34384 push_mc(2,
34055 cpx,34385 cpx,
34056 cpy,34386 cpy,
34057- CHROMA_SIDE,34387+ chroma_side,
34058 sv,34388 sv,
34059- &ZERO_RESIDUAL[..CHROMA_SIDE * CHROMA_SIDE], fctx,34389+ &ZERO_RESIDUAL[..chroma_side * chroma_side], fctx,
34060 );34390 );
34061 luma_grid = Grid::Zero(SIDE * SIDE);34391 luma_grid = Grid::Zero(SIDE * SIDE);
34062- u_grid = Grid::Zero(CHROMA_SIDE * CHROMA_SIDE);34392+ u_grid = Grid::Zero(chroma_side * chroma_side);
34063- v_grid = Grid::Zero(CHROMA_SIDE * CHROMA_SIDE);34393+ v_grid = Grid::Zero(chroma_side * chroma_side);
34064 } else {34394 } else {
34065 let around = neighbours.around_mi(leaf_mi, 8);34395 let around = neighbours.around_mi(leaf_mi, 8);
34066 let (grid8, luma_tx_type) = read_inter_luma8(34396 let (grid8, luma_tx_type) = read_inter_luma8(
@@ -34083,15 +34413,15 @@ fn decode_inter_block8(
34083 u_grid = read_inter_plane(34413 u_grid = read_inter_plane(
34084 dec,34414 dec,
34085 cdfs,34415 cdfs,
34086- TxbSet::Chroma4,34416+ chroma_set,
34087- scan4,34417+ chroma_scan,
34088 1,34418 1,
34089 around[1],34419 around[1],
34090 mode_for_tx,34420 mode_for_tx,
34091 u,34421 u,
34092 cpx,34422 cpx,
34093 cpy,34423 cpy,
34094- CHROMA_SIDE,34424+ chroma_side,
34095 base_q_idx,34425 base_q_idx,
34096 su,34426 su,
34097 Some(luma_tx_type),34427 Some(luma_tx_type),
@@ -34101,15 +34431,15 @@ fn decode_inter_block8(
34101 v_grid = read_inter_plane(34431 v_grid = read_inter_plane(
34102 dec,34432 dec,
34103 cdfs,34433 cdfs,
34104- TxbSet::Chroma4,34434+ chroma_set,
34105- scan4,34435+ chroma_scan,
34106 2,34436 2,
34107 around[2],34437 around[2],
34108 mode_for_tx,34438 mode_for_tx,
34109 v,34439 v,
34110 cpx,34440 cpx,
34111 cpy,34441 cpy,
34112- CHROMA_SIDE,34442+ chroma_side,
34113 base_q_idx,34443 base_q_idx,
34114 sv,34444 sv,
34115 Some(luma_tx_type),34445 Some(luma_tx_type),
@@ -34596,7 +34926,7 @@ fn decode_inter_block8(
34596 SIDE,34926 SIDE,
34597 SIDE,34927 SIDE,
34598 SIDE,34928 SIDE,
34599- CHROMA_SIDE,34929+ chroma_side,
34600 px,34930 px,
34601 py,34931 py,
34602 cpx,34932 cpx,
@@ -34620,15 +34950,15 @@ fn decode_inter_block8(
34620 return;34950 return;
34621 };34951 };
34622 let mut pred_y = refill(std::mem::take(&mut out[0]), SIDE * SIDE);34952 let mut pred_y = refill(std::mem::take(&mut out[0]), SIDE * SIDE);
34623- let mut pred_u = refill(std::mem::take(&mut out[1]), CHROMA_SIDE * CHROMA_SIDE);34953+ let mut pred_u = refill(std::mem::take(&mut out[1]), chroma_side * chroma_side);
34624- let mut pred_v = refill(std::mem::take(&mut out[2]), CHROMA_SIDE * CHROMA_SIDE);34954+ let mut pred_v = refill(std::mem::take(&mut out[2]), chroma_side * chroma_side);
34625 // Merge of lane-gmaffine (this leaf's own switchable filters) with34955 // Merge of lane-gmaffine (this leaf's own switchable filters) with
34626 // lane-scaledref (spec 7.11.3.3's scaled walk): same kernels either34956 // lane-scaledref (spec 7.11.3.3's scaled walk): same kernels either
34627 // way, only the sample walk differs.34957 // way, only the sample walk differs.
34628 for (plane, x, y, dim, dst) in [34958 for (plane, x, y, dim, dst) in [
34629 (sref_y, px, py, SIDE, &mut pred_y),34959 (sref_y, px, py, SIDE, &mut pred_y),
34630- (sref_u, cpx, cpy, CHROMA_SIDE, &mut pred_u),34960+ (sref_u, cpx, cpy, chroma_side, &mut pred_u),
34631- (sref_v, cpx, cpy, CHROMA_SIDE, &mut pred_v),34961+ (sref_v, cpx, cpy, chroma_side, &mut pred_v),
34632 ] {34962 ] {
34633 let luma = std::ptr::eq(plane, sref_y);34963 let luma = std::ptr::eq(plane, sref_y);
34634 if luma_scale == mc::REF_NO_SCALE {34964 if luma_scale == mc::REF_NO_SCALE {
@@ -34680,16 +35010,16 @@ fn decode_inter_block8(
34680 // bail-out: an 8x8 leaf's chroma plane is 4x4 in 420, so it is35010 // bail-out: an 8x8 leaf's chroma plane is 4x4 in 420, so it is
34681 // predicted translationally even when the luma warps.35011 // predicted translationally even when the luma warps.
34682 #[allow(clippy::absurd_extreme_comparisons)]35012 #[allow(clippy::absurd_extreme_comparisons)]
34683- if CHROMA_SIDE >= 8 {35013+ if chroma_side >= 8 {
34684 crate::warp::warp_affine(35014 crate::warp::warp_affine(
34685 params, &sref_u.data, sref_u.true_width as i32, sref_u.true_height as i32,35015 params, &sref_u.data, sref_u.true_width as i32, sref_u.true_height as i32,
34686- sref_u.width as i32, &mut pred_u, cpx as i32, cpy as i32, CHROMA_SIDE as i32,35016+ sref_u.width as i32, &mut pred_u, cpx as i32, cpy as i32, chroma_side as i32,
34687- CHROMA_SIDE as i32, CHROMA_SIDE as i32, ss_x(fctx) as i32, ss_y(fctx) as i32, fctx,35017+ chroma_side as i32, chroma_side as i32, ss_x(fctx) as i32, ss_y(fctx) as i32, fctx,
34688 );35018 );
34689 crate::warp::warp_affine(35019 crate::warp::warp_affine(
34690 params, &sref_v.data, sref_v.true_width as i32, sref_v.true_height as i32,35020 params, &sref_v.data, sref_v.true_width as i32, sref_v.true_height as i32,
34691- sref_v.width as i32, &mut pred_v, cpx as i32, cpy as i32, CHROMA_SIDE as i32,35021+ sref_v.width as i32, &mut pred_v, cpx as i32, cpy as i32, chroma_side as i32,
34692- CHROMA_SIDE as i32, CHROMA_SIDE as i32, ss_x(fctx) as i32, ss_y(fctx) as i32, fctx,35022+ chroma_side as i32, chroma_side as i32, ss_x(fctx) as i32, ss_y(fctx) as i32, fctx,
34693 );35023 );
34694 }35024 }
34695 }35025 }
@@ -34703,10 +35033,10 @@ fn decode_inter_block8(
34703 if let Some(ii) = interintra_mode {35033 if let Some(ii) = interintra_mode {
34704 interintra_blend(y, px, py, SIDE, SIDE, SIDE, ii, wedge_mask, &mut pred_y, fctx);35034 interintra_blend(y, px, py, SIDE, SIDE, SIDE, ii, wedge_mask, &mut pred_y, fctx);
34705 interintra_blend(35035 interintra_blend(
34706- u, cpx, cpy, CHROMA_SIDE, CHROMA_SIDE, CHROMA_SIDE, ii, wedge_mask, &mut pred_u, fctx,35036+ u, cpx, cpy, chroma_side, chroma_side, chroma_side, ii, wedge_mask, &mut pred_u, fctx,
34707 );35037 );
34708 interintra_blend(35038 interintra_blend(
34709- v, cpx, cpy, CHROMA_SIDE, CHROMA_SIDE, CHROMA_SIDE, ii, wedge_mask, &mut pred_v, fctx,35039+ v, cpx, cpy, chroma_side, chroma_side, chroma_side, ii, wedge_mask, &mut pred_v, fctx,
34710 );35040 );
34711 }35041 }
34712 35042
@@ -34718,8 +35048,8 @@ fn decode_inter_block8(
34718 push_pred(fctx, build);35048 push_pred(fctx, build);
34719 (35049 (
34720 Pred::Cur { plane: 0, stride: SIDE, off: 0 },35050 Pred::Cur { plane: 0, stride: SIDE, off: 0 },
34721- Pred::Cur { plane: 1, stride: CHROMA_SIDE, off: 0 },35051+ Pred::Cur { plane: 1, stride: chroma_side, off: 0 },
34722- Pred::Cur { plane: 2, stride: CHROMA_SIDE, off: 0 },35052+ Pred::Cur { plane: 2, stride: chroma_side, off: 0 },
34723 )35053 )
34724 } else {35054 } else {
34725 // The interintra predictor reads this block's own above/left35055 // The interintra predictor reads this block's own above/left
@@ -34734,8 +35064,8 @@ fn decode_inter_block8(
34734 (pred_y, pred_u, pred_v) = (by, bu, bv);35064 (pred_y, pred_u, pred_v) = (by, bu, bv);
34735 (35065 (
34736 Pred::Inline(&pred_y, SIDE),35066 Pred::Inline(&pred_y, SIDE),
34737- Pred::Inline(&pred_u, CHROMA_SIDE),35067+ Pred::Inline(&pred_u, chroma_side),
34738- Pred::Inline(&pred_v, CHROMA_SIDE),35068+ Pred::Inline(&pred_v, chroma_side),
34739 )35069 )
34740 };35070 };
34741 split8 = read_block_tx_size(35071 split8 = read_block_tx_size(
@@ -34755,20 +35085,20 @@ fn decode_inter_block8(
34755 push_mc(1, 35085 push_mc(1,
34756 cpx,35086 cpx,
34757 cpy,35087 cpy,
34758- CHROMA_SIDE,35088+ chroma_side,
34759 su,35089 su,
34760- &ZERO_RESIDUAL[..CHROMA_SIDE * CHROMA_SIDE], fctx,35090+ &ZERO_RESIDUAL[..chroma_side * chroma_side], fctx,
34761 );35091 );
34762 push_mc(2, 35092 push_mc(2,
34763 cpx,35093 cpx,
34764 cpy,35094 cpy,
34765- CHROMA_SIDE,35095+ chroma_side,
34766 sv,35096 sv,
34767- &ZERO_RESIDUAL[..CHROMA_SIDE * CHROMA_SIDE], fctx,35097+ &ZERO_RESIDUAL[..chroma_side * chroma_side], fctx,
34768 );35098 );
34769 luma_grid = Grid::Zero(SIDE * SIDE);35099 luma_grid = Grid::Zero(SIDE * SIDE);
34770- u_grid = Grid::Zero(CHROMA_SIDE * CHROMA_SIDE);35100+ u_grid = Grid::Zero(chroma_side * chroma_side);
34771- v_grid = Grid::Zero(CHROMA_SIDE * CHROMA_SIDE);35101+ v_grid = Grid::Zero(chroma_side * chroma_side);
34772 } else {35102 } else {
34773 let around = neighbours.around_mi(leaf_mi, 8);35103 let around = neighbours.around_mi(leaf_mi, 8);
34774 let (grid8, luma_tx_type) = read_inter_luma8(35104 let (grid8, luma_tx_type) = read_inter_luma8(
@@ -34791,15 +35121,15 @@ fn decode_inter_block8(
34791 u_grid = read_inter_plane(35121 u_grid = read_inter_plane(
34792 dec,35122 dec,
34793 cdfs,35123 cdfs,
34794- TxbSet::Chroma4,35124+ chroma_set,
34795- scan4,35125+ chroma_scan,
34796 1,35126 1,
34797 around[1],35127 around[1],
34798 mode_for_tx,35128 mode_for_tx,
34799 u,35129 u,
34800 cpx,35130 cpx,
34801 cpy,35131 cpy,
34802- CHROMA_SIDE,35132+ chroma_side,
34803 base_q_idx,35133 base_q_idx,
34804 su,35134 su,
34805 Some(luma_tx_type),35135 Some(luma_tx_type),
@@ -34809,15 +35139,15 @@ fn decode_inter_block8(
34809 v_grid = read_inter_plane(35139 v_grid = read_inter_plane(
34810 dec,35140 dec,
34811 cdfs,35141 cdfs,
34812- TxbSet::Chroma4,35142+ chroma_set,
34813- scan4,35143+ chroma_scan,
34814 2,35144 2,
34815 around[2],35145 around[2],
34816 mode_for_tx,35146 mode_for_tx,
34817 v,35147 v,
34818 cpx,35148 cpx,
34819 cpy,35149 cpy,
34820- CHROMA_SIDE,35150+ chroma_side,
34821 base_q_idx,35151 base_q_idx,
34822 sv,35152 sv,
34823 Some(luma_tx_type),35153 Some(luma_tx_type),
@@ -34918,7 +35248,7 @@ fn decode_inter_block8(
34918 // The CHROMA neighbour's own `uv_mode` decides the chroma edge filter35248 // The CHROMA neighbour's own `uv_mode` decides the chroma edge filter
34919 // strength (`get_intra_edge_filter_type`, reconintra.c:974), never the35249 // strength (`get_intra_edge_filter_type`, reconintra.c:974), never the
34920 // luma one -- same read [`decode_leaf8`] does.35250 // luma one -- same read [`decode_leaf8`] does.
34921- let smooth_neighbor_uv = neighbours.smooth_uv_neighbour(leaf_mi.0, leaf_mi.1, r, c);35251+ let smooth_neighbor_uv = neighbours.smooth_uv_neighbour(leaf_mi.0, leaf_mi.1, r, c, fctx);
34922 // lane-screen r2: see [`decode_inter_block`]'s own copy -- `mode`/35252 // lane-screen r2: see [`decode_inter_block`]'s own copy -- `mode`/
34923 // `uv_mode` are both effectively `DC_PRED`-gated already on this35253 // `uv_mode` are both effectively `DC_PRED`-gated already on this
34924 // leaf (a nonzero `mode` still passes, but `uv_mode` is forced35254 // leaf (a nonzero `mode` still passes, but `uv_mode` is forced
@@ -35033,14 +35363,17 @@ fn decode_inter_block8(
35033 PaletteY { size: n, colors, map }35363 PaletteY { size: n, colors, map }
35034 });35364 });
35035 let palette_uv = palette_uv_pending.map(|(n, u_colors, v_colors)| {35365 let palette_uv = palette_uv_pending.map(|(n, u_colors, v_colors)| {
35036- // `av1_get_plane_block_size(BLOCK_8X8, 1, 1)` = BLOCK_4X4.35366+ // `av1_get_plane_block_size(BLOCK_8X8, 1, 1)` at the sequence's
35367+ // own subsampling: BLOCK_4X4 at 4:2:0, BLOCK_8X8 at 4:4:4
35368+ // (`chroma_side`, lane-av1-444).
35037 let on = palette_onscreen_uv(35369 let on = palette_onscreen_uv(
35038 (SIDE, SIDE),35370 (SIDE, SIDE),
35039 palette_onscreen(rmi, cmi, SIDE, SIDE, fctx),35371 palette_onscreen(rmi, cmi, SIDE, SIDE, fctx),
35040- (CHROMA_SIDE, CHROMA_SIDE),35372+ (chroma_side, chroma_side),
35373+ (ss_x(fctx), ss_y(fctx)),
35041 );35374 );
35042 let map =35375 let map =
35043- decode_color_index_map_wh(dec, cdfs, n, CHROMA_SIDE, CHROMA_SIDE, on, true);35376+ decode_color_index_map_wh(dec, cdfs, n, chroma_side, chroma_side, on, true);
35044 hit!(PALETTE_UV_HITS);35377 hit!(PALETTE_UV_HITS);
35045 hit!(INTRA_IN_INTER_PALETTE_UV_HITS);35378 hit!(INTRA_IN_INTER_PALETTE_UV_HITS);
35046 hit_do! {35379 hit_do! {
@@ -35214,11 +35547,11 @@ fn decode_inter_block8(
35214 push_intra(1, 35547 push_intra(1,
35215 cpx,35548 cpx,
35216 cpy,35549 cpy,
35217- CHROMA_SIDE,35550+ chroma_side,
35218 uv_predict_mode,35551 uv_predict_mode,
35219 angle_delta_uv,35552 angle_delta_uv,
35220 reach,35553 reach,
35221- &ZERO_RESIDUAL[..CHROMA_SIDE * CHROMA_SIDE],35554+ &ZERO_RESIDUAL[..chroma_side * chroma_side],
35222 alpha.zip(ac).map(|((au, _), ac)| (au, ac)),35555 alpha.zip(ac).map(|((au, _), ac)| (au, ac)),
35223 None,35556 None,
35224 smooth_neighbor_uv, fctx,35557 smooth_neighbor_uv, fctx,
@@ -35229,18 +35562,18 @@ fn decode_inter_block8(
35229 push_intra(2, 35562 push_intra(2,
35230 cpx,35563 cpx,
35231 cpy,35564 cpy,
35232- CHROMA_SIDE,35565+ chroma_side,
35233 uv_predict_mode,35566 uv_predict_mode,
35234 angle_delta_uv,35567 angle_delta_uv,
35235 reach,35568 reach,
35236- &ZERO_RESIDUAL[..CHROMA_SIDE * CHROMA_SIDE],35569+ &ZERO_RESIDUAL[..chroma_side * chroma_side],
35237 alpha.zip(ac).map(|((_, av), ac)| (av, ac)),35570 alpha.zip(ac).map(|((_, av), ac)| (av, ac)),
35238 None,35571 None,
35239 smooth_neighbor_uv, fctx,35572 smooth_neighbor_uv, fctx,
35240 );35573 );
35241 luma_grid = Grid::Zero(SIDE * SIDE);35574 luma_grid = Grid::Zero(SIDE * SIDE);
35242- u_grid = Grid::Zero(CHROMA_SIDE * CHROMA_SIDE);35575+ u_grid = Grid::Zero(chroma_side * chroma_side);
35243- v_grid = Grid::Zero(CHROMA_SIDE * CHROMA_SIDE);35576+ v_grid = Grid::Zero(chroma_side * chroma_side);
35244 } else {35577 } else {
35245 let around = neighbours.around_mi(leaf_mi, 8);35578 let around = neighbours.around_mi(leaf_mi, 8);
35246 luma_grid = if split8 {35579 luma_grid = if split8 {
@@ -35290,8 +35623,8 @@ fn decode_inter_block8(
35290 u_grid = read_plane(35623 u_grid = read_plane(
35291 dec,35624 dec,
35292 cdfs,35625 cdfs,
35293- TxbSet::Chroma4,35626+ chroma_set,
35294- scan4,35627+ chroma_scan,
35295 1,35628 1,
35296 around[1],35629 around[1],
35297 mode,35630 mode,
@@ -35301,8 +35634,8 @@ fn decode_inter_block8(
35301 u,35634 u,
35302 cpx,35635 cpx,
35303 cpy,35636 cpy,
35304- CHROMA_SIDE,35637+ chroma_side,
35305- TX4,35638+ chroma_tx,
35306 base_q_idx,35639 base_q_idx,
35307 alpha.zip(ac).map(|((au, _), ac)| (au, ac)),35640 alpha.zip(ac).map(|((au, _), ac)| (au, ac)),
35308 None,35641 None,
@@ -35315,8 +35648,8 @@ fn decode_inter_block8(
35315 v_grid = read_plane(35648 v_grid = read_plane(
35316 dec,35649 dec,
35317 cdfs,35650 cdfs,
35318- TxbSet::Chroma4,35651+ chroma_set,
35319- scan4,35652+ chroma_scan,
35320 2,35653 2,
35321 around[2],35654 around[2],
35322 mode,35655 mode,
@@ -35326,8 +35659,8 @@ fn decode_inter_block8(
35326 v,35659 v,
35327 cpx,35660 cpx,
35328 cpy,35661 cpy,
35329- CHROMA_SIDE,35662+ chroma_side,
35330- TX4,35663+ chroma_tx,
35331 base_q_idx,35664 base_q_idx,
35332 alpha.zip(ac).map(|((_, av), ac)| (av, ac)),35665 alpha.zip(ac).map(|((_, av), ac)| (av, ac)),
35333 None,35666 None,
crates/ec-av1/src/encode.rsmodified+2 -0
@@ -6500,6 +6500,8 @@ fn search_chroma(
6500 side.min(luma.true_height.saturating_sub(y)),6500 side.min(luma.true_height.saturating_sub(y)),
6501 ),6501 ),
6502 (at.side, at.side),6502 (at.side, at.side),
6503+ // The encoder writes 4:2:0 only (`palette_uv_side` above).
6504+ (1, 1),
6503 );6505 );
6504 for mut pal in palette_candidates(&u_source) {6506 for mut pal in palette_candidates(&u_source) {
6505 let n = usize::from(pal.size);6507 let n = usize::from(pal.size);
crates/ec-av1/src/tile.rsmodified+674 -351
@@ -12,17 +12,17 @@
12 use std::cell::RefCell;12 use std::cell::RefCell;
13 #[cfg(test)]13 #[cfg(test)]
14 use std::collections::BTreeMap;14 use std::collections::BTreeMap;
15-use std::sync::LazyLock;
16 use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering};15 use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering};
16+use std::sync::LazyLock;
17 17
18 use ec_core::{Error, Result};18 use ec_core::{Error, Result};
19 19
20 use crate::cdf;20 use crate::cdf;
21 use crate::cdf_state::{Cdfs, MvComponentCdfs, TxbSet, TxbTables};21 use crate::cdf_state::{Cdfs, MvComponentCdfs, TxbSet, TxbTables};
22-use crate::decode::{TXFM_CTX_INIT, txfm_partition_ctx_rect};22+use crate::decode::{txfm_partition_ctx_rect, TXFM_CTX_INIT};
23 use crate::msac::SymbolEncoder;23 use crate::msac::SymbolEncoder;
24+use crate::mvstack::{find_mv_stack, mv16, MiGrid, MiInfo, NO_REF1};
24 use crate::transform::TxType;25 use crate::transform::TxType;
25-use crate::mvstack::{MiGrid, MiInfo, NO_REF1, find_mv_stack, mv16};
26 26
27 // ---------------------------------------------------------------------------27 // ---------------------------------------------------------------------------
28 // lane-av1lr: per-64x64 `cdef_idx` (spec 5.11.56, `read_cdef`). The decoder28 // lane-av1lr: per-64x64 `cdef_idx` (spec 5.11.56, `read_cdef`). The decoder
@@ -386,9 +386,7 @@ fn write_inter_block_128(
386 let scan32 = default_scan(TX32);386 let scan32 = default_scan(TX32);
387 let unit_of = |plane: usize, cr: usize, cc: usize| -> Vec<i32> {387 let unit_of = |plane: usize, cr: usize, cc: usize| -> Vec<i32> {
388 (0..32)388 (0..32)
389- .flat_map(|row| {389+ .flat_map(|row| packed[plane][(cr * 32 + row) * 64 + cc * 32..][..32].to_vec())
390- packed[plane][(cr * 32 + row) * 64 + cc * 32..][..32].to_vec()
391- })
392 .collect()390 .collect()
393 };391 };
394 let mut chroma_units: Vec<((usize, usize), [Vec<i32>; 2])> = Vec::new();392 let mut chroma_units: Vec<((usize, usize), [Vec<i32>; 2])> = Vec::new();
@@ -659,9 +657,7 @@ fn write_inter_block_128_rect(
659 let scan32 = default_scan(TX32);657 let scan32 = default_scan(TX32);
660 let unit_of = |plane: usize, cr: usize, cc: usize| -> Vec<i32> {658 let unit_of = |plane: usize, cr: usize, cc: usize| -> Vec<i32> {
661 (0..32)659 (0..32)
662- .flat_map(|row| {660+ .flat_map(|row| packed[plane][(cr * 32 + row) * 64 + cc * 32..][..32].to_vec())
663- packed[plane][(cr * 32 + row) * 64 + cc * 32..][..32].to_vec()
664- })
665 .collect()661 .collect()
666 };662 };
667 let mut chroma_units: Vec<((usize, usize), [Vec<i32>; 2])> = Vec::new();663 let mut chroma_units: Vec<((usize, usize), [Vec<i32>; 2])> = Vec::new();
@@ -702,7 +698,8 @@ fn write_inter_block_128_rect(
702 &mut cdfs.txb(TxbSet::Chroma32, 0),698 &mut cdfs.txb(TxbSet::Chroma32, 0),
703 &grid,699 &grid,
704 &scan32,700 &scan32,
705- usize::from(around.above_coded) + usize::from(around.left_coded)701+ usize::from(around.above_coded)
702+ + usize::from(around.left_coded)
706 + 3 * usize::from(is_half),703 + 3 * usize::from(is_half),
707 dc_sign_ctx(around.dc_vote),704 dc_sign_ctx(around.dc_vote),
708 Some(plane),705 Some(plane),
@@ -781,8 +778,7 @@ pub fn take_sb128_ab_hits() -> [usize; 4] {
781 /// lane-b128r: how many of those blocks carried a REAL residual (four778 /// lane-b128r: how many of those blocks carried a REAL residual (four
782 /// TX_64X64 luma units plus their per-chunk chroma pair) rather than being779 /// TX_64X64 luma units plus their per-chunk chroma pair) rather than being
783 /// skipped, since the last [`take_sb128_residual_hits`].780 /// skipped, since the last [`take_sb128_residual_hits`].
784-static SB128_RESIDUAL_HITS: std::sync::atomic::AtomicUsize =781+static SB128_RESIDUAL_HITS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
785- std::sync::atomic::AtomicUsize::new(0);
786 782
787 /// The 128x128-with-residual count since the last call, and zero it.783 /// The 128x128-with-residual count since the last call, and zero it.
788 pub fn take_sb128_residual_hits() -> usize {784 pub fn take_sb128_residual_hits() -> usize {
@@ -792,8 +788,7 @@ pub fn take_sb128_residual_hits() -> usize {
792 /// [`write_inter_block_128_rect`] codes) carried a REAL residual rather than788 /// [`write_inter_block_128_rect`] codes) carried a REAL residual rather than
793 /// being skipped, since the last [`take_sb128_rectres_hits`] -- the writer789 /// being skipped, since the last [`take_sb128_rectres_hits`] -- the writer
794 /// side of [`crate::encode::take_b128_rectres_hits`]'s search count.790 /// side of [`crate::encode::take_b128_rectres_hits`]'s search count.
795-static SB128_RECTRES_HITS: std::sync::atomic::AtomicUsize =791+static SB128_RECTRES_HITS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
796- std::sync::atomic::AtomicUsize::new(0);
797 792
798 /// The rect/AB-piece-with-residual count since the last call, and zero it.793 /// The rect/AB-piece-with-residual count since the last call, and zero it.
799 pub fn take_sb128_rectres_hits() -> usize {794 pub fn take_sb128_rectres_hits() -> usize {
@@ -803,8 +798,7 @@ pub fn take_sb128_rectres_hits() -> usize {
803 /// lane-b128r: how many of those blocks named a SECOND reference (the798 /// lane-b128r: how many of those blocks named a SECOND reference (the
804 /// compound arm at the 128 root), since the last799 /// compound arm at the 128 root), since the last
805 /// [`take_sb128_compound_hits`].800 /// [`take_sb128_compound_hits`].
806-static SB128_COMPOUND_HITS: std::sync::atomic::AtomicUsize =801+static SB128_COMPOUND_HITS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
807- std::sync::atomic::AtomicUsize::new(0);
808 802
809 /// The compound-128 count since the last call, and zero it.803 /// The compound-128 count since the last call, and zero it.
810 pub fn take_sb128_compound_hits() -> usize {804 pub fn take_sb128_compound_hits() -> usize {
@@ -838,12 +832,7 @@ fn write_lr(enc: &mut SymbolEncoder, cdfs: &mut Cdfs, mi_row: u32, mi_col: u32,
838 let info = plan.units[(rcol + rrow * plan.horz_units) as usize];832 let info = plan.units[(rcol + rrow * plan.horz_units) as usize];
839 enc.symbol(usize::from(info.is_some()), &mut cdfs.restore_wiener);833 enc.symbol(usize::from(info.is_some()), &mut cdfs.restore_wiener);
840 if let Some(info) = info {834 if let Some(info) = info {
841- crate::restoration::write_wiener_filter(835+ crate::restoration::write_wiener_filter(enc, false, &mut plan.reference, &info);
842- enc,
843- false,
844- &mut plan.reference,
845- &info,
846- );
847 }836 }
848 }837 }
849 }838 }
@@ -919,7 +908,10 @@ impl TileLayout {
919 let starts = |total: u32, log2: u32| {908 let starts = |total: u32, log2: u32| {
920 let total128 = total.div_ceil(unit);909 let total128 = total.div_ceil(unit);
921 let step = total128.div_ceil(1 << log2).max(1);910 let step = total128.div_ceil(1 << log2).max(1);
922- let mut v: Vec<u32> = (0..total128).step_by(step as usize).map(|s| s * unit).collect();911+ let mut v: Vec<u32> = (0..total128)
912+ .step_by(step as usize)
913+ .map(|s| s * unit)
914+ .collect();
923 v.push(total);915 v.push(total);
924 v916 v
925 };917 };
@@ -1067,11 +1059,9 @@ pub(crate) fn cdef_unit_owner(blocks: &[Quadrant], mi_cols: u32, mi_rows: u32) -
1067 // (its two squares are one unit each); only a non-skip one1059 // (its two squares are one unit each); only a non-skip one
1068 // codes a literal, so only it fires.1060 // codes a literal, so only it fires.
1069 Quadrant::Ab128(symbol, pieces) => {1061 Quadrant::Ab128(symbol, pieces) => {
1070- for (piece, ((mi_r, mi_c), (w, h))) in1062+ for (piece, ((mi_r, mi_c), (w, h))) in pieces
1071- pieces.iter().zip(crate::encode::ab128_pieces(1063+ .iter()
1072- usize::from(*symbol),1064+ .zip(crate::encode::ab128_pieces(usize::from(*symbol), (0, 0)))
1073- (0, 0),
1074- ))
1075 {1065 {
1076 if piece.skip {1066 if piece.skip {
1077 continue;1067 continue;
@@ -1111,7 +1101,12 @@ thread_local! {
1111 /// carries no literal at all).1101 /// carries no literal at all).
1112 pub(crate) fn arm_cdef_idx(bits: u8, sb_cols: usize, grid: Vec<u8>) {1102 pub(crate) fn arm_cdef_idx(bits: u8, sb_cols: usize, grid: Vec<u8>) {
1113 CDEF_IDX.with(|c| {1103 CDEF_IDX.with(|c| {
1114- *c.borrow_mut() = (bits > 0).then(|| CdefIdxPlan { bits, sb_cols, grid, last: None });1104+ *c.borrow_mut() = (bits > 0).then(|| CdefIdxPlan {
1105+ bits,
1106+ sb_cols,
1107+ grid,
1108+ last: None,
1109+ });
1115 });1110 });
1116 }1111 }
1117 1112
@@ -1143,8 +1138,12 @@ thread_local! {
1143 /// lane-deltaq) when `res == 0`.1138 /// lane-deltaq) when `res == 0`.
1144 pub(crate) fn arm_delta_q(res: i32, sb_cols: usize, grid: Vec<u8>, base: u8) {1139 pub(crate) fn arm_delta_q(res: i32, sb_cols: usize, grid: Vec<u8>, base: u8) {
1145 DELTA_Q.with(|c| {1140 DELTA_Q.with(|c| {
1146- *c.borrow_mut() =1141+ *c.borrow_mut() = (res > 0).then(|| DeltaQPlan {
1147- (res > 0).then(|| DeltaQPlan { res, sb_cols, grid, cur: i32::from(base) });1142+ res,
1143+ sb_cols,
1144+ grid,
1145+ cur: i32::from(base),
1146+ });
1148 });1147 });
1149 }1148 }
1150 1149
@@ -1285,14 +1284,23 @@ fn write_filter_intra(
1285 filter_intra: Option<u8>,1284 filter_intra: Option<u8>,
1286 ) {1285 ) {
1287 if !filter_intra_armed() || mode != DC_PRED || has_palette_y {1286 if !filter_intra_armed() || mode != DC_PRED || has_palette_y {
1288- debug_assert!(filter_intra.is_none(), "no `use_filter_intra` symbol exists here");1287+ debug_assert!(
1288+ filter_intra.is_none(),
1289+ "no `use_filter_intra` symbol exists here"
1290+ );
1289 return;1291 return;
1290 }1292 }
1291 let Some(class) = crate::decode::filter_intra_size_class(side) else {1293 let Some(class) = crate::decode::filter_intra_size_class(side) else {
1292- debug_assert!(filter_intra.is_none(), "past av1_filter_intra_allowed_bsize");1294+ debug_assert!(
1295+ filter_intra.is_none(),
1296+ "past av1_filter_intra_allowed_bsize"
1297+ );
1293 return;1298 return;
1294 };1299 };
1295- enc.symbol(usize::from(filter_intra.is_some()), &mut cdfs.filter_intra[class]);1300+ enc.symbol(
1301+ usize::from(filter_intra.is_some()),
1302+ &mut cdfs.filter_intra[class],
1303+ );
1296 if let Some(fi) = filter_intra {1304 if let Some(fi) = filter_intra {
1297 enc.symbol(usize::from(fi), &mut cdfs.filter_intra_mode);1305 enc.symbol(usize::from(fi), &mut cdfs.filter_intra_mode);
1298 FILTER_INTRA_HITS[0].fetch_add(1, std::sync::atomic::Ordering::Relaxed);1306 FILTER_INTRA_HITS[0].fetch_add(1, std::sync::atomic::Ordering::Relaxed);
@@ -1525,7 +1533,12 @@ fn write_motion_mode(
1525 /// a single-reference or intra block, and `record_compound_mi`'s overwrite of1533 /// a single-reference or intra block, and `record_compound_mi`'s overwrite of
1526 /// the ALTREF default for a compound one (this writer only codes1534 /// the ALTREF default for a compound one (this writer only codes
1527 /// `comp_group_idx == 0`, `compound_idx == 1`).1535 /// `comp_group_idx == 0`, `compound_idx == 1`).
1528-fn record_block_compound(neighbours: &mut Neighbours, at_mi: (usize, usize), side: usize, block: &BlockCoeffs) {1536+fn record_block_compound(
1537+ neighbours: &mut Neighbours,
1538+ at_mi: (usize, usize),
1539+ side: usize,
1540+ block: &BlockCoeffs,
1541+) {
1529 if let Some(r1) = block.inter.and_then(|i| i.ref1) {1542 if let Some(r1) = block.inter.and_then(|i| i.ref1) {
1530 neighbours.record_compound_mi(at_mi, side, r1, 0, 1);1543 neighbours.record_compound_mi(at_mi, side, r1, 0, 1);
1531 }1544 }
@@ -1577,14 +1590,19 @@ pub(crate) fn high_precision_mv() -> bool {
1577 /// `allow_high_precision_mv`, one per non-zero mv component with it), `[1]`1590 /// `allow_high_precision_mv`, one per non-zero mv component with it), `[1]`
1578 /// counts the ones that came out ZERO -- an eighth-pel vector, i.e. one a1591 /// counts the ones that came out ZERO -- an eighth-pel vector, i.e. one a
1579 /// frame without the flag cannot name at all.1592 /// frame without the flag cannot name at all.
1580-static HP_SYMBOLS: [std::sync::atomic::AtomicU64; 2] =1593+static HP_SYMBOLS: [std::sync::atomic::AtomicU64; 2] = [
1581- [std::sync::atomic::AtomicU64::new(0), std::sync::atomic::AtomicU64::new(0)];1594+ std::sync::atomic::AtomicU64::new(0),
1595+ std::sync::atomic::AtomicU64::new(0),
1596+];
1582 1597
1583 /// Reads and clears [`HP_SYMBOLS`]: `(hp symbols coded, eighth-pel ones)`.1598 /// Reads and clears [`HP_SYMBOLS`]: `(hp symbols coded, eighth-pel ones)`.
1584 #[allow(dead_code)] // read only from the `#[cfg(test)]` witness1599 #[allow(dead_code)] // read only from the `#[cfg(test)]` witness
1585 pub(crate) fn take_hp_symbols() -> (u64, u64) {1600 pub(crate) fn take_hp_symbols() -> (u64, u64) {
1586 let all = HP_SYMBOLS[0].swap(0, std::sync::atomic::Ordering::Relaxed);1601 let all = HP_SYMBOLS[0].swap(0, std::sync::atomic::Ordering::Relaxed);
1587- (all, HP_SYMBOLS[1].swap(0, std::sync::atomic::Ordering::Relaxed))1602+ (
1603+ all,
1604+ HP_SYMBOLS[1].swap(0, std::sync::atomic::Ordering::Relaxed),
1605+ )
1588 }1606 }
1589 1607
1590 /// [`take_hp_symbols`] without clearing -- what the NEWMV census prints.1608 /// [`take_hp_symbols`] without clearing -- what the NEWMV census prints.
@@ -1611,10 +1629,10 @@ fn write_compound_ref_frames(
1611 (ref0, ref1): (i8, i8),1629 (ref0, ref1): (i8, i8),
1612 ) -> Result<()> {1630 ) -> Result<()> {
1613 use crate::mvstack::{1631 use crate::mvstack::{
1632+ comp_reference_type_ctx, single_ref_p1_ctx, single_ref_p2_ctx, single_ref_p3_ctx,
1633+ single_ref_p4_ctx, single_ref_p5_ctx, single_ref_p6_ctx, uni_comp_ref_p1_ctx,
1614 ALTREF2_FRAME, ALTREF_FRAME, BWDREF_FRAME, GOLDEN_FRAME, LAST2_FRAME, LAST3_FRAME,1634 ALTREF2_FRAME, ALTREF_FRAME, BWDREF_FRAME, GOLDEN_FRAME, LAST2_FRAME, LAST3_FRAME,
1615- LAST_FRAME, comp_reference_type_ctx, single_ref_p1_ctx, single_ref_p2_ctx,1635+ LAST_FRAME,
1616- single_ref_p3_ctx, single_ref_p4_ctx, single_ref_p5_ctx, single_ref_p6_ctx,
1617- uni_comp_ref_p1_ctx,
1618 };1636 };
1619 let a = (above_ref > 0).then_some(above_ref);1637 let a = (above_ref > 0).then_some(above_ref);
1620 let l = (left_ref > 0).then_some(left_ref);1638 let l = (left_ref > 0).then_some(left_ref);
@@ -1810,14 +1828,20 @@ fn write_compound_block(
1810 let mut walk = 1usize;1828 let mut walk = 1usize;
1811 while walk < 3 && stack.entries.len() > walk + 1 {1829 while walk < 3 && stack.entries.len() > walk + 1 {
1812 let advance = walk < idx;1830 let advance = walk < idx;
1813- enc.symbol(usize::from(advance), &mut cdfs.drl_mode[stack.drl_ctx[walk]]);1831+ enc.symbol(
1832+ usize::from(advance),
1833+ &mut cdfs.drl_mode[stack.drl_ctx[walk]],
1834+ );
1814 if !advance {1835 if !advance {
1815 break;1836 break;
1816 }1837 }
1817 walk += 1;1838 walk += 1;
1818 }1839 }
1819 let near = |i: usize| {1840 let near = |i: usize| {
1820- stack.entries.get(i).map_or(stack.near_mv, |e| (e.mv0, e.mv1))1841+ stack
1842+ .entries
1843+ .get(i)
1844+ .map_or(stack.near_mv, |e| (e.mv0, e.mv1))
1821 };1845 };
1822 if walk != idx && near(walk) != near(idx) {1846 if walk != idx && near(walk) != near(idx) {
1823 note_drl_clamp(idx, walk, stack.entries.len());1847 note_drl_clamp(idx, walk, stack.entries.len());
@@ -1845,14 +1869,20 @@ fn write_compound_block(
1845 let mut walk = 0usize;1869 let mut walk = 0usize;
1846 while walk < 2 && stack.entries.len() > walk + 1 {1870 while walk < 2 && stack.entries.len() > walk + 1 {
1847 let advance = walk < idx;1871 let advance = walk < idx;
1848- enc.symbol(usize::from(advance), &mut cdfs.drl_mode[stack.drl_ctx[walk]]);1872+ enc.symbol(
1873+ usize::from(advance),
1874+ &mut cdfs.drl_mode[stack.drl_ctx[walk]],
1875+ );
1849 if !advance {1876 if !advance {
1850 break;1877 break;
1851 }1878 }
1852 walk += 1;1879 walk += 1;
1853 }1880 }
1854 let nearest = |i: usize| {1881 let nearest = |i: usize| {
1855- stack.entries.get(i).map_or(stack.nearest_mv, |e| (e.mv0, e.mv1))1882+ stack
1883+ .entries
1884+ .get(i)
1885+ .map_or(stack.nearest_mv, |e| (e.mv0, e.mv1))
1856 };1886 };
1857 if walk != idx && nearest(walk) != nearest(idx) {1887 if walk != idx && nearest(walk) != nearest(idx) {
1858 note_drl_clamp(idx, walk, stack.entries.len());1888 note_drl_clamp(idx, walk, stack.entries.len());
@@ -1993,7 +2023,11 @@ fn write_delta_q(
1993 // under `EC_AV1_SB128` -- decode.rs `maybe_read_delta_q` gates on2023 // under `EC_AV1_SB128` -- decode.rs `maybe_read_delta_q` gates on
1994 // `sb_mi_cur`, and a writer gating on a hardcoded 64 codes three2024 // `sb_mi_cur`, and a writer gating on a hardcoded 64 codes three
1995 // symbol groups per 128 root the reader never reads.2025 // symbol groups per 128 root the reader never reads.
1996- let sb_mi = if sb128_armed() { SB_MI as usize * 2 } else { SB_MI as usize };2026+ let sb_mi = if sb128_armed() {
2027+ SB_MI as usize * 2
2028+ } else {
2029+ SB_MI as usize
2030+ };
1997 if mi.0 % sb_mi != 0 || mi.1 % sb_mi != 0 {2031 if mi.0 % sb_mi != 0 || mi.1 % sb_mi != 0 {
1998 return;2032 return;
1999 }2033 }
@@ -2820,9 +2854,9 @@ impl Quadrant {
2820 Quadrant::Whole(block) | Quadrant::Whole64(block) | Quadrant::Whole128(block) => {2854 Quadrant::Whole(block) | Quadrant::Whole64(block) | Quadrant::Whole128(block) => {
2821 std::slice::from_ref(block)2855 std::slice::from_ref(block)
2822 }2856 }
2823- Quadrant::Split(blocks)2857+ Quadrant::Split(blocks) | Quadrant::Rect128(_, blocks) | Quadrant::Ab128(_, blocks) => {
2824- | Quadrant::Rect128(_, blocks)2858+ blocks.as_slice()
2825- | Quadrant::Ab128(_, blocks) => blocks.as_slice(),2859+ }
2826 Quadrant::Covered => &[],2860 Quadrant::Covered => &[],
2827 }2861 }
2828 }2862 }
@@ -3040,8 +3074,12 @@ impl Neighbours {
3040 fn start_row(&mut self) {3074 fn start_row(&mut self) {
3041 self.left.iter_mut().for_each(|l| *l = Default::default());3075 self.left.iter_mut().for_each(|l| *l = Default::default());
3042 self.left_mode.iter_mut().for_each(|m| *m = DC_PRED);3076 self.left_mode.iter_mut().for_each(|m| *m = DC_PRED);
3043- self.left_side.iter_mut().for_each(|s| *s = NO_NEIGHBOUR_SIDE);3077+ self.left_side
3044- self.left_side_mi.iter_mut().for_each(|s| *s = NO_NEIGHBOUR_SIDE);3078+ .iter_mut()
3079+ .for_each(|s| *s = NO_NEIGHBOUR_SIDE);
3080+ self.left_side_mi
3081+ .iter_mut()
3082+ .for_each(|s| *s = NO_NEIGHBOUR_SIDE);
3045 self.left_skip.iter_mut().for_each(|s| *s = false);3083 self.left_skip.iter_mut().for_each(|s| *s = false);
3046 self.left_inter.iter_mut().for_each(|i| *i = false);3084 self.left_inter.iter_mut().for_each(|i| *i = false);
3047 self.left_ref.iter_mut().for_each(|r| *r = -1);3085 self.left_ref.iter_mut().for_each(|r| *r = -1);
@@ -3052,7 +3090,9 @@ impl Neighbours {
3052 self.left_txfm.iter_mut().for_each(|t| *t = TXFM_CTX_INIT);3090 self.left_txfm.iter_mut().for_each(|t| *t = TXFM_CTX_INIT);
3053 self.left_palette_size.iter_mut().for_each(|s| *s = 0);3091 self.left_palette_size.iter_mut().for_each(|s| *s = 0);
3054 self.left_palette_uv_size.iter_mut().for_each(|s| *s = 0);3092 self.left_palette_uv_size.iter_mut().for_each(|s| *s = 0);
3055- self.left_palette_uv_colors.iter_mut().for_each(|c| *c = [0u16; 8]);3093+ self.left_palette_uv_colors
3094+ .iter_mut()
3095+ .for_each(|c| *c = [0u16; 8]);
3056 }3096 }
3057 3097
3058 /// `av1_get_palette_mode_ctx` + `av1_get_palette_cache`, mirrored from3098 /// `av1_get_palette_mode_ctx` + `av1_get_palette_cache`, mirrored from
@@ -3069,12 +3109,18 @@ impl Neighbours {
3069 let above_ok = r % 16 != 0;3109 let above_ok = r % 16 != 0;
3070 let above_is_palette = r > 0 && self.above_palette_size[c] > 0;3110 let above_is_palette = r > 0 && self.above_palette_size[c] > 0;
3071 let (above_n, above_colors) = if above_ok && self.above_palette_size[c] > 0 {3111 let (above_n, above_colors) = if above_ok && self.above_palette_size[c] > 0 {
3072- (usize::from(self.above_palette_size[c]), self.above_palette_colors[c])3112+ (
3113+ usize::from(self.above_palette_size[c]),
3114+ self.above_palette_colors[c],
3115+ )
3073 } else {3116 } else {
3074 (0, [0u16; 8])3117 (0, [0u16; 8])
3075 };3118 };
3076 let (left_n, left_colors) = if c > 0 && self.left_palette_size[r] > 0 {3119 let (left_n, left_colors) = if c > 0 && self.left_palette_size[r] > 0 {
3077- (usize::from(self.left_palette_size[r]), self.left_palette_colors[r])3120+ (
3121+ usize::from(self.left_palette_size[r]),
3122+ self.left_palette_colors[r],
3123+ )
3078 } else {3124 } else {
3079 (0, [0u16; 8])3125 (0, [0u16; 8])
3080 };3126 };
@@ -3167,7 +3213,13 @@ impl Neighbours {
3167 /// [`Self::record_palette_y`] for the chroma bands (decode.rs3213 /// [`Self::record_palette_y`] for the chroma bands (decode.rs
3168 /// `record_palette_uv_rect`) -- `size == 0` clears stale state the same3214 /// `record_palette_uv_rect`) -- `size == 0` clears stale state the same
3169 /// way, and the span is in LUMA pixels for both planes' bands alike.3215 /// way, and the span is in LUMA pixels for both planes' bands alike.
3170- fn record_palette_uv(&mut self, (r, c): (usize, usize), (w, h): (usize, usize), size: u8, colors: [u16; 8]) {3216+ fn record_palette_uv(
3217+ &mut self,
3218+ (r, c): (usize, usize),
3219+ (w, h): (usize, usize),
3220+ size: u8,
3221+ colors: [u16; 8],
3222+ ) {
3171 // lane-b128hv: the above band spans the block's WIDTH and the left3223 // lane-b128hv: the above band spans the block's WIDTH and the left
3172 // band its HEIGHT; identical to the one loop this replaces at w == h.3224 // band its HEIGHT; identical to the one loop this replaces at w == h.
3173 for cell in 0..(w / MI).max(1) {3225 for cell in 0..(w / MI).max(1) {
@@ -3191,7 +3243,13 @@ impl Neighbours {
3191 /// What a just-written block leaves in the palette bands (decode.rs3243 /// What a just-written block leaves in the palette bands (decode.rs
3192 /// `record_palette_y_rect`): its size and colours over every 4x4 unit it3244 /// `record_palette_y_rect`): its size and colours over every 4x4 unit it
3193 /// spans, `0` for a block that took no palette.3245 /// spans, `0` for a block that took no palette.
3194- fn record_palette_y(&mut self, (r, c): (usize, usize), (w, h): (usize, usize), size: u8, colors: [u16; 8]) {3246+ fn record_palette_y(
3247+ &mut self,
3248+ (r, c): (usize, usize),
3249+ (w, h): (usize, usize),
3250+ size: u8,
3251+ colors: [u16; 8],
3252+ ) {
3195 // lane-b128hv: the above band spans the block's WIDTH and the left3253 // lane-b128hv: the above band spans the block's WIDTH and the left
3196 // band its HEIGHT; identical to the one loop this replaces at w == h.3254 // band its HEIGHT; identical to the one loop this replaces at w == h.
3197 for cell in 0..(w / MI).max(1) {3255 for cell in 0..(w / MI).max(1) {
@@ -3216,7 +3274,13 @@ impl Neighbours {
3216 /// of an INTER frame leaves in the two `TXFM_CONTEXT` bands -- its3274 /// of an INTER frame leaves in the two `TXFM_CONTEXT` bands -- its
3217 /// resolved transform size over the span `(w_px, h_px)`, which for a3275 /// resolved transform size over the span `(w_px, h_px)`, which for a
3218 /// SKIPPED inter block is its own block size instead.3276 /// SKIPPED inter block is its own block size instead.
3219- fn record_txfm(&mut self, (mi_r, mi_c): (usize, usize), tx_px: usize, w_px: usize, h_px: usize) {3277+ fn record_txfm(
3278+ &mut self,
3279+ (mi_r, mi_c): (usize, usize),
3280+ tx_px: usize,
3281+ w_px: usize,
3282+ h_px: usize,
3283+ ) {
3220 for i in 0..h_px / MI {3284 for i in 0..h_px / MI {
3221 if let Some(cell) = self.left_txfm.get_mut(mi_r + i) {3285 if let Some(cell) = self.left_txfm.get_mut(mi_r + i) {
3222 *cell = tx_px as u8;3286 *cell = tx_px as u8;
@@ -3492,9 +3556,22 @@ impl Neighbours {
3492 /// Writes one inter-frame block's skip flag and inter/intra state into3556 /// Writes one inter-frame block's skip flag and inter/intra state into
3493 /// every 16x16 column and row it covers, the same span [`Self::record`]3557 /// every 16x16 column and row it covers, the same span [`Self::record`]
3494 /// fills for the coefficient and mode state.3558 /// fills for the coefficient and mode state.
3495- fn record_inter(&mut self, at: (usize, usize), side: usize, skip: bool, is_inter: bool, ref_frame: i8) {3559+ fn record_inter(
3560+ &mut self,
3561+ at: (usize, usize),
3562+ side: usize,
3563+ skip: bool,
3564+ is_inter: bool,
3565+ ref_frame: i8,
3566+ ) {
3496 let (r, c) = at;3567 let (r, c) = at;
3497- self.record_inter_mi((r * (SUB / MI), c * (SUB / MI)), side, skip, is_inter, ref_frame);3568+ self.record_inter_mi(
3569+ (r * (SUB / MI), c * (SUB / MI)),
3570+ side,
3571+ skip,
3572+ is_inter,
3573+ ref_frame,
3574+ );
3498 }3575 }
3499 3576
3500 /// lane-b128hv: [`Self::record_inter_mi`] with independent above (width)3577 /// lane-b128hv: [`Self::record_inter_mi`] with independent above (width)
@@ -3572,7 +3649,14 @@ impl Neighbours {
3572 3649
3573 /// [`Self::record_inter`] taking the block's position directly in 4x43650 /// [`Self::record_inter`] taking the block's position directly in 4x4
3574 /// mode-info units, for a block finer than one [`SUB`] slot.3651 /// mode-info units, for a block finer than one [`SUB`] slot.
3575- fn record_inter_mi(&mut self, (mi_r, mi_c): (usize, usize), side: usize, skip: bool, is_inter: bool, ref_frame: i8) {3652+ fn record_inter_mi(
3653+ &mut self,
3654+ (mi_r, mi_c): (usize, usize),
3655+ side: usize,
3656+ skip: bool,
3657+ is_inter: bool,
3658+ ref_frame: i8,
3659+ ) {
3576 for cell in 0..side / MI {3660 for cell in 0..side / MI {
3577 self.above_skip[mi_c + cell] = skip;3661 self.above_skip[mi_c + cell] = skip;
3578 self.left_skip[mi_r + cell] = skip;3662 self.left_skip[mi_r + cell] = skip;
@@ -3838,7 +3922,16 @@ pub(crate) fn sb_coeff_key_frame_tile_cdfs(
3838 neighbours.start_row();3922 neighbours.start_row();
3839 }3923 }
3840 if sb128 && sb_start {3924 if sb128 && sb_start {
3841- write_sb128_root(&mut enc, &mut cdfs, &neighbours, sb_r, sb_c, mi_cols, mi_rows, PARTITION_SPLIT);3925+ write_sb128_root(
3926+ &mut enc,
3927+ &mut cdfs,
3928+ &neighbours,
3929+ sb_r,
3930+ sb_c,
3931+ mi_cols,
3932+ mi_rows,
3933+ PARTITION_SPLIT,
3934+ );
3842 }3935 }
3843 {3936 {
3844 if !sb128 {3937 if !sb128 {
@@ -4316,7 +4409,12 @@ fn write_palette_colors_y(enc: &mut SymbolEncoder, colors: &[u16], cache: &[u16]
4316 /// below sees, so it only ever makes the encoder take FEWER chroma palettes4409 /// below sees, so it only ever makes the encoder take FEWER chroma palettes
4317 /// than it should -- never a wrong stream. Upgrade path: price both forms4410 /// than it should -- never a wrong stream. Upgrade path: price both forms
4318 /// here and write the cheaper one under its own leading bit.4411 /// here and write the cheaper one under its own leading bit.
4319-fn write_palette_colors_uv(enc: &mut SymbolEncoder, u_colors: &[u16], v_colors: &[u16], cache: &[u16]) {4412+fn write_palette_colors_uv(
4413+ enc: &mut SymbolEncoder,
4414+ u_colors: &[u16],
4415+ v_colors: &[u16],
4416+ cache: &[u16],
4417+) {
4320 let n = u_colors.len();4418 let n = u_colors.len();
4321 let mut cached: Vec<u16> = Vec::with_capacity(n);4419 let mut cached: Vec<u16> = Vec::with_capacity(n);
4322 for &c in cache {4420 for &c in cache {
@@ -4441,7 +4539,14 @@ pub(crate) fn palette_uv_bits(pal: &PaletteUv, side: usize, on: (usize, usize))
4441 enc.symbol(n - 2, &mut size_cdf);4539 enc.symbol(n - 2, &mut size_cdf);
4442 write_palette_colors_uv(&mut enc, &pal.u_colors[..n], &pal.v_colors[..n], &[]);4540 write_palette_colors_uv(&mut enc, &pal.u_colors[..n], &pal.v_colors[..n], &[]);
4443 let mut idx_cdfs = cdf::PALETTE_UV_COLOR_INDEX[n - 2];4541 let mut idx_cdfs = cdf::PALETTE_UV_COLOR_INDEX[n - 2];
4444- write_color_index_map(&mut enc, &mut idx_cdfs, &pal.map, palette_uv_side(side), n, on);4542+ write_color_index_map(
4543+ &mut enc,
4544+ &mut idx_cdfs,
4545+ &pal.map,
4546+ palette_uv_side(side),
4547+ n,
4548+ on,
4549+ );
4445 enc.bits()4550 enc.bits()
4446 }4551 }
4447 4552
@@ -4552,7 +4657,12 @@ fn write_dv_component(enc: &mut SymbolEncoder, c: &mut MvComponentCdfs, diff: i3
4552 4657
4553 /// One intrabc block's DV as a residual against [`intrabc_dv_pred`], off the4658 /// One intrabc block's DV as a residual against [`intrabc_dv_pred`], off the
4554 /// `dv` nmv context (decode.rs `read_intrabc_dv` -> `read_mv`).4659 /// `dv` nmv context (decode.rs `read_intrabc_dv` -> `read_mv`).
4555-fn write_dv(enc: &mut SymbolEncoder, cdfs: &mut Cdfs, dv: (i32, i32), pred: (i32, i32)) -> Result<()> {4660+fn write_dv(
4661+ enc: &mut SymbolEncoder,
4662+ cdfs: &mut Cdfs,
4663+ dv: (i32, i32),
4664+ pred: (i32, i32),
4665+) -> Result<()> {
4556 let diff = (dv.0 - pred.0, dv.1 - pred.1);4666 let diff = (dv.0 - pred.0, dv.1 - pred.1);
4557 let joint = match (diff.0 != 0, diff.1 != 0) {4667 let joint = match (diff.0 != 0, diff.1 != 0) {
4558 (false, false) => 0,4668 (false, false) => 0,
@@ -4584,7 +4694,8 @@ fn write_intrabc_block(
4584 mi: (usize, usize),4694 mi: (usize, usize),
4585 side: usize,4695 side: usize,
4586 ) -> Result<()> {4696 ) -> Result<()> {
4587- let skip_ctx = usize::from(neighbours.above_skip[mi.1]) + usize::from(neighbours.left_skip[mi.0]);4697+ let skip_ctx =
4698+ usize::from(neighbours.above_skip[mi.1]) + usize::from(neighbours.left_skip[mi.0]);
4588 enc.symbol(1, &mut cdfs.skip[skip_ctx]);4699 enc.symbol(1, &mut cdfs.skip[skip_ctx]);
4589 write_cdef_idx(enc, mi, true);4700 write_cdef_idx(enc, mi, true);
4590 enc.symbol(1, &mut cdfs.intrabc);4701 enc.symbol(1, &mut cdfs.intrabc);
@@ -4705,7 +4816,8 @@ fn write_intra_mode(
4705 // above + left. Every block this writer codes outside an `allow_intrabc`4816 // above + left. Every block this writer codes outside an `allow_intrabc`
4706 // frame is unskipped, so both bands stay false and this stays 0 -- the4817 // frame is unskipped, so both bands stay false and this stays 0 -- the
4707 // literal every stream before this lane was written with.4818 // literal every stream before this lane was written with.
4708- let skip_ctx = usize::from(neighbours.above_skip[mi.1]) + usize::from(neighbours.left_skip[mi.0]);4819+ let skip_ctx =
4820+ usize::from(neighbours.above_skip[mi.1]) + usize::from(neighbours.left_skip[mi.0]);
4709 enc.symbol(0, &mut cdfs.skip[skip_ctx]);4821 enc.symbol(0, &mut cdfs.skip[skip_ctx]);
4710 write_cdef_idx(enc, mi, false);4822 write_cdef_idx(enc, mi, false);
4711 // `read_intrabc_info` (spec 5.11.13): the flag is read for EVERY intra4823 // `read_intrabc_info` (spec 5.11.13): the flag is read for EVERY intra
@@ -4737,7 +4849,9 @@ fn write_intra_mode(
4737 write_cfl_alphas(4849 write_cfl_alphas(
4738 enc,4850 enc,
4739 cdfs,4851 cdfs,
4740- block.cfl_alphas.expect("a UV_CFL_PRED block carries its alphas"),4852+ block
4853+ .cfl_alphas
4854+ .expect("a UV_CFL_PRED block carries its alphas"),
4741 );4855 );
4742 }4856 }
4743 // `angle_delta_uv` (spec `read_intra_angle_info`) off the same CDF array4857 // `angle_delta_uv` (spec `read_intra_angle_info`) off the same CDF array
@@ -4858,7 +4972,8 @@ fn write_palette_syntax(
4858 &pal.map,4972 &pal.map,
4859 uv_side,4973 uv_side,
4860 n,4974 n,
4861- crate::decode::palette_onscreen_uv((side, side), on, (uv_side, uv_side)),4975+ // The encoder writes 4:2:0 only (`palette_uv_side` above).
4976+ crate::decode::palette_onscreen_uv((side, side), on, (uv_side, uv_side), (1, 1)),
4862 );4977 );
4863 note_palette_uv(n);4978 note_palette_uv(n);
4864 }4979 }
@@ -4890,8 +5005,7 @@ static PALETTE_HITS: [std::sync::atomic::AtomicUsize; 9] =
4890 /// i.e. palette blocks this writer cut at the frame's right/bottom mi edge.5005 /// i.e. palette blocks this writer cut at the frame's right/bottom mi edge.
4891 /// A gate asserts it non-zero so it cannot pass on a stream that never cuts5006 /// A gate asserts it non-zero so it cannot pass on a stream that never cuts
4892 /// one (class `gate-blind-to-feature`).5007 /// one (class `gate-blind-to-feature`).
4893-static PALETTE_CUT_MAPS: std::sync::atomic::AtomicUsize =5008+static PALETTE_CUT_MAPS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
4894- std::sync::atomic::AtomicUsize::new(0);
4895 5009
4896 /// Current value of [`PALETTE_CUT_MAPS`] (colour-index maps).5010 /// Current value of [`PALETTE_CUT_MAPS`] (colour-index maps).
4897 pub fn palette_cut_maps_written() -> usize {5011 pub fn palette_cut_maps_written() -> usize {
@@ -4975,15 +5089,7 @@ fn write_block(
4975 }5089 }
4976 let (above_mode, left_mode) = (neighbours.above_mode[c], neighbours.left_mode[r]);5090 let (above_mode, left_mode) = (neighbours.above_mode[c], neighbours.left_mode[r]);
4977 let mode = write_intra_mode(5091 let mode = write_intra_mode(
4978- enc,5092+ enc, cdfs, neighbours, block, above_mode, left_mode, cfl, at_mi, side,
4979- cdfs,
4980- neighbours,
4981- block,
4982- above_mode,
4983- left_mode,
4984- cfl,
4985- at_mi,
4986- side,
4987 );5093 );
4988 // The coefficient tables read `fimode_to_intradir[filter_intra_mode]`'s5094 // The coefficient tables read `fimode_to_intradir[filter_intra_mode]`'s
4989 // row on a filter-intra block; the neighbour publication below still gets5095 // row on a filter-intra block; the neighbour publication below still gets
@@ -5085,7 +5191,9 @@ fn write_leaf8(
5085 // true first divergence (a differently-sized alphabet under the same5191 // true first divergence (a differently-sized alphabet under the same
5086 // DC_PRED decision desyncs the coder even though the decoded mode is5192 // DC_PRED decision desyncs the coder even though the decoded mode is
5087 // unchanged).5193 // unchanged).
5088- let mode = write_intra_mode(enc, cdfs, neighbours, block, above_mode, left_mode, true, leaf_mi, 8);5194+ let mode = write_intra_mode(
5195+ enc, cdfs, neighbours, block, above_mode, left_mode, true, leaf_mi, 8,
5196+ );
5089 let planes = [TxbSet::Luma8, TxbSet::Chroma4, TxbSet::Chroma4];5197 let planes = [TxbSet::Luma8, TxbSet::Chroma4, TxbSet::Chroma4];
5090 // As [`write_block`]: the coefficient row follows the filter-intra mode,5198 // As [`write_block`]: the coefficient row follows the filter-intra mode,
5091 // the mode this leaf publishes (and returns to the next leaf) does not.5199 // the mode this leaf publishes (and returns to the next leaf) does not.
@@ -5218,7 +5326,11 @@ fn write_block_planes(
5218 /// The single luma transform type a block carries, `DCT_DCT` for one that5326 /// The single luma transform type a block carries, `DCT_DCT` for one that
5219 /// never searched one (lane-txset).5327 /// never searched one (lane-txset).
5220 fn block_tx_type(block: &BlockCoeffs) -> TxType {5328 fn block_tx_type(block: &BlockCoeffs) -> TxType {
5221- block.luma_tx_types.first().copied().unwrap_or(TxType::DctDct)5329+ block
5330+ .luma_tx_types
5331+ .first()
5332+ .copied()
5333+ .unwrap_or(TxType::DctDct)
5222 }5334 }
5223 5335
5224 /// The `(side / tx)^2` transform units of one block's luma residual, raster5336 /// The `(side / tx)^2` transform units of one block's luma residual, raster
@@ -5294,9 +5406,7 @@ fn write_luma_tus(
5294 grid.to_vec()5406 grid.to_vec()
5295 } else {5407 } else {
5296 (0..tx)5408 (0..tx)
5297- .flat_map(|row| {5409+ .flat_map(|row| grid[(tu_row * tx + row) * side + tu_col * tx..][..tx].to_vec())
5298- grid[(tu_row * tx + row) * side + tu_col * tx..][..tx].to_vec()
5299- })
5300 .collect()5410 .collect()
5301 };5411 };
5302 // spec `get_txb_ctx_general`: a lone transform unit covering its5412 // spec `get_txb_ctx_general`: a lone transform unit covering its
@@ -5381,7 +5491,9 @@ fn write_luma_select(
5381 }5491 }
5382 }5492 }
5383 let tx = max_tx >> depth;5493 let tx = max_tx >> depth;
5384- write_luma_tus(enc, cdfs, neighbours, at_mi, side, tx, grid, mode, scans, false, tx_types)?;5494+ write_luma_tus(
5495+ enc, cdfs, neighbours, at_mi, side, tx, grid, mode, scans, false, tx_types,
5496+ )?;
5385 neighbours.record_tx(at_mi, side, tx);5497 neighbours.record_tx(at_mi, side, tx);
5386 Ok(tx)5498 Ok(tx)
5387 }5499 }
@@ -5681,7 +5793,9 @@ pub(crate) fn predicted_coeff_bits(blocks: &[Quadrant], base_q_idx: u8) -> f64 {
5681 .map(|tu| {5793 .map(|tu| {
5682 let (r0, c0) = ((tu / 2) * 32, (tu % 2) * 32);5794 let (r0, c0) = ((tu / 2) * 32, (tu % 2) * 32);
5683 let unit: Vec<i32> = (0..32)5795 let unit: Vec<i32> = (0..32)
5684- .flat_map(|row| g[(r0 + row) * 64 + c0..][..32].to_vec())5796+ .flat_map(|row| {
5797+ g[(r0 + row) * 64 + c0..][..32].to_vec()
5798+ })
5685 .collect();5799 .collect();
5686 coeff_bits(&unit, TxbSet::Luma32Inter, q_ctx, 0, 0)5800 coeff_bits(&unit, TxbSet::Luma32Inter, q_ctx, 0, 0)
5687 })5801 })
@@ -5696,7 +5810,10 @@ pub(crate) fn predicted_coeff_bits(blocks: &[Quadrant], base_q_idx: u8) -> f64 {
5696 }5810 }
5697 Quadrant::Covered => return 0.0,5811 Quadrant::Covered => return 0.0,
5698 };5812 };
5699- q.blocks().iter().map(|b| block_bits(b, side, q_ctx)).sum::<f64>()5813+ q.blocks()
5814+ .iter()
5815+ .map(|b| block_bits(b, side, q_ctx))
5816+ .sum::<f64>()
5700 })5817 })
5701 .sum()5818 .sum()
5702 }5819 }
@@ -5764,7 +5881,7 @@ fn scan_of(side: usize) -> &'static Vec<u16> {
5764 /// DECODER's own table so the two cannot drift, cached per size and class.5881 /// DECODER's own table so the two cannot drift, cached per size and class.
5765 fn class_scan_of(side: usize, class: crate::decode::TxClass) -> &'static [u16] {5882 fn class_scan_of(side: usize, class: crate::decode::TxClass) -> &'static [u16] {
5766 static SCANS: LazyLock<[[Vec<u16>; 2]; 4]> = LazyLock::new(|| {5883 static SCANS: LazyLock<[[Vec<u16>; 2]; 4]> = LazyLock::new(|| {
5767- use crate::decode::{TxClass, class_scan_table};5884+ use crate::decode::{class_scan_table, TxClass};
5768 [TX4, TX8, TX16, TX32].map(|side| {5885 [TX4, TX8, TX16, TX32].map(|side| {
5769 [5886 [
5770 class_scan_table(side, TxClass::Horiz),5887 class_scan_table(side, TxClass::Horiz),
@@ -5846,7 +5963,16 @@ pub(crate) fn coeff_bits_typed(
5846 }5963 }
5847 let scan = scan_of(coding.side);5964 let scan = scan_of(coding.side);
5848 let mut enc = SymbolEncoder::pricer();5965 let mut enc = SymbolEncoder::pricer();
5849- write_coeffs(&mut enc, &mut coding, grid, scan, skip_ctx, sign_ctx, None, tx_type);5966+ write_coeffs(
5967+ &mut enc,
5968+ &mut coding,
5969+ grid,
5970+ scan,
5971+ skip_ctx,
5972+ sign_ctx,
5973+ None,
5974+ tx_type,
5975+ );
5850 enc.bits()5976 enc.bits()
5851 };5977 };
5852 // lane-av1speed2: 60% of the transform units of an inter stream hold no5978 // lane-av1speed2: 60% of the transform units of an inter stream hold no
@@ -5866,12 +5992,20 @@ pub(crate) fn coeff_bits_typed(
5866 let all_zero = grid.iter().all(|&level| level == 0);5992 let all_zero = grid.iter().all(|&level| level == 0);
5867 PRICING_BASE.with_borrow_mut(|base| {5993 PRICING_BASE.with_borrow_mut(|base| {
5868 if let Some(slot) = base.as_deref_mut().filter(|s| s.0.q_ctx == q_ctx) {5994 if let Some(slot) = base.as_deref_mut().filter(|s| s.0.q_ctx == q_ctx) {
5869- return if all_zero { zero_price(slot) } else { price(slot) };5995+ return if all_zero {
5996+ zero_price(slot)
5997+ } else {
5998+ price(slot)
5999+ };
5870 }6000 }
5871 PRICING.with_borrow_mut(|slots| {6001 PRICING.with_borrow_mut(|slots| {
5872 let slot = slots[q_ctx.min(3)]6002 let slot = slots[q_ctx.min(3)]
5873 .get_or_insert_with(|| Box::new((Cdfs::new(q_ctx), Cdfs::new(q_ctx))));6003 .get_or_insert_with(|| Box::new((Cdfs::new(q_ctx), Cdfs::new(q_ctx))));
5874- if all_zero { zero_price(slot) } else { price(slot) }6004+ if all_zero {
6005+ zero_price(slot)
6006+ } else {
6007+ price(slot)
6008+ }
5875 })6009 })
5876 })6010 })
5877 }6011 }
@@ -5951,7 +6085,11 @@ pub(crate) fn rdoq(
5951 // is what the writer and the pricer take (`coded_corner`).6085 // is what the writer and the pricer take (`coded_corner`).
5952 let coded = side.min(TX32);6086 let coded = side.min(TX32);
5953 let q_ctx = crate::decode::q_ctx_of(base_q_idx);6087 let q_ctx = crate::decode::q_ctx_of(base_q_idx);
5954- let mut dense = if coded == side { Vec::new() } else { vec![0i32; coded * coded] };6088+ let mut dense = if coded == side {
6089+ Vec::new()
6090+ } else {
6091+ vec![0i32; coded * coded]
6092+ };
5955 fn price(6093 fn price(
5956 levels: &[i32],6094 levels: &[i32],
5957 dense: &mut [i32],6095 dense: &mut [i32],
@@ -5971,7 +6109,9 @@ pub(crate) fn rdoq(
5971 }6109 }
5972 coeff_bits_typed(dense, set, q_ctx, skip_ctx, sign_ctx, tx_type)6110 coeff_bits_typed(dense, set, q_ctx, skip_ctx, sign_ctx, tx_type)
5973 }6111 }
5974- let mut bits = price(levels, &mut dense, side, coded, set, q_ctx, skip_ctx, sign_ctx, tx_type);6112+ let mut bits = price(
6113+ levels, &mut dense, side, coded, set, q_ctx, skip_ctx, sign_ctx, tx_type,
6114+ );
5975 // The DC has its own quantiser, so its squared error per unit of level is6115 // The DC has its own quantiser, so its squared error per unit of level is
5976 // `(dc_q / ac_q)^2` of an AC coefficient's.6116 // `(dc_q / ac_q)^2` of an AC coefficient's.
5977 let dc = f64::from(crate::quant::dc_q(8, i32::from(base_q_idx)));6117 let dc = f64::from(crate::quant::dc_q(8, i32::from(base_q_idx)));
@@ -6022,9 +6162,11 @@ pub(crate) fn rdoq(
6022 let candidate = level - level.signum();6162 let candidate = level - level.signum();
6023 levels[i] = candidate;6163 levels[i] = candidate;
6024 budget -= 1;6164 budget -= 1;
6025- let after = price(levels, &mut dense, side, coded, set, q_ctx, skip_ctx, sign_ctx, tx_type);6165+ let after = price(
6026- let distortion = weight6166+ levels, &mut dense, side, coded, set, q_ctx, skip_ctx, sign_ctx, tx_type,
6027- * ((s - f64::from(candidate)).powi(2) - (s - f64::from(level)).powi(2));6167+ );
6168+ let distortion =
6169+ weight * ((s - f64::from(candidate)).powi(2) - (s - f64::from(level)).powi(2));
6028 if distortion + lambda * (after - bits) < 0.0 {6170 if distortion + lambda * (after - bits) < 0.0 {
6029 bits = after;6171 bits = after;
6030 if census {6172 if census {
@@ -6049,8 +6191,14 @@ pub(crate) fn rdoq(
6049 /// at the end of the scan), zeroed coefficients, lowered coefficients, bits6191 /// at the end of the scan), zeroed coefficients, lowered coefficients, bits
6050 /// after the pass * 64, bits before the pass * 64.6192 /// after the pass * 64, bits before the pass * 64.
6051 pub(crate) static RDOQ_STATS: [AtomicU64; 8] = [6193 pub(crate) static RDOQ_STATS: [AtomicU64; 8] = [
6052- AtomicU64::new(0), AtomicU64::new(0), AtomicU64::new(0), AtomicU64::new(0),6194+ AtomicU64::new(0),
6053- AtomicU64::new(0), AtomicU64::new(0), AtomicU64::new(0), AtomicU64::new(0),6195+ AtomicU64::new(0),
6196+ AtomicU64::new(0),
6197+ AtomicU64::new(0),
6198+ AtomicU64::new(0),
6199+ AtomicU64::new(0),
6200+ AtomicU64::new(0),
6201+ AtomicU64::new(0),
6054 ];6202 ];
6055 6203
6056 /// The pre-pass half of [`RDOQ_STATS`] (`EC_AV1_RDOQ_CENSUS`): what the6204 /// The pre-pass half of [`RDOQ_STATS`] (`EC_AV1_RDOQ_CENSUS`): what the
@@ -6146,11 +6294,13 @@ thread_local! {
6146 /// at native, still with every one of its frames armed with the defaults.6294 /// at native, still with every one of its frames armed with the defaults.
6147 pub(crate) fn arm_pricing_cdfs(base: Option<&Cdfs>, screen: bool) {6295 pub(crate) fn arm_pricing_cdfs(base: Option<&Cdfs>, screen: bool) {
6148 static MODE: std::sync::OnceLock<u8> = std::sync::OnceLock::new();6296 static MODE: std::sync::OnceLock<u8> = std::sync::OnceLock::new();
6149- let mode = *MODE.get_or_init(|| match std::env::var("EC_AV1_PRICE_FRAME_CDFS").as_deref() {6297+ let mode = *MODE.get_or_init(
6150- Ok("0") => 0,6298+ || match std::env::var("EC_AV1_PRICE_FRAME_CDFS").as_deref() {
6151- Ok("1") => 1,6299+ Ok("0") => 0,
6152- _ => 2,6300+ Ok("1") => 1,
6153- });6301+ _ => 2,
6302+ },
6303+ );
6154 let on = match mode {6304 let on = match mode {
6155 0 => false,6305 0 => false,
6156 1 => true,6306 1 => true,
@@ -6158,7 +6308,9 @@ pub(crate) fn arm_pricing_cdfs(base: Option<&Cdfs>, screen: bool) {
6158 };6308 };
6159 PRICING_HITS[usize::from(on && base.is_some())].fetch_add(1, Ordering::Relaxed);6309 PRICING_HITS[usize::from(on && base.is_some())].fetch_add(1, Ordering::Relaxed);
6160 PRICING_BASE.with_borrow_mut(|slot| {6310 PRICING_BASE.with_borrow_mut(|slot| {
6161- *slot = base.filter(|_| on).map(|c| Box::new((c.clone(), c.clone())));6311+ *slot = base
6312+ .filter(|_| on)
6313+ .map(|c| Box::new((c.clone(), c.clone())));
6162 });6314 });
6163 }6315 }
6164 6316
@@ -6166,8 +6318,7 @@ pub(crate) fn arm_pricing_cdfs(base: Option<&Cdfs>, screen: bool) {
6166 /// many with the frame's REAL starting tables ([`arm_pricing_cdfs`]), so a6318 /// many with the frame's REAL starting tables ([`arm_pricing_cdfs`]), so a
6167 /// gate can print which side of the screen gate every frame landed on. Counts6319 /// gate can print which side of the screen gate every frame landed on. Counts
6168 /// arming calls, not frames: a multi-tile frame arms one worker per tile.6320 /// arming calls, not frames: a multi-tile frame arms one worker per tile.
6169-pub(crate) static PRICING_HITS: [AtomicUsize; 2] =6321+pub(crate) static PRICING_HITS: [AtomicUsize; 2] = [AtomicUsize::new(0), AtomicUsize::new(0)];
6170- [AtomicUsize::new(0), AtomicUsize::new(0)];
6171 6322
6172 /// Reads and clears [`PRICING_HITS`]: (default-table armings, real-table6323 /// Reads and clears [`PRICING_HITS`]: (default-table armings, real-table
6173 /// armings).6324 /// armings).
@@ -6389,7 +6540,10 @@ fn write_coeffs(
6389 // lane-av1speed3: `side` is 4, 8, 16 or 32, but it is a runtime value --6540 // lane-av1speed3: `side` is 4, 8, 16 or 32, but it is a runtime value --
6390 // `pos / side` compiles to a real integer division, once per coefficient,6541 // `pos / side` compiles to a real integer division, once per coefficient,
6391 // in the encoder's largest self-time symbol. Same row and column.6542 // in the encoder's largest self-time symbol. Same row and column.
6392- debug_assert!(side.is_power_of_two(), "the scan splits a position by shifting");6543+ debug_assert!(
6544+ side.is_power_of_two(),
6545+ "the scan splits a position by shifting"
6546+ );
6393 let (shift, mask) = (side.trailing_zeros(), side - 1);6547 let (shift, mask) = (side.trailing_zeros(), side - 1);
6394 for scan_idx in (0..eob).rev() {6548 for scan_idx in (0..eob).rev() {
6395 let pos = scan[scan_idx] as usize;6549 let pos = scan[scan_idx] as usize;
@@ -6649,7 +6803,11 @@ fn br_ctx(
6649 if row == 0 && col == 0 {6803 if row == 0 && col == 0 {
6650 return mag;6804 return mag;
6651 }6805 }
6652- let near_origin = if class == TxClass::Horiz { col == 0 } else { row == 0 };6806+ let near_origin = if class == TxClass::Horiz {
6807+ col == 0
6808+ } else {
6809+ row == 0
6810+ };
6653 return if near_origin { mag + 7 } else { mag + 14 };6811 return if near_origin { mag + 7 } else { mag + 14 };
6654 }6812 }
6655 let p = row * side + col;6813 let p = row * side + col;
@@ -6800,7 +6958,11 @@ pub(crate) fn intra_inter_ctx(
6800 /// `CLASS0_SIZE << (class + 2)` (spec 3), the magnitude an `MV_CLASS_n`6958 /// `CLASS0_SIZE << (class + 2)` (spec 3), the magnitude an `MV_CLASS_n`
6801 /// component's own bits start counting from; class zero starts at zero.6959 /// component's own bits start counting from; class zero starts at zero.
6802 fn mv_class_base(class: usize) -> i32 {6960 fn mv_class_base(class: usize) -> i32 {
6803- if class == 0 { 0 } else { 2i32 << (class + 2) }6961+ if class == 0 {
6962+ 0
6963+ } else {
6964+ 2i32 << (class + 2)
6965+ }
6804 }6966 }
6805 6967
6806 /// The class a pre-offset magnitude `z` (`|diff| - 1`) falls in — the inverse6968 /// The class a pre-offset magnitude `z` (`|diff| - 1`) falls in — the inverse
@@ -6923,11 +7085,12 @@ fn write_single_ref(
6923 left_ref: i8,7085 left_ref: i8,
6924 left_ref1: Option<i8>,7086 left_ref1: Option<i8>,
6925 ) {7087 ) {
6926- REF_HITS[(ref_frame.max(1) - 1) as usize % 7].fetch_add(1, std::sync::atomic::Ordering::Relaxed);7088+ REF_HITS[(ref_frame.max(1) - 1) as usize % 7]
7089+ .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
6927 use crate::mvstack::{7090 use crate::mvstack::{
6928- ALTREF2_FRAME, ALTREF_FRAME, BWDREF_FRAME, GOLDEN_FRAME, LAST2_FRAME, LAST3_FRAME,
6929 single_ref_p1_ctx, single_ref_p2_ctx, single_ref_p3_ctx, single_ref_p4_ctx,7091 single_ref_p1_ctx, single_ref_p2_ctx, single_ref_p3_ctx, single_ref_p4_ctx,
6930- single_ref_p5_ctx, single_ref_p6_ctx,7092+ single_ref_p5_ctx, single_ref_p6_ctx, ALTREF2_FRAME, ALTREF_FRAME, BWDREF_FRAME,
7093+ GOLDEN_FRAME, LAST2_FRAME, LAST3_FRAME,
6931 };7094 };
6932 let above = (above_ref > 0).then_some(above_ref);7095 let above = (above_ref > 0).then_some(above_ref);
6933 let left = (left_ref > 0).then_some(left_ref);7096 let left = (left_ref > 0).then_some(left_ref);
@@ -7071,7 +7234,10 @@ pub(crate) fn take_drl_hits() -> [usize; 4] {
7071 /// pricer's own offer set is enumerated against this rule by7234 /// pricer's own offer set is enumerated against this rule by
7072 /// `crate::encode::tests::every_drl_index_the_new_mv_pricer_offers_is_one_the_writer_can_signal`.7235 /// `crate::encode::tests::every_drl_index_the_new_mv_pricer_offers_is_one_the_writer_can_signal`.
7073 pub(crate) fn signalled_drl_idx(entries: usize, start: usize, target: usize) -> usize {7236 pub(crate) fn signalled_drl_idx(entries: usize, start: usize, target: usize) -> usize {
7074- target.min(start + 2).min(entries.saturating_sub(1)).max(start)7237+ target
7238+ .min(start + 2)
7239+ .min(entries.saturating_sub(1))
7240+ .max(start)
7075 }7241 }
7076 7242
7077 fn write_drl_idx(7243 fn write_drl_idx(
@@ -7161,7 +7327,7 @@ fn write_inter_mode(
7161 enc.symbol(1, &mut cdfs.new_mv[stack.new_mv_ctx]); // not NEWMV7327 enc.symbol(1, &mut cdfs.new_mv[stack.new_mv_ctx]); // not NEWMV
7162 enc.symbol(1, &mut cdfs.zero_mv[stack.zero_mv_ctx]); // not GLOBALMV7328 enc.symbol(1, &mut cdfs.zero_mv[stack.zero_mv_ctx]); // not GLOBALMV
7163 enc.symbol(1, &mut cdfs.ref_mv[stack.ref_mv_ctx]); // NEARMV7329 enc.symbol(1, &mut cdfs.ref_mv[stack.ref_mv_ctx]); // NEARMV
7164- // `RefMvIdx` starts at 1 for NEARMV (spec 5.11.24).7330+ // `RefMvIdx` starts at 1 for NEARMV (spec 5.11.24).
7165 let idx = idx.max(1);7331 let idx = idx.max(1);
7166 let signalled = write_drl_idx(enc, cdfs, stack, 1, idx);7332 let signalled = write_drl_idx(enc, cdfs, stack, 1, idx);
7167 let mv = stack.entries.get(signalled).map_or(stack.near_mv, |e| e.mv);7333 let mv = stack.entries.get(signalled).map_or(stack.near_mv, |e| e.mv);
@@ -7335,12 +7501,7 @@ pub(crate) fn sb_coeff_inter_frame_tile_cdfs(
7335 let scan4 = default_scan(TX4);7501 let scan4 = default_scan(TX4);
7336 // The same four tables again as one array, which is what the per-unit7502 // The same four tables again as one array, which is what the per-unit
7337 // luma writer indexes by transform side.7503 // luma writer indexes by transform side.
7338- let all_scans = [7504+ let all_scans = [scan32.clone(), scan16.clone(), scan8.clone(), scan4.clone()];
7339- scan32.clone(),
7340- scan16.clone(),
7341- scan8.clone(),
7342- scan4.clone(),
7343- ];
7344 let zero_grids = [7505 let zero_grids = [
7345 vec![0i32; TX32 * TX32],7506 vec![0i32; TX32 * TX32],
7346 vec![0i32; TX16 * TX16],7507 vec![0i32; TX16 * TX16],
@@ -7431,7 +7592,10 @@ pub(crate) fn sb_coeff_inter_frame_tile_cdfs(
7431 // (mi_row, mi_col) and a second one at + hbs (16 mi) along the7592 // (mi_row, mi_col) and a second one at + hbs (16 mi) along the
7432 // cut axis, the second only when it is inside the frame -- the7593 // cut axis, the second only when it is inside the frame -- the
7433 // search only offers a root wholly inside, so both are.7594 // search only offers a root wholly inside, so both are.
7434- let base_mi = ((sb_r & !1) as usize * SB_MI as usize, (sb_c & !1) as usize * SB_MI as usize);7595+ let base_mi = (
7596+ (sb_r & !1) as usize * SB_MI as usize,
7597+ (sb_c & !1) as usize * SB_MI as usize,
7598+ );
7435 let (w, h) = if horz { (128usize, 64usize) } else { (64, 128) };7599 let (w, h) = if horz { (128usize, 64usize) } else { (64, 128) };
7436 for (i, half) in halves.iter().enumerate() {7600 for (i, half) in halves.iter().enumerate() {
7437 let at_mi = if horz {7601 let at_mi = if horz {
@@ -7467,8 +7631,9 @@ pub(crate) fn sb_coeff_inter_frame_tile_cdfs(
7467 (sb_r & !1) as usize * SB_MI as usize,7631 (sb_r & !1) as usize * SB_MI as usize,
7468 (sb_c & !1) as usize * SB_MI as usize,7632 (sb_c & !1) as usize * SB_MI as usize,
7469 );7633 );
7470- for (((mi_r, mi_c), (w, h)), piece) in7634+ for (((mi_r, mi_c), (w, h)), piece) in crate::encode::ab128_pieces(symbol, base_mi)
7471- crate::encode::ab128_pieces(symbol, base_mi).iter().zip(pieces)7635+ .iter()
7636+ .zip(pieces)
7472 {7637 {
7473 write_inter_block_128_rect(7638 write_inter_block_128_rect(
7474 &mut enc,7639 &mut enc,
@@ -7532,7 +7697,13 @@ pub(crate) fn sb_coeff_inter_frame_tile_cdfs(
7532 + usize::from(neighbours.left_skip[mi_r]);7697 + usize::from(neighbours.left_skip[mi_r]);
7533 enc.symbol(usize::from(block.skip), &mut cdfs.skip[skip_ctx]);7698 enc.symbol(usize::from(block.skip), &mut cdfs.skip[skip_ctx]);
7534 write_cdef_idx(&mut enc, (mi_r, mi_c), block.skip);7699 write_cdef_idx(&mut enc, (mi_r, mi_c), block.skip);
7535- write_delta_q(&mut enc, &mut cdfs, (mi_r, mi_c), !sb128_armed(), block.skip);7700+ write_delta_q(
7701+ &mut enc,
7702+ &mut cdfs,
7703+ (mi_r, mi_c),
7704+ !sb128_armed(),
7705+ block.skip,
7706+ );
7536 let ii_ctx = intra_inter_ctx(7707 let ii_ctx = intra_inter_ctx(
7537 has_above,7708 has_above,
7538 has_left,7709 has_left,
@@ -7926,7 +8097,6 @@ pub(crate) fn sb_coeff_inter_frame_tile_cdfs(
7926 // caller (r15): the SUB-grid skip/inter arrays8097 // caller (r15): the SUB-grid skip/inter arrays
7927 // are otherwise left stale for the next8098 // are otherwise left stale for the next
7928 // 16x16 slot.8099 // 16x16 slot.
7929-
7930 }8100 }
7931 }8101 }
7932 continue;8102 continue;
@@ -7951,111 +8121,137 @@ pub(crate) fn sb_coeff_inter_frame_tile_cdfs(
7951 8121
7952 let mode_for_tx;8122 let mode_for_tx;
7953 if let Some(info) = block.inter {8123 if let Some(info) = block.inter {
7954- write_comp_mode(&mut enc, &mut cdfs, &neighbours, (mi_r, mi_c), (has_above, has_left), info.ref1.is_some());8124+ write_comp_mode(
8125+ &mut enc,
8126+ &mut cdfs,
8127+ &neighbours,
8128+ (mi_r, mi_c),
8129+ (has_above, has_left),
8130+ info.ref1.is_some(),
8131+ );
7955 let (mi_row, mi_col) = (r32 as usize * 8, c32 as usize * 8);8132 let (mi_row, mi_col) = (r32 as usize * 8, c32 as usize * 8);
7956 if info.ref1.is_some() {8133 if info.ref1.is_some() {
7957 let (mv, mv1, is_new_mv) = write_compound_block(8134 let (mv, mv1, is_new_mv) = write_compound_block(
7958- &mut enc, &mut cdfs, &neighbours, &grid, (mi_row, mi_col), (8, 8),8135+ &mut enc,
7959- (has_above, has_left), info, (mi_cols as usize, mi_rows as usize),8136+ &mut cdfs,
8137+ &neighbours,
8138+ &grid,
8139+ (mi_row, mi_col),
8140+ (8, 8),
8141+ (has_above, has_left),
8142+ info,
8143+ (mi_cols as usize, mi_rows as usize),
7960 )?;8144 )?;
7961 for dr in 0..8 {8145 for dr in 0..8 {
7962 for dc in 0..8 {8146 for dc in 0..8 {
7963- grid.set(mi_row + dr, mi_col + dc, MiInfo {8147+ grid.set(
7964- is_inter: true,8148+ mi_row + dr,
7965- ref_frame: info.ref_frame,8149+ mi_col + dc,
7966- ref_frame1: info.ref1.unwrap_or(NO_REF1),8150+ MiInfo {
7967- mv1: mv16(mv1),8151+ is_inter: true,
7968- mv: mv16(mv),8152+ ref_frame: info.ref_frame,
7969- is_new_mv,8153+ ref_frame1: info.ref1.unwrap_or(NO_REF1),
7970- size: 8,8154+ mv1: mv16(mv1),
7971- size_h: 8,8155+ mv: mv16(mv),
7972- is_global_mv0: false,8156+ is_new_mv,
7973- is_global_mv1: false,8157+ size: 8,
7974- });8158+ size_h: 8,
8159+ is_global_mv0: false,
8160+ is_global_mv1: false,
8161+ },
8162+ );
7975 }8163 }
7976 }8164 }
7977 mode_for_tx = 0;8165 mode_for_tx = 0;
7978 } else {8166 } else {
7979- write_single_ref(&mut enc, &mut cdfs, info.ref_frame, neighbours.above_ref[mi_c], neighbours.above_ref1[mi_c], neighbours.left_ref[mi_r], neighbours.left_ref1[mi_r]);8167+ write_single_ref(
8168+ &mut enc,
8169+ &mut cdfs,
8170+ info.ref_frame,
8171+ neighbours.above_ref[mi_c],
8172+ neighbours.above_ref1[mi_c],
8173+ neighbours.left_ref[mi_r],
8174+ neighbours.left_ref1[mi_r],
8175+ );
7980 8176
7981- let stack = find_mv_stack(8177+ let stack = find_mv_stack(
7982- &grid,8178+ &grid,
7983- mi_row,8179+ mi_row,
7984- mi_col,8180+ mi_col,
7985- 8,8181+ 8,
7986- 8,8182+ 8,
7987- info.ref_frame,8183+ info.ref_frame,
7988- mi_cols as usize,8184+ mi_cols as usize,
7989- mi_rows as usize,8185+ mi_rows as usize,
7990- );8186+ );
7991 8187
7992- let (mv, is_new_mv) = write_inter_mode(&mut enc, &mut cdfs, info, &stack)?;8188+ let (mv, is_new_mv) = write_inter_mode(&mut enc, &mut cdfs, info, &stack)?;
7993- if crate::msac::symtrace::dir().is_some() {8189+ if crate::msac::symtrace::dir().is_some() {
7994- crate::msac::symtrace::note(&format!(8190+ crate::msac::symtrace::note(&format!(
7995- " MODE mi=({mi_r},{mi_c}) mode={:?} idx={} mv={mv:?} info_mv={:?}",8191+ " MODE mi=({mi_r},{mi_c}) mode={:?} idx={} mv={mv:?} info_mv={:?}",
7996- info.mode, info.ref_mv_idx, info.mv8192+ info.mode, info.ref_mv_idx, info.mv
7997- ));8193+ ));
7998- }8194+ }
7999- grid.set(8195+ grid.set(
8000- mi_row,8196+ mi_row,
8001- mi_col,8197+ mi_col,
8002- MiInfo {8198+ MiInfo {
8003- is_inter: true,8199+ is_inter: true,
8004- ref_frame: info.ref_frame,8200+ ref_frame: info.ref_frame,
8005- ref_frame1: NO_REF1,8201+ ref_frame1: NO_REF1,
8006- mv1: (0, 0),8202+ mv1: (0, 0),
8007- mv: mv16(mv),8203+ mv: mv16(mv),
8008- is_new_mv,8204+ is_new_mv,
8009- size: 8,8205+ size: 8,
8010- size_h: 8,8206+ size_h: 8,
8011- is_global_mv0: false,8207+ is_global_mv0: false,
8012- is_global_mv1: false,8208+ is_global_mv1: false,
8013- },8209+ },
8014- );8210+ );
8015- for dr in 0..8 {8211+ for dr in 0..8 {
8016- for dc in 0..8 {8212+ for dc in 0..8 {
8017- if dr == 0 && dc == 0 {8213+ if dr == 0 && dc == 0 {
8018- continue;8214+ continue;
8215+ }
8216+ grid.set(
8217+ mi_row + dr,
8218+ mi_col + dc,
8219+ MiInfo {
8220+ is_inter: true,
8221+ ref_frame: info.ref_frame,
8222+ ref_frame1: NO_REF1,
8223+ mv1: (0, 0),
8224+ mv: mv16(mv),
8225+ is_new_mv,
8226+ size: 8,
8227+ size_h: 8,
8228+ is_global_mv0: false,
8229+ is_global_mv1: false,
8230+ },
8231+ );
8019 }8232 }
8020- grid.set(
8021- mi_row + dr,
8022- mi_col + dc,
8023- MiInfo {
8024- is_inter: true,
8025- ref_frame: info.ref_frame,
8026- ref_frame1: NO_REF1,
8027- mv1: (0, 0),
8028- mv: mv16(mv),
8029- is_new_mv,
8030- size: 8,
8031- size_h: 8,
8032- is_global_mv0: false,
8033- is_global_mv1: false,
8034- },
8035- );
8036 }8233 }
8037- }8234+ write_motion_mode(
8038- write_motion_mode(8235+ &mut enc,
8039- &mut enc,8236+ &mut cdfs,
8040- &mut cdfs,8237+ &grid,
8041- &grid,8238+ (mi_row, mi_col),
8042- (mi_row, mi_col),8239+ (8, 8),
8043- (8, 8),8240+ (BLOCK, BLOCK),
8044- (BLOCK, BLOCK),8241+ (mi_cols as usize, mi_rows as usize),
8045- (mi_cols as usize, mi_rows as usize),8242+ info.ref_frame,
8046- info.ref_frame,8243+ block.motion_mode,
8047- block.motion_mode,8244+ )?;
8048- )?;8245+ mode_for_tx = 0;
8049- mode_for_tx = 0;
8050 }8246 }
8051 } else {8247 } else {
8052 let mode = usize::from(block.mode);8248 let mode = usize::from(block.mode);
8053 enc.symbol(mode, &mut cdfs.y_mode[SIZE_GROUP_32]);8249 enc.symbol(mode, &mut cdfs.y_mode[SIZE_GROUP_32]);
8054 if (V_PRED..=D67_PRED).contains(&mode) {8250 if (V_PRED..=D67_PRED).contains(&mode) {
8055 enc.symbol(8251 enc.symbol(
8056- (ANGLE_DELTA_ZERO as i32 + i32::from(block.angle_delta_y)) as usize,8252+ (ANGLE_DELTA_ZERO as i32 + i32::from(block.angle_delta_y)) as usize,
8057- &mut cdfs.angle_delta[mode - V_PRED],8253+ &mut cdfs.angle_delta[mode - V_PRED],
8058- );8254+ );
8059 }8255 }
8060 let uv_mode = usize::from(block.uv_mode);8256 let uv_mode = usize::from(block.uv_mode);
8061 enc.symbol(uv_mode, &mut cdfs.uv_mode_cfl[mode]);8257 enc.symbol(uv_mode, &mut cdfs.uv_mode_cfl[mode]);
@@ -8208,7 +8404,8 @@ fn write_inter_frame_leaf(
8208 8404
8209 let (r, c) = at;8405 let (r, c) = at;
8210 let (mi_r, mi_c) = (r * (SUB / MI), c * (SUB / MI));8406 let (mi_r, mi_c) = (r * (SUB / MI), c * (SUB / MI));
8211- let skip_ctx = usize::from(neighbours.above_skip[mi_c]) + usize::from(neighbours.left_skip[mi_r]);8407+ let skip_ctx =
8408+ usize::from(neighbours.above_skip[mi_c]) + usize::from(neighbours.left_skip[mi_r]);
8212 enc.symbol(usize::from(block.skip), &mut cdfs.skip[skip_ctx]);8409 enc.symbol(usize::from(block.skip), &mut cdfs.skip[skip_ctx]);
8213 write_cdef_idx(enc, (mi_r, mi_c), block.skip);8410 write_cdef_idx(enc, (mi_r, mi_c), block.skip);
8214 write_delta_q(enc, cdfs, (mi_r, mi_c), false, block.skip);8411 write_delta_q(enc, cdfs, (mi_r, mi_c), false, block.skip);
@@ -8221,87 +8418,113 @@ fn write_inter_frame_leaf(
8221 8418
8222 let mode_for_tx;8419 let mode_for_tx;
8223 if let Some(info) = block.inter {8420 if let Some(info) = block.inter {
8224- write_comp_mode(enc, cdfs, neighbours, (mi_r, mi_c), (has_above, has_left), info.ref1.is_some());8421+ write_comp_mode(
8422+ enc,
8423+ cdfs,
8424+ neighbours,
8425+ (mi_r, mi_c),
8426+ (has_above, has_left),
8427+ info.ref1.is_some(),
8428+ );
8225 let (mi_row, mi_col) = (r * SUB_MI as usize, c * SUB_MI as usize);8429 let (mi_row, mi_col) = (r * SUB_MI as usize, c * SUB_MI as usize);
8226 if info.ref1.is_some() {8430 if info.ref1.is_some() {
8227 let n = SUB_MI as usize;8431 let n = SUB_MI as usize;
8228 let (mv, mv1, is_new_mv) = write_compound_block(8432 let (mv, mv1, is_new_mv) = write_compound_block(
8229- enc, cdfs, neighbours, grid, (mi_row, mi_col), (n, n),8433+ enc,
8230- (has_above, has_left), info, (mi_cols as usize, mi_rows as usize),8434+ cdfs,
8435+ neighbours,
8436+ grid,
8437+ (mi_row, mi_col),
8438+ (n, n),
8439+ (has_above, has_left),
8440+ info,
8441+ (mi_cols as usize, mi_rows as usize),
8231 )?;8442 )?;
8232 for dr in 0..n {8443 for dr in 0..n {
8233 for dc in 0..n {8444 for dc in 0..n {
8234- grid.set(mi_row + dr, mi_col + dc, MiInfo {8445+ grid.set(
8235- is_inter: true,8446+ mi_row + dr,
8236- ref_frame: info.ref_frame,8447+ mi_col + dc,
8237- ref_frame1: info.ref1.unwrap_or(NO_REF1),8448+ MiInfo {
8238- mv1: mv16(mv1),8449+ is_inter: true,
8239- mv: mv16(mv),8450+ ref_frame: info.ref_frame,
8240- is_new_mv,8451+ ref_frame1: info.ref1.unwrap_or(NO_REF1),
8241- size: n as u8,8452+ mv1: mv16(mv1),
8242- size_h: n as u8,8453+ mv: mv16(mv),
8243- is_global_mv0: false,8454+ is_new_mv,
8244- is_global_mv1: false,8455+ size: n as u8,
8245- });8456+ size_h: n as u8,
8457+ is_global_mv0: false,
8458+ is_global_mv1: false,
8459+ },
8460+ );
8246 }8461 }
8247 }8462 }
8248 mode_for_tx = 0;8463 mode_for_tx = 0;
8249 } else {8464 } else {
8250- write_single_ref(enc, cdfs, info.ref_frame, neighbours.above_ref[mi_c], neighbours.above_ref1[mi_c], neighbours.left_ref[mi_r], neighbours.left_ref1[mi_r]);8465+ write_single_ref(
8466+ enc,
8467+ cdfs,
8468+ info.ref_frame,
8469+ neighbours.above_ref[mi_c],
8470+ neighbours.above_ref1[mi_c],
8471+ neighbours.left_ref[mi_r],
8472+ neighbours.left_ref1[mi_r],
8473+ );
8251 8474
8252- let stack = find_mv_stack(8475+ let stack = find_mv_stack(
8253- grid,8476+ grid,
8254- mi_row,8477+ mi_row,
8255- mi_col,8478+ mi_col,
8256- SUB_MI as usize,8479+ SUB_MI as usize,
8257- SUB_MI as usize,8480+ SUB_MI as usize,
8258- info.ref_frame,8481+ info.ref_frame,
8259- mi_cols as usize,8482+ mi_cols as usize,
8260- mi_rows as usize,8483+ mi_rows as usize,
8261- );8484+ );
8262 8485
8263- let (mv, is_new_mv) = write_inter_mode(enc, cdfs, info, &stack)?;8486+ let (mv, is_new_mv) = write_inter_mode(enc, cdfs, info, &stack)?;
8264- for dr in 0..SUB_MI as usize {8487+ for dr in 0..SUB_MI as usize {
8265- for dc in 0..SUB_MI as usize {8488+ for dc in 0..SUB_MI as usize {
8266- grid.set(8489+ grid.set(
8267- mi_row + dr,8490+ mi_row + dr,
8268- mi_col + dc,8491+ mi_col + dc,
8269- MiInfo {8492+ MiInfo {
8270- is_inter: true,8493+ is_inter: true,
8271- ref_frame: info.ref_frame,8494+ ref_frame: info.ref_frame,
8272- ref_frame1: NO_REF1,8495+ ref_frame1: NO_REF1,
8273- mv1: (0, 0),8496+ mv1: (0, 0),
8274- mv: mv16(mv),8497+ mv: mv16(mv),
8275- is_new_mv,8498+ is_new_mv,
8276- size: SUB_MI as usize as u8,8499+ size: SUB_MI as usize as u8,
8277- size_h: SUB_MI as usize as u8,8500+ size_h: SUB_MI as usize as u8,
8278- is_global_mv0: false,8501+ is_global_mv0: false,
8279- is_global_mv1: false,8502+ is_global_mv1: false,
8280- },8503+ },
8281- );8504+ );
8505+ }
8282 }8506 }
8283- }8507+ write_motion_mode(
8284- write_motion_mode(8508+ enc,
8285- enc,8509+ cdfs,
8286- cdfs,8510+ grid,
8287- grid,8511+ (mi_row, mi_col),
8288- (mi_row, mi_col),8512+ (SUB_MI as usize, SUB_MI as usize),
8289- (SUB_MI as usize, SUB_MI as usize),8513+ (SUB, SUB),
8290- (SUB, SUB),8514+ (mi_cols as usize, mi_rows as usize),
8291- (mi_cols as usize, mi_rows as usize),8515+ info.ref_frame,
8292- info.ref_frame,8516+ block.motion_mode,
8293- block.motion_mode,8517+ )?;
8294- )?;8518+ mode_for_tx = 0;
8295- mode_for_tx = 0;
8296 }8519 }
8297 } else {8520 } else {
8298 let mode = usize::from(block.mode);8521 let mode = usize::from(block.mode);
8299 enc.symbol(mode, &mut cdfs.y_mode[SIZE_GROUP_16]);8522 enc.symbol(mode, &mut cdfs.y_mode[SIZE_GROUP_16]);
8300 if (V_PRED..=D67_PRED).contains(&mode) {8523 if (V_PRED..=D67_PRED).contains(&mode) {
8301 enc.symbol(8524 enc.symbol(
8302- (ANGLE_DELTA_ZERO as i32 + i32::from(block.angle_delta_y)) as usize,8525+ (ANGLE_DELTA_ZERO as i32 + i32::from(block.angle_delta_y)) as usize,
8303- &mut cdfs.angle_delta[mode - V_PRED],8526+ &mut cdfs.angle_delta[mode - V_PRED],
8304- );8527+ );
8305 }8528 }
8306 let uv_mode = usize::from(block.uv_mode);8529 let uv_mode = usize::from(block.uv_mode);
8307 enc.symbol(uv_mode, &mut cdfs.uv_mode_cfl[mode]);8530 enc.symbol(uv_mode, &mut cdfs.uv_mode_cfl[mode]);
@@ -8380,7 +8603,16 @@ fn write_inter_frame_leaf(
8380 ];8603 ];
8381 if split {8604 if split {
8382 write_luma_tus(8605 write_luma_tus(
8383- enc, cdfs, neighbours, at_mi, SUB, tx, &grids[0], mode_for_tx, all_scans, is_inter,8606+ enc,
8607+ cdfs,
8608+ neighbours,
8609+ at_mi,
8610+ SUB,
8611+ tx,
8612+ &grids[0],
8613+ mode_for_tx,
8614+ all_scans,
8615+ is_inter,
8384 &block.luma_tx_types,8616 &block.luma_tx_types,
8385 )?;8617 )?;
8386 }8618 }
@@ -8467,93 +8699,122 @@ fn write_inter_frame_leaf8(
8467 write_cdef_idx(enc, leaf_mi, block.skip);8699 write_cdef_idx(enc, leaf_mi, block.skip);
8468 write_delta_q(enc, cdfs, leaf_mi, false, block.skip);8700 write_delta_q(enc, cdfs, leaf_mi, false, block.skip);
8469 8701
8470- let (has_above, has_left) = (neighbours.has_above(leaf_mi.0), neighbours.has_left(leaf_mi.1));8702+ let (has_above, has_left) = (
8703+ neighbours.has_above(leaf_mi.0),
8704+ neighbours.has_left(leaf_mi.1),
8705+ );
8471 let ii_ctx = intra_inter_ctx(has_above, has_left, above_inter, left_inter);8706 let ii_ctx = intra_inter_ctx(has_above, has_left, above_inter, left_inter);
8472 let is_inter = block.inter.is_some();8707 let is_inter = block.inter.is_some();
8473 enc.symbol(usize::from(is_inter), &mut cdfs.intra_inter[ii_ctx]);8708 enc.symbol(usize::from(is_inter), &mut cdfs.intra_inter[ii_ctx]);
8474 8709
8475 let mode_for_tx;8710 let mode_for_tx;
8476 if let Some(info) = block.inter {8711 if let Some(info) = block.inter {
8477- write_comp_mode(enc, cdfs, neighbours, leaf_mi, (has_above, has_left), info.ref1.is_some());8712+ write_comp_mode(
8713+ enc,
8714+ cdfs,
8715+ neighbours,
8716+ leaf_mi,
8717+ (has_above, has_left),
8718+ info.ref1.is_some(),
8719+ );
8478 let (mi_row, mi_col) = leaf_mi;8720 let (mi_row, mi_col) = leaf_mi;
8479 if info.ref1.is_some() {8721 if info.ref1.is_some() {
8480 let (mv, mv1, is_new_mv) = write_compound_block(8722 let (mv, mv1, is_new_mv) = write_compound_block(
8481- enc, cdfs, neighbours, grid, (mi_row, mi_col), (2, 2),8723+ enc,
8482- (has_above, has_left), info, (mi_cols as usize, mi_rows as usize),8724+ cdfs,
8725+ neighbours,
8726+ grid,
8727+ (mi_row, mi_col),
8728+ (2, 2),
8729+ (has_above, has_left),
8730+ info,
8731+ (mi_cols as usize, mi_rows as usize),
8483 )?;8732 )?;
8484 for dr in 0..2 {8733 for dr in 0..2 {
8485 for dc in 0..2 {8734 for dc in 0..2 {
8486- grid.set(mi_row + dr, mi_col + dc, MiInfo {8735+ grid.set(
8487- is_inter: true,8736+ mi_row + dr,
8488- ref_frame: info.ref_frame,8737+ mi_col + dc,
8489- ref_frame1: info.ref1.unwrap_or(NO_REF1),8738+ MiInfo {
8490- mv1: mv16(mv1),8739+ is_inter: true,
8491- mv: mv16(mv),8740+ ref_frame: info.ref_frame,
8492- is_new_mv,8741+ ref_frame1: info.ref1.unwrap_or(NO_REF1),
8493- size: 2,8742+ mv1: mv16(mv1),
8494- size_h: 2,8743+ mv: mv16(mv),
8495- is_global_mv0: false,8744+ is_new_mv,
8496- is_global_mv1: false,8745+ size: 2,
8497- });8746+ size_h: 2,
8747+ is_global_mv0: false,
8748+ is_global_mv1: false,
8749+ },
8750+ );
8498 }8751 }
8499 }8752 }
8500 mode_for_tx = 0;8753 mode_for_tx = 0;
8501 } else {8754 } else {
8502- write_single_ref(enc, cdfs, info.ref_frame, neighbours.above_ref[leaf_mi.1], neighbours.above_ref1[leaf_mi.1], neighbours.left_ref[leaf_mi.0], neighbours.left_ref1[leaf_mi.0]);8755+ write_single_ref(
8756+ enc,
8757+ cdfs,
8758+ info.ref_frame,
8759+ neighbours.above_ref[leaf_mi.1],
8760+ neighbours.above_ref1[leaf_mi.1],
8761+ neighbours.left_ref[leaf_mi.0],
8762+ neighbours.left_ref1[leaf_mi.0],
8763+ );
8503 8764
8504- let stack = find_mv_stack(8765+ let stack = find_mv_stack(
8505- grid,8766+ grid,
8506- mi_row,8767+ mi_row,
8507- mi_col,8768+ mi_col,
8508- 2,8769+ 2,
8509- 2,8770+ 2,
8510- info.ref_frame,8771+ info.ref_frame,
8511- mi_cols as usize,8772+ mi_cols as usize,
8512- mi_rows as usize,8773+ mi_rows as usize,
8513- );8774+ );
8514 8775
8515- let (mv, is_new_mv) = write_inter_mode(enc, cdfs, info, &stack)?;8776+ let (mv, is_new_mv) = write_inter_mode(enc, cdfs, info, &stack)?;
8516- for dr in 0..2 {8777+ for dr in 0..2 {
8517- for dc in 0..2 {8778+ for dc in 0..2 {
8518- grid.set(8779+ grid.set(
8519- mi_row + dr,8780+ mi_row + dr,
8520- mi_col + dc,8781+ mi_col + dc,
8521- MiInfo {8782+ MiInfo {
8522- is_inter: true,8783+ is_inter: true,
8523- ref_frame: info.ref_frame,8784+ ref_frame: info.ref_frame,
8524- ref_frame1: NO_REF1,8785+ ref_frame1: NO_REF1,
8525- mv1: (0, 0),8786+ mv1: (0, 0),
8526- mv: mv16(mv),8787+ mv: mv16(mv),
8527- is_new_mv,8788+ is_new_mv,
8528- size: 2,8789+ size: 2,
8529- size_h: 2,8790+ size_h: 2,
8530- is_global_mv0: false,8791+ is_global_mv0: false,
8531- is_global_mv1: false,8792+ is_global_mv1: false,
8532- },8793+ },
8533- );8794+ );
8795+ }
8534 }8796 }
8535- }8797+ write_motion_mode(
8536- write_motion_mode(8798+ enc,
8537- enc,8799+ cdfs,
8538- cdfs,8800+ grid,
8539- grid,8801+ (mi_row, mi_col),
8540- (mi_row, mi_col),8802+ (2, 2),
8541- (2, 2),8803+ (8, 8),
8542- (8, 8),8804+ (mi_cols as usize, mi_rows as usize),
8543- (mi_cols as usize, mi_rows as usize),8805+ info.ref_frame,
8544- info.ref_frame,8806+ block.motion_mode,
8545- block.motion_mode,8807+ )?;
8546- )?;8808+ mode_for_tx = 0;
8547- mode_for_tx = 0;
8548 }8809 }
8549 } else {8810 } else {
8550 let mode = usize::from(block.mode);8811 let mode = usize::from(block.mode);
8551 enc.symbol(mode, &mut cdfs.y_mode[SIZE_GROUP_8]);8812 enc.symbol(mode, &mut cdfs.y_mode[SIZE_GROUP_8]);
8552 if (V_PRED..=D67_PRED).contains(&mode) {8813 if (V_PRED..=D67_PRED).contains(&mode) {
8553 enc.symbol(8814 enc.symbol(
8554- (ANGLE_DELTA_ZERO as i32 + i32::from(block.angle_delta_y)) as usize,8815+ (ANGLE_DELTA_ZERO as i32 + i32::from(block.angle_delta_y)) as usize,
8555- &mut cdfs.angle_delta[mode - V_PRED],8816+ &mut cdfs.angle_delta[mode - V_PRED],
8556- );8817+ );
8557 }8818 }
8558 let uv_mode = usize::from(block.uv_mode);8819 let uv_mode = usize::from(block.uv_mode);
8559 enc.symbol(uv_mode, &mut cdfs.uv_mode_cfl[mode]);8820 enc.symbol(uv_mode, &mut cdfs.uv_mode_cfl[mode]);
@@ -8606,7 +8867,7 @@ fn write_inter_frame_leaf8(
8606 } else {8867 } else {
8607 [TxbSet::Luma8, TxbSet::Chroma4, TxbSet::Chroma4]8868 [TxbSet::Luma8, TxbSet::Chroma4, TxbSet::Chroma4]
8608 };8869 };
8609- let tx = if tx_select {8870+ let tx = if tx_select {
8610 write_tx_syntax_inter(8871 write_tx_syntax_inter(
8611 enc,8872 enc,
8612 cdfs,8873 cdfs,
@@ -8628,7 +8889,13 @@ fn write_inter_frame_leaf8(
8628 vec![0i32; TX4 * TX4],8889 vec![0i32; TX4 * TX4],
8629 ];8890 ];
8630 neighbours.record_mi(leaf_mi, 8, &zero_grids);8891 neighbours.record_mi(leaf_mi, 8, &zero_grids);
8631- neighbours.record_inter_mi(leaf_mi, 8, block.skip, block.inter.is_some(), block_ref(block));8892+ neighbours.record_inter_mi(
8893+ leaf_mi,
8894+ 8,
8895+ block.skip,
8896+ block.inter.is_some(),
8897+ block_ref(block),
8898+ );
8632 record_block_compound(neighbours, leaf_mi, 8, block);8899 record_block_compound(neighbours, leaf_mi, 8, block);
8633 } else {8900 } else {
8634 let grids = [8901 let grids = [
@@ -8638,7 +8905,16 @@ fn write_inter_frame_leaf8(
8638 ];8905 ];
8639 if split {8906 if split {
8640 write_luma_tus(8907 write_luma_tus(
8641- enc, cdfs, neighbours, leaf_mi, 8, tx, &grids[0], mode_for_tx, all_scans, is_inter,8908+ enc,
8909+ cdfs,
8910+ neighbours,
8911+ leaf_mi,
8912+ 8,
8913+ tx,
8914+ &grids[0],
8915+ mode_for_tx,
8916+ all_scans,
8917+ is_inter,
8642 &block.luma_tx_types,8918 &block.luma_tx_types,
8643 )?;8919 )?;
8644 }8920 }
@@ -8669,8 +8945,8 @@ mod tests {
8669 use crate::sequence::sequence_header_obu;8945 use crate::sequence::sequence_header_obu;
8670 use ec_av1_syntax::sequence::SequenceHeader;8946 use ec_av1_syntax::sequence::SequenceHeader;
8671 use ec_av1_syntax::{8947 use ec_av1_syntax::{
8672- FrameHeader, FrameType, LoopFilterParams, PRIMARY_REF_NONE, QuantizationParams, TileInfo,8948+ FrameHeader, FrameType, LoopFilterParams, QuantizationParams, TileInfo, TxMode,
8673- TxMode,8949+ PRIMARY_REF_NONE,
8674 };8950 };
8675 use std::io::Write;8951 use std::io::Write;
8676 use std::process::{Command, Stdio};8952 use std::process::{Command, Stdio};
@@ -10327,7 +10603,11 @@ mod tests {
10327 .map(|&v| {10603 .map(|&v| {
10328 let m = f64::from(v).abs() + 0.5;10604 let m = f64::from(v).abs() + 0.5;
10329 let l = if m < 1.0 { 0 } else { m.floor() as i32 };10605 let l = if m < 1.0 { 0 } else { m.floor() as i32 };
10330- if v < 0.0 { -l } else { l }10606+ if v < 0.0 {
10607+ -l
10608+ } else {
10609+ l
10610+ }
10331 })10611 })
10332 .collect();10612 .collect();
10333 let before = levels.clone();10613 let before = levels.clone();
@@ -10340,18 +10620,40 @@ mod tests {
10340 .zip(&scaled)10620 .zip(&scaled)
10341 .enumerate()10621 .enumerate()
10342 .map(|(i, (&l, &s))| {10622 .map(|(i, (&l, &s))| {
10343- (if i == 0 { dc_weight } else { 1.0 })10623+ (if i == 0 { dc_weight } else { 1.0 }) * (f64::from(s) - f64::from(l)).powi(2)
10344- * (f64::from(s) - f64::from(l)).powi(2)
10345 })10624 })
10346 .sum();10625 .sum();
10347 d + lambda * coeff_bits(grid, set, crate::decode::q_ctx_of(q), 0, 0)10626 d + lambda * coeff_bits(grid, set, crate::decode::q_ctx_of(q), 0, 0)
10348 };10627 };
10349- let bits = rdoq(&mut levels, &scaled, side, q, set, 0, 0, lambda, TxType::DctDct);10628+ let bits = rdoq(
10350- assert_eq!(bits, coeff_bits(&levels, set, crate::decode::q_ctx_of(q), 0, 0));10629+ &mut levels,
10351- assert!(cost(&levels) <= cost(&before), "{:?} vs {:?}", cost(&levels), cost(&before));10630+ &scaled,
10631+ side,
10632+ q,
10633+ set,
10634+ 0,
10635+ 0,
10636+ lambda,
10637+ TxType::DctDct,
10638+ );
10639+ assert_eq!(
10640+ bits,
10641+ coeff_bits(&levels, set, crate::decode::q_ctx_of(q), 0, 0)
10642+ );
10643+ assert!(
10644+ cost(&levels) <= cost(&before),
10645+ "{:?} vs {:?}",
10646+ cost(&levels),
10647+ cost(&before)
10648+ );
10352 // The tail is what it is for: the last coded position moves earlier.10649 // The tail is what it is for: the last coded position moves earlier.
10353 let last = |g: &[i32]| scan_of(side).iter().rposition(|&p| g[usize::from(p)] != 0);10650 let last = |g: &[i32]| scan_of(side).iter().rposition(|&p| g[usize::from(p)] != 0);
10354- assert!(last(&levels) < last(&before), "eob {:?} -> {:?}", last(&before), last(&levels));10651+ assert!(
10652+ last(&levels) < last(&before),
10653+ "eob {:?} -> {:?}",
10654+ last(&before),
10655+ last(&levels)
10656+ );
10355 // A 64-point transform is priced on its top-left 32x32 corner, and10657 // A 64-point transform is priced on its top-left 32x32 corner, and
10356 // the pass has to hand back THAT price (the dense path).10658 // the pass has to hand back THAT price (the dense path).
10357 let (side, set) = (64usize, TxbSet::Luma64);10659 let (side, set) = (64usize, TxbSet::Luma64);
@@ -10364,14 +10666,31 @@ mod tests {
10364 .map(|&v| {10666 .map(|&v| {
10365 let m = f64::from(v).abs() + 0.5;10667 let m = f64::from(v).abs() + 0.5;
10366 let l = if m < 1.0 { 0 } else { m.floor() as i32 };10668 let l = if m < 1.0 { 0 } else { m.floor() as i32 };
10367- if v < 0.0 { -l } else { l }10669+ if v < 0.0 {
10670+ -l
10671+ } else {
10672+ l
10673+ }
10368 })10674 })
10369 .collect();10675 .collect();
10370- let bits = rdoq(&mut levels, &scaled, side, q, set, 0, 0, lambda, TxType::DctDct);10676+ let bits = rdoq(
10677+ &mut levels,
10678+ &scaled,
10679+ side,
10680+ q,
10681+ set,
10682+ 0,
10683+ 0,
10684+ lambda,
10685+ TxType::DctDct,
10686+ );
10371 let corner: Vec<i32> = (0..32)10687 let corner: Vec<i32> = (0..32)
10372 .flat_map(|row| levels[row * side..][..32].to_vec())10688 .flat_map(|row| levels[row * side..][..32].to_vec())
10373 .collect();10689 .collect();
10374- assert_eq!(bits, coeff_bits(&corner, set, crate::decode::q_ctx_of(q), 0, 0));10690+ assert_eq!(
10691+ bits,
10692+ coeff_bits(&corner, set, crate::decode::q_ctx_of(q), 0, 0)
10693+ );
10375 }10694 }
10376 10695
10377 #[test]10696 #[test]
@@ -10848,7 +11167,11 @@ mod tests {
10848 (d << 3) | (fr << 1) | 111167 (d << 3) | (fr << 1) | 1
10849 };11168 };
10850 let mag = mv_class_base(class) + local as i32 + 1;11169 let mag = mv_class_base(class) + local as i32 + 1;
10851- if sign == 1 { -mag } else { mag }11170+ if sign == 1 {
11171+ -mag
11172+ } else {
11173+ mag
11174+ }
10852 }11175 }
10853 11176
10854 /// Decodes one superblock a [`sb_coeff_inter_frame_tile`] payload wrote,11177 /// Decodes one superblock a [`sb_coeff_inter_frame_tile`] payload wrote,