Files

90 lines
4.6 KiB
Python

# HE-AAC (SBR) / HE-AACv2 (SBR + PS) fixtures (tools/fixtures.py). fdkaac encodes; ffmpeg's native decoder is the
# reference; ffmpeg also remuxes into M4A (implicit signalling), MPEG-TS, MP4 and Matroska (with a tiny MPEG-1 video).
# tests/gen/heaac.luau { name, data, container ("adts" | "m4a" | "ts" | "mp4" | "mkv"), signalling ("explicit" |
# "implicit"), ps, rate (the output rate), core (the core rate), channels (our output: a mono
# core without PS stays mono; ffmpeg's duplicate channel is dropped from ref), frames (ffmpeg's),
# ref = ffmpeg's decode as s16le, or same = the name of an earlier case ffmpeg decodes identically }
import math, os, random, struct, tempfile
PKGS = ["ffmpeg", "fdk-aac-encoder"]
INPUTS = []
def music(frames, channels, rate, seed):
rng = random.Random(seed)
out = []
for i in range(frames):
t = i / rate
for c in range(channels):
v = 0.2 * math.sin(2 * math.pi * (220 * (c + 1)) * t + 3 * math.sin(2 * math.pi * 5 * t))
v += 0.1 * math.sin(2 * math.pi * 660 * t) + 0.06 * math.sin(2 * math.pi * 1320.5 * t + c)
v += 0.04 * math.sin(2 * math.pi * (5000 + 300 * c) * t) + 0.03 * math.sin(2 * math.pi * (9000 + 700 * c) * t)
v += 0.08 * (rng.random() * 2 - 1)
if (i // (rate // 8)) % 3 == 1:
v += 0.12 * (rng.random() * 2 - 1)
if i % (rate // 3) < 40:
v += 0.5 * (1 - (i % (rate // 3)) / 40) * (1 if (i // 7) % 2 else -1)
out.append(max(-32768, min(32767, int(round(v * 20000)))))
return out
def wav(ints, ch, rate):
data = struct.pack("<%dh" % len(ints), *ints)
fmt = struct.pack("<HHIIHH", 1, ch, rate, rate * ch * 2, ch * 2, 16)
return b"RIFF" + struct.pack("<I", 36 + len(data)) + b"WAVEfmt " + struct.pack("<I", 16) + fmt + b"data" + struct.pack("<I", len(data)) + data
def generate(ctx):
cases = []
with tempfile.TemporaryDirectory() as d:
def sh(cmd):
return ctx.nix(PKGS, ["sh", "-c", "cd '%s' && %s" % (d, cmd)])
def read(name):
with open(os.path.join(d, name), "rb") as f:
return f.read()
def add(name, container, signalling, ps, rate, ch, outch):
raw = sh("ffmpeg -v error -i %s -vn -f s16le -" % name)
ffch = 2 if ch == 1 else ch # ffmpeg outputs a mono SBR stream as stereo (implicit PS)
frames = len(raw) // (2 * ffch)
if outch != ffch:
s = struct.unpack("<%dh" % (frames * ffch), raw[: frames * ffch * 2])
raw = struct.pack("<%dh" % frames, *s[0::ffch])
case = {"name": name, "data": read(name), "container": container, "signalling": signalling, "ps": ps,
"rate": rate, "core": rate // 2, "channels": outch, "frames": frames}
for c in cases:
if c.get("ref") == raw:
case["same"] = c["name"] # the same decode as an earlier case: its ref is not repeated
break
else:
case["ref"] = raw
cases.append(case)
video = "-f lavfi -i testsrc=s=32x32:d=1:r=24"
for base, prof, br, ch, rate, extra in (
("v1_stereo_48k", 5, 64000, 2, 48000, ("m4a_implicit", "ts", "mp4", "mkv")),
("v1_mono_48k", 5, 32000, 1, 48000, ()),
("v2_48k", 29, 32000, 2, 48000, ("ts",)),
("v1_stereo_44k", 5, 48000, 2, 44100, ()),
("v1_stereo_32k", 5, 32000, 2, 32000, ()),
("v1_51_48k", 5, 160000, 6, 48000, ()),
):
with open(os.path.join(d, base + ".wav"), "wb") as f:
f.write(wav(music(rate, ch, rate, 7 + ch), ch, rate))
sh("fdkaac -S -p %d -b %d -f 2 -o %s.aac %s.wav" % (prof, br, base, base))
ps = prof == 29
outch = 2 if ps else ch
add(base + ".aac", "adts", "implicit", ps, rate, ch, outch)
if ch <= 2:
sh("fdkaac -S -p %d -b %d -o %s.m4a %s.wav" % (prof, br, base, base))
add(base + ".m4a", "m4a", "explicit", ps, rate, ch, outch)
for x in extra:
if x == "m4a_implicit":
sh("ffmpeg -v error -i %s.aac -c copy %s_implicit.m4a" % (base, base))
add(base + "_implicit.m4a", "m4a", "implicit", ps, rate, ch, outch)
else:
sh("ffmpeg -v error %s -i %s.m4a -map 0:v -map 1:a -c:v mpeg1video -b:v 50k -c:a copy %s.%s" % (video, base, base, x))
add(base + "." + x, x, "explicit", ps, rate, ch, outch)
ctx.emit("heaac", cases)