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

+18 -40
View File
@@ -1,12 +1,8 @@
--!strict
--!native
--!optimize 2
-- codecs/audio/Aac/Decode: the AAC-LC raw_data_block decoder (ISO/IEC 14496-3 subpart 4): SCE / CPE / LFE / DSE / PCE /
-- FIL / END elements, ics_info with window grouping, section data, scalefactors (with noise energies and intensity
-- positions), pulse data, TNS, the eleven spectral codebooks with escapes, dequantisation, M/S and intensity stereo,
-- perceptual noise substitution (FFmpeg's LCG so the noise matches it), the filterbank (sine / KBD windows, the four
-- window sequences, IMDCT through codecs/audio/Mdct, overlap-add). FIL extension payloads (SBR / PS) are reported to a hook
-- and otherwise skipped. Written from the standard; no third-party code. Never throws: returns nil, message.
-- codecs/audio/Aac/Decode: the AAC-LC raw_data_block decoder (ISO/IEC 14496-3 subpart 4).
-- 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")
@@ -18,14 +14,13 @@ local Decode = {}
local ONLY_LONG, LONG_START, EIGHT_SHORT, LONG_STOP = 0, 1, 2, 3
local ZERO_HCB, ESC_HCB, NOISE_HCB, INTENSITY_HCB2, INTENSITY_HCB = 0, 11, 13, 14, 15
-- ------------------------------------------------------------------------------------------------ bit reader
-- one packet at a time: module-level cursor over a zero-padded copy (a frame decode never yields)
local B: buffer = buffer.create(16)
local BP = 0 -- bit position
local BL = 0 -- bit limit
local BP = 0
local BL = 0
local OVER = false
local function getBits(n: number): number -- 1 .. 25
local function getBits(n: number): number
local p = BP
BP = p + n
if p > BL then
@@ -49,7 +44,6 @@ local function getLong(n: number): number
return getBits(n)
end
-- ------------------------------------------------------------------------------------------------ Huffman
local PEEK = 10
type Book = { tab: { number }, map: { [number]: number }, maxLen: number }
@@ -78,14 +72,12 @@ for i = 1, 11 do
SPEC_BOOK[i] = book(Tables.SPEC_CODES[i], Tables.SPEC_BITS[i])
end
-- the next symbol of a book, -1 for an invalid code
local function huff(bk: Book): number
local e = bk.tab[peekBits(PEEK) + 1]
if e > 0 then
BP += band(e, 31)
return rshift(e, 5)
end
-- a code longer than PEEK bits
local code = getBits(PEEK)
local map = bk.map
for l = PEEK + 1, bk.maxLen do
@@ -98,7 +90,6 @@ local function huff(bk: Book): number
return -1
end
-- per codebook: dimension, unsigned, modulus, offset
local DIM = { 4, 4, 4, 4, 2, 2, 2, 2, 2, 2, 2 }
local UNSIGNED = { false, false, true, true, false, false, true, true, true, true, true }
local MOD = { 3, 3, 3, 3, 9, 9, 8, 8, 13, 13, 17 }
@@ -110,7 +101,6 @@ for i = 0, 8191 do
POW43[i + 1] = i ^ (4 / 3)
end
-- ------------------------------------------------------------------------------------------------ windows
local SINE_LONG = Mdct.window("sine", 2048) :: { number }
local KBD_LONG = Mdct.window("kbd", 2048, 4) :: { number }
local SINE_SHORT = Mdct.window("sine", 256) :: { number }
@@ -118,37 +108,34 @@ local KBD_SHORT = Mdct.window("kbd", 256, 6) :: { number }
local PLAN_LONG = Mdct.new(2048) :: Mdct.Plan
local PLAN_SHORT = Mdct.new(256) :: Mdct.Plan
-- ------------------------------------------------------------------------------------------------ state
export type Channel = {
spec: { number }, -- 1024 dequantised coefficients (8 x 128 for short windows)
overlap: { number }, -- 1024 samples carried to the next frame
spec: { number },
overlap: { number },
prevShape: number,
-- this frame's ics
windowSequence: number,
windowShape: number,
maxSfb: number,
numGroups: number,
groupLen: { number },
bandType: { number }, -- [g * maxSfb + sfb + 1]
bandType: { number },
sf: { number },
-- tns
tnsFilters: { { { number } } }, -- per window: { length, order, direction, coefs ... } flattened per filter
tnsFilters: { { { number } } },
}
export type State = {
sfIndex: number,
channels: number, -- configured channels (0: as found)
channels: number,
frameLength: number,
ch: { Channel },
pcm: { number }, -- interleaved output floats
random: number, -- PNS generator state
time: { number }, -- 2048 IMDCT scratch
frame: { number }, -- 2048 windowed frame scratch
short: { number }, -- 256 scratch
pcm: { number },
random: number,
time: { number },
frame: { number },
short: { number },
onExtension: ((kind: number, element: number, buf: buffer, bitPos: number, bits: number) -> ())?,
extensions: number, -- extension payloads seen (SBR / PS / ...)
sbr: boolean, -- an SBR payload was seen
map: { number }?, -- element channel order -> output channel (1-based)
extensions: number,
sbr: boolean,
map: { number }?,
}
local function newChannel(): Channel
@@ -168,7 +155,6 @@ local function newChannel(): Channel
end
Decode.newChannel = newChannel
-- ------------------------------------------------------------------------------------------------ ics
local function icsInfo(st: State, c: Channel): string?
if getBits(1) ~= 0 then
return "reserved ics bit set"
@@ -394,7 +380,6 @@ local function applyTns(st: State, c: Channel)
end
end
-- the spectral data of one channel, dequantised into c.spec (also pulses, noise, intensity zeros)
local function spectralData(st: State, c: Channel, pulses: { number }?): string?
local short = c.windowSequence == EIGHT_SHORT
local swb = if short then Tables.SWB_SHORT[st.sfIndex + 1] else Tables.SWB_LONG[st.sfIndex + 1]
@@ -530,7 +515,6 @@ local function spectralData(st: State, c: Channel, pulses: { number }?): string?
win += glen
end
if pulses then
-- long window: add the pulses to the quantised values, then dequantise every band
for i = 1, #pulses, 2 do
local pos = pulses[i] + 1
if pos <= 1024 then
@@ -609,7 +593,6 @@ local function ics(st: State, c: Channel, commonWindow: boolean): string?
return spectralData(st, c, pulses)
end
-- ------------------------------------------------------------------------------------------------ stereo
local function msStereo(st: State, l: Channel, r: Channel, mask: { number })
local short = l.windowSequence == EIGHT_SHORT
local swb = if short then Tables.SWB_SHORT[st.sfIndex + 1] else Tables.SWB_LONG[st.sfIndex + 1]
@@ -663,7 +646,6 @@ local function intensity(st: State, l: Channel, r: Channel, mask: { number }, ms
end
end
-- ------------------------------------------------------------------------------------------------ filterbank
local function filterbank(st: State, c: Channel, out: { number }, stride: number, offset: number)
local seq, shape, prev = c.windowSequence, c.windowShape, c.prevShape
local spec, overlap = c.spec, c.overlap
@@ -688,7 +670,6 @@ local function filterbank(st: State, c: Channel, out: { number }, stride: number
else
local t = st.time
Mdct.inverse(PLAN_LONG, spec, 1, 1, t, 1, 2 / 2048)
-- left half
if seq == LONG_STOP then
local rise = if prev == 1 then KBD_SHORT else SINE_SHORT
for n = 1, 448 do
@@ -706,7 +687,6 @@ local function filterbank(st: State, c: Channel, out: { number }, stride: number
frame[n] = t[n] * rise[n]
end
end
-- right half
if seq == LONG_START then
local fall = if shape == 1 then KBD_SHORT else SINE_SHORT
for n = 1025, 1472 do
@@ -734,8 +714,6 @@ local function filterbank(st: State, c: Channel, out: { number }, stride: number
c.prevShape = shape
end
-- ------------------------------------------------------------------------------------------------ frame
-- frame(st, buf, off, len) -> channels decoded | nil, message. Output: st.pcm, 1024 frames interleaved.
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)