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

+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