Files
rosetta/tests/audio/vorbis.luau
T
4DBugandClaude Opus 5.5 434a304315 ws-cleanup: codecs/ grouped by domain; containers/Chunks, codecs/audio/Stream, codecs/image/Pixels, codecs/model/Mat4
C2, the hierarchy (tools/move.py, every reference rewritten mechanically):
- codecs/compress: Deflate Lzma Zstd Brotli Bzip2 Lzw; codecs/entropy: Huffman (from core) RangeCoder;
  codecs/image: Jpeg Dct Vp8 Vp8L Yuv Quantize Raster Blend Pixels; codecs/audio: Mdct Aac Alac Pcm Resample
  VorbisComment Tracker SoundFont Stream; codecs/video: H264 Mpeg1 Video; codecs/text: Json Xml Text Url;
  codecs/font: TrueType Cff; codecs/model: MeshFlatten Mat4. containers/ stays flat; core/ keeps Bytes Bits Err
  Crc32 Adler32 Base64 Md5 Input Sniff.
- LAYERS lint: codecs must sit in a domain folder; gen_registry lists codecs by domain (docs/FORMATS.md).
- Checked: build.py both targets, rojo build of build/roblox, the full suite on both runtimes.

C3, generic code out of format folders:
- containers/Chunks (new): the chunked-file walker (RIFF / RIFX / IFF / PNG / CAF layouts: endianness, size first,
  trailer, padding, lenient missing pad, open-ended ids, size hooks). containers/Riff walks with it (RF64 ds64
  through the resize hook); image/png, audio/aiff and audio/caf list their chunk handlers over it.
- codecs/audio/Stream (new): Stream.packets, the packet-decoder Decoder (read / seek with pre-roll / position,
  priming and end trimming) that aac, caf and m4a each carried (~70 lines each).
- codecs/image/Pixels: image/bmp/Pixels promoted (+ rgb555); image/ico's DIB decoder uses its row loops instead of
  its own (the AND mask / alpha rule stay ICO's).
- codecs/model/Mat4 (new): matrices / quaternions / TRS shared by FBX (Xform keeps only the FBX transform model),
  Collada (Math keeps only <lookat> / <skew>) and USD (its matrix helpers and general 4x4 inverse); Mat4.mul is
  Types.Scene.mul. The Collada normal matrix is now the inverse transpose (it was the inverse; same result for the
  fixtures' bind shapes).
- README rewritten (install for Rojo and LÖVE / LuaJIT, quick start per category, F.* and F.ops, host notes:
  FORMATS_NO_JIT_TUNE, paths, layout); docs/STYLE.md: the codec domains, "what belongs in a format folder" with the
  shared building blocks and the DUP rule, conventions (WAVE order, lower-case tags, loops, the api hooks
  decodeImage / encodeImage / openAudio / plugins / resolve, the codecs/video/Video registry), Err.from, the type
  baseline. New tests: tests/containers/chunks, tests/codecs/mat4. Type baseline 93.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-31 12:25:03 -05:00

206 lines
7.5 KiB
Luau

-- audio/vorbis tests: oggenc / ffmpeg streams (qualities -1 .. 10, managed bitrate, 8 .. 96 kHz, mono / stereo / 5.1,
-- transients, silence, tags, sub-block lengths) against oggdec (libvorbisfile: sample counts and 16-bit samples, so
-- start / end trimming is exact) and ffmpeg's f32 decode (SNR); streaming reads; seeking (sample-exact against the
-- reference at the target); the packet-level decoder; errors; fuzz. Fixtures: tools/fixtures/vorbis.py.
local T = require("@tests/T")
local Vorbis = require("@formats/audio/vorbis")
local Bytes = require("@formats/core/Bytes")
local Err = require("@formats/core/Err")
local Types = require("@formats/Types")
local Pcm = require("@formats/codecs/audio/Pcm")
local Ogg = require("@formats/containers/Ogg")
local Cases = require("@tests/gen/vorbis_cases")
local t = T.suite("vorbis")
local function reader(s: string): Bytes.Reader
return Bytes.reader(buffer.fromstring(s))
end
local function s16(x: number): number
local v = math.floor(x * 32768 + 0.5)
return math.clamp(v, -32768, 32767)
end
-- the largest |our sample as s16 - oggdec's| over n samples of ours at byte a0 against ref16 from sample r0
local function maxDiff16(a: buffer, a0: number, ref: buffer, r0: number, n: number): number
local m = 0
for i = 0, n - 1 do
local d = math.abs(s16(buffer.readf32(a, a0 + i * 4)) - buffer.readi16(ref, (r0 + i) * 2))
if d > m then
m = d
end
end
return m
end
local function snr(a: buffer, ref: buffer, n: number): number
local sig, er = 0, 0
for i = 0, n - 1 do
local x = buffer.readf32(ref, i * 4)
local d = x - buffer.readf32(a, i * 4)
sig += x * x
er += d * d
end
if er == 0 then
return 200
end
return if sig > 0 then 10 * math.log10(sig / er) else -200
end
t:eq(Vorbis.name, "vorbis", "name")
t:eq(Vorbis.category, "audio", "category")
t:check(Vorbis.caps.decode and Vorbis.open ~= nil, "caps")
local minSnr = math.huge
for _, c in Cases do
local a, err = Vorbis.decode(reader(c.data))
t:check(a, c.name .. ": " .. tostring(err))
t:check(a.sampleRate == c.rate and a.channels == c.channels, c.name .. ": format")
t:eq(a.frames, c.frames, c.name .. ": frames = oggdec's")
t:eq(a.frames, c.inputFrames, c.name .. ": frames = the encoder's input")
local ref16 = buffer.fromstring(c.ref16)
local n = c.frames * c.channels
-- oggdec -R keeps the Vorbis channel order; the decoder returns WAVE order (codecs/audio/Pcm)
local order = Pcm.WAVE_FROM_VORBIS[c.channels]
if c.channels > 2 and order then
local w = buffer.create(buffer.len(ref16))
for f = 0, c.frames - 1 do
for ch = 1, c.channels do
buffer.writei16(w, (f * c.channels + ch - 1) * 2, buffer.readi16(ref16, (f * c.channels + order[ch] - 1) * 2))
end
end
ref16 = w
end
local d = maxDiff16(a.samples, 0, ref16, 0, n)
t:check(d <= 1, string.format("%s: within 1 LSB of oggdec (max %d)", c.name, d))
if c.ffRef then
local s = snr(a.samples, buffer.fromstring(c.ffRef), n)
minSnr = math.min(minSnr, s)
t:check(s > 100, string.format("%s: SNR vs ffmpeg %.1f dB", c.name, s))
end
-- streaming in odd chunks gives the same samples
local dec = Vorbis.open(reader(c.data)) :: Types.Decoder
t:eq(dec.frames, c.frames, c.name .. ": Decoder.frames")
local got, ok = 0, true
while true do
local b, k = dec:read(1013)
if k == 0 then
break
end
for i = 0, k * c.channels - 1, 7 do
ok = ok and buffer.readf32(b, i * 4) == buffer.readf32(a.samples, got * c.channels * 4 + i * 4)
end
got += k
end
t:check(ok and got == c.frames, c.name .. ": streamed reads")
-- seeks: sample-exact against the full decode
for _, frac in { 0.5, 0.1, 0.93, 0 } do
local target = math.floor(c.frames * frac)
dec:seek(target / c.rate)
t:check(math.abs(dec:position() - target / c.rate) < 1e-9, c.name .. ": position after seek")
local b, k = dec:read(300)
local want = math.min(300, c.frames - target)
t:eq(k, want, string.format("%s: frames after seek to %d", c.name, target))
local same = true
for i = 0, k * c.channels - 1 do
same = same and buffer.readf32(b, i * 4) == buffer.readf32(a.samples, (target * c.channels + i) * 4)
end
t:check(same, string.format("%s: samples after seek to %d", c.name, target))
end
end
t:note("%d streams match oggdec (counts, 1 LSB) and ffmpeg (min SNR %.1f dB)", #Cases, minSnr)
local function case(name: string): any
for _, c in Cases do
if c.name == name then
return c
end
end
error("no case " .. name)
end
-- tags, meta, sniff, sub-range
do
local c = case("q2-stereo-32k-tags")
local dec = Vorbis.open(reader(c.data)) :: any
t:eq(dec.tags.title, "A Title", "title tag")
t:eq(dec.tags.artist, "An Artist", "artist tag")
t:eq(dec.tags.album, "The Album", "album tag")
t:eq(dec.tags.track, "7", "track tag")
t:eq(dec.tags.comment, "hello vorbis", "comment tag")
t:check(type(dec.vendor) == "string" and string.find(dec.vendor, "Xiph", 1, true) ~= nil, "vendor")
t:check(dec.bitrate > 0 and math.abs(dec.duration - c.frames / c.rate) < 1e-9, "bitrate / duration")
t:eq(Vorbis.sniff(reader(c.data)), 1, "sniff")
t:eq(Vorbis.sniff(reader("OggS")), 0, "sniff short")
t:eq(Vorbis.sniff(reader("RIFF")), 0, "sniff other")
local big = buffer.create(#c.data + 11)
buffer.writestring(big, 11, c.data)
local a = Vorbis.decode(Bytes.reader(big, 11, #c.data))
t:check(a and a.frames == c.frames, "decode from a sub-range")
end
-- the packet-level decoder (as Matroska / MP4 would use it) gives the same samples, minus the Ogg trimming
do
local c = case("q5-stereo-44k-music")
local full = Vorbis.decode(reader(c.data)) :: Types.Audio
local demux = Ogg.open(buffer.fromstring(c.data))
local serial = demux:find("vorbis")
local h = {}
for i = 1, 3 do
h[i] = Ogg.copy(demux:nextPacket(serial))
end
local pd = Vorbis.packetDecoder(h[1], h[2], h[3])
t:check(pd and pd.channels == 2 and pd.sampleRate == 44100, "packetDecoder")
local total, same = 0, true
for p in demux:packets(serial) do
local n = pd:decode(p.data, p.offset, p.size)
for i = 1, n, 13 do
if total + i <= full.frames then
same = same and math.abs(pd.out[1][i] - buffer.readf32(full.samples, (total + i - 1) * 8)) < 1e-6
end
end
total += math.max(n, 0)
end
t:check(same and total >= full.frames, "packetDecoder samples")
local _, e = Vorbis.packetDecoder(h[2], h[1], h[3])
t:check(e and e.code == Err.CORRUPT, "packetDecoder with headers swapped")
end
-- errors
do
local _, e1 = Vorbis.decode(reader("OggS but not really"))
t:check(e1 and Err.is(e1), "junk: " .. tostring(e1))
local c = case("short-100").data
local _, e2 = Vorbis.decode(reader(string.sub(c, 1, 60)))
t:check(e2 and Err.is(e2), "cut in the headers: " .. tostring(e2))
-- a truncated stream decodes what is there
local q = case("q5-stereo-44k-music")
local cut = Vorbis.decode(reader(string.sub(q.data, 1, #q.data // 2)), { verify = false })
t:check(cut and cut.frames > 0 and cut.frames < q.frames, "truncated stream")
end
-- fuzz: whole streams with mutations past the headers, truncations, garbage
do
local samples = {}
for _, name in { "short-100", "short-3000-mono", "q-1-mono-8k-music", "ffmpeg-mono-16k" } do
table.insert(samples, case(name).data)
end
t:fuzz("decode", function(b: buffer)
return Vorbis.decode(Bytes.reader(b), { verify = false })
end, samples, { mutations = 40 })
t:fuzz("decode (CRC checked)", function(b: buffer)
return Vorbis.decode(Bytes.reader(b))
end, samples, { mutations = 5, garbage = 20 })
t:fuzz("open + seek", function(b: buffer)
local d = Vorbis.open(Bytes.reader(b), { verify = false })
if d then
d:read(500)
d:seek(0.05)
d:read(500)
d:seek(1e9)
d:read(10)
end
return d
end, samples, { mutations = 10, garbage = 20 })
end
t:done()