C2, the hierarchy (tools/move.py, every reference rewritten mechanically): - codecs/compress: Deflate Lzma Zstd Brotli Bzip2 Lzw; codecs/entropy: Huffman (from core) RangeCoder; codecs/image: Jpeg Dct Vp8 Vp8L Yuv Quantize Raster Blend Pixels; codecs/audio: Mdct Aac Alac Pcm Resample VorbisComment Tracker SoundFont Stream; codecs/video: H264 Mpeg1 Video; codecs/text: Json Xml Text Url; codecs/font: TrueType Cff; codecs/model: MeshFlatten Mat4. containers/ stays flat; core/ keeps Bytes Bits Err Crc32 Adler32 Base64 Md5 Input Sniff. - LAYERS lint: codecs must sit in a domain folder; gen_registry lists codecs by domain (docs/FORMATS.md). - Checked: build.py both targets, rojo build of build/roblox, the full suite on both runtimes. C3, generic code out of format folders: - containers/Chunks (new): the chunked-file walker (RIFF / RIFX / IFF / PNG / CAF layouts: endianness, size first, trailer, padding, lenient missing pad, open-ended ids, size hooks). containers/Riff walks with it (RF64 ds64 through the resize hook); image/png, audio/aiff and audio/caf list their chunk handlers over it. - codecs/audio/Stream (new): Stream.packets, the packet-decoder Decoder (read / seek with pre-roll / position, priming and end trimming) that aac, caf and m4a each carried (~70 lines each). - codecs/image/Pixels: image/bmp/Pixels promoted (+ rgb555); image/ico's DIB decoder uses its row loops instead of its own (the AND mask / alpha rule stay ICO's). - codecs/model/Mat4 (new): matrices / quaternions / TRS shared by FBX (Xform keeps only the FBX transform model), Collada (Math keeps only <lookat> / <skew>) and USD (its matrix helpers and general 4x4 inverse); Mat4.mul is Types.Scene.mul. The Collada normal matrix is now the inverse transpose (it was the inverse; same result for the fixtures' bind shapes). - README rewritten (install for Rojo and LÖVE / LuaJIT, quick start per category, F.* and F.ops, host notes: FORMATS_NO_JIT_TUNE, paths, layout); docs/STYLE.md: the codec domains, "what belongs in a format folder" with the shared building blocks and the DUP rule, conventions (WAVE order, lower-case tags, loops, the api hooks decodeImage / encodeImage / openAudio / plugins / resolve, the codecs/video/Video registry), Err.from, the type baseline. New tests: tests/containers/chunks, tests/codecs/mat4. Type baseline 93. Co-Authored-By: Claude Opus 5.5 <[email protected]>
183 lines
6.6 KiB
Luau
183 lines
6.6 KiB
Luau
-- codecs/audio/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/audio/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()
|