--!strict -- codecs/Vp8: the VP8 video codec (RFC 6386), decode only. A codec (layer 1): image/webp uses it for lossy frames, the -- WebM / Matroska and IVF video tracks can use it too. A port of libwebp 1.6 (BSD-3-Clause, -- licenses/libwebp-LICENSE.txt), bit-exact with libwebp: the yuv420p planes equal `dwebp -yuv`, and toRgba equals -- `dwebp -pam` (fancy upsampling) or `dwebp -nofancy -pam`. -- Supports: key frames (intra 16x16 / 4x4 / chroma prediction, segments, both loop filters, token partitions, -- probability updates, skip). Not supported yet: inter frames (decodeFrame returns UNSUPPORTED; the decoder object -- keeps the cross-frame state so they can be added), the horizontal / vertical scale bits (ignored, as libwebp does). -- -- local Vp8 = require("@formats/codecs/Vp8") -- local frame, err = Vp8.decodeFrame(buf, off, len) -- a yuv420p Types.Image (+ frame.meta.source.vp8 header) -- local rgba = Vp8.toRgba(frame) -- rgba8 Image (libwebp's fancy upsampling) -- local dec = Vp8.newDecoder(); dec:decodeFrame(...) -- a stream of frames (WebM) -- -- Never throws: bad data returns nil, Err (core/Err codes TRUNCATED / CORRUPT / UNSUPPORTED / LIMIT / ARG). local Err = require("@formats/core/Err") local Types = require("@formats/Types") local Decode = require("@formats/codecs/Vp8/Decode") local Yuv = require("@formats/codecs/Vp8/Yuv") local Vp8 = {} local DEFAULT_MAX_PIXELS = 2 ^ 28 export type Options = { maxPixels: number?, -- default 2^28 } local function fail(msg: string?, offset: number?): Err.Err local m = msg or "bad data" if string.sub(m, 1, 11) == "truncated: " then return Err.new(Err.TRUNCATED, string.sub(m, 12), offset) elseif string.sub(m, 1, 7) == "limit: " then return Err.new(Err.LIMIT, string.sub(m, 8), offset) elseif string.sub(m, 1, 13) == "unsupported: " then return Err.new(Err.UNSUPPORTED, string.sub(m, 14), offset) end return Err.new(Err.CORRUPT, m, offset) end local function range(buf: buffer, off: number?, len: number?): (number, number) local n = buffer.len(buf) local o = math.clamp(off or 0, 0, n) return o, math.clamp(len or (n - o), 0, n - o) end -- info(buf, off?, len?) -> { keyFrame, profile, show, partitionLength, width, height, xscale, yscale } | nil, Err function Vp8.info(buf: buffer, off: number?, len: number?): (any, Err.Err?) if type(buf) ~= "buffer" then return nil, Err.new(Err.ARG, "info: expected a buffer") end local o, l = range(buf, off, len) local h, err = Decode.header(buf, o, l) if not h then return nil, fail(err, o) end return h, nil end local DecoderMeta = {} DecoderMeta.__index = DecoderMeta export type Decoder = typeof(setmetatable({} :: { state: Decode.State }, DecoderMeta)) -- newDecoder() -> Decoder: decodes successive frames of one stream function Vp8.newDecoder(): Decoder return setmetatable({ state = Decode.new() }, DecoderMeta) end -- decoder:decodeFrame(buf, off?, len?, opts?) -> a yuv420p Image | nil, Err function DecoderMeta.decodeFrame(self: Decoder, buf: buffer, off: number?, len: number?, opts: Options?): (Types.Image?, Err.Err?) if type(buf) ~= "buffer" then return nil, Err.new(Err.ARG, "decodeFrame: expected a buffer") end local o, l = range(buf, off, len) local f, err = Decode.decodeFrame(self.state, buf, o, l, opts and opts.maxPixels or DEFAULT_MAX_PIXELS) if not f then return nil, fail(err, o) end local img = Types.Image.new(f.width, f.height, "yuv420p") local ny = f.width * f.height local nc = buffer.len(f.u) buffer.copy(img.pixels, 0, f.y, 0, ny) buffer.copy(img.pixels, ny, f.u, 0, nc) buffer.copy(img.pixels, ny + nc, f.v, 0, nc) img.meta.source = { format = "vp8", keyFrame = f.header.keyFrame, profile = f.header.profile } return img, nil end -- decodeFrame(buf, off?, len?, opts?) -> a yuv420p Image | nil, Err (one key frame) function Vp8.decodeFrame(buf: buffer, off: number?, len: number?, opts: Options?): (Types.Image?, Err.Err?) return Vp8.newDecoder():decodeFrame(buf, off, len, opts) end -- decodePlanes(buf, off?, len?, opts?) -> y, u, v, width, height | nil, Err: one key frame as separate planes function Vp8.decodePlanes(buf: buffer, off: number?, len: number?, opts: Options?): (buffer?, any, buffer?, number?, number?) if type(buf) ~= "buffer" then return nil, Err.new(Err.ARG, "decodePlanes: expected a buffer") end local o, l = range(buf, off, len) local f, err = Decode.decodeFrame(Decode.new(), buf, o, l, opts and opts.maxPixels or DEFAULT_MAX_PIXELS) if not f then return nil, fail(err, o) end return f.y, f.u, f.v, f.width, f.height end -- planes(img) -> y, u, v offsets in img.pixels and the chroma width / height function Vp8.planes(img: Types.Image): (number, number, number, number, number) local cw, ch = (img.width + 1) // 2, (img.height + 1) // 2 local ny = img.width * img.height return 0, ny, ny + cw * ch, cw, ch end -- toRgba(yuvImage, fancy?) -> rgba8 Image. fancy (default true): libwebp's bilinear chroma upsampling; false: point -- sampling (dwebp -nofancy) function Vp8.toRgba(img: Types.Image, fancy: boolean?): (Types.Image?, Err.Err?) if type(img) ~= "table" or img.format ~= "yuv420p" then return nil, Err.new(Err.ARG, "toRgba: needs a yuv420p Image") end local w, h = img.width, img.height local _, uo, vo, cw, ch = Vp8.planes(img) local y = buffer.create(w * h) local u = buffer.create(cw * ch) local v = buffer.create(cw * ch) buffer.copy(y, 0, img.pixels, 0, w * h) buffer.copy(u, 0, img.pixels, uo, cw * ch) buffer.copy(v, 0, img.pixels, vo, cw * ch) local out = Types.Image.new(w, h, "rgba8") Yuv.toRgba(y, u, v, w, h, fancy ~= false, out.pixels) out.meta.source = img.meta.source return out, nil end -- yuvToRgba(y, u, v, w, h, fancy?) -> rgba8 pixels from separate planes (alpha 255) function Vp8.yuvToRgba(y: buffer, u: buffer, v: buffer, w: number, h: number, fancy: boolean?): buffer return Yuv.toRgba(y, u, v, w, h, fancy ~= false) end Vp8.setAlpha = Yuv.setAlpha return Vp8