Files
rosetta/tests/video/mp4.luau
T

85 lines
3.2 KiB
Luau

-- video/mp4 tests: MP4 / MOV files from ffmpeg (MJPEG + AAC / PCM / FLAC / MP2, H.264 with B-frames and edit lists +
-- AAC, fragmented MP4, MPEG-1 + Opus): tracks and every packet against ffprobe (pts moved by the edit lists, as ffmpeg),
-- frames against ffmpeg (PSNR), audio through the api hook (AAC priming dropped), seeking, our MP4 / MOV writer read
-- back, fuzz. Fixtures: tools/fixtures/mp4.py.
local T = require("@tests/T")
local C = require("@tests/video/_common")
local F = require("@formats")
local Mp4 = require("@formats/video/mp4")
local Bytes = require("@formats/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
-- our writer: MP4 (MJPEG + AAC copied) and MOV (MJPEG + PCM) read back packet for packet
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
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()