ws-video: video/mkv tests (ffprobe packets, ffmpeg audio, lacing / zlib / unknown clusters / subtitles, WebM remux), zlib content compression, codec-delay trimming of non-Opus audio (AudioWrap.trim), video/y4m, containers/Avi + video/avi (WIP tests)
This commit is contained in:
1 parent
8a905a4510
commit
9411cd97c6
14 files changed
+1862
-20
No files matched your search
@@ -0,0 +1,214 @@
|
||||
-- tests/video/_common: shared checks for the video container tests (mkv, avi, y4m, mpegts, mpegps, mp4): a demuxer's
|
||||
-- tracks and packets against ffprobe's, decoded frames against ffmpeg's (CRC-32 for bit-exact codecs, PSNR for the
|
||||
-- others), audio through the api's hook against ffmpeg's f32 decode, seeking. Not a test by itself.
|
||||
local F = require("@formats")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Crc32 = require("@formats/core/Crc32")
|
||||
local Video = require("@formats/codecs/Video")
|
||||
|
||||
local C = {}
|
||||
|
||||
-- the codecs whose decode must equal ffmpeg's bit for bit
|
||||
C.EXACT = { vp8 = true, rawvideo = true }
|
||||
|
||||
-- psnr(a, b, off?, n?) -> dB between two byte strings / buffers (math.huge when equal)
|
||||
function C.psnr(a: buffer, b: buffer, n: number?): number
|
||||
local len = n or math.min(buffer.len(a), buffer.len(b))
|
||||
local se = 0
|
||||
for i = 0, len - 1 do
|
||||
local d = buffer.readu8(a, i) - buffer.readu8(b, i)
|
||||
se += d * d
|
||||
end
|
||||
if se == 0 then
|
||||
return math.huge
|
||||
end
|
||||
return 10 * math.log10(255 * 255 * len / se)
|
||||
end
|
||||
|
||||
-- snr(ours f32 buffer, reference f32 string, n samples) -> dB
|
||||
function C.snr(ours: buffer, ref: string, n: number): number
|
||||
local rb = buffer.fromstring(ref)
|
||||
local sig, err = 0, 0
|
||||
for i = 0, n - 1 do
|
||||
local r = buffer.readf32(rb, i * 4)
|
||||
local d = buffer.readf32(ours, i * 4) - r
|
||||
sig += r * r
|
||||
err += d * d
|
||||
end
|
||||
if err == 0 then
|
||||
return math.huge
|
||||
end
|
||||
return 10 * math.log10(sig / err)
|
||||
end
|
||||
|
||||
-- packets(t, dmx, c, opts): every packet (file order) against ffprobe's: track, time, size, keyframe, CRC
|
||||
function C.packets(t: any, dmx: any, c: any, label: string, opts: any?)
|
||||
local o = opts or {}
|
||||
local trackOf = {} -- ffprobe stream index -> our track
|
||||
for i, s in c.streams do
|
||||
trackOf[s.index] = o.trackFor and o.trackFor(dmx, s) or dmx.tracks[i]
|
||||
end
|
||||
dmx:rewind()
|
||||
local k, bad = 0, 0
|
||||
local first = nil
|
||||
for _, want in c.packets do
|
||||
local tr = trackOf[want.stream]
|
||||
if not (o.skipStream and o.skipStream(want, tr)) then
|
||||
local p = dmx:read()
|
||||
k += 1
|
||||
if not p then
|
||||
t:check(false, label .. ": packet " .. k .. " missing")
|
||||
return
|
||||
end
|
||||
local ok = tr ~= nil and p.track == tr.id and buffer.len(p.data) == want.size and Crc32.of(p.data) == want.crc
|
||||
local s = c.streams[want.stream + 1]
|
||||
if ok and want.pts and s and not o.noPts then
|
||||
local ours = p.pts / tr.timescale - (tr.codecDelay or 0) -- ffmpeg shifts by Matroska's CodecDelay
|
||||
local theirs = want.pts * s.tbn / s.tbd
|
||||
ok = math.abs(ours - theirs) < 0.0015
|
||||
end
|
||||
if ok and not o.noKey and tr and tr.kind == "video" then
|
||||
ok = p.keyframe == want.key
|
||||
end
|
||||
if not ok then
|
||||
bad += 1
|
||||
if not first then
|
||||
first = string.format("packet %d: track %s/%s size %d/%d pts %s/%s key %s/%s crc %s", k, tostring(p.track), tostring(tr and tr.id), buffer.len(p.data), want.size, tostring(p.pts), tostring(want.pts), tostring(p.keyframe), tostring(want.key), tostring(Crc32.of(p.data) == want.crc))
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
t:check(bad == 0, label .. ": " .. k .. " packets equal ffprobe's (" .. bad .. " differ" .. (if first then "; first: " .. first else "") .. ")")
|
||||
t:check(dmx:read() == nil, label .. ": no extra packets")
|
||||
end
|
||||
|
||||
-- frames(t, v, c, label): every decoded frame against ffmpeg's (CRC for EXACT codecs, else PSNR of the raw ones)
|
||||
function C.frames(t: any, v: any, c: any, label: string, minPsnr: number?)
|
||||
if not c.frames then
|
||||
return
|
||||
end
|
||||
if not Video.canDecode(v.codec) then
|
||||
t:note("%s: no %s decoder, frames not checked", label, v.codec)
|
||||
return
|
||||
end
|
||||
v:reset()
|
||||
local n, bad, worst = 0, 0, math.huge
|
||||
local firstBad = nil
|
||||
while true do
|
||||
local f, err = v:nextFrame()
|
||||
if not f then
|
||||
if err and err.code == F.Err.UNSUPPORTED and string.find(err.message, "inter frames", 1, true) then
|
||||
t:note("%s: TODO inter frames not decodable yet", label) -- TEMP until ws/video-vp8 merges
|
||||
return
|
||||
end
|
||||
t:check(err == nil, label .. ": decode error " .. tostring(err))
|
||||
break
|
||||
end
|
||||
n += 1
|
||||
local img = f.image
|
||||
if img.format ~= "yuv420p" then
|
||||
t:check(false, label .. ": frame " .. n .. " is " .. img.format)
|
||||
break
|
||||
end
|
||||
if C.EXACT[v.codec] then
|
||||
if Crc32.of(img.pixels) ~= c.frames[n] then
|
||||
bad += 1
|
||||
firstBad = firstBad or n
|
||||
end
|
||||
elseif c.yuv and c.yuv[n] then
|
||||
local p = C.psnr(img.pixels, buffer.fromstring(c.yuv[n]))
|
||||
worst = math.min(worst, p)
|
||||
end
|
||||
end
|
||||
t:eq(n, #c.frames, label .. ": frame count")
|
||||
if C.EXACT[v.codec] then
|
||||
t:check(bad == 0, label .. ": every frame bit-exact with ffmpeg (" .. bad .. " differ, first " .. tostring(firstBad) .. ")")
|
||||
elseif c.yuv then
|
||||
t:check(worst >= (minPsnr or 38), string.format("%s: PSNR vs ffmpeg %.1f dB", label, worst))
|
||||
end
|
||||
end
|
||||
|
||||
-- audio(t, v, c, label, exact?): the audio decoder (through the api hook) against ffmpeg's f32
|
||||
function C.audio(t: any, v: any, c: any, label: string, exact: boolean?, minSnr: number?)
|
||||
if not c.pcm then
|
||||
return
|
||||
end
|
||||
if not v.audio then
|
||||
local code = v.audioError and v.audioError.code
|
||||
t:check(code == F.Err.UNKNOWN_FORMAT or code == F.Err.UNSUPPORTED, label .. ": audio missing only for a missing audio format (" .. tostring(v.audioError) .. ")")
|
||||
return
|
||||
end
|
||||
local a = v.audio
|
||||
a:seek(0)
|
||||
local want = #c.pcm // 4
|
||||
local out, got = a:read(want // a.channels)
|
||||
local n = got * a.channels
|
||||
t:check(n == want or n * 4 == c.pcmBytes, label .. ": " .. n .. " samples read of " .. want)
|
||||
n = math.min(n, want)
|
||||
if exact then
|
||||
local same = true
|
||||
local rb = buffer.fromstring(c.pcm)
|
||||
for i = 0, n - 1 do
|
||||
if math.abs(buffer.readf32(out, i * 4) - buffer.readf32(rb, i * 4)) > 1e-6 then
|
||||
same = false
|
||||
break
|
||||
end
|
||||
end
|
||||
t:check(same, label .. ": PCM equal to ffmpeg's")
|
||||
else
|
||||
local s = C.snr(out, c.pcm, n)
|
||||
t:check(s >= (minSnr or 30), string.format("%s: audio SNR vs ffmpeg %.1f dB", label, s))
|
||||
end
|
||||
end
|
||||
|
||||
-- seek(t, v, c, label, at): after seek(at) the next frame is the first at or after `at`
|
||||
function C.seek(t: any, v: any, c: any, label: string, at: number)
|
||||
if not c.frames or not Video.canDecode(v.codec) then
|
||||
return
|
||||
end
|
||||
v:seek(at)
|
||||
local f, ferr = v:nextFrame()
|
||||
if not f and ferr and string.find(ferr.message, "inter frames", 1, true) then
|
||||
v:reset()
|
||||
return -- TEMP until ws/video-vp8 merges
|
||||
end
|
||||
t:check(f ~= nil and f.pts >= at - 1e-6 and f.pts < at + 0.2, label .. ": seek to " .. at .. " lands at " .. tostring(f and f.pts))
|
||||
if f and C.EXACT[v.codec] then
|
||||
local idx = math.floor(f.pts * v.frameRate + 0.5) + 1
|
||||
t:eq(Crc32.of(f.image.pixels), c.frames[idx], label .. ": the frame after seeking equals ffmpeg's frame " .. idx)
|
||||
end
|
||||
v:reset()
|
||||
end
|
||||
|
||||
-- fuzzOpen(Fmt) -> a fuzz function: open, read every packet, decode up to 8 frames, read some audio
|
||||
function C.fuzzOpen(Fmt: any): (buffer) -> any
|
||||
return function(b: buffer): any
|
||||
local v = Fmt.open(Bytes.reader(b), { openAudio = function(d: any, tr: any): any
|
||||
local AudioWrap = require("@formats/containers/AudioWrap")
|
||||
AudioWrap.wrap(d, tr)
|
||||
return nil
|
||||
end })
|
||||
if not v then
|
||||
return nil
|
||||
end
|
||||
for _ = 1, 8 do
|
||||
local f = v:nextFrame()
|
||||
if not f then
|
||||
break
|
||||
end
|
||||
end
|
||||
if v.demuxer then
|
||||
v.demuxer:rewind()
|
||||
while v.demuxer:read() do
|
||||
end
|
||||
end
|
||||
return v
|
||||
end
|
||||
end
|
||||
|
||||
-- open(data, opts?) -> VideoDecoder through the api (category video)
|
||||
function C.open(data: string, opts: any?): (any, any)
|
||||
return F.video.open(buffer.fromstring(data), opts)
|
||||
end
|
||||
|
||||
return C
|
||||
@@ -0,0 +1,128 @@
|
||||
-- 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()
|
||||
Reference in new issue
Block a user