139 lines
5.8 KiB
Luau
139 lines
5.8 KiB
Luau
-- audio/mpc tests: Musepack SV8 (mpcenc 1.30) and SV7 (mppenc 1.16) against libmpcdec's float output and ffmpeg.
|
|
-- Fixtures: tools/fixtures/mpc.py.
|
|
local T = require("@tests/T")
|
|
local Mpc = require("@rosetta/audio/mpc")
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local Err = require("@rosetta/core/Err")
|
|
local Fx = require("@tests/gen/mpc_cases")
|
|
local Seek = require("@tests/gen/mpc_seek")
|
|
|
|
local t = T.suite("mpc")
|
|
|
|
local function reader(s: string): Bytes.Reader
|
|
return Bytes.reader(buffer.fromstring(s))
|
|
end
|
|
|
|
local function compare(got: buffer, gotFrom: number, ref: string, ch: number, frames: number): (number, number)
|
|
local rb = buffer.fromstring(ref)
|
|
local n = math.min(frames * ch, #ref // 4)
|
|
local sig, err, maxd = 0, 0, 0
|
|
for i = 0, n - 1 do
|
|
local r = buffer.readf32(rb, i * 4)
|
|
local d = buffer.readf32(got, (gotFrom * ch + i) * 4) - r
|
|
sig += r * r
|
|
err += d * d
|
|
maxd = math.max(maxd, math.abs(d))
|
|
end
|
|
return if err == 0 then math.huge else 10 * math.log10(sig / err), maxd
|
|
end
|
|
|
|
t:eq(Mpc.name, "mpc", "name")
|
|
t:check(Mpc.caps.decode and not Mpc.caps.encode and Mpc.encode == nil, "caps: decode only")
|
|
|
|
local byName = {}
|
|
for _, c in Fx do
|
|
byName[c.name] = c
|
|
local r = reader(c.data)
|
|
t:check(Mpc.sniff(r) >= 0.9, c.name .. ": sniff")
|
|
local a, err = Mpc.decode(r)
|
|
t:check(a, c.name .. ": " .. tostring(err))
|
|
if not a then
|
|
continue
|
|
end
|
|
t:check(a.channels == c.channels and a.sampleRate == c.rate, c.name .. ": format")
|
|
t:eq(a.frames, c.frames, c.name .. ": gapless length")
|
|
t:eq(a.meta.source.version, c.version, c.name .. ": stream version")
|
|
local s, maxd = compare(a.samples, 0, c.ref, c.channels, c.exact)
|
|
t:check(s > 120 and maxd < 1e-5, string.format("%s: SNR %.1f dB, max diff %g vs libmpcdec", c.name, s, maxd))
|
|
local all = compare(a.samples, 0, c.ref, c.channels, c.frames)
|
|
t:check(all > 60, string.format("%s: whole SNR %.1f dB", c.name, all))
|
|
local line = string.format("%s: SV%d %d ch %d Hz, %d frames, SNR %.1f dB (max diff %.2g) vs libmpcdec", c.name, c.version, c.channels, c.rate, a.frames, s, maxd)
|
|
if c.ff then
|
|
local f = compare(a.samples, 0, c.ff, c.channels, c.frames)
|
|
t:check(f > 80, string.format("%s: SNR %.1f dB vs ffmpeg", c.name, f))
|
|
line ..= string.format(", %.1f dB vs ffmpeg (s16)", f)
|
|
end
|
|
t:note("%s", line)
|
|
if c.tags then
|
|
for k, v in c.tags do
|
|
t:eq(a.tags[k], v, c.name .. ": tag " .. k)
|
|
end
|
|
end
|
|
end
|
|
|
|
-- SV8 seeks exactly; SV7 like libmpcdec's seek (32 frames of pre-roll from reset scalefactors)
|
|
for _, sk in Seek do
|
|
local c = byName[sk.name]
|
|
local whole = Mpc.decode(reader(c.data)) :: any
|
|
local dec = Mpc.open(reader(c.data)) :: any
|
|
local _ = dec:read(5000) -- decode a little first: the seek must forget the state
|
|
dec:seek(sk.at / c.rate)
|
|
t:check(math.abs(dec:position() - sk.at / c.rate) < 1e-9, sk.name .. ": position after seek")
|
|
local b, n = dec:read(4096)
|
|
t:eq(n, 4096, sk.name .. ": read after seek")
|
|
if c.version == 8 then
|
|
local _, own = compare(b, 0, string.sub(buffer.tostring(whole.samples), sk.at * 8 + 1, (sk.at + 4096) * 8), 2, 4096)
|
|
t:check(own < 1e-6, string.format("%s: seek to %d is exact (max diff %g)", sk.name, sk.at, own))
|
|
-- libmpcdec seeks without warming the synthesis up: compare after its first frame
|
|
local _, lib = compare(b, 1152, string.sub(sk.ref, 1152 * 8 + 1), 2, 4096 - 1152)
|
|
t:check(lib < 1e-5, string.format("%s: seek matches libmpcdec past 1152 frames (max diff %g)", sk.name, lib))
|
|
else
|
|
local s, lib = compare(b, 0, sk.ref, 2, 4096)
|
|
t:check(lib < 1e-5, string.format("%s: seek to %d matches libmpcdec's (max diff %g)", sk.name, sk.at, lib))
|
|
local own = compare(b, 0, string.sub(buffer.tostring(whole.samples), sk.at * 8 + 1, (sk.at + 4096) * 8), 2, 4096)
|
|
t:note("%s: seek %d: SNR %.1f dB vs the continuous decode (SV7 seeks restart the scalefactors: not always exact)", sk.name, sk.at, own)
|
|
local _ = s
|
|
end
|
|
dec:seek(0)
|
|
local b0, n0 = dec:read(1000)
|
|
local _, d0 = compare(b0, 0, c.ref, 2, n0)
|
|
t:check(n0 == 1000 and d0 < 1e-5, sk.name .. ": seek back to 0")
|
|
end
|
|
|
|
do
|
|
local c = byName.sv8_q5_stereo
|
|
local id3 = "ID3\3\0\0\0\0\0\20" .. string.rep("\0", 20)
|
|
local a = Mpc.decode(reader(id3 .. c.data)) :: any
|
|
t:check(a and a.frames == c.frames, "ID3v2 in front")
|
|
t:check(Mpc.sniff(reader(id3 .. c.data)) == 1, "ID3v2 in front: sniff")
|
|
for _, name in { "sv8_q5_stereo", "sv7_q5_stereo" } do
|
|
local d = byName[name]
|
|
local cut = Mpc.decode(reader(string.sub(d.data, 1, #d.data * 2 // 3))) :: any
|
|
t:check(cut and cut.frames > 0 and cut.frames < d.frames, name .. ": truncated file decodes its start")
|
|
if cut then
|
|
local _, m = compare(cut.samples, 0, d.ref, 2, math.min(cut.frames - 1152, d.exact))
|
|
t:check(m < 1e-5, name .. ": truncated file: same start")
|
|
end
|
|
end
|
|
end
|
|
|
|
do
|
|
local c8, c7 = byName.sv8_q5_stereo.data, byName.sv7_q5_stereo.data
|
|
t:eq(Mpc.sniff(reader("RIFF....WAVE")), 0, "sniff a WAV")
|
|
local _, e = Mpc.decode(reader("MPCX" .. string.rep("\0", 64)))
|
|
t:check(e and e.code == Err.CORRUPT, "bad magic: " .. tostring(e))
|
|
-- the stream header's CRC (SH is the first packet: "SH", size, CRC, version ...)
|
|
local p = string.find(c8, "SH", 5, true) :: number
|
|
local bad = string.sub(c8, 1, p + 7) .. string.char((string.byte(c8, p + 8) + 1) % 256) .. string.sub(c8, p + 9)
|
|
_, e = Mpc.decode(reader(bad))
|
|
t:check(e and e.code == Err.CHECKSUM, "SH CRC: " .. tostring(e))
|
|
_, e = Mpc.decode(reader("MP+\6" .. string.sub(c7, 5)))
|
|
t:check(e and e.code == Err.UNSUPPORTED, "SV6: " .. tostring(e))
|
|
_, e = Mpc.decode(reader(string.sub(c7, 1, 20)))
|
|
t:check(e and e.code == Err.TRUNCATED, "short SV7 header: " .. tostring(e))
|
|
_, e = Mpc.decode(reader(c8), { maxFrames = 1000 })
|
|
t:check(e and e.code == Err.LIMIT, "maxFrames: " .. tostring(e))
|
|
end
|
|
|
|
do
|
|
local samples = {}
|
|
for _, c in Fx do
|
|
table.insert(samples, string.sub(c.data, 1, 3000))
|
|
end
|
|
t:fuzz("decode", function(b: buffer)
|
|
return Mpc.decode(Bytes.reader(b))
|
|
end, samples, { mutations = 25, keepHeader = 4 })
|
|
end
|
|
|
|
t:done()
|