video: yield points in H.264 picture setup and VP8 rows, rgba / scale conversion; disposable packets and skipDisposable

- H264: picture and grid allocations split between yield calls (a 1080p picture's tables are 1.6 M slots)
- Vp8: Decode.decodeFrame hook three times per macroblock row and once per loop filter row; Options.yield
- Yuv.toRgba / Yuv.scale: optional hook every 16 rows; imageToRgba / imageScale take an out Image and the hook
- Video.open passes opts.yield to the conversions, the VP8 and MPEG-1/2 decoders
- codecs/video/Video/Disposable: H.264 nal_ref_idc 0 (avcC length-prefixed / Annex B), VP8 frames refreshing no
  reference; containers/Demux sets packet.disposable for h264 / vp8 tracks; Video.isDisposable(track, packet)
- skipDisposable: H.264 keeps skipped pictures in output order (pts pool / POC bookkeeping), VP8 applies the header
  state then skips; receive() returns false for a skipped picture, VideoFrame.index counts it
- Types: Packet.disposable, VideoCodecDecoder.receive may return false
- tools/fixtures/videoctl.py: VP8 two temporal layers (IVF + WebM)
This commit is contained in:
4DBug committed 2026-11-02 21:35:33 -06:00
1 parent dde6be117c
commit 15e0dfcc86
12 files changed
+469 -74

No files matched your search

+71 -25
View File
@@ -27,6 +27,8 @@ export type Options = {
-- yield is pcall'ed (it can't throw through native code); the entropy decoders keep state in module locals, so no
-- other H.264 decode may run in the VM while one is suspended
yield: (() -> ())?,
-- pictures with nal_ref_idc 0 are not reconstructed: they keep their place in output order as { skipped = true }
skipDisposable: boolean?,
}
function Decoder.new(opts: Options?): any
@@ -76,6 +78,26 @@ local function newPicture(self: any, sps: any): any
local ys = w + 2 * PAD
local cs = w // 2 + 2 * PADC
local n4 = mbW * mbH * 16
-- a 1080p picture's tables are 1.6 M slots: allocated in steps between yields
local hook = self.opts.yield
local y = buffer.create(ys * (h + 2 * PAD))
local u, v = buffer.create(cs * (h // 2 + 2 * PADC)), buffer.create(cs * (h // 2 + 2 * PADC))
local ref0, ref1 = table.create(n4, -1), table.create(n4, -1)
if hook then
pcall(hook)
end
local uid0, uid1 = table.create(n4, -1), table.create(n4, -1)
if hook then
pcall(hook)
end
local mv0 = table.create(n4 * 2, 0)
if hook then
pcall(hook)
end
local mv1 = table.create(n4 * 2, 0)
if hook then
pcall(hook)
end
return {
mbW = mbW,
mbH = mbH,
@@ -83,16 +105,16 @@ local function newPicture(self: any, sps: any): any
cs = cs,
yo = PAD * ys + PAD,
co = PADC * cs + PADC,
y = buffer.create(ys * (h + 2 * PAD)),
u = buffer.create(cs * (h // 2 + 2 * PADC)),
v = buffer.create(cs * (h // 2 + 2 * PADC)),
y = y,
u = u,
v = v,
mbIntra = table.create(mbW * mbH, true),
ref0 = table.create(n4, -1),
ref1 = table.create(n4, -1),
uid0 = table.create(n4, -1),
uid1 = table.create(n4, -1),
mv0 = table.create(n4 * 2, 0),
mv1 = table.create(n4 * 2, 0),
ref0 = ref0,
ref1 = ref1,
uid0 = uid0,
uid1 = uid1,
mv0 = mv0,
mv1 = mv1,
uid = 0,
poc = 0,
frameNum = 0,
@@ -100,12 +122,13 @@ local function newPicture(self: any, sps: any): any
short = false,
longIdx = 0,
waiting = false,
skipped = false,
pts = 0,
}
end
local function release(self: any, p: any)
if not p.short and not p.long and not p.waiting and p ~= self.cur and p ~= self.gray then
if not p.short and not p.long and not p.waiting and p ~= self.cur and p ~= self.gray and not p.skipped then
for _, q in self.pool do
if q == p then
return
@@ -132,6 +155,10 @@ end
local function emit(self: any, p: any)
p.waiting = false
if p.skipped then
table.insert(self.out, { image = nil, skipped = true, pts = p.pts, duration = 0, poc = p.poc, keyframe = false })
return
end
local sps = p.sps
local W, H = sps.width, sps.height
local img = Types.Image.new(W, H, "yuv420p")
@@ -571,7 +598,7 @@ local function setupGrids(self: any, sps: any)
if self.S and self.S.mbW == mbW and self.S.mbH == mbH then
return
end
local S = Mb.grids(mbW, mbH)
local S = Mb.grids(mbW, mbH, self.opts.yield)
S.mbW, S.mbH = mbW, mbH
S.w4 = mbW * 4
S.cw4 = mbW * 2
@@ -643,7 +670,9 @@ local function startPicture(self: any, sh: any, pts: number?)
fillGap(self, sh)
end
end
local p = newPicture(self, sps)
local skip = sh.nalRefIdc == 0 and self.opts.skipDisposable == true
-- a skipped picture only carries what output order and POC need
local p = if skip then { skipped = true, short = false, long = false, waiting = false } else newPicture(self, sps)
self.uid += 1
p.uid = self.uid
p.sps = sps
@@ -660,6 +689,10 @@ local function startPicture(self: any, sh: any, pts: number?)
self.decoded = 0
self.sliceNum = 0
self.sliceParams = {}
if skip then
return
end
p.skipped = false
local S = self.S
local st = S.sliceTab
for i = 1, #st do
@@ -720,20 +753,22 @@ local function finishPicture(self: any)
end
local sh = self.curSh
local S = self.S
if self.decoded < S.mbW * S.mbH then
conceal(self, p)
if not p.skipped then
if self.decoded < S.mbW * S.mbH then
conceal(self, p)
end
local hook = self.opts.yield
if self.opts.deblock ~= false then
Deblock.picture(S, p, self.sliceParams)
end
if hook then
pcall(hook)
end
local w, h = S.mbW * 16, S.mbH * 16
Inter.extend(p.y, p.yo, p.ys, w, h, PAD)
Inter.extend(p.u, p.co, p.cs, w // 2, h // 2, PADC)
Inter.extend(p.v, p.co, p.cs, w // 2, h // 2, PADC)
end
local hook = self.opts.yield
if self.opts.deblock ~= false then
Deblock.picture(S, p, self.sliceParams)
end
if hook then
pcall(hook)
end
local w, h = S.mbW * 16, S.mbH * 16
Inter.extend(p.y, p.yo, p.ys, w, h, PAD)
Inter.extend(p.u, p.co, p.cs, w // 2, h // 2, PADC)
Inter.extend(p.v, p.co, p.cs, w // 2, h // 2, PADC)
local mmco5 = false
if sh.nalRefIdc ~= 0 then
mmco5 = mark(self, p, sh)
@@ -979,6 +1014,10 @@ function Decoder.nal(self: any, buf: buffer, off: number, len: number, pts: numb
end
startPicture(self, sh, pts)
end
if self.cur.skipped then
self.decoded = 1
return
end
decodeSlice(self, sh, r)
if self.decoded >= self.S.mbW * self.S.mbH then
finishPicture(self)
@@ -994,6 +1033,13 @@ function Decoder.nal(self: any, buf: buffer, off: number, len: number, pts: numb
-- SEI (6), filler (12), SPS extension (13), prefix / SVC / MVC (14, 15, 20) and reserved types are ignored
end
-- the end of a packet holding one access unit: a skipped picture is complete
function Decoder.packetEnd(self: any)
if self.cur and self.cur.skipped then
finishPicture(self)
end
end
function Decoder.flush(self: any)
finishPicture(self)
bump(self, 0)