aiff: COMM counts ima4 packets; caf: ima4 through Adpcm/Decoder.blocks (exact seeking), and a constant-size packet stream whose pakt lists no packets now uses bytesPerPacket (was 0 frames). Bit-exact with ffmpeg.
186 lines
8.2 KiB
Luau
186 lines
8.2 KiB
Luau
-- 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 / ima4, 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 mac = string.sub(c, 1, p - 1) .. "MAC3" .. string.sub(c, p + 4)
|
|
local _, e2 = Aiff.decode(reader(mac))
|
|
t:check(e2 and e2.code == Err.UNSUPPORTED, "MAC3: UNSUPPORTED: " .. tostring(e2))
|
|
local i4 = Aiff.decode(reader(case("ffmpeg-ima4").data)) :: any
|
|
t:check(i4.meta.source.compression == "ima4" and i4.frames % 64 == 0, "ima4 meta / whole packets")
|
|
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()
|