Files
rosetta/tests/video/fli.luau
T

103 lines
3.4 KiB
Luau

-- video/fli tests: FLI / FLC files encoded by the fixture script; every frame equals ffmpeg's decode (CRC-32 of rgba).
-- Fixtures: tools/fixtures/fli.py.
local T = require("@tests/T")
local F = require("@rosetta")
local Fli = require("@rosetta/video/fli")
local Bytes = require("@rosetta/core/Bytes")
local Crc32 = require("@rosetta/core/Crc32")
local Err = require("@rosetta/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
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 (native: no pcall in between)
local Flic = require("@rosetta/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()