| 1 | /* libFLAC - Free Lossless Audio Codec library |
| 2 | * Copyright (C) 2000-2009 Josh Coalson |
| 3 | * Copyright (C) 2011-2016 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[], unsigned data_len); |
| 58 | |
| 59 | /* |
| 60 | * FLAC__lpc_compute_autocorrelation() |
| 61 | * -------------------------------------------------------------------- |
| 62 | * Compute the autocorrelation for lags between 0 and lag-1. |
| 63 | * Assumes data[] outside of [0,data_len-1] == 0. |
| 64 | * Asserts that lag > 0. |
| 65 | * |
| 66 | * IN data[0,data_len-1] |
| 67 | * IN data_len |
| 68 | * IN 0 < lag <= data_len |
| 69 | * OUT autoc[0,lag-1] |
| 70 | */ |
| 71 | void FLAC__lpc_compute_autocorrelation(const FLAC__real data[], unsigned data_len, unsigned lag, FLAC__real autoc[]); |
| 72 | #ifndef FLAC__NO_ASM |
| 73 | # ifdef FLAC__CPU_IA32 |
| 74 | # ifdef FLAC__HAS_NASM |
| 75 | void FLAC__lpc_compute_autocorrelation_asm_ia32(const FLAC__real data[], unsigned data_len, unsigned lag, FLAC__real autoc[]); |
| 76 | void FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_4_old(const FLAC__real data[], unsigned data_len, unsigned lag, FLAC__real autoc[]); |
| 77 | void FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_8_old(const FLAC__real data[], unsigned data_len, unsigned lag, FLAC__real autoc[]); |
| 78 | void FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_12_old(const FLAC__real data[], unsigned data_len, unsigned lag, FLAC__real autoc[]); |
| 79 | void FLAC__lpc_compute_autocorrelation_asm_ia32_sse_lag_16_old(const FLAC__real data[], unsigned data_len, unsigned lag, FLAC__real autoc[]); |
| 80 | # endif |
| 81 | # endif |
| 82 | # if (defined FLAC__CPU_IA32 || defined FLAC__CPU_X86_64) && FLAC__HAS_X86INTRIN |
| 83 | # ifdef FLAC__SSE_SUPPORTED |
| 84 | void FLAC__lpc_compute_autocorrelation_intrin_sse_lag_4_old(const FLAC__real data[], unsigned data_len, unsigned lag, FLAC__real autoc[]); |
| 85 | void FLAC__lpc_compute_autocorrelation_intrin_sse_lag_8_old(const FLAC__real data[], unsigned data_len, unsigned lag, FLAC__real autoc[]); |
| 86 | void FLAC__lpc_compute_autocorrelation_intrin_sse_lag_12_old(const FLAC__real data[], unsigned data_len, unsigned lag, FLAC__real autoc[]); |
| 87 | void FLAC__lpc_compute_autocorrelation_intrin_sse_lag_16_old(const FLAC__real data[], unsigned data_len, unsigned lag, FLAC__real autoc[]); |
| 88 | void FLAC__lpc_compute_autocorrelation_intrin_sse_lag_4_new(const FLAC__real data[], unsigned data_len, unsigned lag, FLAC__real autoc[]); |
| 89 | void FLAC__lpc_compute_autocorrelation_intrin_sse_lag_8_new(const FLAC__real data[], unsigned data_len, unsigned lag, FLAC__real autoc[]); |
| 90 | void FLAC__lpc_compute_autocorrelation_intrin_sse_lag_12_new(const FLAC__real data[], unsigned data_len, unsigned lag, FLAC__real autoc[]); |
| 91 | void FLAC__lpc_compute_autocorrelation_intrin_sse_lag_16_new(const FLAC__real data[], unsigned data_len, unsigned lag, FLAC__real autoc[]); |
| 92 | # endif |
| 93 | # endif |
| 94 | #endif |
| 95 | |
| 96 | /* |
| 97 | * FLAC__lpc_compute_lp_coefficients() |
| 98 | * -------------------------------------------------------------------- |
| 99 | * Computes LP coefficients for orders 1..max_order. |
| 100 | * Do not call if autoc[0] == 0.0. This means the signal is zero |
| 101 | * and there is no point in calculating a predictor. |
| 102 | * |
| 103 | * IN autoc[0,max_order] autocorrelation values |
| 104 | * IN 0 < max_order <= FLAC__MAX_LPC_ORDER max LP order to compute |
| 105 | * OUT lp_coeff[0,max_order-1][0,max_order-1] LP coefficients for each order |
| 106 | * *** IMPORTANT: |
| 107 | * *** lp_coeff[0,max_order-1][max_order,FLAC__MAX_LPC_ORDER-1] are untouched |
| 108 | * OUT error[0,max_order-1] error for each order (more |
| 109 | * specifically, the variance of |
| 110 | * the error signal times # of |
| 111 | * samples in the signal) |
| 112 | * |
| 113 | * Example: if max_order is 9, the LP coefficients for order 9 will be |
| 114 | * in lp_coeff[8][0,8], the LP coefficients for order 8 will be |
| 115 | * in lp_coeff[7][0,7], etc. |
| 116 | */ |
| 117 | void FLAC__lpc_compute_lp_coefficients(const FLAC__real autoc[], unsigned *max_order, FLAC__real lp_coeff[][FLAC__MAX_LPC_ORDER], double error[]); |
| 118 | |
| 119 | /* |
| 120 | * FLAC__lpc_quantize_coefficients() |
| 121 | * -------------------------------------------------------------------- |
| 122 | * Quantizes the LP coefficients. NOTE: precision + bits_per_sample |
| 123 | * must be less than 32 (sizeof(FLAC__int32)*8). |
| 124 | * |
| 125 | * IN lp_coeff[0,order-1] LP coefficients |
| 126 | * IN order LP order |
| 127 | * IN FLAC__MIN_QLP_COEFF_PRECISION < precision |
| 128 | * desired precision (in bits, including sign |
| 129 | * bit) of largest coefficient |
| 130 | * OUT qlp_coeff[0,order-1] quantized coefficients |
| 131 | * OUT shift # of bits to shift right to get approximated |
| 132 | * LP coefficients. NOTE: could be negative. |
| 133 | * RETURN 0 => quantization OK |
| 134 | * 1 => coefficients require too much shifting for *shift to |
| 135 | * fit in the LPC subframe header. 'shift' is unset. |
| 136 | * 2 => coefficients are all zero, which is bad. 'shift' is |
| 137 | * unset. |
| 138 | */ |
| 139 | int FLAC__lpc_quantize_coefficients(const FLAC__real lp_coeff[], unsigned order, unsigned precision, FLAC__int32 qlp_coeff[], int *shift); |
| 140 | |
| 141 | /* |
| 142 | * FLAC__lpc_compute_residual_from_qlp_coefficients() |
| 143 | * -------------------------------------------------------------------- |
| 144 | * Compute the residual signal obtained from sutracting the predicted |
| 145 | * signal from the original. |
| 146 | * |
| 147 | * IN data[-order,data_len-1] original signal (NOTE THE INDICES!) |
| 148 | * IN data_len length of original signal |
| 149 | * IN qlp_coeff[0,order-1] quantized LP coefficients |
| 150 | * IN order > 0 LP order |
| 151 | * IN lp_quantization quantization of LP coefficients in bits |
| 152 | * OUT residual[0,data_len-1] residual signal |
| 153 | */ |
| 154 | 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[]); |
| 155 | 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[]); |
| 156 | #ifndef FLAC__NO_ASM |
| 157 | # ifdef FLAC__CPU_IA32 |
| 158 | # ifdef FLAC__HAS_NASM |
| 159 | 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[]); |
| 160 | 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[]); |
| 161 | void FLAC__lpc_compute_residual_from_qlp_coefficients_wide_asm_ia32(const FLAC__int32 *data, unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 residual[]); |
| 162 | # endif |
| 163 | # endif |
| 164 | # if (defined FLAC__CPU_IA32 || defined FLAC__CPU_X86_64) && FLAC__HAS_X86INTRIN |
| 165 | # ifdef FLAC__SSE2_SUPPORTED |
| 166 | void FLAC__lpc_compute_residual_from_qlp_coefficients_16_intrin_sse2(const FLAC__int32 *data, unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 residual[]); |
| 167 | void FLAC__lpc_compute_residual_from_qlp_coefficients_intrin_sse2(const FLAC__int32 *data, unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 residual[]); |
| 168 | # endif |
| 169 | # ifdef FLAC__SSE4_1_SUPPORTED |
| 170 | void FLAC__lpc_compute_residual_from_qlp_coefficients_intrin_sse41(const FLAC__int32 *data, unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 residual[]); |
| 171 | void FLAC__lpc_compute_residual_from_qlp_coefficients_wide_intrin_sse41(const FLAC__int32 *data, unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 residual[]); |
| 172 | # endif |
| 173 | # ifdef FLAC__AVX2_SUPPORTED |
| 174 | void FLAC__lpc_compute_residual_from_qlp_coefficients_16_intrin_avx2(const FLAC__int32 *data, unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 residual[]); |
| 175 | void FLAC__lpc_compute_residual_from_qlp_coefficients_intrin_avx2(const FLAC__int32 *data, unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 residual[]); |
| 176 | void FLAC__lpc_compute_residual_from_qlp_coefficients_wide_intrin_avx2(const FLAC__int32 *data, unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 residual[]); |
| 177 | # endif |
| 178 | # endif |
| 179 | #endif |
| 180 | |
| 181 | #endif /* !defined FLAC__INTEGER_ONLY_LIBRARY */ |
| 182 | |
| 183 | /* |
| 184 | * FLAC__lpc_restore_signal() |
| 185 | * -------------------------------------------------------------------- |
| 186 | * Restore the original signal by summing the residual and the |
| 187 | * predictor. |
| 188 | * |
| 189 | * IN residual[0,data_len-1] residual signal |
| 190 | * IN data_len length of original signal |
| 191 | * IN qlp_coeff[0,order-1] quantized LP coefficients |
| 192 | * IN order > 0 LP order |
| 193 | * IN lp_quantization quantization of LP coefficients in bits |
| 194 | * *** IMPORTANT: the caller must pass in the historical samples: |
| 195 | * IN data[-order,-1] previously-reconstructed historical samples |
| 196 | * OUT data[0,data_len-1] original signal |
| 197 | */ |
| 198 | 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[]); |
| 199 | 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[]); |
| 200 | #ifndef FLAC__NO_ASM |
| 201 | # ifdef FLAC__CPU_IA32 |
| 202 | # ifdef FLAC__HAS_NASM |
| 203 | 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[]); |
| 204 | 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[]); |
| 205 | void FLAC__lpc_restore_signal_wide_asm_ia32(const FLAC__int32 residual[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 data[]); |
| 206 | # endif /* FLAC__HAS_NASM */ |
| 207 | # endif /* FLAC__CPU_IA32 */ |
| 208 | # if (defined FLAC__CPU_IA32 || defined FLAC__CPU_X86_64) && FLAC__HAS_X86INTRIN |
| 209 | # ifdef FLAC__SSE2_SUPPORTED |
| 210 | void FLAC__lpc_restore_signal_16_intrin_sse2(const FLAC__int32 residual[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 data[]); |
| 211 | # endif |
| 212 | # ifdef FLAC__SSE4_1_SUPPORTED |
| 213 | void FLAC__lpc_restore_signal_wide_intrin_sse41(const FLAC__int32 residual[], unsigned data_len, const FLAC__int32 qlp_coeff[], unsigned order, int lp_quantization, FLAC__int32 data[]); |
| 214 | # endif |
| 215 | # endif |
| 216 | #endif /* FLAC__NO_ASM */ |
| 217 | |
| 218 | #ifndef FLAC__INTEGER_ONLY_LIBRARY |
| 219 | |
| 220 | /* |
| 221 | * FLAC__lpc_compute_expected_bits_per_residual_sample() |
| 222 | * -------------------------------------------------------------------- |
| 223 | * Compute the expected number of bits per residual signal sample |
| 224 | * based on the LP error (which is related to the residual variance). |
| 225 | * |
| 226 | * IN lpc_error >= 0.0 error returned from calculating LP coefficients |
| 227 | * IN total_samples > 0 # of samples in residual signal |
| 228 | * RETURN expected bits per sample |
| 229 | */ |
| 230 | double FLAC__lpc_compute_expected_bits_per_residual_sample(double lpc_error, unsigned total_samples); |
| 231 | double FLAC__lpc_compute_expected_bits_per_residual_sample_with_error_scale(double lpc_error, double error_scale); |
| 232 | |
| 233 | /* |
| 234 | * FLAC__lpc_compute_best_order() |
| 235 | * -------------------------------------------------------------------- |
| 236 | * Compute the best order from the array of signal errors returned |
| 237 | * during coefficient computation. |
| 238 | * |
| 239 | * IN lpc_error[0,max_order-1] >= 0.0 error returned from calculating LP coefficients |
| 240 | * IN max_order > 0 max LP order |
| 241 | * IN total_samples > 0 # of samples in residual signal |
| 242 | * IN overhead_bits_per_order # of bits overhead for each increased LP order |
| 243 | * (includes warmup sample size and quantized LP coefficient) |
| 244 | * RETURN [1,max_order] best order |
| 245 | */ |
| 246 | unsigned FLAC__lpc_compute_best_order(const double lpc_error[], unsigned max_order, unsigned total_samples, unsigned overhead_bits_per_order); |
| 247 | |
| 248 | #endif /* !defined FLAC__INTEGER_ONLY_LIBRARY */ |
| 249 | |
| 250 | #endif |