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)
+7 -16
View File
@@ -1,11 +1,7 @@
--!strict
-- codecs/video/Theora/Header: the three Theora headers (Theora I specification, section 6): identification (frame /
-- picture size and offsets, frame rate, aspect, colour space, pixel format, keyframe granule shift), comment (Vorbis
-- comment layout) and setup (loop-filter limits, quantisation parameters, the 80 DCT token Huffman trees), and the
-- tables derived from them: the dequantisation matrices of every qi / plane / intra-inter and the collapsed Huffman
-- lookup tables the frame decoder walks. A Luau port of libtheora 1.2.0 (Xiph.Org, BSD 3-clause:
-- licenses/libtheora-BSD.txt): decinfo.c, dequant.c, quant.c (oc_dequant_tables_init), huffdec.c (the tree unpacking;
-- the table layout here is simpler). Never throws: bad headers return nil, message.
-- codecs/video/Theora/Header: the three Theora headers (Theora I specification, section 6) and the dequantisation and
-- Huffman lookup tables derived from them. A Luau port of libtheora 1.2.0 (Xiph.Org, BSD 3-clause,
-- licenses/libtheora-BSD.txt): decinfo.c, dequant.c, quant.c (oc_dequant_tables_init), huffdec.c.
local Bits = require("@rosetta/core/Bits")
local H = {}
@@ -18,7 +14,7 @@ export type Info = {
frameHeight: number,
picWidth: number, -- the picture region (the displayed part)
picHeight: number,
picX: number, -- from the left
picX: number,
picY: number, -- from the bottom, as stored
fpsNum: number,
fpsDen: number,
@@ -53,8 +49,7 @@ local function ilog(v: number): number
return n
end
-- ------------------------------------------------------------------------------------------------ identification
-- info(buf, off, len) -> Info | nil, message: the packet starting with 0x80 "theora"
-- identification header: 0x80 "theora"
function H.info(b: buffer, off: number, len: number): (Info?, string?)
if len < 42 or buffer.readu8(b, off) ~= 0x80 or buffer.readstring(b, off + 1, 6) ~= "theora" then
return nil, "not a Theora identification header"
@@ -99,8 +94,7 @@ function H.info(b: buffer, off: number, len: number): (Info?, string?)
return info, nil
end
-- ------------------------------------------------------------------------------------------------ comment
-- comment(buf, off, len) -> vendor, { "KEY=value" } | nil, message: the packet starting with 0x81 "theora"
-- comment header: 0x81 "theora"
function H.comment(b: buffer, off: number, len: number): (string?, { string }?, string?)
if len < 7 or buffer.readu8(b, off) ~= 0x81 or buffer.readstring(b, off + 1, 6) ~= "theora" then
return nil, nil, "not a Theora comment header"
@@ -139,7 +133,6 @@ function H.comment(b: buffer, off: number, len: number): (string?, { string }?,
return vendor, list, nil
end
-- ------------------------------------------------------------------------------------------------ Huffman trees
-- A tree is read depth first (0: an internal node, 1 then 5 bits: a leaf with that token). It becomes lookup tables in
-- one flat array: a node is huff[o] = n (bits to peek) followed by 2^n entries, entry = length * 64 + token (a leaf
-- reached after `length` <= n of the peeked bits) or -offset (another node, after all n bits).
@@ -166,7 +159,6 @@ local function readTree(br: any): ({ number }?, { number }?, { number }?)
return nil, nil, nil
end
local t = br:read(5)
-- insert the leaf
local n = 1
for d = 1, len do
local k = (code // 2 ^ (32 - d)) % 2
@@ -247,11 +239,10 @@ local function buildTable(out: { number }, kid0: { number }, kid1: { number }, t
return node(1)
end
-- ------------------------------------------------------------------------------------------------ setup
local DC_MIN = { 16, 32 }
local AC_MIN = { 8, 16 }
-- setup(buf, off, len) -> Setup | nil, message: the packet starting with 0x82 "theora"
-- setup header: 0x82 "theora"
function H.setup(b: buffer, off: number, len: number): (Setup?, string?)
if len < 7 or buffer.readu8(b, off) ~= 0x82 or buffer.readstring(b, off + 1, 6) ~= "theora" then
return nil, "not a Theora setup header"
+6 -34
View File
@@ -1,20 +1,7 @@
--!strict
-- codecs/video/Theora: a Theora video decoder (the Theora I specification, Xiph.Org), for Ogg (video/ogv) and Matroska
-- (V_THEORA). A Luau port of the libtheora 1.2.0 decoder (BSD 3-clause, licenses/libtheora-BSD.txt): Header.luau
-- (identification / comment / setup headers, dequantisation tables, Huffman trees) and Decoder.luau (native frames).
-- Supports: every Theora I stream (bitstream 3.2.x and older 3.x): intra and inter frames, golden frames, 4MV
-- macro blocks, all three pixel formats (4:2:0, 4:2:2, 4:4:4), picture regions smaller than the coded frame (odd sizes,
-- offsets), empty packets (a repeated frame). Output is bit-exact with libtheora / ffmpeg (tests/codecs/theora.luau);
-- yuv420p images average 4:2:2 / 4:4:4 chroma down (planes() keeps the native layout).
-- Not supported: libtheora's optional post-processing (deblocking / deringing, off by default there too).
--
-- local dec = Theora.new({ idHeader, commentHeader, setupHeader }) -- or the Xiph-laced Matroska CodecPrivate
-- local kind = dec:decode(packet) -- "key" | "inter" | "dup" (empty packet / no coded blocks) | nil, Err
-- local img = dec:image() -- a fresh yuv420p Types.Image of the picture region
-- local y, u, v, cw, ch = dec:planes() -- native planes (chroma cw x ch), top-down
-- Theora.granuleFrame(dec.info, granulepos) -- the 0-based frame index of an Ogg granule position
--
-- Never throws: bad headers / packets come back as nil, Err (a corrupt frame still updates the picture).
-- codecs/video/Theora: a Theora video decoder (the Theora I specification, Xiph.Org), for Ogg and Matroska (V_THEORA).
-- A Luau port of the libtheora 1.2.0 decoder (BSD 3-clause, licenses/libtheora-BSD.txt), bit-exact with libtheora /
-- ffmpeg; libtheora's optional post-processing is not supported.
local Err = require("@rosetta/core/Err")
local Types = require("@rosetta/Types")
local VorbisComment = require("@rosetta/codecs/audio/VorbisComment")
@@ -26,12 +13,11 @@ local Theora = {}
export type Info = Header.Info
export type Options = {
maxPixels: number?, -- larger coded frames are refused (default 4096 * 2304)
maxPixels: number?,
}
local PIXEL_FORMATS = { [0] = "yuv420p", "reserved", "yuv422p", "yuv444p" }
-- isHeader(buf, off?, len?) -> 1 / 2 / 3 for the identification / comment / setup header, else nil
function Theora.isHeader(b: buffer, off: number?, len: number?): number?
local o = off or 0
local n = len or (buffer.len(b) - o)
@@ -45,7 +31,6 @@ function Theora.isHeader(b: buffer, off: number?, len: number?): number?
return nil
end
-- splitConfig(config) -> the three header packets from Matroska's Xiph-laced CodecPrivate (or nil)
function Theora.splitConfig(c: buffer): { buffer }?
local n = buffer.len(c)
if n < 3 or buffer.readu8(c, 0) ~= 2 then
@@ -77,7 +62,6 @@ function Theora.splitConfig(c: buffer): { buffer }?
return out
end
-- info(idHeader) -> Header.Info (frame / picture size, frame rate, granule shift, pixel format, ...) | nil, Err
function Theora.info(h: buffer): (Header.Info?, Err.Err?)
local ok, info, err = pcall(Header.info, h, 0, buffer.len(h))
if not ok or not info then
@@ -86,7 +70,6 @@ function Theora.info(h: buffer): (Header.Info?, Err.Err?)
return info, nil
end
-- comment(commentHeader) -> vendor, { "KEY=value" }, tags (lower-case keys) | nil, nil, nil, Err
function Theora.comment(h: buffer): (string?, { string }?, { [string]: string }?, Err.Err?)
local ok, vendor, list, err = pcall(Header.comment, h, 0, buffer.len(h))
if not ok or not vendor or not list then
@@ -95,7 +78,6 @@ function Theora.comment(h: buffer): (string?, { string }?, { [string]: string }?
return vendor, list, (VorbisComment.tags(list)), nil
end
-- granuleFrame(info, granulepos) -> the 0-based index of the frame a granule position ends on (-1 for none)
function Theora.granuleFrame(info: Header.Info, g: number): number
if g < 0 then
return -1
@@ -111,7 +93,6 @@ function Theora.granuleFrame(info: Header.Info, g: number): number
return f
end
-- ------------------------------------------------------------------------------------------------ decoder
local Dec = {}
Dec.__index = Dec
@@ -126,14 +107,13 @@ export type TheoraDecoder = typeof(setmetatable(
frameRate: number,
pixelFormat: string, -- "yuv420p" | "yuv422p" | "yuv444p"
aspect: number, -- pixel aspect ratio (1 when unknown)
frames: number, -- packets decoded (dupes included)
frames: number,
_st: Decoder.State,
_any: boolean, -- a frame was decoded
_any: boolean,
},
Dec
))
-- new(headers, opts?) -> TheoraDecoder | nil, Err: headers = the three header packets, or the Xiph-laced config
function Theora.new(headers: { buffer } | buffer, opts: Options?): (TheoraDecoder?, Err.Err?)
local list: { buffer }? = nil
if type(headers) == "buffer" then
@@ -177,7 +157,6 @@ function Theora.new(headers: { buffer } | buffer, opts: Options?): (TheoraDecode
}, Dec), nil
end
-- decode(data, off?, len?) -> "key" | "inter" | "dup" | nil, Err. Header packets are skipped (nil, nil).
function Dec.decode(self: TheoraDecoder, data: buffer, off: number?, len: number?): (string?, Err.Err?)
local o = off or 0
local n = len or (buffer.len(data) - o)
@@ -198,7 +177,6 @@ function Dec.decode(self: TheoraDecoder, data: buffer, off: number?, len: number
return kind, nil
end
-- reset(): after a seek (the next frame should be a keyframe; until then inter frames predict from grey / old data)
function Dec.reset(self: TheoraDecoder)
local st = self._st
st.shown = st.prev
@@ -210,8 +188,6 @@ local function window(self: TheoraDecoder): (number, number, number, number)
return info.picX, info.frameHeight - info.picHeight - info.picY, info.picWidth, info.picHeight
end
-- planes() -> y, u, v (fresh top-down buffers of the picture region, chroma in the native layout), chroma width,
-- chroma height
function Dec.planes(self: TheoraDecoder): (buffer, buffer, buffer, number, number)
local st = self._st
local x0, y0, w, h = window(self)
@@ -223,7 +199,6 @@ function Dec.planes(self: TheoraDecoder): (buffer, buffer, buffer, number, numbe
return Decoder.plane(st, 0, x0, y0, w, h), Decoder.plane(st, 1, cx0, cy0, cw, ch), Decoder.plane(st, 2, cx0, cy0, cw, ch), cw, ch
end
-- image() -> a fresh yuv420p Types.Image of the picture (4:2:2 / 4:4:4 chroma averaged down)
function Dec.image(self: TheoraDecoder): Types.Image
local y, u, v, cw, ch = self:planes()
local w, h = self.width, self.height
@@ -247,9 +222,6 @@ function Dec.image(self: TheoraDecoder): Types.Image
return img
end
-- ------------------------------------------------------------------------------------------------ registry
-- newVideoDecoder(track, opts?) -> a Types.VideoCodecDecoder (the codecs/video/Video entry "theora"): track.config
-- holds the three headers (Xiph-laced, as Matroska's CodecPrivate); one picture per packet, empty packets repeat
function Theora.newVideoDecoder(track: Types.Track, opts: any?): (Types.VideoCodecDecoder?, Err.Err?)
if not track.config then
return nil, Err.new(Err.CORRUPT, "Theora track without its headers")