merge ws/audio
This commit is contained in:
commit
fa98dd77c2
43 files changed
+8963
-19
No files matched your search
@@ -0,0 +1,183 @@
|
||||
-- audio/aiff tests: files written by ffmpeg and sox in every sample format (AIFF 8 / 12 / 16 / 24 / 32-bit, AIFF-C
|
||||
-- sowt / fl32 / fl64 / ulaw / alaw, 6 channels, text chunks) against ffmpeg's decode; streaming reads and seeks;
|
||||
-- our encoder's output for every sample format decoded by ffmpeg (fixture step) and by us; errors; fuzz.
|
||||
-- Fixtures: tools/fixtures/aiff.py.
|
||||
local T = require("@tests/T")
|
||||
local Aiff = require("@formats/audio/aiff")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Err = require("@formats/core/Err")
|
||||
local Types = require("@formats/Types")
|
||||
local Cases = require("@tests/gen/aiff_cases")
|
||||
local Enc = require("@tests/gen/aiff_enc")
|
||||
local Sig = require("@tests/gen/aiff_sig")
|
||||
|
||||
local t = T.suite("aiff")
|
||||
|
||||
local function reader(s: string): Bytes.Reader
|
||||
return Bytes.reader(buffer.fromstring(s))
|
||||
end
|
||||
-- max |a - b| over n f32 samples (a at byte ao, b at byte bo)
|
||||
local function maxDiff(a: buffer, ao: number, b: buffer, bo: number, n: number): number
|
||||
local m = 0
|
||||
for i = 0, n - 1 do
|
||||
local d = math.abs(buffer.readf32(a, ao + i * 4) - buffer.readf32(b, bo + i * 4))
|
||||
if d > m then
|
||||
m = d
|
||||
end
|
||||
end
|
||||
return m
|
||||
end
|
||||
|
||||
-- the contract
|
||||
t:eq(Aiff.name, "aiff", "name")
|
||||
t:eq(Aiff.category, "audio", "category")
|
||||
t:check(Aiff.caps.decode and Aiff.caps.encode and table.find(Aiff.extensions, "aif") ~= nil, "caps / extensions")
|
||||
|
||||
-- reference files: decode, stream, seek
|
||||
for _, c in Cases do
|
||||
local a, err = Aiff.decode(reader(c.data))
|
||||
t:check(a, c.name .. ": " .. tostring(err))
|
||||
t:check(a.kind == "audio" and a.sampleRate == c.rate and a.channels == c.channels, c.name .. ": format")
|
||||
t:eq(a.frames, c.frames, c.name .. ": frames")
|
||||
local ref = buffer.fromstring(c.ref)
|
||||
local n = c.frames * c.channels
|
||||
t:check(maxDiff(a.samples, 0, ref, 0, n) < 1e-6, c.name .. ": samples match ffmpeg")
|
||||
-- streaming in odd chunks
|
||||
local dec = Aiff.open(reader(c.data)) :: Types.Decoder
|
||||
local got, out = 0, buffer.create(0)
|
||||
local ok = true
|
||||
while true do
|
||||
local b, k = dec:read(777, out)
|
||||
if k == 0 then
|
||||
break
|
||||
end
|
||||
out = b
|
||||
ok = ok and maxDiff(b, 0, ref, got * c.channels * 4, k * c.channels) < 1e-6
|
||||
got += k
|
||||
end
|
||||
t:check(ok and got == c.frames, c.name .. ": streamed reads")
|
||||
dec:seek(1000 / c.rate)
|
||||
t:check(math.abs(dec:position() - 1000 / c.rate) < 1e-9, c.name .. ": position")
|
||||
local b, k = dec:read(50)
|
||||
t:check(k == 50 and maxDiff(b, 0, ref, 1000 * c.channels * 4, 50 * c.channels) < 1e-6, c.name .. ": seek")
|
||||
end
|
||||
t:note("%d reference files match ffmpeg", #Cases)
|
||||
|
||||
-- details of particular files
|
||||
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
|
||||
do
|
||||
local a = Aiff.decode(reader(case("ffmpeg-tags").data)) :: Types.Audio
|
||||
t:eq(a.tags.title, "Song Name", "NAME chunk")
|
||||
t:eq(a.tags.artist, "Some One", "AUTH chunk")
|
||||
local fl = Aiff.decode(reader(case("ffmpeg-fl32").data)) :: Types.Audio
|
||||
t:check(fl.meta.source.aifc and fl.meta.source.compression == "fl32", "fl32 meta")
|
||||
-- a 12-bit sample size: samples sit left-justified in 16-bit containers
|
||||
local s16 = case("sox-aiff-s16")
|
||||
local q = string.find(s16.data, "COMM", 1, true) :: number
|
||||
local d12 = string.sub(s16.data, 1, q + 13) .. "\0\12" .. string.sub(s16.data, q + 16)
|
||||
local s12 = Aiff.decode(reader(d12)) :: Types.Audio
|
||||
t:check(s12.meta.source.bitDepth == 12 and maxDiff(s12.samples, 0, buffer.fromstring(s16.ref), 0, s16.frames * 2) < 1e-6, "12-bit sample size")
|
||||
local sw = Aiff.info(reader(case("ffmpeg-sowt").data))
|
||||
t:check(sw and sw.compression == "sowt" and sw.format == "s16le", "info: sowt")
|
||||
-- inside a bigger buffer
|
||||
local c = case("ffmpeg-s16be")
|
||||
local big = buffer.create(#c.data + 9)
|
||||
buffer.writestring(big, 5, c.data)
|
||||
local a2 = Aiff.decode(Bytes.reader(big, 5, #c.data)) :: Types.Audio
|
||||
t:check(a2 and maxDiff(a2.samples, 0, buffer.fromstring(c.ref), 0, c.frames * c.channels) < 1e-6, "decode from a sub-range")
|
||||
-- truncated sample data: the frames present
|
||||
local cut = string.sub(c.data, 1, #c.data - 1001)
|
||||
local a3 = Aiff.decode(reader(cut)) :: Types.Audio
|
||||
t:check(a3 and a3.frames < c.frames and a3.frames > 0 and a3.meta.source.truncated, "truncated SSND")
|
||||
end
|
||||
|
||||
-- our encoder: ffmpeg decodes every sample format (fixture step), within the format's step; streamed == from Audio
|
||||
local TOL = { s8 = 0.5 / 128, ["s16-mono"] = 0.5 / 32768, s16 = 0.5 / 32768, sowt16 = 0.5 / 32768, ["aifc-s16"] = 0.5 / 32768, s24 = 0.5 / 8388608, sowt24 = 0.5 / 8388608, s32 = 1e-7, f32 = 0, f64 = 0, ulaw = 0.04, alaw = 0.04 }
|
||||
local sig = buffer.fromstring(Sig.data)
|
||||
for _, e in Enc do
|
||||
t:eq(e.ffmpegErrors, "", e.name .. ": ffmpeg reads our file without errors")
|
||||
t:check(e.frames == Sig.frames and e.rate == Sig.rate, e.name .. ": ffmpeg frames / rate")
|
||||
t:check(e.maxDiff <= TOL[e.name] + 1e-7, string.format("%s: ffmpeg decode within %g (max diff %g)", e.name, TOL[e.name], e.maxDiff))
|
||||
t:check(e.sameStreamed, e.name .. ": encoding a Decoder gives the same bytes")
|
||||
local back = Aiff.decode(reader(e.data)) :: Types.Audio
|
||||
t:check(back and back.frames == Sig.frames and back.channels == e.channels, e.name .. ": our decoder reads our file")
|
||||
if e.channels == 2 then
|
||||
t:check(maxDiff(back.samples, 0, sig, 0, Sig.frames * 2) <= TOL[e.name] + 1e-7, e.name .. ": round trip")
|
||||
end
|
||||
end
|
||||
t:note("%d encoder outputs read back by ffmpeg and by us", #Enc)
|
||||
|
||||
-- encoder: tags, odd sizes, errors
|
||||
do
|
||||
local a = Types.Audio.new(44100, 1, 3)
|
||||
buffer.writef32(a.samples, 0, 0.5)
|
||||
buffer.writef32(a.samples, 4, -1)
|
||||
buffer.writef32(a.samples, 8, 2) -- clips
|
||||
a.tags = { title = "odd", artist = "me", comment = "x" }
|
||||
local b = Aiff.encode(a, { sampleFormat = "s8" }) :: buffer
|
||||
local back = Aiff.decode(Bytes.reader(b)) :: Types.Audio
|
||||
t:check(back.frames == 3 and back.tags.title == "odd" and back.tags.artist == "me" and back.tags.comment == "x", "odd length + tags")
|
||||
t:check(buffer.readf32(back.samples, 0) == 0.5 and buffer.readf32(back.samples, 4) == -1 and buffer.readf32(back.samples, 8) == 127 / 128, "s8 values, clipping")
|
||||
t:eq(buffer.len(b) % 2, 0, "chunks padded to even sizes")
|
||||
t:check(Aiff.info(Bytes.reader(b)).sampleRate == 44100, "80-bit rate round trip")
|
||||
local empty = Aiff.encode(Types.Audio.new(8000, 2, 0)) :: buffer
|
||||
local e0 = Aiff.decode(Bytes.reader(empty))
|
||||
t:check(e0 and e0.frames == 0, "empty audio")
|
||||
local _, e1 = Aiff.encode({} :: any)
|
||||
t:check(e1 and e1.code == Err.ARG, "encode junk: ARG")
|
||||
local _, e2 = Aiff.encode(a, { sampleFormat = "s12" } :: any)
|
||||
t:check(e2 and e2.code == Err.ARG, "bad sampleFormat: ARG")
|
||||
local _, e3 = Aiff.encode(a, { sampleFormat = "f32", littleEndian = true })
|
||||
t:check(e3 and e3.code == Err.ARG, "littleEndian float: ARG")
|
||||
local _, e5 = Aiff.encode(a, { sampleFormat = "s24", littleEndian = true })
|
||||
t:check(e5 and e5.code == Err.ARG, "littleEndian 24-bit: ARG")
|
||||
t:eq(Aiff.sniff(Bytes.reader(b)), 1, "sniff")
|
||||
t:eq(Aiff.sniff(reader("RIFF....WAVE")), 0, "sniff wav")
|
||||
t:eq(Aiff.sniff(reader("")), 0, "sniff nothing")
|
||||
end
|
||||
|
||||
-- errors
|
||||
do
|
||||
local _, e = Aiff.decode(reader("FORM\0\0\0\4WAVE"))
|
||||
t:check(e and e.code == Err.CORRUPT, "wrong FORM type: " .. tostring(e))
|
||||
local c = case("ffmpeg-fl32").data
|
||||
local p = string.find(c, "fl32", 1, true) :: number
|
||||
local ima = string.sub(c, 1, p - 1) .. "ima4" .. string.sub(c, p + 4)
|
||||
local _, e2 = Aiff.decode(reader(ima))
|
||||
t:check(e2 and e2.code == Err.UNSUPPORTED, "ima4: UNSUPPORTED: " .. tostring(e2))
|
||||
local _, e3 = Aiff.decode(reader(string.sub(c, 1, 30)))
|
||||
t:check(e3 and (e3.code == Err.TRUNCATED or e3.code == Err.CORRUPT), "cut in COMM: " .. tostring(e3))
|
||||
local _, e4 = Aiff.decode(reader(case("ffmpeg-s16be").data), { maxFrames = 10 })
|
||||
t:check(e4 and e4.code == Err.LIMIT, "maxFrames: LIMIT")
|
||||
end
|
||||
|
||||
-- fuzz
|
||||
do
|
||||
local samples = {}
|
||||
for i, c in Cases do
|
||||
if i % 3 == 1 then
|
||||
table.insert(samples, string.sub(c.data, 1, 600))
|
||||
end
|
||||
end
|
||||
t:fuzz("decode", function(b: buffer)
|
||||
return Aiff.decode(Bytes.reader(b))
|
||||
end, samples, { mutations = 30, keepHeader = 0 })
|
||||
t:fuzz("open + read", function(b: buffer)
|
||||
local d = Aiff.open(Bytes.reader(b))
|
||||
if d then
|
||||
d:read(100)
|
||||
d:seek(0.001)
|
||||
d:read(100)
|
||||
end
|
||||
return d
|
||||
end, samples, { mutations = 10, garbage = 50 })
|
||||
end
|
||||
|
||||
t:done()
|
||||
@@ -0,0 +1,147 @@
|
||||
-- audio/au tests: files written by ffmpeg and sox in every encoding (8 / 16 / 24 / 32-bit, float, double, mu-law,
|
||||
-- A-law, an annotation) against ffmpeg's decode; streaming reads and seeks; our encoder's output for every encoding
|
||||
-- decoded by ffmpeg (fixture step) and by us; an unknown data size; errors; fuzz. Fixtures: tools/fixtures/au.py.
|
||||
local T = require("@tests/T")
|
||||
local Au = require("@formats/audio/au")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Err = require("@formats/core/Err")
|
||||
local Types = require("@formats/Types")
|
||||
local Cases = require("@tests/gen/au_cases")
|
||||
local Enc = require("@tests/gen/au_enc")
|
||||
local Sig = require("@tests/gen/au_sig")
|
||||
|
||||
local t = T.suite("au")
|
||||
|
||||
local function reader(s: string): Bytes.Reader
|
||||
return Bytes.reader(buffer.fromstring(s))
|
||||
end
|
||||
local function maxDiff(a: buffer, ao: number, b: buffer, bo: number, n: number): number
|
||||
local m = 0
|
||||
for i = 0, n - 1 do
|
||||
local d = math.abs(buffer.readf32(a, ao + i * 4) - buffer.readf32(b, bo + i * 4))
|
||||
if d > m then
|
||||
m = d
|
||||
end
|
||||
end
|
||||
return m
|
||||
end
|
||||
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
|
||||
|
||||
t:eq(Au.name, "au", "name")
|
||||
t:eq(Au.category, "audio", "category")
|
||||
t:check(Au.caps.decode and Au.caps.encode and table.find(Au.extensions, "snd") ~= nil, "caps / extensions")
|
||||
|
||||
for _, c in Cases do
|
||||
local a, err = Au.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")
|
||||
local ref = buffer.fromstring(c.ref)
|
||||
t:check(maxDiff(a.samples, 0, ref, 0, c.frames * c.channels) < 1e-6, c.name .. ": samples match ffmpeg")
|
||||
local dec = Au.open(reader(c.data)) :: Types.Decoder
|
||||
local got, ok = 0, true
|
||||
while true do
|
||||
local b, k = dec:read(1000)
|
||||
if k == 0 then
|
||||
break
|
||||
end
|
||||
ok = ok and maxDiff(b, 0, ref, got * c.channels * 4, k * c.channels) < 1e-6
|
||||
got += k
|
||||
end
|
||||
t:check(ok and got == c.frames, c.name .. ": streamed reads")
|
||||
dec:seek(700 / c.rate)
|
||||
local b, k = dec:read(40)
|
||||
t:check(k == 40 and maxDiff(b, 0, ref, 700 * c.channels * 4, 40 * c.channels) < 1e-6, c.name .. ": seek")
|
||||
end
|
||||
t:note("%d reference files match ffmpeg", #Cases)
|
||||
|
||||
do
|
||||
local a = Au.decode(reader(case("sox-comment").data)) :: Types.Audio
|
||||
t:check(a.tags.comment ~= nil and string.find(a.tags.comment, "hello sun", 1, true) ~= nil, "annotation as comment")
|
||||
-- an unknown data size (0xFFFFFFFF) runs to the end of the file
|
||||
local c = case("ffmpeg-s16be")
|
||||
local unk = string.sub(c.data, 1, 8) .. "\255\255\255\255" .. string.sub(c.data, 13)
|
||||
local a2 = Au.decode(reader(unk)) :: Types.Audio
|
||||
t:check(a2 and a2.frames == c.frames, "unknown data size")
|
||||
local cut = Au.decode(reader(string.sub(c.data, 1, #c.data - 400))) :: Types.Audio
|
||||
t:check(cut and cut.frames == c.frames - 100 and cut.meta.source.truncated, "truncated data")
|
||||
local big = buffer.create(#c.data + 3)
|
||||
buffer.writestring(big, 3, c.data)
|
||||
local a3 = Au.decode(Bytes.reader(big, 3, #c.data)) :: Types.Audio
|
||||
t:check(a3 and maxDiff(a3.samples, 0, buffer.fromstring(c.ref), 0, c.frames * 2) < 1e-6, "decode from a sub-range")
|
||||
end
|
||||
|
||||
local TOL = { s8 = 0.5 / 128, ["s16-mono"] = 0.5 / 32768, s16 = 0.5 / 32768, s24 = 0.5 / 8388608, s32 = 1e-7, f32 = 0, f64 = 0, ulaw = 0.04, alaw = 0.04 }
|
||||
local sig = buffer.fromstring(Sig.data)
|
||||
for _, e in Enc do
|
||||
t:eq(e.ffmpegErrors, "", e.name .. ": ffmpeg reads our file without errors")
|
||||
t:check(e.frames == Sig.frames and e.rate == Sig.rate, e.name .. ": ffmpeg frames / rate")
|
||||
t:check(e.maxDiff <= TOL[e.name] + 1e-7, string.format("%s: ffmpeg decode within %g (max diff %g)", e.name, TOL[e.name], e.maxDiff))
|
||||
t:check(e.sameStreamed, e.name .. ": encoding a Decoder gives the same bytes")
|
||||
local back = Au.decode(reader(e.data)) :: Types.Audio
|
||||
t:check(back and back.frames == Sig.frames and back.channels == e.channels, e.name .. ": our decoder reads our file")
|
||||
if e.channels == 2 then
|
||||
t:check(maxDiff(back.samples, 0, sig, 0, Sig.frames * 2) <= TOL[e.name] + 1e-7, e.name .. ": round trip")
|
||||
end
|
||||
end
|
||||
t:note("%d encoder outputs read back by ffmpeg and by us", #Enc)
|
||||
|
||||
do
|
||||
local a = Types.Audio.new(8000, 1, 2)
|
||||
buffer.writef32(a.samples, 0, 0.25)
|
||||
buffer.writef32(a.samples, 4, -0.25)
|
||||
a.tags = { comment = "annotated!" }
|
||||
local b = Au.encode(a) :: buffer
|
||||
t:eq(bit32.byteswap(buffer.readu32(b, 4)) % 8, 0, "data offset is a multiple of 8")
|
||||
local back = Au.decode(Bytes.reader(b)) :: Types.Audio
|
||||
t:check(back.frames == 2 and back.tags.comment == "annotated!" and buffer.readf32(back.samples, 0) == 0.25, "comment + samples")
|
||||
local _, e1 = Au.encode(42 :: any)
|
||||
t:check(e1 and e1.code == Err.ARG, "encode junk: ARG")
|
||||
local _, e2 = Au.encode(a, { sampleFormat = "u8" } :: any)
|
||||
t:check(e2 and e2.code == Err.ARG, "bad sampleFormat: ARG")
|
||||
local frac = Types.Audio.new(8000.5, 1, 1)
|
||||
local _, e3 = Au.encode(frac)
|
||||
t:check(e3 and e3.code == Err.ARG, "fractional rate: ARG")
|
||||
t:eq(Au.sniff(Bytes.reader(b)), 1, "sniff")
|
||||
t:eq(Au.sniff(reader("FORM")), 0, "sniff other")
|
||||
-- G.721 ADPCM (encoding 23): unsupported
|
||||
local adpcm = string.sub(buffer.tostring(b), 1, 12) .. "\0\0\0\23" .. string.sub(buffer.tostring(b), 17)
|
||||
local _, e4 = Au.decode(reader(adpcm))
|
||||
t:check(e4 and e4.code == Err.UNSUPPORTED, "encoding 23: UNSUPPORTED")
|
||||
local _, e5 = Au.decode(reader(".snd\0\0\0\24"))
|
||||
t:check(e5 and e5.code == Err.CORRUPT, "short header: CORRUPT")
|
||||
local _, e6 = Au.decode(reader(case("ffmpeg-s16be").data), { maxFrames = 5 })
|
||||
t:check(e6 and e6.code == Err.LIMIT, "maxFrames: LIMIT")
|
||||
end
|
||||
|
||||
do
|
||||
local samples = {}
|
||||
for i, c in Cases do
|
||||
if i % 2 == 1 then
|
||||
table.insert(samples, string.sub(c.data, 1, 400))
|
||||
end
|
||||
end
|
||||
t:fuzz("decode", function(b: buffer)
|
||||
return Au.decode(Bytes.reader(b))
|
||||
end, samples, { mutations = 30 })
|
||||
t:fuzz("open + read", function(b: buffer)
|
||||
local d = Au.open(Bytes.reader(b))
|
||||
if d then
|
||||
d:read(64)
|
||||
d:seek(1e9)
|
||||
d:read(64)
|
||||
d:seek(-1)
|
||||
d:read(64)
|
||||
end
|
||||
return d
|
||||
end, samples, { mutations = 10, garbage = 50 })
|
||||
end
|
||||
|
||||
t:done()
|
||||
@@ -22,7 +22,7 @@ for _, c in Cases do
|
||||
end
|
||||
|
||||
t:eq(Flac.name, "flac", "name")
|
||||
t:check(Flac.caps.decode and not Flac.caps.encode, "caps")
|
||||
t:check(Flac.caps.decode and Flac.caps.encode, "caps") -- encode: ws-audio (tests/audio/flac_encode.luau)
|
||||
|
||||
local seekChecks = 0
|
||||
for _, fx in Cases do
|
||||
@@ -156,8 +156,8 @@ do
|
||||
t:check(a and a.frames == fx.frames, "F.audio.load")
|
||||
local d = F.audio.open(byName.s16_l3.data)
|
||||
t:check(d and d.frames == byName.s16_l3.frames, "F.audio.open")
|
||||
local _, se = F.save(a, nil, { format = "flac" })
|
||||
t:check(se and se.code == Err.UNSUPPORTED, "F.save flac is read-only here: " .. tostring(se))
|
||||
local saved, se = F.save(a, nil, { format = "flac" }) -- the encoder (ws-audio)
|
||||
t:check(saved and F.audio.load(saved).frames == fx.frames, "F.save flac: " .. tostring(se))
|
||||
end
|
||||
|
||||
-- a damaged frame is dropped, the rest decodes
|
||||
|
||||
@@ -0,0 +1,109 @@
|
||||
-- audio/flac encoder tests: every fixture-step encode (levels 0-8, 1 / 2 / 6 channels, 8 / 12 / 16 / 20 / 24 bits,
|
||||
-- odd lengths and block sizes, odd rates, seek tables, padding) passed `flac -t` and carries the MD5 of the quantized
|
||||
-- input; sizes within 10% of libFLAC at the same level; each decodes bit-exactly through our own decoder; streaming
|
||||
-- from a Decoder gives the same bytes; tags; option errors. Fixtures: tools/fixtures/flac_enc.py.
|
||||
local T = require("@tests/T")
|
||||
local Flac = require("@formats/audio/flac")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Err = require("@formats/core/Err")
|
||||
local Types = require("@formats/Types")
|
||||
local Pcm = require("@formats/codecs/Pcm")
|
||||
local Enc = require("@tests/gen/flac_enc")
|
||||
local Sigs = require("@tests/gen/flac_enc_sigs")
|
||||
|
||||
local t = T.suite("flac_encode")
|
||||
|
||||
local sigCache: { [string]: any } = {}
|
||||
local function sig(name: string): Types.Audio
|
||||
local s = sigCache[name]
|
||||
if not s then
|
||||
local m = Sigs[name]
|
||||
s = Types.Audio.new(m.rate, m.channels, m.frames)
|
||||
buffer.writestring(s.samples, 0, m.data)
|
||||
sigCache[name] = s
|
||||
end
|
||||
return s
|
||||
end
|
||||
|
||||
t:check(Flac.caps.encode and Flac.encode ~= nil, "caps.encode")
|
||||
|
||||
local worst = 0
|
||||
for _, e in Enc do
|
||||
t:eq(e.flacTest, "", e.name .. ": flac -t")
|
||||
t:eq(e.md5, e.wantMd5, e.name .. ": STREAMINFO MD5 = MD5 of the quantized input")
|
||||
if e.refSize then
|
||||
local ratio = e.size / e.refSize
|
||||
worst = math.max(worst, ratio)
|
||||
t:check(ratio < 1.1, string.format("%s: %d bytes, libFLAC %d (%.3f)", e.name, e.size, e.refSize, ratio))
|
||||
end
|
||||
-- our decoder: bit-exact against the input quantized to e.bits
|
||||
local a = sig(e.sig)
|
||||
local back, err = Flac.decode(Bytes.reader(buffer.fromstring(e.data)))
|
||||
t:check(back, e.name .. ": our decoder: " .. tostring(err))
|
||||
t:check(back.frames == a.frames and back.channels == a.channels and back.sampleRate == a.sampleRate, e.name .. ": format")
|
||||
local s = 2 ^ (e.bits - 1)
|
||||
local exact = true
|
||||
for i = 0, a.frames * a.channels - 1 do
|
||||
local want = math.clamp(math.floor(buffer.readf32(a.samples, i * 4) * s + 0.5), -s, s - 1)
|
||||
if math.floor(buffer.readf32(back.samples, i * 4) * s + 0.5) ~= want then
|
||||
exact = false
|
||||
break
|
||||
end
|
||||
end
|
||||
t:check(exact, e.name .. ": bit-exact round trip")
|
||||
t:eq(back.meta.source.bitDepth, e.bits, e.name .. ": bits per sample")
|
||||
end
|
||||
t:note("%d encodes pass flac -t with the right MD5 and decode bit-exactly; worst size vs libFLAC %.3f", #Enc, worst)
|
||||
|
||||
-- streaming: a Decoder source gives the same bytes as the Audio
|
||||
do
|
||||
local a = sig("flacsig_music_2_7000_44100")
|
||||
local f1 = Flac.encode(a, { level = 5 }) :: buffer
|
||||
local f2 = Flac.encode(Pcm.audioDecoder(a), { level = 5 }) :: buffer
|
||||
t:check(buffer.tostring(f1) == buffer.tostring(f2), "Decoder source = Audio source")
|
||||
-- a decoder of our own output re-encodes to the same stream (its bit depth carries over)
|
||||
local dec = Flac.open(Bytes.reader(f1)) :: any
|
||||
local f3 = Flac.encode(dec, { level = 5 }) :: buffer
|
||||
t:check(buffer.tostring(f3) == buffer.tostring(f1), "FLAC -> FLAC through the streaming decoder")
|
||||
end
|
||||
|
||||
-- tags, bit depth from the source
|
||||
do
|
||||
local a = Types.Audio.new(48000, 1, 3000)
|
||||
for i = 0, 2999 do
|
||||
buffer.writef32(a.samples, i * 4, math.floor(math.sin(i / 7) * 100) / 128)
|
||||
end
|
||||
a.tags = { title = "Tïtle", artist = "Artist", track = "3" }
|
||||
a.meta.source = { format = "wav", bitDepth = 8 }
|
||||
local f = Flac.encode(a) :: buffer
|
||||
local back = Flac.decode(Bytes.reader(f)) :: Types.Audio
|
||||
t:eq(back.meta.source.bitDepth, 8, "bit depth from meta.source")
|
||||
-- (the absorbed FLAC decoder keeps Vorbis comment keys upper case)
|
||||
local tags = back.tags :: any
|
||||
t:eq(tags.title or tags.TITLE, "Tïtle", "title")
|
||||
t:eq(tags.artist or tags.ARTIST, "Artist", "artist")
|
||||
t:eq(tags.track or tags.TRACKNUMBER, "3", "track number")
|
||||
local empty = Flac.encode(Types.Audio.new(44100, 2, 0)) :: buffer
|
||||
local e0 = Flac.decode(Bytes.reader(empty))
|
||||
t:check(e0 and e0.frames == 0, "empty audio")
|
||||
end
|
||||
|
||||
-- errors
|
||||
do
|
||||
local a = Types.Audio.new(44100, 2, 10)
|
||||
local function code(v: any, o: any): string?
|
||||
local _, e = Flac.encode(v, o)
|
||||
return e and e.code
|
||||
end
|
||||
t:eq(code({}, nil), Err.ARG, "junk value")
|
||||
t:eq(code(a, { level = 9 }), Err.ARG, "level 9")
|
||||
t:eq(code(a, { level = 2.5 }), Err.ARG, "level 2.5")
|
||||
t:eq(code(a, { bits = 32 }), Err.ARG, "32 bits")
|
||||
t:eq(code(a, { bits = 3 }), Err.ARG, "3 bits")
|
||||
t:eq(code(a, { blockSize = 8 }), Err.ARG, "block size 8")
|
||||
t:eq(code(Types.Audio.new(44100, 9, 1), nil), Err.ARG, "9 channels")
|
||||
t:eq(code(Types.Audio.new(44100.5, 1, 1), nil), Err.ARG, "fractional rate")
|
||||
end
|
||||
|
||||
-- (the decoder's fuzz pass is in tests/audio/flac.luau; the encoder takes no untrusted bytes)
|
||||
t:done()
|
||||
@@ -82,7 +82,7 @@ local rand = T.rng(12345)
|
||||
t:eq(Mp3.name, "mp3", "name")
|
||||
t:eq(Mp3.category, "audio", "category")
|
||||
t:check(table.find(Mp3.extensions, "mp3") ~= nil and table.find(Mp3.extensions, "mp2") ~= nil, "extensions")
|
||||
t:check(Mp3.caps.decode and not Mp3.caps.encode and Mp3.open ~= nil, "caps")
|
||||
t:check(Mp3.caps.decode and Mp3.caps.encode and Mp3.open ~= nil, "caps") -- encode: ws-audio (tests/audio/mp3_encode.luau)
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ decode vs references
|
||||
for _, c in Cases do
|
||||
@@ -487,8 +487,8 @@ do
|
||||
t:check(a and a.kind == "audio" and a.frames == 22050 and a.tags.title == "Test Title", "F.audio.load: " .. tostring(err))
|
||||
local dec = F.audio.open(data)
|
||||
t:check(dec and dec.sampleRate == 44100 and dec.frames == 22050, "F.audio.open")
|
||||
local _, serr = F.save(a, nil, { format = "mp3" })
|
||||
t:check(serr and serr.code == Err.UNSUPPORTED, "F.save mp3: read-only")
|
||||
local saved, serr = F.save(a, nil, { format = "mp3" }) -- the layer III encoder (ws-audio)
|
||||
t:check(saved and F.audio.load(saved).sampleRate == 44100, "F.save mp3: " .. tostring(serr))
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ fuzz
|
||||
|
||||
@@ -0,0 +1,143 @@
|
||||
-- audio/mp3 encoder tests (layer III and layer II): every fixture-step encode (sample rates of MPEG-1 / 2 / 2.5,
|
||||
-- bit rates 24 .. 384 kbps, stereo / joint / mono) decodes in ffmpeg without errors and in LAME, with the expected
|
||||
-- length and an SNR over a per-bit-rate floor after aligning the decoder delay; our own decoder (the minimp3 port)
|
||||
-- agrees; streaming from a Decoder gives the same bytes; the ID3v2 tag round-trips; option errors.
|
||||
-- Fixtures: tools/fixtures/mp3_enc.py.
|
||||
local T = require("@tests/T")
|
||||
local Mp3 = require("@formats/audio/mp3")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Err = require("@formats/core/Err")
|
||||
local Types = require("@formats/Types")
|
||||
local Pcm = require("@formats/codecs/Pcm")
|
||||
local Enc = require("@tests/gen/mp3_enc")
|
||||
local Sigs = require("@tests/gen/mp3_enc_sigs")
|
||||
|
||||
local t = T.suite("mp3_encode")
|
||||
|
||||
local function audio(name: string): Types.Audio
|
||||
local s = Sigs[name]
|
||||
local a = Types.Audio.new(s.rate, s.channels, s.frames)
|
||||
buffer.writestring(a.samples, 0, s.data)
|
||||
return a
|
||||
end
|
||||
|
||||
-- SNR of y (delayed by lag frames) against x over frames [from, to)
|
||||
local function snr(x: Types.Audio, y: Types.Audio, lag: number, from: number, to: number): number
|
||||
local ch = x.channels
|
||||
local sig, er = 0, 0
|
||||
for i = from, to - 1 do
|
||||
local j = i + lag
|
||||
if j >= 0 and j < y.frames then
|
||||
for c = 0, ch - 1 do
|
||||
local a = buffer.readf32(x.samples, (i * ch + c) * 4)
|
||||
local b = buffer.readf32(y.samples, (j * y.channels + math.min(c, y.channels - 1)) * 4)
|
||||
sig += a * a
|
||||
er += (a - b) * (a - b)
|
||||
end
|
||||
end
|
||||
end
|
||||
if er == 0 then
|
||||
return 200
|
||||
end
|
||||
return if sig > 0 then 10 * math.log10(sig / er) else -200
|
||||
end
|
||||
|
||||
-- the SNR floors (dB) on the music / sine / sweep signals, by layer and bit rate per channel
|
||||
local function floorFor(e: any): number
|
||||
if string.find(e.name, "silence", 1, true) then
|
||||
return -math.huge
|
||||
elseif string.find(e.name, "noise", 1, true) then
|
||||
return 8 -- white noise has no spectral structure to exploit
|
||||
end
|
||||
local perCh = e.bitrate / (if e.mode == "mono" then 1 else 2)
|
||||
local nyquistBand = e.rate <= 16000 -- low rates lose little to the encoder's band limit
|
||||
if e.layer == 3 then
|
||||
if perCh >= 64 then
|
||||
return 14
|
||||
elseif perCh >= 32 or nyquistBand then
|
||||
return 8
|
||||
end
|
||||
return 4
|
||||
end
|
||||
if perCh >= 96 then
|
||||
return 14
|
||||
elseif perCh >= 48 then
|
||||
return 8
|
||||
end
|
||||
return 4
|
||||
end
|
||||
|
||||
t:check(Mp3.caps.encode and Mp3.encode ~= nil, "caps.encode")
|
||||
|
||||
local minByLayer = { [2] = math.huge, [3] = math.huge }
|
||||
for _, e in Enc do
|
||||
t:eq(e.ffErrors, "", e.name .. ": ffmpeg decodes without errors")
|
||||
t:check(e.ffRate == e.rate, e.name .. ": rate")
|
||||
t:check(e.ffChannels == (if e.mode == "mono" then 1 else 2), e.name .. ": channels")
|
||||
-- the stream covers the input: the encoder adds one frame of flush and the filterbank delay
|
||||
local spf = if e.layer == 3 and e.rate < 32000 then 576 else 1152
|
||||
t:check(e.ffFrames >= e.frames and e.ffFrames <= e.frames + 3 * spf, string.format("%s: %d decoded frames for %d", e.name, e.ffFrames, e.frames))
|
||||
local f = floorFor(e)
|
||||
t:check(e.ffSnr >= f, string.format("%s: ffmpeg SNR %.1f dB (floor %g)", e.name, e.ffSnr, f))
|
||||
if e.lameSnr then
|
||||
t:check(e.lameSnr >= f, string.format("%s: LAME SNR %.1f dB (floor %g)", e.name, e.lameSnr, f))
|
||||
end
|
||||
if f > -math.huge then
|
||||
minByLayer[e.layer] = math.min(minByLayer[e.layer], e.ffSnr)
|
||||
end
|
||||
-- our decoder agrees with ffmpeg
|
||||
local x = audio(e.sig)
|
||||
local y, err = Mp3.decode(Bytes.reader(buffer.fromstring(e.data)))
|
||||
t:check(y, e.name .. ": our decoder: " .. tostring(err))
|
||||
if e.channels == y.channels then
|
||||
local s = snr(x, y, e.ffLag, 200, x.frames)
|
||||
t:check(s >= f and math.abs(s - e.ffSnr) < 3, string.format("%s: our decoder %.1f dB, ffmpeg %.1f dB", e.name, s, e.ffSnr))
|
||||
end
|
||||
end
|
||||
t:note("%d encodes: ffmpeg + LAME decode them; lowest SNR layer III %.1f dB, layer II %.1f dB", #Enc, minByLayer[3], minByLayer[2])
|
||||
|
||||
-- streaming, tags, defaults
|
||||
do
|
||||
local a = audio("music_2_44100")
|
||||
local f1 = Mp3.encode(a, { bitrate = 128 }) :: buffer
|
||||
local f2 = Mp3.encode(Pcm.audioDecoder(a), { bitrate = 128 }) :: buffer
|
||||
t:check(buffer.tostring(f1) == buffer.tostring(f2), "Decoder source = Audio source")
|
||||
a.tags = { title = "Ünïcode title", artist = "An Artist", album = "Album", track = "4", comment = "made here" }
|
||||
local f3 = Mp3.encode(a, { layer = 2 }) :: buffer
|
||||
t:eq(buffer.readstring(f3, 0, 3), "ID3", "an ID3v2 tag in front")
|
||||
local back = Mp3.open(Bytes.reader(f3)) :: any
|
||||
t:eq(back.tags.title, "Ünïcode title", "title")
|
||||
t:eq(back.tags.artist, "An Artist", "artist")
|
||||
t:eq(back.tags.album, "Album", "album")
|
||||
t:eq(back.tags.track, "4", "track")
|
||||
t:eq(back.layer, 2, "layer II by default bit rate 192")
|
||||
local f4 = Mp3.encode(a, { id3 = false }) :: buffer
|
||||
t:check(buffer.readu8(f4, 0) == 0xFF, "id3 = false")
|
||||
local m = Types.Audio.new(22050, 1, 3000)
|
||||
local fm = Mp3.encode(m) :: buffer
|
||||
local dm = Mp3.open(Bytes.reader(fm)) :: any
|
||||
t:check(dm.channels == 1 and dm.sampleRate == 22050, "mono MPEG-2 default")
|
||||
local empty = Mp3.encode(Types.Audio.new(44100, 2, 0)) :: buffer
|
||||
t:check(empty and buffer.len(empty) > 0, "empty input: one flush frame")
|
||||
end
|
||||
|
||||
-- errors
|
||||
do
|
||||
local function code(v: any, o: any): string?
|
||||
local _, e = Mp3.encode(v, o)
|
||||
return e and e.code
|
||||
end
|
||||
local a = Types.Audio.new(44100, 2, 100)
|
||||
t:eq(code({}, nil), Err.ARG, "junk")
|
||||
t:eq(code(a, { layer = 1 }), Err.ARG, "layer I")
|
||||
t:eq(code(a, { bitrate = 100 }), Err.ARG, "100 kbps")
|
||||
t:eq(code(a, { mode = "dual" }), Err.ARG, "dual mode")
|
||||
t:eq(code(Types.Audio.new(44000, 2, 10), nil), Err.ARG, "44 kHz")
|
||||
t:eq(code(Types.Audio.new(8000, 2, 10), { layer = 2 }), Err.ARG, "layer II at 8 kHz")
|
||||
t:eq(code(Types.Audio.new(44100, 3, 10), nil), Err.ARG, "3 channels")
|
||||
t:eq(code(a, { layer = 2, bitrate = 32 }), Err.ARG, "layer II stereo at 32 kbps")
|
||||
t:eq(code(Types.Audio.new(44100, 1, 10), { layer = 2, bitrate = 320 }), Err.ARG, "layer II mono at 320 kbps")
|
||||
end
|
||||
|
||||
-- (the decoder's fuzz pass is in tests/audio/mp3.luau; the encoders take no untrusted bytes)
|
||||
t:done()
|
||||
@@ -0,0 +1,129 @@
|
||||
-- audio/voc tests: files written by ffmpeg (type 1 + 8 blocks for 8-bit, type 9 otherwise) and sox against ffmpeg's
|
||||
-- decode; silence / text / continuation blocks and repeat markers built here; streaming reads and seeks; our
|
||||
-- encoder's output for every sample format decoded by ffmpeg (fixture step) and by us; errors; fuzz.
|
||||
-- Fixtures: tools/fixtures/voc.py.
|
||||
local T = require("@tests/T")
|
||||
local Voc = require("@formats/audio/voc")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Err = require("@formats/core/Err")
|
||||
local Types = require("@formats/Types")
|
||||
local Cases = require("@tests/gen/voc_cases")
|
||||
local Enc = require("@tests/gen/voc_enc")
|
||||
local Sig = require("@tests/gen/voc_sig")
|
||||
|
||||
local t = T.suite("voc")
|
||||
|
||||
local function reader(s: string): Bytes.Reader
|
||||
return Bytes.reader(buffer.fromstring(s))
|
||||
end
|
||||
local function maxDiff(a: buffer, ao: number, b: buffer, bo: number, n: number): number
|
||||
local m = 0
|
||||
for i = 0, n - 1 do
|
||||
local d = math.abs(buffer.readf32(a, ao + i * 4) - buffer.readf32(b, bo + i * 4))
|
||||
if d > m then
|
||||
m = d
|
||||
end
|
||||
end
|
||||
return m
|
||||
end
|
||||
|
||||
t:eq(Voc.name, "voc", "name")
|
||||
t:eq(Voc.category, "audio", "category")
|
||||
t:check(Voc.caps.decode and Voc.caps.encode, "caps")
|
||||
|
||||
for _, c in Cases do
|
||||
local a, err = Voc.decode(reader(c.data))
|
||||
t:check(a, c.name .. ": " .. tostring(err))
|
||||
t:check(a.sampleRate == c.rate and a.channels == c.channels, string.format("%s: format %d Hz x %d, want %d x %d", c.name, a.sampleRate, a.channels, c.rate, c.channels))
|
||||
t:eq(a.frames, c.frames, c.name .. ": frames")
|
||||
local ref = buffer.fromstring(c.ref)
|
||||
t:check(maxDiff(a.samples, 0, ref, 0, c.frames * c.channels) < 1e-6, c.name .. ": samples match ffmpeg")
|
||||
local dec = Voc.open(reader(c.data)) :: Types.Decoder
|
||||
dec:seek(500 / c.rate)
|
||||
local b, k = dec:read(64)
|
||||
t:check(k == 64 and maxDiff(b, 0, ref, 500 * c.channels * 4, 64 * c.channels) < 1e-6, c.name .. ": seek")
|
||||
end
|
||||
t:note("%d reference files match ffmpeg", #Cases)
|
||||
|
||||
-- hand-built blocks: text, type 1 u8, silence, continuation, repeat markers, terminator
|
||||
do
|
||||
local hdr = "Creative Voice File\26\26\0\10\1\41\17"
|
||||
local function blk(typ: number, payload: string): string
|
||||
local n = #payload
|
||||
return string.char(typ, n % 256, (n // 256) % 256, n // 65536) .. payload
|
||||
end
|
||||
local data = hdr
|
||||
.. blk(5, "made by hand\0")
|
||||
.. blk(1, string.char(156, 0) .. string.char(128, 192, 64)) -- 10 kHz, 3 samples
|
||||
.. blk(3, string.char(4, 0, 156)) -- 5 frames of silence
|
||||
.. blk(6, string.char(2, 0)) -- repeat start (ignored)
|
||||
.. blk(2, string.char(255, 0)) -- continuation
|
||||
.. blk(7, "") -- repeat end
|
||||
.. blk(4, string.char(1, 0)) -- marker
|
||||
.. "\0"
|
||||
local a, err = Voc.decode(reader(data))
|
||||
t:check(a, "hand-built: " .. tostring(err))
|
||||
t:check(a.sampleRate == 10000 and a.channels == 1 and a.frames == 10, "hand-built: 10 kHz, 3 + 5 + 2 frames")
|
||||
t:eq(a.tags.comment, "made by hand", "text block")
|
||||
local want = { 0, 0.5, -0.5, 0, 0, 0, 0, 0, 127 / 128, -1 }
|
||||
local ok = true
|
||||
for i, v in want do
|
||||
ok = ok and buffer.readf32(a.samples, (i - 1) * 4) == v
|
||||
end
|
||||
t:check(ok, "hand-built samples")
|
||||
-- no terminator, a block cut short: the data present
|
||||
local cut = Voc.decode(reader(string.sub(data, 1, 26 + 17 + 4 + 2 + 2)))
|
||||
t:check(cut and cut.frames == 2 and cut.meta.source.truncated, "cut in a block")
|
||||
-- Creative ADPCM: unsupported
|
||||
local _, e = Voc.decode(reader(hdr .. blk(1, string.char(156, 1) .. "\0\0") .. "\0"))
|
||||
t:check(e and e.code == Err.UNSUPPORTED, "ADPCM: UNSUPPORTED")
|
||||
local _, e2 = Voc.decode(reader(hdr .. blk(2, "\0\0") .. "\0"))
|
||||
t:check(e2 and e2.code == Err.CORRUPT, "continuation first: CORRUPT")
|
||||
local _, e3 = Voc.decode(reader(hdr .. "\0"))
|
||||
t:check(e3 and e3.code == Err.CORRUPT, "no sound: CORRUPT")
|
||||
local _, e4 = Voc.decode(reader("Creative Voice Fil"))
|
||||
t:check(e4 and e4.code == Err.CORRUPT, "short: CORRUPT")
|
||||
t:eq(Voc.sniff(reader(data)), 1, "sniff")
|
||||
t:eq(Voc.sniff(reader(".snd")), 0, "sniff other")
|
||||
end
|
||||
|
||||
local TOL = { s16 = 0.5 / 32768, ["s16-mono"] = 0.5 / 32768, u8 = 0.5 / 128, ulaw = 0.04, alaw = 0.04 }
|
||||
local sig = buffer.fromstring(Sig.data)
|
||||
for _, e in Enc do
|
||||
t:eq(e.ffmpegErrors, "", e.name .. ": ffmpeg reads our file without errors")
|
||||
t:check(e.frames == Sig.frames and e.rate == Sig.rate, e.name .. ": ffmpeg frames / rate")
|
||||
t:check(e.maxDiff <= TOL[e.name] + 1e-7, string.format("%s: ffmpeg decode within %g (max diff %g)", e.name, TOL[e.name], e.maxDiff))
|
||||
t:check(e.sameStreamed, e.name .. ": encoding a Decoder gives the same bytes")
|
||||
local back = Voc.decode(reader(e.data)) :: Types.Audio
|
||||
t:check(back and back.frames == Sig.frames and back.channels == e.channels, e.name .. ": our decoder reads our file")
|
||||
if e.channels == 2 then
|
||||
t:check(maxDiff(back.samples, 0, sig, 0, Sig.frames * 2) <= TOL[e.name] + 1e-7, e.name .. ": round trip")
|
||||
end
|
||||
end
|
||||
t:note("%d encoder outputs read back by ffmpeg and by us", #Enc)
|
||||
|
||||
do
|
||||
local a = Types.Audio.new(22050, 2, 1)
|
||||
a.tags = { comment = "hi" }
|
||||
local b = Voc.encode(a) :: buffer
|
||||
local back = Voc.decode(Bytes.reader(b)) :: Types.Audio
|
||||
t:check(back.frames == 1 and back.tags.comment == "hi", "one frame + comment")
|
||||
local _, e1 = Voc.encode(nil :: any)
|
||||
t:check(e1 and e1.code == Err.ARG, "encode nil: ARG")
|
||||
local _, e2 = Voc.encode(a, { sampleFormat = "s24" } :: any)
|
||||
t:check(e2 and e2.code == Err.ARG, "bad sampleFormat: ARG")
|
||||
local _, e3 = Voc.decode(reader(Cases[1].data), { maxFrames = 5 })
|
||||
t:check(e3 and e3.code == Err.LIMIT, "maxFrames: LIMIT")
|
||||
end
|
||||
|
||||
do
|
||||
local samples = {}
|
||||
for _, c in Cases do
|
||||
table.insert(samples, string.sub(c.data, 1, 300))
|
||||
end
|
||||
t:fuzz("decode", function(b: buffer)
|
||||
return Voc.decode(Bytes.reader(b))
|
||||
end, samples, { mutations = 30 })
|
||||
end
|
||||
|
||||
t:done()
|
||||
@@ -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()
|
||||
@@ -0,0 +1,110 @@
|
||||
-- audio/vorbis encoder tests: every fixture-step encode (qualities -1 .. 10, mono / stereo / 5.1, 8 .. 96 kHz, odd
|
||||
-- lengths, silence) passes ogginfo, decodes in oggdec (libvorbisfile) with the exact input length and an SNR over a
|
||||
-- per-quality floor, and in ffmpeg; our own decoder gives the same; the codebooks are valid prefix codes; streaming
|
||||
-- from a Decoder gives the same bytes; tags round-trip; option errors. Fixtures: tools/fixtures/vorbis_enc.py.
|
||||
local T = require("@tests/T")
|
||||
local Vorbis = require("@formats/audio/vorbis")
|
||||
local VEnc = require("@formats/audio/vorbis/Encode")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Err = require("@formats/core/Err")
|
||||
local Types = require("@formats/Types")
|
||||
local Pcm = require("@formats/codecs/Pcm")
|
||||
local Enc = require("@tests/gen/vorbis_enc")
|
||||
local Sigs = require("@tests/gen/vorbis_enc_sigs")
|
||||
|
||||
local t = T.suite("vorbis_encode")
|
||||
|
||||
local function audio(name: string): Types.Audio
|
||||
local s = Sigs[name]
|
||||
local a = Types.Audio.new(s.rate, s.channels, s.frames)
|
||||
buffer.writestring(a.samples, 0, s.data)
|
||||
return a
|
||||
end
|
||||
local function snr(x: Types.Audio, y: Types.Audio): number
|
||||
local sig, er = 0, 0
|
||||
for i = 0, math.min(x.frames, y.frames) * x.channels - 1 do
|
||||
local a = buffer.readf32(x.samples, i * 4)
|
||||
local d = a - buffer.readf32(y.samples, i * 4)
|
||||
sig += a * a
|
||||
er += d * d
|
||||
end
|
||||
if er == 0 then
|
||||
return 200
|
||||
end
|
||||
return if sig > 0 then 10 * math.log10(sig / er) else -200
|
||||
end
|
||||
local function floorFor(q: number): number
|
||||
return 8 + 1.2 * math.max(q, 0)
|
||||
end
|
||||
|
||||
t:check(Vorbis.caps.encode and Vorbis.encode ~= nil, "caps.encode")
|
||||
|
||||
-- the codebooks: Kraft sums of exactly 1 (complete prefix codes) and lengths 1 .. 24
|
||||
for i, b in VEnc.BOOKS do
|
||||
local k, okLen = 0, true
|
||||
for _, l in b.lengths do
|
||||
k += 2 ^ -l
|
||||
okLen = okLen and l >= 1 and l <= 24
|
||||
end
|
||||
t:check(math.abs(k - 1) < 1e-9 and okLen, "codebook " .. (i - 1) .. ": complete prefix code")
|
||||
end
|
||||
|
||||
local minSnr = math.huge
|
||||
for _, e in Enc do
|
||||
t:eq(e.ogginfo, "", e.name .. ": ogginfo reports no warnings / errors")
|
||||
t:eq(e.oggdecFrames, e.frames, e.name .. ": oggdec frames = input frames")
|
||||
t:eq(e.ffErrors, "", e.name .. ": ffmpeg decodes without errors")
|
||||
local silent = string.find(e.name, "silence", 1, true) ~= nil
|
||||
local f = if silent then -math.huge else floorFor(e.quality)
|
||||
t:check(e.oggdecSnr >= f, string.format("%s: oggdec SNR %.1f dB (floor %.1f)", e.name, e.oggdecSnr, f))
|
||||
if e.frames >= 2048 then
|
||||
-- (ffmpeg does not trim the end of a stream that fits on one Ogg page: up to a block more)
|
||||
t:check(e.ffFrames >= e.frames and e.ffFrames <= e.frames + 1024, string.format("%s: ffmpeg frames %d", e.name, e.ffFrames))
|
||||
t:check(e.ffSnr >= f, string.format("%s: ffmpeg SNR %.1f dB", e.name, e.ffSnr))
|
||||
end
|
||||
if not silent then
|
||||
minSnr = math.min(minSnr, e.oggdecSnr)
|
||||
end
|
||||
-- our decoder: the same length and quality (it is sample-exact with libvorbis)
|
||||
local x = audio(e.sig)
|
||||
local y, err = Vorbis.decode(Bytes.reader(buffer.fromstring(e.data)))
|
||||
t:check(y, e.name .. ": our decoder: " .. tostring(err))
|
||||
t:eq(y.frames, e.frames, e.name .. ": our decoder's frames")
|
||||
local s = snr(x, y)
|
||||
t:check(math.abs(s - e.oggdecSnr) < 0.5 or s >= 100, string.format("%s: our decoder %.1f dB, oggdec %.1f dB", e.name, s, e.oggdecSnr))
|
||||
end
|
||||
t:note("%d encodes pass ogginfo / oggdec / ffmpeg; lowest SNR %.1f dB", #Enc, minSnr)
|
||||
|
||||
-- streaming, tags
|
||||
do
|
||||
local a = audio("music_2_3000_44100")
|
||||
local f1 = Vorbis.encode(a) :: buffer
|
||||
local f2 = Vorbis.encode(Pcm.audioDecoder(a)) :: buffer
|
||||
t:check(buffer.tostring(f1) == buffer.tostring(f2), "Decoder source = Audio source")
|
||||
a.tags = { title = "Tïtle", artist = "Artist", track = "9" }
|
||||
local f3 = Vorbis.encode(a, { quality = 1, serial = 77 }) :: buffer
|
||||
local d = Vorbis.open(Bytes.reader(f3)) :: any
|
||||
t:eq(d.tags.title, "Tïtle", "title tag")
|
||||
t:eq(d.tags.track, "9", "track tag")
|
||||
t:check(string.find(d.vendor, "formats-luau", 1, true) ~= nil, "vendor")
|
||||
local empty = Vorbis.encode(Types.Audio.new(44100, 1, 0)) :: buffer
|
||||
local e0 = Vorbis.decode(Bytes.reader(empty))
|
||||
t:check(e0 and e0.frames == 0, "empty input")
|
||||
end
|
||||
|
||||
-- errors
|
||||
do
|
||||
local a = Types.Audio.new(44100, 2, 10)
|
||||
local function code(v: any, o: any): string?
|
||||
local _, e = Vorbis.encode(v, o)
|
||||
return e and e.code
|
||||
end
|
||||
t:eq(code("x", nil), Err.ARG, "junk")
|
||||
t:eq(code(a, { quality = 11 }), Err.ARG, "quality 11")
|
||||
t:eq(code(a, { quality = -2 }), Err.ARG, "quality -2")
|
||||
t:eq(code(a, { serial = -1 }), Err.ARG, "serial -1")
|
||||
t:eq(code(Types.Audio.new(44100.5, 1, 1), nil), Err.ARG, "fractional rate")
|
||||
end
|
||||
|
||||
-- (the decoder's fuzz pass is in tests/audio/vorbis.luau; the encoder takes no untrusted bytes)
|
||||
t:done()
|
||||
@@ -0,0 +1,30 @@
|
||||
-- core/Md5 tests: the RFC 1321 test suite, incremental updates at every split, sub-ranges, a long input.
|
||||
local T = require("@tests/T")
|
||||
local Md5 = require("@formats/core/Md5")
|
||||
|
||||
local t = T.suite("md5")
|
||||
local VECTORS = {
|
||||
{ "", "d41d8cd98f00b204e9800998ecf8427e" },
|
||||
{ "a", "0cc175b9c0f1b6a831c399e269772661" },
|
||||
{ "abc", "900150983cd24fb0d6963f7d28e17f72" },
|
||||
{ "message digest", "f96b697d7cb7938d525a2f31aaf161d0" },
|
||||
{ "abcdefghijklmnopqrstuvwxyz", "c3fcd3d76192e4007dfb496cca67e13b" },
|
||||
{ "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", "d174ab98d277d9f5a5611c2c9f419d9f" },
|
||||
{ string.rep("1234567890", 8), "57edf4a22be3c955ac49da2e2107b67a" },
|
||||
}
|
||||
for _, v in VECTORS do
|
||||
t:eq(Md5.of(buffer.fromstring(v[1])), v[2], "md5 of " .. string.format("%q", v[1]))
|
||||
end
|
||||
-- incremental: every split point of a 130-byte message, three pieces
|
||||
local msg = string.rep("The quick brown fox jumps over the lazy dog. ", 3)
|
||||
local want = Md5.of(buffer.fromstring(msg))
|
||||
for cut = 0, #msg, 7 do
|
||||
local h = Md5.new()
|
||||
h:update(buffer.fromstring(string.sub(msg, 1, cut)))
|
||||
h:update(buffer.fromstring(string.sub(msg, cut + 1, cut + 20)))
|
||||
h:update(buffer.fromstring(string.sub(msg, cut + 21)))
|
||||
t:eq(Md5.hex(h:digest()), want, "split at " .. cut)
|
||||
end
|
||||
t:eq(Md5.of(buffer.fromstring("xxabcyy"), 2, 3), "900150983cd24fb0d6963f7d28e17f72", "sub-range")
|
||||
t:eq(Md5.of(buffer.fromstring(string.rep("a", 1000000))), "7707d6ae4e027c70eea2a935c2296f21", "a million a's")
|
||||
t:done()
|
||||
Reference in new issue
Block a user