Files
rosetta/tests/codecs/adpcm.luau
T

271 lines
9.9 KiB
Luau

-- codecs/adpcm tests: every decoder kernel of codecs/audio/Adpcm against a reference decoder, bit-exact as s16
-- (tools/fixtures/adpcm.py: ADX from ffmpeg and PS-ADPCM / DSP from our Python encoders decoded by vgmstream, Apple
-- ima4 from ffmpeg, IMA nibble streams decoded by ffmpeg); ADX coefficients; the encoders (IMA nibbles, IMA WAV
-- blocks, MS blocks) decoded back with a signal-to-noise floor; the Decoder layouts (seeking matches a straight
-- read); Flash ADPCM by hand; a fuzz pass of random bytes through every kernel (ranges valid, contents random) and
-- the layouts.
local T = require("@tests/T")
local Adpcm = require("@rosetta/codecs/audio/Adpcm")
local Decoder = require("@rosetta/codecs/audio/Adpcm/Decoder")
local Cases = require("@tests/gen/adpcm_codec")
local Sig = require("@tests/gen/adpcm_sig")
local t = T.suite("adpcm codec")
-- s16le reference vs interleaved f32: the count of differing samples and the first one
local function compare(out: buffer, ref: string, count: number): (number, number)
local r = buffer.fromstring(ref)
local bad, first = 0, -1
for i = 0, count - 1 do
if buffer.readf32(out, i * 4) * 32768 ~= buffer.readi16(r, i * 2) then
bad += 1
if first < 0 then
first = i
end
end
end
return bad, first
end
-- ------------------------------------------------------------------------------------------------ kernels
for _, c in Cases do
local src = buffer.fromstring(c.data)
local ch, frames = c.ch, c.frames
local out = buffer.create((frames + 64) * ch * 4)
local st = buffer.create(8 * ch)
if c.kind == "adx" then
local c1, c2 = Adpcm.adxCoefs(c.cutoff, c.rate)
local nf = math.ceil(frames / 32)
for k = 0, ch - 1 do
Adpcm.adx(src, k * 18, 18 * ch, nf, c1, c2, c.v3, st, k * 8, out, k, ch)
end
elseif c.kind == "psx" then
Adpcm.psx(src, 0, 16, frames // 28, st, 0, out, 0, 1)
elseif c.kind == "dsp" then
local coefs = buffer.create(64)
for i, v in c.coefs do
buffer.writei32(coefs, (i - 1) * 4, v)
end
-- in pieces: the state carries over
Adpcm.dsp(src, 0, 14 * 9, coefs, 0, st, 0, out, 0, 1)
Adpcm.dsp(src, 8 * 9, frames - 14 * 9, coefs, 0, st, 0, out, 14 * 9, 1)
elseif c.kind == "ima_qt" then
for p = 0, frames // 64 - 1 do
for k = 0, ch - 1 do
Adpcm.imaQt(src, (p * ch + k) * 34, st, k * 8, out, p * 64 * ch + k, ch)
end
end
elseif c.kind == "ima" then
for k = 0, ch - 1 do
Adpcm.ima(src, 0, k, ch, frames, true, st, k * 8, out, k, ch)
end
end
local bad, first = compare(out, c.ref, frames * ch)
t:check(bad == 0, string.format("%s: %d of %d samples differ (first at %d)", c.name, bad, frames * ch, first))
end
t:note("%d streams bit-exact with the reference decoders", #Cases)
-- ADX coefficients: 500 Hz at 44.1 kHz / 22.05 kHz (CRI's single-precision derivation, as vgmstream)
do
local a1, a2 = Adpcm.adxCoefs(500, 44100)
t:check(a1 == 7334 and a2 == -3283, "adx coefs 44100: " .. a1 .. " " .. a2)
local z1, z2 = Adpcm.adxCoefs(0, 0)
t:check(z1 == 0 and z2 == 0, "adx coefs of rate 0")
end
-- ModPlug ADPCM: deltas from the table, wrapping as a signed byte
do
local src = buffer.create(18)
for k = 0, 15 do
buffer.writei8(src, k, if k == 1 then 100 elseif k == 2 then -3 else 0)
end
buffer.writeu8(src, 16, 0x11) -- +100, +100 (wraps to -56)
buffer.writeu8(src, 17, 0x02) -- -3, +0
local out = buffer.create(16)
Adpcm.modplug(src, 0, 4, out, 0)
local want = { 100, -56, -59, -59 }
for i, v in want do
t:eq(buffer.readf32(out, (i - 1) * 4) * 128, v, "modplug sample " .. i)
end
end
-- Flash ADPCM (bit-exact with ffmpeg's adpcm_swf in tests/video/flv.luau): a hand-made 2-bit packet of 4 bytes:
-- code size 0 (2-bit codes), first sample 100, step index 0, then the codes 01 11 00 00
do
local bits = "00" .. "0000000001100100" .. "000000" .. "01110000"
local src = buffer.create(4)
for i = 0, 3 do
buffer.writeu8(src, i, tonumber(string.sub(bits, i * 8 + 1, i * 8 + 8), 2) :: number)
end
t:eq(Adpcm.swfFrames(src, 0, 4, 1), 5, "swf: frames in a 4-byte packet")
local out = buffer.create(20)
t:eq(Adpcm.swf(src, 0, 4, 1, out, 0), 5, "swf: frames decoded")
-- 01: +7 + 7/2 = 110 (index 0 + 2: step 9); 11: -(9 + 4) = 97 (index 4: step 11); 00: +5 = 102 (index 3: step
-- 10); 00: +5 = 107
local want = { 100, 110, 97, 102, 107 }
for i, v in want do
t:eq(buffer.readf32(out, (i - 1) * 4) * 32768, v, "swf sample " .. i)
end
end
-- ------------------------------------------------------------------------------------------------ encoders
local function snr(x: buffer, y: buffer, count: number): number
local sig, err = 0, 0
for i = 0, count - 1 do
local a = buffer.readf32(x, i * 4)
local e = a - buffer.readf32(y, i * 4)
sig += a * a
err += e * e
end
return if err == 0 then 200 else 10 * math.log10(sig / err)
end
do
local x = buffer.fromstring(Sig.data)
local ch, n = Sig.channels, Sig.frames
-- IMA nibbles, both nibble orders
for _, hi in { true, false } do
local raw = buffer.create((n * ch + 1) // 2)
local st = buffer.create(8 * ch)
for c = 0, ch - 1 do
Adpcm.imaEncode(x, c, ch, n, st, c * 8, raw, 0, c, ch, hi)
end
local back = buffer.create(n * ch * 4)
local ds = buffer.create(8 * ch)
for c = 0, ch - 1 do
Adpcm.ima(raw, 0, c, ch, n, hi, ds, c * 8, back, c, ch)
end
local s = snr(x, back, n * ch)
t:check(s > 20, string.format("ima encode (hi %s): %.1f dB", tostring(hi), s))
t:check(buffer.readi32(st, 0) == buffer.readi32(ds, 0), "ima encoder and decoder end in the same state")
end
-- IMA WAV and MS blocks
for _, kind in { "ima_wav", "ms" } do
local ba = 512
local spb = Decoder.blockFrames(kind, ba, ch) :: number
local blocks = (n + spb - 1) // spb
local raw = buffer.create(blocks * ba)
local st = buffer.create(8 * ch)
for b = 0, blocks - 1 do
if kind == "ms" then
Adpcm.msEncode(x, b * spb, n - b * spb, ch, spb, raw, b * ba)
else
Adpcm.imaWavEncode(x, b * spb, n - b * spb, ch, spb, st, raw, b * ba)
end
end
local back = buffer.create(blocks * spb * ch * 4)
local coefs = buffer.create(56)
for k = 0, 6 do
buffer.writei32(coefs, k * 8, Adpcm.MS_COEF1[k + 1])
buffer.writei32(coefs, k * 8 + 4, Adpcm.MS_COEF2[k + 1])
end
local scratch = buffer.create(20 * ch)
for b = 0, blocks - 1 do
local got
if kind == "ms" then
got = Adpcm.msBlock(raw, b * ba, ba, ch, spb, back, b * spb * ch, coefs, 7, scratch)
else
got = Adpcm.imaWav(raw, b * ba, ba, ch, spb, back, b * spb * ch)
end
t:eq(got, spb, kind .. ": block frames")
end
local s = snr(x, back, n * ch)
t:check(s > (if kind == "ms" then 24 else 20), string.format("%s encode: %.1f dB", kind, s))
end
end
-- ------------------------------------------------------------------------------------------------ Decoder layouts
local function readAll(dec: any, step: number): buffer
local out = buffer.create(dec.frames * dec.channels * 4)
local got = 0
while true do
local b, n = dec:read(step)
if n == 0 then
break
end
buffer.copy(out, got * dec.channels * 4, b, 0, n * dec.channels * 4)
got += n
end
t:eq(got, dec.frames, "layout: frames read")
return out
end
do
-- psx in rows of 64 bytes per channel (stereo), seeking back and forth lands on the same samples
local rng = T.rng(5)
local ch, il, rows = 2, 64, 40
local data = buffer.create(il * ch * rows)
for i = 0, buffer.len(data) - 1 do
buffer.writeu8(data, i, if i % 16 == 0 then rng(5) * 16 + rng(13) elseif i % 16 == 1 then 0 else rng(256))
end
for _, kind in { "psx", "dsp" } do
local coefs = buffer.create(64 * ch)
for k = 0, 16 * ch - 1 do
buffer.writei32(coefs, k * 4, (k % 2 == 0) and 1800 or -700)
end
local il2 = if kind == "dsp" then 64 else il
local dec = Decoder.interleaved({ sampleRate = 8000, channels = ch, tags = {} }, {
kind = kind :: any, buf = data, offset = 0, size = buffer.len(data), interleave = il2, coefs = coefs })
t:check(dec ~= nil, kind .. ": interleaved decoder")
local all = readAll(dec, 1000)
for _, f in { 500, 3, dec.frames - 7, 1234, 0 } do
dec:seek(f / 8000)
local b, n = dec:read(5)
local same = n == math.min(5, dec.frames - f)
for k = 0, n * ch - 1 do
same = same and buffer.readf32(b, k * 4) == buffer.readf32(all, (f * ch + k) * 4)
end
t:check(same, kind .. ": seek to " .. f .. " is exact")
end
end
-- a cut file: the whole frames of every channel
local dec = Decoder.interleaved({ sampleRate = 8000, channels = 2, tags = {} }, {
kind = "psx", buf = data, offset = 0, size = il * ch * 3 + il + 20, interleave = il })
t:eq(dec.frames, 3 * 4 * 28 + 28, "cut interleaved: the whole frames both channels have")
end
-- ------------------------------------------------------------------------------------------------ fuzz
local samples = {}
for _, c in Cases do
table.insert(samples, c.data)
end
t:fuzz("kernels", function(b: buffer)
local n = buffer.len(b)
local out = buffer.create(64 * 64 * 4)
local st = buffer.create(64)
local coefs = buffer.create(64)
for k = 0, 15 do
buffer.writei32(coefs, k * 4, if n > k then buffer.readi8(b, k) * 64 else 0)
end
if n >= 18 then
Adpcm.adx(b, 0, 18, 1, 7334, -3283, n % 2 == 0, st, 0, out, 0, 1)
end
if n >= 16 then
Adpcm.psx(b, 0, 16, 1, st, 8, out, 0, 1)
end
if n >= 8 then
Adpcm.dsp(b, 0, 14, coefs, 0, st, 16, out, 0, 1)
end
if n >= 34 then
Adpcm.imaQt(b, 0, st, 24, out, 0, 1)
end
if n >= 1 then
Adpcm.ima(b, 0, 0, 1, math.min(n * 2, 1000), true, st, 32, out, 0, 1)
end
if n >= 17 then
Adpcm.modplug(b, 0, math.min((n - 16) * 2, 1000), out, 0)
end
if n >= 1 then
Adpcm.swf(b, 0, math.min(n, 600), 1 + n % 2, buffer.create(Adpcm.swfFrames(b, 0, math.min(n, 600), 1 + n % 2) * 8 + 8), 0)
end
Adpcm.imaWav(b, 0, n, 2, 1017, buffer.create(1017 * 2 * 4), 0)
Adpcm.msBlock(b, 0, n, 2, 1012, buffer.create(1012 * 2 * 4), 0, coefs, 8, buffer.create(40))
local dec = Decoder.interleaved({ sampleRate = 8000, channels = 2, tags = {} }, { kind = "adx", buf = b, offset = 0, size = n, interleave = 18, c1 = 7334, c2 = -3283 })
if dec then
dec:read(100000)
dec:seek(0)
end
return true
end, samples, { mutations = 6, garbage = 60 })
t:done()