Files
rosetta/tests/audio/vorbis_encode.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

111 lines
4.6 KiB
Luau

-- audio/vorbis encoder tests: every fixture-step encode (qualities -1 .. 10, mono / stereo / 5.1, 8 .. 96 kHz, odd
-- lengths, silence) passes ogginfo, decodes in oggdec (libvorbisfile) with the exact input length and an SNR over a
-- per-quality floor, and in ffmpeg; our own decoder gives the same; the codebooks are valid prefix codes; streaming
-- from a Decoder gives the same bytes; tags round-trip; option errors. Fixtures: tools/fixtures/vorbis_enc.py.
local T = require("@tests/T")
local Vorbis = require("@formats/audio/vorbis")
local VEnc = require("@formats/audio/vorbis/Encode")
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 Enc = require("@tests/gen/vorbis_enc")
local Sigs = require("@tests/gen/vorbis_enc_sigs")
local t = T.suite("vorbis_encode")
local function audio(name: string): Types.Audio
local s = Sigs[name]
local a = Types.Audio.new(s.rate, s.channels, s.frames)
buffer.writestring(a.samples, 0, s.data)
return a
end
local function snr(x: Types.Audio, y: Types.Audio): number
local sig, er = 0, 0
for i = 0, math.min(x.frames, y.frames) * x.channels - 1 do
local a = buffer.readf32(x.samples, i * 4)
local d = a - buffer.readf32(y.samples, i * 4)
sig += a * a
er += d * d
end
if er == 0 then
return 200
end
return if sig > 0 then 10 * math.log10(sig / er) else -200
end
local function floorFor(q: number): number
return 8 + 1.2 * math.max(q, 0)
end
t:check(Vorbis.caps.encode and Vorbis.encode ~= nil, "caps.encode")
-- the codebooks: Kraft sums of exactly 1 (complete prefix codes) and lengths 1 .. 24
for i, b in VEnc.BOOKS do
local k, okLen = 0, true
for _, l in b.lengths do
k += 2 ^ -l
okLen = okLen and l >= 1 and l <= 24
end
t:check(math.abs(k - 1) < 1e-9 and okLen, "codebook " .. (i - 1) .. ": complete prefix code")
end
local minSnr = math.huge
for _, e in Enc do
t:eq(e.ogginfo, "", e.name .. ": ogginfo reports no warnings / errors")
t:eq(e.oggdecFrames, e.frames, e.name .. ": oggdec frames = input frames")
t:eq(e.ffErrors, "", e.name .. ": ffmpeg decodes without errors")
local silent = string.find(e.name, "silence", 1, true) ~= nil
local f = if silent then -math.huge else floorFor(e.quality)
t:check(e.oggdecSnr >= f, string.format("%s: oggdec SNR %.1f dB (floor %.1f)", e.name, e.oggdecSnr, f))
if e.frames >= 2048 then
-- (ffmpeg does not trim the end of a stream that fits on one Ogg page: up to a block more)
t:check(e.ffFrames >= e.frames and e.ffFrames <= e.frames + 1024, string.format("%s: ffmpeg frames %d", e.name, e.ffFrames))
t:check(e.ffSnr >= f, string.format("%s: ffmpeg SNR %.1f dB", e.name, e.ffSnr))
end
if not silent then
minSnr = math.min(minSnr, e.oggdecSnr)
end
-- our decoder: the same length and quality (it is sample-exact with libvorbis)
local x = audio(e.sig)
local y, err = Vorbis.decode(Bytes.reader(buffer.fromstring(e.data)))
t:check(y, e.name .. ": our decoder: " .. tostring(err))
t:eq(y.frames, e.frames, e.name .. ": our decoder's frames")
local s = snr(x, y)
t:check(math.abs(s - e.oggdecSnr) < 0.5 or s >= 100, string.format("%s: our decoder %.1f dB, oggdec %.1f dB", e.name, s, e.oggdecSnr))
end
t:note("%d encodes pass ogginfo / oggdec / ffmpeg; lowest SNR %.1f dB", #Enc, minSnr)
-- streaming, tags
do
local a = audio("music_2_3000_44100")
local f1 = Vorbis.encode(a) :: buffer
local f2 = Vorbis.encode(Pcm.audioDecoder(a)) :: buffer
t:check(buffer.tostring(f1) == buffer.tostring(f2), "Decoder source = Audio source")
a.tags = { title = "Tïtle", artist = "Artist", track = "9" }
local f3 = Vorbis.encode(a, { quality = 1, serial = 77 }) :: buffer
local d = Vorbis.open(Bytes.reader(f3)) :: any
t:eq(d.tags.title, "Tïtle", "title tag")
t:eq(d.tags.track, "9", "track tag")
t:check(string.find(d.vendor, "formats-luau", 1, true) ~= nil, "vendor")
local empty = Vorbis.encode(Types.Audio.new(44100, 1, 0)) :: buffer
local e0 = Vorbis.decode(Bytes.reader(empty))
t:check(e0 and e0.frames == 0, "empty input")
end
-- errors
do
local a = Types.Audio.new(44100, 2, 10)
local function code(v: any, o: any): string?
local _, e = Vorbis.encode(v, o)
return e and e.code
end
t:eq(code("x", nil), Err.ARG, "junk")
t:eq(code(a, { quality = 11 }), Err.ARG, "quality 11")
t:eq(code(a, { quality = -2 }), Err.ARG, "quality -2")
t:eq(code(a, { serial = -1 }), Err.ARG, "serial -1")
t:eq(code(Types.Audio.new(44100.5, 1, 1), nil), Err.ARG, "fractional rate")
end
-- (the decoder's fuzz pass is in tests/audio/vorbis.luau; the encoder takes no untrusted bytes)
t:done()