open-standards/wai/format-probe-conformance/tools/yuv.py
Transaction Science 7d61c2f8f1 wai: extensions format-probes and projection; the corpus format-probe-conformance with its second implementation
External vectors written by permissively licensed encoders at pinned
revisions from synthetic pictures (tools/make_external.sh, provenance.json),
generated vectors and negative controls, the facts each encoder was asked
for, the LCEVC streams fetch.sh fetches by digest, and
format_probe_verify.py, written from the two texts.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-22 07:55:38 -04:00

42 lines
1.6 KiB
Python

"""Writes the synthetic pictures the external vectors are encoded from: planar samples of a
fixed pattern, so the inputs are the same on every machine and hold no captured content.
python3 -I yuv.py <out> <width> <height> <frames> <layout> <depth> [<shift>]
<layout> is 420, 422, 444 (planes Y, Cb, Cr) or rgb (planes R, G, B). Samples above 8 bits
are two bytes, little-endian. <shift> moves the pattern right by that many columns, the
second view of a stereo pair.
"""
import sys
def main(out, w, h, frames, layout, depth, shift=0):
w, h, frames, depth, shift = int(w), int(h), int(frames), int(depth), int(shift)
top = (1 << depth) - 1
if layout == "rgb":
planes = [(1, 1)] * 3
else:
sub = {"420": (2, 2), "422": (2, 1), "444": (1, 1)}[layout]
planes = [(1, 1), sub, sub]
data = bytearray()
for f in range(frames):
for p, (sx, sy) in enumerate(planes):
pw, ph = (w + sx - 1) // sx, (h + sy - 1) // sy
for y in range(ph):
for x in range(pw):
xs = (x * sx - shift) % w
# a diagonal ramp, a checker and a moving bar, per plane
v = (xs * 3 + y * sy * 5 + f * 7 + p * 40) * (top // 255 if top > 255 else 1)
if ((xs // 8) + (y * sy // 8) + p) % 2:
v += top // 6
if (xs + f * 4) % w < 6:
v = top - v % (top // 4 + 1)
v %= top + 1
data += bytes([v]) if depth <= 8 else v.to_bytes(2, "little")
with open(out, "wb") as fh:
fh.write(data)
if __name__ == "__main__":
main(*sys.argv[1:])