#!/usr/bin/env python3 """Independent verifier for the integer payload corpus (extensions/integer-payloads.md). Python standard library only; it shares no code with wai-rs. Written from the extension's text, it: - parses every committed payload (WIV1, WIH1, WIA1, WIS1) with the rules and in the check order of the extension's §2-§6, and requires each to parse; - converts each payload's JSON source independently (§10) and requires the result to equal the committed payload byte for byte, and the payload's SHA-256 to be the one the index states; - decodes the WIV1 payloads with keyframe 0 in full - the rANS decoder with its bypass escape, its stop at the first exhausted symbol and its canonical end, whole-pel and half-pel motion compensation - and requires every frame's SHA-256 and the clip's to be the ones the vector's sha256.json states; - parses every refusal fixture and requires a parse-layer fixture to be refused with its code, and a decode-layer fixture to parse; a decode-layer fixture of WIV1 keyframe 0 is decoded and must be refused with its code (stream_exhausted, stream_noncanonical). Not decoded independently: WIH1, WIV1 with keyframe 1 (its keyframe is a WIH1), WIA1 and WIS1, whose decodes run integer networks this script does not implement. Their decoded outputs are checked by wai-rs against the same sidecars, on every leg of the cross-architecture matrix for WIV1 and WIH1. The decode-layer fixtures of those formats are checked here to parse, and by wai-rs for their codes. Frame streams (extensions/frame-stream.md, the corpus frame-stream/), from that text and integer-payloads.md §16: - splits every clip a stream names again, with its own rANS encoder (the encoder of integer-payloads.md §2, state from 2^31), and requires each committed frame payload byte for byte; splits the neural clip and joins its frames, and requires both; - parses every WAI1 envelope and makes frame-stream §5's checks over every stream, in order, with the BLAKE3 content hash of jwp-receipts §2.1 (../claim-conformance/ _blake3.py), decoding every WIF1 frame of keyframe 0 (its own stream, its canonical end, the prediction from the previous decoded frame) and requiring each frame's SHA-256 and, for a clean split, the whole clip's frames byte for byte; a WIF1 frame of keyframe 1 and a WNF1 frame are parsed and held to their group's geometry, not decoded; - refuses every payload fixture with its code at its layer, every split and join refusal with its code, and checks every catalog case (moq-streaming-format §4.3). NMSF tracks (extensions/frame-stream.md §9, the corpus frame-stream/nmsf/): it reads every NMSF object (the 26-byte header of draft-herz-moq-nmsf-01 §3.2, its components §3.6) and catalog entry with its own parser, maps every rewrap stream and requires each committed frame object's WNF1 payload and manifest (its frame member, pin and capability), admits the frame objects with its own frame-stream §5 checks (number and capture_ms included), unwraps them and requires the NMSF objects back byte for byte, and checks every binding, catalog, unwrap and links case, and that every code of §9.6 is given by one. Conditional inter-frame video (integer-payloads.md §17, the corpus int-cond/), from that text and integer-operators.md §13.2: - parses the WCB1 bundle (its WHM1 networks and its tables, read as JSON that holds only integers), plans each network on a frame's shapes, and decodes the committed WIC1 clip in full: the context network over the previous frame's feature (zero for frame 0), the entropy context network, the rANS decode of each latent element against its σ's bucket, the conditional decoder and the synthesis with its RGB8 step, every network's integer arithmetic and the check ranges of whm1/1 written here; requires every frame's SHA-256 to be the sidecar's; - splits the clip and joins its frames, makes frame-stream §5's checks over every stream of wai.video.int_cond.frame and decodes its frames, refuses every fixture with its code at its layer, every set fixture and every join refusal. python3 int-payload-conformance/int_payload_verify.py int-payload-conformance python3 int-payload-conformance/int_payload_verify.py --frame-stream int-payload-conformance python3 int-payload-conformance/int_payload_verify.py --int-cond int-payload-conformance Exit status 1 on any failure. """ import base64 import hashlib import json import math import os import struct import sys MAGICS = (b"WIV1", b"WIH1", b"WIA1", b"WIS1", b"WIF1", b"WCF1", b"WIC1") MAX_TENSOR = 1 << 26 MAX_CLIP = 1 << 30 MAX_FRAMES = 1 << 20 MAX_MEDIAN_Q = 1 << 47 CAP_MAGIC = { "wai.video.int_motion": b"WIV1", "wai.video.int_hyper": b"WIV1", "wai.neural.int_hyper": b"WIH1", "wai.audio.int_codec": b"WIA1", "wai.splat.int_codec": b"WIS1", } class Refused(Exception): def __init__(self, code, why): super().__init__(f"{code}: {why}") self.code = code class Reader: def __init__(self, b, p): self.b, self.p = b, p def left(self): return len(self.b) - self.p def take(self, n): if n > self.left(): raise Refused("stream_length", "the payload ends inside a section") s = self.b[self.p:self.p + n] self.p += n return s def u8(self): return self.take(1)[0] def u16(self): return struct.unpack("= b for a, b in zip(t, t[1:])): raise Refused("cdf_malformed", "not strictly increasing") def check_magic(p, want): m = p[:4] if len(m) == 4 and m == want: return if len(m) == 4 and m in MAGICS: raise Refused("format_capability_mismatch", "another integer format") raise Refused("bad_magic", "not the capability's magic") def opening(p, magic, header): check_magic(p, magic) if len(p) < 5: raise Refused("stream_length", "no version byte") if p[4] != 1: raise Refused("version", "a version this revision does not define") if len(p) < header: raise Refused("stream_length", "the payload ends inside its fixed header") return Reader(p, 5) def too_large(dims, cap): n = 1 for d in dims: n *= d if n > cap: raise Refused("too_large", "over the cap") def parse_wiv1(p, want_keyframe=None): r = opening(p, b"WIV1", 36) mvp, kf, res0 = r.u8(), r.u8(), r.u8() n_frames, h, w, block, q = r.u32(), r.u32(), r.u32(), r.u32(), r.u32() offset, cdf_len, res1 = r.i32(), r.u16(), r.u16() if res0 or res1: raise Refused("reserved_nonzero", "reserved") if mvp not in (1, 2): raise Refused("field_range", "mv_precision") if kf > 1: raise Refused("field_range", "keyframe") if 0 in (n_frames, h, w): raise Refused("field_range", "n_frames, h or w") if block == 0: raise Refused("field_range", "block") if not 1 <= q <= 65535: raise Refused("field_range", "q") if not 3 <= cdf_len <= 4097: raise Refused("field_range", "cdf_len") if n_frames == 1 and (mvp != 1 or block != 1): raise Refused("field_range", "a one-frame clip has mv_precision 1 and block 1") if want_keyframe is not None and want_keyframe != kf: raise Refused("keyframe_capability_mismatch", "keyframe") too_large([h, w, 3], MAX_TENSOR) too_large([n_frames, h, w, 3], MAX_CLIP) too_large([n_frames], MAX_FRAMES) nb = -(-h // block) * -(-w // block) too_large([2, n_frames - 1, nb], MAX_TENSOR) cdf = r.cdf(cdf_len) raw = r.take((n_frames - 1) * nb * 4) mvs = [struct.unpack_from(" MAX_MEDIAN_Q for x in med): raise Refused("field_range", "median") cdfs = [r.cdf(cdf_len) for _ in range(n_ch)] rans = r.stream() r.end() return dict(afrac=afrac, sr=sr, n_ch=n_ch, lat_len=lat_len, t_out=t_out, offset=offset, median_q=med, cdfs=cdfs, rans=rans) def parse_wis1(p): r = opening(p, b"WIS1", 32) afrac, res = r.u8(), r.u16() c, sh, sw, s, attr, n_g = (r.u32() for _ in range(6)) if res: raise Refused("reserved_nonzero", "reserved") if not 1 <= afrac <= 30: raise Refused("field_range", "afrac") if 0 in (c, sh, sw, s, attr, n_g): raise Refused("field_range", "a dimension") if n_g > s * s: raise Refused("field_range", "n_gaussians") too_large([c, sh, sw], MAX_TENSOR) too_large([attr, s, s], MAX_TENSOR) raw = r.take(8 * attr) ranges = [struct.unpack_from(" hi: raise Refused("field_range", "a range") lens = list(struct.unpack(f"<{c}H", r.take(2 * c))) if any(not 3 <= n <= 4097 for n in lens): raise Refused("field_range", "a CDF length") offs = list(struct.unpack(f"<{c}i", r.take(4 * c))) med = list(struct.unpack(f"<{c}q", r.take(8 * c))) if any(abs(x) > MAX_MEDIAN_Q for x in med): raise Refused("field_range", "median") cdfs = [r.cdf(n) for n in lens] rans = r.stream() r.end() return dict(afrac=afrac, c=c, sh=sh, sw=sw, s=s, attr=attr, n_gaussians=n_g, ranges=ranges, cdfs=cdfs, offsets=offs, median_q=med, rans=rans) def parse(cap, p): m = CAP_MAGIC[cap] if m == b"WIV1": return parse_wiv1(p, 1 if cap == "wai.video.int_hyper" else 0) return {b"WIH1": parse_wih1, b"WIA1": parse_wia1, b"WIS1": parse_wis1}[m](p) # ---- writers: the JSON sources converted independently (extensions/integer-payloads.md §10) ---- def put_stream(b): return struct.pack("= len(self.w): self.exhausted = True return self.x = (self.x << 32) | self.w[self.i] self.i += 1 def bits(self, n): v = self.x & ((1 << n) - 1) self.x >>= n self.renorm() return v def decode(self, cdf, offset): n = len(cdf) max_value = n - 2 cum = self.x & 0xFFFF lo, hi = 1, n - 1 while lo < hi: mid = (lo + hi) // 2 if cdf[mid] > cum: hi = mid else: lo = mid + 1 s = lo - 1 self.x = (cdf[s + 1] - cdf[s]) * (self.x >> 16) + (self.x & 0xFFFF) - cdf[s] self.renorm() value = s if value == max_value: val = self.bits(4) count, groups = val, 0 while val == 15 and groups < 8 and not self.exhausted: val = self.bits(4) count += val groups += 1 raw = 0 for j in range(min(count, 8)): raw |= self.bits(4) << (4 * j) value = raw >> 1 value = sat(-value - 1) if raw & 1 else sat(value + max_value) return sat(value + offset) def decode_wiv1_k0(v): h, w, block, q, prec = v["h"], v["w"], v["block"], v["q"], v["mv_precision"] nbx = -(-w // block) nb = nbx * -(-h // block) d = Rans(v["rans"]) frames, prev = [], None for f in range(v["n_frames"]): mvs = v["mvs"][(f - 1) * nb:f * nb] if f else None # The frame grows a sample at a time as its symbols decode: nothing is # allocated ahead of the stream that fills it. cur = bytearray() for y in range(h): for x in range(w): dy, dx = mvs[(y // block) * nbx + x // block] if f else (0, 0) for c in range(3): if f == 0: pred = 128 else: def p(yy, xx): return prev[(min(max(yy, 0), h - 1) * w + min(max(xx, 0), w - 1)) * 3 + c] if prec == 1: pred = p(y + dy, x + dx) else: iy, fy, ix, fx = y + (dy >> 1), dy & 1, x + (dx >> 1), dx & 1 if not fy and not fx: pred = p(iy, ix) elif not fy: pred = (p(iy, ix) + p(iy, ix + 1) + 1) >> 1 elif not fx: pred = (p(iy, ix) + p(iy + 1, ix) + 1) >> 1 else: pred = (p(iy, ix) + p(iy, ix + 1) + p(iy + 1, ix) + p(iy + 1, ix + 1) + 2) >> 2 r = d.decode(v["cdf"], v["offset"]) if d.exhausted: # §2: the first symbol during whose decode the stream runs out. raise Refused("stream_exhausted", "the residual stream ran out") cur.append(min(max(pred + r * q, 0), 255)) frames.append(bytes(cur)) prev = cur if d.i != len(d.w) or d.x != 1 << 31: # §2: after the last symbol every word has been read and the state is 2^31. raise Refused("stream_noncanonical", "the residual stream does not end canonically") return frames def main(cdir, frame_stream_only=False, int_cond_only=False): wai = os.path.normpath(os.path.join(cdir, "..")) if frame_stream_only or int_cond_only: fs_fails = frame_stream_main(wai) if frame_stream_only else int_cond_main(wai) for x in fs_fails: print("FAIL", x) return 1 if fs_fails else 0 def rd(rel): with open(os.path.join(wai, rel), "rb") as f: return f.read() def jl(rel): return json.loads(rd(rel)) idx = jl("int-payload-conformance/vectors.json") fails, decoded, parsed_only = [], 0, [] for v in idx["vectors"]: name, cap = v["name"], v["capability"] p = rd(v["payload"]) try: fields = parse(cap, p) except Refused as e: fails.append(f"{name}: does not parse: {e}") continue if hashlib.sha256(p).hexdigest() != v["payload_sha256"]: fails.append(f"{name}: payload SHA-256 differs from the index") src = v["converted_from"] j = jl(src["json"]) m = CAP_MAGIC[cap] try: if m == b"WIV1": conv = wiv1_from_json(j, rd(src["keyframe"]) if "keyframe" in src else None) elif m == b"WIH1": conv = wih1_from_json(j) elif m == b"WIA1": conv = wia1_from_json(j, jl(src["sample_rate"])["sample_rate"]) else: conv = wis1_from_json(j) except NotConvertible as e: fails.append(f"{name}: {src['json']} is not convertible: {e}") continue if conv != p: fails.append(f"{name}: the independent conversion of {src['json']} differs from the payload") if m == b"WIV1" and fields["keyframe"] == 0: frames = decode_wiv1_k0(fields) side = jl(v["sidecar"])[name] digests = [hashlib.sha256(f).digest() for f in frames] if [d.hex() for d in digests] != side["frames_sha256"] or hashlib.sha256(b"".join(digests)).hexdigest() != side["clip_sha256"]: fails.append(f"{name}: the independent decode differs from {v['sidecar']}") else: decoded += 1 print(f"ok {name}: parsed, converted, decoded to its sidecar's SHA-256 ({len(frames)} frames)") else: parsed_only.append(name) print(f"ok {name}: parsed and converted (not decoded independently)") checked, decode_layer = 0, [] for f in idx["refusals"]: name, cap, code, layer = f["name"], f["capability"], f["code"], f["layer"] p = rd(f["payload"]) try: fields = parse(cap, p) got = None except Refused as e: got = e.code if layer == "parse": if got != code: fails.append(f"{name}: parsed with {got}, want {code}") continue else: if got is not None: fails.append(f"{name}: refused at parse with {got}, want a decode-layer {code}") continue if CAP_MAGIC[cap] == b"WIV1" and fields["keyframe"] == 0: try: decode_wiv1_k0(fields) fails.append(f"{name}: decoded, want {code}") continue except Refused as e: if e.code != code: fails.append(f"{name}: decode refused with {e.code}, want {code}") continue else: decode_layer.append(name) checked += 1 print(f"ok {name}: {code}" + ("" if layer == "parse" or name not in decode_layer else " (parses; the code is the decode's)")) # wiv1_end_state codes three symbols from the state 2^31 + 1, as the words [1, 4]; its # canonical twin, the words [0, 4], decodes to one grey pixel. twin = bytearray(rd("int-payload-conformance/refusals/wiv1_end_state.bin")) twin[-8:-4] = struct.pack(" 1: raise Refused("field_range", "kind") intra = kind == 0 if kf > 1: raise Refused("field_range", "keyframe") if (intra and mvp != 0) or (not intra and mvp not in (1, 2)): raise Refused("field_range", "mv_precision") if 0 in (h, w): raise Refused("field_range", "h or w") if (intra and block != 0) or (not intra and block == 0): raise Refused("field_range", "block") if (intra and not 1 <= q <= 65535) or (not intra and (q != 0 or offset != 0)): raise Refused("field_range", "q or offset") if (intra and not 3 <= cdf_len <= 4097) or (not intra and cdf_len != 0): raise Refused("field_range", "cdf_len") if want_keyframe is not None and want_keyframe != kf: raise Refused("keyframe_capability_mismatch", "keyframe") too_large([h, w, 3], MAX_TENSOR) f = dict(kind=kind, keyframe=kf, h=h, w=w) if intra: f.update(q=q, offset=offset, cdf=r.cdf(cdf_len), rans=None, wih1=None) if kf == 1: k = r.u32() if k != r.left(): raise Refused("nested_length", "K is not the rest") f["wih1"] = r.take(k) parse_wih1(f["wih1"]) else: f["rans"] = r.stream() r.end() else: nb = -(-h // block) * -(-w // block) too_large([2, nb], MAX_TENSOR) raw = r.take(nb * 4) f.update(mv_precision=mvp, block=block, mvs=[struct.unpack_from(" 1: raise Refused("field_range", "kind") header = (h, w, fps, cy, cz, sy, sz, q, cf) check_wnf1_header(header) z, y = r.take(lz), r.take(ly) r.end() return dict(kind=kind, header=header, z=z, y=y) def check_wnf1_header(hd): h, w, fps, cy, cz, sy, sz, q, cf = hd if 0 in (h, w, cy, cz, cf): raise Refused("field_range", "a dimension") if sy > 15 or sz > 15: raise Refused("field_range", "a side's logarithm") too_large([h, w, 3], MAX_TENSOR) too_large([cy, 1 << sy, 1 << sy], MAX_TENSOR) too_large([cz, 1 << sz, 1 << sz], MAX_TENSOR) too_large([cf, 1 << sy, 1 << sy], MAX_TENSOR) def write_wnf1(kind, hd, z, y): h, w, fps, cy, cz, sy, sz, q, cf = hd return (b"WNF1" + bytes([1, kind, 0, 0]) + struct.pack("= max_value: sym, raw = max_value, 2 * (v - max_value) else: sym, raw = v, None out = [("t", cdf[sym], cdf[sym + 1] - cdf[sym])] if raw is not None: nb = 0 while raw >> (nb * 4): nb += 1 count = nb while count >= 15: out.append(("b", 15)) count -= 15 out.append(("b", count)) out.extend(("b", (raw >> (4 * j)) & 15) for j in range(nb)) return out def rans_encode(values, cdf, offset): syms = [s for v in values for s in _value_syms(v, cdf, offset)] state, words = 1 << 31, [] for s in reversed(syms): if s[0] == "t": start, freq = s[1], s[2] x_max = ((1 << 31 >> 16) << 32) * freq while state >= x_max: words.append(state & 0xFFFFFFFF) state >>= 32 state = ((state // freq) << 16) + (state % freq) + start else: x_max = ((1 << 31 >> 16) << 32) * (1 << 12) if state >= x_max: words.append(state & 0xFFFFFFFF) state >>= 32 state = (state << 4) | s[1] return struct.pack("> 32) + b"".join(struct.pack("> 1), dy & 1, x + (dx >> 1), dx & 1 if not fy and not fx: pred = p(iy, ix) elif not fy: pred = (p(iy, ix) + p(iy, ix + 1) + 1) >> 1 elif not fx: pred = (p(iy, ix) + p(iy + 1, ix) + 1) >> 1 else: pred = (p(iy, ix) + p(iy, ix + 1) + p(iy + 1, ix) + p(iy + 1, ix + 1) + 2) >> 2 r = d.decode(cdf, offset) if d.exhausted: raise Refused("stream_exhausted", "the frame's stream ran out") cur.append(min(max(pred + r * q, 0), 255)) if d.i != len(d.w) or d.x != 1 << 31: raise Refused("stream_noncanonical", "the frame's stream does not end canonically") return bytes(cur) def read_envelope(b): """SPEC §2: the manifest and the payload, or envelope_malformed.""" try: if b[:4] != b"WAI1": raise ValueError("magic") ml = struct.unpack_from("= 1 and prev is not None): raise Refused("frame_link_mismatch", "an inter frame without one") h = _content_hash(env).hex() prior = (m.get("conditioning") or {}).get("prior") out = b"" if intra: self.last = self.state = None if self.cap in INT_FRAME_CAPS: geom = (f["h"], f["w"]) if self.decode and f["keyframe"] == 0: out = decode_frame(f["rans"], f["h"], f["w"], f["q"], f["offset"], f["cdf"]) self.state = dict(geom=geom, q=f.get("q"), offset=f.get("offset"), cdf=f.get("cdf"), ref=out) else: self.state = dict(geom=f["header"]) else: if self.last is None: raise Refused("frame_no_reference", "no reference") if prev != self.last["hash"]: raise Refused("frame_gap" if index > self.last["index"] + 1 else "frame_prev_mismatch", "prev") if index != self.last["index"] + 1: raise Refused("frame_index_mismatch", "index") if number != (None if self.last["number"] is None else self.last["number"] + 1): raise Refused("frame_number_mismatch", "number") if prior != self.last["prior"]: raise Refused("frame_prior_changed", "pin") geom = (f["h"], f["w"]) if self.cap in INT_FRAME_CAPS else f["header"] if geom != self.state["geom"]: raise Refused("geometry_mismatch", "geometry") if self.decode and self.cap in INT_FRAME_CAPS and f["keyframe"] == 0: st = self.state out = decode_frame(f["rans"], f["h"], f["w"], st["q"], st["offset"], st["cdf"], st["ref"], f["mv_precision"], f["block"], f["mvs"]) st["ref"] = out self.last = dict(hash=h, index=index, prior=prior, number=number) return out def split_dcvc(clip): if len(clip) < 26: raise Refused("stream_length", "header") h, w, n, fps, cy, cz, sy, sz, q, cf = struct.unpack_from(" len(clip): raise Refused("stream_length", "the frame table") out = [] for i in range(n): lz, ly = struct.unpack_from(" len(clip): raise Refused("stream_length", "a bitstream") out.append(write_wnf1(int(i > 0), hd, clip[at:at + lz], clip[at + lz:at + lz + ly])) at += lz + ly if at != len(clip): raise Refused("trailing", "after the last bitstream") return out def join_dcvc(frames): fs = [parse_wnf1(f) for f in frames] if not fs or any(f["kind"] != int(i > 0) for i, f in enumerate(fs)): raise Refused("field_range", "a group is its intra frame, then inter frames") if any(f["header"] != fs[0]["header"] for f in fs): raise Refused("geometry_mismatch", "a header") h, w, fps, cy, cz, sy, sz, q, cf = fs[0]["header"] return (struct.pack("BBIQIII", b, 0) if plen != len(b) - 26: raise Refused("nmsf_object_malformed", "payload_len") if ft > 1: raise Refused("nmsf_reserved", "frame_type") if qp > 63: raise Refused("nmsf_reserved", "qp") return (ft, qp, fn, pts, w, h), b[26:] def nmsf_components(p, n): at, out = 0, [] for _ in range(n): if len(p) - at < 16: raise Refused("nmsf_object_malformed", "a component's header") c, h, w, dl = struct.unpack_from(">IIII", p, at) if dl > len(p) - at - 16: raise Refused("nmsf_object_malformed", "data_len") out.append((c, h, w, p[at + 16:at + 16 + dl])) at += 16 + dl if at != len(p): raise Refused("nmsf_object_malformed", "after the last component") return out def nmsf_write(hd, payload): return struct.pack(">BBIQIII", *hd, len(payload)) + payload def nmsf_component(c, h, w, data): return struct.pack(">IIII", c, h, w, len(data)) + data def nmsf_shape(comp): c, h, w, _ = comp if h != w or h < 1 or h & (h - 1) or h.bit_length() - 1 > 15: raise Refused("nmsf_shape_unmappable", "a side") if not 1 <= c <= 65535: raise Refused("nmsf_shape_unmappable", "channels") return c, h.bit_length() - 1 def nmsf_binding(v): """§9.3.""" def bad(why): return Refused("nmsf_binding_malformed", why) if not isinstance(v, dict): raise bad("not an object") if not isinstance(v.get("codec"), str): raise bad("codec") mv = v.get("modelVersion") if mv is not None and not isinstance(mv, str): raise bad("modelVersion") cf = v.get("cf") if not (type(cf) is int and 1 <= cf <= 65535): raise bad("cf") p = v.get("prior") if p is not None: if not isinstance(p, dict) or not all(isinstance(p.get(k), str) for k in ("id", "sha256", "determinism")): raise bad("prior") if not _is_hash(p["sha256"]) or any(p.get(k) is not None and not isinstance(p[k], str) for k in ("form", "tier")): raise bad("prior") return dict(codec=v["codec"], mv=mv, cf=cf, prior=p) def fps_milli(f): m = round(f * 1000.0) if 1 <= m <= 0xFFFFFFFF and m / 1000.0 == f: return int(m) raise Refused("nmsf_value_unmappable", "framerate") def nmsf_fields(e): def m(why): return Refused("nmsf_catalog_malformed", why) if not isinstance(e, dict): raise m("an entry") if not (isinstance(e.get("name"), str) and e["name"]): raise m("name") if e.get("packaging") != "nvc": raise m("packaging") if not isinstance(e.get("isLive"), bool): raise m("isLive") for k in ("codec", "colorspace"): if not isinstance(e.get(k), str): raise m(k) for k in ("width", "height", "gopSize"): if not (type(e.get(k)) is int and e[k] >= 1): raise m(k) fr = e.get("framerate") if not (_num(fr) and math.isfinite(fr) and fr > 0): raise m("framerate") if "priority" in e and not _num(e["priority"]): raise m("priority") for k in ("nvcRole", "depends"): if k in e and not isinstance(e[k], str): raise m(k) mv = None if "nvc" in e: if not isinstance(e["nvc"], dict): raise m("nvc") if "modelVersion" in e["nvc"]: mv = e["nvc"]["modelVersion"] if not isinstance(mv, str): raise m("nvc.modelVersion") return mv def nmsf_read(entries): """§9.2: one entry or two, then the codec and the framerate.""" mvs = [nmsf_fields(e) for e in entries] if len(entries) == 1: if "nvcRole" in entries[0]: raise Refused("nmsf_catalog_malformed", "a single track with a role") mode = 1 elif len(entries) == 2: roles = [e.get("nvcRole") for e in entries] if sorted(r for r in roles if r is not None) != ["hyperprior", "latent"] or None in roles: raise Refused("nmsf_catalog_malformed", "roles") hi = roles.index("hyperprior") hy, la = entries[hi], entries[1 - hi] if hy["name"] == la["name"] or la.get("depends") != hy["name"]: raise Refused("nmsf_catalog_malformed", "depends") for k in ("codec", "isLive", "width", "height", "framerate", "colorspace", "gopSize"): a, b = hy.get(k, _ABSENT), la.get(k, _ABSENT) same = float(a) == float(b) if (_num(a) and _num(b) and not isinstance(a, bool) and not isinstance(b, bool)) else a == b if not same: raise Refused("nmsf_track_mismatch", k) if mvs[0] != mvs[1]: raise Refused("nmsf_track_mismatch", "modelVersion") mode = 2 else: raise Refused("nmsf_catalog_malformed", "one track or two") codec = entries[0]["codec"] if codec not in NMSF_CODECS: raise Refused("nmsf_codec_unmapped", codec) cap, kind = NMSF_CODECS[codec] return dict(mode=mode, entries=entries, codec=codec, mv=mvs[0], cap=cap, kind=kind, fps=fps_milli(entries[0]["framerate"])) def nmsf_bind(t, binds): for b in binds: if b["codec"] == t["codec"] and b["mv"] == t["mv"]: return b raise Refused("nmsf_model_unbound", t["codec"]) def nmsf_wai_entry(t, b, name): first, cap = t["entries"][0], t["cap"] e = {"name": name, "packaging": "wai", "isLive": first["isLive"], "role": "video", "width": first["width"], "height": first["height"], "framerate": first["framerate"], "wai_capabilities": [cap], "wai_frame_stream": {"capability": cap}} if b["prior"] is not None: pe = {"capability": cap, "sha256": b["prior"]["sha256"]} for k in ("form", "tier"): if b["prior"].get(k) is not None: pe[k] = b["prior"][k] e["wai_priors"] = [pe] e["wai_nmsf"] = t["entries"] return e def nmsf_read_wai(entry): """§9.2, a WAI entry: its frame stream, wai_nmsf, and the pin it lists.""" cap = catalog_read(entry) if cap is None: raise Refused("nmsf_no_form", "no frame stream") if entry.get("wai_nmsf") is None: raise Refused("nmsf_no_form", "no wai_nmsf") if not isinstance(entry["wai_nmsf"], list): raise Refused("nmsf_catalog_malformed", "wai_nmsf") t = nmsf_read(entry["wai_nmsf"]) if t["cap"] != cap: raise Refused("nmsf_catalog_malformed", "the codec's capability") pins = [] wp = entry.get("wai_priors") if wp is not None: if not isinstance(wp, list): raise Refused("nmsf_catalog_malformed", "wai_priors") for p in wp: if not isinstance(p, dict): raise Refused("nmsf_catalog_malformed", "a wai_priors entry") if p.get("capability") != cap: continue if not _is_hash(p.get("sha256")) or (p.get("form") is not None and not isinstance(p["form"], str)): raise Refused("nmsf_catalog_malformed", "a pin") pins.append({"sha256": p["sha256"], "form": p.get("form")}) if len(pins) > 1: raise Refused("nmsf_no_form", "several pins") return t, (pins[0] if pins else None) class NmsfRewrap: """§9.4. `expect` gives the frame object's payload and `frame` member, which the caller holds the committed object to; `commit` makes it the group's last.""" def __init__(self, t, b): self.t, self.b, self.g = t, b, None def expect(self, oid, objs): try: return self._map(oid, objs) except Refused: self.g = None raise def _map(self, oid, objs): if len(objs) != self.t["mode"]: raise Refused("nmsf_track_mismatch", "objects") parsed = [nmsf_object(o) for o in objs] hd = parsed[0][0] if any(p[0] != hd for p in parsed): raise Refused("nmsf_track_mismatch", "headers") if self.t["mode"] == 1: z, y = nmsf_components(parsed[0][1], 2) else: z, y = nmsf_components(parsed[0][1], 1)[0], nmsf_components(parsed[1][1], 1)[0] ft, qp, fn, pts, w, h = hd if ft == 0: self.g = None if oid is not None and oid != 0: raise Refused("nmsf_group_order", "an Intra frame past Object ID 0") index, prev = 0, None else: g = self.g if g is None or fn != g["number"] + 1 or (oid is not None and oid != g["index"] + 1): raise Refused("nmsf_group_order", "the group") index, prev = g["index"] + 1, g["hash"] if pts > MAX_INDEX: raise Refused("nmsf_value_unmappable", "pts_ms") cz, sz = nmsf_shape(z) cy, sy = nmsf_shape(y) header = (h, w, self.t["fps"], cy, cz, sy, sz, qp, self.b["cf"]) try: check_wnf1_header(header) except Refused as e: raise Refused("nmsf_shape_unmappable", e.code) if ft == 1 and header != self.g["header"]: raise Refused("nmsf_group_header_changed", "the header") member = {"index": index, "prev": prev, "number": fn} if pts: member["capture_ms"] = pts self.pending = dict(index=index, number=fn, header=header) return write_wnf1(ft, header, z[3], y[3]), member def commit(self, env): self.g = dict(self.pending, hash=_content_hash(env).hex()) class NmsfUnwrap: """§9.5: a sink's checks (frame-stream §5), then the NMSF objects.""" def __init__(self, t, pin): self.t, self.pin, self.st = t, pin, Stream(t["cap"], False) def push(self, env): self.st.push(env) try: return self._map(env) except Refused: self.st.last = self.st.state = None raise def _map(self, env): m, payload = read_envelope(env) f = parse_wnf1(payload) if f["kind"] == 0: p = (m.get("conditioning") or {}).get("prior") got = None if p is None else {"sha256": p.get("sha256"), "form": p.get("form")} if got != self.pin: raise Refused("nmsf_model_unbound", "the pin") fr = m["frame"] if "number" not in fr or fr["number"] > 0xFFFFFFFF: raise Refused("nmsf_value_unmappable", "number") h, w, fps, cy, cz, sy, sz, q, cf = f["header"] if q > 63 or fps != self.t["fps"]: raise Refused("nmsf_value_unmappable", "q or fps") hd = (f["kind"], q, fr["number"], fr.get("capture_ms", 0), w, h) zc = nmsf_component(cz, 1 << sz, 1 << sz, f["z"]) yc = nmsf_component(cy, 1 << sy, 1 << sy, f["y"]) return [nmsf_write(hd, zc + yc)] if self.t["mode"] == 1 else [nmsf_write(hd, zc), nmsf_write(hd, yc)] def _code(f, *a): try: return "ok", f(*a) except Refused as e: return e.code, None def nmsf_main(wai): def rd(rel): with open(os.path.join(wai, rel), "rb") as f: return f.read() fails = [] idx = json.loads(rd(f"{NMSF}/index.json")) binds = [nmsf_binding(b) for b in idx["bindings"]] for c in idx["binding_cases"]: got, _ = _code(nmsf_binding, c["binding"]) (print(f"ok nmsf binding {c['name']}: {got}") if got == c["expect"] and got != "ok" else fails.append(f"nmsf binding {c['name']}: {got}, want {c['expect']}")) nmsf_cases, wai_entries = {}, {} for c in idx["nmsf_catalog"]: nmsf_cases[c["name"]] = c def m(c=c): t = nmsf_read(c["nmsf"]) return nmsf_wai_entry(t, nmsf_bind(t, binds), c["wai_name"]) got, w = _code(m) if got != c["expect"] or (got == "ok" and w != c["wai"]): fails.append(f"nmsf catalog {c['name']}: {got}, want {c['expect']}") else: print(f"ok nmsf catalog {c['name']}: {got}") if w is not None: wai_entries[c["name"]] = w tracks = {} for c in idx["wai_catalog"]: got, tp = _code(nmsf_read_wai, c["wai"]) good = got == c["expect"] and (got != "ok" or (tp[0]["entries"] == c["nmsf"] and tp[1] == c["pin"])) (print(f"ok nmsf wai catalog {c['name']}: {got}") if good else fails.append(f"nmsf wai catalog {c['name']}: {got}, want {c['expect']}")) if tp is not None: tracks[c["name"]] = tp for s in idx["rewrap"]: case = nmsf_cases[s["catalog"]] t = nmsf_read(case["nmsf"]) rw, bad, pairs = NmsfRewrap(t, nmsf_bind(t, binds)), [], [] for k, o in enumerate(s["objects"]): objs = [rd(r) for r in o["nmsf"]] got, exp = _code(rw.expect, o.get("object_id"), objs) if got != o["expect"]: bad.append(f"object {k}: {got}, want {o['expect']}") continue if got != "ok": continue env = rd(o["wai"]) mm, payload = read_envelope(env) cond = mm.get("conditioning") or {} if (payload != exp[0] or mm.get("frame") != exp[1] or mm.get("media") != "video" or mm.get("intent") != "perceptual" or (mm.get("model_requirement") or {}).get("capability") != t["cap"] or cond.get("kind") != t["kind"] or cond.get("prior") != rw.b["prior"] or _content_hash(env).hex() != o["content_hash"]): bad.append(f"object {k}: the frame object is not the mapping of its NMSF objects") rw.commit(env) pairs.append((env, objs)) # The frame objects are a stream a sink admits, and unwrap to the NMSF objects. st = Stream(t["cap"], False) uw = NmsfUnwrap(*nmsf_read_wai(wai_entries[s["catalog"]])) for k, (env, objs) in enumerate(pairs): got, _ = _code(st.push, env) if got != "ok": bad.append(f"frame object {k}: a sink gives {got}") got, back = _code(uw.push, env) if back != objs: bad.append(f"frame object {k}: unwraps to {got}, not its NMSF objects") for b in bad: fails.append(f"nmsf rewrap {s['name']}: {b}") if not bad: print(f"ok nmsf rewrap {s['name']}: {len(s['objects'])} objects as expected, " f"{len(pairs)} frame objects mapped and unwrapped back byte for byte") for s in idx["unwrap"]: uw, bad = NmsfUnwrap(*tracks[s["catalog"]]), [] for k, o in enumerate(s["objects"]): got, back = _code(uw.push, rd(o["wai"])) if got != o["expect"] or (got == "ok" and back != [rd(r) for r in o["nmsf"]]): bad.append(f"object {k}: {got}, want {o['expect']}") for b in bad: fails.append(f"nmsf unwrap {s['name']}: {b}") if not bad: print(f"ok nmsf unwrap {s['name']}: {len(s['objects'])} objects as expected") for s in idx["links"]: st, bad = Stream(s["capability"], False), [] for k, o in enumerate(s["objects"]): got, _ = _code(st.push, rd(o["wai"])) if got != o["expect"]: bad.append(f"object {k}: {got}, want {o['expect']}") for b in bad: fails.append(f"nmsf links {s['name']}: {b}") if not bad: print(f"ok nmsf links {s['name']}") text = json.dumps(idx) missing = [c for c in NMSF_CODES if f'"{c}"' not in text] if missing: fails.append(f"nmsf: no case gives {', '.join(missing)}") else: print(f"ok nmsf: every one of the {len(NMSF_CODES)} codes is given by a case") return fails # ---- conditional inter-frame video (integer-payloads.md §17) ---- IC = "int-payload-conformance/int-cond" COND_CAP, COND_FRAME_CAP = "wai.video.int_cond", "wai.video.int_cond.frame" I64_MAX, I64_MIN = (1 << 63) - 1, -(1 << 63) I128_MAX = (1 << 127) - 1 def parse_wcf1(p): """§17.1, its checks in §2's order.""" r = opening(p, b"WCF1", 32) kind, res = r.u8(), r.u16() dims = [r.u32() for _ in range(6)] if res: raise Refused("reserved_nonzero", "reserved") if kind > 1: raise Refused("field_range", "kind") if 0 in dims: raise Refused("field_range", "a dimension is zero") h, w, m, sh, sw, cf = dims too_large([h, w, 3], MAX_TENSOR) too_large([2, m, sh, sw], MAX_TENSOR) too_large([cf, sh, sw], MAX_TENSOR) rans = r.stream() r.end() return dict(kind=kind, header=tuple(dims), rans=rans) def write_wcf1(kind, header, rans): return b"WCF1" + struct.pack(" MAX_FRAMES: raise Refused("too_large", "frames") frames, raw = [], [] for k in range(n): b = r.take(r.u32()) f = parse_wcf1(b) if (f["kind"] == 0) != (k == 0): raise Refused("field_range", "frame 0 intra, the rest inter") if k == 0: h, w = f["header"][:2] too_large([n, h, w, 3], MAX_CLIP) elif f["header"] != frames[0]["header"]: raise Refused("geometry_mismatch", "a frame's header is not frame 0's") frames.append(f) raw.append(b) r.end() return frames, raw def write_wic1(frames): return b"WIC1" + struct.pack("= 20) afrac, gf, shift, n = struct.unpack_from("<4I", b, 4) p, layers = 20, [] for _ in range(n): need(p < len(b)) tag = b[p] p += 1 if tag in (0, 1): nf = 6 if tag == 0 else 5 need(p + 4 * nf <= len(b)) f = struct.unpack_from(f"<{nf}I", b, p) p += 4 * nf cin, cout, k = f[0], f[1], f[2] nw = cin * cout * k * k need(p + 2 * nw + 16 * cout <= len(b)) wts = list(struct.unpack_from(f"<{nw}h", b, p)) p += 2 * nw mult = list(struct.unpack_from(f"<{cout}q", b, p)) bias = list(struct.unpack_from(f"<{cout}q", b, p + 8 * cout)) p += 16 * cout L = dict(tag=tag, cin=cin, cout=cout, k=k, stride=f[3], pad=f[4], opad=f[5] if tag == 0 else 0, w=wts, mult=mult, bias=bias) layers.append(L) elif tag == 3: layers.append(dict(tag=3)) elif tag == 4: need(p + 12 <= len(b)) q, fr = struct.unpack_from(" len(b): raise ModelMismatch("a section's length") n = struct.unpack_from(" len(b): raise ModelMismatch("a section past the bundle") parts.append(b[p + 4:p + 4 + n]) p += 4 + n if p != len(b): raise ModelMismatch("trailing") nets = [parse_whm1(x) for x in parts[:4]] afrac = nets[0]["afrac"] if any(n["afrac"] != afrac for n in nets) or not 1 <= afrac <= 30: raise ModelMismatch("afrac") t = _int_json(parts[4]) isint = lambda v: type(v) is int edges, rows, lens, offs = t.get("scale_edges"), t.get("cdfs"), t.get("cdf_lengths"), t.get("offsets") if t.get("afrac") != afrac or not all(type(x) is list for x in (edges, rows, lens, offs)): raise ModelMismatch("tables") n = len(edges) if not 1 <= n <= 65536 or len(rows) != n or len(lens) != n or len(offs) != n: raise ModelMismatch("table counts") if not all(isint(e) and I64_MIN <= e <= I64_MAX for e in edges) or not all(isint(o) and I32_MIN <= o <= I32_MAX for o in offs): raise ModelMismatch("an edge or offset") cdfs = [] for row, ln in zip(rows, lens): if type(row) is not list or not isint(ln) or not 0 <= ln <= len(row) or not all(isint(v) and I32_MIN <= v <= I32_MAX for v in row): raise ModelMismatch("a table") try: check_cdf(row[:ln]) except Refused: raise ModelMismatch("a table is not a CDF") cdfs.append(row[:ln]) return dict(nets=dict(zip(("ctx", "prior", "dec", "recon"), nets)), afrac=afrac, edges=edges, cdfs=cdfs, offsets=offs) except (ModelMismatch, struct.error) as e: raise Refused("model_mismatch", str(e)) def plan_net(net, c, h, w): """integer-payloads.md §7, *Planning the networks*, for the layers this verifier decodes: (out_c, out_h, out_w), or the refusal.""" cap = MAX_TENSOR if 0 in (c, h, w): raise Refused("model_mismatch", "an empty input") if c * h * w > cap: raise Refused("too_large", "a network's input") for L in net["layers"]: t = L["tag"] if t in (0, 1): co, k, s, p = L["cout"], L["k"], L["stride"], L["pad"] if co == 0 or k == 0 or s == 0 or L["cin"] != c or net["shift"] > 127: raise Refused("model_mismatch", "a layer does not fit") if t == 0: fh, fw = (h - 1) * s + k + L["opad"], (w - 1) * s + k + L["opad"] if fh > cap or fw > cap or co * fh * fw > cap: raise Refused("too_large", "a transposed convolution") if fh <= 2 * p or fw <= 2 * p: raise Refused("model_mismatch", "no output") c, h, w = co, fh - 2 * p, fw - 2 * p else: hp, wp = h + 2 * p, w + 2 * p if hp > cap or wp > cap: raise Refused("too_large", "a padded side") if hp < k or wp < k: raise Refused("model_mismatch", "a kernel past its input") ho, wo = (hp - k) // s + 1, (wp - k) // s + 1 if co * ho * wo > cap: raise Refused("too_large", "a convolution's output") c, h, w = co, ho, wo elif t == 4 and L["f"] > 63: raise Refused("model_mismatch", "a leaky shift") return c, h, w def _check(net, L, c, x): """whm1/1's check range on a layer's input (integer-operators.md §13.2): P(max|x|).""" X = max((max(abs(v) for v in pl) for pl in x), default=0) if L["tag"] in (0, 1): n = c * L["k"] * L["k"] * max(abs(v) for v in L["w"]) * X ok = n <= I64_MAX and ((n * max(abs(v) for v in L["mult"])) >> net["shift"]) + 1 + max(abs(v) for v in L["bias"]) <= I64_MAX else: ok = X * abs(L["q"]) <= I64_MAX if not ok: raise Refused("field_range", "a value outside a check range of the network") def run_net(net, x, c, h, w, rgb8=False): """A WHM1 network's integers (integer-operators.md §13.2) on x, c planes of h*w.""" shift = net["shift"] for L in net["layers"]: t = L["tag"] if t in (0, 1, 4): _check(net, L, c, x) if t == 1: co, k, s, p, W = L["cout"], L["k"], L["stride"], L["pad"], L["w"] ho, wo = (h + 2 * p - k) // s + 1, (w + 2 * p - k) // s + 1 out = [] for oc in range(co): acc = [0] * (ho * wo) for ic in range(c): xp = x[ic] for ky in range(k): for kx in range(k): wv = W[((oc * c + ic) * k + ky) * k + kx] if wv == 0: continue for oy in range(ho): iy = oy * s + ky - p if iy < 0 or iy >= h: continue lo = max(0, -(-(p - kx) // s)) hi = min(wo, (w - 1 + p - kx) // s + 1) if lo >= hi: continue a0 = iy * w + lo * s + kx - p seg = xp[a0:a0 + (hi - lo - 1) * s + 1:s] b0 = oy * wo acc[b0 + lo:b0 + hi] = [u + wv * v for u, v in zip(acc[b0 + lo:b0 + hi], seg)] m, b = L["mult"][oc], L["bias"][oc] out.append([((a * m) >> shift) + b for a in acc]) x, c, h, w = out, co, ho, wo elif t == 0: co, k, s, p, W = L["cout"], L["k"], L["stride"], L["pad"], L["w"] fh, fw = (h - 1) * s + k + L["opad"], (w - 1) * s + k + L["opad"] ho, wo = fh - 2 * p, fw - 2 * p out = [] for oc in range(co): full = [0] * (fh * fw) for ic in range(c): xp = x[ic] for ky in range(k): for kx in range(k): wv = W[((ic * co + oc) * k + ky) * k + kx] if wv == 0: continue for iy in range(h): d0 = (iy * s + ky) * fw + kx row = xp[iy * w:(iy + 1) * w] full[d0:d0 + (w - 1) * s + 1:s] = [u + wv * v for u, v in zip(full[d0:d0 + (w - 1) * s + 1:s], row)] m, b = L["mult"][oc], L["bias"][oc] out.append([((full[(oy + p) * fw + ox + p] * m) >> shift) + b for oy in range(ho) for ox in range(wo)]) x, c, h, w = out, co, ho, wo elif t == 3: x = [[v if v > 0 else 0 for v in pl] for pl in x] elif t == 4: q, f = L["q"], L["f"] x = [[v if v >= 0 else (v * q) >> f for v in pl] for pl in x] if rgb8: one, half = 1 << net["afrac"], 1 << (net["afrac"] - 1) x = [[(min(max(v, 0), one) * 255 + half) >> net["afrac"] for v in pl] for pl in x] return x, c, h, w def cond_plan(model, hd): """§17.4 step 3: every network planned, then the shapes the frame declares.""" h, w, m, sh, sw, cf = hd n = model["nets"] ctx = plan_net(n["ctx"], cf, sh, sw) prior = plan_net(n["prior"], ctx[0], sh, sw) dec = plan_net(n["dec"], m + ctx[0], sh, sw) recon = plan_net(n["recon"], cf, sh, sw) if ctx[1:] != (sh, sw) or prior != (2 * m, sh, sw) or dec != (cf, sh, sw): raise Refused("geometry_mismatch", "a network does not give the shape the frame declares") if recon[0] != 3: raise Refused("model_mismatch", "recon does not give three channels") if recon[1:] != (h, w): raise Refused("geometry_mismatch", "recon does not give h × w") def cond_frame(model, hd, rans, ref): """§17.4 steps 5-9: one frame from its stream and the reference feature (None: zero).""" h, w, m, sh, sw, cf = hd n, af = model["nets"], model["afrac"] feat = ref if ref is not None else [[0] * (sh * sw) for _ in range(cf)] c, cc, _, _ = run_net(n["ctx"], feat, cf, sh, sw) pr, _, _, _ = run_net(n["prior"], c, cc, sh, sw) sigma, mu = [v for pl in pr[:m] for v in pl], [v for pl in pr[m:] for v in pl] d = Rans(rans) edges, cdfs, offs = model["edges"], model["cdfs"], model["offsets"] e0, last = edges[0], edges[:-1] y = [] for s, u in zip(sigma, mu): sq = max(s, e0) b = len(edges) - 1 - sum(1 for e in last if sq <= e) r = d.decode(cdfs[b], offs[b]) if d.exhausted: raise Refused("stream_exhausted", "the latent stream ran out") v = (r << af) + u if not I64_MIN <= v <= I64_MAX: raise Refused("field_range", "a latent value leaves i64") y.append(v) if d.i != len(d.w) or d.x != 1 << 31: raise Refused("stream_noncanonical", "the latent stream does not end canonically") plane = sh * sw x = [y[i * plane:(i + 1) * plane] for i in range(m)] + c f, fc, _, _ = run_net(n["dec"], x, m + cc, sh, sw) out, oc, oh, ow = run_net(n["recon"], f, fc, sh, sw, rgb8=True) return bytes(out[ch][i] for i in range(oh * ow) for ch in range(3)), f def cond_clip(clip, set_bytes): frames, _ = parse_wic1(clip) if set_bytes is None: raise Refused("set_incomplete", "model.wcb") model = parse_wcb1(set_bytes) hd = frames[0]["header"] cond_plan(model, hd) out, ref = [], None for f in frames: rgb, ref = cond_frame(model, hd, f["rans"], ref) out.append(rgb) return out class CondStream: """frame-stream.md §5 over wai.video.int_cond.frame, decoding every frame.""" def __init__(self, model, set_sha): self.model, self.set_sha, self.last, self.state = model, set_sha, None, None def push(self, env): try: return self._admit(env) except Refused: self.last = self.state = None raise def _admit(self, env): m, payload = read_envelope(env) if (m.get("model_requirement") or {}).get("capability") != COND_FRAME_CAP: raise Refused("frame_capability_mismatch", "another capability") index, prev, number = read_link(m) f = parse_wcf1(payload) intra = f["kind"] == 0 if intra and not (index == 0 and prev is None): raise Refused("frame_link_mismatch", "an intra frame with a link") if not intra and not (index >= 1 and prev is not None): raise Refused("frame_link_mismatch", "an inter frame without one") h = _content_hash(env).hex() prior = (m.get("conditioning") or {}).get("prior") if intra: self.last = self.state = None ref = None else: if self.last is None: raise Refused("frame_no_reference", "no reference") if prev != self.last["hash"]: raise Refused("frame_gap" if index > self.last["index"] + 1 else "frame_prev_mismatch", "prev") if index != self.last["index"] + 1: raise Refused("frame_index_mismatch", "index") if number != (None if self.last["number"] is None else self.last["number"] + 1): raise Refused("frame_number_mismatch", "number") if prior != self.last["prior"]: raise Refused("frame_prior_changed", "pin") if f["header"] != self.state["header"]: raise Refused("geometry_mismatch", "an inter frame's header is not its group's") ref = self.state["feature"] if not prior or prior.get("sha256") != self.set_sha: raise Refused("prior_unverified", "the corpus pins every frame to its set") cond_plan(self.model, f["header"]) rgb, feat = cond_frame(self.model, f["header"], f["rans"], ref) self.state = dict(header=f["header"], feature=feat) self.last = dict(hash=h, index=index, number=number, prior=prior) return rgb def int_cond_main(wai): def rd(rel): with open(os.path.join(wai, rel), "rb") as f: return f.read() fails = [] idx = json.loads(rd(f"{IC}/index.json")) set_bytes = rd(idx["set"]["model.wcb"]) set_sha = hashlib.sha256(set_bytes).hexdigest() if set_sha != idx["set_sha256"] or idx["prior"]["sha256"] != set_sha: fails.append("int-cond: the set's SHA-256 is not the index's or the pin's") model = parse_wcb1(set_bytes) clip = rd(idx["clip"]["payload"]) side = json.loads(rd(idx["clip"]["sidecar"]))["clip"] whole = cond_clip(clip, set_bytes) digests = [hashlib.sha256(f).digest() for f in whole] if [d.hex() for d in digests] != side["frames_sha256"] or hashlib.sha256(b"".join(digests)).hexdigest() != side["clip_sha256"]: fails.append("int-cond clip: the independent decode differs from its sidecar") else: print(f"ok int-cond clip: decoded independently to its sidecar's SHA-256 ({len(whole)} frames)") frames = [rd(f) for f in idx["clip"]["frames"]] if parse_wic1(clip)[1] != frames or write_wic1(frames) != clip: fails.append("int-cond clip: the split and the join are not the committed frames and clip") else: print(f"ok int-cond clip: splits into its {len(frames)} frames and they join into it") for s in idx["streams"]: st, outs, bad = CondStream(model, set_sha), [], [] for k, o in enumerate(s["objects"]): e = rd(o["envelope"]) try: out, got = st.push(e), "ok" except Refused as r: out, got = None, r.code if got != o["expect"]: bad.append(f"object {k}: {got}, want {o['expect']}") continue if got == "ok": if _content_hash(e).hex() != o["content_hash"]: bad.append(f"object {k}: content hash") if hashlib.sha256(out).hexdigest() != o.get("frame_sha256"): bad.append(f"object {k}: frame SHA-256") outs.append(out) if s["name"] == "clip" and outs != whole: bad.append("its frames are not the clip's, byte for byte") fails += [f"int-cond stream {s['name']}: {b}" for b in bad] if not bad: print(f"ok int-cond stream {s['name']}: {len(s['objects'])} objects as expected, decoded") for f in idx["refusals"]: name, cap, code, layer = f["name"], f["capability"], f["code"], f["layer"] b = rd(f["payload"]) parse = parse_wic1 if cap == COND_CAP else parse_wcf1 try: parse(b) got = None except Refused as e: got = e.code if layer == "decode": if got is not None: fails.append(f"int-cond {name}: refused at parse with {got}, want a decode-layer {code}") continue try: cond_clip(b, set_bytes) except Refused as e: got = e.code if got != code: fails.append(f"int-cond {name}: {got}, want {code} at {layer}") else: print(f"ok int-cond {name}: {code} ({layer})") for sc in idx["sets"]: path = sc["set"].get("model.wcb") try: cond_clip(clip, rd(path) if path else None) got = None except Refused as e: got = e.code (print(f"ok int-cond set {sc['name']}: {got}") if got == sc["code"] else fails.append(f"int-cond set {sc['name']}: {got}, want {sc['code']}")) for j in idx["join_refusals"]: try: parse_wic1(write_wic1([rd(x) for x in j["frames"]])) got = None except Refused as e: got = e.code (print(f"ok int-cond join {j['name']}: {got}") if got == j["code"] else fails.append(f"int-cond join {j['name']}: {got}, want {j['code']}")) return fails if __name__ == "__main__": args = sys.argv[1:] only = "--frame-stream" in args cond = "--int-cond" in args args = [a for a in args if a not in ("--frame-stream", "--int-cond")] sys.exit(main(args[0] if args else os.path.dirname(os.path.abspath(__file__)), frame_stream_only=only, int_cond_only=cond))