merge ws/video: containers mkv/avi/mp4/ts/ps/y4m (+writers), codecs Jpeg/Mpeg1/Vp8 inter/Video registry, AudioWrap, F.video.open

This commit is contained in:
4DBug committed 2026-10-31 10:22:17 -05:00
commit 2eaed46eea
96 files changed
+13398 -517

No files matched your search

+132
View File
@@ -898,3 +898,135 @@ do
Step.encode(cad)
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
-- ------------------------------------------------------------------------------------------- ws-video: containers
do
local F = require("@formats")
local VB = require("@tests/gen/videobench")
local function clock(n: number, f: () -> number): (number, number)
local frames = f()
local dt = math.huge
for _ = 1, n do
local t0 = os.clock()
frames = f()
dt = math.min(dt, os.clock() - t0)
end
return dt, frames
end
for _, c in VB do
local data = buffer.fromstring(c.data)
local dtOpen = clock(3, function(): number
F.video.open(data, { audio = false })
return 1
end)
for _, rgba in { false, true } do
local ok, dt, frames = pcall(clock, 2, function(): number
local v = assert(F.video.open(data, { audio = false, rgba = rgba }))
local n = 0
while true do
local f, err = v:nextFrame()
if not f then
assert(err == nil, tostring(err))
break
end
n += 1
end
return n
end)
if ok then
print(string.format("%-8s %-30s %8.2f ms %7.1f fps (open %.1f ms)", runtime, string.format("video %s %s %s", c.size, c.name, if rgba then "rgba" else "yuv"), dt * 1000 / frames, frames / dt, dtOpen * 1000))
else
print(string.format("%-8s %-30s %s", runtime, "video " .. c.size .. " " .. c.name, tostring(dt)))
end
end
if c.name == "raw.y4m" then
local dt, frames = clock(2, function(): number
assert(F.convert(data, "avi", { quality = 85 }))
return 8
end)
print(string.format("%-8s %-30s %8.2f ms %7.1f fps", runtime, "video " .. c.size .. " y4m -> mjpeg avi", dt * 1000 / frames, frames / dt))
end
end
end
-- ------------------------------------------------------------------------------------------- ws-video-vp8: fps
do
-- VP8 video (codecs/Vp8, tools/fixtures/vp8video.py): whole IVF streams (libvpx, panning testsrc2) through one
-- decoder object; frames per second. On LuaJIT the library's host module raises the trace limits (maxtrace=8000,
-- see src/Formats/host/init.luau): the second pair of lines runs with them, as the api does.
local Vp8 = require("@formats/codecs/Vp8")
local VB = require("@tests/gen/vp8video_bench")
local function run(tag: string)
for _, c in VB do
local b = buffer.fromstring(c.ivf)
local frames = {}
local p = 32
while p + 12 <= buffer.len(b) do
local n = buffer.readu32(b, p)
table.insert(frames, { p + 12, n })
p += 12 + n
end
local dt = bench(string.format("vp8 decode %dx%d%s", c.width, c.height, tag), 3, c.width * c.height * 1.5 * #frames, function()
local dec = Vp8.newDecoder()
for _, f in frames do
dec:decodeFrame(b, f[1], f[2])
end
end)
print(string.format("%-8s %-30s %8.1f fps", runtime, " (" .. #frames .. " frames)", #frames / dt))
end
end
run("")
if runtime == "luajit" then
require("@formats/host")
run(" (jit tuned)")
end
end