ws-h264: tests (bit-exact vs ffmpeg, AVCC, NAL-at-a-time, unsupported, fuzz), bench lines; LuaJIT speedups (no table-allocating if-expressions, arithmetic bit reads, branch-free CABAC decisions, split MC filters)

This commit is contained in:
4DBug committed 2026-10-31 10:07:39 -05:00
1 parent e53ad35620
commit 6fcf717214
12 files changed
+648 -408

No files matched your search

+34 -40
View File
@@ -18,6 +18,7 @@ local K_I4, K_I8, K_I16, K_PCM, K_P, K_PSKIP, K_B, K_BSKIP, K_BDIRECT = 1, 2, 3,
Mb.K_I4, Mb.K_I8, Mb.K_I16, Mb.K_PCM, Mb.K_P, Mb.K_PSKIP, Mb.K_B, Mb.K_BSKIP, Mb.K_BDIRECT = K_I4, K_I8, K_I16, K_PCM, K_P, K_PSKIP, K_B, K_BSKIP, K_BDIRECT
local UNAVAIL = -2
local min, max = math.min, math.max
local add4, addDc4, add8 = Transform.add4, Transform.addDc4, Transform.add8
local PAD, PADC = Inter.PAD, Inter.PADC
@@ -181,7 +182,7 @@ function Mb.predMode(S: any, M: any, bx: number, by: number): number
local ipm = S.ipm
local g = M.g0 + by * S.w4 + bx
local a, b = ipm[g - 1], ipm[g - S.w4]
return if a < b then a else b
return ((a < b) and (a) or (b))
end
-- ------------------------------------------------------------------------------------------------ motion vectors
@@ -216,21 +217,15 @@ local function neighbour(S: any, M: any, refs: { number }, mvs: { number }, nx:
end
local function median(a: number, b: number, c: number): number
if a > b then
a, b = b, a
end
if b > c then
b = c
end
return if a > b then a else b
return max(min(a, b), min(max(a, b), c))
end
-- mvp(S, M, list, x, y, w, ref, shape) -> mvpLX for the partition at (x, y) (4x4 units) of width w with reference
-- index ref. shape: 0 generic, 1 = 16x8 upper, 2 = 16x8 lower, 3 = 8x16 left, 4 = 8x16 right
local function mvp(S: any, M: any, list: number, x: number, y: number, w: number, ref: number, shape: number): (number, number)
local pic = S.pic
local refs = if list == 0 then pic.ref0 else pic.ref1
local mvs = if list == 0 then pic.mv0 else pic.mv1
local refs = ((list == 0) and (pic.ref0) or (pic.ref1))
local mvs = ((list == 0) and (pic.mv0) or (pic.mv1))
local rA, ax, ay = neighbour(S, M, refs, mvs, x - 1, y)
local rB, bx, by = neighbour(S, M, refs, mvs, x, y - 1)
local rC, cx, cy = neighbour(S, M, refs, mvs, x + w, y - 1)
@@ -283,8 +278,8 @@ Mb.mvp = mvp
local function set(S: any, M: any, x: number, y: number, w: number, h: number, r0: number, m0x: number, m0y: number, r1: number, m1x: number, m1y: number)
local pic = S.pic
local ref0, ref1, mv0, mv1, uid0, uid1 = pic.ref0, pic.ref1, pic.mv0, pic.mv1, pic.uid0, pic.uid1
local u0 = if r0 >= 0 then S.uids0[r0 + 1] or -1 else -1
local u1 = if r1 >= 0 then S.uids1[r1 + 1] or -1 else -1
local u0 = ((r0 >= 0) and (S.uids0[r0 + 1] or -1) or (-1))
local u1 = ((r1 >= 0) and (S.uids1[r1 + 1] or -1) or (-1))
local w4 = S.w4
local done = M.done
for j = y, y + h - 1 do
@@ -308,12 +303,7 @@ end
-- clipping helpers
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
return min(max(v, lo), hi)
end
local function tdiv(a: number, b: number): number
@@ -353,8 +343,8 @@ end
local function colocated(S: any, M: any, bx: number, by: number): (number, number, number, number)
local col = S.colPic
if S.direct8x8 then
bx = if bx < 2 then 0 else 3
by = if by < 2 then 0 else 3
bx = ((bx < 2) and (0) or (3))
by = ((by < 2) and (0) or (3))
end
if col.mbIntra[M.addr + 1] then
return -1, 0, 0, -1
@@ -373,9 +363,9 @@ end
local function minPositive(a: number, b: number): number
if a >= 0 and b >= 0 then
return if a < b then a else b
return ((a < b) and (a) or (b))
end
return if a > b then a else b
return ((a > b) and (a) or (b))
end
-- spatialRefs(S, M): the macroblock-level references and predictors of spatial direct (8.4.1.2.2), cached in M
@@ -383,8 +373,8 @@ local function spatialRefs(S: any, M: any)
local pic = S.pic
local r0, r1 = -1, -1
for l = 0, 1 do
local refs = if l == 0 then pic.ref0 else pic.ref1
local mvs = if l == 0 then pic.mv0 else pic.mv1
local refs = ((l == 0) and (pic.ref0) or (pic.ref1))
local mvs = ((l == 0) and (pic.mv0) or (pic.mv1))
local rA = neighbour(S, M, refs, mvs, -1, 0)
local rB = neighbour(S, M, refs, mvs, 0, -1)
local rC = neighbour(S, M, refs, mvs, 4, -1)
@@ -422,7 +412,7 @@ end
-- direct8(S, M, i8): direct motion of 8x8 quadrant i8 (B_Skip, B_Direct_16x16, B_Direct_8x8)
local function direct8(S: any, M: any, i8: number)
local x0, y0 = (i8 % 2) * 2, (i8 // 2) * 2
local step = if S.direct8x8 then 2 else 1
local step = ((S.direct8x8) and (2) or (1))
if S.directSpatial then
if not M.dReady then
spatialRefs(S, M)
@@ -490,7 +480,7 @@ local function motion(S: any, M: any)
local part = M.part
local mvd0, mvd1 = M.mvd0, M.mvd1
if part < 3 then
local n = if part == 0 then 1 else 2
local n = ((part == 0) and (1) or (2))
for p = 1, n do
local x, y, w, h = 0, 0, 4, 4
local shape = 0
@@ -599,8 +589,8 @@ local function mc(S: any, M: any)
local g = M.g0 + y * S.w4 + x
local r0, r1 = pic.ref0[g], pic.ref1[g]
local m0x, m0y, m1x, m1y = pic.mv0[2 * g - 1], pic.mv0[2 * g], pic.mv1[2 * g - 1], pic.mv1[2 * g]
local ref0 = if r0 >= 0 then refPic(list0, r0) else nil
local ref1 = if r1 >= 0 then refPic(list1, r1) else nil
local ref0 = ((r0 >= 0) and (refPic(list0, r0)) or (nil))
local ref1 = ((r1 >= 0) and (refPic(list1, r1)) or (nil))
if ref0 == nil and ref1 == nil then
ref0 = list0[1] or list1[1]
r0 = 0
@@ -637,20 +627,20 @@ local function mc(S: any, M: any)
if plane == 0 then
predLuma(S, ref0, P0, px, py, lw, lh, m0x, m0y)
else
predChroma(S, ref0, P0, if plane == 1 then ref0.u else ref0.v, cx, cy, cw, ch, m0x, m0y)
predChroma(S, ref0, P0, ((plane == 1) and (ref0.u) or (ref0.v)), cx, cy, cw, ch, m0x, m0y)
end
end
if ref1 then
if plane == 0 then
predLuma(S, ref1, P1, px, py, lw, lh, m1x, m1y)
else
predChroma(S, ref1, P1, if plane == 1 then ref1.u else ref1.v, cx, cy, cw, ch, m1x, m1y)
predChroma(S, ref1, P1, ((plane == 1) and (ref1.u) or (ref1.v)), cx, cy, cw, ch, m1x, m1y)
end
end
if wm == 2 and bi then
Inter.weight2(P0, P1, bw, bh, dst, o, s, 5, implicitW0, implicitW1, 0)
elseif explicit then
local logWD = if plane == 0 then S.lumaDenom else S.chromaDenom
local logWD = ((plane == 0) and (S.lumaDenom) or (S.chromaDenom))
local wi = plane * 2 + 1
if bi then
local b0, b1 = r0 * 8, r1 * 8
@@ -707,11 +697,11 @@ local function chromaResidual(S: any, M: any)
if M.hasDcC then
local dq = S.dq.d4
local intra = M.kind <= K_PCM
Transform.chromaDc(M.dcC, 0, dq[if intra then 2 else 5][M.qpc + 1][1], cc, 0)
Transform.chromaDc(M.dcC, 4, dq[if intra then 3 else 6][M.qpc2 + 1][1], cc, 64)
Transform.chromaDc(M.dcC, 0, dq[((intra) and (2) or (5))][M.qpc + 1][1], cc, 0)
Transform.chromaDc(M.dcC, 4, dq[((intra) and (3) or (6))][M.qpc2 + 1][1], cc, 64)
end
for plane = 0, 1 do
local b = if plane == 0 then pic.u else pic.v
local b = ((plane == 0) and (pic.u) or (pic.v))
for blk = 0, 3 do
local base = (plane * 4 + blk) * 16
local q = o + (blk // 2) * 4 * s + (blk % 2) * 4
@@ -789,7 +779,11 @@ local function intraLuma(S: any, M: any)
end
local tr
if by == 0 then
tr = if bx < 3 then M.iT else M.iTR
if bx < 3 then
tr = M.iT
else
tr = M.iTR
end
else
tr = not (bx == 3 or (bx == 1 and (by == 1 or by == 3)))
end
@@ -836,9 +830,9 @@ function Mb.recon(S: any, M: any)
S.kind[addr] = kind
S.sliceTab[addr] = S.slice
S.t8Tab[addr] = M.t8
S.qpTab[addr] = if kind == K_PCM then 0 else M.qp
S.qpcTab[addr] = if kind == K_PCM then S.qpcOf[1] else M.qpc
S.qpc2Tab[addr] = if kind == K_PCM then S.qpc2Of[1] else M.qpc2
S.qpTab[addr] = ((kind == K_PCM) and (0) or (M.qp))
S.qpcTab[addr] = ((kind == K_PCM) and (S.qpcOf[1]) or (M.qpc))
S.qpc2Tab[addr] = ((kind == K_PCM) and (S.qpc2Of[1]) or (M.qpc2))
pic.mbIntra[addr] = kind <= K_PCM
local nzDb = S.nzDb
local g0 = M.g0
@@ -878,13 +872,13 @@ function Mb.recon(S: any, M: any)
if M.t8 == 1 then
for i8 = 0, 3 do
local bx, by = (i8 % 2) * 2, (i8 // 2) * 2
local v = if nz[by * 4 + bx + 1] > 0 then 1 else 0
local v = ((nz[by * 4 + bx + 1] > 0) and (1) or (0))
local g = g0 + by * w4 + bx
nzDb[g], nzDb[g + 1], nzDb[g + w4], nzDb[g + w4 + 1] = v, v, v, v
end
else
for blk = 0, 15 do
nzDb[g0 + (blk // 4) * w4 + blk % 4] = if nz[blk + 1] > 0 then 1 else 0
nzDb[g0 + (blk // 4) * w4 + blk % 4] = ((nz[blk + 1] > 0) and (1) or (0))
end
end
if M.cbp % 16 ~= 0 then