Files
rosetta/src/Rosetta/codecs/audio/Aac/init.luau
T

318 lines
8.6 KiB
Luau

--!strict
-- codecs/audio/Aac: AAC-LC decoder (ISO/IEC 14496-3 subpart 4, ISO/IEC 13818-7): AudioSpecificConfig, ADTS, raw_data_block.
-- Written from the standard; no third-party code. HE-AAC decodes its AAC-LC core at the core rate. Not supported: Main
-- prediction, LTP, SSR, coupling channels, 960-sample frames, ER / USAC (UNSUPPORTED).
local Err = require("@rosetta/core/Err")
local Bits = require("@rosetta/core/Bits")
local Decode = require("@rosetta/codecs/audio/Aac/Decode")
local Aac = {}
Aac.RATES = { 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050, 16000, 12000, 11025, 8000, 7350 }
local CONFIG_CHANNELS = { 1, 2, 3, 4, 5, 6, 8 }
-- element order (AAC: C, L, R, ...) -> WAV order (L, R, C, LFE, ...) for channel configurations 3 .. 7
local CONFIG_MAP: { [number]: { number } } = {
[3] = { 3, 1, 2 },
[4] = { 3, 1, 2, 4 },
[5] = { 3, 1, 2, 4, 5 },
[6] = { 3, 1, 2, 5, 6, 4 },
[7] = { 3, 7, 8, 1, 2, 5, 6, 4 },
}
export type Config = {
objectType: number, -- 2 = AAC-LC (the core object type when SBR / PS is signalled)
sfIndex: number,
sampleRate: number,
channelConfig: number,
channels: number,
sbr: boolean,
ps: boolean,
extensionRate: number?,
frameLength: number,
}
local function rateIndex(rate: number): number
-- the nearest standard rate (ISO 14496-3 table 4.82 mapping for explicit frequencies)
local best, bi = math.huge, 4
for i, r in Aac.RATES do
if math.abs(r - rate) < best then
best, bi = math.abs(r - rate), i - 1
end
end
return bi
end
local function objectType(br: Bits.BitReader): number
local t = br:read(5)
if t == 31 then
t = 32 + br:read(6)
end
return t
end
local function frequency(br: Bits.BitReader): (number, number)
local i = br:read(4)
if i == 15 then
local f = br:read(24)
return rateIndex(f), f
end
return i, Aac.RATES[i + 1] or 0
end
-- program_config_element
local function pce(br: Bits.BitReader): number
br:read(4 + 2 + 4)
local front, side, back = br:read(4), br:read(4), br:read(4)
local lfe, assoc, cc = br:read(2), br:read(3), br:read(4)
if br:read(1) == 1 then
br:read(4)
end
if br:read(1) == 1 then
br:read(4)
end
if br:read(1) == 1 then
br:read(3)
end
local channels = 0
for _ = 1, front + side + back do
channels += if br:read(1) == 1 then 2 else 1
br:read(4)
end
channels += lfe
br:skip(lfe * 4 + assoc * 4 + cc * 5)
br:align()
br:skip(br:read(8) * 8)
return channels
end
function Aac.config(asc: buffer): (Config?, Err.Err?)
if type(asc) ~= "buffer" or buffer.len(asc) < 2 then
return nil, Err.new(Err.TRUNCATED, "AudioSpecificConfig is shorter than 2 bytes")
end
local br = Bits.msbReader(asc)
local aot = objectType(br)
local sfi, rate = frequency(br)
local chcfg = br:read(4)
local sbr, ps = false, false
local extRate: number? = nil
if aot == 5 or aot == 29 then
sbr = true
ps = aot == 29
local _, er = frequency(br)
extRate = er
aot = objectType(br)
if aot == 22 then
br:read(4)
end
end
if aot ~= 2 and aot ~= 1 and aot ~= 4 then
if aot == 0 or br.err then
return nil, Err.new(Err.CORRUPT, "bad AudioSpecificConfig")
end
return nil, Err.new(Err.UNSUPPORTED, "AAC object type " .. aot .. " (only AAC-LC)")
end
if aot ~= 2 then
return nil, Err.new(Err.UNSUPPORTED, if aot == 1 then "AAC Main" else "AAC LTP")
end
-- GASpecificConfig
local frameLengthFlag = br:read(1)
if br:read(1) == 1 then
br:read(14)
end
local ext = br:read(1)
local channels = CONFIG_CHANNELS[chcfg] or 0
if chcfg == 0 then
channels = pce(br)
elseif chcfg > 7 then
return nil, Err.new(Err.UNSUPPORTED, "channel configuration " .. chcfg)
end
if ext == 1 then
br:read(1) -- extensionFlag3
end
-- backward-compatible SBR signalling
if not sbr and br:bitsLeft() >= 16 then
if br:read(11) == 0x2b7 then
local et = objectType(br)
if et == 5 and br:read(1) == 1 then
sbr = true
local _, er = frequency(br)
extRate = er
if br:bitsLeft() >= 12 and br:read(11) == 0x548 then
ps = br:read(1) == 1
end
end
end
end
if br.err and channels == 0 then
return nil, br.err
end
if sfi > 12 or rate <= 0 then
return nil, Err.new(Err.CORRUPT, "bad sampling frequency index " .. sfi)
end
if frameLengthFlag == 1 then
return nil, Err.new(Err.UNSUPPORTED, "960-sample AAC frames")
end
if channels < 1 or channels > 8 then
return nil, Err.new(Err.UNSUPPORTED, channels .. " channels")
end
return {
objectType = aot,
sfIndex = sfi,
sampleRate = rate,
channelConfig = chcfg,
channels = channels,
sbr = sbr,
ps = ps,
extensionRate = extRate,
frameLength = 1024,
}, nil
end
function Aac.asc(aot: number, sfIndex: number, channelConfig: number): buffer
local b = buffer.create(2)
local v = aot * 2048 + sfIndex * 128 + channelConfig * 8
buffer.writeu8(b, 0, bit32.rshift(v, 8))
buffer.writeu8(b, 1, bit32.band(v, 255))
return b
end
export type Adts = {
headerSize: number, -- 7, or 9 with a CRC
frameSize: number,
objectType: number,
sfIndex: number,
sampleRate: number,
channelConfig: number,
blocks: number, -- raw_data_blocks in the frame (1 .. 4)
protected: boolean,
}
function Aac.adts(b: buffer, o: number, limit: number): Adts?
if o < 0 or o + 7 > limit or o + 7 > buffer.len(b) then
return nil
end
local b0, b1, b2, b3, b4, b5, b6 = buffer.readu8(b, o), buffer.readu8(b, o + 1), buffer.readu8(b, o + 2), buffer.readu8(b, o + 3), buffer.readu8(b, o + 4), buffer.readu8(b, o + 5), buffer.readu8(b, o + 6)
if b0 ~= 0xFF or bit32.band(b1, 0xF6) ~= 0xF0 then -- sync, layer 0
return nil
end
local protected = bit32.band(b1, 1) == 0
local profile = bit32.rshift(b2, 6)
local sfi = bit32.band(bit32.rshift(b2, 2), 15)
local ch = bit32.band(b2, 1) * 4 + bit32.rshift(b3, 6)
local size = bit32.band(b3, 3) * 2048 + b4 * 8 + bit32.rshift(b5, 5)
local blocks = bit32.band(b6, 3) + 1
local hs = if protected then 9 else 7
if sfi > 12 or size < hs then
return nil
end
return {
headerSize = hs,
frameSize = size,
objectType = profile + 1,
sfIndex = sfi,
sampleRate = Aac.RATES[sfi + 1],
channelConfig = ch,
blocks = blocks,
protected = protected,
}
end
export type Decoder = {
config: Config,
channels: number,
sampleRate: number,
frameLength: number,
pcm: { number },
sbr: boolean,
onExtension: ((kind: number, element: number, buf: buffer, bitPos: number, bits: number) -> ())?,
decode: (self: Decoder, buf: buffer, offset: number?, length: number?) -> (number?, Err.Err?),
reset: (self: Decoder) -> (),
state: Decode.State,
}
local function decode(self: Decoder, buf: buffer, offset: number?, length: number?): (number?, Err.Err?)
local o = offset or 0
local n = length or (buffer.len(buf) - o)
if type(buf) ~= "buffer" or o < 0 or n < 0 or o + n > buffer.len(buf) then
return nil, Err.new(Err.ARG, "packet range out of the buffer")
end
local st = self.state
st.onExtension = self.onExtension
local chans, msg = Decode.frame(st, buf, o, n)
if not chans then
return nil, Err.new(Err.CORRUPT, msg or "bad frame", o)
end
if st.sbr then
self.sbr = true
end
if chans == 0 then
for i = 1, 1024 * self.channels do
st.pcm[i] = 0
end
return 1024, nil
end
if chans ~= self.channels then
return nil, Err.new(Err.CORRUPT, string.format("frame has %d channels, the configuration %d", chans, self.channels), o)
end
return 1024, nil
end
local function reset(self: Decoder)
for _, c in self.state.ch do
for i = 1, 1024 do
c.overlap[i] = 0
end
c.prevShape = 0
end
end
function Aac.decoder(asc: any): (Decoder?, Err.Err?)
local cfg: Config
if type(asc) == "buffer" then
local c, err = Aac.config(asc)
if not c then
return nil, err
end
cfg = c
elseif type(asc) == "table" and type(asc.sfIndex) == "number" and type(asc.channels) == "number" then
local sfi = asc.sfIndex
if sfi < 0 or sfi > 12 or asc.channels < 1 or asc.channels > 8 then
return nil, Err.new(Err.ARG, "bad sampling index / channels")
end
if (asc.objectType or 2) ~= 2 then
return nil, Err.new(Err.UNSUPPORTED, "AAC object type " .. asc.objectType .. " (only AAC-LC)")
end
cfg = {
objectType = 2,
sfIndex = sfi,
sampleRate = Aac.RATES[sfi + 1],
channelConfig = asc.channelConfig or asc.channels,
channels = asc.channels,
sbr = false,
ps = false,
frameLength = 1024,
} :: Config
else
return nil, Err.new(Err.ARG, "decoder: expected an AudioSpecificConfig buffer")
end
local st = Decode.state(cfg.sfIndex)
st.channels = cfg.channels
if cfg.channelConfig >= 3 then
st.map = CONFIG_MAP[cfg.channelConfig]
end
return {
config = cfg,
channels = cfg.channels,
sampleRate = cfg.sampleRate,
frameLength = 1024,
pcm = st.pcm,
sbr = cfg.sbr,
onExtension = nil,
decode = decode,
reset = reset,
state = st,
}, nil
end
return Aac