ws-cleanup: codecs/ grouped by domain; containers/Chunks, codecs/audio/Stream, codecs/image/Pixels, codecs/model/Mat4
C2, the hierarchy (tools/move.py, every reference rewritten mechanically): - codecs/compress: Deflate Lzma Zstd Brotli Bzip2 Lzw; codecs/entropy: Huffman (from core) RangeCoder; codecs/image: Jpeg Dct Vp8 Vp8L Yuv Quantize Raster Blend Pixels; codecs/audio: Mdct Aac Alac Pcm Resample VorbisComment Tracker SoundFont Stream; codecs/video: H264 Mpeg1 Video; codecs/text: Json Xml Text Url; codecs/font: TrueType Cff; codecs/model: MeshFlatten Mat4. containers/ stays flat; core/ keeps Bytes Bits Err Crc32 Adler32 Base64 Md5 Input Sniff. - LAYERS lint: codecs must sit in a domain folder; gen_registry lists codecs by domain (docs/FORMATS.md). - Checked: build.py both targets, rojo build of build/roblox, the full suite on both runtimes. C3, generic code out of format folders: - containers/Chunks (new): the chunked-file walker (RIFF / RIFX / IFF / PNG / CAF layouts: endianness, size first, trailer, padding, lenient missing pad, open-ended ids, size hooks). containers/Riff walks with it (RF64 ds64 through the resize hook); image/png, audio/aiff and audio/caf list their chunk handlers over it. - codecs/audio/Stream (new): Stream.packets, the packet-decoder Decoder (read / seek with pre-roll / position, priming and end trimming) that aac, caf and m4a each carried (~70 lines each). - codecs/image/Pixels: image/bmp/Pixels promoted (+ rgb555); image/ico's DIB decoder uses its row loops instead of its own (the AND mask / alpha rule stay ICO's). - codecs/model/Mat4 (new): matrices / quaternions / TRS shared by FBX (Xform keeps only the FBX transform model), Collada (Math keeps only <lookat> / <skew>) and USD (its matrix helpers and general 4x4 inverse); Mat4.mul is Types.Scene.mul. The Collada normal matrix is now the inverse transpose (it was the inverse; same result for the fixtures' bind shapes). - README rewritten (install for Rojo and LÖVE / LuaJIT, quick start per category, F.* and F.ops, host notes: FORMATS_NO_JIT_TUNE, paths, layout); docs/STYLE.md: the codec domains, "what belongs in a format folder" with the shared building blocks and the DUP rule, conventions (WAVE order, lower-case tags, loops, the api hooks decodeImage / encodeImage / openAudio / plugins / resolve, the codecs/video/Video registry), Err.from, the type baseline. New tests: tests/containers/chunks, tests/codecs/mat4. Type baseline 93.
This commit is contained in:
1 parent
763f4a7c04
commit
51b08575c4
283 files changed
+1877
-1660
No files matched your search
@@ -0,0 +1,306 @@
|
||||
--!strict
|
||||
--!native
|
||||
-- codecs/entropy/Huffman: canonical Huffman codes (RFC 1951 3.2.2 / JPEG Annex C order) for decoders and encoders.
|
||||
--
|
||||
-- Decoding tables (built by buildLsb / buildMsb) are u32 buffers, indexed by the next P bits of the stream:
|
||||
-- entry = symbol * 32 + length a code of `length` <= P bits
|
||||
-- entry = offset * 32 (length 0) a link: index offset + the following S bits (subtable of 2^S entries)
|
||||
-- entry = 0 no code (incomplete code set): the stream is corrupt
|
||||
-- so a symbol is one or two table reads. LSB tables (DEFLATE, VP8L) are indexed by bit-reversed codes, MSB tables
|
||||
-- (JPEG, MPEG) by the code itself. Code lengths are 1..MAXLEN (16), symbols < 2^27.
|
||||
-- The hot loops (codecs/compress/Deflate/Inflate) inline the lookup on their own bit buffer; decodeLsb / decodeMsb do it on a
|
||||
-- core/Bits reader.
|
||||
--
|
||||
-- Encoding: lengths(freqs, maxLen) -> length-limited code lengths; codes(lengths) -> canonical code values.
|
||||
-- Never throws: bad code sets return -1 / nil.
|
||||
local Bits = require("@formats/core/Bits")
|
||||
|
||||
local band, bor, lshift, rshift = bit32.band, bit32.bor, bit32.lshift, bit32.rshift
|
||||
local readu8, readu32, writeu32 = buffer.readu8, buffer.readu32, buffer.writeu32
|
||||
local bfill = buffer.fill
|
||||
|
||||
local MAXLEN = 16
|
||||
|
||||
local Huffman = {}
|
||||
Huffman.MAXLEN = MAXLEN
|
||||
|
||||
-- 8-bit reversal
|
||||
local REV = table.create(256, 0)
|
||||
for i = 0, 255 do
|
||||
local r, v = 0, i
|
||||
for _ = 1, 8 do
|
||||
r = r * 2 + v % 2
|
||||
v = v // 2
|
||||
end
|
||||
REV[i + 1] = r
|
||||
end
|
||||
|
||||
-- reverse(code, len) -> the low len bits of code reversed (len <= 16)
|
||||
local function reverse(code: number, len: number): number
|
||||
return rshift(bor(lshift(REV[band(code, 255) + 1], 8), REV[band(rshift(code, 8), 255) + 1]), 16 - len)
|
||||
end
|
||||
Huffman.reverse = reverse
|
||||
|
||||
-- capacity(nsym, P, maxLen) -> the table entries (u32) needed for nsym symbols, primary width P, codes up to maxLen
|
||||
function Huffman.capacity(nsym: number, P: number, maxLen: number): number
|
||||
return 2 ^ P + (if maxLen > P then nsym * 2 ^ (maxLen - P) else 0)
|
||||
end
|
||||
|
||||
local count = table.create(MAXLEN + 1, 0)
|
||||
local nextc = table.create(MAXLEN + 1, 0)
|
||||
|
||||
local function build(lens: buffer, off: number, n: number, tab: buffer, P: number, msb: boolean): number
|
||||
for i = 1, MAXLEN + 1 do
|
||||
count[i] = 0
|
||||
end
|
||||
for s = 0, n - 1 do
|
||||
local l = readu8(lens, off + s)
|
||||
if l > MAXLEN then
|
||||
return -1
|
||||
end
|
||||
count[l + 1] += 1
|
||||
end
|
||||
count[1] = 0
|
||||
local left = 1
|
||||
local maxLen = 0
|
||||
for l = 1, MAXLEN do
|
||||
left = left * 2 - count[l + 1]
|
||||
if left < 0 then
|
||||
return -1
|
||||
end
|
||||
if count[l + 1] > 0 then
|
||||
maxLen = l
|
||||
end
|
||||
end
|
||||
local code = 0
|
||||
for l = 1, MAXLEN do
|
||||
code = (code + count[l]) * 2
|
||||
nextc[l] = code
|
||||
end
|
||||
local psize = lshift(1, P)
|
||||
bfill(tab, 0, 0, psize * 4)
|
||||
local S = if maxLen > P then maxLen - P else 0
|
||||
local ssize = lshift(1, S)
|
||||
local nextSub = psize
|
||||
for s = 0, n - 1 do
|
||||
local l = readu8(lens, off + s)
|
||||
if l > 0 then
|
||||
local c = nextc[l]
|
||||
nextc[l] = c + 1
|
||||
local entry = s * 32 + l
|
||||
if l <= P then
|
||||
if msb then
|
||||
-- the code is the top l bits of the P-bit index: fill every completion of the low bits
|
||||
local first = lshift(c, P - l)
|
||||
local last = first + lshift(1, P - l) - 1
|
||||
for i = first, last do
|
||||
writeu32(tab, i * 4, entry)
|
||||
end
|
||||
else
|
||||
local step = lshift(1, l)
|
||||
local i = reverse(c, l)
|
||||
while i < psize do
|
||||
writeu32(tab, i * 4, entry)
|
||||
i += step
|
||||
end
|
||||
end
|
||||
else
|
||||
local pre, rest
|
||||
if msb then
|
||||
pre = rshift(c, l - P)
|
||||
rest = band(c, lshift(1, l - P) - 1)
|
||||
else
|
||||
local rev = reverse(c, l)
|
||||
pre = band(rev, psize - 1)
|
||||
rest = rshift(rev, P)
|
||||
end
|
||||
local e = readu32(tab, pre * 4)
|
||||
local sub
|
||||
if e == 0 then
|
||||
sub = nextSub
|
||||
nextSub += ssize
|
||||
bfill(tab, sub * 4, 0, ssize * 4)
|
||||
writeu32(tab, pre * 4, sub * 32)
|
||||
else
|
||||
sub = rshift(e, 5)
|
||||
end
|
||||
local k = l - P -- bits of the code in the subtable index
|
||||
if msb then
|
||||
local first = lshift(rest, S - k)
|
||||
for i = first, first + lshift(1, S - k) - 1 do
|
||||
writeu32(tab, (sub + i) * 4, entry)
|
||||
end
|
||||
else
|
||||
local step = lshift(1, k)
|
||||
local i = rest
|
||||
while i < ssize do
|
||||
writeu32(tab, (sub + i) * 4, entry)
|
||||
i += step
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
return S
|
||||
end
|
||||
|
||||
-- buildLsb(lens, off, n, tab, P) -> S: a table for the code lengths lens[off .. off + n - 1] (u8), indexed by the next
|
||||
-- P bits LSB-first (bit-reversed codes). S = the subtable width (0 when no code is longer than P), -1 = an
|
||||
-- over-subscribed or invalid code set. tab must hold capacity(n, P, maxLen) u32 entries.
|
||||
function Huffman.buildLsb(lens: buffer, off: number, n: number, tab: buffer, P: number): number
|
||||
return build(lens, off, n, tab, P, false)
|
||||
end
|
||||
|
||||
-- buildMsb(lens, off, n, tab, P) -> S: the same, indexed by the next P bits MSB-first
|
||||
function Huffman.buildMsb(lens: buffer, off: number, n: number, tab: buffer, P: number): number
|
||||
return build(lens, off, n, tab, P, true)
|
||||
end
|
||||
|
||||
-- decodeLsb(br, tab, P, S) -> symbol, or -1 for an invalid code (br: a core/Bits LSB reader)
|
||||
function Huffman.decodeLsb(br: Bits.BitReader, tab: buffer, P: number, S: number): number
|
||||
local e = readu32(tab, br:peek(P) * 4)
|
||||
local l = band(e, 31)
|
||||
if l == 0 then
|
||||
if e == 0 then
|
||||
return -1
|
||||
end
|
||||
local idx = br:peek(P + S)
|
||||
e = readu32(tab, (rshift(e, 5) + rshift(idx, P)) * 4)
|
||||
l = band(e, 31)
|
||||
if l == 0 then
|
||||
return -1
|
||||
end
|
||||
end
|
||||
br:skip(l)
|
||||
return rshift(e, 5)
|
||||
end
|
||||
|
||||
-- decodeMsb(br, tab, P, S) -> symbol, or -1 (br: a core/Bits MSB reader)
|
||||
function Huffman.decodeMsb(br: Bits.BitReader, tab: buffer, P: number, S: number): number
|
||||
local e = readu32(tab, br:peek(P) * 4)
|
||||
local l = band(e, 31)
|
||||
if l == 0 then
|
||||
if e == 0 then
|
||||
return -1
|
||||
end
|
||||
local idx = br:peek(P + S)
|
||||
e = readu32(tab, (rshift(e, 5) + band(idx, lshift(1, S) - 1)) * 4)
|
||||
l = band(e, 31)
|
||||
if l == 0 then
|
||||
return -1
|
||||
end
|
||||
end
|
||||
br:skip(l)
|
||||
return rshift(e, 5)
|
||||
end
|
||||
|
||||
-- codes(lengths) -> canonical code values (MSB-first, as written by an MSB writer; reverse() them for LSB streams).
|
||||
-- lengths: { number } indexed 1..n (symbol + 1), 0 = unused.
|
||||
function Huffman.codes(lengths: { number }): { number }
|
||||
local cnt = table.create(MAXLEN + 1, 0)
|
||||
for _, l in lengths do
|
||||
if l > 0 then
|
||||
cnt[l + 1] += 1
|
||||
end
|
||||
end
|
||||
local nxt = table.create(MAXLEN + 1, 0)
|
||||
local code = 0
|
||||
for l = 1, MAXLEN do
|
||||
code = (code + cnt[l]) * 2
|
||||
nxt[l + 1] = code
|
||||
end
|
||||
local out = table.create(#lengths, 0)
|
||||
for i, l in lengths do
|
||||
if l > 0 then
|
||||
out[i] = nxt[l + 1]
|
||||
nxt[l + 1] += 1
|
||||
end
|
||||
end
|
||||
return out
|
||||
end
|
||||
|
||||
-- lengths(freqs, maxLen) -> code lengths ({ number }, same indexing as freqs), at most maxLen bits, optimal without
|
||||
-- the limit; over the limit, the JPEG Annex K.3 adjustment. A single used symbol gets length 1.
|
||||
function Huffman.lengths(freqs: { number }, maxLen: number): { number }
|
||||
local n = #freqs
|
||||
local out = table.create(n, 0)
|
||||
-- leaves sorted by frequency (ties by symbol, for determinism)
|
||||
local syms = {}
|
||||
for i = 1, n do
|
||||
if freqs[i] > 0 then
|
||||
table.insert(syms, i)
|
||||
end
|
||||
end
|
||||
if #syms == 0 then
|
||||
return out
|
||||
end
|
||||
if #syms == 1 then
|
||||
out[syms[1]] = 1
|
||||
return out
|
||||
end
|
||||
table.sort(syms, function(a: number, b: number): boolean
|
||||
if freqs[a] ~= freqs[b] then
|
||||
return freqs[a] < freqs[b]
|
||||
end
|
||||
return a < b
|
||||
end)
|
||||
-- two-queue Huffman: leaves (sorted) and internal nodes (created in increasing weight order)
|
||||
local m = #syms
|
||||
local weight = table.create(2 * m, 0)
|
||||
local parent = table.create(2 * m, 0)
|
||||
for i = 1, m do
|
||||
weight[i] = freqs[syms[i]]
|
||||
end
|
||||
local li, ni, nn = 1, m + 1, m + 1 -- next leaf, next internal node to take, next free node
|
||||
local function take(): number
|
||||
if li <= m and (ni >= nn or weight[li] <= weight[ni]) then
|
||||
li += 1
|
||||
return li - 1
|
||||
end
|
||||
ni += 1
|
||||
return ni - 1
|
||||
end
|
||||
for _ = 1, m - 1 do
|
||||
local a = take()
|
||||
local b = take()
|
||||
weight[nn] = weight[a] + weight[b]
|
||||
parent[a] = nn
|
||||
parent[b] = nn
|
||||
nn += 1
|
||||
end
|
||||
-- depths: the root is nn - 1
|
||||
local depth = table.create(2 * m, 0)
|
||||
for i = nn - 2, 1, -1 do
|
||||
depth[i] = depth[parent[i]] + 1
|
||||
end
|
||||
-- count codes per length; limit (JPEG K.3: move pairs from the deepest level up)
|
||||
local bl = table.create(m + maxLen + 2, 0)
|
||||
local deepest = 0
|
||||
for i = 1, m do
|
||||
bl[depth[i]] += 1
|
||||
deepest = math.max(deepest, depth[i])
|
||||
end
|
||||
for l = deepest, maxLen + 1, -1 do
|
||||
while bl[l] > 0 do
|
||||
local j = l - 2
|
||||
while j > 0 and bl[j] == 0 do
|
||||
j -= 1
|
||||
end
|
||||
bl[l] -= 2
|
||||
bl[l - 1] += 1
|
||||
bl[j + 1] += 2
|
||||
bl[j] -= 1
|
||||
end
|
||||
end
|
||||
-- the longest codes go to the least frequent symbols (syms is sorted ascending)
|
||||
local k = 1
|
||||
for l = math.min(deepest, maxLen), 1, -1 do
|
||||
for _ = 1, bl[l] do
|
||||
out[syms[k]] = l
|
||||
k += 1
|
||||
end
|
||||
end
|
||||
return out
|
||||
end
|
||||
|
||||
return Huffman
|
||||
@@ -0,0 +1,215 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- codecs/entropy/RangeCoder/Decoder: the Opus 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
|
||||
@@ -0,0 +1,252 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- codecs/entropy/RangeCoder/Encoder: the Opus range encoder (RFC 6716 section 5.1), a port of libopus celt/entenc.c (BSD:
|
||||
-- licenses/libopus-BSD.txt), the mirror of codecs/entropy/RangeCoder/Decoder. Writes one fixed-size frame: the range-coded
|
||||
-- symbols from the front, the raw bits (ec_enc_bits) from the back, zeros between. Values stay below 2^32 as plain
|
||||
-- numbers. Never throws: running out of room sets `error`.
|
||||
local RangeEncoder = {}
|
||||
|
||||
export type Enc = {
|
||||
buf: buffer,
|
||||
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 CODE_TOP = 2147483648 -- 1 << 31
|
||||
local CODE_BOT = 8388608 -- CODE_TOP >> 8
|
||||
local CODE_SHIFT = 23
|
||||
local SYM_MAX = 255
|
||||
|
||||
local countlz = bit32.countlz
|
||||
local band = bit32.band
|
||||
local readu8, writeu8 = buffer.readu8, buffer.writeu8
|
||||
local floor = math.floor
|
||||
|
||||
local function ilog(x: number): number
|
||||
return 32 - countlz(x)
|
||||
end
|
||||
RangeEncoder.ilog = ilog
|
||||
|
||||
-- new(storage) -> Enc over a fresh zeroed buffer of storage bytes
|
||||
function RangeEncoder.new(storage: number): Enc
|
||||
return {
|
||||
buf = buffer.create(math.max(storage, 1)),
|
||||
storage = storage,
|
||||
offs = 0,
|
||||
end_offs = 0,
|
||||
end_window = 0,
|
||||
nend_bits = 0,
|
||||
nbits_total = 33, -- EC_CODE_BITS + 1
|
||||
rng = CODE_TOP,
|
||||
val = 0,
|
||||
rem = -1,
|
||||
ext = 0,
|
||||
error = 0,
|
||||
}
|
||||
end
|
||||
|
||||
local function writeByte(e: Enc, v: number)
|
||||
if e.offs + e.end_offs >= e.storage then
|
||||
e.error = -1
|
||||
return
|
||||
end
|
||||
writeu8(e.buf, e.offs, band(v, 255))
|
||||
e.offs += 1
|
||||
end
|
||||
|
||||
local function writeByteAtEnd(e: Enc, v: number)
|
||||
if e.offs + e.end_offs >= e.storage then
|
||||
e.error = -1
|
||||
return
|
||||
end
|
||||
e.end_offs += 1
|
||||
writeu8(e.buf, e.storage - e.end_offs, band(v, 255))
|
||||
end
|
||||
|
||||
local function carryOut(e: Enc, c: number)
|
||||
if c ~= SYM_MAX then
|
||||
local carry = if c >= 256 then 1 else 0
|
||||
if e.rem >= 0 then
|
||||
writeByte(e, e.rem + carry)
|
||||
end
|
||||
if e.ext > 0 then
|
||||
local sym = band(SYM_MAX + carry, 255)
|
||||
repeat
|
||||
writeByte(e, sym)
|
||||
e.ext -= 1
|
||||
until e.ext <= 0
|
||||
end
|
||||
e.rem = band(c, 255)
|
||||
else
|
||||
e.ext += 1
|
||||
end
|
||||
end
|
||||
|
||||
local function normalize(e: Enc)
|
||||
while e.rng <= CODE_BOT do
|
||||
carryOut(e, floor(e.val / 8388608))
|
||||
e.val = (e.val * 256) % CODE_TOP
|
||||
e.rng *= 256
|
||||
e.nbits_total += 8
|
||||
end
|
||||
end
|
||||
|
||||
-- encode(fl, fh, ft): the symbol [fl, fh) of ft
|
||||
function RangeEncoder.encode(e: Enc, fl: number, fh: number, ft: number)
|
||||
local r = floor(e.rng / ft)
|
||||
if fl > 0 then
|
||||
e.val += e.rng - r * (ft - fl)
|
||||
e.rng = r * (fh - fl)
|
||||
else
|
||||
e.rng -= r * (ft - fh)
|
||||
end
|
||||
normalize(e)
|
||||
end
|
||||
|
||||
-- encodeBin(fl, fh, bits): ft = 2^bits
|
||||
function RangeEncoder.encodeBin(e: Enc, fl: number, fh: number, bits: number)
|
||||
local ft = 2 ^ bits
|
||||
local r = floor(e.rng / ft)
|
||||
if fl > 0 then
|
||||
e.val += e.rng - r * (ft - fl)
|
||||
e.rng = r * (fh - fl)
|
||||
else
|
||||
e.rng -= r * (ft - fh)
|
||||
end
|
||||
normalize(e)
|
||||
end
|
||||
|
||||
-- bitLogp(v, logp): a bit whose probability of being 1 is 2^-logp
|
||||
function RangeEncoder.bitLogp(e: Enc, v: boolean, logp: number)
|
||||
local r = e.rng
|
||||
local s = floor(r / 2 ^ logp)
|
||||
r -= s
|
||||
if v then
|
||||
e.val += r
|
||||
e.rng = s
|
||||
else
|
||||
e.rng = r
|
||||
end
|
||||
normalize(e)
|
||||
end
|
||||
|
||||
-- icdf(s, icdf, ftb): symbol s (0-based) of an inverse CDF table (1-based Luau array) with 2^ftb total
|
||||
function RangeEncoder.icdf(e: Enc, s: number, icdf: { number }, ftb: number)
|
||||
local r = floor(e.rng / 2 ^ ftb)
|
||||
if s > 0 then
|
||||
e.val += e.rng - r * icdf[s]
|
||||
e.rng = r * (icdf[s] - icdf[s + 1])
|
||||
else
|
||||
e.rng -= r * icdf[s + 1]
|
||||
end
|
||||
normalize(e)
|
||||
end
|
||||
|
||||
-- bits(fl, n): n raw bits (n <= 25), written from the end of the frame
|
||||
function RangeEncoder.bits(e: Enc, fl: number, n: number)
|
||||
local window = e.end_window
|
||||
local used = e.nend_bits
|
||||
if used + n > 32 then
|
||||
repeat
|
||||
writeByteAtEnd(e, window % 256)
|
||||
window = floor(window / 256)
|
||||
used -= 8
|
||||
until used < 8
|
||||
end
|
||||
window += fl * 2 ^ used
|
||||
used += n
|
||||
e.end_window = window
|
||||
e.nend_bits = used
|
||||
e.nbits_total += n
|
||||
end
|
||||
|
||||
-- uint(fl, ft): fl uniformly in [0, ft), ft up to 2^32
|
||||
function RangeEncoder.uint(e: Enc, fl: number, ft: number)
|
||||
ft -= 1
|
||||
local ftb = ilog(ft)
|
||||
if ftb > 8 then
|
||||
ftb -= 8
|
||||
local sh = 2 ^ ftb
|
||||
local ft1 = floor(ft / sh) + 1
|
||||
local hi = floor(fl / sh)
|
||||
RangeEncoder.encode(e, hi, hi + 1, ft1)
|
||||
RangeEncoder.bits(e, fl - hi * sh, ftb)
|
||||
else
|
||||
RangeEncoder.encode(e, fl, fl + 1, ft + 1)
|
||||
end
|
||||
end
|
||||
|
||||
function RangeEncoder.tell(e: Enc): number
|
||||
return e.nbits_total - ilog(e.rng)
|
||||
end
|
||||
|
||||
local CORRECTION = { 35733, 38967, 42495, 46340, 50535, 55109, 60097, 65535 }
|
||||
function RangeEncoder.tellFrac(e: Enc): number
|
||||
local nbits = e.nbits_total * 8
|
||||
local l = ilog(e.rng)
|
||||
local r = floor(e.rng / 2 ^ (l - 16))
|
||||
local b = floor(r / 4096) - 8
|
||||
if r > CORRECTION[b + 1] then
|
||||
b += 1
|
||||
end
|
||||
return nbits - (l * 8 + b)
|
||||
end
|
||||
|
||||
-- done(): flush; the frame is e.buf (storage bytes)
|
||||
function RangeEncoder.done(e: Enc)
|
||||
local l = 32 - ilog(e.rng)
|
||||
local msk = floor((CODE_TOP - 1) / 2 ^ l)
|
||||
local stop = floor((e.val + msk) / (msk + 1)) * (msk + 1)
|
||||
if stop + msk >= e.val + e.rng then
|
||||
l += 1
|
||||
msk = floor(msk / 2)
|
||||
stop = floor((e.val + msk) / (msk + 1)) * (msk + 1)
|
||||
end
|
||||
while l > 0 do
|
||||
carryOut(e, floor(stop / 8388608))
|
||||
stop = (stop * 256) % CODE_TOP
|
||||
l -= 8
|
||||
end
|
||||
if e.rem >= 0 or e.ext > 0 then
|
||||
carryOut(e, 0)
|
||||
end
|
||||
local window = e.end_window
|
||||
local used = e.nend_bits
|
||||
while used >= 8 do
|
||||
writeByteAtEnd(e, window % 256)
|
||||
window = floor(window / 256)
|
||||
used -= 8
|
||||
end
|
||||
if e.error == 0 then
|
||||
local a, b = e.offs, e.storage - e.end_offs
|
||||
if b > a then
|
||||
buffer.fill(e.buf, a, 0, b - a)
|
||||
end
|
||||
if used > 0 then
|
||||
if e.end_offs >= e.storage then
|
||||
e.error = -1
|
||||
else
|
||||
l = -l
|
||||
if e.offs + e.end_offs >= e.storage and l < used then
|
||||
window = window % 2 ^ l
|
||||
e.error = -1
|
||||
end
|
||||
local p = e.storage - e.end_offs - 1
|
||||
writeu8(e.buf, p, bit32.bor(readu8(e.buf, p), window))
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local _ = CODE_SHIFT
|
||||
return RangeEncoder
|
||||
@@ -0,0 +1,13 @@
|
||||
--!strict
|
||||
-- codecs/entropy/RangeCoder: the RFC 6716 range coder (libopus celt/entdec.c + entenc.c, BSD: licenses/libopus-BSD.txt):
|
||||
-- symbols range-coded from the front of a frame, raw bits from its back. Opus (CELT and SILK) uses it; LZMA, VP8 and
|
||||
-- CABAC have their own (different) binary coders. Moved here from audio/opus by the cleanup pass (same code).
|
||||
--
|
||||
-- local RD = require("@formats/codecs/entropy/RangeCoder/Decoder") -- RD.new(buf, base, size), RD.icdf / bits / uint ...
|
||||
-- local RE = require("@formats/codecs/entropy/RangeCoder/Encoder") -- RE.new(size), RE.icdf / bits / uint, RE.done
|
||||
local RangeCoder = {
|
||||
Decoder = require("@formats/codecs/entropy/RangeCoder/Decoder"),
|
||||
Encoder = require("@formats/codecs/entropy/RangeCoder/Encoder"),
|
||||
}
|
||||
|
||||
return RangeCoder
|
||||
Reference in new issue
Block a user