Files
rosetta/tests/codecs/dct.luau
T
4DBug 51b08575c4 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.
2026-10-31 12:25:03 -05:00

159 lines
4.7 KiB
Luau

-- codecs/image/Dct tests: the islow inverse DCT against a double-precision reference (within 1 level per sample, as T.81
-- annex A accuracy needs), forward DCT + quantiser against the reference transform (round to nearest of v / (8 q)),
-- fdct -> idct round trips at q = 1 within 1 level, edge replication of fdctQuantize, and garbage coefficients never
-- throw (outputs stay clamped). The bit-exact djpeg / cjpeg checks are in tests/image/jpeg.luau.
local T = require("@tests/T")
local Dct = require("@formats/codecs/image/Dct")
local t = T.suite("dct")
local rand = T.rng(11)
local C = {}
for u = 0, 7 do
C[u] = if u == 0 then math.sqrt(0.5) else 1
end
local cosT = {}
for x = 0, 7 do
for u = 0, 7 do
cosT[x * 8 + u] = math.cos((2 * x + 1) * u * math.pi / 16)
end
end
local ones = buffer.create(128)
for i = 0, 63 do
buffer.writeu16(ones, i * 2, 1)
end
-- inverse: random sparse coefficients, compare with the float IDCT
do
local worst = 0
for trial = 1, 300 do
local coef = buffer.create(128)
local F = {}
for i = 0, 63 do
local v = 0
if rand(4) == 0 or i == 0 then
v = rand(512) - 256
if i == 0 then
v = rand(2048) - 1024
end
end
F[i] = v
buffer.writei16(coef, i * 2, v)
end
local plane = buffer.create(128)
Dct.idctBlock(coef, 0, ones, plane, 0, 8, 2, 128, 255)
for y = 0, 7 do
for x = 0, 7 do
local s = 0
for v = 0, 7 do
for u = 0, 7 do
s += C[u] * C[v] * F[v * 8 + u] * cosT[x * 8 + u] * cosT[y * 8 + v]
end
end
local want = math.clamp(math.floor(s / 4 + 128 + 0.5), 0, 255)
local got = buffer.readu16(plane, (y * 8 + x) * 2)
worst = math.max(worst, math.abs(got - want))
end
end
local _ = trial
end
t:check(worst <= 1, "idct within 1 of the float reference: " .. worst)
t:note("idct: worst error %d over 300 random blocks", worst)
end
-- forward + quantiser
do
local worst = 0
for _ = 1, 200 do
local src = buffer.create(64)
for i = 0, 63 do
buffer.writeu8(src, i, rand(256))
end
local q = buffer.create(128)
for i = 0, 63 do
buffer.writeu16(q, i * 2, 1 + rand(40))
end
local coef = buffer.create(128)
Dct.fdctQuantize(src, 8, 0, 0, 8, 8, q, coef, 0)
for v = 0, 7 do
for u = 0, 7 do
local s = 0
for y = 0, 7 do
for x = 0, 7 do
s += (buffer.readu8(src, y * 8 + x) - 128) * cosT[x * 8 + u] * cosT[y * 8 + v]
end
end
s *= C[u] * C[v] / 4
local want = s / buffer.readu16(q, (v * 8 + u) * 2)
worst = math.max(worst, math.abs(buffer.readi16(coef, (v * 8 + u) * 2) - want))
end
end
end
t:check(worst <= 0.75, "fdct + quantise within rounding of the float reference: " .. worst)
-- round trip at q = 1
local rt = 0
for _ = 1, 100 do
local src = buffer.create(64)
for i = 0, 63 do
buffer.writeu8(src, i, rand(256))
end
local coef = buffer.create(128)
Dct.fdctQuantize(src, 8, 0, 0, 8, 8, ones, coef, 0)
local plane = buffer.create(128)
Dct.idctBlock(coef, 0, ones, plane, 0, 8, 2, 128, 255)
for i = 0, 63 do
rt = math.max(rt, math.abs(buffer.readu16(plane, i * 2) - buffer.readu8(src, i)))
end
end
t:check(rt <= 1, "fdct -> idct round trip at q = 1: " .. rt)
-- edge replication: a 3x2 image is the same as its 8x8 edge-extended version
local small = T.bytes({ 10, 200, 30, 40, 50, 250 })
local ext = buffer.create(64)
for y = 0, 7 do
for x = 0, 7 do
buffer.writeu8(ext, y * 8 + x, buffer.readu8(small, math.min(y, 1) * 3 + math.min(x, 2)))
end
end
local a, b = buffer.create(128), buffer.create(128)
Dct.fdctQuantize(small, 3, 0, 0, 3, 2, ones, a, 0)
Dct.fdctQuantize(ext, 8, 0, 0, 8, 8, ones, b, 0)
t:check(buffer.tostring(a) == buffer.tostring(b), "edge replication")
end
-- components and garbage: clamped output, no throw
do
local coef = buffer.create(4 * 3 * 128)
for i = 0, buffer.len(coef) // 2 - 1 do
buffer.writei16(coef, i * 2, rand(65536) - 32768)
end
local q = buffer.create(128)
for i = 0, 63 do
buffer.writeu16(q, i * 2, rand(65536))
end
for _, size in { 8, 4, 2, 1 } do
for _, prec in { { 2, 128, 255 }, { 1, 2048, 4095 } } do
local plane = buffer.create(4 * size * 3 * size * 2)
local ok = pcall(Dct.idctComponent, coef, q, 4, 3, size, plane, prec[1], prec[2], prec[3])
t:check(ok, "idctComponent size " .. size .. " on garbage")
local inRange = true
for i = 0, buffer.len(plane) // 2 - 1 do
if buffer.readu16(plane, i * 2) > prec[3] then
inRange = false
end
end
t:check(inRange, "clamped output, size " .. size)
end
end
local samples = { string.rep("\0", 128) }
t:fuzz("idctBlock", function(b: buffer)
local cb = buffer.create(128)
buffer.copy(cb, 0, b, 0, math.min(128, buffer.len(b)))
local plane = buffer.create(128)
Dct.idctBlock(cb, 0, q, plane, 0, 8, 2, 128, 255)
return plane
end, samples, { garbage = 50 })
end
t:done()