diff --git a/crates/ec-av1/examples/decode_probe.rs b/crates/ec-av1/examples/decode_probe.rs index fb8fc195..babb6dad 100644 --- a/crates/ec-av1/examples/decode_probe.rs +++ b/crates/ec-av1/examples/decode_probe.rs @@ -45,6 +45,11 @@ fn main() { println!("sub8_inter_split: groups={}", ec_av1::decode::sub8_inter_split_hits()); let (h84, h48) = ec_av1::decode::sub8_inter_rect_hits(); println!("sub8_inter_rect: horz8x4={h84} vert4x8={h48}"); + let si = ec_av1::stream::sub8_intra_rect_hits(); + println!( + "sub8_intra_rect: horz8x4={} vert4x8={} chroma_ref={} mixed={}", + si.0, si.1, si.2, si.3 + ); let leaf = ec_av1::stream::vartx_rect_leaf_hits(); println!("vartx_rect_leaf: 32x16={} 16x32={}", leaf[0], leaf[1]); // lane-inter16ab r1: the 16x16-level inter AB arms, so a recipe sweep diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index 5528a793..4f6e9f64 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -3117,6 +3117,21 @@ thread_local! { /// inter leaves (lane-intersub8 r3), indexed `[0] = HORZ` (two BLOCK_8X4), /// `[1] = VERT` (two BLOCK_4X8). static SUB8_INTER_RECT_HITS: std::cell::Cell<[usize; 2]> = const { std::cell::Cell::new([0; 2]) }; + /// lane-sub8intra: INTRA leaves decoded inside an inter frame's sub-8x8 + /// HORZ/VERT partition -- `[0]` 8x4, `[1]` 4x8, `[2]` of those that were + /// the chroma reference (so coded the group's 4x4 chroma intra), `[3]` + /// MIXED groups whose chroma reference is INTER while its sibling is + /// intra (the `is_sub8x8_inter` false path). + static SUB8_INTRA_RECT_HITS: std::cell::Cell<[usize; 4]> = const { std::cell::Cell::new([0; 4]) }; +} + +/// Current value of [`SUB8_INTRA_RECT_HITS`]: `(8x4, 4x8, chroma-reference, +/// mixed groups)`. +pub fn sub8_intra_rect_hits() -> (usize, usize, usize, usize) { + SUB8_INTRA_RECT_HITS.with(|c| { + let h = c.get(); + (h[0], h[1], h[2], h[3]) + }) } /// Current value of [`SUB8_INTER_RECT_HITS`]: `(8x4 groups, 4x8 groups)`. @@ -22621,6 +22636,9 @@ fn decode_inter_sub8_split4( [None; 4]; let mut first_tx_type = TxType::DctDct; let mut last_skip = true; + // Set when the chroma-reference (last) sub-block is INTRA: it coded the + // group's one chroma unit itself. + let mut intra_chroma = false; for i in 0..4usize { let (dr, dc) = (i / 2, i % 2); let lmi = (gr + dr, gc + dc); @@ -22649,10 +22667,48 @@ fn decode_inter_sub8_split4( ); let is_inter = dec.symbol(&mut cdfs.intra_inter[ii_ctx]) == 1; if !is_inter { - return Err(unsupported( - "an intra 4x4 block inside an inter frame's sub-8x8 split (this decoder codes \ - only inter 4x4 sub-blocks there)", - )); + if std::env::var_os("EC_SUB8INTRA").is_some() { + eprintln!( + "EC_SUB8INTRA shape=4x4 sub={i} has_chroma={} skip={skip} mi={rmi},{cmi} tx_select={}", + i == 3, + TX_SELECT_INTER.with(std::cell::Cell::get), + ); + } + // lane-sub8intra: same `EC_SUB8INTRA_DECODE` switch as the + // HORZ/VERT arm; the refusal is the default until the gate is + // green. + if std::env::var_os("EC_SUB8INTRA_DECODE").is_none() { + return Err(unsupported( + "an intra 4x4 block inside an inter frame's sub-8x8 split (this decoder codes \ + only inter 4x4 sub-blocks there)", + )); + } + // lane-sub8intra: the `BLOCK_4X4` twin of the HORZ/VERT arm -- + // same [`decode_intra_sub8_leaf`], no tx-depth symbol, chroma on + // the LAST sub-block (`is_chroma_reference` under 4:2:0). + decode_intra_sub8_leaf( + dec, + cdfs, + neighbours, + grid, + group_mi, + lmi, + (B4, B4), + i == 3, + skip, + scan4, + y, + u, + v, + base_q_idx, + ENABLE_FILTER_INTRA_INTER.with(std::cell::Cell::get), + TX_SELECT_INTER.with(std::cell::Cell::get), + reduced_tx_set, + )?; + if i == 3 { + intra_chroma = true; + } + continue; } // Same print point as the oracle's `EC_MODE` (entry of // `read_inter_block_mode_info`, i.e. after skip / is_inter), so the @@ -22873,11 +22929,14 @@ fn decode_inter_sub8_split4( None, None, )?; - if i == 0 { + if i == 0 || piece[0].is_none() { // `av1_get_tx_type`: an inter block's chroma inherits the // tx_type of the CO-LOCATED luma position, which for the // group's one chroma unit is luma (0, 0) -- the FIRST - // sub-block, not the last. + // sub-block, not the last. lane-sub8intra: an INTRA first + // sub-block writes no inter tx_type there at all, so the + // later inter sub-blocks' own is what `xd->tx_type_map` + // holds. first_tx_type = tx_type; } neighbours.record_mi_luma_rect(lmi, B4, B4, &luma_grid); @@ -22894,59 +22953,98 @@ fn decode_inter_sub8_split4( neighbours.fill_lf_grid_rect(lmi, 1, 1, B4 as u8, B4 as u8, ref_frame.max(LAST_FRAME)); last_skip = skip; } - // Chroma, once for the whole group, on the last sub-block's own `skip` - // flag -- prediction from all four sub-blocks (2x2 pieces), residual as - // one TX_4X4 unit per plane. - let mut pred_u = vec![0u16; B4 * B4]; - let mut pred_v = vec![0u16; B4 * B4]; - for i in 0..4usize { - let (dr, dc) = (i / 2, i % 2); - let Some((ref_frame, mv, h_filter, v_filter)) = piece[i] else { - continue; - }; - let (_, sref_u, sref_v) = ref_planes(ref_frame, ref_y, ref_u, ref_v, other_refs)?; - let (ox, oy) = (cpx + dc * 2, cpy + dr * 2); - for (plane, dst) in [(sref_u, &mut pred_u), (sref_v, &mut pred_v)] { - let mut buf = vec![0u16; 4]; - mc::predict_with_filters( - &plane.data, - plane.width, - plane.true_width, - plane.true_height, - mv_to_q4(ox, mv.1, false), - mv_to_q4(oy, mv.0, false), - 2, - 2, - h_filter, - v_filter, - &mut buf, - ); - for row in 0..2 { - for col in 0..2 { - dst[(dr * 2 + row) * B4 + dc * 2 + col] = buf[row * 2 + col]; + // lane-sub8intra: when the chroma-reference sub-block was INTRA it coded + // and reconstructed the group's one chroma unit itself. + if !intra_chroma { + // Chroma, once for the whole group, on the last sub-block's own `skip` + // flag -- prediction from all four sub-blocks (2x2 pieces), residual as + // one TX_4X4 unit per plane. + let mut pred_u = vec![0u16; B4 * B4]; + let mut pred_v = vec![0u16; B4 * B4]; + // `is_sub8x8_inter` (reconinter_template.inc:54) is false as soon as ANY + // mi of the group is intra: the group's 4x4 chroma is then predicted WHOLE + // from the chroma-reference (last) sub-block's own mv, not in 2x2 pieces. + let mixed = piece.iter().any(Option::is_none); + if mixed { + SUB8_INTRA_RECT_HITS.with(|h| { + let mut st = h.get(); + st[3] += 1; + h.set(st); + }); + } + for i in 0..4usize { + let (dr, dc) = (i / 2, i % 2); + if mixed && i != 3 { + continue; + } + let Some((ref_frame, mv, h_filter, v_filter)) = piece[i] else { + continue; + }; + let (_, sref_u, sref_v) = ref_planes(ref_frame, ref_y, ref_u, ref_v, other_refs)?; + let (ox, oy) = if mixed { + (cpx, cpy) + } else { + (cpx + dc * 2, cpy + dr * 2) + }; + let (pw, ph) = if mixed { (B4, B4) } else { (2, 2) }; + let (row0, col0) = if mixed { (0, 0) } else { (dr * 2, dc * 2) }; + for (plane, dst) in [(sref_u, &mut pred_u), (sref_v, &mut pred_v)] { + let mut buf = vec![0u16; pw * ph]; + mc::predict_with_filters( + &plane.data, + plane.width, + plane.true_width, + plane.true_height, + mv_to_q4(ox, mv.1, false), + mv_to_q4(oy, mv.0, false), + pw, + ph, + h_filter, + v_filter, + &mut buf, + ); + for row in 0..ph { + for col in 0..pw { + dst[(row0 + row) * B4 + col0 + col] = buf[row * pw + col]; + } } } } + let (u_grid, v_grid) = if last_skip { + u.reconstruct_mc(cpx, cpy, B4, &pred_u, &vec![0i32; B4 * B4]); + v.reconstruct_mc(cpx, cpy, B4, &pred_v, &vec![0i32; B4 * B4]); + (vec![0i32; B4 * B4], vec![0i32; B4 * B4]) + } else { + let around = neighbours.around_mi(group_mi, 8); + let ug = read_inter_plane( + dec, cdfs, TxbSet::Chroma4, scan4, 1, around[1], 0, u, cpx, cpy, B4, base_q_idx, + &pred_u, Some(first_tx_type), None, + )? + .0; + let vg = read_inter_plane( + dec, cdfs, TxbSet::Chroma4, scan4, 2, around[2], 0, v, cpx, cpy, B4, base_q_idx, + &pred_v, Some(first_tx_type), None, + )? + .0; + (ug, vg) + }; + neighbours.record_uv_mode_mi(gr, gc, 2, 2, DC_PRED); + let round_up_even = |n: usize| n.div_ceil(2) * 2; + let bound_h = round_up_even(neighbours.mi_rows); + let bound_w = round_up_even(neighbours.mi_cols); + let u_state = neighbour_state(&u_grid); + let v_state = neighbour_state(&v_grid); + for cell in 0..2usize { + if cell < 2usize.min(bound_h.saturating_sub(gr)) { + neighbours.left[gr + cell][1] = u_state; + neighbours.left[gr + cell][2] = v_state; + } + if cell < 2usize.min(bound_w.saturating_sub(gc)) { + neighbours.above[gc + cell][1] = u_state; + neighbours.above[gc + cell][2] = v_state; + } + } } - let (u_grid, v_grid) = if last_skip { - u.reconstruct_mc(cpx, cpy, B4, &pred_u, &vec![0i32; B4 * B4]); - v.reconstruct_mc(cpx, cpy, B4, &pred_v, &vec![0i32; B4 * B4]); - (vec![0i32; B4 * B4], vec![0i32; B4 * B4]) - } else { - let around = neighbours.around_mi(group_mi, 8); - let ug = read_inter_plane( - dec, cdfs, TxbSet::Chroma4, scan4, 1, around[1], 0, u, cpx, cpy, B4, base_q_idx, - &pred_u, Some(first_tx_type), None, - )? - .0; - let vg = read_inter_plane( - dec, cdfs, TxbSet::Chroma4, scan4, 2, around[2], 0, v, cpx, cpy, B4, base_q_idx, - &pred_v, Some(first_tx_type), None, - )? - .0; - (ug, vg) - }; - neighbours.record_uv_mode_mi(gr, gc, 2, 2, DC_PRED); // Same two neighbour tails [`decode_leaf_split4`] runs: the partition // context of a `BLOCK_4X4` subsize (`partition_context_lookup` bit 0 = 1, // reached by `side_mi = 4` here) and the group's chroma coefficient state. @@ -22954,6 +23052,389 @@ fn decode_inter_sub8_split4( neighbours.left_side_mi[gr + cell] = 4; neighbours.above_side_mi[gc + cell] = 4; } + Ok(()) +} + +/// One INTRA 8x4/4x8 leaf inside an INTER frame's sub-8x8 HORZ/VERT partition +/// (lane-sub8intra) -- the shape [`decode_inter_sub8_rect2`] refused by name. +/// Mode syntax is `read_intra_block_mode_info`'s (decodemv.c:1065), not the key +/// frame's above/left pair: `y_mode[size_group_lookup[BLOCK_8X4] == 0]`, NO +/// angle delta (`av1_use_angle_delta` needs `bsize >= BLOCK_8X8`), `uv_mode` + +/// CfL alphas only on the chroma-reference sub-block (`is_chroma_reference`: +/// the SECOND one under 4:2:0), then `filter_intra` off BLOCK_8X4/BLOCK_4X8's +/// own class row (`av1_filter_intra_allowed_bsize` is `wide <= 32 && high <= +/// 32`, so this shape is allowed). Everything below the mode read -- the tx +/// depth symbol, the whole-leaf `TX_8X4`/`TX_4X8` or split `TX_4X4` walk, the +/// 4x4 chroma unit at the group's origin -- is [`decode_leaf_rect8`]'s body for +/// the same shape; the INTER-frame differences are the tx-depth context +/// (`above_txfm`/`left_txfm`, not the key frame's deblock-grid approximation), +/// `set_txfm_ctxs` publication, and the inter-side bands + mi map the caller's +/// inter leaves also write (`is_inter = 0`, ref `INTRA_FRAME`, no interp +/// filter). +#[allow(clippy::too_many_arguments)] +fn decode_intra_sub8_leaf( + dec: &mut SymbolDecoder, + cdfs: &mut Cdfs, + neighbours: &mut Neighbours, + grid: &mut MiGrid, + group_mi: (usize, usize), + lmi: (usize, usize), + // `(bw, bh)`: 8x4 / 4x8 under HORZ/VERT, 4x4 under SPLIT. + (bw, bh): (usize, usize), + has_chroma: bool, + skip: bool, + scan4: &[u16], + y: &mut PlaneBuf, + u: &mut PlaneBuf, + v: &mut PlaneBuf, + base_q_idx: u8, + enable_filter_intra: bool, + tx_select: bool, + reduced_tx_set: bool, +) -> Result { + let vert = bw < bh; + let (w_mi, h_mi) = (bw / MI, bh / MI); + let (gr, gc) = group_mi; + let (r, c) = (gr / (SUB / MI), gc / (SUB / MI)); + let (above_mode, left_mode) = neighbours.modes_above_left(r, c); + let leaf_above = neighbours + .mode_above_mi(lmi.0, lmi.1) + .unwrap_or(above_mode); + let leaf_left = neighbours.mode_left_mi(lmi.0, lmi.1).unwrap_or(left_mode); + let step = std::env::var_os("EC_TRACE_MODE_STEP").is_some(); + // `size_group_lookup[BLOCK_8X4] == size_group_lookup[BLOCK_4X8] == 0` + // (libaom common_data.h:61). + let mode = dec.symbol(&mut cdfs.y_mode[0]); + if step { + eprintln!( + "EC_ISTEP mi_row={} mi_col={} name=mode val={mode} rng={}", + lmi.0, + lmi.1, + dec.debug_state().0 + ); + } + // No angle delta below 8x8 (`av1_use_angle_delta`), for luma or chroma. + let angle_delta_y = 0; + let (uv_mode, alpha) = if has_chroma { + // `is_cfl_allowed` is `wide <= 32 && high <= 32` -- true for this + // shape, so the 14-symbol CFL alphabet is the right one. + let uv_mode = dec.symbol(&mut cdfs.uv_mode_cfl[mode]); + if (9..=12).contains(&uv_mode) { + SMOOTH_UV_HITS.with(|c| c.set(c.get() + 1)); + } + let alpha = if uv_mode == UV_CFL_PRED { + Some(read_cfl_alphas(dec, cdfs)) + } else { + None + }; + if (V_PRED..=D67_PRED).contains(&uv_mode) { + DIRECTIONAL_UV_HITS.with(|c| c.set(c.get() + 1)); + } + (uv_mode, alpha) + } else { + (DC_PRED, None) + }; + let mut filter_intra = None; + if mode == DC_PRED && enable_filter_intra { + let fi_class = if bw == bh { + filter_intra_size_class(bw).expect("BLOCK_4X4 has its own filter_intra row") + } else { + filter_intra_size_class_rect(bw, bh) + .expect("BLOCK_4X8/BLOCK_8X4 are inside av1_filter_intra_allowed_bsize") + }; + if dec.symbol(&mut cdfs.filter_intra[fi_class]) != 0 { + FILTER_INTRA_HITS.with(|c| c.set(c.get() + 1)); + FILTER_INTRA_RECT_HITS.with(|c| c.set(c.get() + 1)); + FILTER_INTRA_RECT_SUB8_HITS.with(|c| c.set(c.get() + 1)); + filter_intra = Some(dec.symbol(&mut cdfs.filter_intra_mode)); + } + } + // `read_block_tx_size`' non-var-tx arm: an INTRA block of an inter frame + // reads `read_tx_size(allow_select = 1)` -- `bsize_to_tx_size_cat + // (BLOCK_4X8) == 0` (blockd.h:1344, depth table entry 1), the same + // 2-symbol category [`decode_leaf_rect8`] reads -- with the var-tx + // context bands, which on an inter frame are live. + // `block_signals_txsize(BLOCK_4X4)` is false (blockd.h:1027): the 4x4 leaf + // codes no depth symbol at all, its transform is its own size. + let depth = if tx_select && bw != bh { + let ctx = tx_size_context_txfm_rect(neighbours, lmi, bw, bh); + let d = dec.symbol(&mut cdfs.tx_size_cat0[ctx]); + if step { + eprintln!( + "EC_ISTEP mi_row={} mi_col={} name=tx_depth val={d} ctx={ctx} cat=0 rng={}", + lmi.0, + lmi.1, + dec.debug_state().0 + ); + } + d + } else { + 0 + }; + if depth != 0 { + TX_DEPTH_HITS.with(|c| c.set(c.get() + 1)); + RECT8_SPLIT_TX_HITS.with(|c| c.set(c.get() + 1)); + } + let split = depth != 0; + let (px, py) = (lmi.1 * MI, lmi.0 * MI); + let reach = if bw == bh { + Reach::of(bw, px, py, y.width, y.height) + } else { + Reach::of_rect(bw, bh, px, py, y.width, y.height) + }; + let smooth_neighbor = is_smooth_mode(leaf_above) || is_smooth_mode(leaf_left); + if smooth_neighbor { + SMOOTH_LUMA_HITS.with(|c| c.set(c.get() + 1)); + } + neighbours.record_mode_mi(lmi.0, lmi.1, w_mi, h_mi, mode); + let scan: &[u16] = if vert { &SCAN_4X8 } else { &SCAN_8X4 }; + if split { + // `sub_tx_size_map[TX_4X8] == TX_4X4`: two 4x4 units along the long + // axis, each predicted off the previous one's reconstruction. + for tu in 0..2usize { + let tu_mi = if vert { + (lmi.0 + tu, lmi.1) + } else { + (lmi.0, lmi.1 + tu) + }; + let (tu_px, tu_py) = (tu_mi.1 * MI, tu_mi.0 * MI); + let tu_reach = tu_reach( + bw, + bh, + tu_px - px, + tu_py - py, + 4, + reach, + px, + py, + y.width, + y.height, + ); + if skip { + let zeros = vec![0i32; 16]; + y.reconstruct( + tu_px, tu_py, 4, mode, angle_delta_y, tu_reach, &zeros, None, filter_intra, + smooth_neighbor, + ); + neighbours.record_mi_luma(tu_mi, 4, &zeros); + continue; + } + let tu_around = neighbours.around_mi(tu_mi, 4)[0]; + let tu_skip_ctx = neighbours.luma_skip_ctx(tu_mi, 1); + let tu_grid = read_plane( + dec, + cdfs, + if reduced_tx_set { + TxbSet::Luma4 + } else { + TxbSet::Luma4Set1 + }, + scan4, + 0, + tu_around, + mode, + mode, + angle_delta_y, + tu_reach, + y, + tu_px, + tu_py, + 4, + TX4, + base_q_idx, + None, + filter_intra, + Some(tu_skip_ctx), + smooth_neighbor, + )?; + neighbours.record_mi_luma(tu_mi, 4, &tu_grid); + } + } else if bw == bh { + // A `BLOCK_4X4` leaf's luma TU *is* the whole block, so `get_txb_ctx` + // fixes `txb_skip_ctx` at 0 (`luma_skip_ctx: None`) -- the same square + // reader [`decode_leaf_split4`] uses. + if skip { + y.reconstruct( + px, py, 4, mode, angle_delta_y, reach, &vec![0i32; 16], None, filter_intra, + smooth_neighbor, + ); + neighbours.record_mi_luma(lmi, 4, &vec![0i32; 16]); + } else { + let tu_around = neighbours.around_mi(lmi, 4)[0]; + let tu_grid = read_plane( + dec, + cdfs, + if reduced_tx_set { + TxbSet::Luma4 + } else { + TxbSet::Luma4Set1 + }, + scan4, + 0, + tu_around, + mode, + mode, + angle_delta_y, + reach, + y, + px, + py, + 4, + TX4, + base_q_idx, + None, + filter_intra, + None, + smooth_neighbor, + )?; + neighbours.record_mi_luma(lmi, 4, &tu_grid); + } + } else { + let grid_levels = if skip { + let zeros = vec![0i32; bw * bh]; + y.reconstruct_rect( + px, py, bw, bh, mode, angle_delta_y, reach, &zeros, None, filter_intra, + smooth_neighbor, + ); + zeros + } else { + // The leaf's luma TU *is* the leaf, so `get_txb_ctx` fixes + // `txb_skip_ctx` at 0. + let around = neighbours.around_mi_rect(lmi, bw, bh)[0]; + let set = if reduced_tx_set { + TxbSet::LumaRect4x8 + } else { + TxbSet::LumaRect4x8Set1 + }; + let mut coding = cdfs.txb(set, fi_tx_row(mode, filter_intra)); + let (levels, tx_type) = read_coeffs_rect( + dec, + &mut coding, + scan, + bw, + bh, + 0, + dc_sign_ctx(around.2), + TxType::DctDct, + )?; + let residual = dequant_and_inverse_typed_wh( + &levels, + bw, + bh, + crate::decode::bit_depth(), + CURRENT_Q_IDX.with(|q| q.get()), + plane_q_delta(0).0, + plane_q_delta(0).1, + tx_type, + ); + y.reconstruct_rect( + px, py, bw, bh, mode, angle_delta_y, reach, &residual, None, filter_intra, + smooth_neighbor, + ); + levels + }; + let state = neighbour_state(&grid_levels); + for cell in 0..h_mi { + if lmi.0 + cell < neighbours.mi_rows { + neighbours.left[lmi.0 + cell][0] = state; + } + } + for cell in 0..w_mi { + if lmi.1 + cell < neighbours.mi_cols { + neighbours.above[lmi.1 + cell][0] = state; + } + } + } + let (tx_w, tx_h) = if split { (4, 4) } else { (bw, bh) }; + neighbours.fill_skip_grid_rect(lmi, w_mi, h_mi, skip); + neighbours.fill_lf_grid_rect(lmi, w_mi, h_mi, tx_w as u8, tx_h as u8, 0); + // `set_txfm_ctxs(tx_size, n4_w, n4_h, skip && is_inter == 0, xd)`: an + // intra block of an inter frame publishes its TRANSFORM dims, never its + // block dims (blockd.h). Missing this left the next block reading the + // previous block's band (the lane-intra16x4 finding for the 16x4 strip). + for i in 0..w_mi { + if let Some(cell) = neighbours.above_txfm.get_mut(lmi.1 + i) { + *cell = tx_w as u8; + } + } + for i in 0..h_mi { + if let Some(cell) = neighbours.left_txfm.get_mut(lmi.0 + i) { + *cell = tx_h as u8; + } + } + neighbours.above_mode[c] = mode; + neighbours.left_mode[r] = mode; + // The inter-side bands and the mi map, exactly as the square + // intra-in-inter block writes them: `is_inter = 0`, `INTRA_FRAME` ref, no + // interpolation filter, no skip mode. + neighbours.record_inter_rect_mi(lmi, w_mi, h_mi, skip, false, 0, [3, 3], false); + for dr in 0..h_mi { + for dc in 0..w_mi { + grid.set( + lmi.0 + dr, + lmi.1 + dc, + MiInfo { + is_inter: false, + ref_frame: -1, + ref_frame1: None, + mv1: None, + mv: (0, 0), + is_new_mv: false, + size: w_mi, + size_h: h_mi, + is_global_mv0: false, + is_global_mv1: false, + }, + ); + } + } + SUB8_INTRA_RECT_HITS.with(|h| { + let mut s = h.get(); + s[usize::from(vert)] += 1; + s[2] += usize::from(has_chroma); + h.set(s); + }); + if !has_chroma { + return Ok(mode); + } + // The group's single 4x4 chroma unit, intra-predicted from THIS + // (chroma-reference) sub-block's own `uv_mode` -- `is_sub8x8_inter` + // (reconinter_template.inc:54) returns false as soon as any mi of the + // group is intra, and the decoder reconstructs chroma with the + // chroma-reference block's own type. Same body as [`decode_leaf_rect8`]'s + // post-loop chroma. + let (gpx, gpy) = (gc * MI, gr * MI); + let (cpx, cpy) = (gpx / 2, gpy / 2); + let group_reach = Reach::of(8, gpx, gpy, y.width, y.height); + let smooth_neighbor_uv = neighbours.smooth_uv_neighbour(gr, gc, r, c); + let uv_predict_mode = if uv_mode == UV_CFL_PRED { DC_PRED } else { uv_mode }; + let angle_delta_uv = 0; + neighbours.above_uv_mode[c] = uv_predict_mode; + neighbours.left_uv_mode[r] = uv_predict_mode; + neighbours.record_uv_mode_mi(gr, gc, 2, 2, uv_predict_mode); + let ac = alpha.map(|_| cfl_ac_q3(y, gpx, gpy, 8)); + let (u_grid, v_grid): (Vec, Vec) = if skip { + if alpha.is_some() { + CHROMA_SKIP_CFL_HITS.with(|h| h.set(h.get() + 1)); + SKIP_CFL_HITS.with(|c| c.set(c.get() + 1)); + } + u.reconstruct(cpx, cpy, 4, uv_predict_mode, angle_delta_uv, group_reach, &vec![0i32; 16], alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), None, smooth_neighbor_uv); + v.reconstruct(cpx, cpy, 4, uv_predict_mode, angle_delta_uv, group_reach, &vec![0i32; 16], alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), None, smooth_neighbor_uv); + (vec![0i32; 16], vec![0i32; 16]) + } else { + let chroma_around = neighbours.around_mi(group_mi, 8); + let ug = read_plane( + dec, cdfs, TxbSet::Chroma4, scan4, 1, chroma_around[1], uv_mode, uv_predict_mode, + angle_delta_uv, group_reach, u, cpx, cpy, 4, TX4, base_q_idx, + alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), None, None, smooth_neighbor_uv, + )?; + let vg = read_plane( + dec, cdfs, TxbSet::Chroma4, scan4, 2, chroma_around[2], uv_mode, uv_predict_mode, + angle_delta_uv, group_reach, v, cpx, cpy, 4, TX4, base_q_idx, + alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), None, None, smooth_neighbor_uv, + )?; + (ug, vg) + }; let round_up_even = |n: usize| n.div_ceil(2) * 2; let bound_h = round_up_even(neighbours.mi_rows); let bound_w = round_up_even(neighbours.mi_cols); @@ -22969,7 +23450,7 @@ fn decode_inter_sub8_split4( neighbours.above[gc + cell][2] = v_state; } } - Ok(()) + Ok(mode) } /// Decodes a `PARTITION_HORZ`/`PARTITION_VERT` of one 8x8 INTER block into two @@ -23059,6 +23540,9 @@ fn decode_inter_sub8_rect2( [None; 2]; let mut first_tx_type = TxType::DctDct; let mut last_skip = true; + // Set when the chroma-reference sub-block is INTRA: it coded the group's + // one chroma unit itself. + let mut intra_chroma = false; for i in 0..2usize { let lmi = if vert { (gr, gc + i) } else { (gr + i, gc) }; if lmi.0 >= mi_rows as usize || lmi.1 >= mi_cols as usize { @@ -23086,10 +23570,53 @@ fn decode_inter_sub8_rect2( ); let is_inter = dec.symbol(&mut cdfs.intra_inter[ii_ctx]) == 1; if !is_inter { - return Err(unsupported( - "an intra 8x4/4x8 block inside an inter frame's sub-8x8 HORZ/VERT partition \ - (this decoder codes only inter sub-blocks there)", - )); + if std::env::var_os("EC_SUB8INTRA").is_some() { + eprintln!( + "EC_SUB8INTRA shape={}x{} sub={i} has_chroma={} skip={skip} mi={rmi},{cmi} tx_select={}", + bw, + bh, + i == 1, + TX_SELECT_INTER.with(std::cell::Cell::get), + ); + } + // lane-sub8intra: the leaf decodes ([`decode_intra_sub8_leaf`]) + // only under `EC_SUB8INTRA_DECODE` -- the r1 gate is RED (a + // 128x128 8-bit witness desyncs a few blocks later), so the + // refusal stays the default behaviour (class + // `refusal-lifted-without-a-gate`). + if std::env::var_os("EC_SUB8INTRA_DECODE").is_none() { + return Err(unsupported( + "an intra 8x4/4x8 block inside an inter frame's sub-8x8 HORZ/VERT partition \ + (this decoder codes only inter sub-blocks there)", + )); + } + // Its chroma rule is `is_sub8x8_inter`'s: when the + // chroma-reference (second) sub-block is the intra one, the + // group's 4x4 chroma is coded and predicted INTRA there and the + // inter chroma tail below is skipped entirely. + decode_intra_sub8_leaf( + dec, + cdfs, + neighbours, + grid, + group_mi, + lmi, + (bw, bh), + i == 1, + skip, + scan4, + y, + u, + v, + base_q_idx, + ENABLE_FILTER_INTRA_INTER.with(std::cell::Cell::get), + TX_SELECT_INTER.with(std::cell::Cell::get), + reduced_tx_set, + )?; + if i == 1 { + intra_chroma = true; + } + continue; } if std::env::var_os("EC_TRACE_MODE").is_some() { eprintln!( @@ -23349,10 +23876,13 @@ fn decode_inter_sub8_rect2( None, )? }; - if idx == 0 { + if idx == 0 && (i == 0 || piece[0].is_none()) { // `av1_get_tx_type`: the group's one chroma unit scales // back to luma (0, 0), i.e. the FIRST sub-block's first - // transform unit. + // transform unit -- unless that sub-block is INTRA + // (lane-sub8intra), in which case it wrote no inter + // tx_type at all and `xd->tx_type_map[0]` of the + // chroma-reference block is this one's. first_tx_type = tu_tx_type; } neighbours.record_mi_luma_rect(tu_mi, tw, th, &tu_grid); @@ -23372,64 +23902,109 @@ fn decode_inter_sub8_rect2( neighbours.fill_skip_grid_rect(lmi, w_mi, h_mi, skip); last_skip = skip; } - // The group's single 4x4 chroma unit per plane, on the chroma-reference - // (second) sub-block's own skip flag. - const B4: usize = 4; - let mut pred_u = vec![0u16; B4 * B4]; - let mut pred_v = vec![0u16; B4 * B4]; - let (piece_w, piece_h) = if vert { (2usize, 4usize) } else { (4usize, 2usize) }; - for i in 0..2usize { - let Some((ref_frame, mv, h_filter, v_filter)) = piece[i] else { - continue; + // lane-sub8intra: when the chroma-reference sub-block was INTRA it coded + // and reconstructed the group's one chroma unit itself. + if !intra_chroma { + // The group's single 4x4 chroma unit per plane, on the chroma-reference + // (second) sub-block's own skip flag. + const B4: usize = 4; + let mut pred_u = vec![0u16; B4 * B4]; + let mut pred_v = vec![0u16; B4 * B4]; + // `is_sub8x8_inter` (reconinter_template.inc:54) is false as soon as ANY + // mi of the group is intra, so a group whose chroma reference is inter but + // whose sibling is INTRA does NOT get the two-piece sub8x8 prediction: the + // ordinary 8x8-and-bigger path predicts the WHOLE 4x4 chroma unit from the + // chroma-reference block's own mv, at the group's chroma origin + // (`row_start`/`col_start` shift `pre_x`/`pre_y` there either way). + let mixed = piece[0].is_none(); + if mixed { + SUB8_INTRA_RECT_HITS.with(|h| { + let mut s = h.get(); + s[3] += 1; + h.set(s); + }); + } + let (piece_w, piece_h) = match (mixed, vert) { + (true, _) => (B4, B4), + (false, true) => (2usize, 4usize), + (false, false) => (4usize, 2usize), }; - let (_, sref_u, sref_v) = ref_planes(ref_frame, ref_y, ref_u, ref_v, other_refs)?; - let (ox, oy) = if vert { - (cpx + i * piece_w, cpy) + for i in 0..2usize { + let Some((ref_frame, mv, h_filter, v_filter)) = piece[i] else { + continue; + }; + let (_, sref_u, sref_v) = ref_planes(ref_frame, ref_y, ref_u, ref_v, other_refs)?; + let (ox, oy) = if mixed { + (cpx, cpy) + } else if vert { + (cpx + i * piece_w, cpy) + } else { + (cpx, cpy + i * piece_h) + }; + let (dc, dr) = if mixed { + (0, 0) + } else if vert { + (i * piece_w, 0) + } else { + (0, i * piece_h) + }; + for (plane, dst) in [(sref_u, &mut pred_u), (sref_v, &mut pred_v)] { + let mut buf = vec![0u16; piece_w * piece_h]; + mc::predict_with_filters( + &plane.data, + plane.width, + plane.true_width, + plane.true_height, + mv_to_q4(ox, mv.1, false), + mv_to_q4(oy, mv.0, false), + piece_w, + piece_h, + h_filter, + v_filter, + &mut buf, + ); + for row in 0..piece_h { + for col in 0..piece_w { + dst[(dr + row) * B4 + dc + col] = buf[row * piece_w + col]; + } + } + } + } + let (u_grid, v_grid) = if last_skip { + u.reconstruct_mc(cpx, cpy, B4, &pred_u, &vec![0i32; B4 * B4]); + v.reconstruct_mc(cpx, cpy, B4, &pred_v, &vec![0i32; B4 * B4]); + (vec![0i32; B4 * B4], vec![0i32; B4 * B4]) } else { - (cpx, cpy + i * piece_h) + let around = neighbours.around_mi(group_mi, 8); + let ug = read_inter_plane( + dec, cdfs, TxbSet::Chroma4, scan4, 1, around[1], 0, u, cpx, cpy, B4, base_q_idx, + &pred_u, Some(first_tx_type), None, + )? + .0; + let vg = read_inter_plane( + dec, cdfs, TxbSet::Chroma4, scan4, 2, around[2], 0, v, cpx, cpy, B4, base_q_idx, + &pred_v, Some(first_tx_type), None, + )? + .0; + (ug, vg) }; - let (dc, dr) = if vert { (i * piece_w, 0) } else { (0, i * piece_h) }; - for (plane, dst) in [(sref_u, &mut pred_u), (sref_v, &mut pred_v)] { - let mut buf = vec![0u16; piece_w * piece_h]; - mc::predict_with_filters( - &plane.data, - plane.width, - plane.true_width, - plane.true_height, - mv_to_q4(ox, mv.1, false), - mv_to_q4(oy, mv.0, false), - piece_w, - piece_h, - h_filter, - v_filter, - &mut buf, - ); - for row in 0..piece_h { - for col in 0..piece_w { - dst[(dr + row) * B4 + dc + col] = buf[row * piece_w + col]; - } + neighbours.record_uv_mode_mi(gr, gc, 2, 2, DC_PRED); + let round_up_even = |n: usize| n.div_ceil(2) * 2; + let bound_h = round_up_even(neighbours.mi_rows); + let bound_w = round_up_even(neighbours.mi_cols); + let u_state = neighbour_state(&u_grid); + let v_state = neighbour_state(&v_grid); + for cell in 0..2usize { + if cell < 2usize.min(bound_h.saturating_sub(gr)) { + neighbours.left[gr + cell][1] = u_state; + neighbours.left[gr + cell][2] = v_state; + } + if cell < 2usize.min(bound_w.saturating_sub(gc)) { + neighbours.above[gc + cell][1] = u_state; + neighbours.above[gc + cell][2] = v_state; } } } - let (u_grid, v_grid) = if last_skip { - u.reconstruct_mc(cpx, cpy, B4, &pred_u, &vec![0i32; B4 * B4]); - v.reconstruct_mc(cpx, cpy, B4, &pred_v, &vec![0i32; B4 * B4]); - (vec![0i32; B4 * B4], vec![0i32; B4 * B4]) - } else { - let around = neighbours.around_mi(group_mi, 8); - let ug = read_inter_plane( - dec, cdfs, TxbSet::Chroma4, scan4, 1, around[1], 0, u, cpx, cpy, B4, base_q_idx, - &pred_u, Some(first_tx_type), None, - )? - .0; - let vg = read_inter_plane( - dec, cdfs, TxbSet::Chroma4, scan4, 2, around[2], 0, v, cpx, cpy, B4, base_q_idx, - &pred_v, Some(first_tx_type), None, - )? - .0; - (ug, vg) - }; - neighbours.record_uv_mode_mi(gr, gc, 2, 2, DC_PRED); // `update_partition_context` writes `partition_context_lookup[subsize]` // over the whole 8x8 span -- BLOCK_4X8's width above and height left, the // same values [`decode_leaf_rect8`] writes for the intra shape. @@ -23437,21 +24012,6 @@ fn decode_inter_sub8_rect2( neighbours.above_side_mi[gc + cell] = bw; neighbours.left_side_mi[gr + cell] = bh; } - let round_up_even = |n: usize| n.div_ceil(2) * 2; - let bound_h = round_up_even(neighbours.mi_rows); - let bound_w = round_up_even(neighbours.mi_cols); - let u_state = neighbour_state(&u_grid); - let v_state = neighbour_state(&v_grid); - for cell in 0..2usize { - if cell < 2usize.min(bound_h.saturating_sub(gr)) { - neighbours.left[gr + cell][1] = u_state; - neighbours.left[gr + cell][2] = v_state; - } - if cell < 2usize.min(bound_w.saturating_sub(gc)) { - neighbours.above[gc + cell][1] = u_state; - neighbours.above[gc + cell][2] = v_state; - } - } Ok(()) } diff --git a/crates/ec-av1/src/stream.rs b/crates/ec-av1/src/stream.rs index 33115438..b778517c 100644 --- a/crates/ec-av1/src/stream.rs +++ b/crates/ec-av1/src/stream.rs @@ -86,6 +86,12 @@ pub fn intra16x4_in_inter_hits() -> (usize, usize, usize) { crate::decode::intra16x4_in_inter_hits() } +/// lane-sub8intra: INTRA 8x4/4x8 leaves inside an inter frame's sub-8x8 +/// HORZ/VERT partition, `(8x4, 4x8, chroma-reference, mixed groups)`. +pub fn sub8_intra_rect_hits() -> (usize, usize, usize, usize) { + crate::decode::sub8_intra_rect_hits() +} + /// lane-r14 r3: RECTANGULAR var-tx leaf transform units, `[32x16, 16x32]` -- /// the leaves `sub_tx_size_map` produces when a 1:4 strip with a 64-px axis /// splits its transform (`[TX_64X16] == TX_32X16`, `[TX_16X64] == TX_16X32`). diff --git a/lanes/sub8intra-r1.handoff.md b/lanes/sub8intra-r1.handoff.md new file mode 100644 index 00000000..6e89b1d4 --- /dev/null +++ b/lanes/sub8intra-r1.handoff.md @@ -0,0 +1,108 @@ +# lane-sub8intra r1 HANDOFF (tip = this commit) + +Env switch: **`EC_SUB8INTRA_DECODE=1`** enables the decode path; `EC_SUB8INTRA=1` prints the +measurement line at the refusal sites. WITHOUT the switch both refusals are unchanged +(verified: hg-0 and the 1920x792 @900 cut both still stop at +"an intra 8x4/4x8 block inside an inter frame's sub-8x8 HORZ/VERT partition"). +No refusal lifted, `refusal_inventory.rs` / `gate_coverage.rs` untouched, no gate added. + +## Step 1 measurement (`EC_SUB8INTRA=1 EC_RECT64_SPLIT=1 EC_INTRA16X4_DECODE=1`, decode_probe) + +| probe | shape | sub-block | is chroma ref | skip | tx_select | +|---|---|---|---|---|---| +| 10-bit 3840x1608 cut 0 | 4x8 | 0 | false | 0 | 1 | +| 10-bit 3840x1608 cut 300 | 8x4 | 0 | false | 0 | 1 | +| 10-bit 1920x792 @900 | 4x8 | 0 | false | 0 | 1 | +| 10-bit 1920x792 @5400 | 4x8 | 0 | false | 0 | 1 | +| 10-bit 1920x792 @6300 | 4x8 | 0 | false | 0 | 1 | +| 10-bit 1920x792 @8100 | -- (stops earlier on the clipped-16x4 arm) | + +5/6 fire first on sub-block 0 (NOT the chroma reference), non-skip, `--enable-tx-size-search` on. +The 4x4 SPLIT refusal is never the first wall, but becomes the wall on 3/6 probes once the +HORZ/VERT arm decodes, so both arms are implemented. + +## What is implemented (crates/ec-av1/src/decode.rs) + +* `decode_intra_sub8_leaf(.., (bw,bh), has_chroma, skip, ..)` -- one INTRA 8x4/4x8 **or 4x4** + leaf on the inter path. Reads `y_mode[0]` (`size_group_lookup[BLOCK_8X4]==0`), NO angle delta + (`av1_use_angle_delta` needs >= 8x8), `uv_mode` off `uv_mode_cfl[mode]` + CfL alphas only on + the chroma reference, `filter_intra` off the shape's own class row + (`filter_intra_size_class_rect` / `_class(4)`), then the tx-depth symbol + (`tx_size_cat0`, ctx from `tx_size_context_txfm_rect` -- the INTER frame's live + `above_txfm`/`left_txfm`, not the key frame's deblock approximation; `BLOCK_4X4` reads none, + `block_signals_txsize` is false). Luma = `decode_leaf_rect8`'s body (whole `TX_8X4`/`TX_4X8` + via `read_coeffs_rect` + `LumaRect4x8[Set1]` + `SCAN_8X4`/`SCAN_4X8`, or two `TX_4X4` units at + depth 1; square `read_plane` for 4x4). Chroma, when this leaf IS the chroma reference: the + group's one 4x4 unit intra-predicted at the 8x8 group origin with `group_reach = Reach::of(8,..)`, + CfL AC from the group's 8x8 luma -- `decode_leaf_rect8`'s post-loop chroma verbatim. + Publishes: mode into `record_mode_mi` + the coarse `above_mode`/`left_mode`, skip grid, + lf grid at tx dims, `set_txfm_ctxs` equivalent (`above_txfm = tx_w`, `left_txfm = tx_h`), + `record_inter_rect_mi(.., is_inter=false, ref 0, filter [3,3])`, and `MiInfo { is_inter:false, + ref_frame:-1 }` over every mi cell. +* `decode_inter_sub8_rect2` / `decode_inter_sub8_split4`: the `!is_inter` arm calls it (behind + the env switch), sets `intra_chroma` when the leaf was the chroma reference, and the whole + chroma tail (prediction, residual, `record_uv_mode_mi`, the u/v neighbour-state loop) is + guarded by `if !intra_chroma`. +* MIXED groups (chroma reference INTER, a sibling INTRA): `is_sub8x8_inter` + (reconinter_template.inc:54) returns false as soon as any mi of the group is intra, so the + chroma is predicted WHOLE 4x4 from the chroma-reference block's own mv at the group's chroma + origin instead of in 2x2/4x2 pieces -- implemented in both functions (`mixed` flag). +* tx_type rule for that chroma: `first_tx_type` now comes from the chroma-reference block's own + first TU when sub-block 0 is intra (`xd->tx_type_map[0]` of the current mi). NOTE the + pre-existing all-inter rule ("FIRST sub-block") is untouched but looks wrong against + blockd.h:1288 -- `xd->tx_type_map` is rebased to the CURRENT (chroma-reference) mi, so the + co-located luma (0,0) is the LAST sub-block's, not the first's. Flagged, not changed. +* Counter `sub8_intra_rect_hits() -> (8x4, 4x8, chroma_ref, mixed)`, printed by decode_probe + (`sub8_intra_rect: ...`), exported through `stream.rs`. +* `EC_TRACE_MODE_STEP` prints `name=mode` and `name=tx_depth val/ctx` for the leaf, in the + oracle's EC_ISTEP format. + +## Pixel state under the bypass -- the gate is RED + +* Film: all six probes move past this wall (hg-0 7 leaves, hg-300 10, @900 21 (+2 mixed), + @5400 28 (+2 mixed), @6300 2) and stop at the NEXT wall (4x4 split intra on 3 of them, + the 16x4 strip arms on the others). No film pixel comparison is possible: every firing frame + also carries the unproven intra-16x4 strip. +* aomenc witness sweep `~/.cache/sub8intra-tmp/sweep.sh` and `sweep2.sh` (recipe: 128x128 / + 192x128 gray lavfi sources, cq 48..63, `--min-partition-size=4 --enable-1to4-partitions=0 + --max-partition-size=8|16 --enable-filter-intra=0 --sb-size=64 --enable-tx-size-search=1`), + logs `sweep1.log` / `sweep2.log`: the counters fire on every arm (up to 293 8x4 leaves) but + **only 1 of 24 arms decodes all 6 frames, and that one MISMATCHES at byte 24735**. Eleven + arms encoded with `--max-partition-size=8` -- where a 16x4 block CANNOT exist -- still report + "an intra 16x4/4x16 strip inside an inter 16x16-level 1:4 partition", i.e. the refusal is + hallucinated by our own desync (class `refusal-from-own-desync`). + +## Where the desync is (bisected, do not redo this) + +Witness `~/.cache/sub8intra-tmp/x_1t_48_8_128x128.obu` (8-bit 128x128, cq48, max-partition 8; +1 intra 8x4 leaf then the bogus refusal). + +* EC_MODE ladder (ours vs instrumented aomdec, `r.l`/`o.l` in that dir): identical for 168 + entries; first divergence at `mi_row=24 mi_col=0` (ref rng=39238, ours rng=44268 and ours + reads a 16-wide block where the reference reads four 4x4s). +* The intra leaf itself is EXACT: it is at mi(23,12) (HORZ pair with the inter leaf at + mi(22,12), so the INTRA one is the chroma reference). Ours prints + `EC_ISTEP mi_row=23 mi_col=12 name=mode val=0 rng=38508` and + `name=tx_depth val=0 ctx=2 cat=0 rng=49194`; the oracle prints the identical tx_depth line + (ref_step.log:2463). So `y_mode[0]`, the missing angle delta, the uv/CfL reads and the + tx-depth ctx are all right. +* `tag=all_zero` ladder (`rz.l`/`oz.l`): the first 1405 luma/chroma transform units match + exactly; unit 1406 diverges (ref `all_zero=0 rng=40294`, ours `all_zero=0 rng=52548`). Both + decoders are still reading plane 0 there, so the divergence is in symbols read BETWEEN two + luma TUs -- i.e. inside the block following the intra leaf (the group at mi(22..23,14), the + 8x8 immediately to its right) or in that group's chroma. + +## Exact next step + +1. On that witness, ladder the group at mi(22,14)/(23,14) element by element (aomdec + `EC_ISTEP2 name=interp` / `EC_MODE`, ours `EC_MODE_MV`/`EC_MODE_VALR`) -- the intra leaf at + mi(23,12) is its LEFT neighbour, so the prime suspects are the left-side bands the intra + leaf publishes: `left_txfm` (tx dims vs block dims), `left_ref`/`left_inter` from + `record_inter_rect_mi(ref 0)` (INTRA_FRAME is 0 vs the `-1` the mi grid gets), the + coefficient state `left[..][0..2]`, and `left_side_mi` (the caller writes the group's + partition ctx AFTER the leaf, check the order). +2. Then re-check the chroma of the MIXED group at mi(22,12): its chroma is coded by the intra + leaf, so the inter sibling's own `piece` is dropped -- confirm against + `build_inter_predictors_sub8x8`'s `is_sub8x8_inter == false` path. +3. Only then re-run the sweep; a green arm needs `--max-partition-size=8` (no 16x4 shape can + exist there, so any 1:4 refusal is proof of a remaining desync).