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

+5 -31
View File
@@ -1,22 +1,10 @@
--!strict
--!native
--!optimize 2
-- codecs/video/Theora/Decoder: the Theora frame decoder (Theora I specification, section 7; native, never throws). A
-- Luau port of libtheora 1.2.0's decoder (Xiph.Org, BSD 3-clause: licenses/libtheora-BSD.txt; decode.c, state.c,
-- idct.c, fragment.c) with the bit-exact reconstruction of the specification:
-- * frame header, coded super block / block flags (the run-length codes), macro block modes (the 8 mode
-- alphabets), motion vectors (VLC / fixed length, last / prior vectors, 4MV with the chroma averages of each pixel
-- format), per-block qi indices, DC / AC tokens with EOB runs across blocks, planes and coefficients;
-- * DC prediction, dequantisation, the spec's 16-bit integer IDCT (DC-only blocks as libtheora: (dc * q + 15) >> 5),
-- half-pel motion compensation (two-reference average toward zero), the loop filter, border extension for
-- unrestricted motion vectors, golden / previous reference frames;
-- * 4:2:0, 4:2:2 and 4:4:4; empty packets repeat the previous frame.
-- Differences from libtheora: coefficients are kept per fragment (64 i16 each) instead of libtheora's token lists,
-- the frame is reconstructed whole (no MCU pipelining, same result), there is no post-processing (libtheora's default),
-- and intermediate IDCT sums are not wrapped to 16 bits (only out-of-range streams differ).
--
-- Planes are stored in Theora's own orientation (row 0 is the bottom of the picture) with 16 (luma) / 8 (subsampled
-- chroma) pixels of edge padding; output() turns the picture region into top-down planes.
-- codecs/video/Theora/Decoder: the Theora frame decoder (Theora I specification, section 7). A Luau port of libtheora
-- 1.2.0's decoder (Xiph.Org, BSD 3-clause: licenses/libtheora-BSD.txt; decode.c, state.c, idct.c, fragment.c).
-- DC-only blocks as libtheora: (dc * q + 15) >> 5; IDCT sums are not wrapped to 16 bits (only out-of-range streams differ).
-- Planes are in Theora's orientation (row 0 = bottom) with 16 (luma) / 8 (subsampled chroma) pixels of edge padding.
local Header = require("@rosetta/codecs/video/Theora/Header")
local readu8 = buffer.readu8
@@ -211,7 +199,6 @@ export type State = {
keyframes: number,
}
-- new(info, setup) -> State
function D.new(info: Header.Info, setup: Header.Setup): State
local pf = info.pixelFormat
local hdec = if pf % 2 == 0 then 1 else 0
@@ -382,7 +369,6 @@ function D.new(info: Header.Info, setup: Header.Setup): State
return st :: State
end
-- ------------------------------------------------------------------------------------------------ reconstruction
-- the 8-point IDCT of libtheora's idct8 (the spec's), on 8 values; returns the 8 outputs
local function idct8(x0: number, x1: number, x2: number, x3: number, x4: number, x5: number, x6: number, x7: number): (number, number, number, number, number, number, number, number)
local t0 = (C4 * (x0 + x4)) // 65536
@@ -522,7 +508,6 @@ local function reconFrag(st: State, fi: number, qis: { number }, dstPlanes: { bu
st.last[fi] = 0
idctRows(blk, st.tmp)
end
-- the prediction
if ri == SELF then
for y = 0, 7 do
bfill(dst, pos + y * stride, 128, 8)
@@ -552,7 +537,6 @@ local function reconFrag(st: State, fi: number, qis: { number }, dstPlanes: { bu
end
end
end
-- the residual
if last ~= 0 then
idctAdd(st.tmp, dst, pos, stride)
elseif fv ~= 0 then
@@ -565,7 +549,6 @@ local function reconFrag(st: State, fi: number, qis: { number }, dstPlanes: { bu
end
end
-- ------------------------------------------------------------------------------------------------ loop filter
local function filterH(b: buffer, p: number, stride: number, bv: { number })
p -= 2
for _ = 0, 7 do
@@ -735,14 +718,10 @@ local function dcPredict(st: State)
end
end
-- ------------------------------------------------------------------------------------------------ frame decode
-- decode(st, data, off, len) -> kind ("key" | "inter" | "dup") | nil, message. The picture is then in
-- st.refs[st.shown].
function D.decode(st: State, data: buffer, off: number, len: number): (string?, string?)
if len <= 0 then
return "dup", nil
end
-- a zero-padded copy: reads past the end give zero bits, every read site clamps pos to the end first
local bb = buffer.create(len + 24)
bcopy(bb, 0, data, off, len)
local maxp = len * 8
@@ -1216,7 +1195,6 @@ function D.decode(st: State, data: buffer, off: number, len: number): (string?,
dcPredict(st)
-- reconstruction into a free reference buffer
local self = 1
while self == st.gold or self == st.prev do
self += 1
@@ -1249,9 +1227,6 @@ function D.decode(st: State, data: buffer, off: number, len: number): (string?,
return if intra then "key" else "inter", nil
end
-- ------------------------------------------------------------------------------------------------ output
-- the picture region of the shown frame as top-down planes: plane(st, pli, x0, y0, w, h) copies a w x h window whose
-- top-left corner is (x0, y0) counted from the top of the plane
function D.plane(st: State, pli: number, x0: number, y0: number, w: number, h: number): buffer
local src = st.refs[st.shown][pli + 1]
local stride, origin, ph = st.stride[pli + 1], st.origin[pli + 1], st.ph[pli + 1]
@@ -1263,8 +1238,7 @@ function D.plane(st: State, pli: number, x0: number, y0: number, w: number, h: n
return out
end
-- downsample(src, cw, ch, hdec, dst, off, ow, oh): 4:2:2 (hdec 1: rows averaged in pairs) or 4:4:4 (hdec 0: 2x2
-- averages) chroma of cw x ch into ow x oh 4:2:0 chroma at dst[off]
-- 4:2:2 (rows averaged in pairs) or 4:4:4 (2x2 averages) chroma down to 4:2:0
function D.downsample(src: buffer, cw: number, ch: number, hdec: number, dst: buffer, off: number, ow: number, oh: number)
for r = 0, oh - 1 do
local r0 = math.min(2 * r, ch - 1)