-- video/ogv tests: Ogg Theora from ffmpeg (libtheora + libvorbis), bit-exact with its framecrc (Adler-32 of yuv420p). -- Fixtures: tools/fixtures/theora.py. local T = require("@tests/T") local C = require("@tests/video/_common") local F = require("@rosetta") local Ogv = require("@rosetta/video/ogv") local Theora = require("@rosetta/codecs/video/Theora") local AudioWrap = require("@rosetta/containers/AudioWrap") 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("ogv") local AV = require("@tests/gen/theora_av") :: any local Q0 = require("@tests/gen/theora_q0") :: any local DUPE = require("@tests/gen/theora_dupe") :: any local MKV = require("@tests/gen/theora_mkv") :: any local OPUS = require("@tests/gen/theora_opus") :: any local FLAC = require("@tests/gen/theora_flac") :: any local function file(fx: any): buffer return Base64.decode(fx.file) :: buffer end -- frames ffmpeg does not show (empty packets) must repeat the previous one local function frames(v: any, fx: any, label: string, fps: number) local want: { [number]: number } = {} for _, f in fx.frames do want[f[1]] = f[2] end local n, bad, shown, prev = 0, 0, 0, nil :: number? while true do local f, err = v:nextFrame() if not f then t:check(err == nil, label .. ": decode error " .. tostring(err)) break end n += 1 local c = Adler32.of(f.image.pixels, nil, nil, 0) local w = want[math.floor(f.pts * fps + 0.5)] if w then shown += 1 if c ~= w then bad += 1 end elseif c ~= prev then bad += 1 end prev = c end t:eq(bad, 0, label .. ": frames equal to ffmpeg's (" .. n .. " frames)") t:eq(shown, #fx.frames, label .. ": every frame ffmpeg shows") return n end t:eq(Ogv.name, "ogv", "name") t:eq(Ogv.category, "video", "category") t:check(table.find(Ogv.extensions, "ogv") ~= nil, "ogv extension") t:eq(Ogv.sniff(Bytes.reader(file(AV))), 1, "sniff: Theora + Vorbis") t:eq(Ogv.sniff(Bytes.reader(file(Q0))), 1, "sniff: Theora") t:eq(Ogv.sniff(Bytes.reader(buffer.fromstring("OggS\0\2" .. string.rep("\0", 40)))), 0, "sniff: an Ogg page without Theora") t:eq(Ogv.sniff(Bytes.reader(buffer.fromstring("RIFF"))), 0, "sniff: not Ogg") local v, err = F.video.open(file(AV)) t:check(v ~= nil, "open: " .. tostring(err)) if v then t:eq(v.format, "ogv", "format") t:eq(v.codec, "theora", "codec") t:eq(v.width * 1000 + v.height, 160120, "size") t:eq(v.frameRate, 25, "frame rate") local dmx = v.demuxer :: any t:eq(#dmx.tracks, 2, "two tracks") local vt, at = dmx:track("video"), dmx:track("audio") t:check(vt and vt.codec == "theora" and vt.timescale == 25, "video track") t:check(at and at.codec == "vorbis" and at.sampleRate == 44100 and at.channels == 2, "audio track") t:check(vt and vt.config and #(Theora.splitConfig(vt.config) or {}) == 3, "video config: three Xiph-laced headers") t:check(math.abs(v.duration - 1) < 0.05, "duration " .. v.duration) t:check(v.meta.tags and v.meta.tags.encoder ~= nil, "tags from the comment header") local first = dmx:get(vt.id, 1) t:check(first and first.keyframe and first.pts == 0, "first packet: a keyframe at 0") t:check(not dmx:get(vt.id, 2).keyframe, "second packet: an inter frame") frames(v, AV, "av", 25) local pcm = Base64.decode(AV.pcm) :: buffer C.audio(t, v, { pcm = buffer.tostring(pcm) }, "av", false, 20) v:seek(0.5) local f = v:nextFrame() t:check(f ~= nil and f.pts >= 0.5 - 1e-6 and f.pts < 0.6, "seek 0.5 -> " .. tostring(f and f.pts)) if f then local want = nil for _, w in AV.frames do if w[1] == math.floor(f.pts * 25 + 0.5) then want = w[2] end end t:eq(Adler32.of(f.image.pixels, nil, nil, 0), want, "the frame after seeking equals ffmpeg's") end v:reset() local out, rerr = F.save(v, nil, { format = "ogv" }) t:check(out ~= nil, "remux: " .. tostring(rerr)) if out then t:eq(Ogv.sniff(Bytes.reader(out)), 1, "remux: sniff") local v2 = F.video.open(out) :: any t:check(v2 ~= nil and #v2.demuxer.tracks == 2, "remux: two tracks") if v2 then frames(v2, AV, "remux", 25) C.audio(t, v2, { pcm = buffer.tostring(pcm) }, "remux", false, 20) end end v:reset() local mk, merr = F.save(v, nil, { format = "mkv" }) t:check(mk ~= nil, "to mkv: " .. tostring(merr)) if mk then local v3 = F.video.open(mk) :: any t:check(v3 ~= nil and v3.codec == "theora", "to mkv: Theora track") if v3 then frames(v3, AV, "to mkv", 25) end end end -- 3. Opus and FLAC audio tracks (FLAC: exact) for _, c in { { OPUS, "opus", 48000 }, { FLAC, "flac", 22050 } } do local fx, codec, rate = c[1], c[2], c[3] local vd, e = F.video.open(file(fx)) t:check(vd ~= nil, codec .. ": open: " .. tostring(e)) if vd then local at = (vd.demuxer :: any):track("audio") t:check(at and at.codec == codec and at.sampleRate == rate and at.channels == 1, codec .. ": audio track") frames(vd, fx, codec, 25) C.audio(t, vd, { pcm = buffer.tostring(Base64.decode(fx.pcm) :: buffer) }, codec, codec == "flac", 20) if codec == "opus" then vd:reset() local out = F.save(vd, nil, { format = "ogv" }) local v2 = out and F.video.open(out) :: any t:check(v2 ~= nil and v2.audio ~= nil, "opus: remux keeps the audio") if v2 then C.audio(t, v2, { pcm = buffer.tostring(Base64.decode(fx.pcm) :: buffer) }, "opus remux", false, 20) end end end end -- 4. an empty packet: a repeated frame with its own time do local vd = Ogv.open(Bytes.reader(file(DUPE))) :: any local n = 0 local prev, dupAt = nil, nil while true do local f = vd:nextFrame() if not f then break end n += 1 local c = Adler32.of(f.image.pixels, nil, nil, 0) if n == 6 then dupAt = f.pts t:eq(c, prev, "dupe: frame 6 repeats frame 5") end prev = c end t:eq(n, #DUPE.frames + 1, "dupe: one frame per packet") t:check(dupAt ~= nil and math.abs((dupAt :: number) - 0.2) < 1e-6, "dupe: its time slot (0.2 s)") t:check(vd.audio == nil and vd.audioError == nil, "no audio track, no audio error") end -- 5. Theora in Matroska -> ogv do local mv = F.video.open(file(MKV)) :: any local out, e = F.save(mv, nil, { format = "ogv" }) t:check(out ~= nil, "mkv -> ogv: " .. tostring(e)) if out then local v2 = Ogv.open(Bytes.reader(out)) :: any frames(v2, MKV, "mkv -> ogv", 25) end end -- 6. not Theora: plain Ogg Vorbis goes to the audio format, writing other codecs fails do local vd = Ogv.open(Bytes.reader(file(AV))) :: any local at = vd.demuxer:track("audio") local ogg = AudioWrap.wrap(vd.demuxer, at) :: buffer t:eq(Ogv.sniff(Bytes.reader(ogg)), 0, "sniff: Ogg Vorbis is not ogv") local a = F.load(ogg, { name = "x.ogg" }) t:check(a ~= nil and a.kind == "audio", "F.load of Ogg Vorbis named .ogg: audio") local v4, e4 = Ogv.open(Bytes.reader(ogg)) t:check(v4 ~= nil and v4.decodeError ~= nil, "an Ogg without video: opens, frames UNSUPPORTED (" .. tostring(e4) .. ")") local _, ee = Ogv.encode(Ogv.open(Bytes.reader(ogg)) :: any) t:check(ee ~= nil and ee.code == Err.UNSUPPORTED, "encode without Theora: UNSUPPORTED") local _, eg = Ogv.open(Bytes.reader(buffer.fromstring("OggS garbage"))) t:check(eg ~= nil, "garbage: an error") local cut = buffer.create(5000) buffer.copy(cut, 0, file(Q0), 0, 5000) local vc = Ogv.open(Bytes.reader(cut)) t:check(vc ~= nil, "a truncated file opens") end t:fuzz("open", C.fuzzOpen(Ogv), { buffer.tostring(file(Q0)) }, { mutations = 16, garbage = 50 }) t:done()