codecs/audio: comments to specs only

This commit is contained in:
4DBug committed 2026-11-01 01:55:39 -05:00
1 parent 0bd7b87d2b
commit 6dcce481bb
69 files changed
+523 -1604

No files matched your search

+10 -29
View File
@@ -1,20 +1,7 @@
--!strict
-- codecs/audio/Aac: AAC-LC decoder (ISO/IEC 14496-3 subpart 4, ISO/IEC 13818-7): the AudioSpecificConfig (with a program
-- config element, explicit and backward-compatible SBR signalling), ADTS headers, and raw_data_block decoding
-- (codecs/audio/Aac/Decode). A codec (layer 1): audio/m4a, audio/aac (ADTS), and later MP4 / MKV / MPEG-TS video build on it.
-- Written from the standard (tables: codecs/audio/Aac/Tables); no third-party code.
-- Supports: AAC-LC (object type 2) 1 - 8 channels, all sampling rates, long / short / start / stop windows, sine and KBD
-- windows, M/S, intensity stereo, PNS, TNS, pulses. HE-AAC (SBR, PS: object types 5 / 29, or implicit SBR payloads)
-- decodes its AAC-LC core at the core rate; `decoder.onExtension` receives every FIL extension payload (the hook for a
-- later SBR / PS module), `decoder.sbr` says whether one was seen. Not supported: AAC Main prediction, LTP, SSR gain
-- control, coupling channels, 960-sample frames, ER / USAC object types (UNSUPPORTED).
--
-- local Aac = require("@rosetta/codecs/audio/Aac")
-- local dec, err = Aac.decoder(asc) -- an AudioSpecificConfig (buffer) or { objectType, sfIndex, channels }
-- local frames, err = dec:decode(buf, offset, length) -- one raw_data_block -> dec.pcm (frames * channels floats)
-- local h = Aac.adts(buf, offset, limit) -- an ADTS header: { headerSize, frameSize, objectType, sfIndex, ... }
--
-- Never throws: bad data returns nil, Err.
-- 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")
@@ -35,12 +22,12 @@ local CONFIG_MAP: { [number]: { number } } = {
export type Config = {
objectType: number, -- 2 = AAC-LC (the core object type when SBR / PS is signalled)
sfIndex: number,
sampleRate: number, -- the core rate
sampleRate: number,
channelConfig: number,
channels: number,
sbr: boolean, -- SBR signalled (explicitly or backward-compatibly)
sbr: boolean,
ps: boolean,
extensionRate: number?, -- the SBR output rate
extensionRate: number?,
frameLength: number,
}
@@ -72,7 +59,7 @@ local function frequency(br: Bits.BitReader): (number, number)
return i, Aac.RATES[i + 1] or 0
end
-- pce(br) -> channels (program_config_element)
-- 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)
@@ -98,7 +85,6 @@ local function pce(br: Bits.BitReader): number
return channels
end
-- config(asc) -> Config | nil, Err: parse an AudioSpecificConfig
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")
@@ -182,7 +168,6 @@ function Aac.config(asc: buffer): (Config?, Err.Err?)
}, nil
end
-- asc(objectType, sfIndex, channelConfig) -> a 2-byte AudioSpecificConfig
function Aac.asc(aot: number, sfIndex: number, channelConfig: number): buffer
local b = buffer.create(2)
local v = aot * 2048 + sfIndex * 128 + channelConfig * 8
@@ -193,7 +178,7 @@ end
export type Adts = {
headerSize: number, -- 7, or 9 with a CRC
frameSize: number, -- the whole frame, header included
frameSize: number,
objectType: number,
sfIndex: number,
sampleRate: number,
@@ -202,7 +187,6 @@ export type Adts = {
protected: boolean,
}
-- adts(buf, offset, limit) -> Adts | nil (not a valid header at offset)
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
@@ -233,14 +217,13 @@ function Aac.adts(b: buffer, o: number, limit: number): Adts?
}
end
-- ------------------------------------------------------------------------------------------------ decoder
export type Decoder = {
config: Config,
channels: number,
sampleRate: number,
frameLength: number,
pcm: { number }, -- the last frame: 1024 * channels interleaved floats (-1 .. 1), WAV channel order
sbr: boolean, -- SBR payloads seen in the stream (implicit signalling) or signalled
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) -> (),
@@ -263,7 +246,6 @@ local function decode(self: Decoder, buf: buffer, offset: number?, length: numbe
self.sbr = true
end
if chans == 0 then
-- an empty frame (only fill elements): silence
for i = 1, 1024 * self.channels do
st.pcm[i] = 0
end
@@ -284,7 +266,6 @@ local function reset(self: Decoder)
end
end
-- decoder(asc | { objectType?, sfIndex, channels }) -> Decoder | nil, Err
function Aac.decoder(asc: any): (Decoder?, Err.Err?)
local cfg: Config
if type(asc) == "buffer" then