dtx_dec.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899
  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 : dtx_dec.c
  11. * Purpose : Decode comfort noise when in DTX
  12. *
  13. *****************************************************************************
  14. */
  15. /*
  16. *****************************************************************************
  17. * MODULE INCLUDE FILE AND VERSION ID
  18. *****************************************************************************
  19. */
  20. #include "dtx_dec.h"
  21. const char dtx_dec_id[] = "@(#)$Id $" dtx_dec_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 "copy.h"
  33. #include "set_zero.h"
  34. #include "mode.h"
  35. #include "log2.h"
  36. #include "lsp_az.h"
  37. #include "pow2.h"
  38. #include "a_refl.h"
  39. #include "b_cn_cod.h"
  40. #include "syn_filt.h"
  41. #include "lsp_lsf.h"
  42. #include "reorder.h"
  43. #include "count.h"
  44. #include "q_plsf_5.tab"
  45. #include "lsp.tab"
  46. /*
  47. *****************************************************************************
  48. * LOCAL VARIABLES AND TABLES
  49. *****************************************************************************
  50. */
  51. #define PN_INITIAL_SEED 0x70816958L /* Pseudo noise generator seed value */
  52. /***************************************************
  53. * Scaling factors for the lsp variability operation *
  54. ***************************************************/
  55. static const Word16 lsf_hist_mean_scale[M] = {
  56. 20000,
  57. 20000,
  58. 20000,
  59. 20000,
  60. 20000,
  61. 18000,
  62. 16384,
  63. 8192,
  64. 0,
  65. 0
  66. };
  67. /*************************************************
  68. * level adjustment for different modes Q11 *
  69. *************************************************/
  70. static const Word16 dtx_log_en_adjust[9] =
  71. {
  72. -1023, /* MR475 */
  73. -878, /* MR515 */
  74. -732, /* MR59 */
  75. -586, /* MR67 */
  76. -440, /* MR74 */
  77. -294, /* MR795 */
  78. -148, /* MR102 */
  79. 0, /* MR122 */
  80. 0, /* MRDTX */
  81. };
  82. /*
  83. *****************************************************************************
  84. * PUBLIC PROGRAM CODE
  85. *****************************************************************************
  86. */
  87. /*
  88. **************************************************************************
  89. *
  90. * Function : dtx_dec_init
  91. *
  92. **************************************************************************
  93. */
  94. int dtx_dec_init (dtx_decState **st)
  95. {
  96. dtx_decState* s;
  97. if (st == (dtx_decState **) NULL){
  98. wfprintf(stderr, "dtx_dec_init: invalid parameter\n");
  99. return -1;
  100. }
  101. *st = NULL;
  102. /* allocate memory */
  103. if ((s= (dtx_decState *) wmalloc(sizeof(dtx_decState))) == NULL){
  104. wfprintf(stderr, "dtx_dec_init: can not malloc state structure\n");
  105. return -1;
  106. }
  107. dtx_dec_reset(s);
  108. *st = s;
  109. return 0;
  110. }
  111. /*
  112. **************************************************************************
  113. *
  114. * Function : dtx_dec_reset
  115. *
  116. **************************************************************************
  117. */
  118. int dtx_dec_reset (dtx_decState *st)
  119. {
  120. int i;
  121. if (st == (dtx_decState *) NULL){
  122. wfprintf(stderr, "dtx_dec_reset: invalid parameter\n");
  123. return -1;
  124. }
  125. st->since_last_sid = 0;
  126. st->true_sid_period_inv = (1 << 13);
  127. st->log_en = 3500;
  128. st->old_log_en = 3500;
  129. /* low level noise for better performance in DTX handover cases*/
  130. st->L_pn_seed_rx = PN_INITIAL_SEED;
  131. /* Initialize state->lsp [] and state->lsp_old [] */
  132. Copy(lsp_init_data, &st->lsp[0], M);
  133. Copy(lsp_init_data, &st->lsp_old[0], M);
  134. st->lsf_hist_ptr = 0;
  135. st->log_pg_mean = 0;
  136. st->log_en_hist_ptr = 0;
  137. /* initialize decoder lsf history */
  138. Copy(mean_lsf, &st->lsf_hist[0], M);
  139. for (i = 1; i < DTX_HIST_SIZE; i++)
  140. {
  141. Copy(&st->lsf_hist[0], &st->lsf_hist[M*i], M);
  142. }
  143. Set_zero(st->lsf_hist_mean, M*DTX_HIST_SIZE);
  144. /* initialize decoder log frame energy */
  145. for (i = 0; i < DTX_HIST_SIZE; i++)
  146. {
  147. st->log_en_hist[i] = st->log_en;
  148. }
  149. st->log_en_adjust = 0;
  150. st->dtxHangoverCount = DTX_HANG_CONST;
  151. st->decAnaElapsedCount = 32767;
  152. st->sid_frame = 0;
  153. st->valid_data = 0;
  154. st->dtxHangoverAdded = 0;
  155. st->dtxGlobalState = DTX;
  156. st->data_updated = 0;
  157. return 0;
  158. }
  159. /*
  160. **************************************************************************
  161. *
  162. * Function : dtx_dec_exit
  163. *
  164. **************************************************************************
  165. */
  166. void dtx_dec_exit (dtx_decState **st)
  167. {
  168. if (st == NULL || *st == NULL)
  169. return;
  170. /* deallocate memory */
  171. wfree(*st);
  172. *st = NULL;
  173. return;
  174. }
  175. /*
  176. **************************************************************************
  177. *
  178. * Function : dtx_dec
  179. *
  180. **************************************************************************
  181. */
  182. int dtx_dec(
  183. dtx_decState *st, /* i/o : State struct */
  184. Word16 mem_syn[], /* i/o : AMR decoder state */
  185. D_plsfState* lsfState, /* i/o : decoder lsf states */
  186. gc_predState* predState, /* i/o : prediction states */
  187. Cb_gain_averageState* averState, /* i/o : CB gain average states */
  188. enum DTXStateType new_state, /* i : new DTX state */
  189. enum Mode mode, /* i : AMR mode */
  190. Word16 parm[], /* i : Vector of synthesis parameters */
  191. Word16 synth[], /* o : synthesised speech */
  192. Word16 A_t[] /* o : decoded LP filter in 4 subframes*/
  193. )
  194. {
  195. Word16 log_en_index;
  196. Word16 i, j;
  197. Word16 int_fac;
  198. Word32 L_log_en_int;
  199. Word16 lsp_int[M];
  200. Word16 log_en_int_e;
  201. Word16 log_en_int_m;
  202. Word16 level;
  203. Word16 acoeff[M + 1];
  204. Word16 refl[M];
  205. Word16 pred_err;
  206. Word16 ex[L_SUBFR];
  207. Word16 ma_pred_init;
  208. Word16 log_pg_e, log_pg_m;
  209. Word16 log_pg;
  210. Flag negative;
  211. Word16 lsf_mean;
  212. Word32 L_lsf_mean;
  213. Word16 lsf_variab_index;
  214. Word16 lsf_variab_factor;
  215. Word16 lsf_int[M];
  216. Word16 lsf_int_variab[M];
  217. Word16 lsp_int_variab[M];
  218. Word16 acoeff_variab[M + 1];
  219. Word16 lsf[M];
  220. Word32 L_lsf[M];
  221. Word16 ptr;
  222. Word16 tmp_int_length;
  223. /* This function is called if synthesis state is not SPEECH
  224. * the globally passed inputs to this function are
  225. * st->sid_frame
  226. * st->valid_data
  227. * st->dtxHangoverAdded
  228. * new_state (SPEECH, DTX, DTX_MUTE)
  229. */
  230. test(); test();
  231. if ((st->dtxHangoverAdded != 0) &&
  232. (st->sid_frame != 0))
  233. {
  234. /* sid_first after dtx hangover period */
  235. /* or sid_upd after dtxhangover */
  236. /* set log_en_adjust to correct value */
  237. st->log_en_adjust = dtx_log_en_adjust[mode];
  238. ptr = add_ex(st->lsf_hist_ptr, M); move16();
  239. test();
  240. if (sub_ex(ptr, 80) == 0)
  241. {
  242. ptr = 0; move16();
  243. }
  244. Copy( &st->lsf_hist[st->lsf_hist_ptr],&st->lsf_hist[ptr],M);
  245. ptr = add_ex(st->log_en_hist_ptr,1); move16();
  246. test();
  247. if (sub_ex(ptr, DTX_HIST_SIZE) == 0)
  248. {
  249. ptr = 0; move16();
  250. }
  251. move16();
  252. st->log_en_hist[ptr] = st->log_en_hist[st->log_en_hist_ptr]; /* Q11 */
  253. /* compute mean log energy and lsp *
  254. * from decoded signal (SID_FIRST) */
  255. st->log_en = 0; move16();
  256. for (i = 0; i < M; i++)
  257. {
  258. L_lsf[i] = 0; move16();
  259. }
  260. /* average energy and lsp */
  261. for (i = 0; i < DTX_HIST_SIZE; i++)
  262. {
  263. st->log_en = add_ex(st->log_en,
  264. shr_ex(st->log_en_hist[i],3));
  265. for (j = 0; j < M; j++)
  266. {
  267. L_lsf[j] = L_add_ex(L_lsf[j],
  268. L_deposit_l_ex(st->lsf_hist[i * M + j]));
  269. }
  270. }
  271. for (j = 0; j < M; j++)
  272. {
  273. lsf[j] = extract_l_ex(L_shr_ex(L_lsf[j],3)); /* divide by 8 */ move16();
  274. }
  275. Lsf_lsp(lsf, st->lsp, M);
  276. /* make log_en speech coder mode independent */
  277. /* added again later before synthesis */
  278. st->log_en = sub_ex(st->log_en, st->log_en_adjust);
  279. /* compute lsf variability vector */
  280. Copy(st->lsf_hist, st->lsf_hist_mean, 80);
  281. for (i = 0; i < M; i++)
  282. {
  283. L_lsf_mean = 0; move32();
  284. /* compute mean lsf */
  285. for (j = 0; j < 8; j++)
  286. {
  287. L_lsf_mean = L_add_ex(L_lsf_mean,
  288. L_deposit_l_ex(st->lsf_hist_mean[i+j*M]));
  289. }
  290. lsf_mean = extract_l_ex(L_shr_ex(L_lsf_mean, 3)); move16();
  291. /* subtract mean and limit to within reasonable limits *
  292. * moreover the upper lsf's are attenuated */
  293. for (j = 0; j < 8; j++)
  294. {
  295. /* subtract mean */
  296. st->lsf_hist_mean[i+j*M] =
  297. sub_ex(st->lsf_hist_mean[i+j*M], lsf_mean);
  298. /* attenuate deviation from mean, especially for upper lsf's */
  299. st->lsf_hist_mean[i+j*M] =
  300. mult_ex(st->lsf_hist_mean[i+j*M], lsf_hist_mean_scale[i]);
  301. /* limit the deviation */
  302. test();
  303. if (st->lsf_hist_mean[i+j*M] < 0)
  304. {
  305. negative = 1; move16();
  306. }
  307. else
  308. {
  309. negative = 0; move16();
  310. }
  311. st->lsf_hist_mean[i+j*M] = abs_s_ex(st->lsf_hist_mean[i+j*M]);
  312. /* apply soft limit */
  313. test();
  314. if (sub_ex(st->lsf_hist_mean[i+j*M], 655) > 0)
  315. {
  316. st->lsf_hist_mean[i+j*M] =
  317. add_ex(655, shr_ex(sub_ex(st->lsf_hist_mean[i+j*M], 655), 2));
  318. }
  319. /* apply hard limit */
  320. test();
  321. if (sub_ex(st->lsf_hist_mean[i+j*M], 1310) > 0)
  322. {
  323. st->lsf_hist_mean[i+j*M] = 1310; move16();
  324. }
  325. test();
  326. if (negative != 0)
  327. {
  328. st->lsf_hist_mean[i+j*M] = -st->lsf_hist_mean[i+j*M];move16();
  329. }
  330. }
  331. }
  332. }
  333. test();
  334. if (st->sid_frame != 0 )
  335. {
  336. /* Set old SID parameters, always shift */
  337. /* even if there is no new valid_data */
  338. Copy(st->lsp, st->lsp_old, M);
  339. st->old_log_en = st->log_en; move16();
  340. test();
  341. if (st->valid_data != 0 ) /* new data available (no CRC) */
  342. {
  343. /* Compute interpolation factor, since the division only works *
  344. * for values of since_last_sid < 32 we have to limit the *
  345. * interpolation to 32 frames */
  346. tmp_int_length = st->since_last_sid; move16();
  347. st->since_last_sid = 0; move16();
  348. test();
  349. if (sub_ex(tmp_int_length, 32) > 0)
  350. {
  351. tmp_int_length = 32; move16();
  352. }
  353. test();
  354. if (sub_ex(tmp_int_length, 2) >= 0)
  355. {
  356. move16();
  357. st->true_sid_period_inv = div_s(1 << 10, shl_ex(tmp_int_length, 10));
  358. }
  359. else
  360. {
  361. st->true_sid_period_inv = 1 << 14; /* 0.5 it Q15 */ move16();
  362. }
  363. Init_D_plsf_3(lsfState, parm[0]); /* temporay initialization */
  364. D_plsf_3(lsfState, MRDTX, 0, &parm[1], st->lsp);
  365. Set_zero(lsfState->past_r_q, M); /* reset for next speech frame */
  366. log_en_index = parm[4]; move16();
  367. /* Q11 and divide by 4 */
  368. st->log_en = shl_ex(log_en_index, (11 - 2)); move16();
  369. /* Subtract 2.5 in Q11 */
  370. st->log_en = sub_ex(st->log_en, (2560 * 2));
  371. /* Index 0 is reserved for silence */
  372. test();
  373. if (log_en_index == 0)
  374. {
  375. st->log_en = MIN_16; move16();
  376. }
  377. /* no interpolation at startup after coder reset */
  378. /* or when SID_UPD has been received right after SPEECH */
  379. test(); test();
  380. if ((st->data_updated == 0) ||
  381. (sub_ex(st->dtxGlobalState, SPEECH) == 0)
  382. )
  383. {
  384. Copy(st->lsp, st->lsp_old, M);
  385. st->old_log_en = st->log_en; move16();
  386. }
  387. } /* endif valid_data */
  388. /* initialize gain predictor memory of other modes */
  389. ma_pred_init = sub_ex(shr_ex(st->log_en,1), 9000); move16();
  390. test();
  391. if (ma_pred_init > 0)
  392. {
  393. ma_pred_init = 0; move16();
  394. }
  395. test();
  396. if (sub_ex(ma_pred_init, -14436) < 0)
  397. {
  398. ma_pred_init = -14436; move16();
  399. }
  400. predState->past_qua_en[0] = ma_pred_init; move16();
  401. predState->past_qua_en[1] = ma_pred_init; move16();
  402. predState->past_qua_en[2] = ma_pred_init; move16();
  403. predState->past_qua_en[3] = ma_pred_init; move16();
  404. /* past_qua_en for other modes than MR122 */
  405. ma_pred_init = mult_ex(5443, ma_pred_init);
  406. /* scale down by factor 20*log10(2) in Q15 */
  407. predState->past_qua_en_MR122[0] = ma_pred_init; move16();
  408. predState->past_qua_en_MR122[1] = ma_pred_init; move16();
  409. predState->past_qua_en_MR122[2] = ma_pred_init; move16();
  410. predState->past_qua_en_MR122[3] = ma_pred_init; move16();
  411. } /* endif sid_frame */
  412. /* CN generation */
  413. /* recompute level adjustment factor Q11 *
  414. * st->log_en_adjust = 0.9*st->log_en_adjust + *
  415. * 0.1*dtx_log_en_adjust[mode]); */
  416. move16();
  417. st->log_en_adjust = add_ex(mult_ex(st->log_en_adjust, 29491),
  418. shr_ex(mult_ex(shl_ex(dtx_log_en_adjust[mode],5),3277),5));
  419. /* Interpolate SID info */
  420. int_fac = shl_ex(add_ex(1,st->since_last_sid), 10); /* Q10 */ move16();
  421. int_fac = mult_ex(int_fac, st->true_sid_period_inv); /* Q10 * Q15 -> Q10 */
  422. /* Maximize to 1.0 in Q10 */
  423. test();
  424. if (sub_ex(int_fac, 1024) > 0)
  425. {
  426. int_fac = 1024; move16();
  427. }
  428. int_fac = shl_ex(int_fac, 4); /* Q10 -> Q14 */
  429. L_log_en_int = L_mult_ex(int_fac, st->log_en); /* Q14 * Q11->Q26 */ move32();
  430. for(i = 0; i < M; i++)
  431. {
  432. lsp_int[i] = mult_ex(int_fac, st->lsp[i]);/* Q14 * Q15 -> Q14 */ move16();
  433. }
  434. int_fac = sub_ex(16384, int_fac); /* 1-k in Q14 */ move16();
  435. /* (Q14 * Q11 -> Q26) + Q26 -> Q26 */
  436. L_log_en_int = L_mac_ex(L_log_en_int, int_fac, st->old_log_en);
  437. for(i = 0; i < M; i++)
  438. {
  439. /* Q14 + (Q14 * Q15 -> Q14) -> Q14 */
  440. lsp_int[i] = add_ex(lsp_int[i], mult_ex(int_fac, st->lsp_old[i])); move16();
  441. lsp_int[i] = shl_ex(lsp_int[i], 1); /* Q14 -> Q15 */ move16();
  442. }
  443. /* compute the amount of lsf variability */
  444. lsf_variab_factor = sub_ex(st->log_pg_mean,2457); /* -0.6 in Q12 */ move16();
  445. /* *0.3 Q12*Q15 -> Q12 */
  446. lsf_variab_factor = sub_ex(4096, mult_ex(lsf_variab_factor, 9830));
  447. /* limit to values between 0..1 in Q12 */
  448. test();
  449. if (sub_ex(lsf_variab_factor, 4096) > 0)
  450. {
  451. lsf_variab_factor = 4096; move16();
  452. }
  453. test();
  454. if (lsf_variab_factor < 0)
  455. {
  456. lsf_variab_factor = 0; move16();
  457. }
  458. lsf_variab_factor = shl_ex(lsf_variab_factor, 3); /* -> Q15 */ move16();
  459. /* get index of vector to do variability with */
  460. lsf_variab_index = pseudonoise(&st->L_pn_seed_rx, 3); move16();
  461. /* convert to lsf */
  462. Lsp_lsf(lsp_int, lsf_int, M);
  463. /* apply lsf variability */
  464. Copy(lsf_int, lsf_int_variab, M);
  465. for(i = 0; i < M; i++)
  466. {
  467. move16();
  468. lsf_int_variab[i] = add_ex(lsf_int_variab[i],
  469. mult_ex(lsf_variab_factor,
  470. st->lsf_hist_mean[i+lsf_variab_index*M]));
  471. }
  472. /* make sure that LSP's are ordered */
  473. Reorder_lsf(lsf_int, LSF_GAP, M);
  474. Reorder_lsf(lsf_int_variab, LSF_GAP, M);
  475. /* copy lsf to speech decoders lsf state */
  476. Copy(lsf_int, lsfState->past_lsf_q, M);
  477. /* convert to lsp */
  478. Lsf_lsp(lsf_int, lsp_int, M);
  479. Lsf_lsp(lsf_int_variab, lsp_int_variab, M);
  480. /* Compute acoeffs Q12 acoeff is used for level *
  481. * normalization and postfilter, acoeff_variab is *
  482. * used for synthesis filter *
  483. * by doing this we make sure that the level *
  484. * in high frequenncies does not jump up and down */
  485. Lsp_Az(lsp_int, acoeff);
  486. Lsp_Az(lsp_int_variab, acoeff_variab);
  487. /* For use in postfilter */
  488. Copy(acoeff, &A_t[0], M + 1);
  489. Copy(acoeff, &A_t[M + 1], M + 1);
  490. Copy(acoeff, &A_t[2 * (M + 1)], M + 1);
  491. Copy(acoeff, &A_t[3 * (M + 1)], M + 1);
  492. /* Compute reflection coefficients Q15 */
  493. A_Refl(&acoeff[1], refl);
  494. /* Compute prediction error in Q15 */
  495. pred_err = MAX_16; /* 0.99997 in Q15 */ move16();
  496. for (i = 0; i < M; i++)
  497. {
  498. pred_err = mult_ex(pred_err, sub_ex(MAX_16, mult_ex(refl[i], refl[i])));
  499. }
  500. /* compute logarithm of prediction gain */
  501. Log2(L_deposit_l_ex(pred_err), &log_pg_e, &log_pg_m);
  502. /* convert exponent and mantissa to Word16 Q12 */
  503. log_pg = shl_ex(sub_ex(log_pg_e,15), 12); /* Q12 */ move16();
  504. log_pg = shr_ex(sub_ex(0,add_ex(log_pg, shr_ex(log_pg_m, 15-12))), 1); move16();
  505. st->log_pg_mean = add_ex(mult_ex(29491,st->log_pg_mean),
  506. mult_ex(3277, log_pg)); move16();
  507. /* Compute interpolated log energy */
  508. L_log_en_int = L_shr_ex(L_log_en_int, 10); /* Q26 -> Q16 */ move32();
  509. /* Add 4 in Q16 */
  510. L_log_en_int = L_add_ex(L_log_en_int, 4 * 65536L); move32();
  511. /* subtract prediction gain */
  512. L_log_en_int = L_sub_ex(L_log_en_int, L_shl_ex(L_deposit_l_ex(log_pg), 4));move32();
  513. /* adjust level to speech coder mode */
  514. L_log_en_int = L_add_ex(L_log_en_int,
  515. L_shl_ex(L_deposit_l_ex(st->log_en_adjust), 5)); move32();
  516. log_en_int_e = extract_h_ex(L_log_en_int); move16();
  517. move16();
  518. log_en_int_m = extract_l_ex(L_shr_ex(L_sub_ex(L_log_en_int,
  519. L_deposit_h_ex(log_en_int_e)), 1));
  520. level = extract_l_ex(Pow2(log_en_int_e, log_en_int_m)); /* Q4 */ move16();
  521. for (i = 0; i < 4; i++)
  522. {
  523. /* Compute innovation vector */
  524. build_CN_code(&st->L_pn_seed_rx, ex);
  525. for (j = 0; j < L_SUBFR; j++)
  526. {
  527. ex[j] = mult_ex(level, ex[j]); move16();
  528. }
  529. /* Synthesize */
  530. Syn_filt(acoeff_variab, ex, &synth[i * L_SUBFR], L_SUBFR,
  531. mem_syn, 1);
  532. } /* next i */
  533. /* reset codebook averaging variables */
  534. averState->hangVar = 20; move16();
  535. averState->hangCount = 0; move16();
  536. test();
  537. if (sub_ex(new_state, DTX_MUTE) == 0)
  538. {
  539. /* mute comfort noise as it has been quite a long time since
  540. * last SID update was performed */
  541. tmp_int_length = st->since_last_sid; move16();
  542. test();
  543. if (sub_ex(tmp_int_length, 32) > 0)
  544. {
  545. tmp_int_length = 32; move16();
  546. }
  547. /* safety guard against division by zero */
  548. test();
  549. if(tmp_int_length <= 0) {
  550. tmp_int_length = 8; move16();
  551. }
  552. move16();
  553. st->true_sid_period_inv = div_s(1 << 10, shl_ex(tmp_int_length, 10));
  554. st->since_last_sid = 0; move16();
  555. Copy(st->lsp, st->lsp_old, M);
  556. st->old_log_en = st->log_en; move16();
  557. /* subtract 1/8 in Q11 i.e -6/8 dB */
  558. st->log_en = sub_ex(st->log_en, 256); move16();
  559. }
  560. /* reset interpolation length timer
  561. * if data has been updated. */
  562. test(); test(); test(); test();
  563. if ((st->sid_frame != 0) &&
  564. ((st->valid_data != 0) ||
  565. ((st->valid_data == 0) && (st->dtxHangoverAdded) != 0)))
  566. {
  567. st->since_last_sid = 0; move16();
  568. st->data_updated = 1; move16();
  569. }
  570. return 0;
  571. }
  572. void dtx_dec_activity_update(dtx_decState *st,
  573. Word16 lsf[],
  574. Word16 frame[])
  575. {
  576. Word16 i;
  577. Word32 L_frame_en;
  578. Word16 log_en_e, log_en_m, log_en;
  579. /* update lsp history */
  580. st->lsf_hist_ptr = add_ex(st->lsf_hist_ptr,M); move16();
  581. test();
  582. if (sub_ex(st->lsf_hist_ptr, 80) == 0)
  583. {
  584. st->lsf_hist_ptr = 0; move16();
  585. }
  586. Copy(lsf, &st->lsf_hist[st->lsf_hist_ptr], M);
  587. /* compute log energy based on frame energy */
  588. L_frame_en = 0; /* Q0 */ move32();
  589. for (i=0; i < L_FRAME; i++)
  590. {
  591. L_frame_en = L_mac_ex(L_frame_en, frame[i], frame[i]);
  592. }
  593. Log2(L_frame_en, &log_en_e, &log_en_m);
  594. /* convert exponent and mantissa to Word16 Q10 */
  595. log_en = shl_ex(log_en_e, 10); /* Q10 */
  596. log_en = add_ex(log_en, shr_ex(log_en_m, 15-10));
  597. /* divide with L_FRAME i.e subtract with log2(L_FRAME) = 7.32193 */
  598. log_en = sub_ex(log_en, 7497+1024);
  599. /* insert into log energy buffer, no division by two as *
  600. * log_en in decoder is Q11 */
  601. st->log_en_hist_ptr = add_ex(st->log_en_hist_ptr, 1);
  602. test();
  603. if (sub_ex(st->log_en_hist_ptr, DTX_HIST_SIZE) == 0)
  604. {
  605. st->log_en_hist_ptr = 0; move16();
  606. }
  607. st->log_en_hist[st->log_en_hist_ptr] = log_en; /* Q11 */ move16();
  608. }
  609. /*
  610. Table of new SPD synthesis states
  611. | previous SPD_synthesis_state
  612. Incoming |
  613. frame_type | SPEECH | DTX | DTX_MUTE
  614. ---------------------------------------------------------------
  615. RX_SPEECH_GOOD , | | |
  616. RX_SPEECH_PR_DEGRADED | SPEECH | SPEECH | SPEECH
  617. ----------------------------------------------------------------
  618. RX_SPEECH_BAD, | SPEECH | DTX | DTX_MUTE
  619. ----------------------------------------------------------------
  620. RX_SID_FIRST, | DTX | DTX/(DTX_MUTE)| DTX_MUTE
  621. ----------------------------------------------------------------
  622. RX_SID_UPDATE, | DTX | DTX | DTX
  623. ----------------------------------------------------------------
  624. RX_SID_BAD, | DTX | DTX/(DTX_MUTE)| DTX_MUTE
  625. ----------------------------------------------------------------
  626. RX_NO_DATA | SPEECH | DTX/(DTX_MUTE)| DTX_MUTE
  627. |(class2 garb.)| |
  628. ----------------------------------------------------------------
  629. RX_ONSET | SPEECH | DTX/(DTX_MUTE)| DTX_MUTE
  630. |(class2 garb.)| |
  631. ----------------------------------------------------------------
  632. */
  633. enum DTXStateType rx_dtx_handler(
  634. dtx_decState *st, /* i/o : State struct */
  635. enum RXFrameType frame_type /* i : Frame type */
  636. )
  637. {
  638. enum DTXStateType newState;
  639. enum DTXStateType encState;
  640. /* DTX if SID frame or previously in DTX{_MUTE} and (NO_RX OR BAD_SPEECH) */
  641. test(); test(); test();
  642. test(); test(); test();
  643. test(); test();
  644. if ((sub_ex(frame_type, RX_SID_FIRST) == 0) ||
  645. (sub_ex(frame_type, RX_SID_UPDATE) == 0) ||
  646. (sub_ex(frame_type, RX_SID_BAD) == 0) ||
  647. (((sub_ex(st->dtxGlobalState, DTX) == 0) ||
  648. (sub_ex(st->dtxGlobalState, DTX_MUTE) == 0)) &&
  649. ((sub_ex(frame_type, RX_NO_DATA) == 0) ||
  650. (sub_ex(frame_type, RX_SPEECH_BAD) == 0) ||
  651. (sub_ex(frame_type, RX_ONSET) == 0))))
  652. {
  653. newState = DTX; move16();
  654. /* stay in mute for these input types */
  655. test(); test(); test(); test(); test();
  656. if ((sub_ex(st->dtxGlobalState, DTX_MUTE) == 0) &&
  657. ((sub_ex(frame_type, RX_SID_BAD) == 0) ||
  658. (sub_ex(frame_type, RX_SID_FIRST) == 0) ||
  659. (sub_ex(frame_type, RX_ONSET) == 0) ||
  660. (sub_ex(frame_type, RX_NO_DATA) == 0)))
  661. {
  662. newState = DTX_MUTE; move16();
  663. }
  664. /* evaluate if noise parameters are too old */
  665. /* since_last_sid is reset when CN parameters have been updated */
  666. st->since_last_sid = add_ex(st->since_last_sid, 1); move16();
  667. /* no update of sid parameters in DTX for a long while */
  668. /* Due to the delayed update of st->since_last_sid counter
  669. SID_UPDATE frames need to be handled separately to avoid
  670. entering DTX_MUTE for late SID_UPDATE frames
  671. */
  672. test(); test(); logic16();
  673. if((sub_ex(frame_type, RX_SID_UPDATE) != 0) &&
  674. (sub_ex(st->since_last_sid, DTX_MAX_EMPTY_THRESH) > 0))
  675. {
  676. newState = DTX_MUTE; move16();
  677. }
  678. }
  679. else
  680. {
  681. newState = SPEECH; move16();
  682. st->since_last_sid = 0; move16();
  683. }
  684. /*
  685. reset the decAnaElapsed Counter when receiving CNI data the first
  686. time, to robustify counter missmatch after handover
  687. this might delay the bwd CNI analysis in the new decoder slightly.
  688. */
  689. test(); test();
  690. if ((st->data_updated == 0) &&
  691. (sub_ex(frame_type, RX_SID_UPDATE) == 0))
  692. {
  693. st->decAnaElapsedCount = 0; move16();
  694. }
  695. /* update the SPE-SPD DTX hangover synchronization */
  696. /* to know when SPE has added dtx hangover */
  697. st->decAnaElapsedCount = add_ex(st->decAnaElapsedCount, 1); move16();
  698. st->dtxHangoverAdded = 0; move16();
  699. test(); test(); test(); test(); test();
  700. if ((sub_ex(frame_type, RX_SID_FIRST) == 0) ||
  701. (sub_ex(frame_type, RX_SID_UPDATE) == 0) ||
  702. (sub_ex(frame_type, RX_SID_BAD) == 0) ||
  703. (sub_ex(frame_type, RX_ONSET) == 0) ||
  704. (sub_ex(frame_type, RX_NO_DATA) == 0))
  705. {
  706. encState = DTX; move16();
  707. /*
  708. In frame errors simulations RX_NO_DATA may occasionally mean that
  709. a speech packet was probably sent by the encoder,
  710. the assumed _encoder_ state should be SPEECH in such cases.
  711. */
  712. test(); logic16();
  713. if((sub_ex(frame_type, RX_NO_DATA) == 0) &&
  714. (sub_ex(newState, SPEECH) == 0))
  715. {
  716. encState = SPEECH; move16();
  717. }
  718. /* Note on RX_ONSET operation differing from RX_NO_DATA operation:
  719. If a RX_ONSET is received in the decoder (by "accident")
  720. it is still most likely that the encoder state
  721. for the "ONSET frame" was DTX.
  722. */
  723. }
  724. else
  725. {
  726. encState = SPEECH; move16();
  727. }
  728. test();
  729. if (sub_ex(encState, SPEECH) == 0)
  730. {
  731. st->dtxHangoverCount = DTX_HANG_CONST; move16();
  732. }
  733. else
  734. {
  735. test();
  736. if (sub_ex(st->decAnaElapsedCount, DTX_ELAPSED_FRAMES_THRESH) > 0)
  737. {
  738. st->dtxHangoverAdded = 1; move16();
  739. st->decAnaElapsedCount = 0; move16();
  740. st->dtxHangoverCount = 0; move16();
  741. }
  742. else if (test(), st->dtxHangoverCount == 0)
  743. {
  744. st->decAnaElapsedCount = 0; move16();
  745. }
  746. else
  747. {
  748. st->dtxHangoverCount = sub_ex(st->dtxHangoverCount, 1); move16();
  749. }
  750. }
  751. if (sub_ex(newState, SPEECH) != 0)
  752. {
  753. /* DTX or DTX_MUTE
  754. * CN data is not in a first SID, first SIDs are marked as SID_BAD
  755. * but will do backwards analysis if a hangover period has been added
  756. * according to the state machine above
  757. */
  758. st->sid_frame = 0; move16();
  759. st->valid_data = 0; move16();
  760. test();
  761. if (sub_ex(frame_type, RX_SID_FIRST) == 0)
  762. {
  763. st->sid_frame = 1; move16();
  764. }
  765. else if (test(), sub_ex(frame_type, RX_SID_UPDATE) == 0)
  766. {
  767. st->sid_frame = 1; move16();
  768. st->valid_data = 1; move16();
  769. }
  770. else if (test(), sub_ex(frame_type, RX_SID_BAD) == 0)
  771. {
  772. st->sid_frame = 1; move16();
  773. st->dtxHangoverAdded = 0; /* use old data */ move16();
  774. }
  775. }
  776. return newState;
  777. /* newState is used by both SPEECH AND DTX synthesis routines */
  778. }