vad1.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071
  1. /*
  2. *****************************************************************************
  3. **-------------------------------------------------------------------------**
  4. ** **
  5. ** GSM AMR-NB speech codec R98 Version 7.6.0 December 12, 2001 **
  6. ** R99 Version 3.3.0 **
  7. ** REL-4 Version 4.1.0 **
  8. ** **
  9. **-------------------------------------------------------------------------**
  10. *****************************************************************************
  11. *
  12. * File : vad1.c
  13. * Purpose : Voice Activity Detection (VAD) for AMR (option 1)
  14. *
  15. *****************************************************************************
  16. */
  17. /*
  18. *****************************************************************************
  19. * MODULE INCLUDE FILE AND VERSION ID
  20. *****************************************************************************
  21. */
  22. #include "vad.h"
  23. const char vad1_id[] = "@(#)$Id $" vad_h;
  24. /*
  25. *****************************************************************************
  26. * INCLUDE FILES
  27. *****************************************************************************
  28. */
  29. #include <stdlib.h>
  30. #include <stdio.h>
  31. #include "typedef.h"
  32. #include "basic_op.h"
  33. #include "count.h"
  34. #include "oper_32b.h"
  35. #include "cnst_vad.h"
  36. /*
  37. *****************************************************************************
  38. * LOCAL VARIABLES AND TABLES
  39. *****************************************************************************
  40. */
  41. /*
  42. ********************************************************************************
  43. * PRIVATE PROGRAM CODE
  44. ********************************************************************************
  45. */
  46. /****************************************************************************
  47. *
  48. * Function : first_filter_stage
  49. * Purpose : Scale input down by one bit. Calculate 5th order
  50. * half-band lowpass/highpass filter pair with
  51. * decimation.
  52. *
  53. ***************************************************************************/
  54. static void first_filter_stage(Word16 in[], /* i : input signal */
  55. Word16 out[], /* o : output values, every other */
  56. /* output is low-pass part and */
  57. /* other is high-pass part every */
  58. Word16 data[] /* i/o : filter memory */
  59. )
  60. {
  61. Word16 temp0, temp1, temp2, temp3, i;
  62. Word16 data0, data1;
  63. data0 = data[0]; move16 ();
  64. data1 = data[1]; move16 ();
  65. for (i = 0; i < FRAME_LEN/4; i++)
  66. {
  67. temp0 = sub_ex(shr_ex(in[4*i+0], 2), mult_ex(COEFF5_1, data0));
  68. temp1 = add_ex(data0, mult_ex(COEFF5_1, temp0));
  69. temp3 = sub_ex(shr_ex(in[4*i+1], 2), mult_ex(COEFF5_2, data1));
  70. temp2 = add_ex(data1, mult_ex(COEFF5_2, temp3));
  71. out[4*i+0] = add_ex(temp1, temp2); move16 ();
  72. out[4*i+1] = sub_ex(temp1, temp2); move16 ();
  73. data0 = sub_ex(shr_ex(in[4*i+2], 2), mult_ex(COEFF5_1, temp0));
  74. temp1 = add_ex(temp0, mult_ex(COEFF5_1, data0));
  75. data1 = sub_ex(shr_ex(in[4*i+3], 2), mult_ex(COEFF5_2, temp3));
  76. temp2 = add_ex(temp3, mult_ex(COEFF5_2, data1));
  77. out[4*i+2] = add_ex(temp1, temp2); move16 ();
  78. out[4*i+3] = sub_ex(temp1, temp2); move16 ();
  79. }
  80. data[0] = data0; move16 ();
  81. data[1] = data1; move16 ();
  82. }
  83. /****************************************************************************
  84. *
  85. * Function : filter5
  86. * Purpose : Fifth-order half-band lowpass/highpass filter pair with
  87. * decimation.
  88. *
  89. ***************************************************************************/
  90. static void filter5(Word16 *in0, /* i/o : input values; output low-pass part */
  91. Word16 *in1, /* i/o : input values; output high-pass part */
  92. Word16 data[] /* i/o : updated filter memory */
  93. )
  94. {
  95. Word16 temp0, temp1, temp2;
  96. temp0 = sub_ex(*in0, mult_ex(COEFF5_1, data[0]));
  97. temp1 = add_ex(data[0], mult_ex(COEFF5_1, temp0));
  98. data[0] = temp0; move16 ();
  99. temp0 = sub_ex(*in1, mult_ex(COEFF5_2, data[1]));
  100. temp2 = add_ex(data[1], mult_ex(COEFF5_2, temp0));
  101. data[1] = temp0; move16 ();
  102. *in0 = shr_ex(add_ex(temp1, temp2), 1); move16 ();
  103. *in1 = shr_ex(sub_ex(temp1, temp2), 1); move16 ();
  104. }
  105. /****************************************************************************
  106. *
  107. * Function : filter3
  108. * Purpose : Third-order half-band lowpass/highpass filter pair with
  109. * decimation.
  110. * Return value :
  111. *
  112. ***************************************************************************/
  113. static void filter3(Word16 *in0, /* i/o : input values; output low-pass part */
  114. Word16 *in1, /* i/o : input values; output high-pass part */
  115. Word16 *data /* i/o : updated filter memory */
  116. )
  117. {
  118. Word16 temp1, temp2;
  119. temp1 = sub_ex(*in1, mult_ex(COEFF3, *data));
  120. temp2 = add_ex(*data, mult_ex(COEFF3, temp1));
  121. *data = temp1; move16 ();
  122. *in1 = shr_ex(sub_ex(*in0, temp2), 1); move16 ();
  123. *in0 = shr_ex(add_ex(*in0, temp2), 1); move16 ();
  124. }
  125. /****************************************************************************
  126. *
  127. * Function : level_calculation
  128. * Purpose : Calculate signal level in a sub_ex-band. Level is calculated
  129. * by summing absolute values of the input data.
  130. * Return value : signal level
  131. *
  132. ***************************************************************************/
  133. static Word16 level_calculation(
  134. Word16 data[], /* i : signal buffer */
  135. Word16 *sub_level, /* i : level calculate at the end of the previous frame */
  136. /* o : level of signal calculated from the last */
  137. /* (count2 - count1) samples */
  138. Word16 count1, /* i : number of samples to be counted */
  139. Word16 count2, /* i : number of samples to be counted */
  140. Word16 ind_m, /* i : step size for the index of the data buffer */
  141. Word16 ind_a, /* i : starting index of the data buffer */
  142. Word16 scale /* i : scaling for the level calculation */
  143. )
  144. {
  145. Word32 l_temp1, l_temp2;
  146. Word16 level, i;
  147. l_temp1 = 0L; move32 ();
  148. for (i = count1; i < count2; i++)
  149. {
  150. l_temp1 = L_mac_ex(l_temp1, 1, abs_s_ex(data[ind_m*i+ind_a]));
  151. }
  152. l_temp2 = L_add_ex(l_temp1, L_shl_ex(*sub_level, sub_ex(16, scale)));
  153. *sub_level = extract_h_ex(L_shl_ex(l_temp1, scale));
  154. for (i = 0; i < count1; i++)
  155. {
  156. l_temp2 = L_mac_ex(l_temp2, 1, abs_s_ex(data[ind_m*i+ind_a]));
  157. }
  158. level = extract_h_ex(L_shl_ex(l_temp2, scale));
  159. return level;
  160. }
  161. /****************************************************************************
  162. *
  163. * Function : filter_bank
  164. * Purpose : Divides input signal into 9-bands and calculas level of
  165. * the signal in each band
  166. *
  167. ***************************************************************************/
  168. static void filter_bank(vadState1 *st, /* i/o : State struct */
  169. Word16 in[], /* i : input frame */
  170. Word16 level[] /* 0 : signal levels at each band */
  171. )
  172. {
  173. Word16 i;
  174. Word16 tmp_buf[FRAME_LEN];
  175. /* calculate the filter bank */
  176. first_filter_stage(in, tmp_buf, st->a_data5[0]);
  177. for (i = 0; i < FRAME_LEN/4; i++)
  178. {
  179. filter5(&tmp_buf[4*i], &tmp_buf[4*i+2], st->a_data5[1]);
  180. filter5(&tmp_buf[4*i+1], &tmp_buf[4*i+3], st->a_data5[2]);
  181. }
  182. for (i = 0; i < FRAME_LEN/8; i++)
  183. {
  184. filter3(&tmp_buf[8*i+0], &tmp_buf[8*i+4], &st->a_data3[0]);
  185. filter3(&tmp_buf[8*i+2], &tmp_buf[8*i+6], &st->a_data3[1]);
  186. filter3(&tmp_buf[8*i+3], &tmp_buf[8*i+7], &st->a_data3[4]);
  187. }
  188. for (i = 0; i < FRAME_LEN/16; i++)
  189. {
  190. filter3(&tmp_buf[16*i+0], &tmp_buf[16*i+8], &st->a_data3[2]);
  191. filter3(&tmp_buf[16*i+4], &tmp_buf[16*i+12], &st->a_data3[3]);
  192. }
  193. /* calculate levels in each frequency band */
  194. /* 3000 - 4000 Hz*/
  195. level[8] = level_calculation(tmp_buf, &st->sub_level[8], FRAME_LEN/4-8,
  196. FRAME_LEN/4, 4, 1, 15);
  197. move16 ();
  198. /* 2500 - 3000 Hz*/
  199. level[7] = level_calculation(tmp_buf, &st->sub_level[7], FRAME_LEN/8-4,
  200. FRAME_LEN/8, 8, 7, 16);
  201. move16 ();
  202. /* 2000 - 2500 Hz*/
  203. level[6] = level_calculation(tmp_buf, &st->sub_level[6], FRAME_LEN/8-4,
  204. FRAME_LEN/8, 8, 3, 16);
  205. move16 ();
  206. /* 1500 - 2000 Hz*/
  207. level[5] = level_calculation(tmp_buf, &st->sub_level[5], FRAME_LEN/8-4,
  208. FRAME_LEN/8, 8, 2, 16);
  209. move16 ();
  210. /* 1000 - 1500 Hz*/
  211. level[4] = level_calculation(tmp_buf, &st->sub_level[4], FRAME_LEN/8-4,
  212. FRAME_LEN/8, 8, 6, 16);
  213. move16 ();
  214. /* 750 - 1000 Hz*/
  215. level[3] = level_calculation(tmp_buf, &st->sub_level[3], FRAME_LEN/16-2,
  216. FRAME_LEN/16, 16, 4, 16);
  217. move16 ();
  218. /* 500 - 750 Hz*/
  219. level[2] = level_calculation(tmp_buf, &st->sub_level[2], FRAME_LEN/16-2,
  220. FRAME_LEN/16, 16, 12, 16);
  221. move16 ();
  222. /* 250 - 500 Hz*/
  223. level[1] = level_calculation(tmp_buf, &st->sub_level[1], FRAME_LEN/16-2,
  224. FRAME_LEN/16, 16, 8, 16);
  225. move16 ();
  226. /* 0 - 250 Hz*/
  227. level[0] = level_calculation(tmp_buf, &st->sub_level[0], FRAME_LEN/16-2,
  228. FRAME_LEN/16, 16, 0, 16);
  229. move16 ();
  230. }
  231. /****************************************************************************
  232. *
  233. * Function : update_cntrl
  234. * Purpose : Control update of the background noise estimate.
  235. * Inputs : pitch: flags for pitch detection
  236. * stat_count: stationary counter
  237. * tone: flags indicating presence of a tone
  238. * complex: flags for complex detection
  239. * vadreg: intermediate VAD flags
  240. * Output : stat_count: stationary counter
  241. *
  242. ***************************************************************************/
  243. static void update_cntrl(vadState1 *st, /* i/o : State struct */
  244. Word16 level[] /* i : sub_ex-band levels of the input frame */
  245. )
  246. {
  247. Word16 i, temp, stat_rat, exp;
  248. Word16 num, denom;
  249. Word16 alpha;
  250. /* handle highband complex signal input separately */
  251. /* if ther has been highband correlation for some time */
  252. /* make sure that the VAD update speed is low for a while */
  253. test ();
  254. if (st->complex_warning != 0)
  255. {
  256. test ();
  257. if (sub_ex(st->stat_count, CAD_MIN_STAT_COUNT) < 0)
  258. {
  259. st->stat_count = CAD_MIN_STAT_COUNT; move16 ();
  260. }
  261. }
  262. /* NB stat_count is allowed to be decreased by one below again */
  263. /* deadlock in speech is not possible unless the signal is very */
  264. /* complex and need a high rate */
  265. /* if fullband pitch or tone have been detected for a while, initialize stat_count */
  266. logic16 (); test (); logic16 (); test ();
  267. if ((sub_ex((st->pitch & 0x6000), 0x6000) == 0) ||
  268. (sub_ex((st->tone & 0x7c00), 0x7c00) == 0))
  269. {
  270. st->stat_count = STAT_COUNT; move16 ();
  271. }
  272. else
  273. {
  274. /* if 8 last vad-decisions have been "0", reinitialize stat_count */
  275. logic16 (); test ();
  276. if ((st->vadreg & 0x7f80) == 0)
  277. {
  278. st->stat_count = STAT_COUNT; move16 ();
  279. }
  280. else
  281. {
  282. stat_rat = 0; move16 ();
  283. for (i = 0; i < COMPLEN; i++)
  284. {
  285. test ();
  286. if (sub_ex(level[i], st->ave_level[i]) > 0)
  287. {
  288. num = level[i]; move16 ();
  289. denom = st->ave_level[i]; move16 ();
  290. }
  291. else
  292. {
  293. num = st->ave_level[i]; move16 ();
  294. denom = level[i]; move16 ();
  295. }
  296. /* Limit nimimum value of num and denom to STAT_THR_LEVEL */
  297. test ();
  298. if (sub_ex(num, STAT_THR_LEVEL) < 0)
  299. {
  300. num = STAT_THR_LEVEL; move16 ();
  301. }
  302. test ();
  303. if (sub_ex(denom, STAT_THR_LEVEL) < 0)
  304. {
  305. denom = STAT_THR_LEVEL; move16 ();
  306. }
  307. exp = norm_s_ex(denom);
  308. denom = shl_ex(denom, exp);
  309. /* stat_rat = num/denom * 64 */
  310. temp = div_s(shr_ex(num, 1), denom);
  311. stat_rat = add_ex(stat_rat, shr_ex(temp, sub_ex(8, exp)));
  312. }
  313. /* compare stat_rat with a threshold and update stat_count */
  314. test ();
  315. if (sub_ex(stat_rat, STAT_THR) > 0)
  316. {
  317. st->stat_count = STAT_COUNT; move16 ();
  318. }
  319. else
  320. {
  321. logic16 ();test ();
  322. if ((st->vadreg & 0x4000) != 0)
  323. {
  324. test ();
  325. if (st->stat_count != 0)
  326. {
  327. st->stat_count = sub_ex(st->stat_count, 1); move16 ();
  328. }
  329. }
  330. }
  331. }
  332. }
  333. /* Update average amplitude estimate for stationarity estimation */
  334. alpha = ALPHA4; move16 ();
  335. test ();
  336. if (sub_ex(st->stat_count, STAT_COUNT) == 0)
  337. {
  338. alpha = 32767; move16 ();
  339. }
  340. else if ((st->vadreg & 0x4000) == 0)
  341. {
  342. logic16 (); test ();
  343. alpha = ALPHA5; move16 ();
  344. }
  345. for (i = 0; i < COMPLEN; i++)
  346. {
  347. st->ave_level[i] = add_ex(st->ave_level[i],
  348. mult_r_ex(alpha, sub_ex(level[i], st->ave_level[i])));
  349. move16 ();
  350. }
  351. }
  352. /****************************************************************************
  353. *
  354. * Function : hangover_addition
  355. * Purpose : Add hangover for complex signal or after speech bursts
  356. * Inputs : burst_count: counter for the length of speech bursts
  357. * hang_count: hangover counter
  358. * vadreg: intermediate VAD decision
  359. * Outputs : burst_count: counter for the length of speech bursts
  360. * hang_count: hangover counter
  361. * Return value : VAD_flag indicating final VAD decision
  362. *
  363. ***************************************************************************/
  364. static Word16 hangover_addition(
  365. vadState1 *st, /* i/o : State struct */
  366. Word16 noise_level, /* i : average level of the noise */
  367. /* estimates */
  368. Word16 low_power /* i : flag power of the input frame */
  369. )
  370. {
  371. Word16 hang_len, burst_len;
  372. /*
  373. Calculate burst_len and hang_len
  374. burst_len: number of consecutive intermediate vad flags with "1"-decision
  375. required for hangover addition
  376. hang_len: length of the hangover
  377. */
  378. test ();
  379. if (sub_ex(noise_level, HANG_NOISE_THR) > 0)
  380. {
  381. burst_len = BURST_LEN_HIGH_NOISE; move16 ();
  382. hang_len = HANG_LEN_HIGH_NOISE; move16 ();
  383. }
  384. else
  385. {
  386. burst_len = BURST_LEN_LOW_NOISE; move16 ();
  387. hang_len = HANG_LEN_LOW_NOISE; move16 ();
  388. }
  389. /* if the input power (pow_sum) is lower than a threshold, clear
  390. counters and set VAD_flag to "0" "fast exit" */
  391. test ();
  392. if (low_power != 0)
  393. {
  394. st->burst_count = 0; move16 ();
  395. st->hang_count = 0; move16 ();
  396. st->complex_hang_count = 0; move16 ();
  397. st->complex_hang_timer = 0; move16 ();
  398. return 0;
  399. }
  400. test ();
  401. if (sub_ex(st->complex_hang_timer, CVAD_HANG_LIMIT) > 0)
  402. {
  403. test ();
  404. if (sub_ex(st->complex_hang_count, CVAD_HANG_LENGTH) < 0)
  405. {
  406. st->complex_hang_count = CVAD_HANG_LENGTH; move16 ();
  407. }
  408. }
  409. /* long time very complex signal override VAD output function */
  410. test ();
  411. if (st->complex_hang_count != 0)
  412. {
  413. st->burst_count = BURST_LEN_HIGH_NOISE; move16 ();
  414. st->complex_hang_count = sub_ex(st->complex_hang_count, 1); move16 ();
  415. return 1;
  416. }
  417. else
  418. {
  419. /* let hp_corr work in from a noise_period indicated by the VAD */
  420. test (); test (); logic16 ();
  421. if (((st->vadreg & 0x3ff0) == 0) &&
  422. (sub_ex(st->corr_hp_fast, CVAD_THRESH_IN_NOISE) > 0))
  423. {
  424. return 1;
  425. }
  426. }
  427. /* update the counters (hang_count, burst_count) */
  428. logic16 (); test ();
  429. if ((st->vadreg & 0x4000) != 0)
  430. {
  431. st->burst_count = add_ex(st->burst_count, 1); move16 ();
  432. test ();
  433. if (sub_ex(st->burst_count, burst_len) >= 0)
  434. {
  435. st->hang_count = hang_len; move16 ();
  436. }
  437. return 1;
  438. }
  439. else
  440. {
  441. st->burst_count = 0; move16 ();
  442. test ();
  443. if (st->hang_count > 0)
  444. {
  445. st->hang_count = sub_ex(st->hang_count, 1); move16 ();
  446. return 1;
  447. }
  448. }
  449. return 0;
  450. }
  451. /****************************************************************************
  452. *
  453. * Function : noise_estimate_update
  454. * Purpose : Update of background noise estimate
  455. * Inputs : bckr_est: background noise estimate
  456. * pitch: flags for pitch detection
  457. * stat_count: stationary counter
  458. * Outputs : bckr_est: background noise estimate
  459. *
  460. ***************************************************************************/
  461. static void noise_estimate_update(
  462. vadState1 *st, /* i/o : State struct */
  463. Word16 level[] /* i : sub_ex-band levels of the input frame */
  464. )
  465. {
  466. Word16 i, alpha_up, alpha_down, bckr_add;
  467. /* Control update of bckr_est[] */
  468. update_cntrl(st, level);
  469. /* Choose update speed */
  470. bckr_add = 2; move16 ();
  471. logic16 (); test (); logic16 (); test (); test ();
  472. if (((0x7800 & st->vadreg) == 0) &&
  473. ((st->pitch & 0x7800) == 0)
  474. && (st->complex_hang_count == 0))
  475. {
  476. alpha_up = ALPHA_UP1; move16 ();
  477. alpha_down = ALPHA_DOWN1; move16 ();
  478. }
  479. else
  480. {
  481. test (); test ();
  482. if ((st->stat_count == 0)
  483. && (st->complex_hang_count == 0))
  484. {
  485. alpha_up = ALPHA_UP2; move16 ();
  486. alpha_down = ALPHA_DOWN2; move16 ();
  487. }
  488. else
  489. {
  490. alpha_up = 0; move16 ();
  491. alpha_down = ALPHA3; move16 ();
  492. bckr_add = 0; move16 ();
  493. }
  494. }
  495. /* Update noise estimate (bckr_est) */
  496. for (i = 0; i < COMPLEN; i++)
  497. {
  498. Word16 temp;
  499. temp = sub_ex(st->old_level[i], st->bckr_est[i]);
  500. test ();
  501. if (temp < 0)
  502. { /* update downwards*/
  503. st->bckr_est[i] = add_ex(-2, add_ex(st->bckr_est[i], mult_r_ex(alpha_down, temp)));
  504. move16 ();
  505. /* limit minimum value of the noise estimate to NOISE_MIN */
  506. test ();
  507. if (sub_ex(st->bckr_est[i], NOISE_MIN) < 0)
  508. {
  509. st->bckr_est[i] = NOISE_MIN; move16 ();
  510. }
  511. }
  512. else
  513. { /* update upwards */
  514. st->bckr_est[i] = add_ex(bckr_add, add_ex(st->bckr_est[i], mult_r_ex(alpha_up, temp)));
  515. move16 ();
  516. /* limit maximum value of the noise estimate to NOISE_MAX */
  517. test ();
  518. if (sub_ex(st->bckr_est[i], NOISE_MAX) > 0)
  519. {
  520. st->bckr_est[i] = NOISE_MAX; move16 ();
  521. }
  522. }
  523. }
  524. /* Update signal levels of the previous frame (old_level) */
  525. for(i = 0; i < COMPLEN; i++)
  526. {
  527. st->old_level[i] = level[i]; move16 ();
  528. }
  529. }
  530. /****************************************************************************
  531. *
  532. * Function : complex_estimate_adapt
  533. * Purpose : Update/adapt of complex signal estimate
  534. * Inputs : low_power: low signal power flag
  535. * Outputs : st->corr_hp_fast: long term complex signal estimate
  536. *
  537. ***************************************************************************/
  538. static void complex_estimate_adapt(
  539. vadState1 *st, /* i/o : VAD state struct */
  540. Word16 low_power /* i : very low level flag of the input frame */
  541. )
  542. {
  543. Word16 alpha; /* Q15 */
  544. Word32 L_tmp; /* Q31 */
  545. /* adapt speed on own state */
  546. test ();
  547. if (sub_ex(st->best_corr_hp, st->corr_hp_fast) < 0) /* decrease */
  548. {
  549. test ();
  550. if (sub_ex(st->corr_hp_fast, CVAD_THRESH_ADAPT_HIGH) < 0)
  551. { /* low state */
  552. alpha = CVAD_ADAPT_FAST; move16();
  553. }
  554. else
  555. { /* high state */
  556. alpha = CVAD_ADAPT_REALLY_FAST; move16();
  557. }
  558. }
  559. else /* increase */
  560. {
  561. test ();
  562. if (sub_ex(st->corr_hp_fast, CVAD_THRESH_ADAPT_HIGH) < 0)
  563. {
  564. alpha = CVAD_ADAPT_FAST; move16();
  565. }
  566. else
  567. {
  568. alpha = CVAD_ADAPT_SLOW; move16();
  569. }
  570. }
  571. L_tmp = L_deposit_h_ex(st->corr_hp_fast);
  572. L_tmp = L_msu_ex(L_tmp, alpha, st->corr_hp_fast);
  573. L_tmp = L_mac_ex(L_tmp, alpha, st->best_corr_hp);
  574. st->corr_hp_fast = round_ex(L_tmp); /* Q15 */ move16();
  575. test ();
  576. if (sub_ex(st->corr_hp_fast, CVAD_MIN_CORR) < 0)
  577. {
  578. st->corr_hp_fast = CVAD_MIN_CORR; move16();
  579. }
  580. test ();
  581. if (low_power != 0)
  582. {
  583. st->corr_hp_fast = CVAD_MIN_CORR; move16();
  584. }
  585. }
  586. /****************************************************************************
  587. *
  588. * Function : complex_vad
  589. * Purpose : complex background decision
  590. * Return value : the complex background decision
  591. *
  592. ***************************************************************************/
  593. static Word16 complex_vad(vadState1 *st, /* i/o : VAD state struct */
  594. Word16 low_power /* i : flag power of the input frame */
  595. )
  596. {
  597. st->complex_high = shr_ex(st->complex_high, 1); move16 ();
  598. st->complex_low = shr_ex(st->complex_low, 1); move16 ();
  599. test ();
  600. if (low_power == 0)
  601. {
  602. test ();
  603. if (sub_ex(st->corr_hp_fast, CVAD_THRESH_ADAPT_HIGH) > 0)
  604. {
  605. st->complex_high = st->complex_high | 0x4000; logic16 (); move16 ();
  606. }
  607. test ();
  608. if (sub_ex(st->corr_hp_fast, CVAD_THRESH_ADAPT_LOW) > 0 )
  609. {
  610. st->complex_low = st->complex_low | 0x4000; logic16 (); move16 ();
  611. }
  612. }
  613. test ();
  614. if (sub_ex(st->corr_hp_fast, CVAD_THRESH_HANG) > 0)
  615. {
  616. st->complex_hang_timer = add_ex(st->complex_hang_timer, 1); move16 ();
  617. }
  618. else
  619. {
  620. st->complex_hang_timer = 0; move16 ();
  621. }
  622. test (); logic16 (); test (); logic16 ();
  623. return ((sub_ex((st->complex_high & 0x7f80), 0x7f80) == 0) ||
  624. (sub_ex((st->complex_low & 0x7fff), 0x7fff) == 0));
  625. }
  626. /****************************************************************************
  627. *
  628. * Function : vad_decision
  629. * Purpose : Calculates VAD_flag
  630. * Inputs : bckr_est: background noise estimate
  631. * vadreg: intermediate VAD flags
  632. * Outputs : noise_level: average level of the noise estimates
  633. * vadreg: intermediate VAD flags
  634. * Return value : VAD_flag
  635. *
  636. ***************************************************************************/
  637. static Word16 vad_decision(
  638. vadState1 *st, /* i/o : State struct */
  639. Word16 level[COMPLEN], /* i : sub_ex-band levels of the input frame */
  640. Word32 pow_sum /* i : power of the input frame */
  641. )
  642. {
  643. Word16 i;
  644. Word16 snr_sum;
  645. Word32 L_temp;
  646. Word16 vad_thr, temp, noise_level;
  647. Word16 low_power_flag;
  648. /*
  649. Calculate squared sum of the input levels (level)
  650. divided by the background noise components (bckr_est).
  651. */
  652. L_temp = 0; move32();
  653. for (i = 0; i < COMPLEN; i++)
  654. {
  655. Word16 exp;
  656. exp = norm_s_ex(st->bckr_est[i]);
  657. temp = shl_ex(st->bckr_est[i], exp);
  658. temp = div_s(shr_ex(level[i], 1), temp);
  659. temp = shl_ex(temp, sub_ex(exp, UNIRSHFT-1));
  660. L_temp = L_mac_ex(L_temp, temp, temp);
  661. }
  662. snr_sum = extract_h_ex(L_shl_ex(L_temp, 6));
  663. snr_sum = mult_ex(snr_sum, INV_COMPLEN);
  664. /* Calculate average level of estimated background noise */
  665. L_temp = 0; move32();
  666. for (i = 0; i < COMPLEN; i++)
  667. {
  668. L_temp = L_add_ex(L_temp, st->bckr_est[i]);
  669. }
  670. noise_level = extract_h_ex(L_shl_ex(L_temp, 13));
  671. /* Calculate VAD threshold */
  672. vad_thr = add_ex(mult_ex(VAD_SLOPE, sub_ex(noise_level, VAD_P1)), VAD_THR_HIGH);
  673. test ();
  674. if (sub_ex(vad_thr, VAD_THR_LOW) < 0)
  675. {
  676. vad_thr = VAD_THR_LOW; move16 ();
  677. }
  678. /* Shift VAD decision register */
  679. st->vadreg = shr_ex(st->vadreg, 1); move16 ();
  680. /* Make intermediate VAD decision */
  681. test ();
  682. if (sub_ex(snr_sum, vad_thr) > 0)
  683. {
  684. st->vadreg = st->vadreg | 0x4000; logic16 (); move16 ();
  685. }
  686. /* primary vad decsion made */
  687. /* check if the input power (pow_sum) is lower than a threshold" */
  688. test ();
  689. if (L_sub_ex(pow_sum, VAD_POW_LOW) < 0)
  690. {
  691. low_power_flag = 1; move16 ();
  692. }
  693. else
  694. {
  695. low_power_flag = 0; move16 ();
  696. }
  697. /* update complex signal estimate st->corr_hp_fast and hangover reset timer using */
  698. /* low_power_flag and corr_hp_fast and various adaptation speeds */
  699. complex_estimate_adapt(st, low_power_flag);
  700. /* check multiple thresholds of the st->corr_hp_fast value */
  701. st->complex_warning = complex_vad(st, low_power_flag); move16();
  702. /* Update speech subband vad background noise estimates */
  703. noise_estimate_update(st, level);
  704. /* Add speech and complex hangover and return speech VAD_flag */
  705. /* long term complex hangover may be added */
  706. st->speech_vad_decision = hangover_addition(st, noise_level, low_power_flag);
  707. move16 ();
  708. return (st->speech_vad_decision);
  709. }
  710. /*
  711. *****************************************************************************
  712. * PUBLIC PROGRAM CODE
  713. *****************************************************************************
  714. */
  715. /*************************************************************************
  716. *
  717. * Function: vad1_init
  718. * Purpose: Allocates state memory and initializes state memory
  719. *
  720. **************************************************************************
  721. */
  722. int vad1_init (vadState1 **state)
  723. {
  724. vadState1* s;
  725. if (state == (vadState1 **) NULL){
  726. wfprintf(stderr, "vad_init: invalid parameter\n");
  727. return -1;
  728. }
  729. *state = NULL;
  730. /* allocate memory */
  731. if ((s = (vadState1 *) wmalloc(sizeof(vadState1))) == NULL){
  732. wfprintf(stderr, "vad_init: can not malloc state structure\n");
  733. return -1;
  734. }
  735. vad1_reset(s);
  736. *state = s;
  737. return 0;
  738. }
  739. /*************************************************************************
  740. *
  741. * Function: vad1_reset
  742. * Purpose: Initializes state memory to zero
  743. *
  744. **************************************************************************
  745. */
  746. int vad1_reset (vadState1 *state)
  747. {
  748. Word16 i, j;
  749. if (state == (vadState1 *) NULL){
  750. wfprintf(stderr, "vad_reset: invalid parameter\n");
  751. return -1;
  752. }
  753. /* Initialize pitch detection variables */
  754. state->oldlag_count = 0;
  755. state->oldlag = 0;
  756. state->pitch = 0;
  757. state->tone = 0;
  758. state->complex_high = 0;
  759. state->complex_low = 0;
  760. state->complex_hang_timer = 0;
  761. state->vadreg = 0;
  762. state->stat_count = 0;
  763. state->burst_count = 0;
  764. state->hang_count = 0;
  765. state->complex_hang_count = 0;
  766. /* initialize memory used by the filter bank */
  767. for (i = 0; i < 3; i++)
  768. {
  769. for (j = 0; j < 2; j++)
  770. {
  771. state->a_data5[i][j] = 0;
  772. }
  773. }
  774. for (i = 0; i < 5; i++)
  775. {
  776. state->a_data3[i] = 0;
  777. }
  778. /* initialize the rest of the memory */
  779. for (i = 0; i < COMPLEN; i++)
  780. {
  781. state->bckr_est[i] = NOISE_INIT;
  782. state->old_level[i] = NOISE_INIT;
  783. state->ave_level[i] = NOISE_INIT;
  784. state->sub_level[i] = 0;
  785. }
  786. state->best_corr_hp = CVAD_LOWPOW_RESET;
  787. state->speech_vad_decision = 0;
  788. state->complex_warning = 0;
  789. state->sp_burst_count = 0;
  790. state->corr_hp_fast = CVAD_LOWPOW_RESET;
  791. return 0;
  792. }
  793. /*************************************************************************
  794. *
  795. * Function: vad1_exit
  796. * Purpose: The memory used for state memory is freed
  797. *
  798. **************************************************************************
  799. */
  800. void vad1_exit (vadState1 **state)
  801. {
  802. if (state == NULL || *state == NULL)
  803. return;
  804. /* deallocate memory */
  805. wfree(*state);
  806. *state = NULL;
  807. return;
  808. }
  809. /****************************************************************************
  810. *
  811. * Function : vad_complex_detection_update
  812. * Purpose : update vad->bestCorr_hp complex signal feature state
  813. *
  814. ***************************************************************************/
  815. void vad_complex_detection_update (vadState1 *st, /* i/o : State struct */
  816. Word16 best_corr_hp /* i : best Corr */
  817. )
  818. {
  819. st->best_corr_hp = best_corr_hp; move16();
  820. }
  821. /****************************************************************************
  822. *
  823. * Function : vad_tone_detection
  824. * Purpose : Set tone flag if pitch gain is high. This is used to detect
  825. * signaling tones and other signals with high pitch gain.
  826. * Inputs : tone: flags indicating presence of a tone
  827. * Outputs : tone: flags indicating presence of a tone
  828. *
  829. ***************************************************************************/
  830. void vad_tone_detection (vadState1 *st, /* i/o : State struct */
  831. Word32 t0, /* i : autocorrelation maxima */
  832. Word32 t1 /* i : energy */
  833. )
  834. {
  835. Word16 temp;
  836. /*
  837. if (t0 > TONE_THR * t1)
  838. set tone flag
  839. */
  840. temp = round_ex(t1);
  841. test (); test ();
  842. if ((temp > 0) && (L_msu_ex(t0, temp, TONE_THR) > 0))
  843. {
  844. st->tone = st->tone | 0x4000; logic16 (); move16 ();
  845. }
  846. }
  847. /****************************************************************************
  848. *
  849. * Function : vad_tone_detection_update
  850. * Purpose : Update the tone flag register. Tone flags are shifted right
  851. * by one bit. This function should be called from the speech
  852. * encoder before call to Vad_tone_detection() function.
  853. *
  854. ***************************************************************************/
  855. void vad_tone_detection_update (
  856. vadState1 *st, /* i/o : State struct */
  857. Word16 one_lag_per_frame /* i : 1 if one open-loop lag is
  858. calculated per each frame,
  859. otherwise 0 */
  860. )
  861. {
  862. /* Shift tone flags right by one bit */
  863. st->tone = shr_ex(st->tone, 1); move16 ();
  864. /* If open-loop lag is calculated only once in each frame, do extra update
  865. and assume that the other tone flag of the frame is one. */
  866. if (one_lag_per_frame != 0)
  867. {
  868. st->tone = shr_ex(st->tone, 1);
  869. st->tone = st->tone | 0x2000; logic16 (); move16 ();
  870. }
  871. }
  872. /****************************************************************************
  873. *
  874. * Function : vad_pitch_detection
  875. * Purpose : Test whether signal contains pitch or other periodic
  876. * component.
  877. * Return value : Boolean voiced / unvoiced decision in state variable
  878. *
  879. ***************************************************************************/
  880. void vad_pitch_detection (vadState1 *st, /* i/o : State struct */
  881. Word16 T_op[] /* i : speech encoder open loop lags */
  882. )
  883. {
  884. Word16 lagcount, i;
  885. lagcount = 0; move16 ();
  886. for (i = 0; i < 2; i++)
  887. {
  888. test ();
  889. if (sub_ex (abs_s_ex (sub_ex (st->oldlag, T_op[i])), LTHRESH) < 0)
  890. {
  891. lagcount = add_ex (lagcount, 1);
  892. }
  893. /* Save the current LTP lag */
  894. st->oldlag = T_op[i]; move16 ();
  895. }
  896. /* Make pitch decision.
  897. Save flag of the pitch detection to the variable pitch.
  898. */
  899. st->pitch = shr_ex(st->pitch, 1); move16();
  900. test ();
  901. if (sub_ex ( add_ex (st->oldlag_count, lagcount), NTHRESH) >= 0)
  902. {
  903. st->pitch = st->pitch | 0x4000; logic16(); move16();
  904. }
  905. /* Update oldlagcount */
  906. st->oldlag_count = lagcount; move16 ();
  907. }
  908. /****************************************************************************
  909. *
  910. * Function : vad
  911. * Purpose : Main program for Voice Activity Detection (VAD) for AMR
  912. * Return value : VAD Decision, 1 = speech, 0 = noise
  913. *
  914. ***************************************************************************/
  915. Word16 vad1(vadState1 *st, /* i/o : State struct */
  916. Word16 in_buf[] /* i : samples of the input frame */
  917. )
  918. {
  919. Word16 level[COMPLEN];
  920. Word32 pow_sum;
  921. Word16 i;
  922. /* Calculate power of the input frame. */
  923. pow_sum = 0L; move32 ();
  924. for (i = 0; i < FRAME_LEN; i++)
  925. {
  926. pow_sum = L_mac_ex(pow_sum, in_buf[i-LOOKAHEAD], in_buf[i-LOOKAHEAD]);
  927. }
  928. /*
  929. If input power is very low, clear pitch flag of the current frame
  930. */
  931. test ();
  932. if (L_sub_ex(pow_sum, POW_PITCH_THR) < 0)
  933. {
  934. st->pitch = st->pitch & 0x3fff; logic16 (); move16 ();
  935. }
  936. /*
  937. If input power is very low, clear complex flag of the "current" frame
  938. */
  939. test ();
  940. if (L_sub_ex(pow_sum, POW_COMPLEX_THR) < 0)
  941. {
  942. st->complex_low = st->complex_low & 0x3fff; logic16 (); move16 ();
  943. }
  944. /*
  945. Run the filter bank which calculates signal levels at each band
  946. */
  947. filter_bank(st, in_buf, level);
  948. return (vad_decision(st, level, pow_sum));
  949. }