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
This commit is contained in:
1 parent
110061960a
commit
087e1c861e
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
|
return br
|
||||||
end
|
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
|
local function getBit(br: Br, prob: number): number
|
||||||
if br.bits < 0 then
|
if br.bits < 0 then
|
||||||
load(br)
|
load(br)
|
||||||
@@ -82,16 +84,11 @@ local function getBit(br: Br, prob: number): number
|
|||||||
local range = br.range
|
local range = br.range
|
||||||
local pos = br.bits
|
local pos = br.bits
|
||||||
local split = rshift(range * prob, 8)
|
local split = rshift(range * prob, 8)
|
||||||
local bit = 0
|
local bit = rshift(split - rshift(br.value, pos), 31)
|
||||||
if rshift(br.value, pos) > split then
|
local r = split + 1 + bit * (range - split - split - 1)
|
||||||
range -= split
|
br.value -= bit * lshift(split + 1, pos)
|
||||||
br.value -= (split + 1) * 2 ^ pos
|
local shift = SHIFT[r + 1]
|
||||||
bit = 1
|
br.range = lshift(r, shift) - 1
|
||||||
else
|
|
||||||
range = split + 1
|
|
||||||
end
|
|
||||||
local shift = SHIFT[range + 1]
|
|
||||||
br.range = lshift(range, shift) - 1
|
|
||||||
br.bits = pos - shift
|
br.bits = pos - shift
|
||||||
return bit
|
return bit
|
||||||
end
|
end
|
||||||
@@ -387,7 +384,11 @@ local function getCoeffs(br: Br, P: { number }, t: number, ctx: number, dqDc: nu
|
|||||||
v = -v
|
v = -v
|
||||||
end
|
end
|
||||||
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
|
n += 1
|
||||||
end
|
end
|
||||||
br.value, br.bits, br.range, br.pos = value, bits, range, pos
|
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
|
end
|
||||||
|
|
||||||
-- ------------------------------------------------------------------------------------------------ loop filter
|
-- ------------------------------------------------------------------------------------------------ loop filter
|
||||||
local function sclip1(v: number): number
|
-- the filters are libwebp's DoFilter2 / DoFilter4 / DoFilter6 inlined into their loops (VP8ksclip1 clamps to
|
||||||
return if v < -128 then -128 elseif v > 127 then 127 else v
|
-- [-128, 127], VP8ksclip2 to [-16, 15], VP8kclip1 to [0, 255]). Arithmetic shifts of small signed values are
|
||||||
end
|
-- logical shifts of the value plus a bias: (a + 4) >> 3 = rshift(a + 4 + 1024, 3) - 128 for a >= -1024.
|
||||||
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
|
|
||||||
|
|
||||||
|
-- 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 function simpleFilter(b: buffer, p: number, step: number, along: number, thresh: number)
|
||||||
local t2 = 2 * thresh + 1
|
local t2 = 2 * thresh + 1
|
||||||
for i = 0, 15 do
|
for _ = 1, 16 do
|
||||||
local q = p + i * along
|
local p1, p0, q0, q1 = readu8(b, p - 2 * step), readu8(b, p - step), readu8(b, p), readu8(b, p + step)
|
||||||
local p1, p0, q0, q1 = readu8(b, q - 2 * step), readu8(b, q - step), readu8(b, q), readu8(b, q + step)
|
local w = p1 - q1
|
||||||
if 4 * abs(p0 - q0) + abs(p1 - q1) <= t2 then
|
local a = 3 * (q0 - p0) + (if w < -128 then -128 elseif w > 127 then 127 else w)
|
||||||
doFilter2(b, q, step)
|
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
|
end
|
||||||
|
p += along
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- filterLoop(b, p, hstride, vstride, size, thresh, ithresh, hevThresh, six): FilterLoop26 (six) / FilterLoop24
|
-- filterEdge(b, p, hs, vs, size, thresh, it, hevT): the macroblock edge filter (FilterLoop26)
|
||||||
local function filterLoop(b: buffer, p: number, hs: number, vs: number, size: number, thresh: number, it: number, hevT: number, six: boolean)
|
local function filterEdge(b: buffer, p: number, hs: number, vs: number, size: number, thresh: number, it: number, hevT: number)
|
||||||
local t2 = 2 * thresh + 1
|
local t2 = 2 * thresh + 1
|
||||||
for _ = 1, size do
|
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 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 w = p1 - q1
|
||||||
local p3, p2 = readu8(b, p - 4 * hs), readu8(b, p - 3 * hs)
|
local a = 3 * (q0 - p0) + (if w < -128 then -128 elseif w > 127 then 127 else w)
|
||||||
local q2, q3 = readu8(b, p + 2 * hs), readu8(b, p + 3 * hs)
|
if a ~= 0 then
|
||||||
if abs(p3 - p2) <= it and abs(p2 - p1) <= it and abs(q3 - q2) <= it and abs(q2 - q1) <= it then
|
local dp, dq = abs(p1 - p0), abs(q1 - q0)
|
||||||
if abs(p1 - p0) > hevT or abs(q1 - q0) > hevT then
|
if 4 * abs(p0 - q0) + abs(w) <= t2 and dp <= it and dq <= it then
|
||||||
doFilter2(b, p, hs)
|
local p3, p2 = readu8(b, p - 4 * hs), readu8(b, p - 3 * hs)
|
||||||
elseif six then
|
local q2, q3 = readu8(b, p + 2 * hs), readu8(b, p + 3 * hs)
|
||||||
doFilter6(b, p, hs)
|
if abs(p3 - p2) <= it and abs(p2 - p1) <= it and abs(q3 - q2) <= it and abs(q2 - q1) <= it then
|
||||||
else
|
if dp > hevT or dq > hevT then
|
||||||
doFilter4(b, p, hs)
|
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
|
end
|
||||||
end
|
end
|
||||||
@@ -1443,6 +1509,7 @@ function Decode.decodeFrame(st: State, buf: buffer, off: number, len: number, ma
|
|||||||
end
|
end
|
||||||
local topY, topU, topV = bcreate(mbw * 16), bcreate(mbw * 8), bcreate(mbw * 8)
|
local topY, topU, topV = bcreate(mbw * 16), bcreate(mbw * 8), bcreate(mbw * 8)
|
||||||
local yuv = bcreate(YUV_SIZE)
|
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 coeffs = bcreate(mbw * 768)
|
||||||
local dcb = bcreate(32)
|
local dcb = bcreate(32)
|
||||||
-- per macroblock column / row state
|
-- per macroblock column / row state
|
||||||
@@ -1497,7 +1564,10 @@ function Decode.decodeFrame(st: State, buf: buffer, off: number, len: number, ma
|
|||||||
seg = segMap[si]
|
seg = segMap[si]
|
||||||
end
|
end
|
||||||
mbSeg[x1] = seg
|
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
|
if not key then
|
||||||
readInterMb(st, br, probIntra, probLast, probGf, (mby + 1) * (mbw + 1) + x1 + 1, mbx, mby, rows)
|
readInterMb(st, br, probIntra, probLast, probGf, (mby + 1) * (mbw + 1) + x1 + 1, mbx, mby, rows)
|
||||||
continue
|
continue
|
||||||
@@ -1676,6 +1746,9 @@ function Decode.decodeFrame(st: State, buf: buffer, off: number, len: number, ma
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- reconstruct the row (libwebp ReconstructRow; inter macroblocks predict from the references)
|
-- 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
|
for j = 0, 15 do
|
||||||
writeu8(yuv, Y_OFF + j * BPS - 1, 129)
|
writeu8(yuv, Y_OFF + j * BPS - 1, 129)
|
||||||
end
|
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)
|
bcopy(yuv, V_OFF - BPS, topV, mbx * 8, 8)
|
||||||
end
|
end
|
||||||
if rf > 0 then
|
if rf > 0 then
|
||||||
local mi = (mby + 1) * (mbw + 1) + x1 + 1
|
-- the prediction (Predict.row): luma rows 0..15, then the chroma rows (U and V side by side)
|
||||||
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)
|
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
|
if bits ~= 0 then
|
||||||
for n = 0, 15 do
|
for n = 0, 15 do
|
||||||
local dst = Y_OFF + (n % 4) * 4 + (n // 4) * 4 * BPS
|
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 il, hev = fIlevel[fi], fHev[fi]
|
||||||
local up = uvb + mby * 8 * uvs + mbx * 8
|
local up = uvb + mby * 8 * uvs + mbx * 8
|
||||||
if mbx > 0 then
|
if mbx > 0 then
|
||||||
filterLoop(Y, yp, 1, ys, 16, limit + 4, il, hev, true)
|
filterEdge(Y, yp, 1, ys, 16, limit + 4, il, hev)
|
||||||
filterLoop(U, up, 1, uvs, 8, limit + 4, il, hev, true)
|
filterEdge(U, up, 1, uvs, 8, limit + 4, il, hev)
|
||||||
filterLoop(V, up, 1, uvs, 8, limit + 4, il, hev, true)
|
filterEdge(V, up, 1, uvs, 8, limit + 4, il, hev)
|
||||||
end
|
end
|
||||||
if inner then
|
if inner then
|
||||||
for k = 1, 3 do
|
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
|
end
|
||||||
filterLoop(U, up + 4, 1, uvs, 8, limit, il, hev, false)
|
filterInner(U, up + 4, 1, uvs, 8, limit, il, hev)
|
||||||
filterLoop(V, up + 4, 1, uvs, 8, limit, il, hev, false)
|
filterInner(V, up + 4, 1, uvs, 8, limit, il, hev)
|
||||||
end
|
end
|
||||||
if mby > 0 then
|
if mby > 0 then
|
||||||
filterLoop(Y, yp, ys, 1, 16, limit + 4, il, hev, true)
|
filterEdge(Y, yp, ys, 1, 16, limit + 4, il, hev)
|
||||||
filterLoop(U, up, uvs, 1, 8, limit + 4, il, hev, true)
|
filterEdge(U, up, uvs, 1, 8, limit + 4, il, hev)
|
||||||
filterLoop(V, up, uvs, 1, 8, limit + 4, il, hev, true)
|
filterEdge(V, up, uvs, 1, 8, limit + 4, il, hev)
|
||||||
end
|
end
|
||||||
if inner then
|
if inner then
|
||||||
for k = 1, 3 do
|
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
|
end
|
||||||
filterLoop(U, up + 4 * uvs, uvs, 1, 8, limit, il, hev, false)
|
filterInner(U, up + 4 * uvs, uvs, 1, 8, limit, il, hev)
|
||||||
filterLoop(V, up + 4 * uvs, uvs, 1, 8, limit, il, hev, false)
|
filterInner(V, up + 4 * uvs, uvs, 1, 8, limit, il, hev)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -25,19 +25,16 @@ local BPS = 32
|
|||||||
local emu = bcreate(32 * 32) -- a block with its taps, edges replicated
|
local emu = bcreate(32 * 32) -- a block with its taps, edges replicated
|
||||||
local tmp = bcreate(32 * 32) -- the first (horizontal) pass of a two-dimensional filter
|
local tmp = bcreate(32 * 32) -- the first (horizontal) pass of a two-dimensional filter
|
||||||
|
|
||||||
-- the six-tap filter along rows: dst[i] = clamp((sum of src[i - 2 .. i + 3] * taps + 64) >> 7)
|
-- the six-tap filter along rows: dst[i] = clamp((sum of src[i - 2 .. i + 3] * taps + 64) >> 7). Each output reads
|
||||||
|
-- its six pixels (a sliding window of locals is no faster in Luau and stops LuaJIT's register allocator).
|
||||||
local function sixH(src: buffer, so: number, ss: number, dst: buffer, d: number, ds: number, w: number, h: number, f: number)
|
local function sixH(src: buffer, so: number, ss: number, dst: buffer, d: number, ds: number, w: number, h: number, f: number)
|
||||||
local k = f * 6
|
local k = f * 6
|
||||||
local t0, t1, t2, t3, t4, t5 = SIX[k + 1], SIX[k + 2], SIX[k + 3], SIX[k + 4], SIX[k + 5], SIX[k + 6]
|
local t0, t1, t2, t3, t4, t5 = SIX[k + 1], SIX[k + 2], SIX[k + 3], SIX[k + 4], SIX[k + 5], SIX[k + 6]
|
||||||
for _ = 1, h do
|
for _ = 1, h do
|
||||||
local p = so - 2
|
for i = so - 2, so + w - 3 do
|
||||||
local a, b, c, e, g = readu8(src, p), readu8(src, p + 1), readu8(src, p + 2), readu8(src, p + 3), readu8(src, p + 4)
|
local v = readu8(src, i) * t0 + readu8(src, i + 1) * t1 + readu8(src, i + 2) * t2 + readu8(src, i + 3) * t3
|
||||||
p += 5
|
+ readu8(src, i + 4) * t4 + readu8(src, i + 5) * t5 + 64
|
||||||
for i = 0, w - 1 do
|
writeu8(dst, d + i - so + 2, if v < 0 then 0 elseif v >= 32768 then 255 else rshift(v, 7))
|
||||||
local q = readu8(src, p + i)
|
|
||||||
local v = a * t0 + b * t1 + c * t2 + e * t3 + g * t4 + q * t5 + 64
|
|
||||||
writeu8(dst, d + i, if v < 0 then 0 elseif v >= 32768 then 255 else rshift(v, 7))
|
|
||||||
a, b, c, e, g = b, c, e, g, q
|
|
||||||
end
|
end
|
||||||
so += ss
|
so += ss
|
||||||
d += ds
|
d += ds
|
||||||
@@ -48,20 +45,17 @@ end
|
|||||||
local function sixV(src: buffer, so: number, ss: number, dst: buffer, d: number, ds: number, w: number, h: number, f: number)
|
local function sixV(src: buffer, so: number, ss: number, dst: buffer, d: number, ds: number, w: number, h: number, f: number)
|
||||||
local k = f * 6
|
local k = f * 6
|
||||||
local t0, t1, t2, t3, t4, t5 = SIX[k + 1], SIX[k + 2], SIX[k + 3], SIX[k + 4], SIX[k + 5], SIX[k + 6]
|
local t0, t1, t2, t3, t4, t5 = SIX[k + 1], SIX[k + 2], SIX[k + 3], SIX[k + 4], SIX[k + 5], SIX[k + 6]
|
||||||
for i = 0, w - 1 do
|
local s1, s2, s3 = ss, 2 * ss, 3 * ss
|
||||||
local p = so + i - 2 * ss
|
for _ = 1, h do
|
||||||
local a, b, c = readu8(src, p), readu8(src, p + ss), readu8(src, p + 2 * ss)
|
local p = so - s2
|
||||||
local e, g = readu8(src, p + 3 * ss), readu8(src, p + 4 * ss)
|
for i = 0, w - 1 do
|
||||||
p += 5 * ss
|
local q = p + i
|
||||||
local o = d + i
|
local v = readu8(src, q) * t0 + readu8(src, q + s1) * t1 + readu8(src, q + s2) * t2 + readu8(src, q + s3) * t3
|
||||||
for _ = 1, h do
|
+ readu8(src, q + s2 + s2) * t4 + readu8(src, q + s2 + s3) * t5 + 64
|
||||||
local q = readu8(src, p)
|
writeu8(dst, d + i, if v < 0 then 0 elseif v >= 32768 then 255 else rshift(v, 7))
|
||||||
local v = a * t0 + b * t1 + c * t2 + e * t3 + g * t4 + q * t5 + 64
|
|
||||||
writeu8(dst, o, if v < 0 then 0 elseif v >= 32768 then 255 else rshift(v, 7))
|
|
||||||
a, b, c, e, g = b, c, e, g, q
|
|
||||||
p += ss
|
|
||||||
o += ds
|
|
||||||
end
|
end
|
||||||
|
so += ss
|
||||||
|
d += ds
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -69,11 +63,8 @@ end
|
|||||||
local function bilH(src: buffer, so: number, ss: number, dst: buffer, d: number, ds: number, w: number, h: number, f: number)
|
local function bilH(src: buffer, so: number, ss: number, dst: buffer, d: number, ds: number, w: number, h: number, f: number)
|
||||||
local t0, t1 = BIL[f * 2 + 1], BIL[f * 2 + 2]
|
local t0, t1 = BIL[f * 2 + 1], BIL[f * 2 + 2]
|
||||||
for _ = 1, h do
|
for _ = 1, h do
|
||||||
local a = readu8(src, so)
|
|
||||||
for i = 0, w - 1 do
|
for i = 0, w - 1 do
|
||||||
local b = readu8(src, so + i + 1)
|
writeu8(dst, d + i, rshift(readu8(src, so + i) * t0 + readu8(src, so + i + 1) * t1 + 64, 7))
|
||||||
writeu8(dst, d + i, rshift(a * t0 + b * t1 + 64, 7))
|
|
||||||
a = b
|
|
||||||
end
|
end
|
||||||
so += ss
|
so += ss
|
||||||
d += ds
|
d += ds
|
||||||
@@ -82,17 +73,12 @@ end
|
|||||||
|
|
||||||
local function bilV(src: buffer, so: number, ss: number, dst: buffer, d: number, ds: number, w: number, h: number, f: number)
|
local function bilV(src: buffer, so: number, ss: number, dst: buffer, d: number, ds: number, w: number, h: number, f: number)
|
||||||
local t0, t1 = BIL[f * 2 + 1], BIL[f * 2 + 2]
|
local t0, t1 = BIL[f * 2 + 1], BIL[f * 2 + 2]
|
||||||
for i = 0, w - 1 do
|
for _ = 1, h do
|
||||||
local p = so + i
|
for i = 0, w - 1 do
|
||||||
local a = readu8(src, p)
|
writeu8(dst, d + i, rshift(readu8(src, so + i) * t0 + readu8(src, so + ss + i) * t1 + 64, 7))
|
||||||
local o = d + i
|
|
||||||
for _ = 1, h do
|
|
||||||
p += ss
|
|
||||||
local b = readu8(src, p)
|
|
||||||
writeu8(dst, o, rshift(a * t0 + b * t1 + 64, 7))
|
|
||||||
a = b
|
|
||||||
o += ds
|
|
||||||
end
|
end
|
||||||
|
so += ss
|
||||||
|
d += ds
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -154,12 +140,18 @@ end
|
|||||||
|
|
||||||
local function chroma16(v: number): number
|
local function chroma16(v: number): number
|
||||||
-- the chroma vector of a whole macroblock: half the luma one, rounded away from zero
|
-- the chroma vector of a whole macroblock: half the luma one, rounded away from zero
|
||||||
return if v < 0 then -((1 - v) // 2) else (v + 1) // 2
|
if v < 0 then
|
||||||
|
return -((1 - v) // 2)
|
||||||
|
end
|
||||||
|
return (v + 1) // 2
|
||||||
end
|
end
|
||||||
|
|
||||||
local function chroma4(s: number): number
|
local function chroma4(s: number): number
|
||||||
-- the chroma vector of a 2x2 group of luma blocks: the sum of their 4 vectors / 8, rounded half away from zero
|
-- the chroma vector of a 2x2 group of luma blocks: the sum of their 4 vectors / 8, rounded half away from zero
|
||||||
return if s < 0 then -((4 - s) // 8) else (s + 4) // 8
|
if s < 0 then
|
||||||
|
return -((4 - s) // 8)
|
||||||
|
end
|
||||||
|
return (s + 4) // 8
|
||||||
end
|
end
|
||||||
|
|
||||||
export type Planes = {
|
export type Planes = {
|
||||||
@@ -239,6 +231,20 @@ function Predict.mb(ref: Planes, mbx: number, mby: number, mvr: number, mvc: num
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- row(refs, mby, mbw, ref, part, miMvR, miMvC, miSubR, miSubC, mi0, pred, chunk, yo, uo, vo, bil, full): predicts the
|
||||||
|
-- inter macroblocks of a row (ref[mbx + 1] > 0: refs[ref]) into pred, macroblock mbx at mbx * chunk + yo / uo / vo;
|
||||||
|
-- mi0 + mbx + 1: the mode info index of macroblock mbx. A loop of its own (LuaJIT traces it apart from the decoder).
|
||||||
|
function Predict.row(refs: { Planes }, mby: number, mbw: number, ref: { number }, part: { number }, miMvR: { number }, miMvC: { number }, miSubR: { number }, miSubC: { number }, mi0: number, pred: buffer, chunk: number, yo: number, uo: number, vo: number, bil: boolean, full: boolean)
|
||||||
|
for mbx = 0, mbw - 1 do
|
||||||
|
local rf = ref[mbx + 1]
|
||||||
|
if rf > 0 then
|
||||||
|
local mi = mi0 + mbx + 1
|
||||||
|
local o = mbx * chunk
|
||||||
|
Predict.mb(refs[rf], mbx, mby, miMvR[mi], miMvC[mi], part[mbx + 1], miSubR, miSubC, (mi - 1) * 16, pred, o + yo, o + uo, o + vo, bil, full)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
-- extend(P, stride, base, w, h): fills the BORDER pixels around a w x h plane with its edge pixels
|
-- extend(P, stride, base, w, h): fills the BORDER pixels around a w x h plane with its edge pixels
|
||||||
local function extend(P: buffer, stride: number, base: number, w: number, h: number)
|
local function extend(P: buffer, stride: number, base: number, w: number, h: number)
|
||||||
for r = 0, h - 1 do
|
for r = 0, h - 1 do
|
||||||
|
|||||||
Reference in new issue
Block a user