vad2.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914
  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 : vad2.c
  11. * Purpose : Voice Activity Detection (VAD) for AMR (option 2)
  12. *
  13. *****************************************************************************
  14. */
  15. /*
  16. *****************************************************************************
  17. * VERSION ID
  18. *****************************************************************************
  19. */
  20. const char vad2_id[] = "@(#)$Id $";
  21. /***************************************************************************
  22. *
  23. * FUNCTION NAME: vad2()
  24. *
  25. * PURPOSE:
  26. * This function provides the Voice Activity Detection function option 2
  27. * for the Adaptive Multi-rate (AMR) codec.
  28. *
  29. * INPUTS:
  30. *
  31. * farray_ptr
  32. * pointer to Word16[80] input array
  33. * vadState2
  34. * pointer to vadState2 state structure
  35. *
  36. * OUTPUTS:
  37. *
  38. * state variables are updated
  39. *
  40. * RETURN VALUE:
  41. *
  42. * Word16
  43. * VAD(m) - two successive calls to vad2() yield
  44. * the VAD decision for the 20 ms frame:
  45. * VAD_flag = VAD(m-1) || VAD(m)
  46. *
  47. *
  48. *************************************************************************/
  49. /* Includes */
  50. #include <stdio.h>
  51. #include <stdlib.h>
  52. #include "typedef.h"
  53. #include "cnst.h"
  54. #include "basic_op.h"
  55. #include "oper_32b.h"
  56. #include "count.h"
  57. #include "log2.h"
  58. #include "pow2.h"
  59. #include "vad2.h"
  60. /* Local functions */
  61. /***************************************************************************
  62. *
  63. * FUNCTION NAME: fn10Log10
  64. *
  65. * PURPOSE:
  66. * The purpose of this function is to take the 10*log base 10 of input and
  67. * divide by 128 and return; i.e. output = 10*log10(input)/128 (scaled as 7,8)
  68. *
  69. * INPUTS:
  70. *
  71. * L_Input
  72. * input (scaled as 31-fbits,fbits)
  73. * fbits
  74. * number of fractional bits on input
  75. *
  76. * OUTPUTS:
  77. *
  78. * none
  79. *
  80. * RETURN VALUE:
  81. *
  82. * Word16
  83. * output (scaled as 7,8)
  84. *
  85. * DESCRIPTION:
  86. *
  87. * 10*log10(x)/128 = 10*(log10(2) * (log2(x<<fbits)-log2(1<<fbits)) >> 7
  88. * = 3.0103 * (log2(x<<fbits) - fbits) >> 7
  89. * = ((3.0103/4.0 * (log2(x<<fbits) - fbits) << 2) >> 7
  90. * = (3.0103/4.0 * (log2(x<<fbits) - fbits) >> 5
  91. *
  92. *************************************************************************/
  93. Word16 fn10Log10 (Word32 L_Input, Word16 fbits)
  94. {
  95. Word16 integer; /* Integer part of Log2. (range: 0<=val<=30) */
  96. Word16 fraction; /* Fractional part of Log2. (range: 0<=val<1) */
  97. Word32 Ltmp;
  98. Word16 tmp;
  99. Log2(L_Input, &integer, &fraction);
  100. integer = sub_ex(integer, fbits);
  101. Ltmp = Mpy_32_16 (integer, fraction, 24660); /* 24660 = 10*log10(2)/4 scaled 0,15 */
  102. Ltmp = L_shr_r_ex(Ltmp, 5+1); /* extra shift for 30,1 => 15,0 extract correction */
  103. tmp = extract_l_ex(Ltmp);
  104. return (tmp);
  105. }
  106. /***************************************************************************
  107. *
  108. * FUNCTION NAME: block_norm_ex
  109. *
  110. * PURPOSE:
  111. * The purpose of this function is block normalise the input data sequence
  112. *
  113. * INPUTS:
  114. *
  115. * &in[0]
  116. * pointer to data sequence to be normalised
  117. * length
  118. * number of elements in data sequence
  119. * headroom
  120. * number of headroom bits (i.e.,
  121. *
  122. * OUTPUTS:
  123. *
  124. * &out[0]
  125. * normalised output data sequence pointed to by &out[0]
  126. *
  127. * RETURN VALUE:
  128. *
  129. * Word16
  130. * number of bits sequence was left shifted
  131. *
  132. * DESCRIPTION:
  133. *
  134. * 1) Search for maximum absolute valued data element
  135. * 2) Normalise the max element with "headroom"
  136. * 3) Transfer/shift the input sequence to the output buffer
  137. * 4) Return the number of left shifts
  138. *
  139. * CAVEATS:
  140. * An input sequence of all zeros will return the maximum
  141. * number of left shifts allowed, NOT the value returned
  142. * by a norm_s_ex(0) call, since it desired to associate an
  143. * all zeros sequence with low energy.
  144. *
  145. *************************************************************************/
  146. Word16 block_norm_ex (Word16 * in, Word16 * out, Word16 length, Word16 headroom)
  147. {
  148. Word16 i, max, scnt, adata;
  149. max = abs_s_ex(in[0]);
  150. for (i = 1; i < length; i++)
  151. {
  152. adata = abs_s_ex(in[i]); test();
  153. if (sub_ex(adata, max) > 0)
  154. {
  155. max = adata; move16();
  156. }
  157. }
  158. test();
  159. if (max != 0)
  160. {
  161. scnt = sub_ex(norm_s_ex(max), headroom);
  162. for (i = 0; i < length; i++)
  163. {
  164. out[i] = shl_ex(in[i], scnt); move16();
  165. }
  166. }
  167. else
  168. {
  169. scnt = sub_ex(16, headroom);
  170. for (i = 0; i < length; i++)
  171. {
  172. out[i] = 0; move16();
  173. }
  174. }
  175. return (scnt);
  176. }
  177. /********************************************* The VAD function ***************************************************/
  178. Word16 vad2 (Word16 * farray_ptr, vadState2 * st)
  179. {
  180. /*
  181. * The channel table is defined below. In this table, the
  182. * lower and higher frequency coefficients for each of the 16
  183. * channels are specified. The table excludes the coefficients
  184. * with numbers 0 (DC), 1, and 64 (Foldover frequency).
  185. */
  186. static Word16 ch_tbl[NUM_CHAN][2] =
  187. {
  188. {2, 3},
  189. {4, 5},
  190. {6, 7},
  191. {8, 9},
  192. {10, 11},
  193. {12, 13},
  194. {14, 16},
  195. {17, 19},
  196. {20, 22},
  197. {23, 26},
  198. {27, 30},
  199. {31, 35},
  200. {36, 41},
  201. {42, 48},
  202. {49, 55},
  203. {56, 63}
  204. };
  205. /* channel energy scaling table - allows efficient division by number
  206. * of DFT bins in the channel: 1/2, 1/3, 1/4, etc.
  207. */
  208. static Word16 ch_tbl_sh[NUM_CHAN] =
  209. {
  210. 16384, 16384, 16384, 16384, 16384, 16384, 10923, 10923,
  211. 10923, 8192, 8192, 6554, 5461, 4681, 4681, 4096
  212. };
  213. /*
  214. * The voice metric table is defined below. It is a non-
  215. * linear table with a deadband near zero. It maps the SNR
  216. * index (quantized SNR value) to a number that is a measure
  217. * of voice quality.
  218. */
  219. static Word16 vm_tbl[90] =
  220. {
  221. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  222. 3, 3, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 7, 7, 7,
  223. 8, 8, 9, 9, 10, 10, 11, 12, 12, 13, 13, 14, 15,
  224. 15, 16, 17, 17, 18, 19, 20, 20, 21, 22, 23, 24,
  225. 24, 25, 26, 27, 28, 28, 29, 30, 31, 32, 33, 34,
  226. 35, 36, 37, 37, 38, 39, 40, 41, 42, 43, 44, 45,
  227. 46, 47, 48, 49, 50, 50, 50, 50, 50, 50, 50, 50,
  228. 50, 50
  229. };
  230. /* hangover as a function of peak SNR (3 dB steps) */
  231. static Word16 hangover_table[20] =
  232. {
  233. 30, 30, 30, 30, 30, 30, 28, 26, 24, 22, 20, 18, 16, 14, 12, 10, 8, 8, 8, 8
  234. };
  235. /* burst sensitivity as a function of peak SNR (3 dB steps) */
  236. static Word16 burstcount_table[20] =
  237. {
  238. 8, 8, 8, 8, 8, 8, 8, 8, 7, 6, 5, 4, 4, 4, 4, 4, 4, 4, 4, 4
  239. };
  240. /* voice metric sensitivity as a function of peak SNR (3 dB steps) */
  241. static Word16 vm_threshold_table[20] =
  242. {
  243. 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 34, 40, 51, 71, 100, 139, 191, 257, 337, 432
  244. };
  245. /* State tables that use 22,9 or 27,4 scaling for ch_enrg[] */
  246. static Word16 noise_floor_chan[2] = {NOISE_FLOOR_CHAN_0, NOISE_FLOOR_CHAN_1};
  247. static Word16 min_chan_enrg[2] = {MIN_CHAN_ENRG_0, MIN_CHAN_ENRG_1};
  248. static Word16 ine_noise[2] = {INE_NOISE_0, INE_NOISE_1};
  249. static Word16 fbits[2] = {FRACTIONAL_BITS_0, FRACTIONAL_BITS_1};
  250. static Word16 state_change_shift_r[2] = {STATE_1_TO_0_SHIFT_R, STATE_0_TO_1_SHIFT_R};
  251. /* Energy scale table given 30,1 input scaling (also account for -6 dB shift on input) */
  252. static Word16 enrg_norm_shift[2] = {(FRACTIONAL_BITS_0-1+2), (FRACTIONAL_BITS_1-1+2)};
  253. /* Automatic variables */
  254. Word32 Lenrg; /* scaled as 30,1 */
  255. Word32 Ltne; /* scaled as 22,9 */
  256. Word32 Ltce; /* scaled as 22,9 or 27,4 */
  257. Word16 tne_db; /* scaled as 7,8 */
  258. Word16 tce_db; /* scaled as 7,8 */
  259. Word16 input_buffer[FRM_LEN]; /* used for block normalising input data */
  260. Word16 data_buffer[FFT_LEN]; /* used for in-place FFT */
  261. Word16 ch_snr[NUM_CHAN]; /* scaled as 7,8 */
  262. Word16 ch_snrq; /* scaled as 15,0 (in 0.375 dB steps) */
  263. Word16 vm_sum; /* scaled as 15,0 */
  264. Word16 ch_enrg_dev; /* scaled as 7,8 */
  265. Word32 Lpeak; /* maximum channel energy */
  266. Word16 p2a_flag; /* flag to indicate spectral peak-to-average ratio > 10 dB */
  267. Word16 ch_enrg_db[NUM_CHAN]; /* scaled as 7,8 */
  268. Word16 ch_noise_db; /* scaled as 7,8 */
  269. Word16 alpha; /* scaled as 0,15 */
  270. Word16 one_m_alpha; /* scaled as 0,15 */
  271. Word16 update_flag; /* set to indicate a background noise estimate update */
  272. Word16 i, j, j1, j2; /* Scratch variables */
  273. Word16 hi1, lo1;
  274. Word32 Ltmp, Ltmp1, Ltmp2;
  275. Word16 tmp;
  276. Word16 normb_shift; /* block norm shift count */
  277. Word16 ivad; /* intermediate VAD decision (return value) */
  278. Word16 tsnrq; /* total signal-to-noise ratio (quantized 3 dB steps) scaled as 15,0 */
  279. Word16 xt; /* instantaneous frame SNR in dB, scaled as 7,8 */
  280. Word16 state_change;
  281. /* Increment frame counter */
  282. st->Lframe_cnt = L_add_ex(st->Lframe_cnt, 1);
  283. /* Block normalize the input */
  284. normb_shift = block_norm_ex(farray_ptr, input_buffer, FRM_LEN, FFT_HEADROOM);
  285. /* Pre-emphasize the input data and store in the data buffer with the appropriate offset */
  286. for (i = 0; i < DELAY; i++)
  287. {
  288. data_buffer[i] = 0; move16();
  289. }
  290. st->pre_emp_mem = shr_r_ex(st->pre_emp_mem, sub_ex(st->last_normb_shift, normb_shift));
  291. st->last_normb_shift = normb_shift; move16();
  292. data_buffer[DELAY] = add_ex(input_buffer[0], mult_ex(PRE_EMP_FAC, st->pre_emp_mem)); move16();
  293. for (i = DELAY + 1, j = 1; i < DELAY + FRM_LEN; i++, j++)
  294. {
  295. data_buffer[i] = add_ex(input_buffer[j], mult_ex(PRE_EMP_FAC, input_buffer[j-1])); move16();
  296. }
  297. st->pre_emp_mem = input_buffer[FRM_LEN-1]; move16();
  298. for (i = DELAY + FRM_LEN; i < FFT_LEN; i++)
  299. {
  300. data_buffer[i] = 0; move16();
  301. }
  302. /* Perform FFT on the data buffer */
  303. r_fft_ex(data_buffer);
  304. /* Use normb_shift factor to determine the scaling of the energy estimates */
  305. state_change = 0; move16();
  306. test();
  307. if (st->shift_state == 0)
  308. { test();
  309. if (sub_ex(normb_shift, -FFT_HEADROOM+2) <= 0)
  310. {
  311. state_change = 1; move16();
  312. st->shift_state = 1; move16();
  313. }
  314. }
  315. else
  316. { test();
  317. if (sub_ex(normb_shift, -FFT_HEADROOM+5) >= 0)
  318. {
  319. state_change = 1; move16();
  320. st->shift_state = 0; move16();
  321. }
  322. }
  323. /* Scale channel energy estimate */ test();
  324. if (state_change)
  325. {
  326. for (i = LO_CHAN; i <= HI_CHAN; i++)
  327. {
  328. st->Lch_enrg[i] = L_shr_ex(st->Lch_enrg[i], state_change_shift_r[st->shift_state]); move32();
  329. }
  330. }
  331. /* Estimate the energy in each channel */
  332. test();
  333. if (L_sub_ex(st->Lframe_cnt, 1) == 0)
  334. {
  335. alpha = 32767; move16();
  336. one_m_alpha = 0; move16();
  337. }
  338. else
  339. {
  340. alpha = CEE_SM_FAC; move16();
  341. one_m_alpha = ONE_MINUS_CEE_SM_FAC; move16();
  342. }
  343. for (i = LO_CHAN; i <= HI_CHAN; i++)
  344. {
  345. Lenrg = 0; move16();
  346. j1 = ch_tbl[i][0]; move16();
  347. j2 = ch_tbl[i][1]; move16();
  348. for (j = j1; j <= j2; j++)
  349. {
  350. Lenrg = L_mac_ex(Lenrg, data_buffer[2 * j], data_buffer[2 * j]);
  351. Lenrg = L_mac_ex(Lenrg, data_buffer[2 * j + 1], data_buffer[2 * j + 1]);
  352. }
  353. /* Denorm energy & scale 30,1 according to the state */
  354. Lenrg = L_shr_r_ex(Lenrg, sub_ex(shl_ex(normb_shift, 1), enrg_norm_shift[st->shift_state]));
  355. /* integrate over time: e[i] = (1-alpha)*e[i] + alpha*enrg/num_bins_in_chan */
  356. tmp = mult_ex(alpha, ch_tbl_sh[i]);
  357. L_Extract (Lenrg, &hi1, &lo1);
  358. Ltmp = Mpy_32_16(hi1, lo1, tmp);
  359. L_Extract (st->Lch_enrg[i], &hi1, &lo1);
  360. st->Lch_enrg[i] = L_add_ex(Ltmp, Mpy_32_16(hi1, lo1, one_m_alpha)); move32();
  361. test();
  362. if (L_sub_ex(st->Lch_enrg[i], min_chan_enrg[st->shift_state]) < 0)
  363. {
  364. st->Lch_enrg[i] = min_chan_enrg[st->shift_state]; move32();
  365. }
  366. }
  367. /* Compute the total channel energy estimate (Ltce) */
  368. Ltce = 0; move16();
  369. for (i = LO_CHAN; i <= HI_CHAN; i++)
  370. {
  371. Ltce = L_add_ex(Ltce, st->Lch_enrg[i]);
  372. }
  373. /* Calculate spectral peak-to-average ratio, set flag if p2a > 10 dB */
  374. Lpeak = 0; move32();
  375. for (i = LO_CHAN+2; i <= HI_CHAN; i++) /* Sine waves not valid for low frequencies */
  376. { test();
  377. if (L_sub_ex(st->Lch_enrg [i], Lpeak) > 0)
  378. {
  379. Lpeak = st->Lch_enrg [i]; move32();
  380. }
  381. }
  382. /* Set p2a_flag if peak (dB) > average channel energy (dB) + 10 dB */
  383. /* Lpeak > Ltce/num_channels * 10^(10/10) */
  384. /* Lpeak > (10/16)*Ltce */
  385. L_Extract (Ltce, &hi1, &lo1);
  386. Ltmp = Mpy_32_16(hi1, lo1, 20480);
  387. test();
  388. if (L_sub_ex(Lpeak, Ltmp) > 0)
  389. {
  390. p2a_flag = TRUE; move16();
  391. }
  392. else
  393. {
  394. p2a_flag = FALSE; move16();
  395. }
  396. /* Initialize channel noise estimate to either the channel energy or fixed level */
  397. /* Scale the energy appropriately to yield state 0 (22,9) scaling for noise */
  398. test();
  399. if (L_sub_ex(st->Lframe_cnt, 4) <= 0)
  400. { test();
  401. if (p2a_flag == TRUE)
  402. {
  403. for (i = LO_CHAN; i <= HI_CHAN; i++)
  404. {
  405. st->Lch_noise[i] = INE_NOISE_0; move32();
  406. }
  407. }
  408. else
  409. {
  410. for (i = LO_CHAN; i <= HI_CHAN; i++)
  411. { test();
  412. if (L_sub_ex(st->Lch_enrg[i], ine_noise[st->shift_state]) < 0)
  413. {
  414. st->Lch_noise[i] = INE_NOISE_0; move32();
  415. }
  416. else
  417. { test();
  418. if (st->shift_state == 1)
  419. {
  420. st->Lch_noise[i] = L_shr_ex(st->Lch_enrg[i], state_change_shift_r[0]);
  421. move32();
  422. }
  423. else
  424. {
  425. st->Lch_noise[i] = st->Lch_enrg[i]; move32();
  426. }
  427. }
  428. }
  429. }
  430. }
  431. /* Compute the channel energy (in dB), the channel SNRs, and the sum of voice metrics */
  432. vm_sum = 0; move16();
  433. for (i = LO_CHAN; i <= HI_CHAN; i++)
  434. {
  435. ch_enrg_db[i] = fn10Log10(st->Lch_enrg[i], fbits[st->shift_state]); move16();
  436. ch_noise_db = fn10Log10(st->Lch_noise[i], FRACTIONAL_BITS_0);
  437. ch_snr[i] = sub_ex(ch_enrg_db[i], ch_noise_db); move16();
  438. /* quantize channel SNR in 3/8 dB steps (scaled 7,8 => 15,0) */
  439. /* ch_snr = round((snr/(3/8))>>8) */
  440. /* = round(((0.6667*snr)<<2)>>8) */
  441. /* = round((0.6667*snr)>>6) */
  442. ch_snrq = shr_r_ex(mult_ex(21845, ch_snr[i]), 6);
  443. /* Accumulate the sum of voice metrics */ test();
  444. if (sub_ex(ch_snrq, 89) < 0)
  445. { test();
  446. if (ch_snrq > 0)
  447. {
  448. j = ch_snrq; move16();
  449. }
  450. else
  451. {
  452. j = 0; move16();
  453. }
  454. }
  455. else
  456. {
  457. j = 89; move16();
  458. }
  459. vm_sum = add_ex(vm_sum, vm_tbl[j]);
  460. }
  461. /* Initialize NOMINAL peak voice energy and average noise energy, calculate instantaneous SNR */
  462. test(),test(),logic16();
  463. if (L_sub_ex(st->Lframe_cnt, 4) <= 0 || st->fupdate_flag == TRUE)
  464. {
  465. /* tce_db = (96 - 22 - 10*log10(64) (due to FFT)) scaled as 7,8 */
  466. tce_db = 14320; move16();
  467. st->negSNRvar = 0; move16();
  468. st->negSNRbias = 0; move16();
  469. /* Compute the total noise estimate (Ltne) */
  470. Ltne = 0; move32();
  471. for (i = LO_CHAN; i <= HI_CHAN; i++)
  472. {
  473. Ltne = L_add_ex(Ltne, st->Lch_noise[i]);
  474. }
  475. /* Get total noise in dB */
  476. tne_db = fn10Log10(Ltne, FRACTIONAL_BITS_0);
  477. /* Initialise instantaneous and long-term peak signal-to-noise ratios */
  478. xt = sub_ex(tce_db, tne_db);
  479. st->tsnr = xt; move16();
  480. }
  481. else
  482. {
  483. /* Calculate instantaneous frame signal-to-noise ratio */
  484. /* xt = 10*log10( sum(2.^(ch_snr*0.1*log2(10)))/length(ch_snr) ) */
  485. Ltmp1 = 0; move32();
  486. for (i=LO_CHAN; i<=HI_CHAN; i++) {
  487. /* Ltmp2 = ch_snr[i] * 0.1 * log2(10); (ch_snr scaled as 7,8) */
  488. Ltmp2 = L_shr_ex(L_mult_ex(ch_snr[i], 10885), 8);
  489. L_Extract(Ltmp2, &hi1, &lo1);
  490. hi1 = add_ex(hi1, 3); /* 2^3 to compensate for negative SNR */
  491. Ltmp1 = L_add_ex(Ltmp1, Pow2(hi1, lo1));
  492. }
  493. xt = fn10Log10(Ltmp1, 4+3); /* average by 16, inverse compensation 2^3 */
  494. /* Estimate long-term "peak" SNR */ test(),test();
  495. if (sub_ex(xt, st->tsnr) > 0)
  496. {
  497. /* tsnr = 0.9*tsnr + 0.1*xt; */
  498. st->tsnr = round_ex(L_add_ex(L_mult_ex(29491, st->tsnr), L_mult_ex(3277, xt)));
  499. }
  500. /* else if (xt > 0.625*tsnr) */
  501. else if (sub_ex(xt, mult_ex(20480, st->tsnr)) > 0)
  502. {
  503. /* tsnr = 0.998*tsnr + 0.002*xt; */
  504. st->tsnr = round_ex(L_add_ex(L_mult_ex(32702, st->tsnr), L_mult_ex(66, xt)));
  505. }
  506. }
  507. /* Quantize the long-term SNR in 3 dB steps, limit to 0 <= tsnrq <= 19 */
  508. tsnrq = shr_ex(mult_ex(st->tsnr, 10923), 8);
  509. /* tsnrq = min(19, max(0, tsnrq)); */ test(),test();
  510. if (sub_ex(tsnrq, 19) > 0)
  511. {
  512. tsnrq = 19; move16();
  513. }
  514. else if (tsnrq < 0)
  515. {
  516. tsnrq = 0; move16();
  517. }
  518. /* Calculate the negative SNR sensitivity bias */
  519. test();
  520. if (xt < 0)
  521. {
  522. /* negSNRvar = 0.99*negSNRvar + 0.01*xt*xt; */
  523. /* xt scaled as 7,8 => xt*xt scaled as 14,17, shift to 7,8 and round */
  524. tmp = round_ex(L_shl_ex(L_mult_ex(xt, xt), 7));
  525. st->negSNRvar = round_ex(L_add_ex(L_mult_ex(32440, st->negSNRvar), L_mult_ex(328, tmp)));
  526. /* if (negSNRvar > 4.0) negSNRvar = 4.0; */ test();
  527. if (sub_ex(st->negSNRvar, 1024) > 0)
  528. {
  529. st->negSNRvar = 1024; move16();
  530. }
  531. /* negSNRbias = max(12.0*(negSNRvar - 0.65), 0.0); */
  532. tmp = mult_r_ex(shl_ex(sub_ex(st->negSNRvar, 166), 4), 24576); test();
  533. if (tmp < 0)
  534. {
  535. st->negSNRbias = 0; move16();
  536. }
  537. else
  538. {
  539. st->negSNRbias = shr_ex(tmp, 8);
  540. }
  541. }
  542. /* Determine VAD as a function of the voice metric sum and quantized SNR */
  543. tmp = add_ex(vm_threshold_table[tsnrq], st->negSNRbias); test();
  544. if (sub_ex(vm_sum, tmp) > 0)
  545. {
  546. ivad = 1; move16();
  547. st->burstcount = add_ex(st->burstcount, 1); test();
  548. if (sub_ex(st->burstcount, burstcount_table[tsnrq]) > 0)
  549. {
  550. st->hangover = hangover_table[tsnrq]; move16();
  551. }
  552. }
  553. else
  554. {
  555. st->burstcount = 0; move16();
  556. st->hangover = sub_ex(st->hangover, 1); test();
  557. if (st->hangover <= 0)
  558. {
  559. ivad = 0; move16();
  560. st->hangover = 0; move16();
  561. }
  562. else
  563. {
  564. ivad = 1; move16();
  565. }
  566. }
  567. /* Calculate log spectral deviation */
  568. ch_enrg_dev = 0; move16();
  569. test();
  570. if (L_sub_ex(st->Lframe_cnt, 1) == 0)
  571. {
  572. for (i = LO_CHAN; i <= HI_CHAN; i++)
  573. {
  574. st->ch_enrg_long_db[i] = ch_enrg_db[i]; move16();
  575. }
  576. }
  577. else
  578. {
  579. for (i = LO_CHAN; i <= HI_CHAN; i++)
  580. {
  581. tmp = abs_s_ex(sub_ex(st->ch_enrg_long_db[i], ch_enrg_db[i]));
  582. ch_enrg_dev = add_ex(ch_enrg_dev, tmp);
  583. }
  584. }
  585. /*
  586. * Calculate long term integration constant as a function of instantaneous SNR
  587. * (i.e., high SNR (tsnr dB) -> slower integration (alpha = HIGH_ALPHA),
  588. * low SNR (0 dB) -> faster integration (alpha = LOW_ALPHA)
  589. */
  590. /* alpha = HIGH_ALPHA - ALPHA_RANGE * (tsnr - xt) / tsnr, low <= alpha <= high */
  591. tmp = sub_ex(st->tsnr, xt); test(),logic16(),test(),test();
  592. if (tmp <= 0 || st->tsnr <= 0)
  593. {
  594. alpha = HIGH_ALPHA; move16();
  595. one_m_alpha = 32768L-HIGH_ALPHA; move16();
  596. }
  597. else if (sub_ex(tmp, st->tsnr) > 0)
  598. {
  599. alpha = LOW_ALPHA; move16();
  600. one_m_alpha = 32768L-LOW_ALPHA; move16();
  601. }
  602. else
  603. {
  604. tmp = div_s(tmp, st->tsnr);
  605. alpha = sub_ex(HIGH_ALPHA, mult_ex(ALPHA_RANGE, tmp));
  606. one_m_alpha = sub_ex(32767, alpha);
  607. }
  608. /* Calc long term log spectral energy */
  609. for (i = LO_CHAN; i <= HI_CHAN; i++)
  610. {
  611. Ltmp1 = L_mult_ex(one_m_alpha, ch_enrg_db[i]);
  612. Ltmp2 = L_mult_ex(alpha, st->ch_enrg_long_db[i]);
  613. st->ch_enrg_long_db[i] = round_ex(L_add_ex(Ltmp1, Ltmp2));
  614. }
  615. /* Set or clear the noise update flags */
  616. update_flag = FALSE; move16();
  617. st->fupdate_flag = FALSE; move16();
  618. test(),test();
  619. if (sub_ex(vm_sum, UPDATE_THLD) <= 0)
  620. { test();
  621. if (st->burstcount == 0)
  622. {
  623. update_flag = TRUE; move16();
  624. st->update_cnt = 0; move16();
  625. }
  626. }
  627. else if (L_sub_ex(Ltce, noise_floor_chan[st->shift_state]) > 0)
  628. { test();
  629. if (sub_ex(ch_enrg_dev, DEV_THLD) < 0)
  630. { test();
  631. if (p2a_flag == FALSE)
  632. { test();
  633. if (st->LTP_flag == FALSE)
  634. {
  635. st->update_cnt = add_ex(st->update_cnt, 1); test();
  636. if (sub_ex(st->update_cnt, UPDATE_CNT_THLD) >= 0)
  637. {
  638. update_flag = TRUE; move16();
  639. st->fupdate_flag = TRUE; move16();
  640. }
  641. }
  642. }
  643. }
  644. }
  645. test();
  646. if (sub_ex(st->update_cnt, st->last_update_cnt) == 0)
  647. {
  648. st->hyster_cnt = add_ex(st->hyster_cnt, 1);
  649. }
  650. else
  651. {
  652. st->hyster_cnt = 0; move16();
  653. }
  654. st->last_update_cnt = st->update_cnt; move16();
  655. test();
  656. if (sub_ex(st->hyster_cnt, HYSTER_CNT_THLD) > 0)
  657. {
  658. st->update_cnt = 0; move16();
  659. }
  660. /* Conditionally update the channel noise estimates */
  661. test();
  662. if (update_flag == TRUE)
  663. {
  664. /* Check shift state */ test();
  665. if (st->shift_state == 1)
  666. {
  667. /* get factor to shift ch_enrg[] from state 1 to 0 (noise always state 0) */
  668. tmp = state_change_shift_r[0]; move16();
  669. }
  670. else
  671. {
  672. /* No shift if already state 0 */
  673. tmp = 0; move16();
  674. }
  675. /* Update noise energy estimate */
  676. for (i = LO_CHAN; i <= HI_CHAN; i++)
  677. { test();
  678. /* integrate over time: en[i] = (1-alpha)*en[i] + alpha*e[n] */
  679. /* (extract with shift compensation for state 1) */
  680. L_Extract (L_shr_ex(st->Lch_enrg[i], tmp), &hi1, &lo1);
  681. Ltmp = Mpy_32_16(hi1, lo1, CNE_SM_FAC);
  682. L_Extract (st->Lch_noise[i], &hi1, &lo1);
  683. st->Lch_noise[i] = L_add_ex(Ltmp, Mpy_32_16(hi1, lo1, ONE_MINUS_CNE_SM_FAC)); move32();
  684. /* Limit low level noise */ test();
  685. if (L_sub_ex(st->Lch_noise[i], MIN_NOISE_ENRG_0) < 0)
  686. {
  687. st->Lch_noise[i] = MIN_NOISE_ENRG_0; move32();
  688. }
  689. }
  690. }
  691. return(ivad);
  692. } /* end of vad2 () */
  693. /**** Other related functions *****/
  694. /*************************************************************************
  695. *
  696. * Function: vad2_init
  697. * Purpose: Allocates state memory and initializes state memory
  698. *
  699. **************************************************************************
  700. */
  701. int vad2_init (vadState2 **state)
  702. {
  703. vadState2* s;
  704. if (state == (vadState2 **) NULL){
  705. wfprintf(stderr, "vad2_init: invalid parameter\n");
  706. return -1;
  707. }
  708. *state = NULL;
  709. /* allocate memory */
  710. if ((s = (vadState2 *) wmalloc(sizeof(vadState2))) == NULL){
  711. wfprintf(stderr, "vad2_init: can not malloc state structure\n");
  712. return -1;
  713. }
  714. vad2_reset(s);
  715. *state = s;
  716. return 0;
  717. }
  718. /***************************************************************************
  719. *
  720. * FUNCTION NAME: vad2_reset()
  721. *
  722. * PURPOSE:
  723. * The purpose of this function is to initialise the vad2() state
  724. * variables.
  725. *
  726. * INPUTS:
  727. *
  728. * &st
  729. * pointer to data structure of vad2 state variables
  730. *
  731. * OUTPUTS:
  732. *
  733. * none
  734. *
  735. * RETURN VALUE:
  736. *
  737. * none
  738. *
  739. * DESCRIPTION:
  740. *
  741. * Set all values in vad2 state to zero. Since it is
  742. * known that all elements in the structure contain
  743. * 16 and 32 bit fixed point elements, the initialisation
  744. * is performed by zeroing out the number of bytes in the
  745. * structure divided by two.
  746. *
  747. *************************************************************************/
  748. int vad2_reset (vadState2 * st)
  749. {
  750. Word16 i;
  751. Word16 *ptr;
  752. if (st == (vadState2 *) NULL){
  753. wfprintf(stderr, "vad2_reset: invalid parameter\n");
  754. return -1;
  755. }
  756. ptr = (Word16 *)st; move16();
  757. for (i = 0; i < sizeof(vadState2)/2; i++)
  758. {
  759. *ptr++ = 0; move16();
  760. }
  761. return 0;
  762. } /* end of vad2_reset () */
  763. /*************************************************************************
  764. *
  765. * Function: vad2_exit
  766. * Purpose: The memory used for state memory is freed
  767. *
  768. **************************************************************************
  769. */
  770. void vad2_exit (vadState2 **state)
  771. {
  772. if (state == NULL || *state == NULL)
  773. return;
  774. /* deallocate memory */
  775. wfree(*state);
  776. *state = NULL;
  777. return;
  778. }