open-standards/wai/wai-rs/tests/int_video_ops.rs
Transaction Science 946f3beda9
All checks were successful
joule-moq-remote host agent / joule-moq-remote · does this change touch it (pull_request) Successful in 6s
joule-moq-room room service / joule-moq-room · does this change touch it (pull_request) Successful in 7s
joule-moq-web browser client / joule-moq-web · does this change touch it (pull_request) Successful in 6s
standards workspaces build + test / which workspaces this change touches (pull_request) Successful in 6s
wai-app and Create / wai-app · does this change touch it (pull_request) Successful in 6s
wai-audit collector and player integration / does this change touch it (pull_request) Successful in 6s
WAI breaking changes and the conformance runner / WAI breaking · does this change touch WAI (pull_request) Successful in 6s
WAI operation catalogue / does this change touch it (pull_request) Successful in 6s
WAI conformance / does this change touch it (pull_request) Successful in 6s
WAI energy receipts / does this change touch it (pull_request) Successful in 5s
WAI examples / WAI examples · does this change touch them (pull_request) Successful in 6s
wai-fleet service / does this change touch it (pull_request) Successful in 6s
WAI JPEG-AI whole decode (pinned inputs, required) / does this change touch it (pull_request) Successful in 7s
wai-ledger service / does this change touch it (pull_request) Successful in 6s
joule-moq-remote host agent / joule-moq-remote, every target (macos) (pull_request) Successful in 1m25s
wai-pack / wai-pack · does this change touch it (pull_request) Successful in 6s
wai-quantum for JavaScript / does this change touch it (pull_request) Successful in 6s
wai-quantum studio and desktop / does this change touch it (pull_request) Successful in 6s
wai-sink-android native sink library / does this change touch it (pull_request) Successful in 6s
wai-sink-swift native sink package / does this change touch it (pull_request) Successful in 6s
joule-moq-remote host agent / joule-moq-remote, every target (linux-arm64) (pull_request) Successful in 1m57s
repository checks / lockfiles, advisories, tests in CI, workflow hygiene, spec and section references, public text (pull_request) Successful in 2m7s
WAI Studio / does this change touch it (pull_request) Successful in 6s
wai-web reach builds / wai-web reach · does this change touch it (pull_request) Successful in 6s
wai-web wasm32 / does this change touch it (pull_request) Successful in 6s
wai-app and Create / wai-app · tests, clippy, the line language's verifier (linux) (pull_request) Successful in 29s
wai-audit collector and player integration / collector, player integration and the browser end to end · macos (pull_request) Successful in 2m1s
WAI conformance / fast integer kernels, every x86_64 kernel set (AVX-512 silicon) (pull_request) Successful in 2m59s
wai-audit collector and player integration / collector · linux (pull_request) Successful in 1m1s
WAI conformance / cross-arch byte-equality · macos (pull_request) Successful in 26s
standards workspaces build + test / macOS energy backends (pull_request) Successful in 4m19s
WAI conformance / v2 vocabulary · manifests validate as stated (pull_request) Successful in 16s
WAI conformance / codec wrappers · fuzz smoke, text decode limits, C entry points, provenance, the semantic path (pull_request) Successful in 1m4s
WAI breaking changes and the conformance runner / WAI breaking · wire, record and API against main · linux (pull_request) Successful in 1m15s
wai-app and Create / wai-app · Create, browser end-to-end (macos) (pull_request) Successful in 5m15s
joule-moq-room room service / joule-moq-room, every target (pull_request) Successful in 6m19s
wai-fleet service / service · macos (pull_request) Successful in 1m2s
WAI breaking changes and the conformance runner / WAI breaking and conform · their tests, and the independent verifiers through wai conform · linux (pull_request) Successful in 2m46s
WAI conformance / delivery telemetry · CMCD and CMSD against an independent library (pull_request) Successful in 1m43s
wai-ledger service / service · macos (pull_request) Successful in 1m24s
WAI conformance / cross-arch byte-equality · linux-arm64 (pull_request) Successful in 1m2s
WAI operation catalogue / the catalogue and every door · linux (pull_request) Successful in 4m18s
wai-quantum studio and desktop / desktop app · windows (pull_request) Successful in 9m19s
wai-pack / wai-pack · macos (pull_request) Successful in 3m8s
WAI conformance / cross-arch byte-equality · linux (pull_request) Successful in 30s
WAI energy receipts / energy receipts · macos (pull_request) Successful in 7m49s
WAI conformance / C2PA manifest emission · signed, read back, validated (pull_request) Successful in 3m23s
WAI conformance / faces from keypoints under wasm32-wasip2 (wasmtime) (pull_request) Successful in 1m39s
WAI conformance / quantum core and corpus under wasm32-wasip2 (wasmtime) (pull_request) Successful in 3m9s
wai-fleet service / service · linux (pull_request) Successful in 2m5s
wai-quantum studio and desktop / desktop app · macos (pull_request) Successful in 3m28s
wai-ledger service / service · linux (pull_request) Successful in 1m43s
wai-pack / wai-pack · linux (pull_request) Successful in 1m55s
wai-quantum for JavaScript / javascript · linux (pull_request) Successful in 3m0s
WAI Studio / the catalogue's actions, the unit tests and the core · linux (pull_request) Successful in 1m10s
joule-moq-web browser client / joule-moq-web · browser end-to-end (macos) (pull_request) Successful in 16m12s
WAI conformance / each feature of the independent verifiers' steps compiles alone (pull_request) Successful in 11m32s
wai-sink-android native sink library / every ABI, the AAR, and the instrumented tests in a headless emulator · macos (pull_request) Successful in 4m51s
WAI examples / WAI examples · the archive, the readiness check, the browser and Apple · macos (pull_request) Successful in 13m12s
WAI conformance / cargo-class corpora · macos (pull_request) Successful in 15m2s
wai-quantum studio and desktop / studio · browser (pull_request) Successful in 4m3s
WAI conformance / cargo-class corpora · linux-arm64 (pull_request) Successful in 16m34s
wai-web reach builds / wai-web reach builds · the package of record, every output as recorded (pull_request) Successful in 2m40s
standards workspaces build + test / joule-code-rs (joule-code/joule-code-rs, joule-code-rs, linux-arm64, true) (pull_request) Successful in 17m22s
wai-pack / wai-pack · linux-arm64 (pull_request) Successful in 1m44s
wai-quantum studio and desktop / desktop app · linux (pull_request) Successful in 5m46s
wai-pack / wai-pack · the floor through a media prober (pull_request) Successful in 34s
wai-pack / wai-pack · dependency licences (pull_request) Successful in 17s
WAI examples / WAI examples · portable · linux (pull_request) Successful in 9m28s
WAI examples / WAI examples · android-app · linux-arm64 (pull_request) Successful in 5m7s
WAI conformance / cargo-class corpora · linux (pull_request) Successful in 14m49s
wai-web reach builds / wai-web reach builds · this tree's wai-web, every rung on its engine line (pull_request) Successful in 4m34s
wai-sink-android native sink library / the JNI layer and the platform-free half on the host JVM · linux-arm64 (pull_request) Successful in 2m16s
WAI JPEG-AI whole decode (pinned inputs, required) / fetch pinned JPEG-AI inputs → whole decode + pixel hash, required (pull_request) Successful in 11m4s
WAI energy receipts / energy receipts · linux (pull_request) Successful in 13m6s
wai-web wasm32 / wai-web wasm32 build · corpus, menu and writers (pull_request) Successful in 5m58s
wai-sink-swift native sink package / libwai slices, desktop tests, simulator tests · macos (pull_request) Successful in 10m55s
WAI Studio / Studio in Chromium and Firefox · macos (pull_request) Successful in 9m51s
WAI Studio / Studio in WebKit · linux (pull_request) Successful in 10m15s
wai-pack / wai-pack · independent verifier (Python) (pull_request) Successful in 9m21s
WAI energy receipts / energy receipts · linux-arm64 (pull_request) Successful in 30m14s
WAI conformance / independent verifiers · claims, JWP, key discovery and integer payloads (Python) (pull_request) Successful in 1h4m7s
Changelog entries and the conformance workflow's lists move into files a change adds
Every change to WAI added an entry at the head of wai/CHANGELOG.md, and every new module
appended to the hand-kept lists of .github/workflows/wai-conformance.yml. So any two
branches conflicted on those lines and had to merge one after another. This moves both into
files a change adds.

- Changelog entries: a change adds wai/changes/<yyyy-mm-dd>-<slug>.md holding the entry
  text. scripts/changelog-assemble.py renders the entries into CHANGELOG.md's Unreleased
  section for a release, in a stable order. scripts/check-changelog-fragment.py, run by
  repo-checks on every pull request, fails a pull request that changes wai/ and adds or
  edits no entry file directly in wai/changes/ (a release passes, and so does a commit
  with the trailer line "Changelog: none"). A pull request opened before the check reached
  main passes as it is; one opened after it that adds its entry to the head of Unreleased
  fails and is told where the entry goes. wai breaking reads breaking changes from entry
  files too.
- The conformance workflow: each per-module verifier step is a file in
  .github/wai-conformance/verifiers/, each corpus a file in corpora/, and the
  integer-kernel tests carry a tag line. scripts/wai-conformance-lists.py derives every
  list the workflow kept by hand (WAI_FEATURES, the union --bin and --test lists, the
  features-alone lists, the kernel and corpus steps) from those files when the workflow
  runs. Its `selftest --exact` holds the derived lists to the hand lists of the commit
  before this one, and its dry-run prints every command each job runs: the dry-runs before
  and after this change differ only in the gate's path list, which gains the two paths this
  change adds.
- What stays shared, and how to insert there, is written in wai/CONTRIBUTING.md and
  wai/changes/README.md: wai-rs Cargo.toml features and [[test]] entries, lib.rs, registry
  rows, wai/.gitattributes and the live corpus table take an insert beside the related
  entry, never at the end.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-05 01:41:29 -05:00

454 lines
23 KiB
Rust
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// wai-conformance: int-kernels int-kernels-amd
//! `WIV1` (`wai.video.int_motion`, and the P-frames of `wai.video.int_hyper`) through the
//! integer operator set: the stage operators of `extensions/integer-operators.md` §13.5,
//! E1 `rans64_decode`, E10 `mc_predict` and E9 `residual_step`, with no graph.
//!
//! A clip runs no network, so it has no lowering and no bounds certificate. What it has
//! is its **stage form** (`int_video::ops::stages`): the operands its decode hands each
//! stage operator. The operators' own conformance vectors are
//! `ops-conformance/stage/`, which `wai-verify vectors verify` and the independent
//! checker (`tools/wai_ops_check.py`) reproduce byte for byte.
#![cfg(all(feature = "video_int", feature = "legacy_int"))]
use std::path::{Path, PathBuf};
use serde_json::Value;
use wai::int_payload::wire;
use wai::int_video::{self, ops, VideoStream};
use wai_ops::stage::rans64::{self, End, Escape, TableRule};
fn wai() -> PathBuf {
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("..")
}
fn read(p: &Path) -> Vec<u8> {
std::fs::read(p).unwrap_or_else(|e| panic!("{}: {e}", p.display()))
}
/// A `WAI1` envelope's capability and payload.
fn envelope(env: &[u8]) -> Option<(String, Vec<u8>)> {
if env.get(..4)? != b"WAI1" {
return None;
}
let ml = u32::from_le_bytes(env.get(4..8)?.try_into().ok()?) as usize;
let man: Value = serde_json::from_slice(env.get(8..8 + ml)?).ok()?;
let at = 8 + ml;
let pl = u32::from_le_bytes(env.get(at..at + 4)?.try_into().ok()?) as usize;
let cap = man["model_requirement"]["capability"].as_str()?.to_string();
Some((cap, env.get(at + 4..at + 4 + pl)?.to_vec()))
}
/// Every committed `WIV1` payload: the vectors of int-payload-conformance and every
/// base of the staged corpus that carries one, each with its name and its keyframe.
fn committed() -> Vec<(String, Vec<u8>)> {
let idx: Value = serde_json::from_slice(&read(&wai().join("int-payload-conformance/vectors.json"))).unwrap();
let mut out = Vec::new();
for v in idx["vectors"].as_array().unwrap() {
let p = read(&wai().join(v["payload"].as_str().unwrap()));
if p.starts_with(b"WIV1") {
out.push((v["name"].as_str().unwrap().to_string(), p));
}
}
let cases = wai().join("staged-conformance/cases");
let mut dirs: Vec<_> = std::fs::read_dir(&cases).unwrap().map(|e| e.unwrap().path()).collect();
dirs.sort();
for d in dirs {
let mut files: Vec<_> = std::fs::read_dir(&d).unwrap().map(|e| e.unwrap().path()).filter(|p| p.extension().is_some_and(|x| x == "wai")).collect();
files.sort();
for f in files {
if let Some((_, p)) = envelope(&read(&f))
&& p.starts_with(b"WIV1")
{
out.push((format!("{}/{}", d.file_name().unwrap().to_string_lossy(), f.file_name().unwrap().to_string_lossy()), p));
}
}
}
out
}
fn stream<'a>(v: &'a wire::Wiv1<'a>) -> VideoStream<'a> {
VideoStream {
n_frames: v.n_frames as usize, h: v.h as usize, w: v.w as usize, block: v.block as usize,
q: v.q as i64, offset: v.offset, mv_precision: v.mv_precision, cdf: &v.cdf, mvs: &v.mvs, rans: v.rans,
}
}
/// E9's 32-bit form holds on `[lo, hi]` (§5E E9, *Width*): `hi − lo ≤ 65535`, and
/// `lo − 131070` and `hi + 131070` fit `i32`.
fn e9_width_holds((lo, hi): (i64, i64)) -> bool {
lo < hi && hi - lo <= 65535 && lo - 131070 >= i32::MIN as i64 && hi + 131070 <= i32::MAX as i64
}
/// Gate 1. Every committed clip has its stage form, and the form is the payload's: E1's
/// table and offset are the clip's, its modes `t4097`, `lenient`, `canonical`; E10's
/// precision is the clip's `mv_precision`; E9's step is `q` on `[0, 255]`, a range on
/// which E9's 32-bit form holds. The committed clips reach both precisions and both
/// keyframes.
#[test]
fn every_committed_clip_has_its_stage_form() {
assert_eq!((ops::MODES.table, ops::MODES.escape, ops::MODES.end), (TableRule::T4097, Escape::Lenient, End::Canonical));
assert_eq!((ops::RANGE, ops::GREY), ((0, 255), 128));
assert!(e9_width_holds(ops::RANGE), "E9's 32-bit form holds on [0, 255]");
let all = committed();
let (mut precisions, mut keyframes) = (std::collections::BTreeSet::new(), std::collections::BTreeSet::new());
for (name, p) in &all {
let v = wire::parse_wiv1(p, None).unwrap_or_else(|e| panic!("{name}: {e}"));
let s = stream(&v);
let f = ops::stages(&s).unwrap_or_else(|e| panic!("{name}: {e}"));
assert_eq!(f.cdf, &v.cdf[..], "{name}: E1's table");
assert_eq!(f.offset, v.offset as i64, "{name}: E1's offset");
assert!(rans64::table_ok(f.cdf, TableRule::T4097), "{name}: E1's t4097 rule admits the table");
assert_eq!(f.precision, v.mv_precision as u32, "{name}: E10's precision");
assert_eq!(f.q, v.q as i64, "{name}: E9's step");
assert_eq!((f.frame, f.nb), (v.h as usize * v.w as usize * 3, (v.h.div_ceil(v.block) * v.w.div_ceil(v.block)) as usize), "{name}: the frame and the block grid");
precisions.insert(v.mv_precision);
keyframes.insert(v.keyframe);
}
assert!(all.len() >= 3 + 3, "the three vectors and the staged corpus's video bases: {}", all.len());
assert_eq!(precisions.into_iter().collect::<Vec<_>>(), [1, 2], "whole-pel and half-pel");
assert_eq!(keyframes.into_iter().collect::<Vec<_>>(), [0, 1], "both keyframes");
println!("{} committed WIV1 payloads, each with its stage form", all.len());
}
/// Gate 1. E1's table rule `t4097` is `WIV1`'s: the parser and the decode's checks
/// (`int_video::check`, through `ops::stages`) admit a table exactly when E1 does, on
/// the committed tables and on each rule broken once.
#[test]
fn e1s_table_rule_is_the_payloads() {
let (_, p) = committed().into_iter().find(|(n, _)| n == "int_video").unwrap();
let v = wire::parse_wiv1(&p, Some(0)).unwrap();
let ok = v.cdf.clone();
let n = ok.len();
let mut cases: Vec<(&str, Vec<i32>)> = vec![("the committed table", ok.clone())];
cases.push(("two entries", vec![0, 65536]));
cases.push(("three entries", vec![0, 1, 65536]));
cases.push(("4097 entries", (0..4097).map(|k| (k * 16).min(65536)).collect()));
// Strictly increasing, so that only its length breaks a rule.
cases.push(("4098 entries", (0..4098).map(|k| if k == 4097 { 65536 } else { k * 15 }).collect()));
let mut c = ok.clone();
c[0] = 1;
cases.push(("a first entry of 1", c));
let mut c = ok.clone();
c[n - 1] = 65535;
cases.push(("a last entry of 65535", c));
let mut c = ok.clone();
c[n - 1] = 65537;
cases.push(("a last entry of 65537", c));
let mut c = ok.clone();
c[2] = c[1];
cases.push(("two equal entries", c));
let mut c = ok.clone();
c.swap(1, 2);
cases.push(("a decrease", c));
for (what, cdf) in cases {
let e1 = rans64::table_ok(&cdf, TableRule::T4097);
// The decode's checks, on the clip with this table.
let s = VideoStream { cdf: &cdf, ..stream(&v) };
let stages = ops::stages(&s);
assert_eq!(stages.is_ok(), e1, "{what}: the decode's checks and E1 disagree ({stages:?})");
// The parser, where the header can carry the table's length.
if (3..=4097).contains(&cdf.len()) {
let w = wire::Wiv1 { cdf: cdf.clone(), ..v.clone() };
let bytes = wire::write_wiv1(&w);
let parsed = wire::parse_wiv1(&bytes, Some(0));
assert_eq!(parsed.is_ok(), e1, "{what}: the parser and E1 disagree");
if !e1 {
assert_eq!(parsed.unwrap_err().refusal.code(), "cdf_malformed", "{what}");
}
} else {
assert!(!e1, "{what}: E1 refuses a length outside 3..=4097");
}
}
}
/// Gate 1. The operators' conformance vectors cover what this stage form uses: E1 under
/// `t4097`, `lenient` and `canonical`, decoded and refused; E10 at both precisions on
/// three channels; E9 on `[0, 255]`, at `r = ±R` and `±(R + 1)`.
#[test]
fn the_stage_vectors_cover_the_stage_form() {
let lines = |op: &str| -> Vec<Value> {
let t = String::from_utf8(read(&wai().join(format!("ops-conformance/stage/{op}.jsonl")))).unwrap();
t.lines().map(|l| serde_json::from_str(l).unwrap()).collect()
};
let e1 = lines("rans64_decode");
let ours = |v: &&Value| v["modes"]["table"] == "t4097" && v["modes"]["escape"] == "lenient" && v["modes"]["end"] == "canonical";
assert!(e1.iter().filter(ours).any(|v| v["expect"].get("refuse").is_none()), "E1 decodes under the stage form's modes");
for code in ["stream_exhausted", "stream_noncanonical"] {
assert!(e1.iter().filter(ours).any(|v| v["expect"]["refuse"] == code), "E1 refuses {code} under the stage form's modes");
}
let e10 = lines("mc_predict");
for p in ["1", "2"] {
assert!(e10.iter().any(|v| v["in"]["precision"] == p && v["in"]["frame"]["shape"][0] == "3"), "E10 at precision {p}");
}
let e9: Vec<Value> = lines("residual_step").into_iter().filter(|v| v["in"]["lo"] == "0" && v["in"]["hi"] == "255").collect();
let at = |k: i64| e9.iter().any(|v| {
let q: i64 = v["in"]["q"].as_str().unwrap().parse().unwrap();
let r: i64 = v["in"]["r"].as_str().unwrap().parse().unwrap();
r == k * (255 / q + 1) || r == k * (255 / q + 2)
});
assert!(at(1) && at(-1), "E9 on [0, 255] at ±R and ±(R + 1)");
}
fn hex(b: &[u8]) -> String {
b.iter().map(|x| format!("{x:02x}")).collect()
}
fn json(rel: &str) -> Value {
serde_json::from_slice(&read(&wai().join(rel))).unwrap()
}
/// Every frame's SHA-256 and the clip's, against the sidecar entry `key` of
/// `int_video/sha256.json`; every frame's FNV-1a and the clip's, against the export's
/// `conf`.
fn assert_clip(frames: &[Vec<u8>], key: &str, conf: &str) {
use sha2::Digest;
let side = json("wai-rs/tests/vectors/int_video/sha256.json");
let conf = json(&format!("wai-rs/tests/vectors/int_video/{conf}"));
let (want, fnv) = (side[key]["frames_sha256"].as_array().unwrap(), conf["frame_hashes"].as_array().unwrap());
assert_eq!((want.len(), fnv.len()), (frames.len(), frames.len()), "{key}: frame count");
let (mut all, mut all_fnv) = (Vec::new(), Vec::new());
for (i, f) in frames.iter().enumerate() {
let d = sha2::Sha256::digest(f);
assert_eq!(hex(&d), want[i].as_str().unwrap(), "{key}: frame {i}");
let h = format!("{:016x}", wai::int_video::fnv1a(f));
assert_eq!(h, fnv[i].as_str().unwrap(), "{key}: frame {i}'s FNV-1a");
all.extend_from_slice(&d);
all_fnv.extend((0..8).map(|j| u8::from_str_radix(&h[2 * j..2 * j + 2], 16).unwrap()));
}
assert_eq!(hex(&sha2::Sha256::digest(&all)), side[key]["clip_sha256"].as_str().unwrap(), "{key}: the clip's SHA-256");
assert_eq!(format!("{:016x}", wai::int_video::fnv1a(&all_fnv)), conf["clip_hash"].as_str().unwrap(), "{key}: the clip's FNV-1a");
}
/// The tiny conditional codec's set, the one every committed keyframe-1 clip decodes
/// against, as a sink holds it.
fn tiny_set() -> std::collections::BTreeMap<String, Vec<u8>> {
[("entropy.json", "tiny_entropy.json"), ("g_s.whm", "tiny_g_s.bin"), ("h_s.whm", "tiny_h_s.bin")]
.into_iter()
.map(|(k, f)| (k.to_string(), read(&wai().join("wai-rs/tests/vectors/int_hyper").join(f))))
.collect()
}
/// A clip's decode through the stage operators and the direct decode, frame for frame,
/// or their refusals: keyframe 0 from grey; keyframe 1 on the I-frame the sink decodes
/// from the nested `WIH1` (`frame0`).
fn both(s: &VideoStream<'_>, frame0: Option<&[u8]>) -> (Result<Vec<Vec<u8>>, &'static str>, Result<Vec<Vec<u8>>, &'static str>) {
match frame0 {
None => (ops::decode_typed(s).map(|c| c.frames), int_video::legacy::decode_typed(s).map(|c| c.frames)),
Some(f0) => (ops::decode_pframes_typed(f0, s).map(|c| c.frames), int_video::legacy::decode_pframes_typed(f0, s).map(|c| c.frames)),
}
}
/// Gate 2: golden equality. Every committed `WIV1` payload, the staged corpus's bases
/// among them, decodes through the stage operators to the frames the direct decode
/// gives, keyframe 1 on the I-frame the sink decodes; and the three vectors of
/// int-payload-conformance to every digest the corpus commits for them, the half-pel
/// real footage among them: every frame's and the clip's SHA-256 and FNV-1a, from the
/// payload and, for keyframe 0, from the JSON form it is converted from.
#[test]
fn every_committed_clip_decodes_through_the_stage_operators_as_directly() {
use wai::int_payload::{decode_int, resolve, IntDecoded};
use wai::pin::HeldBytes;
let set = tiny_set();
let view: std::collections::BTreeMap<String, &[u8]> = set.iter().map(|(k, v)| (k.clone(), v.as_slice())).collect();
let params = resolve(wai::codecs::CAP_NEURAL_INT_HYPER, None, &[HeldBytes::Set { listing: b"", files: &view }], None).unwrap();
let mut decoded = 0;
for (name, p) in committed() {
let v = wire::parse_wiv1(&p, None).unwrap();
let s = stream(&v);
let frame0 = (v.keyframe == 1).then(|| match decode_int(wai::codecs::CAP_VIDEO_INT_HYPER, &p, &params) {
Ok(IntDecoded::Video { mut frames, .. }) => frames.swap_remove(0),
other => panic!("{name}: the keyframe: {other:?}"),
});
let (through, direct) = both(&s, frame0.as_deref());
let frames = through.unwrap_or_else(|e| panic!("{name}: {e}"));
assert!(Ok(&frames) == direct.as_ref(), "{name}: the decode through the stage operators differs from the direct decode");
match name.as_str() {
"int_video" => assert_clip(&frames, "int_video", "int_video.json"),
"video_real" => assert_clip(&frames, "video_real", "video_real.json"),
"video_nk" => assert_clip(&frames, "video_nk", "video_nk.json"),
_ => {}
}
decoded += 1;
}
assert_eq!(decoded, committed().len());
// The JSON forms of the keyframe-0 vectors.
for (bs, key, conf) in [("int_video_bitstream.json", "int_video", "int_video.json"), ("video_real_bitstream.json", "video_real", "video_real.json")] {
let j = int_video::VideoJson::from_value(&json(&format!("wai-rs/tests/vectors/int_video/{bs}"))).unwrap();
let clip = ops::decode_typed(&j.stream()).unwrap();
assert_clip(&clip.frames, key, conf);
}
println!("{decoded} committed clips through the stage operators, each as directly");
}
/// The code integer-payloads §8 gives a refusal of the clip's decode, as the sink maps it
/// (`int_payload`'s `WIV1` decode).
fn code_of(e: &str) -> &'static str {
match e {
int_video::STREAM_EXHAUSTED => "stream_exhausted",
int_video::STREAM_NONCANONICAL => "stream_noncanonical",
_ => "field_range",
}
}
/// Both decodes of `s` refuse alike, or decode alike; returns the refusal, if any.
fn alike(what: &str, s: &VideoStream<'_>, frame0: Option<&[u8]>) -> Option<&'static str> {
let (through, direct) = both(s, frame0);
assert!(through == direct, "{what}: through the stage operators {:?}, directly {:?}", through.as_ref().err(), direct.as_ref().err());
through.err()
}
/// Gate 3: refusal parity. (a) Every committed refusal fixture of `WIV1` that the decode
/// refuses (int-payload-conformance, layer `decode`) is refused by both decodes alike,
/// with the fixture's code, and by the sink. (b) One fixture per refusal the decode
/// makes, in its order, each one change from a committed clip: both refuse with the same
/// reason. (c) The committed clip's stream cut at each of its last 64 words and at
/// words across it, and bytes changed across it: both decode alike or refuse alike.
#[test]
fn every_refusal_is_the_direct_decodes() {
use wai::int_payload::{decode_int, resolve, IntDecoded};
use wai::pin::HeldBytes;
let set = tiny_set();
let view: std::collections::BTreeMap<String, &[u8]> = set.iter().map(|(k, v)| (k.clone(), v.as_slice())).collect();
let params = resolve(wai::codecs::CAP_NEURAL_INT_HYPER, None, &[HeldBytes::Set { listing: b"", files: &view }], None).unwrap();
// (a)
let idx = json("int-payload-conformance/vectors.json");
let mut fixtures = 0;
for f in idx["refusals"].as_array().unwrap() {
let cap = f["capability"].as_str().unwrap();
if !cap.starts_with("wai.video.") || f["layer"] != "decode" {
continue;
}
let (name, want) = (f["name"].as_str().unwrap(), f["code"].as_str().unwrap());
let p = read(&wai().join(f["payload"].as_str().unwrap()));
let none = wai::int_payload::IntParams::none();
let sink = decode_int(cap, &p, if cap == wai::codecs::CAP_VIDEO_INT_HYPER { &params } else { &none });
assert_eq!(sink.as_ref().err().map(|e| e.code()), Some(want), "{name}: the sink");
let v = wire::parse_wiv1(&p, None).unwrap();
let s = stream(&v);
let frame0 = match v.wih1 {
None => None,
Some(k) => match decode_int(wai::codecs::CAP_NEURAL_INT_HYPER, k, &params) {
// The keyframe's image is the clip's size; otherwise the sink refuses it before
// the P-frames (geometry_mismatch), which neither decode below reaches.
Ok(IntDecoded::Image { rgb, width, height }) if (width, height) == (v.w, v.h) => Some(rgb),
_ => {
assert_eq!(want, "geometry_mismatch", "{name}");
continue;
}
},
};
let r = alike(name, &s, frame0.as_deref()).unwrap_or_else(|| panic!("{name}: both decode"));
assert_eq!(code_of(r), want, "{name}");
fixtures += 1;
}
assert!(fixtures >= 4, "{fixtures} decode fixtures");
// (b)
let (_, p) = committed().into_iter().find(|(n, _)| n == "int_video").unwrap();
let v = wire::parse_wiv1(&p, Some(0)).unwrap();
let ok = stream(&v);
let mut cdf_short = v.cdf.clone();
cdf_short.truncate(2);
let mut cdf_first = v.cdf.clone();
cdf_first[0] = 1;
let mut cdf_flat = v.cdf.clone();
cdf_flat[2] = cdf_flat[1];
let mvs_short = &v.mvs[..v.mvs.len() - 1];
let rans_odd = &v.rans[..v.rans.len() - 1];
let rans_short = &v.rans[..4];
let rans_cut = &v.rans[..v.rans.len() - 4];
let mut rans_long = v.rans.to_vec();
rans_long.extend([0, 0, 0, 0]);
let mut rans_state = v.rans.to_vec();
rans_state[0] ^= 1;
let cases: Vec<(&str, VideoStream<'_>, Option<&str>)> = vec![
("n_frames 0", VideoStream { n_frames: 0, ..ok }, None),
("h 0", VideoStream { h: 0, ..ok }, None),
("w 0", VideoStream { w: 0, ..ok }, None),
("block 0", VideoStream { block: 0, ..ok }, None),
("q 0", VideoStream { q: 0, ..ok }, None),
("q 65536", VideoStream { q: 65_536, ..ok }, None),
("mv_precision 3", VideoStream { mv_precision: 3, ..ok }, None),
("a table of two entries", VideoStream { cdf: &cdf_short, ..ok }, None),
("a table from 1", VideoStream { cdf: &cdf_first, ..ok }, None),
("a table with two equal entries", VideoStream { cdf: &cdf_flat, ..ok }, None),
("a frame past 2^26", VideoStream { h: 4097, w: 5462, ..ok }, None),
("a clip past 2^30", VideoStream { n_frames: 1 << 11, h: 512, w: 512, ..ok }, None),
("frames past 2^20", VideoStream { n_frames: (1 << 20) + 1, h: 1, w: 1, ..ok }, None),
("a vector field past 2^26", VideoStream { n_frames: 1 << 20, h: 16, w: 16, block: 1, ..ok }, None),
("a vector short", VideoStream { mvs: mvs_short, ..ok }, None),
("a stream not of whole words", VideoStream { rans: rans_odd, ..ok }, None),
("a stream of one word", VideoStream { rans: rans_short, ..ok }, None),
("a stream cut by a word", VideoStream { rans: rans_cut, ..ok }, Some("stream_exhausted")),
("a word left unread", VideoStream { rans: &rans_long, ..ok }, Some("stream_noncanonical")),
("another end state", VideoStream { rans: &rans_state, ..ok }, None),
];
for (what, s, want) in &cases {
let r = alike(what, s, None).unwrap_or_else(|| panic!("{what}: both decode"));
if let Some(want) = want {
assert_eq!(code_of(r), *want, "{what}");
}
}
// The I-frame of keyframe 1 that is not the clip's size.
let (_, nk) = committed().into_iter().find(|(n, _)| n == "video_nk").unwrap();
let vk = wire::parse_wiv1(&nk, Some(1)).unwrap();
let wrong = vec![0u8; vk.h as usize * vk.w as usize * 3 - 1];
assert!(alike("an I-frame a byte short", &stream(&vk), Some(&wrong)).is_some());
// (c)
// Every word of the stream's last 64, and every 61st before them; bytes changed every
// 31 bytes. (The clip's stream is some thousands of words; each case decodes it twice.)
let mut n = 0;
let words = v.rans.len() / 4;
for k in (0..words).filter(|&k| k + 64 >= words || k % 61 == 0) {
let cut = 4 * k;
alike(&format!("the stream cut to {cut} bytes"), &VideoStream { rans: &v.rans[..cut], ..ok }, None);
n += 1;
}
for at in (0..v.rans.len()).step_by(31) {
let mut r = v.rans.to_vec();
r[at] ^= 1 << (at % 8);
alike(&format!("byte {at} changed"), &VideoStream { rans: &r, ..ok }, None);
n += 1;
}
println!("{fixtures} committed fixtures, {} per-rule fixtures and {n} changed streams refused alike", cases.len() + 1);
}
/// Gate 5: the 32-bit form. Every committed clip decodes with E9 in its 32-bit form
/// (`ops::decode_typed_i32`, the arithmetic a 32-bit fabric runs) to the frames the exact
/// form gives, keyframe 1 on its I-frame; and so does every refusal fixture of gate 3's
/// sweep, cut and changed streams alike.
#[cfg(feature = "i32_forms")]
#[test]
fn the_32bit_form_decodes_every_committed_clip_alike() {
use wai::int_payload::{decode_int, resolve, IntDecoded};
use wai::pin::HeldBytes;
let set = tiny_set();
let view: std::collections::BTreeMap<String, &[u8]> = set.iter().map(|(k, v)| (k.clone(), v.as_slice())).collect();
let params = resolve(wai::codecs::CAP_NEURAL_INT_HYPER, None, &[HeldBytes::Set { listing: b"", files: &view }], None).unwrap();
let narrow = |s: &VideoStream<'_>, f0: Option<&[u8]>| match f0 {
None => ops::decode_typed_i32(s).map(|c| c.frames),
Some(f0) => ops::decode_pframes_typed_i32(f0, s).map(|c| c.frames),
};
let mut n = 0;
for (name, p) in committed() {
let v = wire::parse_wiv1(&p, None).unwrap();
let s = stream(&v);
let frame0 = (v.keyframe == 1).then(|| match decode_int(wai::codecs::CAP_VIDEO_INT_HYPER, &p, &params) {
Ok(IntDecoded::Video { mut frames, .. }) => frames.swap_remove(0),
other => panic!("{name}: the keyframe: {other:?}"),
});
let (exact, _) = both(&s, frame0.as_deref());
assert!(narrow(&s, frame0.as_deref()) == exact, "{name}: the 32-bit form differs from the exact form");
n += 1;
}
let (_, p) = committed().into_iter().find(|(n, _)| n == "int_video").unwrap();
let v = wire::parse_wiv1(&p, Some(0)).unwrap();
let ok = stream(&v);
for at in (0..v.rans.len()).step_by(31) {
let mut r = v.rans.to_vec();
r[at] ^= 1 << (at % 8);
let s = VideoStream { rans: &r, ..ok };
assert!(narrow(&s, None) == both(&s, None).0, "byte {at} changed: the 32-bit form differs");
n += 1;
}
println!("{n} clips through the 32-bit form, each as through the exact form");
}