diff --git a/crates/ec-av1/src/decode.rs b/crates/ec-av1/src/decode.rs index 471d4fb8..1e132312 100644 --- a/crates/ec-av1/src/decode.rs +++ b/crates/ec-av1/src/decode.rs @@ -7734,6 +7734,110 @@ fn every_intra_in_inter_shape_the_census_lists_has_a_size_group_row() { ); } +/// The `motion_mode` / `obmc` CDF row for a block footprint: libaom's +/// `motion_mode_cdf[mbmi->bsize]`, in this crate's packed row order (which +/// `cdf::MOTION_MODE` documents -- the 128-px rows come last, so they are +/// matched before the square arm's `log2` shortcut). +/// +/// `None` is the refusal "a motion_mode symbol for a block shape with no CDF +/// row here"; the enumeration below is what proves no conformant block can +/// reach it. +pub(crate) fn motion_mode_cdf_row(write_w: usize, write_h: usize) -> Option { + Some(match (write_w, write_h) { + // lane-sb128c r5: the 128 root's own rows come FIRST -- the square arm + // below maps 128 to row 4 (BLOCK_16X32). + (64, 128) => 14, + (128, 64) => 15, + (128, 128) => 16, + (w, h) if w == h => (w.trailing_zeros() - 3) as usize, + (16, 32) => 4, + (32, 16) => 5, + (32, 64) => 6, + (64, 32) => 7, + (8, 32) => 8, + (32, 8) => 9, + (16, 64) => 10, + (64, 16) => 11, + // lane-inter4 r3: the 16x16-level rect inter leaves. + (8, 16) => 12, + (16, 8) => 13, + _ => return None, + }) +} + +/// Every `BLOCK_SIZES_ALL` footprint, as `(width, height)` in pixels: the six +/// squares, the ten 2:1 shapes and the six 1:4 ones (libaom `common_data.h`, +/// `block_size_wide`/`block_size_high`). +#[cfg(test)] +const BLOCK_SIZES_ALL_WH: [(usize, usize); 22] = [ + (4, 4), + (4, 8), + (8, 4), + (8, 8), + (8, 16), + (16, 8), + (16, 16), + (16, 32), + (32, 16), + (32, 32), + (32, 64), + (64, 32), + (64, 64), + (64, 128), + (128, 64), + (128, 128), + (4, 16), + (16, 4), + (8, 32), + (32, 8), + (16, 64), + (64, 16), +]; + +/// lane-t900 r25, enumeration: the refusal "a motion_mode symbol for a block +/// shape with no CDF row here" names a shape no conformant stream can present. +/// +/// A `motion_mode`/`obmc` symbol is read only under `motion_mode_eligible`, +/// whose `write_w.min(write_h) >= 8` clause is libaom's +/// `is_motion_variation_allowed_bsize` (`blockd.h:1455`). So the reachable +/// domain of [`motion_mode_cdf_row`] is exactly the `BLOCK_SIZES_ALL` +/// footprints with `min(bw, bh) >= 8` -- 17 of the 22 -- and each of those +/// must map to a distinct row of the 17-row `Cdfs::motion_mode` table. The +/// five it excludes (4x4, 4x8, 8x4, 4x16, 16x4) are the ones the eligibility +/// clause rejects before the lookup. +#[test] +fn every_shape_that_allows_motion_variation_has_a_motion_mode_cdf_row() { + let mut rows = std::collections::BTreeSet::new(); + let mut excluded = 0; + for (w, h) in BLOCK_SIZES_ALL_WH { + if w.min(h) < 8 { + excluded += 1; + assert!( + w == 4 || h == 4, + "{w}x{h} is not one of the sub-8 shapes is_motion_variation_allowed_bsize rejects" + ); + continue; + } + let row = motion_mode_cdf_row(w, h).unwrap_or_else(|| { + panic!( + "a {w}x{h} block passes is_motion_variation_allowed_bsize and reads a motion_mode \ + symbol, but has no CDF row -- the refusal is a real gap, not a dead arm" + ) + }); + assert!( + row < 17, + "{w}x{h} maps to motion_mode row {row}, outside the 17-row table" + ); + assert!(rows.insert(row), "{w}x{h} shares motion_mode row {row}"); + } + assert_eq!(excluded, 5, "the sub-8 footprint set changed"); + assert_eq!( + rows.len(), + 17, + "17 eligible footprints must fill all 17 rows of the motion_mode table" + ); +} + fn decode_intra_rect_in_inter( dec: &mut SymbolDecoder, cdfs: &mut Cdfs, @@ -22342,30 +22446,9 @@ fn decode_inter_block( // documented on `cdf::MOTION_MODE`); a wrong row is a wrong // interval narrowing, not a wrong value (class // wrong-alphabet-same-value). - let bsize_idx = match (write_w, write_h) { - // lane-sb128c r5: the 128 root's own rows come FIRST -- - // the square arm below maps 128 to row 4 (BLOCK_16X32). - (64, 128) => 14, - (128, 64) => 15, - (128, 128) => 16, - (w, h) if w == h => (w.trailing_zeros() - 3) as usize, - (16, 32) => 4, - (32, 16) => 5, - (32, 64) => 6, - (64, 32) => 7, - (8, 32) => 8, - (32, 8) => 9, - (16, 64) => 10, - (64, 16) => 11, - // lane-inter4 r3: the 16x16-level rect inter leaves. - (8, 16) => 12, - (16, 8) => 13, - _ => { - return Err(unsupported( - "a motion_mode symbol for a block shape with no CDF row here", - )); - } - }; + let bsize_idx = motion_mode_cdf_row(write_w, write_h).ok_or_else(|| { + unsupported("a motion_mode symbol for a block shape with no CDF row here") + })?; // `motion_mode_allowed` reads the 3-symbol `motion_mode_cdf` // instead of the 2-symbol `obmc_cdf` exactly when // `num_proj_ref >= 1` under `allow_warped_motion`. diff --git a/crates/ec-av1/src/refusal_inventory.rs b/crates/ec-av1/src/refusal_inventory.rs index 4811fad3..932442b3 100644 --- a/crates/ec-av1/src/refusal_inventory.rs +++ b/crates/ec-av1/src/refusal_inventory.rs @@ -262,6 +262,33 @@ const PROVEN: &[(&str, &str)] = &[ "a Golomb tail longer than this decoder reads", "read_golomb_reads_every_value_a_conformant_stream_can_carry", ), + // lane-t900 r25, enumeration: `parse_tile_group` is the only producer of + // the `tiles` vector `decode_stream` tests, and its success path always + // pushes at least one `Tile` -- swept over a real frame OBU's tile payload + // truncated from empty upwards. + ( + "a frame OBU with no tile group", + "a_frame_obu_that_parses_always_carries_at_least_one_tile", + ), + // lane-t900 r25, negative gates: hand-built streams that reach each + // refusal by name and output no picture (a `show_existing_frame` header + // over all 8 empty slots; a stream that opens on an inter frame). + ( + "a show_existing_frame header naming an empty reference slot", + "a_show_existing_frame_header_naming_an_empty_slot_is_refused_by_name", + ), + ( + "an inter frame with no key frame before it", + "an_inter_frame_opening_a_stream_is_refused_by_name", + ), + // lane-t900 r25, enumeration: a motion_mode/obmc symbol is read only under + // `is_motion_variation_allowed_bsize` (min side >= 8), and each of the 17 + // `BLOCK_SIZES_ALL` footprints that clause admits maps to a distinct row of + // the 17-row table. + ( + "a motion_mode symbol for a block shape with no CDF row here", + "every_shape_that_allows_motion_variation_has_a_motion_mode_cdf_row", + ), ( "a bit depth of 12 (this decoder is gated at 8 and 10 only: warp/MC/wiener rounding shifts change at 12-bit and no 12-bit gate exists)", "a_twelve_bit_sequence_header_is_refused_by_name", diff --git a/crates/ec-av1/src/stream.rs b/crates/ec-av1/src/stream.rs index 03d7cfa2..7726ceaf 100644 --- a/crates/ec-av1/src/stream.rs +++ b/crates/ec-av1/src/stream.rs @@ -1168,6 +1168,162 @@ mod tests { ); } + /// lane-t900 r25: the refusal "a frame OBU with no tile group" names a + /// shape the parser cannot hand [`decode_stream`]. + /// + /// `tiles` reaches that check from exactly two places -- `ObuKind::Frame`'s + /// own vector and the `ObuKind::TileGroup` accumulation -- and both are + /// filled by `ec_av1_syntax`'s `parse_tile_group`, whose only success path + /// pushes one `Tile` per `tile_num` of the INCLUSIVE range + /// `tg_start..=tg_end`. That range is never empty: the single-tile-group + /// case takes `(0, num_tiles - 1)` saturating at 0, and the multi-group + /// case rejects `end < start` as corrupt. So an `OBU_FRAME` either fails to + /// parse or carries at least one tile, however short its payload is -- + /// including the empty payload the charter for this round asked to build, + /// which parses into ONE zero-byte tile rather than none. + /// + /// Sweeping a real frame OBU's tile payload from empty upwards asserts both + /// halves: every `Frame` OBU that parses has a tile, and `decode_stream` + /// never reports this refusal for any of them. + #[test] + fn a_frame_obu_that_parses_always_carries_at_least_one_tile() { + const REFUSAL: &str = "a frame OBU with no tile group"; + let picture = test_card(64, 64); + let encoded = encode_key_frame(&picture, 100, 0.5).unwrap(); + let (seq, header) = crate::encode::key_frame_headers(64, 64, 100).unwrap(); + let mut frame_obus_parsed = 0; + for len in 0..=encoded.tile.len().min(24) { + let mut stream = crate::sequence::sequence_header_obu(&seq).unwrap(); + stream.extend_from_slice( + &crate::frame::frame_obu(&seq, &header, &encoded.tile[..len]).unwrap(), + ); + let mut parser = Av1Parser::new(); + let mut pos = 0usize; + while pos < stream.len() { + let Ok(obu) = parser.parse_obu(&stream[pos..]) else { + break; + }; + pos += obu.total_size; + if let ObuKind::Frame(_, tiles) = &obu.kind { + frame_obus_parsed += 1; + assert!( + !tiles.is_empty(), + "a {len}-byte tile payload parsed into a frame OBU with no tile group -- \ + the refusal {REFUSAL:?} is reachable after all" + ); + } + } + if let Err(e) = decode_stream(&stream) { + let e = e.to_string(); + assert!( + !e.contains(REFUSAL), + "a {len}-byte tile payload reached {REFUSAL:?}: {e}" + ); + } + } + assert!( + frame_obus_parsed >= 2, + "the sweep parsed {frame_obus_parsed} frame OBUs -- it proves nothing about the \ + refusal's reachability" + ); + } + + /// lane-t900 r25, negative gate: a `show_existing_frame` header naming a + /// slot no frame has refreshed is refused BY NAME, and outputs no picture. + /// + /// The OBU is hand-built because [`crate::frame::write_frame_header`] + /// refuses to write one (it writes shown key and inter frames only): spec + /// 5.9.2's whole header for this case is `show_existing_frame` (1) plus + /// `frame_to_show_map_idx` (3), the sequence carrying neither a decoder + /// model nor frame ids. + /// + /// Deviation from the round's charter, which asked for a key frame first + /// with the named slot's `refresh_frame_flags` bit clear: a SHOWN key frame + /// refreshes every slot (spec 5.9.2 forces `refresh_frame_flags` to + /// `all_frames` and does not code it, which is also what + /// `write_frame_header` does), so the only reachable empty-slot stream is + /// one where no frame has refreshed that slot yet. The stream below is a + /// valid sequence header followed by the header OBU, and every one of the + /// eight slots is tried. + #[test] + fn a_show_existing_frame_header_naming_an_empty_slot_is_refused_by_name() { + const REFUSAL: &str = "a show_existing_frame header naming an empty reference slot"; + let (seq, _) = crate::encode::key_frame_headers(64, 64, 100).unwrap(); + for slot in 0..NUM_REF_FRAMES as u32 { + let mut w = ec_core::BitWriter::new(); + w.write_bit(true); // show_existing_frame + w.write_bits(slot, 3); // frame_to_show_map_idx + crate::bits::write_byte_alignment(&mut w); + let mut stream = crate::sequence::sequence_header_obu(&seq).unwrap(); + stream.extend_from_slice(&crate::obu::wrap_obu( + &ec_av1_syntax::obu::ObuHeader { + obu_type: ec_av1_syntax::obu::ObuType::FrameHeader, + extension_flag: false, + has_size_field: true, + temporal_id: 0, + spatial_id: 0, + }, + &w.into_bytes(), + )); + + let mut shown = 0; + let err = decode_stream_with(&stream, |_, _, _| { + shown += 1; + Ok(()) + }) + .unwrap_err() + .to_string(); + assert!( + err.contains(REFUSAL), + "slot {slot}: expected {REFUSAL:?}, got: {err}" + ); + assert_eq!(shown, 0, "slot {slot}: an empty slot was output as a picture"); + } + } + + /// lane-t900 r25, negative gate: an inter frame whose `ref_frame_idx[0]` + /// slot holds no picture -- a stream that opens on an inter frame -- is + /// refused BY NAME rather than predicting from nothing. + /// + /// Deviation from the round's charter, which asked for a real aomenc + /// two-frame stream with the key frame's OBUs stripped: this crate's own + /// `write_frame_header` writes a shown INTER frame (short reference + /// signalling and non-identity global motion are the parts it refuses), so + /// the fixture is built from the writers, needs neither ffmpeg nor aomenc, + /// and is byte-identical on every run. The tile payload is a real key + /// frame's; the refusal fires on the header's reference lookup, before any + /// tile is decoded. + #[test] + fn an_inter_frame_opening_a_stream_is_refused_by_name() { + const REFUSAL: &str = "an inter frame with no key frame before it"; + let picture = test_card(64, 64); + let encoded = encode_key_frame(&picture, 100, 0.5).unwrap(); + let (seq, key) = crate::encode::key_frame_headers(64, 64, 100).unwrap(); + let inter = FrameHeader { + frame_type: FrameType::Inter, + frame_is_intra: false, + // Keeps `primary_ref_frame`, `frame_size_with_refs` and + // `use_ref_frame_mvs` out of the written header (spec 5.9.2). + error_resilient_mode: true, + primary_ref_frame: PRIMARY_REF_NONE, + refresh_frame_flags: 0x02, + order_hint: 1, + ..key + }; + let mut stream = crate::sequence::sequence_header_obu(&seq).unwrap(); + stream.extend_from_slice(&crate::frame::frame_obu(&seq, &inter, &encoded.tile).unwrap()); + + let mut shown = 0; + let err = decode_stream_with(&stream, |_, _, _| { + shown += 1; + Ok(()) + }) + .unwrap_err() + .to_string(); + assert!(err.contains(REFUSAL), "expected {REFUSAL:?}, got: {err}"); + assert_eq!(shown, 0, "an inter frame with no reference produced a picture"); + } + /// lane-interedge r1: one attempt of the inter frame-edge half-strip gate -- /// encode `width x height` with real aomenc (rect partitions ON, every other /// lane's open tool pinned off), decode it, and return