ws-m4a: codecs/Aac (AAC-LC decoder: ASC / PCE / ADTS, all tools, SBR/PS extension hook), codecs/Alac (decoder + encoder), audio/m4a (AAC + ALAC read, ALAC write, tags, gapless, seek), audio/aac (ADTS), audio/caf (lpcm/alac/aac/ulaw/alaw read, lpcm/alac write); fixtures checked by ffmpeg / fdkaac / MP4Box
Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
8b29568e0f
commit
f66594908c
22 files changed
+5380
No files matched your search
@@ -0,0 +1,182 @@
|
||||
-- codecs/Aac tests: AudioSpecificConfig parsing (every fixture's esds, explicit SBR / PS signalling from fdkaac,
|
||||
-- crafted configs: PCE channel counts, 960-sample frames, Main / LTP object types), ADTS headers, raw frames decoded
|
||||
-- straight from ADTS against ffmpeg's float decode, the FIL extension hook on HE-AAC (implicit SBR), reset, errors,
|
||||
-- the fuzz no-throw pass on raw frames. Fixtures: tools/fixtures/m4a.py.
|
||||
local T = require("@tests/T")
|
||||
local Aac = require("@formats/codecs/Aac")
|
||||
local IsoBmff = require("@formats/containers/IsoBmff")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Bits = require("@formats/core/Bits")
|
||||
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-codec")
|
||||
|
||||
-- ADTS frames of a stream: { offset, size } of the raw blocks
|
||||
local function frames(data: buffer): ({ { number } }, Aac.Adts?)
|
||||
local out, p, first = {}, 0, nil
|
||||
while true do
|
||||
local h = Aac.adts(data, p, buffer.len(data))
|
||||
if not h or p + h.frameSize > buffer.len(data) then
|
||||
break
|
||||
end
|
||||
first = first or h
|
||||
table.insert(out, { p + h.headerSize, h.frameSize - h.headerSize })
|
||||
p += h.frameSize
|
||||
end
|
||||
return out, first
|
||||
end
|
||||
|
||||
-- ASCs from the M4A fixtures
|
||||
for _, c in Fx do
|
||||
if c.container == "m4a" then
|
||||
local r = Bytes.reader(buffer.fromstring(c.data))
|
||||
local movie = IsoBmff.parse(r) :: IsoBmff.Movie
|
||||
local cfg, err = Aac.config(movie.tracks[1].config :: buffer)
|
||||
t:check(cfg, c.name .. ": " .. tostring(err))
|
||||
t:check(cfg.objectType == 2 and cfg.sampleRate == c.rate and cfg.channels == c.channels and not cfg.sbr, c.name .. ": AAC-LC config")
|
||||
end
|
||||
end
|
||||
for _, c in He do
|
||||
if string.sub(c.name, -4) == ".m4a" then
|
||||
local r = Bytes.reader(buffer.fromstring(c.data))
|
||||
local movie = IsoBmff.parse(r) :: IsoBmff.Movie
|
||||
local cfg, err = Aac.config(movie.tracks[1].config :: buffer)
|
||||
t:check(cfg, c.name .. ": " .. tostring(err))
|
||||
t:check(cfg.sbr and cfg.sampleRate == c.rate // 2 and cfg.extensionRate == c.rate, c.name .. ": SBR signalled, core at half rate")
|
||||
t:eq(cfg.ps, c.ps, c.name .. ": PS signalled")
|
||||
end
|
||||
end
|
||||
|
||||
-- crafted configs
|
||||
do
|
||||
local asc = Aac.asc(2, 4, 2)
|
||||
local cfg = Aac.config(asc) :: Aac.Config
|
||||
t:check(cfg and cfg.sampleRate == 44100 and cfg.channels == 2, "asc(2, 44.1 kHz, stereo)")
|
||||
local _, e1 = Aac.config(Aac.asc(1, 4, 2))
|
||||
t:check(e1 and e1.code == Err.UNSUPPORTED, "AAC Main: " .. tostring(e1))
|
||||
local _, e2 = Aac.config(Aac.asc(23, 4, 2))
|
||||
t:check(e2 and e2.code == Err.UNSUPPORTED, "ER AAC LD")
|
||||
local b960 = Aac.asc(2, 3, 1)
|
||||
buffer.writeu8(b960, 1, bit32.bor(buffer.readu8(b960, 1), 4)) -- frameLengthFlag
|
||||
local _, e3 = Aac.config(b960)
|
||||
t:check(e3 and e3.code == Err.UNSUPPORTED, "960-sample frames")
|
||||
-- channel configuration 0 with a PCE: front C (SCE) + L/R (CPE), back L/R (CPE), one LFE = 6 channels
|
||||
local bw = Bits.msbWriter()
|
||||
bw:write(2, 5)
|
||||
bw:write(3, 4)
|
||||
bw:write(0, 4)
|
||||
bw:write(0, 3) -- GASpecificConfig flags
|
||||
bw:write(0, 4) -- PCE: tag
|
||||
bw:write(1, 2)
|
||||
bw:write(3, 4)
|
||||
bw:write(2, 4) -- front
|
||||
bw:write(0, 4) -- side
|
||||
bw:write(1, 4) -- back
|
||||
bw:write(1, 2) -- lfe
|
||||
bw:write(0, 3)
|
||||
bw:write(0, 4)
|
||||
bw:write(0, 3) -- mixdowns
|
||||
bw:write(0, 5)
|
||||
bw:write(16 + 1, 5) -- front: SCE 0, CPE 1
|
||||
bw:write(16 + 2, 5) -- back: CPE 2
|
||||
bw:write(0, 4) -- lfe tag
|
||||
bw:align()
|
||||
bw:write(0, 8) -- comment
|
||||
local pcfg, perr = Aac.config(bw:finish())
|
||||
t:check(pcfg and pcfg.channels == 6 and pcfg.channelConfig == 0, "PCE: 6 channels: " .. tostring(perr))
|
||||
local _, e4 = Aac.decoder("nope" :: any)
|
||||
t:check(e4 and e4.code == Err.ARG, "decoder of a string")
|
||||
end
|
||||
|
||||
-- ADTS headers + raw frames against ffmpeg
|
||||
for _, c in Fx do
|
||||
if c.container ~= "adts" then
|
||||
continue
|
||||
end
|
||||
local data = buffer.fromstring(c.data)
|
||||
local list, h = frames(data)
|
||||
t:check(h and h.objectType == 2 and h.sampleRate == c.rate, c.name .. ": ADTS header")
|
||||
local dec = Aac.decoder({ sfIndex = (h :: Aac.Adts).sfIndex, channels = c.channels }) :: Aac.Decoder
|
||||
local ref = buffer.fromstring(c.ref)
|
||||
local sig, err = 0, 0
|
||||
local o = 0
|
||||
for _, f in list do
|
||||
local n, e = dec:decode(data, f[1], f[2])
|
||||
t:check(n == 1024, c.name .. ": frame: " .. tostring(e))
|
||||
for k = 1, 1024 * c.channels do
|
||||
local r = buffer.readf32(ref, o)
|
||||
sig += r * r
|
||||
err += (dec.pcm[k] - r) ^ 2
|
||||
o += 4
|
||||
end
|
||||
end
|
||||
local snr = 10 * math.log10(sig / math.max(err, 1e-30))
|
||||
t:check(snr > 90, string.format("%s: SNR %.1f dB", c.name, snr))
|
||||
t:note("%s: %d raw frames, SNR %.1f dB vs ffmpeg", c.name, #list, snr)
|
||||
-- reset: decoding again from the start gives the same first frames
|
||||
dec:reset()
|
||||
dec:decode(data, list[1][1], list[1][2])
|
||||
local again = table.clone(dec.pcm)
|
||||
local dec2 = Aac.decoder({ sfIndex = (h :: Aac.Adts).sfIndex, channels = c.channels }) :: Aac.Decoder
|
||||
dec2:decode(data, list[1][1], list[1][2])
|
||||
local same = true
|
||||
for k = 1, 1024 * c.channels do
|
||||
same = same and again[k] == dec2.pcm[k]
|
||||
end
|
||||
t:check(same, c.name .. ": reset clears the overlap")
|
||||
end
|
||||
|
||||
-- the extension hook on HE-AAC (implicit SBR in ADTS)
|
||||
for _, c in He do
|
||||
if string.sub(c.name, -4) == ".aac" then
|
||||
local data = buffer.fromstring(c.data)
|
||||
local list, h = frames(data)
|
||||
local dec = Aac.decoder({ sfIndex = (h :: Aac.Adts).sfIndex, channels = 2 }) :: Aac.Decoder
|
||||
local seen, bits = 0, 0
|
||||
dec.onExtension = function(kind: number, _element: number, _buf: buffer, _pos: number, n: number)
|
||||
if kind == 13 or kind == 14 then
|
||||
seen += 1
|
||||
bits += n
|
||||
end
|
||||
end
|
||||
for _, f in list do
|
||||
dec:decode(data, f[1], f[2])
|
||||
end
|
||||
t:check(seen == #list and dec.sbr, string.format("%s: an SBR payload per frame (%d of %d, %d bits)", c.name, seen, #list, bits))
|
||||
end
|
||||
end
|
||||
|
||||
-- errors
|
||||
do
|
||||
local dec = Aac.decoder(Aac.asc(2, 4, 2)) :: Aac.Decoder
|
||||
local _, e = dec:decode(buffer.fromstring("\x20\x00\x00\x00\x00"))
|
||||
t:check(e and e.code == Err.CORRUPT, "garbage frame: " .. tostring(e))
|
||||
local _, e2 = dec:decode(buffer.create(4), 3, 10)
|
||||
t:check(e2 and e2.code == Err.ARG, "range out of the buffer")
|
||||
t:check(Aac.adts(buffer.fromstring("\255\241"), 0, 2) == nil, "short ADTS header")
|
||||
end
|
||||
|
||||
-- fuzz raw frames (stereo with every tool, mono, 5.1)
|
||||
do
|
||||
local pick = {}
|
||||
for _, c in Fx do
|
||||
if c.container == "adts" then
|
||||
local data = buffer.fromstring(c.data)
|
||||
local list, h = frames(data)
|
||||
local f = list[math.min(#list, 9)]
|
||||
local raw = buffer.create(f[2])
|
||||
buffer.copy(raw, 0, data, f[1], f[2])
|
||||
table.insert(pick, { raw, (h :: Aac.Adts).sfIndex, c.channels })
|
||||
end
|
||||
end
|
||||
for _, p in pick do
|
||||
local dec = Aac.decoder({ sfIndex = p[2], channels = p[3] }) :: Aac.Decoder
|
||||
t:fuzz("raw frame " .. p[3] .. " ch", function(b: buffer)
|
||||
return dec:decode(b)
|
||||
end, { p[1] }, { mutations = 60, garbage = 100 })
|
||||
end
|
||||
end
|
||||
|
||||
t:done()
|
||||
Reference in new issue
Block a user