Files
rosetta/tests/containers/isobmff.luau
T

231 lines
9.0 KiB
Luau

-- containers/IsoBmff tests: ffmpeg / MP4Box files against ffprobe (-ignore_editlist), AVIF items, the muxer.
-- Fixtures: tools/fixtures/isobmff.py.
local T = require("@tests/T")
local IsoBmff = require("@rosetta/containers/IsoBmff")
local Bytes = require("@rosetta/core/Bytes")
local Crc32 = require("@rosetta/core/Crc32")
local Fx = require("@tests/gen/isobmff")
local t = T.suite("isobmff")
local CODEC = { h264 = "h264", aac = "aac", mjpeg = "mjpeg", pcm_s16be = "pcm", hevc = "hevc", alac = "alac" }
local function reader(s: string, pad: number?): Bytes.Reader
if pad then
local b = buffer.create(#s + pad * 2)
buffer.writestring(b, pad, s)
return Bytes.reader(b, pad, #s)
end
return Bytes.reader(buffer.fromstring(s))
end
for _, c in Fx.cases do
for _, pad in { 0, 5 } do
local r = reader(c.data, if pad > 0 then pad else nil)
local label = c.name .. (if pad > 0 then " (sub-range)" else "")
t:check(IsoBmff.sniff(r) ~= nil, label .. ": sniff")
local dmx, err = IsoBmff.demux(r)
t:check(dmx, label .. ": " .. tostring(err))
t:eq(#dmx.tracks, #c.streams, label .. ": track count")
for i, s in c.streams do
local tr = dmx.tracks[i]
t:eq(tr.kind, s.kind, label .. ": track " .. i .. " kind")
t:eq(tr.codec, CODEC[s.codec] or s.codec, label .. ": track " .. i .. " codec")
t:eq(tr.timescale, s.timescale, label .. ": timescale")
if s.width then
t:check(tr.width == s.width and tr.height == s.height, label .. ": video size")
end
if s.sampleRate then
t:check(tr.sampleRate == s.sampleRate and tr.channels == s.channels, label .. ": audio format")
end
if s.extradata > 0 and s.codec ~= "alac" then
t:eq(if tr.config then buffer.len(tr.config) else 0, s.extradata, label .. ": codec config size")
end
end
local n, bad = 0, nil
local base = r.base
for p in dmx:packets() do
n += 1
local want = c.packets[n]
if not want then
bad = bad or ("extra packet " .. n)
break
end
local tr = dmx.tracks[want.stream]
if p.track ~= tr.id then
bad = bad or ("packet " .. n .. " track")
end
local off = tr.offsets[p.index]
if off - base ~= want.pos or buffer.len(p.data) ~= want.size or p.dts ~= want.dts or p.pts ~= want.pts or p.keyframe ~= want.key then
bad = bad or string.format("packet %d: pos %d/%d size %d/%d dts %d/%d pts %d/%d key %s/%s", n, off - base, want.pos, buffer.len(p.data), want.size, p.dts, want.dts, p.pts, want.pts, tostring(p.keyframe), tostring(want.key))
end
end
t:check(bad == nil, label .. ": " .. tostring(bad))
t:eq(n, #c.packets, label .. ": packet count")
if pad == 0 then
t:note("%s: %d tracks, %d packets match ffprobe", c.name, #c.streams, n)
end
end
end
do
local c = Fx.cases[1]
local dmx = IsoBmff.demux(reader(c.data))
local v = dmx.tracks[1]
local count = 0
for p in dmx:packets(v.id) do
count += 1
t:eq(p.track, v.id, "per-track packets")
end
t:eq(count, v.sampleCount, "per-track packet count")
dmx:seek(0.75)
local p = dmx:packets(v.id)()
t:check(p and p.keyframe and p.dts / v.timescale <= 0.75, "seek lands on a keyframe before the time")
t:check(v.skip >= 0 and v.edits ~= nil, "edit list parsed")
end
do
local r = reader(Fx.tagged.data)
local movie = IsoBmff.parse(r) :: IsoBmff.Movie
local tags, pics = IsoBmff.tags(r, movie)
for k, v in Fx.tagged.tags do
t:eq(tags[k], v, "tag " .. k)
end
t:check(tags.encoder ~= nil, "encoder tag")
t:eq(#pics, 1, "one cover")
t:eq(pics[1] and buffer.len(pics[1].data), Fx.tagged.cover, "cover bytes")
t:eq(pics[1] and pics[1].mime, "image/png", "cover mime")
end
for _, c in Fx.avif do
local r = reader(c.data)
t:check(IsoBmff.sniff(r) == "avif", c.name .. ": brand")
local movie = IsoBmff.parse(r) :: IsoBmff.Movie
t:check(movie and #movie.tracks == 0, c.name .. ": no tracks")
local items, err = IsoBmff.items(r, movie)
t:check(items, c.name .. ": items: " .. tostring(err))
local prim = items.items[items.primary]
t:check(prim and prim.type == "av01", c.name .. ": primary item is av01")
local ispe, av1C
for _, p in prim.properties do
if p.type == "ispe" then
ispe = p
elseif p.type == "av1C" then
av1C = p
end
end
t:check(ispe and ispe.width == c.width and ispe.height == c.height, c.name .. ": ispe")
t:check(av1C and av1C.config and buffer.readu8(av1C.config, 0) == 0x81, c.name .. ": av1C marker / version")
local data = IsoBmff.itemData(r, items, items.primary :: number)
t:check(data and buffer.len(data) > 10, c.name .. ": item data")
-- an AV1 OBU stream: the first OBU is a sequence header (type 1) or a temporal delimiter (2)
local obu = data and bit32.band(bit32.rshift(buffer.readu8(data, 0), 3), 15)
t:check(obu == 1 or obu == 2, c.name .. ": AV1 OBU stream")
local auxl = nil
for _, id in items.order do
local it = items.items[id]
if it.refs.auxl then
auxl = it
end
end
t:eq(auxl ~= nil, c.alpha, c.name .. ": alpha auxiliary item")
end
do
local rand = T.rng(5)
local function sample(n: number): buffer
local b = buffer.create(n)
for i = 0, n - 1 do
buffer.writeu8(b, i, rand(256))
end
return b
end
local aud, vid = {}, {}
for i = 1, 90 do
aud[i] = sample(50 + rand(200))
end
for i = 1, 30 do
vid[i] = sample(100 + rand(900))
end
local cts = {}
local sync = {}
for i = 1, 30 do
cts[i] = (i % 3) * 512
sync[i] = i % 10 == 1
end
local cfg = buffer.fromstring("\x12\x10")
local file, err = IsoBmff.mux({
brand = "mp42",
compatible = { "mp42", "isom" },
tracks = {
{ kind = "video", entry = "avc1", timescale = 15360, samples = vid, durations = 512, cts = cts, sync = sync, width = 64, height = 48, config = buffer.fromstring("\1\100\0\10\255") },
{ kind = "audio", entry = "mp4a", timescale = 44100, samples = aud, durations = 1024, sampleRate = 44100, channels = 2, config = cfg, skip = 1024, playDuration = 44100 * 2 },
},
tags = { title = "Mux Test", artist = "Me", track = "2", tracktotal = "9", custom = "free form", bpm = "120" },
pictures = { { mime = "image/jpeg", data = buffer.fromstring("\255\216\255\217") } },
chunkDuration = 0.25,
})
t:check(file, "mux: " .. tostring(err))
local r = Bytes.reader(file :: buffer)
local dmx = IsoBmff.demux(r)
t:check(dmx and #dmx.tracks == 2, "mux: demux our file")
local v, a = dmx.tracks[1], dmx.tracks[2]
t:check(v.codec == "h264" and v.width == 64 and v.height == 48 and v.config and buffer.len(v.config) == 5, "mux: video track")
t:check(a.codec == "aac" and a.sampleRate == 44100 and a.channels == 2 and a.config and buffer.tostring(a.config) == "\x12\x10", "mux: audio track + esds")
t:check(a.skip == 1024 and a.playDuration == 88200, "mux: edit list")
local ok = true
for i = 1, 30 do
local s = IsoBmff.sample(r, v, i) :: buffer
ok = ok and buffer.tostring(s) == buffer.tostring(vid[i]) and v.dts[i] == (i - 1) * 512 and (v.cts :: any)[i] == cts[i] and ((v.sync :: any)[i] == true) == sync[i]
end
t:check(ok, "mux: video samples, times, cts, sync")
ok = true
for i = 1, 90 do
ok = ok and buffer.tostring(IsoBmff.sample(r, a, i) :: buffer) == buffer.tostring(aud[i]) and a.durations[i] == 1024
end
t:check(ok, "mux: audio samples")
-- interleaved: offsets alternate between the tracks
t:check(v.offsets[30] > a.offsets[1] and a.offsets[90] > v.offsets[1], "mux: interleaved chunks")
local tags, pics = IsoBmff.tags(r, dmx.movie)
t:check(tags.title == "Mux Test" and tags.artist == "Me" and tags.track == "2" and tags.tracktotal == "9" and tags.custom == "free form" and tags.bpm == "120", "mux: tags round trip")
t:check(#pics == 1 and buffer.len(pics[1].data) == 4, "mux: cover round trip")
t:note("mux: %d bytes, crc %08x", buffer.len(file :: buffer), Crc32.of(file :: buffer))
local f2 = IsoBmff.mux({ tracks = { { kind = "audio", entry = "alac", timescale = 8000, data = buffer.create(30), sizes = { 10, 10, 10 }, durations = { 4096, 4096, 100 }, config = buffer.create(24) } } })
local d2 = IsoBmff.demux(Bytes.reader(f2 :: buffer))
t:check(d2 and d2.tracks[1].codec == "alac" and d2.tracks[1].sampleCount == 3 and d2.tracks[1].durations[3] == 100 and buffer.len(d2.tracks[1].config) == 24, "mux: data + sizes, alac cookie")
for _, bad in { {}, { tracks = {} }, { tracks = { { kind = "audio", entry = "toolong", timescale = 1, samples = {}, durations = 1 } } }, { tracks = { { kind = "x", entry = "abcd", timescale = 1, samples = {}, durations = 1 } } }, { tracks = { { kind = "audio", entry = "abcd", timescale = 1 } } } } do
local f, e = IsoBmff.mux(bad :: any)
t:check(f == nil and e and e.code == "ARG", "mux: bad spec -> ARG")
end
end
do
local samples = {}
for _, c in Fx.cases do
table.insert(samples, string.sub(c.data, 1, 6000))
table.insert(samples, c.data)
end
for _, c in Fx.avif do
table.insert(samples, c.data)
end
t:fuzz("demux", function(b: buffer)
local r = Bytes.reader(b)
local dmx = IsoBmff.demux(r)
if dmx then
for p in dmx:packets() do
local _ = p
end
IsoBmff.tags(r, dmx.movie)
local items = IsoBmff.items(r, dmx.movie)
if items then
for _, id in items.order do
IsoBmff.itemData(r, items, id)
end
end
end
return dmx
end, samples, { mutations = 12 })
end
t:done()