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:
4DBug committed 2026-11-02 00:21:13 -06:00
1 parent 627ddded77
commit 686a1fbf86
14 files changed
+2967 -35

No files matched your search

+18 -10
View File
@@ -1,8 +1,8 @@
--!strict
--!optimize 2
-- audio/m4a: MPEG-4 audio (M4A / M4B / MP4 / MOV audio tracks) via containers/IsoBmff. Supports: AAC-LC (HE-AAC plays
-- its core), ALAC, edit-list gapless trimming, seeking, iTunes tags and cover art; encode ALAC (16 / 20 / 24-bit, 1-8
-- channels). Not supported: writing AAC, other codecs in MP4 (UNSUPPORTED), encrypted files.
-- audio/m4a: MPEG-4 audio (M4A / M4B / MP4 / MOV audio tracks) via containers/IsoBmff. Supports: AAC-LC, HE-AAC /
-- HE-AACv2 (explicit or implicit SBR / PS; opts.sbr = false keeps the AAC-LC core), ALAC, edit-list gapless trimming,
-- seeking, iTunes tags and cover art; encode ALAC (16 / 20 / 24-bit, 1-8 channels). Not supported: writing AAC, other codecs in MP4 (UNSUPPORTED), encrypted files.
-- ------------------------------------------------------------------------------------------------ requires
local Err = require("@rosetta/core/Err")
@@ -29,6 +29,7 @@ export type DecodeOptions = {
track: number?, -- the track id to play (default: the first AAC / ALAC track)
gapless: boolean?, -- apply the edit list (default true)
maxSamples: number?, -- default 2^25
sbr: boolean?, -- false: decode only the AAC-LC core of HE-AAC (half rate, faster); default true
}
export type EncodeOptions = {
bitDepth: number?, -- 16 (default), 20 or 24
@@ -63,9 +64,10 @@ type PacketDecoder = {
pcm: { number },
decode: (buf: buffer, off: number, len: number) -> (number?, Err.Err?),
reset: () -> (),
sbr: boolean?,
}
local function packetDecoder(t: IsoBmff.Track): (PacketDecoder?, Err.Err?)
local function packetDecoder(t: IsoBmff.Track, buf: buffer, sbr: boolean?): (PacketDecoder?, Err.Err?)
if not t.config then
return nil, Err.new(Err.CORRUPT, "track " .. t.id .. " (" .. t.codec .. ") has no codec configuration")
end
@@ -97,11 +99,14 @@ local function packetDecoder(t: IsoBmff.Track): (PacketDecoder?, Err.Err?)
}
return pd, nil
elseif t.codec == "aac" then
local dec, err = Aac.decoder(t.config :: buffer)
local dec, err = Aac.decoder(t.config :: buffer, { sbr = sbr })
if not dec then
return nil, err
end
local d = dec :: Aac.Decoder
if Aac.probeFrames(d, buf, t.offsets, t.sizes) then
d:reset()
end
local pd: PacketDecoder = {
channels = d.channels,
sampleRate = d.sampleRate,
@@ -113,6 +118,7 @@ local function packetDecoder(t: IsoBmff.Track): (PacketDecoder?, Err.Err?)
reset = function()
d:reset()
end,
sbr = d.sbrOutput,
}
return pd, nil
end
@@ -146,7 +152,7 @@ function M4a.open(r: Bytes.Reader, opts: DecodeOptions?): (Types.Decoder?, Err.E
return nil, Err.new(Err.CORRUPT, "no audio track")
end
local t = track :: IsoBmff.Track
local pd, perr = packetDecoder(t)
local pd, perr = packetDecoder(t, r.buf, o.sbr)
if not pd then
return nil, perr
end
@@ -171,9 +177,11 @@ function M4a.open(r: Bytes.Reader, opts: DecodeOptions?): (Types.Decoder?, Err.E
for w in string.gmatch(smpb, "%x+") do
table.insert(f, tonumber(w, 16) :: number)
end
if #f >= 4 and f[2] and f[4] and f[4] > 0 and f[2] + f[4] <= total then
skip = f[2]
total = f[2] + f[4]
-- in media ticks (the core rate of HE-AAC, as ffmpeg reads it)
local pre, len = math.floor((f[2] or 0) * tickScale + 0.5), math.floor((f[4] or 0) * tickScale + 0.5)
if #f >= 4 and len > 0 and pre + len <= total then
skip = pre
total = pre + len
end
end
local frames = math.max(0, total - skip)
@@ -210,7 +218,7 @@ function M4a.open(r: Bytes.Reader, opts: DecodeOptions?): (Types.Decoder?, Err.E
reset = p.reset,
skip = skip,
stop = total,
preroll = if t.codec == "aac" then 1 else 0,
preroll = if p.sbr then 3 elseif t.codec == "aac" then 1 else 0,
cumulative = true,
})
end