Headers (identification, comment via VorbisComment, setup: loop-filter limits, quant ranges -> dequant tables, the 80 Huffman trees as collapsed lookup tables), intra / inter frames, coded SB / block flags, MB modes (8 alphabets), MVs (VLC / CLC, last / prior, 4MV with per-format chroma averages, half / quarter-pel), block qi, DCT tokens with EOB runs, DC prediction, the spec IDCT, loop filter, golden / previous refs, 4:2:0 / 4:2:2 / 4:4:4, picture-region crop, empty-packet dupes. Bit-exact with ffmpeg on every frame of 12 fixtures (tests/codecs/theora). Outside the folder: codecs/video/Video BUILTIN entry theora gets a decoder (track.config = Xiph-laced headers, as Matroska's CodecPrivate); licenses/libtheora-BSD.txt + NOTICE row.
371 lines
11 KiB
Luau
371 lines
11 KiB
Luau
--!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.
|
|
local Bits = require("@formats/core/Bits")
|
|
|
|
local H = {}
|
|
|
|
export type Info = {
|
|
versionMajor: number,
|
|
versionMinor: number,
|
|
versionSub: number,
|
|
frameWidth: number, -- coded frame size (multiples of 16)
|
|
frameHeight: number,
|
|
picWidth: number, -- the picture region (the displayed part)
|
|
picHeight: number,
|
|
picX: number, -- from the left
|
|
picY: number, -- from the bottom, as stored
|
|
fpsNum: number,
|
|
fpsDen: number,
|
|
aspectNum: number,
|
|
aspectDen: number,
|
|
colorspace: number, -- 0 unspecified, 1 Rec. 470M, 2 Rec. 470BG
|
|
bitrate: number,
|
|
quality: number,
|
|
granuleShift: number,
|
|
pixelFormat: number, -- 0 4:2:0, 2 4:2:2, 3 4:4:4 (1 is reserved)
|
|
}
|
|
|
|
export type Setup = {
|
|
lflim: { number }, -- [qi + 1]: loop filter limit
|
|
dequant: buffer, -- u16 [((qi * 3 + pli) * 2 + qti) * 64 + zzi], zig-zag order, scaled by 4 like libtheora
|
|
huff: { number }, -- the 80 collapsed trees, one flat array (see buildTable)
|
|
roots: { number }, -- [table + 1]: the root node offset in huff
|
|
}
|
|
|
|
-- the zig-zag scan: natural index of the coefficient at scan position zzi
|
|
H.ZIGZAG = {
|
|
0, 1, 8, 16, 9, 2, 3, 10, 17, 24, 32, 25, 18, 11, 4, 5, 12, 19, 26, 33, 40, 48, 41, 34, 27, 20, 13, 6, 7, 14, 21, 28,
|
|
35, 42, 49, 56, 57, 50, 43, 36, 29, 22, 15, 23, 30, 37, 44, 51, 58, 59, 52, 45, 38, 31, 39, 46, 53, 60, 61, 54, 47, 55, 62, 63,
|
|
}
|
|
|
|
local function ilog(v: number): number
|
|
local n = 0
|
|
while v > 0 do
|
|
n += 1
|
|
v = v // 2
|
|
end
|
|
return n
|
|
end
|
|
|
|
-- ------------------------------------------------------------------------------------------------ identification
|
|
-- info(buf, off, len) -> Info | nil, message: the packet starting with 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"
|
|
end
|
|
local br = Bits.msbReader(b, off + 7, len - 7)
|
|
local info: Info = {
|
|
versionMajor = br:read(8),
|
|
versionMinor = br:read(8),
|
|
versionSub = br:read(8),
|
|
frameWidth = br:read(16) * 16,
|
|
frameHeight = br:read(16) * 16,
|
|
picWidth = br:read(24),
|
|
picHeight = br:read(24),
|
|
picX = br:read(8),
|
|
picY = br:read(8),
|
|
fpsNum = br:read(32),
|
|
fpsDen = br:read(32),
|
|
aspectNum = br:read(24),
|
|
aspectDen = br:read(24),
|
|
colorspace = br:read(8),
|
|
bitrate = br:read(24),
|
|
quality = br:read(6),
|
|
granuleShift = br:read(5),
|
|
pixelFormat = br:read(2),
|
|
}
|
|
local reserved = br:read(3)
|
|
if br.err then
|
|
return nil, "truncated identification header"
|
|
end
|
|
if info.versionMajor > 3 or (info.versionMajor == 3 and info.versionMinor > 2) then
|
|
return nil, "unsupported: Theora bitstream version " .. info.versionMajor .. "." .. info.versionMinor
|
|
end
|
|
if info.frameWidth == 0 or info.frameHeight == 0 or info.picWidth + info.picX > info.frameWidth or info.picHeight + info.picY > info.frameHeight then
|
|
return nil, "bad frame / picture size"
|
|
end
|
|
if info.fpsNum == 0 or info.fpsDen == 0 then
|
|
return nil, "bad frame rate"
|
|
end
|
|
if info.pixelFormat == 1 or reserved ~= 0 then
|
|
return nil, "reserved pixel format"
|
|
end
|
|
return info, nil
|
|
end
|
|
|
|
-- ------------------------------------------------------------------------------------------------ comment
|
|
-- comment(buf, off, len) -> vendor, { "KEY=value" } | nil, message: the packet starting with 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"
|
|
end
|
|
local p, e = off + 7, off + len
|
|
local function str(): string?
|
|
if p + 4 > e then
|
|
return nil
|
|
end
|
|
local n = buffer.readu32(b, p)
|
|
p += 4
|
|
if n > e - p then
|
|
return nil
|
|
end
|
|
local s = buffer.readstring(b, p, n)
|
|
p += n
|
|
return s
|
|
end
|
|
local vendor = str()
|
|
if not vendor or p + 4 > e then
|
|
return nil, nil, "truncated comment header"
|
|
end
|
|
local count = buffer.readu32(b, p)
|
|
p += 4
|
|
if count * 4 > e - p then
|
|
return nil, nil, "bad comment count"
|
|
end
|
|
local list: { string } = {}
|
|
for _ = 1, count do
|
|
local s = str()
|
|
if not s then
|
|
return nil, nil, "truncated comment header"
|
|
end
|
|
table.insert(list, s)
|
|
end
|
|
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).
|
|
local MAXBITS = 8
|
|
|
|
local function readTree(br: any): ({ number }?, { number }?, { number }?)
|
|
-- the trie: kid0 / kid1 child ids (0 = none), tok = token or -1 (node 1 is the root)
|
|
local kid0, kid1, tok = { 0 }, { 0 }, { -1 }
|
|
-- as libtheora's oc_huff_tree_unpack: code is the current codeword left-aligned in 32 bits, len its length
|
|
local code, len, nleaves = 0, 0, 0
|
|
while true do
|
|
local bit = br:read(1)
|
|
if br.err then
|
|
return nil, nil, nil
|
|
end
|
|
if bit == 0 then
|
|
len += 1
|
|
if len > 32 then
|
|
return nil, nil, nil
|
|
end
|
|
else
|
|
nleaves += 1
|
|
if nleaves > 32 then
|
|
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
|
|
local kids = if k == 0 then kid0 else kid1
|
|
local nx = kids[n]
|
|
if nx == 0 then
|
|
nx = #tok + 1
|
|
kid0[nx], kid1[nx], tok[nx] = 0, 0, -1
|
|
kids[n] = nx
|
|
end
|
|
n = nx
|
|
end
|
|
tok[n] = t
|
|
if len <= 0 then
|
|
break
|
|
end
|
|
-- the next codeword: clear the trailing 1 bits, then set the last 0 bit
|
|
local cb = 2 ^ (32 - len)
|
|
while len > 0 and (code // cb) % 2 == 1 do
|
|
code -= cb
|
|
cb *= 2
|
|
len -= 1
|
|
end
|
|
if len <= 0 then
|
|
break
|
|
end
|
|
code += cb
|
|
end
|
|
end
|
|
return kid0, kid1, tok
|
|
end
|
|
|
|
local function buildTable(out: { number }, kid0: { number }, kid1: { number }, tok: { number }): number
|
|
local heights: { [number]: number } = {}
|
|
local function height(n: number): number
|
|
local h = heights[n]
|
|
if h then
|
|
return h
|
|
end
|
|
h = 0
|
|
if tok[n] < 0 then
|
|
local a = if kid0[n] ~= 0 then height(kid0[n]) else 0
|
|
local c = if kid1[n] ~= 0 then height(kid1[n]) else 0
|
|
h = 1 + math.max(a, c)
|
|
end
|
|
heights[n] = h
|
|
return h
|
|
end
|
|
local function node(n: number): number
|
|
local nb = math.clamp(height(n), 1, MAXBITS)
|
|
local off = #out + 1
|
|
out[off] = nb
|
|
local size = 2 ^ nb
|
|
for i = 1, size do
|
|
out[off + i] = 0
|
|
end
|
|
for i = 0, size - 1 do
|
|
local cur, d = n, 0
|
|
while d < nb and tok[cur] < 0 do
|
|
local bit = (i // 2 ^ (nb - 1 - d)) % 2
|
|
local nx = if bit == 0 then kid0[cur] else kid1[cur]
|
|
d += 1
|
|
cur = nx
|
|
if nx == 0 then
|
|
break
|
|
end
|
|
end
|
|
if cur == 0 then
|
|
out[off + 1 + i] = d * 64 -- no code here (a corrupt tree): token 0
|
|
elseif tok[cur] >= 0 then
|
|
out[off + 1 + i] = d * 64 + tok[cur]
|
|
else
|
|
out[off + 1 + i] = -node(cur)
|
|
end
|
|
end
|
|
return off
|
|
end
|
|
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"
|
|
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"
|
|
end
|
|
local br = Bits.msbReader(b, off + 7, len - 7)
|
|
-- loop filter limits, AC / DC scales
|
|
local lflim, acScale, dcScale = table.create(64, 0), table.create(64, 0), table.create(64, 0)
|
|
local nb = br:read(3)
|
|
for qi = 1, 64 do
|
|
lflim[qi] = br:read(nb)
|
|
end
|
|
nb = br:read(4) + 1
|
|
for qi = 1, 64 do
|
|
acScale[qi] = br:read(nb)
|
|
end
|
|
nb = br:read(4) + 1
|
|
for qi = 1, 64 do
|
|
dcScale[qi] = br:read(nb)
|
|
end
|
|
local nbms = br:read(9) + 1
|
|
local bms: { { number } } = {}
|
|
for m = 1, nbms do
|
|
local mat = table.create(64, 0)
|
|
for ci = 1, 64 do
|
|
mat[ci] = br:read(8)
|
|
end
|
|
bms[m] = mat
|
|
end
|
|
if br.err then
|
|
return nil, "truncated setup header"
|
|
end
|
|
-- the quant ranges of the 6 (qti, pli) pairs: sizes and base matrix indices (0-based)
|
|
local ranges: { { sizes: { number }, idx: { number } } } = {}
|
|
local mb = ilog(nbms - 1)
|
|
for i = 0, 5 do
|
|
local qti, pli = i // 3, i % 3
|
|
local copy = false
|
|
if i > 0 and br:read(1) == 0 then
|
|
copy = true
|
|
local from = i - 1
|
|
if qti > 0 and br:read(1) == 1 then
|
|
from = pli -- the same plane of the intra set
|
|
end
|
|
ranges[i + 1] = ranges[from + 1]
|
|
end
|
|
if not copy then
|
|
local sizes, idx = {}, { br:read(mb) }
|
|
local qi = 0
|
|
while qi < 63 do
|
|
local s = br:read(ilog(62 - qi)) + 1
|
|
table.insert(sizes, s)
|
|
qi += s
|
|
table.insert(idx, br:read(mb))
|
|
end
|
|
if qi > 63 then
|
|
return nil, "bad quant ranges"
|
|
end
|
|
for _, m in idx do
|
|
if m >= nbms then
|
|
return nil, "bad base matrix index"
|
|
end
|
|
end
|
|
ranges[i + 1] = { sizes = sizes, idx = idx }
|
|
end
|
|
end
|
|
if br.err then
|
|
return nil, "truncated setup header"
|
|
end
|
|
-- the dequantisation tables (oc_dequant_tables_init)
|
|
local dequant = buffer.create(64 * 6 * 64 * 2)
|
|
local base = table.create(64, 0)
|
|
for qti = 0, 1 do
|
|
for pli = 0, 2 do
|
|
local r = ranges[qti * 3 + pli + 1]
|
|
local qi = 0
|
|
for qri = 0, #r.sizes do
|
|
local m0 = bms[r.idx[qri + 1] + 1]
|
|
table.move(m0, 1, 64, 1, base)
|
|
local qiStart = qi
|
|
local qiEnd = if qri == #r.sizes then qi + 1 else qi + r.sizes[qri + 1]
|
|
while true do
|
|
local o = ((qi * 3 + pli) * 2 + qti) * 64
|
|
local q = (dcScale[qi + 1] * base[1] // 100) * 4
|
|
buffer.writeu16(dequant, o * 2, math.clamp(q, DC_MIN[qti + 1], 4096))
|
|
for zzi = 1, 63 do
|
|
q = (acScale[qi + 1] * base[H.ZIGZAG[zzi + 1] + 1] // 100) * 4
|
|
buffer.writeu16(dequant, (o + zzi) * 2, math.clamp(q, AC_MIN[qti + 1], 4096))
|
|
end
|
|
qi += 1
|
|
if qi >= qiEnd then
|
|
break
|
|
end
|
|
-- interpolate the next base matrix
|
|
local m1 = bms[r.idx[qri + 2] + 1]
|
|
local sz = r.sizes[qri + 1]
|
|
for ci = 1, 64 do
|
|
base[ci] = (2 * ((qiEnd - qi) * m0[ci] + (qi - qiStart) * m1[ci]) + sz) // (2 * sz)
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|
|
-- the 80 Huffman trees
|
|
local huff: { number } = {}
|
|
local roots: { number } = {}
|
|
for t = 1, 80 do
|
|
local k0, k1, tk = readTree(br)
|
|
if not k0 or not k1 or not tk then
|
|
return nil, "bad Huffman tree " .. (t - 1)
|
|
end
|
|
roots[t] = buildTable(huff, k0, k1, tk)
|
|
end
|
|
return { lflim = lflim, dequant = dequant, huff = huff, roots = roots }, nil
|
|
end
|
|
|
|
return H
|