//! The stream-level entry point a real decoder needs: walk a raw AV1 OBU //! stream (`Encoded::stream`, or any low-overhead-format bitstream this //! crate's own writers produce) via [`ec_av1_syntax::Av1Parser`] and dispatch //! each frame's tile payload to [`crate::decode::decode_key_frame_tile_with_cdfs`] / //! [`crate::decode::decode_inter_frame_tile_with_cdfs`], threading a //! single-slot picture DPB exactly as [`crate::encode::encode_sequence`] does //! on the write side, plus the full 8-slot *CDF* reference bank spec 7.20 //! (`load_cdfs`) and 7.4 (the reference frame update) describe: a frame whose //! header names `primary_ref_frame != PRIMARY_REF_NONE` resumes from the //! adapted table a previous frame's `refresh_frame_flags` left in that slot, //! rather than the spec 8.4 defaults. //! //! This is the only reachable path for a caller that does not have the //! encoder's own `Encoded::tile`/`mi_cols`/`mi_rows`/`base_q_idx` fields (they //! are `pub(crate)`, and rightly so: the wire is `stream`, everything else is //! an implementation detail of how this crate happens to build it). use ec_av1_syntax::{ Av1Parser, FrameType, NUM_REF_FRAMES, ObuKind, PRIMARY_REF_NONE, TxMode, WarpModel, }; use ec_core::{Error, Result}; use crate::cdf_state::Cdfs; use crate::decode; use crate::decode::{ decode_inter_frame_tile_with_cdfs, decode_key_frame_tile, decode_key_frame_tile_with_cdfs, q_ctx_of, }; use crate::encode::Picture; /// Decode every frame in a raw AV1 OBU stream, in coding order. /// /// A key frame becomes the reference for the inter frames that follow it, /// until the next key frame resets the DPB — the same single-slot chain /// [`crate::encode::encode_sequence`] threads on the write side. Only a /// `Frame` OBU (an uncompressed header and its tile group in one OBU, which /// is all this crate's encoder ever writes) carries a tile payload; sequence /// headers and temporal delimiters are consumed for the state they carry (or /// skipped) and produce no picture. /// /// # Errors /// Returns an error when the stream is truncated or malformed (as /// [`Av1Parser`] reports it), when a frame header names anything this /// crate's tile decoders do not reconstruct (see their own docs), or when an /// inter frame appears before any key frame has supplied a reference. pub fn decode_stream(data: &[u8]) -> Result> { let mut parser = Av1Parser::new(); let mut pictures = Vec::new(); let mut pictures_decoded: usize = 0; // Spec 7.20/7.4: each of the 8 reference slots remembers the picture a // previous frame's `refresh_frame_flags` stored into it, exactly like the // CDF slots below -- `LAST_FRAME` names a slot via `ref_frame_idx[0]`, // not simply "the previous frame decoded"; a frame whose own predecessor // didn't refresh that slot (an altref/hidden frame, or any GOP with more // than one live reference) must keep predicting from whatever picture is // still sitting there. Round 7's cdffwd hunt: a global `reference` // overwritten every frame silently always used "the most recent decode" // instead, producing a bit-exact motion vector into the WRONG picture -- // luma-only (chroma coincidentally shares this fixture's flat regions) // and small-magnitude (the two pictures mostly agree), which is exactly // what made it look like a rounding bug rather than a wrong reference. let mut ref_slots: [Option; NUM_REF_FRAMES] = std::array::from_fn(|_| None); // lane-av1tmvp: each of the 8 reference slots' own saved temporal motion // field (spec 7.9's per-frame `MotionFieldMvs` storage, libaom // `cur_frame->mvs`) plus the `OrderHint` the picture in that slot was // decoded with -- `Picture` itself carries neither, so both ride // alongside `ref_slots` on the exact same `refresh_frame_flags` update. let mut motion_field_slots: [Option; NUM_REF_FRAMES] = std::array::from_fn(|_| None); let mut order_hint_slots: [u32; NUM_REF_FRAMES] = [0; NUM_REF_FRAMES]; // Spec 7.20/7.4: each of the 8 reference slots remembers the CDF state a // previous frame's `refresh_frame_flags` stored into it -- the frame's // own end-of-tile adapted table, or (when the frame set // `disable_frame_end_update_cdf`) the table it started from, unchanged. let mut cdf_slots: [Option; NUM_REF_FRAMES] = std::array::from_fn(|_| None); let mut pos = 0usize; while pos < data.len() { let obu = parser.parse_obu(&data[pos..])?; let obu_offset = pos; pos += obu.total_size; if let ObuKind::FrameHeader(header) = &obu.kind { // `show_existing_frame` (spec 7.21/5.9.2): no tile payload, this // OBU only names a DPB slot to output again -- the coded-frame // count and the shown-picture count are different things (a // hidden altref's own `Frame` OBU below is never pushed to // `pictures` on its own; it is only pushed here, later, when a // `show_existing_frame` header names its slot). Before r15 this // arm didn't exist at all: every `ObuKind::Frame` was pushed // unconditionally regardless of `show_frame`, so a hidden altref // was emitted (wrongly, in decode order) and its real // `show_existing_frame` output was silently dropped. let slot = header.frame_to_show_map_idx as usize; let picture = ref_slots[slot].clone().ok_or_else(|| { Error::unsupported( "AV1 decode_stream", "a show_existing_frame header naming an empty reference slot", ) })?; for i in 0..NUM_REF_FRAMES { if header.refresh_frame_flags & (1 << i) != 0 { cdf_slots[i] = cdf_slots[slot].clone(); ref_slots[i] = Some(picture.clone()); motion_field_slots[i] = motion_field_slots[slot].clone(); order_hint_slots[i] = order_hint_slots[slot]; } } let output = if header.film_grain.apply_grain { let mc_identity = parser .sequence_header() .is_some_and(|seq| seq.color_config.matrix_coefficients == 0); crate::film_grain::apply_grain(&picture, &header.film_grain, mc_identity) } else { picture }; pictures.push(output); continue; } let ObuKind::Frame(header, tiles) = obu.kind else { continue; }; let tile = tiles.first().ok_or_else(|| { Error::unsupported("AV1 decode_stream", "a frame OBU with no tile group") })?; // `read_cdef` (spec `decodeframe.c`, called at the first non-skip // block of each 64x64) only reads a literal `cdef_idx` when // `cdef_bits > 0`; at `cdef_bits == 0` there is nothing to read (this // crate's own writer's only case, and both `lane-av1cdef` gate // streams'), so the per-block symbol this crate's tile readers never // consume is a true no-op there. A `cdef_bits > 0` stream would // desync the moment it hit a real strength selector this decoder // never reads — refuse that by name rather than silently miscode, // the same pattern as the other round-2 refusals below. if header.cdef.bits != 0 { return Err(Error::unsupported( "AV1 decode_stream", "a frame with cdef_bits > 0 (this decoder never reads the per-64x64 cdef_idx symbol)", )); } // 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", "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`, // libaom decodemv.c): three more per-block/per-SB symbols this crate // never reads at all, each gated by a header field this decoder // otherwise accepts uncomplaining -- the same silent-desync shape // `use_filter_intra` was. `allow_screen_content_tools` gates both // `read_intrabc_info` and `read_palette_mode_info`; `delta.q_present`/ // `delta.lf_present` gate `read_delta_qindex`/`read_delta_lflevel` // (spec 5.11.10/5.11.11, one symbol group per superblock). Refuse // before desyncing rather than silently miscode, the same pattern as // the CDEF/TxMode::Select refusals above. // lane-screen r2: key frames consume (not reconstruct) palette/ // intrabc syntax via read_intra_mode, and decode_inter_block/ // decode_inter_block8's own intra-sub-block branches now consume // palette the same way (libaom's read_intra_block_mode_info has no // intrabc call at all -- that symbol is intra-frame-only). A // genuine palette/intrabc use still refuses by name deeper in the // block readers, so no whole-frame refusal is needed here anymore. if header.delta.q_present || header.delta.lf_present { return Err(Error::unsupported( "AV1 decode_stream", "a frame with delta_q_present or delta_lf_present set (this decoder never reads the per-superblock delta symbols)", )); } if header.segmentation.enabled { return Err(Error::unsupported( "AV1 decode_stream", "a frame with segmentation enabled (this decoder never reads a per-block segment_id symbol)", )); } // Loop restoration carries per-restoration-unit symbols in the tile // (spec 5.11.57 `read_lr`, one group per LR unit reached from the // superblock walk) that this decoder never reads -- same // silent-desync shape as the refusals above. Traced live 2026-08-27: // an aomenc inter frame with `Sgrproj` on the V plane desynced the // partition walk into out-of-alphabet garbage. if header.loop_restoration.uses_lr { return Err(Error::unsupported( "AV1 decode_stream", "a frame with loop restoration enabled (this decoder never reads the per-unit lr symbols)", )); } // `decode_inter_block`/`decode_inter_block8`'s `GLOBALMV` arm (spec // 5.11.26's `read_inter_intra`... really `assign_mv`'s `GLOBALMV` // case, spec 7.10.2.1) uses `gm_get_motion_vector`, which is the // zero vector only under `GmType == IDENTITY` -- any other warp // model needs the full affine/translation MV derivation this // decoder does not carry. `global_motion` is indexed `ref_frame - 1` // (`read_global_motion_params`'s `i` loops from `LAST_FRAME`); // lane-av1refs widens this from `[0, 3]` (LAST_FRAME/GOLDEN_FRAME) // to every one of the 7 single-reference slots `decode_inter_block` // can now select. // // lane-gm r2 STEP-8 ATTEMPT (reverted): removing this refusal // exposed a real pixel mismatch -- // a_real_aomenc_stream_with_interintra_wedge_decodes_pixel_exact // seed 43, frame 1 luma, with EC_WEDGE_GATE_ATTEMPTS=20 // EC_AV1_REQUIRE_AOMENC=1 -- so the single-ref/compound MV wiring // is not yet provably correct end to end; keeping the refusal // until that seed is captured (EC_AV1_GATE_DUMP), self-pinned, and // root-caused rather than shipping a wrong decode. // // lane-gm r3 ROOT CAUSE (pinned, range-ladder-confirmed, still not // fixed): the mismatch is NOT an mv/mvstack/entropy bug -- msac // RANGE at the first divergent block (mi=(8,0), frame 1) matches // aomdec's own `EC_PART` trace bit-for-bit (rng=38664 both sides), // and this decoder's own `gm_get_motion_vector` for that block // (ROTZOOM, seed 43's own header) computes the spec-correct // mv=(-8,8) (hand-verified against mv.h's `block_center_x`/`_y`, // which found and fixed a real `- 1` omission in `warp.rs` -- // harmless for THIS block's rounding but a genuine spec bug fixed // regardless). The actual gap: `reconinter.c`'s `allow_warp` has a // `global_warp_allowed` branch (`warp_types.global_warp_allowed = // is_global_mv_block(...)`, `reconinter_enc.c:281`) that predicts // an `is_global_mv_block` block (ROTZOOM/AFFINE GLOBALMV, >=8x8) // with the FULL per-pixel affine global-motion warp, independent // of `motion_mode` -- this decoder only ever builds a warp // prediction for local `WARPED_CAUSAL` motion mode and falls back // to plain translational MC with the block-centre mv for every // other GLOBALMV block, which is only a centre-point approximation // of the true per-pixel warp. That is the entire remaining gap. // lane-gm r4: `decode.rs` now feeds `global_motion[ref]` into // `crate::warp::global_warp_params` for every single-ref // `is_global_mv_block` (ROTZOOM/AFFINE), matching `allow_warp`'s // `global_warp_allowed` branch -- ROTZOOM is pin-verified // (`gm-seed43.obu`, frame 1 luma MATCH) and TRANSLATION already // worked (`gm_get_motion_vector`'s translation-only mv, no warp // needed). AFFINE is untested this round (no aomenc fixture in the // wedge gate reached a 6-parameter model) -- keep refusing it by // name rather than shipping an unverified decode. if header.frame_type != FrameType::Key && header.global_motion[..7] .iter() .any(|gm| gm.model == WarpModel::Affine) { return Err(Error::unsupported( "AV1 decode_stream", "an inter frame whose global motion for a single-reference frame is AFFINE (unverified this round; ROTZOOM/TRANSLATION/IDENTITY are proven)", )); } // lane-gm r4/r5/r6: r4 found a NEW mismatch on frames with two // concurrently active ROTZOOM/AFFINE ref slots (GOLDEN + BWDREF) and // refused the shape by name rather than guess-fix it. r5 localized // the mismatch to an ordinary NEWMV/GOLDEN block, not the new // global-warp branch at all -- proving "two active slots" was a // correlated symptom, not the cause. r6 range-laddered that block // against aomdec's `EC_TRACE_MODE` (identical `rng` before/after // mode info -- symbol consumption bit-exact) and found the real // defect in `mvstack.rs`'s single-reference predictor fallback: // libaom's `setup_ref_mv_list` fills any `mv_ref_list` slot the real // neighbour scan left short with this block's OWN global motion // vector for its ref (`gm_get_motion_vector`), not zero -- invisible // whenever the live ref's global motion is itself // identity/translation, wrong the moment it's a real ROTZOOM/AFFINE // and the querying block's own stack comes up short (frame 14's // mi=(0,0), the very first block decoded, has no neighbours at // all). Fixed at the predictor fallback; the refusal above (single // untested shape: AFFINE on a single-ref frame) stays, but the // *multi-slot* refusal this comment used to guard is lifted -- the // r5/r6 pin (seed-43 wedge-gate mismatch) now decodes all 24 frames // byte-exact with it gone. // lane-av1comp: `decode_inter_block`/`decode_inter_block8` now read // `comp_mode` per block whenever this frame's own `reference_select` // header bit is set (spec 5.11.25), and refuse by name the blocks // that pick `COMPOUND_REFERENCE` -- two-reference motion // compensation is not wired yet. Round 14: `skip_mode` (spec 5.9.22) // is wired at the block level too (forced NEAREST_NEARESTMV compound // of `skip_mode_frame`, plain average blend), so the old blanket // `skip_mode_present` refusal is gone. // `context_update_tile_id` (spec `decode_tile`/`exit_symbol`) names // which tile's end-of-tile adapted CDFs become the frame's own; this // decoder only ever decodes tile 0 (`tiles.first()` above), so a // multi-tile frame is refused rather than silently forwarding the // wrong tile's table (or leaving the rest of the picture undecoded). if header.tile_info.cols > 1 || header.tile_info.rows > 1 { return Err(Error::unsupported( "AV1 decode_stream", "a frame with more than one tile (this decoder only ever decodes tile 0)", )); } // `Tile::offset` is relative to the buffer `parse_obu` was handed // (`&data[pos..]` at the time this OBU was parsed), so it is relative // to `obu_offset`, not to `data` as a whole. let start = obu_offset + tile.offset; let tile_bytes = data.get(start..start + tile.size).ok_or(Error::NeedMore)?; let enable_filter_intra = parser .sequence_header() .is_some_and(|seq| seq.enable_filter_intra); let enable_dual_filter = parser .sequence_header() .is_some_and(|seq| seq.enable_dual_filter); let enable_edge_filter = parser .sequence_header() .is_some_and(|seq| seq.enable_intra_edge_filter); // Inter frames' intra blocks read this through a thread-local that only // the key-frame tile path used to set; without this call a stream whose // inter frames are decoded on a thread that previously decoded a // different sequence would inherit that sequence's bit. crate::decode::set_enable_edge_filter(enable_edge_filter); // lane-av1comp: `comp_group_idx`/`compound_idx`'s own gating bits. let enable_masked_compound = parser .sequence_header() .is_some_and(|seq| seq.enable_masked_compound); let enable_jnt_comp = parser .sequence_header() .is_some_and(|seq| seq.enable_jnt_comp); // lane-sb128 r4: `interintra`'s own gating bit -- see decode.rs's // read site doc. let enable_interintra_compound = parser .sequence_header() .is_some_and(|seq| seq.enable_interintra_compound); let interp_fixed = match header.interpolation_filter { ec_av1_syntax::InterpolationFilter::Switchable => None, fixed => Some(crate::mc::InterpFilterKind::from_header(fixed)), }; // Spec 7.20 `load_cdfs`: a frame naming a `primary_ref_frame` resumes // from that reference slot's saved CDF state instead of the spec 8.4 // defaults `decode_key/inter_frame_tile_with_cdfs`'s `None` case // builds. let initial_cdfs = if header.primary_ref_frame == PRIMARY_REF_NONE { None } else { let slot = header.ref_frame_idx[header.primary_ref_frame as usize] as usize; Some(cdf_slots[slot].clone().ok_or_else(|| { Error::unsupported( "AV1 decode_stream", "a frame naming primary_ref_frame at a reference slot with no saved CDF state", ) })?) }; let started_from = initial_cdfs.clone(); let order_hint_bits = parser .sequence_header() .map_or(0, |seq| seq.order_hint_bits); // spec 7.9/7.20: this frame's own `ref_order_hints` -- the OrderHint // of the picture sitting in each of its 7 single references' DPB // slots, for this frame's *own* saved [`crate::motion_field::MotionField`] // (a later frame projects from it) whether or not this frame itself // reads temporal candidates. let ref_order_hints: [u32; 7] = std::array::from_fn(|i| order_hint_slots[header.ref_frame_idx[i] as usize]); let (picture, end_cdfs, motion_field) = if header.frame_type == FrameType::Key { let (picture, end_cdfs) = decode_key_frame_tile_with_cdfs( &[tile_bytes], &header.tile_info, header.mi_cols, header.mi_rows, header.quantization.base_q_idx, header.frame_width, header.frame_height, enable_filter_intra, enable_edge_filter, &header.cdef, &header.loop_filter, initial_cdfs, header.tx_mode == TxMode::Select, header.reduced_tx_set, header.allow_screen_content_tools, header.allow_intrabc, )?; // A key frame codes no inter blocks -- its own saved motion // field has no cells set, matching libaom's own "intra frame // contributes nothing to a later projection" behaviour, but // still carries `order_hint`/`ref_order_hints` for the distance // arithmetic a later frame's projection needs. let motion_field = crate::motion_field::MotionField::new( header.mi_cols as usize, header.mi_rows as usize, header.order_hint, ref_order_hints, ); (picture, end_cdfs, motion_field) } else { let last_slot = header.ref_frame_idx[0] as usize; let reference = ref_slots[last_slot].as_ref().ok_or_else(|| { Error::unsupported( "AV1 decode_stream", "an inter frame with no key frame before it", ) })?; // `ref_frame_idx[i]` names the DPB slot for `LAST_FRAME + i` // (spec 7.8/7.20). Any of them having no picture yet is not a // stream error on its own: `decode_inter_block` only needs a // given slot when a block actually selects that reference, and // refuses by name there if the slot is still empty -- widened // (lane-av1refs) from `GOLDEN_FRAME`'s own slot alone to every // one of the 7 single-reference slots. let other_refs: [Option<&Picture>; 8] = std::array::from_fn(|ref_frame| { if ref_frame == 0 { None } else { ref_slots .get(header.ref_frame_idx[ref_frame - 1] as usize) .and_then(Option::as_ref) } }); // spec 7.9's own driver, `av1_setup_motion_field`: only run when // this frame's header actually asks for temporal candidates -- // `find_mv_stack_with_sign_bias` reproduces its old always-`None` // behaviour bit for bit when this is `None` (see its own doc). let tpl_field = header.use_ref_frame_mvs.then(|| { crate::motion_field::setup_motion_field( &motion_field_slots, header.ref_frame_idx, header.order_hint, order_hint_bits, header.mi_rows as usize, header.mi_cols as usize, ) }); decode_inter_frame_tile_with_cdfs( tile_bytes, header.mi_cols, header.mi_rows, header.quantization.base_q_idx, header.frame_width, header.frame_height, reference, other_refs, &header.cdef, &header.loop_filter, initial_cdfs, header.allow_high_precision_mv, header.force_integer_mv, header.ref_frame_sign_bias, header.global_motion, interp_fixed, enable_dual_filter, order_hint_bits, header.order_hint, ref_order_hints, tpl_field.as_ref(), header.reference_select, enable_masked_compound, enable_jnt_comp, enable_interintra_compound, header.skip_mode_present, header.skip_mode_frame, header.reduced_tx_set, header.is_motion_mode_switchable, header.allow_warped_motion, header.allow_screen_content_tools, )? }; // Spec 7.20: `disable_frame_end_update_cdf` stores the frame's // *initial* table into the slots it refreshes, not the adapted // end-of-tile one. let stored_cdfs = if header.disable_frame_end_update_cdf { started_from.unwrap_or_else(|| Cdfs::new(q_ctx_of(header.quantization.base_q_idx))) } else { // Spec 7.20 `save_cdfs` (libaom `av1_reset_cdf_symbol_counters`): // the adapted table is saved with every symbol counter zeroed, // not with the counts the tile's own decode left behind. let mut end_cdfs = end_cdfs; end_cdfs.reset_counts(); end_cdfs }; if let Ok(path) = std::env::var("EC_AV1_DUMP_TABLES") { use std::io::Write; if let Ok(mut f) = std::fs::OpenOptions::new() .create(true) .append(true) .open(&path) { let _ = writeln!(f, "=== frame idx={} ===", pictures.len()); let _ = writeln!(f, "partition_w64 {:?}", stored_cdfs.partition_w64); let _ = writeln!(f, "partition_w32 {:?}", stored_cdfs.partition_w32); let _ = writeln!(f, "partition_w16 {:?}", stored_cdfs.partition_w16); let _ = writeln!(f, "skip {:?}", stored_cdfs.skip); let _ = writeln!(f, "new_mv {:?}", stored_cdfs.new_mv); let _ = writeln!(f, "zero_mv {:?}", stored_cdfs.zero_mv); let _ = writeln!(f, "ref_mv {:?}", stored_cdfs.ref_mv); let _ = writeln!(f, "mv_joint {:?}", stored_cdfs.mv_joint); } } // lane-comppin r3: decode-order (not display-order) dump of every // frame's own post-deblock buffer -- the pre-existing pixel diff // (`scratch_isolate_pinned_mismatch`) only ever compares *shown* // frames, so a hidden reference (altref/bwdref) that DPB-corrupts a // later shown frame is otherwise invisible; diff this byte-for-byte // against `EC_AV1_POSTFILT_DUMP.fN` from the instrumented aomdec // build to isolate whether the defect is in a hidden frame's own // reconstruction or downstream of it. if let Ok(path) = std::env::var("EC_AV1_DECODE_ORDER_DUMP") { use std::io::Write; let idx = pictures_decoded; if let Ok(mut f) = std::fs::File::create(format!("{path}.f{idx}")) { let _ = f.write_all(&picture.y); let _ = f.write_all(&picture.u); let _ = f.write_all(&picture.v); } } pictures_decoded += 1; for i in 0..NUM_REF_FRAMES { if header.refresh_frame_flags & (1 << i) != 0 { cdf_slots[i] = Some(stored_cdfs.clone()); // Spec 7.18.3.1: synthesized grain is never stored for later // prediction -- the reference bank always keeps the clean, // pre-grain decode, even though the frame pushed onto the // caller's output below carries the grained picture. ref_slots[i] = Some(picture.clone()); motion_field_slots[i] = Some(motion_field.clone()); order_hint_slots[i] = header.order_hint; } } if header.show_frame { let output = if header.film_grain.apply_grain { let mc_identity = parser .sequence_header() .is_some_and(|seq| seq.color_config.matrix_coefficients == 0); crate::film_grain::apply_grain(&picture, &header.film_grain, mc_identity) } else { picture }; pictures.push(output); } } Ok(pictures) } #[cfg(test)] mod tests { use super::*; use crate::encode::{Picture as Pic, encode_key_frame, encode_sequence}; fn test_card(width: usize, height: usize) -> Pic { let mut picture = Pic::grey(width, height); for row in 0..height { for col in 0..width { picture.y[row * width + col] = ((row * 7 + col * 11) % 251) as u8; } } for row in 0..height / 2 { for col in 0..width / 2 { let i = row * width / 2 + col; picture.u[i] = (100 + (col * 60 / (width / 2).max(1))) as u8; picture.v[i] = (200 - (row * 80 / (height / 2).max(1))) as u8; } } picture } fn panned_test_card(width: usize, height: usize, shift: i64) -> Pic { let mut picture = Pic::grey(width, height); for y in 0..height { for x in 0..width { let sx = (x as i64 - shift).rem_euclid(width as i64) as f64; let gradient = sx * 200.0 / width as f64; picture.y[y * width + x] = (20.0 + gradient).clamp(0.0, 255.0) as u8; } } for y in 0..height / 2 { for x in 0..width / 2 { let sx = (x as i64 - shift / 2).rem_euclid((width / 2) as i64) as usize; let i = y * width / 2 + x; picture.u[i] = (100 + (sx * 60 / (width / 2))) as u8; picture.v[i] = (200 - (y * 80 / (height / 2))) as u8; } } picture } /// `decode_stream` on a lone key frame's stream matches both the tile /// path (`decode_key_frame_tile` called directly on `Encoded::tile`) and /// the encoder's own reconstruction, at an even and an odd size. #[test] fn decode_stream_matches_the_tile_path_on_a_key_frame() { for &(width, height) in &[(64usize, 64usize), (216, 96)] { let picture = test_card(width, height); let encoded = encode_key_frame(&picture, 100, 0.5).unwrap(); let via_tile = decode_key_frame_tile( &encoded.tile, encoded.mi_cols, encoded.mi_rows, encoded.base_q_idx, width as u32, height as u32, false, &ec_av1_syntax::CdefParams::default(), &ec_av1_syntax::LoopFilterParams::default(), false, // Our own encoder always writes `reduced_tx_set: true`. true, false, false, ) .unwrap(); let via_stream = decode_stream(&encoded.stream).unwrap(); assert_eq!(via_stream.len(), 1, "{width}x{height}: one picture"); assert_eq!(via_stream[0].y, via_tile.y, "{width}x{height}: luma"); assert_eq!(via_stream[0].u, via_tile.u, "{width}x{height}: U"); assert_eq!(via_stream[0].v, via_tile.v, "{width}x{height}: V"); assert_eq!( via_stream[0].y, encoded.reconstruction.y, "{width}x{height}: luma vs encoder reconstruction" ); } } /// A GOP (key frame plus panned inter frames, so blocks actually take /// motion) decodes bit-exact through `decode_stream` alone, threading its /// own DPB rather than a test-supplied reference. fn gop_round_trips(width: usize, height: usize) { let pictures: Vec<_> = (0..4) .map(|i| panned_test_card(width, height, i * 3)) .collect(); let encoded = encode_sequence(&pictures, 100, 0.5).unwrap(); let decoded = decode_stream(&encoded.stream).unwrap(); assert_eq!(decoded.len(), encoded.frames.len()); for (i, (got, frame)) in decoded.iter().zip(&encoded.frames).enumerate() { assert_eq!( got.y, frame.reconstruction.y, "{width}x{height} frame {i} luma" ); assert_eq!( got.u, frame.reconstruction.u, "{width}x{height} frame {i} U" ); assert_eq!( got.v, frame.reconstruction.v, "{width}x{height} frame {i} V" ); } } #[test] fn decode_stream_round_trips_a_gop() { gop_round_trips(128, 64); } /// Same claim at a size that is not a whole number of 32x32 blocks, so /// the inter frame's 16x16-leaf split path is exercised too. #[test] fn decode_stream_round_trips_an_odd_size_gop() { gop_round_trips(216, 96); } use std::io::Write; use std::process::{Command, Stdio}; /// Firing-detection gates (single-ref envelope, temporal-MV, `comp_mode`, /// `skip_mode`/compound) all read a process-global atomic counter delta /// (`decode::ref_hits`/`tmv_hits`/`comp_mode_hits`/`skip_mode_hits`) to /// prove a real block picked the feature under test, not just that a /// header bit was set. Under `cargo test`'s default parallel run, a /// SIBLING test's own decode bumps the same global counter mid-gate -- /// a false "fired" reading pixel-compares an attempt the gate would /// otherwise `continue` past, on top of genuine defects that path may /// still have. Serialising every counter-delta gate behind this one /// mutex makes firing detection honest again (matches the existing /// cdf-forwarding-gate flake shape, ledger `omp-liveness-pgrep-self-match`). static GATE_COUNTER_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(()); fn lock_gate_counters() -> std::sync::MutexGuard<'static, ()> { GATE_COUNTER_LOCK.lock().unwrap_or_else(|e| e.into_inner()) } /// lane-cdfflake: a cheap, dependency-free (FNV-1a, not cryptographic -- /// this is a load-bearing-identity fingerprint for flake triage, not a /// security digest, so no `sha2` dep is pulled in) fixture fingerprint /// for the cdf-forwarding gate's instrumentation below. fn fnv1a64(bytes: &[u8]) -> u64 { let mut hash = 0xcbf29ce484222325u64; for &b in bytes { hash ^= b as u64; hash = hash.wrapping_mul(0x100000001b3); } hash } fn have_ffmpeg() -> bool { Command::new("ffmpeg") .arg("-version") .stdout(Stdio::null()) .stderr(Stdio::null()) .status() .is_ok_and(|s| s.success()) } /// Decodes `frames` concatenated 4:2:0 frames out of one AV1 OBU stream. fn ffmpeg_decode_sequence( stream: &[u8], width: usize, height: usize, frames: usize, ) -> Vec { let mut child = Command::new("ffmpeg") .args([ "-v", "error", "-f", "obu", "-i", "-", "-f", "rawvideo", "-pix_fmt", "yuv420p", "-", ]) .stdin(Stdio::piped()) .stdout(Stdio::piped()) .stderr(Stdio::piped()) .spawn() .expect("ffmpeg failed to start"); child .stdin .take() .expect("ffmpeg stdin") .write_all(stream) .expect("writing the stream to ffmpeg"); let out = child.wait_with_output().expect("ffmpeg failed to run"); assert!( out.status.success(), "ffmpeg refused the stream: {}", String::from_utf8_lossy(&out.stderr) ); let (luma, chroma) = (width * height, width * height / 4); let frame_bytes = luma + 2 * chroma; assert_eq!( out.stdout.len(), frame_bytes * frames, "expected {frames} 4:2:0 frames, ffmpeg said: {}", String::from_utf8_lossy(&out.stderr) ); (0..frames) .map(|i| { let base = i * frame_bytes; Pic { width, height, y: out.stdout[base..base + luma].to_vec(), u: out.stdout[base + luma..base + luma + chroma].to_vec(), v: out.stdout[base + luma + chroma..base + frame_bytes].to_vec(), } }) .collect() } /// `decode_stream` agrees with ffmpeg/dav1d on the same wire bytes -- an /// independent decoder, not just this crate checking its own tile path. #[test] fn decode_stream_agrees_with_ffmpeg_on_a_gop() { if !have_ffmpeg() { eprintln!("SKIP decode_stream_agrees_with_ffmpeg_on_a_gop: no ffmpeg"); return; } let (width, height) = (128usize, 64usize); let pictures: Vec<_> = (0..3) .map(|i| panned_test_card(width, height, i * 3)) .collect(); let encoded = encode_sequence(&pictures, 100, 0.5).unwrap(); let ffmpeg_frames = ffmpeg_decode_sequence(&encoded.stream, width, height, 3); let decoded = decode_stream(&encoded.stream).unwrap(); assert_eq!(decoded.len(), 3); for (i, (got, want)) in decoded.iter().zip(&ffmpeg_frames).enumerate() { assert_eq!(got.y, want.y, "frame {i} luma vs ffmpeg"); assert_eq!(got.u, want.u, "frame {i} U vs ffmpeg"); assert_eq!(got.v, want.v, "frame {i} V vs ffmpeg"); } } /// A hand-built 3-frame stream that actually fires `GOLDEN_FRAME` /// through [`decode_stream`], proving the round-4 flip (`decode.rs`'s /// `decode_inter_block` `GOLDEN_FRAME` arm) the way 120+ real aomenc /// attempts never could: frame 0 is a real key frame (this crate's own /// encoder, real texture), refreshing all 8 DPB slots as spec requires -- /// slot 3 keeps that exact picture from here on. Frame 1 is a hand-coded /// inter frame whose lone 32x32 block is *intra* (`DC_PRED`, `skip`), /// refreshing only slot 0 with a flat 128 block -- distinguishable from /// frame 0's real texture, so slot 0 (`LAST_FRAME`) and slot 3 /// (`GOLDEN_FRAME`) hold visibly different pictures once frame 2 runs. /// Frame 2 is a hand-coded inter frame naming `ref_frame_idx[3] = 3` /// (GOLDEN_FRAME's slot) and `ref_frame_idx[0] = 0` (LAST_FRAME's), whose /// lone block codes `single_ref` all the way out to `GOLDEN_FRAME` /// (`p1=0, p3=1, p5=1`) and a `skip` zero-MV `NEARESTMV`: a direct copy /// of GOLDEN's picture, so its output must equal frame 0's own /// reconstruction exactly, and must differ from frame 1's -- proof the /// decoder actually read the golden plane, not silently substituted /// `LAST_FRAME`'s. Rounds 4-9 masked and re-masked this arm chasing a /// deblock/MC residue that turned out to be film-grain synthesis /// (lane-av1golden7's `apply_grain` refusal, unrelated to GOLDEN); /// lane-av1golden8 fixed mvstack's single-ref extension pass, clearing /// the last real defect (`pinned_golden7` non_last_ref_hits 0->2), so /// this fixture flips back to its round-4 pixel-exact form. ffmpeg /// decodes the identical wire bytes as the foreign oracle /// (shared-oracle-blindness class): this crate's own decoder agreeing /// with itself would prove nothing. #[test] fn a_hand_built_golden_reference_decodes_pixel_exact_against_ffmpeg() { if !have_ffmpeg() { eprintln!( "SKIP a_hand_built_golden_reference_decodes_pixel_exact_against_ffmpeg: no ffmpeg" ); return; } use crate::cdf_state::Cdfs; use crate::decode::{intra_inter_ctx, non_last_ref_hits, q_ctx_of}; use crate::encode::{inter_frame_headers, key_frame_headers}; use crate::frame::frame_obu; use crate::msac::SymbolEncoder; use crate::mvstack::{ GOLDEN_FRAME, MiGrid, find_mv_stack, single_ref_p1_ctx, single_ref_p3_ctx, single_ref_p5_ctx, }; use crate::obu::temporal_delimiter; let (width, height) = (32usize, 32usize); let base_q_idx = 100u8; // The whole picture is one 32x32 block: `partition_ctx`/`skip_ctx`/ // `intra_inter_ctx`'s neighbour inputs are all "no neighbour yet" -- // a fresh `Neighbours`' `above_side_mi`/`left_side_mi` start at `SB` // (64), so `partition_ctx_mi`'s `side_mi * 2 <= side` (32) is false // on both axes (ctx 0), and `above_skip`/`left_skip`/`above_inter`/ // `left_inter`/`above_ref`/`left_ref` all start `false`/`-1` // (decode.rs `Neighbours::new`) -- computed here without // constructing the (module-private) `Neighbours` itself. const PARTITION_NONE: usize = 0; let ii_ctx = intra_inter_ctx(false, false, false, false); let sr_p1_ctx = single_ref_p1_ctx(None, None, None, None); let sr_p3_ctx = single_ref_p3_ctx(None, None, None, None); let sr_p5_ctx = single_ref_p5_ctx(None, None, None, None); for run in 0..4 { let picture = test_card(width, height); let key = encode_key_frame(&picture, base_q_idx, 0.5).unwrap(); let (seq, _) = key_frame_headers(width, height, base_q_idx).unwrap(); // Frame 1: hand-coded, intra-only, refreshes slot 0 with a flat // 128 block (no neighbours to predict `DC_PRED` from). let (_, header1) = inter_frame_headers(width, height, base_q_idx, 1, 0).unwrap(); let tile1 = { let mut cdfs = Cdfs::new(q_ctx_of(base_q_idx)); let mut enc = SymbolEncoder::new(); enc.symbol(PARTITION_NONE, &mut cdfs.partition_w32[0]); enc.symbol(1, &mut cdfs.skip[0]); // skip enc.symbol(0, &mut cdfs.intra_inter[ii_ctx]); // is_inter = false enc.symbol(0, &mut cdfs.y_mode[3]); // DC_PRED, size group 3 (32x32) enc.symbol(0, &mut cdfs.uv_mode_cfl[0]); // DC_PRED enc.finish() }; let mut stream = key.stream.clone(); stream.extend(temporal_delimiter()); stream.extend(frame_obu(&seq, &header1, &tile1).unwrap()); // Frame 2: hand-coded, single_ref all the way to GOLDEN_FRAME, // skip zero-MV NEARESTMV -- a direct copy of slot 3 (frame 0). let (_, mut header2) = inter_frame_headers(width, height, base_q_idx, 2, 0).unwrap(); header2.ref_frame_idx[3] = 3; // GOLDEN_FRAME's own slot: frame 0's, untouched by frame 1 let tile2 = { let mut cdfs = Cdfs::new(q_ctx_of(base_q_idx)); let mut enc = SymbolEncoder::new(); let grid = MiGrid::new(8, 8); let stack = find_mv_stack(&grid, 0, 0, 8, 8, GOLDEN_FRAME, 8, 8); enc.symbol(PARTITION_NONE, &mut cdfs.partition_w32[0]); enc.symbol(1, &mut cdfs.skip[0]); // skip enc.symbol(1, &mut cdfs.intra_inter[ii_ctx]); // is_inter = true enc.symbol(0, &mut cdfs.single_ref[sr_p1_ctx][0]); // p1: LAST/LAST2/LAST3/GOLDEN group enc.symbol(1, &mut cdfs.single_ref[sr_p3_ctx][2]); // p3: GOLDEN/LAST3 sub-group enc.symbol(1, &mut cdfs.single_ref[sr_p5_ctx][4]); // p5: GOLDEN_FRAME enc.symbol(1, &mut cdfs.new_mv[stack.new_mv_ctx]); // not_new -> not NEWMV enc.symbol(1, &mut cdfs.zero_mv[stack.zero_mv_ctx]); // not_zero -> not GLOBALMV enc.symbol(0, &mut cdfs.ref_mv[stack.ref_mv_ctx]); // nearest -> NEARESTMV, (0, 0) enc.finish() }; stream.extend(temporal_delimiter()); stream.extend(frame_obu(&seq, &header2, &tile2).unwrap()); let before = non_last_ref_hits(); let decoded = decode_stream(&stream).unwrap(); assert_eq!(decoded.len(), 3, "run {run}: expected 3 pictures"); assert!( non_last_ref_hits() > before, "run {run}: GOLDEN_FRAME never fired" ); let ffmpeg_frames = ffmpeg_decode_sequence(&stream, width, height, 3); for (i, (got, want)) in decoded.iter().zip(&ffmpeg_frames).enumerate() { assert_eq!(got.y, want.y, "run {run} frame {i}: luma vs ffmpeg"); assert_eq!(got.u, want.u, "run {run} frame {i}: U vs ffmpeg"); assert_eq!(got.v, want.v, "run {run} frame {i}: V vs ffmpeg"); } assert_eq!( decoded[0].y, key.reconstruction.y, "run {run}: frame 0 vs the encoder's own reconstruction" ); assert_eq!( decoded[2].y, key.reconstruction.y, "run {run}: GOLDEN_FRAME's block did not reproduce the keyframe it names" ); assert_ne!( decoded[2].y, decoded[1].y, "run {run}: LAST_FRAME's and GOLDEN_FRAME's slots were not actually \ distinguishable -- the gate proves nothing" ); } } /// Builds a `gradients` lavfi source string with colours derived from /// `seed`, not left to the filter's own default. /// /// Measured (lane-fixdet r1): ffmpeg's `gradients` source IGNORES its /// `seed=` parameter for colour selection -- five encodes of an /// identical command line (same `seed=`, same everything) produced five /// different fixtures, because the unset `c0..c9` stops are randomized /// from something other than that seed. Every one of this file's ~20 /// gate fixtures built a `gradients` source this way, so none of them /// had a reproducible input: a pin captured from one run could not be /// regenerated, and "attempt N failed" was as likely to be a different /// fixture as a different decode outcome. /// /// Freezing the colours outright (as the earliest four gates below do, /// hand-picking `c0=red:c1=blue:c2=green`) fixes reproducibility but /// throws away the per-attempt content variety the sweep gates need to /// make features (OBMC, warp, wedge, ...) fire at all -- this repo's own /// `sampler-decorrelated-gate` class. So: derive `c0..c3` deterministically /// FROM the seed with a plain integer hash, and keep passing `seed=` /// itself too, so a future ffmpeg that actually honours it only adds /// more variety, never removes the determinism this buys. fn gradients_source(seed: u32, width: usize, height: usize, tail: &str) -> String { fn hash_color(seed: u32, salt: u32) -> String { let h = seed .wrapping_mul(2_654_435_761) .wrapping_add(salt.wrapping_mul(0x9E37_79B9)) ^ (seed.rotate_left(13)); format!("0x{:06x}", h & 0x00ff_ffff) } let (c0, c1, c2, c3) = ( hash_color(seed, 0), hash_color(seed, 1), hash_color(seed, 2), hash_color(seed, 3), ); format!("gradients=size={width}x{height}:c0={c0}:c1={c1}:c2={c2}:c3={c3}:seed={seed}:{tail}") } /// The determinism guard: same seed twice must byte-match ffmpeg's own /// output; different seeds must not. Regresses this fix if it ever /// breaks. #[test] fn gradients_source_is_reproducible_per_seed() { if !have_ffmpeg() { eprintln!("SKIP gradients_source_is_reproducible_per_seed: no ffmpeg"); return; } fn render_hash(lavfi: &str) -> Vec { let out = Command::new("ffmpeg") .args([ "-v", "error", "-f", "lavfi", "-i", lavfi, "-frames:v", "1", "-f", "rawvideo", "-", ]) .stdin(Stdio::null()) .stdout(Stdio::piped()) .stderr(Stdio::piped()) .output() .expect("ffmpeg failed to run"); assert!(out.status.success(), "ffmpeg refused {lavfi}"); out.stdout } let a1 = render_hash(&gradients_source(42, 64, 64, "duration=0.04:rate=25")); let a2 = render_hash(&gradients_source(43, 64, 64, "duration=0.04:rate=25")); let a3 = render_hash(&gradients_source(42, 64, 64, "duration=0.04:rate=25")); assert_eq!(a1, a3, "same seed must reproduce byte-identical output"); assert_ne!(a1, a2, "different seeds must not collide"); } /// Encodes a real libaom-av1 stream (via ffmpeg's own encoder, not this /// crate's) with the same reduced-partition flags lane-av1adst's r6 /// probe found still select `ADST_ADST`/`ADST_DCT`/`DCT_ADST` on /// gradient content (TX_SET_INTRA_2), and decodes it with /// [`decode_stream`] -- lifting the round-2 refusal is only real if it /// gets past an encoder this crate never wrote a byte of. fn libaom_encode(lavfi: &str, width: usize, height: usize, crf: u32) -> Option> { libaom_encode_with(lavfi, width, height, crf, &[]) } /// As [`libaom_encode`], with extra encoder args appended -- used to pin /// a deterministic stream (`-threads 1` etc., since libaom's `cpu-used 8` /// RD search is not otherwise stable run-to-run on this box, and lavfi /// `gradients` needs its own `seed=` for the same reason) and to gate /// individual feature flags (`-enable-cdef 0`) a given fixture needs off. fn libaom_encode_with( lavfi: &str, width: usize, height: usize, crf: u32, extra: &[&str], ) -> Option> { let mut args = vec![ "-v", "error", "-f", "lavfi", "-i", lavfi, "-frames:v", "1", "-pix_fmt", "yuv420p", "-c:v", "libaom-av1", ]; let crf_str = crf.to_string(); args.extend(["-crf", &crf_str]); args.extend([ "-cpu-used", "8", "-enable-rect-partitions", "0", "-enable-ab-partitions", "0", "-enable-1to4-partitions", "0", "-enable-angle-delta", "0", "-enable-cfl-intra", "0", ]); args.extend(extra.iter().copied()); args.extend(["-f", "obu", "-"]); let out = Command::new("ffmpeg") .args(&args) .stdin(Stdio::null()) .stdout(Stdio::piped()) .stderr(Stdio::piped()) .output() .expect("ffmpeg failed to run"); if !out.status.success() { eprintln!( "ffmpeg refused to encode {lavfi} at {width}x{height}: {}", String::from_utf8_lossy(&out.stderr) ); return None; } Some(out.stdout) } /// The end-to-end gate: two real libaom-av1 configs, decoded with /// [`decode_stream`] and checked against ffmpeg's own decode of the same /// bytes. Not every config this encoder writes decodes yet (partitions /// below 16x16, AB partitions, CfL and others remain round-2 gaps per the /// r6 catalogue) -- both configs here pick `cpu-used 8` plus the /// partition/angle-delta/CfL flags r6 found narrow the encoder onto this /// decoder's supported syntax, while still landing on non-`DCT_DCT` tx /// types on gradient content. #[test] fn a_real_libaom_stream_with_adst_decodes_end_to_end() { if !have_ffmpeg() { eprintln!("SKIP a_real_libaom_stream_with_adst_decodes_end_to_end: no ffmpeg"); return; } let configs: [(String, usize, usize, u32); 2] = [ ("testsrc2=size=64x64:rate=1".to_string(), 64, 64, 15), (gradients_source(42, 64, 64, "rate=1"), 64, 64, 15), ]; let mut verdicts = Vec::new(); for (lavfi, width, height, crf) in configs { let lavfi = lavfi.as_str(); let Some(stream) = libaom_encode(lavfi, width, height, crf) else { verdicts.push(format!("{lavfi}: ffmpeg itself refused to encode")); continue; }; match decode_stream(&stream) { Ok(frames) => { let ffmpeg_frames = ffmpeg_decode_sequence(&stream, width, height, 1); let matches = frames[0].y == ffmpeg_frames[0].y && frames[0].u == ffmpeg_frames[0].u && frames[0].v == ffmpeg_frames[0].v; verdicts.push(format!( "{lavfi}: decoded, {} ffmpeg's own decode", if matches { "MATCHES" } else { "MISMATCHES" } )); // Not a hard `assert!`: libaom's own mode decisions at // `cpu-used 8` are not observed stable run-to-run on this // box (multi-threaded RD search), so a run that happens // to land on a still-unsupported syntax element (e.g. a // partition below 16x16, a separate round-2 gap) can // decode without erroring yet disagree -- that failure // mode belongs to those other gaps, not to this round's // ADST wiring, and the eprintln below reports it either // way rather than hiding it. } Err(e) => { verdicts.push(format!("{lavfi}: still refuses ({e})")); } } } eprintln!("libaom ADST end-to-end verdicts:"); for v in &verdicts { eprintln!(" {v}"); } } /// Hardened past the report-only test above: a real libaom-av1 gradients /// stream, pinned deterministic (`gradients`' own `seed=`, plus /// `-threads 1`/`-row-mt 0`/no tiling so libaom's `cpu-used 8` RD search /// cannot pick a different mode set run to run) and with CDEF turned off /// (this decoder's tile path never applies that in-loop filter -- see /// `cdef_is_active` in `decode_stream` -- so a stream that leaves it on /// decodes-but-wrong instead of refusing at cdef_bits==0; ffmpeg's /// `-enable-cdef 0` keeps this fixture out of that gap rather than this /// test working around it). Asserts pixel-exact against ffmpeg's own /// decode of the identical bytes, not just "decoded without error". #[test] fn a_real_libaom_gradients_stream_decodes_pixel_exact() { if !have_ffmpeg() { eprintln!("SKIP a_real_libaom_gradients_stream_decodes_pixel_exact: no ffmpeg"); return; } let (width, height) = (64, 64); let stream = libaom_encode_with( &gradients_source(42, width, height, "rate=1"), width, height, 15, &[ "-threads", "1", "-row-mt", "0", "-tile-columns", "0", "-tile-rows", "0", "-enable-cdef", "0", ], ) .expect("ffmpeg encode"); let frames = decode_stream(&stream).expect("decode_stream on a real libaom stream"); let ffmpeg_frames = ffmpeg_decode_sequence(&stream, width, height, 1); assert_eq!(frames[0].y, ffmpeg_frames[0].y, "luma vs ffmpeg"); assert_eq!(frames[0].u, ffmpeg_frames[0].u, "U vs ffmpeg"); assert_eq!(frames[0].v, ffmpeg_frames[0].v, "V vs ffmpeg"); } /// As [`a_real_libaom_gradients_stream_decodes_pixel_exact`], but at /// 32x32 (below `is_cfl_allowed`'s 32x32 luma bound as a *single* /// undivided block, spec 5.11.5) rather than 64x64 -- a whole 64x64 SB /// is never CFL-eligible itself, only its <=32x32 partitions, and /// `testsrc2`/`gradients` at 64x64 with rect/ab/1to4/angle-delta search /// disabled reliably pick a whole-SB `PARTITION_NONE` (probed /// lane-av1real r2), so no fixture at that size exercises CfL at all. /// A 32x32 frame is exactly the whole-block, CFL-allowed case, and /// lane-av1real r2 found `gradients` there reliably selects /// `UV_CFL_PRED` (`uv_mode=13`) across a crf sweep -- `testsrc2` there /// stays DC. Proves the CfL port this round added, not just DC-chroma /// decode: pixel-exact against ffmpeg's own decode, not just "decoded". /// #[test] fn a_real_libaom_cfl_stream_decodes_pixel_exact() { if !have_ffmpeg() { eprintln!("SKIP a_real_libaom_cfl_stream_decodes_pixel_exact: no ffmpeg"); return; } let (width, height) = (32, 32); let stream = libaom_encode_with( &gradients_source(42, width, height, "rate=1"), width, height, 15, &[ "-threads", "1", "-row-mt", "0", "-tile-columns", "0", "-tile-rows", "0", "-enable-cdef", "0", ], ) .expect("ffmpeg encode"); let frames = decode_stream(&stream).expect("decode_stream on a real libaom stream"); let ffmpeg_frames = ffmpeg_decode_sequence(&stream, width, height, 1); assert_eq!(frames[0].y, ffmpeg_frames[0].y, "luma vs ffmpeg"); assert_eq!(frames[0].u, ffmpeg_frames[0].u, "U vs ffmpeg"); assert_eq!(frames[0].v, ffmpeg_frames[0].v, "V vs ffmpeg"); } /// As [`a_real_libaom_gradients_stream_decodes_pixel_exact`], but leaves /// CDEF *on* (no `-enable-cdef 0`) -- the whole point of `lane-av1cdef`: /// proves `apply_cdef` itself, not just that this decoder still works /// when the filter never fires. Gradient content at a low crf reliably /// gives libaom something to dering. If a run happens to land on /// `cdef_bits > 0` (per-64x64 `cdef_idx`, still unimplemented -- see the /// refusal in `decode_stream`) the stream is skipped rather than failed: /// that gap is named by the refusal's own error text, not silently /// swallowed here. #[test] fn a_real_libaom_gradients_stream_with_cdef_decodes_pixel_exact() { if !have_ffmpeg() { eprintln!( "SKIP a_real_libaom_gradients_stream_with_cdef_decodes_pixel_exact: no ffmpeg" ); return; } let (width, height) = (64, 64); let stream = libaom_encode_with( &gradients_source(42, width, height, "rate=1"), width, height, 30, &[ "-threads", "1", "-row-mt", "0", "-tile-columns", "0", "-tile-rows", "0", ], ) .expect("ffmpeg encode"); match decode_stream(&stream) { Ok(frames) => { let ffmpeg_frames = ffmpeg_decode_sequence(&stream, width, height, 1); assert_eq!(frames[0].y, ffmpeg_frames[0].y, "luma vs ffmpeg (CDEF on)"); assert_eq!(frames[0].u, ffmpeg_frames[0].u, "U vs ffmpeg (CDEF on)"); assert_eq!(frames[0].v, ffmpeg_frames[0].v, "V vs ffmpeg (CDEF on)"); } Err(e) => { eprintln!("SKIP a_real_libaom_gradients_stream_with_cdef_decodes_pixel_exact: {e}"); } } } /// Path to a real `aomenc` build carrying the reference options this /// fixture needs (`--max-partition-size`, `--enable-tx-size-search`, /// `--loopfilter-control`) -- this box's `ffmpeg` links a libaom too old /// to expose `enable-tx-size-search`/`loopfilter-control` through its /// own `-aom-params` passthrough (probed live: "Cannot find aom /// option"), so [`a_real_aomenc_filter_intra_stream_decodes_pixel_exact`] /// shells out to the standalone binary instead of ffmpeg's libaom-av1 /// wrapper every other test here uses. `EC_AV1_AOMENC` overrides the /// default dev-build path; the test skips (not fails) when neither this /// nor `ffmpeg` is present. fn aomenc_path() -> std::path::PathBuf { if let Some(p) = std::env::var_os("EC_AV1_AOMENC") { return std::path::PathBuf::from(p); } // The oracle used to live only under `/tmp`, which this box's tmpfs // guard reclaims -- when it did, all 20 aomenc gates below went // silently vacuous (SKIP, suite still green). The durable location // is provisioned by `scripts/build-aom-oracle.sh`; the `/tmp` path // stays last as a legacy fallback. let mut candidates = Vec::new(); if let Some(home) = std::env::var_os("HOME") { candidates.push(std::path::PathBuf::from(&home).join(".cache/aom-oracle/build/aomenc")); } candidates.push("/tmp/libaom-src/build/encoder/aomenc".into()); candidates .iter() .find(|p| p.is_file()) .cloned() .unwrap_or_else(|| candidates.remove(0)) } /// Whether the aomenc oracle is present. Absence normally SKIPs (a /// checkout without the oracle should still run the rest of the suite), /// but `EC_AV1_REQUIRE_AOMENC=1` turns it into a hard failure so a batch /// run cannot report green off 20 skipped gates. /// Where a pinned stream lives. `fixtures/` is a symlink to a durable /// directory outside the repo, so a pin captured in one session is still /// there in the next; a session scratchpad is not (tmpfs reaps it, and /// four pins were silently dead because of that). fn pin_dir() -> std::path::PathBuf { if let Some(p) = std::env::var_os("EC_AV1_PIN_DIR") { return std::path::PathBuf::from(p); } std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")) .join("../../fixtures") .canonicalize() .unwrap_or_else(|_| std::path::PathBuf::from("fixtures")) } fn have_aomenc() -> bool { let present = aomenc_path().is_file(); assert!( present || std::env::var_os("EC_AV1_REQUIRE_AOMENC").is_none(), "EC_AV1_REQUIRE_AOMENC is set but no aomenc at {} -- run scripts/build-aom-oracle.sh", aomenc_path().display() ); present } /// A real `aomenc` filter-intra fixture: every other intra mode disabled /// (smooth/paeth/directional/angle-delta) and /// `--max-partition-size=32` (past which `av1_filter_intra_allowed_bsize`'s /// <=32x32 bound means `use_filter_intra` never reads at all, spec /// 5.11.14), so the only way this frame codes is `DC_PRED` with filter /// intra on top -- `--loopfilter-control=0` is required too: this /// decoder has no in-loop deblocking filter at all, and libaom's default /// (on) smooths every block-boundary column, which first looked like a /// filter-intra math bug (mismatches straddling the 32-pixel block /// seam) until the recipe without it round-tripped byte-exact. Checks /// [`decode::filter_intra_hits`] actually moved (a process-global /// counter, so before/after rather than an absolute value) to prove the /// symbol fired, then pixel-exactness against ffmpeg's own decode of the /// identical bytes on every plane. #[test] fn a_real_aomenc_filter_intra_stream_decodes_pixel_exact() { if !have_ffmpeg() { eprintln!("SKIP a_real_aomenc_filter_intra_stream_decodes_pixel_exact: no ffmpeg"); return; } if !have_aomenc() { eprintln!( "SKIP a_real_aomenc_filter_intra_stream_decodes_pixel_exact: no aomenc at {}", aomenc_path().display() ); return; } let (width, height) = (64usize, 64usize); // Recipe-search hooks: with the encoder pinned to one thread the // stream is a deterministic function of (seed, cq), so a firing recipe // can be searched for without a rebuild. let fi_seed: u32 = std::env::var("EC_FI_SEED") .ok() .and_then(|v| v.parse().ok()) .unwrap_or(42); let fi_source = gradients_source(fi_seed, 64, 64, "duration=0.04:rate=25"); let fi_cq = std::env::var("EC_FI_CQ").unwrap_or_else(|_| "40".to_string()); let fi_cq_arg = format!("--cq-level={fi_cq}"); let y4m = Command::new("ffmpeg") .args([ "-v", "error", "-f", "lavfi", "-i", &fi_source, "-pix_fmt", "yuv420p", "-f", "yuv4mpegpipe", "-", ]) .stdin(Stdio::null()) .stdout(Stdio::piped()) .stderr(Stdio::piped()) .output() .expect("ffmpeg failed to run"); assert!( y4m.status.success(), "ffmpeg refused to generate the y4m fixture: {}", String::from_utf8_lossy(&y4m.stderr) ); let mut child = Command::new(aomenc_path()) .args([ "--codec=av1", "--passes=1", "--end-usage=q", &fi_cq_arg, "--cpu-used=0", // Every sibling gate pins these; this one did not, and a // multi-threaded aomenc is not deterministic, so each run // encoded a slightly different stream and the gate flaked // against whichever features that run happened to pick. // Pinning them makes seed 42 mean one stream (class // `parallel-flake-is-attempt-selection`). "--threads=1", "--row-mt=0", "--enable-filter-intra=1", "--enable-smooth-intra=0", "--enable-paeth-intra=0", "--enable-directional-intra=0", "--enable-angle-delta=0", "--enable-tx-size-search=0", "--enable-cdef=0", "--max-partition-size=32", "--loopfilter-control=0", "--enable-palette=0", "--enable-intrabc=0", "--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 the y4m fixture 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::filter_intra_hits(); // `screen_content_tools_determination` (libaom encoder_utils.c) is a // real internal trial-encode/PSNR comparison, not a flag this CLI // exposes a knob for -- on this flat gradient fixture it lands on // `allow_screen_content_tools=0` most runs but occasionally trips // over its 0.9 dB threshold the other way even with palette/intrabc // both disabled. That is a genuine separate gap (`decode_stream` // never reads `intrabc`/`palette_mode_info`), not a filter-intra // regression, so it is skipped here exactly as // `a_real_libaom_gradients_stream_with_cdef_decodes_pixel_exact` // above skips its own still-unsupported-gap runs. let frames = match decode_stream(&stream) { Ok(frames) => frames, Err(e) => { eprintln!("SKIP a_real_aomenc_filter_intra_stream_decodes_pixel_exact: {e}"); return; } }; assert!( decode::filter_intra_hits() > before, "use_filter_intra never fired decoding this stream" ); let ffmpeg_frames = ffmpeg_decode_sequence(&stream, width, height, 1); assert_eq!(frames[0].y, ffmpeg_frames[0].y, "luma vs ffmpeg"); assert_eq!(frames[0].u, ffmpeg_frames[0].u, "U vs ffmpeg"); assert_eq!(frames[0].v, ffmpeg_frames[0].v, "V vs ffmpeg"); } /// As [`a_real_aomenc_filter_intra_stream_decodes_pixel_exact`]'s /// harness, but with the deblocking filter left *on* (no /// `--loopfilter-control=0`) at a crf high enough that libaom actually /// picks a nonzero level -- proves `apply_deblock` itself, not just that /// this decoder still works when every edge lands on level 0. CDEF /// stays off so a pixel mismatch can only come from the loop filter. #[test] fn a_real_aomenc_intra_stream_with_deblocking_decodes_pixel_exact() { if !have_ffmpeg() { eprintln!( "SKIP a_real_aomenc_intra_stream_with_deblocking_decodes_pixel_exact: no ffmpeg" ); return; } if !have_aomenc() { eprintln!( "SKIP a_real_aomenc_intra_stream_with_deblocking_decodes_pixel_exact: no aomenc at {}", aomenc_path().display() ); return; } let (width, height) = (64usize, 64usize); let source = gradients_source(42, width, height, "duration=0.04:rate=25"); let y4m = Command::new("ffmpeg") .args(["-v", "error", "-f", "lavfi", "-i"]) .arg(&source) .args(["-pix_fmt", "yuv420p", "-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=55", "--cpu-used=0", "--enable-filter-intra=0", "--enable-smooth-intra=0", "--enable-paeth-intra=0", "--enable-directional-intra=0", "--enable-angle-delta=0", "--enable-tx-size-search=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=0", // use_ref_frame_mvs (temporal MV projection) is unimplemented in mvstack; // leaving it on silently desyncs symbols on inter frames (grainfix lesson). "--enable-ref-frame-mvs=0", "--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::deblock_hits(); let frames = match decode_stream(&stream) { Ok(frames) => frames, Err(e) => { eprintln!( "SKIP a_real_aomenc_intra_stream_with_deblocking_decodes_pixel_exact: {e}" ); return; } }; assert!( decode::deblock_hits() > before, "no deblocking edge fired decoding this stream -- raise cq-level" ); let ffmpeg_frames = ffmpeg_decode_sequence(&stream, width, height, 1); assert_eq!(frames[0].y, ffmpeg_frames[0].y, "luma vs ffmpeg"); assert_eq!(frames[0].u, ffmpeg_frames[0].u, "U vs ffmpeg"); 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. // start_scale 5.0 is the proven first try (decodes pixel-exact // AND reads the seven-symbol set-1 tx_type rows on TX8 TUs); // later attempts walk down for variety when aomenc's RD flips // elsewhere. let source = format!( "mandelbrot=size=64x64:start_scale={}:rate=25", 5.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", // Every non-DC intra tool off: directional chroma and // angle deltas are separate round-2 gaps, and DC-only // residual is exactly what gives the RD search a reason // to split a transform. "--enable-smooth-intra=0", "--enable-paeth-intra=0", "--enable-directional-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", // 16x16 blocks are what split into the TX8 TUs whose // set-1 tx_type rows this gate exists to prove; some // scales resolve depth 2 (TX4, a named refusal) and the // attempt loop just moves on. "--min-partition-size=16", "--enable-palette=0", "--enable-intrabc=0", "--enable-cfl-intra=0", // use_ref_frame_mvs (temporal MV projection) is unimplemented in mvstack; // leaving it on silently desyncs symbols on inter frames (grainfix lesson). "--enable-ref-frame-mvs=0", "--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") ); } /// The directional-chroma/angle-delta gate: a real aomenc key frame with /// `--enable-directional-intra`/`--enable-angle-delta` left ON (the /// opposite of every sibling recipe's `=0`, which existed specifically to /// keep this round-2 gap from firing while a different surface was under /// test). A colourful, sharp-edged source gives the RD search a reason to /// pick a directional `uv_mode` and/or a nonzero angle delta -- some /// seeds still won't, so retry like every other real-encoder gate here, /// but the first attempt that actually fires either counter must decode /// pixel-exact. #[test] fn a_real_aomenc_stream_with_directional_chroma_decodes_pixel_exact() { if !have_ffmpeg() { eprintln!( "SKIP a_real_aomenc_stream_with_directional_chroma_decodes_pixel_exact: no ffmpeg" ); return; } if !have_aomenc() { eprintln!( "SKIP a_real_aomenc_stream_with_directional_chroma_decodes_pixel_exact: no aomenc at {}", aomenc_path().display() ); return; } let (width, height) = (64usize, 64usize); let mut refusals = Vec::new(); let mut fired_runs = 0u32; for attempt in 0..40u32 { let seed = 42 + attempt; let source = format!( "mandelbrot=size=64x64:start_scale={}:rate=25", 5.0 - f64::from(attempt) * 0.06 ); let y4m = Command::new("ffmpeg") .args([ "-v", "error", "-f", "lavfi", "-i", &source, "-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", // Smooth/Paeth chroma is a separate, still-refused round-2 // gap (an unrelated `Edges::build` no-neighbour bug this // lane does not cover) -- off so the RD search's only // non-DC/CFL chroma choice left is directional. "--enable-smooth-intra=0", "--enable-paeth-intra=0", "--enable-tx-size-search=0", "--enable-cdef=0", "--enable-restoration=0", // The intra edge filter/upsample (spec 7.11.2.7-2.9) is // now implemented (`directional()`) -- left ON (aomenc's // own default) so this gate actually exercises it instead // of the un-filtered fast path a disabled flag would keep // testing. "--enable-intra-edge-filter=1", "--max-partition-size=32", // Same reason as the tx-select gate above: 8x8 and other // sub-16x16 splits are a separate, already-named refusal. "--min-partition-size=16", "--enable-palette=0", "--enable-intrabc=0", "--enable-cfl-intra=0", // use_ref_frame_mvs (temporal MV projection) is unimplemented in mvstack; // leaving it on silently desyncs symbols on inter frames (grainfix lesson). "--enable-ref-frame-mvs=0", "--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 (uv_before, angle_before) = (decode::directional_uv_hits(), decode::angle_delta_hits()); let frames = match decode_stream(&stream) { Ok(frames) => frames, Err(e) => { refusals.push(format!("seed {seed}: {e}")); continue; } }; if decode::directional_uv_hits() == uv_before && decode::angle_delta_hits() == angle_before { refusals.push(format!( "seed {seed}: decoded, but neither a directional uv_mode nor a \ nonzero angle delta fired" )); continue; } fired_runs += 1; 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})"); if fired_runs >= 4 { return; } } assert!( fired_runs > 0, "every attempt hit a named refusal or never exercised directional chroma:\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 /// decoder's inter path does not model), `--kf-max-dist` past the clip's /// own length keeps frame 0 the only key frame. #[test] fn a_real_aomenc_inter_sequence_with_deblocking_decodes_pixel_exact() { if !have_ffmpeg() { eprintln!( "SKIP a_real_aomenc_inter_sequence_with_deblocking_decodes_pixel_exact: no ffmpeg" ); return; } if !have_aomenc() { eprintln!( "SKIP a_real_aomenc_inter_sequence_with_deblocking_decodes_pixel_exact: no aomenc at {}", aomenc_path().display() ); return; } let (width, height, frame_count) = (64usize, 64usize, 4usize); let source = gradients_source(42, width, height, "duration=0.16:rate=25"); let y4m = Command::new("ffmpeg") .args(["-v", "error", "-f", "lavfi", "-i"]) .arg(&source) .args([ "-pix_fmt", "yuv420p", "-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=55", "--cpu-used=0", "--lag-in-frames=0", "--auto-alt-ref=0", "--kf-max-dist=1000", // Forces `primary_ref_frame = PRIMARY_REF_NONE` on every // frame (spec 5.9.2): without it a real encoder's inter // frame loads its initial CDF state from the previous // frame's *adapted* tables (spec 7.20's `load_cdfs`), which // this decoder never does -- it always starts each frame's // `Cdfs` from the spec defaults (`decode.rs`'s // `Cdfs::new(q_ctx)`, called fresh per frame in // `decode_stream`). Without this flag the inter frame's // very first symbol desyncs immediately (traced 2026-08-27: // no coefficient trace at all before the tile's first // partition read comes back an out-of-alphabet value). A // real decoder needs cross-frame CDF forwarding to handle // that stream; this fixture sidesteps it the same way a // real broadcast/low-latency encode legitimately can. "--error-resilient=1", "--enable-rect-partitions=0", "--enable-ab-partitions=0", "--enable-1to4-partitions=0", "--enable-filter-intra=0", "--enable-smooth-intra=0", "--enable-paeth-intra=0", "--enable-directional-intra=0", "--enable-angle-delta=0", "--enable-tx-size-search=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=0", // use_ref_frame_mvs (temporal MV projection) is unimplemented in mvstack; // leaving it on silently desyncs symbols on inter frames (grainfix lesson). "--enable-ref-frame-mvs=0", "--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; if let Ok(dump) = std::env::var("EC_AV1_GATE_DUMP") { std::fs::write(dump, &stream).expect("dump stream"); } let before = decode::deblock_hits(); let frames = match decode_stream(&stream) { Ok(frames) => frames, Err(e) => { eprintln!( "SKIP a_real_aomenc_inter_sequence_with_deblocking_decodes_pixel_exact: {e}" ); return; } }; assert!( decode::deblock_hits() > before, "no deblocking edge fired decoding this sequence -- raise cq-level" ); let ffmpeg_frames = ffmpeg_decode_sequence(&stream, width, height, frame_count); assert_eq!(frames.len(), frame_count); for (i, (got, want)) in frames.iter().zip(&ffmpeg_frames).enumerate() { // gradients' colors ignore its seed, so this gate encodes fresh // content every run -- a mismatch here may never reproduce. // Self-pin the stream before panicking so the failure is // bisectable (replay: EC_AV1_PIN on scratch_isolate_pinned_mismatch). let ok = got.y == want.y && got.u == want.u && got.v == want.v; if !ok { let pin = std::env::temp_dir().join("ec-av1-deblocking-gate-fail.obu"); let _ = std::fs::write(&pin, &stream); panic!("frame {i} mismatch vs ffmpeg -- stream pinned at {}", pin.display()); } } } /// The decisive cross-frame CDF forwarding test: identical to /// [`a_real_aomenc_inter_sequence_with_deblocking_decodes_pixel_exact`] /// except it drops `--error-resilient=1`, so aomenc codes every inter /// frame's `primary_ref_frame` at its default (not `PRIMARY_REF_NONE`) /// and expects the decoder to resume from the previous frame's *adapted* /// CDF state (spec 7.20 `load_cdfs`). Without cross-frame forwarding, /// frame 1's very first tile symbol desyncs (an out-of-alphabet /// partition read); with it, every frame decodes pixel-exact. #[test] fn a_real_aomenc_inter_sequence_with_cdf_forwarding_decodes_pixel_exact() { if !have_ffmpeg() { eprintln!( "SKIP a_real_aomenc_inter_sequence_with_cdf_forwarding_decodes_pixel_exact: no ffmpeg" ); return; } if !have_aomenc() { eprintln!( "SKIP a_real_aomenc_inter_sequence_with_cdf_forwarding_decodes_pixel_exact: no aomenc at {}", aomenc_path().display() ); return; } let (width, height, frame_count) = (64usize, 64usize, 4usize); // The fixture's content varies run to run (lavfi gradients ignores its // seed option for the c0..c7 colors -- they default to "random"), so: // most encodes of this recipe trip one of the decoder's NAMED round-2 // refusals (a reference other than LAST_FRAME, a sub-16 partition -- // genuine coverage gaps, separately documented), and only some produce // a stream fully inside this decoder's declared support. So: attempt // several seeds, and the first stream that DECODES must be pixel-exact // -- that assertion is what guards the forwarded-CDF state (a wrong // table decodes plausible-but-wrong pixels; the counter-reset bug this // gate caught did exactly that). Exhausting every attempt on named // refusals skips loudly, listing them. let mut refusals = Vec::new(); for attempt in 0..8u32 { let seed = 42 + attempt; let source = gradients_source(seed, 64, 64, "duration=0.16:rate=25"); let y4m = Command::new("ffmpeg") .args([ "-v", "error", "-f", "lavfi", "-i", &source, "-pix_fmt", "yuv420p", "-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) ); // lane-cdfflake: hard length check on the y4m fixture itself -- // a truncated/racing ffmpeg write would otherwise surface as a // downstream pixel mismatch indistinguishable from a real decode // defect. y4m frames are `FRAME\n` (6 bytes) + raw 4:2:0 payload; // find the header/frame-data boundary at the first `FRAME` marker // rather than parsing the header fields (duration/rate can move // the header text length without moving the frame math). let marker_at = y4m .stdout .windows(5) .position(|w| w == b"FRAME") .expect("y4m stream missing FRAME marker"); let frame_420_bytes = width * height * 3 / 2; assert_eq!( y4m.stdout.len(), marker_at + frame_count * (6 + frame_420_bytes), "y4m fixture length mismatch (seed {seed})" ); let y4m_hash = fnv1a64(&y4m.stdout); let mut child = Command::new(aomenc_path()) .args([ "--codec=av1", "--passes=1", "--end-usage=q", "--cq-level=55", "--cpu-used=0", "--lag-in-frames=0", "--auto-alt-ref=0", "--kf-max-dist=1000", // `--threads=1` (not the default): a multithreaded RD search // makes aomenc's screen-content-tools heuristic race (seen // flaky under `cargo test`'s own parallelism -- the same // fixture, same seed, occasionally sets // `allow_screen_content_tools`, a round-2 gap unrelated to // CDF forwarding), so pin it to one thread for a // deterministic header. "--threads=1", "--row-mt=0", // `--enable-order-hint=0` kills BWDREF/ALTREF entirely (no // order hints -> no bidirectional distance weighting) and, // combined with `--auto-alt-ref=0`/`--lag-in-frames=0` // above, leaves aomenc's RD search only LAST_FRAME/ // LAST2_FRAME/LAST3_FRAME/GOLDEN_FRAME to pick from -- // GOLDEN itself needs no order hint (it is the keyframe's // fixed slot, not a distance-weighted reference), so this // does not suppress GOLDEN selection the way the sibling // CDF-forwarding gate's own comment worried about; it just // narrows the pool this gate needs GOLDEN to win out of. "--enable-order-hint=0", "--enable-warped-motion=0", "--enable-obmc=0", "--enable-masked-comp=0", "--enable-interintra-comp=0", "--enable-dist-wtd-comp=0", "--enable-diff-wtd-comp=0", "--enable-onesided-comp=0", "--enable-interintra-wedge=0", "--enable-smooth-interintra=0", "--enable-rect-partitions=0", "--enable-ab-partitions=0", "--enable-1to4-partitions=0", "--enable-filter-intra=0", "--enable-smooth-intra=0", "--enable-paeth-intra=0", "--enable-directional-intra=0", "--enable-angle-delta=0", "--enable-tx-size-search=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=0", // use_ref_frame_mvs (temporal MV projection) is unimplemented in mvstack; // leaving it on silently desyncs symbols on inter frames (grainfix lesson). "--enable-ref-frame-mvs=0", "--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 stream_hash = fnv1a64(&stream); // A named `unsupported` refusal is a documented coverage gap (each // one carries its own test debt elsewhere), not a forwarding verdict // -- try the next seed. Any stream that DECODES must be pixel-exact, // and any non-refusal error is a hard failure. let frames = match decode_stream(&stream) { Err(e) => { let msg = e.to_string(); assert!( msg.contains("unsupported"), "cross-frame CDF forwarding failed outright (seed {seed}): {msg}" ); refusals.push(format!("seed {seed}: {msg}")); continue; } Ok(frames) => frames, }; let ffmpeg_frames = ffmpeg_decode_sequence(&stream, width, height, frame_count); assert_eq!(frames.len(), frame_count); // lane-cdfflake: reconstruct ffmpeg's raw reference bytes (the // three per-frame slices are contiguous chunks of the raw // `-f rawvideo` output ffmpeg_decode_sequence sliced, so // concatenating them back is bit-for-bit the same bytes ffmpeg // actually produced -- no second ffmpeg invocation needed). let ffmpeg_raw: Vec = ffmpeg_frames .iter() .flat_map(|p| p.y.iter().chain(&p.u).chain(&p.v).copied()) .collect(); let ffmpeg_raw_hash = fnv1a64(&ffmpeg_raw); eprintln!( "cdfflake attempt seed={seed}: y4m_hash={y4m_hash:016x} \ stream_hash={stream_hash:016x} ffmpeg_raw_hash={ffmpeg_raw_hash:016x}" ); // lane-av1golden3: pin the exact raw stream on the first mismatch // so a flaky-looking gate failure becomes a deterministic, static // fixture to bisect against -- env-gated, no cost on a normal run. let mismatched = frames .iter() .zip(&ffmpeg_frames) .any(|(got, want)| got.y != want.y || got.u != want.u || got.v != want.v); if mismatched { if let Ok(path) = std::env::var("EC_AV1_GATE_DUMP") { std::fs::write(&path, &stream).expect("writing pinned stream"); eprintln!("EC_AV1_GATE_DUMP: wrote mismatching stream (seed {seed}) to {path}"); } // lane-cdfflake: unconditional pin on mismatch -- the aomenc // stream AND ffmpeg's raw reference bytes, so a flake caught // under load names the liar (stream bytes differ from a // clean rerun -> subprocess instability; reference bytes // differ -> ffmpeg instability; both stable -> real decode // defect) without relying on the env var being set. // The pin goes to `fixtures/`, which outlives the session: an // earlier version wrote to a session scratchpad, so once tmpfs // reaped it a real mismatch aborted on the write instead of // reporting itself. A failed write must not mask the assert // below either, so it warns rather than panicking. let sp = pin_dir(); let stream_pin = sp.join(format!("cdfflake-stream-seed{seed}.bin")); let ref_pin = sp.join(format!("cdfflake-ffmpeg-raw-seed{seed}.bin")); for (path, bytes) in [(&stream_pin, &stream), (&ref_pin, &ffmpeg_raw)] { if let Err(e) = std::fs::write(path, bytes) { eprintln!("cdfflake: could not pin to {}: {e}", path.display()); } } eprintln!( "cdfflake MISMATCH seed={seed}: pinned stream + ffmpeg reference to {}", sp.display() ); } for (i, (got, want)) in frames.iter().zip(&ffmpeg_frames).enumerate() { assert_eq!(got.y, want.y, "frame {i} luma vs ffmpeg (seed {seed})"); assert_eq!(got.u, want.u, "frame {i} U vs ffmpeg (seed {seed})"); assert_eq!(got.v, want.v, "frame {i} V vs ffmpeg (seed {seed})"); } return; } eprintln!( "SKIP a_real_aomenc_inter_sequence_with_cdf_forwarding_decodes_pixel_exact: every \ attempt hit a named refusal:\n{}", refusals.join("\n") ); } /// lane-av1golden3: reproduces the pinned mismatch bytes captured by /// `EC_AV1_GATE_DUMP=$SP/golden3-pin.obu` off /// [`a_real_aomenc_inter_sequence_with_cdf_forwarding_decodes_pixel_exact`] /// (seed 47, frame 1 U). Deterministic and static -- no aomenc/ffmpeg /// re-encode involved, only re-decodes the fixed bytes on disk, so it is /// `#[ignore]`d (the file only exists on this machine's scratchpad) but /// gives a fast red/green loop for the actual fix. #[test] #[ignore = "reads a pinned fixture path outside the repo; run manually"] fn pinned_golden3_stream_decodes_pixel_exact() { let path = std::env::var("EC_AV1_GATE_DUMP_PIN") .map(std::path::PathBuf::from) .unwrap_or_else(|_| pin_dir().join("golden3-pin.obu")); let Ok(stream) = std::fs::read(&path) else { eprintln!( "SKIP pinned_golden3_stream_decodes_pixel_exact: no pinned bytes at {} \ -- re-capture with EC_AV1_GATE_DUMP off the cdf-forwarding gate", path.display() ); return; }; if !have_ffmpeg() { eprintln!("SKIP pinned_golden3_stream_decodes_pixel_exact: no ffmpeg"); return; } let frames = decode_stream(&stream).expect("pinned stream must decode"); let ffmpeg_frames = ffmpeg_decode_sequence(&stream, 64, 64, 4); for (i, (got, want)) in frames.iter().zip(&ffmpeg_frames).enumerate() { assert_eq!(got.y, want.y, "frame {i} luma vs ffmpeg (pinned)"); assert_eq!(got.u, want.u, "frame {i} U vs ffmpeg (pinned)"); assert_eq!(got.v, want.v, "frame {i} V vs ffmpeg (pinned)"); } } /// lane-av1golden4: reproduces the pinned mismatch bytes captured by /// `EC_AV1_GATE_DUMP=$SP/golden4-pin.obu` off /// [`a_real_aomenc_inter_sequence_with_cdf_forwarding_decodes_pixel_exact`] /// (seed 43, frame 3 luma) the round the GOLDEN mask lifted. Deterministic /// and static -- `#[ignore]`d, run manually. #[test] #[ignore = "reads a pinned fixture path outside the repo; run manually"] fn pinned_golden4_stream_decodes_pixel_exact() { use crate::decode::non_last_ref_hits; let path = std::env::var("EC_AV1_GATE_DUMP_PIN") .map(std::path::PathBuf::from) .unwrap_or_else(|_| pin_dir().join("golden4-pin.obu")); let Ok(stream) = std::fs::read(&path) else { eprintln!( "SKIP pinned_golden4_stream_decodes_pixel_exact: no pinned bytes at {} \ -- re-capture with EC_AV1_GATE_DUMP off the cdf-forwarding gate", path.display() ); return; }; if !have_ffmpeg() { eprintln!("SKIP pinned_golden4_stream_decodes_pixel_exact: no ffmpeg"); return; } let before = non_last_ref_hits(); let frames = decode_stream(&stream).expect("pinned stream must decode"); eprintln!( "non_last_ref_hits before={before} after={}", non_last_ref_hits() ); let ffmpeg_frames = ffmpeg_decode_sequence(&stream, 64, 64, 4); for (i, (got, want)) in frames.iter().zip(&ffmpeg_frames).enumerate() { assert_eq!(got.y, want.y, "frame {i} luma vs ffmpeg (pinned)"); assert_eq!(got.u, want.u, "frame {i} U vs ffmpeg (pinned)"); assert_eq!(got.v, want.v, "frame {i} V vs ffmpeg (pinned)"); } } /// lane-av1golden7 round 9: pins the residual GOLDEN_FRAME MC bug found /// by the forwarding gate once film grain is out of the way (1/20 real /// aomenc streams, frame 3 luma, apply_grain=false, `non_last_ref_hits` /// delta=2). `GOLDEN_FRAME` refuses by name until this decodes exact. #[test] #[ignore = "reads a pinned fixture path outside the repo; run manually"] fn pinned_golden7_stream_decodes_pixel_exact() { use crate::decode::non_last_ref_hits; let path = concat!( env!("CARGO_MANIFEST_DIR"), "/../../fixtures/golden7-forwarding-mismatch.obu" ); let stream = std::fs::read(path).expect("reading pinned stream"); if !have_ffmpeg() { eprintln!("SKIP pinned_golden7_stream_decodes_pixel_exact: no ffmpeg"); return; } let before = non_last_ref_hits(); let frames = decode_stream(&stream).expect("pinned stream must decode"); eprintln!( "non_last_ref_hits before={before} after={}", non_last_ref_hits() ); let ffmpeg_frames = ffmpeg_decode_sequence(&stream, 64, 64, 4); for (i, (got, want)) in frames.iter().zip(&ffmpeg_frames).enumerate() { assert_eq!(got.y, want.y, "frame {i} luma vs ffmpeg (pinned)"); assert_eq!(got.u, want.u, "frame {i} U vs ffmpeg (pinned)"); assert_eq!(got.v, want.v, "frame {i} V vs ffmpeg (pinned)"); } } /// lane-av1refs's decisive single-non-LAST-reference gate: identical /// recipe to /// [`a_real_aomenc_inter_sequence_with_cdf_forwarding_decodes_pixel_exact`] /// except `--enable-order-hint=0` is DROPPED (that flag's own comment on /// the sibling test names it as exactly what suppresses aomenc's RD from /// ever picking `GOLDEN_FRAME`) and the sequence runs 8 frames, not 4 -- /// more chances for the RD search to reach past the first GOP's initial /// key frame. `--auto-alt-ref=0` stays: this decoder only supports /// `LAST_FRAME`/`GOLDEN_FRAME` so far, and alt-ref/backward references /// are still a named refusal, not this gate's target. A stream that /// decodes without firing [`decode::non_last_ref_hits`] is not a /// coverage win for this round -- SKIP it as a genuine, named miss /// rather than pass silently on untested ground. #[test] fn a_real_aomenc_inter_sequence_with_a_golden_reference_decodes_pixel_exact() { if !have_ffmpeg() { eprintln!( "SKIP a_real_aomenc_inter_sequence_with_a_golden_reference_decodes_pixel_exact: no ffmpeg" ); return; } if !have_aomenc() { eprintln!( "SKIP a_real_aomenc_inter_sequence_with_a_golden_reference_decodes_pixel_exact: no aomenc at {}", aomenc_path().display() ); return; } let (width, height, frame_count) = (64usize, 64usize, 8usize); let mut refusals = Vec::new(); let mut never_fired = 0u32; // aomenc's own RD search is nondeterministic run-to-run even at // `--threads=1` (confirmed live: identical CLI args, identical // input, two different output byte streams) -- so beyond varying // the fixture's seed, each attempt is its own independent coin // flip against every named refusal below. 120 attempts (not 40) // to give that coin flip enough tries to land on a stream this // decoder both accepts and that fires GOLDEN_FRAME. `duration=0.32` // at `rate=25` (not `0.24`) matches every sibling gate's own // frame-count fixture exactly -- a shorter duration here once // produced a frame-0 (keyframe, no reference at all) luma mismatch // against ffmpeg, i.e. a source-frame-count rounding artifact of // this recipe's own choosing, not a decoder bug. for attempt in 0..120u32 { let seed = 42 + attempt % 40; let source = gradients_source(seed, 64, 64, "duration=0.32:rate=25"); let y4m = Command::new("ffmpeg") .args([ "-v", "error", "-f", "lavfi", "-i", &source, "-pix_fmt", "yuv420p", "-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=55", "--cpu-used=0", "--lag-in-frames=0", "--auto-alt-ref=0", "--kf-max-dist=1000", "--threads=1", "--row-mt=0", // Narrows the RD search's reference alphabet toward // LAST_FRAME/GOLDEN_FRAME -- without this, aomenc's RD is // just as likely to pick LAST2/LAST3/BWDREF/ALTREF2/ // ALTREF (this decoder's already-documented, still-open // refusals), starving this gate of the one reference it // targets. "--max-reference-frames=3", "--reduced-reference-set=1", "--enable-warped-motion=0", "--enable-obmc=0", "--enable-masked-comp=0", "--enable-interintra-comp=0", "--enable-dist-wtd-comp=0", "--enable-diff-wtd-comp=0", "--enable-onesided-comp=0", "--enable-interintra-wedge=0", "--enable-smooth-interintra=0", "--enable-rect-partitions=0", "--enable-ab-partitions=0", "--enable-1to4-partitions=0", "--enable-filter-intra=0", "--enable-smooth-intra=0", "--enable-paeth-intra=0", "--enable-directional-intra=0", "--enable-angle-delta=0", "--enable-tx-size-search=0", "--enable-cdef=0", "--enable-restoration=0", // `min == max == 32` forces PARTITION_NONE for every // 32x32 block, not just biases toward it -- with // `--min-partition-size=16` alone aomenc's exhaustive // `cpu-used=0` RD search still occasionally read HORZ/ // VERT/SPLIT partitions below 32x32 despite // `--enable-rect-partitions=0`/`--enable-ab-partitions=0`/ // `--enable-1to4-partitions=0` all being off (two // separate, already-documented round-2 gaps: "a // partition below 16x16", "a partition type this encoder // never writes"), starving this gate's already-narrow // seed pool. Locking both bounds to 32 removes any // partition-depth choice from the search entirely, so // this gate can isolate GOLDEN_FRAME selection alone. "--max-partition-size=32", "--min-partition-size=32", "--enable-palette=0", "--enable-intrabc=0", "--enable-cfl-intra=0", // use_ref_frame_mvs (temporal MV projection) is unimplemented in mvstack; // leaving it on silently desyncs symbols on inter frames (grainfix lesson). "--enable-ref-frame-mvs=0", // `--tune-content=film` was dropped: on this box it // makes aomenc emit `apply_grain` almost every attempt // (lane-av1golden8 round 10 measurement, live: 120/120 // real attempts hit a named refusal, 0 ever fired // GOLDEN_FRAME -- mostly `apply_grain`, dominated by its // own recipe flag) -- film grain synthesis has its own // dedicated refusal test and must not starve this gate // of the GOLDEN_FRAME draws it exists to prove. Default // content tuning still occasionally flips // `allow_screen_content_tools` on for this synthetic // gradient content (a separate, already-documented open // refusal), which the 120-attempt coin flip below // tolerates the same way. "--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::non_last_ref_hits(); let frames = match decode_stream(&stream) { Err(e) => { let msg = e.to_string(); assert!( msg.contains("unsupported"), "a golden-reference stream failed outright (seed {seed}): {msg}" ); refusals.push(format!("seed {seed}: {msg}")); continue; } Ok(frames) => frames, }; if decode::non_last_ref_hits() == before { // This seed's RD search never actually picked GOLDEN_FRAME -- // a genuine miss, not a refusal. Try the next seed rather // than claiming coverage this attempt did not exercise. never_fired += 1; continue; } let ffmpeg_frames = ffmpeg_decode_sequence(&stream, width, height, frame_count); assert_eq!(frames.len(), frame_count); // Same pin-on-mismatch pattern as the CDF forwarding gate above: // env-gated, no cost on a normal run. let mismatched = frames .iter() .zip(&ffmpeg_frames) .any(|(got, want)| got.y != want.y || got.u != want.u || got.v != want.v); if mismatched { if let Ok(path) = std::env::var("EC_AV1_GATE_DUMP") { std::fs::write(&path, &stream).expect("writing pinned stream"); eprintln!("EC_AV1_GATE_DUMP: wrote mismatching stream (seed {seed}) to {path}"); } } for (i, (got, want)) in frames.iter().zip(&ffmpeg_frames).enumerate() { assert_eq!(got.y, want.y, "frame {i} luma vs ffmpeg (seed {seed})"); assert_eq!(got.u, want.u, "frame {i} U vs ffmpeg (seed {seed})"); assert_eq!(got.v, want.v, "frame {i} V vs ffmpeg (seed {seed})"); } eprintln!( "FIRING seed {seed}: non_last_ref_hits advanced by {}", decode::non_last_ref_hits() - before ); return; } eprintln!( "SKIP a_real_aomenc_inter_sequence_with_a_golden_reference_decodes_pixel_exact: \ {never_fired} attempts decoded but never fired GOLDEN_FRAME, and every other \ attempt hit a named refusal:\n{}", refusals.join("\n") ); } /// lane-av1refs's decisive gate for the 5 references widened past /// `LAST_FRAME`/`GOLDEN_FRAME`: same envelope and pin-on-mismatch /// pattern as /// [`a_real_aomenc_inter_sequence_with_a_golden_reference_decodes_pixel_exact`], /// generalised over `(target_ref, extra_args, frame_count)` so each of /// `LAST2`/`LAST3`/`BWDREF`/`ALTREF2`/`ALTREF` gets its own attempt pool /// and its own proof that `decode::ref_hits(target_ref)` -- not just /// `non_last_ref_hits`, which cannot distinguish one reference from /// another -- actually advanced. `--max-reference-frames=3 /// --reduced-reference-set=1` (the golden gate's own narrowing) is /// DROPPED here: both suppress exactly the references this gate needs /// aomenc's RD to reach. fn a_real_aomenc_single_ref_gate( gate_name: &str, target_ref: i8, target_ref_name: &str, extra_args: &[&str], width: usize, height: usize, frame_count: usize, attempts: u32, ) { let _gate_lock = lock_gate_counters(); if !have_ffmpeg() { eprintln!("SKIP {gate_name}: no ffmpeg"); return; } if !have_aomenc() { eprintln!("SKIP {gate_name}: no aomenc at {}", aomenc_path().display()); return; } let mut refusals = Vec::new(); let mut never_fired = 0u32; for attempt in 0..attempts { let seed = 42 + attempt % 40; let duration = frame_count as f64 / 25.0; let source = gradients_source(seed, width, height, &format!("duration={duration}:rate=25")); let y4m = Command::new("ffmpeg") .args([ "-v", "error", "-f", "lavfi", "-i", &source, "-pix_fmt", "yuv420p", "-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 args: Vec<&str> = vec![ "--codec=av1", "--passes=1", "--end-usage=q", "--cq-level=55", "--cpu-used=0", "--kf-max-dist=1000", "--threads=1", "--row-mt=0", "--enable-warped-motion=0", "--enable-obmc=0", "--enable-masked-comp=0", "--enable-interintra-comp=0", "--enable-dist-wtd-comp=0", "--enable-diff-wtd-comp=0", "--enable-onesided-comp=0", "--enable-interintra-wedge=0", "--enable-smooth-interintra=0", "--enable-rect-partitions=0", "--enable-ab-partitions=0", "--enable-1to4-partitions=0", "--enable-filter-intra=0", "--enable-smooth-intra=0", "--enable-paeth-intra=0", "--enable-directional-intra=0", "--enable-angle-delta=0", "--enable-tx-size-search=0", "--enable-cdef=0", "--enable-restoration=0", "--max-partition-size=32", "--min-partition-size=32", "--enable-palette=0", "--enable-intrabc=0", "--enable-cfl-intra=0", // use_ref_frame_mvs (temporal MV projection) is unimplemented in // mvstack; leaving it on silently desyncs symbols on inter frames. "--enable-ref-frame-mvs=0", "--obu", "-o", "-", "-", ]; // extra_args goes right before the trailing "-o", "-", "-" triple. let tail = args.split_off(args.len() - 3); args.extend_from_slice(extra_args); args.extend_from_slice(&tail); let mut child = Command::new(aomenc_path()) .args(&args) .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::ref_hits(target_ref); let frames = match decode_stream(&stream) { Err(e) => { let msg = e.to_string(); assert!( msg.contains("unsupported"), "{gate_name} failed outright (seed {seed}): {msg}" ); refusals.push(format!("seed {seed}: {msg}")); continue; } Ok(frames) => frames, }; if decode::ref_hits(target_ref) == before { never_fired += 1; continue; } let ffmpeg_frames = ffmpeg_decode_sequence(&stream, width, height, frame_count); assert_eq!(frames.len(), frame_count); let mismatched = frames .iter() .zip(&ffmpeg_frames) .any(|(got, want)| got.y != want.y || got.u != want.u || got.v != want.v); if mismatched { if let Ok(path) = std::env::var("EC_AV1_GATE_DUMP") { std::fs::write(&path, &stream).expect("writing pinned stream"); eprintln!("EC_AV1_GATE_DUMP: wrote mismatching stream (seed {seed}) to {path}"); } } for (i, (got, want)) in frames.iter().zip(&ffmpeg_frames).enumerate() { assert_eq!( got.y, want.y, "{gate_name} frame {i} luma vs ffmpeg (seed {seed})" ); assert_eq!( got.u, want.u, "{gate_name} frame {i} U vs ffmpeg (seed {seed})" ); assert_eq!( got.v, want.v, "{gate_name} frame {i} V vs ffmpeg (seed {seed})" ); } eprintln!( "{gate_name} FIRING seed {seed}: {target_ref_name} hits advanced by {}", decode::ref_hits(target_ref) - before ); return; } eprintln!( "SKIP {gate_name}: {never_fired} attempts decoded but never fired {target_ref_name}, \ and every other attempt hit a named refusal:\n{}", refusals.join("\n") ); } /// lane-av1comp: proves `reference_select`/`comp_mode` are actually read /// off a real bitstream rather than only unit-tested in isolation -- /// `--auto-alt-ref=1 --lag-in-frames=16` gives aomenc a backward /// reference to pick `COMPOUND_REFERENCE` with, `--enable-order-hint=1` /// is required for it to ever consider compound at all. Three /// acceptable outcomes: (a) a stream where `reference_select` never /// actually drove a block to `COMPOUND_REFERENCE` decodes bit-exact end /// to end (proving the per-block `comp_mode` read did not desync the /// single-ref blocks around it); (b) a stream that does fire compound /// is refused by name with [`decode::comp_mode_hits`] having advanced /// (proving `comp_mode`/`read_compound_ref_frames` consumed the right /// symbols before refusing, rather than desyncing and refusing for an /// unrelated reason) -- masked/wedge compound (`comp_group_idx == 1`) /// and a partition below 16x16 (a real, separately tracked capability /// gap) are the two refusal arms this can still legitimately name; (c) /// r13 wired plain `COMPOUND_AVERAGE` MC for real, so as of r15 a /// stream that fires compound may also decode fully -- accepted only /// when every output frame is pixel-exact against ffmpeg. #[test] fn a_real_aomenc_stream_with_reference_select_reads_comp_mode_correctly() { const NAME: &str = "a_real_aomenc_stream_with_reference_select_reads_comp_mode_correctly"; let _gate_lock = lock_gate_counters(); if !have_ffmpeg() { eprintln!("SKIP {NAME}: no ffmpeg"); return; } if !have_aomenc() { eprintln!("SKIP {NAME}: no aomenc at {}", aomenc_path().display()); return; } let (width, height, frame_count) = (64usize, 64usize, 20usize); let mut refusals = Vec::new(); let mut never_fired = 0u32; for attempt in 0..60u32 { let seed = 42 + attempt % 40; let duration = frame_count as f64 / 25.0; let source = gradients_source(seed, width, height, &format!("duration={duration}:rate=25")); let y4m = Command::new("ffmpeg") .args([ "-v", "error", "-f", "lavfi", "-i", &source, "-pix_fmt", "yuv420p", "-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 args: Vec<&str> = vec![ "--codec=av1", "--passes=1", "--end-usage=q", "--cq-level=45", "--cpu-used=0", "--kf-max-dist=1000", "--threads=1", "--row-mt=0", "--auto-alt-ref=1", "--lag-in-frames=16", "--enable-order-hint=1", "--enable-warped-motion=0", "--enable-obmc=0", "--enable-masked-comp=0", "--enable-interintra-comp=0", "--enable-onesided-comp=0", "--enable-interintra-wedge=0", "--enable-smooth-interintra=0", "--enable-rect-partitions=0", "--enable-ab-partitions=0", "--enable-1to4-partitions=0", "--enable-filter-intra=0", "--enable-smooth-intra=0", "--enable-paeth-intra=0", "--enable-directional-intra=0", "--enable-angle-delta=0", "--enable-tx-size-search=0", "--enable-cdef=0", "--enable-restoration=0", "--max-partition-size=32", "--min-partition-size=32", "--enable-palette=0", "--enable-intrabc=0", "--enable-cfl-intra=0", "--enable-ref-frame-mvs=0", "--obu", "-o", "-", "-", ]; let mut child = Command::new(aomenc_path()) .args(&args) .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::comp_mode_hits(); match decode_stream(&stream) { Err(e) => { let msg = e.to_string(); let fired = decode::comp_mode_hits() > before; assert!( msg.contains("unsupported"), "{NAME} failed outright (seed {seed}): {msg}" ); if fired { // `comp_mode` firing does not stop the block walk -- // a later, unrelated block in the same tile can // still hit any other named refusal this decoder // carries. A partition below 16x16 is a real, open // capability gap tracked on its own (not a // COMPOUND_REFERENCE defect); accept it here too // rather than fail a compound-specific gate on a // partition-search coincidence. lane-av1blend r7: // re-swept the wide gate with the plain-average // blend unmasked (r6/r7's ref_ctx fix alone) and it // still mismatches on real streams beyond that one // bug -- re-masked comp_group_idx == 0 too, so this // stays a legal named refusal. assert!( msg.contains("COMPOUND_REFERENCE") || msg.contains("a partition"), "{NAME}: comp_mode fired (seed {seed}) but the refusal \ was for something else: {msg}" ); let advanced = decode::comp_mode_hits() - before; eprintln!( "{NAME} FIRING seed {seed}: comp_mode hits advanced by \ {advanced}, refused by name as expected: {msg}" ); return; } refusals.push(format!("seed {seed}: {msg}")); continue; } Ok(frames) => { if decode::comp_mode_hits() == before { never_fired += 1; continue; } // Outcome (c): plain COMPOUND_AVERAGE decoded fully -- // non-negotiable pixel exactness, no tolerance. let ffmpeg_frames = ffmpeg_decode_sequence(&stream, width, height, frame_count); assert_eq!(frames.len(), frame_count); let mismatched = frames .iter() .zip(&ffmpeg_frames) .any(|(got, want)| got.y != want.y || got.u != want.u || got.v != want.v); if mismatched { let pin = std::env::temp_dir().join("ec-av1-reference-select-gate-fail.obu"); let _ = std::fs::write(&pin, &stream); if let Ok(path) = std::env::var("EC_AV1_GATE_DUMP") { std::fs::write(&path, &stream).expect("writing pinned stream"); eprintln!("EC_AV1_GATE_DUMP: wrote mismatching stream (seed {seed}) to {path}"); } for (i, (got, want)) in frames.iter().zip(&ffmpeg_frames).enumerate() { assert_eq!( got.y, want.y, "{NAME} frame {i} luma vs ffmpeg (seed {seed}) -- stream pinned at {}", pin.display() ); assert_eq!( got.u, want.u, "{NAME} frame {i} U vs ffmpeg (seed {seed}) -- stream pinned at {}", pin.display() ); assert_eq!( got.v, want.v, "{NAME} frame {i} V vs ffmpeg (seed {seed}) -- stream pinned at {}", pin.display() ); } } else { for (i, (got, want)) in frames.iter().zip(&ffmpeg_frames).enumerate() { assert_eq!(got.y, want.y, "{NAME} frame {i} luma vs ffmpeg (seed {seed})"); assert_eq!(got.u, want.u, "{NAME} frame {i} U vs ffmpeg (seed {seed})"); assert_eq!(got.v, want.v, "{NAME} frame {i} V vs ffmpeg (seed {seed})"); } } eprintln!( "{NAME} FIRING seed {seed}: comp_mode hits advanced by {}, decoded \ fully and pixel-exact", decode::comp_mode_hits() - before ); return; } } } eprintln!( "SKIP {NAME}: {never_fired} attempts decoded fully (reference_select never drove a \ block to COMPOUND_REFERENCE) and every other attempt hit a named refusal that never \ reached comp_mode:\n{}", refusals.join("\n") ); } /// lane-av1comp round 14's decisive gate for `skip_mode`: same /// pin-on-mismatch pattern as [`a_real_aomenc_single_ref_gate`], proving /// [`decode::skip_mode_hits`] actually advanced (a real block picked /// `skip_mode`, not just that the frame header bit was set) and that the /// resulting forced-compound `NEAREST_NEARESTMV` decode is still /// pixel-exact against ffmpeg. `--auto-alt-ref=1 --lag-in-frames=16 /// --enable-fwd-kf=0` (backward references) plus /// `--enable-ref-frame-mvs=0` (unimplemented temporal MV projection, /// same as every other gate here) is aomenc's own requirement for /// `skip_mode_present` to ever be considered at all (spec 5.9.22 needs /// both a forward and a backward reference candidate). #[test] fn a_real_aomenc_stream_with_compound_references_decodes_pixel_exact() { const NAME: &str = "a_real_aomenc_stream_with_compound_references_decodes_pixel_exact"; let _gate_lock = lock_gate_counters(); if !have_ffmpeg() { eprintln!("SKIP {NAME}: no ffmpeg"); return; } if !have_aomenc() { eprintln!("SKIP {NAME}: no aomenc at {}", aomenc_path().display()); return; } let (width, height, frame_count) = (64usize, 64usize, 24usize); let mut refusals = Vec::new(); let mut never_fired = 0u32; for attempt in 0..80u32 { let seed = 42 + attempt % 40; let duration = frame_count as f64 / 25.0; let source = gradients_source(seed, width, height, &format!("duration={duration}:rate=25")); let y4m = Command::new("ffmpeg") .args([ "-v", "error", "-f", "lavfi", "-i", &source, "-pix_fmt", "yuv420p", "-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 args: Vec<&str> = vec![ "--codec=av1", "--passes=1", "--end-usage=q", "--cq-level=45", "--cpu-used=0", "--kf-max-dist=1000", "--threads=1", "--row-mt=0", "--auto-alt-ref=1", "--lag-in-frames=16", "--enable-fwd-kf=0", "--enable-order-hint=1", "--enable-warped-motion=0", "--enable-obmc=0", "--enable-masked-comp=0", "--enable-interintra-comp=0", "--enable-onesided-comp=0", "--enable-interintra-wedge=0", "--enable-smooth-interintra=0", "--enable-rect-partitions=0", "--enable-ab-partitions=0", "--enable-1to4-partitions=0", "--enable-filter-intra=0", "--enable-smooth-intra=0", "--enable-paeth-intra=0", "--enable-directional-intra=0", "--enable-angle-delta=0", "--enable-tx-size-search=0", "--enable-cdef=0", "--enable-restoration=0", "--max-partition-size=32", "--min-partition-size=32", "--enable-palette=0", "--enable-intrabc=0", "--enable-cfl-intra=0", "--enable-ref-frame-mvs=0", "--obu", "-o", "-", "-", ]; let mut child = Command::new(aomenc_path()) .args(&args) .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::skip_mode_hits(); let frames = match decode_stream(&stream) { Err(e) => { let msg = e.to_string(); assert!( msg.contains("unsupported"), "{NAME} failed outright (seed {seed}): {msg}" ); refusals.push(format!("seed {seed}: {msg}")); continue; } Ok(frames) => frames, }; if decode::skip_mode_hits() == before { never_fired += 1; continue; } let ffmpeg_frames = ffmpeg_decode_sequence(&stream, width, height, frame_count); assert_eq!(frames.len(), frame_count); let mismatched = frames .iter() .zip(&ffmpeg_frames) .any(|(got, want)| got.y != want.y || got.u != want.u || got.v != want.v); if mismatched { let pin = std::env::temp_dir().join("ec-av1-compound-refs-gate-fail.obu"); let _ = std::fs::write(&pin, &stream); if let Ok(path) = std::env::var("EC_AV1_GATE_DUMP") { std::fs::write(&path, &stream).expect("writing pinned stream"); eprintln!("EC_AV1_GATE_DUMP: wrote mismatching stream (seed {seed}) to {path}"); } for (i, (got, want)) in frames.iter().zip(&ffmpeg_frames).enumerate() { assert_eq!( got.y, want.y, "{NAME} frame {i} luma vs ffmpeg (seed {seed}) -- stream pinned at {}", pin.display() ); assert_eq!( got.u, want.u, "{NAME} frame {i} U vs ffmpeg (seed {seed}) -- stream pinned at {}", pin.display() ); assert_eq!( got.v, want.v, "{NAME} frame {i} V vs ffmpeg (seed {seed}) -- stream pinned at {}", pin.display() ); } } eprintln!( "{NAME} FIRING seed {seed}: skip_mode hits advanced by {}", decode::skip_mode_hits() - before ); return; } eprintln!( "SKIP {NAME}: {never_fired} attempts decoded but never fired skip_mode, and every \ other attempt hit a named refusal:\n{}", refusals.join("\n") ); } /// lane-motionmode round 1: `--enable-obmc=1 --enable-warped-motion=0` /// on the base recipe (`--auto-alt-ref=1 --lag-in-frames=16 /// --enable-fwd-kf=0 --enable-ref-frame-mvs=0`) -- pixel-exact against /// ffmpeg, gated on [`decode::obmc_hits`] actually advancing (a real /// block picked `OBMC_CAUSAL`, not just that the header bit was set). /// Structure copied from /// `a_real_aomenc_stream_with_compound_references_decodes_pixel_exact`: /// a seed sweep with a named-refusal escape hatch and an /// `EC_AV1_GATE_DUMP` pin on mismatch. #[test] fn a_real_aomenc_stream_with_obmc_decodes_pixel_exact() { const NAME: &str = "a_real_aomenc_stream_with_obmc_decodes_pixel_exact"; let _gate_lock = lock_gate_counters(); if !have_ffmpeg() { eprintln!("SKIP {NAME}: no ffmpeg"); return; } if !have_aomenc() { eprintln!("SKIP {NAME}: no aomenc at {}", aomenc_path().display()); return; } let (width, height, frame_count) = (64usize, 64usize, 24usize); let mut refusals = Vec::new(); let mut never_fired = 0u32; for attempt in 0..80u32 { let seed = 42 + attempt % 40; let duration = frame_count as f64 / 25.0; let source = gradients_source(seed, width, height, &format!("duration={duration}:rate=25")); let y4m = Command::new("ffmpeg") .args([ "-v", "error", "-f", "lavfi", "-i", &source, "-pix_fmt", "yuv420p", "-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 args: Vec<&str> = vec![ "--codec=av1", "--passes=1", "--end-usage=q", "--cq-level=45", "--cpu-used=0", "--kf-max-dist=1000", "--threads=1", "--row-mt=0", "--auto-alt-ref=1", "--lag-in-frames=16", "--enable-fwd-kf=0", "--enable-order-hint=1", "--enable-warped-motion=0", "--enable-obmc=1", "--enable-masked-comp=0", "--enable-interintra-comp=0", "--enable-onesided-comp=0", "--enable-interintra-wedge=0", "--enable-smooth-interintra=0", "--enable-rect-partitions=0", "--enable-ab-partitions=0", "--enable-1to4-partitions=0", "--enable-filter-intra=0", "--enable-smooth-intra=0", "--enable-paeth-intra=0", "--enable-directional-intra=0", "--enable-angle-delta=0", "--enable-tx-size-search=0", "--enable-cdef=0", "--enable-restoration=0", "--max-partition-size=32", "--min-partition-size=32", "--enable-palette=0", "--enable-intrabc=0", "--enable-cfl-intra=0", "--enable-ref-frame-mvs=0", "--obu", "-o", "-", "-", ]; let mut child = Command::new(aomenc_path()) .args(&args) .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::obmc_hits(); let frames = match decode_stream(&stream) { Err(e) => { let msg = e.to_string(); assert!( msg.contains("unsupported"), "{NAME} failed outright (seed {seed}): {msg}" ); refusals.push(format!("seed {seed}: {msg}")); continue; } Ok(frames) => frames, }; if decode::obmc_hits() == before { never_fired += 1; continue; } let ffmpeg_frames = ffmpeg_decode_sequence(&stream, width, height, frame_count); assert_eq!(frames.len(), frame_count); let mismatched = frames .iter() .zip(&ffmpeg_frames) .any(|(got, want)| got.y != want.y || got.u != want.u || got.v != want.v); if mismatched { if let Ok(path) = std::env::var("EC_AV1_GATE_DUMP") { std::fs::write(&path, &stream).expect("writing pinned stream"); eprintln!("EC_AV1_GATE_DUMP: wrote mismatching stream (seed {seed}) to {path}"); } } for (i, (got, want)) in frames.iter().zip(&ffmpeg_frames).enumerate() { assert_eq!(got.y, want.y, "{NAME} frame {i} luma vs ffmpeg (seed {seed})"); assert_eq!(got.u, want.u, "{NAME} frame {i} U vs ffmpeg (seed {seed})"); assert_eq!(got.v, want.v, "{NAME} frame {i} V vs ffmpeg (seed {seed})"); } eprintln!( "{NAME} FIRING seed {seed}: obmc hits advanced by {}", decode::obmc_hits() - before ); return; } eprintln!( "SKIP {NAME}: {never_fired} attempts decoded but never fired obmc, and every \ other attempt hit a named refusal:\n{}", refusals.join("\n") ); } /// lane-motionmode round 3: same shape as /// `a_real_aomenc_stream_with_obmc_decodes_pixel_exact`, but relaxed the /// `--min-partition-size=32 --max-partition-size=32` clamp (that other /// gate's own doc: locked to remove partition-depth choice from the /// search entirely) to `8`/`32`, to make an 8x8-leaf `OBMC_CAUSAL` pick /// possible at all. Gated on [`decode::obmc_hits_8`] specifically, not /// the aggregate [`decode::obmc_hits`]. Measured live (round 3, this /// box): 80 attempts, 0 ever called `decode_inter_block8` at all -- /// `cpu-used=0`'s RD search never once splits a 16x16 down to a clean /// 4-way 8x8 leaf for this smooth `gradients` fixture (large blocks /// always win the RD cost there); the handful of named refusals seen /// are `PARTITION_SPLIT` chosen *at* 8x8 (down to 4x4, already refused /// at `decode.rs`'s `part8 != PARTITION_NONE` check) or /// `allow_screen_content_tools`, neither reaching a leaf. Kept anyway /// (mirrors this file's own `never_fired`-skip convention) as the /// honest record of the attempt; a real 8x8 OBMC fixture needs /// higher-frequency content than this gate's fixtures use, out of /// round-3 budget. #[test] fn a_real_aomenc_stream_with_obmc_8x8_decodes_pixel_exact() { const NAME: &str = "a_real_aomenc_stream_with_obmc_8x8_decodes_pixel_exact"; let _gate_lock = lock_gate_counters(); if !have_ffmpeg() { eprintln!("SKIP {NAME}: no ffmpeg"); return; } if !have_aomenc() { eprintln!("SKIP {NAME}: no aomenc at {}", aomenc_path().display()); return; } let (width, height, frame_count) = (64usize, 64usize, 24usize); let mut refusals = Vec::new(); let mut never_fired = 0u32; let mut total_obmc8 = 0usize; let n_attempts: u32 = std::env::var("EC_OBMC8_ATTEMPTS") .ok() .and_then(|s| s.parse().ok()) .unwrap_or(80); for attempt in 0..n_attempts { let seed = 42 + attempt % 40; let duration = frame_count as f64 / 25.0; // Recipe search hook: `gradients` is smooth, so aomenc's RD never // splits below 16x16 and this gate never fired one 8x8 OBMC block // in 45 attempts -- it "passed" by skipping (gate-blind-to-feature). // EC_AV1_OBMC8_SOURCE overrides the lavfi source so a firing recipe // can be searched for without a rebuild. let source = std::env::var("EC_AV1_OBMC8_SOURCE").unwrap_or_else(|_| { gradients_source(seed, width, height, &format!("duration={duration}:rate=25")) }); // A source supplied through EC_AV1_OBMC8_SOURCE need not carry its // own `duration=`; `-t` is the bound that always holds. Without it // an endless source (`testsrc2`) fills the pipe forever and the // gate hangs to its 300 s timeout. let duration_arg = format!("{duration}"); let y4m = Command::new("ffmpeg") .args([ "-v", "error", "-f", "lavfi", "-i", &source, "-t", &duration_arg, "-pix_fmt", "yuv420p", "-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 args: Vec<&str> = vec![ "--codec=av1", "--passes=1", "--end-usage=q", "--cq-level=45", "--cpu-used=0", "--kf-max-dist=1000", "--threads=1", "--row-mt=0", "--auto-alt-ref=1", "--lag-in-frames=16", "--enable-fwd-kf=0", "--enable-order-hint=1", "--enable-warped-motion=0", "--enable-obmc=1", "--enable-masked-comp=0", "--enable-interintra-comp=0", "--enable-onesided-comp=0", "--enable-interintra-wedge=0", "--enable-smooth-interintra=0", "--enable-rect-partitions=0", "--enable-ab-partitions=0", "--enable-1to4-partitions=0", "--enable-filter-intra=0", "--enable-smooth-intra=0", "--enable-paeth-intra=0", "--enable-directional-intra=0", "--enable-angle-delta=0", "--enable-tx-size-search=0", "--enable-cdef=0", "--enable-restoration=0", "--min-partition-size=8", "--max-partition-size=32", "--enable-palette=0", "--enable-intrabc=0", "--enable-cfl-intra=0", "--enable-ref-frame-mvs=0", "--obu", "-o", "-", "-", ]; let mut child = Command::new(aomenc_path()) .args(&args) .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 before8 = decode::obmc_hits_8(); let frames = match decode_stream(&stream) { Err(e) => { let msg = e.to_string(); assert!( msg.contains("unsupported"), "{NAME} failed outright (seed {seed}): {msg}" ); refusals.push(format!("seed {seed}: {msg}")); continue; } Ok(frames) => frames, }; let fired8 = decode::obmc_hits_8() - before8; total_obmc8 += fired8; if fired8 == 0 { never_fired += 1; continue; } let ffmpeg_frames = ffmpeg_decode_sequence(&stream, width, height, frame_count); assert_eq!(frames.len(), frame_count); let mismatched = frames .iter() .zip(&ffmpeg_frames) .any(|(got, want)| got.y != want.y || got.u != want.u || got.v != want.v); if mismatched { if let Ok(path) = std::env::var("EC_AV1_GATE_DUMP") { std::fs::write(&path, &stream).expect("writing pinned stream"); eprintln!("EC_AV1_GATE_DUMP: wrote mismatching stream (seed {seed}) to {path}"); } } for (i, (got, want)) in frames.iter().zip(&ffmpeg_frames).enumerate() { assert_eq!(got.y, want.y, "{NAME} frame {i} luma vs ffmpeg (seed {seed})"); assert_eq!(got.u, want.u, "{NAME} frame {i} U vs ffmpeg (seed {seed})"); assert_eq!(got.v, want.v, "{NAME} frame {i} V vs ffmpeg (seed {seed})"); } eprintln!( "{NAME} FIRING seed {seed}: 8x8 obmc hits {fired8} (total across attempts so far {total_obmc8})" ); return; } eprintln!( "SKIP {NAME}: {never_fired} attempts decoded but never fired an 8x8 obmc block \ ({total_obmc8} total 8x8 obmc hits across all attempts), and every other attempt \ hit a named refusal:\n{}", refusals.join("\n") ); } /// lane-warp r5e (flipped from lane-motionmode r1's refuses-or-matches): /// `--enable-warped-motion=1` streams must DECODE pixel-exact -- /// `av1_findSamples`/`num_proj_ref`, the 3-symbol `motion_mode` read, /// the affine projection/filter, and the WARPED_CAUSAL interp-filter /// derivation are all ported. A refusal naming warp, or any pixel /// mismatch, fails the gate; refusals for OTHER named capabilities /// (screen content tools) still skip that seed. The sweep must also /// actually exercise warp (`warp_selected_hits > 0`) so a decoder that /// silently stops selecting WARPED_CAUSAL cannot pass vacuously. #[test] fn a_real_aomenc_stream_with_warped_motion_refuses_or_matches() { const NAME: &str = "a_real_aomenc_stream_with_warped_motion_refuses_or_matches"; let _gate_lock = lock_gate_counters(); if !have_ffmpeg() { eprintln!("SKIP {NAME}: no ffmpeg"); return; } if !have_aomenc() { eprintln!("SKIP {NAME}: no aomenc at {}", aomenc_path().display()); return; } let (width, height, frame_count) = (64usize, 64usize, 24usize); let mut named_refusals = 0u32; let mut matched = 0u32; let n_attempts: u32 = std::env::var("EC_WARP_GATE_ATTEMPTS") .ok() .and_then(|s| s.parse().ok()) .unwrap_or(40); for attempt in 0..n_attempts { let seed = 42 + attempt; let duration = frame_count as f64 / 25.0; let source = gradients_source(seed, width, height, &format!("duration={duration}:rate=25")); let y4m = Command::new("ffmpeg") .args([ "-v", "error", "-f", "lavfi", "-i", &source, "-pix_fmt", "yuv420p", "-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 args: Vec<&str> = vec![ "--codec=av1", "--passes=1", "--end-usage=q", "--cq-level=45", "--cpu-used=0", "--kf-max-dist=1000", "--threads=1", "--row-mt=0", "--auto-alt-ref=1", "--lag-in-frames=16", "--enable-fwd-kf=0", "--enable-order-hint=1", "--enable-warped-motion=1", "--enable-obmc=1", "--tune-content=default", "--enable-masked-comp=0", "--enable-interintra-comp=0", "--enable-onesided-comp=0", "--enable-interintra-wedge=0", "--enable-smooth-interintra=0", "--enable-rect-partitions=0", "--enable-ab-partitions=0", "--enable-1to4-partitions=0", "--enable-filter-intra=0", "--enable-smooth-intra=0", "--enable-paeth-intra=0", "--enable-directional-intra=0", "--enable-angle-delta=0", "--enable-tx-size-search=0", "--enable-cdef=0", "--enable-restoration=0", "--max-partition-size=32", "--min-partition-size=32", "--enable-palette=0", "--enable-intrabc=0", "--enable-cfl-intra=0", "--enable-ref-frame-mvs=0", "--obu", "-o", "-", "-", ]; let mut child = Command::new(aomenc_path()) .args(&args) .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 frames = match decode_stream(&stream) { Err(e) => { let msg = e.to_string(); assert!( msg.contains("unsupported"), "{NAME} failed outright, not a named refusal (seed {seed}): {msg}" ); assert!( !msg.contains("warp"), "{NAME} refused on warp (seed {seed}) -- warp decode is ported, this \ refusal must not exist: {msg}" ); named_refusals += 1; eprintln!("seed {seed} refusal (non-warp capability): {msg}"); continue; } Ok(frames) => frames, }; let ffmpeg_frames = ffmpeg_decode_sequence(&stream, width, height, frame_count); assert_eq!(frames.len(), frame_count); let mismatched = frames .iter() .zip(&ffmpeg_frames) .any(|(got, want)| got.y != want.y || got.u != want.u || got.v != want.v); if mismatched && let Ok(path) = std::env::var("EC_AV1_GATE_DUMP") { std::fs::write(&path, &stream).expect("writing pinned stream"); eprintln!("EC_AV1_GATE_DUMP: wrote mismatching stream (seed {seed}) to {path}"); } for (i, (got, want)) in frames.iter().zip(&ffmpeg_frames).enumerate() { assert_eq!(got.y, want.y, "{NAME} frame {i} luma vs ffmpeg (seed {seed})"); assert_eq!(got.u, want.u, "{NAME} frame {i} U vs ffmpeg (seed {seed})"); assert_eq!(got.v, want.v, "{NAME} frame {i} V vs ffmpeg (seed {seed})"); } matched += 1; } assert!( matched > 0, "{NAME}: every attempt refused on other capabilities; the gate never exercised warp" ); assert!( crate::decode::warp_selected_hits() > 0, "{NAME}: {matched} matches but zero WARPED_CAUSAL blocks fired -- the gate decodes \ warp streams without ever selecting warp, so it proves nothing about warp" ); eprintln!( "{NAME}: {named_refusals} non-warp refusals, {matched} pixel-exact matches out of {n_attempts}, warp_selected_hits={}", crate::decode::warp_selected_hits() ); } /// lane-interintra r1: `--enable-interintra-comp=1` streams must DECODE /// pixel-exact -- the interintra flag/mode/wedge-flag syntax and the /// non-wedge smooth blend are ported. A refusal naming interintra fails /// the gate UNLESS it is the wedge refusal (`use_wedge_interintra == 1` /// is r2, and this recipe encodes with wedge off so it must not fire /// either); warp/obmc are enabled and supported; refusals for OTHER /// named capabilities (screen content tools) still skip that seed. The /// sweep must actually exercise interintra (`interintra_hits > 0`). #[test] fn a_real_aomenc_stream_with_interintra_decodes_pixel_exact() { const NAME: &str = "a_real_aomenc_stream_with_interintra_decodes_pixel_exact"; let _gate_lock = lock_gate_counters(); if !have_ffmpeg() { eprintln!("SKIP {NAME}: no ffmpeg"); return; } if !have_aomenc() { eprintln!("SKIP {NAME}: no aomenc at {}", aomenc_path().display()); return; } let (width, height, frame_count) = (64usize, 64usize, 24usize); let mut named_refusals = 0u32; let mut matched = 0u32; let n_attempts: u32 = std::env::var("EC_INTERINTRA_GATE_ATTEMPTS") .ok() .and_then(|s| s.parse().ok()) .unwrap_or(40); for attempt in 0..n_attempts { let seed = 42 + attempt; let duration = frame_count as f64 / 25.0; let source = gradients_source(seed, width, height, &format!("duration={duration}:rate=25")); let y4m = Command::new("ffmpeg") .args([ "-v", "error", "-f", "lavfi", "-i", &source, "-pix_fmt", "yuv420p", "-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 args: Vec<&str> = vec![ "--codec=av1", "--passes=1", "--end-usage=q", "--cq-level=45", "--cpu-used=0", "--kf-max-dist=1000", "--threads=1", "--row-mt=0", "--auto-alt-ref=1", "--lag-in-frames=16", "--enable-fwd-kf=0", "--enable-order-hint=1", "--enable-warped-motion=1", "--enable-obmc=1", "--tune-content=default", "--enable-masked-comp=0", "--enable-interintra-comp=1", "--enable-onesided-comp=0", "--enable-interintra-wedge=0", "--enable-smooth-interintra=1", "--enable-rect-partitions=0", "--enable-ab-partitions=0", "--enable-1to4-partitions=0", "--enable-filter-intra=0", "--enable-smooth-intra=0", "--enable-paeth-intra=0", "--enable-directional-intra=0", "--enable-angle-delta=0", "--enable-tx-size-search=0", "--enable-cdef=0", "--enable-restoration=0", "--max-partition-size=32", "--min-partition-size=32", "--enable-palette=0", "--enable-intrabc=0", "--enable-cfl-intra=0", "--enable-ref-frame-mvs=0", "--obu", "-o", "-", "-", ]; let mut child = Command::new(aomenc_path()) .args(&args) .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 frames = match decode_stream(&stream) { Err(e) => { let msg = e.to_string(); assert!( msg.contains("unsupported"), "{NAME} failed outright, not a named refusal (seed {seed}): {msg}" ); assert!( !msg.contains("warp"), "{NAME} refused on warp (seed {seed}) -- warp decode is ported, this \ refusal must not exist: {msg}" ); assert!( !msg.contains("interintra"), "{NAME} refused on interintra (seed {seed}) -- non-wedge interintra \ is ported and this recipe encodes with wedge off: {msg}" ); named_refusals += 1; eprintln!("seed {seed} refusal (non-warp capability): {msg}"); continue; } Ok(frames) => frames, }; let ffmpeg_frames = ffmpeg_decode_sequence(&stream, width, height, frame_count); assert_eq!(frames.len(), frame_count); let mismatched = frames .iter() .zip(&ffmpeg_frames) .any(|(got, want)| got.y != want.y || got.u != want.u || got.v != want.v); if mismatched && let Ok(path) = std::env::var("EC_AV1_GATE_DUMP") { std::fs::write(&path, &stream).expect("writing pinned stream"); eprintln!("EC_AV1_GATE_DUMP: wrote mismatching stream (seed {seed}) to {path}"); } for (i, (got, want)) in frames.iter().zip(&ffmpeg_frames).enumerate() { assert_eq!(got.y, want.y, "{NAME} frame {i} luma vs ffmpeg (seed {seed})"); assert_eq!(got.u, want.u, "{NAME} frame {i} U vs ffmpeg (seed {seed})"); assert_eq!(got.v, want.v, "{NAME} frame {i} V vs ffmpeg (seed {seed})"); } matched += 1; } assert!( matched > 0, "{NAME}: every attempt refused on other capabilities; the gate never exercised warp" ); assert!( crate::decode::interintra_hits() > 0, "{NAME}: {matched} matches but zero interintra blocks fired -- the gate decodes \ these streams without ever taking interintra, so it proves nothing" ); eprintln!( "{NAME}: {named_refusals} other-capability refusals, {matched} pixel-exact matches out of {n_attempts}, interintra_hits={}", crate::decode::interintra_hits() ); } /// lane-wii r2: `--enable-interintra-wedge=1` streams must DECODE /// pixel-exact -- the wedge-interintra syntax (adapting `wedge_index` /// CDF symbol, fixed sign 0) and the wedge mask blend are ported over /// the checksum-verified codebook. `--enable-masked-comp=0` stays so /// the encoder cannot route inter blocks through the still-unported /// COMPOUND_WEDGE/DIFFWTD path. A refusal naming interintra/wedge /// fails the gate; refusals for OTHER named capabilities still skip /// that seed. Zero `wii_hits` on a run means the encoder never /// selected wedge-interintra -- soft-skipped (warned), not failed: /// that is encoder choice, not a decoder bug. #[test] fn a_real_aomenc_stream_with_interintra_wedge_decodes_pixel_exact() { const NAME: &str = "a_real_aomenc_stream_with_interintra_wedge_decodes_pixel_exact"; let _gate_lock = lock_gate_counters(); if !have_ffmpeg() { eprintln!("SKIP {NAME}: no ffmpeg"); return; } if !have_aomenc() { eprintln!("SKIP {NAME}: no aomenc at {}", aomenc_path().display()); return; } let (width, height, frame_count) = (64usize, 64usize, 24usize); let mut named_refusals = 0u32; let mut matched = 0u32; let n_attempts: u32 = std::env::var("EC_WEDGE_GATE_ATTEMPTS") .ok() .and_then(|s| s.parse().ok()) .unwrap_or(40); for attempt in 0..n_attempts { let seed = 42 + attempt; let duration = frame_count as f64 / 25.0; let source = if attempt % 2 == 0 { gradients_source(seed, width, height, &format!("duration={duration}:rate=25")) } else { // `end_pts` does NOT bound mandelbrot (it generated 300+ // frames and hung the gate for an hour); `-t` below is the // real bound, matching every other mandelbrot gate recipe. format!("mandelbrot=size={width}x{height}:rate=25") }; let duration_arg = format!("{duration}"); let y4m = Command::new("ffmpeg") .args([ "-v", "error", "-f", "lavfi", "-i", &source, "-pix_fmt", "yuv420p", "-t", &duration_arg, "-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 args: Vec<&str> = vec![ "--codec=av1", "--passes=1", "--end-usage=q", "--cq-level=45", "--cpu-used=0", "--kf-max-dist=1000", "--threads=1", "--row-mt=0", "--auto-alt-ref=1", "--lag-in-frames=16", "--enable-fwd-kf=0", "--enable-order-hint=1", "--enable-warped-motion=1", "--enable-obmc=1", "--tune-content=default", "--enable-masked-comp=0", "--enable-interintra-comp=1", "--enable-onesided-comp=0", "--enable-interintra-wedge=1", "--enable-smooth-interintra=1", "--enable-rect-partitions=0", "--enable-ab-partitions=0", "--enable-1to4-partitions=0", "--enable-filter-intra=0", "--enable-smooth-intra=0", "--enable-paeth-intra=0", "--enable-directional-intra=0", "--enable-angle-delta=0", "--enable-tx-size-search=0", "--enable-cdef=0", "--enable-restoration=0", "--max-partition-size=32", "--min-partition-size=32", "--enable-palette=0", "--enable-intrabc=0", "--enable-cfl-intra=0", "--enable-ref-frame-mvs=0", "--obu", "-o", "-", "-", ]; let mut child = Command::new(aomenc_path()) .args(&args) .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 frames = match decode_stream(&stream) { Err(e) => { let msg = e.to_string(); assert!( msg.contains("unsupported"), "{NAME} failed outright, not a named refusal (seed {seed}): {msg}" ); // Verifier note (lane-wii r2): no refusal string containing // "wedge"/"interintra" exists in the decoder any more, so // string-forbidding asserts here would be vacuous (class // gate-blind-to-feature). The proof this gate carries is // pixel-exact decode plus the wii_hits count printed below. named_refusals += 1; eprintln!("seed {seed} refusal (non-wedge capability): {msg}"); continue; } Ok(frames) => frames, }; let ffmpeg_frames = ffmpeg_decode_sequence(&stream, width, height, frame_count); assert_eq!(frames.len(), frame_count); let mismatched = frames .iter() .zip(&ffmpeg_frames) .any(|(got, want)| got.y != want.y || got.u != want.u || got.v != want.v); if mismatched && let Ok(path) = std::env::var("EC_AV1_GATE_DUMP") { std::fs::write(&path, &stream).expect("writing pinned stream"); eprintln!("EC_AV1_GATE_DUMP: wrote mismatching stream (seed {seed}) to {path}"); } for (i, (got, want)) in frames.iter().zip(&ffmpeg_frames).enumerate() { assert_eq!(got.y, want.y, "{NAME} frame {i} luma vs ffmpeg (seed {seed})"); assert_eq!(got.u, want.u, "{NAME} frame {i} U vs ffmpeg (seed {seed})"); assert_eq!(got.v, want.v, "{NAME} frame {i} V vs ffmpeg (seed {seed})"); } matched += 1; } assert!( matched > 0, "{NAME}: every attempt refused on other capabilities; the gate never exercised wedge-interintra" ); // Hardened from a soft-note (verifier: gate-blind-to-feature): the // recipe fired wedge-interintra on EVERY one of 6 hammer runs // (wii_hits 2-7), so a zero-hit run is a regression, not sampling. assert!( crate::decode::wii_hits() > 0, "{NAME}: {matched} pixel-exact matches but zero wedge-interintra blocks fired -- \ the blend is unexercised (recipe fired 2-7 hits per run when landed)" ); eprintln!( "{NAME}: {named_refusals} other-capability refusals, {matched} pixel-exact matches out of {n_attempts}, wii_hits={}", crate::decode::wii_hits() ); } /// lane-rectgate r1: every prior gate pins `--enable-rect-partitions=0 /// --enable-ab-partitions=0 --min/max-partition-size=32` so aomenc never /// gets to choose a real rect/ab split. This charter's premise ("rect /// HORZ/VERT + HORZ_A/HORZ_B/VERT_A/VERT_B landed during the warp lane") /// does NOT hold: `decode_frame`'s inter-tile `match part32` only has /// arms for `PARTITION_NONE`/`SPLIT`/`HORZ_B` -- `HORZ`, `VERT`, /// `HORZ_A`, `VERT_A`, `VERT_B`, `HORZ_4`, `VERT_4` all fall into the /// generic "a partition type this encoder never writes" `_` arm. Two /// recipes were swept empirically (40 attempts each) to characterize the /// gap: /// - `--enable-rect-partitions=1 --enable-ab-partitions=1`, no /// min/max clamp: 0/40 matched -- every attempt hit either the /// screen-content refusal or a genuinely-unimplemented partition value /// (1/2/6/7/9 all observed), always somewhere in the 24-frame stream. /// - Same, but `--enable-1to4-partitions=0` explicit and /// `--min-partition-size=16`: still 0/40, `value=9` (`VERT_4`) /// dominates -- this build's `--enable-1to4-partitions=0` does NOT /// suppress `VERT_4` selection (aomenc-side, confirmed by direct flag /// toggle; outside this decoder). This recipe (below) restores the /// `--enable-rect-partitions=0 --min/max-partition-size=32` pin every /// other gate uses, flipping only `--enable-ab-partitions=1`: matches /// cleanly (33/40, rest screen-content refusals) but /// `extended_partition_hits` stayed 0 -- confirmed with the same probe /// wired into the already-green warp gate too (144 `warp_selected_hits`, /// 0 `extended_partition_hits`), so `PARTITION_HORZ_B` itself is /// apparently never chosen by aomenc for this small gradient fixture at /// `cq-level=45` regardless of clamp. No decode defect (crash or pixel /// mismatch) was found in any sweep -- every refusal is a correctly /// named, non-silent "unsupported" error. A refusal or mismatch naming /// any OTHER already-ported capability (warp, obmc, interintra) still /// fails the gate outright. #[test] fn a_real_aomenc_stream_with_free_partitions_decodes_pixel_exact() { const NAME: &str = "a_real_aomenc_stream_with_free_partitions_decodes_pixel_exact"; let _gate_lock = lock_gate_counters(); if !have_ffmpeg() { eprintln!("SKIP {NAME}: no ffmpeg"); return; } if !have_aomenc() { eprintln!("SKIP {NAME}: no aomenc at {}", aomenc_path().display()); return; } let (width, height, frame_count) = (64usize, 64usize, 24usize); let mut named_refusals = 0u32; let mut matched = 0u32; let n_attempts: u32 = std::env::var("EC_RECTGATE_ATTEMPTS") .ok() .and_then(|s| s.parse().ok()) .unwrap_or(40); for attempt in 0..n_attempts { let seed = 42 + attempt; let duration = frame_count as f64 / 25.0; let source = gradients_source(seed, width, height, &format!("duration={duration}:rate=25")); let y4m = Command::new("ffmpeg") .args([ "-v", "error", "-f", "lavfi", "-i", &source, "-pix_fmt", "yuv420p", "-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 args: Vec<&str> = vec![ "--codec=av1", "--passes=1", "--end-usage=q", "--cq-level=45", "--cpu-used=0", "--kf-max-dist=1000", "--threads=1", "--row-mt=0", "--auto-alt-ref=1", "--lag-in-frames=16", "--enable-fwd-kf=0", "--enable-order-hint=1", "--enable-warped-motion=1", "--enable-obmc=1", "--tune-content=default", "--enable-masked-comp=0", "--enable-interintra-comp=0", "--enable-onesided-comp=0", "--enable-interintra-wedge=0", "--enable-smooth-interintra=0", // lane-rect r2: HORZ/VERT decode for real (rect-flake-1 // byte-exact: size_h + warp dims + OBMC overlap + tx height) // -- free recipe restored; remaining partition kinds refuse // by name and skip their seeds. "--enable-rect-partitions=1", "--enable-ab-partitions=1", "--enable-1to4-partitions=0", "--enable-filter-intra=0", "--enable-smooth-intra=0", "--enable-paeth-intra=0", "--enable-directional-intra=0", "--enable-angle-delta=0", "--enable-tx-size-search=0", "--enable-cdef=0", "--enable-restoration=0", "--max-partition-size=32", "--min-partition-size=16", "--enable-palette=0", "--enable-intrabc=0", "--enable-cfl-intra=0", "--enable-ref-frame-mvs=0", "--obu", "-o", "-", "-", ]; let mut child = Command::new(aomenc_path()) .args(&args) .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 frames = match decode_stream(&stream) { Err(e) => { let msg = e.to_string(); assert!( msg.contains("unsupported"), "{NAME} failed outright, not a named refusal (seed {seed}): {msg}" ); for banned in ["warp", "obmc", "interintra"] { assert!( !msg.contains(banned), "{NAME} refused on {banned} (seed {seed}) -- that capability is \ ported and this recipe does not need it: {msg}" ); } named_refusals += 1; eprintln!("seed {seed} refusal: {msg}"); continue; } Ok(frames) => frames, }; let ffmpeg_frames = ffmpeg_decode_sequence(&stream, width, height, frame_count); assert_eq!(frames.len(), frame_count); let mismatched = frames .iter() .zip(&ffmpeg_frames) .any(|(got, want)| got.y != want.y || got.u != want.u || got.v != want.v); if mismatched && let Ok(path) = std::env::var("EC_AV1_GATE_DUMP") { std::fs::write(&path, &stream).expect("writing pinned stream"); eprintln!("EC_AV1_GATE_DUMP: wrote mismatching stream (seed {seed}) to {path}"); } for (i, (got, want)) in frames.iter().zip(&ffmpeg_frames).enumerate() { assert_eq!(got.y, want.y, "{NAME} frame {i} luma vs ffmpeg (seed {seed})"); assert_eq!(got.u, want.u, "{NAME} frame {i} U vs ffmpeg (seed {seed})"); assert_eq!(got.v, want.v, "{NAME} frame {i} V vs ffmpeg (seed {seed})"); } matched += 1; } assert!( matched > 0, "{NAME}: every attempt refused; the gate never decoded a free-partition stream" ); // lane-partitions r1: HORZ/VERT arms landed, so the diversity probe // graduates from rectgate r1's eprintln to a report line on // `rect_partition_hits`; a zero-hit run SOFT-reports (aomenc RD may // legitimately never pick a rect split in a small window -- same // sampling caveat as the masked-compound gate's zero-hit runs). // `extended_partition_hits` (HORZ_B) stays unasserted per rectgate r1. if crate::decode::rect_partition_hits() == 0 { eprintln!( "{NAME}: SOFT-NOTE -- zero HORZ/VERT strips fired this run \ ({matched} matches, {named_refusals} refusals); sampling, not a regression" ); } else { // r5: the desync was the tx-depth symbol being read // unconditionally where both square paths gate it on `tx_select` // -- with --enable-tx-size-search=0 the encoder writes no such // symbol and the decoder consumed one that was never there. With // that fixed the real coefficient decode is pixel-exact, so this // is a HARD assert again: strips fired but no coefficients read // would mean the coded path silently stopped being exercised. assert!( crate::decode::rect_coeff_hits() > 0, "{NAME}: {} HORZ/VERT strips fired but rect_coeff_hits==0 -- the \ coded rect path is no longer exercised", crate::decode::rect_partition_hits() ); } eprintln!( "{NAME}: {named_refusals} named refusals, {matched} pixel-exact matches out of {n_attempts}, rect_partition_hits={} rect_coeff_hits={} extended_partition_hits={} partab_hits={}", crate::decode::rect_partition_hits(), crate::decode::rect_coeff_hits(), crate::decode::extended_partition_hits(), crate::decode::partab_hits() ); } /// lane-partab r1: `--enable-ab-partitions=1` (aomenc default-on) INTER /// streams must consume PARTITION_HORZ_A/VERT_A/VERT_B entropy-exact AND /// decode pixel-exact -- `partab_hits() > 0` is soft-skipped on a /// zero-hit run (aomenc RD may legitimately never pick an AB split in a /// small window, same sampling caveat as wedge/rect). The INTER AB /// refusal is FORBIDDEN (decode.rs names it "an INTER 32x32 partition /// ..."); intra-frame AB refusals stay accepted named refusals -- the /// intra dispatch is a later lane. Recipe = the free-partition gate's, /// which fires AB live (r1: partab_hits 6/2/4/3/1/5 over six /// 40-attempt runs). #[test] fn a_real_aomenc_stream_with_ab_partitions_decodes_pixel_exact() { const NAME: &str = "a_real_aomenc_stream_with_ab_partitions_decodes_pixel_exact"; let _gate_lock = lock_gate_counters(); if !have_ffmpeg() { eprintln!("SKIP {NAME}: no ffmpeg"); return; } if !have_aomenc() { eprintln!("SKIP {NAME}: no aomenc at {}", aomenc_path().display()); return; } let (width, height, frame_count) = (64usize, 64usize, 24usize); let mut named_refusals = 0u32; let mut matched = 0u32; let n_attempts: u32 = std::env::var("EC_RECTGATE_ATTEMPTS") .ok() .and_then(|s| s.parse().ok()) .unwrap_or(40); for attempt in 0..n_attempts { let seed = 42 + attempt; let duration = frame_count as f64 / 25.0; let source = gradients_source(seed, width, height, &format!("duration={duration}:rate=25")); let y4m = Command::new("ffmpeg") .args([ "-v", "error", "-f", "lavfi", "-i", &source, "-pix_fmt", "yuv420p", "-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 args: Vec<&str> = vec![ "--codec=av1", "--passes=1", "--end-usage=q", "--cq-level=45", "--cpu-used=0", "--kf-max-dist=1000", "--threads=1", "--row-mt=0", "--auto-alt-ref=1", "--lag-in-frames=16", "--enable-fwd-kf=0", "--enable-order-hint=1", "--enable-warped-motion=1", "--enable-obmc=1", "--tune-content=default", "--enable-masked-comp=0", "--enable-interintra-comp=0", "--enable-onesided-comp=0", "--enable-interintra-wedge=0", "--enable-smooth-interintra=0", // lane-rect r2 recipe, kept byte-identical by lane-partab // r1: free partition search, 1to4 off, intra tools off. "--enable-rect-partitions=1", "--enable-ab-partitions=1", "--enable-1to4-partitions=0", "--enable-filter-intra=0", "--enable-smooth-intra=0", "--enable-paeth-intra=0", "--enable-directional-intra=0", "--enable-angle-delta=0", "--enable-tx-size-search=0", "--enable-cdef=0", "--enable-restoration=0", "--max-partition-size=32", "--min-partition-size=16", "--enable-palette=0", "--enable-intrabc=0", "--enable-cfl-intra=0", "--enable-ref-frame-mvs=0", "--obu", "-o", "-", "-", ]; let mut child = Command::new(aomenc_path()) .args(&args) .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 frames = match decode_stream(&stream) { Err(e) => { let msg = e.to_string(); assert!( msg.contains("unsupported"), "{NAME} failed outright, not a named refusal (seed {seed}): {msg}" ); for banned in ["warp", "obmc", "interintra"] { assert!( !msg.contains(banned), "{NAME} refused on {banned} (seed {seed}) -- that capability is \ ported and this recipe does not need it: {msg}" ); } for v in [4, 6, 7] { assert!( !msg.contains(&format!( "an INTER 32x32 partition type this encoder never writes \ (value={v})" )), "{NAME} refused an INTER AB partition (value={v}, seed {seed}) -- \ all three AB arms are ported, this refusal is forbidden: {msg}" ); } named_refusals += 1; eprintln!("seed {seed} refusal: {msg}"); continue; } Ok(frames) => frames, }; let ffmpeg_frames = ffmpeg_decode_sequence(&stream, width, height, frame_count); assert_eq!(frames.len(), frame_count); for (i, (got, want)) in frames.iter().zip(&ffmpeg_frames).enumerate() { assert_eq!(got.y, want.y, "{NAME} frame {i} luma vs ffmpeg (seed {seed})"); assert_eq!(got.u, want.u, "{NAME} frame {i} U vs ffmpeg (seed {seed})"); assert_eq!(got.v, want.v, "{NAME} frame {i} V vs ffmpeg (seed {seed})"); } matched += 1; } assert!( matched > 0, "{NAME}: every attempt refused; the gate never decoded a stream" ); if crate::decode::partab_hits() == 0 { eprintln!( "{NAME}: SOFT-NOTE -- zero AB partitions fired this run \ ({matched} matches, {named_refusals} refusals); sampling, not a regression" ); } eprintln!( "{NAME}: {named_refusals} named refusals, {matched} pixel-exact matches out of {n_attempts}, partab_hits={}", crate::decode::partab_hits() ); } /// lane-maskcomp r2 / lane-wedge r3: `--enable-masked-comp=1` streams /// must consume `compound_type`/`wedge_idx`/`wedge_sign`/`mask_type` /// entropy-exact AND build the real blend, DIFFWTD or WEDGE -- /// `masked_compound_hits() > 0` is a hard assert and BOTH /// `COMPOUND_DIFFWTD` and `COMPOUND_WEDGE` refusals are now FORBIDDEN /// (matches interintra's own gate). r3: `--enable-dist-wtd-comp=0` /// (forces the masked choice toward wedge when comp_group_idx==1) plus /// a hard-diagonal-edge `geq` source (wedge's OBLIQUE directions favor /// a real diagonal edge over the r2 gradients content) to fire /// COMPOUND_WEDGE live; `wedge_hits()` is soft-skipped (not hard /// asserted) on a zero-hit run -- the recipe search is not proven to /// converge every run, see charter's "if no recipe fires" fallback. #[test] fn a_real_aomenc_stream_with_masked_compound_decodes_pixel_exact() { const NAME: &str = "a_real_aomenc_stream_with_masked_compound_decodes_pixel_exact"; let _gate_lock = lock_gate_counters(); if !have_ffmpeg() { eprintln!("SKIP {NAME}: no ffmpeg"); return; } if !have_aomenc() { eprintln!("SKIP {NAME}: no aomenc at {}", aomenc_path().display()); return; } let (width, height, frame_count) = (64usize, 64usize, 24usize); let mut named_refusals = 0u32; let mut matched = 0u32; let n_attempts: u32 = std::env::var("EC_MASKCOMP_GATE_ATTEMPTS") .ok() .and_then(|s| s.parse().ok()) .unwrap_or(80); for attempt in 0..n_attempts { let seed = 42 + attempt; let duration = frame_count as f64 / 25.0; // lane-wedge r3: a hard diagonal edge (not a smooth gradient) // is the content wedge's OBLIQUE master masks are shaped for -- // `mandelbrot`'s pan position varies the edge angle/position // across attempts the way the r2 gradients seed did. let source = format!( "mandelbrot=size={width}x{height}:rate=25:start_x={sx}:start_y={sy}", sx = -0.6 + 0.005 * (attempt as f64), sy = -0.4 + 0.005 * (attempt as f64) ); let y4m = Command::new("ffmpeg") .args([ "-v", "error", "-f", "lavfi", "-i", &source, "-t", &duration.to_string(), "-pix_fmt", "yuv420p", "-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 args: Vec<&str> = vec![ "--codec=av1", "--passes=1", "--end-usage=q", "--cq-level=45", "--cpu-used=0", "--kf-max-dist=1000", "--threads=1", "--row-mt=0", "--auto-alt-ref=1", "--lag-in-frames=16", "--enable-fwd-kf=0", "--enable-order-hint=1", "--enable-warped-motion=1", // r3: mandelbrot's pan/zoom triggers aomenc's single-ref // GLOBALMV global-motion search (a capability distinct from // and outside this lane's scope) -- off, matching this // gate's own goal of exercising masked compound, not GM. "--enable-global-motion=0", "--enable-obmc=1", "--tune-content=default", "--enable-masked-comp=1", // r3: force the masked choice toward wedge (r2 always set // this =1; distance-weighted compound competes with the // wedge/diffwtd choice for the same comp_group_idx==1 slot). "--enable-dist-wtd-comp=0", "--enable-interintra-comp=1", "--enable-onesided-comp=0", "--enable-interintra-wedge=0", "--enable-smooth-interintra=1", "--enable-rect-partitions=0", "--enable-ab-partitions=0", "--enable-1to4-partitions=0", "--enable-filter-intra=0", "--enable-smooth-intra=0", "--enable-paeth-intra=0", "--enable-directional-intra=0", "--enable-angle-delta=0", "--enable-tx-size-search=0", "--enable-cdef=0", "--enable-restoration=0", "--max-partition-size=32", "--min-partition-size=32", "--enable-palette=0", "--enable-intrabc=0", "--enable-cfl-intra=0", "--enable-ref-frame-mvs=0", "--obu", "-o", "-", "-", ]; let mut child = Command::new(aomenc_path()) .args(&args) .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 frames = match decode_stream(&stream) { Err(e) => { let msg = e.to_string(); assert!( msg.contains("unsupported"), "{NAME} failed outright, not a named refusal (seed {seed}): {msg}" ); assert!( !msg.contains("warp"), "{NAME} refused on warp (seed {seed}) -- warp decode is ported: {msg}" ); assert!( !msg.contains("masked COMPOUND_REFERENCE"), "{NAME} refused a masked-compound block (seed {seed}) -- both DIFFWTD \ and WEDGE blends are ported, this refusal is forbidden: {msg}" ); assert!( !msg.contains("interintra"), "{NAME} refused on interintra (seed {seed}) -- non-wedge \ interintra is ported: {msg}" ); named_refusals += 1; eprintln!("seed {seed} refusal: {msg}"); if std::env::var_os("EC_AV1_GATE_DUMP").is_some() { // r1's self-pin is armed via EC_AV1_GATE_DUMP= // only for a real pixel MISMATCH below, not for this // still-expected named refusal. } continue; } Ok(frames) => frames, }; let ffmpeg_frames = ffmpeg_decode_sequence(&stream, width, height, frame_count); assert_eq!(frames.len(), frame_count); let mismatched = frames .iter() .zip(&ffmpeg_frames) .any(|(got, want)| got.y != want.y || got.u != want.u || got.v != want.v); if mismatched && let Ok(path) = std::env::var("EC_AV1_GATE_DUMP") { std::fs::write(&path, &stream).expect("writing pinned stream"); eprintln!("EC_AV1_GATE_DUMP: wrote mismatching stream (seed {seed}) to {path}"); } for (i, (got, want)) in frames.iter().zip(&ffmpeg_frames).enumerate() { assert_eq!(got.y, want.y, "{NAME} frame {i} luma vs ffmpeg (seed {seed})"); assert_eq!(got.u, want.u, "{NAME} frame {i} U vs ffmpeg (seed {seed})"); assert_eq!(got.v, want.v, "{NAME} frame {i} V vs ffmpeg (seed {seed})"); } matched += 1; } // r1: observed flaky at 40 attempts -- aomenc's cpu-used=0 RD is // run-to-run nondeterministic in whether it ever picks // comp_group_idx == 1 for this synthetic gradients content, and // allow_screen_content_tools sometimes eats most of a small seed // window (class gate-recipe-confound / never-fired-gate, memory). // 80 attempts is the mitigation, not a fix; a still-zero run here // is that sampling flake, not a decode regression -- check // masked_compound_hits/named_refusals in the panic message before // assuming a real bug. // r2: DIFFWTD is ported and gated, so a zero-hit run is no longer // soft-skipped -- hard assert, matching the interintra gate. assert!( crate::decode::masked_compound_hits() > 0, "{NAME}: zero masked-compound blocks fired ({matched} matches, {named_refusals} \ other refusals out of {n_attempts}) -- gate proved nothing this run" ); // r3: wedge_hits() is soft-skipped, not hard-asserted -- the recipe // search (dist-wtd-comp=0 + diagonal mandelbrot content) is not // proven to fire wedge on every run; charter's fallback is to land // the checksum-verified codebook + blend with this soft path. if crate::decode::wedge_hits() == 0 { eprintln!( "{NAME}: WARNING wedge_hits()==0 this run -- COMPOUND_WEDGE never fired \ ({matched} matches, {named_refusals} refusals out of {n_attempts}); codebook \ is checksum-verified but unexercised live this run" ); } eprintln!( "{NAME}: {named_refusals} other-capability refusals, {matched} pixel-exact matches \ out of {n_attempts}, masked_compound_hits={} wedge_hits={}", crate::decode::masked_compound_hits(), crate::decode::wedge_hits() ); } /// Pinned streams captured by `EC_AV1_GATE_DUMP` off /// `a_real_aomenc_stream_with_warped_motion_refuses_or_matches` -- each /// one is a former mismatch, kept as a regression pin (warp-mismatch: /// the WARPED_CAUSAL interp-read suppression; warp-flake-7: the same /// suppression for skip_mode blocks; warp-flake-5: mvstack entry /// clamping; ii-flake-1..8: interintra neighbours excluded from /// warp-sample gathering, ref_frame[1] == INTRA_FRAME in the mi grid). /// `EC_AV1_GATE_DUMP_PIN` overrides to a single stream. /// Deterministic and static -- `#[ignore]`d because the gitignored /// fixtures dir may be absent; run manually. #[test] #[ignore = "reads pinned fixture paths under the gitignored fixtures dir; run manually"] fn pinned_warp_stream_decodes_pixel_exact() { if !have_ffmpeg() { eprintln!("SKIP pinned_warp_stream_decodes_pixel_exact: no ffmpeg"); return; } let fixtures = concat!(env!("CARGO_MANIFEST_DIR"), "/../../fixtures"); let paths: Vec = match std::env::var("EC_AV1_GATE_DUMP_PIN") { Ok(p) => vec![p], Err(_) => [ "warp-mismatch", "warp-flake-5", "warp-flake-7", "ii-flake-1", "ii-flake-2", "ii-flake-3", "ii-flake-5", "ii-flake-6", "ii-flake-7", "ii-flake-8", // switchable_interp missing from reset_counts (counter // saturation slowed the adaptation rate) -- unrelated to // interintra, caught by the same gate. "ii-flake-9", // lane-rect r2: HORZ strip rect defects (mvstack size_h, // warp sample/projection dims, OBMC overlap, deblock tx-h). "rect-flake-1", // scan_row/scan_col weight `inc` min'd by the wrong candidate // axis (width vs height) -- ties reordered DRL entry 1 for a // block under a 32x16 strip, and suppressed the -5 extended // row scan. "rect-flake-2", // overlappable_left stepped the vertical walk by the // neighbour's WIDTH: a 32x16 left strip swallowed the strip // below it, blending the wrong OBMC prediction there. "rect-flake-3", ] .iter() .map(|n| format!("{fixtures}/{n}.obu")) .collect(), }; for path in paths { eprintln!("pin: {path}"); check_pinned_warp_stream(&path); } } fn check_pinned_warp_stream(path: &str) { use crate::decode::warp_selected_hits; let stream = std::fs::read(path).expect("reading pinned stream"); let before = warp_selected_hits(); let frames = decode_stream(&stream).expect("pinned stream must decode"); eprintln!( "warp_selected_hits before={before} after={}", warp_selected_hits() ); let ffmpeg_frames = ffmpeg_decode_sequence(&stream, 64, 64, 24); for (i, (got, want)) in frames.iter().zip(&ffmpeg_frames).enumerate() { if got.y != want.y { for row in 0..64 { let mut line = String::new(); for col in 0..64 { let d = got.y[row * 64 + col] as i32 - want.y[row * 64 + col] as i32; line.push(if d == 0 { '.' } else if d.abs() < 4 { 'o' } else { 'X' }); } eprintln!("{line}"); } } assert_eq!(got.y, want.y, "frame {i} luma vs ffmpeg (pinned)"); assert_eq!(got.u, want.u, "frame {i} U vs ffmpeg (pinned)"); assert_eq!(got.v, want.v, "frame {i} V vs ffmpeg (pinned)"); } } /// `LAST2_FRAME`/`LAST3_FRAME` are just older slots in the same forward /// reference-buffer ring `LAST_FRAME`/`GOLDEN_FRAME` already prove -- /// dropping the golden gate's `--max-reference-frames=3 /// --reduced-reference-set=1` narrowing (which suppresses exactly these) /// and running long enough for aomenc's own buffer rotation to reach /// past the first 2 frames is the whole difference. #[test] fn a_real_aomenc_stream_with_a_last2_reference_decodes_pixel_exact() { a_real_aomenc_single_ref_gate( "a_real_aomenc_stream_with_a_last2_reference_decodes_pixel_exact", crate::mvstack::LAST2_FRAME, "LAST2_FRAME", &["--auto-alt-ref=0", "--lag-in-frames=0"], 64, 64, 8, 120, ); } #[test] fn a_real_aomenc_stream_with_a_last3_reference_decodes_pixel_exact() { a_real_aomenc_single_ref_gate( "a_real_aomenc_stream_with_a_last3_reference_decodes_pixel_exact", crate::mvstack::LAST3_FRAME, "LAST3_FRAME", &["--auto-alt-ref=0", "--lag-in-frames=0"], 64, 64, 8, 120, ); } /// `BWDREF`/`ALTREF2`/`ALTREF` are all backward (display-order-forward) /// references that only exist once aomenc builds a hierarchical GOP -- /// `--auto-alt-ref=1 --lag-in-frames` (need at least one full mini-GOP /// of lookahead) is what makes aomenc code them at all; without it /// every attempt would hit this decoder's own real, still-open /// per-block sign-bias gap by never drawing the reference in the first /// place, not by proving it correct. #[test] fn a_real_aomenc_stream_with_a_bwdref_reference_decodes_pixel_exact() { a_real_aomenc_single_ref_gate( "a_real_aomenc_stream_with_a_bwdref_reference_decodes_pixel_exact", crate::mvstack::BWDREF_FRAME, "BWDREF_FRAME", &[ "--auto-alt-ref=1", "--lag-in-frames=16", "--enable-fwd-kf=0", ], 64, 64, 16, 120, ); } /// lane-altref2 r1: 16 frames was too short for aomenc to ever build a /// second-level alt-ref pyramid, so this gate ran 86 attempts and fired /// ALTREF2 exactly zero times (class `gate-blind-to-feature`) -- the /// flags were already right, only the clip length was wrong. A 64-frame /// clip at the same `--lag-in-frames=16` gives aomenc enough lookahead /// to schedule a 2-level GF pyramid and reliably codes ALTREF2 (4/4 /// hits on a plain seed-42 direct-aomenc probe; see /// lanes/altref2-r1.report.md for the recipe search table). Firing is /// now HARD-asserted below: the gate fails rather than skips if /// `ref_hits(ALTREF2_FRAME)` does not advance. #[test] fn a_real_aomenc_stream_with_an_altref2_reference_decodes_pixel_exact() { let before = decode::ref_hits(crate::mvstack::ALTREF2_FRAME); a_real_aomenc_single_ref_gate( "a_real_aomenc_stream_with_an_altref2_reference_decodes_pixel_exact", crate::mvstack::ALTREF2_FRAME, "ALTREF2_FRAME", &[ "--auto-alt-ref=1", "--lag-in-frames=16", "--enable-fwd-kf=0", ], 64, 64, 64, 120, ); let after = decode::ref_hits(crate::mvstack::ALTREF2_FRAME); assert!( after > before, "a_real_aomenc_stream_with_an_altref2_reference_decodes_pixel_exact: \ ALTREF2_FRAME never fired across 120 attempts -- the gate is vacuous \ again (class gate-blind-to-feature); see lanes/altref2-r1.report.md \ for the recipe that is supposed to make this fire" ); } #[test] fn a_real_aomenc_stream_with_an_altref_reference_decodes_pixel_exact() { a_real_aomenc_single_ref_gate( "a_real_aomenc_stream_with_an_altref_reference_decodes_pixel_exact", crate::mvstack::ALTREF_FRAME, "ALTREF_FRAME", &[ "--auto-alt-ref=1", "--lag-in-frames=16", "--enable-fwd-kf=0", ], 64, 64, 16, 120, ); } /// lane-av1tmvp: the temporal-MV gate -- unlike every `_single_ref_gate` /// attempt above, this one leaves `--enable-ref-frame-mvs` at aomenc's /// own default (on) instead of pinning it off, so `use_ref_frame_mvs` /// actually fires and `decode::tmv_hits()` (this gate's own capability /// proof, not just `non_last_ref_hits`) has to advance for the run to /// count as a real pass rather than a `never_fired` skip. fn a_real_aomenc_temporal_mv_gate(seed_base: u32, attempts: u32) { let gate_name = "a_real_aomenc_temporal_mv_gate"; let _gate_lock = lock_gate_counters(); if !have_ffmpeg() { eprintln!("SKIP {gate_name}: no ffmpeg"); return; } if !have_aomenc() { eprintln!("SKIP {gate_name}: no aomenc at {}", aomenc_path().display()); return; } let (width, height, frame_count) = (64usize, 64usize, 16usize); let mut never_fired = 0u32; let mut refusals = Vec::new(); for attempt in 0..attempts { let seed = seed_base + attempt; let duration = frame_count as f64 / 25.0; let source = gradients_source(seed, width, height, &format!("duration={duration}:rate=25")); let y4m = Command::new("ffmpeg") .args([ "-v", "error", "-f", "lavfi", "-i", &source, "-pix_fmt", "yuv420p", "-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) ); // Same envelope as `a_real_aomenc_single_ref_gate`, minus its // `--enable-ref-frame-mvs=0` pin -- everything else this decoder // still cannot code stays refused the same way. let args: Vec<&str> = vec![ "--codec=av1", "--passes=1", "--end-usage=q", "--cq-level=55", "--cpu-used=0", "--kf-max-dist=1000", "--threads=1", "--row-mt=0", "--auto-alt-ref=1", "--lag-in-frames=16", "--enable-fwd-kf=0", "--enable-warped-motion=0", "--enable-obmc=0", "--enable-masked-comp=0", "--enable-interintra-comp=0", "--enable-dist-wtd-comp=0", "--enable-diff-wtd-comp=0", "--enable-onesided-comp=0", "--enable-interintra-wedge=0", "--enable-smooth-interintra=0", "--enable-rect-partitions=0", "--enable-ab-partitions=0", "--enable-1to4-partitions=0", "--enable-filter-intra=0", "--enable-smooth-intra=0", "--enable-paeth-intra=0", "--enable-directional-intra=0", "--enable-angle-delta=0", "--enable-tx-size-search=0", "--enable-cdef=0", "--enable-restoration=0", "--max-partition-size=32", "--min-partition-size=32", "--enable-palette=0", "--enable-intrabc=0", "--enable-cfl-intra=0", "--obu", "-o", "-", "-", ]; let mut child = Command::new(aomenc_path()) .args(&args) .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::tmv_hits(); let frames = match decode_stream(&stream) { Err(e) => { let msg = e.to_string(); assert!( msg.contains("unsupported"), "{gate_name} failed outright (seed {seed}): {msg}" ); refusals.push(format!("seed {seed}: {msg}")); continue; } Ok(frames) => frames, }; if decode::tmv_hits() == before { never_fired += 1; continue; } let ffmpeg_frames = ffmpeg_decode_sequence(&stream, width, height, frame_count); assert_eq!(frames.len(), frame_count); if let Ok(path) = std::env::var("EC_AV1_GATE_DUMP") { let mismatched = frames .iter() .zip(&ffmpeg_frames) .any(|(got, want)| got.y != want.y || got.u != want.u || got.v != want.v); if mismatched { std::fs::write(&path, &stream).expect("writing pinned stream"); eprintln!("EC_AV1_GATE_DUMP: wrote mismatching stream (seed {seed}) to {path}"); } } for (i, (got, want)) in frames.iter().zip(&ffmpeg_frames).enumerate() { assert_eq!( got.y, want.y, "{gate_name} frame {i} luma vs ffmpeg (seed {seed})" ); assert_eq!( got.u, want.u, "{gate_name} frame {i} U vs ffmpeg (seed {seed})" ); assert_eq!( got.v, want.v, "{gate_name} frame {i} V vs ffmpeg (seed {seed})" ); } eprintln!( "{gate_name} FIRING seed {seed}: tmv_hits advanced by {}", decode::tmv_hits() - before ); return; } eprintln!( "SKIP {gate_name}: {never_fired} attempts decoded but never fired a temporal MV \ candidate, and every other attempt hit a named refusal:\n{}", refusals.join("\n") ); } #[test] fn a_real_aomenc_stream_with_temporal_mvs_decodes_pixel_exact() { a_real_aomenc_temporal_mv_gate(42, 40); } /// bisect scratch: decode a pinned stream exactly once -- generic /// pin-repro tool, kept for manual use. #[test] #[ignore] fn scratch_decode_pinned_stream_once() { let path = std::env::var("EC_AV1_PIN").expect("set EC_AV1_PIN to the .obu path"); let stream = std::fs::read(&path).expect("read pinned stream"); match decode_stream(&stream) { Ok(frames) => eprintln!("OK: {} frames", frames.len()), Err(e) => eprintln!("ERR: {e}"), } } /// scratch: isolate a pinned mismatching stream's first divergent pixel. #[test] #[ignore] fn scratch_isolate_pinned_mismatch() { let path = std::env::var("EC_AV1_PIN").expect("set EC_AV1_PIN to the .obu path"); let stream = std::fs::read(&path).expect("read pinned stream"); { let mut p = Av1Parser::new(); let mut pos = 0usize; while pos < stream.len() { let obu = p.parse_obu(&stream[pos..]).expect("parse"); pos += obu.total_size; if let ObuKind::Frame(header, _) = obu.kind { eprintln!("interpolation_filter: {:?}", header.interpolation_filter); eprintln!("cdef: {:?}", header.cdef); eprintln!("quantization: {:?}", header.quantization); eprintln!( "coded_lossless: {:?} tx_mode: {:?}", header.coded_lossless, header.tx_mode ); eprintln!("delta: {:?}", header.delta); eprintln!("segmentation: {:?}", header.segmentation); eprintln!("loop_restoration: {:?}", header.loop_restoration); eprintln!( "use_128x128_superblock: {:?} disable_cdf_update: {:?} disable_frame_end_update_cdf: {:?} primary_ref_frame: {:?} allow_high_precision_mv: {:?} force_integer_mv: {:?}", p.sequence_header().map(|s| s.use_128x128_superblock), header.disable_cdf_update, header.disable_frame_end_update_cdf, header.primary_ref_frame, header.allow_high_precision_mv, header.force_integer_mv, ); } } } let width = std::env::var("EC_AV1_PIN_W") .map(|s| s.parse().unwrap()) .unwrap_or(64); let height = std::env::var("EC_AV1_PIN_H") .map(|s| s.parse().unwrap()) .unwrap_or(64); let frame_count = std::env::var("EC_AV1_PIN_N") .map(|s| s.parse().unwrap()) .unwrap_or(4); let frames = decode_stream(&stream).expect("our decode"); let ffmpeg_frames = ffmpeg_decode_sequence(&stream, width, height, frame_count); for f in 0..frame_count.min(frames.len()).min(ffmpeg_frames.len()) { let ours = &frames[f]; let theirs = &ffmpeg_frames[f]; for (plane_name, a, b, w) in [ ("y", &ours.y, &theirs.y, width), ("u", &ours.u, &theirs.u, width / 2), ("v", &ours.v, &theirs.v, width / 2), ] { let mut first = None; let mut count = 0; let mut worst = 0i32; for (i, (x, y)) in a.iter().zip(b.iter()).enumerate() { if x != y { count += 1; if first.is_none() { first = Some(i); } worst = worst.max((*x as i32 - *y as i32).abs()); } } if let Some(i) = first { eprintln!( "frame {f} plane {plane_name}: {count} mismatches (worst delta {worst}), first at offset {i} (row {}, col {}) ours={} theirs={}", i / w, i % w, a[i], b[i] ); } else { eprintln!("frame {f} plane {plane_name}: MATCH"); } } } } /// Final round of the TX4/tx-class lane's own gate: `--min-partition-size=8 /// --max-partition-size=8` pins every leaf to exactly 8x8 (spec /// 5.11.4's own boundary carve-out aside, which a multiple-of-8 frame /// never triggers), so a real `aomenc` stream from this recipe can only /// legitimately read `PARTITION_NONE` at every `bsize=3` node -- any /// other value this decoder sees there is a genuine desync, not a /// "partition below 8x8" the encoder actually wrote (traced live against /// a debug `aomdec` 2026-08-27: the true bitstream's own 8x8 partition /// reads are all `NONE`; the mismatch was `base_ctx`'s DC-position /// short-circuit firing for `TX_CLASS_HORIZ`/`VERT` too, off by one /// `BR` read into a spurious extra Golomb call). The sinusoidal-stripe /// `geq` source (a smooth luma ramp, not the sharp two-tone stripes that /// tripped `aomenc`'s screen-content-tools auto-detect) is what actually /// resolves `V_DCT`/`H_DCT` (`TxClass::of`) on some 8x8 TUs at this /// content's frequency; the attempt loop moves on from named refusals /// (`allow_screen_content_tools`, angle-delta) same as every other /// multi-seed recipe in this file, and only counts a run as a genuine /// firing when [`decode::tx_class1_hits`] actually moved. #[test] fn a_real_aomenc_min8_stream_with_tx_class1_decodes_pixel_exact() { if !have_ffmpeg() { eprintln!( "SKIP a_real_aomenc_min8_stream_with_tx_class1_decodes_pixel_exact: no ffmpeg" ); return; } if !have_aomenc() { eprintln!( "SKIP a_real_aomenc_min8_stream_with_tx_class1_decodes_pixel_exact: no aomenc at {}", aomenc_path().display() ); return; } let (width, height) = (64usize, 64usize); let mut refusals = Vec::new(); let mut fired_runs = 0u32; for attempt in 0..60u32 { let cq = 20 + (attempt % 5) * 10; let period = [4u32, 6, 8, 12, 16][(attempt as usize / 5) % 5]; let y4m = Command::new("ffmpeg") .args([ "-v", "error", "-f", "lavfi", "-i", "testsrc2=size=64x64:rate=25", "-vf", &format!( "geq=lum='128+80*sin(2*PI*Y/{period})':cb=128:cr=128,noise=alls=6:allf=t" ), "-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", &format!("--cq-level={cq}"), "--cpu-used=0", "--enable-directional-intra=0", "--enable-smooth-intra=0", "--enable-paeth-intra=0", "--enable-rect-partitions=0", "--enable-angle-delta=0", "--enable-palette=0", "--reduced-tx-type-set=0", "--enable-restoration=0", "--enable-cdef=0", "--loopfilter-control=0", "--min-partition-size=8", "--max-partition-size=8", "--enable-ab-partitions=0", "--enable-1to4-partitions=0", "--enable-filter-intra=0", "--enable-intrabc=0", "--enable-cfl-intra=0", // use_ref_frame_mvs (temporal MV projection) is unimplemented in mvstack; // leaving it on silently desyncs symbols on inter frames (grainfix lesson). "--enable-ref-frame-mvs=0", "--tune-content=default", "--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"); if !out.status.success() { refusals.push(format!( "cq={cq} period={period}: aomenc itself refused the fixture" )); continue; } let stream = out.stdout; let before = decode::tx_class1_hits(); let frames = match decode_stream(&stream) { Ok(frames) => frames, Err(e) => { refusals.push(format!("cq={cq} period={period}: {e}")); continue; } }; if decode::tx_class1_hits() == before { refusals.push(format!( "cq={cq} period={period}: decoded, but no block read a V_DCT/H_DCT tx_type" )); continue; } let ffmpeg_frames = ffmpeg_decode_sequence(&stream, width, height, 1); assert_eq!( frames[0].y, ffmpeg_frames[0].y, "luma vs ffmpeg (cq={cq} period={period})" ); assert_eq!( frames[0].u, ffmpeg_frames[0].u, "U vs ffmpeg (cq={cq} period={period})" ); assert_eq!( frames[0].v, ffmpeg_frames[0].v, "V vs ffmpeg (cq={cq} period={period})" ); fired_runs += 1; if fired_runs >= 4 { return; } } panic!( "fewer than 4 firing+pixel-exact runs out of 60 attempts:\n{}", refusals.join("\n") ); } /// lane-av1golden7 r9's decisive fixture: a real aomenc stream (seed 59, /// `--tune-content=film`, GOLDEN_FRAME firing downstream) whose frame-0 /// *intra keyframe* mismatched ffmpeg on 3855/4096 luma pixels -- traced /// to `apply_grain=true` (spec 7.18.3 grain synthesis, now implemented in /// `crate::film_grain`), entirely unrelated to GOLDEN_FRAME or frame /// ordering. Frames beyond 0 in this fixture hit an unrelated, pre-existing /// gap (`decode_inter_block`'s GOLDEN_FRAME MC path, lane-av1golden7's own /// `golden7-forwarding-mismatch.obu`), so this test truncates the stream to /// the sequence header + frame-0 OBUs only -- exactly the span the /// mismatch was traced to -- rather than reaching into `decode.rs`/`mc.rs` /// to widen MC support, which is out of this lane's scope. #[test] fn a_real_aomenc_stream_with_film_grain_decodes_pixel_exact() { if !have_ffmpeg() { eprintln!("SKIP a_real_aomenc_stream_with_film_grain_decodes_pixel_exact: no ffmpeg"); return; } let data = std::fs::read(concat!( env!("CARGO_MANIFEST_DIR"), "/../../fixtures/golden6-mismatch.obu" )) .unwrap(); // Truncate right after the first `Frame` OBU (the key frame): every // byte up to there is sequence header + frame-0, which is a complete, // independently decodable single-frame stream. let mut parser = Av1Parser::new(); let mut pos = 0usize; let mut frame0_end = None; while pos < data.len() { let obu = parser.parse_obu(&data[pos..]).unwrap(); pos += obu.total_size; if matches!(obu.kind, ObuKind::Frame(..)) { frame0_end = Some(pos); break; } } let data = &data[..frame0_end.expect("fixture has at least one frame OBU")]; let (width, height) = (64usize, 64usize); let ffmpeg_frames = ffmpeg_decode_sequence(data, width, height, 1); let decoded = decode_stream(data).unwrap(); assert_eq!(decoded.len(), 1); let (got, want) = (&decoded[0], &ffmpeg_frames[0]); assert_eq!(got.y, want.y, "frame 0 luma vs ffmpeg (film grain)"); assert_eq!(got.u, want.u, "frame 0 U vs ffmpeg (film grain)"); assert_eq!(got.v, want.v, "frame 0 V vs ffmpeg (film grain)"); } /// A real-encoder sweep, not just the one pinned fixture above /// (fixture-proves-symbol-not-signal class): several fresh /// `--tune-content=film` draws at different seeds/content, each decoded /// pixel-exact against ffmpeg's own AV1 decoder (which synthesizes grain /// by default -- confirmed via `ffmpeg -h decoder=av1`, no /// `-export_side_data`/grain-disable flag is passed anywhere in this /// file). #[test] fn real_aomenc_film_grain_streams_decode_pixel_exact() { if !have_ffmpeg() { eprintln!("SKIP real_aomenc_film_grain_streams_decode_pixel_exact: no ffmpeg"); return; } if !have_aomenc() { eprintln!("SKIP real_aomenc_film_grain_streams_decode_pixel_exact: no aomenc"); return; } let (width, height) = (64usize, 64usize); for seed in [1u64, 2, 3] { let dir = std::env::temp_dir().join(format!( "ec-av1-filmgrain-gate-{}-{seed}", std::process::id() )); std::fs::create_dir_all(&dir).unwrap(); let yuv_path = dir.join("in.yuv"); let obu_path = dir.join("out.obu"); let frames = 3usize; let luma = width * height; let chroma = luma / 4; let mut yuv = Vec::with_capacity((luma + 2 * chroma) * frames); for f in 0..frames { let card = panned_test_card(width, height, ((f as i32 + seed as i32) * 5) as i64); yuv.extend_from_slice(&card.y); yuv.extend_from_slice(&card.u); yuv.extend_from_slice(&card.v); } std::fs::write(&yuv_path, &yuv).unwrap(); let status = Command::new(aomenc_path()) .args([ &format!("--width={width}"), &format!("--height={height}"), "--input-bit-depth=8", "--bit-depth=8", "--fps=25/1", &format!("--limit={frames}"), // The full constraint set of the forwarding/golden gates, // so the streams stay inside this decoder's supported // envelope and the gate isolates grain synthesis alone; // `--tune-content=film` is the one flag under test (it // enables apply_grain). "--codec=av1", "--passes=1", "--end-usage=q", "--cq-level=32", "--cpu-used=0", "--lag-in-frames=0", "--auto-alt-ref=0", "--kf-max-dist=1000", "--threads=1", "--row-mt=0", "--max-reference-frames=3", "--reduced-reference-set=1", "--enable-warped-motion=0", "--enable-obmc=0", "--enable-masked-comp=0", "--enable-interintra-comp=0", "--enable-dist-wtd-comp=0", "--enable-diff-wtd-comp=0", "--enable-onesided-comp=0", "--enable-interintra-wedge=0", "--enable-smooth-interintra=0", "--enable-rect-partitions=0", "--enable-ab-partitions=0", "--enable-1to4-partitions=0", "--enable-filter-intra=0", "--enable-smooth-intra=0", "--enable-paeth-intra=0", "--enable-directional-intra=0", "--enable-angle-delta=0", "--enable-tx-size-search=0", "--enable-cdef=0", "--enable-restoration=0", "--max-partition-size=32", "--min-partition-size=32", "--enable-palette=0", "--enable-intrabc=0", "--enable-cfl-intra=0", // Temporal MV projection (`use_ref_frame_mvs`) has no // decode path here (`mvstack.rs`'s documented corner-cut: // `zero_mv_ctx` and the mode-context GLOBALMV_OFFSET bit // are always computed as if it were off) -- this gate // isolates grain synthesis, so it must stay inside that // envelope like every other feature disabled above, not // silently decode a wrong mode context. Root cause of // this test's frame-1 mismatch: aomenc turns // `use_ref_frame_mvs` on by default once order hints are // enabled, unrelated to `--tune-content=film`. "--enable-ref-frame-mvs=0", "--tune-content=film", "--obu", "-o", ]) .arg(&obu_path) .arg(&yuv_path) .stdout(Stdio::null()) .stderr(Stdio::null()) .status() .expect("aomenc failed to start"); assert!(status.success(), "aomenc failed for seed {seed}"); let encoded = std::fs::read(&obu_path).unwrap(); let _ = std::fs::remove_dir_all(&dir); let mut parser = ec_av1_syntax::Av1Parser::new(); let mut pos = 0usize; let mut saw_grain = false; while pos < encoded.len() { let obu = parser.parse_obu(&encoded[pos..]).unwrap(); pos += obu.total_size; if let ec_av1_syntax::ObuKind::Frame(header, _) = obu.kind && header.film_grain.apply_grain { saw_grain = true; } } assert!(saw_grain, "seed {seed}: aomenc did not turn on apply_grain"); let ffmpeg_frames = ffmpeg_decode_sequence(&encoded, width, height, frames); let decoded = decode_stream(&encoded).unwrap(); assert_eq!(decoded.len(), frames, "seed {seed}"); for (i, (got, want)) in decoded.iter().zip(&ffmpeg_frames).enumerate() { assert_eq!(got.y, want.y, "seed {seed} frame {i} luma vs ffmpeg"); assert_eq!(got.u, want.u, "seed {seed} frame {i} U vs ffmpeg"); assert_eq!(got.v, want.v, "seed {seed} frame {i} V vs ffmpeg"); } } } /// lane-tiles r3: a real two-tile-column aomenc key frame, decoded /// through [`decode_key_frame_tile_with_cdfs`] directly with the whole /// `tiles: &[&[u8]]` list -- `decode_stream`'s `cols > 1 || rows > 1` /// refusal (this file, ~line 292) still fires for any stream this wide, /// so this gate bypasses it on purpose to prove the per-tile loop r2 /// landed (fresh `SymbolDecoder`/CDF copy per tile, `Neighbours::start_tile`, /// `PlaneBuf::set_tile_origin`) actually decodes both tiles pixel-exact /// before that refusal is lifted. 128x64 is exactly two 64x64 /// superblocks wide with `--tile-columns=1` (`TileCols=2`), so tile 1 is /// also the frame's own right edge -- the known gap (`PlaneBuf`'s /// tile_x0/tile_y0 only clip left/top reach, not a non-last column's /// right-edge reach) does not apply to this fixture; it would to a /// 3+-column one. Tile count itself is a structural aomenc param, not /// RD-dependent content, but this decoder's chroma smooth/paeth gap /// (`decode.rs` "a smooth or paeth chroma mode (round 2)", unrelated to /// tiles, pre-dating this lane) is content-dependent -- no aomenc flag /// disables UV-only smooth/paeth search, so a small seed sweep is /// needed the same way the sibling `a_real_aomenc_stream_with_*` gates /// retry past unrelated named refusals (every refusal in this crate is /// prefixed `"AV1 tile"` regardless of category, so unlike the sibling /// gates' `warp`/`obmc`/`interintra` banned lists there is no /// tile-specific substring to forbid here -- `matched > 0` plus the /// hard `tile_hits > 1`/pixel-exact asserts on any actual decode are /// the real proof). #[test] fn a_real_aomenc_stream_with_two_tile_columns_decodes_pixel_exact() { const NAME: &str = "a_real_aomenc_stream_with_two_tile_columns_decodes_pixel_exact"; if !have_ffmpeg() { eprintln!("SKIP {NAME}: no ffmpeg"); return; } if !have_aomenc() { eprintln!("SKIP {NAME}: no aomenc at {}", aomenc_path().display()); return; } let (width, height) = (128usize, 64usize); let mut matched = 0u32; let mut named_refusals = 0u32; for attempt in 0..20u32 { let seed = 42 + attempt; let source = gradients_source(seed, width, height, "duration=0.04:rate=25"); let y4m = Command::new("ffmpeg") .args([ "-v", "error", "-f", "lavfi", "-i", &source, "-pix_fmt", "yuv420p", "-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", "--limit=1", "--kf-max-dist=1000", "--threads=1", "--row-mt=0", "--tile-columns=1", "--tile-rows=0", // This decoder hardcodes SB_MI=16 (64px) superblocks // everywhere (`decode.rs`'s sb128 dead-end, ledger // `av1-superblock-contexts`) -- without this, aomenc's // own default 128x128 superblock makes 128x64 exactly // ONE superblock wide, so `--tile-columns=1` has nothing // to split and `cols` comes back 1. "--sb-size=64", // Deblocking crosses tile boundaries by default (spec // `loop_filter_across_tiles_enabled`); `PlaneBuf`'s // tile_x0/tile_y0 origin only ever clips a tile's own // left/top reach (r2/r3's documented known gap), so a // non-last tile column's right-edge deblock would read // into the next tile's still-undecoded/differently- // origined samples. Off here since deblocking itself is // not what this gate is proving. "--loopfilter-control=0", "--enable-rect-partitions=0", "--enable-ab-partitions=0", "--enable-1to4-partitions=0", "--enable-filter-intra=0", "--enable-smooth-intra=0", "--enable-paeth-intra=0", "--enable-directional-intra=0", "--enable-angle-delta=0", "--enable-tx-size-search=0", "--enable-cdef=0", "--enable-restoration=0", "--max-partition-size=32", "--min-partition-size=32", "--enable-palette=0", "--enable-intrabc=0", "--enable-cfl-intra=0", "--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; // Parse the stream ourselves (rather than `decode_stream`, whose // multi-tile refusal is exactly what this gate exists to run // ahead of) to reach the real `Frame` OBU's `tile_info` and its // full `Vec` -- `Av1Parser::parse_obu` already locates // every tile in a multi-tile tile group (`parse_tile_group`), it // is only `decode_stream` that ever narrows this to // `tiles.first()`. let mut parser = Av1Parser::new(); let mut pos = 0usize; let mut frame = None; while pos < stream.len() { let obu_offset = pos; let obu = parser.parse_obu(&stream[pos..]).unwrap(); pos += obu.total_size; if let ObuKind::Frame(header, tiles) = obu.kind { frame = Some((header, tiles, obu_offset)); break; } } let (header, tiles, obu_offset) = frame.expect("stream has a Frame OBU"); assert!( header.tile_info.cols > 1, "{NAME}: aomenc did not actually split into >1 tile column (cols={}, seed {seed})", header.tile_info.cols ); assert_eq!( tiles.len(), header.tile_info.cols as usize, "{NAME}: expected one tile per column (seed {seed})" ); // `Tile::offset` is relative to the buffer `parse_obu` was // handed (`&stream[pos..]` at the time this OBU was parsed), so // it needs `obu_offset` added back -- the same adjustment // `decode_stream` makes for its own single-tile slice above. let tile_bufs: Vec<&[u8]> = tiles .iter() .map(|t| &stream[obu_offset + t.offset..obu_offset + t.offset + t.size]) .collect(); let enable_filter_intra = parser .sequence_header() .is_some_and(|seq| seq.enable_filter_intra); let enable_edge_filter = parser .sequence_header() .is_some_and(|seq| seq.enable_intra_edge_filter); let before = crate::decode::tile_hits(); let picture = match decode_key_frame_tile_with_cdfs( &tile_bufs, &header.tile_info, header.mi_cols, header.mi_rows, header.quantization.base_q_idx, header.frame_width, header.frame_height, enable_filter_intra, enable_edge_filter, &header.cdef, &header.loop_filter, None, header.tx_mode == TxMode::Select, header.reduced_tx_set, header.allow_screen_content_tools, header.allow_intrabc, ) { Err(e) => { let msg = e.to_string(); assert!( msg.contains("unsupported"), "{NAME} failed outright, not a named refusal (seed {seed}): {msg}" ); named_refusals += 1; eprintln!("seed {seed} refusal: {msg}"); continue; } Ok((picture, _end_cdfs)) => picture, }; let hits = crate::decode::tile_hits() - before; assert!( hits > 1, "{NAME}: tile_hits delta was {hits}, expected >1 -- both tiles must actually \ decode, not just tile 0 (seed {seed})" ); let ffmpeg_frames = ffmpeg_decode_sequence(&stream, width, height, 1); assert_eq!(picture.y, ffmpeg_frames[0].y, "{NAME}: luma vs ffmpeg (seed {seed})"); assert_eq!(picture.u, ffmpeg_frames[0].u, "{NAME}: U vs ffmpeg (seed {seed})"); assert_eq!(picture.v, ffmpeg_frames[0].v, "{NAME}: V vs ffmpeg (seed {seed})"); matched += 1; } assert!( matched > 0, "{NAME}: every attempt refused ({named_refusals} refusals); the gate never decoded \ a two-tile-column stream" ); eprintln!( "{NAME}: {matched} pixel-exact matches, {named_refusals} named refusals out of 20" ); } }