Files
rosetta/tests/video/mkv.luau
T

129 lines
4.8 KiB
Luau

-- video/mkv tests: Matroska / WebM files from ffmpeg and mkvmerge (VP8 + Opus / Vorbis, MJPEG + PCM, MPEG-1 + MP2,
-- VP8 + FLAC, srt subtitles, Xiph / EBML / fixed lacing, zlib compression, unknown-size clusters, a truncated file):
-- tracks and every packet against ffprobe, frames against ffmpeg (bit-exact VP8, PSNR for MJPEG / MPEG-1), audio
-- through the api hook against ffmpeg, seeking, subtitles; our muxer's output (remux and MJPEG) read back. Fixtures:
-- tools/fixtures/mkv.py.
local T = require("@tests/T")
local C = require("@tests/video/_common")
local F = require("@formats")
local Mkv = require("@formats/video/mkv")
local Bytes = require("@formats/core/Bytes")
local Err = require("@formats/core/Err")
local Fx = require("@tests/gen/mkv_cases")
local t = T.suite("mkv")
-- 1. contract
t:eq(Mkv.name, "mkv", "name")
t:eq(Mkv.category, "video", "category")
t:check(table.find(Mkv.extensions, "webm") ~= nil, "webm extension")
local EXACT_AUDIO = { pcm_s16le = true, flac = true }
local byName = {}
for _, c in Fx do
byName[c.name] = c
end
-- 2. every fixture
for _, c in Fx do
local label = c.name
t:check(Mkv.sniff(Bytes.reader(buffer.fromstring(c.data))) > 0, label .. ": sniff")
if c.truncated then
local v, err = C.open(c.data)
t:check(v ~= nil or err ~= nil, label .. ": opens or fails cleanly")
if v then
local n = 0
while v:nextFrame() do
n += 1
end
t:note("%s: %d frames before the cut", label, n)
end
else
local v, err = C.open(c.data)
t:check(v ~= nil, label .. ": open: " .. tostring(err))
if v then
local dmx = v.demuxer
t:eq(#dmx.tracks, #c.streams, label .. ": track count")
for i, s in c.streams do
local tr = dmx.tracks[i]
if tr then
t:eq(tr.kind, s.kind, label .. ": track " .. i .. " kind")
if s.width then
t:check(tr.width == s.width and tr.height == s.height, label .. ": track " .. i .. " size")
end
if s.rate then
t:check(tr.sampleRate == s.rate and tr.channels == s.channels, label .. ": track " .. i .. " rate / channels")
end
end
end
-- Vorbis frames inside a lace carry no time of their own (ffmpeg derives them from the Vorbis block sizes)
C.packets(t, dmx, c, label, { noPts = c.name == "laced_vorbis.mkv" or c.name == "zlib_vorbis.mka" })
C.frames(t, v, c, label)
local at = dmx:track("audio")
if at then
C.audio(t, v, c, label, EXACT_AUDIO[at.codec], if at.codec == "vorbis" then 20 else 30)
end
C.seek(t, v, c, label, 0.6)
end
end
end
-- 3. specific files
do
local v = C.open(byName["vp8_opus.webm"].data)
t:eq(v.format, "webm", "webm: format")
t:eq(v.codec, "vp8", "webm: codec")
t:eq(v.width, 160, "webm: width")
t:check(math.abs(v.duration - 1) < 0.1, "webm: duration " .. v.duration)
t:check(#v.demuxer.meta.cues > 0, "webm: cues read")
local subs = Mkv.subtitles(C.open(byName["subs.mkv"].data).demuxer)
t:check(subs ~= nil and #subs.cues == 2, "subtitles: two cues")
if subs then
t:eq(subs.cues[1].text, "Hello", "subtitles: text")
t:check(math.abs(subs.cues[1].start - 0.1) < 0.002 and math.abs(subs.cues[1].stop - 0.5) < 0.002, "subtitles: times")
end
local lv = C.open(byName["lacing.mka"].data)
t:eq(lv.demuxer:count(1), 10, "lacing: 10 frames from three laced blocks")
local _, _, _, _, len = lv.demuxer:info(1, 4)
t:eq(len, 160, "lacing: EBML-laced frame size")
end
-- 4. our muxer: remux WebM (VP8 + Opus copied) and read it back packet for packet
do
local src = byName["vp8_opus.webm"]
local v = C.open(src.data)
local out, err = F.save(v, nil, { format = "webm" })
t:check(out ~= nil, "remux webm: " .. tostring(err))
if out then
local v2 = F.video.open(out)
t:check(v2 ~= nil and v2.format == "webm", "remux webm: reads back as webm")
if v2 then
local a, b = v.demuxer, v2.demuxer
t:eq(#b.tracks, 2, "remux webm: two tracks")
local same = a:count(1) == b:count(1) and a:count(2) == b:count(2)
for k = 1, a:count(1) do
local p, q = a:get(1, k), b:get(1, k)
if not q or p.pts ~= q.pts or buffer.tostring(p.data) ~= buffer.tostring(q.data) or p.keyframe ~= q.keyframe then
same = false
end
end
t:check(same, "remux webm: identical video packets")
C.frames(t, v2, src, "remux webm")
C.audio(t, v2, src, "remux webm", false, 30)
end
end
local bad, berr = F.save(C.open(byName["mjpeg_pcm.mkv"].data), nil, { format = "webm" })
t:check(bad == nil and berr ~= nil and berr.code == Err.UNSUPPORTED, "webm refuses MJPEG: " .. tostring(berr))
end
-- 5. errors and the fuzz pass
local e1 = select(2, Mkv.open(Bytes.reader(buffer.fromstring("\26\69\223\163\128"))))
t:check(e1 ~= nil, "empty EBML header fails")
local samples = {}
for _, n in { "vp8_opus.webm", "mjpeg_pcm.mkv", "lacing.mka", "zlib_vorbis.mka", "subs.mkv" } do
local d = byName[n].data
table.insert(samples, string.sub(d, 1, math.min(#d, 60000)))
end
t:fuzz("open", C.fuzzOpen(Mkv), samples, { mutations = 6, garbage = 100 })
t:done()