171 lines
8.6 KiB
Python
171 lines
8.6 KiB
Python
# MPEG-1 video fixtures for codecs/Mpeg1 (tools/fixtures.py), ported from mpeg1-luau tools/fixtures.py: elementary
|
|
# streams made by ffmpeg's mpeg1video encoder (testsrc2 / mandelbrot, several bit rates, odd sizes, custom matrices,
|
|
# B-frames, a size change), their access units, and ffmpeg's own decode of each as references:
|
|
# tests/gen/mpeg1_<name>.luau { name, width, height, frames, file (base64), units = { { offset, length } },
|
|
# means = { { r, g, b } } (every frame, display order, ffmpeg's RGBA),
|
|
# rgba = { { index, pixels } } (two whole RGBA frames, base64 zlib),
|
|
# yuv = { { index, planes } } (three whole frames as ffmpeg -pix_fmt yuv420p, base64 zlib) }
|
|
# tests/gen/mpeg1_sizechange.luau { file (base64), parts = { { width, height, frames, last (RGBA, base64 zlib) } } }
|
|
# tests/gen/mpeg1_bench.luau { { width, height, file (base64), units } }: 320x240 and 640x480 streams (with
|
|
# B-frames) for tools/bench.luau
|
|
# Binary fields are base64 (file, units' data) or base64 of zlib data (frames: the test inflates them with
|
|
# codecs/Deflate): Luau string literals of binary data cost ~3.7 bytes per byte, base64 1.33. With short streams and
|
|
# few whole frames the set stays near 10 MB.
|
|
import base64, os, shutil, subprocess, tempfile, zlib
|
|
|
|
def b64(b):
|
|
return base64.b64encode(b).decode()
|
|
|
|
|
|
def z64(b):
|
|
return base64.b64encode(zlib.compress(b, 9)).decode()
|
|
|
|
|
|
FF = ["-hide_banner", "-loglevel", "error", "-y"]
|
|
ENC = ["-c:v", "mpeg1video", "-f", "mpeg1video"]
|
|
# a non-default intra matrix (steeper) and inter matrix (rising), 64 values in natural order
|
|
INTRA = ",".join(str(8 + (i // 8 + i % 8) * 3) for i in range(64))
|
|
INTER = ",".join(str(16 + (i // 8 + i % 8)) for i in range(64))
|
|
|
|
|
|
def src(kind, w, h, rate, t):
|
|
return ["-f", "lavfi", "-i", "%s=size=%dx%d:rate=%s" % (kind, w, h, rate), "-t", str(t)]
|
|
|
|
|
|
# name: (ffmpeg input + encoder args, size patched into the sequence headers or None, rgba sample picks, yuv picks)
|
|
CASES = {
|
|
"qcif_200k": (src("testsrc2", 176, 144, 25, 1.2) + ["-b:v", "200k", "-bf", "0", "-g", "50"], None),
|
|
"main_450k": (src("testsrc2", 320, 180, 24, 1.5) + ["-b:v", "450k", "-bf", "0", "-g", "48"], None),
|
|
"zoom_150k": (src("mandelbrot", 320, 180, 24, 1.5) + ["-b:v", "150k", "-bf", "0", "-g", "48"], None),
|
|
"zoom_1500k": (src("mandelbrot", 320, 180, 24, 1) + ["-b:v", "1500k", "-bf", "0", "-g", "48"], None),
|
|
"big_1000k": (src("testsrc2", 640, 360, 24, 0.5) + ["-b:v", "1000k", "-bf", "0", "-g", "36"], None),
|
|
# odd sizes: ffmpeg's encoder only writes even ones, so the size in the sequence headers is patched; the
|
|
# reference is ffmpeg's decode of the unpatched stream, cropped (swscale's odd-size RGBA path interpolates
|
|
# chroma differently from its even-size one, which is what the decoder matches)
|
|
"odd_173x95": (src("mandelbrot", 174, 96, 24, 1) + ["-b:v", "300k", "-bf", "0", "-g", "24"], (173, 95)),
|
|
"bframes": (src("testsrc2", 320, 180, 24, 1) + ["-b:v", "600k", "-bf", "2", "-g", "24"], None),
|
|
"bframes_zoom": (src("mandelbrot", 176, 144, 24, 1.5) + ["-b:v", "250k", "-bf", "3", "-g", "16"], None),
|
|
"matrix": (src("testsrc2", 176, 144, 25, 1) + ["-b:v", "300k", "-bf", "0", "-g", "12",
|
|
"-intra_matrix", INTRA, "-inter_matrix", INTER], None),
|
|
"q1": (src("mandelbrot", 176, 144, 25, 0.6) + ["-q:v", "1", "-bf", "0", "-g", "8"], None),
|
|
"q31": (src("testsrc2", 176, 144, 25, 1) + ["-q:v", "31", "-bf", "0", "-g", "25"], None),
|
|
"zeromv": (src("mandelbrot", 176, 144, 25, 1) + ["-b:v", "300k", "-bf", "0", "-g", "25", "-me_method", "zero"], None),
|
|
}
|
|
|
|
|
|
def patch_size(es, w, h):
|
|
# every sequence header (00 00 01 B3): 12-bit width, 12-bit height
|
|
b = bytearray(es)
|
|
i = 0
|
|
while True:
|
|
i = b.find(b"\x00\x00\x01\xb3", i)
|
|
if i < 0:
|
|
return bytes(b)
|
|
b[i + 4] = w >> 4
|
|
b[i + 5] = ((w & 15) << 4) | (h >> 8)
|
|
b[i + 6] = h & 255
|
|
i += 4
|
|
|
|
|
|
def access_units(es):
|
|
# one unit per picture: from the first header byte before it (sequence / GOP) to the next unit start
|
|
starts = []
|
|
i = 0
|
|
pending = None
|
|
while True:
|
|
i = es.find(b"\x00\x00\x01", i)
|
|
if i < 0 or i + 3 >= len(es):
|
|
break
|
|
code = es[i + 3]
|
|
if code in (0xB3, 0xB8) and pending is None:
|
|
pending = i
|
|
elif code == 0x00:
|
|
starts.append(pending if pending is not None else i)
|
|
pending = None
|
|
i += 3
|
|
return [[s, (starts[k + 1] if k + 1 < len(starts) else len(es)) - s] for k, s in enumerate(starts)]
|
|
|
|
|
|
def crop(frames, w, h, cw, ch, bpp):
|
|
fs = w * h * bpp
|
|
out = bytearray()
|
|
for k in range(len(frames) // fs):
|
|
for y in range(ch):
|
|
o = k * fs + y * w * bpp
|
|
out += frames[o:o + cw * bpp]
|
|
return bytes(out)
|
|
|
|
|
|
def crop_yuv(frames, w, h, cw, ch):
|
|
fs = w * h + 2 * ((w + 1) // 2) * ((h + 1) // 2)
|
|
out = bytearray()
|
|
for k in range(len(frames) // fs):
|
|
f = frames[k * fs:(k + 1) * fs]
|
|
out += crop(f[:w * h], w, h, cw, ch, 1)
|
|
c = ((w + 1) // 2) * ((h + 1) // 2)
|
|
for pl in range(2):
|
|
out += crop(f[w * h + pl * c:w * h + (pl + 1) * c], (w + 1) // 2, (h + 1) // 2, (cw + 1) // 2, (ch + 1) // 2, 1)
|
|
return bytes(out)
|
|
|
|
|
|
def generate(ctx):
|
|
ffmpeg = shutil.which("ffmpeg") or ctx.nix("ffmpeg", ["sh", "-c", "command -v ffmpeg"]).decode().strip()
|
|
ffprobe = shutil.which("ffprobe") or os.path.join(os.path.dirname(ffmpeg), "ffprobe")
|
|
|
|
def ff(args):
|
|
subprocess.run([ffmpeg] + FF + args, check=True, capture_output=True)
|
|
|
|
with tempfile.TemporaryDirectory(prefix="formats-mpeg1-") as tmp:
|
|
def encode(name, args, size):
|
|
path = os.path.join(tmp, name + ".m1v")
|
|
ff(args + ENC + [path])
|
|
es = open(path, "rb").read()
|
|
return (patch_size(es, *size) if size else es), path
|
|
|
|
def reference(path, fmt):
|
|
out = path + "." + fmt
|
|
ff(["-i", path, "-f", "rawvideo", "-pix_fmt", fmt, out])
|
|
r = subprocess.run([ffprobe, "-v", "error", "-select_streams", "v:0", "-show_entries", "stream=width,height",
|
|
"-of", "csv=p=0", path], check=True, capture_output=True)
|
|
w, h = (int(x) for x in r.stdout.decode().strip().split(",")[:2])
|
|
return open(out, "rb").read(), w, h
|
|
|
|
for name, (args, size) in CASES.items():
|
|
es, path = encode(name, args, size)
|
|
rgba, w, h = reference(path, "rgba")
|
|
yuv, _, _ = reference(path, "yuv420p")
|
|
if size:
|
|
rgba, yuv = crop(rgba, w, h, size[0], size[1], 4), crop_yuv(yuv, w, h, size[0], size[1])
|
|
w, h = size
|
|
fs, ys = w * h * 4, w * h + 2 * ((w + 1) // 2) * ((h + 1) // 2)
|
|
n = len(rgba) // fs
|
|
means = []
|
|
for k in range(n):
|
|
fr = rgba[k * fs:(k + 1) * fs]
|
|
means.append([round(sum(fr[c::4]) / (w * h), 4) for c in range(3)])
|
|
# whole frames: the first P-picture and the last one (the most drift) as RGBA, three as yuv420p
|
|
picks = sorted({1, n - 1}) if n > 2 else list(range(n))
|
|
ypicks = sorted({0, n // 2, n - 1})
|
|
ctx.emit("mpeg1_" + name, {
|
|
"name": name, "width": w, "height": h, "frames": n, "file": b64(es), "units": access_units(es),
|
|
"means": means,
|
|
"rgba": [{"index": k + 1, "pixels": z64(rgba[k * fs:(k + 1) * fs])} for k in picks],
|
|
"yuv": [{"index": k + 1, "planes": z64(yuv[k * ys:(k + 1) * ys])} for k in ypicks],
|
|
})
|
|
|
|
# a size change mid-stream: 176x144 then 320x180 (each part decoded by ffmpeg on its own)
|
|
a, pa = encode("sc_a", src("testsrc2", 176, 144, 24, 0.5) + ["-b:v", "200k", "-bf", "0", "-g", "6"], None)
|
|
b, pb = encode("sc_b", src("mandelbrot", 320, 180, 24, 0.5) + ["-b:v", "400k", "-bf", "0", "-g", "6"], None)
|
|
parts = []
|
|
for path in (pa, pb):
|
|
rg, w, h = reference(path, "rgba")
|
|
n = len(rg) // (w * h * 4)
|
|
parts.append({"width": w, "height": h, "frames": n, "last": z64(rg[(n - 1) * w * h * 4:])})
|
|
ctx.emit("mpeg1_sizechange", {"file": b64(a + b), "parts": parts})
|
|
|
|
bench = []
|
|
for w, h, rate in ((320, 240, "600k"), (640, 480, "1500k")):
|
|
es, _ = encode("bench", src("testsrc2", w, h, 25, 2) + ["-b:v", rate, "-bf", "2", "-g", "25"], None)
|
|
bench.append({"width": w, "height": h, "file": b64(es), "units": access_units(es)})
|
|
ctx.emit("mpeg1_bench", bench)
|