forge
Explore
Log in

Don't you dare go Hollow

Four more refusals proven -- mi grid, ref height, primary_ref CDFs, intra mode

Browse files at this commit

367c38e679589152bf519cf35c9aa4d8a58c2b2dAhmet Kaan GümüşParent: d68cdb0
  • test
    • four more refusals proven -- mi grid, ref height, primary_ref CDFs, intra mode
Show full message
test(av1): four more refusals proven -- mi grid, ref height, primary_ref CDFs, intra mode

Two enumerations and two negative gates, taking the inventory's numerator
from 21 to 25 of 40:

* "a frame with no mode-info grid": `compute_image_size` (spec 5.9.9) is
  zero only for a zero frame width/height, and no path that sets one can
  produce that -- `frame_size()` is `+1`-coded, `superres_params()`
  divides by 9..=16 with rounding (enumerated over the whole 16-bit
  upscaled domain, minimum 1), and `show_existing_frame` /
  `frame_size_with_refs` copy a slot. Swept per axis through the REAL
  header reader over every size this crate's writers can express.
* "a reference picture whose height does not match this frame's own true
  size": negative gate, a written 64x64 key frame followed by a 64x32
  inter frame (`frame_size_override_flag` with error resilience on, the
  one combination the writer expresses) -- refused by name, one picture
  out.
* "a frame naming primary_ref_frame at a reference slot with no saved CDF
  state": the picture and CDF slots are written by exactly two loops over
  the same refresh mask (enumerated from the source, so no slot holds a
  picture without CDFs), and the reachable case is a slot nothing has
  refreshed -- a stream opening on a non-error-resilient inter frame,
  gated at all eight slots. This refusal, not the missing-key-frame one,
  is what such a stream reaches: the CDF load happens in the header path.
* "an intra mode this decoder does not code (round 2)": all three guards
  refuse `mode >= 13` on a symbol read from the 13-symbol `y_mode` CDF;
  the test finds each site by the refusal in its own body, resolves the
  CDF the symbol came from, and fails if a wider alphabet (`uv_mode_cfl`
  is 14) ever feeds one.
crates/ec-av1/src/refusal_inventory.rsmodified+101 -0
@@ -297,6 +297,31 @@ const PROVEN: &[(&str, &str)] = &[
297 "intra block copy on a HORZ/VERT/1:4 rect intra strip (reconstruction is not ported at this shape)",297 "intra block copy on a HORZ/VERT/1:4 rect intra strip (reconstruction is not ported at this shape)",
298 "a_real_aomenc_screen_key_frame_reads_use_intrabc_on_rect_strips",298 "a_real_aomenc_screen_key_frame_reads_use_intrabc_on_rect_strips",
299 ),299 ),
300+ // lane-t900 r26, enumeration: a y_mode symbol comes from a 13-symbol CDF
301+ // and all three guards refuse 13 and above.
302+ (
303+ "an intra mode this decoder does not code (round 2)",
304+ "every_intra_mode_symbol_of_the_y_mode_alphabet_is_coded",
305+ ),
306+ // lane-t900 r26, enumeration: `compute_image_size` is 0 only for a zero
307+ // frame width/height, which no path a header can code produces (swept
308+ // through the real reader per axis, superres division enumerated).
309+ (
310+ "a frame with no mode-info grid",
311+ "every_frame_size_a_header_can_code_has_a_mode_info_grid",
312+ ),
313+ // lane-t900 r26, negative gates: written streams that reach each refusal
314+ // by name (a 64x32 inter frame over a 64x64 reference; a stream opening on
315+ // a non-error-resilient inter frame whose primary_ref slot is empty, over
316+ // all 8 slots) with the picture/CDF slot invariant enumerated alongside.
317+ (
318+ "a reference picture whose height does not match this frame's own true size",
319+ "an_inter_frame_shorter_than_its_reference_is_refused_by_name",
320+ ),
321+ (
322+ "a frame naming primary_ref_frame at a reference slot with no saved CDF state",
323+ "an_inter_frame_naming_an_unrefreshed_primary_ref_slot_is_refused_by_name",
324+ ),
300 ];325 ];
301 326
302 /// Gates whose `Err` arm turns a decode failure into a printed SKIP rather than327 /// Gates whose `Err` arm turns a decode failure into a printed SKIP rather than
@@ -836,6 +861,82 @@ mod tests {
836 );861 );
837 }862 }
838 863
864+ /// The intra-mode alphabet, enumerated against the guard that refuses a
865+ /// symbol outside it.
866+ ///
867+ /// Three readers -- the rect-in-inter arm, the intra-in-inter square arm
868+ /// and the 8x8 leaf -- read a y_mode symbol and refuse `mode >= 13` with
869+ /// "an intra mode this decoder does not code (round 2)". A `y_mode` symbol
870+ /// comes from `Cdfs::y_mode[size_group]`, whose alphabet is
871+ /// `Y_MODE[g].len() - 1` (the last slot is the adaptation counter), and
872+ /// `SymbolDecoder::symbol` cannot return a value outside `0..nsyms` -- so
873+ /// the guard is unreachable exactly when every size group's alphabet is at
874+ /// most the threshold.
875+ ///
876+ /// Found by the refusal in the guard's own body rather than by line
877+ /// number, and the CDF the symbol was read from is checked too: a
878+ /// threshold of 13 over a 14-symbol table would be a live refusal, and a
879+ /// site reading a WIDER alphabet (`uv_mode` with CfL is 14) under the same
880+ /// guard would be one as well.
881+ #[test]
882+ fn every_intra_mode_symbol_of_the_y_mode_alphabet_is_coded() {
883+ const REFUSAL: &str = "an intra mode this decoder does not code (round 2)";
884+ // Both uv_mode alphabets are listed so the CfL-allowed one (14
885+ // symbols, `UV_MODE_CFL_ALLOWED`) cannot silently become the source of
886+ // a symbol tested against 13.
887+ let alphabets: [(&str, usize); 3] = [
888+ ("y_mode", crate::cdf::Y_MODE[0].len() - 1),
889+ ("uv_mode_no_cfl", crate::cdf::UV_MODE_NO_CFL[0].len() - 1),
890+ ("uv_mode_cfl", crate::cdf::UV_MODE_CFL[0].len() - 1),
891+ ];
892+ assert_eq!(alphabets[0].1, 13, "the y_mode alphabet changed size");
893+ for group in &crate::cdf::Y_MODE {
894+ assert_eq!(group.len() - 1, alphabets[0].1, "the y_mode rows disagree on width");
895+ }
896+
897+ let src = include_str!("decode.rs");
898+ let lines: Vec<&str> = src.lines().collect();
899+ let mut sites = 0usize;
900+ for (i, line) in lines.iter().enumerate() {
901+ let t = line.trim();
902+ let Some(rest) = t.strip_prefix("if mode >= ") else { continue };
903+ let Some(threshold) = rest.trim_end_matches(" {").parse::<usize>().ok() else {
904+ continue;
905+ };
906+ // Only the guards carrying THIS refusal.
907+ if !lines[i..(i + 4).min(lines.len())].join("\n").contains(REFUSAL) {
908+ continue;
909+ }
910+ sites += 1;
911+ // The symbol's own CDF: the nearest `dec.symbol(&mut cdfs.X[..])`
912+ // above the guard.
913+ let read = lines[i.saturating_sub(12)..i]
914+ .iter()
915+ .rev()
916+ .find(|l| l.contains("dec.symbol(&mut cdfs."))
917+ .unwrap_or_else(|| panic!("line {}: no symbol read above the guard", i + 1));
918+ let table = read
919+ .split("dec.symbol(&mut cdfs.")
920+ .nth(1)
921+ .unwrap()
922+ .split('[')
923+ .next()
924+ .unwrap();
925+ let (name, nsyms) = alphabets
926+ .iter()
927+ .find(|(n, _)| *n == table)
928+ .unwrap_or_else(|| panic!("line {}: unknown mode alphabet {table:?}", i + 1));
929+ assert!(
930+ *nsyms <= threshold,
931+ "line {}: the guard refuses mode >= {threshold} on a symbol read from a \
932+ {nsyms}-symbol {name} CDF -- symbol {threshold} is inside that alphabet, so \
933+ the refusal is live and this enumeration does not prove it",
934+ i + 1
935+ );
936+ }
937+ assert_eq!(sites, 3, "the intra-mode guard is at three sites, found {sites}");
938+ }
939+
839 /// Every entry of [`PROVEN`] must still name a live refusal and a test940 /// Every entry of [`PROVEN`] must still name a live refusal and a test
840 /// that exists, and the count it prints is the inventory's numerator.941 /// that exists, and the count it prints is the inventory's numerator.
841 #[test]942 #[test]
crates/ec-av1/src/stream.rsmodified+202 -0
@@ -1324,6 +1324,208 @@ mod tests {
1324 assert_eq!(shown, 0, "an inter frame with no reference produced a picture");1324 assert_eq!(shown, 0, "an inter frame with no reference produced a picture");
1325 }1325 }
1326 1326
1327+ /// lane-t900 r26, negative gate: an inter frame whose own coded size is
1328+ /// SHORTER than the reference it predicts from is refused BY NAME, and
1329+ /// outputs no picture beyond the key frame that preceded it.
1330+ ///
1331+ /// The height mismatch is the one size difference this decoder cannot
1332+ /// absorb: `mc::predict_scaled` scales horizontally (a `use_superres`
1333+ /// reference), and nothing on this path scales vertically. The fixture is
1334+ /// written, not hand-built: a shown key frame at 64x64 (which refreshes
1335+ /// every slot), then a shown inter frame carrying
1336+ /// `frame_size_override_flag` with `error_resilient_mode` set -- the one
1337+ /// combination [`crate::frame::write_frame_header`] writes, since
1338+ /// `frame_size_with_refs` (spec 5.9.7) is only read when error resilience
1339+ /// is OFF. The inter frame's tile payload is the key frame's; the refusal
1340+ /// fires on the reference's dimensions before a tile symbol is read.
1341+ #[test]
1342+ fn an_inter_frame_shorter_than_its_reference_is_refused_by_name() {
1343+ const REFUSAL: &str =
1344+ "a reference picture whose height does not match this frame's own true size";
1345+ let picture = test_card(64, 64);
1346+ let encoded = encode_key_frame(&picture, 100, 0.5).unwrap();
1347+ let (seq, key) = crate::encode::key_frame_headers(64, 64, 100).unwrap();
1348+ let inter = FrameHeader {
1349+ frame_type: FrameType::Inter,
1350+ frame_is_intra: false,
1351+ error_resilient_mode: true,
1352+ primary_ref_frame: PRIMARY_REF_NONE,
1353+ refresh_frame_flags: 0x02,
1354+ order_hint: 1,
1355+ frame_size_override_flag: true,
1356+ frame_height: 32,
1357+ render_height: 32,
1358+ ..key.clone()
1359+ };
1360+ let mut stream = crate::sequence::sequence_header_obu(&seq).unwrap();
1361+ stream.extend_from_slice(&crate::frame::frame_obu(&seq, &key, &encoded.tile).unwrap());
1362+ stream.extend_from_slice(&crate::frame::frame_obu(&seq, &inter, &encoded.tile).unwrap());
1363+
1364+ let mut shown = 0;
1365+ let err = decode_stream_with(&stream, |_, _, _| {
1366+ shown += 1;
1367+ Ok(())
1368+ })
1369+ .unwrap_err()
1370+ .to_string();
1371+ assert!(err.contains(REFUSAL), "expected {REFUSAL:?}, got: {err}");
1372+ assert_eq!(shown, 1, "expected the key frame alone, got {shown} pictures");
1373+ }
1374+
1375+ /// lane-t900 r26, negative gate: an inter frame naming a
1376+ /// `primary_ref_frame` whose reference slot has never been refreshed is
1377+ /// refused BY NAME rather than decoded from the spec 8.4 default tables.
1378+ ///
1379+ /// Enumeration first, because the refusal's premise is an invariant: the
1380+ /// picture slots and the CDF slots are written by exactly two loops in
1381+ /// [`decode_stream`] (the `show_existing_frame` arm's copy and the
1382+ /// end-of-frame store), and both walk the SAME `refresh_frame_flags` mask
1383+ /// writing `ref_slots[i]` and `cdf_slots[i]` together -- so no slot can
1384+ /// hold a picture without CDFs, and the reachable case is a slot nothing
1385+ /// has refreshed at all. That is what this builds: a stream that OPENS on
1386+ /// a non-error-resilient inter frame (the only header shape that codes
1387+ /// `primary_ref_frame` at all, spec 5.9.2), tried at all eight slots.
1388+ ///
1389+ /// This refusal, not "an inter frame with no key frame before it", is the
1390+ /// one such a stream reaches: the CDF load happens in the header path,
1391+ /// before the reference pictures are looked up.
1392+ #[test]
1393+ fn an_inter_frame_naming_an_unrefreshed_primary_ref_slot_is_refused_by_name() {
1394+ const REFUSAL: &str =
1395+ "a frame naming primary_ref_frame at a reference slot with no saved CDF state";
1396+ // The invariant above, read off the source: every line that stores a
1397+ // picture into a slot sits in a block that stores that slot's CDFs too.
1398+ let src = include_str!("stream.rs");
1399+ let stores: Vec<usize> = src
1400+ .lines()
1401+ .enumerate()
1402+ .filter(|(_, l)| l.trim().starts_with("ref_slots[i] = "))
1403+ .map(|(i, _)| i)
1404+ .collect();
1405+ assert_eq!(
1406+ stores.len(),
1407+ 2,
1408+ "the picture slots gained a writer -- check it stores cdf_slots too"
1409+ );
1410+ let lines: Vec<&str> = src.lines().collect();
1411+ for &i in &stores {
1412+ let window = lines[i.saturating_sub(6)..(i + 6).min(lines.len())].join("\n");
1413+ assert!(
1414+ window.contains("cdf_slots[i] = "),
1415+ "line {} stores a reference picture with no cdf_slots store beside it: a slot \
1416+ can then hold a picture with no saved CDF state",
1417+ i + 1
1418+ );
1419+ }
1420+
1421+ let picture = test_card(64, 64);
1422+ let encoded = encode_key_frame(&picture, 100, 0.5).unwrap();
1423+ let (seq, key) = crate::encode::key_frame_headers(64, 64, 100).unwrap();
1424+ for slot in 0..NUM_REF_FRAMES as u8 {
1425+ let inter = FrameHeader {
1426+ frame_type: FrameType::Inter,
1427+ frame_is_intra: false,
1428+ // `primary_ref_frame` is coded only when error resilience is
1429+ // off (spec 5.9.2); `frame_size_override_flag` stays clear so
1430+ // the header writes a plain `frame_size()`.
1431+ error_resilient_mode: false,
1432+ primary_ref_frame: 0,
1433+ ref_frame_idx: [slot; ec_av1_syntax::REFS_PER_FRAME],
1434+ refresh_frame_flags: 0x02,
1435+ order_hint: 1,
1436+ ..key.clone()
1437+ };
1438+ let mut stream = crate::sequence::sequence_header_obu(&seq).unwrap();
1439+ stream
1440+ .extend_from_slice(&crate::frame::frame_obu(&seq, &inter, &encoded.tile).unwrap());
1441+
1442+ let mut shown = 0;
1443+ let err = decode_stream_with(&stream, |_, _, _| {
1444+ shown += 1;
1445+ Ok(())
1446+ })
1447+ .unwrap_err()
1448+ .to_string();
1449+ assert!(err.contains(REFUSAL), "slot {slot}: expected {REFUSAL:?}, got: {err}");
1450+ assert_eq!(shown, 0, "slot {slot}: a frame with no CDF state produced a picture");
1451+ }
1452+ }
1453+
1454+ /// lane-t900 r26, enumeration: no frame header a stream can carry derives
1455+ /// an empty mode-info grid, so "a frame with no mode-info grid" names a
1456+ /// case outside the coded domain.
1457+ ///
1458+ /// `compute_image_size` (spec 5.9.9) is `mi_cols = 2 * ((frame_width + 7)
1459+ /// >> 3)`, which is 0 only for `frame_width == 0`, and a frame width is
1460+ /// never 0 on any of the three paths that set one:
1461+ ///
1462+ /// * `frame_size()` (5.9.5) reads `frame_width_minus_1` and adds one, or
1463+ /// takes the sequence's `max_frame_width` (itself `+ 1`-coded);
1464+ /// * `superres_params()` (5.9.8) divides by a denominator of 9..=16 with
1465+ /// rounding, whose minimum over the whole 16-bit upscaled-width domain
1466+ /// is enumerated below and is 1, not 0;
1467+ /// * `show_existing_frame` and `frame_size_with_refs()` copy a slot's own
1468+ /// dimensions, which came from one of the above.
1469+ ///
1470+ /// The sweep drives the REAL header reader (write a header, parse it back)
1471+ /// over every width and every height a 16-bit `frame_size()` can code,
1472+ /// per axis -- the derivation is independent per axis -- rather than
1473+ /// re-deriving the formula here.
1474+ #[test]
1475+ fn every_frame_size_a_header_can_code_has_a_mode_info_grid() {
1476+ let mut sizes: Vec<u32> = (1..=1024).collect();
1477+ sizes.extend([2048, 4095, 4096, 8192, 16384, 32768, 65535, 65536]);
1478+ // `mi_cols` is nondecreasing in `frame_width`, so an empty grid can
1479+ // only appear at the SMALL end -- which is why a size this crate's
1480+ // tile-info writer cannot express (its tile log2 range runs out well
1481+ // before 65536) is skipped rather than worked around, while every
1482+ // size up to 1024 must be covered.
1483+ let mut skipped: Vec<u32> = Vec::new();
1484+ for n in sizes {
1485+ for (w, h) in [(n, 16), (16, n)] {
1486+ let Ok((seq, header)) =
1487+ crate::encode::key_frame_headers(w as usize, h as usize, 100)
1488+ else {
1489+ skipped.push(n);
1490+ continue;
1491+ };
1492+ let Ok(stream) = crate::frame::frame_obu(&seq, &header, &[]) else {
1493+ skipped.push(n);
1494+ continue;
1495+ };
1496+ let mut parser = Av1Parser::new();
1497+ parser.parse_obu(&crate::sequence::sequence_header_obu(&seq).unwrap()).unwrap();
1498+ let obu = parser.parse_obu(&stream).unwrap();
1499+ let ObuKind::Frame(parsed, _) = &obu.kind else {
1500+ panic!("{w}x{h}: the written frame OBU did not parse as one");
1501+ };
1502+ assert!(
1503+ parsed.mi_cols >= 2 && parsed.mi_rows >= 2,
1504+ "{w}x{h} parsed to a {}x{} mode-info grid",
1505+ parsed.mi_cols,
1506+ parsed.mi_rows
1507+ );
1508+ }
1509+ }
1510+ assert!(
1511+ skipped.iter().all(|&n| n > 1024),
1512+ "the writer could not express these small frame sizes, so the small end of the \
1513+ sweep is incomplete: {skipped:?}"
1514+ );
1515+ // The superres arm, which no writer here can express: spec 5.9.8's
1516+ // `(upscaled_width * 8 + denom / 2) / denom` over its whole domain.
1517+ for denom in 9u32..=16 {
1518+ for upscaled in [1u32, 2, 3, 7, 8, 9, 255, 4096, 65535, 65536] {
1519+ let width = (upscaled * 8 + denom / 2) / denom;
1520+ assert!(
1521+ width >= 1,
1522+ "superres denom {denom} maps an upscaled width of {upscaled} to {width}, \
1523+ which would derive an empty mode-info grid"
1524+ );
1525+ }
1526+ }
1527+ }
1528+
1327 /// lane-interedge r1: one attempt of the inter frame-edge half-strip gate --1529 /// lane-interedge r1: one attempt of the inter frame-edge half-strip gate --
1328 /// encode `width x height` with real aomenc (rect partitions ON, every other1530 /// encode `width x height` with real aomenc (rect partitions ON, every other
1329 /// lane's open tool pinned off), decode it, and return1531 /// lane's open tool pinned off), decode it, and return