-- audio/caf tests: ALAC CAF from ffmpeg bit-exact; AAC CAF (ffmpeg's ADTS frames wrapped by the fixture) against -- ffmpeg's float decode; our CAF writer (PCM 16 / 24-bit, ALAC; re-encoded here and compared with the bytes ffmpeg -- decoded bit-exactly); PCM layouts (little / big endian, float) through our writer + a hand-built header; info tags; -- seek; errors; the fuzz no-throw pass. Fixtures: tools/fixtures/m4a.py. local T = require("@tests/T") local Caf = require("@rosetta/audio/caf") local Bytes = require("@rosetta/core/Bytes") local Crc32 = require("@rosetta/core/Crc32") local Err = require("@rosetta/core/Err") local Signal = require("@tests/audio/_signal") local Cases = require("@tests/audio/_encode_cases") local Alac = require("@tests/gen/m4a_alac") local Aac = require("@tests/gen/m4a_aac") local Ours = require("@tests/gen/m4a_ours") local t = T.suite("caf") local function reader(s: string | buffer): Bytes.Reader return Bytes.reader(if type(s) == "string" then buffer.fromstring(s) else s) end local function intCrc(a: any, bits: number): number local n = a.frames * a.channels local b = buffer.create(n * 4) local scale = 2 ^ (bits - 1) for i = 0, n - 1 do buffer.writei32(b, i * 4, math.floor(buffer.readf32(a.samples, i * 4) * scale + 0.5)) end return Crc32.of(b) end t:eq(Caf.name, "caf", "name") t:check(Caf.caps.decode and Caf.caps.encode, "caps") for _, c in Alac do if c.container == "caf" then local r = reader(c.data) t:eq(Caf.sniff(r), 1, c.name .. ": sniff") local a, err = Caf.decode(r) t:check(a, c.name .. ": " .. tostring(err)) t:check(a.channels == c.channels and a.sampleRate == c.rate and a.frames == c.frames, c.name .. ": format / length (pakt)") t:eq(intCrc(a, c.bits), c.crc, c.name .. ": bit-exact") end end for _, c in Aac do if c.container == "caf" then local a, err = Caf.decode(reader(c.data)) t:check(a, c.name .. ": " .. tostring(err)) t:eq(a.frames, c.frames, c.name .. ": frames") local rb = buffer.fromstring(c.ref) local sig, e = 0, 0 for i = 0, a.frames * a.channels - 1 do local r = buffer.readf32(rb, i * 4) sig += r * r e += (buffer.readf32(a.samples, i * 4) - r) ^ 2 end local snr = if e == 0 then math.huge else 10 * math.log10(sig / e) t:check(snr > 90, string.format("%s: SNR %.1f dB", c.name, snr)) t:note("%s: SNR %.1f dB vs ffmpeg", c.name, snr) end end -- our writer: the bytes ffmpeg validated do local byName = {} for _, o in Ours do byName[o.name] = o end for _, c in Cases do if c.container ~= "caf" then continue end local o = byName[c.name] t:check(o and o.ffmpeg, c.name .. ": ffmpeg decoded it bit-exactly") local a = Signal.audio(c.rate, c.frames, c.channels, c.bits, c.seed) local file = Caf.encode(a, { codec = c.codec, bitDepth = c.bits }) :: buffer t:eq(string.format("%08x", Crc32.of(file)), string.format("%08x", o.crc), c.name .. ": the validated bytes") local back = Caf.decode(reader(file)) :: any t:check(back and back.frames == c.frames, c.name .. ": our length") local ints = Signal.ints(c.frames, c.channels, c.bits, c.seed) local ok = true local scale = 2 ^ (c.bits - 1) for i = 1, #ints do ok = ok and math.floor(buffer.readf32(back.samples, (i - 1) * 4) * scale + 0.5) == ints[i] end t:check(ok, c.name .. ": round trip") end end -- float PCM, tags, a decoder as the source, a hand-built little-endian header do local a = Signal.audio(32000, 3000, 3, 16, 12) a.tags = { title = "Caf Title", artist = "A", date = "1999", comment = "c", custom = "x" } local file = Caf.encode(a, { codec = "pcm", bitDepth = 32 }) :: buffer local back = Caf.decode(reader(file)) :: any t:check(back.frames == 3000 and buffer.tostring(back.samples) == buffer.tostring(a.samples), "float PCM round trip") for k, v in a.tags do t:eq(back.tags[k], v, "tag " .. k) end local dec = Caf.open(reader(file)) :: any local again = Caf.encode(dec, { codec = "alac" }) :: buffer local b2 = Caf.decode(reader(again)) :: any t:check(b2.frames == 3000 and buffer.tostring(b2.samples) == buffer.tostring(a.samples), "ALAC from a streaming decoder") -- little-endian 24-bit integer PCM, data chunk of unknown size (-1) local w = Bytes.writer(64) w:string("caff") w:u16be(1) w:u16be(0) w:string("desc") w:u64be(32) w:f64be(8000) w:string("lpcm") w:u32be(2) w:u32be(3) w:u32be(1) w:u32be(1) w:u32be(24) w:string("data") w:u32be(0xFFFFFFFF) w:u32be(0xFFFFFFFF) w:u32be(0) for _, v in { 0, 4194304, -4194304, 8388607, -8388608 } do w:u24le(v % 16777216) end local le = Caf.decode(Bytes.reader(w:finish())) :: any t:check(le and le.frames == 5, "24-bit LE, data size -1") t:check(le and math.abs(buffer.readf32(le.samples, 4) - 0.5) < 1e-7 and math.abs(buffer.readf32(le.samples, 16) + 1) < 1e-7, "24-bit LE values") end -- seek do local c = Alac[#Alac] local whole = Caf.decode(reader(c.data)) :: any local dec = Caf.open(reader(c.data)) :: any for _, at in { 0.1, 0.0, 0.15 } do dec:seek(at) local b, n = dec:read(400) local f0 = math.floor(at * whole.sampleRate + 0.5) local same = n == 400 for i = 0, n * whole.channels - 1 do same = same and buffer.readf32(b, i * 4) == buffer.readf32(whole.samples, (f0 * whole.channels + i) * 4) end t:check(same, string.format("seek %.2f s", at)) end end -- errors do t:eq(Caf.sniff(reader("caff\0\2\0\0")), 0, "sniff: wrong version") local _, e = Caf.decode(reader("caff\0\1\0\0")) t:check(e and e.code == Err.CORRUPT, "no desc: " .. tostring(e)) local _, e2 = Caf.encode(Signal.audio(8000, 10, 1, 16, 1), { codec = "mp3" } :: any) t:check(e2 and e2.code == Err.ARG, "bad codec: " .. tostring(e2)) end -- fuzz do local samples = {} for _, c in Alac do if c.container == "caf" then table.insert(samples, string.sub(c.data, 1, 9000)) end end for _, c in Aac do if c.container == "caf" then table.insert(samples, string.sub(c.data, 1, 6000)) end end local pcm = Caf.encode(Signal.audio(8000, 300, 2, 16, 3), { codec = "pcm" }) :: buffer table.insert(samples, buffer.tostring(pcm)) t:fuzz("decode", function(b: buffer) return Caf.decode(Bytes.reader(b)) end, samples, { mutations = 30 }) end -- ------------------------------------------------------------------------------------------------ IMA4 do local H = require("@tests/audio/_adpcm") local Ima4 = require("@tests/gen/caf_ima4") for _, c in Ima4 do H.check(t, Caf, c) local a = Caf.decode(Bytes.reader(buffer.fromstring(c.data))) :: any t:eq(a.meta.source.codec, "ima4", c.name .. ": codec") end t:note("%d IMA4 files bit-exact with ffmpeg", #Ima4) end t:done()