--!strict --!native --!optimize 2 -- codecs/image/Dct: the 8x8 DCTs JPEG, Motion-JPEG and MPEG-1 share (native; never throws). -- libjpeg's islow (jidctint.c / jfdctint.c), the reduced IDCTs (jidctred.c) and jcdctmgr.c's rounding quantiser, bit-exact -- with libjpeg-turbo. From jpeg-luau 1.0 (same author); IJG code: licenses/libjpeg-turbo-LICENSE.md. local readi16, readu16, writeu16, writeu32, readu8, writei16 = buffer.readi16, buffer.readu16, buffer.writeu16, buffer.writeu32, buffer.readu8, buffer.writei16 local clamp, abs, sign = math.clamp, math.abs, math.sign local Dct = {} local ws = table.create(64, 0) local function dequant(q: buffer): { number } local t = table.create(64, 0) for i = 0, 63 do t[i + 1] = readu16(q, i * 2) end return t end local function idct8(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, 7 do local o = off + c * 2 local c1, c2, c3 = readi16(coef, o + 16), readi16(coef, o + 32), readi16(coef, o + 48) local c4, c5, c6, c7 = readi16(coef, o + 64), readi16(coef, o + 80), readi16(coef, o + 96), readi16(coef, o + 112) if c1 == 0 and c2 == 0 and c3 == 0 and c4 == 0 and c5 == 0 and c6 == 0 and c7 == 0 then local dc = readi16(coef, o) * qt[c + 1] * dcm ws[c + 1] = dc ws[c + 9] = dc ws[c + 17] = dc ws[c + 25] = dc ws[c + 33] = dc ws[c + 41] = dc ws[c + 49] = dc ws[c + 57] = dc else -- even part (the pass-1 rounding folded into tmp0 / tmp1) local z2 = c2 * qt[c + 17] local z3 = c6 * qt[c + 49] local z1 = (z2 + z3) * 4433 local tmp2 = z1 - z3 * 15137 local tmp3 = z1 + z2 * 6270 z2 = readi16(coef, o) * qt[c + 1] 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 = tmp1 + tmp2, tmp1 - tmp2 -- odd part local t0 = c7 * qt[c + 57] local t1 = c5 * qt[c + 41] local t2 = c3 * qt[c + 25] local t3 = c1 * qt[c + 9] z1 = t0 + t3 z2 = t1 + t2 z3 = t0 + t2 local z4 = t1 + t3 local z5 = (z3 + z4) * 9633 z1 *= -7373 z2 *= -20995 z3 = z3 * -16069 + z5 z4 = z4 * -3196 + z5 t0 = t0 * 2446 + z1 + z3 t1 = t1 * 16819 + z2 + z4 t2 = t2 * 25172 + z2 + z3 t3 = t3 * 12299 + z1 + z4 ws[c + 1] = (tmp10 + t3) // s1 ws[c + 57] = (tmp10 - t3) // s1 ws[c + 9] = (tmp11 + t2) // s1 ws[c + 49] = (tmp11 - t2) // s1 ws[c + 17] = (tmp12 + t1) // s1 ws[c + 41] = (tmp12 - t1) // s1 ws[c + 25] = (tmp13 + t0) // s1 ws[c + 33] = (tmp13 - t0) // s1 end end local sd = 2 ^ (p1 + 3) local hd = sd / 2 + center * sd -- u16 pairs written as one u32 (little endian: the left sample in the low half) 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 w4, w5, w6, w7 = ws[b + 5], ws[b + 6], ws[b + 7], ws[b + 8] local o = (out + r * stride) * 2 if w1 == 0 and w2 == 0 and w3 == 0 and w4 == 0 and w5 == 0 and w6 == 0 and w7 == 0 then local v = clamp((w0 + hd) // sd, 0, maxv) * 65537 writeu32(plane, o, v) writeu32(plane, o + 4, v) writeu32(plane, o + 8, v) writeu32(plane, o + 12, v) else local z1 = (w2 + w6) * 4433 local tmp2 = z1 - w6 * 15137 local tmp3 = z1 + w2 * 6270 local tmp0 = (w0 + w4) * 8192 + h2 local tmp1 = (w0 - w4) * 8192 + h2 local tmp10, tmp13 = tmp0 + tmp3, tmp0 - tmp3 local tmp11, tmp12 = tmp1 + tmp2, tmp1 - tmp2 z1 = w7 + w1 local z2 = w5 + w3 local z3 = w7 + w3 local z4 = w5 + w1 local z5 = (z3 + z4) * 9633 z1 *= -7373 z2 *= -20995 z3 = z3 * -16069 + z5 z4 = z4 * -3196 + z5 local t0 = w7 * 2446 + z1 + z3 local t1 = w5 * 16819 + z2 + z4 local t2 = w3 * 25172 + z2 + z3 local t3 = w1 * 12299 + z1 + z4 writeu32(plane, o, clamp((tmp10 + t3) // s2, 0, maxv) + clamp((tmp11 + t2) // s2, 0, maxv) * 65536) writeu32(plane, o + 4, clamp((tmp12 + t1) // s2, 0, maxv) + clamp((tmp13 + t0) // s2, 0, maxv) * 65536) writeu32(plane, o + 8, clamp((tmp13 - t0) // s2, 0, maxv) + clamp((tmp12 - t1) // s2, 0, maxv) * 65536) writeu32(plane, o + 12, clamp((tmp11 - t2) // s2, 0, maxv) + clamp((tmp10 - t3) // s2, 0, maxv) * 65536) end end end -- 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, 4 do local o = off + c * 2 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 ws[c + 17] = dc ws[c + 25] = dc ws[c + 33] = dc ws[c + 41] = dc ws[c + 49] = dc ws[c + 57] = dc else local z2 = c2 * qt[c + 17] local tmp2 = z2 * 4433 local tmp3 = tmp2 + z2 * 6270 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 = 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 z3 = t2 * -16069 + z5 local z4 = t3 * -3196 + z5 local t0 = z1 + z3 local t1 = z2 + z4 t2 = t2 * 25172 + z2 + z3 t3 = t3 * 12299 + z1 + z4 ws[c + 1] = (tmp10 + t3) // s1 ws[c + 57] = (tmp10 - t3) // s1 ws[c + 9] = (tmp11 + t2) // s1 ws[c + 49] = (tmp11 - t2) // s1 ws[c + 17] = (tmp12 + t1) // s1 ws[c + 41] = (tmp12 - t1) // s1 ws[c + 25] = (tmp13 + t0) // s1 ws[c + 33] = (tmp13 - t0) // s1 end end local sd = 2 ^ (p1 + 3) local hd = sd / 2 + center * sd for r = 0, 7 do local b = r * 8 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 and w4 == 0 then local v = clamp((w0 + hd) // sd, 0, maxv) * 65537 writeu32(plane, o, v) writeu32(plane, o + 4, v) writeu32(plane, o + 8, v) writeu32(plane, o + 12, v) else local tmp2 = w2 * 4433 local tmp3 = tmp2 + w2 * 6270 local tmp0 = (w0 + w4) * 8192 + h2 local tmp1 = (w0 - w4) * 8192 + h2 local tmp10, tmp13 = tmp0 + tmp3, tmp0 - tmp3 local tmp11, tmp12 = tmp1 + tmp2, tmp1 - tmp2 local z5 = (w3 + w1) * 9633 local z1 = w1 * -7373 local z2 = w3 * -20995 local z3 = w3 * -16069 + z5 local z4 = w1 * -3196 + z5 local t0 = z1 + z3 local t1 = z2 + z4 local t2 = w3 * 25172 + z2 + z3 local t3 = w1 * 12299 + z1 + z4 writeu32(plane, o, clamp((tmp10 + t3) // s2, 0, maxv) + clamp((tmp11 + t2) // s2, 0, maxv) * 65536) writeu32(plane, o + 4, clamp((tmp12 + t1) // s2, 0, maxv) + clamp((tmp13 + t0) // s2, 0, maxv) * 65536) writeu32(plane, o + 8, clamp((tmp13 - t0) // s2, 0, maxv) + clamp((tmp12 - t1) // s2, 0, maxv) * 65536) writeu32(plane, o + 12, clamp((tmp11 - t2) // s2, 0, maxv) + clamp((tmp10 - t3) // s2, 0, maxv) * 65536) end end end -- a block with only its DC coefficient: one value (the column and row passes' DC shortcuts) local function idct8dc(coef: buffer, off: number, qt: { number }, plane: buffer, out: number, stride: number, p1: number, center: number, maxv: number) local sd = 2 ^ (p1 + 3) local v = clamp((readi16(coef, off) * qt[1] * 2 ^ p1 + sd / 2 + center * sd) // sd, 0, maxv) * 65537 local o = out * 2 local rs = stride * 2 for _ = 0, 7 do writeu32(plane, o, v) writeu32(plane, o + 4, v) writeu32(plane, o + 8, v) writeu32(plane, o + 12, v) o += rs end end local function idct4(coef: buffer, off: number, q: buffer, plane: buffer, out: number, stride: number, p1: number, center: number, maxv: number) local s1 = 2 ^ (14 - p1) local h1 = s1 / 2 local dcm = 2 ^ p1 for c = 0, 7 do if c ~= 4 then local o = off + c * 2 local qo = c * 2 local c1, c2, c3 = readi16(coef, o + 16), readi16(coef, o + 32), readi16(coef, o + 48) local c5, c6, c7 = readi16(coef, o + 80), readi16(coef, o + 96), readi16(coef, o + 112) if c1 == 0 and c2 == 0 and c3 == 0 and c5 == 0 and c6 == 0 and c7 == 0 then local dc = readi16(coef, o) * readu16(q, qo) * dcm ws[c + 1] = dc ws[c + 9] = dc ws[c + 17] = dc ws[c + 25] = dc else local tmp0 = readi16(coef, o) * readu16(q, qo) * 16384 local tmp2 = c2 * readu16(q, qo + 32) * 15137 - c6 * readu16(q, qo + 96) * 6270 local tmp10, tmp12 = tmp0 + tmp2, tmp0 - tmp2 local z1 = c7 * readu16(q, qo + 112) local z2 = c5 * readu16(q, qo + 80) local z3 = c3 * readu16(q, qo + 48) local z4 = c1 * readu16(q, qo + 16) tmp0 = z1 * -1730 + z2 * 11893 + z3 * -17799 + z4 * 8697 tmp2 = z1 * -4176 + z2 * -4926 + z3 * 7373 + z4 * 20995 ws[c + 1] = (tmp10 + tmp2 + h1) // s1 ws[c + 25] = (tmp10 - tmp2 + h1) // s1 ws[c + 9] = (tmp12 + tmp0 + h1) // s1 ws[c + 17] = (tmp12 - tmp0 + h1) // s1 end end end local s2 = 2 ^ (17 + p1) local h2 = s2 / 2 + center * s2 local sd = 2 ^ (p1 + 3) local hd = sd / 2 + center * sd for r = 0, 3 do local b = r * 8 local w0, w1, w2, w3 = ws[b + 1], ws[b + 2], ws[b + 3], ws[b + 4] local w5, w6, w7 = ws[b + 6], ws[b + 7], ws[b + 8] local o = (out + r * stride) * 2 if w1 == 0 and w2 == 0 and w3 == 0 and w5 == 0 and w6 == 0 and w7 == 0 then local v = clamp((w0 + hd) // sd, 0, maxv) writeu16(plane, o, v) writeu16(plane, o + 2, v) writeu16(plane, o + 4, v) writeu16(plane, o + 6, v) else local tmp0 = w0 * 16384 local tmp2 = w2 * 15137 - w6 * 6270 local tmp10, tmp12 = tmp0 + tmp2, tmp0 - tmp2 tmp0 = w7 * -1730 + w5 * 11893 + w3 * -17799 + w1 * 8697 tmp2 = w7 * -4176 + w5 * -4926 + w3 * 7373 + w1 * 20995 writeu16(plane, o, clamp((tmp10 + tmp2 + h2) // s2, 0, maxv)) writeu16(plane, o + 6, clamp((tmp10 - tmp2 + h2) // s2, 0, maxv)) writeu16(plane, o + 2, clamp((tmp12 + tmp0 + h2) // s2, 0, maxv)) writeu16(plane, o + 4, clamp((tmp12 - tmp0 + h2) // s2, 0, maxv)) end end end local function idct2(coef: buffer, off: number, q: buffer, plane: buffer, out: number, stride: number, p1: number, center: number, maxv: number) local s1 = 2 ^ (15 - p1) local h1 = s1 / 2 local dcm = 2 ^ p1 for c = 0, 7 do if c ~= 2 and c ~= 4 and c ~= 6 then local o = off + c * 2 local qo = c * 2 local c1, c3 = readi16(coef, o + 16), readi16(coef, o + 48) local c5, c7 = readi16(coef, o + 80), readi16(coef, o + 112) if c1 == 0 and c3 == 0 and c5 == 0 and c7 == 0 then local dc = readi16(coef, o) * readu16(q, qo) * dcm ws[c + 1] = dc ws[c + 9] = dc else local tmp10 = readi16(coef, o) * readu16(q, qo) * 32768 local tmp0 = c7 * readu16(q, qo + 112) * -5906 + c5 * readu16(q, qo + 80) * 6967 + c3 * readu16(q, qo + 48) * -10426 + c1 * readu16(q, qo + 16) * 29692 ws[c + 1] = (tmp10 + tmp0 + h1) // s1 ws[c + 9] = (tmp10 - tmp0 + h1) // s1 end end end local s2 = 2 ^ (18 + p1) local h2 = s2 / 2 + center * s2 local sd = 2 ^ (p1 + 3) local hd = sd / 2 + center * sd for r = 0, 1 do local b = r * 8 local w0, w1, w3, w5, w7 = ws[b + 1], ws[b + 2], ws[b + 4], ws[b + 6], ws[b + 8] local o = (out + r * stride) * 2 if w1 == 0 and w3 == 0 and w5 == 0 and w7 == 0 then local v = clamp((w0 + hd) // sd, 0, maxv) writeu16(plane, o, v) writeu16(plane, o + 2, v) else local tmp10 = w0 * 32768 local tmp0 = w7 * -5906 + w5 * 6967 + w3 * -10426 + w1 * 29692 writeu16(plane, o, clamp((tmp10 + tmp0 + h2) // s2, 0, maxv)) writeu16(plane, o + 2, clamp((tmp10 - tmp0 + h2) // s2, 0, maxv)) end end end -- p1 = PASS1_BITS (2 for 8-bit, 1 for 12-bit) -- nz (optional): u8 per block, the highest zigzag index that may be nonzero function Dct.idctComponent(coef: buffer, q: buffer, bpl: number, bpc: number, size: number, plane: buffer, p1: number, center: number, maxv: number, hook: (() -> ())?, nz: buffer?) local stride = bpl * size local qt = dequant(q) for by = 0, bpc - 1 do if hook and by > 0 then pcall(hook) end if size == 8 and nz then local bi = by * bpl local out = by * 8 * stride for off = bi * 128, (bi + bpl - 1) * 128, 128 do local k = readu8(nz, bi) if k == 0 then idct8dc(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 bi += 1 out += 8 end continue end for bx = 0, bpl - 1 do local off = (by * bpl + bx) * 128 local out = by * size * stride + bx * size if size == 8 then idct8(coef, off, qt, plane, out, stride, p1, center, maxv) elseif size == 4 then idct4(coef, off, q, plane, out, stride, p1, center, maxv) elseif size == 2 then idct2(coef, off, q, plane, out, stride, p1, center, maxv) else local v = (readi16(coef, off) * readu16(q, 0) + 4) // 8 + center writeu16(plane, out * 2, clamp(v, 0, maxv)) end end end end function Dct.idctBlock(coef: buffer, off: number, q: buffer, plane: buffer, out: number, stride: number, p1: number, center: number, maxv: number) idct8(coef, off, dequant(q), plane, out, stride, p1, center, maxv) end local fw = table.create(64, 0) -- (|v| + q * 4) // (q * 8) with v's sign, branch-free (a sign branch here gave LuaJIT side traces it could not compile) local function quant(coef: buffer, off: number, qv: number, v: number, i: number) writei16(coef, off + i * 2, (abs(v) + qv / 2) // qv * sign(v)) end -- quantised as jcdctmgr.c: (|v| + q * 4) // (q * 8) with the sign restored; div[i + 1] = q * 8 for natural index i function Dct.fdctQuantize(src: buffer, stride: number, x: number, y: number, w: number, h: number, div: { number }, coef: buffer, off: number) local lastX, lastY = w - 1, h - 1 for r = 0, 7 do local row = math.min(y + r, lastY) * stride local d0: number, d1: number, d2: number, d3: number, d4: number, d5: number, d6: number, d7: number if x + 7 <= lastX then local p = row + x d0, d1, d2, d3 = readu8(src, p), readu8(src, p + 1), readu8(src, p + 2), readu8(src, p + 3) :: number d4, d5, d6, d7 = readu8(src, p + 4), readu8(src, p + 5), readu8(src, p + 6), readu8(src, p + 7) :: number else d0 = readu8(src, row + math.min(x, lastX)) d1 = readu8(src, row + math.min(x + 1, lastX)) d2 = readu8(src, row + math.min(x + 2, lastX)) d3 = readu8(src, row + math.min(x + 3, lastX)) d4 = readu8(src, row + math.min(x + 4, lastX)) d5 = readu8(src, row + math.min(x + 5, lastX)) d6 = readu8(src, row + math.min(x + 6, lastX)) d7 = readu8(src, row + math.min(x + 7, lastX)) end d0 -= 128 d1 -= 128 d2 -= 128 d3 -= 128 d4 -= 128 d5 -= 128 d6 -= 128 d7 -= 128 local tmp0, tmp7 = d0 + d7, d0 - d7 local tmp1, tmp6 = d1 + d6, d1 - d6 local tmp2, tmp5 = d2 + d5, d2 - d5 local tmp3, tmp4 = d3 + d4, d3 - d4 local tmp10, tmp13 = tmp0 + tmp3, tmp0 - tmp3 local tmp11, tmp12 = tmp1 + tmp2, tmp1 - tmp2 local b = r * 8 + 1 fw[b] = (tmp10 + tmp11) * 4 fw[b + 4] = (tmp10 - tmp11) * 4 local z1 = (tmp12 + tmp13) * 4433 fw[b + 2] = (z1 + tmp13 * 6270 + 1024) // 2048 fw[b + 6] = (z1 - tmp12 * 15137 + 1024) // 2048 z1 = tmp4 + tmp7 local z2 = tmp5 + tmp6 local z3 = tmp4 + tmp6 local z4 = tmp5 + tmp7 local z5 = (z3 + z4) * 9633 tmp4 *= 2446 tmp5 *= 16819 tmp6 *= 25172 tmp7 *= 12299 z1 *= -7373 z2 *= -20995 z3 = z3 * -16069 + z5 z4 = z4 * -3196 + z5 fw[b + 7] = (tmp4 + z1 + z3 + 1024) // 2048 fw[b + 5] = (tmp5 + z2 + z4 + 1024) // 2048 fw[b + 3] = (tmp6 + z2 + z3 + 1024) // 2048 fw[b + 1] = (tmp7 + z1 + z4 + 1024) // 2048 end for c = 1, 8 do local d0, d1, d2, d3 = fw[c], fw[c + 8], fw[c + 16], fw[c + 24] local d4, d5, d6, d7 = fw[c + 32], fw[c + 40], fw[c + 48], fw[c + 56] local tmp0, tmp7 = d0 + d7, d0 - d7 local tmp1, tmp6 = d1 + d6, d1 - d6 local tmp2, tmp5 = d2 + d5, d2 - d5 local tmp3, tmp4 = d3 + d4, d3 - d4 local tmp10, tmp13 = tmp0 + tmp3, tmp0 - tmp3 local tmp11, tmp12 = tmp1 + tmp2, tmp1 - tmp2 local o0 = (tmp10 + tmp11 + 2) // 4 local o4 = (tmp10 - tmp11 + 2) // 4 local z1 = (tmp12 + tmp13) * 4433 local o2 = (z1 + tmp13 * 6270 + 16384) // 32768 local o6 = (z1 - tmp12 * 15137 + 16384) // 32768 z1 = tmp4 + tmp7 local z2 = tmp5 + tmp6 local z3 = tmp4 + tmp6 local z4 = tmp5 + tmp7 local z5 = (z3 + z4) * 9633 tmp4 *= 2446 tmp5 *= 16819 tmp6 *= 25172 tmp7 *= 12299 z1 *= -7373 z2 *= -20995 z3 = z3 * -16069 + z5 z4 = z4 * -3196 + z5 local o7 = (tmp4 + z1 + z3 + 16384) // 32768 local o5 = (tmp5 + z2 + z4 + 16384) // 32768 local o3 = (tmp6 + z2 + z3 + 16384) // 32768 local o1 = (tmp7 + z1 + z4 + 16384) // 32768 local cc = c - 1 quant(coef, off, div[c], o0, cc) quant(coef, off, div[c + 8], o1, cc + 8) quant(coef, off, div[c + 16], o2, cc + 16) quant(coef, off, div[c + 24], o3, cc + 24) quant(coef, off, div[c + 32], o4, cc + 32) quant(coef, off, div[c + 40], o5, cc + 40) quant(coef, off, div[c + 48], o6, cc + 48) quant(coef, off, div[c + 56], o7, cc + 56) end end return Dct