Mpeg1 is now the Mpeg2 module under its old name (core, tables and VLCs moved to Mpeg2/). Dsp.mc takes separate source / destination strides; Dsp.edge emulates edges. containers/MpegEs: field-picture pairs are one access unit (pictureStructure). Registry: mpeg1 and mpeg2 decode through Mpeg2.newVideoDecoder; more MPEG-2 FourCCs. Co-Authored-By: Claude Opus 5.5 <[email protected]>
145 lines
4.6 KiB
Luau
145 lines
4.6 KiB
Luau
--!strict
|
|
-- codecs/video/Mpeg2/Vlc: lookup tables for the MPEG-1 / MPEG-2 variable-length codes, built at load time (Tables.luau).
|
|
--
|
|
-- A flat table is indexed by the next K bits of the stream (K = the longest code) and holds
|
|
-- len * 65536 + (value + 32768) per entry; len = 0 marks an invalid code. The DCT coefficient tables are two-level
|
|
-- (8 bits, then 8 more for the long codes) and hold len * 65536 + value, value = run * 256 + level, ESC = 0xFFFF,
|
|
-- EOB = 0xFFFE, or SUB + subtable for a long-code prefix. DCT_NEXT is the table for every coefficient after the
|
|
-- first one of a block, where "10" is end_of_block and "11" is run 0 / level 1 (jsmpeg reads this as "1" and one
|
|
-- more bit); DCT_FIRST keeps "1" as run 0 / level 1. DCT_B15 is MPEG-2's Table B-15 (intra_vlc_format = 1), DC_LUMA /
|
|
-- DC_CHROMA cover sizes up to 11. Absorbed from mpeg1-luau e2e2898 (same author, built from jsmpeg's binary trees);
|
|
-- the MPEG-2 tables are built from code lists.
|
|
local T = require("@formats/codecs/video/Mpeg2/Tables")
|
|
|
|
local V = {}
|
|
|
|
type Leaf = { code: number, len: number, value: number }
|
|
|
|
local function leaves(tree: { [number]: number }): { Leaf }
|
|
local out: { Leaf } = {}
|
|
local function walk(state: number, code: number, len: number)
|
|
if tree[state] == 0 and len > 0 then
|
|
table.insert(out, { code = code, len = len, value = tree[state + 2] })
|
|
return
|
|
end
|
|
for bit = 0, 1 do
|
|
local nxt = tree[state + bit]
|
|
if nxt ~= nil and nxt > 0 then
|
|
walk(nxt, code * 2 + bit, len + 1)
|
|
end
|
|
end
|
|
end
|
|
walk(0, 0, 0)
|
|
return out
|
|
end
|
|
|
|
local function flatLeaves(ls: { Leaf }): (buffer, number)
|
|
local k = 0
|
|
for _, l in ls do
|
|
k = math.max(k, l.len)
|
|
end
|
|
local lut = buffer.create(4 * 2 ^ k)
|
|
for _, l in ls do
|
|
local shift = k - l.len
|
|
local first = l.code * 2 ^ shift
|
|
for i = first, first + 2 ^ shift - 1 do
|
|
buffer.writeu32(lut, i * 4, l.len * 65536 + l.value + 32768)
|
|
end
|
|
end
|
|
return lut, k
|
|
end
|
|
|
|
local function flat(tree: { [number]: number }): (buffer, number)
|
|
return flatLeaves(leaves(tree))
|
|
end
|
|
|
|
-- { { code, length } } indexed from 1 -> leaves with value = index - 1
|
|
local function codeLeaves(codes: { { number } }): { Leaf }
|
|
local out: { Leaf } = {}
|
|
for i, c in codes do
|
|
table.insert(out, { code = c[1], len = c[2], value = i - 1 })
|
|
end
|
|
return out
|
|
end
|
|
|
|
V.SUB = 0x1000000
|
|
V.ESC = 0xFFFF
|
|
V.EOB = 0xFFFE
|
|
|
|
-- two-level table: 256 first-level entries, then 256-entry subtables for prefixes of codes longer than 8 bits
|
|
local function twoLevel(ls: { Leaf }): buffer
|
|
local subs: { [number]: number } = {}
|
|
local nsub = 0
|
|
for _, l in ls do
|
|
if l.len > 8 then
|
|
local prefix = l.code // 2 ^ (l.len - 8)
|
|
if subs[prefix] == nil then
|
|
nsub += 1
|
|
subs[prefix] = nsub
|
|
end
|
|
end
|
|
end
|
|
local lut = buffer.create(4 * 256 * (1 + nsub))
|
|
for _, l in ls do
|
|
if l.len <= 8 then
|
|
local shift = 8 - l.len
|
|
local first = l.code * 2 ^ shift
|
|
for i = first, first + 2 ^ shift - 1 do
|
|
buffer.writeu32(lut, i * 4, l.len * 65536 + l.value)
|
|
end
|
|
else
|
|
local prefix = l.code // 2 ^ (l.len - 8)
|
|
local s = subs[prefix]
|
|
buffer.writeu32(lut, prefix * 4, V.SUB + s)
|
|
local rest = l.code % 2 ^ (l.len - 8)
|
|
local shift = 16 - l.len
|
|
local first = rest * 2 ^ shift
|
|
for i = first, first + 2 ^ shift - 1 do
|
|
buffer.writeu32(lut, (s * 256 + i) * 4, l.len * 65536 + l.value)
|
|
end
|
|
end
|
|
end
|
|
return lut
|
|
end
|
|
|
|
V.MBAI, V.MBAI_BITS = flat(T.MACROBLOCK_ADDRESS_INCREMENT) -- 34 = stuffing, 35 = escape
|
|
V.MBTYPE_I, V.MBTYPE_I_BITS = flat(T.MACROBLOCK_TYPE_INTRA)
|
|
V.MBTYPE_P, V.MBTYPE_P_BITS = flat(T.MACROBLOCK_TYPE_PREDICTIVE)
|
|
V.MBTYPE_B, V.MBTYPE_B_BITS = flat(T.MACROBLOCK_TYPE_B)
|
|
do
|
|
-- MPEG-2 adds "0000 0000 1" = 0 (Table B-9: a 4:2:2 macroblock whose coded blocks are all in the extra chroma
|
|
-- pattern bits)
|
|
local ls = leaves(T.CODE_BLOCK_PATTERN)
|
|
table.insert(ls, { code = 1, len = 9, value = 0 })
|
|
V.CBP, V.CBP_BITS = flatLeaves(ls)
|
|
end
|
|
V.MOTION, V.MOTION_BITS = flat(T.MOTION)
|
|
V.DC_LUMA, V.DC_LUMA_BITS = flatLeaves(codeLeaves(T.DC_SIZE_LUMA))
|
|
V.DC_CHROMA, V.DC_CHROMA_BITS = flatLeaves(codeLeaves(T.DC_SIZE_CHROMA))
|
|
|
|
do
|
|
local ls = leaves(T.DCT_COEFF)
|
|
V.DCT_FIRST = twoLevel(ls)
|
|
local nextLs: { Leaf } = {}
|
|
for _, l in ls do
|
|
if l.len == 1 and l.code == 1 then
|
|
table.insert(nextLs, { code = 2, len = 2, value = V.EOB }) -- "10"
|
|
table.insert(nextLs, { code = 3, len = 2, value = 0x0001 }) -- "11"
|
|
else
|
|
table.insert(nextLs, l)
|
|
end
|
|
end
|
|
V.DCT_NEXT = twoLevel(nextLs)
|
|
end
|
|
|
|
do
|
|
local ls: { Leaf } = { { code = 1, len = 6, value = V.ESC }, { code = 6, len = 4, value = V.EOB } }
|
|
local b = T.DCT_COEFF_B15
|
|
for i = 1, #b, 3 do
|
|
table.insert(ls, { code = b[i], len = b[i + 1], value = b[i + 2] })
|
|
end
|
|
V.DCT_B15 = twoLevel(ls)
|
|
end
|
|
|
|
return V
|