codecs: H.264 / Yuv / video registry work, docs/API + guide/video, yuv + mp4 tests (uncommitted work found in the checkout, owner unknown; tests pass on both runtimes)

This commit is contained in:
4DBug committed 2026-11-01 01:16:04 -05:00
1 parent 05eedbc8cf
commit 2956576d7f
12 files changed
+252 -3

No files matched your search

+63
View File
@@ -10,6 +10,8 @@
-- Yuv.toRgba(planes, w, h, uo, vo, cw, range?, matrix?, out?) -> rgba8 buffer (alpha 255)
-- Yuv.fromRgba(rgba, w, h, range?, matrix?, out?) -> yuv420p buffer (Y, then U, then V; ceil(w/2) x ceil(h/2))
-- Yuv.imageToRgba(img) / Yuv.imageFromRgba(img, range?, matrix?) -> Types.Image
-- Yuv.scale(src, w, h, dw, dh, out?) -> yuv420p buffer of dw x dh (each plane bilinear; for shrinking by up to 2x per
-- axis, larger factors skip source samples); Yuv.imageScale(img, dw, dh) -> Types.Image (yuv420p in and out)
local Types = require("@formats/Types")
local readu8, writeu8, writeu32 = buffer.readu8, buffer.writeu8, buffer.writeu32
@@ -222,4 +224,65 @@ function Yuv.imageFromRgba(img: Types.Image, range: string?, matrix: string?): T
return out
end
-- one plane (sw x sh bytes at so) -> (dw x dh at do), bilinear in 8-bit fixed point (source sample centres)
local function scalePlane(src: buffer, so: number, sw: number, sh: number, dst: buffer, d0: number, dw: number, dh: number)
local fx, fy = sw / dw, sh / dh
for y = 0, dh - 1 do
local sy = (y + 0.5) * fy - 0.5
if sy < 0 then
sy = 0
end
local y0 = floor(sy)
if y0 > sh - 1 then
y0 = sh - 1
end
local y1 = if y0 < sh - 1 then y0 + 1 else y0
local wy = floor((sy - y0) * 256)
local r0, r1 = so + y0 * sw, so + y1 * sw
local o = d0 + y * dw
for x = 0, dw - 1 do
local sx = (x + 0.5) * fx - 0.5
if sx < 0 then
sx = 0
end
local x0 = floor(sx)
if x0 > sw - 1 then
x0 = sw - 1
end
local x1 = if x0 < sw - 1 then x0 + 1 else x0
local wx = floor((sx - x0) * 256)
local a = readu8(src, r0 + x0) * (256 - wx) + readu8(src, r0 + x1) * wx
local b = readu8(src, r1 + x0) * (256 - wx) + readu8(src, r1 + x1) * wx
writeu8(dst, o + x, (a * (256 - wy) + b * wy + 32768) // 65536)
end
end
end
-- scale(src, w, h, dw, dh, out?) -> a yuv420p buffer of dw x dh (planes Y, U, V; chroma ceil(dw / 2) x ceil(dh / 2)).
-- src must hold w x h yuv420p (checked: a short buffer or bad sizes return a zero-filled result of the right size).
function Yuv.scale(src: buffer, w: number, h: number, width: number, height: number, out: buffer?): buffer
local dw: number = math.max(1, floor(width))
local dh: number = math.max(1, floor(height))
local cw: number = (w + 1) // 2
local ch: number = (h + 1) // 2
local dcw: number = (dw + 1) // 2
local dch: number = (dh + 1) // 2
local size = dw * dh + 2 * dcw * dch
local dst = if out and buffer.len(out) >= size then out else bcreate(size)
if w < 1 or h < 1 or buffer.len(src) < w * h + 2 * cw * ch then
return dst
end
scalePlane(src, 0, w, h, dst, 0, dw, dh)
scalePlane(src, w * h, cw, ch, dst, dw * dh, dcw, dch)
scalePlane(src, w * h + cw * ch, cw, ch, dst, dw * dh + dcw * dch, dcw, dch)
return dst
end
function Yuv.imageScale(img: Types.Image, dw: number, dh: number): Types.Image
local out = Types.Image.new(math.max(1, floor(dw)), math.max(1, floor(dh)), "yuv420p")
Yuv.scale(img.pixels, img.width, img.height, out.width, out.height, out.pixels)
out.meta = table.clone(img.meta or {})
return out
end
return Yuv
+5
View File
@@ -1110,6 +1110,8 @@ function Cabac.slice(S: any, M: any, r: Bitstream.Reader): (boolean, string?)
local total = S.mbW * S.mbH
local st = S.type
local skipKind = ((st == 1) and (K_BSKIP) or (K_PSKIP))
local hook = S.hook
local every = Mb.YIELD_MBS
while true do
if addr >= total then
return false, "slice data past the end of the picture"
@@ -1125,6 +1127,9 @@ function Cabac.slice(S: any, M: any, r: Bitstream.Reader): (boolean, string?)
end
end
addr += 1
if hook and addr % every == 0 then
pcall(hook) -- (may yield: the engine state lives in this module's locals, see Decoder Options.yield)
end
if epos > elimit + 8 then
return false, "truncated: slice data"
end
+5
View File
@@ -735,6 +735,8 @@ function Cavlc.slice(S: any, M: any, r: Bitstream.Reader): (boolean, string?)
local st = S.type
local skipKind = ((st == 1) and (K_BSKIP) or (K_PSKIP))
local more = true
local hook = S.hook
local every = Mb.YIELD_MBS
repeat
if st ~= 2 then
local run = ue(r)
@@ -760,6 +762,9 @@ function Cavlc.slice(S: any, M: any, r: Bitstream.Reader): (boolean, string?)
return false, err
end
addr += 1
if hook and addr % every == 0 then
pcall(hook)
end
if r.pos > r.size then
return false, "truncated: slice data"
end
@@ -164,7 +164,11 @@ function Deblock.picture(S: any, pic: any, params: { any })
local nzDb = S.nzDb
local ys, cs = pic.ys, pic.cs
local Y, U, V = pic.y, pic.u, pic.v
local hook = S.hook
for my = 0, mbH - 1 do
if hook and my > 0 then
pcall(hook)
end
for mx = 0, mbW - 1 do
local addr = my * mbW + mx + 1
local sn = sliceTab[addr]
@@ -26,6 +26,11 @@ export type Options = {
maxPixels: number?, -- largest picture accepted (default 2^22: 4096 x 1024)
lowDelay: boolean?, -- output pictures in decoding order without reordering (only right for streams without B frames)
deblock: boolean?, -- false skips the loop filter (not conformant: faster, for previews); default true
-- a cooperative yield point, called (pcall'ed: it can't throw through the native code) every YIELD_MBS macroblocks
-- of slice data and every macroblock row of the loop filter: e.g. a function that calls coroutine.yield() once a time
-- budget is spent, to spread one picture over several frames. While a decode is suspended, no other H.264 decode may
-- run in the same Luau VM (the entropy decoders keep their state in module locals); default nil (no calls)
yield: (() -> ())?,
}
-- new(opts) -> decoder state
@@ -673,6 +678,7 @@ local function startPicture(self: any, sh: any, pts: number?)
st[i] = 0
end
S.pic = p
S.hook = self.opts.yield
end
local function conceal(self: any, p: any)
@@ -729,9 +735,13 @@ local function finishPicture(self: any)
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)
+1
View File
@@ -12,6 +12,7 @@ local Transform = require("@formats/codecs/video/H264/Transform")
local Inter = require("@formats/codecs/video/H264/Inter")
local Mb = {}
Mb.YIELD_MBS = 16 -- macroblocks between calls of the cooperative yield hook (S.hook: Decoder Options.yield)
-- macroblock kinds (S.kind per macroblock)
local K_I4, K_I8, K_I16, K_PCM, K_P, K_PSKIP, K_B, K_BSKIP, K_BDIRECT = 1, 2, 3, 4, 5, 6, 7, 8, 9
+3 -1
View File
@@ -41,6 +41,7 @@ export type Options = {
maxPixels: number?, -- largest picture (default 2^22 luma samples)
lowDelay: boolean?, -- output in decoding order, no reordering delay (streams without B frames / reordering)
deblock: boolean?, -- false skips the deblocking filter (faster, not conformant); default true
yield: (() -> ())?, -- a cooperative yield point (see Decoder Options.yield): pcall'ed every 16 macroblocks
}
export type Frame = {
@@ -206,7 +207,8 @@ end
-- Pictures come out in display order with the packet timestamps sorted (display order); 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 o: Options = { maxPixels = opts and opts.maxPixels, deblock = opts and opts.deblock, lowDelay = opts and opts.lowDelay,
yield = opts and opts.yield }
local function make(): (H264Decoder?, Err.Err?)
local d, err = H264.newDecoder(o)
if d and track.config then
+14
View File
@@ -384,6 +384,11 @@ export type OpenOptions = {
maxPixels: number?,
openAudio: ((demuxer: Types.Demuxer, track: Types.Track) -> (Types.Decoder?, Err.Err?))?, -- the api's audio hook
audio: boolean?, -- false: do not open the audio track
-- shrink yuv420p frames so the longer side is at most this many pixels (aspect kept; bilinear, Yuv.scale), before
-- any rgba conversion. v.width / v.height stay the source size; frame.image has the output size. Default: none
maxSide: number?,
-- passed to codecs that support a cooperative yield point (H.264: codecs/video/H264 Options.yield)
yield: (() -> ())?,
[string]: any,
}
@@ -474,6 +479,15 @@ function Video.open(demuxer: Types.Demuxer, opts: OpenOptions?): (Types.VideoDec
d = t - (lastPts :: number)
end
lastPts = t
local maxSide = o.maxSide
if maxSide and maxSide >= 1 and img.format == "yuv420p" and math.max(img.width, img.height) > maxSide then
local hook: any = o.yield
if hook then
pcall(hook)
end
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)))
end
if o.rgba then
img = Yuv.imageToRgba(img) or img
end