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:
1 parent
8a706a993c
commit
7a6368242b
44 files changed
+3083
-1599
No files matched your search
@@ -1,5 +1,6 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- codecs/video/Mpeg2/Decoder: the MPEG-1 / MPEG-2 video decoding core (ISO/IEC 11172-2, 13818-2 Main profile).
|
||||
-- From mpeg1-luau e2e2898 (same author; a port of jsmpeg's src/mpeg1.js by Dominic Szablewski, MIT,
|
||||
-- licenses/jsmpeg-MIT.txt). Dequantisation follows ffmpeg's arithmetic; coefficients are saturated to -2048..2047
|
||||
@@ -48,7 +49,6 @@ for i = 0, 31 do
|
||||
buffer.writeu8(NON_LINEAR, i, T.NON_LINEAR_QSCALE[i])
|
||||
end
|
||||
|
||||
local COFF = 40000
|
||||
|
||||
local MBAI, MBAI_BITS = V.MBAI, V.MBAI_BITS
|
||||
local CBP, CBP_BITS = V.CBP, V.CBP_BITS
|
||||
@@ -63,15 +63,20 @@ local MBTYPE_BITS = { V.MBTYPE_I_BITS, V.MBTYPE_P_BITS, V.MBTYPE_B_BITS }
|
||||
-- BT.601 limited range: R = 1.164 (Y - 16) + 1.596 (Cr - 128), G = 1.164 (Y - 16) - 0.392 (Cb - 128) - 0.813 (Cr -
|
||||
-- 128), B = 1.164 (Y - 16) + 2.017 (Cb - 128), in 8-bit fixed point. The rounding offsets (-128, -160, -72 / 256)
|
||||
-- are fitted to ffmpeg's swscale output, which lands about half a level below round-to-nearest.
|
||||
-- the constant terms, plus COFF * 256 so the sums stay positive for rshift
|
||||
local RBIAS = COFF * 256 - 298 * 16 - 409 * 128 - 128
|
||||
local GBIAS = COFF * 256 - 298 * 16 + 100 * 128 + 208 * 128 - 160
|
||||
local BBIAS = COFF * 256 - 298 * 16 - 516 * 128 - 72
|
||||
|
||||
-- clamp by lookup: RGB_CLAMP[v + COFF] for v in [-COFF, COFF + 255]; the colour biases include COFF * 256
|
||||
local RGB_CLAMP = buffer.create(2 * COFF + 256)
|
||||
for i = 0, 2 * COFF + 255 do
|
||||
buffer.writeu8(RGB_CLAMP, i, math.clamp(i - COFF, 0, 255))
|
||||
-- the constant terms, plus TOFF * 256: (sum + bias) >> 8 lies in 747..1558 for 8-bit samples, the index of the
|
||||
-- clamped channel already shifted into place (TB carries the alpha)
|
||||
local TOFF = 1024
|
||||
local TRB = TOFF * 256 - 298 * 16 - 409 * 128 - 128
|
||||
local TGB = TOFF * 256 - 298 * 16 + 100 * 128 + 208 * 128 - 160
|
||||
local TBB = TOFF * 256 - 298 * 16 - 516 * 128 - 72
|
||||
local TR: { number } = table.create(2048, 0)
|
||||
local TG: { number } = table.create(2048, 0)
|
||||
local TB: { number } = table.create(2048, 0)
|
||||
for i = 1, 2048 do
|
||||
local v = math.clamp(i - TOFF, 0, 255)
|
||||
TR[i] = v
|
||||
TG[i] = v * 256
|
||||
TB[i] = v * 65536 + 0xFF000000
|
||||
end
|
||||
|
||||
-- 4:2:0 -> RGBA8 with nearest chroma, like ffmpeg's default swscale path; hook is pcall'ed (can't throw through here)
|
||||
@@ -83,41 +88,56 @@ local function toRgba(Y: buffer, Cb: buffer, Cr: buffer, cw: number, w: number,
|
||||
local chw = cw // 2
|
||||
local w2 = w // 2 -- whole pixel pairs per row
|
||||
local stride = w * 4
|
||||
local C = RGB_CLAMP
|
||||
local tr, tg, tb = TR, TG, TB
|
||||
local n = 0
|
||||
for y = r0, r1 - 1, 2 do
|
||||
local yo = y * cw
|
||||
local co = (y // 2) * chw
|
||||
local o = base + (y - r0) * stride
|
||||
local two = y + 1 < r1
|
||||
for _ = 1, w2 do
|
||||
local cb, cr = readu8(Cb, co), readu8(Cr, co)
|
||||
co += 1
|
||||
local rr = cr * 409 + RBIAS
|
||||
local gg = GBIAS - cb * 100 - cr * 208
|
||||
local bb = cb * 516 + BBIAS
|
||||
local yv = readu8(Y, yo) * 298
|
||||
writeu32(out, o, readu8(C, rshift(yv + rr, 8)) + readu8(C, rshift(yv + gg, 8)) * 256 + readu8(C, rshift(yv + bb, 8)) * 65536 + 0xFF000000)
|
||||
yv = readu8(Y, yo + 1) * 298
|
||||
writeu32(out, o + 4, readu8(C, rshift(yv + rr, 8)) + readu8(C, rshift(yv + gg, 8)) * 256 + readu8(C, rshift(yv + bb, 8)) * 65536 + 0xFF000000)
|
||||
if two then
|
||||
if y + 1 < r1 then
|
||||
local o2 = o + stride
|
||||
for _ = 1, w2 do
|
||||
local cb, cr = readu8(Cb, co), readu8(Cr, co)
|
||||
co += 1
|
||||
local rr = cr * 409 + TRB
|
||||
local gg = TGB - cb * 100 - cr * 208
|
||||
local bb = cb * 516 + TBB
|
||||
local yv = readu8(Y, yo) * 298
|
||||
writeu32(out, o, tr[rshift(yv + rr, 8)] + tg[rshift(yv + gg, 8)] + tb[rshift(yv + bb, 8)])
|
||||
yv = readu8(Y, yo + 1) * 298
|
||||
writeu32(out, o + 4, tr[rshift(yv + rr, 8)] + tg[rshift(yv + gg, 8)] + tb[rshift(yv + bb, 8)])
|
||||
yv = readu8(Y, yo + cw) * 298
|
||||
writeu32(out, o + stride, readu8(C, rshift(yv + rr, 8)) + readu8(C, rshift(yv + gg, 8)) * 256 + readu8(C, rshift(yv + bb, 8)) * 65536 + 0xFF000000)
|
||||
writeu32(out, o2, tr[rshift(yv + rr, 8)] + tg[rshift(yv + gg, 8)] + tb[rshift(yv + bb, 8)])
|
||||
yv = readu8(Y, yo + cw + 1) * 298
|
||||
writeu32(out, o + stride + 4, readu8(C, rshift(yv + rr, 8)) + readu8(C, rshift(yv + gg, 8)) * 256 + readu8(C, rshift(yv + bb, 8)) * 65536 + 0xFF000000)
|
||||
writeu32(out, o2 + 4, tr[rshift(yv + rr, 8)] + tg[rshift(yv + gg, 8)] + tb[rshift(yv + bb, 8)])
|
||||
yo += 2
|
||||
o += 8
|
||||
o2 += 8
|
||||
end
|
||||
else
|
||||
for _ = 1, w2 do
|
||||
local cb, cr = readu8(Cb, co), readu8(Cr, co)
|
||||
co += 1
|
||||
local rr = cr * 409 + TRB
|
||||
local gg = TGB - cb * 100 - cr * 208
|
||||
local bb = cb * 516 + TBB
|
||||
local yv = readu8(Y, yo) * 298
|
||||
writeu32(out, o, tr[rshift(yv + rr, 8)] + tg[rshift(yv + gg, 8)] + tb[rshift(yv + bb, 8)])
|
||||
yv = readu8(Y, yo + 1) * 298
|
||||
writeu32(out, o + 4, tr[rshift(yv + rr, 8)] + tg[rshift(yv + gg, 8)] + tb[rshift(yv + bb, 8)])
|
||||
yo += 2
|
||||
o += 8
|
||||
end
|
||||
yo += 2
|
||||
o += 8
|
||||
end
|
||||
if w % 2 == 1 then
|
||||
-- the last column of an odd width
|
||||
local cb, cr = readu8(Cb, co), readu8(Cr, co)
|
||||
local rr = cr * 409 + RBIAS
|
||||
local gg = GBIAS - cb * 100 - cr * 208
|
||||
local bb = cb * 516 + BBIAS
|
||||
for k = 0, if two then 1 else 0 do
|
||||
local rr = cr * 409 + TRB
|
||||
local gg = TGB - cb * 100 - cr * 208
|
||||
local bb = cb * 516 + TBB
|
||||
for k = 0, if y + 1 < r1 then 1 else 0 do
|
||||
local yv = readu8(Y, yo + k * cw) * 298
|
||||
writeu32(out, o + k * stride, readu8(C, rshift(yv + rr, 8)) + readu8(C, rshift(yv + gg, 8)) * 256 + readu8(C, rshift(yv + bb, 8)) * 65536 + 0xFF000000)
|
||||
writeu32(out, o + k * stride, tr[rshift(yv + rr, 8)] + tg[rshift(yv + gg, 8)] + tb[rshift(yv + bb, 8)])
|
||||
end
|
||||
end
|
||||
n += 2
|
||||
@@ -132,16 +152,14 @@ D.toRgba = toRgba
|
||||
-- YCbCr 4:2:2 planes -> RGBA8: chroma row y for luma row y, chroma column x // 2
|
||||
local function toRgba422(Y: buffer, Cb: buffer, Cr: buffer, cw: number, w: number, h: number, out: buffer)
|
||||
local chw = cw // 2
|
||||
local tr, tg, tb = TR, TG, TB
|
||||
for y = 0, h - 1 do
|
||||
local o = y * w * 4
|
||||
for x = 0, w - 1 do
|
||||
local c = y * chw + x // 2
|
||||
local cb, cr = readu8(Cb, c), readu8(Cr, c)
|
||||
local yv = readu8(Y, y * cw + x) * 298
|
||||
local r = math.clamp(rshift(yv + cr * 409 + RBIAS, 8) - COFF, 0, 255)
|
||||
local g = math.clamp(rshift(yv + GBIAS - cb * 100 - cr * 208, 8) - COFF, 0, 255)
|
||||
local b = math.clamp(rshift(yv + cb * 516 + BBIAS, 8) - COFF, 0, 255)
|
||||
writeu32(out, o + x * 4, r + g * 256 + b * 65536 + 0xFF000000)
|
||||
writeu32(out, o + x * 4, tr[rshift(yv + cr * 409 + TRB, 8)] + tg[rshift(yv + TGB - cb * 100 - cr * 208, 8)] + tb[rshift(yv + cb * 516 + TBB, 8)])
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -723,7 +741,7 @@ function D.new(maxPixels: number?): Core
|
||||
local function decodeBlock(b: number): boolean
|
||||
local n = 0
|
||||
local q: buffer
|
||||
local onlyDc = true
|
||||
local last = 0 -- the highest natural index written
|
||||
local luma = b < 4
|
||||
local parityOdd = 1 -- MPEG-2 mismatch control: 1 while the coefficient sum is even
|
||||
if mbIntra then
|
||||
@@ -783,31 +801,30 @@ function D.new(maxPixels: number?): Core
|
||||
local intra = mbIntra
|
||||
local qs = qscale
|
||||
local m2 = mpeg2
|
||||
local d = data
|
||||
local p = pos
|
||||
while true do
|
||||
local bp = rshift(pos, 3)
|
||||
local w = bswap(readu32(data, bp))
|
||||
local s = band(pos, 7)
|
||||
if s ~= 0 then
|
||||
w = bor(lshift(w, s), rshift(readu8(data, bp + 4), 8 - s))
|
||||
end
|
||||
-- 25 valid bits at least: the longest code (16) and its sign
|
||||
local w = lshift(bswap(readu32(d, rshift(p, 3))), band(p, 7))
|
||||
local e = readu32(lut, rshift(w, 24) * 4)
|
||||
if e >= SUB then
|
||||
e = readu32(lut, (band(e, 0xFFFF) * 256 + band(rshift(w, 16), 255)) * 4)
|
||||
end
|
||||
local l = rshift(e, 16)
|
||||
if l == 0 then
|
||||
pos = p
|
||||
err = true
|
||||
return false
|
||||
end
|
||||
local v = band(e, 0xFFFF)
|
||||
lut = nextLut
|
||||
if v == EOB then
|
||||
pos += l
|
||||
p += l
|
||||
break
|
||||
end
|
||||
local run, level, neg
|
||||
if v == ESC then
|
||||
pos += l
|
||||
pos = p + l
|
||||
run = read(6)
|
||||
if m2 then
|
||||
-- a 12-bit signed level
|
||||
@@ -834,14 +851,16 @@ function D.new(maxPixels: number?): Core
|
||||
err = true
|
||||
return false
|
||||
end
|
||||
p = pos
|
||||
else
|
||||
run = rshift(v, 8)
|
||||
level = band(v, 255)
|
||||
neg = band(lshift(w, l), 0x80000000) ~= 0 -- the sign bit follows the code
|
||||
pos += l + 1
|
||||
p += l + 1
|
||||
end
|
||||
n += run
|
||||
if n > 63 then
|
||||
pos = p
|
||||
err = true
|
||||
return false
|
||||
end
|
||||
@@ -849,15 +868,15 @@ function D.new(maxPixels: number?): Core
|
||||
-- dequantise the magnitude (ffmpeg's arithmetic); MPEG-1 makes it odd toward zero
|
||||
local m
|
||||
if intra then
|
||||
m = (level * qs * readu8(q, zz)) // 16
|
||||
m = rshift(level * qs * readu8(q, zz), 4)
|
||||
else
|
||||
m = ((level * 2 + 1) * qs * readu8(q, zz)) // 32
|
||||
m = rshift((level * 2 + 1) * qs * readu8(q, zz), 5)
|
||||
end
|
||||
if not m2 then
|
||||
if m == 0 then
|
||||
m = -1
|
||||
elseif band(m, 1) == 0 then
|
||||
m -= 1
|
||||
else
|
||||
m = bor(m - 1, 1)
|
||||
end
|
||||
end
|
||||
if neg then
|
||||
@@ -872,17 +891,18 @@ function D.new(maxPixels: number?): Core
|
||||
elseif m < -2048 then
|
||||
m = -2048
|
||||
end
|
||||
if zz ~= 0 then
|
||||
onlyDc = false
|
||||
if zz > last then
|
||||
last = zz
|
||||
end
|
||||
writei32(blk, zz * 4, m)
|
||||
n += 1
|
||||
end
|
||||
pos = p
|
||||
if m2 and parityOdd == 1 then
|
||||
-- mismatch control: toggle the last coefficient's lowest bit
|
||||
local v = readi32(blk, 252)
|
||||
writei32(blk, 252, if v % 2 ~= 0 then v - 1 else v + 1)
|
||||
onlyDc = false
|
||||
last = 63
|
||||
end
|
||||
local dst, off, stride
|
||||
if luma then
|
||||
@@ -917,12 +937,12 @@ function D.new(maxPixels: number?): Core
|
||||
off = (mbRow * 16 + k * 8) * chw + mbCol * 8
|
||||
end
|
||||
end
|
||||
if onlyDc then
|
||||
if last == 0 then
|
||||
fillBlock(dst, off, stride, (readi32(blk, 0) + 4) // 8, not intra)
|
||||
writei32(blk, 0, 0)
|
||||
else
|
||||
idct(blk, dst, off, stride, not intra)
|
||||
buffer.fill(blk, 0, 0, 256)
|
||||
idct(blk, dst, off, stride, not intra, last)
|
||||
buffer.fill(blk, 0, 0, (last // 8 + 1) * 32)
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
Reference in new issue
Block a user