176 lines
6.0 KiB
Luau
176 lines
6.0 KiB
Luau
-- video/smk tests: Smacker files by the fixture script; video and audio equal ffmpeg's decode (CRC-32 of rgba / f32le).
|
|
-- Fixtures: tools/fixtures/smk.py.
|
|
local T = require("@tests/T")
|
|
local F = require("@rosetta")
|
|
local Smk = require("@rosetta/video/smk")
|
|
local SmkAudio = require("@rosetta/video/smk/Audio")
|
|
local Legacy = require("@rosetta/codecs/video/Legacy")
|
|
local Bytes = require("@rosetta/core/Bytes")
|
|
local Crc32 = require("@rosetta/core/Crc32")
|
|
local Err = require("@rosetta/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
|
|
|
|
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
|
|
|
|
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 video codec is native, no pcall in between
|
|
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()
|