merge master into ws/arch2 (ws/video2, ws/3d3, ws/game Archive.slice / collect)
This commit is contained in:
commit
79fd3c05dd
213 files changed
+47945
-1533
No files matched your search
@@ -0,0 +1,241 @@
|
||||
-- codecs/image/Bcn tests: random BC1..BC7 blocks (every BC7 and BC6H mode, the BC1 3-colour mode, odd sizes) against
|
||||
-- Pillow's and Godot's (bcdec) decodes; BC6H floats against Godot; the encoders (BC1 / BC3 / BC4 / BC5) within reach
|
||||
-- of Pillow's encoder PSNR, exact on flat blocks; argument errors; the fuzz no-throw pass. Fixtures: tools/fixtures/bcn.py.
|
||||
local T = require("@tests/T")
|
||||
local Bcn = require("@formats/codecs/image/Bcn")
|
||||
local Half = require("@formats/codecs/image/Half")
|
||||
local Fx = require("@tests/gen/bcn")
|
||||
|
||||
local t = T.suite("bcn")
|
||||
|
||||
-- the largest per-byte difference between two rgba8 buffers (channels: which of r, g, b, a count)
|
||||
local function maxDiff(a: buffer, b: buffer, n: number, channels: { number }): (number, number)
|
||||
local worst, at = 0, -1
|
||||
for i = 0, n - 1 do
|
||||
for _, c in channels do
|
||||
local d = math.abs(buffer.readu8(a, i * 4 + c) - buffer.readu8(b, i * 4 + c))
|
||||
if d > worst then
|
||||
worst, at = d, i
|
||||
end
|
||||
end
|
||||
end
|
||||
return worst, at
|
||||
end
|
||||
|
||||
local RGBA, RGB, RG, R = { 0, 1, 2, 3 }, { 0, 1, 2 }, { 0, 1 }, { 0 }
|
||||
-- which channels each reference defines, and the allowed difference to it
|
||||
local PILLOW: { [string]: { any } } = {
|
||||
bc1 = { RGBA, 0 },
|
||||
bc2 = { RGBA, 0 },
|
||||
bc3 = { RGBA, 0 },
|
||||
bc4 = { RGB, 0 },
|
||||
bc5 = { RG, 0 },
|
||||
bc5s = { RG, 1 }, -- Pillow interpolates the biased unsigned values with truncation
|
||||
bc7 = { RGBA, 0 },
|
||||
}
|
||||
|
||||
local stats = { pillow = 0, godot = 0 }
|
||||
for _, c in Fx.cases do
|
||||
local src = buffer.fromstring(c.data)
|
||||
local px, err = Bcn.decode(c.kind, src, 0, c.w, c.h)
|
||||
t:check(px, c.name .. ": " .. tostring(err))
|
||||
local n = c.w * c.h
|
||||
if Bcn.FLOAT[c.kind] then
|
||||
t:eq(buffer.len(px), n * 16, c.name .. ": rgbaf32 size")
|
||||
-- Godot (bcdec) floats: exact half values
|
||||
local g = buffer.fromstring(c.godot)
|
||||
local worst = 0
|
||||
for i = 0, n - 1 do
|
||||
for ch = 0, 2 do
|
||||
local a, b = buffer.readf32(px, i * 16 + ch * 4), buffer.readf32(g, i * 16 + ch * 4)
|
||||
local d = math.abs(a - b) / math.max(math.abs(b), 1e-3)
|
||||
worst = math.max(worst, d)
|
||||
end
|
||||
end
|
||||
t:check(worst < 1e-3, string.format("%s: relative error %g against Godot", c.name, worst))
|
||||
stats.godot += 1
|
||||
-- Pillow: 8-bit truncation of the clamped float (and no rounding term in its interpolation): within 1. Its
|
||||
-- signed BC6H differs from bcdec and the spec (negative endpoints), so only BC6H UF16 is compared.
|
||||
local p = buffer.fromstring(c.pillow)
|
||||
local w2 = 0
|
||||
for i = 0, n - 1 do
|
||||
for ch = 0, 2 do
|
||||
local v = math.clamp(buffer.readf32(px, i * 16 + ch * 4), 0, 1)
|
||||
w2 = math.max(w2, math.abs(math.floor(v * 255) - buffer.readu8(p, i * 4 + ch)))
|
||||
end
|
||||
end
|
||||
if c.kind == "bc6h" then
|
||||
t:check(w2 <= 1, string.format("%s: %d off Pillow", c.name, w2))
|
||||
stats.pillow += 1
|
||||
end
|
||||
else
|
||||
t:eq(buffer.len(px), n * 4, c.name .. ": rgba8 size")
|
||||
local ref = PILLOW[c.kind]
|
||||
local d, at = maxDiff(px, buffer.fromstring(c.pillow), n, ref[1])
|
||||
t:check(d <= ref[2], string.format("%s: %d off Pillow at pixel %d", c.name, d, at))
|
||||
stats.pillow += 1
|
||||
if c.godot then
|
||||
local chans = if c.kind == "bc4" then R elseif c.kind == "bc5" then RG else RGBA
|
||||
local dg, atg = maxDiff(px, buffer.fromstring(c.godot), n, chans)
|
||||
-- bcdec rounds some interpolated values where Pillow truncates: within 1
|
||||
t:check(dg <= 1, string.format("%s: %d off Godot at pixel %d", c.name, dg, atg))
|
||||
stats.godot += 1
|
||||
end
|
||||
end
|
||||
-- into a bigger buffer at an offset, from an offset
|
||||
local big = buffer.create(buffer.len(src) + 5)
|
||||
buffer.copy(big, 5, src)
|
||||
local bpp = if Bcn.FLOAT[c.kind] then 16 else 4
|
||||
local out = buffer.create(n * bpp + 8)
|
||||
local px2 = Bcn.decode(c.kind, big, 5, c.w, c.h, out, 8)
|
||||
t:check(px2 == out and buffer.readstring(out, 8, n * bpp) == buffer.readstring(px, 0, n * bpp), c.name .. ": offsets")
|
||||
end
|
||||
t:note("%d block images decoded: %d Pillow, %d Godot comparisons", #Fx.cases, stats.pillow, stats.godot)
|
||||
|
||||
-- BC4 SNORM = the red channel of BC5 SNORM
|
||||
do
|
||||
for _, c in Fx.cases do
|
||||
if c.kind == "bc5s" then
|
||||
local src = buffer.fromstring(c.data)
|
||||
local nb = buffer.len(src) // 16
|
||||
local b4 = buffer.create(nb * 8)
|
||||
for i = 0, nb - 1 do
|
||||
buffer.copy(b4, i * 8, src, i * 16, 8)
|
||||
end
|
||||
local p4 = Bcn.decode("bc4s", b4, 0, c.w, c.h) :: buffer
|
||||
local p5 = Bcn.decode("bc5s", src, 0, c.w, c.h) :: buffer
|
||||
local ok = true
|
||||
for i = 0, c.w * c.h - 1 do
|
||||
ok = ok and buffer.readu8(p4, i * 4) == buffer.readu8(p5, i * 4) and buffer.readu8(p4, i * 4 + 1) == buffer.readu8(p5, i * 4)
|
||||
end
|
||||
t:check(ok, c.name .. ": bc4s = bc5s red")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- encoders
|
||||
local function psnr(a: buffer, b: buffer, n: number, channels: { number }): number
|
||||
local se = 0
|
||||
for i = 0, n - 1 do
|
||||
for _, c in channels do
|
||||
local d = buffer.readu8(a, i * 4 + c) - buffer.readu8(b, i * 4 + c)
|
||||
se += d * d
|
||||
end
|
||||
end
|
||||
return if se == 0 then 99 else 10 * math.log10(255 * 255 * n * #channels / se)
|
||||
end
|
||||
|
||||
for _, e in Fx.enc do
|
||||
local px = buffer.fromstring(e.rgba)
|
||||
local n = e.w * e.h
|
||||
for _, k in { { "bc1", RGB }, { "bc3", RGBA }, { "bc5", RG }, { "bc4", R } } do
|
||||
local kind, ch = k[1], k[2]
|
||||
local blocks, err = Bcn.encode(kind, px, e.w, e.h)
|
||||
t:check(blocks, kind .. " encode: " .. tostring(err))
|
||||
t:eq(buffer.len(blocks), Bcn.size(kind, e.w, e.h), e.name .. " " .. kind .. ": size")
|
||||
local back = Bcn.decode(kind, blocks, 0, e.w, e.h) :: buffer
|
||||
local p = psnr(px, back, n, ch)
|
||||
local base = e.psnr[kind]
|
||||
if base then
|
||||
t:check(p >= base - 0.5, string.format("%s %s: PSNR %.2f dB, Pillow %.2f dB", e.name, kind, p, base))
|
||||
t:note("%s %s: %.2f dB (Pillow %.2f dB)", e.name, kind, p, base)
|
||||
else
|
||||
t:check(p >= 35, string.format("%s %s: PSNR %.2f dB", e.name, kind, p))
|
||||
end
|
||||
if kind == "bc1" then
|
||||
-- the transparent pixels survive (3-colour mode)
|
||||
local bad = 0
|
||||
for i = 0, n - 1 do
|
||||
local a = buffer.readu8(px, i * 4 + 3)
|
||||
if (a >= 128) ~= (buffer.readu8(back, i * 4 + 3) == 255) then
|
||||
bad += 1
|
||||
end
|
||||
end
|
||||
t:eq(bad, 0, e.name .. " bc1: alpha mask")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- flat colours are exact through the single-colour tables (BC1 4-colour and 3-colour, BC4)
|
||||
do
|
||||
local rand = T.rng(3)
|
||||
local worst = 0
|
||||
for _ = 1, 200 do
|
||||
local r, g, b = rand(256), rand(256), rand(256)
|
||||
local px = buffer.create(64)
|
||||
for i = 0, 15 do
|
||||
buffer.writeu32(px, i * 4, r + g * 256 + b * 65536 + (if i == 5 then 0 else 255) * 16777216)
|
||||
end
|
||||
for _, opaque in { true, false } do
|
||||
if opaque then
|
||||
buffer.writeu8(px, 23, 255)
|
||||
else
|
||||
buffer.writeu8(px, 23, 0)
|
||||
end
|
||||
local back = Bcn.decode("bc1", Bcn.encode("bc1", px, 4, 4) :: buffer, 0, 4, 4) :: buffer
|
||||
for i = 0, 15 do
|
||||
if opaque or i ~= 5 then
|
||||
for c = 0, 2 do
|
||||
worst = math.max(worst, math.abs(buffer.readu8(back, i * 4 + c) - buffer.readu8(px, i * 4 + c)))
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
-- 5 / 6-bit endpoints reach every 8-bit value within 1 (4-colour) / 2 (3-colour midpoints)
|
||||
t:check(worst <= 2, "flat bc1 colours within " .. worst)
|
||||
local px = buffer.create(64)
|
||||
for v = 0, 255, 5 do
|
||||
for i = 0, 15 do
|
||||
buffer.writeu32(px, i * 4, v + 0xFF000000)
|
||||
end
|
||||
local back = Bcn.decode("bc4", Bcn.encode("bc4", px, 4, 4) :: buffer, 0, 4, 4) :: buffer
|
||||
t:eq(buffer.readu8(back, 0), v, "flat bc4 " .. v)
|
||||
end
|
||||
end
|
||||
|
||||
-- Half
|
||||
do
|
||||
t:eq(Half.fromFloat(1), 0x3C00, "half 1")
|
||||
t:eq(Half.fromFloat(-2), 0xC000, "half -2")
|
||||
t:eq(Half.fromFloat(65504), 0x7BFF, "half max")
|
||||
t:eq(Half.fromFloat(1e6), 0x7C00, "half overflow")
|
||||
t:eq(Half.fromFloat(2 ^ -24), 1, "half smallest subnormal")
|
||||
t:eq(Half.fromFloat(0 / 0), 0x7E00, "half NaN")
|
||||
local ok = true
|
||||
for h = 0, 65535 do
|
||||
local f = Half.TABLE[h]
|
||||
if f == f and Half.fromFloat(f) ~= h and not (h == 0x8000) then
|
||||
ok = false
|
||||
end
|
||||
end
|
||||
t:check(ok, "half round trip of every pattern")
|
||||
end
|
||||
|
||||
-- errors
|
||||
do
|
||||
local _, e1 = Bcn.decode("bc9", buffer.create(8), 0, 4, 4)
|
||||
t:check(e1 and string.find(e1, "unsupported") ~= nil, "unknown kind")
|
||||
local _, e2 = Bcn.decode("bc1", buffer.create(7), 0, 4, 4)
|
||||
t:check(e2 and string.find(e2, "truncated") ~= nil, "short data")
|
||||
local _, e3 = Bcn.encode("bc7", buffer.create(64), 4, 4)
|
||||
t:check(e3 ~= nil, "no bc7 encoder")
|
||||
local _, e4 = Bcn.encode("bc1", buffer.create(10), 4, 4)
|
||||
t:check(e4 ~= nil, "short pixels")
|
||||
end
|
||||
|
||||
-- fuzz: random block data of every kind and size
|
||||
local samples = {}
|
||||
for _, c in Fx.cases do
|
||||
table.insert(samples, buffer.fromstring(c.data))
|
||||
end
|
||||
for _, kind in { "bc1", "bc2", "bc3", "bc4", "bc4s", "bc5", "bc5s", "bc6h", "bc6hs", "bc7" } do
|
||||
t:fuzz("decode " .. kind, function(b: buffer)
|
||||
local n = buffer.len(b) // Bcn.BLOCK[kind]
|
||||
if n < 1 then
|
||||
return Bcn.decode(kind, b, 0, 4, 4)
|
||||
end
|
||||
return Bcn.decode(kind, b, 0, 4 * n - 1, 3)
|
||||
end, samples, { mutations = 2 })
|
||||
end
|
||||
t:done()
|
||||
@@ -0,0 +1,81 @@
|
||||
-- codecs/model/Draco tests: Google's Draco test data (bitstreams 0.9 - 2.2) and files encoded by draco_encoder 1.5.7
|
||||
-- at every compression level, quantization and as point clouds, decoded and compared byte for byte (points, faces,
|
||||
-- every attribute's values per point, as CRCs) with libdraco's own decoder (tools/fixtures/_draco_dump.cc), kd-tree
|
||||
-- point clouds included; argument errors; the fuzz no-throw pass (which also fails on any internal error the
|
||||
-- decoder's safety net caught). Fixtures: tools/fixtures/draco.py.
|
||||
local T = require("@tests/T")
|
||||
local Draco = require("@formats/codecs/model/Draco")
|
||||
local Err = require("@formats/core/Err")
|
||||
local Crc32 = require("@formats/core/Crc32")
|
||||
local Cases = require("@tests/gen/draco_cases")
|
||||
|
||||
local t = T.suite("draco")
|
||||
|
||||
local counts = { exact = 0, rejected = 0, kd = 0 }
|
||||
local samples = {}
|
||||
for _, c in Cases do
|
||||
local g, err = Draco.decode(c.data)
|
||||
if c.error then
|
||||
t:check(g == nil and err ~= nil, c.name .. ": libdraco rejects it, so do we")
|
||||
counts.rejected += 1
|
||||
else
|
||||
t:check(g, c.name .. ": decode: " .. tostring(err))
|
||||
if g then
|
||||
t:eq(g.numPoints, c.points, c.name .. ": points")
|
||||
if c.faces then
|
||||
t:eq(g.numFaces, c.faces, c.name .. ": faces")
|
||||
t:eq(Crc32.of(g.indices :: buffer), c.indices, c.name .. ": indices")
|
||||
end
|
||||
t:eq(#g.attributes, #c.attrs, c.name .. ": attribute count")
|
||||
for i, want in c.attrs do
|
||||
local a = g.attributes[i]
|
||||
if a then
|
||||
t:eq(a.typeId, want.type, c.name .. ": attribute " .. i .. " type")
|
||||
t:eq(a.type, Draco.DATA_TYPES[want.dataType], c.name .. ": attribute " .. i .. " data type")
|
||||
t:eq(a.components, want.components, c.name .. ": attribute " .. i .. " components")
|
||||
t:eq(a.normalized, want.normalized, c.name .. ": attribute " .. i .. " normalized")
|
||||
t:eq(a.uniqueId, want.uniqueId, c.name .. ": attribute " .. i .. " unique id")
|
||||
t:eq(Crc32.of(a.data), want.crc, c.name .. ": attribute " .. i .. " (" .. a.name .. ") values")
|
||||
end
|
||||
end
|
||||
counts.exact += 1
|
||||
if g.method == "kdtree" then
|
||||
counts.kd += 1
|
||||
end
|
||||
if #c.data < 4000 then
|
||||
table.insert(samples, c.data)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
t:note("%d files decoded exactly like libdraco (%d kd-tree point clouds), %d rejected by both", counts.exact, counts.kd, counts.rejected)
|
||||
|
||||
-- the attribute lookup and the header
|
||||
local first = Cases[1]
|
||||
local info = Draco.info(first.data)
|
||||
t:check(info and (info.kind == "mesh" or info.kind == "points"), "info")
|
||||
t:check(Draco.sniff(buffer.fromstring(first.data)), "sniff")
|
||||
t:check(not Draco.sniff(buffer.fromstring("DRAC")), "sniff short")
|
||||
|
||||
-- errors
|
||||
local _, e1 = Draco.decode(buffer.fromstring("NOTDRACO......"))
|
||||
t:check(e1 and e1.code == Err.CORRUPT, "not Draco data: " .. tostring(e1))
|
||||
local _, e2 = Draco.decode(buffer.fromstring("DRACO\9\0\1\1\0\0"))
|
||||
t:check(e2 and e2.code == Err.UNSUPPORTED, "future version: " .. tostring(e2))
|
||||
local _, e3 = Draco.decode(42 :: any)
|
||||
t:check(e3 and e3.code == Err.ARG, "bad argument")
|
||||
|
||||
-- fuzz: never throws, and the internal safety net never fires
|
||||
local internal = 0
|
||||
t:fuzz("decode", function(b: buffer)
|
||||
local g, err = Draco.decode(b)
|
||||
if not g and err and string.find(err.message, "internal error", 1, true) then
|
||||
internal += 1
|
||||
if internal <= 5 then
|
||||
print(" internal error: " .. err.message)
|
||||
end
|
||||
end
|
||||
return g
|
||||
end, samples, { mutations = 12, keepHeader = 11 })
|
||||
t:eq(internal, 0, "fuzz: internal errors")
|
||||
t:done()
|
||||
@@ -0,0 +1,129 @@
|
||||
-- codecs/image/Etc tests: random ETC1 / ETC2 (every mode: individual, differential, T, H, planar; RGB A1 opaque and
|
||||
-- transparent; RGBA with EAC alpha) and EAC R11 / RG11 (unsigned, signed, multiplier 0) blocks against Godot 4's
|
||||
-- decode (etcpak); odd sizes; the 16-bit EAC output; errors; the fuzz no-throw pass. Fixtures: tools/fixtures/etc.py.
|
||||
local T = require("@tests/T")
|
||||
local Etc = require("@formats/codecs/image/Etc")
|
||||
local Fx = require("@tests/gen/etc")
|
||||
|
||||
local t = T.suite("etc")
|
||||
|
||||
local function maxDiff(a: buffer, b: buffer, n: number, channels: { number }): (number, number)
|
||||
local worst, at = 0, -1
|
||||
for i = 0, n - 1 do
|
||||
for _, c in channels do
|
||||
local d = math.abs(buffer.readu8(a, i * 4 + c) - buffer.readu8(b, i * 4 + c))
|
||||
if d > worst then
|
||||
worst, at = d, i
|
||||
end
|
||||
end
|
||||
end
|
||||
return worst, at
|
||||
end
|
||||
|
||||
local worstEac = 0
|
||||
for _, c in Fx do
|
||||
local src = buffer.fromstring(c.data)
|
||||
local px, err = Etc.decode(c.kind, src, 0, c.w, c.h)
|
||||
t:check(px, c.name .. ": " .. tostring(err))
|
||||
local n = c.w * c.h
|
||||
t:eq(buffer.len(px), n * 4, c.name .. ": size")
|
||||
local eacKind = string.sub(c.kind, 1, 3) == "eac"
|
||||
if c.spec then
|
||||
-- signed EAC against the spec rule (values -1023..1023), biased into 8 bits
|
||||
local two = c.kind == "eac_rg11s"
|
||||
local bad = 0
|
||||
for i = 0, n - 1 do
|
||||
for ch = 0, if two then 1 else 0 do
|
||||
local want = math.floor(c.spec[i * 2 + ch + 1] * 127 / 1023 + 0.5) + 128
|
||||
if buffer.readu8(px, i * 4 + ch) ~= want then
|
||||
bad += 1
|
||||
end
|
||||
end
|
||||
end
|
||||
t:eq(bad, 0, c.name .. ": against the spec values")
|
||||
elseif c.kind == "etc2_rgba1" then
|
||||
-- Godot decoded the blocks as ETC2 RGB: opaque blocks match it; transparent ones have index 2 transparent
|
||||
-- black and indexes 1 / 3 unchanged (index 0 drops its modifier in the differential mode); planar is opaque
|
||||
local g = buffer.fromstring(c.godot)
|
||||
local bw = (c.w + 3) // 4
|
||||
local bad = 0
|
||||
for y = 0, c.h - 1 do
|
||||
for x = 0, c.w - 1 do
|
||||
local bo = ((y // 4) * bw + x // 4) * 8
|
||||
local hi = bit32.byteswap(buffer.readu32(src, bo))
|
||||
local lo = bit32.byteswap(buffer.readu32(src, bo + 4))
|
||||
local i = (x % 4) * 4 + y % 4
|
||||
local idx = bit32.band(bit32.rshift(lo, i + 15), 2) + bit32.band(bit32.rshift(lo, i), 1)
|
||||
local opaque = bit32.band(hi, 2) ~= 0
|
||||
local function ch(v: number, at: number): number
|
||||
v = bit32.band(bit32.rshift(hi, at), 31) + (function(d: number): number
|
||||
return if d >= 4 then d - 8 else d
|
||||
end)(bit32.band(bit32.rshift(hi, at - 3), 7))
|
||||
return v
|
||||
end
|
||||
local r2, g2, b2 = ch(0, 27), ch(0, 19), ch(0, 11)
|
||||
local planar = r2 >= 0 and r2 <= 31 and g2 >= 0 and g2 <= 31 and (b2 < 0 or b2 > 31)
|
||||
local diff = r2 >= 0 and r2 <= 31 and g2 >= 0 and g2 <= 31 and b2 >= 0 and b2 <= 31
|
||||
local o = (y * c.w + x) * 4
|
||||
local mine, ref = buffer.readu32(px, o), buffer.readu32(g, o)
|
||||
if opaque or planar then
|
||||
bad += if mine == ref then 0 else 1
|
||||
elseif idx == 2 then
|
||||
bad += if mine == 0 then 0 else 1
|
||||
elseif idx ~= 0 or not diff then
|
||||
bad += if mine == ref then 0 else 1
|
||||
end
|
||||
end
|
||||
end
|
||||
t:eq(bad, 0, c.name .. ": punch-through rules over Godot's ETC2 RGB decode")
|
||||
else
|
||||
local g = buffer.fromstring(c.godot)
|
||||
local chans = if c.kind == "eac_r11" then { 0 } elseif eacKind then { 0, 1 } else { 0, 1, 2, 3 }
|
||||
local d, at = maxDiff(px, g, n, chans)
|
||||
if eacKind then
|
||||
-- Godot keeps EAC as 16-bit / half floats and converts to 8 bits with its own rounding
|
||||
worstEac = math.max(worstEac, d)
|
||||
t:check(d <= 1, string.format("%s: %d off Godot at pixel %d", c.name, d, at))
|
||||
else
|
||||
t:check(d == 0, string.format("%s: %d off Godot at pixel %d", c.name, d, at))
|
||||
end
|
||||
end
|
||||
-- 16-bit EAC agrees with the 8-bit output
|
||||
if eacKind then
|
||||
local p16 = Etc.decode(c.kind, src, 0, c.w, c.h, nil, 0, true) :: buffer
|
||||
t:eq(buffer.len(p16), n * 8, c.name .. ": rgba16 size")
|
||||
local ok = true
|
||||
for i = 0, n - 1 do
|
||||
local v16 = buffer.readu16(p16, i * 8)
|
||||
ok = ok and math.abs(math.floor(v16 / 257 + 0.5) - buffer.readu8(px, i * 4)) <= 1
|
||||
end
|
||||
t:check(ok, c.name .. ": rgba16 ~ rgba8")
|
||||
end
|
||||
-- from / into offsets
|
||||
local big = buffer.create(buffer.len(src) + 3)
|
||||
buffer.copy(big, 3, src)
|
||||
local out = buffer.create(n * 4 + 4)
|
||||
Etc.decode(c.kind, big, 3, c.w, c.h, out, 4)
|
||||
t:check(buffer.readstring(out, 4, n * 4) == buffer.tostring(px), c.name .. ": offsets")
|
||||
end
|
||||
t:note("%d block images match Godot (EAC within %d)", #Fx, worstEac)
|
||||
|
||||
do
|
||||
local _, e1 = Etc.decode("etc3", buffer.create(8), 0, 4, 4)
|
||||
t:check(e1 and string.find(e1, "unsupported") ~= nil, "unknown kind")
|
||||
local _, e2 = Etc.decode("etc2_rgba", buffer.create(8), 0, 4, 4)
|
||||
t:check(e2 and string.find(e2, "truncated") ~= nil, "short data")
|
||||
t:eq(Etc.size("etc2_rgba", 5, 5), 64, "size")
|
||||
end
|
||||
|
||||
local samples = {}
|
||||
for _, c in Fx do
|
||||
table.insert(samples, buffer.fromstring(c.data))
|
||||
end
|
||||
for kind in Etc.BLOCK do
|
||||
t:fuzz("decode " .. kind, function(b: buffer)
|
||||
local n = buffer.len(b) // Etc.BLOCK[kind]
|
||||
return Etc.decode(kind, b, 0, math.max(1, 4 * n - 2), 4)
|
||||
end, samples, { mutations = 2 })
|
||||
end
|
||||
t:done()
|
||||
@@ -0,0 +1,113 @@
|
||||
-- codecs/video/Legacy tests: Cinepak (24-bit, grey, 8-bit palettised, several strips, a 92x68 size), Microsoft Video 1
|
||||
-- (16-bit, 8-bit with an AVI palette), QuickTime RLE (depths 1 / 2 / 4 / 8 with colour tables, default Macintosh
|
||||
-- tables and grey ramps; 16 / 24 / 32-bit and grey from ffmpeg) and QuickTime SMC, in AVI, MOV and MKV: every frame
|
||||
-- equals ffmpeg's decode (CRC-32 of rgba); seeking; the registry entries; the fuzz pass on each codec's packet decoder.
|
||||
-- Fixtures: tools/fixtures/legacy.py.
|
||||
local T = require("@tests/T")
|
||||
local F = require("@formats")
|
||||
local Crc32 = require("@formats/core/Crc32")
|
||||
local Video = require("@formats/codecs/video/Video")
|
||||
local Legacy = require("@formats/codecs/video/Legacy")
|
||||
local Fx = require("@tests/gen/legacy_cases")
|
||||
|
||||
local t = T.suite("legacy")
|
||||
|
||||
-- the registry
|
||||
t:eq(Video.fromFourcc("cvid"), "cinepak", "cvid")
|
||||
t:eq(Video.fromFourcc("CRAM"), "msvideo1", "CRAM")
|
||||
t:eq(Video.fromFourcc("msvc"), "msvideo1", "msvc")
|
||||
t:eq(Video.fromFourcc("rle "), "qtrle", "rle ")
|
||||
t:eq(Video.fromFourcc("smc "), "smc", "smc ")
|
||||
for _, id in Legacy.CODECS do
|
||||
t:check(Video.canDecode(id), id .. ": decodable")
|
||||
end
|
||||
|
||||
local samples: { [string]: { any } } = {}
|
||||
|
||||
for _, c in Fx do
|
||||
local label = c.name
|
||||
local v, err = F.video.open(c.data)
|
||||
t:check(v ~= nil, label .. ": open: " .. tostring(err))
|
||||
if v then
|
||||
t:eq(v.codec, c.codec, label .. ": codec")
|
||||
t:eq(v.width, c.width, label .. ": width")
|
||||
local n, bad, first = 0, 0, nil
|
||||
while true do
|
||||
local f, ferr = v:nextFrame()
|
||||
if not f then
|
||||
t:check(ferr == nil, label .. ": decode error " .. tostring(ferr))
|
||||
break
|
||||
end
|
||||
n += 1
|
||||
local img = f.image
|
||||
if img.format ~= "rgba8" or img.width ~= c.width or Crc32.of(img.pixels) ~= c.frames[n] then
|
||||
bad += 1
|
||||
first = first or n
|
||||
end
|
||||
end
|
||||
t:check(n == #c.frames and bad == 0, string.format("%s: %d of %d frames equal ffmpeg's (%d differ, first %s)", label, n - bad, #c.frames, bad, tostring(first)))
|
||||
-- seeking: back to the start, the first frame again
|
||||
v:seek(0)
|
||||
local f = v:nextFrame()
|
||||
t:check(f ~= nil and Crc32.of(f.image.pixels) == c.frames[1], label .. ": seek(0)")
|
||||
-- packets for the fuzz pass
|
||||
local dmx = v.demuxer
|
||||
local tr = dmx:track("video")
|
||||
if not samples[c.codec] then
|
||||
local list = {}
|
||||
for k = 1, math.min(3, dmx:count(tr.id)) do
|
||||
table.insert(list, dmx:get(tr.id, k).data)
|
||||
end
|
||||
samples[c.codec] = { track = tr, packets = list }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- the fuzz pass on the native packet decoders (no pcall in between)
|
||||
local P = Legacy.Picture
|
||||
local raw: { [string]: (tr: any) -> (buffer) -> any } = {
|
||||
cinepak = function(tr)
|
||||
return function(b)
|
||||
local pic = P.new(tr.width, tr.height, tr.bitsPerPixel == 8, 4, 4, tr.palette)
|
||||
return Legacy.Cinepak.decode(pic, b, Legacy.Cinepak.state(), false)
|
||||
end
|
||||
end,
|
||||
msvideo1 = function(tr)
|
||||
return function(b)
|
||||
local bits = if tr.bitsPerPixel == 8 then 8 else 16
|
||||
return Legacy.Msvideo1.decode(P.new(tr.width, tr.height, bits == 8, 4, 4), b, bits)
|
||||
end
|
||||
end,
|
||||
qtrle = function(tr)
|
||||
return function(b)
|
||||
local d = tr.bitsPerPixel or 24
|
||||
for _, depth in { d, 1, 2, 4, 8, 16, 32 } do
|
||||
Legacy.QtRle.decode(P.new(tr.width, tr.height, Legacy.QtRle.palettised(depth), Legacy.QtRle.pad(depth), 1), b, depth)
|
||||
end
|
||||
return true
|
||||
end
|
||||
end,
|
||||
smc = function(tr)
|
||||
return function(b)
|
||||
return Legacy.Smc.decode(P.new(tr.width, tr.height, true, 4, 4), b, Legacy.Smc.tables())
|
||||
end
|
||||
end,
|
||||
}
|
||||
for id, s in samples do
|
||||
local fn = raw[id]
|
||||
if fn then
|
||||
t:fuzz(id .. " packets", fn(s.track), s.packets, { mutations = 24, garbage = 60 })
|
||||
end
|
||||
end
|
||||
t:fuzz("open", function(b: buffer)
|
||||
local v = F.video.open(b)
|
||||
if v then
|
||||
for _ = 1, 3 do
|
||||
if not v:nextFrame() then
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
return v
|
||||
end, { Fx[1].data, Fx[#Fx].data }, { mutations = 8, garbage = 20 })
|
||||
t:done()
|
||||
+25
-2
@@ -1,4 +1,5 @@
|
||||
-- codecs/video/Mpeg1 tests, ported from mpeg1-luau tests/decode.luau and tests/robust.luau. Against ffmpeg
|
||||
-- codecs/video/Mpeg1 tests, ported from mpeg1-luau tests/decode.luau and tests/robust.luau. Every frame bit-exact with
|
||||
-- `ffmpeg -idct int` (the IDCT codecs/video/Dsp reproduces; frames with concealed macroblocks excepted). Against ffmpeg
|
||||
-- (tools/fixtures/mpeg1.py): every stream decodes, one access unit at a time, to as many frames as ffmpeg's decode,
|
||||
-- in display order; each frame's RGBA channel means within 1.5 of ffmpeg's; whole sampled frames within 2 MAE per
|
||||
-- channel (IDCT and colour rounding) and, as yuv420p (frameImage), within a PSNR floor of ffmpeg -pix_fmt yuv420p;
|
||||
@@ -11,6 +12,7 @@ local Mpeg1 = require("@formats/codecs/video/Mpeg1")
|
||||
local Base64 = require("@formats/core/Base64")
|
||||
local Deflate = require("@formats/codecs/compress/Deflate")
|
||||
local Types = require("@formats/Types")
|
||||
local Crc32 = require("@formats/core/Crc32")
|
||||
|
||||
local STREAMS = {
|
||||
require("@tests/gen/mpeg1_qcif_200k"),
|
||||
@@ -81,6 +83,7 @@ local function psnr(a: buffer, ao: number, b: buffer, bo: number, n: number): nu
|
||||
end
|
||||
|
||||
local MAE_LIMIT = 2
|
||||
local totalExact, totalConcealed = 0, 0
|
||||
local MEAN_LIMIT = 1.5
|
||||
|
||||
-- 1. every stream against ffmpeg
|
||||
@@ -101,6 +104,7 @@ for _, fx in STREAMS do
|
||||
local hasB = string.find(name, "bframes") ~= nil
|
||||
local dec = Mpeg1.new({ reorder = hasB })
|
||||
local frames = 0
|
||||
local exact, concealed = 0, 0
|
||||
local maxMae, maxMeanErr = 0, 0
|
||||
local sums = {}
|
||||
local function onFrame(f: any)
|
||||
@@ -136,6 +140,21 @@ for _, fx in STREAMS do
|
||||
end
|
||||
t:check(buffer.readu8(px, 3) == 255 and buffer.readu8(px, n * 4 - 1) == 255, "alpha 255")
|
||||
sums[frames] = buffer.tostring(px)
|
||||
-- bit-exact against ffmpeg -idct int (outside macroblocks the decoder had to conceal)
|
||||
local ex = fx.exact[frames]
|
||||
if ex then
|
||||
local img = Mpeg1.frameImage(f) :: Types.Image
|
||||
local yc = Crc32.of(img.pixels, 0, n)
|
||||
local uc = Crc32.of(img.pixels, n, cw * ch)
|
||||
local vc = Crc32.of(img.pixels, n + cw * ch, cw * ch)
|
||||
if yc == ex[1] and uc == ex[2] and vc == ex[3] then
|
||||
exact += 1
|
||||
elseif next(f.concealed) ~= nil then
|
||||
concealed += 1
|
||||
else
|
||||
t:check(false, string.format("%s frame %d (type %d): differs from ffmpeg -idct int", name, frames, f.pictureType))
|
||||
end
|
||||
end
|
||||
end
|
||||
local t0 = os.clock()
|
||||
for _, u in fx.units do
|
||||
@@ -155,7 +174,10 @@ for _, fx in STREAMS do
|
||||
t:eq(frames, fx.frames, name .. ": frames (ffmpeg's count)")
|
||||
t:eq(dec.stats.errors, 0, name .. ": no bitstream errors")
|
||||
worst = math.max(worst, maxMae)
|
||||
t:note("%-13s %4dx%-4d %3d frames worst MAE %.3f means err %.3f %6.1f fps (with the checks)", name, w, h, frames, maxMae, maxMeanErr, frames / dt)
|
||||
t:note("%-13s %4dx%-4d %3d frames %3d bit-exact worst MAE %.3f means err %.3f %6.1f fps (with the checks)", name, w, h, frames, exact, maxMae, maxMeanErr, frames / dt)
|
||||
t:eq(exact + concealed, frames, name .. ": frames equal to ffmpeg -idct int (or with concealed macroblocks)")
|
||||
totalExact += exact
|
||||
totalConcealed += concealed
|
||||
|
||||
-- streamed: random pieces through write / decodeFrame, then flush
|
||||
local d2 = Mpeg1.new({ reorder = hasB })
|
||||
@@ -187,6 +209,7 @@ for _, fx in STREAMS do
|
||||
t:eq(got, frames, name .. ": streamed frame count")
|
||||
end
|
||||
t:note("worst sampled-frame MAE %.3f (limit %d); worst yuv420p PSNR luma %.1f, chroma %.1f dB", worst, MAE_LIMIT, worstY, worstC)
|
||||
t:note("%d frames bit-exact with ffmpeg -idct int, %d with concealed macroblocks (ffmpeg error concealment)", totalExact, totalConcealed)
|
||||
|
||||
-- 2. a size change mid-stream
|
||||
do
|
||||
|
||||
@@ -0,0 +1,304 @@
|
||||
-- codecs/video/Mpeg2 tests. Against `ffmpeg -idct int` (tools/fixtures/mpeg2.py): every stream (progressive and
|
||||
-- interlaced frame pictures with field prediction and field DCT, top / bottom field first, 4:2:2, the alternate scan,
|
||||
-- Table B-15, the non-linear quantiser scale, DC precision 9..11, custom luma / chroma matrices, f_code > 1, an odd
|
||||
-- size, B-frames, dual prime, field pictures, concealment vectors) decodes one access unit at a time to exactly ffmpeg's planes, every frame, in display
|
||||
-- order; the same stream written in random pieces gives the same frames; frame flags (mpeg2, chroma, progressive,
|
||||
-- topFieldFirst); frameImage (4:2:2 -> yuv420p, rgba8); the registry's VideoCodecDecoder; truncated and corrupted
|
||||
-- streams never throw; the fuzz no-throw pass.
|
||||
local T = require("@tests/T")
|
||||
local Mpeg2 = require("@formats/codecs/video/Mpeg2")
|
||||
local Video = require("@formats/codecs/video/Video")
|
||||
local Base64 = require("@formats/core/Base64")
|
||||
local Crc32 = require("@formats/core/Crc32")
|
||||
local Deflate = require("@formats/codecs/compress/Deflate")
|
||||
|
||||
local STREAMS = {
|
||||
require("@tests/gen/mpeg2_prog"),
|
||||
require("@tests/gen/mpeg2_tff"),
|
||||
require("@tests/gen/mpeg2_bff_alt"),
|
||||
require("@tests/gen/mpeg2_tools"),
|
||||
require("@tests/gen/mpeg2_dc9_matrix"),
|
||||
require("@tests/gen/mpeg2_dc11_q1"),
|
||||
require("@tests/gen/mpeg2_motion"),
|
||||
require("@tests/gen/mpeg2_odd_ilace"),
|
||||
require("@tests/gen/mpeg2_c422"),
|
||||
require("@tests/gen/mpeg2_c422_ilace"),
|
||||
require("@tests/gen/mpeg2_dualprime"),
|
||||
require("@tests/gen/mpeg2_fields"),
|
||||
require("@tests/gen/mpeg2_fields_conceal"),
|
||||
}
|
||||
|
||||
local t = T.suite("codecs/mpeg2")
|
||||
local rand = T.rng(11)
|
||||
|
||||
local function b64(s: string): buffer
|
||||
return Base64.decode(s) :: buffer
|
||||
end
|
||||
|
||||
-- CRC-32 of a plane cropped to w x h from a buffer with stride
|
||||
local function planeCrc(b: buffer, stride: number, w: number, h: number): number
|
||||
local row = buffer.create(w * h)
|
||||
for y = 0, h - 1 do
|
||||
buffer.copy(row, y * w, b, y * stride, w)
|
||||
end
|
||||
return Crc32.of(row)
|
||||
end
|
||||
|
||||
-- the cropped planes of a frame, as ffmpeg writes them (w x h luma, ceil(w / 2) x (h or ceil(h / 2)) chroma)
|
||||
local function crcs(f: any): (number, number, number)
|
||||
local w, h = f.width, f.height
|
||||
local cw = (w + 1) // 2
|
||||
local ch = if f.chroma == 2 then h else (h + 1) // 2
|
||||
local chw = f.codedWidth // 2
|
||||
return planeCrc(f.y, f.codedWidth, w, h), planeCrc(f.cb, chw, cw, ch), planeCrc(f.cr, chw, cw, ch)
|
||||
end
|
||||
|
||||
-- differing samples between a frame's luma and a reference yuv frame (debug detail on a mismatch)
|
||||
local function lumaDiff(f: any, ref: buffer): (number, number)
|
||||
local n, first = 0, -1
|
||||
for y = 0, f.height - 1 do
|
||||
for x = 0, f.width - 1 do
|
||||
if buffer.readu8(f.y, y * f.codedWidth + x) ~= buffer.readu8(ref, y * f.width + x) then
|
||||
n += 1
|
||||
if first < 0 then
|
||||
first = y * 65536 + x
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
return n, first
|
||||
end
|
||||
|
||||
local function decodeUnits(fx: any, file: buffer): { any }
|
||||
local dec = Mpeg2.new({ reorder = true, rgba = false })
|
||||
local out = {}
|
||||
local function take(f: any)
|
||||
local y, u, v = crcs(f)
|
||||
-- the 16-line luma bands now: the planes belong to the decoder
|
||||
local bands = {}
|
||||
for b = 0, (f.height + 15) // 16 - 1 do
|
||||
local rows = math.min(16, f.height - b * 16)
|
||||
local buf = buffer.create(f.width * rows)
|
||||
for r = 0, rows - 1 do
|
||||
buffer.copy(buf, r * f.width, f.y, (b * 16 + r) * f.codedWidth, f.width)
|
||||
end
|
||||
bands[b] = Crc32.of(buf)
|
||||
end
|
||||
table.insert(out, { y, u, v, f, bands })
|
||||
end
|
||||
for i, u in fx.units do
|
||||
local f = dec:decode(file, u[1], u[2], i)
|
||||
while f do
|
||||
take(f)
|
||||
f = dec:decode(file, 0, 0, i)
|
||||
end
|
||||
end
|
||||
while true do
|
||||
local f = dec:flush()
|
||||
if not f then
|
||||
break
|
||||
end
|
||||
take(f)
|
||||
end
|
||||
return out
|
||||
end
|
||||
|
||||
-- 1. every stream against ffmpeg, bit-exact. ffmpeg's encoder sometimes ends a B-picture slice before the row's last
|
||||
-- macroblock (invalid MPEG-2); both decoders conceal the gap, ffmpeg with its error-resilience guesses and deblocking.
|
||||
-- Frames with concealed macroblocks compare every 16-line luma band but those and their neighbours.
|
||||
local total, concealedFrames = 0, 0
|
||||
for _, fx in STREAMS do
|
||||
local name = fx.name
|
||||
local file = b64(fx.file)
|
||||
local t0 = os.clock()
|
||||
local got = decodeUnits(fx, file)
|
||||
local dt = os.clock() - t0
|
||||
t:eq(#got, fx.frames, name .. ": frame count")
|
||||
local bad, msg = 0, ""
|
||||
for k, ref in fx.crcs do
|
||||
local g = got[k]
|
||||
if g and (g[1] ~= ref[1] or g[2] ~= ref[2] or g[3] ~= ref[3]) then
|
||||
local f = g[4]
|
||||
local ok = next(f.concealed) ~= nil
|
||||
if ok then
|
||||
concealedFrames += 1
|
||||
for band, crc in fx.bands[k] do
|
||||
local b = band - 1
|
||||
if not (f.concealed[b] or f.concealed[b - 1] or f.concealed[b + 1]) then
|
||||
ok = ok and g[5][b] == crc
|
||||
end
|
||||
end
|
||||
end
|
||||
if not ok then
|
||||
bad += 1
|
||||
if bad == 1 then
|
||||
local detail = ""
|
||||
if k == 1 and #got == 1 then
|
||||
local n, first = lumaDiff(f, Deflate.zlib(b64(fx.first)) :: buffer)
|
||||
detail = string.format(" (%d luma samples differ, first at %d,%d)", n, first % 65536, first // 65536)
|
||||
end
|
||||
msg = string.format(" (first: frame %d, type %d: y %s u %s v %s%s)", k, f.pictureType,
|
||||
tostring(g[1] == ref[1]), tostring(g[2] == ref[2]), tostring(g[3] == ref[3]), detail)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
t:eq(bad, 0, name .. ": frames differing from ffmpeg" .. msg)
|
||||
local f1 = got[1] and got[1][4]
|
||||
if f1 then
|
||||
t:check(f1.mpeg2 == true, name .. ": mpeg2 flag")
|
||||
t:eq(f1.chroma, fx.chroma, name .. ": chroma format")
|
||||
end
|
||||
total += #got
|
||||
t:note(" %-12s %4dx%-4d %s %3d frames %6.1f fps (with the checks)", name, fx.width, fx.height,
|
||||
if fx.chroma == 2 then "4:2:2" else "4:2:0", #got, #got / dt)
|
||||
end
|
||||
|
||||
-- 2. interlace flags
|
||||
do
|
||||
local got = decodeUnits(STREAMS[2], b64(STREAMS[2].file))
|
||||
t:check(got[1][4].progressive == false and got[1][4].topFieldFirst == true, "tff: interlaced, top field first")
|
||||
got = decodeUnits(STREAMS[3], b64(STREAMS[3].file))
|
||||
t:check(got[1][4].progressive == false and got[1][4].topFieldFirst == false, "bff_alt: bottom field first")
|
||||
got = decodeUnits(STREAMS[1], b64(STREAMS[1].file))
|
||||
t:check(got[1][4].progressive == true, "prog: progressive")
|
||||
end
|
||||
|
||||
-- 3. the same stream written in random pieces (decodeFrame) gives the same frames as unit by unit
|
||||
for _, idx in { 2, 9, 12 } do
|
||||
local fx = STREAMS[idx]
|
||||
local file = b64(fx.file)
|
||||
local want = decodeUnits(fx, file)
|
||||
local dec = Mpeg2.new({ reorder = true, rgba = false })
|
||||
local p, k, ok = 0, 0, true
|
||||
local n = buffer.len(file)
|
||||
local function check(f: any)
|
||||
k += 1
|
||||
local y, u, v = crcs(f)
|
||||
local ref = want[k]
|
||||
ok = ok and ref ~= nil and y == ref[1] and u == ref[2] and v == ref[3]
|
||||
end
|
||||
while p < n do
|
||||
local len = math.min(n - p, 1 + rand(5000))
|
||||
dec:write(file, p, len)
|
||||
p += len
|
||||
while true do
|
||||
local f = dec:decodeFrame()
|
||||
if not f then
|
||||
break
|
||||
end
|
||||
check(f)
|
||||
end
|
||||
end
|
||||
while true do
|
||||
local f = dec:flush()
|
||||
if not f then
|
||||
break
|
||||
end
|
||||
check(f)
|
||||
end
|
||||
t:check(ok and k == fx.frames, string.format("%s: random pieces give the same %d frames (%d)", fx.name, fx.frames, k))
|
||||
end
|
||||
|
||||
-- 4. frameImage: yuv420p (4:2:2 converted), rgba8
|
||||
do
|
||||
local fx = STREAMS[9]
|
||||
local dec = Mpeg2.new({ reorder = false, rgba = true })
|
||||
local file = b64(fx.file)
|
||||
local f = dec:decode(file, fx.units[1][1], fx.units[1][2])
|
||||
t:check(f ~= nil and f.pixels ~= nil, "c422: rgba frame")
|
||||
if f then
|
||||
local img = Mpeg2.frameImage(f)
|
||||
t:check(img ~= nil and img.format == "yuv420p" and img.width == fx.width, "c422: frameImage yuv420p")
|
||||
if img then
|
||||
local cw, ch = (fx.width + 1) // 2, (fx.height + 1) // 2
|
||||
t:eq(buffer.len(img.pixels), fx.width * fx.height + 2 * cw * ch, "c422: yuv420p size")
|
||||
t:eq(img.meta.source.format, "mpeg2", "c422: meta.source.format")
|
||||
t:eq(img.meta.source.chroma, "422", "c422: meta.source.chroma")
|
||||
end
|
||||
local rgba = Mpeg2.frameImage(f, true)
|
||||
t:check(rgba ~= nil and rgba.format == "rgba8", "c422: frameImage rgba8")
|
||||
end
|
||||
t:check(Mpeg2.frameImage({} :: any) == nil, "frameImage of a non-frame is nil")
|
||||
end
|
||||
|
||||
-- 5. the registry's decoder (packets = access units), display order with pts
|
||||
do
|
||||
t:check(Video.fromFourcc("mpg2") == "mpeg2" and Video.fromMatroska("V_MPEG2") == "mpeg2", "registry names")
|
||||
t:check(Video.canDecode("mpeg2") and Video.canDecode("mpeg1"), "registry decoders")
|
||||
local fx = STREAMS[1]
|
||||
local file = b64(fx.file)
|
||||
local want = decodeUnits(fx, file)
|
||||
local dec = assert(Video.decoder({ id = 1, kind = "video", codec = "mpeg2", timescale = 25 } :: any))
|
||||
local frames, order = 0, true
|
||||
local last = -1
|
||||
for i, u in fx.units do
|
||||
local d = buffer.create(u[2])
|
||||
buffer.copy(d, 0, file, u[1], u[2])
|
||||
dec:decode({ track = 1, pts = i, keyframe = false, data = d } :: any)
|
||||
while true do
|
||||
local img, pts = dec:receive()
|
||||
if not img then
|
||||
break
|
||||
end
|
||||
frames += 1
|
||||
local ref = want[frames]
|
||||
local w, h = fx.width, fx.height
|
||||
order = order and ref ~= nil and Crc32.of(img.pixels, 0, w * h) == ref[1] and img.meta.yuv ~= nil
|
||||
last = pts or last
|
||||
end
|
||||
end
|
||||
dec:flush()
|
||||
while dec:receive() do
|
||||
frames += 1
|
||||
end
|
||||
t:eq(frames, fx.frames, "registry decoder: frame count")
|
||||
t:check(order, "registry decoder: frames in display order, equal to the codec's")
|
||||
end
|
||||
|
||||
-- 6. robustness: truncated units, bit flips, garbage (never throws), and the fuzz pass
|
||||
do
|
||||
local samples = {}
|
||||
for _, idx in { 1, 2, 9, 11 } do
|
||||
local fx = STREAMS[idx]
|
||||
local file = b64(fx.file)
|
||||
local n = math.min(buffer.len(file), 60000)
|
||||
local b = buffer.create(n)
|
||||
buffer.copy(b, 0, file, 0, n)
|
||||
table.insert(samples, b)
|
||||
end
|
||||
local threw = 0
|
||||
for round = 1, 12 do
|
||||
local src = samples[1 + round % #samples]
|
||||
local b = buffer.create(buffer.len(src))
|
||||
buffer.copy(b, 0, src)
|
||||
for _ = 1, 20 do
|
||||
local i = rand(buffer.len(b))
|
||||
buffer.writeu8(b, i, bit32.bxor(buffer.readu8(b, i), bit32.lshift(1, rand(8))))
|
||||
end
|
||||
local ok = pcall(function()
|
||||
local dec = Mpeg2.new({ reorder = true })
|
||||
dec:write(b)
|
||||
while dec:decodeFrame() do
|
||||
end
|
||||
while dec:flush() do
|
||||
end
|
||||
end)
|
||||
if not ok then
|
||||
threw += 1
|
||||
end
|
||||
end
|
||||
t:eq(threw, 0, "corrupted streams never throw")
|
||||
t:fuzz("decode", function(b: buffer)
|
||||
local dec = Mpeg2.new({ reorder = true })
|
||||
dec:write(b)
|
||||
local f = dec:decodeFrame()
|
||||
while f do
|
||||
f = dec:decodeFrame()
|
||||
end
|
||||
return dec:flush()
|
||||
end, samples, { mutations = 60, garbage = 10 })
|
||||
end
|
||||
|
||||
t:note(" %d frames bit-exact with ffmpeg -idct int (%d with concealed macroblocks: compared outside them)", total, concealedFrames)
|
||||
t:done()
|
||||
@@ -0,0 +1,93 @@
|
||||
-- codecs/model/Rig tests: morph targets and weights animations from frames, skeletons in local and world space
|
||||
-- (node TRS, inverse bind matrices against Mat4), influences (top four, normalised), joint TRS animations; and
|
||||
-- Pixels.palette.
|
||||
local T = require("@tests/T")
|
||||
local Rig = require("@formats/codecs/model/Rig")
|
||||
local Mat4 = require("@formats/codecs/model/Mat4")
|
||||
local Pixels = require("@formats/codecs/image/Pixels")
|
||||
local Types = require("@formats/Types")
|
||||
|
||||
local t = T.suite("codecs/rig")
|
||||
local Scene = Types.Scene
|
||||
|
||||
local function f32s(list: { number }): buffer
|
||||
local b = buffer.create(#list * 4)
|
||||
for i, v in list do
|
||||
buffer.writef32(b, (i - 1) * 4, v)
|
||||
end
|
||||
return b
|
||||
end
|
||||
local function near(a: number, b: number): boolean
|
||||
return math.abs(a - b) < 1e-5
|
||||
end
|
||||
|
||||
-- morph
|
||||
do
|
||||
local base = { 0, 0, 0, 1, 0, 0 }
|
||||
local prim: Types.Primitive = { attributes = { POSITION = Scene.accessor(f32s(base), "f32", 3, 2) }, mode = "points" }
|
||||
local frames = { f32s(base), f32s({ 0, 1, 0, 1, 2, 0 }), f32s({ 0, 0, 3, 1, 0, 3 }) }
|
||||
t:check(Rig.morph(prim, frames), "morph")
|
||||
local tg = prim.targets :: any
|
||||
t:eq(#tg, 3, "one target per frame")
|
||||
t:eq(buffer.readf32(tg[2].POSITION.data, 4), 1, "delta y")
|
||||
t:eq(buffer.readf32(tg[3].POSITION.data, 20), 3, "delta z")
|
||||
t:check(not Rig.morph(prim, { buffer.create(4) }), "short frame refused")
|
||||
local s = Scene.new()
|
||||
s.nodes[1] = { children = {}, mesh = 1 }
|
||||
Rig.morphAnimation(s, 1, 3, { { name = "a", first = 2, last = 3, fps = 5 }, { name = "none", first = 9, last = 12 } })
|
||||
t:eq(#s.animations, 1, "out-of-range range skipped")
|
||||
local ch = s.animations[1].channels[1]
|
||||
t:eq(ch.path, "weights", "weights channel")
|
||||
t:eq(ch.input.count, 2, "two keys")
|
||||
t:check(near(buffer.readf32(ch.input.data, 4), 0.2), "fps")
|
||||
t:eq(buffer.readf32(ch.output.data, 1 * 4), 1, "key 1 -> target 2")
|
||||
t:eq(buffer.readf32(ch.output.data, (3 + 2) * 4), 1, "key 2 -> target 3")
|
||||
end
|
||||
|
||||
-- skeleton
|
||||
do
|
||||
local q = Mat4.axisAngle(0, 0, 1, 90)
|
||||
local joints = {
|
||||
{ name = "root", parent = 0, translation = { 1, 0, 0 }, rotation = q },
|
||||
{ name = "child", parent = 1, translation = { 0, 2, 0 }, rotation = { 0, 0, 0, 1 } },
|
||||
}
|
||||
local s = Scene.new()
|
||||
local nodes, skin = Rig.skeleton(s, joints, "local")
|
||||
t:eq(#nodes, 2, "two joint nodes")
|
||||
t:eq(s.roots[1], nodes[1], "root")
|
||||
t:eq(s.nodes[nodes[1]].children[1], nodes[2], "parenting")
|
||||
local ibm = s.skins[skin].inverseBindMatrices :: Types.Accessor
|
||||
-- child world = T(1,0,0) R90 T(0,2,0): its origin is at (-1, 0, 0); the inverse maps it back to 0
|
||||
local inv = {}
|
||||
for k = 1, 16 do
|
||||
inv[k] = buffer.readf32(ibm.data, (16 + k - 1) * 4)
|
||||
end
|
||||
local x, y, z = Mat4.point(inv, -1, 0, 0)
|
||||
t:check(near(x, 0) and near(y, 0) and near(z, 0), "inverse bind matrix")
|
||||
-- the same joints given in world space give the same local TRS
|
||||
local w = Scene.new()
|
||||
local wn = Rig.skeleton(w, {
|
||||
joints[1],
|
||||
{ name = "child", parent = 1, translation = { -1, 0, 0 }, rotation = q },
|
||||
}, "world")
|
||||
local c = w.nodes[wn[2]]
|
||||
t:check(near(c.translation[1], 0) and near(c.translation[2], 2) and near(c.rotation[4], 1), "world -> local")
|
||||
local j, wt = Rig.influences({ { 0, 1 }, { 1, 0.2, 2, 0.6, 3, 0.1, 4, 0.05, 5, 0.05 } })
|
||||
t:eq(buffer.readu16(j.data, 8), 2, "largest weight first")
|
||||
t:check(near(buffer.readf32(wt.data, 16) + buffer.readf32(wt.data, 20) + buffer.readf32(wt.data, 24) + buffer.readf32(wt.data, 28), 1), "normalised")
|
||||
t:eq(buffer.readf32(wt.data, 0), 1, "single influence")
|
||||
local ai = Rig.animation(s, "wave", nodes, { 0, 0.5 }, { [2] = { rotation = { 0, 0, 0, 1, 0, 0, 1, 0 }, translation = { 0 } } })
|
||||
t:eq(#s.animations[ai].channels, 1, "short track skipped, rotation kept")
|
||||
t:eq(s.animations[ai].channels[1].node, nodes[2], "channel node")
|
||||
end
|
||||
|
||||
-- palette
|
||||
do
|
||||
local pal = Pixels.palette(buffer.fromstring("\1\2\3\4\5\6"), 0, 2)
|
||||
t:eq(buffer.readu32(pal, 4), 0xFF060504, "rgb triples")
|
||||
t:eq(buffer.readu32(pal, 8), 0xFF000000, "missing entries black")
|
||||
t:eq(buffer.readu32(Pixels.palette(buffer.fromstring("\1\2\3"), 0, 1, true), 0), 0xFF010203, "bgr")
|
||||
t:eq(buffer.readu32(Pixels.palette(buffer.create(3), -5, 1), 0), 0xFF000000, "bad base")
|
||||
end
|
||||
|
||||
t:done()
|
||||
@@ -0,0 +1,92 @@
|
||||
-- codecs/model/Sampler tests: STEP / LINEAR / CUBICSPLINE channels (glTF's Hermite definition), quaternion slerp
|
||||
-- (shortest path, unit length), clamping outside the keys, weights channels, Sampler.times and Sampler.pose; glTF's
|
||||
-- InterpolationTest sample model (every interpolation) sampled at its keys gives the key values; bad channels.
|
||||
local T = require("@tests/T")
|
||||
local Sampler = require("@formats/codecs/model/Sampler")
|
||||
local Gltf = require("@formats/model/gltf")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Types = require("@formats/Types")
|
||||
local Picks = require("@tests/gen/iqm_gltf")
|
||||
|
||||
local t = T.suite("codecs/sampler")
|
||||
local S = Types.Scene
|
||||
|
||||
local function acc(list: { number }, comps: number): Types.Accessor
|
||||
local b = buffer.create(#list * 4)
|
||||
for i, v in list do
|
||||
buffer.writef32(b, (i - 1) * 4, v)
|
||||
end
|
||||
return S.accessor(b, "f32", comps, #list // comps)
|
||||
end
|
||||
local function near(a: number, b: number, eps: number?): boolean
|
||||
return math.abs(a - b) < (eps or 1e-5)
|
||||
end
|
||||
|
||||
do
|
||||
local ch: Types.Channel = { node = 1, path = "translation", input = acc({ 0, 1, 3 }, 1), output = acc({ 0, 0, 0, 2, 4, 6, 2, 0, 0 }, 3), interpolation = "LINEAR" }
|
||||
local s = assert(Sampler.channel(ch))
|
||||
t:check(near(s(0.5)[1], 1) and near(s(0.5)[2], 2), "linear: halfway")
|
||||
t:check(near(s(2)[1], 2) and near(s(2)[2], 2), "linear: second span")
|
||||
t:check(near(s(-1)[2], 0) and near(s(9)[1], 2), "linear: clamped outside the keys")
|
||||
ch.interpolation = "STEP"
|
||||
local st = assert(Sampler.channel(ch))
|
||||
t:check(near(st(0.99)[1], 0) and near(st(1)[1], 2), "step")
|
||||
end
|
||||
do
|
||||
-- rotation: 0 -> 90 degrees about z, and a key stored with the opposite sign (shortest path)
|
||||
local h = math.sqrt(0.5)
|
||||
local ch: Types.Channel = { node = 1, path = "rotation", input = acc({ 0, 1, 2 }, 1), output = acc({ 0, 0, 0, 1, 0, 0, h, h, 0, 0, -h, -h }, 4), interpolation = "LINEAR" }
|
||||
local s = assert(Sampler.channel(ch))
|
||||
local q = s(0.5)
|
||||
local a = 2 * math.acos(q[4])
|
||||
t:check(near(a, math.pi / 4, 1e-4) and near(q[1] ^ 2 + q[2] ^ 2 + q[3] ^ 2 + q[4] ^ 2, 1), "slerp: 45 degrees, unit length")
|
||||
local q2 = s(1.5)
|
||||
t:check(near(math.abs(q2[3]), h, 1e-4), "slerp: the same orientation stored negated stays put")
|
||||
end
|
||||
do
|
||||
-- cubic spline: in-tangent, value, out-tangent per key; p(t) for p0 = 0, p1 = 1, m0 = m1 = 0 is smoothstep
|
||||
local ch: Types.Channel = { node = 1, path = "scale", input = acc({ 0, 2 }, 1), output = acc({ 9, 9, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 9, 9, 9 }, 3), interpolation = "CUBICSPLINE" }
|
||||
local s = assert(Sampler.channel(ch))
|
||||
t:check(near(s(1)[1], 0.5) and near(s(0.5)[1], 3 * 0.25 ^ 2 - 2 * 0.25 ^ 3), "cubic: smoothstep")
|
||||
t:check(near(s(2)[2], 1), "cubic: the last value")
|
||||
-- tangents scale with the key spacing
|
||||
local ch2: Types.Channel = { node = 1, path = "translation", input = acc({ 0, 2 }, 1), output = acc({ 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, 3), interpolation = "CUBICSPLINE" }
|
||||
local s2 = assert(Sampler.channel(ch2))
|
||||
-- h10(0.5) * dt * m0 = 0.125 * 2 * 1
|
||||
t:check(near(s2(1)[1], 0.25), "cubic: tangents times the key spacing")
|
||||
end
|
||||
do
|
||||
local ch: Types.Channel = { node = 2, path = "weights", input = acc({ 0, 1 }, 1), output = acc({ 1, 0, 0, 1 }, 1), interpolation = "LINEAR" }
|
||||
local s = assert(Sampler.channel(ch))
|
||||
local w = s(0.25)
|
||||
t:check(#w == 2 and near(w[1], 0.75) and near(w[2], 0.25), "weights")
|
||||
local anim: Types.Animation = { name = "a", channels = { ch, { node = 1, path = "translation", input = acc({ 0.5, 1 }, 1), output = acc({ 0, 0, 0, 1, 1, 1 }, 3), interpolation = "STEP" } } }
|
||||
local times = Sampler.times(anim)
|
||||
t:eq(table.concat(times, " "), "0 0.5 1", "times: sorted, unique")
|
||||
local pose = Sampler.pose(S.new(), anim, 1)
|
||||
t:check(pose[2].weights and pose[1].translation and near(pose[1].translation[1], 1), "pose")
|
||||
t:check(Sampler.channel({ node = 1, path = "translation", input = acc({ 0, 1 }, 1), output = acc({ 1, 2 }, 1), interpolation = "LINEAR" }) == nil, "short output: nil")
|
||||
t:check(Sampler.channel({ node = 1, path = "translation", input = {} :: any, output = {} :: any, interpolation = "LINEAR" }) == nil, "junk: nil")
|
||||
end
|
||||
do
|
||||
-- a real file: sampling a glTF sample model's skin animation at its own keys returns the key values
|
||||
local g = assert(Gltf.decode(Bytes.reader(buffer.fromstring(Picks[1].data)), { files = Picks[1].files }))
|
||||
local ok, total = true, 0
|
||||
for _, anim in g.animations do
|
||||
for _, ch in anim.channels do
|
||||
local s = assert(Sampler.channel(ch))
|
||||
local IN, OUT = assert(S.reader(ch.input)), assert(S.reader(ch.output))
|
||||
local comps = if ch.path == "rotation" then 4 else 3
|
||||
for k = 0, ch.input.count - 1 do
|
||||
local v = s(IN(k, 0))
|
||||
for c = 1, comps do
|
||||
ok = ok and near(v[c], OUT(k, c - 1), 1e-4)
|
||||
end
|
||||
total += 1
|
||||
end
|
||||
end
|
||||
end
|
||||
t:check(ok and total > 0, Picks[1].name .. ": samples at the keys are the keys (" .. total .. ")")
|
||||
end
|
||||
|
||||
t:done()
|
||||
@@ -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()
|
||||
@@ -0,0 +1,148 @@
|
||||
-- codecs/model/Vrml tests: the classic-encoding lexer and parser (comments, commas, string escapes, hex / exponent
|
||||
-- numbers, DEF / USE, PROTO with IS fields, nested PROTOs and ROUTEs in bodies, EXTERNPROTO, Script interfaces,
|
||||
-- IMPORT / EXPORT, the X3D header statements), the XML encoding's adapter (containerField, ProtoDeclare /
|
||||
-- ProtoInstance, IS / connect), typed field reads of raw XML attributes, detection, errors, the fuzz pass.
|
||||
local T = require("@tests/T")
|
||||
local Vrml = require("@formats/codecs/model/Vrml")
|
||||
local Fields = require("@formats/codecs/model/Vrml/Fields")
|
||||
local Err = require("@formats/core/Err")
|
||||
|
||||
local t = T.suite("vrml codec")
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ lexer / parser
|
||||
do
|
||||
local g = assert(Vrml.parse([[#VRML V2.0 utf8
|
||||
# a comment, then commas as blanks
|
||||
DEF A Transform { translation 1,2,3 # trailing comment
|
||||
children [ Shape { geometry IndexedFaceSet { coordIndex [ 0, 1, 2, -1 ] coord Coordinate { point [ 0 0 0, 1e1 0 0, .5 -2.5E-1 +3 ] } } } ] }
|
||||
WorldInfo { title "say \"hi\" \\ there" info [ "a" "b" ] }
|
||||
DEF P PixelTexture { image 1 1 4 0xFF8000FF }
|
||||
USE A
|
||||
]]))
|
||||
t:eq(#g.roots, 4, "statements")
|
||||
t:eq(g.header.version, "2.0", "header")
|
||||
local a = g.roots[1]
|
||||
t:eq(a.def, "A", "DEF")
|
||||
t:eq(table.concat(Fields.vec(a, "translation", { 0, 0, 0 }), " "), "1 2 3", "commas are blanks")
|
||||
local coord = Fields.node(Fields.node(Fields.nodes(a, "children")[1], "geometry"), "coord")
|
||||
t:eq(table.concat(Fields.nums(coord, "point") :: { number }, " "), "0 0 0 10 0 0 0.5 -0.25 3", "exponents, leading dot, plus sign")
|
||||
t:eq(Fields.str(g.roots[2], "title"), 'say "hi" \\ there', "string escapes")
|
||||
t:eq(#Fields.strs(g.roots[2], "info"), 2, "MFString")
|
||||
t:eq((Fields.nums(g.roots[3], "image") :: { number })[4], 0xFF8000FF, "hex numbers")
|
||||
t:check(g.roots[4] == a, "USE is the same node")
|
||||
end
|
||||
|
||||
do
|
||||
local g = assert(Vrml.parse([[#VRML V2.0 utf8
|
||||
PROTO Outer [ field SFFloat size 1 exposedField SFColor tint 1 1 1 eventIn SFBool flip ] {
|
||||
PROTO Inner [ field SFFloat r 1 ] { Sphere { radius IS r } }
|
||||
DEF G Group { children [ Shape { geometry Inner { r IS size } appearance Appearance { material DEF M Material { diffuseColor IS tint } } } ] }
|
||||
DEF Clock TimeSensor { }
|
||||
DEF I PositionInterpolator { key [ 0 1 ] keyValue [ 0 0 0 1 1 1 ] }
|
||||
ROUTE Clock.fraction_changed TO I.set_fraction
|
||||
ROUTE I.value_changed TO G.set_translation
|
||||
}
|
||||
EXTERNPROTO Far [ field SFFloat x ] [ "far.wrl#Far" "urn:far" ]
|
||||
DEF O1 Outer { size 3 tint 0 1 0 }
|
||||
Outer { }
|
||||
Far { x 2 }
|
||||
DEF S Script { field SFNode target USE O1 eventIn SFTime touch url "javascript: function touch() {}" }
|
||||
IMPORT inl.Thing AS Thing
|
||||
EXPORT S AS Exported
|
||||
]]))
|
||||
t:eq(#g.roots, 4, "proto instances, extern instance, script")
|
||||
local o1, o2 = g.roots[1], g.roots[2]
|
||||
t:eq(o1.type, "Group", "an instance is its body's first node")
|
||||
t:eq(o1.def, "O1", "the instance's DEF name")
|
||||
local function radius(o: any): number
|
||||
local sh = Fields.nodes(o, "children")[1]
|
||||
return Fields.num(Fields.node(sh, "geometry"), "radius", -1)
|
||||
end
|
||||
t:eq(radius(o1), 3, "IS through a nested PROTO (instance value)")
|
||||
t:eq(radius(o2), 1, "IS default")
|
||||
local mat = Fields.node(Fields.node(Fields.nodes(o1, "children")[1], "appearance"), "material")
|
||||
t:eq(table.concat(Fields.vec(mat, "diffuseColor", { 9, 9, 9 }), " "), "0 1 0", "IS on an SFColor")
|
||||
local mat2 = Fields.node(Fields.node(Fields.nodes(o2, "children")[1], "appearance"), "material")
|
||||
t:check(mat ~= mat2, "each instance has its own copy of the body")
|
||||
t:eq(#g.routes, 4, "the body's ROUTEs, per instance")
|
||||
t:check(g.routes[2].to == o1 or g.routes[2].to.fields == o1.fields, "routes point at the instance's nodes")
|
||||
t:eq(g.roots[3].type, "Far", "EXTERNPROTO instance keeps its type")
|
||||
t:check(g.roots[3].extern == true, "EXTERNPROTO instance flagged")
|
||||
t:eq(Fields.node(g.roots[4], "target"), o1, "Script interface field with a USE value")
|
||||
end
|
||||
|
||||
do
|
||||
local g = assert(Vrml.parse("#X3D V3.3 utf8\nPROFILE Immersive\nCOMPONENT CADGeometry:2\nMETA \"author\" \"me\"\nUNIT length foot 0.3048\nGroup { }\n"))
|
||||
t:check(g.header.x3d and g.header.version == "3.3" and g.header.profile == "Immersive", "X3D header, PROFILE")
|
||||
t:eq(g.components[1], "CADGeometry:2", "COMPONENT")
|
||||
t:eq(g.meta.author, "me", "META")
|
||||
t:eq(g.units.length.factor, 0.3048, "UNIT")
|
||||
local s = assert(Vrml.build(g))
|
||||
t:eq(s.meta.unit, 0.3048, "UNIT length -> meta.unit")
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ XML adapter
|
||||
do
|
||||
local g = assert(Vrml.graph([[<?xml version="1.0"?>
|
||||
<X3D profile="Interchange" version="4.0"><head><component name="Shaders" level="1"/></head><Scene>
|
||||
<ProtoDeclare name="Ball"><ProtoInterface><field name="r" type="SFFloat" accessType="initializeOnly" value="2"/></ProtoInterface>
|
||||
<ProtoBody><Shape><Sphere><IS><connect nodeField="radius" protoField="r"/></IS></Sphere></Shape></ProtoBody></ProtoDeclare>
|
||||
<Group DEF="G"><ProtoInstance name="Ball"><fieldValue name="r" value="5"/></ProtoInstance>
|
||||
<ProtoInstance name="Ball" containerField="children"/></Group>
|
||||
<Shape><Appearance><ImageTexture url='"a.png" "b.png"'/></Appearance><Box containerField="geometry" size="1, 2, 3" solid="FALSE"/></Shape>
|
||||
<Transform USE="G"/>
|
||||
</Scene></X3D>]]))
|
||||
t:check(g.header.xml and g.header.version == "4.0", "XML header")
|
||||
t:eq(g.components[1], "Shaders:1", "head component")
|
||||
local grp = g.roots[1]
|
||||
local kids = Fields.nodes(grp, "children")
|
||||
t:eq(#kids, 2, "ProtoInstances in children")
|
||||
t:eq(Fields.num(Fields.node(kids[1], "geometry"), "radius", -1), 5, "fieldValue")
|
||||
t:eq(Fields.num(Fields.node(kids[2], "geometry"), "radius", -1), 2, "ProtoInterface default")
|
||||
local shape = g.roots[2]
|
||||
local box = Fields.node(shape, "geometry")
|
||||
t:eq(table.concat(Fields.vec(box, "size", { 9, 9, 9 }), " "), "1 2 3", "numbers with commas")
|
||||
t:eq(Fields.bool(box, "solid", true), false, "XML booleans (any case)")
|
||||
local tex = Fields.node(Fields.node(shape, "appearance"), "texture")
|
||||
local urls = Fields.strs(tex, "url")
|
||||
t:check(#urls == 2 and urls[2] == "b.png", "quoted MFString")
|
||||
t:eq(g.roots[3], grp, "USE in XML")
|
||||
local single = { type = "ImageTexture", fields = { url = "plain name.png" } }
|
||||
t:eq(Fields.strs(single, "url")[1], "plain name.png", "unquoted single MFString")
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ errors
|
||||
do
|
||||
local function code(text: string): string?
|
||||
local _, e = Vrml.graph(text)
|
||||
return e and e.code
|
||||
end
|
||||
t:eq(code("#VRML V2.0 utf8\nShape { geometry Box { }"), Err.CORRUPT, "unclosed node")
|
||||
t:eq(code("#VRML V2.0 utf8\nShape { geometry USE X }"), Err.CORRUPT, "undefined USE")
|
||||
t:eq(code("#VRML V2.0 utf8\nROUTE A.b TO"), Err.CORRUPT, "bad ROUTE")
|
||||
t:eq(code("#VRML V2.0 utf8\nWorldInfo { title \"x }"), Err.TRUNCATED, "unterminated string")
|
||||
t:eq(code("#VRML V1.0 ascii\n"), Err.UNSUPPORTED, "VRML 1.0")
|
||||
t:eq(code("\224\0\0\1rest"), Err.UNSUPPORTED, "Fast Infoset X3D")
|
||||
t:eq(code("#VRML V2.0 utf8\n" .. string.rep("Group { children ", 250)), Err.LIMIT, "nesting limit")
|
||||
t:eq(code("<X3D><Scene><Group></Scene></X3D>"), Err.CORRUPT, "bad XML")
|
||||
local _, e = Vrml.encode({} :: any)
|
||||
t:eq(e and e.code, Err.ARG, "encode junk")
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ fuzz
|
||||
do
|
||||
local samples = {
|
||||
"#VRML V2.0 utf8\nPROTO P [ field SFFloat a 1 ] { Box { size IS a IS a } }\nDEF X P { a 2 } USE X ROUTE X.a TO X.b\n",
|
||||
"#X3D V3.3 utf8\nPROFILE Full\nShape { geometry Extrusion { spine [ 0 0 0 0 1 0 1 1 1 ] crossSection [ 0 0 1 0 1 1 0 0 ] } }\n",
|
||||
"<X3D><Scene><ProtoDeclare name='A'><ProtoBody><Box/></ProtoBody></ProtoDeclare><ProtoInstance name='A'/><Shape><IndexedFaceSet coordIndex='0 1 2 -1 2 3 0'><Coordinate point='0 0 0 1 0 0 1 1 0 0 1 0'/></IndexedFaceSet></Shape></Scene></X3D>",
|
||||
}
|
||||
t:fuzz("graph + build", function(b: buffer)
|
||||
local g = Vrml.graph(b)
|
||||
if g then
|
||||
return Vrml.build(g)
|
||||
end
|
||||
return nil
|
||||
end, samples, { mutations = 400 })
|
||||
end
|
||||
|
||||
t:done()
|
||||
Reference in new issue
Block a user