A stage-only decode has no fast kernel behind an interpreter. What a faster
fabric runs instead is E9's 32-bit form (§5E E9, Width): the form D3b's
integer GPU legs take. Gate 5 holds it to the exact form.
- wai-ops: residual_step_i32 and i32_form_fits behind the new i32_forms
feature (and cfg(test), cfg(kani), so the unit tests and K29 keep them);
residual_step_by_i32, E9 through its 32-bit form for a driver whose
operands are i64. wai-verify declares the feature name its included
sources test.
- wai-rs i32_forms: int_video::ops::{decode_typed_i32,
decode_pframes_typed_i32} and staged::wir1::ops::apply_unit_i32, the
drivers with E9 as a parameter.
- Parity: every committed clip and layer through the 32-bit form as through
the exact form (tests/int_video_ops.rs, tests/wir1_ops.rs), changed
streams too; the differential compares the 32-bit form on every random
case; byte-exact-conformance (i32_forms on by default) decodes WIV1 and the
staged sets through it to the pinned digests on every leg.
- CI: the fast-kernel steps carry i32_forms, appended so gate.sh's grep
still finds them.
Gate 4's falsification, for the record: ops::frame's block grid taken as
w / block, which only a grid with a partial last column reaches (every
committed clip's sides are multiples of its block). Gates 1 to 3 stayed
green (int_video_ops 5, wir1_ops 5); int_video_random_clips_decode_alike
failed at seed 3372 (h 5, w 3, block 4). Timed 300 s, seed 20261017: debug
2,402,935 clips and 408,439 layers, release 11,948,953 and 3,322,036,
alike.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
453 lines
23 KiB
Rust
453 lines
23 KiB
Rust
//! `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, ¶ms) {
|
||
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 { ¶ms } 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, ¶ms) {
|
||
// 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, ¶ms) {
|
||
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");
|
||
}
|