ws-audio2: merge ws/audio2-lossy (Musepack SV7/SV8, AC-3, AudioWrap AC-3 hook)
This commit is contained in:
commit
39da2f6648
25 files changed
+4196
-4
No files matched your search
@@ -0,0 +1,155 @@
|
||||
-- audio/ac3 tests: AC-3 streams from ffmpeg (every acmod with and without LFE, coupling incl. cplbegf 0, rematrixing,
|
||||
-- 32 / 44.1 / 48 kHz) and aften (block switching, 1+1 dual mono, dynamic range profiles, the alternate BSI) against
|
||||
-- ffmpeg's float decode, or liba52's for block-switched streams (SNR, max difference; dithflags cleared in the
|
||||
-- fixture step so both sides decode the same numbers); dynamic range on; the encoders' dithered originals; AC-3 tracks
|
||||
-- of MKV / MP4 / MPEG-TS / AVI through the api's audio hook; exact seeking; resync and truncation; error codes (E-AC-3,
|
||||
-- CRC); the fuzz no-throw pass. Fixtures: tools/fixtures/ac3.py.
|
||||
local T = require("@tests/T")
|
||||
local F = require("@formats")
|
||||
local Ac3F = require("@formats/audio/ac3")
|
||||
local Ac3 = require("@formats/codecs/audio/Ac3")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Err = require("@formats/core/Err")
|
||||
local Fx = require("@tests/gen/ac3_cases")
|
||||
local Tracks = require("@tests/gen/ac3_tracks")
|
||||
|
||||
local t = T.suite("ac3")
|
||||
|
||||
local function reader(s: string): Bytes.Reader
|
||||
return Bytes.reader(buffer.fromstring(s))
|
||||
end
|
||||
|
||||
-- SNR (dB) and the largest difference of interleaved f32 `got` against the f32le reference `ref`
|
||||
local function compare(got: buffer, ref: string, count: number): (number, number)
|
||||
local rb = buffer.fromstring(ref)
|
||||
local n = math.min(count, #ref // 4, buffer.len(got) // 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, 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(Ac3F.name, "ac3", "name")
|
||||
t:check(Ac3F.caps.decode and not Ac3F.caps.encode and Ac3F.encode == nil, "caps: decode only")
|
||||
|
||||
local byName = {}
|
||||
for _, c in Fx do
|
||||
byName[c.name] = c
|
||||
local r = reader(c.data)
|
||||
t:check(Ac3F.sniff(r) >= 0.9, c.name .. ": sniff")
|
||||
local a, err = Ac3F.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 .. ": frames")
|
||||
-- liba52's 6-channel output is 16-bit
|
||||
local min = if c.refTool == "a52dec" and c.channels > 2 then 80 else 95
|
||||
local s, maxd = compare(a.samples, c.ref, a.frames * a.channels)
|
||||
t:check(s > min, string.format("%s: SNR %.1f dB vs %s", c.name, s, c.refTool))
|
||||
t:note("%s: %d ch %d Hz, %d frames: SNR %.1f dB, max diff %.1e vs %s", c.name, a.channels, a.sampleRate, a.frames, s, maxd, c.refTool)
|
||||
if c.refDrc then
|
||||
local d = Ac3F.decode(r, { drc = 1 }) :: any
|
||||
local sd = compare(d.samples, c.refDrc, d.frames * d.channels)
|
||||
t:check(sd > min, string.format("%s: dynamic range on: SNR %.1f dB vs %s", c.name, sd, c.refTool))
|
||||
local off = compare(d.samples, c.ref, d.frames * d.channels)
|
||||
t:check(off < 40, c.name .. ": the dynamic range gains change the output")
|
||||
end
|
||||
if c.dithered then
|
||||
-- the encoder's own stream (dithflag set): our dither against the reference's undithered decode
|
||||
local dd = Ac3F.decode(reader(c.dithered)) :: any
|
||||
local sd = compare(dd.samples, c.ref, dd.frames * dd.channels)
|
||||
t:check(dd and sd > 10, string.format("%s: dithered original decodes (SNR %.1f dB, the dither noise)", c.name, sd))
|
||||
end
|
||||
end
|
||||
|
||||
-- AC-3 tracks of video containers, through the api's audio hook (containers/AudioWrap -> audio/ac3)
|
||||
for _, c in Tracks do
|
||||
local v, err = F.video.open(buffer.fromstring(c.data))
|
||||
t:check(v, c.name .. ": open: " .. tostring(err))
|
||||
if not v then
|
||||
continue
|
||||
end
|
||||
t:check(v.audio, c.name .. ": audio track opens: " .. tostring(v.audioError))
|
||||
if v.audio then
|
||||
local a = assert(F.ops.audio.toAudio(v.audio))
|
||||
t:check(a.channels == c.channels and a.sampleRate == c.rate, c.name .. ": audio format")
|
||||
local s = compare(a.samples, c.ref, a.frames * a.channels)
|
||||
t:check(s > 95, string.format("%s: AC-3 track SNR %.1f dB vs ffmpeg", c.name, s))
|
||||
t:note("%s: AC-3 track, %d ch, %d frames, SNR %.1f dB", c.name, a.channels, a.frames, s)
|
||||
end
|
||||
end
|
||||
|
||||
-- streaming + exact seeking (one frame of pre-roll for the overlap)
|
||||
do
|
||||
local c = byName["5.1_448"]
|
||||
local whole = Ac3F.decode(reader(c.data)) :: any
|
||||
local dec = Ac3F.open(reader(c.data)) :: any
|
||||
local _ = dec:read(3000)
|
||||
for _, at in { 0.1, 0.0, 0.0517 } do
|
||||
dec:seek(at)
|
||||
local b, n = dec:read(700)
|
||||
local f0 = math.floor(at * c.rate + 0.5)
|
||||
local maxd = 0
|
||||
for i = 0, n * 6 - 1 do
|
||||
maxd = math.max(maxd, math.abs(buffer.readf32(b, i * 4) - buffer.readf32(whole.samples, (f0 * 6 + i) * 4)))
|
||||
end
|
||||
t:check(n == 700 and maxd < 1e-6, string.format("seek %.4f s (max diff %g)", at, maxd))
|
||||
end
|
||||
end
|
||||
|
||||
-- resync over garbage, a truncated last frame, the raw codec API
|
||||
do
|
||||
local c = byName.stereo_44k
|
||||
local a = Ac3F.decode(reader("\0\11\119garbage" .. c.data)) :: any
|
||||
t:check(a and a.frames == c.frames, "garbage in front: same frames")
|
||||
local h = Ac3.header(buffer.fromstring(c.data), 0, #c.data) :: Ac3.Header
|
||||
t:check(h.sampleRate == 44100 and h.channels == 2 and h.acmod == 2 and h.bitrate == 192000, "header fields")
|
||||
local cut = Ac3F.decode(reader(string.sub(c.data, 1, #c.data - 10))) :: any
|
||||
t:check(cut and cut.frames == c.frames - 1536, "truncated last frame dropped")
|
||||
local d = Ac3.decoder()
|
||||
local n = d:decode(buffer.fromstring(c.data), 0, h.frameSize)
|
||||
t:check(n == 1536 and d.channels == 2 and #d.pcm == 1536 * 2, "decoder: one syncframe")
|
||||
end
|
||||
|
||||
-- errors
|
||||
do
|
||||
local c = byName.stereo_44k.data
|
||||
t:eq(Ac3F.sniff(reader("RIFF....WAVE")), 0, "sniff a WAV")
|
||||
local _, e = Ac3F.decode(reader(string.rep("\0", 100)))
|
||||
t:check(e and e.code == Err.CORRUPT, "no frames: " .. tostring(e))
|
||||
-- an E-AC-3 header: bsid 16, frmsiz 255 (512 bytes), then the same again
|
||||
local eac3 = "\11\119\0\255\128\128" .. string.rep("\0", 506)
|
||||
_, e = Ac3F.decode(reader(eac3 .. eac3))
|
||||
t:check(e and e.code == Err.UNSUPPORTED, "E-AC-3: " .. tostring(e))
|
||||
local _, fe = F.audio.load(buffer.fromstring(eac3 .. eac3))
|
||||
t:check(fe and fe.code == Err.UNSUPPORTED, "E-AC-3 through the api: " .. tostring(fe))
|
||||
-- a flipped bit in the middle of the first frame: the CRC catches it
|
||||
local bad = string.sub(c, 1, 300) .. string.char(bit32.bxor(string.byte(c, 301), 4)) .. string.sub(c, 302)
|
||||
_, e = Ac3F.decode(reader(bad))
|
||||
t:check(e and e.code == Err.CHECKSUM, "CRC: " .. tostring(e))
|
||||
_, e = Ac3F.decode(reader(c), { maxFrames = 2 })
|
||||
t:check(e and e.code == Err.LIMIT, "maxFrames: " .. tostring(e))
|
||||
end
|
||||
|
||||
-- fuzz (without the CRC check, so mutations reach the decoder)
|
||||
do
|
||||
local samples = {}
|
||||
for _, c in Fx do
|
||||
table.insert(samples, string.sub(c.data, 1, 4000))
|
||||
end
|
||||
t:fuzz("decode", function(b: buffer)
|
||||
return Ac3F.decode(Bytes.reader(b), { crc = false })
|
||||
end, samples, { mutations = 25 })
|
||||
t:fuzz("decode (crc)", function(b: buffer)
|
||||
return Ac3F.decode(Bytes.reader(b))
|
||||
end, samples, { mutations = 4, garbage = 20 })
|
||||
end
|
||||
|
||||
t:done()
|
||||
@@ -0,0 +1,145 @@
|
||||
-- audio/mpc tests: Musepack SV8 (mpcenc 1.30: stereo / mono, 32 / 44.1 / 48 kHz, quality 1 .. 9, 1 / 4 / 16 / 64
|
||||
-- frames per packet) and SV7 (mppenc 1.16, quality 2 / 5 / 8) against libmpcdec's float output (mpcf32: gapless
|
||||
-- length, near bit-exact) and ffmpeg's decode (SNR); APEv2 tags; seeking (SV8 exact, SV7 like libmpcdec); a leading
|
||||
-- ID3v2 tag; truncated files; error codes; the fuzz no-throw pass. Fixtures: tools/fixtures/mpc.py.
|
||||
local T = require("@tests/T")
|
||||
local Mpc = require("@formats/audio/mpc")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Err = require("@formats/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
|
||||
|
||||
-- SNR (dB) and the largest difference of frames [from, to) of got against a f32le reference (frames from 0)
|
||||
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
|
||||
|
||||
-- seeking: SV8 is exact (one packet of pre-roll); SV7 decodes 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
|
||||
|
||||
-- an ID3v2 tag in front; truncated files
|
||||
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
|
||||
|
||||
-- errors
|
||||
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
|
||||
|
||||
-- fuzz
|
||||
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()
|
||||
@@ -0,0 +1,57 @@
|
||||
-- codecs/audio/Ac3 tests: the codec API under audio/ac3 and the container hook: header fields of every fixture layout,
|
||||
-- the CRC (a syncframe checks to 0), one-frame decodes against the reference (frame by frame, as a container feeds
|
||||
-- them), the block offsets, reset, argument / data errors, and a fuzz pass over single frames (CRC off).
|
||||
-- Fixtures: tools/fixtures/ac3.py.
|
||||
local T = require("@tests/T")
|
||||
local Ac3 = require("@formats/codecs/audio/Ac3")
|
||||
local Err = require("@formats/core/Err")
|
||||
local Fx = require("@tests/gen/ac3_cases")
|
||||
|
||||
local t = T.suite("codecs/ac3")
|
||||
|
||||
local ACMOD_CH = { [0] = 2, 1, 2, 3, 3, 4, 4, 5 }
|
||||
local frames = {}
|
||||
for _, c in Fx do
|
||||
local b = buffer.fromstring(c.data)
|
||||
local h = Ac3.header(b, 0, buffer.len(b))
|
||||
t:check(h, c.name .. ": header")
|
||||
if not h then
|
||||
continue
|
||||
end
|
||||
t:check(h.sampleRate == c.rate and h.channels == c.channels, c.name .. ": header rate / channels")
|
||||
t:eq(ACMOD_CH[h.acmod] + (if h.lfeon then 1 else 0), c.channels, c.name .. ": acmod + lfe")
|
||||
t:eq(Ac3.crc(b, 2, h.frameSize - 2), 0, c.name .. ": crc2 checks")
|
||||
-- frame by frame, fresh decoder: the second frame against the reference
|
||||
local d = Ac3.decoder()
|
||||
local n1 = d:decode(b, 0, h.frameSize)
|
||||
local h2 = Ac3.header(b, h.frameSize, buffer.len(b)) :: Ac3.Header
|
||||
local n2, err = d:decode(b, h.frameSize, h2.frameSize)
|
||||
t:check(n1 == 1536 and n2 == 1536, c.name .. ": two frames: " .. tostring(err))
|
||||
local rb = buffer.fromstring(c.ref)
|
||||
local maxd = 0
|
||||
for i = 1, 1536 * c.channels do
|
||||
maxd = math.max(maxd, math.abs(d.pcm[i] - buffer.readf32(rb, (1536 * c.channels + i - 1) * 4)))
|
||||
end
|
||||
t:check(maxd < 1e-4, string.format("%s: frame 2 (max diff %g)", c.name, maxd))
|
||||
t:check(#d.blocks == 6 and d.blocks[1] > 40 and d.blocks[6] < h2.frameSize * 8, c.name .. ": block offsets")
|
||||
d:reset()
|
||||
table.insert(frames, buffer.readstring(b, 0, h.frameSize))
|
||||
end
|
||||
|
||||
-- errors
|
||||
do
|
||||
local d = Ac3.decoder()
|
||||
local _, e = d:decode(buffer.create(10), 20)
|
||||
t:check(e and e.code == Err.ARG, "offset past the buffer: " .. tostring(e))
|
||||
_, e = d:decode(buffer.fromstring(string.rep("\0", 64)), 0)
|
||||
t:check(e and e.code == Err.CORRUPT, "no sync: " .. tostring(e))
|
||||
_, e = d:decode(buffer.fromstring(string.sub(frames[1], 1, 100)), 0)
|
||||
t:check(e and e.code == Err.TRUNCATED, "short frame: " .. tostring(e))
|
||||
t:check(Ac3.header(buffer.fromstring("\11\119\0\0\255\0\0\0"), 0, 8) == nil, "frmsizecod 63 / fscod 3: no header")
|
||||
end
|
||||
|
||||
t:fuzz("decode one frame (crc off)", function(b: buffer)
|
||||
return Ac3.decoder({ crc = false }):decode(b, 0)
|
||||
end, frames, { mutations = 30, keepHeader = 6 })
|
||||
|
||||
t:done()
|
||||
@@ -0,0 +1,79 @@
|
||||
-- codecs/audio/ApeTag tests: APEv2 tags with and without a header, APEv1, multi-value items, binary items, an ID3v1
|
||||
-- tag after the APE tag, a tag inside a sub-range, malformed sizes / counts; the fuzz no-throw pass. The real files
|
||||
-- (wavpack -w, mac -t, ...) are covered by tests/audio/wavpack.luau and tests/audio/ape.luau.
|
||||
local T = require("@tests/T")
|
||||
local ApeTag = require("@formats/codecs/audio/ApeTag")
|
||||
|
||||
local t = T.suite("apetag")
|
||||
|
||||
local function u32(v: number): string
|
||||
return string.char(v % 256, (v // 256) % 256, (v // 65536) % 256, (v // 16777216) % 256)
|
||||
end
|
||||
-- tag(items, version, header) -> the tag bytes; items = { { key, value, flags? } }
|
||||
local function tag(items: { any }, version: number, header: boolean): string
|
||||
local body = {}
|
||||
for _, it in items do
|
||||
table.insert(body, u32(#it[2]) .. u32(it[3] or 0) .. it[1] .. "\0" .. it[2])
|
||||
end
|
||||
local b = table.concat(body)
|
||||
local flags = if header then 0x80000000 else 0
|
||||
local function block(isHeader: boolean): string
|
||||
return "APETAGEX" .. u32(version) .. u32(#b + 32) .. u32(#items) .. u32(flags + (if isHeader then 0x20000000 else 0)) .. string.rep("\0", 8)
|
||||
end
|
||||
return (if header then block(true) else "") .. b .. block(false)
|
||||
end
|
||||
|
||||
do
|
||||
local s = "audio data" .. tag({ { "Title", "Hello" }, { "Artist", "A\0B" }, { "Year", "2026" }, { "Track", "3/9" },
|
||||
{ "Cover Art (Front)", "f.png\0PNG", 2 }, { "Album Artist", "Band" } }, 2000, true)
|
||||
local b = buffer.fromstring(s)
|
||||
local tags, info = ApeTag.find(b, 0, #s)
|
||||
t:check(tags and info, "v2 found")
|
||||
t:eq(tags.title, "Hello", "title")
|
||||
t:eq(tags.artist, "A; B", "multi value")
|
||||
t:eq(tags.date, "2026", "year -> date")
|
||||
t:eq(tags.track, "3/9", "track")
|
||||
t:eq(tags.albumartist, "Band", "album artist")
|
||||
t:eq(tags["cover art (front)"], nil, "binary item not a tag")
|
||||
t:eq(info.start, 10, "start at the header")
|
||||
t:eq(info.stop, #s, "stop")
|
||||
t:eq(info.version, 2000, "version")
|
||||
t:eq(#info.items, 6, "items")
|
||||
t:eq(info.items[5].kind, "binary", "binary kind")
|
||||
t:eq(info.items[5].value, "f.png\0PNG", "binary value")
|
||||
-- ID3v1 after it
|
||||
local s2 = s .. "TAG" .. string.rep("x", 125)
|
||||
local tags2, info2 = ApeTag.find(buffer.fromstring(s2), 0, #s2)
|
||||
t:check(tags2 and tags2.title == "Hello" and info2.stop == #s, "before ID3v1")
|
||||
-- inside a sub-range
|
||||
local s3 = "zz" .. s .. "yyy"
|
||||
local tags3, info3 = ApeTag.find(buffer.fromstring(s3), 2, 2 + #s)
|
||||
t:check(tags3 and tags3.title == "Hello" and info3.start == 12, "sub-range")
|
||||
-- the start bounds the header
|
||||
local tags4, info4 = ApeTag.find(b, 50, #s)
|
||||
t:check(tags4 == nil and info4 == nil, "items before start")
|
||||
end
|
||||
do
|
||||
local s = tag({ { "Title", "Old" } }, 1000, false)
|
||||
local tags, info = ApeTag.find(buffer.fromstring(s), 0, #s)
|
||||
t:check(tags and tags.title == "Old" and info.start == 0 and info.version == 1000, "v1")
|
||||
t:eq(ApeTag.find(buffer.fromstring("no tag here at all, not even close to it"), 0, 40), nil, "none")
|
||||
end
|
||||
-- malformed: a huge item length, a huge count, a size larger than the file
|
||||
do
|
||||
local s = tag({ { "Title", "Hello" } }, 2000, false)
|
||||
local bad = string.sub(s, 1, 0) .. u32(0x7FFFFFFF) .. string.sub(s, 5)
|
||||
local tags = ApeTag.find(buffer.fromstring(bad), 0, #bad)
|
||||
t:check(tags and tags.title == nil, "bad item length stops")
|
||||
local big = string.sub(s, 1, #s - 32 + 12) .. u32(1e9) .. string.sub(s, #s - 32 + 17)
|
||||
t:eq(ApeTag.find(buffer.fromstring(big), 0, #big), nil, "size past the start")
|
||||
end
|
||||
|
||||
local samples = {
|
||||
buffer.fromstring("data" .. tag({ { "Title", "Hello" }, { "Artist", "X" } }, 2000, true)),
|
||||
buffer.fromstring(tag({ { "A", "b" } }, 1000, false) .. "TAG" .. string.rep("\0", 125)),
|
||||
}
|
||||
t:fuzz("find", function(b: buffer)
|
||||
return ApeTag.find(b, 0, buffer.len(b))
|
||||
end, samples, { mutations = 400 })
|
||||
t:done()
|
||||
@@ -0,0 +1,58 @@
|
||||
-- codecs/audio/Musepack tests: the codec API under audio/mpc: magic, the stream fields of SV7 / SV8 fixtures (version,
|
||||
-- rate, channels, band count, packet layout, gapless length), packets decoded by hand against libmpcdec (the
|
||||
-- synthesis delay at the front), out-of-order decodes, and a fuzz pass over open + every packet. Fixtures:
|
||||
-- tools/fixtures/mpc.py.
|
||||
local T = require("@tests/T")
|
||||
local Musepack = require("@formats/codecs/audio/Musepack")
|
||||
local Fx = require("@tests/gen/mpc_cases")
|
||||
|
||||
local t = T.suite("codecs/musepack")
|
||||
|
||||
for _, c in Fx do
|
||||
local b = buffer.fromstring(c.data)
|
||||
t:eq(Musepack.magic(b, 0, buffer.len(b)), c.version, c.name .. ": magic")
|
||||
local s, err = Musepack.open(b, 0, buffer.len(b))
|
||||
t:check(s, c.name .. ": open: " .. tostring(err))
|
||||
if not s then
|
||||
continue
|
||||
end
|
||||
t:check(s.version == c.version and s.sampleRate == c.rate and s.channels == c.channels, c.name .. ": fields")
|
||||
t:eq(s.frames, c.frames, c.name .. ": gapless length")
|
||||
t:check(s.maxBand >= 1 and s.maxBand <= 31 and s.count >= 1 and s.skip >= 481, c.name .. ": bands / packets")
|
||||
t:eq(s.stop - s.skip, c.frames, c.name .. ": stop - skip = length")
|
||||
-- the first packet by hand: output frame k is raw frame skip + k
|
||||
local n = s.decode(1)
|
||||
t:check(n and n > 0 and n % 1152 == 0 and n <= s.blockFrames * 1152, c.name .. ": first packet length")
|
||||
local pcm = s.pcm()
|
||||
local rb = buffer.fromstring(c.ref)
|
||||
local ch = c.channels
|
||||
local maxd = 0
|
||||
for k = 0, math.min(500, (n :: number) - s.skip - 1) do
|
||||
for j = 1, ch do
|
||||
maxd = math.max(maxd, math.abs(pcm[(s.skip + k) * ch + j] - buffer.readf32(rb, (k * ch + j - 1) * 4)))
|
||||
end
|
||||
end
|
||||
t:check(maxd < 1e-5, string.format("%s: first packet matches libmpcdec (max diff %g)", c.name, maxd))
|
||||
if s.count >= 3 then
|
||||
s.reset()
|
||||
t:check(s.decode(3) ~= nil and s.decode(1) ~= nil, c.name .. ": decodes out of order after reset")
|
||||
end
|
||||
end
|
||||
|
||||
local samples = {}
|
||||
for _, c in Fx do
|
||||
table.insert(samples, string.sub(c.data, 1, 2500))
|
||||
end
|
||||
t:fuzz("open + every packet", function(b: buffer)
|
||||
local s = Musepack.open(b, 0, buffer.len(b))
|
||||
if s then
|
||||
for i = 1, math.min(s.count, 40) do
|
||||
if not s.decode(i) then
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
return s
|
||||
end, samples, { mutations = 25, keepHeader = 4 })
|
||||
|
||||
t:done()
|
||||
Reference in new issue
Block a user