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,11 +1,8 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- codecs/audio/Alac/Encode: an ALAC frame encoder: per element (SCE / CPE) the stereo mix (mixBits 2, the best mixRes of
|
||||
-- 0 .. 4 by first-difference energy), per channel LPC coefficients (Levinson-Durbin on a windowed autocorrelation,
|
||||
-- orders 4 and 8 tried, quantised with denShift 9), residuals through the same adaptive FIR the decoder runs, the
|
||||
-- adaptive Golomb-Rice code with zero runs; an uncompressed (escape) frame when that is smaller. 16 / 20 / 24-bit
|
||||
-- input. Written for this library from the ALAC format; no third-party code. Never throws.
|
||||
-- codecs/audio/Alac/Encode: an ALAC frame encoder (mixBits 2, LPC orders 4 / 8 with denShift 9, escape frames).
|
||||
-- Written from the ALAC format; no third-party code.
|
||||
local band, rshift, bor, lshift, countlz = bit32.band, bit32.rshift, bit32.bor, bit32.lshift, bit32.countlz
|
||||
local readu8, writeu8 = buffer.readu8, buffer.writeu8
|
||||
local floor, abs = math.floor, math.abs
|
||||
@@ -27,16 +24,15 @@ export type State = {
|
||||
mb: number,
|
||||
kb: number,
|
||||
channels: number,
|
||||
out: buffer, -- packet scratch
|
||||
pos: number, -- bit position in out
|
||||
cap: number, -- bits available
|
||||
out: buffer,
|
||||
pos: number,
|
||||
cap: number,
|
||||
u: { number },
|
||||
v: { number },
|
||||
err: { number },
|
||||
work: { number },
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ bit writer
|
||||
local function put(st: State, v: number, n: number)
|
||||
local pos = st.pos
|
||||
if pos + n > st.cap then
|
||||
@@ -66,7 +62,6 @@ local function putBig(st: State, v: number, n: number)
|
||||
end
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ rice code
|
||||
local function signExtend(v: number, bits: number): number
|
||||
local m = POW[bits + 1]
|
||||
v = v % m
|
||||
@@ -76,7 +71,6 @@ local function signExtend(v: number, bits: number): number
|
||||
return v
|
||||
end
|
||||
|
||||
-- the bits of one scalar (no writing)
|
||||
local function scalarBits(x: number, k: number, m: number, bps: number): number
|
||||
local pre = if k == 1 then x else floor(x / m)
|
||||
if pre >= 9 then
|
||||
@@ -96,7 +90,6 @@ local function scalar(st: State, x: number, k: number, m: number, bps: number)
|
||||
putBig(st, x, bps)
|
||||
return
|
||||
end
|
||||
-- pre ones and a zero
|
||||
put(st, POW[pre + 2] - 2, pre + 1)
|
||||
if k ~= 1 then
|
||||
local r = x - pre * m
|
||||
@@ -108,7 +101,6 @@ local function scalar(st: State, x: number, k: number, m: number, bps: number)
|
||||
end
|
||||
end
|
||||
|
||||
-- code the residuals e[1..n] (write = false: just count the bits)
|
||||
local function rice(st: State, e: { number }, n: number, bps: number, mult: number, write: boolean): number
|
||||
local history = st.mb
|
||||
local kb = st.kb
|
||||
@@ -162,7 +154,6 @@ local function rice(st: State, e: { number }, n: number, bps: number, mult: numb
|
||||
return bits
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ prediction
|
||||
-- residuals of x[1..n] under the decoder's adaptive FIR starting from coefs (adapted in place, like the decoder)
|
||||
local function residuals(x: { number }, e: { number }, n: number, bps: number, coefs: { number }, order: number, quant: number)
|
||||
e[1] = x[1]
|
||||
@@ -201,7 +192,6 @@ local function residuals(x: { number }, e: { number }, n: number, bps: number, c
|
||||
end
|
||||
end
|
||||
|
||||
-- LPC coefficients of x[1..n] for the given order (Levinson-Durbin on a Welch-windowed autocorrelation), quantised
|
||||
local function lpc(x: { number }, n: number, order: number, work: { number }): { number }
|
||||
local ac = table.create(order + 1, 0)
|
||||
for i = 1, n do
|
||||
@@ -248,7 +238,6 @@ local function lpc(x: { number }, n: number, order: number, work: { number }): {
|
||||
return coefs
|
||||
end
|
||||
|
||||
-- choose order / coefficients for one channel: returns order, coefs (as transmitted), bits
|
||||
local function analyse(st: State, x: { number }, n: number, bps: number): (number, { number }, number)
|
||||
local mult = floor(st.pb * PB_FACTOR / 4)
|
||||
local bestOrder, bestCoefs, bestBits = 0, {}, math.huge
|
||||
@@ -274,9 +263,6 @@ local function analyse(st: State, x: { number }, n: number, bps: number): (numbe
|
||||
return bestOrder, bestCoefs, bestBits
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ frame
|
||||
-- frame(st, samples, first, frames, map) -> buffer: samples interleaved (integers, WAV channel order) from index
|
||||
-- `first`; map[k] = the input channel (1-based) of ALAC channel k; elements: list of 1 / 2 channel counts
|
||||
function Encode.frame(st: State, samples: { number }, first: number, frames: number, map: { number }, elements: { number }): buffer
|
||||
local channels, depth = st.channels, st.bitDepth
|
||||
local n = frames
|
||||
@@ -299,7 +285,6 @@ function Encode.frame(st: State, samples: { number }, first: number, frames: num
|
||||
local bps = depth + nch - 1
|
||||
local mixRes = 0
|
||||
if nch == 2 then
|
||||
-- the mix with the smallest first-difference energy
|
||||
local best = math.huge
|
||||
for res = 0, 4 do
|
||||
local cost = 0
|
||||
@@ -334,7 +319,6 @@ function Encode.frame(st: State, samples: { number }, first: number, frames: num
|
||||
u[i] = samples[first + (i - 1) * channels + a - 1]
|
||||
end
|
||||
end
|
||||
-- header
|
||||
put(st, if nch == 2 then 1 else 0, 3)
|
||||
put(st, 0, 4)
|
||||
put(st, 0, 12)
|
||||
@@ -376,7 +360,6 @@ function Encode.frame(st: State, samples: { number }, first: number, frames: num
|
||||
end
|
||||
end
|
||||
if not compressed then
|
||||
-- uncompressed frame
|
||||
buffer.fill(st.out, 0, 0)
|
||||
st.pos = 0
|
||||
ch = 1
|
||||
|
||||
Reference in new issue
Block a user