Initialize Tizen 2.3
[external/opencore-amr.git] / opencore / codecs_v2 / audio / gsm_amr / amr_nb / dec / src / dec_amr.cpp
1 /* ------------------------------------------------------------------
2  * Copyright (C) 1998-2009 PacketVideo
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
13  * express or implied.
14  * See the License for the specific language governing permissions
15  * and limitations under the License.
16  * -------------------------------------------------------------------
17  */
18 /****************************************************************************************
19 Portions of this file are derived from the following 3GPP standard:
20
21     3GPP TS 26.073
22     ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec
23     Available from http://www.3gpp.org
24
25 (C) 2004, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TTA, TTC)
26 Permission to distribute, modify and use this file under the standard license
27 terms listed above has been obtained from the copyright holder.
28 ****************************************************************************************/
29 /*
30 ------------------------------------------------------------------------------
31
32  Filename: dec_amr.cpp
33  Funtions: Decoder_amr_init
34            Decoder_amr_reset
35            Decoder_amr
36
37 ------------------------------------------------------------------------------
38  MODULE DESCRIPTION
39
40  This file contains the function used to decode one speech frame using a given
41  codec mode. The functions used to initialize, reset, and exit are also
42  included in this file.
43
44 ------------------------------------------------------------------------------
45 */
46
47 /*----------------------------------------------------------------------------
48 ; INCLUDES
49 ----------------------------------------------------------------------------*/
50 #include "dec_amr.h"
51 #include "typedef.h"
52 #include "cnst.h"
53 #include "set_zero.h"
54 #include "syn_filt.h"
55 #include "d_plsf.h"
56 #include "agc.h"
57 #include "int_lpc.h"
58 #include "dec_gain.h"
59 #include "dec_lag3.h"
60 #include "dec_lag6.h"
61 #include "d2_9pf.h"
62 #include "d2_11pf.h"
63 #include "d3_14pf.h"
64 #include "d4_17pf.h"
65 #include "d8_31pf.h"
66 #include "d1035pf.h"
67 #include "pred_lt.h"
68 #include "d_gain_p.h"
69 #include "d_gain_c.h"
70 #include "dec_gain.h"
71 #include "ec_gains.h"
72 #include "ph_disp.h"
73 #include "c_g_aver.h"
74 #include "int_lsf.h"
75 #include "lsp_lsf.h"
76 #include "lsp_avg.h"
77 #include "bgnscd.h"
78 #include "ex_ctrl.h"
79 #include "sqrt_l.h"
80 #include "frame.h"
81 #include "b_cn_cod.h"
82 #include "basic_op.h"
83 #include "oscl_mem.h"
84
85 /*----------------------------------------------------------------------------
86 ; MACROS
87 ; Define module specific macros here
88 ----------------------------------------------------------------------------*/
89
90 /*----------------------------------------------------------------------------
91 ; DEFINES
92 ; Include all pre-processor statements here. Include conditional
93 ; compile variables also.
94 ----------------------------------------------------------------------------*/
95
96 /*----------------------------------------------------------------------------
97 ; LOCAL FUNCTION DEFINITIONS
98 ; Function Prototype declaration
99 ----------------------------------------------------------------------------*/
100
101 /*----------------------------------------------------------------------------
102 ; LOCAL VARIABLE DEFINITIONS
103 ; Variable declaration - defined here and used outside this module
104 ----------------------------------------------------------------------------*/
105
106
107 /*
108 ------------------------------------------------------------------------------
109  FUNCTION NAME: Decoder_amr_init
110 ------------------------------------------------------------------------------
111  INPUT AND OUTPUT DEFINITIONS
112
113  Inputs:
114     state = pointer to a pointer to structures of type Decoder_amrState
115
116  Outputs:
117     structure pointed to by the pointer which is pointed to by state is
118       initialized to each field's initial values
119
120     state pointer points to the address of the memory allocated by
121       Decoder_amr_init function
122
123  Returns:
124     return_value = 0, if the initialization was successful; -1, otherwise (int)
125
126  Global Variables Used:
127     None
128
129  Local Variables Needed:
130     None
131
132 ------------------------------------------------------------------------------
133  FUNCTION DESCRIPTION
134
135  This function allocates and initializes state memory used by the Decoder_amr
136  function. It stores the pointer to the filter status structure in state. This
137  pointer has to be passed to Decoder_amr in each call. The function returns
138  0, if initialization was successful and -1, otherwise.
139
140 ------------------------------------------------------------------------------
141  REQUIREMENTS
142
143  None
144
145 ------------------------------------------------------------------------------
146  REFERENCES
147
148  dec_amr.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
149
150 ------------------------------------------------------------------------------
151  PSEUDO-CODE
152
153 int Decoder_amr_init (Decoder_amrState **state)
154 {
155   Decoder_amrState* s;
156   Word16 i;
157
158   if (state == (Decoder_amrState **) NULL){
159       fprintf(stderr, "Decoder_amr_init: invalid parameter\n");
160       return -1;
161   }
162   *state = NULL;
163
164   // allocate memory
165   if ((s= (Decoder_amrState *) malloc(sizeof(Decoder_amrState))) == NULL){
166       fprintf(stderr, "Decoder_amr_init: can not malloc state structure\n");
167       return -1;
168   }
169
170   s->T0_lagBuff = 40;
171   s->inBackgroundNoise = 0;
172   s->voicedHangover = 0;
173   for (i = 0; i < 9; i++)
174      s->ltpGainHistory[i] = 0;
175
176   s->lsfState = NULL;
177   s->ec_gain_p_st = NULL;
178   s->ec_gain_c_st = NULL;
179   s->pred_state = NULL;
180   s->ph_disp_st = NULL;
181   s->dtxDecoderState = NULL;
182
183   if (D_plsf_init(&s->lsfState) ||
184       ec_gain_pitch_init(&s->ec_gain_p_st) ||
185       ec_gain_code_init(&s->ec_gain_c_st) ||
186       gc_pred_init(&s->pred_state) ||
187       Cb_gain_average_init(&s->Cb_gain_averState) ||
188       lsp_avg_init(&s->lsp_avg_st) ||
189       Bgn_scd_init(&s->background_state) ||
190       ph_disp_init(&s->ph_disp_st) ||
191       dtx_dec_init(&s->dtxDecoderState)) {
192       Decoder_amr_exit(&s);
193       return -1;
194   }
195
196   Decoder_amr_reset(s, (enum Mode)0);
197   *state = s;
198
199   return 0;
200 }
201
202 ------------------------------------------------------------------------------
203  CAUTION [optional]
204  [State any special notes, constraints or cautions for users of this function]
205
206 ------------------------------------------------------------------------------
207 */
208
209 Word16 Decoder_amr_init(Decoder_amrState *s)
210 {
211     Word16 i;
212
213     if (s == (Decoder_amrState *) NULL)
214     {
215         /* fprint(stderr, "Decoder_amr_init: invalid parameter\n");  */
216         return(-1);
217     }
218
219     get_const_tbls(&s->common_amr_tbls);
220
221     s->T0_lagBuff = 40;
222     s->inBackgroundNoise = 0;
223     s->voicedHangover = 0;
224
225     /* Initialize overflow Flag */
226
227     s->overflow = 0;
228
229     for (i = 0; i < LTP_GAIN_HISTORY_LEN; i++)
230     {
231         s->ltpGainHistory[i] = 0;
232     }
233
234     D_plsf_reset(&s->lsfState, s->common_amr_tbls.mean_lsf_5_ptr);
235     ec_gain_pitch_reset(&s->ec_gain_p_st);
236     ec_gain_code_reset(&s->ec_gain_c_st);
237     Cb_gain_average_reset(&s->Cb_gain_averState);
238     lsp_avg_reset(&s->lsp_avg_st, s->common_amr_tbls.mean_lsf_5_ptr);
239     Bgn_scd_reset(&s->background_state);
240     ph_disp_reset(&s->ph_disp_st);
241     dtx_dec_reset(&s->dtxDecoderState);
242     gc_pred_reset(&s->pred_state);
243
244     Decoder_amr_reset(s, MR475);
245
246     return(0);
247 }
248
249 /****************************************************************************/
250
251 /*
252 ------------------------------------------------------------------------------
253  FUNCTION NAME: Decoder_amr_reset
254 ------------------------------------------------------------------------------
255  INPUT AND OUTPUT DEFINITIONS
256
257  Inputs:
258     state = pointer to a structure of type Decoder_amrState
259     mode = codec mode (enum Mode)
260
261  Outputs:
262     structure pointed to by state is initialized to its reset value
263
264  Returns:
265     return_value = 0, if reset was successful; -1, otherwise (int)
266
267  Global Variables Used:
268     None
269
270  Local Variables Needed:
271     None
272
273 ------------------------------------------------------------------------------
274  FUNCTION DESCRIPTION
275
276  This function resets the state memory used by the Decoder_amr function. It
277  returns a 0, if reset was successful and -1, otherwise.
278
279 ------------------------------------------------------------------------------
280  REQUIREMENTS
281
282  None
283
284 ------------------------------------------------------------------------------
285  REFERENCES
286
287  dec_amr.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
288
289 ------------------------------------------------------------------------------
290  PSEUDO-CODE
291
292 int Decoder_amr_reset (Decoder_amrState *state, enum Mode mode)
293 {
294   Word16 i;
295
296   if (state == (Decoder_amrState *) NULL){
297       fprintf(stderr, "Decoder_amr_reset: invalid parameter\n");
298       return -1;
299   }
300
301   // Initialize static pointer
302   state->exc = state->old_exc + PIT_MAX + L_INTERPOL;
303
304   // Static vectors to zero
305   Set_zero (state->old_exc, PIT_MAX + L_INTERPOL);
306
307   if (mode != MRDTX)
308      Set_zero (state->mem_syn, M);
309
310   // initialize pitch sharpening
311   state->sharp = SHARPMIN;
312   state->old_T0 = 40;
313
314   // Initialize state->lsp_old []
315
316   if (mode != MRDTX) {
317       Copy(lsp_init_data, &state->lsp_old[0], M);
318   }
319
320   // Initialize memories of bad frame handling
321   state->prev_bf = 0;
322   state->prev_pdf = 0;
323   state->state = 0;
324
325   state->T0_lagBuff = 40;
326   state->inBackgroundNoise = 0;
327   state->voicedHangover = 0;
328   if (mode != MRDTX) {
329       for (i=0;i<9;i++)
330           state->excEnergyHist[i] = 0;
331   }
332
333   for (i = 0; i < 9; i++)
334      state->ltpGainHistory[i] = 0;
335
336   Cb_gain_average_reset(state->Cb_gain_averState);
337   if (mode != MRDTX)
338      lsp_avg_reset(state->lsp_avg_st);
339   D_plsf_reset(state->lsfState);
340   ec_gain_pitch_reset(state->ec_gain_p_st);
341   ec_gain_code_reset(state->ec_gain_c_st);
342
343   if (mode != MRDTX)
344      gc_pred_reset(state->pred_state);
345
346   Bgn_scd_reset(state->background_state);
347   state->nodataSeed = 21845;
348   ph_disp_reset(state->ph_disp_st);
349   if (mode != MRDTX)
350      dtx_dec_reset(state->dtxDecoderState);
351
352   return 0;
353 }
354
355 ------------------------------------------------------------------------------
356  CAUTION [optional]
357  [State any special notes, constraints or cautions for users of this function]
358
359 ------------------------------------------------------------------------------
360 */
361
362 Word16 Decoder_amr_reset(Decoder_amrState *state, enum Mode mode)
363 {
364     Word16 i;
365
366     if (state == (Decoder_amrState *) NULL)
367     {
368         /* fprint(stderr, "Decoder_amr_reset: invalid parameter\n");  */
369         return(-1);
370     }
371
372     /* Initialize static pointer */
373     state->exc = state->old_exc + PIT_MAX + L_INTERPOL;
374
375     /* Static vectors to zero */
376     oscl_memset(state->old_exc, 0, sizeof(Word16)*(PIT_MAX + L_INTERPOL));
377
378     if (mode != MRDTX)
379     {
380         oscl_memset(state->mem_syn, 0, sizeof(Word16)*M);
381     }
382     /* initialize pitch sharpening */
383     state->sharp = SHARPMIN;
384     state->old_T0 = 40;
385
386     /* Initialize overflow Flag */
387
388     state->overflow = 0;
389
390     /* Initialize state->lsp_old [] */
391
392     if (mode != MRDTX)
393     {
394         state->lsp_old[0] = 30000;
395         state->lsp_old[1] = 26000;
396         state->lsp_old[2] = 21000;
397         state->lsp_old[3] = 15000;
398         state->lsp_old[4] = 8000;
399         state->lsp_old[5] = 0;
400         state->lsp_old[6] = -8000;
401         state->lsp_old[7] = -15000;
402         state->lsp_old[8] = -21000;
403         state->lsp_old[9] = -26000;
404     }
405
406     /* Initialize memories of bad frame handling */
407     state->prev_bf = 0;
408     state->prev_pdf = 0;
409     state->state = 0;
410
411     state->T0_lagBuff = 40;
412     state->inBackgroundNoise = 0;
413     state->voicedHangover = 0;
414     if (mode != MRDTX)
415     {
416         for (i = 0; i < EXC_ENERGY_HIST_LEN; i++)
417         {
418             state->excEnergyHist[i] = 0;
419         }
420     }
421
422     for (i = 0; i < LTP_GAIN_HISTORY_LEN; i++)
423     {
424         state->ltpGainHistory[i] = 0;
425     }
426
427     Cb_gain_average_reset(&(state->Cb_gain_averState));
428     if (mode != MRDTX)
429     {
430         lsp_avg_reset(&(state->lsp_avg_st), state->common_amr_tbls.mean_lsf_5_ptr);
431     }
432     D_plsf_reset(&(state->lsfState), state->common_amr_tbls.mean_lsf_5_ptr);
433     ec_gain_pitch_reset(&(state->ec_gain_p_st));
434     ec_gain_code_reset(&(state->ec_gain_c_st));
435
436     if (mode != MRDTX)
437     {
438         gc_pred_reset(&(state->pred_state));
439     }
440
441     Bgn_scd_reset(&(state->background_state));
442     state->nodataSeed = 21845;
443     ph_disp_reset(&(state->ph_disp_st));
444     if (mode != MRDTX)
445     {
446         dtx_dec_reset(&(state->dtxDecoderState));
447     }
448
449     return(0);
450 }
451
452 /****************************************************************************/
453
454 /*
455 ------------------------------------------------------------------------------
456  FUNCTION NAME: Decoder_amr
457 ------------------------------------------------------------------------------
458  INPUT AND OUTPUT DEFINITIONS
459
460  Inputs:
461     st = pointer to a structure of type Decoder_amrState
462     mode = codec mode (enum Mode)
463     parm = buffer of synthesis parameters (Word16)
464     frame_type = received frame type (enum RXFrameType)
465     synth = buffer containing synthetic speech (Word16)
466     A_t = buffer containing decoded LP filter in 4 subframes (Word16)
467
468  Outputs:
469     structure pointed to by st contains the newly calculated decoder
470       parameters
471     synth buffer contains the decoded speech samples
472     A_t buffer contains the decoded LP filter parameters
473
474  Returns:
475     return_value = 0 (int)
476
477  Global Variables Used:
478     None
479
480  Local Variables Needed:
481     None
482
483 ------------------------------------------------------------------------------
484  FUNCTION DESCRIPTION
485
486  This function performs the decoding of one speech frame for a given codec
487  mode.
488
489 ------------------------------------------------------------------------------
490  REQUIREMENTS
491
492  None
493
494 ------------------------------------------------------------------------------
495  REFERENCES
496
497  dec_amr.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
498
499 ------------------------------------------------------------------------------
500  PSEUDO-CODE
501
502 int Decoder_amr (
503     Decoder_amrState *st,      // i/o : State variables
504     enum Mode mode,            // i   : AMR mode
505     Word16 parm[],             // i   : vector of synthesis parameters
506                                         (PRM_SIZE)
507     enum RXFrameType frame_type, // i   : received frame type
508     Word16 synth[],            // o   : synthesis speech (L_FRAME)
509     Word16 A_t[]               // o   : decoded LP filter in 4 subframes
510                                         (AZ_SIZE)
511 )
512 {
513     // LPC coefficients
514
515     Word16 *Az;                // Pointer on A_t
516
517     // LSPs
518
519     Word16 lsp_new[M];
520     Word16 lsp_mid[M];
521
522     // LSFs
523
524     Word16 prev_lsf[M];
525     Word16 lsf_i[M];
526
527     // Algebraic codevector
528
529     Word16 code[L_SUBFR];
530
531     // excitation
532
533     Word16 excp[L_SUBFR];
534     Word16 exc_enhanced[L_SUBFR];
535
536     // Scalars
537
538     Word16 i, i_subfr;
539     Word16 T0, T0_frac, index, index_mr475 = 0;
540     Word16 gain_pit, gain_code, gain_code_mix, pit_sharp, pit_flag, pitch_fac;
541     Word16 t0_min, t0_max;
542     Word16 delta_frc_low, delta_frc_range;
543     Word16 tmp_shift;
544     Word16 temp;
545     Word32 L_temp;
546     Word16 flag4;
547     Word16 carefulFlag;
548     Word16 excEnergy;
549     Word16 subfrNr;
550     Word16 evenSubfr = 0;
551
552     Word16 bfi = 0;   // bad frame indication flag
553     Word16 pdfi = 0;  // potential degraded bad frame flag
554
555     enum DTXStateType newDTXState;  // SPEECH , DTX, DTX_MUTE
556
557     // find the new  DTX state  SPEECH OR DTX
558     newDTXState = rx_dtx_handler(st->dtxDecoderState, frame_type);
559
560     // DTX actions
561     if (sub(newDTXState, SPEECH) != 0 )
562     {
563        Decoder_amr_reset (st, MRDTX);
564
565        dtx_dec(st->dtxDecoderState,
566                st->mem_syn,
567                st->lsfState,
568                st->pred_state,
569                st->Cb_gain_averState,
570                newDTXState,
571                mode,
572                parm, synth, A_t);
573        // update average lsp
574
575        Lsf_lsp(st->lsfState->past_lsf_q, st->lsp_old, M);
576        lsp_avg(st->lsp_avg_st, st->lsfState->past_lsf_q);
577        goto the_end;
578     }
579
580     // SPEECH action state machine
581     if ((sub(frame_type, RX_SPEECH_BAD) == 0) ||
582         (sub(frame_type, RX_NO_DATA) == 0) ||
583         (sub(frame_type, RX_ONSET) == 0))
584     {
585        bfi = 1;
586        if ((sub(frame_type, RX_NO_DATA) == 0) ||
587            (sub(frame_type, RX_ONSET) == 0))
588        {
589       build_CN_param(&st->nodataSeed,
590              prmno[mode],
591              bitno[mode],
592              parm);
593        }
594     }
595     else if (sub(frame_type, RX_SPEECH_DEGRADED) == 0)
596     {
597        pdfi = 1;
598     }
599
600     if (bfi != 0)
601     {
602         st->state = add (st->state, 1);
603     }
604     else if (sub (st->state, 6) == 0)
605
606     {
607         st->state = 5;
608     }
609     else
610     {
611         st->state = 0;
612     }
613
614     if (sub (st->state, 6) > 0)
615     {
616         st->state = 6;
617     }
618
619     // If this frame is the first speech frame after CNI period,
620     // set the BFH state machine to an appropriate state depending
621     // on whether there was DTX muting before start of speech or not
622     // If there was DTX muting, the first speech frame is muted.
623     // If there was no DTX muting, the first speech frame is not
624     // muted. The BFH state machine starts from state 5, however, to
625     // keep the audible noise resulting from a SID frame which is
626     // erroneously interpreted as a good speech frame as small as
627     // possible (the decoder output in this case is quickly muted)
628
629     if (sub(st->dtxDecoderState->dtxGlobalState, DTX) == 0)
630     {
631        st->state = 5;
632        st->prev_bf = 0;
633     }
634     else if (sub(st->dtxDecoderState->dtxGlobalState, DTX_MUTE) == 0)
635     {
636        st->state = 5;
637        st->prev_bf = 1;
638     }
639
640     // save old LSFs for CB gain smoothing
641     Copy (st->lsfState->past_lsf_q, prev_lsf, M);
642
643     // decode LSF parameters and generate interpolated lpc coefficients
644        for the 4 subframes
645     if (sub (mode, MR122) != 0)
646     {
647        D_plsf_3(st->lsfState, mode, bfi, parm, lsp_new);
648
649        // Advance synthesis parameters pointer
650        parm += 3;
651
652        Int_lpc_1to3(st->lsp_old, lsp_new, A_t);
653     }
654     else
655     {
656        D_plsf_5 (st->lsfState, bfi, parm, lsp_mid, lsp_new);
657
658        // Advance synthesis parameters pointer
659        parm += 5;
660
661        Int_lpc_1and3 (st->lsp_old, lsp_mid, lsp_new, A_t);
662     }
663
664     // update the LSPs for the next frame
665     for (i = 0; i < M; i++)
666     {
667        st->lsp_old[i] = lsp_new[i];
668     }
669
670     *------------------------------------------------------------------------*
671     *          Loop for every subframe in the analysis frame                 *
672     *------------------------------------------------------------------------*
673     * The subframe size is L_SUBFR and the loop is repeated L_FRAME/L_SUBFR  *
674     *  times                                                                 *
675     *     - decode the pitch delay                                           *
676     *     - decode algebraic code                                            *
677     *     - decode pitch and codebook gains                                  *
678     *     - find the excitation and compute synthesis speech                 *
679     *------------------------------------------------------------------------*
680
681     // pointer to interpolated LPC parameters
682     Az = A_t;
683
684     evenSubfr = 0;
685     subfrNr = -1;
686     for (i_subfr = 0; i_subfr < L_FRAME; i_subfr += L_SUBFR)
687     {
688        subfrNr = add(subfrNr, 1);
689        evenSubfr = sub(1, evenSubfr);
690
691        // flag for first and 3th subframe
692        pit_flag = i_subfr;
693
694        if (sub (i_subfr, L_FRAME_BY2) == 0)
695        {
696           if (sub(mode, MR475) != 0 && sub(mode, MR515) != 0)
697           {
698              pit_flag = 0;
699           }
700        }
701
702        // pitch index
703        index = *parm++;
704
705         *-------------------------------------------------------*
706         * - decode pitch lag and find adaptive codebook vector. *
707         *-------------------------------------------------------*
708
709        if (sub(mode, MR122) != 0)
710        {
711           // flag4 indicates encoding with 4 bit resolution;
712           // this is needed for mode MR475, MR515, MR59 and MR67
713
714           flag4 = 0;
715           if ((sub (mode, MR475) == 0) ||
716               (sub (mode, MR515) == 0) ||
717               (sub (mode, MR59) == 0) ||
718               (sub (mode, MR67) == 0) ) {
719              flag4 = 1;
720           }
721
722            *-------------------------------------------------------*
723            * - get ranges for the t0_min and t0_max                *
724            * - only needed in delta decoding                       *
725            *-------------------------------------------------------*
726
727           delta_frc_low = 5;
728           delta_frc_range = 9;
729
730           if ( sub(mode, MR795) == 0 )
731           {
732              delta_frc_low = 10;
733              delta_frc_range = 19;
734           }
735
736           t0_min = sub(st->old_T0, delta_frc_low);
737           if (sub(t0_min, PIT_MIN) < 0)
738           {
739              t0_min = PIT_MIN;
740           }
741           t0_max = add(t0_min, delta_frc_range);
742           if (sub(t0_max, PIT_MAX) > 0)
743           {
744              t0_max = PIT_MAX;
745              t0_min = sub(t0_max, delta_frc_range);
746           }
747
748           Dec_lag3 (index, t0_min, t0_max, pit_flag, st->old_T0,
749                     &T0, &T0_frac, flag4);
750
751           st->T0_lagBuff = T0;
752
753           if (bfi != 0)
754           {
755              if (sub (st->old_T0, PIT_MAX) < 0)
756              {                                      // Graceful pitch
757                 st->old_T0 = add(st->old_T0, 1);    // degradation
758              }
759              T0 = st->old_T0;
760              T0_frac = 0;
761
762              if ( st->inBackgroundNoise != 0 &&
763                   sub(st->voicedHangover, 4) > 0 &&
764                   ((sub(mode, MR475) == 0 ) ||
765                    (sub(mode, MR515) == 0 ) ||
766                    (sub(mode, MR59) == 0) )
767                   )
768              {
769                 T0 = st->T0_lagBuff;
770              }
771           }
772
773           Pred_lt_3or6 (st->exc, T0, T0_frac, L_SUBFR, 1);
774        }
775        else
776        {
777           Dec_lag6 (index, PIT_MIN_MR122,
778                     PIT_MAX, pit_flag, &T0, &T0_frac);
779
780           if ( bfi == 0 && (pit_flag == 0 || sub (index, 61) < 0))
781           {
782           }
783           else
784           {
785              st->T0_lagBuff = T0;
786              T0 = st->old_T0;
787              T0_frac = 0;
788           }
789
790           Pred_lt_3or6 (st->exc, T0, T0_frac, L_SUBFR, 0);
791        }
792
793         *-------------------------------------------------------*
794         * - (MR122 only: Decode pitch gain.)                    *
795         * - Decode innovative codebook.                         *
796         * - set pitch sharpening factor                         *
797         *-------------------------------------------------------*
798
799         if (sub (mode, MR475) == 0 || sub (mode, MR515) == 0)
800         {   // MR475, MR515
801            index = *parm++;        // index of position
802            i = *parm++;            // signs
803
804            decode_2i40_9bits (subfrNr, i, index, code);
805
806            pit_sharp = shl (st->sharp, 1);
807         }
808         else if (sub (mode, MR59) == 0)
809         {   // MR59
810            index = *parm++;        // index of position
811            i = *parm++;            // signs
812
813            decode_2i40_11bits (i, index, code);
814
815            pit_sharp = shl (st->sharp, 1);
816         }
817         else if (sub (mode, MR67) == 0)
818         {   // MR67
819            index = *parm++;        // index of position
820            i = *parm++;            // signs
821
822            decode_3i40_14bits (i, index, code);
823
824            pit_sharp = shl (st->sharp, 1);
825         }
826         else if (sub (mode, MR795) <= 0)
827         {   // MR74, MR795
828            index = *parm++;        // index of position
829            i = *parm++;            // signs
830
831            decode_4i40_17bits (i, index, code);
832
833            pit_sharp = shl (st->sharp, 1);
834         }
835         else if (sub (mode, MR102) == 0)
836         {  // MR102
837            dec_8i40_31bits (parm, code);
838            parm += 7;
839
840            pit_sharp = shl (st->sharp, 1);
841         }
842         else
843         {  // MR122
844            index = *parm++;
845            if (bfi != 0)
846            {
847               ec_gain_pitch (st->ec_gain_p_st, st->state, &gain_pit);
848            }
849            else
850            {
851               gain_pit = d_gain_pitch (mode, index);
852            }
853            ec_gain_pitch_update (st->ec_gain_p_st, bfi, st->prev_bf,
854                                  &gain_pit);
855
856            dec_10i40_35bits (parm, code);
857            parm += 10;
858
859            // pit_sharp = gain_pit;
860            // if (pit_sharp > 1.0) pit_sharp = 1.0;
861
862            pit_sharp = shl (gain_pit, 1);
863         }
864
865          *-------------------------------------------------------*
866          * - Add the pitch contribution to code[].               *
867          *-------------------------------------------------------*
868         for (i = T0; i < L_SUBFR; i++)
869         {
870            temp = mult (code[i - T0], pit_sharp);
871            code[i] = add (code[i], temp);
872         }
873
874          *------------------------------------------------------------*
875          * - Decode codebook gain (MR122) or both pitch               *
876          *   gain and codebook gain (all others)                      *
877          * - Update pitch sharpening "sharp" with quantized gain_pit  *
878          *------------------------------------------------------------*
879
880         if (sub (mode, MR475) == 0)
881         {
882            // read and decode pitch and code gain
883            if (evenSubfr != 0)
884            {
885               index_mr475 = *parm++; // index of gain(s)
886            }
887
888            if (bfi == 0)
889            {
890               Dec_gain(st->pred_state, mode, index_mr475, code,
891                        evenSubfr, &gain_pit, &gain_code);
892            }
893            else
894            {
895               ec_gain_pitch (st->ec_gain_p_st, st->state, &gain_pit);
896               ec_gain_code (st->ec_gain_c_st, st->pred_state, st->state,
897                             &gain_code);
898            }
899            ec_gain_pitch_update (st->ec_gain_p_st, bfi, st->prev_bf,
900                                  &gain_pit);
901            ec_gain_code_update (st->ec_gain_c_st, bfi, st->prev_bf,
902                                 &gain_code);
903
904            pit_sharp = gain_pit;
905            if (sub (pit_sharp, SHARPMAX) > 0)
906            {
907                pit_sharp = SHARPMAX;
908            }
909
910         }
911         else if ((sub (mode, MR74) <= 0) ||
912                  (sub (mode, MR102) == 0))
913         {
914             // read and decode pitch and code gain
915             index = *parm++; // index of gain(s)
916
917             if (bfi == 0)
918             {
919                Dec_gain(st->pred_state, mode, index, code,
920                         evenSubfr, &gain_pit, &gain_code);
921             }
922             else
923             {
924                ec_gain_pitch (st->ec_gain_p_st, st->state, &gain_pit);
925                ec_gain_code (st->ec_gain_c_st, st->pred_state, st->state,
926                              &gain_code);
927             }
928             ec_gain_pitch_update (st->ec_gain_p_st, bfi, st->prev_bf,
929                                   &gain_pit);
930             ec_gain_code_update (st->ec_gain_c_st, bfi, st->prev_bf,
931                                  &gain_code);
932
933             pit_sharp = gain_pit;
934             if (sub (pit_sharp, SHARPMAX) > 0)
935             {
936                pit_sharp = SHARPMAX;
937             }
938
939             if (sub (mode, MR102) == 0)
940             {
941                if (sub (st->old_T0, add(L_SUBFR, 5)) > 0)
942                {
943                   pit_sharp = shr(pit_sharp, 2);
944                }
945             }
946         }
947         else
948         {
949            // read and decode pitch gain
950            index = *parm++; // index of gain(s)
951
952            if (sub (mode, MR795) == 0)
953            {
954               // decode pitch gain
955               if (bfi != 0)
956               {
957                  ec_gain_pitch (st->ec_gain_p_st, st->state, &gain_pit);
958               }
959               else
960               {
961                  gain_pit = d_gain_pitch (mode, index);
962               }
963               ec_gain_pitch_update (st->ec_gain_p_st, bfi, st->prev_bf,
964                                     &gain_pit);
965
966               // read and decode code gain
967               index = *parm++;
968               if (bfi == 0)
969               {
970                  d_gain_code (st->pred_state, mode, index, code, &gain_code);
971               }
972               else
973               {
974                  ec_gain_code (st->ec_gain_c_st, st->pred_state, st->state,
975                                &gain_code);
976               }
977               ec_gain_code_update (st->ec_gain_c_st, bfi, st->prev_bf,
978                                    &gain_code);
979
980               pit_sharp = gain_pit;
981               if (sub (pit_sharp, SHARPMAX) > 0)
982               {
983                  pit_sharp = SHARPMAX;
984               }
985            }
986            else
987            { // MR122
988               if (bfi == 0)
989               {
990                  d_gain_code (st->pred_state, mode, index, code, &gain_code);
991               }
992               else
993               {
994                  ec_gain_code (st->ec_gain_c_st, st->pred_state, st->state,
995                                &gain_code);
996               }
997               ec_gain_code_update (st->ec_gain_c_st, bfi, st->prev_bf,
998                                    &gain_code);
999
1000               pit_sharp = gain_pit;
1001            }
1002         }
1003
1004         // store pitch sharpening for next subframe
1005         // (for modes which use the previous pitch gain for
1006         // pitch sharpening in the search phase)
1007         // do not update sharpening in even subframes for MR475
1008         if (sub(mode, MR475) != 0 || evenSubfr == 0)
1009         {
1010             st->sharp = gain_pit;
1011             if (sub (st->sharp, SHARPMAX) > 0)
1012             {
1013                 st->sharp = SHARPMAX;
1014             }
1015         }
1016
1017         pit_sharp = shl (pit_sharp, 1);
1018         if (sub (pit_sharp, 16384) > 0)
1019         {
1020            for (i = 0; i < L_SUBFR; i++)
1021             {
1022                temp = mult (st->exc[i], pit_sharp);
1023                L_temp = L_mult (temp, gain_pit);
1024                if (sub(mode, MR122)==0)
1025                {
1026                   L_temp = L_shr (L_temp, 1);
1027                }
1028                excp[i] = pv_round (L_temp);
1029             }
1030         }
1031
1032          *-------------------------------------------------------*
1033          * - Store list of LTP gains needed in the source        *
1034          *   characteristic detector (SCD)                       *
1035          *-------------------------------------------------------*
1036         if ( bfi == 0 )
1037         {
1038            for (i = 0; i < 8; i++)
1039            {
1040               st->ltpGainHistory[i] = st->ltpGainHistory[i+1];
1041            }
1042            st->ltpGainHistory[8] = gain_pit;
1043         }
1044
1045          *-------------------------------------------------------*
1046          * - Limit gain_pit if in background noise and BFI       *
1047          *   for MR475, MR515, MR59                              *
1048          *-------------------------------------------------------*
1049
1050         if ( (st->prev_bf != 0 || bfi != 0) && st->inBackgroundNoise != 0 &&
1051              ((sub(mode, MR475) == 0) ||
1052               (sub(mode, MR515) == 0) ||
1053               (sub(mode, MR59) == 0))
1054              )
1055         {
1056            if ( sub (gain_pit, 12288) > 0)    // if (gain_pit > 0.75) in Q14
1057               gain_pit = add( shr( sub(gain_pit, 12288), 1 ), 12288 );
1058               // gain_pit = (gain_pit-0.75)/2.0 + 0.75;
1059
1060            if ( sub (gain_pit, 14745) > 0)    // if (gain_pit > 0.90) in Q14
1061            {
1062               gain_pit = 14745;
1063            }
1064         }
1065
1066          *-------------------------------------------------------*
1067          *  Calculate CB mixed gain                              *
1068          *-------------------------------------------------------*
1069         Int_lsf(prev_lsf, st->lsfState->past_lsf_q, i_subfr, lsf_i);
1070         gain_code_mix = Cb_gain_average(
1071             st->Cb_gain_averState, mode, gain_code,
1072             lsf_i, st->lsp_avg_st->lsp_meanSave, bfi,
1073             st->prev_bf, pdfi, st->prev_pdf,
1074             st->inBackgroundNoise, st->voicedHangover);
1075
1076         // make sure that MR74, MR795, MR122 have original code_gain
1077         if ((sub(mode, MR67) > 0) && (sub(mode, MR102) != 0) )
1078            // MR74, MR795, MR122
1079         {
1080            gain_code_mix = gain_code;
1081         }
1082
1083          *-------------------------------------------------------*
1084          * - Find the total excitation.                          *
1085          * - Find synthesis speech corresponding to st->exc[].   *
1086          *-------------------------------------------------------*
1087         if (sub(mode, MR102) <= 0) // MR475, MR515, MR59, MR67, MR74, MR795, MR102
1088         {
1089            pitch_fac = gain_pit;
1090            tmp_shift = 1;
1091         }
1092         else       // MR122
1093         {
1094            pitch_fac = shr (gain_pit, 1);
1095            tmp_shift = 2;
1096         }
1097
1098         // copy unscaled LTP excitation to exc_enhanced (used in phase
1099          * dispersion below) and compute total excitation for LTP feedback
1100
1101         for (i = 0; i < L_SUBFR; i++)
1102         {
1103            exc_enhanced[i] = st->exc[i];
1104
1105            // st->exc[i] = gain_pit*st->exc[i] + gain_code*code[i];
1106            L_temp = L_mult (st->exc[i], pitch_fac);
1107                                                       // 12.2: Q0 * Q13
1108                                                       //  7.4: Q0 * Q14
1109            L_temp = L_mac (L_temp, code[i], gain_code);
1110                                                       // 12.2: Q12 * Q1
1111                                                       //  7.4: Q13 * Q1
1112            L_temp = L_shl (L_temp, tmp_shift);                   // Q16
1113            st->exc[i] = pv_round (L_temp);
1114         }
1115
1116          *-------------------------------------------------------*
1117          * - Adaptive phase dispersion                           *
1118          *-------------------------------------------------------*
1119         ph_disp_release(st->ph_disp_st); // free phase dispersion adaption
1120
1121         if ( ((sub(mode, MR475) == 0) ||
1122               (sub(mode, MR515) == 0) ||
1123               (sub(mode, MR59) == 0))   &&
1124              sub(st->voicedHangover, 3) > 0 &&
1125              st->inBackgroundNoise != 0 &&
1126              bfi != 0 )
1127         {
1128            ph_disp_lock(st->ph_disp_st); // Always Use full Phase Disp.
1129         }                                // if error in bg noise
1130
1131         // apply phase dispersion to innovation (if enabled) and
1132            compute total excitation for synthesis part
1133         ph_disp(st->ph_disp_st, mode,
1134                 exc_enhanced, gain_code_mix, gain_pit, code,
1135                 pitch_fac, tmp_shift);
1136
1137          *-------------------------------------------------------*
1138          * - The Excitation control module are active during BFI.*
1139          * - Conceal drops in signal energy if in bg noise.      *
1140          *-------------------------------------------------------*
1141
1142         L_temp = 0;
1143         for (i = 0; i < L_SUBFR; i++)
1144         {
1145             L_temp = L_mac (L_temp, exc_enhanced[i], exc_enhanced[i] );
1146         }
1147
1148         L_temp = L_shr (L_temp, 1);     // excEnergy = sqrt(L_temp) in Q0
1149         L_temp = sqrt_l_exp(L_temp, &temp); // function result
1150         L_temp = L_shr(L_temp, add( shr(temp, 1), 15));
1151         L_temp = L_shr(L_temp, 2);       // To cope with 16-bit and
1152         excEnergy = extract_l(L_temp);   // scaling in ex_ctrl()
1153
1154         if ( ((sub (mode, MR475) == 0) ||
1155               (sub (mode, MR515) == 0) ||
1156               (sub (mode, MR59) == 0))  &&
1157              sub(st->voicedHangover, 5) > 0 &&
1158              st->inBackgroundNoise != 0 &&
1159              sub(st->state, 4) < 0 &&
1160              ( (pdfi != 0 && st->prev_pdf != 0) ||
1161                 bfi != 0 ||
1162                 st->prev_bf != 0) )
1163         {
1164            carefulFlag = 0;
1165            if ( pdfi != 0 && bfi == 0 )
1166            {
1167               carefulFlag = 1;
1168            }
1169
1170            Ex_ctrl(exc_enhanced,
1171                    excEnergy,
1172                    st->excEnergyHist,
1173                    st->voicedHangover,
1174                    st->prev_bf,
1175                    carefulFlag);
1176         }
1177
1178         if ( st->inBackgroundNoise != 0 &&
1179              ( bfi != 0 || st->prev_bf != 0 ) &&
1180              sub(st->state, 4) < 0 )
1181         {
1182            ; // do nothing!
1183         }
1184         else
1185         {
1186            // Update energy history for all modes
1187            for (i = 0; i < 8; i++)
1188            {
1189               st->excEnergyHist[i] = st->excEnergyHist[i+1];
1190            }
1191            st->excEnergyHist[8] = excEnergy;
1192         }
1193          *-------------------------------------------------------*
1194          * Excitation control module end.                        *
1195          *-------------------------------------------------------*
1196
1197         if (sub (pit_sharp, 16384) > 0)
1198         {
1199            for (i = 0; i < L_SUBFR; i++)
1200            {
1201               excp[i] = add (excp[i], exc_enhanced[i]);
1202            }
1203            agc2 (exc_enhanced, excp, L_SUBFR);
1204            Overflow = 0;
1205            Syn_filt (Az, excp, &synth[i_subfr], L_SUBFR,
1206                      st->mem_syn, 0);
1207         }
1208         else
1209         {
1210            Overflow = 0;
1211            Syn_filt (Az, exc_enhanced, &synth[i_subfr], L_SUBFR,
1212                      st->mem_syn, 0);
1213         }
1214
1215         if (Overflow != 0)    // Test for overflow
1216         {
1217            for (i = 0; i < PIT_MAX + L_INTERPOL + L_SUBFR; i++)
1218            {
1219               st->old_exc[i] = shr(st->old_exc[i], 2);
1220            }
1221            for (i = 0; i < L_SUBFR; i++)
1222            {
1223               exc_enhanced[i] = shr(exc_enhanced[i], 2);
1224            }
1225            Syn_filt(Az, exc_enhanced, &synth[i_subfr], L_SUBFR, st->mem_syn, 1);
1226         }
1227         else
1228         {
1229            Copy(&synth[i_subfr+L_SUBFR-M], st->mem_syn, M);
1230         }
1231
1232          *--------------------------------------------------*
1233          * Update signal for next frame.                    *
1234          * -> shift to the left by L_SUBFR  st->exc[]       *
1235          *--------------------------------------------------*
1236
1237         Copy (&st->old_exc[L_SUBFR], &st->old_exc[0], PIT_MAX + L_INTERPOL);
1238
1239         // interpolated LPC parameters for next subframe
1240         Az += MP1;
1241
1242         // store T0 for next subframe
1243         st->old_T0 = T0;
1244     }
1245
1246      *-------------------------------------------------------*
1247      * Call the Source Characteristic Detector which updates *
1248      * st->inBackgroundNoise and st->voicedHangover.         *
1249      *-------------------------------------------------------*
1250
1251     st->inBackgroundNoise = Bgn_scd(st->background_state,
1252                                     &(st->ltpGainHistory[0]),
1253                                     &(synth[0]),
1254                                     &(st->voicedHangover) );
1255
1256     dtx_dec_activity_update(st->dtxDecoderState,
1257                             st->lsfState->past_lsf_q,
1258                             synth);
1259
1260     // store bfi for next subframe
1261     st->prev_bf = bfi;
1262     st->prev_pdf = pdfi;
1263
1264      *--------------------------------------------------*
1265      * Calculate the LSF averages on the eight          *
1266      * previous frames                                  *
1267      *--------------------------------------------------*
1268
1269     lsp_avg(st->lsp_avg_st, st->lsfState->past_lsf_q);
1270
1271 the_end:
1272     st->dtxDecoderState->dtxGlobalState = newDTXState;
1273
1274     return 0;
1275 }
1276
1277 ------------------------------------------------------------------------------
1278  CAUTION [optional]
1279  [State any special notes, constraints or cautions for users of this function]
1280
1281 ------------------------------------------------------------------------------
1282 */
1283
1284 void Decoder_amr(
1285     Decoder_amrState *st,      /* i/o : State variables                   */
1286     enum Mode mode,            /* i   : AMR mode                          */
1287     Word16 parm[],             /* i   : vector of synthesis parameters
1288                                         (PRM_SIZE)                        */
1289     enum RXFrameType frame_type, /* i   : received frame type             */
1290     Word16 synth[],            /* o   : synthesis speech (L_FRAME)        */
1291     Word16 A_t[]               /* o   : decoded LP filter in 4 subframes
1292                                         (AZ_SIZE)                         */
1293 )
1294 {
1295     /* LPC coefficients */
1296
1297     Word16 *Az;                /* Pointer on A_t */
1298
1299     /* LSPs */
1300
1301     Word16 lsp_new[M];
1302     Word16 lsp_mid[M];
1303
1304     /* LSFs */
1305
1306     Word16 prev_lsf[M];
1307     Word16 lsf_i[M];
1308
1309     /* Algebraic codevector */
1310
1311     Word16 code[L_SUBFR];
1312
1313     /* excitation */
1314
1315     Word16 excp[L_SUBFR];
1316     Word16 exc_enhanced[L_SUBFR];
1317
1318     /* Scalars */
1319
1320     Word16 i;
1321     Word16 i_subfr;
1322     Word16 T0;
1323     Word16 T0_frac;
1324     Word16 index;
1325     Word16 index_mr475 = 0;
1326     Word16 gain_pit;
1327     Word16 gain_code;
1328     Word16 gain_code_mix;
1329     Word16 pit_sharp;
1330     Word16 pit_flag;
1331     Word16 pitch_fac;
1332     Word16 t0_min;
1333     Word16 t0_max;
1334     Word16 delta_frc_low;
1335     Word16 delta_frc_range;
1336     Word16 tmp_shift;
1337     Word16 temp;
1338     Word32 L_temp;
1339     Word16 flag4;
1340     Word16 carefulFlag;
1341     Word16 excEnergy;
1342     Word16 subfrNr;
1343     Word16 evenSubfr = 0;
1344
1345     Word16 bfi = 0;   /* bad frame indication flag                          */
1346     Word16 pdfi = 0;  /* potential degraded bad frame flag                  */
1347
1348     enum DTXStateType newDTXState;  /* SPEECH , DTX, DTX_MUTE */
1349     Flag   *pOverflow = &(st->overflow);     /* Overflow flag            */
1350
1351
1352     /* find the new  DTX state  SPEECH OR DTX */
1353     newDTXState = rx_dtx_handler(&(st->dtxDecoderState), frame_type, pOverflow);
1354
1355     /* DTX actions */
1356
1357     if (newDTXState != SPEECH)
1358     {
1359         Decoder_amr_reset(st, MRDTX);
1360
1361         dtx_dec(&(st->dtxDecoderState),
1362                 st->mem_syn,
1363                 &(st->lsfState),
1364                 &(st->pred_state),
1365                 &(st->Cb_gain_averState),
1366                 newDTXState,
1367                 mode,
1368                 parm, &(st->common_amr_tbls), synth, A_t, pOverflow);
1369
1370         /* update average lsp */
1371         Lsf_lsp(
1372             st->lsfState.past_lsf_q,
1373             st->lsp_old,
1374             M,
1375             pOverflow);
1376
1377         lsp_avg(
1378             &(st->lsp_avg_st),
1379             st->lsfState.past_lsf_q,
1380             pOverflow);
1381
1382         goto the_end;
1383     }
1384
1385     /* SPEECH action state machine  */
1386     if ((frame_type == RX_SPEECH_BAD) || (frame_type == RX_NO_DATA) ||
1387             (frame_type == RX_ONSET))
1388     {
1389         bfi = 1;
1390
1391         if ((frame_type == RX_NO_DATA) || (frame_type == RX_ONSET))
1392         {
1393             build_CN_param(&st->nodataSeed,
1394                            st->common_amr_tbls.prmno_ptr[mode],
1395                            st->common_amr_tbls.bitno_ptr[mode],
1396                            parm,
1397                            st->common_amr_tbls.window_200_40_ptr,
1398                            pOverflow);
1399         }
1400     }
1401     else if (frame_type == RX_SPEECH_DEGRADED)
1402     {
1403         pdfi = 1;
1404     }
1405
1406     if (bfi != 0)
1407     {
1408         st->state += 1;
1409     }
1410     else if (st->state == 6)
1411
1412     {
1413         st->state = 5;
1414     }
1415     else
1416     {
1417         st->state = 0;
1418     }
1419
1420
1421     if (st->state > 6)
1422     {
1423         st->state = 6;
1424     }
1425
1426     /* If this frame is the first speech frame after CNI period,     */
1427     /* set the BFH state machine to an appropriate state depending   */
1428     /* on whether there was DTX muting before start of speech or not */
1429     /* If there was DTX muting, the first speech frame is muted.     */
1430     /* If there was no DTX muting, the first speech frame is not     */
1431     /* muted. The BFH state machine starts from state 5, however, to */
1432     /* keep the audible noise resulting from a SID frame which is    */
1433     /* erroneously interpreted as a good speech frame as small as    */
1434     /* possible (the decoder output in this case is quickly muted)   */
1435
1436     if (st->dtxDecoderState.dtxGlobalState == DTX)
1437     {
1438         st->state = 5;
1439         st->prev_bf = 0;
1440     }
1441     else if (st->dtxDecoderState.dtxGlobalState == DTX_MUTE)
1442     {
1443         st->state = 5;
1444         st->prev_bf = 1;
1445     }
1446
1447     /* save old LSFs for CB gain smoothing */
1448     oscl_memmove((void *)prev_lsf, st->lsfState.past_lsf_q, M*sizeof(*st->lsfState.past_lsf_q));
1449
1450     /* decode LSF parameters and generate interpolated lpc coefficients
1451        for the 4 subframes */
1452
1453     if (mode != MR122)
1454     {
1455         D_plsf_3(
1456             &(st->lsfState),
1457             mode,
1458             bfi,
1459             parm,
1460             &st->common_amr_tbls,
1461             lsp_new,
1462             pOverflow);
1463
1464         /* Advance synthesis parameters pointer */
1465         parm += 3;
1466
1467         Int_lpc_1to3(
1468             st->lsp_old,
1469             lsp_new,
1470             A_t,
1471             pOverflow);
1472     }
1473     else
1474     {
1475         D_plsf_5(
1476             &(st->lsfState),
1477             bfi,
1478             parm,
1479             &(st->common_amr_tbls),
1480             lsp_mid,
1481             lsp_new,
1482             pOverflow);
1483
1484         /* Advance synthesis parameters pointer */
1485         parm += 5;
1486
1487         Int_lpc_1and3(
1488             st->lsp_old,
1489             lsp_mid,
1490             lsp_new,
1491             A_t,
1492             pOverflow);
1493     }
1494
1495     /* update the LSPs for the next frame */
1496     for (i = 0; i < M; i++)
1497     {
1498         st->lsp_old[i] = lsp_new[i];
1499     }
1500
1501     /*------------------------------------------------------------------------*
1502      *          Loop for every subframe in the analysis frame                 *
1503      *------------------------------------------------------------------------*
1504      * The subframe size is L_SUBFR and the loop is repeated L_FRAME/L_SUBFR  *
1505      *  times                                                                 *
1506      *     - decode the pitch delay                                           *
1507      *     - decode algebraic code                                            *
1508      *     - decode pitch and codebook gains                                  *
1509      *     - find the excitation and compute synthesis speech                 *
1510      *------------------------------------------------------------------------*/
1511
1512     /* pointer to interpolated LPC parameters */
1513     Az = A_t;
1514
1515     evenSubfr = 0;
1516     subfrNr = -1;
1517     for (i_subfr = 0; i_subfr < L_FRAME; i_subfr += L_SUBFR)
1518     {
1519         subfrNr += 1;
1520         evenSubfr = 1 - evenSubfr;
1521
1522         /* flag for first and 3th subframe */
1523         pit_flag = i_subfr;
1524
1525
1526         if (i_subfr == L_FRAME_BY2)
1527         {
1528             if ((mode != MR475) && (mode != MR515))
1529             {
1530                 pit_flag = 0;
1531             }
1532         }
1533
1534         /* pitch index */
1535         index = *parm++;
1536
1537         /*-------------------------------------------------------*
1538         * - decode pitch lag and find adaptive codebook vector. *
1539         *-------------------------------------------------------*/
1540
1541         if (mode != MR122)
1542         {
1543             /* flag4 indicates encoding with 4 bit resolution;     */
1544             /* this is needed for mode MR475, MR515, MR59 and MR67 */
1545
1546             flag4 = 0;
1547
1548             if ((mode == MR475) || (mode == MR515) || (mode == MR59) ||
1549                     (mode == MR67))
1550             {
1551                 flag4 = 1;
1552             }
1553
1554             /*-------------------------------------------------------*
1555             * - get ranges for the t0_min and t0_max                *
1556             * - only needed in delta decoding                       *
1557             *-------------------------------------------------------*/
1558
1559             delta_frc_low = 5;
1560             delta_frc_range = 9;
1561
1562             if (mode == MR795)
1563             {
1564                 delta_frc_low = 10;
1565                 delta_frc_range = 19;
1566             }
1567
1568             t0_min = st->old_T0 - delta_frc_low;
1569
1570             if (t0_min < PIT_MIN)
1571             {
1572                 t0_min = PIT_MIN;
1573             }
1574             t0_max = t0_min + delta_frc_range;
1575
1576             if (t0_max > PIT_MAX)
1577             {
1578                 t0_max = PIT_MAX;
1579                 t0_min = t0_max - delta_frc_range;
1580             }
1581
1582             Dec_lag3(index, t0_min, t0_max, pit_flag, st->old_T0,
1583                      &T0, &T0_frac, flag4, pOverflow);
1584
1585             st->T0_lagBuff = T0;
1586
1587             if (bfi != 0)
1588             {
1589                 if (st->old_T0 < PIT_MAX)
1590                 {                               /* Graceful pitch */
1591                     st->old_T0 += 1;            /* degradation    */
1592                 }
1593                 T0 = st->old_T0;
1594                 T0_frac = 0;
1595
1596                 if ((st->inBackgroundNoise != 0) && (st->voicedHangover > 4) &&
1597                         ((mode == MR475) || (mode == MR515) || (mode == MR59)))
1598                 {
1599                     T0 = st->T0_lagBuff;
1600                 }
1601             }
1602
1603             Pred_lt_3or6(st->exc, T0, T0_frac, L_SUBFR, 1, pOverflow);
1604         }
1605         else
1606         {
1607             Dec_lag6(index, PIT_MIN_MR122,
1608                      PIT_MAX, pit_flag, &T0, &T0_frac, pOverflow);
1609
1610
1611             if (!(bfi == 0 && (pit_flag == 0 || index < 61)))
1612             {
1613                 st->T0_lagBuff = T0;
1614                 T0 = st->old_T0;
1615                 T0_frac = 0;
1616             }
1617
1618             Pred_lt_3or6(st->exc, T0, T0_frac, L_SUBFR, 0, pOverflow);
1619         }
1620
1621         /*-------------------------------------------------------*
1622          * - (MR122 only: Decode pitch gain.)                    *
1623          * - Decode innovative codebook.                         *
1624          * - set pitch sharpening factor                         *
1625          *-------------------------------------------------------*/
1626         if ((mode == MR475) || (mode == MR515))
1627         {   /* MR475, MR515 */
1628             index = *parm++;        /* index of position */
1629             i = *parm++;            /* signs             */
1630
1631             decode_2i40_9bits(subfrNr, i, index, st->common_amr_tbls.startPos_ptr, code, pOverflow);
1632
1633             L_temp = (Word32)st->sharp << 1;
1634             if (L_temp != (Word32)((Word16) L_temp))
1635             {
1636                 pit_sharp = (st->sharp > 0) ? MAX_16 : MIN_16;
1637             }
1638             else
1639             {
1640                 pit_sharp = (Word16) L_temp;
1641             }
1642         }
1643         else if (mode == MR59)
1644         {   /* MR59 */
1645             index = *parm++;        /* index of position */
1646             i = *parm++;            /* signs             */
1647
1648             decode_2i40_11bits(i, index, code);
1649
1650             L_temp = (Word32)st->sharp << 1;
1651             if (L_temp != (Word32)((Word16) L_temp))
1652             {
1653                 pit_sharp = (st->sharp > 0) ? MAX_16 : MIN_16;
1654             }
1655             else
1656             {
1657                 pit_sharp = (Word16) L_temp;
1658             }
1659         }
1660         else if (mode == MR67)
1661         {   /* MR67 */
1662             index = *parm++;        /* index of position */
1663             i = *parm++;            /* signs             */
1664
1665             decode_3i40_14bits(i, index, code);
1666
1667             L_temp = (Word32)st->sharp << 1;
1668             if (L_temp != (Word32)((Word16) L_temp))
1669             {
1670                 pit_sharp = (st->sharp > 0) ? MAX_16 : MIN_16;
1671             }
1672             else
1673             {
1674                 pit_sharp = (Word16) L_temp;
1675             }
1676         }
1677         else if (mode <= MR795)
1678         {   /* MR74, MR795 */
1679             index = *parm++;        /* index of position */
1680             i = *parm++;            /* signs             */
1681
1682             decode_4i40_17bits(i, index, st->common_amr_tbls.dgray_ptr, code);
1683
1684             L_temp = (Word32)st->sharp << 1;
1685             if (L_temp != (Word32)((Word16) L_temp))
1686             {
1687                 pit_sharp = (st->sharp > 0) ? MAX_16 : MIN_16;
1688             }
1689             else
1690             {
1691                 pit_sharp = (Word16) L_temp;
1692             }
1693         }
1694         else if (mode == MR102)
1695         {  /* MR102 */
1696             dec_8i40_31bits(parm, code, pOverflow);
1697             parm += 7;
1698
1699             L_temp = (Word32)st->sharp << 1;
1700             if (L_temp != (Word32)((Word16) L_temp))
1701             {
1702                 pit_sharp = (st->sharp > 0) ? MAX_16 : MIN_16;
1703             }
1704             else
1705             {
1706                 pit_sharp = (Word16) L_temp;
1707             }
1708         }
1709         else
1710         {  /* MR122 */
1711             index = *parm++;
1712
1713             if (bfi != 0)
1714             {
1715                 ec_gain_pitch(
1716                     &(st->ec_gain_p_st),
1717                     st->state,
1718                     &gain_pit,
1719                     pOverflow);
1720             }
1721             else
1722             {
1723                 gain_pit = d_gain_pitch(mode, index, st->common_amr_tbls.qua_gain_pitch_ptr);
1724             }
1725             ec_gain_pitch_update(
1726                 &(st->ec_gain_p_st),
1727                 bfi,
1728                 st->prev_bf,
1729                 &gain_pit,
1730                 pOverflow);
1731
1732
1733             dec_10i40_35bits(parm, code, st->common_amr_tbls.dgray_ptr);
1734             parm += 10;
1735
1736             /* pit_sharp = gain_pit;                   */
1737             /* if (pit_sharp > 1.0) pit_sharp = 1.0;   */
1738
1739             L_temp = (Word32)gain_pit << 1;
1740             if (L_temp != (Word32)((Word16) L_temp))
1741             {
1742                 pit_sharp = (gain_pit > 0) ? MAX_16 : MIN_16;
1743             }
1744             else
1745             {
1746                 pit_sharp = (Word16) L_temp;
1747             }
1748         }
1749         /*-------------------------------------------------------*
1750          * - Add the pitch contribution to code[].               *
1751          *-------------------------------------------------------*/
1752         for (i = T0; i < L_SUBFR; i++)
1753         {
1754             temp = mult(*(code + i - T0), pit_sharp, pOverflow);
1755             *(code + i) = add_16(*(code + i), temp, pOverflow);
1756
1757         }
1758
1759         /*------------------------------------------------------------*
1760          * - Decode codebook gain (MR122) or both pitch               *
1761          *   gain and codebook gain (all others)                      *
1762          * - Update pitch sharpening "sharp" with quantized gain_pit  *
1763          *------------------------------------------------------------*/
1764         if (mode == MR475)
1765         {
1766             /* read and decode pitch and code gain */
1767
1768             if (evenSubfr != 0)
1769             {
1770                 index_mr475 = *parm++;         /* index of gain(s) */
1771             }
1772
1773             if (bfi == 0)
1774             {
1775                 Dec_gain(
1776                     &(st->pred_state),
1777                     mode,
1778                     index_mr475,
1779                     code,
1780                     evenSubfr,
1781                     &gain_pit,
1782                     &gain_code,
1783                     &(st->common_amr_tbls),
1784                     pOverflow);
1785             }
1786             else
1787             {
1788                 ec_gain_pitch(
1789                     &(st->ec_gain_p_st),
1790                     st->state,
1791                     &gain_pit,
1792                     pOverflow);
1793
1794                 ec_gain_code(
1795                     &(st->ec_gain_c_st),
1796                     &(st->pred_state),
1797                     st->state,
1798                     &gain_code,
1799                     pOverflow);
1800             }
1801             ec_gain_pitch_update(
1802                 &st->ec_gain_p_st,
1803                 bfi,
1804                 st->prev_bf,
1805                 &gain_pit,
1806                 pOverflow);
1807
1808             ec_gain_code_update(
1809                 &st->ec_gain_c_st,
1810                 bfi,
1811                 st->prev_bf,
1812                 &gain_code,
1813                 pOverflow);
1814
1815             pit_sharp = gain_pit;
1816
1817             if (pit_sharp > SHARPMAX)
1818             {
1819                 pit_sharp = SHARPMAX;
1820             }
1821
1822         }
1823         else if ((mode <= MR74) || (mode == MR102))
1824         {
1825             /* read and decode pitch and code gain */
1826             index = *parm++;                 /* index of gain(s) */
1827
1828             if (bfi == 0)
1829             {
1830                 Dec_gain(
1831                     &(st->pred_state),
1832                     mode,
1833                     index,
1834                     code,
1835                     evenSubfr,
1836                     &gain_pit,
1837                     &gain_code,
1838                     &(st->common_amr_tbls),
1839                     pOverflow);
1840             }
1841             else
1842             {
1843                 ec_gain_pitch(
1844                     &(st->ec_gain_p_st),
1845                     st->state,
1846                     &gain_pit,
1847                     pOverflow);
1848
1849                 ec_gain_code(
1850                     &(st->ec_gain_c_st),
1851                     &(st->pred_state),
1852                     st->state,
1853                     &gain_code,
1854                     pOverflow);
1855             }
1856
1857             ec_gain_pitch_update(
1858                 &(st->ec_gain_p_st),
1859                 bfi,
1860                 st->prev_bf,
1861                 &gain_pit,
1862                 pOverflow);
1863
1864             ec_gain_code_update(
1865                 &(st->ec_gain_c_st),
1866                 bfi,
1867                 st->prev_bf,
1868                 &gain_code,
1869                 pOverflow);
1870
1871             pit_sharp = gain_pit;
1872
1873             if (pit_sharp > SHARPMAX)
1874             {
1875                 pit_sharp = SHARPMAX;
1876             }
1877
1878             if (mode == MR102)
1879             {
1880                 if (st->old_T0 > (L_SUBFR + 5))
1881                 {
1882                     if (pit_sharp < 0)
1883                     {
1884                         pit_sharp = ~((~pit_sharp) >> 2);
1885                     }
1886                     else
1887                     {
1888                         pit_sharp = pit_sharp >> 2;
1889                     }
1890                 }
1891             }
1892         }
1893         else
1894         {
1895             /* read and decode pitch gain */
1896             index = *parm++;                 /* index of gain(s) */
1897
1898             if (mode == MR795)
1899             {
1900                 /* decode pitch gain */
1901                 if (bfi != 0)
1902                 {
1903                     ec_gain_pitch(
1904                         &(st->ec_gain_p_st),
1905                         st->state,
1906                         &gain_pit,
1907                         pOverflow);
1908                 }
1909                 else
1910                 {
1911                     gain_pit = d_gain_pitch(mode, index, st->common_amr_tbls.qua_gain_pitch_ptr);
1912                 }
1913                 ec_gain_pitch_update(
1914                     &(st->ec_gain_p_st),
1915                     bfi,
1916                     st->prev_bf,
1917                     &gain_pit,
1918                     pOverflow);
1919
1920                 /* read and decode code gain */
1921                 index = *parm++;
1922
1923                 if (bfi == 0)
1924                 {
1925                     d_gain_code(
1926                         &(st->pred_state),
1927                         mode,
1928                         index,
1929                         code,
1930                         st->common_amr_tbls.qua_gain_code_ptr,
1931                         &gain_code,
1932                         pOverflow);
1933                 }
1934                 else
1935                 {
1936                     ec_gain_code(
1937                         &(st->ec_gain_c_st),
1938                         &(st->pred_state),
1939                         st->state,
1940                         &gain_code,
1941                         pOverflow);
1942                 }
1943
1944                 ec_gain_code_update(
1945                     &(st->ec_gain_c_st),
1946                     bfi,
1947                     st->prev_bf,
1948                     &gain_code,
1949                     pOverflow);
1950
1951                 pit_sharp = gain_pit;
1952
1953                 if (pit_sharp > SHARPMAX)
1954                 {
1955                     pit_sharp = SHARPMAX;
1956                 }
1957             }
1958             else
1959             { /* MR122 */
1960
1961                 if (bfi == 0)
1962                 {
1963                     d_gain_code(
1964                         &(st->pred_state),
1965                         mode,
1966                         index,
1967                         code,
1968                         st->common_amr_tbls.qua_gain_code_ptr,
1969                         &gain_code,
1970                         pOverflow);
1971                 }
1972                 else
1973                 {
1974                     ec_gain_code(
1975                         &(st->ec_gain_c_st),
1976                         &(st->pred_state),
1977                         st->state,
1978                         &gain_code,
1979                         pOverflow);
1980                 }
1981
1982                 ec_gain_code_update(
1983                     &(st->ec_gain_c_st),
1984                     bfi,
1985                     st->prev_bf,
1986                     &gain_code,
1987                     pOverflow);
1988
1989                 pit_sharp = gain_pit;
1990             }
1991         }
1992
1993         /* store pitch sharpening for next subframe             */
1994         /* (for modes which use the previous pitch gain for     */
1995         /* pitch sharpening in the search phase)                */
1996         /* do not update sharpening in even subframes for MR475 */
1997         if ((mode != MR475) || (evenSubfr == 0))
1998         {
1999             st->sharp = gain_pit;
2000
2001             if (st->sharp > SHARPMAX)
2002             {
2003                 st->sharp = SHARPMAX;
2004             }
2005         }
2006
2007         pit_sharp = shl(pit_sharp, 1, pOverflow);
2008
2009         if (pit_sharp > 16384)
2010         {
2011             for (i = 0; i < L_SUBFR; i++)
2012             {
2013                 temp = mult(st->exc[i], pit_sharp, pOverflow);
2014                 L_temp = L_mult(temp, gain_pit, pOverflow);
2015
2016                 if (mode == MR122)
2017                 {
2018                     if (L_temp < 0)
2019                     {
2020                         L_temp = ~((~L_temp) >> 1);
2021                     }
2022                     else
2023                     {
2024                         L_temp = L_temp >> 1;
2025                     }
2026                 }
2027                 *(excp + i) = pv_round(L_temp, pOverflow);
2028             }
2029         }
2030
2031         /*-------------------------------------------------------*
2032          * - Store list of LTP gains needed in the source        *
2033          *   characteristic detector (SCD)                       *
2034          *-------------------------------------------------------*/
2035
2036         if (bfi == 0)
2037         {
2038             for (i = 0; i < 8; i++)
2039             {
2040                 st->ltpGainHistory[i] = st->ltpGainHistory[i+1];
2041             }
2042             st->ltpGainHistory[8] = gain_pit;
2043         }
2044
2045         /*-------------------------------------------------------*
2046         * - Limit gain_pit if in background noise and BFI       *
2047         *   for MR475, MR515, MR59                              *
2048         *-------------------------------------------------------*/
2049
2050
2051         if ((st->prev_bf != 0 || bfi != 0) && st->inBackgroundNoise != 0 &&
2052                 ((mode == MR475) || (mode == MR515) || (mode == MR59)))
2053         {
2054
2055             if (gain_pit > 12288)    /* if (gain_pit > 0.75) in Q14*/
2056             {
2057                 gain_pit = ((gain_pit - 12288) >> 1) + 12288;
2058                 /* gain_pit = (gain_pit-0.75)/2.0 + 0.75; */
2059             }
2060
2061             if (gain_pit > 14745)    /* if (gain_pit > 0.90) in Q14*/
2062             {
2063                 gain_pit = 14745;
2064             }
2065         }
2066
2067         /*-------------------------------------------------------*
2068          *  Calculate CB mixed gain                              *
2069          *-------------------------------------------------------*/
2070         Int_lsf(
2071             prev_lsf,
2072             st->lsfState.past_lsf_q,
2073             i_subfr,
2074             lsf_i,
2075             pOverflow);
2076
2077         gain_code_mix =
2078             Cb_gain_average(
2079                 &(st->Cb_gain_averState),
2080                 mode,
2081                 gain_code,
2082                 lsf_i,
2083                 st->lsp_avg_st.lsp_meanSave,
2084                 bfi,
2085                 st->prev_bf,
2086                 pdfi,
2087                 st->prev_pdf,
2088                 st->inBackgroundNoise,
2089                 st->voicedHangover,
2090                 pOverflow);
2091
2092         /* make sure that MR74, MR795, MR122 have original code_gain*/
2093         if ((mode > MR67) && (mode != MR102))
2094             /* MR74, MR795, MR122 */
2095         {
2096             gain_code_mix = gain_code;
2097         }
2098
2099         /*-------------------------------------------------------*
2100          * - Find the total excitation.                          *
2101          * - Find synthesis speech corresponding to st->exc[].   *
2102          *-------------------------------------------------------*/
2103         if (mode <= MR102) /* MR475, MR515, MR59, MR67, MR74, MR795, MR102*/
2104         {
2105             pitch_fac = gain_pit;
2106             tmp_shift = 1;
2107         }
2108         else       /* MR122 */
2109         {
2110             if (gain_pit < 0)
2111             {
2112                 pitch_fac = ~((~gain_pit) >> 1);
2113             }
2114             else
2115             {
2116                 pitch_fac = gain_pit >> 1;
2117             }
2118             tmp_shift = 2;
2119         }
2120
2121         /* copy unscaled LTP excitation to exc_enhanced (used in phase
2122          * dispersion below) and compute total excitation for LTP feedback
2123          */
2124         for (i = 0; i < L_SUBFR; i++)
2125         {
2126             exc_enhanced[i] = st->exc[i];
2127
2128             /* st->exc[i] = gain_pit*st->exc[i] + gain_code*code[i]; */
2129             L_temp = L_mult(st->exc[i], pitch_fac, pOverflow);
2130             /* 12.2: Q0 * Q13 */
2131             /*  7.4: Q0 * Q14 */
2132             L_temp = L_mac(L_temp, code[i], gain_code, pOverflow);
2133             /* 12.2: Q12 * Q1 */
2134             /*  7.4: Q13 * Q1 */
2135             L_temp = L_shl(L_temp, tmp_shift, pOverflow);     /* Q16 */
2136             st->exc[i] = pv_round(L_temp, pOverflow);
2137         }
2138
2139         /*-------------------------------------------------------*
2140          * - Adaptive phase dispersion                           *
2141          *-------------------------------------------------------*/
2142         ph_disp_release(&(st->ph_disp_st)); /* free phase dispersion adaption */
2143
2144
2145         if (((mode == MR475) || (mode == MR515) || (mode == MR59)) &&
2146                 (st->voicedHangover > 3) && (st->inBackgroundNoise != 0) &&
2147                 (bfi != 0))
2148         {
2149             ph_disp_lock(&(st->ph_disp_st)); /* Always Use full Phase Disp. */
2150         }                                 /* if error in bg noise       */
2151
2152         /* apply phase dispersion to innovation (if enabled) and
2153            compute total excitation for synthesis part           */
2154         ph_disp(
2155             &(st->ph_disp_st),
2156             mode,
2157             exc_enhanced,
2158             gain_code_mix,
2159             gain_pit,
2160             code,
2161             pitch_fac,
2162             tmp_shift,
2163             &(st->common_amr_tbls),
2164             pOverflow);
2165
2166         /*-------------------------------------------------------*
2167          * - The Excitation control module are active during BFI.*
2168          * - Conceal drops in signal energy if in bg noise.      *
2169          *-------------------------------------------------------*/
2170         L_temp = 0;
2171         for (i = 0; i < L_SUBFR; i++)
2172         {
2173             L_temp = L_mac(L_temp, *(exc_enhanced + i), *(exc_enhanced + i), pOverflow);
2174         }
2175
2176         /* excEnergy = sqrt(L_temp) in Q0 */
2177         if (L_temp < 0)
2178         {
2179             L_temp = ~((~L_temp) >> 1);
2180         }
2181         else
2182         {
2183             L_temp = L_temp >> 1;
2184         }
2185
2186         L_temp = sqrt_l_exp(L_temp, &temp, pOverflow);
2187         /* To cope with 16-bit and scaling in ex_ctrl() */
2188         L_temp = L_shr(L_temp, (Word16)((temp >> 1) + 15), pOverflow);
2189         if (L_temp < 0)
2190         {
2191             excEnergy = (Word16)(~((~L_temp) >> 2));
2192         }
2193         else
2194         {
2195             excEnergy = (Word16)(L_temp >> 2);
2196         }
2197
2198         if (((mode == MR475) || (mode == MR515) || (mode == MR59))  &&
2199                 (st->voicedHangover > 5) && (st->inBackgroundNoise != 0) &&
2200                 (st->state < 4) &&
2201                 ((pdfi != 0 && st->prev_pdf != 0) || bfi != 0 || st->prev_bf != 0))
2202         {
2203             carefulFlag = 0;
2204
2205             if (pdfi != 0 && bfi == 0)
2206             {
2207                 carefulFlag = 1;
2208             }
2209
2210             Ex_ctrl(exc_enhanced,
2211                     excEnergy,
2212                     st->excEnergyHist,
2213                     st->voicedHangover,
2214                     st->prev_bf,
2215                     carefulFlag, pOverflow);
2216         }
2217
2218         if (!((st->inBackgroundNoise != 0) && (bfi != 0 || st->prev_bf != 0) &&
2219                 (st->state < 4)))
2220         {
2221             /* Update energy history for all modes */
2222             for (i = 0; i < 8; i++)
2223             {
2224                 st->excEnergyHist[i] = st->excEnergyHist[i+1];
2225             }
2226             st->excEnergyHist[8] = excEnergy;
2227         }
2228         /*-------------------------------------------------------*
2229          * Excitation control module end.                        *
2230          *-------------------------------------------------------*/
2231         if (pit_sharp > 16384)
2232         {
2233             for (i = 0; i < L_SUBFR; i++)
2234             {
2235                 *(excp + i) = add_16(*(excp + i), *(exc_enhanced + i), pOverflow);
2236
2237             }
2238             agc2(exc_enhanced, excp, L_SUBFR, pOverflow);
2239             *pOverflow = 0;
2240             Syn_filt(Az, excp, &synth[i_subfr], L_SUBFR,
2241                      st->mem_syn, 0);
2242         }
2243         else
2244         {
2245             *pOverflow = 0;
2246             Syn_filt(Az, exc_enhanced, &synth[i_subfr], L_SUBFR,
2247                      st->mem_syn, 0);
2248         }
2249
2250         if (*pOverflow != 0)    /* Test for overflow */
2251         {
2252             for (i = PIT_MAX + L_INTERPOL + L_SUBFR - 1; i >= 0; i--)
2253             {
2254                 if (st->old_exc[i] < 0)
2255                 {
2256                     st->old_exc[i] = ~((~st->old_exc[i]) >> 2);
2257                 }
2258                 else
2259                 {
2260                     st->old_exc[i] = st->old_exc[i] >> 2;
2261                 }
2262
2263             }
2264
2265             for (i = L_SUBFR - 1; i >= 0; i--)
2266             {
2267                 if (*(exc_enhanced + i) < 0)
2268                 {
2269                     *(exc_enhanced + i) = ~((~(*(exc_enhanced + i))) >> 2);
2270                 }
2271                 else
2272                 {
2273                     *(exc_enhanced + i) = *(exc_enhanced + i) >> 2;
2274                 }
2275             }
2276             Syn_filt(Az, exc_enhanced, &synth[i_subfr], L_SUBFR, st->mem_syn, 1);
2277         }
2278         else
2279         {
2280             oscl_memmove((void *)st->mem_syn, &synth[i_subfr+L_SUBFR-M], M*sizeof(synth[0]));
2281         }
2282
2283         /*--------------------------------------------------*
2284          * Update signal for next frame.                    *
2285          * -> shift to the left by L_SUBFR  st->exc[]       *
2286          *--------------------------------------------------*/
2287
2288         oscl_memmove((void *)&st->old_exc[0], &st->old_exc[L_SUBFR], (PIT_MAX + L_INTERPOL)*sizeof(st->old_exc[0]));
2289
2290         /* interpolated LPC parameters for next subframe */
2291         Az += MP1;
2292
2293         /* store T0 for next subframe */
2294         st->old_T0 = T0;
2295     }
2296
2297     /*-------------------------------------------------------*
2298      * Call the Source Characteristic Detector which updates *
2299      * st->inBackgroundNoise and st->voicedHangover.         *
2300      *-------------------------------------------------------*/
2301
2302     st->inBackgroundNoise =
2303         Bgn_scd(
2304             &(st->background_state),
2305             &(st->ltpGainHistory[0]),
2306             &(synth[0]),
2307             &(st->voicedHangover),
2308             pOverflow);
2309
2310     dtx_dec_activity_update(
2311         &(st->dtxDecoderState),
2312         st->lsfState.past_lsf_q,
2313         synth,
2314         pOverflow);
2315
2316     /* store bfi for next subframe */
2317     st->prev_bf = bfi;
2318     st->prev_pdf = pdfi;
2319
2320     /*--------------------------------------------------*
2321      * Calculate the LSF averages on the eight          *
2322      * previous frames                                  *
2323      *--------------------------------------------------*/
2324     lsp_avg(
2325         &(st->lsp_avg_st),
2326         st->lsfState.past_lsf_q,
2327         pOverflow);
2328
2329 the_end:
2330     st->dtxDecoderState.dtxGlobalState = newDTXState;
2331
2332 //    return(0);
2333 }