forge
Explore
Log in

Don't you dare go Hollow

Lane-av1superpin -- the two remaining measured-exact 4:4:4 superres members

Browse files at this commit

ae116f19b5421a51978d294dfaf20566fa6077ffAhmet Kaan GümüşParent: c3f7cbf
  • test
    • lane-av1superpin -- the two remaining measured-exact 4:4:4 superres members
Show full message
test(av1): lane-av1superpin -- the two remaining measured-exact 4:4:4 superres members

den=9 (coded 228x128, the widest frame-edge margin this geometry allows) and
superres-mode=2 (parsed denominators 11/14/15/9, one per frame). Both assert
use_superres, the per-frame PARSED denominator, upscaled_width, the coded size,
and re-derive FrameWidth from (UpscaledWidth*8 + denom/2)/denom.

The mode-2 gate also asserts the flag-ignoring property directly: encoding the
recipe with no denominator and with 9/12/16 all reproduce the pinned bytes, and
three of the four parsed denominators are values no flag ever named -- so a
future gate cannot pass by reading the flag instead of the header.

Oracle: libaom v3.13.3-7-g9bb526a (scripts/build-aom-oracle.sh source tree;
--version is not an accepted flag on that build).
crates/ec-av1/fixtures/444_lossy_superres_256x128_d9.obuadded+0 -0

Binary file changed

crates/ec-av1/fixtures/444_lossy_superres_mode2_256x128.obuadded+0 -0

Binary file changed

crates/ec-av1/src/stream.rsmodified+381 -0
@@ -7479,6 +7479,387 @@ pub(crate) mod tests {
7479 }7479 }
7480 }7480 }
7481 7481
7482+ /// Shared body for the lane-av1superpin 4:4:4 superres arms. It is the
7483+ /// body of [`a_444_lossy_superres_stream_decodes_pixel_exact`] with the
7484+ /// single `(denominator, coded_width)` constant generalised to a PER-FRAME
7485+ /// table, which is what the mode-2 cell needs: libaom picks a different
7486+ /// denominator for each frame there, so a single constant would either
7487+ /// describe frame 0 only or be a literal the arithmetic below could not
7488+ /// hold.
7489+ ///
7490+ /// Everything that makes the den=12 cell non-vacuous is here, unchanged:
7491+ /// the pinned bytes (length + FNV-1a), 4:4:4 / 8-bit / superres-enabled
7492+ /// from the sequence header, `use_superres` on every parsed frame header,
7493+ /// the per-frame parsed denominator and coded size, and the coded width
7494+ /// RE-DERIVED as `(UpscaledWidth*8 + denom/2)/denom` from that frame's OWN
7495+ /// parsed denominator rather than compared to a literal alone -- so a
7496+ /// re-pinned fixture cannot satisfy the literals while the scaled-size
7497+ /// arithmetic moves under them. Then `superres_hits` and
7498+ /// `predict_scaled_hits` as DELTAS (both counters are process-wide with no
7499+ /// reset, so absolute values would pass on residue from another stream),
7500+ /// the returned frames at the UPSCALED width with full-resolution 4:4:4
7501+ /// chroma planes, and every decode-order frame byte-exact against the
7502+ /// instrumented aomdec.
7503+ #[allow(clippy::too_many_arguments)]
7504+ fn a_444_superres_arm(
7505+ name: &str,
7506+ fixture: &str,
7507+ fixture_len: usize,
7508+ fixture_fnv: u64,
7509+ fixture_sha: &str,
7510+ upscaled: (usize, usize),
7511+ // `(denominator, coded_width)` per DECODE-ORDER frame.
7512+ per_frame: &[(u8, usize)],
7513+ ) -> Vec<ec_av1_syntax::FrameHeader> {
7514+ let obu = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")).join(fixture);
7515+ let stream = std::fs::read(&obu).unwrap_or_else(|e| {
7516+ panic!(
7517+ "{name}: pinned fixture {} is missing ({e}) -- the gate cannot run, \
7518+ which is a failure, not a skip",
7519+ obu.display()
7520+ )
7521+ });
7522+ assert_eq!(stream.len(), fixture_len, "{name}: fixture length moved");
7523+ assert_eq!(fnv1a64(&stream), fixture_fnv, "{name}: fixture bytes moved");
7524+
7525+ // --- the header facts the CELL depends on, parsed not assumed ------
7526+ let mut parser = Av1Parser::new();
7527+ let mut pos = 0usize;
7528+ let mut seq = None;
7529+ let mut heads: Vec<FrameHeader> = Vec::new();
7530+ while pos < stream.len() {
7531+ let obu = parser
7532+ .parse_obu(&stream[pos..])
7533+ .unwrap_or_else(|e| panic!("{name}: aomenc OBU does not parse: {e:?}"));
7534+ if obu.total_size == 0 {
7535+ break;
7536+ }
7537+ pos += obu.total_size;
7538+ match obu.kind {
7539+ ObuKind::SequenceHeader(s) => seq = Some(s),
7540+ ObuKind::FrameHeader(h) => heads.push(*h),
7541+ ObuKind::Frame(h, _) => heads.push(*h),
7542+ _ => {}
7543+ }
7544+ }
7545+ let seq = seq.unwrap_or_else(|| panic!("{name}: the stream carries no sequence header"));
7546+ assert_eq!(
7547+ (
7548+ seq.color_config.subsampling_x,
7549+ seq.color_config.subsampling_y
7550+ ),
7551+ (0, 0),
7552+ "{name}: the stream is not 4:4:4 (ss = ({}, {}))",
7553+ seq.color_config.subsampling_x,
7554+ seq.color_config.subsampling_y
7555+ );
7556+ assert_eq!(
7557+ seq.color_config.bit_depth, 8,
7558+ "{name}: not the 8-bit cell this gate names"
7559+ );
7560+ assert!(
7561+ seq.enable_superres,
7562+ "{name}: the sequence header does not enable superres at all"
7563+ );
7564+ assert_eq!(
7565+ heads.len(),
7566+ per_frame.len(),
7567+ "{name}: {} frame headers, expected {}",
7568+ heads.len(),
7569+ per_frame.len()
7570+ );
7571+ for (i, (h, (denom, coded))) in heads.iter().zip(per_frame).enumerate() {
7572+ // The assertion the phantom cell failed. Without a denominator
7573+ // flag aomenc picks 8 and this is false while the stream still
7574+ // decodes, which is how a whole sweep cell ends up mislabelled.
7575+ assert!(
7576+ h.use_superres,
7577+ "{name}: frame {i} has use_superres = false -- this is an UNSCALED \
7578+ stream and the gate would be measuring something other than the \
7579+ cell it names (class gate-blind-to-feature)"
7580+ );
7581+ assert_eq!(
7582+ h.superres_denom, *denom,
7583+ "{name}: frame {i} PARSED denominator moved from {denom} -- read the \
7584+ header, never the encoder flag (mode 2 chooses its own)"
7585+ );
7586+ assert_eq!(
7587+ (h.upscaled_width as usize, h.frame_height as usize),
7588+ upscaled,
7589+ "{name}: frame {i} upscaled size moved"
7590+ );
7591+ assert_eq!(
7592+ (h.frame_width as usize, h.frame_height as usize),
7593+ (*coded, upscaled.1),
7594+ "{name}: frame {i} CODED size moved -- this is the scaled half of \
7595+ the cell and the part an unsized stream gets wrong"
7596+ );
7597+ // Re-derive rather than only compare literals, so a re-pinned
7598+ // fixture cannot satisfy the numbers above while the scaled-size
7599+ // arithmetic has moved under them. The denominator here is the
7600+ // one PARSED out of this frame, so the check cannot be satisfied by
7601+ // a literal that merely happens to agree.
7602+ let d = u32::from(*denom);
7603+ assert_eq!(
7604+ (h.upscaled_width * 8 + d / 2) / d,
7605+ h.frame_width,
7606+ "{name}: frame {i}: FrameWidth is not (UpscaledWidth*8 + denom/2)/denom -- \
7607+ the scaled size the cell rests on is not the spec's"
7608+ );
7609+ }
7610+
7611+ // --- the pixels -----------------------------------------------------
7612+ let _guard = lock_gate_counters();
7613+ let before_sr = crate::superres::superres_hits();
7614+ let before_scaled = crate::mc::predict_scaled_hits();
7615+ let frames = decode_stream(&stream)
7616+ .unwrap_or_else(|e| panic!("{name}: the pinned 4:4:4 superres stream was refused: {e}"));
7617+ let scaled_up = crate::superres::superres_hits() - before_sr;
7618+ let scaled_mc = crate::mc::predict_scaled_hits() - before_scaled;
7619+ assert!(
7620+ scaled_up > 0,
7621+ "{name}: zero superres_hits -- the upscaler never ran (class \
7622+ gate-blind-to-feature)"
7623+ );
7624+ assert!(
7625+ scaled_mc > 0,
7626+ "{name}: zero predict_scaled_hits -- no inter block read a scaled reference, \
7627+ so the scaled-PREDICTION half of the cell is unexercised (class \
7628+ gate-blind-to-feature)"
7629+ );
7630+ assert_eq!(frames.len(), per_frame.len(), "{name}: decoded frame count");
7631+ for (i, f) in frames.iter().enumerate() {
7632+ // The upscale must have REPLACED the frame: a decoder that returned
7633+ // the coded-width picture fails here, and without this check the
7634+ // chroma-shape assertion below could be satisfied by coded-size
7635+ // planes.
7636+ assert_eq!(
7637+ (f.width, f.height),
7638+ upscaled,
7639+ "{name}: frame {i} came back {}x{}, not the UPSCALED {}x{}",
7640+ f.width,
7641+ f.height,
7642+ upscaled.0,
7643+ upscaled.1
7644+ );
7645+ for (plane, len) in [("U", f.u.len()), ("V", f.v.len())] {
7646+ assert_eq!(
7647+ len,
7648+ upscaled.0 * upscaled.1,
7649+ "{name}: frame {i} {plane} plane is {len} samples; at 4:4:4 upscaled it \
7650+ must be full resolution ({}x{})",
7651+ upscaled.0,
7652+ upscaled.1
7653+ );
7654+ }
7655+ }
7656+ if aomdec_path().is_file() {
7657+ let (decoded, hidden) = decode_all_frames_vs_oracle(&stream, name);
7658+ eprintln!(
7659+ "{name}: {decoded} decode-order frame(s) ({hidden} hidden) byte-exact vs \
7660+ aomdec at {upscaled:?} upscaled from {per_frame:?} (denominator, coded \
7661+ width) per frame, {scaled_up} upscale(s), {scaled_mc} scaled MC block(s); \
7662+ fixture sha256 {fixture_sha}"
7663+ );
7664+ } else {
7665+ eprintln!("{name}: no oracle aomdec, pixel arm skipped");
7666+ }
7667+ heads
7668+ }
7669+
7670+ /// lane-av1superpin (a): `--superres-mode=1 --superres-denominator=9`, the
7671+ /// WIDEST frame-edge margin this 256x128 geometry allows -- coded 228x128
7672+ /// upscaled to 256x128, `denom = 9`, on all four frames.
7673+ ///
7674+ /// **Why 9 and not 12.** The den=12 cell beside it (coded 171x128) is the
7675+ /// one the chroma-format sweep's matrix row ended up pointing at, and
7676+ /// denom 16 (coded 128x128) is the degenerate one where the coded width
7677+ /// equals the superblock grid exactly. Nine is the largest denominator
7678+ /// libaom's own `SUPERRES_NUM` ladder accepts here and still scales, and
7679+ /// it is the member that leaves the most coded samples at the frame's
7680+ /// right edge: `228 * 8 / 256` of the frame is a 28-column upscale margin,
7681+ /// against 12's 85 columns. A wide margin is where a round-trip is most
7682+ /// likely to read past the coded picture, so it is the member worth
7683+ /// pinning next to the den=12 one.
7684+ ///
7685+ /// **The recipe** (sha256
7686+ /// `3d619ee0fda3d9bb9cf8ff0541ba00ea5d83ae38cd8eec1322f8dbbbe6a5ec96`,
7687+ /// 17437 bytes, 4 frames, `testsrc2 256x128 rate=25` yuv444p):
7688+ /// ```text
7689+ /// ffmpeg -f lavfi -i "testsrc2=size=256x128:rate=25" -frames:v 4 \
7690+ /// -pix_fmt yuv444p -strict -1 -f yuv4mpegpipe - | \
7691+ /// aomenc --codec=av1 --passes=1 --end-usage=q --cq-level=20 --cpu-used=2 \
7692+ /// --threads=1 --row-mt=0 --lag-in-frames=0 --kf-max-dist=100 \
7693+ /// --limit=4 --superres-mode=1 --superres-denominator=9 \
7694+ /// --superres-kf-denominator=9 --obu -o - -
7695+ /// ```
7696+ /// Byte-reproducible: re-encoding this recipe reproduces the pinned 17437
7697+ /// bytes exactly, and the PARSED headers of that re-encode were read
7698+ /// (`frame=228x128 upscaled=256 use_superres=true denom=9` on all four) --
7699+ /// which is where the numbers below come from, not from the flags.
7700+ #[test]
7701+ fn a_444_lossy_superres_mode1_den9_stream_decodes_pixel_exact() {
7702+ a_444_superres_arm(
7703+ "a_444_lossy_superres_mode1_den9_stream_decodes_pixel_exact",
7704+ "fixtures/444_lossy_superres_256x128_d9.obu",
7705+ 17437,
7706+ 0x1ff2_fb84_528f_aa05,
7707+ "3d619ee0fda3d9bb9cf8ff0541ba00ea5d83ae38cd8eec1322f8dbbbe6a5ec96",
7708+ (256, 128),
7709+ &[(9, 228); 4],
7710+ );
7711+ }
7712+
7713+ /// lane-av1superpin (b): `--superres-mode=2` (random denominator), coded
7714+ /// 186x128 on the first frame and a DIFFERENT denominator on each frame
7715+ /// after it.
7716+ ///
7717+ /// **This is the cell that makes the flag-vs-header distinction
7718+ /// unavoidable.** Measured on the pinned bytes by parsing every frame
7719+ /// header, `--superres-mode=2` does not honour `--superres-denominator`
7720+ /// at all -- encoding the recipe four times (no denominator, then 9, 12,
7721+ /// 16) produced ONE stream, sha256
7722+ /// `c18496cf3b7db0feca9aa904024cec2115be80b5b94f87c848ac52f9b32ffd97`
7723+ /// (16620 bytes), byte-identical every time -- and the parsed headers of
7724+ /// that stream are:
7725+ ///
7726+ /// ```text
7727+ /// FH0 frame=186x128 upscaled=256 use_superres=true denom=11
7728+ /// FH1 frame=146x128 upscaled=256 use_superres=true denom=14
7729+ /// FH2 frame=137x128 upscaled=256 use_superres=true denom=15
7730+ /// FH3 frame=228x128 upscaled=256 use_superres=true denom=9
7731+ /// ```
7732+ ///
7733+ /// So a gate written against this cell that read the flag instead of the
7734+ /// header would assert `denom == 9` (or 12, or 16) and be wrong on three
7735+ /// of four frames -- or, worse, pass while printing numbers the stream does
7736+ /// not carry. Three of the four parsed denominators (11, 14, 15) are values
7737+ /// no flag on the command line ever named, which is the property the arm
7738+ /// below asserts directly.
7739+ ///
7740+ /// **Why it is worth a gate of its own** beyond the flag-ignoring point:
7741+ /// the den=12 and den=9 cells both scale every frame at one denominator,
7742+ /// so `av1_superres_upscale` is only ever handed one coded-to-upscaled
7743+ /// ratio. This stream hands it four (256->186, 256->146, 256->137,
7744+ /// 256->228) in one decode, and libaom's upscaler picks its filter taps
7745+ /// from `denom` per frame. That is the case a single-denominator gate
7746+ /// cannot reach.
7747+ ///
7748+ /// **The recipe**:
7749+ /// ```text
7750+ /// ffmpeg -f lavfi -i "testsrc2=size=256x128:rate=25" -frames:v 4 \
7751+ /// -pix_fmt yuv444p -strict -1 -f yuv4mpegpipe - | \
7752+ /// aomenc --codec=av1 --passes=1 --end-usage=q --cq-level=20 --cpu-used=2 \
7753+ /// --threads=1 --row-mt=0 --lag-in-frames=0 --kf-max-dist=100 \
7754+ /// --limit=4 --superres-mode=2 --obu -o - -
7755+ /// ```
7756+ #[test]
7757+ fn a_444_lossy_superres_mode2_random_denom_stream_decodes_pixel_exact() {
7758+ const NAME: &str = "a_444_lossy_superres_mode2_random_denom_stream_decodes_pixel_exact";
7759+ let heads = a_444_superres_arm(
7760+ NAME,
7761+ "fixtures/444_lossy_superres_mode2_256x128.obu",
7762+ 16620,
7763+ 0x8cbb_36b2_5e0f_07ab,
7764+ "c18496cf3b7db0feca9aa904024cec2115be80b5b94f87c848ac52f9b32ffd97",
7765+ (256, 128),
7766+ &[(11, 186), (14, 146), (15, 137), (9, 228)],
7767+ );
7768+
7769+ // The flag-ignoring arm. Every denominator libaom accepted on the
7770+ // command line, plus the recipe's own "no denominator at all", must
7771+ // land on the SAME bytes as the pinned fixture. If a future libaom
7772+ // starts honouring the flag under mode 2 this goes red -- and that is
7773+ // the point: the gate above is pinned to parsed denominators, so the
7774+ // day the flag starts mattering the pin has to be re-derived from the
7775+ // stream, not from the recipe.
7776+ if !have_ffmpeg() || !have_aomenc() {
7777+ eprintln!("{NAME}: flag-ignoring arm skipped (no ffmpeg/aomenc)");
7778+ return;
7779+ }
7780+ let y4m = Command::new("ffmpeg")
7781+ .args([
7782+ "-v", "error", "-f", "lavfi", "-i", "testsrc2=size=256x128:rate=25",
7783+ "-frames:v", "4", "-pix_fmt", "yuv444p", "-strict", "-1", "-f",
7784+ "yuv4mpegpipe", "-",
7785+ ])
7786+ .stdin(Stdio::null())
7787+ .stdout(Stdio::piped())
7788+ .stderr(Stdio::piped())
7789+ .output()
7790+ .expect("ffmpeg failed to run");
7791+ assert!(
7792+ y4m.status.success(),
7793+ "{NAME}: ffmpeg refused to generate the y4m source: {}",
7794+ String::from_utf8_lossy(&y4m.stderr)
7795+ );
7796+ let pinned =
7797+ std::fs::read(std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
7798+ .join("fixtures/444_lossy_superres_mode2_256x128.obu"))
7799+ .expect("pinned fixture disappeared between the two reads");
7800+ for extra in [
7801+ vec![],
7802+ vec!["--superres-denominator=9"],
7803+ vec!["--superres-denominator=12"],
7804+ vec!["--superres-denominator=16"],
7805+ ] {
7806+ let mut args: Vec<&str> = vec![
7807+ "--codec=av1",
7808+ "--passes=1",
7809+ "--end-usage=q",
7810+ "--cq-level=20",
7811+ "--cpu-used=2",
7812+ "--threads=1",
7813+ "--row-mt=0",
7814+ "--lag-in-frames=0",
7815+ "--kf-max-dist=100",
7816+ "--limit=4",
7817+ "--superres-mode=2",
7818+ ];
7819+ args.extend(extra.iter().copied());
7820+ args.extend(["--obu", "-o", "-", "-"]);
7821+ let out = run_with_stdin(Command::new(aomenc_path()).args(&args), &y4m.stdout);
7822+ assert!(
7823+ out.status.success(),
7824+ "{NAME}: aomenc refused the recipe with {extra:?}: {}",
7825+ String::from_utf8_lossy(&out.stderr)
7826+ );
7827+ assert_eq!(
7828+ out.stdout.len(),
7829+ pinned.len(),
7830+ "{NAME}: --superres-mode=2 {extra:?} produced {} bytes, pinned is {} -- \
7831+ mode 2 now honours --superres-denominator (or stopped scaling), and this \
7832+ gate's per-frame denominators are pinned to the PARSED headers, not to \
7833+ the recipe",
7834+ out.stdout.len(),
7835+ pinned.len()
7836+ );
7837+ assert_eq!(
7838+ fnv1a64(&out.stdout),
7839+ fnv1a64(&pinned),
7840+ "{NAME}: --superres-mode=2 {extra:?} does NOT reproduce the pinned bytes"
7841+ );
7842+ }
7843+ // And the denominators the stream carries must include values no flag
7844+ // on the command line ever named -- the property that makes "read the
7845+ // parsed header" the only correct source for the table above.
7846+ let flagged = [9u8, 12, 16];
7847+ assert!(
7848+ heads
7849+ .iter()
7850+ .any(|h| h.superres_denom > 0 && !flagged.contains(&h.superres_denom)),
7851+ "{NAME}: every parsed denominator is one of the flags {flagged:?} -- if libaom \
7852+ ever starts honouring --superres-denominator under mode 2 this cell stops being \
7853+ the flag-ignoring one and the pin has to be re-derived"
7854+ );
7855+ eprintln!(
7856+ "{NAME}: --superres-mode=2 with no/9/12/16 denominator flags all reproduce the \
7857+ pinned {} bytes; parsed denominators {:?} (flag values {flagged:?} ignored)",
7858+ pinned.len(),
7859+ heads.iter().map(|h| h.superres_denom).collect::<Vec<_>>()
7860+ );
7861+ }
7862+
7482 /// lane-av1lrflush: the pipelined loop-restoration band release flushes7863 /// lane-av1lrflush: the pipelined loop-restoration band release flushes
7483 /// RU row 0 on the inter frame whose top superblock row carries a7864 /// RU row 0 on the inter frame whose top superblock row carries a
7484 /// 128-root rect -- the exact shape lanes/av1444sb.report.md r2 named as7865 /// 128-root rect -- the exact shape lanes/av1444sb.report.md r2 named as