rename: src/Formats -> src/Rosetta, @formats -> @rosetta requires, build/luajit/rosetta + rosetta._compat, build/roblox/Rosetta, ROSETTA_* env/globals, writer vendor strings
This commit is contained in:
1 parent
2956576d7f
commit
22ddcd78fa
941 files changed
+3364
-3364
No files matched your search
@@ -0,0 +1,461 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- codecs/image/Jpeg/Scan: Huffman-coded scan decoding into quantised DCT coefficients (native; never throws). Absorbed from
|
||||
-- jpeg-luau 1.0 src/Jpeg/Scan.luau (same author); unchanged but for this header.
|
||||
--
|
||||
-- `destuff` copies one scan's entropy-coded bytes into a clean buffer: it drops the stuffed 0x00 after each 0xFF,
|
||||
-- records where each RSTn marker fell, and stops at any other marker. The clean buffer is followed by at least
|
||||
-- PAD zero bytes, so the bit reader can read 32 bits at any position up to PAD bytes past the end without a
|
||||
-- bounds check. `run` checks the position once per MCU, and no MCU (at most 10 blocks, enforced by the parser)
|
||||
-- can consume PAD bytes. So every buffer index stays in range, even on garbage data.
|
||||
--
|
||||
-- The bit reader keeps no state but a bit position: `peek` reads the big-endian 32-bit word that holds it.
|
||||
-- Huffman tables (built by `buildTable`) are buffers:
|
||||
-- [0, 1024) fast lookup by the next 9 bits: u16 (length << 8) | symbol, 0 = longer code
|
||||
-- [1024, 1096) maxcode[l], l = 0..17: u32, one past the last code of length l, left-aligned to 16 bits
|
||||
-- [1096, 1168) delta[l]: i32, symbol index = code + delta[l]
|
||||
-- [1168, 1424) symbols
|
||||
-- [1424, 1426) symbol count
|
||||
-- [1428, 3476) fast AC lookup by the next 9 bits: i32 value * 256 + run * 16 + total length, when the code
|
||||
-- and its extra bits fit in 9 bits (0 otherwise)
|
||||
local readu8, readu16, readu32, readi32, readi16 = buffer.readu8, buffer.readu16, buffer.readu32, buffer.readi32, buffer.readi16
|
||||
local writeu8, writeu16, writeu32, writei32, writei16 = buffer.writeu8, buffer.writeu16, buffer.writeu32, buffer.writei32, buffer.writei16
|
||||
local band, bor, lshift, rshift, byteswap = bit32.band, bit32.bor, bit32.lshift, bit32.rshift, bit32.byteswap
|
||||
|
||||
local Scan = {}
|
||||
|
||||
Scan.PAD = 4096
|
||||
Scan.TABLE_SIZE = 3476
|
||||
|
||||
local T_MAX = 1024
|
||||
local T_DELTA = 1096
|
||||
local T_VAL = 1168
|
||||
local T_COUNT = 1424
|
||||
local T_FASTAC = 1428
|
||||
|
||||
-- zigzag index -> natural (row-major) index, padded with 63s so a run past the end stays in range
|
||||
local ZZ = buffer.create(80)
|
||||
do
|
||||
local i = 0
|
||||
for s = 0, 14 do
|
||||
local lo, hi = math.max(0, s - 7), math.min(s, 7)
|
||||
if s % 2 == 0 then
|
||||
for r = hi, lo, -1 do
|
||||
writeu8(ZZ, i, r * 8 + (s - r))
|
||||
i += 1
|
||||
end
|
||||
else
|
||||
for r = lo, hi do
|
||||
writeu8(ZZ, i, r * 8 + (s - r))
|
||||
i += 1
|
||||
end
|
||||
end
|
||||
end
|
||||
for j = 64, 79 do
|
||||
writeu8(ZZ, j, 63)
|
||||
end
|
||||
end
|
||||
Scan.ZIGZAG = ZZ
|
||||
|
||||
-- Builds a Huffman table from the 16 code-length counts and the symbols (Luau arrays, 1-based). An
|
||||
-- over-subscribed table keeps the codes that fit; the rest decode as invalid.
|
||||
function Scan.buildTable(counts: { number }, symbols: { number }): buffer
|
||||
local t = buffer.create(Scan.TABLE_SIZE)
|
||||
local code, k = 0, 0
|
||||
local nsym = #symbols
|
||||
local codes, sizes = {}, {}
|
||||
local broken = false
|
||||
for l = 1, 16 do
|
||||
writei32(t, T_DELTA + l * 4, k - code)
|
||||
local n = counts[l] or 0
|
||||
for _ = 1, n do
|
||||
if broken or code >= lshift(1, l) or k >= nsym or k >= 256 then
|
||||
broken = true
|
||||
break
|
||||
end
|
||||
k += 1
|
||||
codes[k] = code
|
||||
sizes[k] = l
|
||||
code += 1
|
||||
end
|
||||
writeu32(t, T_MAX + l * 4, if broken then 0 else lshift(code, 16 - l))
|
||||
code *= 2
|
||||
end
|
||||
writeu32(t, T_MAX + 17 * 4, 0xFFFFFFFF)
|
||||
for i = 1, k do
|
||||
writeu8(t, T_VAL + i - 1, symbols[i])
|
||||
end
|
||||
writeu16(t, T_COUNT, k)
|
||||
-- fast lookups
|
||||
for i = 1, k do
|
||||
local l = sizes[i]
|
||||
if l <= 9 then
|
||||
local sym = symbols[i]
|
||||
local base = lshift(codes[i], 9 - l)
|
||||
for j = 0, lshift(1, 9 - l) - 1 do
|
||||
local idx = base + j
|
||||
writeu16(t, idx * 2, l * 256 + sym)
|
||||
local r, s = rshift(sym, 4), band(sym, 15)
|
||||
if s ~= 0 and l + s <= 9 then
|
||||
local v = band(rshift(idx, 9 - l - s), lshift(1, s) - 1)
|
||||
if v < lshift(1, s - 1) then
|
||||
v = v - lshift(1, s) + 1
|
||||
end
|
||||
writei32(t, T_FASTAC + idx * 4, v * 256 + r * 16 + l + s)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
return t
|
||||
end
|
||||
|
||||
-- Copies the entropy-coded data at src[pos..n) into dst (at least n - pos + PAD bytes): byte stuffing removed,
|
||||
-- RSTn offsets (in dst) stored in rst[1..]. Returns the source position of the marker that ended the scan (or n),
|
||||
-- the clean length, and the RST count. The PAD bytes after the clean data are zeroed.
|
||||
function Scan.destuff(src: buffer, pos: number, n: number, dst: buffer, rst: { number }): (number, number, number)
|
||||
local o, nr = 0, 0
|
||||
while pos < n do
|
||||
local b = readu8(src, pos)
|
||||
if b ~= 0xFF then
|
||||
writeu8(dst, o, b)
|
||||
o += 1
|
||||
pos += 1
|
||||
else
|
||||
local q = pos + 1
|
||||
while q < n and readu8(src, q) == 0xFF do
|
||||
q += 1
|
||||
end
|
||||
if q >= n then
|
||||
pos = n
|
||||
break
|
||||
end
|
||||
local m = readu8(src, q)
|
||||
if m == 0 then
|
||||
writeu8(dst, o, 0xFF)
|
||||
o += 1
|
||||
pos = q + 1
|
||||
elseif m >= 0xD0 and m <= 0xD7 then
|
||||
nr += 1
|
||||
rst[nr] = o
|
||||
pos = q + 1
|
||||
else
|
||||
pos = q - 1
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
buffer.fill(dst, o, 0, Scan.PAD)
|
||||
return pos, o, nr
|
||||
end
|
||||
|
||||
local function huff(d: buffer, pos: number, t: buffer): (number, number)
|
||||
local w = lshift(byteswap(readu32(d, rshift(pos, 3))), band(pos, 7))
|
||||
local f = readu16(t, rshift(w, 23) * 2)
|
||||
if f ~= 0 then
|
||||
return band(f, 255), pos + rshift(f, 8)
|
||||
end
|
||||
local c = rshift(w, 16)
|
||||
local l = 10
|
||||
while c >= readu32(t, T_MAX + l * 4) do
|
||||
l += 1
|
||||
end
|
||||
if l > 16 then
|
||||
return 0, pos + 16 -- invalid code
|
||||
end
|
||||
local idx = rshift(c, 16 - l) + readi32(t, T_DELTA + l * 4)
|
||||
if idx < 0 or idx >= readu16(t, T_COUNT) then
|
||||
return 0, pos + l
|
||||
end
|
||||
return readu8(t, T_VAL + idx), pos + l
|
||||
end
|
||||
|
||||
-- the next s bits (1 <= s <= 16) as an unsigned number
|
||||
local function bits(d: buffer, pos: number, s: number): number
|
||||
return rshift(lshift(byteswap(readu32(d, rshift(pos, 3))), band(pos, 7)), 32 - s)
|
||||
end
|
||||
|
||||
local function extend(v: number, s: number): number
|
||||
if v < lshift(1, s - 1) then
|
||||
return v - lshift(1, s) + 1
|
||||
end
|
||||
return v
|
||||
end
|
||||
|
||||
-- sequential (baseline / extended) block: returns pos, DC predictor
|
||||
local function blockSeq(d: buffer, pos: number, coef: buffer, off: number, dct: buffer, act: buffer, pred: number): (number, number)
|
||||
local s
|
||||
s, pos = huff(d, pos, dct)
|
||||
if s ~= 0 then
|
||||
if s > 16 then
|
||||
s = 16
|
||||
end
|
||||
pred += extend(bits(d, pos, s), s)
|
||||
pos += s
|
||||
end
|
||||
writei16(coef, off, pred)
|
||||
local k = 1
|
||||
while k < 64 do
|
||||
local w = lshift(byteswap(readu32(d, rshift(pos, 3))), band(pos, 7))
|
||||
local fa = readi32(act, T_FASTAC + rshift(w, 23) * 4)
|
||||
if fa ~= 0 then
|
||||
local low = fa % 256
|
||||
k += low // 16
|
||||
pos += low % 16
|
||||
if k > 63 then
|
||||
break
|
||||
end
|
||||
writei16(coef, off + readu8(ZZ, k) * 2, fa // 256)
|
||||
k += 1
|
||||
else
|
||||
local rs
|
||||
rs, pos = huff(d, pos, act)
|
||||
local r, sz = rshift(rs, 4), band(rs, 15)
|
||||
if sz == 0 then
|
||||
if r ~= 15 then
|
||||
break
|
||||
end
|
||||
k += 16
|
||||
else
|
||||
k += r
|
||||
if k > 63 then
|
||||
break
|
||||
end
|
||||
writei16(coef, off + readu8(ZZ, k) * 2, extend(bits(d, pos, sz), sz))
|
||||
pos += sz
|
||||
k += 1
|
||||
end
|
||||
end
|
||||
end
|
||||
return pos, pred
|
||||
end
|
||||
|
||||
local function blockDcFirst(d: buffer, pos: number, coef: buffer, off: number, dct: buffer, pred: number, al: number): (number, number)
|
||||
local s
|
||||
s, pos = huff(d, pos, dct)
|
||||
if s ~= 0 then
|
||||
if s > 16 then
|
||||
s = 16
|
||||
end
|
||||
pred += extend(bits(d, pos, s), s)
|
||||
pos += s
|
||||
end
|
||||
writei16(coef, off, pred * lshift(1, al))
|
||||
return pos, pred
|
||||
end
|
||||
|
||||
local function blockAcFirst(d: buffer, pos: number, coef: buffer, off: number, act: buffer, ss: number, se: number, al: number, eobrun: number): (number, number)
|
||||
if eobrun > 0 then
|
||||
return pos, eobrun - 1
|
||||
end
|
||||
local mul = lshift(1, al)
|
||||
local k = ss
|
||||
while k <= se do
|
||||
local w = lshift(byteswap(readu32(d, rshift(pos, 3))), band(pos, 7))
|
||||
local fa = readi32(act, T_FASTAC + rshift(w, 23) * 4)
|
||||
if fa ~= 0 then
|
||||
local low = fa % 256
|
||||
k += low // 16
|
||||
pos += low % 16
|
||||
if k > se then
|
||||
break
|
||||
end
|
||||
writei16(coef, off + readu8(ZZ, k) * 2, (fa // 256) * mul)
|
||||
k += 1
|
||||
else
|
||||
local rs
|
||||
rs, pos = huff(d, pos, act)
|
||||
local r, s = rshift(rs, 4), band(rs, 15)
|
||||
if s == 0 then
|
||||
if r < 15 then
|
||||
eobrun = lshift(1, r) - 1
|
||||
if r > 0 then
|
||||
eobrun += bits(d, pos, r)
|
||||
pos += r
|
||||
end
|
||||
break
|
||||
end
|
||||
k += 16
|
||||
else
|
||||
k += r
|
||||
if k > se then
|
||||
break
|
||||
end
|
||||
writei16(coef, off + readu8(ZZ, k) * 2, extend(bits(d, pos, s), s) * mul)
|
||||
pos += s
|
||||
k += 1
|
||||
end
|
||||
end
|
||||
end
|
||||
return pos, eobrun
|
||||
end
|
||||
|
||||
-- successive-approximation refinement of AC coefficients ss..se (as stb_image / libjpeg decode_mcu_AC_refine)
|
||||
local function blockAcRefine(d: buffer, pos: number, coef: buffer, off: number, act: buffer, ss: number, se: number, al: number, eobrun: number): (number, number)
|
||||
local bit = lshift(1, al)
|
||||
if eobrun > 0 then
|
||||
for k = ss, se do
|
||||
local o = off + readu8(ZZ, k) * 2
|
||||
local p = readi16(coef, o)
|
||||
if p ~= 0 then
|
||||
local b = rshift(lshift(byteswap(readu32(d, rshift(pos, 3))), band(pos, 7)), 31)
|
||||
pos += 1
|
||||
if b ~= 0 and band(p, bit) == 0 then
|
||||
writei16(coef, o, if p > 0 then p + bit else p - bit)
|
||||
end
|
||||
end
|
||||
end
|
||||
return pos, eobrun - 1
|
||||
end
|
||||
local k = ss
|
||||
while k <= se do
|
||||
local rs
|
||||
rs, pos = huff(d, pos, act)
|
||||
local s, r = band(rs, 15), rshift(rs, 4)
|
||||
local val = 0
|
||||
if s == 0 then
|
||||
if r < 15 then
|
||||
eobrun = lshift(1, r) - 1
|
||||
if r > 0 then
|
||||
eobrun += bits(d, pos, r)
|
||||
pos += r
|
||||
end
|
||||
r = 64 -- the rest of the block only refines
|
||||
end
|
||||
else
|
||||
-- s must be 1; anything else is corrupt and treated as 1
|
||||
local b = rshift(lshift(byteswap(readu32(d, rshift(pos, 3))), band(pos, 7)), 31)
|
||||
pos += 1
|
||||
val = if b ~= 0 then bit else -bit
|
||||
end
|
||||
while k <= se do
|
||||
local o = off + readu8(ZZ, k) * 2
|
||||
k += 1
|
||||
local p = readi16(coef, o)
|
||||
if p ~= 0 then
|
||||
local b = rshift(lshift(byteswap(readu32(d, rshift(pos, 3))), band(pos, 7)), 31)
|
||||
pos += 1
|
||||
if b ~= 0 and band(p, bit) == 0 then
|
||||
writei16(coef, o, if p > 0 then p + bit else p - bit)
|
||||
end
|
||||
else
|
||||
if r == 0 then
|
||||
if val ~= 0 then
|
||||
writei16(coef, o, val)
|
||||
end
|
||||
break
|
||||
end
|
||||
r -= 1
|
||||
end
|
||||
end
|
||||
end
|
||||
-- an EOB run that started in this block counts it already (eobrun = run length - 1)
|
||||
return pos, eobrun
|
||||
end
|
||||
|
||||
export type ScanComp = {
|
||||
coef: buffer, -- i16 coefficients, 64 per block in natural order, blocks row-major on a bpl-wide grid
|
||||
bpl: number,
|
||||
h: number,
|
||||
v: number,
|
||||
bw: number, -- blocks across / down the component itself (non-interleaved scans)
|
||||
bh: number,
|
||||
dct: buffer?,
|
||||
act: buffer?,
|
||||
pred: number,
|
||||
}
|
||||
|
||||
export type ScanParams = {
|
||||
comps: { ScanComp },
|
||||
mcusX: number,
|
||||
mcusY: number,
|
||||
ri: number, -- restart interval in MCUs (0 = none)
|
||||
mode: number, -- 0 sequential, 1 DC first, 2 DC refine, 3 AC first, 4 AC refine
|
||||
ss: number,
|
||||
se: number,
|
||||
al: number,
|
||||
hook: (() -> ())?, -- a cooperative yield point after every MCU row (pcall'ed: it can't throw through here)
|
||||
}
|
||||
|
||||
-- Decodes one scan from the clean data d (cleanLen bytes, then PAD zeros). Returns the final bit position and
|
||||
-- whether the data ran out (or overran) before the last MCU.
|
||||
function Scan.run(d: buffer, cleanLen: number, rst: { number }, nrst: number, P: ScanParams): (number, boolean)
|
||||
local comps = P.comps
|
||||
local nc = #comps
|
||||
local mode, ss, se, al, ri = P.mode, P.ss, P.se, P.al, P.ri
|
||||
local endBits = cleanLen * 8
|
||||
local units, ux
|
||||
if nc == 1 then
|
||||
ux = comps[1].bw
|
||||
units = ux * comps[1].bh
|
||||
else
|
||||
ux = P.mcusX
|
||||
units = ux * P.mcusY
|
||||
end
|
||||
for i = 1, nc do
|
||||
comps[i].pred = 0
|
||||
end
|
||||
local pos, eobrun, rstIdx = 0, 0, 0
|
||||
local truncated, skipping = false, false
|
||||
local bit = lshift(1, al)
|
||||
local hook = P.hook
|
||||
for u = 0, units - 1 do
|
||||
if hook and u > 0 and u % ux == 0 then
|
||||
pcall(hook)
|
||||
end
|
||||
if ri > 0 and u > 0 and u % ri == 0 then
|
||||
rstIdx += 1
|
||||
if rstIdx <= nrst then
|
||||
pos = rst[rstIdx] * 8
|
||||
skipping = false
|
||||
end
|
||||
for i = 1, nc do
|
||||
comps[i].pred = 0
|
||||
end
|
||||
eobrun = 0
|
||||
end
|
||||
if pos > endBits then
|
||||
truncated = true
|
||||
if ri == 0 or rstIdx >= nrst then
|
||||
break
|
||||
end
|
||||
skipping = true
|
||||
end
|
||||
if not skipping 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
|
||||
pos, c.pred = blockSeq(d, pos, coef, off, c.dct :: buffer, c.act :: buffer, c.pred)
|
||||
elseif mode == 1 then
|
||||
pos, c.pred = blockDcFirst(d, pos, coef, off, c.dct :: buffer, c.pred, al)
|
||||
elseif mode == 2 then
|
||||
local b = rshift(lshift(byteswap(readu32(d, rshift(pos, 3))), band(pos, 7)), 31)
|
||||
pos += 1
|
||||
if b ~= 0 then
|
||||
writei16(coef, off, bor(readi16(coef, off), bit))
|
||||
end
|
||||
elseif mode == 3 then
|
||||
pos, eobrun = blockAcFirst(d, pos, coef, off, c.act :: buffer, ss, se, al, eobrun)
|
||||
else
|
||||
pos, eobrun = blockAcRefine(d, pos, coef, off, c.act :: buffer, ss, se, al, eobrun)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
return pos, truncated
|
||||
end
|
||||
|
||||
return Scan
|
||||
Reference in new issue
Block a user