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()
|
||||
Reference in new issue
Block a user