Documentation
¶
Overview ¶
Package dec implements the AAC-LC decoder, ported from the fixed-point flavor of FFmpeg's AAC decoder (libavcodec/aac/ @ d09d5afc3a). Decoder phase D0 covers the bitstream layer: ADTS and AudioSpecificConfig parsing, the raw_data_block element loop, and symbol-level decoding of section data, scalefactors, TNS/pulse side info and quantized spectral coefficients. Dequantization, the IMDCT and the tool synthesis arrive in later phases.
Index ¶
Constants ¶
const ( TypeSCE = iota TypeCPE TypeCCE TypeLFE TypeDSE TypePCE TypeFIL TypeEnd )
Raw data block element types. Mirror enum RawDataBlockType (libavcodec/aac.h @ d09d5afc3a).
const ( ZeroBT = 0 EscBT = 11 ReservedBT = 12 NoiseBT = 13 IntensityBT = 15 )
Band types. Mirror enum BandType (libavcodec/aac.h @ d09d5afc3a).
const ( OnlyLongSequence = iota LongStartSequence EightShortSequence LongStopSequence )
Window sequences. Mirror enum WindowSequence (libavcodec/aac.h @ d09d5afc3a).
const ADTSHeaderSize = adtsHeaderSize
ADTSHeaderSize is the fixed ADTS header length in bytes (AV_AAC_ADTS_HEADER_SIZE). The streaming framer peeks this many bytes to read aac_frame_length before pulling the whole frame.
Variables ¶
var ( // ErrSync reports a missing ADTS syncword. Mirrors // AAC_PARSE_ERROR_SYNC (libavcodec/adts_header.h @ d09d5afc3a). ErrSync = errors.New("dec: ADTS syncword not found") // ErrInvalidData reports a malformed bitstream. Mirrors // AVERROR_INVALIDDATA returns of the C decoder. ErrInvalidData = errors.New("dec: invalid data") // ErrUnsupported reports well-formed input the decoder does not // support yet (non-LC object types, channel configs > 2, PCE, CCE, // 960-sample frames, mid-stream config changes). ErrUnsupported = errors.New("dec: unsupported") // ErrUnsupportedSBR reports HE-AAC (AAC-LC + SBR): the stream signals SBR // in the AudioSpecificConfig or a leading fill element. It wraps // ErrUnsupported, and ErrUnsupportedPS wraps it in turn, so a caller can // test errors.Is(err, ErrUnsupportedSBR) to catch the whole HE-AAC family // (SBR and SBR+PS) and fall back to an external decoder deliberately. ErrUnsupportedSBR = fmt.Errorf("%w: SBR (HE-AAC)", ErrUnsupported) // ErrUnsupportedPS reports HE-AACv2 (AAC-LC + SBR + PS). Because PS implies // SBR it wraps ErrUnsupportedSBR (and thus ErrUnsupported); test // ErrUnsupportedPS to distinguish HE-AACv2 from plain HE-AAC. PS is // detected only when signalled in the ASC; an ADTS HE-AACv2 stream whose // SBR is found in a fill element surfaces as ErrUnsupportedSBR. ErrUnsupportedPS = fmt.Errorf("%w + PS (HE-AACv2)", ErrUnsupportedSBR) )
Sentinel errors. DecodeFrame wraps them with context via fmt.Errorf %w.
Functions ¶
func FindSync ¶
FindSync returns the offset of the next plausible ADTS frame at or after pos: a 12-bit syncword whose header parses. Returns -1 if none is found. Mirrors the resynchronization scan of the ADTS parser layer.
This is a single-header scanner over a whole in-memory slice, used only by tests that split trusted, committed corpus streams where every 0xFFF is a real frame boundary. It deliberately does NOT chain-validate a candidate against the following header: the production streaming resync that must reject a false 0xFFF inside payload on a lossy transport lives in pcm.(*Decoder).syncADTS and its confirmChain helper, which have the bufio Peek/EOF context this slice scanner does not. Keep the two separate; do not port the hardening here without a non-test caller that needs it.
Types ¶
type ADTSHeader ¶
type ADTSHeader struct {
ObjectType int // profile_objecttype + 1
ChanConfig int
CRCAbsent int
NumAACFrames int
SamplingIndex int
SampleRate int
Samples int
FrameLength int
}
ADTSHeader carries the parsed fixed and variable ADTS header fields. Mirrors AACADTSHeaderInfo (libavcodec/adts_header.h @ d09d5afc3a).
func ParseADTS ¶
func ParseADTS(r *bits.Reader) (ADTSHeader, error)
ParseADTS parses one ADTS frame header from r. Mirrors libavcodec/adts_header.c:ff_adts_header_parse @ d09d5afc3a, including its exact field order and validation.
func ParseADTSHeaderBytes ¶
func ParseADTSHeaderBytes(buf []byte) (ADTSHeader, error)
ParseADTSHeaderBytes parses one ADTS frame header from the front of buf. It is the byte-slice entry point the pcm framer uses so it never imports the internal bits reader. buf must be at least ADTSHeaderSize bytes; a shorter buffer returns ErrInvalidData (truncated header).
type CPE ¶
type CPE struct {
MSMask [128]uint8
Ch [2]SCE
MaxSFBSte int
// contains filtered or unexported fields
}
CPE is a channel element: one or two SCEs plus the CPE-only mid/side mask. Mirrors struct ChannelElement (libavcodec/aac/aacdec.h @ d09d5afc3a).
type Config ¶
type Config struct {
ObjectType int
SamplingIndex int
SampleRate int
ChanConfig int
SBR int // -1 unsignalled, 0 absent, 1 present
PS int
FrameLenShort bool
}
Config is the decoder configuration established from the first ADTS header or from an AudioSpecificConfig. Mirrors the fields of MPEG4AudioConfig (libavcodec/mpeg4audio.h @ d09d5afc3a) that AAC-LC mono/stereo decoding consumes.
func ParseASC ¶
ParseASC parses an MPEG-4 AudioSpecificConfig for AAC-LC. It ports the AAC path of libavcodec/mpeg4audio.c:ff_mpeg4audio_get_config_gb plus the LC arm of libavcodec/aac/aacdec.c:decode_ga_specific_config @ d09d5afc3a, keeping the C's two-pass structure: the common header, one frame-length bit and the trailing SBR/PS sync-extension scan run on a copy of the reader; GASpecificConfig is then parsed from the specific-config position. Non-LC object types, chan_config 0 (PCE-configured), chan_config > 2 and 960-sample frames return ErrUnsupported naming the offending value.
type Decoder ¶
type Decoder struct {
// Elems lists the audio channel elements decoded from the last
// DecodeFrame call in bitstream order.
Elems []ElemRef
// contains filtered or unexported fields
}
Decoder decodes AAC-LC bitstream symbols frame by frame.
func NewADTS ¶
func NewADTS() *Decoder
NewADTS returns a Decoder for ADTS input; the configuration is taken from the first frame header.
func NewRaw ¶
NewRaw returns a Decoder for raw AAC access units configured by the given AudioSpecificConfig.
func (*Decoder) AppendS16 ¶
AppendS16 decodes one access unit and appends the frame's reconstructed interleaved little-endian signed 16-bit PCM to dst, returning the extended slice and the number of inter-channel samples emitted (frameSamples on success). It runs the full DecodeFrame parse followed by the internal reconstruction pass, so the pcm package reaches PCM without touching the unexported reconstruct.
The S16 samples are the decoder's native S32P output arithmetic-shifted right by 16. That conversion is proven byte-identical to "ffmpeg -bitexact -c:a aac_fixed -f s16le" (plan-d2 section 6): swresample under BITEXACT is a plain truncating >>16 with no dither, and clip_output already baked in the round-to-nearest bias.
On a decode error dst is returned unmodified with a zero sample count and the wrapped error; the caller (pcm.Decoder) decides whether the error ends the stream or is skipped.
func (*Decoder) Channels ¶
Channels reports the decoded channel count (1 or 2 for AAC-LC mono/stereo). It is valid once the decoder is configured (always for NewRaw; after the first DecodeFrame for NewADTS) and returns 0 before that.
func (*Decoder) Config ¶
Config returns the decoder configuration. Valid once configured (always for NewRaw; after the first DecodeFrame for NewADTS).
func (*Decoder) DecodeFrame ¶
DecodeFrame decodes one access unit: an ADTS frame (NewADTS) or one raw AAC frame (NewRaw). The decoded symbol state is left in the decoder's channel elements and listed in Elems. Mirrors the frame path aac_decode_frame_int -> parse_adts_frame_header -> decode_frame_ga of libavcodec/aac/aacdec.c @ d09d5afc3a at the symbol level.
func (*Decoder) ResetADTS ¶
func (d *Decoder) ResetADTS()
ResetADTS rearms the decoder for a fresh ADTS stream while keeping the allocated channel elements, the Elems backing array and the dsp IMDCT contexts, so a pooled caller (pcm.Decoder.Reset) re-decodes with zero allocation. The next frame's ADTS header reconfigures the layout via configure, which reuses the retained che allocation.
type ICSInfo ¶
type ICSInfo struct {
WindowSequence [2]int
UseKBWindow [2]int
MaxSFB int
NumWindows int
NumWindowGroups int
GroupLen [8]int
NumSWB int
SWBOffset []uint16
TNSMaxBands int
PredictorPresent bool
}
ICSInfo carries individual_channel_stream configuration. Mirrors struct IndividualChannelStream (libavcodec/aac/aacdec.h @ d09d5afc3a), symbol fields only.
type SCE ¶
type SCE struct {
ICS ICSInfo
TNS TNSData
BandType [128]uint8
SFO [128]int32
SF [128]int32
QCoefs [1024]int32
Pulse Pulse
PulsePresent bool
// D1 reconstruction state. Coeffs is the dequantized spectrum the
// IMDCT consumes (the C dequantizes coeffs in place; D2 wires QCoefs
// -> Coeffs). Saved mirrors SingleChannelElement.saved_fixed[1536]
// (LC uses the first 512 entries); Output holds the frame's 1024
// time-domain samples (the C writes them into the AVFrame buffer).
Coeffs [1024]int32
Saved [1536]int32
Output [1024]int32
}
SCE is the per-channel decode state down to quantized spectral symbols. Mirrors the symbol-level fields of struct SingleChannelElement (libavcodec/aac/aacdec.h @ d09d5afc3a).
type TNSData ¶
type TNSData struct {
Present bool
NFilt [8]int
Length [8][4]int
Order [8][4]int
Direction [8][4]int
CoefFixed [8][4][tnsMaxOrder]int32
}
TNSData carries parsed temporal noise shaping side info. Mirrors struct TemporalNoiseShaping (libavcodec/aac/aacdec.h @ d09d5afc3a); CoefFixed holds the Q31 coefficients the fixed decoder derives at parse time.