ws-video: video/mpegts (containers/MpegTs absorbed from mpegts-luau 6e16197, its unit queue fixed for LuaJIT; M2TS, resync before the first packet, private stream types by PES id), video/mpegps (containers/MpegPs: MPEG-1 / MPEG-2 packs, PSM, private stream 1; pictures cut from the joined ES, PTS from PES / GOP temporal references; raw .m1v), containers/MpegEs; tests vs ffprobe / ffmpeg

This commit is contained in:
4DBug committed 2026-10-31 09:50:42 -05:00
1 parent a2763bee35
commit aa4963dc27
11 files changed
+1852 -15

No files matched your search

+18 -15
View File
@@ -41,45 +41,48 @@ function C.snr(ours: buffer, ref: string, n: number): number
return 10 * math.log10(sig / err)
end
-- packets(t, dmx, c, opts): every packet (file order) against ffprobe's: track, time, size, keyframe, CRC
-- 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
dmx:rewind()
local k, bad = 0, 0
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
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
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 want.pts and s and not o.noPts then
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 not o.noKey and tr and tr.kind == "video" then
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("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))
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
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")
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)
+48
View File
@@ -0,0 +1,48 @@
-- video/mpegps tests: program streams from ffmpeg (MPEG-1 system stream, B-frames, MPEG-2 PS / VOB packs, a raw .m1v
-- elementary stream): tracks, picture packets (count and timestamps against ffmpeg's frames), frames against ffmpeg
-- (PSNR) once an MPEG-1 decoder is registered, audio through the api hook (SNR), seeking, fuzz. Fixtures:
-- tools/fixtures/mpegts.py.
local T = require("@tests/T")
local C = require("@tests/video/_common")
local Ps = require("@formats/video/mpegps")
local Bytes = require("@formats/core/Bytes")
local Fx = require("@tests/gen/mpegps_cases")
local t = T.suite("mpegps")
t:eq(Ps.name, "mpegps", "name")
for _, c in Fx do
local label = c.name
t:check(Ps.sniff(Bytes.reader(buffer.fromstring(c.data))) > 0, label .. ": sniff")
local v, err = C.open(c.data)
t:check(v ~= nil, label .. ": open " .. tostring(err))
if v then
local dmx = v.demuxer
local vt = dmx:track("video")
t:check(vt ~= nil and vt.codec == "mpeg1", label .. ": an MPEG-1 video track")
t:eq(dmx:count(vt.id), #c.frames, label .. ": one packet per picture")
-- presentation times: one per frame slot
local seen, ok = {}, true
for k = 1, dmx:count(vt.id) do
local pts = dmx:info(vt.id, k)
local slot = math.floor((pts - dmx:info(vt.id, 1)) / 3600 + 0.5)
if seen[slot] then
ok = false
end
seen[slot] = true
end
t:check(ok, label .. ": distinct presentation times")
C.frames(t, v, c, label, 36)
if dmx:track("audio") then
C.audio(t, v, c, label, false, 30)
end
C.seek(t, v, c, label, 0.5)
end
end
local samples = {}
for _, c in Fx do
table.insert(samples, string.sub(c.data, 1, 30000))
end
t:fuzz("open", C.fuzzOpen(Ps), samples, { mutations = 6, garbage = 100 })
t:done()
+64
View File
@@ -0,0 +1,64 @@
-- video/mpegts tests: transport streams from ffmpeg (MPEG-1 + MP2, B-frames, H.264 + AAC, M2TS, garbage before the
-- first packet, a truncated file): tracks and the video packets against ffprobe (time, size, CRC, keyframe), frames
-- against ffmpeg (PSNR) once an MPEG-1 decoder is registered, audio through the api hook (SNR), seeking, fuzz.
-- Fixtures: tools/fixtures/mpegts.py.
local T = require("@tests/T")
local C = require("@tests/video/_common")
local Ts = require("@formats/video/mpegts")
local Bytes = require("@formats/core/Bytes")
local Fx = require("@tests/gen/mpegts_cases")
local t = T.suite("mpegts")
t:eq(Ts.name, "mpegts", "name")
local byName = {}
for _, c in Fx do
byName[c.name] = c
end
local function audioStream(want: any, tr: any): boolean
return tr == nil or tr.kind ~= "video" -- ffmpeg's parser re-splits PES audio into frames
end
for _, c in Fx do
local label = c.name
local v, err = C.open(c.data)
if c.truncated then
t:check(v ~= nil or err ~= nil, label .. ": opens or fails cleanly")
else
t:check(Ts.sniff(Bytes.reader(buffer.fromstring(c.data))) > 0 or label == "garbage.ts", label .. ": sniff")
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]
t:check(tr ~= nil and tr.kind == s.kind, label .. ": track " .. i .. " kind")
if tr and s.width and tr.width then -- (H.264 sizes come from its SPS, read by the codec)
t:check(tr.width == s.width and tr.height == s.height, label .. ": track " .. i .. " size")
end
end
if c.packets then
C.packets(t, dmx, c, label, { skipStream = audioStream })
end
C.frames(t, v, c, label, 36)
if dmx:track("audio") then
C.audio(t, v, c, label, false, 30)
end
C.seek(t, v, c, label, 0.5)
end
end
end
do
local v = C.open(byName["mpeg1_mp2.ts"].data)
t:eq(v.codec, "mpeg1", "stream type 0x02 without a sequence extension is MPEG-1")
t:eq(v.demuxer.tracks[2].codec, "mp2", "MPEG audio layer II")
t:eq(C.open(byName["h264_aac.ts"].data).codec, "h264", "H.264 track")
end
local samples = {}
for _, n in { "mpeg1_mp2.ts", "bframes.ts", "h264_aac.ts" } do
table.insert(samples, string.sub(byName[n].data, 1, 188 * 120))
end
t:fuzz("open", C.fuzzOpen(Ts), samples, { mutations = 6, garbage = 100 })
t:done()