Files
rosetta/src/Rosetta/codecs/audio/Ac3/Decode.luau
T

917 lines
24 KiB
Luau

--!strict
--!native
--!optimize 2
-- codecs/audio/Ac3/Decode: one AC-3 syncframe (6 audio blocks of 256 samples) -> PCM. Written from ATSC A/52:2018
-- (section 5.3 syntax, 7.1 exponents, 7.2 parametric bit allocation with delta bit allocation, 7.3 mantissas with
-- grouping and dither, 7.4 coupling with phase flags, 7.5 rematrixing, 7.7.1 dynamic range, 7.9 the 512 / 2 x 256
-- inverse transforms with block switching) on codecs/audio/Mdct; no third-party code. The bit reader is module state
-- over a frame the caller pads by PAD bytes; every section stops when the position passes the frame. No error /
-- assert: failures return nil, "message".
local T = require("@rosetta/codecs/audio/Ac3/Tables")
local Mdct = require("@rosetta/codecs/audio/Mdct")
local readu32, bswap, extract, band, rshift = buffer.readu32, bit32.byteswap, bit32.extract, bit32.band, bit32.rshift
local max, min = math.max, math.min
local Decode = {}
Decode.PAD = 2048
local CPL, LFE = 7, 6 -- channel slots: 1 .. 5 full bandwidth, 6 LFE, 7 the coupling channel
local NFCHANS = { [0] = 2, 1, 2, 3, 3, 4, 4, 5 }
local SLOWDEC, FASTDEC, SLOWGAIN, DBPBTAB, FLOORTAB, FASTGAIN = T.SLOWDEC, T.FASTDEC, T.SLOWGAIN, T.DBPBTAB, T.FLOORTAB, T.FASTGAIN
local BNDTAB, BNDSZ, MASKTAB, LATAB, HTH, BAPTAB = T.BNDTAB, T.BNDSZ, T.MASKTAB, T.LATAB, T.HTH, T.BAPTAB
-- 2^-e for the exponents 0 .. 24
local POW2 = table.create(32, 0)
for e = 0, 31 do
POW2[e + 1] = 2 ^ -e
end
-- symmetric quantiser values (Tables 7.19 - 7.23): Q1[code + 1] etc.
local function levels(n: number): { number }
local t = {}
for c = 0, n - 1 do
t[c + 1] = (2 * c - (n - 1)) / n
end
return t
end
local Q1, Q2, Q3, Q4, Q5 = levels(3), levels(5), levels(7), levels(11), levels(15)
-- bits of the asymmetric quantisers (bap 6 .. 15)
local QBITS = { [6] = 5, [7] = 6, [8] = 7, [9] = 8, [10] = 9, [11] = 10, [12] = 11, [13] = 12, [14] = 14, [15] = 16 }
local WINDOW = Mdct.window("kbd", 512, 5) :: { number }
local PLAN512 = Mdct.new(512) :: Mdct.Plan
local PLAN256 = Mdct.new(256) :: Mdct.Plan
-- ------------------------------------------------------------------------------------------------ bit reader
local B: buffer = buffer.create(0)
local P = 0
local function bits(n: number): number
if n <= 0 then
return 0
end
if n > 24 then
local hi = bits(n - 16)
return hi * 65536 + bits(16)
end
local w = bswap(readu32(B, rshift(P, 3)))
local v = extract(w, 32 - band(P, 7) - n, n)
P += n
return v
end
-- ------------------------------------------------------------------------------------------------ state
export type State = {
-- the frame header
fscod: number,
acmod: number,
lfeon: boolean,
nfchans: number,
channels: number, -- output channels
map: { number }, -- output index of slots 1 .. 6
drc: number, -- the dynamic range scale (0 = off)
-- per channel slot (1 .. 7)
exp: { { number } },
bap: { { number } },
coef: { { number } },
delay: { { number } },
endmant: { number },
chexpstr: { number },
blksw: { boolean },
dithflag: { boolean },
fsnroffst: { number },
fgaincod: { number },
dbaOn: { boolean },
dbaOffst: { { number } },
dbaLen: { { number } },
dbaBa: { { number } },
-- coupling
cplinu: boolean,
chincpl: { boolean },
phsflginu: boolean,
cplbegf: number,
cplendf: number,
cplbndstrc: { boolean }, -- [sbnd + 1], sbnd = 0 .. 17 (absolute sub-band)
cplco: { { number } }, -- [ch][sbnd + 1]
phsflg: { boolean }, -- [sbnd + 1]
cplfleak: number,
cplsleak: number,
rematflg: { boolean },
-- bit allocation parameters
sdcycod: number,
fdcycod: number,
sgaincod: number,
dbpbcod: number,
floorcod: number,
csnroffst: number,
dynrng: { number }, -- gain for Ch1 / Ch2
seed: number, -- dither generator
-- scratch
psd: { number },
bndpsd: { number },
excite: { number },
mask: { number },
y: { number },
pcm: { number },
blocks: { number }, -- the bit offset of each audio block in the last frame (for tools)
}
local function arrays(n: number, size: number, v: any): { any }
local t = {}
for i = 1, n do
t[i] = table.create(size, v)
end
return t
end
function Decode.state(): State
return {
fscod = 0,
acmod = 2,
lfeon = false,
nfchans = 2,
channels = 2,
map = { 1, 2, 3, 4, 5, 6 },
drc = 0,
exp = arrays(7, 264, 0),
bap = arrays(7, 264, 0),
coef = arrays(7, 256, 0),
delay = arrays(6, 256, 0),
endmant = table.create(7, 0),
chexpstr = table.create(7, 0),
blksw = table.create(7, false),
dithflag = table.create(7, false),
fsnroffst = table.create(7, 0),
fgaincod = table.create(7, 0),
dbaOn = table.create(7, false),
dbaOffst = arrays(7, 8, 0),
dbaLen = arrays(7, 8, 0),
dbaBa = arrays(7, 8, 0),
cplinu = false,
chincpl = table.create(7, false),
phsflginu = false,
cplbegf = 0,
cplendf = 0,
cplbndstrc = table.create(18, false),
cplco = arrays(5, 18, 0),
phsflg = table.create(18, false),
cplfleak = 0,
cplsleak = 0,
rematflg = table.create(4, false),
sdcycod = 0,
fdcycod = 0,
sgaincod = 0,
dbpbcod = 0,
floorcod = 0,
csnroffst = 0,
dynrng = { 1, 1 },
seed = 1,
psd = table.create(256, 0),
bndpsd = table.create(50, 0),
excite = table.create(50, 0),
mask = table.create(50, 0),
y = table.create(512, 0),
pcm = table.create(1536 * 2, 0),
blocks = table.create(6, 0),
}
end
-- reset(st): forget the overlap (before a seek)
function Decode.reset(st: State)
for _, d in st.delay do
for i = 1, 256 do
d[i] = 0
end
end
end
-- ------------------------------------------------------------------------------------------------ exponents
-- read ngrps grouped exponents (7 bits = 3 deltas each) after absexp into e[first + 1 ..] (bins first ..), grpsize
-- bins per delta; false when an exponent leaves 0 .. 24
local function exponents(e: { number }, absexp: number, ngrps: number, grpsize: number, first: number): boolean
local prev = absexp
local k = first + 1
for _ = 1, ngrps do
local g = bits(7)
if g >= 125 then
return false
end
local d1 = g // 25
local d2 = (g % 25) // 5
local d3 = g % 5
for m = 1, 3 do
local d = if m == 1 then d1 elseif m == 2 then d2 else d3
prev += d - 2
if prev < 0 or prev > 24 then
return false
end
for _ = 1, grpsize do
e[k] = prev
k += 1
end
end
end
return true
end
-- ------------------------------------------------------------------------------------------------ bit allocation
local function logadd(a: number, b: number): number
local c = a - b
local adr = min(rshift(if c >= 0 then c else -c, 1), 255)
return if c >= 0 then a + LATAB[adr + 1] else b + LATAB[adr + 1]
end
local function lowcomp(a: number, b0: number, b1: number, bin: number): number
if bin < 7 then
if b0 + 256 == b1 then
a = 384
elseif b0 > b1 then
a = max(0, a - 64)
end
elseif bin < 20 then
if b0 + 256 == b1 then
a = 320
elseif b0 > b1 then
a = max(0, a - 64)
end
else
a = max(0, a - 128)
end
return a
end
-- section 7.2.2: the bap of bins [start, stop) of channel slot ch
local function allocate(st: State, ch: number, start: number, stop: number, fgain: number, snroffset: number, isLfe: boolean)
local e, bap = st.exp[ch], st.bap[ch]
local psd, bndpsd, excite, mask = st.psd, st.bndpsd, st.excite, st.mask
local sdecay, fdecay = SLOWDEC[st.sdcycod + 1], FASTDEC[st.fdcycod + 1]
local sgain, dbknee, floor = SLOWGAIN[st.sgaincod + 1], DBPBTAB[st.dbpbcod + 1], FLOORTAB[st.floorcod + 1]
if floor >= 0x8000 then
floor -= 0x10000 -- 0xf800: a negative 16-bit value
end
for bin = start, stop - 1 do
psd[bin + 1] = 3072 - e[bin + 1] * 128
end
-- PSD integration
local j = start
local k = MASKTAB[start + 1]
local lastbin
repeat
lastbin = min(BNDTAB[k + 1] + BNDSZ[k + 1], stop)
local v = psd[j + 1]
j += 1
while j < lastbin do
v = logadd(v, psd[j + 1])
j += 1
end
bndpsd[k + 1] = v
k += 1
until stop <= lastbin
-- excitation
local bndstrt = MASKTAB[start + 1]
local bndend = MASKTAB[stop] + 1
local fastleak, slowleak = 0, 0
local begin
if bndstrt == 0 then
local lc = 0
lc = lowcomp(lc, bndpsd[1], bndpsd[2], 0)
excite[1] = bndpsd[1] - fgain - lc
lc = lowcomp(lc, bndpsd[2], bndpsd[3], 1)
excite[2] = bndpsd[2] - fgain - lc
begin = 7
for bin = 2, 6 do
local last = isLfe and bin == 6
if not last then
lc = lowcomp(lc, bndpsd[bin + 1], bndpsd[bin + 2], bin)
end
fastleak = bndpsd[bin + 1] - fgain
slowleak = bndpsd[bin + 1] - sgain
excite[bin + 1] = fastleak - lc
if not last and bndpsd[bin + 1] <= bndpsd[bin + 2] then
begin = bin + 1
break
end
end
for bin = begin, min(bndend, 22) - 1 do
if not (isLfe and bin == 6) then
lc = lowcomp(lc, bndpsd[bin + 1], bndpsd[bin + 2], bin)
end
fastleak = max(fastleak - fdecay, bndpsd[bin + 1] - fgain)
slowleak = max(slowleak - sdecay, bndpsd[bin + 1] - sgain)
excite[bin + 1] = max(fastleak - lc, slowleak)
end
begin = 22
else
begin = bndstrt
fastleak = st.cplfleak * 256 + 768
slowleak = st.cplsleak * 256 + 768
end
for bin = begin, bndend - 1 do
fastleak = max(fastleak - fdecay, bndpsd[bin + 1] - fgain)
slowleak = max(slowleak - sdecay, bndpsd[bin + 1] - sgain)
excite[bin + 1] = max(fastleak, slowleak)
end
-- masking curve
local hth = HTH[st.fscod + 1]
for bin = bndstrt, bndend - 1 do
local x = excite[bin + 1]
local p = bndpsd[bin + 1]
if p < dbknee then
x += (dbknee - p) // 4
end
mask[bin + 1] = max(x, hth[bin + 1])
end
-- delta bit allocation
if st.dbaOn[ch] then
local offs, lens, bas = st.dbaOffst[ch], st.dbaLen[ch], st.dbaBa[ch]
local b = 0
for seg = 1, #offs do
b += offs[seg]
local ba = bas[seg]
local delta = if ba >= 4 then (ba - 3) * 128 else (ba - 4) * 128
for _ = 1, lens[seg] do
if b < 50 then
mask[b + 1] += delta
end
b += 1
end
end
end
-- bit allocation pointers
local i = start
j = MASKTAB[start + 1]
repeat
lastbin = min(BNDTAB[j + 1] + BNDSZ[j + 1], stop)
local m = mask[j + 1] - snroffset - floor
if m < 0 then
m = 0
end
m = band(m, 0x1fe0) + floor
while i < lastbin do
local adr = (psd[i + 1] - m) // 32
bap[i + 1] = BAPTAB[min(63, max(0, adr)) + 1]
i += 1
end
j += 1
until stop <= lastbin
end
-- ------------------------------------------------------------------------------------------------ mantissas
-- grouped mantissa state (bap 1, 2, 4 share groups across channels within a block)
local G1, G2, G4 = table.create(3, 0), table.create(3, 0), table.create(2, 0)
local n1, n2, n4 = 0, 0, 0
local function dither(st: State): number
local s = (st.seed * 1664525 + 1013904223) % 4294967296
st.seed = s
return (s / 2147483648 - 1) * 0.707
end
-- the mantissas of bins [start, stop) of slot ch into out[], scaled by their exponents (bap 0: dither when asked)
local function mantissas(st: State, ch: number, start: number, stop: number, out: { number }, dith: boolean)
local e, bap = st.exp[ch], st.bap[ch]
for bin = start, stop - 1 do
local b = bap[bin + 1]
local v
if b == 0 then
v = if dith then dither(st) else 0
elseif b == 1 then
if n1 == 0 then
local g = bits(5)
if g > 26 then
g = 26
end
G1[1], G1[2], G1[3] = Q1[g // 9 + 1], Q1[(g % 9) // 3 + 1], Q1[g % 3 + 1]
n1 = 3
end
v = G1[4 - n1]
n1 -= 1
elseif b == 2 then
if n2 == 0 then
local g = bits(7)
if g > 124 then
g = 124
end
G2[1], G2[2], G2[3] = Q2[g // 25 + 1], Q2[(g % 25) // 5 + 1], Q2[g % 5 + 1]
n2 = 3
end
v = G2[4 - n2]
n2 -= 1
elseif b == 3 then
v = Q3[min(bits(3), 6) + 1]
elseif b == 4 then
if n4 == 0 then
local g = bits(7)
if g > 120 then
g = 120
end
G4[1], G4[2] = Q4[g // 11 + 1], Q4[g % 11 + 1]
n4 = 2
end
v = G4[3 - n4]
n4 -= 1
elseif b == 5 then
v = Q5[min(bits(4), 14) + 1]
else
local nb = QBITS[b]
local m = bits(nb)
local half = 2 ^ (nb - 1)
if m >= half then
m -= 2 * half
end
v = m / half
end
out[bin + 1] = v * POW2[e[bin + 1] + 1]
end
end
-- ------------------------------------------------------------------------------------------------ audio block
local function readDba(st: State, ch: number)
local nseg = bits(3) + 1
local offs, lens, bas = st.dbaOffst[ch], st.dbaLen[ch], st.dbaBa[ch]
table.clear(offs)
table.clear(lens)
table.clear(bas)
for seg = 1, nseg do
offs[seg] = bits(5)
lens[seg] = bits(4)
bas[seg] = bits(3)
end
end
local EXPGRP = { 3, 6, 12 } -- bins per group of 3 deltas, by strategy (D15 / D25 / D45)
local function audblk(st: State, blk: number, limit: number): (boolean?, string?)
local nf, acmod = st.nfchans, st.acmod
for ch = 1, nf do
st.blksw[ch] = bits(1) == 1
end
for ch = 1, nf do
st.dithflag[ch] = bits(1) == 1
end
for i = 1, if acmod == 0 then 2 else 1 do
if bits(1) == 1 then
local v = bits(8)
local x = rshift(v, 5)
if x >= 4 then
x -= 8
end
local g = 2 ^ (x + 1) * (32 + band(v, 31)) / 64
st.dynrng[i] = if st.drc == 1 then g else g ^ st.drc
elseif blk == 0 then
st.dynrng[i] = 1
end
end
-- coupling strategy
if bits(1) == 1 then
st.cplinu = bits(1) == 1
if st.cplinu then
if acmod < 2 then
return nil, "coupling in a mono / dual mono stream"
end
for ch = 1, nf do
st.chincpl[ch] = bits(1) == 1
end
st.phsflginu = if acmod == 2 then bits(1) == 1 else false
st.cplbegf = bits(4)
st.cplendf = bits(4)
if st.cplendf + 3 <= st.cplbegf then
return nil, "bad coupling range"
end
st.cplbndstrc[st.cplbegf + 1] = false
for sbnd = st.cplbegf + 1, st.cplendf + 2 do
st.cplbndstrc[sbnd + 1] = bits(1) == 1
end
else
for ch = 1, nf do
st.chincpl[ch] = false
end
st.phsflginu = false
end
elseif blk == 0 then
return nil, "no coupling strategy in block 0"
end
local cplinu = st.cplinu
local cplbegf, cplendf = st.cplbegf, st.cplendf
local cplstrt = 37 + 12 * cplbegf
local cplend = 37 + 12 * (cplendf + 3)
-- coupling coordinates (per sub-band, duplicated over each coupling band)
if cplinu then
local anyNew = false
for ch = 1, nf do
if st.chincpl[ch] then
if bits(1) == 1 then
anyNew = true
local mstr = bits(2) * 3
local co = st.cplco[ch]
local v = 0
for sbnd = cplbegf, cplendf + 2 do
if not st.cplbndstrc[sbnd + 1] then
local ex, mant = bits(4), bits(4)
local m = if ex == 15 then mant / 16 else (mant + 16) / 32
v = m * 2 ^ -(ex + mstr)
end
co[sbnd + 1] = v
end
end
end
end
if acmod == 2 and st.phsflginu and anyNew then
local f = false
for sbnd = cplbegf, cplendf + 2 do
if not st.cplbndstrc[sbnd + 1] then
f = bits(1) == 1
end
st.phsflg[sbnd + 1] = f
end
end
end
-- rematrixing
if acmod == 2 then
if bits(1) == 1 then
local n = if not cplinu or cplbegf > 2 then 4 elseif cplbegf > 0 then 3 else 2
for r = 1, 4 do
st.rematflg[r] = if r <= n then bits(1) == 1 else false
end
elseif blk == 0 then
for r = 1, 4 do
st.rematflg[r] = false
end
end
end
-- exponent strategies
local chexpstr = st.chexpstr
chexpstr[CPL] = if cplinu then bits(2) else 0
for ch = 1, nf do
chexpstr[ch] = bits(2)
end
chexpstr[LFE] = if st.lfeon then bits(1) else 0
if blk == 0 then
for ch = 1, nf do
if chexpstr[ch] == 0 then
return nil, "exponent reuse in block 0"
end
end
if (cplinu and chexpstr[CPL] == 0) or (st.lfeon and chexpstr[LFE] == 0) then
return nil, "exponent reuse in block 0"
end
end
local endmant = st.endmant
for ch = 1, nf do
if chexpstr[ch] ~= 0 then
if st.chincpl[ch] then
endmant[ch] = cplstrt
else
local bw = bits(6)
if bw > 60 then
return nil, "bad channel bandwidth code"
end
endmant[ch] = 37 + 3 * (bw + 12)
end
end
end
endmant[LFE] = 7
-- exponents
if cplinu and chexpstr[CPL] ~= 0 then
local abs = bits(4) * 2
local grp = EXPGRP[chexpstr[CPL]]
if not exponents(st.exp[CPL], abs, (cplend - cplstrt) // grp, grp // 3, cplstrt) then
return nil, "bad coupling exponents"
end
end
for ch = 1, nf do
local s = chexpstr[ch]
if s ~= 0 then
local grp = EXPGRP[s]
local ngrps
if s == 1 then
ngrps = (endmant[ch] - 1) // 3
elseif s == 2 then
ngrps = (endmant[ch] - 1 + 3) // 6
else
ngrps = (endmant[ch] - 1 + 9) // 12
end
local e = st.exp[ch]
e[1] = bits(4)
if not exponents(e, e[1], ngrps, grp // 3, 1) then
return nil, "bad exponents"
end
bits(2) -- gainrng
end
end
if st.lfeon and chexpstr[LFE] ~= 0 then
local e = st.exp[LFE]
e[1] = bits(4)
if not exponents(e, e[1], 2, 1, 1) then
return nil, "bad LFE exponents"
end
end
if P > limit then
return nil, "truncated block"
end
-- bit allocation parameters
if bits(1) == 1 then
st.sdcycod, st.fdcycod, st.sgaincod, st.dbpbcod, st.floorcod = bits(2), bits(2), bits(2), bits(2), bits(3)
elseif blk == 0 then
return nil, "no bit allocation parameters in block 0"
end
if bits(1) == 1 then
st.csnroffst = bits(6)
if cplinu then
st.fsnroffst[CPL], st.fgaincod[CPL] = bits(4), bits(3)
end
for ch = 1, nf do
st.fsnroffst[ch], st.fgaincod[ch] = bits(4), bits(3)
end
if st.lfeon then
st.fsnroffst[LFE], st.fgaincod[LFE] = bits(4), bits(3)
end
elseif blk == 0 then
return nil, "no SNR offsets in block 0"
end
if cplinu then
if bits(1) == 1 then
st.cplfleak, st.cplsleak = bits(3), bits(3)
end
end
-- delta bit allocation
if bits(1) == 1 then
local modes = table.create(7, 0)
if cplinu then
modes[CPL] = bits(2)
end
for ch = 1, nf do
modes[ch] = bits(2)
end
if cplinu and modes[CPL] == 1 then
readDba(st, CPL)
end
for ch = 1, nf do
if modes[ch] == 1 then
readDba(st, ch)
end
end
for _, ch in { 1, 2, 3, 4, 5, CPL } do
if modes[ch] == 1 then
st.dbaOn[ch] = true
elseif modes[ch] == 2 or modes[ch] == 3 then
st.dbaOn[ch] = false
end
end
elseif blk == 0 then
for ch = 1, 7 do
st.dbaOn[ch] = false
end
end
-- skip field
if bits(1) == 1 then
P += bits(9) * 8
end
if P > limit then
return nil, "truncated block"
end
-- bit allocation
local zero = st.csnroffst == 0
if zero then
for ch = 1, nf do
if st.fsnroffst[ch] ~= 0 then
zero = false
end
end
if (cplinu and st.fsnroffst[CPL] ~= 0) or (st.lfeon and st.fsnroffst[LFE] ~= 0) then
zero = false
end
end
local function alloc(ch: number, start: number, stop: number, isLfe: boolean)
if zero then
for i = start + 1, stop do
st.bap[ch][i] = 0
end
else
local snr = ((st.csnroffst - 15) * 16 + st.fsnroffst[ch]) * 4
allocate(st, ch, start, stop, FASTGAIN[st.fgaincod[ch] + 1], snr, isLfe)
end
end
if cplinu then
alloc(CPL, cplstrt, cplend, false)
end
for ch = 1, nf do
alloc(ch, 0, endmant[ch], false)
end
if st.lfeon then
alloc(LFE, 0, 7, true)
end
-- mantissas
n1, n2, n4 = 0, 0, 0
local gotCpl = false
local coef = st.coef
for ch = 1, nf do
local c = coef[ch]
mantissas(st, ch, 0, endmant[ch], c, st.dithflag[ch])
for i = endmant[ch] + 1, 256 do
c[i] = 0
end
if cplinu and st.chincpl[ch] and not gotCpl then
mantissas(st, CPL, cplstrt, cplend, coef[CPL], false)
gotCpl = true
end
if P > limit then
return nil, "truncated block"
end
end
if st.lfeon then
local c = coef[LFE]
mantissas(st, LFE, 0, 7, c, false)
for i = 8, 256 do
c[i] = 0
end
end
if P > limit then
return nil, "truncated block"
end
-- decoupling (section 7.4): dither the coupling channel's zero-bit mantissas per channel
if cplinu then
local cc, cbap, cexp = coef[CPL], st.bap[CPL], st.exp[CPL]
for ch = 1, nf do
if st.chincpl[ch] then
local c, co = coef[ch], st.cplco[ch]
local dith = st.dithflag[ch]
local flip = acmod == 2 and ch == 2 and st.phsflginu
for sbnd = cplbegf, cplendf + 2 do
local f = co[sbnd + 1] * 8
if flip and st.phsflg[sbnd + 1] then
f = -f
end
for k = 37 + 12 * sbnd + 1, 37 + 12 * sbnd + 12 do
local v = cc[k]
if cbap[k] == 0 then
v = if dith then dither(st) * POW2[cexp[k] + 1] else 0
end
c[k] = v * f
end
end
for k = cplend + 1, 256 do
c[k] = 0
end
end
end
end
-- rematrixing (section 7.5)
if acmod == 2 then
local stop = if cplinu then cplstrt else min(endmant[1], endmant[2])
local l, r = coef[1], coef[2]
local edges = { 13, 25, 37, 61, 253 }
for rb = 1, 4 do
if st.rematflg[rb] then
for k = edges[rb] + 1, min(edges[rb + 1], stop) do
local a, b = l[k], r[k]
l[k], r[k] = a + b, a - b
end
end
end
end
return true, nil
end
-- ------------------------------------------------------------------------------------------------ transform
-- the inverse transform of slot ch (long or two short), windowed, overlap-added into out[base + i * step]
local function synth(st: State, ch: number, short: boolean, gain: number, out: { number }, base: number, step: number)
local x, y, d, w = st.coef[ch], st.y, st.delay[ch], WINDOW
if short then
-- two 256-point transforms of the interleaved coefficients, phases alpha = -1 (n0 = 0) and +1 (n0 = N / 2)
local z = st.psd -- 256 scratch
Mdct.inverse(PLAN256, x, 1, 2, z, 1, -2 * gain)
for n = 0, 63 do
y[n + 1] = -z[n + 193]
end
for n = 64, 255 do
y[n + 1] = z[n - 63]
end
Mdct.inverse(PLAN256, x, 2, 2, z, 1, -2 * gain)
for n = 0, 191 do
y[257 + n] = z[n + 65]
end
for n = 192, 255 do
y[257 + n] = -z[n - 191]
end
else
Mdct.inverse(PLAN512, x, 1, 1, y, 1, -2 * gain)
end
local p = base
for n = 1, 256 do
out[p] = y[n] * w[n] + d[n]
d[n] = y[n + 256] * w[n + 256]
p += step
end
end
-- ------------------------------------------------------------------------------------------------ frame
-- the output index of each slot (1 .. 5 full bandwidth, 6 LFE) in WAVE order: L R C LFE surrounds
local ORDER: { [number]: { number } } = {
[0] = { 1, 2 },
{ 1 },
{ 1, 2 },
{ 1, 3, 2 },
{ 1, 2, 3 },
{ 1, 3, 2, 4 },
{ 1, 2, 3, 4 },
{ 1, 3, 2, 4, 5 },
}
local HAS_C = { [0] = false, true, false, true, false, true, false, true }
-- frame(st, buf, len, drc) -> 1536 | nil, message: decodes the syncframe at buf[0 .. len) (padded by PAD bytes)
-- into st.pcm (1536 * st.channels interleaved floats, WAVE order). drc: the dynamic range scale (0 = off, 1 = full).
function Decode.frame(st: State, buf: buffer, len: number, drc: number): (number?, string?)
if len < 8 or buffer.len(buf) < len + Decode.PAD then
return nil, "truncated frame"
end
B, P = buf, 32
local fscod = bits(2)
bits(6)
local bsid = bits(5)
if bsid > 8 then
B = buffer.create(0)
return nil, "unsupported: bsid " .. bsid .. (if bsid > 10 then " (E-AC-3)" else " (reduced sample rate)")
end
if fscod == 3 then
B = buffer.create(0)
return nil, "bad sample rate code"
end
bits(3) -- bsmod
local acmod = bits(3)
if band(acmod, 1) == 1 and acmod ~= 1 then
bits(2)
end
if band(acmod, 4) ~= 0 then
bits(2)
end
if acmod == 2 then
bits(2)
end
local lfeon = bits(1) == 1
for _ = 1, if acmod == 0 then 2 else 1 do
bits(5)
if bits(1) == 1 then
bits(8)
end
if bits(1) == 1 then
bits(8)
end
if bits(1) == 1 then
bits(7)
end
end
bits(2)
if bits(1) == 1 then
bits(14)
end
if bits(1) == 1 then
bits(14)
end
if bits(1) == 1 then
P += (bits(6) + 1) * 8
end
local nf = NFCHANS[acmod]
local channels = nf + (if lfeon then 1 else 0)
if st.acmod ~= acmod or st.lfeon ~= lfeon then
-- a new channel layout: build the output map and forget the overlap
local order = ORDER[acmod]
local map = table.create(6, 0)
local nfront = if HAS_C[acmod] then 3 elseif acmod == 1 then 1 else 2
for ch = 1, nf do
local o = order[ch]
map[ch] = if lfeon and o > nfront then o + 1 else o
end
if lfeon then
map[LFE] = nfront + 1
end
st.map = map
st.pcm = table.create(1536 * channels, 0)
Decode.reset(st)
end
st.fscod, st.acmod, st.lfeon, st.nfchans, st.channels, st.drc = fscod, acmod, lfeon, nf, channels, drc
local pcm = st.pcm
local limit = len * 8 - 16 -- crc2
for blk = 0, 5 do
st.blocks[blk + 1] = P
local ok, err = audblk(st, blk, limit)
if not ok then
B = buffer.create(0)
return nil, err
end
local base = blk * 256 * channels
for ch = 1, nf do
local g = st.dynrng[if acmod == 0 and ch == 2 then 2 else 1]
synth(st, ch, st.blksw[ch], g, pcm, base + st.map[ch], channels)
end
if lfeon then
synth(st, LFE, false, st.dynrng[1], pcm, base + st.map[LFE], channels)
end
end
B = buffer.create(0)
return 1536, nil
end
return Decode