merge ws/video2-legacy (FLIC, Cinepak, MSV1, QT RLE / SMC, Smacker; video/fli, video/smk)

This commit is contained in:
4DBug committed 2026-10-31 15:23:13 -05:00
commit 9694c652ec
32 files changed
+4786 -5

No files matched your search

+113
View File
@@ -0,0 +1,113 @@
-- codecs/video/Legacy tests: Cinepak (24-bit, grey, 8-bit palettised, several strips, a 92x68 size), Microsoft Video 1
-- (16-bit, 8-bit with an AVI palette), QuickTime RLE (depths 1 / 2 / 4 / 8 with colour tables, default Macintosh
-- tables and grey ramps; 16 / 24 / 32-bit and grey from ffmpeg) and QuickTime SMC, in AVI, MOV and MKV: every frame
-- equals ffmpeg's decode (CRC-32 of rgba); seeking; the registry entries; the fuzz pass on each codec's packet decoder.
-- Fixtures: tools/fixtures/legacy.py.
local T = require("@tests/T")
local F = require("@formats")
local Crc32 = require("@formats/core/Crc32")
local Video = require("@formats/codecs/video/Video")
local Legacy = require("@formats/codecs/video/Legacy")
local Fx = require("@tests/gen/legacy_cases")
local t = T.suite("legacy")
-- the registry
t:eq(Video.fromFourcc("cvid"), "cinepak", "cvid")
t:eq(Video.fromFourcc("CRAM"), "msvideo1", "CRAM")
t:eq(Video.fromFourcc("msvc"), "msvideo1", "msvc")
t:eq(Video.fromFourcc("rle "), "qtrle", "rle ")
t:eq(Video.fromFourcc("smc "), "smc", "smc ")
for _, id in Legacy.CODECS do
t:check(Video.canDecode(id), id .. ": decodable")
end
local samples: { [string]: { any } } = {}
for _, c in Fx do
local label = c.name
local v, err = F.video.open(c.data)
t:check(v ~= nil, label .. ": open: " .. tostring(err))
if v then
t:eq(v.codec, c.codec, label .. ": codec")
t:eq(v.width, c.width, label .. ": width")
local n, bad, first = 0, 0, nil
while true do
local f, ferr = v:nextFrame()
if not f then
t:check(ferr == nil, label .. ": decode error " .. tostring(ferr))
break
end
n += 1
local img = f.image
if img.format ~= "rgba8" or img.width ~= c.width or Crc32.of(img.pixels) ~= c.frames[n] then
bad += 1
first = first or n
end
end
t:check(n == #c.frames and bad == 0, string.format("%s: %d of %d frames equal ffmpeg's (%d differ, first %s)", label, n - bad, #c.frames, bad, tostring(first)))
-- seeking: back to the start, the first frame again
v:seek(0)
local f = v:nextFrame()
t:check(f ~= nil and Crc32.of(f.image.pixels) == c.frames[1], label .. ": seek(0)")
-- packets for the fuzz pass
local dmx = v.demuxer
local tr = dmx:track("video")
if not samples[c.codec] then
local list = {}
for k = 1, math.min(3, dmx:count(tr.id)) do
table.insert(list, dmx:get(tr.id, k).data)
end
samples[c.codec] = { track = tr, packets = list }
end
end
end
-- the fuzz pass on the native packet decoders (no pcall in between)
local P = Legacy.Picture
local raw: { [string]: (tr: any) -> (buffer) -> any } = {
cinepak = function(tr)
return function(b)
local pic = P.new(tr.width, tr.height, tr.bitsPerPixel == 8, 4, 4, tr.palette)
return Legacy.Cinepak.decode(pic, b, Legacy.Cinepak.state(), false)
end
end,
msvideo1 = function(tr)
return function(b)
local bits = if tr.bitsPerPixel == 8 then 8 else 16
return Legacy.Msvideo1.decode(P.new(tr.width, tr.height, bits == 8, 4, 4), b, bits)
end
end,
qtrle = function(tr)
return function(b)
local d = tr.bitsPerPixel or 24
for _, depth in { d, 1, 2, 4, 8, 16, 32 } do
Legacy.QtRle.decode(P.new(tr.width, tr.height, Legacy.QtRle.palettised(depth), Legacy.QtRle.pad(depth), 1), b, depth)
end
return true
end
end,
smc = function(tr)
return function(b)
return Legacy.Smc.decode(P.new(tr.width, tr.height, true, 4, 4), b, Legacy.Smc.tables())
end
end,
}
for id, s in samples do
local fn = raw[id]
if fn then
t:fuzz(id .. " packets", fn(s.track), s.packets, { mutations = 24, garbage = 60 })
end
end
t:fuzz("open", function(b: buffer)
local v = F.video.open(b)
if v then
for _ = 1, 3 do
if not v:nextFrame() then
break
end
end
end
return v
end, { Fx[1].data, Fx[#Fx].data }, { mutations = 8, garbage = 20 })
t:done()
+105
View File
@@ -0,0 +1,105 @@
-- video/fli tests: FLI (COLOR_64, BRUN, LC, BLACK, COPY, PSTAMP, a ring frame), FLC (prefix chunk, COLOR_256, SS2 with
-- line skips and the last-byte opcode, an empty frame), an odd-size FLC, and 15 / 16 / 24-bit FLCs (BRUN, DTA_BRUN,
-- DELTA, DTA_LC, COPY, DTA_COPY, BLACK), all encoded by the fixture script: every frame equals ffmpeg's decode
-- (CRC-32 of rgba), frame rates, the ring frame option, seeking, errors, the fuzz pass. Fixtures: tools/fixtures/fli.py.
local T = require("@tests/T")
local F = require("@formats")
local Fli = require("@formats/video/fli")
local Bytes = require("@formats/core/Bytes")
local Crc32 = require("@formats/core/Crc32")
local Err = require("@formats/core/Err")
local Fx = require("@tests/gen/fli_cases")
local t = T.suite("fli")
t:eq(Fli.name, "fli", "name")
t:eq(Fli.category, "video", "category")
local function reader(s: string): Bytes.Reader
return Bytes.reader(buffer.fromstring(s))
end
for _, c in Fx do
local label = c.name
t:check(Fli.sniff(reader(c.data)) > 0, label .. ": sniff")
t:eq(F.detect(buffer.fromstring(c.data)), "fli", 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, "flic", label .. ": codec")
t:check(v.width == c.width and v.height == c.height, label .. ": size")
t:check(math.abs(v.frameRate - c.rate) < 1e-9, label .. ": frame rate " .. v.frameRate)
t:eq(v.track.bitsPerPixel, c.depth, label .. ": depth")
local ring = v.demuxer.meta.ringFrame
local want = if 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.25)
local f = v:nextFrame()
local k = math.floor(0.25 * c.rate + 1 - 1e-9) + 1
t:check(f ~= nil and Crc32.of(f.image.pixels) == c.frames[k], label .. ": seek to frame " .. k)
if ring then
local v2 = Fli.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 = Fli.open(reader(string.sub(Fx[1].data, 1, 100)))
t:check(e ~= nil and e.code == Err.TRUNCATED, "short header: TRUNCATED")
local bad = Fx[1].data
bad = string.sub(bad, 1, 4) .. "\0\0" .. string.sub(bad, 7)
t:eq(Fli.sniff(reader(bad)), 0, "bad magic: no sniff")
local _, e2 = Fli.open(reader(bad))
t:check(e2 ~= nil and e2.code == Err.CORRUPT, "bad magic: CORRUPT")
local big = string.sub(Fx[1].data, 1, 8) .. "\255\15\255\15" .. string.sub(Fx[1].data, 13)
local _, e3 = Fli.open(reader(big), { maxPixels = 2 ^ 20 })
t:check(e3 ~= nil and e3.code == Err.LIMIT, "maxPixels: LIMIT")
end
local samples = {}
for _, c in Fx do
table.insert(samples, c.data)
end
t:fuzz("open", function(b: buffer)
local v = Fli.open(Bytes.reader(b))
if v then
while v:nextFrame() do
end
end
return v
end, samples, { mutations = 12, garbage = 40, keepHeader = 16 })
-- the frame decoder itself (codecs/video/Legacy/Flic, native: no pcall in between)
local Flic = require("@formats/codecs/video/Legacy").Flic
for _, c in { Fx[1], Fx[2], Fx[4], Fx[6] } do
local d = Fli.demux(reader(c.data))
local packets = {}
for k = 1, math.min(4, d:count(1)) do
table.insert(packets, d:get(1, k).data)
end
t:fuzz(c.name .. " frames", function(b: buffer)
local st = Flic.state(c.width, c.height, c.depth)
if st then
Flic.decode(st, b)
return Flic.image(st)
end
return nil
end, packets, { mutations = 20, garbage = 30 })
end
t:done()
+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()