--!strict --!native --!optimize 2 -- codecs/Dct: the 8x8 DCTs JPEG, Motion-JPEG and MPEG-1 share (native; never throws). -- Inverse: libjpeg's islow (jidctint.c: the Loeffler-Ligtenberg-Moschytz algorithm, 13-bit constants, two passes with -- PASS1_BITS of scaling between them), plus the reduced 4x4 / 2x2 / 1x1 IDCTs for scaled decodes (jidctred.c); the -- results match libjpeg-turbo's djpeg bit for bit. Absorbed from jpeg-luau 1.0 src/Jpeg/Idct.luau (same author). -- Forward: libjpeg's islow (jfdctint.c) with jcdctmgr.c's rounding quantiser, so an encoder built on it produces -- the same coefficients as cjpeg. Both are Luau translations of IJG code (licenses/libjpeg-turbo-LICENSE.md). -- Values are exact integers in doubles: no overflow even on corrupt coefficients; outputs are clamped to 0..maxv. -- -- Dct.idctComponent(coef, q, bpl, bpc, size, plane, p1, center, maxv) -- every block of a component -- Dct.idctBlock(coef, off, q, plane, out, stride, p1, center, maxv) -- one 8x8 block (u16 samples) -- Dct.fdctQuantize(src, stride, x, y, w, h, div, coef, off) -- one block from u8 samples local readi16, readu16, writeu16, readu8, writei16 = buffer.readi16, buffer.readu16, buffer.writeu16, buffer.readu8, buffer.writei16 local clamp = math.clamp local Dct = {} local ws = table.create(64, 0) local function idct8(coef: buffer, off: number, q: buffer, plane: buffer, out: number, stride: number, p1: number, center: number, maxv: number) 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 qo = 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) * readu16(q, qo) * 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 local z2 = c2 * readu16(q, qo + 32) local z3 = c6 * readu16(q, qo + 96) local z1 = (z2 + z3) * 4433 local tmp2 = z1 - z3 * 15137 local tmp3 = z1 + z2 * 6270 z2 = readi16(coef, o) * readu16(q, qo) z3 = c4 * readu16(q, qo + 64) local tmp0 = (z2 + z3) * 8192 local tmp1 = (z2 - z3) * 8192 local tmp10, tmp13 = tmp0 + tmp3, tmp0 - tmp3 local tmp11, tmp12 = tmp1 + tmp2, tmp1 - tmp2 -- odd part local t0 = c7 * readu16(q, qo + 112) local t1 = c5 * readu16(q, qo + 80) local t2 = c3 * readu16(q, qo + 48) local t3 = c1 * readu16(q, qo + 16) z1 = t0 + t3 z2 = t1 + t2 z3 = t0 + t2 local z4 = t1 + t3 local z5 = (z3 + z4) * 9633 t0 *= 2446 t1 *= 16819 t2 *= 25172 t3 *= 12299 z1 *= -7373 z2 *= -20995 z3 = z3 * -16069 + z5 z4 = z4 * -3196 + z5 t0 += z1 + z3 t1 += z2 + z4 t2 += z2 + z3 t3 += z1 + z4 ws[c + 1] = (tmp10 + t3 + h1) // s1 ws[c + 57] = (tmp10 - t3 + h1) // s1 ws[c + 9] = (tmp11 + t2 + h1) // s1 ws[c + 49] = (tmp11 - t2 + h1) // s1 ws[c + 17] = (tmp12 + t1 + h1) // s1 ws[c + 41] = (tmp12 - t1 + h1) // s1 ws[c + 25] = (tmp13 + t0 + h1) // s1 ws[c + 33] = (tmp13 - t0 + h1) // 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 = 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) for i = 0, 14, 2 do writeu16(plane, o + i, v) end else local z1 = (w2 + w6) * 4433 local tmp2 = z1 - w6 * 15137 local tmp3 = z1 + w2 * 6270 local tmp0 = (w0 + w4) * 8192 local tmp1 = (w0 - w4) * 8192 local tmp10, tmp13 = tmp0 + tmp3, tmp0 - tmp3 local tmp11, tmp12 = tmp1 + tmp2, tmp1 - tmp2 local t0, t1, t2, t3 = w7, w5, w3, w1 z1 = t0 + t3 local z2 = t1 + t2 local z3 = t0 + t2 local z4 = t1 + t3 local z5 = (z3 + z4) * 9633 t0 *= 2446 t1 *= 16819 t2 *= 25172 t3 *= 12299 z1 *= -7373 z2 *= -20995 z3 = z3 * -16069 + z5 z4 = z4 * -3196 + z5 t0 += z1 + z3 t1 += z2 + z4 t2 += z2 + z3 t3 += z1 + z4 writeu16(plane, o, clamp((tmp10 + t3 + h2) // s2, 0, maxv)) writeu16(plane, o + 14, clamp((tmp10 - t3 + h2) // s2, 0, maxv)) writeu16(plane, o + 2, clamp((tmp11 + t2 + h2) // s2, 0, maxv)) writeu16(plane, o + 12, clamp((tmp11 - t2 + h2) // s2, 0, maxv)) writeu16(plane, o + 4, clamp((tmp12 + t1 + h2) // s2, 0, maxv)) writeu16(plane, o + 10, clamp((tmp12 - t1 + h2) // s2, 0, maxv)) writeu16(plane, o + 6, clamp((tmp13 + t0 + h2) // s2, 0, maxv)) writeu16(plane, o + 8, clamp((tmp13 - t0 + h2) // s2, 0, maxv)) end 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 -- Inverse-transforms every block of a component: coef (i16, 64 per block, natural order) on a bpl x bpc block -- grid, quantisation table q (64 u16, natural order), into plane (u16 samples, (bpl * size) per row) at size x -- size samples per block. p1 is PASS1_BITS (2 for 8-bit, 1 for 12-bit), center the level shift, maxv the top. function Dct.idctComponent(coef: buffer, q: buffer, bpl: number, bpc: number, size: number, plane: buffer, p1: number, center: number, maxv: number) local stride = bpl * size for by = 0, bpc - 1 do 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, q, 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 -- idctBlock(coef, off, q, plane, out, stride, p1, center, maxv): one 8x8 block, coef (i16, natural order) at byte -- offset off, dequantised by q (64 u16), into the u16 plane at sample index out with stride samples per row function Dct.idctBlock(coef: buffer, off: number, q: buffer, plane: buffer, out: number, stride: number, p1: number, center: number, maxv: number) idct8(coef, off, q, plane, out, stride, p1, center, maxv) end -- ------------------------------------------------------------------------------------------------ forward local fw = table.create(64, 0) local function quant(coef: buffer, off: number, div: buffer, v: number, i: number) local qv = readu16(div, i * 2) * 8 local h = qv // 2 if v < 0 then writei16(coef, off + i * 2, -((h - v) // qv)) else writei16(coef, off + i * 2, (v + h) // qv) end end -- fdctQuantize(src, stride, x, y, w, h, div, coef, off): the 8x8 block of u8 samples at (x, y) in src (stride bytes -- per row; samples past w - 1 / h - 1 repeat the edge), level-shifted by 128, forward DCT (jfdctint.c islow), then -- quantised as jcdctmgr.c: (|v| + q * 4) // (q * 8) with the sign restored, q = div (64 u16, natural order). Writes -- 64 i16 at coef[off] (natural order). The caller keeps x < w, y < h and the src range valid. function Dct.fdctQuantize(src: buffer, stride: number, x: number, y: number, w: number, h: number, div: buffer, 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) d4, d5, d6, d7 = readu8(src, p + 4), readu8(src, p + 5), readu8(src, p + 6), readu8(src, p + 7) 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, o0, cc) quant(coef, off, div, o1, cc + 8) quant(coef, off, div, o2, cc + 16) quant(coef, off, div, o3, cc + 24) quant(coef, off, div, o4, cc + 32) quant(coef, off, div, o5, cc + 40) quant(coef, off, div, o6, cc + 48) quant(coef, off, div, o7, cc + 56) end end return Dct