summaryrefslogtreecommitdiff
path: root/audio_codec/libamr/dec_main.c (plain)
blob: ad6718620b836f6a61a93f5e17f702c7ba6f003b
1/*
2 *===================================================================
3 * 3GPP AMR Wideband Floating-point Speech Codec
4 *===================================================================
5 */
6#include <stdlib.h>
7#include <math.h>
8#include <memory.h>
9#include <string.h>
10#include "typedef.h"
11#include "dec_main.h"
12#include "dec_dtx.h"
13#include "dec_acelp.h"
14#include "dec_gain.h"
15#include "dec_lpc.h"
16#include "dec_util.h"
17
18
19#define MAX_16 (Word16)0x7fff
20#define MIN_16 (Word16)0x8000
21
22#define L_FRAME 256 /* Frame size */
23#define NB_SUBFR 4 /* Number of subframe per frame */
24#define L_SUBFR 64 /* Subframe size */
25#define MODE_7k 0 /* modes */
26#define MODE_9k 1
27#define MODE_12k 2
28#define MODE_14k 3
29#define MODE_16k 4
30#define MODE_18k 5
31#define MODE_20k 6
32#define MODE_23k 7
33#define MODE_24k 8
34#define RX_SPEECH_PROBABLY_DEGRADED 1 /* rx types */
35#define RX_SPEECH_LOST 2
36#define RX_SPEECH_BAD 3
37#define RX_NO_DATA 7
38#define Q_MAX 8 /* scaling max for signal */
39#define PIT_SHARP 27853 /* pitch sharpening factor = 0.85 Q15 */
40#define PIT_MIN 34 /* Minimum pitch lag with resolution 1/4 */
41#define PIT_FR2 128 /* Minimum pitch lag with resolution 1/2 */
42#define PIT_FR1_9b 160 /* Minimum pitch lag with resolution 1 */
43#define PIT_FR1_8b 92 /* Minimum pitch lag with resolution 1 */
44
45extern const Word16 D_ROM_isp[];
46extern const Word16 D_ROM_isf[];
47extern const Word16 D_ROM_interpol_frac[];
48
49#ifdef WIN32
50#pragma warning( disable : 4310)
51#endif
52
53/*
54 * Decoder_reset
55 *
56 * Parameters:
57 * st I/O: pointer to state structure
58 * reset_all I: perform full reset
59 *
60 * Function:
61 * Initialisation of variables for the decoder section.
62 *
63 *
64 * Returns:
65 * void
66 */
67void D_MAIN_reset(void *st, Word16 reset_all)
68{
69 Word32 i;
70
71 Decoder_State *dec_state;
72
73 dec_state = (Decoder_State*)st;
74 memset(dec_state->mem_exc, 0, (PIT_MAX + L_INTERPOL) * sizeof(Word16));
75 memset(dec_state->mem_isf_q, 0, M * sizeof(Word16));
76 dec_state->mem_T0_frac = 0; /* old pitch value = 64.0 */
77 dec_state->mem_T0 = 64;
78 dec_state->mem_first_frame = 1;
79 dec_state->mem_gc_thres = 0;
80 dec_state->mem_tilt_code = 0;
81 memset(dec_state->mem_ph_disp, 0, 8 * sizeof(Word16));
82
83 /* scaling memories for excitation */
84 dec_state->mem_q = Q_MAX;
85 dec_state->mem_subfr_q[3] = Q_MAX;
86 dec_state->mem_subfr_q[2] = Q_MAX;
87 dec_state->mem_subfr_q[1] = Q_MAX;
88 dec_state->mem_subfr_q[0] = Q_MAX;
89
90 if (reset_all != 0) {
91 /* routines initialization */
92 D_GAIN_init(dec_state->mem_gain);
93 memset(dec_state->mem_oversamp, 0, (2 * 12) * sizeof(Word16));
94 memset(dec_state->mem_sig_out, 0, 6 * sizeof(Word16));
95 memset(dec_state->mem_hf, 0, (31 - 1) * sizeof(Word16));
96 memset(dec_state->mem_hf3, 0, (31 - 1) * sizeof(Word16));
97 memset(dec_state->mem_hp400, 0, 6 * sizeof(Word16));
98 D_GAIN_lag_concealment_init(dec_state->mem_lag);
99
100 /* isp initialization */
101 memcpy(dec_state->mem_isp, D_ROM_isp, M * sizeof(Word16));
102 memcpy(dec_state->mem_isf, D_ROM_isf, M * sizeof(Word16));
103
104 for (i = 0; i < L_MEANBUF; i++) {
105 memcpy(&dec_state->mem_isf_buf[i * M], D_ROM_isf, M * sizeof(Word16));
106 }
107
108 /* variable initialization */
109 dec_state->mem_deemph = 0;
110 dec_state->mem_seed = 21845; /* init random with 21845 */
111 dec_state->mem_seed2 = 21845;
112 dec_state->mem_seed3 = 21845;
113 dec_state->mem_state = 0;
114 dec_state->mem_bfi = 0;
115
116 /* Static vectors to zero */
117 memset(dec_state->mem_syn_hf, 0, M16k * sizeof(Word16));
118 memset(dec_state->mem_syn_hi, 0, M * sizeof(Word16));
119 memset(dec_state->mem_syn_lo, 0, M * sizeof(Word16));
120 D_DTX_reset(dec_state->dtx_decSt, D_ROM_isf);
121 dec_state->mem_vad_hist = 0;
122 }
123
124 return;
125}
126
127
128/*
129 * Decoder_init
130 *
131 * Parameters:
132 * spd_state O: pointer to state structure
133 *
134 * Function:
135 * Initialization of variables for the decoder section.
136 * Memory allocation.
137 *
138 * Returns:
139 * return zero if succesful
140 */
141Word32 D_MAIN_init(void **spd_state)
142{
143 /* Decoder states */
144 Decoder_State *st;
145
146 *spd_state = NULL;
147
148 /*
149 * Memory allocation for coder state.
150 */
151 if ((st = (Decoder_State*)malloc(sizeof(Decoder_State))) == NULL) {
152 return(-1);
153 }
154
155 st->dtx_decSt = NULL;
156 D_DTX_init(&st->dtx_decSt, D_ROM_isf);
157 D_MAIN_reset((void *)st, 1);
158 *spd_state = (void *)st;
159
160 return(0);
161}
162
163
164/*
165 * Decoder_close
166 *
167 * Parameters:
168 * spd_state I: pointer to state structure
169 *
170 * Function:
171 * Free coder memory.
172 *
173 * Returns:
174 * void
175 */
176void D_MAIN_close(void **spd_state)
177{
178 D_DTX_exit(&(((Decoder_State *)(*spd_state))->dtx_decSt));
179 free(*spd_state);
180
181 return;
182}
183
184
185/*
186 * Decoder_exe
187 *
188 * Parameters:
189 * mode I: used mode
190 * prms I: parameter vector
191 * synth_out O: synthesis speech
192 * spe_state B: state structure
193 * frame_type I: received frame type
194 *
195 * Function:
196 * Main decoder routine.
197 *
198 * Returns:
199 * 0 if successful
200 */
201Word32 D_MAIN_decode(Word16 mode, Word16 prms[], Word16 synth16k[],
202 void *spd_state, UWord8 frame_type)
203{
204
205 Word32 code2[L_SUBFR]; /* algebraic codevector */
206 Word32 L_tmp, L_tmp2, L_gain_code, L_stab_fac;
207 Word32 i, j, i_subfr, pit_flag;
208 Word32 T0, T0_frac, T0_max, select, T0_min = 0;
209
210 Word16 exc2[L_FRAME]; /* excitation vector */
211 Word16 Aq[NB_SUBFR * (M + 1)]; /* A(z) quantized for the 4 subframes */
212 Word16 code[L_SUBFR]; /* algebraic codevector */
213 Word16 excp[L_SUBFR]; /* excitation vector */
214 Word16 HfIsf[M16k];
215 Word16 ispnew[M]; /* immittance spectral pairs at 4nd sfr*/
216 Word16 isf[M]; /* ISF (frequency domain) at 4nd sfr */
217 Word16 isf_tmp[M]; /* ISF tmp */
218 Word16 ind[8]; /* quantization indices */
219
220 Word16 index, fac, voice_fac, max, Q_new = 0;
221 Word16 gain_pit, gain_code, gain_code_lo, tmp;
222 Word16 corr_gain = 0;
223 UWord16 pit_sharp = 0;
224
225 Word16 *exc; /* Excitation vector */
226 Word16 *p_Aq; /* ptr to A(z) for the 4 subframes */
227 Word16 *p_isf; /* prt to isf */
228
229 Decoder_State *st; /* Decoder states */
230 UWord8 newDTXState, bfi, unusable_frame;
231 UWord8 vad_flag;
232
233 st = (Decoder_State*)spd_state;
234
235 /* find the new DTX state SPEECH OR DTX */
236 newDTXState = D_DTX_rx_handler(st->dtx_decSt, frame_type);
237
238 if (newDTXState != SPEECH) {
239 D_DTX_exe(st->dtx_decSt, exc2, newDTXState, isf, &prms);
240 }
241
242 /* SPEECH action state machine */
243 if ((frame_type == RX_SPEECH_BAD) |
244 (frame_type == RX_SPEECH_PROBABLY_DEGRADED)) {
245 /* bfi for all index, bits are not usable */
246 bfi = 1;
247 unusable_frame = 0;
248 } else if ((frame_type == RX_NO_DATA) | (frame_type == RX_SPEECH_LOST)) {
249 /* bfi only for lsf, gains and pitch period */
250 bfi = 1;
251 unusable_frame = 1;
252 } else {
253 bfi = 0;
254 unusable_frame = 0;
255 }
256
257 if (bfi != 0) {
258 st->mem_state = (UWord8)(st->mem_state + 1);
259
260 if (st->mem_state > 6) {
261 st->mem_state = 6;
262 }
263 } else {
264 st->mem_state = (UWord8)(st->mem_state >> 1);
265 }
266
267 /*
268 * If this frame is the first speech frame after CNI period,
269 * set the BFH state machine to an appropriate state depending
270 * on whether there was DTX muting before start of speech or not
271 * If there was DTX muting, the first speech frame is muted.
272 * If there was no DTX muting, the first speech frame is not
273 * muted. The BFH state machine starts from state 5, however, to
274 * keep the audible noise resulting from a SID frame which is
275 * erroneously interpreted as a good speech frame as small as
276 * possible (the decoder output in this case is quickly muted)
277 */
278
279 if (st->dtx_decSt->mem_dtx_global_state == DTX) {
280 st->mem_state = 5;
281 st->mem_bfi = 0;
282 } else if (st->dtx_decSt->mem_dtx_global_state == D_DTX_MUTE) {
283 st->mem_state = 5;
284 st->mem_bfi = 1;
285 }
286
287 if (newDTXState == SPEECH) {
288 vad_flag = (UWord8)(*prms++);
289
290 if (bfi == 0) {
291 if (vad_flag == 0) {
292 st->mem_vad_hist = (Word16)(st->mem_vad_hist + 1);
293
294 if (st->mem_vad_hist > 32767) {
295 st->mem_vad_hist = 32767;
296 }
297 } else {
298 st->mem_vad_hist = 0;
299 }
300 }
301 }
302
303 /*
304 * DTX-CNG
305 */
306 if (newDTXState != SPEECH) { /* CNG mode */
307 /*
308 * increase slightly energy of noise below 200 Hz
309 * Convert ISFs to the cosine domain
310 */
311 D_LPC_isf_isp_conversion(isf, ispnew, M);
312 D_LPC_isp_a_conversion(ispnew, Aq, 1, M);
313 memcpy(isf_tmp, st->mem_isf, M * sizeof(Word16));
314
315 for (i_subfr = 0; i_subfr < L_FRAME; i_subfr += L_SUBFR) {
316 j = (i_subfr >> 6);
317
318 for (i = 0; i < M; i++) {
319 L_tmp = (isf_tmp[i] * (32767 - D_ROM_interpol_frac[j])) << 1;
320 L_tmp = L_tmp + ((isf[i] * D_ROM_interpol_frac[j]) << 1);
321 HfIsf[i] = (Word16)((L_tmp + 0x8000) >> 16);
322 }
323
324 D_UTIL_dec_synthesis(Aq, &exc2[i_subfr], 0, &synth16k[i_subfr * 5 / 4],
325 (Word16) 1, HfIsf, mode, newDTXState, bfi, st);
326 }
327
328 /* reset speech coder memories */
329 D_MAIN_reset(st, 0);
330 memcpy(st->mem_isf, isf, M * sizeof(Word16));
331 st->mem_bfi = bfi;
332 st->dtx_decSt->mem_dtx_global_state = (UWord8)newDTXState;
333
334 return(0);
335 }
336
337 /*
338 * ACELP
339 */
340
341 exc = st->mem_exc + PIT_MAX + L_INTERPOL;
342
343 /* Decode the ISFs */
344 if (mode <= MODE_7k) {
345 ind[0] = *prms++;
346 ind[1] = *prms++;
347 ind[2] = *prms++;
348 ind[3] = *prms++;
349 ind[4] = *prms++;
350 D_LPC_isf_2s3s_decode(ind, isf, st->mem_isf_q, st->mem_isf,
351 st->mem_isf_buf, bfi);
352 } else {
353 ind[0] = *prms++;
354 ind[1] = *prms++;
355 ind[2] = *prms++;
356 ind[3] = *prms++;
357 ind[4] = *prms++;
358 ind[5] = *prms++;
359 ind[6] = *prms++;
360 D_LPC_isf_2s5s_decode(ind, isf, st->mem_isf_q, st->mem_isf,
361 st->mem_isf_buf, bfi);
362 }
363
364 /* Convert ISFs to the cosine domain */
365 D_LPC_isf_isp_conversion(isf, ispnew, M);
366
367 if (st->mem_first_frame != 0) {
368 st->mem_first_frame = 0;
369 memcpy(st->mem_isp, ispnew, M * sizeof(Word16));
370 }
371
372 /* Find the interpolated ISPs and convert to a[] for all subframes */
373 D_LPC_int_isp_find(st->mem_isp, ispnew, D_ROM_interpol_frac, Aq);
374
375 /* update isp memory for the next frame */
376 memcpy(st->mem_isp, ispnew, M * sizeof(Word16));
377
378 /* Check stability on isf : distance between old isf and current isf */
379 L_tmp = 0;
380 p_isf = st->mem_isf;
381
382 for (i = 0; i < M - 1; i++) {
383 tmp = (Word16)((isf[i] - p_isf[i]));
384 L_tmp = L_tmp + (tmp * tmp);
385 }
386
387 if (L_tmp < 3276928) {
388 L_tmp = L_tmp >> 7;
389 L_tmp = (L_tmp * 26214) >> 15; /* tmp = L_tmp*0.8/256 */
390 L_tmp = 20480 - L_tmp; /* 1.25 - tmp */
391 L_stab_fac = L_tmp << 1; /* Q14 -> Q15 with saturation */
392
393 if (L_stab_fac > 0x7FFF) {
394 L_stab_fac = 0x7FFF;
395 }
396 } else {
397 L_stab_fac = 0x0;
398 }
399
400 memcpy(isf_tmp, st->mem_isf, M * sizeof(Word16));
401 memcpy(st->mem_isf, isf, M * sizeof(Word16));
402
403 /*
404 * Loop for every subframe in the analysis frame
405 *
406 * The subframe size is L_SUBFR and the loop is repeated L_FRAME/L_SUBFR
407 * times
408 * - decode the pitch delay and filter mode
409 * - decode algebraic code
410 * - decode pitch and codebook gains
411 * - find voicing factor and tilt of code for next subframe
412 * - find the excitation and compute synthesis speech
413 */
414
415 p_Aq = Aq; /* pointer to interpolated LPC parameters */
416
417 for (i_subfr = 0; i_subfr < L_FRAME; i_subfr += L_SUBFR) {
418 pit_flag = i_subfr;
419
420 if ((i_subfr == (2 * L_SUBFR)) & (mode > MODE_7k)) {
421 pit_flag = 0;
422 }
423
424 /*
425 * - Decode pitch lag
426 * Lag indeces received also in case of BFI,
427 * so that the parameter pointer stays in sync.
428 */
429
430 if (pit_flag == 0) {
431 if (mode <= MODE_9k) {
432 index = *prms++;
433
434 if (index < ((PIT_FR1_8b - PIT_MIN) * 2)) {
435 T0 = (PIT_MIN + (index >> 1));
436 T0_frac = (index - ((T0 - PIT_MIN) << 1));
437 T0_frac = (T0_frac << 1);
438 } else {
439 T0 = index + (PIT_FR1_8b - ((PIT_FR1_8b - PIT_MIN) * 2));
440 T0_frac = 0;
441 }
442 } else {
443 index = *prms++;
444
445 if (index < ((PIT_FR2 - PIT_MIN) * 4)) {
446 T0 = PIT_MIN + (index >> 2);
447 T0_frac = index - ((T0 - PIT_MIN) << 2);
448 } else if (index <
449 ((((PIT_FR2 - PIT_MIN) * 4) + ((PIT_FR1_9b - PIT_FR2) * 2)))) {
450 index = (Word16)((index - ((PIT_FR2 - PIT_MIN) * 4)));
451 T0 = PIT_FR2 + (index >> 1);
452 T0_frac = index - ((T0 - PIT_FR2) << 1);
453 T0_frac = T0_frac << 1;
454 } else {
455 T0 = index + (PIT_FR1_9b - ((PIT_FR2 - PIT_MIN) * 4) -
456 ((PIT_FR1_9b - PIT_FR2) * 2));
457 T0_frac = 0;
458 }
459 }
460
461 /* find T0_min and T0_max for subframe 2 and 4 */
462 T0_min = T0 - 8;
463
464 if (T0_min < PIT_MIN) {
465 T0_min = PIT_MIN;
466 }
467
468 T0_max = T0_min + 15;
469
470 if (T0_max > PIT_MAX) {
471 T0_max = PIT_MAX;
472 T0_min = T0_max - 15;
473 }
474 } else {
475 /* if subframe 2 or 4 */
476
477 if (mode <= MODE_9k) {
478 index = *prms++;
479 T0 = T0_min + (index >> 1);
480 T0_frac = index - ((T0 - T0_min) << 1);
481 T0_frac = T0_frac << 1;
482 } else {
483 index = *prms++;
484 T0 = T0_min + (index >> 2);
485 T0_frac = index - ((T0 - T0_min) << 2);
486 }
487 }
488
489 /* check BFI after pitch lag decoding */
490 if (bfi != 0) { /* if frame erasure */
491 D_GAIN_lag_concealment(&(st->mem_gain[17]), st->mem_lag, &T0,
492 &(st->mem_T0), &(st->mem_seed3), unusable_frame);
493 T0_frac = 0;
494 }
495
496 /*
497 * Find the pitch gain, the interpolation filter
498 * and the adaptive codebook vector.
499 */
500
501 D_GAIN_adaptive_codebook_excitation(&exc[i_subfr], T0, T0_frac);
502
503 if (unusable_frame) {
504 select = 1;
505 } else {
506 if (mode <= MODE_9k) {
507 select = 0;
508 } else {
509 select = *prms++;
510 }
511 }
512
513 if (select == 0) {
514 /* find pitch excitation with lp filter */
515 for (i = 0; i < L_SUBFR; i++) {
516 L_tmp = 2949 * exc[i - 1 + i_subfr];
517 L_tmp = L_tmp + (10486 * exc[i + i_subfr]);
518 L_tmp = L_tmp + (2949 * exc[i + 1 + i_subfr]);
519 code[i] = (Word16)((L_tmp + 0x2000) >> 14);
520 }
521
522 memcpy(&exc[i_subfr], code, L_SUBFR * sizeof(Word16));
523 }
524
525 /*
526 * Decode innovative codebook.
527 * Add the fixed-gain pitch contribution to code[].
528 */
529
530 if (unusable_frame != 0) {
531 /* the innovative code doesn't need to be scaled (see Q_gain2) */
532 for (i = 0; i < L_SUBFR; i++) {
533 code[i] = (Word16)(D_UTIL_random(&(st->mem_seed)) >> 3);
534 }
535 } else if (mode <= MODE_7k) {
536 ind[0] = *prms++;
537 D_ACELP_decode_2t(ind[0], code);
538 } else if (mode <= MODE_9k) {
539 memcpy(ind, prms, 4 * sizeof(Word16));
540 prms += 4;
541 D_ACELP_decode_4t(ind, 20, code);
542 } else if (mode <= MODE_12k) {
543 memcpy(ind, prms, 4 * sizeof(Word16));
544 prms += 4;
545 D_ACELP_decode_4t(ind, 36, code);
546 } else if (mode <= MODE_14k) {
547 memcpy(ind, prms, 4 * sizeof(Word16));
548 prms += 4;
549 D_ACELP_decode_4t(ind, 44, code);
550 } else if (mode <= MODE_16k) {
551 memcpy(ind, prms, 4 * sizeof(Word16));
552 prms += 4;
553 D_ACELP_decode_4t(ind, 52, code);
554 } else if (mode <= MODE_18k) {
555 memcpy(ind, prms, 8 * sizeof(Word16));
556 prms += 8;
557 D_ACELP_decode_4t(ind, 64, code);
558 } else if (mode <= MODE_20k) {
559 memcpy(ind, prms, 8 * sizeof(Word16));
560 prms += 8;
561 D_ACELP_decode_4t(ind, 72, code);
562 } else {
563 memcpy(ind, prms, 8 * sizeof(Word16));
564 prms += 8;
565 D_ACELP_decode_4t(ind, 88, code);
566 }
567
568 tmp = 0;
569 D_UTIL_preemph(code, st->mem_tilt_code, L_SUBFR, &tmp);
570
571 L_tmp = T0;
572
573 if (T0_frac > 2) {
574 L_tmp = L_tmp + 1;
575 }
576
577 D_GAIN_pitch_sharpening(code, L_tmp, PIT_SHARP);
578
579 /*
580 * Decode codebooks gains.
581 */
582 index = *prms++; /* codebook gain index */
583
584 if (mode <= MODE_9k) {
585 D_GAIN_decode(index, 6, code, &gain_pit, &L_gain_code, bfi,
586 st->mem_bfi, st->mem_state, unusable_frame, st->mem_vad_hist,
587 st->mem_gain);
588 } else {
589 D_GAIN_decode(index, 7, code, &gain_pit, &L_gain_code, bfi,
590 st->mem_bfi, st->mem_state, unusable_frame, st->mem_vad_hist,
591 st->mem_gain);
592 }
593
594 /* find best scaling to perform on excitation (Q_new) */
595 tmp = st->mem_subfr_q[0];
596
597 for (i = 1; i < 4; i++) {
598 if (st->mem_subfr_q[i] < tmp) {
599 tmp = st->mem_subfr_q[i];
600 }
601 }
602
603 /* limit scaling (Q_new) to Q_MAX */
604 if (tmp > Q_MAX) {
605 tmp = Q_MAX;
606 }
607
608 Q_new = 0;
609 L_tmp = L_gain_code; /* L_gain_code in Q16 */
610
611 while ((L_tmp < 0x08000000L) && (Q_new < tmp)) {
612 L_tmp = (L_tmp << 1);
613 Q_new = (Word16)((Q_new + 1));
614 }
615
616 if (L_tmp < 0x7FFF7FFF) {
617 gain_code = (Word16)((L_tmp + 0x8000) >> 16);
618 /* scaled gain_code with Qnew */
619 } else {
620 gain_code = 32767;
621 }
622
623 if (Q_new > st->mem_q) {
624 D_UTIL_signal_up_scale(exc + i_subfr - (PIT_MAX + L_INTERPOL),
625 PIT_MAX + L_INTERPOL + L_SUBFR, (Word16)(Q_new - st->mem_q));
626 } else {
627 D_UTIL_signal_down_scale(exc + i_subfr - (PIT_MAX + L_INTERPOL),
628 PIT_MAX + L_INTERPOL + L_SUBFR, (Word16)(st->mem_q - Q_new));
629 }
630
631 st->mem_q = Q_new;
632
633 /*
634 * Update parameters for the next subframe.
635 * - tilt of code: 0.0 (unvoiced) to 0.5 (voiced)
636 */
637 if (bfi == 0) {
638 /* LTP-Lag history update */
639 for (i = 4; i > 0; i--) {
640 st->mem_lag[i] = st->mem_lag[i - 1];
641 }
642 st->mem_lag[0] = (Word16)T0;
643 st->mem_T0 = (Word16)T0;
644 st->mem_T0_frac = 0; /* Remove fraction in case of BFI */
645 }
646
647 /* find voice factor in Q15 (1=voiced, -1=unvoiced) */
648 memcpy(exc2, &exc[i_subfr], L_SUBFR * sizeof(Word16));
649 D_UTIL_signal_down_scale(exc2, L_SUBFR, 3);
650
651 /* post processing of excitation elements */
652 if (mode <= MODE_9k) {
653 pit_sharp = (Word16)(gain_pit << 1);
654
655 if (pit_sharp > 16384) {
656 if (pit_sharp > 32767) {
657 pit_sharp = 32767;
658 }
659
660 for (i = 0; i < L_SUBFR; i++) {
661 L_tmp = (exc2[i] * pit_sharp) >> 15;
662 L_tmp = L_tmp * gain_pit;
663 excp[i] = (Word16)((L_tmp + 0x8000) >> 16);
664 }
665 }
666 }
667
668 voice_fac = D_GAIN_find_voice_factor(exc2, -3, gain_pit, code, gain_code,
669 L_SUBFR);
670
671 /* tilt of code for next subframe: 0.5=voiced, 0=unvoiced */
672 st->mem_tilt_code = (Word16)((voice_fac >> 2) + 8192);
673
674 /*
675 * Find the total excitation.
676 * Find synthesis speech corresponding to exc[].
677 * Find maximum value of excitation for next scaling
678 */
679 memcpy(exc2, &exc[i_subfr], L_SUBFR * sizeof(Word16));
680 max = 1;
681
682 for (i = 0; i < L_SUBFR; i++) {
683 L_tmp = (code[i] * gain_code) << 5;
684 L_tmp = L_tmp + (exc[i + i_subfr] * gain_pit);
685 L_tmp = (L_tmp + 0x2000) >> 14;
686
687 if ((L_tmp > MIN_16) & (L_tmp < 32768)) {
688 exc[i + i_subfr] = (Word16)L_tmp;
689 tmp = (Word16)(abs(L_tmp));
690
691 if (tmp > max) {
692 max = tmp;
693 }
694 } else if (L_tmp > MAX_16) {
695 exc[i + i_subfr] = MAX_16;
696 max = MAX_16;
697 } else {
698 exc[i + i_subfr] = MIN_16;
699 max = MAX_16;
700 }
701 }
702
703 /* tmp = scaling possible according to max value of excitation */
704 tmp = (Word16)((D_UTIL_norm_s(max) + Q_new) - 1);
705 st->mem_subfr_q[3] = st->mem_subfr_q[2];
706 st->mem_subfr_q[2] = st->mem_subfr_q[1];
707 st->mem_subfr_q[1] = st->mem_subfr_q[0];
708 st->mem_subfr_q[0] = tmp;
709
710 /*
711 * phase dispersion to enhance noise in low bit rate
712 */
713
714 /* L_gain_code in Q16 */
715 D_UTIL_l_extract(L_gain_code, &gain_code, &gain_code_lo);
716
717 if (mode <= MODE_7k) {
718 j = 0; /* high dispersion for rate <= 7.5 kbit/s */
719 } else if (mode <= MODE_9k) {
720 j = 1; /* low dispersion for rate <= 9.6 kbit/s */
721 } else {
722 j = 2; /* no dispersion for rate > 9.6 kbit/s */
723 }
724
725 D_ACELP_phase_dispersion(gain_code, gain_pit, code, (Word16)j,
726 st->mem_ph_disp);
727
728 /*
729 * noise enhancer
730 * - Enhance excitation on noise. (modify gain of code)
731 * If signal is noisy and LPC filter is stable, move gain
732 * of code 1.5 dB toward gain of code threshold.
733 * This decrease by 3 dB noise energy variation.
734 */
735 L_tmp = 16384 - (voice_fac >> 1); /* 1=unvoiced, 0=voiced */
736 fac = (Word16)((L_stab_fac * L_tmp) >> 15);
737 L_tmp = L_gain_code;
738
739 if (L_tmp < st->mem_gc_thres) {
740 L_tmp = (L_tmp + D_UTIL_mpy_32_16(gain_code, gain_code_lo, 6226));
741
742 if (L_tmp > st->mem_gc_thres) {
743 L_tmp = st->mem_gc_thres;
744 }
745 } else {
746 L_tmp = D_UTIL_mpy_32_16(gain_code, gain_code_lo, 27536);
747
748 if (L_tmp < st->mem_gc_thres) {
749 L_tmp = st->mem_gc_thres;
750 }
751 }
752 st->mem_gc_thres = L_tmp;
753 L_gain_code =
754 D_UTIL_mpy_32_16(gain_code, gain_code_lo, (Word16)(32767 - fac));
755 D_UTIL_l_extract(L_tmp, &gain_code, &gain_code_lo);
756 L_gain_code =
757 L_gain_code + D_UTIL_mpy_32_16(gain_code, gain_code_lo, fac);
758
759 /*
760 * pitch enhancer
761 * - Enhance excitation on voice. (HP filtering of code)
762 * On voiced signal, filtering of code by a smooth fir HP
763 * filter to decrease energy of code in low frequency.
764 */
765
766 L_tmp2 = (voice_fac >> 3) + 4096; /* 0.25=voiced, 0=unvoiced */
767 L_tmp = (code[0] << 15) - (code[1] * L_tmp2);
768 code2[0] = (L_tmp + 0x4000) >> 15;
769
770 for (i = 1; i < L_SUBFR - 1; i++) {
771 L_tmp = code[i] << 15;
772 L_tmp = L_tmp - (code[i + 1] * L_tmp2);
773 L_tmp = L_tmp - (code[i - 1] * L_tmp2);
774 code2[i] = (L_tmp + 0x4000) >> 15;
775 }
776
777 L_tmp = code[L_SUBFR - 1] << 15;
778 L_tmp = L_tmp - (code[L_SUBFR - 2] * L_tmp2);
779 code2[L_SUBFR - 1] = (L_tmp + 0x4000) >> 15;
780
781 /* build excitation */
782 gain_code = (Word16)(((L_gain_code << Q_new) + 0x8000) >> 16);
783
784 for (i = 0; i < L_SUBFR; i++) {
785 L_tmp = (code2[i] * gain_code) << 5;
786 L_tmp = L_tmp + (exc2[i] * gain_pit);
787 L_tmp = (L_tmp + 0x2000) >> 14;
788
789 exc2[i] = D_UTIL_saturate(L_tmp);
790 }
791
792 if (mode <= MODE_9k) {
793 if (pit_sharp > 16384) {
794 for (i = 0; i < L_SUBFR; i++) {
795 L_tmp = (excp[i] + exc2[i]);
796 excp[i] = D_UTIL_saturate(L_tmp);
797 }
798
799 D_GAIN_adaptive_control(exc2, excp, L_SUBFR);
800 memcpy(exc2, excp, L_SUBFR * sizeof(Word16));
801 }
802 }
803
804 if (mode <= MODE_7k) {
805 j = (i_subfr >> 6);
806
807 for (i = 0; i < M; i++) {
808 L_tmp = isf_tmp[i] * (32767 - D_ROM_interpol_frac[j]);
809 L_tmp = L_tmp + (isf[i] * D_ROM_interpol_frac[j]);
810 HfIsf[i] = (Word16)((L_tmp + 0x4000) >> 15);
811 }
812 } else {
813 memset(st->mem_syn_hf, 0, (M16k - M) * sizeof(Word16));
814 }
815
816 if (mode >= MODE_24k) {
817 corr_gain = *prms++;
818 D_UTIL_dec_synthesis(p_Aq, exc2, Q_new, &synth16k[i_subfr * 5 / 4],
819 corr_gain, HfIsf, mode, newDTXState, bfi, st);
820 } else {
821 D_UTIL_dec_synthesis(p_Aq, exc2, Q_new, &synth16k[i_subfr * 5 / 4], 0,
822 HfIsf, mode, newDTXState, bfi, st);
823 }
824
825 p_Aq += (M + 1); /* interpolated LPC parameters for next subframe */
826 }
827
828 /*
829 * Update signal for next frame
830 * -> save past of exc[]
831 * -> save pitch parameters.
832 */
833
834 //memmove(st->mem_exc, &st->mem_exc[L_FRAME], (PIT_MAX + L_INTERPOL) * sizeof(Word16));
835 memcpy(st->mem_exc, &st->mem_exc[L_FRAME], (PIT_MAX + L_INTERPOL) * sizeof(Word16));
836 D_UTIL_signal_down_scale(exc, L_FRAME, Q_new);
837 D_DTX_activity_update(st->dtx_decSt, isf, exc);
838 st->dtx_decSt->mem_dtx_global_state = (UWord8)newDTXState;
839 st->mem_bfi = bfi;
840
841 return(0);
842}
843