dsp

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: 3 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 dsp provides the scalar DSP kernels of the AAC encoder. The exported AbsPow34 and QuantizeBands are per-build dispatch points: the default build maps them onto f32-backed SIMD versions, while a -tags noasm build calls the canonical absPow34Scalar and quantizeBandsScalar defined here. Both produce byte-identical output; the scalars stay canonical.

SPDX-License-Identifier: LGPL-2.1-or-later

SPDX-License-Identifier: LGPL-2.1-or-later

Index

Constants

View Source
const AbsPow34IsSIMD = true

AbsPow34IsSIMD is true on the default build, recording that this file provides the SIMD kernel. The root package asserts it against aac.SIMDEnabled (simd_kernels_test.go), so the public accessor cannot claim a kernel this package did not compile.

View Source
const MaxLPCOrder = 32

MaxLPCOrder mirrors MAX_LPC_ORDER (libavcodec/lpc.h @ d09d5afc3a).

View Source
const QuantizeBandsIsSIMD = true

QuantizeBandsIsSIMD is true on the default build, recording that this file provides the SIMD kernel. The root package asserts it against aac.SIMDEnabled (simd_kernels_test.go), so the public accessor cannot claim a kernel this package did not compile.

Variables

This section is empty.

Functions

func AbsPow34

func AbsPow34(out, in []float32)

AbsPow34 is the default dispatch: it maps out[i] = |in[i]|^(3/4) onto f32.AbsPow34 (NEON on arm64, AVX or SSE on amd64, bit-identical pure-Go fallback elsewhere). Its output is byte-identical to absPow34Scalar for finite input: the primitive computes the same exact abs, the same two IEEE correctly-rounded square roots, and the same single correctly-rounded float32 multiply, and there is no add so the no-FMA question never arises. abspow34_simd_equiv_test.go gates it bitwise against the scalar.

The one divergence class is NaN payload bits, which never reaches this call site: the public encoder rejects non-finite PCM (#18). The panic must live here because f32.AbsPow34 silently processes min(len(dst), len(src)) instead of panicking, so without it the destination-length contract would quietly weaken on the default build.

func Autocorr

func Autocorr(x []float64, maxLag int, out []float64)

Autocorr computes autocorrelation lags 0..maxLag of x into out. Mirrors libavcodec/lpc.c:lpc_compute_autocorr_c @ d09d5afc3a, including its bias: every lag sum is seeded with 1.0, not 0 (the C reads one zero sample of left padding for odd lags, which contributes nothing, so the per-lag accumulation order below is identical to the C's paired loop). Ranging over the lag-shifted sub-slice x[j:] pairs each element with x[i], which is the same product in the same order as the C's x[i]*x[i-j], but lets the compiler prove both indices: len(rem) <= len(x), so the loop body carries no bounds check at all (verified with -d=ssa/check_bce/debug=1).

The product carries a float64() barrier so gc does not fuse it into an FMADDD on arm64: the C reference is -ffp-contract=off, and the float64 autoc values feed TNS coefficients that are later cast to float32, so a fused sub-ulp drift is a latent architecture split (issue #79).

func CalcRefCoefs

func CalcRefCoefs(samples []float32, order int, ref []float64, applyWindow bool, scratch []float64) float64

CalcRefCoefs mirrors libavcodec/lpc.c:ff_lpc_calc_ref_coefs_f @ d09d5afc3a: optional Hann window (rectangular when applyWindow is false; TNS uses rectangular for short blocks), double-precision autocorrelation, reflection coefficients. Returns signal/avg_err prediction gain (NaN when avg_err is zero). scratch must hold len(samples) float64.

func ComputeRefCoefs

func ComputeRefCoefs(autoc []float64, maxOrder int, ref, errOut []float64)

ComputeRefCoefs converts autocorrelation values to reflection coefficients with per-order prediction error. Mirrors libavcodec/lpc.c:compute_ref_coefs @ d09d5afc3a (float path: zero error denominators fall back to 1).

errOut may be nil, as it is in the C (compute_ref_coefs is called with a NULL error pointer from ff_lpc_calc_ref_coefs).

Every multiply-add carries a float64() barrier so gc does not fuse it into an FMADDD on arm64, for the same reason as Autocorr: these values become TNS coefficients cast to float32, and the C reference is -ffp-contract=off (#79).

func QuantizeBands

func QuantizeBands(out []int32, in, scaled []float32, isSigned bool, maxval int, q34, rounding float32)

QuantizeBands is the default dispatch. It maps the pow34-scaled magnitude quantizer onto the generic f32 primitives:

signed:   out[i] = copysign(int32(clamp(scaled[i]*q34 + rounding, 0, maxval)), in[i])
unsigned: out[i] =          int32(clamp(scaled[i]*q34 + rounding, 0, maxval))

which is quantizeBandsScalar's body. Bit-identity on encoder input rests on three facts the primitive's contract guarantees, matching the scalar exactly:

  • The product rounds to float32 before rounding is added and is never contracted into an FMA, the same as the scalar's explicit float32() cast. This is the hard no-FMA constraint from the kernel doc.
  • Conversion truncates toward zero, the same as int32(v).
  • Out-of-range clamps rather than wraps: +Inf -> maxval matches the scalar's min(+Inf, m) = m, and the int32 convert collapses both.

The minV = 0 floor is inert here: scaled is AbsPow34 output, so scaled >= 0, and q34 > 0 with rounding >= 0, so scaled[i]*q34 + rounding is never negative and the floor never fires. It is passed only because the primitive requires a lower bound; a negative magnitude (which the encoder never produces, since scaled is AbsPow34 output and |x|^(3/4) is non-negative) would diverge from the unclamped scalar, and TestQuantizeBandsMatchesScalar keeps the sweep inside the non-negative domain.

Signed uses copysign on bit 31 of in[i] where the scalar uses in[i] < 0. The two differ only for a -0.0 sign, and only when the magnitude is nonzero; but a -0.0 coefficient has magnitude 0, which quantizes to int32 0 where the sign is inert, so there is no divergence on encoder input (rounding is always < 1, so a zero magnitude cannot round up to a nonzero value). The equiv test pins the -0.0-on-zero-magnitude case for both isSigned values.

Both sources are length-checked up front regardless of isSigned: the scalar panics on a short in even when unsigned, and the contract must not weaken per branch. The panic must live here because the f32 primitives silently process min(len(...)) instead of panicking, so without it the destination-length contract that TestKernelLengthContract pins would quietly weaken on the default build. quantizebands_simd_equiv_test.go gates the output bitwise against quantizeBandsScalar.

Bands narrower than avxMinLen route to quantizeBandsScalar instead of the primitive: below the AVX-activation width the f32 kernels fall back to a portable Go loop reached through non-inlined cross-package frames, which loses to the inlined scalar, and n=4 is the most common scalefactor-band width. This keeps the vectorized win on the wide bands without a dispatch regression on the common narrow ones. The routed scalar owns its own length panic, so the destination-length contract holds on both paths.

func RefToLPC

func RefToLPC(ref []float32, order int, lpc []float32)

RefToLPC converts reflection coefficients to direct-form LPC coefficients. Mirrors libavcodec/lpc_functions.h:compute_lpc_coefs @ d09d5afc3a with i=0, stride=0, fail=0, normalize=0, exactly as the TNS search calls it. The products r*b and r*f are rounded to float32 before the add, mirroring the C's explicit (LPC_TYPE_U) cast; without the conversions Go may fuse the mul+add into one FMA on arm64 and drift from the C oracle.

func VectorFMul

func VectorFMul(dst, src0, src1 []float32)

VectorFMul computes dst[i] = src0[i] * src1[i]. Mirrors AVFloatDSPContext.vector_fmul (libavutil/float_dsp.c @ d09d5afc3a).

func VectorFMulReverse

func VectorFMulReverse(dst, src0, src1 []float32)

VectorFMulReverse computes dst[i] = src0[i] * src1[len-1-i]. Mirrors AVFloatDSPContext.vector_fmul_reverse.

Types

type LCG

type LCG uint32

LCG is the encoder's PNS pseudo-random generator. Mirrors libavcodec/aacenc_utils.h:lcg_random @ d09d5afc3a. The update order is load-bearing for validation diffs; never substitute math/rand.

const LCGSeed LCG = 0x1f2e3d4c

LCGSeed is the initial state set in aacenc.c (@ d09d5afc3a).

func (*LCG) Next

func (l *LCG) Next() int32

Next advances the state and returns it reinterpreted as int32.

Jump to

Keyboard shortcuts

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