Files
rosetta/src/Rosetta/codecs/video/H264/Cabac.luau
T

1131 lines
26 KiB
Luau

--!strict
--!native
--!optimize 2
-- codecs/video/H264/Cabac: slice data and macroblock layer with CABAC entropy coding (ITU-T H.264 7.3.4, 7.3.5, 9.3):
-- engine (9.3.1.2 / 9.3.3.2), context init (9.3.1.1), binarizations and ctxIdxInc (9.3.2 / 9.3.3.1), residual_block_cabac.
-- codIOffset is kept with `nb` look-ahead bits (low = offset * 2^nb + next bits), so renormalisation is one multiply.
local Bitstream = require("@rosetta/codecs/video/H264/Bitstream")
local Tables = require("@rosetta/codecs/video/H264/Tables")
local CabacTables = require("@rosetta/codecs/video/H264/CabacTables")
local Mb = require("@rosetta/codecs/video/H264/Mb")
local readu8 = buffer.readu8
local min = math.min
local K_I4, K_I8, K_I16, K_PCM, K_P, K_PSKIP, K_B, K_BSKIP, K_BDIRECT = 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
local BLK_X, BLK_Y = Mb.BLK_X, Mb.BLK_Y
local Cabac = {}
local RANGE_LPS = CabacTables.RANGE_LPS
local TRANS = CabacTables.TRANS
local POW2 = table.create(40, 1)
for i = 0, 39 do
POW2[i + 1] = 2 ^ i
end
local NORM = table.create(512, 0) -- NORM[r + 1] = renormalisation shift of range r (r < 512)
for r = 1, 511 do
local s, v = 0, r
while v < 256 do
v *= 2
s += 1
end
NORM[r + 1] = s
end
-- state per context: pStateIdx * 2 + valMPS, at ST[ctxIdx + 1]
local ST = table.create(460, 0)
-- per packed state s (pStateIdx * 2 + valMPS) and LPS bit: the next state and the bin; rangeTabLPS by packed state
local NEXTS, BINS = table.create(256, 0), table.create(256, 0)
local LPSR = table.create(512, 0)
for st = 0, 127 do
local p, mps = st // 2, st % 2
NEXTS[st * 2 + 1] = TRANS[p * 2 + 2] * 2 + mps
BINS[st * 2 + 1] = mps
NEXTS[st * 2 + 2] = TRANS[p * 2 + 1] * 2 + (if p == 0 then 1 - mps else mps)
BINS[st * 2 + 2] = 1 - mps
for q = 0, 3 do
LPSR[st * 4 + q + 1] = RANGE_LPS[p * 4 + q + 1]
end
end
local ebuf: buffer = buffer.create(1)
local epos, elimit = 0, 0
local erange, elow, enb = 510, 0, 0
local function refill()
local b = 0
if epos < elimit then
b = readu8(ebuf, epos)
end
epos += 1
elow = elow * 256 + b
enb += 8
end
-- decision (9.3.3.2.1): the LPS test is min(1, low // scaled) (low < 2.5 * scaled always)
local function decision(ctx: number): number
local s = ST[ctx + 1]
local range = erange
local lps = LPSR[s * 4 + (range // 64) % 4 + 1]
local r = range - lps
local scaled = r * POW2[enb + 1]
local bit = min(1, elow // scaled)
elow -= bit * scaled
r += bit * (lps - r)
local k = s * 2 + bit + 1
ST[ctx + 1] = NEXTS[k]
local sh = NORM[r + 1]
erange = r * POW2[sh + 1]
enb -= sh
if enb < 8 then
refill()
end
return BINS[k]
end
local function bypass(): number
enb -= 1
local scaled = erange * POW2[enb + 1]
local bin = elow // scaled
elow -= bin * scaled
if enb < 8 then
refill()
end
return bin
end
local function terminate(): number
local r = erange - 2
local scaled = r * POW2[enb + 1]
if elow >= scaled then
erange = r
return 1
end
if r < 256 then
r *= 2
enb -= 1
if enb < 8 then
refill()
end
end
erange = r
return 0
end
local function engineInit(b: buffer, p: number, limit: number)
ebuf, epos, elimit = b, p, limit
elow, enb = 0, -9
refill()
refill()
refill()
erange = 510
end
-- the bit position the spec's decoder is at (after a terminate bin of 1)
local function bitPos(): number
return epos * 8 - enb
end
local function initContexts(sliceQp: number, model: number)
local init = CabacTables.CABAC_INIT
local qp = math.clamp(sliceQp, 0, 51)
for ctx = 0, 459 do
local m = init[ctx * 8 + model * 2 + 1]
local n = init[ctx * 8 + model * 2 + 2]
local pre = math.clamp((m * qp) // 16 + n, 1, 126)
if pre <= 63 then
ST[ctx + 1] = (63 - pre) * 2
else
ST[ctx + 1] = (pre - 64) * 2 + 1
end
end
end
local ZZ4, ZZ8 = Tables.ZIGZAG4, Tables.ZIGZAG8
local SIG8, LAST8 = CabacTables.SIG8, CabacTables.LAST8
local CAT_CBF = { 0, 4, 8, 12, 16 }
local CAT_SIG = { 0, 15, 29, 44, 47 }
local CAT_ABS = { 0, 10, 20, 30, 39 }
local MAXC = { 16, 15, 16, 4, 15, 64 }
local SIGPOS = table.create(64, 0)
-- positions: cat 0 / 2 zig-zag, 1 / 4 zig-zag from 1, 3 raster 0..3, 5 the 8x8 zig-zag
local SCAN = {}
do
local ac = table.create(15, 0)
for i = 1, 15 do
ac[i] = ZZ4[i + 1]
end
SCAN[1], SCAN[2], SCAN[3], SCAN[4], SCAN[5], SCAN[6] = ZZ4, ac, ZZ4, { 0, 1, 2, 3 }, ac, ZZ8
end
local SIG3 = { 0, 1, 2 }
local function block(cat: number, out: { number }, base: number, mul: { number }?, div: number): number
local maxc = MAXC[cat + 1]
local n = 0
local last = false
-- significance map
if cat == 5 then
for i = 0, 62 do
if decision(402 + SIG8[i + 1]) == 1 then
n += 1
SIGPOS[n] = i
if decision(417 + LAST8[i + 1]) == 1 then
last = true
break
end
end
end
elseif cat == 3 then
local sb, lb = 105 + 44, 166 + 44
for i = 0, 2 do
if decision(sb + SIG3[i + 1]) == 1 then
n += 1
SIGPOS[n] = i
if decision(lb + SIG3[i + 1]) == 1 then
last = true
break
end
end
end
else
local sb = 105 + CAT_SIG[cat + 1]
local lb = 166 + CAT_SIG[cat + 1]
for i = 0, maxc - 2 do
if decision(sb + i) == 1 then
n += 1
SIGPOS[n] = i
if decision(lb + i) == 1 then
last = true
break
end
end
end
end
if not last then
n += 1
SIGPOS[n] = maxc - 1
end
-- levels, highest frequency first
local absBase = if cat == 5 then 426 else 227 + CAT_ABS[cat + 1]
local gt1, eq1 = 0, 0
local half = div // 2
local catMax = if cat == 3 then 3 else 4
local scan = SCAN[cat + 1]
for k = n, 1, -1 do
local v
local inc = 0
if gt1 == 0 then
inc = min(eq1 + 1, 4)
end
if decision(absBase + inc) == 0 then
v = 1
eq1 += 1
else
local ctx = absBase + 5 + min(gt1, catMax)
local prefix = 1
while prefix < 14 and decision(ctx) == 1 do
prefix += 1
end
v = prefix + 1
if prefix == 14 then
-- Exp-Golomb (k = 0) suffix
local kk = 0
while bypass() == 1 do
v += POW2[kk + 1]
kk += 1
if kk > 24 then
return -1
end
end
while kk > 0 do
kk -= 1
v += bypass() * POW2[kk + 1]
end
end
gt1 += 1
end
if bypass() == 1 then
v = -v
end
local p = scan[SIGPOS[k] + 1]
if mul then
out[base + p + 1] = (v * mul[p + 1] + half) // div
else
out[base + p + 1] = v
end
end
return n
end
-- condition helpers on neighbouring macroblocks (addresses are 1-based here)
local function leftAddr(S: any, M: any): number
return ((M.aL) and (M.addr) or (0))
end
local function topAddr(S: any, M: any): number
return ((M.aT) and (M.addr - S.mbW + 1) or (0))
end
local function mbSkipFlag(S: any, M: any): number
local k = S.kind
local a, b = leftAddr(S, M), topAddr(S, M)
local inc = 0
if a > 0 and k[a] ~= K_PSKIP and k[a] ~= K_BSKIP then
inc += 1
end
if b > 0 and k[b] ~= K_PSKIP and k[b] ~= K_BSKIP then
inc += 1
end
return decision((((S.type == 1) and (24) or (11))) + inc)
end
-- intra mb_type (I slice: prefix false, offset 3; P / B suffix: offset 17 / 32) -> 0..25
local function intraType(S: any, M: any, off: number): number
local b0
if off == 3 then
local k = S.kind
local a, b = leftAddr(S, M), topAddr(S, M)
local inc = 0
if a > 0 and k[a] ~= K_I4 and k[a] ~= K_I8 then
inc += 1
end
if b > 0 and k[b] ~= K_I4 and k[b] ~= K_I8 then
inc += 1
end
b0 = decision(3 + inc)
else
b0 = decision(off)
end
if b0 == 0 then
return 0
end
if terminate() == 1 then
return 25
end
local luma, chroma, pred
if off == 3 then
luma = decision(6)
chroma = decision(7)
if chroma == 1 then
chroma += decision(8)
end
pred = decision(9) * 2
pred += decision(10)
else
luma = decision(off + 1)
chroma = decision(off + 2)
if chroma == 1 then
chroma += decision(off + 2)
end
pred = decision(off + 3) * 2
pred += decision(off + 3)
end
return 1 + pred + 4 * chroma + 12 * luma
end
local function pMbType(): number
-- 0..3 inter, 5+ intra (returned as -1: the caller decodes the suffix)
if decision(14) == 1 then
return -1
end
if decision(15) == 0 then
return ((decision(16) == 0) and (0) or (3))
end
return ((decision(17) == 1) and (1) or (2))
end
local function bMbType(S: any, M: any): number
local k = S.kind
local a, b = leftAddr(S, M), topAddr(S, M)
local inc = 0
if a > 0 and k[a] ~= K_BSKIP and k[a] ~= K_BDIRECT then
inc += 1
end
if b > 0 and k[b] ~= K_BSKIP and k[b] ~= K_BDIRECT then
inc += 1
end
if decision(27 + inc) == 0 then
return 0
end
if decision(30) == 0 then
return 1 + decision(32)
end
local bits = decision(31) * 8
bits += decision(32) * 4
bits += decision(32) * 2
bits += decision(32)
if bits < 8 then
return bits + 3
elseif bits == 13 then
return -1 -- intra
elseif bits == 14 then
return 11
elseif bits == 15 then
return 22
end
bits = bits * 2 + decision(32)
return bits - 4
end
local function pSubType(): number
if decision(21) == 1 then
return 0
end
if decision(22) == 0 then
return 1
end
return ((decision(23) == 1) and (2) or (3))
end
local function bSubType(): number
if decision(36) == 0 then
return 0
end
if decision(37) == 0 then
return 1 + decision(39)
end
local t = 3
if decision(38) == 1 then
if decision(39) == 1 then
return 11 + decision(39)
end
t += 4
end
t += 2 * decision(39)
t += decision(39)
return t
end
local function refIdx(S: any, M: any, rgt: { number }, x: number, y: number): number
local g = M.g0 + y * S.w4 + x
local inc = 0
if (x > 0 or M.aL) and rgt[g - 1] ~= 0 then
inc += 1
end
if (y > 0 or M.aT) and rgt[g - S.w4] ~= 0 then
inc += 2
end
if decision(54 + inc) == 0 then
return 0
end
local v = 1
local ctx = 58
while decision(ctx) == 1 do
v += 1
ctx = 59
if v > 32 then
return -1
end
end
return v
end
-- the mvd grid holds |mvd| (2 per 4x4)
local function mvd(S: any, M: any, grid: { number }, x: number, y: number, comp: number): number
local g = M.g0 + y * S.w4 + x
local sum = 0
if x > 0 or M.aL then
sum += grid[2 * (g - 1) - 1 + comp]
end
if y > 0 or M.aT then
sum += grid[2 * (g - S.w4) - 1 + comp]
end
local base = ((comp == 0) and (40) or (47))
local inc = 0
if sum > 32 then
inc = 2
elseif sum >= 3 then
inc = 1
end
if decision(base + inc) == 0 then
return 0
end
local v = 1
local ctx = base + 3
while v < 9 and decision(ctx) == 1 do
v += 1
if ctx < base + 6 then
ctx += 1
end
end
if v >= 9 then
local k = 3
while bypass() == 1 do
v += POW2[k + 1]
k += 1
if k > 24 then
return 0
end
end
while k > 0 do
k -= 1
v += bypass() * POW2[k + 1]
end
end
if bypass() == 1 then
return -v
end
return v
end
local function fillRegion(S: any, M: any, grid: { number }, x: number, y: number, w: number, h: number, v: number)
local w4 = S.w4
for j = y, y + h - 1 do
local g = M.g0 + j * w4 + x
for i = 0, w - 1 do
grid[g + i] = v
end
end
end
local function fillMvd(S: any, M: any, grid: { number }, x: number, y: number, w: number, h: number, ax: number, ay: number)
local w4 = S.w4
for j = y, y + h - 1 do
local g = M.g0 + j * w4 + x
for i = 0, w - 1 do
grid[2 * (g + i) - 1] = ax
grid[2 * (g + i)] = ay
end
end
end
local function cbpCabac(S: any, M: any): number
local cbpTab = S.cbpTab
local a, b = leftAddr(S, M), topAddr(S, M)
local luma = 0
for b8 = 0, 3 do
local bx, by = b8 % 2, b8 // 2
local condA, condB = 0, 0
if bx == 1 then
if (luma // POW2[b8 - 1 + 1]) % 2 == 0 then
condA = 1
end
elseif a > 0 then
if (cbpTab[a] // POW2[b8 + 1 + 1]) % 2 == 0 then
condA = 1
end
end
if by == 1 then
if (luma // POW2[b8 - 2 + 1]) % 2 == 0 then
condB = 1
end
elseif b > 0 then
if (cbpTab[b] // POW2[b8 + 2 + 1]) % 2 == 0 then
condB = 1
end
end
if decision(73 + condA + 2 * condB) == 1 then
luma += POW2[b8 + 1]
end
end
local ca = ((a > 0) and (cbpTab[a] // 16) or (0))
local cb = ((b > 0) and (cbpTab[b] // 16) or (0))
local inc = (((ca ~= 0) and (1) or (0))) + (((cb ~= 0) and (2) or (0)))
local chroma = 0
if decision(77 + inc) == 1 then
inc = (((ca == 2) and (1) or (0))) + (((cb == 2) and (2) or (0)))
chroma = 1 + decision(81 + inc)
end
return luma + 16 * chroma
end
local function qpDelta(S: any): number
local k = 0
if decision(60 + (((S.lastQpd) and (1) or (0)))) == 1 then
k = 1
local ctx = 62
while decision(ctx) == 1 do
k += 1
ctx = 63
if k > 104 then
return 999
end
end
end
if k % 2 == 1 then
return (k + 1) // 2
end
return -(k // 2)
end
local function chromaPredMode(S: any, M: any): number
local a, b = leftAddr(S, M), topAddr(S, M)
local cpm, kind = S.cpmTab, S.kind
local inc = 0
if a > 0 and kind[a] <= K_I16 and cpm[a] ~= 0 then
inc += 1
end
if b > 0 and kind[b] <= K_I16 and cpm[b] ~= 0 then
inc += 1
end
if decision(64 + inc) == 0 then
return 0
end
if decision(67) == 0 then
return 1
end
return 2 + decision(67)
end
local function t8Flag(S: any, M: any): number
local a, b = leftAddr(S, M), topAddr(S, M)
local t8 = S.t8Tab
local inc = 0
if a > 0 and t8[a] == 1 then
inc += 1
end
if b > 0 and t8[b] == 1 then
inc += 1
end
return decision(399 + inc)
end
-- coded_block_flag condition of a neighbour: v = the neighbour's flag when its block is available
local function cbf(cat: number, condA: number, condB: number): number
return decision(85 + CAT_CBF[cat + 1] + condA + 2 * condB)
end
-- luma 4x4 coded_block_flag neighbours of (bx, by)
local function lumaCond(S: any, M: any, bx: number, by: number, intra: boolean): (number, number)
local g = M.g0 + by * S.w4 + bx
local nzc = S.nzc
local ca, cb
if bx > 0 then
ca = ((nzc[g - 1] ~= 0) and (1) or (0))
elseif M.aL then
ca = ((nzc[g - 1] ~= 0) and (1) or (0))
else
ca = ((intra) and (1) or (0))
end
if by > 0 then
cb = ((nzc[g - S.w4] ~= 0) and (1) or (0))
elseif M.aT then
cb = ((nzc[g - S.w4] ~= 0) and (1) or (0))
else
cb = ((intra) and (1) or (0))
end
return ca, cb
end
local function chromaCond(S: any, M: any, tab: { number }, bx: number, by: number, intra: boolean): (number, number)
local cw = S.cw4
local g = M.c0 + by * cw + bx
local ca, cb
if bx > 0 or M.aL then
ca = ((tab[g - 1] ~= 0) and (1) or (0))
else
ca = ((intra) and (1) or (0))
end
if by > 0 or M.aT then
cb = ((tab[g - cw] ~= 0) and (1) or (0))
else
cb = ((intra) and (1) or (0))
end
return ca, cb
end
local function dcCond(S: any, M: any, bit: number, intra: boolean): (number, number)
local dc = S.dcTab
local a, b = leftAddr(S, M), topAddr(S, M)
local ca, cb
if a > 0 then
ca = (((dc[a] // bit) % 2 ~= 0) and (1) or (0))
else
ca = ((intra) and (1) or (0))
end
if b > 0 then
cb = (((dc[b] // bit) % 2 ~= 0) and (1) or (0))
else
cb = ((intra) and (1) or (0))
end
return ca, cb
end
local function residuals(S: any, M: any): boolean
local kind = M.kind
local cbp = M.cbp
local intra = kind <= K_PCM
local dq = S.dq
local qp = M.qp
local w4, g0 = S.w4, M.g0
local grid = S.nzc
local nz = M.nz
local c4 = M.c4
local dcBits = 0
if kind == K_I16 then
local ca, cb = dcCond(S, M, 1, intra)
if cbf(0, ca, cb) == 1 then
local n = block(0, M.dcY, 0, nil, 1)
if n < 0 then
return false
end
M.hasDcY = true
dcBits += 1
end
end
local mul4 = dq.d4[((intra) and (1) or (4))][qp + 1]
if M.t8 == 1 then
local mul8 = dq.d8[((intra) and (1) or (2))][qp + 1]
for i8 = 0, 3 do
local bx, by = (i8 % 2) * 2, (i8 // 2) * 2
local n = 0
if (cbp // POW2[i8 + 1]) % 2 ~= 0 then
n = block(5, M.c8, i8 * 64, mul8, 64)
if n < 0 then
return false
end
end
local f = ((n > 0) and (1) or (0))
local g = g0 + by * w4 + bx
grid[g], grid[g + 1], grid[g + w4], grid[g + w4 + 1] = f, f, f, f
nz[by * 4 + bx + 1], nz[by * 4 + bx + 2], nz[by * 4 + bx + 5], nz[by * 4 + bx + 6] = n, n, n, n
end
else
local ac = kind == K_I16
local cat = ((ac) and (1) or (2))
for i = 0, 15 do
local bx, by = BLK_X[i + 1], BLK_Y[i + 1]
local blk = by * 4 + bx
local n = 0
if (cbp // POW2[i // 4 + 1]) % 2 ~= 0 then
local ca, cb = lumaCond(S, M, bx, by, intra)
if cbf(cat, ca, cb) == 1 then
n = block(cat, c4, blk * 16, mul4, 16)
if n < 0 then
return false
end
end
end
grid[g0 + by * w4 + bx] = ((n > 0) and (1) or (0))
nz[blk + 1] = n
end
end
local chroma = cbp // 16
local cw, c0 = S.cw4, M.c0
local nzc = M.nzc
if chroma ~= 0 then
for plane = 0, 1 do
local bit = ((plane == 0) and (2) or (4))
local ca, cb = dcCond(S, M, bit, intra)
if cbf(3, ca, cb) == 1 then
local n = block(3, M.dcC, plane * 4, nil, 1)
if n < 0 then
return false
end
M.hasDcC = true
dcBits += bit
end
end
end
local cc = M.cc
for plane = 0, 1 do
local tab = ((plane == 0) and (S.nzcCb) or (S.nzcCr))
local mulc = dq.d4[(((intra) and (2) or (5))) + plane][(((plane == 0) and (M.qpc) or (M.qpc2))) + 1]
for blk = 0, 3 do
local bx, by = blk % 2, blk // 2
local n = 0
if chroma == 2 then
local ca, cb = chromaCond(S, M, tab, bx, by, intra)
if cbf(4, ca, cb) == 1 then
n = block(4, cc, (plane * 4 + blk) * 16, mulc, 16)
if n < 0 then
return false
end
end
end
tab[c0 + by * cw + bx] = ((n > 0) and (1) or (0))
nzc[plane * 4 + blk + 1] = n
end
end
S.dcTab[M.addr + 1] = dcBits
return true
end
local function setQp(S: any, M: any, delta: number): boolean
if delta < -26 or delta > 25 then
return false
end
local qp = (S.qp + delta + 52) % 52
S.qp = qp
M.qp = qp
M.qpc = S.qpcOf[qp + 1]
M.qpc2 = S.qpc2Of[qp + 1]
S.lastQpd = delta ~= 0
return true
end
local function clearCounts(M: any)
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
end
local SUBB_PF = { 0, 1, 2, 3, 1, 1, 2, 2, 3, 3, 1, 2, 3 }
local SUBB_SHAPE = { 0, 0, 0, 0, 1, 2, 1, 2, 1, 2, 3, 3, 3 }
local SUB_N = { 1, 2, 2, 4 }
local SUB_W = { 2, 2, 1, 1 }
local SUB_H = { 2, 1, 2, 1 }
local BPAIR0 = { 1, 2, 1, 2, 1, 2, 3, 3, 3 }
local BPAIR1 = { 1, 2, 2, 1, 3, 3, 1, 2, 3 }
local function intraMb(S: any, M: any, r: Bitstream.Reader, t: number): (boolean, string?)
local addr = M.addr + 1
local w4, g0 = S.w4, M.g0
fillRegion(S, M, S.rgt0, 0, 0, 4, 4, 0)
fillRegion(S, M, S.rgt1, 0, 0, 4, 4, 0)
fillMvd(S, M, S.mvd0, 0, 0, 4, 4, 0, 0)
fillMvd(S, M, S.mvd1, 0, 0, 4, 4, 0, 0)
clearCounts(M)
if t == 25 then
M.kind = K_PCM
-- pcm_alignment_zero_bit and the samples follow the terminate bin
local p = (bitPos() + 7) // 8
if p + 384 > elimit then
return false, "truncated: I_PCM samples"
end
buffer.copy(M.pcm, 0, ebuf, p, 384)
engineInit(ebuf, p + 384, elimit)
M.qp = S.qp
M.qpc, M.qpc2 = 0, 0
M.cbp = 0x2f
fillRegion(S, M, S.nzc, 0, 0, 4, 4, 16)
local cw, c0 = S.cw4, M.c0
for j = 0, 1 do
for i = 0, 1 do
S.nzcCb[c0 + j * cw + i] = 16
S.nzcCr[c0 + j * cw + i] = 16
end
end
S.cbpTab[addr] = 0x2f
S.dcTab[addr] = 7
S.cpmTab[addr] = 0
S.lastQpd = false
Mb.recon(S, M)
return true
end
if t == 0 then
M.kind = K_I4
if S.t8mode and t8Flag(S, M) == 1 then
M.kind = K_I8
M.t8 = 1
end
local ipm = M.ipm
local grid = S.ipm
if M.t8 == 1 then
for i8 = 0, 3 do
local bx, by = (i8 % 2) * 2, (i8 // 2) * 2
local pred = Mb.predMode(S, M, bx, by)
local mode = pred
if decision(68) == 0 then
local rem = decision(69)
rem += decision(69) * 2
rem += decision(69) * 4
mode = ((rem < pred) and (rem) or (rem + 1))
end
ipm[by * 4 + bx + 1] = mode
local g = g0 + by * w4 + bx
grid[g], grid[g + 1], grid[g + w4], grid[g + w4 + 1] = mode, mode, mode, mode
end
else
for i = 0, 15 do
local bx, by = BLK_X[i + 1], BLK_Y[i + 1]
local pred = Mb.predMode(S, M, bx, by)
local mode = pred
if decision(68) == 0 then
local rem = decision(69)
rem += decision(69) * 2
rem += decision(69) * 4
mode = ((rem < pred) and (rem) or (rem + 1))
end
ipm[by * 4 + bx + 1] = mode
grid[g0 + by * w4 + bx] = mode
end
end
M.cpm = chromaPredMode(S, M)
S.cpmTab[addr] = M.cpm
M.cbp = cbpCabac(S, M)
else
M.kind = K_I16
M.i16 = (t - 1) % 4
M.cbp = ((t - 1) // 4) % 3 * 16 + (((t >= 13) and (15) or (0)))
M.cpm = chromaPredMode(S, M)
S.cpmTab[addr] = M.cpm
end
S.cbpTab[addr] = M.cbp
S.kind[addr] = M.kind
if M.cbp > 0 or M.kind == K_I16 then
if not setQp(S, M, qpDelta(S)) then
return false, "mb_qp_delta out of range"
end
else
setQp(S, M, 0)
end
S.t8Tab[addr] = M.t8
if not residuals(S, M) then
return false, "bad residual"
end
Mb.recon(S, M)
return true
end
local function interMb(S: any, M: any, r: Bitstream.Reader, t: number, isB: boolean): (boolean, string?)
clearCounts(M)
local n0, n1 = S.n0, S.n1
local pf, sub, dir, ref0, ref1, mvd0, mvd1 = M.pf, M.sub, M.dir, M.ref0, M.ref1, M.mvd0, M.mvd1
local rgt0, rgt1, g0, g1 = S.rgt0, S.rgt1, S.mvd0, S.mvd1
for i = 1, 4 do
dir[i] = false
ref0[i], ref1[i] = 0, 0
pf[i] = 0
end
for i = 1, 32 do
mvd0[i], mvd1[i] = 0, 0
end
local noSmall = true
local addr = M.addr + 1
if isB and t == 0 then
M.kind = K_BDIRECT
M.part = 0
noSmall = S.direct8x8
fillRegion(S, M, rgt0, 0, 0, 4, 4, 0)
fillRegion(S, M, rgt1, 0, 0, 4, 4, 0)
fillMvd(S, M, g0, 0, 0, 4, 4, 0, 0)
fillMvd(S, M, g1, 0, 0, 4, 4, 0, 0)
elseif (not isB and t < 3) or (isB and t <= 21) then
M.kind = ((isB) and (K_B) or (K_P))
if not isB then
M.part = t
pf[1], pf[2] = 1, 1
elseif t <= 3 then
M.part = 0
pf[1] = t
else
M.part = ((t % 2 == 0) and (1) or (2))
local k = (t - 4) // 2 + 1
pf[1], pf[2] = BPAIR0[k], BPAIR1[k]
end
local part = M.part
local n = ((part == 0) and (1) or (2))
for l = 0, 1 do
local rgt = ((l == 0) and (rgt0) or (rgt1))
local nr = ((l == 0) and (n0) or (n1))
local refs = ((l == 0) and (ref0) or (ref1))
for p = 1, n do
local x, y, w, h = 0, 0, 4, 4
if part == 1 then
y, h = (p - 1) * 2, 2
elseif part == 2 then
x, w = (p - 1) * 2, 2
end
local uses = pf[p] >= 2
if l == 0 then
uses = pf[p] % 2 == 1
end
local v = 0
if uses and nr > 1 then
v = refIdx(S, M, rgt, x, y)
if v < 0 or v >= nr then
return false, "ref_idx out of range"
end
end
refs[p] = v
fillRegion(S, M, rgt, x, y, w, h, ((uses and v > 0) and (1) or (0)))
end
end
for l = 0, 1 do
local grid = ((l == 0) and (g0) or (g1))
local md = ((l == 0) and (mvd0) or (mvd1))
for p = 1, n do
local x, y, w, h = 0, 0, 4, 4
if part == 1 then
y, h = (p - 1) * 2, 2
elseif part == 2 then
x, w = (p - 1) * 2, 2
end
local uses = pf[p] >= 2
if l == 0 then
uses = pf[p] % 2 == 1
end
local mx, my = 0, 0
if uses then
mx = mvd(S, M, grid, x, y, 0)
my = mvd(S, M, grid, x, y, 1)
end
md[2 * p - 1], md[2 * p] = mx, my
fillMvd(S, M, grid, x, y, w, h, ((mx < 0) and (-mx) or (mx)), ((my < 0) and (-my) or (my)))
end
end
else
-- P_8x8 (3) / P_8x8ref0 (4) / B_8x8 (22)
M.kind = ((isB) and (K_B) or (K_P))
M.part = 3
for i = 1, 4 do
if isB then
local s = bSubType()
pf[i] = SUBB_PF[s + 1]
sub[i] = SUBB_SHAPE[s + 1]
dir[i] = s == 0
if s == 0 then
if not S.direct8x8 then
noSmall = false
end
elseif SUB_N[sub[i] + 1] > 1 then
noSmall = false
end
else
local s = pSubType()
pf[i] = 1
sub[i] = s
if s > 0 then
noSmall = false
end
end
end
for l = 0, 1 do
local rgt = ((l == 0) and (rgt0) or (rgt1))
local nr = ((l == 0) and (n0) or (n1))
local refs = ((l == 0) and (ref0) or (ref1))
for i = 1, 4 do
local x, y = ((i - 1) % 2) * 2, ((i - 1) // 2) * 2
local uses = pf[i] >= 2
if l == 0 then
uses = pf[i] % 2 == 1
end
uses = uses and not dir[i]
local v = 0
if uses and nr > 1 then
v = refIdx(S, M, rgt, x, y)
if v < 0 or v >= nr then
return false, "ref_idx out of range"
end
end
refs[i] = v
fillRegion(S, M, rgt, x, y, 2, 2, ((uses and v > 0) and (1) or (0)))
end
end
for l = 0, 1 do
local grid = ((l == 0) and (g0) or (g1))
local md = ((l == 0) and (mvd0) or (mvd1))
for i = 1, 4 do
local x0, y0 = ((i - 1) % 2) * 2, ((i - 1) // 2) * 2
local uses = pf[i] >= 2
if l == 0 then
uses = pf[i] % 2 == 1
end
uses = uses and not dir[i]
if not uses then
fillMvd(S, M, grid, x0, y0, 2, 2, 0, 0)
else
local s = sub[i]
local w, h = SUB_W[s + 1], SUB_H[s + 1]
for k = 0, SUB_N[s + 1] - 1 do
local x, y = x0, y0
if s == 1 then
y += k
elseif s == 2 then
x += k
elseif s == 3 then
x += k % 2
y += k // 2
end
local mx = mvd(S, M, grid, x, y, 0)
local my = mvd(S, M, grid, x, y, 1)
local p = (i - 1) * 4 + k + 1
md[2 * p - 1], md[2 * p] = mx, my
fillMvd(S, M, grid, x, y, w, h, ((mx < 0) and (-mx) or (mx)), ((my < 0) and (-my) or (my)))
end
end
end
end
end
S.kind[addr] = M.kind
M.cbp = cbpCabac(S, M)
S.cbpTab[addr] = M.cbp
S.cpmTab[addr] = 0
M.t8 = 0
if M.cbp % 16 ~= 0 and S.t8mode and noSmall then
M.t8 = t8Flag(S, M)
end
S.t8Tab[addr] = M.t8
if M.cbp > 0 then
if not setQp(S, M, qpDelta(S)) then
return false, "mb_qp_delta out of range"
end
else
setQp(S, M, 0)
end
if not residuals(S, M) then
return false, "bad residual"
end
Mb.recon(S, M)
return true
end
local function macroblock(S: any, M: any, r: Bitstream.Reader): (boolean, string?)
local st = S.type
if st == 2 then
return intraMb(S, M, r, intraType(S, M, 3))
elseif st == 0 then
local t = pMbType()
if t < 0 then
return intraMb(S, M, r, intraType(S, M, 17))
end
return interMb(S, M, r, t, false)
end
local t = bMbType(S, M)
if t < 0 then
return intraMb(S, M, r, intraType(S, M, 32))
end
return interMb(S, M, r, t, true)
end
function Cabac.slice(S: any, M: any, r: Bitstream.Reader): (boolean, string?)
local p = (r.pos + 7) // 8
local limit = r.size // 8
if p >= limit then
return false, "truncated: slice data"
end
engineInit(r.buf, p, limit)
initContexts(S.qp, ((S.type == 2) and (0) or (S.cabacInit + 1)))
S.lastQpd = false
local addr = S.firstMb
local total = S.mbW * S.mbH
local st = S.type
local skipKind = ((st == 1) and (K_BSKIP) or (K_PSKIP))
local hook = S.hook
local every = Mb.YIELD_MBS
while true do
if addr >= total then
return false, "slice data past the end of the picture"
end
Mb.start(S, M, addr)
if st ~= 2 and mbSkipFlag(S, M) == 1 then
Mb.skip(S, M, skipKind)
S.lastQpd = false
else
local ok, err = macroblock(S, M, r)
if not ok then
return false, err
end
end
addr += 1
if hook and addr % every == 0 then
pcall(hook) -- (may yield: the engine state lives in this module's locals, see Decoder Options.yield)
end
if epos > elimit + 8 then
return false, "truncated: slice data"
end
if terminate() == 1 then
break
end
end
return true
end
return Cabac