-- codecs/audio/Ac3 tests: the codec API under audio/ac3 and the container hook: header fields of every fixture layout, -- the CRC (a syncframe checks to 0), one-frame decodes against the reference (frame by frame, as a container feeds -- them), the block offsets, reset, argument / data errors, and a fuzz pass over single frames (CRC off). -- Fixtures: tools/fixtures/ac3.py. local T = require("@tests/T") local Ac3 = require("@rosetta/codecs/audio/Ac3") local Err = require("@rosetta/core/Err") local Fx = require("@tests/gen/ac3_cases") local t = T.suite("codecs/ac3") local ACMOD_CH = { [0] = 2, 1, 2, 3, 3, 4, 4, 5 } local frames = {} for _, c in Fx do local b = buffer.fromstring(c.data) local h = Ac3.header(b, 0, buffer.len(b)) t:check(h, c.name .. ": header") if not h then continue end t:check(h.sampleRate == c.rate and h.channels == c.channels, c.name .. ": header rate / channels") t:eq(ACMOD_CH[h.acmod] + (if h.lfeon then 1 else 0), c.channels, c.name .. ": acmod + lfe") t:eq(Ac3.crc(b, 2, h.frameSize - 2), 0, c.name .. ": crc2 checks") -- frame by frame, fresh decoder: the second frame against the reference local d = Ac3.decoder() local n1 = d:decode(b, 0, h.frameSize) local h2 = Ac3.header(b, h.frameSize, buffer.len(b)) :: Ac3.Header local n2, err = d:decode(b, h.frameSize, h2.frameSize) t:check(n1 == 1536 and n2 == 1536, c.name .. ": two frames: " .. tostring(err)) local rb = buffer.fromstring(c.ref) local maxd = 0 for i = 1, 1536 * c.channels do maxd = math.max(maxd, math.abs(d.pcm[i] - buffer.readf32(rb, (1536 * c.channels + i - 1) * 4))) end t:check(maxd < 1e-4, string.format("%s: frame 2 (max diff %g)", c.name, maxd)) t:check(#d.blocks == 6 and d.blocks[1] > 40 and d.blocks[6] < h2.frameSize * 8, c.name .. ": block offsets") d:reset() table.insert(frames, buffer.readstring(b, 0, h.frameSize)) end -- errors do local d = Ac3.decoder() local _, e = d:decode(buffer.create(10), 20) t:check(e and e.code == Err.ARG, "offset past the buffer: " .. tostring(e)) _, e = d:decode(buffer.fromstring(string.rep("\0", 64)), 0) t:check(e and e.code == Err.CORRUPT, "no sync: " .. tostring(e)) _, e = d:decode(buffer.fromstring(string.sub(frames[1], 1, 100)), 0) t:check(e and e.code == Err.TRUNCATED, "short frame: " .. tostring(e)) t:check(Ac3.header(buffer.fromstring("\11\119\0\0\255\0\0\0"), 0, 8) == nil, "frmsizecod 63 / fscod 3: no header") end t:fuzz("decode one frame (crc off)", function(b: buffer) return Ac3.decoder({ crc = false }):decode(b, 0) end, frames, { mutations = 30, keepHeader = 6 }) t:done()