ws-video2-theora: Theora speed (fused prediction + IDCT add, inlined token decode, table-driven tokens), bench lines, guide section

tools/bench.luau: a ws-video2-theora block (320x240 testsrc2 / mandelbrot, decode and
decode + image()). docs/guide/video.md: an Ogg Theora section (tested snippet; inputs
from the new tools/fixtures/docs_theora.py) and the formats / writers tables.
This commit is contained in:
4DBug committed 2026-10-31 14:43:22 -05:00
1 parent 4dcb005180
commit 1013925a85
6 files changed
+342 -222

No files matched your search

+247 -215
View File
@@ -24,9 +24,10 @@ local writeu8 = buffer.writeu8
local readu16 = buffer.readu16
local readi16 = buffer.readi16
local writei16 = buffer.writei16
local readi32 = buffer.readi32
local writei32 = buffer.writei32
local readu32 = buffer.readu32
local writeu32 = buffer.writeu32
local band = bit32.band
local bxor = bit32.bxor
local bcopy = buffer.copy
local bfill = buffer.fill
local lshift = bit32.lshift
@@ -93,10 +94,56 @@ do
end
-- DCT tokens: extra bits per token
local TOKEN_EXTRA = {
[0] = 0, 0, 0, 2, 3, 4, 12, 3, 6, 0, 0, 0, 0, 1, 1, 1, 1, 2, 3, 4, 5, 6, 10, 1, 1, 1, 1, 1, 3, 4, 2, 3,
local TOKEN_EXTRA = { -- [token + 1]
0, 0, 0, 2, 3, 4, 12, 3, 6, 0, 0, 0, 0, 1, 1, 1, 1, 2, 3, 4, 5, 6, 10, 1, 1, 1, 1, 1, 3, 4, 2, 3,
}
-- the zero-run / coefficient tokens (7..31) by token * 1024 + extra bits + 1: run * 4096 + value + 2048, where run is
-- the zeros before the value (value 0: a pure zero run of run + 1)
local TOKEN_VALUE = table.create(32 * 1024, 2048)
for tok = 7, 31 do
local ex = TOKEN_EXTRA[tok + 1]
for eb = 0, 2 ^ ex - 1 do
local v, r = 0, 0
if tok < 9 then
r = eb -- zero runs: eb + 1 zeros
elseif tok < 13 then
v = if tok == 9 then 1 elseif tok == 10 then -1 elseif tok == 11 then 2 else -2
elseif tok < 17 then
v = if eb == 1 then -(tok - 10) else tok - 10
elseif tok < 23 then
local half = 2 ^ (ex - 1) -- the top extra bit is the sign
local base = if tok == 17 then 7 elseif tok == 18 then 9 elseif tok == 19 then 13 elseif tok == 20 then 21 elseif tok == 21 then 37 else 69
v = base + eb % half
if eb >= half then
v = -v
end
elseif tok < 28 then
r = tok - 22
v = if eb == 1 then -1 else 1
elseif tok == 28 then
r = 6 + eb % 4
v = if eb >= 4 then -1 else 1
elseif tok == 29 then
r = 10 + eb % 8
v = if eb >= 8 then -1 else 1
elseif tok == 30 then
r = 1
v = 2 + eb % 2
if eb >= 2 then
v = -v
end
else
r = 2 + eb % 2
v = 2 + (eb // 2) % 2
if eb >= 4 then
v = -v
end
end
TOKEN_VALUE[tok * 1024 + eb + 1] = r * 4096 + v + 2048
end
end
-- natural index of zig-zag position (1-based by position + 1)
local ZIGZAG = Header.ZIGZAG
@@ -158,9 +205,8 @@ export type State = {
lflim: { number },
huff: { number },
roots: { number },
blk: buffer, -- i32 x 64 work blocks
blk: buffer, -- i16 x 64 work blocks
tmp: buffer,
res: buffer,
bv: { number }, -- the loop filter's bounding values for the frame, [(f + 4) >> 3 + 129]
keyframes: number,
}
@@ -191,9 +237,8 @@ function D.new(info: Header.Info, setup: Header.Setup): State
lflim = setup.lflim,
huff = setup.huff,
roots = setup.roots,
blk = buffer.create(256),
tmp = buffer.create(256),
res = buffer.create(256),
blk = buffer.create(128),
tmp = buffer.create(128),
bv = table.create(257, 0),
keyframes = 0,
}
@@ -366,23 +411,24 @@ local function idct8(x0: number, x1: number, x2: number, x3: number, x4: number,
return t0 + t7, t1 + t6, t2 + t5, t3 + t4, t3 - t4, t2 - t5, t1 - t6, t0 - t7
end
-- 2-D IDCT of blk (i16, natural order) into res (i32 residuals). Like libtheora, the dequantised coefficients and
-- the first pass's outputs are 16-bit (the i16 buffers wrap them), which also bounds every residual to +-24000.
local function idct(blk: buffer, tmp: buffer, res: buffer)
-- the first IDCT pass: the rows of blk (i16, natural order) into the columns of tmp (i16: like libtheora, the
-- dequantised coefficients and these intermediate values are 16-bit, which also bounds every residual)
local function idctRows(blk: buffer, tmp: buffer)
for i = 0, 7 do
local o = i * 16
local x0, x1, x2, x3 = readi16(blk, o), readi16(blk, o + 2), readi16(blk, o + 4), readi16(blk, o + 6)
local x4, x5, x6, x7 = readi16(blk, o + 8), readi16(blk, o + 10), readi16(blk, o + 12), readi16(blk, o + 14)
local c = i * 2
if x0 == 0 and x1 == 0 and x2 == 0 and x3 == 0 and x4 == 0 and x5 == 0 and x6 == 0 and x7 == 0 then
writei16(tmp, c, 0)
writei16(tmp, c + 16, 0)
writei16(tmp, c + 32, 0)
writei16(tmp, c + 48, 0)
writei16(tmp, c + 64, 0)
writei16(tmp, c + 80, 0)
writei16(tmp, c + 96, 0)
writei16(tmp, c + 112, 0)
if x1 == 0 and x2 == 0 and x3 == 0 and x4 == 0 and x5 == 0 and x6 == 0 and x7 == 0 then
local y = (C4 * x0) // 65536
writei16(tmp, c, y)
writei16(tmp, c + 16, y)
writei16(tmp, c + 32, y)
writei16(tmp, c + 48, y)
writei16(tmp, c + 64, y)
writei16(tmp, c + 80, y)
writei16(tmp, c + 96, y)
writei16(tmp, c + 112, y)
else
local y0, y1, y2, y3, y4, y5, y6, y7 = idct8(x0, x1, x2, x3, x4, x5, x6, x7)
writei16(tmp, c, y0)
@@ -395,20 +441,29 @@ local function idct(blk: buffer, tmp: buffer, res: buffer)
writei16(tmp, c + 112, y7)
end
end
for i = 0, 7 do
local o = i * 16
local x0, x1, x2, x3 = readi16(tmp, o), readi16(tmp, o + 2), readi16(tmp, o + 4), readi16(tmp, o + 6)
local x4, x5, x6, x7 = readi16(tmp, o + 8), readi16(tmp, o + 10), readi16(tmp, o + 12), readi16(tmp, o + 14)
local y0, y1, y2, y3, y4, y5, y6, y7 = idct8(x0, x1, x2, x3, x4, x5, x6, x7)
local c = i * 4
writei32(res, c, (y0 + 8) // 16)
writei32(res, c + 32, (y1 + 8) // 16)
writei32(res, c + 64, (y2 + 8) // 16)
writei32(res, c + 96, (y3 + 8) // 16)
writei32(res, c + 128, (y4 + 8) // 16)
writei32(res, c + 160, (y5 + 8) // 16)
writei32(res, c + 192, (y6 + 8) // 16)
writei32(res, c + 224, (y7 + 8) // 16)
end
-- the second pass, added to the prediction already in dst: column c of the block is row c of tmp
local function idctAdd(tmp: buffer, dst: buffer, pos: number, stride: number)
for c = 0, 7 do
local o = c * 16
local y0, y1, y2, y3, y4, y5, y6, y7 = idct8(readi16(tmp, o), readi16(tmp, o + 2), readi16(tmp, o + 4), readi16(tmp, o + 6), readi16(tmp, o + 8), readi16(tmp, o + 10), readi16(tmp, o + 12), readi16(tmp, o + 14))
local p = pos + c
writeu8(dst, p, readu8(CLAMP, readu8(dst, p) + (y0 + 8) // 16 + COFF))
p += stride
writeu8(dst, p, readu8(CLAMP, readu8(dst, p) + (y1 + 8) // 16 + COFF))
p += stride
writeu8(dst, p, readu8(CLAMP, readu8(dst, p) + (y2 + 8) // 16 + COFF))
p += stride
writeu8(dst, p, readu8(CLAMP, readu8(dst, p) + (y3 + 8) // 16 + COFF))
p += stride
writeu8(dst, p, readu8(CLAMP, readu8(dst, p) + (y4 + 8) // 16 + COFF))
p += stride
writeu8(dst, p, readu8(CLAMP, readu8(dst, p) + (y5 + 8) // 16 + COFF))
p += stride
writeu8(dst, p, readu8(CLAMP, readu8(dst, p) + (y6 + 8) // 16 + COFF))
p += stride
writeu8(dst, p, readu8(CLAMP, readu8(dst, p) + (y7 + 8) // 16 + COFF))
end
end
@@ -426,7 +481,8 @@ local function mvSplit(d: number, q: boolean): (number, number)
return a, b
end
-- reconstruct one coded fragment into dst (the SELF reference planes)
-- reconstruct one coded fragment into dst (the SELF reference planes): the prediction (128 for intra blocks, the
-- reference block or the average of two for inter ones) goes to dst first, then the residual is added
local function reconFrag(st: State, fi: number, qis: { number }, dstPlanes: { buffer })
local pli = st.fpl[fi]
local mode = st.fmode[fi]
@@ -435,12 +491,20 @@ local function reconFrag(st: State, fi: number, qis: { number }, dstPlanes: { bu
local dcq = readu16(dq, (((qis[1] * 3 + pli) * 2 + qti) * 64) * 2)
local dc = st.dcv[fi]
local last = st.last[fi]
local res = st.res
local flat = false
local stride = st.stride[pli + 1]
local dst = dstPlanes[pli + 1]
local pos = st.fpos[fi]
local ri = st.refi[fi]
local fv = 0
if last == 0 then
flat = true
fv = wrap16((dc * dcq + 15) // 32)
if ri == SELF then
local v = readu8(CLAMP, fv + 128 + COFF)
for y = 0, 7 do
bfill(dst, pos + y * stride, v, 8)
end
return
end
else
local blk = st.blk
bfill(blk, 0, 0, 128)
@@ -456,50 +520,44 @@ local function reconFrag(st: State, fi: number, qis: { number }, dstPlanes: { bu
end
end
st.last[fi] = 0
idct(blk, st.tmp, res)
idctRows(blk, st.tmp)
end
local stride = st.stride[pli + 1]
local dst = dstPlanes[pli + 1]
local pos = st.fpos[fi]
local ri = st.refi[fi]
-- the prediction
if ri == SELF then
if flat then
local v = readu8(CLAMP, fv + 128 + COFF)
for y = 0, 7 do
bfill(dst, pos + y * stride, 128, 8)
end
else
local ref = st.refs[if ri == GOLD then st.gold else st.prev][pli + 1]
local mx, mx2 = mvSplit(st.mvx[fi], pli > 0 and st.hdec == 1)
local my, my2 = mvSplit(st.mvy[fi], pli > 0 and st.vdec == 1)
local s1 = pos + my * stride + mx
if mx2 ~= 0 or my2 ~= 0 then
-- (a + b) >> 1 per byte, four at a time
local s2 = s1 + my2 * stride + mx2
for y = 0, 7 do
bfill(dst, pos + y * stride, v, 8)
local d, a, b = pos + y * stride, s1 + y * stride, s2 + y * stride
local u, v = readu32(ref, a), readu32(ref, b)
writeu32(dst, d, band(u, v) + rshift(band(bxor(u, v), 0xFEFEFEFE), 1))
u, v = readu32(ref, a + 4), readu32(ref, b + 4)
writeu32(dst, d + 4, band(u, v) + rshift(band(bxor(u, v), 0xFEFEFEFE), 1))
end
else
for y = 0, 7 do
local d, s = pos + y * stride, y * 32
for x = 0, 7 do
writeu8(dst, d + x, readu8(CLAMP, readi32(res, s + x * 4) + 128 + COFF))
end
local d, a = pos + y * stride, s1 + y * stride
writeu32(dst, d, readu32(ref, a))
writeu32(dst, d + 4, readu32(ref, a + 4))
end
end
return
end
local ref = st.refs[if ri == GOLD then st.gold else st.prev][pli + 1]
local qx = pli > 0 and st.hdec == 1
local qy = pli > 0 and st.vdec == 1
local mx, mx2 = mvSplit(st.mvx[fi], qx)
local my, my2 = mvSplit(st.mvy[fi], qy)
local s1 = pos + my * stride + mx
if mx2 ~= 0 or my2 ~= 0 then
local s2 = s1 + my2 * stride + mx2
-- the residual
if last ~= 0 then
idctAdd(st.tmp, dst, pos, stride)
elseif fv ~= 0 then
for y = 0, 7 do
local d, a, b, r = pos + y * stride, s1 + y * stride, s2 + y * stride, y * 32
for x = 0, 7 do
local p = (readu8(ref, a + x) + readu8(ref, b + x)) // 2
local v = if flat then fv else readi32(res, r + x * 4)
writeu8(dst, d + x, readu8(CLAMP, p + v + COFF))
end
end
else
for y = 0, 7 do
local d, a, r = pos + y * stride, s1 + y * stride, y * 32
for x = 0, 7 do
local v = if flat then fv else readi32(res, r + x * 4)
writeu8(dst, d + x, readu8(CLAMP, readu8(ref, a + x) + v + COFF))
local d = pos + y * stride
for x = d, d + 7 do
writeu8(dst, x, readu8(CLAMP, readu8(dst, x) + fv + COFF))
end
end
end
@@ -593,6 +651,88 @@ local function borders(st: State, planes: { buffer })
end
end
-- DC prediction (raster order per plane): each coded block's DC adds a prediction from its left / upper neighbours
-- coded from the same reference frame (or the last such DC in the plane)
local function dcPredict(st: State)
local coded, refi, dcv = st.coded, st.refi, st.dcv
for pli = 0, 2 do
local nh, nv = st.nhf[pli + 1], st.nvf[pli + 1]
local base = st.foff[pli + 1]
local pl0, pl1, pl2 = 0, 0, 0 -- the last DC of each reference frame (GOLD, PREV, SELF)
for fy = 0, nv - 1 do
local row = base + fy * nh
local lref, ulref, uref = -1, -1, -1
if fy > 0 then
uref = refi[row - nh + 1]
end
for fx = 0, nh - 1 do
local fi = row + fx + 1
local urref = -1
if fy > 0 and fx + 1 < nh then
urref = refi[fi - nh + 1]
end
if coded[fi] ~= 0 then
local r = refi[fi]
local pred: number
if fy == 0 then
pred = if r == 0 then pl0 elseif r == 1 then pl1 else pl2
else
local m = (if lref == r then 1 else 0) + (if ulref == r then 2 else 0) + (if uref == r then 4 else 0) + (if urref == r then 8 else 0)
if m == 0 then
pred = if r == 0 then pl0 elseif r == 1 then pl1 else pl2
elseif m == 1 or m == 3 then
pred = dcv[fi - 1]
elseif m == 2 then
pred = dcv[fi - nh - 1]
elseif m == 4 or m == 6 or m == 12 then
pred = dcv[fi - nh]
elseif m == 5 then
local s = dcv[fi - 1] + dcv[fi - nh]
pred = if s < 0 then -((-s) // 2) else s // 2
elseif m == 8 then
pred = dcv[fi - nh + 1]
elseif m == 9 or m == 11 or m == 13 then
local s = 75 * dcv[fi - 1] + 53 * dcv[fi - nh + 1]
pred = if s < 0 then -((-s) // 128) else s // 128
elseif m == 10 then
local s = dcv[fi - nh - 1] + dcv[fi - nh + 1]
pred = if s < 0 then -((-s) // 2) else s // 2
elseif m == 14 then
local s = 3 * (dcv[fi - nh - 1] + dcv[fi - nh + 1]) + 10 * dcv[fi - nh]
pred = if s < 0 then -((-s) // 16) else s // 16
else
local p0, p1, p2 = dcv[fi - 1], dcv[fi - nh - 1], dcv[fi - nh]
local s = 29 * (p0 + p2) - 26 * p1
pred = if s < 0 then -((-s) // 32) else s // 32
if math.abs(pred - p2) > 128 then
pred = p2
elseif math.abs(pred - p0) > 128 then
pred = p0
elseif math.abs(pred - p1) > 128 then
pred = p1
end
end
end
local v = wrap16(dcv[fi] + pred)
dcv[fi] = v
if r == 0 then
pl0 = v
elseif r == 1 then
pl1 = v
else
pl2 = v
end
lref = r
else
lref = -1
end
ulref = uref
uref = urref
end
end
end
end
-- ------------------------------------------------------------------------------------------------ frame decode
-- decode(st, data, off, len) -> kind ("key" | "inter" | "dup") | nil, message. The picture is then in
-- st.refs[st.shown].
@@ -609,31 +749,11 @@ function D.decode(st: State, data: buffer, off: number, len: number): (string?,
if pos > maxp then
pos = maxp
end
local w = rshift(lshift(bswap(readu32(bb, pos // 8)), pos % 8), 32 - n)
local w = rshift(lshift(bswap(readu32(bb, rshift(pos, 3))), band(pos, 7)), 32 - n)
pos += n
return w
end
local huff = st.huff
local function token(root: number): number
if pos > maxp then
pos = maxp
end
local o = root
while true do
local n = huff[o]
local w = rshift(lshift(bswap(readu32(bb, pos // 8)), pos % 8), 32 - n)
local e = huff[o + 1 + w]
if e >= 0 then
pos += e // 64
return e % 64
end
pos += n
o = -e
if pos > maxp then
pos = maxp
end
end
end
local function sbRun(): number
if bits(1) == 0 then
return 1
@@ -1028,9 +1148,28 @@ function D.decode(st: State, data: buffer, off: number, len: number): (string?,
eobs -= 1
t = 64
else
local tok = token(root)
local ex = TOKEN_EXTRA[tok]
local eb = if ex > 0 then bits(ex) else 0
-- the token (inlined: the hottest loop), then its extra bits
if pos > maxp then
pos = maxp
end
local o, tok = root, 0
while true do
local nb = huff[o]
local e = huff[o + 1 + rshift(lshift(bswap(readu32(bb, rshift(pos, 3))), band(pos, 7)), 32 - nb)]
if e >= 0 then
pos += rshift(e, 6)
tok = band(e, 63)
break
end
pos += nb
o = -e
end
local ex = TOKEN_EXTRA[tok + 1]
local eb = 0
if ex > 0 then
eb = rshift(lshift(bswap(readu32(bb, rshift(pos, 3))), band(pos, 7)), 32 - ex)
pos += ex
end
if tok < 7 then
local run: number
if tok < 3 then
@@ -1046,49 +1185,18 @@ function D.decode(st: State, data: buffer, off: number, len: number): (string?,
end
eobs = run - 1
t = 64
elseif tok < 9 then
t = zzi + eb + 1
else
local v, r = 0, 0
if tok < 13 then
v = if tok == 9 then 1 elseif tok == 10 then -1 elseif tok == 11 then 2 else -2
elseif tok < 17 then
v = if eb == 1 then -(tok - 10) else tok - 10
elseif tok < 23 then
local half = 2 ^ (ex - 1)
local base = if tok == 17 then 7 elseif tok == 18 then 9 elseif tok == 19 then 13 elseif tok == 20 then 21 elseif tok == 21 then 37 else 69
v = base + eb % half
if eb >= half then
v = -v
-- zero runs and coefficients: run * 4096 + value + 2048 (value 0: a zero run)
local e = TOKEN_VALUE[tok * 1024 + eb + 1]
local p = zzi + rshift(e, 12)
local v = band(e, 4095) - 2048
if v ~= 0 then
if p == 0 then
dcv[fi] = v
elseif p < 64 then
writei16(coef, (fi - 1) * 128 + p * 2, v)
last[fi] = p
end
elseif tok < 28 then
r = tok - 22
v = if eb == 1 then -1 else 1
elseif tok == 28 then
r = 6 + eb % 4
v = if eb >= 4 then -1 else 1
elseif tok == 29 then
r = 10 + eb % 8
v = if eb >= 8 then -1 else 1
elseif tok == 30 then
r = 1
v = 2 + eb % 2
if eb >= 2 then
v = -v
end
else
r = 2 + eb % 2
v = 2 + (eb // 2) % 2
if eb >= 4 then
v = -v
end
end
local p = zzi + r
if p == 0 then
dcv[fi] = v
elseif p < 64 then
writei16(coef, (fi - 1) * 128 + p * 2, v)
last[fi] = p
end
t = p + 1
end
@@ -1104,83 +1212,7 @@ function D.decode(st: State, data: buffer, off: number, len: number): (string?,
end
end
-- DC prediction (raster order per plane)
for pli = 0, 2 do
local nh, nv = st.nhf[pli + 1], st.nvf[pli + 1]
local base = st.foff[pli + 1]
local pl0, pl1, pl2 = 0, 0, 0 -- the last DC of each reference frame (GOLD, PREV, SELF)
for fy = 0, nv - 1 do
local row = base + fy * nh
local lref, ulref, uref = -1, -1, -1
if fy > 0 then
uref = refi[row - nh + 1]
end
for fx = 0, nh - 1 do
local fi = row + fx + 1
local urref = -1
if fy > 0 and fx + 1 < nh then
urref = refi[fi - nh + 1]
end
if coded[fi] ~= 0 then
local r = refi[fi]
local pred: number
if fy == 0 then
pred = if r == 0 then pl0 elseif r == 1 then pl1 else pl2
else
local m = (if lref == r then 1 else 0) + (if ulref == r then 2 else 0) + (if uref == r then 4 else 0) + (if urref == r then 8 else 0)
if m == 0 then
pred = if r == 0 then pl0 elseif r == 1 then pl1 else pl2
elseif m == 1 or m == 3 then
pred = dcv[fi - 1]
elseif m == 2 then
pred = dcv[fi - nh - 1]
elseif m == 4 or m == 6 or m == 12 then
pred = dcv[fi - nh]
elseif m == 5 then
local s = dcv[fi - 1] + dcv[fi - nh]
pred = if s < 0 then -((-s) // 2) else s // 2
elseif m == 8 then
pred = dcv[fi - nh + 1]
elseif m == 9 or m == 11 or m == 13 then
local s = 75 * dcv[fi - 1] + 53 * dcv[fi - nh + 1]
pred = if s < 0 then -((-s) // 128) else s // 128
elseif m == 10 then
local s = dcv[fi - nh - 1] + dcv[fi - nh + 1]
pred = if s < 0 then -((-s) // 2) else s // 2
elseif m == 14 then
local s = 3 * (dcv[fi - nh - 1] + dcv[fi - nh + 1]) + 10 * dcv[fi - nh]
pred = if s < 0 then -((-s) // 16) else s // 16
else
local p0, p1, p2 = dcv[fi - 1], dcv[fi - nh - 1], dcv[fi - nh]
local s = 29 * (p0 + p2) - 26 * p1
pred = if s < 0 then -((-s) // 32) else s // 32
if math.abs(pred - p2) > 128 then
pred = p2
elseif math.abs(pred - p0) > 128 then
pred = p0
elseif math.abs(pred - p1) > 128 then
pred = p1
end
end
end
local v = wrap16(dcv[fi] + pred)
dcv[fi] = v
if r == 0 then
pl0 = v
elseif r == 1 then
pl1 = v
else
pl2 = v
end
lref = r
else
lref = -1
end
ulref = uref
uref = urref
end
end
end
dcPredict(st)
-- reconstruction into a free reference buffer
local self = 1
+1 -1
View File
@@ -226,7 +226,7 @@ local function buildTable(out: { number }, kid0: { number }, kid1: { number }, t
for i = 0, size - 1 do
local cur, d = n, 0
while d < nb and tok[cur] < 0 do
local bit = (i // 2 ^ (nb - 1 - d)) % 2
local bit = bit32.extract(i, nb - 1 - d, 1)
local nx = if bit == 0 then kid0[cur] else kid1[cur]
d += 1
cur = nx