dec_amr.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140
  1. /*************************************************************************
  2. *
  3. * GSM AMR-NB speech codec R98 Version 7.6.0 December 12, 2001
  4. * R99 Version 3.3.0
  5. * REL-4 Version 4.1.0
  6. *
  7. *****************************************************************************
  8. *
  9. * File : dec_amr.c
  10. * Purpose : Decoding of one speech frame using given codec mode
  11. *
  12. *****************************************************************************
  13. */
  14. /*
  15. *****************************************************************************
  16. * MODULE INCLUDE FILE AND VERSION ID
  17. *****************************************************************************
  18. */
  19. #include "dec_amr.h"
  20. const char dec_amr_id[] = "@(#)$Id $" dec_amr_h;
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #include "typedef.h"
  24. #include "basic_op.h"
  25. #include "count.h"
  26. #include "cnst.h"
  27. #include "copy.h"
  28. #include "set_zero.h"
  29. #include "syn_filt.h"
  30. #include "d_plsf.h"
  31. #include "agc.h"
  32. #include "int_lpc.h"
  33. #include "dec_gain.h"
  34. #include "dec_lag3.h"
  35. #include "dec_lag6.h"
  36. #include "d2_9pf.h"
  37. #include "d2_11pf.h"
  38. #include "d3_14pf.h"
  39. #include "d4_17pf.h"
  40. #include "d8_31pf.h"
  41. #include "d1035pf.h"
  42. #include "pred_lt.h"
  43. #include "d_gain_p.h"
  44. #include "d_gain_c.h"
  45. #include "dec_gain.h"
  46. #include "ec_gains.h"
  47. #include "ph_disp.h"
  48. #include "c_g_aver.h"
  49. #include "int_lsf.h"
  50. #include "lsp_lsf.h"
  51. #include "lsp_avg.h"
  52. #include "bgnscd.h"
  53. #include "ex_ctrl.h"
  54. #include "sqrt_l.h"
  55. #include "frame.h"
  56. #include "lsp.tab"
  57. #include "bitno.tab"
  58. #include "b_cn_cod.h"
  59. /*
  60. *****************************************************************************
  61. * LOCAL VARIABLES AND TABLES
  62. *****************************************************************************
  63. */
  64. /*-----------------------------------------------------------------*
  65. * Decoder constant parameters (defined in "cnst.h") *
  66. *-----------------------------------------------------------------*
  67. * L_FRAME : Frame size. *
  68. * L_FRAME_BY2 : Half the frame size. *
  69. * L_SUBFR : Sub-frame size. *
  70. * M : LPC order. *
  71. * MP1 : LPC order+1 *
  72. * PIT_MIN : Minimum pitch lag. *
  73. * PIT_MIN_MR122 : Minimum pitch lag for the MR122 mode. *
  74. * PIT_MAX : Maximum pitch lag. *
  75. * L_INTERPOL : Length of filter for interpolation *
  76. * PRM_SIZE : size of vector containing analysis parameters *
  77. *-----------------------------------------------------------------*/
  78. /*
  79. *****************************************************************************
  80. * PUBLIC PROGRAM CODE
  81. *****************************************************************************
  82. */
  83. /*
  84. **************************************************************************
  85. *
  86. * Function : Decoder_amr_init
  87. * Purpose : Allocates and initializes state memory
  88. *
  89. **************************************************************************
  90. */
  91. int Decoder_amr_init (Decoder_amrState **state)
  92. {
  93. Decoder_amrState* s;
  94. Word16 i;
  95. if (state == (Decoder_amrState **) NULL){
  96. wfprintf(stderr, "Decoder_amr_init: invalid parameter\n");
  97. return -1;
  98. }
  99. *state = NULL;
  100. /* allocate memory */
  101. if ((s= (Decoder_amrState *) wmalloc(sizeof(Decoder_amrState))) == NULL){
  102. wfprintf(stderr, "Decoder_amr_init: can not malloc state structure\n");
  103. return -1;
  104. }
  105. s->T0_lagBuff = 40;
  106. s->inBackgroundNoise = 0;
  107. s->voicedHangover = 0;
  108. for (i = 0; i < 9; i++)
  109. s->ltpGainHistory[i] = 0;
  110. s->lsfState = NULL;
  111. s->ec_gain_p_st = NULL;
  112. s->ec_gain_c_st = NULL;
  113. s->pred_state = NULL;
  114. s->ph_disp_st = NULL;
  115. s->dtxDecoderState = NULL;
  116. if (D_plsf_init(&s->lsfState) ||
  117. ec_gain_pitch_init(&s->ec_gain_p_st) ||
  118. ec_gain_code_init(&s->ec_gain_c_st) ||
  119. gc_pred_init(&s->pred_state) ||
  120. Cb_gain_average_init(&s->Cb_gain_averState) ||
  121. lsp_avg_init(&s->lsp_avg_st) ||
  122. Bgn_scd_init(&s->background_state) ||
  123. ph_disp_init(&s->ph_disp_st) ||
  124. dtx_dec_init(&s->dtxDecoderState)) {
  125. Decoder_amr_exit(&s);
  126. return -1;
  127. }
  128. Decoder_amr_reset(s, (enum Mode)0);
  129. *state = s;
  130. return 0;
  131. }
  132. /*
  133. **************************************************************************
  134. *
  135. * Function : Decoder_amr_reset
  136. * Purpose : Resets state memory
  137. *
  138. **************************************************************************
  139. */
  140. int Decoder_amr_reset (Decoder_amrState *state, enum Mode mode)
  141. {
  142. Word16 i;
  143. if (state == (Decoder_amrState *) NULL){
  144. wfprintf(stderr, "Decoder_amr_reset: invalid parameter\n");
  145. return -1;
  146. }
  147. /* Initialize static pointer */
  148. state->exc = state->old_exc + PIT_MAX + L_INTERPOL;
  149. /* Static vectors to zero */
  150. Set_zero (state->old_exc, PIT_MAX + L_INTERPOL);
  151. if (mode != MRDTX)
  152. Set_zero (state->mem_syn, M);
  153. /* initialize pitch sharpening */
  154. state->sharp = SHARPMIN;
  155. state->old_T0 = 40;
  156. /* Initialize state->lsp_old [] */
  157. if (mode != MRDTX) {
  158. Copy(lsp_init_data, &state->lsp_old[0], M);
  159. }
  160. /* Initialize memories of bad frame handling */
  161. state->prev_bf = 0;
  162. state->prev_pdf = 0;
  163. state->state = 0;
  164. state->T0_lagBuff = 40;
  165. state->inBackgroundNoise = 0;
  166. state->voicedHangover = 0;
  167. if (mode != MRDTX) {
  168. for (i=0;i<9;i++)
  169. state->excEnergyHist[i] = 0;
  170. }
  171. for (i = 0; i < 9; i++)
  172. state->ltpGainHistory[i] = 0;
  173. Cb_gain_average_reset(state->Cb_gain_averState);
  174. if (mode != MRDTX)
  175. lsp_avg_reset(state->lsp_avg_st);
  176. D_plsf_reset(state->lsfState);
  177. ec_gain_pitch_reset(state->ec_gain_p_st);
  178. ec_gain_code_reset(state->ec_gain_c_st);
  179. if (mode != MRDTX)
  180. gc_pred_reset(state->pred_state);
  181. Bgn_scd_reset(state->background_state);
  182. state->nodataSeed = 21845;
  183. ph_disp_reset(state->ph_disp_st);
  184. if (mode != MRDTX)
  185. dtx_dec_reset(state->dtxDecoderState);
  186. return 0;
  187. }
  188. /*
  189. **************************************************************************
  190. *
  191. * Function : Decoder_amr_exit
  192. * Purpose : The memory used for state memory is freed
  193. *
  194. **************************************************************************
  195. */
  196. void Decoder_amr_exit (Decoder_amrState **state)
  197. {
  198. if (state == NULL || *state == NULL)
  199. return;
  200. D_plsf_exit(&(*state)->lsfState);
  201. ec_gain_pitch_exit(&(*state)->ec_gain_p_st);
  202. ec_gain_code_exit(&(*state)->ec_gain_c_st);
  203. gc_pred_exit(&(*state)->pred_state);
  204. Bgn_scd_exit(&(*state)->background_state);
  205. ph_disp_exit(&(*state)->ph_disp_st);
  206. Cb_gain_average_exit(&(*state)->Cb_gain_averState);
  207. lsp_avg_exit(&(*state)->lsp_avg_st);
  208. dtx_dec_exit(&(*state)->dtxDecoderState);
  209. /* deallocate memory */
  210. wfree(*state);
  211. *state = NULL;
  212. return;
  213. }
  214. /*
  215. **************************************************************************
  216. *
  217. * Function : Decoder_amr
  218. * Purpose : Speech decoder routine.
  219. *
  220. **************************************************************************
  221. */
  222. int Decoder_amr (
  223. Decoder_amrState *st, /* i/o : State variables */
  224. enum Mode mode, /* i : AMR mode */
  225. Word16 parm[], /* i : vector of synthesis parameters
  226. (PRM_SIZE) */
  227. enum RXFrameType frame_type, /* i : received frame type */
  228. Word16 synth[], /* o : synthesis speech (L_FRAME) */
  229. Word16 A_t[] /* o : decoded LP filter in 4 subframes
  230. (AZ_SIZE) */
  231. )
  232. {
  233. /* LPC coefficients */
  234. Word16 *Az; /* Pointer on A_t */
  235. /* LSPs */
  236. Word16 lsp_new[M];
  237. Word16 lsp_mid[M];
  238. /* LSFs */
  239. Word16 prev_lsf[M];
  240. Word16 lsf_i[M];
  241. /* Algebraic codevector */
  242. Word16 code[L_SUBFR];
  243. /* excitation */
  244. Word16 excp[L_SUBFR];
  245. Word16 exc_enhanced[L_SUBFR];
  246. /* Scalars */
  247. Word16 i, i_subfr;
  248. Word16 T0, T0_frac, index, index_mr475 = 0;
  249. Word16 gain_pit, gain_code, gain_code_mix, pit_sharp, pit_flag, pitch_fac;
  250. Word16 t0_min, t0_max;
  251. Word16 delta_frc_low, delta_frc_range;
  252. Word16 tmp_shift;
  253. Word16 temp;
  254. Word32 L_temp;
  255. Word16 flag4;
  256. Word16 carefulFlag;
  257. Word16 excEnergy;
  258. Word16 subfrNr;
  259. Word16 evenSubfr = 0;
  260. Word16 bfi = 0; /* bad frame indication flag */
  261. Word16 pdfi = 0; /* potential degraded bad frame flag */
  262. enum DTXStateType newDTXState; /* SPEECH , DTX, DTX_MUTE */
  263. /* find the new DTX state SPEECH OR DTX */
  264. newDTXState = rx_dtx_handler(st->dtxDecoderState, frame_type);
  265. move16 (); /* function result */
  266. /* DTX actions */
  267. test();
  268. if (sub_ex(newDTXState, SPEECH) != 0 )
  269. {
  270. Decoder_amr_reset (st, MRDTX);
  271. dtx_dec(st->dtxDecoderState,
  272. st->mem_syn,
  273. st->lsfState,
  274. st->pred_state,
  275. st->Cb_gain_averState,
  276. newDTXState,
  277. mode,
  278. parm, synth, A_t);
  279. /* update average lsp */
  280. Lsf_lsp(st->lsfState->past_lsf_q, st->lsp_old, M);
  281. lsp_avg(st->lsp_avg_st, st->lsfState->past_lsf_q);
  282. goto the_end;
  283. }
  284. /* SPEECH action state machine */
  285. test (); test (); test ();
  286. if ((sub_ex(frame_type, RX_SPEECH_BAD) == 0) ||
  287. (sub_ex(frame_type, RX_NO_DATA) == 0) ||
  288. (sub_ex(frame_type, RX_ONSET) == 0))
  289. {
  290. bfi = 1; move16 ();
  291. test(); test();
  292. if ((sub_ex(frame_type, RX_NO_DATA) == 0) ||
  293. (sub_ex(frame_type, RX_ONSET) == 0))
  294. {
  295. build_CN_param(&st->nodataSeed,
  296. prmno[mode],
  297. bitno[mode],
  298. parm);
  299. }
  300. }
  301. else if (sub_ex(frame_type, RX_SPEECH_DEGRADED) == 0)
  302. {
  303. pdfi = 1; move16 ();
  304. }
  305. test();
  306. if (bfi != 0)
  307. {
  308. st->state = add_ex (st->state, 1);
  309. }
  310. else if (sub_ex (st->state, 6) == 0)
  311. {
  312. st->state = 5; move16 ();
  313. }
  314. else
  315. {
  316. st->state = 0; move16 ();
  317. }
  318. test ();
  319. if (sub_ex (st->state, 6) > 0)
  320. {
  321. st->state = 6; move16 ();
  322. }
  323. /* If this frame is the first speech frame after CNI period, */
  324. /* set the BFH state machine to an appropriate state depending */
  325. /* on whether there was DTX muting before start of speech or not */
  326. /* If there was DTX muting, the first speech frame is muted. */
  327. /* If there was no DTX muting, the first speech frame is not */
  328. /* muted. The BFH state machine starts from state 5, however, to */
  329. /* keep the audible noise resulting from a SID frame which is */
  330. /* erroneously interpreted as a good speech frame as small as */
  331. /* possible (the decoder output in this case is quickly muted) */
  332. test();
  333. if (sub_ex(st->dtxDecoderState->dtxGlobalState, DTX) == 0)
  334. {
  335. st->state = 5; move16 ();
  336. st->prev_bf = 0; move16 ();
  337. }
  338. else if (test (), sub_ex(st->dtxDecoderState->dtxGlobalState, DTX_MUTE) == 0)
  339. {
  340. st->state = 5; move16 ();
  341. st->prev_bf = 1; move16 ();
  342. }
  343. /* save old LSFs for CB gain smoothing */
  344. Copy (st->lsfState->past_lsf_q, prev_lsf, M);
  345. /* decode LSF parameters and generate interpolated lpc coefficients
  346. for the 4 subframes */
  347. test ();
  348. if (sub_ex (mode, MR122) != 0)
  349. {
  350. D_plsf_3(st->lsfState, mode, bfi, parm, lsp_new);
  351. fwc (); /* function worst case */
  352. /* Advance synthesis parameters pointer */
  353. parm += 3; move16 ();
  354. Int_lpc_1to3(st->lsp_old, lsp_new, A_t);
  355. }
  356. else
  357. {
  358. D_plsf_5 (st->lsfState, bfi, parm, lsp_mid, lsp_new);
  359. fwc (); /* function worst case */
  360. /* Advance synthesis parameters pointer */
  361. parm += 5; move16 ();
  362. Int_lpc_1and3 (st->lsp_old, lsp_mid, lsp_new, A_t);
  363. }
  364. /* update the LSPs for the next frame */
  365. for (i = 0; i < M; i++)
  366. {
  367. st->lsp_old[i] = lsp_new[i]; move16 ();
  368. }
  369. fwc (); /* function worst case */
  370. /*------------------------------------------------------------------------*
  371. * Loop for every subframe in the analysis frame *
  372. *------------------------------------------------------------------------*
  373. * The subframe size is L_SUBFR and the loop is repeated L_FRAME/L_SUBFR *
  374. * times *
  375. * - decode the pitch delay *
  376. * - decode algebraic code *
  377. * - decode pitch and codebook gains *
  378. * - find the excitation and compute synthesis speech *
  379. *------------------------------------------------------------------------*/
  380. /* pointer to interpolated LPC parameters */
  381. Az = A_t; move16 ();
  382. evenSubfr = 0; move16();
  383. subfrNr = -1; move16();
  384. for (i_subfr = 0; i_subfr < L_FRAME; i_subfr += L_SUBFR)
  385. {
  386. subfrNr = add_ex(subfrNr, 1);
  387. evenSubfr = sub_ex(1, evenSubfr);
  388. /* flag for first and 3th subframe */
  389. pit_flag = i_subfr; move16 ();
  390. test();
  391. if (sub_ex (i_subfr, L_FRAME_BY2) == 0)
  392. {
  393. test(); test();
  394. if (sub_ex(mode, MR475) != 0 && sub_ex(mode, MR515) != 0)
  395. {
  396. pit_flag = 0; move16 ();
  397. }
  398. }
  399. /* pitch index */
  400. index = *parm++; move16 ();
  401. /*-------------------------------------------------------*
  402. * - decode pitch lag and find adaptive codebook vector. *
  403. *-------------------------------------------------------*/
  404. test ();
  405. if (sub_ex(mode, MR122) != 0)
  406. {
  407. /* flag4 indicates encoding with 4 bit resolution; */
  408. /* this is needed for mode MR475, MR515, MR59 and MR67 */
  409. flag4 = 0; move16 ();
  410. test (); test (); test (); test ();
  411. if ((sub_ex (mode, MR475) == 0) ||
  412. (sub_ex (mode, MR515) == 0) ||
  413. (sub_ex (mode, MR59) == 0) ||
  414. (sub_ex (mode, MR67) == 0) ) {
  415. flag4 = 1; move16 ();
  416. }
  417. /*-------------------------------------------------------*
  418. * - get ranges for the t0_min and t0_max *
  419. * - only needed in delta decoding *
  420. *-------------------------------------------------------*/
  421. delta_frc_low = 5; move16();
  422. delta_frc_range = 9; move16();
  423. test ();
  424. if ( sub_ex(mode, MR795) == 0 )
  425. {
  426. delta_frc_low = 10; move16();
  427. delta_frc_range = 19; move16();
  428. }
  429. t0_min = sub_ex(st->old_T0, delta_frc_low);
  430. test ();
  431. if (sub_ex(t0_min, PIT_MIN) < 0)
  432. {
  433. t0_min = PIT_MIN; move16();
  434. }
  435. t0_max = add_ex(t0_min, delta_frc_range);
  436. test ();
  437. if (sub_ex(t0_max, PIT_MAX) > 0)
  438. {
  439. t0_max = PIT_MAX; move16();
  440. t0_min = sub_ex(t0_max, delta_frc_range);
  441. }
  442. Dec_lag3 (index, t0_min, t0_max, pit_flag, st->old_T0,
  443. &T0, &T0_frac, flag4);
  444. st->T0_lagBuff = T0; move16 ();
  445. test ();
  446. if (bfi != 0)
  447. {
  448. test ();
  449. if (sub_ex (st->old_T0, PIT_MAX) < 0)
  450. { /* Graceful pitch */
  451. st->old_T0 = add_ex(st->old_T0, 1); /* degradation */
  452. }
  453. T0 = st->old_T0; move16 ();
  454. T0_frac = 0; move16 ();
  455. test (); test (); test (); test (); test ();
  456. if ( st->inBackgroundNoise != 0 &&
  457. sub_ex(st->voicedHangover, 4) > 0 &&
  458. ((sub_ex(mode, MR475) == 0 ) ||
  459. (sub_ex(mode, MR515) == 0 ) ||
  460. (sub_ex(mode, MR59) == 0) )
  461. )
  462. {
  463. T0 = st->T0_lagBuff; move16 ();
  464. }
  465. }
  466. fwc (); /* function worst case */
  467. Pred_lt_3or6 (st->exc, T0, T0_frac, L_SUBFR, 1);
  468. }
  469. else
  470. {
  471. Dec_lag6 (index, PIT_MIN_MR122,
  472. PIT_MAX, pit_flag, &T0, &T0_frac);
  473. test (); test (); test ();
  474. if ( bfi == 0 && (pit_flag == 0 || sub_ex (index, 61) < 0))
  475. {
  476. }
  477. else
  478. {
  479. st->T0_lagBuff = T0; move16 ();
  480. T0 = st->old_T0; move16 ();
  481. T0_frac = 0; move16 ();
  482. }
  483. fwc (); /* function worst case */
  484. Pred_lt_3or6 (st->exc, T0, T0_frac, L_SUBFR, 0);
  485. }
  486. fwc (); /* function worst case */
  487. /*-------------------------------------------------------*
  488. * - (MR122 only: Decode pitch gain.) *
  489. * - Decode innovative codebook. *
  490. * - set pitch sharpening factor *
  491. *-------------------------------------------------------*/
  492. test (); test ();
  493. if (sub_ex (mode, MR475) == 0 || sub_ex (mode, MR515) == 0)
  494. { /* MR475, MR515 */
  495. index = *parm++; /* index of position */ move16 ();
  496. i = *parm++; /* signs */ move16 ();
  497. fwc (); /* function worst case */
  498. decode_2i40_9bits (subfrNr, i, index, code);
  499. fwc (); /* function worst case */
  500. pit_sharp = shl_ex (st->sharp, 1);
  501. }
  502. else if (sub_ex (mode, MR59) == 0)
  503. { /* MR59 */
  504. test ();
  505. index = *parm++; /* index of position */ move16 ();
  506. i = *parm++; /* signs */ move16 ();
  507. fwc (); /* function worst case */
  508. decode_2i40_11bits (i, index, code);
  509. fwc (); /* function worst case */
  510. pit_sharp = shl_ex (st->sharp, 1);
  511. }
  512. else if (sub_ex (mode, MR67) == 0)
  513. { /* MR67 */
  514. test (); test ();
  515. index = *parm++; /* index of position */ move16 ();
  516. i = *parm++; /* signs */ move16 ();
  517. fwc (); /* function worst case */
  518. decode_3i40_14bits (i, index, code);
  519. fwc (); /* function worst case */
  520. pit_sharp = shl_ex (st->sharp, 1);
  521. }
  522. else if (sub_ex (mode, MR795) <= 0)
  523. { /* MR74, MR795 */
  524. test (); test (); test ();
  525. index = *parm++; /* index of position */ move16 ();
  526. i = *parm++; /* signs */ move16 ();
  527. fwc (); /* function worst case */
  528. decode_4i40_17bits (i, index, code);
  529. fwc (); /* function worst case */
  530. pit_sharp = shl_ex (st->sharp, 1);
  531. }
  532. else if (sub_ex (mode, MR102) == 0)
  533. { /* MR102 */
  534. test (); test (); test ();
  535. fwc (); /* function worst case */
  536. dec_8i40_31bits (parm, code);
  537. parm += 7; move16 ();
  538. fwc (); /* function worst case */
  539. pit_sharp = shl_ex (st->sharp, 1);
  540. }
  541. else
  542. { /* MR122 */
  543. test (); test (); test ();
  544. index = *parm++; move16 ();
  545. test();
  546. if (bfi != 0)
  547. {
  548. ec_gain_pitch (st->ec_gain_p_st, st->state, &gain_pit);
  549. }
  550. else
  551. {
  552. gain_pit = d_gain_pitch (mode, index); move16 ();
  553. }
  554. ec_gain_pitch_update (st->ec_gain_p_st, bfi, st->prev_bf,
  555. &gain_pit);
  556. fwc (); /* function worst case */
  557. dec_10i40_35bits (parm, code);
  558. parm += 10; move16 ();
  559. fwc (); /* function worst case */
  560. /* pit_sharp = gain_pit; */
  561. /* if (pit_sharp > 1.0) pit_sharp = 1.0; */
  562. pit_sharp = shl_ex (gain_pit, 1);
  563. }
  564. /*-------------------------------------------------------*
  565. * - Add the pitch contribution to code[]. *
  566. *-------------------------------------------------------*/
  567. for (i = T0; i < L_SUBFR; i++)
  568. {
  569. temp = mult_ex (code[i - T0], pit_sharp);
  570. code[i] = add_ex (code[i], temp);
  571. move16 ();
  572. }
  573. fwc (); /* function worst case */
  574. /*------------------------------------------------------------*
  575. * - Decode codebook gain (MR122) or both pitch *
  576. * gain and codebook gain (all others) *
  577. * - Update pitch sharpening "sharp" with quantized gain_pit *
  578. *------------------------------------------------------------*/
  579. if (test(), sub_ex (mode, MR475) == 0)
  580. {
  581. /* read and decode pitch and code gain */
  582. test();
  583. if (evenSubfr != 0)
  584. {
  585. index_mr475 = *parm++; move16 (); /* index of gain(s) */
  586. }
  587. test();
  588. if (bfi == 0)
  589. {
  590. Dec_gain(st->pred_state, mode, index_mr475, code,
  591. evenSubfr, &gain_pit, &gain_code);
  592. }
  593. else
  594. {
  595. ec_gain_pitch (st->ec_gain_p_st, st->state, &gain_pit);
  596. ec_gain_code (st->ec_gain_c_st, st->pred_state, st->state,
  597. &gain_code);
  598. }
  599. ec_gain_pitch_update (st->ec_gain_p_st, bfi, st->prev_bf,
  600. &gain_pit);
  601. ec_gain_code_update (st->ec_gain_c_st, bfi, st->prev_bf,
  602. &gain_code);
  603. fwc (); /* function worst case */
  604. pit_sharp = gain_pit; move16 ();
  605. test ();
  606. if (sub_ex (pit_sharp, SHARPMAX) > 0)
  607. {
  608. pit_sharp = SHARPMAX; move16 ();
  609. }
  610. }
  611. else if (test(), test(), (sub_ex (mode, MR74) <= 0) ||
  612. (sub_ex (mode, MR102) == 0))
  613. {
  614. /* read and decode pitch and code gain */
  615. index = *parm++; move16 (); /* index of gain(s) */
  616. test();
  617. if (bfi == 0)
  618. {
  619. Dec_gain(st->pred_state, mode, index, code,
  620. evenSubfr, &gain_pit, &gain_code);
  621. }
  622. else
  623. {
  624. ec_gain_pitch (st->ec_gain_p_st, st->state, &gain_pit);
  625. ec_gain_code (st->ec_gain_c_st, st->pred_state, st->state,
  626. &gain_code);
  627. }
  628. ec_gain_pitch_update (st->ec_gain_p_st, bfi, st->prev_bf,
  629. &gain_pit);
  630. ec_gain_code_update (st->ec_gain_c_st, bfi, st->prev_bf,
  631. &gain_code);
  632. fwc (); /* function worst case */
  633. pit_sharp = gain_pit; move16 ();
  634. test ();
  635. if (sub_ex (pit_sharp, SHARPMAX) > 0)
  636. {
  637. pit_sharp = SHARPMAX; move16 ();
  638. }
  639. if (sub_ex (mode, MR102) == 0)
  640. {
  641. if (sub_ex (st->old_T0, add_ex(L_SUBFR, 5)) > 0)
  642. {
  643. pit_sharp = shr_ex(pit_sharp, 2);
  644. }
  645. }
  646. }
  647. else
  648. {
  649. /* read and decode pitch gain */
  650. index = *parm++; move16 (); /* index of gain(s) */
  651. test ();
  652. if (sub_ex (mode, MR795) == 0)
  653. {
  654. /* decode pitch gain */
  655. test();
  656. if (bfi != 0)
  657. {
  658. ec_gain_pitch (st->ec_gain_p_st, st->state, &gain_pit);
  659. }
  660. else
  661. {
  662. gain_pit = d_gain_pitch (mode, index); move16 ();
  663. }
  664. ec_gain_pitch_update (st->ec_gain_p_st, bfi, st->prev_bf,
  665. &gain_pit);
  666. /* read and decode code gain */
  667. index = *parm++; move16 ();
  668. test();
  669. if (bfi == 0)
  670. {
  671. d_gain_code (st->pred_state, mode, index, code, &gain_code);
  672. }
  673. else
  674. {
  675. ec_gain_code (st->ec_gain_c_st, st->pred_state, st->state,
  676. &gain_code);
  677. }
  678. ec_gain_code_update (st->ec_gain_c_st, bfi, st->prev_bf,
  679. &gain_code);
  680. fwc (); /* function worst case */
  681. pit_sharp = gain_pit; move16 ();
  682. test ();
  683. if (sub_ex (pit_sharp, SHARPMAX) > 0)
  684. {
  685. pit_sharp = SHARPMAX; move16 ();
  686. }
  687. }
  688. else
  689. { /* MR122 */
  690. test();
  691. if (bfi == 0)
  692. {
  693. d_gain_code (st->pred_state, mode, index, code, &gain_code);
  694. }
  695. else
  696. {
  697. ec_gain_code (st->ec_gain_c_st, st->pred_state, st->state,
  698. &gain_code);
  699. }
  700. ec_gain_code_update (st->ec_gain_c_st, bfi, st->prev_bf,
  701. &gain_code);
  702. fwc (); /* function worst case */
  703. pit_sharp = gain_pit; move16 ();
  704. }
  705. }
  706. /* store pitch sharpening for next subframe */
  707. /* (for modes which use the previous pitch gain for
  708. pitch sharpening in the search phase) */
  709. /* do not update sharpening in even subframes for MR475 */
  710. test(); test();
  711. if (sub_ex(mode, MR475) != 0 || evenSubfr == 0)
  712. {
  713. st->sharp = gain_pit; move16 ();
  714. test ();
  715. if (sub_ex (st->sharp, SHARPMAX) > 0)
  716. {
  717. st->sharp = SHARPMAX; move16 ();
  718. }
  719. }
  720. pit_sharp = shl_ex (pit_sharp, 1);
  721. test ();
  722. if (sub_ex (pit_sharp, 16384) > 0)
  723. {
  724. for (i = 0; i < L_SUBFR; i++)
  725. {
  726. temp = mult_ex (st->exc[i], pit_sharp);
  727. L_temp = L_mult_ex (temp, gain_pit);
  728. test ();
  729. if (sub_ex(mode, MR122)==0)
  730. {
  731. L_temp = L_shr_ex (L_temp, 1);
  732. }
  733. excp[i] = round_ex (L_temp); move16 ();
  734. }
  735. }
  736. /*-------------------------------------------------------*
  737. * - Store list of LTP gains needed in the source *
  738. * characteristic detector (SCD) *
  739. *-------------------------------------------------------*/
  740. test ();
  741. if ( bfi == 0 )
  742. {
  743. for (i = 0; i < 8; i++)
  744. {
  745. st->ltpGainHistory[i] = st->ltpGainHistory[i+1]; move16 ();
  746. }
  747. st->ltpGainHistory[8] = gain_pit; move16 ();
  748. }
  749. /*-------------------------------------------------------*
  750. * - Limit gain_pit if in background noise and BFI *
  751. * for MR475, MR515, MR59 *
  752. *-------------------------------------------------------*/
  753. test (); test (); test (); test (); test (); test ();
  754. if ( (st->prev_bf != 0 || bfi != 0) && st->inBackgroundNoise != 0 &&
  755. ((sub_ex(mode, MR475) == 0) ||
  756. (sub_ex(mode, MR515) == 0) ||
  757. (sub_ex(mode, MR59) == 0))
  758. )
  759. {
  760. test ();
  761. if ( sub_ex (gain_pit, 12288) > 0) /* if (gain_pit > 0.75) in Q14*/
  762. gain_pit = add_ex( shr_ex( sub_ex(gain_pit, 12288), 1 ), 12288 );
  763. /* gain_pit = (gain_pit-0.75)/2.0 + 0.75; */
  764. test ();
  765. if ( sub_ex (gain_pit, 14745) > 0) /* if (gain_pit > 0.90) in Q14*/
  766. {
  767. gain_pit = 14745; move16 ();
  768. }
  769. }
  770. /*-------------------------------------------------------*
  771. * Calculate CB mixed gain *
  772. *-------------------------------------------------------*/
  773. Int_lsf(prev_lsf, st->lsfState->past_lsf_q, i_subfr, lsf_i);
  774. gain_code_mix = Cb_gain_average(
  775. st->Cb_gain_averState, mode, gain_code,
  776. lsf_i, st->lsp_avg_st->lsp_meanSave, bfi,
  777. st->prev_bf, pdfi, st->prev_pdf,
  778. st->inBackgroundNoise, st->voicedHangover); move16 ();
  779. /* make sure that MR74, MR795, MR122 have original code_gain*/
  780. test();
  781. if ((sub_ex(mode, MR67) > 0) && (sub_ex(mode, MR102) != 0) )
  782. /* MR74, MR795, MR122 */
  783. {
  784. gain_code_mix = gain_code; move16 ();
  785. }
  786. /*-------------------------------------------------------*
  787. * - Find the total excitation. *
  788. * - Find synthesis speech corresponding to st->exc[]. *
  789. *-------------------------------------------------------*/
  790. test ();
  791. if (sub_ex(mode, MR102) <= 0) /* MR475, MR515, MR59, MR67, MR74, MR795, MR102*/
  792. {
  793. pitch_fac = gain_pit; move16 ();
  794. tmp_shift = 1; move16 ();
  795. }
  796. else /* MR122 */
  797. {
  798. pitch_fac = shr_ex (gain_pit, 1); move16 ();
  799. tmp_shift = 2; move16 ();
  800. }
  801. /* copy unscaled LTP excitation to exc_enhanced (used in phase
  802. * dispersion below) and compute total excitation for LTP feedback
  803. */
  804. for (i = 0; i < L_SUBFR; i++)
  805. {
  806. exc_enhanced[i] = st->exc[i]; move16 ();
  807. /* st->exc[i] = gain_pit*st->exc[i] + gain_code*code[i]; */
  808. L_temp = L_mult_ex (st->exc[i], pitch_fac);
  809. /* 12.2: Q0 * Q13 */
  810. /* 7.4: Q0 * Q14 */
  811. L_temp = L_mac_ex (L_temp, code[i], gain_code);
  812. /* 12.2: Q12 * Q1 */
  813. /* 7.4: Q13 * Q1 */
  814. L_temp = L_shl_ex (L_temp, tmp_shift); /* Q16 */
  815. st->exc[i] = round_ex (L_temp); move16 ();
  816. }
  817. /*-------------------------------------------------------*
  818. * - Adaptive phase dispersion *
  819. *-------------------------------------------------------*/
  820. ph_disp_release(st->ph_disp_st); /* free phase dispersion adaption */
  821. test (); test (); test (); test (); test (); test ();
  822. if ( ((sub_ex(mode, MR475) == 0) ||
  823. (sub_ex(mode, MR515) == 0) ||
  824. (sub_ex(mode, MR59) == 0)) &&
  825. sub_ex(st->voicedHangover, 3) > 0 &&
  826. st->inBackgroundNoise != 0 &&
  827. bfi != 0 )
  828. {
  829. ph_disp_lock(st->ph_disp_st); /* Always Use full Phase Disp. */
  830. } /* if error in bg noise */
  831. /* apply phase dispersion to innovation (if enabled) and
  832. compute total excitation for synthesis part */
  833. ph_disp(st->ph_disp_st, mode,
  834. exc_enhanced, gain_code_mix, gain_pit, code,
  835. pitch_fac, tmp_shift);
  836. /*-------------------------------------------------------*
  837. * - The Excitation control module are active during BFI.*
  838. * - Conceal drops in signal energy if in bg noise. *
  839. *-------------------------------------------------------*/
  840. L_temp = 0; move32 ();
  841. for (i = 0; i < L_SUBFR; i++)
  842. {
  843. L_temp = L_mac_ex (L_temp, exc_enhanced[i], exc_enhanced[i] );
  844. }
  845. L_temp = L_shr_ex (L_temp, 1); /* excEnergy = sqrt(L_temp) in Q0 */
  846. L_temp = sqrt_l_exp(L_temp, &temp); move32 (); /* function result */
  847. L_temp = L_shr_ex(L_temp, add_ex( shr_ex(temp, 1), 15));
  848. L_temp = L_shr_ex(L_temp, 2); /* To cope with 16-bit and */
  849. excEnergy = extract_l_ex(L_temp); /* scaling in ex_ctrl() */
  850. test (); test (); test (); test (); test ();
  851. test (); test (); test (); test (); test ();
  852. if ( ((sub_ex (mode, MR475) == 0) ||
  853. (sub_ex (mode, MR515) == 0) ||
  854. (sub_ex (mode, MR59) == 0)) &&
  855. sub_ex(st->voicedHangover, 5) > 0 &&
  856. st->inBackgroundNoise != 0 &&
  857. sub_ex(st->state, 4) < 0 &&
  858. ( (pdfi != 0 && st->prev_pdf != 0) ||
  859. bfi != 0 ||
  860. st->prev_bf != 0) )
  861. {
  862. carefulFlag = 0; move32 ();
  863. test (); test ();
  864. if ( pdfi != 0 && bfi == 0 )
  865. {
  866. carefulFlag = 1; move16 ();
  867. }
  868. Ex_ctrl(exc_enhanced,
  869. excEnergy,
  870. st->excEnergyHist,
  871. st->voicedHangover,
  872. st->prev_bf,
  873. carefulFlag);
  874. }
  875. test (); test (); test (); test ();
  876. if ( st->inBackgroundNoise != 0 &&
  877. ( bfi != 0 || st->prev_bf != 0 ) &&
  878. sub_ex(st->state, 4) < 0 )
  879. {
  880. ; /* do nothing! */
  881. }
  882. else
  883. {
  884. /* Update energy history for all modes */
  885. for (i = 0; i < 8; i++)
  886. {
  887. st->excEnergyHist[i] = st->excEnergyHist[i+1]; move16 ();
  888. }
  889. st->excEnergyHist[8] = excEnergy; move16 ();
  890. }
  891. /*-------------------------------------------------------*
  892. * Excitation control module end. *
  893. *-------------------------------------------------------*/
  894. fwc (); /* function worst case */
  895. test ();
  896. if (sub_ex (pit_sharp, 16384) > 0)
  897. {
  898. for (i = 0; i < L_SUBFR; i++)
  899. {
  900. excp[i] = add_ex (excp[i], exc_enhanced[i]);
  901. move16 ();
  902. }
  903. agc2 (exc_enhanced, excp, L_SUBFR);
  904. Overflow = 0; move16 ();
  905. Syn_filt (Az, excp, &synth[i_subfr], L_SUBFR,
  906. st->mem_syn, 0);
  907. }
  908. else
  909. {
  910. Overflow = 0; move16 ();
  911. Syn_filt (Az, exc_enhanced, &synth[i_subfr], L_SUBFR,
  912. st->mem_syn, 0);
  913. }
  914. test ();
  915. if (Overflow != 0) /* Test for overflow */
  916. {
  917. for (i = 0; i < PIT_MAX + L_INTERPOL + L_SUBFR; i++)
  918. {
  919. st->old_exc[i] = shr_ex(st->old_exc[i], 2); move16 ();
  920. }
  921. for (i = 0; i < L_SUBFR; i++)
  922. {
  923. exc_enhanced[i] = shr_ex(exc_enhanced[i], 2); move16 ();
  924. }
  925. Syn_filt(Az, exc_enhanced, &synth[i_subfr], L_SUBFR, st->mem_syn, 1);
  926. }
  927. else
  928. {
  929. Copy(&synth[i_subfr+L_SUBFR-M], st->mem_syn, M);
  930. }
  931. /*--------------------------------------------------*
  932. * Update signal for next frame. *
  933. * -> shift to the left by L_SUBFR st->exc[] *
  934. *--------------------------------------------------*/
  935. Copy (&st->old_exc[L_SUBFR], &st->old_exc[0], PIT_MAX + L_INTERPOL);
  936. fwc (); /* function worst case */
  937. /* interpolated LPC parameters for next subframe */
  938. Az += MP1; move16 ();
  939. /* store T0 for next subframe */
  940. st->old_T0 = T0; move16 ();
  941. }
  942. /*-------------------------------------------------------*
  943. * Call the Source Characteristic Detector which updates *
  944. * st->inBackgroundNoise and st->voicedHangover. *
  945. *-------------------------------------------------------*/
  946. move16 (); /* function result */
  947. st->inBackgroundNoise = Bgn_scd(st->background_state,
  948. &(st->ltpGainHistory[0]),
  949. &(synth[0]),
  950. &(st->voicedHangover) );
  951. dtx_dec_activity_update(st->dtxDecoderState,
  952. st->lsfState->past_lsf_q,
  953. synth);
  954. fwc (); /* function worst case */
  955. /* store bfi for next subframe */
  956. st->prev_bf = bfi; move16 ();
  957. st->prev_pdf = pdfi; move16 ();
  958. /*--------------------------------------------------*
  959. * Calculate the LSF averages on the eight *
  960. * previous frames *
  961. *--------------------------------------------------*/
  962. lsp_avg(st->lsp_avg_st, st->lsfState->past_lsf_q);
  963. fwc (); /* function worst case */
  964. the_end:
  965. st->dtxDecoderState->dtxGlobalState = newDTXState; move16();
  966. return 0;
  967. }