edith_codecs
Clone
https://git.tahinli.com/Tahinli/edith_codecs.git
0 watching·0 forks·Rust·370.3 MiB·a5441e8·Tahinli·3 months ago·1014 commits
| 1 | //! The stream-level entry point a real decoder needs: walk a raw AV1 OBU |
| 2 | //! stream (`Encoded::stream`, or any low-overhead-format bitstream this |
| 3 | //! crate's own writers produce) via [`ec_av1_syntax::Av1Parser`] and dispatch |
| 4 | //! each frame's tile payload to [`crate::decode::decode_key_frame_tile_with_cdfs`] / |
| 5 | //! [`crate::decode::decode_inter_frame_tile_with_cdfs`], threading a |
| 6 | //! single-slot picture DPB exactly as [`crate::encode::encode_sequence`] does |
| 7 | //! on the write side, plus the full 8-slot *CDF* reference bank spec 7.20 |
| 8 | //! (`load_cdfs`) and 7.4 (the reference frame update) describe: a frame whose |
| 9 | //! header names `primary_ref_frame != PRIMARY_REF_NONE` resumes from the |
| 10 | //! adapted table a previous frame's `refresh_frame_flags` left in that slot, |
| 11 | //! rather than the spec 8.4 defaults. |
| 12 | //! |
| 13 | //! This is the only reachable path for a caller that does not have the |
| 14 | //! encoder's own `Encoded::tile`/`mi_cols`/`mi_rows`/`base_q_idx` fields (they |
| 15 | //! are `pub(crate)`, and rightly so: the wire is `stream`, everything else is |
| 16 | //! an implementation detail of how this crate happens to build it). |
| 17 | |
| 18 | use ec_av1_syntax::{ |
| 19 | Av1Parser, FrameType, NUM_REF_FRAMES, ObuKind, PRIMARY_REF_NONE, TxMode, WarpModel, |
| 20 | }; |
| 21 | use ec_core::{Error, Result}; |
| 22 | |
| 23 | use crate::cdf_state::Cdfs; |
| 24 | use crate::decode; |
| 25 | use crate::decode::{ |
| 26 | decode_inter_frame_tile_with_cdfs, decode_key_frame_tile, decode_key_frame_tile_with_cdfs, |
| 27 | q_ctx_of, |
| 28 | }; |
| 29 | use crate::encode::Picture; |
| 30 | |
| 31 | /// Decode every frame in a raw AV1 OBU stream, in coding order. |
| 32 | /// |
| 33 | /// A key frame becomes the reference for the inter frames that follow it, |
| 34 | /// until the next key frame resets the DPB — the same single-slot chain |
| 35 | /// [`crate::encode::encode_sequence`] threads on the write side. Only a |
| 36 | /// `Frame` OBU (an uncompressed header and its tile group in one OBU, which |
| 37 | /// is all this crate's encoder ever writes) carries a tile payload; sequence |
| 38 | /// headers and temporal delimiters are consumed for the state they carry (or |
| 39 | /// skipped) and produce no picture. |
| 40 | /// |
| 41 | /// # Errors |
| 42 | /// Returns an error when the stream is truncated or malformed (as |
| 43 | /// [`Av1Parser`] reports it), when a frame header names anything this |
| 44 | /// crate's tile decoders do not reconstruct (see their own docs), or when an |
| 45 | /// inter frame appears before any key frame has supplied a reference. |
| 46 | pub fn decode_stream(data: &[u8]) -> Result<Vec<Picture>> { |
| 47 | let mut parser = Av1Parser::new(); |
| 48 | let mut pictures = Vec::new(); |
| 49 | let mut pictures_decoded: usize = 0; |
| 50 | // Spec 7.20/7.4: each of the 8 reference slots remembers the picture a |
| 51 | // previous frame's `refresh_frame_flags` stored into it, exactly like the |
| 52 | // CDF slots below -- `LAST_FRAME` names a slot via `ref_frame_idx[0]`, |
| 53 | // not simply "the previous frame decoded"; a frame whose own predecessor |
| 54 | // didn't refresh that slot (an altref/hidden frame, or any GOP with more |
| 55 | // than one live reference) must keep predicting from whatever picture is |
| 56 | // still sitting there. Round 7's cdffwd hunt: a global `reference` |
| 57 | // overwritten every frame silently always used "the most recent decode" |
| 58 | // instead, producing a bit-exact motion vector into the WRONG picture -- |
| 59 | // luma-only (chroma coincidentally shares this fixture's flat regions) |
| 60 | // and small-magnitude (the two pictures mostly agree), which is exactly |
| 61 | // what made it look like a rounding bug rather than a wrong reference. |
| 62 | let mut ref_slots: [Option<Picture>; NUM_REF_FRAMES] = std::array::from_fn(|_| None); |
| 63 | // lane-av1tmvp: each of the 8 reference slots' own saved temporal motion |
| 64 | // field (spec 7.9's per-frame `MotionFieldMvs` storage, libaom |
| 65 | // `cur_frame->mvs`) plus the `OrderHint` the picture in that slot was |
| 66 | // decoded with -- `Picture` itself carries neither, so both ride |
| 67 | // alongside `ref_slots` on the exact same `refresh_frame_flags` update. |
| 68 | let mut motion_field_slots: [Option<crate::motion_field::MotionField>; NUM_REF_FRAMES] = |
| 69 | std::array::from_fn(|_| None); |
| 70 | let mut order_hint_slots: [u32; NUM_REF_FRAMES] = [0; NUM_REF_FRAMES]; |
| 71 | // Spec 7.20/7.4: each of the 8 reference slots remembers the CDF state a |
| 72 | // previous frame's `refresh_frame_flags` stored into it -- the frame's |
| 73 | // own end-of-tile adapted table, or (when the frame set |
| 74 | // `disable_frame_end_update_cdf`) the table it started from, unchanged. |
| 75 | let mut cdf_slots: [Option<Cdfs>; NUM_REF_FRAMES] = std::array::from_fn(|_| None); |
| 76 | |
| 77 | let mut pos = 0usize; |
| 78 | while pos < data.len() { |
| 79 | let obu = parser.parse_obu(&data[pos..])?; |
| 80 | let obu_offset = pos; |
| 81 | pos += obu.total_size; |
| 82 | |
| 83 | if let ObuKind::FrameHeader(header) = &obu.kind { |
| 84 | // `show_existing_frame` (spec 7.21/5.9.2): no tile payload, this |
| 85 | // OBU only names a DPB slot to output again -- the coded-frame |
| 86 | // count and the shown-picture count are different things (a |
| 87 | // hidden altref's own `Frame` OBU below is never pushed to |
| 88 | // `pictures` on its own; it is only pushed here, later, when a |
| 89 | // `show_existing_frame` header names its slot). Before r15 this |
| 90 | // arm didn't exist at all: every `ObuKind::Frame` was pushed |
| 91 | // unconditionally regardless of `show_frame`, so a hidden altref |
| 92 | // was emitted (wrongly, in decode order) and its real |
| 93 | // `show_existing_frame` output was silently dropped. |
| 94 | let slot = header.frame_to_show_map_idx as usize; |
| 95 | let picture = ref_slots[slot].clone().ok_or_else(|| { |
| 96 | Error::unsupported( |
| 97 | "AV1 decode_stream", |
| 98 | "a show_existing_frame header naming an empty reference slot", |
| 99 | ) |
| 100 | })?; |
| 101 | for i in 0..NUM_REF_FRAMES { |
| 102 | if header.refresh_frame_flags & (1 << i) != 0 { |
| 103 | cdf_slots[i] = cdf_slots[slot].clone(); |
| 104 | ref_slots[i] = Some(picture.clone()); |
| 105 | motion_field_slots[i] = motion_field_slots[slot].clone(); |
| 106 | order_hint_slots[i] = order_hint_slots[slot]; |
| 107 | } |
| 108 | } |
| 109 | let output = if header.film_grain.apply_grain { |
| 110 | let mc_identity = parser |
| 111 | .sequence_header() |
| 112 | .is_some_and(|seq| seq.color_config.matrix_coefficients == 0); |
| 113 | crate::film_grain::apply_grain(&picture, &header.film_grain, mc_identity) |
| 114 | } else { |
| 115 | picture |
| 116 | }; |
| 117 | pictures.push(output); |
| 118 | continue; |
| 119 | } |
| 120 | let ObuKind::Frame(header, tiles) = obu.kind else { |
| 121 | continue; |
| 122 | }; |
| 123 | let tile = tiles.first().ok_or_else(|| { |
| 124 | Error::unsupported("AV1 decode_stream", "a frame OBU with no tile group") |
| 125 | })?; |
| 126 | // `read_cdef` (spec `decodeframe.c`, called at the first non-skip |
| 127 | // block of each 64x64) only reads a literal `cdef_idx` when |
| 128 | // `cdef_bits > 0`; at `cdef_bits == 0` there is nothing to read (this |
| 129 | // crate's own writer's only case, and both `lane-av1cdef` gate |
| 130 | // streams'), so the per-block symbol this crate's tile readers never |
| 131 | // consume is a true no-op there. A `cdef_bits > 0` stream would |
| 132 | // desync the moment it hit a real strength selector this decoder |
| 133 | // never reads — refuse that by name rather than silently miscode, |
| 134 | // the same pattern as the other round-2 refusals below. |
| 135 | if header.cdef.bits != 0 { |
| 136 | return Err(Error::unsupported( |
| 137 | "AV1 decode_stream", |
| 138 | "a frame with cdef_bits > 0 (this decoder never reads the per-64x64 cdef_idx symbol)", |
| 139 | )); |
| 140 | } |
| 141 | // An intra frame's `decode_key_frame_tile*` now reads `tx_depth` |
| 142 | // under `TxMode::Select` (lane-av1txsel, spec 5.11.16): the key-frame |
| 143 | // decode path threads `tx_select` through `decode_block`/ |
| 144 | // `decode_leaf8`, and refuses on its own (a named `unsupported`, not |
| 145 | // a silent desync) the one case it still cannot code -- a resolved |
| 146 | // 4x4 luma transform, which has no coefficient tables here. This |
| 147 | // refusal is narrowed to what genuinely still has no reader at all: |
| 148 | // `decode_inter_frame`'s inter path never threads `tx_select` |
| 149 | // through its own block loop, so an inter frame under |
| 150 | // `TxMode::Select` still desyncs the same way the comment above used |
| 151 | // to describe for every frame -- lane-av1real r3's |
| 152 | // luma-near-flat-vs-ffmpeg-gradient bug. |
| 153 | if header.tx_mode == TxMode::Select && header.frame_type != FrameType::Key { |
| 154 | return Err(Error::unsupported( |
| 155 | "AV1 decode_stream", |
| 156 | "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)", |
| 157 | )); |
| 158 | } |
| 159 | // lane-av1real r11's blindness-audit sweep (`read_intra_frame_mode_info`, |
| 160 | // libaom decodemv.c): three more per-block/per-SB symbols this crate |
| 161 | // never reads at all, each gated by a header field this decoder |
| 162 | // otherwise accepts uncomplaining -- the same silent-desync shape |
| 163 | // `use_filter_intra` was. `allow_screen_content_tools` gates both |
| 164 | // `read_intrabc_info` and `read_palette_mode_info`; `delta.q_present`/ |
| 165 | // `delta.lf_present` gate `read_delta_qindex`/`read_delta_lflevel` |
| 166 | // (spec 5.11.10/5.11.11, one symbol group per superblock). Refuse |
| 167 | // before desyncing rather than silently miscode, the same pattern as |
| 168 | // the CDEF/TxMode::Select refusals above. |
| 169 | // lane-screen r2: key frames consume (not reconstruct) palette/ |
| 170 | // intrabc syntax via read_intra_mode, and decode_inter_block/ |
| 171 | // decode_inter_block8's own intra-sub-block branches now consume |
| 172 | // palette the same way (libaom's read_intra_block_mode_info has no |
| 173 | // intrabc call at all -- that symbol is intra-frame-only). A |
| 174 | // genuine palette/intrabc use still refuses by name deeper in the |
| 175 | // block readers, so no whole-frame refusal is needed here anymore. |
| 176 | if header.delta.q_present || header.delta.lf_present { |
| 177 | return Err(Error::unsupported( |
| 178 | "AV1 decode_stream", |
| 179 | "a frame with delta_q_present or delta_lf_present set (this decoder never reads the per-superblock delta symbols)", |
| 180 | )); |
| 181 | } |
| 182 | if header.segmentation.enabled { |
| 183 | return Err(Error::unsupported( |
| 184 | "AV1 decode_stream", |
| 185 | "a frame with segmentation enabled (this decoder never reads a per-block segment_id symbol)", |
| 186 | )); |
| 187 | } |
| 188 | // Loop restoration carries per-restoration-unit symbols in the tile |
| 189 | // (spec 5.11.57 `read_lr`, one group per LR unit reached from the |
| 190 | // superblock walk) that this decoder never reads -- same |
| 191 | // silent-desync shape as the refusals above. Traced live 2026-08-27: |
| 192 | // an aomenc inter frame with `Sgrproj` on the V plane desynced the |
| 193 | // partition walk into out-of-alphabet garbage. |
| 194 | if header.loop_restoration.uses_lr { |
| 195 | return Err(Error::unsupported( |
| 196 | "AV1 decode_stream", |
| 197 | "a frame with loop restoration enabled (this decoder never reads the per-unit lr symbols)", |
| 198 | )); |
| 199 | } |
| 200 | // `decode_inter_block`/`decode_inter_block8`'s `GLOBALMV` arm (spec |
| 201 | // 5.11.26's `read_inter_intra`... really `assign_mv`'s `GLOBALMV` |
| 202 | // case, spec 7.10.2.1) uses `gm_get_motion_vector`, which is the |
| 203 | // zero vector only under `GmType == IDENTITY` -- any other warp |
| 204 | // model needs the full affine/translation MV derivation this |
| 205 | // decoder does not carry. `global_motion` is indexed `ref_frame - 1` |
| 206 | // (`read_global_motion_params`'s `i` loops from `LAST_FRAME`); |
| 207 | // lane-av1refs widens this from `[0, 3]` (LAST_FRAME/GOLDEN_FRAME) |
| 208 | // to every one of the 7 single-reference slots `decode_inter_block` |
| 209 | // can now select. |
| 210 | // |
| 211 | // lane-gm r2 STEP-8 ATTEMPT (reverted): removing this refusal |
| 212 | // exposed a real pixel mismatch -- |
| 213 | // a_real_aomenc_stream_with_interintra_wedge_decodes_pixel_exact |
| 214 | // seed 43, frame 1 luma, with EC_WEDGE_GATE_ATTEMPTS=20 |
| 215 | // EC_AV1_REQUIRE_AOMENC=1 -- so the single-ref/compound MV wiring |
| 216 | // is not yet provably correct end to end; keeping the refusal |
| 217 | // until that seed is captured (EC_AV1_GATE_DUMP), self-pinned, and |
| 218 | // root-caused rather than shipping a wrong decode. |
| 219 | // |
| 220 | // lane-gm r3 ROOT CAUSE (pinned, range-ladder-confirmed, still not |
| 221 | // fixed): the mismatch is NOT an mv/mvstack/entropy bug -- msac |
| 222 | // RANGE at the first divergent block (mi=(8,0), frame 1) matches |
| 223 | // aomdec's own `EC_PART` trace bit-for-bit (rng=38664 both sides), |
| 224 | // and this decoder's own `gm_get_motion_vector` for that block |
| 225 | // (ROTZOOM, seed 43's own header) computes the spec-correct |
| 226 | // mv=(-8,8) (hand-verified against mv.h's `block_center_x`/`_y`, |
| 227 | // which found and fixed a real `- 1` omission in `warp.rs` -- |
| 228 | // harmless for THIS block's rounding but a genuine spec bug fixed |
| 229 | // regardless). The actual gap: `reconinter.c`'s `allow_warp` has a |
| 230 | // `global_warp_allowed` branch (`warp_types.global_warp_allowed = |
| 231 | // is_global_mv_block(...)`, `reconinter_enc.c:281`) that predicts |
| 232 | // an `is_global_mv_block` block (ROTZOOM/AFFINE GLOBALMV, >=8x8) |
| 233 | // with the FULL per-pixel affine global-motion warp, independent |
| 234 | // of `motion_mode` -- this decoder only ever builds a warp |
| 235 | // prediction for local `WARPED_CAUSAL` motion mode and falls back |
| 236 | // to plain translational MC with the block-centre mv for every |
| 237 | // other GLOBALMV block, which is only a centre-point approximation |
| 238 | // of the true per-pixel warp. That is the entire remaining gap. |
| 239 | // lane-gm r4: `decode.rs` now feeds `global_motion[ref]` into |
| 240 | // `crate::warp::global_warp_params` for every single-ref |
| 241 | // `is_global_mv_block` (ROTZOOM/AFFINE), matching `allow_warp`'s |
| 242 | // `global_warp_allowed` branch -- ROTZOOM is pin-verified |
| 243 | // (`gm-seed43.obu`, frame 1 luma MATCH) and TRANSLATION already |
| 244 | // worked (`gm_get_motion_vector`'s translation-only mv, no warp |
| 245 | // needed). AFFINE is untested this round (no aomenc fixture in the |
| 246 | // wedge gate reached a 6-parameter model) -- keep refusing it by |
| 247 | // name rather than shipping an unverified decode. |
| 248 | if header.frame_type != FrameType::Key |
| 249 | && header.global_motion[..7] |
| 250 | .iter() |
| 251 | .any(|gm| gm.model == WarpModel::Affine) |
| 252 | { |
| 253 | return Err(Error::unsupported( |
| 254 | "AV1 decode_stream", |
| 255 | "an inter frame whose global motion for a single-reference frame is AFFINE (unverified this round; ROTZOOM/TRANSLATION/IDENTITY are proven)", |
| 256 | )); |
| 257 | } |
| 258 | // lane-gm r4/r5/r6: r4 found a NEW mismatch on frames with two |
| 259 | // concurrently active ROTZOOM/AFFINE ref slots (GOLDEN + BWDREF) and |
| 260 | // refused the shape by name rather than guess-fix it. r5 localized |
| 261 | // the mismatch to an ordinary NEWMV/GOLDEN block, not the new |
| 262 | // global-warp branch at all -- proving "two active slots" was a |
| 263 | // correlated symptom, not the cause. r6 range-laddered that block |
| 264 | // against aomdec's `EC_TRACE_MODE` (identical `rng` before/after |
| 265 | // mode info -- symbol consumption bit-exact) and found the real |
| 266 | // defect in `mvstack.rs`'s single-reference predictor fallback: |
| 267 | // libaom's `setup_ref_mv_list` fills any `mv_ref_list` slot the real |
| 268 | // neighbour scan left short with this block's OWN global motion |
| 269 | // vector for its ref (`gm_get_motion_vector`), not zero -- invisible |
| 270 | // whenever the live ref's global motion is itself |
| 271 | // identity/translation, wrong the moment it's a real ROTZOOM/AFFINE |
| 272 | // and the querying block's own stack comes up short (frame 14's |
| 273 | // mi=(0,0), the very first block decoded, has no neighbours at |
| 274 | // all). Fixed at the predictor fallback; the refusal above (single |
| 275 | // untested shape: AFFINE on a single-ref frame) stays, but the |
| 276 | // *multi-slot* refusal this comment used to guard is lifted -- the |
| 277 | // r5/r6 pin (seed-43 wedge-gate mismatch) now decodes all 24 frames |
| 278 | // byte-exact with it gone. |
| 279 | // lane-av1comp: `decode_inter_block`/`decode_inter_block8` now read |
| 280 | // `comp_mode` per block whenever this frame's own `reference_select` |
| 281 | // header bit is set (spec 5.11.25), and refuse by name the blocks |
| 282 | // that pick `COMPOUND_REFERENCE` -- two-reference motion |
| 283 | // compensation is not wired yet. Round 14: `skip_mode` (spec 5.9.22) |
| 284 | // is wired at the block level too (forced NEAREST_NEARESTMV compound |
| 285 | // of `skip_mode_frame`, plain average blend), so the old blanket |
| 286 | // `skip_mode_present` refusal is gone. |
| 287 | // `context_update_tile_id` (spec `decode_tile`/`exit_symbol`) names |
| 288 | // which tile's end-of-tile adapted CDFs become the frame's own; this |
| 289 | // decoder only ever decodes tile 0 (`tiles.first()` above), so a |
| 290 | // multi-tile frame is refused rather than silently forwarding the |
| 291 | // wrong tile's table (or leaving the rest of the picture undecoded). |
| 292 | if header.tile_info.cols > 1 || header.tile_info.rows > 1 { |
| 293 | return Err(Error::unsupported( |
| 294 | "AV1 decode_stream", |
| 295 | "a frame with more than one tile (this decoder only ever decodes tile 0)", |
| 296 | )); |
| 297 | } |
| 298 | // `Tile::offset` is relative to the buffer `parse_obu` was handed |
| 299 | // (`&data[pos..]` at the time this OBU was parsed), so it is relative |
| 300 | // to `obu_offset`, not to `data` as a whole. |
| 301 | let start = obu_offset + tile.offset; |
| 302 | let tile_bytes = data.get(start..start + tile.size).ok_or(Error::NeedMore)?; |
| 303 | let enable_filter_intra = parser |
| 304 | .sequence_header() |
| 305 | .is_some_and(|seq| seq.enable_filter_intra); |
| 306 | let enable_dual_filter = parser |
| 307 | .sequence_header() |
| 308 | .is_some_and(|seq| seq.enable_dual_filter); |
| 309 | let enable_edge_filter = parser |
| 310 | .sequence_header() |
| 311 | .is_some_and(|seq| seq.enable_intra_edge_filter); |
| 312 | // Inter frames' intra blocks read this through a thread-local that only |
| 313 | // the key-frame tile path used to set; without this call a stream whose |
| 314 | // inter frames are decoded on a thread that previously decoded a |
| 315 | // different sequence would inherit that sequence's bit. |
| 316 | crate::decode::set_enable_edge_filter(enable_edge_filter); |
| 317 | // lane-av1comp: `comp_group_idx`/`compound_idx`'s own gating bits. |
| 318 | let enable_masked_compound = parser |
| 319 | .sequence_header() |
| 320 | .is_some_and(|seq| seq.enable_masked_compound); |
| 321 | let enable_jnt_comp = parser |
| 322 | .sequence_header() |
| 323 | .is_some_and(|seq| seq.enable_jnt_comp); |
| 324 | // lane-sb128 r4: `interintra`'s own gating bit -- see decode.rs's |
| 325 | // read site doc. |
| 326 | let enable_interintra_compound = parser |
| 327 | .sequence_header() |
| 328 | .is_some_and(|seq| seq.enable_interintra_compound); |
| 329 | let interp_fixed = match header.interpolation_filter { |
| 330 | ec_av1_syntax::InterpolationFilter::Switchable => None, |
| 331 | fixed => Some(crate::mc::InterpFilterKind::from_header(fixed)), |
| 332 | }; |
| 333 | // Spec 7.20 `load_cdfs`: a frame naming a `primary_ref_frame` resumes |
| 334 | // from that reference slot's saved CDF state instead of the spec 8.4 |
| 335 | // defaults `decode_key/inter_frame_tile_with_cdfs`'s `None` case |
| 336 | // builds. |
| 337 | let initial_cdfs = if header.primary_ref_frame == PRIMARY_REF_NONE { |
| 338 | None |
| 339 | } else { |
| 340 | let slot = header.ref_frame_idx[header.primary_ref_frame as usize] as usize; |
| 341 | Some(cdf_slots[slot].clone().ok_or_else(|| { |
| 342 | Error::unsupported( |
| 343 | "AV1 decode_stream", |
| 344 | "a frame naming primary_ref_frame at a reference slot with no saved CDF state", |
| 345 | ) |
| 346 | })?) |
| 347 | }; |
| 348 | let started_from = initial_cdfs.clone(); |
| 349 | |
| 350 | let order_hint_bits = parser |
| 351 | .sequence_header() |
| 352 | .map_or(0, |seq| seq.order_hint_bits); |
| 353 | // spec 7.9/7.20: this frame's own `ref_order_hints` -- the OrderHint |
| 354 | // of the picture sitting in each of its 7 single references' DPB |
| 355 | // slots, for this frame's *own* saved [`crate::motion_field::MotionField`] |
| 356 | // (a later frame projects from it) whether or not this frame itself |
| 357 | // reads temporal candidates. |
| 358 | let ref_order_hints: [u32; 7] = |
| 359 | std::array::from_fn(|i| order_hint_slots[header.ref_frame_idx[i] as usize]); |
| 360 | |
| 361 | let (picture, end_cdfs, motion_field) = if header.frame_type == FrameType::Key { |
| 362 | let (picture, end_cdfs) = decode_key_frame_tile_with_cdfs( |
| 363 | &[tile_bytes], |
| 364 | &header.tile_info, |
| 365 | header.mi_cols, |
| 366 | header.mi_rows, |
| 367 | header.quantization.base_q_idx, |
| 368 | header.frame_width, |
| 369 | header.frame_height, |
| 370 | enable_filter_intra, |
| 371 | enable_edge_filter, |
| 372 | &header.cdef, |
| 373 | &header.loop_filter, |
| 374 | initial_cdfs, |
| 375 | header.tx_mode == TxMode::Select, |
| 376 | header.reduced_tx_set, |
| 377 | header.allow_screen_content_tools, |
| 378 | header.allow_intrabc, |
| 379 | )?; |
| 380 | // A key frame codes no inter blocks -- its own saved motion |
| 381 | // field has no cells set, matching libaom's own "intra frame |
| 382 | // contributes nothing to a later projection" behaviour, but |
| 383 | // still carries `order_hint`/`ref_order_hints` for the distance |
| 384 | // arithmetic a later frame's projection needs. |
| 385 | let motion_field = crate::motion_field::MotionField::new( |
| 386 | header.mi_cols as usize, |
| 387 | header.mi_rows as usize, |
| 388 | header.order_hint, |
| 389 | ref_order_hints, |
| 390 | ); |
| 391 | (picture, end_cdfs, motion_field) |
| 392 | } else { |
| 393 | let last_slot = header.ref_frame_idx[0] as usize; |
| 394 | let reference = ref_slots[last_slot].as_ref().ok_or_else(|| { |
| 395 | Error::unsupported( |
| 396 | "AV1 decode_stream", |
| 397 | "an inter frame with no key frame before it", |
| 398 | ) |
| 399 | })?; |
| 400 | // `ref_frame_idx[i]` names the DPB slot for `LAST_FRAME + i` |
| 401 | // (spec 7.8/7.20). Any of them having no picture yet is not a |
| 402 | // stream error on its own: `decode_inter_block` only needs a |
| 403 | // given slot when a block actually selects that reference, and |
| 404 | // refuses by name there if the slot is still empty -- widened |
| 405 | // (lane-av1refs) from `GOLDEN_FRAME`'s own slot alone to every |
| 406 | // one of the 7 single-reference slots. |
| 407 | let other_refs: [Option<&Picture>; 8] = std::array::from_fn(|ref_frame| { |
| 408 | if ref_frame == 0 { |
| 409 | None |
| 410 | } else { |
| 411 | ref_slots |
| 412 | .get(header.ref_frame_idx[ref_frame - 1] as usize) |
| 413 | .and_then(Option::as_ref) |
| 414 | } |
| 415 | }); |
| 416 | // spec 7.9's own driver, `av1_setup_motion_field`: only run when |
| 417 | // this frame's header actually asks for temporal candidates -- |
| 418 | // `find_mv_stack_with_sign_bias` reproduces its old always-`None` |
| 419 | // behaviour bit for bit when this is `None` (see its own doc). |
| 420 | let tpl_field = header.use_ref_frame_mvs.then(|| { |
| 421 | crate::motion_field::setup_motion_field( |
| 422 | &motion_field_slots, |
| 423 | header.ref_frame_idx, |
| 424 | header.order_hint, |
| 425 | order_hint_bits, |
| 426 | header.mi_rows as usize, |
| 427 | header.mi_cols as usize, |
| 428 | ) |
| 429 | }); |
| 430 | decode_inter_frame_tile_with_cdfs( |
| 431 | tile_bytes, |
| 432 | header.mi_cols, |
| 433 | header.mi_rows, |
| 434 | header.quantization.base_q_idx, |
| 435 | header.frame_width, |
| 436 | header.frame_height, |
| 437 | reference, |
| 438 | other_refs, |
| 439 | &header.cdef, |
| 440 | &header.loop_filter, |
| 441 | initial_cdfs, |
| 442 | header.allow_high_precision_mv, |
| 443 | header.force_integer_mv, |
| 444 | header.ref_frame_sign_bias, |
| 445 | header.global_motion, |
| 446 | interp_fixed, |
| 447 | enable_dual_filter, |
| 448 | order_hint_bits, |
| 449 | header.order_hint, |
| 450 | ref_order_hints, |
| 451 | tpl_field.as_ref(), |
| 452 | header.reference_select, |
| 453 | enable_masked_compound, |
| 454 | enable_jnt_comp, |
| 455 | enable_interintra_compound, |
| 456 | header.skip_mode_present, |
| 457 | header.skip_mode_frame, |
| 458 | header.reduced_tx_set, |
| 459 | header.is_motion_mode_switchable, |
| 460 | header.allow_warped_motion, |
| 461 | header.allow_screen_content_tools, |
| 462 | )? |
| 463 | }; |
| 464 | // Spec 7.20: `disable_frame_end_update_cdf` stores the frame's |
| 465 | // *initial* table into the slots it refreshes, not the adapted |
| 466 | // end-of-tile one. |
| 467 | let stored_cdfs = if header.disable_frame_end_update_cdf { |
| 468 | started_from.unwrap_or_else(|| Cdfs::new(q_ctx_of(header.quantization.base_q_idx))) |
| 469 | } else { |
| 470 | // Spec 7.20 `save_cdfs` (libaom `av1_reset_cdf_symbol_counters`): |
| 471 | // the adapted table is saved with every symbol counter zeroed, |
| 472 | // not with the counts the tile's own decode left behind. |
| 473 | let mut end_cdfs = end_cdfs; |
| 474 | end_cdfs.reset_counts(); |
| 475 | end_cdfs |
| 476 | }; |
| 477 | if let Ok(path) = std::env::var("EC_AV1_DUMP_TABLES") { |
| 478 | use std::io::Write; |
| 479 | if let Ok(mut f) = std::fs::OpenOptions::new() |
| 480 | .create(true) |
| 481 | .append(true) |
| 482 | .open(&path) |
| 483 | { |
| 484 | let _ = writeln!(f, "=== frame idx={} ===", pictures.len()); |
| 485 | let _ = writeln!(f, "partition_w64 {:?}", stored_cdfs.partition_w64); |
| 486 | let _ = writeln!(f, "partition_w32 {:?}", stored_cdfs.partition_w32); |
| 487 | let _ = writeln!(f, "partition_w16 {:?}", stored_cdfs.partition_w16); |
| 488 | let _ = writeln!(f, "skip {:?}", stored_cdfs.skip); |
| 489 | let _ = writeln!(f, "new_mv {:?}", stored_cdfs.new_mv); |
| 490 | let _ = writeln!(f, "zero_mv {:?}", stored_cdfs.zero_mv); |
| 491 | let _ = writeln!(f, "ref_mv {:?}", stored_cdfs.ref_mv); |
| 492 | let _ = writeln!(f, "mv_joint {:?}", stored_cdfs.mv_joint); |
| 493 | } |
| 494 | } |
| 495 | // lane-comppin r3: decode-order (not display-order) dump of every |
| 496 | // frame's own post-deblock buffer -- the pre-existing pixel diff |
| 497 | // (`scratch_isolate_pinned_mismatch`) only ever compares *shown* |
| 498 | // frames, so a hidden reference (altref/bwdref) that DPB-corrupts a |
| 499 | // later shown frame is otherwise invisible; diff this byte-for-byte |
| 500 | // against `EC_AV1_POSTFILT_DUMP.fN` from the instrumented aomdec |
| 501 | // build to isolate whether the defect is in a hidden frame's own |
| 502 | // reconstruction or downstream of it. |
| 503 | if let Ok(path) = std::env::var("EC_AV1_DECODE_ORDER_DUMP") { |
| 504 | use std::io::Write; |
| 505 | let idx = pictures_decoded; |
| 506 | if let Ok(mut f) = std::fs::File::create(format!("{path}.f{idx}")) { |
| 507 | let _ = f.write_all(&picture.y); |
| 508 | let _ = f.write_all(&picture.u); |
| 509 | let _ = f.write_all(&picture.v); |
| 510 | } |
| 511 | } |
| 512 | pictures_decoded += 1; |
| 513 | for i in 0..NUM_REF_FRAMES { |
| 514 | if header.refresh_frame_flags & (1 << i) != 0 { |
| 515 | cdf_slots[i] = Some(stored_cdfs.clone()); |
| 516 | // Spec 7.18.3.1: synthesized grain is never stored for later |
| 517 | // prediction -- the reference bank always keeps the clean, |
| 518 | // pre-grain decode, even though the frame pushed onto the |
| 519 | // caller's output below carries the grained picture. |
| 520 | ref_slots[i] = Some(picture.clone()); |
| 521 | motion_field_slots[i] = Some(motion_field.clone()); |
| 522 | order_hint_slots[i] = header.order_hint; |
| 523 | } |
| 524 | } |
| 525 | if header.show_frame { |
| 526 | let output = if header.film_grain.apply_grain { |
| 527 | let mc_identity = parser |
| 528 | .sequence_header() |
| 529 | .is_some_and(|seq| seq.color_config.matrix_coefficients == 0); |
| 530 | crate::film_grain::apply_grain(&picture, &header.film_grain, mc_identity) |
| 531 | } else { |
| 532 | picture |
| 533 | }; |
| 534 | pictures.push(output); |
| 535 | } |
| 536 | } |
| 537 | Ok(pictures) |
| 538 | } |
| 539 | |
| 540 | #[cfg(test)] |
| 541 | mod tests { |
| 542 | use super::*; |
| 543 | use crate::encode::{Picture as Pic, encode_key_frame, encode_sequence}; |
| 544 | |
| 545 | fn test_card(width: usize, height: usize) -> Pic { |
| 546 | let mut picture = Pic::grey(width, height); |
| 547 | for row in 0..height { |
| 548 | for col in 0..width { |
| 549 | picture.y[row * width + col] = ((row * 7 + col * 11) % 251) as u8; |
| 550 | } |
| 551 | } |
| 552 | for row in 0..height / 2 { |
| 553 | for col in 0..width / 2 { |
| 554 | let i = row * width / 2 + col; |
| 555 | picture.u[i] = (100 + (col * 60 / (width / 2).max(1))) as u8; |
| 556 | picture.v[i] = (200 - (row * 80 / (height / 2).max(1))) as u8; |
| 557 | } |
| 558 | } |
| 559 | picture |
| 560 | } |
| 561 | |
| 562 | fn panned_test_card(width: usize, height: usize, shift: i64) -> Pic { |
| 563 | let mut picture = Pic::grey(width, height); |
| 564 | for y in 0..height { |
| 565 | for x in 0..width { |
| 566 | let sx = (x as i64 - shift).rem_euclid(width as i64) as f64; |
| 567 | let gradient = sx * 200.0 / width as f64; |
| 568 | picture.y[y * width + x] = (20.0 + gradient).clamp(0.0, 255.0) as u8; |
| 569 | } |
| 570 | } |
| 571 | for y in 0..height / 2 { |
| 572 | for x in 0..width / 2 { |
| 573 | let sx = (x as i64 - shift / 2).rem_euclid((width / 2) as i64) as usize; |
| 574 | let i = y * width / 2 + x; |
| 575 | picture.u[i] = (100 + (sx * 60 / (width / 2))) as u8; |
| 576 | picture.v[i] = (200 - (y * 80 / (height / 2))) as u8; |
| 577 | } |
| 578 | } |
| 579 | picture |
| 580 | } |
| 581 | |
| 582 | /// `decode_stream` on a lone key frame's stream matches both the tile |
| 583 | /// path (`decode_key_frame_tile` called directly on `Encoded::tile`) and |
| 584 | /// the encoder's own reconstruction, at an even and an odd size. |
| 585 | #[test] |
| 586 | fn decode_stream_matches_the_tile_path_on_a_key_frame() { |
| 587 | for &(width, height) in &[(64usize, 64usize), (216, 96)] { |
| 588 | let picture = test_card(width, height); |
| 589 | let encoded = encode_key_frame(&picture, 100, 0.5).unwrap(); |
| 590 | let via_tile = decode_key_frame_tile( |
| 591 | &encoded.tile, |
| 592 | encoded.mi_cols, |
| 593 | encoded.mi_rows, |
| 594 | encoded.base_q_idx, |
| 595 | width as u32, |
| 596 | height as u32, |
| 597 | false, |
| 598 | &ec_av1_syntax::CdefParams::default(), |
| 599 | &ec_av1_syntax::LoopFilterParams::default(), |
| 600 | false, |
| 601 | // Our own encoder always writes `reduced_tx_set: true`. |
| 602 | true, |
| 603 | false, |
| 604 | false, |
| 605 | ) |
| 606 | .unwrap(); |
| 607 | let via_stream = decode_stream(&encoded.stream).unwrap(); |
| 608 | assert_eq!(via_stream.len(), 1, "{width}x{height}: one picture"); |
| 609 | assert_eq!(via_stream[0].y, via_tile.y, "{width}x{height}: luma"); |
| 610 | assert_eq!(via_stream[0].u, via_tile.u, "{width}x{height}: U"); |
| 611 | assert_eq!(via_stream[0].v, via_tile.v, "{width}x{height}: V"); |
| 612 | assert_eq!( |
| 613 | via_stream[0].y, encoded.reconstruction.y, |
| 614 | "{width}x{height}: luma vs encoder reconstruction" |
| 615 | ); |
| 616 | } |
| 617 | } |
| 618 | |
| 619 | /// A GOP (key frame plus panned inter frames, so blocks actually take |
| 620 | /// motion) decodes bit-exact through `decode_stream` alone, threading its |
| 621 | /// own DPB rather than a test-supplied reference. |
| 622 | fn gop_round_trips(width: usize, height: usize) { |
| 623 | let pictures: Vec<_> = (0..4) |
| 624 | .map(|i| panned_test_card(width, height, i * 3)) |
| 625 | .collect(); |
| 626 | let encoded = encode_sequence(&pictures, 100, 0.5).unwrap(); |
| 627 | let decoded = decode_stream(&encoded.stream).unwrap(); |
| 628 | assert_eq!(decoded.len(), encoded.frames.len()); |
| 629 | for (i, (got, frame)) in decoded.iter().zip(&encoded.frames).enumerate() { |
| 630 | assert_eq!( |
| 631 | got.y, frame.reconstruction.y, |
| 632 | "{width}x{height} frame {i} luma" |
| 633 | ); |
| 634 | assert_eq!( |
| 635 | got.u, frame.reconstruction.u, |
| 636 | "{width}x{height} frame {i} U" |
| 637 | ); |
| 638 | assert_eq!( |
| 639 | got.v, frame.reconstruction.v, |
| 640 | "{width}x{height} frame {i} V" |
| 641 | ); |
| 642 | } |
| 643 | } |
| 644 | |
| 645 | #[test] |
| 646 | fn decode_stream_round_trips_a_gop() { |
| 647 | gop_round_trips(128, 64); |
| 648 | } |
| 649 | |
| 650 | /// Same claim at a size that is not a whole number of 32x32 blocks, so |
| 651 | /// the inter frame's 16x16-leaf split path is exercised too. |
| 652 | #[test] |
| 653 | fn decode_stream_round_trips_an_odd_size_gop() { |
| 654 | gop_round_trips(216, 96); |
| 655 | } |
| 656 | |
| 657 | use std::io::Write; |
| 658 | use std::process::{Command, Stdio}; |
| 659 | |
| 660 | /// Firing-detection gates (single-ref envelope, temporal-MV, `comp_mode`, |
| 661 | /// `skip_mode`/compound) all read a process-global atomic counter delta |
| 662 | /// (`decode::ref_hits`/`tmv_hits`/`comp_mode_hits`/`skip_mode_hits`) to |
| 663 | /// prove a real block picked the feature under test, not just that a |
| 664 | /// header bit was set. Under `cargo test`'s default parallel run, a |
| 665 | /// SIBLING test's own decode bumps the same global counter mid-gate -- |
| 666 | /// a false "fired" reading pixel-compares an attempt the gate would |
| 667 | /// otherwise `continue` past, on top of genuine defects that path may |
| 668 | /// still have. Serialising every counter-delta gate behind this one |
| 669 | /// mutex makes firing detection honest again (matches the existing |
| 670 | /// cdf-forwarding-gate flake shape, ledger `omp-liveness-pgrep-self-match`). |
| 671 | static GATE_COUNTER_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(()); |
| 672 | fn lock_gate_counters() -> std::sync::MutexGuard<'static, ()> { |
| 673 | GATE_COUNTER_LOCK.lock().unwrap_or_else(|e| e.into_inner()) |
| 674 | } |
| 675 | |
| 676 | /// lane-cdfflake: a cheap, dependency-free (FNV-1a, not cryptographic -- |
| 677 | /// this is a load-bearing-identity fingerprint for flake triage, not a |
| 678 | /// security digest, so no `sha2` dep is pulled in) fixture fingerprint |
| 679 | /// for the cdf-forwarding gate's instrumentation below. |
| 680 | fn fnv1a64(bytes: &[u8]) -> u64 { |
| 681 | let mut hash = 0xcbf29ce484222325u64; |
| 682 | for &b in bytes { |
| 683 | hash ^= b as u64; |
| 684 | hash = hash.wrapping_mul(0x100000001b3); |
| 685 | } |
| 686 | hash |
| 687 | } |
| 688 | |
| 689 | fn have_ffmpeg() -> bool { |
| 690 | Command::new("ffmpeg") |
| 691 | .arg("-version") |
| 692 | .stdout(Stdio::null()) |
| 693 | .stderr(Stdio::null()) |
| 694 | .status() |
| 695 | .is_ok_and(|s| s.success()) |
| 696 | } |
| 697 | |
| 698 | /// Decodes `frames` concatenated 4:2:0 frames out of one AV1 OBU stream. |
| 699 | fn ffmpeg_decode_sequence( |
| 700 | stream: &[u8], |
| 701 | width: usize, |
| 702 | height: usize, |
| 703 | frames: usize, |
| 704 | ) -> Vec<Pic> { |
| 705 | let mut child = Command::new("ffmpeg") |
| 706 | .args([ |
| 707 | "-v", "error", "-f", "obu", "-i", "-", "-f", "rawvideo", "-pix_fmt", "yuv420p", "-", |
| 708 | ]) |
| 709 | .stdin(Stdio::piped()) |
| 710 | .stdout(Stdio::piped()) |
| 711 | .stderr(Stdio::piped()) |
| 712 | .spawn() |
| 713 | .expect("ffmpeg failed to start"); |
| 714 | child |
| 715 | .stdin |
| 716 | .take() |
| 717 | .expect("ffmpeg stdin") |
| 718 | .write_all(stream) |
| 719 | .expect("writing the stream to ffmpeg"); |
| 720 | let out = child.wait_with_output().expect("ffmpeg failed to run"); |
| 721 | assert!( |
| 722 | out.status.success(), |
| 723 | "ffmpeg refused the stream: {}", |
| 724 | String::from_utf8_lossy(&out.stderr) |
| 725 | ); |
| 726 | let (luma, chroma) = (width * height, width * height / 4); |
| 727 | let frame_bytes = luma + 2 * chroma; |
| 728 | assert_eq!( |
| 729 | out.stdout.len(), |
| 730 | frame_bytes * frames, |
| 731 | "expected {frames} 4:2:0 frames, ffmpeg said: {}", |
| 732 | String::from_utf8_lossy(&out.stderr) |
| 733 | ); |
| 734 | (0..frames) |
| 735 | .map(|i| { |
| 736 | let base = i * frame_bytes; |
| 737 | Pic { |
| 738 | width, |
| 739 | height, |
| 740 | y: out.stdout[base..base + luma].to_vec(), |
| 741 | u: out.stdout[base + luma..base + luma + chroma].to_vec(), |
| 742 | v: out.stdout[base + luma + chroma..base + frame_bytes].to_vec(), |
| 743 | } |
| 744 | }) |
| 745 | .collect() |
| 746 | } |
| 747 | |
| 748 | /// `decode_stream` agrees with ffmpeg/dav1d on the same wire bytes -- an |
| 749 | /// independent decoder, not just this crate checking its own tile path. |
| 750 | #[test] |
| 751 | fn decode_stream_agrees_with_ffmpeg_on_a_gop() { |
| 752 | if !have_ffmpeg() { |
| 753 | eprintln!("SKIP decode_stream_agrees_with_ffmpeg_on_a_gop: no ffmpeg"); |
| 754 | return; |
| 755 | } |
| 756 | let (width, height) = (128usize, 64usize); |
| 757 | let pictures: Vec<_> = (0..3) |
| 758 | .map(|i| panned_test_card(width, height, i * 3)) |
| 759 | .collect(); |
| 760 | let encoded = encode_sequence(&pictures, 100, 0.5).unwrap(); |
| 761 | let ffmpeg_frames = ffmpeg_decode_sequence(&encoded.stream, width, height, 3); |
| 762 | let decoded = decode_stream(&encoded.stream).unwrap(); |
| 763 | assert_eq!(decoded.len(), 3); |
| 764 | for (i, (got, want)) in decoded.iter().zip(&ffmpeg_frames).enumerate() { |
| 765 | assert_eq!(got.y, want.y, "frame {i} luma vs ffmpeg"); |
| 766 | assert_eq!(got.u, want.u, "frame {i} U vs ffmpeg"); |
| 767 | assert_eq!(got.v, want.v, "frame {i} V vs ffmpeg"); |
| 768 | } |
| 769 | } |
| 770 | |
| 771 | /// A hand-built 3-frame stream that actually fires `GOLDEN_FRAME` |
| 772 | /// through [`decode_stream`], proving the round-4 flip (`decode.rs`'s |
| 773 | /// `decode_inter_block` `GOLDEN_FRAME` arm) the way 120+ real aomenc |
| 774 | /// attempts never could: frame 0 is a real key frame (this crate's own |
| 775 | /// encoder, real texture), refreshing all 8 DPB slots as spec requires -- |
| 776 | /// slot 3 keeps that exact picture from here on. Frame 1 is a hand-coded |
| 777 | /// inter frame whose lone 32x32 block is *intra* (`DC_PRED`, `skip`), |
| 778 | /// refreshing only slot 0 with a flat 128 block -- distinguishable from |
| 779 | /// frame 0's real texture, so slot 0 (`LAST_FRAME`) and slot 3 |
| 780 | /// (`GOLDEN_FRAME`) hold visibly different pictures once frame 2 runs. |
| 781 | /// Frame 2 is a hand-coded inter frame naming `ref_frame_idx[3] = 3` |
| 782 | /// (GOLDEN_FRAME's slot) and `ref_frame_idx[0] = 0` (LAST_FRAME's), whose |
| 783 | /// lone block codes `single_ref` all the way out to `GOLDEN_FRAME` |
| 784 | /// (`p1=0, p3=1, p5=1`) and a `skip` zero-MV `NEARESTMV`: a direct copy |
| 785 | /// of GOLDEN's picture, so its output must equal frame 0's own |
| 786 | /// reconstruction exactly, and must differ from frame 1's -- proof the |
| 787 | /// decoder actually read the golden plane, not silently substituted |
| 788 | /// `LAST_FRAME`'s. Rounds 4-9 masked and re-masked this arm chasing a |
| 789 | /// deblock/MC residue that turned out to be film-grain synthesis |
| 790 | /// (lane-av1golden7's `apply_grain` refusal, unrelated to GOLDEN); |
| 791 | /// lane-av1golden8 fixed mvstack's single-ref extension pass, clearing |
| 792 | /// the last real defect (`pinned_golden7` non_last_ref_hits 0->2), so |
| 793 | /// this fixture flips back to its round-4 pixel-exact form. ffmpeg |
| 794 | /// decodes the identical wire bytes as the foreign oracle |
| 795 | /// (shared-oracle-blindness class): this crate's own decoder agreeing |
| 796 | /// with itself would prove nothing. |
| 797 | #[test] |
| 798 | fn a_hand_built_golden_reference_decodes_pixel_exact_against_ffmpeg() { |
| 799 | if !have_ffmpeg() { |
| 800 | eprintln!( |
| 801 | "SKIP a_hand_built_golden_reference_decodes_pixel_exact_against_ffmpeg: no ffmpeg" |
| 802 | ); |
| 803 | return; |
| 804 | } |
| 805 | use crate::cdf_state::Cdfs; |
| 806 | use crate::decode::{intra_inter_ctx, non_last_ref_hits, q_ctx_of}; |
| 807 | use crate::encode::{inter_frame_headers, key_frame_headers}; |
| 808 | use crate::frame::frame_obu; |
| 809 | use crate::msac::SymbolEncoder; |
| 810 | use crate::mvstack::{ |
| 811 | GOLDEN_FRAME, MiGrid, find_mv_stack, single_ref_p1_ctx, single_ref_p3_ctx, |
| 812 | single_ref_p5_ctx, |
| 813 | }; |
| 814 | use crate::obu::temporal_delimiter; |
| 815 | |
| 816 | let (width, height) = (32usize, 32usize); |
| 817 | let base_q_idx = 100u8; |
| 818 | // The whole picture is one 32x32 block: `partition_ctx`/`skip_ctx`/ |
| 819 | // `intra_inter_ctx`'s neighbour inputs are all "no neighbour yet" -- |
| 820 | // a fresh `Neighbours`' `above_side_mi`/`left_side_mi` start at `SB` |
| 821 | // (64), so `partition_ctx_mi`'s `side_mi * 2 <= side` (32) is false |
| 822 | // on both axes (ctx 0), and `above_skip`/`left_skip`/`above_inter`/ |
| 823 | // `left_inter`/`above_ref`/`left_ref` all start `false`/`-1` |
| 824 | // (decode.rs `Neighbours::new`) -- computed here without |
| 825 | // constructing the (module-private) `Neighbours` itself. |
| 826 | const PARTITION_NONE: usize = 0; |
| 827 | let ii_ctx = intra_inter_ctx(false, false, false, false); |
| 828 | let sr_p1_ctx = single_ref_p1_ctx(None, None, None, None); |
| 829 | let sr_p3_ctx = single_ref_p3_ctx(None, None, None, None); |
| 830 | let sr_p5_ctx = single_ref_p5_ctx(None, None, None, None); |
| 831 | |
| 832 | for run in 0..4 { |
| 833 | let picture = test_card(width, height); |
| 834 | let key = encode_key_frame(&picture, base_q_idx, 0.5).unwrap(); |
| 835 | let (seq, _) = key_frame_headers(width, height, base_q_idx).unwrap(); |
| 836 | |
| 837 | // Frame 1: hand-coded, intra-only, refreshes slot 0 with a flat |
| 838 | // 128 block (no neighbours to predict `DC_PRED` from). |
| 839 | let (_, header1) = inter_frame_headers(width, height, base_q_idx, 1, 0).unwrap(); |
| 840 | let tile1 = { |
| 841 | let mut cdfs = Cdfs::new(q_ctx_of(base_q_idx)); |
| 842 | let mut enc = SymbolEncoder::new(); |
| 843 | enc.symbol(PARTITION_NONE, &mut cdfs.partition_w32[0]); |
| 844 | enc.symbol(1, &mut cdfs.skip[0]); // skip |
| 845 | enc.symbol(0, &mut cdfs.intra_inter[ii_ctx]); // is_inter = false |
| 846 | enc.symbol(0, &mut cdfs.y_mode[3]); // DC_PRED, size group 3 (32x32) |
| 847 | enc.symbol(0, &mut cdfs.uv_mode_cfl[0]); // DC_PRED |
| 848 | enc.finish() |
| 849 | }; |
| 850 | let mut stream = key.stream.clone(); |
| 851 | stream.extend(temporal_delimiter()); |
| 852 | stream.extend(frame_obu(&seq, &header1, &tile1).unwrap()); |
| 853 | |
| 854 | // Frame 2: hand-coded, single_ref all the way to GOLDEN_FRAME, |
| 855 | // skip zero-MV NEARESTMV -- a direct copy of slot 3 (frame 0). |
| 856 | let (_, mut header2) = inter_frame_headers(width, height, base_q_idx, 2, 0).unwrap(); |
| 857 | header2.ref_frame_idx[3] = 3; // GOLDEN_FRAME's own slot: frame 0's, untouched by frame 1 |
| 858 | let tile2 = { |
| 859 | let mut cdfs = Cdfs::new(q_ctx_of(base_q_idx)); |
| 860 | let mut enc = SymbolEncoder::new(); |
| 861 | let grid = MiGrid::new(8, 8); |
| 862 | let stack = find_mv_stack(&grid, 0, 0, 8, 8, GOLDEN_FRAME, 8, 8); |
| 863 | enc.symbol(PARTITION_NONE, &mut cdfs.partition_w32[0]); |
| 864 | enc.symbol(1, &mut cdfs.skip[0]); // skip |
| 865 | enc.symbol(1, &mut cdfs.intra_inter[ii_ctx]); // is_inter = true |
| 866 | enc.symbol(0, &mut cdfs.single_ref[sr_p1_ctx][0]); // p1: LAST/LAST2/LAST3/GOLDEN group |
| 867 | enc.symbol(1, &mut cdfs.single_ref[sr_p3_ctx][2]); // p3: GOLDEN/LAST3 sub-group |
| 868 | enc.symbol(1, &mut cdfs.single_ref[sr_p5_ctx][4]); // p5: GOLDEN_FRAME |
| 869 | enc.symbol(1, &mut cdfs.new_mv[stack.new_mv_ctx]); // not_new -> not NEWMV |
| 870 | enc.symbol(1, &mut cdfs.zero_mv[stack.zero_mv_ctx]); // not_zero -> not GLOBALMV |
| 871 | enc.symbol(0, &mut cdfs.ref_mv[stack.ref_mv_ctx]); // nearest -> NEARESTMV, (0, 0) |
| 872 | enc.finish() |
| 873 | }; |
| 874 | stream.extend(temporal_delimiter()); |
| 875 | stream.extend(frame_obu(&seq, &header2, &tile2).unwrap()); |
| 876 | |
| 877 | let before = non_last_ref_hits(); |
| 878 | let decoded = decode_stream(&stream).unwrap(); |
| 879 | assert_eq!(decoded.len(), 3, "run {run}: expected 3 pictures"); |
| 880 | assert!( |
| 881 | non_last_ref_hits() > before, |
| 882 | "run {run}: GOLDEN_FRAME never fired" |
| 883 | ); |
| 884 | |
| 885 | let ffmpeg_frames = ffmpeg_decode_sequence(&stream, width, height, 3); |
| 886 | for (i, (got, want)) in decoded.iter().zip(&ffmpeg_frames).enumerate() { |
| 887 | assert_eq!(got.y, want.y, "run {run} frame {i}: luma vs ffmpeg"); |
| 888 | assert_eq!(got.u, want.u, "run {run} frame {i}: U vs ffmpeg"); |
| 889 | assert_eq!(got.v, want.v, "run {run} frame {i}: V vs ffmpeg"); |
| 890 | } |
| 891 | assert_eq!( |
| 892 | decoded[0].y, key.reconstruction.y, |
| 893 | "run {run}: frame 0 vs the encoder's own reconstruction" |
| 894 | ); |
| 895 | assert_eq!( |
| 896 | decoded[2].y, key.reconstruction.y, |
| 897 | "run {run}: GOLDEN_FRAME's block did not reproduce the keyframe it names" |
| 898 | ); |
| 899 | assert_ne!( |
| 900 | decoded[2].y, decoded[1].y, |
| 901 | "run {run}: LAST_FRAME's and GOLDEN_FRAME's slots were not actually \ |
| 902 | distinguishable -- the gate proves nothing" |
| 903 | ); |
| 904 | } |
| 905 | } |
| 906 | |
| 907 | /// Builds a `gradients` lavfi source string with colours derived from |
| 908 | /// `seed`, not left to the filter's own default. |
| 909 | /// |
| 910 | /// Measured (lane-fixdet r1): ffmpeg's `gradients` source IGNORES its |
| 911 | /// `seed=` parameter for colour selection -- five encodes of an |
| 912 | /// identical command line (same `seed=`, same everything) produced five |
| 913 | /// different fixtures, because the unset `c0..c9` stops are randomized |
| 914 | /// from something other than that seed. Every one of this file's ~20 |
| 915 | /// gate fixtures built a `gradients` source this way, so none of them |
| 916 | /// had a reproducible input: a pin captured from one run could not be |
| 917 | /// regenerated, and "attempt N failed" was as likely to be a different |
| 918 | /// fixture as a different decode outcome. |
| 919 | /// |
| 920 | /// Freezing the colours outright (as the earliest four gates below do, |
| 921 | /// hand-picking `c0=red:c1=blue:c2=green`) fixes reproducibility but |
| 922 | /// throws away the per-attempt content variety the sweep gates need to |
| 923 | /// make features (OBMC, warp, wedge, ...) fire at all -- this repo's own |
| 924 | /// `sampler-decorrelated-gate` class. So: derive `c0..c3` deterministically |
| 925 | /// FROM the seed with a plain integer hash, and keep passing `seed=` |
| 926 | /// itself too, so a future ffmpeg that actually honours it only adds |
| 927 | /// more variety, never removes the determinism this buys. |
| 928 | fn gradients_source(seed: u32, width: usize, height: usize, tail: &str) -> String { |
| 929 | fn hash_color(seed: u32, salt: u32) -> String { |
| 930 | let h = seed |
| 931 | .wrapping_mul(2_654_435_761) |
| 932 | .wrapping_add(salt.wrapping_mul(0x9E37_79B9)) |
| 933 | ^ (seed.rotate_left(13)); |
| 934 | format!("0x{:06x}", h & 0x00ff_ffff) |
| 935 | } |
| 936 | let (c0, c1, c2, c3) = ( |
| 937 | hash_color(seed, 0), |
| 938 | hash_color(seed, 1), |
| 939 | hash_color(seed, 2), |
| 940 | hash_color(seed, 3), |
| 941 | ); |
| 942 | format!("gradients=size={width}x{height}:c0={c0}:c1={c1}:c2={c2}:c3={c3}:seed={seed}:{tail}") |
| 943 | } |
| 944 | |
| 945 | /// The determinism guard: same seed twice must byte-match ffmpeg's own |
| 946 | /// output; different seeds must not. Regresses this fix if it ever |
| 947 | /// breaks. |
| 948 | #[test] |
| 949 | fn gradients_source_is_reproducible_per_seed() { |
| 950 | if !have_ffmpeg() { |
| 951 | eprintln!("SKIP gradients_source_is_reproducible_per_seed: no ffmpeg"); |
| 952 | return; |
| 953 | } |
| 954 | fn render_hash(lavfi: &str) -> Vec<u8> { |
| 955 | let out = Command::new("ffmpeg") |
| 956 | .args([ |
| 957 | "-v", "error", "-f", "lavfi", "-i", lavfi, "-frames:v", "1", "-f", "rawvideo", |
| 958 | "-", |
| 959 | ]) |
| 960 | .stdin(Stdio::null()) |
| 961 | .stdout(Stdio::piped()) |
| 962 | .stderr(Stdio::piped()) |
| 963 | .output() |
| 964 | .expect("ffmpeg failed to run"); |
| 965 | assert!(out.status.success(), "ffmpeg refused {lavfi}"); |
| 966 | out.stdout |
| 967 | } |
| 968 | let a1 = render_hash(&gradients_source(42, 64, 64, "duration=0.04:rate=25")); |
| 969 | let a2 = render_hash(&gradients_source(43, 64, 64, "duration=0.04:rate=25")); |
| 970 | let a3 = render_hash(&gradients_source(42, 64, 64, "duration=0.04:rate=25")); |
| 971 | assert_eq!(a1, a3, "same seed must reproduce byte-identical output"); |
| 972 | assert_ne!(a1, a2, "different seeds must not collide"); |
| 973 | } |
| 974 | |
| 975 | /// Encodes a real libaom-av1 stream (via ffmpeg's own encoder, not this |
| 976 | /// crate's) with the same reduced-partition flags lane-av1adst's r6 |
| 977 | /// probe found still select `ADST_ADST`/`ADST_DCT`/`DCT_ADST` on |
| 978 | /// gradient content (TX_SET_INTRA_2), and decodes it with |
| 979 | /// [`decode_stream`] -- lifting the round-2 refusal is only real if it |
| 980 | /// gets past an encoder this crate never wrote a byte of. |
| 981 | fn libaom_encode(lavfi: &str, width: usize, height: usize, crf: u32) -> Option<Vec<u8>> { |
| 982 | libaom_encode_with(lavfi, width, height, crf, &[]) |
| 983 | } |
| 984 | |
| 985 | /// As [`libaom_encode`], with extra encoder args appended -- used to pin |
| 986 | /// a deterministic stream (`-threads 1` etc., since libaom's `cpu-used 8` |
| 987 | /// RD search is not otherwise stable run-to-run on this box, and lavfi |
| 988 | /// `gradients` needs its own `seed=` for the same reason) and to gate |
| 989 | /// individual feature flags (`-enable-cdef 0`) a given fixture needs off. |
| 990 | fn libaom_encode_with( |
| 991 | lavfi: &str, |
| 992 | width: usize, |
| 993 | height: usize, |
| 994 | crf: u32, |
| 995 | extra: &[&str], |
| 996 | ) -> Option<Vec<u8>> { |
| 997 | let mut args = vec![ |
| 998 | "-v", |
| 999 | "error", |
| 1000 | "-f", |
| 1001 | "lavfi", |
| 1002 | "-i", |
| 1003 | lavfi, |
| 1004 | "-frames:v", |
| 1005 | "1", |
| 1006 | "-pix_fmt", |
| 1007 | "yuv420p", |
| 1008 | "-c:v", |
| 1009 | "libaom-av1", |
| 1010 | ]; |
| 1011 | let crf_str = crf.to_string(); |
| 1012 | args.extend(["-crf", &crf_str]); |
| 1013 | args.extend([ |
| 1014 | "-cpu-used", |
| 1015 | "8", |
| 1016 | "-enable-rect-partitions", |
| 1017 | "0", |
| 1018 | "-enable-ab-partitions", |
| 1019 | "0", |
| 1020 | "-enable-1to4-partitions", |
| 1021 | "0", |
| 1022 | "-enable-angle-delta", |
| 1023 | "0", |
| 1024 | "-enable-cfl-intra", |
| 1025 | "0", |
| 1026 | ]); |
| 1027 | args.extend(extra.iter().copied()); |
| 1028 | args.extend(["-f", "obu", "-"]); |
| 1029 | let out = Command::new("ffmpeg") |
| 1030 | .args(&args) |
| 1031 | .stdin(Stdio::null()) |
| 1032 | .stdout(Stdio::piped()) |
| 1033 | .stderr(Stdio::piped()) |
| 1034 | .output() |
| 1035 | .expect("ffmpeg failed to run"); |
| 1036 | if !out.status.success() { |
| 1037 | eprintln!( |
| 1038 | "ffmpeg refused to encode {lavfi} at {width}x{height}: {}", |
| 1039 | String::from_utf8_lossy(&out.stderr) |
| 1040 | ); |
| 1041 | return None; |
| 1042 | } |
| 1043 | Some(out.stdout) |
| 1044 | } |
| 1045 | |
| 1046 | /// The end-to-end gate: two real libaom-av1 configs, decoded with |
| 1047 | /// [`decode_stream`] and checked against ffmpeg's own decode of the same |
| 1048 | /// bytes. Not every config this encoder writes decodes yet (partitions |
| 1049 | /// below 16x16, AB partitions, CfL and others remain round-2 gaps per the |
| 1050 | /// r6 catalogue) -- both configs here pick `cpu-used 8` plus the |
| 1051 | /// partition/angle-delta/CfL flags r6 found narrow the encoder onto this |
| 1052 | /// decoder's supported syntax, while still landing on non-`DCT_DCT` tx |
| 1053 | /// types on gradient content. |
| 1054 | #[test] |
| 1055 | fn a_real_libaom_stream_with_adst_decodes_end_to_end() { |
| 1056 | if !have_ffmpeg() { |
| 1057 | eprintln!("SKIP a_real_libaom_stream_with_adst_decodes_end_to_end: no ffmpeg"); |
| 1058 | return; |
| 1059 | } |
| 1060 | let configs: [(String, usize, usize, u32); 2] = [ |
| 1061 | ("testsrc2=size=64x64:rate=1".to_string(), 64, 64, 15), |
| 1062 | (gradients_source(42, 64, 64, "rate=1"), 64, 64, 15), |
| 1063 | ]; |
| 1064 | let mut verdicts = Vec::new(); |
| 1065 | for (lavfi, width, height, crf) in configs { |
| 1066 | let lavfi = lavfi.as_str(); |
| 1067 | let Some(stream) = libaom_encode(lavfi, width, height, crf) else { |
| 1068 | verdicts.push(format!("{lavfi}: ffmpeg itself refused to encode")); |
| 1069 | continue; |
| 1070 | }; |
| 1071 | match decode_stream(&stream) { |
| 1072 | Ok(frames) => { |
| 1073 | let ffmpeg_frames = ffmpeg_decode_sequence(&stream, width, height, 1); |
| 1074 | let matches = frames[0].y == ffmpeg_frames[0].y |
| 1075 | && frames[0].u == ffmpeg_frames[0].u |
| 1076 | && frames[0].v == ffmpeg_frames[0].v; |
| 1077 | verdicts.push(format!( |
| 1078 | "{lavfi}: decoded, {} ffmpeg's own decode", |
| 1079 | if matches { "MATCHES" } else { "MISMATCHES" } |
| 1080 | )); |
| 1081 | // Not a hard `assert!`: libaom's own mode decisions at |
| 1082 | // `cpu-used 8` are not observed stable run-to-run on this |
| 1083 | // box (multi-threaded RD search), so a run that happens |
| 1084 | // to land on a still-unsupported syntax element (e.g. a |
| 1085 | // partition below 16x16, a separate round-2 gap) can |
| 1086 | // decode without erroring yet disagree -- that failure |
| 1087 | // mode belongs to those other gaps, not to this round's |
| 1088 | // ADST wiring, and the eprintln below reports it either |
| 1089 | // way rather than hiding it. |
| 1090 | } |
| 1091 | Err(e) => { |
| 1092 | verdicts.push(format!("{lavfi}: still refuses ({e})")); |
| 1093 | } |
| 1094 | } |
| 1095 | } |
| 1096 | eprintln!("libaom ADST end-to-end verdicts:"); |
| 1097 | for v in &verdicts { |
| 1098 | eprintln!(" {v}"); |
| 1099 | } |
| 1100 | } |
| 1101 | |
| 1102 | /// Hardened past the report-only test above: a real libaom-av1 gradients |
| 1103 | /// stream, pinned deterministic (`gradients`' own `seed=`, plus |
| 1104 | /// `-threads 1`/`-row-mt 0`/no tiling so libaom's `cpu-used 8` RD search |
| 1105 | /// cannot pick a different mode set run to run) and with CDEF turned off |
| 1106 | /// (this decoder's tile path never applies that in-loop filter -- see |
| 1107 | /// `cdef_is_active` in `decode_stream` -- so a stream that leaves it on |
| 1108 | /// decodes-but-wrong instead of refusing at cdef_bits==0; ffmpeg's |
| 1109 | /// `-enable-cdef 0` keeps this fixture out of that gap rather than this |
| 1110 | /// test working around it). Asserts pixel-exact against ffmpeg's own |
| 1111 | /// decode of the identical bytes, not just "decoded without error". |
| 1112 | #[test] |
| 1113 | fn a_real_libaom_gradients_stream_decodes_pixel_exact() { |
| 1114 | if !have_ffmpeg() { |
| 1115 | eprintln!("SKIP a_real_libaom_gradients_stream_decodes_pixel_exact: no ffmpeg"); |
| 1116 | return; |
| 1117 | } |
| 1118 | let (width, height) = (64, 64); |
| 1119 | let stream = libaom_encode_with( |
| 1120 | &gradients_source(42, width, height, "rate=1"), |
| 1121 | width, |
| 1122 | height, |
| 1123 | 15, |
| 1124 | &[ |
| 1125 | "-threads", |
| 1126 | "1", |
| 1127 | "-row-mt", |
| 1128 | "0", |
| 1129 | "-tile-columns", |
| 1130 | "0", |
| 1131 | "-tile-rows", |
| 1132 | "0", |
| 1133 | "-enable-cdef", |
| 1134 | "0", |
| 1135 | ], |
| 1136 | ) |
| 1137 | .expect("ffmpeg encode"); |
| 1138 | let frames = decode_stream(&stream).expect("decode_stream on a real libaom stream"); |
| 1139 | let ffmpeg_frames = ffmpeg_decode_sequence(&stream, width, height, 1); |
| 1140 | assert_eq!(frames[0].y, ffmpeg_frames[0].y, "luma vs ffmpeg"); |
| 1141 | assert_eq!(frames[0].u, ffmpeg_frames[0].u, "U vs ffmpeg"); |
| 1142 | assert_eq!(frames[0].v, ffmpeg_frames[0].v, "V vs ffmpeg"); |
| 1143 | } |
| 1144 | |
| 1145 | /// As [`a_real_libaom_gradients_stream_decodes_pixel_exact`], but at |
| 1146 | /// 32x32 (below `is_cfl_allowed`'s 32x32 luma bound as a *single* |
| 1147 | /// undivided block, spec 5.11.5) rather than 64x64 -- a whole 64x64 SB |
| 1148 | /// is never CFL-eligible itself, only its <=32x32 partitions, and |
| 1149 | /// `testsrc2`/`gradients` at 64x64 with rect/ab/1to4/angle-delta search |
| 1150 | /// disabled reliably pick a whole-SB `PARTITION_NONE` (probed |
| 1151 | /// lane-av1real r2), so no fixture at that size exercises CfL at all. |
| 1152 | /// A 32x32 frame is exactly the whole-block, CFL-allowed case, and |
| 1153 | /// lane-av1real r2 found `gradients` there reliably selects |
| 1154 | /// `UV_CFL_PRED` (`uv_mode=13`) across a crf sweep -- `testsrc2` there |
| 1155 | /// stays DC. Proves the CfL port this round added, not just DC-chroma |
| 1156 | /// decode: pixel-exact against ffmpeg's own decode, not just "decoded". |
| 1157 | /// |
| 1158 | #[test] |
| 1159 | fn a_real_libaom_cfl_stream_decodes_pixel_exact() { |
| 1160 | if !have_ffmpeg() { |
| 1161 | eprintln!("SKIP a_real_libaom_cfl_stream_decodes_pixel_exact: no ffmpeg"); |
| 1162 | return; |
| 1163 | } |
| 1164 | let (width, height) = (32, 32); |
| 1165 | let stream = libaom_encode_with( |
| 1166 | &gradients_source(42, width, height, "rate=1"), |
| 1167 | width, |
| 1168 | height, |
| 1169 | 15, |
| 1170 | &[ |
| 1171 | "-threads", |
| 1172 | "1", |
| 1173 | "-row-mt", |
| 1174 | "0", |
| 1175 | "-tile-columns", |
| 1176 | "0", |
| 1177 | "-tile-rows", |
| 1178 | "0", |
| 1179 | "-enable-cdef", |
| 1180 | "0", |
| 1181 | ], |
| 1182 | ) |
| 1183 | .expect("ffmpeg encode"); |
| 1184 | let frames = decode_stream(&stream).expect("decode_stream on a real libaom stream"); |
| 1185 | let ffmpeg_frames = ffmpeg_decode_sequence(&stream, width, height, 1); |
| 1186 | assert_eq!(frames[0].y, ffmpeg_frames[0].y, "luma vs ffmpeg"); |
| 1187 | assert_eq!(frames[0].u, ffmpeg_frames[0].u, "U vs ffmpeg"); |
| 1188 | assert_eq!(frames[0].v, ffmpeg_frames[0].v, "V vs ffmpeg"); |
| 1189 | } |
| 1190 | |
| 1191 | /// As [`a_real_libaom_gradients_stream_decodes_pixel_exact`], but leaves |
| 1192 | /// CDEF *on* (no `-enable-cdef 0`) -- the whole point of `lane-av1cdef`: |
| 1193 | /// proves `apply_cdef` itself, not just that this decoder still works |
| 1194 | /// when the filter never fires. Gradient content at a low crf reliably |
| 1195 | /// gives libaom something to dering. If a run happens to land on |
| 1196 | /// `cdef_bits > 0` (per-64x64 `cdef_idx`, still unimplemented -- see the |
| 1197 | /// refusal in `decode_stream`) the stream is skipped rather than failed: |
| 1198 | /// that gap is named by the refusal's own error text, not silently |
| 1199 | /// swallowed here. |
| 1200 | #[test] |
| 1201 | fn a_real_libaom_gradients_stream_with_cdef_decodes_pixel_exact() { |
| 1202 | if !have_ffmpeg() { |
| 1203 | eprintln!( |
| 1204 | "SKIP a_real_libaom_gradients_stream_with_cdef_decodes_pixel_exact: no ffmpeg" |
| 1205 | ); |
| 1206 | return; |
| 1207 | } |
| 1208 | let (width, height) = (64, 64); |
| 1209 | let stream = libaom_encode_with( |
| 1210 | &gradients_source(42, width, height, "rate=1"), |
| 1211 | width, |
| 1212 | height, |
| 1213 | 30, |
| 1214 | &[ |
| 1215 | "-threads", |
| 1216 | "1", |
| 1217 | "-row-mt", |
| 1218 | "0", |
| 1219 | "-tile-columns", |
| 1220 | "0", |
| 1221 | "-tile-rows", |
| 1222 | "0", |
| 1223 | ], |
| 1224 | ) |
| 1225 | .expect("ffmpeg encode"); |
| 1226 | match decode_stream(&stream) { |
| 1227 | Ok(frames) => { |
| 1228 | let ffmpeg_frames = ffmpeg_decode_sequence(&stream, width, height, 1); |
| 1229 | assert_eq!(frames[0].y, ffmpeg_frames[0].y, "luma vs ffmpeg (CDEF on)"); |
| 1230 | assert_eq!(frames[0].u, ffmpeg_frames[0].u, "U vs ffmpeg (CDEF on)"); |
| 1231 | assert_eq!(frames[0].v, ffmpeg_frames[0].v, "V vs ffmpeg (CDEF on)"); |
| 1232 | } |
| 1233 | Err(e) => { |
| 1234 | eprintln!("SKIP a_real_libaom_gradients_stream_with_cdef_decodes_pixel_exact: {e}"); |
| 1235 | } |
| 1236 | } |
| 1237 | } |
| 1238 | |
| 1239 | /// Path to a real `aomenc` build carrying the reference options this |
| 1240 | /// fixture needs (`--max-partition-size`, `--enable-tx-size-search`, |
| 1241 | /// `--loopfilter-control`) -- this box's `ffmpeg` links a libaom too old |
| 1242 | /// to expose `enable-tx-size-search`/`loopfilter-control` through its |
| 1243 | /// own `-aom-params` passthrough (probed live: "Cannot find aom |
| 1244 | /// option"), so [`a_real_aomenc_filter_intra_stream_decodes_pixel_exact`] |
| 1245 | /// shells out to the standalone binary instead of ffmpeg's libaom-av1 |
| 1246 | /// wrapper every other test here uses. `EC_AV1_AOMENC` overrides the |
| 1247 | /// default dev-build path; the test skips (not fails) when neither this |
| 1248 | /// nor `ffmpeg` is present. |
| 1249 | fn aomenc_path() -> std::path::PathBuf { |
| 1250 | if let Some(p) = std::env::var_os("EC_AV1_AOMENC") { |
| 1251 | return std::path::PathBuf::from(p); |
| 1252 | } |
| 1253 | // The oracle used to live only under `/tmp`, which this box's tmpfs |
| 1254 | // guard reclaims -- when it did, all 20 aomenc gates below went |
| 1255 | // silently vacuous (SKIP, suite still green). The durable location |
| 1256 | // is provisioned by `scripts/build-aom-oracle.sh`; the `/tmp` path |
| 1257 | // stays last as a legacy fallback. |
| 1258 | let mut candidates = Vec::new(); |
| 1259 | if let Some(home) = std::env::var_os("HOME") { |
| 1260 | candidates.push(std::path::PathBuf::from(&home).join(".cache/aom-oracle/build/aomenc")); |
| 1261 | } |
| 1262 | candidates.push("/tmp/libaom-src/build/encoder/aomenc".into()); |
| 1263 | candidates |
| 1264 | .iter() |
| 1265 | .find(|p| p.is_file()) |
| 1266 | .cloned() |
| 1267 | .unwrap_or_else(|| candidates.remove(0)) |
| 1268 | } |
| 1269 | |
| 1270 | /// Whether the aomenc oracle is present. Absence normally SKIPs (a |
| 1271 | /// checkout without the oracle should still run the rest of the suite), |
| 1272 | /// but `EC_AV1_REQUIRE_AOMENC=1` turns it into a hard failure so a batch |
| 1273 | /// run cannot report green off 20 skipped gates. |
| 1274 | /// Where a pinned stream lives. `fixtures/` is a symlink to a durable |
| 1275 | /// directory outside the repo, so a pin captured in one session is still |
| 1276 | /// there in the next; a session scratchpad is not (tmpfs reaps it, and |
| 1277 | /// four pins were silently dead because of that). |
| 1278 | fn pin_dir() -> std::path::PathBuf { |
| 1279 | if let Some(p) = std::env::var_os("EC_AV1_PIN_DIR") { |
| 1280 | return std::path::PathBuf::from(p); |
| 1281 | } |
| 1282 | std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")) |
| 1283 | .join("../../fixtures") |
| 1284 | .canonicalize() |
| 1285 | .unwrap_or_else(|_| std::path::PathBuf::from("fixtures")) |
| 1286 | } |
| 1287 | |
| 1288 | fn have_aomenc() -> bool { |
| 1289 | let present = aomenc_path().is_file(); |
| 1290 | assert!( |
| 1291 | present || std::env::var_os("EC_AV1_REQUIRE_AOMENC").is_none(), |
| 1292 | "EC_AV1_REQUIRE_AOMENC is set but no aomenc at {} -- run scripts/build-aom-oracle.sh", |
| 1293 | aomenc_path().display() |
| 1294 | ); |
| 1295 | present |
| 1296 | } |
| 1297 | |
| 1298 | /// A real `aomenc` filter-intra fixture: every other intra mode disabled |
| 1299 | /// (smooth/paeth/directional/angle-delta) and |
| 1300 | /// `--max-partition-size=32` (past which `av1_filter_intra_allowed_bsize`'s |
| 1301 | /// <=32x32 bound means `use_filter_intra` never reads at all, spec |
| 1302 | /// 5.11.14), so the only way this frame codes is `DC_PRED` with filter |
| 1303 | /// intra on top -- `--loopfilter-control=0` is required too: this |
| 1304 | /// decoder has no in-loop deblocking filter at all, and libaom's default |
| 1305 | /// (on) smooths every block-boundary column, which first looked like a |
| 1306 | /// filter-intra math bug (mismatches straddling the 32-pixel block |
| 1307 | /// seam) until the recipe without it round-tripped byte-exact. Checks |
| 1308 | /// [`decode::filter_intra_hits`] actually moved (a process-global |
| 1309 | /// counter, so before/after rather than an absolute value) to prove the |
| 1310 | /// symbol fired, then pixel-exactness against ffmpeg's own decode of the |
| 1311 | /// identical bytes on every plane. |
| 1312 | #[test] |
| 1313 | fn a_real_aomenc_filter_intra_stream_decodes_pixel_exact() { |
| 1314 | if !have_ffmpeg() { |
| 1315 | eprintln!("SKIP a_real_aomenc_filter_intra_stream_decodes_pixel_exact: no ffmpeg"); |
| 1316 | return; |
| 1317 | } |
| 1318 | if !have_aomenc() { |
| 1319 | eprintln!( |
| 1320 | "SKIP a_real_aomenc_filter_intra_stream_decodes_pixel_exact: no aomenc at {}", |
| 1321 | aomenc_path().display() |
| 1322 | ); |
| 1323 | return; |
| 1324 | } |
| 1325 | let (width, height) = (64usize, 64usize); |
| 1326 | // Recipe-search hooks: with the encoder pinned to one thread the |
| 1327 | // stream is a deterministic function of (seed, cq), so a firing recipe |
| 1328 | // can be searched for without a rebuild. |
| 1329 | let fi_seed: u32 = std::env::var("EC_FI_SEED") |
| 1330 | .ok() |
| 1331 | .and_then(|v| v.parse().ok()) |
| 1332 | .unwrap_or(42); |
| 1333 | let fi_source = gradients_source(fi_seed, 64, 64, "duration=0.04:rate=25"); |
| 1334 | let fi_cq = std::env::var("EC_FI_CQ").unwrap_or_else(|_| "40".to_string()); |
| 1335 | let fi_cq_arg = format!("--cq-level={fi_cq}"); |
| 1336 | let y4m = Command::new("ffmpeg") |
| 1337 | .args([ |
| 1338 | "-v", |
| 1339 | "error", |
| 1340 | "-f", |
| 1341 | "lavfi", |
| 1342 | "-i", |
| 1343 | &fi_source, |
| 1344 | "-pix_fmt", |
| 1345 | "yuv420p", |
| 1346 | "-f", |
| 1347 | "yuv4mpegpipe", |
| 1348 | "-", |
| 1349 | ]) |
| 1350 | .stdin(Stdio::null()) |
| 1351 | .stdout(Stdio::piped()) |
| 1352 | .stderr(Stdio::piped()) |
| 1353 | .output() |
| 1354 | .expect("ffmpeg failed to run"); |
| 1355 | assert!( |
| 1356 | y4m.status.success(), |
| 1357 | "ffmpeg refused to generate the y4m fixture: {}", |
| 1358 | String::from_utf8_lossy(&y4m.stderr) |
| 1359 | ); |
| 1360 | let mut child = Command::new(aomenc_path()) |
| 1361 | .args([ |
| 1362 | "--codec=av1", |
| 1363 | "--passes=1", |
| 1364 | "--end-usage=q", |
| 1365 | &fi_cq_arg, |
| 1366 | "--cpu-used=0", |
| 1367 | // Every sibling gate pins these; this one did not, and a |
| 1368 | // multi-threaded aomenc is not deterministic, so each run |
| 1369 | // encoded a slightly different stream and the gate flaked |
| 1370 | // against whichever features that run happened to pick. |
| 1371 | // Pinning them makes seed 42 mean one stream (class |
| 1372 | // `parallel-flake-is-attempt-selection`). |
| 1373 | "--threads=1", |
| 1374 | "--row-mt=0", |
| 1375 | "--enable-filter-intra=1", |
| 1376 | "--enable-smooth-intra=0", |
| 1377 | "--enable-paeth-intra=0", |
| 1378 | "--enable-directional-intra=0", |
| 1379 | "--enable-angle-delta=0", |
| 1380 | "--enable-tx-size-search=0", |
| 1381 | "--enable-cdef=0", |
| 1382 | "--max-partition-size=32", |
| 1383 | "--loopfilter-control=0", |
| 1384 | "--enable-palette=0", |
| 1385 | "--enable-intrabc=0", |
| 1386 | "--obu", |
| 1387 | "-o", |
| 1388 | "-", |
| 1389 | "-", |
| 1390 | ]) |
| 1391 | .stdin(Stdio::piped()) |
| 1392 | .stdout(Stdio::piped()) |
| 1393 | .stderr(Stdio::piped()) |
| 1394 | .spawn() |
| 1395 | .expect("aomenc failed to start"); |
| 1396 | child |
| 1397 | .stdin |
| 1398 | .take() |
| 1399 | .expect("aomenc stdin") |
| 1400 | .write_all(&y4m.stdout) |
| 1401 | .expect("writing the y4m fixture to aomenc"); |
| 1402 | let out = child.wait_with_output().expect("aomenc failed to run"); |
| 1403 | assert!( |
| 1404 | out.status.success(), |
| 1405 | "aomenc refused the fixture: {}", |
| 1406 | String::from_utf8_lossy(&out.stderr) |
| 1407 | ); |
| 1408 | let stream = out.stdout; |
| 1409 | let before = decode::filter_intra_hits(); |
| 1410 | // `screen_content_tools_determination` (libaom encoder_utils.c) is a |
| 1411 | // real internal trial-encode/PSNR comparison, not a flag this CLI |
| 1412 | // exposes a knob for -- on this flat gradient fixture it lands on |
| 1413 | // `allow_screen_content_tools=0` most runs but occasionally trips |
| 1414 | // over its 0.9 dB threshold the other way even with palette/intrabc |
| 1415 | // both disabled. That is a genuine separate gap (`decode_stream` |
| 1416 | // never reads `intrabc`/`palette_mode_info`), not a filter-intra |
| 1417 | // regression, so it is skipped here exactly as |
| 1418 | // `a_real_libaom_gradients_stream_with_cdef_decodes_pixel_exact` |
| 1419 | // above skips its own still-unsupported-gap runs. |
| 1420 | let frames = match decode_stream(&stream) { |
| 1421 | Ok(frames) => frames, |
| 1422 | Err(e) => { |
| 1423 | eprintln!("SKIP a_real_aomenc_filter_intra_stream_decodes_pixel_exact: {e}"); |
| 1424 | return; |
| 1425 | } |
| 1426 | }; |
| 1427 | assert!( |
| 1428 | decode::filter_intra_hits() > before, |
| 1429 | "use_filter_intra never fired decoding this stream" |
| 1430 | ); |
| 1431 | let ffmpeg_frames = ffmpeg_decode_sequence(&stream, width, height, 1); |
| 1432 | assert_eq!(frames[0].y, ffmpeg_frames[0].y, "luma vs ffmpeg"); |
| 1433 | assert_eq!(frames[0].u, ffmpeg_frames[0].u, "U vs ffmpeg"); |
| 1434 | assert_eq!(frames[0].v, ffmpeg_frames[0].v, "V vs ffmpeg"); |
| 1435 | } |
| 1436 | |
| 1437 | /// As [`a_real_aomenc_filter_intra_stream_decodes_pixel_exact`]'s |
| 1438 | /// harness, but with the deblocking filter left *on* (no |
| 1439 | /// `--loopfilter-control=0`) at a crf high enough that libaom actually |
| 1440 | /// picks a nonzero level -- proves `apply_deblock` itself, not just that |
| 1441 | /// this decoder still works when every edge lands on level 0. CDEF |
| 1442 | /// stays off so a pixel mismatch can only come from the loop filter. |
| 1443 | #[test] |
| 1444 | fn a_real_aomenc_intra_stream_with_deblocking_decodes_pixel_exact() { |
| 1445 | if !have_ffmpeg() { |
| 1446 | eprintln!( |
| 1447 | "SKIP a_real_aomenc_intra_stream_with_deblocking_decodes_pixel_exact: no ffmpeg" |
| 1448 | ); |
| 1449 | return; |
| 1450 | } |
| 1451 | if !have_aomenc() { |
| 1452 | eprintln!( |
| 1453 | "SKIP a_real_aomenc_intra_stream_with_deblocking_decodes_pixel_exact: no aomenc at {}", |
| 1454 | aomenc_path().display() |
| 1455 | ); |
| 1456 | return; |
| 1457 | } |
| 1458 | let (width, height) = (64usize, 64usize); |
| 1459 | let source = gradients_source(42, width, height, "duration=0.04:rate=25"); |
| 1460 | let y4m = Command::new("ffmpeg") |
| 1461 | .args(["-v", "error", "-f", "lavfi", "-i"]) |
| 1462 | .arg(&source) |
| 1463 | .args(["-pix_fmt", "yuv420p", "-f", "yuv4mpegpipe", "-"]) |
| 1464 | .stdin(Stdio::null()) |
| 1465 | .stdout(Stdio::piped()) |
| 1466 | .stderr(Stdio::piped()) |
| 1467 | .output() |
| 1468 | .expect("ffmpeg failed to run"); |
| 1469 | assert!( |
| 1470 | y4m.status.success(), |
| 1471 | "ffmpeg fixture: {}", |
| 1472 | String::from_utf8_lossy(&y4m.stderr) |
| 1473 | ); |
| 1474 | let mut child = Command::new(aomenc_path()) |
| 1475 | .args([ |
| 1476 | "--codec=av1", |
| 1477 | "--passes=1", |
| 1478 | "--end-usage=q", |
| 1479 | "--cq-level=55", |
| 1480 | "--cpu-used=0", |
| 1481 | "--enable-filter-intra=0", |
| 1482 | "--enable-smooth-intra=0", |
| 1483 | "--enable-paeth-intra=0", |
| 1484 | "--enable-directional-intra=0", |
| 1485 | "--enable-angle-delta=0", |
| 1486 | "--enable-tx-size-search=0", |
| 1487 | "--enable-cdef=0", |
| 1488 | // Loop restoration is a named refusal (aomenc's RD picks |
| 1489 | // Sgrproj on this kind of content) -- off in every recipe |
| 1490 | // whose test targets a different surface. |
| 1491 | "--enable-restoration=0", |
| 1492 | "--max-partition-size=32", |
| 1493 | "--enable-palette=0", |
| 1494 | "--enable-intrabc=0", |
| 1495 | "--enable-cfl-intra=0", |
| 1496 | // use_ref_frame_mvs (temporal MV projection) is unimplemented in mvstack; |
| 1497 | // leaving it on silently desyncs symbols on inter frames (grainfix lesson). |
| 1498 | "--enable-ref-frame-mvs=0", |
| 1499 | "--obu", |
| 1500 | "-o", |
| 1501 | "-", |
| 1502 | "-", |
| 1503 | ]) |
| 1504 | .stdin(Stdio::piped()) |
| 1505 | .stdout(Stdio::piped()) |
| 1506 | .stderr(Stdio::piped()) |
| 1507 | .spawn() |
| 1508 | .expect("aomenc failed to start"); |
| 1509 | child |
| 1510 | .stdin |
| 1511 | .take() |
| 1512 | .expect("aomenc stdin") |
| 1513 | .write_all(&y4m.stdout) |
| 1514 | .expect("writing y4m to aomenc"); |
| 1515 | let out = child.wait_with_output().expect("aomenc failed to run"); |
| 1516 | assert!( |
| 1517 | out.status.success(), |
| 1518 | "aomenc refused the fixture: {}", |
| 1519 | String::from_utf8_lossy(&out.stderr) |
| 1520 | ); |
| 1521 | let stream = out.stdout; |
| 1522 | let before = decode::deblock_hits(); |
| 1523 | let frames = match decode_stream(&stream) { |
| 1524 | Ok(frames) => frames, |
| 1525 | Err(e) => { |
| 1526 | eprintln!( |
| 1527 | "SKIP a_real_aomenc_intra_stream_with_deblocking_decodes_pixel_exact: {e}" |
| 1528 | ); |
| 1529 | return; |
| 1530 | } |
| 1531 | }; |
| 1532 | assert!( |
| 1533 | decode::deblock_hits() > before, |
| 1534 | "no deblocking edge fired decoding this stream -- raise cq-level" |
| 1535 | ); |
| 1536 | let ffmpeg_frames = ffmpeg_decode_sequence(&stream, width, height, 1); |
| 1537 | assert_eq!(frames[0].y, ffmpeg_frames[0].y, "luma vs ffmpeg"); |
| 1538 | assert_eq!(frames[0].u, ffmpeg_frames[0].u, "U vs ffmpeg"); |
| 1539 | assert_eq!(frames[0].v, ffmpeg_frames[0].v, "V vs ffmpeg"); |
| 1540 | } |
| 1541 | |
| 1542 | /// As [`a_real_aomenc_intra_stream_with_deblocking_decodes_pixel_exact`], |
| 1543 | /// but WITHOUT `--enable-tx-size-search=0` -- the one flag every other |
| 1544 | /// recipe in this file pins off specifically to keep the stream on |
| 1545 | /// `TxMode::Largest`. Dropped, aomenc's RD is free to pick |
| 1546 | /// `TxMode::Select` and split some blocks' luma transform below their own |
| 1547 | /// side (spec 5.11.16), which is what this test's `tx_depth_hits()` |
| 1548 | /// counter proves actually happened -- a stream that resolves every |
| 1549 | /// `tx_depth` to 0 would pixel-match by construction (identical to |
| 1550 | /// `TxMode::Largest`) without exercising the new split-TU decode path at |
| 1551 | /// all. Same multi-seed retry shape as |
| 1552 | /// [`a_real_aomenc_inter_sequence_with_cdf_forwarding_decodes_pixel_exact`]: |
| 1553 | /// aomenc's own RD is free (with tx-size search on) to also occasionally |
| 1554 | /// pick `allow_screen_content_tools` on this content, a separate, |
| 1555 | /// already-named refusal -- try several seeds, and the first stream that |
| 1556 | /// both decodes AND actually split a transform must be pixel-exact. |
| 1557 | #[test] |
| 1558 | fn a_real_aomenc_intra_stream_with_tx_select_decodes_pixel_exact() { |
| 1559 | if !have_ffmpeg() { |
| 1560 | eprintln!( |
| 1561 | "SKIP a_real_aomenc_intra_stream_with_tx_select_decodes_pixel_exact: no ffmpeg" |
| 1562 | ); |
| 1563 | return; |
| 1564 | } |
| 1565 | if !have_aomenc() { |
| 1566 | eprintln!( |
| 1567 | "SKIP a_real_aomenc_intra_stream_with_tx_select_decodes_pixel_exact: no aomenc at {}", |
| 1568 | aomenc_path().display() |
| 1569 | ); |
| 1570 | return; |
| 1571 | } |
| 1572 | let (width, height) = (64usize, 64usize); |
| 1573 | let mut refusals = Vec::new(); |
| 1574 | for attempt in 0..40u32 { |
| 1575 | let seed = 42 + attempt; |
| 1576 | // A flat gradient never has enough local detail for the RD |
| 1577 | // search to prefer a split transform over the block's own full |
| 1578 | // size -- `mandelbrot`'s fractal boundary gives every attempt |
| 1579 | // (a different `start_scale`) sharp, varied-frequency edges. |
| 1580 | // start_scale 5.0 is the proven first try (decodes pixel-exact |
| 1581 | // AND reads the seven-symbol set-1 tx_type rows on TX8 TUs); |
| 1582 | // later attempts walk down for variety when aomenc's RD flips |
| 1583 | // elsewhere. |
| 1584 | let source = format!( |
| 1585 | "mandelbrot=size=64x64:start_scale={}:rate=25", |
| 1586 | 5.0 - f64::from(attempt) * 0.06 |
| 1587 | ); |
| 1588 | let y4m = Command::new("ffmpeg") |
| 1589 | .args([ |
| 1590 | "-v", |
| 1591 | "error", |
| 1592 | "-f", |
| 1593 | "lavfi", |
| 1594 | "-i", |
| 1595 | &source, |
| 1596 | // Desaturated: chroma stays flat (constant 128) so |
| 1597 | // `uv_mode` always resolves to `DC_PRED` and this |
| 1598 | // decoder's still-unsupported directional-chroma round-2 |
| 1599 | // gap never fires, leaving the luma detail alone to |
| 1600 | // drive whether `TxMode::Select`'s RD splits a block. |
| 1601 | "-vf", |
| 1602 | "hue=s=0", |
| 1603 | "-pix_fmt", |
| 1604 | "yuv420p", |
| 1605 | "-t", |
| 1606 | "0.04", |
| 1607 | "-f", |
| 1608 | "yuv4mpegpipe", |
| 1609 | "-", |
| 1610 | ]) |
| 1611 | .stdin(Stdio::null()) |
| 1612 | .stdout(Stdio::piped()) |
| 1613 | .stderr(Stdio::piped()) |
| 1614 | .output() |
| 1615 | .expect("ffmpeg failed to run"); |
| 1616 | assert!( |
| 1617 | y4m.status.success(), |
| 1618 | "ffmpeg fixture: {}", |
| 1619 | String::from_utf8_lossy(&y4m.stderr) |
| 1620 | ); |
| 1621 | let mut child = Command::new(aomenc_path()) |
| 1622 | .args([ |
| 1623 | "--codec=av1", |
| 1624 | "--passes=1", |
| 1625 | "--end-usage=q", |
| 1626 | "--cq-level=45", |
| 1627 | "--cpu-used=0", |
| 1628 | "--enable-rect-partitions=0", |
| 1629 | "--enable-ab-partitions=0", |
| 1630 | "--enable-1to4-partitions=0", |
| 1631 | "--enable-filter-intra=0", |
| 1632 | // Every non-DC intra tool off: directional chroma and |
| 1633 | // angle deltas are separate round-2 gaps, and DC-only |
| 1634 | // residual is exactly what gives the RD search a reason |
| 1635 | // to split a transform. |
| 1636 | "--enable-smooth-intra=0", |
| 1637 | "--enable-paeth-intra=0", |
| 1638 | "--enable-directional-intra=0", |
| 1639 | "--enable-angle-delta=0", |
| 1640 | "--enable-cdef=0", |
| 1641 | // Loop restoration is a named refusal (aomenc's RD picks |
| 1642 | // Sgrproj on this kind of content) -- off in every recipe |
| 1643 | // whose test targets a different surface. |
| 1644 | "--enable-restoration=0", |
| 1645 | "--max-partition-size=32", |
| 1646 | // 16x16 blocks are what split into the TX8 TUs whose |
| 1647 | // set-1 tx_type rows this gate exists to prove; some |
| 1648 | // scales resolve depth 2 (TX4, a named refusal) and the |
| 1649 | // attempt loop just moves on. |
| 1650 | "--min-partition-size=16", |
| 1651 | "--enable-palette=0", |
| 1652 | "--enable-intrabc=0", |
| 1653 | "--enable-cfl-intra=0", |
| 1654 | // use_ref_frame_mvs (temporal MV projection) is unimplemented in mvstack; |
| 1655 | // leaving it on silently desyncs symbols on inter frames (grainfix lesson). |
| 1656 | "--enable-ref-frame-mvs=0", |
| 1657 | "--obu", |
| 1658 | "-o", |
| 1659 | "-", |
| 1660 | "-", |
| 1661 | ]) |
| 1662 | .stdin(Stdio::piped()) |
| 1663 | .stdout(Stdio::piped()) |
| 1664 | .stderr(Stdio::piped()) |
| 1665 | .spawn() |
| 1666 | .expect("aomenc failed to start"); |
| 1667 | child |
| 1668 | .stdin |
| 1669 | .take() |
| 1670 | .expect("aomenc stdin") |
| 1671 | .write_all(&y4m.stdout) |
| 1672 | .expect("writing y4m to aomenc"); |
| 1673 | let out = child.wait_with_output().expect("aomenc failed to run"); |
| 1674 | assert!( |
| 1675 | out.status.success(), |
| 1676 | "aomenc refused the fixture: {}", |
| 1677 | String::from_utf8_lossy(&out.stderr) |
| 1678 | ); |
| 1679 | let stream = out.stdout; |
| 1680 | let before = decode::tx_depth_hits(); |
| 1681 | let frames = match decode_stream(&stream) { |
| 1682 | Ok(frames) => frames, |
| 1683 | Err(e) => { |
| 1684 | refusals.push(format!("seed {seed}: {e}")); |
| 1685 | continue; |
| 1686 | } |
| 1687 | }; |
| 1688 | if decode::tx_depth_hits() == before { |
| 1689 | refusals.push(format!( |
| 1690 | "seed {seed}: decoded, but no block resolved a split tx_depth" |
| 1691 | )); |
| 1692 | continue; |
| 1693 | } |
| 1694 | let ffmpeg_frames = ffmpeg_decode_sequence(&stream, width, height, 1); |
| 1695 | assert_eq!( |
| 1696 | frames[0].y, ffmpeg_frames[0].y, |
| 1697 | "luma vs ffmpeg (seed {seed})" |
| 1698 | ); |
| 1699 | assert_eq!(frames[0].u, ffmpeg_frames[0].u, "U vs ffmpeg (seed {seed})"); |
| 1700 | assert_eq!(frames[0].v, ffmpeg_frames[0].v, "V vs ffmpeg (seed {seed})"); |
| 1701 | return; |
| 1702 | } |
| 1703 | eprintln!( |
| 1704 | "SKIP a_real_aomenc_intra_stream_with_tx_select_decodes_pixel_exact: every attempt \ |
| 1705 | hit a named refusal or never split a transform:\n{}", |
| 1706 | refusals.join("\n") |
| 1707 | ); |
| 1708 | } |
| 1709 | |
| 1710 | /// The directional-chroma/angle-delta gate: a real aomenc key frame with |
| 1711 | /// `--enable-directional-intra`/`--enable-angle-delta` left ON (the |
| 1712 | /// opposite of every sibling recipe's `=0`, which existed specifically to |
| 1713 | /// keep this round-2 gap from firing while a different surface was under |
| 1714 | /// test). A colourful, sharp-edged source gives the RD search a reason to |
| 1715 | /// pick a directional `uv_mode` and/or a nonzero angle delta -- some |
| 1716 | /// seeds still won't, so retry like every other real-encoder gate here, |
| 1717 | /// but the first attempt that actually fires either counter must decode |
| 1718 | /// pixel-exact. |
| 1719 | #[test] |
| 1720 | fn a_real_aomenc_stream_with_directional_chroma_decodes_pixel_exact() { |
| 1721 | if !have_ffmpeg() { |
| 1722 | eprintln!( |
| 1723 | "SKIP a_real_aomenc_stream_with_directional_chroma_decodes_pixel_exact: no ffmpeg" |
| 1724 | ); |
| 1725 | return; |
| 1726 | } |
| 1727 | if !have_aomenc() { |
| 1728 | eprintln!( |
| 1729 | "SKIP a_real_aomenc_stream_with_directional_chroma_decodes_pixel_exact: no aomenc at {}", |
| 1730 | aomenc_path().display() |
| 1731 | ); |
| 1732 | return; |
| 1733 | } |
| 1734 | let (width, height) = (64usize, 64usize); |
| 1735 | let mut refusals = Vec::new(); |
| 1736 | let mut fired_runs = 0u32; |
| 1737 | for attempt in 0..40u32 { |
| 1738 | let seed = 42 + attempt; |
| 1739 | let source = format!( |
| 1740 | "mandelbrot=size=64x64:start_scale={}:rate=25", |
| 1741 | 5.0 - f64::from(attempt) * 0.06 |
| 1742 | ); |
| 1743 | let y4m = Command::new("ffmpeg") |
| 1744 | .args([ |
| 1745 | "-v", |
| 1746 | "error", |
| 1747 | "-f", |
| 1748 | "lavfi", |
| 1749 | "-i", |
| 1750 | &source, |
| 1751 | "-pix_fmt", |
| 1752 | "yuv420p", |
| 1753 | "-t", |
| 1754 | "0.04", |
| 1755 | "-f", |
| 1756 | "yuv4mpegpipe", |
| 1757 | "-", |
| 1758 | ]) |
| 1759 | .stdin(Stdio::null()) |
| 1760 | .stdout(Stdio::piped()) |
| 1761 | .stderr(Stdio::piped()) |
| 1762 | .output() |
| 1763 | .expect("ffmpeg failed to run"); |
| 1764 | assert!( |
| 1765 | y4m.status.success(), |
| 1766 | "ffmpeg fixture: {}", |
| 1767 | String::from_utf8_lossy(&y4m.stderr) |
| 1768 | ); |
| 1769 | let mut child = Command::new(aomenc_path()) |
| 1770 | .args([ |
| 1771 | "--codec=av1", |
| 1772 | "--passes=1", |
| 1773 | "--end-usage=q", |
| 1774 | "--cq-level=45", |
| 1775 | "--cpu-used=0", |
| 1776 | "--enable-rect-partitions=0", |
| 1777 | "--enable-ab-partitions=0", |
| 1778 | "--enable-1to4-partitions=0", |
| 1779 | "--enable-filter-intra=0", |
| 1780 | // Smooth/Paeth chroma is a separate, still-refused round-2 |
| 1781 | // gap (an unrelated `Edges::build` no-neighbour bug this |
| 1782 | // lane does not cover) -- off so the RD search's only |
| 1783 | // non-DC/CFL chroma choice left is directional. |
| 1784 | "--enable-smooth-intra=0", |
| 1785 | "--enable-paeth-intra=0", |
| 1786 | "--enable-tx-size-search=0", |
| 1787 | "--enable-cdef=0", |
| 1788 | "--enable-restoration=0", |
| 1789 | // The intra edge filter/upsample (spec 7.11.2.7-2.9) is |
| 1790 | // now implemented (`directional()`) -- left ON (aomenc's |
| 1791 | // own default) so this gate actually exercises it instead |
| 1792 | // of the un-filtered fast path a disabled flag would keep |
| 1793 | // testing. |
| 1794 | "--enable-intra-edge-filter=1", |
| 1795 | "--max-partition-size=32", |
| 1796 | // Same reason as the tx-select gate above: 8x8 and other |
| 1797 | // sub-16x16 splits are a separate, already-named refusal. |
| 1798 | "--min-partition-size=16", |
| 1799 | "--enable-palette=0", |
| 1800 | "--enable-intrabc=0", |
| 1801 | "--enable-cfl-intra=0", |
| 1802 | // use_ref_frame_mvs (temporal MV projection) is unimplemented in mvstack; |
| 1803 | // leaving it on silently desyncs symbols on inter frames (grainfix lesson). |
| 1804 | "--enable-ref-frame-mvs=0", |
| 1805 | "--obu", |
| 1806 | "-o", |
| 1807 | "-", |
| 1808 | "-", |
| 1809 | ]) |
| 1810 | .stdin(Stdio::piped()) |
| 1811 | .stdout(Stdio::piped()) |
| 1812 | .stderr(Stdio::piped()) |
| 1813 | .spawn() |
| 1814 | .expect("aomenc failed to start"); |
| 1815 | child |
| 1816 | .stdin |
| 1817 | .take() |
| 1818 | .expect("aomenc stdin") |
| 1819 | .write_all(&y4m.stdout) |
| 1820 | .expect("writing y4m to aomenc"); |
| 1821 | let out = child.wait_with_output().expect("aomenc failed to run"); |
| 1822 | assert!( |
| 1823 | out.status.success(), |
| 1824 | "aomenc refused the fixture: {}", |
| 1825 | String::from_utf8_lossy(&out.stderr) |
| 1826 | ); |
| 1827 | let stream = out.stdout; |
| 1828 | let (uv_before, angle_before) = |
| 1829 | (decode::directional_uv_hits(), decode::angle_delta_hits()); |
| 1830 | let frames = match decode_stream(&stream) { |
| 1831 | Ok(frames) => frames, |
| 1832 | Err(e) => { |
| 1833 | refusals.push(format!("seed {seed}: {e}")); |
| 1834 | continue; |
| 1835 | } |
| 1836 | }; |
| 1837 | if decode::directional_uv_hits() == uv_before |
| 1838 | && decode::angle_delta_hits() == angle_before |
| 1839 | { |
| 1840 | refusals.push(format!( |
| 1841 | "seed {seed}: decoded, but neither a directional uv_mode nor a \ |
| 1842 | nonzero angle delta fired" |
| 1843 | )); |
| 1844 | continue; |
| 1845 | } |
| 1846 | fired_runs += 1; |
| 1847 | let ffmpeg_frames = ffmpeg_decode_sequence(&stream, width, height, 1); |
| 1848 | assert_eq!( |
| 1849 | frames[0].y, ffmpeg_frames[0].y, |
| 1850 | "luma vs ffmpeg (seed {seed})" |
| 1851 | ); |
| 1852 | assert_eq!(frames[0].u, ffmpeg_frames[0].u, "U vs ffmpeg (seed {seed})"); |
| 1853 | assert_eq!(frames[0].v, ffmpeg_frames[0].v, "V vs ffmpeg (seed {seed})"); |
| 1854 | if fired_runs >= 4 { |
| 1855 | return; |
| 1856 | } |
| 1857 | } |
| 1858 | assert!( |
| 1859 | fired_runs > 0, |
| 1860 | "every attempt hit a named refusal or never exercised directional chroma:\n{}", |
| 1861 | refusals.join("\n") |
| 1862 | ); |
| 1863 | } |
| 1864 | |
| 1865 | /// As above, but a multi-frame inter sequence -- `--lag-in-frames=0 |
| 1866 | /// --auto-alt-ref=0` keep every non-key frame a simple forward |
| 1867 | /// single-`LAST_FRAME` P frame (no alt-ref/backward reference this |
| 1868 | /// decoder's inter path does not model), `--kf-max-dist` past the clip's |
| 1869 | /// own length keeps frame 0 the only key frame. |
| 1870 | #[test] |
| 1871 | fn a_real_aomenc_inter_sequence_with_deblocking_decodes_pixel_exact() { |
| 1872 | if !have_ffmpeg() { |
| 1873 | eprintln!( |
| 1874 | "SKIP a_real_aomenc_inter_sequence_with_deblocking_decodes_pixel_exact: no ffmpeg" |
| 1875 | ); |
| 1876 | return; |
| 1877 | } |
| 1878 | if !have_aomenc() { |
| 1879 | eprintln!( |
| 1880 | "SKIP a_real_aomenc_inter_sequence_with_deblocking_decodes_pixel_exact: no aomenc at {}", |
| 1881 | aomenc_path().display() |
| 1882 | ); |
| 1883 | return; |
| 1884 | } |
| 1885 | let (width, height, frame_count) = (64usize, 64usize, 4usize); |
| 1886 | let source = gradients_source(42, width, height, "duration=0.16:rate=25"); |
| 1887 | let y4m = Command::new("ffmpeg") |
| 1888 | .args(["-v", "error", "-f", "lavfi", "-i"]) |
| 1889 | .arg(&source) |
| 1890 | .args([ |
| 1891 | "-pix_fmt", |
| 1892 | "yuv420p", |
| 1893 | "-f", |
| 1894 | "yuv4mpegpipe", |
| 1895 | "-", |
| 1896 | ]) |
| 1897 | .stdin(Stdio::null()) |
| 1898 | .stdout(Stdio::piped()) |
| 1899 | .stderr(Stdio::piped()) |
| 1900 | .output() |
| 1901 | .expect("ffmpeg failed to run"); |
| 1902 | assert!( |
| 1903 | y4m.status.success(), |
| 1904 | "ffmpeg fixture: {}", |
| 1905 | String::from_utf8_lossy(&y4m.stderr) |
| 1906 | ); |
| 1907 | let mut child = Command::new(aomenc_path()) |
| 1908 | .args([ |
| 1909 | "--codec=av1", |
| 1910 | "--passes=1", |
| 1911 | "--end-usage=q", |
| 1912 | "--cq-level=55", |
| 1913 | "--cpu-used=0", |
| 1914 | "--lag-in-frames=0", |
| 1915 | "--auto-alt-ref=0", |
| 1916 | "--kf-max-dist=1000", |
| 1917 | // Forces `primary_ref_frame = PRIMARY_REF_NONE` on every |
| 1918 | // frame (spec 5.9.2): without it a real encoder's inter |
| 1919 | // frame loads its initial CDF state from the previous |
| 1920 | // frame's *adapted* tables (spec 7.20's `load_cdfs`), which |
| 1921 | // this decoder never does -- it always starts each frame's |
| 1922 | // `Cdfs` from the spec defaults (`decode.rs`'s |
| 1923 | // `Cdfs::new(q_ctx)`, called fresh per frame in |
| 1924 | // `decode_stream`). Without this flag the inter frame's |
| 1925 | // very first symbol desyncs immediately (traced 2026-08-27: |
| 1926 | // no coefficient trace at all before the tile's first |
| 1927 | // partition read comes back an out-of-alphabet value). A |
| 1928 | // real decoder needs cross-frame CDF forwarding to handle |
| 1929 | // that stream; this fixture sidesteps it the same way a |
| 1930 | // real broadcast/low-latency encode legitimately can. |
| 1931 | "--error-resilient=1", |
| 1932 | "--enable-rect-partitions=0", |
| 1933 | "--enable-ab-partitions=0", |
| 1934 | "--enable-1to4-partitions=0", |
| 1935 | "--enable-filter-intra=0", |
| 1936 | "--enable-smooth-intra=0", |
| 1937 | "--enable-paeth-intra=0", |
| 1938 | "--enable-directional-intra=0", |
| 1939 | "--enable-angle-delta=0", |
| 1940 | "--enable-tx-size-search=0", |
| 1941 | "--enable-cdef=0", |
| 1942 | // Loop restoration is a named refusal (aomenc's RD picks |
| 1943 | // Sgrproj on this kind of content) -- off in every recipe |
| 1944 | // whose test targets a different surface. |
| 1945 | "--enable-restoration=0", |
| 1946 | "--max-partition-size=32", |
| 1947 | "--enable-palette=0", |
| 1948 | "--enable-intrabc=0", |
| 1949 | "--enable-cfl-intra=0", |
| 1950 | // use_ref_frame_mvs (temporal MV projection) is unimplemented in mvstack; |
| 1951 | // leaving it on silently desyncs symbols on inter frames (grainfix lesson). |
| 1952 | "--enable-ref-frame-mvs=0", |
| 1953 | "--obu", |
| 1954 | "-o", |
| 1955 | "-", |
| 1956 | "-", |
| 1957 | ]) |
| 1958 | .stdin(Stdio::piped()) |
| 1959 | .stdout(Stdio::piped()) |
| 1960 | .stderr(Stdio::piped()) |
| 1961 | .spawn() |
| 1962 | .expect("aomenc failed to start"); |
| 1963 | child |
| 1964 | .stdin |
| 1965 | .take() |
| 1966 | .expect("aomenc stdin") |
| 1967 | .write_all(&y4m.stdout) |
| 1968 | .expect("writing y4m to aomenc"); |
| 1969 | let out = child.wait_with_output().expect("aomenc failed to run"); |
| 1970 | assert!( |
| 1971 | out.status.success(), |
| 1972 | "aomenc refused the fixture: {}", |
| 1973 | String::from_utf8_lossy(&out.stderr) |
| 1974 | ); |
| 1975 | let stream = out.stdout; |
| 1976 | if let Ok(dump) = std::env::var("EC_AV1_GATE_DUMP") { |
| 1977 | std::fs::write(dump, &stream).expect("dump stream"); |
| 1978 | } |
| 1979 | let before = decode::deblock_hits(); |
| 1980 | let frames = match decode_stream(&stream) { |
| 1981 | Ok(frames) => frames, |
| 1982 | Err(e) => { |
| 1983 | eprintln!( |
| 1984 | "SKIP a_real_aomenc_inter_sequence_with_deblocking_decodes_pixel_exact: {e}" |
| 1985 | ); |
| 1986 | return; |
| 1987 | } |
| 1988 | }; |
| 1989 | assert!( |
| 1990 | decode::deblock_hits() > before, |
| 1991 | "no deblocking edge fired decoding this sequence -- raise cq-level" |
| 1992 | ); |
| 1993 | let ffmpeg_frames = ffmpeg_decode_sequence(&stream, width, height, frame_count); |
| 1994 | assert_eq!(frames.len(), frame_count); |
| 1995 | for (i, (got, want)) in frames.iter().zip(&ffmpeg_frames).enumerate() { |
| 1996 | // gradients' colors ignore its seed, so this gate encodes fresh |
| 1997 | // content every run -- a mismatch here may never reproduce. |
| 1998 | // Self-pin the stream before panicking so the failure is |
| 1999 | // bisectable (replay: EC_AV1_PIN on scratch_isolate_pinned_mismatch). |
| 2000 | let ok = got.y == want.y && got.u == want.u && got.v == want.v; |
| 2001 | if !ok { |
| 2002 | let pin = std::env::temp_dir().join("ec-av1-deblocking-gate-fail.obu"); |
| 2003 | let _ = std::fs::write(&pin, &stream); |
| 2004 | panic!("frame {i} mismatch vs ffmpeg -- stream pinned at {}", pin.display()); |
| 2005 | } |
| 2006 | } |
| 2007 | } |
| 2008 | |
| 2009 | /// The decisive cross-frame CDF forwarding test: identical to |
| 2010 | /// [`a_real_aomenc_inter_sequence_with_deblocking_decodes_pixel_exact`] |
| 2011 | /// except it drops `--error-resilient=1`, so aomenc codes every inter |
| 2012 | /// frame's `primary_ref_frame` at its default (not `PRIMARY_REF_NONE`) |
| 2013 | /// and expects the decoder to resume from the previous frame's *adapted* |
| 2014 | /// CDF state (spec 7.20 `load_cdfs`). Without cross-frame forwarding, |
| 2015 | /// frame 1's very first tile symbol desyncs (an out-of-alphabet |
| 2016 | /// partition read); with it, every frame decodes pixel-exact. |
| 2017 | #[test] |
| 2018 | fn a_real_aomenc_inter_sequence_with_cdf_forwarding_decodes_pixel_exact() { |
| 2019 | if !have_ffmpeg() { |
| 2020 | eprintln!( |
| 2021 | "SKIP a_real_aomenc_inter_sequence_with_cdf_forwarding_decodes_pixel_exact: no ffmpeg" |
| 2022 | ); |
| 2023 | return; |
| 2024 | } |
| 2025 | if !have_aomenc() { |
| 2026 | eprintln!( |
| 2027 | "SKIP a_real_aomenc_inter_sequence_with_cdf_forwarding_decodes_pixel_exact: no aomenc at {}", |
| 2028 | aomenc_path().display() |
| 2029 | ); |
| 2030 | return; |
| 2031 | } |
| 2032 | let (width, height, frame_count) = (64usize, 64usize, 4usize); |
| 2033 | // The fixture's content varies run to run (lavfi gradients ignores its |
| 2034 | // seed option for the c0..c7 colors -- they default to "random"), so: |
| 2035 | // most encodes of this recipe trip one of the decoder's NAMED round-2 |
| 2036 | // refusals (a reference other than LAST_FRAME, a sub-16 partition -- |
| 2037 | // genuine coverage gaps, separately documented), and only some produce |
| 2038 | // a stream fully inside this decoder's declared support. So: attempt |
| 2039 | // several seeds, and the first stream that DECODES must be pixel-exact |
| 2040 | // -- that assertion is what guards the forwarded-CDF state (a wrong |
| 2041 | // table decodes plausible-but-wrong pixels; the counter-reset bug this |
| 2042 | // gate caught did exactly that). Exhausting every attempt on named |
| 2043 | // refusals skips loudly, listing them. |
| 2044 | let mut refusals = Vec::new(); |
| 2045 | for attempt in 0..8u32 { |
| 2046 | let seed = 42 + attempt; |
| 2047 | let source = gradients_source(seed, 64, 64, "duration=0.16:rate=25"); |
| 2048 | let y4m = Command::new("ffmpeg") |
| 2049 | .args([ |
| 2050 | "-v", |
| 2051 | "error", |
| 2052 | "-f", |
| 2053 | "lavfi", |
| 2054 | "-i", |
| 2055 | &source, |
| 2056 | "-pix_fmt", |
| 2057 | "yuv420p", |
| 2058 | "-f", |
| 2059 | "yuv4mpegpipe", |
| 2060 | "-", |
| 2061 | ]) |
| 2062 | .stdin(Stdio::null()) |
| 2063 | .stdout(Stdio::piped()) |
| 2064 | .stderr(Stdio::piped()) |
| 2065 | .output() |
| 2066 | .expect("ffmpeg failed to run"); |
| 2067 | assert!( |
| 2068 | y4m.status.success(), |
| 2069 | "ffmpeg fixture: {}", |
| 2070 | String::from_utf8_lossy(&y4m.stderr) |
| 2071 | ); |
| 2072 | // lane-cdfflake: hard length check on the y4m fixture itself -- |
| 2073 | // a truncated/racing ffmpeg write would otherwise surface as a |
| 2074 | // downstream pixel mismatch indistinguishable from a real decode |
| 2075 | // defect. y4m frames are `FRAME\n` (6 bytes) + raw 4:2:0 payload; |
| 2076 | // find the header/frame-data boundary at the first `FRAME` marker |
| 2077 | // rather than parsing the header fields (duration/rate can move |
| 2078 | // the header text length without moving the frame math). |
| 2079 | let marker_at = y4m |
| 2080 | .stdout |
| 2081 | .windows(5) |
| 2082 | .position(|w| w == b"FRAME") |
| 2083 | .expect("y4m stream missing FRAME marker"); |
| 2084 | let frame_420_bytes = width * height * 3 / 2; |
| 2085 | assert_eq!( |
| 2086 | y4m.stdout.len(), |
| 2087 | marker_at + frame_count * (6 + frame_420_bytes), |
| 2088 | "y4m fixture length mismatch (seed {seed})" |
| 2089 | ); |
| 2090 | let y4m_hash = fnv1a64(&y4m.stdout); |
| 2091 | let mut child = Command::new(aomenc_path()) |
| 2092 | .args([ |
| 2093 | "--codec=av1", |
| 2094 | "--passes=1", |
| 2095 | "--end-usage=q", |
| 2096 | "--cq-level=55", |
| 2097 | "--cpu-used=0", |
| 2098 | "--lag-in-frames=0", |
| 2099 | "--auto-alt-ref=0", |
| 2100 | "--kf-max-dist=1000", |
| 2101 | // `--threads=1` (not the default): a multithreaded RD search |
| 2102 | // makes aomenc's screen-content-tools heuristic race (seen |
| 2103 | // flaky under `cargo test`'s own parallelism -- the same |
| 2104 | // fixture, same seed, occasionally sets |
| 2105 | // `allow_screen_content_tools`, a round-2 gap unrelated to |
| 2106 | // CDF forwarding), so pin it to one thread for a |
| 2107 | // deterministic header. |
| 2108 | "--threads=1", |
| 2109 | "--row-mt=0", |
| 2110 | // `--enable-order-hint=0` kills BWDREF/ALTREF entirely (no |
| 2111 | // order hints -> no bidirectional distance weighting) and, |
| 2112 | // combined with `--auto-alt-ref=0`/`--lag-in-frames=0` |
| 2113 | // above, leaves aomenc's RD search only LAST_FRAME/ |
| 2114 | // LAST2_FRAME/LAST3_FRAME/GOLDEN_FRAME to pick from -- |
| 2115 | // GOLDEN itself needs no order hint (it is the keyframe's |
| 2116 | // fixed slot, not a distance-weighted reference), so this |
| 2117 | // does not suppress GOLDEN selection the way the sibling |
| 2118 | // CDF-forwarding gate's own comment worried about; it just |
| 2119 | // narrows the pool this gate needs GOLDEN to win out of. |
| 2120 | "--enable-order-hint=0", |
| 2121 | "--enable-warped-motion=0", |
| 2122 | "--enable-obmc=0", |
| 2123 | "--enable-masked-comp=0", |
| 2124 | "--enable-interintra-comp=0", |
| 2125 | "--enable-dist-wtd-comp=0", |
| 2126 | "--enable-diff-wtd-comp=0", |
| 2127 | "--enable-onesided-comp=0", |
| 2128 | "--enable-interintra-wedge=0", |
| 2129 | "--enable-smooth-interintra=0", |
| 2130 | "--enable-rect-partitions=0", |
| 2131 | "--enable-ab-partitions=0", |
| 2132 | "--enable-1to4-partitions=0", |
| 2133 | "--enable-filter-intra=0", |
| 2134 | "--enable-smooth-intra=0", |
| 2135 | "--enable-paeth-intra=0", |
| 2136 | "--enable-directional-intra=0", |
| 2137 | "--enable-angle-delta=0", |
| 2138 | "--enable-tx-size-search=0", |
| 2139 | "--enable-cdef=0", |
| 2140 | // Loop restoration is a named refusal (aomenc's RD picks |
| 2141 | // Sgrproj on this kind of content) -- off in every recipe |
| 2142 | // whose test targets a different surface. |
| 2143 | "--enable-restoration=0", |
| 2144 | "--max-partition-size=32", |
| 2145 | "--enable-palette=0", |
| 2146 | "--enable-intrabc=0", |
| 2147 | "--enable-cfl-intra=0", |
| 2148 | // use_ref_frame_mvs (temporal MV projection) is unimplemented in mvstack; |
| 2149 | // leaving it on silently desyncs symbols on inter frames (grainfix lesson). |
| 2150 | "--enable-ref-frame-mvs=0", |
| 2151 | "--obu", |
| 2152 | "-o", |
| 2153 | "-", |
| 2154 | "-", |
| 2155 | ]) |
| 2156 | .stdin(Stdio::piped()) |
| 2157 | .stdout(Stdio::piped()) |
| 2158 | .stderr(Stdio::piped()) |
| 2159 | .spawn() |
| 2160 | .expect("aomenc failed to start"); |
| 2161 | child |
| 2162 | .stdin |
| 2163 | .take() |
| 2164 | .expect("aomenc stdin") |
| 2165 | .write_all(&y4m.stdout) |
| 2166 | .expect("writing y4m to aomenc"); |
| 2167 | let out = child.wait_with_output().expect("aomenc failed to run"); |
| 2168 | assert!( |
| 2169 | out.status.success(), |
| 2170 | "aomenc refused the fixture: {}", |
| 2171 | String::from_utf8_lossy(&out.stderr) |
| 2172 | ); |
| 2173 | let stream = out.stdout; |
| 2174 | let stream_hash = fnv1a64(&stream); |
| 2175 | // A named `unsupported` refusal is a documented coverage gap (each |
| 2176 | // one carries its own test debt elsewhere), not a forwarding verdict |
| 2177 | // -- try the next seed. Any stream that DECODES must be pixel-exact, |
| 2178 | // and any non-refusal error is a hard failure. |
| 2179 | let frames = match decode_stream(&stream) { |
| 2180 | Err(e) => { |
| 2181 | let msg = e.to_string(); |
| 2182 | assert!( |
| 2183 | msg.contains("unsupported"), |
| 2184 | "cross-frame CDF forwarding failed outright (seed {seed}): {msg}" |
| 2185 | ); |
| 2186 | refusals.push(format!("seed {seed}: {msg}")); |
| 2187 | continue; |
| 2188 | } |
| 2189 | Ok(frames) => frames, |
| 2190 | }; |
| 2191 | let ffmpeg_frames = ffmpeg_decode_sequence(&stream, width, height, frame_count); |
| 2192 | assert_eq!(frames.len(), frame_count); |
| 2193 | // lane-cdfflake: reconstruct ffmpeg's raw reference bytes (the |
| 2194 | // three per-frame slices are contiguous chunks of the raw |
| 2195 | // `-f rawvideo` output ffmpeg_decode_sequence sliced, so |
| 2196 | // concatenating them back is bit-for-bit the same bytes ffmpeg |
| 2197 | // actually produced -- no second ffmpeg invocation needed). |
| 2198 | let ffmpeg_raw: Vec<u8> = ffmpeg_frames |
| 2199 | .iter() |
| 2200 | .flat_map(|p| p.y.iter().chain(&p.u).chain(&p.v).copied()) |
| 2201 | .collect(); |
| 2202 | let ffmpeg_raw_hash = fnv1a64(&ffmpeg_raw); |
| 2203 | eprintln!( |
| 2204 | "cdfflake attempt seed={seed}: y4m_hash={y4m_hash:016x} \ |
| 2205 | stream_hash={stream_hash:016x} ffmpeg_raw_hash={ffmpeg_raw_hash:016x}" |
| 2206 | ); |
| 2207 | // lane-av1golden3: pin the exact raw stream on the first mismatch |
| 2208 | // so a flaky-looking gate failure becomes a deterministic, static |
| 2209 | // fixture to bisect against -- env-gated, no cost on a normal run. |
| 2210 | let mismatched = frames |
| 2211 | .iter() |
| 2212 | .zip(&ffmpeg_frames) |
| 2213 | .any(|(got, want)| got.y != want.y || got.u != want.u || got.v != want.v); |
| 2214 | if mismatched { |
| 2215 | if let Ok(path) = std::env::var("EC_AV1_GATE_DUMP") { |
| 2216 | std::fs::write(&path, &stream).expect("writing pinned stream"); |
| 2217 | eprintln!("EC_AV1_GATE_DUMP: wrote mismatching stream (seed {seed}) to {path}"); |
| 2218 | } |
| 2219 | // lane-cdfflake: unconditional pin on mismatch -- the aomenc |
| 2220 | // stream AND ffmpeg's raw reference bytes, so a flake caught |
| 2221 | // under load names the liar (stream bytes differ from a |
| 2222 | // clean rerun -> subprocess instability; reference bytes |
| 2223 | // differ -> ffmpeg instability; both stable -> real decode |
| 2224 | // defect) without relying on the env var being set. |
| 2225 | // The pin goes to `fixtures/`, which outlives the session: an |
| 2226 | // earlier version wrote to a session scratchpad, so once tmpfs |
| 2227 | // reaped it a real mismatch aborted on the write instead of |
| 2228 | // reporting itself. A failed write must not mask the assert |
| 2229 | // below either, so it warns rather than panicking. |
| 2230 | let sp = pin_dir(); |
| 2231 | let stream_pin = sp.join(format!("cdfflake-stream-seed{seed}.bin")); |
| 2232 | let ref_pin = sp.join(format!("cdfflake-ffmpeg-raw-seed{seed}.bin")); |
| 2233 | for (path, bytes) in [(&stream_pin, &stream), (&ref_pin, &ffmpeg_raw)] { |
| 2234 | if let Err(e) = std::fs::write(path, bytes) { |
| 2235 | eprintln!("cdfflake: could not pin to {}: {e}", path.display()); |
| 2236 | } |
| 2237 | } |
| 2238 | eprintln!( |
| 2239 | "cdfflake MISMATCH seed={seed}: pinned stream + ffmpeg reference to {}", |
| 2240 | sp.display() |
| 2241 | ); |
| 2242 | } |
| 2243 | for (i, (got, want)) in frames.iter().zip(&ffmpeg_frames).enumerate() { |
| 2244 | assert_eq!(got.y, want.y, "frame {i} luma vs ffmpeg (seed {seed})"); |
| 2245 | assert_eq!(got.u, want.u, "frame {i} U vs ffmpeg (seed {seed})"); |
| 2246 | assert_eq!(got.v, want.v, "frame {i} V vs ffmpeg (seed {seed})"); |
| 2247 | } |
| 2248 | return; |
| 2249 | } |
| 2250 | eprintln!( |
| 2251 | "SKIP a_real_aomenc_inter_sequence_with_cdf_forwarding_decodes_pixel_exact: every \ |
| 2252 | attempt hit a named refusal:\n{}", |
| 2253 | refusals.join("\n") |
| 2254 | ); |
| 2255 | } |
| 2256 | |
| 2257 | /// lane-av1golden3: reproduces the pinned mismatch bytes captured by |
| 2258 | /// `EC_AV1_GATE_DUMP=$SP/golden3-pin.obu` off |
| 2259 | /// [`a_real_aomenc_inter_sequence_with_cdf_forwarding_decodes_pixel_exact`] |
| 2260 | /// (seed 47, frame 1 U). Deterministic and static -- no aomenc/ffmpeg |
| 2261 | /// re-encode involved, only re-decodes the fixed bytes on disk, so it is |
| 2262 | /// `#[ignore]`d (the file only exists on this machine's scratchpad) but |
| 2263 | /// gives a fast red/green loop for the actual fix. |
| 2264 | #[test] |
| 2265 | #[ignore = "reads a pinned fixture path outside the repo; run manually"] |
| 2266 | fn pinned_golden3_stream_decodes_pixel_exact() { |
| 2267 | let path = std::env::var("EC_AV1_GATE_DUMP_PIN") |
| 2268 | .map(std::path::PathBuf::from) |
| 2269 | .unwrap_or_else(|_| pin_dir().join("golden3-pin.obu")); |
| 2270 | let Ok(stream) = std::fs::read(&path) else { |
| 2271 | eprintln!( |
| 2272 | "SKIP pinned_golden3_stream_decodes_pixel_exact: no pinned bytes at {} \ |
| 2273 | -- re-capture with EC_AV1_GATE_DUMP off the cdf-forwarding gate", |
| 2274 | path.display() |
| 2275 | ); |
| 2276 | return; |
| 2277 | }; |
| 2278 | if !have_ffmpeg() { |
| 2279 | eprintln!("SKIP pinned_golden3_stream_decodes_pixel_exact: no ffmpeg"); |
| 2280 | return; |
| 2281 | } |
| 2282 | let frames = decode_stream(&stream).expect("pinned stream must decode"); |
| 2283 | let ffmpeg_frames = ffmpeg_decode_sequence(&stream, 64, 64, 4); |
| 2284 | for (i, (got, want)) in frames.iter().zip(&ffmpeg_frames).enumerate() { |
| 2285 | assert_eq!(got.y, want.y, "frame {i} luma vs ffmpeg (pinned)"); |
| 2286 | assert_eq!(got.u, want.u, "frame {i} U vs ffmpeg (pinned)"); |
| 2287 | assert_eq!(got.v, want.v, "frame {i} V vs ffmpeg (pinned)"); |
| 2288 | } |
| 2289 | } |
| 2290 | |
| 2291 | /// lane-av1golden4: reproduces the pinned mismatch bytes captured by |
| 2292 | /// `EC_AV1_GATE_DUMP=$SP/golden4-pin.obu` off |
| 2293 | /// [`a_real_aomenc_inter_sequence_with_cdf_forwarding_decodes_pixel_exact`] |
| 2294 | /// (seed 43, frame 3 luma) the round the GOLDEN mask lifted. Deterministic |
| 2295 | /// and static -- `#[ignore]`d, run manually. |
| 2296 | #[test] |
| 2297 | #[ignore = "reads a pinned fixture path outside the repo; run manually"] |
| 2298 | fn pinned_golden4_stream_decodes_pixel_exact() { |
| 2299 | use crate::decode::non_last_ref_hits; |
| 2300 | let path = std::env::var("EC_AV1_GATE_DUMP_PIN") |
| 2301 | .map(std::path::PathBuf::from) |
| 2302 | .unwrap_or_else(|_| pin_dir().join("golden4-pin.obu")); |
| 2303 | let Ok(stream) = std::fs::read(&path) else { |
| 2304 | eprintln!( |
| 2305 | "SKIP pinned_golden4_stream_decodes_pixel_exact: no pinned bytes at {} \ |
| 2306 | -- re-capture with EC_AV1_GATE_DUMP off the cdf-forwarding gate", |
| 2307 | path.display() |
| 2308 | ); |
| 2309 | return; |
| 2310 | }; |
| 2311 | if !have_ffmpeg() { |
| 2312 | eprintln!("SKIP pinned_golden4_stream_decodes_pixel_exact: no ffmpeg"); |
| 2313 | return; |
| 2314 | } |
| 2315 | let before = non_last_ref_hits(); |
| 2316 | let frames = decode_stream(&stream).expect("pinned stream must decode"); |
| 2317 | eprintln!( |
| 2318 | "non_last_ref_hits before={before} after={}", |
| 2319 | non_last_ref_hits() |
| 2320 | ); |
| 2321 | let ffmpeg_frames = ffmpeg_decode_sequence(&stream, 64, 64, 4); |
| 2322 | for (i, (got, want)) in frames.iter().zip(&ffmpeg_frames).enumerate() { |
| 2323 | assert_eq!(got.y, want.y, "frame {i} luma vs ffmpeg (pinned)"); |
| 2324 | assert_eq!(got.u, want.u, "frame {i} U vs ffmpeg (pinned)"); |
| 2325 | assert_eq!(got.v, want.v, "frame {i} V vs ffmpeg (pinned)"); |
| 2326 | } |
| 2327 | } |
| 2328 | |
| 2329 | /// lane-av1golden7 round 9: pins the residual GOLDEN_FRAME MC bug found |
| 2330 | /// by the forwarding gate once film grain is out of the way (1/20 real |
| 2331 | /// aomenc streams, frame 3 luma, apply_grain=false, `non_last_ref_hits` |
| 2332 | /// delta=2). `GOLDEN_FRAME` refuses by name until this decodes exact. |
| 2333 | #[test] |
| 2334 | #[ignore = "reads a pinned fixture path outside the repo; run manually"] |
| 2335 | fn pinned_golden7_stream_decodes_pixel_exact() { |
| 2336 | use crate::decode::non_last_ref_hits; |
| 2337 | let path = concat!( |
| 2338 | env!("CARGO_MANIFEST_DIR"), |
| 2339 | "/../../fixtures/golden7-forwarding-mismatch.obu" |
| 2340 | ); |
| 2341 | let stream = std::fs::read(path).expect("reading pinned stream"); |
| 2342 | if !have_ffmpeg() { |
| 2343 | eprintln!("SKIP pinned_golden7_stream_decodes_pixel_exact: no ffmpeg"); |
| 2344 | return; |
| 2345 | } |
| 2346 | let before = non_last_ref_hits(); |
| 2347 | let frames = decode_stream(&stream).expect("pinned stream must decode"); |
| 2348 | eprintln!( |
| 2349 | "non_last_ref_hits before={before} after={}", |
| 2350 | non_last_ref_hits() |
| 2351 | ); |
| 2352 | let ffmpeg_frames = ffmpeg_decode_sequence(&stream, 64, 64, 4); |
| 2353 | for (i, (got, want)) in frames.iter().zip(&ffmpeg_frames).enumerate() { |
| 2354 | assert_eq!(got.y, want.y, "frame {i} luma vs ffmpeg (pinned)"); |
| 2355 | assert_eq!(got.u, want.u, "frame {i} U vs ffmpeg (pinned)"); |
| 2356 | assert_eq!(got.v, want.v, "frame {i} V vs ffmpeg (pinned)"); |
| 2357 | } |
| 2358 | } |
| 2359 | |
| 2360 | /// lane-av1refs's decisive single-non-LAST-reference gate: identical |
| 2361 | /// recipe to |
| 2362 | /// [`a_real_aomenc_inter_sequence_with_cdf_forwarding_decodes_pixel_exact`] |
| 2363 | /// except `--enable-order-hint=0` is DROPPED (that flag's own comment on |
| 2364 | /// the sibling test names it as exactly what suppresses aomenc's RD from |
| 2365 | /// ever picking `GOLDEN_FRAME`) and the sequence runs 8 frames, not 4 -- |
| 2366 | /// more chances for the RD search to reach past the first GOP's initial |
| 2367 | /// key frame. `--auto-alt-ref=0` stays: this decoder only supports |
| 2368 | /// `LAST_FRAME`/`GOLDEN_FRAME` so far, and alt-ref/backward references |
| 2369 | /// are still a named refusal, not this gate's target. A stream that |
| 2370 | /// decodes without firing [`decode::non_last_ref_hits`] is not a |
| 2371 | /// coverage win for this round -- SKIP it as a genuine, named miss |
| 2372 | /// rather than pass silently on untested ground. |
| 2373 | #[test] |
| 2374 | fn a_real_aomenc_inter_sequence_with_a_golden_reference_decodes_pixel_exact() { |
| 2375 | if !have_ffmpeg() { |
| 2376 | eprintln!( |
| 2377 | "SKIP a_real_aomenc_inter_sequence_with_a_golden_reference_decodes_pixel_exact: no ffmpeg" |
| 2378 | ); |
| 2379 | return; |
| 2380 | } |
| 2381 | if !have_aomenc() { |
| 2382 | eprintln!( |
| 2383 | "SKIP a_real_aomenc_inter_sequence_with_a_golden_reference_decodes_pixel_exact: no aomenc at {}", |
| 2384 | aomenc_path().display() |
| 2385 | ); |
| 2386 | return; |
| 2387 | } |
| 2388 | let (width, height, frame_count) = (64usize, 64usize, 8usize); |
| 2389 | let mut refusals = Vec::new(); |
| 2390 | let mut never_fired = 0u32; |
| 2391 | // aomenc's own RD search is nondeterministic run-to-run even at |
| 2392 | // `--threads=1` (confirmed live: identical CLI args, identical |
| 2393 | // input, two different output byte streams) -- so beyond varying |
| 2394 | // the fixture's seed, each attempt is its own independent coin |
| 2395 | // flip against every named refusal below. 120 attempts (not 40) |
| 2396 | // to give that coin flip enough tries to land on a stream this |
| 2397 | // decoder both accepts and that fires GOLDEN_FRAME. `duration=0.32` |
| 2398 | // at `rate=25` (not `0.24`) matches every sibling gate's own |
| 2399 | // frame-count fixture exactly -- a shorter duration here once |
| 2400 | // produced a frame-0 (keyframe, no reference at all) luma mismatch |
| 2401 | // against ffmpeg, i.e. a source-frame-count rounding artifact of |
| 2402 | // this recipe's own choosing, not a decoder bug. |
| 2403 | for attempt in 0..120u32 { |
| 2404 | let seed = 42 + attempt % 40; |
| 2405 | let source = gradients_source(seed, 64, 64, "duration=0.32:rate=25"); |
| 2406 | let y4m = Command::new("ffmpeg") |
| 2407 | .args([ |
| 2408 | "-v", |
| 2409 | "error", |
| 2410 | "-f", |
| 2411 | "lavfi", |
| 2412 | "-i", |
| 2413 | &source, |
| 2414 | "-pix_fmt", |
| 2415 | "yuv420p", |
| 2416 | "-f", |
| 2417 | "yuv4mpegpipe", |
| 2418 | "-", |
| 2419 | ]) |
| 2420 | .stdin(Stdio::null()) |
| 2421 | .stdout(Stdio::piped()) |
| 2422 | .stderr(Stdio::piped()) |
| 2423 | .output() |
| 2424 | .expect("ffmpeg failed to run"); |
| 2425 | assert!( |
| 2426 | y4m.status.success(), |
| 2427 | "ffmpeg fixture: {}", |
| 2428 | String::from_utf8_lossy(&y4m.stderr) |
| 2429 | ); |
| 2430 | let mut child = Command::new(aomenc_path()) |
| 2431 | .args([ |
| 2432 | "--codec=av1", |
| 2433 | "--passes=1", |
| 2434 | "--end-usage=q", |
| 2435 | "--cq-level=55", |
| 2436 | "--cpu-used=0", |
| 2437 | "--lag-in-frames=0", |
| 2438 | "--auto-alt-ref=0", |
| 2439 | "--kf-max-dist=1000", |
| 2440 | "--threads=1", |
| 2441 | "--row-mt=0", |
| 2442 | // Narrows the RD search's reference alphabet toward |
| 2443 | // LAST_FRAME/GOLDEN_FRAME -- without this, aomenc's RD is |
| 2444 | // just as likely to pick LAST2/LAST3/BWDREF/ALTREF2/ |
| 2445 | // ALTREF (this decoder's already-documented, still-open |
| 2446 | // refusals), starving this gate of the one reference it |
| 2447 | // targets. |
| 2448 | "--max-reference-frames=3", |
| 2449 | "--reduced-reference-set=1", |
| 2450 | "--enable-warped-motion=0", |
| 2451 | "--enable-obmc=0", |
| 2452 | "--enable-masked-comp=0", |
| 2453 | "--enable-interintra-comp=0", |
| 2454 | "--enable-dist-wtd-comp=0", |
| 2455 | "--enable-diff-wtd-comp=0", |
| 2456 | "--enable-onesided-comp=0", |
| 2457 | "--enable-interintra-wedge=0", |
| 2458 | "--enable-smooth-interintra=0", |
| 2459 | "--enable-rect-partitions=0", |
| 2460 | "--enable-ab-partitions=0", |
| 2461 | "--enable-1to4-partitions=0", |
| 2462 | "--enable-filter-intra=0", |
| 2463 | "--enable-smooth-intra=0", |
| 2464 | "--enable-paeth-intra=0", |
| 2465 | "--enable-directional-intra=0", |
| 2466 | "--enable-angle-delta=0", |
| 2467 | "--enable-tx-size-search=0", |
| 2468 | "--enable-cdef=0", |
| 2469 | "--enable-restoration=0", |
| 2470 | // `min == max == 32` forces PARTITION_NONE for every |
| 2471 | // 32x32 block, not just biases toward it -- with |
| 2472 | // `--min-partition-size=16` alone aomenc's exhaustive |
| 2473 | // `cpu-used=0` RD search still occasionally read HORZ/ |
| 2474 | // VERT/SPLIT partitions below 32x32 despite |
| 2475 | // `--enable-rect-partitions=0`/`--enable-ab-partitions=0`/ |
| 2476 | // `--enable-1to4-partitions=0` all being off (two |
| 2477 | // separate, already-documented round-2 gaps: "a |
| 2478 | // partition below 16x16", "a partition type this encoder |
| 2479 | // never writes"), starving this gate's already-narrow |
| 2480 | // seed pool. Locking both bounds to 32 removes any |
| 2481 | // partition-depth choice from the search entirely, so |
| 2482 | // this gate can isolate GOLDEN_FRAME selection alone. |
| 2483 | "--max-partition-size=32", |
| 2484 | "--min-partition-size=32", |
| 2485 | "--enable-palette=0", |
| 2486 | "--enable-intrabc=0", |
| 2487 | "--enable-cfl-intra=0", |
| 2488 | // use_ref_frame_mvs (temporal MV projection) is unimplemented in mvstack; |
| 2489 | // leaving it on silently desyncs symbols on inter frames (grainfix lesson). |
| 2490 | "--enable-ref-frame-mvs=0", |
| 2491 | // `--tune-content=film` was dropped: on this box it |
| 2492 | // makes aomenc emit `apply_grain` almost every attempt |
| 2493 | // (lane-av1golden8 round 10 measurement, live: 120/120 |
| 2494 | // real attempts hit a named refusal, 0 ever fired |
| 2495 | // GOLDEN_FRAME -- mostly `apply_grain`, dominated by its |
| 2496 | // own recipe flag) -- film grain synthesis has its own |
| 2497 | // dedicated refusal test and must not starve this gate |
| 2498 | // of the GOLDEN_FRAME draws it exists to prove. Default |
| 2499 | // content tuning still occasionally flips |
| 2500 | // `allow_screen_content_tools` on for this synthetic |
| 2501 | // gradient content (a separate, already-documented open |
| 2502 | // refusal), which the 120-attempt coin flip below |
| 2503 | // tolerates the same way. |
| 2504 | "--obu", |
| 2505 | "-o", |
| 2506 | "-", |
| 2507 | "-", |
| 2508 | ]) |
| 2509 | .stdin(Stdio::piped()) |
| 2510 | .stdout(Stdio::piped()) |
| 2511 | .stderr(Stdio::piped()) |
| 2512 | .spawn() |
| 2513 | .expect("aomenc failed to start"); |
| 2514 | child |
| 2515 | .stdin |
| 2516 | .take() |
| 2517 | .expect("aomenc stdin") |
| 2518 | .write_all(&y4m.stdout) |
| 2519 | .expect("writing y4m to aomenc"); |
| 2520 | let out = child.wait_with_output().expect("aomenc failed to run"); |
| 2521 | assert!( |
| 2522 | out.status.success(), |
| 2523 | "aomenc refused the fixture: {}", |
| 2524 | String::from_utf8_lossy(&out.stderr) |
| 2525 | ); |
| 2526 | let stream = out.stdout; |
| 2527 | let before = decode::non_last_ref_hits(); |
| 2528 | let frames = match decode_stream(&stream) { |
| 2529 | Err(e) => { |
| 2530 | let msg = e.to_string(); |
| 2531 | assert!( |
| 2532 | msg.contains("unsupported"), |
| 2533 | "a golden-reference stream failed outright (seed {seed}): {msg}" |
| 2534 | ); |
| 2535 | refusals.push(format!("seed {seed}: {msg}")); |
| 2536 | continue; |
| 2537 | } |
| 2538 | Ok(frames) => frames, |
| 2539 | }; |
| 2540 | if decode::non_last_ref_hits() == before { |
| 2541 | // This seed's RD search never actually picked GOLDEN_FRAME -- |
| 2542 | // a genuine miss, not a refusal. Try the next seed rather |
| 2543 | // than claiming coverage this attempt did not exercise. |
| 2544 | never_fired += 1; |
| 2545 | continue; |
| 2546 | } |
| 2547 | let ffmpeg_frames = ffmpeg_decode_sequence(&stream, width, height, frame_count); |
| 2548 | assert_eq!(frames.len(), frame_count); |
| 2549 | // Same pin-on-mismatch pattern as the CDF forwarding gate above: |
| 2550 | // env-gated, no cost on a normal run. |
| 2551 | let mismatched = frames |
| 2552 | .iter() |
| 2553 | .zip(&ffmpeg_frames) |
| 2554 | .any(|(got, want)| got.y != want.y || got.u != want.u || got.v != want.v); |
| 2555 | if mismatched { |
| 2556 | if let Ok(path) = std::env::var("EC_AV1_GATE_DUMP") { |
| 2557 | std::fs::write(&path, &stream).expect("writing pinned stream"); |
| 2558 | eprintln!("EC_AV1_GATE_DUMP: wrote mismatching stream (seed {seed}) to {path}"); |
| 2559 | } |
| 2560 | } |
| 2561 | for (i, (got, want)) in frames.iter().zip(&ffmpeg_frames).enumerate() { |
| 2562 | assert_eq!(got.y, want.y, "frame {i} luma vs ffmpeg (seed {seed})"); |
| 2563 | assert_eq!(got.u, want.u, "frame {i} U vs ffmpeg (seed {seed})"); |
| 2564 | assert_eq!(got.v, want.v, "frame {i} V vs ffmpeg (seed {seed})"); |
| 2565 | } |
| 2566 | eprintln!( |
| 2567 | "FIRING seed {seed}: non_last_ref_hits advanced by {}", |
| 2568 | decode::non_last_ref_hits() - before |
| 2569 | ); |
| 2570 | return; |
| 2571 | } |
| 2572 | eprintln!( |
| 2573 | "SKIP a_real_aomenc_inter_sequence_with_a_golden_reference_decodes_pixel_exact: \ |
| 2574 | {never_fired} attempts decoded but never fired GOLDEN_FRAME, and every other \ |
| 2575 | attempt hit a named refusal:\n{}", |
| 2576 | refusals.join("\n") |
| 2577 | ); |
| 2578 | } |
| 2579 | |
| 2580 | /// lane-av1refs's decisive gate for the 5 references widened past |
| 2581 | /// `LAST_FRAME`/`GOLDEN_FRAME`: same envelope and pin-on-mismatch |
| 2582 | /// pattern as |
| 2583 | /// [`a_real_aomenc_inter_sequence_with_a_golden_reference_decodes_pixel_exact`], |
| 2584 | /// generalised over `(target_ref, extra_args, frame_count)` so each of |
| 2585 | /// `LAST2`/`LAST3`/`BWDREF`/`ALTREF2`/`ALTREF` gets its own attempt pool |
| 2586 | /// and its own proof that `decode::ref_hits(target_ref)` -- not just |
| 2587 | /// `non_last_ref_hits`, which cannot distinguish one reference from |
| 2588 | /// another -- actually advanced. `--max-reference-frames=3 |
| 2589 | /// --reduced-reference-set=1` (the golden gate's own narrowing) is |
| 2590 | /// DROPPED here: both suppress exactly the references this gate needs |
| 2591 | /// aomenc's RD to reach. |
| 2592 | fn a_real_aomenc_single_ref_gate( |
| 2593 | gate_name: &str, |
| 2594 | target_ref: i8, |
| 2595 | target_ref_name: &str, |
| 2596 | extra_args: &[&str], |
| 2597 | width: usize, |
| 2598 | height: usize, |
| 2599 | frame_count: usize, |
| 2600 | attempts: u32, |
| 2601 | ) { |
| 2602 | let _gate_lock = lock_gate_counters(); |
| 2603 | if !have_ffmpeg() { |
| 2604 | eprintln!("SKIP {gate_name}: no ffmpeg"); |
| 2605 | return; |
| 2606 | } |
| 2607 | if !have_aomenc() { |
| 2608 | eprintln!("SKIP {gate_name}: no aomenc at {}", aomenc_path().display()); |
| 2609 | return; |
| 2610 | } |
| 2611 | let mut refusals = Vec::new(); |
| 2612 | let mut never_fired = 0u32; |
| 2613 | for attempt in 0..attempts { |
| 2614 | let seed = 42 + attempt % 40; |
| 2615 | let duration = frame_count as f64 / 25.0; |
| 2616 | let source = |
| 2617 | gradients_source(seed, width, height, &format!("duration={duration}:rate=25")); |
| 2618 | let y4m = Command::new("ffmpeg") |
| 2619 | .args([ |
| 2620 | "-v", |
| 2621 | "error", |
| 2622 | "-f", |
| 2623 | "lavfi", |
| 2624 | "-i", |
| 2625 | &source, |
| 2626 | "-pix_fmt", |
| 2627 | "yuv420p", |
| 2628 | "-f", |
| 2629 | "yuv4mpegpipe", |
| 2630 | "-", |
| 2631 | ]) |
| 2632 | .stdin(Stdio::null()) |
| 2633 | .stdout(Stdio::piped()) |
| 2634 | .stderr(Stdio::piped()) |
| 2635 | .output() |
| 2636 | .expect("ffmpeg failed to run"); |
| 2637 | assert!( |
| 2638 | y4m.status.success(), |
| 2639 | "ffmpeg fixture: {}", |
| 2640 | String::from_utf8_lossy(&y4m.stderr) |
| 2641 | ); |
| 2642 | let mut args: Vec<&str> = vec![ |
| 2643 | "--codec=av1", |
| 2644 | "--passes=1", |
| 2645 | "--end-usage=q", |
| 2646 | "--cq-level=55", |
| 2647 | "--cpu-used=0", |
| 2648 | "--kf-max-dist=1000", |
| 2649 | "--threads=1", |
| 2650 | "--row-mt=0", |
| 2651 | "--enable-warped-motion=0", |
| 2652 | "--enable-obmc=0", |
| 2653 | "--enable-masked-comp=0", |
| 2654 | "--enable-interintra-comp=0", |
| 2655 | "--enable-dist-wtd-comp=0", |
| 2656 | "--enable-diff-wtd-comp=0", |
| 2657 | "--enable-onesided-comp=0", |
| 2658 | "--enable-interintra-wedge=0", |
| 2659 | "--enable-smooth-interintra=0", |
| 2660 | "--enable-rect-partitions=0", |
| 2661 | "--enable-ab-partitions=0", |
| 2662 | "--enable-1to4-partitions=0", |
| 2663 | "--enable-filter-intra=0", |
| 2664 | "--enable-smooth-intra=0", |
| 2665 | "--enable-paeth-intra=0", |
| 2666 | "--enable-directional-intra=0", |
| 2667 | "--enable-angle-delta=0", |
| 2668 | "--enable-tx-size-search=0", |
| 2669 | "--enable-cdef=0", |
| 2670 | "--enable-restoration=0", |
| 2671 | "--max-partition-size=32", |
| 2672 | "--min-partition-size=32", |
| 2673 | "--enable-palette=0", |
| 2674 | "--enable-intrabc=0", |
| 2675 | "--enable-cfl-intra=0", |
| 2676 | // use_ref_frame_mvs (temporal MV projection) is unimplemented in |
| 2677 | // mvstack; leaving it on silently desyncs symbols on inter frames. |
| 2678 | "--enable-ref-frame-mvs=0", |
| 2679 | "--obu", |
| 2680 | "-o", |
| 2681 | "-", |
| 2682 | "-", |
| 2683 | ]; |
| 2684 | // extra_args goes right before the trailing "-o", "-", "-" triple. |
| 2685 | let tail = args.split_off(args.len() - 3); |
| 2686 | args.extend_from_slice(extra_args); |
| 2687 | args.extend_from_slice(&tail); |
| 2688 | let mut child = Command::new(aomenc_path()) |
| 2689 | .args(&args) |
| 2690 | .stdin(Stdio::piped()) |
| 2691 | .stdout(Stdio::piped()) |
| 2692 | .stderr(Stdio::piped()) |
| 2693 | .spawn() |
| 2694 | .expect("aomenc failed to start"); |
| 2695 | child |
| 2696 | .stdin |
| 2697 | .take() |
| 2698 | .expect("aomenc stdin") |
| 2699 | .write_all(&y4m.stdout) |
| 2700 | .expect("writing y4m to aomenc"); |
| 2701 | let out = child.wait_with_output().expect("aomenc failed to run"); |
| 2702 | assert!( |
| 2703 | out.status.success(), |
| 2704 | "aomenc refused the fixture: {}", |
| 2705 | String::from_utf8_lossy(&out.stderr) |
| 2706 | ); |
| 2707 | let stream = out.stdout; |
| 2708 | let before = decode::ref_hits(target_ref); |
| 2709 | let frames = match decode_stream(&stream) { |
| 2710 | Err(e) => { |
| 2711 | let msg = e.to_string(); |
| 2712 | assert!( |
| 2713 | msg.contains("unsupported"), |
| 2714 | "{gate_name} failed outright (seed {seed}): {msg}" |
| 2715 | ); |
| 2716 | refusals.push(format!("seed {seed}: {msg}")); |
| 2717 | continue; |
| 2718 | } |
| 2719 | Ok(frames) => frames, |
| 2720 | }; |
| 2721 | if decode::ref_hits(target_ref) == before { |
| 2722 | never_fired += 1; |
| 2723 | continue; |
| 2724 | } |
| 2725 | let ffmpeg_frames = ffmpeg_decode_sequence(&stream, width, height, frame_count); |
| 2726 | assert_eq!(frames.len(), frame_count); |
| 2727 | let mismatched = frames |
| 2728 | .iter() |
| 2729 | .zip(&ffmpeg_frames) |
| 2730 | .any(|(got, want)| got.y != want.y || got.u != want.u || got.v != want.v); |
| 2731 | if mismatched { |
| 2732 | if let Ok(path) = std::env::var("EC_AV1_GATE_DUMP") { |
| 2733 | std::fs::write(&path, &stream).expect("writing pinned stream"); |
| 2734 | eprintln!("EC_AV1_GATE_DUMP: wrote mismatching stream (seed {seed}) to {path}"); |
| 2735 | } |
| 2736 | } |
| 2737 | for (i, (got, want)) in frames.iter().zip(&ffmpeg_frames).enumerate() { |
| 2738 | assert_eq!( |
| 2739 | got.y, want.y, |
| 2740 | "{gate_name} frame {i} luma vs ffmpeg (seed {seed})" |
| 2741 | ); |
| 2742 | assert_eq!( |
| 2743 | got.u, want.u, |
| 2744 | "{gate_name} frame {i} U vs ffmpeg (seed {seed})" |
| 2745 | ); |
| 2746 | assert_eq!( |
| 2747 | got.v, want.v, |
| 2748 | "{gate_name} frame {i} V vs ffmpeg (seed {seed})" |
| 2749 | ); |
| 2750 | } |
| 2751 | eprintln!( |
| 2752 | "{gate_name} FIRING seed {seed}: {target_ref_name} hits advanced by {}", |
| 2753 | decode::ref_hits(target_ref) - before |
| 2754 | ); |
| 2755 | return; |
| 2756 | } |
| 2757 | eprintln!( |
| 2758 | "SKIP {gate_name}: {never_fired} attempts decoded but never fired {target_ref_name}, \ |
| 2759 | and every other attempt hit a named refusal:\n{}", |
| 2760 | refusals.join("\n") |
| 2761 | ); |
| 2762 | } |
| 2763 | |
| 2764 | /// lane-av1comp: proves `reference_select`/`comp_mode` are actually read |
| 2765 | /// off a real bitstream rather than only unit-tested in isolation -- |
| 2766 | /// `--auto-alt-ref=1 --lag-in-frames=16` gives aomenc a backward |
| 2767 | /// reference to pick `COMPOUND_REFERENCE` with, `--enable-order-hint=1` |
| 2768 | /// is required for it to ever consider compound at all. Three |
| 2769 | /// acceptable outcomes: (a) a stream where `reference_select` never |
| 2770 | /// actually drove a block to `COMPOUND_REFERENCE` decodes bit-exact end |
| 2771 | /// to end (proving the per-block `comp_mode` read did not desync the |
| 2772 | /// single-ref blocks around it); (b) a stream that does fire compound |
| 2773 | /// is refused by name with [`decode::comp_mode_hits`] having advanced |
| 2774 | /// (proving `comp_mode`/`read_compound_ref_frames` consumed the right |
| 2775 | /// symbols before refusing, rather than desyncing and refusing for an |
| 2776 | /// unrelated reason) -- masked/wedge compound (`comp_group_idx == 1`) |
| 2777 | /// and a partition below 16x16 (a real, separately tracked capability |
| 2778 | /// gap) are the two refusal arms this can still legitimately name; (c) |
| 2779 | /// r13 wired plain `COMPOUND_AVERAGE` MC for real, so as of r15 a |
| 2780 | /// stream that fires compound may also decode fully -- accepted only |
| 2781 | /// when every output frame is pixel-exact against ffmpeg. |
| 2782 | #[test] |
| 2783 | fn a_real_aomenc_stream_with_reference_select_reads_comp_mode_correctly() { |
| 2784 | const NAME: &str = "a_real_aomenc_stream_with_reference_select_reads_comp_mode_correctly"; |
| 2785 | let _gate_lock = lock_gate_counters(); |
| 2786 | if !have_ffmpeg() { |
| 2787 | eprintln!("SKIP {NAME}: no ffmpeg"); |
| 2788 | return; |
| 2789 | } |
| 2790 | if !have_aomenc() { |
| 2791 | eprintln!("SKIP {NAME}: no aomenc at {}", aomenc_path().display()); |
| 2792 | return; |
| 2793 | } |
| 2794 | let (width, height, frame_count) = (64usize, 64usize, 20usize); |
| 2795 | let mut refusals = Vec::new(); |
| 2796 | let mut never_fired = 0u32; |
| 2797 | for attempt in 0..60u32 { |
| 2798 | let seed = 42 + attempt % 40; |
| 2799 | let duration = frame_count as f64 / 25.0; |
| 2800 | let source = |
| 2801 | gradients_source(seed, width, height, &format!("duration={duration}:rate=25")); |
| 2802 | let y4m = Command::new("ffmpeg") |
| 2803 | .args([ |
| 2804 | "-v", |
| 2805 | "error", |
| 2806 | "-f", |
| 2807 | "lavfi", |
| 2808 | "-i", |
| 2809 | &source, |
| 2810 | "-pix_fmt", |
| 2811 | "yuv420p", |
| 2812 | "-f", |
| 2813 | "yuv4mpegpipe", |
| 2814 | "-", |
| 2815 | ]) |
| 2816 | .stdin(Stdio::null()) |
| 2817 | .stdout(Stdio::piped()) |
| 2818 | .stderr(Stdio::piped()) |
| 2819 | .output() |
| 2820 | .expect("ffmpeg failed to run"); |
| 2821 | assert!( |
| 2822 | y4m.status.success(), |
| 2823 | "ffmpeg fixture: {}", |
| 2824 | String::from_utf8_lossy(&y4m.stderr) |
| 2825 | ); |
| 2826 | let args: Vec<&str> = vec![ |
| 2827 | "--codec=av1", |
| 2828 | "--passes=1", |
| 2829 | "--end-usage=q", |
| 2830 | "--cq-level=45", |
| 2831 | "--cpu-used=0", |
| 2832 | "--kf-max-dist=1000", |
| 2833 | "--threads=1", |
| 2834 | "--row-mt=0", |
| 2835 | "--auto-alt-ref=1", |
| 2836 | "--lag-in-frames=16", |
| 2837 | "--enable-order-hint=1", |
| 2838 | "--enable-warped-motion=0", |
| 2839 | "--enable-obmc=0", |
| 2840 | "--enable-masked-comp=0", |
| 2841 | "--enable-interintra-comp=0", |
| 2842 | "--enable-onesided-comp=0", |
| 2843 | "--enable-interintra-wedge=0", |
| 2844 | "--enable-smooth-interintra=0", |
| 2845 | "--enable-rect-partitions=0", |
| 2846 | "--enable-ab-partitions=0", |
| 2847 | "--enable-1to4-partitions=0", |
| 2848 | "--enable-filter-intra=0", |
| 2849 | "--enable-smooth-intra=0", |
| 2850 | "--enable-paeth-intra=0", |
| 2851 | "--enable-directional-intra=0", |
| 2852 | "--enable-angle-delta=0", |
| 2853 | "--enable-tx-size-search=0", |
| 2854 | "--enable-cdef=0", |
| 2855 | "--enable-restoration=0", |
| 2856 | "--max-partition-size=32", |
| 2857 | "--min-partition-size=32", |
| 2858 | "--enable-palette=0", |
| 2859 | "--enable-intrabc=0", |
| 2860 | "--enable-cfl-intra=0", |
| 2861 | "--enable-ref-frame-mvs=0", |
| 2862 | "--obu", |
| 2863 | "-o", |
| 2864 | "-", |
| 2865 | "-", |
| 2866 | ]; |
| 2867 | let mut child = Command::new(aomenc_path()) |
| 2868 | .args(&args) |
| 2869 | .stdin(Stdio::piped()) |
| 2870 | .stdout(Stdio::piped()) |
| 2871 | .stderr(Stdio::piped()) |
| 2872 | .spawn() |
| 2873 | .expect("aomenc failed to start"); |
| 2874 | child |
| 2875 | .stdin |
| 2876 | .take() |
| 2877 | .expect("aomenc stdin") |
| 2878 | .write_all(&y4m.stdout) |
| 2879 | .expect("writing y4m to aomenc"); |
| 2880 | let out = child.wait_with_output().expect("aomenc failed to run"); |
| 2881 | assert!( |
| 2882 | out.status.success(), |
| 2883 | "aomenc refused the fixture: {}", |
| 2884 | String::from_utf8_lossy(&out.stderr) |
| 2885 | ); |
| 2886 | let stream = out.stdout; |
| 2887 | let before = decode::comp_mode_hits(); |
| 2888 | match decode_stream(&stream) { |
| 2889 | Err(e) => { |
| 2890 | let msg = e.to_string(); |
| 2891 | let fired = decode::comp_mode_hits() > before; |
| 2892 | assert!( |
| 2893 | msg.contains("unsupported"), |
| 2894 | "{NAME} failed outright (seed {seed}): {msg}" |
| 2895 | ); |
| 2896 | if fired { |
| 2897 | // `comp_mode` firing does not stop the block walk -- |
| 2898 | // a later, unrelated block in the same tile can |
| 2899 | // still hit any other named refusal this decoder |
| 2900 | // carries. A partition below 16x16 is a real, open |
| 2901 | // capability gap tracked on its own (not a |
| 2902 | // COMPOUND_REFERENCE defect); accept it here too |
| 2903 | // rather than fail a compound-specific gate on a |
| 2904 | // partition-search coincidence. lane-av1blend r7: |
| 2905 | // re-swept the wide gate with the plain-average |
| 2906 | // blend unmasked (r6/r7's ref_ctx fix alone) and it |
| 2907 | // still mismatches on real streams beyond that one |
| 2908 | // bug -- re-masked comp_group_idx == 0 too, so this |
| 2909 | // stays a legal named refusal. |
| 2910 | assert!( |
| 2911 | msg.contains("COMPOUND_REFERENCE") || msg.contains("a partition"), |
| 2912 | "{NAME}: comp_mode fired (seed {seed}) but the refusal \ |
| 2913 | was for something else: {msg}" |
| 2914 | ); |
| 2915 | let advanced = decode::comp_mode_hits() - before; |
| 2916 | eprintln!( |
| 2917 | "{NAME} FIRING seed {seed}: comp_mode hits advanced by \ |
| 2918 | {advanced}, refused by name as expected: {msg}" |
| 2919 | ); |
| 2920 | return; |
| 2921 | } |
| 2922 | refusals.push(format!("seed {seed}: {msg}")); |
| 2923 | continue; |
| 2924 | } |
| 2925 | Ok(frames) => { |
| 2926 | if decode::comp_mode_hits() == before { |
| 2927 | never_fired += 1; |
| 2928 | continue; |
| 2929 | } |
| 2930 | // Outcome (c): plain COMPOUND_AVERAGE decoded fully -- |
| 2931 | // non-negotiable pixel exactness, no tolerance. |
| 2932 | let ffmpeg_frames = ffmpeg_decode_sequence(&stream, width, height, frame_count); |
| 2933 | assert_eq!(frames.len(), frame_count); |
| 2934 | let mismatched = frames |
| 2935 | .iter() |
| 2936 | .zip(&ffmpeg_frames) |
| 2937 | .any(|(got, want)| got.y != want.y || got.u != want.u || got.v != want.v); |
| 2938 | if mismatched { |
| 2939 | let pin = std::env::temp_dir().join("ec-av1-reference-select-gate-fail.obu"); |
| 2940 | let _ = std::fs::write(&pin, &stream); |
| 2941 | if let Ok(path) = std::env::var("EC_AV1_GATE_DUMP") { |
| 2942 | std::fs::write(&path, &stream).expect("writing pinned stream"); |
| 2943 | eprintln!("EC_AV1_GATE_DUMP: wrote mismatching stream (seed {seed}) to {path}"); |
| 2944 | } |
| 2945 | for (i, (got, want)) in frames.iter().zip(&ffmpeg_frames).enumerate() { |
| 2946 | assert_eq!( |
| 2947 | got.y, want.y, |
| 2948 | "{NAME} frame {i} luma vs ffmpeg (seed {seed}) -- stream pinned at {}", |
| 2949 | pin.display() |
| 2950 | ); |
| 2951 | assert_eq!( |
| 2952 | got.u, want.u, |
| 2953 | "{NAME} frame {i} U vs ffmpeg (seed {seed}) -- stream pinned at {}", |
| 2954 | pin.display() |
| 2955 | ); |
| 2956 | assert_eq!( |
| 2957 | got.v, want.v, |
| 2958 | "{NAME} frame {i} V vs ffmpeg (seed {seed}) -- stream pinned at {}", |
| 2959 | pin.display() |
| 2960 | ); |
| 2961 | } |
| 2962 | } else { |
| 2963 | for (i, (got, want)) in frames.iter().zip(&ffmpeg_frames).enumerate() { |
| 2964 | assert_eq!(got.y, want.y, "{NAME} frame {i} luma vs ffmpeg (seed {seed})"); |
| 2965 | assert_eq!(got.u, want.u, "{NAME} frame {i} U vs ffmpeg (seed {seed})"); |
| 2966 | assert_eq!(got.v, want.v, "{NAME} frame {i} V vs ffmpeg (seed {seed})"); |
| 2967 | } |
| 2968 | } |
| 2969 | eprintln!( |
| 2970 | "{NAME} FIRING seed {seed}: comp_mode hits advanced by {}, decoded \ |
| 2971 | fully and pixel-exact", |
| 2972 | decode::comp_mode_hits() - before |
| 2973 | ); |
| 2974 | return; |
| 2975 | } |
| 2976 | } |
| 2977 | } |
| 2978 | eprintln!( |
| 2979 | "SKIP {NAME}: {never_fired} attempts decoded fully (reference_select never drove a \ |
| 2980 | block to COMPOUND_REFERENCE) and every other attempt hit a named refusal that never \ |
| 2981 | reached comp_mode:\n{}", |
| 2982 | refusals.join("\n") |
| 2983 | ); |
| 2984 | } |
| 2985 | |
| 2986 | /// lane-av1comp round 14's decisive gate for `skip_mode`: same |
| 2987 | /// pin-on-mismatch pattern as [`a_real_aomenc_single_ref_gate`], proving |
| 2988 | /// [`decode::skip_mode_hits`] actually advanced (a real block picked |
| 2989 | /// `skip_mode`, not just that the frame header bit was set) and that the |
| 2990 | /// resulting forced-compound `NEAREST_NEARESTMV` decode is still |
| 2991 | /// pixel-exact against ffmpeg. `--auto-alt-ref=1 --lag-in-frames=16 |
| 2992 | /// --enable-fwd-kf=0` (backward references) plus |
| 2993 | /// `--enable-ref-frame-mvs=0` (unimplemented temporal MV projection, |
| 2994 | /// same as every other gate here) is aomenc's own requirement for |
| 2995 | /// `skip_mode_present` to ever be considered at all (spec 5.9.22 needs |
| 2996 | /// both a forward and a backward reference candidate). |
| 2997 | #[test] |
| 2998 | fn a_real_aomenc_stream_with_compound_references_decodes_pixel_exact() { |
| 2999 | const NAME: &str = "a_real_aomenc_stream_with_compound_references_decodes_pixel_exact"; |
| 3000 | let _gate_lock = lock_gate_counters(); |
| 3001 | if !have_ffmpeg() { |
| 3002 | eprintln!("SKIP {NAME}: no ffmpeg"); |
| 3003 | return; |
| 3004 | } |
| 3005 | if !have_aomenc() { |
| 3006 | eprintln!("SKIP {NAME}: no aomenc at {}", aomenc_path().display()); |
| 3007 | return; |
| 3008 | } |
| 3009 | let (width, height, frame_count) = (64usize, 64usize, 24usize); |
| 3010 | let mut refusals = Vec::new(); |
| 3011 | let mut never_fired = 0u32; |
| 3012 | for attempt in 0..80u32 { |
| 3013 | let seed = 42 + attempt % 40; |
| 3014 | let duration = frame_count as f64 / 25.0; |
| 3015 | let source = |
| 3016 | gradients_source(seed, width, height, &format!("duration={duration}:rate=25")); |
| 3017 | let y4m = Command::new("ffmpeg") |
| 3018 | .args([ |
| 3019 | "-v", |
| 3020 | "error", |
| 3021 | "-f", |
| 3022 | "lavfi", |
| 3023 | "-i", |
| 3024 | &source, |
| 3025 | "-pix_fmt", |
| 3026 | "yuv420p", |
| 3027 | "-f", |
| 3028 | "yuv4mpegpipe", |
| 3029 | "-", |
| 3030 | ]) |
| 3031 | .stdin(Stdio::null()) |
| 3032 | .stdout(Stdio::piped()) |
| 3033 | .stderr(Stdio::piped()) |
| 3034 | .output() |
| 3035 | .expect("ffmpeg failed to run"); |
| 3036 | assert!( |
| 3037 | y4m.status.success(), |
| 3038 | "ffmpeg fixture: {}", |
| 3039 | String::from_utf8_lossy(&y4m.stderr) |
| 3040 | ); |
| 3041 | let args: Vec<&str> = vec![ |
| 3042 | "--codec=av1", |
| 3043 | "--passes=1", |
| 3044 | "--end-usage=q", |
| 3045 | "--cq-level=45", |
| 3046 | "--cpu-used=0", |
| 3047 | "--kf-max-dist=1000", |
| 3048 | "--threads=1", |
| 3049 | "--row-mt=0", |
| 3050 | "--auto-alt-ref=1", |
| 3051 | "--lag-in-frames=16", |
| 3052 | "--enable-fwd-kf=0", |
| 3053 | "--enable-order-hint=1", |
| 3054 | "--enable-warped-motion=0", |
| 3055 | "--enable-obmc=0", |
| 3056 | "--enable-masked-comp=0", |
| 3057 | "--enable-interintra-comp=0", |
| 3058 | "--enable-onesided-comp=0", |
| 3059 | "--enable-interintra-wedge=0", |
| 3060 | "--enable-smooth-interintra=0", |
| 3061 | "--enable-rect-partitions=0", |
| 3062 | "--enable-ab-partitions=0", |
| 3063 | "--enable-1to4-partitions=0", |
| 3064 | "--enable-filter-intra=0", |
| 3065 | "--enable-smooth-intra=0", |
| 3066 | "--enable-paeth-intra=0", |
| 3067 | "--enable-directional-intra=0", |
| 3068 | "--enable-angle-delta=0", |
| 3069 | "--enable-tx-size-search=0", |
| 3070 | "--enable-cdef=0", |
| 3071 | "--enable-restoration=0", |
| 3072 | "--max-partition-size=32", |
| 3073 | "--min-partition-size=32", |
| 3074 | "--enable-palette=0", |
| 3075 | "--enable-intrabc=0", |
| 3076 | "--enable-cfl-intra=0", |
| 3077 | "--enable-ref-frame-mvs=0", |
| 3078 | "--obu", |
| 3079 | "-o", |
| 3080 | "-", |
| 3081 | "-", |
| 3082 | ]; |
| 3083 | let mut child = Command::new(aomenc_path()) |
| 3084 | .args(&args) |
| 3085 | .stdin(Stdio::piped()) |
| 3086 | .stdout(Stdio::piped()) |
| 3087 | .stderr(Stdio::piped()) |
| 3088 | .spawn() |
| 3089 | .expect("aomenc failed to start"); |
| 3090 | child |
| 3091 | .stdin |
| 3092 | .take() |
| 3093 | .expect("aomenc stdin") |
| 3094 | .write_all(&y4m.stdout) |
| 3095 | .expect("writing y4m to aomenc"); |
| 3096 | let out = child.wait_with_output().expect("aomenc failed to run"); |
| 3097 | assert!( |
| 3098 | out.status.success(), |
| 3099 | "aomenc refused the fixture: {}", |
| 3100 | String::from_utf8_lossy(&out.stderr) |
| 3101 | ); |
| 3102 | let stream = out.stdout; |
| 3103 | let before = decode::skip_mode_hits(); |
| 3104 | let frames = match decode_stream(&stream) { |
| 3105 | Err(e) => { |
| 3106 | let msg = e.to_string(); |
| 3107 | assert!( |
| 3108 | msg.contains("unsupported"), |
| 3109 | "{NAME} failed outright (seed {seed}): {msg}" |
| 3110 | ); |
| 3111 | refusals.push(format!("seed {seed}: {msg}")); |
| 3112 | continue; |
| 3113 | } |
| 3114 | Ok(frames) => frames, |
| 3115 | }; |
| 3116 | if decode::skip_mode_hits() == before { |
| 3117 | never_fired += 1; |
| 3118 | continue; |
| 3119 | } |
| 3120 | let ffmpeg_frames = ffmpeg_decode_sequence(&stream, width, height, frame_count); |
| 3121 | assert_eq!(frames.len(), frame_count); |
| 3122 | let mismatched = frames |
| 3123 | .iter() |
| 3124 | .zip(&ffmpeg_frames) |
| 3125 | .any(|(got, want)| got.y != want.y || got.u != want.u || got.v != want.v); |
| 3126 | if mismatched { |
| 3127 | let pin = std::env::temp_dir().join("ec-av1-compound-refs-gate-fail.obu"); |
| 3128 | let _ = std::fs::write(&pin, &stream); |
| 3129 | if let Ok(path) = std::env::var("EC_AV1_GATE_DUMP") { |
| 3130 | std::fs::write(&path, &stream).expect("writing pinned stream"); |
| 3131 | eprintln!("EC_AV1_GATE_DUMP: wrote mismatching stream (seed {seed}) to {path}"); |
| 3132 | } |
| 3133 | for (i, (got, want)) in frames.iter().zip(&ffmpeg_frames).enumerate() { |
| 3134 | assert_eq!( |
| 3135 | got.y, want.y, |
| 3136 | "{NAME} frame {i} luma vs ffmpeg (seed {seed}) -- stream pinned at {}", |
| 3137 | pin.display() |
| 3138 | ); |
| 3139 | assert_eq!( |
| 3140 | got.u, want.u, |
| 3141 | "{NAME} frame {i} U vs ffmpeg (seed {seed}) -- stream pinned at {}", |
| 3142 | pin.display() |
| 3143 | ); |
| 3144 | assert_eq!( |
| 3145 | got.v, want.v, |
| 3146 | "{NAME} frame {i} V vs ffmpeg (seed {seed}) -- stream pinned at {}", |
| 3147 | pin.display() |
| 3148 | ); |
| 3149 | } |
| 3150 | } |
| 3151 | eprintln!( |
| 3152 | "{NAME} FIRING seed {seed}: skip_mode hits advanced by {}", |
| 3153 | decode::skip_mode_hits() - before |
| 3154 | ); |
| 3155 | return; |
| 3156 | } |
| 3157 | eprintln!( |
| 3158 | "SKIP {NAME}: {never_fired} attempts decoded but never fired skip_mode, and every \ |
| 3159 | other attempt hit a named refusal:\n{}", |
| 3160 | refusals.join("\n") |
| 3161 | ); |
| 3162 | } |
| 3163 | |
| 3164 | /// lane-motionmode round 1: `--enable-obmc=1 --enable-warped-motion=0` |
| 3165 | /// on the base recipe (`--auto-alt-ref=1 --lag-in-frames=16 |
| 3166 | /// --enable-fwd-kf=0 --enable-ref-frame-mvs=0`) -- pixel-exact against |
| 3167 | /// ffmpeg, gated on [`decode::obmc_hits`] actually advancing (a real |
| 3168 | /// block picked `OBMC_CAUSAL`, not just that the header bit was set). |
| 3169 | /// Structure copied from |
| 3170 | /// `a_real_aomenc_stream_with_compound_references_decodes_pixel_exact`: |
| 3171 | /// a seed sweep with a named-refusal escape hatch and an |
| 3172 | /// `EC_AV1_GATE_DUMP` pin on mismatch. |
| 3173 | #[test] |
| 3174 | fn a_real_aomenc_stream_with_obmc_decodes_pixel_exact() { |
| 3175 | const NAME: &str = "a_real_aomenc_stream_with_obmc_decodes_pixel_exact"; |
| 3176 | let _gate_lock = lock_gate_counters(); |
| 3177 | if !have_ffmpeg() { |
| 3178 | eprintln!("SKIP {NAME}: no ffmpeg"); |
| 3179 | return; |
| 3180 | } |
| 3181 | if !have_aomenc() { |
| 3182 | eprintln!("SKIP {NAME}: no aomenc at {}", aomenc_path().display()); |
| 3183 | return; |
| 3184 | } |
| 3185 | let (width, height, frame_count) = (64usize, 64usize, 24usize); |
| 3186 | let mut refusals = Vec::new(); |
| 3187 | let mut never_fired = 0u32; |
| 3188 | for attempt in 0..80u32 { |
| 3189 | let seed = 42 + attempt % 40; |
| 3190 | let duration = frame_count as f64 / 25.0; |
| 3191 | let source = |
| 3192 | gradients_source(seed, width, height, &format!("duration={duration}:rate=25")); |
| 3193 | let y4m = Command::new("ffmpeg") |
| 3194 | .args([ |
| 3195 | "-v", |
| 3196 | "error", |
| 3197 | "-f", |
| 3198 | "lavfi", |
| 3199 | "-i", |
| 3200 | &source, |
| 3201 | "-pix_fmt", |
| 3202 | "yuv420p", |
| 3203 | "-f", |
| 3204 | "yuv4mpegpipe", |
| 3205 | "-", |
| 3206 | ]) |
| 3207 | .stdin(Stdio::null()) |
| 3208 | .stdout(Stdio::piped()) |
| 3209 | .stderr(Stdio::piped()) |
| 3210 | .output() |
| 3211 | .expect("ffmpeg failed to run"); |
| 3212 | assert!( |
| 3213 | y4m.status.success(), |
| 3214 | "ffmpeg fixture: {}", |
| 3215 | String::from_utf8_lossy(&y4m.stderr) |
| 3216 | ); |
| 3217 | let args: Vec<&str> = vec![ |
| 3218 | "--codec=av1", |
| 3219 | "--passes=1", |
| 3220 | "--end-usage=q", |
| 3221 | "--cq-level=45", |
| 3222 | "--cpu-used=0", |
| 3223 | "--kf-max-dist=1000", |
| 3224 | "--threads=1", |
| 3225 | "--row-mt=0", |
| 3226 | "--auto-alt-ref=1", |
| 3227 | "--lag-in-frames=16", |
| 3228 | "--enable-fwd-kf=0", |
| 3229 | "--enable-order-hint=1", |
| 3230 | "--enable-warped-motion=0", |
| 3231 | "--enable-obmc=1", |
| 3232 | "--enable-masked-comp=0", |
| 3233 | "--enable-interintra-comp=0", |
| 3234 | "--enable-onesided-comp=0", |
| 3235 | "--enable-interintra-wedge=0", |
| 3236 | "--enable-smooth-interintra=0", |
| 3237 | "--enable-rect-partitions=0", |
| 3238 | "--enable-ab-partitions=0", |
| 3239 | "--enable-1to4-partitions=0", |
| 3240 | "--enable-filter-intra=0", |
| 3241 | "--enable-smooth-intra=0", |
| 3242 | "--enable-paeth-intra=0", |
| 3243 | "--enable-directional-intra=0", |
| 3244 | "--enable-angle-delta=0", |
| 3245 | "--enable-tx-size-search=0", |
| 3246 | "--enable-cdef=0", |
| 3247 | "--enable-restoration=0", |
| 3248 | "--max-partition-size=32", |
| 3249 | "--min-partition-size=32", |
| 3250 | "--enable-palette=0", |
| 3251 | "--enable-intrabc=0", |
| 3252 | "--enable-cfl-intra=0", |
| 3253 | "--enable-ref-frame-mvs=0", |
| 3254 | "--obu", |
| 3255 | "-o", |
| 3256 | "-", |
| 3257 | "-", |
| 3258 | ]; |
| 3259 | let mut child = Command::new(aomenc_path()) |
| 3260 | .args(&args) |
| 3261 | .stdin(Stdio::piped()) |
| 3262 | .stdout(Stdio::piped()) |
| 3263 | .stderr(Stdio::piped()) |
| 3264 | .spawn() |
| 3265 | .expect("aomenc failed to start"); |
| 3266 | child |
| 3267 | .stdin |
| 3268 | .take() |
| 3269 | .expect("aomenc stdin") |
| 3270 | .write_all(&y4m.stdout) |
| 3271 | .expect("writing y4m to aomenc"); |
| 3272 | let out = child.wait_with_output().expect("aomenc failed to run"); |
| 3273 | assert!( |
| 3274 | out.status.success(), |
| 3275 | "aomenc refused the fixture: {}", |
| 3276 | String::from_utf8_lossy(&out.stderr) |
| 3277 | ); |
| 3278 | let stream = out.stdout; |
| 3279 | let before = decode::obmc_hits(); |
| 3280 | let frames = match decode_stream(&stream) { |
| 3281 | Err(e) => { |
| 3282 | let msg = e.to_string(); |
| 3283 | assert!( |
| 3284 | msg.contains("unsupported"), |
| 3285 | "{NAME} failed outright (seed {seed}): {msg}" |
| 3286 | ); |
| 3287 | refusals.push(format!("seed {seed}: {msg}")); |
| 3288 | continue; |
| 3289 | } |
| 3290 | Ok(frames) => frames, |
| 3291 | }; |
| 3292 | if decode::obmc_hits() == before { |
| 3293 | never_fired += 1; |
| 3294 | continue; |
| 3295 | } |
| 3296 | let ffmpeg_frames = ffmpeg_decode_sequence(&stream, width, height, frame_count); |
| 3297 | assert_eq!(frames.len(), frame_count); |
| 3298 | let mismatched = frames |
| 3299 | .iter() |
| 3300 | .zip(&ffmpeg_frames) |
| 3301 | .any(|(got, want)| got.y != want.y || got.u != want.u || got.v != want.v); |
| 3302 | if mismatched { |
| 3303 | if let Ok(path) = std::env::var("EC_AV1_GATE_DUMP") { |
| 3304 | std::fs::write(&path, &stream).expect("writing pinned stream"); |
| 3305 | eprintln!("EC_AV1_GATE_DUMP: wrote mismatching stream (seed {seed}) to {path}"); |
| 3306 | } |
| 3307 | } |
| 3308 | for (i, (got, want)) in frames.iter().zip(&ffmpeg_frames).enumerate() { |
| 3309 | assert_eq!(got.y, want.y, "{NAME} frame {i} luma vs ffmpeg (seed {seed})"); |
| 3310 | assert_eq!(got.u, want.u, "{NAME} frame {i} U vs ffmpeg (seed {seed})"); |
| 3311 | assert_eq!(got.v, want.v, "{NAME} frame {i} V vs ffmpeg (seed {seed})"); |
| 3312 | } |
| 3313 | eprintln!( |
| 3314 | "{NAME} FIRING seed {seed}: obmc hits advanced by {}", |
| 3315 | decode::obmc_hits() - before |
| 3316 | ); |
| 3317 | return; |
| 3318 | } |
| 3319 | eprintln!( |
| 3320 | "SKIP {NAME}: {never_fired} attempts decoded but never fired obmc, and every \ |
| 3321 | other attempt hit a named refusal:\n{}", |
| 3322 | refusals.join("\n") |
| 3323 | ); |
| 3324 | } |
| 3325 | |
| 3326 | /// lane-motionmode round 3: same shape as |
| 3327 | /// `a_real_aomenc_stream_with_obmc_decodes_pixel_exact`, but relaxed the |
| 3328 | /// `--min-partition-size=32 --max-partition-size=32` clamp (that other |
| 3329 | /// gate's own doc: locked to remove partition-depth choice from the |
| 3330 | /// search entirely) to `8`/`32`, to make an 8x8-leaf `OBMC_CAUSAL` pick |
| 3331 | /// possible at all. Gated on [`decode::obmc_hits_8`] specifically, not |
| 3332 | /// the aggregate [`decode::obmc_hits`]. Measured live (round 3, this |
| 3333 | /// box): 80 attempts, 0 ever called `decode_inter_block8` at all -- |
| 3334 | /// `cpu-used=0`'s RD search never once splits a 16x16 down to a clean |
| 3335 | /// 4-way 8x8 leaf for this smooth `gradients` fixture (large blocks |
| 3336 | /// always win the RD cost there); the handful of named refusals seen |
| 3337 | /// are `PARTITION_SPLIT` chosen *at* 8x8 (down to 4x4, already refused |
| 3338 | /// at `decode.rs`'s `part8 != PARTITION_NONE` check) or |
| 3339 | /// `allow_screen_content_tools`, neither reaching a leaf. Kept anyway |
| 3340 | /// (mirrors this file's own `never_fired`-skip convention) as the |
| 3341 | /// honest record of the attempt; a real 8x8 OBMC fixture needs |
| 3342 | /// higher-frequency content than this gate's fixtures use, out of |
| 3343 | /// round-3 budget. |
| 3344 | #[test] |
| 3345 | fn a_real_aomenc_stream_with_obmc_8x8_decodes_pixel_exact() { |
| 3346 | const NAME: &str = "a_real_aomenc_stream_with_obmc_8x8_decodes_pixel_exact"; |
| 3347 | let _gate_lock = lock_gate_counters(); |
| 3348 | if !have_ffmpeg() { |
| 3349 | eprintln!("SKIP {NAME}: no ffmpeg"); |
| 3350 | return; |
| 3351 | } |
| 3352 | if !have_aomenc() { |
| 3353 | eprintln!("SKIP {NAME}: no aomenc at {}", aomenc_path().display()); |
| 3354 | return; |
| 3355 | } |
| 3356 | let (width, height, frame_count) = (64usize, 64usize, 24usize); |
| 3357 | let mut refusals = Vec::new(); |
| 3358 | let mut never_fired = 0u32; |
| 3359 | let mut total_obmc8 = 0usize; |
| 3360 | let n_attempts: u32 = std::env::var("EC_OBMC8_ATTEMPTS") |
| 3361 | .ok() |
| 3362 | .and_then(|s| s.parse().ok()) |
| 3363 | .unwrap_or(80); |
| 3364 | for attempt in 0..n_attempts { |
| 3365 | let seed = 42 + attempt % 40; |
| 3366 | let duration = frame_count as f64 / 25.0; |
| 3367 | // Recipe search hook: `gradients` is smooth, so aomenc's RD never |
| 3368 | // splits below 16x16 and this gate never fired one 8x8 OBMC block |
| 3369 | // in 45 attempts -- it "passed" by skipping (gate-blind-to-feature). |
| 3370 | // EC_AV1_OBMC8_SOURCE overrides the lavfi source so a firing recipe |
| 3371 | // can be searched for without a rebuild. |
| 3372 | let source = std::env::var("EC_AV1_OBMC8_SOURCE").unwrap_or_else(|_| { |
| 3373 | gradients_source(seed, width, height, &format!("duration={duration}:rate=25")) |
| 3374 | }); |
| 3375 | // A source supplied through EC_AV1_OBMC8_SOURCE need not carry its |
| 3376 | // own `duration=`; `-t` is the bound that always holds. Without it |
| 3377 | // an endless source (`testsrc2`) fills the pipe forever and the |
| 3378 | // gate hangs to its 300 s timeout. |
| 3379 | let duration_arg = format!("{duration}"); |
| 3380 | let y4m = Command::new("ffmpeg") |
| 3381 | .args([ |
| 3382 | "-v", |
| 3383 | "error", |
| 3384 | "-f", |
| 3385 | "lavfi", |
| 3386 | "-i", |
| 3387 | &source, |
| 3388 | "-t", |
| 3389 | &duration_arg, |
| 3390 | "-pix_fmt", |
| 3391 | "yuv420p", |
| 3392 | "-f", |
| 3393 | "yuv4mpegpipe", |
| 3394 | "-", |
| 3395 | ]) |
| 3396 | .stdin(Stdio::null()) |
| 3397 | .stdout(Stdio::piped()) |
| 3398 | .stderr(Stdio::piped()) |
| 3399 | .output() |
| 3400 | .expect("ffmpeg failed to run"); |
| 3401 | assert!( |
| 3402 | y4m.status.success(), |
| 3403 | "ffmpeg fixture: {}", |
| 3404 | String::from_utf8_lossy(&y4m.stderr) |
| 3405 | ); |
| 3406 | let args: Vec<&str> = vec![ |
| 3407 | "--codec=av1", |
| 3408 | "--passes=1", |
| 3409 | "--end-usage=q", |
| 3410 | "--cq-level=45", |
| 3411 | "--cpu-used=0", |
| 3412 | "--kf-max-dist=1000", |
| 3413 | "--threads=1", |
| 3414 | "--row-mt=0", |
| 3415 | "--auto-alt-ref=1", |
| 3416 | "--lag-in-frames=16", |
| 3417 | "--enable-fwd-kf=0", |
| 3418 | "--enable-order-hint=1", |
| 3419 | "--enable-warped-motion=0", |
| 3420 | "--enable-obmc=1", |
| 3421 | "--enable-masked-comp=0", |
| 3422 | "--enable-interintra-comp=0", |
| 3423 | "--enable-onesided-comp=0", |
| 3424 | "--enable-interintra-wedge=0", |
| 3425 | "--enable-smooth-interintra=0", |
| 3426 | "--enable-rect-partitions=0", |
| 3427 | "--enable-ab-partitions=0", |
| 3428 | "--enable-1to4-partitions=0", |
| 3429 | "--enable-filter-intra=0", |
| 3430 | "--enable-smooth-intra=0", |
| 3431 | "--enable-paeth-intra=0", |
| 3432 | "--enable-directional-intra=0", |
| 3433 | "--enable-angle-delta=0", |
| 3434 | "--enable-tx-size-search=0", |
| 3435 | "--enable-cdef=0", |
| 3436 | "--enable-restoration=0", |
| 3437 | "--min-partition-size=8", |
| 3438 | "--max-partition-size=32", |
| 3439 | "--enable-palette=0", |
| 3440 | "--enable-intrabc=0", |
| 3441 | "--enable-cfl-intra=0", |
| 3442 | "--enable-ref-frame-mvs=0", |
| 3443 | "--obu", |
| 3444 | "-o", |
| 3445 | "-", |
| 3446 | "-", |
| 3447 | ]; |
| 3448 | let mut child = Command::new(aomenc_path()) |
| 3449 | .args(&args) |
| 3450 | .stdin(Stdio::piped()) |
| 3451 | .stdout(Stdio::piped()) |
| 3452 | .stderr(Stdio::piped()) |
| 3453 | .spawn() |
| 3454 | .expect("aomenc failed to start"); |
| 3455 | child |
| 3456 | .stdin |
| 3457 | .take() |
| 3458 | .expect("aomenc stdin") |
| 3459 | .write_all(&y4m.stdout) |
| 3460 | .expect("writing y4m to aomenc"); |
| 3461 | let out = child.wait_with_output().expect("aomenc failed to run"); |
| 3462 | assert!( |
| 3463 | out.status.success(), |
| 3464 | "aomenc refused the fixture: {}", |
| 3465 | String::from_utf8_lossy(&out.stderr) |
| 3466 | ); |
| 3467 | let stream = out.stdout; |
| 3468 | let before8 = decode::obmc_hits_8(); |
| 3469 | let frames = match decode_stream(&stream) { |
| 3470 | Err(e) => { |
| 3471 | let msg = e.to_string(); |
| 3472 | assert!( |
| 3473 | msg.contains("unsupported"), |
| 3474 | "{NAME} failed outright (seed {seed}): {msg}" |
| 3475 | ); |
| 3476 | refusals.push(format!("seed {seed}: {msg}")); |
| 3477 | continue; |
| 3478 | } |
| 3479 | Ok(frames) => frames, |
| 3480 | }; |
| 3481 | let fired8 = decode::obmc_hits_8() - before8; |
| 3482 | total_obmc8 += fired8; |
| 3483 | if fired8 == 0 { |
| 3484 | never_fired += 1; |
| 3485 | continue; |
| 3486 | } |
| 3487 | let ffmpeg_frames = ffmpeg_decode_sequence(&stream, width, height, frame_count); |
| 3488 | assert_eq!(frames.len(), frame_count); |
| 3489 | let mismatched = frames |
| 3490 | .iter() |
| 3491 | .zip(&ffmpeg_frames) |
| 3492 | .any(|(got, want)| got.y != want.y || got.u != want.u || got.v != want.v); |
| 3493 | if mismatched { |
| 3494 | if let Ok(path) = std::env::var("EC_AV1_GATE_DUMP") { |
| 3495 | std::fs::write(&path, &stream).expect("writing pinned stream"); |
| 3496 | eprintln!("EC_AV1_GATE_DUMP: wrote mismatching stream (seed {seed}) to {path}"); |
| 3497 | } |
| 3498 | } |
| 3499 | for (i, (got, want)) in frames.iter().zip(&ffmpeg_frames).enumerate() { |
| 3500 | assert_eq!(got.y, want.y, "{NAME} frame {i} luma vs ffmpeg (seed {seed})"); |
| 3501 | assert_eq!(got.u, want.u, "{NAME} frame {i} U vs ffmpeg (seed {seed})"); |
| 3502 | assert_eq!(got.v, want.v, "{NAME} frame {i} V vs ffmpeg (seed {seed})"); |
| 3503 | } |
| 3504 | eprintln!( |
| 3505 | "{NAME} FIRING seed {seed}: 8x8 obmc hits {fired8} (total across attempts so far {total_obmc8})" |
| 3506 | ); |
| 3507 | return; |
| 3508 | } |
| 3509 | eprintln!( |
| 3510 | "SKIP {NAME}: {never_fired} attempts decoded but never fired an 8x8 obmc block \ |
| 3511 | ({total_obmc8} total 8x8 obmc hits across all attempts), and every other attempt \ |
| 3512 | hit a named refusal:\n{}", |
| 3513 | refusals.join("\n") |
| 3514 | ); |
| 3515 | } |
| 3516 | |
| 3517 | /// lane-warp r5e (flipped from lane-motionmode r1's refuses-or-matches): |
| 3518 | /// `--enable-warped-motion=1` streams must DECODE pixel-exact -- |
| 3519 | /// `av1_findSamples`/`num_proj_ref`, the 3-symbol `motion_mode` read, |
| 3520 | /// the affine projection/filter, and the WARPED_CAUSAL interp-filter |
| 3521 | /// derivation are all ported. A refusal naming warp, or any pixel |
| 3522 | /// mismatch, fails the gate; refusals for OTHER named capabilities |
| 3523 | /// (screen content tools) still skip that seed. The sweep must also |
| 3524 | /// actually exercise warp (`warp_selected_hits > 0`) so a decoder that |
| 3525 | /// silently stops selecting WARPED_CAUSAL cannot pass vacuously. |
| 3526 | #[test] |
| 3527 | fn a_real_aomenc_stream_with_warped_motion_refuses_or_matches() { |
| 3528 | const NAME: &str = "a_real_aomenc_stream_with_warped_motion_refuses_or_matches"; |
| 3529 | let _gate_lock = lock_gate_counters(); |
| 3530 | if !have_ffmpeg() { |
| 3531 | eprintln!("SKIP {NAME}: no ffmpeg"); |
| 3532 | return; |
| 3533 | } |
| 3534 | if !have_aomenc() { |
| 3535 | eprintln!("SKIP {NAME}: no aomenc at {}", aomenc_path().display()); |
| 3536 | return; |
| 3537 | } |
| 3538 | let (width, height, frame_count) = (64usize, 64usize, 24usize); |
| 3539 | let mut named_refusals = 0u32; |
| 3540 | let mut matched = 0u32; |
| 3541 | let n_attempts: u32 = std::env::var("EC_WARP_GATE_ATTEMPTS") |
| 3542 | .ok() |
| 3543 | .and_then(|s| s.parse().ok()) |
| 3544 | .unwrap_or(40); |
| 3545 | for attempt in 0..n_attempts { |
| 3546 | let seed = 42 + attempt; |
| 3547 | let duration = frame_count as f64 / 25.0; |
| 3548 | let source = |
| 3549 | gradients_source(seed, width, height, &format!("duration={duration}:rate=25")); |
| 3550 | let y4m = Command::new("ffmpeg") |
| 3551 | .args([ |
| 3552 | "-v", |
| 3553 | "error", |
| 3554 | "-f", |
| 3555 | "lavfi", |
| 3556 | "-i", |
| 3557 | &source, |
| 3558 | "-pix_fmt", |
| 3559 | "yuv420p", |
| 3560 | "-f", |
| 3561 | "yuv4mpegpipe", |
| 3562 | "-", |
| 3563 | ]) |
| 3564 | .stdin(Stdio::null()) |
| 3565 | .stdout(Stdio::piped()) |
| 3566 | .stderr(Stdio::piped()) |
| 3567 | .output() |
| 3568 | .expect("ffmpeg failed to run"); |
| 3569 | assert!( |
| 3570 | y4m.status.success(), |
| 3571 | "ffmpeg fixture: {}", |
| 3572 | String::from_utf8_lossy(&y4m.stderr) |
| 3573 | ); |
| 3574 | let args: Vec<&str> = vec![ |
| 3575 | "--codec=av1", |
| 3576 | "--passes=1", |
| 3577 | "--end-usage=q", |
| 3578 | "--cq-level=45", |
| 3579 | "--cpu-used=0", |
| 3580 | "--kf-max-dist=1000", |
| 3581 | "--threads=1", |
| 3582 | "--row-mt=0", |
| 3583 | "--auto-alt-ref=1", |
| 3584 | "--lag-in-frames=16", |
| 3585 | "--enable-fwd-kf=0", |
| 3586 | "--enable-order-hint=1", |
| 3587 | "--enable-warped-motion=1", |
| 3588 | "--enable-obmc=1", |
| 3589 | "--tune-content=default", |
| 3590 | "--enable-masked-comp=0", |
| 3591 | "--enable-interintra-comp=0", |
| 3592 | "--enable-onesided-comp=0", |
| 3593 | "--enable-interintra-wedge=0", |
| 3594 | "--enable-smooth-interintra=0", |
| 3595 | "--enable-rect-partitions=0", |
| 3596 | "--enable-ab-partitions=0", |
| 3597 | "--enable-1to4-partitions=0", |
| 3598 | "--enable-filter-intra=0", |
| 3599 | "--enable-smooth-intra=0", |
| 3600 | "--enable-paeth-intra=0", |
| 3601 | "--enable-directional-intra=0", |
| 3602 | "--enable-angle-delta=0", |
| 3603 | "--enable-tx-size-search=0", |
| 3604 | "--enable-cdef=0", |
| 3605 | "--enable-restoration=0", |
| 3606 | "--max-partition-size=32", |
| 3607 | "--min-partition-size=32", |
| 3608 | "--enable-palette=0", |
| 3609 | "--enable-intrabc=0", |
| 3610 | "--enable-cfl-intra=0", |
| 3611 | "--enable-ref-frame-mvs=0", |
| 3612 | "--obu", |
| 3613 | "-o", |
| 3614 | "-", |
| 3615 | "-", |
| 3616 | ]; |
| 3617 | let mut child = Command::new(aomenc_path()) |
| 3618 | .args(&args) |
| 3619 | .stdin(Stdio::piped()) |
| 3620 | .stdout(Stdio::piped()) |
| 3621 | .stderr(Stdio::piped()) |
| 3622 | .spawn() |
| 3623 | .expect("aomenc failed to start"); |
| 3624 | child |
| 3625 | .stdin |
| 3626 | .take() |
| 3627 | .expect("aomenc stdin") |
| 3628 | .write_all(&y4m.stdout) |
| 3629 | .expect("writing y4m to aomenc"); |
| 3630 | let out = child.wait_with_output().expect("aomenc failed to run"); |
| 3631 | assert!( |
| 3632 | out.status.success(), |
| 3633 | "aomenc refused the fixture: {}", |
| 3634 | String::from_utf8_lossy(&out.stderr) |
| 3635 | ); |
| 3636 | let stream = out.stdout; |
| 3637 | let frames = match decode_stream(&stream) { |
| 3638 | Err(e) => { |
| 3639 | let msg = e.to_string(); |
| 3640 | assert!( |
| 3641 | msg.contains("unsupported"), |
| 3642 | "{NAME} failed outright, not a named refusal (seed {seed}): {msg}" |
| 3643 | ); |
| 3644 | assert!( |
| 3645 | !msg.contains("warp"), |
| 3646 | "{NAME} refused on warp (seed {seed}) -- warp decode is ported, this \ |
| 3647 | refusal must not exist: {msg}" |
| 3648 | ); |
| 3649 | named_refusals += 1; |
| 3650 | eprintln!("seed {seed} refusal (non-warp capability): {msg}"); |
| 3651 | continue; |
| 3652 | } |
| 3653 | Ok(frames) => frames, |
| 3654 | }; |
| 3655 | let ffmpeg_frames = ffmpeg_decode_sequence(&stream, width, height, frame_count); |
| 3656 | assert_eq!(frames.len(), frame_count); |
| 3657 | let mismatched = frames |
| 3658 | .iter() |
| 3659 | .zip(&ffmpeg_frames) |
| 3660 | .any(|(got, want)| got.y != want.y || got.u != want.u || got.v != want.v); |
| 3661 | if mismatched && let Ok(path) = std::env::var("EC_AV1_GATE_DUMP") { |
| 3662 | std::fs::write(&path, &stream).expect("writing pinned stream"); |
| 3663 | eprintln!("EC_AV1_GATE_DUMP: wrote mismatching stream (seed {seed}) to {path}"); |
| 3664 | } |
| 3665 | for (i, (got, want)) in frames.iter().zip(&ffmpeg_frames).enumerate() { |
| 3666 | assert_eq!(got.y, want.y, "{NAME} frame {i} luma vs ffmpeg (seed {seed})"); |
| 3667 | assert_eq!(got.u, want.u, "{NAME} frame {i} U vs ffmpeg (seed {seed})"); |
| 3668 | assert_eq!(got.v, want.v, "{NAME} frame {i} V vs ffmpeg (seed {seed})"); |
| 3669 | } |
| 3670 | matched += 1; |
| 3671 | } |
| 3672 | assert!( |
| 3673 | matched > 0, |
| 3674 | "{NAME}: every attempt refused on other capabilities; the gate never exercised warp" |
| 3675 | ); |
| 3676 | assert!( |
| 3677 | crate::decode::warp_selected_hits() > 0, |
| 3678 | "{NAME}: {matched} matches but zero WARPED_CAUSAL blocks fired -- the gate decodes \ |
| 3679 | warp streams without ever selecting warp, so it proves nothing about warp" |
| 3680 | ); |
| 3681 | eprintln!( |
| 3682 | "{NAME}: {named_refusals} non-warp refusals, {matched} pixel-exact matches out of {n_attempts}, warp_selected_hits={}", |
| 3683 | crate::decode::warp_selected_hits() |
| 3684 | ); |
| 3685 | } |
| 3686 | |
| 3687 | /// lane-interintra r1: `--enable-interintra-comp=1` streams must DECODE |
| 3688 | /// pixel-exact -- the interintra flag/mode/wedge-flag syntax and the |
| 3689 | /// non-wedge smooth blend are ported. A refusal naming interintra fails |
| 3690 | /// the gate UNLESS it is the wedge refusal (`use_wedge_interintra == 1` |
| 3691 | /// is r2, and this recipe encodes with wedge off so it must not fire |
| 3692 | /// either); warp/obmc are enabled and supported; refusals for OTHER |
| 3693 | /// named capabilities (screen content tools) still skip that seed. The |
| 3694 | /// sweep must actually exercise interintra (`interintra_hits > 0`). |
| 3695 | #[test] |
| 3696 | fn a_real_aomenc_stream_with_interintra_decodes_pixel_exact() { |
| 3697 | const NAME: &str = "a_real_aomenc_stream_with_interintra_decodes_pixel_exact"; |
| 3698 | let _gate_lock = lock_gate_counters(); |
| 3699 | if !have_ffmpeg() { |
| 3700 | eprintln!("SKIP {NAME}: no ffmpeg"); |
| 3701 | return; |
| 3702 | } |
| 3703 | if !have_aomenc() { |
| 3704 | eprintln!("SKIP {NAME}: no aomenc at {}", aomenc_path().display()); |
| 3705 | return; |
| 3706 | } |
| 3707 | let (width, height, frame_count) = (64usize, 64usize, 24usize); |
| 3708 | let mut named_refusals = 0u32; |
| 3709 | let mut matched = 0u32; |
| 3710 | let n_attempts: u32 = std::env::var("EC_INTERINTRA_GATE_ATTEMPTS") |
| 3711 | .ok() |
| 3712 | .and_then(|s| s.parse().ok()) |
| 3713 | .unwrap_or(40); |
| 3714 | for attempt in 0..n_attempts { |
| 3715 | let seed = 42 + attempt; |
| 3716 | let duration = frame_count as f64 / 25.0; |
| 3717 | let source = |
| 3718 | gradients_source(seed, width, height, &format!("duration={duration}:rate=25")); |
| 3719 | let y4m = Command::new("ffmpeg") |
| 3720 | .args([ |
| 3721 | "-v", |
| 3722 | "error", |
| 3723 | "-f", |
| 3724 | "lavfi", |
| 3725 | "-i", |
| 3726 | &source, |
| 3727 | "-pix_fmt", |
| 3728 | "yuv420p", |
| 3729 | "-f", |
| 3730 | "yuv4mpegpipe", |
| 3731 | "-", |
| 3732 | ]) |
| 3733 | .stdin(Stdio::null()) |
| 3734 | .stdout(Stdio::piped()) |
| 3735 | .stderr(Stdio::piped()) |
| 3736 | .output() |
| 3737 | .expect("ffmpeg failed to run"); |
| 3738 | assert!( |
| 3739 | y4m.status.success(), |
| 3740 | "ffmpeg fixture: {}", |
| 3741 | String::from_utf8_lossy(&y4m.stderr) |
| 3742 | ); |
| 3743 | let args: Vec<&str> = vec![ |
| 3744 | "--codec=av1", |
| 3745 | "--passes=1", |
| 3746 | "--end-usage=q", |
| 3747 | "--cq-level=45", |
| 3748 | "--cpu-used=0", |
| 3749 | "--kf-max-dist=1000", |
| 3750 | "--threads=1", |
| 3751 | "--row-mt=0", |
| 3752 | "--auto-alt-ref=1", |
| 3753 | "--lag-in-frames=16", |
| 3754 | "--enable-fwd-kf=0", |
| 3755 | "--enable-order-hint=1", |
| 3756 | "--enable-warped-motion=1", |
| 3757 | "--enable-obmc=1", |
| 3758 | "--tune-content=default", |
| 3759 | "--enable-masked-comp=0", |
| 3760 | "--enable-interintra-comp=1", |
| 3761 | "--enable-onesided-comp=0", |
| 3762 | "--enable-interintra-wedge=0", |
| 3763 | "--enable-smooth-interintra=1", |
| 3764 | "--enable-rect-partitions=0", |
| 3765 | "--enable-ab-partitions=0", |
| 3766 | "--enable-1to4-partitions=0", |
| 3767 | "--enable-filter-intra=0", |
| 3768 | "--enable-smooth-intra=0", |
| 3769 | "--enable-paeth-intra=0", |
| 3770 | "--enable-directional-intra=0", |
| 3771 | "--enable-angle-delta=0", |
| 3772 | "--enable-tx-size-search=0", |
| 3773 | "--enable-cdef=0", |
| 3774 | "--enable-restoration=0", |
| 3775 | "--max-partition-size=32", |
| 3776 | "--min-partition-size=32", |
| 3777 | "--enable-palette=0", |
| 3778 | "--enable-intrabc=0", |
| 3779 | "--enable-cfl-intra=0", |
| 3780 | "--enable-ref-frame-mvs=0", |
| 3781 | "--obu", |
| 3782 | "-o", |
| 3783 | "-", |
| 3784 | "-", |
| 3785 | ]; |
| 3786 | let mut child = Command::new(aomenc_path()) |
| 3787 | .args(&args) |
| 3788 | .stdin(Stdio::piped()) |
| 3789 | .stdout(Stdio::piped()) |
| 3790 | .stderr(Stdio::piped()) |
| 3791 | .spawn() |
| 3792 | .expect("aomenc failed to start"); |
| 3793 | child |
| 3794 | .stdin |
| 3795 | .take() |
| 3796 | .expect("aomenc stdin") |
| 3797 | .write_all(&y4m.stdout) |
| 3798 | .expect("writing y4m to aomenc"); |
| 3799 | let out = child.wait_with_output().expect("aomenc failed to run"); |
| 3800 | assert!( |
| 3801 | out.status.success(), |
| 3802 | "aomenc refused the fixture: {}", |
| 3803 | String::from_utf8_lossy(&out.stderr) |
| 3804 | ); |
| 3805 | let stream = out.stdout; |
| 3806 | let frames = match decode_stream(&stream) { |
| 3807 | Err(e) => { |
| 3808 | let msg = e.to_string(); |
| 3809 | assert!( |
| 3810 | msg.contains("unsupported"), |
| 3811 | "{NAME} failed outright, not a named refusal (seed {seed}): {msg}" |
| 3812 | ); |
| 3813 | assert!( |
| 3814 | !msg.contains("warp"), |
| 3815 | "{NAME} refused on warp (seed {seed}) -- warp decode is ported, this \ |
| 3816 | refusal must not exist: {msg}" |
| 3817 | ); |
| 3818 | assert!( |
| 3819 | !msg.contains("interintra"), |
| 3820 | "{NAME} refused on interintra (seed {seed}) -- non-wedge interintra \ |
| 3821 | is ported and this recipe encodes with wedge off: {msg}" |
| 3822 | ); |
| 3823 | named_refusals += 1; |
| 3824 | eprintln!("seed {seed} refusal (non-warp capability): {msg}"); |
| 3825 | continue; |
| 3826 | } |
| 3827 | Ok(frames) => frames, |
| 3828 | }; |
| 3829 | let ffmpeg_frames = ffmpeg_decode_sequence(&stream, width, height, frame_count); |
| 3830 | assert_eq!(frames.len(), frame_count); |
| 3831 | let mismatched = frames |
| 3832 | .iter() |
| 3833 | .zip(&ffmpeg_frames) |
| 3834 | .any(|(got, want)| got.y != want.y || got.u != want.u || got.v != want.v); |
| 3835 | if mismatched && let Ok(path) = std::env::var("EC_AV1_GATE_DUMP") { |
| 3836 | std::fs::write(&path, &stream).expect("writing pinned stream"); |
| 3837 | eprintln!("EC_AV1_GATE_DUMP: wrote mismatching stream (seed {seed}) to {path}"); |
| 3838 | } |
| 3839 | for (i, (got, want)) in frames.iter().zip(&ffmpeg_frames).enumerate() { |
| 3840 | assert_eq!(got.y, want.y, "{NAME} frame {i} luma vs ffmpeg (seed {seed})"); |
| 3841 | assert_eq!(got.u, want.u, "{NAME} frame {i} U vs ffmpeg (seed {seed})"); |
| 3842 | assert_eq!(got.v, want.v, "{NAME} frame {i} V vs ffmpeg (seed {seed})"); |
| 3843 | } |
| 3844 | matched += 1; |
| 3845 | } |
| 3846 | assert!( |
| 3847 | matched > 0, |
| 3848 | "{NAME}: every attempt refused on other capabilities; the gate never exercised warp" |
| 3849 | ); |
| 3850 | assert!( |
| 3851 | crate::decode::interintra_hits() > 0, |
| 3852 | "{NAME}: {matched} matches but zero interintra blocks fired -- the gate decodes \ |
| 3853 | these streams without ever taking interintra, so it proves nothing" |
| 3854 | ); |
| 3855 | eprintln!( |
| 3856 | "{NAME}: {named_refusals} other-capability refusals, {matched} pixel-exact matches out of {n_attempts}, interintra_hits={}", |
| 3857 | crate::decode::interintra_hits() |
| 3858 | ); |
| 3859 | } |
| 3860 | |
| 3861 | /// lane-wii r2: `--enable-interintra-wedge=1` streams must DECODE |
| 3862 | /// pixel-exact -- the wedge-interintra syntax (adapting `wedge_index` |
| 3863 | /// CDF symbol, fixed sign 0) and the wedge mask blend are ported over |
| 3864 | /// the checksum-verified codebook. `--enable-masked-comp=0` stays so |
| 3865 | /// the encoder cannot route inter blocks through the still-unported |
| 3866 | /// COMPOUND_WEDGE/DIFFWTD path. A refusal naming interintra/wedge |
| 3867 | /// fails the gate; refusals for OTHER named capabilities still skip |
| 3868 | /// that seed. Zero `wii_hits` on a run means the encoder never |
| 3869 | /// selected wedge-interintra -- soft-skipped (warned), not failed: |
| 3870 | /// that is encoder choice, not a decoder bug. |
| 3871 | #[test] |
| 3872 | fn a_real_aomenc_stream_with_interintra_wedge_decodes_pixel_exact() { |
| 3873 | const NAME: &str = "a_real_aomenc_stream_with_interintra_wedge_decodes_pixel_exact"; |
| 3874 | let _gate_lock = lock_gate_counters(); |
| 3875 | if !have_ffmpeg() { |
| 3876 | eprintln!("SKIP {NAME}: no ffmpeg"); |
| 3877 | return; |
| 3878 | } |
| 3879 | if !have_aomenc() { |
| 3880 | eprintln!("SKIP {NAME}: no aomenc at {}", aomenc_path().display()); |
| 3881 | return; |
| 3882 | } |
| 3883 | let (width, height, frame_count) = (64usize, 64usize, 24usize); |
| 3884 | let mut named_refusals = 0u32; |
| 3885 | let mut matched = 0u32; |
| 3886 | let n_attempts: u32 = std::env::var("EC_WEDGE_GATE_ATTEMPTS") |
| 3887 | .ok() |
| 3888 | .and_then(|s| s.parse().ok()) |
| 3889 | .unwrap_or(40); |
| 3890 | for attempt in 0..n_attempts { |
| 3891 | let seed = 42 + attempt; |
| 3892 | let duration = frame_count as f64 / 25.0; |
| 3893 | let source = if attempt % 2 == 0 { |
| 3894 | gradients_source(seed, width, height, &format!("duration={duration}:rate=25")) |
| 3895 | } else { |
| 3896 | // `end_pts` does NOT bound mandelbrot (it generated 300+ |
| 3897 | // frames and hung the gate for an hour); `-t` below is the |
| 3898 | // real bound, matching every other mandelbrot gate recipe. |
| 3899 | format!("mandelbrot=size={width}x{height}:rate=25") |
| 3900 | }; |
| 3901 | let duration_arg = format!("{duration}"); |
| 3902 | let y4m = Command::new("ffmpeg") |
| 3903 | .args([ |
| 3904 | "-v", |
| 3905 | "error", |
| 3906 | "-f", |
| 3907 | "lavfi", |
| 3908 | "-i", |
| 3909 | &source, |
| 3910 | "-pix_fmt", |
| 3911 | "yuv420p", |
| 3912 | "-t", |
| 3913 | &duration_arg, |
| 3914 | "-f", |
| 3915 | "yuv4mpegpipe", |
| 3916 | "-", |
| 3917 | ]) |
| 3918 | .stdin(Stdio::null()) |
| 3919 | .stdout(Stdio::piped()) |
| 3920 | .stderr(Stdio::piped()) |
| 3921 | .output() |
| 3922 | .expect("ffmpeg failed to run"); |
| 3923 | assert!( |
| 3924 | y4m.status.success(), |
| 3925 | "ffmpeg fixture: {}", |
| 3926 | String::from_utf8_lossy(&y4m.stderr) |
| 3927 | ); |
| 3928 | let args: Vec<&str> = vec![ |
| 3929 | "--codec=av1", |
| 3930 | "--passes=1", |
| 3931 | "--end-usage=q", |
| 3932 | "--cq-level=45", |
| 3933 | "--cpu-used=0", |
| 3934 | "--kf-max-dist=1000", |
| 3935 | "--threads=1", |
| 3936 | "--row-mt=0", |
| 3937 | "--auto-alt-ref=1", |
| 3938 | "--lag-in-frames=16", |
| 3939 | "--enable-fwd-kf=0", |
| 3940 | "--enable-order-hint=1", |
| 3941 | "--enable-warped-motion=1", |
| 3942 | "--enable-obmc=1", |
| 3943 | "--tune-content=default", |
| 3944 | "--enable-masked-comp=0", |
| 3945 | "--enable-interintra-comp=1", |
| 3946 | "--enable-onesided-comp=0", |
| 3947 | "--enable-interintra-wedge=1", |
| 3948 | "--enable-smooth-interintra=1", |
| 3949 | "--enable-rect-partitions=0", |
| 3950 | "--enable-ab-partitions=0", |
| 3951 | "--enable-1to4-partitions=0", |
| 3952 | "--enable-filter-intra=0", |
| 3953 | "--enable-smooth-intra=0", |
| 3954 | "--enable-paeth-intra=0", |
| 3955 | "--enable-directional-intra=0", |
| 3956 | "--enable-angle-delta=0", |
| 3957 | "--enable-tx-size-search=0", |
| 3958 | "--enable-cdef=0", |
| 3959 | "--enable-restoration=0", |
| 3960 | "--max-partition-size=32", |
| 3961 | "--min-partition-size=32", |
| 3962 | "--enable-palette=0", |
| 3963 | "--enable-intrabc=0", |
| 3964 | "--enable-cfl-intra=0", |
| 3965 | "--enable-ref-frame-mvs=0", |
| 3966 | "--obu", |
| 3967 | "-o", |
| 3968 | "-", |
| 3969 | "-", |
| 3970 | ]; |
| 3971 | let mut child = Command::new(aomenc_path()) |
| 3972 | .args(&args) |
| 3973 | .stdin(Stdio::piped()) |
| 3974 | .stdout(Stdio::piped()) |
| 3975 | .stderr(Stdio::piped()) |
| 3976 | .spawn() |
| 3977 | .expect("aomenc failed to start"); |
| 3978 | child |
| 3979 | .stdin |
| 3980 | .take() |
| 3981 | .expect("aomenc stdin") |
| 3982 | .write_all(&y4m.stdout) |
| 3983 | .expect("writing y4m to aomenc"); |
| 3984 | let out = child.wait_with_output().expect("aomenc failed to run"); |
| 3985 | assert!( |
| 3986 | out.status.success(), |
| 3987 | "aomenc refused the fixture: {}", |
| 3988 | String::from_utf8_lossy(&out.stderr) |
| 3989 | ); |
| 3990 | let stream = out.stdout; |
| 3991 | let frames = match decode_stream(&stream) { |
| 3992 | Err(e) => { |
| 3993 | let msg = e.to_string(); |
| 3994 | assert!( |
| 3995 | msg.contains("unsupported"), |
| 3996 | "{NAME} failed outright, not a named refusal (seed {seed}): {msg}" |
| 3997 | ); |
| 3998 | // Verifier note (lane-wii r2): no refusal string containing |
| 3999 | // "wedge"/"interintra" exists in the decoder any more, so |
| 4000 | // string-forbidding asserts here would be vacuous (class |
| 4001 | // gate-blind-to-feature). The proof this gate carries is |
| 4002 | // pixel-exact decode plus the wii_hits count printed below. |
| 4003 | named_refusals += 1; |
| 4004 | eprintln!("seed {seed} refusal (non-wedge capability): {msg}"); |
| 4005 | continue; |
| 4006 | } |
| 4007 | Ok(frames) => frames, |
| 4008 | }; |
| 4009 | let ffmpeg_frames = ffmpeg_decode_sequence(&stream, width, height, frame_count); |
| 4010 | assert_eq!(frames.len(), frame_count); |
| 4011 | let mismatched = frames |
| 4012 | .iter() |
| 4013 | .zip(&ffmpeg_frames) |
| 4014 | .any(|(got, want)| got.y != want.y || got.u != want.u || got.v != want.v); |
| 4015 | if mismatched && let Ok(path) = std::env::var("EC_AV1_GATE_DUMP") { |
| 4016 | std::fs::write(&path, &stream).expect("writing pinned stream"); |
| 4017 | eprintln!("EC_AV1_GATE_DUMP: wrote mismatching stream (seed {seed}) to {path}"); |
| 4018 | } |
| 4019 | for (i, (got, want)) in frames.iter().zip(&ffmpeg_frames).enumerate() { |
| 4020 | assert_eq!(got.y, want.y, "{NAME} frame {i} luma vs ffmpeg (seed {seed})"); |
| 4021 | assert_eq!(got.u, want.u, "{NAME} frame {i} U vs ffmpeg (seed {seed})"); |
| 4022 | assert_eq!(got.v, want.v, "{NAME} frame {i} V vs ffmpeg (seed {seed})"); |
| 4023 | } |
| 4024 | matched += 1; |
| 4025 | } |
| 4026 | assert!( |
| 4027 | matched > 0, |
| 4028 | "{NAME}: every attempt refused on other capabilities; the gate never exercised wedge-interintra" |
| 4029 | ); |
| 4030 | // Hardened from a soft-note (verifier: gate-blind-to-feature): the |
| 4031 | // recipe fired wedge-interintra on EVERY one of 6 hammer runs |
| 4032 | // (wii_hits 2-7), so a zero-hit run is a regression, not sampling. |
| 4033 | assert!( |
| 4034 | crate::decode::wii_hits() > 0, |
| 4035 | "{NAME}: {matched} pixel-exact matches but zero wedge-interintra blocks fired -- \ |
| 4036 | the blend is unexercised (recipe fired 2-7 hits per run when landed)" |
| 4037 | ); |
| 4038 | eprintln!( |
| 4039 | "{NAME}: {named_refusals} other-capability refusals, {matched} pixel-exact matches out of {n_attempts}, wii_hits={}", |
| 4040 | crate::decode::wii_hits() |
| 4041 | ); |
| 4042 | } |
| 4043 | /// lane-rectgate r1: every prior gate pins `--enable-rect-partitions=0 |
| 4044 | /// --enable-ab-partitions=0 --min/max-partition-size=32` so aomenc never |
| 4045 | /// gets to choose a real rect/ab split. This charter's premise ("rect |
| 4046 | /// HORZ/VERT + HORZ_A/HORZ_B/VERT_A/VERT_B landed during the warp lane") |
| 4047 | /// does NOT hold: `decode_frame`'s inter-tile `match part32` only has |
| 4048 | /// arms for `PARTITION_NONE`/`SPLIT`/`HORZ_B` -- `HORZ`, `VERT`, |
| 4049 | /// `HORZ_A`, `VERT_A`, `VERT_B`, `HORZ_4`, `VERT_4` all fall into the |
| 4050 | /// generic "a partition type this encoder never writes" `_` arm. Two |
| 4051 | /// recipes were swept empirically (40 attempts each) to characterize the |
| 4052 | /// gap: |
| 4053 | /// - `--enable-rect-partitions=1 --enable-ab-partitions=1`, no |
| 4054 | /// min/max clamp: 0/40 matched -- every attempt hit either the |
| 4055 | /// screen-content refusal or a genuinely-unimplemented partition value |
| 4056 | /// (1/2/6/7/9 all observed), always somewhere in the 24-frame stream. |
| 4057 | /// - Same, but `--enable-1to4-partitions=0` explicit and |
| 4058 | /// `--min-partition-size=16`: still 0/40, `value=9` (`VERT_4`) |
| 4059 | /// dominates -- this build's `--enable-1to4-partitions=0` does NOT |
| 4060 | /// suppress `VERT_4` selection (aomenc-side, confirmed by direct flag |
| 4061 | /// toggle; outside this decoder). This recipe (below) restores the |
| 4062 | /// `--enable-rect-partitions=0 --min/max-partition-size=32` pin every |
| 4063 | /// other gate uses, flipping only `--enable-ab-partitions=1`: matches |
| 4064 | /// cleanly (33/40, rest screen-content refusals) but |
| 4065 | /// `extended_partition_hits` stayed 0 -- confirmed with the same probe |
| 4066 | /// wired into the already-green warp gate too (144 `warp_selected_hits`, |
| 4067 | /// 0 `extended_partition_hits`), so `PARTITION_HORZ_B` itself is |
| 4068 | /// apparently never chosen by aomenc for this small gradient fixture at |
| 4069 | /// `cq-level=45` regardless of clamp. No decode defect (crash or pixel |
| 4070 | /// mismatch) was found in any sweep -- every refusal is a correctly |
| 4071 | /// named, non-silent "unsupported" error. A refusal or mismatch naming |
| 4072 | /// any OTHER already-ported capability (warp, obmc, interintra) still |
| 4073 | /// fails the gate outright. |
| 4074 | #[test] |
| 4075 | fn a_real_aomenc_stream_with_free_partitions_decodes_pixel_exact() { |
| 4076 | const NAME: &str = "a_real_aomenc_stream_with_free_partitions_decodes_pixel_exact"; |
| 4077 | let _gate_lock = lock_gate_counters(); |
| 4078 | if !have_ffmpeg() { |
| 4079 | eprintln!("SKIP {NAME}: no ffmpeg"); |
| 4080 | return; |
| 4081 | } |
| 4082 | if !have_aomenc() { |
| 4083 | eprintln!("SKIP {NAME}: no aomenc at {}", aomenc_path().display()); |
| 4084 | return; |
| 4085 | } |
| 4086 | let (width, height, frame_count) = (64usize, 64usize, 24usize); |
| 4087 | let mut named_refusals = 0u32; |
| 4088 | let mut matched = 0u32; |
| 4089 | let n_attempts: u32 = std::env::var("EC_RECTGATE_ATTEMPTS") |
| 4090 | .ok() |
| 4091 | .and_then(|s| s.parse().ok()) |
| 4092 | .unwrap_or(40); |
| 4093 | for attempt in 0..n_attempts { |
| 4094 | let seed = 42 + attempt; |
| 4095 | let duration = frame_count as f64 / 25.0; |
| 4096 | let source = |
| 4097 | gradients_source(seed, width, height, &format!("duration={duration}:rate=25")); |
| 4098 | let y4m = Command::new("ffmpeg") |
| 4099 | .args([ |
| 4100 | "-v", |
| 4101 | "error", |
| 4102 | "-f", |
| 4103 | "lavfi", |
| 4104 | "-i", |
| 4105 | &source, |
| 4106 | "-pix_fmt", |
| 4107 | "yuv420p", |
| 4108 | "-f", |
| 4109 | "yuv4mpegpipe", |
| 4110 | "-", |
| 4111 | ]) |
| 4112 | .stdin(Stdio::null()) |
| 4113 | .stdout(Stdio::piped()) |
| 4114 | .stderr(Stdio::piped()) |
| 4115 | .output() |
| 4116 | .expect("ffmpeg failed to run"); |
| 4117 | assert!( |
| 4118 | y4m.status.success(), |
| 4119 | "ffmpeg fixture: {}", |
| 4120 | String::from_utf8_lossy(&y4m.stderr) |
| 4121 | ); |
| 4122 | let args: Vec<&str> = vec![ |
| 4123 | "--codec=av1", |
| 4124 | "--passes=1", |
| 4125 | "--end-usage=q", |
| 4126 | "--cq-level=45", |
| 4127 | "--cpu-used=0", |
| 4128 | "--kf-max-dist=1000", |
| 4129 | "--threads=1", |
| 4130 | "--row-mt=0", |
| 4131 | "--auto-alt-ref=1", |
| 4132 | "--lag-in-frames=16", |
| 4133 | "--enable-fwd-kf=0", |
| 4134 | "--enable-order-hint=1", |
| 4135 | "--enable-warped-motion=1", |
| 4136 | "--enable-obmc=1", |
| 4137 | "--tune-content=default", |
| 4138 | "--enable-masked-comp=0", |
| 4139 | "--enable-interintra-comp=0", |
| 4140 | "--enable-onesided-comp=0", |
| 4141 | "--enable-interintra-wedge=0", |
| 4142 | "--enable-smooth-interintra=0", |
| 4143 | // lane-rect r2: HORZ/VERT decode for real (rect-flake-1 |
| 4144 | // byte-exact: size_h + warp dims + OBMC overlap + tx height) |
| 4145 | // -- free recipe restored; remaining partition kinds refuse |
| 4146 | // by name and skip their seeds. |
| 4147 | "--enable-rect-partitions=1", |
| 4148 | "--enable-ab-partitions=1", |
| 4149 | "--enable-1to4-partitions=0", |
| 4150 | "--enable-filter-intra=0", |
| 4151 | "--enable-smooth-intra=0", |
| 4152 | "--enable-paeth-intra=0", |
| 4153 | "--enable-directional-intra=0", |
| 4154 | "--enable-angle-delta=0", |
| 4155 | "--enable-tx-size-search=0", |
| 4156 | "--enable-cdef=0", |
| 4157 | "--enable-restoration=0", |
| 4158 | "--max-partition-size=32", |
| 4159 | "--min-partition-size=16", |
| 4160 | "--enable-palette=0", |
| 4161 | "--enable-intrabc=0", |
| 4162 | "--enable-cfl-intra=0", |
| 4163 | "--enable-ref-frame-mvs=0", |
| 4164 | "--obu", |
| 4165 | "-o", |
| 4166 | "-", |
| 4167 | "-", |
| 4168 | ]; |
| 4169 | let mut child = Command::new(aomenc_path()) |
| 4170 | .args(&args) |
| 4171 | .stdin(Stdio::piped()) |
| 4172 | .stdout(Stdio::piped()) |
| 4173 | .stderr(Stdio::piped()) |
| 4174 | .spawn() |
| 4175 | .expect("aomenc failed to start"); |
| 4176 | child |
| 4177 | .stdin |
| 4178 | .take() |
| 4179 | .expect("aomenc stdin") |
| 4180 | .write_all(&y4m.stdout) |
| 4181 | .expect("writing y4m to aomenc"); |
| 4182 | let out = child.wait_with_output().expect("aomenc failed to run"); |
| 4183 | assert!( |
| 4184 | out.status.success(), |
| 4185 | "aomenc refused the fixture: {}", |
| 4186 | String::from_utf8_lossy(&out.stderr) |
| 4187 | ); |
| 4188 | let stream = out.stdout; |
| 4189 | let frames = match decode_stream(&stream) { |
| 4190 | Err(e) => { |
| 4191 | let msg = e.to_string(); |
| 4192 | assert!( |
| 4193 | msg.contains("unsupported"), |
| 4194 | "{NAME} failed outright, not a named refusal (seed {seed}): {msg}" |
| 4195 | ); |
| 4196 | for banned in ["warp", "obmc", "interintra"] { |
| 4197 | assert!( |
| 4198 | !msg.contains(banned), |
| 4199 | "{NAME} refused on {banned} (seed {seed}) -- that capability is \ |
| 4200 | ported and this recipe does not need it: {msg}" |
| 4201 | ); |
| 4202 | } |
| 4203 | named_refusals += 1; |
| 4204 | eprintln!("seed {seed} refusal: {msg}"); |
| 4205 | continue; |
| 4206 | } |
| 4207 | Ok(frames) => frames, |
| 4208 | }; |
| 4209 | let ffmpeg_frames = ffmpeg_decode_sequence(&stream, width, height, frame_count); |
| 4210 | assert_eq!(frames.len(), frame_count); |
| 4211 | let mismatched = frames |
| 4212 | .iter() |
| 4213 | .zip(&ffmpeg_frames) |
| 4214 | .any(|(got, want)| got.y != want.y || got.u != want.u || got.v != want.v); |
| 4215 | if mismatched && let Ok(path) = std::env::var("EC_AV1_GATE_DUMP") { |
| 4216 | std::fs::write(&path, &stream).expect("writing pinned stream"); |
| 4217 | eprintln!("EC_AV1_GATE_DUMP: wrote mismatching stream (seed {seed}) to {path}"); |
| 4218 | } |
| 4219 | for (i, (got, want)) in frames.iter().zip(&ffmpeg_frames).enumerate() { |
| 4220 | assert_eq!(got.y, want.y, "{NAME} frame {i} luma vs ffmpeg (seed {seed})"); |
| 4221 | assert_eq!(got.u, want.u, "{NAME} frame {i} U vs ffmpeg (seed {seed})"); |
| 4222 | assert_eq!(got.v, want.v, "{NAME} frame {i} V vs ffmpeg (seed {seed})"); |
| 4223 | } |
| 4224 | matched += 1; |
| 4225 | } |
| 4226 | assert!( |
| 4227 | matched > 0, |
| 4228 | "{NAME}: every attempt refused; the gate never decoded a free-partition stream" |
| 4229 | ); |
| 4230 | // lane-partitions r1: HORZ/VERT arms landed, so the diversity probe |
| 4231 | // graduates from rectgate r1's eprintln to a report line on |
| 4232 | // `rect_partition_hits`; a zero-hit run SOFT-reports (aomenc RD may |
| 4233 | // legitimately never pick a rect split in a small window -- same |
| 4234 | // sampling caveat as the masked-compound gate's zero-hit runs). |
| 4235 | // `extended_partition_hits` (HORZ_B) stays unasserted per rectgate r1. |
| 4236 | if crate::decode::rect_partition_hits() == 0 { |
| 4237 | eprintln!( |
| 4238 | "{NAME}: SOFT-NOTE -- zero HORZ/VERT strips fired this run \ |
| 4239 | ({matched} matches, {named_refusals} refusals); sampling, not a regression" |
| 4240 | ); |
| 4241 | } else { |
| 4242 | // r5: the desync was the tx-depth symbol being read |
| 4243 | // unconditionally where both square paths gate it on `tx_select` |
| 4244 | // -- with --enable-tx-size-search=0 the encoder writes no such |
| 4245 | // symbol and the decoder consumed one that was never there. With |
| 4246 | // that fixed the real coefficient decode is pixel-exact, so this |
| 4247 | // is a HARD assert again: strips fired but no coefficients read |
| 4248 | // would mean the coded path silently stopped being exercised. |
| 4249 | assert!( |
| 4250 | crate::decode::rect_coeff_hits() > 0, |
| 4251 | "{NAME}: {} HORZ/VERT strips fired but rect_coeff_hits==0 -- the \ |
| 4252 | coded rect path is no longer exercised", |
| 4253 | crate::decode::rect_partition_hits() |
| 4254 | ); |
| 4255 | } |
| 4256 | eprintln!( |
| 4257 | "{NAME}: {named_refusals} named refusals, {matched} pixel-exact matches out of {n_attempts}, rect_partition_hits={} rect_coeff_hits={} extended_partition_hits={} partab_hits={}", |
| 4258 | crate::decode::rect_partition_hits(), |
| 4259 | crate::decode::rect_coeff_hits(), |
| 4260 | crate::decode::extended_partition_hits(), |
| 4261 | crate::decode::partab_hits() |
| 4262 | ); |
| 4263 | } |
| 4264 | |
| 4265 | /// lane-partab r1: `--enable-ab-partitions=1` (aomenc default-on) INTER |
| 4266 | /// streams must consume PARTITION_HORZ_A/VERT_A/VERT_B entropy-exact AND |
| 4267 | /// decode pixel-exact -- `partab_hits() > 0` is soft-skipped on a |
| 4268 | /// zero-hit run (aomenc RD may legitimately never pick an AB split in a |
| 4269 | /// small window, same sampling caveat as wedge/rect). The INTER AB |
| 4270 | /// refusal is FORBIDDEN (decode.rs names it "an INTER 32x32 partition |
| 4271 | /// ..."); intra-frame AB refusals stay accepted named refusals -- the |
| 4272 | /// intra dispatch is a later lane. Recipe = the free-partition gate's, |
| 4273 | /// which fires AB live (r1: partab_hits 6/2/4/3/1/5 over six |
| 4274 | /// 40-attempt runs). |
| 4275 | #[test] |
| 4276 | fn a_real_aomenc_stream_with_ab_partitions_decodes_pixel_exact() { |
| 4277 | const NAME: &str = "a_real_aomenc_stream_with_ab_partitions_decodes_pixel_exact"; |
| 4278 | let _gate_lock = lock_gate_counters(); |
| 4279 | if !have_ffmpeg() { |
| 4280 | eprintln!("SKIP {NAME}: no ffmpeg"); |
| 4281 | return; |
| 4282 | } |
| 4283 | if !have_aomenc() { |
| 4284 | eprintln!("SKIP {NAME}: no aomenc at {}", aomenc_path().display()); |
| 4285 | return; |
| 4286 | } |
| 4287 | let (width, height, frame_count) = (64usize, 64usize, 24usize); |
| 4288 | let mut named_refusals = 0u32; |
| 4289 | let mut matched = 0u32; |
| 4290 | let n_attempts: u32 = std::env::var("EC_RECTGATE_ATTEMPTS") |
| 4291 | .ok() |
| 4292 | .and_then(|s| s.parse().ok()) |
| 4293 | .unwrap_or(40); |
| 4294 | for attempt in 0..n_attempts { |
| 4295 | let seed = 42 + attempt; |
| 4296 | let duration = frame_count as f64 / 25.0; |
| 4297 | let source = |
| 4298 | gradients_source(seed, width, height, &format!("duration={duration}:rate=25")); |
| 4299 | let y4m = Command::new("ffmpeg") |
| 4300 | .args([ |
| 4301 | "-v", |
| 4302 | "error", |
| 4303 | "-f", |
| 4304 | "lavfi", |
| 4305 | "-i", |
| 4306 | &source, |
| 4307 | "-pix_fmt", |
| 4308 | "yuv420p", |
| 4309 | "-f", |
| 4310 | "yuv4mpegpipe", |
| 4311 | "-", |
| 4312 | ]) |
| 4313 | .stdin(Stdio::null()) |
| 4314 | .stdout(Stdio::piped()) |
| 4315 | .stderr(Stdio::piped()) |
| 4316 | .output() |
| 4317 | .expect("ffmpeg failed to run"); |
| 4318 | assert!( |
| 4319 | y4m.status.success(), |
| 4320 | "ffmpeg fixture: {}", |
| 4321 | String::from_utf8_lossy(&y4m.stderr) |
| 4322 | ); |
| 4323 | let args: Vec<&str> = vec![ |
| 4324 | "--codec=av1", |
| 4325 | "--passes=1", |
| 4326 | "--end-usage=q", |
| 4327 | "--cq-level=45", |
| 4328 | "--cpu-used=0", |
| 4329 | "--kf-max-dist=1000", |
| 4330 | "--threads=1", |
| 4331 | "--row-mt=0", |
| 4332 | "--auto-alt-ref=1", |
| 4333 | "--lag-in-frames=16", |
| 4334 | "--enable-fwd-kf=0", |
| 4335 | "--enable-order-hint=1", |
| 4336 | "--enable-warped-motion=1", |
| 4337 | "--enable-obmc=1", |
| 4338 | "--tune-content=default", |
| 4339 | "--enable-masked-comp=0", |
| 4340 | "--enable-interintra-comp=0", |
| 4341 | "--enable-onesided-comp=0", |
| 4342 | "--enable-interintra-wedge=0", |
| 4343 | "--enable-smooth-interintra=0", |
| 4344 | // lane-rect r2 recipe, kept byte-identical by lane-partab |
| 4345 | // r1: free partition search, 1to4 off, intra tools off. |
| 4346 | "--enable-rect-partitions=1", |
| 4347 | "--enable-ab-partitions=1", |
| 4348 | "--enable-1to4-partitions=0", |
| 4349 | "--enable-filter-intra=0", |
| 4350 | "--enable-smooth-intra=0", |
| 4351 | "--enable-paeth-intra=0", |
| 4352 | "--enable-directional-intra=0", |
| 4353 | "--enable-angle-delta=0", |
| 4354 | "--enable-tx-size-search=0", |
| 4355 | "--enable-cdef=0", |
| 4356 | "--enable-restoration=0", |
| 4357 | "--max-partition-size=32", |
| 4358 | "--min-partition-size=16", |
| 4359 | "--enable-palette=0", |
| 4360 | "--enable-intrabc=0", |
| 4361 | "--enable-cfl-intra=0", |
| 4362 | "--enable-ref-frame-mvs=0", |
| 4363 | "--obu", |
| 4364 | "-o", |
| 4365 | "-", |
| 4366 | "-", |
| 4367 | ]; |
| 4368 | let mut child = Command::new(aomenc_path()) |
| 4369 | .args(&args) |
| 4370 | .stdin(Stdio::piped()) |
| 4371 | .stdout(Stdio::piped()) |
| 4372 | .stderr(Stdio::piped()) |
| 4373 | .spawn() |
| 4374 | .expect("aomenc failed to start"); |
| 4375 | child |
| 4376 | .stdin |
| 4377 | .take() |
| 4378 | .expect("aomenc stdin") |
| 4379 | .write_all(&y4m.stdout) |
| 4380 | .expect("writing y4m to aomenc"); |
| 4381 | let out = child.wait_with_output().expect("aomenc failed to run"); |
| 4382 | assert!( |
| 4383 | out.status.success(), |
| 4384 | "aomenc refused the fixture: {}", |
| 4385 | String::from_utf8_lossy(&out.stderr) |
| 4386 | ); |
| 4387 | let stream = out.stdout; |
| 4388 | let frames = match decode_stream(&stream) { |
| 4389 | Err(e) => { |
| 4390 | let msg = e.to_string(); |
| 4391 | assert!( |
| 4392 | msg.contains("unsupported"), |
| 4393 | "{NAME} failed outright, not a named refusal (seed {seed}): {msg}" |
| 4394 | ); |
| 4395 | for banned in ["warp", "obmc", "interintra"] { |
| 4396 | assert!( |
| 4397 | !msg.contains(banned), |
| 4398 | "{NAME} refused on {banned} (seed {seed}) -- that capability is \ |
| 4399 | ported and this recipe does not need it: {msg}" |
| 4400 | ); |
| 4401 | } |
| 4402 | for v in [4, 6, 7] { |
| 4403 | assert!( |
| 4404 | !msg.contains(&format!( |
| 4405 | "an INTER 32x32 partition type this encoder never writes \ |
| 4406 | (value={v})" |
| 4407 | )), |
| 4408 | "{NAME} refused an INTER AB partition (value={v}, seed {seed}) -- \ |
| 4409 | all three AB arms are ported, this refusal is forbidden: {msg}" |
| 4410 | ); |
| 4411 | } |
| 4412 | named_refusals += 1; |
| 4413 | eprintln!("seed {seed} refusal: {msg}"); |
| 4414 | continue; |
| 4415 | } |
| 4416 | Ok(frames) => frames, |
| 4417 | }; |
| 4418 | let ffmpeg_frames = ffmpeg_decode_sequence(&stream, width, height, frame_count); |
| 4419 | assert_eq!(frames.len(), frame_count); |
| 4420 | for (i, (got, want)) in frames.iter().zip(&ffmpeg_frames).enumerate() { |
| 4421 | assert_eq!(got.y, want.y, "{NAME} frame {i} luma vs ffmpeg (seed {seed})"); |
| 4422 | assert_eq!(got.u, want.u, "{NAME} frame {i} U vs ffmpeg (seed {seed})"); |
| 4423 | assert_eq!(got.v, want.v, "{NAME} frame {i} V vs ffmpeg (seed {seed})"); |
| 4424 | } |
| 4425 | matched += 1; |
| 4426 | } |
| 4427 | assert!( |
| 4428 | matched > 0, |
| 4429 | "{NAME}: every attempt refused; the gate never decoded a stream" |
| 4430 | ); |
| 4431 | if crate::decode::partab_hits() == 0 { |
| 4432 | eprintln!( |
| 4433 | "{NAME}: SOFT-NOTE -- zero AB partitions fired this run \ |
| 4434 | ({matched} matches, {named_refusals} refusals); sampling, not a regression" |
| 4435 | ); |
| 4436 | } |
| 4437 | eprintln!( |
| 4438 | "{NAME}: {named_refusals} named refusals, {matched} pixel-exact matches out of {n_attempts}, partab_hits={}", |
| 4439 | crate::decode::partab_hits() |
| 4440 | ); |
| 4441 | } |
| 4442 | |
| 4443 | /// lane-maskcomp r2 / lane-wedge r3: `--enable-masked-comp=1` streams |
| 4444 | /// must consume `compound_type`/`wedge_idx`/`wedge_sign`/`mask_type` |
| 4445 | /// entropy-exact AND build the real blend, DIFFWTD or WEDGE -- |
| 4446 | /// `masked_compound_hits() > 0` is a hard assert and BOTH |
| 4447 | /// `COMPOUND_DIFFWTD` and `COMPOUND_WEDGE` refusals are now FORBIDDEN |
| 4448 | /// (matches interintra's own gate). r3: `--enable-dist-wtd-comp=0` |
| 4449 | /// (forces the masked choice toward wedge when comp_group_idx==1) plus |
| 4450 | /// a hard-diagonal-edge `geq` source (wedge's OBLIQUE directions favor |
| 4451 | /// a real diagonal edge over the r2 gradients content) to fire |
| 4452 | /// COMPOUND_WEDGE live; `wedge_hits()` is soft-skipped (not hard |
| 4453 | /// asserted) on a zero-hit run -- the recipe search is not proven to |
| 4454 | /// converge every run, see charter's "if no recipe fires" fallback. |
| 4455 | #[test] |
| 4456 | fn a_real_aomenc_stream_with_masked_compound_decodes_pixel_exact() { |
| 4457 | const NAME: &str = "a_real_aomenc_stream_with_masked_compound_decodes_pixel_exact"; |
| 4458 | let _gate_lock = lock_gate_counters(); |
| 4459 | if !have_ffmpeg() { |
| 4460 | eprintln!("SKIP {NAME}: no ffmpeg"); |
| 4461 | return; |
| 4462 | } |
| 4463 | if !have_aomenc() { |
| 4464 | eprintln!("SKIP {NAME}: no aomenc at {}", aomenc_path().display()); |
| 4465 | return; |
| 4466 | } |
| 4467 | let (width, height, frame_count) = (64usize, 64usize, 24usize); |
| 4468 | let mut named_refusals = 0u32; |
| 4469 | let mut matched = 0u32; |
| 4470 | let n_attempts: u32 = std::env::var("EC_MASKCOMP_GATE_ATTEMPTS") |
| 4471 | .ok() |
| 4472 | .and_then(|s| s.parse().ok()) |
| 4473 | .unwrap_or(80); |
| 4474 | for attempt in 0..n_attempts { |
| 4475 | let seed = 42 + attempt; |
| 4476 | let duration = frame_count as f64 / 25.0; |
| 4477 | // lane-wedge r3: a hard diagonal edge (not a smooth gradient) |
| 4478 | // is the content wedge's OBLIQUE master masks are shaped for -- |
| 4479 | // `mandelbrot`'s pan position varies the edge angle/position |
| 4480 | // across attempts the way the r2 gradients seed did. |
| 4481 | let source = format!( |
| 4482 | "mandelbrot=size={width}x{height}:rate=25:start_x={sx}:start_y={sy}", |
| 4483 | sx = -0.6 + 0.005 * (attempt as f64), |
| 4484 | sy = -0.4 + 0.005 * (attempt as f64) |
| 4485 | ); |
| 4486 | let y4m = Command::new("ffmpeg") |
| 4487 | .args([ |
| 4488 | "-v", |
| 4489 | "error", |
| 4490 | "-f", |
| 4491 | "lavfi", |
| 4492 | "-i", |
| 4493 | &source, |
| 4494 | "-t", |
| 4495 | &duration.to_string(), |
| 4496 | "-pix_fmt", |
| 4497 | "yuv420p", |
| 4498 | "-f", |
| 4499 | "yuv4mpegpipe", |
| 4500 | "-", |
| 4501 | ]) |
| 4502 | .stdin(Stdio::null()) |
| 4503 | .stdout(Stdio::piped()) |
| 4504 | .stderr(Stdio::piped()) |
| 4505 | .output() |
| 4506 | .expect("ffmpeg failed to run"); |
| 4507 | assert!( |
| 4508 | y4m.status.success(), |
| 4509 | "ffmpeg fixture: {}", |
| 4510 | String::from_utf8_lossy(&y4m.stderr) |
| 4511 | ); |
| 4512 | let args: Vec<&str> = vec![ |
| 4513 | "--codec=av1", |
| 4514 | "--passes=1", |
| 4515 | "--end-usage=q", |
| 4516 | "--cq-level=45", |
| 4517 | "--cpu-used=0", |
| 4518 | "--kf-max-dist=1000", |
| 4519 | "--threads=1", |
| 4520 | "--row-mt=0", |
| 4521 | "--auto-alt-ref=1", |
| 4522 | "--lag-in-frames=16", |
| 4523 | "--enable-fwd-kf=0", |
| 4524 | "--enable-order-hint=1", |
| 4525 | "--enable-warped-motion=1", |
| 4526 | // r3: mandelbrot's pan/zoom triggers aomenc's single-ref |
| 4527 | // GLOBALMV global-motion search (a capability distinct from |
| 4528 | // and outside this lane's scope) -- off, matching this |
| 4529 | // gate's own goal of exercising masked compound, not GM. |
| 4530 | "--enable-global-motion=0", |
| 4531 | "--enable-obmc=1", |
| 4532 | "--tune-content=default", |
| 4533 | "--enable-masked-comp=1", |
| 4534 | // r3: force the masked choice toward wedge (r2 always set |
| 4535 | // this =1; distance-weighted compound competes with the |
| 4536 | // wedge/diffwtd choice for the same comp_group_idx==1 slot). |
| 4537 | "--enable-dist-wtd-comp=0", |
| 4538 | "--enable-interintra-comp=1", |
| 4539 | "--enable-onesided-comp=0", |
| 4540 | "--enable-interintra-wedge=0", |
| 4541 | "--enable-smooth-interintra=1", |
| 4542 | "--enable-rect-partitions=0", |
| 4543 | "--enable-ab-partitions=0", |
| 4544 | "--enable-1to4-partitions=0", |
| 4545 | "--enable-filter-intra=0", |
| 4546 | "--enable-smooth-intra=0", |
| 4547 | "--enable-paeth-intra=0", |
| 4548 | "--enable-directional-intra=0", |
| 4549 | "--enable-angle-delta=0", |
| 4550 | "--enable-tx-size-search=0", |
| 4551 | "--enable-cdef=0", |
| 4552 | "--enable-restoration=0", |
| 4553 | "--max-partition-size=32", |
| 4554 | "--min-partition-size=32", |
| 4555 | "--enable-palette=0", |
| 4556 | "--enable-intrabc=0", |
| 4557 | "--enable-cfl-intra=0", |
| 4558 | "--enable-ref-frame-mvs=0", |
| 4559 | "--obu", |
| 4560 | "-o", |
| 4561 | "-", |
| 4562 | "-", |
| 4563 | ]; |
| 4564 | let mut child = Command::new(aomenc_path()) |
| 4565 | .args(&args) |
| 4566 | .stdin(Stdio::piped()) |
| 4567 | .stdout(Stdio::piped()) |
| 4568 | .stderr(Stdio::piped()) |
| 4569 | .spawn() |
| 4570 | .expect("aomenc failed to start"); |
| 4571 | child |
| 4572 | .stdin |
| 4573 | .take() |
| 4574 | .expect("aomenc stdin") |
| 4575 | .write_all(&y4m.stdout) |
| 4576 | .expect("writing y4m to aomenc"); |
| 4577 | let out = child.wait_with_output().expect("aomenc failed to run"); |
| 4578 | assert!( |
| 4579 | out.status.success(), |
| 4580 | "aomenc refused the fixture: {}", |
| 4581 | String::from_utf8_lossy(&out.stderr) |
| 4582 | ); |
| 4583 | let stream = out.stdout; |
| 4584 | let frames = match decode_stream(&stream) { |
| 4585 | Err(e) => { |
| 4586 | let msg = e.to_string(); |
| 4587 | assert!( |
| 4588 | msg.contains("unsupported"), |
| 4589 | "{NAME} failed outright, not a named refusal (seed {seed}): {msg}" |
| 4590 | ); |
| 4591 | assert!( |
| 4592 | !msg.contains("warp"), |
| 4593 | "{NAME} refused on warp (seed {seed}) -- warp decode is ported: {msg}" |
| 4594 | ); |
| 4595 | assert!( |
| 4596 | !msg.contains("masked COMPOUND_REFERENCE"), |
| 4597 | "{NAME} refused a masked-compound block (seed {seed}) -- both DIFFWTD \ |
| 4598 | and WEDGE blends are ported, this refusal is forbidden: {msg}" |
| 4599 | ); |
| 4600 | assert!( |
| 4601 | !msg.contains("interintra"), |
| 4602 | "{NAME} refused on interintra (seed {seed}) -- non-wedge \ |
| 4603 | interintra is ported: {msg}" |
| 4604 | ); |
| 4605 | named_refusals += 1; |
| 4606 | eprintln!("seed {seed} refusal: {msg}"); |
| 4607 | if std::env::var_os("EC_AV1_GATE_DUMP").is_some() { |
| 4608 | // r1's self-pin is armed via EC_AV1_GATE_DUMP=<path> |
| 4609 | // only for a real pixel MISMATCH below, not for this |
| 4610 | // still-expected named refusal. |
| 4611 | } |
| 4612 | continue; |
| 4613 | } |
| 4614 | Ok(frames) => frames, |
| 4615 | }; |
| 4616 | let ffmpeg_frames = ffmpeg_decode_sequence(&stream, width, height, frame_count); |
| 4617 | assert_eq!(frames.len(), frame_count); |
| 4618 | let mismatched = frames |
| 4619 | .iter() |
| 4620 | .zip(&ffmpeg_frames) |
| 4621 | .any(|(got, want)| got.y != want.y || got.u != want.u || got.v != want.v); |
| 4622 | if mismatched && let Ok(path) = std::env::var("EC_AV1_GATE_DUMP") { |
| 4623 | std::fs::write(&path, &stream).expect("writing pinned stream"); |
| 4624 | eprintln!("EC_AV1_GATE_DUMP: wrote mismatching stream (seed {seed}) to {path}"); |
| 4625 | } |
| 4626 | for (i, (got, want)) in frames.iter().zip(&ffmpeg_frames).enumerate() { |
| 4627 | assert_eq!(got.y, want.y, "{NAME} frame {i} luma vs ffmpeg (seed {seed})"); |
| 4628 | assert_eq!(got.u, want.u, "{NAME} frame {i} U vs ffmpeg (seed {seed})"); |
| 4629 | assert_eq!(got.v, want.v, "{NAME} frame {i} V vs ffmpeg (seed {seed})"); |
| 4630 | } |
| 4631 | matched += 1; |
| 4632 | } |
| 4633 | // r1: observed flaky at 40 attempts -- aomenc's cpu-used=0 RD is |
| 4634 | // run-to-run nondeterministic in whether it ever picks |
| 4635 | // comp_group_idx == 1 for this synthetic gradients content, and |
| 4636 | // allow_screen_content_tools sometimes eats most of a small seed |
| 4637 | // window (class gate-recipe-confound / never-fired-gate, memory). |
| 4638 | // 80 attempts is the mitigation, not a fix; a still-zero run here |
| 4639 | // is that sampling flake, not a decode regression -- check |
| 4640 | // masked_compound_hits/named_refusals in the panic message before |
| 4641 | // assuming a real bug. |
| 4642 | // r2: DIFFWTD is ported and gated, so a zero-hit run is no longer |
| 4643 | // soft-skipped -- hard assert, matching the interintra gate. |
| 4644 | assert!( |
| 4645 | crate::decode::masked_compound_hits() > 0, |
| 4646 | "{NAME}: zero masked-compound blocks fired ({matched} matches, {named_refusals} \ |
| 4647 | other refusals out of {n_attempts}) -- gate proved nothing this run" |
| 4648 | ); |
| 4649 | // r3: wedge_hits() is soft-skipped, not hard-asserted -- the recipe |
| 4650 | // search (dist-wtd-comp=0 + diagonal mandelbrot content) is not |
| 4651 | // proven to fire wedge on every run; charter's fallback is to land |
| 4652 | // the checksum-verified codebook + blend with this soft path. |
| 4653 | if crate::decode::wedge_hits() == 0 { |
| 4654 | eprintln!( |
| 4655 | "{NAME}: WARNING wedge_hits()==0 this run -- COMPOUND_WEDGE never fired \ |
| 4656 | ({matched} matches, {named_refusals} refusals out of {n_attempts}); codebook \ |
| 4657 | is checksum-verified but unexercised live this run" |
| 4658 | ); |
| 4659 | } |
| 4660 | eprintln!( |
| 4661 | "{NAME}: {named_refusals} other-capability refusals, {matched} pixel-exact matches \ |
| 4662 | out of {n_attempts}, masked_compound_hits={} wedge_hits={}", |
| 4663 | crate::decode::masked_compound_hits(), |
| 4664 | crate::decode::wedge_hits() |
| 4665 | ); |
| 4666 | } |
| 4667 | |
| 4668 | /// Pinned streams captured by `EC_AV1_GATE_DUMP` off |
| 4669 | /// `a_real_aomenc_stream_with_warped_motion_refuses_or_matches` -- each |
| 4670 | /// one is a former mismatch, kept as a regression pin (warp-mismatch: |
| 4671 | /// the WARPED_CAUSAL interp-read suppression; warp-flake-7: the same |
| 4672 | /// suppression for skip_mode blocks; warp-flake-5: mvstack entry |
| 4673 | /// clamping; ii-flake-1..8: interintra neighbours excluded from |
| 4674 | /// warp-sample gathering, ref_frame[1] == INTRA_FRAME in the mi grid). |
| 4675 | /// `EC_AV1_GATE_DUMP_PIN` overrides to a single stream. |
| 4676 | /// Deterministic and static -- `#[ignore]`d because the gitignored |
| 4677 | /// fixtures dir may be absent; run manually. |
| 4678 | #[test] |
| 4679 | #[ignore = "reads pinned fixture paths under the gitignored fixtures dir; run manually"] |
| 4680 | fn pinned_warp_stream_decodes_pixel_exact() { |
| 4681 | if !have_ffmpeg() { |
| 4682 | eprintln!("SKIP pinned_warp_stream_decodes_pixel_exact: no ffmpeg"); |
| 4683 | return; |
| 4684 | } |
| 4685 | let fixtures = concat!(env!("CARGO_MANIFEST_DIR"), "/../../fixtures"); |
| 4686 | let paths: Vec<String> = match std::env::var("EC_AV1_GATE_DUMP_PIN") { |
| 4687 | Ok(p) => vec![p], |
| 4688 | Err(_) => [ |
| 4689 | "warp-mismatch", |
| 4690 | "warp-flake-5", |
| 4691 | "warp-flake-7", |
| 4692 | "ii-flake-1", |
| 4693 | "ii-flake-2", |
| 4694 | "ii-flake-3", |
| 4695 | "ii-flake-5", |
| 4696 | "ii-flake-6", |
| 4697 | "ii-flake-7", |
| 4698 | "ii-flake-8", |
| 4699 | // switchable_interp missing from reset_counts (counter |
| 4700 | // saturation slowed the adaptation rate) -- unrelated to |
| 4701 | // interintra, caught by the same gate. |
| 4702 | "ii-flake-9", |
| 4703 | // lane-rect r2: HORZ strip rect defects (mvstack size_h, |
| 4704 | // warp sample/projection dims, OBMC overlap, deblock tx-h). |
| 4705 | "rect-flake-1", |
| 4706 | // scan_row/scan_col weight `inc` min'd by the wrong candidate |
| 4707 | // axis (width vs height) -- ties reordered DRL entry 1 for a |
| 4708 | // block under a 32x16 strip, and suppressed the -5 extended |
| 4709 | // row scan. |
| 4710 | "rect-flake-2", |
| 4711 | // overlappable_left stepped the vertical walk by the |
| 4712 | // neighbour's WIDTH: a 32x16 left strip swallowed the strip |
| 4713 | // below it, blending the wrong OBMC prediction there. |
| 4714 | "rect-flake-3", |
| 4715 | ] |
| 4716 | .iter() |
| 4717 | .map(|n| format!("{fixtures}/{n}.obu")) |
| 4718 | .collect(), |
| 4719 | }; |
| 4720 | for path in paths { |
| 4721 | eprintln!("pin: {path}"); |
| 4722 | check_pinned_warp_stream(&path); |
| 4723 | } |
| 4724 | } |
| 4725 | |
| 4726 | fn check_pinned_warp_stream(path: &str) { |
| 4727 | use crate::decode::warp_selected_hits; |
| 4728 | let stream = std::fs::read(path).expect("reading pinned stream"); |
| 4729 | let before = warp_selected_hits(); |
| 4730 | let frames = decode_stream(&stream).expect("pinned stream must decode"); |
| 4731 | eprintln!( |
| 4732 | "warp_selected_hits before={before} after={}", |
| 4733 | warp_selected_hits() |
| 4734 | ); |
| 4735 | let ffmpeg_frames = ffmpeg_decode_sequence(&stream, 64, 64, 24); |
| 4736 | for (i, (got, want)) in frames.iter().zip(&ffmpeg_frames).enumerate() { |
| 4737 | if got.y != want.y { |
| 4738 | for row in 0..64 { |
| 4739 | let mut line = String::new(); |
| 4740 | for col in 0..64 { |
| 4741 | let d = got.y[row * 64 + col] as i32 - want.y[row * 64 + col] as i32; |
| 4742 | line.push(if d == 0 { |
| 4743 | '.' |
| 4744 | } else if d.abs() < 4 { |
| 4745 | 'o' |
| 4746 | } else { |
| 4747 | 'X' |
| 4748 | }); |
| 4749 | } |
| 4750 | eprintln!("{line}"); |
| 4751 | } |
| 4752 | } |
| 4753 | assert_eq!(got.y, want.y, "frame {i} luma vs ffmpeg (pinned)"); |
| 4754 | assert_eq!(got.u, want.u, "frame {i} U vs ffmpeg (pinned)"); |
| 4755 | assert_eq!(got.v, want.v, "frame {i} V vs ffmpeg (pinned)"); |
| 4756 | } |
| 4757 | } |
| 4758 | |
| 4759 | /// `LAST2_FRAME`/`LAST3_FRAME` are just older slots in the same forward |
| 4760 | /// reference-buffer ring `LAST_FRAME`/`GOLDEN_FRAME` already prove -- |
| 4761 | /// dropping the golden gate's `--max-reference-frames=3 |
| 4762 | /// --reduced-reference-set=1` narrowing (which suppresses exactly these) |
| 4763 | /// and running long enough for aomenc's own buffer rotation to reach |
| 4764 | /// past the first 2 frames is the whole difference. |
| 4765 | #[test] |
| 4766 | fn a_real_aomenc_stream_with_a_last2_reference_decodes_pixel_exact() { |
| 4767 | a_real_aomenc_single_ref_gate( |
| 4768 | "a_real_aomenc_stream_with_a_last2_reference_decodes_pixel_exact", |
| 4769 | crate::mvstack::LAST2_FRAME, |
| 4770 | "LAST2_FRAME", |
| 4771 | &["--auto-alt-ref=0", "--lag-in-frames=0"], |
| 4772 | 64, |
| 4773 | 64, |
| 4774 | 8, |
| 4775 | 120, |
| 4776 | ); |
| 4777 | } |
| 4778 | |
| 4779 | #[test] |
| 4780 | fn a_real_aomenc_stream_with_a_last3_reference_decodes_pixel_exact() { |
| 4781 | a_real_aomenc_single_ref_gate( |
| 4782 | "a_real_aomenc_stream_with_a_last3_reference_decodes_pixel_exact", |
| 4783 | crate::mvstack::LAST3_FRAME, |
| 4784 | "LAST3_FRAME", |
| 4785 | &["--auto-alt-ref=0", "--lag-in-frames=0"], |
| 4786 | 64, |
| 4787 | 64, |
| 4788 | 8, |
| 4789 | 120, |
| 4790 | ); |
| 4791 | } |
| 4792 | |
| 4793 | /// `BWDREF`/`ALTREF2`/`ALTREF` are all backward (display-order-forward) |
| 4794 | /// references that only exist once aomenc builds a hierarchical GOP -- |
| 4795 | /// `--auto-alt-ref=1 --lag-in-frames` (need at least one full mini-GOP |
| 4796 | /// of lookahead) is what makes aomenc code them at all; without it |
| 4797 | /// every attempt would hit this decoder's own real, still-open |
| 4798 | /// per-block sign-bias gap by never drawing the reference in the first |
| 4799 | /// place, not by proving it correct. |
| 4800 | #[test] |
| 4801 | fn a_real_aomenc_stream_with_a_bwdref_reference_decodes_pixel_exact() { |
| 4802 | a_real_aomenc_single_ref_gate( |
| 4803 | "a_real_aomenc_stream_with_a_bwdref_reference_decodes_pixel_exact", |
| 4804 | crate::mvstack::BWDREF_FRAME, |
| 4805 | "BWDREF_FRAME", |
| 4806 | &[ |
| 4807 | "--auto-alt-ref=1", |
| 4808 | "--lag-in-frames=16", |
| 4809 | "--enable-fwd-kf=0", |
| 4810 | ], |
| 4811 | 64, |
| 4812 | 64, |
| 4813 | 16, |
| 4814 | 120, |
| 4815 | ); |
| 4816 | } |
| 4817 | |
| 4818 | /// lane-altref2 r1: 16 frames was too short for aomenc to ever build a |
| 4819 | /// second-level alt-ref pyramid, so this gate ran 86 attempts and fired |
| 4820 | /// ALTREF2 exactly zero times (class `gate-blind-to-feature`) -- the |
| 4821 | /// flags were already right, only the clip length was wrong. A 64-frame |
| 4822 | /// clip at the same `--lag-in-frames=16` gives aomenc enough lookahead |
| 4823 | /// to schedule a 2-level GF pyramid and reliably codes ALTREF2 (4/4 |
| 4824 | /// hits on a plain seed-42 direct-aomenc probe; see |
| 4825 | /// lanes/altref2-r1.report.md for the recipe search table). Firing is |
| 4826 | /// now HARD-asserted below: the gate fails rather than skips if |
| 4827 | /// `ref_hits(ALTREF2_FRAME)` does not advance. |
| 4828 | #[test] |
| 4829 | fn a_real_aomenc_stream_with_an_altref2_reference_decodes_pixel_exact() { |
| 4830 | let before = decode::ref_hits(crate::mvstack::ALTREF2_FRAME); |
| 4831 | a_real_aomenc_single_ref_gate( |
| 4832 | "a_real_aomenc_stream_with_an_altref2_reference_decodes_pixel_exact", |
| 4833 | crate::mvstack::ALTREF2_FRAME, |
| 4834 | "ALTREF2_FRAME", |
| 4835 | &[ |
| 4836 | "--auto-alt-ref=1", |
| 4837 | "--lag-in-frames=16", |
| 4838 | "--enable-fwd-kf=0", |
| 4839 | ], |
| 4840 | 64, |
| 4841 | 64, |
| 4842 | 64, |
| 4843 | 120, |
| 4844 | ); |
| 4845 | let after = decode::ref_hits(crate::mvstack::ALTREF2_FRAME); |
| 4846 | assert!( |
| 4847 | after > before, |
| 4848 | "a_real_aomenc_stream_with_an_altref2_reference_decodes_pixel_exact: \ |
| 4849 | ALTREF2_FRAME never fired across 120 attempts -- the gate is vacuous \ |
| 4850 | again (class gate-blind-to-feature); see lanes/altref2-r1.report.md \ |
| 4851 | for the recipe that is supposed to make this fire" |
| 4852 | ); |
| 4853 | } |
| 4854 | |
| 4855 | #[test] |
| 4856 | fn a_real_aomenc_stream_with_an_altref_reference_decodes_pixel_exact() { |
| 4857 | a_real_aomenc_single_ref_gate( |
| 4858 | "a_real_aomenc_stream_with_an_altref_reference_decodes_pixel_exact", |
| 4859 | crate::mvstack::ALTREF_FRAME, |
| 4860 | "ALTREF_FRAME", |
| 4861 | &[ |
| 4862 | "--auto-alt-ref=1", |
| 4863 | "--lag-in-frames=16", |
| 4864 | "--enable-fwd-kf=0", |
| 4865 | ], |
| 4866 | 64, |
| 4867 | 64, |
| 4868 | 16, |
| 4869 | 120, |
| 4870 | ); |
| 4871 | } |
| 4872 | |
| 4873 | /// lane-av1tmvp: the temporal-MV gate -- unlike every `_single_ref_gate` |
| 4874 | /// attempt above, this one leaves `--enable-ref-frame-mvs` at aomenc's |
| 4875 | /// own default (on) instead of pinning it off, so `use_ref_frame_mvs` |
| 4876 | /// actually fires and `decode::tmv_hits()` (this gate's own capability |
| 4877 | /// proof, not just `non_last_ref_hits`) has to advance for the run to |
| 4878 | /// count as a real pass rather than a `never_fired` skip. |
| 4879 | fn a_real_aomenc_temporal_mv_gate(seed_base: u32, attempts: u32) { |
| 4880 | let gate_name = "a_real_aomenc_temporal_mv_gate"; |
| 4881 | let _gate_lock = lock_gate_counters(); |
| 4882 | if !have_ffmpeg() { |
| 4883 | eprintln!("SKIP {gate_name}: no ffmpeg"); |
| 4884 | return; |
| 4885 | } |
| 4886 | if !have_aomenc() { |
| 4887 | eprintln!("SKIP {gate_name}: no aomenc at {}", aomenc_path().display()); |
| 4888 | return; |
| 4889 | } |
| 4890 | let (width, height, frame_count) = (64usize, 64usize, 16usize); |
| 4891 | let mut never_fired = 0u32; |
| 4892 | let mut refusals = Vec::new(); |
| 4893 | for attempt in 0..attempts { |
| 4894 | let seed = seed_base + attempt; |
| 4895 | let duration = frame_count as f64 / 25.0; |
| 4896 | let source = |
| 4897 | gradients_source(seed, width, height, &format!("duration={duration}:rate=25")); |
| 4898 | let y4m = Command::new("ffmpeg") |
| 4899 | .args([ |
| 4900 | "-v", |
| 4901 | "error", |
| 4902 | "-f", |
| 4903 | "lavfi", |
| 4904 | "-i", |
| 4905 | &source, |
| 4906 | "-pix_fmt", |
| 4907 | "yuv420p", |
| 4908 | "-f", |
| 4909 | "yuv4mpegpipe", |
| 4910 | "-", |
| 4911 | ]) |
| 4912 | .stdin(Stdio::null()) |
| 4913 | .stdout(Stdio::piped()) |
| 4914 | .stderr(Stdio::piped()) |
| 4915 | .output() |
| 4916 | .expect("ffmpeg failed to run"); |
| 4917 | assert!( |
| 4918 | y4m.status.success(), |
| 4919 | "ffmpeg fixture: {}", |
| 4920 | String::from_utf8_lossy(&y4m.stderr) |
| 4921 | ); |
| 4922 | // Same envelope as `a_real_aomenc_single_ref_gate`, minus its |
| 4923 | // `--enable-ref-frame-mvs=0` pin -- everything else this decoder |
| 4924 | // still cannot code stays refused the same way. |
| 4925 | let args: Vec<&str> = vec![ |
| 4926 | "--codec=av1", |
| 4927 | "--passes=1", |
| 4928 | "--end-usage=q", |
| 4929 | "--cq-level=55", |
| 4930 | "--cpu-used=0", |
| 4931 | "--kf-max-dist=1000", |
| 4932 | "--threads=1", |
| 4933 | "--row-mt=0", |
| 4934 | "--auto-alt-ref=1", |
| 4935 | "--lag-in-frames=16", |
| 4936 | "--enable-fwd-kf=0", |
| 4937 | "--enable-warped-motion=0", |
| 4938 | "--enable-obmc=0", |
| 4939 | "--enable-masked-comp=0", |
| 4940 | "--enable-interintra-comp=0", |
| 4941 | "--enable-dist-wtd-comp=0", |
| 4942 | "--enable-diff-wtd-comp=0", |
| 4943 | "--enable-onesided-comp=0", |
| 4944 | "--enable-interintra-wedge=0", |
| 4945 | "--enable-smooth-interintra=0", |
| 4946 | "--enable-rect-partitions=0", |
| 4947 | "--enable-ab-partitions=0", |
| 4948 | "--enable-1to4-partitions=0", |
| 4949 | "--enable-filter-intra=0", |
| 4950 | "--enable-smooth-intra=0", |
| 4951 | "--enable-paeth-intra=0", |
| 4952 | "--enable-directional-intra=0", |
| 4953 | "--enable-angle-delta=0", |
| 4954 | "--enable-tx-size-search=0", |
| 4955 | "--enable-cdef=0", |
| 4956 | "--enable-restoration=0", |
| 4957 | "--max-partition-size=32", |
| 4958 | "--min-partition-size=32", |
| 4959 | "--enable-palette=0", |
| 4960 | "--enable-intrabc=0", |
| 4961 | "--enable-cfl-intra=0", |
| 4962 | "--obu", |
| 4963 | "-o", |
| 4964 | "-", |
| 4965 | "-", |
| 4966 | ]; |
| 4967 | let mut child = Command::new(aomenc_path()) |
| 4968 | .args(&args) |
| 4969 | .stdin(Stdio::piped()) |
| 4970 | .stdout(Stdio::piped()) |
| 4971 | .stderr(Stdio::piped()) |
| 4972 | .spawn() |
| 4973 | .expect("aomenc failed to start"); |
| 4974 | child |
| 4975 | .stdin |
| 4976 | .take() |
| 4977 | .expect("aomenc stdin") |
| 4978 | .write_all(&y4m.stdout) |
| 4979 | .expect("writing y4m to aomenc"); |
| 4980 | let out = child.wait_with_output().expect("aomenc failed to run"); |
| 4981 | assert!( |
| 4982 | out.status.success(), |
| 4983 | "aomenc refused the fixture: {}", |
| 4984 | String::from_utf8_lossy(&out.stderr) |
| 4985 | ); |
| 4986 | let stream = out.stdout; |
| 4987 | let before = decode::tmv_hits(); |
| 4988 | let frames = match decode_stream(&stream) { |
| 4989 | Err(e) => { |
| 4990 | let msg = e.to_string(); |
| 4991 | assert!( |
| 4992 | msg.contains("unsupported"), |
| 4993 | "{gate_name} failed outright (seed {seed}): {msg}" |
| 4994 | ); |
| 4995 | refusals.push(format!("seed {seed}: {msg}")); |
| 4996 | continue; |
| 4997 | } |
| 4998 | Ok(frames) => frames, |
| 4999 | }; |
| 5000 | if decode::tmv_hits() == before { |
| 5001 | never_fired += 1; |
| 5002 | continue; |
| 5003 | } |
| 5004 | let ffmpeg_frames = ffmpeg_decode_sequence(&stream, width, height, frame_count); |
| 5005 | assert_eq!(frames.len(), frame_count); |
| 5006 | if let Ok(path) = std::env::var("EC_AV1_GATE_DUMP") { |
| 5007 | let mismatched = frames |
| 5008 | .iter() |
| 5009 | .zip(&ffmpeg_frames) |
| 5010 | .any(|(got, want)| got.y != want.y || got.u != want.u || got.v != want.v); |
| 5011 | if mismatched { |
| 5012 | std::fs::write(&path, &stream).expect("writing pinned stream"); |
| 5013 | eprintln!("EC_AV1_GATE_DUMP: wrote mismatching stream (seed {seed}) to {path}"); |
| 5014 | } |
| 5015 | } |
| 5016 | for (i, (got, want)) in frames.iter().zip(&ffmpeg_frames).enumerate() { |
| 5017 | assert_eq!( |
| 5018 | got.y, want.y, |
| 5019 | "{gate_name} frame {i} luma vs ffmpeg (seed {seed})" |
| 5020 | ); |
| 5021 | assert_eq!( |
| 5022 | got.u, want.u, |
| 5023 | "{gate_name} frame {i} U vs ffmpeg (seed {seed})" |
| 5024 | ); |
| 5025 | assert_eq!( |
| 5026 | got.v, want.v, |
| 5027 | "{gate_name} frame {i} V vs ffmpeg (seed {seed})" |
| 5028 | ); |
| 5029 | } |
| 5030 | eprintln!( |
| 5031 | "{gate_name} FIRING seed {seed}: tmv_hits advanced by {}", |
| 5032 | decode::tmv_hits() - before |
| 5033 | ); |
| 5034 | return; |
| 5035 | } |
| 5036 | eprintln!( |
| 5037 | "SKIP {gate_name}: {never_fired} attempts decoded but never fired a temporal MV \ |
| 5038 | candidate, and every other attempt hit a named refusal:\n{}", |
| 5039 | refusals.join("\n") |
| 5040 | ); |
| 5041 | } |
| 5042 | |
| 5043 | #[test] |
| 5044 | fn a_real_aomenc_stream_with_temporal_mvs_decodes_pixel_exact() { |
| 5045 | a_real_aomenc_temporal_mv_gate(42, 40); |
| 5046 | } |
| 5047 | |
| 5048 | /// bisect scratch: decode a pinned stream exactly once -- generic |
| 5049 | /// pin-repro tool, kept for manual use. |
| 5050 | #[test] |
| 5051 | #[ignore] |
| 5052 | fn scratch_decode_pinned_stream_once() { |
| 5053 | let path = std::env::var("EC_AV1_PIN").expect("set EC_AV1_PIN to the .obu path"); |
| 5054 | let stream = std::fs::read(&path).expect("read pinned stream"); |
| 5055 | match decode_stream(&stream) { |
| 5056 | Ok(frames) => eprintln!("OK: {} frames", frames.len()), |
| 5057 | Err(e) => eprintln!("ERR: {e}"), |
| 5058 | } |
| 5059 | } |
| 5060 | |
| 5061 | /// scratch: isolate a pinned mismatching stream's first divergent pixel. |
| 5062 | #[test] |
| 5063 | #[ignore] |
| 5064 | fn scratch_isolate_pinned_mismatch() { |
| 5065 | let path = std::env::var("EC_AV1_PIN").expect("set EC_AV1_PIN to the .obu path"); |
| 5066 | let stream = std::fs::read(&path).expect("read pinned stream"); |
| 5067 | { |
| 5068 | let mut p = Av1Parser::new(); |
| 5069 | let mut pos = 0usize; |
| 5070 | while pos < stream.len() { |
| 5071 | let obu = p.parse_obu(&stream[pos..]).expect("parse"); |
| 5072 | pos += obu.total_size; |
| 5073 | if let ObuKind::Frame(header, _) = obu.kind { |
| 5074 | eprintln!("interpolation_filter: {:?}", header.interpolation_filter); |
| 5075 | eprintln!("cdef: {:?}", header.cdef); |
| 5076 | eprintln!("quantization: {:?}", header.quantization); |
| 5077 | eprintln!( |
| 5078 | "coded_lossless: {:?} tx_mode: {:?}", |
| 5079 | header.coded_lossless, header.tx_mode |
| 5080 | ); |
| 5081 | eprintln!("delta: {:?}", header.delta); |
| 5082 | eprintln!("segmentation: {:?}", header.segmentation); |
| 5083 | eprintln!("loop_restoration: {:?}", header.loop_restoration); |
| 5084 | eprintln!( |
| 5085 | "use_128x128_superblock: {:?} disable_cdf_update: {:?} disable_frame_end_update_cdf: {:?} primary_ref_frame: {:?} allow_high_precision_mv: {:?} force_integer_mv: {:?}", |
| 5086 | p.sequence_header().map(|s| s.use_128x128_superblock), |
| 5087 | header.disable_cdf_update, |
| 5088 | header.disable_frame_end_update_cdf, |
| 5089 | header.primary_ref_frame, |
| 5090 | header.allow_high_precision_mv, |
| 5091 | header.force_integer_mv, |
| 5092 | ); |
| 5093 | } |
| 5094 | } |
| 5095 | } |
| 5096 | let width = std::env::var("EC_AV1_PIN_W") |
| 5097 | .map(|s| s.parse().unwrap()) |
| 5098 | .unwrap_or(64); |
| 5099 | let height = std::env::var("EC_AV1_PIN_H") |
| 5100 | .map(|s| s.parse().unwrap()) |
| 5101 | .unwrap_or(64); |
| 5102 | let frame_count = std::env::var("EC_AV1_PIN_N") |
| 5103 | .map(|s| s.parse().unwrap()) |
| 5104 | .unwrap_or(4); |
| 5105 | let frames = decode_stream(&stream).expect("our decode"); |
| 5106 | let ffmpeg_frames = ffmpeg_decode_sequence(&stream, width, height, frame_count); |
| 5107 | for f in 0..frame_count.min(frames.len()).min(ffmpeg_frames.len()) { |
| 5108 | let ours = &frames[f]; |
| 5109 | let theirs = &ffmpeg_frames[f]; |
| 5110 | for (plane_name, a, b, w) in [ |
| 5111 | ("y", &ours.y, &theirs.y, width), |
| 5112 | ("u", &ours.u, &theirs.u, width / 2), |
| 5113 | ("v", &ours.v, &theirs.v, width / 2), |
| 5114 | ] { |
| 5115 | let mut first = None; |
| 5116 | let mut count = 0; |
| 5117 | let mut worst = 0i32; |
| 5118 | for (i, (x, y)) in a.iter().zip(b.iter()).enumerate() { |
| 5119 | if x != y { |
| 5120 | count += 1; |
| 5121 | if first.is_none() { |
| 5122 | first = Some(i); |
| 5123 | } |
| 5124 | worst = worst.max((*x as i32 - *y as i32).abs()); |
| 5125 | } |
| 5126 | } |
| 5127 | if let Some(i) = first { |
| 5128 | eprintln!( |
| 5129 | "frame {f} plane {plane_name}: {count} mismatches (worst delta {worst}), first at offset {i} (row {}, col {}) ours={} theirs={}", |
| 5130 | i / w, |
| 5131 | i % w, |
| 5132 | a[i], |
| 5133 | b[i] |
| 5134 | ); |
| 5135 | } else { |
| 5136 | eprintln!("frame {f} plane {plane_name}: MATCH"); |
| 5137 | } |
| 5138 | } |
| 5139 | } |
| 5140 | } |
| 5141 | |
| 5142 | /// Final round of the TX4/tx-class lane's own gate: `--min-partition-size=8 |
| 5143 | /// --max-partition-size=8` pins every leaf to exactly 8x8 (spec |
| 5144 | /// 5.11.4's own boundary carve-out aside, which a multiple-of-8 frame |
| 5145 | /// never triggers), so a real `aomenc` stream from this recipe can only |
| 5146 | /// legitimately read `PARTITION_NONE` at every `bsize=3` node -- any |
| 5147 | /// other value this decoder sees there is a genuine desync, not a |
| 5148 | /// "partition below 8x8" the encoder actually wrote (traced live against |
| 5149 | /// a debug `aomdec` 2026-08-27: the true bitstream's own 8x8 partition |
| 5150 | /// reads are all `NONE`; the mismatch was `base_ctx`'s DC-position |
| 5151 | /// short-circuit firing for `TX_CLASS_HORIZ`/`VERT` too, off by one |
| 5152 | /// `BR` read into a spurious extra Golomb call). The sinusoidal-stripe |
| 5153 | /// `geq` source (a smooth luma ramp, not the sharp two-tone stripes that |
| 5154 | /// tripped `aomenc`'s screen-content-tools auto-detect) is what actually |
| 5155 | /// resolves `V_DCT`/`H_DCT` (`TxClass::of`) on some 8x8 TUs at this |
| 5156 | /// content's frequency; the attempt loop moves on from named refusals |
| 5157 | /// (`allow_screen_content_tools`, angle-delta) same as every other |
| 5158 | /// multi-seed recipe in this file, and only counts a run as a genuine |
| 5159 | /// firing when [`decode::tx_class1_hits`] actually moved. |
| 5160 | #[test] |
| 5161 | fn a_real_aomenc_min8_stream_with_tx_class1_decodes_pixel_exact() { |
| 5162 | if !have_ffmpeg() { |
| 5163 | eprintln!( |
| 5164 | "SKIP a_real_aomenc_min8_stream_with_tx_class1_decodes_pixel_exact: no ffmpeg" |
| 5165 | ); |
| 5166 | return; |
| 5167 | } |
| 5168 | if !have_aomenc() { |
| 5169 | eprintln!( |
| 5170 | "SKIP a_real_aomenc_min8_stream_with_tx_class1_decodes_pixel_exact: no aomenc at {}", |
| 5171 | aomenc_path().display() |
| 5172 | ); |
| 5173 | return; |
| 5174 | } |
| 5175 | let (width, height) = (64usize, 64usize); |
| 5176 | let mut refusals = Vec::new(); |
| 5177 | let mut fired_runs = 0u32; |
| 5178 | for attempt in 0..60u32 { |
| 5179 | let cq = 20 + (attempt % 5) * 10; |
| 5180 | let period = [4u32, 6, 8, 12, 16][(attempt as usize / 5) % 5]; |
| 5181 | let y4m = Command::new("ffmpeg") |
| 5182 | .args([ |
| 5183 | "-v", |
| 5184 | "error", |
| 5185 | "-f", |
| 5186 | "lavfi", |
| 5187 | "-i", |
| 5188 | "testsrc2=size=64x64:rate=25", |
| 5189 | "-vf", |
| 5190 | &format!( |
| 5191 | "geq=lum='128+80*sin(2*PI*Y/{period})':cb=128:cr=128,noise=alls=6:allf=t" |
| 5192 | ), |
| 5193 | "-pix_fmt", |
| 5194 | "yuv420p", |
| 5195 | "-t", |
| 5196 | "0.04", |
| 5197 | "-f", |
| 5198 | "yuv4mpegpipe", |
| 5199 | "-", |
| 5200 | ]) |
| 5201 | .stdin(Stdio::null()) |
| 5202 | .stdout(Stdio::piped()) |
| 5203 | .stderr(Stdio::piped()) |
| 5204 | .output() |
| 5205 | .expect("ffmpeg failed to run"); |
| 5206 | assert!( |
| 5207 | y4m.status.success(), |
| 5208 | "ffmpeg fixture: {}", |
| 5209 | String::from_utf8_lossy(&y4m.stderr) |
| 5210 | ); |
| 5211 | let mut child = Command::new(aomenc_path()) |
| 5212 | .args([ |
| 5213 | "--codec=av1", |
| 5214 | "--passes=1", |
| 5215 | "--end-usage=q", |
| 5216 | &format!("--cq-level={cq}"), |
| 5217 | "--cpu-used=0", |
| 5218 | "--enable-directional-intra=0", |
| 5219 | "--enable-smooth-intra=0", |
| 5220 | "--enable-paeth-intra=0", |
| 5221 | "--enable-rect-partitions=0", |
| 5222 | "--enable-angle-delta=0", |
| 5223 | "--enable-palette=0", |
| 5224 | "--reduced-tx-type-set=0", |
| 5225 | "--enable-restoration=0", |
| 5226 | "--enable-cdef=0", |
| 5227 | "--loopfilter-control=0", |
| 5228 | "--min-partition-size=8", |
| 5229 | "--max-partition-size=8", |
| 5230 | "--enable-ab-partitions=0", |
| 5231 | "--enable-1to4-partitions=0", |
| 5232 | "--enable-filter-intra=0", |
| 5233 | "--enable-intrabc=0", |
| 5234 | "--enable-cfl-intra=0", |
| 5235 | // use_ref_frame_mvs (temporal MV projection) is unimplemented in mvstack; |
| 5236 | // leaving it on silently desyncs symbols on inter frames (grainfix lesson). |
| 5237 | "--enable-ref-frame-mvs=0", |
| 5238 | "--tune-content=default", |
| 5239 | "--obu", |
| 5240 | "-o", |
| 5241 | "-", |
| 5242 | "-", |
| 5243 | ]) |
| 5244 | .stdin(Stdio::piped()) |
| 5245 | .stdout(Stdio::piped()) |
| 5246 | .stderr(Stdio::piped()) |
| 5247 | .spawn() |
| 5248 | .expect("aomenc failed to start"); |
| 5249 | child |
| 5250 | .stdin |
| 5251 | .take() |
| 5252 | .expect("aomenc stdin") |
| 5253 | .write_all(&y4m.stdout) |
| 5254 | .expect("writing y4m to aomenc"); |
| 5255 | let out = child.wait_with_output().expect("aomenc failed to run"); |
| 5256 | if !out.status.success() { |
| 5257 | refusals.push(format!( |
| 5258 | "cq={cq} period={period}: aomenc itself refused the fixture" |
| 5259 | )); |
| 5260 | continue; |
| 5261 | } |
| 5262 | let stream = out.stdout; |
| 5263 | let before = decode::tx_class1_hits(); |
| 5264 | let frames = match decode_stream(&stream) { |
| 5265 | Ok(frames) => frames, |
| 5266 | Err(e) => { |
| 5267 | refusals.push(format!("cq={cq} period={period}: {e}")); |
| 5268 | continue; |
| 5269 | } |
| 5270 | }; |
| 5271 | if decode::tx_class1_hits() == before { |
| 5272 | refusals.push(format!( |
| 5273 | "cq={cq} period={period}: decoded, but no block read a V_DCT/H_DCT tx_type" |
| 5274 | )); |
| 5275 | continue; |
| 5276 | } |
| 5277 | let ffmpeg_frames = ffmpeg_decode_sequence(&stream, width, height, 1); |
| 5278 | assert_eq!( |
| 5279 | frames[0].y, ffmpeg_frames[0].y, |
| 5280 | "luma vs ffmpeg (cq={cq} period={period})" |
| 5281 | ); |
| 5282 | assert_eq!( |
| 5283 | frames[0].u, ffmpeg_frames[0].u, |
| 5284 | "U vs ffmpeg (cq={cq} period={period})" |
| 5285 | ); |
| 5286 | assert_eq!( |
| 5287 | frames[0].v, ffmpeg_frames[0].v, |
| 5288 | "V vs ffmpeg (cq={cq} period={period})" |
| 5289 | ); |
| 5290 | fired_runs += 1; |
| 5291 | if fired_runs >= 4 { |
| 5292 | return; |
| 5293 | } |
| 5294 | } |
| 5295 | panic!( |
| 5296 | "fewer than 4 firing+pixel-exact runs out of 60 attempts:\n{}", |
| 5297 | refusals.join("\n") |
| 5298 | ); |
| 5299 | } |
| 5300 | |
| 5301 | /// lane-av1golden7 r9's decisive fixture: a real aomenc stream (seed 59, |
| 5302 | /// `--tune-content=film`, GOLDEN_FRAME firing downstream) whose frame-0 |
| 5303 | /// *intra keyframe* mismatched ffmpeg on 3855/4096 luma pixels -- traced |
| 5304 | /// to `apply_grain=true` (spec 7.18.3 grain synthesis, now implemented in |
| 5305 | /// `crate::film_grain`), entirely unrelated to GOLDEN_FRAME or frame |
| 5306 | /// ordering. Frames beyond 0 in this fixture hit an unrelated, pre-existing |
| 5307 | /// gap (`decode_inter_block`'s GOLDEN_FRAME MC path, lane-av1golden7's own |
| 5308 | /// `golden7-forwarding-mismatch.obu`), so this test truncates the stream to |
| 5309 | /// the sequence header + frame-0 OBUs only -- exactly the span the |
| 5310 | /// mismatch was traced to -- rather than reaching into `decode.rs`/`mc.rs` |
| 5311 | /// to widen MC support, which is out of this lane's scope. |
| 5312 | #[test] |
| 5313 | fn a_real_aomenc_stream_with_film_grain_decodes_pixel_exact() { |
| 5314 | if !have_ffmpeg() { |
| 5315 | eprintln!("SKIP a_real_aomenc_stream_with_film_grain_decodes_pixel_exact: no ffmpeg"); |
| 5316 | return; |
| 5317 | } |
| 5318 | let data = std::fs::read(concat!( |
| 5319 | env!("CARGO_MANIFEST_DIR"), |
| 5320 | "/../../fixtures/golden6-mismatch.obu" |
| 5321 | )) |
| 5322 | .unwrap(); |
| 5323 | // Truncate right after the first `Frame` OBU (the key frame): every |
| 5324 | // byte up to there is sequence header + frame-0, which is a complete, |
| 5325 | // independently decodable single-frame stream. |
| 5326 | let mut parser = Av1Parser::new(); |
| 5327 | let mut pos = 0usize; |
| 5328 | let mut frame0_end = None; |
| 5329 | while pos < data.len() { |
| 5330 | let obu = parser.parse_obu(&data[pos..]).unwrap(); |
| 5331 | pos += obu.total_size; |
| 5332 | if matches!(obu.kind, ObuKind::Frame(..)) { |
| 5333 | frame0_end = Some(pos); |
| 5334 | break; |
| 5335 | } |
| 5336 | } |
| 5337 | let data = &data[..frame0_end.expect("fixture has at least one frame OBU")]; |
| 5338 | let (width, height) = (64usize, 64usize); |
| 5339 | let ffmpeg_frames = ffmpeg_decode_sequence(data, width, height, 1); |
| 5340 | let decoded = decode_stream(data).unwrap(); |
| 5341 | assert_eq!(decoded.len(), 1); |
| 5342 | let (got, want) = (&decoded[0], &ffmpeg_frames[0]); |
| 5343 | assert_eq!(got.y, want.y, "frame 0 luma vs ffmpeg (film grain)"); |
| 5344 | assert_eq!(got.u, want.u, "frame 0 U vs ffmpeg (film grain)"); |
| 5345 | assert_eq!(got.v, want.v, "frame 0 V vs ffmpeg (film grain)"); |
| 5346 | } |
| 5347 | |
| 5348 | /// A real-encoder sweep, not just the one pinned fixture above |
| 5349 | /// (fixture-proves-symbol-not-signal class): several fresh |
| 5350 | /// `--tune-content=film` draws at different seeds/content, each decoded |
| 5351 | /// pixel-exact against ffmpeg's own AV1 decoder (which synthesizes grain |
| 5352 | /// by default -- confirmed via `ffmpeg -h decoder=av1`, no |
| 5353 | /// `-export_side_data`/grain-disable flag is passed anywhere in this |
| 5354 | /// file). |
| 5355 | #[test] |
| 5356 | fn real_aomenc_film_grain_streams_decode_pixel_exact() { |
| 5357 | if !have_ffmpeg() { |
| 5358 | eprintln!("SKIP real_aomenc_film_grain_streams_decode_pixel_exact: no ffmpeg"); |
| 5359 | return; |
| 5360 | } |
| 5361 | if !have_aomenc() { |
| 5362 | eprintln!("SKIP real_aomenc_film_grain_streams_decode_pixel_exact: no aomenc"); |
| 5363 | return; |
| 5364 | } |
| 5365 | let (width, height) = (64usize, 64usize); |
| 5366 | for seed in [1u64, 2, 3] { |
| 5367 | let dir = std::env::temp_dir().join(format!( |
| 5368 | "ec-av1-filmgrain-gate-{}-{seed}", |
| 5369 | std::process::id() |
| 5370 | )); |
| 5371 | std::fs::create_dir_all(&dir).unwrap(); |
| 5372 | let yuv_path = dir.join("in.yuv"); |
| 5373 | let obu_path = dir.join("out.obu"); |
| 5374 | let frames = 3usize; |
| 5375 | let luma = width * height; |
| 5376 | let chroma = luma / 4; |
| 5377 | let mut yuv = Vec::with_capacity((luma + 2 * chroma) * frames); |
| 5378 | for f in 0..frames { |
| 5379 | let card = panned_test_card(width, height, ((f as i32 + seed as i32) * 5) as i64); |
| 5380 | yuv.extend_from_slice(&card.y); |
| 5381 | yuv.extend_from_slice(&card.u); |
| 5382 | yuv.extend_from_slice(&card.v); |
| 5383 | } |
| 5384 | std::fs::write(&yuv_path, &yuv).unwrap(); |
| 5385 | let status = Command::new(aomenc_path()) |
| 5386 | .args([ |
| 5387 | &format!("--width={width}"), |
| 5388 | &format!("--height={height}"), |
| 5389 | "--input-bit-depth=8", |
| 5390 | "--bit-depth=8", |
| 5391 | "--fps=25/1", |
| 5392 | &format!("--limit={frames}"), |
| 5393 | // The full constraint set of the forwarding/golden gates, |
| 5394 | // so the streams stay inside this decoder's supported |
| 5395 | // envelope and the gate isolates grain synthesis alone; |
| 5396 | // `--tune-content=film` is the one flag under test (it |
| 5397 | // enables apply_grain). |
| 5398 | "--codec=av1", |
| 5399 | "--passes=1", |
| 5400 | "--end-usage=q", |
| 5401 | "--cq-level=32", |
| 5402 | "--cpu-used=0", |
| 5403 | "--lag-in-frames=0", |
| 5404 | "--auto-alt-ref=0", |
| 5405 | "--kf-max-dist=1000", |
| 5406 | "--threads=1", |
| 5407 | "--row-mt=0", |
| 5408 | "--max-reference-frames=3", |
| 5409 | "--reduced-reference-set=1", |
| 5410 | "--enable-warped-motion=0", |
| 5411 | "--enable-obmc=0", |
| 5412 | "--enable-masked-comp=0", |
| 5413 | "--enable-interintra-comp=0", |
| 5414 | "--enable-dist-wtd-comp=0", |
| 5415 | "--enable-diff-wtd-comp=0", |
| 5416 | "--enable-onesided-comp=0", |
| 5417 | "--enable-interintra-wedge=0", |
| 5418 | "--enable-smooth-interintra=0", |
| 5419 | "--enable-rect-partitions=0", |
| 5420 | "--enable-ab-partitions=0", |
| 5421 | "--enable-1to4-partitions=0", |
| 5422 | "--enable-filter-intra=0", |
| 5423 | "--enable-smooth-intra=0", |
| 5424 | "--enable-paeth-intra=0", |
| 5425 | "--enable-directional-intra=0", |
| 5426 | "--enable-angle-delta=0", |
| 5427 | "--enable-tx-size-search=0", |
| 5428 | "--enable-cdef=0", |
| 5429 | "--enable-restoration=0", |
| 5430 | "--max-partition-size=32", |
| 5431 | "--min-partition-size=32", |
| 5432 | "--enable-palette=0", |
| 5433 | "--enable-intrabc=0", |
| 5434 | "--enable-cfl-intra=0", |
| 5435 | // Temporal MV projection (`use_ref_frame_mvs`) has no |
| 5436 | // decode path here (`mvstack.rs`'s documented corner-cut: |
| 5437 | // `zero_mv_ctx` and the mode-context GLOBALMV_OFFSET bit |
| 5438 | // are always computed as if it were off) -- this gate |
| 5439 | // isolates grain synthesis, so it must stay inside that |
| 5440 | // envelope like every other feature disabled above, not |
| 5441 | // silently decode a wrong mode context. Root cause of |
| 5442 | // this test's frame-1 mismatch: aomenc turns |
| 5443 | // `use_ref_frame_mvs` on by default once order hints are |
| 5444 | // enabled, unrelated to `--tune-content=film`. |
| 5445 | "--enable-ref-frame-mvs=0", |
| 5446 | "--tune-content=film", |
| 5447 | "--obu", |
| 5448 | "-o", |
| 5449 | ]) |
| 5450 | .arg(&obu_path) |
| 5451 | .arg(&yuv_path) |
| 5452 | .stdout(Stdio::null()) |
| 5453 | .stderr(Stdio::null()) |
| 5454 | .status() |
| 5455 | .expect("aomenc failed to start"); |
| 5456 | assert!(status.success(), "aomenc failed for seed {seed}"); |
| 5457 | let encoded = std::fs::read(&obu_path).unwrap(); |
| 5458 | let _ = std::fs::remove_dir_all(&dir); |
| 5459 | |
| 5460 | let mut parser = ec_av1_syntax::Av1Parser::new(); |
| 5461 | let mut pos = 0usize; |
| 5462 | let mut saw_grain = false; |
| 5463 | while pos < encoded.len() { |
| 5464 | let obu = parser.parse_obu(&encoded[pos..]).unwrap(); |
| 5465 | pos += obu.total_size; |
| 5466 | if let ec_av1_syntax::ObuKind::Frame(header, _) = obu.kind |
| 5467 | && header.film_grain.apply_grain |
| 5468 | { |
| 5469 | saw_grain = true; |
| 5470 | } |
| 5471 | } |
| 5472 | assert!(saw_grain, "seed {seed}: aomenc did not turn on apply_grain"); |
| 5473 | |
| 5474 | let ffmpeg_frames = ffmpeg_decode_sequence(&encoded, width, height, frames); |
| 5475 | let decoded = decode_stream(&encoded).unwrap(); |
| 5476 | assert_eq!(decoded.len(), frames, "seed {seed}"); |
| 5477 | for (i, (got, want)) in decoded.iter().zip(&ffmpeg_frames).enumerate() { |
| 5478 | assert_eq!(got.y, want.y, "seed {seed} frame {i} luma vs ffmpeg"); |
| 5479 | assert_eq!(got.u, want.u, "seed {seed} frame {i} U vs ffmpeg"); |
| 5480 | assert_eq!(got.v, want.v, "seed {seed} frame {i} V vs ffmpeg"); |
| 5481 | } |
| 5482 | } |
| 5483 | } |
| 5484 | |
| 5485 | /// lane-tiles r3: a real two-tile-column aomenc key frame, decoded |
| 5486 | /// through [`decode_key_frame_tile_with_cdfs`] directly with the whole |
| 5487 | /// `tiles: &[&[u8]]` list -- `decode_stream`'s `cols > 1 || rows > 1` |
| 5488 | /// refusal (this file, ~line 292) still fires for any stream this wide, |
| 5489 | /// so this gate bypasses it on purpose to prove the per-tile loop r2 |
| 5490 | /// landed (fresh `SymbolDecoder`/CDF copy per tile, `Neighbours::start_tile`, |
| 5491 | /// `PlaneBuf::set_tile_origin`) actually decodes both tiles pixel-exact |
| 5492 | /// before that refusal is lifted. 128x64 is exactly two 64x64 |
| 5493 | /// superblocks wide with `--tile-columns=1` (`TileCols=2`), so tile 1 is |
| 5494 | /// also the frame's own right edge -- the known gap (`PlaneBuf`'s |
| 5495 | /// tile_x0/tile_y0 only clip left/top reach, not a non-last column's |
| 5496 | /// right-edge reach) does not apply to this fixture; it would to a |
| 5497 | /// 3+-column one. Tile count itself is a structural aomenc param, not |
| 5498 | /// RD-dependent content, but this decoder's chroma smooth/paeth gap |
| 5499 | /// (`decode.rs` "a smooth or paeth chroma mode (round 2)", unrelated to |
| 5500 | /// tiles, pre-dating this lane) is content-dependent -- no aomenc flag |
| 5501 | /// disables UV-only smooth/paeth search, so a small seed sweep is |
| 5502 | /// needed the same way the sibling `a_real_aomenc_stream_with_*` gates |
| 5503 | /// retry past unrelated named refusals (every refusal in this crate is |
| 5504 | /// prefixed `"AV1 tile"` regardless of category, so unlike the sibling |
| 5505 | /// gates' `warp`/`obmc`/`interintra` banned lists there is no |
| 5506 | /// tile-specific substring to forbid here -- `matched > 0` plus the |
| 5507 | /// hard `tile_hits > 1`/pixel-exact asserts on any actual decode are |
| 5508 | /// the real proof). |
| 5509 | #[test] |
| 5510 | fn a_real_aomenc_stream_with_two_tile_columns_decodes_pixel_exact() { |
| 5511 | const NAME: &str = "a_real_aomenc_stream_with_two_tile_columns_decodes_pixel_exact"; |
| 5512 | if !have_ffmpeg() { |
| 5513 | eprintln!("SKIP {NAME}: no ffmpeg"); |
| 5514 | return; |
| 5515 | } |
| 5516 | if !have_aomenc() { |
| 5517 | eprintln!("SKIP {NAME}: no aomenc at {}", aomenc_path().display()); |
| 5518 | return; |
| 5519 | } |
| 5520 | let (width, height) = (128usize, 64usize); |
| 5521 | let mut matched = 0u32; |
| 5522 | let mut named_refusals = 0u32; |
| 5523 | for attempt in 0..20u32 { |
| 5524 | let seed = 42 + attempt; |
| 5525 | let source = gradients_source(seed, width, height, "duration=0.04:rate=25"); |
| 5526 | let y4m = Command::new("ffmpeg") |
| 5527 | .args([ |
| 5528 | "-v", |
| 5529 | "error", |
| 5530 | "-f", |
| 5531 | "lavfi", |
| 5532 | "-i", |
| 5533 | &source, |
| 5534 | "-pix_fmt", |
| 5535 | "yuv420p", |
| 5536 | "-f", |
| 5537 | "yuv4mpegpipe", |
| 5538 | "-", |
| 5539 | ]) |
| 5540 | .stdin(Stdio::null()) |
| 5541 | .stdout(Stdio::piped()) |
| 5542 | .stderr(Stdio::piped()) |
| 5543 | .output() |
| 5544 | .expect("ffmpeg failed to run"); |
| 5545 | assert!( |
| 5546 | y4m.status.success(), |
| 5547 | "ffmpeg fixture: {}", |
| 5548 | String::from_utf8_lossy(&y4m.stderr) |
| 5549 | ); |
| 5550 | let mut child = Command::new(aomenc_path()) |
| 5551 | .args([ |
| 5552 | "--codec=av1", |
| 5553 | "--passes=1", |
| 5554 | "--end-usage=q", |
| 5555 | "--cq-level=45", |
| 5556 | "--cpu-used=0", |
| 5557 | "--limit=1", |
| 5558 | "--kf-max-dist=1000", |
| 5559 | "--threads=1", |
| 5560 | "--row-mt=0", |
| 5561 | "--tile-columns=1", |
| 5562 | "--tile-rows=0", |
| 5563 | // This decoder hardcodes SB_MI=16 (64px) superblocks |
| 5564 | // everywhere (`decode.rs`'s sb128 dead-end, ledger |
| 5565 | // `av1-superblock-contexts`) -- without this, aomenc's |
| 5566 | // own default 128x128 superblock makes 128x64 exactly |
| 5567 | // ONE superblock wide, so `--tile-columns=1` has nothing |
| 5568 | // to split and `cols` comes back 1. |
| 5569 | "--sb-size=64", |
| 5570 | // Deblocking crosses tile boundaries by default (spec |
| 5571 | // `loop_filter_across_tiles_enabled`); `PlaneBuf`'s |
| 5572 | // tile_x0/tile_y0 origin only ever clips a tile's own |
| 5573 | // left/top reach (r2/r3's documented known gap), so a |
| 5574 | // non-last tile column's right-edge deblock would read |
| 5575 | // into the next tile's still-undecoded/differently- |
| 5576 | // origined samples. Off here since deblocking itself is |
| 5577 | // not what this gate is proving. |
| 5578 | "--loopfilter-control=0", |
| 5579 | "--enable-rect-partitions=0", |
| 5580 | "--enable-ab-partitions=0", |
| 5581 | "--enable-1to4-partitions=0", |
| 5582 | "--enable-filter-intra=0", |
| 5583 | "--enable-smooth-intra=0", |
| 5584 | "--enable-paeth-intra=0", |
| 5585 | "--enable-directional-intra=0", |
| 5586 | "--enable-angle-delta=0", |
| 5587 | "--enable-tx-size-search=0", |
| 5588 | "--enable-cdef=0", |
| 5589 | "--enable-restoration=0", |
| 5590 | "--max-partition-size=32", |
| 5591 | "--min-partition-size=32", |
| 5592 | "--enable-palette=0", |
| 5593 | "--enable-intrabc=0", |
| 5594 | "--enable-cfl-intra=0", |
| 5595 | "--obu", |
| 5596 | "-o", |
| 5597 | "-", |
| 5598 | "-", |
| 5599 | ]) |
| 5600 | .stdin(Stdio::piped()) |
| 5601 | .stdout(Stdio::piped()) |
| 5602 | .stderr(Stdio::piped()) |
| 5603 | .spawn() |
| 5604 | .expect("aomenc failed to start"); |
| 5605 | child |
| 5606 | .stdin |
| 5607 | .take() |
| 5608 | .expect("aomenc stdin") |
| 5609 | .write_all(&y4m.stdout) |
| 5610 | .expect("writing y4m to aomenc"); |
| 5611 | let out = child.wait_with_output().expect("aomenc failed to run"); |
| 5612 | assert!( |
| 5613 | out.status.success(), |
| 5614 | "aomenc refused the fixture: {}", |
| 5615 | String::from_utf8_lossy(&out.stderr) |
| 5616 | ); |
| 5617 | let stream = out.stdout; |
| 5618 | |
| 5619 | // Parse the stream ourselves (rather than `decode_stream`, whose |
| 5620 | // multi-tile refusal is exactly what this gate exists to run |
| 5621 | // ahead of) to reach the real `Frame` OBU's `tile_info` and its |
| 5622 | // full `Vec<Tile>` -- `Av1Parser::parse_obu` already locates |
| 5623 | // every tile in a multi-tile tile group (`parse_tile_group`), it |
| 5624 | // is only `decode_stream` that ever narrows this to |
| 5625 | // `tiles.first()`. |
| 5626 | let mut parser = Av1Parser::new(); |
| 5627 | let mut pos = 0usize; |
| 5628 | let mut frame = None; |
| 5629 | while pos < stream.len() { |
| 5630 | let obu_offset = pos; |
| 5631 | let obu = parser.parse_obu(&stream[pos..]).unwrap(); |
| 5632 | pos += obu.total_size; |
| 5633 | if let ObuKind::Frame(header, tiles) = obu.kind { |
| 5634 | frame = Some((header, tiles, obu_offset)); |
| 5635 | break; |
| 5636 | } |
| 5637 | } |
| 5638 | let (header, tiles, obu_offset) = frame.expect("stream has a Frame OBU"); |
| 5639 | assert!( |
| 5640 | header.tile_info.cols > 1, |
| 5641 | "{NAME}: aomenc did not actually split into >1 tile column (cols={}, seed {seed})", |
| 5642 | header.tile_info.cols |
| 5643 | ); |
| 5644 | assert_eq!( |
| 5645 | tiles.len(), |
| 5646 | header.tile_info.cols as usize, |
| 5647 | "{NAME}: expected one tile per column (seed {seed})" |
| 5648 | ); |
| 5649 | // `Tile::offset` is relative to the buffer `parse_obu` was |
| 5650 | // handed (`&stream[pos..]` at the time this OBU was parsed), so |
| 5651 | // it needs `obu_offset` added back -- the same adjustment |
| 5652 | // `decode_stream` makes for its own single-tile slice above. |
| 5653 | let tile_bufs: Vec<&[u8]> = tiles |
| 5654 | .iter() |
| 5655 | .map(|t| &stream[obu_offset + t.offset..obu_offset + t.offset + t.size]) |
| 5656 | .collect(); |
| 5657 | |
| 5658 | let enable_filter_intra = parser |
| 5659 | .sequence_header() |
| 5660 | .is_some_and(|seq| seq.enable_filter_intra); |
| 5661 | let enable_edge_filter = parser |
| 5662 | .sequence_header() |
| 5663 | .is_some_and(|seq| seq.enable_intra_edge_filter); |
| 5664 | |
| 5665 | let before = crate::decode::tile_hits(); |
| 5666 | let picture = match decode_key_frame_tile_with_cdfs( |
| 5667 | &tile_bufs, |
| 5668 | &header.tile_info, |
| 5669 | header.mi_cols, |
| 5670 | header.mi_rows, |
| 5671 | header.quantization.base_q_idx, |
| 5672 | header.frame_width, |
| 5673 | header.frame_height, |
| 5674 | enable_filter_intra, |
| 5675 | enable_edge_filter, |
| 5676 | &header.cdef, |
| 5677 | &header.loop_filter, |
| 5678 | None, |
| 5679 | header.tx_mode == TxMode::Select, |
| 5680 | header.reduced_tx_set, |
| 5681 | header.allow_screen_content_tools, |
| 5682 | header.allow_intrabc, |
| 5683 | ) { |
| 5684 | Err(e) => { |
| 5685 | let msg = e.to_string(); |
| 5686 | assert!( |
| 5687 | msg.contains("unsupported"), |
| 5688 | "{NAME} failed outright, not a named refusal (seed {seed}): {msg}" |
| 5689 | ); |
| 5690 | named_refusals += 1; |
| 5691 | eprintln!("seed {seed} refusal: {msg}"); |
| 5692 | continue; |
| 5693 | } |
| 5694 | Ok((picture, _end_cdfs)) => picture, |
| 5695 | }; |
| 5696 | let hits = crate::decode::tile_hits() - before; |
| 5697 | assert!( |
| 5698 | hits > 1, |
| 5699 | "{NAME}: tile_hits delta was {hits}, expected >1 -- both tiles must actually \ |
| 5700 | decode, not just tile 0 (seed {seed})" |
| 5701 | ); |
| 5702 | |
| 5703 | let ffmpeg_frames = ffmpeg_decode_sequence(&stream, width, height, 1); |
| 5704 | assert_eq!(picture.y, ffmpeg_frames[0].y, "{NAME}: luma vs ffmpeg (seed {seed})"); |
| 5705 | assert_eq!(picture.u, ffmpeg_frames[0].u, "{NAME}: U vs ffmpeg (seed {seed})"); |
| 5706 | assert_eq!(picture.v, ffmpeg_frames[0].v, "{NAME}: V vs ffmpeg (seed {seed})"); |
| 5707 | matched += 1; |
| 5708 | } |
| 5709 | assert!( |
| 5710 | matched > 0, |
| 5711 | "{NAME}: every attempt refused ({named_refusals} refusals); the gate never decoded \ |
| 5712 | a two-tile-column stream" |
| 5713 | ); |
| 5714 | eprintln!( |
| 5715 | "{NAME}: {matched} pixel-exact matches, {named_refusals} named refusals out of 20" |
| 5716 | ); |
| 5717 | } |
| 5718 | } |