ws-audio: merge master (ws/m4a codecs/Mdct already in use since 6535a3c; audio/opus encoder, data, vec, 3d, webp)
This commit is contained in:
commit
43b8f4b4f5
400 files changed
+82546
-38
No files matched your search
@@ -0,0 +1,12 @@
|
||||
-- tests/audio/_encode_cases: the files our ALAC / CAF writers produce for the reference check (tools/fixtures/m4a.py
|
||||
-- runs tools/fixtures/_m4a_encode.luau over them; tests/audio/m4a.luau and caf.luau re-encode and compare).
|
||||
return {
|
||||
{ name = "ours_alac16_stereo", container = "m4a", rate = 44100, frames = 4096 * 3 + 1234, channels = 2, bits = 16, seed = 1, tags = { title = "Our Title \u{e9}", artist = "formats-luau", album = "Tests", track = "5", tracktotal = "10", date = "2026", genre = "Test", comment = "round trip" } },
|
||||
{ name = "ours_alac16_mono", container = "m4a", rate = 22050, frames = 5000, channels = 1, bits = 16, seed = 2 },
|
||||
{ name = "ours_alac24_stereo", container = "m4a", rate = 48000, frames = 9000, channels = 2, bits = 24, seed = 3 },
|
||||
{ name = "ours_alac16_6ch", container = "m4a", rate = 48000, frames = 4500, channels = 6, bits = 16, seed = 4 },
|
||||
{ name = "ours_alac20_stereo", container = "m4a", rate = 96000, frames = 3000, channels = 2, bits = 20, seed = 5 },
|
||||
{ name = "ours_caf_alac16", container = "caf", codec = "alac", rate = 44100, frames = 7000, channels = 2, bits = 16, seed = 6 },
|
||||
{ name = "ours_caf_pcm16", container = "caf", codec = "pcm", rate = 44100, frames = 3000, channels = 2, bits = 16, seed = 7 },
|
||||
{ name = "ours_caf_pcm24", container = "caf", codec = "pcm", rate = 48000, frames = 2000, channels = 1, bits = 24, seed = 8 },
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
-- tests/audio/_opus_cases: the inputs of the CELT encoder reference check (tools/fixtures/opus_celt.py runs
|
||||
-- tools/fixtures/_opus_encode.luau over them; tests/audio/opus_encode.luau re-encodes and compares): a deterministic
|
||||
-- music-like signal (chord + vibrato + chirp + clicks + a silent stretch) at a given rate / channel count.
|
||||
local Types = require("@formats/Types")
|
||||
|
||||
local Cases = {}
|
||||
|
||||
Cases.list = {
|
||||
{ name = "celt_mono_64k", rate = 48000, channels = 1, seconds = 1.5, bitrate = 64000, minSnr = 15 },
|
||||
{ name = "celt_stereo_128k", rate = 48000, channels = 2, seconds = 1.5, bitrate = 128000, minSnr = 15 },
|
||||
{ name = "celt_stereo_256k", rate = 48000, channels = 2, seconds = 1, bitrate = 256000, minSnr = 22 },
|
||||
{ name = "celt_mono_24k", rate = 48000, channels = 1, seconds = 1, bitrate = 24000, minSnr = 8 },
|
||||
{ name = "celt_44k_stereo_96k", rate = 44100, channels = 2, seconds = 1, bitrate = 96000, minSnr = 0, tags = { title = "Celt \u{e9}", artist = "formats-luau", album = "Opus" } },
|
||||
{ name = "celt_short", rate = 48000, channels = 1, seconds = 0.01, bitrate = 48000, minSnr = -100 },
|
||||
}
|
||||
|
||||
function Cases.signal(c: any): Types.Audio
|
||||
local n = math.floor(c.seconds * c.rate + 0.5)
|
||||
local a = Types.Audio.new(c.rate, c.channels, n)
|
||||
local s = 7
|
||||
for i = 0, n - 1 do
|
||||
local t = i / c.rate
|
||||
for ch = 0, c.channels - 1 do
|
||||
s = (s * 48271) % 2147483647
|
||||
local v = 0.18 * math.sin(2 * math.pi * 220 * t + 2 * math.sin(2 * math.pi * 5 * t))
|
||||
v += 0.12 * math.sin(2 * math.pi * (277.2 + ch * 2) * t) + 0.1 * math.sin(2 * math.pi * 329.6 * t)
|
||||
v += 0.08 * math.sin(2 * math.pi * (1000 + 3000 * t) * t)
|
||||
v += 0.01 * (s / 2147483647 - 0.5)
|
||||
if i % (c.rate // 4) < 30 then
|
||||
v += 0.4 * (if (i // 5) % 2 == 0 then 1 else -1)
|
||||
end
|
||||
if t > 0.6 and t < 0.75 then
|
||||
v = 0 -- digital silence (the encoder's silence frames)
|
||||
end
|
||||
buffer.writef32(a.samples, (i * c.channels + ch) * 4, v)
|
||||
end
|
||||
end
|
||||
a.tags = c.tags or {}
|
||||
return a
|
||||
end
|
||||
|
||||
return Cases
|
||||
@@ -0,0 +1,42 @@
|
||||
-- tests/audio/_signal: the deterministic integer test signal of tools/fixtures/_m4a_signal.py (the same numbers), as
|
||||
-- an interleaved integer array and as a Types.Audio (f32, exact for up to 24 bits). Used by the m4a / caf tests and the
|
||||
-- fixture-time encoder runs (tools/fixtures/_m4a_encode.luau).
|
||||
local Types = require("@formats/Types")
|
||||
|
||||
local Signal = {}
|
||||
|
||||
function Signal.ints(frames: number, channels: number, bits: number, seed: number): { number }
|
||||
local s = seed
|
||||
local peak = 2 ^ (bits - 1) - 1
|
||||
local out = table.create(frames * channels, 0)
|
||||
local k = 1
|
||||
for i = 0, frames - 1 do
|
||||
for c = 0, channels - 1 do
|
||||
s = (s * 48271) % 2147483647
|
||||
local p1 = 97 + 31 * c
|
||||
local tri = math.abs((i % p1) * 2 - p1) * 2 - p1
|
||||
local v = tri * (peak // (4 * p1))
|
||||
local p2 = 1500 + 200 * c
|
||||
v += if (i // p2) % 2 == 0 then peak // 6 else -(peak // 6)
|
||||
v += ((s // 65536) % 4096 - 2048) * (peak // 65536 + 1)
|
||||
if i % 7001 < 3 then
|
||||
v += peak // 2
|
||||
end
|
||||
out[k] = math.clamp(v, -peak - 1, peak)
|
||||
k += 1
|
||||
end
|
||||
end
|
||||
return out
|
||||
end
|
||||
|
||||
function Signal.audio(rate: number, frames: number, channels: number, bits: number, seed: number): Types.Audio
|
||||
local ints = Signal.ints(frames, channels, bits, seed)
|
||||
local a = Types.Audio.new(rate, channels, frames)
|
||||
local scale = 1 / 2 ^ (bits - 1)
|
||||
for i, v in ints do
|
||||
buffer.writef32(a.samples, (i - 1) * 4, v * scale)
|
||||
end
|
||||
return a
|
||||
end
|
||||
|
||||
return Signal
|
||||
@@ -0,0 +1,117 @@
|
||||
-- audio/aac tests: ADTS streams from ffmpeg and fdkaac (stereo, mono 32 kHz) against ffmpeg's float decode (SNR,
|
||||
-- length); an HE-AAC ADTS stream plays its core; a leading ID3v2 tag (skipped, text frames as tags) and garbage before
|
||||
-- the first frame (resync); a truncated last frame; streaming + seek; sniff; errors; the fuzz no-throw pass.
|
||||
-- Fixtures: tools/fixtures/m4a.py.
|
||||
local T = require("@tests/T")
|
||||
local AacF = require("@formats/audio/aac")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Err = require("@formats/core/Err")
|
||||
local Fx = require("@tests/gen/m4a_aac")
|
||||
local He = require("@tests/gen/m4a_he")
|
||||
|
||||
local t = T.suite("aac")
|
||||
|
||||
local function reader(s: string): Bytes.Reader
|
||||
return Bytes.reader(buffer.fromstring(s))
|
||||
end
|
||||
|
||||
local function snr(a: any, ref: string, skip: number?): number
|
||||
local off = (skip or 0) * a.channels
|
||||
local n = math.min(a.frames * a.channels - off, #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, (off + i) * 4) - r
|
||||
sig += r * r
|
||||
err += d * d
|
||||
end
|
||||
return if err == 0 then math.huge else 10 * math.log10(sig / err)
|
||||
end
|
||||
|
||||
t:eq(AacF.name, "aac", "name")
|
||||
t:check(AacF.caps.decode and not AacF.caps.encode and AacF.encode == nil, "caps: decode only")
|
||||
|
||||
local first
|
||||
for _, c in Fx do
|
||||
if c.container ~= "adts" then
|
||||
continue
|
||||
end
|
||||
first = first or c
|
||||
local r = reader(c.data)
|
||||
t:check(AacF.sniff(r) >= 0.9, c.name .. ": sniff")
|
||||
local a, err = AacF.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 (ADTS keeps the priming)")
|
||||
local s = snr(a, c.ref)
|
||||
t:check(s > 90, string.format("%s: SNR %.1f dB", 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 ADTS: the core
|
||||
for _, c in He do
|
||||
if string.sub(c.name, -4) == ".aac" then
|
||||
local a, err = AacF.decode(reader(c.data))
|
||||
t:check(a and a.sampleRate == c.rate // 2 and a.frames > 0, c.name .. ": core decode: " .. tostring(err))
|
||||
end
|
||||
end
|
||||
|
||||
-- ID3v2 in front + garbage, truncated tail
|
||||
do
|
||||
local function frame(id: string, text: string): string
|
||||
local body = "\3" .. text
|
||||
local n = #body
|
||||
return id .. string.char(0, 0, n // 128, n % 128) .. "\0\0" .. body
|
||||
end
|
||||
local frames = frame("TIT2", "Adts Title") .. frame("TPE1", "Someone")
|
||||
local n = #frames
|
||||
local id3 = "ID3\4\0\0" .. string.char(0, 0, n // 128, n % 128) .. frames
|
||||
local data = id3 .. "\0\1\2garbage" .. first.data
|
||||
local a, err = AacF.decode(reader(data))
|
||||
t:check(a, "id3 + garbage: " .. tostring(err))
|
||||
t:check(a.tags.title == "Adts Title" and a.tags.artist == "Someone", "id3 tags")
|
||||
t:eq(a.frames, first.frames, "id3 + garbage: same frames")
|
||||
t:check(snr(a, first.ref) > 90, "id3 + garbage: same audio")
|
||||
local cut = AacF.decode(reader(string.sub(first.data, 1, #first.data - 5))) :: any
|
||||
t:check(cut and cut.frames == first.frames - 1024, "truncated last frame dropped")
|
||||
end
|
||||
|
||||
-- streaming + seek
|
||||
do
|
||||
local whole = AacF.decode(reader(first.data)) :: any
|
||||
local dec = AacF.open(reader(first.data)) :: any
|
||||
for _, at in { 0.3, 0.0, 0.5123 } do
|
||||
dec:seek(at)
|
||||
local b, n = dec:read(600)
|
||||
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
|
||||
-- (the stream has no PNS: noise substitution would draw different noise after a seek)
|
||||
t:check(n == 600 and maxd < 1e-6, string.format("seek %.3f s (max diff %g)", at, maxd))
|
||||
end
|
||||
end
|
||||
|
||||
-- errors
|
||||
do
|
||||
t:eq(AacF.sniff(reader("RIFF....WAVE")), 0, "sniff a WAV")
|
||||
local _, e = AacF.decode(reader(string.rep("\0", 100)))
|
||||
t:check(e and e.code == Err.CORRUPT, "no frames: " .. tostring(e))
|
||||
end
|
||||
|
||||
-- fuzz
|
||||
do
|
||||
local samples = {}
|
||||
for _, c in Fx do
|
||||
if c.container == "adts" then
|
||||
table.insert(samples, string.sub(c.data, 1, 6000))
|
||||
end
|
||||
end
|
||||
t:fuzz("decode", function(b: buffer)
|
||||
return AacF.decode(Bytes.reader(b))
|
||||
end, samples, { mutations = 30 })
|
||||
end
|
||||
|
||||
t:done()
|
||||
@@ -0,0 +1,177 @@
|
||||
-- audio/caf tests: ALAC CAF from ffmpeg bit-exact; AAC CAF (ffmpeg's ADTS frames wrapped by the fixture) against
|
||||
-- ffmpeg's float decode; our CAF writer (PCM 16 / 24-bit, ALAC; re-encoded here and compared with the bytes ffmpeg
|
||||
-- decoded bit-exactly); PCM layouts (little / big endian, float) through our writer + a hand-built header; info tags;
|
||||
-- seek; errors; the fuzz no-throw pass. Fixtures: tools/fixtures/m4a.py.
|
||||
local T = require("@tests/T")
|
||||
local Caf = require("@formats/audio/caf")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Crc32 = require("@formats/core/Crc32")
|
||||
local Err = require("@formats/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 Ours = require("@tests/gen/m4a_ours")
|
||||
|
||||
local t = T.suite("caf")
|
||||
|
||||
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
|
||||
|
||||
t:eq(Caf.name, "caf", "name")
|
||||
t:check(Caf.caps.decode and Caf.caps.encode, "caps")
|
||||
|
||||
for _, c in Alac do
|
||||
if c.container == "caf" then
|
||||
local r = reader(c.data)
|
||||
t:eq(Caf.sniff(r), 1, c.name .. ": sniff")
|
||||
local a, err = Caf.decode(r)
|
||||
t:check(a, c.name .. ": " .. tostring(err))
|
||||
t:check(a.channels == c.channels and a.sampleRate == c.rate and a.frames == c.frames, c.name .. ": format / length (pakt)")
|
||||
t:eq(intCrc(a, c.bits), c.crc, c.name .. ": bit-exact")
|
||||
end
|
||||
end
|
||||
|
||||
for _, c in Aac do
|
||||
if c.container == "caf" then
|
||||
local a, err = Caf.decode(reader(c.data))
|
||||
t:check(a, c.name .. ": " .. tostring(err))
|
||||
t:eq(a.frames, c.frames, c.name .. ": frames")
|
||||
local rb = buffer.fromstring(c.ref)
|
||||
local sig, e = 0, 0
|
||||
for i = 0, a.frames * a.channels - 1 do
|
||||
local r = buffer.readf32(rb, i * 4)
|
||||
sig += r * r
|
||||
e += (buffer.readf32(a.samples, i * 4) - r) ^ 2
|
||||
end
|
||||
local snr = if e == 0 then math.huge else 10 * math.log10(sig / e)
|
||||
t:check(snr > 90, string.format("%s: SNR %.1f dB", c.name, snr))
|
||||
t:note("%s: SNR %.1f dB vs ffmpeg", c.name, snr)
|
||||
end
|
||||
end
|
||||
|
||||
-- our writer: the bytes ffmpeg validated
|
||||
do
|
||||
local byName = {}
|
||||
for _, o in Ours do
|
||||
byName[o.name] = o
|
||||
end
|
||||
for _, c in Cases do
|
||||
if c.container ~= "caf" then
|
||||
continue
|
||||
end
|
||||
local o = byName[c.name]
|
||||
t:check(o and o.ffmpeg, c.name .. ": ffmpeg decoded it bit-exactly")
|
||||
local a = Signal.audio(c.rate, c.frames, c.channels, c.bits, c.seed)
|
||||
local file = Caf.encode(a, { codec = c.codec, bitDepth = c.bits }) :: buffer
|
||||
t:eq(string.format("%08x", Crc32.of(file)), string.format("%08x", o.crc), c.name .. ": the validated bytes")
|
||||
local back = Caf.decode(reader(file)) :: any
|
||||
t:check(back and back.frames == c.frames, c.name .. ": our length")
|
||||
local ints = Signal.ints(c.frames, c.channels, c.bits, c.seed)
|
||||
local ok = true
|
||||
local scale = 2 ^ (c.bits - 1)
|
||||
for i = 1, #ints 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")
|
||||
end
|
||||
end
|
||||
|
||||
-- float PCM, tags, a decoder as the source, a hand-built little-endian header
|
||||
do
|
||||
local a = Signal.audio(32000, 3000, 3, 16, 12)
|
||||
a.tags = { title = "Caf Title", artist = "A", date = "1999", comment = "c", custom = "x" }
|
||||
local file = Caf.encode(a, { codec = "pcm", bitDepth = 32 }) :: buffer
|
||||
local back = Caf.decode(reader(file)) :: any
|
||||
t:check(back.frames == 3000 and buffer.tostring(back.samples) == buffer.tostring(a.samples), "float PCM round trip")
|
||||
for k, v in a.tags do
|
||||
t:eq(back.tags[k], v, "tag " .. k)
|
||||
end
|
||||
local dec = Caf.open(reader(file)) :: any
|
||||
local again = Caf.encode(dec, { codec = "alac" }) :: buffer
|
||||
local b2 = Caf.decode(reader(again)) :: any
|
||||
t:check(b2.frames == 3000 and buffer.tostring(b2.samples) == buffer.tostring(a.samples), "ALAC from a streaming decoder")
|
||||
-- little-endian 24-bit integer PCM, data chunk of unknown size (-1)
|
||||
local w = Bytes.writer(64)
|
||||
w:string("caff")
|
||||
w:u16be(1)
|
||||
w:u16be(0)
|
||||
w:string("desc")
|
||||
w:u64be(32)
|
||||
w:f64be(8000)
|
||||
w:string("lpcm")
|
||||
w:u32be(2)
|
||||
w:u32be(3)
|
||||
w:u32be(1)
|
||||
w:u32be(1)
|
||||
w:u32be(24)
|
||||
w:string("data")
|
||||
w:u32be(0xFFFFFFFF)
|
||||
w:u32be(0xFFFFFFFF)
|
||||
w:u32be(0)
|
||||
for _, v in { 0, 4194304, -4194304, 8388607, -8388608 } do
|
||||
w:u24le(v % 16777216)
|
||||
end
|
||||
local le = Caf.decode(Bytes.reader(w:finish())) :: any
|
||||
t:check(le and le.frames == 5, "24-bit LE, data size -1")
|
||||
t:check(le and math.abs(buffer.readf32(le.samples, 4) - 0.5) < 1e-7 and math.abs(buffer.readf32(le.samples, 16) + 1) < 1e-7, "24-bit LE values")
|
||||
end
|
||||
|
||||
-- seek
|
||||
do
|
||||
local c = Alac[#Alac]
|
||||
local whole = Caf.decode(reader(c.data)) :: any
|
||||
local dec = Caf.open(reader(c.data)) :: any
|
||||
for _, at in { 0.1, 0.0, 0.15 } do
|
||||
dec:seek(at)
|
||||
local b, n = dec:read(400)
|
||||
local f0 = math.floor(at * whole.sampleRate + 0.5)
|
||||
local same = n == 400
|
||||
for i = 0, n * whole.channels - 1 do
|
||||
same = same and buffer.readf32(b, i * 4) == buffer.readf32(whole.samples, (f0 * whole.channels + i) * 4)
|
||||
end
|
||||
t:check(same, string.format("seek %.2f s", at))
|
||||
end
|
||||
end
|
||||
|
||||
-- errors
|
||||
do
|
||||
t:eq(Caf.sniff(reader("caff\0\2\0\0")), 0, "sniff: wrong version")
|
||||
local _, e = Caf.decode(reader("caff\0\1\0\0"))
|
||||
t:check(e and e.code == Err.CORRUPT, "no desc: " .. tostring(e))
|
||||
local _, e2 = Caf.encode(Signal.audio(8000, 10, 1, 16, 1), { codec = "mp3" } :: any)
|
||||
t:check(e2 and e2.code == Err.ARG, "bad codec: " .. tostring(e2))
|
||||
end
|
||||
|
||||
-- fuzz
|
||||
do
|
||||
local samples = {}
|
||||
for _, c in Alac do
|
||||
if c.container == "caf" then
|
||||
table.insert(samples, string.sub(c.data, 1, 9000))
|
||||
end
|
||||
end
|
||||
for _, c in Aac do
|
||||
if c.container == "caf" then
|
||||
table.insert(samples, string.sub(c.data, 1, 6000))
|
||||
end
|
||||
end
|
||||
local pcm = Caf.encode(Signal.audio(8000, 300, 2, 16, 3), { codec = "pcm" }) :: buffer
|
||||
table.insert(samples, buffer.tostring(pcm))
|
||||
t:fuzz("decode", function(b: buffer)
|
||||
return Caf.decode(Bytes.reader(b))
|
||||
end, samples, { mutations = 30 })
|
||||
end
|
||||
|
||||
t:done()
|
||||
@@ -0,0 +1,218 @@
|
||||
-- audio/m4a tests: ALAC files from ffmpeg decode bit-exactly (16 / 24-bit, mono / stereo / 5.1, partial frames);
|
||||
-- AAC-LC files from ffmpeg and fdkaac (stereo / mono / 5.1, 8 - 96 kHz, PNS / intensity / M/S / TNS) against ffmpeg's
|
||||
-- float decode (SNR, gapless length through the edit list); HE-AAC / HE-AACv2 play their core; our ALAC writer
|
||||
-- (re-encoded here, compared with the bytes ffmpeg decoded bit-exactly and MP4Box accepted in the fixture step); tags
|
||||
-- and cover art round trips; streaming reads, seeking; the contract; the fuzz no-throw pass.
|
||||
-- Fixtures: tools/fixtures/m4a.py.
|
||||
local T = require("@tests/T")
|
||||
local M4a = require("@formats/audio/m4a")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Crc32 = require("@formats/core/Crc32")
|
||||
local Err = require("@formats/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
|
||||
|
||||
-- the integers of a decoded Audio (exact for <= 24-bit sources), CRC-32 as little-endian int32
|
||||
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
|
||||
|
||||
-- the contract
|
||||
t:eq(M4a.name, "m4a", "name")
|
||||
t:eq(M4a.category, "audio", "category")
|
||||
t:check(M4a.caps.decode and M4a.caps.encode, "caps")
|
||||
|
||||
-- ALAC from ffmpeg: bit-exact
|
||||
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)
|
||||
|
||||
-- AAC: against ffmpeg's float decode
|
||||
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: the AAC-LC core at half the rate, SBR seen
|
||||
for _, c in He do
|
||||
if string.sub(c.name, -4) ~= ".m4a" then
|
||||
continue
|
||||
end
|
||||
local dec, err = M4a.open(reader(c.data))
|
||||
t:check(dec, c.name .. ": " .. tostring(err))
|
||||
t:eq(dec.sampleRate, c.rate // 2, c.name .. ": 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 .. ": the core decodes (energy " .. e .. ")")
|
||||
end
|
||||
|
||||
-- our ALAC writer: the bytes ffmpeg validated
|
||||
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
|
||||
|
||||
-- cover art + streaming a decoder into the encoder
|
||||
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")
|
||||
-- read in odd chunks
|
||||
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
|
||||
|
||||
-- errors
|
||||
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
|
||||
|
||||
-- fuzz
|
||||
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()
|
||||
@@ -81,7 +81,7 @@ end
|
||||
-- ------------------------------------------------------------------------------------------------ contract
|
||||
t:eq(Opus.name, "opus", "name")
|
||||
t:eq(Opus.category, "audio", "category")
|
||||
t:check(Opus.caps.decode and not Opus.caps.encode and Opus.open ~= nil and Opus.decode ~= nil, "caps")
|
||||
t:check(Opus.caps.decode and Opus.caps.encode and Opus.open ~= nil and Opus.decode ~= nil and Opus.encode ~= nil, "caps (the CELT encoder: tests/audio/opus_encode.luau)")
|
||||
t:eq(Opus.sniff(Bytes.reader(FX.celt_st_128.data)), 1, "sniff an Opus file")
|
||||
t:eq(Opus.sniff(Bytes.reader(buffer.fromstring("OggS" .. string.rep("\0", 40)))), 0, "sniff another Ogg")
|
||||
t:eq(Opus.sniff(Bytes.reader(buffer.create(0))), 0, "sniff nothing")
|
||||
|
||||
@@ -0,0 +1,114 @@
|
||||
-- audio/opus encoder tests (the CELT-only writer, ws-m4a; mono and mid / side stereo): our Ogg Opus files (re-encoded here, compared with the bytes
|
||||
-- checked in the fixture step) decode in libopus' opusdec to the input's exact length (pre-skip + end trimming) with a
|
||||
-- minimum SNR, opusinfo finds no problem, ffmpeg reads them, OpusTags carry the tags; our decoder agrees with opusdec;
|
||||
-- resampled 44.1 kHz input; silence frames; streaming input; errors; a fuzz no-throw pass over our files.
|
||||
-- Fixtures: tools/fixtures/opus_celt.py.
|
||||
local T = require("@tests/T")
|
||||
local Opus = require("@formats/audio/opus")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Crc32 = require("@formats/core/Crc32")
|
||||
local Err = require("@formats/core/Err")
|
||||
local Cases = require("@tests/audio/_opus_cases")
|
||||
local Fx = require("@tests/gen/opus_celt")
|
||||
|
||||
local t = T.suite("opus-encode")
|
||||
|
||||
t:check(Opus.caps.encode and Opus.encode ~= nil, "caps: encode")
|
||||
|
||||
local byName = {}
|
||||
for _, f in Fx do
|
||||
byName[f.name] = f
|
||||
end
|
||||
|
||||
local function snr(ref: buffer, refBase: number, got: buffer, n: number): number
|
||||
local s, e = 0, 0
|
||||
for i = 0, n - 1 do
|
||||
local x = buffer.readf32(ref, (refBase + i) * 4)
|
||||
local d = buffer.readf32(got, (refBase + i) * 4) - x
|
||||
s += x * x
|
||||
e += d * d
|
||||
end
|
||||
return if e == 0 then math.huge else 10 * math.log10(s / math.max(e, 1e-30))
|
||||
end
|
||||
|
||||
local files = {}
|
||||
for _, c in Cases.list do
|
||||
local fx = byName[c.name]
|
||||
t:check(fx, c.name .. ": fixture")
|
||||
local input = Cases.signal(c)
|
||||
local file, err = Opus.encode(input, { bitrate = c.bitrate })
|
||||
t:check(file, c.name .. ": encode: " .. tostring(err))
|
||||
table.insert(files, file)
|
||||
t:eq(string.format("%08x", Crc32.of(file :: buffer)), string.format("%08x", fx.crc), c.name .. ": the bytes the reference tools checked")
|
||||
t:check(fx.opusinfo, c.name .. ": opusinfo reports no problem")
|
||||
t:check(fx.ffmpeg, c.name .. ": ffmpeg decodes it")
|
||||
local frames48 = math.floor(input.frames * 48000 / c.rate + 0.5)
|
||||
local ch = c.channels
|
||||
t:eq(#fx.opusdec // (4 * ch), frames48, c.name .. ": opusdec length = the input's (pre-skip + end trimming)")
|
||||
t:eq(fx.ffmpegFrames, frames48, c.name .. ": ffmpeg length")
|
||||
-- our decoder vs opusdec
|
||||
local ours = Opus.decode(Bytes.reader(file :: buffer)) :: any
|
||||
t:eq(ours.frames, frames48, c.name .. ": our decoder's length")
|
||||
local ref = buffer.fromstring(fx.opusdec)
|
||||
local agree = snr(ref, 0, ours.samples, frames48 * ch)
|
||||
t:check(agree > 60, string.format("%s: our decoder vs opusdec %.1f dB", c.name, agree))
|
||||
-- quality vs the input (48 kHz inputs; skip the edges)
|
||||
if c.rate == 48000 and frames48 > 4000 then
|
||||
local s = snr(input.samples, 960 * ch, ref, (frames48 - 1920) * ch)
|
||||
t:check(s >= c.minSnr, string.format("%s: SNR %.1f dB (minimum %d)", c.name, s, c.minSnr))
|
||||
t:note("%s: %d bytes, SNR %.1f dB vs the input, our decoder vs opusdec %.1f dB", c.name, buffer.len(file :: buffer), s, agree)
|
||||
end
|
||||
if c.tags then
|
||||
for k, v in c.tags do
|
||||
t:eq(fx.comments[k], v, c.name .. ": opusinfo comment " .. k)
|
||||
t:eq(ours.tags[k], v, c.name .. ": our tag " .. k)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- silence frames decode to silence
|
||||
do
|
||||
local c = Cases.list[1]
|
||||
local ref = buffer.fromstring(byName[c.name].opusdec)
|
||||
local peak = 0
|
||||
for i = math.floor(0.62 * 48000), math.floor(0.73 * 48000) do
|
||||
peak = math.max(peak, math.abs(buffer.readf32(ref, i * 4)))
|
||||
end
|
||||
t:check(peak < 1e-3, "digital silence stays silent (peak " .. peak .. ")")
|
||||
end
|
||||
|
||||
-- a streaming decoder as the input
|
||||
do
|
||||
local c = Cases.list[1]
|
||||
local dec = Opus.open(Bytes.reader(files[1] :: buffer)) :: any
|
||||
local again, err = Opus.encode(dec, { bitrate = 64000 })
|
||||
t:check(again, "encode from a decoder: " .. tostring(err))
|
||||
local back = Opus.decode(Bytes.reader(again :: buffer)) :: any
|
||||
t:eq(back.frames, math.floor(c.seconds * 48000 + 0.5), "re-encoded length")
|
||||
end
|
||||
|
||||
-- errors
|
||||
do
|
||||
local three = Cases.signal({ rate = 48000, channels = 3, seconds = 0.05 })
|
||||
local _, e = Opus.encode(three)
|
||||
t:check(e and e.code == Err.UNSUPPORTED, "3 channels: " .. tostring(e))
|
||||
local _, e2 = Opus.encode(Cases.signal(Cases.list[6]), { bitrate = 1000 })
|
||||
t:check(e2 and e2.code == Err.ARG, "bitrate too low")
|
||||
local _, e3 = Opus.encode({} :: any)
|
||||
t:check(e3 and e3.code == Err.ARG, "not audio")
|
||||
end
|
||||
|
||||
-- fuzz the decoder over our files
|
||||
do
|
||||
local samples = {}
|
||||
for i, f in files do
|
||||
if i <= 3 then
|
||||
table.insert(samples, string.sub(buffer.tostring(f :: buffer), 1, 4000))
|
||||
end
|
||||
end
|
||||
t:fuzz("decode our files", function(b: buffer)
|
||||
return Opus.decode(Bytes.reader(b))
|
||||
end, samples, { mutations = 10, garbage = 30 })
|
||||
end
|
||||
|
||||
t:done()
|
||||
Reference in new issue
Block a user