ws-m4a: codecs/Aac (AAC-LC decoder: ASC / PCE / ADTS, all tools, SBR/PS extension hook), codecs/Alac (decoder + encoder), audio/m4a (AAC + ALAC read, ALAC write, tags, gapless, seek), audio/aac (ADTS), audio/caf (lpcm/alac/aac/ulaw/alaw read, lpcm/alac write); fixtures checked by ffmpeg / fdkaac / MP4Box
Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
8b29568e0f
commit
f66594908c
22 files changed
+5380
No files matched your search
@@ -0,0 +1,427 @@
|
||||
--!strict
|
||||
--!native
|
||||
--!optimize 2
|
||||
-- codecs/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.
|
||||
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
|
||||
|
||||
local Decode = {}
|
||||
|
||||
export type State = {
|
||||
frameLength: number,
|
||||
bitDepth: number,
|
||||
pb: number,
|
||||
mb: number,
|
||||
kb: number,
|
||||
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
|
||||
mix: { { number } },
|
||||
shift: { number },
|
||||
out: { number }, -- interleaved output samples (integers), frames * channels
|
||||
}
|
||||
|
||||
local POW = table.create(64, 0)
|
||||
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)
|
||||
end
|
||||
|
||||
local function signExtend(v: number, bits: number): number
|
||||
local m = POW[bits + 1]
|
||||
v = v % m
|
||||
if v >= m / 2 then
|
||||
v -= m
|
||||
end
|
||||
return v
|
||||
end
|
||||
|
||||
-- the adaptive Golomb-Rice value: unary prefix (escape after 9 ones: `bps` raw bits), then k bits
|
||||
local function scalar(b: buffer, pos: number, k: number, m: number, bps: number): (number, number)
|
||||
local w = lshift(byteswap(readu32(b, rshift(pos, 3))), band(pos, 7))
|
||||
local pre = countlz(bnot(w))
|
||||
if pre >= 9 then
|
||||
pos += 9
|
||||
local v
|
||||
if bps > 25 then
|
||||
v = bitsAt(b, pos, bps - 16) * 65536 + bitsAt(b, pos + bps - 16, 16)
|
||||
else
|
||||
v = bitsAt(b, pos, bps)
|
||||
end
|
||||
return v, pos + bps
|
||||
end
|
||||
pos += pre + 1
|
||||
if k == 1 then
|
||||
return pre, pos
|
||||
end
|
||||
local v = bitsAt(b, pos, k)
|
||||
local x = pre * m
|
||||
if v > 1 then
|
||||
return x + v - 1, pos + k
|
||||
end
|
||||
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
|
||||
local signMod = 0
|
||||
local i = 1
|
||||
while i <= n do
|
||||
if pos > limit then
|
||||
return -1 -- each value reads at most 41 bits past here: inside the 16 padding bytes
|
||||
end
|
||||
local k = 31 - countlz(floor(history / 512) + 3)
|
||||
if k > kb then
|
||||
k = kb
|
||||
end
|
||||
local x
|
||||
x, pos = scalar(b, pos, k, POW[k + 1] - 1, bps)
|
||||
x += signMod
|
||||
signMod = 0
|
||||
out[i] = if x % 2 == 0 then x / 2 else -(x + 1) / 2
|
||||
if x > 0xFFFF then
|
||||
history = 0xFFFF
|
||||
else
|
||||
history += x * mult - floor(history * mult / 512)
|
||||
end
|
||||
if history < 128 and i < n then
|
||||
if pos > limit then
|
||||
return -1
|
||||
end
|
||||
local lg = if history > 0 then 31 - countlz(history) else 0
|
||||
k = 7 - lg + floor((history + 16) / 64)
|
||||
if k > kb then
|
||||
k = kb
|
||||
end
|
||||
local run
|
||||
run, pos = scalar(b, pos, k, POW[k + 1] - 1, 16)
|
||||
if run > 0 then
|
||||
if run > n - i then
|
||||
return -1
|
||||
end
|
||||
for j = i + 1, i + run do
|
||||
out[j] = 0
|
||||
end
|
||||
i += run
|
||||
end
|
||||
if run < 0xFFFF then
|
||||
signMod = 1 -- the next value is not 0 (unless the run hit its maximum)
|
||||
end
|
||||
history = 0
|
||||
end
|
||||
i += 1
|
||||
end
|
||||
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
|
||||
return
|
||||
end
|
||||
if order == 0 then
|
||||
if out ~= err then
|
||||
for i = 2, n do
|
||||
out[i] = err[i]
|
||||
end
|
||||
end
|
||||
return
|
||||
end
|
||||
if order == 31 then
|
||||
for i = 2, n do
|
||||
out[i] = signExtend(out[i - 1] + err[i], bps)
|
||||
end
|
||||
return
|
||||
end
|
||||
local i = 2
|
||||
while i <= order + 1 and i <= n do
|
||||
out[i] = signExtend(out[i - 1] + err[i], bps)
|
||||
i += 1
|
||||
end
|
||||
local half = if quant > 0 then POW[quant] else 0
|
||||
local qd = POW[quant + 1]
|
||||
-- coefs[1] multiplies the most recent sample
|
||||
while i <= n do
|
||||
local d = out[i - order - 1]
|
||||
local val = 0
|
||||
for j = 1, order do
|
||||
val += (out[i - j] - d) * coefs[j]
|
||||
end
|
||||
val = floor((val + half) / qd)
|
||||
local e = err[i]
|
||||
out[i] = signExtend(val + d + e, bps)
|
||||
if e ~= 0 then
|
||||
local es = if e > 0 then 1 else -1
|
||||
-- adapt from the oldest sample to the newest while the error keeps its sign
|
||||
for j = order, 1, -1 do
|
||||
if e * es <= 0 then
|
||||
break
|
||||
end
|
||||
local v = d - out[i - j]
|
||||
local sgn = if v > 0 then es elseif v < 0 then -es else 0
|
||||
coefs[j] -= sgn
|
||||
e -= floor(v * sgn / qd) * (order - j + 1)
|
||||
end
|
||||
end
|
||||
i += 1
|
||||
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"
|
||||
end
|
||||
local b = st.scratch
|
||||
if buffer.len(b) < len + 16 then
|
||||
b = buffer.create(len + 64)
|
||||
st.scratch = b
|
||||
end
|
||||
buffer.copy(b, 0, buf, off, len)
|
||||
buffer.fill(b, len, 0, 16)
|
||||
local limit = len * 8
|
||||
local pos = 0
|
||||
local channels = st.channels
|
||||
local ch = 0 -- next output channel
|
||||
local frames = -1
|
||||
local out = st.out
|
||||
while true do
|
||||
if pos + 3 > limit then
|
||||
return nil, "frame runs past the end"
|
||||
end
|
||||
local tag = bitsAt(b, pos, 3)
|
||||
pos += 3
|
||||
if tag == 7 then -- END
|
||||
break
|
||||
elseif tag == 0 or tag == 1 or tag == 3 then -- SCE / CPE / LFE
|
||||
local nch = if tag == 1 then 2 else 1
|
||||
if ch + nch > channels then
|
||||
return nil, "more channels than the configuration"
|
||||
end
|
||||
pos += 4 -- element instance tag
|
||||
if bitsAt(b, pos, 12) ~= 0 then
|
||||
return nil, "bad element header"
|
||||
end
|
||||
pos += 12
|
||||
local h = bitsAt(b, pos, 4)
|
||||
pos += 4
|
||||
local partial = rshift(h, 3)
|
||||
local shiftBytes = band(rshift(h, 1), 3)
|
||||
local escape = band(h, 1)
|
||||
if shiftBytes == 3 then
|
||||
return nil, "bad shift"
|
||||
end
|
||||
local n = st.frameLength
|
||||
if partial == 1 then
|
||||
n = bitsAt(b, pos, 16) * 65536 + bitsAt(b, pos + 16, 16)
|
||||
pos += 32
|
||||
end
|
||||
if n < 1 or n > st.frameLength then
|
||||
return nil, "bad frame length " .. n
|
||||
end
|
||||
if frames >= 0 and n ~= frames then
|
||||
return nil, "elements of different lengths"
|
||||
end
|
||||
frames = n
|
||||
if #out < n * channels then
|
||||
for k = #out + 1, n * channels do
|
||||
out[k] = 0
|
||||
end
|
||||
end
|
||||
local shift = shiftBytes * 8
|
||||
local bps = st.bitDepth - shift + nch - 1
|
||||
if bps > 32 or bps < 1 then
|
||||
return nil, "bad sample size"
|
||||
end
|
||||
if pos > limit then
|
||||
return nil, "frame runs past the end"
|
||||
end
|
||||
local mixBits, mixRes = 0, 0
|
||||
local p0, p1 = st.pred[1], st.pred[2]
|
||||
local m0, m1 = st.mix[1], st.mix[2]
|
||||
local shiftBuf = st.shift
|
||||
if escape == 0 then
|
||||
mixBits = bitsAt(b, pos, 8)
|
||||
mixRes = bitsAt(b, pos + 8, 8)
|
||||
pos += 16
|
||||
if mixRes >= 128 then
|
||||
mixRes -= 256
|
||||
end
|
||||
local mode, denShift, pbFactor, order = {}, {}, {}, {}
|
||||
local coefs = { {}, {} }
|
||||
for c = 1, nch do
|
||||
local x = bitsAt(b, pos, 8)
|
||||
local y = bitsAt(b, pos + 8, 8)
|
||||
pos += 16
|
||||
mode[c], denShift[c] = rshift(x, 4), band(x, 15)
|
||||
pbFactor[c], order[c] = rshift(y, 5), band(y, 31)
|
||||
if pos + order[c] * 16 > limit then
|
||||
return nil, "frame runs past the end"
|
||||
end
|
||||
for j = 1, order[c] do
|
||||
local v = bitsAt(b, pos, 16)
|
||||
pos += 16
|
||||
coefs[c][j] = if v >= 32768 then v - 65536 else v
|
||||
end
|
||||
end
|
||||
if pos > limit then
|
||||
return nil, "frame runs past the end"
|
||||
end
|
||||
local shiftPos = pos
|
||||
if shift > 0 then
|
||||
pos += shift * n * nch
|
||||
end
|
||||
for c = 1, nch do
|
||||
local p = if c == 1 then p0 else p1
|
||||
local mb = if c == 1 then m0 else m1
|
||||
pos = rice(b, pos, limit, p, n, bps, floor(st.pb * pbFactor[c] / 4), st)
|
||||
if pos < 0 or pos > limit then
|
||||
return nil, "bad residuals"
|
||||
end
|
||||
if mode[c] == 15 then
|
||||
predict(p, p, n, bps, {}, 31, 0)
|
||||
elseif mode[c] ~= 0 then
|
||||
return nil, "unknown prediction mode " .. mode[c]
|
||||
end
|
||||
predict(p, mb, n, bps, coefs[c], order[c], denShift[c])
|
||||
end
|
||||
if shift > 0 then
|
||||
local sp = shiftPos
|
||||
for i = 1, n * nch do
|
||||
shiftBuf[i] = bitsAt(b, sp, shift)
|
||||
sp += shift
|
||||
end
|
||||
end
|
||||
else
|
||||
-- uncompressed: interleaved samples of bitDepth bits
|
||||
local bits = st.bitDepth
|
||||
if pos + n * nch * bits > limit then
|
||||
return nil, "frame runs past the end"
|
||||
end
|
||||
for i = 1, n do
|
||||
for c = 1, nch do
|
||||
local v
|
||||
if bits > 25 then
|
||||
v = bitsAt(b, pos, bits - 16) * 65536 + bitsAt(b, pos + bits - 16, 16)
|
||||
else
|
||||
v = bitsAt(b, pos, bits)
|
||||
end
|
||||
pos += bits
|
||||
v = signExtend(v, bits)
|
||||
if c == 1 then
|
||||
m0[i] = v
|
||||
else
|
||||
m1[i] = v
|
||||
end
|
||||
end
|
||||
end
|
||||
shift = 0
|
||||
end
|
||||
-- unmix + shifted bytes -> interleaved output
|
||||
local sm = POW[shift + 1]
|
||||
if nch == 2 then
|
||||
local den = POW[mixBits + 1]
|
||||
for i = 1, n do
|
||||
local u, v = m0[i], m1[i]
|
||||
local l, r
|
||||
if mixRes ~= 0 then
|
||||
l = u + v - floor(mixRes * v / den)
|
||||
r = l - v
|
||||
else
|
||||
l, r = u, v
|
||||
end
|
||||
if shift > 0 then
|
||||
l = l * sm + shiftBuf[2 * i - 1]
|
||||
r = r * sm + shiftBuf[2 * i]
|
||||
end
|
||||
local o = (i - 1) * channels + ch + 1
|
||||
out[o] = l
|
||||
out[o + 1] = r
|
||||
end
|
||||
else
|
||||
for i = 1, n do
|
||||
local v = m0[i]
|
||||
if shift > 0 then
|
||||
v = v * sm + shiftBuf[i]
|
||||
end
|
||||
out[(i - 1) * channels + ch + 1] = v
|
||||
end
|
||||
end
|
||||
ch += nch
|
||||
elseif tag == 4 then -- DSE
|
||||
pos += 4
|
||||
local align = bitsAt(b, pos, 1)
|
||||
local count = bitsAt(b, pos + 1, 8)
|
||||
pos += 9
|
||||
if count == 255 then
|
||||
count += bitsAt(b, pos, 8)
|
||||
pos += 8
|
||||
end
|
||||
if align == 1 then
|
||||
pos = (pos + 7) // 8 * 8
|
||||
end
|
||||
pos += count * 8
|
||||
elseif tag == 6 then -- FIL
|
||||
local count = bitsAt(b, pos, 4)
|
||||
pos += 4
|
||||
if count == 15 then
|
||||
count += bitsAt(b, pos, 8) - 1
|
||||
pos += 8
|
||||
end
|
||||
pos += count * 8
|
||||
else
|
||||
return nil, "unsupported element " .. tag
|
||||
end
|
||||
if pos > limit then
|
||||
return nil, "frame runs past the end"
|
||||
end
|
||||
end
|
||||
if frames < 0 then
|
||||
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
|
||||
end
|
||||
end
|
||||
end
|
||||
return frames, nil
|
||||
end
|
||||
|
||||
-- the scratch state for a configuration
|
||||
function Decode.state(cfg: any): State
|
||||
local n = cfg.frameLength
|
||||
return {
|
||||
frameLength = n,
|
||||
bitDepth = cfg.bitDepth,
|
||||
pb = cfg.pb,
|
||||
mb = cfg.mb,
|
||||
kb = cfg.kb,
|
||||
channels = cfg.channels,
|
||||
maxRun = cfg.maxRun,
|
||||
scratch = buffer.create(math.max(64, cfg.maxFrameBytes or 0) + 64),
|
||||
pred = { table.create(n, 0), table.create(n, 0) },
|
||||
mix = { table.create(n, 0), table.create(n, 0) },
|
||||
shift = table.create(n * 2, 0),
|
||||
out = table.create(n * cfg.channels, 0),
|
||||
}
|
||||
end
|
||||
|
||||
local _ = readu8
|
||||
return Decode
|
||||
Reference in new issue
Block a user