ws-h264: codecs/H264 decoder (CAVLC + CABAC, I/P/B, 8x8, weighted, direct, deblock, DPB/POC/reorder); fixtures from libx264 vs ffmpeg
Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
75c677074a
commit
7ae481d4ed
15 files changed
+6343
No files matched your search
@@ -0,0 +1,263 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- codecs/H264/Deblock: the in-loop deblocking filter of ITU-T H.264 8.7 for frame macroblocks, 8-bit 4:2:0:
|
||||
-- boundary strengths (8.7.2.1, from the per-4x4 intra / coefficient / reference / motion grids), alpha / beta / tC0
|
||||
-- (tables 8-16 / 8-17), the bS < 4 and bS = 4 luma and chroma filters, disable_deblocking_filter_idc 0 / 1 / 2 and the
|
||||
-- slice alpha / beta offsets. Written from the spec. Runs over the whole picture in macroblock order once all its
|
||||
-- slices are decoded (the result equals filtering each macroblock as it is decoded).
|
||||
-- Never throws.
|
||||
local Tables = require("@formats/codecs/H264/Tables")
|
||||
|
||||
local readu8, writeu8 = buffer.readu8, buffer.writeu8
|
||||
local ALPHA, BETA, TC0 = Tables.ALPHA, Tables.BETA, Tables.TC0
|
||||
|
||||
local Deblock = {}
|
||||
|
||||
local BS = table.create(4, 0)
|
||||
|
||||
local function clip3(lo: number, hi: number, v: number): number
|
||||
if v < lo then
|
||||
return lo
|
||||
elseif v > hi then
|
||||
return hi
|
||||
end
|
||||
return v
|
||||
end
|
||||
|
||||
local function abs(v: number): number
|
||||
return if v < 0 then -v else v
|
||||
end
|
||||
|
||||
-- luma(b, o, across, along, qp, aOff, bOff): filters the 16 samples of one luma edge; `across` steps from q0 to q1
|
||||
-- (1 for vertical edges, the stride for horizontal ones), `along` to the next sample of the edge; BS holds the four
|
||||
-- strengths
|
||||
local function luma(b: buffer, o: number, across: number, along: number, qp: number, aOff: number, bOff: number)
|
||||
local indexA = clip3(0, 51, qp + aOff)
|
||||
local alpha = ALPHA[indexA + 13]
|
||||
local beta = BETA[clip3(0, 51, qp + bOff) + 13]
|
||||
if alpha == 0 or beta == 0 then
|
||||
return
|
||||
end
|
||||
for seg = 0, 3 do
|
||||
local bS = BS[seg + 1]
|
||||
if bS > 0 then
|
||||
local tc0 = TC0[(indexA + 12) * 4 + bS + 1]
|
||||
for k = 0, 3 do
|
||||
local q = o + (seg * 4 + k) * along
|
||||
local p0 = readu8(b, q - across)
|
||||
local q0 = readu8(b, q)
|
||||
local p1 = readu8(b, q - 2 * across)
|
||||
local q1 = readu8(b, q + across)
|
||||
if abs(p0 - q0) < alpha and abs(p1 - p0) < beta and abs(q1 - q0) < beta then
|
||||
local p2 = readu8(b, q - 3 * across)
|
||||
local q2 = readu8(b, q + 2 * across)
|
||||
local ap = abs(p2 - p0)
|
||||
local aq = abs(q2 - q0)
|
||||
if bS < 4 then
|
||||
local tc = tc0 + (if ap < beta then 1 else 0) + (if aq < beta then 1 else 0)
|
||||
local delta = clip3(-tc, tc, ((q0 - p0) * 4 + (p1 - q1) + 4) // 8)
|
||||
writeu8(b, q - across, clip3(0, 255, p0 + delta))
|
||||
writeu8(b, q, clip3(0, 255, q0 - delta))
|
||||
if ap < beta then
|
||||
writeu8(b, q - 2 * across, p1 + clip3(-tc0, tc0, (p2 + (p0 + q0 + 1) // 2 - 2 * p1) // 2))
|
||||
end
|
||||
if aq < beta then
|
||||
writeu8(b, q + across, q1 + clip3(-tc0, tc0, (q2 + (p0 + q0 + 1) // 2 - 2 * q1) // 2))
|
||||
end
|
||||
else
|
||||
local strong = abs(p0 - q0) < alpha // 4 + 2
|
||||
if ap < beta and strong then
|
||||
local p3 = readu8(b, q - 4 * across)
|
||||
writeu8(b, q - across, (p2 + 2 * p1 + 2 * p0 + 2 * q0 + q1 + 4) // 8)
|
||||
writeu8(b, q - 2 * across, (p2 + p1 + p0 + q0 + 2) // 4)
|
||||
writeu8(b, q - 3 * across, (2 * p3 + 3 * p2 + p1 + p0 + q0 + 4) // 8)
|
||||
else
|
||||
writeu8(b, q - across, (2 * p1 + p0 + q1 + 2) // 4)
|
||||
end
|
||||
if aq < beta and strong then
|
||||
local q3 = readu8(b, q + 3 * across)
|
||||
writeu8(b, q, (p1 + 2 * p0 + 2 * q0 + 2 * q1 + q2 + 4) // 8)
|
||||
writeu8(b, q + across, (p0 + q0 + q1 + q2 + 2) // 4)
|
||||
writeu8(b, q + 2 * across, (2 * q3 + 3 * q2 + q1 + q0 + p0 + 4) // 8)
|
||||
else
|
||||
writeu8(b, q, (2 * q1 + q0 + p1 + 2) // 4)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- chroma(b, o, across, along, qp, aOff, bOff): the 8 samples of one chroma edge (bS of luma segment i // 2)
|
||||
local function chroma(b: buffer, o: number, across: number, along: number, qp: number, aOff: number, bOff: number)
|
||||
local indexA = clip3(0, 51, qp + aOff)
|
||||
local alpha = ALPHA[indexA + 13]
|
||||
local beta = BETA[clip3(0, 51, qp + bOff) + 13]
|
||||
if alpha == 0 or beta == 0 then
|
||||
return
|
||||
end
|
||||
for i = 0, 7 do
|
||||
local bS = BS[i // 2 + 1]
|
||||
if bS > 0 then
|
||||
local q = o + i * along
|
||||
local p0 = readu8(b, q - across)
|
||||
local q0 = readu8(b, q)
|
||||
local p1 = readu8(b, q - 2 * across)
|
||||
local q1 = readu8(b, q + across)
|
||||
if abs(p0 - q0) < alpha and abs(p1 - p0) < beta and abs(q1 - q0) < beta then
|
||||
if bS < 4 then
|
||||
local tc = TC0[(indexA + 12) * 4 + bS + 1] + 1
|
||||
local delta = clip3(-tc, tc, ((q0 - p0) * 4 + (p1 - q1) + 4) // 8)
|
||||
writeu8(b, q - across, clip3(0, 255, p0 + delta))
|
||||
writeu8(b, q, clip3(0, 255, q0 - delta))
|
||||
else
|
||||
writeu8(b, q - across, (2 * p1 + p0 + q1 + 2) // 4)
|
||||
writeu8(b, q, (2 * q1 + q0 + p1 + 2) // 4)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- motionDiffers(pic, p, q) -> true when the 4x4 blocks p and q (grid indices) need bS 1 (8.7.2.1)
|
||||
local function mvDiff(mvA: { number }, a: number, mvB: { number }, b: number): boolean
|
||||
return abs(mvA[2 * a - 1] - mvB[2 * b - 1]) >= 4 or abs(mvA[2 * a] - mvB[2 * b]) >= 4
|
||||
end
|
||||
|
||||
local function motionDiffers(pic: any, p: number, q: number): boolean
|
||||
local uid0, uid1, mv0, mv1 = pic.uid0, pic.uid1, pic.mv0, pic.mv1
|
||||
local pa, pb, qa, qb = uid0[p], uid1[p], uid0[q], uid1[q]
|
||||
local np = (if pa >= 0 then 1 else 0) + (if pb >= 0 then 1 else 0)
|
||||
local nq = (if qa >= 0 then 1 else 0) + (if qb >= 0 then 1 else 0)
|
||||
if np ~= nq then
|
||||
return true
|
||||
end
|
||||
if np == 1 then
|
||||
local pr, pm = pa, mv0
|
||||
if pa < 0 then
|
||||
pr, pm = pb, mv1
|
||||
end
|
||||
local qr, qm = qa, mv0
|
||||
if qa < 0 then
|
||||
qr, qm = qb, mv1
|
||||
end
|
||||
if pr ~= qr then
|
||||
return true
|
||||
end
|
||||
return mvDiff(pm, p, qm, q)
|
||||
elseif np == 0 then
|
||||
return false
|
||||
end
|
||||
if not ((pa == qa and pb == qb) or (pa == qb and pb == qa)) then
|
||||
return true
|
||||
end
|
||||
if pa ~= pb then
|
||||
if pa == qa then
|
||||
return mvDiff(mv0, p, mv0, q) or mvDiff(mv1, p, mv1, q)
|
||||
end
|
||||
return mvDiff(mv0, p, mv1, q) or mvDiff(mv1, p, mv0, q)
|
||||
end
|
||||
return (mvDiff(mv0, p, mv0, q) or mvDiff(mv1, p, mv1, q)) and (mvDiff(mv0, p, mv1, q) or mvDiff(mv1, p, mv0, q))
|
||||
end
|
||||
|
||||
-- picture(S, pic, params): deblocks the whole picture. params[sliceNum] = { idc, alphaOffset, betaOffset }
|
||||
function Deblock.picture(S: any, pic: any, params: { any })
|
||||
local mbW, mbH = S.mbW, S.mbH
|
||||
local w4 = S.w4
|
||||
local kind, sliceTab, qpTab, qpcTab, qpc2Tab, t8Tab = S.kind, S.sliceTab, S.qpTab, S.qpcTab, S.qpc2Tab, S.t8Tab
|
||||
local nzDb = S.nzDb
|
||||
local ys, cs = pic.ys, pic.cs
|
||||
local Y, U, V = pic.y, pic.u, pic.v
|
||||
for my = 0, mbH - 1 do
|
||||
for mx = 0, mbW - 1 do
|
||||
local addr = my * mbW + mx + 1
|
||||
local sn = sliceTab[addr]
|
||||
local prm = params[sn]
|
||||
if sn == 0 or not prm or prm[1] == 1 then
|
||||
continue
|
||||
end
|
||||
local idc, aOff, bOff = prm[1], prm[2], prm[3]
|
||||
local intraQ = kind[addr] <= 4
|
||||
local t8 = t8Tab[addr] == 1
|
||||
local qpQ, qpcQ, qpc2Q = qpTab[addr], qpcTab[addr], qpc2Tab[addr]
|
||||
local g0 = my * 4 * w4 + mx * 4 + 1
|
||||
local lo = pic.yo + my * 16 * ys + mx * 16
|
||||
local co = pic.co + my * 8 * cs + mx * 8
|
||||
for dir = 0, 1 do
|
||||
-- dir 0: vertical edges (left neighbour), dir 1: horizontal edges (top neighbour)
|
||||
local nAddr = if dir == 0 then addr - 1 else addr - mbW
|
||||
local edgeOk = if dir == 0 then mx > 0 else my > 0
|
||||
if edgeOk and idc == 2 and sliceTab[nAddr] ~= sn then
|
||||
edgeOk = false
|
||||
end
|
||||
if edgeOk and sliceTab[nAddr] == 0 then
|
||||
edgeOk = false
|
||||
end
|
||||
for e = 0, 3 do
|
||||
if e == 0 and not edgeOk then
|
||||
continue
|
||||
end
|
||||
if t8 and (e == 1 or e == 3) then
|
||||
continue
|
||||
end
|
||||
-- strengths of the four segments
|
||||
local intraP = intraQ
|
||||
local qpP, qpcP, qpc2P = qpQ, qpcQ, qpc2Q
|
||||
if e == 0 then
|
||||
intraP = kind[nAddr] <= 4
|
||||
qpP, qpcP, qpc2P = qpTab[nAddr], qpcTab[nAddr], qpc2Tab[nAddr]
|
||||
end
|
||||
local any = false
|
||||
for k = 0, 3 do
|
||||
local qg, pg
|
||||
if dir == 0 then
|
||||
qg = g0 + k * w4 + e
|
||||
pg = qg - 1
|
||||
else
|
||||
qg = g0 + e * w4 + k
|
||||
pg = qg - w4
|
||||
end
|
||||
local bS
|
||||
if intraP or intraQ then
|
||||
bS = if e == 0 then 4 else 3
|
||||
elseif nzDb[qg] ~= 0 or nzDb[pg] ~= 0 then
|
||||
bS = 2
|
||||
elseif motionDiffers(pic, pg, qg) then
|
||||
bS = 1
|
||||
else
|
||||
bS = 0
|
||||
end
|
||||
BS[k + 1] = bS
|
||||
if bS > 0 then
|
||||
any = true
|
||||
end
|
||||
end
|
||||
if not any then
|
||||
continue
|
||||
end
|
||||
local qpav = (qpP + qpQ + 1) // 2
|
||||
if dir == 0 then
|
||||
luma(Y, lo + e * 4, 1, ys, qpav, aOff, bOff)
|
||||
else
|
||||
luma(Y, lo + e * 4 * ys, ys, 1, qpav, aOff, bOff)
|
||||
end
|
||||
if e % 2 == 0 then
|
||||
local qc = (qpcP + qpcQ + 1) // 2
|
||||
local qc2 = (qpc2P + qpc2Q + 1) // 2
|
||||
if dir == 0 then
|
||||
chroma(U, co + e * 2, 1, cs, qc, aOff, bOff)
|
||||
chroma(V, co + e * 2, 1, cs, qc2, aOff, bOff)
|
||||
else
|
||||
chroma(U, co + e * 2 * cs, cs, 1, qc, aOff, bOff)
|
||||
chroma(V, co + e * 2 * cs, cs, 1, qc2, aOff, bOff)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return Deblock
|
||||
Reference in new issue
Block a user