From 850ec634a6ab8576c8611174328ed2e952de29aa Mon Sep 17 00:00:00 2001 From: 4DBug Date: Sat, 31 Oct 2026 09:49:33 -0500 Subject: [PATCH] ws-video-codecs: bench lines: MJPEG decode (yuv / rgba) and yuv420p encode fps at 320x240 / 640x480, MPEG-1 decode fps (yuv / rgba) --- tools/bench.luau | 51 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/tools/bench.luau b/tools/bench.luau index 710195d..decc77c 100644 --- a/tools/bench.luau +++ b/tools/bench.luau @@ -535,3 +535,54 @@ do Webp.encode(img, { effort = 1 }) end) end +-- ------------------------------------------------------------------------------------------- ws-video-codecs: fps +do + local Jpeg = require("@formats/codecs/Jpeg") + local Mpeg1 = require("@formats/codecs/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 + 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", if rgba then "rgba" else "yuv", s.width, s.height), 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