flac_private_lpc.h 11 KB

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