legacy video: bench lines (320x240 / 320x200 fixtures), Smacker frame decoder through 10-bit lookup tables
This commit is contained in:
1 parent
bf639260e4
commit
d99b026b07
5 files changed
+157
-56
No files matched your search
@@ -9,12 +9,13 @@
|
||||
-- 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.
|
||||
-- Trees are flat arrays (a node stores the size of its left subtree, as a negative number); the frame decoder reads
|
||||
-- 10 bits at a time through a lookup table into them.
|
||||
-- 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 readu8, readu32, writeu8, writeu32 = buffer.readu8, buffer.readu32, buffer.writeu8, buffer.writeu32
|
||||
local band, rshift = bit32.band, bit32.rshift
|
||||
|
||||
local Smacker = {}
|
||||
@@ -135,7 +136,7 @@ function Smacker.tree8(br: Bits): ({ number }?, string?)
|
||||
end
|
||||
local tree8 = Smacker.tree8
|
||||
|
||||
export type BigTree = { vals: { number }, last: { number } } -- last: the escape leaves' indexes (1-based)
|
||||
export type BigTree = { vals: { number }, last: { number }, lut: buffer? } -- 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?)
|
||||
@@ -213,29 +214,26 @@ function Smacker.trees(config: buffer): (Trees?, string?)
|
||||
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
|
||||
local LUT_BITS = 10
|
||||
|
||||
-- the first LUT_BITS bits of a code -> (entry index * 32 + bits used): a leaf, or the node reached after LUT_BITS bits
|
||||
local function lut(vals: { number }): buffer
|
||||
local size = 2 ^ LUT_BITS
|
||||
local t = buffer.create(size * 4)
|
||||
for pat = 0, size - 1 do
|
||||
local i, len = 1, 0
|
||||
local e = vals[1]
|
||||
while e < 0 and len < LUT_BITS do
|
||||
if band(rshift(pat, len), 1) == 1 then
|
||||
i += -e - 1
|
||||
end
|
||||
i += 1
|
||||
len += 1
|
||||
e = vals[i]
|
||||
end
|
||||
if bit(br) == 1 then
|
||||
i += -e - 1
|
||||
end
|
||||
i += 1
|
||||
e = vals[i]
|
||||
writeu32(t, pat * 4, i * 32 + len)
|
||||
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
|
||||
return t
|
||||
end
|
||||
|
||||
local function reset(t: BigTree)
|
||||
@@ -248,17 +246,65 @@ function Smacker.decode(pic: Picture.Picture, d: buffer, trees: Trees, v4: boole
|
||||
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))
|
||||
for _, t in { mmap, mclr, full, typ } do
|
||||
reset(t)
|
||||
if not t.lut then
|
||||
t.lut = lut(t.vals)
|
||||
end
|
||||
end
|
||||
local n = buffer.len(d)
|
||||
local nbits = n * 8
|
||||
local pos = 0
|
||||
-- the next value of a tree (updating its recent-value cache) | -1 when the bits run out inside the tree
|
||||
local function code(t: BigTree): number
|
||||
local vals = t.vals
|
||||
local i, p
|
||||
local byte = rshift(pos, 3)
|
||||
if byte + 3 <= n then
|
||||
local v = band(rshift(readu8(d, byte) + readu8(d, byte + 1) * 256 + readu8(d, byte + 2) * 65536, band(pos, 7)), 1023)
|
||||
local ent = readu32(t.lut :: buffer, v * 4)
|
||||
i = rshift(ent, 5)
|
||||
p = pos + band(ent, 31)
|
||||
else
|
||||
i, p = 1, pos
|
||||
end
|
||||
local e = vals[i]
|
||||
while e < 0 do
|
||||
if p >= nbits then
|
||||
return -1
|
||||
end
|
||||
if band(rshift(readu8(d, rshift(p, 3)), band(p, 7)), 1) == 1 then
|
||||
i += -e - 1
|
||||
end
|
||||
p += 1
|
||||
i += 1
|
||||
e = vals[i]
|
||||
end
|
||||
pos = p
|
||||
local last = t.last
|
||||
local l0 = last[1]
|
||||
if e ~= vals[l0] then
|
||||
local l1 = last[2]
|
||||
vals[last[3]] = vals[l1]
|
||||
vals[l1] = vals[l0]
|
||||
vals[l0] = e
|
||||
end
|
||||
return e
|
||||
end
|
||||
local function bit(): number
|
||||
local p = pos
|
||||
pos = p + 1
|
||||
if p >= nbits then
|
||||
return 0
|
||||
end
|
||||
return band(rshift(readu8(d, rshift(p, 3)), band(p, 7)), 1)
|
||||
end
|
||||
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
|
||||
local ty = code(typ)
|
||||
if ty < 0 then
|
||||
return false, "truncated frame"
|
||||
end
|
||||
local run = RUNS[band(rshift(ty, 2), 63) + 1]
|
||||
@@ -283,9 +329,9 @@ function Smacker.decode(pic: Picture.Picture, d: buffer, trees: Trees, v4: boole
|
||||
if blk >= blocks then
|
||||
break
|
||||
end
|
||||
local clr = code(mclr, br)
|
||||
local map = code(mmap, br)
|
||||
if not clr or not map then
|
||||
local clr = code(mclr)
|
||||
local map = code(mmap)
|
||||
if clr < 0 or map < 0 then
|
||||
return false, "truncated frame"
|
||||
end
|
||||
local hi, lo = rshift(clr, 8), band(clr, 255)
|
||||
@@ -303,12 +349,12 @@ function Smacker.decode(pic: Picture.Picture, d: buffer, trees: Trees, v4: boole
|
||||
else
|
||||
local mode = 0
|
||||
if v4 then
|
||||
if br.pos >= br.nbits then
|
||||
if pos >= nbits then
|
||||
return false, "truncated frame"
|
||||
end
|
||||
if bit(br) == 1 then
|
||||
if bit() == 1 then
|
||||
mode = 1
|
||||
elseif bit(br) == 1 then
|
||||
elseif bit() == 1 then
|
||||
mode = 2
|
||||
end
|
||||
end
|
||||
@@ -319,33 +365,29 @@ function Smacker.decode(pic: Picture.Picture, d: buffer, trees: Trees, v4: boole
|
||||
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
|
||||
local a = code(full)
|
||||
local b = code(full)
|
||||
if a < 0 or b < 0 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))
|
||||
-- the first code is the right pixel pair, the second the left one
|
||||
writeu32(px, o + r * stride, b + a * 65536)
|
||||
end
|
||||
elseif mode == 1 then
|
||||
for half = 0, 1 do
|
||||
local a = code(full, br)
|
||||
if not a then
|
||||
local a = code(full)
|
||||
if a < 0 then
|
||||
return false, "truncated frame"
|
||||
end
|
||||
local lo, hi = band(a, 255), rshift(a, 8)
|
||||
local v = lo * 0x0101 + hi * 0x01010000
|
||||
local v = band(a, 255) * 0x0101 + rshift(a, 8) * 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
|
||||
local p2 = code(full)
|
||||
local p1 = code(full)
|
||||
if p1 < 0 or p2 < 0 then
|
||||
return false, "truncated frame"
|
||||
end
|
||||
local v = p1 + p2 * 65536
|
||||
|
||||
Reference in new issue
Block a user