-- video/h264 tests: H.264 by ffmpeg / libx264 in raw Annex B, MP4, MKV, MPEG-TS and AVI, bit-exact against ffmpeg. -- Fixtures: tools/fixtures/h264.py (h264_containers). local T = require("@tests/T") local F = require("@rosetta") local Fmt = require("@rosetta/video/h264") local Bytes = require("@rosetta/core/Bytes") local Crc32 = require("@rosetta/core/Crc32") local Cases = require("@tests/gen/h264_containers") local t = T.suite("video/h264") t:eq(Fmt.name, "h264", "name") local function frames(v: any): ({ any }, any) local out = {} while true do local f, err = v:nextFrame() if not f then return out, err end table.insert(out, f) end end local byName = {} for _, c in Cases do byName[c.name] = c local v, err = F.video.open(c.data) t:check(v ~= nil, c.name .. ": F.video.open " .. tostring(err)) if v then t:eq(v.codec, "h264", c.name .. ": codec") local got, ferr = frames(v) t:check(ferr == nil, c.name .. ": no decode error " .. tostring(ferr)) t:eq(#got, #c.frames, c.name .. ": frame count") local good, mono = 0, true for i, f in got do if Crc32.of(f.image.pixels) == c.frames[i] then good += 1 end if i > 1 and f.pts <= got[i - 1].pts then mono = false end end t:eq(good, #c.frames, c.name .. ": frames bit-exact with ffmpeg") t:check(mono, c.name .. ": display-order timestamps increase") t:check(got[1] and got[1].image.meta.yuv and got[1].image.meta.yuv.range == "limited", c.name .. ": meta.yuv") local rgba = got[1] and v:toRGBA(got[1]) t:check(rgba and rgba.format == "rgba8" and rgba.width == c.width, c.name .. ": toRGBA") end end do local c = byName["cabac_b.h264"] local r = Bytes.reader(buffer.fromstring(c.data)) t:check(Fmt.sniff(r) >= 0.5, "sniff a raw stream") t:check(Fmt.sniff(Bytes.reader(buffer.fromstring(byName["cabac_b.mp4"].data))) == 0, "sniff rejects MP4") local dmx = Fmt.demux(r) t:check(dmx and dmx:count(1) == #c.frames, "one packet per access unit") local v = Fmt.open(Bytes.reader(buffer.fromstring(c.data))) local got = frames(v) t:check(#got == #c.frames and math.abs(got[2].pts - 1 / 25) < 1e-9, "raw: pts in output order at 25 fps") end for _, src in { "cabac_b.mp4", "baseline.mkv", "baseline.ts" } do local c = byName[src] local v = F.video.open(c.data) local out, err = F.save(v, nil, { format = "h264" }) t:check(out ~= nil, src .. " -> h264: " .. tostring(err)) if out then local v2 = F.video.open(out) t:check(v2 ~= nil and v2.format == "h264", src .. " -> h264: reads back as raw H.264") local got = frames(v2) local good = 0 for i, f in got do if Crc32.of(f.image.pixels) == c.frames[i] then good += 1 end end t:eq(good, #c.frames, src .. " -> h264: frames bit-exact") end end local samples = { byName["baseline.h264"].data, byName["cabac_b.h264"].data } t:fuzz("open + frames", function(b: buffer) local v = Fmt.open(Bytes.reader(b), { maxPixels = 2 ^ 18 }) if v then for _ = 1, 40 do if not v:nextFrame() then break end end end return v end, samples, { mutations = 8, keepHeader = 30 }) t:done()