dtx_enc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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_enc.c
  11. * Purpose : DTX mode computation of SID parameters
  12. *
  13. ********************************************************************************
  14. */
  15. /*
  16. ********************************************************************************
  17. * MODULE INCLUDE FILE AND VERSION ID
  18. ********************************************************************************
  19. */
  20. #include "dtx_enc.h"
  21. const char dtx_enc_id[] = "@(#)$Id $" dtx_enc_h;
  22. /*
  23. ********************************************************************************
  24. * INCLUDE FILES
  25. ********************************************************************************
  26. */
  27. #include <stdlib.h>
  28. #include <stdio.h>
  29. #include "q_plsf.h"
  30. #include "typedef.h"
  31. #include "basic_op.h"
  32. #include "oper_32b.h"
  33. #include "copy.h"
  34. #include "set_zero.h"
  35. #include "mode.h"
  36. #include "log2.h"
  37. #include "lsp_lsf.h"
  38. #include "reorder.h"
  39. #include "count.h"
  40. /*
  41. ********************************************************************************
  42. * LOCAL VARIABLES AND TABLES
  43. ********************************************************************************
  44. */
  45. #include "lsp.tab"
  46. /*
  47. ********************************************************************************
  48. * PUBLIC PROGRAM CODE
  49. ********************************************************************************
  50. */
  51. /*
  52. **************************************************************************
  53. *
  54. * Function : dtx_enc_init
  55. *
  56. **************************************************************************
  57. */
  58. int dtx_enc_init (dtx_encState **st)
  59. {
  60. dtx_encState* s;
  61. if (st == (dtx_encState **) NULL){
  62. wfprintf(stderr, "dtx_enc_init: invalid parameter\n");
  63. return -1;
  64. }
  65. *st = NULL;
  66. /* allocate memory */
  67. if ((s= (dtx_encState *) wmalloc(sizeof(dtx_encState))) == NULL){
  68. wfprintf(stderr, "dtx_enc_init: can not malloc state structure\n");
  69. return -1;
  70. }
  71. dtx_enc_reset(s);
  72. *st = s;
  73. return 0;
  74. }
  75. /*
  76. **************************************************************************
  77. *
  78. * Function : dtx_enc_reset
  79. *
  80. **************************************************************************
  81. */
  82. int dtx_enc_reset (dtx_encState *st)
  83. {
  84. Word16 i;
  85. if (st == (dtx_encState *) NULL){
  86. wfprintf(stderr, "dtx_enc_reset: invalid parameter\n");
  87. return -1;
  88. }
  89. st->hist_ptr = 0;
  90. st->log_en_index = 0;
  91. st->init_lsf_vq_index = 0;
  92. st->lsp_index[0] = 0;
  93. st->lsp_index[1] = 0;
  94. st->lsp_index[2] = 0;
  95. /* Init lsp_hist[] */
  96. for(i = 0; i < DTX_HIST_SIZE; i++)
  97. {
  98. Copy(lsp_init_data, &st->lsp_hist[i * M], M);
  99. }
  100. /* Reset energy history */
  101. Set_zero(st->log_en_hist, M);
  102. st->dtxHangoverCount = DTX_HANG_CONST;
  103. st->decAnaElapsedCount = 32767;
  104. return 1;
  105. }
  106. /*
  107. **************************************************************************
  108. *
  109. * Function : dtx_enc_exit
  110. *
  111. **************************************************************************
  112. */
  113. void dtx_enc_exit (dtx_encState **st)
  114. {
  115. if (st == NULL || *st == NULL)
  116. return;
  117. /* deallocate memory */
  118. wfree(*st);
  119. *st = NULL;
  120. return;
  121. }
  122. /*
  123. **************************************************************************
  124. *
  125. * Function : dtx_enc
  126. *
  127. **************************************************************************
  128. */
  129. int dtx_enc(dtx_encState *st, /* i/o : State struct */
  130. Word16 computeSidFlag, /* i : compute SID */
  131. Q_plsfState *qSt, /* i/o : Qunatizer state struct */
  132. gc_predState* predState, /* i/o : State struct */
  133. Word16 **anap /* o : analysis parameters */
  134. )
  135. {
  136. Word16 i,j;
  137. Word16 log_en;
  138. Word16 lsf[M];
  139. Word16 lsp[M];
  140. Word16 lsp_q[M];
  141. Word32 L_lsp[M];
  142. /* VOX mode computation of SID parameters */
  143. test (); test ();
  144. if ((computeSidFlag != 0))
  145. {
  146. /* compute new SID frame if safe i.e don't
  147. * compute immediately after a talk spurt */
  148. log_en = 0; move16 ();
  149. for (i = 0; i < M; i++)
  150. {
  151. L_lsp[i] = 0; move16 ();
  152. }
  153. /* average energy and lsp */
  154. for (i = 0; i < DTX_HIST_SIZE; i++)
  155. {
  156. log_en = add_ex(log_en,
  157. shr_ex(st->log_en_hist[i],2));
  158. for (j = 0; j < M; j++)
  159. {
  160. L_lsp[j] = L_add_ex(L_lsp[j],
  161. L_deposit_l_ex(st->lsp_hist[i * M + j]));
  162. }
  163. }
  164. log_en = shr_ex(log_en, 1);
  165. for (j = 0; j < M; j++)
  166. {
  167. lsp[j] = extract_l_ex(L_shr_ex(L_lsp[j], 3)); /* divide by 8 */
  168. }
  169. /* quantize logarithmic energy to 6 bits */
  170. st->log_en_index = add_ex(log_en, 2560); /* +2.5 in Q10 */
  171. st->log_en_index = add_ex(st->log_en_index, 128); /* add 0.5/4 in Q10 */
  172. st->log_en_index = shr_ex(st->log_en_index, 8);
  173. test ();
  174. if (sub_ex(st->log_en_index, 63) > 0)
  175. {
  176. st->log_en_index = 63; move16 ();
  177. }
  178. test ();
  179. if (st->log_en_index < 0)
  180. {
  181. st->log_en_index = 0; move16 ();
  182. }
  183. /* update gain predictor memory */
  184. log_en = shl_ex(st->log_en_index, -2+10); /* Q11 and divide by 4 */
  185. log_en = sub_ex(log_en, 2560); /* add 2.5 in Q11 */
  186. log_en = sub_ex(log_en, 9000);
  187. test ();
  188. if (log_en > 0)
  189. {
  190. log_en = 0; move16 ();
  191. }
  192. test ();
  193. if (sub_ex(log_en, -14436) < 0)
  194. {
  195. log_en = -14436; move16 ();
  196. }
  197. /* past_qua_en for other modes than MR122 */
  198. predState->past_qua_en[0] = log_en; move16 ();
  199. predState->past_qua_en[1] = log_en; move16 ();
  200. predState->past_qua_en[2] = log_en; move16 ();
  201. predState->past_qua_en[3] = log_en; move16 ();
  202. /* scale down by factor 20*log10(2) in Q15 */
  203. log_en = mult_ex(5443, log_en);
  204. /* past_qua_en for mode MR122 */
  205. predState->past_qua_en_MR122[0] = log_en; move16 ();
  206. predState->past_qua_en_MR122[1] = log_en; move16 ();
  207. predState->past_qua_en_MR122[2] = log_en; move16 ();
  208. predState->past_qua_en_MR122[3] = log_en; move16 ();
  209. /* make sure that LSP's are ordered */
  210. Lsp_lsf(lsp, lsf, M);
  211. Reorder_lsf(lsf, LSF_GAP, M);
  212. Lsf_lsp(lsf, lsp, M);
  213. /* Quantize lsp and put on parameter list */
  214. Q_plsf_3(qSt, MRDTX, lsp, lsp_q, st->lsp_index,
  215. &st->init_lsf_vq_index);
  216. }
  217. *(*anap)++ = st->init_lsf_vq_index; /* 3 bits */ move16 ();
  218. *(*anap)++ = st->lsp_index[0]; /* 8 bits */ move16 ();
  219. *(*anap)++ = st->lsp_index[1]; /* 9 bits */ move16 ();
  220. *(*anap)++ = st->lsp_index[2]; /* 9 bits */ move16 ();
  221. *(*anap)++ = st->log_en_index; /* 6 bits */ move16 ();
  222. /* = 35 bits */
  223. return 0;
  224. }
  225. /*
  226. **************************************************************************
  227. *
  228. * Function : dtx_buffer
  229. * Purpose : handles the DTX buffer
  230. *
  231. **************************************************************************
  232. */
  233. int dtx_buffer(dtx_encState *st, /* i/o : State struct */
  234. Word16 lsp_new[], /* i : LSP vector */
  235. Word16 speech[] /* i : speech samples */
  236. )
  237. {
  238. Word16 i;
  239. Word32 L_frame_en;
  240. Word16 log_en_e;
  241. Word16 log_en_m;
  242. Word16 log_en;
  243. /* update pointer to circular buffer */
  244. st->hist_ptr = add_ex(st->hist_ptr, 1);
  245. test ();
  246. if (sub_ex(st->hist_ptr, DTX_HIST_SIZE) == 0)
  247. {
  248. st->hist_ptr = 0; move16 ();
  249. }
  250. /* copy lsp vector into buffer */
  251. Copy(lsp_new, &st->lsp_hist[st->hist_ptr * M], M);
  252. /* compute log energy based on frame energy */
  253. L_frame_en = 0; /* Q0 */ move32 ();
  254. for (i=0; i < L_FRAME; i++)
  255. {
  256. L_frame_en = L_mac_ex(L_frame_en, speech[i], speech[i]);
  257. }
  258. Log2(L_frame_en, &log_en_e, &log_en_m);
  259. /* convert exponent and mantissa to Word16 Q10 */
  260. log_en = shl_ex(log_en_e, 10); /* Q10 */
  261. log_en = add_ex(log_en, shr_ex(log_en_m, 15-10));
  262. /* divide with L_FRAME i.e subtract with log2(L_FRAME) = 7.32193 */
  263. log_en = sub_ex(log_en, 8521);
  264. /* insert into log energy buffer with division by 2 */
  265. log_en = shr_ex(log_en, 1);
  266. st->log_en_hist[st->hist_ptr] = log_en; /* Q10 */ move16 ();
  267. return 0;
  268. }
  269. /*
  270. **************************************************************************
  271. *
  272. * Function : tx_dtx_handler
  273. * Purpose : adds extra speech hangover to analyze speech on the decoding side.
  274. *
  275. **************************************************************************
  276. */
  277. Word16 tx_dtx_handler(dtx_encState *st, /* i/o : State struct */
  278. Word16 vad_flag, /* i : vad decision */
  279. enum Mode *usedMode /* i/o : mode changed or not */
  280. )
  281. {
  282. Word16 compute_new_sid_possible;
  283. /* this state machine is in synch with the GSMEFR txDtx machine */
  284. st->decAnaElapsedCount = add_ex(st->decAnaElapsedCount, 1);
  285. compute_new_sid_possible = 0; move16();
  286. test();
  287. if (vad_flag != 0)
  288. {
  289. st->dtxHangoverCount = DTX_HANG_CONST; move16();
  290. }
  291. else
  292. { /* non-speech */
  293. test();
  294. if (st->dtxHangoverCount == 0)
  295. { /* out of decoder analysis hangover */
  296. st->decAnaElapsedCount = 0; move16();
  297. *usedMode = MRDTX; move16();
  298. compute_new_sid_possible = 1; move16();
  299. }
  300. else
  301. { /* in possible analysis hangover */
  302. st->dtxHangoverCount = sub_ex(st->dtxHangoverCount, 1);
  303. /* decAnaElapsedCount + dtxHangoverCount < DTX_ELAPSED_FRAMES_THRESH */
  304. test ();
  305. if (sub_ex(add_ex(st->decAnaElapsedCount, st->dtxHangoverCount),
  306. DTX_ELAPSED_FRAMES_THRESH) < 0)
  307. {
  308. *usedMode = MRDTX; move16();
  309. /* if short time since decoder update, do not add extra HO */
  310. }
  311. /*
  312. else
  313. override VAD and stay in
  314. speech mode *usedMode
  315. and add extra hangover
  316. */
  317. }
  318. }
  319. return compute_new_sid_possible;
  320. }