main_FLP.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /***********************************************************************
  2. Copyright (c) 2006-2011, Skype Limited. All rights reserved.
  3. Redistribution and use in source and binary forms, with or without
  4. modification, are permitted provided that the following conditions
  5. are met:
  6. - Redistributions of source code must retain the above copyright notice,
  7. this list of conditions and the following disclaimer.
  8. - Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in the
  10. documentation and/or other materials provided with the distribution.
  11. - Neither the name of Internet Society, IETF or IETF Trust, nor the
  12. names of specific contributors, may be used to endorse or promote
  13. products derived from this software without specific prior written
  14. permission.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  19. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  20. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  21. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  22. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  23. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  24. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  25. POSSIBILITY OF SUCH DAMAGE.
  26. ***********************************************************************/
  27. #ifndef SILK_MAIN_FLP_H
  28. #define SILK_MAIN_FLP_H
  29. #include "SigProc_FLP.h"
  30. #include "SigProc_FIX.h"
  31. #include "structs_FLP.h"
  32. #include "main.h"
  33. #include "define.h"
  34. #include "debug.h"
  35. #include "entenc.h"
  36. #ifdef __cplusplus
  37. extern "C"
  38. {
  39. #endif
  40. #define silk_encoder_state_Fxx silk_encoder_state_FLP
  41. #define silk_encode_do_VAD_Fxx silk_encode_do_VAD_FLP
  42. #define silk_encode_frame_Fxx silk_encode_frame_FLP
  43. /*********************/
  44. /* Encoder Functions */
  45. /*********************/
  46. /* High-pass filter with cutoff frequency adaptation based on pitch lag statistics */
  47. void silk_HP_variable_cutoff(
  48. silk_encoder_state_Fxx state_Fxx[] /* I/O Encoder states */
  49. );
  50. /* Encoder main function */
  51. void silk_encode_do_VAD_FLP(
  52. silk_encoder_state_FLP *psEnc /* I/O Encoder state FLP */
  53. );
  54. /* Encoder main function */
  55. opus_int silk_encode_frame_FLP(
  56. silk_encoder_state_FLP *psEnc, /* I/O Encoder state FLP */
  57. opus_int32 *pnBytesOut, /* O Number of payload bytes; */
  58. ec_enc *psRangeEnc, /* I/O compressor data structure */
  59. opus_int condCoding, /* I The type of conditional coding to use */
  60. opus_int maxBits, /* I If > 0: maximum number of output bits */
  61. opus_int useCBR /* I Flag to force constant-bitrate operation */
  62. );
  63. /* Initializes the Silk encoder state */
  64. opus_int silk_init_encoder(
  65. silk_encoder_state_FLP *psEnc, /* I/O Encoder state FLP */
  66. int arch /* I Run-tim architecture */
  67. );
  68. /* Control the Silk encoder */
  69. opus_int silk_control_encoder(
  70. silk_encoder_state_FLP *psEnc, /* I/O Pointer to Silk encoder state FLP */
  71. silk_EncControlStruct *encControl, /* I Control structure */
  72. const opus_int32 TargetRate_bps, /* I Target max bitrate (bps) */
  73. const opus_int allow_bw_switch, /* I Flag to allow switching audio bandwidth */
  74. const opus_int channelNb, /* I Channel number */
  75. const opus_int force_fs_kHz
  76. );
  77. /****************/
  78. /* Prefiltering */
  79. /****************/
  80. void silk_prefilter_FLP(
  81. silk_encoder_state_FLP *psEnc, /* I/O Encoder state FLP */
  82. const silk_encoder_control_FLP *psEncCtrl, /* I Encoder control FLP */
  83. silk_float xw[], /* O Weighted signal */
  84. const silk_float x[] /* I Speech signal */
  85. );
  86. /**************************/
  87. /* Noise shaping analysis */
  88. /**************************/
  89. /* Compute noise shaping coefficients and initial gain values */
  90. void silk_noise_shape_analysis_FLP(
  91. silk_encoder_state_FLP *psEnc, /* I/O Encoder state FLP */
  92. silk_encoder_control_FLP *psEncCtrl, /* I/O Encoder control FLP */
  93. const silk_float *pitch_res, /* I LPC residual from pitch analysis */
  94. const silk_float *x /* I Input signal [frame_length + la_shape] */
  95. );
  96. /* Autocorrelations for a warped frequency axis */
  97. void silk_warped_autocorrelation_FLP(
  98. silk_float *corr, /* O Result [order + 1] */
  99. const silk_float *input, /* I Input data to correlate */
  100. const silk_float warping, /* I Warping coefficient */
  101. const opus_int length, /* I Length of input */
  102. const opus_int order /* I Correlation order (even) */
  103. );
  104. /* Calculation of LTP state scaling */
  105. void silk_LTP_scale_ctrl_FLP(
  106. silk_encoder_state_FLP *psEnc, /* I/O Encoder state FLP */
  107. silk_encoder_control_FLP *psEncCtrl, /* I/O Encoder control FLP */
  108. opus_int condCoding /* I The type of conditional coding to use */
  109. );
  110. /**********************************************/
  111. /* Prediction Analysis */
  112. /**********************************************/
  113. /* Find pitch lags */
  114. void silk_find_pitch_lags_FLP(
  115. silk_encoder_state_FLP *psEnc, /* I/O Encoder state FLP */
  116. silk_encoder_control_FLP *psEncCtrl, /* I/O Encoder control FLP */
  117. silk_float res[], /* O Residual */
  118. const silk_float x[], /* I Speech signal */
  119. int arch /* I Run-time architecture */
  120. );
  121. /* Find LPC and LTP coefficients */
  122. void silk_find_pred_coefs_FLP(
  123. silk_encoder_state_FLP *psEnc, /* I/O Encoder state FLP */
  124. silk_encoder_control_FLP *psEncCtrl, /* I/O Encoder control FLP */
  125. const silk_float res_pitch[], /* I Residual from pitch analysis */
  126. const silk_float x[], /* I Speech signal */
  127. opus_int condCoding /* I The type of conditional coding to use */
  128. );
  129. /* LPC analysis */
  130. void silk_find_LPC_FLP(
  131. silk_encoder_state *psEncC, /* I/O Encoder state */
  132. opus_int16 NLSF_Q15[], /* O NLSFs */
  133. const silk_float x[], /* I Input signal */
  134. const silk_float minInvGain /* I Prediction gain from LTP (dB) */
  135. );
  136. /* LTP analysis */
  137. void silk_find_LTP_FLP(
  138. silk_float b[ MAX_NB_SUBFR * LTP_ORDER ], /* O LTP coefs */
  139. silk_float WLTP[ MAX_NB_SUBFR * LTP_ORDER * LTP_ORDER ], /* O Weight for LTP quantization */
  140. silk_float *LTPredCodGain, /* O LTP coding gain */
  141. const silk_float r_lpc[], /* I LPC residual */
  142. const opus_int lag[ MAX_NB_SUBFR ], /* I LTP lags */
  143. const silk_float Wght[ MAX_NB_SUBFR ], /* I Weights */
  144. const opus_int subfr_length, /* I Subframe length */
  145. const opus_int nb_subfr, /* I number of subframes */
  146. const opus_int mem_offset /* I Number of samples in LTP memory */
  147. );
  148. void silk_LTP_analysis_filter_FLP(
  149. silk_float *LTP_res, /* O LTP res MAX_NB_SUBFR*(pre_lgth+subfr_lngth) */
  150. const silk_float *x, /* I Input signal, with preceding samples */
  151. const silk_float B[ LTP_ORDER * MAX_NB_SUBFR ], /* I LTP coefficients for each subframe */
  152. const opus_int pitchL[ MAX_NB_SUBFR ], /* I Pitch lags */
  153. const silk_float invGains[ MAX_NB_SUBFR ], /* I Inverse quantization gains */
  154. const opus_int subfr_length, /* I Length of each subframe */
  155. const opus_int nb_subfr, /* I number of subframes */
  156. const opus_int pre_length /* I Preceding samples for each subframe */
  157. );
  158. /* Calculates residual energies of input subframes where all subframes have LPC_order */
  159. /* of preceding samples */
  160. void silk_residual_energy_FLP(
  161. silk_float nrgs[ MAX_NB_SUBFR ], /* O Residual energy per subframe */
  162. const silk_float x[], /* I Input signal */
  163. silk_float a[ 2 ][ MAX_LPC_ORDER ], /* I AR coefs for each frame half */
  164. const silk_float gains[], /* I Quantization gains */
  165. const opus_int subfr_length, /* I Subframe length */
  166. const opus_int nb_subfr, /* I number of subframes */
  167. const opus_int LPC_order /* I LPC order */
  168. );
  169. /* 16th order LPC analysis filter */
  170. void silk_LPC_analysis_filter_FLP(
  171. silk_float r_LPC[], /* O LPC residual signal */
  172. const silk_float PredCoef[], /* I LPC coefficients */
  173. const silk_float s[], /* I Input signal */
  174. const opus_int length, /* I Length of input signal */
  175. const opus_int Order /* I LPC order */
  176. );
  177. /* LTP tap quantizer */
  178. void silk_quant_LTP_gains_FLP(
  179. silk_float B[ MAX_NB_SUBFR * LTP_ORDER ], /* I/O (Un-)quantized LTP gains */
  180. opus_int8 cbk_index[ MAX_NB_SUBFR ], /* O Codebook index */
  181. opus_int8 *periodicity_index, /* O Periodicity index */
  182. opus_int32 *sum_log_gain_Q7, /* I/O Cumulative max prediction gain */
  183. const silk_float W[ MAX_NB_SUBFR * LTP_ORDER * LTP_ORDER ], /* I Error weights */
  184. const opus_int mu_Q10, /* I Mu value (R/D tradeoff) */
  185. const opus_int lowComplexity, /* I Flag for low complexity */
  186. const opus_int nb_subfr, /* I number of subframes */
  187. int arch /* I Run-time architecture */
  188. );
  189. /* Residual energy: nrg = wxx - 2 * wXx * c + c' * wXX * c */
  190. silk_float silk_residual_energy_covar_FLP( /* O Weighted residual energy */
  191. const silk_float *c, /* I Filter coefficients */
  192. silk_float *wXX, /* I/O Weighted correlation matrix, reg. out */
  193. const silk_float *wXx, /* I Weighted correlation vector */
  194. const silk_float wxx, /* I Weighted correlation value */
  195. const opus_int D /* I Dimension */
  196. );
  197. /* Processing of gains */
  198. void silk_process_gains_FLP(
  199. silk_encoder_state_FLP *psEnc, /* I/O Encoder state FLP */
  200. silk_encoder_control_FLP *psEncCtrl, /* I/O Encoder control FLP */
  201. opus_int condCoding /* I The type of conditional coding to use */
  202. );
  203. /******************/
  204. /* Linear Algebra */
  205. /******************/
  206. /* Calculates correlation matrix X'*X */
  207. void silk_corrMatrix_FLP(
  208. const silk_float *x, /* I x vector [ L+order-1 ] used to create X */
  209. const opus_int L, /* I Length of vectors */
  210. const opus_int Order, /* I Max lag for correlation */
  211. silk_float *XX /* O X'*X correlation matrix [order x order] */
  212. );
  213. /* Calculates correlation vector X'*t */
  214. void silk_corrVector_FLP(
  215. const silk_float *x, /* I x vector [L+order-1] used to create X */
  216. const silk_float *t, /* I Target vector [L] */
  217. const opus_int L, /* I Length of vecors */
  218. const opus_int Order, /* I Max lag for correlation */
  219. silk_float *Xt /* O X'*t correlation vector [order] */
  220. );
  221. /* Add noise to matrix diagonal */
  222. void silk_regularize_correlations_FLP(
  223. silk_float *XX, /* I/O Correlation matrices */
  224. silk_float *xx, /* I/O Correlation values */
  225. const silk_float noise, /* I Noise energy to add */
  226. const opus_int D /* I Dimension of XX */
  227. );
  228. /* Function to solve linear equation Ax = b, where A is an MxM symmetric matrix */
  229. void silk_solve_LDL_FLP(
  230. silk_float *A, /* I/O Symmetric square matrix, out: reg. */
  231. const opus_int M, /* I Size of matrix */
  232. const silk_float *b, /* I Pointer to b vector */
  233. silk_float *x /* O Pointer to x solution vector */
  234. );
  235. /* Apply sine window to signal vector. */
  236. /* Window types: */
  237. /* 1 -> sine window from 0 to pi/2 */
  238. /* 2 -> sine window from pi/2 to pi */
  239. void silk_apply_sine_window_FLP(
  240. silk_float px_win[], /* O Pointer to windowed signal */
  241. const silk_float px[], /* I Pointer to input signal */
  242. const opus_int win_type, /* I Selects a window type */
  243. const opus_int length /* I Window length, multiple of 4 */
  244. );
  245. /* Wrapper functions. Call flp / fix code */
  246. /* Convert AR filter coefficients to NLSF parameters */
  247. void silk_A2NLSF_FLP(
  248. opus_int16 *NLSF_Q15, /* O NLSF vector [ LPC_order ] */
  249. const silk_float *pAR, /* I LPC coefficients [ LPC_order ] */
  250. const opus_int LPC_order /* I LPC order */
  251. );
  252. /* Convert NLSF parameters to AR prediction filter coefficients */
  253. void silk_NLSF2A_FLP(
  254. silk_float *pAR, /* O LPC coefficients [ LPC_order ] */
  255. const opus_int16 *NLSF_Q15, /* I NLSF vector [ LPC_order ] */
  256. const opus_int LPC_order /* I LPC order */
  257. );
  258. /* Limit, stabilize, and quantize NLSFs */
  259. void silk_process_NLSFs_FLP(
  260. silk_encoder_state *psEncC, /* I/O Encoder state */
  261. silk_float PredCoef[ 2 ][ MAX_LPC_ORDER ], /* O Prediction coefficients */
  262. opus_int16 NLSF_Q15[ MAX_LPC_ORDER ], /* I/O Normalized LSFs (quant out) (0 - (2^15-1)) */
  263. const opus_int16 prev_NLSF_Q15[ MAX_LPC_ORDER ] /* I Previous Normalized LSFs (0 - (2^15-1)) */
  264. );
  265. /* Floating-point Silk NSQ wrapper */
  266. void silk_NSQ_wrapper_FLP(
  267. silk_encoder_state_FLP *psEnc, /* I/O Encoder state FLP */
  268. silk_encoder_control_FLP *psEncCtrl, /* I/O Encoder control FLP */
  269. SideInfoIndices *psIndices, /* I/O Quantization indices */
  270. silk_nsq_state *psNSQ, /* I/O Noise Shaping Quantzation state */
  271. opus_int8 pulses[], /* O Quantized pulse signal */
  272. const silk_float x[] /* I Prefiltered input signal */
  273. );
  274. #ifdef __cplusplus
  275. }
  276. #endif
  277. #endif