api: encoderFor finds image formats for a VideoDecoder, videoToImage() collects the frames; F.detect asks a format's optional variant(r) for a more precise name (video/mkv: Mkv.variant, from Parse.docType). codecs/video/Video: index after seek = round(pts * frameRate) + 1. Co-Authored-By: Claude Opus 5.5 <[email protected]>
228 lines
7.7 KiB
Luau
228 lines
7.7 KiB
Luau
-- 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/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, label, opts?): every track's packets against ffprobe's (in order): size, CRC, time, keyframe.
|
|
-- opts.skipStream(ffprobePacket, track) -> true skips a stream (ffmpeg's parsers re-split some, e.g. MPEG audio in TS)
|
|
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
|
|
local k: { [number]: number } = {}
|
|
local total, 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
|
|
total += 1
|
|
local id = if tr then tr.id else -1
|
|
k[id] = (k[id] or 0) + 1
|
|
local p = if tr then dmx:get(id, k[id]) else nil
|
|
local ok = p ~= nil and buffer.len(p.data) == want.size and Crc32.of(p.data) == want.crc
|
|
local s = c.streams[want.stream + 1]
|
|
if ok and p 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 p 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("track %s packet %d: size %s/%d pts %s/%s key %s/%s crc %s", tostring(id), k[id], tostring(p and buffer.len(p.data)), want.size, tostring(p and p.pts), tostring(want.pts), tostring(p and p.keyframe), tostring(want.key), tostring(p and Crc32.of(p.data) == want.crc))
|
|
end
|
|
end
|
|
end
|
|
end
|
|
local extra = 0
|
|
for id, n in k do
|
|
extra += math.max(0, dmx:count(id) - n)
|
|
end
|
|
t:check(bad == 0, label .. ": " .. total .. " packets equal ffprobe's (" .. bad .. " differ" .. (if first then "; first: " .. first else "") .. ")")
|
|
t:check(extra == 0, label .. ": no extra packets (" .. extra .. ")")
|
|
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
|
|
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 = buffer.create(want * 4)
|
|
local got = 0
|
|
while got * a.channels < want do
|
|
local chunk, k = a:read(want // a.channels - got)
|
|
if not chunk or k <= 0 then
|
|
break
|
|
end
|
|
buffer.copy(out, got * a.channels * 4, chunk, 0, k * a.channels * 4)
|
|
got += k
|
|
end
|
|
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 c.rgba) or not Video.canDecode(v.codec) then
|
|
return
|
|
end
|
|
if v.duration > 0 and at > v.duration * 0.6 then
|
|
at = math.floor(v.duration * 50) / 100
|
|
end
|
|
v:seek(at)
|
|
local f = v:nextFrame()
|
|
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 v.frameRate > 0 then
|
|
-- frame.index restarts at the seek position (not counted on from before the seek)
|
|
t:eq(f.index, math.floor(f.pts * v.frameRate + 0.5) + 1, label .. ": frame index after seeking")
|
|
local g = v:nextFrame()
|
|
t:check(g == nil or g.index == f.index + 1, label .. ": index counts on after the seek")
|
|
end
|
|
if f and C.EXACT[v.codec] and c.frames 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
|