tests: HE-AAC against ffmpeg (ADTS, M4A explicit / implicit, TS, MP4, MKV; mono, stereo, 5.1, PS)
This commit is contained in:
1 parent
686a1fbf86
commit
778502c54d
4 files changed
+304
-10
No files matched your search
@@ -50,7 +50,9 @@ end
|
||||
for _, c in He do
|
||||
if string.sub(c.name, -4) == ".aac" then
|
||||
local a, err = AacF.decode(reader(c.data))
|
||||
t:check(a and a.sampleRate == c.rate // 2 and a.frames > 0, c.name .. ": core decode: " .. tostring(err))
|
||||
t:check(a and a.sampleRate == c.rate and a.frames > 0, c.name .. ": SBR found in ADTS: " .. tostring(err))
|
||||
local core, cerr = AacF.decode(reader(c.data), { sbr = false })
|
||||
t:check(core and core.sampleRate == c.rate // 2 and core.frames * 2 == a.frames, c.name .. ": sbr = false: " .. tostring(cerr))
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -0,0 +1,201 @@
|
||||
-- HE-AAC tests: SBR (mono, stereo, 5.1) and SBR + PS from fdkaac in ADTS, M4A (explicit and implicit signalling),
|
||||
-- MPEG-TS, MP4 and Matroska against ffmpeg's decode (SNR, output rate, length, energy above the core's Nyquist),
|
||||
-- opts.sbr = false, backward-compatible explicit signalling, seeking, and the fuzz pass. Fixtures: tools/fixtures/heaac.py.
|
||||
local T = require("@tests/T")
|
||||
local F = require("@rosetta")
|
||||
local Bytes = require("@rosetta/core/Bytes")
|
||||
local Aac = require("@rosetta/codecs/audio/Aac")
|
||||
local AacF = require("@rosetta/audio/aac")
|
||||
local M4a = require("@rosetta/audio/m4a")
|
||||
local Fx = require("@tests/gen/heaac")
|
||||
|
||||
local t = T.suite("heaac")
|
||||
|
||||
local byName: { [string]: any } = {}
|
||||
for _, c in Fx do
|
||||
byName[c.name] = c
|
||||
end
|
||||
|
||||
local function reader(s: string): Bytes.Reader
|
||||
return Bytes.reader(buffer.fromstring(s))
|
||||
end
|
||||
|
||||
-- every frame of a Decoder as one f32 buffer
|
||||
local function drain(d: any): (buffer, number)
|
||||
local parts, total = {}, 0
|
||||
while true do
|
||||
local b, n = d:read(4096)
|
||||
if not n or n == 0 then
|
||||
break
|
||||
end
|
||||
table.insert(parts, buffer.readstring(b, 0, n * d.channels * 4))
|
||||
total += n
|
||||
end
|
||||
return buffer.fromstring(table.concat(parts)), total
|
||||
end
|
||||
|
||||
local function open(c: any, opts: any?): any
|
||||
if c.container == "adts" then
|
||||
return AacF.open(reader(c.data), opts)
|
||||
elseif c.container == "m4a" then
|
||||
return M4a.open(reader(c.data), opts)
|
||||
end
|
||||
local o = opts or {}
|
||||
local v, err = F.video.open(buffer.fromstring(c.data), { sbr = o.sbr })
|
||||
if not v then
|
||||
return nil, err
|
||||
end
|
||||
return v.audio, v.audioError
|
||||
end
|
||||
|
||||
-- a 63-tap windowed-sinc high-pass at a quarter of the output rate: the band SBR fills
|
||||
local HP = table.create(63, 0)
|
||||
for i = 0, 62 do
|
||||
local x = i - 31
|
||||
local lp = if x == 0 then 0.5 else math.sin(math.pi * 0.5 * x) / (math.pi * x)
|
||||
HP[i + 1] = ((if x == 0 then 1 else 0) - lp) * (0.54 - 0.46 * math.cos(2 * math.pi * i / 62))
|
||||
end
|
||||
|
||||
-- SNR (dB) of ours (f32) against ref (s16le) over n frames; the high band's energy ours / ref (dB) and its share of
|
||||
-- the reference's energy
|
||||
local function compare(samples: buffer, ref: string, ch: number, n: number): (number, number, number)
|
||||
local rb = buffer.fromstring(ref)
|
||||
local m = math.min(n, buffer.len(rb) // (2 * ch), buffer.len(samples) // (4 * ch))
|
||||
local sig, err = 0, 0
|
||||
local r, x = table.create(m, 0), table.create(m, 0)
|
||||
local hiOurs, hiRef = 0, 0
|
||||
for c = 0, ch - 1 do
|
||||
for i = 0, m - 1 do
|
||||
local k = i * ch + c
|
||||
r[i + 1] = buffer.readi16(rb, k * 2) / 32768
|
||||
x[i + 1] = buffer.readf32(samples, k * 4)
|
||||
sig += r[i + 1] ^ 2
|
||||
err += (x[i + 1] - r[i + 1]) ^ 2
|
||||
end
|
||||
for i = 64, m - 64, 3 do
|
||||
local hr, hx = 0, 0
|
||||
for j = 1, 63 do
|
||||
hr += r[i + j - 31] * HP[j]
|
||||
hx += x[i + j - 31] * HP[j]
|
||||
end
|
||||
hiRef += hr * hr
|
||||
hiOurs += hx * hx
|
||||
end
|
||||
end
|
||||
local snr = if err == 0 then math.huge else 10 * math.log10(sig / err)
|
||||
return snr, 10 * math.log10((hiOurs + 1e-30) / (hiRef + 1e-30)), 3 * hiRef / (sig + 1e-30)
|
||||
end
|
||||
|
||||
-- 1. every fixture against ffmpeg
|
||||
local decoded: { [string]: buffer } = {}
|
||||
for _, c in Fx do
|
||||
local d, err = open(c)
|
||||
t:check(d, c.name .. ": " .. tostring(err))
|
||||
t:eq(d.sampleRate, c.rate, c.name .. ": output rate")
|
||||
t:eq(d.channels, c.channels, c.name .. ": channels")
|
||||
local samples, n = drain(d)
|
||||
decoded[c.name] = samples
|
||||
if c.container == "mkv" then
|
||||
-- (Matroska's end trimming is the container's; the start is checked through the SNR)
|
||||
t:check(n >= c.frames, c.name .. ": frames " .. n)
|
||||
else
|
||||
t:eq(n, c.frames, c.name .. ": frames")
|
||||
t:eq(d.frames, c.frames, c.name .. ": the stream info's frames")
|
||||
end
|
||||
local ref = c.ref or (byName[c.same] :: any).ref
|
||||
local snr, hiDb, hiShare = compare(samples, ref, c.channels, math.min(n, c.frames))
|
||||
t:check(snr > 60, string.format("%s: SNR %.1f dB", c.name, snr))
|
||||
t:check(math.abs(hiDb) < 0.1 and hiShare > 1e-3, string.format("%s: high band %.3f dB vs ffmpeg (%.1f dB of the signal)", c.name, hiDb, 10 * math.log10(hiShare)))
|
||||
t:note("%s (%s, %s%s): %d ch %d Hz, %d frames, SNR %.1f dB vs ffmpeg", c.name, c.container, c.signalling,
|
||||
if c.ps then ", PS" else "", d.channels, d.sampleRate, n, snr)
|
||||
end
|
||||
|
||||
-- 2. opts.sbr = false: the AAC-LC core at the core rate
|
||||
for _, name in { "v1_stereo_48k.aac", "v1_stereo_48k.m4a", "v2_48k.aac", "v1_stereo_48k.ts" } do
|
||||
local c = byName[name]
|
||||
local d, err = open(c, { sbr = false })
|
||||
t:check(d, name .. ": sbr = false: " .. tostring(err))
|
||||
t:eq(d.sampleRate, c.core, name .. ": sbr = false: core rate")
|
||||
t:eq(d.channels, if c.ps then 1 else c.channels, name .. ": sbr = false: core channels")
|
||||
local _, n = drain(d)
|
||||
t:check(n > 0 and math.abs(n * 2 - c.frames) <= 4096, name .. ": sbr = false: frames " .. n)
|
||||
end
|
||||
|
||||
-- 3. backward-compatible explicit signalling (14496-3 1.6.5.2: AAC-LC config + sync extension 0x2b7, SBR, ext rate)
|
||||
do
|
||||
local c = byName["v1_stereo_48k.aac"]
|
||||
local data = buffer.fromstring(c.data)
|
||||
local h = Aac.adts(data, 0, buffer.len(data)) :: Aac.Adts
|
||||
-- aot 2 (5) sfi (4) ch (4) GASpecific 000 (3) | 0x2b7 (11) aot 5 (5) sbr 1 (1) extSfi (4) = 37 bits
|
||||
local bitsOut = { { 2, 5 }, { h.sfIndex, 4 }, { 2, 4 }, { 0, 3 }, { 0x2b7, 11 }, { 5, 5 }, { 1, 1 }, { h.sfIndex - 3, 4 } }
|
||||
local asc = buffer.create(5)
|
||||
local pos = 0
|
||||
for _, f in bitsOut do
|
||||
for b = f[2] - 1, 0, -1 do
|
||||
if bit32.band(bit32.rshift(f[1], b), 1) == 1 then
|
||||
local byte = pos // 8
|
||||
buffer.writeu8(asc, byte, bit32.bor(buffer.readu8(asc, byte), bit32.lshift(1, 7 - pos % 8)))
|
||||
end
|
||||
pos += 1
|
||||
end
|
||||
end
|
||||
local cfg = Aac.config(asc) :: Aac.Config
|
||||
t:check(cfg and cfg.sbr and cfg.extensionRate == c.rate, "backward-compatible signalling parsed")
|
||||
local d = Aac.decoder(asc) :: Aac.Decoder
|
||||
t:check(d.sampleRate == c.rate and d.frameLength == 2048 and d.sbrOutput, "explicit SBR without probing")
|
||||
local whole = decoded["v1_stereo_48k.aac"]
|
||||
local p, frame, maxd = 0, 0, 0
|
||||
while true do
|
||||
local fh = Aac.adts(data, p, buffer.len(data))
|
||||
if not fh then
|
||||
break
|
||||
end
|
||||
local n = d:decode(data, p + fh.headerSize, fh.frameSize - fh.headerSize) :: number
|
||||
for i = 1, n * 2 do
|
||||
maxd = math.max(maxd, math.abs(d.pcm[i] - buffer.readf32(whole, (frame * 4096 + i - 1) * 4)))
|
||||
end
|
||||
frame += 1
|
||||
p += fh.frameSize
|
||||
end
|
||||
t:check(maxd < 1e-6, "explicit decoder = the ADTS path (max diff " .. maxd .. ")")
|
||||
local lc = Aac.decoder(asc, { sbr = false }) :: Aac.Decoder
|
||||
t:check(lc.sampleRate == c.core and not lc.sbrOutput, "sbr = false with explicit signalling")
|
||||
end
|
||||
|
||||
-- 4. seeking: the position is exact; the SBR noise phase restarts, so compare energies after the pre-roll
|
||||
for _, name in { "v1_stereo_48k.m4a", "v2_48k.aac" } do
|
||||
local c = byName[name]
|
||||
local whole = decoded[name]
|
||||
local d = open(c)
|
||||
for _, at in { 0.5, 0.123 } do
|
||||
d:seek(at)
|
||||
local b, n = d:read(2048)
|
||||
local f0 = math.floor(at * c.rate + 0.5)
|
||||
local e1, e2, diff = 0, 0, 0
|
||||
for i = 0, n * c.channels - 1 do
|
||||
local x, y = buffer.readf32(b, i * 4), buffer.readf32(whole, (f0 * c.channels + i) * 4)
|
||||
e1 += x * x
|
||||
e2 += y * y
|
||||
diff += (x - y) ^ 2
|
||||
end
|
||||
local snr = 10 * math.log10(e2 / math.max(diff, 1e-30))
|
||||
t:check(n == 2048 and snr > 15, string.format("%s: seek %.3f s (SNR %.1f dB vs a straight decode)", name, at, snr))
|
||||
end
|
||||
end
|
||||
|
||||
-- 5. the fuzz pass
|
||||
do
|
||||
local samples = {}
|
||||
for _, name in { "v1_stereo_48k.aac", "v2_48k.aac", "v1_mono_48k.aac", "v1_51_48k.aac" } do
|
||||
table.insert(samples, string.sub(byName[name].data, 1, 3000))
|
||||
end
|
||||
t:fuzz("adts", function(b: buffer)
|
||||
return AacF.decode(Bytes.reader(b))
|
||||
end, samples, { mutations = 40 })
|
||||
local m4as = { string.sub(byName["v2_48k.m4a"].data, 1, 6000), byName["v1_stereo_48k_implicit.m4a"].data }
|
||||
t:fuzz("m4a", function(b: buffer)
|
||||
return M4a.decode(Bytes.reader(b))
|
||||
end, m4as, { mutations = 20, keepHeader = 2000 })
|
||||
end
|
||||
|
||||
t:done()
|
||||
+11
-9
@@ -79,20 +79,22 @@ for _, c in Aac do
|
||||
t:note("%s: %d ch %d Hz, %d frames, SNR %.1f dB vs ffmpeg", c.name, c.channels, c.rate, a.frames, s)
|
||||
end
|
||||
|
||||
-- HE-AAC: the AAC-LC core at half the rate, SBR seen
|
||||
-- HE-AAC: SBR output at the full rate (tests/audio/heaac.luau checks it against ffmpeg); opts.sbr = false: the core
|
||||
for _, c in He do
|
||||
if string.sub(c.name, -4) ~= ".m4a" then
|
||||
continue
|
||||
end
|
||||
local dec, err = M4a.open(reader(c.data))
|
||||
t:check(dec, c.name .. ": " .. tostring(err))
|
||||
t:eq(dec.sampleRate, c.rate // 2, c.name .. ": core rate")
|
||||
local b, n = dec:read(8192)
|
||||
local e = 0
|
||||
for i = 0, n * dec.channels - 1 do
|
||||
e += buffer.readf32(b, i * 4) ^ 2
|
||||
for _, sbr in { true, false } do
|
||||
local dec, err = M4a.open(reader(c.data), { sbr = sbr })
|
||||
t:check(dec, c.name .. ": " .. tostring(err))
|
||||
t:eq(dec.sampleRate, if sbr then c.rate else c.rate // 2, c.name .. (if sbr then ": SBR rate" else ": core rate"))
|
||||
local b, n = dec:read(8192)
|
||||
local e = 0
|
||||
for i = 0, n * dec.channels - 1 do
|
||||
e += buffer.readf32(b, i * 4) ^ 2
|
||||
end
|
||||
t:check(n == 8192 and e > 1, c.name .. ": decodes (energy " .. e .. ")")
|
||||
end
|
||||
t:check(n == 8192 and e > 1, c.name .. ": the core decodes (energy " .. e .. ")")
|
||||
end
|
||||
|
||||
do
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
# 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)
|
||||
Reference in new issue
Block a user