codecs/entropy: comments to specs only
This commit is contained in:
1 parent
0bd7b87d2b
commit
878e1b3a44
5 files changed
+18
-61
No files matched your search
+2
-2
@@ -222,8 +222,8 @@ Codecs (layer 3, shared by formats; not loadable on their own), by domain:
|
||||
- codecs/compress/Xxh32: XXH32 of a buffer range (any seed), for the LZ4 frame format's header, block and content
|
||||
- codecs/compress/Zstd: Zstandard decompression (RFC 8878). A codec (layer 3): archive/zst and anything else carrying zstd
|
||||
- **entropy**
|
||||
- codecs/entropy/Huffman: canonical Huffman codes (RFC 1951 3.2.2 / JPEG Annex C order) for decoders and encoders.
|
||||
- codecs/entropy/RangeCoder: the RFC 6716 range coder (libopus celt/entdec.c + entenc.c, BSD: licenses/libopus-BSD.txt):
|
||||
- codecs/entropy/Huffman: canonical Huffman codes (RFC 1951 3.2.2 / JPEG Annex C order).
|
||||
- codecs/entropy/RangeCoder: the RFC 6716 range coder used by Opus, a port of libopus celt/entdec.c + entenc.c.
|
||||
- **font**
|
||||
- codecs/font/Bitmap: the standard Font value for bitmap fonts (BDF, PCF, PSF, Windows FON / FNT): a Types.BitmapStrike
|
||||
- codecs/font/Cff: Compact Font Format (Adobe TN #5176) and Type 2 charstrings (TN #5177): the outlines of OpenType fonts
|
||||
|
||||
@@ -1,18 +1,10 @@
|
||||
--!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.
|
||||
-- codecs/entropy/Huffman: canonical Huffman codes (RFC 1951 3.2.2 / JPEG Annex C order).
|
||||
-- Decoding table entry (u32, indexed by the next P bits; LSB tables by bit-reversed codes, MSB tables by the code):
|
||||
-- symbol * 32 + length a code of length <= P bits
|
||||
-- offset * 32 a link: index offset + the following S bits (subtable of 2^S entries)
|
||||
-- 0 no code (incomplete code set)
|
||||
local Bits = require("@rosetta/core/Bits")
|
||||
|
||||
local band, bor, lshift, rshift = bit32.band, bit32.bor, bit32.lshift, bit32.rshift
|
||||
@@ -24,7 +16,6 @@ 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
|
||||
@@ -35,13 +26,11 @@ for i = 0, 255 do
|
||||
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
|
||||
@@ -90,7 +79,6 @@ local function build(lens: buffer, off: number, n: number, tab: buffer, P: numbe
|
||||
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
|
||||
@@ -124,7 +112,7 @@ local function build(lens: buffer, off: number, n: number, tab: buffer, P: numbe
|
||||
else
|
||||
sub = rshift(e, 5)
|
||||
end
|
||||
local k = l - P -- bits of the code in the subtable index
|
||||
local k = l - P
|
||||
if msb then
|
||||
local first = lshift(rest, S - k)
|
||||
for i = first, first + lshift(1, S - k) - 1 do
|
||||
@@ -144,19 +132,14 @@ local function build(lens: buffer, off: number, n: number, tab: buffer, P: numbe
|
||||
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)
|
||||
@@ -175,7 +158,6 @@ function Huffman.decodeLsb(br: Bits.BitReader, tab: buffer, P: number, S: number
|
||||
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)
|
||||
@@ -194,8 +176,6 @@ function Huffman.decodeMsb(br: Bits.BitReader, tab: buffer, P: number, S: number
|
||||
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
|
||||
@@ -219,12 +199,9 @@ function Huffman.codes(lengths: { number }): { number }
|
||||
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
|
||||
@@ -244,14 +221,13 @@ function Huffman.lengths(freqs: { number }, maxLen: number): { number }
|
||||
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 li, ni, nn = 1, m + 1, m + 1
|
||||
local function take(): number
|
||||
if li <= m and (ni >= nn or weight[li] <= weight[ni]) then
|
||||
li += 1
|
||||
@@ -268,12 +244,11 @@ function Huffman.lengths(freqs: { number }, maxLen: number): { number }
|
||||
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)
|
||||
-- length limiting: JPEG Annex 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
|
||||
@@ -292,7 +267,6 @@ function Huffman.lengths(freqs: { number }, maxLen: number): { number }
|
||||
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
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
--!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.
|
||||
-- codecs/entropy/RangeCoder/Decoder: the Opus range decoder (RFC 6716 4.1), a port of libopus celt/entdec.c (BSD:
|
||||
-- licenses/libopus-BSD.txt). Raw bits (ec_dec_bits) are read from the end of the frame backwards.
|
||||
-- All values stay below 2^32, so floor division on plain numbers is exact.
|
||||
local RangeDecoder = {}
|
||||
|
||||
export type Dec = {
|
||||
@@ -54,7 +53,7 @@ local function normalize(d: Dec)
|
||||
else
|
||||
rem = 0
|
||||
end
|
||||
sym = rshift(bor(lshift(sym, 8), rem), SYM_BITS - CODE_EXTRA) -- (sym << 8 | rem) >> 1
|
||||
sym = rshift(bor(lshift(sym, 8), rem), SYM_BITS - CODE_EXTRA)
|
||||
val = band(val * 256 + band(SYM_MAX, bit32.bnot(sym)), CODE_TOP - 1)
|
||||
end
|
||||
d.rng, d.val, d.rem = rng, val, rem
|
||||
@@ -87,7 +86,6 @@ function RangeDecoder.new(buf: buffer, base: number, storage: number): Dec
|
||||
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
|
||||
@@ -129,7 +127,6 @@ function RangeDecoder.bitLogp(d: Dec, logp: number): boolean
|
||||
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
|
||||
|
||||
@@ -1,10 +1,8 @@
|
||||
--!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`.
|
||||
-- codecs/entropy/RangeCoder/Encoder: the Opus range encoder (RFC 6716 5.1), a port of libopus celt/entenc.c (BSD:
|
||||
-- licenses/libopus-BSD.txt). Range-coded symbols from the front of the frame, raw bits (ec_enc_bits) from the back.
|
||||
local RangeEncoder = {}
|
||||
|
||||
export type Enc = {
|
||||
@@ -37,7 +35,6 @@ local function ilog(x: number): number
|
||||
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)),
|
||||
@@ -101,7 +98,6 @@ local function normalize(e: Enc)
|
||||
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
|
||||
@@ -113,7 +109,6 @@ function RangeEncoder.encode(e: Enc, fl: number, fh: number, ft: number)
|
||||
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)
|
||||
@@ -126,7 +121,6 @@ function RangeEncoder.encodeBin(e: Enc, fl: number, fh: number, bits: number)
|
||||
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)
|
||||
@@ -140,7 +134,6 @@ function RangeEncoder.bitLogp(e: Enc, v: boolean, logp: number)
|
||||
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
|
||||
@@ -152,7 +145,6 @@ function RangeEncoder.icdf(e: Enc, s: number, icdf: { number }, ftb: number)
|
||||
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
|
||||
@@ -170,7 +162,6 @@ function RangeEncoder.bits(e: Enc, fl: number, n: number)
|
||||
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)
|
||||
@@ -202,7 +193,6 @@ function RangeEncoder.tellFrac(e: Enc): number
|
||||
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)
|
||||
|
||||
@@ -1,10 +1,6 @@
|
||||
--!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("@rosetta/codecs/entropy/RangeCoder/Decoder") -- RD.new(buf, base, size), RD.icdf / bits / uint ...
|
||||
-- local RE = require("@rosetta/codecs/entropy/RangeCoder/Encoder") -- RE.new(size), RE.icdf / bits / uint, RE.done
|
||||
-- codecs/entropy/RangeCoder: the RFC 6716 range coder used by Opus, a port of libopus celt/entdec.c + entenc.c.
|
||||
-- BSD: licenses/libopus-BSD.txt.
|
||||
local RangeCoder = {
|
||||
Decoder = require("@rosetta/codecs/entropy/RangeCoder/Decoder"),
|
||||
Encoder = require("@rosetta/codecs/entropy/RangeCoder/Encoder"),
|
||||
|
||||
Reference in new issue
Block a user