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:
4DBugandClaude Opus 5.5 committed 2026-10-31 09:53:41 -05:00
1 parent 75c677074a
commit 7ae481d4ed
15 files changed
+6343

No files matched your search

+262
View File
@@ -0,0 +1,262 @@
--!strict
--!native
--!optimize 2
-- codecs/H264/Bitstream: NAL unit framing and the RBSP bit reader of ITU-T H.264 (7.3 / 7.4 / 9.1): Annex B start
-- code splitting, AVCC length-prefixed samples, avcC (AVCDecoderConfigurationRecord, ISO/IEC 14496-15 5.3.3)
-- parsing, emulation-prevention removal, and the u(n) / ue(v) / se(v) / te(v) readers. Written from the spec.
-- Never throws: reads past the end of an RBSP return zero bits (the buffer carries 8 zero bytes of padding and every
-- 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
export type Reader = {
buf: buffer, -- the RBSP (emulation prevention removed) followed by 8 zero bytes
pos: number, -- bit position
size: number, -- RBSP length in bits (up to the end of the data, trailing bits included)
stop: number, -- bit position of the rbsp_stop_one_bit (size when there is none)
n: number, -- buffer length in bytes
}
-- ------------------------------------------------------------------------------------------------ framing
-- splitAnnexB(buf, off, len) -> { off1, len1, off2, len2, ... }: NAL unit payload ranges between start codes
-- (00 00 01, with any leading zero bytes); trailing zero bytes of a NAL are dropped.
function Bitstream.splitAnnexB(buf: buffer, off: number, len: number): { number }
local out = {}
local e = off + len
local i = off
local start = -1
while i + 2 < e do
local b2 = readu8(buf, i + 2)
if b2 > 1 then
i += 3
elseif readu8(buf, i + 1) ~= 0 then
i += 2
elseif b2 == 1 and readu8(buf, i) == 0 then
if start >= 0 then
local j = i
while j > start and readu8(buf, j - 1) == 0 do
j -= 1
end
if j > start then
table.insert(out, start)
table.insert(out, j - start)
end
end
start = i + 3
i += 3
else
i += 1
end
end
if start >= 0 then
local j = e
while j > start and readu8(buf, j - 1) == 0 do
j -= 1
end
if j > start then
table.insert(out, start)
table.insert(out, j - start)
end
end
return out
end
-- isAnnexB(buf, off, len) -> true when the data starts with a start code (00 00 01 or 00 00 00 01)
function Bitstream.isAnnexB(buf: buffer, off: number, len: number): boolean
if len >= 3 and readu8(buf, off) == 0 and readu8(buf, off + 1) == 0 then
local b2 = readu8(buf, off + 2)
if b2 == 1 then
return true
end
return b2 == 0 and len >= 4 and readu8(buf, off + 3) == 1
end
return false
end
-- splitLength(buf, off, len, size) -> NAL ranges of a length-prefixed (AVCC / MP4 sample) buffer, or nil when a
-- length runs past the end (the NALs before it are kept in the second result)
function Bitstream.splitLength(buf: buffer, off: number, len: number, size: number): ({ number }?, { number })
local out = {}
local e = off + len
local p = off
while p + size <= e do
local n = 0
for k = 0, size - 1 do
n = n * 256 + readu8(buf, p + k)
end
p += size
if n > e - p then
return nil, out
end
if n > 0 then
table.insert(out, p)
table.insert(out, n)
end
p += n
end
return out, out
end
-- parseAvcC(buf, off, len) -> { lengthSize, profile, level, nals = { off, len, ... } (SPS then PPS, + SPS ext) } |
-- nil, message
function Bitstream.parseAvcC(buf: buffer, off: number, len: number): (any, string?)
if len < 7 then
return nil, "truncated: avcC shorter than 7 bytes"
end
if readu8(buf, off) ~= 1 then
return nil, "avcC configurationVersion is not 1"
end
local cfg = {
profile = readu8(buf, off + 1),
compat = readu8(buf, off + 2),
level = readu8(buf, off + 3),
lengthSize = band(readu8(buf, off + 4), 3) + 1,
nals = {} :: { number },
}
local p = off + 5
local e = off + len
for pass = 1, 2 do
if p >= e then
return nil, "truncated: avcC parameter set count"
end
local count = if pass == 1 then band(readu8(buf, p), 31) else readu8(buf, p)
p += 1
for _ = 1, count do
if p + 2 > e then
return nil, "truncated: avcC parameter set length"
end
local n = readu8(buf, p) * 256 + readu8(buf, p + 1)
p += 2
if p + n > e then
return nil, "truncated: avcC parameter set"
end
table.insert(cfg.nals, p)
table.insert(cfg.nals, n)
p += n
end
end
return cfg, nil
end
-- ------------------------------------------------------------------------------------------------ rbsp
-- reader(buf, off, len, skip) -> Reader over the RBSP of a NAL unit's bytes [off, off + len), the first `skip`
-- bytes (the NAL header) left out; emulation_prevention_three_byte (00 00 03) removed.
function Bitstream.reader(buf: buffer, off: number, len: number, skip: number): Reader
local out = buffer.create(len + 8)
local o = 0
local zeros = 0
for i = off + skip, off + len - 1 do
local b = readu8(buf, i)
if zeros >= 2 and b == 3 then
zeros = 0
else
buffer.writeu8(out, o, b)
o += 1
if b == 0 then
zeros += 1
else
zeros = 0
end
end
end
-- the rbsp_stop_one_bit: the last set bit of the last non-zero byte
local stop = o * 8
local j = o - 1
while j >= 0 and readu8(out, j) == 0 do
j -= 1
end
if j >= 0 then
local b = readu8(out, j)
local k = 0
while band(b, lshift(1, k)) == 0 do
k += 1
end
stop = j * 8 + 7 - k
end
return { buf = out, pos = 0, size = o * 8, stop = stop, n = o + 8 }
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
return 0
end
local b = r.buf
local s = band(pos, 7)
local w = byteswap(readu32(b, p))
if s == 0 then
return w
end
return bor(lshift(w, s), rshift(readu8(b, p + 4), 8 - s))
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)
r.pos += n
if n >= 32 then
return v
end
return rshift(v, 32 - 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)
if p >= r.n then
return 0
end
return band(rshift(readu8(r.buf, p), 7 - band(pos, 7)), 1)
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)
if v == 0 then
r.pos += 32
return 4294967296
end
local lz = countlz(v)
if lz < 16 then
local n = 2 * lz + 1
r.pos += n
return rshift(v, 32 - n) - 1
end
r.pos += lz
v = peek32(r)
r.pos += lz + 1
return rshift(v, 31 - lz) - 1
end
Bitstream.ue = ue
-- se(r) -> se(v)
function Bitstream.se(r: Reader): number
local k = ue(r)
if k % 2 == 1 then
return (k + 1) // 2
end
return -(k // 2)
end
-- te(r, range) -> te(v) with the given range (max value)
function Bitstream.te(r: Reader, range: number): number
if range == 1 then
return 1 - Bitstream.u1(r)
end
return ue(r)
end
-- moreData(r) -> more_rbsp_data()
function Bitstream.moreData(r: Reader): boolean
return r.pos < r.stop
end
return Bitstream