codecs/audio: comments to specs only

This commit is contained in:
4DBug committed 2026-11-01 01:55:39 -05:00
1 parent 0bd7b87d2b
commit 6dcce481bb
69 files changed
+523 -1604

No files matched your search

+8 -23
View File
@@ -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)