-- video/y4m tests: YUV4MPEG2 from ffmpeg against its yuv420p decode (bit-exact for 4:2:0 8-bit, else PSNR). -- Fixtures: tools/fixtures/y4m.py. local T = require("@tests/T") local C = require("@tests/video/_common") local F = require("@rosetta") local Y4m = require("@rosetta/video/y4m") local Bytes = require("@rosetta/core/Bytes") local Crc32 = require("@rosetta/core/Crc32") local Err = require("@rosetta/core/Err") local Types = require("@rosetta/Types") local Fx = require("@tests/gen/y4m_cases") local t = T.suite("y4m") t:eq(Y4m.name, "y4m", "name") for _, c in Fx do local label = c.name t:check(Y4m.sniff(Bytes.reader(buffer.fromstring(c.data))) == 1, label .. ": sniff") local v, err = Y4m.open(Bytes.reader(buffer.fromstring(c.data))) t:check(v ~= nil, label .. ": open " .. tostring(err)) if v then t:check(v.width == c.width and v.height == c.height, label .. ": size") t:check(math.abs(v.frameRate - c.rate[1] / c.rate[2]) < 1e-9, label .. ": frame rate") local n, bad, worst = 0, 0, math.huge while true do local f = v:nextFrame() if not f then break end n += 1 if c.exact then if Crc32.of(f.image.pixels) ~= c.frames[n] then bad += 1 end elseif c.yuv[n] then worst = math.min(worst, C.psnr(f.image.pixels, buffer.fromstring(c.yuv[n]))) end end t:eq(n, #c.frames, label .. ": frame count") if c.exact then t:check(bad == 0, label .. ": frames bit-exact with ffmpeg") else t:check(worst > 30, string.format("%s: PSNR vs ffmpeg %.1f dB", label, worst)) end end end -- header as ffmpeg writes it do local c = Fx[1] local v = F.video.open(buffer.fromstring(c.data)) local out, err = F.save(v, nil, { format = "y4m" }) t:check(out ~= nil, "write: " .. tostring(err)) if out then local s = buffer.tostring(out) t:check(string.find(s, "YUV4MPEG2 W173 H95 F30000:1001 Ip A1:1 C420jpeg XYSCSS=420JPEG\n", 1, true) == 1, "write: header " .. string.sub(s, 1, 60)) local v2 = Y4m.open(Bytes.reader(out)) local n, same = 0, true while true do local f = v2:nextFrame() if not f then break end n += 1 same = same and Crc32.of(f.image.pixels) == c.frames[n] end t:check(n == #c.frames and same, "write: frames round trip") end -- an rgba frames-backed video (a 2-frame animation) to y4m local a = Types.Image.new(4, 2) buffer.fill(a.pixels, 0, 255) local b = Types.Image.new(4, 2) a.frames = { { image = a, delay = 0.5, x = 0, y = 0, dispose = "none", blend = "source" }, { image = b, delay = 0.5, x = 0, y = 0, dispose = "none", blend = "source" } } local y, yerr = F.save(a, nil, { format = "y4m" }) t:check(y ~= nil, "write from frames: " .. tostring(yerr)) if y then local v3 = Y4m.open(Bytes.reader(y)) local f1 = v3:nextFrame() local f2 = v3:nextFrame() t:check(f1 ~= nil and f2 ~= nil and v3:nextFrame() == nil, "write from frames: 2 frames") t:eq(v3.frameRate, 2, "write from frames: frame rate from the delays") t:eq(buffer.readu8(f1.image.pixels, 0), 235, "write from frames: white -> Y 235 (limited range)") t:eq(buffer.readu8(f2.image.pixels, 0), 16, "write from frames: black -> Y 16") end end local function code(s: string): string? local _, e = Y4m.open(Bytes.reader(buffer.fromstring(s))) return e and e.code end t:eq(code("YUV4MPEG2 W0 H0\n"), Err.CORRUPT, "zero size") t:eq(code("YUV4MPEG2 W16 H16 Cfoo\n"), Err.UNSUPPORTED, "unknown colour space") t:eq(code("YUV4MPEG2 W99999999 H99999 C420\n"), Err.CORRUPT, "huge size") t:eq(code("YUV4MPEG2 W16 H16"), Err.TRUNCATED, "no header end") local samples = {} for _, c in Fx do table.insert(samples, string.sub(c.data, 1, 30000)) end t:fuzz("open", C.fuzzOpen(Y4m), samples, { mutations = 6, garbage = 100 }) t:done()