-- codecs/audio/Alac tests: encoder -> decoder round trips, ffmpeg's ALAC packets, the magic cookie, errors. -- Fixtures: tools/fixtures/m4a.py. local T = require("@tests/T") local Alac = require("@rosetta/codecs/audio/Alac") local IsoBmff = require("@rosetta/containers/IsoBmff") local Bytes = require("@rosetta/core/Bytes") local Crc32 = require("@rosetta/core/Crc32") local Err = require("@rosetta/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) 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 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 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()