cod_amr.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  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 : cod_amr.c
  11. * Purpose : Main encoder routine operating on a frame basis.
  12. *
  13. *****************************************************************************
  14. */
  15. #include "cod_amr.h"
  16. const char cod_amr_id[] = "@(#)$Id $" cod_amr_h;
  17. /*
  18. *****************************************************************************
  19. * INCLUDE FILES
  20. *****************************************************************************
  21. */
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include "typedef.h"
  25. #include "basic_op.h"
  26. #include "count.h"
  27. #include "cnst.h"
  28. #include "copy.h"
  29. #include "set_zero.h"
  30. #include "qua_gain.h"
  31. #include "lpc.h"
  32. #include "lsp.h"
  33. #include "pre_big.h"
  34. #include "ol_ltp.h"
  35. #include "p_ol_wgh.h"
  36. #include "spreproc.h"
  37. #include "cl_ltp.h"
  38. #include "pred_lt.h"
  39. #include "spstproc.h"
  40. #include "cbsearch.h"
  41. #include "gain_q.h"
  42. #include "copy.h"
  43. #include "convolve.h"
  44. #include "ton_stab.h"
  45. #include "vad.h"
  46. #include "dtx_enc.h"
  47. /*
  48. *****************************************************************************
  49. * LOCAL VARIABLES AND TABLES
  50. *****************************************************************************
  51. */
  52. /*
  53. *****************************************************************************
  54. * PUBLIC VARIABLES AND TABLES
  55. *****************************************************************************
  56. */
  57. /* Spectral expansion factors */
  58. static const Word16 gamma1[M] =
  59. {
  60. 30802, 28954, 27217, 25584, 24049,
  61. 22606, 21250, 19975, 18777, 17650
  62. };
  63. /* gamma1 differs for the 12k2 coder */
  64. static const Word16 gamma1_12k2[M] =
  65. {
  66. 29491, 26542, 23888, 21499, 19349,
  67. 17414, 15672, 14105, 12694, 11425
  68. };
  69. static const Word16 gamma2[M] =
  70. {
  71. 19661, 11797, 7078, 4247, 2548,
  72. 1529, 917, 550, 330, 198
  73. };
  74. /*
  75. *****************************************************************************
  76. * PUBLIC PROGRAM CODE
  77. *****************************************************************************
  78. */
  79. /*
  80. **************************************************************************
  81. *
  82. * Function : cod_amr_init
  83. * Purpose : Allocates memory and initializes state variables
  84. *
  85. **************************************************************************
  86. */
  87. int cod_amr_init (cod_amrState **state, Flag dtx)
  88. {
  89. cod_amrState* s;
  90. if (state == (cod_amrState **) NULL){
  91. wfprintf(stderr, "cod_amr_init: invalid parameter\n");
  92. return -1;
  93. }
  94. *state = NULL;
  95. /* allocate memory */
  96. if ((s= (cod_amrState *) wmalloc(sizeof(cod_amrState))) == NULL){
  97. wfprintf(stderr, "cod_amr_init: can not malloc state structure\n");
  98. return -1;
  99. }
  100. s->lpcSt = NULL;
  101. s->lspSt = NULL;
  102. s->clLtpSt = NULL;
  103. s->gainQuantSt = NULL;
  104. s->pitchOLWghtSt = NULL;
  105. s->tonStabSt = NULL;
  106. s->vadSt = NULL;
  107. s->dtx_encSt = NULL;
  108. s->dtx = dtx;
  109. /* Init sub_ex states */
  110. if (cl_ltp_init(&s->clLtpSt) ||
  111. lsp_init(&s->lspSt) ||
  112. gainQuant_init(&s->gainQuantSt) ||
  113. p_ol_wgh_init(&s->pitchOLWghtSt) ||
  114. ton_stab_init(&s->tonStabSt) ||
  115. #ifndef VAD2
  116. vad1_init(&s->vadSt) ||
  117. #else
  118. vad2_init(&s->vadSt) ||
  119. #endif
  120. dtx_enc_init(&s->dtx_encSt) ||
  121. lpc_init(&s->lpcSt)) {
  122. cod_amr_exit(&s);
  123. return -1;
  124. }
  125. cod_amr_reset(s);
  126. *state = s;
  127. return 0;
  128. }
  129. /*
  130. **************************************************************************
  131. *
  132. * Function : cod_amr_reset
  133. * Purpose : Resets state memory
  134. *
  135. **************************************************************************
  136. */
  137. int cod_amr_reset (cod_amrState *st)
  138. {
  139. Word16 i;
  140. if (st == (cod_amrState *) NULL){
  141. wfprintf(stderr, "cod_amr_reset: invalid parameter\n");
  142. return -1;
  143. }
  144. /*-----------------------------------------------------------------------*
  145. * Initialize pointers to speech vector. *
  146. *-----------------------------------------------------------------------*/
  147. st->new_speech = st->old_speech + L_TOTAL - L_FRAME; /* New speech */
  148. st->speech = st->new_speech - L_NEXT; /* Present frame */
  149. st->p_window = st->old_speech + L_TOTAL - L_WINDOW; /* For LPC window */
  150. st->p_window_12k2 = st->p_window - L_NEXT; /* EFR LPC window: no lookahead */
  151. /* Initialize static pointers */
  152. st->wsp = st->old_wsp + PIT_MAX;
  153. st->exc = st->old_exc + PIT_MAX + L_INTERPOL;
  154. st->zero = st->ai_zero + MP1;
  155. st->error = st->mem_err + M;
  156. st->h1 = &st->hvec[L_SUBFR];
  157. /* Static vectors to zero */
  158. Set_zero(st->old_speech, L_TOTAL);
  159. Set_zero(st->old_exc, PIT_MAX + L_INTERPOL);
  160. Set_zero(st->old_wsp, PIT_MAX);
  161. Set_zero(st->mem_syn, M);
  162. Set_zero(st->mem_w, M);
  163. Set_zero(st->mem_w0, M);
  164. Set_zero(st->mem_err, M);
  165. Set_zero(st->zero, L_SUBFR);
  166. Set_zero(st->hvec, L_SUBFR); /* set to zero "h1[-L_SUBFR..-1]" */
  167. /* OL LTP states */
  168. for (i = 0; i < 5; i++)
  169. {
  170. st->old_lags[i] = 40;
  171. }
  172. /* Reset lpc states */
  173. lpc_reset(st->lpcSt);
  174. /* Reset lsp states */
  175. lsp_reset(st->lspSt);
  176. /* Reset clLtp states */
  177. cl_ltp_reset(st->clLtpSt);
  178. gainQuant_reset(st->gainQuantSt);
  179. p_ol_wgh_reset(st->pitchOLWghtSt);
  180. ton_stab_reset(st->tonStabSt);
  181. #ifndef VAD2
  182. vad1_reset(st->vadSt);
  183. #else
  184. vad2_reset(st->vadSt);
  185. #endif
  186. dtx_enc_reset(st->dtx_encSt);
  187. st->sharp = SHARPMIN;
  188. return 0;
  189. }
  190. /*
  191. **************************************************************************
  192. *
  193. * Function : cod_amr_exit
  194. * Purpose : The memory used for state memory is freed
  195. *
  196. **************************************************************************
  197. */
  198. void cod_amr_exit (cod_amrState **state)
  199. {
  200. if (state == NULL || *state == NULL)
  201. return;
  202. /* dealloc members */
  203. lpc_exit(&(*state)->lpcSt);
  204. lsp_exit(&(*state)->lspSt);
  205. gainQuant_exit(&(*state)->gainQuantSt);
  206. cl_ltp_exit(&(*state)->clLtpSt);
  207. p_ol_wgh_exit(&(*state)->pitchOLWghtSt);
  208. ton_stab_exit(&(*state)->tonStabSt);
  209. #ifndef VAD2
  210. vad1_exit(&(*state)->vadSt);
  211. #else
  212. vad2_exit(&(*state)->vadSt);
  213. #endif
  214. dtx_enc_exit(&(*state)->dtx_encSt);
  215. /* deallocate memory */
  216. wfree(*state);
  217. *state = NULL;
  218. return;
  219. }
  220. /***************************************************************************
  221. * FUNCTION: cod_amr_first
  222. *
  223. * PURPOSE: Copes with look-ahead.
  224. *
  225. * INPUTS:
  226. * No input argument are passed to this function. However, before
  227. * calling this function, 40 new speech data should be copied to the
  228. * vector new_speech[]. This is a global pointer which is declared in
  229. * this file (it points to the end of speech buffer minus 200).
  230. *
  231. ***************************************************************************/
  232. int cod_amr_first(cod_amrState *st, /* i/o : State struct */
  233. Word16 new_speech[]) /* i : speech input (L_FRAME) */
  234. {
  235. Copy(new_speech,&st->new_speech[-L_NEXT], L_NEXT);
  236. /* Copy(new_speech,st->new_speech,L_FRAME); */
  237. return 0;
  238. }
  239. /***************************************************************************
  240. * FUNCTION: cod_amr
  241. *
  242. * PURPOSE: Main encoder routine.
  243. *
  244. * DESCRIPTION: This function is called every 20 ms speech frame,
  245. * operating on the newly read 160 speech samples. It performs the
  246. * principle encoding functions to produce the set of encoded parameters
  247. * which include the LSP, adaptive codebook, and fixed codebook
  248. * quantization indices (addresses and gains).
  249. *
  250. * INPUTS:
  251. * No input argument are passed to this function. However, before
  252. * calling this function, 160 new speech data should be copied to the
  253. * vector new_speech[]. This is a global pointer which is declared in
  254. * this file (it points to the end of speech buffer minus 160).
  255. *
  256. * OUTPUTS:
  257. *
  258. * ana[]: vector of analysis parameters.
  259. * synth[]: Local synthesis speech (for debugging purposes)
  260. *
  261. ***************************************************************************/
  262. int cod_amr(
  263. cod_amrState *st, /* i/o : State struct */
  264. enum Mode mode, /* i : AMR mode */
  265. Word16 new_speech[], /* i : speech input (L_FRAME) */
  266. Word16 ana[], /* o : Analysis parameters */
  267. enum Mode *usedMode, /* o : used mode */
  268. Word16 synth[] /* o : Local synthesis */
  269. )
  270. {
  271. /* LPC coefficients */
  272. Word16 A_t[(MP1) * 4]; /* A(z) unquantized for the 4 subframes */
  273. Word16 Aq_t[(MP1) * 4]; /* A(z) quantized for the 4 subframes */
  274. Word16 *A, *Aq; /* Pointer on A_t and Aq_t */
  275. Word16 lsp_new[M];
  276. /* Other vectors */
  277. Word16 xn[L_SUBFR]; /* Target vector for pitch search */
  278. Word16 xn2[L_SUBFR]; /* Target vector for codebook search */
  279. Word16 code[L_SUBFR]; /* Fixed codebook excitation */
  280. Word16 y1[L_SUBFR]; /* Filtered adaptive excitation */
  281. Word16 y2[L_SUBFR]; /* Filtered fixed codebook excitation */
  282. Word16 gCoeff[6]; /* Correlations between xn, y1, & y2: */
  283. Word16 res[L_SUBFR]; /* Short term (LPC) prediction residual */
  284. Word16 res2[L_SUBFR]; /* Long term (LTP) prediction residual */
  285. /* Vector and scalars needed for the MR475 */
  286. Word16 xn_sf0[L_SUBFR]; /* Target vector for pitch search */
  287. Word16 y2_sf0[L_SUBFR]; /* Filtered codebook innovation */
  288. Word16 code_sf0[L_SUBFR]; /* Fixed codebook excitation */
  289. Word16 h1_sf0[L_SUBFR]; /* The impulse response of sf0 */
  290. Word16 mem_syn_save[M]; /* Filter memory */
  291. Word16 mem_w0_save[M]; /* Filter memory */
  292. Word16 mem_err_save[M]; /* Filter memory */
  293. Word16 sharp_save; /* Sharpening */
  294. Word16 evenSubfr; /* Even subframe indicator */
  295. Word16 T0_sf0 = 0; /* Integer pitch lag of sf0 */
  296. Word16 T0_frac_sf0 = 0; /* Fractional pitch lag of sf0 */
  297. Word16 i_subfr_sf0 = 0; /* Position in exc[] for sf0 */
  298. Word16 gain_pit_sf0; /* Quantized pitch gain for sf0 */
  299. Word16 gain_code_sf0; /* Quantized codebook gain for sf0 */
  300. /* Scalars */
  301. Word16 i_subfr, subfrNr;
  302. Word16 T_op[L_FRAME/L_FRAME_BY2];
  303. Word16 T0, T0_frac;
  304. Word16 gain_pit, gain_code;
  305. /* Flags */
  306. Word16 lsp_flag = 0; /* indicates resonance in LPC filter */
  307. Word16 gp_limit; /* pitch gain limit value */
  308. Word16 vad_flag; /* VAD decision flag */
  309. Word16 compute_sid_flag; /* SID analysis flag */
  310. Copy(new_speech, st->new_speech, L_FRAME);
  311. *usedMode = mode; move16 ();
  312. /* DTX processing */
  313. if (st->dtx)
  314. { /* no test() call since this if is only in simulation env */
  315. /* Find VAD decision */
  316. #ifdef VAD2
  317. vad_flag = vad2 (st->new_speech, st->vadSt);
  318. vad_flag = vad2 (st->new_speech+80, st->vadSt) || vad_flag; logic16();
  319. #else
  320. vad_flag = vad1(st->vadSt, st->new_speech);
  321. #endif
  322. fwc (); /* function worst case */
  323. /* NB! usedMode may change here */
  324. compute_sid_flag = tx_dtx_handler(st->dtx_encSt,
  325. vad_flag,
  326. usedMode);
  327. }
  328. else
  329. {
  330. compute_sid_flag = 0; move16 ();
  331. }
  332. /*------------------------------------------------------------------------*
  333. * - Perform LPC analysis: *
  334. * * autocorrelation + lag windowing *
  335. * * Levinson-durbin algorithm to find a[] *
  336. * * convert a[] to lsp[] *
  337. * * quantize and code the LSPs *
  338. * * find the interpolated LSPs and convert to a[] for all *
  339. * subframes (both quantized and unquantized) *
  340. *------------------------------------------------------------------------*/
  341. /* LP analysis */
  342. lpc(st->lpcSt, mode, st->p_window, st->p_window_12k2, A_t);
  343. fwc (); /* function worst case */
  344. /* From A(z) to lsp. LSP quantization and interpolation */
  345. lsp(st->lspSt, mode, *usedMode, A_t, Aq_t, lsp_new, &ana);
  346. fwc (); /* function worst case */
  347. /* Buffer lsp's and energy */
  348. dtx_buffer(st->dtx_encSt,
  349. lsp_new,
  350. st->new_speech);
  351. /* Check if in DTX mode */
  352. test();
  353. if (sub_ex(*usedMode, MRDTX) == 0)
  354. {
  355. dtx_enc(st->dtx_encSt,
  356. compute_sid_flag,
  357. st->lspSt->qSt,
  358. st->gainQuantSt->gc_predSt,
  359. &ana);
  360. Set_zero(st->old_exc, PIT_MAX + L_INTERPOL);
  361. Set_zero(st->mem_w0, M);
  362. Set_zero(st->mem_err, M);
  363. Set_zero(st->zero, L_SUBFR);
  364. Set_zero(st->hvec, L_SUBFR); /* set to zero "h1[-L_SUBFR..-1]" */
  365. /* Reset lsp states */
  366. lsp_reset(st->lspSt);
  367. Copy(lsp_new, st->lspSt->lsp_old, M);
  368. Copy(lsp_new, st->lspSt->lsp_old_q, M);
  369. /* Reset clLtp states */
  370. cl_ltp_reset(st->clLtpSt);
  371. st->sharp = SHARPMIN; move16 ();
  372. }
  373. else
  374. {
  375. /* check resonance in the filter */
  376. lsp_flag = check_lsp(st->tonStabSt, st->lspSt->lsp_old); move16 ();
  377. }
  378. /*----------------------------------------------------------------------*
  379. * - Find the weighted input speech w_sp[] for the whole speech frame *
  380. * - Find the open-loop pitch delay for first 2 subframes *
  381. * - Set the range for searching closed-loop pitch in 1st subframe *
  382. * - Find the open-loop pitch delay for last 2 subframes *
  383. *----------------------------------------------------------------------*/
  384. #ifdef VAD2
  385. if (st->dtx)
  386. { /* no test() call since this if is only in simulation env */
  387. st->vadSt->L_Rmax = 0; move32 ();
  388. st->vadSt->L_R0 = 0; move32 ();
  389. }
  390. #endif
  391. for(subfrNr = 0, i_subfr = 0;
  392. subfrNr < L_FRAME/L_FRAME_BY2;
  393. subfrNr++, i_subfr += L_FRAME_BY2)
  394. {
  395. /* Pre-processing on 80 samples */
  396. pre_big(mode, gamma1, gamma1_12k2, gamma2, A_t, i_subfr, st->speech,
  397. st->mem_w, st->wsp);
  398. test (); test ();
  399. if ((sub_ex(mode, MR475) != 0) && (sub_ex(mode, MR515) != 0))
  400. {
  401. /* Find open loop pitch lag for two subframes */
  402. ol_ltp(st->pitchOLWghtSt, st->vadSt, mode, &st->wsp[i_subfr],
  403. &T_op[subfrNr], st->old_lags, st->ol_gain_flg, subfrNr,
  404. st->dtx);
  405. }
  406. }
  407. fwc (); /* function worst case */
  408. test (); test();
  409. if ((sub_ex(mode, MR475) == 0) || (sub_ex(mode, MR515) == 0))
  410. {
  411. /* Find open loop pitch lag for ONE FRAME ONLY */
  412. /* search on 160 samples */
  413. ol_ltp(st->pitchOLWghtSt, st->vadSt, mode, &st->wsp[0], &T_op[0],
  414. st->old_lags, st->ol_gain_flg, 1, st->dtx);
  415. T_op[1] = T_op[0]; move16 ();
  416. }
  417. fwc (); /* function worst case */
  418. #ifdef VAD2
  419. if (st->dtx)
  420. { /* no test() call since this if is only in simulation env */
  421. LTP_flag_update(st->vadSt, mode);
  422. }
  423. #endif
  424. #ifndef VAD2
  425. /* run VAD pitch detection */
  426. if (st->dtx)
  427. { /* no test() call since this if is only in simulation env */
  428. vad_pitch_detection(st->vadSt, T_op);
  429. }
  430. #endif
  431. fwc (); /* function worst case */
  432. if (sub_ex(*usedMode, MRDTX) == 0)
  433. {
  434. goto the_end;
  435. }
  436. /*------------------------------------------------------------------------*
  437. * Loop for every subframe in the analysis frame *
  438. *------------------------------------------------------------------------*
  439. * To find the pitch and innovation parameters. The subframe size is *
  440. * L_SUBFR and the loop is repeated L_FRAME/L_SUBFR times. *
  441. * - find the weighted LPC coefficients *
  442. * - find the LPC residual signal res[] *
  443. * - compute the target signal for pitch search *
  444. * - compute impulse response of weighted synthesis filter (h1[]) *
  445. * - find the closed-loop pitch parameters *
  446. * - encode the pitch dealy *
  447. * - update the impulse response h1[] by including fixed-gain pitch *
  448. * - find target vector for codebook search *
  449. * - codebook search *
  450. * - encode codebook address *
  451. * - VQ of pitch and codebook gains *
  452. * - find synthesis speech *
  453. * - update states of weighting filter *
  454. *------------------------------------------------------------------------*/
  455. A = A_t; /* pointer to interpolated LPC parameters */
  456. Aq = Aq_t; /* pointer to interpolated quantized LPC parameters */
  457. evenSubfr = 0; move16 ();
  458. subfrNr = -1; move16 ();
  459. for (i_subfr = 0; i_subfr < L_FRAME; i_subfr += L_SUBFR)
  460. {
  461. subfrNr = add_ex(subfrNr, 1);
  462. evenSubfr = sub_ex(1, evenSubfr);
  463. /* Save states for the MR475 mode */
  464. test(); test();
  465. if ((evenSubfr != 0) && (sub_ex(*usedMode, MR475) == 0))
  466. {
  467. Copy(st->mem_syn, mem_syn_save, M);
  468. Copy(st->mem_w0, mem_w0_save, M);
  469. Copy(st->mem_err, mem_err_save, M);
  470. sharp_save = st->sharp;
  471. }
  472. /*-----------------------------------------------------------------*
  473. * - Preprocessing of subframe *
  474. *-----------------------------------------------------------------*/
  475. test();
  476. if (sub_ex(*usedMode, MR475) != 0)
  477. {
  478. subframePreProc(*usedMode, gamma1, gamma1_12k2,
  479. gamma2, A, Aq, &st->speech[i_subfr],
  480. st->mem_err, st->mem_w0, st->zero,
  481. st->ai_zero, &st->exc[i_subfr],
  482. st->h1, xn, res, st->error);
  483. }
  484. else
  485. { /* MR475 */
  486. subframePreProc(*usedMode, gamma1, gamma1_12k2,
  487. gamma2, A, Aq, &st->speech[i_subfr],
  488. st->mem_err, mem_w0_save, st->zero,
  489. st->ai_zero, &st->exc[i_subfr],
  490. st->h1, xn, res, st->error);
  491. /* save impulse response (modified in cbsearch) */
  492. test ();
  493. if (evenSubfr != 0)
  494. {
  495. Copy (st->h1, h1_sf0, L_SUBFR);
  496. }
  497. }
  498. /* copy the LP residual (res2 is modified in the CL LTP search) */
  499. Copy (res, res2, L_SUBFR);
  500. fwc (); /* function worst case */
  501. /*-----------------------------------------------------------------*
  502. * - Closed-loop LTP search *
  503. *-----------------------------------------------------------------*/
  504. cl_ltp(st->clLtpSt, st->tonStabSt, *usedMode, i_subfr, T_op, st->h1,
  505. &st->exc[i_subfr], res2, xn, lsp_flag, xn2, y1,
  506. &T0, &T0_frac, &gain_pit, gCoeff, &ana,
  507. &gp_limit);
  508. /* update LTP lag history */
  509. move16 (); test(); test ();
  510. if ((subfrNr == 0) && (st->ol_gain_flg[0] > 0))
  511. {
  512. st->old_lags[1] = T0; move16 ();
  513. }
  514. move16 (); test(); test ();
  515. if ((sub_ex(subfrNr, 3) == 0) && (st->ol_gain_flg[1] > 0))
  516. {
  517. st->old_lags[0] = T0; move16 ();
  518. }
  519. fwc (); /* function worst case */
  520. /*-----------------------------------------------------------------*
  521. * - Inovative codebook search (find index and gain) *
  522. *-----------------------------------------------------------------*/
  523. cbsearch(xn2, st->h1, T0, st->sharp, gain_pit, res2,
  524. code, y2, &ana, *usedMode, subfrNr);
  525. fwc (); /* function worst case */
  526. /*------------------------------------------------------*
  527. * - Quantization of gains. *
  528. *------------------------------------------------------*/
  529. gainQuant(st->gainQuantSt, *usedMode, res, &st->exc[i_subfr], code,
  530. xn, xn2, y1, y2, gCoeff, evenSubfr, gp_limit,
  531. &gain_pit_sf0, &gain_code_sf0,
  532. &gain_pit, &gain_code, &ana);
  533. fwc (); /* function worst case */
  534. /* update gain history */
  535. update_gp_clipping(st->tonStabSt, gain_pit);
  536. test();
  537. if (sub_ex(*usedMode, MR475) != 0)
  538. {
  539. /* Subframe Post Porcessing */
  540. subframePostProc(st->speech, *usedMode, i_subfr, gain_pit,
  541. gain_code, Aq, synth, xn, code, y1, y2, st->mem_syn,
  542. st->mem_err, st->mem_w0, st->exc, &st->sharp);
  543. }
  544. else
  545. {
  546. test();
  547. if (evenSubfr != 0)
  548. {
  549. i_subfr_sf0 = i_subfr; move16 ();
  550. Copy(xn, xn_sf0, L_SUBFR);
  551. Copy(y2, y2_sf0, L_SUBFR);
  552. Copy(code, code_sf0, L_SUBFR);
  553. T0_sf0 = T0; move16 ();
  554. T0_frac_sf0 = T0_frac; move16 ();
  555. /* Subframe Post Porcessing */
  556. subframePostProc(st->speech, *usedMode, i_subfr, gain_pit,
  557. gain_code, Aq, synth, xn, code, y1, y2,
  558. mem_syn_save, st->mem_err, mem_w0_save,
  559. st->exc, &st->sharp);
  560. st->sharp = sharp_save; move16();
  561. }
  562. else
  563. {
  564. /* update both subframes for the MR475 */
  565. /* Restore states for the MR475 mode */
  566. Copy(mem_err_save, st->mem_err, M);
  567. /* re-build excitation for sf 0 */
  568. Pred_lt_3or6(&st->exc[i_subfr_sf0], T0_sf0, T0_frac_sf0,
  569. L_SUBFR, 1);
  570. Convolve(&st->exc[i_subfr_sf0], h1_sf0, y1, L_SUBFR);
  571. Aq -= MP1;
  572. subframePostProc(st->speech, *usedMode, i_subfr_sf0,
  573. gain_pit_sf0, gain_code_sf0, Aq,
  574. synth, xn_sf0, code_sf0, y1, y2_sf0,
  575. st->mem_syn, st->mem_err, st->mem_w0, st->exc,
  576. &sharp_save); /* overwrites sharp_save */
  577. Aq += MP1;
  578. /* re-run pre-processing to get xn right (needed by postproc) */
  579. /* (this also reconstructs the unsharpened h1 for sf 1) */
  580. subframePreProc(*usedMode, gamma1, gamma1_12k2,
  581. gamma2, A, Aq, &st->speech[i_subfr],
  582. st->mem_err, st->mem_w0, st->zero,
  583. st->ai_zero, &st->exc[i_subfr],
  584. st->h1, xn, res, st->error);
  585. /* re-build excitation sf 1 (changed if lag < L_SUBFR) */
  586. Pred_lt_3or6(&st->exc[i_subfr], T0, T0_frac, L_SUBFR, 1);
  587. Convolve(&st->exc[i_subfr], st->h1, y1, L_SUBFR);
  588. subframePostProc(st->speech, *usedMode, i_subfr, gain_pit,
  589. gain_code, Aq, synth, xn, code, y1, y2,
  590. st->mem_syn, st->mem_err, st->mem_w0,
  591. st->exc, &st->sharp);
  592. }
  593. }
  594. fwc (); /* function worst case */
  595. A += MP1; /* interpolated LPC parameters for next subframe */
  596. Aq += MP1;
  597. }
  598. Copy(&st->old_exc[L_FRAME], &st->old_exc[0], PIT_MAX + L_INTERPOL);
  599. the_end:
  600. /*--------------------------------------------------*
  601. * Update signal for next frame. *
  602. *--------------------------------------------------*/
  603. Copy(&st->old_wsp[L_FRAME], &st->old_wsp[0], PIT_MAX);
  604. Copy(&st->old_speech[L_FRAME], &st->old_speech[0], L_TOTAL - L_FRAME);
  605. fwc (); /* function worst case */
  606. return 0;
  607. }