comparison libtwamr/dec_amr.c @ 426:7bef001cd8b8

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