perf(image): VP8L literal symbols inline, cross-colour identity tiles skipped, T / TR / TL predictors and SWAR addPixels; JPEG IDCT 5x5 path, gray / 4:4:4 colour fast paths; QOI index as tables; TGA rows without per-pixel div/mod; BC7 / BC6H bit reads from a word table; bench_image: bcn / exr

This commit is contained in:
4DBug committed 2026-11-01 19:41:47 -06:00
1 parent 9855a7edd6
commit 903e239a0e
7 files changed
+293 -121

No files matched your search

+8 -13
View File
@@ -175,21 +175,16 @@ local function subset(ns: number, part: number, i: number): number
return 0
end
local w0, w1, w2, w3 = 0, 0, 0, 0
-- the block's four u32 words, then zero words (a field reaching past bit 127 reads zeros)
local W = { 0, 0, 0, 0, 0, 0 }
local function load(src: buffer, p: number)
w0, w1, w2, w3 = readu32(src, p), readu32(src, p + 4), readu32(src, p + 8), readu32(src, p + 12)
end
local function word(i: number): number
return if i == 0 then w0 elseif i == 1 then w1 elseif i == 2 then w2 else w3
W[1], W[2], W[3], W[4] = readu32(src, p), readu32(src, p + 4), readu32(src, p + 8), readu32(src, p + 12)
end
local function bits(at: number, n: number): number
if n == 0 then
return 0
end
local wi, sh = rshift(at, 5), band(at, 31)
local v = rshift(word(wi), sh)
if sh + n > 32 and wi < 3 then
v = bor(v, lshift(word(wi + 1), 32 - sh))
local wi, sh = rshift(at, 5) + 1, band(at, 31)
local v = rshift(W[wi], sh)
if sh + n > 32 then
v = bor(v, lshift(W[wi + 1], 32 - sh))
end
return band(v, lshift(1, n) - 1)
end
@@ -412,7 +407,7 @@ end
function Decode.bc6h(src: buffer, p: number, blk: buffer, signed: boolean)
load(src, p)
local m = band(w0, 31)
local m = band(W[1], 31)
local mode, bit, epbits, ib
local low = band(m, 3)
if low < 2 then
+18 -14
View File
@@ -124,18 +124,18 @@ local function idct8(coef: buffer, off: number, qt: { number }, plane: buffer, o
end
end
-- idct8 with every nonzero coefficient in the top-left 4 x 4 (zigzag index <= 9): the same sums without the zero terms
local function idct8x4(coef: buffer, off: number, qt: { number }, plane: buffer, out: number, stride: number, p1: number, center: number, maxv: number)
-- idct8 with every nonzero coefficient in the top-left 5 x 5 (zigzag index <= 14): the same sums without the zero terms
local function idct8x5(coef: buffer, off: number, qt: { number }, plane: buffer, out: number, stride: number, p1: number, center: number, maxv: number)
local ws = ws
local s1 = 2 ^ (13 - p1)
local h1 = s1 / 2
local s2 = 2 ^ (16 + p1)
local h2 = s2 / 2 + center * s2
local dcm = 2 ^ p1
for c = 0, 3 do
for c = 0, 4 do
local o = off + c * 2
local c1, c2, c3 = readi16(coef, o + 16), readi16(coef, o + 32), readi16(coef, o + 48)
if c1 == 0 and c2 == 0 and c3 == 0 then
local c1, c2, c3, c4 = readi16(coef, o + 16), readi16(coef, o + 32), readi16(coef, o + 48), readi16(coef, o + 64)
if c1 == 0 and c2 == 0 and c3 == 0 and c4 == 0 then
local dc = readi16(coef, o) * qt[c + 1] * dcm
ws[c + 1] = dc
ws[c + 9] = dc
@@ -149,15 +149,18 @@ local function idct8x4(coef: buffer, off: number, qt: { number }, plane: buffer,
local z2 = c2 * qt[c + 17]
local tmp2 = z2 * 4433
local tmp3 = tmp2 + z2 * 6270
local tmp0 = readi16(coef, o) * qt[c + 1] * 8192 + h1
z2 = readi16(coef, o) * qt[c + 1]
local z3 = c4 * qt[c + 33]
local tmp0 = (z2 + z3) * 8192 + h1
local tmp1 = (z2 - z3) * 8192 + h1
local tmp10, tmp13 = tmp0 + tmp3, tmp0 - tmp3
local tmp11, tmp12 = tmp0 + tmp2, tmp0 - tmp2
local tmp11, tmp12 = tmp1 + tmp2, tmp1 - tmp2
local t2 = c3 * qt[c + 25]
local t3 = c1 * qt[c + 9]
local z5 = (t2 + t3) * 9633
local z1 = t3 * -7373
z2 = t2 * -20995
local z3 = t2 * -16069 + z5
z3 = t2 * -16069 + z5
local z4 = t3 * -3196 + z5
local t0 = z1 + z3
local t1 = z2 + z4
@@ -177,9 +180,9 @@ local function idct8x4(coef: buffer, off: number, qt: { number }, plane: buffer,
local hd = sd / 2 + center * sd
for r = 0, 7 do
local b = r * 8
local w0, w1, w2, w3 = ws[b + 1], ws[b + 2], ws[b + 3], ws[b + 4]
local w0, w1, w2, w3, w4 = ws[b + 1], ws[b + 2], ws[b + 3], ws[b + 4], ws[b + 5]
local o = (out + r * stride) * 2
if w1 == 0 and w2 == 0 and w3 == 0 then
if w1 == 0 and w2 == 0 and w3 == 0 and w4 == 0 then
local v = clamp((w0 + hd) // sd, 0, maxv) * 65537
writeu32(plane, o, v)
writeu32(plane, o + 4, v)
@@ -188,9 +191,10 @@ local function idct8x4(coef: buffer, off: number, qt: { number }, plane: buffer,
else
local tmp2 = w2 * 4433
local tmp3 = tmp2 + w2 * 6270
local tmp0 = w0 * 8192 + h2
local tmp0 = (w0 + w4) * 8192 + h2
local tmp1 = (w0 - w4) * 8192 + h2
local tmp10, tmp13 = tmp0 + tmp3, tmp0 - tmp3
local tmp11, tmp12 = tmp0 + tmp2, tmp0 - tmp2
local tmp11, tmp12 = tmp1 + tmp2, tmp1 - tmp2
local z5 = (w3 + w1) * 9633
local z1 = w1 * -7373
local z2 = w3 * -20995
@@ -347,8 +351,8 @@ function Dct.idctComponent(coef: buffer, q: buffer, bpl: number, bpc: number, si
local k = readu8(nz, bi)
if k == 0 then
idct8dc(coef, off, qt, plane, out, stride, p1, center, maxv)
elseif k <= 9 then
idct8x4(coef, off, qt, plane, out, stride, p1, center, maxv)
elseif k <= 14 then
idct8x5(coef, off, qt, plane, out, stride, p1, center, maxv)
else
idct8(coef, off, qt, plane, out, stride, p1, center, maxv)
end
+10 -10
View File
@@ -211,10 +211,7 @@ local function ycc8(P: Params): boolean
local d = cb0 - yb
for i = yb, yb + outW * 2 - 2, 2 do
local yv, cb, cr = readu16(py, i), readu16(pcb, i + d), readu16(pcr, i + d)
local r = clamp(yv + (FIX_R * cr + KR) // 65536, 0, 255)
local g = clamp(yv + (KG - FIX_GB * cb - FIX_GR * cr) // 65536, 0, 255)
local b = clamp(yv + (FIX_B * cb + KB) // 65536, 0, 255)
writeu32(out, o, 0xFF000000 + r + g * 256 + b * 65536)
writeu32(out, o, LR[yv + (FIX_R * cr + KRo) // 65536] + LG[yv + (KGo - FIX_GB * cb - FIX_GR * cr) // 65536] + LB[yv + (FIX_B * cb + KBo) // 65536])
o += 4
end
continue
@@ -279,13 +276,16 @@ function Color.run(P: Params)
end
local s0, b0 = fetch(comps[1], y, outW)
if space == 0 or nc < 3 then
for x = 0, outW - 1 do
local v = readu16(s0, b0 + x * 2)
if toByte then
v = readu8(toByte, v)
if toByte then
for x = 0, outW - 1 do
writeu32(out, o, 0xFF000000 + readu8(toByte, readu16(s0, b0 + x * 2)) * 0x10101)
o += 4
end
else
for i = b0, b0 + outW * 2 - 2, 2 do
writeu32(out, o, 0xFF000000 + readu16(s0, i) * 0x10101)
o += 4
end
writeu32(out, o, 0xFF000000 + v * 0x10101)
o += 4
end
else
local s1, b1 = fetch(comps[2], y, outW)
+102 -15
View File
@@ -337,6 +337,7 @@ local function decodeData(d: State, data: buffer, w: number, h: number, cacheBit
local hmask = if himg then lshift(1, hbits) - 1 else -1
local i, col, row = 0, 0, 0
local gG, gR, gB, gA, gD = groups[1], groups[2], groups[3], groups[4], groups[5]
local tab = d.tab
local function setGroup()
if himg then
local g = readu32(himg, (hxs * rshift(row, hbits) + rshift(col, hbits)) * 4) * 5
@@ -347,12 +348,79 @@ local function decodeData(d: State, data: buffer, w: number, h: number, cacheBit
if band(col, hmask) == 0 then
setGroup()
end
local code = readSymbol(d, gG)
-- readSymbol inlined for the literal path (the bit state in locals, written back before any call)
local bb, bc = d.bitbuf, d.bitcnt
if bc < MAX_LEN then
d.bitbuf, d.bitcnt = bb, bc
fill(d)
bb, bc = d.bitbuf, d.bitcnt
end
local ecode = readu32(tab, (gG + band(bb, 255)) * 4)
local ncode = band(ecode, 31)
if ncode > ROOT_BITS then
local idx = gG + band(bb, 255)
bb = rshift(bb, ROOT_BITS)
bc -= ROOT_BITS
ecode = readu32(tab, (idx + rshift(ecode, 5) + band(bb, lshift(1, ncode - ROOT_BITS) - 1)) * 4)
ncode = band(ecode, 31)
end
bb = rshift(bb, ncode)
bc -= ncode
local code = rshift(ecode, 5)
if code < NUM_LITERAL then
local r = readSymbol(d, gR)
local b = readSymbol(d, gB)
local a = readSymbol(d, gA)
if d.bitcnt < d.over * 8 then
if bc < MAX_LEN then
d.bitbuf, d.bitcnt = bb, bc
fill(d)
bb, bc = d.bitbuf, d.bitcnt
end
local er = readu32(tab, (gR + band(bb, 255)) * 4)
local nr = band(er, 31)
if nr > ROOT_BITS then
local idx = gR + band(bb, 255)
bb = rshift(bb, ROOT_BITS)
bc -= ROOT_BITS
er = readu32(tab, (idx + rshift(er, 5) + band(bb, lshift(1, nr - ROOT_BITS) - 1)) * 4)
nr = band(er, 31)
end
bb = rshift(bb, nr)
bc -= nr
local r = rshift(er, 5)
if bc < MAX_LEN then
d.bitbuf, d.bitcnt = bb, bc
fill(d)
bb, bc = d.bitbuf, d.bitcnt
end
local eb = readu32(tab, (gB + band(bb, 255)) * 4)
local nb = band(eb, 31)
if nb > ROOT_BITS then
local idx = gB + band(bb, 255)
bb = rshift(bb, ROOT_BITS)
bc -= ROOT_BITS
eb = readu32(tab, (idx + rshift(eb, 5) + band(bb, lshift(1, nb - ROOT_BITS) - 1)) * 4)
nb = band(eb, 31)
end
bb = rshift(bb, nb)
bc -= nb
local b = rshift(eb, 5)
if bc < MAX_LEN then
d.bitbuf, d.bitcnt = bb, bc
fill(d)
bb, bc = d.bitbuf, d.bitcnt
end
local ea = readu32(tab, (gA + band(bb, 255)) * 4)
local na = band(ea, 31)
if na > ROOT_BITS then
local idx = gA + band(bb, 255)
bb = rshift(bb, ROOT_BITS)
bc -= ROOT_BITS
ea = readu32(tab, (idx + rshift(ea, 5) + band(bb, lshift(1, na - ROOT_BITS) - 1)) * 4)
na = band(ea, 31)
end
bb = rshift(bb, na)
bc -= na
local a = rshift(ea, 5)
d.bitbuf, d.bitcnt = bb, bc
if bc < d.over * 8 then
break
end
local argb = a * 16777216 + r * 65536 + code * 256 + b
@@ -367,6 +435,7 @@ local function decodeData(d: State, data: buffer, w: number, h: number, cacheBit
row += 1
end
elseif code < lenLimit then
d.bitbuf, d.bitcnt = bb, bc
local ls = code - NUM_LITERAL
local length
if ls < 4 then
@@ -422,6 +491,7 @@ local function decodeData(d: State, data: buffer, w: number, h: number, cacheBit
setGroup()
end
elseif code < cacheLimit and cache then
d.bitbuf, d.bitcnt = bb, bc
if d.bitcnt < d.over * 8 then
break
end
@@ -444,8 +514,9 @@ local function decodeData(d: State, data: buffer, w: number, h: number, cacheBit
return true, nil
end
-- per-byte sums mod 256 (the low seven bits added, the top bits xored in)
local function addPixels(a: number, b: number): number
return band(band(a, 0xff00ff00) + band(b, 0xff00ff00), 0xff00ff00) + band(band(a, 0x00ff00ff) + band(b, 0x00ff00ff), 0x00ff00ff)
return bxor(band(a, 0x7f7f7f7f) + band(b, 0x7f7f7f7f), band(bxor(a, b), 0x80808080))
end
Decode.addPixels = addPixels
@@ -553,9 +624,11 @@ local function inversePredictor(data: buffer, w: number, h: number, bits: number
L = addPixels(readu32(data, (row + k) * 4), L)
writeu32(data, (row + k) * 4, L)
end
elseif mode == 2 then
for k = x, xe - 1 do
writeu32(data, (row + k) * 4, addPixels(readu32(data, (row + k) * 4), readu32(data, (up + k) * 4)))
elseif mode == 2 or mode == 3 or mode == 4 then
-- T, TR, TL: the upper row only
local d = (up - row + (if mode == 2 then 0 elseif mode == 3 then 1 else -1)) * 4
for o = (row + x) * 4, (row + xe - 1) * 4, 4 do
writeu32(data, o, addPixels(readu32(data, o), readu32(data, o + d)))
end
else
for k = x, xe - 1 do
@@ -584,12 +657,26 @@ local function inverseCrossColor(data: buffer, w: number, h: number, bits: numbe
local g2b = sint8(band(rshift(m, 8), 255))
local r2b = sint8(band(rshift(m, 16), 255))
local xe = math.min(w, lshift(rshift(x, bits) + 1, bits))
for k = y * w + x, y * w + xe - 1 do
local argb = readu32(data, k * 4)
local g = sint8(band(rshift(argb, 8), 255))
local r = band(band(rshift(argb, 16), 255) + ((g2r * g) // 32), 255)
local b = band(band(argb, 255) + ((g2b * g) // 32) + ((r2b * sint8(r)) // 32), 255)
writeu32(data, k * 4, band(argb, 0xff00ff00) + r * 65536 + b)
if g2r == 0 and g2b == 0 and r2b == 0 then
x = xe -- the identity
continue
end
if r2b == 0 then
for o = (y * w + x) * 4, (y * w + xe - 1) * 4, 4 do
local argb = readu32(data, o)
local g = sint8(band(rshift(argb, 8), 255))
local r = band(band(rshift(argb, 16), 255) + ((g2r * g) // 32), 255)
local b = band(band(argb, 255) + ((g2b * g) // 32), 255)
writeu32(data, o, band(argb, 0xff00ff00) + r * 65536 + b)
end
else
for o = (y * w + x) * 4, (y * w + xe - 1) * 4, 4 do
local argb = readu32(data, o)
local g = sint8(band(rshift(argb, 8), 255))
local r = band(band(rshift(argb, 16), 255) + ((g2r * g) // 32), 255)
local b = band(band(argb, 255) + ((g2b * g) // 32) + ((r2b * sint8(r)) // 32), 255)
writeu32(data, o, band(argb, 0xff00ff00) + r * 65536 + b)
end
end
x = xe
end
+52 -45
View File
@@ -8,8 +8,14 @@ local Codec = {}
local index = buffer.create(256)
-- the decoder's index as four tables (1-based by hash + 1)
local IR, IG, IB, IA = table.create(64, 0), table.create(64, 0), table.create(64, 0), table.create(64, 0)
function Codec.decode(b: buffer, p: number, limit: number, out: buffer, n: number): (boolean, string?)
buffer.fill(index, 0, 0, 256)
local ir, ig, ib, ia = IR, IG, IB, IA
for i = 1, 64 do
ir[i], ig[i], ib[i], ia[i] = 0, 0, 0, 0
end
local r, g, bl, a = 0, 0, 0, 255
local o = 0
local stop = n * 4
@@ -19,62 +25,63 @@ function Codec.decode(b: buffer, p: number, limit: number, out: buffer, n: numbe
end
local op = readu8(b, p)
p += 1
if op == 0xFE then
if p + 3 > limit then
return false, "truncated"
end
r = readu8(b, p)
g = readu8(b, p + 1)
bl = readu8(b, p + 2)
p += 3
elseif op == 0xFF then
if p + 4 > limit then
return false, "truncated"
end
r = readu8(b, p)
g = readu8(b, p + 1)
bl = readu8(b, p + 2)
a = readu8(b, p + 3)
p += 4
else
local tag = op // 64
if tag == 0 then
local q = op * 4
r = readu8(index, q)
g = readu8(index, q + 1)
bl = readu8(index, q + 2)
a = readu8(index, q + 3)
elseif tag == 1 then
r = (r + (op // 16) % 4 - 2) % 256
g = (g + (op // 4) % 4 - 2) % 256
bl = (bl + op % 4 - 2) % 256
elseif tag == 2 then
if p >= limit then
if op >= 0xC0 then
if op == 0xFE then
if p + 3 > limit then
return false, "truncated"
end
local d2 = readu8(b, p)
p += 1
local dg = op % 64 - 32
g = (g + dg) % 256
r = (r + dg + d2 // 16 - 8) % 256
bl = (bl + dg + d2 % 16 - 8) % 256
r = readu8(b, p)
g = readu8(b, p + 1)
bl = readu8(b, p + 2)
p += 3
elseif op == 0xFF then
if p + 4 > limit then
return false, "truncated"
end
r = readu8(b, p)
g = readu8(b, p + 1)
bl = readu8(b, p + 2)
a = readu8(b, p + 3)
p += 4
else
local run = op % 64 -- 1..62 more copies of the previous pixel (63 / 64 are RGB / RGBA)
-- a run of 1..62 copies of the previous pixel (63 / 64 are RGB / RGBA); every op stores its pixel at its
-- hash, so the index holds it already unless the run is the first op
local v = r + g * 256 + bl * 65536 + a * 16777216
local e = math.min(o + run * 4, stop - 4)
local first = o == 0
local e = math.min(o + (op - 0xBF) * 4, stop)
while o < e do
writeu32(out, o, v)
o += 4
end
if not first then
continue
end
local h = (r * 3 + g * 5 + bl * 7 + a * 11) % 64 + 1
ir[h], ig[h], ib[h], ia[h] = r, g, bl, a
continue
end
elseif op < 0x40 then
local q = op + 1
r, g, bl, a = ir[q], ig[q], ib[q], ia[q]
elseif op < 0x80 then
r = (r + (op // 16) % 4 - 2) % 256
g = (g + (op // 4) % 4 - 2) % 256
bl = (bl + op % 4 - 2) % 256
else
if p >= limit then
return false, "truncated"
end
local d2 = readu8(b, p)
p += 1
local dg = op % 64 - 32
g = (g + dg) % 256
r = (r + dg + d2 // 16 - 8) % 256
bl = (bl + dg + d2 % 16 - 8) % 256
end
writeu32(out, o, r + g * 256 + bl * 65536 + a * 16777216)
o += 4
local h = ((r * 3 + g * 5 + bl * 7 + a * 11) % 64) * 4
writeu8(index, h, r)
writeu8(index, h + 1, g)
writeu8(index, h + 2, bl)
writeu8(index, h + 3, a)
local h = (r * 3 + g * 5 + bl * 7 + a * 11) % 64 + 1
ir[h], ig[h], ib[h], ia[h] = r, g, bl, a
end
return true, nil
end
+72 -24
View File
@@ -33,22 +33,44 @@ Codec.pixel = pixel
function Codec.decode(b: buffer, p: number, limit: number, w: number, h: number, bpp: number, kind: number, alpha1: boolean, rle: boolean, bottomUp: boolean, rtl: boolean, pal: buffer, palN: number, out: buffer): (number, boolean)
local n = w * h
local i = 0
local function put(v: number)
local row, col = i // w, i % w
if bottomUp then
row = h - 1 - row
end
if rtl then
col = w - 1 - col
end
writeu32(out, (row * w + col) * 4, v)
i += 1
-- the output position of pixel i: row by row (bottom-up flips rows), each row left to right or right to left
local step = if rtl then -4 else 4
local row, col = 0, 0
local function rowStart(r: number): number
return ((if bottomUp then h - 1 - r else r) * w + (if rtl then w - 1 else 0)) * 4
end
local o = rowStart(0)
if not rle then
local avail = math.min(n, (limit - p) // bpp)
for _ = 1, avail do
put(pixel(b, p, kind, alpha1, pal, palN))
p += bpp
while i < avail do
local e = math.min(avail - i, w - col)
if kind == 3 then
for _ = 1, e do
writeu32(out, o, readu8(b, p + 2) + readu8(b, p + 1) * 256 + readu8(b, p) * 65536 + 0xFF000000)
p += 3
o += step
end
elseif kind == 4 then
for _ = 1, e do
local v = readu32(b, p)
writeu32(out, o, v - v % 16777216 + (v // 65536) % 256 + v % 65536 - v % 256 + (v % 256) * 65536)
p += 4
o += step
end
else
for _ = 1, e do
writeu32(out, o, pixel(b, p, kind, alpha1, pal, palN))
p += bpp
o += step
end
end
i += e
col += e
if col == w then
col = 0
row += 1
o = rowStart(row)
end
end
return i, avail < n
end
@@ -58,23 +80,49 @@ function Codec.decode(b: buffer, p: number, limit: number, w: number, h: number,
end
local hd = readu8(b, p)
p += 1
local count = hd % 128 + 1
if hd >= 128 then
local count = math.min(hd % 128 + 1, n - i)
local packed = hd >= 128
local v = 0
if packed then
if p + bpp > limit then
return i, true
end
local v = pixel(b, p, kind, alpha1, pal, palN)
v = pixel(b, p, kind, alpha1, pal, palN)
p += bpp
for _ = 1, math.min(count, n - i) do
put(v)
end
while count > 0 do
local e = math.min(count, w - col)
if packed then
for _ = 1, e do
writeu32(out, o, v)
o += step
end
else
if p + e * bpp > limit then
-- the pixels that are there, then truncated
local k = (limit - p) // bpp
for _ = 1, k do
writeu32(out, o, pixel(b, p, kind, alpha1, pal, palN))
p += bpp
o += step
end
return i + k, true
end
for _ = 1, e do
writeu32(out, o, pixel(b, p, kind, alpha1, pal, palN))
p += bpp
o += step
end
end
else
for _ = 1, math.min(count, n - i) do
if p + bpp > limit then
return i, true
i += e
col += e
count -= e
if col == w then
col = 0
row += 1
if row < h then
o = rowStart(row)
end
put(pixel(b, p, kind, alpha1, pal, palN))
p += bpp
end
end
end