--!strict --!native --!optimize 2 -- codecs/Jpeg/Arith: arithmetic-coded scans (ITU T.81 annex D / F.1.4, sequential and progressive) into quantised -- DCT coefficients (native; never throws). A Luau translation of libjpeg-turbo's jdarith.c (Guido Vollbeding's -- decoder, IJG licence: licenses/libjpeg-turbo-LICENSE.md) and the jaricom.c probability table. -- -- It reads the destuffed scan data that Scan.destuff produces (0xFF 0x00 already removed, RSTn positions recorded); -- past the end of a restart segment it reads zero bytes, the convention libjpeg follows when the decoder runs into a -- marker (libjpeg's encoder also drops trailing zero bytes, so reading past the end is normal and not a sign of -- truncation: the caller flags a scan that runs into the end of the file instead). Statistics bins live in one buffer: DC tables at [t * 64], AC tables at [256 + t * 256], the fixed 0.5 -- bin at [1280]. local readu8, writeu8, readi16, writei16 = buffer.readu8, buffer.writeu8, buffer.readi16, buffer.writei16 local band, bxor = bit32.band, bit32.bxor local Arith = {} -- jaricom.c: Qe << 16 | Next_Index_MPS << 8 | Switch_MPS << 7 | Next_Index_LPS local TAB = buffer.create(114 * 4) do local raw = { { 0x5a1d, 1, 1, 1 }, { 0x2586, 14, 2, 0 }, { 0x1114, 16, 3, 0 }, { 0x080b, 18, 4, 0 }, { 0x03d8, 20, 5, 0 }, { 0x01da, 23, 6, 0 }, { 0x00e5, 25, 7, 0 }, { 0x006f, 28, 8, 0 }, { 0x0036, 30, 9, 0 }, { 0x001a, 33, 10, 0 }, { 0x000d, 35, 11, 0 }, { 0x0006, 9, 12, 0 }, { 0x0003, 10, 13, 0 }, { 0x0001, 12, 13, 0 }, { 0x5a7f, 15, 15, 1 }, { 0x3f25, 36, 16, 0 }, { 0x2cf2, 38, 17, 0 }, { 0x207c, 39, 18, 0 }, { 0x17b9, 40, 19, 0 }, { 0x1182, 42, 20, 0 }, { 0x0cef, 43, 21, 0 }, { 0x09a1, 45, 22, 0 }, { 0x072f, 46, 23, 0 }, { 0x055c, 48, 24, 0 }, { 0x0406, 49, 25, 0 }, { 0x0303, 51, 26, 0 }, { 0x0240, 52, 27, 0 }, { 0x01b1, 54, 28, 0 }, { 0x0144, 56, 29, 0 }, { 0x00f5, 57, 30, 0 }, { 0x00b7, 59, 31, 0 }, { 0x008a, 60, 32, 0 }, { 0x0068, 62, 33, 0 }, { 0x004e, 63, 34, 0 }, { 0x003b, 32, 35, 0 }, { 0x002c, 33, 9, 0 }, { 0x5ae1, 37, 37, 1 }, { 0x484c, 64, 38, 0 }, { 0x3a0d, 65, 39, 0 }, { 0x2ef1, 67, 40, 0 }, { 0x261f, 68, 41, 0 }, { 0x1f33, 69, 42, 0 }, { 0x19a8, 70, 43, 0 }, { 0x1518, 72, 44, 0 }, { 0x1177, 73, 45, 0 }, { 0x0e74, 74, 46, 0 }, { 0x0bfb, 75, 47, 0 }, { 0x09f8, 77, 48, 0 }, { 0x0861, 78, 49, 0 }, { 0x0706, 79, 50, 0 }, { 0x05cd, 48, 51, 0 }, { 0x04de, 50, 52, 0 }, { 0x040f, 50, 53, 0 }, { 0x0363, 51, 54, 0 }, { 0x02d4, 52, 55, 0 }, { 0x025c, 53, 56, 0 }, { 0x01f8, 54, 57, 0 }, { 0x01a4, 55, 58, 0 }, { 0x0160, 56, 59, 0 }, { 0x0125, 57, 60, 0 }, { 0x00f6, 58, 61, 0 }, { 0x00cb, 59, 62, 0 }, { 0x00ab, 61, 63, 0 }, { 0x008f, 61, 32, 0 }, { 0x5b12, 65, 65, 1 }, { 0x4d04, 80, 66, 0 }, { 0x412c, 81, 67, 0 }, { 0x37d8, 82, 68, 0 }, { 0x2fe8, 83, 69, 0 }, { 0x293c, 84, 70, 0 }, { 0x2379, 86, 71, 0 }, { 0x1edf, 87, 72, 0 }, { 0x1aa9, 87, 73, 0 }, { 0x174e, 72, 74, 0 }, { 0x1424, 72, 75, 0 }, { 0x119c, 74, 76, 0 }, { 0x0f6b, 74, 77, 0 }, { 0x0d51, 75, 78, 0 }, { 0x0bb6, 77, 79, 0 }, { 0x0a40, 77, 48, 0 }, { 0x5832, 80, 81, 1 }, { 0x4d1c, 88, 82, 0 }, { 0x438e, 89, 83, 0 }, { 0x3bdd, 90, 84, 0 }, { 0x34ee, 91, 85, 0 }, { 0x2eae, 92, 86, 0 }, { 0x299a, 93, 87, 0 }, { 0x2516, 86, 71, 0 }, { 0x5570, 88, 89, 1 }, { 0x4ca9, 95, 90, 0 }, { 0x44d9, 96, 91, 0 }, { 0x3e22, 97, 92, 0 }, { 0x3824, 99, 93, 0 }, { 0x32b4, 99, 94, 0 }, { 0x2e17, 93, 86, 0 }, { 0x56a8, 95, 96, 1 }, { 0x4f46, 101, 97, 0 }, { 0x47e5, 102, 98, 0 }, { 0x41cf, 103, 99, 0 }, { 0x3c3d, 104, 100, 0 }, { 0x375e, 99, 93, 0 }, { 0x5231, 105, 102, 0 }, { 0x4c0f, 106, 103, 0 }, { 0x4639, 107, 104, 0 }, { 0x415e, 103, 99, 0 }, { 0x5627, 105, 106, 1 }, { 0x50e7, 108, 107, 0 }, { 0x4b85, 109, 103, 0 }, { 0x5597, 110, 109, 0 }, { 0x504f, 111, 107, 0 }, { 0x5a10, 110, 111, 1 }, { 0x5522, 112, 109, 0 }, { 0x59eb, 112, 111, 1 }, { 0x5a1d, 113, 113, 0 }, } for i, e in raw do buffer.writeu16(TAB, (i - 1) * 4, e[1]) -- Qe writeu8(TAB, (i - 1) * 4 + 2, e[4] * 128 + e[2]) -- Switch_MPS << 7 | Next_Index_LPS writeu8(TAB, (i - 1) * 4 + 3, e[3]) -- Next_Index_MPS end end local ZZ: buffer = buffer.create(80) -- set by Arith.init (Scan.ZIGZAG) local STATS = 1281 local POW2 = buffer.create(8) for i = 0, 7 do writeu8(POW2, i, 2 ^ i) end local FIXED = 1280 -- decoder state (one scan at a time; never yields) local D: buffer = buffer.create(0) local P = 0 -- next byte local SEGEND = 0 -- reads at or past this return 0 local OVER = 0 -- zero bytes supplied past the end local C, A, CT = 0, 0, -16 local st: buffer = buffer.create(STATS) local function decode(s: number): number while A < 0x8000 do CT -= 1 if CT < 0 then local data = 0 if P < SEGEND then data = readu8(D, P) P += 1 else OVER += 1 end C = C * 256 + data CT += 8 if CT < 0 then CT += 1 if CT == 0 then A = 0x8000 end end end A *= 2 end local sv = readu8(st, s) local t = band(sv, 0x7F) * 4 local qe = buffer.readu16(TAB, t) local nl = readu8(TAB, t + 2) local nm = readu8(TAB, t + 3) local temp = A - qe A = temp temp *= readu8(POW2, CT) if C >= temp then C -= temp if A < qe then A = qe writeu8(st, s, bxor(band(sv, 0x80), nm)) else A = qe writeu8(st, s, bxor(band(sv, 0x80), nl)) sv = bxor(sv, 0x80) end elseif A < 0x8000 then if A < qe then writeu8(st, s, bxor(band(sv, 0x80), nl)) sv = bxor(sv, 0x80) else writeu8(st, s, bxor(band(sv, 0x80), nm)) end end return if sv >= 0x80 then 1 else 0 end local function i16(v: number): number v = band(v, 0xFFFF) return if v >= 0x8000 then v - 0x10000 else v end export type ArithComp = { coef: buffer, bpl: number, h: number, v: number, bw: number, bh: number, dcTbl: number, -- 0..3 acTbl: number, pred: number, ctx: number, } export type ArithParams = { comps: { ArithComp }, mcusX: number, mcusY: number, ri: number, mode: number, -- 0 sequential, 1 DC first, 2 DC refine, 3 AC first, 4 AC refine ss: number, se: number, al: number, dcL: { number }, -- conditioning per table (DAC), 1-based by table + 1 dcU: { number }, acK: { number }, } -- DC difference (F.1.4.4.1 / figures F.19, F.21 - F.24); returns the new predictor or nil on a bad code local function dcDiff(c: ArithComp, L: number, U: number): number? local base = c.dcTbl * 64 local s = base + c.ctx if decode(s) == 0 then c.ctx = 0 return c.pred end local sign = decode(s + 1) s = s + 2 + sign local m = decode(s) if m ~= 0 then s = base + 20 while decode(s) ~= 0 do m *= 2 if m == 0x8000 then return nil end s += 1 end end if m < 2 ^ L // 2 then c.ctx = 0 elseif m > 2 ^ U // 2 then c.ctx = 12 + sign * 4 else c.ctx = 4 + sign * 4 end local v = m s += 14 m = m // 2 while m > 0 do if decode(s) ~= 0 then v += m end m = m // 2 end v += 1 if sign ~= 0 then v = -v end return band(c.pred + v, 0xFFFF) end -- AC coefficients k = ss..se of one block (figure F.20); false on a bad code local function acFirst(coef: buffer, off: number, tbl: number, ss: number, se: number, K: number, mul: number): boolean local base = 256 + tbl * 256 local k = ss while k <= se do local s = base + 3 * (k - 1) if decode(s) ~= 0 then break -- EOB end while decode(s + 1) == 0 do s += 3 k += 1 if k > se then return false end end local sign = decode(FIXED) s += 2 local m = decode(s) if m ~= 0 then if decode(s) ~= 0 then m *= 2 s = base + (if k <= K then 189 else 217) while decode(s) ~= 0 do m *= 2 if m == 0x8000 then return false end s += 1 end end end local v = m s += 14 m = m // 2 while m > 0 do if decode(s) ~= 0 then v += m end m = m // 2 end v += 1 if sign ~= 0 then v = -v end writei16(coef, off + readu8(ZZ, k) * 2, i16(v * mul)) k += 1 end return true end local function acRefine(coef: buffer, off: number, tbl: number, ss: number, se: number, al: number): boolean local base = 256 + tbl * 256 local p1 = 2 ^ al local m1 = -p1 local kex = se while kex > 0 do if readi16(coef, off + readu8(ZZ, kex) * 2) ~= 0 then break end kex -= 1 end local k = ss while k <= se do local s = base + 3 * (k - 1) if k > kex then if decode(s) ~= 0 then break end end while true do local o = off + readu8(ZZ, k) * 2 local cv = readi16(coef, o) if cv ~= 0 then if decode(s + 2) ~= 0 then writei16(coef, o, i16(if cv < 0 then cv + m1 else cv + p1)) end break end if decode(s + 1) ~= 0 then writei16(coef, o, i16(if decode(FIXED) ~= 0 then m1 else p1)) break end s += 3 k += 1 if k > se then return false end end k += 1 end return true end function Arith.init(zigzag: buffer) ZZ = zigzag end -- run(d, cleanLen, rst, nrst, P) -> truncated: decodes one scan (see Scan.run for the data layout) function Arith.run(d: buffer, cleanLen: number, rst: { number }, nrst: number, Pm: ArithParams): boolean local comps = Pm.comps local nc = #comps local mode, ss, se, al, ri = Pm.mode, Pm.ss, Pm.se, Pm.al, Pm.ri local units, ux if nc == 1 then ux = comps[1].bw units = ux * comps[1].bh else ux = Pm.mcusX units = ux * Pm.mcusY end local resetDc = mode == 0 or mode == 1 local resetAc = mode == 0 or mode == 3 or mode == 4 local function reset() for i = 1, nc do local c = comps[i] if resetDc then buffer.fill(st, c.dcTbl * 64, 0, 64) c.pred = 0 c.ctx = 0 end if resetAc then buffer.fill(st, 256 + c.acTbl * 256, 0, 256) end end C, A, CT = 0, 0, -16 end D = d P = 0 SEGEND = if nrst >= 1 then math.min(rst[1], cleanLen) else cleanLen OVER = 0 writeu8(st, FIXED, 113) reset() local rstIdx = 0 local bad = false local truncated = false local mul = 2 ^ al for u = 0, units - 1 do if ri > 0 and u > 0 and u % ri == 0 then rstIdx += 1 if rstIdx <= nrst then P = math.min(rst[rstIdx], cleanLen) SEGEND = if rstIdx + 1 <= nrst then math.min(rst[rstIdx + 1], cleanLen) else cleanLen bad = false else truncated = true break end reset() end if not bad then local my, mx = u // ux, u % ux for ci = 1, nc do local c = comps[ci] local bh, bw, by0, bx0 if nc == 1 then bh, bw, by0, bx0 = 1, 1, my, mx else bh, bw, by0, bx0 = c.v, c.h, my * c.v, mx * c.h end local coef, bpl = c.coef, c.bpl for by = by0, by0 + bh - 1 do for bx = bx0, bx0 + bw - 1 do local off = (by * bpl + bx) * 128 if mode == 0 then local pred = dcDiff(c, Pm.dcL[c.dcTbl + 1], Pm.dcU[c.dcTbl + 1]) if not pred then bad = true else c.pred = pred writei16(coef, off, i16(pred)) if not acFirst(coef, off, c.acTbl, 1, 63, Pm.acK[c.acTbl + 1], 1) then bad = true end end elseif mode == 1 then local pred = dcDiff(c, Pm.dcL[c.dcTbl + 1], Pm.dcU[c.dcTbl + 1]) if not pred then bad = true else c.pred = pred writei16(coef, off, i16(pred * mul)) end elseif mode == 2 then if decode(FIXED) ~= 0 then local cv = readi16(coef, off) if band(cv, mul) == 0 then writei16(coef, off, i16(cv + mul)) end end elseif mode == 3 then if not acFirst(coef, off, c.acTbl, ss, se, Pm.acK[c.acTbl + 1], mul) then bad = true end else if not acRefine(coef, off, c.acTbl, ss, se, al) then bad = true end end if bad then break end end if bad then break end end if bad then break end end end end D = buffer.create(0) return truncated end return Arith