Files
rosetta/src/Formats/codecs/video/H264/Deblock.luau
T
4DBug 51b08575c4 ws-cleanup: codecs/ grouped by domain; containers/Chunks, codecs/audio/Stream, codecs/image/Pixels, codecs/model/Mat4
C2, the hierarchy (tools/move.py, every reference rewritten mechanically):
- codecs/compress: Deflate Lzma Zstd Brotli Bzip2 Lzw; codecs/entropy: Huffman (from core) RangeCoder;
  codecs/image: Jpeg Dct Vp8 Vp8L Yuv Quantize Raster Blend Pixels; codecs/audio: Mdct Aac Alac Pcm Resample
  VorbisComment Tracker SoundFont Stream; codecs/video: H264 Mpeg1 Video; codecs/text: Json Xml Text Url;
  codecs/font: TrueType Cff; codecs/model: MeshFlatten Mat4. containers/ stays flat; core/ keeps Bytes Bits Err
  Crc32 Adler32 Base64 Md5 Input Sniff.
- LAYERS lint: codecs must sit in a domain folder; gen_registry lists codecs by domain (docs/FORMATS.md).
- Checked: build.py both targets, rojo build of build/roblox, the full suite on both runtimes.

C3, generic code out of format folders:
- containers/Chunks (new): the chunked-file walker (RIFF / RIFX / IFF / PNG / CAF layouts: endianness, size first,
  trailer, padding, lenient missing pad, open-ended ids, size hooks). containers/Riff walks with it (RF64 ds64
  through the resize hook); image/png, audio/aiff and audio/caf list their chunk handlers over it.
- codecs/audio/Stream (new): Stream.packets, the packet-decoder Decoder (read / seek with pre-roll / position,
  priming and end trimming) that aac, caf and m4a each carried (~70 lines each).
- codecs/image/Pixels: image/bmp/Pixels promoted (+ rgb555); image/ico's DIB decoder uses its row loops instead of
  its own (the AND mask / alpha rule stay ICO's).
- codecs/model/Mat4 (new): matrices / quaternions / TRS shared by FBX (Xform keeps only the FBX transform model),
  Collada (Math keeps only <lookat> / <skew>) and USD (its matrix helpers and general 4x4 inverse); Mat4.mul is
  Types.Scene.mul. The Collada normal matrix is now the inverse transpose (it was the inverse; same result for the
  fixtures' bind shapes).
- README rewritten (install for Rojo and LÖVE / LuaJIT, quick start per category, F.* and F.ops, host notes:
  FORMATS_NO_JIT_TUNE, paths, layout); docs/STYLE.md: the codec domains, "what belongs in a format folder" with the
  shared building blocks and the DUP rule, conventions (WAVE order, lower-case tags, loops, the api hooks
  decodeImage / encodeImage / openAudio / plugins / resolve, the codecs/video/Video registry), Err.from, the type
  baseline. New tests: tests/containers/chunks, tests/codecs/mat4. Type baseline 93.
2026-10-31 12:25:03 -05:00

261 lines
8.5 KiB
Luau

--!strict
--!native
--!optimize 2
-- codecs/video/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/video/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 min, max = math.min, math.max
local function clip3(lo: number, hi: number, v: number): number
return min(max(v, lo), hi)
end
local abs = math.abs
-- 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 + (((ap < beta) and (1) or (0))) + (((aq < beta) and (1) or (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 = (((pa >= 0) and (1) or (0))) + (((pb >= 0) and (1) or (0)))
local nq = (((qa >= 0) and (1) or (0))) + (((qb >= 0) and (1) or (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 = ((dir == 0) and (addr - 1) or (addr - mbW))
local edgeOk = my > 0
if dir == 0 then
edgeOk = mx > 0
end
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 = ((e == 0) and (4) or (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