1 /* libFLAC - Free Lossless Audio Codec library
2 * Copyright (C) 2000,2001,2002 Josh Coalson
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
20 #ifndef FLAC__PRIVATE__LPC_H
21 #define FLAC__PRIVATE__LPC_H
23 #include "FLAC/ordinals.h"
25 #define FLAC__MAX_LPC_ORDER (32u)
28 * FLAC__lpc_compute_autocorrelation()
29 * --------------------------------------------------------------------
30 * Compute the autocorrelation for lags between 0 and lag-1.
31 * Assumes data[] outside of [0,data_len-1] == 0.
32 * Asserts that lag > 0.
34 * IN data[0,data_len-1]
36 * IN 0 < lag <= data_len
39 void FLAC__lpc_compute_autocorrelation(const FLAC__real data[], unsigned data_len, unsigned lag, FLAC__real autoc[]);
43 void FLAC__lpc_compute_autocorrelation_asm_ia32(const FLAC__real data[], unsigned data_len, unsigned lag, FLAC__real autoc[]);
44 void FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_4(const FLAC__real data[], unsigned data_len, unsigned lag, FLAC__real autoc[]);
45 void FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_8(const FLAC__real data[], unsigned data_len, unsigned lag, FLAC__real autoc[]);
46 void FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_12(const FLAC__real data[], unsigned data_len, unsigned lag, FLAC__real autoc[]);
47 void FLAC__lpc_compute_autocorrelation_asm_ia32_3dnow(const FLAC__real data[], unsigned data_len, unsigned lag, FLAC__real autoc[]);
53 * FLAC__lpc_compute_lp_coefficients()
54 * --------------------------------------------------------------------
55 * Computes LP coefficients for orders 1..max_order.
56 * Do not call if autoc[0] == 0.0. This means the signal is zero
57 * and there is no point in calculating a predictor.
59 * IN autoc[0,max_order] autocorrelation values
60 * IN 0 < max_order <= FLAC__MAX_LPC_ORDER max LP order to compute
61 * OUT lp_coeff[0,max_order-1][0,max_order-1] LP coefficients for each order
63 * *** lp_coeff[0,max_order-1][max_order,FLAC__MAX_LPC_ORDER-1] are untouched
64 * OUT error[0,max_order-1] error for each order
66 * Example: if max_order is 9, the LP coefficients for order 9 will be
67 * in lp_coeff[8][0,8], the LP coefficients for order 8 will be
68 * in lp_coeff[7][0,7], etc.
70 void FLAC__lpc_compute_lp_coefficients(const FLAC__real autoc[], unsigned max_order, FLAC__real lp_coeff[][FLAC__MAX_LPC_ORDER], FLAC__real error[]);
73 * FLAC__lpc_quantize_coefficients()
74 * --------------------------------------------------------------------
75 * Quantizes the LP coefficients. NOTE: precision + bits_per_sample
76 * must be less than 32 (sizeof(FLAC__int32)*8).
78 * IN lp_coeff[0,order-1] LP coefficients
80 * IN FLAC__MIN_QLP_COEFF_PRECISION < precision
81 * desired precision (in bits, including sign
82 * bit) of largest coefficient
83 * OUT qlp_coeff[0,order-1] quantized coefficients
84 * OUT shift # of bits to shift right to get approximated
85 * LP coefficients. NOTE: could be negative.
86 * RETURN 0 => quantization OK
87 * 1 => coefficients require too much shifting for *shift to
88 * fit in the LPC subframe header. 'shift' is unset.
89 * 2 => coefficients are all zero, which is bad. 'shift' is
92 int FLAC__lpc_quantize_coefficients(const FLAC__real lp_coeff[], unsigned order, unsigned precision, FLAC__int32 qlp_coeff[], int *shift);
95 * FLAC__lpc_compute_residual_from_qlp_coefficients()
96 * --------------------------------------------------------------------
97 * Compute the residual signal obtained from sutracting the predicted
98 * signal from the original.
100 * IN data[-order,data_len-1] original signal (NOTE THE INDICES!)
101 * IN data_len length of original signal
102 * IN qlp_coeff[0,order-1] quantized LP coefficients
103 * IN order > 0 LP order
104 * IN lp_quantization quantization of LP coefficients in bits
105 * OUT residual[0,data_len-1] residual signal
107 void FLAC__lpc_compute_residual_from_qlp_coefficients(const FLAC__int32 data[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 residual[]);
108 void FLAC__lpc_compute_residual_from_qlp_coefficients_wide(const FLAC__int32 data[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 residual[]);
110 #ifdef FLAC__CPU_IA32
111 #ifdef FLAC__HAS_NASM
112 void FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32(const FLAC__int32 data[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 residual[]);
113 void FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32_mmx(const FLAC__int32 data[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 residual[]);
119 * FLAC__lpc_restore_signal()
120 * --------------------------------------------------------------------
121 * Restore the original signal by summing the residual and the
124 * IN residual[0,data_len-1] residual signal
125 * IN data_len length of original signal
126 * IN qlp_coeff[0,order-1] quantized LP coefficients
127 * IN order > 0 LP order
128 * IN lp_quantization quantization of LP coefficients in bits
129 * *** IMPORTANT: the caller must pass in the historical samples:
130 * IN data[-order,-1] previously-reconstructed historical samples
131 * OUT data[0,data_len-1] original signal
133 void FLAC__lpc_restore_signal(const FLAC__int32 residual[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 data[]);
134 void FLAC__lpc_restore_signal_wide(const FLAC__int32 residual[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 data[]);
136 #ifdef FLAC__CPU_IA32
137 #ifdef FLAC__HAS_NASM
138 void FLAC__lpc_restore_signal_asm_ia32(const FLAC__int32 residual[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 data[]);
139 void FLAC__lpc_restore_signal_asm_ia32_mmx(const FLAC__int32 residual[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 data[]);
145 * FLAC__lpc_compute_expected_bits_per_residual_sample()
146 * --------------------------------------------------------------------
147 * Compute the expected number of bits per residual signal sample
148 * based on the LP error (which is related to the residual variance).
150 * IN lpc_error >= 0.0 error returned from calculating LP coefficients
151 * IN total_samples > 0 # of samples in residual signal
152 * RETURN expected bits per sample
154 FLAC__real FLAC__lpc_compute_expected_bits_per_residual_sample(FLAC__real lpc_error, unsigned total_samples);
155 FLAC__real FLAC__lpc_compute_expected_bits_per_residual_sample_with_error_scale(FLAC__real lpc_error, double error_scale);
158 * FLAC__lpc_compute_best_order()
159 * --------------------------------------------------------------------
160 * Compute the best order from the array of signal errors returned
161 * during coefficient computation.
163 * IN lpc_error[0,max_order-1] >= 0.0 error returned from calculating LP coefficients
164 * IN max_order > 0 max LP order
165 * IN total_samples > 0 # of samples in residual signal
166 * IN bits_per_signal_sample # of bits per sample in the original signal
167 * RETURN [1,max_order] best order
169 unsigned FLAC__lpc_compute_best_order(const FLAC__real lpc_error[], unsigned max_order, unsigned total_samples, unsigned bits_per_signal_sample);