codecs/audio: comments to specs only
This commit is contained in:
1 parent
0bd7b87d2b
commit
6dcce481bb
69 files changed
+523
-1604
No files matched your search
@@ -1,12 +1,9 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- codecs/audio/Opus/Celt: CELT decoder (RFC 6716 section 4.3), a port of the libopus float decoder: celt/celt_decoder.c, bands.c, vq.c,
|
||||
-- cwrs.c, rate.c, quant_bands.c, laplace.c, celt.c (comb filter). Only the standard 48 kHz / 960-sample mode is
|
||||
-- needed by Opus. Packet loss concealment is the noise-based variant only (no pitch PLC).
|
||||
--
|
||||
-- Arrays are Luau tables of numbers. All normalised-band data (X, Y, the folding memory `norm` and the scratch
|
||||
-- copies) lives in one table W, and the "pointers" of the C code are 0-based offsets into it.
|
||||
-- codecs/audio/Opus/Celt: CELT decoder (RFC 6716 section 4.3), 48 kHz / 960-sample mode, noise-based PLC only.
|
||||
-- A port of the libopus float decoder (celt_decoder.c, bands.c, vq.c, cwrs.c, rate.c, quant_bands.c, laplace.c, celt.c;
|
||||
-- BSD, licenses/libopus-BSD.txt). The C code's band pointers are 0-based offsets into one table W.
|
||||
local RD = require("@rosetta/codecs/entropy/RangeCoder/Decoder")
|
||||
local Mdct = require("@rosetta/codecs/audio/Opus/Mdct")
|
||||
local T = require("@rosetta/codecs/audio/Opus/CeltTables")
|
||||
@@ -102,7 +99,6 @@ for n = 1, UK - 1 do
|
||||
end
|
||||
end
|
||||
|
||||
-- shared workspace (decoding is not re-entrant)
|
||||
local NORM_BASE = 1920
|
||||
local W = table.create(4096, 0)
|
||||
local TMP = table.create(256, 0)
|
||||
@@ -164,7 +160,6 @@ local function bitexactLog2tan(isin: number, icos: number): number
|
||||
return (ls - lc) * 2048 + fracMul16(isin, fracMul16(isin, -2597) + 7932) - fracMul16(icos, fracMul16(icos, -2597) + 7932)
|
||||
end
|
||||
|
||||
-- ---------------------------------------------------------------------------------------------------------------
|
||||
-- energy (quant_bands.c, laplace.c)
|
||||
|
||||
local function laplaceDecode(dec: RD.Dec, fs: number, decay: number): number
|
||||
@@ -278,7 +273,6 @@ local function unquantEnergyFinalise(st: any, start: number, endb: number, fineQ
|
||||
end
|
||||
end
|
||||
|
||||
-- ---------------------------------------------------------------------------------------------------------------
|
||||
-- bit allocation (rate.c)
|
||||
|
||||
local function bits2pulses(bandI: number, LM: number, bits: number): number
|
||||
@@ -315,7 +309,7 @@ local function getPulses(i: number): number
|
||||
return lshift(8 + band(i, 7), rshift(i, 3) - 1)
|
||||
end
|
||||
|
||||
-- clt_compute_allocation + interp_bits2pulses (decoder side). Returns codedBands, balance, intensity, dualStereo.
|
||||
-- clt_compute_allocation + interp_bits2pulses (decoder side)
|
||||
local function computeAllocation(start: number, endb: number, offsets: { number }, cap: { number }, allocTrim: number,
|
||||
total: number, pulses: { number }, ebits: { number }, finePriority: { number }, C: number, LM: number, ec: RD.Dec)
|
||||
total = imax(total, 0)
|
||||
@@ -544,7 +538,6 @@ local function computeAllocation(start: number, endb: number, offsets: { number
|
||||
return codedBands, balance, intensity, dualStereo
|
||||
end
|
||||
|
||||
-- ---------------------------------------------------------------------------------------------------------------
|
||||
-- PVQ (cwrs.c, vq.c)
|
||||
|
||||
local function cwrsi(n: number, k: number, i: number, iy: { number }): number
|
||||
@@ -605,7 +598,6 @@ local function cwrsi(n: number, k: number, i: number, iy: { number }): number
|
||||
end
|
||||
n -= 1
|
||||
end
|
||||
-- n == 2
|
||||
p = 2 * k + 1
|
||||
local neg = i >= p
|
||||
if neg then
|
||||
@@ -619,7 +611,6 @@ local function cwrsi(n: number, k: number, i: number, iy: { number }): number
|
||||
val = if neg then k - k0 else k0 - k
|
||||
iy[idx] = val
|
||||
yy += val * val
|
||||
-- n == 1
|
||||
val = if i ~= 0 then -k else k
|
||||
iy[idx + 1] = val
|
||||
yy += val * val
|
||||
@@ -717,7 +708,6 @@ local function algUnquant(X: number, N: number, K: number, spread: number, B: nu
|
||||
return mask
|
||||
end
|
||||
|
||||
-- ---------------------------------------------------------------------------------------------------------------
|
||||
-- bands (bands.c)
|
||||
|
||||
type Ctx = {
|
||||
@@ -819,7 +809,6 @@ local function computeQn(N: number, b: number, offset: number, pulseCap: number,
|
||||
return lshift(rshift(qn + 1, 1), 1)
|
||||
end
|
||||
|
||||
-- returns b, fill, inv, imid, iside, delta, itheta, qalloc
|
||||
local function computeTheta(N: number, b: number, B: number, B0: number, LM: number, stereo: boolean, fill: number)
|
||||
local i = ctx.i
|
||||
local ec = ctx.ec
|
||||
@@ -1328,9 +1317,6 @@ local function antiCollapse(st: any, C: number, size: number, LM: number, start:
|
||||
end
|
||||
end
|
||||
|
||||
-- ---------------------------------------------------------------------------------------------------------------
|
||||
-- synthesis
|
||||
|
||||
local function denormaliseBands(X: number, freq: { number }, logE: { number }, eoff: number, start: number, endb: number, M: number, silence: boolean)
|
||||
local N = M * SHORT_MDCT
|
||||
local bound = M * EB[endb + 1]
|
||||
@@ -1486,9 +1472,6 @@ local function deemphasis(st: any, CC: number, N: number, pcm: { number }, pbase
|
||||
end
|
||||
end
|
||||
|
||||
-- ---------------------------------------------------------------------------------------------------------------
|
||||
-- decoder state
|
||||
|
||||
local function resetState(st: any)
|
||||
st.rng = 0
|
||||
st.error = 0
|
||||
@@ -1596,8 +1579,7 @@ local function decodeLost(st: any, N: number, LM: number)
|
||||
st.last_frame_type = FRAME_PLC_NOISE
|
||||
end
|
||||
|
||||
-- celt_decode_with_ec. pcm[pbase + j * channels + c]; frameSize at 48 kHz. dec may be nil (a new range decoder over
|
||||
-- buf[off, off + len)). buf == nil or len <= 1 conceals. Returns the samples per channel, or < 0 on error.
|
||||
-- celt_decode_with_ec (buf nil or len <= 1 conceals)
|
||||
function Celt.decode(st: any, buf: buffer?, off: number, len: number, pcm: { number }, pbase: number, frameSize: number,
|
||||
dec: RD.Dec?, accum: boolean): number
|
||||
local CC = st.channels
|
||||
@@ -1766,7 +1748,6 @@ function Celt.decode(st: any, buf: buffer?, off: number, len: number, pcm: { num
|
||||
pulses, fineQuant, finePriority, C, LM, ec)
|
||||
unquantFineEnergy(st, start, endb, fineQuant, ec, C)
|
||||
|
||||
-- shift the decoder memory by one frame
|
||||
for c = 1, CC do
|
||||
table.move(st.mem[c], N + 1, DECODE_BUFFER_SIZE + OVERLAP, 1)
|
||||
end
|
||||
|
||||
@@ -1,12 +1,9 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- codecs/audio/Opus/CeltEncoder: a fixed-frame CELT encoder (RFC 6716 section 5.3): 20 ms frames (LM 3), full band, constant
|
||||
-- bit rate, long MDCTs only (no transient detection), no pitch pre-filter, normal spreading, default allocation trim,
|
||||
-- mono, or stereo as mid / side per band (the angle, intensity when it has no bits; dual L / R stereo on request). The bitstream side is a port of the libopus 1.6 encoder paths
|
||||
-- (celt_encoder.c, quant_bands.c, laplace.c, rate.c, bands.c, vq.c, cwrs.c; BSD: licenses/libopus-BSD.txt) that
|
||||
-- mirror codecs/audio/Opus/Celt's decoder step for step; the analysis is simplified. The MDCT is codecs/audio/Mdct.
|
||||
-- Never throws.
|
||||
-- codecs/audio/Opus/CeltEncoder: a fixed-frame CELT encoder (RFC 6716 section 5.3): 20 ms, full band, CBR, long MDCTs only.
|
||||
-- The bitstream side is a port of the libopus 1.6 encoder paths (celt_encoder.c, quant_bands.c, laplace.c, rate.c,
|
||||
-- bands.c, vq.c, cwrs.c; BSD, licenses/libopus-BSD.txt); the analysis is simplified.
|
||||
local RE = require("@rosetta/codecs/entropy/RangeCoder/Encoder")
|
||||
local T = require("@rosetta/codecs/audio/Opus/CeltTables")
|
||||
local Mdct = require("@rosetta/codecs/audio/Mdct")
|
||||
@@ -67,7 +64,6 @@ local EXP2_TABLE8 = { 16384, 17866, 19483, 21247, 23170, 25267, 27554, 30048 }
|
||||
local SPREAD_NORMAL = 2
|
||||
local SPREAD_FACTOR = { 15, 10, 5 }
|
||||
|
||||
-- the MDCT window (the decoder's) and plan
|
||||
local WINDOW = table.create(OVERLAP, 0)
|
||||
for i = 0, OVERLAP - 1 do
|
||||
local s = math.sin(0.5 * math.pi * (i + 0.5) / OVERLAP)
|
||||
@@ -116,7 +112,6 @@ local function bitexactLog2tan(isin: number, icos: number): number
|
||||
return (ls - lc) * 2048 + fracMul16(isin, fracMul16(isin, -2597) + 7932) - fracMul16(icos, fracMul16(icos, -2597) + 7932)
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ energy
|
||||
-- ec_laplace_encode: returns the value actually coded (clamped in the flat tail)
|
||||
local function laplaceEncode(enc: RE.Enc, value: number, fs: number, decay: number): number
|
||||
local fl = 0
|
||||
@@ -153,23 +148,22 @@ end
|
||||
|
||||
export type State = {
|
||||
channels: number,
|
||||
oldBandE: { number }, -- the decoder's view (2 * 21)
|
||||
oldBandE: { number },
|
||||
preMem: { number },
|
||||
inMem: { { number } }, -- the last OVERLAP pre-emphasised samples per channel
|
||||
inMem: { { number } },
|
||||
frames: number,
|
||||
X: { { number } }, -- per channel 960 normalised coefficients
|
||||
X: { { number } },
|
||||
bandLogE: { number },
|
||||
bandE: { number }, -- linear band amplitudes (intensity stereo weights)
|
||||
bandE: { number },
|
||||
error: { number },
|
||||
work: { number },
|
||||
tmp: { number },
|
||||
iy: { number },
|
||||
remaining: number,
|
||||
spread: number,
|
||||
dual: boolean, -- stereo: L / R coded independently (default false: mid / side)
|
||||
dual: boolean,
|
||||
}
|
||||
|
||||
-- new(channels) -> State
|
||||
function CeltEncoder.new(channels: number): State
|
||||
local inMem = {}
|
||||
local X = {}
|
||||
@@ -302,7 +296,6 @@ local function quantEnergyFinalise(st: State, fineQuant: { number }, finePriorit
|
||||
end
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ allocation
|
||||
local function bits2pulses(bandI: number, lm: number, bits: number): number
|
||||
local base = CACHE_INDEX[(lm + 1) * NB_EBANDS + bandI + 1]
|
||||
local lo, hi = 0, CACHE_BITS[base + 1]
|
||||
@@ -566,7 +559,6 @@ local function computeAllocation(offsets: { number }, cap: { number }, allocTrim
|
||||
return codedBands, balance, intensity, dualStereo
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ PVQ
|
||||
-- icwrs: the index of the pulse vector iy[1..n]
|
||||
local function icwrs(n: number, iy: { number }): number
|
||||
local j = n -- 1-based position of _y[n - 1]
|
||||
@@ -700,7 +692,6 @@ local SCRATCH_A = table.create(256, 0)
|
||||
local ROT = table.create(256, 0)
|
||||
|
||||
local function algQuant(st: State, X: { number }, xo: number, n: number, K: number, enc: RE.Enc)
|
||||
-- rotate a copy (the original band stays for nothing else, but keep it intact)
|
||||
for j = 1, n do
|
||||
ROT[j] = X[xo + j]
|
||||
end
|
||||
@@ -710,7 +701,6 @@ local function algQuant(st: State, X: { number }, xo: number, n: number, K: numb
|
||||
encUint(enc, icwrs(n, iy), pvqU(n, K) + pvqU(n, K + 1))
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ bands
|
||||
local function computeQn(n: number, b: number, offset: number, pulseCap: number): number
|
||||
local N2 = 2 * n - 1
|
||||
local qb = tdiv(b + N2 * offset, N2)
|
||||
@@ -929,14 +919,10 @@ local function quantAllBands(st: State, C: number, pulses: { number }, dualStere
|
||||
end
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ frame
|
||||
-- encodeFrame(st, pcm, base, stride, nbBytes) -> buffer: one CELT frame of nbBytes from 960 samples per channel
|
||||
-- (pcm[base + k * stride + c], floats -1 .. 1, interleaved)
|
||||
function CeltEncoder.encodeFrame(st: State, pcm: { number }, base: number, stride: number, nbBytes: number): buffer
|
||||
local C = st.channels
|
||||
local work, tmp = st.work, st.tmp
|
||||
local silent = true
|
||||
-- pre-emphasis, the windowed 1920-sample MDCT input, band energies, normalisation
|
||||
for c = 1, C do
|
||||
local inMem = st.inMem[c]
|
||||
local m = st.preMem[c]
|
||||
@@ -988,7 +974,6 @@ function CeltEncoder.encodeFrame(st: State, pcm: { number }, base: number, strid
|
||||
local enc = RE.new(nbBytes)
|
||||
local totalBits = nbBytes * 8
|
||||
local oldE = st.oldBandE
|
||||
-- silence
|
||||
if silent then
|
||||
encBitLogp(enc, true, 15)
|
||||
enc.nbits_total += totalBits - tell(enc)
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
--!strict
|
||||
-- codecs/audio/Opus/CeltTables: Generated from libopus celt/static_modes_float.h and celt/quant_bands.c (BSD, see licenses/). 1-based arrays.
|
||||
-- codecs/audio/Opus/CeltTables: from libopus celt/static_modes_float.h and celt/quant_bands.c (BSD, licenses/).
|
||||
return {
|
||||
logN400 = {
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 8, 8, 8, 8, 16, 16, 16, 21, 21, 24, 29, 34, 36,
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
--!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.
|
||||
-- codecs/audio/Opus/Decoder: the Opus decoder proper (libopus src/opus_decoder.c and opus_multistream_decoder.c).
|
||||
-- SILK / hybrid / CELT mode switching, redundant CELT frames of mode transitions, cross-fades, PLC, multistream.
|
||||
local RD = require("@rosetta/codecs/entropy/RangeCoder/Decoder")
|
||||
local Packet = require("@rosetta/codecs/audio/Opus/Packet")
|
||||
local Celt = require("@rosetta/codecs/audio/Opus/Celt")
|
||||
@@ -153,7 +152,6 @@ local function decodeFrame(st: State, buf: buffer?, off: number, len: number, pc
|
||||
end
|
||||
frameSize = audiosize
|
||||
|
||||
-- SILK
|
||||
if mode ~= MODE_CELT then
|
||||
local tooSmall = frameSize < F10
|
||||
local sarr, sbase = pcm, pbase
|
||||
@@ -289,8 +287,7 @@ local function decodeFrame(st: State, buf: buffer?, off: number, len: number, pc
|
||||
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).
|
||||
-- opus_decode_native (buf nil or len 0: conceal frameSize samples)
|
||||
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
|
||||
@@ -337,7 +334,6 @@ function Decoder.decode(st: State, buf: buffer?, off: number, len: number, pcm:
|
||||
return nb, packetOffset
|
||||
end
|
||||
|
||||
-- ---------------------------------------------------------------------------------------------------------------
|
||||
-- multistream (channel mapping families 0 / 1 / 255)
|
||||
|
||||
export type MS = {
|
||||
@@ -370,7 +366,6 @@ function Decoder.resetMultistream(ms: MS)
|
||||
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
|
||||
@@ -386,7 +381,7 @@ local function copyOut(ms: MS, id: number, src: { number }, sc: number, sstride:
|
||||
end
|
||||
end
|
||||
|
||||
-- opus_multistream_decode_native (float). Returns the samples per channel, or < 0 on error.
|
||||
-- opus_multistream_decode_native
|
||||
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)
|
||||
@@ -428,7 +423,6 @@ function Decoder.decodeMultistream(ms: MS, buf: buffer?, off: number, len: numbe
|
||||
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
|
||||
@@ -444,8 +438,6 @@ function Decoder.writeF32(src: { number }, base: number, count: number, gain: nu
|
||||
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
|
||||
|
||||
@@ -1,19 +1,15 @@
|
||||
--!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.
|
||||
-- codecs/audio/Opus/Mdct: CELT inverse MDCT (libopus celt/mdct.c clt_mdct_backward) over the kiss_fft radix 5 / 3 / 4 / 2 stages.
|
||||
-- N = 1920 >> shift (FFT sizes 480 .. 60). Kept apart from codecs/audio/Mdct: CELT keeps only the middle N / 2 of the
|
||||
-- full IMDCT, so the shared transform was 2-3x slower and changed the output bits.
|
||||
local Mdct = {}
|
||||
|
||||
local sin, cos, pi = math.sin, math.cos, math.pi
|
||||
|
||||
type Plan = {
|
||||
n: number, -- FFT size (N / 4)
|
||||
n: number,
|
||||
factors: { number },
|
||||
bitrev: { number },
|
||||
twr: { number },
|
||||
@@ -82,13 +78,11 @@ 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
|
||||
@@ -96,14 +90,12 @@ local function bfly2(fr: { number }, fi: { number }, N: number)
|
||||
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
|
||||
@@ -265,7 +257,7 @@ local function bfly5(fr: { number }, fi: { number }, fstride: number, twr: { num
|
||||
end
|
||||
end
|
||||
|
||||
-- in-place forward FFT of bit-reversed input (opus_fft_impl)
|
||||
-- opus_fft_impl (bit-reversed input)
|
||||
local function fftImpl(pl: Plan)
|
||||
local factors = pl.factors
|
||||
local fr, fi, twr, twi = pl.fr, pl.fi, pl.twr, pl.twi
|
||||
@@ -295,9 +287,7 @@ local function fftImpl(pl: Plan)
|
||||
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).
|
||||
-- clt_mdct_backward
|
||||
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
|
||||
@@ -319,7 +309,6 @@ function Mdct.backward(inp: { number }, inBase: number, out: { number }, outBase
|
||||
xp2 -= 2 * stride
|
||||
end
|
||||
fftImpl(pl)
|
||||
-- post-rotate
|
||||
local h = outBase + (overlap // 2)
|
||||
for k = 0, N4 - 1 do
|
||||
local re = fi[k + 1]
|
||||
|
||||
@@ -46,7 +46,6 @@ 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)
|
||||
@@ -62,7 +61,6 @@ function Packet.samplesPerFrame(toc: number, Fs: number): number
|
||||
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
|
||||
@@ -100,9 +98,7 @@ local function parseSize(buf: buffer, p: number, len: number): (number, number)
|
||||
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.
|
||||
-- opus_packet_parse_impl; self-delimited packets (multistream) carry an explicit size for the last frame
|
||||
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
|
||||
|
||||
@@ -1,19 +1,8 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- codecs/audio/Opus/Silk: SILK decoder: a bit-exact port of the libopus fixed-point SILK decoder (silk/dec_API.c and what it calls:
|
||||
-- decode_frame, decode_indices, decode_pulses, shell_coder, code_signs, decode_parameters, gain_quant, NLSF_*,
|
||||
-- LPC_fit, LPC_inv_pred_gain, decode_core, stereo_*, CNG, PLC, resampler_private_up2_HQ / IIR_FIR). BSD, see
|
||||
-- licenses/.
|
||||
--
|
||||
-- local st = Silk.new(apiChannels)
|
||||
-- local ret, n = Silk.decode(st, ctrl, lostFlag, firstFrame, ec, out, outBase)
|
||||
-- ctrl = { nChannelsAPI, nChannelsInternal, API_sampleRate, internalSampleRate, payloadSize_ms }
|
||||
-- decodes one 10 / 20 ms SILK frame and writes n samples per channel (at API_sampleRate) to the Luau array
|
||||
-- `out`, interleaved by nChannelsAPI: out[outBase + i * nChannelsAPI + c], scaled int16 / 32768.
|
||||
--
|
||||
-- Fixed-point emulation: all values are Luau numbers holding C int32 / int16 values. Products of two int32 values
|
||||
-- (64-bit in C) are split so that every intermediate stays below 2^53 and therefore exact.
|
||||
-- codecs/audio/Opus/Silk: SILK decoder, a bit-exact port of the libopus fixed-point SILK decoder (silk/dec_API.c and what
|
||||
-- it calls; BSD, licenses/). C int32 / int16 values are Luau numbers; int32 x int32 products are split to stay below 2^53.
|
||||
local RD = require("@rosetta/codecs/entropy/RangeCoder/Decoder")
|
||||
local T = require("@rosetta/codecs/audio/Opus/SilkTables")
|
||||
|
||||
@@ -24,7 +13,7 @@ local floor = math.floor
|
||||
|
||||
local Silk = {}
|
||||
|
||||
local P2 = table.create(64, 0) -- P2[s] = 2^s (1-based by s: P2[s] for s >= 1, P2_0 = 1)
|
||||
local P2 = table.create(64, 0)
|
||||
for s = 1, 63 do
|
||||
P2[s] = 2 ^ s
|
||||
end
|
||||
@@ -35,8 +24,6 @@ local function p2(s: number): number
|
||||
return P2[s]
|
||||
end
|
||||
|
||||
-- C helpers -----------------------------------------------------------------------------------------------------
|
||||
|
||||
local function s16(x: number): number
|
||||
x = x % 65536
|
||||
if x >= 32768 then
|
||||
@@ -252,8 +239,6 @@ local function sumSqrShift(x: { number }, xo: number, len: number): (number, num
|
||||
return nrg, shft
|
||||
end
|
||||
|
||||
-- LPC helpers ---------------------------------------------------------------------------------------------------
|
||||
|
||||
-- silk_bwexpander (int16 coefficients), ar 1-based
|
||||
local function bwexpander(ar: { number }, d: number, chirp_Q16: number)
|
||||
local chirp_minus_one_Q16 = chirp_Q16 - 65536
|
||||
@@ -451,8 +436,6 @@ local function lpcAnalysisFilter(out: { number }, oo: number, inp: { number }, i
|
||||
end
|
||||
end
|
||||
|
||||
-- NLSF ----------------------------------------------------------------------------------------------------------
|
||||
|
||||
local CB_NB_MB = {
|
||||
nVectors = 32,
|
||||
order = 10,
|
||||
@@ -604,8 +587,6 @@ local function nlsfDecode(pNLSF_Q15: { number }, NLSFIndices: { number }, CB: an
|
||||
nlsfStabilize(pNLSF_Q15, CB.deltaMin_Q15, order)
|
||||
end
|
||||
|
||||
-- Resampler (decoder side: 8 / 12 / 16 kHz up to the API rate) ----------------------------------------------------
|
||||
|
||||
local RES_COPY, RES_UP2, RES_IIR_FIR = 0, 1, 2
|
||||
local up2_hq_0 = { 1746, 14986, 39083 - 65536 }
|
||||
local up2_hq_1 = { 6854, 25769, 55542 - 65536 }
|
||||
@@ -733,7 +714,6 @@ local function up2HQ(S: { number }, out: { number }, oo: number, inp: { number }
|
||||
S[1], S[2], S[3], S[4], S[5], S[6] = s0, s1, s2, s3, s4, s5
|
||||
end
|
||||
|
||||
-- returns the next output index
|
||||
local function iirFirInterpol(out: { number }, oo: number, buf: { number }, max_index_Q16: number, incr: number): number
|
||||
local index_Q16 = 0
|
||||
while index_Q16 < max_index_Q16 do
|
||||
@@ -814,8 +794,6 @@ local function resampler(S: any, out: { number }, inp: { number }, io: number, i
|
||||
end
|
||||
end
|
||||
|
||||
-- Decoder state -------------------------------------------------------------------------------------------------
|
||||
|
||||
local function newChannel(): any
|
||||
return {
|
||||
prev_gain_Q16 = 65536,
|
||||
@@ -943,8 +921,6 @@ local function decoderSetFs(ch: any, fs_kHz: number, fs_API_Hz: number): number
|
||||
return ret
|
||||
end
|
||||
|
||||
-- Bitstream: indices and pulses ---------------------------------------------------------------------------------
|
||||
|
||||
local LTP_gain_iCDF = { T.LTP_gain_iCDF_0, T.LTP_gain_iCDF_1, T.LTP_gain_iCDF_2 }
|
||||
local LTP_vq_Q7 = { T.LTP_gain_vq_0, T.LTP_gain_vq_1, T.LTP_gain_vq_2 }
|
||||
local LBRR_flags_iCDF = { T.LBRR_flags_2_iCDF, T.LBRR_flags_3_iCDF }
|
||||
@@ -1115,8 +1091,6 @@ local function decodePulses(d: any, pulses: { number }, signalType: number, quan
|
||||
end
|
||||
end
|
||||
|
||||
-- Parameters ----------------------------------------------------------------------------------------------------
|
||||
|
||||
local pNLSF_Q15 = table.create(16, 0)
|
||||
local pNLSF0_Q15 = table.create(16, 0)
|
||||
|
||||
@@ -1214,8 +1188,6 @@ local function decodeParameters(ch: any, ctrl: any, condCoding: number)
|
||||
end
|
||||
end
|
||||
|
||||
-- Core synthesis ------------------------------------------------------------------------------------------------
|
||||
|
||||
local sLTP = table.create(320, 0)
|
||||
local sLTP_Q15 = table.create(640, 0)
|
||||
local res_Q14 = table.create(80, 0)
|
||||
@@ -1381,8 +1353,6 @@ local function decodeCore(ch: any, ctrl: any, xq: { number }, xo: number, pulses
|
||||
end
|
||||
end
|
||||
|
||||
-- PLC / CNG -----------------------------------------------------------------------------------------------------
|
||||
|
||||
local HARM_ATT_Q15 = { 32440, 31130 }
|
||||
local PLC_RAND_ATTENUATE_V_Q15 = { 31130, 26214 }
|
||||
local PLC_RAND_ATTENUATE_UV_Q15 = { 32440, 29491 }
|
||||
@@ -1708,8 +1678,6 @@ local function cng(ch: any, ctrl: any, frame: { number }, fo: number, length: nu
|
||||
end
|
||||
end
|
||||
|
||||
-- Frame decode --------------------------------------------------------------------------------------------------
|
||||
|
||||
-- silk_decode_frame: writes frame_length samples to out[fo ..]
|
||||
local function decodeFrame(ch: any, d: any, out: { number }, fo: number, lostFlag: number, condCoding: number): number
|
||||
local L = ch.frame_length
|
||||
@@ -1760,8 +1728,6 @@ local function decodeFrame(ch: any, d: any, out: { number }, fo: number, lostFla
|
||||
return L
|
||||
end
|
||||
|
||||
-- Stereo --------------------------------------------------------------------------------------------------------
|
||||
|
||||
local function stereoDecodePred(d: any, pred_Q13: { number })
|
||||
local n = icdf(d, T.stereo_pred_joint_iCDF, 1, 8)
|
||||
local ix02 = n // 5
|
||||
@@ -1818,8 +1784,6 @@ local function stereoMSToLR(state: any, x1: { number }, x2: { number }, pred_Q13
|
||||
end
|
||||
end
|
||||
|
||||
-- API -----------------------------------------------------------------------------------------------------------
|
||||
|
||||
export type State = any
|
||||
|
||||
function Silk.new(apiChannels: number): State
|
||||
|
||||
@@ -1,9 +1,6 @@
|
||||
--!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.
|
||||
-- codecs/audio/Opus: the Opus codec (RFC 6716) without its container.
|
||||
-- A port of the libopus 1.6 decoder (BSD, licenses/libopus-BSD.txt), absorbed from opus-luau dd2b4e5; the CELT encoder is new.
|
||||
local Opus = {
|
||||
Packet = require("@rosetta/codecs/audio/Opus/Packet"),
|
||||
Decoder = require("@rosetta/codecs/audio/Opus/Decoder"),
|
||||
|
||||
Reference in new issue
Block a user