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
|
||||
|
||||
Reference in new issue
Block a user