-- audio/ac3 tests: AC-3 streams from ffmpeg (every acmod with and without LFE, coupling incl. cplbegf 0, rematrixing, -- 32 / 44.1 / 48 kHz) and aften (block switching, 1+1 dual mono, dynamic range profiles, the alternate BSI) against -- ffmpeg's float decode, or liba52's for block-switched streams (SNR, max difference; dithflags cleared in the -- fixture step so both sides decode the same numbers); dynamic range on; the encoders' dithered originals; AC-3 tracks -- of MKV / MP4 / MPEG-TS / AVI through the api's audio hook; exact seeking; resync and truncation; error codes (E-AC-3, -- CRC); the fuzz no-throw pass. Fixtures: tools/fixtures/ac3.py. local T = require("@tests/T") local F = require("@rosetta") local Ac3F = require("@rosetta/audio/ac3") local Ac3 = require("@rosetta/codecs/audio/Ac3") local Bytes = require("@rosetta/core/Bytes") local Err = require("@rosetta/core/Err") local Fx = require("@tests/gen/ac3_cases") local Tracks = require("@tests/gen/ac3_tracks") local t = T.suite("ac3") local function reader(s: string): Bytes.Reader return Bytes.reader(buffer.fromstring(s)) end -- SNR (dB) and the largest difference of interleaved f32 `got` against the f32le reference `ref` local function compare(got: buffer, ref: string, count: number): (number, number) local rb = buffer.fromstring(ref) local n = math.min(count, #ref // 4, buffer.len(got) // 4) local sig, err, maxd = 0, 0, 0 for i = 0, n - 1 do local r = buffer.readf32(rb, i * 4) local d = buffer.readf32(got, i * 4) - r sig += r * r err += d * d maxd = math.max(maxd, math.abs(d)) end return if err == 0 then math.huge else 10 * math.log10(sig / err), maxd end t:eq(Ac3F.name, "ac3", "name") t:check(Ac3F.caps.decode and not Ac3F.caps.encode and Ac3F.encode == nil, "caps: decode only") local byName = {} for _, c in Fx do byName[c.name] = c local r = reader(c.data) t:check(Ac3F.sniff(r) >= 0.9, c.name .. ": sniff") local a, err = Ac3F.decode(r) t:check(a, c.name .. ": " .. tostring(err)) if not a then continue end t:check(a.channels == c.channels and a.sampleRate == c.rate, c.name .. ": format") t:eq(a.frames, c.frames, c.name .. ": frames") -- liba52's 6-channel output is 16-bit local min = if c.refTool == "a52dec" and c.channels > 2 then 80 else 95 local s, maxd = compare(a.samples, c.ref, a.frames * a.channels) t:check(s > min, string.format("%s: SNR %.1f dB vs %s", c.name, s, c.refTool)) t:note("%s: %d ch %d Hz, %d frames: SNR %.1f dB, max diff %.1e vs %s", c.name, a.channels, a.sampleRate, a.frames, s, maxd, c.refTool) if c.refDrc then local d = Ac3F.decode(r, { drc = 1 }) :: any local sd = compare(d.samples, c.refDrc, d.frames * d.channels) t:check(sd > min, string.format("%s: dynamic range on: SNR %.1f dB vs %s", c.name, sd, c.refTool)) local off = compare(d.samples, c.ref, d.frames * d.channels) t:check(off < 40, c.name .. ": the dynamic range gains change the output") end if c.dithered then -- the encoder's own stream (dithflag set): our dither against the reference's undithered decode local dd = Ac3F.decode(reader(c.dithered)) :: any local sd = compare(dd.samples, c.ref, dd.frames * dd.channels) t:check(dd and sd > 10, string.format("%s: dithered original decodes (SNR %.1f dB, the dither noise)", c.name, sd)) end end -- AC-3 tracks of video containers, through the api's audio hook (containers/AudioWrap -> audio/ac3) for _, c in Tracks do local v, err = F.video.open(buffer.fromstring(c.data)) t:check(v, c.name .. ": open: " .. tostring(err)) if not v then continue end t:check(v.audio, c.name .. ": audio track opens: " .. tostring(v.audioError)) if v.audio then local a = assert(F.ops.audio.toAudio(v.audio)) t:check(a.channels == c.channels and a.sampleRate == c.rate, c.name .. ": audio format") local s = compare(a.samples, c.ref, a.frames * a.channels) t:check(s > 95, string.format("%s: AC-3 track SNR %.1f dB vs ffmpeg", c.name, s)) t:note("%s: AC-3 track, %d ch, %d frames, SNR %.1f dB", c.name, a.channels, a.frames, s) end end -- streaming + exact seeking (one frame of pre-roll for the overlap) do local c = byName["5.1_448"] local whole = Ac3F.decode(reader(c.data)) :: any local dec = Ac3F.open(reader(c.data)) :: any local _ = dec:read(3000) for _, at in { 0.1, 0.0, 0.0517 } do dec:seek(at) local b, n = dec:read(700) local f0 = math.floor(at * c.rate + 0.5) local maxd = 0 for i = 0, n * 6 - 1 do maxd = math.max(maxd, math.abs(buffer.readf32(b, i * 4) - buffer.readf32(whole.samples, (f0 * 6 + i) * 4))) end t:check(n == 700 and maxd < 1e-6, string.format("seek %.4f s (max diff %g)", at, maxd)) end end -- resync over garbage, a truncated last frame, the raw codec API do local c = byName.stereo_44k local a = Ac3F.decode(reader("\0\11\119garbage" .. c.data)) :: any t:check(a and a.frames == c.frames, "garbage in front: same frames") local h = Ac3.header(buffer.fromstring(c.data), 0, #c.data) :: Ac3.Header t:check(h.sampleRate == 44100 and h.channels == 2 and h.acmod == 2 and h.bitrate == 192000, "header fields") local cut = Ac3F.decode(reader(string.sub(c.data, 1, #c.data - 10))) :: any t:check(cut and cut.frames == c.frames - 1536, "truncated last frame dropped") local d = Ac3.decoder() local n = d:decode(buffer.fromstring(c.data), 0, h.frameSize) t:check(n == 1536 and d.channels == 2 and #d.pcm == 1536 * 2, "decoder: one syncframe") end -- errors do local c = byName.stereo_44k.data t:eq(Ac3F.sniff(reader("RIFF....WAVE")), 0, "sniff a WAV") local _, e = Ac3F.decode(reader(string.rep("\0", 100))) t:check(e and e.code == Err.CORRUPT, "no frames: " .. tostring(e)) -- an E-AC-3 header: bsid 16, frmsiz 255 (512 bytes), then the same again local eac3 = "\11\119\0\255\128\128" .. string.rep("\0", 506) _, e = Ac3F.decode(reader(eac3 .. eac3)) t:check(e and e.code == Err.UNSUPPORTED, "E-AC-3: " .. tostring(e)) local _, fe = F.audio.load(buffer.fromstring(eac3 .. eac3)) t:check(fe and fe.code == Err.UNSUPPORTED, "E-AC-3 through the api: " .. tostring(fe)) -- a flipped bit in the middle of the first frame: the CRC catches it local bad = string.sub(c, 1, 300) .. string.char(bit32.bxor(string.byte(c, 301), 4)) .. string.sub(c, 302) _, e = Ac3F.decode(reader(bad)) t:check(e and e.code == Err.CHECKSUM, "CRC: " .. tostring(e)) _, e = Ac3F.decode(reader(c), { maxFrames = 2 }) t:check(e and e.code == Err.LIMIT, "maxFrames: " .. tostring(e)) end -- fuzz (without the CRC check, so mutations reach the decoder) do local samples = {} for _, c in Fx do table.insert(samples, string.sub(c.data, 1, 4000)) end t:fuzz("decode", function(b: buffer) return Ac3F.decode(Bytes.reader(b), { crc = false }) end, samples, { mutations = 25 }) t:fuzz("decode (crc)", function(b: buffer) return Ac3F.decode(Bytes.reader(b)) end, samples, { mutations = 4, garbage = 20 }) end t:done()