forge
Explore
Log in

Don't you dare go Hollow

wip(sub8): decode PARTITION_SPLIT of 8x8 into four BLOCK_4X4 leaves (intra); HORZ/VERT and inter sub8x8 refused by name

Browse files at this commit

c7ed79babf27460ef713e1dc5eadb8a40453cfd7Ahmet Kaan GümüşParent: d21774e
crates/ec-av1/src/decode.rsmodified+302 -15
@@ -920,6 +920,21 @@ pub(crate) fn angle_delta_hits() -> usize {
920 pub(crate) fn tx_class1_hits() -> usize {920 pub(crate) fn tx_class1_hits() -> usize {
921 TX_CLASS1_HITS.with(|c| c.get())921 TX_CLASS1_HITS.with(|c| c.get())
922 }922 }
923+
924+// How many `PARTITION_SPLIT` decisions below an 8x8 block (four independent
925+// `BLOCK_4X4` leaves, spec `decode_partition`'s `bSize < BLOCK_8X8` recursion
926+// bottom) this thread's decode actually reconstructed -- the same
927+// before/after counter pattern as [`TX_CLASS1_HITS`], proving a stream
928+// genuinely exercised the sub-8x8 leaf path rather than every attempt
929+// coincidentally landing on `PARTITION_NONE`.
930+thread_local! {
931+ static SUB8_SPLIT_HITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
932+}
933+
934+/// Current value of [`SUB8_SPLIT_HITS`].
935+pub(crate) fn sub8_split_hits() -> usize {
936+ SUB8_SPLIT_HITS.with(|c| c.get())
937+}
923 const SUB_MI: u32 = 4;938 const SUB_MI: u32 = 4;
924 const MI: usize = 4;939 const MI: usize = 4;
925 const SB: usize = 64;940 const SB: usize = 64;
@@ -5444,6 +5459,242 @@ fn decode_leaf8(
5444 Ok(mode)5459 Ok(mode)
5445 }5460 }
5446 5461
5462+/// `read_intra_frame_mode_info`'s mode-info read for one `BLOCK_4X4` leaf
5463+/// below an 8x8 partition (spec `decode_partition`'s `bSize < BLOCK_8X8`
5464+/// bottom, where no partition symbol is read at all -- `decode_block` runs
5465+/// directly). No palette (`av1_allow_palette` needs both dims >= 8, spec
5466+/// blockd.h) and no `read_tx_size` (a sub-8x8 block's transform is always
5467+/// its own bsize, spec 5.11.16). `has_chroma` follows `is_chroma_reference`
5468+/// (spec/libaom `av1_common_int.h`): for 4:2:0, only the bottom-right leaf
5469+/// of the 2x2 4x4 group carries chroma syntax at all.
5470+#[allow(clippy::too_many_arguments)]
5471+fn read_intra_mode_sub8(
5472+ dec: &mut SymbolDecoder,
5473+ cdfs: &mut Cdfs,
5474+ above_mode: usize,
5475+ left_mode: usize,
5476+ has_chroma: bool,
5477+ enable_filter_intra: bool,
5478+ skip_ctx: usize,
5479+ allow_intrabc: bool,
5480+ mi_r: usize,
5481+ mi_c: usize,
5482+) -> Result<(bool, usize, i32, Option<(usize, i32, Option<(i32, i32)>)>, Option<usize>)> {
5483+ let trace = std::env::var_os("EC_AV1_TRACE").is_some();
5484+ let skip = dec.symbol(&mut cdfs.skip[skip_ctx]) != 0;
5485+ if trace {
5486+ eprintln!("TRACE sub8 skip mi=({mi_r},{mi_c}) value={}", skip as i32);
5487+ }
5488+ maybe_read_cdef_idx(dec, mi_r, mi_c, skip);
5489+ maybe_read_delta_q(dec, cdfs, mi_r, mi_c, false, skip);
5490+ maybe_read_delta_lf(dec, cdfs, mi_r, mi_c, false, skip);
5491+ if allow_intrabc {
5492+ let use_intrabc = dec.symbol(&mut cdfs.intrabc) != 0;
5493+ if use_intrabc {
5494+ return Err(unsupported(
5495+ "a block that actually uses intrabc (this decoder never reconstructs one)",
5496+ ));
5497+ }
5498+ }
5499+ let above_ctx = INTRA_MODE_CTX[above_mode];
5500+ let left_ctx = INTRA_MODE_CTX[left_mode];
5501+ let mode = dec.symbol(&mut cdfs.kf_y_mode[above_ctx][left_ctx]);
5502+ if trace {
5503+ eprintln!("TRACE sub8 y_mode mi=({mi_r},{mi_c}) ctx=({above_ctx},{left_ctx}) value={mode}");
5504+ }
5505+ let angle_delta_y = if (V_PRED..=D67_PRED).contains(&mode) {
5506+ read_angle_delta(dec, &mut cdfs.angle_delta[mode - V_PRED])
5507+ } else {
5508+ 0
5509+ };
5510+ let chroma = if has_chroma {
5511+ let uv_mode = dec.symbol(&mut cdfs.uv_mode_cfl[mode]);
5512+ if trace {
5513+ eprintln!("TRACE sub8 uv_mode value={uv_mode}");
5514+ }
5515+ if (9..=12).contains(&uv_mode) {
5516+ SMOOTH_UV_HITS.with(|c| c.set(c.get() + 1));
5517+ }
5518+ let alpha = if uv_mode == UV_CFL_PRED {
5519+ Some(read_cfl_alphas(dec, cdfs))
5520+ } else {
5521+ None
5522+ };
5523+ let angle_delta_uv = if (V_PRED..=D67_PRED).contains(&uv_mode) {
5524+ DIRECTIONAL_UV_HITS.with(|c| c.set(c.get() + 1));
5525+ read_angle_delta(dec, &mut cdfs.angle_delta[uv_mode - V_PRED])
5526+ } else {
5527+ 0
5528+ };
5529+ Some((uv_mode, angle_delta_uv, alpha))
5530+ } else {
5531+ None
5532+ };
5533+ let mut filter_intra = None;
5534+ if mode == DC_PRED && enable_filter_intra {
5535+ // `BLOCK_4X4`'s own row (class 0, `filter_intra_size_class(4)`) --
5536+ // every sub-8x8 leaf here is a `BLOCK_4X4`.
5537+ let use_filter_intra = dec.symbol(&mut cdfs.filter_intra[0]) != 0;
5538+ if trace {
5539+ eprintln!("TRACE sub8 use_filter_intra value={}", use_filter_intra as i32);
5540+ }
5541+ if use_filter_intra {
5542+ FILTER_INTRA_HITS.with(|c| c.set(c.get() + 1));
5543+ let fi_mode = dec.symbol(&mut cdfs.filter_intra_mode);
5544+ filter_intra = Some(fi_mode);
5545+ }
5546+ }
5547+ Ok((skip, mode, angle_delta_y, chroma, filter_intra))
5548+}
5549+
5550+/// Decodes a `PARTITION_SPLIT` of one 8x8 block into four `BLOCK_4X4` leaves
5551+/// (spec `decode_partition`'s recursion bottom), sharing [`decode_leaf8`]'s
5552+/// own signature so its caller's `prev_leaf`/`above_mode`/`left_mode`
5553+/// bookkeeping needs no change. Each leaf reads its own luma mode info,
5554+/// neighboured off the *previously decoded leaf in this same group* on
5555+/// whichever axis is adjacent (mirroring [`decode_leaf8`]'s own `prev_leaf`
5556+/// pattern, one level deeper); chroma is read and reconstructed exactly
5557+/// once, on the last (bottom-right) leaf, at the whole 8x8 group's own 4x4
5558+/// chroma unit -- [`read_intra_mode_sub8`]'s doc has the `is_chroma_reference`
5559+/// derivation.
5560+#[allow(clippy::too_many_arguments)]
5561+fn decode_leaf_split4(
5562+ dec: &mut SymbolDecoder,
5563+ cdfs: &mut Cdfs,
5564+ neighbours: &mut Neighbours,
5565+ outer_at: (usize, usize),
5566+ leaf_mi: (usize, usize),
5567+ scan4: &[u16],
5568+ prev_leaf: Option<((usize, usize), usize)>,
5569+ y: &mut PlaneBuf,
5570+ u: &mut PlaneBuf,
5571+ v: &mut PlaneBuf,
5572+ base_q_idx: u8,
5573+ enable_filter_intra: bool,
5574+ allow_intrabc: bool,
5575+ reduced_tx_set: bool,
5576+) -> Result<usize> {
5577+ SUB8_SPLIT_HITS.with(|c| c.set(c.get() + 1));
5578+ let (r, c) = outer_at;
5579+ let mut above_mode = neighbours.above_mode[c];
5580+ let mut left_mode = neighbours.left_mode[r];
5581+ if let Some(((pr, pc), pmode)) = prev_leaf {
5582+ if pc == leaf_mi.1 && leaf_mi.0 == pr + 2 {
5583+ above_mode = pmode;
5584+ } else if pr == leaf_mi.0 && leaf_mi.1 == pc + 2 {
5585+ left_mode = pmode;
5586+ }
5587+ }
5588+ let mut leaf_modes = [0usize; 4];
5589+ let mut leaf_skips = [false; 4];
5590+ let mut last_uv: Option<(usize, i32, Option<(i32, i32)>)> = None;
5591+ for i in 0..4usize {
5592+ let (dr, dc) = (i / 2, i % 2);
5593+ let lmi = (leaf_mi.0 + dr, leaf_mi.1 + dc);
5594+ let leaf_above = if dr == 0 { above_mode } else { leaf_modes[i - 2] };
5595+ let leaf_left = if dc == 0 { left_mode } else { leaf_modes[i - 1] };
5596+ let has_chroma = i == 3;
5597+ let skip_ctx = neighbours.skip_txfm_ctx(lmi.0, lmi.1);
5598+ let (skip, mode, angle_delta_y, chroma, filter_intra) = read_intra_mode_sub8(
5599+ dec, cdfs, leaf_above, leaf_left, has_chroma, enable_filter_intra, skip_ctx,
5600+ allow_intrabc, lmi.0, lmi.1,
5601+ )?;
5602+ leaf_modes[i] = mode;
5603+ leaf_skips[i] = skip;
5604+ let smooth_neighbor = is_smooth_mode(leaf_above) || is_smooth_mode(leaf_left);
5605+ if smooth_neighbor {
5606+ SMOOTH_LUMA_HITS.with(|c| c.set(c.get() + 1));
5607+ }
5608+ let (px, py) = (lmi.1 * MI, lmi.0 * MI);
5609+ let reach = Reach::of(4, px, py, y.width, y.height);
5610+ if skip {
5611+ y.reconstruct(px, py, 4, mode, angle_delta_y, reach, &vec![0i32; 16], None, filter_intra, smooth_neighbor);
5612+ neighbours.record_mi_luma(lmi, 4, &vec![0i32; 16]);
5613+ } else {
5614+ let tu_around = neighbours.around_mi(lmi, 4)[0];
5615+ let tu_skip_ctx = neighbours.luma_skip_ctx(lmi, 1);
5616+ let tu_grid = read_plane(
5617+ dec,
5618+ cdfs,
5619+ if reduced_tx_set { TxbSet::Luma4 } else { TxbSet::Luma4Set1 },
5620+ scan4,
5621+ 0,
5622+ tu_around,
5623+ mode,
5624+ mode,
5625+ angle_delta_y,
5626+ reach,
5627+ y,
5628+ px,
5629+ py,
5630+ 4,
5631+ 4,
5632+ base_q_idx,
5633+ None,
5634+ filter_intra,
5635+ Some(tu_skip_ctx),
5636+ smooth_neighbor,
5637+ )?;
5638+ neighbours.record_mi_luma(lmi, 4, &tu_grid);
5639+ }
5640+ neighbours.fill_skip_grid(lmi, 1, skip);
5641+ neighbours.fill_lf_grid(lmi, 1, 4, 0);
5642+ if has_chroma {
5643+ last_uv = chroma;
5644+ }
5645+ }
5646+ let (gpx, gpy) = (leaf_mi.1 * MI, leaf_mi.0 * MI);
5647+ let (cpx, cpy) = (gpx / 2, gpy / 2);
5648+ let group_reach = Reach::of(8, gpx, gpy, y.width, y.height);
5649+ let (uv_mode, angle_delta_uv, alpha) =
5650+ last_uv.expect("i==3 always sets has_chroma, so chroma is always Some");
5651+ let uv_predict_mode = if uv_mode == UV_CFL_PRED { DC_PRED } else { uv_mode };
5652+ let (u_grid, v_grid): (Vec<i32>, Vec<i32>) = if leaf_skips[3] {
5653+ u.reconstruct(cpx, cpy, 4, uv_predict_mode, angle_delta_uv, group_reach, &vec![0i32; 16], None, None, false);
5654+ v.reconstruct(cpx, cpy, 4, uv_predict_mode, angle_delta_uv, group_reach, &vec![0i32; 16], None, None, false);
5655+ (vec![0i32; 16], vec![0i32; 16])
5656+ } else {
5657+ let ac = alpha.map(|_| cfl_ac_q3(y, gpx, gpy, 8));
5658+ let chroma_around = neighbours.around_mi(leaf_mi, 8);
5659+ let ug = read_plane(
5660+ dec, cdfs, TxbSet::Chroma4, scan4, 1, chroma_around[1], uv_mode, uv_predict_mode,
5661+ angle_delta_uv, group_reach, u, cpx, cpy, 4, TX4, base_q_idx,
5662+ alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), None, None, false,
5663+ )?;
5664+ let vg = read_plane(
5665+ dec, cdfs, TxbSet::Chroma4, scan4, 2, chroma_around[2], uv_mode, uv_predict_mode,
5666+ angle_delta_uv, group_reach, v, cpx, cpy, 4, TX4, base_q_idx,
5667+ alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), None, None, false,
5668+ )?;
5669+ (ug, vg)
5670+ };
5671+ // Chroma neighbour bookkeeping for the whole 8x8 group -- mirrors
5672+ // [`decode_leaf8`]'s own tx4-chroma section (`record_split_luma`'s
5673+ // rescaling is wrong for an already-mi-unit position, so this inlines
5674+ // its tail directly at `leaf_mi`, side_mi=2).
5675+ let side_mi = 2;
5676+ for cell in 0..side_mi {
5677+ neighbours.left_side_mi[leaf_mi.0 + cell] = 8;
5678+ neighbours.above_side_mi[leaf_mi.1 + cell] = 8;
5679+ }
5680+ let round_up_even = |n: usize| n.div_ceil(2) * 2;
5681+ let bound_h = round_up_even(neighbours.mi_rows);
5682+ let bound_w = round_up_even(neighbours.mi_cols);
5683+ let u_state = neighbour_state(&u_grid);
5684+ let v_state = neighbour_state(&v_grid);
5685+ for cell in 0..side_mi {
5686+ if cell < side_mi.min(bound_h.saturating_sub(leaf_mi.0)) {
5687+ neighbours.left[leaf_mi.0 + cell][1] = u_state;
5688+ neighbours.left[leaf_mi.0 + cell][2] = v_state;
5689+ }
5690+ if cell < side_mi.min(bound_w.saturating_sub(leaf_mi.1)) {
5691+ neighbours.above[leaf_mi.1 + cell][1] = u_state;
5692+ neighbours.above[leaf_mi.1 + cell][2] = v_state;
5693+ }
5694+ }
5695+ Ok(leaf_modes[3])
5696+}
5697+
5447 /// Spec 7.15.3's `Cdef_Directions`: `(row, col)` offsets for the two primary5698 /// Spec 7.15.3's `Cdef_Directions`: `(row, col)` offsets for the two primary
5448 /// taps of each of the eight directions; the secondary taps at `dir+2`/`dir-2`5699 /// taps of each of the eight directions; the secondary taps at `dir+2`/`dir-2`
5449 /// (mod 8) reuse the same table.5700 /// (mod 8) reuse the same table.
@@ -7102,12 +7353,8 @@ pub(crate) fn decode_key_frame_tile_with_cdfs(
7102 "TRACE partition_w8 mi=({mr},{mc}) ctx={leaf_ctx} value={part8}"7353 "TRACE partition_w8 mi=({mr},{mc}) ctx={leaf_ctx} value={part8}"
7103 );7354 );
7104 }7355 }
7105- if part8 != PARTITION_NONE {7356+ let leaf_mode = if part8 == PARTITION_NONE {
7106- return Err(unsupported(7357+ decode_leaf8(
7107- "a partition below 8x8 (this decoder codes no leaf smaller than 8x8)",
7108- ));
7109- }
7110- let leaf_mode = decode_leaf8(
7111 &mut dec,7358 &mut dec,
7112 &mut cdfs,7359 &mut cdfs,
7113 &mut neighbours,7360 &mut neighbours,
@@ -7124,7 +7371,29 @@ pub(crate) fn decode_key_frame_tile_with_cdfs(
7124 allow_intrabc,7371 allow_intrabc,
7125 tx_select,7372 tx_select,
7126 reduced_tx_set,7373 reduced_tx_set,
7127- )?;7374+ )?
7375+ } else if part8 == PARTITION_SPLIT {
7376+ decode_leaf_split4(
7377+ &mut dec,
7378+ &mut cdfs,
7379+ &mut neighbours,
7380+ at16,
7381+ leaf_mi,
7382+ &scan4,
7383+ prev_leaf,
7384+ &mut y,
7385+ &mut u,
7386+ &mut v,
7387+ base_q_idx,
7388+ enable_filter_intra,
7389+ allow_intrabc,
7390+ reduced_tx_set,
7391+ )?
7392+ } else {
7393+ return Err(unsupported(
7394+ "a HORZ/VERT partition below 8x8 (this decoder's transform primitive is square-only; 4x8/8x4 need a real rectangular transform)",
7395+ ));
7396+ };
7128 prev_leaf = Some((leaf_mi, leaf_mode));7397 prev_leaf = Some((leaf_mi, leaf_mode));
7129 }7398 }
7130 if let Some((_, mode)) = prev_leaf {7399 if let Some((_, mode)) = prev_leaf {
@@ -7173,12 +7442,8 @@ pub(crate) fn decode_key_frame_tile_with_cdfs(
7173 "TRACE partition_w8 mi=({mr},{mc}) ctx={leaf_ctx} value={part8}"7442 "TRACE partition_w8 mi=({mr},{mc}) ctx={leaf_ctx} value={part8}"
7174 );7443 );
7175 }7444 }
7176- if part8 != PARTITION_NONE {7445+ let leaf_mode = if part8 == PARTITION_NONE {
7177- return Err(unsupported(7446+ decode_leaf8(
7178- "a partition below 8x8 (this decoder codes no leaf smaller than 8x8)",
7179- ));
7180- }
7181- let leaf_mode = decode_leaf8(
7182 &mut dec,7447 &mut dec,
7183 &mut cdfs,7448 &mut cdfs,
7184 &mut neighbours,7449 &mut neighbours,
@@ -7195,7 +7460,29 @@ pub(crate) fn decode_key_frame_tile_with_cdfs(
7195 allow_intrabc,7460 allow_intrabc,
7196 tx_select,7461 tx_select,
7197 reduced_tx_set,7462 reduced_tx_set,
7198- )?;7463+ )?
7464+ } else if part8 == PARTITION_SPLIT {
7465+ decode_leaf_split4(
7466+ &mut dec,
7467+ &mut cdfs,
7468+ &mut neighbours,
7469+ at16,
7470+ leaf_mi,
7471+ &scan4,
7472+ prev_leaf,
7473+ &mut y,
7474+ &mut u,
7475+ &mut v,
7476+ base_q_idx,
7477+ enable_filter_intra,
7478+ allow_intrabc,
7479+ reduced_tx_set,
7480+ )?
7481+ } else {
7482+ return Err(unsupported(
7483+ "a HORZ/VERT partition below 8x8 (this decoder's transform primitive is square-only; 4x8/8x4 need a real rectangular transform)",
7484+ ));
7485+ };
7199 prev_leaf = Some((leaf_mi, leaf_mode));7486 prev_leaf = Some((leaf_mi, leaf_mode));
7200 }7487 }
7201 // `record()`'s `above_mode`/`left_mode`7488 // `record()`'s `above_mode`/`left_mode`
@@ -12309,7 +12596,7 @@ pub(crate) fn decode_inter_frame_tile_with_cdfs(
12309 let part8 = dec.symbol(&mut cdfs.partition_w8[leaf_ctx]);12596 let part8 = dec.symbol(&mut cdfs.partition_w8[leaf_ctx]);
12310 if part8 != PARTITION_NONE {12597 if part8 != PARTITION_NONE {
12311 return Err(unsupported(12598 return Err(unsupported(
12312- "a partition below 8x8 (this decoder codes no leaf smaller than 8x8)",12599+ "an inter partition below 8x8 (this decoder codes no inter leaf smaller than 8x8; lane-sub8 scoped to intra)",
12313 ));12600 ));
12314 }12601 }
12315 let (skip, is_inter, skip_mode_leaf, compound_ctx8) =12602 let (skip, is_inter, skip_mode_leaf, compound_ctx8) =
crates/ec-av1/src/refusal_inventory.rsmodified+3 -1
@@ -28,7 +28,7 @@ const CAPABILITY_CLAIMS: &[&str] = &[
28 "a non-2D tx class on a rectangular transform (never expected at this size)",28 "a non-2D tx class on a rectangular transform (never expected at this size)",
29 "a non-DC chroma mode on an 8x8 inter-frame leaf (this encoder never writes one)",29 "a non-DC chroma mode on an 8x8 inter-frame leaf (this encoder never writes one)",
30 "a nonzero angle delta (this encoder never writes one)",30 "a nonzero angle delta (this encoder never writes one)",
31- "a partition below 8x8 (this decoder codes no leaf smaller than 8x8)",31+ "a partition below 16x16 other than a clean split (this decoder codes only the square arms below 16x16)",
32 "a tx_type symbol on a rectangular transform (never expected at this size)",32 "a tx_type symbol on a rectangular transform (never expected at this size)",
33 ];33 ];
34 34
@@ -38,6 +38,8 @@ const REFUSALS: &[&str] = &[
38 "a coded (non-skip) HORZ_B/VERT_B rect strip below 16x16 (this decoder ports only the skip case at this size)",38 "a coded (non-skip) HORZ_B/VERT_B rect strip below 16x16 (this decoder ports only the skip case at this size)",
39 "a coded (non-skip) HORZ/VERT rect strip below 16x16 (this decoder ports only the skip case at this size)",39 "a coded (non-skip) HORZ/VERT rect strip below 16x16 (this decoder ports only the skip case at this size)",
40 "a HORZ_A/HORZ_B/VERT_A partition below 16x16 (this decoder codes only the square arms, HORZ, VERT, VERT_B, and a clean split below 16x16)",40 "a HORZ_A/HORZ_B/VERT_A partition below 16x16 (this decoder codes only the square arms, HORZ, VERT, VERT_B, and a clean split below 16x16)",
41+ "a HORZ/VERT partition below 8x8 (this decoder's transform primitive is square-only; 4x8/8x4 need a real rectangular transform)",
42+ "an inter partition below 8x8 (this decoder codes no inter leaf smaller than 8x8; lane-sub8 scoped to intra)",
41 "a 16x16 block whose true edge cuts through both axes needs a rectangular transform this decoder does not code yet",43 "a 16x16 block whose true edge cuts through both axes needs a rectangular transform this decoder does not code yet",
42 "a 16x16 inter block whose true edge cuts through both axes needs a rectangular transform this decoder does not code yet",44 "a 16x16 inter block whose true edge cuts through both axes needs a rectangular transform this decoder does not code yet",
43 "a 32x32 partition type this decoder does not code (value={part32})",45 "a 32x32 partition type this decoder does not code (value={part32})",