diff --git a/crates/ec-av1/src/cdf.rs b/crates/ec-av1/src/cdf.rs index f0ec78f7..2aaf4793 100644 --- a/crates/ec-av1/src/cdf.rs +++ b/crates/ec-av1/src/cdf.rs @@ -326,6 +326,31 @@ pub const FILTER_INTRA: [[u16; 3]; 4] = [ /// `FILTER_INTRA_MODES` a `use_filter_intra` block picks. pub const FILTER_INTRA_MODE: [u16; 6] = [8949, 12776, 17211, 29558, 32768, 0]; +/// `default_tx_size_cdf[0]` (entropymode.c): an 8x8 block's `tx_depth` flag +/// (`TX8` vs `TX4`), indexed by `tx_size_context` (0..=2). +pub const TX_SIZE_CAT0: [[u16; 3]; 3] = [[19968, 32768, 0], [19968, 32768, 0], [24320, 32768, 0]]; +/// `default_tx_size_cdf[1]` (entropymode.c): a 16x16 block's `tx_depth` +/// (`TX16`/`TX8`/`TX4`), indexed by `tx_size_context`. +pub const TX_SIZE_CAT1: [[u16; 4]; 3] = [ + [12272, 30172, 32768, 0], + [12272, 30172, 32768, 0], + [18677, 30848, 32768, 0], +]; +/// `default_tx_size_cdf[2]` (entropymode.c): a 32x32 block's `tx_depth` +/// (`TX32`/`TX16`/`TX8`), indexed by `tx_size_context`. +pub const TX_SIZE_CAT2: [[u16; 4]; 3] = [ + [12986, 15180, 32768, 0], + [12986, 15180, 32768, 0], + [24302, 25602, 32768, 0], +]; +/// `default_tx_size_cdf[3]` (entropymode.c): a 64x64 block's `tx_depth` +/// (`TX64`/`TX32`/`TX16`), indexed by `tx_size_context`. +pub const TX_SIZE_CAT3: [[u16; 4]; 3] = [ + [5782, 11475, 32768, 0], + [5782, 11475, 32768, 0], + [16803, 22759, 32768, 0], +]; + /// `Default_Cfl_Alpha_Cdf` (spec 9.4): a `UV_CFL_PRED` block's per-plane /// alpha magnitude (spec 5.11.45's `cfl_alpha_u`/`cfl_alpha_v`), indexed by /// the joint sign's context (`CFL_CONTEXT_U`/`CFL_CONTEXT_V`, diff --git a/crates/ec-av1/src/cdf_state.rs b/crates/ec-av1/src/cdf_state.rs index 8ed2ac40..60d201e7 100644 --- a/crates/ec-av1/src/cdf_state.rs +++ b/crates/ec-av1/src/cdf_state.rs @@ -260,6 +260,15 @@ pub(crate) struct Cdfs { pub filter_intra: [[u16; 3]; 4], /// Which `FILTER_INTRA_MODES` entry a `use_filter_intra` block picks. pub filter_intra_mode: [u16; 6], + /// `TxMode::Select`'s `tx_depth` flag at an 8x8 block, by `tx_size_context` + /// (0..=2) -- see [`cdf::TX_SIZE_CAT0`]. + pub tx_size_cat0: [[u16; 3]; 3], + /// `tx_depth` at a 16x16 block -- see [`cdf::TX_SIZE_CAT1`]. + pub tx_size_cat1: [[u16; 4]; 3], + /// `tx_depth` at a 32x32 block -- see [`cdf::TX_SIZE_CAT2`]. + pub tx_size_cat2: [[u16; 4]; 3], + /// `tx_depth` at a 64x64 block -- see [`cdf::TX_SIZE_CAT3`]. + pub tx_size_cat3: [[u16; 4]; 3], } /// One motion vector component's adapting state (spec 9.4's `Default_Mv_*`, @@ -434,6 +443,10 @@ impl Cdfs { .for_each(MvComponentCdfs::reset_counts); reset2(&mut self.filter_intra); reset1(&mut self.filter_intra_mode); + reset2(&mut self.tx_size_cat0); + reset2(&mut self.tx_size_cat1); + reset2(&mut self.tx_size_cat2); + reset2(&mut self.tx_size_cat3); } /// The defaults a key frame starts from (spec 8.4, `init_coeff_cdfs` and @@ -793,6 +806,10 @@ impl Cdfs { mv_comp: [MvComponentCdfs::new(), MvComponentCdfs::new()], filter_intra: cdf::FILTER_INTRA, filter_intra_mode: cdf::FILTER_INTRA_MODE, + tx_size_cat0: cdf::TX_SIZE_CAT0, + tx_size_cat1: cdf::TX_SIZE_CAT1, + tx_size_cat2: cdf::TX_SIZE_CAT2, + tx_size_cat3: cdf::TX_SIZE_CAT3, } } diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index c8db63b3..6c94b1ad 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -61,6 +61,19 @@ static DEBLOCK_HITS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicU pub(crate) fn deblock_hits() -> usize { DEBLOCK_HITS.load(std::sync::atomic::Ordering::Relaxed) } + +/// How many [`read_tx_size`] reads resolved a `tx_depth` strictly less than +/// the block's own side, across every call in the process -- the same +/// before/after counter pattern as [`FILTER_INTRA_HITS`], proving a stream +/// actually exercised a *split* transform under `TxMode::Select` rather than +/// every block coincidentally resolving `depth=0` (indistinguishable from +/// `TxMode::Largest` pixel-wise). +static TX_DEPTH_HITS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0); + +/// Current value of [`TX_DEPTH_HITS`]. +pub(crate) fn tx_depth_hits() -> usize { + TX_DEPTH_HITS.load(std::sync::atomic::Ordering::Relaxed) +} const SUB_MI: u32 = 4; const MI: usize = 4; const SB: usize = 64; @@ -1098,6 +1111,10 @@ fn decode_block( v: &mut PlaneBuf, base_q_idx: u8, enable_filter_intra: bool, + scan32: &[u16], + scan16: &[u16], + scan8: &[u16], + tx_select: bool, ) -> Result<()> { let (r, c) = at; let (px, py) = (c * SUB, r * SUB); @@ -1110,8 +1127,31 @@ fn decode_block( side, enable_filter_intra, )?; + let (mi_r, mi_c) = (r * (SUB / MI), c * (SUB / MI)); + // `TxMode::Select`'s `tx_depth` (spec 5.11.16): read for every intra + // block, skipped or not. `logical_tx` is what the loop filter's edge + // lookup and the next block's own `tx_size_context` want (the *nominal* + // transform size -- 64 even for the 64x64 corner-scanned case, spec + // 5.11.40); `coeff_tx_side` is the actual coded coefficient grid's side, + // which only differs from it at that one corner case. + let (logical_tx, coeff_tx_side) = if tx_select { + let resolved = read_tx_size(dec, cdfs, neighbours, (mi_r, mi_c), side); + if resolved == 4 { + return Err(unsupported( + "a resolved 4x4 luma transform (TxMode::Select depth exhausted; this decoder has no 4x4 luma coefficient tables)", + )); + } + (resolved, if resolved == 64 { 32 } else { resolved }) + } else { + (side, luma_tx) + }; + let luma_set = if tx_select { + txbset_for(logical_tx) + } else { + luma_set + }; + let luma_scan = scan_for(coeff_tx_side, scan32, scan16, scan8); let reach = Reach::of(side, px, py, y.width, y.height); - let (luma_grid, u_grid, v_grid); let (cpx, cpy) = (px / 2, py / 2); let chroma_side = side / 2; if skip { @@ -1148,16 +1188,20 @@ fn decode_block( alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), None, ); - luma_grid = vec![0i32; side * side]; - u_grid = vec![0i32; chroma_side * chroma_side]; - v_grid = vec![0i32; chroma_side * chroma_side]; - } else { + let luma_grid = vec![0i32; side * side]; + let u_grid = vec![0i32; chroma_side * chroma_side]; + let v_grid = vec![0i32; chroma_side * chroma_side]; + neighbours.record(at, side, mode, &[luma_grid, u_grid, v_grid]); + } else if !tx_select || logical_tx == side { + // A single luma transform unit -- the old path, unchanged (including + // the 64x64 corner case, `logical_tx == side == 64` with + // `coeff_tx_side == 32`). let around = neighbours.around(at, side); - luma_grid = read_plane( + let luma_grid = read_plane( dec, cdfs, luma_set, - scans.0, + luma_scan, 0, around[0], mode, @@ -1167,13 +1211,13 @@ fn decode_block( px, py, side, - luma_tx, + coeff_tx_side, base_q_idx, None, filter_intra, )?; let ac = alpha.map(|_| cfl_ac_q3(y, px, py, side)); - u_grid = read_plane( + let u_grid = read_plane( dec, cdfs, chroma_set, @@ -1192,7 +1236,7 @@ fn decode_block( alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), None, )?; - v_grid = read_plane( + let v_grid = read_plane( dec, cdfs, chroma_set, @@ -1211,13 +1255,94 @@ fn decode_block( alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), None, )?; + neighbours.record(at, side, mode, &[luma_grid, u_grid, v_grid]); + } else { + // Several luma transform units, raster order (spec + // `transform_block`'s loop): the block's resolved transform is + // genuinely smaller than its own side, so each tile of it predicts + // and codes independently, its own context read fresh off whatever + // the earlier tiles in this same block already wrote -- the same + // per-TU pattern [`decode_leaf8`] already runs for an 8x8 leaf. + let n_axis = side / logical_tx; + for tu_row in 0..n_axis { + for tu_col in 0..n_axis { + let tu_mi = ( + mi_r + tu_row * (logical_tx / MI), + mi_c + tu_col * (logical_tx / MI), + ); + let tu_px = px + tu_col * logical_tx; + let tu_py = py + tu_row * logical_tx; + let tu_around = neighbours.around_mi(tu_mi, logical_tx)[0]; + let tu_reach = Reach::of(logical_tx, tu_px, tu_py, y.width, y.height); + let tu_grid = read_plane( + dec, + cdfs, + luma_set, + luma_scan, + 0, + tu_around, + mode, + mode, + tu_reach, + y, + tu_px, + tu_py, + logical_tx, + logical_tx, + base_q_idx, + None, + filter_intra, + )?; + neighbours.record_mi_luma(tu_mi, logical_tx, &tu_grid); + } + } + let ac = alpha.map(|_| cfl_ac_q3(y, px, py, side)); + let chroma_around = neighbours.around(at, side); + let u_grid = read_plane( + dec, + cdfs, + chroma_set, + scans.1, + 1, + chroma_around[1], + mode, + DC_PRED, + Reach::none(), + u, + cpx, + cpy, + chroma_side, + chroma_tx, + base_q_idx, + alpha.zip(ac.as_deref()).map(|((au, _), ac)| (au, ac)), + None, + )?; + let v_grid = read_plane( + dec, + cdfs, + chroma_set, + scans.1, + 2, + chroma_around[2], + mode, + DC_PRED, + Reach::none(), + v, + cpx, + cpy, + chroma_side, + chroma_tx, + base_q_idx, + alpha.zip(ac.as_deref()).map(|((_, av), ac)| (av, ac)), + None, + )?; + neighbours.record_split_luma(at, side, mode, [&u_grid, &v_grid]); } - neighbours.record(at, side, mode, &[luma_grid, u_grid, v_grid]); neighbours.fill_skip_grid((r * (SUB / MI), c * (SUB / MI)), side / MI, skip); neighbours.fill_lf_grid( (r * (SUB / MI), c * (SUB / MI)), side / MI, - side as u8, + logical_tx as u8, false, ); Ok(()) @@ -1246,6 +1371,7 @@ fn decode_leaf8( v: &mut PlaneBuf, base_q_idx: u8, enable_filter_intra: bool, + tx_select: bool, ) -> Result { let (r, c) = outer_at; let mut above_mode = neighbours.above_mode[c]; @@ -1269,6 +1395,19 @@ fn decode_leaf8( 8, enable_filter_intra, )?; + // `TxMode::Select`'s `tx_depth` at an 8x8 leaf (spec 5.11.16): the only + // depths an 8x8 block offers are `TX8` (depth 0, this decoder's one + // supported case) and `TX4` (depth 1, which this crate has no 4x4 luma + // coefficient tables for) -- read to stay in sync, then refuse rather + // than silently miscode. + if tx_select { + let resolved = read_tx_size(dec, cdfs, neighbours, leaf_mi, 8); + if resolved == 4 { + return Err(unsupported( + "a resolved 4x4 luma transform (TxMode::Select depth exhausted; this decoder has no 4x4 luma coefficient tables)", + )); + } + } let (px, py) = (leaf_mi.1 * MI, leaf_mi.0 * MI); let reach = Reach::of(8, px, py, y.width, y.height); let (cpx, cpy) = (px / 2, py / 2); @@ -1600,6 +1739,70 @@ fn tx_px_at(n: &Neighbours, chroma: bool, mi_r: usize, mi_c: usize) -> u32 { if chroma { luma_tx / 2 } else { luma_tx } } +/// `get_tx_size_context` (spec 9.3's `TxDepthCtx`, libaom +/// `TXFM_CONTEXT`/`entropymode.c`): the sum of two boolean terms, each +/// present only when its neighbour exists inside the true frame -- whether +/// the block immediately above (at this block's own leftmost mi column) or +/// to the left (at its own topmost mi row) coded a transform at least as wide +/// as `own_side`. +fn tx_size_context(n: &Neighbours, (mi_r, mi_c): (usize, usize), own_side: usize) -> usize { + let above = mi_r > 0 && tx_px_at(n, false, mi_r - 1, mi_c) as usize >= own_side; + let left = mi_c > 0 && tx_px_at(n, false, mi_r, mi_c - 1) as usize >= own_side; + usize::from(above) + usize::from(left) +} + +/// `read_tx_size`/`read_selected_tx_size` (spec 5.11.16): under +/// `TxMode::Select`, an intra block's `tx_depth` symbol, resolved straight to +/// the transform's own side in pixels (`side >> depth`) -- this decoder's +/// four square categories (8/16/32/64) each halve at most twice, matching +/// libaom's own per-size depth cap (`cdf::TX_SIZE_CAT0`..`TX_SIZE_CAT3`). +fn read_tx_size( + dec: &mut SymbolDecoder, + cdfs: &mut Cdfs, + n: &Neighbours, + at_mi: (usize, usize), + side: usize, +) -> usize { + let ctx = tx_size_context(n, at_mi, side); + let depth = match side { + 8 => dec.symbol(&mut cdfs.tx_size_cat0[ctx]), + 16 => dec.symbol(&mut cdfs.tx_size_cat1[ctx]), + 32 => dec.symbol(&mut cdfs.tx_size_cat2[ctx]), + 64 => dec.symbol(&mut cdfs.tx_size_cat3[ctx]), + _ => unreachable!("decode_block/decode_leaf8 only call this at 8/16/32/64"), + }; + if depth != 0 { + TX_DEPTH_HITS.fetch_add(1, std::sync::atomic::Ordering::Relaxed); + } + side >> depth +} + +/// The luma coefficient scan table for a resolved transform side -- 32/16/8, +/// the only three sizes a `TxMode::Select` block's coded coefficient grid +/// ([`read_tx_size`]'s resolved side, or 32 at the 64x64 corner case) can be. +fn scan_for<'a>(tx_px: usize, scan32: &'a [u16], scan16: &'a [u16], scan8: &'a [u16]) -> &'a [u16] { + match tx_px { + 32 => scan32, + 16 => scan16, + 8 => scan8, + _ => unreachable!("read_tx_size never resolves the coefficient grid to anything else"), + } +} + +/// The [`TxbSet`] a resolved luma transform side reads its coefficient tables +/// from -- 64 is only ever the 64x64-block, depth-0, corner-scanned case +/// ([`TxbSet::Luma64`]); every other resolved side maps to its own matching +/// set. +fn txbset_for(tx_px: usize) -> TxbSet { + match tx_px { + 64 => TxbSet::Luma64, + 32 => TxbSet::Luma32, + 16 => TxbSet::Luma16, + 8 => TxbSet::Luma8, + _ => unreachable!("read_tx_size never resolves a block's own side to anything else"), + } +} + /// `tx_size_wide_unit_log2`/`tx_size_high_unit_log2`: `log2(px / 4)`, the /// index [`tx_dim_to_filter_length`] and the chroma 4-vs-6 choice key on. fn unit_log2(px: u32) -> i32 { @@ -2140,6 +2343,7 @@ pub fn decode_key_frame_tile( enable_filter_intra: bool, cdef: &CdefParams, loop_filter: &LoopFilterParams, + tx_select: bool, ) -> Result { decode_key_frame_tile_with_cdfs( data, @@ -2152,6 +2356,7 @@ pub fn decode_key_frame_tile( cdef, loop_filter, None, + tx_select, ) .map(|(picture, _)| picture) } @@ -2175,6 +2380,7 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( cdef: &CdefParams, loop_filter: &LoopFilterParams, initial_cdfs: Option, + tx_select: bool, ) -> Result<(Picture, Cdfs)> { if mi_cols == 0 || mi_rows == 0 { return Err(unsupported("a frame with no mode-info grid")); @@ -2280,6 +2486,10 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( &mut v, base_q_idx, enable_filter_intra, + &scan32, + &scan16, + &scan8, + tx_select, )?; } PARTITION_SPLIT => { @@ -2337,6 +2547,10 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( &mut v, base_q_idx, enable_filter_intra, + &scan32, + &scan16, + &scan8, + tx_select, )?; } PARTITION_SPLIT => { @@ -2374,6 +2588,10 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( &mut v, base_q_idx, enable_filter_intra, + &scan32, + &scan16, + &scan8, + tx_select, )?; continue; } @@ -2417,6 +2635,7 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( &mut v, base_q_idx, enable_filter_intra, + tx_select, )?; prev_leaf = Some((leaf_mi, leaf_mode)); } @@ -2479,6 +2698,7 @@ pub(crate) fn decode_key_frame_tile_with_cdfs( &mut v, base_q_idx, enable_filter_intra, + tx_select, )?; prev_leaf = Some((leaf_mi, leaf_mode)); } @@ -3871,7 +4091,8 @@ mod tests { 128, false, &CdefParams::default(), - &LoopFilterParams::default() + &LoopFilterParams::default(), + false ) .is_err() ); @@ -3912,6 +4133,7 @@ mod tests { false, &CdefParams::default(), &LoopFilterParams::default(), + false, ) .unwrap(); assert_eq!(decoded.width, w, "{w}x{h}: decoded width"); @@ -3993,6 +4215,7 @@ mod tests { false, &CdefParams::default(), &LoopFilterParams::default(), + false, ) .unwrap(); assert!( @@ -4176,6 +4399,7 @@ mod tests { false, &CdefParams::default(), &LoopFilterParams::default(), + false, ) .unwrap(); assert_eq!(ours.y, ffmpeg_decoded.y, "{width}x{height}: luma vs ffmpeg"); @@ -4246,6 +4470,7 @@ mod tests { false, &CdefParams::default(), &LoopFilterParams::default(), + false, ) .unwrap(); assert_eq!( @@ -4519,6 +4744,7 @@ mod tests { false, &CdefParams::default(), &LoopFilterParams::default(), + false, ) .unwrap(); assert_eq!(reference.y, ffmpeg_frames[0].y, "frame 0 luma vs ffmpeg"); diff --git a/crates/ec-av1/src/stream.rs b/crates/ec-av1/src/stream.rs index cd0c8197..2fc5e8cd 100644 --- a/crates/ec-av1/src/stream.rs +++ b/crates/ec-av1/src/stream.rs @@ -92,22 +92,22 @@ pub fn decode_stream(data: &[u8]) -> Result> { "a frame with cdef_bits > 0 (this decoder never reads the per-64x64 cdef_idx symbol)", )); } - // Every `decode_block` call below assumes the block's transform is - // its own full size (`TxMode::Largest`, which is all this crate's own - // encoder ever writes, spec `read_tx_size` inferring the max depth - // with no bits) -- it never reads a `tx_depth` symbol at all. A real - // encoder's `TxMode::Select` (spec 5.9.2's `tx_mode` bit) codes one - // per intra block, and skipping that read does not error -- the - // decoder just goes on consuming the next bits as if they were the - // uv_mode/coefficient symbols they are not, so the block "decodes" - // syntactically clean into low-energy garbage: lane-av1real r3's - // luma-near-flat-vs-ffmpeg-gradient bug. Refuse before that, the same - // way as the CDEF case above, rather than silently miscode every - // `TxMode::Select` stream. - if header.tx_mode == TxMode::Select { + // An intra frame's `decode_key_frame_tile*` now reads `tx_depth` + // under `TxMode::Select` (lane-av1txsel, spec 5.11.16): the key-frame + // decode path threads `tx_select` through `decode_block`/ + // `decode_leaf8`, and refuses on its own (a named `unsupported`, not + // a silent desync) the one case it still cannot code -- a resolved + // 4x4 luma transform, which has no coefficient tables here. This + // refusal is narrowed to what genuinely still has no reader at all: + // `decode_inter_frame`'s inter path never threads `tx_select` + // through its own block loop, so an inter frame under + // `TxMode::Select` still desyncs the same way the comment above used + // to describe for every frame -- lane-av1real r3's + // luma-near-flat-vs-ffmpeg-gradient bug. + if header.tx_mode == TxMode::Select && header.frame_type != FrameType::Key { return Err(Error::unsupported( "AV1 decode_stream", - "a frame using TxMode::Select (this decoder never reads a tx_depth symbol, so it desyncs after the first intra block's mode)", + "an inter frame using TxMode::Select (this decoder's inter path never reads a tx_depth symbol, so it desyncs after the first block's mode)", )); } // lane-av1real r11's blindness-audit sweep (`read_intra_frame_mode_info`, @@ -214,6 +214,7 @@ pub fn decode_stream(data: &[u8]) -> Result> { &header.cdef, &header.loop_filter, initial_cdfs, + header.tx_mode == TxMode::Select, )? } else { let last_slot = header.ref_frame_idx[0] as usize; @@ -339,6 +340,7 @@ mod tests { false, &ec_av1_syntax::CdefParams::default(), &ec_av1_syntax::LoopFilterParams::default(), + false, ) .unwrap(); let via_stream = decode_stream(&encoded.stream).unwrap(); @@ -996,6 +998,155 @@ mod tests { assert_eq!(frames[0].v, ffmpeg_frames[0].v, "V vs ffmpeg"); } + /// As [`a_real_aomenc_intra_stream_with_deblocking_decodes_pixel_exact`], + /// but WITHOUT `--enable-tx-size-search=0` -- the one flag every other + /// recipe in this file pins off specifically to keep the stream on + /// `TxMode::Largest`. Dropped, aomenc's RD is free to pick + /// `TxMode::Select` and split some blocks' luma transform below their own + /// side (spec 5.11.16), which is what this test's `tx_depth_hits()` + /// counter proves actually happened -- a stream that resolves every + /// `tx_depth` to 0 would pixel-match by construction (identical to + /// `TxMode::Largest`) without exercising the new split-TU decode path at + /// all. Same multi-seed retry shape as + /// [`a_real_aomenc_inter_sequence_with_cdf_forwarding_decodes_pixel_exact`]: + /// aomenc's own RD is free (with tx-size search on) to also occasionally + /// pick `allow_screen_content_tools` on this content, a separate, + /// already-named refusal -- try several seeds, and the first stream that + /// both decodes AND actually split a transform must be pixel-exact. + #[test] + fn a_real_aomenc_intra_stream_with_tx_select_decodes_pixel_exact() { + if !have_ffmpeg() { + eprintln!( + "SKIP a_real_aomenc_intra_stream_with_tx_select_decodes_pixel_exact: no ffmpeg" + ); + return; + } + if !have_aomenc() { + eprintln!( + "SKIP a_real_aomenc_intra_stream_with_tx_select_decodes_pixel_exact: no aomenc at {}", + aomenc_path().display() + ); + return; + } + let (width, height) = (64usize, 64usize); + let mut refusals = Vec::new(); + for attempt in 0..40u32 { + let seed = 42 + attempt; + // A flat gradient never has enough local detail for the RD + // search to prefer a split transform over the block's own full + // size -- `mandelbrot`'s fractal boundary gives every attempt + // (a different `start_scale`) sharp, varied-frequency edges. + let source = format!( + "mandelbrot=size=64x64:start_scale={}:rate=25", + 3.0 - f64::from(attempt) * 0.06 + ); + let y4m = Command::new("ffmpeg") + .args([ + "-v", + "error", + "-f", + "lavfi", + "-i", + &source, + // Desaturated: chroma stays flat (constant 128) so + // `uv_mode` always resolves to `DC_PRED` and this + // decoder's still-unsupported directional-chroma round-2 + // gap never fires, leaving the luma detail alone to + // drive whether `TxMode::Select`'s RD splits a block. + "-vf", + "hue=s=0", + "-pix_fmt", + "yuv420p", + "-t", + "0.04", + "-f", + "yuv4mpegpipe", + "-", + ]) + .stdin(Stdio::null()) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .output() + .expect("ffmpeg failed to run"); + assert!( + y4m.status.success(), + "ffmpeg fixture: {}", + String::from_utf8_lossy(&y4m.stderr) + ); + let mut child = Command::new(aomenc_path()) + .args([ + "--codec=av1", + "--passes=1", + "--end-usage=q", + "--cq-level=45", + "--cpu-used=0", + "--enable-rect-partitions=0", + "--enable-ab-partitions=0", + "--enable-1to4-partitions=0", + "--enable-filter-intra=0", + "--enable-angle-delta=0", + "--enable-cdef=0", + // Loop restoration is a named refusal (aomenc's RD picks + // Sgrproj on this kind of content) -- off in every recipe + // whose test targets a different surface. + "--enable-restoration=0", + "--max-partition-size=32", + "--enable-palette=0", + "--enable-intrabc=0", + "--enable-cfl-intra=1", + "--obu", + "-o", + "-", + "-", + ]) + .stdin(Stdio::piped()) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .spawn() + .expect("aomenc failed to start"); + child + .stdin + .take() + .expect("aomenc stdin") + .write_all(&y4m.stdout) + .expect("writing y4m to aomenc"); + let out = child.wait_with_output().expect("aomenc failed to run"); + assert!( + out.status.success(), + "aomenc refused the fixture: {}", + String::from_utf8_lossy(&out.stderr) + ); + let stream = out.stdout; + let before = decode::tx_depth_hits(); + let frames = match decode_stream(&stream) { + Ok(frames) => frames, + Err(e) => { + refusals.push(format!("seed {seed}: {e}")); + continue; + } + }; + if decode::tx_depth_hits() == before { + refusals.push(format!( + "seed {seed}: decoded, but no block resolved a split tx_depth" + )); + continue; + } + let ffmpeg_frames = ffmpeg_decode_sequence(&stream, width, height, 1); + assert_eq!( + frames[0].y, ffmpeg_frames[0].y, + "luma vs ffmpeg (seed {seed})" + ); + assert_eq!(frames[0].u, ffmpeg_frames[0].u, "U vs ffmpeg (seed {seed})"); + assert_eq!(frames[0].v, ffmpeg_frames[0].v, "V vs ffmpeg (seed {seed})"); + return; + } + eprintln!( + "SKIP a_real_aomenc_intra_stream_with_tx_select_decodes_pixel_exact: every attempt \ + hit a named refusal or never split a transform:\n{}", + refusals.join("\n") + ); + } + /// As above, but a multi-frame inter sequence -- `--lag-in-frames=0 /// --auto-alt-ref=0` keep every non-key frame a simple forward /// single-`LAST_FRAME` P frame (no alt-ref/backward reference this