From d99b026b0711e60a59317e432a3703f74a8dbd9f Mon Sep 17 00:00:00 2001 From: 4DBug Date: Sat, 31 Oct 2026 14:41:29 -0500 Subject: [PATCH] legacy video: bench lines (320x240 / 320x200 fixtures), Smacker frame decoder through 10-bit lookup tables --- src/Formats/codecs/video/Legacy/Smacker.luau | 146 ++++++++++++------- tools/bench.luau | 39 +++++ tools/fixtures/fli.py | 6 + tools/fixtures/legacy.py | 8 + tools/fixtures/smk.py | 14 +- 5 files changed, 157 insertions(+), 56 deletions(-) diff --git a/src/Formats/codecs/video/Legacy/Smacker.luau b/src/Formats/codecs/video/Legacy/Smacker.luau index c5923ec..eab6650 100644 --- a/src/Formats/codecs/video/Legacy/Smacker.luau +++ b/src/Formats/codecs/video/Legacy/Smacker.luau @@ -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 diff --git a/tools/bench.luau b/tools/bench.luau index b32ee01..f593634 100644 --- a/tools/bench.luau +++ b/tools/bench.luau @@ -1078,3 +1078,42 @@ do jitTune(false) end end +-- ------------------------------------------------------------------------------------------- ws-video2-legacy: fps +do + -- the legacy codecs (codecs/video/Legacy) through F.video.open, rgba8 frames out: ffmpeg's encoders at 320x240 + -- (tools/fixtures/legacy.py), a 320x200 FLC (fli.py) and a 320x200 SMK4 (smk.py, textured: mostly FULL blocks); + -- frames per second of the whole decode (and the Smacker audio track, seconds of audio per second) + local F = require("@formats") + local LB = require("@tests/gen/legacy_bench") + local cases = table.clone(LB) + table.insert(cases, { name = "fli.flc", data = require("@tests/gen/fli_bench").data, width = 320, height = 200 }) + table.insert(cases, { name = "smk4.smk", data = require("@tests/gen/smk_bench").data, width = 320, height = 200 }) + for _, c in cases do + local data = buffer.fromstring(c.data) + local frames = 0 + local dt = math.huge + for _ = 1, 3 do + local t0 = os.clock() + local v = assert(F.video.open(data, { audio = false })) + frames = 0 + while v:nextFrame() do + frames += 1 + end + dt = math.min(dt, os.clock() - t0) + end + print(string.format("%-8s %-30s %8.2f ms %7.1f fps", runtime, string.format("legacy %s %dx%d", c.name, c.width, c.height), dt * 1000 / frames, frames / dt)) + end + local v = assert(F.video.open(buffer.fromstring(require("@tests/gen/smk_bench").data))) + local a = assert(v.audio) + local t0 = os.clock() + local n = 0 + while true do + local _, got = a:read(4096) + if got == 0 then + break + end + n += got + end + local dt = os.clock() - t0 + print(string.format("%-8s %-30s %8.2f ms %7.1fx real time", runtime, "smacker audio 22 kHz stereo", dt * 1000, n / a.sampleRate / dt)) +end diff --git a/tools/fixtures/fli.py b/tools/fixtures/fli.py index 0428f02..e7ad3d6 100644 --- a/tools/fixtures/fli.py +++ b/tools/fixtures/fli.py @@ -393,3 +393,9 @@ def generate(ctx): assert crcs, name cases.append({"name": name, "data": data, "width": w, "height": h, "depth": depth, "rate": rate, "frames": crcs}) ctx.emit("fli_cases", cases) + # tools/bench.luau: a 320x200 FLC, 10 frames (BRUN then SS2 deltas) + w, h, n = 320, 200, 10 + imgs = idx_frames(w, h, n, 256, 9) + frames = [frame([color([(0, pal(256, 9, False))], False), brun([[bytes([v]) for v in r] for r in imgs[0]], 1, True)])] + frames += [frame([ss2(imgs[t], imgs[t - 1])]) for t in range(1, n)] + ctx.emit("fli_bench", {"data": file(0xAF12, w, h, 8, 66, frames), "width": w, "height": h, "frames": n}) diff --git a/tools/fixtures/legacy.py b/tools/fixtures/legacy.py index 863b866..76353ce 100644 --- a/tools/fixtures/legacy.py +++ b/tools/fixtures/legacy.py @@ -98,3 +98,11 @@ def generate(ctx): for c in cases: assert c["frames"], c["name"] + ": ffmpeg decoded no frame" ctx.emit("legacy_cases", cases) + # tools/bench.luau: 320x240, 10 frames of each codec + bench = [] + big = ["-f", "lavfi", "-i", "testsrc2=size=320x240:rate=10", "-t", "1"] + for name, args in [("cinepak.avi", ["-c:v", "cinepak"]), ("msvideo1.avi", ["-c:v", "msvideo1"]), + ("qtrle.mov", ["-c:v", "qtrle", "-pix_fmt", "rgb24"]), ("smc.mov", ["-c:v", "smc"])]: + ff(*big, *args, "bench_" + name) + bench.append({"name": name, "data": open(os.path.join(tmp, "bench_" + name), "rb").read(), "width": 320, "height": 240}) + ctx.emit("legacy_bench", bench) diff --git a/tools/fixtures/smk.py b/tools/fixtures/smk.py index 0d9ee5a..0bfba7a 100644 --- a/tools/fixtures/smk.py +++ b/tools/fixtures/smk.py @@ -381,11 +381,14 @@ def tone(n, ch, bits, rate, seed, f0): # ---------------------------------------------------------------------------------------------------- files -def idx_frames(w, h, n, seed): +def idx_frames(w, h, n, seed, busy=False): rnd = random.Random(seed) out = [] for t in range(n): - img = [[(x // 8 + y // 8 * 3) % 40 for x in range(w)] for y in range(h)] + if busy: # textured, moving: mostly FULL blocks (the bench) + img = [[(x * 3 + y * 5 + ((x + t * 4) * (y + t)) // 7) % 251 for x in range(w)] for y in range(h)] + else: + img = [[(x // 8 + y // 8 * 3) % 40 for x in range(w)] for y in range(h)] for y in range(8, 24): for x in range(8 + t * 4, 24 + t * 4): img[y][x] = 100 + (x + y) % 2 # two colours: MONO blocks @@ -399,10 +402,10 @@ def idx_frames(w, h, n, seed): return out -def smk_file(version, w, h, nframes, pts_inc, tracks, ring, seed): +def smk_file(version, w, h, nframes, pts_inc, tracks, ring, seed, busy=False): """version 2 / 4; tracks = [(kind, rate, ch, bits)] kind "packed" / "raw"; returns bytes, track info""" rnd = random.Random(seed) - frames = idx_frames(w, h, nframes, seed) + frames = idx_frames(w, h, nframes, seed, busy) if ring: frames = frames + [frames[0]] treebytes, sizes, video = encode_video(frames, w, h, version == 4, rnd) @@ -491,3 +494,6 @@ def generate(ctx): cases.append({"name": name, "data": data, "width": w, "height": h, "rate": rate, "frames": crcs, "audio": audio, "ring": ring}) ctx.emit("smk_cases", cases) + # tools/bench.luau: a 320x200 SMK4 with 22.05 kHz packed stereo audio, 10 frames + ctx.emit("smk_bench", {"data": smk_file(4, 320, 200, 10, 100, [("packed", 22050, 2, 16)], False, 9, True), "width": 320, + "height": 200, "frames": 10})