codecs/video: comments to specs only

This commit is contained in:
4DBug committed 2026-11-01 01:54:40 -05:00
1 parent 0bd7b87d2b
commit 8e78869f0e
36 files changed
+219 -809

No files matched your search

+10 -27
View File
@@ -1,10 +1,7 @@
--!strict
-- codecs/video/H264/Decoder: the decoding process above the macroblock layer (ITU-T H.264 8.1, 8.2, C.4): NAL unit
-- dispatch, picture boundaries (7.4.1.2.4), picture order count types 0 / 1 / 2 (8.2.1), frame_num gaps (8.2.5.2),
-- reference picture lists with modification (8.2.4), sliding window and adaptive (MMCO 1-6) marking (8.2.5), output
-- in POC order through a reorder buffer (num_reorder_frames, else the DPB size), concealment of lost macroblocks,
-- deblocking and border extension. Written from the spec.
-- Never throws: errors are returned (a corrupt slice is skipped and its macroblocks concealed).
-- codecs/video/H264/Decoder: the decoding process above the macroblock layer (ITU-T H.264 8.1, 8.2, C.4): picture
-- boundaries (7.4.1.2.4), POC types 0 / 1 / 2 (8.2.1), frame_num gaps (8.2.5.2), list modification (8.2.4), marking
-- (8.2.5), output in POC order (num_reorder_frames, else the DPB size).
local Bitstream = require("@rosetta/codecs/video/H264/Bitstream")
local Params = require("@rosetta/codecs/video/H264/Params")
local Slice = require("@rosetta/codecs/video/H264/Slice")
@@ -23,17 +20,14 @@ local Decoder = {}
Decoder.__index = Decoder
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)
maxPixels: number?,
lowDelay: boolean?,
deblock: boolean?,
-- 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: (() -> ())?,
}
-- new(opts) -> decoder state
function Decoder.new(opts: Options?): any
local o = opts or {}
return setmetatable({
@@ -58,7 +52,7 @@ function Decoder.new(opts: Options?): any
maxLongTermIdx = -1,
pool = {},
gray = nil,
frames = 0, -- pictures decoded
frames = 0,
errors = 0,
lastError = nil,
fatal = nil,
@@ -67,7 +61,6 @@ function Decoder.new(opts: Options?): any
}, Decoder)
end
-- ------------------------------------------------------------------------------------------------ pictures
local function newPicture(self: any, sps: any): any
local mbW, mbH = sps.mbWidth, sps.mbHeight
local pool = self.pool
@@ -136,7 +129,6 @@ local function grayPicture(self: any, sps: any): any
return g
end
-- ------------------------------------------------------------------------------------------------ output
local function emit(self: any, p: any)
p.waiting = false
local sps = p.sps
@@ -189,7 +181,6 @@ local function reorderDepth(self: any, sps: any): number
return sps.maxDecFrameBuffering or sps.maxDpbFrames
end
-- ------------------------------------------------------------------------------------------------ reference lists
local function shortRefs(self: any): { any }
local t = {}
for _, p in self.refs do
@@ -288,7 +279,6 @@ local function modify(self: any, list: { any }, ops: { number }, n: number, sh:
return true
end
-- buildLists(self, sh, cur) -> list0, list1 (arrays of pictures, truncated to num_ref_idx_active)
local function buildLists(self: any, sh: any, cur: any): ({ any }, { any }, boolean)
picNums(self, sh)
local list0, list1 = {}, {}
@@ -368,7 +358,6 @@ local function buildLists(self: any, sh: any, cur: any): ({ any }, { any }, bool
return list0, list1, ok
end
-- ------------------------------------------------------------------------------------------------ marking
local function unmark(self: any, p: any)
p.short = false
p.long = false
@@ -411,7 +400,7 @@ local function addRef(self: any, p: any)
table.insert(self.refs, p)
end
-- mark(self, cur, sh): 8.2.5 after decoding the current (reference) picture; returns true when MMCO 5 was present
-- 8.2.5 marking after a reference picture; true when MMCO 5 was present
local function mark(self: any, cur: any, sh: any): boolean
local sps = sh.sps
if sh.idr then
@@ -511,7 +500,6 @@ local function mark(self: any, cur: any, sh: any): boolean
return mmco5
end
-- ------------------------------------------------------------------------------------------------ poc
local function computePoc(self: any, sh: any): (number, number)
local sps = sh.sps
local maxFrameNum = 2 ^ sps.log2MaxFrameNum
@@ -577,7 +565,6 @@ local function computePoc(self: any, sh: any): (number, number)
return t, t
end
-- ------------------------------------------------------------------------------------------------ picture decode
local function setupGrids(self: any, sps: any)
local mbW, mbH = sps.mbWidth, sps.mbHeight
if self.S and self.S.mbW == mbW and self.S.mbH == mbH then
@@ -760,7 +747,6 @@ local function finishPicture(self: any)
p.poc = 0
p.frameNum = 0
end
-- POC / frame_num state for the next pictures
if sh.nalRefIdc ~= 0 then
if mmco5 then
self.prevPocMsb = 0
@@ -917,7 +903,6 @@ local function decodeSlice(self: any, sh: any, r: Bitstream.Reader)
end
end
-- nal(self, buf, off, len, pts): one NAL unit (no start code)
function Decoder.nal(self: any, buf: buffer, off: number, len: number, pts: number?)
if len < 1 then
return
@@ -1008,13 +993,11 @@ 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
-- flush(self): finish the current picture and output every waiting picture
function Decoder.flush(self: any)
finishPicture(self)
bump(self, 0)
end
-- take(self) -> the frames output since the last call
function Decoder.take(self: any): { any }
local out = self.out
self.out = {}