-- codecs/video/Theora tests: every frame bit-exact against ffmpeg's vp3 decoder (Adler-32 of planes, as framecrc). -- Fixtures: tools/fixtures/theora.py. local T = require("@tests/T") local Theora = require("@rosetta/codecs/video/Theora") local Video = require("@rosetta/codecs/video/Video") local Ogg = require("@rosetta/containers/Ogg") local Mkv = require("@rosetta/video/mkv") local Bytes = require("@rosetta/core/Bytes") local Base64 = require("@rosetta/core/Base64") local Adler32 = require("@rosetta/core/Adler32") local Err = require("@rosetta/core/Err") local t = T.suite("theora") local CASES = { "q6", "q0", "q10", "motion", "odd", "static", "yuv422", "yuv444", "odd444", "crop", "crop422" } local FIXTURES: { [string]: any } = { q6 = require("@tests/gen/theora_q6"), q0 = require("@tests/gen/theora_q0"), q10 = require("@tests/gen/theora_q10"), motion = require("@tests/gen/theora_motion"), odd = require("@tests/gen/theora_odd"), static = require("@tests/gen/theora_static"), yuv422 = require("@tests/gen/theora_yuv422"), yuv444 = require("@tests/gen/theora_yuv444"), odd444 = require("@tests/gen/theora_odd444"), crop = require("@tests/gen/theora_crop"), crop422 = require("@tests/gen/theora_crop422"), } local function packets(file: buffer): ({ buffer }, { buffer }) local d = Ogg.open(file) local serial = d:find("theora") local hdrs, data = {}, {} for p in d:packets(serial) do if #hdrs < 3 then table.insert(hdrs, Ogg.copy(p)) else table.insert(data, Ogg.copy(p)) end end return hdrs, data end -- ffmpeg's framecrc: Adler-32 started at 0 over the packed planes local function crc(y: buffer, u: buffer, v: buffer): number local a = Adler32.of(y, nil, nil, 0) a = Adler32.of(u, nil, nil, a) return Adler32.of(v, nil, nil, a) end local samples: { buffer } = {} local headerSets: { { buffer } } = {} for _, name in CASES do local fx = FIXTURES[name] local hdrs, data = packets(Base64.decode(fx.file) :: buffer) table.insert(headerSets, hdrs) local dec, err = Theora.new(hdrs) t:check(dec ~= nil, name .. ": headers: " .. tostring(err)) if dec then t:eq(dec.width, fx.width, name .. ": width") t:eq(dec.height, fx.height, name .. ": height") t:eq(dec.pixelFormat, fx.pixfmt, name .. ": pixel format") local want: { [number]: number } = {} for _, f in fx.frames do want[f[1]] = f[2] end local bad, dupes, prev = 0, 0, nil :: number? for k, p in data do local kind, e = dec:decode(p) if not kind then bad += 1 t:check(false, name .. ": frame " .. (k - 1) .. ": " .. tostring(e)) continue end local y, u, v = dec:planes() local c = crc(y, u, v) local w = want[k - 1] if kind == "dup" then dupes += 1 t:check(w == nil, name .. ": ffmpeg shows no frame for empty packet " .. (k - 1)) if c ~= prev then bad += 1 end elseif w ~= c then bad += 1 end prev = c end t:eq(bad, 0, name .. ": frames different from ffmpeg's (of " .. #data .. ", " .. dupes .. " repeated)") t:eq(#data - dupes, #fx.frames, name .. ": as many shown frames as ffmpeg") table.insert(samples, data[1]) if data[2] then table.insert(samples, data[2]) end end end -- 2. an empty packet spliced in after the 5th frame repeats it; the rest follows do local fx = require("@tests/gen/theora_dupe") :: any local hdrs, data = packets(Base64.decode(fx.file) :: buffer) local dec = Theora.new(hdrs) :: Theora.TheoraDecoder local bad, prev = 0, nil :: number? for k, p in data do local kind = dec:decode(p) local y, u, v = dec:planes() local c = crc(y, u, v) if k == 6 then t:eq(kind, "dup", "dupe: the empty packet") t:eq(buffer.len(p), 0, "dupe: it is empty") t:eq(c, prev, "dupe: repeats the previous picture") elseif c ~= fx.frames[if k > 6 then k - 1 else k][2] then bad += 1 end prev = c end t:eq(bad, 0, "dupe: the other frames") end for _, name in { "q6", "crop", "yuv422", "crop422", "odd444" } do local fx = FIXTURES[name] local hdrs, data = packets(Base64.decode(fx.file) :: buffer) local dec = Theora.new(hdrs) :: Theora.TheoraDecoder dec:decode(data[1]) dec:decode(data[2]) local img = dec:image() local y, u, v, cw, ch = dec:planes() local w, h = dec.width, dec.height local ow, oh = (w + 1) // 2, (h + 1) // 2 t:eq(img.format, "yuv420p", name .. ": image format") t:eq(img.width * 1000 + img.height, w * 1000 + h, name .. ": image size") t:eq(buffer.len(img.pixels), w * h + 2 * ow * oh, name .. ": image bytes") t:eq(Adler32.of(img.pixels, 0, w * h), Adler32.of(y), name .. ": luma") local bad = 0 for r = 0, oh - 1 do for c = 0, ow - 1 do local want if fx.pixfmt == "yuv420p" then want = buffer.readu8(u, math.min(r, ch - 1) * cw + c) elseif fx.pixfmt == "yuv422p" then want = (buffer.readu8(u, math.min(2 * r, ch - 1) * cw + c) + buffer.readu8(u, math.min(2 * r + 1, ch - 1) * cw + c) + 1) // 2 else local r0, r1 = math.min(2 * r, ch - 1), math.min(2 * r + 1, ch - 1) local c0, c1 = math.min(2 * c, cw - 1), math.min(2 * c + 1, cw - 1) want = (buffer.readu8(u, r0 * cw + c0) + buffer.readu8(u, r0 * cw + c1) + buffer.readu8(u, r1 * cw + c0) + buffer.readu8(u, r1 * cw + c1) + 2) // 4 end if buffer.readu8(img.pixels, w * h + r * ow + c) ~= want then bad += 1 end end end t:eq(bad, 0, name .. ": U downsampled") local _ = v t:eq(img.meta.yuv and img.meta.yuv.matrix, "bt601", name .. ": colour matrix") end -- Theora in Matroska: the registry entry (V_THEORA, Xiph-laced CodecPrivate) through video/mkv do t:eq(Video.fromMatroska("V_THEORA"), "theora", "registry: V_THEORA") t:check(Video.canDecode("theora"), "registry: theora decodes") local fx = require("@tests/gen/theora_mkv") :: any local v, err = Mkv.open(Bytes.reader(Base64.decode(fx.file) :: buffer)) t:check(v ~= nil, "mkv: open: " .. tostring(err)) if v then t:eq(v.codec, "theora", "mkv: codec") local n, bad, shown = 0, 0, 0 local want: { [number]: number } = {} for _, f in fx.frames do want[f[1]] = f[2] end while true do local f, e = v:nextFrame() if not f then t:check(e == nil, "mkv: frames: " .. tostring(e)) break end local img = f.image local w = want[math.floor(f.pts * 25 + 0.5)] if w then shown += 1 if Adler32.of(img.pixels, nil, nil, 0) ~= w then bad += 1 end end n += 1 end t:eq(bad, 0, "mkv: frames as ffmpeg's") t:eq(shown, #fx.frames, "mkv: every frame ffmpeg shows") end end do local fx = FIXTURES.q6 local hdrs, data = packets(Base64.decode(fx.file) :: buffer) local dec = Theora.new(hdrs) :: Theora.TheoraDecoder t:eq(dec.frameRate, 25, "frame rate") t:check(dec.vendor ~= "" and dec.tags.encoder ~= nil, "comment header: vendor and ENCODER tag") t:eq(Theora.isHeader(hdrs[1]), 1, "isHeader: identification") t:eq(Theora.isHeader(hdrs[3]), 3, "isHeader: setup") t:eq(Theora.isHeader(data[1]), nil, "isHeader: data") local kind, e = dec:decode(hdrs[2]) t:check(kind == nil and e == nil, "a header packet in the data is skipped") t:eq(Theora.granuleFrame(dec.info, 0), -1, "granule 0 (3.2.1 numbering)") t:eq(Theora.granuleFrame(dec.info, 2 ^ dec.info.granuleShift * 12 + 3), 14, "granule: keyframe 12 + 3") -- the Xiph-laced config Matroska uses local lens = { buffer.len(hdrs[1]), buffer.len(hdrs[2]) } local w = Bytes.writer(64) w:u8(2) for _, l in lens do local x = l while x >= 255 do w:u8(255) x -= 255 end w:u8(x) end for _, h in hdrs do w:bytes(h) end local cfg = w:finish() local parts = Theora.splitConfig(cfg) t:check(parts ~= nil and #parts == 3 and buffer.len(parts[3]) == buffer.len(hdrs[3]), "splitConfig") t:check(Theora.new(cfg) ~= nil, "new(config)") local d2, e2 = Theora.new({ hdrs[1], hdrs[2] }) t:check(d2 == nil and e2 ~= nil and e2.code == Err.CORRUPT, "missing setup header: CORRUPT") local _, e3 = Theora.new({ hdrs[2], hdrs[1], hdrs[3] }) t:check(e3 ~= nil and e3.code == Err.CORRUPT, "headers out of order: CORRUPT") local cut = buffer.create(100) buffer.copy(cut, 0, hdrs[3], 0, 100) local _, e4 = Theora.new({ hdrs[1], hdrs[2], cut }) t:check(e4 ~= nil, "truncated setup header: " .. tostring(e4)) local _, e5 = Theora.new(hdrs, { maxPixels = 1000 }) t:check(e5 ~= nil and e5.code == Err.LIMIT, "maxPixels: LIMIT") local _, e6 = dec:decode(data[1], 5, buffer.len(data[1])) t:check(e6 ~= nil and e6.code == Err.ARG, "decode: range outside the buffer") -- an inter frame before any keyframe decodes over grey local fresh = Theora.new(hdrs) :: Theora.TheoraDecoder t:check(fresh:decode(data[2]) == "inter", "an inter frame first") local track: any = { id = 1, kind = "video", codec = "theora", timescale = 25, config = cfg } local vd = Theora.newVideoDecoder(track) t:check(vd ~= nil, "newVideoDecoder") if vd then vd:decode({ track = 1, pts = 0, keyframe = true, data = data[1] }) local img, pts, key = vd:receive() t:check(img ~= nil and pts == 0 and key == true, "newVideoDecoder: a keyframe picture") vd:reset() t:check(vd:receive() == nil, "newVideoDecoder: reset drops pending pictures") end local nv, nerr = Theora.newVideoDecoder({ id = 1, kind = "video", codec = "theora", timescale = 25 } :: any) t:check(nv == nil and nerr ~= nil, "newVideoDecoder without config") end -- fuzz: a decoder primed with a keyframe, fed mutated packets do local hdrs = headerSets[1] t:fuzz("headers", function(b: buffer) return Theora.new({ hdrs[1], hdrs[2], b }) end, { hdrs[3] }, { mutations = 24, garbage = 50 }) t:fuzz("identification", function(b: buffer) return Theora.new({ b, hdrs[2], hdrs[3] }) end, { hdrs[1] }, { mutations = 24, garbage = 20 }) for i, h in headerSets do local dec = Theora.new(h) :: Theora.TheoraDecoder local s = { samples[2 * i - 1], samples[2 * i] } dec:decode(s[1]) t:fuzz("frames " .. CASES[i], function(b: buffer) local kind = dec:decode(b) dec:planes() return kind end, s, { mutations = 12, garbage = 20, seed = i }) end end t:done()