209 lines
7.6 KiB
Luau
209 lines
7.6 KiB
Luau
-- audio/m4a tests: ALAC from ffmpeg bit-exact; AAC from ffmpeg and fdkaac against ffmpeg's float decode (gapless).
|
|
-- our ALAC writer's bytes as ffmpeg decoded them bit-exactly and MP4Box accepted in the fixture step.
|
|
-- Fixtures: tools/fixtures/m4a.py.
|
|
local T = require("@tests/T")
|
|
local M4a = require("@rosetta/audio/m4a")
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local Crc32 = require("@rosetta/core/Crc32")
|
|
local Err = require("@rosetta/core/Err")
|
|
local Signal = require("@tests/audio/_signal")
|
|
local Cases = require("@tests/audio/_encode_cases")
|
|
local Alac = require("@tests/gen/m4a_alac")
|
|
local Aac = require("@tests/gen/m4a_aac")
|
|
local He = require("@tests/gen/m4a_he")
|
|
local Ours = require("@tests/gen/m4a_ours")
|
|
|
|
local t = T.suite("m4a")
|
|
|
|
local function reader(s: string | buffer): Bytes.Reader
|
|
return Bytes.reader(if type(s) == "string" then buffer.fromstring(s) else s)
|
|
end
|
|
|
|
local function intCrc(a: any, bits: number): number
|
|
local n = a.frames * a.channels
|
|
local b = buffer.create(n * 4)
|
|
local scale = 2 ^ (bits - 1)
|
|
for i = 0, n - 1 do
|
|
buffer.writei32(b, i * 4, math.floor(buffer.readf32(a.samples, i * 4) * scale + 0.5))
|
|
end
|
|
return Crc32.of(b)
|
|
end
|
|
|
|
local function snr(a: any, ref: string): (number, number)
|
|
local n = math.min(a.frames * a.channels, #ref // 4)
|
|
local rb = buffer.fromstring(ref)
|
|
local sig, err = 0, 0
|
|
for i = 0, n - 1 do
|
|
local r = buffer.readf32(rb, i * 4)
|
|
local d = buffer.readf32(a.samples, i * 4) - r
|
|
sig += r * r
|
|
err += d * d
|
|
end
|
|
if err == 0 then
|
|
return math.huge, n
|
|
end
|
|
return 10 * math.log10(sig / err), n
|
|
end
|
|
|
|
t:eq(M4a.name, "m4a", "name")
|
|
t:eq(M4a.category, "audio", "category")
|
|
t:check(M4a.caps.decode and M4a.caps.encode, "caps")
|
|
|
|
for _, c in Alac do
|
|
if c.container ~= "m4a" then
|
|
continue
|
|
end
|
|
local r = reader(c.data)
|
|
t:check(M4a.sniff(r) == 1, c.name .. ": sniff")
|
|
local a, err = M4a.decode(r)
|
|
t:check(a, c.name .. ": " .. tostring(err))
|
|
t:check(a.channels == c.channels and a.sampleRate == c.rate, c.name .. ": format")
|
|
t:eq(a.frames, c.frames, c.name .. ": frames")
|
|
t:eq(intCrc(a, c.bits), c.crc, c.name .. ": samples bit-exact")
|
|
t:eq(a.meta.source.codec, "alac", c.name .. ": codec")
|
|
end
|
|
t:note("%d ffmpeg ALAC files decode bit-exactly", #Alac - 1)
|
|
|
|
local worst = math.huge
|
|
for _, c in Aac do
|
|
if c.container ~= "m4a" then
|
|
continue
|
|
end
|
|
local a, err = M4a.decode(reader(c.data))
|
|
t:check(a, c.name .. ": " .. tostring(err))
|
|
t:check(a.channels == c.channels and a.sampleRate == c.rate, c.name .. ": format")
|
|
t:eq(a.frames, c.frames, c.name .. ": gapless length")
|
|
local s = snr(a, c.ref)
|
|
worst = math.min(worst, s)
|
|
t:check(s > 90, string.format("%s: SNR %.1f dB vs ffmpeg", c.name, s))
|
|
t:note("%s: %d ch %d Hz, %d frames, SNR %.1f dB vs ffmpeg", c.name, c.channels, c.rate, a.frames, s)
|
|
end
|
|
|
|
-- HE-AAC: SBR output at the full rate (tests/audio/heaac.luau checks it against ffmpeg); opts.sbr = false: the core
|
|
for _, c in He do
|
|
if string.sub(c.name, -4) ~= ".m4a" then
|
|
continue
|
|
end
|
|
for _, sbr in { true, false } do
|
|
local dec, err = M4a.open(reader(c.data), { sbr = sbr })
|
|
t:check(dec, c.name .. ": " .. tostring(err))
|
|
t:eq(dec.sampleRate, if sbr then c.rate else c.rate // 2, c.name .. (if sbr then ": SBR rate" else ": core rate"))
|
|
local b, n = dec:read(8192)
|
|
local e = 0
|
|
for i = 0, n * dec.channels - 1 do
|
|
e += buffer.readf32(b, i * 4) ^ 2
|
|
end
|
|
t:check(n == 8192 and e > 1, c.name .. ": decodes (energy " .. e .. ")")
|
|
end
|
|
end
|
|
|
|
do
|
|
local byName = {}
|
|
for _, o in Ours do
|
|
byName[o.name] = o
|
|
end
|
|
for _, c in Cases do
|
|
if c.container ~= "m4a" then
|
|
continue
|
|
end
|
|
local o = byName[c.name]
|
|
t:check(o and o.ffmpeg and o.mp4box and o.codec == "alac", c.name .. ": ffmpeg decoded it bit-exactly, MP4Box read it")
|
|
t:eq(o.frames, c.frames, c.name .. ": ffmpeg's length")
|
|
local a = Signal.audio(c.rate, c.frames, c.channels, c.bits, c.seed)
|
|
a.tags = c.tags or {}
|
|
local file, err = M4a.encode(a, { bitDepth = c.bits })
|
|
t:check(file, c.name .. ": encode: " .. tostring(err))
|
|
t:eq(string.format("%08x", Crc32.of(file :: buffer)), string.format("%08x", o.crc), c.name .. ": the validated bytes")
|
|
-- and our own decoder gets the integers back
|
|
local back = M4a.decode(Bytes.reader(file :: buffer)) :: any
|
|
local ints = Signal.ints(c.frames, c.channels, c.bits, c.seed)
|
|
local ok = back.frames == c.frames
|
|
local scale = 2 ^ (c.bits - 1)
|
|
for i = 1, #ints, 7 do
|
|
ok = ok and math.floor(buffer.readf32(back.samples, (i - 1) * 4) * scale + 0.5) == ints[i]
|
|
end
|
|
t:check(ok, c.name .. ": round trip")
|
|
if c.tags then
|
|
for k, v in c.tags do
|
|
t:eq(back.tags[k], v, c.name .. ": tag " .. k)
|
|
end
|
|
t:eq(o.tags.title, c.tags.title, c.name .. ": ffprobe title")
|
|
t:eq(o.tags.track, c.tags.track .. "/" .. c.tags.tracktotal, c.name .. ": ffprobe track")
|
|
t:eq(o.tags.artist, c.tags.artist, c.name .. ": ffprobe artist")
|
|
end
|
|
t:note("%s: %d bytes (%.0f%% of PCM)", c.name, buffer.len(file :: buffer), 100 * buffer.len(file :: buffer) / (c.frames * c.channels * c.bits / 8))
|
|
end
|
|
end
|
|
|
|
do
|
|
local a = Signal.audio(44100, 5000, 2, 16, 9)
|
|
a.meta.pictures = { { mime = "image/png", data = buffer.fromstring("\137PNG fake") } }
|
|
a.tags = { title = "Pic" }
|
|
local file = M4a.encode(a) :: buffer
|
|
local back = M4a.decode(Bytes.reader(file)) :: any
|
|
t:check(back.meta.pictures and #back.meta.pictures == 1 and back.meta.pictures[1].mime == "image/png", "cover art round trip")
|
|
local dec = M4a.open(Bytes.reader(file)) :: any
|
|
local again = M4a.encode(dec) :: buffer
|
|
local back2 = M4a.decode(Bytes.reader(again)) :: any
|
|
t:check(back2.frames == 5000 and buffer.tostring(back2.samples) == buffer.tostring(back.samples), "encode from a streaming decoder")
|
|
local _, e = M4a.encode({} :: any)
|
|
t:check(e and e.code == Err.ARG, "encode of a non-audio returns Err.ARG")
|
|
local _, e2 = M4a.encode(a, { bitDepth = 12 })
|
|
t:check(e2 and e2.code == Err.ARG, "bad bitDepth returns Err.ARG")
|
|
end
|
|
|
|
-- streaming + seek (AAC without PNS: its noise is random, so a seek changes it; and ALAC)
|
|
for _, c in { Aac[1], Alac[1] } do
|
|
local whole = M4a.decode(reader(c.data)) :: any
|
|
local dec = M4a.open(reader(c.data)) :: any
|
|
t:eq(dec.frames, whole.frames, c.name .. ": decoder frames")
|
|
local got, out = 0, buffer.create(whole.frames * whole.channels * 4)
|
|
while true do
|
|
local b, n = dec:read(777)
|
|
if n == 0 then
|
|
break
|
|
end
|
|
buffer.copy(out, got * whole.channels * 4, b, 0, n * whole.channels * 4)
|
|
got += n
|
|
end
|
|
t:check(got == whole.frames and buffer.tostring(out) == buffer.tostring(whole.samples), c.name .. ": chunked reads equal the whole decode")
|
|
for _, at in { 0.1, 0.0, 0.05 } do
|
|
dec:seek(at)
|
|
local b, n = dec:read(500)
|
|
local f0 = math.floor(at * whole.sampleRate + 0.5)
|
|
local maxd = 0
|
|
for i = 0, n * whole.channels - 1 do
|
|
maxd = math.max(maxd, math.abs(buffer.readf32(b, i * 4) - buffer.readf32(whole.samples, (f0 * whole.channels + i) * 4)))
|
|
end
|
|
t:check(n == 500 and maxd < 1e-6, string.format("%s: seek %.2f s (max diff %g)", c.name, at, maxd))
|
|
t:check(math.abs(dec:position() - (f0 + 500) / whole.sampleRate) < 1e-9, c.name .. ": position after seek")
|
|
end
|
|
end
|
|
|
|
do
|
|
local _, e = M4a.decode(reader("\0\0\0\8ftypM4A "))
|
|
t:check(e ~= nil, "a lone ftyp: " .. tostring(e))
|
|
local _, e2 = M4a.decode(reader("hello world, not an mp4"))
|
|
t:check(e2 ~= nil and Err.is(e2), "junk: " .. tostring(e2))
|
|
t:eq(M4a.sniff(reader("hello")), 0, "sniff junk")
|
|
end
|
|
|
|
do
|
|
local samples = {}
|
|
for i, c in Alac do
|
|
if c.container == "m4a" and i <= 2 then
|
|
table.insert(samples, string.sub(c.data, 1, 12000))
|
|
end
|
|
end
|
|
for i, c in Aac do
|
|
if c.container == "m4a" and i <= 2 then
|
|
table.insert(samples, string.sub(c.data, 1, 9000))
|
|
end
|
|
end
|
|
t:fuzz("decode", function(b: buffer)
|
|
return M4a.decode(Bytes.reader(b))
|
|
end, samples, { mutations = 25 })
|
|
end
|
|
|
|
t:done()
|