Files
rosetta/src/Formats/audio/opus/RangeEncoder.luau
T

253 lines
5.7 KiB
Luau

--!strict
--!native
--!optimize 2
-- audio/opus/RangeEncoder: the range encoder (RFC 6716 section 5.1), a port of libopus celt/entenc.c (BSD:
-- licenses/libopus-BSD.txt), the mirror of audio/opus/RangeDecoder. 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