-- video decoder control tests: the cooperative yield hook (H.264, VP8, MPEG-1, rgba / scale conversions), disposable -- packets and skipDisposable, frame reuse (opts.reuse, v:release, decoder:release); outputs against ffmpeg's CRCs. -- Fixtures: tools/fixtures/h264.py (h264_containers), tools/fixtures/videoctl.py, tools/fixtures/videobench.py. local T = require("@tests/T") local F = require("@rosetta") local Crc32 = require("@rosetta/core/Crc32") local Video = require("@rosetta/codecs/video/Video") local Disposable = require("@rosetta/codecs/video/Video/Disposable") local H264 = require("@rosetta/codecs/video/H264") local Vp8 = require("@rosetta/codecs/image/Vp8") local Yuv = require("@rosetta/codecs/image/Yuv") local H264Cases = require("@tests/gen/h264_containers") local Vp8Cases = require("@tests/gen/videoctl_vp8") local Bench = require("@tests/gen/videobench") local t = T.suite("video/control") local function crcs(v: any, rgba: boolean?): ({ number }, { number }, any) local out, idx = {}, {} while true do local f, err = v:nextFrame() if not f then return out, idx, err end table.insert(out, Crc32.of(f.image.pixels)) table.insert(idx, f.index) end end local function same(a: { number }, b: { number }): boolean if #a ~= #b then return false end for i = 1, #a do if a[i] ~= b[i] then return false end end return true end local function ivfFrames(b: buffer): { buffer } local out = {} local p = buffer.readu16(b, 6) while p + 12 <= buffer.len(b) do local n = buffer.readu32(b, p) if p + 12 + n > buffer.len(b) then break end local f = buffer.create(n) buffer.copy(f, 0, b, p + 12, n) table.insert(out, f) p += 12 + n end return out end -- yield: the hook suspends a coroutine at every call; frames stay identical local function stepped(data: any, opts: { [string]: any }): ({ number }, number, number) local calls = 0 local o = table.clone(opts) o.audio = false o.yield = function() calls += 1 coroutine.yield() end local v = assert(F.video.open(data, o)) local got: { number } = {} local co = coroutine.create(function() got = crcs(v) end) local steps = 0 while coroutine.status(co) ~= "dead" do local ok, err = coroutine.resume(co) assert(ok, err) steps += 1 end return got, calls, steps end local yieldInputs = { { "h264 mp4", H264Cases[2].data }, { "vp8 webm", Vp8Cases[1].webm }, } for _, c in Bench do if c.size == "320x240" and (c.name == "mpeg1.ts" or c.name == "vp8.webm") then table.insert(yieldInputs, { c.name, c.data }) end end for _, c in yieldInputs do for _, opts in { {}, { rgba = true }, { maxSide = 64 }, { maxSide = 64, rgba = true } } do local tag = string.format("%s rgba=%s maxSide=%s", c[1], tostring(opts.rgba), tostring(opts.maxSide)) local o = table.clone(opts) o.audio = false local want = crcs(assert(F.video.open(c[2], o))) local got, calls, steps = stepped(c[2], opts) t:check(#want > 0 and same(got, want), tag .. ": frames identical under the yield hook") t:check(calls > #want and steps == calls + 1, string.format("%s: hook called %d times for %d frames, %d steps", tag, calls, #want, steps)) end end do -- Yuv.toRgba: the hook every 16 rows, same pixels local img = F.Types.Image.new(40, 70, "yuv420p") for i = 0, buffer.len(img.pixels) - 1 do buffer.writeu8(img.pixels, i, (i * 7) % 256) end local calls = 0 local a = assert(Yuv.imageToRgba(img)) local out = F.Types.Image.new(40, 70, "rgba8") local b = assert(Yuv.imageToRgba(img, out, function() calls += 1 end)) t:check(b == out and buffer.tostring(a.pixels) == buffer.tostring(b.pixels), "imageToRgba into an out Image, with a hook") t:eq(calls, 4, "toRgba hook calls (rows 16, 32, 48, 64)") local s1 = Yuv.imageScale(img, 20, 30) calls = 0 local s2 = Yuv.imageScale(img, 20, 30, F.Types.Image.new(20, 30, "yuv420p"), function() calls += 1 end) t:check(buffer.tostring(s1.pixels) == buffer.tostring(s2.pixels) and calls > 0, "imageScale into an out Image, with a hook") end -- disposable packets: H.264 (nal_ref_idc 0) in every container, and skipDisposable keeps the rest bit-exact for _, c in H264Cases do local v = assert(F.video.open(c.data, { audio = false })) local d = v.demuxer local n, nd, agree = 0, 0, true for p in d:packets(v.track.id) do n += 1 if p.disposable then nd += 1 end if (p.disposable == true) ~= (Video.isDisposable(v.track, p)) then agree = false end end local want = if string.sub(c.name, 1, 7) == "cabac_b" then 9 else 0 t:eq(nd, want, c.name .. ": disposable packets") t:check(agree, c.name .. ": packet.disposable == Video.isDisposable") local v2 = assert(F.video.open(c.data, { audio = false, skipDisposable = true })) local got, idx, err = crcs(v2) local good = 0 for i, crc in got do if c.frames[idx[i]] == crc then good += 1 end end t:check(err == nil and #got == #c.frames - nd and good == #got, string.format("%s: skipDisposable %d frames shown of %d, %d bit-exact at their index", c.name, #got, #c.frames, good)) end do -- H.264 decoder API: skipped frames keep their place local c = H264Cases[1] assert(c.name == "cabac_b.h264") local all = assert(H264.decodeAll(c.data)) local some = assert(H264.decodeAll(c.data, { skipDisposable = true })) local skipped, good = 0, 0 for i, f in some do if f.skipped then skipped += 1 t:check(f.image == nil and f.pts == all[i].pts, "skipped frame " .. i .. ": no image, its pts") elseif Crc32.of(f.image.pixels) == c.frames[i] then good += 1 end end t:check(#some == #all and skipped == 9 and good == #all - 9, string.format("decodeAll skipDisposable: %d skipped, %d / %d bit-exact", skipped, good, #all - 9)) local au = H264.accessUnits(c.data) local hits = 0 for i = 1, #au, 3 do local b = buffer.create(au[i + 1]) buffer.copy(b, 0, buffer.fromstring(c.data), au[i], au[i + 1]) if Video.isDisposable("h264", b) then hits += 1 end end t:eq(hits, 9, "Annex B access units: disposable") end -- VP8: frames refreshing no reference (two temporal layers) for _, c in Vp8Cases do local frames = ivfFrames(buffer.fromstring(c.ivf)) local nd, ndrop = 0, 0 local dec, skipDec = Vp8.newDecoder(), Vp8.newDecoder() local exact, skipped, wrong = 0, 0, 0 for i, f in frames do local disp, drop = Disposable.vp8(f) nd += if disp then 1 else 0 ndrop += if drop then 1 else 0 local full = assert(dec:decodeFrame(f)) local img, err = skipDec:decodeFrame(f, nil, nil, { skipDisposable = true }) if img then exact += if Crc32.of(img.pixels) == Crc32.of(full.pixels) and Crc32.of(full.pixels) == c.crcs[i] then 1 else 0 elseif err == nil then skipped += 1 wrong += if disp then 0 else 1 end end t:check(nd >= 8 and ndrop == 0, string.format("%s: %d of %d frames disposable, %d droppable", c.name, nd, #frames, ndrop)) t:check(skipped == nd and wrong == 0 and exact == #frames - nd, string.format("%s: decodeFrame skipDisposable skipped %d, %d others bit-exact", c.name, skipped, exact)) local v = assert(F.video.open(c.webm, { audio = false })) local pd = 0 for p in v.demuxer:packets(v.track.id) do pd += if p.disposable then 1 else 0 end t:eq(pd, nd, c.name .. ": webm packet.disposable") local got, idx = crcs(assert(F.video.open(c.webm, { audio = false, skipDisposable = true }))) local good = 0 for i, crc in got do good += if c.crcs[idx[i]] == crc then 1 else 0 end t:check(#got == #frames - nd and good == #got, string.format("%s: webm skipDisposable %d shown, %d bit-exact at their index", c.name, #got, good)) end -- reuse: frames identical, Images recycled local reuseInputs = { { "h264 mp4", H264Cases[2].data }, { "h264 ts", H264Cases[4].data }, { "vp8 webm", Vp8Cases[1].webm }, } for _, c in Bench do if c.size == "320x240" and c.name ~= "raw.y4m" then table.insert(reuseInputs, { c.name, c.data }) end end for _, c in reuseInputs do for _, opts in { {}, { rgba = true }, { maxSide = 64 } } do local tag = string.format("%s rgba=%s maxSide=%s", c[1], tostring(opts.rgba), tostring(opts.maxSide)) local o = table.clone(opts) o.audio = false local want = crcs(assert(F.video.open(c[2], o))) o.reuse = true local v = assert(F.video.open(c[2], o)) local got, seen, again = {}, {}, 0 while true do local f = v:nextFrame() if not f then break end table.insert(got, Crc32.of(f.image.pixels)) if seen[f.image.pixels] then again += 1 end seen[f.image.pixels] = true end t:check(same(got, want), tag .. ": reuse frames identical") local recycles = c[1] ~= "mjpeg.avi" or opts.rgba or opts.maxSide t:check(not recycles or again > 0, string.format("%s: %d of %d frames in a recycled buffer", tag, again, #got)) end end do -- v:release and decoder:release local c = H264Cases[2] local v = assert(F.video.open(c.data, { audio = false })) local a = assert(v:nextFrame()) local ca = Crc32.of(a.image.pixels) v:release(a) local b = assert(v:nextFrame()) t:check(b.image == a.image and Crc32.of(b.image.pixels) == c.frames[2] and ca == c.frames[1], "v:release: the next frame reuses the Image") local dec = assert(H264.newDecoder()) t:check(not dec:release(F.Types.Image.new(176, 144, "yuv420p")), "H264 decoder:release refuses a foreign Image") local vd = Vp8.newDecoder() local f1 = ivfFrames(buffer.fromstring(Vp8Cases[1].ivf)) local i1 = assert(vd:decodeFrame(f1[1])) local k1 = Crc32.of(i1.pixels) t:check(vd:release(i1), "Vp8 decoder:release") local i2 = assert(vd:decodeFrame(f1[2])) t:check(i2 == i1 and k1 == Vp8Cases[1].crcs[1] and Crc32.of(i2.pixels) == Vp8Cases[1].crcs[2], "Vp8: the released Image holds the next frame") end local samples = {} for _, c in H264Cases do table.insert(samples, buffer.fromstring(string.sub(c.data, 1, 4000))) end for _, f in ivfFrames(buffer.fromstring(Vp8Cases[1].ivf)) do if #samples < 12 then table.insert(samples, f) end end t:fuzz("Disposable.check", function(b: buffer) Disposable.check("h264", b, nil) Disposable.check("h264", b, buffer.fromstring("\1\100\0\31\255\225\0")) return Disposable.check("vp8", b) end, samples) t:done()