ws-h264: merge ws/video; codecs/Video registers h264 (avc1/avc3/H264/... FourCCs, V_MPEG4/ISO/AVC) via H264.newVideoDecoder; video/h264 raw Annex B format (demux by access unit, copy-writer from MP4/MKV samples); MMCO 5 resets frame_num (JVT MR2/MR4/MR5); meta.yuv from the VUI

This commit is contained in:
4DBug committed 2026-10-31 10:29:56 -05:00
commit d4486dccce
211 files changed
+43039 -552

No files matched your search

+3
View File
@@ -152,6 +152,7 @@ local function emit(self: any, p: any)
if sps.sarW and sps.sarH then
img.meta.sar = { sps.sarW, sps.sarH }
end
img.meta.yuv = { range = if sps.fullRange then "full" else "limited", matrix = if sps.matrix == 1 then "bt709" else "bt601" }
table.insert(self.out, { image = img, pts = p.pts, duration = 0, poc = p.poc, keyframe = p.idr })
release(self, p)
end
@@ -743,9 +744,11 @@ local function finishPicture(self: any)
bump(self, 0)
end
if mmco5 then
-- 8.2.1: after MMCO 5 the picture counts as frame_num 0 with its POC moved to 0
local t = p.poc
p.topPoc -= t
p.poc = 0
p.frameNum = 0
end
-- POC / frame_num state for the next pictures
if sh.nalRefIdc ~= 0 then
+191
View File
@@ -27,6 +27,7 @@ local Err = require("@formats/core/Err")
local Types = require("@formats/Types")
local Bitstream = require("@formats/codecs/H264/Bitstream")
local Decoder = require("@formats/codecs/H264/Decoder")
local Params = require("@formats/codecs/H264/Params")
local H264 = {}
@@ -195,6 +196,196 @@ function H264.parseAvcC(data: any): (any, Err.Err?)
return cfg, nil
end
-- ------------------------------------------------------------------------------------------------ video layer
-- newVideoDecoder(track, opts?) -> a Types.VideoCodecDecoder (the codecs/Video registry entry for "h264"):
-- track.config = avcC (MP4 / Matroska; packets length-prefixed) or nil (Annex B packets: MPEG-TS, AVI, raw .h264).
-- Pictures come out in display order with the pts of the packet that carried them; a track with
-- `ptsInOutputOrder` (raw Annex B, no timestamps) numbers them in output order instead.
function H264.newVideoDecoder(track: Types.Track, opts: any?): (Types.VideoCodecDecoder?, Err.Err?)
local o: Options = { maxPixels = opts and opts.maxPixels, deblock = opts and opts.deblock, lowDelay = opts and opts.lowDelay }
local function make(): (H264Decoder?, Err.Err?)
local d, err = H264.newDecoder(o)
if d and track.config then
local ok, e = d:configure(track.config)
if not ok then
return nil, e
end
end
return d, err
end
local dec, err = make()
if not dec then
return nil, err
end
local queue: { any } = {}
local outIndex: number? = if track.ptsInOutputOrder then 0 else nil
local self: any = {}
local function push(frames: { Frame })
for _, f in frames do
local pts = f.pts
if track.ptsInOutputOrder then
pts = outIndex or 0
outIndex = pts + 1
end
table.insert(queue, { f.image, pts, f.keyframe })
end
end
function self.decode(_: any, packet: Types.Packet): Err.Err?
if not dec then
return Err.new(Err.CORRUPT, "decoder not available")
end
if outIndex == nil and track.ptsInOutputOrder then
outIndex = packet.pts
end
local frames, e = (dec :: H264Decoder):decode(packet.data, packet.pts)
if not frames then
return e
end
push(frames)
return nil
end
function self.receive(_: any): (Types.Image?, number?, boolean?)
local q = table.remove(queue, 1)
if q then
return q[1], q[2], q[3]
end
return nil
end
function self.flush(_: any)
if dec then
push((dec :: H264Decoder):flush())
end
end
function self.reset(_: any)
dec = make()
table.clear(queue)
if track.ptsInOutputOrder then
outIndex = nil
end
end
return self, nil
end
-- accessUnits(buf, off?, len?) -> { off, len, keyframe, ... } (triples): an Annex B stream cut into access units
-- (7.4.1.2.3: a new unit starts at an AUD, SPS, PPS or SEI after a slice, or at a slice with first_mb_in_slice 0
-- after a slice). keyframe: the unit holds an IDR slice.
function H264.accessUnits(data: any, off: number?, len: number?): { any }
local b = toBuffer(data)
local out = {}
if not b then
return out
end
local n = buffer.len(b)
local o = math.clamp(off or 0, 0, n)
local l = math.clamp(len or (n - o), 0, n - o)
local nals = Bitstream.splitAnnexB(b, o, l)
local start, hasSlice, key = o, false, false
for i = 1, #nals, 2 do
local p, nl = nals[i], nals[i + 1]
local t = buffer.readu8(b, p) % 32
local isSlice = t == 1 or t == 5
local boundary = false
if hasSlice then
if t == 9 or t == 7 or t == 8 or t == 6 or (t >= 14 and t <= 18) then
boundary = true
elseif isSlice and nl > 1 and buffer.readu8(b, p + 1) >= 128 then
boundary = true
end
end
if boundary then
-- the unit ends where this NAL's start code begins
local s = p
while s > start and buffer.readu8(b, s - 1) ~= 1 do
s -= 1
end
s -= 1
while s > start and buffer.readu8(b, s - 1) == 0 do
s -= 1
end
table.insert(out, start)
table.insert(out, s - start)
table.insert(out, key)
start, hasSlice, key = s, false, false
end
if isSlice then
hasSlice = true
if t == 5 then
key = true
end
end
end
if hasSlice and o + l > start then
table.insert(out, start)
table.insert(out, o + l - start)
table.insert(out, key)
end
return out
end
-- toAnnexB(config, sample) -> buffer: a length-prefixed sample (MP4 / Matroska) as Annex B; with config (avcC) the
-- parameter sets are prepended (pass it for the first sample / keyframes). nil, Err on a bad avcC / sample
function H264.toAnnexB(config: buffer?, sample: buffer): (buffer?, Err.Err?)
local parts = {}
local lengthSize = 4
if config then
local cfg, err = Bitstream.parseAvcC(config, 0, buffer.len(config))
if not cfg then
return nil, Err.new(Err.CORRUPT, err or "bad avcC")
end
lengthSize = cfg.lengthSize
for i = 1, #cfg.nals, 2 do
table.insert(parts, "\0\0\0\1" .. buffer.readstring(config, cfg.nals[i], cfg.nals[i + 1]))
end
end
local nals = Bitstream.splitLength(sample, 0, buffer.len(sample), lengthSize)
if not nals then
return nil, Err.new(Err.CORRUPT, "NAL length past the end of the sample")
end
for i = 1, #nals, 2 do
table.insert(parts, "\0\0\0\1" .. buffer.readstring(sample, nals[i], nals[i + 1]))
end
return buffer.fromstring(table.concat(parts)), nil
end
-- info(data) -> { width, height, profile, level, frameRate?, ... } of the first SPS of an Annex B stream or avcC | nil
function H264.info(data: any): any
local b = toBuffer(data)
if not b then
return nil
end
local nals
if buffer.len(b) > 6 and buffer.readu8(b, 0) == 1 then
local cfg = Bitstream.parseAvcC(b, 0, buffer.len(b))
nals = if cfg then cfg.nals else {}
else
nals = Bitstream.splitAnnexB(b, 0, buffer.len(b))
end
for i = 1, #nals, 2 do
if buffer.readu8(b, nals[i]) % 32 == 7 then
local sps = Params.sps(Bitstream.reader(b, nals[i], nals[i + 1], 1))
if sps then
local fps = nil
if sps.unitsInTick and sps.timeScale and sps.unitsInTick > 0 then
fps = sps.timeScale / (2 * sps.unitsInTick)
end
return {
width = sps.width,
height = sps.height,
profile = sps.profile,
level = sps.level,
chromaFormat = sps.chromaFormat,
bitDepth = sps.bitDepth,
interlaced = not sps.frameMbsOnly,
frameRate = fps,
maxRefFrames = sps.maxRefFrames,
numReorderFrames = sps.numReorderFrames,
}
end
end
end
return nil
end
-- planes(img) -> y, u, v offsets in img.pixels and the chroma width / height
function H264.planes(img: Types.Image): (number, number, number, number, number)
local cw, ch = (img.width + 1) // 2, (img.height + 1) // 2