ws-audio2: merge master (c5f2777)
This commit is contained in:
commit
d7697a8688
126 files changed
+25832
-1541
No files matched your search
@@ -169,4 +169,23 @@ do
|
||||
end, samples, { mutations = 3, garbage = 300 })
|
||||
end
|
||||
|
||||
-- peek: the first bytes of a gzip / zlib / raw stream without inflating the rest
|
||||
do
|
||||
local src = buffer.create(100000)
|
||||
for i = 0, 99999 do
|
||||
buffer.writeu8(src, i, (i * 7 + (i // 97)) % 251)
|
||||
end
|
||||
local want = buffer.readstring(src, 0, 1000)
|
||||
for _, kind in { "gzip", "zlibCompress", "compress" } do
|
||||
local z = assert((Deflate :: any)[kind](src)) :: buffer
|
||||
local head = assert(Deflate.peek(z, nil, nil, 1000))
|
||||
t:check(buffer.len(head) <= 1000 and buffer.len(head) >= 1000 - 258, "peek " .. kind .. " size")
|
||||
t:eq(buffer.tostring(head), string.sub(want, 1, buffer.len(head)), "peek " .. kind .. " bytes")
|
||||
end
|
||||
local small = assert(Deflate.peek(assert(Deflate.gzip(buffer.fromstring("hello"))), nil, nil, 1000))
|
||||
t:eq(buffer.tostring(small), "hello", "peek a short stream")
|
||||
local _, e = Deflate.peek(buffer.fromstring("\31\139\8\0\0\0\0\0\0\3\255\255\255\255\255\255\255\255"), nil, nil, 10)
|
||||
t:check(e ~= nil, "peek bad data")
|
||||
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()
|
||||
@@ -0,0 +1,101 @@
|
||||
-- codecs/compress/Lz4 tests: every reference block (python-lz4 fast at accelerations 1 / 4 / 16, HC 4 / 9 / 12)
|
||||
-- decodes to its corpus, with and without the size; our encoder's blocks decode with the reference (fixture verdicts)
|
||||
-- and round trip here; linked blocks through decodeInto's history; error codes; truncated / mutated / random blocks
|
||||
-- never throw. Fixtures: tools/fixtures/lz4.py.
|
||||
local T = require("@tests/T")
|
||||
local Lz4 = require("@formats/codecs/compress/Lz4")
|
||||
local Err = require("@formats/core/Err")
|
||||
local Fx = require("@tests/gen/lz4")
|
||||
local C = require("@tests/gen/lz4_corpora")
|
||||
|
||||
local t = T.suite("lz4")
|
||||
|
||||
local nbytes = 0
|
||||
for _, c in Fx.cases do
|
||||
local want = C[c.corpus]
|
||||
local out, err = Lz4.decompress(c.data, nil, nil, #want)
|
||||
t:check(out ~= nil and buffer.tostring(out) == want, c.name .. ": " .. tostring(err))
|
||||
if #want > 0 then
|
||||
local grown = Lz4.decompress(c.data)
|
||||
t:check(grown ~= nil and buffer.tostring(grown) == want, c.name .. ": without the size")
|
||||
end
|
||||
nbytes += #want
|
||||
end
|
||||
t:note("%d reference blocks, %.1f MB decoded", #Fx.cases, nbytes / 1e6)
|
||||
|
||||
for _, v in Fx.ours do
|
||||
t:check(v.ok, v.name .. ": the reference decodes our block")
|
||||
end
|
||||
for name, data in C do
|
||||
local b = Lz4.compress(data) :: buffer
|
||||
local back = Lz4.decompress(b, nil, nil, #data)
|
||||
t:check(back ~= nil and buffer.tostring(back) == data, name .. ": round trip")
|
||||
if name == "text" or name == "zeros" or name == "runs" then
|
||||
t:check(buffer.len(b) < #data / 2, name .. ": compresses")
|
||||
end
|
||||
t:check(buffer.len(b) <= Lz4.bound(#data), name .. ": within bound")
|
||||
end
|
||||
|
||||
-- at an offset, and linked blocks: the second block refers into the first one's output
|
||||
do
|
||||
local text = C.text
|
||||
local a, b2 = string.sub(text, 1, 70000), string.sub(text, 70001, 140000)
|
||||
local pa = Lz4.compress(a) :: buffer
|
||||
-- a block of b2 that copies the first 40 bytes of b2's region from a (a match reaching back across blocks)
|
||||
local big = buffer.create(buffer.len(pa) + 20)
|
||||
buffer.copy(big, 9, pa)
|
||||
local out = Lz4.decompress(big, 9, buffer.len(pa), #a)
|
||||
t:check(out ~= nil and buffer.tostring(out) == a, "at an offset")
|
||||
local hand = buffer.create(3 + 1 + 2 + 1)
|
||||
-- token: 0 literals, match length 15 + ext; offset = 100 back into the previous block
|
||||
buffer.writeu8(hand, 0, 0x0F)
|
||||
buffer.writeu16(hand, 1, 100)
|
||||
buffer.writeu8(hand, 3, 20) -- match length 4 + 15 + 20 = 39
|
||||
buffer.writeu8(hand, 4, 0x10) -- the last sequence: one literal
|
||||
buffer.writeu8(hand, 5, 33)
|
||||
local dst = buffer.create(#a + 40)
|
||||
buffer.writestring(dst, 0, a)
|
||||
local e, msg = Lz4.decodeInto(hand, 0, 6, dst, #a, #a + 40, 0)
|
||||
t:check(e == #a + 40, "linked block: " .. tostring(msg))
|
||||
t:eq(buffer.readstring(dst, #a, 39), string.sub(a, #a - 99, #a - 61), "linked block: match from the history")
|
||||
local e2, msg2 = Lz4.decodeInto(hand, 0, 6, dst, #a, #a + 40)
|
||||
t:check(e2 == nil and string.find(msg2 or "", "offset") ~= nil, "without history the match is corrupt")
|
||||
t:check(b2 ~= nil, "second half")
|
||||
end
|
||||
|
||||
-- errors
|
||||
do
|
||||
local _, e1 = Lz4.decompress("")
|
||||
t:check(e1 and e1.code == Err.TRUNCATED, "empty: " .. tostring(e1))
|
||||
local _, e2 = Lz4.decompress("\x50ab")
|
||||
t:check(e2 and e2.code == Err.TRUNCATED, "literals past the end: " .. tostring(e2))
|
||||
local _, e3 = Lz4.decompress("\x14abcd\x09\x00\x10z")
|
||||
t:check(e3 and e3.code == Err.CORRUPT, "offset before the start: " .. tostring(e3))
|
||||
local _, e4 = Lz4.decompress("\x10a", nil, nil, 5)
|
||||
t:check(e4 and e4.code == Err.CORRUPT, "shorter than outSize: " .. tostring(e4))
|
||||
local _, e5 = Lz4.decompress("\x50abcde", nil, nil, 2)
|
||||
t:check(e5 and e5.code == Err.LIMIT, "longer than outSize: " .. tostring(e5))
|
||||
local _, e6 = Lz4.decompress(C.zeros and (Lz4.compress(C.zeros) :: buffer), nil, nil, nil, { maxBytes = 1000 })
|
||||
t:check(e6 and e6.code == Err.LIMIT, "maxBytes: " .. tostring(e6))
|
||||
local ok, r1, r2 = pcall(Lz4.decompress, 5 :: any)
|
||||
t:check(ok and r1 == nil and r2.code == Err.ARG, "a non-buffer argument returns Err.ARG")
|
||||
local _, e7 = Lz4.decompress("\x10a", nil, nil, -1)
|
||||
t:check(e7 and e7.code == Err.ARG, "bad outSize")
|
||||
end
|
||||
|
||||
do
|
||||
local samples = {}
|
||||
for i, c in Fx.cases do
|
||||
if i % 4 == 0 and #c.data > 8 and #c.data < 30000 then
|
||||
table.insert(samples, c.data)
|
||||
end
|
||||
end
|
||||
t:fuzz("decompress", function(b: buffer)
|
||||
return Lz4.decompress(b, nil, nil, nil, { maxBytes = 1e6 })
|
||||
end, samples, { mutations = 30, garbage = 2000 })
|
||||
t:fuzz("decompress (sized)", function(b: buffer)
|
||||
return Lz4.decompress(b, nil, nil, 70000)
|
||||
end, samples, { mutations = 30, garbage = 500, seed = 5 })
|
||||
end
|
||||
|
||||
t:done()
|
||||
@@ -0,0 +1,155 @@
|
||||
-- codecs/model/Minecraft tests: packed index arrays (spanning and not, 1..16 bits, against a bit-by-bit reference),
|
||||
-- Sponge varints, nibble arrays, YZX reordering, block-state strings and their NBT form, every legacy id / data pair
|
||||
-- through legacyState / legacyId, colours (air, families, fallbacks), voxels / blocksOf and nearest-colour blocks.
|
||||
-- The formats using it: tests/model/schematic.luau, tests/model/mca.luau.
|
||||
local T = require("@tests/T")
|
||||
local Mc = require("@formats/codecs/model/Minecraft")
|
||||
local Types = require("@formats/Types")
|
||||
|
||||
local t = T.suite("minecraft")
|
||||
local P = Mc.Packed
|
||||
local rand = T.rng(3)
|
||||
|
||||
-- 1. packed arrays against a reference that walks the bits one at a time
|
||||
local function refBit(longs: buffer, p: number): number
|
||||
return bit32.extract(buffer.readu8(longs, p // 8), p % 8, 1)
|
||||
end
|
||||
for bits = 1, 16 do
|
||||
for _, spanning in { true, false } do
|
||||
local count = 100 + rand(300)
|
||||
local max = math.min(2 ^ bits, 70000)
|
||||
local idx = buffer.create(count * 2)
|
||||
for i = 0, count - 1 do
|
||||
buffer.writeu16(idx, i * 2, rand(max))
|
||||
end
|
||||
local longs = P.pack(idx, count, bits, spanning)
|
||||
t:eq(buffer.len(longs), P.longsNeeded(count, bits, spanning) * 8, bits .. " bits: size")
|
||||
local per = 64 // bits
|
||||
local bad = 0
|
||||
for i = 0, count - 1 do
|
||||
local p = if spanning then i * bits else (i // per) * 64 + (i % per) * bits
|
||||
local v = 0
|
||||
for k = 0, bits - 1 do
|
||||
v += refBit(longs, p + k) * 2 ^ k
|
||||
end
|
||||
if v ~= buffer.readu16(idx, i * 2) then
|
||||
bad += 1
|
||||
end
|
||||
end
|
||||
t:eq(bad, 0, bits .. " bits " .. tostring(spanning) .. ": layout")
|
||||
local back = buffer.create(count * 2)
|
||||
local ok, e = P.unpack(longs, bits, count, spanning, back, max)
|
||||
t:check(ok, tostring(e))
|
||||
t:eq(buffer.tostring(back), buffer.tostring(idx), bits .. " bits " .. tostring(spanning) .. ": round trip")
|
||||
end
|
||||
end
|
||||
do
|
||||
local out = buffer.create(40)
|
||||
t:check(not P.unpack(buffer.create(8), 4, 20, false, out, 16), "too few longs")
|
||||
local l = P.pack(buffer.fromstring("\5\0"), 1, 4, false)
|
||||
t:check(not P.unpack(l, 4, 1, false, out, 3), "index past the palette")
|
||||
t:eq(P.bitsFor(17, 4), 5, "bitsFor")
|
||||
t:eq(P.bitsFor(1, 2), 2, "bitsFor minimum")
|
||||
end
|
||||
|
||||
-- 2. varints, nibbles, YZX
|
||||
do
|
||||
local idx = buffer.create(400)
|
||||
for i = 0, 199 do
|
||||
buffer.writeu16(idx, i * 2, (i * 997) % 20000)
|
||||
end
|
||||
local v = P.toVarints(idx, 200)
|
||||
local back = buffer.create(400)
|
||||
t:check(P.varints(v, 0, buffer.len(v), 200, back, 20000), "varints")
|
||||
t:eq(buffer.tostring(back), buffer.tostring(idx), "varints round trip")
|
||||
t:check(not P.varints(v, 0, buffer.len(v) - 1, 200, back, 20000), "varints truncated")
|
||||
local nib = buffer.create(2)
|
||||
P.setNibble(nib, 0, 0, 0xA, true)
|
||||
P.setNibble(nib, 0, 1, 0x3, true)
|
||||
P.setNibble(nib, 0, 2, 0x7, false)
|
||||
t:eq(buffer.readu8(nib, 0), 0xA3, "high nibble first")
|
||||
t:eq(buffer.readu8(nib, 1), 0x07, "low nibble first")
|
||||
t:eq(P.nibble(nib, 0, 1, true), 3, "read nibble")
|
||||
local sx, sy, sz = 3, 4, 5
|
||||
local yzx = buffer.create(sx * sy * sz * 2)
|
||||
for y = 0, sy - 1 do
|
||||
for z = 0, sz - 1 do
|
||||
for x = 0, sx - 1 do
|
||||
buffer.writeu16(yzx, (x + z * sx + y * sx * sz) * 2, x * 100 + y * 10 + z)
|
||||
end
|
||||
end
|
||||
end
|
||||
local vox = P.fromYzx(yzx, sx, sy, sz)
|
||||
t:eq(buffer.readu16(vox, (2 + sx * (3 + sy * 4)) * 2), 234, "fromYzx")
|
||||
t:eq(buffer.tostring(P.toYzx(vox, sx, sy, sz)), buffer.tostring(yzx), "toYzx")
|
||||
end
|
||||
|
||||
-- 3. states
|
||||
do
|
||||
local name, props = Mc.parseState("oak_log[axis=y, foo = bar]")
|
||||
t:eq(name, "minecraft:oak_log", "namespace added")
|
||||
t:eq(#props, 2, "props")
|
||||
t:eq(Mc.stateString(name, props), "minecraft:oak_log[axis=y,foo=bar]", "stateString")
|
||||
local c = Mc.stateToNbt("minecraft:chest[facing=north,type=single]")
|
||||
t:eq(table.concat(Types.keys(c.Properties), ","), "facing,type", "NBT property order")
|
||||
t:eq(Mc.stateFromNbt(c), "minecraft:chest[facing=north,type=single]", "stateFromNbt")
|
||||
t:eq(Mc.baseName("minecraft:stone"), "stone", "baseName")
|
||||
t:check(Mc.isAir("minecraft:cave_air") and not Mc.isAir("minecraft:stone"), "isAir")
|
||||
end
|
||||
|
||||
-- 4. legacy ids: every id / data pair reads back exactly
|
||||
do
|
||||
local bad = 0
|
||||
for id = 0, 255 do
|
||||
for d = 0, 15 do
|
||||
local s = Mc.legacyState(id, d)
|
||||
local i2, d2 = Mc.legacyId(s)
|
||||
if i2 ~= id or d2 ~= d then
|
||||
bad += 1
|
||||
end
|
||||
end
|
||||
end
|
||||
t:eq(bad, 0, "legacy round trip")
|
||||
t:eq(Mc.legacyState(35, 14), "minecraft:red_wool", "wool 14")
|
||||
t:eq(Mc.legacyState(5, 2), "minecraft:birch_planks", "planks 2")
|
||||
t:eq(Mc.legacyState(17, 5), "minecraft:spruce_log[data=5]", "log axis bits kept")
|
||||
t:eq(select(1, Mc.legacyId("minecraft:white_concrete")), 251, "modern name -> legacy id")
|
||||
end
|
||||
|
||||
-- 5. colours
|
||||
do
|
||||
t:eq(Mc.color("minecraft:air"), nil, "air")
|
||||
local function hex(s: string): string
|
||||
local c = Mc.color(s) :: { number }
|
||||
return string.format("%02x%02x%02x%02x", c[1], c[2], c[3], c[4])
|
||||
end
|
||||
t:eq(hex("minecraft:stone"), "7d7d7dff", "exact")
|
||||
t:eq(hex("minecraft:stone_brick_stairs[facing=east]"), hex("minecraft:stone_bricks"), "stairs -> base")
|
||||
t:eq(hex("minecraft:red_concrete"), "8e2020ff", "dye family")
|
||||
t:eq(hex("minecraft:dark_oak_log"), "3e2f17ff", "wood bark")
|
||||
t:eq(string.sub(hex("minecraft:red_stained_glass"), 7, 8), "80", "stained glass translucent")
|
||||
t:eq(hex("minecraft:wool[data=14]"), hex("minecraft:red_wool"), "legacy data colour")
|
||||
t:eq(hex("mod:something_odd"), "909090ff", "fallback")
|
||||
end
|
||||
|
||||
-- 6. voxels / blocksOf
|
||||
do
|
||||
local states = { "minecraft:air", "minecraft:stone", "minecraft:red_wool", "minecraft:stone" }
|
||||
local idx = buffer.create(8 * 2)
|
||||
for i = 0, 7 do
|
||||
buffer.writeu16(idx, i * 2, i % 4)
|
||||
end
|
||||
local v = Mc.voxels(2, 2, 2, states, idx)
|
||||
t:eq(buffer.readu8(v.data, 0), 0, "air -> 0")
|
||||
t:eq(buffer.readu8(v.data, 1), buffer.readu8(v.data, 3), "same colour, same index")
|
||||
t:check(buffer.readu8(v.data, 2) ~= buffer.readu8(v.data, 1), "different colours")
|
||||
local s2, i2 = Mc.blocksOf(v)
|
||||
t:check(s2 == states and i2 == idx, "blocksOf keeps the blocks")
|
||||
local g = Types.Voxels.new(2, 1, 1)
|
||||
g.palette[7] = { 240, 118, 19, 255 }
|
||||
buffer.writeu8(g.data, 1, 7)
|
||||
local s3, i3 = Mc.blocksOf(g)
|
||||
t:eq((s3 :: { string })[buffer.readu16(i3, 2) + 1], "minecraft:orange_wool", "nearest block by colour")
|
||||
t:eq((s3 :: { string })[buffer.readu16(i3, 0) + 1], "minecraft:air", "empty -> air")
|
||||
end
|
||||
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,256 @@
|
||||
-- codecs/text/Nbt tests: random trees written by Python's nbtlib (Java big-endian, Bedrock little-endian, gzip, zlib,
|
||||
-- the level.dat header) decode to nbtlib's typed values (a canonical dump), re-encode to nbtlib's exact bytes in both
|
||||
-- byte orders, nbtlib's SNBT (indented and compact) parses to the same values and our SNBT reads back exactly;
|
||||
-- Bedrock network varint NBT; modified UTF-8; long extremes; SNBT grammar details; peek; errors; the fuzz pass.
|
||||
-- Fixtures: tools/fixtures/nbt.py.
|
||||
local T = require("@tests/T")
|
||||
local Nbt = require("@formats/codecs/text/Nbt")
|
||||
local Types = require("@formats/Types")
|
||||
local Err = require("@formats/core/Err")
|
||||
local Fx = require("@tests/gen/nbt")
|
||||
|
||||
local t = T.suite("nbt")
|
||||
|
||||
local function hexOf(b: buffer, o: number, n: number, rev: boolean): string
|
||||
local parts = {}
|
||||
for i = 0, n - 1 do
|
||||
parts[i + 1] = string.format("%02x", buffer.readu8(b, if rev then o + n - 1 - i else o + i))
|
||||
end
|
||||
return table.concat(parts)
|
||||
end
|
||||
|
||||
-- the canonical dump tools/fixtures/nbt.py writes
|
||||
local function canon(v: any): string
|
||||
local ty = Nbt.typeOf(v)
|
||||
local x = if Nbt.isTag(v) then v.value else v
|
||||
if ty == "byte" or ty == "short" or ty == "int" or ty == "long" then
|
||||
local n = if type(x) == "boolean" then (if x then 1 else 0) else x
|
||||
return string.sub(ty :: string, 1, 1) .. (if type(n) == "string" then n else string.format("%d", n))
|
||||
elseif ty == "float" then
|
||||
local b = buffer.create(4)
|
||||
buffer.writef32(b, 0, x)
|
||||
return "f" .. hexOf(b, 0, 4, true)
|
||||
elseif ty == "double" then
|
||||
local b = buffer.create(8)
|
||||
buffer.writef64(b, 0, x)
|
||||
return "d" .. hexOf(b, 0, 8, true)
|
||||
elseif ty == "string" then
|
||||
return "S" .. #x .. ":" .. x
|
||||
elseif ty == "byteArray" then
|
||||
return "B" .. hexOf(x, 0, buffer.len(x), false)
|
||||
elseif ty == "intArray" then
|
||||
local p = {}
|
||||
for i, n in x do
|
||||
p[i] = string.format("%d", n)
|
||||
end
|
||||
return "I[" .. table.concat(p, ",") .. "]"
|
||||
elseif ty == "longArray" then
|
||||
local p = {}
|
||||
for i, n in Nbt.longs(v) do
|
||||
p[i] = if type(n) == "string" then n else string.format("%d", n)
|
||||
end
|
||||
return "L[" .. table.concat(p, ",") .. "]"
|
||||
elseif ty == "list" then
|
||||
local p = {}
|
||||
for i, e in v do
|
||||
p[i] = canon(e)
|
||||
end
|
||||
return "[" .. (Nbt.listType(v) or "?") .. "|" .. table.concat(p, ",") .. "]"
|
||||
end
|
||||
local p = {}
|
||||
for _, k in Types.keys(v) do
|
||||
table.insert(p, #k .. ":" .. k .. "=" .. canon(v[k]))
|
||||
end
|
||||
return "{" .. table.concat(p, ";") .. "}"
|
||||
end
|
||||
|
||||
local function same(a: buffer, b: string): boolean
|
||||
return buffer.tostring(a) == b
|
||||
end
|
||||
|
||||
-- 1. nbtlib trees
|
||||
for _, c in Fx.cases do
|
||||
local v, info = Nbt.decode(c.raw)
|
||||
t:check(v ~= nil, c.name .. ": decode " .. tostring(info))
|
||||
t:eq(canon(v), c.canon, c.name .. ": value")
|
||||
t:eq(info.name, c.root, c.name .. ": root name")
|
||||
t:check(info.endian == "big" and info.compression == "none", c.name .. ": info")
|
||||
local out = Nbt.encode(v) :: buffer
|
||||
t:check(out and same(out, c.raw), c.name .. ": same bytes (big-endian)")
|
||||
local le = Nbt.encode(v, { endian = "little" }) :: buffer
|
||||
t:check(le and same(le, c.little), c.name .. ": same bytes (little-endian)")
|
||||
for _, k in { "little", "gzip", "zlib", "header" } do
|
||||
local w, wi = Nbt.decode(c[k])
|
||||
t:check(w ~= nil, c.name .. ": decode " .. k .. " " .. tostring(wi))
|
||||
t:eq(canon(w), c.canon, c.name .. ": " .. k .. " value")
|
||||
if #c.raw > 4 then -- an empty compound with an empty name reads the same either way
|
||||
t:eq(wi.endian, if k == "little" or k == "header" then "little" else "big", c.name .. ": " .. k .. " endian")
|
||||
end
|
||||
t:eq(wi.compression, if k == "gzip" or k == "zlib" then k else "none", c.name .. ": " .. k .. " compression")
|
||||
-- written back the same way it was read
|
||||
local again = Nbt.encode(w) :: buffer
|
||||
local w2, wi2 = Nbt.decode(again)
|
||||
t:eq(canon(w2), c.canon, c.name .. ": " .. k .. " round trip")
|
||||
t:eq(wi2.compression, wi.compression, c.name .. ": " .. k .. " keeps compression")
|
||||
t:eq(wi2.header, wi.header, c.name .. ": " .. k .. " keeps the header")
|
||||
if k == "header" then
|
||||
t:eq(wi.header, c.version, c.name .. ": header version")
|
||||
t:check(same(again, c.header), c.name .. ": header same bytes")
|
||||
end
|
||||
end
|
||||
-- SNBT: nbtlib's text parses to the same values (an empty list's type is not in the text); ours reads back
|
||||
-- exactly (both forms)
|
||||
local function untyped(s: string): string
|
||||
return (string.gsub(s, "%[%a+|%]", "[|]"))
|
||||
end
|
||||
for _, k in { "snbt", "compact" } do
|
||||
local s, e = Nbt.parse(c[k])
|
||||
t:check(s ~= nil, c.name .. ": parse " .. k .. " " .. tostring(e))
|
||||
t:eq(untyped(canon(s)), untyped(c.canon), c.name .. ": " .. k .. " value")
|
||||
end
|
||||
for _, o in { {}, { indent = 2 }, { quote = "'" } } do
|
||||
local text = Nbt.print(v, o) :: string
|
||||
local back, e = Nbt.parse(text)
|
||||
t:check(back ~= nil, c.name .. ": our SNBT parses " .. tostring(e))
|
||||
t:eq(untyped(canon(back)), untyped(c.canon), c.name .. ": our SNBT round trip")
|
||||
end
|
||||
end
|
||||
|
||||
-- 2. Bedrock network NBT
|
||||
for i, c in Fx.net do
|
||||
local v, info = Nbt.decode(c.data, { varint = true })
|
||||
t:check(v ~= nil, "net " .. i .. ": " .. tostring(info))
|
||||
t:eq(canon(v), c.canon, "net " .. i .. ": value")
|
||||
t:check(same(Nbt.encode(v, { varint = true }) :: buffer, c.data), "net " .. i .. ": same bytes")
|
||||
end
|
||||
|
||||
-- 3. details
|
||||
do
|
||||
-- longs: exact past 2^53, both directions
|
||||
for _, s in { "9223372036854775807", "-9223372036854775808", "9007199254740993", "-9007199254740993", "4294967296", "-1" } do
|
||||
local hi, lo = Nbt.toHiLo(s)
|
||||
t:check(hi ~= nil, "toHiLo " .. s)
|
||||
t:eq(tostring(Nbt.fromHiLo(hi :: number, lo :: number)), s, "long text " .. s)
|
||||
end
|
||||
t:eq(Nbt.toHiLo("9223372036854775808"), nil, "long overflow")
|
||||
t:eq(Nbt.long("12").value, 12, "small long text becomes a number")
|
||||
-- modified UTF-8: NUL as C0 80 and U+1F600 as a surrogate pair in Java files, plain UTF-8 in Bedrock ones
|
||||
local v = Nbt.compound({ s = "a\0b\u{1F600}" }, { "s" })
|
||||
local big = Nbt.write(v) :: buffer
|
||||
t:check(string.find(buffer.tostring(big), "a\192\128b\237\160\189\237\184\128", 1, true) ~= nil, "modified UTF-8 out")
|
||||
t:eq((Nbt.read(big)).s, "a\0b\u{1F600}", "modified UTF-8 in")
|
||||
local little = Nbt.write(v, { endian = "little" }) :: buffer
|
||||
t:check(string.find(buffer.tostring(little), "a\0b\u{1F600}", 1, true) ~= nil, "Bedrock strings are UTF-8")
|
||||
-- lenient inputs: plain numbers, booleans, plain tables
|
||||
local p = Nbt.read(Nbt.write({ a = 1.5, b = true, c = { 1, 2 }, d = {}, e = 2 ^ 40 }) :: buffer)
|
||||
t:check(p.a.type == "double" and p.b.type == "byte" and p.b.value == 1, "plain number / boolean")
|
||||
t:check(Nbt.listType(p.c) == "int" and Nbt.listType(p.d) == "end" and p.e.type == "double", "plain lists")
|
||||
-- mixed lists are wrapped in compounds, as Minecraft 1.21.5 does
|
||||
local m = Nbt.read(Nbt.write({ l = { 1, "x" } }) :: buffer)
|
||||
t:check(Nbt.listType(m.l) == "compound" and m.l[1][""] == 1 and m.l[2][""] == "x", "heterogeneous list")
|
||||
-- empty typed list keeps its type
|
||||
local et = Nbt.read(Nbt.write({ l = Nbt.list("compound") }) :: buffer)
|
||||
t:eq(Nbt.listType(et.l), "compound", "empty typed list")
|
||||
-- tostring of tags is SNBT; == compares type and value
|
||||
t:eq(tostring(Nbt.byte(3)), "3b", "tostring byte")
|
||||
t:eq(tostring(Nbt.float(0.1)), "0.1f", "tostring float (shortest)")
|
||||
t:check(Nbt.short(5) == Nbt.short(5) and Nbt.short(5) ~= Nbt.int(5) :: any, "tag equality")
|
||||
-- get
|
||||
local g = assert(Nbt.parse("{Data:{Pos:[1.0d,2.5d,3.0d],Inv:[{id:\"x\"}]}}"))
|
||||
t:eq(Nbt.get(g, "Data.Pos[1]").value, 2.5, "get list element")
|
||||
t:eq(Nbt.get(g, "Data.Inv[0].id"), "x", "get nested")
|
||||
t:eq(Nbt.get(g, "Data.Missing.x"), nil, "get missing")
|
||||
end
|
||||
|
||||
-- 4. SNBT grammar
|
||||
do
|
||||
local cases: { { string } } = {
|
||||
{ "1b", "byte:1" }, { "-5S", "short:-5" }, { "7", "int:7" }, { "7i", "int:7" }, { "1_000", "int:1000" },
|
||||
{ "123L", "long:123" }, { "0x1F", "int:31" }, { "0xFFub", "byte:-1" }, { "0x7Fsb", "byte:127" },
|
||||
{ "0b101", "int:5" }, { "0b", "byte:0" }, { "200ub", "byte:-56" }, { "1.5", "double:1.5" }, { ".5f", "float:0.5" },
|
||||
{ "2e3", "double:2000" }, { "3f", "float:3" }, { "4D", "double:4" }, { "true", "byte:1" }, { "false", "byte:0" },
|
||||
{ "abc", "string:abc" }, { "1.2.3", "string:1.2.3" }, { "'it''", "" }, { "'a\\'b'", "string:a'b" },
|
||||
{ '"\\u00e9\\x41\\n"', "string:\u{e9}A\n" }, { "-Infinityd", "double:-inf" },
|
||||
}
|
||||
for _, c in cases do
|
||||
local v = Nbt.parse(c[1])
|
||||
if c[2] == "" then
|
||||
t:eq(v, nil, "snbt " .. c[1] .. " fails")
|
||||
else
|
||||
local ty = Nbt.typeOf(v) :: string
|
||||
local x = Nbt.number(v)
|
||||
local got = ty .. ":" .. (if ty == "string" then v elseif x == -math.huge then "-inf" else string.format("%.17g", x :: number))
|
||||
t:eq(got, c[2], "snbt " .. c[1])
|
||||
end
|
||||
end
|
||||
local a = assert(Nbt.parse("[B; 1b, -2b, 3]"))
|
||||
t:check(typeof(a) == "buffer" and buffer.len(a) == 3 and buffer.readi8(a, 1) == -2, "byte array")
|
||||
local ia = assert(Nbt.parse("[I;]"))
|
||||
t:check(ia.type == "intArray" and #ia.value == 0, "empty int array")
|
||||
local la = assert(Nbt.parse("[L; -9223372036854775808L, 5L]"))
|
||||
t:eq(Nbt.longs(la)[1], "-9223372036854775808", "long array extreme")
|
||||
local tc = assert(Nbt.parse("{a:1,b:[1,2,],}"))
|
||||
t:check(tc.a == 1 and #tc.b == 2, "trailing commas")
|
||||
t:eq(Nbt.print(assert(Nbt.parse('{"a b":1,c:"say \\"hi\\""}'))), "{\"a b\":1,c:'say \"hi\"'}", "quoting")
|
||||
local _, e1 = Nbt.parse("{a:1")
|
||||
t:check(e1 and e1.code == Err.CORRUPT, "unterminated compound")
|
||||
local _, e2 = Nbt.parse("{a:300b}")
|
||||
t:check(e2 and e2.code == Err.CORRUPT, "byte out of range")
|
||||
local _, e3 = Nbt.parse(string.rep("[", 600) .. string.rep("]", 600))
|
||||
t:check(e3 and e3.code == Err.LIMIT, "depth limit")
|
||||
end
|
||||
|
||||
-- 5. peek
|
||||
do
|
||||
local c = Fx.cases[2]
|
||||
for _, k in { "raw", "gzip", "zlib", "header" } do
|
||||
local p = Nbt.peek(buffer.fromstring(c[k]))
|
||||
t:check(p ~= nil and p.name == c.root and p.rootType == "compound", "peek " .. k)
|
||||
end
|
||||
t:eq(Nbt.peek(buffer.fromstring("hello world")), nil, "peek text")
|
||||
end
|
||||
|
||||
-- 6. errors
|
||||
do
|
||||
local _, e = Nbt.decode("\10\0\0\1\0\1a")
|
||||
t:check(e and e.code == Err.TRUNCATED, "truncated")
|
||||
local _, e2 = Nbt.decode("\13\0\0")
|
||||
t:check(e2 and e2.code == Err.CORRUPT, "bad root type")
|
||||
local _, e3 = Nbt.decode("\10\0\0\9\0\1l\1\127\255\255\255")
|
||||
t:check(e3 and e3.code == Err.TRUNCATED, "huge list length")
|
||||
local deep = {}
|
||||
local cur = deep
|
||||
for _ = 1, 600 do
|
||||
local n = {}
|
||||
cur.x = n
|
||||
cur = n
|
||||
end
|
||||
local _, e4 = Nbt.encode(deep)
|
||||
t:check(e4 and e4.code == Err.ARG, "encode depth")
|
||||
local _, e5 = Nbt.encode({ a = Nbt.byte(300) })
|
||||
t:check(e5 and e5.code == Err.ARG, "encode byte range")
|
||||
local _, e6 = Nbt.encode({ a = function() end })
|
||||
t:check(e6 and e6.code == Err.ARG, "encode function")
|
||||
end
|
||||
|
||||
-- 7. fuzz
|
||||
local samples = {}
|
||||
for i = 1, 8 do
|
||||
table.insert(samples, Fx.cases[i].raw)
|
||||
table.insert(samples, Fx.cases[i].little)
|
||||
end
|
||||
table.insert(samples, Fx.cases[3].gzip)
|
||||
t:fuzz("decode", function(b: buffer)
|
||||
return Nbt.decode(b)
|
||||
end, samples)
|
||||
t:fuzz("decode varint", function(b: buffer)
|
||||
return Nbt.decode(b, { varint = true })
|
||||
end, { Fx.net[1].data, Fx.net[2].data })
|
||||
local texts = {}
|
||||
for i = 1, 6 do
|
||||
table.insert(texts, Fx.cases[i].compact)
|
||||
end
|
||||
t:fuzz("parse", function(b: buffer)
|
||||
return Nbt.parse(buffer.tostring(b))
|
||||
end, texts)
|
||||
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,60 @@
|
||||
-- data/nbt tests: the contract, nbtlib files through the api (F.load / F.save, NBT -> SNBT -> NBT), detection by
|
||||
-- content and extension, exact round trips (root name, edition, compression and the Bedrock header kept), error
|
||||
-- codes, and the fuzz pass. The engine itself: tests/codecs/nbt.luau; SNBT files: tests/data/snbt.luau.
|
||||
-- Fixtures: tools/fixtures/nbt.py.
|
||||
local T = require("@tests/T")
|
||||
local F = require("@formats")
|
||||
local NbtF = require("@formats/data/nbt")
|
||||
local SnbtF = require("@formats/data/snbt")
|
||||
local Nbt = require("@formats/codecs/text/Nbt")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Err = require("@formats/core/Err")
|
||||
local Fx = require("@tests/gen/nbt")
|
||||
|
||||
local t = T.suite("data/nbt")
|
||||
|
||||
t:eq(NbtF.name, "nbt", "name")
|
||||
t:eq(SnbtF.name, "snbt", "snbt name")
|
||||
t:check(NbtF.caps.decode and NbtF.caps.encode and SnbtF.caps.decode and SnbtF.caps.encode, "caps")
|
||||
|
||||
for i = 1, 12 do
|
||||
local c = Fx.cases[i]
|
||||
local v = F.load(c.gzip, { name = "level.dat" })
|
||||
t:check(v ~= nil, c.name .. ": F.load gzip")
|
||||
local text = assert(F.saveString(v, { format = "snbt" }))
|
||||
local back = assert(F.load(text, { format = "snbt" }))
|
||||
t:eq(Nbt.print(back), Nbt.print(v), c.name .. ": NBT -> SNBT -> value")
|
||||
local again = assert(F.save(v, nil, { format = "nbt" })) :: buffer
|
||||
t:eq(buffer.readu8(again, 0), 0x1f, c.name .. ": saved gzip like the source")
|
||||
t:eq(Nbt.print(F.load(again, { format = "nbt" })), Nbt.print(v), c.name .. ": saved file reads back")
|
||||
t:eq(Nbt.info(F.load(again, { format = "nbt" })).name, c.root, c.name .. ": root name kept")
|
||||
local raw = assert(F.save(v, nil, { format = "nbt", compression = "none" })) :: buffer
|
||||
t:eq(buffer.tostring(raw), c.raw, c.name .. ": uncompressed = nbtlib's bytes")
|
||||
local le = assert(F.load(c.header, { format = "nbt" }))
|
||||
t:eq(buffer.tostring(assert(F.save(le, nil, { format = "nbt" })) :: buffer), c.header, c.name .. ": Bedrock level.dat same bytes")
|
||||
end
|
||||
|
||||
-- detection
|
||||
do
|
||||
local c = Fx.cases[3]
|
||||
t:eq(F.detect(c.gzip, { name = "level.dat" }), "nbt", "detect gzip .dat")
|
||||
t:eq(F.detect(c.raw, { name = "x.nbt" }), "nbt", "detect raw .nbt")
|
||||
end
|
||||
|
||||
-- errors
|
||||
do
|
||||
local _, e = NbtF.decode(Bytes.reader(buffer.fromstring("\10\0\0\1")))
|
||||
t:check(e and e.code == Err.TRUNCATED, "truncated")
|
||||
local _, e3 = NbtF.encode(nil :: any)
|
||||
t:check(e3 and e3.code == Err.ARG, "encode nil")
|
||||
end
|
||||
|
||||
local samples = {}
|
||||
for i = 1, 6 do
|
||||
table.insert(samples, Fx.cases[i].raw)
|
||||
table.insert(samples, Fx.cases[i].header)
|
||||
end
|
||||
t:fuzz("decode", function(b: buffer)
|
||||
return NbtF.decode(Bytes.reader(b))
|
||||
end, samples)
|
||||
t:done()
|
||||
@@ -0,0 +1,47 @@
|
||||
-- data/snbt tests: the contract, nbtlib's SNBT text (indented and compact) through the api, conversion to and from
|
||||
-- binary NBT (F.convert), detection against JSON, options (indent, quote), error codes, and the fuzz pass. The
|
||||
-- grammar itself: tests/codecs/nbt.luau. Fixtures: tools/fixtures/nbt.py.
|
||||
local T = require("@tests/T")
|
||||
local F = require("@formats")
|
||||
local SnbtF = require("@formats/data/snbt")
|
||||
local Nbt = require("@formats/codecs/text/Nbt")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Err = require("@formats/core/Err")
|
||||
local Fx = require("@tests/gen/nbt")
|
||||
|
||||
local t = T.suite("data/snbt")
|
||||
|
||||
t:eq(SnbtF.name, "snbt", "name")
|
||||
t:check(SnbtF.caps.decode and SnbtF.caps.encode, "caps")
|
||||
|
||||
for i = 1, 12 do
|
||||
local c = Fx.cases[i]
|
||||
local v = assert(F.load(c.snbt, { format = "snbt" }))
|
||||
local bin = assert(F.convert(c.compact, "nbt", { format = "snbt", compression = "none" })) :: buffer
|
||||
t:eq(Nbt.print(assert(Nbt.decode(bin))), Nbt.print(v), c.name .. ": SNBT -> NBT")
|
||||
local text = assert(F.convert(c.raw, "snbt", { format = "nbt" })) :: buffer
|
||||
t:eq(Nbt.print(assert(Nbt.parse(text))), Nbt.print(v), c.name .. ": NBT -> SNBT")
|
||||
local pretty = assert(F.saveString(v, { format = "snbt", indent = "\t" }))
|
||||
t:eq(Nbt.print(assert(F.load(pretty, { format = "snbt" }))), Nbt.print(v), c.name .. ": indented")
|
||||
end
|
||||
|
||||
do
|
||||
t:eq(F.detect("{DataVersion:3465,size:[I;1,2,3]}"), "snbt", "detect snbt")
|
||||
t:eq(F.detect('{"a": 1}'), "json", "json is not snbt")
|
||||
t:eq(F.detect('{"a": 1b}'), "snbt", "typed number")
|
||||
t:eq(F.detect(Fx.cases[3].compact, { name = "x.snbt" }), "snbt", "detect .snbt")
|
||||
t:eq(F.saveString(Nbt.parse("{a:'x'}"), { format = "snbt", quote = "'" }), "{a:'x'}", "quote option")
|
||||
local _, e = SnbtF.decode(Bytes.reader(buffer.fromstring("{a:")))
|
||||
t:check(e and e.code == Err.CORRUPT, "corrupt")
|
||||
local _, e2 = SnbtF.encode({ a = function() end } :: any)
|
||||
t:check(e2 and e2.code == Err.ARG, "encode a function")
|
||||
end
|
||||
|
||||
local texts = {}
|
||||
for i = 1, 6 do
|
||||
table.insert(texts, Fx.cases[i].snbt)
|
||||
end
|
||||
t:fuzz("decode", function(b: buffer)
|
||||
return SnbtF.decode(Bytes.reader(b))
|
||||
end, texts)
|
||||
t:done()
|
||||
@@ -0,0 +1,138 @@
|
||||
-- host/Roblox tests: the pure half of the EditableMesh adapter (meshPages: triangle runs within the triangle / vertex
|
||||
-- limits, corners mapping back to the source indices, double-sided counting, errors; pageData: centring, per-axis
|
||||
-- scale, inverse-scaled unit normals, uvs + flipV, linear -> sRGB colours). toEditableMesh itself needs Roblox.
|
||||
local T = require("@tests/T")
|
||||
local Host = require("@formats/host/Roblox")
|
||||
local Types = require("@formats/Types")
|
||||
local Err = require("@formats/core/Err")
|
||||
|
||||
local t = T.suite("host_roblox")
|
||||
local S = Types.Scene
|
||||
|
||||
local function f32(list: { number }, comps: number): Types.Accessor
|
||||
local a = S.accessor(nil, "f32", comps, #list // comps)
|
||||
for i, v in list do
|
||||
buffer.writef32(a.data, (i - 1) * 4, v)
|
||||
end
|
||||
return a
|
||||
end
|
||||
local function near(a: number, b: number, eps: number?): boolean
|
||||
return math.abs(a - b) <= (eps or 1e-5)
|
||||
end
|
||||
|
||||
-- an n x n grid of quads in the xz plane (two triangles each), u16 indices, normals up, uvs, colours
|
||||
local function grid(n: number): Types.Primitive
|
||||
local pos, nrm, uv, col = {}, {}, {}, {}
|
||||
for z = 0, n do
|
||||
for x = 0, n do
|
||||
table.insert(pos, x)
|
||||
table.insert(pos, 0)
|
||||
table.insert(pos, z)
|
||||
for _, v in { 0, 1, 0 } do
|
||||
table.insert(nrm, v)
|
||||
end
|
||||
table.insert(uv, x / n)
|
||||
table.insert(uv, z / n)
|
||||
for _, v in { 0.5, 0, 1, 0.25 } do
|
||||
table.insert(col, v)
|
||||
end
|
||||
end
|
||||
end
|
||||
local idx = {}
|
||||
for z = 0, n - 1 do
|
||||
for x = 0, n - 1 do
|
||||
local a = z * (n + 1) + x
|
||||
local b, c, d = a + 1, a + n + 1, a + n + 2
|
||||
for _, v in { a, c, b, b, c, d } do
|
||||
table.insert(idx, v)
|
||||
end
|
||||
end
|
||||
end
|
||||
local ia = S.accessor(nil, "u16", 1, #idx)
|
||||
for i, v in idx do
|
||||
buffer.writeu16(ia.data, (i - 1) * 2, v)
|
||||
end
|
||||
return {
|
||||
attributes = { POSITION = f32(pos, 3), NORMAL = f32(nrm, 3), TEXCOORD_0 = f32(uv, 2), COLOR_0 = f32(col, 4) },
|
||||
indices = ia,
|
||||
mode = "triangles",
|
||||
}
|
||||
end
|
||||
|
||||
-- 1. pages within the limits, covering every triangle once, corners = the source indices
|
||||
local prim = grid(10) -- 121 vertices, 200 triangles
|
||||
local pages = assert(Host.meshPages(prim, { maxTriangles = 64 }))
|
||||
t:eq(#pages, 4, "200 triangles / 64 per page")
|
||||
local covered = 0
|
||||
for pi, pg in pages do
|
||||
t:eq(pg.first, covered, "page " .. pi .. " starts after the previous one")
|
||||
t:check(pg.count <= 64, "page " .. pi .. " within maxTriangles")
|
||||
t:eq(buffer.len(pg.corners), pg.count * 12, "page " .. pi .. " corners")
|
||||
for k = 0, pg.count * 3 - 1 do
|
||||
local src = buffer.readu16((prim.indices :: any).data, (pg.first * 3 + k) * 2)
|
||||
local l = buffer.readu32(pg.corners, k * 4)
|
||||
if pg.vertices[l + 1] ~= src then
|
||||
t:check(false, string.format("page %d corner %d maps back to %d", pi, k, src))
|
||||
end
|
||||
end
|
||||
covered += pg.count
|
||||
end
|
||||
t:eq(covered, 200, "every triangle in a page")
|
||||
t:eq(pages[1].min[1], 0, "page bounds min")
|
||||
t:check(pages[1].max[3] > 0, "page bounds max")
|
||||
|
||||
local one = assert(Host.meshPages(prim))
|
||||
t:eq(#one, 1, "default limits: one page")
|
||||
t:eq(#one[1].vertices, 121, "every vertex once")
|
||||
|
||||
local two = assert(Host.meshPages(prim, { maxTriangles = 64, doubleSided = true }))
|
||||
t:eq(#two, 7, "double-sided: 32 front triangles per page")
|
||||
|
||||
local tight = assert(Host.meshPages(prim, { maxVertices = 30 }))
|
||||
for _, pg in tight do
|
||||
t:check(#pg.vertices <= 30, "maxVertices")
|
||||
end
|
||||
t:check(#tight > 1, "maxVertices splits")
|
||||
|
||||
-- no indices: vertices in order
|
||||
local soup = { attributes = { POSITION = f32({ 0, 0, 0, 1, 0, 0, 0, 1, 0, 2, 0, 0, 3, 0, 0, 2, 1, 0 }, 3) }, mode = "triangles" }
|
||||
local sp = assert(Host.meshPages(soup :: any))
|
||||
t:eq(sp[1].count, 2, "unindexed triangles")
|
||||
t:eq(#sp[1].vertices, 6, "unindexed vertices")
|
||||
|
||||
-- 2. errors
|
||||
local _, e1 = Host.meshPages({ attributes = { POSITION = f32({ 0, 0, 0 }, 3) }, mode = "lines" } :: any)
|
||||
t:check(e1 and e1.code == Err.ARG, "lines are not triangles")
|
||||
local _, e2 = Host.meshPages({} :: any)
|
||||
t:check(e2 and e2.code == Err.ARG, "no POSITION")
|
||||
local bad = grid(1)
|
||||
buffer.writeu16((bad.indices :: any).data, 2, 999)
|
||||
local _, e3 = Host.meshPages(bad)
|
||||
t:check(e3 and e3.code == Err.ARG, "index out of range")
|
||||
local _, e4 = Host.pageData(prim, {} :: any)
|
||||
t:check(e4 and e4.code == Err.ARG, "pageData needs a page")
|
||||
|
||||
-- 3. pageData: centred, scaled, normals, uvs, colours
|
||||
local pg = one[1]
|
||||
local d = assert(Host.pageData(prim, pg, { scale = { 2, 1, 4 } }))
|
||||
t:eq(d.vertexCount, 121, "vertex count")
|
||||
t:eq(d.triangleCount, 200, "triangle count")
|
||||
t:eq(d.centre[1], 10, "centre x (5 * 2)")
|
||||
t:eq(d.centre[3], 20, "centre z (5 * 4)")
|
||||
local v0 = pg.vertices[1] -- source vertex of local 1
|
||||
local sx0 = v0 % 11
|
||||
t:eq(d.positions[1], (sx0 - 5) * 2, "position x relative to the centre, scaled")
|
||||
t:check(d.normals and near(d.normals[2], 1), "unit normal")
|
||||
t:check(d.uvs and near(d.uvs[1], sx0 / 10), "uv")
|
||||
t:check(d.colors and near(d.colors[1], 0.7353569, 1e-4), "linear 0.5 -> sRGB")
|
||||
t:check(d.colors and near(d.colors[3], 1) and near(d.colors[4], 0.25), "sRGB 1 and alpha kept")
|
||||
|
||||
local slanted = { attributes = { POSITION = f32({ 0, 0, 0, 1, 0, 0, 0, 1, 0 }, 3), NORMAL = f32({ 1, 1, 0, 1, 1, 0, 1, 1, 0 }, 3) }, mode = "triangles" }
|
||||
local sd = assert(Host.pageData(slanted :: any, assert(Host.meshPages(slanted :: any))[1], { scale = { 2, 1, 1 }, origin = { 0, 0, 0 } }))
|
||||
t:check(near(sd.normals[1], 0.5 / math.sqrt(1.25)) and near(sd.normals[2], 1 / math.sqrt(1.25)), "normals scale by the inverse")
|
||||
t:eq(sd.positions[4], 2, "origin option")
|
||||
local fl = assert(Host.pageData(prim, pg, { flipV = true, normals = false, colors = false }))
|
||||
t:check(fl.normals == nil and fl.colors == nil, "attributes switched off")
|
||||
t:check(fl.uvs and near(fl.uvs[2], 1 - (v0 // 11) / 10), "flipV")
|
||||
|
||||
t:done()
|
||||
@@ -0,0 +1,286 @@
|
||||
-- tests/model/_rbx: helpers shared by the model/rbxm and model/rbxmx tests: instance paths (the scheme of the lune dump
|
||||
-- in tools/fixtures/rbxmx.py), our property values in lune's canonical form, a dump comparison, a deep tree comparison,
|
||||
-- and a tree with a value of every type built from scratch.
|
||||
local Rbx = require("@formats/codecs/model/Rbx")
|
||||
|
||||
local H = {}
|
||||
|
||||
local f32buf = buffer.create(4)
|
||||
local function f32(v: number): number
|
||||
buffer.writef32(f32buf, 0, v)
|
||||
return buffer.readf32(f32buf, 0)
|
||||
end
|
||||
|
||||
local function hex(s: string): string
|
||||
return (string.gsub(s, ".", function(c: string): string
|
||||
return string.format("%02x", string.byte(c))
|
||||
end))
|
||||
end
|
||||
|
||||
-- paths(tree) -> { [path] = instance }, { [instance] = path }: names joined with "/", "#k" for the k-th sibling of a name
|
||||
function H.paths(tree: any): ({ [string]: any }, { [any]: string })
|
||||
local byPath, ofInst = {}, {}
|
||||
local function walk(list: { any }, prefix: string)
|
||||
local seen: { [string]: number } = {}
|
||||
for _, inst in list do
|
||||
local n = inst.name
|
||||
seen[n] = (seen[n] or 0) + 1
|
||||
local p = prefix .. (if prefix == "" then "" else "/") .. n .. (if seen[n] > 1 then "#" .. seen[n] else "")
|
||||
byPath[p] = inst
|
||||
ofInst[inst] = p
|
||||
walk(inst.children, p)
|
||||
end
|
||||
end
|
||||
walk(tree.roots, "")
|
||||
return byPath, ofInst
|
||||
end
|
||||
|
||||
local function numbers(t: string, v: any): { number }?
|
||||
if type(v) == "number" then
|
||||
return { v }
|
||||
elseif type(v) ~= "table" then
|
||||
return nil
|
||||
end
|
||||
if t == "NumberSequence" or t == "ColorSequence" then
|
||||
local out = {}
|
||||
for _, k in v do
|
||||
local n = if t == "ColorSequence" then 4 else 3 -- lune has no ColorSequence envelope
|
||||
for c = 1, n do
|
||||
table.insert(out, k[c])
|
||||
end
|
||||
end
|
||||
return out
|
||||
end
|
||||
return v
|
||||
end
|
||||
|
||||
-- same(ours: Value, want: lune canon, pathOf, propName) -> ok, detail
|
||||
function H.same(v: any, want: any, pathOf: { [any]: string }, pname: string): (boolean, string)
|
||||
local t = if v then v.type else "nil"
|
||||
local x = if v then v.value else nil
|
||||
local wt = want.t
|
||||
if wt == "nil" then
|
||||
return x == nil, "want nil, got " .. t
|
||||
elseif wt == "string" then
|
||||
return type(x) == "string" and hex(x) == want.s, "string differs (" .. t .. ")"
|
||||
elseif wt == "bool" then
|
||||
return x == want.b, "bool differs"
|
||||
elseif wt == "Instance" then
|
||||
return type(x) == "table" and pathOf[x] == want.s, "ref differs: " .. tostring(x and pathOf[x]) .. " vs " .. tostring(want.s)
|
||||
elseif wt == "Content" then
|
||||
if want.kind == "None" then
|
||||
return x == nil, "content not none"
|
||||
elseif want.kind == "Uri" then
|
||||
return x == want.s, "content uri " .. tostring(x)
|
||||
end
|
||||
return type(x) == "table" and pathOf[x] == want.s, "content object"
|
||||
elseif wt == "UniqueId" then
|
||||
return x == want.s, "uniqueid " .. tostring(x) .. " vs " .. tostring(want.s)
|
||||
elseif wt == "Font" then
|
||||
return type(x) == "table" and x.family == want.s and x.weight == want.n[1] and x.style == want.style, "font differs"
|
||||
end
|
||||
local got = numbers(t, x)
|
||||
if t == "Color3uint8" and got then
|
||||
got = { f32(got[1] / 255), f32(got[2] / 255), f32(got[3] / 255) }
|
||||
end
|
||||
local n = want.n
|
||||
if not got or not n then
|
||||
return false, "no numbers: " .. t .. " vs " .. tostring(wt)
|
||||
end
|
||||
local count = #n
|
||||
if t == "PhysicalProperties" then
|
||||
count = math.min(#got, #n)
|
||||
elseif #got ~= #n then
|
||||
return false, string.format("%d numbers, want %d (%s / %s)", #got, #n, t, wt)
|
||||
end
|
||||
for i = 1, count do
|
||||
if got[i] ~= n[i] then
|
||||
return false, string.format("%s[%d] = %.9g, want %.9g", t, i, got[i], n[i])
|
||||
end
|
||||
end
|
||||
return true, ""
|
||||
end
|
||||
|
||||
-- compare(t, tree, dump, label) -> properties compared: every instance / property of a lune dump against our tree
|
||||
function H.compare(t: any, tree: any, dump: any, label: string): number
|
||||
local byPath, pathOf = H.paths(tree)
|
||||
local n = 0
|
||||
for path, e in dump do
|
||||
local inst = byPath[path]
|
||||
t:check(inst ~= nil, label .. ": instance " .. path)
|
||||
if inst then
|
||||
t:check(inst.className == e.class, label .. ": " .. path .. " class " .. inst.className .. ", want " .. e.class)
|
||||
for pname, want in e.props do
|
||||
local v = if pname == "Name" then { type = "String", value = inst.name } else inst.properties[pname]
|
||||
local ok, detail = H.same(v, want, pathOf, pname)
|
||||
t:check(ok, label .. ": " .. path .. "." .. pname .. ": " .. detail)
|
||||
n += 1
|
||||
end
|
||||
end
|
||||
end
|
||||
return n
|
||||
end
|
||||
|
||||
-- the type family a value compares under (a binary file has one string type; XML writes BrickColor as an int)
|
||||
local FAMILY: { [string]: string } = {
|
||||
String = "string",
|
||||
BinaryString = "string",
|
||||
ProtectedString = "string",
|
||||
ContentId = "string",
|
||||
Bytecode = "string",
|
||||
BrickColor = "int",
|
||||
Int32 = "int",
|
||||
}
|
||||
local function family(t: string): string
|
||||
return FAMILY[t] or t
|
||||
end
|
||||
|
||||
-- equal(a, b, pathA, pathB) -> ok, where: values compared deeply (Refs / Content objects by path)
|
||||
local function equal(a: any, b: any, pa: { [any]: string }, pb: { [any]: string }): boolean
|
||||
if type(a) ~= type(b) then
|
||||
return false
|
||||
end
|
||||
if type(a) == "buffer" then
|
||||
return buffer.tostring(a) == buffer.tostring(b)
|
||||
elseif type(a) ~= "table" then
|
||||
return a == b or (a ~= a and b ~= b)
|
||||
end
|
||||
if a.className and b.className then
|
||||
return pa[a] == pb[b]
|
||||
end
|
||||
for k, v in a do
|
||||
if not equal(v, b[k], pa, pb) then
|
||||
return false
|
||||
end
|
||||
end
|
||||
for k in b do
|
||||
if a[k] == nil then
|
||||
return false
|
||||
end
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
-- sameTree(t, a, b, label, opts?) -> instances compared: class, name, children and every property (opts.loose:
|
||||
-- string / int families compare as one type, properties only b has are allowed when another instance of the class has them in a; opts.skipRaw: Raw values are not compared)
|
||||
function H.sameTree(t: any, a: any, b: any, label: string, opts: any?): number
|
||||
local o = opts or {}
|
||||
local pa = select(2, H.paths(a))
|
||||
local pb = select(2, H.paths(b))
|
||||
local n = 0
|
||||
local bad = 0
|
||||
local classProps: { [string]: { [string]: boolean } } = {}
|
||||
for inst in Rbx.descendants(a) do
|
||||
local i = inst :: any
|
||||
classProps[i.className] = classProps[i.className] or {}
|
||||
for k in i.properties do
|
||||
classProps[i.className][k] = true
|
||||
end
|
||||
end
|
||||
local function walk(la: { any }, lb: { any }, where: string)
|
||||
if #la ~= #lb then
|
||||
t:check(false, string.format("%s: %s has %d children, want %d", label, where, #lb, #la))
|
||||
return
|
||||
end
|
||||
for i, x in la do
|
||||
local y = lb[i]
|
||||
n += 1
|
||||
local w = where .. "/" .. x.name
|
||||
if x.className ~= y.className or x.name ~= y.name then
|
||||
t:check(false, label .. ": " .. w .. ": " .. y.className .. " " .. y.name)
|
||||
else
|
||||
for k, v in x.properties do
|
||||
local u = y.properties[k]
|
||||
local skip = o.skipRaw and v.type == "Raw"
|
||||
if not skip then
|
||||
local tt = if o.loose then family(v.type) == family(u and u.type or "") else u and u.type == v.type
|
||||
if not tt or not equal(v.value, u.value, pa, pb) then
|
||||
bad += 1
|
||||
if bad < 10 then
|
||||
t:check(false, label .. ": " .. w .. "." .. k .. " (" .. v.type .. " / " .. tostring(u and u.type) .. ")")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
for k, u in y.properties do
|
||||
-- a binary file stores a value for every instance of a class: the writer fills in the ones the XML
|
||||
-- twin leaves out (when another instance of the class has the property)
|
||||
local filled = o.loose and classProps[y.className] and classProps[y.className][k]
|
||||
if x.properties[k] == nil and not (o.skipRaw and u.type == "Raw") and not filled then
|
||||
bad += 1
|
||||
if bad < 10 then
|
||||
t:check(false, label .. ": " .. w .. "." .. k .. " is extra")
|
||||
end
|
||||
end
|
||||
end
|
||||
walk(x.children, y.children, w)
|
||||
end
|
||||
end
|
||||
end
|
||||
walk(a.roots, b.roots, "")
|
||||
t:check(bad == 0, label .. ": " .. bad .. " property differences")
|
||||
return n
|
||||
end
|
||||
|
||||
-- everything() -> a model with one value of every type (and several instances per class)
|
||||
function H.everything(): any
|
||||
local tree = Rbx.tree()
|
||||
local root = Rbx.add(tree, Rbx.instance("Model", "Everything"))
|
||||
local a = Rbx.add(root, Rbx.instance("Folder", "A"))
|
||||
local b = Rbx.add(root, Rbx.instance("Folder", "B"))
|
||||
local c = Rbx.add(a, Rbx.instance("Part", "C"))
|
||||
local S = Rbx.set
|
||||
S(a, "Str", "String", "plain")
|
||||
S(a, "Bin", "BinaryString", "\0\1\2\255")
|
||||
S(a, "Prot", "ProtectedString", "print('x')")
|
||||
S(a, "Cid", "ContentId", "rbxassetid://1")
|
||||
S(a, "Flag", "Bool", true)
|
||||
S(a, "I32", "Int32", -2147483648)
|
||||
S(a, "I64", "Int64", 2 ^ 52 + 1)
|
||||
S(a, "F32", "Float32", f32(0.1))
|
||||
S(a, "F64", "Float64", 0.1)
|
||||
S(a, "En", "Enum", 7)
|
||||
S(a, "Brick", "BrickColor", 1004)
|
||||
S(a, "Sec", "SecurityCapabilities", 2 ^ 40)
|
||||
S(a, "Fc", "Faces", 63)
|
||||
S(a, "Ax", "Axes", 7)
|
||||
S(a, "Ud", "UDim", { 0.5, -7 })
|
||||
S(a, "Ud2", "UDim2", { 0.25, 1, -0.5, 1000 })
|
||||
S(a, "Nr", "NumberRange", { -1, 1 })
|
||||
S(a, "V2", "Vector2", { 1.5, -2.5 })
|
||||
S(a, "V2i", "Vector2int16", { -32768, 32767 })
|
||||
S(a, "V3", "Vector3", { 1, 2, 3 })
|
||||
S(a, "V3i", "Vector3int16", { -5, 0, 5 })
|
||||
S(a, "C3", "Color3", { 0.5, 0.25, 1 })
|
||||
S(a, "C3u", "Color3uint8", { 0, 128, 255 })
|
||||
S(a, "Rc", "Rect", { 1, 2, 3, 4 })
|
||||
S(a, "Ry", "Ray", { 1, 2, 3, 0, 0, -1 })
|
||||
S(a, "Cf", "CFrame", { 1, 2, 3, 0, -1, 0, 1, 0, 0, 0, 0, 1 })
|
||||
S(a, "Cf2", "CFrame", { 1, 2, 3, f32(0.36), f32(0.48), f32(-0.8), f32(-0.8), f32(0.6), 0, f32(0.48), f32(0.64), f32(0.6) })
|
||||
S(a, "Ocf", "OptionalCFrame", { 4, 5, 6, 1, 0, 0, 0, 1, 0, 0, 0, 1 })
|
||||
S(b, "Ocf", "OptionalCFrame", nil)
|
||||
S(a, "Ns", "NumberSequence", { { 0, 1, 0 }, { 0.5, 2, 0.25 }, { 1, 3, 0 } })
|
||||
S(a, "Cs", "ColorSequence", { { 0, 1, 0, 0, 0 }, { 1, 0, 0, 1, 0 } })
|
||||
S(a, "Phys", "PhysicalProperties", { 1, 0.5, 0.25, 2, 3, 1 })
|
||||
S(b, "Phys", "PhysicalProperties", { 1, 0.5, 0.25, 2, 3, 0.75 })
|
||||
S(c, "Phys", "PhysicalProperties", nil)
|
||||
S(a, "Ref", "Ref", c)
|
||||
S(b, "Ref", "Ref", nil)
|
||||
S(a, "Sh", "SharedString", "shared\0bytes")
|
||||
S(b, "Sh", "SharedString", "shared\0bytes")
|
||||
S(a, "Uid", "UniqueId", "7fb188dace632b4702e9c68d004815fd")
|
||||
S(b, "Uid", "UniqueId", "00000000000000010000000200000003")
|
||||
S(a, "Font", "Font", { family = "rbxasset://fonts/families/Arial.json", weight = 700, style = "Italic", cachedFaceId = "rbxasset://fonts/arialbd.ttf" })
|
||||
S(a, "Ct", "Content", "rbxassetid://5")
|
||||
S(b, "Ct", "Content", c)
|
||||
S(c, "Ct", "Content", nil)
|
||||
-- the same properties on B (columns of two values), so every type is also written for a second instance
|
||||
for k, v in a.properties do
|
||||
if b.properties[k] == nil then
|
||||
b.properties[k] = v
|
||||
end
|
||||
end
|
||||
return tree
|
||||
end
|
||||
|
||||
return H
|
||||
@@ -0,0 +1,148 @@
|
||||
-- model/mca tests: region files with 1.18+ chunks (each compression: zlib, gzip, stored, LZ4 block streams),
|
||||
-- 1.16 / 1.13 / 1.12 chunks (palettes non-spanning and spanning, numeric ids with Add nibbles) and an external .mcc
|
||||
-- chunk: every cell of the assembled grid against the fixture (a CRC of the cell texts), placement, the chunk list,
|
||||
-- block entities, opts.chunks / opts.area / maxCells, Mca.list / Mca.chunk, detection, errors and the fuzz pass.
|
||||
-- Fixtures: tools/fixtures/mca.py.
|
||||
local T = require("@tests/T")
|
||||
local F = require("@formats")
|
||||
local Mca = require("@formats/model/mca")
|
||||
local Mc = require("@formats/codecs/model/Minecraft")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Crc32 = require("@formats/core/Crc32")
|
||||
local Err = require("@formats/core/Err")
|
||||
local Types = require("@formats/Types")
|
||||
local Fx = require("@tests/gen/mca")
|
||||
|
||||
local t = T.suite("mca")
|
||||
|
||||
t:eq(Mca.name, "mca", "name")
|
||||
t:check(Mca.caps.decode and not Mca.caps.encode, "caps: read-only")
|
||||
|
||||
local function reader(s: string): Bytes.Reader
|
||||
return Bytes.reader(buffer.fromstring(s))
|
||||
end
|
||||
|
||||
-- the fixture's cell texts: the state, or "legacy:<id>:<data>" inside chunks without a DataVersion
|
||||
local function cellText(scene: Types.Scene): string
|
||||
local v = (scene.voxels :: { Types.Voxels })[1]
|
||||
local b = v.blocks :: Types.VoxelBlocks
|
||||
local o = scene.nodes[1].translation :: { number }
|
||||
local legacyChunk: { [string]: boolean } = {}
|
||||
for _, c in scene.meta.source.chunks do
|
||||
if c.dataVersion == nil then
|
||||
legacyChunk[c.worldX .. "," .. c.worldZ] = true
|
||||
end
|
||||
end
|
||||
local sx, sy, sz = v.size[1], v.size[2], v.size[3]
|
||||
local parts = table.create(sx * sy * sz)
|
||||
local i = 0
|
||||
for z = 0, sz - 1 do
|
||||
for y = 0, sy - 1 do
|
||||
for x = 0, sx - 1 do
|
||||
local st = b.palette[buffer.readu16(b.data, i * 2) + 1]
|
||||
local wx, wz = o[1] + x, o[3] + z
|
||||
if legacyChunk[(wx // 16) .. "," .. (wz // 16)] then
|
||||
local id, d = Mc.legacyId(st)
|
||||
st = "legacy:" .. tostring(id) .. ":" .. tostring(d)
|
||||
end
|
||||
i += 1
|
||||
parts[i] = st
|
||||
end
|
||||
end
|
||||
end
|
||||
return table.concat(parts, "\n")
|
||||
end
|
||||
|
||||
for _, c in Fx.regions do
|
||||
local opts = { name = c.name, resolve = function(name: string): buffer?
|
||||
local f = c.files and c.files[name]
|
||||
return if f then buffer.fromstring(f) else nil
|
||||
end }
|
||||
local scene, err = Mca.decode(reader(c.data), opts)
|
||||
t:check(scene ~= nil, c.name .. ": " .. tostring(err))
|
||||
local s = scene :: Types.Scene
|
||||
local v = (s.voxels :: { Types.Voxels })[1]
|
||||
t:eq(table.concat(v.size, ","), table.concat(c.size, ","), c.name .. ": size")
|
||||
t:eq(table.concat(s.nodes[1].translation :: { number }, ","), table.concat(c.min, ","), c.name .. ": minimum corner")
|
||||
t:eq(Crc32.of(buffer.fromstring(cellText(s))), c.crc, c.name .. ": every cell")
|
||||
t:eq(#s.meta.source.chunks, #c.chunks, c.name .. ": chunks")
|
||||
for i, want in c.chunks do
|
||||
local got = s.meta.source.chunks[i]
|
||||
t:check(got.x == want.x and got.z == want.z, c.name .. ": chunk " .. i .. " position")
|
||||
t:eq(got.timestamp, want.timestamp, c.name .. ": chunk " .. i .. " timestamp")
|
||||
t:eq(got.compression, want.compression, c.name .. ": chunk " .. i .. " compression")
|
||||
t:eq(got.dataVersion, want.dataVersion, c.name .. ": chunk " .. i .. " DataVersion")
|
||||
t:check(got.nbt ~= nil, c.name .. ": chunk " .. i .. " NBT kept")
|
||||
end
|
||||
t:eq(#((v.blocks :: Types.VoxelBlocks).blockEntities :: { any }), c.blockEntities, c.name .. ": block entities")
|
||||
-- colours follow the blocks
|
||||
local air = 0
|
||||
for i = 0, buffer.len(v.data) - 1 do
|
||||
if buffer.readu8(v.data, i) == 0 then
|
||||
air += 1
|
||||
end
|
||||
end
|
||||
t:check(air > 0 and air < buffer.len(v.data), c.name .. ": colour grid")
|
||||
end
|
||||
|
||||
-- details on the 1.18+ region
|
||||
do
|
||||
local c = Fx.regions[1]
|
||||
local list = assert(Mca.list(reader(c.data)))
|
||||
t:eq(#list, 4, "list")
|
||||
local nbt, comp = Mca.chunk(reader(c.data), 3, 2)
|
||||
t:check(nbt and nbt.xPos == 3 and nbt.Status == "minecraft:full", "Mca.chunk")
|
||||
t:eq(comp, "lz4", "Mca.chunk compression")
|
||||
t:eq(Mca.chunk(reader(c.data), 9, 9), nil, "absent chunk")
|
||||
-- only some chunks / an area
|
||||
local one = assert(Mca.decode(reader(c.data), { chunks = { { 1, 0 } }, keepNbt = false }))
|
||||
t:eq(table.concat((one.voxels :: { Types.Voxels })[1].size, ","), "16,48,16", "opts.chunks")
|
||||
t:eq(one.meta.source.chunks[1].nbt, nil, "keepNbt = false")
|
||||
local part = assert(Mca.decode(reader(c.data), { area = { 4, -64, 4, 19, -60, 9 } }))
|
||||
local pv = (part.voxels :: { Types.Voxels })[1]
|
||||
t:eq(table.concat(pv.size, ","), "16,5,6", "opts.area size")
|
||||
t:eq(table.concat(part.nodes[1].translation :: { number }, ","), "4,-64,4", "opts.area corner")
|
||||
-- the area's cells equal the same cells of the whole grid
|
||||
local whole = assert(Mca.decode(reader(c.data)))
|
||||
local wv = (whole.voxels :: { Types.Voxels })[1]
|
||||
local wo = whole.nodes[1].translation :: { number }
|
||||
local bad = 0
|
||||
for z = 0, 5 do
|
||||
for y = 0, 4 do
|
||||
for x = 0, 15 do
|
||||
local a = pv.blocks.palette[buffer.readu16(pv.blocks.data, (x + 16 * (y + 5 * z)) * 2) + 1]
|
||||
local gx, gy, gz = 4 + x - wo[1], -64 + y - wo[2], 4 + z - wo[3]
|
||||
local w = wv.blocks.palette[buffer.readu16(wv.blocks.data, (gx + wv.size[1] * (gy + wv.size[2] * gz)) * 2) + 1]
|
||||
if a ~= w then
|
||||
bad += 1
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
t:eq(bad, 0, "area cells")
|
||||
local _, e = Mca.decode(reader(c.data), { maxCells = 1000 })
|
||||
t:check(e and e.code == Err.LIMIT, "maxCells")
|
||||
t:eq(F.detect(c.data), "mca", "F.detect")
|
||||
local viaApi = F.load(c.data, { name = "r.0.0.mca" })
|
||||
t:check(viaApi and viaApi.kind == "scene", "F.load")
|
||||
t:check(F.save(viaApi, nil, { format = "schem" }) ~= nil, "region -> .schem")
|
||||
end
|
||||
|
||||
-- errors
|
||||
do
|
||||
local ext = Fx.regions[3]
|
||||
local _, e1 = Mca.decode(reader(ext.data))
|
||||
t:check(e1 and e1.code == Err.UNSUPPORTED, "external chunk without resolve")
|
||||
local _, e2 = Mca.decode(reader(string.sub(Fx.regions[1].data, 1, 9000)))
|
||||
t:check(e2 and e2.code == Err.TRUNCATED, "truncated region")
|
||||
local _, e3 = Mca.decode(reader(string.rep("\0", 100)))
|
||||
t:check(e3 and e3.code == Err.TRUNCATED, "short header")
|
||||
local _, e4 = Mca.chunk(reader(Fx.regions[1].data), 40, 0)
|
||||
t:check(e4 and e4.code == Err.ARG, "bad chunk coordinates")
|
||||
end
|
||||
|
||||
local samples = { Fx.regions[2].data, string.sub(Fx.regions[1].data, 1, 20000) }
|
||||
t:fuzz("decode", function(b: buffer)
|
||||
return Mca.decode(Bytes.reader(b))
|
||||
end, samples, { keepHeader = 0 })
|
||||
t:done()
|
||||
@@ -0,0 +1,130 @@
|
||||
-- model/qb tests: Qubicle Binary files with every option (RGBA / BGRA, left- / right-handed, RLE or not, visibility
|
||||
-- masks, more than 255 colours) against the reference reader / writer of the fixture (exact colours of every cell,
|
||||
-- names, positions), the indexed palette view, our encodings read back by the reference (same settings, flipped
|
||||
-- settings, palette grids with computed masks), the api, error codes and the fuzz pass. Fixtures: tools/fixtures/qb.py.
|
||||
local T = require("@tests/T")
|
||||
local F = require("@formats")
|
||||
local Qb = require("@formats/model/qb")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Crc32 = require("@formats/core/Crc32")
|
||||
local Err = require("@formats/core/Err")
|
||||
local Types = require("@formats/Types")
|
||||
local Fx = require("@tests/gen/qb")
|
||||
|
||||
local t = T.suite("qb")
|
||||
|
||||
t:eq(Qb.name, "qb", "name")
|
||||
t:check(Qb.caps.decode and Qb.caps.encode, "caps")
|
||||
|
||||
local function dec(data: string | buffer): (Types.Scene?, any)
|
||||
return Qb.decode(Bytes.reader(if type(data) == "string" then buffer.fromstring(data) else data))
|
||||
end
|
||||
|
||||
local function check(scene: Types.Scene, want: any, label: string)
|
||||
t:eq(#scene.nodes, #want.matrices, label .. ": matrices")
|
||||
for i, m in want.matrices do
|
||||
local node = scene.nodes[i]
|
||||
local v = (scene.voxels :: { Types.Voxels })[node.voxels :: number]
|
||||
t:eq(node.name, m.name, label .. " " .. i .. ": name")
|
||||
t:eq(table.concat(v.size, ","), table.concat(m.size, ","), label .. " " .. i .. ": size")
|
||||
t:eq(table.concat(node.translation :: { number }, ","), table.concat(m.pos, ","), label .. " " .. i .. ": position")
|
||||
t:eq(Crc32.of(v.colors :: buffer), m.crc, label .. " " .. i .. ": colours")
|
||||
local filled = 0
|
||||
for k = 0, buffer.len(v.data) - 1 do
|
||||
if buffer.readu8(v.data, k) ~= 0 then
|
||||
filled += 1
|
||||
end
|
||||
end
|
||||
t:eq(filled, m.filled, label .. " " .. i .. ": filled cells")
|
||||
end
|
||||
end
|
||||
|
||||
for _, c in Fx.cases do
|
||||
local s, err = dec(c.data)
|
||||
t:check(s ~= nil, c.name .. ": " .. tostring(err))
|
||||
check(s :: Types.Scene, c, c.name)
|
||||
local src = (s :: Types.Scene).meta.source
|
||||
t:check(src.colorFormat == c.colorFormat and src.rightHanded == c.rightHanded and src.compressed == c.compressed and src.visibilityMask == c.visibilityMask, c.name .. ": settings")
|
||||
t:check(buffer.tostring(Qb.encode(s) :: buffer) == c.data, c.name .. ": same bytes again")
|
||||
-- the palette view: exact colours when there are up to 255
|
||||
local v = ((s :: Types.Scene).voxels :: { Types.Voxels })[1]
|
||||
local exact = true
|
||||
for k = 0, buffer.len(v.data) - 1 do
|
||||
local idx = buffer.readu8(v.data, k)
|
||||
if idx > 0 then
|
||||
local p, col = v.palette[idx], buffer.readu32(v.colors :: buffer, k * 4)
|
||||
if p[1] + p[2] * 256 + p[3] * 65536 ~= col % 16777216 then
|
||||
exact = false
|
||||
end
|
||||
end
|
||||
end
|
||||
t:eq(exact, c.name ~= "case5", c.name .. ": palette exact up to 255 colours")
|
||||
t:eq(F.detect(c.data), "qb", c.name .. ": detect")
|
||||
end
|
||||
|
||||
-- our encodings, read by the reference
|
||||
for name, want in Fx.ours do
|
||||
local base = string.match(name, "^(case%d)_")
|
||||
if base then
|
||||
local c
|
||||
for _, x in Fx.cases do
|
||||
if x.name == base then
|
||||
c = x
|
||||
end
|
||||
end
|
||||
t:check(c ~= nil, name .. ": source")
|
||||
-- the same cells whatever the settings (scene coordinates)
|
||||
for i, m in want.matrices do
|
||||
t:eq(m.crc, c.matrices[i].crc, name .. " " .. i .. ": colours read back")
|
||||
t:eq(table.concat(m.pos, ","), table.concat(c.matrices[i].pos, ","), name .. " " .. i .. ": position read back")
|
||||
end
|
||||
if string.find(name, "_flip") then
|
||||
t:check(want.rightHanded ~= c.rightHanded and want.compressed ~= c.compressed, name .. ": flipped settings")
|
||||
end
|
||||
end
|
||||
end
|
||||
do
|
||||
local m = Fx.ours.palette_mask
|
||||
t:check(m.visibilityMask and m.matrices[1].filled == 15, "palette grid with a mask")
|
||||
local plain = Fx.ours.palette_plain
|
||||
t:check(not plain.visibilityMask and plain.rightHanded and plain.compressed, "defaults")
|
||||
-- our decode of the masked file: the centre cell (hidden on every side) has mask 1
|
||||
local v = Types.Voxels.new(3, 3, 3)
|
||||
v.palette[1] = { 200, 10, 10, 255 }
|
||||
for i = 0, 26 do
|
||||
buffer.writeu8(v.data, i, 1)
|
||||
end
|
||||
local s = assert(dec(assert(Qb.encode(v, { visibilityMask = true }))))
|
||||
local colors = ((s.voxels :: { Types.Voxels })[1]).colors :: buffer
|
||||
t:eq(buffer.readu8(colors, 13 * 4 + 3), 1, "hidden cell mask")
|
||||
t:eq(buffer.readu8(colors, 0 * 4 + 3), 1 + 2 + 16 + 64, "corner cell mask (left, bottom, back)")
|
||||
end
|
||||
|
||||
-- api and conversion to .vox
|
||||
do
|
||||
local s = assert(F.load(Fx.cases[1].data))
|
||||
t:check(s.kind == "scene", "F.load")
|
||||
t:check(F.save(s, nil, { format = "vox" }) ~= nil, "qb -> vox")
|
||||
t:check(F.save(s, nil, { format = "schem" }) ~= nil, "qb -> schem")
|
||||
end
|
||||
|
||||
-- errors
|
||||
do
|
||||
local _, e1 = dec("\1\1\0\0")
|
||||
t:check(e1 and e1.code == Err.TRUNCATED, "truncated header")
|
||||
local _, e2 = dec("\2\1\0\0" .. string.rep("\0", 20))
|
||||
t:check(e2 and e2.code == Err.CORRUPT, "bad version")
|
||||
local _, e3 = dec(string.sub(Fx.cases[1].data, 1, 60))
|
||||
t:check(e3 and e3.code == Err.TRUNCATED, "truncated matrix")
|
||||
local _, e4 = Qb.encode({} :: any)
|
||||
t:check(e4 and e4.code == Err.ARG, "encode nothing")
|
||||
end
|
||||
|
||||
local samples = {}
|
||||
for _, c in Fx.cases do
|
||||
table.insert(samples, c.data)
|
||||
end
|
||||
t:fuzz("decode", function(b: buffer)
|
||||
return Qb.decode(Bytes.reader(b))
|
||||
end, samples)
|
||||
t:done()
|
||||
@@ -0,0 +1,268 @@
|
||||
-- model/rbxm tests: binary models and places built by rojo 7.7 and lune 0.11 (rbx-dom) decode to the values lune reads
|
||||
-- from them (every property the files hold); the binary decode equals the decode of the XML twin; Zstandard and raw
|
||||
-- chunk variants; an unknown chunk and an unknown property type round trip raw; our rbxm output read back by lune
|
||||
-- equals the original (fixture verdicts); round trips of a tree with every type; options, errors and the fuzz pass.
|
||||
-- Fixtures: tools/fixtures/rbxmx.py.
|
||||
local T = require("@tests/T")
|
||||
local Rbxm = require("@formats/model/rbxm")
|
||||
local Rbxmx = require("@formats/model/rbxmx")
|
||||
local Rbx = require("@formats/codecs/model/Rbx")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Err = require("@formats/core/Err")
|
||||
local H = require("@tests/model/_rbx")
|
||||
local Fx = require("@tests/gen/rbx_files")
|
||||
local Json = require("@formats/codecs/text/Json")
|
||||
local Ours = require("@tests/gen/rbx_ours")
|
||||
|
||||
local t = T.suite("rbxm")
|
||||
|
||||
t:eq(Rbxm.name, "rbxm", "name")
|
||||
t:eq(Rbxm.category, "model", "category")
|
||||
t:check(table.find(Rbxm.extensions, "rbxl") ~= nil, "extensions")
|
||||
t:check(Rbxm.caps.decode and Rbxm.caps.encode, "caps")
|
||||
|
||||
local function decode(data: any, opts: any?): (any, any)
|
||||
return Rbxm.decode(Bytes.reader(if type(data) == "string" then buffer.fromstring(data) else data), opts)
|
||||
end
|
||||
local function file(name: string): any
|
||||
for _, f in Fx.files do
|
||||
if f.name == name then
|
||||
return f
|
||||
end
|
||||
end
|
||||
error("no fixture " .. name)
|
||||
end
|
||||
|
||||
-- every binary reference file against lune's reading of it, and against the decode of its XML twin
|
||||
local nprops = 0
|
||||
local trees: { [string]: any } = {}
|
||||
for _, f in Fx.files do
|
||||
if not f.xml then
|
||||
local tree, err = decode(f.data)
|
||||
t:check(tree ~= nil, f.name .. ": " .. tostring(err))
|
||||
if tree then
|
||||
trees[f.name] = tree
|
||||
t:check(tree.kind == "rbxtree" and tree.meta.source.format == "rbxm", f.name .. ": an RbxTree")
|
||||
t:eq(tree.place, f.place, f.name .. ": place")
|
||||
nprops += H.compare(t, tree, Json.decode(f.dump), f.name)
|
||||
end
|
||||
end
|
||||
end
|
||||
t:note("%d binary files, %d property values match lune", #Fx.files - 3, nprops)
|
||||
for _, pair in { { "model.rbxm", "model.rbxmx" }, { "place.rbxl", "place.rbxlx" }, { "lune.rbxm", "lune.rbxmx" } } do
|
||||
local xt = Rbxmx.decode(Bytes.reader(buffer.fromstring(file(pair[2]).data)))
|
||||
local n = H.sameTree(t, xt, trees[pair[1]], pair[1] .. " vs " .. pair[2], { loose = true })
|
||||
t:check(n > 5, pair[1] .. ": instances compared")
|
||||
end
|
||||
|
||||
-- the typed property folder (tools/fixtures/rbxmx.py TYPES_FOLDER), value by value
|
||||
do
|
||||
local tree = trees["model.rbxm"]
|
||||
local f = Rbx.find(tree, "Kit/Types") :: any
|
||||
t:check(f ~= nil, "Kit/Types")
|
||||
local P = f.properties
|
||||
local function v(name: string, typ: string): any
|
||||
local x = P[name]
|
||||
t:check(x ~= nil and x.type == typ, name .. " is " .. typ .. " (" .. tostring(x and x.type) .. ")")
|
||||
return x and x.value
|
||||
end
|
||||
t:eq(v("PString", "String"), "hello <world> & \"quotes\" \u{e9}\u{65e5}", "String")
|
||||
t:eq(v("PBinary", "String"), "\0\1\2\3\4\255", "BinaryString bytes")
|
||||
t:eq(v("PBool", "Bool"), true, "Bool")
|
||||
t:eq(v("PInt32", "Int32"), -123, "Int32")
|
||||
t:eq(v("PInt64", "Int64"), -1234567890123, "Int64")
|
||||
t:eq(v("PFloat32", "Float32"), 1.5, "Float32")
|
||||
t:eq(v("PFloat64", "Float64"), 3.141592653589793, "Float64")
|
||||
t:eq(table.concat(v("PUDim2", "UDim2"), ","), "0.5,10,0.25,-3", "UDim2")
|
||||
t:eq(table.concat(v("PRay", "Ray"), ","), "1,2,3,4,5,6", "Ray")
|
||||
t:eq(v("PFaces", "Faces"), 1 + 2 + 32, "Faces")
|
||||
t:eq(v("PAxes", "Axes"), 1 + 4, "Axes")
|
||||
t:eq(v("PBrickColor", "BrickColor"), 21, "BrickColor")
|
||||
t:eq(table.concat(v("PVector3int16", "Vector3int16"), ","), "-1,2,300", "Vector3int16")
|
||||
t:eq(table.concat(v("PCFrame", "CFrame"), ","), "1,2,3,0,0,1,0,1,0,-1,0,0", "CFrame (rotation id)")
|
||||
t:check(v("POptCFrame", "OptionalCFrame")[1] == 1, "OptionalCFrame")
|
||||
t:check(P.POptNone and P.POptNone.type == "OptionalCFrame" and P.POptNone.value == nil, "OptionalCFrame none")
|
||||
t:eq(v("PEnum", "Enum"), 3, "Enum")
|
||||
t:eq(#v("PNumberSequence", "NumberSequence"), 3, "NumberSequence keypoints")
|
||||
t:eq(v("PNumberSequence", "NumberSequence")[2][2], -3, "NumberSequence value")
|
||||
t:eq(v("PColorSequence", "ColorSequence")[2][4], 1, "ColorSequence")
|
||||
t:eq(table.concat(v("PNumberRange", "NumberRange"), ","), "1,5", "NumberRange")
|
||||
t:eq(table.concat(v("PRect", "Rect"), ","), "1,2,3,4", "Rect")
|
||||
t:eq(table.concat(v("PPhys", "PhysicalProperties"), ","), "1,0.5,0.25,2,3,0.75", "PhysicalProperties")
|
||||
t:check(P.PPhysDefault and P.PPhysDefault.value == nil, "PhysicalProperties default")
|
||||
local font = v("PFont", "Font")
|
||||
t:check(font.family == "rbxasset://fonts/families/Arial.json" and font.weight == 700 and font.style == "Italic", "Font")
|
||||
t:eq(v("PUniqueId", "UniqueId"), "44b188dace632b4702e9c68d004815fc", "UniqueId")
|
||||
t:eq(v("PUniqueId2", "UniqueId"), "7fb188dace632b4702e9c68d004815fd", "UniqueId (top bits)")
|
||||
t:eq(v("PSecurity", "SecurityCapabilities"), 12345, "SecurityCapabilities")
|
||||
t:eq(v("PContent", "Content"), "rbxassetid://1234", "Content uri")
|
||||
t:check(P.PContentNone and P.PContentNone.value == nil, "Content none")
|
||||
t:eq(v("PContentId", "String"), "rbxassetid://99", "ContentId")
|
||||
t:eq(v("PShared", "SharedString"), "\0\1\2\3", "SharedString")
|
||||
local types2 = Rbx.find(tree, "Kit/Types2") :: any
|
||||
t:eq(types2.properties.PShared.value, "\0\1\2\3", "SharedString shared by two instances")
|
||||
local ptr = Rbx.find(tree, "Kit/Pointer") :: any
|
||||
t:check(ptr.properties.Value.type == "Ref" and ptr.properties.Value.value == Rbx.find(tree, "Kit/Base"), "Ref")
|
||||
t:check(string.find(Rbx.get(Rbx.find(tree, "Kit/Code") :: any, "Source"), "]]>", 1, true) ~= nil, "Source")
|
||||
t:eq(Rbx.fullName(Rbx.find(tree, "Kit/Gui/Frame/Label") :: any), "Kit.Gui.Frame.Label", "fullName")
|
||||
t:eq(#Rbx.find(tree, "Kit/Parts").children, 120, "120 parts")
|
||||
t:eq(#Rbx.list(tree), 1 + 12 + 2 + 120, "every instance")
|
||||
end
|
||||
|
||||
-- chunk variants: Zstandard, raw, an unknown chunk and an unknown property type
|
||||
do
|
||||
local base = trees["model.rbxm"]
|
||||
t:eq(table.concat(base.meta.source.compression, ","), "lz4,none", "rojo: lz4 chunks (END raw)")
|
||||
for _, name in { "model-zstd.rbxm", "model-raw.rbxm" } do
|
||||
local tree = trees[name]
|
||||
H.sameTree(t, base, tree, name)
|
||||
end
|
||||
t:eq(table.concat(trees["model-zstd.rbxm"].meta.source.compression, ","), "none,zstd", "zstd chunks")
|
||||
local u = trees["model-unknown.rbxm"]
|
||||
H.sameTree(t, base, u, "model-unknown.rbxm", { skipRaw = true })
|
||||
t:check(#u.extra == 1 and u.extra[1].name == "ZZZZ", "the unknown chunk is kept")
|
||||
local raws = 0
|
||||
for inst in Rbx.descendants(u) do
|
||||
local m = (inst :: any).properties.Mystery
|
||||
if m then
|
||||
raws += 1
|
||||
t:check(m.type == "Raw" and m.value.typeId == 0x7f, "unknown property type kept raw")
|
||||
end
|
||||
end
|
||||
t:check(raws > 0, "instances with the raw property: " .. raws)
|
||||
local again = decode(Rbxm.encode(u) :: buffer)
|
||||
t:check(again and #again.extra == 1 and buffer.tostring(again.extra[1].data) == buffer.tostring(u.extra[1].data), "unknown chunk round trip")
|
||||
H.sameTree(t, u, again, "unknown round trip")
|
||||
-- an edited class (an instance more) drops the raw column instead of writing it wrong
|
||||
u = decode(file("model-unknown.rbxm").data)
|
||||
local cls
|
||||
for inst in Rbx.descendants(u) do
|
||||
if (inst :: any).properties.Mystery then
|
||||
cls = (inst :: any).className
|
||||
break
|
||||
end
|
||||
end
|
||||
Rbx.add(u.roots[1], Rbx.instance(cls, "Added"))
|
||||
local edited = decode(Rbxm.encode(u) :: buffer)
|
||||
local still = false
|
||||
for inst in Rbx.descendants(edited) do
|
||||
if (inst :: any).properties.Mystery then
|
||||
still = true
|
||||
end
|
||||
end
|
||||
t:check(edited and not still, "a raw column is dropped when its class changes")
|
||||
end
|
||||
|
||||
-- our encoder: lune reads our rbxm back to the same values (tools/fixtures/rbxmx.py)
|
||||
local nours = 0
|
||||
for _, v in Ours do
|
||||
if string.sub(v.name, -5) == "/rbxm" then
|
||||
t:check(v.ok, v.name .. ": " .. v.detail)
|
||||
nours += 1
|
||||
end
|
||||
end
|
||||
t:note("%d of our rbxm files read back by lune", nours)
|
||||
|
||||
-- round trips: every reference file, and a tree with every type
|
||||
for name, tree in trees do
|
||||
local out, err = Rbxm.encode(tree)
|
||||
t:check(out ~= nil, name .. ": encode " .. tostring(err))
|
||||
if out then
|
||||
local back = decode(out)
|
||||
H.sameTree(t, tree, back, name .. ": round trip")
|
||||
t:eq(back.place, tree.place, name .. ": place round trip")
|
||||
end
|
||||
end
|
||||
do
|
||||
local tree = H.everything()
|
||||
local out = Rbxm.encode(tree) :: buffer
|
||||
local back = decode(out)
|
||||
H.sameTree(t, tree, back, "every type", { loose = true })
|
||||
local raw = Rbxm.encode(tree, { compression = "none" }) :: buffer
|
||||
local back2 = decode(raw)
|
||||
H.sameTree(t, tree, back2, "every type, raw chunks", { loose = true })
|
||||
t:eq(table.concat(back2.meta.source.compression, ","), "none", "compression = none")
|
||||
t:check(buffer.len(raw) > buffer.len(out), "lz4 chunks are smaller")
|
||||
-- through XML and back
|
||||
local viaXml = Rbxmx.decode(Bytes.reader(Rbxmx.encode(back) :: buffer))
|
||||
H.sameTree(t, tree, decode(Rbxm.encode(viaXml) :: buffer), "every type via rbxmx", { loose = true })
|
||||
-- a place: roots marked as services
|
||||
local place = Rbx.tree(true)
|
||||
Rbx.add(place, Rbx.instance("Workspace"))
|
||||
Rbx.add(place, Rbx.instance("Lighting"))
|
||||
local pb = decode(Rbxm.encode(place) :: buffer)
|
||||
t:check(pb.place and pb.roots[1].service and pb.roots[2].service, "place services")
|
||||
local pm = decode(Rbxm.encode(place, { place = false }) :: buffer)
|
||||
t:check(not pm.place, "place = false")
|
||||
tree.metadata.ExplicitAutoJoints = "true"
|
||||
t:eq(decode(Rbxm.encode(tree) :: buffer).metadata.ExplicitAutoJoints, "true", "META")
|
||||
end
|
||||
|
||||
-- sniff, errors
|
||||
do
|
||||
local data = file("model.rbxm").data
|
||||
t:eq(Rbxm.sniff(Bytes.reader(buffer.fromstring(data))), 1, "sniff")
|
||||
t:eq(Rbxm.sniff(Bytes.reader(buffer.fromstring(file("model.rbxmx").data))), 0, "sniff XML")
|
||||
local _, e1 = decode("<roblox!\137\255\13\10\26\10")
|
||||
t:check(e1 and e1.code == Err.TRUNCATED, "header only: " .. tostring(e1))
|
||||
local _, e2 = decode("<roblox?" .. string.rep("\0", 40))
|
||||
t:check(e2 and e2.code == Err.CORRUPT, "bad magic: " .. tostring(e2))
|
||||
local _, e3 = decode(string.sub(data, 1, #data - 25))
|
||||
t:check(e3 and e3.code == Err.TRUNCATED, "no END: " .. tostring(e3))
|
||||
local _, e4 = decode(data, { maxInstances = 10 })
|
||||
t:check(e4 and e4.code == Err.LIMIT, "maxInstances: " .. tostring(e4))
|
||||
local _, e5 = Rbxm.encode({} :: any)
|
||||
t:check(e5 and e5.code == Err.ARG, "encode a non-tree")
|
||||
local bad = Rbx.tree()
|
||||
local x, y = Rbx.instance("Folder", "x"), Rbx.instance("Folder", "y")
|
||||
Rbx.set(x, "P", "Int32", 1)
|
||||
Rbx.set(y, "P", "Vector3", { 1, 2, 3 })
|
||||
Rbx.add(bad, x)
|
||||
Rbx.add(bad, y)
|
||||
local _, e6 = Rbxm.encode(bad)
|
||||
t:check(e6 and e6.code == Err.ARG, "mixed property types: " .. tostring(e6))
|
||||
Rbx.set(y, "P", "Nope", 1)
|
||||
local _, e7 = Rbxm.encode(bad)
|
||||
t:check(e7 and e7.code == Err.ARG, "unknown type: " .. tostring(e7))
|
||||
-- a PRNT cycle (A's parent is B, B's parent is A)
|
||||
local cyc = Rbx.tree()
|
||||
local a = Rbx.add(cyc, Rbx.instance("Folder", "A"))
|
||||
Rbx.add(a, Rbx.instance("Folder", "B"))
|
||||
local raw = Rbxm.encode(cyc, { compression = "none" }) :: buffer
|
||||
-- the PRNT body: version, count 2, children [0, 1], parents [-1, 0] -> parents [1, 0]
|
||||
local s = buffer.tostring(raw)
|
||||
local at = string.find(s, "PRNT", 1, true) :: number
|
||||
local body = at - 1 + 16
|
||||
-- parents are the second referent array: zigzag deltas (1, -1) = (2, 1), interleaved big-endian
|
||||
buffer.writeu8(raw, body + 5 + 8 + 6, 2)
|
||||
buffer.writeu8(raw, body + 5 + 8 + 7, 1)
|
||||
local _, e8 = decode(raw)
|
||||
t:check(e8 and e8.code == Err.CORRUPT, "a cycle: " .. tostring(e8))
|
||||
end
|
||||
|
||||
-- through the api: detection (variant: rbxl for places), load, save, convert
|
||||
do
|
||||
local F = require("@formats")
|
||||
t:eq(F.detect(file("place.rbxl").data), "rbxl", "detect a place")
|
||||
t:eq(F.detect(file("model.rbxm").data), "rbxm", "detect a model")
|
||||
t:eq(F.detect(file("place.rbxlx").data), "rbxlx", "detect an XML place")
|
||||
t:eq(F.detect(file("model.rbxmx").data), "rbxmx", "detect an XML model")
|
||||
local tree = F.load(file("model.rbxm").data)
|
||||
t:check(tree and tree.kind == "rbxtree", "F.load")
|
||||
local again = F.save(tree)
|
||||
t:check(type(again) == "buffer" and Rbxm.sniff(Bytes.reader(again)) == 1, "F.save keeps the source format")
|
||||
local x = F.convert(file("model.rbxm").data, "rbxmx")
|
||||
t:check(type(x) == "buffer" and Rbxmx.sniff(Bytes.reader(x)) == 1, "F.convert to rbxmx")
|
||||
local l = F.save(tree, nil, { format = "rbxl" })
|
||||
t:eq(F.detect(l), "rbxl", "save as rbxl marks services")
|
||||
end
|
||||
|
||||
-- fuzz
|
||||
do
|
||||
local samples = { file("model-raw.rbxm").data, Rbxm.encode(H.everything(), { compression = "none" }) :: buffer, Rbxm.encode(H.everything()) :: buffer }
|
||||
t:fuzz("decode", function(b: buffer)
|
||||
return Rbxm.decode(Bytes.reader(b))
|
||||
end, samples, { mutations = 150, garbage = 300, keepHeader = 32 })
|
||||
end
|
||||
|
||||
t:done()
|
||||
@@ -0,0 +1,188 @@
|
||||
-- model/rbxmx tests: XML models and places built by rojo 7.7 and lune 0.11 (rbx-dom) decode to the values lune reads
|
||||
-- from them; our rbxmx output read back by lune equals the original (fixture verdicts); round trips of every reference
|
||||
-- file and of a tree with every type; XML specifics (BinaryString, CDATA, packed Color3, INF / NAN, unknown property
|
||||
-- tags kept raw, referents, SharedStrings, floats written exactly); errors and the fuzz pass.
|
||||
-- Fixtures: tools/fixtures/rbxmx.py.
|
||||
local T = require("@tests/T")
|
||||
local Rbxmx = require("@formats/model/rbxmx")
|
||||
local Rbx = require("@formats/codecs/model/Rbx")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Err = require("@formats/core/Err")
|
||||
local H = require("@tests/model/_rbx")
|
||||
local Fx = require("@tests/gen/rbx_files")
|
||||
local Json = require("@formats/codecs/text/Json")
|
||||
local Ours = require("@tests/gen/rbx_ours")
|
||||
|
||||
local t = T.suite("rbxmx")
|
||||
|
||||
t:eq(Rbxmx.name, "rbxmx", "name")
|
||||
t:eq(Rbxmx.category, "model", "category")
|
||||
t:check(table.find(Rbxmx.extensions, "rbxlx") ~= nil, "extensions")
|
||||
t:check(Rbxmx.caps.decode and Rbxmx.caps.encode, "caps")
|
||||
|
||||
local function decode(data: any, opts: any?): (any, any)
|
||||
return Rbxmx.decode(Bytes.reader(if type(data) == "string" then buffer.fromstring(data) else data), opts)
|
||||
end
|
||||
local function encode(tree: any, opts: any?): string
|
||||
return buffer.tostring(Rbxmx.encode(tree, opts) :: buffer)
|
||||
end
|
||||
|
||||
local nprops = 0
|
||||
local trees: { [string]: any } = {}
|
||||
for _, f in Fx.files do
|
||||
if f.xml then
|
||||
local tree, err = decode(f.data)
|
||||
t:check(tree ~= nil, f.name .. ": " .. tostring(err))
|
||||
if tree then
|
||||
trees[f.name] = tree
|
||||
t:check(tree.kind == "rbxtree" and tree.meta.source.format == "rbxmx", f.name .. ": an RbxTree")
|
||||
t:eq(tree.place, f.place, f.name .. ": place")
|
||||
nprops += H.compare(t, tree, Json.decode(f.dump), f.name)
|
||||
end
|
||||
end
|
||||
end
|
||||
t:note("3 XML files, %d property values match lune", nprops)
|
||||
|
||||
do
|
||||
local tree = trees["model.rbxmx"]
|
||||
local P = (Rbx.find(tree, "Kit/Types") :: any).properties
|
||||
t:eq(P.PBinary.type, "BinaryString", "BinaryString (CDATA)")
|
||||
t:eq(P.PBinary.value, "\0\1\2\3\4\255", "BinaryString bytes")
|
||||
t:eq(P.PContentId.type, "ContentId", "ContentId")
|
||||
t:eq(P.PFloat32.value, 1.5, "float")
|
||||
t:eq(P.PUniqueId.value, "44b188dace632b4702e9c68d004815fc", "UniqueId")
|
||||
t:eq(P.PShared.value, "\0\1\2\3", "SharedString")
|
||||
t:eq(P.PBrickColor.type, "Int32", "a BrickColor of an unknown property reads as its <int>")
|
||||
local base = (Rbx.find(tree, "Kit/Base") :: any).properties
|
||||
t:eq(base.Tags.value, "Enemy\0Boss", "Tags")
|
||||
t:check(#base.AttributesSerialize.value > 10, "AttributesSerialize")
|
||||
local lune = trees["lune.rbxmx"]
|
||||
t:eq((Rbx.find(lune, "Kit/Brick") :: any).properties.Value.type, "BrickColor", "BrickColorValue.Value is a BrickColor")
|
||||
end
|
||||
|
||||
local nours = 0
|
||||
for _, v in Ours do
|
||||
if string.sub(v.name, -6) == "/rbxmx" then
|
||||
t:check(v.ok, v.name .. ": " .. v.detail)
|
||||
nours += 1
|
||||
end
|
||||
end
|
||||
t:note("%d of our rbxmx files read back by lune", nours)
|
||||
|
||||
for name, tree in trees do
|
||||
local back = decode(encode(tree))
|
||||
H.sameTree(t, tree, back, name .. ": round trip")
|
||||
t:eq(back.place, tree.place, name .. ": place round trip")
|
||||
end
|
||||
do
|
||||
local tree = H.everything()
|
||||
local s = encode(tree)
|
||||
H.sameTree(t, tree, decode(s), "every type", { loose = true })
|
||||
t:check(string.find(s, '<roblox version="4">', 1, true) == 1, "rbx-dom's root element")
|
||||
t:check(string.find(s, "<SharedStrings>", 1, true) ~= nil, "SharedStrings")
|
||||
local again = encode(decode(s))
|
||||
t:eq(again, s, "a second write is byte-identical")
|
||||
local flat = encode(tree, { indent = 0 })
|
||||
t:check(not string.find(flat, "\n <Item", 1, true) and decode(flat) ~= nil, "indent = 0")
|
||||
end
|
||||
|
||||
-- XML specifics
|
||||
do
|
||||
local doc = [==[<?xml version="1.0" encoding="utf-8"?>
|
||||
<roblox xmlns:xmime="http://www.w3.org/2005/05/xmlmime" version="4">
|
||||
<Meta name="ExplicitAutoJoints">true</Meta>
|
||||
<External>null</External>
|
||||
<Item class="Part" referent="RBXA">
|
||||
<Properties>
|
||||
<string name="Name"><![CDATA[Odd <name>]]></string>
|
||||
<Color3 name="Color">4294934528</Color3>
|
||||
<float name="Inf">INF</float>
|
||||
<double name="NegInf">-INF</double>
|
||||
<float name="Nan">NAN</float>
|
||||
<Ref name="Self">RBXA</Ref>
|
||||
<Ref name="None">null</Ref>
|
||||
<BinaryString name="Data">
|
||||
AAEC
|
||||
AwQ=
|
||||
</BinaryString>
|
||||
<Content name="Old"><url>rbxasset://textures/a.png</url></Content>
|
||||
<Content name="OldNull"><null></null></Content>
|
||||
<FutureType name="Mystery"><A>1</A><B x="2"/></FutureType>
|
||||
<PhysicalProperties name="Phys"><CustomPhysics>true</CustomPhysics><Density>2</Density><Friction>0.3</Friction><Elasticity>0.5</Elasticity><FrictionWeight>1</FrictionWeight><ElasticityWeight>1</ElasticityWeight></PhysicalProperties>
|
||||
</Properties>
|
||||
</Item>
|
||||
</roblox>]==]
|
||||
local tree, err = decode(doc)
|
||||
t:check(tree ~= nil, "a hand-written file: " .. tostring(err))
|
||||
local p = tree.roots[1]
|
||||
t:eq(p.name, "Odd <name>", "CDATA name")
|
||||
local c = p.properties.Color.value
|
||||
t:check(c[1] == 1 and math.abs(c[2] - 0x80 / 255) < 1e-6 and c[3] == 0, "packed ARGB Color3")
|
||||
t:eq(p.properties.Inf.value, math.huge, "INF")
|
||||
t:eq(p.properties.NegInf.value, -math.huge, "-INF")
|
||||
t:check(p.properties.Nan.value ~= p.properties.Nan.value, "NAN")
|
||||
t:check(p.properties.Self.value == p, "Ref to itself")
|
||||
t:check(p.properties.None.type == "Ref" and p.properties.None.value == nil, "Ref null")
|
||||
t:eq(p.properties.Data.value, "\0\1\2\3\4", "BinaryString with whitespace")
|
||||
t:eq(p.properties.Old.type, "ContentId", "legacy <Content><url>")
|
||||
t:eq(p.properties.Old.value, "rbxasset://textures/a.png", "url")
|
||||
t:eq(p.properties.Mystery.type, "XmlRaw", "unknown tag kept")
|
||||
t:eq(p.properties.Phys.value[6], 1, "acoustic absorption defaults to 1")
|
||||
t:eq(tree.metadata.ExplicitAutoJoints, "true", "Meta")
|
||||
t:eq(#tree.extra, 1, "<External> kept")
|
||||
local out = encode(tree)
|
||||
t:check(string.find(out, '<FutureType name="Mystery"><A>1</A><B x="2"></B></FutureType>', 1, true) ~= nil
|
||||
or string.find(out, "<FutureType", 1, true) ~= nil, "unknown tag written back")
|
||||
local back = decode(out)
|
||||
t:eq(back.roots[1].properties.Mystery.type, "XmlRaw", "unknown tag round trip")
|
||||
t:eq(back.roots[1].properties.Inf.value, math.huge, "INF round trip")
|
||||
t:check(back.roots[1].properties.Self.value == back.roots[1], "Ref round trip")
|
||||
t:check(string.find(out, '<Meta name="ExplicitAutoJoints">true</Meta>', 1, true) ~= nil, "Meta written")
|
||||
-- a String with bytes XML cannot hold is written as a BinaryString
|
||||
local raw = Rbx.tree()
|
||||
local f = Rbx.add(raw, Rbx.instance("Folder"))
|
||||
Rbx.set(f, "Blob", "String", "a\0b\255")
|
||||
local rs = encode(raw)
|
||||
t:check(string.find(rs, '<BinaryString name="Blob">', 1, true) ~= nil, "binary bytes as BinaryString")
|
||||
t:eq(decode(rs).roots[1].properties.Blob.value, "a\0b\255", "binary bytes round trip")
|
||||
-- floats: the shortest text that reads back exactly
|
||||
local fl = Rbx.tree()
|
||||
local g = Rbx.add(fl, Rbx.instance("Folder"))
|
||||
Rbx.set(g, "A", "Float64", 0.1)
|
||||
Rbx.set(g, "B", "Float32", 0.1)
|
||||
Rbx.set(g, "C", "Vector3", { 1 / 3, 1e-30, -2 ^ 100 })
|
||||
local fs = encode(fl)
|
||||
t:check(string.find(fs, '<double name="A">0.1</double>', 1, true) ~= nil, "double 0.1")
|
||||
t:check(string.find(fs, '<float name="B">0.1</float>', 1, true) ~= nil, "float 0.1")
|
||||
local fb = decode(fs).roots[1].properties
|
||||
t:eq(fb.A.value, 0.1, "double exact")
|
||||
local v = buffer.create(4)
|
||||
buffer.writef32(v, 0, 1 / 3)
|
||||
t:eq(fb.C.value[1], buffer.readf32(v, 0), "f32 exact")
|
||||
end
|
||||
|
||||
-- sniff, errors
|
||||
do
|
||||
local data = Fx.files[2].data
|
||||
t:eq(Rbxmx.sniff(Bytes.reader(buffer.fromstring(data))), 1, "sniff")
|
||||
t:eq(Rbxmx.sniff(Bytes.reader(buffer.fromstring("<?xml version='1.0'?>\n<roblox version=\"4\">"))), 1, "sniff with a declaration")
|
||||
t:eq(Rbxmx.sniff(Bytes.reader(buffer.fromstring(Fx.files[1].data))), 0, "sniff binary")
|
||||
t:eq(Rbxmx.sniff(Bytes.reader(buffer.fromstring("<svg/>"))), 0, "sniff other XML")
|
||||
local _, e1 = decode("<svg></svg>")
|
||||
t:check(e1 and e1.code == Err.CORRUPT, "not <roblox>: " .. tostring(e1))
|
||||
local _, e2 = decode("<roblox version=\"4\"><Item class=\"Part\">")
|
||||
t:check(e2 and e2.code == Err.TRUNCATED, "truncated: " .. tostring(e2))
|
||||
local _, e3 = decode("<roblox><Item><Properties/></Item></roblox>")
|
||||
t:check(e3 and e3.code == Err.CORRUPT, "an Item without a class: " .. tostring(e3))
|
||||
local _, e4 = Rbxmx.encode({ kind = "scene" } :: any)
|
||||
t:check(e4 and e4.code == Err.ARG, "encode a non-tree")
|
||||
end
|
||||
|
||||
do
|
||||
local samples = { Fx.files[2].data, encode(H.everything()) }
|
||||
t:fuzz("decode", function(b: buffer)
|
||||
return Rbxmx.decode(Bytes.reader(b))
|
||||
end, samples, { mutations = 80, garbage = 300 })
|
||||
end
|
||||
|
||||
t:done()
|
||||
@@ -0,0 +1,202 @@
|
||||
-- model/robloxmesh tests: meshes of every version written by the fixture's spec writer (1.00 / 1.01 ASCII, 2.00 with
|
||||
-- 36- and 40-byte vertices, 3.00 / 3.01 LODs, 4.00 / 4.01 / 5.00 skinned with one and several subsets, 5.00 FACS,
|
||||
-- 6.00 / 7.00 chunked) decode to the values they were written from (attribute CRCs, LOD index CRCs, global joints and
|
||||
-- weights, bone names / parents / world transforms, FACS, raw chunks); a Draco COREMESH is UNSUPPORTED; our 2.00 /
|
||||
-- 4.00 output read back by the fixture's reader (verdicts); round trips; errors and the fuzz pass.
|
||||
-- Fixtures: tools/fixtures/robloxmesh.py.
|
||||
local T = require("@tests/T")
|
||||
local RobloxMesh = require("@formats/model/robloxmesh")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Crc32 = require("@formats/core/Crc32")
|
||||
local Err = require("@formats/core/Err")
|
||||
local Types = require("@formats/Types")
|
||||
local Mat4 = require("@formats/codecs/model/Mat4")
|
||||
local Fx = require("@tests/gen/robloxmesh")
|
||||
|
||||
local t = T.suite("robloxmesh")
|
||||
|
||||
t:eq(RobloxMesh.name, "robloxmesh", "name")
|
||||
t:eq(RobloxMesh.category, "model", "category")
|
||||
t:check(table.find(RobloxMesh.extensions, "mesh") ~= nil, "extensions")
|
||||
t:check(RobloxMesh.caps.decode and RobloxMesh.caps.encode, "caps")
|
||||
|
||||
local function decode(data: any, opts: any?): (any, any)
|
||||
return RobloxMesh.decode(Bytes.reader(if type(data) == "string" then buffer.fromstring(data) else data), opts)
|
||||
end
|
||||
|
||||
-- a world matrix of node i (parents through the children lists)
|
||||
local function world(scene: any, i: number): { number }
|
||||
local parent = {}
|
||||
for k, n in scene.nodes do
|
||||
for _, c in n.children do
|
||||
parent[c] = k
|
||||
end
|
||||
end
|
||||
local m = Types.Scene.nodeMatrix(scene.nodes[i])
|
||||
local p = parent[i]
|
||||
while p do
|
||||
m = Mat4.mul(Types.Scene.nodeMatrix(scene.nodes[p]), m)
|
||||
p = parent[p]
|
||||
end
|
||||
return m
|
||||
end
|
||||
|
||||
local ncases = 0
|
||||
for _, c in Fx.cases do
|
||||
local scene, err = decode(c.data)
|
||||
if c.unsupported then
|
||||
t:check(scene == nil and err and err.code == Err.UNSUPPORTED, c.name .. ": unsupported (" .. tostring(err) .. ")")
|
||||
continue
|
||||
end
|
||||
t:check(scene ~= nil, c.name .. ": " .. tostring(err))
|
||||
if not scene then
|
||||
continue
|
||||
end
|
||||
ncases += 1
|
||||
local prim = scene.meshes[1].primitives[1]
|
||||
local a = prim.attributes
|
||||
t:eq(scene.meta.source.version, c.version, c.name .. ": version")
|
||||
t:eq(a.POSITION.count, c.vertices, c.name .. ": vertices")
|
||||
t:eq(Crc32.of(a.POSITION.data), c.pos, c.name .. ": POSITION")
|
||||
t:eq(Crc32.of(a.NORMAL.data), c.nrm, c.name .. ": NORMAL")
|
||||
t:eq(Crc32.of(a.TEXCOORD_0.data), c.uv, c.name .. ": TEXCOORD_0")
|
||||
if c.tan then
|
||||
t:check(a.TANGENT and Crc32.of(a.TANGENT.data) == c.tan, c.name .. ": TANGENT")
|
||||
end
|
||||
if c.col then
|
||||
t:check(a.COLOR_0 and a.COLOR_0.type == "unorm8" and Crc32.of(a.COLOR_0.data) == c.col, c.name .. ": COLOR_0")
|
||||
else
|
||||
t:check(a.COLOR_0 == nil, c.name .. ": no COLOR_0")
|
||||
end
|
||||
local lods = { prim }
|
||||
for _, l in (scene.meshes[1].extras or {}).lods or {} do
|
||||
table.insert(lods, l)
|
||||
end
|
||||
t:eq(#lods, #c.lods, c.name .. ": LODs")
|
||||
for k, want in c.lods do
|
||||
t:check(lods[k] and Crc32.of(lods[k].indices.data) == want, c.name .. ": LOD " .. (k - 1) .. " indices")
|
||||
end
|
||||
if c.joints then
|
||||
t:check(a.JOINTS_0 and Crc32.of(a.JOINTS_0.data) == c.joints, c.name .. ": JOINTS_0")
|
||||
t:check(a.WEIGHTS_0 and Crc32.of(a.WEIGHTS_0.data) == c.weights, c.name .. ": WEIGHTS_0")
|
||||
local skin = scene.skins[1]
|
||||
t:eq(#skin.joints, #c.bones, c.name .. ": joints")
|
||||
t:eq(skin.inverseBindMatrices.count, #c.bones, c.name .. ": inverse bind matrices")
|
||||
t:eq(scene.nodes[1].skin, 1, c.name .. ": the mesh node is skinned")
|
||||
local worst = 0
|
||||
for i, b in c.bones do
|
||||
local j = skin.joints[i]
|
||||
local node = scene.nodes[j]
|
||||
t:eq(node.name, b.name, c.name .. ": bone name")
|
||||
t:eq(node.extras.culling, b.culling, c.name .. ": culling")
|
||||
local isRoot = table.find(scene.roots, j) ~= nil
|
||||
t:eq(isRoot, b.parent == nil, c.name .. ": " .. b.name .. " root")
|
||||
if b.parent then
|
||||
t:check(table.find(scene.nodes[skin.joints[b.parent + 1]].children, j) ~= nil, c.name .. ": " .. b.name .. " parent")
|
||||
end
|
||||
local m = world(scene, j)
|
||||
local cf = b.cf
|
||||
local want = { cf[1], cf[4], cf[7], 0, cf[2], cf[5], cf[8], 0, cf[3], cf[6], cf[9], 0, cf[10], cf[11], cf[12], 1 }
|
||||
for k = 1, 16 do
|
||||
worst = math.max(worst, math.abs(m[k] - want[k]))
|
||||
end
|
||||
end
|
||||
t:check(worst < 1e-4, c.name .. ": bone world transforms (" .. worst .. ")")
|
||||
end
|
||||
if c.facs then
|
||||
t:check(scene.meta.source.facs and Crc32.of(scene.meta.source.facs) == c.facs, c.name .. ": FACS kept raw")
|
||||
end
|
||||
if c.chunks then
|
||||
t:eq(scene.meta.source.chunks[1].name, c.chunks[1], c.name .. ": raw chunk")
|
||||
end
|
||||
t:eq(scene.meta.up, "y", c.name .. ": up")
|
||||
end
|
||||
t:note("%d meshes match", ncases)
|
||||
|
||||
for _, v in Fx.ours do
|
||||
t:check(v.ok, v.name .. ": " .. v.detail)
|
||||
end
|
||||
t:note("%d of our files read back by the fixture's reader", #Fx.ours)
|
||||
|
||||
-- round trips through our encoder
|
||||
do
|
||||
for _, name in { "v2.00-40", "v4.00-lods", "v4.00-40bones", "v6.00" } do
|
||||
local c
|
||||
for _, x in Fx.cases do
|
||||
if x.name == name then
|
||||
c = x
|
||||
end
|
||||
end
|
||||
local s = decode(c.data)
|
||||
local out = RobloxMesh.encode(s, { version = "4.00" }) :: buffer
|
||||
local back = decode(out)
|
||||
t:check(back ~= nil, name .. ": re-read")
|
||||
local p0, p1 = s.meshes[1].primitives[1], back.meshes[1].primitives[1]
|
||||
t:eq(p1.indices.count, p0.indices.count, name .. ": triangles")
|
||||
if not c.joints then
|
||||
t:eq(Crc32.of(p1.attributes.POSITION.data), Crc32.of(p0.attributes.POSITION.data), name .. ": positions")
|
||||
t:eq(Crc32.of(p1.indices.data), Crc32.of(p0.indices.data), name .. ": indices")
|
||||
t:eq(Crc32.of(p1.attributes.TANGENT.data), Crc32.of(p0.attributes.TANGENT.data), name .. ": tangents")
|
||||
else
|
||||
t:eq(#back.skins[1].joints, #s.skins[1].joints, name .. ": joints")
|
||||
end
|
||||
local v2 = decode(RobloxMesh.encode(s) :: buffer)
|
||||
t:eq(v2.meta.source.version, "2.00", name .. ": 2.00 by default")
|
||||
end
|
||||
-- a glTF-style scene: a quad under a transformed node, no normals / colours
|
||||
local s = Types.Scene.new()
|
||||
local pos = buffer.create(48)
|
||||
for i, v in { 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0 } do
|
||||
buffer.writef32(pos, (i - 1) * 4, v)
|
||||
end
|
||||
local idx = buffer.create(24)
|
||||
for i, v in { 0, 1, 2, 0, 2, 3 } do
|
||||
buffer.writeu32(idx, (i - 1) * 4, v)
|
||||
end
|
||||
s.meshes[1] = { primitives = { { attributes = { POSITION = Types.Scene.accessor(pos, "f32", 3, 4) }, indices = Types.Scene.accessor(idx, "u32", 1, 6), mode = "triangles" } } }
|
||||
s.nodes[1] = { children = {}, mesh = 1, translation = { 10, 0, 0 } }
|
||||
s.roots[1] = 1
|
||||
local q = decode(RobloxMesh.encode(s) :: buffer)
|
||||
local qa = q.meshes[1].primitives[1].attributes
|
||||
t:eq(buffer.readf32(qa.POSITION.data, 12), 11, "node transform applied")
|
||||
t:eq(buffer.readf32(qa.NORMAL.data, 8), 1, "computed normal +Z")
|
||||
t:check(qa.COLOR_0 == nil, "white / no colours: 36-byte vertices")
|
||||
end
|
||||
|
||||
-- sniff, errors
|
||||
do
|
||||
local c = Fx.cases[3]
|
||||
t:eq(RobloxMesh.sniff(Bytes.reader(buffer.fromstring(c.data))), 1, "sniff")
|
||||
t:eq(RobloxMesh.sniff(Bytes.reader(buffer.fromstring("version 1.0"))), 0, "sniff short")
|
||||
t:eq(RobloxMesh.sniff(Bytes.reader(buffer.fromstring("solid cube\n"))), 0, "sniff STL")
|
||||
local _, e1 = decode("version 9.00\n")
|
||||
t:check(e1 and e1.code == Err.UNSUPPORTED, "version 9: " .. tostring(e1))
|
||||
local _, e2 = decode(string.sub(c.data, 1, 60))
|
||||
t:check(e2 and e2.code == Err.TRUNCATED, "truncated: " .. tostring(e2))
|
||||
local _, e3 = decode(c.data, { maxVertices = 10 })
|
||||
t:check(e3 and e3.code == Err.LIMIT, "maxVertices: " .. tostring(e3))
|
||||
local bad = buffer.fromstring(c.data)
|
||||
buffer.writeu32(bad, buffer.len(bad) - 4, 99999)
|
||||
local _, e4 = decode(bad)
|
||||
t:check(e4 and e4.code == Err.CORRUPT, "face index out of range: " .. tostring(e4))
|
||||
local _, e5 = decode("hello there, a mesh")
|
||||
t:check(e5 and e5.code == Err.CORRUPT, "no version line: " .. tostring(e5))
|
||||
local _, e6 = RobloxMesh.encode(Types.Scene.new())
|
||||
t:check(e6 and e6.code == Err.ARG, "an empty scene: " .. tostring(e6))
|
||||
local _, e7 = RobloxMesh.encode(decode(c.data), { version = "3.00" } :: any)
|
||||
t:check(e7 and e7.code == Err.ARG, "version 3.00 is not written")
|
||||
end
|
||||
|
||||
do
|
||||
local samples = {}
|
||||
for _, c in Fx.cases do
|
||||
if #c.data < 40000 then
|
||||
table.insert(samples, c.data)
|
||||
end
|
||||
end
|
||||
t:fuzz("decode", function(b: buffer)
|
||||
return RobloxMesh.decode(Bytes.reader(b))
|
||||
end, samples, { mutations = 30, garbage = 300, keepHeader = 13 })
|
||||
end
|
||||
|
||||
t:done()
|
||||
@@ -0,0 +1,221 @@
|
||||
-- model/schematic tests: .litematic files written by litemapy (several regions, negative sizes, a 40-state palette,
|
||||
-- tile entities), Sponge .schem v2 (litemapy) and v1 / v3 (nbtlib, by the spec; two-byte varints), MCEdit
|
||||
-- .schematic (legacy ids + data, WorldEdit AddBlocks, a Schematica name table): every cell's block state, placement,
|
||||
-- block entities; our re-encodings give the same NBT for the same variant, and the reference readers (litemapy /
|
||||
-- nbtlib, in the fixture step) read our output, across variants and from colour-only grids, to the same cells;
|
||||
-- detection, the api, error codes and the fuzz pass. Fixtures: tools/fixtures/schematic.py.
|
||||
local T = require("@tests/T")
|
||||
local F = require("@formats")
|
||||
local Schematic = require("@formats/model/schematic")
|
||||
local Mc = require("@formats/codecs/model/Minecraft")
|
||||
local Nbt = require("@formats/codecs/text/Nbt")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Err = require("@formats/core/Err")
|
||||
local Types = require("@formats/Types")
|
||||
local Fx = require("@tests/gen/schematic")
|
||||
local Ours = require("@tests/gen/schematic_ours")
|
||||
|
||||
local t = T.suite("schematic")
|
||||
|
||||
t:eq(Schematic.name, "schematic", "name")
|
||||
t:check(Schematic.caps.decode and Schematic.caps.encode, "caps")
|
||||
|
||||
local function dec(data: string | buffer, opts: any?): (Types.Scene?, any)
|
||||
local b = if type(data) == "string" then buffer.fromstring(data) else data
|
||||
return Schematic.decode(Bytes.reader(b), opts)
|
||||
end
|
||||
|
||||
-- the region record of node i: { name, min, size, cells (state per cell, "\n"-joined), legacy (id * 16 + data) }
|
||||
local function regions(scene: Types.Scene): { any }
|
||||
local out = {}
|
||||
for _, node in scene.nodes do
|
||||
local v = (scene.voxels :: { Types.Voxels })[node.voxels :: number]
|
||||
local b = v.blocks :: Types.VoxelBlocks
|
||||
local n = v.size[1] * v.size[2] * v.size[3]
|
||||
local cells, legacy = table.create(n), table.create(n)
|
||||
for i = 0, n - 1 do
|
||||
local st = b.palette[buffer.readu16(b.data, i * 2) + 1]
|
||||
cells[i + 1] = st
|
||||
local id, d = Mc.legacyId(st)
|
||||
legacy[i + 1] = if id then id * 16 + (d :: number) else -1
|
||||
end
|
||||
local tr = node.translation :: { number }
|
||||
table.insert(out, { name = node.name, min = tr, size = v.size, cells = table.concat(cells, "\n"), legacy = legacy, voxels = v, blockEntities = #(b.blockEntities or {}) })
|
||||
end
|
||||
return out
|
||||
end
|
||||
|
||||
local function sameRegion(got: any, want: any, label: string)
|
||||
t:eq(got.size[1] .. "," .. got.size[2] .. "," .. got.size[3], want.size[1] .. "," .. want.size[2] .. "," .. want.size[3], label .. ": size")
|
||||
t:eq(got.min[1] .. "," .. got.min[2] .. "," .. got.min[3], want.min[1] .. "," .. want.min[2] .. "," .. want.min[3], label .. ": minimum corner")
|
||||
if want.cells then
|
||||
t:eq(got.cells, want.cells, label .. ": cells")
|
||||
else
|
||||
local bad = 0
|
||||
for i, x in want.legacy do
|
||||
if got.legacy[i] ~= x then
|
||||
bad += 1
|
||||
end
|
||||
end
|
||||
t:eq(bad, 0, label .. ": legacy ids")
|
||||
end
|
||||
end
|
||||
|
||||
-- 1. reference files
|
||||
local decoded: { [string]: Types.Scene } = {}
|
||||
for _, c in Fx.cases do
|
||||
local scene, err = dec(c.data)
|
||||
t:check(scene ~= nil, c.name .. ": " .. tostring(err))
|
||||
local s = scene :: Types.Scene
|
||||
decoded[c.name] = s
|
||||
t:eq(s.meta.source.variant, c.variant, c.name .. ": variant")
|
||||
if c.version > 0 then
|
||||
t:eq(s.meta.source.version, c.version, c.name .. ": version")
|
||||
end
|
||||
local regs = regions(s)
|
||||
t:eq(#regs, #c.regions, c.name .. ": regions")
|
||||
local bes = 0
|
||||
for i, want in c.regions do
|
||||
sameRegion(regs[i], want, c.name .. " region " .. i)
|
||||
if want.name ~= "" then
|
||||
t:eq(regs[i].name, want.name, c.name .. " region " .. i .. ": name")
|
||||
end
|
||||
bes += regs[i].blockEntities
|
||||
end
|
||||
t:eq(bes, c.blockEntities, c.name .. ": block entities")
|
||||
-- colours: air is empty, the rest coloured from the default table
|
||||
local v = regs[1].voxels
|
||||
local b = v.blocks
|
||||
for i = 0, v.size[1] * v.size[2] * v.size[3] - 1 do
|
||||
local st = b.palette[buffer.readu16(b.data, i * 2) + 1]
|
||||
local idx = buffer.readu8(v.data, i)
|
||||
if Mc.isAir(st) ~= (idx == 0) then
|
||||
t:check(false, c.name .. ": cell " .. i .. " colour index " .. idx .. " for " .. st)
|
||||
end
|
||||
if idx > 0 then
|
||||
local col, p = Mc.color(st) :: { number }, v.palette[idx]
|
||||
if col[1] ~= p[1] or col[2] ~= p[2] or col[3] ~= p[3] then
|
||||
t:check(false, c.name .. ": cell " .. i .. " colour of " .. st)
|
||||
end
|
||||
end
|
||||
end
|
||||
-- the same variant again: the same NBT (every field), the same cells
|
||||
local out, eerr = Schematic.encode(s)
|
||||
t:check(out ~= nil, c.name .. ": encode " .. tostring(eerr))
|
||||
local again = assert(dec(out :: buffer))
|
||||
t:eq(Nbt.print(again.meta.source.nbt), Nbt.print(s.meta.source.nbt), c.name .. ": same NBT")
|
||||
t:eq(again.meta.source.rootName, s.meta.source.rootName, c.name .. ": root name")
|
||||
for i, r in regions(again) do
|
||||
sameRegion(r, regs[i], c.name .. " again region " .. i)
|
||||
end
|
||||
-- api: detection by content (the flavour) and loading
|
||||
t:eq(F.detect(c.data), c.variant, c.name .. ": F.detect")
|
||||
local viaApi = F.load(c.data)
|
||||
t:check(viaApi and viaApi.kind == "scene", c.name .. ": F.load")
|
||||
end
|
||||
|
||||
-- 2. our encodings read back by litemapy / nbtlib (fixture step)
|
||||
local outputs: { [string]: () -> (buffer?, any) } = {}
|
||||
local function colorGrid(): Types.Voxels
|
||||
local v = Types.Voxels.new(4, 3, 2)
|
||||
v.palette[1] = { 160, 40, 35, 255 }
|
||||
v.palette[2] = { 125, 125, 125, 255 }
|
||||
v.palette[3] = { 60, 110, 230, 120 }
|
||||
for i = 0, 4 * 3 * 2 - 1 do
|
||||
buffer.writeu8(v.data, i, i % 4)
|
||||
end
|
||||
return v
|
||||
end
|
||||
outputs["litemapy.litematic"] = function() return Schematic.encode(decoded.litemapy) end
|
||||
outputs["litemapy.schem"] = function() return Schematic.encode(decoded.litemapy, { variant = "schem", version = 2 }) end
|
||||
outputs["sponge2.schem"] = function() return Schematic.encode(decoded.sponge2) end
|
||||
outputs["sponge2.litematic"] = function() return Schematic.encode(decoded.sponge2, { variant = "litematic" }) end
|
||||
outputs["sponge3.schem"] = function() return Schematic.encode(decoded.sponge3) end
|
||||
outputs["sponge3v2.schem"] = function() return Schematic.encode(decoded.sponge3, { version = 2 }) end
|
||||
outputs["sponge1.schem"] = function() return Schematic.encode(decoded.sponge1) end
|
||||
outputs["mcedit.schematic"] = function() return Schematic.encode(decoded.mcedit) end
|
||||
outputs["worldedit_add.schematic"] = function() return Schematic.encode(decoded.worldedit_add) end
|
||||
outputs["sponge2.schematic"] = function() return Schematic.encode(decoded.sponge2, { variant = "schematic" }) end
|
||||
outputs["mcedit.schem"] = function() return Schematic.encode(decoded.mcedit, { variant = "schem" }) end
|
||||
outputs["colors.schem"] = function() return Schematic.encode(colorGrid()) end
|
||||
outputs["colors.litematic"] = function() return Schematic.encode(colorGrid(), { variant = "litematic" }) end
|
||||
outputs["colors.schematic"] = function() return Schematic.encode(colorGrid(), { variant = "schematic" }) end
|
||||
local count = 0
|
||||
for name, ref in Ours do
|
||||
local make = outputs[name]
|
||||
t:check(make ~= nil, name .. ": known output")
|
||||
local mine = assert(dec(assert(make())))
|
||||
local regs = regions(mine)
|
||||
t:eq(#regs, #ref.regions, name .. ": regions read back")
|
||||
for i, want in ref.regions do
|
||||
sameRegion(regs[i], want, name .. " (reference) region " .. i)
|
||||
end
|
||||
if ref.litemapy then
|
||||
t:eq(ref.litemapy, regs[1].cells, name .. ": litemapy's Sponge reader")
|
||||
end
|
||||
count += 1
|
||||
end
|
||||
t:eq(count, 14, "every output checked")
|
||||
-- the source cells survive the conversions
|
||||
do
|
||||
local function cellsVia(name: string): string
|
||||
return regions(assert(dec(assert(outputs[name]()))))[1].cells
|
||||
end
|
||||
t:eq(cellsVia("sponge2.litematic"), regions(decoded.sponge2)[1].cells, "schem -> litematic cells")
|
||||
t:eq(cellsVia("sponge3v2.schem"), regions(decoded.sponge3)[1].cells, "v3 -> v2 cells")
|
||||
local legacy = regions(assert(dec(assert(outputs["mcedit.schem"]()))))[1].legacy
|
||||
local src = regions(decoded.mcedit)[1].legacy
|
||||
t:eq(table.concat(legacy, ","), table.concat(src, ","), "schematic -> schem keeps legacy states")
|
||||
-- litematic -> schem merges the regions into one grid covering them all
|
||||
local merged = regions(assert(dec(assert(outputs["litemapy.schem"]()))))[1]
|
||||
t:eq(merged.size[1] .. "," .. merged.size[2] .. "," .. merged.size[3], "31,5,12", "merged size")
|
||||
-- colour grid -> blocks by colour
|
||||
local cg = regions(assert(dec(assert(outputs["colors.schem"]()))))[1]
|
||||
local first = string.split(cg.cells, "\n")
|
||||
t:eq(first[1], "minecraft:air", "empty cell -> air")
|
||||
t:eq(first[2], "minecraft:red_wool", "red -> red wool")
|
||||
t:eq(first[4], "minecraft:light_blue_stained_glass", "translucent blue -> stained glass")
|
||||
end
|
||||
|
||||
-- 3. through the api
|
||||
do
|
||||
local scene = assert(F.load(Fx.cases[1].data, { name = "x.litematic" }))
|
||||
local schem = assert(F.save(scene, nil, { format = "schem" })) :: buffer
|
||||
t:eq(F.detect(schem), "schem", "F.save format = schem")
|
||||
local lite = assert(F.save(scene, nil, { format = "litematic" })) :: buffer
|
||||
t:eq(F.detect(lite), "litematic", "F.save format = litematic")
|
||||
local legacy = assert(F.save(scene, nil, { format = "schematic", variant = "schematic" })) :: buffer
|
||||
t:eq(F.detect(legacy), "schematic", "variant schematic")
|
||||
t:eq(F.detect(legacy, { name = "a.schematic" }), "schematic", "detect with a name")
|
||||
-- a vox-like grid without blocks through F.save
|
||||
local g = Types.Voxels.new(2, 2, 2)
|
||||
buffer.writeu8(g.data, 0, 5)
|
||||
t:check(F.save(g, nil, { format = "schem" }) ~= nil, "save a bare Voxels")
|
||||
end
|
||||
|
||||
-- 4. errors
|
||||
do
|
||||
local _, e1 = dec(assert(Nbt.encode(Nbt.compound({ a = 1 }, { "a" }))))
|
||||
t:check(e1 and e1.code == Err.CORRUPT, "not a schematic")
|
||||
local _, e2 = dec(Fx.cases[1].data, { maxCells = 10 })
|
||||
t:check(e2 and e2.code == Err.LIMIT, "maxCells")
|
||||
local _, e3 = Schematic.encode({ kind = "scene" } :: any)
|
||||
t:check(e3 and e3.code == Err.ARG, "encode without voxels")
|
||||
local _, e4 = Schematic.encode(colorGrid(), { variant = "x" :: any })
|
||||
t:check(e4 and e4.code == Err.ARG, "unknown variant")
|
||||
local bad = Nbt.compound({ Width = Nbt.short(4), Height = Nbt.short(4), Length = Nbt.short(4), Blocks = buffer.create(10) }, { "Width", "Height", "Length", "Blocks" })
|
||||
local _, e5 = dec(assert(Nbt.encode(bad, { name = "Schematic" })))
|
||||
t:check(e5 and e5.code == Err.TRUNCATED, "short Blocks")
|
||||
end
|
||||
|
||||
-- 5. fuzz (the uncompressed NBT too, so mutations reach the layouts, not just gzip's CRC)
|
||||
local samples = {}
|
||||
for _, c in Fx.cases do
|
||||
local s = assert(dec(c.data))
|
||||
table.insert(samples, (assert(Nbt.encode(s.meta.source.nbt, { name = s.meta.source.rootName, compression = "none" }))))
|
||||
end
|
||||
table.insert(samples, Fx.cases[1].data)
|
||||
t:fuzz("decode", function(b: buffer)
|
||||
return Schematic.decode(Bytes.reader(b))
|
||||
end, samples)
|
||||
t:done()
|
||||
@@ -0,0 +1,105 @@
|
||||
-- video/fli tests: FLI (COLOR_64, BRUN, LC, BLACK, COPY, PSTAMP, a ring frame), FLC (prefix chunk, COLOR_256, SS2 with
|
||||
-- line skips and the last-byte opcode, an empty frame), an odd-size FLC, and 15 / 16 / 24-bit FLCs (BRUN, DTA_BRUN,
|
||||
-- DELTA, DTA_LC, COPY, DTA_COPY, BLACK), all encoded by the fixture script: every frame equals ffmpeg's decode
|
||||
-- (CRC-32 of rgba), frame rates, the ring frame option, seeking, errors, the fuzz pass. Fixtures: tools/fixtures/fli.py.
|
||||
local T = require("@tests/T")
|
||||
local F = require("@formats")
|
||||
local Fli = require("@formats/video/fli")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Crc32 = require("@formats/core/Crc32")
|
||||
local Err = require("@formats/core/Err")
|
||||
local Fx = require("@tests/gen/fli_cases")
|
||||
|
||||
local t = T.suite("fli")
|
||||
t:eq(Fli.name, "fli", "name")
|
||||
t:eq(Fli.category, "video", "category")
|
||||
|
||||
local function reader(s: string): Bytes.Reader
|
||||
return Bytes.reader(buffer.fromstring(s))
|
||||
end
|
||||
|
||||
for _, c in Fx do
|
||||
local label = c.name
|
||||
t:check(Fli.sniff(reader(c.data)) > 0, label .. ": sniff")
|
||||
t:eq(F.detect(buffer.fromstring(c.data)), "fli", label .. ": detect")
|
||||
local v, err = F.video.open(buffer.fromstring(c.data))
|
||||
t:check(v ~= nil, label .. ": open " .. tostring(err))
|
||||
if v then
|
||||
t:eq(v.codec, "flic", label .. ": codec")
|
||||
t:check(v.width == c.width and v.height == c.height, label .. ": size")
|
||||
t:check(math.abs(v.frameRate - c.rate) < 1e-9, label .. ": frame rate " .. v.frameRate)
|
||||
t:eq(v.track.bitsPerPixel, c.depth, label .. ": depth")
|
||||
local ring = v.demuxer.meta.ringFrame
|
||||
local want = if ring then #c.frames - 1 else #c.frames
|
||||
local n, bad = 0, 0
|
||||
while true do
|
||||
local f, ferr = v:nextFrame()
|
||||
if not f then
|
||||
t:check(ferr == nil, label .. ": " .. tostring(ferr))
|
||||
break
|
||||
end
|
||||
n += 1
|
||||
if f.image.format ~= "rgba8" or Crc32.of(f.image.pixels) ~= c.frames[n] then
|
||||
bad += 1
|
||||
end
|
||||
end
|
||||
t:check(n == want and bad == 0, string.format("%s: %d of %d frames equal ffmpeg's (%d differ)", label, n - bad, want, bad))
|
||||
v:seek(0.25)
|
||||
local f = v:nextFrame()
|
||||
local k = math.floor(0.25 * c.rate + 1 - 1e-9) + 1
|
||||
t:check(f ~= nil and Crc32.of(f.image.pixels) == c.frames[k], label .. ": seek to frame " .. k)
|
||||
if ring then
|
||||
local v2 = Fli.open(reader(c.data), { ringFrame = true })
|
||||
local m = 0
|
||||
while v2 and v2:nextFrame() do
|
||||
m += 1
|
||||
end
|
||||
t:eq(m, #c.frames, label .. ": with the ring frame")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- errors
|
||||
do
|
||||
local _, e = Fli.open(reader(string.sub(Fx[1].data, 1, 100)))
|
||||
t:check(e ~= nil and e.code == Err.TRUNCATED, "short header: TRUNCATED")
|
||||
local bad = Fx[1].data
|
||||
bad = string.sub(bad, 1, 4) .. "\0\0" .. string.sub(bad, 7)
|
||||
t:eq(Fli.sniff(reader(bad)), 0, "bad magic: no sniff")
|
||||
local _, e2 = Fli.open(reader(bad))
|
||||
t:check(e2 ~= nil and e2.code == Err.CORRUPT, "bad magic: CORRUPT")
|
||||
local big = string.sub(Fx[1].data, 1, 8) .. "\255\15\255\15" .. string.sub(Fx[1].data, 13)
|
||||
local _, e3 = Fli.open(reader(big), { maxPixels = 2 ^ 20 })
|
||||
t:check(e3 ~= nil and e3.code == Err.LIMIT, "maxPixels: LIMIT")
|
||||
end
|
||||
|
||||
local samples = {}
|
||||
for _, c in Fx do
|
||||
table.insert(samples, c.data)
|
||||
end
|
||||
t:fuzz("open", function(b: buffer)
|
||||
local v = Fli.open(Bytes.reader(b))
|
||||
if v then
|
||||
while v:nextFrame() do
|
||||
end
|
||||
end
|
||||
return v
|
||||
end, samples, { mutations = 12, garbage = 40, keepHeader = 16 })
|
||||
-- the frame decoder itself (codecs/video/Legacy/Flic, native: no pcall in between)
|
||||
local Flic = require("@formats/codecs/video/Legacy").Flic
|
||||
for _, c in { Fx[1], Fx[2], Fx[4], Fx[6] } do
|
||||
local d = Fli.demux(reader(c.data))
|
||||
local packets = {}
|
||||
for k = 1, math.min(4, d:count(1)) do
|
||||
table.insert(packets, d:get(1, k).data)
|
||||
end
|
||||
t:fuzz(c.name .. " frames", function(b: buffer)
|
||||
local st = Flic.state(c.width, c.height, c.depth)
|
||||
if st then
|
||||
Flic.decode(st, b)
|
||||
return Flic.image(st)
|
||||
end
|
||||
return nil
|
||||
end, packets, { mutations = 20, garbage = 30 })
|
||||
end
|
||||
t:done()
|
||||
@@ -0,0 +1,80 @@
|
||||
-- MPEG-2 video in the containers (tools/fixtures/mpeg2.py, "mpeg2_containers"): the same elementary stream copied by
|
||||
-- ffmpeg into MPEG-TS (stream type 0x02, and the PMT patched to 0x80, private / DigiCipher II video), MPEG-PS (VOB),
|
||||
-- Matroska (V_MPEG2) and AVI: each opens through F.video.open with codec "mpeg2", its frames equal ffmpeg's
|
||||
-- `-idct int` decode (luma CRC per frame, display order), seeking lands on a frame; the raw .m2v opens too.
|
||||
local T = require("@tests/T")
|
||||
local F = require("@formats")
|
||||
local Base64 = require("@formats/core/Base64")
|
||||
local Crc32 = require("@formats/core/Crc32")
|
||||
local Fx = require("@tests/gen/mpeg2_containers")
|
||||
local Prog = require("@tests/gen/mpeg2_prog")
|
||||
|
||||
local t = T.suite("video/mpeg2")
|
||||
|
||||
local function lumaCrc(img: any): number
|
||||
return Crc32.of(img.pixels, 0, img.width * img.height)
|
||||
end
|
||||
|
||||
local CASES = {
|
||||
{ "ts", "mpegts" },
|
||||
{ "ts80", "mpegts" },
|
||||
{ "mpg", "mpegps" },
|
||||
{ "mkv", "mkv" },
|
||||
{ "avi", "avi" },
|
||||
}
|
||||
for _, c in CASES do
|
||||
local key, format = c[1], c[2]
|
||||
local data = Base64.decode(Fx[key]) :: buffer
|
||||
local v, err = F.video.open(data)
|
||||
t:check(v ~= nil, key .. ": opens: " .. tostring(err))
|
||||
if v then
|
||||
t:eq(v.format, format, key .. ": format")
|
||||
t:eq(v.codec, "mpeg2", key .. ": codec")
|
||||
t:check(v.width == Prog.width and v.height == Prog.height, key .. ": size")
|
||||
local frames, exact = 0, 0
|
||||
while true do
|
||||
local f, ferr = v:nextFrame()
|
||||
t:check(ferr == nil, key .. ": frame error " .. tostring(ferr))
|
||||
if not f then
|
||||
break
|
||||
end
|
||||
frames += 1
|
||||
local ref = Fx.crcs[frames]
|
||||
if ref and lumaCrc(f.image) == ref[1] then
|
||||
exact += 1
|
||||
end
|
||||
end
|
||||
t:eq(frames, #Fx.crcs, key .. ": frame count")
|
||||
-- the frames ffmpeg's encoder left with an uncoded macroblock are concealed differently (tests/codecs/mpeg2)
|
||||
t:check(exact >= frames - 2, string.format("%s: %d of %d frames equal ffmpeg's", key, exact, frames))
|
||||
v:seek(0.4)
|
||||
local f = v:nextFrame()
|
||||
t:check(f ~= nil and math.abs(f.pts - 0.4) < 0.05, key .. ": seek")
|
||||
t:note(" %-5s %-7s %d frames, %d bit-exact", key, format, frames, exact)
|
||||
end
|
||||
end
|
||||
|
||||
-- the elementary stream itself
|
||||
do
|
||||
local v, err = F.video.open(Base64.decode(Prog.file) :: buffer)
|
||||
t:check(v ~= nil and v.codec == "mpeg2", "m2v: opens as mpeg2: " .. tostring(err))
|
||||
if v then
|
||||
local n = 0
|
||||
while v:nextFrame() do
|
||||
n += 1
|
||||
end
|
||||
t:eq(n, Prog.frames, "m2v: frame count")
|
||||
end
|
||||
end
|
||||
|
||||
t:fuzz("open", function(b: buffer)
|
||||
local v = F.video.open(b)
|
||||
if v then
|
||||
for _ = 1, 3 do
|
||||
v:nextFrame()
|
||||
end
|
||||
end
|
||||
return v
|
||||
end, { Base64.decode(Fx.ts) :: buffer, Base64.decode(Fx.mpg) :: buffer }, { mutations = 30, garbage = 5 })
|
||||
|
||||
t:done()
|
||||
@@ -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()
|
||||
@@ -0,0 +1,180 @@
|
||||
-- video/smk tests: Smacker files encoded by the fixture script (SMK2 and SMK4, every block type and run, the trees'
|
||||
-- escape cache, palette updates, a ring frame, ms and 10 us frame times; packed 16-bit stereo / 8-bit mono / 16-bit
|
||||
-- mono / 8-bit stereo audio and raw s16le / u8 tracks): every video frame and every audio track equal ffmpeg's
|
||||
-- decode (CRC-32 of rgba / f32le), audio seeking, errors, and the fuzz pass on the container, the video codec and the
|
||||
-- audio codec. Fixtures: tools/fixtures/smk.py.
|
||||
local T = require("@tests/T")
|
||||
local F = require("@formats")
|
||||
local Smk = require("@formats/video/smk")
|
||||
local SmkAudio = require("@formats/video/smk/Audio")
|
||||
local Legacy = require("@formats/codecs/video/Legacy")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Crc32 = require("@formats/core/Crc32")
|
||||
local Err = require("@formats/core/Err")
|
||||
local Fx = require("@tests/gen/smk_cases")
|
||||
|
||||
local t = T.suite("smk")
|
||||
t:eq(Smk.name, "smk", "name")
|
||||
t:eq(Smk.category, "video", "category")
|
||||
|
||||
local function reader(s: string): Bytes.Reader
|
||||
return Bytes.reader(buffer.fromstring(s))
|
||||
end
|
||||
|
||||
-- every frame of an audio Decoder -> f32 buffer, frames
|
||||
local function drain(dec: any): (buffer, number)
|
||||
local chunks, total = {}, 0
|
||||
while true do
|
||||
local b, n = dec:read(1000)
|
||||
if n == 0 then
|
||||
break
|
||||
end
|
||||
table.insert(chunks, { b, n })
|
||||
total += n
|
||||
end
|
||||
local out = buffer.create(total * dec.channels * 4)
|
||||
local at = 0
|
||||
for _, c in chunks do
|
||||
buffer.copy(out, at, c[1], 0, c[2] * dec.channels * 4)
|
||||
at += c[2] * dec.channels * 4
|
||||
end
|
||||
return out, total
|
||||
end
|
||||
|
||||
for _, c in Fx do
|
||||
local label = c.name
|
||||
t:check(Smk.sniff(reader(c.data)) == 1, label .. ": sniff")
|
||||
t:eq(F.detect(buffer.fromstring(c.data)), "smk", label .. ": detect")
|
||||
local v, err = F.video.open(buffer.fromstring(c.data))
|
||||
t:check(v ~= nil, label .. ": open " .. tostring(err))
|
||||
if v then
|
||||
t:eq(v.codec, "smackvid", label .. ": codec")
|
||||
t:check(v.width == c.width and v.height == c.height, label .. ": size")
|
||||
t:check(math.abs(v.frameRate - c.rate) < 1e-6, label .. ": frame rate " .. v.frameRate)
|
||||
local want = if c.ring then #c.frames - 1 else #c.frames
|
||||
local n, bad = 0, 0
|
||||
while true do
|
||||
local f, ferr = v:nextFrame()
|
||||
if not f then
|
||||
t:check(ferr == nil, label .. ": " .. tostring(ferr))
|
||||
break
|
||||
end
|
||||
n += 1
|
||||
if f.image.format ~= "rgba8" or Crc32.of(f.image.pixels) ~= c.frames[n] then
|
||||
bad += 1
|
||||
end
|
||||
end
|
||||
t:check(n == want and bad == 0, string.format("%s: %d of %d frames equal ffmpeg's (%d differ)", label, n - bad, want, bad))
|
||||
v:seek(0.2)
|
||||
local f = v:nextFrame()
|
||||
local k = math.floor(0.2 * c.rate - 1e-9) + 2
|
||||
t:check(f ~= nil and Crc32.of(f.image.pixels) == c.frames[k], label .. ": seek to frame " .. k)
|
||||
-- audio: the first track through the VideoDecoder, every track through Smk.openAudio
|
||||
if #c.audio > 0 then
|
||||
t:check(v.audio ~= nil, label .. ": audio " .. tostring(v.audioError))
|
||||
end
|
||||
local tracks = {}
|
||||
for _, tr in v.demuxer.tracks do
|
||||
if tr.kind == "audio" then
|
||||
table.insert(tracks, tr)
|
||||
end
|
||||
end
|
||||
t:eq(#tracks, #c.audio, label .. ": audio tracks")
|
||||
for i, a in c.audio do
|
||||
local tr = tracks[i]
|
||||
t:eq(tr.codec, a.codec, label .. ": audio " .. i .. " codec")
|
||||
local dec, aerr = Smk.openAudio(v.demuxer, tr)
|
||||
t:check(dec ~= nil, label .. ": audio " .. i .. ": " .. tostring(aerr))
|
||||
if dec then
|
||||
t:check(dec.sampleRate == a.rate and dec.channels == a.channels, label .. ": audio " .. i .. " format")
|
||||
local pcm, frames = drain(dec)
|
||||
t:eq(frames, a.frames, label .. ": audio " .. i .. " frames")
|
||||
t:eq(Crc32.of(pcm), a.crc, label .. ": audio " .. i .. " equals ffmpeg's")
|
||||
dec:seek(0)
|
||||
local again = drain(dec)
|
||||
t:eq(Crc32.of(again), a.crc, label .. ": audio " .. i .. " after seek(0)")
|
||||
dec:seek(0.3)
|
||||
local tail, rest = drain(dec)
|
||||
local skip = a.frames - rest
|
||||
t:check(math.abs(skip - 0.3 * a.rate) <= 1, label .. ": audio " .. i .. " seek(0.3) at frame " .. skip)
|
||||
local same = buffer.len(tail) > 0
|
||||
for q = 0, math.min(buffer.len(tail), 4000) - 4, 4 do
|
||||
if buffer.readu32(tail, q) ~= buffer.readu32(pcm, skip * a.channels * 4 + q) then
|
||||
same = false
|
||||
break
|
||||
end
|
||||
end
|
||||
t:check(same, label .. ": audio " .. i .. " seek(0.3) samples")
|
||||
end
|
||||
end
|
||||
if c.ring then
|
||||
local v2 = Smk.open(reader(c.data), { ringFrame = true })
|
||||
local m = 0
|
||||
while v2 and v2:nextFrame() do
|
||||
m += 1
|
||||
end
|
||||
t:eq(m, #c.frames, label .. ": with the ring frame")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- errors
|
||||
do
|
||||
local _, e = Smk.open(reader(string.sub(Fx[1].data, 1, 60)))
|
||||
t:check(e ~= nil and e.code == Err.TRUNCATED, "short header: TRUNCATED")
|
||||
local _, e2 = Smk.open(reader("SMK3" .. string.sub(Fx[1].data, 5)))
|
||||
t:check(e2 ~= nil and e2.code == Err.CORRUPT, "bad magic: CORRUPT")
|
||||
t:eq(Smk.sniff(reader("SMK3" .. string.sub(Fx[1].data, 5))), 0, "bad magic: no sniff")
|
||||
local _, e3 = Smk.open(reader(Fx[1].data), { maxPixels = 100 })
|
||||
t:check(e3 ~= nil and e3.code == Err.LIMIT, "maxPixels: LIMIT")
|
||||
local _, e4 = Smk.open(reader(string.sub(Fx[1].data, 1, 200)))
|
||||
t:check(e4 ~= nil and e4.code == Err.TRUNCATED, "trees past the end: TRUNCATED")
|
||||
end
|
||||
|
||||
-- fuzz: the container, the video codec (native, no pcall in between) and the audio codec
|
||||
local samples = {}
|
||||
for _, c in Fx do
|
||||
table.insert(samples, c.data)
|
||||
end
|
||||
t:fuzz("open", function(b: buffer)
|
||||
local v = Smk.open(Bytes.reader(b))
|
||||
if v then
|
||||
for _ = 1, 4 do
|
||||
if not v:nextFrame() then
|
||||
break
|
||||
end
|
||||
end
|
||||
if v.audio then
|
||||
v.audio:read(500)
|
||||
end
|
||||
end
|
||||
return v
|
||||
end, samples, { mutations = 16, garbage = 40, keepHeader = 4 })
|
||||
local S, P = Legacy.Smacker, Legacy.Picture
|
||||
for _, c in { Fx[1], Fx[2] } do
|
||||
local d = Smk.demux(reader(c.data))
|
||||
local tr = d.tracks[1]
|
||||
local packets = {}
|
||||
for k = 1, math.min(3, d:count(1)) do
|
||||
table.insert(packets, d:get(1, k).data)
|
||||
end
|
||||
t:fuzz(c.name .. " trees", function(b: buffer)
|
||||
return S.trees(b)
|
||||
end, { tr.config }, { mutations = 30, garbage = 30, keepHeader = 16 })
|
||||
local trees = assert(S.trees(tr.config))
|
||||
t:fuzz(c.name .. " frames", function(b: buffer)
|
||||
return S.decode(P.new(c.width, c.height, true, 4, 4), b, trees, tr.codecTag == "SMK4")
|
||||
end, packets, { mutations = 30, garbage = 30 })
|
||||
for _, at in d.tracks do
|
||||
if at.kind == "audio" then
|
||||
local ap = {}
|
||||
for k = 1, math.min(2, d:count(at.id)) do
|
||||
table.insert(ap, d:get(at.id, k).data)
|
||||
end
|
||||
t:fuzz(c.name .. " audio " .. at.codec, function(b: buffer)
|
||||
return SmkAudio.decode(b, at.channels, at.bitsPerSample, at.codec == "smackaudio")
|
||||
end, ap, { mutations = 20, garbage = 20 })
|
||||
end
|
||||
end
|
||||
end
|
||||
t:done()
|
||||
Reference in new issue
Block a user