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]>
205 lines
7.4 KiB
Luau
205 lines
7.4 KiB
Luau
-- audio/flac tests: every fixture from tools/fixtures/flac.py (flac 0-8, 4-32 bits, 1-6 channels, odd rates and block
|
|
-- sizes, LPC order 32, wasted bits, every stereo mode, ffmpeg's encoder, Ogg FLAC, ID3v2 in front) decoded bit-exact
|
|
-- against `flac -d`; sample-accurate seeking; f32 scaling and output reuse; decode() to an Audio from a sub-range
|
|
-- Reader; tags and pictures; the length from the last frame; sniff; the api; damaged frames skipped; the fuzz pass.
|
|
-- Ported from flac-luau tests/flac.luau. Fixtures: tools/fixtures/flac.py.
|
|
local T = require("@tests/T")
|
|
local VorbisComment = require("@formats/codecs/audio/VorbisComment")
|
|
local Flac = require("@formats/audio/flac")
|
|
local Bytes = require("@formats/core/Bytes")
|
|
local Crc32 = require("@formats/core/Crc32")
|
|
local Err = require("@formats/core/Err")
|
|
local F = require("@formats")
|
|
local Cases = require("@tests/gen/flac_cases")
|
|
|
|
local t = T.suite("flac")
|
|
|
|
local function open(b: any, opts: any?): (any, any)
|
|
return Flac.open(Bytes.reader(if type(b) == "string" then buffer.fromstring(b) else b), opts)
|
|
end
|
|
local byName = {}
|
|
for _, c in Cases do
|
|
byName[c.name] = c
|
|
end
|
|
|
|
t:eq(Flac.name, "flac", "name")
|
|
t:check(Flac.caps.decode and Flac.caps.encode, "caps") -- encode: ws-audio (tests/audio/flac_encode.luau)
|
|
|
|
local seekChecks = 0
|
|
for _, fx in Cases do
|
|
local name = fx.name
|
|
local dec, err = open(fx.data)
|
|
t:check(dec, name .. ": open: " .. tostring(err))
|
|
t:check(dec.sampleRate == fx.rate and dec.channels == fx.channels and dec.bitsPerSample == fx.bps, name .. ": format")
|
|
t:eq(dec.frames, fx.frames, name .. ": frames")
|
|
t:check(math.abs(dec.duration - fx.frames / fx.rate) < 1e-9, name .. ": duration")
|
|
t:eq(dec.container, if fx.ogg then "ogg" else "flac", name .. ": container")
|
|
local ch = dec.channels
|
|
local all = table.create(fx.frames * ch, 0)
|
|
local crc, n, k = 0, 0, 0
|
|
local sizes = { 4096, 1, 777, 10000, 3 }
|
|
while true do
|
|
k += 1
|
|
local out, got = dec:readInt(sizes[k % #sizes + 1])
|
|
if got == 0 then
|
|
break
|
|
end
|
|
crc = Crc32.of(out, 0, got * ch * 4, crc)
|
|
for i = 0, got * ch - 1 do
|
|
all[n * ch + i + 1] = buffer.readi32(out, i * 4)
|
|
end
|
|
n += got
|
|
end
|
|
t:eq(n, fx.frames, name .. ": decoded frames")
|
|
t:eq(crc, fx.crc, name .. ": samples identical to flac -d")
|
|
t:eq(select(2, dec:position()), fx.frames, name .. ": position at end")
|
|
-- f32 scaling, output reuse
|
|
dec:seekSample(0)
|
|
local reuse = buffer.create(100 * ch * 4)
|
|
local f, gotf = dec:read(100, reuse)
|
|
t:check(f == reuse, name .. ": output buffer reused")
|
|
local scale = 2 ^ (fx.bps - 1)
|
|
local bad = 0
|
|
for i = 0, math.min(gotf, 100) * ch - 1 do
|
|
if math.abs(buffer.readf32(f, i * 4) - all[i + 1] / scale) >= 1e-7 then
|
|
bad += 1
|
|
end
|
|
end
|
|
t:eq(bad, 0, name .. ": f32 samples")
|
|
-- seeking
|
|
local rand = T.rng(99)
|
|
local targets = { 0, 1, fx.frames - 1, fx.frames // 2, 4096, 4095, 4097, 1152, 1151 }
|
|
for _ = 1, 12 do
|
|
table.insert(targets, rand(fx.frames))
|
|
end
|
|
for _, s in targets do
|
|
if s >= 0 and s < fx.frames then
|
|
t:check(dec:seekSample(s), name .. ": seek " .. s)
|
|
t:eq(select(2, dec:position()), s, name .. ": position after seek " .. s)
|
|
local out, got = dec:readInt(50)
|
|
t:eq(got, math.min(50, fx.frames - s), name .. ": read after seek")
|
|
local diff = 0
|
|
for i = 0, got * ch - 1 do
|
|
if buffer.readi32(out, i * 4) ~= all[s * ch + i + 1] then
|
|
diff += 1
|
|
end
|
|
end
|
|
t:eq(diff, 0, name .. ": samples after seek " .. s)
|
|
seekChecks += 1
|
|
end
|
|
end
|
|
dec:seek(dec.duration / 3)
|
|
local _, pos = dec:position()
|
|
t:check(math.abs(pos - fx.frames / 3) <= 1, name .. ": seek seconds")
|
|
dec:seek(dec.duration + 5)
|
|
t:eq(select(2, dec:read(10)), 0, name .. ": read past the end")
|
|
for key, v in fx.tags or {} do -- (tags keys are lower case: codecs/audio/VorbisComment)
|
|
t:eq(dec.tags[VorbisComment.tagKey(key)], v, name .. ": tag " .. key)
|
|
end
|
|
t:eq(Flac.sniff(Bytes.reader(buffer.fromstring(fx.data))), 1, name .. ": sniff")
|
|
end
|
|
t:note("%d fixtures bit-exact against flac -d, %d seeks", #Cases, seekChecks)
|
|
|
|
-- decode() from a sub-range Reader
|
|
for _, name in { "s16_l5", "ogg_s16", "id3", "s24_96k" } do
|
|
local fx = byName[name]
|
|
local big = buffer.create(#fx.data + 13)
|
|
buffer.writestring(big, 6, fx.data)
|
|
local a, err = Flac.decode(Bytes.reader(big, 6, #fx.data))
|
|
t:check(a and a.kind == "audio" and a.frames == fx.frames and a.channels == fx.channels, name .. ": decode: " .. tostring(err))
|
|
local d = open(fx.data)
|
|
local ref = d:read(fx.frames)
|
|
t:check(buffer.tostring(ref) == buffer.tostring(a.samples), name .. ": decode = read")
|
|
t:eq(a.meta.source.bitDepth, fx.bps, name .. ": meta.source")
|
|
end
|
|
|
|
-- tags and pictures
|
|
do
|
|
local dec = open(byName.s16_noseek.data)
|
|
t:check(#dec.pictures == 1 and dec.pictures[1].mime == "image/png" and dec.pictures[1].width == 1, "picture")
|
|
t:eq(#dec.seekTable, 0, "no seek table")
|
|
t:check(string.find(dec.vendor, "reference libFLAC", 1, true), "vendor")
|
|
t:eq(#dec.md5, 32, "md5")
|
|
end
|
|
|
|
-- STREAMINFO without a total length: found from the last frame
|
|
for _, name in { "s16_l5", "varblock", "bs17" } do
|
|
local fx = byName[name]
|
|
local b = buffer.fromstring(fx.data)
|
|
buffer.writeu8(b, 8 + 13, bit32.band(buffer.readu8(b, 8 + 13), 0xF0))
|
|
buffer.writeu32(b, 8 + 14, 0)
|
|
local dec = open(b)
|
|
t:check(dec and dec.frames == fx.frames, name .. ": length from the last frame")
|
|
local crc, n = 0, 0
|
|
while true do
|
|
local out, got = dec:readInt(5000)
|
|
if got == 0 then
|
|
break
|
|
end
|
|
crc = Crc32.of(out, 0, got * dec.channels * 4, crc)
|
|
n += got
|
|
end
|
|
t:check(n == fx.frames and crc == fx.crc, name .. ": decode without total length")
|
|
end
|
|
|
|
-- sniff, errors, the api
|
|
do
|
|
t:eq(Flac.sniff(Bytes.reader(buffer.fromstring("ID3\4\0\0\0\0\0\2xxfLaC"))), 1, "sniff ID3 + fLaC")
|
|
t:eq(Flac.sniff(Bytes.reader(buffer.fromstring("ID3\4\0\0\0\0\0\2xx\255\251"))), 0, "sniff ID3 + mp3")
|
|
t:eq(Flac.sniff(Bytes.reader(buffer.fromstring("RIFF"))), 0, "sniff RIFF")
|
|
local _, e1 = open("fLaC")
|
|
t:check(e1 and Err.is(e1), "no metadata: " .. tostring(e1))
|
|
local _, e2 = open(buffer.create(0))
|
|
t:check(e2 and e2.code == Err.CORRUPT, "empty")
|
|
local fx = byName.ogg_s16
|
|
t:eq(F.detect(fx.data), "flac", "F.detect Ogg FLAC")
|
|
local a = F.audio.load(fx.data)
|
|
t:check(a and a.frames == fx.frames, "F.audio.load")
|
|
local d = F.audio.open(byName.s16_l3.data)
|
|
t:check(d and d.frames == byName.s16_l3.frames, "F.audio.open")
|
|
local saved, se = F.save(a, nil, { format = "flac" }) -- the encoder (ws-audio)
|
|
t:check(saved and F.audio.load(saved).frames == fx.frames, "F.save flac: " .. tostring(se))
|
|
end
|
|
|
|
-- a damaged frame is dropped, the rest decodes
|
|
do
|
|
local src = buffer.fromstring(byName.s16_l5.data)
|
|
local len = buffer.len(src)
|
|
buffer.writeu8(src, len // 2, bit32.bxor(buffer.readu8(src, len // 2), 0x55))
|
|
local d = open(src)
|
|
local total = 0
|
|
while true do
|
|
local _, got = d:read(4096)
|
|
if got == 0 then
|
|
break
|
|
end
|
|
total += got
|
|
end
|
|
t:check(total < d.frames and total >= d.frames - 2 * 4096, "one damaged frame dropped: " .. total)
|
|
end
|
|
|
|
-- fuzz
|
|
local samples = {}
|
|
for _, name in { "s16_l5", "ogg_s16", "id3", "varblock", "s32_rice2", "u8_mono", "bits4" } do
|
|
table.insert(samples, byName[name].data)
|
|
end
|
|
t:fuzz("open + read + seek", function(b: buffer)
|
|
local d = Flac.open(Bytes.reader(b))
|
|
if d then
|
|
local guard = 0
|
|
while select(2, d:read(4096)) > 0 and guard < 200 do
|
|
guard += 1
|
|
end
|
|
d:seek(0.1)
|
|
d:read(100)
|
|
d:seekSample(d.frames // 2)
|
|
d:read(100)
|
|
end
|
|
return d
|
|
end, samples, { mutations = 12, keepHeader = 42 })
|
|
t:fuzz("decode", function(b: buffer)
|
|
return Flac.decode(Bytes.reader(b))
|
|
end, samples, { mutations = 6, garbage = 60 })
|
|
|
|
t:done()
|