OSDN Git Service

[flac] Update FLAC to 1.4.1
[timidity41/timidity41.git] / FLAC / src / private / lpc.h
1 /* libFLAC - Free Lossless Audio Codec library
2  * Copyright (C) 2000-2009  Josh Coalson
3  * Copyright (C) 2011-2022  Xiph.Org Foundation
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * - Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * - Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * - Neither the name of the Xiph.org Foundation nor the names of its
17  * contributors may be used to endorse or promote products derived from
18  * this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 #ifndef FLAC__PRIVATE__LPC_H
34 #define FLAC__PRIVATE__LPC_H
35
36 #ifdef HAVE_CONFIG_H
37 #include <config.h>
38 #endif
39
40 #include "private/cpu.h"
41 #include "private/float.h"
42 #include "FLAC/format.h"
43
44 #ifndef FLAC__INTEGER_ONLY_LIBRARY
45
46 /*
47  *      FLAC__lpc_window_data()
48  *      --------------------------------------------------------------------
49  *      Applies the given window to the data.
50  *  OPT: asm implementation
51  *
52  *      IN in[0,data_len-1]
53  *      IN window[0,data_len-1]
54  *      OUT out[0,lag-1]
55  *      IN data_len
56  */
57 void FLAC__lpc_window_data(const FLAC__int32 in[], const FLAC__real window[], FLAC__real out[], uint32_t data_len);
58 void FLAC__lpc_window_data_wide(const FLAC__int64 in[], const FLAC__real window[], FLAC__real out[], uint32_t data_len);
59 void FLAC__lpc_window_data_partial(const FLAC__int32 in[], const FLAC__real window[], FLAC__real out[], uint32_t data_len, uint32_t part_size, uint32_t data_shift);
60 void FLAC__lpc_window_data_partial_wide(const FLAC__int64 in[], const FLAC__real window[], FLAC__real out[], uint32_t data_len, uint32_t part_size, uint32_t data_shift);
61
62 /*
63  *      FLAC__lpc_compute_autocorrelation()
64  *      --------------------------------------------------------------------
65  *      Compute the autocorrelation for lags between 0 and lag-1.
66  *      Assumes data[] outside of [0,data_len-1] == 0.
67  *      Asserts that lag > 0.
68  *
69  *      IN data[0,data_len-1]
70  *      IN data_len
71  *      IN 0 < lag <= data_len
72  *      OUT autoc[0,lag-1]
73  */
74 void FLAC__lpc_compute_autocorrelation(const FLAC__real data[], uint32_t data_len, uint32_t lag, double autoc[]);
75 #ifndef FLAC__NO_ASM
76 #  if (defined FLAC__CPU_IA32 || defined FLAC__CPU_X86_64) && FLAC__HAS_X86INTRIN
77 #    ifdef FLAC__SSE2_SUPPORTED
78 void FLAC__lpc_compute_autocorrelation_intrin_sse2_lag_8(const FLAC__real data[], uint32_t data_len, uint32_t lag, double autoc[]);
79 void FLAC__lpc_compute_autocorrelation_intrin_sse2_lag_10(const FLAC__real data[], uint32_t data_len, uint32_t lag, double autoc[]);
80 void FLAC__lpc_compute_autocorrelation_intrin_sse2_lag_14(const FLAC__real data[], uint32_t data_len, uint32_t lag, double autoc[]);
81 #    endif
82 #  endif
83 #  if defined FLAC__CPU_X86_64 && FLAC__HAS_X86INTRIN
84 #    ifdef FLAC__FMA_SUPPORTED
85 void FLAC__lpc_compute_autocorrelation_intrin_fma_lag_8(const FLAC__real data[], uint32_t data_len, uint32_t lag, double autoc[]);
86 void FLAC__lpc_compute_autocorrelation_intrin_fma_lag_12(const FLAC__real data[], uint32_t data_len, uint32_t lag, double autoc[]);
87 void FLAC__lpc_compute_autocorrelation_intrin_fma_lag_16(const FLAC__real data[], uint32_t data_len, uint32_t lag, double autoc[]);
88 #    endif
89 #  endif
90 #if defined(FLAC__CPU_PPC64) && defined(FLAC__USE_VSX)
91 #ifdef FLAC__HAS_TARGET_POWER9
92 void FLAC__lpc_compute_autocorrelation_intrin_power9_vsx_lag_8(const FLAC__real data[], uint32_t data_len, uint32_t lag, double autoc[]);
93 void FLAC__lpc_compute_autocorrelation_intrin_power9_vsx_lag_10(const FLAC__real data[], uint32_t data_len, uint32_t lag, double autoc[]);
94 void FLAC__lpc_compute_autocorrelation_intrin_power9_vsx_lag_14(const FLAC__real data[], uint32_t data_len, uint32_t lag, double autoc[]);
95 #endif
96 #ifdef FLAC__HAS_TARGET_POWER8
97 void FLAC__lpc_compute_autocorrelation_intrin_power8_vsx_lag_8(const FLAC__real data[], uint32_t data_len, uint32_t lag, double autoc[]);
98 void FLAC__lpc_compute_autocorrelation_intrin_power8_vsx_lag_10(const FLAC__real data[], uint32_t data_len, uint32_t lag, double autoc[]);
99 void FLAC__lpc_compute_autocorrelation_intrin_power8_vsx_lag_14(const FLAC__real data[], uint32_t data_len, uint32_t lag, double autoc[]);
100 #endif
101 #endif
102 #if defined FLAC__CPU_ARM64 && FLAC__HAS_NEONINTRIN && FLAC__HAS_A64NEONINTRIN
103 void FLAC__lpc_compute_autocorrelation_intrin_neon_lag_8(const FLAC__real data[], uint32_t data_len, uint32_t lag, double autoc[]);
104 void FLAC__lpc_compute_autocorrelation_intrin_neon_lag_10(const FLAC__real data[], uint32_t data_len, uint32_t lag, double autoc[]);
105 void FLAC__lpc_compute_autocorrelation_intrin_neon_lag_14(const FLAC__real data[], uint32_t data_len, uint32_t lag, double autoc[]);
106 #endif
107 #endif /* FLAC__NO_ASM */
108
109 /*
110  *      FLAC__lpc_compute_lp_coefficients()
111  *      --------------------------------------------------------------------
112  *      Computes LP coefficients for orders 1..max_order.
113  *      Do not call if autoc[0] == 0.0.  This means the signal is zero
114  *      and there is no point in calculating a predictor.
115  *
116  *      IN autoc[0,max_order]                      autocorrelation values
117  *      IN 0 < max_order <= FLAC__MAX_LPC_ORDER    max LP order to compute
118  *      OUT lp_coeff[0,max_order-1][0,max_order-1] LP coefficients for each order
119  *      *** IMPORTANT:
120  *      *** lp_coeff[0,max_order-1][max_order,FLAC__MAX_LPC_ORDER-1] are untouched
121  *      OUT error[0,max_order-1]                   error for each order (more
122  *                                                 specifically, the variance of
123  *                                                 the error signal times # of
124  *                                                 samples in the signal)
125  *
126  *      Example: if max_order is 9, the LP coefficients for order 9 will be
127  *               in lp_coeff[8][0,8], the LP coefficients for order 8 will be
128  *                       in lp_coeff[7][0,7], etc.
129  */
130 void FLAC__lpc_compute_lp_coefficients(const double autoc[], uint32_t *max_order, FLAC__real lp_coeff[][FLAC__MAX_LPC_ORDER], double error[]);
131
132 /*
133  *      FLAC__lpc_quantize_coefficients()
134  *      --------------------------------------------------------------------
135  *      Quantizes the LP coefficients.  NOTE: precision + bits_per_sample
136  *      must be less than 32 (sizeof(FLAC__int32)*8).
137  *
138  *      IN lp_coeff[0,order-1]    LP coefficients
139  *      IN order                  LP order
140  *      IN FLAC__MIN_QLP_COEFF_PRECISION < precision
141  *                                desired precision (in bits, including sign
142  *                                bit) of largest coefficient
143  *      OUT qlp_coeff[0,order-1]  quantized coefficients
144  *      OUT shift                 # of bits to shift right to get approximated
145  *                                LP coefficients.  NOTE: could be negative.
146  *      RETURN 0 => quantization OK
147  *             1 => coefficients require too much shifting for *shift to
148  *              fit in the LPC subframe header.  'shift' is unset.
149  *         2 => coefficients are all zero, which is bad.  'shift' is
150  *              unset.
151  */
152 int FLAC__lpc_quantize_coefficients(const FLAC__real lp_coeff[], uint32_t order, uint32_t precision, FLAC__int32 qlp_coeff[], int *shift);
153
154 /*
155  *      FLAC__lpc_compute_residual_from_qlp_coefficients()
156  *      --------------------------------------------------------------------
157  *      Compute the residual signal obtained from sutracting the predicted
158  *      signal from the original.
159  *
160  *      IN data[-order,data_len-1] original signal (NOTE THE INDICES!)
161  *      IN data_len                length of original signal
162  *      IN qlp_coeff[0,order-1]    quantized LP coefficients
163  *      IN order > 0               LP order
164  *      IN lp_quantization         quantization of LP coefficients in bits
165  *      OUT residual[0,data_len-1] residual signal
166  */
167 void FLAC__lpc_compute_residual_from_qlp_coefficients(const FLAC__int32 *data, uint32_t data_len, const FLAC__int32 qlp_coeff[], uint32_t order, int lp_quantization, FLAC__int32 residual[]);
168 void FLAC__lpc_compute_residual_from_qlp_coefficients_wide(const FLAC__int32 *data, uint32_t data_len, const FLAC__int32 qlp_coeff[], uint32_t order, int lp_quantization, FLAC__int32 residual[]);
169 FLAC__bool FLAC__lpc_compute_residual_from_qlp_coefficients_limit_residual(const FLAC__int32 *data, uint32_t data_len, const FLAC__int32 qlp_coeff[], uint32_t order, int lp_quantization, FLAC__int32 residual[]);
170 FLAC__bool FLAC__lpc_compute_residual_from_qlp_coefficients_limit_residual_33bit(const FLAC__int64 *data, uint32_t data_len, const FLAC__int32 qlp_coeff[], uint32_t order, int lp_quantization, FLAC__int32 residual[]);
171 #ifndef FLAC__NO_ASM
172 #   ifdef FLAC__CPU_ARM64
173 void FLAC__lpc_compute_residual_from_qlp_coefficients_intrin_neon(const FLAC__int32 *data, uint32_t data_len, const FLAC__int32 qlp_coeff[], uint32_t order, int lp_quantization, FLAC__int32 residual[]);
174 void FLAC__lpc_compute_residual_from_qlp_coefficients_wide_intrin_neon(const FLAC__int32 *data, uint32_t data_len, const FLAC__int32 qlp_coeff[], uint32_t order, int lp_quantization, FLAC__int32 residual[]);
175 #   endif
176
177 #  ifdef FLAC__CPU_IA32
178 #    ifdef FLAC__HAS_NASM
179 void FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32(const FLAC__int32 *data, uint32_t data_len, const FLAC__int32 qlp_coeff[], uint32_t order, int lp_quantization, FLAC__int32 residual[]);
180 void FLAC__lpc_compute_residual_from_qlp_coefficients_asm_ia32_mmx(const FLAC__int32 *data, uint32_t data_len, const FLAC__int32 qlp_coeff[], uint32_t order, int lp_quantization, FLAC__int32 residual[]);
181 void FLAC__lpc_compute_residual_from_qlp_coefficients_wide_asm_ia32(const FLAC__int32 *data, uint32_t data_len, const FLAC__int32 qlp_coeff[], uint32_t order, int lp_quantization, FLAC__int32 residual[]);
182 #    endif
183 #  endif
184 #  if (defined FLAC__CPU_IA32 || defined FLAC__CPU_X86_64) && FLAC__HAS_X86INTRIN
185 #    ifdef FLAC__SSE2_SUPPORTED
186 void FLAC__lpc_compute_residual_from_qlp_coefficients_16_intrin_sse2(const FLAC__int32 *data, uint32_t data_len, const FLAC__int32 qlp_coeff[], uint32_t order, int lp_quantization, FLAC__int32 residual[]);
187 void FLAC__lpc_compute_residual_from_qlp_coefficients_intrin_sse2(const FLAC__int32 *data, uint32_t data_len, const FLAC__int32 qlp_coeff[], uint32_t order, int lp_quantization, FLAC__int32 residual[]);
188 #    endif
189 #    ifdef FLAC__SSE4_1_SUPPORTED
190 void FLAC__lpc_compute_residual_from_qlp_coefficients_intrin_sse41(const FLAC__int32 *data, uint32_t data_len, const FLAC__int32 qlp_coeff[], uint32_t order, int lp_quantization, FLAC__int32 residual[]);
191 void FLAC__lpc_compute_residual_from_qlp_coefficients_wide_intrin_sse41(const FLAC__int32 *data, uint32_t data_len, const FLAC__int32 qlp_coeff[], uint32_t order, int lp_quantization, FLAC__int32 residual[]);
192 #    endif
193 #    ifdef FLAC__AVX2_SUPPORTED
194 void FLAC__lpc_compute_residual_from_qlp_coefficients_16_intrin_avx2(const FLAC__int32 *data, uint32_t data_len, const FLAC__int32 qlp_coeff[], uint32_t order, int lp_quantization, FLAC__int32 residual[]);
195 void FLAC__lpc_compute_residual_from_qlp_coefficients_intrin_avx2(const FLAC__int32 *data, uint32_t data_len, const FLAC__int32 qlp_coeff[], uint32_t order, int lp_quantization, FLAC__int32 residual[]);
196 void FLAC__lpc_compute_residual_from_qlp_coefficients_wide_intrin_avx2(const FLAC__int32 *data, uint32_t data_len, const FLAC__int32 qlp_coeff[], uint32_t order, int lp_quantization, FLAC__int32 residual[]);
197 #    endif
198 #  endif
199 #endif
200
201 #endif /* !defined FLAC__INTEGER_ONLY_LIBRARY */
202
203 uint32_t FLAC__lpc_max_prediction_before_shift_bps(uint32_t subframe_bps, const FLAC__int32 qlp_coeff[], uint32_t order);
204 uint32_t FLAC__lpc_max_residual_bps(uint32_t subframe_bps, const FLAC__int32 qlp_coeff[], uint32_t order, int lp_quantization);
205
206 /*
207  *      FLAC__lpc_restore_signal()
208  *      --------------------------------------------------------------------
209  *      Restore the original signal by summing the residual and the
210  *      predictor.
211  *
212  *      IN residual[0,data_len-1]  residual signal
213  *      IN data_len                length of original signal
214  *      IN qlp_coeff[0,order-1]    quantized LP coefficients
215  *      IN order > 0               LP order
216  *      IN lp_quantization         quantization of LP coefficients in bits
217  *      *** IMPORTANT: the caller must pass in the historical samples:
218  *      IN  data[-order,-1]        previously-reconstructed historical samples
219  *      OUT data[0,data_len-1]     original signal
220  */
221 void FLAC__lpc_restore_signal(const FLAC__int32 residual[], uint32_t data_len, const FLAC__int32 qlp_coeff[], uint32_t order, int lp_quantization, FLAC__int32 data[]);
222 void FLAC__lpc_restore_signal_wide(const FLAC__int32 residual[], uint32_t data_len, const FLAC__int32 qlp_coeff[], uint32_t order, int lp_quantization, FLAC__int32 data[]);
223 void FLAC__lpc_restore_signal_wide_33bit(const FLAC__int32 residual[], uint32_t data_len, const FLAC__int32 qlp_coeff[], uint32_t order, int lp_quantization, FLAC__int64 data[]);
224
225 #ifndef FLAC__INTEGER_ONLY_LIBRARY
226
227 /*
228  *      FLAC__lpc_compute_expected_bits_per_residual_sample()
229  *      --------------------------------------------------------------------
230  *      Compute the expected number of bits per residual signal sample
231  *      based on the LP error (which is related to the residual variance).
232  *
233  *      IN lpc_error >= 0.0   error returned from calculating LP coefficients
234  *      IN total_samples > 0  # of samples in residual signal
235  *      RETURN                expected bits per sample
236  */
237 double FLAC__lpc_compute_expected_bits_per_residual_sample(double lpc_error, uint32_t total_samples);
238 double FLAC__lpc_compute_expected_bits_per_residual_sample_with_error_scale(double lpc_error, double error_scale);
239
240 /*
241  *      FLAC__lpc_compute_best_order()
242  *      --------------------------------------------------------------------
243  *      Compute the best order from the array of signal errors returned
244  *      during coefficient computation.
245  *
246  *      IN lpc_error[0,max_order-1] >= 0.0  error returned from calculating LP coefficients
247  *      IN max_order > 0                    max LP order
248  *      IN total_samples > 0                # of samples in residual signal
249  *      IN overhead_bits_per_order          # of bits overhead for each increased LP order
250  *                                          (includes warmup sample size and quantized LP coefficient)
251  *      RETURN [1,max_order]                best order
252  */
253 uint32_t FLAC__lpc_compute_best_order(const double lpc_error[], uint32_t max_order, uint32_t total_samples, uint32_t overhead_bits_per_order);
254
255 #endif /* !defined FLAC__INTEGER_ONLY_LIBRARY */
256
257 #endif