Documentation
¶
Index ¶
- func Add(dst, a, b []int32)
- func LPCRestore(out, residual, coeffs []int32, shift uint)
- func LPCRestoreNarrow(out, residual, coeffs []int32, shift uint)
- func LPCRestoreNarrowSafe(coeffs []int32, bps, shift uint) bool
- func MidSideDecode(left, right, mid, side []int32)
- func MinMax(res []int32) (minVal, maxVal int32)
- func Restore1(dst, src []int32)
- func Restore2(dst, src []int32)
- func Restore3(dst, src []int32)
- func Restore4(dst, src []int32)
- func Sub(dst, a, b []int32)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Add ¶
func Add(dst, a, b []int32)
Add writes dst[i] = a[i] + b[i] for i in [0, n), n = min(len(dst), len(a), len(b)). It reconstructs the left channel in FLAC RIGHT_SIDE decode: Add(left, right, side).
func LPCRestore ¶
LPCRestore reconstructs the samples LPCResidualEncode encoded, inverting it with the serial recurrence:
out[i] = residual[i] for i < order out[i] = residual[i] + int32((Σ_j coeffs[j]*out[i-1-j]) >> shift) for i >= order
where order = len(coeffs). It processes n = min(len(out), len(residual)) and leaves any trailing capacity in out untouched. Given the same coeffs and shift, LPCRestore(LPCResidualEncode(samples)) == samples. shift is clamped to 63, as in LPCResidualEncode, so encode and decode stay matched across all paths.
out and residual may be the same slice (exact aliasing), which the FLAC decode path relies on to reconstruct a subframe in place; partial overlap is not supported.
func LPCRestoreNarrow ¶
LPCRestoreNarrow is LPCRestore with 32-bit accumulation: the prediction sum is computed in int32 and shifted, which is cheaper than int64 on some CPUs. It is bit-identical to LPCRestore only when LPCRestoreNarrowSafe(coeffs, bps, shift) holds for a bps that bounds every warm-up and restored sample, which is the case for a valid FLAC subframe. Otherwise the sum may wrap and the result may differ from LPCRestore, but it never panics or accesses memory out of bounds. When the predicate holds and the warm-up samples are within bps, the two still agree up to and including the first restored sample outside bps and can differ after it. It has the same n clamp, order == 0 / order >= n copy and aliasing contract as LPCRestore. Orders up to the per-architecture ceiling maxScalarRestoreOrderNarrow run int32 code (lpc_small_narrow.go): the unrolled kernels through maxScalarKernelOrderNarrow and the generic loop lpcRestoreGoNarrow above them. Every other order runs LPCRestore, because no narrow SIMD kernel beat the int64 one in measurement.
func LPCRestoreNarrowSafe ¶
LPCRestoreNarrowSafe reports whether a subframe of bps-bit samples with these coefficients and shift can be restored with 32-bit accumulation, LPCRestoreNarrow, without changing the result. It is libFLAC 1.5.0's condition (stream_decoder.c read_subframe_lpc_ with FLAC__lpc_max_residual_bps and FLAC__lpc_max_prediction_before_shift_bps in lpc.c): both
bits(2^(bps-1) * A) <= 32 (the unshifted prediction fits int32) bits(2^(bps-1) + ceil(2^(bps-1) * A / 2^shift)) <= 32
where A = sum(|coeffs[j]|) and bits is the signed bit width. In closed form, with A as an exact 64-bit sum: A < 2^(32-bps), and 2^(bps-1) + ceil(A * 2^(bps-1) / 2^shift) < 2^31.
When it holds and every warm-up and restored sample lies in the bps-bit signed range, |sum(coeffs[j]*x)| <= 2^(bps-1) * A < 2^31 for any subset of terms, so the int32 sum cannot wrap in any summation order and equals the int64 sum. It returns false for bps == 0, bps > 32, shift > maxLPCShiftNarrow and more than maxLPCRestoreOrder coefficients.
func MidSideDecode ¶
func MidSideDecode(left, right, mid, side []int32)
MidSideDecode inverts MidSideEncode, reconstructing the original channels:
sum = (mid[i] << 1) | (side[i] & 1) left[i] = (sum + side[i]) >> 1 right[i] = (sum - side[i]) >> 1
The parity bit (side[i] & 1) restores the low bit of left+right that the encoder's >>1 discarded; left+right and left-right always share parity.
func MinMax ¶
MinMax returns the smallest and largest int32 in res:
minVal = min_i res[i], maxVal = max_i res[i]
Both are signed comparisons. An empty res returns (0, 0). res is read-only; the call allocates nothing.
This is the reduction the FLAC Rice planner runs per finest partition to find each partition's largest-magnitude residual (the escape-code cost input): the largest zigzag fold over a partition is reached at its most-negative or most-positive sample, so max(zigzag(minVal), zigzag(maxVal)) is that extreme. The SIMD fast path uses signed-min/max vector instructions (VPMINSD/VPMAXSD on amd64, SMIN/SMAX on arm64) with a scalar tail; both are bit-exact against the pure-Go reference.
func Restore1 ¶
func Restore1(dst, src []int32)
Restore1 inverts Diff1: dst[0]=src[0], dst[n]=src[n]+dst[n-1].
func Restore2 ¶
func Restore2(dst, src []int32)
Restore2 inverts Diff2: dst[n]=src[n]+2*dst[n-1]-dst[n-2].
func Restore3 ¶
func Restore3(dst, src []int32)
Restore3 inverts Diff3: dst[n]=src[n]+3*dst[n-1]-3*dst[n-2]+dst[n-3].
Types ¶
This section is empty.