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]>
179 lines
5.7 KiB
Luau
179 lines
5.7 KiB
Luau
-- core/Bits and codecs/entropy/Huffman tests: LSB / MSB readers and writers round-trip random fields of 1..32 bits, align /
|
|
-- peek / bytePos, overrun is a sticky error; canonical codes, length-limited code lengths, LSB and MSB decoding tables
|
|
-- (with subtables) decode what the writers wrote; invalid code sets are rejected.
|
|
local T = require("@tests/T")
|
|
local Bits = require("@formats/core/Bits")
|
|
local Huffman = require("@formats/codecs/entropy/Huffman")
|
|
local Err = require("@formats/core/Err")
|
|
|
|
local t = T.suite("bits")
|
|
local rand = T.rng(7)
|
|
|
|
-- round trips
|
|
for _, kind in { "lsb", "msb" } do
|
|
local w = if kind == "lsb" then Bits.lsbWriter() else Bits.msbWriter()
|
|
local fields = {}
|
|
for i = 1, 3000 do
|
|
local n = 1 + rand(32)
|
|
local v = if n == 32 then rand(65536) * 65536 + rand(65536) else rand(2 ^ n)
|
|
fields[i] = { v, n }
|
|
w:write(v, n)
|
|
if i % 500 == 0 then
|
|
w:align()
|
|
fields[i][3] = true
|
|
end
|
|
end
|
|
local buf = w:finish()
|
|
local r = if kind == "lsb" then Bits.lsbReader(buf) else Bits.msbReader(buf)
|
|
local ok = true
|
|
for i, f in fields do
|
|
if r:peek(math.min(f[2], 24)) ~= f[1] % 2 ^ math.min(f[2], 24) and kind == "lsb" then
|
|
ok = false
|
|
end
|
|
local v = r:read(f[2])
|
|
if v ~= f[1] then
|
|
ok = false
|
|
print(" mismatch", kind, i, v, f[1], f[2])
|
|
break
|
|
end
|
|
if f[3] then
|
|
r:align()
|
|
end
|
|
end
|
|
t:check(ok, kind .. ": 3000 random fields round trip")
|
|
t:check(r.err == nil, kind .. ": no error")
|
|
r:read(16)
|
|
t:check(r.err and r.err.code == Err.TRUNCATED, kind .. ": overrun is TRUNCATED")
|
|
end
|
|
|
|
-- known bit orders
|
|
do
|
|
local lsb = Bits.lsbReader(T.bytes({ 0xB4 })) -- 1011 0100
|
|
t:eq(lsb:read(3), 4, "lsb: first field is the low bits")
|
|
t:eq(lsb:read(5), 0x16, "lsb: rest")
|
|
local msb = Bits.msbReader(T.bytes({ 0xB4 }))
|
|
t:eq(msb:read(3), 5, "msb: first field is the high bits")
|
|
t:eq(msb:read(5), 0x14, "msb: rest")
|
|
local w = Bits.msbWriter()
|
|
w:write(5, 3)
|
|
w:write(0x14, 5)
|
|
w:write(1, 1)
|
|
t:eq(T.hex(w:finish()), "b4 80", "msb writer pads with zeros")
|
|
local r = Bits.lsbReader(T.bytes({ 0xFF, 0x12, 0x34 }))
|
|
r:read(3)
|
|
t:eq(r:bytePos(), 1, "bytePos after a partial byte")
|
|
r:align()
|
|
t:eq(r:read(8), 0x12, "align then read a byte")
|
|
t:eq(r:bitsLeft(), 8, "bitsLeft")
|
|
local e = Bits.msbReader(buffer.create(0))
|
|
t:eq(e:peek(8), 0, "peek past the end is zeros")
|
|
t:check(e.err == nil, "peek past the end is not an error")
|
|
end
|
|
|
|
-- Huffman: canonical codes (RFC 1951 3.2.2 example)
|
|
do
|
|
local codes = Huffman.codes({ 3, 3, 3, 3, 3, 2, 4, 4 })
|
|
local want = { 2, 3, 4, 5, 6, 0, 14, 15 }
|
|
local ok = true
|
|
for i = 1, 8 do
|
|
ok = ok and codes[i] == want[i]
|
|
end
|
|
t:check(ok, "canonical codes (RFC 1951 example)")
|
|
t:eq(Huffman.reverse(0x91, 8), 0x89, "reverse")
|
|
t:eq(Huffman.reverse(1, 3), 4, "reverse 3 bits")
|
|
end
|
|
|
|
-- lengths: limits, Kraft equality, single symbol
|
|
local function kraft(lens: { number }): number
|
|
local s = 0
|
|
for _, l in lens do
|
|
if l > 0 then
|
|
s += 2 ^ -l
|
|
end
|
|
end
|
|
return s
|
|
end
|
|
do
|
|
local freqs = {}
|
|
for i = 1, 40 do
|
|
freqs[i] = math.floor(1.6 ^ i) -- Fibonacci-like: unlimited depth would be ~39
|
|
end
|
|
local lens = Huffman.lengths(freqs, 15)
|
|
local maxl = 0
|
|
for _, l in lens do
|
|
maxl = math.max(maxl, l)
|
|
end
|
|
t:check(maxl <= 15, "length limit 15 (got " .. maxl .. ")")
|
|
t:check(math.abs(kraft(lens) - 1) < 1e-12, "complete code")
|
|
local one = Huffman.lengths({ 0, 5, 0 }, 15)
|
|
t:check(one[1] == 0 and one[2] == 1 and one[3] == 0, "a single symbol gets length 1")
|
|
local none = Huffman.lengths({ 0, 0 }, 15)
|
|
t:check(none[1] == 0 and none[2] == 0, "no symbols")
|
|
local u = Huffman.lengths({ 1, 1, 1, 1 }, 15)
|
|
t:check(u[1] == 2 and u[4] == 2, "uniform")
|
|
end
|
|
|
|
-- decoding tables: random code sets, encode with the writer, decode through LSB and MSB tables
|
|
for trial = 1, 30 do
|
|
local nsym = 2 + rand(300)
|
|
local freqs = {}
|
|
for i = 1, nsym do
|
|
freqs[i] = if rand(4) == 0 then 0 else 1 + rand(1000) * rand(50)
|
|
end
|
|
freqs[1] += 1
|
|
freqs[2] += 1
|
|
local maxLen = if trial % 2 == 0 then 15 else 16
|
|
local lens = Huffman.lengths(freqs, maxLen)
|
|
local codes = Huffman.codes(lens)
|
|
local lensBuf = buffer.create(nsym)
|
|
for i = 1, nsym do
|
|
buffer.writeu8(lensBuf, i - 1, lens[i])
|
|
end
|
|
local P = 4 + rand(6)
|
|
local msgs = {}
|
|
for i = 1, 500 do
|
|
local s
|
|
repeat
|
|
s = rand(nsym)
|
|
until lens[s + 1] > 0
|
|
msgs[i] = s
|
|
end
|
|
for _, kind in { "lsb", "msb" } do
|
|
local tab = buffer.create(Huffman.capacity(nsym, P, maxLen) * 4)
|
|
local S = if kind == "lsb" then Huffman.buildLsb(lensBuf, 0, nsym, tab, P) else Huffman.buildMsb(lensBuf, 0, nsym, tab, P)
|
|
t:check(S >= 0, kind .. ": table built")
|
|
local w = if kind == "lsb" then Bits.lsbWriter() else Bits.msbWriter()
|
|
for _, s in msgs do
|
|
local l = lens[s + 1]
|
|
w:write(if kind == "lsb" then Huffman.reverse(codes[s + 1], l) else codes[s + 1], l)
|
|
end
|
|
local buf = w:finish()
|
|
local r = if kind == "lsb" then Bits.lsbReader(buf) else Bits.msbReader(buf)
|
|
local ok = true
|
|
for _, s in msgs do
|
|
local got = if kind == "lsb" then Huffman.decodeLsb(r, tab, P, S) else Huffman.decodeMsb(r, tab, P, S)
|
|
if got ~= s then
|
|
ok = false
|
|
break
|
|
end
|
|
end
|
|
t:check(ok, string.format("%s trial %d: %d symbols, P=%d, decode", kind, trial, nsym, P))
|
|
end
|
|
end
|
|
|
|
-- invalid code sets
|
|
do
|
|
local over = T.bytes({ 1, 1, 1 }) -- three 1-bit codes
|
|
t:eq(Huffman.buildLsb(over, 0, 3, buffer.create(1024 * 4), 8), -1, "over-subscribed")
|
|
local tooLong = T.bytes({ 17, 1 })
|
|
t:eq(Huffman.buildMsb(tooLong, 0, 2, buffer.create(1024 * 4), 8), -1, "length over 16")
|
|
local incomplete = T.bytes({ 2, 0, 0 }) -- one 2-bit code: decoding the missing ones fails
|
|
local tab = buffer.create(256 * 4)
|
|
local S = Huffman.buildMsb(incomplete, 0, 3, tab, 4)
|
|
t:check(S >= 0, "incomplete code set builds")
|
|
local r = Bits.msbReader(T.bytes({ 0xC0 }))
|
|
t:eq(Huffman.decodeMsb(r, tab, 4, S), -1, "missing code decodes to -1")
|
|
end
|
|
|
|
t:done()
|