ws-video-vp8: faster loop filters (DoFilter2/4/6 inlined, flat positions skipped, shifts instead of divisions), branch-free getBit, row-wise inter prediction (LuaJIT traces), no table allocation in getCoeffs on LuaJIT
Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
aff15e7cc9
commit
93a94036d1
2 files changed
+204
-123
No files matched your search
@@ -75,6 +75,8 @@ local function newBr(buf: buffer, off: number, len: number): Br
|
||||
return br
|
||||
end
|
||||
|
||||
-- getBit without a branch on the decoded bit (one inlined copy per call site keeps LuaJIT's trace count down):
|
||||
-- bit = 1 when value >> bits > split, then range = bit ? range - split : split + 1, value -= bit * (split + 1) << bits
|
||||
local function getBit(br: Br, prob: number): number
|
||||
if br.bits < 0 then
|
||||
load(br)
|
||||
@@ -82,16 +84,11 @@ local function getBit(br: Br, prob: number): number
|
||||
local range = br.range
|
||||
local pos = br.bits
|
||||
local split = rshift(range * prob, 8)
|
||||
local bit = 0
|
||||
if rshift(br.value, pos) > split then
|
||||
range -= split
|
||||
br.value -= (split + 1) * 2 ^ pos
|
||||
bit = 1
|
||||
else
|
||||
range = split + 1
|
||||
end
|
||||
local shift = SHIFT[range + 1]
|
||||
br.range = lshift(range, shift) - 1
|
||||
local bit = rshift(split - rshift(br.value, pos), 31)
|
||||
local r = split + 1 + bit * (range - split - split - 1)
|
||||
br.value -= bit * lshift(split + 1, pos)
|
||||
local shift = SHIFT[r + 1]
|
||||
br.range = lshift(r, shift) - 1
|
||||
br.bits = pos - shift
|
||||
return bit
|
||||
end
|
||||
@@ -387,7 +384,11 @@ local function getCoeffs(br: Br, P: { number }, t: number, ctx: number, dqDc: nu
|
||||
v = -v
|
||||
end
|
||||
end
|
||||
writei16(out, o + ZIGZAG[n + 1] * 2, v * (if n > 0 then dqAc else dqDc))
|
||||
if n > 0 then
|
||||
writei16(out, o + ZIGZAG[n + 1] * 2, v * dqAc)
|
||||
else
|
||||
writei16(out, o + ZIGZAG[n + 1] * 2, v * dqDc)
|
||||
end
|
||||
n += 1
|
||||
end
|
||||
br.value, br.bits, br.range, br.pos = value, bits, range, pos
|
||||
@@ -757,75 +758,140 @@ local function checkMode(mbx: number, mby: number, mode: number): number
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ loop filter
|
||||
local function sclip1(v: number): number
|
||||
return if v < -128 then -128 elseif v > 127 then 127 else v
|
||||
end
|
||||
local function sclip2(v: number): number
|
||||
return if v < -16 then -16 elseif v > 15 then 15 else v
|
||||
end
|
||||
|
||||
local function doFilter2(b: buffer, p: number, step: number)
|
||||
local p1, p0, q0, q1 = readu8(b, p - 2 * step), readu8(b, p - step), readu8(b, p), readu8(b, p + step)
|
||||
local a = 3 * (q0 - p0) + sclip1(p1 - q1)
|
||||
local a1 = sclip2((a + 4) // 8)
|
||||
local a2 = sclip2((a + 3) // 8)
|
||||
writeu8(b, p - step, clip8(p0 + a2))
|
||||
writeu8(b, p, clip8(q0 - a1))
|
||||
end
|
||||
|
||||
local function doFilter4(b: buffer, p: number, step: number)
|
||||
local p1, p0, q0, q1 = readu8(b, p - 2 * step), readu8(b, p - step), readu8(b, p), readu8(b, p + step)
|
||||
local a = 3 * (q0 - p0)
|
||||
local a1 = sclip2((a + 4) // 8)
|
||||
local a2 = sclip2((a + 3) // 8)
|
||||
local a3 = (a1 + 1) // 2
|
||||
writeu8(b, p - 2 * step, clip8(p1 + a3))
|
||||
writeu8(b, p - step, clip8(p0 + a2))
|
||||
writeu8(b, p, clip8(q0 - a1))
|
||||
writeu8(b, p + step, clip8(q1 - a3))
|
||||
end
|
||||
|
||||
local function doFilter6(b: buffer, p: number, step: number)
|
||||
local p2, p1, p0 = readu8(b, p - 3 * step), readu8(b, p - 2 * step), readu8(b, p - step)
|
||||
local q0, q1, q2 = readu8(b, p), readu8(b, p + step), readu8(b, p + 2 * step)
|
||||
local a = sclip1(3 * (q0 - p0) + sclip1(p1 - q1))
|
||||
local a1 = (27 * a + 63) // 128
|
||||
local a2 = (18 * a + 63) // 128
|
||||
local a3 = (9 * a + 63) // 128
|
||||
writeu8(b, p - 3 * step, clip8(p2 + a3))
|
||||
writeu8(b, p - 2 * step, clip8(p1 + a2))
|
||||
writeu8(b, p - step, clip8(p0 + a1))
|
||||
writeu8(b, p, clip8(q0 - a1))
|
||||
writeu8(b, p + step, clip8(q1 - a2))
|
||||
writeu8(b, p + 2 * step, clip8(q2 - a3))
|
||||
end
|
||||
-- the filters are libwebp's DoFilter2 / DoFilter4 / DoFilter6 inlined into their loops (VP8ksclip1 clamps to
|
||||
-- [-128, 127], VP8ksclip2 to [-16, 15], VP8kclip1 to [0, 255]). Arithmetic shifts of small signed values are
|
||||
-- logical shifts of the value plus a bias: (a + 4) >> 3 = rshift(a + 4 + 1024, 3) - 128 for a >= -1024.
|
||||
|
||||
-- simpleFilter(b, p, step, along, thresh): SimpleVFilter16 / SimpleHFilter16 (16 positions). A position whose filter
|
||||
-- value a is 0 changes nothing and is skipped before the mask (flat areas).
|
||||
local function simpleFilter(b: buffer, p: number, step: number, along: number, thresh: number)
|
||||
local t2 = 2 * thresh + 1
|
||||
for i = 0, 15 do
|
||||
local q = p + i * along
|
||||
local p1, p0, q0, q1 = readu8(b, q - 2 * step), readu8(b, q - step), readu8(b, q), readu8(b, q + step)
|
||||
if 4 * abs(p0 - q0) + abs(p1 - q1) <= t2 then
|
||||
doFilter2(b, q, step)
|
||||
for _ = 1, 16 do
|
||||
local p1, p0, q0, q1 = readu8(b, p - 2 * step), readu8(b, p - step), readu8(b, p), readu8(b, p + step)
|
||||
local w = p1 - q1
|
||||
local a = 3 * (q0 - p0) + (if w < -128 then -128 elseif w > 127 then 127 else w)
|
||||
if a ~= 0 and 4 * abs(p0 - q0) + abs(w) <= t2 then
|
||||
local a1 = rshift(a + 1028, 3) - 128
|
||||
if a1 < -16 then
|
||||
a1 = -16
|
||||
elseif a1 > 15 then
|
||||
a1 = 15
|
||||
end
|
||||
local a2 = rshift(a + 1027, 3) - 128
|
||||
if a2 < -16 then
|
||||
a2 = -16
|
||||
elseif a2 > 15 then
|
||||
a2 = 15
|
||||
end
|
||||
local v = p0 + a2
|
||||
writeu8(b, p - step, if v < 0 then 0 elseif v > 255 then 255 else v)
|
||||
v = q0 - a1
|
||||
writeu8(b, p, if v < 0 then 0 elseif v > 255 then 255 else v)
|
||||
end
|
||||
p += along
|
||||
end
|
||||
end
|
||||
|
||||
-- filterLoop(b, p, hstride, vstride, size, thresh, ithresh, hevThresh, six): FilterLoop26 (six) / FilterLoop24
|
||||
local function filterLoop(b: buffer, p: number, hs: number, vs: number, size: number, thresh: number, it: number, hevT: number, six: boolean)
|
||||
-- filterEdge(b, p, hs, vs, size, thresh, it, hevT): the macroblock edge filter (FilterLoop26)
|
||||
local function filterEdge(b: buffer, p: number, hs: number, vs: number, size: number, thresh: number, it: number, hevT: number)
|
||||
local t2 = 2 * thresh + 1
|
||||
for _ = 1, size do
|
||||
local p1, p0, q0, q1 = readu8(b, p - 2 * hs), readu8(b, p - hs), readu8(b, p), readu8(b, p + hs)
|
||||
if 4 * abs(p0 - q0) + abs(p1 - q1) <= t2 and abs(p1 - p0) <= it and abs(q1 - q0) <= it then
|
||||
local p3, p2 = readu8(b, p - 4 * hs), readu8(b, p - 3 * hs)
|
||||
local q2, q3 = readu8(b, p + 2 * hs), readu8(b, p + 3 * hs)
|
||||
if abs(p3 - p2) <= it and abs(p2 - p1) <= it and abs(q3 - q2) <= it and abs(q2 - q1) <= it then
|
||||
if abs(p1 - p0) > hevT or abs(q1 - q0) > hevT then
|
||||
doFilter2(b, p, hs)
|
||||
elseif six then
|
||||
doFilter6(b, p, hs)
|
||||
else
|
||||
doFilter4(b, p, hs)
|
||||
local w = p1 - q1
|
||||
local a = 3 * (q0 - p0) + (if w < -128 then -128 elseif w > 127 then 127 else w)
|
||||
if a ~= 0 then
|
||||
local dp, dq = abs(p1 - p0), abs(q1 - q0)
|
||||
if 4 * abs(p0 - q0) + abs(w) <= t2 and dp <= it and dq <= it then
|
||||
local p3, p2 = readu8(b, p - 4 * hs), readu8(b, p - 3 * hs)
|
||||
local q2, q3 = readu8(b, p + 2 * hs), readu8(b, p + 3 * hs)
|
||||
if abs(p3 - p2) <= it and abs(p2 - p1) <= it and abs(q3 - q2) <= it and abs(q2 - q1) <= it then
|
||||
if dp > hevT or dq > hevT then
|
||||
local a1 = rshift(a + 1028, 3) - 128
|
||||
if a1 < -16 then
|
||||
a1 = -16
|
||||
elseif a1 > 15 then
|
||||
a1 = 15
|
||||
end
|
||||
local a2 = rshift(a + 1027, 3) - 128
|
||||
if a2 < -16 then
|
||||
a2 = -16
|
||||
elseif a2 > 15 then
|
||||
a2 = 15
|
||||
end
|
||||
local v = p0 + a2
|
||||
writeu8(b, p - hs, if v < 0 then 0 elseif v > 255 then 255 else v)
|
||||
v = q0 - a1
|
||||
writeu8(b, p, if v < 0 then 0 elseif v > 255 then 255 else v)
|
||||
else
|
||||
if a < -128 then
|
||||
a = -128
|
||||
elseif a > 127 then
|
||||
a = 127
|
||||
end
|
||||
local a1 = rshift(27 * a + 4159, 7) - 32
|
||||
local a2 = rshift(18 * a + 4159, 7) - 32
|
||||
local a3 = rshift(9 * a + 4159, 7) - 32
|
||||
local v = p2 + a3
|
||||
writeu8(b, p - 3 * hs, if v < 0 then 0 elseif v > 255 then 255 else v)
|
||||
v = p1 + a2
|
||||
writeu8(b, p - 2 * hs, if v < 0 then 0 elseif v > 255 then 255 else v)
|
||||
v = p0 + a1
|
||||
writeu8(b, p - hs, if v < 0 then 0 elseif v > 255 then 255 else v)
|
||||
v = q0 - a1
|
||||
writeu8(b, p, if v < 0 then 0 elseif v > 255 then 255 else v)
|
||||
v = q1 - a2
|
||||
writeu8(b, p + hs, if v < 0 then 0 elseif v > 255 then 255 else v)
|
||||
v = q2 - a3
|
||||
writeu8(b, p + 2 * hs, if v < 0 then 0 elseif v > 255 then 255 else v)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
p += vs
|
||||
end
|
||||
end
|
||||
|
||||
-- filterInner(b, p, hs, vs, size, thresh, it, hevT): the inner edge filter (FilterLoop24); nothing changes when
|
||||
-- q0 == p0 and p1 == q1
|
||||
local function filterInner(b: buffer, p: number, hs: number, vs: number, size: number, thresh: number, it: number, hevT: number)
|
||||
local t2 = 2 * thresh + 1
|
||||
for _ = 1, size do
|
||||
local p1, p0, q0, q1 = readu8(b, p - 2 * hs), readu8(b, p - hs), readu8(b, p), readu8(b, p + hs)
|
||||
local w = p1 - q1
|
||||
if q0 ~= p0 or w ~= 0 then
|
||||
local dp, dq = abs(p1 - p0), abs(q1 - q0)
|
||||
if 4 * abs(p0 - q0) + abs(w) <= t2 and dp <= it and dq <= it then
|
||||
local p3, p2 = readu8(b, p - 4 * hs), readu8(b, p - 3 * hs)
|
||||
local q2, q3 = readu8(b, p + 2 * hs), readu8(b, p + 3 * hs)
|
||||
if abs(p3 - p2) <= it and abs(p2 - p1) <= it and abs(q3 - q2) <= it and abs(q2 - q1) <= it then
|
||||
local hev = dp > hevT or dq > hevT
|
||||
local a = 3 * (q0 - p0)
|
||||
if hev then
|
||||
a += if w < -128 then -128 elseif w > 127 then 127 else w
|
||||
end
|
||||
local a1 = rshift(a + 1028, 3) - 128
|
||||
if a1 < -16 then
|
||||
a1 = -16
|
||||
elseif a1 > 15 then
|
||||
a1 = 15
|
||||
end
|
||||
local a2 = rshift(a + 1027, 3) - 128
|
||||
if a2 < -16 then
|
||||
a2 = -16
|
||||
elseif a2 > 15 then
|
||||
a2 = 15
|
||||
end
|
||||
local v = p0 + a2
|
||||
writeu8(b, p - hs, if v < 0 then 0 elseif v > 255 then 255 else v)
|
||||
v = q0 - a1
|
||||
writeu8(b, p, if v < 0 then 0 elseif v > 255 then 255 else v)
|
||||
if not hev then
|
||||
local a3 = rshift(a1 + 17, 1) - 8
|
||||
v = p1 + a3
|
||||
writeu8(b, p - 2 * hs, if v < 0 then 0 elseif v > 255 then 255 else v)
|
||||
v = q1 - a3
|
||||
writeu8(b, p + hs, if v < 0 then 0 elseif v > 255 then 255 else v)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1443,6 +1509,7 @@ function Decode.decodeFrame(st: State, buf: buffer, off: number, len: number, ma
|
||||
end
|
||||
local topY, topU, topV = bcreate(mbw * 16), bcreate(mbw * 8), bcreate(mbw * 8)
|
||||
local yuv = bcreate(YUV_SIZE)
|
||||
local pred = if key then yuv else bcreate(YUV_SIZE * mbw) -- the inter predictions of a row
|
||||
local coeffs = bcreate(mbw * 768)
|
||||
local dcb = bcreate(32)
|
||||
-- per macroblock column / row state
|
||||
@@ -1497,7 +1564,10 @@ function Decode.decodeFrame(st: State, buf: buffer, off: number, len: number, ma
|
||||
seg = segMap[si]
|
||||
end
|
||||
mbSeg[x1] = seg
|
||||
mbSkip[x1] = if useSkip then getBit(br, skipP) else 0
|
||||
mbSkip[x1] = 0
|
||||
if useSkip then
|
||||
mbSkip[x1] = getBit(br, skipP)
|
||||
end
|
||||
if not key then
|
||||
readInterMb(st, br, probIntra, probLast, probGf, (mby + 1) * (mbw + 1) + x1 + 1, mbx, mby, rows)
|
||||
continue
|
||||
@@ -1676,6 +1746,9 @@ function Decode.decodeFrame(st: State, buf: buffer, off: number, len: number, ma
|
||||
end
|
||||
|
||||
-- reconstruct the row (libwebp ReconstructRow; inter macroblocks predict from the references)
|
||||
if not key then
|
||||
Predict.row(refs, mby, mbw, mbRef, mbPart, miMvR, miMvC, miSubR, miSubC, (mby + 1) * (mbw + 1) + 1, pred, YUV_SIZE, Y_OFF, U_OFF, V_OFF, bil, full)
|
||||
end
|
||||
for j = 0, 15 do
|
||||
writeu8(yuv, Y_OFF + j * BPS - 1, 129)
|
||||
end
|
||||
@@ -1713,8 +1786,10 @@ function Decode.decodeFrame(st: State, buf: buffer, off: number, len: number, ma
|
||||
bcopy(yuv, V_OFF - BPS, topV, mbx * 8, 8)
|
||||
end
|
||||
if rf > 0 then
|
||||
local mi = (mby + 1) * (mbw + 1) + x1 + 1
|
||||
Predict.mb(refs[rf], mbx, mby, miMvR[mi], miMvC[mi], mbPart[x1], miSubR, miSubC, (mi - 1) * 16, yuv, Y_OFF, U_OFF, V_OFF, bil, full)
|
||||
-- the prediction (Predict.row): luma rows 0..15, then the chroma rows (U and V side by side)
|
||||
local o = mbx * YUV_SIZE
|
||||
bcopy(yuv, Y_OFF, pred, o + Y_OFF, 15 * BPS + 16)
|
||||
bcopy(yuv, U_OFF, pred, o + U_OFF, 7 * BPS + 24)
|
||||
if bits ~= 0 then
|
||||
for n = 0, 15 do
|
||||
local dst = Y_OFF + (n % 4) * 4 + (n // 4) * 4 * BPS
|
||||
@@ -1802,28 +1877,28 @@ function Decode.decodeFrame(st: State, buf: buffer, off: number, len: number, ma
|
||||
local il, hev = fIlevel[fi], fHev[fi]
|
||||
local up = uvb + mby * 8 * uvs + mbx * 8
|
||||
if mbx > 0 then
|
||||
filterLoop(Y, yp, 1, ys, 16, limit + 4, il, hev, true)
|
||||
filterLoop(U, up, 1, uvs, 8, limit + 4, il, hev, true)
|
||||
filterLoop(V, up, 1, uvs, 8, limit + 4, il, hev, true)
|
||||
filterEdge(Y, yp, 1, ys, 16, limit + 4, il, hev)
|
||||
filterEdge(U, up, 1, uvs, 8, limit + 4, il, hev)
|
||||
filterEdge(V, up, 1, uvs, 8, limit + 4, il, hev)
|
||||
end
|
||||
if inner then
|
||||
for k = 1, 3 do
|
||||
filterLoop(Y, yp + 4 * k, 1, ys, 16, limit, il, hev, false)
|
||||
filterInner(Y, yp + 4 * k, 1, ys, 16, limit, il, hev)
|
||||
end
|
||||
filterLoop(U, up + 4, 1, uvs, 8, limit, il, hev, false)
|
||||
filterLoop(V, up + 4, 1, uvs, 8, limit, il, hev, false)
|
||||
filterInner(U, up + 4, 1, uvs, 8, limit, il, hev)
|
||||
filterInner(V, up + 4, 1, uvs, 8, limit, il, hev)
|
||||
end
|
||||
if mby > 0 then
|
||||
filterLoop(Y, yp, ys, 1, 16, limit + 4, il, hev, true)
|
||||
filterLoop(U, up, uvs, 1, 8, limit + 4, il, hev, true)
|
||||
filterLoop(V, up, uvs, 1, 8, limit + 4, il, hev, true)
|
||||
filterEdge(Y, yp, ys, 1, 16, limit + 4, il, hev)
|
||||
filterEdge(U, up, uvs, 1, 8, limit + 4, il, hev)
|
||||
filterEdge(V, up, uvs, 1, 8, limit + 4, il, hev)
|
||||
end
|
||||
if inner then
|
||||
for k = 1, 3 do
|
||||
filterLoop(Y, yp + 4 * k * ys, ys, 1, 16, limit, il, hev, false)
|
||||
filterInner(Y, yp + 4 * k * ys, ys, 1, 16, limit, il, hev)
|
||||
end
|
||||
filterLoop(U, up + 4 * uvs, uvs, 1, 8, limit, il, hev, false)
|
||||
filterLoop(V, up + 4 * uvs, uvs, 1, 8, limit, il, hev, false)
|
||||
filterInner(U, up + 4 * uvs, uvs, 1, 8, limit, il, hev)
|
||||
filterInner(V, up + 4 * uvs, uvs, 1, 8, limit, il, hev)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in new issue
Block a user