enc

package
v0.7.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 5, 2026 License: LGPL-2.1 Imports: 10 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
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.

const (
	CoderNMR     CoderKind = iota // noise-to-mask ratio trellis (default)
	CoderFast                     // fast two-loop heuristic
	CoderTwoLoop                  // ISO 13818-7 Appendix C two-loop search
)

Coder kinds. Mirror enum AACCoder @ d09d5afc3a.

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

func New(cfg Config) (*Encoder, error)

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) Bandwidth

func (e *Encoder) Bandwidth() int

Bandwidth returns the coding bandwidth in Hz fixed at init.

func (*Encoder) Channels

func (e *Encoder) Channels() int

Channels returns the configured channel count.

func (*Encoder) Drained

func (e *Encoder) Drained() bool

Drained reports whether a flush has consumed all queued samples; once true, EncodeFrame(dst, nil) returns dst unchanged.

func (*Encoder) EncodeFrame

func (e *Encoder) EncodeFrame(dst []byte, samples [][]float32) ([]byte, error)

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

func (e *Encoder) Reset(cfg Config) error

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

func (e *Encoder) SampleRateIndex() int

SampleRateIndex returns the MPEG-4 samplerate table index.

func (*Encoder) Stats

func (e *Encoder) Stats() Stats

Stats returns a snapshot of the tool-usage counters accumulated since New or Reset. Mirrors the AACEncContext stat_* fields and Qavg accounting (aacenc.h:232-239 @ d09d5afc3a).

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).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL