ws-audio: audio/vorbis decoder: stb_vorbis 1.22 port (setup, codebooks, floor 1, residue 0/1/2, coupling) on containers/Ogg + codecs/Mdct, libvorbis-exact trimming, granule-exact seek, packetDecoder for other containers; tests vs oggdec (counts, 1 LSB) + ffmpeg (SNR 138 dB), seeks, fuzz; bench line
This commit is contained in:
1 parent
aee2d3257e
commit
042a782f2f
13 files changed
+2210
-1
No files matched your search
@@ -0,0 +1,193 @@
|
||||
-- audio/vorbis tests: oggenc / ffmpeg streams (qualities -1 .. 10, managed bitrate, 8 .. 96 kHz, mono / stereo / 5.1,
|
||||
-- transients, silence, tags, sub-block lengths) against oggdec (libvorbisfile: sample counts and 16-bit samples, so
|
||||
-- start / end trimming is exact) and ffmpeg's f32 decode (SNR); streaming reads; seeking (sample-exact against the
|
||||
-- reference at the target); the packet-level decoder; errors; fuzz. Fixtures: tools/fixtures/vorbis.py.
|
||||
local T = require("@tests/T")
|
||||
local Vorbis = require("@formats/audio/vorbis")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Err = require("@formats/core/Err")
|
||||
local Types = require("@formats/Types")
|
||||
local Ogg = require("@formats/containers/Ogg")
|
||||
local Cases = require("@tests/gen/vorbis_cases")
|
||||
|
||||
local t = T.suite("vorbis")
|
||||
|
||||
local function reader(s: string): Bytes.Reader
|
||||
return Bytes.reader(buffer.fromstring(s))
|
||||
end
|
||||
local function s16(x: number): number
|
||||
local v = math.floor(x * 32768 + 0.5)
|
||||
return math.clamp(v, -32768, 32767)
|
||||
end
|
||||
-- the largest |our sample as s16 - oggdec's| over n samples of ours at byte a0 against ref16 from sample r0
|
||||
local function maxDiff16(a: buffer, a0: number, ref: buffer, r0: number, n: number): number
|
||||
local m = 0
|
||||
for i = 0, n - 1 do
|
||||
local d = math.abs(s16(buffer.readf32(a, a0 + i * 4)) - buffer.readi16(ref, (r0 + i) * 2))
|
||||
if d > m then
|
||||
m = d
|
||||
end
|
||||
end
|
||||
return m
|
||||
end
|
||||
local function snr(a: buffer, ref: buffer, n: number): number
|
||||
local sig, er = 0, 0
|
||||
for i = 0, n - 1 do
|
||||
local x = buffer.readf32(ref, i * 4)
|
||||
local d = x - buffer.readf32(a, i * 4)
|
||||
sig += x * x
|
||||
er += d * d
|
||||
end
|
||||
if er == 0 then
|
||||
return 200
|
||||
end
|
||||
return if sig > 0 then 10 * math.log10(sig / er) else -200
|
||||
end
|
||||
|
||||
t:eq(Vorbis.name, "vorbis", "name")
|
||||
t:eq(Vorbis.category, "audio", "category")
|
||||
t:check(Vorbis.caps.decode and Vorbis.open ~= nil, "caps")
|
||||
|
||||
local minSnr = math.huge
|
||||
for _, c in Cases do
|
||||
local a, err = Vorbis.decode(reader(c.data))
|
||||
t:check(a, c.name .. ": " .. tostring(err))
|
||||
t:check(a.sampleRate == c.rate and a.channels == c.channels, c.name .. ": format")
|
||||
t:eq(a.frames, c.frames, c.name .. ": frames = oggdec's")
|
||||
t:eq(a.frames, c.inputFrames, c.name .. ": frames = the encoder's input")
|
||||
local ref16 = buffer.fromstring(c.ref16)
|
||||
local n = c.frames * c.channels
|
||||
local d = maxDiff16(a.samples, 0, ref16, 0, n)
|
||||
t:check(d <= 1, string.format("%s: within 1 LSB of oggdec (max %d)", c.name, d))
|
||||
if c.ffRef then
|
||||
local s = snr(a.samples, buffer.fromstring(c.ffRef), n)
|
||||
minSnr = math.min(minSnr, s)
|
||||
t:check(s > 100, string.format("%s: SNR vs ffmpeg %.1f dB", c.name, s))
|
||||
end
|
||||
-- streaming in odd chunks gives the same samples
|
||||
local dec = Vorbis.open(reader(c.data)) :: Types.Decoder
|
||||
t:eq(dec.frames, c.frames, c.name .. ": Decoder.frames")
|
||||
local got, ok = 0, true
|
||||
while true do
|
||||
local b, k = dec:read(1013)
|
||||
if k == 0 then
|
||||
break
|
||||
end
|
||||
for i = 0, k * c.channels - 1, 7 do
|
||||
ok = ok and buffer.readf32(b, i * 4) == buffer.readf32(a.samples, got * c.channels * 4 + i * 4)
|
||||
end
|
||||
got += k
|
||||
end
|
||||
t:check(ok and got == c.frames, c.name .. ": streamed reads")
|
||||
-- seeks: sample-exact against the full decode
|
||||
for _, frac in { 0.5, 0.1, 0.93, 0 } do
|
||||
local target = math.floor(c.frames * frac)
|
||||
dec:seek(target / c.rate)
|
||||
t:check(math.abs(dec:position() - target / c.rate) < 1e-9, c.name .. ": position after seek")
|
||||
local b, k = dec:read(300)
|
||||
local want = math.min(300, c.frames - target)
|
||||
t:eq(k, want, string.format("%s: frames after seek to %d", c.name, target))
|
||||
local same = true
|
||||
for i = 0, k * c.channels - 1 do
|
||||
same = same and buffer.readf32(b, i * 4) == buffer.readf32(a.samples, (target * c.channels + i) * 4)
|
||||
end
|
||||
t:check(same, string.format("%s: samples after seek to %d", c.name, target))
|
||||
end
|
||||
end
|
||||
t:note("%d streams match oggdec (counts, 1 LSB) and ffmpeg (min SNR %.1f dB)", #Cases, minSnr)
|
||||
|
||||
local function case(name: string): any
|
||||
for _, c in Cases do
|
||||
if c.name == name then
|
||||
return c
|
||||
end
|
||||
end
|
||||
error("no case " .. name)
|
||||
end
|
||||
|
||||
-- tags, meta, sniff, sub-range
|
||||
do
|
||||
local c = case("q2-stereo-32k-tags")
|
||||
local dec = Vorbis.open(reader(c.data)) :: any
|
||||
t:eq(dec.tags.title, "A Title", "title tag")
|
||||
t:eq(dec.tags.artist, "An Artist", "artist tag")
|
||||
t:eq(dec.tags.album, "The Album", "album tag")
|
||||
t:eq(dec.tags.track, "7", "track tag")
|
||||
t:eq(dec.tags.comment, "hello vorbis", "comment tag")
|
||||
t:check(type(dec.vendor) == "string" and string.find(dec.vendor, "Xiph", 1, true) ~= nil, "vendor")
|
||||
t:check(dec.bitrate > 0 and math.abs(dec.duration - c.frames / c.rate) < 1e-9, "bitrate / duration")
|
||||
t:eq(Vorbis.sniff(reader(c.data)), 1, "sniff")
|
||||
t:eq(Vorbis.sniff(reader("OggS")), 0, "sniff short")
|
||||
t:eq(Vorbis.sniff(reader("RIFF")), 0, "sniff other")
|
||||
local big = buffer.create(#c.data + 11)
|
||||
buffer.writestring(big, 11, c.data)
|
||||
local a = Vorbis.decode(Bytes.reader(big, 11, #c.data))
|
||||
t:check(a and a.frames == c.frames, "decode from a sub-range")
|
||||
end
|
||||
|
||||
-- the packet-level decoder (as Matroska / MP4 would use it) gives the same samples, minus the Ogg trimming
|
||||
do
|
||||
local c = case("q5-stereo-44k-music")
|
||||
local full = Vorbis.decode(reader(c.data)) :: Types.Audio
|
||||
local demux = Ogg.open(buffer.fromstring(c.data))
|
||||
local serial = demux:find("vorbis")
|
||||
local h = {}
|
||||
for i = 1, 3 do
|
||||
h[i] = Ogg.copy(demux:nextPacket(serial))
|
||||
end
|
||||
local pd = Vorbis.packetDecoder(h[1], h[2], h[3])
|
||||
t:check(pd and pd.channels == 2 and pd.sampleRate == 44100, "packetDecoder")
|
||||
local total, same = 0, true
|
||||
for p in demux:packets(serial) do
|
||||
local n = pd:decode(p.data, p.offset, p.size)
|
||||
for i = 1, n, 13 do
|
||||
if total + i <= full.frames then
|
||||
same = same and math.abs(pd.out[1][i] - buffer.readf32(full.samples, (total + i - 1) * 8)) < 1e-6
|
||||
end
|
||||
end
|
||||
total += math.max(n, 0)
|
||||
end
|
||||
t:check(same and total >= full.frames, "packetDecoder samples")
|
||||
local _, e = Vorbis.packetDecoder(h[2], h[1], h[3])
|
||||
t:check(e and e.code == Err.CORRUPT, "packetDecoder with headers swapped")
|
||||
end
|
||||
|
||||
-- errors
|
||||
do
|
||||
local _, e1 = Vorbis.decode(reader("OggS but not really"))
|
||||
t:check(e1 and Err.is(e1), "junk: " .. tostring(e1))
|
||||
local c = case("short-100").data
|
||||
local _, e2 = Vorbis.decode(reader(string.sub(c, 1, 60)))
|
||||
t:check(e2 and Err.is(e2), "cut in the headers: " .. tostring(e2))
|
||||
-- a truncated stream decodes what is there
|
||||
local q = case("q5-stereo-44k-music")
|
||||
local cut = Vorbis.decode(reader(string.sub(q.data, 1, #q.data // 2)), { verify = false })
|
||||
t:check(cut and cut.frames > 0 and cut.frames < q.frames, "truncated stream")
|
||||
end
|
||||
|
||||
-- fuzz: whole streams with mutations past the headers, truncations, garbage
|
||||
do
|
||||
local samples = {}
|
||||
for _, name in { "short-100", "short-3000-mono", "q-1-mono-8k-music", "ffmpeg-mono-16k" } do
|
||||
table.insert(samples, case(name).data)
|
||||
end
|
||||
t:fuzz("decode", function(b: buffer)
|
||||
return Vorbis.decode(Bytes.reader(b), { verify = false })
|
||||
end, samples, { mutations = 40 })
|
||||
t:fuzz("decode (CRC checked)", function(b: buffer)
|
||||
return Vorbis.decode(Bytes.reader(b))
|
||||
end, samples, { mutations = 5, garbage = 20 })
|
||||
t:fuzz("open + seek", function(b: buffer)
|
||||
local d = Vorbis.open(Bytes.reader(b), { verify = false })
|
||||
if d then
|
||||
d:read(500)
|
||||
d:seek(0.05)
|
||||
d:read(500)
|
||||
d:seek(1e9)
|
||||
d:read(10)
|
||||
end
|
||||
return d
|
||||
end, samples, { mutations = 10, garbage = 20 })
|
||||
end
|
||||
|
||||
t:done()
|
||||
Reference in new issue
Block a user