codecs/audio/Aac/Ps: shared ring positions, symmetric hybrid filter, silent bands skipped

This commit is contained in:
4DBug committed 2026-11-02 00:36:01 -06:00
1 parent 95efdd7e3e
commit a081e7d437
2 files changed
+115 -71

No files matched your search

+114 -70
View File
@@ -57,27 +57,25 @@ local G0 = { 0.00746082949812, 0.02270420949825, 0.04546865930473, 0.07266113929
0.125, 0.11793710567217, 0.09885108575264, 0.07266113929591, 0.04546865930473, 0.02270420949825, 0.00746082949812 }
local G12 = { 0.0, 0.01899487526049, 0.0, -0.07293139167538, 0.0, 0.30596630545168, 0.5, 0.30596630545168, 0.0,
-0.07293139167538, 0.0, 0.01899487526049, 0.0 }
-- band 0, type A, Q = 8: H0[(q * 13 + m) * 2 + 1 / 2] = g[m] e^(i 2 pi / 8 (q + 1/2) (m - 6))
local H0R, H0I = table.create(8 * 13, 0), table.create(8 * 13, 0)
for q = 0, 7 do
for m = 0, 12 do
-- band 0, type A, Q = 8: A0C / A0S[q * 6 + m'] = g[m] cos / sin(2 pi / 8 (q + 1/2) (m - 6)) for m = m' - 1 = 0 .. 5
local A0C, A0S = table.create(4 * 6, 0), table.create(4 * 6, 0)
for q = 0, 3 do
for m = 0, 5 do
local a = 2 * pi / 8 * (q + 0.5) * (m - 6)
H0R[q * 13 + m + 1] = G0[m + 1] * cos(a)
H0I[q * 13 + m + 1] = G0[m + 1] * sin(a)
A0C[q * 6 + m + 1] = G0[m + 1] * cos(a)
A0S[q * 6 + m + 1] = G0[m + 1] * sin(a)
end
end
-- the hybrid band receiving each band-0 filter output (s4 = q2 + q5, s5 = q3 + q4)
local Q0_TO = { 2, 3, 4, 5, 5, 4, 0, 1 }
-- 8.6.4.5 decorrelator constants (20-band configuration)
local AP_A = { 0.65143905753106, 0.56471812200776, 0.48954165955695 }
local AP_D = { 3, 4, 5 }
local Q_FRACT = { 0.43, 0.75, 0.347 }
local F_CENTER = { -3 / 8, -1 / 8, 1 / 8, 3 / 8, 5 / 8, 7 / 8, 5 / 4, 7 / 4, 9 / 4, 11 / 4 }
local NR_AP = 30
local PHI_R, PHI_I = table.create(NR_AP, 0), table.create(NR_AP, 0)
local QF_R, QF_I = table.create(NR_AP * 3, 0), table.create(NR_AP * 3, 0)
local G_DECAY = table.create(NR_AP, 0)
local CO = table.create(NR_AP * 3, 0)
for k = 0, NR_AP - 1 do
local f = if k < 10 then F_CENTER[k + 1] else k + 0.5 - 7
PHI_R[k + 1], PHI_I[k + 1] = cos(-pi * 0.39 * f), sin(-pi * 0.39 * f)
@@ -85,6 +83,9 @@ for k = 0, NR_AP - 1 do
QF_R[k * 3 + m + 1], QF_I[k * 3 + m + 1] = cos(-pi * Q_FRACT[m + 1] * f), sin(-pi * Q_FRACT[m + 1] * f)
end
G_DECAY[k + 1] = if k > 10 then math.max(0, 1 - 0.05 * (k - 10)) else 1
for m = 0, 2 do
CO[k * 3 + m + 1] = AP_A[m + 1] * G_DECAY[k + 1]
end
end
local PEAK_DECAY, A_SMOOTH, GAMMA = 0.76592833836465, 0.25, 1.5
@@ -102,16 +103,16 @@ function Ps.state(): any
iccPrevN = 0,
hist = table.create(3 * 6 * 2, 0), -- hybrid input history: [(p * 6 + j) * 2 + 1 / 2]
apIn = table.create(NR_AP * 4, 0), -- z^-2 delay: in[n-1], in[n-2] (re, im)
apW = table.create(NR_AP * 3 * 5 * 2, 0), -- all-pass links: [((k * 3 + m) * 5 + pos) * 2 + 1 / 2]
apPos = table.create(NR_AP * 3, 0),
apW = table.create(NR_AP * 3 * 5 * 2, 0), -- all-pass links: [k * 30 + (m * 5 + pos) * 2 + 1 / 2]
apCnt = 0,
dl = table.create(41 * 14 * 2, 0), -- plain delays of hybrid bands 30 .. 70
dlPos = table.create(41, 0),
dlCnt = 0,
peak = table.create(20, 0),
smooth = table.create(20, 0),
smoothDiff = table.create(20, 0),
hPrev = table.create(20 * 4, 0),
prevHadPs = false,
active = false,
nbPrev = 71,
-- frame parameters
nEnv = 0,
border = table.create(5, 0),
@@ -348,14 +349,14 @@ end
-- pass through as hybrid bands 10 .. 70
local BR = table.create(44, 0)
local BI = table.create(44, 0)
local function hybrid(st: any, xr: { number }, xi: { number })
local function hybrid(st: any, xr: { number }, xi: { number }, nb: number)
local sr, si, hist = st.sr, st.si, st.hist
for n = 0, 31 do
local o, x = n * 71, n * 64
for k = 0, 9 do
sr[o + k + 1], si[o + k + 1] = 0, 0
end
for k = 3, 63 do
for k = 3, nb - 8 do
sr[o + k + 8], si[o + k + 8] = xr[x + k + 1], xi[x + k + 1]
end
end
@@ -367,20 +368,37 @@ local function hybrid(st: any, xr: { number }, xi: { number })
BR[j + 7], BI[j + 7] = xr[j * 64 + p + 1], xi[j * 64 + p + 1]
end
if p == 0 then
for q = 0, 7 do
local to = Q0_TO[q + 1] + 1
local hb = q * 13
for n = 0, 31 do
local ar, ai = 0, 0
for m = 0, 12 do
local cr, ci = H0R[hb + m + 1], H0I[hb + m + 1]
local vr, vi = BR[n + 13 - m], BI[n + 13 - m]
ar += cr * vr - ci * vi
ai += cr * vi + ci * vr
-- type A, Q = 8, symmetric g: y_q = A_q + i B_q, y_(7-q) = A_q - i B_q with A_q = sum_m g[m] cos(t (m - 6))
-- (x[m] + x[12 - m]), B_q = sum_m g[m] sin(t (m - 6)) (x[m] - x[12 - m]), t = 2 pi / 8 (q + 1/2)
for n = 0, 31 do
local o = n * 71
local cr, ci = BR[n + 7] * G0[7], BI[n + 7] * G0[7]
for q = 0, 3 do
local ar, ai, br, bi = cr, ci, 0, 0
local hb = q * 6
for m = 1, 6 do
-- buf[n + 12 - m'] with m' = m - 1 (the newer tap) and its mirror buf[n + m']
local xr, xi = BR[n + 14 - m], BI[n + 14 - m]
local yr, yi = BR[n + m], BI[n + m]
local c, sn = A0C[hb + m], A0S[hb + m]
ar += c * (xr + yr)
ai += c * (xi + yi)
br += sn * (xr - yr)
bi += sn * (xi - yi)
end
-- y_q = A + i B, y_(7-q) = A - i B
local q0r, q0i, q1r, q1i = ar - bi, ai + br, ar + bi, ai - br
if q == 0 then
sr[o + 3], si[o + 3] = q0r, q0i
sr[o + 2], si[o + 2] = q1r, q1i
elseif q == 1 then
sr[o + 4], si[o + 4] = q0r, q0i
sr[o + 1], si[o + 1] = q1r, q1i
elseif q == 2 then
sr[o + 5], si[o + 5] = q0r + q1r, q0i + q1i
else
sr[o + 6], si[o + 6] = q0r + q1r, q0i + q1i
end
local o = n * 71 + to
sr[o] += ar
si[o] += ai
end
end
else
@@ -410,16 +428,17 @@ local PW = table.create(20, 0)
local GR = table.create(20, 0)
-- 8.6.4.5 decorrelation of the hybrid rows
local function decorrelate(st: any)
local function decorrelate(st: any, nb: number)
local sr, si, dr, di = st.sr, st.si, st.dr, st.di
local peak, smooth, sdiff = st.peak, st.smooth, st.smoothDiff
local apIn, apW, apPos, dl, dlPos = st.apIn, st.apW, st.apPos, st.dl, st.dlPos
local apIn, apW, dl = st.apIn, st.apW, st.dl
local nAp = math.min(NR_AP, nb)
for n = 0, 31 do
local o = n * 71
for i = 1, 20 do
PW[i] = 0
end
for k = 1, 71 do
for k = 1, nb do
local r, im = sr[o + k], si[o + k]
local b = B_K[k] + 1
PW[b] += r * r + im * im
@@ -431,50 +450,54 @@ local function decorrelate(st: any)
pk = p
end
peak[i] = pk
smooth[i] += A_SMOOTH * (p - smooth[i])
sdiff[i] += A_SMOOTH * (pk - p - sdiff[i])
GR[i] = if GAMMA * sdiff[i] > smooth[i] then smooth[i] / (GAMMA * sdiff[i]) else 1
local sm = smooth[i] + A_SMOOTH * (p - smooth[i])
local sd = sdiff[i] + A_SMOOTH * (pk - p - sdiff[i])
smooth[i], sdiff[i] = sm, sd
GR[i] = if GAMMA * sd > sm then sm / (GAMMA * sd) else 1
end
for k = 0, NR_AP - 1 do
-- the all-pass links' ring positions (delays 3, 4, 5) and the plain delay's (14) are shared by every band
local cnt = st.apCnt
local w0, w1, w2 = (cnt % 3) * 2, (5 + cnt % 4) * 2, (10 + cnt % 5) * 2
st.apCnt = (cnt + 1) % 60
for k = 0, nAp - 1 do
local a4 = k * 4
local ur, ui = apIn[a4 + 3], apIn[a4 + 4]
apIn[a4 + 3], apIn[a4 + 4] = apIn[a4 + 1], apIn[a4 + 2]
apIn[a4 + 1], apIn[a4 + 2] = sr[o + k + 1], si[o + k + 1]
local pr, pi2 = PHI_R[k + 1], PHI_I[k + 1]
ur, ui = pr * ur - pi2 * ui, pr * ui + pi2 * ur
local g = G_DECAY[k + 1]
for m = 0, 2 do
local coef = AP_A[m + 1] * g
local qi = k * 3 + m + 1
local qr, qim = QF_R[qi], QF_I[qi]
local pos = apPos[qi]
local w = ((k * 3 + m) * 5 + pos) * 2
local wr, wi = apW[w + 1], apW[w + 2]
local tr, ti = qr * wr - qim * wi, qr * wi + qim * wr
local nr, ni = ur + tr * coef, ui + ti * coef
ur, ui = tr - nr * coef, ti - ni * coef
apW[w + 1], apW[w + 2] = nr, ni
pos += 1
if pos >= AP_D[m + 1] then
pos = 0
end
apPos[qi] = pos
end
local qb, wb = k * 3, k * 30
-- link m: w[n] = u[n] + a g Q w[n - d], v[n] = Q w[n - d] - a g w[n]
local coef, qr, qim = CO[qb + 1], QF_R[qb + 1], QF_I[qb + 1]
local w = wb + w0
local wr, wi = apW[w + 1], apW[w + 2]
local tr, ti = qr * wr - qim * wi, qr * wi + qim * wr
local nr, ni = ur + tr * coef, ui + ti * coef
ur, ui = tr - nr * coef, ti - ni * coef
apW[w + 1], apW[w + 2] = nr, ni
coef, qr, qim = CO[qb + 2], QF_R[qb + 2], QF_I[qb + 2]
w = wb + w1
wr, wi = apW[w + 1], apW[w + 2]
tr, ti = qr * wr - qim * wi, qr * wi + qim * wr
nr, ni = ur + tr * coef, ui + ti * coef
ur, ui = tr - nr * coef, ti - ni * coef
apW[w + 1], apW[w + 2] = nr, ni
coef, qr, qim = CO[qb + 3], QF_R[qb + 3], QF_I[qb + 3]
w = wb + w2
wr, wi = apW[w + 1], apW[w + 2]
tr, ti = qr * wr - qim * wi, qr * wi + qim * wr
nr, ni = ur + tr * coef, ui + ti * coef
ur, ui = tr - nr * coef, ti - ni * coef
apW[w + 1], apW[w + 2] = nr, ni
local gr = GR[B_K[k + 1] + 1]
dr[o + k + 1], di[o + k + 1] = ur * gr, ui * gr
end
for k = NR_AP, 70 do
local i = k - NR_AP
local len = if k < 42 then 14 else 1
local pos = dlPos[i + 1]
local w = (i * 14 + pos) * 2
local dc = st.dlCnt
st.dlCnt = (dc + 1) % 14
for k = NR_AP, nb - 1 do
local w = (k - NR_AP) * 28 + (if k < 42 then dc * 2 else 0)
local vr, vi = dl[w + 1], dl[w + 2]
dl[w + 1], dl[w + 2] = sr[o + k + 1], si[o + k + 1]
pos += 1
if pos >= len then
pos = 0
end
dlPos[i + 1] = pos
local gr = GR[B_K[k + 1] + 1]
dr[o + k + 1], di[o + k + 1] = vr * gr, vi * gr
end
@@ -487,28 +510,49 @@ local function clear(t: { number })
end
end
-- one frame: X (38 slots x 64 bands, [n * 64 + k + 1]) -> L / R QMF slots (32 x 64) in lr / li / rr / ri
function Ps.process(st: any, had: boolean, xr: { number }, xi: { number }, lr: { number }, li: { number }, rr: { number }, ri: { number })
-- one frame: X (38 slots x 64 bands, [n * 64 + k + 1], silent from QMF band top) -> L / R QMF slots (32 x 64)
function Ps.process(st: any, had: boolean, top: number, xr: { number }, xi: { number }, lr: { number }, li: { number }, rr: { number }, ri: { number })
if not st.prevHadPs then
clear(st.apIn)
clear(st.apW)
clear(st.apPos)
clear(st.dl)
clear(st.dlPos)
end
if not had then
st.nEnv = 0
end
-- hybrid bands above the SBR range (QMF band top and up) are silent: skipped, their delay lines cleared
local nb = math.clamp(top + 7, 10, 71)
if nb < st.nbPrev then
for k = nb, st.nbPrev - 1 do
if k < NR_AP then
for i = k * 4 + 1, k * 4 + 4 do
st.apIn[i] = 0
end
for i = k * 30 + 1, k * 30 + 30 do
st.apW[i] = 0
end
else
local d = k - NR_AP
for i = d * 28 + 1, d * 28 + 28 do
st.dl[i] = 0
end
end
end
end
st.nbPrev = nb
fillMatrices(st)
hybrid(st, xr, xi)
decorrelate(st)
hybrid(st, xr, xi, nb)
decorrelate(st, nb)
local sr, si, dr, di, hs = st.sr, st.si, st.dr, st.di, st.hs
for n = 0, 31 do
local o, x, hb = n * 71, n * 64, n * 80
local l0r, l0i, r0r, r0i = 0, 0, 0, 0
local l1r, l1i, r1r, r1i = 0, 0, 0, 0
local l2r, l2i, r2r, r2i = 0, 0, 0, 0
for k = 1, 71 do
for k = nb - 6, 64 do
lr[x + k], li[x + k], rr[x + k], ri[x + k] = 0, 0, 0, 0
end
for k = 1, nb do
local h = hb + B_K[k] * 4
local h11, h12, h21, h22 = hs[h + 1], hs[h + 2], hs[h + 3], hs[h + 4]
local s_r, s_i, d_r, d_i = sr[o + k], si[o + k], dr[o + k], di[o + k]
+1 -1
View File
@@ -1026,7 +1026,7 @@ function Sbr.process(e: any, core: { { number } }, out: { number }, stride: numb
end
local ps = e.ps
if ps.haveConfig then
Ps.process(ps, ok and e.psHad, XR, XI, LR, LI, RR, RI)
Ps.process(ps, ok and e.psHad, if ok then e.kx + e.M else 32, XR, XI, LR, LI, RR, RI)
local o2 = offs[2]
for l = 0, 31 do
SbrQmf.synthesis(q, l, LR, LI, l * 64, out, o + l * 64 * stride, stride)