Files

255 lines
8.5 KiB
Luau

-- containers/Ogg tests: encoder and hand-made multiplexed files against the parser in tools/fixtures/ogg.py, the muxer.
-- The muxer was checked against opus-tools: an opusenc file remuxed through it passes opusinfo, decodes identically.
-- Fixtures: tools/fixtures/ogg.py.
local T = require("@tests/T")
local Ogg = require("@rosetta/containers/Ogg")
local Bytes = require("@rosetta/core/Bytes")
local Crc32 = require("@rosetta/core/Crc32")
local Err = require("@rosetta/core/Err")
local Cases = require("@tests/gen/ogg_cases")
local t = T.suite("ogg")
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
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
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")
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
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
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()