ws-audio2: codecs/audio/Adpcm (IMA / ima4 / IMA WAV / MS / ADX / PS-ADPCM / DSP decode, IMA + MS encode, Decoder layouts) and audio/adpcm (raw streams)
- codecs/audio/Adpcm: decoder kernels bit-exact with ffmpeg (IMA, ima4, MS) and vgmstream (ADX, PS-ADPCM, DSP); IMA nibble / IMA WAV / MS block encoders. Adpcm/Decoder: streaming Decoders over the shared layouts (channel interleaved frames, nibble streams, self-contained blocks) with exact seeking through state snapshots. - audio/wav: its IMA / MS block decoders moved to codecs/audio/Adpcm (wav tests unchanged and green). - codecs/audio/Stream (shared, small and backward compatible): pcm() may return a buffer of f32 (copied with buffer.copy) instead of a table. - audio/adpcm: headerless ima / ima_wav / ms / ima_qt / psx streams by options; encode ima / ima_wav / ms. - licenses: vgmstream (ISC).
This commit is contained in:
1 parent
de4f14a343
commit
363b14932f
14 files changed
+1931
-166
No files matched your search
@@ -0,0 +1,127 @@
|
||||
-- audio/adpcm tests: headerless streams (IMA nibbles, IMA WAV / MS blocks, Apple ima4, PS-ADPCM) decoded with the
|
||||
-- layout in the options, bit-exact against ffmpeg / vgmstream (tools/fixtures/adpcm.py), with seeking; our encoder's
|
||||
-- streams (ima, ms, ima_wav) as ffmpeg decodes them (wrapped in KVAG / WAV in the fixture step) against our decode of
|
||||
-- the same bytes, and their quality; options and errors; the api by format name; the fuzz pass.
|
||||
local T = require("@tests/T")
|
||||
local Raw = require("@formats/audio/adpcm")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Err = require("@formats/core/Err")
|
||||
local Types = require("@formats/Types")
|
||||
local F = require("@formats")
|
||||
local Cases = require("@tests/gen/adpcm_raw")
|
||||
local Ours = require("@tests/gen/adpcm_ours")
|
||||
local Sig = require("@tests/gen/adpcm_sig")
|
||||
|
||||
local t = T.suite("adpcm")
|
||||
|
||||
t:eq(Raw.name, "adpcm", "name")
|
||||
t:check(Raw.caps.decode and Raw.caps.encode and Raw.open ~= nil, "caps")
|
||||
t:eq(Raw.sniff(Bytes.reader(buffer.fromstring("RIFF....WAVE"))), 0, "sniff is always 0")
|
||||
|
||||
local function same(out: buffer, ref: buffer, from: number, count: number): boolean
|
||||
for i = 0, count - 1 do
|
||||
if buffer.readf32(out, i * 4) * 32768 ~= buffer.readi16(ref, (from + i) * 2) then
|
||||
return false
|
||||
end
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
local samples = {}
|
||||
for _, c in Cases do
|
||||
table.insert(samples, c.data)
|
||||
local opts = { codec = c.codec, sampleRate = c.rate, channels = c.ch, blockAlign = c.blockAlign }
|
||||
local ref = buffer.fromstring(c.ref)
|
||||
local frames = buffer.len(ref) // (2 * c.ch)
|
||||
local a, err = Raw.decode(Bytes.reader(buffer.fromstring(c.data)), opts)
|
||||
t:check(a, c.name .. ": " .. tostring(err))
|
||||
t:eq(a.frames, frames, c.name .. ": frames")
|
||||
t:check(same(a.samples, ref, 0, frames * c.ch), c.name .. ": bit-exact")
|
||||
t:eq(a.meta.source.codec, c.codec, c.name .. ": meta.source.codec")
|
||||
local dec = Raw.open(Bytes.reader(buffer.fromstring(c.data)), opts) :: any
|
||||
for _, f in { frames // 2, 7, frames - 3, 1000, 0 } do
|
||||
dec:seek(f / c.rate)
|
||||
local b, n = dec:read(40)
|
||||
t:check(n == math.min(40, frames - f) and same(b, ref, f * c.ch, n * c.ch), c.name .. ": seek to " .. f)
|
||||
end
|
||||
end
|
||||
t:note("%d raw streams bit-exact with ffmpeg / vgmstream", #Cases)
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ encoder
|
||||
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
|
||||
|
||||
local audio = Types.Audio.new(Sig.rate, Sig.channels, Sig.frames)
|
||||
buffer.writestring(audio.samples, 0, Sig.data)
|
||||
for _, c in Ours do
|
||||
local opts = (if c.name == "ima" then { codec = "ima" } else { codec = c.name, blockAlign = 512 }) :: any
|
||||
opts.channels, opts.sampleRate = 2, Sig.rate
|
||||
local out, err = Raw.encode(audio, opts)
|
||||
t:check(out and buffer.tostring(out) == c.data, c.name .. ": encode is deterministic: " .. tostring(err))
|
||||
local ref = buffer.fromstring(c.ref)
|
||||
local back = Raw.decode(Bytes.reader(buffer.fromstring(c.data)), opts) :: any
|
||||
local n = buffer.len(ref) // 4
|
||||
t:eq(back.frames, n, c.name .. ": ffmpeg and we decode as many frames")
|
||||
t:check(same(back.samples, ref, 0, n * 2), c.name .. ": ffmpeg decodes our stream as we do")
|
||||
local s = snr(audio.samples, back.samples, Sig.frames * 2)
|
||||
t:check(s > 20, string.format("%s: %.1f dB", c.name, s))
|
||||
t:note("encode %s: %.1f dB", c.name, s)
|
||||
-- from a streaming Decoder too
|
||||
local again = Raw.encode(Raw.open(Bytes.reader(out :: buffer), opts) :: any, opts)
|
||||
t:check(again ~= nil, c.name .. ": encode from a Decoder")
|
||||
end
|
||||
do
|
||||
local _, e1 = Raw.encode(audio, { codec = "psx" } :: any)
|
||||
t:check(e1 and e1.code == Err.ARG, "encode psx: ARG")
|
||||
local _, e2 = Raw.encode(audio, { codec = "ms", blockAlign = 8 })
|
||||
t:check(e2 and e2.code == Err.ARG, "encode: tiny blockAlign")
|
||||
local _, e3 = Raw.encode(audio, { codec = "ima_wav", blockAlign = 514 })
|
||||
t:check(e3 and e3.code == Err.ARG, "encode ima_wav: blockAlign not a multiple of 4 * channels")
|
||||
local _, e4 = Raw.encode({} :: any)
|
||||
t:check(e4 and e4.code == Err.ARG, "encode {}")
|
||||
local _, e5 = Raw.open(Bytes.reader(buffer.create(10)), { codec = "dsp" } :: any)
|
||||
t:check(e5 and e5.code == Err.ARG, "open: unknown codec")
|
||||
local _, e6 = Raw.open(Bytes.reader(buffer.create(10)), { channels = 0 })
|
||||
t:check(e6 and e6.code == Err.ARG, "open: 0 channels")
|
||||
local _, e7 = Raw.open(Bytes.reader(buffer.create(10)), { codec = "ms", channels = 2, blockAlign = 9 })
|
||||
t:check(e7 and e7.code == Err.CORRUPT, "open: block too small")
|
||||
local _, e8 = Raw.decode(Bytes.reader(buffer.create(100)), { maxFrames = 10 })
|
||||
t:check(e8 and e8.code == Err.LIMIT, "maxFrames")
|
||||
local lo = Raw.decode(Bytes.reader(buffer.fromstring("\1\2\3")), { lowFirst = true }) :: any
|
||||
local hi = Raw.decode(Bytes.reader(buffer.fromstring("\16\32\48"))) :: any
|
||||
t:check(lo.frames == 6 and buffer.tostring(lo.samples) == buffer.tostring(hi.samples), "nibble order")
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ api
|
||||
do
|
||||
local c = Cases[1]
|
||||
local a, err = F.audio.load(c.data, { format = "adpcm", codec = c.codec, sampleRate = c.rate, channels = c.ch })
|
||||
t:check(a and a.frames == buffer.len(buffer.fromstring(c.ref)) // (2 * c.ch), "F.audio.load by format: " .. tostring(err))
|
||||
t:eq(F.detect(c.data, { name = "sound.ima" }), "adpcm", "F.detect by extension")
|
||||
local out = F.save(a, nil, { format = "adpcm", codec = "ms" })
|
||||
t:check(out ~= nil and buffer.len(out) % 256 == 0, "F.save adpcm")
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ fuzz
|
||||
local kinds = { "ima", "ima_wav", "ms", "ima_qt", "psx" }
|
||||
local k = 0
|
||||
t:fuzz("open + read + seek", function(b: buffer)
|
||||
k += 1
|
||||
local dec = Raw.open(Bytes.reader(b), { codec = kinds[k % 5 + 1] :: any, channels = k % 3 + 1, blockAlign = 64 + k % 7 * 4, interleave = 32 })
|
||||
if dec then
|
||||
dec:read(100000)
|
||||
dec:seek(dec.duration / 3)
|
||||
dec:read(77)
|
||||
end
|
||||
return dec
|
||||
end, samples, { mutations = 6 })
|
||||
|
||||
t:done()
|
||||
@@ -0,0 +1,228 @@
|
||||
-- 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); a fuzz pass of random bytes through every kernel (ranges valid, contents random) and the layouts.
|
||||
local T = require("@tests/T")
|
||||
local Adpcm = require("@formats/codecs/audio/Adpcm")
|
||||
local Decoder = require("@formats/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
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ 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
|
||||
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()
|
||||
Reference in new issue
Block a user