118 lines
4.7 KiB
Luau
118 lines
4.7 KiB
Luau
-- audio/opus encoder tests (the CELT-only writer, ws-m4a; mono and mid / side stereo): our Ogg Opus files (re-encoded here, compared with the bytes
|
|
-- checked in the fixture step) decode in libopus' opusdec to the input's exact length (pre-skip + end trimming) with a
|
|
-- minimum SNR, opusinfo finds no problem, ffmpeg reads them, OpusTags carry the tags; our decoder agrees with opusdec;
|
|
-- resampled 44.1 kHz input; silence frames; streaming input; errors; a fuzz no-throw pass over our files.
|
|
-- Fixtures: tools/fixtures/opus_celt.py.
|
|
local T = require("@tests/T")
|
|
local Opus = require("@rosetta/audio/opus")
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local Crc32 = require("@rosetta/core/Crc32")
|
|
local Err = require("@rosetta/core/Err")
|
|
local Cases = require("@tests/audio/_opus_cases")
|
|
local Fx = require("@tests/gen/opus_celt")
|
|
|
|
local t = T.suite("opus-encode")
|
|
|
|
t:check(Opus.caps.encode and Opus.encode ~= nil, "caps: encode")
|
|
|
|
local byName = {}
|
|
for _, f in Fx do
|
|
byName[f.name] = f
|
|
end
|
|
|
|
local function snr(ref: buffer, refBase: number, got: buffer, n: number): number
|
|
local s, e = 0, 0
|
|
for i = 0, n - 1 do
|
|
local x = buffer.readf32(ref, (refBase + i) * 4)
|
|
local d = buffer.readf32(got, (refBase + i) * 4) - x
|
|
s += x * x
|
|
e += d * d
|
|
end
|
|
return if e == 0 then math.huge else 10 * math.log10(s / math.max(e, 1e-30))
|
|
end
|
|
|
|
local files = {}
|
|
for _, c in Cases.list do
|
|
local fx = byName[c.name]
|
|
t:check(fx, c.name .. ": fixture")
|
|
local input = Cases.signal(c)
|
|
local file, err = Opus.encode(input, { bitrate = c.bitrate })
|
|
t:check(file, c.name .. ": encode: " .. tostring(err))
|
|
table.insert(files, file)
|
|
t:eq(string.format("%08x", Crc32.of(file :: buffer)), string.format("%08x", fx.crc), c.name .. ": the bytes the reference tools checked")
|
|
t:check(fx.opusinfo, c.name .. ": opusinfo reports no problem")
|
|
t:check(fx.ffmpeg, c.name .. ": ffmpeg decodes it")
|
|
local frames48 = math.floor(input.frames * 48000 / c.rate + 0.5)
|
|
local ch = c.channels
|
|
t:eq(#fx.opusdec // (4 * ch), frames48, c.name .. ": opusdec length = the input's (pre-skip + end trimming)")
|
|
t:eq(fx.ffmpegFrames, frames48, c.name .. ": ffmpeg length")
|
|
-- our decoder vs opusdec
|
|
local ours = Opus.decode(Bytes.reader(file :: buffer)) :: any
|
|
t:eq(ours.frames, frames48, c.name .. ": our decoder's length")
|
|
local ref = buffer.fromstring(fx.opusdec)
|
|
local agree = snr(ref, 0, ours.samples, frames48 * ch)
|
|
t:check(agree > 60, string.format("%s: our decoder vs opusdec %.1f dB", c.name, agree))
|
|
-- quality vs the input (48 kHz inputs; skip the edges)
|
|
if c.rate == 48000 and frames48 > 4000 then
|
|
local s = snr(input.samples, 960 * ch, ref, (frames48 - 1920) * ch)
|
|
t:check(s >= c.minSnr, string.format("%s: SNR %.1f dB (minimum %d)", c.name, s, c.minSnr))
|
|
t:note("%s: %d bytes, SNR %.1f dB vs the input, our decoder vs opusdec %.1f dB", c.name, buffer.len(file :: buffer), s, agree)
|
|
end
|
|
if c.tags then
|
|
for k, v in c.tags do
|
|
t:eq(fx.comments[k], v, c.name .. ": opusinfo comment " .. k)
|
|
t:eq(ours.tags[k], v, c.name .. ": our tag " .. k)
|
|
end
|
|
end
|
|
end
|
|
|
|
-- silence frames decode to silence
|
|
do
|
|
local c = Cases.list[1]
|
|
local ref = buffer.fromstring(byName[c.name].opusdec)
|
|
local peak = 0
|
|
for i = math.floor(0.62 * 48000), math.floor(0.73 * 48000) do
|
|
peak = math.max(peak, math.abs(buffer.readf32(ref, i * 4)))
|
|
end
|
|
t:check(peak < 1e-3, "digital silence stays silent (peak " .. peak .. ")")
|
|
end
|
|
|
|
-- a streaming decoder as the input
|
|
do
|
|
local c = Cases.list[1]
|
|
local dec = Opus.open(Bytes.reader(files[1] :: buffer)) :: any
|
|
local again, err = Opus.encode(dec, { bitrate = 64000 })
|
|
t:check(again, "encode from a decoder: " .. tostring(err))
|
|
local back = Opus.decode(Bytes.reader(again :: buffer)) :: any
|
|
t:eq(back.frames, math.floor(c.seconds * 48000 + 0.5), "re-encoded length")
|
|
-- values below 1000 are kbit/s: the same file as the bit/s value
|
|
local kb = Opus.encode(Opus.open(Bytes.reader(files[1] :: buffer)) :: any, { bitrate = 64 })
|
|
t:check(kb ~= nil and buffer.tostring(kb :: buffer) == buffer.tostring(again :: buffer), "bitrate 64 = 64000 bit/s")
|
|
end
|
|
|
|
-- errors
|
|
do
|
|
local three = Cases.signal({ rate = 48000, channels = 3, seconds = 0.05 })
|
|
local _, e = Opus.encode(three)
|
|
t:check(e and e.code == Err.UNSUPPORTED, "3 channels: " .. tostring(e))
|
|
local _, e2 = Opus.encode(Cases.signal(Cases.list[6]), { bitrate = 1000 })
|
|
t:check(e2 and e2.code == Err.ARG, "bitrate too low")
|
|
local _, e3 = Opus.encode({} :: any)
|
|
t:check(e3 and e3.code == Err.ARG, "not audio")
|
|
end
|
|
|
|
-- fuzz the decoder over our files
|
|
do
|
|
local samples = {}
|
|
for i, f in files do
|
|
if i <= 3 then
|
|
table.insert(samples, string.sub(buffer.tostring(f :: buffer), 1, 4000))
|
|
end
|
|
end
|
|
t:fuzz("decode our files", function(b: buffer)
|
|
return Opus.decode(Bytes.reader(b))
|
|
end, samples, { mutations = 10, garbage = 30 })
|
|
end
|
|
|
|
t:done()
|