ws-cleanup: audio codecs out of the format folders (codecs/audio/Opus, Vorbis, Flac, Mpa)
The decoders / encoders are libraries, not the file format: audio/opus/{Celt, CeltTables, CeltEncoder, Mdct, Packet,
Silk, SilkTables, Decoder} -> codecs/audio/Opus; audio/vorbis/{Codebook, Packet, Setup, Tables} ->
codecs/audio/Vorbis; audio/flac/{Frame, Encode} -> codecs/audio/Flac; audio/mp3/{Core, Tables, Header, Encode2,
Encode3, EncTables, Analysis} -> codecs/audio/Mpa (tools/move.py; code unchanged). Each gets an index init.luau.
The format folders keep the file mapping: Ogg Opus / Ogg Vorbis (headers, granules, seeking, the Vorbis encoder's
Ogg writer), native / Ogg FLAC streams and metadata, ID3 / Xing / the MP3 frame index. docs: STYLE says a codec is
not the format. Full suite: 248 runs pass on both runtimes, lint green.
This commit is contained in:
1 parent
f5cf02026d
commit
9941113b7b
39 files changed
+146
-82
No files matched your search
@@ -0,0 +1,988 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- codecs/audio/Flac/Encode: the FLAC encoder (RFC 9639). New code, written from the RFC; the presets mirror libFLAC's -0 .. -8
|
||||
-- (block size 1152 / 4096, max LPC order 0 / 6 / 8 / 12, Rice partition order 3 .. 6, independent / adaptive /
|
||||
-- exhaustive stereo decorrelation). Subframes: CONSTANT, VERBATIM, FIXED (order 0..4, chosen by the residual sum),
|
||||
-- LPC (Levinson-Durbin on the tukey(0.5)-windowed autocorrelation, error-feedback coefficient quantization with
|
||||
-- libFLAC's precision table, the order estimated from the prediction error or searched exhaustively at -8), wasted
|
||||
-- bits; Rice / Rice2 partitions with the partition order and parameters searched per subframe. Frames carry CRC-8 /
|
||||
-- CRC-16; STREAMINFO carries the sizes, total samples and the MD5 of the PCM; VORBIS_COMMENT from the tags; an optional
|
||||
-- SEEKTABLE. Streams: input blocks are pulled from a codecs/audio/Pcm Source, so a Decoder is encoded without decoding it
|
||||
-- whole. Never throws: bad options are rejected by audio/flac before calling in.
|
||||
local Bits = require("@formats/core/Bits")
|
||||
local Md5 = require("@formats/core/Md5")
|
||||
local VorbisComment = require("@formats/codecs/audio/VorbisComment")
|
||||
local Frame = require("@formats/codecs/audio/Flac/Frame")
|
||||
|
||||
local Encode = {}
|
||||
|
||||
local floor, abs, log = math.floor, math.abs, math.log
|
||||
local readf32, writeu8 = buffer.readf32, buffer.writeu8
|
||||
|
||||
local POW = table.create(64, 0)
|
||||
for i = 0, 63 do
|
||||
POW[i + 1] = 2 ^ i
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ CRCs
|
||||
-- (the decoder's tables: codecs/audio/Flac/Frame; (b, offset, count) here)
|
||||
local function crc8(b: buffer, o: number, n: number): number
|
||||
return Frame.crc8(b, o, o + n)
|
||||
end
|
||||
local function crc16(b: buffer, o: number, n: number): number
|
||||
return Frame.crc16(b, o, o + n)
|
||||
end
|
||||
Encode.crc8, Encode.crc16 = crc8, crc16
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ bit writer (MSB first)
|
||||
type W = Bits.RawWriter
|
||||
|
||||
local function ensure(w: W, n: number)
|
||||
local need = w.pos + n + 16
|
||||
local cap = buffer.len(w.buf)
|
||||
if need > cap then
|
||||
local nb = buffer.create(math.max(cap * 2, need))
|
||||
buffer.copy(nb, 0, w.buf, 0, w.pos)
|
||||
w.buf = nb
|
||||
end
|
||||
end
|
||||
|
||||
local put = Bits.msbPut
|
||||
|
||||
local function putSigned(w: W, v: number, n: number)
|
||||
put(w, if v < 0 then v + POW[n + 1] else v, n)
|
||||
end
|
||||
|
||||
local function alignByte(w: W)
|
||||
if w.cnt > 0 then
|
||||
put(w, 0, 8 - w.cnt)
|
||||
end
|
||||
end
|
||||
|
||||
local function putUtf8(w: W, v: number)
|
||||
if v < 0x80 then
|
||||
put(w, v, 8)
|
||||
return
|
||||
end
|
||||
local n = if v < 0x800 then 2 elseif v < 0x10000 then 3 elseif v < 0x200000 then 4 elseif v < 0x4000000 then 5 elseif v < 0x80000000 then 6 else 7
|
||||
local first = if n == 7 then 0xFE else (0xFF00 // POW[n + 1]) % 256 -- n leading ones
|
||||
local shift = 6 * (n - 1)
|
||||
put(w, first + floor(v / POW[shift + 1]), 8)
|
||||
for k = n - 2, 0, -1 do
|
||||
put(w, 0x80 + floor(v / POW[6 * k + 1]) % 64, 8)
|
||||
end
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ presets
|
||||
export type Config = {
|
||||
blockSize: number,
|
||||
maxLpcOrder: number,
|
||||
maxPartitionOrder: number,
|
||||
stereo: string, -- "independent" | "adaptive" | "exhaustive"
|
||||
exhaustiveOrder: boolean,
|
||||
}
|
||||
local PRESETS: { Config } = {
|
||||
{ blockSize = 1152, maxLpcOrder = 0, maxPartitionOrder = 3, stereo = "independent", exhaustiveOrder = false },
|
||||
{ blockSize = 1152, maxLpcOrder = 0, maxPartitionOrder = 3, stereo = "adaptive", exhaustiveOrder = false },
|
||||
{ blockSize = 1152, maxLpcOrder = 0, maxPartitionOrder = 3, stereo = "exhaustive", exhaustiveOrder = false },
|
||||
{ blockSize = 4096, maxLpcOrder = 6, maxPartitionOrder = 4, stereo = "independent", exhaustiveOrder = false },
|
||||
{ blockSize = 4096, maxLpcOrder = 8, maxPartitionOrder = 4, stereo = "adaptive", exhaustiveOrder = false },
|
||||
{ blockSize = 4096, maxLpcOrder = 8, maxPartitionOrder = 5, stereo = "exhaustive", exhaustiveOrder = false },
|
||||
{ blockSize = 4096, maxLpcOrder = 8, maxPartitionOrder = 6, stereo = "exhaustive", exhaustiveOrder = false },
|
||||
{ blockSize = 4096, maxLpcOrder = 12, maxPartitionOrder = 6, stereo = "exhaustive", exhaustiveOrder = false },
|
||||
{ blockSize = 4096, maxLpcOrder = 12, maxPartitionOrder = 6, stereo = "exhaustive", exhaustiveOrder = true },
|
||||
}
|
||||
Encode.PRESETS = PRESETS
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ analysis
|
||||
-- a subframe plan for one channel signal
|
||||
type Plan = {
|
||||
kind: number, -- 0 constant, 1 verbatim, 2 fixed, 3 lpc
|
||||
bits: number, -- estimated size in bits (with the subframe header)
|
||||
order: number,
|
||||
wasted: number,
|
||||
bps: number, -- sample size after removing wasted bits
|
||||
res: { number }, -- residual (fixed / lpc), indices 1..n
|
||||
porder: number,
|
||||
params: { number },
|
||||
method: number, -- 0 Rice (4-bit params), 1 Rice2 (5-bit)
|
||||
qlp: { number },
|
||||
shift: number,
|
||||
precision: number,
|
||||
x: { number }, -- the (shifted) samples
|
||||
}
|
||||
|
||||
local function newPlan(n: number): Plan
|
||||
return {
|
||||
kind = 1,
|
||||
bits = 0,
|
||||
order = 0,
|
||||
wasted = 0,
|
||||
bps = 16,
|
||||
res = table.create(n, 0),
|
||||
porder = 0,
|
||||
params = table.create(64, 0),
|
||||
method = 0,
|
||||
qlp = table.create(32, 0),
|
||||
shift = 0,
|
||||
precision = 0,
|
||||
x = table.create(n, 0),
|
||||
}
|
||||
end
|
||||
|
||||
-- scratch per encoder
|
||||
type Scratch = {
|
||||
sums: { number },
|
||||
counts: { number },
|
||||
ks: { number },
|
||||
bestKs: { number },
|
||||
tmpRes: { number },
|
||||
win: { number },
|
||||
winN: number,
|
||||
wx: { number },
|
||||
autoc: { number },
|
||||
lpc: { { number } },
|
||||
err: { number },
|
||||
qlp: { number },
|
||||
}
|
||||
|
||||
-- rice(res, n, order, maxP, plan?) -> bits, porder, method (fills scratch.bestKs with the parameters)
|
||||
local function rice(sc: Scratch, res: { number }, n: number, order: number, maxP: number): (number, number, number)
|
||||
-- the finest usable partition order
|
||||
local p = maxP
|
||||
while p > 0 and (n % POW[p + 1] ~= 0 or n // POW[p + 1] <= order) do
|
||||
p -= 1
|
||||
end
|
||||
local sums, counts = sc.sums, sc.counts
|
||||
local parts = POW[p + 1]
|
||||
local ps = n // parts
|
||||
-- sums of zig-zag values at the finest order
|
||||
for j = 0, parts - 1 do
|
||||
local s = 0
|
||||
local a = if j == 0 then order + 1 else j * ps + 1
|
||||
local e = (j + 1) * ps
|
||||
for i = a, e do
|
||||
local r = res[i]
|
||||
s += if r >= 0 then 2 * r else -2 * r - 1
|
||||
end
|
||||
sums[j + 1] = s
|
||||
counts[j + 1] = e - a + 1
|
||||
end
|
||||
local bestBits, bestP, bestMethod = math.huge, 0, 0
|
||||
local ks, bestKs = sc.ks, sc.bestKs
|
||||
local q = p
|
||||
while true do
|
||||
local np = POW[q + 1]
|
||||
local total = 0
|
||||
local maxk = 0
|
||||
for j = 1, np do
|
||||
local s, c = sums[j], counts[j]
|
||||
local k = 0
|
||||
local cost = s + c -- k = 0: unary s, plus one stop bit per sample
|
||||
if c > 0 and s > c then
|
||||
local k0 = floor(log(s / c) / 0.6931471805599453)
|
||||
for kk = math.max(0, k0 - 1), math.min(30, k0 + 1) do
|
||||
local cc = c * (kk + 1) + floor(s / POW[kk + 1])
|
||||
if cc < cost then
|
||||
cost, k = cc, kk
|
||||
end
|
||||
end
|
||||
end
|
||||
ks[j] = k
|
||||
if k > maxk then
|
||||
maxk = k
|
||||
end
|
||||
total += cost
|
||||
end
|
||||
local method = if maxk > 14 then 1 else 0
|
||||
total += np * (if method == 1 then 5 else 4) + 2 + 4
|
||||
if total < bestBits then
|
||||
bestBits, bestP, bestMethod = total, q, method
|
||||
for j = 1, np do
|
||||
bestKs[j] = ks[j]
|
||||
end
|
||||
end
|
||||
if q == 0 then
|
||||
break
|
||||
end
|
||||
-- merge pairs into the next coarser order
|
||||
for j = 1, np // 2 do
|
||||
sums[j] = sums[2 * j - 1] + sums[2 * j]
|
||||
counts[j] = counts[2 * j - 1] + counts[2 * j]
|
||||
end
|
||||
q -= 1
|
||||
end
|
||||
return bestBits, bestP, bestMethod
|
||||
end
|
||||
|
||||
local function keepRice(sc: Scratch, plan: Plan, porder: number, method: number)
|
||||
plan.porder, plan.method = porder, method
|
||||
local params = plan.params
|
||||
for j = 1, POW[porder + 1] do
|
||||
params[j] = sc.bestKs[j]
|
||||
end
|
||||
end
|
||||
|
||||
-- fixed predictor residuals
|
||||
local function fixedResidual(x: { number }, n: number, order: number, res: { number })
|
||||
for i = 1, order do
|
||||
res[i] = 0
|
||||
end
|
||||
if order == 0 then
|
||||
for i = 1, n do
|
||||
res[i] = x[i]
|
||||
end
|
||||
elseif order == 1 then
|
||||
for i = 2, n do
|
||||
res[i] = x[i] - x[i - 1]
|
||||
end
|
||||
elseif order == 2 then
|
||||
for i = 3, n do
|
||||
res[i] = x[i] - 2 * x[i - 1] + x[i - 2]
|
||||
end
|
||||
elseif order == 3 then
|
||||
for i = 4, n do
|
||||
res[i] = x[i] - 3 * x[i - 1] + 3 * x[i - 2] - x[i - 3]
|
||||
end
|
||||
else
|
||||
for i = 5, n do
|
||||
res[i] = x[i] - 4 * x[i - 1] + 6 * x[i - 2] - 4 * x[i - 3] + x[i - 4]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- the fixed order with the smallest residual magnitude (libFLAC's estimate)
|
||||
local function bestFixedOrder(x: { number }, n: number): (number, number)
|
||||
if n <= 4 then
|
||||
return 0, 0
|
||||
end
|
||||
local s0, s1, s2, s3, s4 = 0, 0, 0, 0, 0
|
||||
local a, b, c, d = x[4], x[4] - x[3], x[4] - 2 * x[3] + x[2], x[4] - 3 * x[3] + 3 * x[2] - x[1]
|
||||
for i = 5, n do
|
||||
local e0 = x[i]
|
||||
local e1 = e0 - a
|
||||
local e2 = e1 - b
|
||||
local e3 = e2 - c
|
||||
local e4 = e3 - d
|
||||
s0 += abs(e0)
|
||||
s1 += abs(e1)
|
||||
s2 += abs(e2)
|
||||
s3 += abs(e3)
|
||||
s4 += abs(e4)
|
||||
a, b, c, d = e0, e1, e2, e3
|
||||
end
|
||||
local best, bs = 0, s0
|
||||
if s1 < bs then
|
||||
best, bs = 1, s1
|
||||
end
|
||||
if s2 < bs then
|
||||
best, bs = 2, s2
|
||||
end
|
||||
if s3 < bs then
|
||||
best, bs = 3, s3
|
||||
end
|
||||
if s4 < bs then
|
||||
best, bs = 4, s4
|
||||
end
|
||||
return best, bs
|
||||
end
|
||||
Encode.bestFixedOrder = bestFixedOrder
|
||||
|
||||
-- libFLAC's default QLP coefficient precision for a block size
|
||||
local function qlpPrecision(bps: number, n: number): number
|
||||
if bps > 16 then
|
||||
return 15
|
||||
end
|
||||
if n <= 192 then
|
||||
return 7
|
||||
elseif n <= 384 then
|
||||
return 8
|
||||
elseif n <= 576 then
|
||||
return 9
|
||||
elseif n <= 1152 then
|
||||
return 10
|
||||
elseif n <= 2304 then
|
||||
return 11
|
||||
elseif n <= 4608 then
|
||||
return 12
|
||||
end
|
||||
return 13
|
||||
end
|
||||
|
||||
-- quantize coefficients lp[1..order] to precision bits: qlp[1..order], shift | nil
|
||||
local function quantize(lp: { number }, order: number, precision: number, qlp: { number }): number?
|
||||
local cmax = 0
|
||||
for i = 1, order do
|
||||
local a = abs(lp[i])
|
||||
if a > cmax then
|
||||
cmax = a
|
||||
end
|
||||
end
|
||||
if cmax <= 0 or cmax ~= cmax or cmax == math.huge then
|
||||
return nil
|
||||
end
|
||||
local log2cmax = 0
|
||||
while cmax >= 1 do
|
||||
cmax /= 2
|
||||
log2cmax += 1
|
||||
end
|
||||
while cmax < 0.5 do
|
||||
cmax *= 2
|
||||
log2cmax -= 1
|
||||
end
|
||||
-- frexp: cmax = m * 2^log2cmax with m in [0.5, 1)
|
||||
log2cmax -= 1
|
||||
local p = precision - 1
|
||||
local qmax = POW[p + 1] - 1
|
||||
local qmin = -POW[p + 1]
|
||||
local shift = p - log2cmax - 1
|
||||
if shift > 15 then
|
||||
shift = 15
|
||||
elseif shift < 0 then
|
||||
return nil
|
||||
end
|
||||
local err = 0
|
||||
local scale = POW[shift + 1]
|
||||
for i = 1, order do
|
||||
err += lp[i] * scale
|
||||
local q = floor(err + 0.5)
|
||||
if q > qmax then
|
||||
q = qmax
|
||||
elseif q < qmin then
|
||||
q = qmin
|
||||
end
|
||||
err -= q
|
||||
qlp[i] = q
|
||||
end
|
||||
return shift
|
||||
end
|
||||
|
||||
local function lpcResidual(x: { number }, n: number, order: number, qlp: { number }, shift: number, res: { number })
|
||||
local d = POW[shift + 1]
|
||||
for i = 1, order do
|
||||
res[i] = 0
|
||||
end
|
||||
for i = order + 1, n do
|
||||
local s = 0
|
||||
for j = 1, order do
|
||||
s += qlp[j] * x[i - j]
|
||||
end
|
||||
res[i] = x[i] - floor(s / d)
|
||||
end
|
||||
end
|
||||
|
||||
-- Levinson-Durbin: sc.lpc[o][1..o] and sc.err[o] for o = 1 .. maxOrder; returns the highest usable order
|
||||
local function levinson(sc: Scratch, maxOrder: number): number
|
||||
local r = sc.autoc
|
||||
local e = r[1]
|
||||
if e <= 0 then
|
||||
return 0
|
||||
end
|
||||
local a = table.create(maxOrder + 1, 0)
|
||||
local tmp = table.create(maxOrder + 1, 0)
|
||||
for o = 1, maxOrder do
|
||||
local acc = r[o + 1]
|
||||
for j = 1, o - 1 do
|
||||
acc -= a[j] * r[o - j + 1]
|
||||
end
|
||||
local k = acc / e
|
||||
for j = 1, o - 1 do
|
||||
tmp[j] = a[j] - k * a[o - j]
|
||||
end
|
||||
for j = 1, o - 1 do
|
||||
a[j] = tmp[j]
|
||||
end
|
||||
a[o] = k
|
||||
e *= (1 - k * k)
|
||||
local row = sc.lpc[o]
|
||||
for j = 1, o do
|
||||
row[j] = a[j]
|
||||
end
|
||||
sc.err[o] = e
|
||||
if e <= 0 then
|
||||
return o
|
||||
end
|
||||
end
|
||||
return maxOrder
|
||||
end
|
||||
|
||||
-- choose the subframe for signal x (n samples, bps bits) into plan (reusing its arrays)
|
||||
local function analyze(sc: Scratch, cfg: Config, src: { number }, n: number, bps: number, plan: Plan)
|
||||
local x = plan.x
|
||||
-- constant?
|
||||
local first = src[1]
|
||||
local constant = true
|
||||
for i = 1, n do
|
||||
local v = src[i]
|
||||
if v ~= first then
|
||||
constant = false
|
||||
end
|
||||
end
|
||||
if constant then
|
||||
plan.kind, plan.bits, plan.wasted, plan.bps = 0, 8 + bps, 0, bps
|
||||
plan.res[1] = first
|
||||
return
|
||||
end
|
||||
-- wasted bits: the common trailing zeros
|
||||
local wasted = 0
|
||||
do
|
||||
local w = 0
|
||||
while w < bps - 1 do
|
||||
local m = POW[w + 2]
|
||||
local all = true
|
||||
for i = 1, n do
|
||||
if src[i] % m ~= 0 then
|
||||
all = false
|
||||
break
|
||||
end
|
||||
end
|
||||
if not all then
|
||||
break
|
||||
end
|
||||
w += 1
|
||||
end
|
||||
wasted = w
|
||||
end
|
||||
local d = POW[wasted + 1]
|
||||
for i = 1, n do
|
||||
x[i] = src[i] / d
|
||||
end
|
||||
local b = bps - wasted
|
||||
local hdr = 8 + wasted
|
||||
plan.wasted, plan.bps = wasted, b
|
||||
-- verbatim
|
||||
plan.kind, plan.bits = 1, hdr + n * b
|
||||
-- fixed
|
||||
local fo = bestFixedOrder(x, n)
|
||||
if n > fo then
|
||||
local tmp = sc.tmpRes
|
||||
fixedResidual(x, n, fo, tmp)
|
||||
local rb, po, me = rice(sc, tmp, n, fo, cfg.maxPartitionOrder)
|
||||
local bits = hdr + fo * b + rb
|
||||
if bits < plan.bits then
|
||||
plan.kind, plan.bits, plan.order = 2, bits, fo
|
||||
sc.tmpRes, plan.res = plan.res, tmp
|
||||
keepRice(sc, plan, po, me)
|
||||
end
|
||||
end
|
||||
-- lpc
|
||||
local maxOrder = math.min(cfg.maxLpcOrder, n - 1, 32)
|
||||
if maxOrder > 0 then
|
||||
-- windowed autocorrelation
|
||||
local win = sc.win
|
||||
if sc.winN ~= n then
|
||||
-- tukey(0.5)
|
||||
local np = floor(0.25 * n)
|
||||
for i = 1, n do
|
||||
win[i] = 1
|
||||
end
|
||||
if np > 0 then
|
||||
for i = 0, np - 1 do
|
||||
local v = 0.5 - 0.5 * math.cos(math.pi * i / np)
|
||||
win[i + 1] = v
|
||||
win[n - i] = v
|
||||
end
|
||||
end
|
||||
sc.winN = n
|
||||
end
|
||||
local wx = sc.wx
|
||||
for i = 1, n do
|
||||
wx[i] = x[i] * win[i]
|
||||
end
|
||||
local ac = sc.autoc
|
||||
for lag = 0, maxOrder do
|
||||
local s = 0
|
||||
for i = lag + 1, n do
|
||||
s += wx[i] * wx[i - lag]
|
||||
end
|
||||
ac[lag + 1] = s
|
||||
end
|
||||
local top = levinson(sc, maxOrder)
|
||||
if top > 0 then
|
||||
local precision = qlpPrecision(b, n)
|
||||
local orders = {}
|
||||
if cfg.exhaustiveOrder then
|
||||
for o = 1, top do
|
||||
table.insert(orders, o)
|
||||
end
|
||||
else
|
||||
-- estimated bits: (n - o) * 0.5 * log2(err_o / n) + o * precision
|
||||
local bestO, bestE = 1, math.huge
|
||||
for o = 1, top do
|
||||
local e = sc.err[o]
|
||||
local est = if e > 0 then (n - o) * 0.5 * log(e / n) / 0.6931471805599453 + o * (precision + b) else -math.huge
|
||||
if est < bestE then
|
||||
bestO, bestE = o, est
|
||||
end
|
||||
end
|
||||
orders[1] = bestO
|
||||
end
|
||||
local qlp = sc.qlp
|
||||
for _, o in orders do
|
||||
local prec = precision
|
||||
local shift = quantize(sc.lpc[o], o, prec, qlp)
|
||||
if shift then
|
||||
local tmp = sc.tmpRes
|
||||
lpcResidual(x, n, o, qlp, shift, tmp)
|
||||
local rb, po, me = rice(sc, tmp, n, o, cfg.maxPartitionOrder)
|
||||
local bits = hdr + o * b + 4 + 5 + o * prec + rb
|
||||
if bits < plan.bits then
|
||||
plan.kind, plan.bits, plan.order = 3, bits, o
|
||||
plan.shift, plan.precision = shift, prec
|
||||
for j = 1, o do
|
||||
plan.qlp[j] = qlp[j]
|
||||
end
|
||||
sc.tmpRes, plan.res = plan.res, tmp
|
||||
keepRice(sc, plan, po, me)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ writing
|
||||
local function writeResidual(w: W, plan: Plan, n: number)
|
||||
local method = plan.method
|
||||
put(w, method, 2)
|
||||
local porder = plan.porder
|
||||
put(w, porder, 4)
|
||||
local parts = POW[porder + 1]
|
||||
local ps = n // parts
|
||||
local res = plan.res
|
||||
local pbits = if method == 1 then 5 else 4
|
||||
for j = 0, parts - 1 do
|
||||
local k = plan.params[j + 1]
|
||||
put(w, k, pbits)
|
||||
local a = if j == 0 then plan.order + 1 else j * ps + 1
|
||||
local e = (j + 1) * ps
|
||||
local mk = POW[k + 1]
|
||||
for i = a, e do
|
||||
local r = res[i]
|
||||
local u = if r >= 0 then 2 * r else -2 * r - 1
|
||||
local hi = floor(u / mk)
|
||||
local lo = u - hi * mk
|
||||
if hi + 1 + k <= 32 then
|
||||
put(w, mk + lo, hi + 1 + k)
|
||||
else
|
||||
local q = hi
|
||||
while q > 24 do
|
||||
put(w, 0, 24)
|
||||
q -= 24
|
||||
end
|
||||
put(w, 1, q + 1)
|
||||
put(w, lo, k)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local function writeSubframe(w: W, plan: Plan, n: number)
|
||||
local kind = plan.kind
|
||||
local typ = if kind == 0 then 0 elseif kind == 1 then 1 elseif kind == 2 then 8 + plan.order else 32 + plan.order - 1
|
||||
put(w, 0, 1)
|
||||
put(w, typ, 6)
|
||||
if kind ~= 0 and plan.wasted > 0 then
|
||||
put(w, 1, 1)
|
||||
put(w, 1, plan.wasted) -- k - 1 zeros, then a one
|
||||
else
|
||||
put(w, 0, 1)
|
||||
end
|
||||
local b = plan.bps
|
||||
if kind == 0 then
|
||||
putSigned(w, plan.res[1], b)
|
||||
return
|
||||
end
|
||||
local x = plan.x
|
||||
if kind == 1 then
|
||||
for i = 1, n do
|
||||
putSigned(w, x[i], b)
|
||||
end
|
||||
return
|
||||
end
|
||||
for i = 1, plan.order do
|
||||
putSigned(w, x[i], b)
|
||||
end
|
||||
if kind == 3 then
|
||||
put(w, plan.precision - 1, 4)
|
||||
put(w, plan.shift, 5)
|
||||
for j = 1, plan.order do
|
||||
putSigned(w, plan.qlp[j], plan.precision)
|
||||
end
|
||||
end
|
||||
writeResidual(w, plan, n)
|
||||
end
|
||||
|
||||
local RATE_CODES: { [number]: number } = {
|
||||
[88200] = 1,
|
||||
[176400] = 2,
|
||||
[192000] = 3,
|
||||
[8000] = 4,
|
||||
[16000] = 5,
|
||||
[22050] = 6,
|
||||
[24000] = 7,
|
||||
[32000] = 8,
|
||||
[44100] = 9,
|
||||
[48000] = 10,
|
||||
[96000] = 11,
|
||||
}
|
||||
local SIZE_CODES: { [number]: number } = { [8] = 1, [12] = 2, [16] = 4, [20] = 5, [24] = 6, [32] = 7 }
|
||||
|
||||
local function blockCode(n: number): (number, number) -- code, extra bits (0, 8, 16)
|
||||
if n == 192 then
|
||||
return 1, 0
|
||||
end
|
||||
for k = 2, 5 do
|
||||
if n == 576 * POW[k - 1] then
|
||||
return k, 0
|
||||
end
|
||||
end
|
||||
for k = 8, 15 do
|
||||
if n == 256 * POW[k - 7] then
|
||||
return k, 0
|
||||
end
|
||||
end
|
||||
if n <= 256 then
|
||||
return 6, 8
|
||||
end
|
||||
return 7, 16
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ encoder
|
||||
export type Source = {
|
||||
sampleRate: number,
|
||||
channels: number,
|
||||
frames: number,
|
||||
tags: { [string]: string },
|
||||
read: (self: any, n: number, out: buffer?) -> (buffer, number),
|
||||
err: any,
|
||||
}
|
||||
export type Options = {
|
||||
level: number,
|
||||
bits: number,
|
||||
blockSize: number?,
|
||||
seekInterval: number, -- seconds between seek points (0 = no SEEKTABLE)
|
||||
padding: number, -- bytes of PADDING
|
||||
vendor: string,
|
||||
}
|
||||
|
||||
-- encode(src, opts) -> buffer | nil, message
|
||||
function Encode.encode(src: Source, opts: Options): (buffer?, string?)
|
||||
local cfg = PRESETS[opts.level + 1]
|
||||
local ch, rate, bps = src.channels, src.sampleRate, opts.bits
|
||||
local bs = opts.blockSize or cfg.blockSize
|
||||
local w: W = { buf = buffer.create(65536), pos = 0, acc = 0, cnt = 0 }
|
||||
-- metadata
|
||||
put(w, 0x664C6143, 32) -- "fLaC"
|
||||
put(w, 0, 1)
|
||||
put(w, 0, 7)
|
||||
put(w, 34, 24)
|
||||
local siData = w.pos
|
||||
for _ = 1, 34 do
|
||||
put(w, 0, 8)
|
||||
end
|
||||
-- seek table placeholders (when the length is known)
|
||||
local seekPoints = 0
|
||||
local seekAt = 0
|
||||
local interval = floor(opts.seekInterval * rate)
|
||||
if interval > 0 and src.frames > 0 then
|
||||
seekPoints = (src.frames - 1) // interval + 1
|
||||
if seekPoints > 65535 // 18 * 18 then
|
||||
seekPoints = 0
|
||||
end
|
||||
end
|
||||
-- vorbis comment
|
||||
local comments = VorbisComment.fields(src.tags)
|
||||
local vendor = opts.vendor
|
||||
local vcLen = 4 + #vendor + 4
|
||||
for _, c in comments do
|
||||
vcLen += 4 + #c
|
||||
end
|
||||
-- SEEKTABLE
|
||||
if seekPoints > 0 then
|
||||
put(w, 0, 1)
|
||||
put(w, 3, 7)
|
||||
put(w, seekPoints * 18, 24)
|
||||
seekAt = w.pos
|
||||
ensure(w, seekPoints * 18)
|
||||
for _ = 1, seekPoints do
|
||||
put(w, 0xFFFFFFFF, 32) -- sample number: a placeholder until a frame fills it
|
||||
put(w, 0xFFFFFFFF, 32)
|
||||
put(w, 0, 32) -- byte offset
|
||||
put(w, 0, 32)
|
||||
put(w, 0, 16) -- frame samples
|
||||
end
|
||||
end
|
||||
-- VORBIS_COMMENT (little-endian lengths)
|
||||
local function le32(v: number)
|
||||
put(w, v % 256, 8)
|
||||
put(w, (v // 256) % 256, 8)
|
||||
put(w, (v // 65536) % 256, 8)
|
||||
put(w, v // 16777216, 8)
|
||||
end
|
||||
ensure(w, vcLen + 8)
|
||||
put(w, if opts.padding > 0 then 0 else 1, 1) -- the last metadata block unless PADDING follows
|
||||
put(w, 4, 7)
|
||||
put(w, vcLen, 24)
|
||||
le32(#vendor)
|
||||
for i = 1, #vendor do
|
||||
put(w, string.byte(vendor, i), 8)
|
||||
end
|
||||
le32(#comments)
|
||||
for _, c in comments do
|
||||
ensure(w, #c + 8)
|
||||
le32(#c)
|
||||
for i = 1, #c do
|
||||
put(w, string.byte(c, i), 8)
|
||||
end
|
||||
end
|
||||
if opts.padding > 0 then
|
||||
ensure(w, opts.padding + 8)
|
||||
put(w, 1, 1)
|
||||
put(w, 1, 7)
|
||||
put(w, opts.padding, 24)
|
||||
for _ = 1, opts.padding do
|
||||
put(w, 0, 8)
|
||||
end
|
||||
end
|
||||
local audioStart = w.pos
|
||||
-- frames
|
||||
local md5 = Md5.new()
|
||||
local mbytes = (bps + 7) // 8
|
||||
local mbuf = buffer.create(bs * ch * mbytes)
|
||||
local scale = POW[bps]
|
||||
local maxv, minv = scale - 1, -scale
|
||||
local X: { { number } } = table.create(ch) :: any
|
||||
for c = 1, ch do
|
||||
X[c] = table.create(bs, 0)
|
||||
end
|
||||
local side = table.create(bs, 0)
|
||||
local mid = table.create(bs, 0)
|
||||
local plans = {}
|
||||
for i = 1, 4 do
|
||||
plans[i] = newPlan(bs)
|
||||
end
|
||||
for c = 1, ch do
|
||||
plans[4 + c] = newPlan(bs)
|
||||
end
|
||||
local lpcRows = {}
|
||||
for o = 1, 32 do
|
||||
lpcRows[o] = table.create(o, 0)
|
||||
end
|
||||
local sc: Scratch = {
|
||||
sums = table.create(64, 0),
|
||||
counts = table.create(64, 0),
|
||||
ks = table.create(64, 0),
|
||||
bestKs = table.create(64, 0),
|
||||
tmpRes = table.create(bs, 0),
|
||||
win = table.create(bs, 0),
|
||||
winN = 0,
|
||||
wx = table.create(bs, 0),
|
||||
autoc = table.create(33, 0),
|
||||
lpc = lpcRows,
|
||||
err = table.create(32, 0),
|
||||
qlp = table.create(32, 0),
|
||||
}
|
||||
local frameNo = 0
|
||||
local total = 0
|
||||
local minFrame, maxFrame = math.huge, 0
|
||||
local seekNext = 0
|
||||
local seekIdx = 0
|
||||
local rateCode = RATE_CODES[rate] or 0
|
||||
local sizeCode = SIZE_CODES[bps] or 0
|
||||
local tmp: buffer? = nil
|
||||
local pendingBuf: buffer? = nil
|
||||
local pendingN, pendingOff = 0, 0
|
||||
-- read exactly bs frames (fewer only at the end)
|
||||
local function readBlock(): (buffer?, number)
|
||||
local out = buffer.create(bs * ch * 4)
|
||||
local got = 0
|
||||
while got < bs do
|
||||
if pendingN == 0 then
|
||||
local b, n = src:read(math.max(bs, 4096), tmp)
|
||||
if n <= 0 then
|
||||
break
|
||||
end
|
||||
tmp = b
|
||||
pendingBuf, pendingN, pendingOff = b, n, 0
|
||||
end
|
||||
local take = math.min(bs - got, pendingN)
|
||||
buffer.copy(out, got * ch * 4, pendingBuf :: buffer, pendingOff * ch * 4, take * ch * 4)
|
||||
got += take
|
||||
pendingN -= take
|
||||
pendingOff += take
|
||||
end
|
||||
return out, got
|
||||
end
|
||||
while true do
|
||||
local fb, n = readBlock()
|
||||
if n <= 0 or fb == nil then
|
||||
break
|
||||
end
|
||||
-- to integers (+ the MD5 bytes, signed little endian)
|
||||
local mo = 0
|
||||
for i = 0, n - 1 do
|
||||
for c = 1, ch do
|
||||
local v = floor(readf32(fb, (i * ch + c - 1) * 4) * scale + 0.5)
|
||||
if v ~= v then
|
||||
v = 0
|
||||
elseif v > maxv then
|
||||
v = maxv
|
||||
elseif v < minv then
|
||||
v = minv
|
||||
end
|
||||
X[c][i + 1] = v
|
||||
local u = if v < 0 then v + 4294967296 else v
|
||||
for k = 0, mbytes - 1 do
|
||||
writeu8(mbuf, mo, floor(u / POW[8 * k + 1]) % 256)
|
||||
mo += 1
|
||||
end
|
||||
end
|
||||
end
|
||||
md5:update(mbuf, 0, mo)
|
||||
-- channel decorrelation
|
||||
local assign = ch - 1
|
||||
local chosen: { Plan } = {}
|
||||
if ch == 2 and cfg.stereo ~= "independent" then
|
||||
local L, R = X[1], X[2]
|
||||
for i = 1, n do
|
||||
local l, r = L[i], R[i]
|
||||
side[i] = l - r
|
||||
mid[i] = floor((l + r) / 2)
|
||||
end
|
||||
local pl, pr, pm, ps = plans[1], plans[2], plans[3], plans[4]
|
||||
if cfg.stereo == "adaptive" then
|
||||
local _, el = bestFixedOrder(L, n)
|
||||
local _, er = bestFixedOrder(R, n)
|
||||
local _, em = bestFixedOrder(mid, n)
|
||||
local _, es = bestFixedOrder(side, n)
|
||||
-- the estimate picks one decorrelated pair; it is kept only if it beats left / right for real
|
||||
local best = math.min(el + es, er + es, em + es)
|
||||
analyze(sc, cfg, L, n, bps, pl)
|
||||
analyze(sc, cfg, R, n, bps, pr)
|
||||
chosen, assign = { pl, pr }, 1
|
||||
if best < el + er then
|
||||
analyze(sc, cfg, side, n, bps + 1, ps)
|
||||
if best == em + es then
|
||||
analyze(sc, cfg, mid, n, bps, pm)
|
||||
if pm.bits + ps.bits < pl.bits + pr.bits then
|
||||
chosen, assign = { pm, ps }, 10
|
||||
end
|
||||
elseif best == el + es then
|
||||
if pl.bits + ps.bits < pl.bits + pr.bits then
|
||||
chosen, assign = { pl, ps }, 8
|
||||
end
|
||||
elseif ps.bits + pr.bits < pl.bits + pr.bits then
|
||||
chosen, assign = { ps, pr }, 9
|
||||
end
|
||||
end
|
||||
else
|
||||
analyze(sc, cfg, L, n, bps, pl)
|
||||
analyze(sc, cfg, R, n, bps, pr)
|
||||
analyze(sc, cfg, mid, n, bps, pm)
|
||||
analyze(sc, cfg, side, n, bps + 1, ps)
|
||||
local lr, ls, rs, msb = pl.bits + pr.bits, pl.bits + ps.bits, ps.bits + pr.bits, pm.bits + ps.bits
|
||||
local best = math.min(lr, ls, rs, msb)
|
||||
if best == lr then
|
||||
chosen, assign = { pl, pr }, 1
|
||||
elseif best == msb then
|
||||
chosen, assign = { pm, ps }, 10
|
||||
elseif best == ls then
|
||||
chosen, assign = { pl, ps }, 8
|
||||
else
|
||||
chosen, assign = { ps, pr }, 9
|
||||
end
|
||||
end
|
||||
else
|
||||
for c = 1, ch do
|
||||
analyze(sc, cfg, X[c], n, bps, plans[4 + c])
|
||||
chosen[c] = plans[4 + c]
|
||||
end
|
||||
end
|
||||
-- the frame
|
||||
local need = n * ch * 4 + 64
|
||||
for _, p in chosen do
|
||||
need += (p.bits // 8) + 8
|
||||
end
|
||||
ensure(w, need)
|
||||
local start = w.pos
|
||||
-- seek points: the first frame at or after each multiple of the interval
|
||||
while seekIdx < seekPoints and total >= seekNext do
|
||||
local at = seekAt + seekIdx * 18
|
||||
local sn, off = total, start - audioStart
|
||||
local tb = w.buf
|
||||
for k = 0, 7 do
|
||||
writeu8(tb, at + k, floor(sn / POW[8 * (7 - k) + 1]) % 256)
|
||||
writeu8(tb, at + 8 + k, floor(off / POW[8 * (7 - k) + 1]) % 256)
|
||||
end
|
||||
writeu8(tb, at + 16, n // 256)
|
||||
writeu8(tb, at + 17, n % 256)
|
||||
seekIdx += 1
|
||||
seekNext = seekIdx * interval
|
||||
end
|
||||
put(w, 0x3FFE, 14)
|
||||
put(w, 0, 1)
|
||||
put(w, 0, 1) -- fixed block size
|
||||
local bcode, bextra = blockCode(n)
|
||||
put(w, bcode, 4)
|
||||
put(w, rateCode, 4)
|
||||
put(w, assign, 4)
|
||||
put(w, sizeCode, 3)
|
||||
put(w, 0, 1)
|
||||
putUtf8(w, frameNo)
|
||||
if bextra > 0 then
|
||||
put(w, n - 1, bextra)
|
||||
end
|
||||
put(w, crc8(w.buf, start, w.pos - start), 8)
|
||||
for _, p in chosen do
|
||||
writeSubframe(w, p, n)
|
||||
end
|
||||
alignByte(w)
|
||||
local c16 = crc16(w.buf, start, w.pos - start)
|
||||
put(w, c16, 16)
|
||||
local fsize = w.pos - start
|
||||
if fsize < minFrame then
|
||||
minFrame = fsize
|
||||
end
|
||||
if fsize > maxFrame then
|
||||
maxFrame = fsize
|
||||
end
|
||||
frameNo += 1
|
||||
total += n
|
||||
if n < bs then
|
||||
break
|
||||
end
|
||||
end
|
||||
if src.err then
|
||||
return nil, tostring(src.err)
|
||||
end
|
||||
-- unused seek points stay placeholders; STREAMINFO
|
||||
local out = buffer.create(w.pos)
|
||||
buffer.copy(out, 0, w.buf, 0, w.pos)
|
||||
local si = Encode.streaminfo(bs, if frameNo > 0 then minFrame else 0, maxFrame, rate, ch, bps, total, md5:digest())
|
||||
buffer.copy(out, siData, si, 0, 34)
|
||||
return out, nil
|
||||
end
|
||||
|
||||
-- streaminfo(...) -> the 34-byte STREAMINFO body
|
||||
function Encode.streaminfo(bs: number, minFrame: number, maxFrame: number, rate: number, ch: number, bps: number, total: number, md5: string): buffer
|
||||
local w: W = { buf = buffer.create(64), pos = 0, acc = 0, cnt = 0 }
|
||||
put(w, bs, 16)
|
||||
put(w, bs, 16)
|
||||
put(w, minFrame, 24)
|
||||
put(w, maxFrame, 24)
|
||||
put(w, rate, 20)
|
||||
put(w, ch - 1, 3)
|
||||
put(w, bps - 1, 5)
|
||||
put(w, floor(total / 2 ^ 32), 4)
|
||||
put(w, total % 2 ^ 32, 32)
|
||||
for i = 1, 16 do
|
||||
put(w, string.byte(md5, i), 8)
|
||||
end
|
||||
local out = buffer.create(34)
|
||||
buffer.copy(out, 0, w.buf, 0, 34)
|
||||
return out
|
||||
end
|
||||
|
||||
return Encode
|
||||
@@ -0,0 +1,610 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- codecs/audio/Flac/Frame: FLAC frames: header parsing (CRC-8), the sync search, subframes (constant, verbatim, fixed, LPC, wasted bits),
|
||||
-- residuals (Rice / Rice2 partitions, escapes), stereo decorrelation and the CRC-16 check.
|
||||
--
|
||||
-- The input buffer must have at least PAD zero bytes after the data end (`len`): the bit reader reads 32-bit
|
||||
-- words without bounds checks and instead compares its position with the data end once per sample. Nothing here
|
||||
-- throws: bad data gives nil, err.
|
||||
-- Absorbed from flac-luau 7083d0d (same author, no third-party code).
|
||||
local readu8, readu32 = buffer.readu8, buffer.readu32
|
||||
local band, bxor, lshift, rshift, countlz, byteswap = bit32.band, bit32.bxor, bit32.lshift, bit32.rshift,
|
||||
bit32.countlz, bit32.byteswap
|
||||
local floor = math.floor
|
||||
|
||||
local Frame = {}
|
||||
Frame.PAD = 32
|
||||
|
||||
-- CRC-8 (poly 0x07) and CRC-16 (poly 0x8005), MSB first, init 0
|
||||
local CRC8 = table.create(256, 0)
|
||||
local CRC16 = table.create(256, 0)
|
||||
for i = 0, 255 do
|
||||
local c = i
|
||||
for _ = 1, 8 do
|
||||
c = if band(c, 0x80) ~= 0 then band(bxor(lshift(c, 1), 0x07), 0xFF) else band(lshift(c, 1), 0xFF)
|
||||
end
|
||||
CRC8[i + 1] = c
|
||||
local d = lshift(i, 8)
|
||||
for _ = 1, 8 do
|
||||
d = if band(d, 0x8000) ~= 0 then band(bxor(lshift(d, 1), 0x8005), 0xFFFF) else band(lshift(d, 1), 0xFFFF)
|
||||
end
|
||||
CRC16[i + 1] = d
|
||||
end
|
||||
|
||||
local function crc16(b: buffer, from: number, to: number): number
|
||||
local c = 0
|
||||
for i = from, to - 1 do
|
||||
c = bxor(band(lshift(c, 8), 0xFFFF), CRC16[bxor(rshift(c, 8), readu8(b, i)) + 1])
|
||||
end
|
||||
return c
|
||||
end
|
||||
Frame.crc16 = crc16
|
||||
|
||||
local function crc8(b: buffer, from: number, to: number): number
|
||||
local c = 0
|
||||
for i = from, to - 1 do
|
||||
c = CRC8[bxor(c, readu8(b, i)) + 1]
|
||||
end
|
||||
return c
|
||||
end
|
||||
Frame.crc8 = crc8
|
||||
|
||||
local POW2 = table.create(64, 0)
|
||||
for i = 0, 63 do
|
||||
POW2[i + 1] = 2 ^ i
|
||||
end
|
||||
|
||||
local BLOCK = { [1] = 192, [2] = 576, [3] = 1152, [4] = 2304, [5] = 4608, [8] = 256, [9] = 512, [10] = 1024,
|
||||
[11] = 2048, [12] = 4096, [13] = 8192, [14] = 16384, [15] = 32768 }
|
||||
local RATE = { [1] = 88200, [2] = 176400, [3] = 192000, [4] = 8000, [5] = 16000, [6] = 22050, [7] = 24000,
|
||||
[8] = 32000, [9] = 44100, [10] = 48000, [11] = 96000 }
|
||||
local BITS = { [1] = 8, [2] = 12, [4] = 16, [5] = 20, [6] = 24, [7] = 32 }
|
||||
|
||||
export type Header = {
|
||||
offset: number, -- byte offset of the sync code
|
||||
size: number, -- header bytes, CRC-8 included
|
||||
blockSize: number,
|
||||
sampleRate: number,
|
||||
channels: number,
|
||||
assignment: number, -- 0..7 independent, 8 left/side, 9 side/right, 10 mid/side
|
||||
bps: number,
|
||||
variable: boolean, -- variable block size: number is a sample number, else a frame number
|
||||
number: number,
|
||||
}
|
||||
|
||||
-- Parse the frame header at pos (data ends at len; the buffer is padded). si gives the STREAMINFO defaults
|
||||
-- (sampleRate, bitsPerSample) for the "from STREAMINFO" codes. Returns nil when it is not a valid header.
|
||||
function Frame.header(b: buffer, pos: number, len: number, si: any): Header?
|
||||
if pos < 0 or pos + 6 > len then
|
||||
return nil
|
||||
end
|
||||
if readu8(b, pos) ~= 0xFF then
|
||||
return nil
|
||||
end
|
||||
local b1 = readu8(b, pos + 1)
|
||||
if band(b1, 0xFE) ~= 0xF8 then
|
||||
return nil
|
||||
end
|
||||
local b2 = readu8(b, pos + 2)
|
||||
local b3 = readu8(b, pos + 3)
|
||||
local bsCode, rCode = rshift(b2, 4), band(b2, 15)
|
||||
local chCode, sCode = rshift(b3, 4), band(rshift(b3, 1), 7)
|
||||
if bsCode == 0 or rCode == 15 or chCode > 10 or sCode == 3 or band(b3, 1) ~= 0 then
|
||||
return nil
|
||||
end
|
||||
-- UTF-8-like coded number
|
||||
local p = pos + 4
|
||||
local x = readu8(b, p)
|
||||
p += 1
|
||||
local num, more
|
||||
if x < 0x80 then
|
||||
num, more = x, 0
|
||||
elseif x >= 0xC0 and x < 0xE0 then
|
||||
num, more = band(x, 0x1F), 1
|
||||
elseif x >= 0xE0 and x < 0xF0 then
|
||||
num, more = band(x, 0x0F), 2
|
||||
elseif x >= 0xF0 and x < 0xF8 then
|
||||
num, more = band(x, 0x07), 3
|
||||
elseif x >= 0xF8 and x < 0xFC then
|
||||
num, more = band(x, 0x03), 4
|
||||
elseif x >= 0xFC and x < 0xFE then
|
||||
num, more = band(x, 0x01), 5
|
||||
elseif x == 0xFE then
|
||||
num, more = 0, 6
|
||||
else
|
||||
return nil
|
||||
end
|
||||
for _ = 1, more do
|
||||
local c = readu8(b, p)
|
||||
p += 1
|
||||
if band(c, 0xC0) ~= 0x80 then
|
||||
return nil
|
||||
end
|
||||
num = num * 64 + band(c, 0x3F)
|
||||
end
|
||||
local variable = band(b1, 1) == 1
|
||||
if not variable and more > 5 then
|
||||
return nil
|
||||
end
|
||||
local bs = BLOCK[bsCode]
|
||||
if bsCode == 6 then
|
||||
bs = readu8(b, p) + 1
|
||||
p += 1
|
||||
elseif bsCode == 7 then
|
||||
bs = readu8(b, p) * 256 + readu8(b, p + 1) + 1
|
||||
p += 2
|
||||
end
|
||||
local rate = RATE[rCode]
|
||||
if rCode == 0 then
|
||||
rate = si and si.sampleRate or 0
|
||||
elseif rCode == 12 then
|
||||
rate = readu8(b, p) * 1000
|
||||
p += 1
|
||||
elseif rCode == 13 then
|
||||
rate = readu8(b, p) * 256 + readu8(b, p + 1)
|
||||
p += 2
|
||||
elseif rCode == 14 then
|
||||
rate = (readu8(b, p) * 256 + readu8(b, p + 1)) * 10
|
||||
p += 2
|
||||
end
|
||||
local bps = BITS[sCode]
|
||||
if sCode == 0 then
|
||||
bps = si and si.bitsPerSample or 0
|
||||
end
|
||||
if not bs or not bps or bps < 4 or bps > 32 or p + 1 > len then
|
||||
return nil
|
||||
end
|
||||
-- CRC-8 over the header
|
||||
if crc8(b, pos, p) ~= readu8(b, p) then
|
||||
return nil
|
||||
end
|
||||
return {
|
||||
offset = pos,
|
||||
size = p + 1 - pos,
|
||||
blockSize = bs,
|
||||
sampleRate = rate or 0,
|
||||
channels = if chCode < 8 then chCode + 1 else 2,
|
||||
assignment = chCode,
|
||||
bps = bps,
|
||||
variable = variable,
|
||||
number = num,
|
||||
}
|
||||
end
|
||||
|
||||
-- The first valid header at an offset in [pos, limit)
|
||||
function Frame.find(b: buffer, pos: number, limit: number, len: number, si: any): Header?
|
||||
local lim = math.min(limit, len - 6)
|
||||
if pos < 0 then
|
||||
pos = 0
|
||||
end
|
||||
while pos < lim do
|
||||
if readu8(b, pos) == 0xFF then
|
||||
local b1 = readu8(b, pos + 1)
|
||||
if b1 == 0xF8 or b1 == 0xF9 then
|
||||
local h = Frame.header(b, pos, len, si)
|
||||
if h then
|
||||
return h
|
||||
end
|
||||
end
|
||||
end
|
||||
pos += 1
|
||||
end
|
||||
return nil
|
||||
end
|
||||
|
||||
-- Bit reading for the header parts of subframes: n <= 32 bits at bit position p (unsigned)
|
||||
local function bits(b: buffer, p: number, n: number): number
|
||||
if n == 0 then
|
||||
return 0
|
||||
elseif n <= 24 then
|
||||
return rshift(lshift(byteswap(readu32(b, rshift(p, 3))), band(p, 7)), 32 - n)
|
||||
end
|
||||
local hi = rshift(lshift(byteswap(readu32(b, rshift(p, 3))), band(p, 7)), 32 - (n - 16))
|
||||
local q = p + n - 16
|
||||
return hi * 65536 + rshift(lshift(byteswap(readu32(b, rshift(q, 3))), band(q, 7)), 16)
|
||||
end
|
||||
|
||||
local function sbits(b: buffer, p: number, n: number): number
|
||||
local v = bits(b, p, n)
|
||||
if n > 0 and v >= POW2[n] then
|
||||
v -= POW2[n + 1]
|
||||
end
|
||||
return v
|
||||
end
|
||||
|
||||
-- Residual coding: fills s[order + 1 .. n] with residuals. Returns the new bit position, or -1.
|
||||
local function residual(b: buffer, p: number, lim: number, s: { number }, n: number, order: number): number
|
||||
local method = bits(b, p, 2)
|
||||
if method > 1 then
|
||||
return -1
|
||||
end
|
||||
local porder = bits(b, p + 2, 4)
|
||||
p += 6
|
||||
local pbits = if method == 0 then 4 else 5
|
||||
local escape = if method == 0 then 15 else 31
|
||||
local parts = POW2[porder + 1]
|
||||
local psize = rshift(n, porder)
|
||||
if psize * parts ~= n or psize < order then
|
||||
return -1
|
||||
end
|
||||
local i = order + 1
|
||||
for part = 0, parts - 1 do
|
||||
if p > lim then
|
||||
return -1
|
||||
end
|
||||
local k = bits(b, p, pbits)
|
||||
p += pbits
|
||||
local stop = (part + 1) * psize
|
||||
if k == escape then
|
||||
local nb = bits(b, p, 5)
|
||||
p += 5
|
||||
if nb == 0 then
|
||||
for j = i, stop do
|
||||
s[j] = 0
|
||||
end
|
||||
else
|
||||
local half, full = POW2[nb], POW2[nb + 1]
|
||||
for j = i, stop do
|
||||
if p > lim then
|
||||
return -1
|
||||
end
|
||||
local v = bits(b, p, nb)
|
||||
p += nb
|
||||
if v >= half then
|
||||
v -= full
|
||||
end
|
||||
s[j] = v
|
||||
end
|
||||
end
|
||||
elseif k <= 24 then
|
||||
-- the hot loop: unary quotient + k-bit remainder, zigzag
|
||||
local pk = POW2[k + 1]
|
||||
local rk = 32 - k
|
||||
for j = i, stop do
|
||||
if p > lim then
|
||||
return -1
|
||||
end
|
||||
local sh = band(p, 7)
|
||||
local w = lshift(byteswap(readu32(b, rshift(p, 3))), sh)
|
||||
local q, r
|
||||
if w ~= 0 then
|
||||
q = countlz(w)
|
||||
local used = q + 1 + k
|
||||
if used <= 32 - sh then
|
||||
r = if k == 0 then 0 else rshift(lshift(w, q + 1), rk)
|
||||
p += used
|
||||
else
|
||||
p += q + 1
|
||||
r = if k == 0 then 0 else rshift(lshift(byteswap(readu32(b, rshift(p, 3))), band(p, 7)), rk)
|
||||
p += k
|
||||
end
|
||||
else
|
||||
-- a long run of zeros
|
||||
q = 32 - sh
|
||||
p += q
|
||||
while true do
|
||||
if p > lim then
|
||||
return -1
|
||||
end
|
||||
local w2 = lshift(byteswap(readu32(b, rshift(p, 3))), band(p, 7))
|
||||
if w2 ~= 0 then
|
||||
local z = countlz(w2)
|
||||
q += z
|
||||
p += z + 1
|
||||
break
|
||||
end
|
||||
local adv = 32 - band(p, 7)
|
||||
q += adv
|
||||
p += adv
|
||||
end
|
||||
r = if k == 0 then 0 else rshift(lshift(byteswap(readu32(b, rshift(p, 3))), band(p, 7)), rk)
|
||||
p += k
|
||||
end
|
||||
local u = q * pk + r
|
||||
local h = floor(u * 0.5)
|
||||
s[j] = if h + h == u then h else -h - 1
|
||||
end
|
||||
else
|
||||
-- k = 25..30 (Rice2): the remainder takes two reads
|
||||
local pk = POW2[k + 1]
|
||||
for j = i, stop do
|
||||
if p > lim then
|
||||
return -1
|
||||
end
|
||||
local q = 0
|
||||
while true do
|
||||
if p > lim then
|
||||
return -1
|
||||
end
|
||||
local w = lshift(byteswap(readu32(b, rshift(p, 3))), band(p, 7))
|
||||
if w ~= 0 then
|
||||
local z = countlz(w)
|
||||
q += z
|
||||
p += z + 1
|
||||
break
|
||||
end
|
||||
local adv = 32 - band(p, 7)
|
||||
q += adv
|
||||
p += adv
|
||||
end
|
||||
local r = bits(b, p, k)
|
||||
p += k
|
||||
local u = q * pk + r
|
||||
local h = floor(u * 0.5)
|
||||
s[j] = if h + h == u then h else -h - 1
|
||||
end
|
||||
end
|
||||
i = stop + 1
|
||||
end
|
||||
return p
|
||||
end
|
||||
|
||||
local function fixed(s: { number }, n: number, order: number)
|
||||
if order == 1 then
|
||||
local a = s[1]
|
||||
for i = 2, n do
|
||||
a += s[i]
|
||||
s[i] = a
|
||||
end
|
||||
elseif order == 2 then
|
||||
local a, c = s[2], s[1]
|
||||
for i = 3, n do
|
||||
local v = s[i] + 2 * a - c
|
||||
s[i] = v
|
||||
c = a
|
||||
a = v
|
||||
end
|
||||
elseif order == 3 then
|
||||
local a, c, d = s[3], s[2], s[1]
|
||||
for i = 4, n do
|
||||
local v = s[i] + 3 * a - 3 * c + d
|
||||
s[i] = v
|
||||
d = c
|
||||
c = a
|
||||
a = v
|
||||
end
|
||||
elseif order == 4 then
|
||||
local a, c, d, e = s[4], s[3], s[2], s[1]
|
||||
for i = 5, n do
|
||||
local v = s[i] + 4 * a - 6 * c + 4 * d - e
|
||||
s[i] = v
|
||||
e = d
|
||||
d = c
|
||||
c = a
|
||||
a = v
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- LPC restoration: s[i] += floor(sum(coef[j] * s[i - j]) / 2^shift); unrolled for orders up to 12
|
||||
local function lpc(s: { number }, n: number, cf: { number }, order: number, shift: number)
|
||||
local div = POW2[shift + 1]
|
||||
local inv = 1 / div
|
||||
if order <= 4 then
|
||||
local c1, c2, c3, c4 = cf[1], cf[2] or 0, cf[3] or 0, cf[4] or 0
|
||||
local s1, s2, s3, s4 = s[order], s[order - 1] or 0, s[order - 2] or 0, s[order - 3] or 0
|
||||
for i = order + 1, n do
|
||||
local v = s[i] + floor((c1 * s1 + c2 * s2 + c3 * s3 + c4 * s4) * inv)
|
||||
s[i] = v
|
||||
s4 = s3
|
||||
s3 = s2
|
||||
s2 = s1
|
||||
s1 = v
|
||||
end
|
||||
elseif order <= 8 then
|
||||
local c1, c2, c3, c4, c5, c6, c7, c8 = cf[1], cf[2], cf[3], cf[4], cf[5], cf[6] or 0, cf[7] or 0, cf[8] or 0
|
||||
local s1, s2, s3, s4, s5 = s[order], s[order - 1], s[order - 2], s[order - 3], s[order - 4]
|
||||
local s6, s7, s8 = s[order - 5] or 0, s[order - 6] or 0, s[order - 7] or 0
|
||||
for i = order + 1, n do
|
||||
local v = s[i]
|
||||
+ floor((c1 * s1 + c2 * s2 + c3 * s3 + c4 * s4 + c5 * s5 + c6 * s6 + c7 * s7 + c8 * s8) * inv)
|
||||
s[i] = v
|
||||
s8 = s7
|
||||
s7 = s6
|
||||
s6 = s5
|
||||
s5 = s4
|
||||
s4 = s3
|
||||
s3 = s2
|
||||
s2 = s1
|
||||
s1 = v
|
||||
end
|
||||
elseif order <= 12 then
|
||||
local c1, c2, c3, c4, c5, c6, c7, c8 = cf[1], cf[2], cf[3], cf[4], cf[5], cf[6], cf[7], cf[8]
|
||||
local c9, c10, c11, c12 = cf[9], cf[10] or 0, cf[11] or 0, cf[12] or 0
|
||||
local s1, s2, s3, s4, s5, s6 = s[order], s[order - 1], s[order - 2], s[order - 3], s[order - 4], s[order - 5]
|
||||
local s7, s8, s9 = s[order - 6], s[order - 7], s[order - 8]
|
||||
local s10, s11, s12 = s[order - 9] or 0, s[order - 10] or 0, s[order - 11] or 0
|
||||
for i = order + 1, n do
|
||||
local v = s[i]
|
||||
+ floor(
|
||||
(
|
||||
c1 * s1 + c2 * s2 + c3 * s3 + c4 * s4 + c5 * s5 + c6 * s6
|
||||
+ c7 * s7 + c8 * s8 + c9 * s9 + c10 * s10 + c11 * s11 + c12 * s12
|
||||
) * inv
|
||||
)
|
||||
s[i] = v
|
||||
s12 = s11
|
||||
s11 = s10
|
||||
s10 = s9
|
||||
s9 = s8
|
||||
s8 = s7
|
||||
s7 = s6
|
||||
s6 = s5
|
||||
s5 = s4
|
||||
s4 = s3
|
||||
s3 = s2
|
||||
s2 = s1
|
||||
s1 = v
|
||||
end
|
||||
else
|
||||
for i = order + 1, n do
|
||||
local sum = 0
|
||||
for j = 1, order do
|
||||
sum += cf[j] * s[i - j]
|
||||
end
|
||||
s[i] += floor(sum * inv)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- One subframe into s[1..n]. Returns the new bit position, or -1.
|
||||
local function subframe(b: buffer, p: number, lim: number, s: { number }, n: number, bps: number, cf: { number }): number
|
||||
if p > lim then
|
||||
return -1
|
||||
end
|
||||
local hdr = bits(b, p, 8)
|
||||
p += 8
|
||||
if hdr >= 128 then
|
||||
return -1
|
||||
end
|
||||
local typ = rshift(hdr, 1)
|
||||
local wasted = 0
|
||||
if band(hdr, 1) == 1 then
|
||||
-- unary count of wasted bits (minus one)
|
||||
while true do
|
||||
if p > lim or wasted > 32 then
|
||||
return -1
|
||||
end
|
||||
local bit = bits(b, p, 1)
|
||||
p += 1
|
||||
wasted += 1
|
||||
if bit == 1 then
|
||||
break
|
||||
end
|
||||
end
|
||||
bps -= wasted
|
||||
if bps < 1 then
|
||||
return -1
|
||||
end
|
||||
end
|
||||
if typ == 0 then
|
||||
local v = sbits(b, p, bps)
|
||||
p += bps
|
||||
for i = 1, n do
|
||||
s[i] = v
|
||||
end
|
||||
elseif typ == 1 then
|
||||
for i = 1, n do
|
||||
if p > lim then
|
||||
return -1
|
||||
end
|
||||
s[i] = sbits(b, p, bps)
|
||||
p += bps
|
||||
end
|
||||
elseif typ >= 8 and typ <= 12 then
|
||||
local order = typ - 8
|
||||
if order > n then
|
||||
return -1
|
||||
end
|
||||
for i = 1, order do
|
||||
if p > lim then
|
||||
return -1
|
||||
end
|
||||
s[i] = sbits(b, p, bps)
|
||||
p += bps
|
||||
end
|
||||
p = residual(b, p, lim, s, n, order)
|
||||
if p < 0 then
|
||||
return -1
|
||||
end
|
||||
fixed(s, n, order)
|
||||
elseif typ >= 32 then
|
||||
local order = typ - 31
|
||||
if order > n then
|
||||
return -1
|
||||
end
|
||||
for i = 1, order do
|
||||
if p > lim then
|
||||
return -1
|
||||
end
|
||||
s[i] = sbits(b, p, bps)
|
||||
p += bps
|
||||
end
|
||||
local prec = bits(b, p, 4) + 1
|
||||
p += 4
|
||||
if prec == 16 then
|
||||
return -1
|
||||
end
|
||||
local shift = sbits(b, p, 5)
|
||||
p += 5
|
||||
if shift < 0 then
|
||||
return -1
|
||||
end
|
||||
for j = 1, order do
|
||||
if p > lim then
|
||||
return -1
|
||||
end
|
||||
cf[j] = sbits(b, p, prec)
|
||||
p += prec
|
||||
end
|
||||
for j = order + 1, 12 do
|
||||
cf[j] = 0 -- the unrolled filters read up to 12 coefficients
|
||||
end
|
||||
p = residual(b, p, lim, s, n, order)
|
||||
if p < 0 then
|
||||
return -1
|
||||
end
|
||||
lpc(s, n, cf, order, shift)
|
||||
else
|
||||
return -1
|
||||
end
|
||||
if wasted > 0 then
|
||||
local m = POW2[wasted + 1]
|
||||
for i = 1, n do
|
||||
s[i] *= m
|
||||
end
|
||||
end
|
||||
return p
|
||||
end
|
||||
|
||||
-- Decode the frame whose header h was parsed (data ends at len). chans[c] are tables of at least h.blockSize
|
||||
-- numbers; they receive the samples. Returns the byte offset after the frame, or nil, err.
|
||||
function Frame.decode(b: buffer, len: number, h: Header, chans: { { number } }, cf: { number }, checkCrc: boolean): (number?, string?)
|
||||
local n = h.blockSize
|
||||
local lim = len * 8
|
||||
local p = (h.offset + h.size) * 8
|
||||
local asg = h.assignment
|
||||
for c = 1, h.channels do
|
||||
local bps = h.bps
|
||||
if (asg == 8 and c == 2) or (asg == 9 and c == 1) or (asg == 10 and c == 2) then
|
||||
bps += 1 -- the side channel
|
||||
end
|
||||
p = subframe(b, p, lim, chans[c], n, bps, cf)
|
||||
if p < 0 then
|
||||
return nil, "bad subframe"
|
||||
end
|
||||
end
|
||||
local e = rshift(p + 7, 3) -- byte align
|
||||
if e + 2 > len then
|
||||
return nil, "truncated frame"
|
||||
end
|
||||
if checkCrc then
|
||||
local want = readu8(b, e) * 256 + readu8(b, e + 1)
|
||||
if crc16(b, h.offset, e) ~= want then
|
||||
return nil, "crc16 mismatch"
|
||||
end
|
||||
end
|
||||
if asg >= 8 then
|
||||
local l, r = chans[1], chans[2]
|
||||
if asg == 8 then
|
||||
for i = 1, n do
|
||||
r[i] = l[i] - r[i]
|
||||
end
|
||||
elseif asg == 9 then
|
||||
for i = 1, n do
|
||||
l[i] += r[i]
|
||||
end
|
||||
else
|
||||
for i = 1, n do
|
||||
local side = r[i]
|
||||
local m = l[i] * 2 + (side % 2)
|
||||
l[i] = (m + side) * 0.5
|
||||
r[i] = (m - side) * 0.5
|
||||
end
|
||||
end
|
||||
end
|
||||
return e + 2, nil
|
||||
end
|
||||
|
||||
return Frame
|
||||
@@ -0,0 +1,11 @@
|
||||
--!strict
|
||||
-- codecs/audio/Flac: FLAC frames without the container: frame header parsing, subframe decoding (constant, verbatim,
|
||||
-- fixed, LPC, Rice residuals, stereo decorrelation) with CRC-8 / CRC-16 (Frame), and the frame encoder (Encode).
|
||||
-- Absorbed from flac-luau 7083d0d (same author); the encoder is new. Native FLAC / Ogg FLAC streams, metadata and
|
||||
-- seeking are audio/flac.
|
||||
local Flac = {
|
||||
Frame = require("@formats/codecs/audio/Flac/Frame"),
|
||||
Encode = require("@formats/codecs/audio/Flac/Encode"),
|
||||
}
|
||||
|
||||
return Flac
|
||||
@@ -0,0 +1,63 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- codecs/audio/Mpa/Analysis: the MPEG audio polyphase analysis filterbank (ISO/IEC 11172-3 section C.1.3, figure C.4): 32
|
||||
-- input samples -> one sample in each of the 32 subbands, with the standard's 512-tap window C = D / 32 (D from
|
||||
-- codecs/audio/Mpa/EncTables) and the matrixing M[k][i] = cos((2k + 1)(i - 16) pi / 64). Shared by the layer II and layer III
|
||||
-- encoders. New code from the standard's flow chart. Never throws.
|
||||
--
|
||||
-- local st = Analysis.new()
|
||||
-- Analysis.run(st, samples, start, out, outStart) -- samples[start .. start + 31] -> out[outStart .. outStart + 31]
|
||||
local E = require("@formats/codecs/audio/Mpa/EncTables")
|
||||
|
||||
local Analysis = {}
|
||||
|
||||
local C = table.create(512, 0)
|
||||
for i = 1, 512 do
|
||||
C[i] = E.D[i] / 32
|
||||
end
|
||||
local M = table.create(32 * 64, 0) -- M[k * 64 + i + 1]
|
||||
for k = 0, 31 do
|
||||
for i = 0, 63 do
|
||||
M[k * 64 + i + 1] = math.cos((2 * k + 1) * (i - 16) * math.pi / 64)
|
||||
end
|
||||
end
|
||||
|
||||
export type State = { x: { number }, off: number, y: { number } }
|
||||
|
||||
function Analysis.new(): State
|
||||
return { x = table.create(1024, 0), off = 0, y = table.create(64, 0) }
|
||||
end
|
||||
|
||||
-- run(st, s, si, out, oi): s[si .. si + 31] (oldest first) -> out[oi .. oi + 31] (subbands 0 .. 31)
|
||||
function Analysis.run(st: State, s: { number }, si: number, out: { number }, oi: number)
|
||||
-- the window X is kept twice (x[p] and x[p + 512]) so X[i] = x[off + i + 1] without a modulo
|
||||
local x = st.x
|
||||
local off = (st.off - 32) % 512
|
||||
st.off = off
|
||||
for j = 0, 31 do
|
||||
local v = s[si + j]
|
||||
local p = (off + 31 - j) % 512 + 1 -- X[31 - j]: the first new sample goes to X[31], the newest to X[0]
|
||||
x[p] = v
|
||||
x[p + 512] = v
|
||||
end
|
||||
local y = st.y
|
||||
for i = 0, 63 do
|
||||
local acc = 0
|
||||
local b = off + i + 1
|
||||
for j = 0, 7 do
|
||||
acc += C[i + 64 * j + 1] * x[b + 64 * j]
|
||||
end
|
||||
y[i + 1] = acc
|
||||
end
|
||||
for k = 0, 31 do
|
||||
local acc = 0
|
||||
local mb = k * 64
|
||||
for i = 1, 64 do
|
||||
acc += M[mb + i] * y[i]
|
||||
end
|
||||
out[oi + k] = acc
|
||||
end
|
||||
end
|
||||
|
||||
return Analysis
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,292 @@
|
||||
--!strict
|
||||
-- codecs/audio/Mpa/EncTables: tables for the MPEG audio encoders, from PDMP3 (public domain / unlicense, by Krister
|
||||
-- Lagerstrom and contributors: licenses/PDMP3-UNLICENSE.txt), which transcribes ISO/IEC 11172-3: the synthesis window D
|
||||
-- (the analysis window is C = D / 32) and the layer III Huffman code trees (the encoders derive codeword tables from
|
||||
-- them). Trees use the standard's decoder layout: an entry with a high byte is a node (high byte: left offset, low
|
||||
-- byte: right offset), otherwise a leaf (x << 4 | y; count1 tables: v w x y bits).
|
||||
local E = {}
|
||||
E.D = {
|
||||
0.000000000, -0.000015259, -0.000015259, -0.000015259, -0.000015259, -0.000015259, -0.000015259, -0.000030518,
|
||||
-0.000030518, -0.000030518, -0.000030518, -0.000045776, -0.000045776, -0.000061035, -0.000061035, -0.000076294,
|
||||
-0.000076294, -0.000091553, -0.000106812, -0.000106812, -0.000122070, -0.000137329, -0.000152588, -0.000167847,
|
||||
-0.000198364, -0.000213623, -0.000244141, -0.000259399, -0.000289917, -0.000320435, -0.000366211, -0.000396729,
|
||||
-0.000442505, -0.000473022, -0.000534058, -0.000579834, -0.000625610, -0.000686646, -0.000747681, -0.000808716,
|
||||
-0.000885010, -0.000961304, -0.001037598, -0.001113892, -0.001205444, -0.001296997, -0.001388550, -0.001480103,
|
||||
-0.001586914, -0.001693726, -0.001785278, -0.001907349, -0.002014160, -0.002120972, -0.002243042, -0.002349854,
|
||||
-0.002456665, -0.002578735, -0.002685547, -0.002792358, -0.002899170, -0.002990723, -0.003082275, -0.003173828,
|
||||
0.003250122, 0.003326416, 0.003387451, 0.003433228, 0.003463745, 0.003479004, 0.003479004, 0.003463745,
|
||||
0.003417969, 0.003372192, 0.003280640, 0.003173828, 0.003051758, 0.002883911, 0.002700806, 0.002487183,
|
||||
0.002227783, 0.001937866, 0.001617432, 0.001266479, 0.000869751, 0.000442505, -0.000030518, -0.000549316,
|
||||
-0.001098633, -0.001693726, -0.002334595, -0.003005981, -0.003723145, -0.004486084, -0.005294800, -0.006118774,
|
||||
-0.007003784, -0.007919312, -0.008865356, -0.009841919, -0.010848999, -0.011886597, -0.012939453, -0.014022827,
|
||||
-0.015121460, -0.016235352, -0.017349243, -0.018463135, -0.019577026, -0.020690918, -0.021789551, -0.022857666,
|
||||
-0.023910522, -0.024932861, -0.025909424, -0.026840210, -0.027725220, -0.028533936, -0.029281616, -0.029937744,
|
||||
-0.030532837, -0.031005859, -0.031387329, -0.031661987, -0.031814575, -0.031845093, -0.031738281, -0.031478882,
|
||||
0.031082153, 0.030517578, 0.029785156, 0.028884888, 0.027801514, 0.026535034, 0.025085449, 0.023422241,
|
||||
0.021575928, 0.019531250, 0.017257690, 0.014801025, 0.012115479, 0.009231567, 0.006134033, 0.002822876,
|
||||
-0.000686646, -0.004394531, -0.008316040, -0.012420654, -0.016708374, -0.021179199, -0.025817871, -0.030609131,
|
||||
-0.035552979, -0.040634155, -0.045837402, -0.051132202, -0.056533813, -0.061996460, -0.067520142, -0.073059082,
|
||||
-0.078628540, -0.084182739, -0.089706421, -0.095169067, -0.100540161, -0.105819702, -0.110946655, -0.115921021,
|
||||
-0.120697021, -0.125259399, -0.129562378, -0.133590698, -0.137298584, -0.140670776, -0.143676758, -0.146255493,
|
||||
-0.148422241, -0.150115967, -0.151306152, -0.151962280, -0.152069092, -0.151596069, -0.150497437, -0.148773193,
|
||||
-0.146362305, -0.143264771, -0.139450073, -0.134887695, -0.129577637, -0.123474121, -0.116577148, -0.108856201,
|
||||
0.100311279, 0.090927124, 0.080688477, 0.069595337, 0.057617188, 0.044784546, 0.031082153, 0.016510010,
|
||||
0.001068115, -0.015228271, -0.032379150, -0.050354004, -0.069168091, -0.088775635, -0.109161377, -0.130310059,
|
||||
-0.152206421, -0.174789429, -0.198059082, -0.221984863, -0.246505737, -0.271591187, -0.297210693, -0.323318481,
|
||||
-0.349868774, -0.376800537, -0.404083252, -0.431655884, -0.459472656, -0.487472534, -0.515609741, -0.543823242,
|
||||
-0.572036743, -0.600219727, -0.628295898, -0.656219482, -0.683914185, -0.711318970, -0.738372803, -0.765029907,
|
||||
-0.791213989, -0.816864014, -0.841949463, -0.866363525, -0.890090942, -0.913055420, -0.935195923, -0.956481934,
|
||||
-0.976852417, -0.996246338, -1.014617920, -1.031936646, -1.048156738, -1.063217163, -1.077117920, -1.089782715,
|
||||
-1.101211548, -1.111373901, -1.120223999, -1.127746582, -1.133926392, -1.138763428, -1.142211914, -1.144287109,
|
||||
1.144989014, 1.144287109, 1.142211914, 1.138763428, 1.133926392, 1.127746582, 1.120223999, 1.111373901,
|
||||
1.101211548, 1.089782715, 1.077117920, 1.063217163, 1.048156738, 1.031936646, 1.014617920, 0.996246338,
|
||||
0.976852417, 0.956481934, 0.935195923, 0.913055420, 0.890090942, 0.866363525, 0.841949463, 0.816864014,
|
||||
0.791213989, 0.765029907, 0.738372803, 0.711318970, 0.683914185, 0.656219482, 0.628295898, 0.600219727,
|
||||
0.572036743, 0.543823242, 0.515609741, 0.487472534, 0.459472656, 0.431655884, 0.404083252, 0.376800537,
|
||||
0.349868774, 0.323318481, 0.297210693, 0.271591187, 0.246505737, 0.221984863, 0.198059082, 0.174789429,
|
||||
0.152206421, 0.130310059, 0.109161377, 0.088775635, 0.069168091, 0.050354004, 0.032379150, 0.015228271,
|
||||
-0.001068115, -0.016510010, -0.031082153, -0.044784546, -0.057617188, -0.069595337, -0.080688477, -0.090927124,
|
||||
0.100311279, 0.108856201, 0.116577148, 0.123474121, 0.129577637, 0.134887695, 0.139450073, 0.143264771,
|
||||
0.146362305, 0.148773193, 0.150497437, 0.151596069, 0.152069092, 0.151962280, 0.151306152, 0.150115967,
|
||||
0.148422241, 0.146255493, 0.143676758, 0.140670776, 0.137298584, 0.133590698, 0.129562378, 0.125259399,
|
||||
0.120697021, 0.115921021, 0.110946655, 0.105819702, 0.100540161, 0.095169067, 0.089706421, 0.084182739,
|
||||
0.078628540, 0.073059082, 0.067520142, 0.061996460, 0.056533813, 0.051132202, 0.045837402, 0.040634155,
|
||||
0.035552979, 0.030609131, 0.025817871, 0.021179199, 0.016708374, 0.012420654, 0.008316040, 0.004394531,
|
||||
0.000686646, -0.002822876, -0.006134033, -0.009231567, -0.012115479, -0.014801025, -0.017257690, -0.019531250,
|
||||
-0.021575928, -0.023422241, -0.025085449, -0.026535034, -0.027801514, -0.028884888, -0.029785156, -0.030517578,
|
||||
0.031082153, 0.031478882, 0.031738281, 0.031845093, 0.031814575, 0.031661987, 0.031387329, 0.031005859,
|
||||
0.030532837, 0.029937744, 0.029281616, 0.028533936, 0.027725220, 0.026840210, 0.025909424, 0.024932861,
|
||||
0.023910522, 0.022857666, 0.021789551, 0.020690918, 0.019577026, 0.018463135, 0.017349243, 0.016235352,
|
||||
0.015121460, 0.014022827, 0.012939453, 0.011886597, 0.010848999, 0.009841919, 0.008865356, 0.007919312,
|
||||
0.007003784, 0.006118774, 0.005294800, 0.004486084, 0.003723145, 0.003005981, 0.002334595, 0.001693726,
|
||||
0.001098633, 0.000549316, 0.000030518, -0.000442505, -0.000869751, -0.001266479, -0.001617432, -0.001937866,
|
||||
-0.002227783, -0.002487183, -0.002700806, -0.002883911, -0.003051758, -0.003173828, -0.003280640, -0.003372192,
|
||||
-0.003417969, -0.003463745, -0.003479004, -0.003479004, -0.003463745, -0.003433228, -0.003387451, -0.003326416,
|
||||
0.003250122, 0.003173828, 0.003082275, 0.002990723, 0.002899170, 0.002792358, 0.002685547, 0.002578735,
|
||||
0.002456665, 0.002349854, 0.002243042, 0.002120972, 0.002014160, 0.001907349, 0.001785278, 0.001693726,
|
||||
0.001586914, 0.001480103, 0.001388550, 0.001296997, 0.001205444, 0.001113892, 0.001037598, 0.000961304,
|
||||
0.000885010, 0.000808716, 0.000747681, 0.000686646, 0.000625610, 0.000579834, 0.000534058, 0.000473022,
|
||||
0.000442505, 0.000396729, 0.000366211, 0.000320435, 0.000289917, 0.000259399, 0.000244141, 0.000213623,
|
||||
0.000198364, 0.000167847, 0.000152588, 0.000137329, 0.000122070, 0.000106812, 0.000106812, 0.000091553,
|
||||
0.000076294, 0.000076294, 0.000061035, 0.000061035, 0.000045776, 0.000045776, 0.000030518, 0.000030518,
|
||||
0.000030518, 0.000030518, 0.000015259, 0.000015259, 0.000015259, 0.000015259, 0.000015259, 0.000015259,
|
||||
}
|
||||
E.LINBITS = { [0] = 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 8, 10, 13, 4, 5, 6, 7, 8, 9, 11, 13, 0, 0 }
|
||||
-- trees by table number (tables 16..23 share 16's tree, 24..31 share 24's)
|
||||
E.TREES = {
|
||||
[1] = {
|
||||
0x0201, 0x0000, 0x0201, 0x0010, 0x0201, 0x0001, 0x0011,
|
||||
},
|
||||
[2] = {
|
||||
0x0201, 0x0000, 0x0401, 0x0201, 0x0010, 0x0001, 0x0201, 0x0011, 0x0401, 0x0201, 0x0020, 0x0021, 0x0201, 0x0012, 0x0201, 0x0002,
|
||||
0x0022,
|
||||
},
|
||||
[3] = {
|
||||
0x0401, 0x0201, 0x0000, 0x0001, 0x0201, 0x0011, 0x0201, 0x0010, 0x0401, 0x0201, 0x0020, 0x0021, 0x0201, 0x0012, 0x0201, 0x0002,
|
||||
0x0022,
|
||||
},
|
||||
[5] = {
|
||||
0x0201, 0x0000, 0x0401, 0x0201, 0x0010, 0x0001, 0x0201, 0x0011, 0x0801, 0x0401, 0x0201, 0x0020, 0x0002, 0x0201, 0x0021, 0x0012,
|
||||
0x0801, 0x0401, 0x0201, 0x0022, 0x0030, 0x0201, 0x0003, 0x0013, 0x0201, 0x0031, 0x0201, 0x0032, 0x0201, 0x0023, 0x0033,
|
||||
},
|
||||
[6] = {
|
||||
0x0601, 0x0401, 0x0201, 0x0000, 0x0010, 0x0011, 0x0601, 0x0201, 0x0001, 0x0201, 0x0020, 0x0021, 0x0601, 0x0201, 0x0012, 0x0201,
|
||||
0x0002, 0x0022, 0x0401, 0x0201, 0x0031, 0x0013, 0x0401, 0x0201, 0x0030, 0x0032, 0x0201, 0x0023, 0x0201, 0x0003, 0x0033,
|
||||
},
|
||||
[7] = {
|
||||
0x0201, 0x0000, 0x0401, 0x0201, 0x0010, 0x0001, 0x0801, 0x0201, 0x0011, 0x0401, 0x0201, 0x0020, 0x0002, 0x0021, 0x1201, 0x0601,
|
||||
0x0201, 0x0012, 0x0201, 0x0022, 0x0030, 0x0401, 0x0201, 0x0031, 0x0013, 0x0401, 0x0201, 0x0003, 0x0032, 0x0201, 0x0023, 0x0004,
|
||||
0x0a01, 0x0401, 0x0201, 0x0040, 0x0041, 0x0201, 0x0014, 0x0201, 0x0042, 0x0024, 0x0c01, 0x0601, 0x0401, 0x0201, 0x0033, 0x0043,
|
||||
0x0050, 0x0401, 0x0201, 0x0034, 0x0005, 0x0051, 0x0601, 0x0201, 0x0015, 0x0201, 0x0052, 0x0025, 0x0401, 0x0201, 0x0044, 0x0035,
|
||||
0x0401, 0x0201, 0x0053, 0x0054, 0x0201, 0x0045, 0x0055,
|
||||
},
|
||||
[8] = {
|
||||
0x0601, 0x0201, 0x0000, 0x0201, 0x0010, 0x0001, 0x0201, 0x0011, 0x0401, 0x0201, 0x0021, 0x0012, 0x0e01, 0x0401, 0x0201, 0x0020,
|
||||
0x0002, 0x0201, 0x0022, 0x0401, 0x0201, 0x0030, 0x0003, 0x0201, 0x0031, 0x0013, 0x0e01, 0x0801, 0x0401, 0x0201, 0x0032, 0x0023,
|
||||
0x0201, 0x0040, 0x0004, 0x0201, 0x0041, 0x0201, 0x0014, 0x0042, 0x0c01, 0x0601, 0x0201, 0x0024, 0x0201, 0x0033, 0x0050, 0x0401,
|
||||
0x0201, 0x0043, 0x0034, 0x0051, 0x0601, 0x0201, 0x0015, 0x0201, 0x0005, 0x0052, 0x0601, 0x0201, 0x0025, 0x0201, 0x0044, 0x0035,
|
||||
0x0201, 0x0053, 0x0201, 0x0045, 0x0201, 0x0054, 0x0055,
|
||||
},
|
||||
[9] = {
|
||||
0x0801, 0x0401, 0x0201, 0x0000, 0x0010, 0x0201, 0x0001, 0x0011, 0x0a01, 0x0401, 0x0201, 0x0020, 0x0021, 0x0201, 0x0012, 0x0201,
|
||||
0x0002, 0x0022, 0x0c01, 0x0601, 0x0401, 0x0201, 0x0030, 0x0003, 0x0031, 0x0201, 0x0013, 0x0201, 0x0032, 0x0023, 0x0c01, 0x0401,
|
||||
0x0201, 0x0041, 0x0014, 0x0401, 0x0201, 0x0040, 0x0033, 0x0201, 0x0042, 0x0024, 0x0a01, 0x0601, 0x0401, 0x0201, 0x0004, 0x0050,
|
||||
0x0043, 0x0201, 0x0034, 0x0051, 0x0801, 0x0401, 0x0201, 0x0015, 0x0052, 0x0201, 0x0025, 0x0044, 0x0601, 0x0401, 0x0201, 0x0005,
|
||||
0x0054, 0x0053, 0x0201, 0x0035, 0x0201, 0x0045, 0x0055,
|
||||
},
|
||||
[10] = {
|
||||
0x0201, 0x0000, 0x0401, 0x0201, 0x0010, 0x0001, 0x0a01, 0x0201, 0x0011, 0x0401, 0x0201, 0x0020, 0x0002, 0x0201, 0x0021, 0x0012,
|
||||
0x1c01, 0x0801, 0x0401, 0x0201, 0x0022, 0x0030, 0x0201, 0x0031, 0x0013, 0x0801, 0x0401, 0x0201, 0x0003, 0x0032, 0x0201, 0x0023,
|
||||
0x0040, 0x0401, 0x0201, 0x0041, 0x0014, 0x0401, 0x0201, 0x0004, 0x0033, 0x0201, 0x0042, 0x0024, 0x1c01, 0x0a01, 0x0601, 0x0401,
|
||||
0x0201, 0x0050, 0x0005, 0x0060, 0x0201, 0x0061, 0x0016, 0x0c01, 0x0601, 0x0401, 0x0201, 0x0043, 0x0034, 0x0051, 0x0201, 0x0015,
|
||||
0x0201, 0x0052, 0x0025, 0x0401, 0x0201, 0x0026, 0x0036, 0x0071, 0x1401, 0x0801, 0x0201, 0x0017, 0x0401, 0x0201, 0x0044, 0x0053,
|
||||
0x0006, 0x0601, 0x0401, 0x0201, 0x0035, 0x0045, 0x0062, 0x0201, 0x0070, 0x0201, 0x0007, 0x0064, 0x0e01, 0x0401, 0x0201, 0x0072,
|
||||
0x0027, 0x0601, 0x0201, 0x0063, 0x0201, 0x0054, 0x0055, 0x0201, 0x0046, 0x0073, 0x0801, 0x0401, 0x0201, 0x0037, 0x0065, 0x0201,
|
||||
0x0056, 0x0074, 0x0601, 0x0201, 0x0047, 0x0201, 0x0066, 0x0075, 0x0401, 0x0201, 0x0057, 0x0076, 0x0201, 0x0067, 0x0077,
|
||||
},
|
||||
[11] = {
|
||||
0x0601, 0x0201, 0x0000, 0x0201, 0x0010, 0x0001, 0x0801, 0x0201, 0x0011, 0x0401, 0x0201, 0x0020, 0x0002, 0x0012, 0x1801, 0x0801,
|
||||
0x0201, 0x0021, 0x0201, 0x0022, 0x0201, 0x0030, 0x0003, 0x0401, 0x0201, 0x0031, 0x0013, 0x0401, 0x0201, 0x0032, 0x0023, 0x0401,
|
||||
0x0201, 0x0040, 0x0004, 0x0201, 0x0041, 0x0014, 0x1e01, 0x1001, 0x0a01, 0x0401, 0x0201, 0x0042, 0x0024, 0x0401, 0x0201, 0x0033,
|
||||
0x0043, 0x0050, 0x0401, 0x0201, 0x0034, 0x0051, 0x0061, 0x0601, 0x0201, 0x0016, 0x0201, 0x0006, 0x0026, 0x0201, 0x0062, 0x0201,
|
||||
0x0015, 0x0201, 0x0005, 0x0052, 0x1001, 0x0a01, 0x0601, 0x0401, 0x0201, 0x0025, 0x0044, 0x0060, 0x0201, 0x0063, 0x0036, 0x0401,
|
||||
0x0201, 0x0070, 0x0017, 0x0071, 0x1001, 0x0601, 0x0401, 0x0201, 0x0007, 0x0064, 0x0072, 0x0201, 0x0027, 0x0401, 0x0201, 0x0053,
|
||||
0x0035, 0x0201, 0x0054, 0x0045, 0x0a01, 0x0401, 0x0201, 0x0046, 0x0073, 0x0201, 0x0037, 0x0201, 0x0065, 0x0056, 0x0a01, 0x0601,
|
||||
0x0401, 0x0201, 0x0055, 0x0057, 0x0074, 0x0201, 0x0047, 0x0066, 0x0401, 0x0201, 0x0075, 0x0076, 0x0201, 0x0067, 0x0077,
|
||||
},
|
||||
[12] = {
|
||||
0x0c01, 0x0401, 0x0201, 0x0010, 0x0001, 0x0201, 0x0011, 0x0201, 0x0000, 0x0201, 0x0020, 0x0002, 0x1001, 0x0401, 0x0201, 0x0021,
|
||||
0x0012, 0x0401, 0x0201, 0x0022, 0x0031, 0x0201, 0x0013, 0x0201, 0x0030, 0x0201, 0x0003, 0x0040, 0x1a01, 0x0801, 0x0401, 0x0201,
|
||||
0x0032, 0x0023, 0x0201, 0x0041, 0x0033, 0x0a01, 0x0401, 0x0201, 0x0014, 0x0042, 0x0201, 0x0024, 0x0201, 0x0004, 0x0050, 0x0401,
|
||||
0x0201, 0x0043, 0x0034, 0x0201, 0x0051, 0x0015, 0x1c01, 0x0e01, 0x0801, 0x0401, 0x0201, 0x0052, 0x0025, 0x0201, 0x0053, 0x0035,
|
||||
0x0401, 0x0201, 0x0060, 0x0016, 0x0061, 0x0401, 0x0201, 0x0062, 0x0026, 0x0601, 0x0401, 0x0201, 0x0005, 0x0006, 0x0044, 0x0201,
|
||||
0x0054, 0x0045, 0x1201, 0x0a01, 0x0401, 0x0201, 0x0063, 0x0036, 0x0401, 0x0201, 0x0070, 0x0007, 0x0071, 0x0401, 0x0201, 0x0017,
|
||||
0x0064, 0x0201, 0x0046, 0x0072, 0x0a01, 0x0601, 0x0201, 0x0027, 0x0201, 0x0055, 0x0073, 0x0201, 0x0037, 0x0056, 0x0801, 0x0401,
|
||||
0x0201, 0x0065, 0x0074, 0x0201, 0x0047, 0x0066, 0x0401, 0x0201, 0x0075, 0x0057, 0x0201, 0x0076, 0x0201, 0x0067, 0x0077,
|
||||
},
|
||||
[13] = {
|
||||
0x0201, 0x0000, 0x0601, 0x0201, 0x0010, 0x0201, 0x0001, 0x0011, 0x1c01, 0x0801, 0x0401, 0x0201, 0x0020, 0x0002, 0x0201, 0x0021,
|
||||
0x0012, 0x0801, 0x0401, 0x0201, 0x0022, 0x0030, 0x0201, 0x0003, 0x0031, 0x0601, 0x0201, 0x0013, 0x0201, 0x0032, 0x0023, 0x0401,
|
||||
0x0201, 0x0040, 0x0004, 0x0041, 0x4601, 0x1c01, 0x0e01, 0x0601, 0x0201, 0x0014, 0x0201, 0x0033, 0x0042, 0x0401, 0x0201, 0x0024,
|
||||
0x0050, 0x0201, 0x0043, 0x0034, 0x0401, 0x0201, 0x0051, 0x0015, 0x0401, 0x0201, 0x0005, 0x0052, 0x0201, 0x0025, 0x0201, 0x0044,
|
||||
0x0053, 0x0e01, 0x0801, 0x0401, 0x0201, 0x0060, 0x0006, 0x0201, 0x0061, 0x0016, 0x0401, 0x0201, 0x0080, 0x0008, 0x0081, 0x1001,
|
||||
0x0801, 0x0401, 0x0201, 0x0035, 0x0062, 0x0201, 0x0026, 0x0054, 0x0401, 0x0201, 0x0045, 0x0063, 0x0201, 0x0036, 0x0070, 0x0601,
|
||||
0x0401, 0x0201, 0x0007, 0x0055, 0x0071, 0x0201, 0x0017, 0x0201, 0x0027, 0x0037, 0x4801, 0x1801, 0x0c01, 0x0401, 0x0201, 0x0018,
|
||||
0x0082, 0x0201, 0x0028, 0x0401, 0x0201, 0x0064, 0x0046, 0x0072, 0x0801, 0x0401, 0x0201, 0x0084, 0x0048, 0x0201, 0x0090, 0x0009,
|
||||
0x0201, 0x0091, 0x0019, 0x1801, 0x0e01, 0x0801, 0x0401, 0x0201, 0x0073, 0x0065, 0x0201, 0x0056, 0x0074, 0x0401, 0x0201, 0x0047,
|
||||
0x0066, 0x0083, 0x0601, 0x0201, 0x0038, 0x0201, 0x0075, 0x0057, 0x0201, 0x0092, 0x0029, 0x0e01, 0x0801, 0x0401, 0x0201, 0x0067,
|
||||
0x0085, 0x0201, 0x0058, 0x0039, 0x0201, 0x0093, 0x0201, 0x0049, 0x0086, 0x0601, 0x0201, 0x00a0, 0x0201, 0x0068, 0x000a, 0x0201,
|
||||
0x00a1, 0x001a, 0x4401, 0x1801, 0x0c01, 0x0401, 0x0201, 0x00a2, 0x002a, 0x0401, 0x0201, 0x0095, 0x0059, 0x0201, 0x00a3, 0x003a,
|
||||
0x0801, 0x0401, 0x0201, 0x004a, 0x0096, 0x0201, 0x00b0, 0x000b, 0x0201, 0x00b1, 0x001b, 0x1401, 0x0801, 0x0201, 0x00b2, 0x0401,
|
||||
0x0201, 0x0076, 0x0077, 0x0094, 0x0601, 0x0401, 0x0201, 0x0087, 0x0078, 0x00a4, 0x0401, 0x0201, 0x0069, 0x00a5, 0x002b, 0x0c01,
|
||||
0x0601, 0x0401, 0x0201, 0x005a, 0x0088, 0x00b3, 0x0201, 0x003b, 0x0201, 0x0079, 0x00a6, 0x0601, 0x0401, 0x0201, 0x006a, 0x00b4,
|
||||
0x00c0, 0x0401, 0x0201, 0x000c, 0x0098, 0x00c1, 0x3c01, 0x1601, 0x0a01, 0x0601, 0x0201, 0x001c, 0x0201, 0x0089, 0x00b5, 0x0201,
|
||||
0x005b, 0x00c2, 0x0401, 0x0201, 0x002c, 0x003c, 0x0401, 0x0201, 0x00b6, 0x006b, 0x0201, 0x00c4, 0x004c, 0x1001, 0x0801, 0x0401,
|
||||
0x0201, 0x00a8, 0x008a, 0x0201, 0x00d0, 0x000d, 0x0201, 0x00d1, 0x0201, 0x004b, 0x0201, 0x0097, 0x00a7, 0x0c01, 0x0601, 0x0201,
|
||||
0x00c3, 0x0201, 0x007a, 0x0099, 0x0401, 0x0201, 0x00c5, 0x005c, 0x00b7, 0x0401, 0x0201, 0x001d, 0x00d2, 0x0201, 0x002d, 0x0201,
|
||||
0x007b, 0x00d3, 0x3401, 0x1c01, 0x0c01, 0x0401, 0x0201, 0x003d, 0x00c6, 0x0401, 0x0201, 0x006c, 0x00a9, 0x0201, 0x009a, 0x00d4,
|
||||
0x0801, 0x0401, 0x0201, 0x00b8, 0x008b, 0x0201, 0x004d, 0x00c7, 0x0401, 0x0201, 0x007c, 0x00d5, 0x0201, 0x005d, 0x00e0, 0x0a01,
|
||||
0x0401, 0x0201, 0x00e1, 0x001e, 0x0401, 0x0201, 0x000e, 0x002e, 0x00e2, 0x0801, 0x0401, 0x0201, 0x00e3, 0x006d, 0x0201, 0x008c,
|
||||
0x00e4, 0x0401, 0x0201, 0x00e5, 0x00ba, 0x00f0, 0x2601, 0x1001, 0x0401, 0x0201, 0x00f1, 0x001f, 0x0601, 0x0401, 0x0201, 0x00aa,
|
||||
0x009b, 0x00b9, 0x0201, 0x003e, 0x0201, 0x00d6, 0x00c8, 0x0c01, 0x0601, 0x0201, 0x004e, 0x0201, 0x00d7, 0x007d, 0x0201, 0x00ab,
|
||||
0x0201, 0x005e, 0x00c9, 0x0601, 0x0201, 0x000f, 0x0201, 0x009c, 0x006e, 0x0201, 0x00f2, 0x002f, 0x2001, 0x1001, 0x0601, 0x0401,
|
||||
0x0201, 0x00d8, 0x008d, 0x003f, 0x0601, 0x0201, 0x00f3, 0x0201, 0x00e6, 0x00ca, 0x0201, 0x00f4, 0x004f, 0x0801, 0x0401, 0x0201,
|
||||
0x00bb, 0x00ac, 0x0201, 0x00e7, 0x00f5, 0x0401, 0x0201, 0x00d9, 0x009d, 0x0201, 0x005f, 0x00e8, 0x1e01, 0x0c01, 0x0601, 0x0201,
|
||||
0x006f, 0x0201, 0x00f6, 0x00cb, 0x0401, 0x0201, 0x00bc, 0x00ad, 0x00da, 0x0801, 0x0201, 0x00f7, 0x0401, 0x0201, 0x007e, 0x007f,
|
||||
0x008e, 0x0601, 0x0401, 0x0201, 0x009e, 0x00ae, 0x00cc, 0x0201, 0x00f8, 0x008f, 0x1201, 0x0801, 0x0401, 0x0201, 0x00db, 0x00bd,
|
||||
0x0201, 0x00ea, 0x00f9, 0x0401, 0x0201, 0x009f, 0x00eb, 0x0201, 0x00be, 0x0201, 0x00cd, 0x00fa, 0x0e01, 0x0401, 0x0201, 0x00dd,
|
||||
0x00ec, 0x0601, 0x0401, 0x0201, 0x00e9, 0x00af, 0x00dc, 0x0201, 0x00ce, 0x00fb, 0x0801, 0x0401, 0x0201, 0x00bf, 0x00de, 0x0201,
|
||||
0x00cf, 0x00ee, 0x0401, 0x0201, 0x00df, 0x00ef, 0x0201, 0x00ff, 0x0201, 0x00ed, 0x0201, 0x00fd, 0x0201, 0x00fc, 0x00fe,
|
||||
},
|
||||
[15] = {
|
||||
0x1001, 0x0601, 0x0201, 0x0000, 0x0201, 0x0010, 0x0001, 0x0201, 0x0011, 0x0401, 0x0201, 0x0020, 0x0002, 0x0201, 0x0021, 0x0012,
|
||||
0x3201, 0x1001, 0x0601, 0x0201, 0x0022, 0x0201, 0x0030, 0x0031, 0x0601, 0x0201, 0x0013, 0x0201, 0x0003, 0x0040, 0x0201, 0x0032,
|
||||
0x0023, 0x0e01, 0x0601, 0x0401, 0x0201, 0x0004, 0x0014, 0x0041, 0x0401, 0x0201, 0x0033, 0x0042, 0x0201, 0x0024, 0x0043, 0x0a01,
|
||||
0x0601, 0x0201, 0x0034, 0x0201, 0x0050, 0x0005, 0x0201, 0x0051, 0x0015, 0x0401, 0x0201, 0x0052, 0x0025, 0x0401, 0x0201, 0x0044,
|
||||
0x0053, 0x0061, 0x5a01, 0x2401, 0x1201, 0x0a01, 0x0601, 0x0201, 0x0035, 0x0201, 0x0060, 0x0006, 0x0201, 0x0016, 0x0062, 0x0401,
|
||||
0x0201, 0x0026, 0x0054, 0x0201, 0x0045, 0x0063, 0x0a01, 0x0601, 0x0201, 0x0036, 0x0201, 0x0070, 0x0007, 0x0201, 0x0071, 0x0055,
|
||||
0x0401, 0x0201, 0x0017, 0x0064, 0x0201, 0x0072, 0x0027, 0x1801, 0x1001, 0x0801, 0x0401, 0x0201, 0x0046, 0x0073, 0x0201, 0x0037,
|
||||
0x0065, 0x0401, 0x0201, 0x0056, 0x0080, 0x0201, 0x0008, 0x0074, 0x0401, 0x0201, 0x0081, 0x0018, 0x0201, 0x0082, 0x0028, 0x1001,
|
||||
0x0801, 0x0401, 0x0201, 0x0047, 0x0066, 0x0201, 0x0083, 0x0038, 0x0401, 0x0201, 0x0075, 0x0057, 0x0201, 0x0084, 0x0048, 0x0601,
|
||||
0x0401, 0x0201, 0x0090, 0x0019, 0x0091, 0x0401, 0x0201, 0x0092, 0x0076, 0x0201, 0x0067, 0x0029, 0x5c01, 0x2401, 0x1201, 0x0a01,
|
||||
0x0401, 0x0201, 0x0085, 0x0058, 0x0401, 0x0201, 0x0009, 0x0077, 0x0093, 0x0401, 0x0201, 0x0039, 0x0094, 0x0201, 0x0049, 0x0086,
|
||||
0x0a01, 0x0601, 0x0201, 0x0068, 0x0201, 0x00a0, 0x000a, 0x0201, 0x00a1, 0x001a, 0x0401, 0x0201, 0x00a2, 0x002a, 0x0201, 0x0095,
|
||||
0x0059, 0x1a01, 0x0e01, 0x0601, 0x0201, 0x00a3, 0x0201, 0x003a, 0x0087, 0x0401, 0x0201, 0x0078, 0x00a4, 0x0201, 0x004a, 0x0096,
|
||||
0x0601, 0x0401, 0x0201, 0x0069, 0x00b0, 0x00b1, 0x0401, 0x0201, 0x001b, 0x00a5, 0x00b2, 0x0e01, 0x0801, 0x0401, 0x0201, 0x005a,
|
||||
0x002b, 0x0201, 0x0088, 0x0097, 0x0201, 0x00b3, 0x0201, 0x0079, 0x003b, 0x0801, 0x0401, 0x0201, 0x006a, 0x00b4, 0x0201, 0x004b,
|
||||
0x00c1, 0x0401, 0x0201, 0x0098, 0x0089, 0x0201, 0x001c, 0x00b5, 0x5001, 0x2201, 0x1001, 0x0601, 0x0401, 0x0201, 0x005b, 0x002c,
|
||||
0x00c2, 0x0601, 0x0401, 0x0201, 0x000b, 0x00c0, 0x00a6, 0x0201, 0x00a7, 0x007a, 0x0a01, 0x0401, 0x0201, 0x00c3, 0x003c, 0x0401,
|
||||
0x0201, 0x000c, 0x0099, 0x00b6, 0x0401, 0x0201, 0x006b, 0x00c4, 0x0201, 0x004c, 0x00a8, 0x1401, 0x0a01, 0x0401, 0x0201, 0x008a,
|
||||
0x00c5, 0x0401, 0x0201, 0x00d0, 0x005c, 0x00d1, 0x0401, 0x0201, 0x00b7, 0x007b, 0x0201, 0x001d, 0x0201, 0x000d, 0x002d, 0x0c01,
|
||||
0x0401, 0x0201, 0x00d2, 0x00d3, 0x0401, 0x0201, 0x003d, 0x00c6, 0x0201, 0x006c, 0x00a9, 0x0601, 0x0401, 0x0201, 0x009a, 0x00b8,
|
||||
0x00d4, 0x0401, 0x0201, 0x008b, 0x004d, 0x0201, 0x00c7, 0x007c, 0x4401, 0x2201, 0x1201, 0x0a01, 0x0401, 0x0201, 0x00d5, 0x005d,
|
||||
0x0401, 0x0201, 0x00e0, 0x000e, 0x00e1, 0x0401, 0x0201, 0x001e, 0x00e2, 0x0201, 0x00aa, 0x002e, 0x0801, 0x0401, 0x0201, 0x00b9,
|
||||
0x009b, 0x0201, 0x00e3, 0x00d6, 0x0401, 0x0201, 0x006d, 0x003e, 0x0201, 0x00c8, 0x008c, 0x1001, 0x0801, 0x0401, 0x0201, 0x00e4,
|
||||
0x004e, 0x0201, 0x00d7, 0x007d, 0x0401, 0x0201, 0x00e5, 0x00ba, 0x0201, 0x00ab, 0x005e, 0x0801, 0x0401, 0x0201, 0x00c9, 0x009c,
|
||||
0x0201, 0x00f1, 0x001f, 0x0601, 0x0401, 0x0201, 0x00f0, 0x006e, 0x00f2, 0x0201, 0x002f, 0x00e6, 0x2601, 0x1201, 0x0801, 0x0401,
|
||||
0x0201, 0x00d8, 0x00f3, 0x0201, 0x003f, 0x00f4, 0x0601, 0x0201, 0x004f, 0x0201, 0x008d, 0x00d9, 0x0201, 0x00bb, 0x00ca, 0x0801,
|
||||
0x0401, 0x0201, 0x00ac, 0x00e7, 0x0201, 0x007e, 0x00f5, 0x0801, 0x0401, 0x0201, 0x009d, 0x005f, 0x0201, 0x00e8, 0x008e, 0x0201,
|
||||
0x00f6, 0x00cb, 0x2201, 0x1201, 0x0a01, 0x0601, 0x0401, 0x0201, 0x000f, 0x00ae, 0x006f, 0x0201, 0x00bc, 0x00da, 0x0401, 0x0201,
|
||||
0x00ad, 0x00f7, 0x0201, 0x007f, 0x00e9, 0x0801, 0x0401, 0x0201, 0x009e, 0x00cc, 0x0201, 0x00f8, 0x008f, 0x0401, 0x0201, 0x00db,
|
||||
0x00bd, 0x0201, 0x00ea, 0x00f9, 0x1001, 0x0801, 0x0401, 0x0201, 0x009f, 0x00dc, 0x0201, 0x00cd, 0x00eb, 0x0401, 0x0201, 0x00be,
|
||||
0x00fa, 0x0201, 0x00af, 0x00dd, 0x0e01, 0x0601, 0x0401, 0x0201, 0x00ec, 0x00ce, 0x00fb, 0x0401, 0x0201, 0x00bf, 0x00ed, 0x0201,
|
||||
0x00de, 0x00fc, 0x0601, 0x0401, 0x0201, 0x00cf, 0x00fd, 0x00ee, 0x0401, 0x0201, 0x00df, 0x00fe, 0x0201, 0x00ef, 0x00ff,
|
||||
},
|
||||
[16] = {
|
||||
0x0201, 0x0000, 0x0601, 0x0201, 0x0010, 0x0201, 0x0001, 0x0011, 0x2a01, 0x0801, 0x0401, 0x0201, 0x0020, 0x0002, 0x0201, 0x0021,
|
||||
0x0012, 0x0a01, 0x0601, 0x0201, 0x0022, 0x0201, 0x0030, 0x0003, 0x0201, 0x0031, 0x0013, 0x0a01, 0x0401, 0x0201, 0x0032, 0x0023,
|
||||
0x0401, 0x0201, 0x0040, 0x0004, 0x0041, 0x0601, 0x0201, 0x0014, 0x0201, 0x0033, 0x0042, 0x0401, 0x0201, 0x0024, 0x0050, 0x0201,
|
||||
0x0043, 0x0034, 0x8a01, 0x2801, 0x1001, 0x0601, 0x0401, 0x0201, 0x0005, 0x0015, 0x0051, 0x0401, 0x0201, 0x0052, 0x0025, 0x0401,
|
||||
0x0201, 0x0044, 0x0035, 0x0053, 0x0a01, 0x0601, 0x0401, 0x0201, 0x0060, 0x0006, 0x0061, 0x0201, 0x0016, 0x0062, 0x0801, 0x0401,
|
||||
0x0201, 0x0026, 0x0054, 0x0201, 0x0045, 0x0063, 0x0401, 0x0201, 0x0036, 0x0070, 0x0071, 0x2801, 0x1201, 0x0801, 0x0201, 0x0017,
|
||||
0x0201, 0x0007, 0x0201, 0x0055, 0x0064, 0x0401, 0x0201, 0x0072, 0x0027, 0x0401, 0x0201, 0x0046, 0x0065, 0x0073, 0x0a01, 0x0601,
|
||||
0x0201, 0x0037, 0x0201, 0x0056, 0x0008, 0x0201, 0x0080, 0x0081, 0x0601, 0x0201, 0x0018, 0x0201, 0x0074, 0x0047, 0x0201, 0x0082,
|
||||
0x0201, 0x0028, 0x0066, 0x1801, 0x0e01, 0x0801, 0x0401, 0x0201, 0x0083, 0x0038, 0x0201, 0x0075, 0x0084, 0x0401, 0x0201, 0x0048,
|
||||
0x0090, 0x0091, 0x0601, 0x0201, 0x0019, 0x0201, 0x0009, 0x0076, 0x0201, 0x0092, 0x0029, 0x0e01, 0x0801, 0x0401, 0x0201, 0x0085,
|
||||
0x0058, 0x0201, 0x0093, 0x0039, 0x0401, 0x0201, 0x00a0, 0x000a, 0x001a, 0x0801, 0x0201, 0x00a2, 0x0201, 0x0067, 0x0201, 0x0057,
|
||||
0x0049, 0x0601, 0x0201, 0x0094, 0x0201, 0x0077, 0x0086, 0x0201, 0x00a1, 0x0201, 0x0068, 0x0095, 0xdc01, 0x7e01, 0x3201, 0x1a01,
|
||||
0x0c01, 0x0601, 0x0201, 0x002a, 0x0201, 0x0059, 0x003a, 0x0201, 0x00a3, 0x0201, 0x0087, 0x0078, 0x0801, 0x0401, 0x0201, 0x00a4,
|
||||
0x004a, 0x0201, 0x0096, 0x0069, 0x0401, 0x0201, 0x00b0, 0x000b, 0x00b1, 0x0a01, 0x0401, 0x0201, 0x001b, 0x00b2, 0x0201, 0x002b,
|
||||
0x0201, 0x00a5, 0x005a, 0x0601, 0x0201, 0x00b3, 0x0201, 0x00a6, 0x006a, 0x0401, 0x0201, 0x00b4, 0x004b, 0x0201, 0x000c, 0x00c1,
|
||||
0x1e01, 0x0e01, 0x0601, 0x0401, 0x0201, 0x00b5, 0x00c2, 0x002c, 0x0401, 0x0201, 0x00a7, 0x00c3, 0x0201, 0x006b, 0x00c4, 0x0801,
|
||||
0x0201, 0x001d, 0x0401, 0x0201, 0x0088, 0x0097, 0x003b, 0x0401, 0x0201, 0x00d1, 0x00d2, 0x0201, 0x002d, 0x00d3, 0x1201, 0x0601,
|
||||
0x0401, 0x0201, 0x001e, 0x002e, 0x00e2, 0x0601, 0x0401, 0x0201, 0x0079, 0x0098, 0x00c0, 0x0201, 0x001c, 0x0201, 0x0089, 0x005b,
|
||||
0x0e01, 0x0601, 0x0201, 0x003c, 0x0201, 0x007a, 0x00b6, 0x0401, 0x0201, 0x004c, 0x0099, 0x0201, 0x00a8, 0x008a, 0x0601, 0x0201,
|
||||
0x000d, 0x0201, 0x00c5, 0x005c, 0x0401, 0x0201, 0x003d, 0x00c6, 0x0201, 0x006c, 0x009a, 0x5801, 0x5601, 0x2401, 0x1001, 0x0801,
|
||||
0x0401, 0x0201, 0x008b, 0x004d, 0x0201, 0x00c7, 0x007c, 0x0401, 0x0201, 0x00d5, 0x005d, 0x0201, 0x00e0, 0x000e, 0x0801, 0x0201,
|
||||
0x00e3, 0x0401, 0x0201, 0x00d0, 0x00b7, 0x007b, 0x0601, 0x0401, 0x0201, 0x00a9, 0x00b8, 0x00d4, 0x0201, 0x00e1, 0x0201, 0x00aa,
|
||||
0x00b9, 0x1801, 0x0a01, 0x0601, 0x0401, 0x0201, 0x009b, 0x00d6, 0x006d, 0x0201, 0x003e, 0x00c8, 0x0601, 0x0401, 0x0201, 0x008c,
|
||||
0x00e4, 0x004e, 0x0401, 0x0201, 0x00d7, 0x00e5, 0x0201, 0x00ba, 0x00ab, 0x0c01, 0x0401, 0x0201, 0x009c, 0x00e6, 0x0401, 0x0201,
|
||||
0x006e, 0x00d8, 0x0201, 0x008d, 0x00bb, 0x0801, 0x0401, 0x0201, 0x00e7, 0x009d, 0x0201, 0x00e8, 0x008e, 0x0401, 0x0201, 0x00cb,
|
||||
0x00bc, 0x009e, 0x00f1, 0x0201, 0x001f, 0x0201, 0x000f, 0x002f, 0x4201, 0x3801, 0x0201, 0x00f2, 0x3401, 0x3201, 0x1401, 0x0801,
|
||||
0x0201, 0x00bd, 0x0201, 0x005e, 0x0201, 0x007d, 0x00c9, 0x0601, 0x0201, 0x00ca, 0x0201, 0x00ac, 0x007e, 0x0401, 0x0201, 0x00da,
|
||||
0x00ad, 0x00cc, 0x0a01, 0x0601, 0x0201, 0x00ae, 0x0201, 0x00db, 0x00dc, 0x0201, 0x00cd, 0x00be, 0x0601, 0x0401, 0x0201, 0x00eb,
|
||||
0x00ed, 0x00ee, 0x0601, 0x0401, 0x0201, 0x00d9, 0x00ea, 0x00e9, 0x0201, 0x00de, 0x0401, 0x0201, 0x00dd, 0x00ec, 0x00ce, 0x003f,
|
||||
0x00f0, 0x0401, 0x0201, 0x00f3, 0x00f4, 0x0201, 0x004f, 0x0201, 0x00f5, 0x005f, 0x0a01, 0x0201, 0x00ff, 0x0401, 0x0201, 0x00f6,
|
||||
0x006f, 0x0201, 0x00f7, 0x007f, 0x0c01, 0x0601, 0x0201, 0x008f, 0x0201, 0x00f8, 0x00f9, 0x0401, 0x0201, 0x009f, 0x00fa, 0x00af,
|
||||
0x0801, 0x0401, 0x0201, 0x00fb, 0x00bf, 0x0201, 0x00fc, 0x00cf, 0x0401, 0x0201, 0x00fd, 0x00df, 0x0201, 0x00fe, 0x00ef,
|
||||
},
|
||||
[24] = {
|
||||
0x3c01, 0x0801, 0x0401, 0x0201, 0x0000, 0x0010, 0x0201, 0x0001, 0x0011, 0x0e01, 0x0601, 0x0401, 0x0201, 0x0020, 0x0002, 0x0021,
|
||||
0x0201, 0x0012, 0x0201, 0x0022, 0x0201, 0x0030, 0x0003, 0x0e01, 0x0401, 0x0201, 0x0031, 0x0013, 0x0401, 0x0201, 0x0032, 0x0023,
|
||||
0x0401, 0x0201, 0x0040, 0x0004, 0x0041, 0x0801, 0x0401, 0x0201, 0x0014, 0x0033, 0x0201, 0x0042, 0x0024, 0x0601, 0x0401, 0x0201,
|
||||
0x0043, 0x0034, 0x0051, 0x0601, 0x0401, 0x0201, 0x0050, 0x0005, 0x0015, 0x0201, 0x0052, 0x0025, 0xfa01, 0x6201, 0x2201, 0x1201,
|
||||
0x0a01, 0x0401, 0x0201, 0x0044, 0x0053, 0x0201, 0x0035, 0x0201, 0x0060, 0x0006, 0x0401, 0x0201, 0x0061, 0x0016, 0x0201, 0x0062,
|
||||
0x0026, 0x0801, 0x0401, 0x0201, 0x0054, 0x0045, 0x0201, 0x0063, 0x0036, 0x0401, 0x0201, 0x0071, 0x0055, 0x0201, 0x0064, 0x0046,
|
||||
0x2001, 0x0e01, 0x0601, 0x0201, 0x0072, 0x0201, 0x0027, 0x0037, 0x0201, 0x0073, 0x0401, 0x0201, 0x0070, 0x0007, 0x0017, 0x0a01,
|
||||
0x0401, 0x0201, 0x0065, 0x0056, 0x0401, 0x0201, 0x0080, 0x0008, 0x0081, 0x0401, 0x0201, 0x0074, 0x0047, 0x0201, 0x0018, 0x0082,
|
||||
0x1001, 0x0801, 0x0401, 0x0201, 0x0028, 0x0066, 0x0201, 0x0083, 0x0038, 0x0401, 0x0201, 0x0075, 0x0057, 0x0201, 0x0084, 0x0048,
|
||||
0x0801, 0x0401, 0x0201, 0x0091, 0x0019, 0x0201, 0x0092, 0x0076, 0x0401, 0x0201, 0x0067, 0x0029, 0x0201, 0x0085, 0x0058, 0x5c01,
|
||||
0x2201, 0x1001, 0x0801, 0x0401, 0x0201, 0x0093, 0x0039, 0x0201, 0x0094, 0x0049, 0x0401, 0x0201, 0x0077, 0x0086, 0x0201, 0x0068,
|
||||
0x00a1, 0x0801, 0x0401, 0x0201, 0x00a2, 0x002a, 0x0201, 0x0095, 0x0059, 0x0401, 0x0201, 0x00a3, 0x003a, 0x0201, 0x0087, 0x0201,
|
||||
0x0078, 0x004a, 0x1601, 0x0c01, 0x0401, 0x0201, 0x00a4, 0x0096, 0x0401, 0x0201, 0x0069, 0x00b1, 0x0201, 0x001b, 0x00a5, 0x0601,
|
||||
0x0201, 0x00b2, 0x0201, 0x005a, 0x002b, 0x0201, 0x0088, 0x00b3, 0x1001, 0x0a01, 0x0601, 0x0201, 0x0090, 0x0201, 0x0009, 0x00a0,
|
||||
0x0201, 0x0097, 0x0079, 0x0401, 0x0201, 0x00a6, 0x006a, 0x00b4, 0x0c01, 0x0601, 0x0201, 0x001a, 0x0201, 0x000a, 0x00b0, 0x0201,
|
||||
0x003b, 0x0201, 0x000b, 0x00c0, 0x0401, 0x0201, 0x004b, 0x00c1, 0x0201, 0x0098, 0x0089, 0x4301, 0x2201, 0x1001, 0x0801, 0x0401,
|
||||
0x0201, 0x001c, 0x00b5, 0x0201, 0x005b, 0x00c2, 0x0401, 0x0201, 0x002c, 0x00a7, 0x0201, 0x007a, 0x00c3, 0x0a01, 0x0601, 0x0201,
|
||||
0x003c, 0x0201, 0x000c, 0x00d0, 0x0201, 0x00b6, 0x006b, 0x0401, 0x0201, 0x00c4, 0x004c, 0x0201, 0x0099, 0x00a8, 0x1001, 0x0801,
|
||||
0x0401, 0x0201, 0x008a, 0x00c5, 0x0201, 0x005c, 0x00d1, 0x0401, 0x0201, 0x00b7, 0x007b, 0x0201, 0x001d, 0x00d2, 0x0901, 0x0401,
|
||||
0x0201, 0x002d, 0x00d3, 0x0201, 0x003d, 0x00c6, 0x55fa, 0x0401, 0x0201, 0x006c, 0x00a9, 0x0201, 0x009a, 0x00d4, 0x2001, 0x1001,
|
||||
0x0801, 0x0401, 0x0201, 0x00b8, 0x008b, 0x0201, 0x004d, 0x00c7, 0x0401, 0x0201, 0x007c, 0x00d5, 0x0201, 0x005d, 0x00e1, 0x0801,
|
||||
0x0401, 0x0201, 0x001e, 0x00e2, 0x0201, 0x00aa, 0x00b9, 0x0401, 0x0201, 0x009b, 0x00e3, 0x0201, 0x00d6, 0x006d, 0x1401, 0x0a01,
|
||||
0x0601, 0x0201, 0x003e, 0x0201, 0x002e, 0x004e, 0x0201, 0x00c8, 0x008c, 0x0401, 0x0201, 0x00e4, 0x00d7, 0x0401, 0x0201, 0x007d,
|
||||
0x00ab, 0x00e5, 0x0a01, 0x0401, 0x0201, 0x00ba, 0x005e, 0x0201, 0x00c9, 0x0201, 0x009c, 0x006e, 0x0801, 0x0201, 0x00e6, 0x0201,
|
||||
0x000d, 0x0201, 0x00e0, 0x000e, 0x0401, 0x0201, 0x00d8, 0x008d, 0x0201, 0x00bb, 0x00ca, 0x4a01, 0x0201, 0x00ff, 0x4001, 0x3a01,
|
||||
0x2001, 0x1001, 0x0801, 0x0401, 0x0201, 0x00ac, 0x00e7, 0x0201, 0x007e, 0x00d9, 0x0401, 0x0201, 0x009d, 0x00e8, 0x0201, 0x008e,
|
||||
0x00cb, 0x0801, 0x0401, 0x0201, 0x00bc, 0x00da, 0x0201, 0x00ad, 0x00e9, 0x0401, 0x0201, 0x009e, 0x00cc, 0x0201, 0x00db, 0x00bd,
|
||||
0x1001, 0x0801, 0x0401, 0x0201, 0x00ea, 0x00ae, 0x0201, 0x00dc, 0x00cd, 0x0401, 0x0201, 0x00eb, 0x00be, 0x0201, 0x00dd, 0x00ec,
|
||||
0x0801, 0x0401, 0x0201, 0x00ce, 0x00ed, 0x0201, 0x00de, 0x00ee, 0x000f, 0x0401, 0x0201, 0x00f0, 0x001f, 0x00f1, 0x0401, 0x0201,
|
||||
0x00f2, 0x002f, 0x0201, 0x00f3, 0x003f, 0x1201, 0x0801, 0x0401, 0x0201, 0x00f4, 0x004f, 0x0201, 0x00f5, 0x005f, 0x0401, 0x0201,
|
||||
0x00f6, 0x006f, 0x0201, 0x00f7, 0x0201, 0x007f, 0x008f, 0x0a01, 0x0401, 0x0201, 0x00f8, 0x00f9, 0x0401, 0x0201, 0x009f, 0x00af,
|
||||
0x00fa, 0x0801, 0x0401, 0x0201, 0x00fb, 0x00bf, 0x0201, 0x00fc, 0x00cf, 0x0401, 0x0201, 0x00fd, 0x00df, 0x0201, 0x00fe, 0x00ef,
|
||||
},
|
||||
[32] = {
|
||||
0x0201, 0x0000, 0x0801, 0x0401, 0x0201, 0x0008, 0x0004, 0x0201, 0x0001, 0x0002, 0x0801, 0x0401, 0x0201, 0x000c, 0x000a, 0x0201,
|
||||
0x0003, 0x0006, 0x0601, 0x0201, 0x0009, 0x0201, 0x0005, 0x0007, 0x0401, 0x0201, 0x000e, 0x000d, 0x0201, 0x000f, 0x000b,
|
||||
},
|
||||
[33] = {
|
||||
0x1001, 0x0801, 0x0401, 0x0201, 0x0000, 0x0001, 0x0201, 0x0002, 0x0003, 0x0401, 0x0201, 0x0004, 0x0005, 0x0201, 0x0006, 0x0007,
|
||||
0x0801, 0x0401, 0x0201, 0x0008, 0x0009, 0x0201, 0x000a, 0x000b, 0x0401, 0x0201, 0x000c, 0x000d, 0x0201, 0x000e, 0x000f,
|
||||
},
|
||||
}
|
||||
return E
|
||||
@@ -0,0 +1,394 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- codecs/audio/Mpa/Encode2: an MPEG audio layer II (MP2) encoder: constant bit rate, MPEG-1 (32 / 44.1 / 48 kHz) and MPEG-2
|
||||
-- low sampling frequencies (16 / 22.05 / 24 kHz), mono or stereo. New code written from ISO/IEC 11172-3 (2.4.2.3,
|
||||
-- C.1.5.2): the polyphase analysis (codecs/audio/Mpa/Analysis), one scalefactor per 12-sample part (scfsi 0: all three are
|
||||
-- sent), the standard bit allocation tables (in minimp3's compact form, CC0: licenses/minimp3-CC0.txt, as the decoder
|
||||
-- in this folder reads them), a greedy allocation that always gives the next step to the band with the most noise
|
||||
-- (signal level minus the quantizer's SNR; no psychoacoustic model), uniform midtread quantization and grouping of
|
||||
-- 3 / 5 / 9-level samples. Never throws.
|
||||
local Bits = require("@formats/core/Bits")
|
||||
local T = require("@formats/codecs/audio/Mpa/Tables")
|
||||
local Analysis = require("@formats/codecs/audio/Mpa/Analysis")
|
||||
|
||||
local Encode2 = {}
|
||||
|
||||
local floor, log = math.floor, math.log
|
||||
local writeu8 = buffer.writeu8
|
||||
|
||||
local POW = table.create(40, 0)
|
||||
for i = 0, 39 do
|
||||
POW[i + 1] = 2 ^ i
|
||||
end
|
||||
|
||||
-- the allocation tables: { bit-allocation-code offset into g_bitalloc_code_tab, bits, band count } runs (minimp3)
|
||||
local ALLOC_L2M1 = { 0, 4, 3, 16, 4, 8, 32, 3, 12, 40, 2, 7 }
|
||||
local ALLOC_L2M1_LOW = { 44, 4, 2, 44, 3, 10 }
|
||||
local ALLOC_L2M2 = { 60, 4, 4, 44, 3, 7, 44, 2, 19 }
|
||||
local CODE = T.g_bitalloc_code_tab
|
||||
|
||||
-- quantizer classes (minimp3's ba): 1..16 -> 2^ba - 1 levels in ba bits; 17 / 18 / 19 -> 3 / 5 / 9 levels grouped
|
||||
local function levels(ba: number): number
|
||||
if ba >= 17 then
|
||||
return if ba == 17 then 3 elseif ba == 18 then 5 else 9
|
||||
end
|
||||
return POW[ba + 1] - 1
|
||||
end
|
||||
-- bits for the 3 samples of one granule
|
||||
local function tripleBits(ba: number): number
|
||||
if ba >= 17 then
|
||||
return if ba == 17 then 5 elseif ba == 18 then 7 else 10
|
||||
end
|
||||
return 3 * ba
|
||||
end
|
||||
-- the quantizer's signal-to-noise ratio in dB (ISO 11172-3 table C.5)
|
||||
local SNR: { [number]: number } = {}
|
||||
do
|
||||
local byLevels = {
|
||||
[3] = 7.00,
|
||||
[5] = 11.00,
|
||||
[7] = 16.00,
|
||||
[9] = 20.84,
|
||||
[15] = 25.28,
|
||||
[31] = 31.59,
|
||||
[63] = 37.75,
|
||||
[127] = 43.84,
|
||||
[255] = 49.89,
|
||||
[511] = 55.93,
|
||||
[1023] = 61.96,
|
||||
[2047] = 67.98,
|
||||
[4095] = 74.01,
|
||||
[8191] = 80.03,
|
||||
[16383] = 86.05,
|
||||
[32767] = 92.01,
|
||||
[65535] = 98.01,
|
||||
}
|
||||
for ba = 1, 19 do
|
||||
SNR[ba] = byLevels[levels(ba)] or (6.02 * ba)
|
||||
end
|
||||
end
|
||||
|
||||
-- scalefactors 2^(1 - i/3), i = 0 .. 62
|
||||
local SCF = table.create(63, 0)
|
||||
for i = 0, 62 do
|
||||
SCF[i + 1] = 2 ^ (1 - i / 3)
|
||||
end
|
||||
|
||||
local RATES = {
|
||||
[44100] = { 3, 0 },
|
||||
[48000] = { 3, 1 },
|
||||
[32000] = { 3, 2 },
|
||||
[22050] = { 2, 0 },
|
||||
[24000] = { 2, 1 },
|
||||
[16000] = { 2, 2 },
|
||||
}
|
||||
Encode2.RATES = RATES
|
||||
local BITRATES_V1 = { 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384 }
|
||||
local BITRATES_V2 = { 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160 }
|
||||
Encode2.BITRATES = { [1] = BITRATES_V1, [2] = BITRATES_V2 }
|
||||
|
||||
type W = Bits.RawWriter
|
||||
local put = Bits.msbPut
|
||||
|
||||
export type Source = {
|
||||
sampleRate: number,
|
||||
channels: number,
|
||||
frames: number,
|
||||
read: (self: any, n: number, out: buffer?) -> (buffer, number),
|
||||
err: any,
|
||||
}
|
||||
export type Options = { bitrate: number, mode: string, prefix: buffer? }
|
||||
|
||||
-- encode(src, opts) -> buffer | nil, message
|
||||
function Encode2.encode(src: Source, opts: Options): (buffer?, string?)
|
||||
local rate = src.sampleRate
|
||||
local info = RATES[rate]
|
||||
if not info then
|
||||
return nil, "unsupported sample rate"
|
||||
end
|
||||
local version, srIndex = info[1], info[2]
|
||||
local mpeg1 = version == 3
|
||||
local inCh = src.channels
|
||||
local nch = if opts.mode == "mono" then 1 else 2
|
||||
local kbps = opts.bitrate
|
||||
local list = if mpeg1 then BITRATES_V1 else BITRATES_V2
|
||||
local brIndex = table.find(list, kbps)
|
||||
if not brIndex then
|
||||
return nil, "unsupported bit rate"
|
||||
end
|
||||
-- the allocation table (the decoder's choice from the header)
|
||||
local alloc, nbands
|
||||
if not mpeg1 then
|
||||
alloc, nbands = ALLOC_L2M2, 30
|
||||
else
|
||||
local perCh = if nch == 2 then kbps // 2 else kbps
|
||||
alloc, nbands = ALLOC_L2M1, 27
|
||||
if perCh < 56 then
|
||||
alloc, nbands = ALLOC_L2M1_LOW, if srIndex == 2 then 12 else 8
|
||||
elseif perCh >= 96 and srIndex ~= 1 then
|
||||
nbands = 30
|
||||
end
|
||||
end
|
||||
local bandOff, bandBits = table.create(nbands, 0), table.create(nbands, 0)
|
||||
do
|
||||
local k, ai = 0, 1
|
||||
local off, bits = 0, 0
|
||||
for i = 0, nbands - 1 do
|
||||
if i == k then
|
||||
off, bits, k = alloc[ai], alloc[ai + 1], k + alloc[ai + 2]
|
||||
ai += 3
|
||||
end
|
||||
bandOff[i + 1], bandBits[i + 1] = off, bits
|
||||
end
|
||||
end
|
||||
-- per band: the classes it can take, in increasing order (index into the code table = the value written)
|
||||
local steps: { { { number } } } = {}
|
||||
for i = 1, nbands do
|
||||
local s = {}
|
||||
for v = 1, POW[bandBits[i] + 1] - 1 do
|
||||
local ba = CODE[bandOff[i] + v]
|
||||
if ba and ba > 0 then
|
||||
table.insert(s, { v, ba })
|
||||
end
|
||||
end
|
||||
steps[i] = s
|
||||
end
|
||||
local w: W = { buf = buffer.create(65536), pos = 0, acc = 0, cnt = 0 }
|
||||
if opts.prefix then
|
||||
local pf = opts.prefix :: buffer
|
||||
if buffer.len(pf) > buffer.len(w.buf) - 16 then
|
||||
w.buf = buffer.create(buffer.len(pf) + 65536)
|
||||
end
|
||||
buffer.copy(w.buf, 0, pf, 0, buffer.len(pf))
|
||||
w.pos = buffer.len(pf)
|
||||
end
|
||||
local ana = { Analysis.new(), Analysis.new() }
|
||||
local pcm = { table.create(1152, 0), table.create(1152, 0) }
|
||||
-- subband samples: sb[c][band * 36 + t + 1], t = 0 .. 35
|
||||
local sbs = { table.create(32 * 36, 0), table.create(32 * 36, 0) }
|
||||
local sub = table.create(32, 0)
|
||||
local scf = { table.create(32 * 3, 0), table.create(32 * 3, 0) } -- scalefactor index per band and part
|
||||
local bal = { table.create(32, 0), table.create(32, 0) } -- allocation step (0 = none) per band
|
||||
local slot = 144000 * kbps
|
||||
local rest = 0
|
||||
local tmp: buffer? = nil
|
||||
local pending: buffer? = nil
|
||||
local pendN, pendOff = 0, 0
|
||||
local srcDone, flushed = false, false
|
||||
while true do
|
||||
local got = 0
|
||||
while got < 1152 and not srcDone do
|
||||
if pendN == 0 then
|
||||
local b, n = src:read(4096, tmp)
|
||||
if n <= 0 then
|
||||
srcDone = true
|
||||
break
|
||||
end
|
||||
tmp = b
|
||||
pending, pendN, pendOff = b, n, 0
|
||||
end
|
||||
local take = math.min(1152 - got, pendN)
|
||||
local pb = pending :: buffer
|
||||
for i = 0, take - 1 do
|
||||
local base = (pendOff + i) * inCh * 4
|
||||
local l = buffer.readf32(pb, base)
|
||||
local r = if inCh > 1 then buffer.readf32(pb, base + 4) else l
|
||||
if nch == 1 then
|
||||
pcm[1][got + i + 1] = if inCh > 1 then (l + r) * 0.5 else l
|
||||
else
|
||||
pcm[1][got + i + 1] = l
|
||||
pcm[2][got + i + 1] = r
|
||||
end
|
||||
end
|
||||
got += take
|
||||
pendN -= take
|
||||
pendOff += take
|
||||
end
|
||||
if got == 0 then
|
||||
if flushed then
|
||||
break
|
||||
end
|
||||
flushed = true
|
||||
end
|
||||
for c = 1, nch do
|
||||
local s = pcm[c]
|
||||
for i = got + 1, 1152 do
|
||||
s[i] = 0
|
||||
end
|
||||
local sb = sbs[c]
|
||||
for t = 0, 35 do
|
||||
Analysis.run(ana[c], s, t * 32 + 1, sub, 1)
|
||||
for b = 0, 31 do
|
||||
sb[b * 36 + t + 1] = sub[b + 1]
|
||||
end
|
||||
end
|
||||
-- scalefactors: the smallest scalefactor (largest index) still >= the part's peak
|
||||
local sc = scf[c]
|
||||
for b = 0, nbands - 1 do
|
||||
for part = 0, 2 do
|
||||
local m = 0
|
||||
local o = b * 36 + part * 12
|
||||
for t = 1, 12 do
|
||||
local v = math.abs(sb[o + t])
|
||||
if v > m then
|
||||
m = v
|
||||
end
|
||||
end
|
||||
local idx = 62
|
||||
if m > 0 then
|
||||
idx = math.clamp(floor(3 * (1 - log(m) / log(2))), 0, 62)
|
||||
while idx > 0 and SCF[idx + 1] < m do
|
||||
idx -= 1
|
||||
end
|
||||
while idx < 62 and SCF[idx + 2] >= m do
|
||||
idx += 1
|
||||
end
|
||||
end
|
||||
sc[b * 3 + part + 1] = idx
|
||||
end
|
||||
end
|
||||
end
|
||||
-- the frame size and the bits left for scalefactors and samples
|
||||
local frameBytes = slot // rate
|
||||
rest += slot % rate
|
||||
local padding = 0
|
||||
if rest >= rate then
|
||||
rest -= rate
|
||||
padding = 1
|
||||
frameBytes += 1
|
||||
end
|
||||
local avail = frameBytes * 8 - 32
|
||||
for b = 1, nbands do
|
||||
avail -= bandBits[b] * nch
|
||||
end
|
||||
-- greedy allocation: the band with the most noise gets its next step while it fits
|
||||
local level = { table.create(nbands, 0), table.create(nbands, 0) } -- signal level (dB) per band
|
||||
for c = 1, nch do
|
||||
local sc = scf[c]
|
||||
for b = 1, nbands do
|
||||
local best = math.min(sc[(b - 1) * 3 + 1], sc[(b - 1) * 3 + 2], sc[(b - 1) * 3 + 3])
|
||||
level[c][b] = 20 * math.log10(SCF[best + 1])
|
||||
bal[c][b] = 0
|
||||
end
|
||||
end
|
||||
while true do
|
||||
local bestC, bestB, bestNoise, bestCost = 0, 0, -math.huge, 0
|
||||
for c = 1, nch do
|
||||
for b = 1, nbands do
|
||||
local st = bal[c][b]
|
||||
local s = steps[b]
|
||||
if st < #s and level[c][b] > -110 then
|
||||
local cur = if st == 0 then 0 else s[st][2]
|
||||
local nxt = s[st + 1][2]
|
||||
local noise = level[c][b] - (if st == 0 then 0 else SNR[cur])
|
||||
local cost = 12 * tripleBits(nxt) - (if st == 0 then 0 else 12 * tripleBits(cur))
|
||||
if st == 0 then
|
||||
cost += 2 + 18 -- scfsi + three scalefactors
|
||||
end
|
||||
if cost <= avail and noise > bestNoise then
|
||||
bestC, bestB, bestNoise, bestCost = c, b, noise, cost
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
if bestC == 0 then
|
||||
break
|
||||
end
|
||||
bal[bestC][bestB] += 1
|
||||
avail -= bestCost
|
||||
end
|
||||
-- write the frame
|
||||
if w.pos + frameBytes + 64 > buffer.len(w.buf) then
|
||||
local nb = buffer.create(buffer.len(w.buf) * 2 + frameBytes)
|
||||
buffer.copy(nb, 0, w.buf, 0, w.pos)
|
||||
w.buf = nb
|
||||
end
|
||||
local start = w.pos
|
||||
put(w, 0x7FF, 11)
|
||||
put(w, version, 2)
|
||||
put(w, 2, 2) -- layer II
|
||||
put(w, 1, 1) -- no CRC
|
||||
put(w, brIndex, 4)
|
||||
put(w, srIndex, 2)
|
||||
put(w, padding, 1)
|
||||
put(w, 0, 1)
|
||||
put(w, if nch == 1 then 3 else 0, 2)
|
||||
put(w, 0, 2)
|
||||
put(w, 0, 1)
|
||||
put(w, 1, 1)
|
||||
put(w, 0, 2)
|
||||
-- bit allocation
|
||||
for b = 1, nbands do
|
||||
for c = 1, nch do
|
||||
local st = bal[c][b]
|
||||
put(w, if st == 0 then 0 else steps[b][st][1], bandBits[b])
|
||||
end
|
||||
end
|
||||
-- scfsi (0: three scalefactors) and the scalefactors
|
||||
for b = 1, nbands do
|
||||
for c = 1, nch do
|
||||
if bal[c][b] > 0 then
|
||||
put(w, 0, 2)
|
||||
end
|
||||
end
|
||||
end
|
||||
for b = 1, nbands do
|
||||
for c = 1, nch do
|
||||
if bal[c][b] > 0 then
|
||||
for part = 1, 3 do
|
||||
put(w, scf[c][(b - 1) * 3 + part], 6)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
-- samples: 12 granules of 3, band by band, channel by channel
|
||||
for gr = 0, 11 do
|
||||
local part = gr // 4
|
||||
for b = 1, nbands do
|
||||
for c = 1, nch do
|
||||
local st = bal[c][b]
|
||||
if st > 0 then
|
||||
local ba = steps[b][st][2]
|
||||
local L = levels(ba)
|
||||
local inv = 1 / SCF[scf[c][(b - 1) * 3 + part + 1] + 1]
|
||||
local o = (b - 1) * 36 + gr * 3
|
||||
local sb = sbs[c]
|
||||
local q0, q1, q2
|
||||
do
|
||||
local function quant(x: number): number
|
||||
local q = floor((x * inv + 1) * L / 2)
|
||||
return if q < 0 then 0 elseif q > L - 1 then L - 1 else q
|
||||
end
|
||||
q0 = quant(sb[o + 1])
|
||||
q1 = quant(sb[o + 2])
|
||||
q2 = quant(sb[o + 3])
|
||||
end
|
||||
if ba >= 17 then
|
||||
put(w, q0 + L * (q1 + L * q2), tripleBits(ba))
|
||||
else
|
||||
put(w, q0, ba)
|
||||
put(w, q1, ba)
|
||||
put(w, q2, ba)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
if w.cnt > 0 then
|
||||
put(w, 0, 8 - w.cnt)
|
||||
end
|
||||
while w.pos < start + frameBytes do
|
||||
writeu8(w.buf, w.pos, 0)
|
||||
w.pos += 1
|
||||
end
|
||||
end
|
||||
if src.err then
|
||||
return nil, tostring(src.err)
|
||||
end
|
||||
local out = buffer.create(w.pos)
|
||||
buffer.copy(out, 0, w.buf, 0, w.pos)
|
||||
return out, nil
|
||||
end
|
||||
|
||||
return Encode2
|
||||
@@ -0,0 +1,665 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- codecs/audio/Mpa/Encode3: an MPEG audio layer III encoder in the spirit of shine: constant bit rate, long blocks only,
|
||||
-- global-gain rate control (no psychoacoustic model, scalefactors all zero), no bit reservoir (main_data_begin = 0),
|
||||
-- MPEG-1 (32 / 44.1 / 48 kHz, two granules), MPEG-2 (16 / 22.05 / 24 kHz) and MPEG-2.5 (8 / 11.025 / 12 kHz), mono,
|
||||
-- stereo or joint (mid / side) stereo. New code written from ISO/IEC 11172-3 / 13818-3 (no shine / LAME code): the
|
||||
-- analysis filterbank (codecs/audio/Mpa/Analysis), the 36-point MDCT with the sine window, the alias-reduction butterflies,
|
||||
-- the x^(3/4) quantizer, big_values / count1 partitioning with per-region Huffman table choice; the Huffman codes are
|
||||
-- derived from the standard's decoder trees (codecs/audio/Mpa/EncTables, from PDMP3, public domain). Never throws.
|
||||
local Bits = require("@formats/core/Bits")
|
||||
local E = require("@formats/codecs/audio/Mpa/EncTables")
|
||||
local T = require("@formats/codecs/audio/Mpa/Tables")
|
||||
local Analysis = require("@formats/codecs/audio/Mpa/Analysis")
|
||||
|
||||
local Encode3 = {}
|
||||
|
||||
local floor, sqrt, cos, pi = math.floor, math.sqrt, math.cos, math.pi
|
||||
local writeu8 = buffer.writeu8
|
||||
|
||||
local POW = table.create(40, 0)
|
||||
for i = 0, 39 do
|
||||
POW[i + 1] = 2 ^ i
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ Huffman codes
|
||||
-- CODE[t][x * 16 + y + 1], LEN[t][...] for the pair tables 1..31 (16..23 and 24..31 share a tree); count1 tables A
|
||||
-- (32) and B (33) indexed by v * 8 + w * 4 + x * 2 + y + 1
|
||||
local CODE: { [number]: { number } } = {}
|
||||
local LEN: { [number]: { number } } = {}
|
||||
local XLEN: { [number]: number } = {}
|
||||
|
||||
local function walk(tree: { number }, codes: { number }, lens: { number }, p: number, code: number, len: number)
|
||||
local v = tree[p + 1]
|
||||
if bit32.band(v, 0xFF00) == 0 then
|
||||
local idx = bit32.band(v, 0xFF) + 1
|
||||
if lens[idx] == 0 then
|
||||
codes[idx], lens[idx] = code, len
|
||||
end
|
||||
return
|
||||
end
|
||||
-- left (bit 0): offsets of 250+ chain
|
||||
local q = p
|
||||
while bit32.rshift(tree[q + 1], 8) >= 250 do
|
||||
q += bit32.rshift(tree[q + 1], 8)
|
||||
end
|
||||
q += bit32.rshift(tree[q + 1], 8)
|
||||
walk(tree, codes, lens, q, code * 2, len + 1)
|
||||
q = p
|
||||
while bit32.band(tree[q + 1], 0xFF) >= 250 do
|
||||
q += bit32.band(tree[q + 1], 0xFF)
|
||||
end
|
||||
q += bit32.band(tree[q + 1], 0xFF)
|
||||
walk(tree, codes, lens, q, code * 2 + 1, len + 1)
|
||||
end
|
||||
|
||||
for t = 1, 33 do
|
||||
local src = if t >= 16 and t <= 23 then 16 elseif t >= 24 and t <= 31 then 24 else t
|
||||
local tree = E.TREES[src]
|
||||
if tree then
|
||||
local codes, lens = table.create(256, 0), table.create(256, 0)
|
||||
walk(tree, codes, lens, 0, 0, 0)
|
||||
-- the leaf value of pair tables is x << 4 | y: index x * 16 + y + 1 (count1: the 4-bit vwxy)
|
||||
CODE[t], LEN[t] = codes, lens
|
||||
local xl = 0
|
||||
for idx = 1, 256 do
|
||||
if lens[idx] > 0 then
|
||||
local x = (idx - 1) // 16
|
||||
if x + 1 > xl then
|
||||
xl = x + 1
|
||||
end
|
||||
end
|
||||
end
|
||||
XLEN[t] = xl
|
||||
end
|
||||
end
|
||||
local LINBITS = E.LINBITS
|
||||
-- the tables that hold values up to m (m <= 15), smallest first
|
||||
local SMALL: { [number]: { number } } = {}
|
||||
for m = 0, 15 do
|
||||
local list = {}
|
||||
for _, t in { 1, 2, 3, 5, 6, 7, 8, 9, 10, 11, 12, 13, 15 } do
|
||||
if XLEN[t] and XLEN[t] - 1 >= m then
|
||||
table.insert(list, t)
|
||||
end
|
||||
end
|
||||
SMALL[m] = list
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ bit writer
|
||||
type W = Bits.RawWriter
|
||||
|
||||
local put = Bits.msbPut
|
||||
Encode3.put = put
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ constants
|
||||
local RATES = {
|
||||
-- rate -> version bits (3 MPEG-1, 2 MPEG-2, 0 MPEG-2.5), sample rate index, scalefactor band group (g_scf_long)
|
||||
[44100] = { 3, 0, 5 },
|
||||
[48000] = { 3, 1, 6 },
|
||||
[32000] = { 3, 2, 7 },
|
||||
[22050] = { 2, 0, 2 },
|
||||
[24000] = { 2, 1, 3 },
|
||||
[16000] = { 2, 2, 4 },
|
||||
[11025] = { 0, 0, 0 },
|
||||
[12000] = { 0, 1, 0 },
|
||||
[8000] = { 0, 2, 1 },
|
||||
}
|
||||
Encode3.RATES = RATES
|
||||
local BITRATES_V1 = { 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320 }
|
||||
local BITRATES_V2 = { 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160 }
|
||||
Encode3.BITRATES = { [1] = BITRATES_V1, [2] = BITRATES_V2 }
|
||||
|
||||
-- the 36-point MDCT (long blocks): X[k] = 1/9 sum_i w_i z_i cos(pi/72 (2i + 19)(2k + 1)), w_i = sin(pi/36 (i + 1/2))
|
||||
local MDCT = table.create(18 * 36, 0)
|
||||
for k = 0, 17 do
|
||||
for i = 0, 35 do
|
||||
MDCT[k * 36 + i + 1] = math.sin(pi / 36 * (i + 0.5)) * cos(pi / 72 * (2 * i + 19) * (2 * k + 1)) / 9
|
||||
end
|
||||
end
|
||||
-- alias reduction coefficients
|
||||
local CI = { -0.6, -0.535, -0.33, -0.185, -0.095, -0.041, -0.0142, -0.0037 }
|
||||
local CS, CA = table.create(8, 0), table.create(8, 0)
|
||||
for i = 1, 8 do
|
||||
local s = sqrt(1 + CI[i] * CI[i])
|
||||
CS[i], CA[i] = 1 / s, CI[i] / s
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ Huffman planning
|
||||
type Plan = {
|
||||
bits: number,
|
||||
bigValues: number, -- pairs
|
||||
count1: number, -- quadruples
|
||||
tables: { number }, -- 3 region tables
|
||||
region0: number,
|
||||
region1: number,
|
||||
c1table: number, -- 0 = A (table 32), 1 = B (33)
|
||||
addr1: number, -- coefficient index where region 1 starts
|
||||
addr2: number,
|
||||
}
|
||||
|
||||
local function newPlan(): Plan
|
||||
return { bits = 0, bigValues = 0, count1 = 0, tables = { 0, 0, 0 }, region0 = 0, region1 = 0, c1table = 0, addr1 = 0, addr2 = 0 }
|
||||
end
|
||||
|
||||
-- bits of the pairs ix[a .. b) with table t (ix 1-based: ix[i + 1] is coefficient i)
|
||||
local function pairBits(ix: { number }, a: number, b: number, t: number): number
|
||||
if t == 0 then
|
||||
return 0
|
||||
end
|
||||
local lens = LEN[t]
|
||||
local lb = LINBITS[t]
|
||||
local bits = 0
|
||||
for i = a, b - 1, 2 do
|
||||
local x, y = ix[i + 1], ix[i + 2]
|
||||
local extra = 0
|
||||
if x > 14 then
|
||||
x = 15
|
||||
extra += lb
|
||||
end
|
||||
if y > 14 then
|
||||
y = 15
|
||||
extra += lb
|
||||
end
|
||||
bits += lens[x * 16 + y + 1] + extra
|
||||
if x ~= 0 then
|
||||
bits += 1
|
||||
end
|
||||
if y ~= 0 then
|
||||
bits += 1
|
||||
end
|
||||
end
|
||||
return bits
|
||||
end
|
||||
|
||||
-- the best table for the pairs ix[a .. b): table, bits
|
||||
local function chooseTable(ix: { number }, a: number, b: number): (number, number)
|
||||
if b <= a then
|
||||
return 0, 0
|
||||
end
|
||||
local m = 0
|
||||
for i = a + 1, b do
|
||||
local v = ix[i]
|
||||
if v > m then
|
||||
m = v
|
||||
end
|
||||
end
|
||||
if m == 0 then
|
||||
return 0, 0
|
||||
end
|
||||
if m <= 15 then
|
||||
local best, bestBits = 0, math.huge
|
||||
for _, t in SMALL[m] do
|
||||
local bits = pairBits(ix, a, b, t)
|
||||
if bits < bestBits then
|
||||
best, bestBits = t, bits
|
||||
end
|
||||
end
|
||||
return best, bestBits
|
||||
end
|
||||
local need = m - 15
|
||||
local best, bestBits = 0, math.huge
|
||||
for _, group in { 16, 24 } do
|
||||
for t = group, group + 7 do
|
||||
if POW[LINBITS[t] + 1] - 1 >= need then
|
||||
local bits = pairBits(ix, a, b, t)
|
||||
if bits < bestBits then
|
||||
best, bestBits = t, bits
|
||||
end
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
return best, bestBits
|
||||
end
|
||||
|
||||
-- plan the Huffman coding of ix (576 values, max <= 8206) into p; bounds = scalefactor band starts (1-based list)
|
||||
local function plan(ix: { number }, bounds: { number }, p: Plan)
|
||||
local i = 576
|
||||
while i > 1 and ix[i] == 0 and ix[i - 1] == 0 do
|
||||
i -= 2
|
||||
end
|
||||
-- count1 region: quadruples of values <= 1
|
||||
local c1 = 0
|
||||
while i > 3 and ix[i] <= 1 and ix[i - 1] <= 1 and ix[i - 2] <= 1 and ix[i - 3] <= 1 do
|
||||
i -= 4
|
||||
c1 += 1
|
||||
end
|
||||
local bv = i -- big_values * 2 coefficients
|
||||
p.bigValues, p.count1 = bv // 2, c1
|
||||
-- count1 bits, table A vs B
|
||||
local ba, bb = 0, 0
|
||||
local lensA = LEN[32]
|
||||
for q = 0, c1 - 1 do
|
||||
local o = bv + q * 4
|
||||
local v, w, x, y = ix[o + 1], ix[o + 2], ix[o + 3], ix[o + 4]
|
||||
local s = v + w + x + y
|
||||
ba += lensA[v * 8 + w * 4 + x * 2 + y + 1] + s
|
||||
bb += 4 + s
|
||||
end
|
||||
p.c1table = if bb < ba then 1 else 0
|
||||
local bits = if bb < ba then bb else ba
|
||||
-- regions: region 0 / 1 / 2 split at scalefactor band boundaries
|
||||
local j = 0
|
||||
while j + 1 < #bounds and bounds[j + 2] <= bv do
|
||||
j += 1
|
||||
end
|
||||
local r0 = math.clamp(floor((j + 1) / 3), 0, 15)
|
||||
local r1 = math.clamp(floor((j + 1) / 3), 0, 7)
|
||||
if r0 + r1 + 2 > 22 then
|
||||
r1 = 22 - r0 - 2
|
||||
end
|
||||
local a1 = math.min(bounds[r0 + 2] or 576, bv)
|
||||
local a2 = math.min(bounds[r0 + r1 + 3] or 576, bv)
|
||||
p.region0, p.region1, p.addr1, p.addr2 = r0, r1, a1, a2
|
||||
local t1, b1 = chooseTable(ix, 0, a1)
|
||||
local t2, b2 = chooseTable(ix, a1, a2)
|
||||
local t3, b3 = chooseTable(ix, a2, bv)
|
||||
p.tables[1], p.tables[2], p.tables[3] = t1, t2, t3
|
||||
p.bits = bits + b1 + b2 + b3
|
||||
end
|
||||
|
||||
local function writePairs(w: W, ix: { number }, sign: { boolean }, a: number, b: number, t: number)
|
||||
if t == 0 then
|
||||
return
|
||||
end
|
||||
local codes, lens = CODE[t], LEN[t]
|
||||
local lb = LINBITS[t]
|
||||
for i = a, b - 1, 2 do
|
||||
local x, y = ix[i + 1], ix[i + 2]
|
||||
local cx, cy = if x > 14 then 15 else x, if y > 14 then 15 else y
|
||||
local idx = cx * 16 + cy + 1
|
||||
put(w, codes[idx], lens[idx])
|
||||
if cx == 15 then
|
||||
put(w, x - 15, lb)
|
||||
end
|
||||
if x ~= 0 then
|
||||
put(w, if sign[i + 1] then 1 else 0, 1)
|
||||
end
|
||||
if cy == 15 then
|
||||
put(w, y - 15, lb)
|
||||
end
|
||||
if y ~= 0 then
|
||||
put(w, if sign[i + 2] then 1 else 0, 1)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local function writeHuffman(w: W, ix: { number }, sign: { boolean }, p: Plan)
|
||||
local bv = p.bigValues * 2
|
||||
writePairs(w, ix, sign, 0, p.addr1, p.tables[1])
|
||||
writePairs(w, ix, sign, p.addr1, p.addr2, p.tables[2])
|
||||
writePairs(w, ix, sign, p.addr2, bv, p.tables[3])
|
||||
local codesA, lensA = CODE[32], LEN[32]
|
||||
for q = 0, p.count1 - 1 do
|
||||
local o = bv + q * 4
|
||||
local v, ww, x, y = ix[o + 1], ix[o + 2], ix[o + 3], ix[o + 4]
|
||||
local idx = v * 8 + ww * 4 + x * 2 + y
|
||||
if p.c1table == 1 then
|
||||
put(w, 15 - idx, 4) -- table B: the 4 bits inverted
|
||||
else
|
||||
put(w, codesA[idx + 1], lensA[idx + 1])
|
||||
end
|
||||
for k = 1, 4 do
|
||||
if ix[o + k] ~= 0 then
|
||||
put(w, if sign[o + k] then 1 else 0, 1)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ quantization
|
||||
type Unit = {
|
||||
xr: { number }, -- 576 MDCT coefficients
|
||||
xa: { number }, -- |xr|^(3/4)
|
||||
ix: { number },
|
||||
sign: { boolean },
|
||||
plan: Plan,
|
||||
gain: number,
|
||||
}
|
||||
|
||||
local function newUnit(): Unit
|
||||
return {
|
||||
xr = table.create(576, 0),
|
||||
xa = table.create(576, 0),
|
||||
ix = table.create(576, 0),
|
||||
sign = table.create(576, false),
|
||||
plan = newPlan(),
|
||||
gain = 210,
|
||||
}
|
||||
end
|
||||
|
||||
-- quantize with global gain g: false when a value overflows 8206
|
||||
local function quantize(u: Unit, g: number): boolean
|
||||
local step = 2 ^ (-(g - 210) * 3 / 16)
|
||||
local xa, ix = u.xa, u.ix
|
||||
for i = 1, 576 do
|
||||
local v = floor(xa[i] * step + 0.4054)
|
||||
if v > 8206 then
|
||||
return false
|
||||
end
|
||||
ix[i] = v
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
-- the finest global gain whose Huffman size fits budget bits
|
||||
local function rateLoop(u: Unit, budget: number, bounds: { number })
|
||||
local xr, xa, sign = u.xr, u.xa, u.sign
|
||||
local maxa = 0
|
||||
for i = 1, 576 do
|
||||
local v = xr[i]
|
||||
sign[i] = v < 0
|
||||
local a = math.abs(v) ^ 0.75
|
||||
xa[i] = a
|
||||
if a > maxa then
|
||||
maxa = a
|
||||
end
|
||||
end
|
||||
local p = u.plan
|
||||
if maxa < 1e-12 then
|
||||
for i = 1, 576 do
|
||||
u.ix[i] = 0
|
||||
end
|
||||
plan(u.ix, bounds, p)
|
||||
u.gain = 0
|
||||
return
|
||||
end
|
||||
-- the gain where the largest value quantizes to 0: an upper bound
|
||||
local lo, hi = 0, 255
|
||||
local best = -1
|
||||
while lo <= hi do
|
||||
local mid = (lo + hi) // 2
|
||||
if quantize(u, mid) then
|
||||
plan(u.ix, bounds, p)
|
||||
if p.bits <= budget then
|
||||
best = mid
|
||||
hi = mid - 1
|
||||
else
|
||||
lo = mid + 1
|
||||
end
|
||||
else
|
||||
lo = mid + 1
|
||||
end
|
||||
end
|
||||
if best < 0 then
|
||||
best = 255
|
||||
end
|
||||
quantize(u, best)
|
||||
plan(u.ix, bounds, p)
|
||||
if p.bits > budget then -- give up on this granule: silence fits any budget
|
||||
for i = 1, 576 do
|
||||
u.ix[i] = 0
|
||||
end
|
||||
plan(u.ix, bounds, p)
|
||||
end
|
||||
u.gain = best
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ encoder
|
||||
export type Source = {
|
||||
sampleRate: number,
|
||||
channels: number,
|
||||
frames: number,
|
||||
read: (self: any, n: number, out: buffer?) -> (buffer, number),
|
||||
err: any,
|
||||
}
|
||||
export type Options = {
|
||||
bitrate: number, -- kbps, valid for the version
|
||||
mode: string, -- "stereo" | "joint" | "mono"
|
||||
prefix: buffer?, -- bytes written before the first frame (an ID3v2 tag)
|
||||
}
|
||||
|
||||
-- encode(src, opts) -> buffer | nil, message
|
||||
function Encode3.encode(src: Source, opts: Options): (buffer?, string?)
|
||||
local rate = src.sampleRate
|
||||
local info = RATES[rate]
|
||||
if not info then
|
||||
return nil, "unsupported sample rate"
|
||||
end
|
||||
local version, srIndex, group = info[1], info[2], info[3]
|
||||
local mpeg1 = version == 3
|
||||
local granules = if mpeg1 then 2 else 1
|
||||
local spf = 576 * granules
|
||||
local inCh = src.channels
|
||||
local mode = opts.mode
|
||||
local nch = if mode == "mono" then 1 else 2
|
||||
local kbps = opts.bitrate
|
||||
local list = if mpeg1 then BITRATES_V1 else BITRATES_V2
|
||||
local brIndex = table.find(list, kbps)
|
||||
if not brIndex then
|
||||
return nil, "unsupported bit rate"
|
||||
end
|
||||
local sideBytes = if mpeg1 then (if nch == 1 then 17 else 32) else (if nch == 1 then 9 else 17)
|
||||
local slotNum = (if mpeg1 then 144000 else 72000) * kbps -- frame bytes * rate
|
||||
-- scalefactor band starts (long blocks)
|
||||
local bounds = { 0 }
|
||||
do
|
||||
local acc = 0
|
||||
for k = 0, 21 do
|
||||
acc += T.g_scf_long[group * 23 + k]
|
||||
table.insert(bounds, acc)
|
||||
end
|
||||
end
|
||||
local w: W = { buf = buffer.create(65536), pos = 0, acc = 0, cnt = 0 }
|
||||
if opts.prefix then
|
||||
local pf = opts.prefix :: buffer
|
||||
local need = buffer.len(pf)
|
||||
if need > buffer.len(w.buf) - 16 then
|
||||
w.buf = buffer.create(need + 65536)
|
||||
end
|
||||
buffer.copy(w.buf, 0, pf, 0, need)
|
||||
w.pos = need
|
||||
end
|
||||
local ana = { Analysis.new(), Analysis.new() }
|
||||
local prev = { table.create(576, 0), table.create(576, 0) } -- previous granule's subband samples [sb * 18 + k + 1]
|
||||
local cur = { table.create(576, 0), table.create(576, 0) }
|
||||
local units = {}
|
||||
for g = 1, 2 do
|
||||
units[g] = { newUnit(), newUnit() }
|
||||
end
|
||||
local pcm = { table.create(spf, 0), table.create(spf, 0) }
|
||||
local sub = table.create(32, 0)
|
||||
local z = table.create(36, 0)
|
||||
local rest = 0
|
||||
local tmp: buffer? = nil
|
||||
local pending: buffer? = nil
|
||||
local pendN, pendOff = 0, 0
|
||||
local srcDone = false
|
||||
local flushed = false
|
||||
while true do
|
||||
-- one frame of input (zero padded at the end; one more silent frame flushes the filterbank delay)
|
||||
local got = 0
|
||||
while got < spf and not srcDone do
|
||||
if pendN == 0 then
|
||||
local b, n = src:read(4096, tmp)
|
||||
if n <= 0 then
|
||||
srcDone = true
|
||||
break
|
||||
end
|
||||
tmp = b
|
||||
pending, pendN, pendOff = b, n, 0
|
||||
end
|
||||
local take = math.min(spf - got, pendN)
|
||||
local pb = pending :: buffer
|
||||
for i = 0, take - 1 do
|
||||
local base = (pendOff + i) * inCh * 4
|
||||
local l = buffer.readf32(pb, base)
|
||||
local r = if inCh > 1 then buffer.readf32(pb, base + 4) else l
|
||||
if nch == 1 then
|
||||
if inCh > 1 then
|
||||
l = (l + r) * 0.5
|
||||
end
|
||||
pcm[1][got + i + 1] = l
|
||||
else
|
||||
pcm[1][got + i + 1] = l
|
||||
pcm[2][got + i + 1] = r
|
||||
end
|
||||
end
|
||||
got += take
|
||||
pendN -= take
|
||||
pendOff += take
|
||||
end
|
||||
if got == 0 then
|
||||
if flushed then
|
||||
break
|
||||
end
|
||||
flushed = true -- one silent frame after the input pushes its tail through the filterbank delay
|
||||
end
|
||||
for c = 1, nch do
|
||||
local s = pcm[c]
|
||||
for i = got + 1, spf do
|
||||
s[i] = 0
|
||||
end
|
||||
end
|
||||
-- analysis + MDCT per granule and channel
|
||||
for g = 1, granules do
|
||||
for c = 1, nch do
|
||||
local cu, pv = cur[c], prev[c]
|
||||
local s = pcm[c]
|
||||
for k = 0, 17 do
|
||||
Analysis.run(ana[c], s, (g - 1) * 576 + k * 32 + 1, sub, 1)
|
||||
for sb = 0, 31 do
|
||||
local v = sub[sb + 1]
|
||||
if sb % 2 == 1 and k % 2 == 1 then
|
||||
v = -v -- frequency inversion of the odd subbands
|
||||
end
|
||||
cu[sb * 18 + k + 1] = v
|
||||
end
|
||||
end
|
||||
local xr = units[g][c].xr
|
||||
for sb = 0, 31 do
|
||||
local o = sb * 18
|
||||
for i = 1, 18 do
|
||||
z[i] = pv[o + i]
|
||||
z[i + 18] = cu[o + i]
|
||||
end
|
||||
for k = 0, 17 do
|
||||
local acc = 0
|
||||
local mb = k * 36
|
||||
for i = 1, 36 do
|
||||
acc += MDCT[mb + i] * z[i]
|
||||
end
|
||||
xr[o + k + 1] = acc
|
||||
end
|
||||
end
|
||||
-- alias reduction butterflies (the inverse of the decoder's)
|
||||
for sb = 1, 31 do
|
||||
local o = sb * 18
|
||||
for i = 0, 7 do
|
||||
local a, b = xr[o - i], xr[o + i + 1]
|
||||
xr[o - i] = a * CS[i + 1] + b * CA[i + 1]
|
||||
xr[o + i + 1] = b * CS[i + 1] - a * CA[i + 1]
|
||||
end
|
||||
end
|
||||
prev[c], cur[c] = cu, pv
|
||||
end
|
||||
if mode == "joint" then
|
||||
local x0, x1 = units[g][1].xr, units[g][2].xr
|
||||
local r2 = sqrt(0.5)
|
||||
for i = 1, 576 do
|
||||
local l, r = x0[i], x1[i]
|
||||
x0[i], x1[i] = (l + r) * r2, (l - r) * r2
|
||||
end
|
||||
end
|
||||
end
|
||||
-- the frame size
|
||||
local frameBytes = slotNum // rate
|
||||
rest += slotNum % rate
|
||||
local padding = 0
|
||||
if rest >= rate then
|
||||
rest -= rate
|
||||
padding = 1
|
||||
frameBytes += 1
|
||||
end
|
||||
local avail = (frameBytes - 4 - sideBytes) * 8
|
||||
local left = avail
|
||||
local nUnits = granules * nch
|
||||
local done = 0
|
||||
for g = 1, granules do
|
||||
for c = 1, nch do
|
||||
local budget = math.min(4095, left // (nUnits - done))
|
||||
local u = units[g][c]
|
||||
rateLoop(u, budget, bounds)
|
||||
left -= u.plan.bits
|
||||
done += 1
|
||||
end
|
||||
end
|
||||
-- header
|
||||
if w.pos + frameBytes + 64 > buffer.len(w.buf) then
|
||||
local nb = buffer.create(buffer.len(w.buf) * 2 + frameBytes)
|
||||
buffer.copy(nb, 0, w.buf, 0, w.pos)
|
||||
w.buf = nb
|
||||
end
|
||||
local start = w.pos
|
||||
put(w, 0x7FF, 11)
|
||||
put(w, version, 2)
|
||||
put(w, 1, 2) -- layer III
|
||||
put(w, 1, 1) -- no CRC
|
||||
put(w, brIndex, 4)
|
||||
put(w, srIndex, 2)
|
||||
put(w, padding, 1)
|
||||
put(w, 0, 1)
|
||||
put(w, if mode == "mono" then 3 elseif mode == "joint" then 1 else 0, 2)
|
||||
put(w, if mode == "joint" then 2 else 0, 2) -- mode extension: mid / side on, intensity off
|
||||
put(w, 0, 1)
|
||||
put(w, 1, 1) -- original
|
||||
put(w, 0, 2)
|
||||
-- side info
|
||||
if mpeg1 then
|
||||
put(w, 0, 9) -- main_data_begin
|
||||
put(w, 0, if nch == 1 then 5 else 3)
|
||||
for _ = 1, nch do
|
||||
put(w, 0, 4) -- scfsi
|
||||
end
|
||||
else
|
||||
put(w, 0, 8)
|
||||
put(w, 0, if nch == 1 then 1 else 2)
|
||||
end
|
||||
for g = 1, granules do
|
||||
for c = 1, nch do
|
||||
local u = units[g][c]
|
||||
local p = u.plan
|
||||
put(w, p.bits, 12) -- part2_3_length (no scalefactor bits)
|
||||
put(w, p.bigValues, 9)
|
||||
put(w, u.gain, 8)
|
||||
put(w, 0, if mpeg1 then 4 else 9) -- scalefac_compress
|
||||
put(w, 0, 1) -- window switching off
|
||||
put(w, p.tables[1], 5)
|
||||
put(w, p.tables[2], 5)
|
||||
put(w, p.tables[3], 5)
|
||||
put(w, p.region0, 4)
|
||||
put(w, p.region1, 3)
|
||||
if mpeg1 then
|
||||
put(w, 0, 1) -- preflag
|
||||
end
|
||||
put(w, 0, 1) -- scalefac_scale
|
||||
put(w, p.c1table, 1)
|
||||
end
|
||||
end
|
||||
-- main data
|
||||
for g = 1, granules do
|
||||
for c = 1, nch do
|
||||
local u = units[g][c]
|
||||
writeHuffman(w, u.ix, u.sign, u.plan)
|
||||
end
|
||||
end
|
||||
if w.cnt > 0 then
|
||||
put(w, 0, 8 - w.cnt)
|
||||
end
|
||||
-- ancillary padding to the frame size
|
||||
while w.pos < start + frameBytes do
|
||||
writeu8(w.buf, w.pos, 0)
|
||||
w.pos += 1
|
||||
end
|
||||
end
|
||||
if src.err then
|
||||
return nil, tostring(src.err)
|
||||
end
|
||||
local out = buffer.create(w.pos)
|
||||
buffer.copy(out, 0, w.buf, 0, w.pos)
|
||||
return out, nil
|
||||
end
|
||||
|
||||
return Encode3
|
||||
@@ -0,0 +1,140 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- codecs/audio/Mpa/Header: MPEG audio frame headers (minimp3's hdr_* macros and functions), frame sync (mp3d_find_frame /
|
||||
-- mp3d_match_frame). Every function reads 4 header bytes at a byte offset of a buffer; callers keep offset + 4
|
||||
-- inside the buffer. Never throws.
|
||||
local T = require("@formats/codecs/audio/Mpa/Tables")
|
||||
|
||||
local band, bxor, rshift = bit32.band, bit32.bxor, bit32.rshift
|
||||
local ru8 = buffer.readu8
|
||||
|
||||
local HALFRATE = T.halfrate
|
||||
local HZ = { [0] = 44100, 48000, 32000 }
|
||||
|
||||
local H = {}
|
||||
|
||||
H.MAX_FREE_FORMAT_FRAME_SIZE = 2304
|
||||
H.MAX_FRAME_SYNC_MATCHES = 10
|
||||
|
||||
-- hdr_valid
|
||||
function H.valid(b: buffer, o: number): boolean
|
||||
if ru8(b, o) ~= 0xFF then
|
||||
return false
|
||||
end
|
||||
local h1, h2 = ru8(b, o + 1), ru8(b, o + 2)
|
||||
return (band(h1, 0xF0) == 0xF0 or band(h1, 0xFE) == 0xE2)
|
||||
and band(rshift(h1, 1), 3) ~= 0
|
||||
and rshift(h2, 4) ~= 15
|
||||
and band(rshift(h2, 2), 3) ~= 3
|
||||
end
|
||||
local valid = H.valid
|
||||
|
||||
-- hdr_compare: h2 is a valid header of the same version / layer / sample rate / free-format-ness as h1
|
||||
function H.compare(b: buffer, o1: number, o2: number): boolean
|
||||
if not valid(b, o2) then
|
||||
return false
|
||||
end
|
||||
local a1, a2 = ru8(b, o1 + 1), ru8(b, o1 + 2)
|
||||
local c1, c2 = ru8(b, o2 + 1), ru8(b, o2 + 2)
|
||||
return band(bxor(a1, c1), 0xFE) == 0
|
||||
and band(bxor(a2, c2), 0x0C) == 0
|
||||
and (band(a2, 0xF0) == 0) == (band(c2, 0xF0) == 0)
|
||||
end
|
||||
local compare = H.compare
|
||||
|
||||
-- from the header bytes h1 = byte 1, h2 = byte 2
|
||||
function H.bitrateKbps(h1: number, h2: number): number
|
||||
local mpeg1 = if band(h1, 8) ~= 0 then 1 else 0
|
||||
local layer = band(rshift(h1, 1), 3)
|
||||
return 2 * HALFRATE[mpeg1 * 45 + (layer - 1) * 15 + rshift(h2, 4)]
|
||||
end
|
||||
local bitrateKbps = H.bitrateKbps
|
||||
|
||||
function H.sampleRate(h1: number, h2: number): number
|
||||
local hz = HZ[band(rshift(h2, 2), 3)]
|
||||
if band(h1, 8) == 0 then
|
||||
hz //= 2
|
||||
end
|
||||
if band(h1, 0x10) == 0 then
|
||||
hz //= 2
|
||||
end
|
||||
return hz
|
||||
end
|
||||
local sampleRate = H.sampleRate
|
||||
|
||||
function H.frameSamples(h1: number): number
|
||||
if band(h1, 6) == 6 then
|
||||
return 384
|
||||
elseif band(h1, 14) == 2 then
|
||||
return 576
|
||||
end
|
||||
return 1152
|
||||
end
|
||||
local frameSamples = H.frameSamples
|
||||
|
||||
-- hdr_frame_bytes (without padding); free format = 0 -> freeSize
|
||||
function H.frameBytes(b: buffer, o: number, freeSize: number): number
|
||||
local h1, h2 = ru8(b, o + 1), ru8(b, o + 2)
|
||||
local hz = sampleRate(h1, h2)
|
||||
local fb = frameSamples(h1) * bitrateKbps(h1, h2) * 125 // hz
|
||||
if band(h1, 6) == 6 then
|
||||
fb = band(fb, 0xFFFFFFFC)
|
||||
end
|
||||
return if fb ~= 0 then fb else freeSize
|
||||
end
|
||||
local frameBytes = H.frameBytes
|
||||
|
||||
function H.padding(b: buffer, o: number): number
|
||||
if band(ru8(b, o + 2), 2) == 0 then
|
||||
return 0
|
||||
end
|
||||
return if band(ru8(b, o + 1), 6) == 6 then 4 else 1
|
||||
end
|
||||
local padding = H.padding
|
||||
|
||||
-- mp3d_match_frame: the frames chained after o (n bytes left) keep comparing equal
|
||||
local function matchFrame(b: buffer, o: number, n: number, fb: number): boolean
|
||||
local i = 0
|
||||
for nmatch = 0, H.MAX_FRAME_SYNC_MATCHES - 1 do
|
||||
i += frameBytes(b, o + i, fb) + padding(b, o + i)
|
||||
if i + 4 > n then
|
||||
return nmatch > 0
|
||||
end
|
||||
if not compare(b, o, o + i) then
|
||||
return false
|
||||
end
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
-- mp3d_find_frame over b[base, base + n): returns (skip, frame bytes incl. padding or 0, free format size)
|
||||
function H.findFrame(b: buffer, base: number, n: number, ffb: number): (number, number, number)
|
||||
for i = 0, n - 5 do
|
||||
local o = base + i
|
||||
if valid(b, o) then
|
||||
local fb = frameBytes(b, o, ffb)
|
||||
local fap = fb + padding(b, o)
|
||||
local k = 4
|
||||
while fb == 0 and k < H.MAX_FREE_FORMAT_FRAME_SIZE and i + 2 * k < n - 4 do
|
||||
if compare(b, o, o + k) then
|
||||
local f = k - padding(b, o)
|
||||
local nextfb = f + padding(b, o + k)
|
||||
if not (i + k + nextfb + 4 > n or not compare(b, o, o + k + nextfb)) then
|
||||
fap = k
|
||||
fb = f
|
||||
ffb = f
|
||||
end
|
||||
end
|
||||
k += 1
|
||||
end
|
||||
if (fb ~= 0 and i + fap <= n and matchFrame(b, o, n - i, fb)) or (i == 0 and fap == n) then
|
||||
return i, fap, ffb
|
||||
end
|
||||
ffb = 0
|
||||
end
|
||||
end
|
||||
return n, 0, ffb
|
||||
end
|
||||
|
||||
return H
|
||||
@@ -0,0 +1,224 @@
|
||||
--!strict
|
||||
-- codecs/audio/Mpa/Tables: constant tables of minimp3 (https://github.com/lieff/minimp3, CC0), generated by tools/gen_tables.py.
|
||||
-- Every list is flattened row-major and 0-based (t[0] is the first element), like the C arrays.
|
||||
local T = {}
|
||||
T.halfrate = { [0] = 0,
|
||||
4, 8, 12, 16, 20, 24, 28, 32, 40, 48, 56, 64, 72, 80, 0, 4, 8, 12, 16, 20, 24, 28, 32, 40,
|
||||
48, 56, 64, 72, 80, 0, 16, 24, 28, 32, 40, 48, 56, 64, 72, 80, 88, 96, 112, 128, 0, 16, 20, 24,
|
||||
28, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 0, 16, 24, 28, 32, 40, 48, 56, 64, 80, 96, 112, 128,
|
||||
160, 192, 0, 16, 32, 48, 64, 80, 96, 112, 128, 144, 160, 176, 192, 208, 224,
|
||||
}
|
||||
T.g_bitalloc_code_tab = { [0] = 0,
|
||||
17, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 0, 17, 18, 3, 19, 4, 5, 6, 7,
|
||||
8, 9, 10, 11, 12, 13, 16, 0, 17, 18, 3, 19, 4, 5, 16, 0, 17, 18, 16, 0, 17, 18, 19, 4,
|
||||
5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0, 17, 18, 3, 19, 4, 5, 6, 7, 8, 9, 10, 11,
|
||||
12, 13, 14, 0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
|
||||
}
|
||||
T.g_scf_long = { [0] = 6,
|
||||
6, 6, 6, 6, 6, 8, 10, 12, 14, 16, 20, 24, 28, 32, 38, 46, 52, 60, 68, 58, 54, 0, 12, 12,
|
||||
12, 12, 12, 12, 16, 20, 24, 28, 32, 40, 48, 56, 64, 76, 90, 2, 2, 2, 2, 2, 0, 6, 6, 6,
|
||||
6, 6, 6, 8, 10, 12, 14, 16, 20, 24, 28, 32, 38, 46, 52, 60, 68, 58, 54, 0, 6, 6, 6, 6,
|
||||
6, 6, 8, 10, 12, 14, 16, 18, 22, 26, 32, 38, 46, 54, 62, 70, 76, 36, 0, 6, 6, 6, 6, 6,
|
||||
6, 8, 10, 12, 14, 16, 20, 24, 28, 32, 38, 46, 52, 60, 68, 58, 54, 0, 4, 4, 4, 4, 4, 4,
|
||||
6, 6, 8, 8, 10, 12, 16, 20, 24, 28, 34, 42, 50, 54, 76, 158, 0, 4, 4, 4, 4, 4, 4, 6,
|
||||
6, 6, 8, 10, 12, 16, 18, 22, 28, 34, 40, 46, 54, 54, 192, 0, 4, 4, 4, 4, 4, 4, 6, 6,
|
||||
8, 10, 12, 16, 20, 24, 30, 38, 46, 56, 68, 84, 102, 26, 0,
|
||||
}
|
||||
T.g_scf_short = { [0] = 4,
|
||||
4, 4, 4, 4, 4, 4, 4, 4, 6, 6, 6, 8, 8, 8, 10, 10, 10, 12, 12, 12, 14, 14, 14, 18,
|
||||
18, 18, 24, 24, 24, 30, 30, 30, 40, 40, 40, 18, 18, 18, 0, 8, 8, 8, 8, 8, 8, 8, 8, 8,
|
||||
12, 12, 12, 16, 16, 16, 20, 20, 20, 24, 24, 24, 28, 28, 28, 36, 36, 36, 2, 2, 2, 2, 2, 2,
|
||||
2, 2, 2, 26, 26, 26, 0, 4, 4, 4, 4, 4, 4, 4, 4, 4, 6, 6, 6, 6, 6, 6, 8, 8,
|
||||
8, 10, 10, 10, 14, 14, 14, 18, 18, 18, 26, 26, 26, 32, 32, 32, 42, 42, 42, 18, 18, 18, 0, 4,
|
||||
4, 4, 4, 4, 4, 4, 4, 4, 6, 6, 6, 8, 8, 8, 10, 10, 10, 12, 12, 12, 14, 14, 14, 18,
|
||||
18, 18, 24, 24, 24, 32, 32, 32, 44, 44, 44, 12, 12, 12, 0, 4, 4, 4, 4, 4, 4, 4, 4, 4,
|
||||
6, 6, 6, 8, 8, 8, 10, 10, 10, 12, 12, 12, 14, 14, 14, 18, 18, 18, 24, 24, 24, 30, 30, 30,
|
||||
40, 40, 40, 18, 18, 18, 0, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 6, 6, 6, 8, 8,
|
||||
8, 10, 10, 10, 12, 12, 12, 14, 14, 14, 18, 18, 18, 22, 22, 22, 30, 30, 30, 56, 56, 56, 0, 4,
|
||||
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 6, 6, 6, 6, 6, 6, 10, 10, 10, 12, 12, 12, 14,
|
||||
14, 14, 16, 16, 16, 20, 20, 20, 26, 26, 26, 66, 66, 66, 0, 4, 4, 4, 4, 4, 4, 4, 4, 4,
|
||||
4, 4, 4, 6, 6, 6, 8, 8, 8, 12, 12, 12, 16, 16, 16, 20, 20, 20, 26, 26, 26, 34, 34, 34,
|
||||
42, 42, 42, 12, 12, 12, 0,
|
||||
}
|
||||
T.g_scf_mixed = { [0] = 6,
|
||||
6, 6, 6, 6, 6, 6, 6, 6, 8, 8, 8, 10, 10, 10, 12, 12, 12, 14, 14, 14, 18, 18, 18, 24,
|
||||
24, 24, 30, 30, 30, 40, 40, 40, 18, 18, 18, 0, 0, 0, 0, 12, 12, 12, 4, 4, 4, 8, 8, 8,
|
||||
12, 12, 12, 16, 16, 16, 20, 20, 20, 24, 24, 24, 28, 28, 28, 36, 36, 36, 2, 2, 2, 2, 2, 2,
|
||||
2, 2, 2, 26, 26, 26, 0, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 8, 8, 8, 10, 10,
|
||||
10, 14, 14, 14, 18, 18, 18, 26, 26, 26, 32, 32, 32, 42, 42, 42, 18, 18, 18, 0, 0, 0, 0, 6,
|
||||
6, 6, 6, 6, 6, 6, 6, 6, 8, 8, 8, 10, 10, 10, 12, 12, 12, 14, 14, 14, 18, 18, 18, 24,
|
||||
24, 24, 32, 32, 32, 44, 44, 44, 12, 12, 12, 0, 0, 0, 0, 6, 6, 6, 6, 6, 6, 6, 6, 6,
|
||||
8, 8, 8, 10, 10, 10, 12, 12, 12, 14, 14, 14, 18, 18, 18, 24, 24, 24, 30, 30, 30, 40, 40, 40,
|
||||
18, 18, 18, 0, 0, 0, 0, 4, 4, 4, 4, 4, 4, 6, 6, 4, 4, 4, 6, 6, 6, 8, 8, 8,
|
||||
10, 10, 10, 12, 12, 12, 14, 14, 14, 18, 18, 18, 22, 22, 22, 30, 30, 30, 56, 56, 56, 0, 0, 4,
|
||||
4, 4, 4, 4, 4, 6, 6, 4, 4, 4, 6, 6, 6, 6, 6, 6, 10, 10, 10, 12, 12, 12, 14, 14,
|
||||
14, 16, 16, 16, 20, 20, 20, 26, 26, 26, 66, 66, 66, 0, 0, 4, 4, 4, 4, 4, 4, 6, 6, 4,
|
||||
4, 4, 6, 6, 6, 8, 8, 8, 12, 12, 12, 16, 16, 16, 20, 20, 20, 26, 26, 26, 34, 34, 34, 42,
|
||||
42, 42, 12, 12, 12, 0, 0,
|
||||
}
|
||||
T.g_expfrac = { [0] = 9.31322575e-10,
|
||||
7.83145814e-10, 6.58544508e-10, 5.53767716e-10,
|
||||
}
|
||||
T.g_scf_partitions = { [0] = 6,
|
||||
5, 5, 5, 6, 5, 5, 5, 6, 5, 7, 3, 11, 10, 0, 0, 7, 7, 7, 0, 6, 6, 6, 3, 8,
|
||||
8, 5, 0, 8, 9, 6, 12, 6, 9, 9, 9, 6, 9, 12, 6, 15, 18, 0, 0, 6, 15, 12, 0, 6,
|
||||
12, 9, 6, 6, 18, 9, 0, 9, 9, 6, 12, 9, 9, 9, 9, 9, 9, 12, 6, 18, 18, 0, 0, 12,
|
||||
12, 12, 0, 12, 9, 9, 6, 15, 12, 9, 0,
|
||||
}
|
||||
T.g_scfc_decode = { [0] = 0,
|
||||
1, 2, 3, 12, 5, 6, 7, 9, 10, 11, 13, 14, 15, 18, 19,
|
||||
}
|
||||
T.g_mod = { [0] = 5,
|
||||
5, 4, 4, 5, 5, 4, 1, 4, 3, 1, 1, 5, 6, 6, 1, 4, 4, 4, 1, 4, 3, 1, 1,
|
||||
}
|
||||
T.g_preamp = { [0] = 1,
|
||||
1, 1, 1, 2, 2, 3, 3, 3, 2,
|
||||
}
|
||||
T.g_pow43 = { [0] = 0,
|
||||
-1, -2.519842, -4.326749, -6.349604, -8.549880, -10.902724, -13.390518, -16.000000, -18.720754, -21.544347, -24.463781, -27.473142, -30.567351, -33.741992, -36.993181, 0, 1, 2.519842, 4.326749, 6.349604, 8.549880, 10.902724, 13.390518, 16.000000,
|
||||
18.720754, 21.544347, 24.463781, 27.473142, 30.567351, 33.741992, 36.993181, 40.317474, 43.711787, 47.173345, 50.699631, 54.288352, 57.937408, 61.644865, 65.408941, 69.227979, 73.100443, 77.024898, 81.000000, 85.024491, 89.097188, 93.216975, 97.382800, 101.593667,
|
||||
105.848633, 110.146801, 114.487321, 118.869381, 123.292209, 127.755065, 132.257246, 136.798076, 141.376907, 145.993119, 150.646117, 155.335327, 160.060199, 164.820202, 169.614826, 174.443577, 179.305980, 184.201575, 189.129918, 194.090580, 199.083145, 204.107210, 209.162385, 214.248292,
|
||||
219.364564, 224.510845, 229.686789, 234.892058, 240.126328, 245.389280, 250.680604, 256.000000, 261.347174, 266.721841, 272.123723, 277.552547, 283.008049, 288.489971, 293.998060, 299.532071, 305.091761, 310.676898, 316.287249, 321.922592, 327.582707, 333.267377, 338.976394, 344.709550,
|
||||
350.466646, 356.247482, 362.051866, 367.879608, 373.730522, 379.604427, 385.501143, 391.420496, 397.362314, 403.326427, 409.312672, 415.320884, 421.350905, 427.402579, 433.475750, 439.570269, 445.685987, 451.822757, 457.980436, 464.158883, 470.357960, 476.577530, 482.817459, 489.077615,
|
||||
495.357868, 501.658090, 507.978156, 514.317941, 520.677324, 527.056184, 533.454404, 539.871867, 546.308458, 552.764065, 559.238575, 565.731879, 572.243870, 578.774440, 585.323483, 591.890898, 598.476581, 605.080431, 611.702349, 618.342238, 625.000000, 631.675540, 638.368763, 645.079578,
|
||||
}
|
||||
T.tabs = { [0] = 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 785, 785, 785, 785, 784, 784, 784, 784, 513, 513, 513, 513, 513, 513, 513, 513, 256,
|
||||
256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, -255, 1313, 1298, 1282, 785, 785, 785, 785, 784,
|
||||
784, 784, 784, 769, 769, 769, 769, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 290,
|
||||
288, -255, 1313, 1298, 1282, 769, 769, 769, 769, 529, 529, 529, 529, 529, 529, 529, 529, 528, 528, 528, 528, 528, 528, 528,
|
||||
528, 512, 512, 512, 512, 512, 512, 512, 512, 290, 288, -253, -318, -351, -367, 785, 785, 785, 785, 784, 784, 784, 784, 769,
|
||||
769, 769, 769, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 819, 818, 547, 547, 275,
|
||||
275, 275, 275, 561, 560, 515, 546, 289, 274, 288, 258, -254, -287, 1329, 1299, 1314, 1312, 1057, 1057, 1042, 1042, 1026, 1026, 784,
|
||||
784, 784, 784, 529, 529, 529, 529, 529, 529, 529, 529, 769, 769, 769, 769, 768, 768, 768, 768, 563, 560, 306, 306, 291,
|
||||
259, -252, -413, -477, -542, 1298, -575, 1041, 1041, 784, 784, 784, 784, 769, 769, 769, 769, 256, 256, 256, 256, 256, 256, 256,
|
||||
256, 256, 256, 256, 256, 256, 256, 256, 256, -383, -399, 1107, 1092, 1106, 1061, 849, 849, 789, 789, 1104, 1091, 773, 773, 1076,
|
||||
1075, 341, 340, 325, 309, 834, 804, 577, 577, 532, 532, 516, 516, 832, 818, 803, 816, 561, 561, 531, 531, 515, 546, 289,
|
||||
289, 288, 258, -252, -429, -493, -559, 1057, 1057, 1042, 1042, 529, 529, 529, 529, 529, 529, 529, 529, 784, 784, 784, 784, 769,
|
||||
769, 769, 769, 512, 512, 512, 512, 512, 512, 512, 512, -382, 1077, -415, 1106, 1061, 1104, 849, 849, 789, 789, 1091, 1076, 1029,
|
||||
1075, 834, 834, 597, 581, 340, 340, 339, 324, 804, 833, 532, 532, 832, 772, 818, 803, 817, 787, 816, 771, 290, 290, 290,
|
||||
290, 288, 258, -253, -349, -414, -447, -463, 1329, 1299, -479, 1314, 1312, 1057, 1057, 1042, 1042, 1026, 1026, 785, 785, 785, 785, 784,
|
||||
784, 784, 784, 769, 769, 769, 769, 768, 768, 768, 768, -319, 851, 821, -335, 836, 850, 805, 849, 341, 340, 325, 336, 533,
|
||||
533, 579, 579, 564, 564, 773, 832, 578, 548, 563, 516, 321, 276, 306, 291, 304, 259, -251, -572, -733, -830, -863, -879, 1041,
|
||||
1041, 784, 784, 784, 784, 769, 769, 769, 769, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
|
||||
256, -511, -527, -543, 1396, 1351, 1381, 1366, 1395, 1335, 1380, -559, 1334, 1138, 1138, 1063, 1063, 1350, 1392, 1031, 1031, 1062, 1062, 1364,
|
||||
1363, 1120, 1120, 1333, 1348, 881, 881, 881, 881, 375, 374, 359, 373, 343, 358, 341, 325, 791, 791, 1123, 1122, -703, 1105, 1045,
|
||||
-719, 865, 865, 790, 790, 774, 774, 1104, 1029, 338, 293, 323, 308, -799, -815, 833, 788, 772, 818, 803, 816, 322, 292, 307,
|
||||
320, 561, 531, 515, 546, 289, 274, 288, 258, -251, -525, -605, -685, -765, -831, -846, 1298, 1057, 1057, 1312, 1282, 785, 785, 785,
|
||||
785, 784, 784, 784, 784, 769, 769, 769, 769, 512, 512, 512, 512, 512, 512, 512, 512, 1399, 1398, 1383, 1367, 1382, 1396, 1351,
|
||||
-511, 1381, 1366, 1139, 1139, 1079, 1079, 1124, 1124, 1364, 1349, 1363, 1333, 882, 882, 882, 882, 807, 807, 807, 807, 1094, 1094, 1136,
|
||||
1136, 373, 341, 535, 535, 881, 775, 867, 822, 774, -591, 324, 338, -671, 849, 550, 550, 866, 864, 609, 609, 293, 336, 534,
|
||||
534, 789, 835, 773, -751, 834, 804, 308, 307, 833, 788, 832, 772, 562, 562, 547, 547, 305, 275, 560, 515, 290, 290, -252,
|
||||
-397, -477, -557, -622, -653, -719, -735, -750, 1329, 1299, 1314, 1057, 1057, 1042, 1042, 1312, 1282, 1024, 1024, 785, 785, 785, 785, 784,
|
||||
784, 784, 784, 769, 769, 769, 769, -383, 1127, 1141, 1111, 1126, 1140, 1095, 1110, 869, 869, 883, 883, 1079, 1109, 882, 882, 375,
|
||||
374, 807, 868, 838, 881, 791, -463, 867, 822, 368, 263, 852, 837, 836, -543, 610, 610, 550, 550, 352, 336, 534, 534, 865,
|
||||
774, 851, 821, 850, 805, 593, 533, 579, 564, 773, 832, 578, 578, 548, 548, 577, 577, 307, 276, 306, 291, 516, 560, 259,
|
||||
259, -250, -2107, -2507, -2764, -2909, -2974, -3007, -3023, 1041, 1041, 1040, 1040, 769, 769, 769, 769, 256, 256, 256, 256, 256, 256, 256,
|
||||
256, 256, 256, 256, 256, 256, 256, 256, 256, -767, -1052, -1213, -1277, -1358, -1405, -1469, -1535, -1550, -1582, -1614, -1647, -1662, -1694, -1726,
|
||||
-1759, -1774, -1807, -1822, -1854, -1886, 1565, -1919, -1935, -1951, -1967, 1731, 1730, 1580, 1717, -1983, 1729, 1564, -1999, 1548, -2015, -2031, 1715, 1595,
|
||||
-2047, 1714, -2063, 1610, -2079, 1609, -2095, 1323, 1323, 1457, 1457, 1307, 1307, 1712, 1547, 1641, 1700, 1699, 1594, 1685, 1625, 1442, 1442, 1322,
|
||||
1322, -780, -973, -910, 1279, 1278, 1277, 1262, 1276, 1261, 1275, 1215, 1260, 1229, -959, 974, 974, 989, 989, -943, 735, 478, 478, 495,
|
||||
463, 506, 414, -1039, 1003, 958, 1017, 927, 942, 987, 957, 431, 476, 1272, 1167, 1228, -1183, 1256, -1199, 895, 895, 941, 941, 1242,
|
||||
1227, 1212, 1135, 1014, 1014, 490, 489, 503, 487, 910, 1013, 985, 925, 863, 894, 970, 955, 1012, 847, -1343, 831, 755, 755, 984,
|
||||
909, 428, 366, 754, 559, -1391, 752, 486, 457, 924, 997, 698, 698, 983, 893, 740, 740, 908, 877, 739, 739, 667, 667, 953,
|
||||
938, 497, 287, 271, 271, 683, 606, 590, 712, 726, 574, 302, 302, 738, 736, 481, 286, 526, 725, 605, 711, 636, 724, 696,
|
||||
651, 589, 681, 666, 710, 364, 467, 573, 695, 466, 466, 301, 465, 379, 379, 709, 604, 665, 679, 316, 316, 634, 633, 436,
|
||||
436, 464, 269, 424, 394, 452, 332, 438, 363, 347, 408, 393, 448, 331, 422, 362, 407, 392, 421, 346, 406, 391, 376, 375,
|
||||
359, 1441, 1306, -2367, 1290, -2383, 1337, -2399, -2415, 1426, 1321, -2431, 1411, 1336, -2447, -2463, -2479, 1169, 1169, 1049, 1049, 1424, 1289, 1412,
|
||||
1352, 1319, -2495, 1154, 1154, 1064, 1064, 1153, 1153, 416, 390, 360, 404, 403, 389, 344, 374, 373, 343, 358, 372, 327, 357, 342,
|
||||
311, 356, 326, 1395, 1394, 1137, 1137, 1047, 1047, 1365, 1392, 1287, 1379, 1334, 1364, 1349, 1378, 1318, 1363, 792, 792, 792, 792, 1152,
|
||||
1152, 1032, 1032, 1121, 1121, 1046, 1046, 1120, 1120, 1030, 1030, -2895, 1106, 1061, 1104, 849, 849, 789, 789, 1091, 1076, 1029, 1090, 1060,
|
||||
1075, 833, 833, 309, 324, 532, 532, 832, 772, 818, 803, 561, 561, 531, 560, 515, 546, 289, 274, 288, 258, -250, -1179, -1579,
|
||||
-1836, -1996, -2124, -2253, -2333, -2413, -2477, -2542, -2574, -2607, -2622, -2655, 1314, 1313, 1298, 1312, 1282, 785, 785, 785, 785, 1040, 1040, 1025,
|
||||
1025, 768, 768, 768, 768, -766, -798, -830, -862, -895, -911, -927, -943, -959, -975, -991, -1007, -1023, -1039, -1055, -1070, 1724, 1647, -1103,
|
||||
-1119, 1631, 1767, 1662, 1738, 1708, 1723, -1135, 1780, 1615, 1779, 1599, 1677, 1646, 1778, 1583, -1151, 1777, 1567, 1737, 1692, 1765, 1722, 1707,
|
||||
1630, 1751, 1661, 1764, 1614, 1736, 1676, 1763, 1750, 1645, 1598, 1721, 1691, 1762, 1706, 1582, 1761, 1566, -1167, 1749, 1629, 767, 766, 751,
|
||||
765, 494, 494, 735, 764, 719, 749, 734, 763, 447, 447, 748, 718, 477, 506, 431, 491, 446, 476, 461, 505, 415, 430, 475,
|
||||
445, 504, 399, 460, 489, 414, 503, 383, 474, 429, 459, 502, 502, 746, 752, 488, 398, 501, 473, 413, 472, 486, 271, 480,
|
||||
270, -1439, -1455, 1357, -1471, -1487, -1503, 1341, 1325, -1519, 1489, 1463, 1403, 1309, -1535, 1372, 1448, 1418, 1476, 1356, 1462, 1387, -1551, 1475,
|
||||
1340, 1447, 1402, 1386, -1567, 1068, 1068, 1474, 1461, 455, 380, 468, 440, 395, 425, 410, 454, 364, 467, 466, 464, 453, 269, 409,
|
||||
448, 268, 432, 1371, 1473, 1432, 1417, 1308, 1460, 1355, 1446, 1459, 1431, 1083, 1083, 1401, 1416, 1458, 1445, 1067, 1067, 1370, 1457, 1051,
|
||||
1051, 1291, 1430, 1385, 1444, 1354, 1415, 1400, 1443, 1082, 1082, 1173, 1113, 1186, 1066, 1185, 1050, -1967, 1158, 1128, 1172, 1097, 1171, 1081,
|
||||
-1983, 1157, 1112, 416, 266, 375, 400, 1170, 1142, 1127, 1065, 793, 793, 1169, 1033, 1156, 1096, 1141, 1111, 1155, 1080, 1126, 1140, 898,
|
||||
898, 808, 808, 897, 897, 792, 792, 1095, 1152, 1032, 1125, 1110, 1139, 1079, 1124, 882, 807, 838, 881, 853, 791, -2319, 867, 368,
|
||||
263, 822, 852, 837, 866, 806, 865, -2399, 851, 352, 262, 534, 534, 821, 836, 594, 594, 549, 549, 593, 593, 533, 533, 848,
|
||||
773, 579, 579, 564, 578, 548, 563, 276, 276, 577, 576, 306, 291, 516, 560, 305, 305, 275, 259, -251, -892, -2058, -2620, -2828,
|
||||
-2957, -3023, -3039, 1041, 1041, 1040, 1040, 769, 769, 769, 769, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256, 256,
|
||||
256, 256, 256, -511, -527, -543, -559, 1530, -575, -591, 1528, 1527, 1407, 1526, 1391, 1023, 1023, 1023, 1023, 1525, 1375, 1268, 1268, 1103,
|
||||
1103, 1087, 1087, 1039, 1039, 1523, -604, 815, 815, 815, 815, 510, 495, 509, 479, 508, 463, 507, 447, 431, 505, 415, 399, -734,
|
||||
-782, 1262, -815, 1259, 1244, -831, 1258, 1228, -847, -863, 1196, -879, 1253, 987, 987, 748, -767, 493, 493, 462, 477, 414, 414, 686,
|
||||
669, 478, 446, 461, 445, 474, 429, 487, 458, 412, 471, 1266, 1264, 1009, 1009, 799, 799, -1019, -1276, -1452, -1581, -1677, -1757, -1821,
|
||||
-1886, -1933, -1997, 1257, 1257, 1483, 1468, 1512, 1422, 1497, 1406, 1467, 1496, 1421, 1510, 1134, 1134, 1225, 1225, 1466, 1451, 1374, 1405, 1252,
|
||||
1252, 1358, 1480, 1164, 1164, 1251, 1251, 1238, 1238, 1389, 1465, -1407, 1054, 1101, -1423, 1207, -1439, 830, 830, 1248, 1038, 1237, 1117, 1223,
|
||||
1148, 1236, 1208, 411, 426, 395, 410, 379, 269, 1193, 1222, 1132, 1235, 1221, 1116, 976, 976, 1192, 1162, 1177, 1220, 1131, 1191, 963,
|
||||
963, -1647, 961, 780, -1663, 558, 558, 994, 993, 437, 408, 393, 407, 829, 978, 813, 797, 947, -1743, 721, 721, 377, 392, 844,
|
||||
950, 828, 890, 706, 706, 812, 859, 796, 960, 948, 843, 934, 874, 571, 571, -1919, 690, 555, 689, 421, 346, 539, 539, 944,
|
||||
779, 918, 873, 932, 842, 903, 888, 570, 570, 931, 917, 674, 674, -2575, 1562, -2591, 1609, -2607, 1654, 1322, 1322, 1441, 1441, 1696,
|
||||
1546, 1683, 1593, 1669, 1624, 1426, 1426, 1321, 1321, 1639, 1680, 1425, 1425, 1305, 1305, 1545, 1668, 1608, 1623, 1667, 1592, 1638, 1666, 1320,
|
||||
1320, 1652, 1607, 1409, 1409, 1304, 1304, 1288, 1288, 1664, 1637, 1395, 1395, 1335, 1335, 1622, 1636, 1394, 1394, 1319, 1319, 1606, 1621, 1392,
|
||||
1392, 1137, 1137, 1137, 1137, 345, 390, 360, 375, 404, 373, 1047, -2751, -2767, -2783, 1062, 1121, 1046, -2799, 1077, -2815, 1106, 1061, 789,
|
||||
789, 1105, 1104, 263, 355, 310, 340, 325, 354, 352, 262, 339, 324, 1091, 1076, 1029, 1090, 1060, 1075, 833, 833, 788, 788, 1088,
|
||||
1028, 818, 818, 803, 803, 561, 561, 531, 531, 816, 771, 546, 546, 289, 274, 288, 258, -253, -317, -381, -446, -478, -509, 1279,
|
||||
1279, -811, -1179, -1451, -1756, -1900, -2028, -2189, -2253, -2333, -2414, -2445, -2511, -2526, 1313, 1298, -2559, 1041, 1041, 1040, 1040, 1025, 1025, 1024,
|
||||
1024, 1022, 1007, 1021, 991, 1020, 975, 1019, 959, 687, 687, 1018, 1017, 671, 671, 655, 655, 1016, 1015, 639, 639, 758, 758, 623,
|
||||
623, 757, 607, 756, 591, 755, 575, 754, 559, 543, 543, 1009, 783, -575, -621, -685, -749, 496, -590, 750, 749, 734, 748, 974,
|
||||
989, 1003, 958, 988, 973, 1002, 942, 987, 957, 972, 1001, 926, 986, 941, 971, 956, 1000, 910, 985, 925, 999, 894, 970, -1071,
|
||||
-1087, -1102, 1390, -1135, 1436, 1509, 1451, 1374, -1151, 1405, 1358, 1480, 1420, -1167, 1507, 1494, 1389, 1342, 1465, 1435, 1450, 1326, 1505, 1310,
|
||||
1493, 1373, 1479, 1404, 1492, 1464, 1419, 428, 443, 472, 397, 736, 526, 464, 464, 486, 457, 442, 471, 484, 482, 1357, 1449, 1434,
|
||||
1478, 1388, 1491, 1341, 1490, 1325, 1489, 1463, 1403, 1309, 1477, 1372, 1448, 1418, 1433, 1476, 1356, 1462, 1387, -1439, 1475, 1340, 1447, 1402,
|
||||
1474, 1324, 1461, 1371, 1473, 269, 448, 1432, 1417, 1308, 1460, -1711, 1459, -1727, 1441, 1099, 1099, 1446, 1386, 1431, 1401, -1743, 1289, 1083,
|
||||
1083, 1160, 1160, 1458, 1445, 1067, 1067, 1370, 1457, 1307, 1430, 1129, 1129, 1098, 1098, 268, 432, 267, 416, 266, 400, -1887, 1144, 1187,
|
||||
1082, 1173, 1113, 1186, 1066, 1050, 1158, 1128, 1143, 1172, 1097, 1171, 1081, 420, 391, 1157, 1112, 1170, 1142, 1127, 1065, 1169, 1049, 1156,
|
||||
1096, 1141, 1111, 1155, 1080, 1126, 1154, 1064, 1153, 1140, 1095, 1048, -2159, 1125, 1110, 1137, -2175, 823, 823, 1139, 1138, 807, 807, 384,
|
||||
264, 368, 263, 868, 838, 853, 791, 867, 822, 852, 837, 866, 806, 865, 790, -2319, 851, 821, 836, 352, 262, 850, 805, 849,
|
||||
-2399, 533, 533, 835, 820, 336, 261, 578, 548, 563, 577, 532, 532, 832, 772, 562, 562, 547, 547, 305, 275, 560, 515, 290,
|
||||
290, 288, 258,
|
||||
}
|
||||
T.tab32 = { [0] = 130,
|
||||
162, 193, 209, 44, 28, 76, 140, 9, 9, 9, 9, 9, 9, 9, 9, 190, 254, 222, 238, 126, 94, 157, 157, 109,
|
||||
61, 173, 205,
|
||||
}
|
||||
T.tab33 = { [0] = 252,
|
||||
236, 220, 204, 188, 172, 156, 140, 124, 108, 92, 76, 60, 44, 28, 12,
|
||||
}
|
||||
T.tabindex = { [0] = 0,
|
||||
32, 64, 98, 0, 132, 180, 218, 292, 364, 426, 538, 648, 746, 0, 1126, 1460, 1460, 1460, 1460, 1460, 1460, 1460, 1460, 1842,
|
||||
1842, 1842, 1842, 1842, 1842, 1842, 1842,
|
||||
}
|
||||
T.g_linbits = { [0] = 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 8, 10, 13, 4,
|
||||
5, 6, 7, 8, 9, 11, 13,
|
||||
}
|
||||
T.g_pan = { [0] = 0,
|
||||
1, 0.21132487, 0.78867513, 0.36602540, 0.63397460, 0.5, 0.5, 0.63397460, 0.36602540, 0.78867513, 0.21132487, 1, 0,
|
||||
}
|
||||
T.g_aa = { [0] = 0.85749293,
|
||||
0.88174200, 0.94962865, 0.98331459, 0.99551782, 0.99916056, 0.99989920, 0.99999316, 0.51449576, 0.47173197, 0.31337745, 0.18191320, 0.09457419, 0.04096558, 0.01419856, 0.00369997,
|
||||
}
|
||||
T.g_twid9 = { [0] = 0.73727734,
|
||||
0.79335334, 0.84339145, 0.88701083, 0.92387953, 0.95371695, 0.97629601, 0.99144486, 0.99904822, 0.67559021, 0.60876143, 0.53729961, 0.46174861, 0.38268343, 0.30070580, 0.21643961, 0.13052619, 0.04361938,
|
||||
}
|
||||
T.g_twid3 = { [0] = 0.79335334,
|
||||
0.92387953, 0.99144486, 0.60876143, 0.38268343, 0.13052619,
|
||||
}
|
||||
T.g_mdct_window = { [0] = 0.99904822,
|
||||
0.99144486, 0.97629601, 0.95371695, 0.92387953, 0.88701083, 0.84339145, 0.79335334, 0.73727734, 0.04361938, 0.13052619, 0.21643961, 0.30070580, 0.38268343, 0.46174861, 0.53729961, 0.60876143, 0.67559021, 1, 1, 1, 1, 1, 1, 0.99144486,
|
||||
0.92387953, 0.79335334, 0, 0, 0, 0, 0, 0, 0.13052619, 0.38268343, 0.60876143,
|
||||
}
|
||||
T.g_sec = { [0] = 10.19000816,
|
||||
0.50060302, 0.50241929, 3.40760851, 0.50547093, 0.52249861, 2.05778098, 0.51544732, 0.56694406, 1.48416460, 0.53104258, 0.64682180, 1.16943991, 0.55310392, 0.78815460, 0.97256821, 0.58293498, 1.06067765, 0.83934963, 0.62250412, 1.72244716, 0.74453628, 0.67480832, 5.10114861,
|
||||
}
|
||||
T.g_win = { [0] = -1,
|
||||
26, -31, 208, 218, 401, -519, 2063, 2000, 4788, -5517, 7134, 5959, 35640, -39336, 74992, -1, 24, -35, 202, 222, 347, -581, 2080, 1952,
|
||||
4425, -5879, 7640, 5288, 33791, -41176, 74856, -1, 21, -38, 196, 225, 294, -645, 2087, 1893, 4063, -6237, 8092, 4561, 31947, -43006, 74630, -1,
|
||||
19, -41, 190, 227, 244, -711, 2085, 1822, 3705, -6589, 8492, 3776, 30112, -44821, 74313, -1, 17, -45, 183, 228, 197, -779, 2075, 1739,
|
||||
3351, -6935, 8840, 2935, 28289, -46617, 73908, -1, 16, -49, 176, 228, 153, -848, 2057, 1644, 3004, -7271, 9139, 2037, 26482, -48390, 73415, -2,
|
||||
14, -53, 169, 227, 111, -919, 2032, 1535, 2663, -7597, 9389, 1082, 24694, -50137, 72835, -2, 13, -58, 161, 224, 72, -991, 2001, 1414,
|
||||
2330, -7910, 9592, 70, 22929, -51853, 72169, -2, 11, -63, 154, 221, 36, -1064, 1962, 1280, 2006, -8209, 9750, -998, 21189, -53534, 71420, -2,
|
||||
10, -68, 147, 215, 2, -1137, 1919, 1131, 1692, -8491, 9863, -2122, 19478, -55178, 70590, -3, 9, -73, 139, 208, -29, -1210, 1870, 970,
|
||||
1388, -8755, 9935, -3300, 17799, -56778, 69679, -3, 8, -79, 132, 200, -57, -1283, 1817, 794, 1095, -8998, 9966, -4533, 16155, -58333, 68692, -4,
|
||||
7, -85, 125, 189, -83, -1356, 1759, 605, 814, -9219, 9959, -5818, 14548, -59838, 67629, -4, 7, -91, 117, 177, -106, -1428, 1698, 402,
|
||||
545, -9416, 9916, -7154, 12980, -61289, 66494, -5, 6, -97, 111, 163, -127, -1498, 1634, 185, 288, -9585, 9838, -8540, 11455, -62684, 65290,
|
||||
}
|
||||
return T
|
||||
@@ -0,0 +1,13 @@
|
||||
--!strict
|
||||
-- codecs/audio/Mpa: MPEG audio layers I / II / III (ISO/IEC 11172-3, 13818-3, MPEG-2.5) without the file around it:
|
||||
-- frame headers (Header), the decoder core (Core: a port of minimp3 by lieff, CC0: licenses/minimp3-CC0.txt,
|
||||
-- absorbed from mp3-luau c755bbf) with its tables, and the layer III / layer II encoders (Encode3, Encode2: shine-style
|
||||
-- CBR, new) with their analysis tables. ID3 tags, Xing / LAME gapless info, the frame index and seeking are audio/mp3.
|
||||
local Mpa = {
|
||||
Header = require("@formats/codecs/audio/Mpa/Header"),
|
||||
Core = require("@formats/codecs/audio/Mpa/Core"),
|
||||
Encode3 = require("@formats/codecs/audio/Mpa/Encode3"),
|
||||
Encode2 = require("@formats/codecs/audio/Mpa/Encode2"),
|
||||
}
|
||||
|
||||
return Mpa
|
||||
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,58 @@
|
||||
--!strict
|
||||
-- codecs/audio/Opus/CeltTables: Generated from libopus celt/static_modes_float.h and celt/quant_bands.c (BSD, see licenses/). 1-based arrays.
|
||||
return {
|
||||
logN400 = {
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 8, 8, 8, 8, 16, 16, 16, 21, 21, 24, 29, 34, 36,
|
||||
},
|
||||
cache_index50 = {
|
||||
-1, -1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 41, 41, 41, 82, 82, 123, 164, 200, 222, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 41, 41, 41, 41, 123, 123, 123, 164, 164, 240, 266, 283, 295, 41, 41, 41, 41, 41, 41,
|
||||
41, 41, 123, 123, 123, 123, 240, 240, 240, 266, 266, 305, 318, 328, 336, 123, 123, 123, 123, 123, 123, 123, 123, 240,
|
||||
240, 240, 240, 305, 305, 305, 318, 318, 343, 351, 358, 364, 240, 240, 240, 240, 240, 240, 240, 240, 305, 305, 305, 305,
|
||||
343, 343, 343, 351, 351, 370, 376, 382, 387,
|
||||
},
|
||||
cache_bits50 = {
|
||||
40, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
|
||||
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 40, 15, 23, 28, 31, 34, 36,
|
||||
38, 39, 41, 42, 43, 44, 45, 46, 47, 47, 49, 50, 51, 52, 53, 54, 55, 55, 57, 58, 59, 60, 61, 62,
|
||||
63, 63, 65, 66, 67, 68, 69, 70, 71, 71, 40, 20, 33, 41, 48, 53, 57, 61, 64, 66, 69, 71, 73, 75,
|
||||
76, 78, 80, 82, 85, 87, 89, 91, 92, 94, 96, 98, 101, 103, 105, 107, 108, 110, 112, 114, 117, 119, 121, 123,
|
||||
124, 126, 128, 40, 23, 39, 51, 60, 67, 73, 79, 83, 87, 91, 94, 97, 100, 102, 105, 107, 111, 115, 118, 121,
|
||||
124, 126, 129, 131, 135, 139, 142, 145, 148, 150, 153, 155, 159, 163, 166, 169, 172, 174, 177, 179, 35, 28, 49, 65,
|
||||
78, 89, 99, 107, 114, 120, 126, 132, 136, 141, 145, 149, 153, 159, 165, 171, 176, 180, 185, 189, 192, 199, 205, 211,
|
||||
216, 220, 225, 229, 232, 239, 245, 251, 21, 33, 58, 79, 97, 112, 125, 137, 148, 157, 166, 174, 182, 189, 195, 201,
|
||||
207, 217, 227, 235, 243, 251, 17, 35, 63, 86, 106, 123, 139, 152, 165, 177, 187, 197, 206, 214, 222, 230, 237, 250,
|
||||
25, 31, 55, 75, 91, 105, 117, 128, 138, 146, 154, 161, 168, 174, 180, 185, 190, 200, 208, 215, 222, 229, 235, 240,
|
||||
245, 255, 16, 36, 65, 89, 110, 128, 144, 159, 173, 185, 196, 207, 217, 226, 234, 242, 250, 11, 41, 74, 103, 128,
|
||||
151, 172, 191, 209, 225, 241, 255, 9, 43, 79, 110, 138, 163, 186, 207, 227, 246, 12, 39, 71, 99, 123, 144, 164,
|
||||
182, 198, 214, 228, 241, 253, 9, 44, 81, 113, 142, 168, 192, 214, 235, 255, 7, 49, 90, 127, 160, 191, 220, 247,
|
||||
6, 51, 95, 134, 170, 203, 234, 7, 47, 87, 123, 155, 184, 212, 237, 6, 52, 97, 137, 174, 208, 240, 5, 57,
|
||||
106, 151, 192, 231, 5, 59, 111, 158, 202, 243, 5, 55, 103, 147, 187, 224, 5, 60, 113, 161, 206, 248, 4, 65,
|
||||
122, 175, 224, 4, 67, 127, 182, 234,
|
||||
},
|
||||
cache_caps50 = {
|
||||
224, 224, 224, 224, 224, 224, 224, 224, 160, 160, 160, 160, 185, 185, 185, 178, 178, 168, 134, 61, 37, 224, 224, 224,
|
||||
224, 224, 224, 224, 224, 240, 240, 240, 240, 207, 207, 207, 198, 198, 183, 144, 66, 40, 160, 160, 160, 160, 160, 160,
|
||||
160, 160, 185, 185, 185, 185, 193, 193, 193, 183, 183, 172, 138, 64, 38, 240, 240, 240, 240, 240, 240, 240, 240, 207,
|
||||
207, 207, 207, 204, 204, 204, 193, 193, 180, 143, 66, 40, 185, 185, 185, 185, 185, 185, 185, 185, 193, 193, 193, 193,
|
||||
193, 193, 193, 183, 183, 172, 138, 65, 39, 207, 207, 207, 207, 207, 207, 207, 207, 204, 204, 204, 204, 201, 201, 201,
|
||||
188, 188, 176, 141, 66, 40, 193, 193, 193, 193, 193, 193, 193, 193, 193, 193, 193, 193, 194, 194, 194, 184, 184, 173,
|
||||
139, 65, 39, 204, 204, 204, 204, 204, 204, 204, 204, 201, 201, 201, 201, 198, 198, 198, 187, 187, 175, 140, 66, 40,
|
||||
},
|
||||
e_prob_model = {
|
||||
72, 127, 65, 129, 66, 128, 65, 128, 64, 128, 62, 128, 64, 128, 64, 128, 92, 78, 92, 79, 92, 78, 90, 79,
|
||||
116, 41, 115, 40, 114, 40, 132, 26, 132, 26, 145, 17, 161, 12, 176, 10, 177, 11, 24, 179, 48, 138, 54, 135,
|
||||
54, 132, 53, 134, 56, 133, 55, 132, 55, 132, 61, 114, 70, 96, 74, 88, 75, 88, 87, 74, 89, 66, 91, 67,
|
||||
100, 59, 108, 50, 120, 40, 122, 37, 97, 43, 78, 50, 83, 78, 84, 81, 88, 75, 86, 74, 87, 71, 90, 73,
|
||||
93, 74, 93, 74, 109, 40, 114, 36, 117, 34, 117, 34, 143, 17, 145, 18, 146, 19, 162, 12, 165, 10, 178, 7,
|
||||
189, 6, 190, 8, 177, 9, 23, 178, 54, 115, 63, 102, 66, 98, 69, 99, 74, 89, 71, 91, 73, 91, 78, 89,
|
||||
86, 80, 92, 66, 93, 64, 102, 59, 103, 60, 104, 60, 117, 52, 123, 44, 138, 35, 133, 31, 97, 38, 77, 45,
|
||||
61, 90, 93, 60, 105, 42, 107, 41, 110, 45, 116, 38, 113, 38, 112, 38, 124, 26, 132, 27, 136, 19, 140, 20,
|
||||
155, 14, 159, 16, 158, 18, 170, 13, 177, 10, 187, 8, 192, 6, 175, 9, 159, 10, 21, 178, 59, 110, 71, 86,
|
||||
75, 85, 84, 83, 91, 66, 88, 73, 87, 72, 92, 75, 98, 72, 105, 58, 107, 54, 115, 52, 114, 55, 112, 56,
|
||||
129, 51, 132, 40, 150, 33, 140, 29, 98, 35, 77, 42, 42, 121, 96, 66, 108, 43, 111, 40, 117, 44, 123, 32,
|
||||
120, 36, 119, 33, 127, 33, 134, 34, 139, 21, 147, 23, 152, 20, 158, 25, 154, 26, 166, 21, 173, 16, 184, 13,
|
||||
184, 10, 150, 13, 139, 15, 22, 178, 63, 114, 74, 82, 84, 83, 92, 82, 103, 62, 96, 72, 96, 67, 101, 73,
|
||||
107, 72, 113, 55, 118, 52, 125, 52, 118, 52, 117, 55, 135, 49, 137, 39, 157, 32, 145, 29, 97, 33, 77, 40,
|
||||
},
|
||||
}
|
||||
@@ -0,0 +1,466 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- codecs/audio/Opus/Decoder: The Opus decoder proper (libopus src/opus_decoder.c and opus_multistream_decoder.c): per-frame mode switching
|
||||
-- between SILK, hybrid and CELT, the redundant CELT frames of mode transitions, cross-fades, PLC fallbacks, the
|
||||
-- multi-frame packet loop and multistream (channel mapping) decoding. Output is 48 kHz float in Luau arrays.
|
||||
local RD = require("@formats/codecs/entropy/RangeCoder/Decoder")
|
||||
local Packet = require("@formats/codecs/audio/Opus/Packet")
|
||||
local Celt = require("@formats/codecs/audio/Opus/Celt")
|
||||
local Silk = require("@formats/codecs/audio/Opus/Silk")
|
||||
|
||||
local Decoder = {}
|
||||
|
||||
local MODE_SILK, MODE_HYBRID, MODE_CELT = Packet.MODE_SILK_ONLY, Packet.MODE_HYBRID, Packet.MODE_CELT_ONLY
|
||||
local BW_NB, BW_MB, BW_WB, BW_SWB = Packet.BW_NB, Packet.BW_MB, Packet.BW_WB, Packet.BW_SWB
|
||||
local F20, F10, F5, F2_5 = 960, 480, 240, 120
|
||||
local min, max = math.min, math.max
|
||||
local readu8 = buffer.readu8
|
||||
|
||||
local WINDOW = Celt.window
|
||||
local TRANS = table.create(F5 * 2, 0)
|
||||
local REDUND = table.create(F5 * 2, 0)
|
||||
local SILKTMP = table.create(F10 * 2, 0)
|
||||
local SILENCE = buffer.create(2)
|
||||
buffer.writeu8(SILENCE, 0, 0xFF)
|
||||
buffer.writeu8(SILENCE, 1, 0xFF)
|
||||
local EMPTY_EC = RD.new(buffer.create(0), 0, 0)
|
||||
local OFFS = table.create(48, 0)
|
||||
local SIZES = table.create(48, 0)
|
||||
|
||||
export type State = {
|
||||
channels: number,
|
||||
celt: any,
|
||||
silk: any,
|
||||
ctrl: any,
|
||||
stream_channels: number,
|
||||
frame_size: number,
|
||||
mode: number,
|
||||
bandwidth: number,
|
||||
prev_mode: number,
|
||||
prev_redundancy: boolean,
|
||||
last_packet_duration: number,
|
||||
rangeFinal: number,
|
||||
}
|
||||
|
||||
function Decoder.new(channels: number): State
|
||||
return {
|
||||
channels = channels,
|
||||
celt = Celt.new(channels),
|
||||
silk = Silk.new(channels),
|
||||
ctrl = {
|
||||
nChannelsAPI = channels,
|
||||
nChannelsInternal = channels,
|
||||
API_sampleRate = 48000,
|
||||
internalSampleRate = 16000,
|
||||
payloadSize_ms = 20,
|
||||
},
|
||||
stream_channels = channels,
|
||||
frame_size = 120,
|
||||
mode = 0,
|
||||
bandwidth = 0,
|
||||
prev_mode = 0,
|
||||
prev_redundancy = false,
|
||||
last_packet_duration = 0,
|
||||
rangeFinal = 0,
|
||||
}
|
||||
end
|
||||
|
||||
function Decoder.reset(st: State)
|
||||
Celt.reset(st.celt)
|
||||
Silk.reset(st.silk)
|
||||
st.stream_channels = st.channels
|
||||
st.frame_size = 120
|
||||
st.mode = 0
|
||||
st.bandwidth = 0
|
||||
st.prev_mode = 0
|
||||
st.prev_redundancy = false
|
||||
st.last_packet_duration = 0
|
||||
st.rangeFinal = 0
|
||||
end
|
||||
|
||||
-- out = w^2 * in2 + (1 - w^2) * in1 over `overlap` samples per channel
|
||||
local function smoothFade(in1: { number }, b1: number, in2: { number }, b2: number, out: { number }, bo: number, overlap: number, ch: number)
|
||||
for c = 0, ch - 1 do
|
||||
for i = 0, overlap - 1 do
|
||||
local w = WINDOW[i + 1]
|
||||
w *= w
|
||||
local k = i * ch + c
|
||||
out[bo + k] = w * in2[b2 + k] + (1 - w) * in1[b1 + k]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local function decodeFrame(st: State, buf: buffer?, off: number, len: number, pcm: { number }, pbase: number, frameSize: number): number
|
||||
local ch = st.channels
|
||||
local celt = st.celt
|
||||
if frameSize < F2_5 then
|
||||
return -2
|
||||
end
|
||||
frameSize = min(frameSize, 2880)
|
||||
local data = buf
|
||||
if len <= 1 then
|
||||
data = nil
|
||||
frameSize = min(frameSize, st.frame_size)
|
||||
end
|
||||
local audiosize, mode, bandwidth
|
||||
local dec = EMPTY_EC
|
||||
if data then
|
||||
audiosize = st.frame_size
|
||||
mode = st.mode
|
||||
bandwidth = st.bandwidth
|
||||
dec = RD.new(data, off, len)
|
||||
else
|
||||
audiosize = frameSize
|
||||
mode = if st.prev_redundancy then MODE_CELT else st.prev_mode
|
||||
bandwidth = 0
|
||||
if mode == 0 then
|
||||
for i = 0, audiosize * ch - 1 do
|
||||
pcm[pbase + i] = 0
|
||||
end
|
||||
return audiosize
|
||||
end
|
||||
if audiosize > F20 then
|
||||
repeat
|
||||
local ret = decodeFrame(st, nil, 0, 0, pcm, pbase, min(audiosize, F20))
|
||||
if ret < 0 then
|
||||
return ret
|
||||
end
|
||||
pbase += ret * ch
|
||||
audiosize -= ret
|
||||
until audiosize <= 0
|
||||
return frameSize
|
||||
elseif audiosize < F20 then
|
||||
if audiosize > F10 then
|
||||
audiosize = F10
|
||||
elseif mode ~= MODE_SILK and audiosize > F5 and audiosize < F10 then
|
||||
audiosize = F5
|
||||
end
|
||||
end
|
||||
end
|
||||
local celtAccum = mode ~= MODE_CELT
|
||||
local transition = false
|
||||
if data and st.prev_mode > 0
|
||||
and ((mode == MODE_CELT and st.prev_mode ~= MODE_CELT and not st.prev_redundancy)
|
||||
or (mode ~= MODE_CELT and st.prev_mode == MODE_CELT)) then
|
||||
transition = true
|
||||
end
|
||||
if transition and mode == MODE_CELT then
|
||||
decodeFrame(st, nil, 0, 0, TRANS, 1, min(F5, audiosize))
|
||||
end
|
||||
if audiosize > frameSize then
|
||||
return -1
|
||||
end
|
||||
frameSize = audiosize
|
||||
|
||||
-- SILK
|
||||
if mode ~= MODE_CELT then
|
||||
local tooSmall = frameSize < F10
|
||||
local sarr, sbase = pcm, pbase
|
||||
if tooSmall then
|
||||
sarr, sbase = SILKTMP, 1
|
||||
end
|
||||
if st.prev_mode == MODE_CELT then
|
||||
Silk.reset(st.silk)
|
||||
end
|
||||
local ctrl = st.ctrl
|
||||
ctrl.payloadSize_ms = max(10, 1000 * audiosize // 48000)
|
||||
if data then
|
||||
ctrl.nChannelsInternal = st.stream_channels
|
||||
if mode == MODE_SILK then
|
||||
ctrl.internalSampleRate = if bandwidth == BW_NB then 8000 elseif bandwidth == BW_MB then 12000 else 16000
|
||||
else
|
||||
ctrl.internalSampleRate = 16000
|
||||
end
|
||||
end
|
||||
local lost = if data == nil then 1 else 0
|
||||
local decoded = 0
|
||||
repeat
|
||||
local ret, n = Silk.decode(st.silk, ctrl, lost, decoded == 0, dec, sarr, sbase + decoded * ch)
|
||||
if ret ~= 0 or n <= 0 then
|
||||
if lost ~= 0 or n <= 0 then
|
||||
n = frameSize - decoded
|
||||
for i = 0, n * ch - 1 do
|
||||
sarr[sbase + decoded * ch + i] = 0
|
||||
end
|
||||
else
|
||||
return -3
|
||||
end
|
||||
end
|
||||
decoded += n
|
||||
until decoded >= frameSize
|
||||
if tooSmall then
|
||||
for i = 0, frameSize * ch - 1 do
|
||||
pcm[pbase + i] = SILKTMP[i + 1]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local startBand = 0
|
||||
local redundancy, celtToSilk = false, false
|
||||
local redundancyBytes = 0
|
||||
if mode ~= MODE_CELT and data and RD.tell(dec) + 17 + (if mode == MODE_HYBRID then 20 else 0) <= 8 * len then
|
||||
if mode == MODE_HYBRID then
|
||||
redundancy = RD.bitLogp(dec, 12)
|
||||
else
|
||||
redundancy = true
|
||||
end
|
||||
if redundancy then
|
||||
celtToSilk = RD.bitLogp(dec, 1)
|
||||
redundancyBytes = if mode == MODE_HYBRID then RD.uint(dec, 256) + 2 else len - bit32.rshift(RD.tell(dec) + 7, 3)
|
||||
len -= redundancyBytes
|
||||
if len * 8 < RD.tell(dec) then
|
||||
len = 0
|
||||
redundancyBytes = 0
|
||||
redundancy = false
|
||||
end
|
||||
dec.storage -= redundancyBytes
|
||||
end
|
||||
end
|
||||
if mode ~= MODE_CELT then
|
||||
startBand = 17
|
||||
end
|
||||
if redundancy then
|
||||
transition = false
|
||||
end
|
||||
if transition and mode ~= MODE_CELT then
|
||||
decodeFrame(st, nil, 0, 0, TRANS, 1, min(F5, audiosize))
|
||||
end
|
||||
if bandwidth ~= 0 then
|
||||
celt["end"] = if bandwidth == BW_NB then 13 elseif bandwidth == BW_MB or bandwidth == BW_WB then 17 elseif bandwidth == BW_SWB then 19 else 21
|
||||
end
|
||||
celt.stream_channels = st.stream_channels
|
||||
local redundantRng = 0
|
||||
if redundancy and celtToSilk then
|
||||
celt.start = 0
|
||||
Celt.decode(celt, data, off + len, redundancyBytes, REDUND, 1, F5, nil, false)
|
||||
redundantRng = celt.rng
|
||||
end
|
||||
celt.start = startBand
|
||||
local celtRet = 0
|
||||
if mode ~= MODE_SILK then
|
||||
local celtFrameSize = min(F20, frameSize)
|
||||
if mode ~= st.prev_mode and st.prev_mode > 0 and not st.prev_redundancy then
|
||||
Celt.reset(celt)
|
||||
end
|
||||
celtRet = Celt.decode(celt, data, off, len, pcm, pbase, celtFrameSize, if data then dec else nil, celtAccum)
|
||||
st.rangeFinal = celt.rng
|
||||
else
|
||||
if st.prev_mode == MODE_HYBRID and not (redundancy and celtToSilk and st.prev_redundancy) then
|
||||
celt.start = 0
|
||||
Celt.decode(celt, SILENCE, 0, 2, pcm, pbase, F2_5, nil, celtAccum)
|
||||
end
|
||||
st.rangeFinal = if data then dec.rng else 0
|
||||
end
|
||||
if redundancy and not celtToSilk then
|
||||
Celt.reset(celt)
|
||||
celt.start = 0
|
||||
Celt.decode(celt, data, off + len, redundancyBytes, REDUND, 1, F5, nil, false)
|
||||
redundantRng = celt.rng
|
||||
local b = pbase + ch * (frameSize - F2_5)
|
||||
smoothFade(pcm, b, REDUND, 1 + ch * F2_5, pcm, b, F2_5, ch)
|
||||
end
|
||||
if redundancy and celtToSilk and (st.prev_mode ~= MODE_SILK or st.prev_redundancy) then
|
||||
for i = 0, F2_5 * ch - 1 do
|
||||
pcm[pbase + i] = REDUND[1 + i]
|
||||
end
|
||||
smoothFade(REDUND, 1 + ch * F2_5, pcm, pbase + ch * F2_5, pcm, pbase + ch * F2_5, F2_5, ch)
|
||||
end
|
||||
if transition then
|
||||
if audiosize >= F5 then
|
||||
for i = 0, ch * F2_5 - 1 do
|
||||
pcm[pbase + i] = TRANS[1 + i]
|
||||
end
|
||||
smoothFade(TRANS, 1 + ch * F2_5, pcm, pbase + ch * F2_5, pcm, pbase + ch * F2_5, F2_5, ch)
|
||||
else
|
||||
smoothFade(TRANS, 1, pcm, pbase, pcm, pbase, F2_5, ch)
|
||||
end
|
||||
end
|
||||
if len <= 1 then
|
||||
st.rangeFinal = 0
|
||||
else
|
||||
st.rangeFinal = bit32.bxor(st.rangeFinal, redundantRng)
|
||||
end
|
||||
st.prev_mode = mode
|
||||
st.prev_redundancy = redundancy and not celtToSilk
|
||||
if celtRet < 0 then
|
||||
return celtRet
|
||||
end
|
||||
return audiosize
|
||||
end
|
||||
|
||||
-- opus_decode_native: decodes one packet (buf == nil or len == 0: conceal frameSize samples). pcm[pbase + i * ch + c].
|
||||
-- Returns the samples per channel (or < 0 on error) and the packet's size (self-delimited packets).
|
||||
function Decoder.decode(st: State, buf: buffer?, off: number, len: number, pcm: { number }, pbase: number, frameSize: number, selfDelimited: boolean?): (number, number)
|
||||
local ch = st.channels
|
||||
if buf == nil or len == 0 then
|
||||
if frameSize % 120 ~= 0 then
|
||||
return -1, 0
|
||||
end
|
||||
local count = 0
|
||||
repeat
|
||||
local ret = decodeFrame(st, nil, 0, 0, pcm, pbase + count * ch, frameSize - count)
|
||||
if ret <= 0 then
|
||||
return if ret < 0 then ret else -3, 0
|
||||
end
|
||||
count += ret
|
||||
until count >= frameSize
|
||||
st.last_packet_duration = count
|
||||
return count, 0
|
||||
end
|
||||
local b = buf :: buffer
|
||||
local toc = readu8(b, off)
|
||||
local packetMode = Packet.mode(toc)
|
||||
local packetBandwidth = Packet.bandwidth(toc)
|
||||
local packetFrameSize = Packet.samplesPerFrame(toc, 48000)
|
||||
local packetChannels = Packet.channels(toc)
|
||||
local count, _, packetOffset = Packet.parse(b, off, len, OFFS, SIZES, selfDelimited)
|
||||
if count < 0 then
|
||||
return -4, 0
|
||||
end
|
||||
if count * packetFrameSize > frameSize then
|
||||
return -2, 0
|
||||
end
|
||||
st.mode = packetMode
|
||||
st.bandwidth = packetBandwidth
|
||||
st.frame_size = packetFrameSize
|
||||
st.stream_channels = packetChannels
|
||||
local nb = 0
|
||||
for i = 1, count do
|
||||
local ret = decodeFrame(st, b, OFFS[i], SIZES[i], pcm, pbase + nb * ch, frameSize - nb)
|
||||
if ret < 0 then
|
||||
return ret, 0
|
||||
end
|
||||
nb += ret
|
||||
end
|
||||
st.last_packet_duration = nb
|
||||
return nb, packetOffset
|
||||
end
|
||||
|
||||
-- ---------------------------------------------------------------------------------------------------------------
|
||||
-- multistream (channel mapping families 0 / 1 / 255)
|
||||
|
||||
export type MS = {
|
||||
channels: number,
|
||||
streams: number,
|
||||
coupled: number,
|
||||
mapping: { number },
|
||||
decoders: { State },
|
||||
tmp: { number },
|
||||
}
|
||||
|
||||
function Decoder.newMultistream(channels: number, streams: number, coupled: number, mapping: { number }): MS
|
||||
local decs = {}
|
||||
for s = 0, streams - 1 do
|
||||
decs[s + 1] = Decoder.new(if s < coupled then 2 else 1)
|
||||
end
|
||||
return {
|
||||
channels = channels,
|
||||
streams = streams,
|
||||
coupled = coupled,
|
||||
mapping = mapping,
|
||||
decoders = decs,
|
||||
tmp = table.create(5760 * 2, 0),
|
||||
}
|
||||
end
|
||||
|
||||
function Decoder.resetMultistream(ms: MS)
|
||||
for _, d in ms.decoders do
|
||||
Decoder.reset(d)
|
||||
end
|
||||
end
|
||||
|
||||
-- copies channel `sc` of src (stride sstride) into every output channel mapped to stream channel index `id`
|
||||
local function copyOut(ms: MS, id: number, src: { number }, sc: number, sstride: number, pcm: { number }, pbase: number, n: number)
|
||||
local nch = ms.channels
|
||||
for c = 0, nch - 1 do
|
||||
if ms.mapping[c + 1] == id then
|
||||
local o = pbase + c
|
||||
local s = 1 + sc
|
||||
for _ = 1, n do
|
||||
pcm[o] = src[s]
|
||||
o += nch
|
||||
s += sstride
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- opus_multistream_decode_native (float). Returns the samples per channel, or < 0 on error.
|
||||
function Decoder.decodeMultistream(ms: MS, buf: buffer?, off: number, len: number, pcm: { number }, pbase: number, frameSize: number): number
|
||||
local doPlc = buf == nil or len == 0
|
||||
frameSize = min(frameSize, 5760)
|
||||
if not doPlc and len < 2 * ms.streams - 1 then
|
||||
return -4
|
||||
end
|
||||
local tmp = ms.tmp
|
||||
for s = 0, ms.streams - 1 do
|
||||
local d = ms.decoders[s + 1]
|
||||
if not doPlc and len <= 0 then
|
||||
return -3
|
||||
end
|
||||
local ret, packetOffset = Decoder.decode(d, if doPlc then nil else buf, off, if doPlc then 0 else len, tmp, 1, frameSize, s ~= ms.streams - 1)
|
||||
if not doPlc then
|
||||
off += packetOffset
|
||||
len -= packetOffset
|
||||
end
|
||||
if ret <= 0 then
|
||||
return if ret < 0 then ret else -3
|
||||
end
|
||||
frameSize = ret
|
||||
if s < ms.coupled then
|
||||
copyOut(ms, s * 2, tmp, 0, 2, pcm, pbase, frameSize)
|
||||
copyOut(ms, s * 2 + 1, tmp, 1, 2, pcm, pbase, frameSize)
|
||||
else
|
||||
copyOut(ms, s + ms.coupled, tmp, 0, 1, pcm, pbase, frameSize)
|
||||
end
|
||||
end
|
||||
local nch = ms.channels
|
||||
for c = 0, nch - 1 do
|
||||
if ms.mapping[c + 1] == 255 then
|
||||
local o = pbase + c
|
||||
for _ = 1, frameSize do
|
||||
pcm[o] = 0
|
||||
o += nch
|
||||
end
|
||||
end
|
||||
end
|
||||
return frameSize
|
||||
end
|
||||
|
||||
-- writes src[base + 1 .. base + count] * gain to out as f32 from byte offset o
|
||||
function Decoder.writeF32(src: { number }, base: number, count: number, gain: number, out: buffer, o: number)
|
||||
local writef32 = buffer.writef32
|
||||
if gain == 1 then
|
||||
for i = base + 1, base + count do
|
||||
writef32(out, o, src[i])
|
||||
o += 4
|
||||
end
|
||||
else
|
||||
for i = base + 1, base + count do
|
||||
writef32(out, o, src[i] * gain)
|
||||
o += 4
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- Stereo downmix of n frames of srcCh-channel audio (src[base + 1 ..], interleaved): matrix[c * 2 + 1 / + 2] are the
|
||||
-- left / right weights of channel c. Writes f32 to out from byte offset o.
|
||||
function Decoder.writeDownmix(src: { number }, base: number, n: number, srcCh: number, matrix: { number }, gain: number, out: buffer, o: number)
|
||||
local writef32 = buffer.writef32
|
||||
local p = base
|
||||
for _ = 1, n do
|
||||
local l, r = 0, 0
|
||||
for c = 0, srcCh - 1 do
|
||||
local v = src[p + c + 1]
|
||||
l += v * matrix[c * 2 + 1]
|
||||
r += v * matrix[c * 2 + 2]
|
||||
end
|
||||
writef32(out, o, l * gain)
|
||||
writef32(out, o + 4, r * gain)
|
||||
o += 8
|
||||
p += srcCh
|
||||
end
|
||||
end
|
||||
|
||||
return Decoder
|
||||
@@ -0,0 +1,346 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- codecs/audio/Opus/Mdct: CELT inverse MDCT (libopus celt/mdct.c clt_mdct_backward) over a mixed-radix complex FFT (a port of the
|
||||
-- kiss_fft stages used by libopus: radix 5, 3, 4 and 2). The 48 kHz mode uses N = 1920 >> shift for
|
||||
-- shift = 0..3 (FFT sizes 480, 240, 120, 60). Arrays are Luau tables of numbers, 1-based.
|
||||
-- Why not codecs/audio/Mdct (measured by the cleanup pass): the shared transform computes the full N-sample IMDCT through a
|
||||
-- DCT-IV, of which CELT keeps the middle N / 2, so routing the decoder through it made CELT decoding 2-3x slower
|
||||
-- (luau: 25 -> 54-76 ms for 3 s of 32 kbps stereo) and changed the output bits (same SNR against opusdec). This
|
||||
-- fused libopus port (pre-rotation, a kiss_fft with fixed factors, post-rotation, TDAC) stays private.
|
||||
local Mdct = {}
|
||||
|
||||
local sin, cos, pi = math.sin, math.cos, math.pi
|
||||
|
||||
type Plan = {
|
||||
n: number, -- FFT size (N / 4)
|
||||
factors: { number },
|
||||
bitrev: { number },
|
||||
twr: { number },
|
||||
twi: { number },
|
||||
trig: { number }, -- MDCT twiddles: (-sin, cos) pairs, N / 2 values
|
||||
fr: { number },
|
||||
fi: { number },
|
||||
}
|
||||
|
||||
local FACTORS = {
|
||||
[0] = { 5, 96, 3, 32, 4, 8, 2, 4, 4, 1 },
|
||||
[1] = { 5, 48, 3, 16, 4, 4, 4, 1 },
|
||||
[2] = { 5, 24, 3, 8, 2, 4, 4, 1 },
|
||||
[3] = { 5, 12, 3, 4, 4, 1 },
|
||||
}
|
||||
|
||||
local function bitrevTable(f: { number }, fout: number, fpos: number, fstride: number, factors: { number }, fi: number)
|
||||
local p = factors[fi]
|
||||
local m = factors[fi + 1]
|
||||
if m == 1 then
|
||||
for j = 0, p - 1 do
|
||||
f[fpos + 1] = fout + j
|
||||
fpos += fstride
|
||||
end
|
||||
else
|
||||
for j = 0, p - 1 do
|
||||
bitrevTable(f, fout, fpos, fstride * p, factors, fi + 2)
|
||||
fpos += fstride
|
||||
fout += m
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local plans: { Plan } = {}
|
||||
for shift = 0, 3 do
|
||||
local N = 1920 // 2 ^ shift
|
||||
local n = N // 4
|
||||
local factors = FACTORS[shift]
|
||||
local bitrev = table.create(n, 0)
|
||||
bitrevTable(bitrev, 0, 0, 1, factors, 1)
|
||||
local twr, twi = table.create(n, 0), table.create(n, 0)
|
||||
for k = 0, n - 1 do
|
||||
local ph = -2 * pi * k / n
|
||||
twr[k + 1] = cos(ph)
|
||||
twi[k + 1] = sin(ph)
|
||||
end
|
||||
local N2, N4 = N // 2, N // 4
|
||||
local trig = table.create(N2, 0)
|
||||
for i = 0, N4 - 1 do
|
||||
trig[2 * i + 1] = -sin(2 * pi * (i + 0.125) / N)
|
||||
trig[2 * i + 2] = cos(2 * pi * (i + 0.125) / N)
|
||||
end
|
||||
plans[shift + 1] = {
|
||||
n = n,
|
||||
factors = factors,
|
||||
bitrev = bitrev,
|
||||
twr = twr,
|
||||
twi = twi,
|
||||
trig = trig,
|
||||
fr = table.create(n, 0),
|
||||
fi = table.create(n, 0),
|
||||
}
|
||||
end
|
||||
|
||||
local function bfly2(fr: { number }, fi: { number }, N: number)
|
||||
local tw = 0.7071067812
|
||||
local p = 1
|
||||
for _ = 1, N do
|
||||
-- k = 0
|
||||
local tr, ti = fr[p + 4], fi[p + 4]
|
||||
fr[p + 4] = fr[p] - tr
|
||||
fi[p + 4] = fi[p] - ti
|
||||
fr[p] += tr
|
||||
fi[p] += ti
|
||||
-- k = 1
|
||||
local a, b = fr[p + 5], fi[p + 5]
|
||||
tr = (a + b) * tw
|
||||
ti = (b - a) * tw
|
||||
fr[p + 5] = fr[p + 1] - tr
|
||||
fi[p + 5] = fi[p + 1] - ti
|
||||
fr[p + 1] += tr
|
||||
fi[p + 1] += ti
|
||||
-- k = 2
|
||||
tr = fi[p + 6]
|
||||
ti = -fr[p + 6]
|
||||
fr[p + 6] = fr[p + 2] - tr
|
||||
fi[p + 6] = fi[p + 2] - ti
|
||||
fr[p + 2] += tr
|
||||
fi[p + 2] += ti
|
||||
-- k = 3
|
||||
a, b = fr[p + 7], fi[p + 7]
|
||||
tr = (b - a) * tw
|
||||
ti = -(b + a) * tw
|
||||
fr[p + 7] = fr[p + 3] - tr
|
||||
fi[p + 7] = fi[p + 3] - ti
|
||||
fr[p + 3] += tr
|
||||
fi[p + 3] += ti
|
||||
p += 8
|
||||
end
|
||||
end
|
||||
|
||||
local function bfly4(fr: { number }, fi: { number }, fstride: number, twr: { number }, twi: { number }, m: number, N: number, mm: number)
|
||||
if m == 1 then
|
||||
local p = 1
|
||||
for _ = 1, N do
|
||||
local r0, i0 = fr[p], fi[p]
|
||||
local r1, i1 = fr[p + 1], fi[p + 1]
|
||||
local r2, i2 = fr[p + 2], fi[p + 2]
|
||||
local r3, i3 = fr[p + 3], fi[p + 3]
|
||||
local s0r, s0i = r0 - r2, i0 - i2
|
||||
r0 += r2
|
||||
i0 += i2
|
||||
local s1r, s1i = r1 + r3, i1 + i3
|
||||
fr[p + 2] = r0 - s1r
|
||||
fi[p + 2] = i0 - s1i
|
||||
fr[p] = r0 + s1r
|
||||
fi[p] = i0 + s1i
|
||||
s1r, s1i = r1 - r3, i1 - i3
|
||||
fr[p + 1] = s0r + s1i
|
||||
fi[p + 1] = s0i - s1r
|
||||
fr[p + 3] = s0r - s1i
|
||||
fi[p + 3] = s0i + s1r
|
||||
p += 4
|
||||
end
|
||||
else
|
||||
local m2, m3 = 2 * m, 3 * m
|
||||
for i = 0, N - 1 do
|
||||
local p = i * mm + 1
|
||||
local t1, t2, t3 = 1, 1, 1
|
||||
for _ = 0, m - 1 do
|
||||
local ar, ai = fr[p + m], fi[p + m]
|
||||
local wr, wi = twr[t1], twi[t1]
|
||||
local s0r, s0i = ar * wr - ai * wi, ar * wi + ai * wr
|
||||
ar, ai = fr[p + m2], fi[p + m2]
|
||||
wr, wi = twr[t2], twi[t2]
|
||||
local s1r, s1i = ar * wr - ai * wi, ar * wi + ai * wr
|
||||
ar, ai = fr[p + m3], fi[p + m3]
|
||||
wr, wi = twr[t3], twi[t3]
|
||||
local s2r, s2i = ar * wr - ai * wi, ar * wi + ai * wr
|
||||
local f0r, f0i = fr[p], fi[p]
|
||||
local s5r, s5i = f0r - s1r, f0i - s1i
|
||||
f0r += s1r
|
||||
f0i += s1i
|
||||
local s3r, s3i = s0r + s2r, s0i + s2i
|
||||
local s4r, s4i = s0r - s2r, s0i - s2i
|
||||
fr[p + m2] = f0r - s3r
|
||||
fi[p + m2] = f0i - s3i
|
||||
t1 += fstride
|
||||
t2 += fstride * 2
|
||||
t3 += fstride * 3
|
||||
fr[p] = f0r + s3r
|
||||
fi[p] = f0i + s3i
|
||||
fr[p + m] = s5r + s4i
|
||||
fi[p + m] = s5i - s4r
|
||||
fr[p + m3] = s5r - s4i
|
||||
fi[p + m3] = s5i + s4r
|
||||
p += 1
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local function bfly3(fr: { number }, fi: { number }, fstride: number, twr: { number }, twi: { number }, m: number, N: number, mm: number)
|
||||
local m2 = 2 * m
|
||||
local epi3i = twi[fstride * m + 1]
|
||||
for i = 0, N - 1 do
|
||||
local p = i * mm + 1
|
||||
local t1, t2 = 1, 1
|
||||
for _ = 1, m do
|
||||
local ar, ai = fr[p + m], fi[p + m]
|
||||
local wr, wi = twr[t1], twi[t1]
|
||||
local s1r, s1i = ar * wr - ai * wi, ar * wi + ai * wr
|
||||
ar, ai = fr[p + m2], fi[p + m2]
|
||||
wr, wi = twr[t2], twi[t2]
|
||||
local s2r, s2i = ar * wr - ai * wi, ar * wi + ai * wr
|
||||
local s3r, s3i = s1r + s2r, s1i + s2i
|
||||
local s0r, s0i = s1r - s2r, s1i - s2i
|
||||
t1 += fstride
|
||||
t2 += fstride * 2
|
||||
local f0r, f0i = fr[p], fi[p]
|
||||
local fmr, fmi = f0r - s3r * 0.5, f0i - s3i * 0.5
|
||||
s0r *= epi3i
|
||||
s0i *= epi3i
|
||||
fr[p] = f0r + s3r
|
||||
fi[p] = f0i + s3i
|
||||
fr[p + m2] = fmr + s0i
|
||||
fi[p + m2] = fmi - s0r
|
||||
fr[p + m] = fmr - s0i
|
||||
fi[p + m] = fmi + s0r
|
||||
p += 1
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local function bfly5(fr: { number }, fi: { number }, fstride: number, twr: { number }, twi: { number }, m: number, N: number, mm: number)
|
||||
local yar, yai = twr[fstride * m + 1], twi[fstride * m + 1]
|
||||
local ybr, ybi = twr[fstride * 2 * m + 1], twi[fstride * 2 * m + 1]
|
||||
for i = 0, N - 1 do
|
||||
local p0 = i * mm + 1
|
||||
local p1, p2, p3, p4 = p0 + m, p0 + 2 * m, p0 + 3 * m, p0 + 4 * m
|
||||
for u = 0, m - 1 do
|
||||
local s0r, s0i = fr[p0], fi[p0]
|
||||
local k = u * fstride
|
||||
local ar, ai = fr[p1], fi[p1]
|
||||
local wr, wi = twr[k + 1], twi[k + 1]
|
||||
local s1r, s1i = ar * wr - ai * wi, ar * wi + ai * wr
|
||||
ar, ai = fr[p2], fi[p2]
|
||||
wr, wi = twr[2 * k + 1], twi[2 * k + 1]
|
||||
local s2r, s2i = ar * wr - ai * wi, ar * wi + ai * wr
|
||||
ar, ai = fr[p3], fi[p3]
|
||||
wr, wi = twr[3 * k + 1], twi[3 * k + 1]
|
||||
local s3r, s3i = ar * wr - ai * wi, ar * wi + ai * wr
|
||||
ar, ai = fr[p4], fi[p4]
|
||||
wr, wi = twr[4 * k + 1], twi[4 * k + 1]
|
||||
local s4r, s4i = ar * wr - ai * wi, ar * wi + ai * wr
|
||||
|
||||
local s7r, s7i = s1r + s4r, s1i + s4i
|
||||
local s10r, s10i = s1r - s4r, s1i - s4i
|
||||
local s8r, s8i = s2r + s3r, s2i + s3i
|
||||
local s9r, s9i = s2r - s3r, s2i - s3i
|
||||
|
||||
fr[p0] = s0r + (s7r + s8r)
|
||||
fi[p0] = s0i + (s7i + s8i)
|
||||
|
||||
local s5r = s0r + (s7r * yar + s8r * ybr)
|
||||
local s5i = s0i + (s7i * yar + s8i * ybr)
|
||||
local s6r = s10i * yai + s9i * ybi
|
||||
local s6i = -(s10r * yai + s9r * ybi)
|
||||
fr[p1] = s5r - s6r
|
||||
fi[p1] = s5i - s6i
|
||||
fr[p4] = s5r + s6r
|
||||
fi[p4] = s5i + s6i
|
||||
|
||||
local s11r = s0r + (s7r * ybr + s8r * yar)
|
||||
local s11i = s0i + (s7i * ybr + s8i * yar)
|
||||
local s12r = s9i * yai - s10i * ybi
|
||||
local s12i = s10r * ybi - s9r * yai
|
||||
fr[p2] = s11r + s12r
|
||||
fi[p2] = s11i + s12i
|
||||
fr[p3] = s11r - s12r
|
||||
fi[p3] = s11i - s12i
|
||||
|
||||
p0 += 1
|
||||
p1 += 1
|
||||
p2 += 1
|
||||
p3 += 1
|
||||
p4 += 1
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- in-place forward FFT of bit-reversed input (opus_fft_impl)
|
||||
local function fftImpl(pl: Plan)
|
||||
local factors = pl.factors
|
||||
local fr, fi, twr, twi = pl.fr, pl.fi, pl.twr, pl.twi
|
||||
local fstride = { 1 }
|
||||
local L = 0
|
||||
local m
|
||||
repeat
|
||||
local p = factors[2 * L + 1]
|
||||
m = factors[2 * L + 2]
|
||||
fstride[L + 2] = fstride[L + 1] * p
|
||||
L += 1
|
||||
until m == 1
|
||||
m = factors[2 * L]
|
||||
for i = L - 1, 0, -1 do
|
||||
local m2 = if i ~= 0 then factors[2 * i] else 1
|
||||
local radix = factors[2 * i + 1]
|
||||
if radix == 2 then
|
||||
bfly2(fr, fi, fstride[i + 1])
|
||||
elseif radix == 4 then
|
||||
bfly4(fr, fi, fstride[i + 1], twr, twi, m, fstride[i + 1], m2)
|
||||
elseif radix == 3 then
|
||||
bfly3(fr, fi, fstride[i + 1], twr, twi, m, fstride[i + 1], m2)
|
||||
else
|
||||
bfly5(fr, fi, fstride[i + 1], twr, twi, m, fstride[i + 1], m2)
|
||||
end
|
||||
m = m2
|
||||
end
|
||||
end
|
||||
|
||||
-- clt_mdct_backward: input coefficients inp[inBase + k*stride] (k = 0 .. N/2-1, inBase is the 1-based index of
|
||||
-- coefficient 0), output written to out[outBase .. outBase + N/2 + overlap/2) with TDAC windowing of the first
|
||||
-- `overlap` samples (out[outBase] is sample 0).
|
||||
function Mdct.backward(inp: { number }, inBase: number, out: { number }, outBase: number, window: { number }, overlap: number, shift: number, stride: number)
|
||||
local pl = plans[shift + 1]
|
||||
local N4 = pl.n
|
||||
local N2 = N4 * 2
|
||||
local trig = pl.trig
|
||||
local bitrev = pl.bitrev
|
||||
local fr, fi = pl.fr, pl.fi
|
||||
-- pre-rotate, storing in bit-reversed order (real and imaginary swapped: an FFT is used instead of an IFFT)
|
||||
local xp1 = inBase
|
||||
local xp2 = inBase + stride * (N2 - 1)
|
||||
for i = 0, N4 - 1 do
|
||||
local x1 = inp[xp1]
|
||||
local x2 = inp[xp2]
|
||||
local t0, t1 = trig[2 * i + 1], trig[2 * i + 2]
|
||||
local rev = bitrev[i + 1] + 1
|
||||
fi[rev] = x2 * t1 + x1 * t0
|
||||
fr[rev] = x1 * t1 - x2 * t0
|
||||
xp1 += 2 * stride
|
||||
xp2 -= 2 * stride
|
||||
end
|
||||
fftImpl(pl)
|
||||
-- post-rotate
|
||||
local h = outBase + (overlap // 2)
|
||||
for k = 0, N4 - 1 do
|
||||
local re = fi[k + 1]
|
||||
local im = fr[k + 1]
|
||||
local t0, t1 = trig[2 * k + 1], trig[2 * k + 2]
|
||||
out[h + 2 * k] = re * t1 + im * t0
|
||||
out[h + N2 - 1 - 2 * k] = re * t0 - im * t1
|
||||
end
|
||||
-- mirror on both sides for TDAC
|
||||
local xp = outBase + overlap - 1
|
||||
local yp = outBase
|
||||
for i = 0, overlap // 2 - 1 do
|
||||
local x1 = out[xp]
|
||||
local x2 = out[yp]
|
||||
local w1 = window[i + 1]
|
||||
local w2 = window[overlap - i]
|
||||
out[yp] = x2 * w2 - x1 * w1
|
||||
out[xp] = x2 * w1 + x1 * w2
|
||||
xp -= 1
|
||||
yp += 1
|
||||
end
|
||||
end
|
||||
|
||||
return Mdct
|
||||
@@ -0,0 +1,235 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- codecs/audio/Opus/Packet: Opus packet framing (RFC 6716 section 3): the TOC byte and frame packing codes 0-3, with padding.
|
||||
local Packet = {}
|
||||
|
||||
local readu8 = buffer.readu8
|
||||
local band, rshift = bit32.band, bit32.rshift
|
||||
|
||||
Packet.MODE_SILK_ONLY = 1000
|
||||
Packet.MODE_HYBRID = 1001
|
||||
Packet.MODE_CELT_ONLY = 1002
|
||||
|
||||
-- bandwidths as in libopus (OPUS_BANDWIDTH_NARROWBAND .. FULLBAND)
|
||||
Packet.BW_NB = 1101
|
||||
Packet.BW_MB = 1102
|
||||
Packet.BW_WB = 1103
|
||||
Packet.BW_SWB = 1104
|
||||
Packet.BW_FB = 1105
|
||||
|
||||
function Packet.mode(toc: number): number
|
||||
if band(toc, 0x80) ~= 0 then
|
||||
return Packet.MODE_CELT_ONLY
|
||||
elseif band(toc, 0x60) == 0x60 then
|
||||
return Packet.MODE_HYBRID
|
||||
else
|
||||
return Packet.MODE_SILK_ONLY
|
||||
end
|
||||
end
|
||||
|
||||
function Packet.bandwidth(toc: number): number
|
||||
if band(toc, 0x80) ~= 0 then
|
||||
local bw = 1102 + band(rshift(toc, 5), 3)
|
||||
if bw == 1102 then
|
||||
bw = 1101
|
||||
end
|
||||
return bw
|
||||
elseif band(toc, 0x60) == 0x60 then
|
||||
return if band(toc, 0x10) ~= 0 then 1105 else 1104
|
||||
else
|
||||
return 1101 + band(rshift(toc, 5), 3)
|
||||
end
|
||||
end
|
||||
|
||||
function Packet.channels(toc: number): number
|
||||
return if band(toc, 4) ~= 0 then 2 else 1
|
||||
end
|
||||
|
||||
-- samples per frame at rate Fs
|
||||
function Packet.samplesPerFrame(toc: number, Fs: number): number
|
||||
if band(toc, 0x80) ~= 0 then
|
||||
local a = band(rshift(toc, 3), 3)
|
||||
return bit32.lshift(Fs, a) // 400
|
||||
elseif band(toc, 0x60) == 0x60 then
|
||||
return if band(toc, 0x08) ~= 0 then Fs // 50 else Fs // 100
|
||||
else
|
||||
local a = band(rshift(toc, 3), 3)
|
||||
if a == 3 then
|
||||
return Fs * 60 // 1000
|
||||
end
|
||||
return bit32.lshift(Fs, a) // 100
|
||||
end
|
||||
end
|
||||
|
||||
-- total samples of a packet at 48 kHz, or -1 if invalid
|
||||
function Packet.samples(buf: buffer, off: number, len: number): number
|
||||
if len < 1 then
|
||||
return -1
|
||||
end
|
||||
local toc = readu8(buf, off)
|
||||
local c = band(toc, 3)
|
||||
local count
|
||||
if c == 0 then
|
||||
count = 1
|
||||
elseif c ~= 3 then
|
||||
count = 2
|
||||
else
|
||||
if len < 2 then
|
||||
return -1
|
||||
end
|
||||
count = band(readu8(buf, off + 1), 0x3F)
|
||||
end
|
||||
local n = count * Packet.samplesPerFrame(toc, 48000)
|
||||
if n * 25 > 48000 * 3 then
|
||||
return -1
|
||||
end
|
||||
return n
|
||||
end
|
||||
|
||||
local function parseSize(buf: buffer, p: number, len: number): (number, number)
|
||||
if len < 1 then
|
||||
return -1, -1
|
||||
end
|
||||
local b0 = readu8(buf, p)
|
||||
if b0 < 252 then
|
||||
return 1, b0
|
||||
elseif len < 2 then
|
||||
return -1, -1
|
||||
end
|
||||
return 2, 4 * readu8(buf, p + 1) + b0
|
||||
end
|
||||
|
||||
-- Splits a packet (opus_packet_parse_impl). Fills offsets[i] / sizes[i] (1-based, absolute buffer offsets) and
|
||||
-- returns the frame count, the TOC byte and the packet's total size including padding (self-delimited packets, as in
|
||||
-- multistream, carry an explicit size for the last frame). The count is -1 for an invalid packet.
|
||||
function Packet.parse(buf: buffer, off: number, len: number, offsets: { number }, sizes: { number }, selfDelimited: boolean?): (number, number, number)
|
||||
if len < 1 then
|
||||
return -1, 0, 0
|
||||
end
|
||||
local toc = readu8(buf, off)
|
||||
local framesize = Packet.samplesPerFrame(toc, 48000)
|
||||
local p = off + 1
|
||||
len -= 1
|
||||
local lastSize = len
|
||||
local count
|
||||
local cbr = false
|
||||
local pad = 0
|
||||
local c = band(toc, 3)
|
||||
if c == 0 then
|
||||
count = 1
|
||||
elseif c == 1 then
|
||||
count = 2
|
||||
cbr = true
|
||||
if not selfDelimited then
|
||||
if band(len, 1) ~= 0 then
|
||||
return -1, toc, 0
|
||||
end
|
||||
lastSize = len // 2
|
||||
sizes[1] = lastSize
|
||||
end
|
||||
elseif c == 2 then
|
||||
count = 2
|
||||
local bytes, s = parseSize(buf, p, len)
|
||||
if bytes < 0 then
|
||||
return -1, toc, 0
|
||||
end
|
||||
len -= bytes
|
||||
if s < 0 or s > len then
|
||||
return -1, toc, 0
|
||||
end
|
||||
sizes[1] = s
|
||||
p += bytes
|
||||
lastSize = len - s
|
||||
else
|
||||
if len < 1 then
|
||||
return -1, toc, 0
|
||||
end
|
||||
local ch = readu8(buf, p)
|
||||
p += 1
|
||||
count = band(ch, 0x3F)
|
||||
if count <= 0 or framesize * count > 5760 then
|
||||
return -1, toc, 0
|
||||
end
|
||||
len -= 1
|
||||
if band(ch, 0x40) ~= 0 then
|
||||
local pb
|
||||
repeat
|
||||
if len <= 0 then
|
||||
return -1, toc, 0
|
||||
end
|
||||
pb = readu8(buf, p)
|
||||
p += 1
|
||||
len -= 1
|
||||
local tmp = if pb == 255 then 254 else pb
|
||||
len -= tmp
|
||||
pad += tmp
|
||||
until pb ~= 255
|
||||
end
|
||||
if len < 0 then
|
||||
return -1, toc, 0
|
||||
end
|
||||
cbr = band(ch, 0x80) == 0
|
||||
if not cbr then
|
||||
lastSize = len
|
||||
for i = 1, count - 1 do
|
||||
local bytes, s = parseSize(buf, p, len)
|
||||
if bytes < 0 then
|
||||
return -1, toc, 0
|
||||
end
|
||||
len -= bytes
|
||||
if s < 0 or s > len then
|
||||
return -1, toc, 0
|
||||
end
|
||||
sizes[i] = s
|
||||
p += bytes
|
||||
lastSize -= bytes + s
|
||||
end
|
||||
if lastSize < 0 then
|
||||
return -1, toc, 0
|
||||
end
|
||||
elseif not selfDelimited then
|
||||
lastSize = len // count
|
||||
if lastSize * count ~= len then
|
||||
return -1, toc, 0
|
||||
end
|
||||
for i = 1, count - 1 do
|
||||
sizes[i] = lastSize
|
||||
end
|
||||
end
|
||||
end
|
||||
if selfDelimited then
|
||||
local bytes, s = parseSize(buf, p, len)
|
||||
if bytes < 0 then
|
||||
return -1, toc, 0
|
||||
end
|
||||
len -= bytes
|
||||
if s < 0 or s > len then
|
||||
return -1, toc, 0
|
||||
end
|
||||
p += bytes
|
||||
sizes[count] = s
|
||||
if cbr then
|
||||
if s * count > len then
|
||||
return -1, toc, 0
|
||||
end
|
||||
for i = 1, count - 1 do
|
||||
sizes[i] = s
|
||||
end
|
||||
elseif bytes + s > lastSize then
|
||||
return -1, toc, 0
|
||||
end
|
||||
else
|
||||
if lastSize > 1275 then
|
||||
return -1, toc, 0
|
||||
end
|
||||
sizes[count] = lastSize
|
||||
end
|
||||
for i = 1, count do
|
||||
offsets[i] = p
|
||||
p += sizes[i]
|
||||
end
|
||||
return count, toc, pad + (p - off)
|
||||
end
|
||||
|
||||
return Packet
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,372 @@
|
||||
--!strict
|
||||
--!optimize 2
|
||||
-- codecs/audio/Opus/SilkTables: SILK decoder tables (generated from libopus silk/tables_*.c, pitch_est_tables.c, resampler_rom.c; BSD, see
|
||||
-- licenses/). Multi-dimensional C arrays are flattened row-major; every array is 1-based.
|
||||
local T = {}
|
||||
T.LTP_per_index_iCDF = {
|
||||
179, 99, 0,
|
||||
}
|
||||
T.LTP_gain_iCDF_0 = {
|
||||
71, 56, 43, 30, 21, 12, 6, 0,
|
||||
}
|
||||
T.LTP_gain_iCDF_1 = {
|
||||
199, 165, 144, 124, 109, 96, 84, 71, 61, 51, 42, 32, 23, 15, 8, 0,
|
||||
}
|
||||
T.LTP_gain_iCDF_2 = {
|
||||
241, 225, 211, 199, 187, 175, 164, 153, 142, 132, 123, 114, 105, 96, 88, 80, 72, 64, 57, 50,
|
||||
44, 38, 33, 29, 24, 20, 16, 12, 9, 5, 2, 0,
|
||||
}
|
||||
T.LTP_gain_vq_0 = {
|
||||
4, 6, 24, 7, 5, 0, 0, 2, 0, 0, 12, 28, 41, 13, -4, -9, 15, 42, 25, 14,
|
||||
1, -2, 62, 41, -9, -10, 37, 65, -4, 3, -6, 4, 66, 7, -8, 16, 14, 38, -3, 33,
|
||||
}
|
||||
T.LTP_gain_vq_1 = {
|
||||
13, 22, 39, 23, 12, -1, 36, 64, 27, -6, -7, 10, 55, 43, 17, 1, 1, 8, 1, 1,
|
||||
6, -11, 74, 53, -9, -12, 55, 76, -12, 8, -3, 3, 93, 27, -4, 26, 39, 59, 3, -8,
|
||||
2, 0, 77, 11, 9, -8, 22, 44, -6, 7, 40, 9, 26, 3, 9, -7, 20, 101, -7, 4,
|
||||
3, -8, 42, 26, 0, -15, 33, 68, 2, 23, -2, 55, 46, -2, 15, 3, -1, 21, 16, 41,
|
||||
}
|
||||
T.LTP_gain_vq_2 = {
|
||||
-6, 27, 61, 39, 5, -11, 42, 88, 4, 1, -2, 60, 65, 6, -4, -1, -5, 73, 56, 1,
|
||||
-9, 19, 94, 29, -9, 0, 12, 99, 6, 4, 8, -19, 102, 46, -13, 3, 2, 13, 3, 2,
|
||||
9, -21, 84, 72, -18, -11, 46, 104, -22, 8, 18, 38, 48, 23, 0, -16, 70, 83, -21, 11,
|
||||
5, -11, 117, 22, -8, -6, 23, 117, -12, 3, 3, -8, 95, 28, 4, -10, 15, 77, 60, -15,
|
||||
-1, 4, 124, 2, -4, 3, 38, 84, 24, -25, 2, 13, 42, 13, 31, 21, -4, 56, 46, -1,
|
||||
-1, 35, 79, -13, 19, -7, 65, 88, -9, -14, 20, 4, 81, 49, -29, 20, 0, 75, 3, -17,
|
||||
5, -9, 44, 92, -8, 1, -3, 22, 69, 31, -6, 95, 41, -12, 5, 39, 67, 16, -4, 1,
|
||||
0, -6, 120, 55, -36, -13, 44, 122, 4, -24, 81, 5, 11, 3, 7, 2, 0, 9, 10, 88,
|
||||
}
|
||||
T.NLSF_CB1_WB_Q8 = {
|
||||
7, 23, 38, 54, 69, 85, 100, 116, 131, 147, 162, 178, 193, 208, 223, 239, 13, 25, 41, 55,
|
||||
69, 83, 98, 112, 127, 142, 157, 171, 187, 203, 220, 236, 15, 21, 34, 51, 61, 78, 92, 106,
|
||||
126, 136, 152, 167, 185, 205, 225, 240, 10, 21, 36, 50, 63, 79, 95, 110, 126, 141, 157, 173,
|
||||
189, 205, 221, 237, 17, 20, 37, 51, 59, 78, 89, 107, 123, 134, 150, 164, 184, 205, 224, 240,
|
||||
10, 15, 32, 51, 67, 81, 96, 112, 129, 142, 158, 173, 189, 204, 220, 236, 8, 21, 37, 51,
|
||||
65, 79, 98, 113, 126, 138, 155, 168, 179, 192, 209, 218, 12, 15, 34, 55, 63, 78, 87, 108,
|
||||
118, 131, 148, 167, 185, 203, 219, 236, 16, 19, 32, 36, 56, 79, 91, 108, 118, 136, 154, 171,
|
||||
186, 204, 220, 237, 11, 28, 43, 58, 74, 89, 105, 120, 135, 150, 165, 180, 196, 211, 226, 241,
|
||||
6, 16, 33, 46, 60, 75, 92, 107, 123, 137, 156, 169, 185, 199, 214, 225, 11, 19, 30, 44,
|
||||
57, 74, 89, 105, 121, 135, 152, 169, 186, 202, 218, 234, 12, 19, 29, 46, 57, 71, 88, 100,
|
||||
120, 132, 148, 165, 182, 199, 216, 233, 17, 23, 35, 46, 56, 77, 92, 106, 123, 134, 152, 167,
|
||||
185, 204, 222, 237, 14, 17, 45, 53, 63, 75, 89, 107, 115, 132, 151, 171, 188, 206, 221, 240,
|
||||
9, 16, 29, 40, 56, 71, 88, 103, 119, 137, 154, 171, 189, 205, 222, 237, 16, 19, 36, 48,
|
||||
57, 76, 87, 105, 118, 132, 150, 167, 185, 202, 218, 236, 12, 17, 29, 54, 71, 81, 94, 104,
|
||||
126, 136, 149, 164, 182, 201, 221, 237, 15, 28, 47, 62, 79, 97, 115, 129, 142, 155, 168, 180,
|
||||
194, 208, 223, 238, 8, 14, 30, 45, 62, 78, 94, 111, 127, 143, 159, 175, 192, 207, 223, 239,
|
||||
17, 30, 49, 62, 79, 92, 107, 119, 132, 145, 160, 174, 190, 204, 220, 235, 14, 19, 36, 45,
|
||||
61, 76, 91, 108, 121, 138, 154, 172, 189, 205, 222, 238, 12, 18, 31, 45, 60, 76, 91, 107,
|
||||
123, 138, 154, 171, 187, 204, 221, 236, 13, 17, 31, 43, 53, 70, 83, 103, 114, 131, 149, 167,
|
||||
185, 203, 220, 237, 17, 22, 35, 42, 58, 78, 93, 110, 125, 139, 155, 170, 188, 206, 224, 240,
|
||||
8, 15, 34, 50, 67, 83, 99, 115, 131, 146, 162, 178, 193, 209, 224, 239, 13, 16, 41, 66,
|
||||
73, 86, 95, 111, 128, 137, 150, 163, 183, 206, 225, 241, 17, 25, 37, 52, 63, 75, 92, 102,
|
||||
119, 132, 144, 160, 175, 191, 212, 231, 19, 31, 49, 65, 83, 100, 117, 133, 147, 161, 174, 187,
|
||||
200, 213, 227, 242, 18, 31, 52, 68, 88, 103, 117, 126, 138, 149, 163, 177, 192, 207, 223, 239,
|
||||
16, 29, 47, 61, 76, 90, 106, 119, 133, 147, 161, 176, 193, 209, 224, 240, 15, 21, 35, 50,
|
||||
61, 73, 86, 97, 110, 119, 129, 141, 175, 198, 218, 237,
|
||||
}
|
||||
T.NLSF_CB1_WB_Wght_Q9 = {
|
||||
3657, 2925, 2925, 2925, 2925, 2925, 2925, 2925, 2925, 2925, 2925, 2925, 2963, 2963, 2925, 2846, 3216, 3085, 2972, 3056,
|
||||
3056, 3010, 3010, 3010, 2963, 2963, 3010, 2972, 2888, 2846, 2846, 2726, 3920, 4014, 2981, 3207, 3207, 2934, 3056, 2846,
|
||||
3122, 3244, 2925, 2846, 2620, 2553, 2780, 2925, 3516, 3197, 3010, 3103, 3019, 2888, 2925, 2925, 2925, 2925, 2888, 2888,
|
||||
2888, 2888, 2888, 2753, 5054, 5054, 2934, 3573, 3385, 3056, 3085, 2793, 3160, 3160, 2972, 2846, 2513, 2540, 2753, 2888,
|
||||
4428, 4149, 2700, 2753, 2972, 3010, 2925, 2846, 2981, 3019, 2925, 2925, 2925, 2925, 2888, 2726, 3620, 3019, 2972, 3056,
|
||||
3056, 2873, 2806, 3056, 3216, 3047, 2981, 3291, 3291, 2981, 3310, 2991, 5227, 5014, 2540, 3338, 3526, 3385, 3197, 3094,
|
||||
3376, 2981, 2700, 2647, 2687, 2793, 2846, 2673, 5081, 5174, 4615, 4428, 2460, 2897, 3047, 3207, 3169, 2687, 2740, 2888,
|
||||
2846, 2793, 2846, 2700, 3122, 2888, 2963, 2925, 2925, 2925, 2925, 2963, 2963, 2963, 2963, 2925, 2925, 2963, 2963, 2963,
|
||||
4202, 3207, 2981, 3103, 3010, 2888, 2888, 2925, 2972, 2873, 2916, 3019, 2972, 3010, 3197, 2873, 3760, 3760, 3244, 3103,
|
||||
2981, 2888, 2925, 2888, 2972, 2934, 2793, 2793, 2846, 2888, 2888, 2660, 3854, 4014, 3207, 3122, 3244, 2934, 3047, 2963,
|
||||
2963, 3085, 2846, 2793, 2793, 2793, 2793, 2580, 3845, 4080, 3357, 3516, 3094, 2740, 3010, 2934, 3122, 3085, 2846, 2846,
|
||||
2647, 2647, 2846, 2806, 5147, 4894, 3225, 3845, 3441, 3169, 2897, 3413, 3451, 2700, 2580, 2673, 2740, 2846, 2806, 2753,
|
||||
4109, 3789, 3291, 3160, 2925, 2888, 2888, 2925, 2793, 2740, 2793, 2740, 2793, 2846, 2888, 2806, 5081, 5054, 3047, 3545,
|
||||
3244, 3056, 3085, 2944, 3103, 2897, 2740, 2740, 2740, 2846, 2793, 2620, 4309, 4309, 2860, 2527, 3207, 3376, 3376, 3075,
|
||||
3075, 3376, 3056, 2846, 2647, 2580, 2726, 2753, 3056, 2916, 2806, 2888, 2740, 2687, 2897, 3103, 3150, 3150, 3216, 3169,
|
||||
3056, 3010, 2963, 2846, 4375, 3882, 2925, 2888, 2846, 2888, 2846, 2846, 2888, 2888, 2888, 2846, 2888, 2925, 2888, 2846,
|
||||
2981, 2916, 2916, 2981, 2981, 3056, 3122, 3216, 3150, 3056, 3010, 2972, 2972, 2972, 2925, 2740, 4229, 4149, 3310, 3347,
|
||||
2925, 2963, 2888, 2981, 2981, 2846, 2793, 2740, 2846, 2846, 2846, 2793, 4080, 4014, 3103, 3010, 2925, 2925, 2925, 2888,
|
||||
2925, 2925, 2846, 2846, 2846, 2793, 2888, 2780, 4615, 4575, 3169, 3441, 3207, 2981, 2897, 3038, 3122, 2740, 2687, 2687,
|
||||
2687, 2740, 2793, 2700, 4149, 4269, 3789, 3657, 2726, 2780, 2888, 2888, 3010, 2972, 2925, 2846, 2687, 2687, 2793, 2888,
|
||||
4215, 3554, 2753, 2846, 2846, 2888, 2888, 2888, 2925, 2925, 2888, 2925, 2925, 2925, 2963, 2888, 5174, 4921, 2261, 3432,
|
||||
3789, 3479, 3347, 2846, 3310, 3479, 3150, 2897, 2460, 2487, 2753, 2925, 3451, 3685, 3122, 3197, 3357, 3047, 3207, 3207,
|
||||
2981, 3216, 3085, 2925, 2925, 2687, 2540, 2434, 2981, 3010, 2793, 2793, 2740, 2793, 2846, 2972, 3056, 3103, 3150, 3150,
|
||||
3150, 3103, 3010, 3010, 2944, 2873, 2687, 2726, 2780, 3010, 3432, 3545, 3357, 3244, 3056, 3010, 2963, 2925, 2888, 2846,
|
||||
3019, 2944, 2897, 3010, 3010, 2972, 3019, 3103, 3056, 3056, 3010, 2888, 2846, 2925, 2925, 2888, 3920, 3967, 3010, 3197,
|
||||
3357, 3216, 3291, 3291, 3479, 3704, 3441, 2726, 2181, 2460, 2580, 2607,
|
||||
}
|
||||
T.NLSF_CB1_iCDF_WB = {
|
||||
225, 204, 201, 184, 183, 175, 158, 154, 153, 135, 119, 115, 113, 110, 109, 99, 98, 95, 79, 68,
|
||||
52, 50, 48, 45, 43, 32, 31, 27, 18, 10, 3, 0, 255, 251, 235, 230, 212, 201, 196, 182,
|
||||
167, 166, 163, 151, 138, 124, 110, 104, 90, 78, 76, 70, 69, 57, 45, 34, 24, 21, 11, 6,
|
||||
5, 4, 3, 0,
|
||||
}
|
||||
T.NLSF_CB2_SELECT_WB = {
|
||||
0, 0, 0, 0, 0, 0, 0, 1, 100, 102, 102, 68, 68, 36, 34, 96, 164, 107, 158, 185,
|
||||
180, 185, 139, 102, 64, 66, 36, 34, 34, 0, 1, 32, 208, 139, 141, 191, 152, 185, 155, 104,
|
||||
96, 171, 104, 166, 102, 102, 102, 132, 1, 0, 0, 0, 0, 16, 16, 0, 80, 109, 78, 107,
|
||||
185, 139, 103, 101, 208, 212, 141, 139, 173, 153, 123, 103, 36, 0, 0, 0, 0, 0, 0, 1,
|
||||
48, 0, 0, 0, 0, 0, 0, 32, 68, 135, 123, 119, 119, 103, 69, 98, 68, 103, 120, 118,
|
||||
118, 102, 71, 98, 134, 136, 157, 184, 182, 153, 139, 134, 208, 168, 248, 75, 189, 143, 121, 107,
|
||||
32, 49, 34, 34, 34, 0, 17, 2, 210, 235, 139, 123, 185, 137, 105, 134, 98, 135, 104, 182,
|
||||
100, 183, 171, 134, 100, 70, 68, 70, 66, 66, 34, 131, 64, 166, 102, 68, 36, 2, 1, 0,
|
||||
134, 166, 102, 68, 34, 34, 66, 132, 212, 246, 158, 139, 107, 107, 87, 102, 100, 219, 125, 122,
|
||||
137, 118, 103, 132, 114, 135, 137, 105, 171, 106, 50, 34, 164, 214, 141, 143, 185, 151, 121, 103,
|
||||
192, 34, 0, 0, 0, 0, 0, 1, 208, 109, 74, 187, 134, 249, 159, 137, 102, 110, 154, 118,
|
||||
87, 101, 119, 101, 0, 2, 0, 36, 36, 66, 68, 35, 96, 164, 102, 100, 36, 0, 2, 33,
|
||||
167, 138, 174, 102, 100, 84, 2, 2, 100, 107, 120, 119, 36, 197, 24, 0,
|
||||
}
|
||||
T.NLSF_CB2_iCDF_WB = {
|
||||
255, 254, 253, 244, 12, 3, 2, 1, 0, 255, 254, 252, 224, 38, 3, 2, 1, 0, 255, 254,
|
||||
251, 209, 57, 4, 2, 1, 0, 255, 254, 244, 195, 69, 4, 2, 1, 0, 255, 251, 232, 184,
|
||||
84, 7, 2, 1, 0, 255, 254, 240, 186, 86, 14, 2, 1, 0, 255, 254, 239, 178, 91, 30,
|
||||
5, 1, 0, 255, 248, 227, 177, 100, 19, 2, 1, 0,
|
||||
}
|
||||
T.NLSF_PRED_WB_Q8 = {
|
||||
175, 148, 160, 176, 178, 173, 174, 164, 177, 174, 196, 182, 198, 192, 182, 68, 62, 66, 60, 72,
|
||||
117, 85, 90, 118, 136, 151, 142, 160, 142, 155,
|
||||
}
|
||||
T.NLSF_DELTA_MIN_WB_Q15 = {
|
||||
100, 3, 40, 3, 3, 3, 5, 14, 14, 10, 11, 3, 8, 9, 7, 3, 347,
|
||||
}
|
||||
T.NLSF_CB1_NB_MB_Q8 = {
|
||||
12, 35, 60, 83, 108, 132, 157, 180, 206, 228, 15, 32, 55, 77, 101, 125, 151, 175, 201, 225,
|
||||
19, 42, 66, 89, 114, 137, 162, 184, 209, 230, 12, 25, 50, 72, 97, 120, 147, 172, 200, 223,
|
||||
26, 44, 69, 90, 114, 135, 159, 180, 205, 225, 13, 22, 53, 80, 106, 130, 156, 180, 205, 228,
|
||||
15, 25, 44, 64, 90, 115, 142, 168, 196, 222, 19, 24, 62, 82, 100, 120, 145, 168, 190, 214,
|
||||
22, 31, 50, 79, 103, 120, 151, 170, 203, 227, 21, 29, 45, 65, 106, 124, 150, 171, 196, 224,
|
||||
30, 49, 75, 97, 121, 142, 165, 186, 209, 229, 19, 25, 52, 70, 93, 116, 143, 166, 192, 219,
|
||||
26, 34, 62, 75, 97, 118, 145, 167, 194, 217, 25, 33, 56, 70, 91, 113, 143, 165, 196, 223,
|
||||
21, 34, 51, 72, 97, 117, 145, 171, 196, 222, 20, 29, 50, 67, 90, 117, 144, 168, 197, 221,
|
||||
22, 31, 48, 66, 95, 117, 146, 168, 196, 222, 24, 33, 51, 77, 116, 134, 158, 180, 200, 224,
|
||||
21, 28, 70, 87, 106, 124, 149, 170, 194, 217, 26, 33, 53, 64, 83, 117, 152, 173, 204, 225,
|
||||
27, 34, 65, 95, 108, 129, 155, 174, 210, 225, 20, 26, 72, 99, 113, 131, 154, 176, 200, 219,
|
||||
34, 43, 61, 78, 93, 114, 155, 177, 205, 229, 23, 29, 54, 97, 124, 138, 163, 179, 209, 229,
|
||||
30, 38, 56, 89, 118, 129, 158, 178, 200, 231, 21, 29, 49, 63, 85, 111, 142, 163, 193, 222,
|
||||
27, 48, 77, 103, 133, 158, 179, 196, 215, 232, 29, 47, 74, 99, 124, 151, 176, 198, 220, 237,
|
||||
33, 42, 61, 76, 93, 121, 155, 174, 207, 225, 29, 53, 87, 112, 136, 154, 170, 188, 208, 227,
|
||||
24, 30, 52, 84, 131, 150, 166, 186, 203, 229, 37, 48, 64, 84, 104, 118, 156, 177, 201, 230,
|
||||
}
|
||||
T.NLSF_CB1_Wght_Q9 = {
|
||||
2897, 2314, 2314, 2314, 2287, 2287, 2314, 2300, 2327, 2287, 2888, 2580, 2394, 2367, 2314, 2274, 2274, 2274, 2274, 2194,
|
||||
2487, 2340, 2340, 2314, 2314, 2314, 2340, 2340, 2367, 2354, 3216, 2766, 2340, 2340, 2314, 2274, 2221, 2207, 2261, 2194,
|
||||
2460, 2474, 2367, 2394, 2394, 2394, 2394, 2367, 2407, 2314, 3479, 3056, 2127, 2207, 2274, 2274, 2274, 2287, 2314, 2261,
|
||||
3282, 3141, 2580, 2394, 2247, 2221, 2207, 2194, 2194, 2114, 4096, 3845, 2221, 2620, 2620, 2407, 2314, 2394, 2367, 2074,
|
||||
3178, 3244, 2367, 2221, 2553, 2434, 2340, 2314, 2167, 2221, 3338, 3488, 2726, 2194, 2261, 2460, 2354, 2367, 2207, 2101,
|
||||
2354, 2420, 2327, 2367, 2394, 2420, 2420, 2420, 2460, 2367, 3779, 3629, 2434, 2527, 2367, 2274, 2274, 2300, 2207, 2048,
|
||||
3254, 3225, 2713, 2846, 2447, 2327, 2300, 2300, 2274, 2127, 3263, 3300, 2753, 2806, 2447, 2261, 2261, 2247, 2127, 2101,
|
||||
2873, 2981, 2633, 2367, 2407, 2354, 2194, 2247, 2247, 2114, 3225, 3197, 2633, 2580, 2274, 2181, 2247, 2221, 2221, 2141,
|
||||
3178, 3310, 2740, 2407, 2274, 2274, 2274, 2287, 2194, 2114, 3141, 3272, 2460, 2061, 2287, 2500, 2367, 2487, 2434, 2181,
|
||||
3507, 3282, 2314, 2700, 2647, 2474, 2367, 2394, 2340, 2127, 3423, 3535, 3038, 3056, 2300, 1950, 2221, 2274, 2274, 2274,
|
||||
3404, 3366, 2087, 2687, 2873, 2354, 2420, 2274, 2474, 2540, 3760, 3488, 1950, 2660, 2897, 2527, 2394, 2367, 2460, 2261,
|
||||
3028, 3272, 2740, 2888, 2740, 2154, 2127, 2287, 2234, 2247, 3695, 3657, 2025, 1969, 2660, 2700, 2580, 2500, 2327, 2367,
|
||||
3207, 3413, 2354, 2074, 2888, 2888, 2340, 2487, 2247, 2167, 3338, 3366, 2846, 2780, 2327, 2154, 2274, 2287, 2114, 2061,
|
||||
2327, 2300, 2181, 2167, 2181, 2367, 2633, 2700, 2700, 2553, 2407, 2434, 2221, 2261, 2221, 2221, 2340, 2420, 2607, 2700,
|
||||
3038, 3244, 2806, 2888, 2474, 2074, 2300, 2314, 2354, 2380, 2221, 2154, 2127, 2287, 2500, 2793, 2793, 2620, 2580, 2367,
|
||||
3676, 3713, 2234, 1838, 2181, 2753, 2726, 2673, 2513, 2207, 2793, 3160, 2726, 2553, 2846, 2513, 2181, 2394, 2221, 2181,
|
||||
}
|
||||
T.NLSF_CB1_iCDF_NB_MB = {
|
||||
212, 178, 148, 129, 108, 96, 85, 82, 79, 77, 61, 59, 57, 56, 51, 49, 48, 45, 42, 41,
|
||||
40, 38, 36, 34, 31, 30, 21, 12, 10, 3, 1, 0, 255, 245, 244, 236, 233, 225, 217, 203,
|
||||
190, 176, 175, 161, 149, 136, 125, 114, 102, 91, 81, 71, 60, 52, 43, 35, 28, 20, 19, 18,
|
||||
12, 11, 5, 0,
|
||||
}
|
||||
T.NLSF_CB2_SELECT_NB_MB = {
|
||||
16, 0, 0, 0, 0, 99, 66, 36, 36, 34, 36, 34, 34, 34, 34, 83, 69, 36, 52, 34,
|
||||
116, 102, 70, 68, 68, 176, 102, 68, 68, 34, 65, 85, 68, 84, 36, 116, 141, 152, 139, 170,
|
||||
132, 187, 184, 216, 137, 132, 249, 168, 185, 139, 104, 102, 100, 68, 68, 178, 218, 185, 185, 170,
|
||||
244, 216, 187, 187, 170, 244, 187, 187, 219, 138, 103, 155, 184, 185, 137, 116, 183, 155, 152, 136,
|
||||
132, 217, 184, 184, 170, 164, 217, 171, 155, 139, 244, 169, 184, 185, 170, 164, 216, 223, 218, 138,
|
||||
214, 143, 188, 218, 168, 244, 141, 136, 155, 170, 168, 138, 220, 219, 139, 164, 219, 202, 216, 137,
|
||||
168, 186, 246, 185, 139, 116, 185, 219, 185, 138, 100, 100, 134, 100, 102, 34, 68, 68, 100, 68,
|
||||
168, 203, 221, 218, 168, 167, 154, 136, 104, 70, 164, 246, 171, 137, 139, 137, 155, 218, 219, 139,
|
||||
}
|
||||
T.NLSF_CB2_iCDF_NB_MB = {
|
||||
255, 254, 253, 238, 14, 3, 2, 1, 0, 255, 254, 252, 218, 35, 3, 2, 1, 0, 255, 254,
|
||||
250, 208, 59, 4, 2, 1, 0, 255, 254, 246, 194, 71, 10, 2, 1, 0, 255, 252, 236, 183,
|
||||
82, 8, 2, 1, 0, 255, 252, 235, 180, 90, 17, 2, 1, 0, 255, 248, 224, 171, 97, 30,
|
||||
4, 1, 0, 255, 254, 236, 173, 95, 37, 7, 1, 0,
|
||||
}
|
||||
T.NLSF_PRED_NB_MB_Q8 = {
|
||||
179, 138, 140, 148, 151, 149, 153, 151, 163, 116, 67, 82, 59, 92, 72, 100, 89, 92,
|
||||
}
|
||||
T.NLSF_DELTA_MIN_NB_MB_Q15 = {
|
||||
250, 3, 6, 3, 3, 3, 4, 3, 3, 3, 461,
|
||||
}
|
||||
T.pitch_lag_iCDF = {
|
||||
253, 250, 244, 233, 212, 182, 150, 131, 120, 110, 98, 85, 72, 60, 49, 40, 32, 25, 19, 15,
|
||||
13, 11, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0,
|
||||
}
|
||||
T.pitch_delta_iCDF = {
|
||||
210, 208, 206, 203, 199, 193, 183, 168, 142, 104, 74, 52, 37, 27, 20, 14, 10, 6, 4, 2,
|
||||
0,
|
||||
}
|
||||
T.pitch_contour_iCDF = {
|
||||
223, 201, 183, 167, 152, 138, 124, 111, 98, 88, 79, 70, 62, 56, 50, 44, 39, 35, 31, 27,
|
||||
24, 21, 18, 16, 14, 12, 10, 8, 6, 4, 3, 2, 1, 0,
|
||||
}
|
||||
T.pitch_contour_NB_iCDF = {
|
||||
188, 176, 155, 138, 119, 97, 67, 43, 26, 10, 0,
|
||||
}
|
||||
T.pitch_contour_10_ms_iCDF = {
|
||||
165, 119, 80, 61, 47, 35, 27, 20, 14, 9, 4, 0,
|
||||
}
|
||||
T.pitch_contour_10_ms_NB_iCDF = {
|
||||
113, 63, 0,
|
||||
}
|
||||
T.stereo_pred_quant_Q13 = {
|
||||
-13732, -10050, -8266, -7526, -6500, -5000, -2950, -820, 820, 2950, 5000, 6500, 7526, 8266, 10050, 13732,
|
||||
}
|
||||
T.stereo_pred_joint_iCDF = {
|
||||
249, 247, 246, 245, 244, 234, 210, 202, 201, 200, 197, 174, 82, 59, 56, 55, 54, 46, 22, 12,
|
||||
11, 10, 9, 7, 0,
|
||||
}
|
||||
T.stereo_only_code_mid_iCDF = {
|
||||
64, 0,
|
||||
}
|
||||
T.LBRR_flags_2_iCDF = {
|
||||
203, 150, 0,
|
||||
}
|
||||
T.LBRR_flags_3_iCDF = {
|
||||
215, 195, 166, 125, 110, 82, 0,
|
||||
}
|
||||
T.lsb_iCDF = {
|
||||
120, 0,
|
||||
}
|
||||
T.LTPscale_iCDF = {
|
||||
128, 64, 0,
|
||||
}
|
||||
T.type_offset_VAD_iCDF = {
|
||||
232, 158, 10, 0,
|
||||
}
|
||||
T.type_offset_no_VAD_iCDF = {
|
||||
230, 0,
|
||||
}
|
||||
T.NLSF_interpolation_factor_iCDF = {
|
||||
243, 221, 192, 181, 0,
|
||||
}
|
||||
T.LTPScales_table_Q14 = {
|
||||
15565, 12288, 8192,
|
||||
}
|
||||
T.uniform3_iCDF = {
|
||||
171, 85, 0,
|
||||
}
|
||||
T.uniform4_iCDF = {
|
||||
192, 128, 64, 0,
|
||||
}
|
||||
T.uniform5_iCDF = {
|
||||
205, 154, 102, 51, 0,
|
||||
}
|
||||
T.uniform6_iCDF = {
|
||||
213, 171, 128, 85, 43, 0,
|
||||
}
|
||||
T.uniform8_iCDF = {
|
||||
224, 192, 160, 128, 96, 64, 32, 0,
|
||||
}
|
||||
T.NLSF_EXT_iCDF = {
|
||||
100, 40, 16, 7, 3, 1, 0,
|
||||
}
|
||||
T.gain_iCDF = {
|
||||
224, 112, 44, 15, 3, 2, 1, 0, 254, 237, 192, 132, 70, 23, 4, 0, 255, 252, 226, 155,
|
||||
61, 11, 2, 0,
|
||||
}
|
||||
T.delta_gain_iCDF = {
|
||||
250, 245, 234, 203, 71, 50, 42, 38, 35, 33, 31, 29, 28, 27, 26, 25, 24, 23, 22, 21,
|
||||
20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1,
|
||||
0,
|
||||
}
|
||||
T.LSFCosTab_FIX_Q12 = {
|
||||
8192, 8190, 8182, 8170, 8152, 8130, 8104, 8072, 8034, 7994, 7946, 7896, 7840, 7778, 7714, 7644, 7568, 7490, 7406, 7318,
|
||||
7226, 7128, 7026, 6922, 6812, 6698, 6580, 6458, 6332, 6204, 6070, 5934, 5792, 5648, 5502, 5352, 5198, 5040, 4880, 4718,
|
||||
4552, 4382, 4212, 4038, 3862, 3684, 3502, 3320, 3136, 2948, 2760, 2570, 2378, 2186, 1990, 1794, 1598, 1400, 1202, 1002,
|
||||
802, 602, 402, 202, 0, -202, -402, -602, -802, -1002, -1202, -1400, -1598, -1794, -1990, -2186, -2378, -2570, -2760, -2948,
|
||||
-3136, -3320, -3502, -3684, -3862, -4038, -4212, -4382, -4552, -4718, -4880, -5040, -5198, -5352, -5502, -5648, -5792, -5934, -6070, -6204,
|
||||
-6332, -6458, -6580, -6698, -6812, -6922, -7026, -7128, -7226, -7318, -7406, -7490, -7568, -7644, -7714, -7778, -7840, -7896, -7946, -7994,
|
||||
-8034, -8072, -8104, -8130, -8152, -8170, -8182, -8190, -8192,
|
||||
}
|
||||
T.pulses_per_block_iCDF = {
|
||||
125, 51, 26, 18, 15, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 198, 105,
|
||||
45, 22, 15, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 213, 162, 116, 83,
|
||||
59, 43, 32, 24, 18, 15, 12, 9, 7, 6, 5, 3, 2, 0, 239, 187, 116, 59, 28, 16,
|
||||
11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 250, 229, 188, 135, 86, 51, 30, 19,
|
||||
13, 10, 8, 6, 5, 4, 3, 2, 1, 0, 249, 235, 213, 185, 156, 128, 103, 83, 66, 53,
|
||||
42, 33, 26, 21, 17, 13, 10, 0, 254, 249, 235, 206, 164, 118, 77, 46, 27, 16, 10, 7,
|
||||
5, 4, 3, 2, 1, 0, 255, 253, 249, 239, 220, 191, 156, 119, 85, 57, 37, 23, 15, 10,
|
||||
6, 4, 2, 0, 255, 253, 251, 246, 237, 223, 203, 179, 152, 124, 98, 75, 55, 40, 29, 21,
|
||||
15, 0, 255, 254, 253, 247, 220, 162, 106, 67, 42, 28, 18, 12, 9, 6, 4, 3, 2, 0,
|
||||
}
|
||||
T.rate_levels_iCDF = {
|
||||
241, 190, 178, 132, 87, 74, 41, 14, 0, 223, 193, 157, 140, 106, 57, 39, 18, 0,
|
||||
}
|
||||
T.shell_code_table0 = {
|
||||
128, 0, 214, 42, 0, 235, 128, 21, 0, 244, 184, 72, 11, 0, 248, 214, 128, 42, 7, 0,
|
||||
248, 225, 170, 80, 25, 5, 0, 251, 236, 198, 126, 54, 18, 3, 0, 250, 238, 211, 159, 82,
|
||||
35, 15, 5, 0, 250, 231, 203, 168, 128, 88, 53, 25, 6, 0, 252, 238, 216, 185, 148, 108,
|
||||
71, 40, 18, 4, 0, 253, 243, 225, 199, 166, 128, 90, 57, 31, 13, 3, 0, 254, 246, 233,
|
||||
212, 183, 147, 109, 73, 44, 23, 10, 2, 0, 255, 250, 240, 223, 198, 166, 128, 90, 58, 33,
|
||||
16, 6, 1, 0, 255, 251, 244, 231, 210, 181, 146, 110, 75, 46, 25, 12, 5, 1, 0, 255,
|
||||
253, 248, 238, 221, 196, 164, 128, 92, 60, 35, 18, 8, 3, 1, 0, 255, 253, 249, 242, 229,
|
||||
208, 180, 146, 110, 76, 48, 27, 14, 7, 3, 1, 0,
|
||||
}
|
||||
T.shell_code_table1 = {
|
||||
129, 0, 207, 50, 0, 236, 129, 20, 0, 245, 185, 72, 10, 0, 249, 213, 129, 42, 6, 0,
|
||||
250, 226, 169, 87, 27, 4, 0, 251, 233, 194, 130, 62, 20, 4, 0, 250, 236, 207, 160, 99,
|
||||
47, 17, 3, 0, 255, 240, 217, 182, 131, 81, 41, 11, 1, 0, 255, 254, 233, 201, 159, 107,
|
||||
61, 20, 2, 1, 0, 255, 249, 233, 206, 170, 128, 86, 50, 23, 7, 1, 0, 255, 250, 238,
|
||||
217, 186, 148, 108, 70, 39, 18, 6, 1, 0, 255, 252, 243, 226, 200, 166, 128, 90, 56, 30,
|
||||
13, 4, 1, 0, 255, 252, 245, 231, 209, 180, 146, 110, 76, 47, 25, 11, 4, 1, 0, 255,
|
||||
253, 248, 237, 219, 194, 163, 128, 93, 62, 37, 19, 8, 3, 1, 0, 255, 254, 250, 241, 226,
|
||||
205, 177, 145, 111, 79, 51, 30, 15, 6, 2, 1, 0,
|
||||
}
|
||||
T.shell_code_table2 = {
|
||||
129, 0, 203, 54, 0, 234, 129, 23, 0, 245, 184, 73, 10, 0, 250, 215, 129, 41, 5, 0,
|
||||
252, 232, 173, 86, 24, 3, 0, 253, 240, 200, 129, 56, 15, 2, 0, 253, 244, 217, 164, 94,
|
||||
38, 10, 1, 0, 253, 245, 226, 189, 132, 71, 27, 7, 1, 0, 253, 246, 231, 203, 159, 105,
|
||||
56, 23, 6, 1, 0, 255, 248, 235, 213, 179, 133, 85, 47, 19, 5, 1, 0, 255, 254, 243,
|
||||
221, 194, 159, 117, 70, 37, 12, 2, 1, 0, 255, 254, 248, 234, 208, 171, 128, 85, 48, 22,
|
||||
8, 2, 1, 0, 255, 254, 250, 240, 220, 189, 149, 107, 67, 36, 16, 6, 2, 1, 0, 255,
|
||||
254, 251, 243, 227, 201, 166, 128, 90, 55, 29, 13, 5, 2, 1, 0, 255, 254, 252, 246, 234,
|
||||
213, 183, 147, 109, 73, 43, 22, 10, 4, 2, 1, 0,
|
||||
}
|
||||
T.shell_code_table3 = {
|
||||
130, 0, 200, 58, 0, 231, 130, 26, 0, 244, 184, 76, 12, 0, 249, 214, 130, 43, 6, 0,
|
||||
252, 232, 173, 87, 24, 3, 0, 253, 241, 203, 131, 56, 14, 2, 0, 254, 246, 221, 167, 94,
|
||||
35, 8, 1, 0, 254, 249, 232, 193, 130, 65, 23, 5, 1, 0, 255, 251, 239, 211, 162, 99,
|
||||
45, 15, 4, 1, 0, 255, 251, 243, 223, 186, 131, 74, 33, 11, 3, 1, 0, 255, 252, 245,
|
||||
230, 202, 158, 105, 57, 24, 8, 2, 1, 0, 255, 253, 247, 235, 214, 179, 132, 84, 44, 19,
|
||||
7, 2, 1, 0, 255, 254, 250, 240, 223, 196, 159, 112, 69, 36, 15, 6, 2, 1, 0, 255,
|
||||
254, 253, 245, 231, 209, 176, 136, 93, 55, 27, 11, 3, 2, 1, 0, 255, 254, 253, 252, 239,
|
||||
221, 194, 158, 117, 76, 42, 18, 4, 3, 2, 1, 0,
|
||||
}
|
||||
T.shell_code_table_offsets = {
|
||||
0, 0, 2, 5, 9, 14, 20, 27, 35, 44, 54, 65, 77, 90, 104, 119, 135,
|
||||
}
|
||||
T.sign_iCDF = {
|
||||
254, 49, 67, 77, 82, 93, 99, 198, 11, 18, 24, 31, 36, 45, 255, 46, 66, 78, 87, 94,
|
||||
104, 208, 14, 21, 32, 42, 51, 66, 255, 94, 104, 109, 112, 115, 118, 248, 53, 69, 80, 88,
|
||||
95, 102,
|
||||
}
|
||||
T.CB_lags_stage2_10_ms = {
|
||||
0, 1, 0, 0, 0, 1,
|
||||
}
|
||||
T.CB_lags_stage3_10_ms = {
|
||||
0, 0, 1, -1, 1, -1, 2, -2, 2, -2, 3, -3, 0, 1, 0, 1, -1, 2, -1, 2,
|
||||
-2, 3, -2, 3,
|
||||
}
|
||||
T.CB_lags_stage2 = {
|
||||
0, 2, -1, -1, -1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0,
|
||||
0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, -1, 2, 1, 0, 1, 1,
|
||||
0, 0, -1, -1,
|
||||
}
|
||||
T.CB_lags_stage3 = {
|
||||
0, 0, 1, -1, 0, 1, -1, 0, -1, 1, -2, 2, -2, -2, 2, -3, 2, 3, -3, -4,
|
||||
3, -4, 4, 4, -5, 5, -6, -5, 6, -7, 6, 5, 8, -9, 0, 0, 1, 0, 0, 0,
|
||||
0, 0, 0, 0, -1, 1, 0, 0, 1, -1, 0, 1, -1, -1, 1, -1, 2, 1, -1, 2,
|
||||
-2, -2, 2, -2, 2, 2, 3, -3, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0,
|
||||
0, 1, -1, 1, 0, 0, 2, 1, -1, 2, -1, -1, 2, -1, 2, 2, -1, 3, -2, -2,
|
||||
-2, 3, 0, 1, 0, 0, 1, 0, 1, -1, 2, -1, 2, -1, 2, 3, -2, 3, -2, -2,
|
||||
4, 4, -3, 5, -3, -4, 6, -4, 6, 5, -5, 8, -6, -5, -7, 9,
|
||||
}
|
||||
T.resampler_frac_FIR_12 = {
|
||||
189, -600, 617, 30567, 117, -159, -1070, 29704, 52, 221, -2392, 28276, -4, 529, -3350, 26341, -48, 758, -3956, 23973,
|
||||
-80, 905, -4235, 21254, -99, 972, -4222, 18278, -107, 967, -3957, 15143, -103, 896, -3487, 11950, -91, 773, -2865, 8798,
|
||||
-71, 611, -2143, 5784, -46, 425, -1375, 2996,
|
||||
}
|
||||
return T
|
||||
@@ -0,0 +1,15 @@
|
||||
--!strict
|
||||
-- codecs/audio/Opus: the Opus codec (RFC 6716) without its container: TOC / packet framing (Packet), the SILK and CELT
|
||||
-- decoders and the multistream / hybrid decoder that drives them (Decoder), the CELT encoder (CeltEncoder) and the
|
||||
-- CELT inverse MDCT (Mdct, a fused libopus port: see its header for why it is not codecs/audio/Mdct). A port of the
|
||||
-- libopus 1.6 decoder (BSD: licenses/libopus-BSD.txt), absorbed from opus-luau dd2b4e5; the encoder is new. Ogg Opus
|
||||
-- (OpusHead, OpusTags, granules, seeking) is audio/opus.
|
||||
local Opus = {
|
||||
Packet = require("@formats/codecs/audio/Opus/Packet"),
|
||||
Decoder = require("@formats/codecs/audio/Opus/Decoder"),
|
||||
Celt = require("@formats/codecs/audio/Opus/Celt"),
|
||||
Silk = require("@formats/codecs/audio/Opus/Silk"),
|
||||
CeltEncoder = require("@formats/codecs/audio/Opus/CeltEncoder"),
|
||||
}
|
||||
|
||||
return Opus
|
||||
@@ -0,0 +1,394 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- codecs/audio/Vorbis/Codebook: the Vorbis LSB-first packet bit reader and codebooks (Vorbis I spec sections 2 and 3):
|
||||
-- reading a codebook from the setup header (ordered / sparse length lists, lookup types 1 and 2, float32_unpack),
|
||||
-- the codeword assignment, and Huffman / VQ decoding. Ported from stb_vorbis 1.22 (public domain / MIT; see
|
||||
-- licenses/stb_vorbis-LICENSE.txt): compute_codewords' lowest-free-leaf assignment, lookup1_values; the decode side
|
||||
-- is a fast table (up to 10 bits) plus a binary tree for longer codes instead of stb's sorted binary search.
|
||||
--
|
||||
-- Never throws. Reading past the packet end gives zero bits and sets br.eop (the spec's end-of-packet condition);
|
||||
-- an invalid codeword also sets eop and decodes to -1.
|
||||
local Codebook = {}
|
||||
|
||||
local readu8 = buffer.readu8
|
||||
local floor = math.floor
|
||||
|
||||
local POW = table.create(41, 0)
|
||||
for i = 0, 40 do
|
||||
POW[i + 1] = 2 ^ i
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ bit reader
|
||||
export type Br = {
|
||||
buf: buffer,
|
||||
pos: number, -- next byte to load
|
||||
limit: number,
|
||||
acc: number, -- loaded, unread bits (LSB first)
|
||||
cnt: number,
|
||||
pad: number, -- how many of the top bits of acc are past the end of the packet
|
||||
eop: boolean, -- read past the end of the packet
|
||||
}
|
||||
|
||||
function Codebook.reader(buf: buffer, offset: number, size: number): Br
|
||||
return { buf = buf, pos = offset, limit = offset + size, acc = 0, cnt = 0, pad = 0, eop = false }
|
||||
end
|
||||
|
||||
-- bits(br, n) -> the next n (0..32) bits as an unsigned number
|
||||
local function bits(br: Br, n: number): number
|
||||
if n == 0 then
|
||||
return 0
|
||||
end
|
||||
local acc, cnt = br.acc, br.cnt
|
||||
if cnt < n then
|
||||
local b, p, lim = br.buf, br.pos, br.limit
|
||||
repeat
|
||||
if p < lim then
|
||||
acc += readu8(b, p) * POW[cnt + 1]
|
||||
p += 1
|
||||
else
|
||||
br.pad += 8
|
||||
end
|
||||
cnt += 8
|
||||
until cnt >= n
|
||||
br.pos = p
|
||||
end
|
||||
local m = POW[n + 1]
|
||||
local v = acc % m
|
||||
br.acc = (acc - v) / m
|
||||
cnt -= n
|
||||
br.cnt = cnt
|
||||
if cnt < br.pad then
|
||||
br.eop = true
|
||||
br.pad = cnt
|
||||
end
|
||||
return v
|
||||
end
|
||||
Codebook.bits = bits
|
||||
|
||||
-- ilog(x): the number of bits needed to hold x (0 for x <= 0)
|
||||
local function ilog(x: number): number
|
||||
local n = 0
|
||||
while x > 0 do
|
||||
n += 1
|
||||
x = floor(x / 2)
|
||||
end
|
||||
return n
|
||||
end
|
||||
Codebook.ilog = ilog
|
||||
|
||||
-- float32_unpack (spec 9.2.2)
|
||||
local function float32Unpack(x: number): number
|
||||
local mantissa = x % 2097152
|
||||
local sign = x >= 2147483648
|
||||
local exp = floor((x % 2147483648) / 2097152)
|
||||
local v = mantissa * 2 ^ (exp - 788)
|
||||
return if sign then -v else v
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ codebooks
|
||||
export type Book = {
|
||||
dims: number,
|
||||
entries: number,
|
||||
fastBits: number,
|
||||
fastLen: { number }, -- per fastBits-bit prefix: code length (> 0), 0 = continue in the tree, -1 = invalid
|
||||
fastSym: { number }, -- symbol, or the tree node to continue from
|
||||
c0: { number }, -- tree: child for bit 0 / 1: > 0 node, < 0 leaf (-symbol - 1), 0 none
|
||||
c1: { number },
|
||||
lookup: number, -- 0, 1, 2
|
||||
vq: { number }?, -- entries * dims values (lookup 1 / 2, expanded)
|
||||
used: number, -- entries with a codeword
|
||||
}
|
||||
|
||||
local MAX_ENTRIES = 2 ^ 20
|
||||
local MAX_VQ = 2 ^ 22
|
||||
|
||||
-- lookup1_values(entries, dims): the largest r with r^dims <= entries
|
||||
local function lookup1Values(entries: number, dims: number): number
|
||||
local r = floor(math.exp(math.log(entries) / dims))
|
||||
if floor((r + 1) ^ dims) <= entries then
|
||||
r += 1
|
||||
end
|
||||
if (r + 1) ^ dims <= entries then
|
||||
return -1
|
||||
end
|
||||
if floor(r ^ dims) > entries then
|
||||
return -1
|
||||
end
|
||||
return r
|
||||
end
|
||||
|
||||
-- assign codewords (spec 3.2.1: in entry order, the lowest free leaf) and build the decode tree + fast table
|
||||
local function build(book: Book, lengths: { number }): string?
|
||||
local n = book.entries
|
||||
local c0, c1 = book.c0, book.c1
|
||||
local nodes = 1
|
||||
c0[1], c1[1] = 0, 0
|
||||
local available = table.create(33, 0) -- available[l]: the free code at depth l, as a 32-bit left-aligned value + 1 (0 = none)
|
||||
local first = true
|
||||
local used = 0
|
||||
local maxLen = 0
|
||||
for i = 0, n - 1 do
|
||||
local len = lengths[i + 1]
|
||||
if len > 0 then
|
||||
local code: number
|
||||
if first then
|
||||
first = false
|
||||
code = 0
|
||||
for l = 1, len do
|
||||
available[l + 1] = POW[33 - l] + 1
|
||||
end
|
||||
else
|
||||
local z = len
|
||||
while z > 0 and available[z + 1] == 0 do
|
||||
z -= 1
|
||||
end
|
||||
if z == 0 then
|
||||
return "over-subscribed codebook"
|
||||
end
|
||||
code = available[z + 1] - 1
|
||||
available[z + 1] = 0
|
||||
for y = len, z + 1, -1 do
|
||||
available[y + 1] = code + POW[33 - y] + 1
|
||||
end
|
||||
end
|
||||
-- walk the MSB-first code into the tree
|
||||
local node = 1
|
||||
for d = 1, len do
|
||||
local bit = floor(code / POW[33 - d]) % 2
|
||||
local child = if bit == 0 then c0[node] else c1[node]
|
||||
if d == len then
|
||||
if child ~= 0 then
|
||||
return "ambiguous codeword"
|
||||
end
|
||||
if bit == 0 then
|
||||
c0[node] = -(i + 1)
|
||||
else
|
||||
c1[node] = -(i + 1)
|
||||
end
|
||||
else
|
||||
if child < 0 then
|
||||
return "ambiguous codeword"
|
||||
elseif child == 0 then
|
||||
nodes += 1
|
||||
c0[nodes], c1[nodes] = 0, 0
|
||||
child = nodes
|
||||
if bit == 0 then
|
||||
c0[node] = child
|
||||
else
|
||||
c1[node] = child
|
||||
end
|
||||
end
|
||||
node = child
|
||||
end
|
||||
end
|
||||
used += 1
|
||||
if len > maxLen then
|
||||
maxLen = len
|
||||
end
|
||||
end
|
||||
end
|
||||
book.used = used
|
||||
-- the fast table: every fastBits-bit prefix (read LSB first) walked down the tree
|
||||
local fb = math.clamp(maxLen, 1, 10)
|
||||
book.fastBits = fb
|
||||
local size = POW[fb + 1]
|
||||
local fl, fs = table.create(size, -1), table.create(size, 0)
|
||||
for idx = 0, size - 1 do
|
||||
local node = 1
|
||||
local v = idx
|
||||
for d = 1, fb do
|
||||
local bit = v % 2
|
||||
v = (v - bit) / 2
|
||||
local child = if bit == 0 then c0[node] else c1[node]
|
||||
if child < 0 then
|
||||
fl[idx + 1], fs[idx + 1] = d, -child - 1
|
||||
break
|
||||
elseif child == 0 then
|
||||
break -- invalid (-1)
|
||||
end
|
||||
node = child
|
||||
if d == fb then
|
||||
fl[idx + 1], fs[idx + 1] = 0, node
|
||||
end
|
||||
end
|
||||
end
|
||||
book.fastLen, book.fastSym = fl, fs
|
||||
return nil
|
||||
end
|
||||
|
||||
-- read(br) -> Book | nil, message: one codebook from the setup header
|
||||
function Codebook.read(br: Br): (Book?, string?)
|
||||
if bits(br, 24) ~= 0x564342 then
|
||||
return nil, "bad codebook sync"
|
||||
end
|
||||
local dims = bits(br, 16)
|
||||
local entries = bits(br, 24)
|
||||
local ordered = bits(br, 1)
|
||||
if br.eop then
|
||||
return nil, "setup header ends in a codebook"
|
||||
end
|
||||
if dims == 0 and entries ~= 0 then
|
||||
return nil, "codebook with 0 dimensions"
|
||||
end
|
||||
if entries > MAX_ENTRIES then
|
||||
return nil, "codebook with " .. entries .. " entries"
|
||||
end
|
||||
local lengths = table.create(entries, 0)
|
||||
if ordered == 1 then
|
||||
local cur = 0
|
||||
local len = bits(br, 5) + 1
|
||||
while cur < entries do
|
||||
local num = bits(br, ilog(entries - cur))
|
||||
if len >= 32 or cur + num > entries or br.eop then
|
||||
return nil, "bad ordered codebook lengths"
|
||||
end
|
||||
for k = cur + 1, cur + num do
|
||||
lengths[k] = len
|
||||
end
|
||||
cur += num
|
||||
len += 1
|
||||
end
|
||||
else
|
||||
local sparse = bits(br, 1)
|
||||
for j = 1, entries do
|
||||
if sparse == 0 or bits(br, 1) == 1 then
|
||||
local l = bits(br, 5) + 1
|
||||
if l == 32 then
|
||||
return nil, "codeword length 32"
|
||||
end
|
||||
lengths[j] = l
|
||||
end
|
||||
end
|
||||
end
|
||||
if br.eop then
|
||||
return nil, "setup header ends in a codebook"
|
||||
end
|
||||
local book: Book = {
|
||||
dims = dims,
|
||||
entries = entries,
|
||||
fastBits = 1,
|
||||
fastLen = {},
|
||||
fastSym = {},
|
||||
c0 = {},
|
||||
c1 = {},
|
||||
lookup = 0,
|
||||
vq = nil,
|
||||
used = 0,
|
||||
}
|
||||
local err = build(book, lengths)
|
||||
if err then
|
||||
return nil, err
|
||||
end
|
||||
local lookup = bits(br, 4)
|
||||
if lookup > 2 then
|
||||
return nil, "lookup type " .. lookup
|
||||
end
|
||||
book.lookup = lookup
|
||||
if lookup > 0 then
|
||||
local minimum = float32Unpack(bits(br, 32))
|
||||
local delta = float32Unpack(bits(br, 32))
|
||||
local valueBits = bits(br, 4) + 1
|
||||
local seq = bits(br, 1) == 1
|
||||
local nvalues
|
||||
if lookup == 1 then
|
||||
nvalues = lookup1Values(entries, dims)
|
||||
if nvalues < 0 then
|
||||
return nil, "bad lookup1 size"
|
||||
end
|
||||
else
|
||||
nvalues = entries * dims
|
||||
end
|
||||
if nvalues == 0 then
|
||||
return nil, "empty lookup table"
|
||||
end
|
||||
if entries * dims > MAX_VQ or nvalues > MAX_VQ then
|
||||
return nil, "lookup table too large"
|
||||
end
|
||||
local mults = table.create(nvalues, 0)
|
||||
for j = 1, nvalues do
|
||||
mults[j] = bits(br, valueBits)
|
||||
end
|
||||
if br.eop then
|
||||
return nil, "setup header ends in a lookup table"
|
||||
end
|
||||
-- expand to one vector per entry (spec 3.2.1, "VQ lookup table vector representation")
|
||||
local vq = table.create(entries * dims, 0)
|
||||
for e = 0, entries - 1 do
|
||||
local last = 0
|
||||
local div = 1
|
||||
for k = 0, dims - 1 do
|
||||
local off
|
||||
if lookup == 1 then
|
||||
off = floor(e / div) % nvalues
|
||||
div *= nvalues
|
||||
else
|
||||
off = e * dims + k
|
||||
end
|
||||
local v = mults[off + 1] * delta + minimum + last
|
||||
if seq then
|
||||
last = v
|
||||
end
|
||||
vq[e * dims + k + 1] = v
|
||||
end
|
||||
end
|
||||
book.vq = vq
|
||||
end
|
||||
return book, nil
|
||||
end
|
||||
|
||||
-- decode(br, book) -> entry number | -1 (end of packet / invalid codeword: br.eop is set)
|
||||
local function decode(br: Br, book: Book): number
|
||||
local fb = book.fastBits
|
||||
local acc, cnt = br.acc, br.cnt
|
||||
if cnt < fb then
|
||||
local b, p, lim = br.buf, br.pos, br.limit
|
||||
repeat
|
||||
if p < lim then
|
||||
acc += readu8(b, p) * POW[cnt + 1]
|
||||
p += 1
|
||||
else
|
||||
br.pad += 8
|
||||
end
|
||||
cnt += 8
|
||||
until cnt >= fb
|
||||
br.pos = p
|
||||
br.acc, br.cnt = acc, cnt
|
||||
end
|
||||
local idx = acc % POW[fb + 1] + 1
|
||||
local l = book.fastLen[idx]
|
||||
if l > 0 then
|
||||
local m = POW[l + 1]
|
||||
br.acc = (acc - acc % m) / m
|
||||
cnt -= l
|
||||
br.cnt = cnt
|
||||
if cnt < br.pad then
|
||||
br.eop = true
|
||||
br.pad = cnt
|
||||
return -1
|
||||
end
|
||||
return book.fastSym[idx]
|
||||
elseif l == 0 then
|
||||
bits(br, fb)
|
||||
local node = book.fastSym[idx]
|
||||
local c0, c1 = book.c0, book.c1
|
||||
while true do
|
||||
local child = if bits(br, 1) == 0 then c0[node] else c1[node]
|
||||
if br.eop then
|
||||
return -1
|
||||
end
|
||||
if child < 0 then
|
||||
return -child - 1
|
||||
elseif child == 0 then
|
||||
br.eop = true
|
||||
return -1
|
||||
end
|
||||
node = child
|
||||
end
|
||||
end
|
||||
br.eop = true
|
||||
return -1
|
||||
end
|
||||
Codebook.decode = decode
|
||||
|
||||
return Codebook
|
||||
@@ -0,0 +1,586 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- codecs/audio/Vorbis/Packet: Vorbis audio packet decoding (Vorbis I spec sections 4.3, 7, 8): mode and window shape, floor 1
|
||||
-- curves, residue types 0 / 1 / 2, inverse channel coupling, the IMDCT (codecs/audio/Mdct), windowing and overlap-add.
|
||||
-- Floors, residues and coupling are ported from stb_vorbis 1.22 vorbis_decode_packet_rest / decode_residue /
|
||||
-- do_floor / draw_line (public domain / MIT; see licenses/stb_vorbis-LICENSE.txt). The overlap follows libvorbis:
|
||||
-- each packet after the first returns the n_prev/4 + n_cur/4 samples between the centres of the two windows, so
|
||||
-- granule positions count exactly the samples returned (the first packet returns none).
|
||||
--
|
||||
-- local st = Packet.new(ident, setup)
|
||||
-- local n = Packet.decode(st, buf, offset, size) -- frames now in st.out[ch][1 .. n]; -1 = not an audio packet
|
||||
-- Packet.reset(st) -- forget the overlap (after a seek / a gap)
|
||||
-- local bs = Packet.blocksize(st, buf, offset, size) -- the packet's block size (0 = not audio), header bits only
|
||||
-- Never throws: a damaged packet decodes as far as its bits go (the spec's end-of-packet rule) or returns -1.
|
||||
local Codebook = require("@formats/codecs/audio/Vorbis/Codebook")
|
||||
local Setup = require("@formats/codecs/audio/Vorbis/Setup")
|
||||
local Tables = require("@formats/codecs/audio/Vorbis/Tables")
|
||||
local Mdct = require("@formats/codecs/audio/Mdct")
|
||||
|
||||
local Packet = {}
|
||||
|
||||
local bits, decode = Codebook.bits, Codebook.decode
|
||||
local ilog = Codebook.ilog
|
||||
local INV_DB = Tables
|
||||
local RANGE = { 256, 128, 86, 64 }
|
||||
|
||||
export type State = {
|
||||
channels: number,
|
||||
n0: number,
|
||||
n1: number,
|
||||
setup: Setup.Setup,
|
||||
plans: { [number]: Mdct.Plan },
|
||||
slopes: { [number]: { number } }, -- rising half-window of overlap length L (= blocksize / 2)
|
||||
spec: { { number } }, -- per channel: n/2 spectral values (residue, then times the floor)
|
||||
frame: { { number } }, -- per channel: n windowed time samples
|
||||
tail: { { number } }, -- per channel: the previous frame from its centre on
|
||||
tailLen: number,
|
||||
prevN: number, -- the previous block size, 0 = no previous frame
|
||||
out: { { number } }, -- per channel: the returned samples
|
||||
finalY: { { number } },
|
||||
step2: { number },
|
||||
inter: { number }, -- residue type 2 interleaved scratch
|
||||
zero: { boolean },
|
||||
reallyZero: { boolean },
|
||||
partClass: { { { number } } },
|
||||
}
|
||||
|
||||
function Packet.new(id: Setup.Ident, setup: Setup.Setup): State?
|
||||
local n0, n1, ch = id.blocksize0, id.blocksize1, id.channels
|
||||
local plans: { [number]: Mdct.Plan } = {}
|
||||
local slopes: { [number]: { number } } = {}
|
||||
for _, n in { n0, n1 } do
|
||||
local p = Mdct.new(n)
|
||||
if not p then
|
||||
return nil
|
||||
end
|
||||
plans[n] = p
|
||||
local w = Mdct.window("vorbis", n) :: { number }
|
||||
local half = table.create(n // 2, 0)
|
||||
for i = 1, n // 2 do
|
||||
half[i] = w[i]
|
||||
end
|
||||
slopes[n // 2] = half
|
||||
end
|
||||
local function arrays(len: number): { { number } }
|
||||
local t = table.create(ch)
|
||||
for c = 1, ch do
|
||||
t[c] = table.create(len, 0)
|
||||
end
|
||||
return t
|
||||
end
|
||||
local partClass = table.create(ch)
|
||||
for c = 1, ch do
|
||||
partClass[c] = {}
|
||||
end
|
||||
return {
|
||||
channels = ch,
|
||||
n0 = n0,
|
||||
n1 = n1,
|
||||
setup = setup,
|
||||
plans = plans,
|
||||
slopes = slopes,
|
||||
spec = arrays(n1 // 2),
|
||||
frame = arrays(n1),
|
||||
tail = arrays(n1 // 2),
|
||||
tailLen = 0,
|
||||
prevN = 0,
|
||||
out = arrays(n1),
|
||||
finalY = arrays(math.max(2, setup.maxFloorValues)),
|
||||
step2 = table.create(math.max(2, setup.maxFloorValues), 0),
|
||||
inter = table.create(n1 // 2 * ch, 0),
|
||||
zero = table.create(ch, false),
|
||||
reallyZero = table.create(ch, false),
|
||||
partClass = partClass,
|
||||
}
|
||||
end
|
||||
|
||||
function Packet.reset(st: State)
|
||||
st.prevN = 0
|
||||
st.tailLen = 0
|
||||
end
|
||||
|
||||
-- blocksize(st, buf, off, size) -> the block size of an audio packet (0 if not audio)
|
||||
function Packet.blocksize(st: State, b: buffer, off: number, size: number): number
|
||||
if size < 1 then
|
||||
return 0
|
||||
end
|
||||
local br = Codebook.reader(b, off, size)
|
||||
if bits(br, 1) ~= 0 then
|
||||
return 0
|
||||
end
|
||||
local m = bits(br, st.setup.modeBits)
|
||||
local mode = st.setup.modes[m + 1]
|
||||
if not mode or br.eop then
|
||||
return 0
|
||||
end
|
||||
return if mode.blockflag == 1 then st.n1 else st.n0
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ floor 1
|
||||
local function predict(x: number, x0: number, x1: number, y0: number, y1: number): number
|
||||
local dy = y1 - y0
|
||||
local adx = x1 - x0
|
||||
local off = ((if dy < 0 then -dy else dy) * (x - x0)) // adx
|
||||
return if dy < 0 then y0 - off else y0 + off
|
||||
end
|
||||
|
||||
-- multiply out[x0 .. x1) by the floor line from (x0, y0) to (x1, y1) (stb draw_line)
|
||||
local function drawLine(out: { number }, x0: number, y0: number, x1: number, y1: number, n: number)
|
||||
local dy = y1 - y0
|
||||
local adx = x1 - x0
|
||||
local ady = if dy < 0 then -dy else dy
|
||||
local base = if dy < 0 then -(ady // adx) else ady // adx
|
||||
local sy = if dy < 0 then base - 1 else base + 1
|
||||
ady -= (if base < 0 then -base else base) * adx
|
||||
if x1 > n then
|
||||
x1 = n
|
||||
end
|
||||
local x, y, err = x0, y0, 0
|
||||
if x < x1 then
|
||||
out[x + 1] *= INV_DB[y % 256 + 1]
|
||||
x += 1
|
||||
while x < x1 do
|
||||
err += ady
|
||||
if err >= adx then
|
||||
err -= adx
|
||||
y += sy
|
||||
else
|
||||
y += base
|
||||
end
|
||||
out[x + 1] *= INV_DB[y % 256 + 1]
|
||||
x += 1
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- decode the floor of one channel into finalY (-1 = unused point); false = this channel is silent
|
||||
local function floorDecode(st: State, br: Codebook.Br, g: Setup.Floor1, finalY: { number }): boolean
|
||||
if bits(br, 1) == 0 then
|
||||
return false
|
||||
end
|
||||
local books = st.setup.books
|
||||
local range = RANGE[g.multiplier]
|
||||
local yb = ilog(range) - 1
|
||||
finalY[1] = bits(br, yb)
|
||||
finalY[2] = bits(br, yb)
|
||||
local offset = 2
|
||||
for j = 1, g.partitions do
|
||||
local pclass = g.partitionClass[j] + 1
|
||||
local cdim = g.classDims[pclass]
|
||||
local cbits = g.classSubs[pclass]
|
||||
local csub = 2 ^ cbits
|
||||
local cval = 0
|
||||
if cbits > 0 then
|
||||
cval = decode(br, books[g.classMaster[pclass] + 1])
|
||||
if cval < 0 then
|
||||
return false
|
||||
end
|
||||
end
|
||||
local subs = g.subBooks[pclass]
|
||||
for _ = 1, cdim do
|
||||
local book = subs[cval % csub + 1]
|
||||
cval = cval // csub
|
||||
if book >= 0 then
|
||||
local v = decode(br, books[book + 1])
|
||||
if v < 0 then
|
||||
return false
|
||||
end
|
||||
finalY[offset + 1] = v
|
||||
else
|
||||
finalY[offset + 1] = 0
|
||||
end
|
||||
offset += 1
|
||||
end
|
||||
end
|
||||
if br.eop then
|
||||
return false
|
||||
end
|
||||
-- step 1: amplitude value synthesis
|
||||
local step2 = st.step2
|
||||
step2[1], step2[2] = 1, 1
|
||||
local xl, low, high = g.xlist, g.low, g.high
|
||||
for j = 2, g.values - 1 do
|
||||
local lo, hi = low[j + 1], high[j + 1]
|
||||
local pred = predict(xl[j + 1], xl[lo + 1], xl[hi + 1], finalY[lo + 1], finalY[hi + 1])
|
||||
local val = finalY[j + 1]
|
||||
local highroom = range - pred
|
||||
local lowroom = pred
|
||||
local room = if highroom < lowroom then highroom * 2 else lowroom * 2
|
||||
if val ~= 0 then
|
||||
step2[lo + 1], step2[hi + 1], step2[j + 1] = 1, 1, 1
|
||||
if val >= room then
|
||||
if highroom > lowroom then
|
||||
finalY[j + 1] = val - lowroom + pred
|
||||
else
|
||||
finalY[j + 1] = pred - val + highroom - 1
|
||||
end
|
||||
elseif val % 2 == 1 then
|
||||
finalY[j + 1] = pred - (val + 1) // 2
|
||||
else
|
||||
finalY[j + 1] = pred + val // 2
|
||||
end
|
||||
else
|
||||
step2[j + 1] = 0
|
||||
finalY[j + 1] = pred
|
||||
end
|
||||
end
|
||||
for j = 1, g.values do
|
||||
if step2[j] == 0 then
|
||||
finalY[j] = -1
|
||||
end
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
-- multiply target[1 .. n2] by the floor curve (stb do_floor)
|
||||
local function floorCurve(g: Setup.Floor1, finalY: { number }, target: { number }, n2: number)
|
||||
local mult = g.multiplier
|
||||
local lx, ly = 0, finalY[1] * mult
|
||||
local sorted, xl = g.sorted, g.xlist
|
||||
for q = 2, g.values do
|
||||
local j = sorted[q] + 1
|
||||
local fy = finalY[j]
|
||||
if fy >= 0 then
|
||||
local hy = fy * mult
|
||||
local hx = xl[j]
|
||||
if lx ~= hx then
|
||||
drawLine(target, lx, ly, hx, hy, n2)
|
||||
end
|
||||
lx, ly = hx, hy
|
||||
end
|
||||
end
|
||||
if lx < n2 then
|
||||
local v = INV_DB[ly % 256 + 1]
|
||||
for j = lx + 1, n2 do
|
||||
target[j] *= v
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ residue
|
||||
-- add the vectors of one partition to target at offset (0-based) .. offset + psize (bounded by limit)
|
||||
local function partition(br: Codebook.Br, book: Codebook.Book, target: { number }, offset: number, psize: number, rtype: number, limit: number): boolean
|
||||
local vq = book.vq
|
||||
if not vq then
|
||||
br.eop = true -- a scalar book in a vector context: invalid stream
|
||||
return false
|
||||
end
|
||||
local dims = book.dims
|
||||
if rtype == 0 then
|
||||
local step = psize // dims
|
||||
for k = 0, step - 1 do
|
||||
local e = decode(br, book)
|
||||
if e < 0 then
|
||||
return false
|
||||
end
|
||||
local base = e * dims
|
||||
for i = 0, dims - 1 do
|
||||
local at = offset + k + i * step
|
||||
if at < limit then
|
||||
target[at + 1] += vq[base + i + 1]
|
||||
end
|
||||
end
|
||||
end
|
||||
else
|
||||
local k = 0
|
||||
while k < psize do
|
||||
local e = decode(br, book)
|
||||
if e < 0 then
|
||||
return false
|
||||
end
|
||||
local base = e * dims
|
||||
local len = math.min(dims, psize - k)
|
||||
local at = offset + k
|
||||
for i = 0, len - 1 do
|
||||
if at + i < limit then
|
||||
target[at + i + 1] += vq[base + i + 1]
|
||||
end
|
||||
end
|
||||
k += dims
|
||||
end
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
-- decode residue r for the channels `targets` (nil = do not decode) of length n2 each
|
||||
local function residue(st: State, br: Codebook.Br, r: Setup.Residue, targets: { { number } | false }, ch: number, n2: number)
|
||||
local books = st.setup.books
|
||||
local rtype = r.type
|
||||
local cbook = books[r.classbook + 1]
|
||||
local classwords = cbook.dims
|
||||
for j = 1, ch do
|
||||
local t = targets[j]
|
||||
if t then
|
||||
for i = 1, n2 do
|
||||
(t :: { number })[i] = 0
|
||||
end
|
||||
end
|
||||
end
|
||||
local any = false
|
||||
for j = 1, ch do
|
||||
if targets[j] then
|
||||
any = true
|
||||
end
|
||||
end
|
||||
if not any then
|
||||
return
|
||||
end
|
||||
local psize = r.partSize
|
||||
local rb = r.books
|
||||
local classdata = r.classdata
|
||||
if rtype == 2 and ch > 1 then
|
||||
local total = n2 * ch
|
||||
local inter = st.inter
|
||||
for i = 1, total do
|
||||
inter[i] = 0
|
||||
end
|
||||
local lb = math.min(r.begin, total)
|
||||
local le = math.min(r.finish, total)
|
||||
local partRead = (le - lb) // psize
|
||||
local pc = st.partClass[1]
|
||||
local done = false
|
||||
for pass = 1, 8 do
|
||||
if done then
|
||||
break
|
||||
end
|
||||
local pcount, classSet = 0, 1
|
||||
while pcount < partRead do
|
||||
if pass == 1 then
|
||||
local q = decode(br, cbook)
|
||||
if q < 0 then
|
||||
done = true
|
||||
break
|
||||
end
|
||||
pc[classSet] = classdata[q + 1]
|
||||
end
|
||||
local cls = pc[classSet]
|
||||
local i = 0
|
||||
while i < classwords and pcount < partRead do
|
||||
local b = rb[cls[i + 1] + 1][pass]
|
||||
if b >= 0 then
|
||||
if not partition(br, books[b + 1], inter, r.begin + pcount * psize, psize, 1, total) then
|
||||
done = true
|
||||
break
|
||||
end
|
||||
end
|
||||
i += 1
|
||||
pcount += 1
|
||||
end
|
||||
if done then
|
||||
break
|
||||
end
|
||||
classSet += 1
|
||||
end
|
||||
end
|
||||
-- de-interleave into the channels that are decoded
|
||||
for j = 1, ch do
|
||||
local t = targets[j]
|
||||
if t then
|
||||
local tt = t :: { number }
|
||||
local v = j
|
||||
for i = 1, n2 do
|
||||
tt[i] = inter[v]
|
||||
v += ch
|
||||
end
|
||||
end
|
||||
end
|
||||
return
|
||||
end
|
||||
local lb = math.min(r.begin, n2)
|
||||
local le = math.min(r.finish, n2)
|
||||
local partRead = (le - lb) // psize
|
||||
local pcs = st.partClass
|
||||
for pass = 1, 8 do
|
||||
local pcount, classSet = 0, 1
|
||||
while pcount < partRead do
|
||||
if pass == 1 then
|
||||
for j = 1, ch do
|
||||
if targets[j] then
|
||||
local q = decode(br, cbook)
|
||||
if q < 0 then
|
||||
return
|
||||
end
|
||||
pcs[j][classSet] = classdata[q + 1]
|
||||
end
|
||||
end
|
||||
end
|
||||
local i = 0
|
||||
while i < classwords and pcount < partRead do
|
||||
for j = 1, ch do
|
||||
local t = targets[j]
|
||||
if t then
|
||||
local c = pcs[j][classSet][i + 1]
|
||||
local b = rb[c + 1][pass]
|
||||
if b >= 0 then
|
||||
if not partition(br, books[b + 1], t :: { number }, r.begin + pcount * psize, psize, rtype, n2) then
|
||||
return
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
i += 1
|
||||
pcount += 1
|
||||
end
|
||||
classSet += 1
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ packet
|
||||
-- decode(st, buf, off, size) -> frames returned in st.out (0 for the first packet), -1 for a non-audio / bad packet
|
||||
function Packet.decode(st: State, b: buffer, off: number, size: number): number
|
||||
if size < 1 then
|
||||
return -1
|
||||
end
|
||||
local setup = st.setup
|
||||
local br = Codebook.reader(b, off, size)
|
||||
if bits(br, 1) ~= 0 then
|
||||
return -1
|
||||
end
|
||||
local mode = setup.modes[bits(br, setup.modeBits) + 1]
|
||||
if not mode then
|
||||
return -1
|
||||
end
|
||||
local n, prevLong, nextLong
|
||||
if mode.blockflag == 1 then
|
||||
n = st.n1
|
||||
prevLong = bits(br, 1) == 1
|
||||
nextLong = bits(br, 1) == 1
|
||||
else
|
||||
n = st.n0
|
||||
prevLong, nextLong = false, false
|
||||
end
|
||||
if br.eop then
|
||||
return -1
|
||||
end
|
||||
local n2 = n // 2
|
||||
local n0 = st.n0
|
||||
local leftStart, leftEnd, rightStart, rightEnd
|
||||
if mode.blockflag == 1 and not prevLong then
|
||||
leftStart, leftEnd = (n - n0) // 4, (n + n0) // 4
|
||||
else
|
||||
leftStart, leftEnd = 0, n2
|
||||
end
|
||||
if mode.blockflag == 1 and not nextLong then
|
||||
rightStart, rightEnd = (n * 3 - n0) // 4, (n * 3 + n0) // 4
|
||||
else
|
||||
rightStart, rightEnd = n2, n
|
||||
end
|
||||
local map = setup.mappings[mode.mapping + 1]
|
||||
local ch = st.channels
|
||||
local spec, zero, reallyZero = st.spec, st.zero, st.reallyZero
|
||||
-- floors
|
||||
for c = 1, ch do
|
||||
local s = map.mux[c] + 1
|
||||
local g = setup.floors[map.submapFloor[s] + 1]
|
||||
local ok = floorDecode(st, br, g, st.finalY[c])
|
||||
zero[c] = not ok
|
||||
reallyZero[c] = not ok
|
||||
end
|
||||
-- channels coupled to a non-silent channel are decoded too
|
||||
for i = 1, map.couplingSteps do
|
||||
local m, a = map.magnitude[i] + 1, map.angle[i] + 1
|
||||
if not zero[m] or not zero[a] then
|
||||
zero[m], zero[a] = false, false
|
||||
end
|
||||
end
|
||||
-- residues, per submap
|
||||
for i = 1, map.submaps do
|
||||
local targets: { { number } | false } = {}
|
||||
local k = 0
|
||||
for c = 1, ch do
|
||||
if map.mux[c] == i - 1 then
|
||||
k += 1
|
||||
targets[k] = if zero[c] then false else spec[c]
|
||||
end
|
||||
end
|
||||
residue(st, br, setup.residues[map.submapResidue[i] + 1], targets, k, n2)
|
||||
end
|
||||
-- inverse coupling
|
||||
for i = map.couplingSteps, 1, -1 do
|
||||
local m, a = spec[map.magnitude[i] + 1], spec[map.angle[i] + 1]
|
||||
for j = 1, n2 do
|
||||
local mv, av = m[j], a[j]
|
||||
local m2, a2
|
||||
if mv > 0 then
|
||||
if av > 0 then
|
||||
m2, a2 = mv, mv - av
|
||||
else
|
||||
a2, m2 = mv, mv + av
|
||||
end
|
||||
else
|
||||
if av > 0 then
|
||||
m2, a2 = mv, mv + av
|
||||
else
|
||||
a2, m2 = mv, mv - av
|
||||
end
|
||||
end
|
||||
m[j], a[j] = m2, a2
|
||||
end
|
||||
end
|
||||
-- floor curves, IMDCT, windows
|
||||
local plan = st.plans[n]
|
||||
local lslope = st.slopes[leftEnd - leftStart]
|
||||
local rslope = st.slopes[rightEnd - rightStart]
|
||||
for c = 1, ch do
|
||||
local sp = spec[c]
|
||||
local fr = st.frame[c]
|
||||
if reallyZero[c] then
|
||||
for j = 1, n do
|
||||
fr[j] = 0
|
||||
end
|
||||
else
|
||||
local s = map.mux[c] + 1
|
||||
floorCurve(setup.floors[map.submapFloor[s] + 1], st.finalY[c], sp, n2)
|
||||
Mdct.inverse(plan, sp, 1, 1, fr, 1)
|
||||
for j = 1, leftStart do
|
||||
fr[j] = 0
|
||||
end
|
||||
local L = leftEnd - leftStart
|
||||
for j = 0, L - 1 do
|
||||
fr[leftStart + j + 1] *= lslope[j + 1]
|
||||
end
|
||||
local R = rightEnd - rightStart
|
||||
for j = 0, R - 1 do
|
||||
fr[rightStart + j + 1] *= rslope[R - j]
|
||||
end
|
||||
for j = rightEnd + 1, n do
|
||||
fr[j] = 0
|
||||
end
|
||||
end
|
||||
end
|
||||
-- overlap-add with the previous frame; return the samples between the two centres
|
||||
local prevN = st.prevN
|
||||
local count = 0
|
||||
if prevN > 0 then
|
||||
count = prevN // 4 + n // 4
|
||||
local o = prevN // 4 - n // 4 -- this frame starts o samples after the previous centre
|
||||
local tl = st.tailLen
|
||||
for c = 1, ch do
|
||||
local out, tail, fr = st.out[c], st.tail[c], st.frame[c]
|
||||
for j = 0, count - 1 do
|
||||
local v = if j < tl then tail[j + 1] else 0
|
||||
local fi = j - o
|
||||
if fi >= 0 then
|
||||
v += fr[fi + 1]
|
||||
end
|
||||
out[j + 1] = v
|
||||
end
|
||||
end
|
||||
end
|
||||
-- the new tail: this frame from its centre to the end of its window
|
||||
local tl = rightEnd - n2
|
||||
for c = 1, ch do
|
||||
local tail, fr = st.tail[c], st.frame[c]
|
||||
for j = 1, tl do
|
||||
tail[j] = fr[n2 + j]
|
||||
end
|
||||
end
|
||||
st.tailLen = tl
|
||||
st.prevN = n
|
||||
return count
|
||||
end
|
||||
|
||||
return Packet
|
||||
@@ -0,0 +1,429 @@
|
||||
--!strict
|
||||
--!native
|
||||
-- codecs/audio/Vorbis/Setup: the three Vorbis header packets (Vorbis I spec section 4.2): identification (channels, rate,
|
||||
-- bitrates, blocksizes), comment (vendor + user comments -> tags) and setup (codebooks, floors, residues, mappings,
|
||||
-- modes). Ported from stb_vorbis 1.22 start_decoder (public domain / MIT; see licenses/stb_vorbis-LICENSE.txt).
|
||||
-- Floor 0 is rejected as unsupported, as stb_vorbis does (no encoder has produced it since 2000).
|
||||
-- Never throws: every parser returns nil, message on bad data.
|
||||
local Codebook = require("@formats/codecs/audio/Vorbis/Codebook")
|
||||
|
||||
local Setup = {}
|
||||
|
||||
local bits, ilog = Codebook.bits, Codebook.ilog
|
||||
local readu8, readu32 = buffer.readu8, buffer.readu32
|
||||
|
||||
export type Ident = {
|
||||
channels: number,
|
||||
sampleRate: number,
|
||||
bitrateMax: number,
|
||||
bitrateNominal: number,
|
||||
bitrateMin: number,
|
||||
blocksize0: number,
|
||||
blocksize1: number,
|
||||
}
|
||||
|
||||
export type Floor1 = {
|
||||
partitions: number,
|
||||
partitionClass: { number }, -- 1-based list of class numbers (0-based values)
|
||||
classDims: { number }, -- [class + 1]
|
||||
classSubs: { number },
|
||||
classMaster: { number },
|
||||
subBooks: { { number } }, -- [class + 1][sub + 1] = book (0-based) or -1
|
||||
multiplier: number,
|
||||
xlist: { number }, -- [j + 1]
|
||||
values: number,
|
||||
sorted: { number }, -- point indices (0-based) by increasing x
|
||||
low: { number }, -- neighbours, [j + 1] (0-based indices)
|
||||
high: { number },
|
||||
}
|
||||
|
||||
export type Residue = {
|
||||
type: number,
|
||||
begin: number,
|
||||
finish: number,
|
||||
partSize: number,
|
||||
classifications: number,
|
||||
classbook: number,
|
||||
books: { { number } }, -- [class + 1][pass + 1] = book or -1
|
||||
classdata: { { number } }, -- [classbook entry + 1][k + 1] = class
|
||||
}
|
||||
|
||||
export type Mapping = {
|
||||
submaps: number,
|
||||
couplingSteps: number,
|
||||
magnitude: { number }, -- [step + 1] channel (0-based)
|
||||
angle: { number },
|
||||
mux: { number }, -- [channel + 1] submap (0-based)
|
||||
submapFloor: { number },
|
||||
submapResidue: { number },
|
||||
}
|
||||
|
||||
export type Mode = { blockflag: number, mapping: number }
|
||||
|
||||
export type Setup = {
|
||||
books: { Codebook.Book },
|
||||
floors: { Floor1 },
|
||||
residues: { Residue },
|
||||
mappings: { Mapping },
|
||||
modes: { Mode },
|
||||
modeBits: number,
|
||||
maxFloorValues: number,
|
||||
}
|
||||
|
||||
local function isVorbis(b: buffer, o: number, n: number, kind: number): boolean
|
||||
return n >= 7 and readu8(b, o) == kind and buffer.readstring(b, o + 1, 6) == "vorbis"
|
||||
end
|
||||
Setup.isVorbis = isVorbis
|
||||
|
||||
-- ident(buf, offset, size) -> Ident | nil, message
|
||||
function Setup.ident(b: buffer, o: number, n: number): (Ident?, string?)
|
||||
if not isVorbis(b, o, n, 1) then
|
||||
return nil, "not a Vorbis identification header"
|
||||
end
|
||||
if n < 30 then
|
||||
return nil, "short identification header"
|
||||
end
|
||||
if readu32(b, o + 7) ~= 0 then
|
||||
return nil, "Vorbis version " .. readu32(b, o + 7)
|
||||
end
|
||||
local ch = readu8(b, o + 11)
|
||||
local rate = readu32(b, o + 12)
|
||||
local bs = readu8(b, o + 28)
|
||||
local l0, l1 = bs % 16, bs // 16
|
||||
if ch == 0 or rate == 0 then
|
||||
return nil, "no channels or no sample rate"
|
||||
end
|
||||
if l0 < 6 or l0 > 13 or l1 < 6 or l1 > 13 or l0 > l1 then
|
||||
return nil, "bad blocksizes"
|
||||
end
|
||||
if readu8(b, o + 29) % 2 == 0 then
|
||||
return nil, "identification header framing bit not set"
|
||||
end
|
||||
return {
|
||||
channels = ch,
|
||||
sampleRate = rate,
|
||||
bitrateMax = buffer.readi32(b, o + 16),
|
||||
bitrateNominal = buffer.readi32(b, o + 20),
|
||||
bitrateMin = buffer.readi32(b, o + 24),
|
||||
blocksize0 = 2 ^ l0,
|
||||
blocksize1 = 2 ^ l1,
|
||||
},
|
||||
nil
|
||||
end
|
||||
|
||||
-- comment(buf, offset, size) -> vendor, { "KEY=value" } | nil, message
|
||||
function Setup.comment(b: buffer, o: number, n: number): (string?, { string }?)
|
||||
if not isVorbis(b, o, n, 3) then
|
||||
return nil, nil
|
||||
end
|
||||
local p, e = o + 7, o + n
|
||||
if p + 4 > e then
|
||||
return nil, nil
|
||||
end
|
||||
local vlen = readu32(b, p)
|
||||
p += 4
|
||||
if vlen > e - p then
|
||||
return nil, nil
|
||||
end
|
||||
local vendor = buffer.readstring(b, p, vlen)
|
||||
p += vlen
|
||||
local list = {}
|
||||
if p + 4 > e then
|
||||
return vendor, list
|
||||
end
|
||||
local count = readu32(b, p)
|
||||
p += 4
|
||||
for _ = 1, count do
|
||||
if p + 4 > e then
|
||||
break
|
||||
end
|
||||
local len = readu32(b, p)
|
||||
p += 4
|
||||
if len > e - p then
|
||||
break
|
||||
end
|
||||
table.insert(list, buffer.readstring(b, p, len))
|
||||
p += len
|
||||
end
|
||||
return vendor, list
|
||||
end
|
||||
|
||||
-- setup(buf, offset, size, ident) -> Setup | nil, message
|
||||
function Setup.setup(b: buffer, o: number, n: number, id: Ident): (Setup?, string?)
|
||||
if not isVorbis(b, o, n, 5) then
|
||||
return nil, "not a Vorbis setup header"
|
||||
end
|
||||
local br = Codebook.reader(b, o + 7, n - 7)
|
||||
local channels = id.channels
|
||||
-- codebooks
|
||||
local books: { Codebook.Book } = {}
|
||||
local nbooks = bits(br, 8) + 1
|
||||
for i = 1, nbooks do
|
||||
local book, err = Codebook.read(br)
|
||||
if not book then
|
||||
return nil, "codebook " .. (i - 1) .. ": " .. tostring(err)
|
||||
end
|
||||
books[i] = book
|
||||
end
|
||||
-- time domain transforms (placeholders)
|
||||
local ntimes = bits(br, 6) + 1
|
||||
for _ = 1, ntimes do
|
||||
if bits(br, 16) ~= 0 then
|
||||
return nil, "bad time domain transform"
|
||||
end
|
||||
end
|
||||
-- floors
|
||||
local floors: { Floor1 } = {}
|
||||
local maxValues = 0
|
||||
local nfloors = bits(br, 6) + 1
|
||||
for i = 1, nfloors do
|
||||
local ftype = bits(br, 16)
|
||||
if ftype == 0 then
|
||||
return nil, "floor 0"
|
||||
elseif ftype ~= 1 then
|
||||
return nil, "bad floor type " .. ftype
|
||||
end
|
||||
local parts = bits(br, 5)
|
||||
local pclass = table.create(parts, 0)
|
||||
local maxClass = -1
|
||||
for j = 1, parts do
|
||||
pclass[j] = bits(br, 4)
|
||||
if pclass[j] > maxClass then
|
||||
maxClass = pclass[j]
|
||||
end
|
||||
end
|
||||
local cdims, csubs, cmaster, subBooks = {}, {}, {}, {}
|
||||
for j = 1, maxClass + 1 do
|
||||
cdims[j] = bits(br, 3) + 1
|
||||
csubs[j] = bits(br, 2)
|
||||
cmaster[j] = -1
|
||||
if csubs[j] > 0 then
|
||||
cmaster[j] = bits(br, 8)
|
||||
if cmaster[j] >= nbooks then
|
||||
return nil, "bad floor master book"
|
||||
end
|
||||
end
|
||||
local sb = {}
|
||||
for k = 1, 2 ^ csubs[j] do
|
||||
sb[k] = bits(br, 8) - 1
|
||||
if sb[k] >= nbooks then
|
||||
return nil, "bad floor subclass book"
|
||||
end
|
||||
end
|
||||
subBooks[j] = sb
|
||||
end
|
||||
local mult = bits(br, 2) + 1
|
||||
local rangebits = bits(br, 4)
|
||||
local xlist = { 0, 2 ^ rangebits }
|
||||
for j = 1, parts do
|
||||
local c = pclass[j]
|
||||
for _ = 1, cdims[c + 1] do
|
||||
table.insert(xlist, bits(br, rangebits))
|
||||
end
|
||||
end
|
||||
local values = #xlist
|
||||
if values > 65 * 8 then
|
||||
return nil, "too many floor points"
|
||||
end
|
||||
if br.eop then
|
||||
return nil, "setup header ends in a floor"
|
||||
end
|
||||
local order = table.create(values, 0)
|
||||
for j = 1, values do
|
||||
order[j] = j - 1
|
||||
end
|
||||
table.sort(order, function(a: number, c: number): boolean
|
||||
return xlist[a + 1] < xlist[c + 1]
|
||||
end)
|
||||
for j = 1, values - 1 do
|
||||
if xlist[order[j] + 1] == xlist[order[j + 1] + 1] then
|
||||
return nil, "duplicate floor x"
|
||||
end
|
||||
end
|
||||
local low, high = table.create(values, 0), table.create(values, 0)
|
||||
for j = 2, values - 1 do
|
||||
local lo, hi = -1, 65536
|
||||
local x = xlist[j + 1]
|
||||
for k = 0, j - 1 do
|
||||
local xv = xlist[k + 1]
|
||||
if xv > lo and xv < x then
|
||||
low[j + 1], lo = k, xv
|
||||
end
|
||||
if xv < hi and xv > x then
|
||||
high[j + 1], hi = k, xv
|
||||
end
|
||||
end
|
||||
end
|
||||
floors[i] = {
|
||||
partitions = parts,
|
||||
partitionClass = pclass,
|
||||
classDims = cdims,
|
||||
classSubs = csubs,
|
||||
classMaster = cmaster,
|
||||
subBooks = subBooks,
|
||||
multiplier = mult,
|
||||
xlist = xlist,
|
||||
values = values,
|
||||
sorted = order,
|
||||
low = low,
|
||||
high = high,
|
||||
}
|
||||
if values > maxValues then
|
||||
maxValues = values
|
||||
end
|
||||
end
|
||||
-- residues
|
||||
local residues: { Residue } = {}
|
||||
local nres = bits(br, 6) + 1
|
||||
for i = 1, nres do
|
||||
local rtype = bits(br, 16)
|
||||
if rtype > 2 then
|
||||
return nil, "bad residue type " .. rtype
|
||||
end
|
||||
local rbegin, rend = bits(br, 24), bits(br, 24)
|
||||
local psize = bits(br, 24) + 1
|
||||
local ncls = bits(br, 6) + 1
|
||||
local cbook = bits(br, 8)
|
||||
if rend < rbegin then
|
||||
return nil, "residue end before begin"
|
||||
end
|
||||
if cbook >= nbooks then
|
||||
return nil, "bad residue classbook"
|
||||
end
|
||||
local cascade = table.create(ncls, 0)
|
||||
for j = 1, ncls do
|
||||
local lowBits = bits(br, 3)
|
||||
local highBits = if bits(br, 1) == 1 then bits(br, 5) else 0
|
||||
cascade[j] = highBits * 8 + lowBits
|
||||
end
|
||||
local rbooks = {}
|
||||
for j = 1, ncls do
|
||||
local row = table.create(8, -1)
|
||||
local c = cascade[j]
|
||||
for k = 1, 8 do
|
||||
if c % 2 == 1 then
|
||||
row[k] = bits(br, 8)
|
||||
if row[k] >= nbooks then
|
||||
return nil, "bad residue book"
|
||||
end
|
||||
end
|
||||
c = c // 2
|
||||
end
|
||||
rbooks[j] = row
|
||||
end
|
||||
if br.eop then
|
||||
return nil, "setup header ends in a residue"
|
||||
end
|
||||
-- class numbers of each classbook entry (stb's classdata)
|
||||
local cb = books[cbook + 1]
|
||||
local cw = cb.dims
|
||||
if cb.entries * cw > 2 ^ 22 then
|
||||
return nil, "residue classbook too large"
|
||||
end
|
||||
local classdata = table.create(cb.entries)
|
||||
for j = 0, cb.entries - 1 do
|
||||
local row = table.create(cw, 0)
|
||||
local tmp = j
|
||||
for k = cw, 1, -1 do
|
||||
row[k] = tmp % ncls
|
||||
tmp = tmp // ncls
|
||||
end
|
||||
classdata[j + 1] = row
|
||||
end
|
||||
residues[i] = {
|
||||
type = rtype,
|
||||
begin = rbegin,
|
||||
finish = rend,
|
||||
partSize = psize,
|
||||
classifications = ncls,
|
||||
classbook = cbook,
|
||||
books = rbooks,
|
||||
classdata = classdata,
|
||||
}
|
||||
end
|
||||
-- mappings
|
||||
local mappings: { Mapping } = {}
|
||||
local nmaps = bits(br, 6) + 1
|
||||
local chBits = ilog(channels - 1)
|
||||
for i = 1, nmaps do
|
||||
if bits(br, 16) ~= 0 then
|
||||
return nil, "bad mapping type"
|
||||
end
|
||||
local submaps = if bits(br, 1) == 1 then bits(br, 4) + 1 else 1
|
||||
local steps = 0
|
||||
local mag, ang = {}, {}
|
||||
if bits(br, 1) == 1 then
|
||||
steps = bits(br, 8) + 1
|
||||
if steps > channels then
|
||||
return nil, "too many coupling steps"
|
||||
end
|
||||
for k = 1, steps do
|
||||
mag[k] = bits(br, chBits)
|
||||
ang[k] = bits(br, chBits)
|
||||
if mag[k] >= channels or ang[k] >= channels or mag[k] == ang[k] then
|
||||
return nil, "bad coupling channels"
|
||||
end
|
||||
end
|
||||
end
|
||||
if bits(br, 2) ~= 0 then
|
||||
return nil, "mapping reserved bits set"
|
||||
end
|
||||
local mux = table.create(channels, 0)
|
||||
if submaps > 1 then
|
||||
for j = 1, channels do
|
||||
mux[j] = bits(br, 4)
|
||||
if mux[j] >= submaps then
|
||||
return nil, "bad channel mux"
|
||||
end
|
||||
end
|
||||
end
|
||||
local sf, sr = {}, {}
|
||||
for j = 1, submaps do
|
||||
bits(br, 8)
|
||||
sf[j] = bits(br, 8)
|
||||
sr[j] = bits(br, 8)
|
||||
if sf[j] >= nfloors or sr[j] >= nres then
|
||||
return nil, "bad submap floor / residue"
|
||||
end
|
||||
end
|
||||
mappings[i] = {
|
||||
submaps = submaps,
|
||||
couplingSteps = steps,
|
||||
magnitude = mag,
|
||||
angle = ang,
|
||||
mux = mux,
|
||||
submapFloor = sf,
|
||||
submapResidue = sr,
|
||||
}
|
||||
end
|
||||
-- modes
|
||||
local modes: { Mode } = {}
|
||||
local nmodes = bits(br, 6) + 1
|
||||
for i = 1, nmodes do
|
||||
local bf = bits(br, 1)
|
||||
local wt, tt = bits(br, 16), bits(br, 16)
|
||||
local m = bits(br, 8)
|
||||
if wt ~= 0 or tt ~= 0 or m >= nmaps then
|
||||
return nil, "bad mode"
|
||||
end
|
||||
modes[i] = { blockflag = bf, mapping = m }
|
||||
end
|
||||
if br.eop then
|
||||
return nil, "setup header truncated"
|
||||
end
|
||||
if bits(br, 1) ~= 1 then
|
||||
return nil, "setup header framing bit not set"
|
||||
end
|
||||
return {
|
||||
books = books,
|
||||
floors = floors,
|
||||
residues = residues,
|
||||
mappings = mappings,
|
||||
modes = modes,
|
||||
modeBits = ilog(nmodes - 1),
|
||||
maxFloorValues = maxValues,
|
||||
},
|
||||
nil
|
||||
end
|
||||
|
||||
return Setup
|
||||
@@ -0,0 +1,37 @@
|
||||
--!strict
|
||||
-- codecs/audio/Vorbis/Tables: the floor 1 inverse dB table (Vorbis I specification, section 10.1, as in stb_vorbis 1.22,
|
||||
-- public domain / MIT, licenses/stb_vorbis-LICENSE.txt). 256 values, index = floor value (1-based here).
|
||||
return table.freeze({
|
||||
1.0649863e-07, 1.1341951e-07, 1.2079015e-07, 1.2863978e-07, 1.3699951e-07, 1.4590251e-07, 1.5538408e-07, 1.6548181e-07,
|
||||
1.7623575e-07, 1.8768855e-07, 1.9988561e-07, 2.1287530e-07, 2.2670913e-07, 2.4144197e-07, 2.5713223e-07, 2.7384213e-07,
|
||||
2.9163793e-07, 3.1059021e-07, 3.3077411e-07, 3.5226968e-07, 3.7516214e-07, 3.9954229e-07, 4.2550680e-07, 4.5315863e-07,
|
||||
4.8260743e-07, 5.1396998e-07, 5.4737065e-07, 5.8294187e-07, 6.2082472e-07, 6.6116941e-07, 7.0413592e-07, 7.4989464e-07,
|
||||
7.9862701e-07, 8.5052630e-07, 9.0579828e-07, 9.6466216e-07, 1.0273513e-06, 1.0941144e-06, 1.1652161e-06, 1.2409384e-06,
|
||||
1.3215816e-06, 1.4074654e-06, 1.4989305e-06, 1.5963394e-06, 1.7000785e-06, 1.8105592e-06, 1.9282195e-06, 2.0535261e-06,
|
||||
2.1869758e-06, 2.3290978e-06, 2.4804557e-06, 2.6416497e-06, 2.8133190e-06, 2.9961443e-06, 3.1908506e-06, 3.3982101e-06,
|
||||
3.6190449e-06, 3.8542308e-06, 4.1047004e-06, 4.3714470e-06, 4.6555282e-06, 4.9580707e-06, 5.2802740e-06, 5.6234160e-06,
|
||||
5.9888572e-06, 6.3780469e-06, 6.7925283e-06, 7.2339451e-06, 7.7040476e-06, 8.2047000e-06, 8.7378876e-06, 9.3057248e-06,
|
||||
9.9104632e-06, 1.0554501e-05, 1.1240392e-05, 1.1970856e-05, 1.2748789e-05, 1.3577278e-05, 1.4459606e-05, 1.5399272e-05,
|
||||
1.6400004e-05, 1.7465768e-05, 1.8600792e-05, 1.9809576e-05, 2.1096914e-05, 2.2467911e-05, 2.3928002e-05, 2.5482978e-05,
|
||||
2.7139006e-05, 2.8902651e-05, 3.0780908e-05, 3.2781225e-05, 3.4911534e-05, 3.7180282e-05, 3.9596466e-05, 4.2169667e-05,
|
||||
4.4910090e-05, 4.7828601e-05, 5.0936773e-05, 5.4246931e-05, 5.7772202e-05, 6.1526565e-05, 6.5524908e-05, 6.9783085e-05,
|
||||
7.4317983e-05, 7.9147585e-05, 8.4291040e-05, 8.9768747e-05, 9.5602426e-05, 0.00010181521, 0.00010843174, 0.00011547824,
|
||||
0.00012298267, 0.00013097477, 0.00013948625, 0.00014855085, 0.00015820453, 0.00016848555, 0.00017943469, 0.00019109536,
|
||||
0.00020351382, 0.00021673929, 0.00023082423, 0.00024582449, 0.00026179955, 0.00027881276, 0.00029693158, 0.00031622787,
|
||||
0.00033677814, 0.00035866388, 0.00038197188, 0.00040679456, 0.00043323036, 0.00046138411, 0.00049136745, 0.00052329927,
|
||||
0.00055730621, 0.00059352311, 0.00063209358, 0.00067317058, 0.00071691700, 0.00076350630, 0.00081312324, 0.00086596457,
|
||||
0.00092223983, 0.00098217216, 0.0010459992, 0.0011139742, 0.0011863665, 0.0012634633, 0.0013455702, 0.0014330129,
|
||||
0.0015261382, 0.0016253153, 0.0017309374, 0.0018434235, 0.0019632195, 0.0020908006, 0.0022266726, 0.0023713743,
|
||||
0.0025254795, 0.0026895994, 0.0028643847, 0.0030505286, 0.0032487691, 0.0034598925, 0.0036847358, 0.0039241906,
|
||||
0.0041792066, 0.0044507950, 0.0047400328, 0.0050480668, 0.0053761186, 0.0057254891, 0.0060975636, 0.0064938176,
|
||||
0.0069158225, 0.0073652516, 0.0078438871, 0.0083536271, 0.0088964928, 0.009474637, 0.010090352, 0.010746080,
|
||||
0.011444421, 0.012188144, 0.012980198, 0.013823725, 0.014722068, 0.015678791, 0.016697687, 0.017782797,
|
||||
0.018938423, 0.020169149, 0.021479854, 0.022875735, 0.024362330, 0.025945531, 0.027631618, 0.029427276,
|
||||
0.031339626, 0.033376252, 0.035545228, 0.037855157, 0.040315199, 0.042935108, 0.045725273, 0.048696758,
|
||||
0.051861348, 0.055231591, 0.058820850, 0.062643361, 0.066714279, 0.071049749, 0.075666962, 0.080584227,
|
||||
0.085821044, 0.091398179, 0.097337747, 0.10366330, 0.11039993, 0.11757434, 0.12521498, 0.13335215,
|
||||
0.14201813, 0.15124727, 0.16107617, 0.17154380, 0.18269168, 0.19456402, 0.20720788, 0.22067342,
|
||||
0.23501402, 0.25028656, 0.26655159, 0.28387361, 0.30232132, 0.32196786, 0.34289114, 0.36517414,
|
||||
0.38890521, 0.41417847, 0.44109412, 0.46975890, 0.50028648, 0.53279791, 0.56742212, 0.60429640,
|
||||
0.64356699, 0.68538959, 0.72993007, 0.77736504, 0.82788260, 0.88168307, 0.9389798, 1.0,
|
||||
})
|
||||
@@ -0,0 +1,13 @@
|
||||
--!strict
|
||||
-- codecs/audio/Vorbis: the Vorbis I decoder without its container: identification / comment / setup headers (Setup),
|
||||
-- codebooks (Codebook), audio packets to PCM (Packet: floors 0 / 1, residues 0 / 1 / 2, coupling, the IMDCT through
|
||||
-- codecs/audio/Mdct) and the shared tables. A Luau port of stb_vorbis 1.22 (public domain / MIT,
|
||||
-- licenses/stb_vorbis-LICENSE.txt). Ogg Vorbis (pages, granules, seeking) and the encoder are audio/vorbis.
|
||||
local Vorbis = {
|
||||
Setup = require("@formats/codecs/audio/Vorbis/Setup"),
|
||||
Codebook = require("@formats/codecs/audio/Vorbis/Codebook"),
|
||||
Packet = require("@formats/codecs/audio/Vorbis/Packet"),
|
||||
Tables = require("@formats/codecs/audio/Vorbis/Tables"),
|
||||
}
|
||||
|
||||
return Vorbis
|
||||
Reference in new issue
Block a user