diff --git a/crates/ec-av1/src/cdf_state.rs b/crates/ec-av1/src/cdf_state.rs index a6081b24..2ce94401 100644 --- a/crates/ec-av1/src/cdf_state.rs +++ b/crates/ec-av1/src/cdf_state.rs @@ -278,10 +278,11 @@ pub(crate) struct TxbTables<'a> { pub txb_skip: &'a mut [[u16; 3]], /// The end-of-block group, whose alphabet the transform size sets. pub eob_pt: &'a mut [u16], - /// `eob_pt`'s `TX_CLASS_HORIZ`/`TX_CLASS_VERT` sibling table (r5): read - /// instead of `eob_pt` once this TU's `tx_type` is known to be - /// `V_DCT`/`H_DCT`. `None` for any size/set whose `tx_type` alphabet - /// cannot produce those two symbols (chroma, and every inter set). + /// Read instead of `eob_pt` once this TU's `tx_type` is known to be + /// `V_DCT`/`H_DCT` (lane-intertx doc fix: every set here carries the + /// row -- the older "none for chroma and every inter set" note + /// described the r5 alphabet, before the wider inter/chroma-reachable + /// sets made those classes reachable there). pub eob_pt_class1: Option<&'a mut [u16]>, /// The top bit of the offset inside that group. pub eob_extra: &'a mut [[u16; 3]; 9], diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index 06c3e511..547ef621 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -18711,10 +18711,19 @@ fn read_inter_luma8( /// sync and only corrupted the inverse transform (one 64x64 superblock of /// high-frequency chroma noise, luma bit-exact). pub(crate) fn reduce_inherited_chroma_tx_type(t: TxType, w: usize, h: usize, fctx: &crate::decode::FrameCtx) -> TxType { + reduce_inherited_chroma_tx_type_flagged(t, w, h, fctx.reduced_tx_set_inter.with(std::cell::Cell::get)) +} + +/// [`reduce_inherited_chroma_tx_type`] against an explicit `reduced_tx_set` +/// bit (lane-intertx): the WRITER calls this with the frame's own header bit +/// ([`crate::cdf_state::Cdfs::reduced_tx_set`]) to derive the type an inter +/// block's chroma transform is coded with, the same derivation its reader +/// makes -- it has no `FrameCtx` in reach and must not silently read one +/// whose value can lag the frame being written (class `stale-context-bit`). +pub(crate) fn reduce_inherited_chroma_tx_type_flagged(t: TxType, w: usize, h: usize, reduced: bool) -> TxType { use TxType::*; let sqr_up = w.max(h); let sqr = w.min(h); - let reduced = fctx.reduced_tx_set_inter.with(std::cell::Cell::get); // `av1_get_ext_tx_set_type(tx_size, is_inter = 1, reduced)`. let allowed: &[TxType] = if sqr_up > 32 { // EXT_TX_SET_DCTONLY diff --git a/crates/ec-av1/src/encode.rs b/crates/ec-av1/src/encode.rs index 240a9e42..a79a890f 100644 --- a/crates/ec-av1/src/encode.rs +++ b/crates/ec-av1/src/encode.rs @@ -472,21 +472,24 @@ fn inter_tx_type_search() -> InterTxSearch { /// The inter luma [`TxbSet`] a `tx`-sided transform unit reads, `None` for the /// 64-point one (it codes no `tx_type` symbol). -/// lane-txw: the INTER half of the widening is still NOT applied here, and -/// the reason lane-txi recorded (the `refs` lane's DPB bookkeeping) was -/// WRONG -- the refusal is `refusal-from-own-desync` twice over. Half of it +/// lane-txw history, resolved by lane-intertx: the INTER half of the +/// widening was blocked by `refusal-from-own-desync` twice over. Half of it /// was this writer coding a `V_DCT`/`H_DCT` unit as `TX_CLASS_2D` -/// (`tile::write_coeffs`, fixed by this lane, which is what unblocked the -/// intra half). What remains is the CHROMA inheritance: an inter block's +/// (`tile::write_coeffs`, fixed by lane-txw, which unblocked the intra +/// half). The other half was the CHROMA inheritance: an inter block's /// chroma transform type is the luma type reduced to the chroma size's set /// (`decode::reduce_inherited_chroma_tx_type`), and (a) the encoder never -/// sets `FrameCtx::reduced_tx_set_inter`, so [`recode_inter_chroma`] reduces -/// against the REDUCED allowance while a `reduced_tx_set = 0` frame's -/// decoder allows the full one, and (b) `tile::write_block_planes` codes -/// every chroma plane as `DCT_DCT`, so an inherited 1-D type desyncs the -/// chroma exactly as luma did. Both are deferred with the wider inter search -/// itself; the intra half needs neither (an intra block's chroma type comes -/// off its own UV mode, never off luma). +/// set `FrameCtx::reduced_tx_set_inter`, so [`recode_inter_chroma`] reduced +/// against a STALE allowance, and (b) `tile::write_block_planes` coded +/// every chroma plane as `DCT_DCT`, so an inherited 1-D type desynced the +/// chroma exactly as luma did. lane-intertx fixed both -- the frame sets +/// its own bit on the context (`encode_inter_frame`), the writer derives +/// the chroma type from this frame's own header bit +/// (`reduce_inherited_chroma_tx_type_flagged`) -- and the widened SEARCH +/// names these sets through [`luma_set_for`] at its call sites, so the +/// wider candidates are reachable and honestly priced. The intra half +/// never needed either (an intra block's chroma type comes off its own UV +/// mode, never off luma). fn inter_luma_set(tx: usize) -> Option { Some(match tx { 4 => TxbSet::Luma4Inter, @@ -5109,7 +5112,15 @@ fn commit_inter_luma( let mut flat_typed: Option = None; let mut flat_type = TxType::DctDct; if !skip { - if let Some(fset) = inter_luma_set(side) { + // lane-intertx: on a `reduced_tx_set = 0` frame the search names the + // Set1 alphabet the writer codes into ([`luma_set_for`], the shape + // the intra search already takes), which is what makes the wider + // seven-type candidate list below reachable -- and prices every + // candidate, `IDTX` included, in the alphabet its symbol is really + // coded into. + if let Some(fset) = + inter_luma_set(side).map(|set| luma_set_for(set, search.screen)) + { let lambda = search.lambda * tx_type_lambda_mult(); let cost = |t: &Trial| t.sse + lambda * t.bits; for &tx_type in inter_tx_type_candidates(fset, search.screen) { @@ -5200,14 +5211,17 @@ fn commit_inter_luma( return (flat_ref.levels.clone(), 0, flat_gain, vec![flat_type]); } let tx = side / 2; - let set = if tx >= 32 { - // lane-b64b: the 64x64 root's halved transform is TX_32X32. - TxbSet::Luma32Inter - } else if tx >= 16 { - TxbSet::Luma16Inter - } else { - TxbSet::Luma8Inter - }; + let set = luma_set_for( + if tx >= 32 { + // lane-b64b: the 64x64 root's halved transform is TX_32X32. + TxbSet::Luma32Inter + } else if tx >= 16 { + TxbSet::Luma16Inter + } else { + TxbSet::Luma8Inter + }, + search.screen, + ); let mut levels = vec![0i32; side * side]; let (mut sse, mut bits) = (0.0, 0.0); let mut units = Vec::with_capacity(4); @@ -12594,6 +12608,14 @@ pub(crate) fn encode_inter_frame( // lane-txi, as on the key frame: the header bit the writer's own // `Cdfs` then reads for every luma `tx_type` symbol. header.reduced_tx_set = !wide_tx_set(screen); + // lane-intertx, desync (a): the frame context this value is read back + // through ( [`decode::reduce_inherited_chroma_tx_type`] inside + // `recode_inter_chroma`, and every inter decode helper the search's own + // trial decode shares) must carry THIS frame's bit, not whatever the + // previous frame's trial decode last left in the shared cell -- a stale + // `true` here narrows the chroma allowance the search reconstructs + // against under the one the header's bit actually allows. + fctx.reduced_tx_set_inter.set(header.reduced_tx_set); // The search prices coefficients against the tables this frame's writer // starts from, not against the defaults (lane-av1txbits) -- but only on a // frame the screen detector said no to (lane-av1price2): armed HERE, after diff --git a/crates/ec-av1/src/encoder.rs b/crates/ec-av1/src/encoder.rs index 9e2644f1..57da7296 100644 --- a/crates/ec-av1/src/encoder.rs +++ b/crates/ec-av1/src/encoder.rs @@ -4993,6 +4993,107 @@ mod tests { eprintln!("SKIP the ffmpeg half: no ffmpeg"); } } + /// lane-intertx's witness: on a clip the screen detector calls + /// NON-screen, the inter luma search offers the wider + /// `reduced_tx_set = 0` alphabets and CODES a 1-D `V_DCT`/`H_DCT` unit + /// (a `coded > 0` guard on the committed-winners census, class + /// `gate-blind-to-feature`), the block's chroma transforms inherit that + /// type reduced to the chroma size's set against the frame's own honest + /// header bit, and BOTH decoders reconstruct every frame sample-exact on + /// ALL THREE planes -- chroma is the plane the inheritance desync + /// corrupts, so luma-only comparisons cannot see it. + #[test] + #[ignore = "sets process-global search levers: run it alone"] + fn a_non_screen_inter_clip_codes_a_one_d_type_both_decoders_read_every_plane_exactly() { + let _knobs = crate::speed::knob_write(); + let _gate_lock = crate::stream::tests::lock_gate_counters(); + crate::encode::set_intra_tx_search(Some(true)); + crate::encode::set_inter_tx_search(Some(true)); + crate::encode::set_wide_tx_set(Some(true)); + crate::encode::force_screen(Some(false)); + let (width, height) = (320usize, 192usize); + // The wide witness's moving card: vertical steps (where a 1-D + // `V_DCT`/`H_DCT` beats the 2-D DCT on the motion-compensated + // residual), ramps (`ADST`) and moving plateaus (`IDTX`), translated + // a few samples per frame so the inter search sees real motion. + let sources: Vec = (0..4) + .map(|t| { + let mut p = test_card(width, height, t * 3); + for y in 0..height { + for x in 0..width { + let v = match ((x + t * 5) / 16 % 2, (y + t * 3) / 16 % 2) { + (0, 0) => 16u16, + (1, 0) => 235, + (0, 1) => ((x + t) % 64 * 3) as u16, + _ => ((y + t) % 64 * 3) as u16, + }; + p.y[y * width + x] = v; + } + } + p + }) + .collect(); + let config = EncoderConfig { + width, + height, + base_q_idx: 90, + gop: 4, + colour: Colour::Bt709Limited, + tile_cols_log2: 0, + tile_rows_log2: 0, + }; + let mut enc = Av1Encoder::new(config).unwrap(); + let _ = crate::encode::take_inter_tx_type_hits(); + let mut stream = Vec::new(); + for packet in encode_all(&mut enc, &sources) { + stream.extend_from_slice(&packet.data); + } + let hits = crate::encode::take_inter_tx_type_hits(); + crate::encode::force_screen(None); + crate::encode::set_wide_tx_set(None); + crate::encode::set_inter_tx_search(None); + crate::encode::set_intra_tx_search(None); + use crate::transform::TxType; + eprintln!( + "inter wide-set witness: inter hits {hits:?}, {} bytes", + stream.len() + ); + assert!( + hits[TxType::Idtx as usize] > 0, + "no inter luma unit was CODED IDTX: {hits:?}" + ); + assert!( + hits[TxType::VDct as usize] + hits[TxType::HDct as usize] > 0, + "no inter luma unit was CODED a 1-D type -- the wider search \ + never fired on non-screen content (class gate-blind-to-feature): {hits:?}" + ); + let ours = crate::stream::decode_stream(&stream).expect("our decoder"); + assert_eq!(ours.len(), sources.len(), "our decoder's frames"); + if have_ffmpeg() { + let theirs = ffmpeg_decode_planes(&stream, width, height); + assert_eq!(theirs.len(), sources.len(), "ffmpeg's frames"); + let (cw, ch) = (width / 2, height / 2); + for (i, (a, (y, u, v))) in ours.iter().zip(&theirs).enumerate() { + for (name, got16, want, w) in + [("y", &a.y, y, width), ("u", &a.u, u, cw), ("v", &a.v, v, ch)] + { + let got: Vec = got16.iter().map(|&s| s as u8).collect(); + assert_eq!(got.len(), want.len(), "frame {i} {name} plane size"); + if let Some(at) = got.iter().zip(want.iter()).position(|(x, y)| x != y) { + panic!( + "frame {i}: {name} differs first at ({}, {}): ours {} vs ffmpeg {}", + at % w, + at / w, + got[at], + want[at], + ); + } + } + } + } else { + eprintln!("SKIP the ffmpeg half: no ffmpeg"); + } + } /// Every SHIPPED SPEED PRESET codes a stream both decoders reconstruct /// sample-exact -- ours and ffmpeg's -- and codes a DIFFERENT stream from diff --git a/crates/ec-av1/src/tile.rs b/crates/ec-av1/src/tile.rs index 2a357973..8e41d1a8 100644 --- a/crates/ec-av1/src/tile.rs +++ b/crates/ec-av1/src/tile.rs @@ -4827,6 +4827,7 @@ fn write_block( tx_mode, tx_select, block_tx_type(block), + false, ); neighbours.record_planes(at, side, mode, grids, !split); clear_skip_band(neighbours, at_mi, side); @@ -4927,6 +4928,7 @@ fn write_leaf8( tx_mode, tx_select, block_tx_type(block), + false, ); neighbours.record_mi_planes(leaf_mi, 8, grids, !split); clear_skip_band(neighbours, leaf_mi, 8); @@ -4951,7 +4953,20 @@ fn write_block_planes( // This block's single luma transform type (lane-txset); chroma's is // derived from its mode by the decoder and codes no symbol. luma_tx_type: TxType, + // lane-intertx: an INTER block's chroma transforms INHERIT the luma + // type (reduced to the chroma set against this frame's own + // `reduced_tx_set` bit); an intra block's comes off its UV mode and + // rides `DCT_DCT`'s 2D class exactly as before. + inter: bool, ) { + // lane-intertx, desync (b): the writer used to code every chroma plane + // `DCT_DCT`. Chroma codes no `tx_type` symbol, but the type names the + // transform class, and the class picks the scan, the `eob_pt` row and + // the context offsets (the lane-txw note in `write_coeffs`) -- so an + // inherited 1-D luma type desynced the chroma exactly as luma itself + // once did. The reduced bit read here is the frame's own header bit, + // the ONE the writer's `Cdfs` is armed with (`Cdfs::reduced_tx_set`). + let reduced = cdfs.reduced_tx_set; for (plane, (grid, scan)) in grids.iter().zip(scans.iter()).enumerate() { if plane == 0 && skip_luma { continue; @@ -4969,15 +4984,28 @@ fn write_block_planes( let before = enc.tell(); #[cfg(test)] let q_ctx = cdfs.q_ctx; + let mut coding = cdfs.txb(planes[plane], mode); + let tx_type = if plane == 0 { + luma_tx_type + } else if inter { + crate::decode::reduce_inherited_chroma_tx_type_flagged( + luma_tx_type, + coding.side.min(32), + coding.side.min(32), + reduced, + ) + } else { + TxType::DctDct + }; write_coeffs( enc, - &mut cdfs.txb(planes[plane], mode), + &mut coding, grid, scan, skip_ctx, dc_sign_ctx(around[plane].dc_vote), Some(plane), - if plane == 0 { luma_tx_type } else { TxType::DctDct }, + tx_type, ); #[cfg(test)] census_record( @@ -7474,6 +7502,7 @@ pub(crate) fn sb_coeff_inter_frame_tile_cdfs( 0, split, block_tx_type(block), + true, ); neighbours.record_planes(sb_at, SB, 0, &grids, !split); } @@ -7938,6 +7967,7 @@ pub(crate) fn sb_coeff_inter_frame_tile_cdfs( mode_for_tx, split, block_tx_type(block), + is_inter, ); neighbours.record_planes(at, BLOCK, mode_for_tx, &grids, !split); } @@ -8179,6 +8209,7 @@ fn write_inter_frame_leaf( mode_for_tx, split, block_tx_type(block), + is_inter, ); neighbours.record_planes(at, SUB, mode_for_tx, &grids, !split); } @@ -8432,6 +8463,7 @@ fn write_inter_frame_leaf8( mode_for_tx, split, block_tx_type(block), + is_inter, ); neighbours.record_mi_planes(leaf_mi, 8, &grids, !split); neighbours.record_inter_mi(leaf_mi, 8, block.skip, is_inter, block_ref(block));