34bca4bc320632389f6846874102112f3a4a39f0
[platform/upstream/flac.git] / src / libFLAC / include / private / lpc.h
1 /* libFLAC - Free Lossless Audio Codec library
2  * Copyright (C) 2000-2009  Josh Coalson
3  * Copyright (C) 2011-2013  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/float.h"
41 #include "FLAC/format.h"
42
43 #ifndef FLAC__INTEGER_ONLY_LIBRARY
44
45 /*
46  *      FLAC__lpc_window_data()
47  *      --------------------------------------------------------------------
48  *      Applies the given window to the data.
49  *  OPT: asm implementation
50  *
51  *      IN in[0,data_len-1]
52  *      IN window[0,data_len-1]
53  *      OUT out[0,lag-1]
54  *      IN data_len
55  */
56 void FLAC__lpc_window_data(const FLAC__int32 in[], const FLAC__real window[], FLAC__real out[], unsigned data_len);
57
58 /*
59  *      FLAC__lpc_compute_autocorrelation()
60  *      --------------------------------------------------------------------
61  *      Compute the autocorrelation for lags between 0 and lag-1.
62  *      Assumes data[] outside of [0,data_len-1] == 0.
63  *      Asserts that lag > 0.
64  *
65  *      IN data[0,data_len-1]
66  *      IN data_len
67  *      IN 0 < lag <= data_len
68  *      OUT autoc[0,lag-1]
69  */
70 void FLAC__lpc_compute_autocorrelation(const FLAC__real data[], unsigned data_len, unsigned lag, FLAC__real autoc[]);
71 #ifndef FLAC__NO_ASM
72 #  ifdef FLAC__CPU_IA32
73 #    ifdef FLAC__HAS_NASM
74 void FLAC__lpc_compute_autocorrelation_asm_ia32(const FLAC__real data[], unsigned data_len, unsigned lag, FLAC__real autoc[]);
75 void FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_4(const FLAC__real data[], unsigned data_len, unsigned lag, FLAC__real autoc[]);
76 void FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_8(const FLAC__real data[], unsigned data_len, unsigned lag, FLAC__real autoc[]);
77 void FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_12(const FLAC__real data[], unsigned data_len, unsigned lag, FLAC__real autoc[]);
78 void FLAC__lpc_compute_autocorrelation_asm_ia32_3dnow(const FLAC__real data[], unsigned data_len, unsigned lag, FLAC__real autoc[]);
79 #    endif
80 #  endif
81 #endif
82
83 /*
84  *      FLAC__lpc_compute_lp_coefficients()
85  *      --------------------------------------------------------------------
86  *      Computes LP coefficients for orders 1..max_order.
87  *      Do not call if autoc[0] == 0.0.  This means the signal is zero
88  *      and there is no point in calculating a predictor.
89  *
90  *      IN autoc[0,max_order]                      autocorrelation values
91  *      IN 0 < max_order <= FLAC__MAX_LPC_ORDER    max LP order to compute
92  *      OUT lp_coeff[0,max_order-1][0,max_order-1] LP coefficients for each order
93  *      *** IMPORTANT:
94  *      *** lp_coeff[0,max_order-1][max_order,FLAC__MAX_LPC_ORDER-1] are untouched
95  *      OUT error[0,max_order-1]                   error for each order (more
96  *                                                 specifically, the variance of
97  *                                                 the error signal times # of
98  *                                                 samples in the signal)
99  *
100  *      Example: if max_order is 9, the LP coefficients for order 9 will be
101  *               in lp_coeff[8][0,8], the LP coefficients for order 8 will be
102  *                       in lp_coeff[7][0,7], etc.
103  */
104 void FLAC__lpc_compute_lp_coefficients(const FLAC__real autoc[], unsigned *max_order, FLAC__real lp_coeff[][FLAC__MAX_LPC_ORDER], FLAC__double error[]);
105
106 /*
107  *      FLAC__lpc_quantize_coefficients()
108  *      --------------------------------------------------------------------
109  *      Quantizes the LP coefficients.  NOTE: precision + bits_per_sample
110  *      must be less than 32 (sizeof(FLAC__int32)*8).
111  *
112  *      IN lp_coeff[0,order-1]    LP coefficients
113  *      IN order                  LP order
114  *      IN FLAC__MIN_QLP_COEFF_PRECISION < precision
115  *                                desired precision (in bits, including sign
116  *                                bit) of largest coefficient
117  *      OUT qlp_coeff[0,order-1]  quantized coefficients
118  *      OUT shift                 # of bits to shift right to get approximated
119  *                                LP coefficients.  NOTE: could be negative.
120  *      RETURN 0 => quantization OK
121  *             1 => coefficients require too much shifting for *shift to
122  *              fit in the LPC subframe header.  'shift' is unset.
123  *         2 => coefficients are all zero, which is bad.  'shift' is
124  *              unset.
125  */
126 int FLAC__lpc_quantize_coefficients(const FLAC__real lp_coeff[], unsigned order, unsigned precision, FLAC__int32 qlp_coeff[], int *shift);
127
128 /*
129  *      FLAC__lpc_compute_residual_from_qlp_coefficients()
130  *      --------------------------------------------------------------------
131  *      Compute the residual signal obtained from sutracting the predicted
132  *      signal from the original.
133  *
134  *      IN data[-order,data_len-1] original signal (NOTE THE INDICES!)
135  *      IN data_len                length of original signal
136  *      IN qlp_coeff[0,order-1]    quantized LP coefficients
137  *      IN order > 0               LP order
138  *      IN lp_quantization         quantization of LP coefficients in bits
139  *      OUT residual[0,data_len-1] residual signal
140  */
141 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[]);
142 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[]);
143 #ifndef FLAC__NO_ASM
144 #  ifdef FLAC__CPU_IA32
145 #    ifdef FLAC__HAS_NASM
146 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[]);
147 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[]);
148 #    endif
149 #  endif
150 #endif
151
152 #endif /* !defined FLAC__INTEGER_ONLY_LIBRARY */
153
154 /*
155  *      FLAC__lpc_restore_signal()
156  *      --------------------------------------------------------------------
157  *      Restore the original signal by summing the residual and the
158  *      predictor.
159  *
160  *      IN residual[0,data_len-1]  residual signal
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  *      *** IMPORTANT: the caller must pass in the historical samples:
166  *      IN  data[-order,-1]        previously-reconstructed historical samples
167  *      OUT data[0,data_len-1]     original signal
168  */
169 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[]);
170 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[]);
171 #ifndef FLAC__NO_ASM
172 #  ifdef FLAC__CPU_IA32
173 #    ifdef FLAC__HAS_NASM
174 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[]);
175 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[]);
176 #    endif /* FLAC__HAS_NASM */
177 #  elif defined FLAC__CPU_PPC
178 void FLAC__lpc_restore_signal_asm_ppc_altivec_16(const FLAC__int32 residual[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 data[]);
179 void FLAC__lpc_restore_signal_asm_ppc_altivec_16_order8(const FLAC__int32 residual[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 data[]);
180 #  endif/* FLAC__CPU_IA32 || FLAC__CPU_PPC */
181 #endif /* FLAC__NO_ASM */
182
183 #ifndef FLAC__INTEGER_ONLY_LIBRARY
184
185 /*
186  *      FLAC__lpc_compute_expected_bits_per_residual_sample()
187  *      --------------------------------------------------------------------
188  *      Compute the expected number of bits per residual signal sample
189  *      based on the LP error (which is related to the residual variance).
190  *
191  *      IN lpc_error >= 0.0   error returned from calculating LP coefficients
192  *      IN total_samples > 0  # of samples in residual signal
193  *      RETURN                expected bits per sample
194  */
195 FLAC__double FLAC__lpc_compute_expected_bits_per_residual_sample(FLAC__double lpc_error, unsigned total_samples);
196 FLAC__double FLAC__lpc_compute_expected_bits_per_residual_sample_with_error_scale(FLAC__double lpc_error, FLAC__double error_scale);
197
198 /*
199  *      FLAC__lpc_compute_best_order()
200  *      --------------------------------------------------------------------
201  *      Compute the best order from the array of signal errors returned
202  *      during coefficient computation.
203  *
204  *      IN lpc_error[0,max_order-1] >= 0.0  error returned from calculating LP coefficients
205  *      IN max_order > 0                    max LP order
206  *      IN total_samples > 0                # of samples in residual signal
207  *      IN overhead_bits_per_order          # of bits overhead for each increased LP order
208  *                                          (includes warmup sample size and quantized LP coefficient)
209  *      RETURN [1,max_order]                best order
210  */
211 unsigned FLAC__lpc_compute_best_order(const FLAC__double lpc_error[], unsigned max_order, unsigned total_samples, unsigned overhead_bits_per_order);
212
213 #endif /* !defined FLAC__INTEGER_ONLY_LIBRARY */
214
215 #endif