Files

92 lines
3.7 KiB
Luau

-- audio/qoa tests: qoa.h's files bit-exact against ffmpeg and qoa.h; our encoder byte-for-byte equal to qoa_encode.
-- Fixtures: tools/fixtures/qoa.py.
local T = require("@tests/T")
local Qoa = require("@rosetta/audio/qoa")
local Bytes = require("@rosetta/core/Bytes")
local Err = require("@rosetta/core/Err")
local Types = require("@rosetta/Types")
local Pcm = require("@rosetta/codecs/audio/Pcm")
local F = require("@rosetta")
local H = require("@tests/audio/_adpcm")
local Cases = require("@tests/gen/qoa_cases")
local Sigs = {
qoa_sig_stereo_44k = require("@tests/gen/qoa_sig_stereo_44k"),
qoa_sig_mono_22k = require("@tests/gen/qoa_sig_mono_22k"),
qoa_sig_6ch_48k = require("@tests/gen/qoa_sig_6ch_48k"),
qoa_sig_loud_mono = require("@tests/gen/qoa_sig_loud_mono"),
qoa_sig_square_stereo = require("@tests/gen/qoa_sig_square_stereo"),
}
local t = T.suite("qoa")
t:eq(Qoa.name, "qoa", "name")
t:check(Qoa.caps.decode and Qoa.caps.encode, "caps")
local samples = {}
for _, c in Cases do
table.insert(samples, c.data)
t:eq(Qoa.sniff(Bytes.reader(buffer.fromstring(c.data))), 1, c.name .. ": sniff")
t:check(c.ref == c.qoaref, c.name .. ": ffmpeg and qoa.h agree")
H.check(t, Qoa, c)
-- our encoder: the same bytes as qoa.h
local S = Sigs[c.sig]
local a = Types.Audio.new(S.rate, S.channels, S.frames)
buffer.writestring(a.samples, 0, S.data)
local out, err = Qoa.encode(a)
local same = out ~= nil and buffer.tostring(out) == c.data
if not same and out then
local o = buffer.tostring(out)
for i = 1, math.max(#o, #c.data) do
if string.byte(o, i) ~= string.byte(c.data, i) then
t:note("%s: first difference at byte %d of %d / %d", c.name, i - 1, #o, #c.data)
break
end
end
end
t:check(same, c.name .. ": encode identical to qoa.h: " .. tostring(err))
local streamed = Qoa.encode(Pcm.audioDecoder(a))
t:check(streamed and buffer.tostring(streamed) == c.data, c.name .. ": encode from a Decoder")
end
t:note("%d qoa.h files decoded bit-exact (ffmpeg, qoa.h) and re-encoded byte-for-byte", #Cases)
do
local c = Cases[1]
local ref = buffer.fromstring(c.ref)
-- streaming: total 0 in the header
local s = buffer.fromstring(c.data)
buffer.writeu32(s, 4, 0)
local a = Qoa.decode(Bytes.reader(s)) :: any
t:check(a and a.frames == buffer.len(ref) // (2 * c.ch) and H.same(a.samples, ref, 0, a.frames * c.ch), "streaming file")
t:check(a.meta.source.streaming == true, "meta.source.streaming")
-- a cut file keeps its whole frames
local cut = Qoa.decode(Bytes.reader(buffer.fromstring(string.sub(c.data, 1, #c.data - 100)))) :: any
t:check(cut and cut.frames == 5120 * 2 and H.same(cut.samples, ref, 0, cut.frames * c.ch), "cut file: the whole frames")
local _, e1 = Qoa.open(Bytes.reader(buffer.fromstring("qoaf\0\0\0\1")))
t:check(e1 and e1.code == Err.TRUNCATED, "no frames: " .. tostring(e1))
local bad = buffer.fromstring(c.data)
buffer.writeu8(bad, 8, 0) -- 0 channels
local _, e2 = Qoa.open(Bytes.reader(bad))
t:check(e2 and e2.code == Err.CORRUPT, "0 channels: " .. tostring(e2))
local _, e3 = Qoa.encode(Types.Audio.new(8000, 9, 10))
t:check(e3 and e3.code == Err.ARG, "9 channels")
local _, e4 = Qoa.encode(Types.Audio.new(8000, 1, 0))
t:check(e4 and e4.code == Err.ARG, "no samples")
local _, e5 = Qoa.decode(Bytes.reader(buffer.fromstring(c.data)), { maxFrames = 10 })
t:check(e5 and e5.code == Err.LIMIT, "maxFrames")
t:eq(F.detect(c.data), "qoa", "F.detect")
local back = F.save(F.audio.load(c.data), nil, { format = "qoa" })
t:check(back and F.detect(back) == "qoa", "F.save qoa")
end
t:fuzz("open + read + seek", function(b: buffer)
local dec = Qoa.open(Bytes.reader(b))
if dec then
dec:read(100000)
dec:seek(dec.duration / 2)
dec:read(33)
end
return dec
end, samples, { mutations = 10 })
t:done()