diff --git a/tests/codecs/ac3.luau b/tests/codecs/ac3.luau new file mode 100644 index 0000000..dbac64a --- /dev/null +++ b/tests/codecs/ac3.luau @@ -0,0 +1,57 @@ +-- 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("@formats/codecs/audio/Ac3") +local Err = require("@formats/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() diff --git a/tests/codecs/musepack.luau b/tests/codecs/musepack.luau new file mode 100644 index 0000000..1deb78b --- /dev/null +++ b/tests/codecs/musepack.luau @@ -0,0 +1,58 @@ +-- codecs/audio/Musepack tests: the codec API under audio/mpc: magic, the stream fields of SV7 / SV8 fixtures (version, +-- rate, channels, band count, packet layout, gapless length), packets decoded by hand against libmpcdec (the +-- synthesis delay at the front), out-of-order decodes, and a fuzz pass over open + every packet. Fixtures: +-- tools/fixtures/mpc.py. +local T = require("@tests/T") +local Musepack = require("@formats/codecs/audio/Musepack") +local Fx = require("@tests/gen/mpc_cases") + +local t = T.suite("codecs/musepack") + +for _, c in Fx do + local b = buffer.fromstring(c.data) + t:eq(Musepack.magic(b, 0, buffer.len(b)), c.version, c.name .. ": magic") + local s, err = Musepack.open(b, 0, buffer.len(b)) + t:check(s, c.name .. ": open: " .. tostring(err)) + if not s then + continue + end + t:check(s.version == c.version and s.sampleRate == c.rate and s.channels == c.channels, c.name .. ": fields") + t:eq(s.frames, c.frames, c.name .. ": gapless length") + t:check(s.maxBand >= 1 and s.maxBand <= 31 and s.count >= 1 and s.skip >= 481, c.name .. ": bands / packets") + t:eq(s.stop - s.skip, c.frames, c.name .. ": stop - skip = length") + -- the first packet by hand: output frame k is raw frame skip + k + local n = s.decode(1) + t:check(n and n > 0 and n % 1152 == 0 and n <= s.blockFrames * 1152, c.name .. ": first packet length") + local pcm = s.pcm() + local rb = buffer.fromstring(c.ref) + local ch = c.channels + local maxd = 0 + for k = 0, math.min(500, (n :: number) - s.skip - 1) do + for j = 1, ch do + maxd = math.max(maxd, math.abs(pcm[(s.skip + k) * ch + j] - buffer.readf32(rb, (k * ch + j - 1) * 4))) + end + end + t:check(maxd < 1e-5, string.format("%s: first packet matches libmpcdec (max diff %g)", c.name, maxd)) + if s.count >= 3 then + s.reset() + t:check(s.decode(3) ~= nil and s.decode(1) ~= nil, c.name .. ": decodes out of order after reset") + end +end + +local samples = {} +for _, c in Fx do + table.insert(samples, string.sub(c.data, 1, 2500)) +end +t:fuzz("open + every packet", function(b: buffer) + local s = Musepack.open(b, 0, buffer.len(b)) + if s then + for i = 1, math.min(s.count, 40) do + if not s.decode(i) then + break + end + end + end + return s +end, samples, { mutations = 25, keepHeader = 4 }) + +t:done()