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)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
4DBugandClaude Opus 5.5 committed 2026-10-31 10:07:39 -05:00
1 parent 7ae481d4ed
commit c848c736ba
12 files changed
+648 -408

No files matched your search

+54 -23
View File
@@ -8,8 +8,8 @@
-- read is range-checked), and callers check `r.pos > r.size` once per syntax structure.
local Bitstream = {}
local readu8, readu32 = buffer.readu8, buffer.readu32
local band, lshift, rshift, bor, countlz, byteswap = bit32.band, bit32.lshift, bit32.rshift, bit32.bor, bit32.countlz, bit32.byteswap
local readu8 = buffer.readu8
local band, lshift = bit32.band, bit32.lshift
export type Reader = {
buf: buffer, -- the RBSP (emulation prevention removed) followed by 8 zero bytes
@@ -179,61 +179,92 @@ function Bitstream.reader(buf: buffer, off: number, len: number, skip: number):
end
-- ------------------------------------------------------------------------------------------------ bits
-- peek32(r) -> the next 32 bits (zeros past the end)
local function peek32(r: Reader): number
local pos = r.pos
local p = rshift(pos, 3)
if p + 5 > r.n then
-- Plain arithmetic instead of bit32 (LuaJIT runs bit32 through compat wrappers whose sign fix-ups split traces).
local P2 = table.create(33, 1) -- P2[k + 1] = 2^k
for k = 0, 32 do
P2[k + 1] = 2 ^ k
end
Bitstream.P2 = P2
local LZ8 = table.create(256, 0) -- LZ8[x + 1] = leading zero bits of the byte x (8 for 0)
for x = 0, 255 do
local n, v = 0, x
while n < 8 and v < 128 do
v *= 2
n += 1
end
LZ8[x + 1] = n
end
Bitstream.LZ8 = LZ8
-- clz32(v) -> leading zero bits of a 32-bit value
local function clz32(v: number): number
if v >= 16777216 then
return LZ8[v // 16777216 + 1]
elseif v >= 65536 then
return 8 + LZ8[v // 65536 + 1]
elseif v >= 256 then
return 16 + LZ8[v // 256 + 1]
end
return 24 + LZ8[v + 1]
end
Bitstream.clz32 = clz32
-- peekAt(b, n, pos) -> the 32 bits at bit position pos of buffer b (n bytes), zeros past the end
local function peekAt(b: buffer, n: number, pos: number): number
local p = pos // 8
if p + 5 > n then
return 0
end
local b = r.buf
local s = band(pos, 7)
local w = byteswap(readu32(b, p))
local s = pos - p * 8
local w = readu8(b, p) * 16777216 + readu8(b, p + 1) * 65536 + readu8(b, p + 2) * 256 + readu8(b, p + 3)
if s == 0 then
return w
end
return bor(lshift(w, s), rshift(readu8(b, p + 4), 8 - s))
return (w * P2[s + 1]) % 4294967296 + readu8(b, p + 4) // P2[9 - s]
end
Bitstream.peekAt = peekAt
-- peek32(r) -> the next 32 bits (zeros past the end)
local function peek32(r: Reader): number
return peekAt(r.buf, r.n, r.pos)
end
Bitstream.peek32 = peek32
-- u(r, n) -> n bits (n 1..32) as an unsigned number
function Bitstream.u(r: Reader, n: number): number
local v = peek32(r)
local v = peekAt(r.buf, r.n, r.pos)
r.pos += n
if n >= 32 then
return v
end
return rshift(v, 32 - n)
return v // P2[33 - n]
end
-- u1(r) -> one bit
function Bitstream.u1(r: Reader): number
local pos = r.pos
r.pos = pos + 1
local p = rshift(pos, 3)
local p = pos // 8
if p >= r.n then
return 0
end
return band(rshift(readu8(r.buf, p), 7 - band(pos, 7)), 1)
return (readu8(r.buf, p) // P2[8 - (pos - p * 8)]) % 2
end
-- ue(r) -> ue(v); a code with more than 31 leading zeros returns 2^32 (out of range for every syntax element)
local function ue(r: Reader): number
local v = peek32(r)
local v = peekAt(r.buf, r.n, r.pos)
if v == 0 then
r.pos += 32
return 4294967296
end
local lz = countlz(v)
local lz = clz32(v)
if lz < 16 then
local n = 2 * lz + 1
r.pos += n
return rshift(v, 32 - n) - 1
return v // P2[33 - n] - 1
end
r.pos += lz
v = peek32(r)
v = peekAt(r.buf, r.n, r.pos)
r.pos += lz + 1
return rshift(v, 31 - lz) - 1
return v // P2[32 - lz] - 1
end
Bitstream.ue = ue