merge ws/video2-theora (Theora codec, ogv)

This commit is contained in:
4DBug committed 2026-10-31 15:22:40 -05:00
commit 77479b6a2f
15 files changed
+3195 -2

No files matched your search

+285
View File
@@ -0,0 +1,285 @@
-- codecs/video/Theora tests. Against ffmpeg (tools/fixtures/theora.py, its vp3 decoder = libtheora's output): every
-- frame of every stream, bit-exact (Adler-32 of the native planes of the picture region, as ffmpeg's framecrc): 4:2:0 /
-- 4:2:2 / 4:4:4, qualities 0..10, a 300 kbit/s high-motion stream (4MV, golden frames, multiple qi), picture regions
-- smaller than the coded frame (even sizes from the encoder, odd sizes at offsets patched into the header), frames
-- the encoder dropped (empty packets: ffmpeg outputs nothing, we repeat the previous picture) and one spliced in;
-- image() as yuv420p (4:2:2 / 4:4:4 chroma averaged); Theora in Matroska through the codecs/video/Video registry.
-- Errors: missing / bad headers, maxPixels, header packets in the data, garbage packets; the fuzz no-throw passes
-- (headers, key and inter frames).
local T = require("@tests/T")
local Theora = require("@formats/codecs/video/Theora")
local Video = require("@formats/codecs/video/Video")
local Ogg = require("@formats/containers/Ogg")
local Mkv = require("@formats/video/mkv")
local Bytes = require("@formats/core/Bytes")
local Base64 = require("@formats/core/Base64")
local Adler32 = require("@formats/core/Adler32")
local Err = require("@formats/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"),
}
-- the Theora packets of an Ogg file: headers, data packets
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 } } = {}
-- 1. every fixture, every frame
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
-- 3. image(): yuv420p, the planes as they are (4:2:0) or averaged (4:2:2 / 4:4:4)
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
-- 4. 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
-- 5. API details and errors
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
-- 6. fuzz: headers, keyframes and inter frames (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()
+211
View File
@@ -0,0 +1,211 @@
-- video/ogv tests: Ogg Theora files from ffmpeg (libtheora + libvorbis; tools/fixtures/theora.py): sniffing (Theora
-- BOS pages, plain Ogg Vorbis left to the audio formats), tracks, granule times, every frame through F.video.open
-- bit-exact with ffmpeg's (Adler-32 of yuv420p, its framecrc), an empty packet (a repeated frame), the Vorbis track
-- through the api's audio hook against ffmpeg's decode, tags, seeking, remuxing (ogv -> ogv, mkv -> ogv, ogv -> mkv)
-- read back, errors and the fuzz no-throw pass.
local T = require("@tests/T")
local C = require("@tests/video/_common")
local F = require("@formats")
local Ogv = require("@formats/video/ogv")
local Theora = require("@formats/codecs/video/Theora")
local AudioWrap = require("@formats/containers/AudioWrap")
local Bytes = require("@formats/core/Bytes")
local Base64 = require("@formats/core/Base64")
local Adler32 = require("@formats/core/Adler32")
local Err = require("@formats/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
-- every frame of a VideoDecoder against ffmpeg's framecrc (yuv420p, keyed by frame number); 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
-- 1. contract and sniffing
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")
-- 2. Theora + Vorbis through the api
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)
-- seeking
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
-- remux ogv -> ogv
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
-- remux ogv -> mkv (V_THEORA)
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
-- 7. fuzz
t:fuzz("open", C.fuzzOpen(Ogv), { buffer.tostring(file(Q0)) }, { mutations = 16, garbage = 50 })
t:done()