ws-webp: codec tests (vp8, vp8l), bench lines, type fixes
This commit is contained in:
1 parent
ef8262d767
commit
ced18478df
7 files changed
+226
-5
No files matched your search
@@ -96,7 +96,7 @@ function Vp8L.encode(rgba: buffer, width: number, height: number, opts: EncodeOp
|
||||
if buffer.len(rgba) < width * height * 4 then
|
||||
return nil, Err.new(Err.ARG, "encode: pixel buffer too small")
|
||||
end
|
||||
local o = opts or {}
|
||||
local o: EncodeOptions = opts or {} :: EncodeOptions
|
||||
local effort = o.effort or 5
|
||||
if type(effort) ~= "number" or effort < 0 or effort > 9 then
|
||||
return nil, Err.new(Err.ARG, "encode: effort must be 0..9")
|
||||
|
||||
@@ -64,7 +64,8 @@ function Alpha.decode(buf: buffer, off: number, len: number, w: number, h: numbe
|
||||
else
|
||||
local d, err = Vp8L.decodeAlpha(buf, off + 1, len - 1, w, h)
|
||||
if not d then
|
||||
return nil, if err.code == "TRUNCATED" then "truncated: alpha " .. err.message else "alpha: " .. err.message
|
||||
local e = err :: any
|
||||
return nil, if e.code == "TRUNCATED" then "truncated: alpha " .. e.message else "alpha: " .. e.message
|
||||
end
|
||||
a = d
|
||||
end
|
||||
|
||||
@@ -420,7 +420,7 @@ function Webp.encode(img: Types.Image, opts: EncodeOptions?): (buffer?, Err.Err?
|
||||
if type(effort) ~= "number" or effort < 0 or effort > 9 then
|
||||
return nil, Err.new(Err.ARG, "effort must be 0..9")
|
||||
end
|
||||
local vopts = { effort = effort, exact = o.exact }
|
||||
local vopts: Vp8L.EncodeOptions = { effort = effort, exact = o.exact }
|
||||
local w, h = img.width, img.height
|
||||
local icc, exif, xmp = metaChunk(img.meta, "icc"), metaChunk(img.meta, "exif"), metaChunk(img.meta, "xmp")
|
||||
local frames = img.frames
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
-- codecs/Vp8 tests: the VP8 key frames of the WebP fixtures (cwebp at many settings and libvpx through ffmpeg, with
|
||||
-- 1..8 token partitions) decoded through the codec API: the yuv420p planes against dwebp -yuv, toRgba against
|
||||
-- dwebp -pam (fancy upsampling) and -nofancy, the decoder object over successive frames, headers, error codes and the
|
||||
-- fuzz no-throw pass on raw frames. Fixtures: tools/fixtures/webp.py.
|
||||
local T = require("@tests/T")
|
||||
local Vp8 = require("@formats/codecs/Vp8")
|
||||
local Crc32 = require("@formats/core/Crc32")
|
||||
local Err = require("@formats/core/Err")
|
||||
local Cases = require("@tests/gen/webp_cases")
|
||||
|
||||
local t = T.suite("vp8")
|
||||
|
||||
local function chunk(data: string): (buffer?, number, number)
|
||||
if string.sub(data, 13, 16) ~= "VP8 " then
|
||||
return nil, 0, 0
|
||||
end
|
||||
local b = buffer.fromstring(data)
|
||||
return b, 20, buffer.readu32(b, 16)
|
||||
end
|
||||
|
||||
local n, vpx = 0, 0
|
||||
local frames = {}
|
||||
local dec = Vp8.newDecoder()
|
||||
for _, c in Cases do
|
||||
local b, off, len = chunk(c.data)
|
||||
if b then
|
||||
local img, err = Vp8.decodeFrame(b, off, len)
|
||||
t:check(img, c.name .. ": " .. tostring(err))
|
||||
t:check(img.format == "yuv420p" and img.width == c.width and img.height == c.height, c.name .. ": size")
|
||||
t:eq(Crc32.of(img.pixels), c.yuv, c.name .. ": yuv420p (dwebp -yuv)")
|
||||
local rgba = Vp8.toRgba(img) :: any
|
||||
t:eq(Crc32.of(rgba.pixels), c.crc, c.name .. ": rgba (dwebp -pam)")
|
||||
local nf = Vp8.toRgba(img, false) :: any
|
||||
t:eq(Crc32.of(nf.pixels), c.nofancy, c.name .. ": rgba (dwebp -nofancy -pam)")
|
||||
-- the same frame through one long-lived decoder
|
||||
local again = dec:decodeFrame(b, off, len)
|
||||
t:check(again and Crc32.of(again.pixels) == c.yuv, c.name .. ": decoder object")
|
||||
local y, u, v, w, h = Vp8.decodePlanes(b, off, len)
|
||||
t:check(y and w == c.width and h == c.height and buffer.len(u) == buffer.len(v :: buffer), c.name .. ": decodePlanes")
|
||||
local info = Vp8.info(b, off, len)
|
||||
t:check(info and info.keyFrame and info.show and info.width == c.width, c.name .. ": info")
|
||||
n += 1
|
||||
if string.find(c.name, "vpx", 1, true) then
|
||||
vpx += 1
|
||||
end
|
||||
if #frames < 10 and len < 6000 then
|
||||
table.insert(frames, buffer.readstring(b, off, len))
|
||||
end
|
||||
end
|
||||
end
|
||||
t:note("%d VP8 key frames (%d from libvpx) match dwebp: planes, fancy and point upsampling", n, vpx)
|
||||
|
||||
-- errors
|
||||
do
|
||||
local f = buffer.fromstring(frames[1])
|
||||
local inter = buffer.fromstring(frames[1])
|
||||
buffer.writeu8(inter, 0, bit32.bor(buffer.readu8(inter, 0), 1))
|
||||
local _, e1 = Vp8.decodeFrame(inter)
|
||||
t:check(e1 and e1.code == Err.UNSUPPORTED, "inter frame: " .. tostring(e1))
|
||||
local bad = buffer.fromstring(frames[1])
|
||||
buffer.writeu8(bad, 3, 0)
|
||||
local _, e2 = Vp8.decodeFrame(bad)
|
||||
t:check(e2 and e2.code == Err.CORRUPT, "bad start code: " .. tostring(e2))
|
||||
local _, e3 = Vp8.decodeFrame(f, 0, 12)
|
||||
t:check(e3 and e3.code == Err.TRUNCATED, "truncated: " .. tostring(e3))
|
||||
local _, e4 = Vp8.decodeFrame(f, 0, buffer.len(f), { maxPixels = 4 })
|
||||
t:check(e4 and e4.code == Err.LIMIT, "maxPixels: " .. tostring(e4))
|
||||
local _, e5 = Vp8.toRgba({} :: any)
|
||||
t:check(e5 and e5.code == Err.ARG, "toRgba of a non-yuv image")
|
||||
end
|
||||
|
||||
-- fuzz on raw frames
|
||||
t:fuzz("decodeFrame", function(b: buffer)
|
||||
return Vp8.decodeFrame(b, 0, buffer.len(b), { maxPixels = 2 ^ 20 })
|
||||
end, frames, { mutations = 12, keepHeader = 10 })
|
||||
t:fuzz("decodeFrame (headers too)", function(b: buffer)
|
||||
return Vp8.decodeFrame(b, 0, buffer.len(b), { maxPixels = 2 ^ 20 })
|
||||
end, frames, { mutations = 4, garbage = 100 })
|
||||
|
||||
t:done()
|
||||
@@ -0,0 +1,120 @@
|
||||
-- codecs/Vp8L tests: the VP8L bitstreams of the WebP fixtures (cwebp -z 0..9, -exact, near-lossless) decoded through
|
||||
-- the codec API against dwebp, the encoder at every effort on generated images (gradients, noise, palettes of 1..300
|
||||
-- colours, odd sizes, alpha), headerless alpha streams, error codes, and the fuzz no-throw pass on raw bitstreams.
|
||||
-- Fixtures: tools/fixtures/webp.py.
|
||||
local T = require("@tests/T")
|
||||
local Vp8L = require("@formats/codecs/Vp8L")
|
||||
local Crc32 = require("@formats/core/Crc32")
|
||||
local Err = require("@formats/core/Err")
|
||||
local Cases = require("@tests/gen/webp_cases")
|
||||
|
||||
local t = T.suite("vp8l")
|
||||
|
||||
-- the VP8L chunk of a simple-format file
|
||||
local function chunk(data: string): (buffer?, number, number)
|
||||
if string.sub(data, 13, 16) ~= "VP8L" then
|
||||
return nil, 0, 0
|
||||
end
|
||||
local b = buffer.fromstring(data)
|
||||
return b, 20, buffer.readu32(b, 16)
|
||||
end
|
||||
|
||||
local n = 0
|
||||
local streams = {}
|
||||
for _, c in Cases do
|
||||
local b, off, len = chunk(c.data)
|
||||
if b then
|
||||
local px, w, h = Vp8L.decode(b, off, len)
|
||||
t:check(px, c.name .. ": " .. tostring(w))
|
||||
t:check(w == c.width and h == c.height, c.name .. ": size")
|
||||
t:eq(Crc32.of(px :: buffer), c.crc, c.name .. ": pixels (dwebp)")
|
||||
local info = Vp8L.info(b, off, len)
|
||||
t:check(info and info.width == c.width and info.height == c.height and info.alpha == c.alpha, c.name .. ": info")
|
||||
local argb = Vp8L.decodeArgb(b, off, len)
|
||||
t:check(argb and Crc32.of(Vp8L.toRgba(argb, c.width * c.height)) == c.crc, c.name .. ": decodeArgb + toRgba")
|
||||
n += 1
|
||||
if #streams < 12 and len < 8000 then
|
||||
table.insert(streams, buffer.readstring(b, off, len))
|
||||
end
|
||||
end
|
||||
end
|
||||
t:note("%d VP8L bitstreams match dwebp", n)
|
||||
|
||||
-- the encoder: round trips at every effort
|
||||
local rand = T.rng(5)
|
||||
local function gen(w: number, h: number, kind: string): buffer
|
||||
local px = buffer.create(w * h * 4)
|
||||
local pal = {}
|
||||
local np = if kind == "pal1" then 1 elseif kind == "pal4" then 4 elseif kind == "pal40" then 40 elseif kind == "pal300" then 300 else 0
|
||||
for i = 1, np do
|
||||
pal[i] = rand(2 ^ 24) * 256 + (if i % 3 == 0 then rand(256) else 255)
|
||||
end
|
||||
for y = 0, h - 1 do
|
||||
for x = 0, w - 1 do
|
||||
local o = (y * w + x) * 4
|
||||
if np > 0 then
|
||||
local v = pal[(x * 7 + y * 3 + (x * y) % 5) % np + 1]
|
||||
buffer.writeu32(px, o, v)
|
||||
elseif kind == "noise" then
|
||||
buffer.writeu32(px, o, rand(2 ^ 16) * 65536 + rand(2 ^ 16))
|
||||
else
|
||||
buffer.writeu8(px, o, (x * 4 + y) % 256)
|
||||
buffer.writeu8(px, o + 1, (y * 3) % 256)
|
||||
buffer.writeu8(px, o + 2, (x * y) % 256)
|
||||
buffer.writeu8(px, o + 3, if kind == "alpha" then (x * 9) % 256 else 255)
|
||||
end
|
||||
end
|
||||
end
|
||||
return px
|
||||
end
|
||||
local count = 0
|
||||
for _, kind in { "grad", "alpha", "noise", "pal1", "pal4", "pal40", "pal300" } do
|
||||
for _, size in { { 1, 1 }, { 2, 3 }, { 17, 5 }, { 64, 33 }, { 130, 2 } } do
|
||||
local w, h = size[1], size[2]
|
||||
local px = gen(w, h, kind)
|
||||
for effort = 0, 9 do
|
||||
local bits, err = Vp8L.encode(px, w, h, { effort = effort, exact = true })
|
||||
t:check(bits, kind .. ": encode: " .. tostring(err))
|
||||
local back, bw, bh = Vp8L.decode(bits :: buffer)
|
||||
t:check(back and bw == w and bh == h, string.format("%s %dx%d effort %d: decode: %s", kind, w, h, effort, tostring(bw)))
|
||||
t:check(buffer.tostring(back :: buffer) == buffer.tostring(px), string.format("%s %dx%d effort %d: round trip", kind, w, h, effort))
|
||||
count += 1
|
||||
end
|
||||
-- a headerless alpha plane
|
||||
local a = buffer.create(w * h)
|
||||
for i = 0, w * h - 1 do
|
||||
buffer.writeu8(a, i, buffer.readu8(px, i * 4 + 1))
|
||||
end
|
||||
local s = Vp8L.encodeAlpha(a, w, h) :: buffer
|
||||
local ab = Vp8L.decodeAlpha(s, 0, buffer.len(s), w, h)
|
||||
t:check(ab and buffer.tostring(ab) == buffer.tostring(a), kind .. ": alpha stream round trip")
|
||||
end
|
||||
end
|
||||
t:note("%d encoder round trips", count)
|
||||
|
||||
-- errors
|
||||
do
|
||||
local _, e1 = Vp8L.decode(buffer.fromstring("\x2e\0\0\0\0"))
|
||||
t:check(e1 and e1.code == Err.CORRUPT, "bad signature: " .. tostring(e1))
|
||||
local _, e2 = Vp8L.decode(buffer.fromstring("\x2f\0\0\0\x20"))
|
||||
t:check(e2 and e2.code == Err.CORRUPT, "bad version: " .. tostring(e2))
|
||||
local _, e3 = Vp8L.decode(buffer.fromstring("\x2f\0"))
|
||||
t:check(e3 and e3.code == Err.TRUNCATED, "short header: " .. tostring(e3))
|
||||
local big = Vp8L.encode(gen(64, 33, "grad"), 64, 33) :: buffer
|
||||
local _, e4 = Vp8L.decode(big, 0, buffer.len(big), 100)
|
||||
t:check(e4 and e4.code == Err.LIMIT, "maxPixels: " .. tostring(e4))
|
||||
local _, e5 = Vp8L.decode(big, 0, buffer.len(big) // 2)
|
||||
t:check(e5 and e5.code == Err.TRUNCATED, "truncated stream: " .. tostring(e5))
|
||||
local _, e6 = Vp8L.encode(buffer.create(4), 2, 2)
|
||||
t:check(e6 and e6.code == Err.ARG, "encode: short pixel buffer")
|
||||
end
|
||||
|
||||
-- fuzz on raw bitstreams
|
||||
t:fuzz("decode", function(b: buffer)
|
||||
return Vp8L.decode(b, 0, buffer.len(b), 2 ^ 20)
|
||||
end, streams, { mutations = 10, keepHeader = 5 })
|
||||
t:fuzz("decodeAlpha", function(b: buffer)
|
||||
return Vp8L.decodeAlpha(b, 0, buffer.len(b), 37, 11)
|
||||
end, streams, { mutations = 4, garbage = 100 })
|
||||
|
||||
t:done()
|
||||
+21
-1
@@ -1,5 +1,6 @@
|
||||
-- Throughput (bash tools/bench.sh): codec MB/s under both runtimes. MB/s are of decompressed output, best of n runs.
|
||||
-- Fixtures: tools/fixtures/deflate.py (a 6 MB corpus) and tools/fixtures/png.py (1024 x 768 images).
|
||||
-- Fixtures: tools/fixtures/deflate.py (a 6 MB corpus), tools/fixtures/png.py (1024 x 768 images) and
|
||||
-- tools/fixtures/webp.py (512 x 512 lossy and lossless files).
|
||||
local Deflate = require("@formats/codecs/Deflate")
|
||||
local Crc32 = require("@formats/core/Crc32")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
@@ -56,3 +57,22 @@ do
|
||||
Png.encode(img)
|
||||
end)
|
||||
end
|
||||
do
|
||||
local Webp = require("@formats/image/webp")
|
||||
local WB = require("@tests/gen/webp_bench")
|
||||
local n = WB.width * WB.height * 4
|
||||
local lossy, lossless = buffer.fromstring(WB.lossy), buffer.fromstring(WB.lossless)
|
||||
bench("webp lossy decode 512x512", 3, n, function()
|
||||
Webp.decode(Bytes.reader(lossy))
|
||||
end)
|
||||
bench("webp lossless decode 512x512", 3, n, function()
|
||||
Webp.decode(Bytes.reader(lossless))
|
||||
end)
|
||||
local img = assert(Webp.decode(Bytes.reader(lossless)))
|
||||
bench("webp lossless encode (effort 5)", 1, n, function()
|
||||
Webp.encode(img)
|
||||
end)
|
||||
bench("webp lossless encode (effort 1)", 1, n, function()
|
||||
Webp.encode(img, { effort = 1 })
|
||||
end)
|
||||
end
|
||||
+1
-1
@@ -3,7 +3,7 @@
|
||||
cd "$(dirname "$0")/.." || exit 1
|
||||
LUAU=${LUAU:-$(command -v luau || nix shell nixpkgs#luau -c sh -c 'command -v luau')}
|
||||
LUAJIT=${LUAJIT:-$(command -v luajit || nix shell nixpkgs#luajit -c sh -c 'command -v luajit')}
|
||||
python3 -I tools/fixtures.py deflate png > /dev/null || exit 1
|
||||
python3 -I tools/fixtures.py deflate png webp > /dev/null || exit 1
|
||||
python3 -I tools/build.py luajit > /dev/null || exit 1
|
||||
"$LUAU" --codegen -O2 tools/bench.luau
|
||||
"$LUAJIT" -e 'package.path = "build/luajit/?.lua;build/luajit/?/init.lua;" .. package.path' build/luajit/tools/bench.lua
|
||||
Reference in new issue
Block a user