258 lines
8.6 KiB
Luau
258 lines
8.6 KiB
Luau
--!strict
|
|
--!native
|
|
--!optimize 2
|
|
-- codecs/image/Vp8/Predict: VP8 inter prediction (RFC 6386 section 18), after libvpx 1.17 vp8/common/reconinter.c and
|
|
-- filter.c (BSD-3-Clause, licenses/libvpx-LICENSE.txt). Out-of-frame motion reads the edge pixels extended forever,
|
|
-- which equals libvpx's 32 pixel border plus its motion vector clamping.
|
|
local Tables = require("@rosetta/codecs/image/Vp8/Tables")
|
|
|
|
local readu8, writeu8, bcopy, bcreate, bfill = buffer.readu8, buffer.writeu8, buffer.copy, buffer.create, buffer.fill
|
|
local readu32, writeu32 = buffer.readu32, buffer.writeu32
|
|
local rshift, band = bit32.rshift, bit32.band
|
|
local SIX, BIL = Tables.SIXTAP, Tables.BILINEAR
|
|
|
|
local Predict = {}
|
|
|
|
local BORDER = 32
|
|
Predict.BORDER = BORDER
|
|
|
|
local BPS = 32
|
|
|
|
local emu = bcreate(32 * 32)
|
|
local tmp = bcreate(32 * 32)
|
|
|
|
-- six-tap 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
|
|
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
|
|
end
|
|
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]
|
|
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
|
|
|
|
-- 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
|
|
for i = 0, w - 1 do
|
|
writeu8(dst, d + i, rshift(readu8(src, so + i) * t0 + readu8(src, so + i + 1) * t1 + 64, 7))
|
|
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 _ = 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
|
|
|
|
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 _ = 1, h do
|
|
for i = 0, w - 4, 4 do
|
|
writeu32(dst, d + i, readu32(sb, so + i))
|
|
end
|
|
so += ss
|
|
d += BPS
|
|
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
|
|
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
|
|
if s < 0 then
|
|
return -((4 - s) // 8)
|
|
end
|
|
return (s + 4) // 8
|
|
end
|
|
|
|
export type Planes = {
|
|
Y: buffer,
|
|
U: buffer,
|
|
V: buffer,
|
|
ys: number,
|
|
uvs: number,
|
|
yb: number,
|
|
uvb: number,
|
|
mbw: number,
|
|
mbh: number,
|
|
}
|
|
|
|
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
|
|
|
|
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
|
|
|
|
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
|