audio perf: AAC codeword unpack tables + fused long-window overlap-add, MDCT inverse branch-free unfold, FFT last-pass loops

This commit is contained in:
4DBug committed 2026-11-01 19:22:33 -06:00
1 parent 59c2438ca1
commit 4ea42f1375
3 files changed
+90 -37

No files matched your search

+30 -14
View File
@@ -94,6 +94,20 @@ local DIM = { 4, 4, 4, 4, 2, 2, 2, 2, 2, 2, 2 }
local UNSIGNED = { false, false, true, true, false, false, true, true, true, true, true }
local MOD = { 3, 3, 3, 3, 9, 9, 8, 8, 13, 13, 17 }
local OFF = { 1, 1, 0, 0, 4, 4, 0, 0, 0, 0, 0 }
-- UNPACK[cb][4 sym + k]: a codeword's dim values (k = 1 .. dim), first value most significant
local UNPACK: { { number } } = {}
for cb = 1, 11 do
local dim, mod, off = DIM[cb], MOD[cb], OFF[cb]
local t = table.create(mod ^ dim * 4, 0)
for sym = 0, mod ^ dim - 1 do
local v = sym
for k = dim, 1, -1 do
t[sym * 4 + k] = v % mod - off
v = v // mod
end
end
UNPACK[cb] = t
end
-- |q|^(4/3)
local POW43 = table.create(8192, 0)
@@ -421,7 +435,7 @@ local function spectralData(st: State, c: Channel, pulses: { number }?): string?
continue
end
local bk = SPEC_BOOK[cb]
local dim, uns, mod, off = DIM[cb], UNSIGNED[cb], MOD[cb], OFF[cb]
local dim, uns, unpack = DIM[cb], UNSIGNED[cb], UNPACK[cb]
local gain = 2 ^ (0.25 * (sf[idx] - 100))
for w = 0, glen - 1 do
local base = (win + w) * 128 + lo
@@ -432,19 +446,8 @@ local function spectralData(st: State, c: Channel, pulses: { number }?): string?
return "bad spectral code"
end
-- unpack the symbol into dim values (first value most significant)
local vals1: number, vals2: number, vals3: number, vals4: number
if dim == 4 then
vals4 = sym % mod - off
sym = (sym - (sym % mod)) / mod
vals3 = sym % mod - off
sym = (sym - (sym % mod)) / mod
vals2 = sym % mod - off
sym = (sym - (sym % mod)) / mod
vals1 = sym % mod - off
else
vals2 = sym % mod - off
vals1 = (sym - (sym % mod)) / mod - off
end
local u = sym * 4
local vals1, vals2, vals3, vals4 = unpack[u + 1], unpack[u + 2], unpack[u + 3], unpack[u + 4]
if uns then
if vals1 ~= 0 and getBits(1) == 1 then
vals1 = -vals1
@@ -670,6 +673,19 @@ local function filterbank(st: State, c: Channel, out: { number }, stride: number
else
local t = st.time
Mdct.inverse(PLAN_LONG, spec, 1, 1, t, 1, 2 / 2048)
if seq ~= LONG_STOP and seq ~= LONG_START then
-- ONLY_LONG: window, overlap-add and output in one pass
local rise = if prev == 1 then KBD_LONG else SINE_LONG
local fall = if shape == 1 then KBD_LONG else SINE_LONG
local p = offset
for n = 1, 1024 do
out[p] = (t[n] * rise[n] + overlap[n]) * scale
overlap[n] = t[1024 + n] * fall[1024 + n]
p += stride
end
c.prevShape = shape
return
end
if seq == LONG_STOP then
local rise = if prev == 1 then KBD_SHORT else SINE_SHORT
for n = 1, 448 do
+28 -1
View File
@@ -82,7 +82,34 @@ end
-- one pass of radix p: cc (i, v, k) = src[1 + i + ido * (v + p * k)] -> ch (i, k, u) = dst[1 + i + ido * (k + l1 * u)]
local function pass(p: number, ido: number, l1: number, ar: { number }, ai: { number }, br: { number }, bi: { number }, tr: { number }, ti: { number }, cr: { number }, ci: { number })
local lp = ido * l1
if p == 4 then
if ido == 1 and p == 4 then -- the last pass: no twiddles, s = 4k + 1, d = k + 1
local lp2, lp3 = 2 * lp, 3 * lp
local s = 1
for d = 1, l1 do
local a0r, a0i = ar[s], ai[s]
local a1r, a1i = ar[s + 1], ai[s + 1]
local a2r, a2i = ar[s + 2], ai[s + 2]
local a3r, a3i = ar[s + 3], ai[s + 3]
local t0r, t0i = a0r + a2r, a0i + a2i
local t1r, t1i = a0r - a2r, a0i - a2i
local t2r, t2i = a1r + a3r, a1i + a3i
local t3r, t3i = a1r - a3r, a1i - a3i
br[d], bi[d] = t0r + t2r, t0i + t2i
br[d + lp], bi[d + lp] = t1r + t3i, t1i - t3r
br[d + lp2], bi[d + lp2] = t0r - t2r, t0i - t2i
br[d + lp3], bi[d + lp3] = t1r - t3i, t1i + t3r
s += 4
end
elseif ido == 1 and p == 2 then
local s = 1
for d = 1, l1 do
local a0r, a0i = ar[s], ai[s]
local a1r, a1i = ar[s + 1], ai[s + 1]
br[d], bi[d] = a0r + a1r, a0i + a1i
br[d + lp], bi[d + lp] = a0r - a1r, a0i - a1i
s += 2
end
elseif p == 4 then
local ido2, ido3, lp2, lp3 = 2 * ido, 3 * ido, 2 * lp, 3 * lp
for k = 0, l1 - 1 do
local s = 1 + ido * 4 * k
+32 -22
View File
@@ -113,37 +113,47 @@ function Mdct.inverse(plan: Plan, inp: { number }, ii: number, is: number?, y: {
local re, im, twc, tws = plan.re, plan.im, plan.twc, plan.tws
local us = is or 1
local sc = scale or 1
for p = 0, q - 1 do
local a, b = inp[ii + 2 * p * us], inp[ii + (m - 1 - 2 * p) * us]
local c, s = twc[p + 1], tws[p + 1]
re[p + 1] = a * c + b * s
im[p + 1] = b * c - a * s
local ia, ib, step = ii, ii + (m - 1) * us, 2 * us -- inp[ii + 2p us], inp[ii + (m - 1 - 2p) us]
for p = 1, q do
local a, b = inp[ia], inp[ib]
local c, s = twc[p], tws[p]
re[p] = a * c + b * s
im[p] = b * c - a * s
ia += step
ib -= step
end
Fft.forward(plan.fft, re, im)
-- the DCT-IV output C[j] (j = 2k and M - 1 - 2k) unfolded into y = (C2, -C2 reversed, -C1 reversed, -C1):
-- y[3h - 1 - j] = -C[j]; y[3h + j] = -C[j] (j < h), y[j - h] = C[j] (j >= h)
local b3 = yi + 3 * h
for k = 0, q - 1 do
local c, s = twc[k + 1], tws[k + 1]
local tr, ti = re[k + 1], im[k + 1]
-- 2k < h exactly when M - 1 - 2k >= h (h = q)
local half = (q + 1) // 2
local yh = yi - h
for k = 0, half - 1 do
local k1 = k + 1
local c, s = twc[k1], tws[k1]
local tr, ti = re[k1], im[k1]
local e = (tr * c + ti * s) * sc -- C[2k]
local o = -(ti * c - tr * s) * sc -- C[M - 1 - 2k]
local j = 2 * k
if j < h then
y[b3 - 1 - j] = -e
y[b3 + j] = -e
else
y[j - h + yi] = e
y[b3 - 1 - j] = -e
end
y[b3 - 1 - j] = -e
y[b3 + j] = -e
j = m - 1 - j
if j < h then
y[b3 - 1 - j] = -o
y[b3 + j] = -o
else
y[j - h + yi] = o
y[b3 - 1 - j] = -o
end
y[j + yh] = o
y[b3 - 1 - j] = -o
end
for k = half, q - 1 do
local k1 = k + 1
local c, s = twc[k1], tws[k1]
local tr, ti = re[k1], im[k1]
local e = (tr * c + ti * s) * sc
local o = -(ti * c - tr * s) * sc
local j = 2 * k
y[j + yh] = e
y[b3 - 1 - j] = -e
j = m - 1 - j
y[b3 - 1 - j] = -o
y[b3 + j] = -o
end
end