1988 lines
60 KiB
Luau
1988 lines
60 KiB
Luau
--!strict
|
|
--!native
|
|
--!optimize 2
|
|
-- codecs/Vp8/Decode: the VP8 frame decoder (RFC 6386): the boolean entropy decoder, frame / segment / filter /
|
|
-- quantizer headers, token probability updates, intra modes, residual tokens, the inverse WHT and DCT, intra
|
|
-- prediction (16x16, 4x4, chroma), and the normal and simple loop filters: a port of libwebp 1.6 src/dec/vp8_dec.c,
|
|
-- tree_dec.c, quant_dec.c, frame_dec.c, src/dsp/dec.c and src/utils/bit_reader* (BSD-3-Clause,
|
|
-- licenses/libwebp-LICENSE.txt). Inter frames after libvpx 1.17 vp8/decoder/decodeframe.c, decodemv.c and
|
|
-- vp8/common/findnearmv.c, vp8_loopfilter.c (BSD-3-Clause, licenses/libvpx-LICENSE.txt): reference updates and sign
|
|
-- bias, the frame context saved around frames that do not refresh it, persistent segment maps and loop filter deltas,
|
|
-- inter modes from the near vectors, motion vectors, split macroblocks, intra macroblocks in inter frames, per
|
|
-- reference / mode loop filter levels; the prediction itself is codecs/Vp8/Predict. Bit-exact with libvpx / ffmpeg
|
|
-- (and with libwebp on key frames). Where libvpx and ffmpeg disagree on streams no encoder writes (an intermediate
|
|
-- clamp of segment filter levels, copying golden to altref and altref to golden in one frame), this follows ffmpeg.
|
|
--
|
|
-- The decoder state (Decode.new) keeps what persists between frames: the frame context, segment / filter headers, the
|
|
-- segment map, the last / golden / altref frames. Never throws: bad data returns nil, "message" ("truncated: ..." when
|
|
-- the data ends early).
|
|
local Tables = require("@formats/codecs/Vp8/Tables")
|
|
local Predict = require("@formats/codecs/Vp8/Predict")
|
|
|
|
local band, bor, rshift, lshift = bit32.band, bit32.bor, bit32.rshift, bit32.lshift
|
|
local readu8, writeu8, readi16, writei16 = buffer.readu8, buffer.writeu8, buffer.readi16, buffer.writei16
|
|
local readu32, writeu32 = buffer.readu32, buffer.writeu32
|
|
local bfill, bcopy, bcreate = buffer.fill, buffer.copy, buffer.create
|
|
local abs = math.abs
|
|
|
|
local COEFF_PROBA0 = Tables.COEFF_PROBA0
|
|
local COEFF_UPDATE_PROBA = Tables.COEFF_UPDATE_PROBA
|
|
local BMODES_PROBA = Tables.BMODES_PROBA
|
|
local DC_TABLE, AC_TABLE = Tables.DC_TABLE, Tables.AC_TABLE
|
|
local BANDS, ZIGZAG = Tables.BANDS, Tables.ZIGZAG
|
|
local CAT = { Tables.CAT3, Tables.CAT4, Tables.CAT5, Tables.CAT6 }
|
|
|
|
local Decode = {}
|
|
|
|
-- ------------------------------------------------------------------------------------------------ boolean decoder
|
|
-- libwebp's VP8BitReader: `range` is the range minus one (127..254), `value` holds the unread bits, `bits` is the
|
|
-- position of the 8-bit window in `value` (< 0: load more). Bytes past the end read as zero once and set eof.
|
|
type Br = { buf: buffer, pos: number, limit: number, value: number, bits: number, range: number, eof: boolean }
|
|
|
|
-- SHIFT[r + 1]: the normalisation shift of a range r (1..255) = 7 - floor(log2 r)
|
|
local SHIFT = table.create(256, 0)
|
|
for r = 1, 255 do
|
|
local s, v = 0, r
|
|
while v < 128 do
|
|
v *= 2
|
|
s += 1
|
|
end
|
|
SHIFT[r + 1] = s
|
|
end
|
|
|
|
local function load(br: Br)
|
|
local p = br.pos
|
|
if p + 3 <= br.limit then
|
|
local b = br.buf
|
|
br.value = br.value * 16777216 + readu8(b, p) * 65536 + readu8(b, p + 1) * 256 + readu8(b, p + 2)
|
|
br.pos = p + 3
|
|
br.bits += 24
|
|
elseif p < br.limit then
|
|
br.value = br.value * 256 + readu8(br.buf, p)
|
|
br.pos = p + 1
|
|
br.bits += 8
|
|
elseif not br.eof then
|
|
br.value *= 256
|
|
br.bits += 8
|
|
br.eof = true
|
|
else
|
|
br.bits = 0
|
|
end
|
|
end
|
|
|
|
local function newBr(buf: buffer, off: number, len: number): Br
|
|
local br = { buf = buf, pos = off, limit = off + len, value = 0, bits = -8, range = 254, eof = false }
|
|
load(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)
|
|
end
|
|
local range = br.range
|
|
local pos = br.bits
|
|
local split = rshift(range * prob, 8)
|
|
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
|
|
|
|
local function getValue(br: Br, n: number): number
|
|
local v = 0
|
|
for i = n - 1, 0, -1 do
|
|
v += getBit(br, 0x80) * 2 ^ i
|
|
end
|
|
return v
|
|
end
|
|
|
|
local function getSignedValue(br: Br, n: number): number
|
|
local v = getValue(br, n)
|
|
return if getBit(br, 0x80) == 1 then -v else v
|
|
end
|
|
|
|
-- ------------------------------------------------------------------------------------------------ state
|
|
export type Quant = { y1dc: number, y1ac: number, y2dc: number, y2ac: number, uvdc: number, uvac: number }
|
|
|
|
-- a decoded frame / reference: the macroblock aligned planes with a Predict.BORDER pixel border
|
|
export type Ref = Predict.Planes & { extended: boolean }
|
|
|
|
export type State = {
|
|
-- the frame context (saved and restored around frames that do not refresh it)
|
|
proba: { number }, -- 4 * 8 * 3 * 11 token probabilities
|
|
ymodeProb: { number }, -- 4
|
|
uvmodeProb: { number }, -- 3
|
|
mvProb: { number }, -- 2 * 19
|
|
-- persistent between frames (reset by key frames)
|
|
segProba: { number }, -- 3
|
|
useSegment: boolean,
|
|
updateMap: boolean,
|
|
absoluteDelta: boolean,
|
|
segQuant: { number }, -- 4
|
|
segFilter: { number }, -- 4
|
|
refLfDelta: { number }, -- 4: intra, last, golden, altref
|
|
modeLfDelta: { number }, -- 4: B_PRED, zero, nearest / near / new, split
|
|
signBias: { number }, -- 4 (by reference frame + 1)
|
|
segMap: { number }, -- the segment of every macroblock
|
|
width: number,
|
|
height: number,
|
|
mbw: number,
|
|
mbh: number,
|
|
hasKey: boolean, -- a key frame was decoded (inter frames need one)
|
|
last: Ref?,
|
|
golden: Ref?,
|
|
alt: Ref?,
|
|
pool: { Ref },
|
|
-- the mode info of the current inter frame, (mbw + 1) x (mbh + 1) with a zero top row and left column
|
|
miRef: { number },
|
|
miMode: { number },
|
|
miMvR: { number },
|
|
miMvC: { number },
|
|
miSubR: { number }, -- 16 per macroblock
|
|
miSubC: { number },
|
|
miN: number, -- the size of the mode info arrays (allocated by the first inter frame)
|
|
pred: buffer?, -- the inter predictions of a row
|
|
}
|
|
|
|
function Decode.new(): State
|
|
return {
|
|
proba = table.clone(COEFF_PROBA0),
|
|
ymodeProb = table.clone(Tables.YMODE_PROB),
|
|
uvmodeProb = table.clone(Tables.UVMODE_PROB),
|
|
mvProb = table.clone(Tables.MV_DEFAULT),
|
|
segProba = { 255, 255, 255 },
|
|
useSegment = false,
|
|
updateMap = false,
|
|
absoluteDelta = false,
|
|
segQuant = { 0, 0, 0, 0 },
|
|
segFilter = { 0, 0, 0, 0 },
|
|
refLfDelta = { 0, 0, 0, 0 },
|
|
modeLfDelta = { 0, 0, 0, 0 },
|
|
signBias = { 0, 0, 0, 0 },
|
|
segMap = {},
|
|
width = 0,
|
|
height = 0,
|
|
mbw = 0,
|
|
mbh = 0,
|
|
hasKey = false,
|
|
last = nil,
|
|
golden = nil,
|
|
alt = nil,
|
|
pool = {},
|
|
miRef = {},
|
|
miMode = {},
|
|
miMvR = {},
|
|
miMvC = {},
|
|
miSubR = {},
|
|
miSubC = {},
|
|
miN = 0,
|
|
pred = nil,
|
|
}
|
|
end
|
|
|
|
-- reset(state): drops the references; the next frame must be a key frame
|
|
function Decode.reset(st: State)
|
|
st.hasKey = false
|
|
st.last, st.golden, st.alt = nil, nil, nil
|
|
end
|
|
|
|
-- header(buf, off, len) -> { keyFrame, profile, show, partitionLength, width, height, xscale, yscale } | nil, msg
|
|
function Decode.header(buf: buffer, off: number, len: number): (any, string?)
|
|
if len < 3 then
|
|
return nil, "truncated: frame tag"
|
|
end
|
|
local bits = readu8(buf, off) + readu8(buf, off + 1) * 256 + readu8(buf, off + 2) * 65536
|
|
local h = {
|
|
keyFrame = band(bits, 1) == 0,
|
|
profile = band(rshift(bits, 1), 7),
|
|
show = band(rshift(bits, 4), 1) == 1,
|
|
partitionLength = rshift(bits, 5),
|
|
width = 0,
|
|
height = 0,
|
|
xscale = 0,
|
|
yscale = 0,
|
|
}
|
|
if h.profile > 3 then
|
|
return nil, "bad profile " .. h.profile
|
|
end
|
|
if h.keyFrame then
|
|
if len < 10 then
|
|
return nil, "truncated: key frame header"
|
|
end
|
|
if readu8(buf, off + 3) ~= 0x9d or readu8(buf, off + 4) ~= 0x01 or readu8(buf, off + 5) ~= 0x2a then
|
|
return nil, "bad VP8 start code"
|
|
end
|
|
local w = readu8(buf, off + 6) + readu8(buf, off + 7) * 256
|
|
local ht = readu8(buf, off + 8) + readu8(buf, off + 9) * 256
|
|
h.width, h.xscale = band(w, 0x3fff), rshift(w, 14)
|
|
h.height, h.yscale = band(ht, 0x3fff), rshift(ht, 14)
|
|
end
|
|
return h, nil
|
|
end
|
|
|
|
-- ------------------------------------------------------------------------------------------------ residuals
|
|
local function getLargeValue(br: Br, P: { number }, p: number): number
|
|
local v
|
|
if getBit(br, P[p + 4]) == 0 then
|
|
if getBit(br, P[p + 5]) == 0 then
|
|
v = 2
|
|
else
|
|
v = 3 + getBit(br, P[p + 6])
|
|
end
|
|
else
|
|
if getBit(br, P[p + 7]) == 0 then
|
|
if getBit(br, P[p + 8]) == 0 then
|
|
v = 5 + getBit(br, 159)
|
|
else
|
|
v = 7 + 2 * getBit(br, 165)
|
|
v += getBit(br, 145)
|
|
end
|
|
else
|
|
local bit1 = getBit(br, P[p + 9])
|
|
local bit0 = getBit(br, P[p + 10 + bit1])
|
|
local cat = 2 * bit1 + bit0
|
|
v = 0
|
|
for _, pr in CAT[cat + 1] do
|
|
v = v + v + getBit(br, pr)
|
|
end
|
|
v += 3 + lshift(8, cat)
|
|
end
|
|
end
|
|
return v
|
|
end
|
|
|
|
-- getCoeffs(br, P, t, ctx, dqDc, dqAc, n, out, o) -> the position of the last non-zero coefficient + 1. Writes the
|
|
-- dequantized coefficients (int16, as libwebp stores them) into out at byte offset o, in raster order. The boolean
|
|
-- decoder is inlined (the hot loop of lossy decoding); getLargeValue goes through br.
|
|
local function getCoeffs(br: Br, P: { number }, t: number, ctx: number, dqDc: number, dqAc: number, n: number, out: buffer, o: number): number
|
|
local tb = t * 8
|
|
local p = ((tb + BANDS[n + 1]) * 3 + ctx) * 11
|
|
local buf, limit = br.buf, br.limit
|
|
local value, bits, range, pos = br.value, br.bits, br.range, br.pos
|
|
while n < 16 do
|
|
do
|
|
if bits < 0 then
|
|
if pos + 3 <= limit then
|
|
value = value * 16777216 + readu8(buf, pos) * 65536 + readu8(buf, pos + 1) * 256 + readu8(buf, pos + 2)
|
|
pos += 3
|
|
bits += 24
|
|
else
|
|
br.value, br.bits, br.pos = value, bits, pos
|
|
load(br)
|
|
value, bits, pos = br.value, br.bits, br.pos
|
|
end
|
|
end
|
|
local split = rshift(range * P[p + 1], 8)
|
|
local more = rshift(value, bits) > split
|
|
if more then
|
|
range -= split
|
|
value -= lshift(split + 1, bits)
|
|
else
|
|
range = split + 1
|
|
end
|
|
local sh = SHIFT[range + 1]
|
|
range = lshift(range, sh) - 1
|
|
bits -= sh
|
|
if not more then
|
|
br.value, br.bits, br.range, br.pos = value, bits, range, pos
|
|
return n
|
|
end
|
|
end
|
|
while true do
|
|
if bits < 0 then
|
|
if pos + 3 <= limit then
|
|
value = value * 16777216 + readu8(buf, pos) * 65536 + readu8(buf, pos + 1) * 256 + readu8(buf, pos + 2)
|
|
pos += 3
|
|
bits += 24
|
|
else
|
|
br.value, br.bits, br.pos = value, bits, pos
|
|
load(br)
|
|
value, bits, pos = br.value, br.bits, br.pos
|
|
end
|
|
end
|
|
local split = rshift(range * P[p + 2], 8)
|
|
local nonzero = rshift(value, bits) > split
|
|
if nonzero then
|
|
range -= split
|
|
value -= lshift(split + 1, bits)
|
|
else
|
|
range = split + 1
|
|
end
|
|
local sh = SHIFT[range + 1]
|
|
range = lshift(range, sh) - 1
|
|
bits -= sh
|
|
if nonzero then
|
|
break
|
|
end
|
|
n += 1
|
|
if n == 16 then
|
|
br.value, br.bits, br.range, br.pos = value, bits, range, pos
|
|
return 16
|
|
end
|
|
p = (tb + BANDS[n + 1]) * 33
|
|
end
|
|
local nb = (tb + BANDS[n + 2]) * 3
|
|
local v
|
|
do
|
|
if bits < 0 then
|
|
if pos + 3 <= limit then
|
|
value = value * 16777216 + readu8(buf, pos) * 65536 + readu8(buf, pos + 1) * 256 + readu8(buf, pos + 2)
|
|
pos += 3
|
|
bits += 24
|
|
else
|
|
br.value, br.bits, br.pos = value, bits, pos
|
|
load(br)
|
|
value, bits, pos = br.value, br.bits, br.pos
|
|
end
|
|
end
|
|
local split = rshift(range * P[p + 3], 8)
|
|
local large = rshift(value, bits) > split
|
|
if large then
|
|
range -= split
|
|
value -= lshift(split + 1, bits)
|
|
else
|
|
range = split + 1
|
|
end
|
|
local sh = SHIFT[range + 1]
|
|
range = lshift(range, sh) - 1
|
|
bits -= sh
|
|
if not large then
|
|
v = 1
|
|
p = (nb + 1) * 11
|
|
else
|
|
br.value, br.bits, br.range, br.pos = value, bits, range, pos
|
|
v = getLargeValue(br, P, p)
|
|
value, bits, range, pos = br.value, br.bits, br.range, br.pos
|
|
p = (nb + 2) * 11
|
|
end
|
|
end
|
|
do
|
|
if bits < 0 then
|
|
if pos + 3 <= limit then
|
|
value = value * 16777216 + readu8(buf, pos) * 65536 + readu8(buf, pos + 1) * 256 + readu8(buf, pos + 2)
|
|
pos += 3
|
|
bits += 24
|
|
else
|
|
br.value, br.bits, br.pos = value, bits, pos
|
|
load(br)
|
|
value, bits, pos = br.value, br.bits, br.pos
|
|
end
|
|
end
|
|
local split = rshift(range * 0x80, 8)
|
|
local neg = rshift(value, bits) > split
|
|
if neg then
|
|
range -= split
|
|
value -= lshift(split + 1, bits)
|
|
else
|
|
range = split + 1
|
|
end
|
|
local sh = SHIFT[range + 1]
|
|
range = lshift(range, sh) - 1
|
|
bits -= sh
|
|
if neg then
|
|
v = -v
|
|
end
|
|
end
|
|
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
|
|
return 16
|
|
end
|
|
|
|
local function nzCode(acc: number, nz: number, dcNz: number): number
|
|
return acc * 4 + (if nz > 3 then 3 elseif nz > 1 then 2 else dcNz)
|
|
end
|
|
|
|
-- the inverse Walsh-Hadamard transform of the 16 Y2 coefficients into the DC of the 16 luma blocks
|
|
local whtTmp = table.create(16, 0)
|
|
local function transformWHT(inp: buffer, out: buffer, o: number)
|
|
for i = 0, 3 do
|
|
local i0, i4, i8, i12 = readi16(inp, i * 2), readi16(inp, (4 + i) * 2), readi16(inp, (8 + i) * 2), readi16(inp, (12 + i) * 2)
|
|
local a0 = i0 + i12
|
|
local a1 = i4 + i8
|
|
local a2 = i4 - i8
|
|
local a3 = i0 - i12
|
|
whtTmp[1 + i] = a0 + a1
|
|
whtTmp[9 + i] = a0 - a1
|
|
whtTmp[5 + i] = a3 + a2
|
|
whtTmp[13 + i] = a3 - a2
|
|
end
|
|
for i = 0, 3 do
|
|
local dc = whtTmp[1 + i * 4] + 3
|
|
local a0 = dc + whtTmp[4 + i * 4]
|
|
local a1 = whtTmp[2 + i * 4] + whtTmp[3 + i * 4]
|
|
local a2 = whtTmp[2 + i * 4] - whtTmp[3 + i * 4]
|
|
local a3 = dc - whtTmp[4 + i * 4]
|
|
local base = o + i * 128
|
|
writei16(out, base, (a0 + a1) // 8)
|
|
writei16(out, base + 32, (a3 + a2) // 8)
|
|
writei16(out, base + 64, (a0 - a1) // 8)
|
|
writei16(out, base + 96, (a3 - a2) // 8)
|
|
end
|
|
end
|
|
|
|
-- ------------------------------------------------------------------------------------------------ reconstruction
|
|
-- the work buffer of libwebp (yuv_b): BPS = 32, Y at row 1 column 8, U and V below it side by side
|
|
local BPS = 32
|
|
local Y_OFF = BPS + 8
|
|
local U_OFF = Y_OFF + BPS * 16 + BPS
|
|
local V_OFF = U_OFF + 16
|
|
local YUV_SIZE = BPS * 17 + BPS * 9
|
|
|
|
local function clip8(v: number): number
|
|
return if v < 0 then 0 elseif v > 255 then 255 else v
|
|
end
|
|
|
|
-- the inverse DCT of one block (libwebp TransformOne) added to dst
|
|
local C1, C2 = 20091, 35468
|
|
local idctTmp = table.create(16, 0)
|
|
local function mul1(a: number): number
|
|
return ((a * C1) // 65536) + a
|
|
end
|
|
local function mul2(a: number): number
|
|
return (a * C2) // 65536
|
|
end
|
|
local function transformOne(c: buffer, o: number, b: buffer, dst: number)
|
|
for i = 0, 3 do
|
|
local i0, i4, i8, i12 = readi16(c, o + i * 2), readi16(c, o + (4 + i) * 2), readi16(c, o + (8 + i) * 2), readi16(c, o + (12 + i) * 2)
|
|
local a = i0 + i8
|
|
local bb = i0 - i8
|
|
local cc = mul2(i4) - mul1(i12)
|
|
local d = mul1(i4) + mul2(i12)
|
|
idctTmp[i * 4 + 1] = a + d
|
|
idctTmp[i * 4 + 2] = bb + cc
|
|
idctTmp[i * 4 + 3] = bb - cc
|
|
idctTmp[i * 4 + 4] = a - d
|
|
end
|
|
for i = 0, 3 do
|
|
local dc = idctTmp[i + 1] + 4
|
|
local a = dc + idctTmp[i + 9]
|
|
local bb = dc - idctTmp[i + 9]
|
|
local cc = mul2(idctTmp[i + 5]) - mul1(idctTmp[i + 13])
|
|
local d = mul1(idctTmp[i + 5]) + mul2(idctTmp[i + 13])
|
|
local row = dst + i * BPS
|
|
writeu8(b, row, clip8(readu8(b, row) + (a + d) // 8))
|
|
writeu8(b, row + 1, clip8(readu8(b, row + 1) + (bb + cc) // 8))
|
|
writeu8(b, row + 2, clip8(readu8(b, row + 2) + (bb - cc) // 8))
|
|
writeu8(b, row + 3, clip8(readu8(b, row + 3) + (a - d) // 8))
|
|
end
|
|
end
|
|
|
|
local function transformDC(c: buffer, o: number, b: buffer, dst: number)
|
|
local dc = (readi16(c, o) + 4) // 8
|
|
for j = 0, 3 do
|
|
local row = dst + j * BPS
|
|
for i = row, row + 3 do
|
|
writeu8(b, i, clip8(readu8(b, i) + dc))
|
|
end
|
|
end
|
|
end
|
|
|
|
-- code: 3 = full transform, 2 = only the first three coefficients (the same result as the full one), 1 = DC only
|
|
local function doTransform(code: number, c: buffer, o: number, b: buffer, dst: number)
|
|
if code >= 2 then
|
|
transformOne(c, o, b, dst)
|
|
elseif code == 1 then
|
|
transformDC(c, o, b, dst)
|
|
end
|
|
end
|
|
|
|
local function doUVTransform(bits: number, c: buffer, o: number, b: buffer, dst: number)
|
|
if band(bits, 0xff) ~= 0 then
|
|
if band(bits, 0xaa) ~= 0 then
|
|
transformOne(c, o, b, dst)
|
|
transformOne(c, o + 32, b, dst + 4)
|
|
transformOne(c, o + 64, b, dst + 4 * BPS)
|
|
transformOne(c, o + 96, b, dst + 4 * BPS + 4)
|
|
else
|
|
if readi16(c, o) ~= 0 then
|
|
transformDC(c, o, b, dst)
|
|
end
|
|
if readi16(c, o + 32) ~= 0 then
|
|
transformDC(c, o + 32, b, dst + 4)
|
|
end
|
|
if readi16(c, o + 64) ~= 0 then
|
|
transformDC(c, o + 64, b, dst + 4 * BPS)
|
|
end
|
|
if readi16(c, o + 96) ~= 0 then
|
|
transformDC(c, o + 96, b, dst + 4 * BPS + 4)
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
-- prediction: modes 0 DC, 1 TM, 2 VE, 3 HE; 4 DC no top, 5 DC no left, 6 DC no top-left (16x16 and chroma)
|
|
local function fill(b: buffer, dst: number, size: number, v: number)
|
|
for j = 0, size - 1 do
|
|
local o = dst + j * BPS
|
|
for i = o, o + size - 4, 4 do
|
|
writeu32(b, i, v * 0x01010101)
|
|
end
|
|
end
|
|
end
|
|
|
|
local function trueMotion(b: buffer, dst: number, size: number)
|
|
local tl = readu8(b, dst - BPS - 1)
|
|
for y = 0, size - 1 do
|
|
local row = dst + y * BPS
|
|
local l = readu8(b, row - 1) - tl
|
|
for x = 0, size - 1 do
|
|
writeu8(b, row + x, clip8(readu8(b, dst - BPS + x) + l))
|
|
end
|
|
end
|
|
end
|
|
|
|
local function predictBlock(b: buffer, dst: number, size: number, mode: number)
|
|
local shift = if size == 16 then 5 else 4
|
|
if mode == 0 then
|
|
local dc = size
|
|
for j = 0, size - 1 do
|
|
dc += readu8(b, dst - 1 + j * BPS) + readu8(b, dst + j - BPS)
|
|
end
|
|
fill(b, dst, size, rshift(dc, shift))
|
|
elseif mode == 1 then
|
|
trueMotion(b, dst, size)
|
|
elseif mode == 2 then
|
|
for j = 0, size - 1 do
|
|
local o = dst + j * BPS
|
|
for i = 0, size - 4, 4 do
|
|
writeu32(b, o + i, readu32(b, dst - BPS + i))
|
|
end
|
|
end
|
|
elseif mode == 3 then
|
|
for j = 0, size - 1 do
|
|
local o = dst + j * BPS
|
|
local v = readu8(b, o - 1) * 0x01010101
|
|
for i = o, o + size - 4, 4 do
|
|
writeu32(b, i, v)
|
|
end
|
|
end
|
|
elseif mode == 4 then -- no top: left samples
|
|
local dc = size // 2
|
|
for j = 0, size - 1 do
|
|
dc += readu8(b, dst - 1 + j * BPS)
|
|
end
|
|
fill(b, dst, size, rshift(dc, shift - 1))
|
|
elseif mode == 5 then -- no left: top samples
|
|
local dc = size // 2
|
|
for j = 0, size - 1 do
|
|
dc += readu8(b, dst + j - BPS)
|
|
end
|
|
fill(b, dst, size, rshift(dc, shift - 1))
|
|
else
|
|
fill(b, dst, size, 0x80)
|
|
end
|
|
end
|
|
|
|
local function avg3(a: number, b: number, c: number): number
|
|
return rshift(a + 2 * b + c + 2, 2)
|
|
end
|
|
local function avg2(a: number, b: number): number
|
|
return rshift(a + b + 1, 1)
|
|
end
|
|
|
|
-- 4x4 prediction (libwebp VP8PredLuma4): 0 DC, 1 TM, 2 VE, 3 HE, 4 RD, 5 VR, 6 LD, 7 VL, 8 HD, 9 HU
|
|
local function predict4(b: buffer, dst: number, mode: number)
|
|
if mode == 0 then
|
|
local dc = 4
|
|
for i = 0, 3 do
|
|
dc += readu8(b, dst + i - BPS) + readu8(b, dst - 1 + i * BPS)
|
|
end
|
|
fill(b, dst, 4, rshift(dc, 3))
|
|
elseif mode == 1 then
|
|
trueMotion(b, dst, 4)
|
|
elseif mode == 2 then
|
|
local t = dst - BPS
|
|
local x, a, bb, c, d, e = readu8(b, t - 1), readu8(b, t), readu8(b, t + 1), readu8(b, t + 2), readu8(b, t + 3), readu8(b, t + 4)
|
|
local v0, v1, v2, v3 = avg3(x, a, bb), avg3(a, bb, c), avg3(bb, c, d), avg3(c, d, e)
|
|
for j = 0, 3 do
|
|
writeu8(b, dst + j * BPS + 0, v0)
|
|
writeu8(b, dst + j * BPS + 1, v1)
|
|
writeu8(b, dst + j * BPS + 2, v2)
|
|
writeu8(b, dst + j * BPS + 3, v3)
|
|
end
|
|
elseif mode == 3 then
|
|
local A, B, C, D = readu8(b, dst - 1 - BPS), readu8(b, dst - 1), readu8(b, dst - 1 + BPS), readu8(b, dst - 1 + 2 * BPS)
|
|
local E = readu8(b, dst - 1 + 3 * BPS)
|
|
writeu32(b, dst, avg3(A, B, C) * 0x01010101)
|
|
writeu32(b, dst + BPS, avg3(B, C, D) * 0x01010101)
|
|
writeu32(b, dst + 2 * BPS, avg3(C, D, E) * 0x01010101)
|
|
writeu32(b, dst + 3 * BPS, avg3(D, E, E) * 0x01010101)
|
|
else
|
|
local I, J, K, L = readu8(b, dst - 1), readu8(b, dst - 1 + BPS), readu8(b, dst - 1 + 2 * BPS), readu8(b, dst - 1 + 3 * BPS)
|
|
local t = dst - BPS
|
|
local X, A, B, C, D = readu8(b, t - 1), readu8(b, t), readu8(b, t + 1), readu8(b, t + 2), readu8(b, t + 3)
|
|
local E, F, G, H = readu8(b, t + 4), readu8(b, t + 5), readu8(b, t + 6), readu8(b, t + 7)
|
|
if mode == 4 then -- RD
|
|
writeu8(b, dst + 96, avg3(J, K, L))
|
|
local v = avg3(I, J, K)
|
|
writeu8(b, dst + 97, v)
|
|
writeu8(b, dst + 64, v)
|
|
v = avg3(X, I, J)
|
|
writeu8(b, dst + 98, v)
|
|
writeu8(b, dst + 65, v)
|
|
writeu8(b, dst + 32, v)
|
|
v = avg3(A, X, I)
|
|
writeu8(b, dst + 99, v)
|
|
writeu8(b, dst + 66, v)
|
|
writeu8(b, dst + 33, v)
|
|
writeu8(b, dst + 0, v)
|
|
v = avg3(B, A, X)
|
|
writeu8(b, dst + 67, v)
|
|
writeu8(b, dst + 34, v)
|
|
writeu8(b, dst + 1, v)
|
|
v = avg3(C, B, A)
|
|
writeu8(b, dst + 35, v)
|
|
writeu8(b, dst + 2, v)
|
|
writeu8(b, dst + 3, avg3(D, C, B))
|
|
elseif mode == 5 then -- VR
|
|
local v = avg2(X, A)
|
|
writeu8(b, dst + 0, v)
|
|
writeu8(b, dst + 65, v)
|
|
v = avg2(A, B)
|
|
writeu8(b, dst + 1, v)
|
|
writeu8(b, dst + 66, v)
|
|
v = avg2(B, C)
|
|
writeu8(b, dst + 2, v)
|
|
writeu8(b, dst + 67, v)
|
|
writeu8(b, dst + 3, avg2(C, D))
|
|
writeu8(b, dst + 96, avg3(K, J, I))
|
|
writeu8(b, dst + 64, avg3(J, I, X))
|
|
v = avg3(I, X, A)
|
|
writeu8(b, dst + 32, v)
|
|
writeu8(b, dst + 97, v)
|
|
v = avg3(X, A, B)
|
|
writeu8(b, dst + 33, v)
|
|
writeu8(b, dst + 98, v)
|
|
v = avg3(A, B, C)
|
|
writeu8(b, dst + 34, v)
|
|
writeu8(b, dst + 99, v)
|
|
writeu8(b, dst + 35, avg3(B, C, D))
|
|
elseif mode == 6 then -- LD
|
|
writeu8(b, dst + 0, avg3(A, B, C))
|
|
local v = avg3(B, C, D)
|
|
writeu8(b, dst + 1, v)
|
|
writeu8(b, dst + 32, v)
|
|
v = avg3(C, D, E)
|
|
writeu8(b, dst + 2, v)
|
|
writeu8(b, dst + 33, v)
|
|
writeu8(b, dst + 64, v)
|
|
v = avg3(D, E, F)
|
|
writeu8(b, dst + 3, v)
|
|
writeu8(b, dst + 34, v)
|
|
writeu8(b, dst + 65, v)
|
|
writeu8(b, dst + 96, v)
|
|
v = avg3(E, F, G)
|
|
writeu8(b, dst + 35, v)
|
|
writeu8(b, dst + 66, v)
|
|
writeu8(b, dst + 97, v)
|
|
v = avg3(F, G, H)
|
|
writeu8(b, dst + 67, v)
|
|
writeu8(b, dst + 98, v)
|
|
writeu8(b, dst + 99, avg3(G, H, H))
|
|
elseif mode == 7 then -- VL
|
|
writeu8(b, dst + 0, avg2(A, B))
|
|
local v = avg2(B, C)
|
|
writeu8(b, dst + 1, v)
|
|
writeu8(b, dst + 64, v)
|
|
v = avg2(C, D)
|
|
writeu8(b, dst + 2, v)
|
|
writeu8(b, dst + 65, v)
|
|
v = avg2(D, E)
|
|
writeu8(b, dst + 3, v)
|
|
writeu8(b, dst + 66, v)
|
|
writeu8(b, dst + 32, avg3(A, B, C))
|
|
v = avg3(B, C, D)
|
|
writeu8(b, dst + 33, v)
|
|
writeu8(b, dst + 96, v)
|
|
v = avg3(C, D, E)
|
|
writeu8(b, dst + 34, v)
|
|
writeu8(b, dst + 97, v)
|
|
v = avg3(D, E, F)
|
|
writeu8(b, dst + 35, v)
|
|
writeu8(b, dst + 98, v)
|
|
writeu8(b, dst + 67, avg3(E, F, G))
|
|
writeu8(b, dst + 99, avg3(F, G, H))
|
|
elseif mode == 8 then -- HD
|
|
local v = avg2(I, X)
|
|
writeu8(b, dst + 0, v)
|
|
writeu8(b, dst + 34, v)
|
|
v = avg2(J, I)
|
|
writeu8(b, dst + 32, v)
|
|
writeu8(b, dst + 66, v)
|
|
v = avg2(K, J)
|
|
writeu8(b, dst + 64, v)
|
|
writeu8(b, dst + 98, v)
|
|
writeu8(b, dst + 96, avg2(L, K))
|
|
writeu8(b, dst + 3, avg3(A, B, C))
|
|
writeu8(b, dst + 2, avg3(X, A, B))
|
|
v = avg3(I, X, A)
|
|
writeu8(b, dst + 1, v)
|
|
writeu8(b, dst + 35, v)
|
|
v = avg3(J, I, X)
|
|
writeu8(b, dst + 33, v)
|
|
writeu8(b, dst + 67, v)
|
|
v = avg3(K, J, I)
|
|
writeu8(b, dst + 65, v)
|
|
writeu8(b, dst + 99, v)
|
|
writeu8(b, dst + 97, avg3(L, K, J))
|
|
else -- HU
|
|
writeu8(b, dst + 0, avg2(I, J))
|
|
local v = avg2(J, K)
|
|
writeu8(b, dst + 2, v)
|
|
writeu8(b, dst + 32, v)
|
|
v = avg2(K, L)
|
|
writeu8(b, dst + 34, v)
|
|
writeu8(b, dst + 64, v)
|
|
writeu8(b, dst + 1, avg3(I, J, K))
|
|
v = avg3(J, K, L)
|
|
writeu8(b, dst + 3, v)
|
|
writeu8(b, dst + 33, v)
|
|
v = avg3(K, L, L)
|
|
writeu8(b, dst + 35, v)
|
|
writeu8(b, dst + 65, v)
|
|
writeu8(b, dst + 67, L)
|
|
writeu8(b, dst + 66, L)
|
|
writeu8(b, dst + 96, L)
|
|
writeu8(b, dst + 97, L)
|
|
writeu8(b, dst + 98, L)
|
|
writeu8(b, dst + 99, L)
|
|
end
|
|
end
|
|
end
|
|
|
|
local function checkMode(mbx: number, mby: number, mode: number): number
|
|
if mode == 0 then
|
|
if mbx == 0 then
|
|
return if mby == 0 then 6 else 5
|
|
end
|
|
return if mby == 0 then 4 else 0
|
|
end
|
|
return mode
|
|
end
|
|
|
|
-- ------------------------------------------------------------------------------------------------ loop filter
|
|
-- 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 _ = 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
|
|
|
|
-- 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)
|
|
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
|
|
p += vs
|
|
end
|
|
end
|
|
|
|
-- ------------------------------------------------------------------------------------------------ inter modes
|
|
local MODE_CTX = Tables.MODE_CONTEXTS
|
|
local SUB_MV_REF_PROB = Tables.SUB_MV_REF_PROB
|
|
local SPLIT_OFFSET, SPLIT_FILL, SPLIT_COUNT = Tables.SPLIT_OFFSET, Tables.SPLIT_FILL, Tables.SPLIT_COUNT
|
|
local BMODE_PROB = Tables.BMODE_PROB
|
|
local MV_UPDATE = Tables.MV_UPDATE
|
|
|
|
-- macroblock modes (the row arrays and miMode): 0 DC, 1 TM, 2 V, 3 H (16x16 intra, libwebp's numbering), 4 B_PRED,
|
|
-- 5 ZEROMV, 6 NEARESTMV, 7 NEARMV, 8 NEWMV, 9 SPLITMV. Their loop filter mode delta index:
|
|
local MODE_LF = { 1, 1, 1, 1, 0, 1, 2, 2, 2, 3 }
|
|
|
|
-- a 4x4 intra mode with the fixed probabilities P[o + 1 .. o + 9] (libvpx vp8_bmode_tree, libwebp's numbering)
|
|
local function readBMode(br: Br, P: { number }, o: number): number
|
|
if getBit(br, P[o + 1]) == 0 then
|
|
return 0
|
|
elseif getBit(br, P[o + 2]) == 0 then
|
|
return 1
|
|
elseif getBit(br, P[o + 3]) == 0 then
|
|
return 2
|
|
elseif getBit(br, P[o + 4]) == 0 then
|
|
if getBit(br, P[o + 5]) == 0 then
|
|
return 3
|
|
end
|
|
return if getBit(br, P[o + 6]) == 0 then 4 else 5
|
|
elseif getBit(br, P[o + 7]) == 0 then
|
|
return 6
|
|
elseif getBit(br, P[o + 8]) == 0 then
|
|
return 7
|
|
end
|
|
return if getBit(br, P[o + 9]) == 0 then 8 else 9
|
|
end
|
|
|
|
-- one motion vector component (libvpx read_mvcomponent) with the probabilities P[o + 1 .. o + 19], in quarter pixels
|
|
local function readMvComponent(br: Br, P: { number }, o: number): number
|
|
local x
|
|
if getBit(br, P[o + 1]) == 1 then -- long: bits 0-2, then 9-4, then bit 3 (implicit when nothing above it is set)
|
|
x = getBit(br, P[o + 10]) + 2 * getBit(br, P[o + 11]) + 4 * getBit(br, P[o + 12])
|
|
for i = 9, 4, -1 do
|
|
x += getBit(br, P[o + 10 + i]) * lshift(1, i)
|
|
end
|
|
if x < 16 or getBit(br, P[o + 13]) == 1 then
|
|
x += 8
|
|
end
|
|
elseif getBit(br, P[o + 3]) == 0 then -- short tree
|
|
if getBit(br, P[o + 4]) == 0 then
|
|
x = getBit(br, P[o + 5])
|
|
else
|
|
x = 2 + getBit(br, P[o + 6])
|
|
end
|
|
elseif getBit(br, P[o + 7]) == 0 then
|
|
x = 4 + getBit(br, P[o + 8])
|
|
else
|
|
x = 6 + getBit(br, P[o + 9])
|
|
end
|
|
if x ~= 0 and getBit(br, P[o + 2]) == 1 then
|
|
x = -x
|
|
end
|
|
return x
|
|
end
|
|
|
|
export type Rows = {
|
|
i4: { boolean }, -- no Y2 block (B_PRED or SPLITMV)
|
|
modes: { number }, -- 16 per macroblock: the 16x16 intra mode or the 4x4 ones
|
|
uv: { number },
|
|
ref: { number }, -- 0 intra, 1 last, 2 golden, 3 altref
|
|
mode: { number }, -- 0..9 (see MODE_LF)
|
|
part: { number }, -- SPLITMV partitioning (0 16x8, 1 8x16, 2 8x8, 3 4x4) or -1
|
|
}
|
|
|
|
local nr, nc, cnt = table.create(4, 0), table.create(4, 0), table.create(4, 0)
|
|
|
|
-- readInterMb(st, br, probs, mi, mbx, mby, rows): the modes and motion vectors of one inter frame macroblock (libvpx
|
|
-- read_mb_modes_mv with vp8_find_near_mvs); mi: its 1-based index in the mode info arrays
|
|
local function readInterMb(st: State, br: Br, probIntra: number, probLast: number, probGf: number, mi: number, mbx: number, mby: number, rows: Rows)
|
|
local miRef, miMode, miMvR, miMvC = st.miRef, st.miMode, st.miMvR, st.miMvC
|
|
local x1 = mbx + 1
|
|
if getBit(br, probIntra) == 0 then
|
|
local P = st.ymodeProb
|
|
local ymode
|
|
if getBit(br, P[1]) == 0 then
|
|
ymode = 0
|
|
elseif getBit(br, P[2]) == 0 then
|
|
ymode = if getBit(br, P[3]) == 0 then 2 else 3
|
|
else
|
|
ymode = if getBit(br, P[4]) == 0 then 1 else 4
|
|
end
|
|
local modes = rows.modes
|
|
if ymode == 4 then
|
|
for k = mbx * 16 + 1, mbx * 16 + 16 do
|
|
modes[k] = readBMode(br, BMODE_PROB, 0)
|
|
end
|
|
else
|
|
modes[mbx * 16 + 1] = ymode
|
|
end
|
|
local U = st.uvmodeProb
|
|
if getBit(br, U[1]) == 0 then
|
|
rows.uv[x1] = 0
|
|
elseif getBit(br, U[2]) == 0 then
|
|
rows.uv[x1] = 2
|
|
else
|
|
rows.uv[x1] = if getBit(br, U[3]) == 0 then 3 else 1
|
|
end
|
|
rows.i4[x1] = ymode == 4
|
|
rows.ref[x1] = 0
|
|
rows.mode[x1] = ymode
|
|
rows.part[x1] = -1
|
|
miRef[mi], miMode[mi], miMvR[mi], miMvC[mi] = 0, ymode, 0, 0
|
|
return
|
|
end
|
|
local ref = 1
|
|
if getBit(br, probLast) == 1 then
|
|
ref = 2 + getBit(br, probGf)
|
|
end
|
|
-- the near motion vectors of the above, left and above-left macroblocks, sign corrected
|
|
local bias = st.signBias
|
|
local sb = bias[ref + 1]
|
|
local a = mi - (st.mbw + 1)
|
|
local l = mi - 1
|
|
local al = a - 1
|
|
for k = 1, 4 do
|
|
nr[k], nc[k], cnt[k] = 0, 0, 0
|
|
end
|
|
local n = 1
|
|
local rf = miRef[a]
|
|
if rf ~= 0 then
|
|
local r, c = miMvR[a], miMvC[a]
|
|
if r ~= 0 or c ~= 0 then
|
|
if bias[rf + 1] ~= sb then
|
|
r, c = -r, -c
|
|
end
|
|
n = 2
|
|
nr[2], nc[2] = r, c
|
|
end
|
|
cnt[n] += 2
|
|
end
|
|
rf = miRef[l]
|
|
if rf ~= 0 then
|
|
local r, c = miMvR[l], miMvC[l]
|
|
if r ~= 0 or c ~= 0 then
|
|
if bias[rf + 1] ~= sb then
|
|
r, c = -r, -c
|
|
end
|
|
if r ~= nr[n] or c ~= nc[n] then
|
|
n += 1
|
|
nr[n], nc[n] = r, c
|
|
end
|
|
cnt[n] += 2
|
|
else
|
|
cnt[1] += 2
|
|
end
|
|
end
|
|
rf = miRef[al]
|
|
if rf ~= 0 then
|
|
local r, c = miMvR[al], miMvC[al]
|
|
if r ~= 0 or c ~= 0 then
|
|
if bias[rf + 1] ~= sb then
|
|
r, c = -r, -c
|
|
end
|
|
if r ~= nr[n] or c ~= nc[n] then
|
|
n += 1
|
|
nr[n], nc[n] = r, c
|
|
end
|
|
cnt[n] += 1
|
|
else
|
|
cnt[1] += 1
|
|
end
|
|
end
|
|
-- clamping bounds (vp8_clamp_mv2), in eighth pixels
|
|
local mbw, mbh = st.mbw, st.mbh
|
|
local toL, toR = -mbx * 128 - 128, (mbw - 1 - mbx) * 128 + 128
|
|
local toT, toB = -mby * 128 - 128, (mbh - 1 - mby) * 128 + 128
|
|
local mode, mvr, mvc, part = 5, 0, 0, -1
|
|
if getBit(br, MODE_CTX[cnt[1] * 4 + 1]) == 1 then
|
|
if cnt[4] > 0 and nr[n] == nr[2] and nc[n] == nc[2] then
|
|
cnt[2] += 1
|
|
end
|
|
if cnt[3] > cnt[2] then
|
|
cnt[2], cnt[3] = cnt[3], cnt[2]
|
|
nr[2], nr[3] = nr[3], nr[2]
|
|
nc[2], nc[3] = nc[3], nc[2]
|
|
end
|
|
local k
|
|
if getBit(br, MODE_CTX[cnt[2] * 4 + 2]) == 0 then
|
|
mode, k = 6, 2
|
|
elseif getBit(br, MODE_CTX[cnt[3] * 4 + 3]) == 0 then
|
|
mode, k = 7, 3
|
|
else
|
|
k = if cnt[2] >= cnt[1] then 2 else 1
|
|
end
|
|
mvr, mvc = nr[k], nc[k]
|
|
if mvc < toL then
|
|
mvc = toL
|
|
elseif mvc > toR then
|
|
mvc = toR
|
|
end
|
|
if mvr < toT then
|
|
mvr = toT
|
|
elseif mvr > toB then
|
|
mvr = toB
|
|
end
|
|
if mode == 5 then
|
|
-- mvr, mvc: the best vector; new or split
|
|
local P = st.mvProb
|
|
local sc = (if miMode[a] == 9 then 2 else 0) + (if miMode[l] == 9 then 2 else 0) + (if miMode[al] == 9 then 1 else 0)
|
|
if getBit(br, MODE_CTX[sc * 4 + 4]) == 0 then
|
|
mode = 8
|
|
mvr += readMvComponent(br, P, 0) * 2
|
|
mvc += readMvComponent(br, P, 19) * 2
|
|
else
|
|
mode = 9
|
|
local bestR, bestC = mvr, mvc
|
|
local s, nump
|
|
if getBit(br, 110) == 1 then
|
|
if getBit(br, 111) == 1 then
|
|
s, nump = getBit(br, 150), 2
|
|
else
|
|
s, nump = 2, 4
|
|
end
|
|
else
|
|
s, nump = 3, 16
|
|
end
|
|
part = s
|
|
local subR, subC = st.miSubR, st.miSubC
|
|
local sb0 = (mi - 1) * 16
|
|
local lb, ab = (l - 1) * 16, (a - 1) * 16
|
|
local splitL, splitA = miMode[l] == 9, miMode[a] == 9
|
|
local count = SPLIT_COUNT[s + 1]
|
|
for j = 0, nump - 1 do
|
|
local b = SPLIT_OFFSET[s * 16 + j + 1]
|
|
local lr, lc, ar, ac
|
|
if b % 4 ~= 0 then
|
|
lr, lc = subR[sb0 + b], subC[sb0 + b]
|
|
elseif splitL then
|
|
lr, lc = subR[lb + b + 4], subC[lb + b + 4]
|
|
else
|
|
lr, lc = miMvR[l], miMvC[l]
|
|
end
|
|
if b >= 4 then
|
|
ar, ac = subR[sb0 + b - 3], subC[sb0 + b - 3]
|
|
elseif splitA then
|
|
ar, ac = subR[ab + b + 13], subC[ab + b + 13]
|
|
else
|
|
ar, ac = miMvR[a], miMvC[a]
|
|
end
|
|
local pi = ((if ar == 0 and ac == 0 then 4 else 0) + (if lr == 0 and lc == 0 then 2 else 0) + (if lr == ar and lc == ac then 1 else 0)) * 3
|
|
local r, c
|
|
if getBit(br, SUB_MV_REF_PROB[pi + 1]) == 0 then
|
|
r, c = lr, lc
|
|
elseif getBit(br, SUB_MV_REF_PROB[pi + 2]) == 0 then
|
|
r, c = ar, ac
|
|
elseif getBit(br, SUB_MV_REF_PROB[pi + 3]) == 0 then
|
|
r, c = 0, 0
|
|
else
|
|
r = bestR + readMvComponent(br, P, 0) * 2
|
|
c = bestC + readMvComponent(br, P, 19) * 2
|
|
end
|
|
local fo = s * 16 + j * count
|
|
for i = fo + 1, fo + count do
|
|
local q = sb0 + SPLIT_FILL[i] + 1
|
|
subR[q], subC[q] = r, c
|
|
end
|
|
end
|
|
mvr, mvc = subR[sb0 + 16], subC[sb0 + 16]
|
|
end
|
|
end
|
|
end
|
|
rows.i4[x1] = mode == 9
|
|
rows.ref[x1] = ref
|
|
rows.mode[x1] = mode
|
|
rows.part[x1] = part
|
|
miRef[mi], miMode[mi], miMvR[mi], miMvC[mi] = ref, mode, mvr, mvc
|
|
end
|
|
|
|
-- ------------------------------------------------------------------------------------------------ frame
|
|
export type Frame = {
|
|
width: number,
|
|
height: number,
|
|
ref: Ref, -- the decoded planes (the decoder keeps them as a reference: copy them out, see cropTo)
|
|
header: any,
|
|
}
|
|
|
|
local BORDER = Predict.BORDER
|
|
|
|
local function newRef(mbw: number, mbh: number): Ref
|
|
local ys, uvs = mbw * 16 + 2 * BORDER, mbw * 8 + 2 * BORDER
|
|
return {
|
|
Y = bcreate(ys * (mbh * 16 + 2 * BORDER)),
|
|
U = bcreate(uvs * (mbh * 8 + 2 * BORDER)),
|
|
V = bcreate(uvs * (mbh * 8 + 2 * BORDER)),
|
|
ys = ys,
|
|
uvs = uvs,
|
|
yb = BORDER * ys + BORDER,
|
|
uvb = BORDER * uvs + BORDER,
|
|
mbw = mbw,
|
|
mbh = mbh,
|
|
extended = false,
|
|
}
|
|
end
|
|
|
|
-- a frame buffer no reference uses
|
|
local function freeRef(st: State): Ref
|
|
for _, r in st.pool do
|
|
if r ~= st.last and r ~= st.golden and r ~= st.alt then
|
|
return r
|
|
end
|
|
end
|
|
local r = newRef(st.mbw, st.mbh)
|
|
table.insert(st.pool, r)
|
|
return r
|
|
end
|
|
|
|
-- cropTo(frame, dy, oy, du, ou, dv, ov): copies the visible planes out (luma stride width, chroma ceil(width / 2))
|
|
function Decode.cropTo(f: Frame, dy: buffer, oy: number, du: buffer, ou: number, dv: buffer, ov: number)
|
|
local r = f.ref
|
|
local w, h = f.width, f.height
|
|
local cw, chh = (w + 1) // 2, (h + 1) // 2
|
|
local Y, U, V, ys, uvs, yb, uvb = r.Y, r.U, r.V, r.ys, r.uvs, r.yb, r.uvb
|
|
for j = 0, h - 1 do
|
|
bcopy(dy, oy + j * w, Y, yb + j * ys, w)
|
|
end
|
|
for j = 0, chh - 1 do
|
|
bcopy(du, ou + j * cw, U, uvb + j * uvs, cw)
|
|
bcopy(dv, ov + j * cw, V, uvb + j * uvs, cw)
|
|
end
|
|
end
|
|
|
|
-- decodeFrame(state, buf, off, len, maxPixels) -> Frame | nil, message: one key or inter frame of the stream
|
|
function Decode.decodeFrame(st: State, buf: buffer, off: number, len: number, maxPixels: number): (Frame?, string?)
|
|
local hdr, herr = Decode.header(buf, off, len)
|
|
if not hdr then
|
|
return nil, herr
|
|
end
|
|
local key = hdr.keyFrame
|
|
local width, height, p, left
|
|
if key then
|
|
width, height = hdr.width, hdr.height
|
|
if width == 0 or height == 0 then
|
|
return nil, "zero frame size"
|
|
end
|
|
if width * height > maxPixels then
|
|
return nil, "limit: " .. width .. " x " .. height .. " is over maxPixels"
|
|
end
|
|
p, left = off + 10, len - 10
|
|
else
|
|
if not st.hasKey then
|
|
return nil, "inter frame without a key frame"
|
|
end
|
|
width, height = st.width, st.height
|
|
p, left = off + 3, len - 3
|
|
end
|
|
if hdr.partitionLength > left then
|
|
return nil, "truncated: first partition"
|
|
end
|
|
local mbw, mbh = (width + 15) // 16, (height + 15) // 16
|
|
if key then
|
|
st.hasKey = false
|
|
if width ~= st.width or height ~= st.height then
|
|
st.width, st.height, st.mbw, st.mbh = width, height, mbw, mbh
|
|
st.last, st.golden, st.alt, st.pool = nil, nil, nil, {}
|
|
st.miN, st.pred = 0, nil
|
|
st.segMap = table.create(mbw * mbh, 0)
|
|
end
|
|
-- key frames reset the frame context, the segment and loop filter deltas
|
|
st.proba = table.clone(COEFF_PROBA0)
|
|
st.ymodeProb = table.clone(Tables.YMODE_PROB)
|
|
st.uvmodeProb = table.clone(Tables.UVMODE_PROB)
|
|
st.mvProb = table.clone(Tables.MV_DEFAULT)
|
|
st.segProba = { 255, 255, 255 }
|
|
st.useSegment, st.updateMap, st.absoluteDelta = false, false, false
|
|
st.segQuant = { 0, 0, 0, 0 }
|
|
st.segFilter = { 0, 0, 0, 0 }
|
|
st.refLfDelta = { 0, 0, 0, 0 }
|
|
st.modeLfDelta = { 0, 0, 0, 0 }
|
|
st.signBias = { 0, 0, 0, 0 }
|
|
end
|
|
local br = newBr(buf, p, hdr.partitionLength)
|
|
p += hdr.partitionLength
|
|
left -= hdr.partitionLength
|
|
if key then
|
|
getValue(br, 1) -- colour space
|
|
getValue(br, 1) -- clamping type
|
|
end
|
|
-- segment header
|
|
st.useSegment = getValue(br, 1) == 1
|
|
if st.useSegment then
|
|
st.updateMap = getValue(br, 1) == 1
|
|
if getValue(br, 1) == 1 then
|
|
st.absoluteDelta = getValue(br, 1) == 1
|
|
for s = 1, 4 do
|
|
st.segQuant[s] = if getValue(br, 1) == 1 then getSignedValue(br, 7) else 0
|
|
end
|
|
for s = 1, 4 do
|
|
st.segFilter[s] = if getValue(br, 1) == 1 then getSignedValue(br, 6) else 0
|
|
end
|
|
end
|
|
if st.updateMap then
|
|
for s = 1, 3 do
|
|
st.segProba[s] = if getValue(br, 1) == 1 then getValue(br, 8) else 255
|
|
end
|
|
end
|
|
else
|
|
st.updateMap = false
|
|
end
|
|
if br.eof then
|
|
return nil, "truncated: segment header"
|
|
end
|
|
-- filter header
|
|
local simple = getValue(br, 1) == 1
|
|
local level = getValue(br, 6)
|
|
local sharpness = getValue(br, 3)
|
|
local useLfDelta = getValue(br, 1) == 1
|
|
if useLfDelta then
|
|
if getValue(br, 1) == 1 then
|
|
for i = 1, 4 do
|
|
if getValue(br, 1) == 1 then
|
|
st.refLfDelta[i] = getSignedValue(br, 6)
|
|
end
|
|
end
|
|
for i = 1, 4 do
|
|
if getValue(br, 1) == 1 then
|
|
st.modeLfDelta[i] = getSignedValue(br, 6)
|
|
end
|
|
end
|
|
end
|
|
end
|
|
local filterType = if level == 0 then 0 elseif simple then 1 else 2
|
|
if br.eof then
|
|
return nil, "truncated: filter header"
|
|
end
|
|
-- partitions
|
|
local numParts = lshift(1, getValue(br, 2))
|
|
local lastPart = numParts - 1
|
|
if left < 3 * lastPart then
|
|
return nil, "truncated: partition sizes"
|
|
end
|
|
local parts: { Br } = {}
|
|
local partStart = p + lastPart * 3
|
|
local sizeLeft = left - lastPart * 3
|
|
for k = 0, lastPart - 1 do
|
|
local q = p + k * 3
|
|
local psize = readu8(buf, q) + readu8(buf, q + 1) * 256 + readu8(buf, q + 2) * 65536
|
|
if psize > sizeLeft then
|
|
psize = sizeLeft
|
|
end
|
|
parts[k + 1] = newBr(buf, partStart, psize)
|
|
partStart += psize
|
|
sizeLeft -= psize
|
|
end
|
|
parts[numParts] = newBr(buf, partStart, sizeLeft)
|
|
if partStart >= off + len then
|
|
return nil, "truncated: partitions"
|
|
end
|
|
-- quantizers
|
|
local baseQ = getValue(br, 7)
|
|
local dqy1dc = if getValue(br, 1) == 1 then getSignedValue(br, 4) else 0
|
|
local dqy2dc = if getValue(br, 1) == 1 then getSignedValue(br, 4) else 0
|
|
local dqy2ac = if getValue(br, 1) == 1 then getSignedValue(br, 4) else 0
|
|
local dquvdc = if getValue(br, 1) == 1 then getSignedValue(br, 4) else 0
|
|
local dquvac = if getValue(br, 1) == 1 then getSignedValue(br, 4) else 0
|
|
local quants: { Quant } = {}
|
|
for s = 1, 4 do
|
|
local q
|
|
if st.useSegment then
|
|
q = st.segQuant[s]
|
|
if not st.absoluteDelta then
|
|
q += baseQ
|
|
end
|
|
else
|
|
q = baseQ
|
|
end
|
|
local function cq(v: number, m: number): number
|
|
return if v < 0 then 0 elseif v > m then m else v
|
|
end
|
|
local y2ac = (AC_TABLE[cq(q + dqy2ac, 127) + 1] * 101581) // 65536
|
|
if y2ac < 8 then
|
|
y2ac = 8
|
|
end
|
|
quants[s] = {
|
|
y1dc = DC_TABLE[cq(q + dqy1dc, 127) + 1],
|
|
y1ac = AC_TABLE[cq(q, 127) + 1],
|
|
y2dc = DC_TABLE[cq(q + dqy2dc, 127) + 1] * 2,
|
|
y2ac = y2ac,
|
|
uvdc = DC_TABLE[cq(q + dquvdc, 117) + 1],
|
|
uvac = AC_TABLE[cq(q + dquvac, 127) + 1],
|
|
}
|
|
end
|
|
-- reference updates (inter frames)
|
|
local refreshGolden, refreshAlt, copyToGolden, copyToAlt = true, true, 0, 0
|
|
if not key then
|
|
refreshGolden = getValue(br, 1) == 1
|
|
refreshAlt = getValue(br, 1) == 1
|
|
copyToGolden = if refreshGolden then 0 else getValue(br, 2)
|
|
copyToAlt = if refreshAlt then 0 else getValue(br, 2)
|
|
st.signBias[3] = getValue(br, 1)
|
|
st.signBias[4] = getValue(br, 1)
|
|
end
|
|
-- a frame that does not refresh the context decodes with the updates below, then the context comes back
|
|
local saved: { { number } }? = nil
|
|
if getValue(br, 1) == 0 then
|
|
saved = { table.clone(st.proba), table.clone(st.ymodeProb), table.clone(st.uvmodeProb), table.clone(st.mvProb) }
|
|
end
|
|
local refreshLast = key or getValue(br, 1) == 1
|
|
-- token probabilities
|
|
local P = st.proba
|
|
for i = 1, 1056 do
|
|
if getBit(br, COEFF_UPDATE_PROBA[i]) == 1 then
|
|
P[i] = getValue(br, 8)
|
|
end
|
|
end
|
|
local useSkip = getValue(br, 1) == 1
|
|
local skipP = if useSkip then getValue(br, 8) else 0
|
|
local probIntra, probLast, probGf = 0, 0, 0
|
|
if not key then
|
|
probIntra = getValue(br, 8)
|
|
probLast = getValue(br, 8)
|
|
probGf = getValue(br, 8)
|
|
if getValue(br, 1) == 1 then
|
|
for i = 1, 4 do
|
|
st.ymodeProb[i] = getValue(br, 8)
|
|
end
|
|
end
|
|
if getValue(br, 1) == 1 then
|
|
for i = 1, 3 do
|
|
st.uvmodeProb[i] = getValue(br, 8)
|
|
end
|
|
end
|
|
local M = st.mvProb
|
|
for i = 1, 38 do
|
|
if getBit(br, MV_UPDATE[i]) == 1 then
|
|
local x = getValue(br, 7)
|
|
M[i] = if x == 0 then 1 else x * 2
|
|
end
|
|
end
|
|
end
|
|
local function fail(msg: string): (Frame?, string?)
|
|
if saved then
|
|
st.proba, st.ymodeProb, st.uvmodeProb, st.mvProb = saved[1], saved[2], saved[3], saved[4]
|
|
end
|
|
return nil, msg
|
|
end
|
|
if br.eof then
|
|
return fail("truncated: frame header")
|
|
end
|
|
|
|
-- loop filter levels [segment][reference][mode delta index] and, per level, the interior limit and hev threshold
|
|
local lvlTab = table.create(64, 0)
|
|
local ilTab, hevTab = table.create(64, 0), table.create(64, 0)
|
|
if filterType > 0 then
|
|
for s = 0, 3 do
|
|
local base
|
|
if st.useSegment then
|
|
base = st.segFilter[s + 1]
|
|
if not st.absoluteDelta then
|
|
base += level
|
|
end
|
|
else
|
|
base = level
|
|
end
|
|
for rf = 0, 3 do
|
|
for m = 0, 3 do
|
|
local lv = base
|
|
if useLfDelta then
|
|
lv += st.refLfDelta[rf + 1]
|
|
if rf > 0 or m == 0 then
|
|
lv += st.modeLfDelta[m + 1]
|
|
end
|
|
end
|
|
lvlTab[s * 16 + rf * 4 + m + 1] = if lv < 0 then 0 elseif lv > 63 then 63 else lv
|
|
end
|
|
end
|
|
end
|
|
for lv = 1, 63 do
|
|
local il = lv
|
|
if sharpness > 0 then
|
|
il = rshift(il, if sharpness > 4 then 2 else 1)
|
|
if il > 9 - sharpness then
|
|
il = 9 - sharpness
|
|
end
|
|
end
|
|
ilTab[lv] = if il < 1 then 1 else il
|
|
if key then
|
|
hevTab[lv] = if lv >= 40 then 2 elseif lv >= 15 then 1 else 0
|
|
else
|
|
hevTab[lv] = if lv >= 40 then 3 elseif lv >= 20 then 2 elseif lv >= 15 then 1 else 0
|
|
end
|
|
end
|
|
end
|
|
|
|
-- the frame buffer (macroblock aligned, with a border) and the references
|
|
local cur = freeRef(st)
|
|
cur.extended = false
|
|
local Y, U, V, ys, uvs, yb, uvb = cur.Y, cur.U, cur.V, cur.ys, cur.uvs, cur.yb, cur.uvb
|
|
local refs: { Ref } = {}
|
|
local bil, full = hdr.profile > 0, hdr.profile == 3
|
|
if not key then
|
|
local rl, rg, ra = st.last :: Ref, st.golden :: Ref, st.alt :: Ref
|
|
refs[1], refs[2], refs[3] = rl, rg, ra
|
|
for k = 1, 3 do
|
|
local r = refs[k]
|
|
if not r.extended then
|
|
Predict.extend(r)
|
|
r.extended = true
|
|
end
|
|
end
|
|
end
|
|
local topY, topU, topV = bcreate(mbw * 16), bcreate(mbw * 8), bcreate(mbw * 8)
|
|
local yuv = bcreate(YUV_SIZE)
|
|
local pred = yuv
|
|
if not key then
|
|
local nmi = (mbw + 1) * (mbh + 1)
|
|
if st.miN ~= nmi then
|
|
-- zero top row and left column: never written, they read as intra macroblocks without motion
|
|
st.miRef, st.miMode = table.create(nmi, 0), table.create(nmi, 0)
|
|
st.miMvR, st.miMvC = table.create(nmi, 0), table.create(nmi, 0)
|
|
st.miSubR, st.miSubC = table.create(nmi * 16, 0), table.create(nmi * 16, 0)
|
|
st.miN = nmi
|
|
st.pred = bcreate(YUV_SIZE * mbw)
|
|
end
|
|
pred = st.pred :: buffer
|
|
end
|
|
local coeffs = bcreate(mbw * 768)
|
|
local dcb = bcreate(32)
|
|
-- per macroblock column / row state
|
|
local intraT = table.create(mbw * 4, 0)
|
|
local intraL = table.create(4, 0)
|
|
local topNz = table.create(mbw, 0)
|
|
local topNzDc = table.create(mbw, 0)
|
|
local rows: Rows = {
|
|
i4 = table.create(mbw, false),
|
|
modes = table.create(mbw * 16, 0),
|
|
uv = table.create(mbw, 0),
|
|
ref = table.create(mbw, 0),
|
|
mode = table.create(mbw, 0),
|
|
part = table.create(mbw, -1),
|
|
}
|
|
local mbI4, mbModes, mbUv, mbRef, mbMode, mbPart = rows.i4, rows.modes, rows.uv, rows.ref, rows.mode, rows.part
|
|
local mbSeg = table.create(mbw, 0)
|
|
local mbSkip = table.create(mbw, 0)
|
|
local mbNzY = table.create(mbw, 0)
|
|
local mbNzUv = table.create(mbw, 0)
|
|
-- filter info for the whole frame: limit, ilevel, inner, hev per macroblock
|
|
local fLimit = table.create(mbw * mbh, 0)
|
|
local fIlevel = table.create(mbw * mbh, 0)
|
|
local fInner = table.create(mbw * mbh, 0)
|
|
local fHev = table.create(mbw * mbh, 0)
|
|
local segProba = st.segProba
|
|
local segMap = st.segMap
|
|
local updateMap = st.updateMap
|
|
local miMvR, miMvC, miSubR, miSubC = st.miMvR, st.miMvC, st.miSubR, st.miSubC
|
|
|
|
for mby = 0, mbh - 1 do
|
|
local tbr = parts[band(mby, lastPart) + 1]
|
|
-- modes of the row (first partition)
|
|
for k = 1, 4 do
|
|
intraL[k] = 0
|
|
end
|
|
for mbx = 0, mbw - 1 do
|
|
local x1 = mbx + 1
|
|
local si = mby * mbw + x1
|
|
local seg
|
|
if updateMap then
|
|
if getBit(br, segProba[1]) == 0 then
|
|
seg = getBit(br, segProba[2])
|
|
else
|
|
seg = getBit(br, segProba[3]) + 2
|
|
end
|
|
segMap[si] = seg
|
|
elseif key then
|
|
seg = 0
|
|
segMap[si] = 0
|
|
else
|
|
seg = segMap[si]
|
|
end
|
|
mbSeg[x1] = seg
|
|
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
|
|
end
|
|
mbRef[x1] = 0
|
|
mbPart[x1] = -1
|
|
local i4 = getBit(br, 145) == 0
|
|
mbI4[x1] = i4
|
|
local t0 = mbx * 4
|
|
if not i4 then
|
|
local ymode
|
|
if getBit(br, 156) == 1 then
|
|
ymode = if getBit(br, 128) == 1 then 1 else 3
|
|
else
|
|
ymode = if getBit(br, 163) == 1 then 2 else 0
|
|
end
|
|
mbModes[mbx * 16 + 1] = ymode
|
|
mbMode[x1] = ymode
|
|
for k = 1, 4 do
|
|
intraT[t0 + k] = ymode
|
|
intraL[k] = ymode
|
|
end
|
|
else
|
|
mbMode[x1] = 4
|
|
for y = 0, 3 do
|
|
local ymode = intraL[y + 1]
|
|
for x = 0, 3 do
|
|
local pr = (intraT[t0 + x + 1] * 10 + ymode) * 9
|
|
if getBit(br, BMODES_PROBA[pr + 1]) == 0 then
|
|
ymode = 0
|
|
elseif getBit(br, BMODES_PROBA[pr + 2]) == 0 then
|
|
ymode = 1
|
|
elseif getBit(br, BMODES_PROBA[pr + 3]) == 0 then
|
|
ymode = 2
|
|
elseif getBit(br, BMODES_PROBA[pr + 4]) == 0 then
|
|
if getBit(br, BMODES_PROBA[pr + 5]) == 0 then
|
|
ymode = 3
|
|
elseif getBit(br, BMODES_PROBA[pr + 6]) == 0 then
|
|
ymode = 4
|
|
else
|
|
ymode = 5
|
|
end
|
|
elseif getBit(br, BMODES_PROBA[pr + 7]) == 0 then
|
|
ymode = 6
|
|
elseif getBit(br, BMODES_PROBA[pr + 8]) == 0 then
|
|
ymode = 7
|
|
elseif getBit(br, BMODES_PROBA[pr + 9]) == 0 then
|
|
ymode = 8
|
|
else
|
|
ymode = 9
|
|
end
|
|
intraT[t0 + x + 1] = ymode
|
|
mbModes[mbx * 16 + y * 4 + x + 1] = ymode
|
|
end
|
|
intraL[y + 1] = ymode
|
|
end
|
|
end
|
|
if getBit(br, 142) == 0 then
|
|
mbUv[x1] = 0
|
|
elseif getBit(br, 114) == 0 then
|
|
mbUv[x1] = 2
|
|
else
|
|
mbUv[x1] = if getBit(br, 183) == 1 then 1 else 3
|
|
end
|
|
end
|
|
if br.eof then
|
|
return fail("truncated: first partition (modes)")
|
|
end
|
|
|
|
-- residuals (token partition)
|
|
local leftNz, leftNzDc = 0, 0
|
|
for mbx = 0, mbw - 1 do
|
|
local x1 = mbx + 1
|
|
local q = quants[mbSeg[x1] + 1]
|
|
local skip = mbSkip[x1]
|
|
local i4 = mbI4[x1]
|
|
if skip == 0 then
|
|
local co = mbx * 768
|
|
bfill(coeffs, co, 0, 768)
|
|
local first, acType
|
|
local dcNz = 0
|
|
if not i4 then
|
|
bfill(dcb, 0, 0, 32)
|
|
local ctx = topNzDc[x1] + leftNzDc
|
|
local nz = getCoeffs(tbr, P, 1, ctx, q.y2dc, q.y2ac, 0, dcb, 0)
|
|
dcNz = if nz > 0 then 1 else 0
|
|
topNzDc[x1] = dcNz
|
|
leftNzDc = dcNz
|
|
if nz > 1 then
|
|
transformWHT(dcb, coeffs, co)
|
|
else
|
|
local dc0 = (readi16(dcb, 0) + 3) // 8
|
|
for i = 0, 15 do
|
|
writei16(coeffs, co + i * 32, dc0)
|
|
end
|
|
end
|
|
first, acType = 1, 0
|
|
else
|
|
first, acType = 0, 3
|
|
end
|
|
local tnz = band(topNz[x1], 0x0f)
|
|
local lnz = band(leftNz, 0x0f)
|
|
local nzY = 0
|
|
local dst = co
|
|
for _ = 0, 3 do
|
|
local l = band(lnz, 1)
|
|
local acc = 0
|
|
for _ = 0, 3 do
|
|
local ctx = l + band(tnz, 1)
|
|
local nz = getCoeffs(tbr, P, acType, ctx, q.y1dc, q.y1ac, first, coeffs, dst)
|
|
l = if nz > first then 1 else 0
|
|
tnz = bor(rshift(tnz, 1), l * 128)
|
|
acc = nzCode(acc, nz, if readi16(coeffs, dst) ~= 0 then 1 else 0)
|
|
dst += 32
|
|
end
|
|
tnz = rshift(tnz, 4)
|
|
lnz = bor(rshift(lnz, 1), l * 128)
|
|
nzY = nzY * 256 + acc
|
|
end
|
|
local outT = tnz
|
|
local outL = rshift(lnz, 4)
|
|
local nzUv = 0
|
|
for ch = 0, 2, 2 do
|
|
local acc = 0
|
|
tnz = band(rshift(topNz[x1], 4 + ch), 255)
|
|
lnz = band(rshift(leftNz, 4 + ch), 255)
|
|
for _ = 0, 1 do
|
|
local l = band(lnz, 1)
|
|
for _ = 0, 1 do
|
|
local ctx = l + band(tnz, 1)
|
|
local nz = getCoeffs(tbr, P, 2, ctx, q.uvdc, q.uvac, 0, coeffs, dst)
|
|
l = if nz > 0 then 1 else 0
|
|
tnz = bor(rshift(tnz, 1), l * 8)
|
|
acc = nzCode(acc, nz, if readi16(coeffs, dst) ~= 0 then 1 else 0)
|
|
dst += 32
|
|
end
|
|
tnz = rshift(tnz, 2)
|
|
lnz = bor(rshift(lnz, 1), l * 32)
|
|
end
|
|
nzUv = bor(nzUv, lshift(acc, 4 * ch))
|
|
outT = bor(outT, lshift(lshift(tnz, 4), ch))
|
|
outL = bor(outL, lshift(band(lnz, 0xf0), ch))
|
|
end
|
|
topNz[x1] = band(outT, 255)
|
|
leftNz = band(outL, 255)
|
|
mbNzY[x1] = nzY
|
|
mbNzUv[x1] = nzUv
|
|
-- no coefficient at all (libvpx: eobtotal == 0) skips the inner edges of the loop filter
|
|
skip = if nzY == 0 and nzUv == 0 and dcNz == 0 then 1 else 0
|
|
else
|
|
topNz[x1] = 0
|
|
leftNz = 0
|
|
if not i4 then
|
|
topNzDc[x1] = 0
|
|
leftNzDc = 0
|
|
end
|
|
mbNzY[x1] = 0
|
|
mbNzUv[x1] = 0
|
|
end
|
|
if filterType > 0 then
|
|
local lv = lvlTab[mbSeg[x1] * 16 + mbRef[x1] * 4 + MODE_LF[mbMode[x1] + 1] + 1]
|
|
local fi = mby * mbw + x1
|
|
if lv > 0 then
|
|
local il = ilTab[lv]
|
|
fLimit[fi] = 2 * lv + il
|
|
fIlevel[fi] = il
|
|
fHev[fi] = hevTab[lv]
|
|
else
|
|
fLimit[fi] = 0
|
|
end
|
|
fInner[fi] = if i4 or skip == 0 then 1 else 0
|
|
end
|
|
if tbr.eof then
|
|
return fail("truncated: token partition")
|
|
end
|
|
end
|
|
|
|
-- reconstruct the row (libwebp ReconstructRow; inter macroblocks predict from the references)
|
|
if not key then
|
|
Predict.row(refs :: any, 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
|
|
for j = 0, 7 do
|
|
writeu8(yuv, U_OFF + j * BPS - 1, 129)
|
|
writeu8(yuv, V_OFF + j * BPS - 1, 129)
|
|
end
|
|
if mby > 0 then
|
|
writeu8(yuv, Y_OFF - 1 - BPS, 129)
|
|
writeu8(yuv, U_OFF - 1 - BPS, 129)
|
|
writeu8(yuv, V_OFF - 1 - BPS, 129)
|
|
else
|
|
bfill(yuv, Y_OFF - BPS - 1, 127, 16 + 4 + 1)
|
|
bfill(yuv, U_OFF - BPS - 1, 127, 8 + 1)
|
|
bfill(yuv, V_OFF - BPS - 1, 127, 8 + 1)
|
|
end
|
|
for mbx = 0, mbw - 1 do
|
|
local x1 = mbx + 1
|
|
local co = mbx * 768
|
|
local bits = mbNzY[x1]
|
|
local rf = mbRef[x1]
|
|
if rf == 0 and mbx > 0 then
|
|
-- the left context of an intra macroblock: the previous macroblock's last columns
|
|
for j = -1, 15 do
|
|
writeu32(yuv, Y_OFF + j * BPS - 4, readu32(yuv, Y_OFF + j * BPS + 12))
|
|
end
|
|
for j = -1, 7 do
|
|
writeu32(yuv, U_OFF + j * BPS - 4, readu32(yuv, U_OFF + j * BPS + 4))
|
|
writeu32(yuv, V_OFF + j * BPS - 4, readu32(yuv, V_OFF + j * BPS + 4))
|
|
end
|
|
end
|
|
if mby > 0 then
|
|
local t = mbx * 16
|
|
writeu32(yuv, Y_OFF - BPS, readu32(topY, t))
|
|
writeu32(yuv, Y_OFF - BPS + 4, readu32(topY, t + 4))
|
|
writeu32(yuv, Y_OFF - BPS + 8, readu32(topY, t + 8))
|
|
writeu32(yuv, Y_OFF - BPS + 12, readu32(topY, t + 12))
|
|
t = mbx * 8
|
|
writeu32(yuv, U_OFF - BPS, readu32(topU, t))
|
|
writeu32(yuv, U_OFF - BPS + 4, readu32(topU, t + 4))
|
|
writeu32(yuv, V_OFF - BPS, readu32(topV, t))
|
|
writeu32(yuv, V_OFF - BPS + 4, readu32(topV, t + 4))
|
|
end
|
|
if rf > 0 then
|
|
-- 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
|
|
doTransform(band(rshift(bits, 30 - 2 * n), 3), coeffs, co + n * 32, yuv, dst)
|
|
end
|
|
end
|
|
else
|
|
if mbI4[x1] then
|
|
local tr = Y_OFF - BPS + 16
|
|
if mby > 0 then
|
|
if mbx >= mbw - 1 then
|
|
writeu32(yuv, tr, readu8(topY, mbx * 16 + 15) * 0x01010101)
|
|
else
|
|
writeu32(yuv, tr, readu32(topY, mbx * 16 + 16))
|
|
end
|
|
end
|
|
local trv = readu32(yuv, tr)
|
|
writeu32(yuv, tr + 4 * BPS, trv)
|
|
writeu32(yuv, tr + 8 * BPS, trv)
|
|
writeu32(yuv, tr + 12 * BPS, trv)
|
|
for n = 0, 15 do
|
|
local dst = Y_OFF + (n % 4) * 4 + (n // 4) * 4 * BPS
|
|
predict4(yuv, dst, mbModes[mbx * 16 + n + 1])
|
|
doTransform(band(rshift(bits, 30 - 2 * n), 3), coeffs, co + n * 32, yuv, dst)
|
|
end
|
|
else
|
|
predictBlock(yuv, Y_OFF, 16, checkMode(mbx, mby, mbModes[mbx * 16 + 1]))
|
|
if bits ~= 0 then
|
|
for n = 0, 15 do
|
|
local dst = Y_OFF + (n % 4) * 4 + (n // 4) * 4 * BPS
|
|
doTransform(band(rshift(bits, 30 - 2 * n), 3), coeffs, co + n * 32, yuv, dst)
|
|
end
|
|
end
|
|
end
|
|
local uvm = checkMode(mbx, mby, mbUv[x1])
|
|
predictBlock(yuv, U_OFF, 8, uvm)
|
|
predictBlock(yuv, V_OFF, 8, uvm)
|
|
end
|
|
local buv = mbNzUv[x1]
|
|
doUVTransform(buv, coeffs, co + 16 * 32, yuv, U_OFF)
|
|
doUVTransform(rshift(buv, 8), coeffs, co + 20 * 32, yuv, V_OFF)
|
|
if mby < mbh - 1 then
|
|
local t = mbx * 16
|
|
local r = Y_OFF + 15 * BPS
|
|
writeu32(topY, t, readu32(yuv, r))
|
|
writeu32(topY, t + 4, readu32(yuv, r + 4))
|
|
writeu32(topY, t + 8, readu32(yuv, r + 8))
|
|
writeu32(topY, t + 12, readu32(yuv, r + 12))
|
|
t = mbx * 8
|
|
r = U_OFF + 7 * BPS
|
|
writeu32(topU, t, readu32(yuv, r))
|
|
writeu32(topU, t + 4, readu32(yuv, r + 4))
|
|
writeu32(topV, t, readu32(yuv, r + 16))
|
|
writeu32(topV, t + 4, readu32(yuv, r + 20))
|
|
end
|
|
local yo = yb + mby * 16 * ys + mbx * 16
|
|
for j = 0, 15 do
|
|
local d, r = yo + j * ys, Y_OFF + j * BPS
|
|
writeu32(Y, d, readu32(yuv, r))
|
|
writeu32(Y, d + 4, readu32(yuv, r + 4))
|
|
writeu32(Y, d + 8, readu32(yuv, r + 8))
|
|
writeu32(Y, d + 12, readu32(yuv, r + 12))
|
|
end
|
|
local uo = uvb + mby * 8 * uvs + mbx * 8
|
|
for j = 0, 7 do
|
|
local d, r = uo + j * uvs, U_OFF + j * BPS
|
|
writeu32(U, d, readu32(yuv, r))
|
|
writeu32(U, d + 4, readu32(yuv, r + 4))
|
|
writeu32(V, d, readu32(yuv, r + 16))
|
|
writeu32(V, d + 4, readu32(yuv, r + 20))
|
|
end
|
|
end
|
|
end
|
|
|
|
-- loop filter over the whole frame, macroblocks in raster order (libwebp DoFilter)
|
|
if filterType > 0 then
|
|
for mby = 0, mbh - 1 do
|
|
for mbx = 0, mbw - 1 do
|
|
local fi = mby * mbw + mbx + 1
|
|
local limit = fLimit[fi]
|
|
if limit ~= 0 then
|
|
local inner = fInner[fi] == 1
|
|
local yp = yb + mby * 16 * ys + mbx * 16
|
|
if filterType == 1 then
|
|
if mbx > 0 then
|
|
simpleFilter(Y, yp, 1, ys, limit + 4)
|
|
end
|
|
if inner then
|
|
for k = 1, 3 do
|
|
simpleFilter(Y, yp + 4 * k, 1, ys, limit)
|
|
end
|
|
end
|
|
if mby > 0 then
|
|
simpleFilter(Y, yp, ys, 1, limit + 4)
|
|
end
|
|
if inner then
|
|
for k = 1, 3 do
|
|
simpleFilter(Y, yp + 4 * k * ys, ys, 1, limit)
|
|
end
|
|
end
|
|
else
|
|
local il, hev = fIlevel[fi], fHev[fi]
|
|
local up = uvb + mby * 8 * uvs + mbx * 8
|
|
if mbx > 0 then
|
|
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
|
|
filterInner(Y, yp + 4 * k, 1, ys, 16, limit, il, hev)
|
|
end
|
|
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
|
|
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
|
|
filterInner(Y, yp + 4 * k * ys, ys, 1, 16, limit, il, hev)
|
|
end
|
|
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
|
|
end
|
|
end
|
|
end
|
|
|
|
-- the references (libvpx swap_frame_buffers; copies read the references of the previous frame, as ffmpeg does)
|
|
if key then
|
|
st.last, st.golden, st.alt = cur, cur, cur
|
|
st.hasKey = true
|
|
else
|
|
local oldLast, oldGolden, oldAlt = st.last, st.golden, st.alt
|
|
if refreshGolden then
|
|
st.golden = cur
|
|
elseif copyToGolden == 1 then
|
|
st.golden = oldLast
|
|
elseif copyToGolden == 2 then
|
|
st.golden = oldAlt
|
|
end
|
|
if refreshAlt then
|
|
st.alt = cur
|
|
elseif copyToAlt == 1 then
|
|
st.alt = oldLast
|
|
elseif copyToAlt == 2 then
|
|
st.alt = oldGolden
|
|
end
|
|
if refreshLast then
|
|
st.last = cur
|
|
end
|
|
end
|
|
if saved then
|
|
st.proba, st.ymodeProb, st.uvmodeProb, st.mvProb = saved[1], saved[2], saved[3], saved[4]
|
|
end
|
|
return { width = width, height = height, ref = cur, header = hdr }, nil
|
|
end
|
|
|
|
return Decode
|