Files

126 lines
4.3 KiB
Luau

-- video/mp4 tests: MP4 / MOV files from ffmpeg against ffprobe (pts moved by edit lists, as ffmpeg) and ffmpeg.
-- Fixtures: tools/fixtures/mp4.py.
local T = require("@tests/T")
local C = require("@tests/video/_common")
local F = require("@rosetta")
local Mp4 = require("@rosetta/video/mp4")
local Bytes = require("@rosetta/core/Bytes")
local Fx = require("@tests/gen/mp4_cases")
local t = T.suite("mp4")
t:eq(Mp4.name, "mp4", "name")
local EXACT_AUDIO = { pcm_s16le = true, flac = true }
local byName = {}
for _, c in Fx do
byName[c.name] = c
end
for _, c in Fx do
local label = c.name
t:check(Mp4.sniff(Bytes.reader(buffer.fromstring(c.data))) >= 0.6, label .. ": sniff")
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")
C.packets(t, dmx, c, label)
C.frames(t, v, c, label, 38)
local at = dmx:track("audio")
if at then
C.audio(t, v, c, label, EXACT_AUDIO[at.codec], 30)
end
C.seek(t, v, c, label, 0.5)
end
end
for _, job in { { "mjpeg_aac.mp4", "mp4" }, { "mjpeg_pcm.mov", "mov" }, { "h264_aac.mp4", "mp4" } } do
local src = byName[job[1]]
local v = C.open(src.data)
local out, err = F.save(v, nil, { format = job[2] })
t:check(out ~= nil, "write " .. job[2] .. " from " .. job[1] .. ": " .. tostring(err))
if out then
local v2 = F.video.open(out)
t:check(v2 ~= nil, "write " .. job[1] .. ": reads back")
if v2 then
local a, b = v.demuxer, v2.demuxer
local same = #a.tracks == #b.tracks
for i, tr in a.tracks do
local tr2 = b.tracks[i]
if not tr2 or a:count(tr.id) ~= b:count(tr2.id) then
same = false
else
for k = 1, a:count(tr.id) do
local p, q = a:get(tr.id, k), b:get(tr2.id, k)
if not q or buffer.tostring(p.data) ~= buffer.tostring(q.data) or math.abs(p.pts / tr.timescale - q.pts / tr2.timescale) > 0.002 then
same = false
end
end
end
end
t:check(same, "write " .. job[1] .. ": same packets and times")
-- copied AAC keeps its edit list: priming and end padding cut as in the source (ffmpeg's length)
local at = a:track("audio")
if at and at.codec == "aac" then
local bt = b:track("audio")
t:check(bt and bt.playDuration and math.abs(bt.playDuration - at.playDuration) < 1e-3 and math.abs((bt.codecDelay or 0) - (at.codecDelay or 0)) < 1e-6, "write " .. job[1] .. ": edit list kept")
if v2.audio and src.pcmBytes then
local all = assert(F.ops.audio.toAudio(v2.audio))
t:eq(all.frames * all.channels * 4, src.pcmBytes, "write " .. job[1] .. ": AAC length = ffmpeg's decode of the source")
end
end
end
end
end
-- the yield hook (decoding in a coroutine that yields at every hook call) gives the same pictures
do
local Crc32 = require("@rosetta/core/Crc32")
local data = byName["h264_aac.mp4"].data
local function crcs(opts: any, inCoroutine: boolean): ({ number }, number, number, number)
local v = assert(F.video.open(data, opts))
local list, calls, w, h = {}, 0, 0, 0
local function body()
while true do
local f = v:nextFrame()
if not f then
break
end
w, h = f.image.width, f.image.height
table.insert(list, Crc32.of(f.image.pixels))
end
end
if inCoroutine then
local co = coroutine.create(body)
while coroutine.status(co) ~= "dead" do
local ok, err = coroutine.resume(co)
t:check(ok, "yield: resume " .. tostring(err))
calls += 1
end
else
body()
end
return list, calls, w, h
end
local plain, _, w0, h0 = crcs({ audio = false }, false)
local hooked, resumes = crcs({ audio = false, yield = function()
coroutine.yield()
end }, true)
t:check(#plain > 0, "h264: frames")
t:check(resumes > #plain, "h264 yield: the hook ran inside pictures (" .. resumes .. " resumes)")
local same = #plain == #hooked
for i, c in plain do
same = same and hooked[i] == c
end
t:check(same, "h264 yield: the same pictures as without the hook")
local _, _, w1, h1 = crcs({ audio = false, maxSide = 32 }, false)
t:check(math.max(w1, h1) == 32 and math.abs(w1 / h1 - w0 / h0) < 0.1, ("maxSide: %dx%d -> %dx%d"):format(w0, h0, w1, h1))
end
local samples = {}
for _, n in { "mjpeg_aac.mp4", "fragmented.mp4", "h264_aac.mp4" } do
table.insert(samples, string.sub(byName[n].data, 1, 30000))
end
t:fuzz("open", C.fuzzOpen(Mp4), samples, { mutations = 6, garbage = 100 })
t:done()