From 93a94036d17ed2d788f2c37738f7ec84f98a4096 Mon Sep 17 00:00:00 2001 From: 4DBug Date: Sat, 31 Oct 2026 10:01:41 -0500 Subject: [PATCH] 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 --- src/Formats/codecs/Vp8/Decode.luau | 245 ++++++++++++++++++---------- src/Formats/codecs/Vp8/Predict.luau | 82 +++++----- 2 files changed, 204 insertions(+), 123 deletions(-) diff --git a/src/Formats/codecs/Vp8/Decode.luau b/src/Formats/codecs/Vp8/Decode.luau index a095913..51adc1e 100644 --- a/src/Formats/codecs/Vp8/Decode.luau +++ b/src/Formats/codecs/Vp8/Decode.luau @@ -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 diff --git a/src/Formats/codecs/Vp8/Predict.luau b/src/Formats/codecs/Vp8/Predict.luau index 3a21374..7f7f64d 100644 --- a/src/Formats/codecs/Vp8/Predict.luau +++ b/src/Formats/codecs/Vp8/Predict.luau @@ -25,19 +25,16 @@ local BPS = 32 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 --- 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 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] for _ = 1, h do - local p = so - 2 - 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) - p += 5 - for i = 0, w - 1 do - 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 + for i = so - 2, so + w - 3 do + local v = readu8(src, i) * t0 + readu8(src, i + 1) * t1 + readu8(src, i + 2) * t2 + readu8(src, i + 3) * t3 + + readu8(src, i + 4) * t4 + readu8(src, i + 5) * t5 + 64 + writeu8(dst, d + i - so + 2, if v < 0 then 0 elseif v >= 32768 then 255 else rshift(v, 7)) end so += ss 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 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] - for i = 0, w - 1 do - local p = so + i - 2 * ss - local a, b, c = readu8(src, p), readu8(src, p + ss), readu8(src, p + 2 * ss) - local e, g = readu8(src, p + 3 * ss), readu8(src, p + 4 * ss) - p += 5 * ss - local o = d + i - for _ = 1, h do - local q = readu8(src, p) - 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 + local s1, s2, s3 = ss, 2 * ss, 3 * ss + for _ = 1, h do + local p = so - s2 + for i = 0, w - 1 do + local q = p + i + local v = readu8(src, q) * t0 + readu8(src, q + s1) * t1 + readu8(src, q + s2) * t2 + readu8(src, q + s3) * t3 + + readu8(src, q + s2 + s2) * t4 + readu8(src, q + s2 + s3) * t5 + 64 + writeu8(dst, d + i, if v < 0 then 0 elseif v >= 32768 then 255 else rshift(v, 7)) end + so += ss + d += ds 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 t0, t1 = BIL[f * 2 + 1], BIL[f * 2 + 2] for _ = 1, h do - local a = readu8(src, so) for i = 0, w - 1 do - local b = readu8(src, so + i + 1) - writeu8(dst, d + i, rshift(a * t0 + b * t1 + 64, 7)) - a = b + writeu8(dst, d + i, rshift(readu8(src, so + i) * t0 + readu8(src, so + i + 1) * t1 + 64, 7)) end so += ss 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 t0, t1 = BIL[f * 2 + 1], BIL[f * 2 + 2] - for i = 0, w - 1 do - local p = so + i - local a = readu8(src, p) - 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 + for _ = 1, h do + for i = 0, w - 1 do + writeu8(dst, d + i, rshift(readu8(src, so + i) * t0 + readu8(src, so + ss + i) * t1 + 64, 7)) end + so += ss + d += ds end end @@ -154,12 +140,18 @@ end local function chroma16(v: number): number -- 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 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 - 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 export type Planes = { @@ -239,6 +231,20 @@ function Predict.mb(ref: Planes, mbx: number, mby: number, mvr: number, mvc: num 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 local function extend(P: buffer, stride: number, base: number, w: number, h: number) for r = 0, h - 1 do