2
0

pitch_analysis_core_FLP.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  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. #ifdef HAVE_CONFIG_H
  28. #include "config.h"
  29. #endif
  30. /*****************************************************************************
  31. * Pitch analyser function
  32. ******************************************************************************/
  33. #include "SigProc_FLP.h"
  34. #include "SigProc_FIX.h"
  35. #include "pitch_est_defines.h"
  36. #include "pitch.h"
  37. #define SCRATCH_SIZE 22
  38. /************************************************************/
  39. /* Internally used functions */
  40. /************************************************************/
  41. static void silk_P_Ana_calc_corr_st3(
  42. silk_float cross_corr_st3[ PE_MAX_NB_SUBFR ][ PE_NB_CBKS_STAGE3_MAX ][ PE_NB_STAGE3_LAGS ], /* O 3 DIM correlation array */
  43. const silk_float frame[], /* I vector to correlate */
  44. opus_int start_lag, /* I start lag */
  45. opus_int sf_length, /* I sub frame length */
  46. opus_int nb_subfr, /* I number of subframes */
  47. opus_int complexity, /* I Complexity setting */
  48. int arch /* I Run-time architecture */
  49. );
  50. static void silk_P_Ana_calc_energy_st3(
  51. silk_float energies_st3[ PE_MAX_NB_SUBFR ][ PE_NB_CBKS_STAGE3_MAX ][ PE_NB_STAGE3_LAGS ], /* O 3 DIM correlation array */
  52. const silk_float frame[], /* I vector to correlate */
  53. opus_int start_lag, /* I start lag */
  54. opus_int sf_length, /* I sub frame length */
  55. opus_int nb_subfr, /* I number of subframes */
  56. opus_int complexity /* I Complexity setting */
  57. );
  58. /************************************************************/
  59. /* CORE PITCH ANALYSIS FUNCTION */
  60. /************************************************************/
  61. opus_int silk_pitch_analysis_core_FLP( /* O Voicing estimate: 0 voiced, 1 unvoiced */
  62. const silk_float *frame, /* I Signal of length PE_FRAME_LENGTH_MS*Fs_kHz */
  63. opus_int *pitch_out, /* O Pitch lag values [nb_subfr] */
  64. opus_int16 *lagIndex, /* O Lag Index */
  65. opus_int8 *contourIndex, /* O Pitch contour Index */
  66. silk_float *LTPCorr, /* I/O Normalized correlation; input: value from previous frame */
  67. opus_int prevLag, /* I Last lag of previous frame; set to zero is unvoiced */
  68. const silk_float search_thres1, /* I First stage threshold for lag candidates 0 - 1 */
  69. const silk_float search_thres2, /* I Final threshold for lag candidates 0 - 1 */
  70. const opus_int Fs_kHz, /* I sample frequency (kHz) */
  71. const opus_int complexity, /* I Complexity setting, 0-2, where 2 is highest */
  72. const opus_int nb_subfr, /* I Number of 5 ms subframes */
  73. int arch /* I Run-time architecture */
  74. )
  75. {
  76. opus_int i, k, d, j;
  77. silk_float frame_8kHz[ PE_MAX_FRAME_LENGTH_MS * 8 ];
  78. silk_float frame_4kHz[ PE_MAX_FRAME_LENGTH_MS * 4 ];
  79. opus_int16 frame_8_FIX[ PE_MAX_FRAME_LENGTH_MS * 8 ];
  80. opus_int16 frame_4_FIX[ PE_MAX_FRAME_LENGTH_MS * 4 ];
  81. opus_int32 filt_state[ 6 ];
  82. silk_float threshold, contour_bias;
  83. silk_float C[ PE_MAX_NB_SUBFR][ (PE_MAX_LAG >> 1) + 5 ];
  84. opus_val32 xcorr[ PE_MAX_LAG_MS * 4 - PE_MIN_LAG_MS * 4 + 1 ];
  85. silk_float CC[ PE_NB_CBKS_STAGE2_EXT ];
  86. const silk_float *target_ptr, *basis_ptr;
  87. double cross_corr, normalizer, energy, energy_tmp;
  88. opus_int d_srch[ PE_D_SRCH_LENGTH ];
  89. opus_int16 d_comp[ (PE_MAX_LAG >> 1) + 5 ];
  90. opus_int length_d_srch, length_d_comp;
  91. silk_float Cmax, CCmax, CCmax_b, CCmax_new_b, CCmax_new;
  92. opus_int CBimax, CBimax_new, lag, start_lag, end_lag, lag_new;
  93. opus_int cbk_size;
  94. silk_float lag_log2, prevLag_log2, delta_lag_log2_sqr;
  95. silk_float energies_st3[ PE_MAX_NB_SUBFR ][ PE_NB_CBKS_STAGE3_MAX ][ PE_NB_STAGE3_LAGS ];
  96. silk_float cross_corr_st3[ PE_MAX_NB_SUBFR ][ PE_NB_CBKS_STAGE3_MAX ][ PE_NB_STAGE3_LAGS ];
  97. opus_int lag_counter;
  98. opus_int frame_length, frame_length_8kHz, frame_length_4kHz;
  99. opus_int sf_length, sf_length_8kHz, sf_length_4kHz;
  100. opus_int min_lag, min_lag_8kHz, min_lag_4kHz;
  101. opus_int max_lag, max_lag_8kHz, max_lag_4kHz;
  102. opus_int nb_cbk_search;
  103. const opus_int8 *Lag_CB_ptr;
  104. /* Check for valid sampling frequency */
  105. celt_assert( Fs_kHz == 8 || Fs_kHz == 12 || Fs_kHz == 16 );
  106. /* Check for valid complexity setting */
  107. celt_assert( complexity >= SILK_PE_MIN_COMPLEX );
  108. celt_assert( complexity <= SILK_PE_MAX_COMPLEX );
  109. silk_assert( search_thres1 >= 0.0f && search_thres1 <= 1.0f );
  110. silk_assert( search_thres2 >= 0.0f && search_thres2 <= 1.0f );
  111. /* Set up frame lengths max / min lag for the sampling frequency */
  112. frame_length = ( PE_LTP_MEM_LENGTH_MS + nb_subfr * PE_SUBFR_LENGTH_MS ) * Fs_kHz;
  113. frame_length_4kHz = ( PE_LTP_MEM_LENGTH_MS + nb_subfr * PE_SUBFR_LENGTH_MS ) * 4;
  114. frame_length_8kHz = ( PE_LTP_MEM_LENGTH_MS + nb_subfr * PE_SUBFR_LENGTH_MS ) * 8;
  115. sf_length = PE_SUBFR_LENGTH_MS * Fs_kHz;
  116. sf_length_4kHz = PE_SUBFR_LENGTH_MS * 4;
  117. sf_length_8kHz = PE_SUBFR_LENGTH_MS * 8;
  118. min_lag = PE_MIN_LAG_MS * Fs_kHz;
  119. min_lag_4kHz = PE_MIN_LAG_MS * 4;
  120. min_lag_8kHz = PE_MIN_LAG_MS * 8;
  121. max_lag = PE_MAX_LAG_MS * Fs_kHz - 1;
  122. max_lag_4kHz = PE_MAX_LAG_MS * 4;
  123. max_lag_8kHz = PE_MAX_LAG_MS * 8 - 1;
  124. /* Resample from input sampled at Fs_kHz to 8 kHz */
  125. if( Fs_kHz == 16 ) {
  126. /* Resample to 16 -> 8 khz */
  127. opus_int16 frame_16_FIX[ 16 * PE_MAX_FRAME_LENGTH_MS ];
  128. silk_float2short_array( frame_16_FIX, frame, frame_length );
  129. silk_memset( filt_state, 0, 2 * sizeof( opus_int32 ) );
  130. silk_resampler_down2( filt_state, frame_8_FIX, frame_16_FIX, frame_length );
  131. silk_short2float_array( frame_8kHz, frame_8_FIX, frame_length_8kHz );
  132. } else if( Fs_kHz == 12 ) {
  133. /* Resample to 12 -> 8 khz */
  134. opus_int16 frame_12_FIX[ 12 * PE_MAX_FRAME_LENGTH_MS ];
  135. silk_float2short_array( frame_12_FIX, frame, frame_length );
  136. silk_memset( filt_state, 0, 6 * sizeof( opus_int32 ) );
  137. silk_resampler_down2_3( filt_state, frame_8_FIX, frame_12_FIX, frame_length );
  138. silk_short2float_array( frame_8kHz, frame_8_FIX, frame_length_8kHz );
  139. } else {
  140. celt_assert( Fs_kHz == 8 );
  141. silk_float2short_array( frame_8_FIX, frame, frame_length_8kHz );
  142. }
  143. /* Decimate again to 4 kHz */
  144. silk_memset( filt_state, 0, 2 * sizeof( opus_int32 ) );
  145. silk_resampler_down2( filt_state, frame_4_FIX, frame_8_FIX, frame_length_8kHz );
  146. silk_short2float_array( frame_4kHz, frame_4_FIX, frame_length_4kHz );
  147. /* Low-pass filter */
  148. for( i = frame_length_4kHz - 1; i > 0; i-- ) {
  149. frame_4kHz[ i ] = silk_ADD_SAT16( frame_4kHz[ i ], frame_4kHz[ i - 1 ] );
  150. }
  151. /******************************************************************************
  152. * FIRST STAGE, operating in 4 khz
  153. ******************************************************************************/
  154. silk_memset(C, 0, sizeof(silk_float) * nb_subfr * ((PE_MAX_LAG >> 1) + 5));
  155. target_ptr = &frame_4kHz[ silk_LSHIFT( sf_length_4kHz, 2 ) ];
  156. for( k = 0; k < nb_subfr >> 1; k++ ) {
  157. /* Check that we are within range of the array */
  158. celt_assert( target_ptr >= frame_4kHz );
  159. celt_assert( target_ptr + sf_length_8kHz <= frame_4kHz + frame_length_4kHz );
  160. basis_ptr = target_ptr - min_lag_4kHz;
  161. /* Check that we are within range of the array */
  162. celt_assert( basis_ptr >= frame_4kHz );
  163. celt_assert( basis_ptr + sf_length_8kHz <= frame_4kHz + frame_length_4kHz );
  164. celt_pitch_xcorr( target_ptr, target_ptr-max_lag_4kHz, xcorr, sf_length_8kHz, max_lag_4kHz - min_lag_4kHz + 1, arch );
  165. /* Calculate first vector products before loop */
  166. cross_corr = xcorr[ max_lag_4kHz - min_lag_4kHz ];
  167. normalizer = silk_energy_FLP( target_ptr, sf_length_8kHz ) +
  168. silk_energy_FLP( basis_ptr, sf_length_8kHz ) +
  169. sf_length_8kHz * 4000.0f;
  170. C[ 0 ][ min_lag_4kHz ] += (silk_float)( 2 * cross_corr / normalizer );
  171. /* From now on normalizer is computed recursively */
  172. for( d = min_lag_4kHz + 1; d <= max_lag_4kHz; d++ ) {
  173. basis_ptr--;
  174. /* Check that we are within range of the array */
  175. silk_assert( basis_ptr >= frame_4kHz );
  176. silk_assert( basis_ptr + sf_length_8kHz <= frame_4kHz + frame_length_4kHz );
  177. cross_corr = xcorr[ max_lag_4kHz - d ];
  178. /* Add contribution of new sample and remove contribution from oldest sample */
  179. normalizer +=
  180. basis_ptr[ 0 ] * (double)basis_ptr[ 0 ] -
  181. basis_ptr[ sf_length_8kHz ] * (double)basis_ptr[ sf_length_8kHz ];
  182. C[ 0 ][ d ] += (silk_float)( 2 * cross_corr / normalizer );
  183. }
  184. /* Update target pointer */
  185. target_ptr += sf_length_8kHz;
  186. }
  187. /* Apply short-lag bias */
  188. for( i = max_lag_4kHz; i >= min_lag_4kHz; i-- ) {
  189. C[ 0 ][ i ] -= C[ 0 ][ i ] * i / 4096.0f;
  190. }
  191. /* Sort */
  192. length_d_srch = 4 + 2 * complexity;
  193. celt_assert( 3 * length_d_srch <= PE_D_SRCH_LENGTH );
  194. silk_insertion_sort_decreasing_FLP( &C[ 0 ][ min_lag_4kHz ], d_srch, max_lag_4kHz - min_lag_4kHz + 1, length_d_srch );
  195. /* Escape if correlation is very low already here */
  196. Cmax = C[ 0 ][ min_lag_4kHz ];
  197. if( Cmax < 0.2f ) {
  198. silk_memset( pitch_out, 0, nb_subfr * sizeof( opus_int ) );
  199. *LTPCorr = 0.0f;
  200. *lagIndex = 0;
  201. *contourIndex = 0;
  202. return 1;
  203. }
  204. threshold = search_thres1 * Cmax;
  205. for( i = 0; i < length_d_srch; i++ ) {
  206. /* Convert to 8 kHz indices for the sorted correlation that exceeds the threshold */
  207. if( C[ 0 ][ min_lag_4kHz + i ] > threshold ) {
  208. d_srch[ i ] = silk_LSHIFT( d_srch[ i ] + min_lag_4kHz, 1 );
  209. } else {
  210. length_d_srch = i;
  211. break;
  212. }
  213. }
  214. celt_assert( length_d_srch > 0 );
  215. for( i = min_lag_8kHz - 5; i < max_lag_8kHz + 5; i++ ) {
  216. d_comp[ i ] = 0;
  217. }
  218. for( i = 0; i < length_d_srch; i++ ) {
  219. d_comp[ d_srch[ i ] ] = 1;
  220. }
  221. /* Convolution */
  222. for( i = max_lag_8kHz + 3; i >= min_lag_8kHz; i-- ) {
  223. d_comp[ i ] += d_comp[ i - 1 ] + d_comp[ i - 2 ];
  224. }
  225. length_d_srch = 0;
  226. for( i = min_lag_8kHz; i < max_lag_8kHz + 1; i++ ) {
  227. if( d_comp[ i + 1 ] > 0 ) {
  228. d_srch[ length_d_srch ] = i;
  229. length_d_srch++;
  230. }
  231. }
  232. /* Convolution */
  233. for( i = max_lag_8kHz + 3; i >= min_lag_8kHz; i-- ) {
  234. d_comp[ i ] += d_comp[ i - 1 ] + d_comp[ i - 2 ] + d_comp[ i - 3 ];
  235. }
  236. length_d_comp = 0;
  237. for( i = min_lag_8kHz; i < max_lag_8kHz + 4; i++ ) {
  238. if( d_comp[ i ] > 0 ) {
  239. d_comp[ length_d_comp ] = (opus_int16)( i - 2 );
  240. length_d_comp++;
  241. }
  242. }
  243. /**********************************************************************************
  244. ** SECOND STAGE, operating at 8 kHz, on lag sections with high correlation
  245. *************************************************************************************/
  246. /*********************************************************************************
  247. * Find energy of each subframe projected onto its history, for a range of delays
  248. *********************************************************************************/
  249. silk_memset( C, 0, PE_MAX_NB_SUBFR*((PE_MAX_LAG >> 1) + 5) * sizeof(silk_float));
  250. if( Fs_kHz == 8 ) {
  251. target_ptr = &frame[ PE_LTP_MEM_LENGTH_MS * 8 ];
  252. } else {
  253. target_ptr = &frame_8kHz[ PE_LTP_MEM_LENGTH_MS * 8 ];
  254. }
  255. for( k = 0; k < nb_subfr; k++ ) {
  256. energy_tmp = silk_energy_FLP( target_ptr, sf_length_8kHz ) + 1.0;
  257. for( j = 0; j < length_d_comp; j++ ) {
  258. d = d_comp[ j ];
  259. basis_ptr = target_ptr - d;
  260. cross_corr = silk_inner_product_FLP( basis_ptr, target_ptr, sf_length_8kHz );
  261. if( cross_corr > 0.0f ) {
  262. energy = silk_energy_FLP( basis_ptr, sf_length_8kHz );
  263. C[ k ][ d ] = (silk_float)( 2 * cross_corr / ( energy + energy_tmp ) );
  264. } else {
  265. C[ k ][ d ] = 0.0f;
  266. }
  267. }
  268. target_ptr += sf_length_8kHz;
  269. }
  270. /* search over lag range and lags codebook */
  271. /* scale factor for lag codebook, as a function of center lag */
  272. CCmax = 0.0f; /* This value doesn't matter */
  273. CCmax_b = -1000.0f;
  274. CBimax = 0; /* To avoid returning undefined lag values */
  275. lag = -1; /* To check if lag with strong enough correlation has been found */
  276. if( prevLag > 0 ) {
  277. if( Fs_kHz == 12 ) {
  278. prevLag = silk_LSHIFT( prevLag, 1 ) / 3;
  279. } else if( Fs_kHz == 16 ) {
  280. prevLag = silk_RSHIFT( prevLag, 1 );
  281. }
  282. prevLag_log2 = silk_log2( (silk_float)prevLag );
  283. } else {
  284. prevLag_log2 = 0;
  285. }
  286. /* Set up stage 2 codebook based on number of subframes */
  287. if( nb_subfr == PE_MAX_NB_SUBFR ) {
  288. cbk_size = PE_NB_CBKS_STAGE2_EXT;
  289. Lag_CB_ptr = &silk_CB_lags_stage2[ 0 ][ 0 ];
  290. if( Fs_kHz == 8 && complexity > SILK_PE_MIN_COMPLEX ) {
  291. /* If input is 8 khz use a larger codebook here because it is last stage */
  292. nb_cbk_search = PE_NB_CBKS_STAGE2_EXT;
  293. } else {
  294. nb_cbk_search = PE_NB_CBKS_STAGE2;
  295. }
  296. } else {
  297. cbk_size = PE_NB_CBKS_STAGE2_10MS;
  298. Lag_CB_ptr = &silk_CB_lags_stage2_10_ms[ 0 ][ 0 ];
  299. nb_cbk_search = PE_NB_CBKS_STAGE2_10MS;
  300. }
  301. for( k = 0; k < length_d_srch; k++ ) {
  302. d = d_srch[ k ];
  303. for( j = 0; j < nb_cbk_search; j++ ) {
  304. CC[j] = 0.0f;
  305. for( i = 0; i < nb_subfr; i++ ) {
  306. /* Try all codebooks */
  307. CC[ j ] += C[ i ][ d + matrix_ptr( Lag_CB_ptr, i, j, cbk_size )];
  308. }
  309. }
  310. /* Find best codebook */
  311. CCmax_new = -1000.0f;
  312. CBimax_new = 0;
  313. for( i = 0; i < nb_cbk_search; i++ ) {
  314. if( CC[ i ] > CCmax_new ) {
  315. CCmax_new = CC[ i ];
  316. CBimax_new = i;
  317. }
  318. }
  319. /* Bias towards shorter lags */
  320. lag_log2 = silk_log2( (silk_float)d );
  321. CCmax_new_b = CCmax_new - PE_SHORTLAG_BIAS * nb_subfr * lag_log2;
  322. /* Bias towards previous lag */
  323. if( prevLag > 0 ) {
  324. delta_lag_log2_sqr = lag_log2 - prevLag_log2;
  325. delta_lag_log2_sqr *= delta_lag_log2_sqr;
  326. CCmax_new_b -= PE_PREVLAG_BIAS * nb_subfr * (*LTPCorr) * delta_lag_log2_sqr / ( delta_lag_log2_sqr + 0.5f );
  327. }
  328. if( CCmax_new_b > CCmax_b && /* Find maximum biased correlation */
  329. CCmax_new > nb_subfr * search_thres2 /* Correlation needs to be high enough to be voiced */
  330. ) {
  331. CCmax_b = CCmax_new_b;
  332. CCmax = CCmax_new;
  333. lag = d;
  334. CBimax = CBimax_new;
  335. }
  336. }
  337. if( lag == -1 ) {
  338. /* No suitable candidate found */
  339. silk_memset( pitch_out, 0, PE_MAX_NB_SUBFR * sizeof(opus_int) );
  340. *LTPCorr = 0.0f;
  341. *lagIndex = 0;
  342. *contourIndex = 0;
  343. return 1;
  344. }
  345. /* Output normalized correlation */
  346. *LTPCorr = (silk_float)( CCmax / nb_subfr );
  347. silk_assert( *LTPCorr >= 0.0f );
  348. if( Fs_kHz > 8 ) {
  349. /* Search in original signal */
  350. /* Compensate for decimation */
  351. silk_assert( lag == silk_SAT16( lag ) );
  352. if( Fs_kHz == 12 ) {
  353. lag = silk_RSHIFT_ROUND( silk_SMULBB( lag, 3 ), 1 );
  354. } else { /* Fs_kHz == 16 */
  355. lag = silk_LSHIFT( lag, 1 );
  356. }
  357. lag = silk_LIMIT_int( lag, min_lag, max_lag );
  358. start_lag = silk_max_int( lag - 2, min_lag );
  359. end_lag = silk_min_int( lag + 2, max_lag );
  360. lag_new = lag; /* to avoid undefined lag */
  361. CBimax = 0; /* to avoid undefined lag */
  362. CCmax = -1000.0f;
  363. /* Calculate the correlations and energies needed in stage 3 */
  364. silk_P_Ana_calc_corr_st3( cross_corr_st3, frame, start_lag, sf_length, nb_subfr, complexity, arch );
  365. silk_P_Ana_calc_energy_st3( energies_st3, frame, start_lag, sf_length, nb_subfr, complexity );
  366. lag_counter = 0;
  367. silk_assert( lag == silk_SAT16( lag ) );
  368. contour_bias = PE_FLATCONTOUR_BIAS / lag;
  369. /* Set up cbk parameters according to complexity setting and frame length */
  370. if( nb_subfr == PE_MAX_NB_SUBFR ) {
  371. nb_cbk_search = (opus_int)silk_nb_cbk_searchs_stage3[ complexity ];
  372. cbk_size = PE_NB_CBKS_STAGE3_MAX;
  373. Lag_CB_ptr = &silk_CB_lags_stage3[ 0 ][ 0 ];
  374. } else {
  375. nb_cbk_search = PE_NB_CBKS_STAGE3_10MS;
  376. cbk_size = PE_NB_CBKS_STAGE3_10MS;
  377. Lag_CB_ptr = &silk_CB_lags_stage3_10_ms[ 0 ][ 0 ];
  378. }
  379. target_ptr = &frame[ PE_LTP_MEM_LENGTH_MS * Fs_kHz ];
  380. energy_tmp = silk_energy_FLP( target_ptr, nb_subfr * sf_length ) + 1.0;
  381. for( d = start_lag; d <= end_lag; d++ ) {
  382. for( j = 0; j < nb_cbk_search; j++ ) {
  383. cross_corr = 0.0;
  384. energy = energy_tmp;
  385. for( k = 0; k < nb_subfr; k++ ) {
  386. cross_corr += cross_corr_st3[ k ][ j ][ lag_counter ];
  387. energy += energies_st3[ k ][ j ][ lag_counter ];
  388. }
  389. if( cross_corr > 0.0 ) {
  390. CCmax_new = (silk_float)( 2 * cross_corr / energy );
  391. /* Reduce depending on flatness of contour */
  392. CCmax_new *= 1.0f - contour_bias * j;
  393. } else {
  394. CCmax_new = 0.0f;
  395. }
  396. if( CCmax_new > CCmax && ( d + (opus_int)silk_CB_lags_stage3[ 0 ][ j ] ) <= max_lag ) {
  397. CCmax = CCmax_new;
  398. lag_new = d;
  399. CBimax = j;
  400. }
  401. }
  402. lag_counter++;
  403. }
  404. for( k = 0; k < nb_subfr; k++ ) {
  405. pitch_out[ k ] = lag_new + matrix_ptr( Lag_CB_ptr, k, CBimax, cbk_size );
  406. pitch_out[ k ] = silk_LIMIT( pitch_out[ k ], min_lag, PE_MAX_LAG_MS * Fs_kHz );
  407. }
  408. *lagIndex = (opus_int16)( lag_new - min_lag );
  409. *contourIndex = (opus_int8)CBimax;
  410. } else { /* Fs_kHz == 8 */
  411. /* Save Lags */
  412. for( k = 0; k < nb_subfr; k++ ) {
  413. pitch_out[ k ] = lag + matrix_ptr( Lag_CB_ptr, k, CBimax, cbk_size );
  414. pitch_out[ k ] = silk_LIMIT( pitch_out[ k ], min_lag_8kHz, PE_MAX_LAG_MS * 8 );
  415. }
  416. *lagIndex = (opus_int16)( lag - min_lag_8kHz );
  417. *contourIndex = (opus_int8)CBimax;
  418. }
  419. celt_assert( *lagIndex >= 0 );
  420. /* return as voiced */
  421. return 0;
  422. }
  423. /***********************************************************************
  424. * Calculates the correlations used in stage 3 search. In order to cover
  425. * the whole lag codebook for all the searched offset lags (lag +- 2),
  426. * the following correlations are needed in each sub frame:
  427. *
  428. * sf1: lag range [-8,...,7] total 16 correlations
  429. * sf2: lag range [-4,...,4] total 9 correlations
  430. * sf3: lag range [-3,....4] total 8 correltions
  431. * sf4: lag range [-6,....8] total 15 correlations
  432. *
  433. * In total 48 correlations. The direct implementation computed in worst
  434. * case 4*12*5 = 240 correlations, but more likely around 120.
  435. ***********************************************************************/
  436. static void silk_P_Ana_calc_corr_st3(
  437. silk_float cross_corr_st3[ PE_MAX_NB_SUBFR ][ PE_NB_CBKS_STAGE3_MAX ][ PE_NB_STAGE3_LAGS ], /* O 3 DIM correlation array */
  438. const silk_float frame[], /* I vector to correlate */
  439. opus_int start_lag, /* I start lag */
  440. opus_int sf_length, /* I sub frame length */
  441. opus_int nb_subfr, /* I number of subframes */
  442. opus_int complexity, /* I Complexity setting */
  443. int arch /* I Run-time architecture */
  444. )
  445. {
  446. const silk_float *target_ptr;
  447. opus_int i, j, k, lag_counter, lag_low, lag_high;
  448. opus_int nb_cbk_search, delta, idx, cbk_size;
  449. silk_float scratch_mem[ SCRATCH_SIZE ];
  450. opus_val32 xcorr[ SCRATCH_SIZE ];
  451. const opus_int8 *Lag_range_ptr, *Lag_CB_ptr;
  452. celt_assert( complexity >= SILK_PE_MIN_COMPLEX );
  453. celt_assert( complexity <= SILK_PE_MAX_COMPLEX );
  454. if( nb_subfr == PE_MAX_NB_SUBFR ) {
  455. Lag_range_ptr = &silk_Lag_range_stage3[ complexity ][ 0 ][ 0 ];
  456. Lag_CB_ptr = &silk_CB_lags_stage3[ 0 ][ 0 ];
  457. nb_cbk_search = silk_nb_cbk_searchs_stage3[ complexity ];
  458. cbk_size = PE_NB_CBKS_STAGE3_MAX;
  459. } else {
  460. celt_assert( nb_subfr == PE_MAX_NB_SUBFR >> 1);
  461. Lag_range_ptr = &silk_Lag_range_stage3_10_ms[ 0 ][ 0 ];
  462. Lag_CB_ptr = &silk_CB_lags_stage3_10_ms[ 0 ][ 0 ];
  463. nb_cbk_search = PE_NB_CBKS_STAGE3_10MS;
  464. cbk_size = PE_NB_CBKS_STAGE3_10MS;
  465. }
  466. target_ptr = &frame[ silk_LSHIFT( sf_length, 2 ) ]; /* Pointer to middle of frame */
  467. for( k = 0; k < nb_subfr; k++ ) {
  468. lag_counter = 0;
  469. /* Calculate the correlations for each subframe */
  470. lag_low = matrix_ptr( Lag_range_ptr, k, 0, 2 );
  471. lag_high = matrix_ptr( Lag_range_ptr, k, 1, 2 );
  472. silk_assert(lag_high-lag_low+1 <= SCRATCH_SIZE);
  473. celt_pitch_xcorr( target_ptr, target_ptr - start_lag - lag_high, xcorr, sf_length, lag_high - lag_low + 1, arch );
  474. for( j = lag_low; j <= lag_high; j++ ) {
  475. silk_assert( lag_counter < SCRATCH_SIZE );
  476. scratch_mem[ lag_counter ] = xcorr[ lag_high - j ];
  477. lag_counter++;
  478. }
  479. delta = matrix_ptr( Lag_range_ptr, k, 0, 2 );
  480. for( i = 0; i < nb_cbk_search; i++ ) {
  481. /* Fill out the 3 dim array that stores the correlations for */
  482. /* each code_book vector for each start lag */
  483. idx = matrix_ptr( Lag_CB_ptr, k, i, cbk_size ) - delta;
  484. for( j = 0; j < PE_NB_STAGE3_LAGS; j++ ) {
  485. silk_assert( idx + j < SCRATCH_SIZE );
  486. silk_assert( idx + j < lag_counter );
  487. cross_corr_st3[ k ][ i ][ j ] = scratch_mem[ idx + j ];
  488. }
  489. }
  490. target_ptr += sf_length;
  491. }
  492. }
  493. /********************************************************************/
  494. /* Calculate the energies for first two subframes. The energies are */
  495. /* calculated recursively. */
  496. /********************************************************************/
  497. static void silk_P_Ana_calc_energy_st3(
  498. silk_float energies_st3[ PE_MAX_NB_SUBFR ][ PE_NB_CBKS_STAGE3_MAX ][ PE_NB_STAGE3_LAGS ], /* O 3 DIM correlation array */
  499. const silk_float frame[], /* I vector to correlate */
  500. opus_int start_lag, /* I start lag */
  501. opus_int sf_length, /* I sub frame length */
  502. opus_int nb_subfr, /* I number of subframes */
  503. opus_int complexity /* I Complexity setting */
  504. )
  505. {
  506. const silk_float *target_ptr, *basis_ptr;
  507. double energy;
  508. opus_int k, i, j, lag_counter;
  509. opus_int nb_cbk_search, delta, idx, cbk_size, lag_diff;
  510. silk_float scratch_mem[ SCRATCH_SIZE ];
  511. const opus_int8 *Lag_range_ptr, *Lag_CB_ptr;
  512. celt_assert( complexity >= SILK_PE_MIN_COMPLEX );
  513. celt_assert( complexity <= SILK_PE_MAX_COMPLEX );
  514. if( nb_subfr == PE_MAX_NB_SUBFR ) {
  515. Lag_range_ptr = &silk_Lag_range_stage3[ complexity ][ 0 ][ 0 ];
  516. Lag_CB_ptr = &silk_CB_lags_stage3[ 0 ][ 0 ];
  517. nb_cbk_search = silk_nb_cbk_searchs_stage3[ complexity ];
  518. cbk_size = PE_NB_CBKS_STAGE3_MAX;
  519. } else {
  520. celt_assert( nb_subfr == PE_MAX_NB_SUBFR >> 1);
  521. Lag_range_ptr = &silk_Lag_range_stage3_10_ms[ 0 ][ 0 ];
  522. Lag_CB_ptr = &silk_CB_lags_stage3_10_ms[ 0 ][ 0 ];
  523. nb_cbk_search = PE_NB_CBKS_STAGE3_10MS;
  524. cbk_size = PE_NB_CBKS_STAGE3_10MS;
  525. }
  526. target_ptr = &frame[ silk_LSHIFT( sf_length, 2 ) ];
  527. for( k = 0; k < nb_subfr; k++ ) {
  528. lag_counter = 0;
  529. /* Calculate the energy for first lag */
  530. basis_ptr = target_ptr - ( start_lag + matrix_ptr( Lag_range_ptr, k, 0, 2 ) );
  531. energy = silk_energy_FLP( basis_ptr, sf_length ) + 1e-3;
  532. silk_assert( energy >= 0.0 );
  533. scratch_mem[lag_counter] = (silk_float)energy;
  534. lag_counter++;
  535. lag_diff = ( matrix_ptr( Lag_range_ptr, k, 1, 2 ) - matrix_ptr( Lag_range_ptr, k, 0, 2 ) + 1 );
  536. for( i = 1; i < lag_diff; i++ ) {
  537. /* remove part outside new window */
  538. energy -= basis_ptr[sf_length - i] * (double)basis_ptr[sf_length - i];
  539. silk_assert( energy >= 0.0 );
  540. /* add part that comes into window */
  541. energy += basis_ptr[ -i ] * (double)basis_ptr[ -i ];
  542. silk_assert( energy >= 0.0 );
  543. silk_assert( lag_counter < SCRATCH_SIZE );
  544. scratch_mem[lag_counter] = (silk_float)energy;
  545. lag_counter++;
  546. }
  547. delta = matrix_ptr( Lag_range_ptr, k, 0, 2 );
  548. for( i = 0; i < nb_cbk_search; i++ ) {
  549. /* Fill out the 3 dim array that stores the correlations for */
  550. /* each code_book vector for each start lag */
  551. idx = matrix_ptr( Lag_CB_ptr, k, i, cbk_size ) - delta;
  552. for( j = 0; j < PE_NB_STAGE3_LAGS; j++ ) {
  553. silk_assert( idx + j < SCRATCH_SIZE );
  554. silk_assert( idx + j < lag_counter );
  555. energies_st3[ k ][ i ][ j ] = scratch_mem[ idx + j ];
  556. silk_assert( energies_st3[ k ][ i ][ j ] >= 0.0f );
  557. }
  558. }
  559. target_ptr += sf_length;
  560. }
  561. }