psy

package
v0.7.3 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: 4 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 psy implements the AAC encoder psychoacoustic model: the LAME block-switching window decision and the 3GPP TS26.403-inspired threshold analysis with its bit reservoir. Mirrors libavcodec/aacpsy.c and the consumed subset of libavcodec/psymodel.c/h @ d09d5afc3a. The single channel group of ff_psy_init (mono SCE or one CPE) is collapsed into the context; coupling's virtual channels are not ported.

Index

Constants

View Source
const HpFilterIsSIMD = true

HpFilterIsSIMD is true on the default build, recording that this package compiled the SIMD high-pass filter dispatch (hpfilter_amd64.go, hpfilter_arm64.go, and the portable stub hpfilter_other.go on other architectures). The root package asserts it against aac.SIMDEnabled (simd_kernels_test.go).

View Source
const PeLogIsSIMD = true

PeLogIsSIMD is true on the default build, recording that this package compiled the SIMD logarithm dispatch for calcPE3gpp (pelog_arm64.go, and the portable stub pelog_other.go on amd64 and other architectures). The root package asserts it against aac.SIMDEnabled (simd_kernels_test.go).

Variables

This section is empty.

Functions

This section is empty.

Types

type Context

type Context struct {
	Bands    [2][]uint8 // scalefactor band sizes: [0] long, [1] short
	NumBands [2]int     // number of scalefactor bands: [0] long, [1] short
	Cutoff   int        // analysis bandwidth in Hz

	Bitres struct {
		Size  int // size of the bit reservoir in bits
		Bits  int // number of bits used in the bit reservoir
		Alloc int // bits allocated by the psy, or -1 if no allocation was done
	}

	Ch []FFChannel // per-channel analysis output
	// contains filtered or unexported fields
}

Context is the psychoacoustic model state: the FFPsyContext fields the AAC encoder consumes plus the private AacPsyContext, merged because Go needs no model vtable (one model exists). Mirrors FFPsyContext (psymodel.h:89-109) and AacPsyContext (aacpsy.c:166-185) @ d09d5afc3a.

func New

func New(sampleRate, bitRate, channels, cutoff int, bands [2][]uint8, numBands [2]int) *Context

New creates the psy model state for one mono SCE or one stereo CPE. Mirrors psy_3gpp_init (aacpsy.c:318-403) plus the consumed subset of ff_psy_init (psymodel.c:28-65) @ d09d5afc3a. bands/numBands carry the long ([0]) and short ([1]) scalefactor band tables; cutoff is the coding bandwidth in Hz (always non-zero: the encoder computes it at init). The QSCALE/global_quality branches are not ported: the pipeline never sets AV_CODEC_FLAG_QSCALE (no VBR mode in the API), so only the ABR path runs.

func (*Context) Analyze

func (ctx *Context) Analyze(frameNum int64, startCh int, coeffs [][]float32, wi []WindowInfo)

Analyze performs the psychoacoustic analysis for the channel group starting at startCh. Mirrors aacpsy.c:psy_3gpp_analyze @ d09d5afc3a, including the rate-loop rewind: the encoder's rate-control loop may re-run the analysis for the same frame, and carried state (bit reservoir, PE history, previous-frame thresholds) must advance exactly once per frame, so it is saved on the frame's first run and rewound on re-runs. The frameNum parameter replaces avctx->frame_num. The channel group is the context's only group (mono SCE or one CPE), so ff_psy_find_group collapses to ctx.channels.

func (*Context) Reset

func (ctx *Context) Reset(sampleRate, bitRate, channels, cutoff int, bands [2][]uint8, numBands [2]int)

Reset re-arms an existing Context for a new, independent stream with the given parameters, reusing the Context and the two per-channel slices (channel and FFChannel are pure value types) so a pooled encoder rebuilds the psychoacoustic model without allocating. Every field is recomputed here and the reused slices are cleared to the same zeroed state make would have produced, so the result is field-for-field equal to a fresh New(...) with the same arguments and the encoder output stays byte-identical. The one difference is unobservable: a reused slice may keep spare capacity after a shrink (cap, not len or contents), which no consumer reads. This backs the allocation-free pcm.EncodeInterleaved reuse path (issue #41). Growing the channel count reallocates the per-channel slices; shrinking or matching it reslices and clears them in place.

func (*Context) Window

func (ctx *Context) Window(la []float32, ch, prevType int) WindowInfo

Window suggests the window sequence, shape and grouping for one channel. Mirrors aacpsy.c:psy_lame_window @ d09d5afc3a: the LAME attack detection (21-tap FIR high pass, sub-shortblock energy ratios against the bitrate-dependent attack threshold, the 2026-06 pre-echo relaxation for isolated onsets) followed by the window-sequence state machine. la is the lookahead pointer (samples2 + 448 + 64 in the encoder) and is nil on the final flush frame, in which case the previous sequence is continued (docs/porting-guide.md pitfall 11). The complexity waiver covers a faithful port of a single 150-line C function.

type FFChannel

type FFChannel struct {
	PsyBands [128]coder.PsyBand // channel band information
	Entropy  float32            // total PE for this channel
}

FFChannel is the per-channel analysis output consumed by the coders. Mirrors struct FFPsyChannel (psymodel.h:60-63 @ d09d5afc3a).

type WindowInfo

type WindowInfo struct {
	WindowType  [3]int // current, previous and next window type
	WindowShape int    // 1 = KBD, 0 = sine
	NumWindows  int
	Grouping    [8]int // windows per group, at group-start positions
}

WindowInfo is the window decision for one channel. Mirrors struct FFPsyWindowInfo (psymodel.h:77-84 @ d09d5afc3a); the clipping evaluation stays in the encoder (internal/enc), which owns the window buffers.

Jump to

Keyboard shortcuts

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