video perf: MPEG-1/2, yuv -> rgba, H.264, VP8 faster in interpreted Luau (and native / LuaJIT), outputs unchanged

- codecs/video/Mpeg2/Decoder: coefficient loop keeps the bit position in a local and reads a 25-bit window with
  one readu32 (no fifth byte); MPEG-1 oddification with bor; dequant shifts; tracks the highest coefficient so the
  IDCT skips the zero rows; toRgba / toRgba422 through shifted-channel lookup tables (one rshift + one index per
  channel, the alpha folded in), two rows per chroma row without a per-pixel branch
- codecs/video/Dsp: idct(blk, dst, off, stride, add, last?): pass 1 only over the rows up to last's, pass 2 with
  the rows-4..7-zero branch tree when they are, fused with the put / add (rounding and the CLAMP offset in one
  division); mc: an unrolled whole-sample copy for 8 / 16 wide blocks, the 4-tap case a column at a time reusing
  each row's pair sums
- codecs/image/Yuv.toRgba: per-matrix luma table, shifted-channel clip tables, chroma once per 2x2
- codecs/video/H264: prediction scratch as buffers (put = u32 copy, bi-pred average four samples at a time),
  whole-sample single-list blocks copied straight from the reference, implicit / identity weights routed to the
  plain paths, 2-tap chroma when a fraction is 0 and the bilinear taps slide along the row, six-tap with two
  multiplies; deblocking skips the inner edges of macroblocks with one motion and no luma coefficients
  (S.flatTab) and finds equal motion before motionDiffers
- codecs/image/Vp8/Predict: six-tap filters with a sliding window (one read per sample)
- --!optimize 2 on every video codec / container module that lacked it (the bundles already force it)
- tools/bench_video.luau: the video fps lines on their own (sections, "once", "jitdefault"), more MPEG-1 clips
This commit is contained in:
4DBug committed 2026-11-01 20:54:46 -06:00
1 parent 8a706a993c
commit 7a6368242b
44 files changed
+3083 -1599

No files matched your search

+18 -14
View File
@@ -26,10 +26,12 @@ local function sixH(src: buffer, so: number, ss: number, dst: buffer, d: 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
for i = so - 2, so + w - 3 do
local v = readu8(src, i) * t0 + readu8(src, i + 1) * t1 + readu8(src, i + 2) * t2 + readu8(src, i + 3) * t3
+ readu8(src, i + 4) * t4 + readu8(src, i + 5) * t5 + 64
writeu8(dst, d + i - so + 2, if v < 0 then 0 elseif v >= 32768 then 255 else rshift(v, 7))
local a0, a1, a2, a3, a4 = readu8(src, so - 2), readu8(src, so - 1), readu8(src, so), readu8(src, so + 1), readu8(src, so + 2)
for i = 0, w - 1 do
local a5 = readu8(src, so + i + 3)
local v = a0 * t0 + a1 * t1 + a2 * t2 + a3 * t3 + a4 * t4 + a5 * t5 + 64
writeu8(dst, d + i, if v < 0 then 0 elseif v >= 32768 then 255 else rshift(v, 7))
a0, a1, a2, a3, a4 = a1, a2, a3, a4, a5
end
so += ss
d += ds
@@ -39,17 +41,19 @@ end
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]
local s1, s2, s3 = ss, 2 * ss, 3 * ss
for _ = 1, h do
local p = so - s2
for i = 0, w - 1 do
local q = p + i
local v = readu8(src, q) * t0 + readu8(src, q + s1) * t1 + readu8(src, q + s2) * t2 + readu8(src, q + s3) * t3
+ readu8(src, q + s2 + s2) * t4 + readu8(src, q + s2 + s3) * t5 + 64
writeu8(dst, d + i, if v < 0 then 0 elseif v >= 32768 then 255 else rshift(v, 7))
for i = 0, w - 1 do
local q = so + i
local a0, a1, a2, a3, a4 = readu8(src, q - 2 * ss), readu8(src, q - ss), readu8(src, q), readu8(src, q + ss), readu8(src, q + 2 * ss)
q += 3 * ss
local o = d + i
for _ = 1, h do
local a5 = readu8(src, q)
local v = a0 * t0 + a1 * t1 + a2 * t2 + a3 * t3 + a4 * t4 + a5 * t5 + 64
writeu8(dst, o, if v < 0 then 0 elseif v >= 32768 then 255 else rshift(v, 7))
a0, a1, a2, a3, a4 = a1, a2, a3, a4, a5
q += ss
o += ds
end
so += ss
d += ds
end
end
+1
View File
@@ -1,4 +1,5 @@
--!strict
--!optimize 2
-- codecs/image/Vp8/Tables: the VP8 tables of RFC 6386 (sections 11.5, 13.4, 13.5, 14.1, 16-18), from libwebp 1.6 src/dec
-- (BSD-3-Clause, licenses/libwebp-LICENSE.txt) and libvpx 1.17 vp8/common (BSD-3-Clause, licenses/libvpx-LICENSE.txt).
-- Flat 1-based arrays.
+1
View File
@@ -1,4 +1,5 @@
--!strict
--!optimize 2
-- codecs/image/Vp8: the VP8 video codec (RFC 6386), decode only.
-- Key frames ported from libwebp 1.6 (BSD-3-Clause, licenses/libwebp-LICENSE.txt), inter frames after libvpx 1.17
-- (BSD-3-Clause, licenses/libvpx-LICENSE.txt); bit-exact with both. Scale bits are ignored, as libwebp and ffmpeg do.
+51 -14
View File
@@ -19,7 +19,7 @@ local MATRIX: { [string]: { kr: number, kb: number } } = {
bt709 = { kr = 0.2126, kb = 0.0722 },
}
type Coeffs = { ys: number, yo: number, crv: number, cgu: number, cgv: number, cbu: number, cs: number }
type Coeffs = { ys: number, yo: number, crv: number, cgu: number, cgv: number, cbu: number, cs: number, yt: { number } }
local cache: { [string]: Coeffs } = {}
local function coeffs(range: string?, matrix: string?): Coeffs
@@ -42,7 +42,11 @@ local function coeffs(range: string?, matrix: string?): Coeffs
cgu = floor(2 * (1 - kb) * kb / kg * cs * 65536 + 0.5),
cgv = floor(2 * (1 - kr) * kr / kg * cs * 65536 + 0.5),
cs = cs,
yt = table.create(256, 0),
}
for v = 0, 255 do
c.yt[v + 1] = (c.ys * (v - c.yo) + 32768) // 65536 + 1024
end
cache[key] = c
return c
end
@@ -51,34 +55,67 @@ local CLIP = bcreate(2304)
for i = 0, 2303 do
writeu8(CLIP, i, math.clamp(i - 1024, 0, 255))
end
-- CLIP's values shifted into their rgba8 byte (CB carries the alpha); indexes stay within 600..1700
local CR: { number } = table.create(2304, 0)
local CG: { number } = table.create(2304, 0)
local CB: { number } = table.create(2304, 0)
for i = 1, 2304 do
local v = math.clamp(i - 1024, 0, 255)
CR[i] = v
CG[i] = v * 256
CB[i] = v * 65536 + 0xff000000
end
function Yuv.toRgba(src: buffer, w: number, h: number, uo: number, vo: number, cw: number, range: string?, matrix: string?, out: buffer?): buffer
local dst = out or bcreate(w * h * 4)
local c = coeffs(range, matrix)
local ys, yo, crv, cgu, cgv, cbu = c.ys, c.yo, c.crv, c.cgu, c.cgv, c.cbu
local crv, cgu, cgv, cbu = c.crv, c.cgu, c.cgv, c.cbu
local yt, cr, cg, cb = c.yt, CR, CG, CB
local ch = (h + 1) // 2
if buffer.len(src) < vo + cw * ch or buffer.len(dst) < w * h * 4 or uo + cw * ch > buffer.len(src) then
return dst
end
for y = 0, h - 1 do
local w2 = w // 2
local stride = w * 4
for y = 0, h - 1, 2 do
local yrow = y * w
local crow = (y // 2) * cw
local ci = uo + (y // 2) * cw
local dv = vo - uo
local o = yrow * 4
for x = 0, w - 1, 2 do
local ci = crow + (x // 2)
local u = readu8(src, uo + ci) - 128
local v = readu8(src, vo + ci) - 128
local two = y + 1 < h
for _ = 1, w2 do
local u = readu8(src, ci) - 128
local v = readu8(src, ci + dv) - 128
ci += 1
local rv = (crv * v + 32768) // 65536
local gv = (cgu * u + cgv * v + 32768) // 65536
local gv = -((cgu * u + cgv * v + 32768) // 65536)
local bu = (cbu * u + 32768) // 65536
local yy = (ys * (readu8(src, yrow + x) - yo) + 32768) // 65536 + 1024
writeu32(dst, o, readu8(CLIP, yy + rv) + readu8(CLIP, yy - gv) * 256 + readu8(CLIP, yy + bu) * 65536 + 0xff000000)
if x + 1 < w then
yy = (ys * (readu8(src, yrow + x + 1) - yo) + 32768) // 65536 + 1024
writeu32(dst, o + 4, readu8(CLIP, yy + rv) + readu8(CLIP, yy - gv) * 256 + readu8(CLIP, yy + bu) * 65536 + 0xff000000)
local yy = yt[readu8(src, yrow) + 1]
writeu32(dst, o, cr[yy + rv] + cg[yy + gv] + cb[yy + bu])
yy = yt[readu8(src, yrow + 1) + 1]
writeu32(dst, o + 4, cr[yy + rv] + cg[yy + gv] + cb[yy + bu])
if two then
yy = yt[readu8(src, yrow + w) + 1]
writeu32(dst, o + stride, cr[yy + rv] + cg[yy + gv] + cb[yy + bu])
yy = yt[readu8(src, yrow + w + 1) + 1]
writeu32(dst, o + stride + 4, cr[yy + rv] + cg[yy + gv] + cb[yy + bu])
end
yrow += 2
o += 8
end
if w % 2 == 1 then
local u = readu8(src, ci) - 128
local v = readu8(src, ci + dv) - 128
local rv = (crv * v + 32768) // 65536
local gv = -((cgu * u + cgv * v + 32768) // 65536)
local bu = (cbu * u + 32768) // 65536
local yy = yt[readu8(src, yrow) + 1]
writeu32(dst, o, cr[yy + rv] + cg[yy + gv] + cb[yy + bu])
if two then
yy = yt[readu8(src, yrow + w) + 1]
writeu32(dst, o + stride, cr[yy + rv] + cg[yy + gv] + cb[yy + bu])
end
end
end
return dst
end