ws-audio2: merge master (c5f2777)

This commit is contained in:
4DBug committed 2026-10-31 15:37:55 -05:00
commit d7697a8688
126 files changed
+25832 -1541

No files matched your search

+282 -1
View File
@@ -1,7 +1,7 @@
-- Throughput (bash tools/bench.sh): codec MB/s under both runtimes. MB/s are of decompressed output, best of n runs.
-- Fixtures: tools/fixtures/deflate.py (a 6 MB corpus), tools/fixtures/png.py (1024 x 768 images), tools/fixtures/mp3.py,
-- tools/fixtures/opus.py (3 s stereo files; MB/s of f32 output, 48 kHz stereo f32 is 0.384 MB/s of real time),
-- tools/fixtures/flac.py; wav encodes / decodes a generated tone; zstd / brotli / bzip2 / lzma (*_bench corpora), fonts
-- tools/fixtures/flac.py; wav encodes / decodes a generated tone; zstd / brotli / bzip2 / lzma / lz4 (*_bench corpora), fonts
-- (glyphs per second), json / xml / yaml, jpeg / gif / tiff / qoi / tga / pnm / hdr and ImageOps.
local Deflate = require("@formats/codecs/compress/Deflate")
local Crc32 = require("@formats/core/Crc32")
@@ -236,6 +236,53 @@ do
Zstd.decompress(z19, nil, nil, { verify = false })
end)
end
do
local Lz4 = require("@formats/codecs/compress/Lz4")
local LB = require("@tests/gen/lz4_bench")
local lz = buffer.fromstring(LB.data)
local raw = assert(Lz4.decompress(lz, nil, nil, LB.size))
assert(Crc32.of(raw) == LB.crc, "lz4 bench corpus mismatch")
bench("lz4 block decompress (hc9)", 5, LB.size, function()
Lz4.decompress(lz, nil, nil, LB.size)
end)
bench("lz4 block compress", 3, LB.size, function()
Lz4.compress(raw)
end)
end
-- ws-roblox: a model of 20000 parts (built here) through model/rbxm and model/rbxmx, and a .mesh decode
do
local Rbx = require("@formats/codecs/model/Rbx")
local Rbxm = require("@formats/model/rbxm")
local Rbxmx = require("@formats/model/rbxmx")
local RobloxMesh = require("@formats/model/robloxmesh")
local tree = Rbx.tree()
local folder = Rbx.add(tree, Rbx.instance("Folder", "Parts"))
for i = 1, 20000 do
local p = Rbx.add(folder, Rbx.instance("Part", "P" .. i))
Rbx.set(p, "CFrame", "CFrame", { i, i % 7, -i, 1, 0, 0, 0, 1, 0, 0, 0, 1 })
Rbx.set(p, "size", "Vector3", { 4, 1, 2 + i % 3 })
Rbx.set(p, "Color3uint8", "Color3uint8", { i % 256, 128, 255 - i % 256 })
Rbx.set(p, "Anchored", "Bool", i % 2 == 0)
Rbx.set(p, "Transparency", "Float32", (i % 4) / 4)
Rbx.set(p, "Material", "Enum", 256)
end
local bin = Rbxm.encode(tree) :: buffer
local xml = Rbxmx.encode(tree) :: buffer
bench("rbxm decode (20k parts)", 3, buffer.len(bin), function()
Rbxm.decode(Bytes.reader(bin))
end)
bench("rbxm encode (20k parts)", 3, buffer.len(bin), function()
Rbxm.encode(tree)
end)
bench("rbxmx decode (20k parts)", 3, buffer.len(xml), function()
Rbxmx.decode(Bytes.reader(xml))
end)
local M = require("@tests/gen/robloxmesh")
local mesh = buffer.fromstring(M.cases[#M.cases - 1].data)
bench("robloxmesh decode (v7, skinned)", 20, buffer.len(mesh), function()
RobloxMesh.decode(Bytes.reader(mesh))
end)
end
do
local Brotli = require("@formats/codecs/compress/Brotli")
local BB = require("@tests/gen/brotli_bench")
@@ -1078,6 +1125,240 @@ do
jitTune(false)
end
end
do
-- NBT / voxels (ws-voxel): a chunk-like NBT tree (24 sections with packed long arrays, 4000 entity-like compounds)
-- through codecs/text/Nbt (binary and SNBT), 1M packed 5-bit indices, a 64^3 Sponge schematic and Qubicle file
local Nbt = require("@formats/codecs/text/Nbt")
local Mc = require("@formats/codecs/model/Minecraft")
local Schematic = require("@formats/model/schematic")
local Qb = require("@formats/model/qb")
local sections = Nbt.list("compound")
for s = 1, 24 do
local pal = Nbt.list("compound")
for p = 1, 16 do
table.insert(pal, Nbt.compound({ Name = "minecraft:block_" .. p, Properties = Nbt.compound({ axis = "y" }, { "axis" }) }, { "Name", "Properties" }))
end
local longs = buffer.create(256 * 8)
for i = 0, 511 do
buffer.writeu32(longs, i * 4, (i * 2654435761) % 4294967296)
end
table.insert(sections, Nbt.compound({ Y = Nbt.byte(s - 5), block_states = Nbt.compound({ palette = pal, data = Nbt.longArray(longs) }, { "palette", "data" }) }, { "Y", "block_states" }))
end
local ents = Nbt.list("compound")
for i = 1, 4000 do
table.insert(ents, Nbt.compound({
id = "minecraft:thing",
Pos = Nbt.list("double", { Nbt.double(i * 0.5), Nbt.double(64), Nbt.double(-i * 0.25) }),
Health = Nbt.float(20),
UUID = Nbt.intArray({ i, -i, i * 3, 7 }),
Tags = Nbt.list("string", { "a", "b" }),
Age = Nbt.short(i % 1000),
Seed = Nbt.long(i * 1234567),
}, { "id", "Pos", "Health", "UUID", "Tags", "Age", "Seed" }))
end
local root = Nbt.compound({ DataVersion = 3953, sections = sections, entities = ents }, { "DataVersion", "sections", "entities" })
local raw = assert(Nbt.encode(root, { compression = "none" }))
bench("nbt decode (" .. buffer.len(raw) // 1000 .. " KB)", 5, buffer.len(raw), function()
Nbt.decode(raw)
end)
bench("nbt encode", 5, buffer.len(raw), function()
Nbt.encode(root, { compression = "none" })
end)
local text = assert(Nbt.print(root))
bench("snbt parse (" .. #text // 1000 .. " KB)", 3, #text, function()
Nbt.parse(text)
end)
bench("snbt print", 3, #text, function()
Nbt.print(root)
end)
local n = 1048576
local idx = buffer.create(n * 2)
for i = 0, n - 1 do
buffer.writeu16(idx, i * 2, (i * 7) % 32)
end
local packed = Mc.Packed.pack(idx, n, 5, false)
local out = buffer.create(n * 2)
bench("packed unpack 1M x 5 bits (MB out)", 5, n * 2, function()
Mc.Packed.unpack(packed, 5, n, false, out, 32)
end)
local V = 64
local states = { "minecraft:air" }
for i = 1, 40 do
table.insert(states, "minecraft:block_" .. i)
end
local cells = buffer.create(V * V * V * 2)
for i = 0, V * V * V - 1 do
buffer.writeu16(cells, i * 2, if (i // V) % 3 == 0 then 0 else 1 + (i // 97) % 40)
end
local grid = Mc.voxels(V, V, V, states, cells)
local schem = assert(Schematic.encode(grid, { compression = "none" }))
bench("schem decode 64^3 (MB of cells)", 3, V * V * V, function()
Schematic.decode(Bytes.reader(schem))
end)
bench("schem encode 64^3", 3, V * V * V, function()
Schematic.encode(grid, { compression = "none" })
end)
local qb = assert(Qb.encode(grid))
bench("qb decode 64^3 rle (MB of cells)", 3, V * V * V, function()
Qb.decode(Bytes.reader(qb))
end)
end
-- ------------------------------------------------------------------------------------------- ws-video-codecs: fps
do
local Jpeg = require("@formats/codecs/image/Jpeg")
local Mpeg1 = require("@formats/codecs/video/Mpeg1")
local Base64 = require("@formats/core/Base64")
local Types = require("@formats/Types")
local MB = require("@tests/gen/mjpeg_bench")
local M1 = require("@tests/gen/mpeg1_bench")
-- best of n runs of f (which handles `frames` frames): ms per frame and frames per second
local function fps(label: string, n: number, frames: number, f: () -> ())
f()
local dt = math.huge
for _ = 1, n do
local t0 = os.clock()
f()
dt = math.min(dt, os.clock() - t0)
end
print(string.format("%-8s %-30s %8.2f ms %7.1f fps", runtime, label, dt * 1000 / frames, frames / dt))
end
for _, c in MB.frames do
local b = Base64.decode(c.data) :: buffer
local img = assert(Jpeg.decode(b, 0, nil, { yuv = true }))
assert(Crc32.of(img.pixels) == c.yuv, "mjpeg bench: " .. c.name .. " does not match TurboJPEG")
fps("mjpeg decode yuv " .. c.name, 10, 1, function()
Jpeg.decode(b, 0, nil, { yuv = true })
end)
fps("mjpeg decode rgba " .. c.name, 10, 1, function()
Jpeg.decode(b)
end)
end
for _, s in MB.sources do
local img = Types.Image.new(s.width, s.height, "yuv420p")
img.pixels = assert(Deflate.zlib(Base64.decode(s.yuv) :: buffer))
fps(string.format("mjpeg encode yuv %dx%d q85", s.width, s.height), 5, 1, function()
Jpeg.encode(img, { quality = 85 })
end)
end
-- (LuaJIT also with the trace limits the api sets, host/init.luau: the default 1000 traces thrash on MPEG-1)
local function mpeg1(tag: string)
for _, s in M1 do
local file = Base64.decode(s.file) :: buffer
for _, rgba in { false, true } do
fps(string.format("mpeg1 decode %s %dx%d%s", if rgba then "rgba" else "yuv", s.width, s.height, tag), 3, #s.units, function()
local dec = Mpeg1.new({ reorder = true, rgba = rgba })
for i, u in s.units do
dec:decode(file, u[1], u[2], i)
end
while dec:flush() do
end
end)
end
end
end
-- MPEG-2: 720x576 interlaced (field prediction / field DCT, B-frames), as on a DVD (ws-video2)
local M2 = require("@tests/gen/mpeg2_bench")
local function mpeg2(tag: string)
for _, s in M2 do
local file = Base64.decode(s.file) :: buffer
fps(string.format("mpeg2 decode yuv %dx%d interlaced%s", s.width, s.height, tag), 3, #s.units, function()
local dec = Mpeg1.new({ reorder = true, rgba = false })
for i, u in s.units do
dec:decode(file, u[1], u[2], i)
end
while dec:flush() do
end
end)
end
end
jitTune(false)
mpeg1("")
mpeg2("")
if runtime == "luajit" then
jitTune(true)
mpeg1(" (jit tuned)")
mpeg2(" (jit tuned)")
jitTune(false)
end
end
do
-- ------------------------------------------------------------------------------------- ws-video2-theora: fps
-- Theora (codecs/video/Theora): 320x240 clips, 50 frames, keyframe every 25: testsrc2 at 600 kbit/s (mostly static:
-- few coded blocks) and a mandelbrot zoom at 1200 kbit/s (every block coded, moving); decode alone and decode +
-- image() (the yuv420p copy every VideoDecoder frame makes)
local Theora = require("@formats/codecs/video/Theora")
local Ogg = require("@formats/containers/Ogg")
local Base64 = require("@formats/core/Base64")
local TB = require("@tests/gen/theora_bench")
local function run(tag: string)
for _, c in TB do
local d = Ogg.open(Base64.decode(c.file) :: buffer)
local hdrs, data = {}, {}
for p in d:packets(d:find("theora")) do
table.insert(if #hdrs < 3 then hdrs else data, Ogg.copy(p))
end
for _, image in { false, true } do
local n = c.width * c.height * 3 // 2 * #data
local dt = bench(string.format("theora %s %dx%d%s%s", c.name, c.width, c.height, if image then " +img" else "", tag), 3, n, function()
local dec = Theora.new(hdrs) :: any
for _, p in data do
dec:decode(p)
if image then
dec:image()
end
end
end)
print(string.format("%-8s %-30s %8.1f fps", runtime, " (" .. #data .. " frames)", #data / dt))
end
end
end
jitTune(false)
run("")
if runtime == "luajit" then
jitTune(true)
run(" (jit tuned)")
jitTune(false)
end
end
-- ------------------------------------------------------------------------------------------- ws-video2-legacy: fps
do
-- the legacy codecs (codecs/video/Legacy) through F.video.open, rgba8 frames out: ffmpeg's encoders at 320x240
-- (tools/fixtures/legacy.py), a 320x200 FLC (fli.py) and a 320x200 SMK4 (smk.py, textured: mostly FULL blocks);
-- frames per second of the whole decode (and the Smacker audio track, seconds of audio per second)
local F = require("@formats")
local LB = require("@tests/gen/legacy_bench")
local cases = table.clone(LB)
table.insert(cases, { name = "fli.flc", data = require("@tests/gen/fli_bench").data, width = 320, height = 200 })
table.insert(cases, { name = "smk4.smk", data = require("@tests/gen/smk_bench").data, width = 320, height = 200 })
for _, c in cases do
local data = buffer.fromstring(c.data)
local frames = 0
local dt = math.huge
for _ = 1, 3 do
local t0 = os.clock()
local v = assert(F.video.open(data, { audio = false }))
frames = 0
while v:nextFrame() do
frames += 1
end
dt = math.min(dt, os.clock() - t0)
end
print(string.format("%-8s %-30s %8.2f ms %7.1f fps", runtime, string.format("legacy %s %dx%d", c.name, c.width, c.height), dt * 1000 / frames, frames / dt))
end
local v = assert(F.video.open(buffer.fromstring(require("@tests/gen/smk_bench").data)))
local a = assert(v.audio)
local t0 = os.clock()
local n = 0
while true do
local _, got = a:read(4096)
if got == 0 then
break
end
n += got
end
local dt = os.clock() - t0
print(string.format("%-8s %-30s %8.2f ms %7.1fx real time", runtime, "smacker audio 22 kHz stereo", dt * 1000, n / a.sampleRate / dt))
end
-- ws-audio2: chip/midi
do
-- AHX / HVL (codecs/audio/Hively): a whole song rendered at 44.1 kHz (tools/fixtures/ahx.py karma.ahx, 3 s; hvl.py