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:
4DBugandClaude Opus 5.5 committed 2026-10-31 14:37:39 -05:00
1 parent 50e270086d
commit 4b098276e2
8 files changed
+1522 -3

No files matched your search

+180
View File
@@ -0,0 +1,180 @@
-- video/smk tests: Smacker files encoded by the fixture script (SMK2 and SMK4, every block type and run, the trees'
-- escape cache, palette updates, a ring frame, ms and 10 us frame times; packed 16-bit stereo / 8-bit mono / 16-bit
-- mono / 8-bit stereo audio and raw s16le / u8 tracks): every video frame and every audio track equal ffmpeg's
-- decode (CRC-32 of rgba / f32le), audio seeking, errors, and the fuzz pass on the container, the video codec and the
-- audio codec. Fixtures: tools/fixtures/smk.py.
local T = require("@tests/T")
local F = require("@formats")
local Smk = require("@formats/video/smk")
local SmkAudio = require("@formats/video/smk/Audio")
local Legacy = require("@formats/codecs/video/Legacy")
local Bytes = require("@formats/core/Bytes")
local Crc32 = require("@formats/core/Crc32")
local Err = require("@formats/core/Err")
local Fx = require("@tests/gen/smk_cases")
local t = T.suite("smk")
t:eq(Smk.name, "smk", "name")
t:eq(Smk.category, "video", "category")
local function reader(s: string): Bytes.Reader
return Bytes.reader(buffer.fromstring(s))
end
-- every frame of an audio Decoder -> f32 buffer, frames
local function drain(dec: any): (buffer, number)
local chunks, total = {}, 0
while true do
local b, n = dec:read(1000)
if n == 0 then
break
end
table.insert(chunks, { b, n })
total += n
end
local out = buffer.create(total * dec.channels * 4)
local at = 0
for _, c in chunks do
buffer.copy(out, at, c[1], 0, c[2] * dec.channels * 4)
at += c[2] * dec.channels * 4
end
return out, total
end
for _, c in Fx do
local label = c.name
t:check(Smk.sniff(reader(c.data)) == 1, label .. ": sniff")
t:eq(F.detect(buffer.fromstring(c.data)), "smk", label .. ": detect")
local v, err = F.video.open(buffer.fromstring(c.data))
t:check(v ~= nil, label .. ": open " .. tostring(err))
if v then
t:eq(v.codec, "smackvid", label .. ": codec")
t:check(v.width == c.width and v.height == c.height, label .. ": size")
t:check(math.abs(v.frameRate - c.rate) < 1e-6, label .. ": frame rate " .. v.frameRate)
local want = if c.ring then #c.frames - 1 else #c.frames
local n, bad = 0, 0
while true do
local f, ferr = v:nextFrame()
if not f then
t:check(ferr == nil, label .. ": " .. tostring(ferr))
break
end
n += 1
if f.image.format ~= "rgba8" or Crc32.of(f.image.pixels) ~= c.frames[n] then
bad += 1
end
end
t:check(n == want and bad == 0, string.format("%s: %d of %d frames equal ffmpeg's (%d differ)", label, n - bad, want, bad))
v:seek(0.2)
local f = v:nextFrame()
local k = math.floor(0.2 * c.rate - 1e-9) + 2
t:check(f ~= nil and Crc32.of(f.image.pixels) == c.frames[k], label .. ": seek to frame " .. k)
-- audio: the first track through the VideoDecoder, every track through Smk.openAudio
if #c.audio > 0 then
t:check(v.audio ~= nil, label .. ": audio " .. tostring(v.audioError))
end
local tracks = {}
for _, tr in v.demuxer.tracks do
if tr.kind == "audio" then
table.insert(tracks, tr)
end
end
t:eq(#tracks, #c.audio, label .. ": audio tracks")
for i, a in c.audio do
local tr = tracks[i]
t:eq(tr.codec, a.codec, label .. ": audio " .. i .. " codec")
local dec, aerr = Smk.openAudio(v.demuxer, tr)
t:check(dec ~= nil, label .. ": audio " .. i .. ": " .. tostring(aerr))
if dec then
t:check(dec.sampleRate == a.rate and dec.channels == a.channels, label .. ": audio " .. i .. " format")
local pcm, frames = drain(dec)
t:eq(frames, a.frames, label .. ": audio " .. i .. " frames")
t:eq(Crc32.of(pcm), a.crc, label .. ": audio " .. i .. " equals ffmpeg's")
dec:seek(0)
local again = drain(dec)
t:eq(Crc32.of(again), a.crc, label .. ": audio " .. i .. " after seek(0)")
dec:seek(0.3)
local tail, rest = drain(dec)
local skip = a.frames - rest
t:check(math.abs(skip - 0.3 * a.rate) <= 1, label .. ": audio " .. i .. " seek(0.3) at frame " .. skip)
local same = buffer.len(tail) > 0
for q = 0, math.min(buffer.len(tail), 4000) - 4, 4 do
if buffer.readu32(tail, q) ~= buffer.readu32(pcm, skip * a.channels * 4 + q) then
same = false
break
end
end
t:check(same, label .. ": audio " .. i .. " seek(0.3) samples")
end
end
if c.ring then
local v2 = Smk.open(reader(c.data), { ringFrame = true })
local m = 0
while v2 and v2:nextFrame() do
m += 1
end
t:eq(m, #c.frames, label .. ": with the ring frame")
end
end
end
-- errors
do
local _, e = Smk.open(reader(string.sub(Fx[1].data, 1, 60)))
t:check(e ~= nil and e.code == Err.TRUNCATED, "short header: TRUNCATED")
local _, e2 = Smk.open(reader("SMK3" .. string.sub(Fx[1].data, 5)))
t:check(e2 ~= nil and e2.code == Err.CORRUPT, "bad magic: CORRUPT")
t:eq(Smk.sniff(reader("SMK3" .. string.sub(Fx[1].data, 5))), 0, "bad magic: no sniff")
local _, e3 = Smk.open(reader(Fx[1].data), { maxPixels = 100 })
t:check(e3 ~= nil and e3.code == Err.LIMIT, "maxPixels: LIMIT")
local _, e4 = Smk.open(reader(string.sub(Fx[1].data, 1, 200)))
t:check(e4 ~= nil and e4.code == Err.TRUNCATED, "trees past the end: TRUNCATED")
end
-- fuzz: the container, the video codec (native, no pcall in between) and the audio codec
local samples = {}
for _, c in Fx do
table.insert(samples, c.data)
end
t:fuzz("open", function(b: buffer)
local v = Smk.open(Bytes.reader(b))
if v then
for _ = 1, 4 do
if not v:nextFrame() then
break
end
end
if v.audio then
v.audio:read(500)
end
end
return v
end, samples, { mutations = 16, garbage = 40, keepHeader = 4 })
local S, P = Legacy.Smacker, Legacy.Picture
for _, c in { Fx[1], Fx[2] } do
local d = Smk.demux(reader(c.data))
local tr = d.tracks[1]
local packets = {}
for k = 1, math.min(3, d:count(1)) do
table.insert(packets, d:get(1, k).data)
end
t:fuzz(c.name .. " trees", function(b: buffer)
return S.trees(b)
end, { tr.config }, { mutations = 30, garbage = 30, keepHeader = 16 })
local trees = assert(S.trees(tr.config))
t:fuzz(c.name .. " frames", function(b: buffer)
return S.decode(P.new(c.width, c.height, true, 4, 4), b, trees, tr.codecTag == "SMK4")
end, packets, { mutations = 30, garbage = 30 })
for _, at in d.tracks do
if at.kind == "audio" then
local ap = {}
for k = 1, math.min(2, d:count(at.id)) do
table.insert(ap, d:get(at.id, k).data)
end
t:fuzz(c.name .. " audio " .. at.codec, function(b: buffer)
return SmkAudio.decode(b, at.channels, at.bitsPerSample, at.codec == "smackaudio")
end, ap, { mutations = 20, garbage = 20 })
end
end
end
t:done()