ws-m4a: merge ws/absorb (audio/opus, containers/Ogg, ...) for the CELT encoder; m4a fixtures load their helper by path (tools/fixtures/ on sys.path shadowed the xml package)

This commit is contained in:
4DBug committed 2026-10-31 09:07:22 -05:00
commit 29a95ce24f
215 files changed
+24209 -61

No files matched your search

+263
View File
@@ -0,0 +1,263 @@
-- containers/Ogg tests: real encoder files (opusenc, flac --ogg, ffmpeg vorbis) and hand-made multiplexed files,
-- checked packet by packet against the independent Python parser of tools/fixtures/ogg.py; seeking on each; a big
-- multiplexed file written with the muxer (bisection seeks); muxer edge cases (lacing at 255 multiples, page breaks,
-- continued packets, flush, eos); a sub-range Reader; truncated / garbage input never throws. The muxer was also
-- checked by hand against opus-tools: an opusenc file remuxed through it passes opusinfo and decodes identically.
-- Ported from ogg-luau tests/ogg.luau. Fixtures: tools/fixtures/ogg.py.
local T = require("@tests/T")
local Ogg = require("@formats/containers/Ogg")
local Bytes = require("@formats/core/Bytes")
local Crc32 = require("@formats/core/Crc32")
local Err = require("@formats/core/Err")
local Cases = require("@tests/gen/ogg_cases")
local t = T.suite("ogg")
-- seek expectations from a packet list: { size, crc, granule, bos, eos }
local function expectSeek(list: { any }, g: number): (number, number)
local start, idx = -1, 1
for i, p in list do
if p[3] ~= -1 and p[3] <= g then
start, idx = p[3], i + 1
end
end
return start, idx
end
local samples = {}
for _, fx in Cases do
local buf = buffer.fromstring(fx.data)
table.insert(samples, fx.data)
local d, err = Ogg.open(buf)
t:check(d, fx.name .. ": open " .. tostring(err))
local streams = d:streams()
t:eq(#streams, #fx.streams, fx.name .. ": stream count")
for si, es in fx.streams do
local s = streams[si]
t:eq(s.serial, es.serial, fx.name .. ": serial")
t:eq(s.codec, es.codec, fx.name .. ": codec")
t:eq(s.lastGranule, es.lastGranule, fx.name .. ": lastGranule")
t:eq(buffer.len(s.firstPacket), es.packets[1][1], fx.name .. ": first packet")
local n, gaps = 0, 0
for p in d:packets(s.serial) do
n += 1
if p.gap then
gaps += 1
end
local e = es.packets[n]
t:check(e, fx.name .. ": too many packets")
t:eq(p.size, e[1], fx.name .. ": packet " .. n .. " size")
t:eq(Crc32.of(p.data, p.offset, p.size), e[2], fx.name .. ": packet " .. n .. " bytes")
t:eq(p.granule, e[3], fx.name .. ": packet " .. n .. " granule")
t:check(p.bos == e[4] and p.eos == e[5], fx.name .. ": packet " .. n .. " bos/eos")
end
t:eq(n, #es.packets, fx.name .. ": packet count")
t:eq(gaps, if fx.name == "mux_lost" and s.serial == 7 then 1 else 0, fx.name .. ": gap flags")
t:check(d:nextPacket(s.serial) == nil, fx.name .. ": end stays at end")
end
if #fx.streams > 1 then
local counts = {}
for i in streams do
counts[i] = 0
end
local d2 = Ogg.open(buf)
local live = #streams
while live > 0 do
live = 0
for i, s in streams do
local p = d2:nextPacket(s.serial)
if p then
live += 1
counts[i] += 1
t:eq(p.size, fx.streams[i].packets[counts[i]][1], fx.name .. ": interleaved read")
end
end
end
for i in streams do
t:eq(counts[i], #fx.streams[i].packets, fx.name .. ": interleaved count")
end
end
-- seeking
for _, es in fx.streams do
local serial = es.serial
local last = es.lastGranule
local targets = { -5, 0, 1, last // 3, last // 2, last - 1, last, last + 100 }
for _, p in es.packets do
if p[3] > 0 then
table.insert(targets, p[3])
table.insert(targets, p[3] - 1)
end
end
for _, g in targets do
local start = d:seekGranule(serial, g)
local eStart, eIdx = expectSeek(es.packets, g)
if start == -1 then
eIdx = 1 -- rewound: the identification packet comes next
end
t:eq(start, eStart, fx.name .. ": seek " .. g .. " start")
local p = d:nextPacket(serial)
local e = es.packets[eIdx]
if e then
t:check(p and p.size == e[1] and Crc32.of(p.data, p.offset, p.size) == e[2], fx.name .. ": packet after seek " .. g)
else
t:check(p == nil, fx.name .. ": seek past the end")
end
end
end
-- a Reader over a sub-range of a bigger buffer
local big = buffer.create(#fx.data + 20)
buffer.writestring(big, 11, fx.data)
local ds = Ogg.open(Bytes.reader(big, 11, #fx.data))
t:check(ds and #ds:streams() == #fx.streams, fx.name .. ": open from a sub-range Reader")
local n = 0
for _ in ds:packets(fx.streams[1].serial) do
n += 1
end
t:eq(n, #fx.streams[1].packets, fx.name .. ": packets from a sub-range Reader")
t:note("%-14s ok (%d streams)", fx.name, #streams)
end
-- the muxer: a big file with 3 streams, random packet sizes, page breaks by size and count; read back + seeks
do
local rand = T.rng(12345)
local mux = Ogg.muxer({ pageSize = 4096 })
local S = {}
for k = 1, 3 do
S[k] = { stream = mux:stream(1000 + k), serial = 1000 + k, granNow = 0, pk = {} }
end
local npk = 2500
for i = 1, npk do
for k, st in S do
local size = if i == 1 then 30 else (if rand(50) == 0 then rand(3000) else rand(300))
local tag = rand(256)
local b = buffer.create(size)
for j = 0, size - 1 do
buffer.writeu8(b, j, (tag + j + 1) % 256)
end
st.granNow += if i == 1 then 0 else (k * 120)
local ok, e = st.stream:packet(b, st.granNow, { flush = i == 1, eos = i == npk })
t:check(ok, "mux packet: " .. tostring(e))
table.insert(st.pk, { size, tag, st.granNow, Crc32.of(b) })
end
end
local file = mux:finish()
local d = Ogg.open(file)
t:check(d and #d:streams() == 3, "big: 3 streams")
for _, st in S do
t:eq(d:stream(st.serial).lastGranule, st.granNow, "big: lastGranule")
local n = 0
for p in d:packets(st.serial) do
n += 1
local e = st.pk[n]
t:check(p.size == e[1] and Crc32.of(p.data, p.offset, p.size) == e[4], "big: packet " .. n)
t:check(p.bos == (n == 1) and p.eos == (n == npk), "big: bos / eos " .. n)
end
t:eq(n, npk, "big: count")
for q = 0, 100 do
local g = math.floor(st.granNow * q / 100)
local start = d:seekGranule(st.serial, g)
local p = d:nextPacket(st.serial)
t:check(start and start <= g and start >= 0, "big: seek start")
local idx
for i, e in st.pk do
if e[3] == start then
idx = i + 1
break
end
end
local e = st.pk[idx]
if e then
t:check(p and p.size == e[1], "big: packet after seek")
else
t:check(p == nil, "big: end after seek")
end
end
end
t:note("muxer: 3 x %d packets, %d bytes, read back and seeked", npk, buffer.len(file))
table.insert(samples, buffer.readstring(file, 0, math.min(buffer.len(file), 20000)))
end
-- muxer edge cases: lacing at multiples of 255, a packet over 255 segments (continued pages), empty packets,
-- headers alone on their pages, finish() without eos, eos twice
do
local mux = Ogg.muxer({ pageSize = 1000 })
local s = mux:stream(42)
local sizes = { 19, 0, 255, 510, 254, 256, 255 * 255, 255 * 255 + 1, 70000, 1, 0, 3000 }
local crcs = {}
for i, n in sizes do
local b = buffer.create(n)
for j = 0, n - 1 do
buffer.writeu8(b, j, (j * 7 + i) % 256)
end
crcs[i] = Crc32.of(b)
t:check(s:packet(b, i * 10, { flush = i <= 2 }), "edge: packet " .. i)
end
local file = mux:finish()
local ok, e = s:packet("late", 999)
t:check(not ok and e and e.code == Err.ARG, "edge: packet after the end is Err.ARG")
local d = Ogg.open(file) :: any
local i = 0
local pages = 0
local pos = 0
while true do
local p = Ogg.Page.find(file, pos)
if not p then
break
end
pages += 1
t:check(p.nsegs <= 255, "edge: segments per page")
if pages == 1 then
t:check(p.bos and p.nsegs == 1 and p.granule == 10, "edge: the first packet alone on the bos page")
end
pos = p.offset + p.size
end
for p in d:packets(42) do
i += 1
t:eq(p.size, sizes[i], "edge: size " .. i)
t:eq(Crc32.of(p.data, p.offset, p.size), crcs[i], "edge: bytes " .. i)
t:eq(p.eos, i == #sizes, "edge: eos " .. i)
end
t:eq(i, #sizes, "edge: packet count")
t:check(pages > 70, "edge: page breaks (" .. pages .. " pages)")
t:eq(d:stream(42).lastGranule, #sizes * 10, "edge: last granule")
t:eq(d:stream(42).codec, "unknown", "edge: codec")
-- strings, ranges and bad arguments
local m2 = Ogg.muxer()
local s2 = m2:stream()
t:check(s2:packet("OpusHead" .. string.rep("\0", 11), 0, { flush = true }), "string packet")
t:check(s2:packet(buffer.fromstring("xxabcxx"), 5, nil, 2, 3), "range packet")
t:check(not s2:packet(buffer.create(3), 5, nil, 2, 3), "bad range")
t:check(not s2:packet(42 :: any, 5), "bad data")
local d2 = Ogg.open(m2:finish()) :: any
t:eq(d2:streams()[1].codec, "opus", "codec of a muxed OpusHead")
t:eq(d2:streams()[1].serial, 1, "default serial")
d2:nextPacket(1)
local p = d2:nextPacket(1)
t:eq(buffer.readstring(p.data, p.offset, p.size), "abc", "range packet bytes")
t:check(d2:nextPacket(1) == nil, "empty eos page ends the stream")
end
-- errors and garbage
do
local _, e1 = Ogg.open(buffer.create(0))
t:check(e1 and e1.code == Err.CORRUPT, "empty: " .. tostring(e1))
t:check(Ogg.open(buffer.fromstring(string.rep("OggS", 100))) == nil, "garbage")
local _, e2 = Ogg.open(42 :: any)
t:check(e2 and e2.code == Err.ARG, "not a buffer: " .. tostring(e2))
end
-- fuzz: open + read every packet + seek never throws
t:fuzz("open + read + seek", function(b: buffer)
local d = Ogg.open(b)
if d then
for _, s in d:streams() do
for _ in d:packets(s.serial) do
end
d:seekGranule(s.serial, s.lastGranule // 2)
d:nextPacket(s.serial)
end
end
return d
end, samples, { mutations = 6 })
t:done()
+114
View File
@@ -0,0 +1,114 @@
-- containers/Riff tests: the writer (RIFF, RIFX, forced RF64 with ds64, LIST/INFO, odd pad bytes, parts) read back by
-- the reader; lenient reading (missing pad byte, truncated chunk, open-ended data of size 0 / 0xFFFFFFFF, the ds64
-- table, a sub-range Reader); errors; the fuzz pass. The WAVE form is checked against ffmpeg in tests/audio/wav.luau.
local T = require("@tests/T")
local Riff = require("@formats/containers/Riff")
local Bytes = require("@formats/core/Bytes")
local Err = require("@formats/core/Err")
local t = T.suite("riff")
local function u32(v: number): string
local b = buffer.create(4)
buffer.writeu32(b, 0, v)
return buffer.tostring(b)
end
-- writer -> reader
local samples = {}
for _, mode in { "riff", "rifx", "rf64" } do
local w = Riff.writer("TEST", { bigEndian = mode == "rifx", rf64 = mode == "rf64", sampleCount = 77 })
w:chunk("abcd", "odd")
w:chunk("efgh", buffer.fromstring("xxevenxx"), 2, 4)
w:list("INFO", { { "INAM", "name" }, { "IART", buffer.fromstring("raw") } })
w:chunk("data", { buffer.fromstring("12345"), buffer.fromstring("678") })
local file = w:finish() :: buffer
table.insert(samples, buffer.tostring(file))
local f, err = Riff.read(Bytes.reader(file))
t:check(f, mode .. ": read: " .. tostring(err))
t:eq(f.container, if mode == "rifx" then "RIFX" elseif mode == "rf64" then "RF64" else "RIFF", mode .. ": container")
t:eq(f.form, "TEST", mode .. ": form")
t:eq(f.size, buffer.len(file) - 8, mode .. ": RIFF size")
local ids = {}
for _, c in f.chunks do
table.insert(ids, c.id)
end
t:eq(table.concat(ids, ","), if mode == "rf64" then "ds64,abcd,efgh,LIST,data" else "abcd,efgh,LIST,data", mode .. ": chunks")
local a = Riff.find(f, "abcd") :: any
t:check(a.size == 3 and buffer.readstring(file, a.offset, 3) == "odd", mode .. ": odd chunk")
local e = Riff.find(f, "efgh") :: any
t:eq(buffer.readstring(file, e.offset, e.size), "even", mode .. ": range chunk")
local l = Riff.find(f, "LIST") :: any
t:eq(Riff.listType(f, l), "INFO", mode .. ": list type")
local subs = Riff.list(f, l)
t:check(#subs == 2 and subs[1].id == "INAM" and buffer.readstring(file, subs[1].offset, subs[1].size) == "name\0", mode .. ": INFO item")
t:check(subs[2].size == 3 and subs[2].avail == 3, mode .. ": raw item")
local d = Riff.find(f, "data") :: any
t:check(d.size == 8 and buffer.readstring(file, d.offset, 8) == "12345678", mode .. ": data parts")
t:eq(buffer.tostring(Riff.body(f, d):bytes(8)), "12345678", mode .. ": body reader")
if mode == "rf64" then
t:check(f.ds64 and f.ds64.dataSize == 8 and f.ds64.sampleCount == 77 and d.declared == 0xFFFFFFFF, "rf64: ds64")
end
-- parts: the same bytes
local parts = w:finish({ parts = true }) :: { buffer }
local j = Bytes.writer()
for _, p in parts do
j:bytes(p)
end
t:check(buffer.tostring(j:finish()) == buffer.tostring(file), mode .. ": parts join to the file")
end
t:check(not Riff.writer("WAVE", { bigEndian = true, rf64 = true }):finish(), "RIFX RF64 refused")
-- lenient reading
do
-- a missing pad byte after an odd chunk
local body = "WAVE" .. "abcd" .. u32(5) .. "12345" .. "next" .. u32(2) .. "ok"
local f = Riff.read(buffer.fromstring("RIFF" .. u32(#body) .. body)) :: any
t:check(#f.chunks == 2 and f.chunks[2].id == "next", "missing pad byte detected")
-- truncated
local tb = "WAVE" .. "data" .. u32(100) .. "short"
local ft = Riff.read(buffer.fromstring("RIFF" .. u32(#tb) .. tb)) :: any
t:check(ft.chunks[1].truncated and ft.chunks[1].avail == 5 and ft.chunks[1].size == 100, "truncated chunk")
-- open-ended
for _, sz in { 0, 0xFFFFFFFF } do
local ob = "WAVE" .. "data" .. u32(sz) .. "abcdefLIST"
local fo = Riff.read(buffer.fromstring("RIFF" .. u32(0) .. ob), { openEnded = { data = true } }) :: any
t:check(#fo.chunks == 1 and fo.chunks[1].streamed and fo.chunks[1].size == 10, "open-ended data " .. sz)
end
-- ds64 table
local ds = "ds64" .. u32(40) .. u32(0) .. u32(0) .. u32(4) .. u32(0) .. u32(0) .. u32(0) .. u32(1) .. "big " .. u32(3) .. u32(0)
local rb = "WAVE" .. ds .. "big " .. u32(0xFFFFFFFF) .. "xyz" .. "\0"
local fr = Riff.read(buffer.fromstring("RF64" .. u32(0xFFFFFFFF) .. rb)) :: any
t:check(fr.ds64 and fr.ds64.table["big "] == 3 and fr.chunks[2].size == 3, "ds64 table")
-- a sub-range reader
local s = samples[1]
local big = buffer.create(#s + 7)
buffer.writestring(big, 3, s)
local fs = Riff.read(Bytes.reader(big, 3, #s)) :: any
t:check(fs and fs.form == "TEST" and #fs.chunks == 4 and fs.base == 3, "sub-range reader")
t:eq(Riff.sniff(Bytes.reader(buffer.fromstring(s))), "TEST", "sniff")
end
-- errors
do
local _, e1 = Riff.read(buffer.fromstring("RIFF"))
t:check(e1 and e1.code == Err.TRUNCATED, "too short")
local _, e2 = Riff.read(buffer.fromstring("OggS00000000"))
t:check(e2 and e2.code == Err.CORRUPT, "not RIFF")
local _, e3 = Riff.read(42 :: any)
t:check(e3 and e3.code == Err.ARG, "not a buffer")
end
t:fuzz("read + list", function(b: buffer)
local f = Riff.read(Bytes.reader(b), { openEnded = { data = true } })
if f then
for _, c in f.chunks do
if c.id == "LIST" then
Riff.list(f, c)
end
end
end
return f
end, samples, { mutations = 30 })
t:done()