519 lines
20 KiB
Luau
519 lines
20 KiB
Luau
-- audio/mp3 tests: every fixture against its minimp3 reference decode (exact length, SNR > 90 dB on windows, block
|
|
-- energies) and against ffmpeg (length, SNR > 75 dB), lame-written streams against ffmpeg; chunked reads, reads into
|
|
-- a caller buffer, sample-accurate seeking; robustness (garbage, corruption, truncation, mono / stereo switches, API
|
|
-- misuse); ID3v1 / v2.2 / 2.3 / 2.4 and APE tags; the contract, sniff, decode to Audio, the fuzz no-throw pass.
|
|
-- Ported from mp3-luau tests/decode.luau, robust.luau, tags.luau. Fixtures: tools/fixtures/mp3.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 Cases = require("@tests/gen/mp3_cases")
|
|
|
|
local t = T.suite("mp3")
|
|
|
|
local function open(b: buffer | string, opts: any?): (any, any)
|
|
local buf = if type(b) == "string" then buffer.fromstring(b) else b
|
|
return Mp3.open(Bytes.reader(buf), opts)
|
|
end
|
|
|
|
local byName: { [string]: any } = {}
|
|
for _, c in Cases do
|
|
byName[c.name] = c
|
|
end
|
|
local function file(name: string): buffer
|
|
return buffer.fromstring(byName[name].data)
|
|
end
|
|
|
|
-- the reference format written by the minimp3 harness (see tools/fixtures/mp3.py)
|
|
local function parseRef(s: string): any
|
|
local b = buffer.fromstring(s)
|
|
local hz, ch, frames, W, N = buffer.readu32(b, 8), buffer.readu32(b, 12), buffer.readu32(b, 16), buffer.readu32(b, 20), buffer.readu32(b, 24)
|
|
local p = 28
|
|
local windows = {}
|
|
for _ = 1, N do
|
|
local start = buffer.readu32(b, p)
|
|
p += 4
|
|
local pcm = buffer.create(W * ch * 4)
|
|
buffer.copy(pcm, 0, b, p, W * ch * 4)
|
|
p += W * ch * 4
|
|
table.insert(windows, { start = start, pcm = pcm })
|
|
end
|
|
local B, nb = buffer.readu32(b, p), buffer.readu32(b, p + 4)
|
|
p += 8
|
|
local energies = {}
|
|
for i = 1, nb do
|
|
energies[i] = buffer.readf64(b, p)
|
|
p += 8
|
|
end
|
|
return { hz = hz, channels = ch, frames = frames, W = W, windows = windows, block = B, energies = energies }
|
|
end
|
|
|
|
local function snr(x: buffer, xo: number, ref: buffer, ro: number, n: number): number
|
|
local sig, err = 0, 0
|
|
for i = 0, n - 1 do
|
|
local r = buffer.readf32(ref, (ro + i) * 4)
|
|
local d = buffer.readf32(x, (xo + i) * 4) - r
|
|
sig += r * r
|
|
err += d * d
|
|
end
|
|
if err == 0 then
|
|
return math.huge
|
|
end
|
|
if sig == 0 then
|
|
return -math.huge
|
|
end
|
|
return 10 * math.log10(sig / err)
|
|
end
|
|
|
|
local function maxDiff(x: buffer, xo: number, y: buffer, yo: number, n: number): number
|
|
local m = 0
|
|
for i = 0, n - 1 do
|
|
local d = math.abs(buffer.readf32(x, (xo + i) * 4) - buffer.readf32(y, (yo + i) * 4))
|
|
if d > m then
|
|
m = d
|
|
end
|
|
end
|
|
return m
|
|
end
|
|
|
|
local rand = T.rng(12345)
|
|
|
|
-- ------------------------------------------------------------------------------------------------ contract
|
|
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 Mp3.caps.encode and Mp3.open ~= nil, "caps") -- encode: ws-audio (tests/audio/mp3_encode.luau)
|
|
|
|
-- ------------------------------------------------------------------------------------------------ decode vs references
|
|
for _, c in Cases do
|
|
local name = c.name
|
|
local buf = buffer.fromstring(c.data)
|
|
local dec, err = open(buf)
|
|
t:check(dec, name .. ": open (" .. tostring(err) .. ")")
|
|
local ch = dec.channels
|
|
local ref = if c.ref then parseRef(c.ref) else nil
|
|
if ref then
|
|
t:eq(dec.sampleRate, ref.hz, name .. ": sample rate")
|
|
t:eq(ch, ref.channels, name .. ": channels")
|
|
t:eq(dec.frames, ref.frames, name .. ": frames vs minimp3")
|
|
t:check(math.abs(dec.duration - ref.frames / ref.hz) < 1e-9, name .. ": duration")
|
|
else
|
|
t:eq(dec.sampleRate, c.rate, name .. ": sample rate")
|
|
t:eq(ch, c.channels, name .. ": channels")
|
|
t:eq(dec.frames, c.srcFrames, name .. ": gapless length = the encoder's input")
|
|
end
|
|
t:eq(dec.frames, c.ffFrames, name .. ": frames vs ffmpeg")
|
|
t:check(dec.bitrate > 0, name .. ": bitrate")
|
|
|
|
-- whole decode in chunks of varied sizes
|
|
local all = buffer.create(dec.frames * ch * 4)
|
|
local total = 0
|
|
while true do
|
|
local pcm, n = dec:read(1 + rand(3000))
|
|
if n == 0 then
|
|
break
|
|
end
|
|
t:check(buffer.len(pcm) == n * ch * 4, name .. ": chunk size")
|
|
buffer.copy(all, total * ch * 4, pcm, 0, n * ch * 4)
|
|
total += n
|
|
end
|
|
t:eq(total, dec.frames, name .. ": decoded frames")
|
|
t:eq(dec:position(), total / dec.sampleRate, name .. ": position at the end")
|
|
local _, n0 = dec:read(100)
|
|
t:eq(n0, 0, name .. ": read after the end")
|
|
|
|
local worst, worstFF = math.huge, math.huge
|
|
if ref then
|
|
for _, w in ref.windows do
|
|
local s = snr(all, w.start * ch, w.pcm, 0, ref.W * ch)
|
|
worst = math.min(worst, s)
|
|
t:check(s > 90, string.format("%s: window at %d SNR %.1f dB vs minimp3", name, w.start, s))
|
|
end
|
|
for bi, e in ref.energies do
|
|
local a = (bi - 1) * ref.block
|
|
local bEnd = math.min(ref.frames, a + ref.block)
|
|
local sum = 0
|
|
for i = a * ch, bEnd * ch - 1 do
|
|
local v = buffer.readf32(all, i * 4)
|
|
sum += v * v
|
|
end
|
|
t:check(math.abs(sum - e) <= 1e-5 * e + 1e-9, string.format("%s: block %d energy", name, bi))
|
|
end
|
|
end
|
|
for _, w in c.ff do
|
|
local pcm = buffer.fromstring(w.pcm)
|
|
local s = snr(all, w.start * ch, pcm, 0, buffer.len(pcm) // 4)
|
|
worstFF = math.min(worstFF, s)
|
|
t:check(s > 75, string.format("%s: window at %d SNR %.1f dB vs ffmpeg", name, w.start, s))
|
|
end
|
|
|
|
-- reading into a caller buffer (reused when big enough, ignored when too small)
|
|
dec:seek(0)
|
|
local out = buffer.create(500 * ch * 4)
|
|
local got, n1 = dec:read(500, out)
|
|
t:check(got == out and n1 == 500, name .. ": read into out")
|
|
t:eq(maxDiff(out, 0, all, 0, 500 * ch), 0, name .. ": out matches")
|
|
local got2, n2 = dec:read(600, out)
|
|
t:check(got2 ~= out and n2 == 600 and maxDiff(got2, 0, all, 500 * ch, 600 * ch) == 0, name .. ": small out ignored")
|
|
|
|
-- seeks: every window, then random targets, compared with the straight decode (bit-identical)
|
|
local targets = {}
|
|
for _, w in c.ff do
|
|
table.insert(targets, w.start)
|
|
end
|
|
for _ = 1, 12 do
|
|
table.insert(targets, rand(dec.frames))
|
|
end
|
|
table.insert(targets, dec.frames - 1)
|
|
for _, tg in targets do
|
|
dec:seekFrame(tg)
|
|
local _, pf = dec:position()
|
|
t:eq(pf, tg, name .. ": position after seek")
|
|
local want = math.min(1152, dec.frames - tg)
|
|
local pcm, n = dec:read(want)
|
|
t:eq(n, want, string.format("%s: read after seek to %d", name, tg))
|
|
t:eq(maxDiff(pcm, 0, all, tg * ch, n * ch), 0, string.format("%s: seek to %d bit-identical", name, tg))
|
|
end
|
|
dec:seek(dec.duration / 3)
|
|
local _, pf = dec:position()
|
|
local pcm, n = dec:read(64)
|
|
t:check(n == 64 and maxDiff(pcm, 0, all, pf * ch, 64 * ch) == 0, name .. ": seek seconds")
|
|
dec:seek(1e9)
|
|
t:eq(select(2, dec:read(10)), 0, name .. ": seek past the end")
|
|
dec:seek(-5)
|
|
local pcm0, n3 = dec:read(256)
|
|
t:check(n3 == 256 and maxDiff(pcm0, 0, all, 0, 256 * ch) == 0, name .. ": seek back to the start")
|
|
|
|
-- decode to Audio, also from a sub-range of a bigger buffer
|
|
local big = buffer.create(buffer.len(buf) + 17)
|
|
buffer.copy(big, 9, buf)
|
|
local a, aerr = Mp3.decode(Bytes.reader(big, 9, buffer.len(buf)))
|
|
t:check(a and a.kind == "audio" and a.frames == dec.frames and a.channels == ch, name .. ": decode (" .. tostring(aerr) .. ")")
|
|
t:eq(maxDiff(a.samples, 0, all, 0, dec.frames * ch), 0, name .. ": decode = stream")
|
|
t:check(a.meta.source.format == "mp3" and a.meta.source.layer == dec.layer, name .. ": meta.source")
|
|
t:eq(Mp3.sniff(Bytes.reader(buf)) > 0.5, true, name .. ": sniff")
|
|
|
|
t:note("%-20s L%d MPEG-%s %5d Hz %d ch %6d frames %3d kbps SNR minimp3 %s, ffmpeg %.1f dB", name, dec.layer, tostring(dec.version), dec.sampleRate, ch, dec.frames, dec.bitrate // 1000, if ref then string.format("%.1f", worst) else "-", worstFF)
|
|
end
|
|
|
|
-- ------------------------------------------------------------------------------------------------ robustness
|
|
local function concat(...: buffer): buffer
|
|
local n = 0
|
|
for _, b in { ... } do
|
|
n += buffer.len(b)
|
|
end
|
|
local out = buffer.create(n)
|
|
local o = 0
|
|
for _, b in { ... } do
|
|
buffer.copy(out, o, b, 0)
|
|
o += buffer.len(b)
|
|
end
|
|
return out
|
|
end
|
|
local function slice(b: buffer, i: number, j: number): buffer
|
|
local out = buffer.create(j - i)
|
|
buffer.copy(out, 0, b, i, j - i)
|
|
return out
|
|
end
|
|
-- decodes everything; checks every sample is finite; returns pcm, frames, decoder
|
|
local function decodeAll(b: buffer, label: string): (buffer?, number, any)
|
|
local ok, dec, err = pcall(open, b)
|
|
t:check(ok, label .. ": open does not throw (" .. tostring(dec) .. ")")
|
|
if not dec then
|
|
return nil, 0, err
|
|
end
|
|
local ok2, pcm, n = pcall(dec.readAll, dec)
|
|
t:check(ok2, label .. ": decode does not throw (" .. tostring(pcm) .. ")")
|
|
local bad = 0
|
|
for i = 0, n * dec.channels - 1 do
|
|
local v = buffer.readf32(pcm, i * 4)
|
|
if v ~= v or v == math.huge or v == -math.huge then
|
|
bad += 1
|
|
end
|
|
end
|
|
t:eq(bad, 0, label .. ": finite samples")
|
|
return pcm, n, dec
|
|
end
|
|
|
|
-- the same pseudo-random sequence as mp3-luau's robust.luau (its junk is known not to chain into frames)
|
|
local orandRaw = T.rng(777)
|
|
local function orand(n: number): number -- 1..n
|
|
return orandRaw(n) + 1
|
|
end
|
|
local base = file("l3_cbr128_js")
|
|
local refPcm, refN, refDec = decodeAll(base, "base")
|
|
assert(refPcm)
|
|
|
|
-- garbage before the first frame (with fake sync words): same output, bit for bit
|
|
do
|
|
local junk = buffer.create(5000)
|
|
for i = 0, 4999 do
|
|
buffer.writeu8(junk, i, (orand(256) - 1))
|
|
end
|
|
for i = 0, 4999, 97 do
|
|
buffer.writeu8(junk, i, 0xFF)
|
|
if i + 1 < 5000 then
|
|
buffer.writeu8(junk, i + 1, 0xFB)
|
|
end
|
|
end
|
|
local pcm, n, dec = decodeAll(concat(junk, base), "garbage prefix")
|
|
t:check(n == refN and dec.frames == refN, string.format("garbage prefix: same length (%d %d vs %d)", n, dec.frames, refN))
|
|
t:check(pcm and maxDiff(pcm, 0, refPcm, 0, n * 2) == 0, "garbage prefix: same samples")
|
|
end
|
|
-- garbage in the middle: re-syncs, keeps going
|
|
do
|
|
local half = buffer.len(base) // 2
|
|
local junk = buffer.create(3333)
|
|
for i = 0, 3332 do
|
|
buffer.writeu8(junk, i, (orand(256) - 1))
|
|
end
|
|
local _, n = decodeAll(concat(slice(base, 0, half), junk, slice(base, half, buffer.len(base))), "garbage middle")
|
|
t:check(math.abs(n - refN) <= 4 * 1152, string.format("garbage middle: %d frames vs %d", n, refN))
|
|
end
|
|
-- corrupt bytes all over the stream
|
|
do
|
|
local b = buffer.create(buffer.len(base))
|
|
buffer.copy(b, 0, base)
|
|
for _ = 1, 60 do
|
|
buffer.writeu8(b, 1000 + orand(buffer.len(b) - 1001), (orand(256) - 1))
|
|
end
|
|
local _, n = decodeAll(b, "corrupt bytes")
|
|
t:check(n > refN // 2, "corrupt bytes: most frames decode")
|
|
end
|
|
-- truncated anywhere
|
|
for _ = 1, 25 do
|
|
local cut = orand(buffer.len(base))
|
|
local _, n = decodeAll(slice(base, 0, cut), "truncated at " .. cut)
|
|
t:check(n <= refN, "truncated: not longer")
|
|
end
|
|
-- a stereo stream (no Xing tag) then a mono one of the same rate -> stereo, the mono part in both channels
|
|
do
|
|
local stereo = file("l3_noreservoir")
|
|
local mono = file("l3_vbr_v7_mono")
|
|
local _, stereoN = decodeAll(stereo, "stereo alone")
|
|
local _, monoN = decodeAll(mono, "mono alone")
|
|
local pcm, n, dec = decodeAll(concat(stereo, mono), "stereo then mono")
|
|
t:check(dec and dec.channels == 2, "switch: stereo output")
|
|
t:check(n >= stereoN + monoN, "switch: both parts decode")
|
|
local same = true
|
|
for i = n - 3000, n - 2000 do
|
|
if buffer.readf32(pcm :: buffer, i * 8) ~= buffer.readf32(pcm :: buffer, i * 8 + 4) then
|
|
same = false
|
|
end
|
|
end
|
|
t:check(same, "switch: mono part duplicated")
|
|
end
|
|
-- bad input and API misuse
|
|
do
|
|
local d, err = open(buffer.create(0))
|
|
t:check(d == nil and Err.is(err) and err.code == Err.CORRUPT, "empty buffer")
|
|
d = open(string.rep("not an mp3 file at all ", 500))
|
|
t:check(d == nil, "text")
|
|
t:check(open(buffer.create(100000)) == nil, "zeros")
|
|
local ffs = buffer.create(100000)
|
|
buffer.fill(ffs, 0, 0xFF)
|
|
t:check(pcall(function()
|
|
local dd = open(ffs)
|
|
if dd then
|
|
dd:readAll()
|
|
end
|
|
end), "all 0xFF")
|
|
local _, lerr = open(base, { maxFrames = 1000 })
|
|
t:check(lerr and lerr.code == Err.LIMIT, "maxFrames")
|
|
local dec = refDec
|
|
dec:seek(0)
|
|
t:eq(select(2, dec:read(-5)), 0, "read(-5)")
|
|
t:eq(select(2, dec:read(nil :: any)), 4096, "read(nil) = 4096 frames")
|
|
dec:seek(nil :: any)
|
|
t:eq(select(2, dec:position()), 0, "seek(nil)")
|
|
dec:seek(0 / 0)
|
|
t:check(select(2, dec:position()) >= 0, "seek(nan)")
|
|
t:eq(select(2, dec:read(0 / 0)), 0, "read(nan)")
|
|
t:eq(Mp3.sniff(Bytes.reader(buffer.fromstring("RIFF....WAVEfmt "))), 0, "sniff a WAV")
|
|
t:eq(Mp3.sniff(Bytes.reader(buffer.create(0))), 0, "sniff nothing")
|
|
end
|
|
|
|
-- ------------------------------------------------------------------------------------------------ tags
|
|
local function eq(a: any, b: any, label: string)
|
|
t:eq(a, b, label)
|
|
end
|
|
do
|
|
local dec = assert(open(file("l3_tags")))
|
|
local tg = dec.tags
|
|
eq(dec.id3.id3v2, "2.3", "fixture id3v2")
|
|
eq(tg.title, "Test Title", "fixture title")
|
|
eq(tg.artist, "Some Artist", "fixture artist")
|
|
eq(tg.album, "An Album", "fixture album")
|
|
eq(tg.date, "2001", "fixture year")
|
|
eq(tg.track, "7", "fixture track")
|
|
eq(tg.genre, "Rock", "fixture genre")
|
|
eq(tg.comment, "hello", "fixture comment")
|
|
t:check(dec.id3.id3v1, "fixture id3v1")
|
|
for k, v in tg do
|
|
t:check(type(k) == "string" and type(v) == "string", "tags are strings")
|
|
end
|
|
end
|
|
|
|
local audio = file("l3_mpeg25_8k")
|
|
local plain = assert(open(audio))
|
|
local plainPcm, plainN = plain:readAll()
|
|
|
|
local function syncsafe(n: number): string
|
|
return string.char(bit32.band(bit32.rshift(n, 21), 127), bit32.band(bit32.rshift(n, 14), 127), bit32.band(bit32.rshift(n, 7), 127), bit32.band(n, 127))
|
|
end
|
|
local function be32(n: number): string
|
|
return string.char(bit32.rshift(n, 24), bit32.band(bit32.rshift(n, 16), 255), bit32.band(bit32.rshift(n, 8), 255), bit32.band(n, 255))
|
|
end
|
|
local function le32(n: number): string
|
|
return string.char(bit32.band(n, 255), bit32.band(bit32.rshift(n, 8), 255), bit32.band(bit32.rshift(n, 16), 255), bit32.rshift(n, 24))
|
|
end
|
|
local function be24(n: number): string
|
|
return string.char(bit32.rshift(n, 16), bit32.band(bit32.rshift(n, 8), 255), bit32.band(n, 255))
|
|
end
|
|
local function utf16le(s: string): string
|
|
local out = { "\xFF\xFE" }
|
|
for _, cp in utf8.codes(s) do
|
|
if cp >= 0x10000 then
|
|
local v = cp - 0x10000
|
|
local hi, lo = 0xD800 + bit32.rshift(v, 10), 0xDC00 + bit32.band(v, 0x3FF)
|
|
table.insert(out, string.char(bit32.band(hi, 255), bit32.rshift(hi, 8), bit32.band(lo, 255), bit32.rshift(lo, 8)))
|
|
else
|
|
table.insert(out, string.char(bit32.band(cp, 255), bit32.rshift(cp, 8)))
|
|
end
|
|
end
|
|
return table.concat(out)
|
|
end
|
|
local function withAudio(prefix: string, suffix: string?): buffer
|
|
return buffer.fromstring(prefix .. buffer.tostring(audio) .. (suffix or ""))
|
|
end
|
|
local function sameAudio(dec: any, label: string)
|
|
eq(dec.frames, plain.frames, label .. ": frames")
|
|
local pcm, n = dec:readAll()
|
|
t:check(n == plainN and maxDiff(pcm, 0, plainPcm, 0, n) == 0, label .. ": same samples")
|
|
end
|
|
|
|
-- ID3v2.4: UTF-8, syncsafe frame sizes, an extended header, multi-value TPE1, numeric TCON, padding
|
|
do
|
|
local function frame(id: string, body: string): string
|
|
return id .. syncsafe(#body) .. "\0\0" .. body
|
|
end
|
|
local frames = frame("TIT2", "\3Ünïcödé ★ title") .. frame("TPE1", "\3One\0Two") .. frame("TCON", "\3(17)") .. frame("TDRC", "\0" .. "1999-05-01") .. frame("COMM", "\3eng\0a comment") .. string.rep("\0", 64)
|
|
local body = syncsafe(6) .. "\1\0" .. frames
|
|
local tag = "ID3\4\0\x40" .. syncsafe(#body) .. body
|
|
local dec = assert(open(withAudio(tag)))
|
|
eq(dec.id3.id3v2, "2.4", "v2.4 version")
|
|
eq(dec.tags.title, "Ünïcödé ★ title", "v2.4 title")
|
|
eq(dec.tags.artist, "One / Two", "v2.4 multi-value")
|
|
eq(dec.tags.genre, "Rock", "v2.4 genre")
|
|
eq(dec.tags.date, "1999-05-01", "v2.4 TDRC")
|
|
eq(dec.tags.comment, "a comment", "v2.4 COMM")
|
|
sameAudio(dec, "v2.4")
|
|
t:check(Mp3.sniff(Bytes.reader(withAudio(tag))) >= 0.7, "sniff behind an ID3v2 tag")
|
|
end
|
|
-- ID3v2.3: UTF-16 with BOM (incl. a surrogate pair), latin-1, whole-tag unsynchronisation
|
|
do
|
|
local function frame(id: string, body: string): string
|
|
return id .. be32(#body) .. "\0\0" .. body
|
|
end
|
|
local frames = frame("TIT2", "\1" .. utf16le("Grüße 𝄞")) .. frame("TALB", "\0Caf\xE9") .. frame("TRCK", "\0" .. "3/12") .. frame("TCON", "\0" .. "Electronic")
|
|
local unsync = string.gsub(frames, "\xFF", "\xFF\0")
|
|
local tag = "ID3\3\0\x80" .. syncsafe(#unsync) .. unsync
|
|
local dec = assert(open(withAudio(tag)))
|
|
eq(dec.id3.id3v2, "2.3", "v2.3 version")
|
|
eq(dec.tags.title, "Grüße 𝄞", "v2.3 UTF-16 title")
|
|
eq(dec.tags.album, "Café", "v2.3 latin-1 album")
|
|
eq(dec.tags.track, "3/12", "v2.3 track")
|
|
eq(dec.tags.genre, "Electronic", "v2.3 genre")
|
|
eq(dec.id3.raw.TALB, "Café", "raw frame")
|
|
sameAudio(dec, "v2.3")
|
|
end
|
|
-- ID3v2.2: 3-character ids, 24-bit sizes
|
|
do
|
|
local function frame(id: string, body: string): string
|
|
return id .. be24(#body) .. body
|
|
end
|
|
local frames = frame("TT2", "\0Old Title") .. frame("TP1", "\0Old Artist") .. frame("TCO", "\0(8)") .. frame("COM", "\0eng\0old comment")
|
|
local tag = "ID3\2\0\0" .. syncsafe(#frames) .. frames
|
|
local dec = assert(open(withAudio(tag)))
|
|
eq(dec.id3.id3v2, "2.2", "v2.2 version")
|
|
eq(dec.tags.title, "Old Title", "v2.2 title")
|
|
eq(dec.tags.artist, "Old Artist", "v2.2 artist")
|
|
eq(dec.tags.genre, "Jazz", "v2.2 genre")
|
|
eq(dec.tags.comment, "old comment", "v2.2 comment")
|
|
sameAudio(dec, "v2.2")
|
|
end
|
|
-- ID3v1.1 alone (track byte), then APEv2 + ID3v1 at the end
|
|
do
|
|
local function pad(s: string, n: number): string
|
|
return s .. string.rep("\0", n - #s)
|
|
end
|
|
local v1 = "TAG" .. pad("V1 Title", 30) .. pad("V1 Artist", 30) .. pad("V1 Album", 30) .. "1987" .. pad("v1 comment", 28) .. "\0\9" .. "\17"
|
|
local dec = assert(open(withAudio("", v1)))
|
|
t:check(dec.id3.id3v1, "v1 flag")
|
|
eq(dec.tags.title, "V1 Title", "v1 title")
|
|
eq(dec.tags.artist, "V1 Artist", "v1 artist")
|
|
eq(dec.tags.album, "V1 Album", "v1 album")
|
|
eq(dec.tags.date, "1987", "v1 year")
|
|
eq(dec.tags.comment, "v1 comment", "v1 comment")
|
|
eq(dec.tags.track, "9", "v1.1 track")
|
|
eq(dec.tags.genre, "Rock", "v1 genre")
|
|
sameAudio(dec, "v1")
|
|
local item = "\5\0\0\0" .. "\0\0\0\0" .. "Title\0" .. "hello"
|
|
local size = #item + 32
|
|
local function apeHdr(isHeader: boolean): string
|
|
local flags = if isHeader then "\0\0\0\xA0" else "\0\0\0\x80"
|
|
return "APETAGEX" .. "\xD0\7\0\0" .. le32(size) .. "\1\0\0\0" .. flags .. string.rep("\0", 8)
|
|
end
|
|
local ape = apeHdr(true) .. item .. apeHdr(false)
|
|
local dec2 = assert(open(withAudio("", ape .. v1)))
|
|
t:check(dec2.id3.ape, "ape flag")
|
|
eq(dec2.tags.title, "V1 Title", "ape + v1 title")
|
|
sameAudio(dec2, "ape + v1")
|
|
end
|
|
-- a truncated ID3v2 tag never throws
|
|
do
|
|
local tag = "ID3\3\0\0" .. syncsafe(5000) .. "TIT2" .. be32(4000) .. "\0\0\0abc"
|
|
local ok, dec = pcall(open, withAudio(tag))
|
|
t:check(ok, "truncated tag does not throw")
|
|
t:check(dec == nil or dec.frames <= plain.frames, "truncated tag")
|
|
end
|
|
|
|
-- ------------------------------------------------------------------------------------------------ through the api
|
|
do
|
|
local F = require("@formats")
|
|
local data = byName.l3_tags.data
|
|
t:eq(F.detect(data), "mp3", "F.detect")
|
|
local a, err = F.audio.load(data)
|
|
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 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
|
|
do
|
|
local samples = {}
|
|
for _, name in { "l3_cbr32_mono", "l3_mpeg2_22k", "l3_mpeg25_8k", "l2_64_mono", "l2_mpeg2_24k", "l3_tags", "lame_cbr64_mono_22k" } do
|
|
local s = byName[name].data
|
|
table.insert(samples, string.sub(s, 1, math.min(#s, 6000)))
|
|
end
|
|
t:fuzz("open + read + seek", function(b: buffer)
|
|
local dec = Mp3.open(Bytes.reader(b))
|
|
if dec then
|
|
dec:read(1 + rand(5000))
|
|
dec:seek(dec.duration * rand(100) / 100)
|
|
dec:read(1 + rand(5000))
|
|
dec:seekFrame(rand(dec.frames + 1))
|
|
dec:readAll()
|
|
end
|
|
Mp3.sniff(Bytes.reader(b))
|
|
return dec
|
|
end, samples, { mutations = 12 })
|
|
t:fuzz("decode", function(b: buffer)
|
|
return Mp3.decode(Bytes.reader(b))
|
|
end, samples, { mutations = 4, garbage = 50 })
|
|
end
|
|
|
|
t:done()
|