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.
This commit is contained in:
4DBug committed 2026-10-31 12:25:03 -05:00
1 parent 763f4a7c04
commit 51b08575c4
283 files changed
+1877 -1660

No files matched your search

+142
View File
@@ -0,0 +1,142 @@
--!strict
-- containers/Chunks: the generic walker for chunked files (a 4-byte id and a byte size per chunk, the body, optional
-- trailer and padding): RIFF / RIFX / RF64 (containers/Riff builds on it), IFF / AIFF / AIFC, PNG (size first, a
-- CRC trailer), CAF (64-bit sizes). Formats list their chunk handlers over the walked list instead of each writing
-- the loop. New in formats-luau (the RIFF leniency comes from containers/Riff, absorbed from wav-luau).
--
-- local list = Chunks.walk(buf, from, limit, Chunks.IFF) -- { Chunk } (offsets absolute in buf)
-- local list = Chunks.walk(buf, from, limit, { sizeFirst = true, bigEndian = true, trailer = 4 }) -- PNG
-- for _, c in list do if c.id == "COMM" then ... c.offset, c.size, c.avail ... end end
-- local c = Chunks.find(list, "SSND")
--
-- A chunk declared past the end keeps what is there (avail < size, truncated = true) and ends the walk; ids in
-- layout.openEnded with a size of 0 / 0xFFFFFFFF / past the end run to the end of the data (streamed writers).
-- layout.lenientPad: an odd-sized chunk written without its pad byte is detected (the next id is printable only one
-- byte earlier). layout.resize(id, declared) can replace a declared size (RF64's ds64). Never throws.
local Chunks = {}
export type Chunk = {
id: string,
start: number, -- the chunk header's offset
offset: number, -- the body's offset
size: number, -- the body size (after layout.resize / open-ended handling)
declared: number, -- the size field as written
avail: number, -- body bytes actually present (= size unless truncated)
truncated: boolean,
streamed: boolean, -- an open-ended chunk with size 0 / 0xFFFFFFFF
}
export type Layout = {
bigEndian: boolean?, -- size fields big-endian (default little)
sizeFirst: boolean?, -- the size comes before the id (PNG)
sizeBytes: number?, -- 4 (default) or 8
trailer: number?, -- bytes after the body that belong to the chunk (PNG: the 4-byte CRC)
pad: number?, -- bodies are padded to a multiple of this (RIFF / IFF: 2; default 1)
lenientPad: boolean?, -- detect odd bodies written without their pad byte
openEnded: { [string]: boolean }?,
resize: ((id: string, declared: number) -> number?)?,
maxChunks: number?, -- default 100000
}
Chunks.RIFF = table.freeze({ pad = 2, lenientPad = true }) :: Layout
Chunks.RIFX = table.freeze({ pad = 2, lenientPad = true, bigEndian = true }) :: Layout
Chunks.IFF = table.freeze({ pad = 2, bigEndian = true }) :: Layout
Chunks.PNG = table.freeze({ sizeFirst = true, bigEndian = true, trailer = 4 }) :: Layout
Chunks.CAF = table.freeze({ bigEndian = true, sizeBytes = 8 }) :: Layout
local readu8, readu32, readstring = buffer.readu8, buffer.readu32, buffer.readstring
local function printable4(b: buffer, p: number, limit: number): boolean
if p + 4 > limit then
return false
end
for i = 0, 3 do
local c = readu8(b, p + i)
if c < 32 or c > 126 then
return false
end
end
return true
end
Chunks.printable4 = printable4
local function u32(b: buffer, o: number, be: boolean): number
local v = readu32(b, o)
return if be then bit32.byteswap(v) else v
end
-- walk(b, p, limit, layout?) -> { Chunk } of [p, limit)
function Chunks.walk(b: buffer, p: number, limit: number, layout: Layout?): { Chunk }
local L: Layout = layout or Chunks.RIFF
local be = L.bigEndian == true
local sizeBytes = L.sizeBytes or 4
local head = 4 + sizeBytes
local trailer = L.trailer or 0
local pad = L.pad or 1
local openEnded = L.openEnded
local resize = L.resize
local maxChunks = L.maxChunks or 100000
local list: { Chunk } = {}
limit = math.min(limit, buffer.len(b))
while p + head <= limit and #list < maxChunks do
local idAt = if L.sizeFirst then p + sizeBytes else p
local sizeAt = if L.sizeFirst then p else p + 4
local id = readstring(b, idAt, 4)
local declared
if sizeBytes == 8 then
local hi, lo = u32(b, sizeAt, be), u32(b, sizeAt + 4, be)
if not be then
hi, lo = lo, hi
end
declared = hi * 4294967296 + lo
else
declared = u32(b, sizeAt, be)
end
local size = declared
if resize then
size = resize(id, declared) or declared
end
local body = p + head
local avail = limit - body
local c: Chunk = {
id = id,
start = p,
offset = body,
size = size,
declared = declared,
avail = math.min(size, avail),
truncated = false,
streamed = false,
}
table.insert(list, c)
if openEnded and openEnded[id] and (size == 0 or size == 0xFFFFFFFF or size > avail) then
c.streamed = size == 0 or size == 0xFFFFFFFF
c.truncated = not c.streamed
c.size, c.avail = avail, avail
break
end
if size + trailer > avail then
c.truncated = true -- (the body or its trailer runs past the end)
break
end
local rem = size % pad
local nextPos = body + size + trailer + (if rem > 0 then pad - rem else 0)
if L.lenientPad and rem > 0 and not printable4(b, nextPos, limit) and printable4(b, body + size + trailer, limit) then
nextPos = body + size + trailer
end
p = nextPos
end
return list
end
-- find(list, id, from?) -> the first chunk with that id at index >= from (default 1), its index
function Chunks.find(list: { Chunk }, id: string, from: number?): (Chunk?, number?)
for i = from or 1, #list do
if list[i].id == id then
return list[i], i
end
end
return nil, nil
end
return Chunks
+23 -52
View File
@@ -1,7 +1,7 @@
--!strict
-- containers/Riff: the RIFF chunk container (RIFF, RIFX big-endian, RF64 / BW64 with the ds64 chunk), read leniently
-- and written. Shared by audio/wav and the other RIFF forms (AVI, WebP, ANI, ...). The chunk walker comes from
-- wav-luau fd9a909 (same author, no third-party code); the writer is new.
-- wav-luau fd9a909 (same author, no third-party code), now the RIFF layout of containers/Chunks; the writer is new.
--
-- local f, err = Riff.read(r, { openEnded = { data = true } }) -- r: a core/Bytes Reader or a buffer
-- f.container, f.form, f.bigEndian -- "RIFF" | "RIFX" | "RF64", "WAVE", false
@@ -20,6 +20,7 @@
-- 0xFFFFFFFF come from the ds64 chunk (and its table). Never throws: bad data returns nil, Err.
local Err = require("@formats/core/Err")
local Bytes = require("@formats/core/Bytes")
local Chunks = require("@formats/containers/Chunks")
local readu8, readu16, readu32, readstring = buffer.readu8, buffer.readu16, buffer.readu32, buffer.readstring
@@ -27,6 +28,7 @@ local Riff = {}
export type Chunk = {
id: string,
start: number, -- absolute offset of the chunk header
offset: number, -- absolute offset of the body in the file's buffer
size: number, -- declared size (ds64-resolved), or what is available for open-ended / truncated chunks
declared: number, -- the raw size field
@@ -51,19 +53,6 @@ export type ReadOptions = {
}
-- ------------------------------------------------------------------------------------------------ read
local function printable4(b: buffer, p: number, limit: number): boolean
if p + 4 > limit then
return false
end
for i = 0, 3 do
local c = readu8(b, p + i)
if c < 32 or c > 126 then
return false
end
end
return true
end
local function u32(b: buffer, o: number, be: boolean): number
local v = readu32(b, o)
return if be then bit32.byteswap(v) else v
@@ -79,43 +68,25 @@ function Riff.sniff(r: any): string?
return if #form == 4 then form else nil
end
-- walk the chunks of [p, limit) into list; returns the list
local function walk(b: buffer, p: number, limit: number, be: boolean, opts: ReadOptions?, ds64: any, list: { Chunk }): { Chunk }
local openEnded = opts and opts.openEnded
local maxChunks = opts and opts.maxChunks or 100000
while p + 8 <= limit and #list < maxChunks do
local id = readstring(b, p, 4)
local declared = u32(b, p + 4, be)
local size = declared
local body = p + 8
local avail = limit - body
if declared == 0xFFFFFFFF and ds64 then
if id == "data" then
size = ds64.dataSize
elseif ds64.table[id] then
size = ds64.table[id]
-- walk(b, p, limit, be, opts, ds64) -> the chunks of [p, limit): containers/Chunks with the RIFF layout (padding to 2,
-- lenient missing pad bytes, open-ended ids, RF64 sizes from ds64)
local function walk(b: buffer, p: number, limit: number, be: boolean, opts: ReadOptions?, ds64: any): { Chunk }
local layout: Chunks.Layout = {
bigEndian = be,
pad = 2,
lenientPad = true,
openEnded = opts and opts.openEnded,
maxChunks = opts and opts.maxChunks,
resize = if ds64
then function(id: string, declared: number): number?
if declared ~= 0xFFFFFFFF then
return nil
end
return if id == "data" then ds64.dataSize else ds64.table[id]
end
end
local c: Chunk = { id = id, offset = body, size = size, declared = declared, avail = math.min(size, avail), truncated = false, streamed = false }
table.insert(list, c)
if openEnded and openEnded[id] and (size == 0 or size == 0xFFFFFFFF or size > avail) then
c.streamed = size == 0 or size == 0xFFFFFFFF
c.truncated = not c.streamed
c.size, c.avail = avail, avail
break
end
if size > avail then
c.truncated = true
break
end
local nextPos = body + size + size % 2
-- an odd-sized chunk written without its pad byte: the next id starts right after the body
if size % 2 == 1 and not printable4(b, nextPos, limit) and printable4(b, body + size, limit) then
nextPos = body + size
end
p = nextPos
end
return list
else nil,
}
return Chunks.walk(b, p, limit, layout) :: any
end
-- read(src, opts?) -> File | nil, Err. src: a core/Bytes Reader (its range) or a buffer.
@@ -172,7 +143,7 @@ function Riff.read(src: any, opts: ReadOptions?): (File?, Err.Err?)
end
end
end
walk(b, base + 12, limit, be, opts, f.ds64, f.chunks)
f.chunks = walk(b, base + 12, limit, be, opts, f.ds64)
return f, nil
end
@@ -199,7 +170,7 @@ function Riff.list(f: File, c: Chunk, opts: ReadOptions?): { Chunk }
if c.avail < 4 then
return {}
end
return walk(f.buf, c.offset + 4, c.offset + c.avail, f.bigEndian, opts, nil, {})
return walk(f.buf, c.offset + 4, c.offset + c.avail, f.bigEndian, opts, nil)
end
-- body(file, chunk) -> a Reader over the chunk's available body
+2 -2
View File
@@ -5,7 +5,7 @@
-- archive comment), the central directory, ZIP64 (records, locator, extra fields), a prefix shift (self-extractor
-- stubs), stored and deflated entries with a CRC-32 check, data descriptors, a wrong local header fallback,
-- case-insensitive / basename lookup that skips __MACOSX. The DEFLATE codec is now required directly
-- (codecs/Deflate) instead of injected. The writer is new: stored / deflated entries (codecs/Deflate compress, stored
-- (codecs/compress/Deflate) instead of injected. The writer is new: stored / deflated entries (codecs/compress/Deflate compress, stored
-- when that is not smaller), UTF-8 names, DOS times, raw copies of existing entries, ZIP64 when needed.
--
-- local zip, err = Zip.open(buf) -- buffer, string or core/Bytes Reader
@@ -21,7 +21,7 @@
local Err = require("@formats/core/Err")
local Bytes = require("@formats/core/Bytes")
local Crc32 = require("@formats/core/Crc32")
local Deflate = require("@formats/codecs/Deflate")
local Deflate = require("@formats/codecs/compress/Deflate")
local Zip = {}
Zip.version = "1.1.0"