ws-cleanup: merge master (ws/h264)

This commit is contained in:
4DBug committed 2026-10-31 11:12:11 -05:00
commit 6b26845afe
28 files changed
+7345 -1

No files matched your search

+21
View File
@@ -1030,3 +1030,24 @@ do
run(" (jit tuned)")
end
end
do
-- H.264 (codecs/H264): whole Annex B streams, frames per second (decode + the cropped yuv420p output copies). The
-- testsrc2 clips are easy (mostly static); the mandelbrot zooms move everywhere. LuaJIT runs twice: with its default
-- trace limits and with the ones host/init.luau sets (maxtrace=8000, maxmcode=65536).
local H264 = require("@formats/codecs/H264")
local HB = require("@tests/gen/h264_bench")
local function run(tag: string)
for _, c in HB do
local n = c.width * c.height * 3 // 2 * #c.frames
local dt = bench(string.format("h264 %s %dx%d%s", c.name, c.width, c.height, tag), 3, n, function()
H264.decodeAll(c.data)
end)
print(string.format("%-8s %-30s %8.1f fps", runtime, " (" .. #c.frames .. " frames)", #c.frames / dt))
end
end
run("")
if runtime == "luajit" then
require("@formats/host")
run(" (jit tuned)")
end
end
+189
View File
@@ -0,0 +1,189 @@
# H.264 fixtures (tools/fixtures.py). Reference tools: ffmpeg's libx264 encodes synthetic clips (testsrc2 / mandelbrot /
# noise) at many settings (profiles, CAVLC / CABAC, B frames, references, weighted prediction, 8x8 transforms, slices,
# scaling matrices, odd sizes with cropping, ...); ffmpeg's own H.264 decoder (-f rawvideo -pix_fmt yuv420p) gives the
# reference frames, compared per frame by CRC-32 of the cropped yuv420p planes.
# tests/gen/h264_cases.luau { name, data (Annex B), width, height, frames = { crc ... }, avcc?, samples? }
# tests/gen/h264_bad.luau streams expected to fail: { name, data, code }
# tests/gen/h264_bench.luau bigger streams for tools/bench.luau: { name, data, width, height, frames }
import os, subprocess, tempfile, zlib
INPUTS = []
SRC = {
"testsrc2": "testsrc2=s={w}x{h}:r=25",
"noise": "testsrc2=s={w}x{h}:r=25,noise=alls=24:allf=t+u",
"mandel": "mandelbrot=s={w}x{h}:r=25",
"fade": "testsrc2=s={w}x{h}:r=25,fade=in:0:8,hue=H=t*2",
}
# name, source, w, h, frames, x264 params, extra ffmpeg args
CASES = [
("cavlc_intra", "testsrc2", 176, 144, 3, "keyint=1:cabac=0:8x8dct=0:bframes=0:ref=1", []),
("cavlc_intra_noise", "noise", 176, 144, 2, "keyint=1:cabac=0:8x8dct=0:bframes=0:qp=12", []),
("cavlc_p", "testsrc2", 176, 144, 12, "cabac=0:8x8dct=0:bframes=0:ref=1:weightp=0", []),
("cavlc_p_refs4", "noise", 176, 144, 12, "cabac=0:8x8dct=0:bframes=0:ref=4:weightp=0:mixed-refs=1", []),
("cavlc_p_weightp", "fade", 176, 144, 12, "cabac=0:8x8dct=0:bframes=0:ref=3:weightp=2", []),
("cavlc_slices", "testsrc2", 176, 144, 6, "cabac=0:8x8dct=0:bframes=0:slices=4", []),
("cavlc_high_8x8", "noise", 176, 144, 8, "cabac=0:8x8dct=1:bframes=0:ref=2", []),
("cavlc_b3", "testsrc2", 176, 144, 14, "cabac=0:8x8dct=0:bframes=3:b-pyramid=normal:ref=3", []),
("cavlc_b_temporal", "noise", 176, 144, 10, "cabac=0:bframes=2:direct=temporal:ref=2:b-pyramid=none", []),
("cavlc_constrained", "noise", 176, 144, 8, "cabac=0:bframes=0:constrained-intra=1:ref=2", []),
("cabac_intra", "testsrc2", 176, 144, 3, "keyint=1:cabac=1:8x8dct=0:bframes=0", []),
("cabac_intra_8x8", "noise", 176, 144, 2, "keyint=1:cabac=1:8x8dct=1:bframes=0:qp=10", []),
("cabac_p", "testsrc2", 176, 144, 12, "cabac=1:8x8dct=0:bframes=0:ref=1:weightp=0", []),
("cabac_p_refs4", "noise", 176, 144, 12, "cabac=1:bframes=0:ref=4:weightp=1", []),
("cabac_weightp", "fade", 176, 144, 12, "cabac=1:bframes=0:ref=3:weightp=2", []),
("cabac_b3", "testsrc2", 176, 144, 16, "cabac=1:bframes=3:b-pyramid=normal:ref=4:weightb=1", []),
("cabac_b3_noise", "noise", 176, 144, 12, "cabac=1:bframes=3:b-pyramid=strict:ref=3:direct=spatial", []),
("cabac_b_temporal", "noise", 176, 144, 10, "cabac=1:bframes=2:direct=temporal:ref=3:weightb=0", []),
("cabac_b_weightb", "fade", 176, 144, 12, "cabac=1:bframes=3:weightb=1:weightp=2:ref=3", []),
("cabac_slices", "noise", 176, 144, 8, "cabac=1:bframes=2:slices=3:ref=2", []),
("high_cqm", "noise", 176, 144, 8, "cabac=1:8x8dct=1:bframes=2:cqm=jvt:ref=2", []),
("high_lowqp", "noise", 176, 144, 4, "cabac=1:8x8dct=1:bframes=1:qp=4", []),
("cavlc_lowqp", "noise", 176, 144, 4, "cabac=0:8x8dct=1:bframes=1:qp=4", []),
("deblock_off", "testsrc2", 176, 144, 6, "cabac=1:bframes=1:no-deblock=1", []),
("deblock_offsets", "noise", 176, 144, 6, "cabac=1:bframes=1:deblock=-3,3", []),
("qvga_main", "mandel", 320, 240, 6, "cabac=1:bframes=2:ref=2:8x8dct=0", []),
("odd_98x62", "noise", 98, 62, 6, "cabac=1:bframes=2:ref=2", []),
("odd_34x18", "testsrc2", 34, 18, 5, "cabac=0:bframes=1:ref=1", []),
("tiny_16x16", "noise", 16, 16, 4, "cabac=1:bframes=1", []),
("wide_200x40", "noise", 200, 40, 6, "cabac=1:bframes=3:ref=3:8x8dct=1", []),
("poc2_baseline", "testsrc2", 176, 144, 8, "cabac=0:bframes=0:ref=2", ["-profile:v", "baseline"]),
("intra_refresh", "noise", 176, 144, 10, "cabac=0:bframes=0:intra-refresh=1:keyint=6", []),
("keyint_short", "testsrc2", 176, 144, 14, "cabac=1:bframes=2:keyint=5:min-keyint=1:open-gop=0", []),
("open_gop", "testsrc2", 176, 144, 14, "cabac=1:bframes=2:keyint=6:open-gop=1", []),
("partitions_all", "noise", 176, 144, 8, "cabac=1:bframes=2:partitions=all:8x8dct=1:subme=9:me=umh", []),
("cavlc_partitions_all", "noise", 176, 144, 8, "cabac=0:bframes=2:partitions=all:8x8dct=1:subme=9", []),
]
BENCH = [
("qvga_cabac_b", "testsrc2", 320, 240, 30, "cabac=1:bframes=3:ref=3:crf=23"),
("qvga_baseline", "testsrc2", 320, 240, 30, "cabac=0:bframes=0:ref=1:8x8dct=0:crf=23"),
("nhd_cabac_b", "testsrc2", 640, 360, 30, "cabac=1:bframes=3:ref=3:crf=23"),
("nhd_baseline", "testsrc2", 640, 360, 30, "cabac=0:bframes=0:ref=1:8x8dct=0:crf=23"),
("qvga_mandel_high", "mandel", 320, 240, 30, "cabac=1:bframes=3:ref=3:8x8dct=1:crf=23"),
("qvga_mandel_base", "mandel", 320, 240, 30, "cabac=0:bframes=0:ref=1:8x8dct=0:crf=23"),
("nhd_mandel_high", "mandel", 640, 360, 30, "cabac=1:bframes=3:ref=3:8x8dct=1:crf=23"),
("nhd_mandel_base", "mandel", 640, 360, 30, "cabac=0:bframes=0:ref=1:8x8dct=0:crf=23"),
]
def ffmpeg(ctx, args):
return ctx.nix("ffmpeg", ["ffmpeg", "-hide_banner", "-loglevel", "error", "-y"] + args)
def encode(ctx, tmp, name, src, w, h, n, params, extra):
out = os.path.join(tmp, name + ".h264")
lav = SRC[src].format(w=w, h=h)
ffmpeg(ctx, ["-f", "lavfi", "-i", lav, "-frames:v", str(n), "-pix_fmt", "yuv420p", "-c:v", "libx264",
"-x264-params", params + ":threads=1", *extra, "-f", "h264", out])
with open(out, "rb") as f:
data = f.read()
ref = os.path.join(tmp, name + ".yuv")
ffmpeg(ctx, ["-threads", "1", "-i", out, "-fps_mode", "passthrough", "-f", "rawvideo", "-pix_fmt", "yuv420p", ref])
with open(ref, "rb") as f:
raw = f.read()
fs = w * h + 2 * ((w + 1) // 2) * ((h + 1) // 2)
frames = [zlib.crc32(raw[i:i + fs]) for i in range(0, len(raw), fs)]
return data, frames
def to_avcc(data):
"""Annex B -> (avcC record, length-prefixed samples split at access unit delimiters / first slices)"""
nals = []
i = 0
starts = []
while True:
j = data.find(b"\x00\x00\x01", i)
if j < 0:
break
starts.append(j + 3)
i = j + 3
for k, s in enumerate(starts):
e = starts[k + 1] - 3 if k + 1 < len(starts) else len(data)
nal = data[s:e]
while nal.endswith(b"\x00"):
nal = nal[:-1]
nals.append(nal)
sps = [n for n in nals if n[0] & 31 == 7]
pps = [n for n in nals if n[0] & 31 == 8]
rec = bytes([1, sps[0][1], sps[0][2], sps[0][3], 0xFF, 0xE0 | len(sps)])
for s in sps:
rec += len(s).to_bytes(2, "big") + s
rec += bytes([len(pps)])
for p in pps:
rec += len(p).to_bytes(2, "big") + p
samples = []
cur = b""
has_slice = False
for n in nals:
t = n[0] & 31
if t in (7, 8):
continue
# a new access unit: SEI / AUD after a slice, or a slice with first_mb_in_slice == 0 (first bit 1)
if has_slice and (t in (6, 9) or (t in (1, 5) and n[1] & 0x80)):
samples.append(cur)
cur = b""
has_slice = False
cur += len(n).to_bytes(4, "big") + n
if t in (1, 5):
has_slice = True
if cur:
samples.append(cur)
return rec, samples
def generate(ctx):
cases = []
with tempfile.TemporaryDirectory() as tmp:
for name, src, w, h, n, params, extra in CASES:
data, frames = encode(ctx, tmp, name, src, w, h, n, params, extra)
c = {"name": name, "data": data, "width": w, "height": h, "frames": frames}
if name in ("cabac_b3", "cavlc_p"):
rec, samples = to_avcc(data)
c["avcc"] = rec
c["samples"] = samples
cases.append(c)
ctx.emit("h264_cases", cases)
bad = []
lav = SRC["testsrc2"].format(w=64, h=64)
out = os.path.join(tmp, "interlaced.h264")
ffmpeg(ctx, ["-f", "lavfi", "-i", lav, "-frames:v", "4", "-pix_fmt", "yuv420p", "-c:v", "libx264",
"-x264-params", "interlaced=1:threads=1", "-f", "h264", out])
bad.append({"name": "interlaced", "data": open(out, "rb").read(), "code": "UNSUPPORTED"})
out = os.path.join(tmp, "yuv444.h264")
ffmpeg(ctx, ["-f", "lavfi", "-i", lav, "-frames:v", "2", "-pix_fmt", "yuv444p", "-c:v", "libx264",
"-x264-params", "threads=1", "-f", "h264", out])
bad.append({"name": "yuv444", "data": open(out, "rb").read(), "code": "UNSUPPORTED"})
out = os.path.join(tmp, "yuv422.h264")
ffmpeg(ctx, ["-f", "lavfi", "-i", lav, "-frames:v", "2", "-pix_fmt", "yuv422p", "-c:v", "libx264",
"-x264-params", "threads=1", "-f", "h264", out])
bad.append({"name": "yuv422", "data": open(out, "rb").read(), "code": "UNSUPPORTED"})
out = os.path.join(tmp, "10bit.h264")
ffmpeg(ctx, ["-f", "lavfi", "-i", lav, "-frames:v", "2", "-pix_fmt", "yuv420p10le", "-c:v", "libx264",
"-x264-params", "threads=1", "-f", "h264", out])
bad.append({"name": "10bit", "data": open(out, "rb").read(), "code": "UNSUPPORTED"})
ctx.emit("h264_bad", bad)
# containers: the same encodes written by ffmpeg into MP4 / MKV (avcC) and TS / AVI (Annex B), referenced by ffmpeg's decode of each file
cont = []
for name, params, n in (("cabac_b", "cabac=1:bframes=3:ref=3:8x8dct=1", 20), ("baseline", "cabac=0:bframes=0:ref=2", 12)):
raw = os.path.join(tmp, "c_" + name + ".h264")
ffmpeg(ctx, ["-f", "lavfi", "-i", SRC["noise"].format(w=176, h=144), "-frames:v", str(n), "-pix_fmt", "yuv420p",
"-c:v", "libx264", "-x264-params", params + ":threads=1", "-f", "h264", raw])
for ext in ("h264", "mp4", "mkv", "ts", "avi"):
path = raw if ext == "h264" else os.path.join(tmp, "c_%s.%s" % (name, ext))
if ext != "h264":
ffmpeg(ctx, ["-f", "lavfi", "-i", SRC["noise"].format(w=176, h=144), "-frames:v", str(n), "-pix_fmt", "yuv420p",
"-c:v", "libx264", "-x264-params", params + ":threads=1", path])
yuv = path + ".yuv"
ffmpeg(ctx, ["-threads", "1", "-i", path, "-fps_mode", "passthrough", "-f", "rawvideo", "-pix_fmt", "yuv420p", yuv])
rawyuv = open(yuv, "rb").read()
fs = 176 * 144 * 3 // 2
cont.append({"name": name + "." + ext, "ext": ext, "data": open(path, "rb").read(), "width": 176, "height": 144,
"frames": [zlib.crc32(rawyuv[i:i + fs]) for i in range(0, len(rawyuv), fs)]})
ctx.emit("h264_containers", cont)
bench = []
for name, src, w, h, n, params in BENCH:
data, frames = encode(ctx, tmp, name, src, w, h, n, params, [])
bench.append({"name": name, "data": data, "width": w, "height": h, "frames": frames})
ctx.emit("h264_bench", bench)
+82
View File
@@ -0,0 +1,82 @@
# H.264 conformance fixtures (tools/fixtures.py): a selection of the ITU-T H.264.1 / JVT conformance bitstreams
# (freely downloadable from https://www.itu.int/wftp3/av-arch/jvt-site/draft_conformance/), downloaded once into
# tests/gen/jvt_cache/. Reference frames: the decoded .yuv shipped in each zip (the JM reference decoder's output) when
# it has the expected size, else ffmpeg's decode (-fps_mode passthrough). Per-frame CRC-32 of yuv420p.
# tests/gen/h264_jvt.luau { name, data64 (the bitstream, base64), width, height, frames = { crc ... }, ref = "jm" | "ffmpeg", expect = "ok" |
# "UNSUPPORTED" } (empty when the site cannot be reached)
import base64, json, os, subprocess, urllib.request, zipfile, zlib
INPUTS = []
BASE = "https://www.itu.int/wftp3/av-arch/jvt-site/draft_conformance/"
OK = [
"AVCv1/BA1_Sony_D", "AVCv1/BA2_Sony_F", "AVCv1/BA3_SVA_C", "AVCv1/BA_MW_D", "AVCv1/BANM_MW_D", "AVCv1/BAMQ2_JVC_C",
"AVCv1/BASQP1_Sony_C", "AVCv1/CABA1_SVA_B", "AVCv1/CABA2_Sony_E", "AVCv1/CABA3_SVA_B", "AVCv1/CABA3_TOSHIBA_E",
"AVCv1/CABACI3_Sony_B", "AVCv1/CABAST3_Sony_E", "AVCv1/CACQP3_Sony_D", "AVCv1/CANL2_Sony_E", "AVCv1/CANL3_SVA_B",
"AVCv1/CANL4_SVA_B", "AVCv1/CAQP1_Sony_B", "AVCv1/CI_MW_D", "AVCv1/CVBS3_Sony_C", "AVCv1/CVFC1_Sony_C",
"AVCv1/CVSE2_Sony_B", "AVCv1/CVSEFDFT3_Sony_E", "AVCv1/CVWP3_TOSHIBA_E", "AVCv1/MIDR_MW_D", "AVCv1/MPS_MW_A",
"AVCv1/MR1_BT_A", "AVCv1/MR1_MW_A", "AVCv1/MR2_TANDBERG_E", "AVCv1/MR4_TANDBERG_C", "AVCv1/NL2_Sony_H",
"AVCv1/NLMQ2_JVC_C", "AVCv1/NRF_MW_E", "AVCv1/SL1_SVA_B", "AVCv1/SVA_BA2_D", "AVCv1/SVA_CL1_E", "AVCv1/SVA_FM1_E",
"AVCv1/SVA_NL2_E", "AVCv1/cavlc_mot_frm0_full_B", "AVCv1/cabac_mot_frm0_full", "FRExt/FRExt1_Panasonic_D",
"FRExt/FRExt3_Panasonic_E", "FRExt/HCAFR1_HHI_C", "FRExt/HCAFR2_HHI_A", "FRExt/HCBP2_HHI_A", "FRExt/HCMP1_HHI_A",
"FRExt/brcm_freh3", "FRExt/freh12_b",
]
UNSUPPORTED = ["FRExt/HCAFF1_HHI_B", "FRExt/HCAMFF1_HHI_B", "AVCv1/SP1_BT_A", "FRExt/Hi422FR1_SONY_A"]
EXT = (".jsv", ".264", ".h264", ".avc", ".26l", ".jvt")
def fetch(ctx, item):
cache = os.path.join(ctx.gen, "jvt_cache")
os.makedirs(cache, exist_ok=True)
path = os.path.join(cache, item.split("/")[1] + ".zip")
if not os.path.exists(path):
try:
with urllib.request.urlopen(BASE + item + ".zip", timeout=120) as r:
data = r.read()
except Exception as e:
print("h264_jvt: cannot download %s: %s" % (item, e))
return None
with open(path, "wb") as f:
f.write(data)
return path
def generate(ctx):
ff = ctx.nix("ffmpeg", ["sh", "-c", "command -v ffmpeg"]).decode().strip()
fp = os.path.join(os.path.dirname(ff), "ffprobe")
out = []
for item in OK + UNSUPPORTED:
z = fetch(ctx, item)
if not z:
continue
zf = zipfile.ZipFile(z)
names = zf.namelist()
bs = [n for n in names if n.lower().endswith(EXT)]
if not bs:
continue
data = zf.read(bs[0])
name = item.split("/")[1]
tmp = os.path.join(ctx.gen, "jvt_cache", name + ".bit")
with open(tmp, "wb") as f:
f.write(data)
if item in UNSUPPORTED:
out.append({"name": name, "data64": base64.b64encode(data).decode(), "width": 0, "height": 0, "frames": [], "ref": "none", "expect": "UNSUPPORTED"})
continue
st = json.loads(subprocess.run([fp, "-v", "error", "-select_streams", "v:0", "-show_entries", "stream=width,height",
"-of", "json", tmp], capture_output=True, text=True).stdout)["streams"][0]
w, h = st["width"], st["height"]
fs = w * h * 3 // 2
raw = subprocess.run([ff, "-v", "error", "-threads", "1", "-i", tmp, "-fps_mode", "passthrough", "-f", "rawvideo",
"-pix_fmt", "yuv420p", "-"], capture_output=True).stdout
frames = [zlib.crc32(raw[i:i + fs]) for i in range(0, len(raw), fs)]
ref = "ffmpeg"
yuvs = [n for n in names if n.lower().endswith(".yuv")]
if yuvs:
jm = zf.read(yuvs[0])
if len(jm) == len(frames) * fs:
jmf = [zlib.crc32(jm[i:i + fs]) for i in range(0, len(jm), fs)]
if jmf != frames:
print("h264_jvt: %s: JM and ffmpeg references differ, using JM" % name)
frames, ref = jmf, "jm"
out.append({"name": name, "data64": base64.b64encode(data).decode(), "width": w, "height": h, "frames": frames, "ref": ref, "expect": "ok"})
ctx.emit("h264_jvt", out)