video/smk: Smacker (SMK2 / SMK4) with its video codec and audio
containers into containers/Demux (palette updates ride on each video packet as packet.palette); the Smacker video codec (header Huffman trees with the escape cache, MONO / FULL (+ SMK4 doubled modes) / SKIP / FILL blocks) lives in codecs/video/Legacy/Smacker and registers as 'smackvid'; Smacker's packed DPCM-Huffman audio (8 / 16-bit, mono / stereo) and raw PCM tracks decode in video/smk/Audio, the Decoder built by the format over codecs/audio/Stream (Smk.openAudio; open() installs it as the openAudio hook). Fixtures encoded by tools/fixtures/smk.py: video and audio bit-exact with ffmpeg. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
50e270086d
commit
4b098276e2
8 files changed
+1522
-3
No files matched your search
@@ -0,0 +1,363 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- codecs/video/Legacy/Smacker: RAD Game Tools' Smacker video (SMK2 / SMK4 frames; the file format is video/smk) and
|
||||
-- the Huffman trees Smacker audio shares. Written from the format description (MultimediaWiki "Smacker"). Bits are
|
||||
-- read LSB first. A tree is a pre-order bit string (1 = node: left subtree then right; 0 = leaf with its value);
|
||||
-- an 8-bit tree's leaves hold bytes, a "big" tree's leaves hold 16-bit values made of a low-byte and a high-byte
|
||||
-- 8-bit tree, three of them being escape leaves that return the three most recently decoded values instead (a
|
||||
-- per-frame cache, reset to 0 at each frame). The four big trees (MMAP, MCLR, FULL, TYPE) come from the file header;
|
||||
-- a frame is 4x4 blocks in raster order, each TYPE code giving a block type and a run length: MONO (two colours and a
|
||||
-- 16-bit mask), FULL (16 pixels as 8 codes; SMK4 adds a 2x2-doubled and a 2x1-doubled mode), SKIP, FILL (one colour).
|
||||
-- Trees are flat arrays (a node stores the size of its left subtree, as a negative number) walked bit by bit.
|
||||
-- Bit-exact with ffmpeg's smackvid decoder. Never throws: trees are bounded (depth, entries), reads past the data give
|
||||
-- zero bits, a frame whose codes run out of bits gives no picture.
|
||||
local Picture = require("@formats/codecs/video/Legacy/Picture")
|
||||
|
||||
local readu8, writeu8, writeu32 = buffer.readu8, buffer.writeu8, buffer.writeu32
|
||||
local band, rshift = bit32.band, bit32.rshift
|
||||
|
||||
local Smacker = {}
|
||||
|
||||
local MAX_DEPTH8 = 27 -- 8-bit trees (3 x 9-bit lookups in the reference decoder)
|
||||
local MAX_DEPTH16 = 500
|
||||
|
||||
-- block runs (TYPE code bits 2..7)
|
||||
local RUNS = table.create(64, 0)
|
||||
for i = 1, 59 do
|
||||
RUNS[i] = i
|
||||
end
|
||||
RUNS[60], RUNS[61], RUNS[62], RUNS[63], RUNS[64] = 128, 256, 512, 1024, 2048
|
||||
|
||||
-- a bit position over buf[0, nbits / 8)
|
||||
export type Bits = { buf: buffer, pos: number, nbits: number }
|
||||
|
||||
function Smacker.bits(buf: buffer, offset: number, length: number): Bits
|
||||
return { buf = buf, pos = offset * 8, nbits = (offset + length) * 8 }
|
||||
end
|
||||
|
||||
local function bit(br: Bits): number
|
||||
local p = br.pos
|
||||
br.pos = p + 1
|
||||
if p >= br.nbits then
|
||||
return 0
|
||||
end
|
||||
return band(rshift(readu8(br.buf, rshift(p, 3)), band(p, 7)), 1)
|
||||
end
|
||||
Smacker.bit = bit
|
||||
|
||||
-- read(br, n) -> n bits (LSB first; zeros past the end)
|
||||
function Smacker.read(br: Bits, n: number): number
|
||||
local v, m = 0, 1
|
||||
for _ = 1, n do
|
||||
v += bit(br) * m
|
||||
m *= 2
|
||||
end
|
||||
return v
|
||||
end
|
||||
local readBits = Smacker.read
|
||||
|
||||
-- walk(tree, br) -> the leaf value (no end-of-data check: 8-bit trees, as the reference's table lookups)
|
||||
local function walk(tree: { number }, br: Bits): number
|
||||
local i = 1
|
||||
local e = tree[1]
|
||||
while e < 0 do
|
||||
if bit(br) == 1 then
|
||||
i += -e - 1
|
||||
end
|
||||
i += 1
|
||||
e = tree[i]
|
||||
end
|
||||
return e
|
||||
end
|
||||
Smacker.walk = walk
|
||||
|
||||
-- the pre-order tree reader: leaf(index) -> value | nil, message; returns the flat tree (1-based)
|
||||
local function build(br: Bits, maxDepth: number, maxEntries: number, leaf: (number) -> (number?, string?), bigChecks: boolean): ({ number }?, string?)
|
||||
local vals: { number } = {}
|
||||
local n = 0
|
||||
local stack: { number } = {}
|
||||
local state: { number } = {}
|
||||
local sp = 0
|
||||
while true do
|
||||
if sp > maxDepth then
|
||||
return nil, "tree too deep"
|
||||
end
|
||||
if n >= maxEntries then
|
||||
return nil, "tree too big"
|
||||
end
|
||||
if bigChecks and br.pos >= br.nbits then
|
||||
return nil, "truncated tree"
|
||||
end
|
||||
if bit(br) == 1 then
|
||||
n += 1
|
||||
vals[n] = -1
|
||||
sp += 1
|
||||
stack[sp], state[sp] = n, 0
|
||||
else
|
||||
local v, msg = leaf(n)
|
||||
if not v then
|
||||
return nil, msg
|
||||
end
|
||||
n += 1
|
||||
vals[n] = v
|
||||
local more = false
|
||||
while sp > 0 do
|
||||
if state[sp] == 0 then
|
||||
local at = stack[sp]
|
||||
vals[at] = -(n - at) - 1 -- the left subtree holds n - at entries
|
||||
state[sp] = 1
|
||||
more = true
|
||||
break
|
||||
end
|
||||
sp -= 1
|
||||
end
|
||||
if not more then
|
||||
return vals, nil
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- tree8(br) -> an 8-bit tree (leaves are bytes) | nil, message
|
||||
function Smacker.tree8(br: Bits): ({ number }?, string?)
|
||||
local leaves = 0
|
||||
return build(br, MAX_DEPTH8, 511, function(_: number): (number?, string?)
|
||||
if leaves >= 256 then
|
||||
return nil, "tree too big"
|
||||
end
|
||||
leaves += 1
|
||||
if br.nbits - br.pos < 8 then
|
||||
return nil, "truncated tree"
|
||||
end
|
||||
return readBits(br, 8), nil
|
||||
end, false)
|
||||
end
|
||||
local tree8 = Smacker.tree8
|
||||
|
||||
export type BigTree = { vals: { number }, last: { number } } -- last: the escape leaves' indexes (1-based)
|
||||
|
||||
-- bigTree(br, size) -> one of the header's trees | nil, message (size = its byte size from the header: bounds entries)
|
||||
local function bigTree(br: Bits, size: number): (BigTree?, string?)
|
||||
if bit(br) == 0 then
|
||||
return { vals = { 0, 0 }, last = { 2, 2, 2 } }, nil -- no tree: every code is 0
|
||||
end
|
||||
local lohi: { { number } } = {}
|
||||
for k = 1, 2 do
|
||||
if bit(br) == 0 then
|
||||
lohi[k] = { 0 }
|
||||
else
|
||||
local t, msg = tree8(br)
|
||||
if not t then
|
||||
return nil, msg
|
||||
end
|
||||
bit(br)
|
||||
lohi[k] = t
|
||||
end
|
||||
end
|
||||
local esc = { readBits(br, 16), readBits(br, 16), readBits(br, 16) }
|
||||
local last = { -1, -1, -1 }
|
||||
local lo, hi = lohi[1], lohi[2]
|
||||
local vals, msg = build(br, MAX_DEPTH16, (size + 3) // 4, function(index: number): (number?, string?)
|
||||
local v = walk(lo, br) + walk(hi, br) * 256
|
||||
for k = 1, 3 do
|
||||
if v == esc[k] then
|
||||
last[k] = index + 1
|
||||
return 0, nil
|
||||
end
|
||||
end
|
||||
return v, nil
|
||||
end, true)
|
||||
if not vals then
|
||||
return nil, msg
|
||||
end
|
||||
bit(br)
|
||||
for k = 1, 3 do
|
||||
if last[k] == -1 then
|
||||
table.insert(vals, 0)
|
||||
last[k] = #vals
|
||||
end
|
||||
end
|
||||
return { vals = vals, last = last }, nil
|
||||
end
|
||||
|
||||
export type Trees = { mmap: BigTree, mclr: BigTree, full: BigTree, type: BigTree }
|
||||
|
||||
-- trees(config) -> the four header trees | nil, message. config = the four u32 tree sizes then the tree bits
|
||||
function Smacker.trees(config: buffer): (Trees?, string?)
|
||||
local n = buffer.len(config)
|
||||
if n <= 16 then
|
||||
return nil, "no trees"
|
||||
end
|
||||
local br = Smacker.bits(config, 16, n - 16)
|
||||
local out: { BigTree } = {}
|
||||
local present = 0
|
||||
for k = 1, 4 do
|
||||
local size = buffer.readu32(config, (k - 1) * 4)
|
||||
if size >= 2 ^ 28 then
|
||||
return nil, "tree size too large"
|
||||
end
|
||||
local here = br.pos < br.nbits and band(rshift(readu8(config, rshift(br.pos, 3)), band(br.pos, 7)), 1) == 1
|
||||
local t, msg = bigTree(br, size)
|
||||
if not t then
|
||||
return nil, msg
|
||||
end
|
||||
if here then
|
||||
present += 1
|
||||
end
|
||||
out[k] = t
|
||||
end
|
||||
if present == 0 or br.pos > br.nbits then
|
||||
return nil, "bad trees"
|
||||
end
|
||||
return { mmap = out[1], mclr = out[2], full = out[3], type = out[4] }, nil
|
||||
end
|
||||
|
||||
-- code(tree, br) -> the next value (updating the recent-value cache) | nil when the bits run out inside the tree
|
||||
local function code(t: BigTree, br: Bits): number?
|
||||
local vals = t.vals
|
||||
local i = 1
|
||||
local e = vals[1]
|
||||
while e < 0 do
|
||||
if br.pos >= br.nbits then
|
||||
return nil
|
||||
end
|
||||
if bit(br) == 1 then
|
||||
i += -e - 1
|
||||
end
|
||||
i += 1
|
||||
e = vals[i]
|
||||
end
|
||||
local last = t.last
|
||||
local l0, l1, l2 = last[1], last[2], last[3]
|
||||
if e ~= vals[l0] then
|
||||
vals[l2] = vals[l1]
|
||||
vals[l1] = vals[l0]
|
||||
vals[l0] = e
|
||||
end
|
||||
return e
|
||||
end
|
||||
|
||||
local function reset(t: BigTree)
|
||||
local vals, last = t.vals, t.last
|
||||
vals[last[1]], vals[last[2]], vals[last[3]] = 0, 0, 0
|
||||
end
|
||||
|
||||
-- decode(pic, data, trees, v4) -> true | false, message. pic: palettised, padded to blocks of 4
|
||||
function Smacker.decode(pic: Picture.Picture, d: buffer, trees: Trees, v4: boolean): (boolean, string?)
|
||||
local px = pic.idx :: buffer
|
||||
local stride = pic.stride
|
||||
local mmap, mclr, full, typ = trees.mmap, trees.mclr, trees.full, trees.type
|
||||
reset(mmap)
|
||||
reset(mclr)
|
||||
reset(full)
|
||||
reset(typ)
|
||||
local br = Smacker.bits(d, 0, buffer.len(d))
|
||||
local bw, bh = pic.w // 4, pic.h // 4
|
||||
local blocks = bw * bh
|
||||
local blk = 0
|
||||
while blk < blocks do
|
||||
local ty = code(typ, br)
|
||||
if not ty then
|
||||
return false, "truncated frame"
|
||||
end
|
||||
local run = RUNS[band(rshift(ty, 2), 63) + 1]
|
||||
local kind = band(ty, 3)
|
||||
if kind == 2 then
|
||||
blk = math.min(blocks, blk + run)
|
||||
elseif kind == 3 then
|
||||
local v = rshift(ty, 8) * 0x01010101
|
||||
for _ = 1, run do
|
||||
if blk >= blocks then
|
||||
break
|
||||
end
|
||||
local o = (blk // bw) * stride * 4 + (blk % bw) * 4
|
||||
writeu32(px, o, v)
|
||||
writeu32(px, o + stride, v)
|
||||
writeu32(px, o + 2 * stride, v)
|
||||
writeu32(px, o + 3 * stride, v)
|
||||
blk += 1
|
||||
end
|
||||
elseif kind == 0 then
|
||||
for _ = 1, run do
|
||||
if blk >= blocks then
|
||||
break
|
||||
end
|
||||
local clr = code(mclr, br)
|
||||
local map = code(mmap, br)
|
||||
if not clr or not map then
|
||||
return false, "truncated frame"
|
||||
end
|
||||
local hi, lo = rshift(clr, 8), band(clr, 255)
|
||||
local o = (blk // bw) * stride * 4 + (blk % bw) * 4
|
||||
for r = 0, 3 do
|
||||
local q = o + r * stride
|
||||
writeu8(px, q, if band(map, 1) ~= 0 then hi else lo)
|
||||
writeu8(px, q + 1, if band(map, 2) ~= 0 then hi else lo)
|
||||
writeu8(px, q + 2, if band(map, 4) ~= 0 then hi else lo)
|
||||
writeu8(px, q + 3, if band(map, 8) ~= 0 then hi else lo)
|
||||
map = rshift(map, 4)
|
||||
end
|
||||
blk += 1
|
||||
end
|
||||
else
|
||||
local mode = 0
|
||||
if v4 then
|
||||
if br.pos >= br.nbits then
|
||||
return false, "truncated frame"
|
||||
end
|
||||
if bit(br) == 1 then
|
||||
mode = 1
|
||||
elseif bit(br) == 1 then
|
||||
mode = 2
|
||||
end
|
||||
end
|
||||
for _ = 1, run do
|
||||
if blk >= blocks then
|
||||
break
|
||||
end
|
||||
local o = (blk // bw) * stride * 4 + (blk % bw) * 4
|
||||
if mode == 0 then
|
||||
for r = 0, 3 do
|
||||
local a = code(full, br)
|
||||
local b = code(full, br)
|
||||
if not a or not b then
|
||||
return false, "truncated frame"
|
||||
end
|
||||
local q = o + r * stride
|
||||
writeu8(px, q + 2, band(a, 255))
|
||||
writeu8(px, q + 3, rshift(a, 8))
|
||||
writeu8(px, q, band(b, 255))
|
||||
writeu8(px, q + 1, rshift(b, 8))
|
||||
end
|
||||
elseif mode == 1 then
|
||||
for half = 0, 1 do
|
||||
local a = code(full, br)
|
||||
if not a then
|
||||
return false, "truncated frame"
|
||||
end
|
||||
local lo, hi = band(a, 255), rshift(a, 8)
|
||||
local v = lo * 0x0101 + hi * 0x01010000
|
||||
writeu32(px, o + half * 2 * stride, v)
|
||||
writeu32(px, o + (half * 2 + 1) * stride, v)
|
||||
end
|
||||
else
|
||||
for half = 0, 1 do
|
||||
local p2 = code(full, br)
|
||||
local p1 = code(full, br)
|
||||
if not p1 or not p2 then
|
||||
return false, "truncated frame"
|
||||
end
|
||||
local v = p1 + p2 * 65536
|
||||
writeu32(px, o + half * 2 * stride, v)
|
||||
writeu32(px, o + (half * 2 + 1) * stride, v)
|
||||
end
|
||||
end
|
||||
blk += 1
|
||||
end
|
||||
end
|
||||
end
|
||||
return true, nil
|
||||
end
|
||||
|
||||
return Smacker
|
||||
@@ -3,7 +3,8 @@
|
||||
-- The 1990s inter-frame codecs as codecs/video/Video registry decoders (rgba8 pictures, one per packet): Autodesk
|
||||
-- FLI / FLC ("flic", the frames of video/fli), Cinepak ("cinepak",
|
||||
-- 'cvid'), Microsoft Video 1 ("msvideo1": CRAM / MSVC / WHAM, 8-bit palettised and 16-bit), QuickTime Animation
|
||||
-- ("qtrle", 'rle ': depths 1 / 2 / 4 / 8 / 16 / 24 / 32 and grey) and QuickTime Graphics ("smc", 'smc ').
|
||||
-- ("qtrle", 'rle ': depths 1 / 2 / 4 / 8 / 16 / 24 / 32 and grey), QuickTime Graphics ("smc", 'smc ') and Smacker
|
||||
-- video ("smackvid", the frames of video/smk: track.config = the header's tree sizes and trees, codecTag SMK2 / SMK4).
|
||||
-- New in formats-luau, written from the format descriptions (see each submodule); decoding is bit-exact with ffmpeg's
|
||||
-- decoders (tests/codecs/legacy.luau). Every decoder keeps the previous picture (a packet paints over it) and returns
|
||||
-- a fresh copy.
|
||||
@@ -13,7 +14,7 @@
|
||||
-- packet on. Depths come from track.bitsPerPixel (AVI biBitCount, the QuickTime depth).
|
||||
--
|
||||
-- local dec, err = Legacy.newDecoder("cinepak", track, { maxPixels = 2^22 }) -- a Types.VideoCodecDecoder
|
||||
-- Legacy.CODECS --> { "flic", "cinepak", "msvideo1", "qtrle", "smc" }
|
||||
-- Legacy.CODECS --> { "flic", "cinepak", "msvideo1", "qtrle", "smc", "smackvid" }
|
||||
--
|
||||
-- Never throws: bad tracks return nil, Err; bad packets return Err from decode().
|
||||
local Err = require("@formats/core/Err")
|
||||
@@ -24,6 +25,7 @@ local Cinepak = require("@formats/codecs/video/Legacy/Cinepak")
|
||||
local Msvideo1 = require("@formats/codecs/video/Legacy/Msvideo1")
|
||||
local QtRle = require("@formats/codecs/video/Legacy/QtRle")
|
||||
local Smc = require("@formats/codecs/video/Legacy/Smc")
|
||||
local Smacker = require("@formats/codecs/video/Legacy/Smacker")
|
||||
|
||||
local Legacy = {}
|
||||
|
||||
@@ -33,7 +35,8 @@ Legacy.Cinepak = Cinepak
|
||||
Legacy.Msvideo1 = Msvideo1
|
||||
Legacy.QtRle = QtRle
|
||||
Legacy.Smc = Smc
|
||||
Legacy.CODECS = { "flic", "cinepak", "msvideo1", "qtrle", "smc" }
|
||||
Legacy.Smacker = Smacker
|
||||
Legacy.CODECS = { "flic", "cinepak", "msvideo1", "qtrle", "smc", "smackvid" }
|
||||
|
||||
export type Options = {
|
||||
maxPixels: number?, -- refuse bigger pictures (LIMIT; default 2^26)
|
||||
@@ -124,6 +127,30 @@ local CODECS: { [string]: Codec } = {
|
||||
return Picture.image(st.pic), nil
|
||||
end,
|
||||
},
|
||||
smackvid = {
|
||||
make = function(t)
|
||||
local cfg = t.config
|
||||
if type(cfg) ~= "buffer" then
|
||||
return nil, "no Smacker trees"
|
||||
end
|
||||
local trees, msg = Smacker.trees(cfg)
|
||||
if not trees then
|
||||
return nil, msg
|
||||
end
|
||||
return { pic = Picture.new(t.width :: number, t.height :: number, true, 4, 4, t.palette), trees = trees, v4 = t.codecTag == "SMK4" }, nil
|
||||
end,
|
||||
step = function(st, data, packet)
|
||||
packetPalette(st.pic, packet)
|
||||
if buffer.len(data) == 0 then
|
||||
return nil, fail("empty frame")
|
||||
end
|
||||
local ok, msg = Smacker.decode(st.pic, data, st.trees, st.v4)
|
||||
if not ok then
|
||||
return nil, fail(msg)
|
||||
end
|
||||
return Picture.image(st.pic), nil
|
||||
end,
|
||||
},
|
||||
flic = {
|
||||
make = function(t)
|
||||
return Flic.state(t.width :: number, t.height :: number, t.bitsPerPixel or 8)
|
||||
|
||||
Reference in new issue
Block a user