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,10 +1,8 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- codecs/audio/Alac/Decode: the ALAC frame decoder: elements SCE / CPE / LFE / DSE / FIL / END, the adaptive Golomb-Rice
|
||||
-- residuals with zero runs, the adaptive FIR predictor (orders 0, 31 and 1 .. 30), stereo unmixing, shifted low bytes,
|
||||
-- uncompressed (escape) frames, 16 / 20 / 24 / 32-bit samples. Written from the ALAC format as published by Apple
|
||||
-- (ALACDecoder / ag_dec / dp_dec, Apache 2.0; behaviour, not code). Never throws: returns nil, message.
|
||||
-- codecs/audio/Alac/Decode: the ALAC frame decoder (SCE / CPE / LFE / DSE / FIL / END elements).
|
||||
-- Written from the ALAC format as published by Apple (ALACDecoder / ag_dec / dp_dec, Apache 2.0; behaviour, not code).
|
||||
local readu32, readu8 = buffer.readu32, buffer.readu8
|
||||
local byteswap, lshift, rshift, band, countlz, bnot = bit32.byteswap, bit32.lshift, bit32.rshift, bit32.band, bit32.countlz, bit32.bnot
|
||||
local floor = math.floor
|
||||
@@ -20,10 +18,10 @@ export type State = {
|
||||
channels: number,
|
||||
maxRun: number,
|
||||
scratch: buffer, -- the packet + 16 zero bytes (reads past the end are safe; the overrun is checked)
|
||||
pred: { { number } }, -- per element channel (0, 1): residuals / predictor output
|
||||
pred: { { number } },
|
||||
mix: { { number } },
|
||||
shift: { number },
|
||||
out: { number }, -- interleaved output samples (integers), frames * channels
|
||||
out: { number },
|
||||
}
|
||||
|
||||
local POW = table.create(64, 0)
|
||||
@@ -31,7 +29,6 @@ for i = 0, 63 do
|
||||
POW[i + 1] = 2 ^ i
|
||||
end
|
||||
|
||||
-- bit reader over st.scratch: pos = bit position. bits(n) for n <= 25.
|
||||
local function bitsAt(b: buffer, pos: number, n: number): number
|
||||
local w = byteswap(readu32(b, rshift(pos, 3)))
|
||||
return rshift(lshift(w, band(pos, 7)), 32 - n)
|
||||
@@ -72,7 +69,6 @@ local function scalar(b: buffer, pos: number, k: number, m: number, bps: number)
|
||||
return x, pos + k - 1
|
||||
end
|
||||
|
||||
-- residuals: n values into out[1..n]; returns the new bit position or -1
|
||||
local function rice(b: buffer, pos: number, limit: number, out: { number }, n: number, bps: number, mult: number, st: State): number
|
||||
local history = st.mb
|
||||
local kb = st.kb
|
||||
@@ -126,7 +122,6 @@ local function rice(b: buffer, pos: number, limit: number, out: { number }, n: n
|
||||
return pos
|
||||
end
|
||||
|
||||
-- the adaptive FIR predictor: err[1..n] -> out[1..n] (may be the same array); coefs adapt in place
|
||||
local function predict(err: { number }, out: { number }, n: number, bps: number, coefs: { number }, order: number, quant: number)
|
||||
out[1] = err[1]
|
||||
if n <= 1 then
|
||||
@@ -180,8 +175,6 @@ local function predict(err: { number }, out: { number }, n: number, bps: number,
|
||||
end
|
||||
end
|
||||
|
||||
-- decode(st, buf, off, len) -> frames | nil, message. Samples land interleaved in st.out (integers), channels in the
|
||||
-- ALAC element order.
|
||||
function Decode.frame(st: State, buf: buffer, off: number, len: number): (number?, string?)
|
||||
if len <= 0 then
|
||||
return nil, "empty packet"
|
||||
@@ -196,7 +189,7 @@ function Decode.frame(st: State, buf: buffer, off: number, len: number): (number
|
||||
local limit = len * 8
|
||||
local pos = 0
|
||||
local channels = st.channels
|
||||
local ch = 0 -- next output channel
|
||||
local ch = 0
|
||||
local frames = -1
|
||||
local out = st.out
|
||||
while true do
|
||||
@@ -331,7 +324,6 @@ function Decode.frame(st: State, buf: buffer, off: number, len: number): (number
|
||||
end
|
||||
shift = 0
|
||||
end
|
||||
-- unmix + shifted bytes -> interleaved output
|
||||
local sm = POW[shift + 1]
|
||||
if nch == 2 then
|
||||
local den = POW[mixBits + 1]
|
||||
@@ -394,7 +386,6 @@ function Decode.frame(st: State, buf: buffer, off: number, len: number): (number
|
||||
return 0, nil
|
||||
end
|
||||
if ch < channels then
|
||||
-- missing channels: silence
|
||||
for i = 1, frames do
|
||||
for c = ch + 1, channels do
|
||||
out[(i - 1) * channels + c] = 0
|
||||
@@ -404,7 +395,6 @@ function Decode.frame(st: State, buf: buffer, off: number, len: number): (number
|
||||
return frames, nil
|
||||
end
|
||||
|
||||
-- the scratch state for a configuration
|
||||
function Decode.state(cfg: any): State
|
||||
local n = cfg.frameLength
|
||||
return {
|
||||
|
||||
Reference in new issue
Block a user