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