ws-h264: codecs/H264 decoder (CAVLC + CABAC, I/P/B, 8x8, weighted, direct, deblock, DPB/POC/reorder); fixtures from libx264 vs ffmpeg
Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
75c677074a
commit
7ae481d4ed
15 files changed
+6343
No files matched your search
@@ -0,0 +1,167 @@
|
||||
# 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"),
|
||||
]
|
||||
|
||||
|
||||
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, "-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)
|
||||
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)
|
||||
Reference in new issue
Block a user