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