Documentation
¶
Overview ¶
Package enc implements the AAC encoder frame pipeline: input buffering, window switching, MDCT, psychoacoustic analysis, the coding tools (TNS, PNS, M/S, I/S), rate control and raw_data_block bitstream writing. Mirrors libavcodec/aacenc.c @ d09d5afc3a for mono SCE and stereo CPE at 44100 and 48000 Hz, with the full LAME window decision and 3GPP psy model and all three coders (NMR, twoloop, fast). The package emits raw AAC access units; ADTS framing is applied by the caller (the root package owns the ADTS writer).
NMR pre-quantization stereo decisions: per-band L/R vs M/S vs intensity from the psy model alone, so the trellis allocates natively on the spectra actually coded. Mirrors the nmr_* stereo helpers of libavcodec/aacenc.c @ d09d5afc3a.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrInvalidAudio = errors.New("enc: input contains (near) NaN/Inf")
ErrInvalidAudio reports non-finite input samples, mirroring the C encoder's 1e16 spectral coefficient guard (aacenc.c:1119-1124 @ d09d5afc3a).
Functions ¶
This section is empty.
Types ¶
type CoderKind ¶
type CoderKind int
CoderKind selects the quantizer search strategy. Mirrors enum AACCoder (libavcodec/aacenc.h @ d09d5afc3a); the zero value is NMR, upstream's default.
type Config ¶
type Config struct {
SampleRate int // 44100 or 48000
Bitrate int // bits per second, total across channels
Channels int // 1 (SCE) or 2 (CPE)
Cutoff int // user bandwidth override in Hz; 0 = automatic (avctx->cutoff)
StrictBitrate bool // strict CBR (mirrors bit_rate_tolerance == 0)
Coder CoderKind // quantizer search; zero value is NMR
NMRSpeed int // NMR speed level 0..4, 0 = slowest/best
// Tool switches; zero values mirror the upstream defaults (all on:
// aac_tns 1, aac_pns 1, aac_ms -1 auto, aac_is 1 @ d09d5afc3a).
// Setting BOTH DisablePNS and DisableIS also narrows the coding
// bandwidth wherever the cutoff formula applies, for every coder
// (aacenc.c:1609-1610); the NMR rate table is unaffected.
DisableTNS bool // disable temporal noise shaping
DisablePNS bool // disable perceptual noise substitution
DisableMS bool // disable the mid/side auto search
DisableIS bool // disable intensity stereo
}
Config selects the encoder parameters.
type Encoder ¶
type Encoder struct {
// contains filtered or unexported fields
}
Encoder is the AAC encoder state: everything is preallocated here so the encode path is allocation-free in steady state (docs/go-design.md allocation policy). Mirrors the mono/stereo AAC-LC subset of AACEncContext (libavcodec/aacenc.h @ d09d5afc3a).
func New ¶
New returns an encoder for cfg. Mirrors aacenc.c:aac_encode_init for the single SCE or CPE element of the v1 channel maps @ d09d5afc3a: samplerate index lookup, bitrate clamping, lambda default, coding bandwidth computation and psy init.
func (*Encoder) Drained ¶
Drained reports whether a flush has consumed all queued samples; once true, EncodeFrame(dst, nil) returns dst unchanged.
func (*Encoder) EncodeFrame ¶
EncodeFrame encodes the next 1024-sample frame of planar float32 PCM in [-1, 1] (one slice per channel) and appends one raw AAC access unit to dst. Pass nil samples to flush; a shorter final frame is zero-padded. Returns dst unchanged during priming (the first call) and once the flush has drained all queued samples. Mirrors aacenc.c:aac_encode_frame for the single SCE or CPE element of the v1 channel maps @ d09d5afc3a.
func (*Encoder) Reset ¶
Reset re-arms the encoder for a new, independent stream with cfg, reusing every retained heap allocation (MDCT contexts, psy context, NMR state, bitstream buffer) so pooled encoders stay allocation-light. Encoding after Reset produces the same bytes as encoding with a fresh New(cfg) encoder. On error the encoder must not be used.
func (*Encoder) SampleRateIndex ¶
SampleRateIndex returns the MPEG-4 samplerate table index.
type Stats ¶
type Stats struct {
LambdaSum float64 // sum of the per-frame operating lambda (aacenc.c:1412)
LambdaCount int64 // frames folded into LambdaSum
Chans int64 // coded channel-frames
Short int64 // of which short-block (transient)
TNSLong int64 // TNS-active channel-frames among long blocks
TNSShort int64 // TNS-active channel-frames among short blocks
ChBands int64 // coded channel-bands
PNS int64 // of which PNS (noise substitution)
CPEBands int64 // coded CPE pair-bands
MS int64 // of which mid/side coded
IS int64 // of which intensity coded
}
Stats holds the encoder's tool-usage counters, accumulated per encoded frame over the final per-band decisions. Mirrors the AACEncContext stat_* counters and lambda accounting (libavcodec/aacenc.h:232-239 @ d09d5afc3a); the C prints them once at uninit, Go exposes them as a snapshot instead (docs/go-design.md: no logging in the library, ever).