ws-cleanup: one base64 (core/Base64)
core/Base64 takes over codecs/Base64's API (encode(buf | string, { url, pad, wrap }, offset, length), decode of both
alphabets, decodeDataUri, dataUri) plus line wrapping (opts.wrap) for plist; codecs/Base64 is removed and glTF /
DAE require core/Base64. The private copies in data/plist (Base64.luau), data/yaml (Parse / Emit) and codecs/Brotli
(the static tables) are gone. tests/codecs/base64 merged into tests/core/base64.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
a289eba8af
commit
a25b7c0caa
18 files changed
+202
-410
No files matched your search
@@ -0,0 +1,214 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- audio/opus/RangeDecoder: Range decoder (RFC 6716 section 4.1, libopus celt/entdec.c). Reads one Opus frame from a `buffer` slice
|
||||
-- [base, base + storage). Raw bits (ec_dec_bits) are read from the end of the slice backwards.
|
||||
-- All values stay below 2^32, so plain Luau numbers hold them exactly; floor division is exact below 2^52.
|
||||
local RangeDecoder = {}
|
||||
|
||||
export type Dec = {
|
||||
buf: buffer,
|
||||
base: number,
|
||||
storage: number,
|
||||
offs: number,
|
||||
end_offs: number,
|
||||
end_window: number,
|
||||
nend_bits: number,
|
||||
nbits_total: number,
|
||||
rng: number,
|
||||
val: number,
|
||||
rem: number,
|
||||
ext: number,
|
||||
error: number,
|
||||
}
|
||||
|
||||
local SYM_BITS = 8
|
||||
local CODE_BITS = 32
|
||||
local CODE_TOP = 2147483648 -- 1 << 31
|
||||
local CODE_BOT = 8388608 -- CODE_TOP >> 8
|
||||
local CODE_EXTRA = 7 -- (CODE_BITS - 2) % SYM_BITS + 1
|
||||
local SYM_MAX = 255
|
||||
local UINT_BITS = 8
|
||||
local WINDOW_SIZE = 32
|
||||
|
||||
local countlz = bit32.countlz
|
||||
local band, bor, lshift, rshift = bit32.band, bit32.bor, bit32.lshift, bit32.rshift
|
||||
local readu8 = buffer.readu8
|
||||
|
||||
local function ilog(x: number): number
|
||||
return 32 - countlz(x)
|
||||
end
|
||||
RangeDecoder.ilog = ilog
|
||||
|
||||
local function normalize(d: Dec)
|
||||
local rng, val, rem = d.rng, d.val, d.rem
|
||||
while rng <= CODE_BOT do
|
||||
d.nbits_total += SYM_BITS
|
||||
rng = rng * 256
|
||||
local sym = rem
|
||||
local offs = d.offs
|
||||
if offs < d.storage then
|
||||
rem = readu8(d.buf, d.base + offs)
|
||||
d.offs = offs + 1
|
||||
else
|
||||
rem = 0
|
||||
end
|
||||
sym = rshift(bor(lshift(sym, 8), rem), SYM_BITS - CODE_EXTRA) -- (sym << 8 | rem) >> 1
|
||||
val = band(val * 256 + band(SYM_MAX, bit32.bnot(sym)), CODE_TOP - 1)
|
||||
end
|
||||
d.rng, d.val, d.rem = rng, val, rem
|
||||
end
|
||||
|
||||
function RangeDecoder.new(buf: buffer, base: number, storage: number): Dec
|
||||
local d: Dec = {
|
||||
buf = buf,
|
||||
base = base,
|
||||
storage = storage,
|
||||
offs = 0,
|
||||
end_offs = 0,
|
||||
end_window = 0,
|
||||
nend_bits = 0,
|
||||
nbits_total = CODE_BITS + 1 - ((CODE_BITS - CODE_EXTRA) // SYM_BITS) * SYM_BITS,
|
||||
rng = 128, -- 1 << CODE_EXTRA
|
||||
val = 0,
|
||||
rem = 0,
|
||||
ext = 0,
|
||||
error = 0,
|
||||
}
|
||||
local rem = 0
|
||||
if storage > 0 then
|
||||
rem = readu8(buf, base)
|
||||
d.offs = 1
|
||||
end
|
||||
d.rem = rem
|
||||
d.val = d.rng - 1 - rshift(rem, SYM_BITS - CODE_EXTRA)
|
||||
normalize(d)
|
||||
return d
|
||||
end
|
||||
|
||||
-- ec_decode: the cumulative frequency of the next symbol, for a total of ft
|
||||
function RangeDecoder.decode(d: Dec, ft: number): number
|
||||
local ext = d.rng // ft
|
||||
d.ext = ext
|
||||
local s = d.val // ext
|
||||
return ft - math.min(s + 1, ft)
|
||||
end
|
||||
|
||||
function RangeDecoder.decodeBin(d: Dec, bits: number): number
|
||||
local ext = rshift(d.rng, bits)
|
||||
d.ext = ext
|
||||
local s = d.val // ext
|
||||
local ft = lshift(1, bits)
|
||||
return ft - math.min(s + 1, ft)
|
||||
end
|
||||
|
||||
function RangeDecoder.update(d: Dec, fl: number, fh: number, ft: number)
|
||||
local s = d.ext * (ft - fh)
|
||||
d.val = band(d.val - s, 0xFFFFFFFF) -- uint32 wrap (corrupt streams only)
|
||||
if fl > 0 then
|
||||
d.rng = d.ext * (fh - fl)
|
||||
else
|
||||
d.rng -= s
|
||||
end
|
||||
normalize(d)
|
||||
end
|
||||
|
||||
function RangeDecoder.bitLogp(d: Dec, logp: number): boolean
|
||||
local r = d.rng
|
||||
local v = d.val
|
||||
local s = rshift(r, logp)
|
||||
local ret = v < s
|
||||
if ret then
|
||||
d.rng = s
|
||||
else
|
||||
d.val = v - s
|
||||
d.rng = r - s
|
||||
end
|
||||
normalize(d)
|
||||
return ret
|
||||
end
|
||||
|
||||
-- ec_dec_icdf over a Luau array (1-based) starting at index `off` (icdf[off] is entry 0)
|
||||
function RangeDecoder.icdf(d: Dec, icdf: { number }, off: number, ftb: number): number
|
||||
local s = d.rng
|
||||
local v = d.val
|
||||
local r = rshift(s, ftb)
|
||||
local ret = -1
|
||||
local t
|
||||
repeat
|
||||
t = s
|
||||
ret += 1
|
||||
s = r * icdf[off + ret]
|
||||
until v >= s
|
||||
d.val = v - s
|
||||
d.rng = t - s
|
||||
normalize(d)
|
||||
return ret
|
||||
end
|
||||
|
||||
function RangeDecoder.bits(d: Dec, bits: number): number
|
||||
local window = d.end_window
|
||||
local available = d.nend_bits
|
||||
if available < bits then
|
||||
repeat
|
||||
local b = 0
|
||||
local eo = d.end_offs
|
||||
if eo < d.storage then
|
||||
eo += 1
|
||||
d.end_offs = eo
|
||||
b = readu8(d.buf, d.base + d.storage - eo)
|
||||
end
|
||||
window = bor(window, lshift(b, available))
|
||||
available += SYM_BITS
|
||||
until available > WINDOW_SIZE - SYM_BITS
|
||||
end
|
||||
local ret = band(window, lshift(1, bits) - 1)
|
||||
if bits == 32 then
|
||||
ret = window
|
||||
end
|
||||
window = rshift(window, bits)
|
||||
available -= bits
|
||||
d.end_window = window
|
||||
d.nend_bits = available
|
||||
d.nbits_total += bits
|
||||
return ret
|
||||
end
|
||||
|
||||
function RangeDecoder.uint(d: Dec, ft0: number): number
|
||||
local ft = ft0 - 1
|
||||
local ftb = ilog(ft)
|
||||
if ftb > UINT_BITS then
|
||||
ftb -= UINT_BITS
|
||||
local ft1 = rshift(ft, ftb) + 1
|
||||
local s = RangeDecoder.decode(d, ft1)
|
||||
RangeDecoder.update(d, s, s + 1, ft1)
|
||||
local t = bor(lshift(s, ftb), RangeDecoder.bits(d, ftb))
|
||||
if t <= ft then
|
||||
return t
|
||||
end
|
||||
d.error = 1
|
||||
return ft
|
||||
else
|
||||
local s = RangeDecoder.decode(d, ft0)
|
||||
RangeDecoder.update(d, s, s + 1, ft0)
|
||||
return s
|
||||
end
|
||||
end
|
||||
|
||||
function RangeDecoder.tell(d: Dec): number
|
||||
return d.nbits_total - ilog(d.rng)
|
||||
end
|
||||
|
||||
local correction = { 35733, 38967, 42495, 46340, 50535, 55109, 60097, 65535 }
|
||||
function RangeDecoder.tellFrac(d: Dec): number
|
||||
local nbits = d.nbits_total * 8
|
||||
local l = ilog(d.rng)
|
||||
local r = rshift(d.rng, l - 16)
|
||||
local b = rshift(r, 12) - 8
|
||||
if r > correction[b + 1] then
|
||||
b += 1
|
||||
end
|
||||
return nbits - (l * 8 + b)
|
||||
end
|
||||
|
||||
return RangeDecoder
|
||||
Reference in new issue
Block a user