codecs/audio/Aac: HE-AAC (SBR) and HE-AACv2 (baseline PS) decoding
New shared modules under codecs/audio/Aac: Sbr (bitstream, band tables, delta decoding), SbrBits, SbrQmf (32-band analysis / 64-band synthesis QMF through DCT-IV), SbrHf (HF generation and adjustment), Ps (baseline PS), SbrTables (ISO tables, from syom 0.8.1, MIT). Explicit signalling enables SBR at construction; implicit SBR / PS is found by Decoder:probe / Aac.probeFrames. audio/aac and audio/m4a report the output rate and take opts.sbr = false for the core only; m4a scales iTunSMPB by the media tick. Outside the folder: containers/AudioWrap writes an AAC-LC ADTS header for HE-AAC AudioSpecificConfigs (the decoder finds SBR in the stream); the api's default openAudio hook forwards opts.sbr.
This commit is contained in:
1 parent
627ddded77
commit
686a1fbf86
14 files changed
+2967
-35
No files matched your search
@@ -5,6 +5,7 @@
|
||||
-- Written from the standard; no third-party code. PNS uses FFmpeg's LCG so the noise matches it.
|
||||
local Mdct = require("@rosetta/codecs/audio/Mdct")
|
||||
local Tables = require("@rosetta/codecs/audio/Aac/Tables")
|
||||
local Sbr = require("@rosetta/codecs/audio/Aac/Sbr")
|
||||
|
||||
local readu32, byteswap, lshift, rshift, band = buffer.readu32, bit32.byteswap, bit32.lshift, bit32.rshift, bit32.band
|
||||
local floor, sqrt, abs = math.floor, math.sqrt, math.abs
|
||||
@@ -150,6 +151,14 @@ export type State = {
|
||||
extensions: number,
|
||||
sbr: boolean,
|
||||
map: { number }?,
|
||||
-- SBR output (codecs/audio/Aac/Sbr): sbrRate = the output rate when on, psOut = a mono stream output as stereo
|
||||
sbrOn: boolean,
|
||||
sbrRate: number,
|
||||
psOut: boolean,
|
||||
sbrElems: { any },
|
||||
elemKind: { number },
|
||||
elemFirst: { number },
|
||||
core: { { number } },
|
||||
}
|
||||
|
||||
local function newChannel(): Channel
|
||||
@@ -730,6 +739,50 @@ local function filterbank(st: State, c: Channel, out: { number }, stride: number
|
||||
c.prevShape = shape
|
||||
end
|
||||
|
||||
local function sbrElement(st: State, i: number, nch: number): any
|
||||
local e = st.sbrElems[i]
|
||||
if not e or e.nch ~= nch then
|
||||
e = Sbr.element(nch, st.sbrRate, st.psOut and nch == 1)
|
||||
st.sbrElems[i] = e
|
||||
end
|
||||
return e
|
||||
end
|
||||
|
||||
local OFFS = { 0, 0 }
|
||||
local CORE2 = { {}, {} }
|
||||
|
||||
-- every element through SBR at the doubled rate (2048 samples per channel); the PS output of a mono element is stereo
|
||||
local function sbrOutput(st: State, order: { Channel }, nElem: number): number
|
||||
local n = #order
|
||||
local ps = st.psOut and n == 1
|
||||
local stride = if ps then 2 else n
|
||||
local map = st.map
|
||||
local core = st.core
|
||||
for i, c in order do
|
||||
local t = core[i]
|
||||
if not t then
|
||||
t = table.create(1024, 0)
|
||||
core[i] = t
|
||||
end
|
||||
filterbank(st, c, t, 1, 1)
|
||||
end
|
||||
for ei = 1, nElem do
|
||||
local first = st.elemFirst[ei]
|
||||
local nch = if st.elemKind[ei] == 1 then 2 else 1
|
||||
local e = sbrElement(st, ei, nch)
|
||||
for j = 1, nch do
|
||||
local idx = first + j - 1
|
||||
OFFS[j] = if map and map[idx] then map[idx] else idx
|
||||
CORE2[j] = core[idx]
|
||||
end
|
||||
if ps then
|
||||
OFFS[2] = 2
|
||||
end
|
||||
Sbr.process(e, CORE2, st.pcm, stride, OFFS)
|
||||
end
|
||||
return stride
|
||||
end
|
||||
|
||||
function Decode.frame(st: State, buf: buffer, off: number, len: number): (number?, string?)
|
||||
if buffer.len(B) < len + 16 then
|
||||
B = buffer.create(len + 256)
|
||||
@@ -741,6 +794,8 @@ function Decode.frame(st: State, buf: buffer, off: number, len: number): (number
|
||||
local nextCh = 1
|
||||
local order: { Channel } = {}
|
||||
local lastElement = -1
|
||||
local nElem = 0
|
||||
local elemKind, elemFirst = st.elemKind, st.elemFirst
|
||||
while true do
|
||||
if BP + 3 > BL then
|
||||
break -- tolerate a missing END
|
||||
@@ -762,6 +817,8 @@ function Decode.frame(st: State, buf: buffer, off: number, len: number): (number
|
||||
end
|
||||
applyTns(st, c)
|
||||
table.insert(order, c)
|
||||
nElem += 1
|
||||
elemKind[nElem], elemFirst[nElem] = id, #order
|
||||
nextCh += 1
|
||||
lastElement = id
|
||||
elseif id == 1 then -- CPE
|
||||
@@ -818,6 +875,8 @@ function Decode.frame(st: State, buf: buffer, off: number, len: number): (number
|
||||
applyTns(st, r)
|
||||
table.insert(order, l)
|
||||
table.insert(order, r)
|
||||
nElem += 1
|
||||
elemKind[nElem], elemFirst[nElem] = id, #order - 1
|
||||
nextCh += 2
|
||||
lastElement = id
|
||||
elseif id == 2 then
|
||||
@@ -845,6 +904,9 @@ function Decode.frame(st: State, buf: buffer, off: number, len: number): (number
|
||||
local kind = getBits(4)
|
||||
if kind == 13 or kind == 14 then -- EXT_SBR_DATA / EXT_SBR_DATA_CRC
|
||||
st.sbr = true
|
||||
if st.sbrOn and nElem > 0 and lastElement ~= 3 then
|
||||
Sbr.extension(sbrElement(st, nElem, if lastElement == 1 then 2 else 1), B, start + 4, count * 8 - 4, kind == 14)
|
||||
end
|
||||
end
|
||||
st.extensions += 1
|
||||
local hook = st.onExtension
|
||||
@@ -865,6 +927,9 @@ function Decode.frame(st: State, buf: buffer, off: number, len: number): (number
|
||||
if n == 0 then
|
||||
return 0, nil
|
||||
end
|
||||
if st.sbrOn then
|
||||
return sbrOutput(st, order, nElem), nil
|
||||
end
|
||||
local pcm = st.pcm
|
||||
local map = st.map
|
||||
for i, c in order do
|
||||
@@ -880,7 +945,7 @@ function Decode.state(sfIndex: number): State
|
||||
channels = 0,
|
||||
frameLength = 1024,
|
||||
ch = {},
|
||||
pcm = table.create(1024 * 8, 0),
|
||||
pcm = table.create(2048 * 8, 0),
|
||||
random = 0x1f2e3d4c,
|
||||
time = table.create(2048, 0),
|
||||
frame = table.create(2048, 0),
|
||||
@@ -889,6 +954,13 @@ function Decode.state(sfIndex: number): State
|
||||
extensions = 0,
|
||||
sbr = false,
|
||||
map = nil,
|
||||
sbrOn = false,
|
||||
sbrRate = 0,
|
||||
psOut = false,
|
||||
sbrElems = {},
|
||||
elemKind = {},
|
||||
elemFirst = {},
|
||||
core = {},
|
||||
}
|
||||
end
|
||||
|
||||
|
||||
Reference in new issue
Block a user