Files
rosetta/tests/codecs/vp8video.luau
T
4DBug 51b08575c4 ws-cleanup: codecs/ grouped by domain; containers/Chunks, codecs/audio/Stream, codecs/image/Pixels, codecs/model/Mat4
C2, the hierarchy (tools/move.py, every reference rewritten mechanically):
- codecs/compress: Deflate Lzma Zstd Brotli Bzip2 Lzw; codecs/entropy: Huffman (from core) RangeCoder;
  codecs/image: Jpeg Dct Vp8 Vp8L Yuv Quantize Raster Blend Pixels; codecs/audio: Mdct Aac Alac Pcm Resample
  VorbisComment Tracker SoundFont Stream; codecs/video: H264 Mpeg1 Video; codecs/text: Json Xml Text Url;
  codecs/font: TrueType Cff; codecs/model: MeshFlatten Mat4. containers/ stays flat; core/ keeps Bytes Bits Err
  Crc32 Adler32 Base64 Md5 Input Sniff.
- LAYERS lint: codecs must sit in a domain folder; gen_registry lists codecs by domain (docs/FORMATS.md).
- Checked: build.py both targets, rojo build of build/roblox, the full suite on both runtimes.

C3, generic code out of format folders:
- containers/Chunks (new): the chunked-file walker (RIFF / RIFX / IFF / PNG / CAF layouts: endianness, size first,
  trailer, padding, lenient missing pad, open-ended ids, size hooks). containers/Riff walks with it (RF64 ds64
  through the resize hook); image/png, audio/aiff and audio/caf list their chunk handlers over it.
- codecs/audio/Stream (new): Stream.packets, the packet-decoder Decoder (read / seek with pre-roll / position,
  priming and end trimming) that aac, caf and m4a each carried (~70 lines each).
- codecs/image/Pixels: image/bmp/Pixels promoted (+ rgb555); image/ico's DIB decoder uses its row loops instead of
  its own (the AND mask / alpha rule stay ICO's).
- codecs/model/Mat4 (new): matrices / quaternions / TRS shared by FBX (Xform keeps only the FBX transform model),
  Collada (Math keeps only <lookat> / <skew>) and USD (its matrix helpers and general 4x4 inverse); Mat4.mul is
  Types.Scene.mul. The Collada normal matrix is now the inverse transpose (it was the inverse; same result for the
  fixtures' bind shapes).
- README rewritten (install for Rojo and LÖVE / LuaJIT, quick start per category, F.* and F.ops, host notes:
  FORMATS_NO_JIT_TUNE, paths, layout); docs/STYLE.md: the codec domains, "what belongs in a format folder" with the
  shared building blocks and the DUP rule, conventions (WAVE order, lower-case tags, loops, the api hooks
  decodeImage / encodeImage / openAudio / plugins / resolve, the codecs/video/Video registry), Err.from, the type
  baseline. New tests: tests/containers/chunks, tests/codecs/mat4. Type baseline 93.
2026-10-31 12:25:03 -05:00

164 lines
5.9 KiB
Luau

-- codecs/image/Vp8 video tests: whole IVF streams through one decoder object, every shown frame bit-exact against ffmpeg's
-- native vp8 decoder (yuv420p CRC32): the official VP8 test vectors (vp80-00-comprehensive-001..018 and others) and
-- libvpx clips at many settings (alt-ref / hidden frames, golden, token partitions, error resilience, profiles 1-3,
-- sharpness, segmentation, q extremes, short key frame intervals, motion off the frame, odd sizes). Also hidden
-- frames, reset() and inter frames without a key frame, and the fuzz no-throw pass over damaged streams.
-- Fixtures: tools/fixtures/vp8video.py.
local T = require("@tests/T")
local Vp8 = require("@formats/codecs/image/Vp8")
local Crc32 = require("@formats/core/Crc32")
local Err = require("@formats/core/Err")
local Cases = require("@tests/gen/vp8video_cases")
local t = T.suite("vp8video")
-- the frames of an IVF file: { offset, length }
local function ivfFrames(b: buffer): { { number } }
local out = {}
local p = buffer.readu16(b, 6) -- header length (32)
while p + 12 <= buffer.len(b) do
local n = buffer.readu32(b, p)
if p + 12 + n > buffer.len(b) then
break
end
table.insert(out, { p + 12, n })
p += 12 + n
end
return out
end
local total, hidden, vectors = 0, 0, 0
local samples: { buffer } = {}
for _, c in Cases do
local b = buffer.fromstring(c.ivf)
local fr = ivfFrames(b)
local dec = Vp8.newDecoder()
local shown, bad, firstBad = 0, 0, ""
local ok = true
for i, f in fr do
local img, err = dec:decodeFrame(b, f[1], f[2])
if not img then
ok = false
firstBad = "frame " .. i .. ": " .. tostring(err)
break
end
local src = img.meta.source
if src.show then
shown += 1
if buffer.len(img.pixels) ~= c.sizes[shown] or Crc32.of(img.pixels) ~= c.crcs[shown] then
bad += 1
if bad == 1 then
firstBad = string.format("frame %d (shown %d, %s)", i, shown, if src.keyFrame then "key" else "inter")
end
end
else
hidden += 1
end
end
t:check(ok and bad == 0 and shown == #c.crcs, string.format("%s: %d / %d frames bit-exact, %d shown of %d; first bad: %s", c.name, shown - bad, #c.crcs, shown, #fr, firstBad))
total += shown
if string.sub(c.name, 1, 5) == "vp80-" then
vectors += 1
end
if #samples < 6 and #c.ivf < 20000 then
table.insert(samples, b)
end
end
t:note("%d streams (%d official test vectors), %d shown frames bit-exact with ffmpeg, %d hidden frames", #Cases, vectors, total, hidden)
-- hidden frames, reset(), inter frames without a key frame
do
local c
for _, x in Cases do
if x.name == "altref_352x288" then
c = x
end
end
local b = buffer.fromstring(c.ivf)
local fr = ivfFrames(b)
local dec = Vp8.newDecoder()
local _, e0 = dec:decodeFrame(b, fr[2][1], fr[2][2])
t:check(e0 and e0.code == Err.CORRUPT, "inter frame first: " .. tostring(e0))
local key = dec:decodeFrame(b, fr[1][1], fr[1][2]) :: any
t:check(key and key.meta.source.keyFrame and key.meta.source.show and key.meta.source.format == "vp8", "key frame meta")
local sawHidden = false
for i = 2, #fr do
local img = dec:decodeFrame(b, fr[i][1], fr[i][2]) :: any
if img and not img.meta.source.show then
sawHidden = true
t:check(not img.meta.source.keyFrame and img.width == c.width, "hidden frame is an Image")
end
end
t:check(sawHidden, "the alt-ref stream has a hidden frame")
-- the returned planes are a copy: decoding more frames leaves an earlier image alone
local d2 = Vp8.newDecoder()
local first = d2:decodeFrame(b, fr[1][1], fr[1][2]) :: any
local crc = Crc32.of(first.pixels)
d2:decodeFrame(b, fr[2][1], fr[2][2])
d2:decodeFrame(b, fr[3][1], fr[3][2])
t:eq(Crc32.of(first.pixels), crc, "images are fresh buffers")
dec:reset()
local _, e1 = dec:decodeFrame(b, fr[3][1], fr[3][2])
t:check(e1 and e1.code == Err.CORRUPT, "inter frame after reset: " .. tostring(e1))
local again = dec:decodeFrame(b, fr[1][1], fr[1][2]) :: any
t:eq(again and Crc32.of(again.pixels), c.crcs[1], "key frame after reset")
local nxt = dec:decodeFrame(b, fr[2][1], fr[2][2]) :: any
t:check(nxt ~= nil, "inter frame after the key frame")
local _, e2 = Vp8.decodeFrame(b, fr[2][1], fr[2][2])
t:check(e2 and e2.code == Err.CORRUPT, "one-shot decodeFrame of an inter frame")
local _, e3 = dec:decodeFrame(b, fr[2][1], 2)
t:check(e3 and e3.code == Err.TRUNCATED, "truncated frame tag: " .. tostring(e3))
end
-- fuzz: whole damaged streams through one decoder (truncated / mutated frames after valid ones)
local function feed(b: buffer)
local dec = Vp8.newDecoder()
local any = false
local p, n = 32, buffer.len(b)
while p + 12 <= n do
local len = math.min(buffer.readu32(b, p), n - p - 12)
local img = dec:decodeFrame(b, p + 12, len, { maxPixels = 2 ^ 20 })
any = any or img ~= nil
p += 12 + len
end
return any, nil
end
-- damage aimed at inter frames: one frame truncated, or bytes changed in its headers / modes (first 48 bytes)
local damaged: { buffer } = {}
local rand = T.rng(8)
for _, b in samples do
local fr = ivfFrames(b)
for _ = 1, 12 do
local k = 2 + rand(#fr - 1)
local parts = {}
for i, f in fr do
local data = buffer.readstring(b, f[1], f[2])
if i == k then
if rand(2) == 0 then
data = string.sub(data, 1, rand(#data))
else
for _ = 1, 1 + rand(3) do
local at = 1 + rand(math.min(48, #data))
data = string.sub(data, 1, at - 1) .. string.char(rand(256)) .. string.sub(data, at + 1)
end
end
end
local hdr = buffer.create(12)
buffer.writeu32(hdr, 0, #data)
table.insert(parts, buffer.tostring(hdr) .. data)
end
table.insert(damaged, buffer.fromstring(buffer.readstring(b, 0, 32) .. table.concat(parts)))
end
end
local throws = 0
for _, b in damaged do
if not pcall(feed, b) then
throws += 1
end
end
t:eq(throws, 0, #damaged .. " streams with one damaged inter frame: no throw")
t:fuzz("stream", feed, samples, { mutations = 5, keepHeader = 32 })
t:fuzz("stream (more damage)", feed, samples, { mutations = 15, keepHeader = 32, garbage = 20 })
t:done()