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]>
135 lines
4.3 KiB
Luau
135 lines
4.3 KiB
Luau
-- codecs/audio/Mdct tests: the FFT against a direct DFT for mixed-radix sizes, the forward / inverse MDCT and the DCT-IV
|
|
-- against their O(N^2) definitions (AAC, Vorbis and CELT sizes), perfect reconstruction (TDAC) with the sine, KBD and
|
|
-- Vorbis windows, window power complementarity, bad sizes. No fixtures (the definitions are the reference).
|
|
local T = require("@tests/T")
|
|
local Mdct = require("@formats/codecs/audio/Mdct")
|
|
|
|
local t = T.suite("mdct")
|
|
local rand = T.rng(7)
|
|
local cos, sin, pi = math.cos, math.sin, math.pi
|
|
|
|
local function noise(n: number): { number }
|
|
local a = table.create(n, 0)
|
|
for i = 1, n do
|
|
a[i] = rand(20001) / 10000 - 1
|
|
end
|
|
return a
|
|
end
|
|
|
|
-- FFT vs DFT
|
|
for _, n in { 1, 2, 3, 4, 5, 6, 7, 8, 12, 15, 16, 30, 49, 60, 64, 120, 240, 480, 77 } do
|
|
local plan = Mdct.fft(n)
|
|
t:check(plan, "fft plan " .. n)
|
|
local re, im = noise(n), noise(n)
|
|
local wr, wi = table.create(n, 0), table.create(n, 0)
|
|
for k = 0, n - 1 do
|
|
local sr, si = 0, 0
|
|
for j = 0, n - 1 do
|
|
local a = -2 * pi * j * k / n
|
|
sr += re[j + 1] * cos(a) - im[j + 1] * sin(a)
|
|
si += re[j + 1] * sin(a) + im[j + 1] * cos(a)
|
|
end
|
|
wr[k + 1], wi[k + 1] = sr, si
|
|
end
|
|
Mdct.Fft.forward(plan :: any, re, im)
|
|
local err = 0
|
|
for k = 1, n do
|
|
err = math.max(err, math.abs(re[k] - wr[k]), math.abs(im[k] - wi[k]))
|
|
end
|
|
t:check(err < 1e-9 * n, string.format("fft %d: max error %g", n, err))
|
|
end
|
|
|
|
-- MDCT / IMDCT vs the definitions
|
|
local maxErr = 0
|
|
for _, n in { 4, 8, 16, 64, 240, 256, 480, 1920, 2048 } do
|
|
local plan = Mdct.new(n) :: Mdct.Plan
|
|
t:check(plan, "mdct plan " .. n)
|
|
local m = n // 2
|
|
local x = noise(n)
|
|
local X = table.create(m, 0)
|
|
Mdct.forward(plan, x, 1, X, 1)
|
|
local Y = noise(m)
|
|
local y = table.create(n, 0)
|
|
Mdct.inverse(plan, Y, 1, 1, y, 1)
|
|
local step = if n > 500 then 37 else 1 -- spot-check the big sizes
|
|
local ef, ei = 0, 0
|
|
for k = 0, m - 1, step do
|
|
local s = 0
|
|
for j = 0, n - 1 do
|
|
s += x[j + 1] * cos(2 * pi / n * (j + 0.5 + n / 4) * (k + 0.5))
|
|
end
|
|
ef = math.max(ef, math.abs(s - X[k + 1]))
|
|
end
|
|
for j = 0, n - 1, step do
|
|
local s = 0
|
|
for k = 0, m - 1 do
|
|
s += Y[k + 1] * cos(2 * pi / n * (j + 0.5 + n / 4) * (k + 0.5))
|
|
end
|
|
ei = math.max(ei, math.abs(s - y[j + 1]))
|
|
end
|
|
t:check(ef < 1e-9 * n and ei < 1e-9 * n, string.format("mdct %d: forward error %g, inverse error %g", n, ef, ei))
|
|
maxErr = math.max(maxErr, ef, ei)
|
|
-- strided coefficients + offsets + scale
|
|
local X2 = table.create(m * 3 + 5, 0)
|
|
Mdct.forward(plan, x, 1, X2, 3, 3, 0.5)
|
|
local ok = true
|
|
for k = 0, m - 1 do
|
|
ok = ok and math.abs(X2[3 + 3 * k] - 0.5 * X[k + 1]) < 1e-9
|
|
end
|
|
t:check(ok, "mdct " .. n .. ": strided / scaled forward")
|
|
end
|
|
t:note("mdct vs definition: max error %.3g", maxErr)
|
|
|
|
-- TDAC: windowed forward + inverse + overlap-add reproduces the signal
|
|
for _, kind in { "sine", "kbd", "vorbis" } do
|
|
for _, n in { 256, 2048, 240 } do
|
|
local w = Mdct.window(kind, n, if n == 256 then 6 else 4) :: { number }
|
|
local m = n // 2
|
|
local plan = Mdct.new(n) :: Mdct.Plan
|
|
local pc = 0
|
|
for i = 1, m do
|
|
pc = math.max(pc, math.abs(w[i] ^ 2 + w[i + m] ^ 2 - 1))
|
|
end
|
|
t:check(pc < 1e-12, kind .. " " .. n .. ": power complementary")
|
|
local frames = 6
|
|
local sig = noise(m * (frames + 1))
|
|
local out = table.create(m * (frames + 1), 0)
|
|
local buf, X, y = table.create(n, 0), table.create(m, 0), table.create(n, 0)
|
|
for f = 0, frames - 1 do
|
|
for i = 1, n do
|
|
buf[i] = sig[f * m + i] * w[i]
|
|
end
|
|
Mdct.forward(plan, buf, 1, X, 1)
|
|
Mdct.inverse(plan, X, 1, 1, y, 1, 4 / n)
|
|
for i = 1, n do
|
|
out[f * m + i] += y[i] * w[i]
|
|
end
|
|
end
|
|
local e = 0
|
|
for i = m + 1, m * frames do
|
|
e = math.max(e, math.abs(out[i] - sig[i]))
|
|
end
|
|
t:check(e < 1e-9, string.format("%s %d: reconstruction error %g", kind, n, e))
|
|
end
|
|
end
|
|
|
|
-- DCT-IV is its own inverse (times 2 / M)
|
|
do
|
|
local plan = Mdct.new(512) :: Mdct.Plan
|
|
local u = noise(256)
|
|
local c, back = table.create(256, 0), table.create(256, 0)
|
|
Mdct.dct4(plan, u, 1, 1, c, 1, 1)
|
|
Mdct.dct4(plan, c, 1, 1, back, 1, 1, 2 / 256)
|
|
local e = 0
|
|
for i = 1, 256 do
|
|
e = math.max(e, math.abs(back[i] - u[i]))
|
|
end
|
|
t:check(e < 1e-10, "dct4 involution: " .. e)
|
|
end
|
|
|
|
-- bad sizes
|
|
t:check(Mdct.new(6) == nil and Mdct.new(0) == nil and Mdct.new(-8) == nil and Mdct.new(8.5) == nil and Mdct.new("x" :: any) == nil, "bad mdct sizes")
|
|
t:check(Mdct.fft(0) == nil and Mdct.window("nope", 8) == nil and Mdct.window("sine", 7) == nil, "bad fft sizes / windows")
|
|
|
|
t:done()
|