- codecs/video/Mpeg2/Decoder: coefficient loop keeps the bit position in a local and reads a 25-bit window with one readu32 (no fifth byte); MPEG-1 oddification with bor; dequant shifts; tracks the highest coefficient so the IDCT skips the zero rows; toRgba / toRgba422 through shifted-channel lookup tables (one rshift + one index per channel, the alpha folded in), two rows per chroma row without a per-pixel branch - codecs/video/Dsp: idct(blk, dst, off, stride, add, last?): pass 1 only over the rows up to last's, pass 2 with the rows-4..7-zero branch tree when they are, fused with the put / add (rounding and the CLAMP offset in one division); mc: an unrolled whole-sample copy for 8 / 16 wide blocks, the 4-tap case a column at a time reusing each row's pair sums - codecs/image/Yuv.toRgba: per-matrix luma table, shifted-channel clip tables, chroma once per 2x2 - codecs/video/H264: prediction scratch as buffers (put = u32 copy, bi-pred average four samples at a time), whole-sample single-list blocks copied straight from the reference, implicit / identity weights routed to the plain paths, 2-tap chroma when a fraction is 0 and the bilinear taps slide along the row, six-tap with two multiplies; deblocking skips the inner edges of macroblocks with one motion and no luma coefficients (S.flatTab) and finds equal motion before motionDiffers - codecs/image/Vp8/Predict: six-tap filters with a sliding window (one read per sample) - --!optimize 2 on every video codec / container module that lacked it (the bundles already force it) - tools/bench_video.luau: the video fps lines on their own (sections, "once", "jitdefault"), more MPEG-1 clips
229 lines
7.3 KiB
Luau
229 lines
7.3 KiB
Luau
-- Video decode fps (luau -O2 tools/bench_video.luau [-a section...]): the tools/bench.luau video lines on their own.
|
|
-- Sections: mpeg1 mpeg2 yuv (Yuv.toRgba and the MPEG decoders' own) ts h264 vp8 theora h263 legacy containers (default: all). Best of n runs.
|
|
local Crc32 = require("@rosetta/core/Crc32")
|
|
local Base64 = require("@rosetta/core/Base64")
|
|
|
|
local runtime = if (_G :: any).jit ~= nil then "luajit" else "luau"
|
|
local args = { ... }
|
|
local only: { [string]: boolean } = {}
|
|
local once = false -- "once": a single run of each line (for instruction counts)
|
|
local jitDefault = false -- "jitdefault": LuaJIT's own trace limits instead of the ones host/init.luau sets
|
|
for _, a in args do
|
|
if a == "once" then
|
|
once = true
|
|
elseif a == "jitdefault" then
|
|
jitDefault = true
|
|
else
|
|
only[a] = true
|
|
end
|
|
end
|
|
local function want(s: string): boolean
|
|
return next(only) == nil or only[s] == true
|
|
end
|
|
|
|
local jit = (_G :: any).jit
|
|
if jit and type(jit.opt) == "table" and not jitDefault then
|
|
jit.opt.start("maxtrace=8000", "maxmcode=65536")
|
|
end
|
|
|
|
local function fps(label: string, n: number, frames: number, f: () -> ()): number
|
|
f()
|
|
local dt = math.huge
|
|
for _ = 1, if once then 1 else n do
|
|
local t0 = os.clock()
|
|
f()
|
|
dt = math.min(dt, os.clock() - t0)
|
|
end
|
|
print(string.format("%-8s %-36s %8.2f ms %7.1f fps", runtime, label, dt * 1000 / frames, frames / dt))
|
|
return dt
|
|
end
|
|
|
|
-- one decoder over every unit of s, then flushed; check (untimed runs only) folds each frame's planes into a crc
|
|
local function mpegRun(Mpeg1: any, s: any, file: buffer, rgba: boolean, check: boolean): number
|
|
local dec = Mpeg1.new({ reorder = true, rgba = rgba })
|
|
local c = 0
|
|
local function take(f: any)
|
|
if f and check then
|
|
c = bit32.bxor(c, Crc32.of(if rgba then f.pixels else f.y), Crc32.of(f.cb))
|
|
end
|
|
end
|
|
for i, u in s.units do
|
|
take(dec:decode(file, u[1], u[2], i))
|
|
end
|
|
while true do
|
|
local f = dec:flush()
|
|
if not f then
|
|
break
|
|
end
|
|
take(f)
|
|
end
|
|
return c
|
|
end
|
|
|
|
if want("mpeg1") then
|
|
local Mpeg1 = require("@rosetta/codecs/video/Mpeg1")
|
|
-- the bench clips (testsrc2, mostly static: full-pel copies) and three test clips with motion everywhere
|
|
local clips = table.clone(require("@tests/gen/mpeg1_bench"))
|
|
table.insert(clips, require("@tests/gen/mpeg1_zoom_1500k"))
|
|
table.insert(clips, require("@tests/gen/mpeg1_bframes_zoom"))
|
|
table.insert(clips, require("@tests/gen/mpeg1_big_1000k"))
|
|
for _, s in clips do
|
|
local file = Base64.decode(s.file) :: buffer
|
|
for _, rgba in { false, true } do
|
|
fps(string.format("mpeg1 %s %s %dx%d", s.name or "bench", if rgba then "rgba" else "yuv", s.width, s.height), 5, #s.units, function()
|
|
mpegRun(Mpeg1, s, file, rgba, false)
|
|
end)
|
|
print(string.format("%-8s crc %08x", runtime, mpegRun(Mpeg1, s, file, rgba, true)))
|
|
end
|
|
end
|
|
end
|
|
|
|
if want("mpeg2") then
|
|
local Mpeg1 = require("@rosetta/codecs/video/Mpeg1")
|
|
for _, s in require("@tests/gen/mpeg2_bench") do
|
|
local file = Base64.decode(s.file) :: buffer
|
|
fps(string.format("mpeg2 yuv %dx%d interlaced", s.width, s.height), 3, #s.units, function()
|
|
mpegRun(Mpeg1, s, file, false, false)
|
|
end)
|
|
print(string.format("%-8s crc %08x", runtime, mpegRun(Mpeg1, s, file, false, true)))
|
|
end
|
|
end
|
|
|
|
if want("yuv") then
|
|
local Yuv = require("@rosetta/codecs/image/Yuv")
|
|
for _, d in { { 320, 240 }, { 640, 360 } } do
|
|
local w, h = d[1], d[2]
|
|
local cw, ch = w // 2, h // 2
|
|
local src = buffer.create(w * h + 2 * cw * ch)
|
|
local r = 12345
|
|
for i = 0, buffer.len(src) - 1 do
|
|
r = (r * 1103515245 + 12345) % 2147483648
|
|
buffer.writeu8(src, i, r // 65536 % 256)
|
|
end
|
|
local out = buffer.create(w * h * 4)
|
|
fps(string.format("yuv toRgba %dx%d", w, h), 20, 1, function()
|
|
Yuv.toRgba(src, w, h, w * h, w * h + cw * ch, cw, nil, nil, out)
|
|
end)
|
|
print(string.format("%-8s crc %08x", runtime, Crc32.of(out)))
|
|
local Mpeg1 = require("@rosetta/codecs/video/Mpeg1")
|
|
local yp, cbp, crp = buffer.create(w * h), buffer.create(cw * ch), buffer.create(cw * ch)
|
|
buffer.copy(yp, 0, src, 0, w * h)
|
|
buffer.copy(cbp, 0, src, w * h, cw * ch)
|
|
buffer.copy(crp, 0, src, w * h + cw * ch, cw * ch)
|
|
fps(string.format("mpeg1 toRgba %dx%d", w, h), 20, 1, function()
|
|
Mpeg1.toRgba(yp, cbp, crp, w, w, h, out)
|
|
end)
|
|
print(string.format("%-8s crc %08x", runtime, Crc32.of(out)))
|
|
end
|
|
end
|
|
|
|
local function videoOpen(names: { [string]: boolean }, tag: string)
|
|
local F = require("@rosetta")
|
|
for _, c in require("@tests/gen/videobench") do
|
|
if names[c.name] then
|
|
local data = buffer.fromstring(c.data)
|
|
for _, rgba in { false, true } do
|
|
local frames = 0
|
|
local dt = math.huge
|
|
for _ = 1, 3 do
|
|
local t0 = os.clock()
|
|
local v = assert(F.video.open(data, { audio = false, rgba = rgba }))
|
|
frames = 0
|
|
while v:nextFrame() do
|
|
frames += 1
|
|
end
|
|
dt = math.min(dt, os.clock() - t0)
|
|
end
|
|
print(string.format("%-8s %-36s %8.2f ms %7.1f fps", runtime, string.format("%s %s %s %s", tag, c.size, c.name, if rgba then "rgba" else "yuv"), dt * 1000 / frames, frames / dt))
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
if want("ts") then
|
|
videoOpen({ ["mpeg1.ts"] = true }, "video")
|
|
end
|
|
|
|
if want("h264") then
|
|
local H264 = require("@rosetta/codecs/video/H264")
|
|
for _, c in require("@tests/gen/h264_bench") do
|
|
fps(string.format("h264 %s %dx%d", c.name, c.width, c.height), 3, #c.frames, function()
|
|
H264.decodeAll(c.data)
|
|
end)
|
|
end
|
|
end
|
|
|
|
if want("vp8") then
|
|
local Vp8 = require("@rosetta/codecs/image/Vp8")
|
|
for _, c in require("@tests/gen/vp8video_bench") 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
|
|
fps(string.format("vp8 %dx%d", c.width, c.height), 3, #frames, function()
|
|
local dec = Vp8.newDecoder()
|
|
for _, f in frames do
|
|
dec:decodeFrame(b, f[1], f[2])
|
|
end
|
|
end)
|
|
end
|
|
end
|
|
|
|
if want("theora") then
|
|
local Theora = require("@rosetta/codecs/video/Theora")
|
|
local Ogg = require("@rosetta/containers/Ogg")
|
|
for _, c in require("@tests/gen/theora_bench") 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
|
|
fps(string.format("theora %s %dx%d", c.name, c.width, c.height), 3, #data, function()
|
|
local dec = Theora.new(hdrs) :: any
|
|
for _, p in data do
|
|
dec:decode(p)
|
|
end
|
|
end)
|
|
end
|
|
end
|
|
|
|
if want("h263") then
|
|
local H263 = require("@rosetta/codecs/video/H263")
|
|
for _, c in require("@tests/gen/h263_bench") do
|
|
local file = Base64.decode(c.file) :: buffer
|
|
fps(string.format("%s %dx%d", c.name, c.width, c.height), 3, #c.units, function()
|
|
local dec = H263.new({ flv = c.flv })
|
|
for _, u in c.units do
|
|
dec:decode(file, u[1], u[2])
|
|
end
|
|
end)
|
|
end
|
|
end
|
|
|
|
if want("legacy") then
|
|
local F = require("@rosetta")
|
|
for _, c in require("@tests/gen/legacy_bench") 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 %-36s %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
|
|
end
|
|
|
|
if want("containers") then
|
|
videoOpen({ ["mjpeg.avi"] = true, ["vp8.webm"] = true, ["raw.y4m"] = true }, "video")
|
|
end
|