Files
rosetta/src/Formats/codecs/H264/Mb.luau
T

931 lines
26 KiB
Luau

--!strict
--!native
--!optimize 2
-- codecs/H264/Mb: macroblock reconstruction shared by the CAVLC and CABAC parsers (ITU-T H.264 8.3 - 8.5):
-- neighbour availability (6.4.11), Intra4x4 / Intra8x8 mode prediction (8.3.1.1 / 8.3.2.1), motion vector prediction
-- (8.4.1.3), P_Skip (8.4.1.1), B direct spatial and temporal (8.4.1.2), motion compensation with weighting, intra
-- prediction and the residual. Written from the spec; the per-4x4 motion / mode grids follow the usual decoder layout
-- (picture-wide arrays, one entry per 4x4 luma block, 1-based).
-- Never throws: every index is in range by construction; reference indices past the list use the first entry.
local Intra = require("@formats/codecs/H264/Intra")
local Transform = require("@formats/codecs/H264/Transform")
local Inter = require("@formats/codecs/H264/Inter")
local Mb = {}
-- macroblock kinds (S.kind per macroblock)
local K_I4, K_I8, K_I16, K_PCM, K_P, K_PSKIP, K_B, K_BSKIP, K_BDIRECT = 1, 2, 3, 4, 5, 6, 7, 8, 9
Mb.K_I4, Mb.K_I8, Mb.K_I16, Mb.K_PCM, Mb.K_P, Mb.K_PSKIP, Mb.K_B, Mb.K_BSKIP, Mb.K_BDIRECT = K_I4, K_I8, K_I16, K_PCM, K_P, K_PSKIP, K_B, K_BSKIP, K_BDIRECT
local UNAVAIL = -2
local min, max = math.min, math.max
local add4, addDc4, add8 = Transform.add4, Transform.addDc4, Transform.add8
local PAD, PADC = Inter.PAD, Inter.PADC
-- raster 4x4 block (bx + 4 by) of the decoding-order block index 0..15 (6.4.3)
local BLK_X = { 0, 1, 0, 1, 2, 3, 2, 3, 0, 1, 0, 1, 2, 3, 2, 3 }
local BLK_Y = { 0, 0, 1, 1, 0, 0, 1, 1, 2, 2, 3, 3, 2, 2, 3, 3 }
Mb.BLK_X, Mb.BLK_Y = BLK_X, BLK_Y
-- new() -> M, the per-macroblock scratch state
function Mb.new(): any
return {
addr = 0,
x = 0,
y = 0,
g0 = 1, -- grid index of the macroblock's 4x4 block (0, 0)
c0 = 1, -- chroma 4x4 grid index of its block (0, 0)
aL = false,
aT = false,
aTR = false,
aTL = false,
iL = false, -- intra sample availability (constrained_intra_pred aware)
iT = false,
iTR = false,
iTL = false,
kind = 0,
t8 = 0,
i16 = 0,
cpm = 0,
cbp = 0,
qp = 0,
qpd = 0, -- mb_qp_delta
part = 0, -- 0 16x16, 1 16x8, 2 8x16, 3 8x8
pf = { 0, 0, 0, 0 }, -- prediction flags per partition: 1 L0, 2 L1, 3 Bi
sub = { 0, 0, 0, 0 }, -- sub-macroblock shape: 0 8x8, 1 8x4, 2 4x8, 3 4x4
dir = { false, false, false, false }, -- B_Direct_8x8 sub-macroblocks
ref0 = { 0, 0, 0, 0 },
ref1 = { 0, 0, 0, 0 },
mvd0 = table.create(32, 0),
mvd1 = table.create(32, 0),
ipm = table.create(16, 2), -- intra 4x4 / 8x8 modes by raster block
c4 = table.create(256, 0), -- luma 4x4 coefficients (dequantised), raster block * 16 + raster position
c8 = table.create(256, 0), -- luma 8x8 coefficients, 8x8 block * 64 + raster position
cc = table.create(128, 0), -- chroma: (plane * 4 + block) * 16 + position
dcY = table.create(16, 0),
dcC = table.create(8, 0),
nz = table.create(16, 0), -- coefficients per luma 4x4 block (raster)
nzc = table.create(8, 0), -- coefficients per chroma AC block (plane * 4 + block)
hasDcY = false,
hasDcC = false,
done = table.create(16, false),
reg = table.create(64, 0), -- motion compensation regions: x, y, w, h (4x4 units)
nreg = 0,
P0 = table.create(256, 0),
P1 = table.create(256, 0),
pcm = buffer.create(384),
}
end
-- grids(mbW, mbH) -> the per-picture decoding grids (shared by the slices of one picture)
function Mb.grids(mbW: number, mbH: number): any
local n = mbW * mbH
local n4 = n * 16
local nc = n * 4
return {
sliceTab = table.create(n, 0),
kind = table.create(n, 0),
cbpTab = table.create(n, 0),
qpTab = table.create(n, 0), -- QPY for deblocking (0 for I_PCM)
qpcTab = table.create(n, 0), -- QPc (Cb)
qpc2Tab = table.create(n, 0), -- QPc (Cr)
t8Tab = table.create(n, 0),
cpmTab = table.create(n, 0),
dcTab = table.create(n, 0), -- CABAC coded_block_flag of the DC blocks: 1 luma, 2 Cb, 4 Cr
qpdTab = table.create(n, 0), -- mb_qp_delta ~= 0
nzc = table.create(n4, 0),
nzcCb = table.create(nc, 0),
nzcCr = table.create(nc, 0),
nzDb = table.create(n4, 0),
ipm = table.create(n4, 2),
mvd0 = table.create(n4 * 2, 0),
mvd1 = table.create(n4 * 2, 0),
rgt0 = table.create(n4, 0),
rgt1 = table.create(n4, 0),
}
end
-- ------------------------------------------------------------------------------------------------ neighbours
-- start(S, M, addr): position and neighbour availability of macroblock addr
function Mb.start(S: any, M: any, addr: number)
local mbW = S.mbW
local x, y = addr % mbW, addr // mbW
M.addr, M.x, M.y = addr, x, y
local w4 = S.w4
M.g0 = y * 4 * w4 + x * 4 + 1
M.c0 = y * 2 * S.cw4 + x * 2 + 1
local st, sl = S.sliceTab, S.slice
local aL = x > 0 and st[addr] == sl -- addr is 0-based: st[addr] is macroblock addr - 1
local aT = y > 0 and st[addr - mbW + 1] == sl
local aTR = y > 0 and x < mbW - 1 and st[addr - mbW + 2] == sl
local aTL = y > 0 and x > 0 and st[addr - mbW] == sl
M.aL, M.aT, M.aTR, M.aTL = aL, aT, aTR, aTL
if S.constrainedIntra then
local k = S.kind
M.iL = aL and k[addr] <= K_PCM
M.iT = aT and k[addr - mbW + 1] <= K_PCM
M.iTR = aTR and k[addr - mbW + 2] <= K_PCM
M.iTL = aTL and k[addr - mbW] <= K_PCM
else
M.iL, M.iT, M.iTR, M.iTL = aL, aT, aTR, aTL
end
local done = M.done
for i = 1, 16 do
done[i] = false
end
M.nreg = 0
M.hasDcY, M.hasDcC = false, false
M.t8 = 0
M.qpd = 0
end
-- nC(S, M, bx, by) -> nC of luma block (bx, by) (9.2.1)
function Mb.nC(S: any, M: any, bx: number, by: number): number
local g = M.g0 + by * S.w4 + bx
local nzc = S.nzc
local la = bx > 0 or M.aL
local ta = by > 0 or M.aT
if la and ta then
return (nzc[g - 1] + nzc[g - S.w4] + 1) // 2
elseif la then
return nzc[g - 1]
elseif ta then
return nzc[g - S.w4]
end
return 0
end
-- nCc(S, M, tab, bx, by) -> nC of chroma AC block (bx, by) of the plane whose counts are in tab
function Mb.nCc(S: any, M: any, tab: { number }, bx: number, by: number): number
local cw = S.cw4
local g = M.c0 + by * cw + bx
local la = bx > 0 or M.aL
local ta = by > 0 or M.aT
if la and ta then
return (tab[g - 1] + tab[g - cw] + 1) // 2
elseif la then
return tab[g - 1]
elseif ta then
return tab[g - cw]
end
return 0
end
-- predMode(S, M, bx, by) -> predIntra4x4PredMode / predIntra8x8PredMode of the block whose top-left 4x4 is (bx, by)
function Mb.predMode(S: any, M: any, bx: number, by: number): number
local la = bx > 0 or M.iL
local ta = by > 0 or M.iT
if not (la and ta) then
return 2
end
local ipm = S.ipm
local g = M.g0 + by * S.w4 + bx
local a, b = ipm[g - 1], ipm[g - S.w4]
return ((a < b) and (a) or (b))
end
-- ------------------------------------------------------------------------------------------------ motion vectors
-- neighbour(S, M, list, nx, ny) -> ref (UNAVAIL when not available), mvx, mvy of the 4x4 at (nx, ny) relative to the
-- macroblock (nx, ny in -1 .. 4)
local function neighbour(S: any, M: any, refs: { number }, mvs: { number }, nx: number, ny: number): (number, number, number)
local ok
if ny < 0 then
if nx < 0 then
ok = M.aTL
elseif nx < 4 then
ok = M.aT
else
ok = M.aTR
end
elseif nx < 0 then
ok = M.aL
elseif nx >= 4 then
ok = false
else
ok = M.done[ny * 4 + nx + 1]
end
if not ok then
return UNAVAIL, 0, 0
end
local g = M.g0 + ny * S.w4 + nx
local r = refs[g]
if r < 0 then
return -1, 0, 0
end
return r, mvs[2 * g - 1], mvs[2 * g]
end
local function median(a: number, b: number, c: number): number
return max(min(a, b), min(max(a, b), c))
end
-- mvp(S, M, list, x, y, w, ref, shape) -> mvpLX for the partition at (x, y) (4x4 units) of width w with reference
-- index ref. shape: 0 generic, 1 = 16x8 upper, 2 = 16x8 lower, 3 = 8x16 left, 4 = 8x16 right
local function mvp(S: any, M: any, list: number, x: number, y: number, w: number, ref: number, shape: number): (number, number)
local pic = S.pic
local refs = ((list == 0) and (pic.ref0) or (pic.ref1))
local mvs = ((list == 0) and (pic.mv0) or (pic.mv1))
local rA, ax, ay = neighbour(S, M, refs, mvs, x - 1, y)
local rB, bx, by = neighbour(S, M, refs, mvs, x, y - 1)
local rC, cx, cy = neighbour(S, M, refs, mvs, x + w, y - 1)
if rC == UNAVAIL then
rC, cx, cy = neighbour(S, M, refs, mvs, x - 1, y - 1)
end
if shape == 1 then
if rB == ref then
return bx, by
end
elseif shape == 2 then
if rA == ref then
return ax, ay
end
elseif shape == 3 then
if rA == ref then
return ax, ay
end
elseif shape == 4 then
if rC == ref then
return cx, cy
end
end
if rB == UNAVAIL and rC == UNAVAIL and rA ~= UNAVAIL then
rB, bx, by = rA, ax, ay
rC, cx, cy = rA, ax, ay
end
local n = 0
if rA == ref then
n += 1
end
if rB == ref then
n += 2
end
if rC == ref then
n += 4
end
if n == 1 then
return ax, ay
elseif n == 2 then
return bx, by
elseif n == 4 then
return cx, cy
end
return median(ax, bx, cx), median(ay, by, cy)
end
Mb.mvp = mvp
-- set(S, M, x, y, w, h, r0, m0x, m0y, r1, m1x, m1y): motion data of a region into the picture grids
local function set(S: any, M: any, x: number, y: number, w: number, h: number, r0: number, m0x: number, m0y: number, r1: number, m1x: number, m1y: number)
local pic = S.pic
local ref0, ref1, mv0, mv1, uid0, uid1 = pic.ref0, pic.ref1, pic.mv0, pic.mv1, pic.uid0, pic.uid1
local u0 = ((r0 >= 0) and (S.uids0[r0 + 1] or -1) or (-1))
local u1 = ((r1 >= 0) and (S.uids1[r1 + 1] or -1) or (-1))
local w4 = S.w4
local done = M.done
for j = y, y + h - 1 do
local g = M.g0 + j * w4 + x
for i = x, x + w - 1 do
ref0[g] = r0
ref1[g] = r1
mv0[2 * g - 1], mv0[2 * g] = m0x, m0y
mv1[2 * g - 1], mv1[2 * g] = m1x, m1y
uid0[g] = u0
uid1[g] = u1
done[j * 4 + i + 1] = true
g += 1
end
end
local k = M.nreg * 4
local reg = M.reg
reg[k + 1], reg[k + 2], reg[k + 3], reg[k + 4] = x, y, w, h
M.nreg += 1
end
-- clipping helpers
local function clip3(lo: number, hi: number, v: number): number
return min(max(v, lo), hi)
end
local function tdiv(a: number, b: number): number
local q = a / b
if q >= 0 then
return math.floor(q)
end
return -math.floor(-q)
end
-- distScale(cur, poc0, poc1) -> DistScaleFactor (8-195 .. 8-198), or nil when td is 0
function Mb.distScale(cur: number, poc0: number, poc1: number): number?
local tb = clip3(-128, 127, cur - poc0)
local td = clip3(-128, 127, poc1 - poc0)
if td == 0 then
return nil
end
local tx = tdiv(16384 + math.abs(tdiv(td, 2)), td)
return clip3(-1024, 1023, (tb * tx + 32) // 64)
end
-- pskip(S, M): P_Skip motion (8.4.1.1)
local function pskip(S: any, M: any)
local mx, my = 0, 0
if M.aL and M.aT then
local pic = S.pic
local rA, ax, ay = neighbour(S, M, pic.ref0, pic.mv0, -1, 0)
local rB, bx, by = neighbour(S, M, pic.ref0, pic.mv0, 0, -1)
if not ((rA == 0 and ax == 0 and ay == 0) or (rB == 0 and bx == 0 and by == 0)) then
mx, my = mvp(S, M, 0, 0, 0, 4, 0, 0)
end
end
set(S, M, 0, 0, 4, 4, 0, mx, my, -1, 0, 0)
end
-- colocated(S, M, bx, by) -> refIdxCol (-1: intra / none), mvCol x, y, uid of the referenced picture (8.4.1.2.1)
local function colocated(S: any, M: any, bx: number, by: number): (number, number, number, number)
local col = S.colPic
if S.direct8x8 then
bx = ((bx < 2) and (0) or (3))
by = ((by < 2) and (0) or (3))
end
if col.mbIntra[M.addr + 1] then
return -1, 0, 0, -1
end
local g = M.g0 + by * S.w4 + bx
local r = col.ref0[g]
if r >= 0 then
return r, col.mv0[2 * g - 1], col.mv0[2 * g], col.uid0[g]
end
r = col.ref1[g]
if r >= 0 then
return r, col.mv1[2 * g - 1], col.mv1[2 * g], col.uid1[g]
end
return -1, 0, 0, -1
end
local function minPositive(a: number, b: number): number
if a >= 0 and b >= 0 then
return ((a < b) and (a) or (b))
end
return ((a > b) and (a) or (b))
end
-- spatialRefs(S, M): the macroblock-level references and predictors of spatial direct (8.4.1.2.2), cached in M
local function spatialRefs(S: any, M: any)
local pic = S.pic
local r0, r1 = -1, -1
for l = 0, 1 do
local refs = ((l == 0) and (pic.ref0) or (pic.ref1))
local mvs = ((l == 0) and (pic.mv0) or (pic.mv1))
local rA = neighbour(S, M, refs, mvs, -1, 0)
local rB = neighbour(S, M, refs, mvs, 0, -1)
local rC = neighbour(S, M, refs, mvs, 4, -1)
if rC == UNAVAIL then
rC = neighbour(S, M, refs, mvs, -1, -1)
end
local r = minPositive(rA, minPositive(rB, rC))
if r < -1 then
r = -1
end
if l == 0 then
r0 = r
else
r1 = r
end
end
M.dZero = false
if r0 < 0 and r1 < 0 then
r0, r1 = 0, 0
M.dZero = true
end
M.dr0, M.dr1 = r0, r1
M.d0x, M.d0y, M.d1x, M.d1y = 0, 0, 0, 0
if not M.dZero then
if r0 >= 0 then
M.d0x, M.d0y = mvp(S, M, 0, 0, 0, 4, r0, 0)
end
if r1 >= 0 then
M.d1x, M.d1y = mvp(S, M, 1, 0, 0, 4, r1, 0)
end
end
M.dReady = true
end
-- direct8(S, M, i8): direct motion of 8x8 quadrant i8 (B_Skip, B_Direct_16x16, B_Direct_8x8)
local function direct8(S: any, M: any, i8: number)
local x0, y0 = (i8 % 2) * 2, (i8 // 2) * 2
local step = ((S.direct8x8) and (2) or (1))
if S.directSpatial then
if not M.dReady then
spatialRefs(S, M)
end
local r0, r1 = M.dr0, M.dr1
local l1short = S.colShort
for y = y0, y0 + 1, step do
for x = x0, x0 + 1, step do
local m0x, m0y, m1x, m1y = M.d0x, M.d0y, M.d1x, M.d1y
if not M.dZero and l1short then
local rc, cx, cy = colocated(S, M, x, y)
if rc == 0 and cx >= -1 and cx <= 1 and cy >= -1 and cy <= 1 then
if r0 == 0 then
m0x, m0y = 0, 0
end
if r1 == 0 then
m1x, m1y = 0, 0
end
end
end
set(S, M, x, y, step, step, r0, m0x, m0y, r1, m1x, m1y)
end
end
else
for y = y0, y0 + 1, step do
for x = x0, x0 + 1, step do
local rc, cx, cy, cu = colocated(S, M, x, y)
local r0 = 0
if rc >= 0 then
r0 = S.uidToRef0[cu] or 0
end
local m0x, m0y, m1x, m1y = cx, cy, 0, 0
local dsf = S.dsf[r0 + 1]
if dsf then
m0x = (dsf * cx + 128) // 256
m0y = (dsf * cy + 128) // 256
m1x = m0x - cx
m1y = m0y - cy
end
set(S, M, x, y, step, step, r0, m0x, m0y, 0, m1x, m1y)
end
end
end
end
-- partition geometry (4x4 units) of the sub-macroblock shapes
local SUB_N = { 1, 2, 2, 4 }
local SUB_W = { 2, 2, 1, 1 }
local SUB_H = { 2, 1, 2, 1 }
-- motion(S, M): motion vectors of an inter macroblock into the grids, and its MC regions
local function motion(S: any, M: any)
local kind = M.kind
M.dReady = false
if kind == K_PSKIP then
pskip(S, M)
return
end
if kind == K_BSKIP or kind == K_BDIRECT then
for i8 = 0, 3 do
direct8(S, M, i8)
end
return
end
local part = M.part
local mvd0, mvd1 = M.mvd0, M.mvd1
if part < 3 then
local n = ((part == 0) and (1) or (2))
for p = 1, n do
local x, y, w, h = 0, 0, 4, 4
local shape = 0
if part == 1 then
y, h = (p - 1) * 2, 2
shape = p
elseif part == 2 then
x, w = (p - 1) * 2, 2
shape = 2 + p
end
local pf = M.pf[p]
local r0, r1 = -1, -1
local m0x, m0y, m1x, m1y = 0, 0, 0, 0
if pf % 2 == 1 then
r0 = M.ref0[p]
m0x, m0y = mvp(S, M, 0, x, y, w, r0, shape)
m0x += mvd0[2 * p - 1]
m0y += mvd0[2 * p]
end
if pf >= 2 then
r1 = M.ref1[p]
m1x, m1y = mvp(S, M, 1, x, y, w, r1, shape)
m1x += mvd1[2 * p - 1]
m1y += mvd1[2 * p]
end
set(S, M, x, y, w, h, r0, m0x, m0y, r1, m1x, m1y)
end
return
end
for i8 = 0, 3 do
if M.dir[i8 + 1] then
direct8(S, M, i8)
else
local x0, y0 = (i8 % 2) * 2, (i8 // 2) * 2
local sub = M.sub[i8 + 1]
local pf = M.pf[i8 + 1]
local w, h = SUB_W[sub + 1], SUB_H[sub + 1]
for k = 0, SUB_N[sub + 1] - 1 do
local x, y = x0, y0
if sub == 1 then
y += k
elseif sub == 2 then
x += k
elseif sub == 3 then
x += k % 2
y += k // 2
end
local p = i8 * 4 + k + 1
local r0, r1 = -1, -1
local m0x, m0y, m1x, m1y = 0, 0, 0, 0
if pf % 2 == 1 then
r0 = M.ref0[i8 + 1]
m0x, m0y = mvp(S, M, 0, x, y, w, r0, 0)
m0x += mvd0[2 * p - 1]
m0y += mvd0[2 * p]
end
if pf >= 2 then
r1 = M.ref1[i8 + 1]
m1x, m1y = mvp(S, M, 1, x, y, w, r1, 0)
m1x += mvd1[2 * p - 1]
m1y += mvd1[2 * p]
end
set(S, M, x, y, w, h, r0, m0x, m0y, r1, m1x, m1y)
end
end
end
end
Mb.motion = motion
-- ------------------------------------------------------------------------------------------------ motion compensation
-- predict(S, M, pic, P, list, x, y, w, h, mvx, mvy, plane): one list's prediction samples of a region into P
local function predLuma(S: any, ref: any, P: { number }, px: number, py: number, w: number, h: number, mvx: number, mvy: number)
local W, H = S.mbW * 16, S.mbH * 16
local ix = px + mvx // 4
local iy = py + mvy // 4
ix = clip3(2 - PAD, W + PAD - w - 4, ix)
iy = clip3(2 - PAD, H + PAD - h - 4, iy)
local s = ref.ys
Inter.luma(ref.y, s, ref.yo + iy * s + ix, mvx % 4, mvy % 4, w, h, P)
end
local function predChroma(S: any, ref: any, P: { number }, plane: buffer, px: number, py: number, w: number, h: number, mvx: number, mvy: number)
local W, H = S.mbW * 8, S.mbH * 8
local ix = px + mvx // 8
local iy = py + mvy // 8
ix = clip3(-PADC, W + PADC - 1 - w, ix)
iy = clip3(-PADC, H + PADC - 1 - h, iy)
local s = ref.cs
Inter.chroma(plane, s, ref.co + iy * s + ix, mvx % 8, mvy % 8, w, h, P)
end
local function refPic(list: { any }, r: number): any
return list[r + 1] or list[1]
end
-- mc(S, M): inter prediction of every region of the macroblock into the current picture
local function mc(S: any, M: any)
local pic = S.pic
local reg = M.reg
local P0, P1 = M.P0, M.P1
local ys, cs = pic.ys, pic.cs
local wm = S.weightMode
local list0, list1 = S.list0, S.list1
for k = 0, M.nreg - 1 do
local x, y, w, h = reg[k * 4 + 1], reg[k * 4 + 2], reg[k * 4 + 3], reg[k * 4 + 4]
local g = M.g0 + y * S.w4 + x
local r0, r1 = pic.ref0[g], pic.ref1[g]
local m0x, m0y, m1x, m1y = pic.mv0[2 * g - 1], pic.mv0[2 * g], pic.mv1[2 * g - 1], pic.mv1[2 * g]
local ref0 = ((r0 >= 0) and (refPic(list0, r0)) or (nil))
local ref1 = ((r1 >= 0) and (refPic(list1, r1)) or (nil))
if ref0 == nil and ref1 == nil then
ref0 = list0[1] or list1[1]
r0 = 0
if ref0 == nil then
continue
end
end
local px, py = M.x * 16 + x * 4, M.y * 16 + y * 4
local lw, lh = w * 4, h * 4
local lo = pic.yo + py * ys + px
local cx, cy, cw, ch = M.x * 8 + x * 2, M.y * 8 + y * 2, w * 2, h * 2
local co = pic.co + cy * cs + cx
-- weights for this pair
local bi = ref0 ~= nil and ref1 ~= nil
local explicit = wm == 1
local implicitW0, implicitW1 = 32, 32
if wm == 2 then
if bi then
local iw = S.implicit[r0 * 32 + r1 + 1]
implicitW0, implicitW1 = 64 - iw, iw
else
explicit = false
end
end
local W0, W1 = S.weights0, S.weights1
for plane = 0, 2 do
local dst, o, s, bw, bh = pic.y, lo, ys, lw, lh
if plane == 1 then
dst, o, s, bw, bh = pic.u, co, cs, cw, ch
elseif plane == 2 then
dst, o, s, bw, bh = pic.v, co, cs, cw, ch
end
if ref0 then
if plane == 0 then
predLuma(S, ref0, P0, px, py, lw, lh, m0x, m0y)
else
predChroma(S, ref0, P0, ((plane == 1) and (ref0.u) or (ref0.v)), cx, cy, cw, ch, m0x, m0y)
end
end
if ref1 then
if plane == 0 then
predLuma(S, ref1, P1, px, py, lw, lh, m1x, m1y)
else
predChroma(S, ref1, P1, ((plane == 1) and (ref1.u) or (ref1.v)), cx, cy, cw, ch, m1x, m1y)
end
end
if wm == 2 and bi then
Inter.weight2(P0, P1, bw, bh, dst, o, s, 5, implicitW0, implicitW1, 0)
elseif explicit then
local logWD = ((plane == 0) and (S.lumaDenom) or (S.chromaDenom))
local wi = plane * 2 + 1
if bi then
local b0, b1 = r0 * 8, r1 * 8
Inter.weight2(P0, P1, bw, bh, dst, o, s, logWD, W0[b0 + wi] or 1, W1[b1 + wi] or 1, ((W0[b0 + wi + 1] or 0) + (W1[b1 + wi + 1] or 0) + 1) // 2)
elseif ref0 then
local b0 = r0 * 8
Inter.weight1(P0, bw, bh, dst, o, s, logWD, W0[b0 + wi] or 1, W0[b0 + wi + 1] or 0)
else
local b1 = r1 * 8
Inter.weight1(P1, bw, bh, dst, o, s, logWD, W1[b1 + wi] or 1, W1[b1 + wi + 1] or 0)
end
elseif bi then
Inter.avg(P0, P1, bw, bh, dst, o, s)
elseif ref0 then
Inter.put(P0, bw, bh, dst, o, s)
else
Inter.put(P1, bw, bh, dst, o, s)
end
end
end
end
-- ------------------------------------------------------------------------------------------------ residual
local function lumaResidual(S: any, M: any)
local pic = S.pic
local b, s = pic.y, pic.ys
local o = pic.yo + M.y * 16 * s + M.x * 16
local nz, c4, c8 = M.nz, M.c4, M.c8
if M.t8 == 1 then
for i8 = 0, 3 do
local bx, by = (i8 % 2) * 2, (i8 // 2) * 2
if nz[by * 4 + bx + 1] > 0 then
add8(c8, i8 * 64, b, o + by * 4 * s + bx * 4, s)
end
end
return
end
for blk = 0, 15 do
if nz[blk + 1] > 0 then
local bx, by = blk % 4, blk // 4
add4(c4, blk * 16, b, o + by * 4 * s + bx * 4, s)
end
end
end
local function chromaResidual(S: any, M: any)
if not M.hasDcC and M.cbp < 32 then
return
end
local pic = S.pic
local s = pic.cs
local o = pic.co + M.y * 8 * s + M.x * 8
local cc, nzc = M.cc, M.nzc
if M.hasDcC then
local dq = S.dq.d4
local intra = M.kind <= K_PCM
Transform.chromaDc(M.dcC, 0, dq[((intra) and (2) or (5))][M.qpc + 1][1], cc, 0)
Transform.chromaDc(M.dcC, 4, dq[((intra) and (3) or (6))][M.qpc2 + 1][1], cc, 64)
end
for plane = 0, 1 do
local b = ((plane == 0) and (pic.u) or (pic.v))
for blk = 0, 3 do
local base = (plane * 4 + blk) * 16
local q = o + (blk // 2) * 4 * s + (blk % 2) * 4
if nzc[plane * 4 + blk + 1] > 0 then
add4(cc, base, b, q, s)
elseif cc[base + 1] ~= 0 then
addDc4(cc[base + 1], b, q, s)
cc[base + 1] = 0
end
end
end
end
-- ------------------------------------------------------------------------------------------------ intra
local function intraLuma(S: any, M: any)
local pic = S.pic
local b, s = pic.y, pic.ys
local o = pic.yo + M.y * 16 * s + M.x * 16
local kind = M.kind
local ipm = M.ipm
if kind == K_I16 then
Intra.pred16x16(b, o, s, M.i16, M.iL, M.iT, M.iTL)
local c4, nz = M.c4, M.nz
if M.hasDcY then
Transform.lumaDc(M.dcY, S.dq.d4[1][M.qp + 1][1], c4)
end
for blk = 0, 15 do
local bx, by = blk % 4, blk // 4
local q = o + by * 4 * s + bx * 4
if nz[blk + 1] > 0 then
add4(c4, blk * 16, b, q, s)
elseif c4[blk * 16 + 1] ~= 0 then
addDc4(c4[blk * 16 + 1], b, q, s)
c4[blk * 16 + 1] = 0
end
end
elseif kind == K_I8 then
local nz, c8 = M.nz, M.c8
for i8 = 0, 3 do
local bx, by = (i8 % 2) * 2, (i8 // 2) * 2
local left = bx > 0 or M.iL
local top = by > 0 or M.iT
local tr, tl
if i8 == 0 then
tr, tl = M.iT, M.iTL
elseif i8 == 1 then
tr, tl = M.iTR, M.iT
elseif i8 == 2 then
tr, tl = true, M.iL
else
tr, tl = false, true
end
local q = o + by * 4 * s + bx * 4
Intra.pred8x8(b, q, s, ipm[by * 4 + bx + 1], left, top, tr, tl)
if nz[by * 4 + bx + 1] > 0 then
add8(c8, i8 * 64, b, q, s)
end
end
else
local nz, c4 = M.nz, M.c4
for i = 0, 15 do
local bx, by = BLK_X[i + 1], BLK_Y[i + 1]
local blk = by * 4 + bx
local left = bx > 0 or M.iL
local top = by > 0 or M.iT
local tl
if bx > 0 and by > 0 then
tl = true
elseif bx > 0 then
tl = M.iT
elseif by > 0 then
tl = M.iL
else
tl = M.iTL
end
local tr
if by == 0 then
if bx < 3 then
tr = M.iT
else
tr = M.iTR
end
else
tr = not (bx == 3 or (bx == 1 and (by == 1 or by == 3)))
end
local q = o + by * 4 * s + bx * 4
Intra.pred4x4(b, q, s, ipm[blk + 1], left, top, tr, tl)
if nz[blk + 1] > 0 then
add4(c4, blk * 16, b, q, s)
end
end
end
end
local function intraChroma(S: any, M: any)
local pic = S.pic
local s = pic.cs
local o = pic.co + M.y * 8 * s + M.x * 8
Intra.predChroma(pic.u, o, s, M.cpm, M.iL, M.iT, M.iTL)
Intra.predChroma(pic.v, o, s, M.cpm, M.iL, M.iT, M.iTL)
end
local function pcm(S: any, M: any)
local pic = S.pic
local src = M.pcm
local s = pic.ys
local o = pic.yo + M.y * 16 * s + M.x * 16
for y = 0, 15 do
buffer.copy(pic.y, o + y * s, src, y * 16, 16)
end
s = pic.cs
o = pic.co + M.y * 8 * s + M.x * 8
for y = 0, 7 do
buffer.copy(pic.u, o + y * s, src, 256 + y * 8, 8)
buffer.copy(pic.v, o + y * s, src, 320 + y * 8, 8)
end
end
-- ------------------------------------------------------------------------------------------------ macroblock
-- recon(S, M): reconstruct the parsed macroblock and store what later macroblocks / deblocking need
function Mb.recon(S: any, M: any)
local kind = M.kind
local addr = M.addr + 1
local pic = S.pic
local w4 = S.w4
S.kind[addr] = kind
S.sliceTab[addr] = S.slice
S.t8Tab[addr] = M.t8
S.qpTab[addr] = ((kind == K_PCM) and (0) or (M.qp))
S.qpcTab[addr] = ((kind == K_PCM) and (S.qpcOf[1]) or (M.qpc))
S.qpc2Tab[addr] = ((kind == K_PCM) and (S.qpc2Of[1]) or (M.qpc2))
pic.mbIntra[addr] = kind <= K_PCM
local nzDb = S.nzDb
local g0 = M.g0
if kind <= K_PCM then
-- intra: no motion
local ref0, ref1, uid0, uid1 = pic.ref0, pic.ref1, pic.uid0, pic.uid1
for j = 0, 3 do
local g = g0 + j * w4
for i = 0, 3 do
ref0[g + i], ref1[g + i], uid0[g + i], uid1[g + i] = -1, -1, -1, -1
end
end
if kind == K_PCM or kind == K_I16 then
local ipm = S.ipm
for j = 0, 3 do
local g = g0 + j * w4
ipm[g], ipm[g + 1], ipm[g + 2], ipm[g + 3] = 2, 2, 2, 2
end
end
if kind == K_PCM then
pcm(S, M)
return
end
intraLuma(S, M)
intraChroma(S, M)
chromaResidual(S, M)
return
end
local ipm = S.ipm
for j = 0, 3 do
local g = g0 + j * w4
ipm[g], ipm[g + 1], ipm[g + 2], ipm[g + 3] = 2, 2, 2, 2
end
motion(S, M)
mc(S, M)
local nz = M.nz
if M.t8 == 1 then
for i8 = 0, 3 do
local bx, by = (i8 % 2) * 2, (i8 // 2) * 2
local v = ((nz[by * 4 + bx + 1] > 0) and (1) or (0))
local g = g0 + by * w4 + bx
nzDb[g], nzDb[g + 1], nzDb[g + w4], nzDb[g + w4 + 1] = v, v, v, v
end
else
for blk = 0, 15 do
nzDb[g0 + (blk // 4) * w4 + blk % 4] = ((nz[blk + 1] > 0) and (1) or (0))
end
end
if M.cbp % 16 ~= 0 then
lumaResidual(S, M)
end
chromaResidual(S, M)
end
-- skip(S, M, kind): a P_Skip / B_Skip macroblock (no residual)
function Mb.skip(S: any, M: any, kind: number)
M.kind = kind
M.cbp = 0
M.t8 = 0
M.qp = S.qp
M.qpc = S.qpcOf[S.qp + 1]
M.qpc2 = S.qpc2Of[S.qp + 1]
local addr = M.addr + 1
S.cbpTab[addr] = 0
S.dcTab[addr] = 0
S.cpmTab[addr] = 0
S.qpdTab[addr] = 0
local nz = M.nz
for i = 1, 16 do
nz[i] = 0
end
local nzc = M.nzc
for i = 1, 8 do
nzc[i] = 0
end
local w4, g0 = S.w4, M.g0
local grid, mvd0, mvd1, rgt0, rgt1 = S.nzc, S.mvd0, S.mvd1, S.rgt0, S.rgt1
for j = 0, 3 do
local g = g0 + j * w4
for i = 0, 3 do
grid[g + i] = 0
mvd0[2 * (g + i) - 1], mvd0[2 * (g + i)] = 0, 0
mvd1[2 * (g + i) - 1], mvd1[2 * (g + i)] = 0, 0
rgt0[g + i], rgt1[g + i] = 0, 0
end
end
local cw, c0 = S.cw4, M.c0
local cb, cr = S.nzcCb, S.nzcCr
cb[c0], cb[c0 + 1], cb[c0 + cw], cb[c0 + cw + 1] = 0, 0, 0, 0
cr[c0], cr[c0 + 1], cr[c0 + cw], cr[c0 + cw + 1] = 0, 0, 0, 0
M.hasDcC = false
Mb.recon(S, M)
end
return Mb