134 lines
6.8 KiB
Python
134 lines
6.8 KiB
Python
# VP8 video fixtures (tools/fixtures.py) for tests/codecs/vp8video.luau and tools/bench.luau. Clips are encoded by
|
|
# ffmpeg's libvpx into IVF (many encoder settings: alt-ref / hidden frames, golden refreshes, token partitions, error
|
|
# resilience, profiles 1-3, sharpness, segmentation, q extremes, short key frame intervals, motion off the frame edges),
|
|
# plus the official VP8 test vectors (tests/fixtures/vp8video/vp80-*.ivf, from the libvpx test data, BSD:
|
|
# licenses/libvpx-LICENSE.txt). The reference output is ffmpeg's native vp8 decoder (bit-exact with libvpx):
|
|
# `ffmpeg -c:v vp8 -i x.ivf -noautoscale -fps_mode passthrough -f framehash -hash crc32 -`: the CRC32 of every
|
|
# shown yuv420p frame (and its size: key frames may change it).
|
|
# tests/gen/vp8video_cases.luau { name, ivf, width, height (of the first frame), crcs, sizes (bytes) }
|
|
# tests/gen/vp8video_bench.luau { { name, ivf, width, height, frames, crcs, sizes } } for tools/bench.luau
|
|
import os, shutil, subprocess, tempfile, zlib
|
|
|
|
INPUTS = ["tests/fixtures/vp8video"]
|
|
|
|
VECTORS = "tests/fixtures/vp8video"
|
|
|
|
|
|
def ffmpeg(ctx, args):
|
|
"""runs ffmpeg (the one on PATH when it has libvpx, else nixpkgs#ffmpeg) and returns stdout"""
|
|
exe = shutil.which("ffmpeg")
|
|
if exe:
|
|
enc = subprocess.run([exe, "-hide_banner", "-encoders"], capture_output=True).stdout
|
|
if b"libvpx " in enc:
|
|
return subprocess.run([exe, "-hide_banner", "-loglevel", "error"] + args, capture_output=True, check=True).stdout
|
|
return ctx.nix("ffmpeg", ["ffmpeg", "-hide_banner", "-loglevel", "error"] + args)
|
|
|
|
|
|
def ivf_size(data):
|
|
return data[12] | data[13] << 8, data[14] | data[15] << 8
|
|
|
|
|
|
def reference(ctx, path):
|
|
"""ffmpeg's decode: (crc32, byte size) of each shown yuv420p frame (sizes may change at key frames)"""
|
|
out = ffmpeg(ctx, ["-c:v", "vp8", "-i", path, "-noautoscale", "-fps_mode", "passthrough", "-f", "framehash",
|
|
"-hash", "crc32", "-"])
|
|
frames = []
|
|
for line in out.decode().splitlines():
|
|
if line and not line.startswith("#"):
|
|
f = [x.strip() for x in line.split(",")]
|
|
frames.append((int(f[5], 16), int(f[4])))
|
|
return frames
|
|
|
|
|
|
# (name, lavfi source, frames, encoder options)
|
|
SRC_320 = "testsrc2=s=320x240:r=25"
|
|
PAN = "testsrc2=s=480x360:r=25,crop=w=320:h=240:x='80+70*sin(t*3)':y='60+50*cos(t*2)'"
|
|
CLIPS = [
|
|
("default_320x240", SRC_320, 20, ["-b:v", "400k"]),
|
|
("odd_173x95", "mandelbrot=s=173x95:r=25", 16, ["-b:v", "200k"]),
|
|
("pan_320x240", PAN, 24, ["-b:v", "300k", "-g", "100"]),
|
|
("altref_352x288", "testsrc2=s=352x288:r=25", 30,
|
|
["-b:v", "300k", "-auto-alt-ref", "1", "-lag-in-frames", "16", "-arnr-maxframes", "7", "-arnr-strength", "5",
|
|
"-arnr-type", "3", "2pass"]),
|
|
("altref_pan", PAN, 30, ["-b:v", "250k", "-auto-alt-ref", "1", "-lag-in-frames", "25", "-g", "200", "2pass"]),
|
|
("tokenparts3", SRC_320, 12, ["-b:v", "500k", "-slices", "8"]),
|
|
("tokenparts1_odd", "mandelbrot=s=201x77:r=25", 12, ["-b:v", "300k", "-slices", "2"]),
|
|
("error_resilient", PAN, 16, ["-b:v", "300k", "-error-resilient", "1"]),
|
|
("profile1", PAN, 16, ["-b:v", "300k", "-profile:v", "1"]),
|
|
("profile2", PAN, 16, ["-b:v", "300k", "-profile:v", "2"]),
|
|
("profile3", PAN, 16, ["-b:v", "300k", "-profile:v", "3"]),
|
|
("sharpness7", PAN, 12, ["-b:v", "200k", "-sharpness", "7"]),
|
|
("sharpness3", "mandelbrot=s=160x120:r=25", 12, ["-b:v", "150k", "-sharpness", "3"]),
|
|
("realtime_cyclic", PAN, 24,
|
|
["-deadline", "realtime", "-cpu-used", "8", "-b:v", "200k", "-static-thresh", "100", "-screen-content-mode", "1"]),
|
|
("realtime_noise", "testsrc2=s=320x240:r=25,noise=alls=30:allf=t", 16,
|
|
["-deadline", "realtime", "-cpu-used", "4", "-b:v", "400k"]),
|
|
("best_split", "testsrc2=s=176x144:r=25,rotate=a='t*0.4':c=black", 16, ["-deadline", "best", "-b:v", "250k"]),
|
|
("cellauto_split", "cellauto=s=176x144:rule=110:r=25,scale=176:144", 16, ["-b:v", "300k", "-cpu-used", "0"]),
|
|
("q_high", PAN, 10, ["-qmin", "0", "-qmax", "2", "-b:v", "20M"]),
|
|
("q_low", PAN, 16, ["-qmin", "60", "-qmax", "63", "-b:v", "20k"]),
|
|
("keyint5", PAN, 16, ["-b:v", "300k", "-g", "5"]),
|
|
("golden", "testsrc2=s=240x160:r=25", 40, ["-b:v", "150k", "-g", "300", "-auto-alt-ref", "0"]),
|
|
("big_motion", "testsrc2=s=640x480:r=25,crop=w=192:h=144:x='224+200*sin(t*9)':y='168+150*cos(t*7)'", 16,
|
|
["-b:v", "300k"]),
|
|
("tiny_24x18", "testsrc2=s=24x18:r=25", 12, ["-b:v", "50k"]),
|
|
("temporal_layers", PAN, 16,
|
|
["-deadline", "realtime", "-b:v", "300k", "-ts-parameters",
|
|
"ts_number_layers=2:ts_target_bitrate=150,300:ts_rate_decimator=2,1:ts_periodicity=2:ts_layer_id=0,1"]),
|
|
]
|
|
|
|
BENCH = [
|
|
("bench_320x240", "testsrc2=s=480x360:r=25,crop=w=320:h=240:x='80+70*sin(t*3)':y='60+50*cos(t*2)'", 40,
|
|
["-b:v", "500k"]),
|
|
("bench_640x480", "testsrc2=s=960x720:r=25,crop=w=640:h=480:x='160+150*sin(t*3)':y='120+100*cos(t*2)'", 30,
|
|
["-b:v", "1200k"]),
|
|
]
|
|
|
|
|
|
def encode(ctx, tmp, name, src, frames, opts):
|
|
path = os.path.join(tmp, name + ".ivf")
|
|
base = ["-f", "lavfi", "-i", src, "-frames:v", str(frames), "-pix_fmt", "yuv420p", "-c:v", "libvpx"]
|
|
try:
|
|
if "2pass" in opts: # alt-ref frames need the two-pass encoder
|
|
opts = [o for o in opts if o != "2pass"]
|
|
log = os.path.join(tmp, name + "-pass")
|
|
ffmpeg(ctx, base + opts + ["-pass", "1", "-passlogfile", log, "-f", "null", "-"])
|
|
opts = opts + ["-pass", "2", "-passlogfile", log]
|
|
ffmpeg(ctx, base + opts + ["-f", "ivf", "-y", path])
|
|
except subprocess.CalledProcessError as e:
|
|
if name == "temporal_layers": # optional: depends on the libvpx wrapper's options
|
|
return None
|
|
raise RuntimeError("vp8video: %s: %s" % (name, e.stderr.decode(errors="replace")))
|
|
return path
|
|
|
|
|
|
def case(ctx, name, path):
|
|
with open(path, "rb") as f:
|
|
data = f.read()
|
|
w, h = ivf_size(data)
|
|
ref = reference(ctx, path)
|
|
return {"name": name, "ivf": data, "width": w, "height": h, "crcs": [r[0] for r in ref], "sizes": [r[1] for r in ref]}
|
|
|
|
|
|
def generate(ctx):
|
|
tmp = tempfile.mkdtemp(prefix="vp8video")
|
|
try:
|
|
cases = []
|
|
vec = os.path.join(ctx.root, VECTORS)
|
|
for f in sorted(os.listdir(vec)):
|
|
if f.endswith(".ivf"):
|
|
cases.append(case(ctx, f[:-4], os.path.join(vec, f)))
|
|
for name, src, frames, opts in CLIPS:
|
|
path = encode(ctx, tmp, name, src, frames, opts)
|
|
if path:
|
|
cases.append(case(ctx, name, path))
|
|
ctx.emit("vp8video_cases", cases)
|
|
bench = []
|
|
for name, src, frames, opts in BENCH:
|
|
c = case(ctx, name, encode(ctx, tmp, name, src, frames, opts))
|
|
c["frames"] = len(c["crcs"])
|
|
bench.append(c)
|
|
ctx.emit("vp8video_bench", bench)
|
|
finally:
|
|
shutil.rmtree(tmp)
|