| 1 |
/* libFLAC - Free Lossless Audio Codec library |
| 2 |
* Copyright (C) 2000-2009 Josh Coalson |
| 3 |
* Copyright (C) 2011-2025 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 "flac_private_cpu.h" |
| 41 |
#include "flac_private_float.h" |
| 42 |
#include "flac_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_ARM64 && FLAC__HAS_NEONINTRIN && FLAC__HAS_A64NEONINTRIN |
| 91 |
void FLAC__lpc_compute_autocorrelation_intrin_neon_lag_8(const FLAC__real data[], uint32_t data_len, uint32_t lag, double autoc[]); |
| 92 |
void FLAC__lpc_compute_autocorrelation_intrin_neon_lag_10(const FLAC__real data[], uint32_t data_len, uint32_t lag, double autoc[]); |
| 93 |
void FLAC__lpc_compute_autocorrelation_intrin_neon_lag_14(const FLAC__real data[], uint32_t data_len, uint32_t lag, double autoc[]); |
| 94 |
#endif |
| 95 |
#endif /* FLAC__NO_ASM */ |
| 96 |
|
| 97 |
/* |
| 98 |
* FLAC__lpc_compute_lp_coefficients() |
| 99 |
* -------------------------------------------------------------------- |
| 100 |
* Computes LP coefficients for orders 1..max_order. |
| 101 |
* Do not call if autoc[0] == 0.0. This means the signal is zero |
| 102 |
* and there is no point in calculating a predictor. |
| 103 |
* |
| 104 |
* IN autoc[0,max_order] autocorrelation values |
| 105 |
* IN 0 < max_order <= FLAC__MAX_LPC_ORDER max LP order to compute |
| 106 |
* OUT lp_coeff[0,max_order-1][0,max_order-1] LP coefficients for each order |
| 107 |
* *** IMPORTANT: |
| 108 |
* *** lp_coeff[0,max_order-1][max_order,FLAC__MAX_LPC_ORDER-1] are untouched |
| 109 |
* OUT error[0,max_order-1] error for each order (more |
| 110 |
* specifically, the variance of |
| 111 |
* the error signal times # of |
| 112 |
* samples in the signal) |
| 113 |
* |
| 114 |
* Example: if max_order is 9, the LP coefficients for order 9 will be |
| 115 |
* in lp_coeff[8][0,8], the LP coefficients for order 8 will be |
| 116 |
* in lp_coeff[7][0,7], etc. |
| 117 |
*/ |
| 118 |
void FLAC__lpc_compute_lp_coefficients(const double autoc[], uint32_t *max_order, FLAC__real lp_coeff[][FLAC__MAX_LPC_ORDER], double error[]); |
| 119 |
|
| 120 |
/* |
| 121 |
* FLAC__lpc_quantize_coefficients() |
| 122 |
* -------------------------------------------------------------------- |
| 123 |
* Quantizes the LP coefficients. NOTE: precision + bits_per_sample |
| 124 |
* must be less than 32 (sizeof(FLAC__int32)*8). |
| 125 |
* |
| 126 |
* IN lp_coeff[0,order-1] LP coefficients |
| 127 |
* IN order LP order |
| 128 |
* IN FLAC__MIN_QLP_COEFF_PRECISION < precision |
| 129 |
* desired precision (in bits, including sign |
| 130 |
* bit) of largest coefficient |
| 131 |
* OUT qlp_coeff[0,order-1] quantized coefficients |
| 132 |
* OUT shift # of bits to shift right to get approximated |
| 133 |
* LP coefficients. NOTE: could be negative. |
| 134 |
* RETURN 0 => quantization OK |
| 135 |
* 1 => coefficients require too much shifting for *shift to |
| 136 |
* fit in the LPC subframe header. 'shift' is unset. |
| 137 |
* 2 => coefficients are all zero, which is bad. 'shift' is |
| 138 |
* unset. |
| 139 |
*/ |
| 140 |
int FLAC__lpc_quantize_coefficients(const FLAC__real lp_coeff[], uint32_t order, uint32_t precision, FLAC__int32 qlp_coeff[], int *shift); |
| 141 |
|
| 142 |
/* |
| 143 |
* FLAC__lpc_compute_residual_from_qlp_coefficients() |
| 144 |
* -------------------------------------------------------------------- |
| 145 |
* Compute the residual signal obtained from sutracting the predicted |
| 146 |
* signal from the original. |
| 147 |
* |
| 148 |
* IN data[-order,data_len-1] original signal (NOTE THE INDICES!) |
| 149 |
* IN data_len length of original signal |
| 150 |
* IN qlp_coeff[0,order-1] quantized LP coefficients |
| 151 |
* IN order > 0 LP order |
| 152 |
* IN lp_quantization quantization of LP coefficients in bits |
| 153 |
* OUT residual[0,data_len-1] residual signal |
| 154 |
*/ |
| 155 |
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 |
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 |
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 |
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 |
#ifndef FLAC__NO_ASM |
| 160 |
# ifdef FLAC__CPU_ARM64 |
| 161 |
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 |
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 |
# endif |
| 164 |
|
| 165 |
# if (defined FLAC__CPU_IA32 || defined FLAC__CPU_X86_64) && FLAC__HAS_X86INTRIN |
| 166 |
# ifdef FLAC__SSE2_SUPPORTED |
| 167 |
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 |
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 |
# endif |
| 170 |
# ifdef FLAC__SSE4_1_SUPPORTED |
| 171 |
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 |
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 |
# endif |
| 174 |
# ifdef FLAC__AVX2_SUPPORTED |
| 175 |
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 |
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 |
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 |
# endif |
| 179 |
# endif |
| 180 |
#endif |
| 181 |
|
| 182 |
#endif /* !defined FLAC__INTEGER_ONLY_LIBRARY */ |
| 183 |
|
| 184 |
FLAC__uint64 FLAC__lpc_max_prediction_value_before_shift(uint32_t subframe_bps, const FLAC__int32 qlp_coeff[], uint32_t order); |
| 185 |
uint32_t FLAC__lpc_max_prediction_before_shift_bps(uint32_t subframe_bps, const FLAC__int32 qlp_coeff[], uint32_t order); |
| 186 |
uint32_t FLAC__lpc_max_residual_bps(uint32_t subframe_bps, const FLAC__int32 qlp_coeff[], uint32_t order, int lp_quantization); |
| 187 |
|
| 188 |
/* |
| 189 |
* FLAC__lpc_restore_signal() |
| 190 |
* -------------------------------------------------------------------- |
| 191 |
* Restore the original signal by summing the residual and the |
| 192 |
* predictor. |
| 193 |
* |
| 194 |
* IN residual[0,data_len-1] residual signal |
| 195 |
* IN data_len length of original signal |
| 196 |
* IN qlp_coeff[0,order-1] quantized LP coefficients |
| 197 |
* IN order > 0 LP order |
| 198 |
* IN lp_quantization quantization of LP coefficients in bits |
| 199 |
* *** IMPORTANT: the caller must pass in the historical samples: |
| 200 |
* IN data[-order,-1] previously-reconstructed historical samples |
| 201 |
* OUT data[0,data_len-1] original signal |
| 202 |
*/ |
| 203 |
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[]); |
| 204 |
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[]); |
| 205 |
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[]); |
| 206 |
|
| 207 |
#ifndef FLAC__INTEGER_ONLY_LIBRARY |
| 208 |
|
| 209 |
/* |
| 210 |
* FLAC__lpc_compute_expected_bits_per_residual_sample() |
| 211 |
* -------------------------------------------------------------------- |
| 212 |
* Compute the expected number of bits per residual signal sample |
| 213 |
* based on the LP error (which is related to the residual variance). |
| 214 |
* |
| 215 |
* IN lpc_error >= 0.0 error returned from calculating LP coefficients |
| 216 |
* IN total_samples > 0 # of samples in residual signal |
| 217 |
* RETURN expected bits per sample |
| 218 |
*/ |
| 219 |
double FLAC__lpc_compute_expected_bits_per_residual_sample(double lpc_error, uint32_t total_samples); |
| 220 |
double FLAC__lpc_compute_expected_bits_per_residual_sample_with_error_scale(double lpc_error, double error_scale); |
| 221 |
|
| 222 |
/* |
| 223 |
* FLAC__lpc_compute_best_order() |
| 224 |
* -------------------------------------------------------------------- |
| 225 |
* Compute the best order from the array of signal errors returned |
| 226 |
* during coefficient computation. |
| 227 |
* |
| 228 |
* IN lpc_error[0,max_order-1] >= 0.0 error returned from calculating LP coefficients |
| 229 |
* IN max_order > 0 max LP order |
| 230 |
* IN total_samples > 0 # of samples in residual signal |
| 231 |
* IN overhead_bits_per_order # of bits overhead for each increased LP order |
| 232 |
* (includes warmup sample size and quantized LP coefficient) |
| 233 |
* RETURN [1,max_order] best order |
| 234 |
*/ |
| 235 |
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); |
| 236 |
|
| 237 |
#endif /* !defined FLAC__INTEGER_ONLY_LIBRARY */ |
| 238 |
|
| 239 |
#endif |