merge ws/m4a
This commit is contained in:
commit
e1ffb21494
42 files changed
+10353
-8
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()
|
||||
@@ -0,0 +1,182 @@
|
||||
-- codecs/Aac tests: AudioSpecificConfig parsing (every fixture's esds, explicit SBR / PS signalling from fdkaac,
|
||||
-- crafted configs: PCE channel counts, 960-sample frames, Main / LTP object types), ADTS headers, raw frames decoded
|
||||
-- straight from ADTS against ffmpeg's float decode, the FIL extension hook on HE-AAC (implicit SBR), reset, errors,
|
||||
-- the fuzz no-throw pass on raw frames. Fixtures: tools/fixtures/m4a.py.
|
||||
local T = require("@tests/T")
|
||||
local Aac = require("@formats/codecs/Aac")
|
||||
local IsoBmff = require("@formats/containers/IsoBmff")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Bits = require("@formats/core/Bits")
|
||||
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-codec")
|
||||
|
||||
-- ADTS frames of a stream: { offset, size } of the raw blocks
|
||||
local function frames(data: buffer): ({ { number } }, Aac.Adts?)
|
||||
local out, p, first = {}, 0, nil
|
||||
while true do
|
||||
local h = Aac.adts(data, p, buffer.len(data))
|
||||
if not h or p + h.frameSize > buffer.len(data) then
|
||||
break
|
||||
end
|
||||
first = first or h
|
||||
table.insert(out, { p + h.headerSize, h.frameSize - h.headerSize })
|
||||
p += h.frameSize
|
||||
end
|
||||
return out, first
|
||||
end
|
||||
|
||||
-- ASCs from the M4A fixtures
|
||||
for _, c in Fx do
|
||||
if c.container == "m4a" then
|
||||
local r = Bytes.reader(buffer.fromstring(c.data))
|
||||
local movie = IsoBmff.parse(r) :: IsoBmff.Movie
|
||||
local cfg, err = Aac.config(movie.tracks[1].config :: buffer)
|
||||
t:check(cfg, c.name .. ": " .. tostring(err))
|
||||
t:check(cfg.objectType == 2 and cfg.sampleRate == c.rate and cfg.channels == c.channels and not cfg.sbr, c.name .. ": AAC-LC config")
|
||||
end
|
||||
end
|
||||
for _, c in He do
|
||||
if string.sub(c.name, -4) == ".m4a" then
|
||||
local r = Bytes.reader(buffer.fromstring(c.data))
|
||||
local movie = IsoBmff.parse(r) :: IsoBmff.Movie
|
||||
local cfg, err = Aac.config(movie.tracks[1].config :: buffer)
|
||||
t:check(cfg, c.name .. ": " .. tostring(err))
|
||||
t:check(cfg.sbr and cfg.sampleRate == c.rate // 2 and cfg.extensionRate == c.rate, c.name .. ": SBR signalled, core at half rate")
|
||||
t:eq(cfg.ps, c.ps, c.name .. ": PS signalled")
|
||||
end
|
||||
end
|
||||
|
||||
-- crafted configs
|
||||
do
|
||||
local asc = Aac.asc(2, 4, 2)
|
||||
local cfg = Aac.config(asc) :: Aac.Config
|
||||
t:check(cfg and cfg.sampleRate == 44100 and cfg.channels == 2, "asc(2, 44.1 kHz, stereo)")
|
||||
local _, e1 = Aac.config(Aac.asc(1, 4, 2))
|
||||
t:check(e1 and e1.code == Err.UNSUPPORTED, "AAC Main: " .. tostring(e1))
|
||||
local _, e2 = Aac.config(Aac.asc(23, 4, 2))
|
||||
t:check(e2 and e2.code == Err.UNSUPPORTED, "ER AAC LD")
|
||||
local b960 = Aac.asc(2, 3, 1)
|
||||
buffer.writeu8(b960, 1, bit32.bor(buffer.readu8(b960, 1), 4)) -- frameLengthFlag
|
||||
local _, e3 = Aac.config(b960)
|
||||
t:check(e3 and e3.code == Err.UNSUPPORTED, "960-sample frames")
|
||||
-- channel configuration 0 with a PCE: front C (SCE) + L/R (CPE), back L/R (CPE), one LFE = 6 channels
|
||||
local bw = Bits.msbWriter()
|
||||
bw:write(2, 5)
|
||||
bw:write(3, 4)
|
||||
bw:write(0, 4)
|
||||
bw:write(0, 3) -- GASpecificConfig flags
|
||||
bw:write(0, 4) -- PCE: tag
|
||||
bw:write(1, 2)
|
||||
bw:write(3, 4)
|
||||
bw:write(2, 4) -- front
|
||||
bw:write(0, 4) -- side
|
||||
bw:write(1, 4) -- back
|
||||
bw:write(1, 2) -- lfe
|
||||
bw:write(0, 3)
|
||||
bw:write(0, 4)
|
||||
bw:write(0, 3) -- mixdowns
|
||||
bw:write(0, 5)
|
||||
bw:write(16 + 1, 5) -- front: SCE 0, CPE 1
|
||||
bw:write(16 + 2, 5) -- back: CPE 2
|
||||
bw:write(0, 4) -- lfe tag
|
||||
bw:align()
|
||||
bw:write(0, 8) -- comment
|
||||
local pcfg, perr = Aac.config(bw:finish())
|
||||
t:check(pcfg and pcfg.channels == 6 and pcfg.channelConfig == 0, "PCE: 6 channels: " .. tostring(perr))
|
||||
local _, e4 = Aac.decoder("nope" :: any)
|
||||
t:check(e4 and e4.code == Err.ARG, "decoder of a string")
|
||||
end
|
||||
|
||||
-- ADTS headers + raw frames against ffmpeg
|
||||
for _, c in Fx do
|
||||
if c.container ~= "adts" then
|
||||
continue
|
||||
end
|
||||
local data = buffer.fromstring(c.data)
|
||||
local list, h = frames(data)
|
||||
t:check(h and h.objectType == 2 and h.sampleRate == c.rate, c.name .. ": ADTS header")
|
||||
local dec = Aac.decoder({ sfIndex = (h :: Aac.Adts).sfIndex, channels = c.channels }) :: Aac.Decoder
|
||||
local ref = buffer.fromstring(c.ref)
|
||||
local sig, err = 0, 0
|
||||
local o = 0
|
||||
for _, f in list do
|
||||
local n, e = dec:decode(data, f[1], f[2])
|
||||
t:check(n == 1024, c.name .. ": frame: " .. tostring(e))
|
||||
for k = 1, 1024 * c.channels do
|
||||
local r = buffer.readf32(ref, o)
|
||||
sig += r * r
|
||||
err += (dec.pcm[k] - r) ^ 2
|
||||
o += 4
|
||||
end
|
||||
end
|
||||
local snr = 10 * math.log10(sig / math.max(err, 1e-30))
|
||||
t:check(snr > 90, string.format("%s: SNR %.1f dB", c.name, snr))
|
||||
t:note("%s: %d raw frames, SNR %.1f dB vs ffmpeg", c.name, #list, snr)
|
||||
-- reset: decoding again from the start gives the same first frames
|
||||
dec:reset()
|
||||
dec:decode(data, list[1][1], list[1][2])
|
||||
local again = table.clone(dec.pcm)
|
||||
local dec2 = Aac.decoder({ sfIndex = (h :: Aac.Adts).sfIndex, channels = c.channels }) :: Aac.Decoder
|
||||
dec2:decode(data, list[1][1], list[1][2])
|
||||
local same = true
|
||||
for k = 1, 1024 * c.channels do
|
||||
same = same and again[k] == dec2.pcm[k]
|
||||
end
|
||||
t:check(same, c.name .. ": reset clears the overlap")
|
||||
end
|
||||
|
||||
-- the extension hook on HE-AAC (implicit SBR in ADTS)
|
||||
for _, c in He do
|
||||
if string.sub(c.name, -4) == ".aac" then
|
||||
local data = buffer.fromstring(c.data)
|
||||
local list, h = frames(data)
|
||||
local dec = Aac.decoder({ sfIndex = (h :: Aac.Adts).sfIndex, channels = 2 }) :: Aac.Decoder
|
||||
local seen, bits = 0, 0
|
||||
dec.onExtension = function(kind: number, _element: number, _buf: buffer, _pos: number, n: number)
|
||||
if kind == 13 or kind == 14 then
|
||||
seen += 1
|
||||
bits += n
|
||||
end
|
||||
end
|
||||
for _, f in list do
|
||||
dec:decode(data, f[1], f[2])
|
||||
end
|
||||
t:check(seen == #list and dec.sbr, string.format("%s: an SBR payload per frame (%d of %d, %d bits)", c.name, seen, #list, bits))
|
||||
end
|
||||
end
|
||||
|
||||
-- errors
|
||||
do
|
||||
local dec = Aac.decoder(Aac.asc(2, 4, 2)) :: Aac.Decoder
|
||||
local _, e = dec:decode(buffer.fromstring("\x20\x00\x00\x00\x00"))
|
||||
t:check(e and e.code == Err.CORRUPT, "garbage frame: " .. tostring(e))
|
||||
local _, e2 = dec:decode(buffer.create(4), 3, 10)
|
||||
t:check(e2 and e2.code == Err.ARG, "range out of the buffer")
|
||||
t:check(Aac.adts(buffer.fromstring("\255\241"), 0, 2) == nil, "short ADTS header")
|
||||
end
|
||||
|
||||
-- fuzz raw frames (stereo with every tool, mono, 5.1)
|
||||
do
|
||||
local pick = {}
|
||||
for _, c in Fx do
|
||||
if c.container == "adts" then
|
||||
local data = buffer.fromstring(c.data)
|
||||
local list, h = frames(data)
|
||||
local f = list[math.min(#list, 9)]
|
||||
local raw = buffer.create(f[2])
|
||||
buffer.copy(raw, 0, data, f[1], f[2])
|
||||
table.insert(pick, { raw, (h :: Aac.Adts).sfIndex, c.channels })
|
||||
end
|
||||
end
|
||||
for _, p in pick do
|
||||
local dec = Aac.decoder({ sfIndex = p[2], channels = p[3] }) :: Aac.Decoder
|
||||
t:fuzz("raw frame " .. p[3] .. " ch", function(b: buffer)
|
||||
return dec:decode(b)
|
||||
end, { p[1] }, { mutations = 60, garbage = 100 })
|
||||
end
|
||||
end
|
||||
|
||||
t:done()
|
||||
@@ -0,0 +1,181 @@
|
||||
-- codecs/Alac tests: encoder -> decoder round trips over silence (zero runs), full-scale noise (uncompressed frames),
|
||||
-- tones, steps, 1 - 8 channels, 16 / 20 / 24 bits, partial and tiny frames, custom frame lengths; ffmpeg's ALAC
|
||||
-- packets (through containers/IsoBmff) decode to the source integers; the magic cookie (plain, behind frma / alac atoms,
|
||||
-- bad versions / depths); errors; the fuzz no-throw pass on packets. Fixtures: tools/fixtures/m4a.py.
|
||||
local T = require("@tests/T")
|
||||
local Alac = require("@formats/codecs/Alac")
|
||||
local IsoBmff = require("@formats/containers/IsoBmff")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Crc32 = require("@formats/core/Crc32")
|
||||
local Err = require("@formats/core/Err")
|
||||
local Fx = require("@tests/gen/m4a_alac")
|
||||
|
||||
local t = T.suite("alac")
|
||||
local rand = T.rng(42)
|
||||
|
||||
local function roundTrip(label: string, ch: number, bits: number, frames: number, gen: (number, number) -> number, fl: number?)
|
||||
local enc = Alac.encoder({ sampleRate = 44100, channels = ch, bitDepth = bits, frameLength = fl }) :: Alac.Encoder
|
||||
t:check(enc, label .. ": encoder")
|
||||
local per = enc.config.frameLength
|
||||
local src = table.create(frames * ch, 0)
|
||||
for i = 0, frames - 1 do
|
||||
for c = 1, ch do
|
||||
src[i * ch + c] = gen(i, c)
|
||||
end
|
||||
end
|
||||
local packets = {}
|
||||
local bytes = 0
|
||||
local f = 0
|
||||
while f < frames do
|
||||
local n = math.min(per, frames - f)
|
||||
local p, err = enc:encode(src, f * ch + 1, n)
|
||||
t:check(p, label .. ": encode: " .. tostring(err))
|
||||
table.insert(packets, { p, n })
|
||||
bytes += buffer.len(p :: buffer)
|
||||
f += n
|
||||
end
|
||||
local cfg = Alac.config(Alac.cookie(enc.config)) :: Alac.Config
|
||||
t:check(cfg and cfg.maxFrameBytes >= 1 and cfg.channels == ch and cfg.bitDepth == bits, label .. ": cookie")
|
||||
local dec = Alac.decoder(cfg)
|
||||
local ok = true
|
||||
local at = 0
|
||||
for _, pk in packets do
|
||||
local n, err = dec:decode(pk[1])
|
||||
if n ~= pk[2] then
|
||||
ok = false
|
||||
t:check(false, label .. ": decode: " .. tostring(err))
|
||||
break
|
||||
end
|
||||
for k = 1, n * ch do
|
||||
if dec.pcm[k] ~= src[at * ch + k] then
|
||||
ok = false
|
||||
end
|
||||
end
|
||||
at += n
|
||||
end
|
||||
t:check(ok, label .. ": round trip")
|
||||
return bytes / (frames * ch * bits / 8)
|
||||
end
|
||||
|
||||
local peakOf = function(bits: number): number
|
||||
return 2 ^ (bits - 1)
|
||||
end
|
||||
|
||||
for _, bits in { 16, 20, 24 } do
|
||||
local pk = peakOf(bits)
|
||||
for _, ch in { 1, 2, 3, 6, 8 } do
|
||||
local r1 = roundTrip(string.format("tone %d-bit %d ch", bits, ch), ch, bits, 9000, function(i, c)
|
||||
return math.floor(math.sin(i * 0.031 * c) * (pk * 0.6) + math.sin(i * 0.0071) * pk * 0.2)
|
||||
end)
|
||||
if ch == 2 then
|
||||
t:note("%d-bit stereo tone: %.0f%% of PCM", bits, r1 * 100)
|
||||
end
|
||||
end
|
||||
roundTrip(bits .. "-bit silence (zero runs)", 2, bits, 10000, function()
|
||||
return 0
|
||||
end)
|
||||
local r = roundTrip(bits .. "-bit full-scale noise (escape frames)", 2, bits, 5000, function()
|
||||
return rand(2 ^ bits) - pk
|
||||
end)
|
||||
t:check(r < 1.01, bits .. "-bit noise stays near PCM size")
|
||||
roundTrip(bits .. "-bit extremes", 1, bits, 600, function(i)
|
||||
return if i % 2 == 0 then pk - 1 else -pk
|
||||
end)
|
||||
roundTrip(bits .. "-bit sparse clicks (zero runs + values)", 2, bits, 7000, function(i, c)
|
||||
return if i % 997 == c then pk // 3 else 0
|
||||
end)
|
||||
end
|
||||
for _, n in { 1, 2, 3, 5, 9, 17, 4095 } do
|
||||
roundTrip("partial frame of " .. n, 2, 16, n, function(i, c)
|
||||
return (i * 37 + c * 1000) % 3000 - 1500
|
||||
end)
|
||||
end
|
||||
roundTrip("frame length 1024", 2, 16, 5000, function(i)
|
||||
return math.floor(math.sin(i * 0.05) * 10000)
|
||||
end, 1024)
|
||||
roundTrip("identical channels", 2, 16, 5000, function(i)
|
||||
return math.floor(math.sin(i * 0.02) * 20000)
|
||||
end)
|
||||
|
||||
-- ffmpeg's packets
|
||||
for _, c in Fx do
|
||||
if c.container == "m4a" then
|
||||
local r = Bytes.reader(buffer.fromstring(c.data))
|
||||
local movie = IsoBmff.parse(r) :: IsoBmff.Movie
|
||||
local tr = movie.tracks[1]
|
||||
local cfg = Alac.config(tr.config :: buffer) :: Alac.Config
|
||||
t:check(cfg and cfg.bitDepth == c.bits and cfg.channels == c.channels, c.name .. ": cookie")
|
||||
local dec = Alac.decoder(cfg)
|
||||
local out = buffer.create(c.frames * c.channels * 4)
|
||||
local o = 0
|
||||
for i = 1, tr.sampleCount do
|
||||
local n, err = dec:decode(r.buf, tr.offsets[i], tr.sizes[i])
|
||||
t:check(n, c.name .. ": packet " .. i .. ": " .. tostring(err))
|
||||
for k = 1, (n :: number) * c.channels do
|
||||
buffer.writei32(out, o, dec.pcm[k])
|
||||
o += 4
|
||||
end
|
||||
end
|
||||
t:eq(Crc32.of(out), c.crc, c.name .. ": ffmpeg's packets decode to the source")
|
||||
end
|
||||
end
|
||||
|
||||
-- the cookie behind QuickTime atoms, bad cookies
|
||||
do
|
||||
local enc = Alac.encoder({ sampleRate = 48000, channels = 2 }) :: Alac.Encoder
|
||||
local plain = Alac.cookie(enc.config)
|
||||
local w = Bytes.writer(64)
|
||||
w:u32be(12)
|
||||
w:string("frma")
|
||||
w:string("alac")
|
||||
w:u32be(36)
|
||||
w:string("alac")
|
||||
w:u32be(0)
|
||||
w:bytes(plain)
|
||||
local cfg = Alac.config(w:finish())
|
||||
t:check(cfg and cfg.sampleRate == 48000 and cfg.frameLength == 4096, "cookie behind frma / alac atoms")
|
||||
local bad = buffer.fromstring(buffer.tostring(plain))
|
||||
buffer.writeu8(bad, 5, 12)
|
||||
local _, e = Alac.config(bad)
|
||||
t:check(e and e.code == Err.UNSUPPORTED, "12-bit cookie: " .. tostring(e))
|
||||
local _, e2 = Alac.config(buffer.create(10))
|
||||
t:check(e2 and e2.code == Err.TRUNCATED, "short cookie")
|
||||
local _, e3 = Alac.encoder({ sampleRate = 44100, channels = 9 })
|
||||
t:check(e3 and e3.code == Err.ARG, "9 channels")
|
||||
local _, e4 = enc:encode({ 1, 2 }, 1, 5)
|
||||
t:check(e4 and e4.code == Err.ARG, "too few samples")
|
||||
local dec = Alac.decoder(cfg :: Alac.Config)
|
||||
local _, e5 = dec:decode(buffer.fromstring("\1\255\255\255"))
|
||||
t:check(e5 and e5.code == Err.CORRUPT, "bad packet: " .. tostring(e5))
|
||||
end
|
||||
|
||||
-- fuzz the packet decoder
|
||||
do
|
||||
local enc = Alac.encoder({ sampleRate = 44100, channels = 2, bitDepth = 16 }) :: Alac.Encoder
|
||||
local src = table.create(2 * 3000, 0)
|
||||
for i = 1, #src do
|
||||
src[i] = math.floor(math.sin(i * 0.01) * 8000) + rand(64)
|
||||
end
|
||||
local samples = {}
|
||||
table.insert(samples, (enc:encode(src, 1, 3000)))
|
||||
for i = 1, #src do
|
||||
src[i] = if i % 300 < 3 then 5000 else 0
|
||||
end
|
||||
table.insert(samples, (enc:encode(src, 1, 3000)))
|
||||
local dec = Alac.decoder(enc.config)
|
||||
local enc24 = Alac.encoder({ sampleRate = 44100, channels = 1, bitDepth = 24 }) :: Alac.Encoder
|
||||
local dec24 = Alac.decoder(enc24.config)
|
||||
local s24 = table.create(2000, 0)
|
||||
for i = 1, 2000 do
|
||||
s24[i] = rand(2 ^ 24) - 2 ^ 23
|
||||
end
|
||||
local p24 = enc24:encode(s24, 1, 2000) :: buffer
|
||||
t:fuzz("decode packet", function(b: buffer)
|
||||
return dec:decode(b)
|
||||
end, samples, { mutations = 40 })
|
||||
t:fuzz("decode 24-bit escape packet", function(b: buffer)
|
||||
return dec24:decode(b)
|
||||
end, { p24 }, { mutations = 20, garbage = 50 })
|
||||
end
|
||||
|
||||
t:done()
|
||||
@@ -0,0 +1,134 @@
|
||||
-- codecs/Mdct tests: the FFT against a direct DFT for mixed-radix sizes, the forward / inverse MDCT and the DCT-IV
|
||||
-- against their O(N^2) definitions (AAC, Vorbis and CELT sizes), perfect reconstruction (TDAC) with the sine, KBD and
|
||||
-- Vorbis windows, window power complementarity, bad sizes. No fixtures (the definitions are the reference).
|
||||
local T = require("@tests/T")
|
||||
local Mdct = require("@formats/codecs/Mdct")
|
||||
|
||||
local t = T.suite("mdct")
|
||||
local rand = T.rng(7)
|
||||
local cos, sin, pi = math.cos, math.sin, math.pi
|
||||
|
||||
local function noise(n: number): { number }
|
||||
local a = table.create(n, 0)
|
||||
for i = 1, n do
|
||||
a[i] = rand(20001) / 10000 - 1
|
||||
end
|
||||
return a
|
||||
end
|
||||
|
||||
-- FFT vs DFT
|
||||
for _, n in { 1, 2, 3, 4, 5, 6, 7, 8, 12, 15, 16, 30, 49, 60, 64, 120, 240, 480, 77 } do
|
||||
local plan = Mdct.fft(n)
|
||||
t:check(plan, "fft plan " .. n)
|
||||
local re, im = noise(n), noise(n)
|
||||
local wr, wi = table.create(n, 0), table.create(n, 0)
|
||||
for k = 0, n - 1 do
|
||||
local sr, si = 0, 0
|
||||
for j = 0, n - 1 do
|
||||
local a = -2 * pi * j * k / n
|
||||
sr += re[j + 1] * cos(a) - im[j + 1] * sin(a)
|
||||
si += re[j + 1] * sin(a) + im[j + 1] * cos(a)
|
||||
end
|
||||
wr[k + 1], wi[k + 1] = sr, si
|
||||
end
|
||||
Mdct.Fft.forward(plan :: any, re, im)
|
||||
local err = 0
|
||||
for k = 1, n do
|
||||
err = math.max(err, math.abs(re[k] - wr[k]), math.abs(im[k] - wi[k]))
|
||||
end
|
||||
t:check(err < 1e-9 * n, string.format("fft %d: max error %g", n, err))
|
||||
end
|
||||
|
||||
-- MDCT / IMDCT vs the definitions
|
||||
local maxErr = 0
|
||||
for _, n in { 4, 8, 16, 64, 240, 256, 480, 1920, 2048 } do
|
||||
local plan = Mdct.new(n) :: Mdct.Plan
|
||||
t:check(plan, "mdct plan " .. n)
|
||||
local m = n // 2
|
||||
local x = noise(n)
|
||||
local X = table.create(m, 0)
|
||||
Mdct.forward(plan, x, 1, X, 1)
|
||||
local Y = noise(m)
|
||||
local y = table.create(n, 0)
|
||||
Mdct.inverse(plan, Y, 1, 1, y, 1)
|
||||
local step = if n > 500 then 37 else 1 -- spot-check the big sizes
|
||||
local ef, ei = 0, 0
|
||||
for k = 0, m - 1, step do
|
||||
local s = 0
|
||||
for j = 0, n - 1 do
|
||||
s += x[j + 1] * cos(2 * pi / n * (j + 0.5 + n / 4) * (k + 0.5))
|
||||
end
|
||||
ef = math.max(ef, math.abs(s - X[k + 1]))
|
||||
end
|
||||
for j = 0, n - 1, step do
|
||||
local s = 0
|
||||
for k = 0, m - 1 do
|
||||
s += Y[k + 1] * cos(2 * pi / n * (j + 0.5 + n / 4) * (k + 0.5))
|
||||
end
|
||||
ei = math.max(ei, math.abs(s - y[j + 1]))
|
||||
end
|
||||
t:check(ef < 1e-9 * n and ei < 1e-9 * n, string.format("mdct %d: forward error %g, inverse error %g", n, ef, ei))
|
||||
maxErr = math.max(maxErr, ef, ei)
|
||||
-- strided coefficients + offsets + scale
|
||||
local X2 = table.create(m * 3 + 5, 0)
|
||||
Mdct.forward(plan, x, 1, X2, 3, 3, 0.5)
|
||||
local ok = true
|
||||
for k = 0, m - 1 do
|
||||
ok = ok and math.abs(X2[3 + 3 * k] - 0.5 * X[k + 1]) < 1e-9
|
||||
end
|
||||
t:check(ok, "mdct " .. n .. ": strided / scaled forward")
|
||||
end
|
||||
t:note("mdct vs definition: max error %.3g", maxErr)
|
||||
|
||||
-- TDAC: windowed forward + inverse + overlap-add reproduces the signal
|
||||
for _, kind in { "sine", "kbd", "vorbis" } do
|
||||
for _, n in { 256, 2048, 240 } do
|
||||
local w = Mdct.window(kind, n, if n == 256 then 6 else 4) :: { number }
|
||||
local m = n // 2
|
||||
local plan = Mdct.new(n) :: Mdct.Plan
|
||||
local pc = 0
|
||||
for i = 1, m do
|
||||
pc = math.max(pc, math.abs(w[i] ^ 2 + w[i + m] ^ 2 - 1))
|
||||
end
|
||||
t:check(pc < 1e-12, kind .. " " .. n .. ": power complementary")
|
||||
local frames = 6
|
||||
local sig = noise(m * (frames + 1))
|
||||
local out = table.create(m * (frames + 1), 0)
|
||||
local buf, X, y = table.create(n, 0), table.create(m, 0), table.create(n, 0)
|
||||
for f = 0, frames - 1 do
|
||||
for i = 1, n do
|
||||
buf[i] = sig[f * m + i] * w[i]
|
||||
end
|
||||
Mdct.forward(plan, buf, 1, X, 1)
|
||||
Mdct.inverse(plan, X, 1, 1, y, 1, 4 / n)
|
||||
for i = 1, n do
|
||||
out[f * m + i] += y[i] * w[i]
|
||||
end
|
||||
end
|
||||
local e = 0
|
||||
for i = m + 1, m * frames do
|
||||
e = math.max(e, math.abs(out[i] - sig[i]))
|
||||
end
|
||||
t:check(e < 1e-9, string.format("%s %d: reconstruction error %g", kind, n, e))
|
||||
end
|
||||
end
|
||||
|
||||
-- DCT-IV is its own inverse (times 2 / M)
|
||||
do
|
||||
local plan = Mdct.new(512) :: Mdct.Plan
|
||||
local u = noise(256)
|
||||
local c, back = table.create(256, 0), table.create(256, 0)
|
||||
Mdct.dct4(plan, u, 1, 1, c, 1, 1)
|
||||
Mdct.dct4(plan, c, 1, 1, back, 1, 1, 2 / 256)
|
||||
local e = 0
|
||||
for i = 1, 256 do
|
||||
e = math.max(e, math.abs(back[i] - u[i]))
|
||||
end
|
||||
t:check(e < 1e-10, "dct4 involution: " .. e)
|
||||
end
|
||||
|
||||
-- bad sizes
|
||||
t:check(Mdct.new(6) == nil and Mdct.new(0) == nil and Mdct.new(-8) == nil and Mdct.new(8.5) == nil and Mdct.new("x" :: any) == nil, "bad mdct sizes")
|
||||
t:check(Mdct.fft(0) == nil and Mdct.window("nope", 8) == nil and Mdct.window("sine", 7) == nil, "bad fft sizes / windows")
|
||||
|
||||
t:done()
|
||||
@@ -0,0 +1,241 @@
|
||||
-- containers/IsoBmff tests: ffmpeg / MP4Box files (H.264 + AAC with B-frames, fast start, QuickTime MJPEG + PCM, HEVC,
|
||||
-- fragmented by ffmpeg and MP4Box, stz2 + co64, ALAC) demuxed packet for packet against ffprobe (-ignore_editlist);
|
||||
-- track facts and codec configurations; iTunes tags and cover art; AVIF items (libavif, plain and with alpha); the
|
||||
-- muxer round-trips through the demuxer (sizes, times, cts, sync, tags, 64-bit sizes); a sub-range reader; the fuzz
|
||||
-- no-throw pass. Fixtures: tools/fixtures/isobmff.py.
|
||||
local T = require("@tests/T")
|
||||
local IsoBmff = require("@formats/containers/IsoBmff")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Crc32 = require("@formats/core/Crc32")
|
||||
local Fx = require("@tests/gen/isobmff")
|
||||
|
||||
local t = T.suite("isobmff")
|
||||
|
||||
local CODEC = { h264 = "h264", aac = "aac", mjpeg = "mjpeg", pcm_s16be = "pcm", hevc = "hevc", alac = "alac" }
|
||||
|
||||
local function reader(s: string, pad: number?): Bytes.Reader
|
||||
if pad then
|
||||
local b = buffer.create(#s + pad * 2)
|
||||
buffer.writestring(b, pad, s)
|
||||
return Bytes.reader(b, pad, #s)
|
||||
end
|
||||
return Bytes.reader(buffer.fromstring(s))
|
||||
end
|
||||
|
||||
-- every fixture against ffprobe
|
||||
for _, c in Fx.cases do
|
||||
for _, pad in { 0, 5 } do
|
||||
local r = reader(c.data, if pad > 0 then pad else nil)
|
||||
local label = c.name .. (if pad > 0 then " (sub-range)" else "")
|
||||
t:check(IsoBmff.sniff(r) ~= nil, label .. ": sniff")
|
||||
local dmx, err = IsoBmff.demux(r)
|
||||
t:check(dmx, label .. ": " .. tostring(err))
|
||||
t:eq(#dmx.tracks, #c.streams, label .. ": track count")
|
||||
for i, s in c.streams do
|
||||
local tr = dmx.tracks[i]
|
||||
t:eq(tr.kind, s.kind, label .. ": track " .. i .. " kind")
|
||||
t:eq(tr.codec, CODEC[s.codec] or s.codec, label .. ": track " .. i .. " codec")
|
||||
t:eq(tr.timescale, s.timescale, label .. ": timescale")
|
||||
if s.width then
|
||||
t:check(tr.width == s.width and tr.height == s.height, label .. ": video size")
|
||||
end
|
||||
if s.sampleRate then
|
||||
t:check(tr.sampleRate == s.sampleRate and tr.channels == s.channels, label .. ": audio format")
|
||||
end
|
||||
if s.extradata > 0 and s.codec ~= "alac" then
|
||||
t:eq(if tr.config then buffer.len(tr.config) else 0, s.extradata, label .. ": codec config size")
|
||||
end
|
||||
end
|
||||
-- packets in file order
|
||||
local n, bad = 0, nil
|
||||
local base = r.base
|
||||
for p in dmx:packets() do
|
||||
n += 1
|
||||
local want = c.packets[n]
|
||||
if not want then
|
||||
bad = bad or ("extra packet " .. n)
|
||||
break
|
||||
end
|
||||
local tr = dmx.tracks[want.stream]
|
||||
if p.track ~= tr.id then
|
||||
bad = bad or ("packet " .. n .. " track")
|
||||
end
|
||||
local off = tr.offsets[p.index]
|
||||
if off - base ~= want.pos or buffer.len(p.data) ~= want.size or p.dts ~= want.dts or p.pts ~= want.pts or p.keyframe ~= want.key then
|
||||
bad = bad or string.format("packet %d: pos %d/%d size %d/%d dts %d/%d pts %d/%d key %s/%s", n, off - base, want.pos, buffer.len(p.data), want.size, p.dts, want.dts, p.pts, want.pts, tostring(p.keyframe), tostring(want.key))
|
||||
end
|
||||
end
|
||||
t:check(bad == nil, label .. ": " .. tostring(bad))
|
||||
t:eq(n, #c.packets, label .. ": packet count")
|
||||
if pad == 0 then
|
||||
t:note("%s: %d tracks, %d packets match ffprobe", c.name, #c.streams, n)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- per-track packets, seek
|
||||
do
|
||||
local c = Fx.cases[1]
|
||||
local dmx = IsoBmff.demux(reader(c.data))
|
||||
local v = dmx.tracks[1]
|
||||
local count = 0
|
||||
for p in dmx:packets(v.id) do
|
||||
count += 1
|
||||
t:eq(p.track, v.id, "per-track packets")
|
||||
end
|
||||
t:eq(count, v.sampleCount, "per-track packet count")
|
||||
dmx:seek(0.75)
|
||||
local p = dmx:packets(v.id)()
|
||||
t:check(p and p.keyframe and p.dts / v.timescale <= 0.75, "seek lands on a keyframe before the time")
|
||||
t:check(v.skip >= 0 and v.edits ~= nil, "edit list parsed")
|
||||
end
|
||||
|
||||
-- tags and cover
|
||||
do
|
||||
local r = reader(Fx.tagged.data)
|
||||
local movie = IsoBmff.parse(r) :: IsoBmff.Movie
|
||||
local tags, pics = IsoBmff.tags(r, movie)
|
||||
for k, v in Fx.tagged.tags do
|
||||
t:eq(tags[k], v, "tag " .. k)
|
||||
end
|
||||
t:check(tags.encoder ~= nil, "encoder tag")
|
||||
t:eq(#pics, 1, "one cover")
|
||||
t:eq(pics[1] and buffer.len(pics[1].data), Fx.tagged.cover, "cover bytes")
|
||||
t:eq(pics[1] and pics[1].mime, "image/png", "cover mime")
|
||||
end
|
||||
|
||||
-- AVIF items
|
||||
for _, c in Fx.avif do
|
||||
local r = reader(c.data)
|
||||
t:check(IsoBmff.sniff(r) == "avif", c.name .. ": brand")
|
||||
local movie = IsoBmff.parse(r) :: IsoBmff.Movie
|
||||
t:check(movie and #movie.tracks == 0, c.name .. ": no tracks")
|
||||
local items, err = IsoBmff.items(r, movie)
|
||||
t:check(items, c.name .. ": items: " .. tostring(err))
|
||||
local prim = items.items[items.primary]
|
||||
t:check(prim and prim.type == "av01", c.name .. ": primary item is av01")
|
||||
local ispe, av1C
|
||||
for _, p in prim.properties do
|
||||
if p.type == "ispe" then
|
||||
ispe = p
|
||||
elseif p.type == "av1C" then
|
||||
av1C = p
|
||||
end
|
||||
end
|
||||
t:check(ispe and ispe.width == c.width and ispe.height == c.height, c.name .. ": ispe")
|
||||
t:check(av1C and av1C.config and buffer.readu8(av1C.config, 0) == 0x81, c.name .. ": av1C marker / version")
|
||||
local data = IsoBmff.itemData(r, items, items.primary :: number)
|
||||
t:check(data and buffer.len(data) > 10, c.name .. ": item data")
|
||||
-- an AV1 OBU stream: the first OBU is a sequence header (type 1) or a temporal delimiter (2)
|
||||
local obu = data and bit32.band(bit32.rshift(buffer.readu8(data, 0), 3), 15)
|
||||
t:check(obu == 1 or obu == 2, c.name .. ": AV1 OBU stream")
|
||||
local auxl = nil
|
||||
for _, id in items.order do
|
||||
local it = items.items[id]
|
||||
if it.refs.auxl then
|
||||
auxl = it
|
||||
end
|
||||
end
|
||||
t:eq(auxl ~= nil, c.alpha, c.name .. ": alpha auxiliary item")
|
||||
end
|
||||
|
||||
-- the muxer: round trip through the demuxer
|
||||
do
|
||||
local rand = T.rng(5)
|
||||
local function sample(n: number): buffer
|
||||
local b = buffer.create(n)
|
||||
for i = 0, n - 1 do
|
||||
buffer.writeu8(b, i, rand(256))
|
||||
end
|
||||
return b
|
||||
end
|
||||
local aud, vid = {}, {}
|
||||
for i = 1, 90 do
|
||||
aud[i] = sample(50 + rand(200))
|
||||
end
|
||||
for i = 1, 30 do
|
||||
vid[i] = sample(100 + rand(900))
|
||||
end
|
||||
local cts = {}
|
||||
local sync = {}
|
||||
for i = 1, 30 do
|
||||
cts[i] = (i % 3) * 512
|
||||
sync[i] = i % 10 == 1
|
||||
end
|
||||
local cfg = buffer.fromstring("\x12\x10")
|
||||
local file, err = IsoBmff.mux({
|
||||
brand = "mp42",
|
||||
compatible = { "mp42", "isom" },
|
||||
tracks = {
|
||||
{ kind = "video", entry = "avc1", timescale = 15360, samples = vid, durations = 512, cts = cts, sync = sync, width = 64, height = 48, config = buffer.fromstring("\1\100\0\10\255") },
|
||||
{ kind = "audio", entry = "mp4a", timescale = 44100, samples = aud, durations = 1024, sampleRate = 44100, channels = 2, config = cfg, skip = 1024, playDuration = 44100 * 2 },
|
||||
},
|
||||
tags = { title = "Mux Test", artist = "Me", track = "2", tracktotal = "9", custom = "free form", bpm = "120" },
|
||||
pictures = { { mime = "image/jpeg", data = buffer.fromstring("\255\216\255\217") } },
|
||||
chunkDuration = 0.25,
|
||||
})
|
||||
t:check(file, "mux: " .. tostring(err))
|
||||
local r = Bytes.reader(file :: buffer)
|
||||
local dmx = IsoBmff.demux(r)
|
||||
t:check(dmx and #dmx.tracks == 2, "mux: demux our file")
|
||||
local v, a = dmx.tracks[1], dmx.tracks[2]
|
||||
t:check(v.codec == "h264" and v.width == 64 and v.height == 48 and v.config and buffer.len(v.config) == 5, "mux: video track")
|
||||
t:check(a.codec == "aac" and a.sampleRate == 44100 and a.channels == 2 and a.config and buffer.tostring(a.config) == "\x12\x10", "mux: audio track + esds")
|
||||
t:check(a.skip == 1024 and a.playDuration == 88200, "mux: edit list")
|
||||
local ok = true
|
||||
for i = 1, 30 do
|
||||
local s = IsoBmff.sample(r, v, i) :: buffer
|
||||
ok = ok and buffer.tostring(s) == buffer.tostring(vid[i]) and v.dts[i] == (i - 1) * 512 and (v.cts :: any)[i] == cts[i] and ((v.sync :: any)[i] == true) == sync[i]
|
||||
end
|
||||
t:check(ok, "mux: video samples, times, cts, sync")
|
||||
ok = true
|
||||
for i = 1, 90 do
|
||||
ok = ok and buffer.tostring(IsoBmff.sample(r, a, i) :: buffer) == buffer.tostring(aud[i]) and a.durations[i] == 1024
|
||||
end
|
||||
t:check(ok, "mux: audio samples")
|
||||
-- interleaved: offsets alternate between the tracks
|
||||
t:check(v.offsets[30] > a.offsets[1] and a.offsets[90] > v.offsets[1], "mux: interleaved chunks")
|
||||
local tags, pics = IsoBmff.tags(r, dmx.movie)
|
||||
t:check(tags.title == "Mux Test" and tags.artist == "Me" and tags.track == "2" and tags.tracktotal == "9" and tags.custom == "free form" and tags.bpm == "120", "mux: tags round trip")
|
||||
t:check(#pics == 1 and buffer.len(pics[1].data) == 4, "mux: cover round trip")
|
||||
t:note("mux: %d bytes, crc %08x", buffer.len(file :: buffer), Crc32.of(file :: buffer))
|
||||
-- data + sizes form, bad specs
|
||||
local f2 = IsoBmff.mux({ tracks = { { kind = "audio", entry = "alac", timescale = 8000, data = buffer.create(30), sizes = { 10, 10, 10 }, durations = { 4096, 4096, 100 }, config = buffer.create(24) } } })
|
||||
local d2 = IsoBmff.demux(Bytes.reader(f2 :: buffer))
|
||||
t:check(d2 and d2.tracks[1].codec == "alac" and d2.tracks[1].sampleCount == 3 and d2.tracks[1].durations[3] == 100 and buffer.len(d2.tracks[1].config) == 24, "mux: data + sizes, alac cookie")
|
||||
for _, bad in { {}, { tracks = {} }, { tracks = { { kind = "audio", entry = "toolong", timescale = 1, samples = {}, durations = 1 } } }, { tracks = { { kind = "x", entry = "abcd", timescale = 1, samples = {}, durations = 1 } } }, { tracks = { { kind = "audio", entry = "abcd", timescale = 1 } } } } do
|
||||
local f, e = IsoBmff.mux(bad :: any)
|
||||
t:check(f == nil and e and e.code == "ARG", "mux: bad spec -> ARG")
|
||||
end
|
||||
end
|
||||
|
||||
-- fuzz
|
||||
do
|
||||
local samples = {}
|
||||
for _, c in Fx.cases do
|
||||
table.insert(samples, string.sub(c.data, 1, 6000))
|
||||
table.insert(samples, c.data)
|
||||
end
|
||||
for _, c in Fx.avif do
|
||||
table.insert(samples, c.data)
|
||||
end
|
||||
t:fuzz("demux", function(b: buffer)
|
||||
local r = Bytes.reader(b)
|
||||
local dmx = IsoBmff.demux(r)
|
||||
if dmx then
|
||||
for p in dmx:packets() do
|
||||
local _ = p
|
||||
end
|
||||
IsoBmff.tags(r, dmx.movie)
|
||||
local items = IsoBmff.items(r, dmx.movie)
|
||||
if items then
|
||||
for _, id in items.order do
|
||||
IsoBmff.itemData(r, items, id)
|
||||
end
|
||||
end
|
||||
end
|
||||
return dmx
|
||||
end, samples, { mutations = 12 })
|
||||
end
|
||||
|
||||
t:done()
|
||||
Reference in new issue
Block a user