pitch_ol.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /*
  2. ********************************************************************************
  3. *
  4. * GSM AMR-NB speech codec R98 Version 7.6.0 December 12, 2001
  5. * R99 Version 3.3.0
  6. * REL-4 Version 4.1.0
  7. *
  8. ********************************************************************************
  9. *
  10. * File : pitch_ol.c
  11. * Purpose : Compute the open loop pitch lag.
  12. *
  13. ********************************************************************************
  14. */
  15. /*
  16. ********************************************************************************
  17. * MODULE INCLUDE FILE AND VERSION ID
  18. ********************************************************************************
  19. */
  20. #include "pitch_ol.h"
  21. const char pitch_ol_id[] = "@(#)$Id $" pitch_ol_h;
  22. /*
  23. ********************************************************************************
  24. * INCLUDE FILES
  25. ********************************************************************************
  26. */
  27. #include <stdlib.h>
  28. #include <stdio.h>
  29. #include "typedef.h"
  30. #include "basic_op.h"
  31. #include "oper_32b.h"
  32. #include "count.h"
  33. #include "cnst.h"
  34. #include "inv_sqrt_ex.h"
  35. #include "vad.h"
  36. #include "calc_cor.h"
  37. #include "hp_max.h"
  38. /*
  39. ********************************************************************************
  40. * LOCAL VARIABLES AND TABLES
  41. ********************************************************************************
  42. */
  43. #define THRESHOLD 27853
  44. /*
  45. ********************************************************************************
  46. * LOCAL PROGRAM CODE
  47. ********************************************************************************
  48. */
  49. /*************************************************************************
  50. *
  51. * FUNCTION: Lag_max
  52. *
  53. * PURPOSE: Find the lag that has maximum correlation of scal_sig[] in a
  54. * given delay range.
  55. *
  56. * DESCRIPTION:
  57. * The correlation is given by
  58. * cor[t] = <scal_sig[n],scal_sig[n-t]>, t=lag_min,...,lag_max
  59. * The functions outputs the maximum correlation after normalization
  60. * and the corresponding lag.
  61. *
  62. *************************************************************************/
  63. #ifdef VAD2
  64. static Word16 Lag_max ( /* o : lag found */
  65. Word32 corr[], /* i : correlation vector. */
  66. Word16 scal_sig[], /* i : scaled signal. */
  67. Word16 scal_fac, /* i : scaled signal factor. */
  68. Word16 scal_flag, /* i : if 1 use EFR compatible scaling */
  69. Word16 L_frame, /* i : length of frame to compute pitch */
  70. Word16 lag_max, /* i : maximum lag */
  71. Word16 lag_min, /* i : minimum lag */
  72. Word16 *cor_max, /* o : normalized correlation of selected lag */
  73. Word32 *rmax, /* o : max(<s[i]*s[j]>) */
  74. Word32 *r0, /* o : residual energy */
  75. Flag dtx /* i : dtx flag; use dtx=1, do not use dtx=0 */
  76. )
  77. #else
  78. static Word16 Lag_max ( /* o : lag found */
  79. vadState *vadSt, /* i/o : VAD state struct */
  80. Word32 corr[], /* i : correlation vector. */
  81. Word16 scal_sig[], /* i : scaled signal. */
  82. Word16 scal_fac, /* i : scaled signal factor. */
  83. Word16 scal_flag, /* i : if 1 use EFR compatible scaling */
  84. Word16 L_frame, /* i : length of frame to compute pitch */
  85. Word16 lag_max, /* i : maximum lag */
  86. Word16 lag_min, /* i : minimum lag */
  87. Word16 *cor_max, /* o : normalized correlation of selected lag */
  88. Flag dtx /* i : dtx flag; use dtx=1, do not use dtx=0 */
  89. )
  90. #endif
  91. {
  92. Word16 i, j;
  93. Word16 *p;
  94. Word32 max, t0;
  95. Word16 max_h, max_l, ener_h, ener_l;
  96. Word16 p_max = 0; /* initialization only needed to keep gcc silent */
  97. max = MIN_32; move32 ();
  98. p_max = lag_max; move16 ();
  99. for (i = lag_max, j = (PIT_MAX-lag_max-1); i >= lag_min; i--, j--)
  100. {
  101. test ();
  102. if (L_sub_ex (corr[-i], max) >= 0)
  103. {
  104. max = corr[-i]; move32 ();
  105. p_max = i; move16 ();
  106. }
  107. }
  108. /* compute energy */
  109. t0 = 0; move32 ();
  110. p = &scal_sig[-p_max]; move16 ();
  111. for (i = 0; i < L_frame; i++, p++)
  112. {
  113. t0 = L_mac_ex (t0, *p, *p);
  114. }
  115. /* 1/sqrt(energy) */
  116. if (dtx)
  117. { /* no test() call since this if is only in simulation env */
  118. #ifdef VAD2
  119. *rmax = max; move32();
  120. *r0 = t0; move32();
  121. #else
  122. /* check tone */
  123. vad_tone_detection (vadSt, max, t0);
  124. #endif
  125. }
  126. t0 = Inv_sqrt_ex (t0); move32 (); /* function result */
  127. test();
  128. if (scal_flag)
  129. {
  130. t0 = L_shl_ex (t0, 1);
  131. }
  132. /* max = max/sqrt(energy) */
  133. L_Extract (max, &max_h, &max_l);
  134. L_Extract (t0, &ener_h, &ener_l);
  135. t0 = Mpy_32 (max_h, max_l, ener_h, ener_l);
  136. test();
  137. if (scal_flag)
  138. {
  139. t0 = L_shr_ex (t0, scal_fac);
  140. *cor_max = extract_h_ex (L_shl_ex (t0, 15)); /* divide by 2 */
  141. }
  142. else
  143. {
  144. *cor_max = extract_l_ex(t0);
  145. }
  146. return (p_max);
  147. }
  148. /*
  149. ********************************************************************************
  150. * PUBLIC PROGRAM CODE
  151. ********************************************************************************
  152. */
  153. /*************************************************************************
  154. *
  155. * FUNCTION: Pitch_ol
  156. *
  157. * PURPOSE: Compute the open loop pitch lag.
  158. *
  159. * DESCRIPTION:
  160. * The open-loop pitch lag is determined based on the perceptually
  161. * weighted speech signal. This is done in the following steps:
  162. * - find three maxima of the correlation <sw[n],sw[n-T]>,
  163. * dividing the search range into three parts:
  164. * pit_min ... 2*pit_min-1
  165. * 2*pit_min ... 4*pit_min-1
  166. * 4*pit_min ... pit_max
  167. * - divide each maximum by <sw[n-t], sw[n-t]> where t is the delay at
  168. * that maximum correlation.
  169. * - select the delay of maximum normalized correlation (among the
  170. * three candidates) while favoring the lower delay ranges.
  171. *
  172. *************************************************************************/
  173. Word16 Pitch_ol ( /* o : open loop pitch lag */
  174. vadState *vadSt, /* i/o : VAD state struct */
  175. enum Mode mode, /* i : coder mode */
  176. Word16 signal[], /* i : signal used to compute the open loop pitch */
  177. /* signal[-pit_max] to signal[-1] should be known */
  178. Word16 pit_min, /* i : minimum pitch lag */
  179. Word16 pit_max, /* i : maximum pitch lag */
  180. Word16 L_frame, /* i : length of frame to compute pitch */
  181. Word16 idx, /* i : frame index */
  182. Flag dtx /* i : dtx flag; use dtx=1, do not use dtx=0 */
  183. )
  184. {
  185. Word16 i, j;
  186. Word16 max1, max2, max3;
  187. Word16 p_max1, p_max2, p_max3;
  188. Word16 scal_flag = 0;
  189. Word32 t0;
  190. #ifdef VAD2
  191. Word32 r01, r02, r03;
  192. Word32 rmax1, rmax2, rmax3;
  193. #else
  194. Word16 corr_hp_max;
  195. #endif
  196. Word32 corr[PIT_MAX+1], *corr_ptr;
  197. /* Scaled signal */
  198. Word16 scaled_signal[L_FRAME + PIT_MAX];
  199. Word16 *scal_sig, scal_fac;
  200. #ifndef VAD2
  201. if (dtx)
  202. { /* no test() call since this if is only in simulation env */
  203. /* update tone detection */
  204. test(); test();
  205. if ((sub_ex(mode, MR475) == 0) || (sub_ex(mode, MR515) == 0))
  206. {
  207. vad_tone_detection_update (vadSt, 1);
  208. }
  209. else
  210. {
  211. vad_tone_detection_update (vadSt, 0);
  212. }
  213. }
  214. #endif
  215. scal_sig = &scaled_signal[pit_max]; move16 ();
  216. t0 = 0L; move32 ();
  217. for (i = -pit_max; i < L_frame; i++)
  218. {
  219. t0 = L_mac_ex (t0, signal[i], signal[i]);
  220. }
  221. /*--------------------------------------------------------*
  222. * Scaling of input signal. *
  223. * *
  224. * if Overflow -> scal_sig[i] = signal[i]>>3 *
  225. * else if t0 < 1^20 -> scal_sig[i] = signal[i]<<3 *
  226. * else -> scal_sig[i] = signal[i] *
  227. *--------------------------------------------------------*/
  228. /*--------------------------------------------------------*
  229. * Verification for risk of overflow. *
  230. *--------------------------------------------------------*/
  231. test ();
  232. if (L_sub_ex (t0, MAX_32) == 0L) /* Test for overflow */
  233. {
  234. for (i = -pit_max; i < L_frame; i++)
  235. {
  236. scal_sig[i] = shr_ex (signal[i], 3); move16 ();
  237. }
  238. scal_fac = 3; move16 ();
  239. }
  240. else if (L_sub_ex (t0, (Word32) 1048576L) < (Word32) 0)
  241. /* if (t0 < 2^20) */
  242. {
  243. test ();
  244. for (i = -pit_max; i < L_frame; i++)
  245. {
  246. scal_sig[i] = shl_ex (signal[i], 3); move16 ();
  247. }
  248. scal_fac = -3; move16 ();
  249. }
  250. else
  251. {
  252. test ();
  253. for (i = -pit_max; i < L_frame; i++)
  254. {
  255. scal_sig[i] = signal[i]; move16 ();
  256. }
  257. scal_fac = 0; move16 ();
  258. }
  259. /* calculate all coreelations of scal_sig, from pit_min to pit_max */
  260. corr_ptr = &corr[pit_max]; move32 ();
  261. comp_corr (scal_sig, L_frame, pit_max, pit_min, corr_ptr);
  262. /*--------------------------------------------------------------------*
  263. * The pitch lag search is divided in three sections. *
  264. * Each section cannot have a pitch multiple. *
  265. * We find a maximum for each section. *
  266. * We compare the maximum of each section by favoring small lags. *
  267. * *
  268. * First section: lag delay = pit_max downto 4*pit_min *
  269. * Second section: lag delay = 4*pit_min-1 downto 2*pit_min *
  270. * Third section: lag delay = 2*pit_min-1 downto pit_min *
  271. *--------------------------------------------------------------------*/
  272. /* mode dependent scaling in Lag_max */
  273. test ();
  274. if (sub_ex(mode, MR122) == 0)
  275. {
  276. scal_flag = 1; move16 ();
  277. }
  278. else
  279. {
  280. scal_flag = 0; move16 ();
  281. }
  282. #ifdef VAD2
  283. j = shl_ex (pit_min, 2);
  284. p_max1 = Lag_max (corr_ptr, scal_sig, scal_fac, scal_flag, L_frame,
  285. pit_max, j, &max1, &rmax1, &r01, dtx);
  286. move16 (); /* function result */
  287. i = sub_ex (j, 1);
  288. j = shl_ex (pit_min, 1);
  289. p_max2 = Lag_max (corr_ptr, scal_sig, scal_fac, scal_flag, L_frame,
  290. i, j, &max2, &rmax2, &r02, dtx);
  291. move16 (); /* function result */
  292. i = sub_ex (j, 1);
  293. p_max3 = Lag_max (corr_ptr, scal_sig, scal_fac, scal_flag, L_frame,
  294. i, pit_min, &max3, &rmax3, &r03, dtx);
  295. move16 (); /* function result */
  296. #else
  297. j = shl_ex (pit_min, 2);
  298. p_max1 = Lag_max (vadSt, corr_ptr, scal_sig, scal_fac, scal_flag, L_frame,
  299. pit_max, j, &max1, dtx); move16 (); /* function result */
  300. i = sub_ex (j, 1);
  301. j = shl_ex (pit_min, 1);
  302. p_max2 = Lag_max (vadSt, corr_ptr, scal_sig, scal_fac, scal_flag, L_frame,
  303. i, j, &max2, dtx); move16 (); /* function result */
  304. i = sub_ex (j, 1);
  305. p_max3 = Lag_max (vadSt, corr_ptr, scal_sig, scal_fac, scal_flag, L_frame,
  306. i, pit_min, &max3, dtx); move16 (); /* function result */
  307. if (dtx)
  308. { /* no test() call since this if is only in simulation env */
  309. test ();
  310. if (sub_ex(idx, 1) == 0)
  311. {
  312. /* calculate max high-passed filtered correlation of all lags */
  313. hp_max (corr_ptr, scal_sig, L_frame, pit_max, pit_min, &corr_hp_max);
  314. /* update complex background detector */
  315. vad_complex_detection_update(vadSt, corr_hp_max);
  316. }
  317. }
  318. #endif
  319. /*--------------------------------------------------------------------*
  320. * Compare the 3 sections maximum, and favor small lag. *
  321. *--------------------------------------------------------------------*/
  322. test ();
  323. if (sub_ex (mult_ex (max1, THRESHOLD), max2) < 0)
  324. {
  325. max1 = max2; move16 ();
  326. p_max1 = p_max2; move16 ();
  327. #ifdef VAD2
  328. if (dtx)
  329. {
  330. rmax1 = rmax2; move32 ();
  331. r01 = r02; move32 ();
  332. }
  333. #endif
  334. }
  335. test ();
  336. if (sub_ex (mult_ex (max1, THRESHOLD), max3) < 0)
  337. {
  338. p_max1 = p_max3; move16 ();
  339. #ifdef VAD2
  340. if (dtx)
  341. {
  342. rmax1 = rmax3; move32 ();
  343. r01 = r03; move32 ();
  344. }
  345. #endif
  346. }
  347. #ifdef VAD2
  348. if (dtx)
  349. {
  350. vadSt->L_Rmax = L_add_ex(vadSt->L_Rmax, rmax1); /* Save max correlation */
  351. vadSt->L_R0 = L_add_ex(vadSt->L_R0, r01); /* Save max energy */
  352. }
  353. #endif
  354. return (p_max1);
  355. }