Files
rosetta/src/Formats/codecs/Vp8/Predict.luau
T

264 lines
9.6 KiB
Luau

--!strict
--!native
--!optimize 2
-- codecs/Vp8/Predict: VP8 inter prediction (RFC 6386 section 18): the six-tap sub-pixel filters (version 0), the
-- bilinear ones (versions 1-3; version 3 also rounds chroma vectors to full pixels), chroma vectors derived from the
-- luma ones, split macroblocks, and the reference frame borders. After libvpx 1.17 vp8/common/reconinter.c and
-- filter.c (BSD-3-Clause, licenses/libvpx-LICENSE.txt). Out-of-frame motion reads the reference as if its edge pixels
-- extended forever, which is exactly what libvpx computes with its 32 pixel border plus its motion vector clamping (the
-- clamps only kick in once every tap is in the border). The planes keep a BORDER pixel border filled by extend(); a
-- block whose taps leave it is first copied with clamped coordinates. Never throws.
local Tables = require("@formats/codecs/Vp8/Tables")
local readu8, writeu8, bcopy, bcreate, bfill = buffer.readu8, buffer.writeu8, buffer.copy, buffer.create, buffer.fill
local rshift, band = bit32.rshift, bit32.band
local SIX, BIL = Tables.SIXTAP, Tables.BILINEAR
local Predict = {}
local BORDER = 32
Predict.BORDER = BORDER
-- the work buffer stride (codecs/Vp8/Decode: BPS)
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)
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
end
so += ss
d += ds
end
end
-- the six-tap filter along columns
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
end
end
end
-- the bilinear filters: dst[i] = (src[i] * t0 + src[i + 1] * t1 + 64) >> 7
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
end
so += ss
d += ds
end
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
end
end
end
-- block(src, stride, base, wmax, hmax, x, y, mvx, mvy, w, h, dst, d, bil): predicts the w x h block at plane pixel
-- (x, y) moved by (mvx, mvy) eighth pixels into dst at d (stride BPS). base: the offset of plane pixel (0, 0); wmax,
-- hmax: the last pixel column / row of the (macroblock aligned) plane.
local function block(src: buffer, stride: number, base: number, wmax: number, hmax: number, x: number, y: number, mvx: number, mvy: number, w: number, h: number, dst: buffer, d: number, bil: boolean)
local fx, fy = band(mvx, 7), band(mvy, 7)
local px, py = x + (mvx - fx) // 8, y + (mvy - fy) // 8
local sb, so, ss = src, 0, stride
if px >= 2 - BORDER and py >= 2 - BORDER and px + w + 3 <= wmax + 1 + BORDER and py + h + 3 <= hmax + 1 + BORDER then
so = base + py * stride + px
else
for r = 0, h + 4 do
local sy = py - 2 + r
if sy < 0 then
sy = 0
elseif sy > hmax then
sy = hmax
end
local row = base + sy * stride
local o = r * 32
for c = 0, w + 4 do
local sx = px - 2 + c
if sx < 0 then
sx = 0
elseif sx > wmax then
sx = wmax
end
writeu8(emu, o + c, readu8(src, row + sx))
end
end
sb, so, ss = emu, 66, 32
end
if fx == 0 then
if fy == 0 then
for r = 0, h - 1 do
bcopy(dst, d + r * BPS, sb, so + r * ss, w)
end
elseif bil then
bilV(sb, so, ss, dst, d, BPS, w, h, fy)
else
sixV(sb, so, ss, dst, d, BPS, w, h, fy)
end
elseif fy == 0 then
if bil then
bilH(sb, so, ss, dst, d, BPS, w, h, fx)
else
sixH(sb, so, ss, dst, d, BPS, w, h, fx)
end
elseif bil then
bilH(sb, so, ss, tmp, 0, 32, w, h + 1, fx)
bilV(tmp, 0, 32, dst, d, BPS, w, h, fy)
else
sixH(sb, so - 2 * ss, ss, tmp, 0, 32, w, h + 5, fx)
sixV(tmp, 64, 32, dst, d, BPS, w, h, fy)
end
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
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
end
export type Planes = {
Y: buffer,
U: buffer,
V: buffer,
ys: number, -- luma stride
uvs: number, -- chroma stride
yb: number, -- offset of luma pixel (0, 0)
uvb: number,
mbw: number,
mbh: number,
}
-- mb(ref, mbx, mby, mvr, mvc, split, subR, subC, sb, dst, yo, uo, vo, bil, full): the prediction of one inter
-- macroblock into the work buffer (luma at yo, chroma at uo / vo). Not split: one vector (mvr, mvc) in eighth pixels;
-- split: the 16 block vectors subR / subC[sb + 1 .. sb + 16] (part < 3: the four 8x8 quarters share them).
function Predict.mb(ref: Planes, mbx: number, mby: number, mvr: number, mvc: number, part: number, subR: { number }, subC: { number }, sb: number, dst: buffer, yo: number, uo: number, vo: number, bil: boolean, full: boolean)
local Y, U, V, ys, uvs, yb, uvb = ref.Y, ref.U, ref.V, ref.ys, ref.uvs, ref.yb, ref.uvb
local wl, hl = ref.mbw * 16 - 1, ref.mbh * 16 - 1
local wc, hc = ref.mbw * 8 - 1, ref.mbh * 8 - 1
local x, y = mbx * 16, mby * 16
local cx, cy = mbx * 8, mby * 8
if part < 0 then
block(Y, ys, yb, wl, hl, x, y, mvc, mvr, 16, 16, dst, yo, bil)
local cr, cc = chroma16(mvr), chroma16(mvc)
if full then
cr -= band(cr, 7)
cc -= band(cc, 7)
end
block(U, uvs, uvb, wc, hc, cx, cy, cc, cr, 8, 8, dst, uo, bil)
block(V, uvs, uvb, wc, hc, cx, cy, cc, cr, 8, 8, dst, vo, bil)
return
end
if part < 3 then
for q = 0, 3 do
local bx, by = (q % 2) * 8, (q // 2) * 8
local k = sb + by + bx // 4 + 1
block(Y, ys, yb, wl, hl, x + bx, y + by, subC[k], subR[k], 8, 8, dst, yo + by * BPS + bx, bil)
end
else
for b = 0, 15, 2 do
local k = sb + b + 1
local bx, by = (b % 4) * 4, (b // 4) * 4
local r0, c0 = subR[k], subC[k]
if r0 == subR[k + 1] and c0 == subC[k + 1] then
block(Y, ys, yb, wl, hl, x + bx, y + by, c0, r0, 8, 4, dst, yo + by * BPS + bx, bil)
else
block(Y, ys, yb, wl, hl, x + bx, y + by, c0, r0, 4, 4, dst, yo + by * BPS + bx, bil)
block(Y, ys, yb, wl, hl, x + bx + 4, y + by, subC[k + 1], subR[k + 1], 4, 4, dst, yo + by * BPS + bx + 4, bil)
end
end
end
for i = 0, 1 do
local rr, rc = 0, 0
for j = 0, 1 do
local k = sb + i * 8 + j * 2 + 1
local cr = chroma4(subR[k] + subR[k + 1] + subR[k + 4] + subR[k + 5])
local cc = chroma4(subC[k] + subC[k + 1] + subC[k + 4] + subC[k + 5])
if full then
cr -= band(cr, 7)
cc -= band(cc, 7)
end
if j == 0 then
rr, rc = cr, cc
elseif cr == rr and cc == rc then
-- both 4x4 chroma blocks of the row share one vector: one 8x4 block
block(U, uvs, uvb, wc, hc, cx, cy + i * 4, cc, cr, 8, 4, dst, uo + i * 4 * BPS, bil)
block(V, uvs, uvb, wc, hc, cx, cy + i * 4, cc, cr, 8, 4, dst, vo + i * 4 * BPS, bil)
else
block(U, uvs, uvb, wc, hc, cx, cy + i * 4, rc, rr, 4, 4, dst, uo + i * 4 * BPS, bil)
block(V, uvs, uvb, wc, hc, cx, cy + i * 4, rc, rr, 4, 4, dst, vo + i * 4 * BPS, bil)
block(U, uvs, uvb, wc, hc, cx + 4, cy + i * 4, cc, cr, 4, 4, dst, uo + i * 4 * BPS + 4, bil)
block(V, uvs, uvb, wc, hc, cx + 4, cy + i * 4, cc, cr, 4, 4, dst, vo + i * 4 * BPS + 4, bil)
end
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
local o = base + r * stride
bfill(P, o - BORDER, readu8(P, o), BORDER)
bfill(P, o + w, readu8(P, o + w - 1), BORDER)
end
local top = base - BORDER
local bottom = base + (h - 1) * stride - BORDER
for r = 1, BORDER do
bcopy(P, top - r * stride, P, top, stride)
bcopy(P, bottom + r * stride, P, bottom, stride)
end
end
function Predict.extend(ref: Planes)
extend(ref.Y, ref.ys, ref.yb, ref.mbw * 16, ref.mbh * 16)
extend(ref.U, ref.uvs, ref.uvb, ref.mbw * 8, ref.mbh * 8)
extend(ref.V, ref.uvs, ref.uvb, ref.mbw * 8, ref.mbh * 8)
end
return Predict