codecs/audio: comments to specs only
This commit is contained in:
1 parent
0bd7b87d2b
commit
6dcce481bb
69 files changed
+523
-1604
No files matched your search
@@ -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)
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
--!strict
|
||||
-- codecs/audio/Aac/Tables: the AAC-LC tables of ISO/IEC 14496-3 (subpart 4, Annex 4.A): the scalefactor and the eleven
|
||||
-- spectral Huffman codebooks (code, length per symbol), the scalefactor band offsets per sampling rate (1024 and 128
|
||||
-- lines) and the TNS band limits. Transcribed from FFmpeg libavcodec/aactab.c (tables of the standard, no code;
|
||||
-- see licenses/NOTICE.md). Generated; do not edit.
|
||||
-- codecs/audio/Aac/Tables: the AAC-LC tables of ISO/IEC 14496-3 (subpart 4, Annex 4.A): Huffman codebooks, scalefactor
|
||||
-- band offsets, TNS band limits. Transcribed from FFmpeg libavcodec/aactab.c (tables of the standard, no code; see
|
||||
-- licenses/NOTICE.md). Generated; do not edit.
|
||||
local Tables = {}
|
||||
|
||||
Tables.SF_CODES = {
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in new issue
Block a user