118 lines
4.1 KiB
Luau
118 lines
4.1 KiB
Luau
-- audio/aac tests: ADTS streams from ffmpeg and fdkaac (stereo, mono 32 kHz) against ffmpeg's float decode (SNR,
|
|
-- length); an HE-AAC ADTS stream plays its core; a leading ID3v2 tag (skipped, text frames as tags) and garbage before
|
|
-- the first frame (resync); a truncated last frame; streaming + seek; sniff; errors; the fuzz no-throw pass.
|
|
-- Fixtures: tools/fixtures/m4a.py.
|
|
local T = require("@tests/T")
|
|
local AacF = require("@formats/audio/aac")
|
|
local Bytes = require("@formats/core/Bytes")
|
|
local Err = require("@formats/core/Err")
|
|
local Fx = require("@tests/gen/m4a_aac")
|
|
local He = require("@tests/gen/m4a_he")
|
|
|
|
local t = T.suite("aac")
|
|
|
|
local function reader(s: string): Bytes.Reader
|
|
return Bytes.reader(buffer.fromstring(s))
|
|
end
|
|
|
|
local function snr(a: any, ref: string, skip: number?): number
|
|
local off = (skip or 0) * a.channels
|
|
local n = math.min(a.frames * a.channels - off, #ref // 4)
|
|
local rb = buffer.fromstring(ref)
|
|
local sig, err = 0, 0
|
|
for i = 0, n - 1 do
|
|
local r = buffer.readf32(rb, i * 4)
|
|
local d = buffer.readf32(a.samples, (off + i) * 4) - r
|
|
sig += r * r
|
|
err += d * d
|
|
end
|
|
return if err == 0 then math.huge else 10 * math.log10(sig / err)
|
|
end
|
|
|
|
t:eq(AacF.name, "aac", "name")
|
|
t:check(AacF.caps.decode and not AacF.caps.encode and AacF.encode == nil, "caps: decode only")
|
|
|
|
local first
|
|
for _, c in Fx do
|
|
if c.container ~= "adts" then
|
|
continue
|
|
end
|
|
first = first or c
|
|
local r = reader(c.data)
|
|
t:check(AacF.sniff(r) >= 0.9, c.name .. ": sniff")
|
|
local a, err = AacF.decode(r)
|
|
t:check(a, c.name .. ": " .. tostring(err))
|
|
t:check(a.channels == c.channels and a.sampleRate == c.rate, c.name .. ": format")
|
|
t:eq(a.frames, c.frames, c.name .. ": frames (ADTS keeps the priming)")
|
|
local s = snr(a, c.ref)
|
|
t:check(s > 90, string.format("%s: SNR %.1f dB", c.name, s))
|
|
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 ADTS: the core
|
|
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))
|
|
end
|
|
end
|
|
|
|
-- ID3v2 in front + garbage, truncated tail
|
|
do
|
|
local function frame(id: string, text: string): string
|
|
local body = "\3" .. text
|
|
local n = #body
|
|
return id .. string.char(0, 0, n // 128, n % 128) .. "\0\0" .. body
|
|
end
|
|
local frames = frame("TIT2", "Adts Title") .. frame("TPE1", "Someone")
|
|
local n = #frames
|
|
local id3 = "ID3\4\0\0" .. string.char(0, 0, n // 128, n % 128) .. frames
|
|
local data = id3 .. "\0\1\2garbage" .. first.data
|
|
local a, err = AacF.decode(reader(data))
|
|
t:check(a, "id3 + garbage: " .. tostring(err))
|
|
t:check(a.tags.title == "Adts Title" and a.tags.artist == "Someone", "id3 tags")
|
|
t:eq(a.frames, first.frames, "id3 + garbage: same frames")
|
|
t:check(snr(a, first.ref) > 90, "id3 + garbage: same audio")
|
|
local cut = AacF.decode(reader(string.sub(first.data, 1, #first.data - 5))) :: any
|
|
t:check(cut and cut.frames == first.frames - 1024, "truncated last frame dropped")
|
|
end
|
|
|
|
-- streaming + seek
|
|
do
|
|
local whole = AacF.decode(reader(first.data)) :: any
|
|
local dec = AacF.open(reader(first.data)) :: any
|
|
for _, at in { 0.3, 0.0, 0.5123 } do
|
|
dec:seek(at)
|
|
local b, n = dec:read(600)
|
|
local f0 = math.floor(at * whole.sampleRate + 0.5)
|
|
local maxd = 0
|
|
for i = 0, n * whole.channels - 1 do
|
|
maxd = math.max(maxd, math.abs(buffer.readf32(b, i * 4) - buffer.readf32(whole.samples, (f0 * whole.channels + i) * 4)))
|
|
end
|
|
-- (the stream has no PNS: noise substitution would draw different noise after a seek)
|
|
t:check(n == 600 and maxd < 1e-6, string.format("seek %.3f s (max diff %g)", at, maxd))
|
|
end
|
|
end
|
|
|
|
-- errors
|
|
do
|
|
t:eq(AacF.sniff(reader("RIFF....WAVE")), 0, "sniff a WAV")
|
|
local _, e = AacF.decode(reader(string.rep("\0", 100)))
|
|
t:check(e and e.code == Err.CORRUPT, "no frames: " .. tostring(e))
|
|
end
|
|
|
|
-- fuzz
|
|
do
|
|
local samples = {}
|
|
for _, c in Fx do
|
|
if c.container == "adts" then
|
|
table.insert(samples, string.sub(c.data, 1, 6000))
|
|
end
|
|
end
|
|
t:fuzz("decode", function(b: buffer)
|
|
return AacF.decode(Bytes.reader(b))
|
|
end, samples, { mutations = 30 })
|
|
end
|
|
|
|
t:done()
|