119 lines
3.7 KiB
Luau
119 lines
3.7 KiB
Luau
--!strict
|
|
--!native
|
|
--!optimize 2
|
|
-- codecs/audio/Aac/SbrQmf: the SBR 32-band complex analysis and 64-band synthesis QMF banks (ISO/IEC 14496-3
|
|
-- 4.6.18.4.1 / 4.6.18.4.2), through DCT-IV factorisations of the modulation (codecs/audio/Aac/SbrDct). Written from
|
|
-- the standard; the window is codecs/audio/Aac/SbrTables.QMF_WINDOW.
|
|
-- analysis: W(k) = 2 e^(-i 3 pi (k + 1/2) / 128) (DCT4_32(u(m) - u(63 - m)) + i DST4_32(u(m) + u(63 - m)))(k)
|
|
-- synthesis: v(n) = -(DCT4_64(Re X) - DST4_64(Im X))(n) / 64, v(127 - n) = (DCT4_64(Re X) + DST4_64(Im X))(n) / 64
|
|
local SbrDct = require("@rosetta/codecs/audio/Aac/SbrDct")
|
|
local SbrTables = require("@rosetta/codecs/audio/Aac/SbrTables")
|
|
|
|
local SbrQmf = {}
|
|
|
|
local C = SbrTables.QMF_WINDOW
|
|
local dct32, ana32, dct64, syn64 = SbrDct.dct32, SbrDct.ana32, SbrDct.dct64, SbrDct.syn64
|
|
|
|
-- the core decoder's samples are scaled by 1 / 32768; the SBR energies assume 16-bit sample values
|
|
local IN_SCALE = 32768
|
|
|
|
-- analysis taps per output n: CA[n] = { c[2n + 128j] * IN_SCALE, j = 0 .. 4 }
|
|
local CA: { { number } } = table.create(64)
|
|
for n = 0, 63 do
|
|
local w = table.create(5, 0)
|
|
for j = 0, 4 do
|
|
w[j + 1] = C[2 * n + 128 * j + 1] * IN_SCALE
|
|
end
|
|
CA[n + 1] = w
|
|
end
|
|
-- synthesis taps per output k: CS[k] = { c[64i + k], i = 0 .. 9 }
|
|
local CS: { { number } } = table.create(64)
|
|
for k = 1, 64 do
|
|
local w = table.create(10, 0)
|
|
for i = 0, 9 do
|
|
w[i + 1] = C[64 * i + k]
|
|
end
|
|
CS[k] = w
|
|
end
|
|
|
|
local U = table.create(64, 0)
|
|
local A = table.create(32, 0)
|
|
local B = table.create(32, 0)
|
|
local CR = table.create(64, 0)
|
|
|
|
-- per-channel filterbank state
|
|
function SbrQmf.state(): any
|
|
return {
|
|
qa = table.create(320 + 1024, 0), -- analysis input: 320 history samples, then the frame
|
|
qs = table.create(41 * 128, 0), -- synthesis v blocks: 9 history blocks, then 32 for the frame
|
|
XLr = table.create(40 * 32, 0), -- X_low[col * 32 + k + 1], col 0..7 = the previous frame's 32..39
|
|
XLi = table.create(40 * 32, 0),
|
|
}
|
|
end
|
|
|
|
-- 32 analysis slots of one frame: 1024 core samples from inp[io + i * is] -> X_low columns 8 .. 39
|
|
function SbrQmf.analysis(s: any, inp: { number }, io: number, is: number)
|
|
local x: { number } = s.qa
|
|
local p = io
|
|
for i = 321, 1344 do
|
|
x[i] = inp[p]
|
|
p += is
|
|
end
|
|
local xr: { number }, xi: { number } = s.XLr, s.XLi
|
|
table.move(xr, 1025, 1280, 1)
|
|
table.move(xi, 1025, 1280, 1)
|
|
local u, a, b, cr = U, A, B, CR
|
|
for l = 0, 31 do
|
|
local t = 353 + 32 * l
|
|
for n = 1, 64 do
|
|
local q = t - n
|
|
local w = CA[n]
|
|
u[n] = x[q] * w[1] + x[q - 64] * w[2] + x[q - 128] * w[3] + x[q - 192] * w[4] + x[q - 256] * w[5]
|
|
end
|
|
for m = 1, 32 do
|
|
local u0, u1 = u[m], u[65 - m]
|
|
a[m] = u0 - u1
|
|
b[m] = u0 + u1
|
|
end
|
|
dct32(a, 0, cr)
|
|
ana32(b, 0, cr, xr, xi, (8 + l) * 32)
|
|
end
|
|
table.move(x, 1025, 1344, 1)
|
|
end
|
|
|
|
-- one synthesis slot: 64 complex subbands (xr / xi at [xo + k], k = 1 .. 64) -> 64 samples at out[o + j * os]
|
|
function SbrQmf.synthesis(s: any, slot: number, xr: { number }, xi: { number }, xo: number, out: { number }, o: number, os: number)
|
|
local v: { number } = s.qs
|
|
local base = (9 + slot) * 128
|
|
dct64(xr, xo, CR)
|
|
syn64(xi, xo, CR, v, base)
|
|
-- out(k) = sum_i v_(newest - 2i)[k] c[128i + k] + v_(newest - 2i - 1)[64 + k] c[128i + 64 + k]
|
|
local p = o
|
|
for k = 1, 64 do
|
|
local q = base + k
|
|
local w = CS[k]
|
|
out[p] = v[q] * w[1] + v[q - 64] * w[2] + v[q - 256] * w[3] + v[q - 320] * w[4] + v[q - 512] * w[5]
|
|
+ v[q - 576] * w[6] + v[q - 768] * w[7] + v[q - 832] * w[8] + v[q - 1024] * w[9] + v[q - 1088] * w[10]
|
|
p += os
|
|
end
|
|
end
|
|
|
|
function SbrQmf.endFrame(s: any)
|
|
local v: { number } = s.qs
|
|
table.move(v, 32 * 128 + 1, 41 * 128, 1)
|
|
end
|
|
|
|
function SbrQmf.reset(s: any)
|
|
for i = 1, 1344 do
|
|
s.qa[i] = 0
|
|
end
|
|
for i = 1, 41 * 128 do
|
|
s.qs[i] = 0
|
|
end
|
|
for i = 1, 1280 do
|
|
s.XLr[i], s.XLi[i] = 0, 0
|
|
end
|
|
end
|
|
|
|
return SbrQmf
|