video: frame reuse (opts.reuse, v:release, decoder:release) and the control tests

- codecs/video/Video/Pool: Images a decoder made, handed back and reused (only its own; fresh meta)
- H264 / Vp8 / Mpeg2 output Images come from a Pool; H264 / Vp8 decoder:release, VideoCodecDecoder.release
- Video.open: opts.reuse recycles the previous frame's Image at the next nextFrame; v:release(frame); the yuv Image
  under an rgba / scaled frame (never seen by the caller) goes back to the codec at once
- tests/video/control.luau: yield hook (identical frames, H.264 / VP8 / MPEG-1, conversions), disposable packets in
  every container, skipDisposable bit-exact at the right index, reuse identical and recycled, fuzz
- docs/guide/video.md: Skipping and reusing frames; docs/API.md
This commit is contained in:
4DBug committed 2026-11-02 21:46:59 -06:00
1 parent 15e0dfcc86
commit 0b2d8774b9
10 files changed
+463 -27

No files matched your search

+2
View File
@@ -232,6 +232,8 @@ export type VideoCodecDecoder = {
receive: (self: any) -> (Image? | false, number?, boolean?),
flush: (self: any) -> (), -- end of stream: pictures held for reordering become available to receive()
reset: (self: any) -> (), -- after a seek: drop references / held pictures
-- an Image from receive() handed back for reuse (the caller is done with it); false if it was not this decoder's
release: ((self: any, image: Image) -> boolean)?,
}
-- Indices are 1-based everywhere (node, mesh, material, texture, ...). Accessor data is tightly packed (no stride, no
+9 -3
View File
@@ -7,6 +7,7 @@ local Err = require("@rosetta/core/Err")
local Types = require("@rosetta/Types")
local Decode = require("@rosetta/codecs/image/Vp8/Decode")
local Yuv = require("@rosetta/codecs/image/Vp8/Yuv")
local Pool = require("@rosetta/codecs/video/Video/Pool")
local Vp8 = {}
@@ -52,10 +53,10 @@ end
local DecoderMeta = {}
DecoderMeta.__index = DecoderMeta
export type Decoder = typeof(setmetatable({} :: { state: Decode.State }, DecoderMeta))
export type Decoder = typeof(setmetatable({} :: { state: Decode.State, images: Pool.Pool }, DecoderMeta))
function Vp8.newDecoder(): Decoder
return setmetatable({ state = Decode.new() }, DecoderMeta)
return setmetatable({ state = Decode.new(), images = Pool.new() }, DecoderMeta)
end
function DecoderMeta.decodeFrame(self: Decoder, buf: buffer, off: number?, len: number?, opts: Options?): (Types.Image?, Err.Err?)
@@ -69,7 +70,7 @@ function DecoderMeta.decodeFrame(self: Decoder, buf: buffer, off: number?, len:
elseif f.skipped then
return nil, nil
end
local img = Types.Image.new(f.width, f.height, "yuv420p")
local img = self.images:take(f.width, f.height, "yuv420p")
local ny = f.width * f.height
local nc = ((f.width + 1) // 2) * ((f.height + 1) // 2)
Decode.cropTo(f, img.pixels, 0, img.pixels, ny, img.pixels, ny + nc)
@@ -78,6 +79,11 @@ function DecoderMeta.decodeFrame(self: Decoder, buf: buffer, off: number?, len:
return img, nil
end
-- hands a decoded Image back for reuse by a later frame (the caller is done with it); false if it was not this decoder's
function DecoderMeta.release(self: Decoder, image: Types.Image): boolean
return self.images:give(image)
end
function DecoderMeta.reset(self: Decoder)
Decode.reset(self.state)
end
+15 -8
View File
@@ -12,6 +12,7 @@ local Cavlc = require("@rosetta/codecs/video/H264/Cavlc")
local Cabac = require("@rosetta/codecs/video/H264/Cabac")
local Deblock = require("@rosetta/codecs/video/H264/Deblock")
local Inter = require("@rosetta/codecs/video/H264/Inter")
local Pool = require("@rosetta/codecs/video/Video/Pool")
local Types = require("@rosetta/Types")
local PAD, PADC = Inter.PAD, Inter.PADC
@@ -54,6 +55,7 @@ function Decoder.new(opts: Options?): any
prevFrameNum = 0,
maxLongTermIdx = -1,
pool = {},
images = Pool.new(), -- output Images given back (H264 decoder:release)
gray = nil,
frames = 0,
errors = 0,
@@ -153,15 +155,11 @@ local function grayPicture(self: any, sps: any): any
return g
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
-- the cropped output copy of a decoded picture
local function toImage(self: any, p: any): Types.Image
local sps = p.sps
local W, H = sps.width, sps.height
local img = Types.Image.new(W, H, "yuv420p")
local img = self.images:take(W, H, "yuv420p")
local px = img.pixels
local cx, cy = sps.cropX, sps.cropY
for y = 0, H - 1 do
@@ -178,7 +176,16 @@ local function emit(self: any, p: any)
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 })
return img
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
table.insert(self.out, { image = toImage(self, p), pts = p.pts, duration = 0, poc = p.poc, keyframe = p.idr })
release(self, p)
end
+8
View File
@@ -140,6 +140,11 @@ function DecoderMeta.decode(self: H264Decoder, data: any, pts: number?, off: num
return frames, nil
end
-- hands a frame's Image back for reuse by a later frame (the caller is done with it); false if it was not this decoder's
function DecoderMeta.release(self: H264Decoder, image: Types.Image): boolean
return self.state.images:give(image)
end
function DecoderMeta.flush(self: H264Decoder): { Frame }
Decoder.flush(self.state)
status(self)
@@ -245,6 +250,9 @@ function H264.newVideoDecoder(track: Types.Track, opts: any?): (Types.VideoCodec
push((dec :: H264Decoder):flush())
end
end
function self.release(_: any, image: Types.Image): boolean
return dec ~= nil and (dec :: H264Decoder):release(image)
end
function self.reset(_: any)
dec = make()
table.clear(queue)
+10 -4
View File
@@ -7,6 +7,7 @@
local Types = require("@rosetta/Types")
local Err = require("@rosetta/core/Err")
local D = require("@rosetta/codecs/video/Mpeg2/Decoder")
local Pool = require("@rosetta/codecs/video/Video/Pool")
local Mpeg2 = {}
@@ -248,7 +249,8 @@ function Mpeg2.toRgba(y: buffer, cb: buffer, cr: buffer, codedWidth: number, wid
return true
end
function Mpeg2.frameImage(frame: Frame, rgba: boolean?): Types.Image?
-- pool: where the Image comes from (a recycled one when the caller released one of the right size)
function Mpeg2.frameImage(frame: Frame, rgba: boolean?, pool: Pool.Pool?): Types.Image?
if type(frame) ~= "table" or type(frame.y) ~= "buffer" or type(frame.cb) ~= "buffer" or type(frame.cr) ~= "buffer" then
return nil
end
@@ -264,14 +266,14 @@ function Mpeg2.frameImage(frame: Frame, rgba: boolean?): Types.Image?
end
local img
if rgba then
img = Types.Image.new(w, h)
img = if pool then pool:take(w, h, "rgba8") else Types.Image.new(w, h)
if c422 then
D.toRgba422(frame.y, frame.cb, frame.cr, cw, w, h, img.pixels)
else
D.toRgba(frame.y, frame.cb, frame.cr, cw, w, h, img.pixels)
end
else
img = Types.Image.new(w, h, "yuv420p")
img = if pool then pool:take(w, h, "yuv420p") else Types.Image.new(w, h, "yuv420p")
local px = img.pixels
for y = 0, h - 1 do
buffer.copy(px, y * w, frame.y, y * cw, w)
@@ -319,8 +321,9 @@ end
function Mpeg2.newVideoDecoder(_track: any, opts: any?): (Types.VideoCodecDecoder?, Err.Err?)
local dec = Mpeg2.new({ reorder = true, rgba = false, maxPixels = opts and opts.maxPixels, yield = opts and opts.yield })
local queue: { any } = {}
local images = Pool.new()
local function push(f: Frame)
local img = Mpeg2.frameImage(f)
local img = Mpeg2.frameImage(f, false, images)
if img then
img.meta.yuv = { range = "limited", matrix = "bt601" }
local tag = f.tag
@@ -360,6 +363,9 @@ function Mpeg2.newVideoDecoder(_track: any, opts: any?): (Types.VideoCodecDecode
dec:reset()
table.clear(queue)
end
function self.release(_: any, image: Types.Image): boolean
return images:give(image)
end
return self :: any, nil
end
+50
View File
@@ -0,0 +1,50 @@
--!strict
--!optimize 2
-- codecs/video/Video/Pool: output Images handed back by the caller (decoder:release), reused for later frames. Only
-- Images this pool made are taken back; a reused Image gets a fresh meta table.
local Types = require("@rosetta/Types")
local Pool = {}
Pool.__index = Pool
export type Pool = typeof(setmetatable({} :: { free: { Types.Image }, made: { [Types.Image]: boolean }, max: number }, Pool))
function Pool.new(max: number?): Pool
return setmetatable({ free = {}, made = setmetatable({}, { __mode = "k" }) :: any, max = max or 3 }, Pool)
end
function Pool.take(self: Pool, w: number, h: number, format: Types.PixelFormat): Types.Image
local free = self.free
for i = #free, 1, -1 do
local img = free[i]
if img.width == w and img.height == h and img.format == format then
table.remove(free, i)
img.meta = {}
return img
end
end
local img = Types.Image.new(w, h, format)
self.made[img] = true
return img
end
-- true when the Image was this pool's (it is kept for reuse, or dropped when the pool is full)
function Pool.give(self: Pool, img: any): boolean
if type(img) ~= "table" or not self.made[img] then
return false
end
local free = self.free
for _, q in free do
if q == img then
return true
end
end
if #free < self.max then
table.insert(free, img)
else
self.made[img] = nil
end
return true
end
return Pool
+35 -4
View File
@@ -5,6 +5,7 @@ local Err = require("@rosetta/core/Err")
local Types = require("@rosetta/Types")
local Yuv = require("@rosetta/codecs/image/Yuv")
local Disposable = require("@rosetta/codecs/video/Video/Disposable")
local Pool = require("@rosetta/codecs/video/Video/Pool")
local Video = {}
@@ -203,6 +204,9 @@ local function vp8Decoder(_track: Types.Track, opts: any?): (Types.VideoCodecDec
end
end)
self.skipped = 0
function self.release(_: any, image: Types.Image): boolean
return dec:release(image)
end
return self, nil
end
@@ -373,6 +377,8 @@ export type OpenOptions = {
audio: boolean?,
maxSide: number?,
yield: (() -> ())?,
skipDisposable: boolean?,
reuse: boolean?, -- nextFrame may overwrite the Image of the frame it returned before
[string]: any,
}
@@ -438,10 +444,25 @@ function Video.open(demuxer: Types.Demuxer, opts: OpenOptions?): (Types.VideoDec
v.startTime = start
local defaultDur = if v.frameRate > 0 then 1 / v.frameRate else 0
local lastPts = nil :: number?
-- rgba8 / scaled Images made here; the codec's own go back through codec:release
local own = Pool.new(2)
local function recycle(img: any)
if img and not own:give(img) and codec and codec.release then
codec:release(img)
end
end
local lastFrame: any = nil
function v.release(self: any, frame: any)
recycle(if type(frame) == "table" and frame.kind == "image" then frame else type(frame) == "table" and frame.image)
end
function v.nextFrame(self: any): (Types.VideoFrame?, Err.Err?)
if not codec then
return nil, codecErr
end
if lastFrame then
recycle(lastFrame.image)
lastFrame = nil
end
local dec = codec :: Types.VideoCodecDecoder
while true do
local img, pts, key = dec:receive()
@@ -466,12 +487,21 @@ function Video.open(demuxer: Types.Demuxer, opts: OpenOptions?): (Types.VideoDec
local maxSide = o.maxSide
if maxSide and maxSide >= 1 and img.format == "yuv420p" and math.max(img.width, img.height) > maxSide then
local s = maxSide / math.max(img.width, img.height)
img = Yuv.imageScale(img, math.max(1, math.floor(img.width * s + 0.5)), math.max(1, math.floor(img.height * s + 0.5)), nil, o.yield)
local dw, dh = math.max(1, math.floor(img.width * s + 0.5)), math.max(1, math.floor(img.height * s + 0.5))
local src = img
img = Yuv.imageScale(src, dw, dh, own:take(dw, dh, "yuv420p"), o.yield)
recycle(src) -- (never seen by the caller)
end
if o.rgba then
img = Yuv.imageToRgba(img, nil, o.yield) or img
if o.rgba and img.format == "yuv420p" then
local src = img
img = Yuv.imageToRgba(src, own:take(src.width, src.height, "rgba8"), o.yield) or src
recycle(src)
end
return { image = img, pts = t, duration = d, keyframe = key, index = index }, nil
local frame = { image = img, pts = t, duration = d, keyframe = key, index = index }
if o.reuse then
lastFrame = frame
end
return frame, nil
end
else
local p = demuxer:read(tid)
@@ -491,6 +521,7 @@ function Video.open(demuxer: Types.Demuxer, opts: OpenOptions?): (Types.VideoDec
end
end
function v.seek(self: any, seconds: number)
lastFrame = nil
demuxer:seek(seconds + start)
if codec then
codec:reset()