Files

88 lines
5.2 KiB
Python

# FLV fixtures (tools/fixtures.py): files made by ffmpeg (Sorenson Spark + MP3 / PCM s16 / PCM u8 / Flash ADPCM mono + stereo /
# Nellymoser / Speex / A-law, H.264 with B-frames + AAC, Flash screen video), with ffprobe's streams and packets,
# ffmpeg's frames (CRC-32 of `-flags +bitexact -idct int` decoding; the first two raw) and audio (f32); then our FLV
# writer (tools/fixtures/_flv_encode.luau) remuxes FLV, MP4, Matroska and MPEG-TS sources and each output is probed,
# decoded and emitted the same way, so the test can read our files with our demuxer and compare them with ffmpeg's view.
# tests/gen/flv_cases.luau { { name, data, streams, packets, frames, yuv, pcm, pcmBytes } }
# tests/gen/flv_src.luau { mp4 = H.264 + AAC MP4, mkv = H.264 + MP3 Matroska, ts = H.264 + AAC MPEG-TS }
# tests/gen/flv_mux.luau { { name, source, data, streams, packets, frames, pcm, pcmBytes } | { name, err } }
import importlib.util, os, shutil, subprocess, tempfile
_spec = importlib.util.spec_from_file_location("_video_helpers", os.path.join(os.path.dirname(os.path.abspath(__file__)), "_video.py"))
V = importlib.util.module_from_spec(_spec)
_spec.loader.exec_module(V)
INPUTS = ["tools/fixtures/_video.py", "tools/fixtures/_flv_encode.luau", "src/Rosetta/video/flv", "src/Rosetta/codecs/video/Video"]
SRC = ["-f", "lavfi", "-i", "testsrc2=size=160x120:rate=12"]
def aud(rate, ch=1, freq=550):
return ["-f", "lavfi", "-i", "sine=frequency=%d:sample_rate=%d" % (freq, rate), "-ac", str(ch)]
def case(tmp, name, decode_audio=True):
path = os.path.join(tmp, name)
c = {"name": name, "data": open(path, "rb").read(), "streams": V.streams(path), "packets": V.packets(path)}
vs = [s for s in c["streams"] if s["kind"] == "video"]
if vs and vs[0]["codec"] in ("flv1", "h264"):
w, h = vs[0]["width"], vs[0]["height"]
raw = V.ffmpeg(["-flags", "+bitexact", "-idct", "int", "-i", path, "-map", "0:v:0", "-fps_mode", "passthrough", "-f",
"rawvideo", "-pix_fmt", "yuv420p", "-"])
n = w * h + 2 * ((w + 1) // 2) * ((h + 1) // 2)
frames = [raw[i:i + n] for i in range(0, len(raw) - n + 1, n)]
c["frames"] = [V.crc(f) for f in frames]
c["yuv"] = frames[:2]
if decode_audio and any(s["kind"] == "audio" for s in c["streams"]):
pcm = V.pcm_f32(path)
c["pcm"] = pcm[:8192 * 4]
c["pcmBytes"] = len(pcm)
return c
def generate(ctx):
tmp = tempfile.mkdtemp(prefix="flvfx_")
ff = lambda *a: V.ffmpeg(list(a), cwd=tmp)
ff(*SRC, *aud(44100), "-t", "1", "-c:v", "flv", "-b:v", "300k", "-g", "6", "-c:a", "libmp3lame", "-b:a", "64k", "sorenson_mp3.flv")
ff(*SRC, *aud(44100), "-t", "1", "-c:v", "libx264", "-preset", "veryfast", "-bf", "2", "-g", "6", "-c:a", "aac", "-b:a", "96k",
"h264_aac.flv")
ff(*SRC, *aud(22050, 2), "-t", "1", "-c:v", "flv", "-g", "12", "-c:a", "pcm_s16le", "sorenson_pcm.flv")
ff(*SRC, *aud(11025), "-t", "1", "-c:v", "flv", "-g", "12", "-c:a", "pcm_u8", "sorenson_pcm8.flv")
ff(*SRC, *aud(22050), "-t", "0.5", "-c:v", "flv", "-c:a", "adpcm_swf", "sorenson_adpcm.flv")
ff(*SRC, *aud(44100, 2), "-t", "0.5", "-c:v", "flv", "-c:a", "adpcm_swf", "sorenson_adpcm2.flv")
ff(*SRC, *aud(22050), "-t", "0.5", "-c:v", "flv", "-c:a", "nellymoser", "sorenson_nelly.flv")
ff(*SRC, *aud(16000), "-t", "0.5", "-c:v", "flv", "-c:a", "libspeex", "sorenson_speex.flv")
ff(*SRC, *aud(8000), "-t", "0.5", "-c:v", "flv", "-c:a", "pcm_alaw", "sorenson_alaw.flv")
ff("-f", "lavfi", "-i", "testsrc2=size=64x48:rate=10", "-t", "0.5", "-c:v", "flashsv", "screen.flv")
ff(*aud(44100, 2), "-t", "0.5", "-c:a", "libmp3lame", "audio_only.flv")
names = ["sorenson_mp3.flv", "h264_aac.flv", "sorenson_pcm.flv", "sorenson_pcm8.flv", "sorenson_adpcm.flv",
"sorenson_adpcm2.flv", "sorenson_nelly.flv", "sorenson_speex.flv", "sorenson_alaw.flv", "screen.flv", "audio_only.flv"]
cases = [case(tmp, n) for n in names]
ctx.emit("flv_cases", cases)
# sources for the writer
ff(*SRC, *aud(44100), "-t", "1", "-c:v", "libx264", "-preset", "veryfast", "-bf", "2", "-g", "6", "-c:a", "aac", "src.mp4")
ff(*SRC, *aud(44100, 2), "-t", "1", "-c:v", "libx264", "-preset", "veryfast", "-bf", "0", "-g", "12", "-c:a", "libmp3lame",
"src.mkv")
ff(*SRC, *aud(44100), "-t", "1", "-c:v", "libx264", "-preset", "veryfast", "-c:a", "aac", "src.ts")
srcs = {k: open(os.path.join(tmp, "src." + k), "rb").read() for k in ("mp4", "mkv", "ts")}
ctx.emit("flv_src", srcs)
luau = os.environ.get("LUAU") or shutil.which("luau")
cmd = [luau, "tools/fixtures/_flv_encode.luau"] if luau else ["nix", "shell", "nixpkgs#luau", "-c", "luau", "tools/fixtures/_flv_encode.luau"]
out = subprocess.run(cmd, capture_output=True, check=True, cwd=ctx.root).stdout.decode()
mux = []
for line in out.split("\n"):
if not line.strip():
continue
name, source, rest = line.split(" ", 2)
if rest.startswith("ERR "):
mux.append({"name": name, "source": source, "err": rest[4:]})
continue
open(os.path.join(tmp, name), "wb").write(bytes.fromhex(rest))
c = case(tmp, name)
c["source"] = source
c.pop("yuv", None)
mux.append(c)
ctx.emit("flv_mux", mux)