Files
rosetta/tests/codecs/alac.luau
T
4DBug 51b08575c4 ws-cleanup: codecs/ grouped by domain; containers/Chunks, codecs/audio/Stream, codecs/image/Pixels, codecs/model/Mat4
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.
2026-10-31 12:25:03 -05:00

182 lines
6.4 KiB
Luau

-- codecs/audio/Alac tests: encoder -> decoder round trips over silence (zero runs), full-scale noise (uncompressed frames),
-- tones, steps, 1 - 8 channels, 16 / 20 / 24 bits, partial and tiny frames, custom frame lengths; ffmpeg's ALAC
-- packets (through containers/IsoBmff) decode to the source integers; the magic cookie (plain, behind frma / alac atoms,
-- bad versions / depths); errors; the fuzz no-throw pass on packets. Fixtures: tools/fixtures/m4a.py.
local T = require("@tests/T")
local Alac = require("@formats/codecs/audio/Alac")
local IsoBmff = require("@formats/containers/IsoBmff")
local Bytes = require("@formats/core/Bytes")
local Crc32 = require("@formats/core/Crc32")
local Err = require("@formats/core/Err")
local Fx = require("@tests/gen/m4a_alac")
local t = T.suite("alac")
local rand = T.rng(42)
local function roundTrip(label: string, ch: number, bits: number, frames: number, gen: (number, number) -> number, fl: number?)
local enc = Alac.encoder({ sampleRate = 44100, channels = ch, bitDepth = bits, frameLength = fl }) :: Alac.Encoder
t:check(enc, label .. ": encoder")
local per = enc.config.frameLength
local src = table.create(frames * ch, 0)
for i = 0, frames - 1 do
for c = 1, ch do
src[i * ch + c] = gen(i, c)
end
end
local packets = {}
local bytes = 0
local f = 0
while f < frames do
local n = math.min(per, frames - f)
local p, err = enc:encode(src, f * ch + 1, n)
t:check(p, label .. ": encode: " .. tostring(err))
table.insert(packets, { p, n })
bytes += buffer.len(p :: buffer)
f += n
end
local cfg = Alac.config(Alac.cookie(enc.config)) :: Alac.Config
t:check(cfg and cfg.maxFrameBytes >= 1 and cfg.channels == ch and cfg.bitDepth == bits, label .. ": cookie")
local dec = Alac.decoder(cfg)
local ok = true
local at = 0
for _, pk in packets do
local n, err = dec:decode(pk[1])
if n ~= pk[2] then
ok = false
t:check(false, label .. ": decode: " .. tostring(err))
break
end
for k = 1, n * ch do
if dec.pcm[k] ~= src[at * ch + k] then
ok = false
end
end
at += n
end
t:check(ok, label .. ": round trip")
return bytes / (frames * ch * bits / 8)
end
local peakOf = function(bits: number): number
return 2 ^ (bits - 1)
end
for _, bits in { 16, 20, 24 } do
local pk = peakOf(bits)
for _, ch in { 1, 2, 3, 6, 8 } do
local r1 = roundTrip(string.format("tone %d-bit %d ch", bits, ch), ch, bits, 9000, function(i, c)
return math.floor(math.sin(i * 0.031 * c) * (pk * 0.6) + math.sin(i * 0.0071) * pk * 0.2)
end)
if ch == 2 then
t:note("%d-bit stereo tone: %.0f%% of PCM", bits, r1 * 100)
end
end
roundTrip(bits .. "-bit silence (zero runs)", 2, bits, 10000, function()
return 0
end)
local r = roundTrip(bits .. "-bit full-scale noise (escape frames)", 2, bits, 5000, function()
return rand(2 ^ bits) - pk
end)
t:check(r < 1.01, bits .. "-bit noise stays near PCM size")
roundTrip(bits .. "-bit extremes", 1, bits, 600, function(i)
return if i % 2 == 0 then pk - 1 else -pk
end)
roundTrip(bits .. "-bit sparse clicks (zero runs + values)", 2, bits, 7000, function(i, c)
return if i % 997 == c then pk // 3 else 0
end)
end
for _, n in { 1, 2, 3, 5, 9, 17, 4095 } do
roundTrip("partial frame of " .. n, 2, 16, n, function(i, c)
return (i * 37 + c * 1000) % 3000 - 1500
end)
end
roundTrip("frame length 1024", 2, 16, 5000, function(i)
return math.floor(math.sin(i * 0.05) * 10000)
end, 1024)
roundTrip("identical channels", 2, 16, 5000, function(i)
return math.floor(math.sin(i * 0.02) * 20000)
end)
-- ffmpeg's packets
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 tr = movie.tracks[1]
local cfg = Alac.config(tr.config :: buffer) :: Alac.Config
t:check(cfg and cfg.bitDepth == c.bits and cfg.channels == c.channels, c.name .. ": cookie")
local dec = Alac.decoder(cfg)
local out = buffer.create(c.frames * c.channels * 4)
local o = 0
for i = 1, tr.sampleCount do
local n, err = dec:decode(r.buf, tr.offsets[i], tr.sizes[i])
t:check(n, c.name .. ": packet " .. i .. ": " .. tostring(err))
for k = 1, (n :: number) * c.channels do
buffer.writei32(out, o, dec.pcm[k])
o += 4
end
end
t:eq(Crc32.of(out), c.crc, c.name .. ": ffmpeg's packets decode to the source")
end
end
-- the cookie behind QuickTime atoms, bad cookies
do
local enc = Alac.encoder({ sampleRate = 48000, channels = 2 }) :: Alac.Encoder
local plain = Alac.cookie(enc.config)
local w = Bytes.writer(64)
w:u32be(12)
w:string("frma")
w:string("alac")
w:u32be(36)
w:string("alac")
w:u32be(0)
w:bytes(plain)
local cfg = Alac.config(w:finish())
t:check(cfg and cfg.sampleRate == 48000 and cfg.frameLength == 4096, "cookie behind frma / alac atoms")
local bad = buffer.fromstring(buffer.tostring(plain))
buffer.writeu8(bad, 5, 12)
local _, e = Alac.config(bad)
t:check(e and e.code == Err.UNSUPPORTED, "12-bit cookie: " .. tostring(e))
local _, e2 = Alac.config(buffer.create(10))
t:check(e2 and e2.code == Err.TRUNCATED, "short cookie")
local _, e3 = Alac.encoder({ sampleRate = 44100, channels = 9 })
t:check(e3 and e3.code == Err.ARG, "9 channels")
local _, e4 = enc:encode({ 1, 2 }, 1, 5)
t:check(e4 and e4.code == Err.ARG, "too few samples")
local dec = Alac.decoder(cfg :: Alac.Config)
local _, e5 = dec:decode(buffer.fromstring("\1\255\255\255"))
t:check(e5 and e5.code == Err.CORRUPT, "bad packet: " .. tostring(e5))
end
-- fuzz the packet decoder
do
local enc = Alac.encoder({ sampleRate = 44100, channels = 2, bitDepth = 16 }) :: Alac.Encoder
local src = table.create(2 * 3000, 0)
for i = 1, #src do
src[i] = math.floor(math.sin(i * 0.01) * 8000) + rand(64)
end
local samples = {}
table.insert(samples, (enc:encode(src, 1, 3000)))
for i = 1, #src do
src[i] = if i % 300 < 3 then 5000 else 0
end
table.insert(samples, (enc:encode(src, 1, 3000)))
local dec = Alac.decoder(enc.config)
local enc24 = Alac.encoder({ sampleRate = 44100, channels = 1, bitDepth = 24 }) :: Alac.Encoder
local dec24 = Alac.decoder(enc24.config)
local s24 = table.create(2000, 0)
for i = 1, 2000 do
s24[i] = rand(2 ^ 24) - 2 ^ 23
end
local p24 = enc24:encode(s24, 1, 2000) :: buffer
t:fuzz("decode packet", function(b: buffer)
return dec:decode(b)
end, samples, { mutations = 40 })
t:fuzz("decode 24-bit escape packet", function(b: buffer)
return dec24:decode(b)
end, { p24 }, { mutations = 20, garbage = 50 })
end
t:done()