ws-video-vp8: VP8 inter frames (libvpx 1.17 port): references, modes / motion vectors, split, six-tap / bilinear prediction, inter loop filter levels, hidden frames, reset; tests vs ffmpeg on 27 test vectors + 24 libvpx clips

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
4DBugandClaude Opus 5.5 committed 2026-10-31 09:46:45 -05:00
1 parent 75c677074a
commit aff15e7cc9
36 files changed
+1338 -161

No files matched your search

File diff suppressed because it is too large. Load diff
+263
View File
@@ -0,0 +1,263 @@
--!strict
--!native
--!optimize 2
-- codecs/Vp8/Predict: VP8 inter prediction (RFC 6386 section 18): the six-tap sub-pixel filters (version 0), the
-- bilinear ones (versions 1-3; version 3 also rounds chroma vectors to full pixels), chroma vectors derived from the
-- luma ones, split macroblocks, and the reference frame borders. After libvpx 1.17 vp8/common/reconinter.c and
-- filter.c (BSD-3-Clause, licenses/libvpx-LICENSE.txt). Out-of-frame motion reads the reference as if its edge pixels
-- extended forever, which is exactly what libvpx computes with its 32 pixel border plus its motion vector clamping (the
-- clamps only kick in once every tap is in the border). The planes keep a BORDER pixel border filled by extend(); a
-- block whose taps leave it is first copied with clamped coordinates. Never throws.
local Tables = require("@formats/codecs/Vp8/Tables")
local readu8, writeu8, bcopy, bcreate, bfill = buffer.readu8, buffer.writeu8, buffer.copy, buffer.create, buffer.fill
local rshift, band = bit32.rshift, bit32.band
local SIX, BIL = Tables.SIXTAP, Tables.BILINEAR
local Predict = {}
local BORDER = 32
Predict.BORDER = BORDER
-- the work buffer stride (codecs/Vp8/Decode: BPS)
local BPS = 32
local emu = bcreate(32 * 32) -- a block with its taps, edges replicated
local tmp = bcreate(32 * 32) -- the first (horizontal) pass of a two-dimensional filter
-- the six-tap filter along rows: dst[i] = clamp((sum of src[i - 2 .. i + 3] * taps + 64) >> 7)
local function sixH(src: buffer, so: number, ss: number, dst: buffer, d: number, ds: number, w: number, h: number, f: number)
local k = f * 6
local t0, t1, t2, t3, t4, t5 = SIX[k + 1], SIX[k + 2], SIX[k + 3], SIX[k + 4], SIX[k + 5], SIX[k + 6]
for _ = 1, h do
local p = so - 2
local a, b, c, e, g = readu8(src, p), readu8(src, p + 1), readu8(src, p + 2), readu8(src, p + 3), readu8(src, p + 4)
p += 5
for i = 0, w - 1 do
local q = readu8(src, p + i)
local v = a * t0 + b * t1 + c * t2 + e * t3 + g * t4 + q * t5 + 64
writeu8(dst, d + i, if v < 0 then 0 elseif v >= 32768 then 255 else rshift(v, 7))
a, b, c, e, g = b, c, e, g, q
end
so += ss
d += ds
end
end
-- the six-tap filter along columns
local function sixV(src: buffer, so: number, ss: number, dst: buffer, d: number, ds: number, w: number, h: number, f: number)
local k = f * 6
local t0, t1, t2, t3, t4, t5 = SIX[k + 1], SIX[k + 2], SIX[k + 3], SIX[k + 4], SIX[k + 5], SIX[k + 6]
for i = 0, w - 1 do
local p = so + i - 2 * ss
local a, b, c = readu8(src, p), readu8(src, p + ss), readu8(src, p + 2 * ss)
local e, g = readu8(src, p + 3 * ss), readu8(src, p + 4 * ss)
p += 5 * ss
local o = d + i
for _ = 1, h do
local q = readu8(src, p)
local v = a * t0 + b * t1 + c * t2 + e * t3 + g * t4 + q * t5 + 64
writeu8(dst, o, if v < 0 then 0 elseif v >= 32768 then 255 else rshift(v, 7))
a, b, c, e, g = b, c, e, g, q
p += ss
o += ds
end
end
end
-- the bilinear filters: dst[i] = (src[i] * t0 + src[i + 1] * t1 + 64) >> 7
local function bilH(src: buffer, so: number, ss: number, dst: buffer, d: number, ds: number, w: number, h: number, f: number)
local t0, t1 = BIL[f * 2 + 1], BIL[f * 2 + 2]
for _ = 1, h do
local a = readu8(src, so)
for i = 0, w - 1 do
local b = readu8(src, so + i + 1)
writeu8(dst, d + i, rshift(a * t0 + b * t1 + 64, 7))
a = b
end
so += ss
d += ds
end
end
local function bilV(src: buffer, so: number, ss: number, dst: buffer, d: number, ds: number, w: number, h: number, f: number)
local t0, t1 = BIL[f * 2 + 1], BIL[f * 2 + 2]
for i = 0, w - 1 do
local p = so + i
local a = readu8(src, p)
local o = d + i
for _ = 1, h do
p += ss
local b = readu8(src, p)
writeu8(dst, o, rshift(a * t0 + b * t1 + 64, 7))
a = b
o += ds
end
end
end
-- block(src, stride, base, wmax, hmax, x, y, mvx, mvy, w, h, dst, d, bil): predicts the w x h block at plane pixel
-- (x, y) moved by (mvx, mvy) eighth pixels into dst at d (stride BPS). base: the offset of plane pixel (0, 0); wmax,
-- hmax: the last pixel column / row of the (macroblock aligned) plane.
local function block(src: buffer, stride: number, base: number, wmax: number, hmax: number, x: number, y: number, mvx: number, mvy: number, w: number, h: number, dst: buffer, d: number, bil: boolean)
local fx, fy = band(mvx, 7), band(mvy, 7)
local px, py = x + (mvx - fx) // 8, y + (mvy - fy) // 8
local sb, so, ss = src, 0, stride
if px >= 2 - BORDER and py >= 2 - BORDER and px + w + 3 <= wmax + 1 + BORDER and py + h + 3 <= hmax + 1 + BORDER then
so = base + py * stride + px
else
for r = 0, h + 4 do
local sy = py - 2 + r
if sy < 0 then
sy = 0
elseif sy > hmax then
sy = hmax
end
local row = base + sy * stride
local o = r * 32
for c = 0, w + 4 do
local sx = px - 2 + c
if sx < 0 then
sx = 0
elseif sx > wmax then
sx = wmax
end
writeu8(emu, o + c, readu8(src, row + sx))
end
end
sb, so, ss = emu, 66, 32
end
if fx == 0 then
if fy == 0 then
for r = 0, h - 1 do
bcopy(dst, d + r * BPS, sb, so + r * ss, w)
end
elseif bil then
bilV(sb, so, ss, dst, d, BPS, w, h, fy)
else
sixV(sb, so, ss, dst, d, BPS, w, h, fy)
end
elseif fy == 0 then
if bil then
bilH(sb, so, ss, dst, d, BPS, w, h, fx)
else
sixH(sb, so, ss, dst, d, BPS, w, h, fx)
end
elseif bil then
bilH(sb, so, ss, tmp, 0, 32, w, h + 1, fx)
bilV(tmp, 0, 32, dst, d, BPS, w, h, fy)
else
sixH(sb, so - 2 * ss, ss, tmp, 0, 32, w, h + 5, fx)
sixV(tmp, 64, 32, dst, d, BPS, w, h, fy)
end
end
local function chroma16(v: number): number
-- the chroma vector of a whole macroblock: half the luma one, rounded away from zero
return if v < 0 then -((1 - v) // 2) else (v + 1) // 2
end
local function chroma4(s: number): number
-- the chroma vector of a 2x2 group of luma blocks: the sum of their 4 vectors / 8, rounded half away from zero
return if s < 0 then -((4 - s) // 8) else (s + 4) // 8
end
export type Planes = {
Y: buffer,
U: buffer,
V: buffer,
ys: number, -- luma stride
uvs: number, -- chroma stride
yb: number, -- offset of luma pixel (0, 0)
uvb: number,
mbw: number,
mbh: number,
}
-- mb(ref, mbx, mby, mvr, mvc, split, subR, subC, sb, dst, yo, uo, vo, bil, full): the prediction of one inter
-- macroblock into the work buffer (luma at yo, chroma at uo / vo). Not split: one vector (mvr, mvc) in eighth pixels;
-- split: the 16 block vectors subR / subC[sb + 1 .. sb + 16] (part < 3: the four 8x8 quarters share them).
function Predict.mb(ref: Planes, mbx: number, mby: number, mvr: number, mvc: number, part: number, subR: { number }, subC: { number }, sb: number, dst: buffer, yo: number, uo: number, vo: number, bil: boolean, full: boolean)
local Y, U, V, ys, uvs, yb, uvb = ref.Y, ref.U, ref.V, ref.ys, ref.uvs, ref.yb, ref.uvb
local wl, hl = ref.mbw * 16 - 1, ref.mbh * 16 - 1
local wc, hc = ref.mbw * 8 - 1, ref.mbh * 8 - 1
local x, y = mbx * 16, mby * 16
local cx, cy = mbx * 8, mby * 8
if part < 0 then
block(Y, ys, yb, wl, hl, x, y, mvc, mvr, 16, 16, dst, yo, bil)
local cr, cc = chroma16(mvr), chroma16(mvc)
if full then
cr -= band(cr, 7)
cc -= band(cc, 7)
end
block(U, uvs, uvb, wc, hc, cx, cy, cc, cr, 8, 8, dst, uo, bil)
block(V, uvs, uvb, wc, hc, cx, cy, cc, cr, 8, 8, dst, vo, bil)
return
end
if part < 3 then
for q = 0, 3 do
local bx, by = (q % 2) * 8, (q // 2) * 8
local k = sb + by + bx // 4 + 1
block(Y, ys, yb, wl, hl, x + bx, y + by, subC[k], subR[k], 8, 8, dst, yo + by * BPS + bx, bil)
end
else
for b = 0, 15, 2 do
local k = sb + b + 1
local bx, by = (b % 4) * 4, (b // 4) * 4
local r0, c0 = subR[k], subC[k]
if r0 == subR[k + 1] and c0 == subC[k + 1] then
block(Y, ys, yb, wl, hl, x + bx, y + by, c0, r0, 8, 4, dst, yo + by * BPS + bx, bil)
else
block(Y, ys, yb, wl, hl, x + bx, y + by, c0, r0, 4, 4, dst, yo + by * BPS + bx, bil)
block(Y, ys, yb, wl, hl, x + bx + 4, y + by, subC[k + 1], subR[k + 1], 4, 4, dst, yo + by * BPS + bx + 4, bil)
end
end
end
for i = 0, 1 do
local rr, rc = 0, 0
for j = 0, 1 do
local k = sb + i * 8 + j * 2 + 1
local cr = chroma4(subR[k] + subR[k + 1] + subR[k + 4] + subR[k + 5])
local cc = chroma4(subC[k] + subC[k + 1] + subC[k + 4] + subC[k + 5])
if full then
cr -= band(cr, 7)
cc -= band(cc, 7)
end
if j == 0 then
rr, rc = cr, cc
elseif cr == rr and cc == rc then
-- both 4x4 chroma blocks of the row share one vector: one 8x4 block
block(U, uvs, uvb, wc, hc, cx, cy + i * 4, cc, cr, 8, 4, dst, uo + i * 4 * BPS, bil)
block(V, uvs, uvb, wc, hc, cx, cy + i * 4, cc, cr, 8, 4, dst, vo + i * 4 * BPS, bil)
else
block(U, uvs, uvb, wc, hc, cx, cy + i * 4, rc, rr, 4, 4, dst, uo + i * 4 * BPS, bil)
block(V, uvs, uvb, wc, hc, cx, cy + i * 4, rc, rr, 4, 4, dst, vo + i * 4 * BPS, bil)
block(U, uvs, uvb, wc, hc, cx + 4, cy + i * 4, cc, cr, 4, 4, dst, uo + i * 4 * BPS + 4, bil)
block(V, uvs, uvb, wc, hc, cx + 4, cy + i * 4, cc, cr, 4, 4, dst, vo + i * 4 * BPS + 4, bil)
end
end
end
end
-- extend(P, stride, base, w, h): fills the BORDER pixels around a w x h plane with its edge pixels
local function extend(P: buffer, stride: number, base: number, w: number, h: number)
for r = 0, h - 1 do
local o = base + r * stride
bfill(P, o - BORDER, readu8(P, o), BORDER)
bfill(P, o + w, readu8(P, o + w - 1), BORDER)
end
local top = base - BORDER
local bottom = base + (h - 1) * stride - BORDER
for r = 1, BORDER do
bcopy(P, top - r * stride, P, top, stride)
bcopy(P, bottom + r * stride, P, bottom, stride)
end
end
function Predict.extend(ref: Planes)
extend(ref.Y, ref.ys, ref.yb, ref.mbw * 16, ref.mbh * 16)
extend(ref.U, ref.uvs, ref.uvb, ref.mbw * 8, ref.mbh * 8)
extend(ref.V, ref.uvs, ref.uvb, ref.mbw * 8, ref.mbh * 8)
end
return Predict
+70 -1
View File
@@ -1,6 +1,9 @@
--!strict
-- codecs/Vp8/Tables: the constant VP8 tables of RFC 6386 (sections 13.5, 13.4, 11.5, 14.1), copied from libwebp 1.6
-- src/dec/tree_dec.c and src/dec/quant_dec.c (BSD-3-Clause, licenses/libwebp-LICENSE.txt). Flat 1-based arrays.
-- src/dec/tree_dec.c and src/dec/quant_dec.c (BSD-3-Clause, licenses/libwebp-LICENSE.txt); the inter frame tables
-- (sections 16-18: mode / motion vector probabilities, mode contexts, split partitions, sub-pixel filters) from
-- libvpx 1.17 vp8/common (entropymode.c, vp8_entropymodedata.h, entropymv.c, modecont.c, decodemv.c, filter.c;
-- BSD-3-Clause, licenses/libvpx-LICENSE.txt). Flat 1-based arrays.
local Tables = {}
-- default token probabilities [type 4][band 8][ctx 3][proba 11]: index ((t * 8 + b) * 3 + c) * 11 + p + 1
@@ -177,4 +180,70 @@ Tables.CAT4 = { 176, 155, 140, 135 }
Tables.CAT5 = { 180, 157, 141, 134, 130 }
Tables.CAT6 = { 254, 254, 243, 230, 196, 177, 153, 140, 133, 130, 129 }
-- inter frames: default intra mode probabilities (16x16 / chroma, kept in the frame context) and the fixed 4x4 ones
Tables.YMODE_PROB = { 112, 86, 140, 37 }
Tables.UVMODE_PROB = { 162, 101, 204 }
Tables.BMODE_PROB = { 120, 90, 79, 133, 87, 85, 80, 111, 151 }
-- motion vector probabilities, row then column (19 each): is short, sign, short tree (7), long bits (10)
Tables.MV_DEFAULT = {
162, 128, 225, 146, 172, 147, 214, 39, 156, 128, 129, 132, 75, 145, 178, 206, 239, 254, 254,
164, 128, 204, 170, 119, 235, 140, 230, 228, 128, 130, 130, 74, 148, 180, 203, 236, 254, 254,
}
Tables.MV_UPDATE = {
237, 246, 253, 253, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 250, 250, 252, 254, 254,
231, 243, 245, 253, 254, 254, 254, 254, 254, 254, 254, 254, 254, 254, 251, 251, 254, 254, 254,
}
-- mode contexts [count 0..5][tree node 0..3]: index count * 4 + node + 1
Tables.MODE_CONTEXTS = {
7, 1, 1, 143,
14, 18, 14, 107,
135, 64, 57, 68,
60, 56, 128, 65,
159, 134, 128, 34,
234, 188, 128, 28,
}
-- sub-block motion vector reference probabilities [(above zero << 2) | (left zero << 1) | (left == above)][3]
Tables.SUB_MV_REF_PROB = {
147, 136, 18,
223, 1, 34,
106, 145, 1,
208, 1, 1,
179, 121, 1,
223, 1, 34,
179, 121, 1,
208, 1, 1,
}
-- split partitions (16x8, 8x16, 8x8, 4x4): the first block of each partition [s * 16 + j + 1], the blocks of each
-- partition in order [s * 16 + j * count + i + 1], and the number of blocks per partition
Tables.SPLIT_OFFSET = {
0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 2, 8, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
}
Tables.SPLIT_FILL = {
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
0, 1, 4, 5, 8, 9, 12, 13, 2, 3, 6, 7, 10, 11, 14, 15,
0, 1, 4, 5, 2, 3, 6, 7, 8, 9, 12, 13, 10, 11, 14, 15,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
}
Tables.SPLIT_COUNT = { 8, 8, 4, 1 }
-- the six-tap sub-pixel filters [fraction 0..7][tap 0..5] and the bilinear ones [fraction][2] (versions 1-3)
Tables.SIXTAP = {
0, 0, 128, 0, 0, 0,
0, -6, 123, 12, -1, 0,
2, -11, 108, 36, -8, 1,
0, -9, 93, 50, -6, 0,
3, -16, 77, 77, -16, 3,
0, -6, 50, 93, -9, 0,
1, -8, 36, 108, -11, 2,
0, -1, 12, 123, -6, 0,
}
Tables.BILINEAR = { 128, 0, 112, 16, 96, 32, 80, 48, 64, 64, 48, 80, 32, 96, 16, 112 }
return Tables
+28 -14
View File
@@ -1,16 +1,21 @@
--!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,
-- WebM / Matroska and IVF video tracks use its decoder object. Key frames are 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).
-- `dwebp -pam` (fancy upsampling) or `dwebp -nofancy -pam`. Inter frames follow libvpx 1.17 (BSD-3-Clause,
-- licenses/libvpx-LICENSE.txt), bit-exact with libvpx / ffmpeg on the official test vectors and libvpx streams.
-- Supports: key and inter frames of every version / profile (0: six-tap filters, normal loop filter; 1-2: bilinear;
-- 3: bilinear with full pixel chroma; the loop filter type is the header's), last / golden / altref references with
-- sign bias, hidden (altref) frames, token partitions, segments, loop filter deltas, probability context save /
-- restore. Not supported: the horizontal / vertical scale bits (ignored, as libwebp and ffmpeg do), error
-- concealment (a damaged frame returns an Err).
--
-- local Vp8 = require("@formats/codecs/Vp8")
-- local frame, err = Vp8.decodeFrame(buf, off, len) -- a yuv420p Types.Image (+ frame.meta.source.vp8 header)
-- local frame, err = Vp8.decodeFrame(buf, off, len) -- one key frame: a yuv420p Types.Image
-- local rgba = Vp8.toRgba(frame) -- rgba8 Image (libwebp's fancy upsampling)
-- local dec = Vp8.newDecoder(); dec:decodeFrame(...) -- a stream of frames (WebM)
-- local dec = Vp8.newDecoder() -- a stream (WebM, IVF): key and inter frames
-- local img, err = dec:decodeFrame(buf, off, len) -- img.meta.source.show == false: a hidden frame
-- dec:reset() -- after a seek: the next frame must be a key frame
--
-- Never throws: bad data returns nil, Err (core/Err codes TRUNCATED / CORRUPT / UNSUPPORTED / LIMIT / ARG).
local Err = require("@formats/core/Err")
@@ -67,7 +72,9 @@ function Vp8.newDecoder(): Decoder
return setmetatable({ state = Decode.new() }, DecoderMeta)
end
-- decoder:decodeFrame(buf, off?, len?, opts?) -> a yuv420p Image | nil, Err
-- decoder:decodeFrame(buf, off?, len?, opts?) -> a yuv420p Image (a fresh buffer) | nil, Err. Key and inter frames;
-- meta.source = { format = "vp8", keyFrame, show (false: a hidden frame, decoded as a reference but not displayed),
-- profile, width, height }. An inter frame before the first key frame (or after reset) returns CORRUPT.
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")
@@ -79,14 +86,18 @@ function DecoderMeta.decodeFrame(self: Decoder, buf: buffer, off: number?, len:
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 }
local nc = ((f.width + 1) // 2) * ((f.height + 1) // 2)
Decode.cropTo(f, img.pixels, 0, img.pixels, ny, img.pixels, ny + nc)
local h = f.header
img.meta.source = { format = "vp8", keyFrame = h.keyFrame, show = h.show, profile = h.profile, width = f.width, height = f.height }
return img, nil
end
-- decoder:reset(): drops the reference frames (after a seek); the next frame must be a key frame
function DecoderMeta.reset(self: Decoder)
Decode.reset(self.state)
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)
@@ -102,7 +113,10 @@ function Vp8.decodePlanes(buf: buffer, off: number?, len: number?, opts: Options
if not f then
return nil, fail(err, o)
end
return f.y, f.u, f.v, f.width, f.height
local nc = ((f.width + 1) // 2) * ((f.height + 1) // 2)
local y, u, v = buffer.create(f.width * f.height), buffer.create(nc), buffer.create(nc)
Decode.cropTo(f, y, 0, u, 0, v, 0)
return y, u, v, f.width, f.height
end
-- planes(img) -> y, u, v offsets in img.pixels and the chroma width / height