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.173
22     ANSI-C code for the Adaptive Multi-Rate - Wideband (AMR-WB) speech codec
23     Available from http://www.3gpp.org
24 
25 (C) 2007, 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 
33 
34  Filename: pvamrwbdecoder.cpp
35 
36 ------------------------------------------------------------------------------
37  INPUT AND OUTPUT DEFINITIONS
38 
39      int16 mode,                      input : used mode
40      int16 prms[],                    input : parameter vector
41      int16 synth16k[],                output: synthesis speech
42      int16 * frame_length,            output:  lenght of the frame
43      void *spd_state,                 i/o   : State structure
44      int16 frame_type,                input : received frame type
45      int16 ScratchMem[]
46 
47 ------------------------------------------------------------------------------
48  FUNCTION DESCRIPTION
49 
50    Performs the main decoder routine AMR WB ACELP coding algorithm with 20 ms
51    speech frames for wideband speech signals.
52 
53 
54 ------------------------------------------------------------------------------
55  REQUIREMENTS
56 
57 
58 ------------------------------------------------------------------------------
59  REFERENCES
60 
61 ------------------------------------------------------------------------------
62  PSEUDO-CODE
63 
64 ------------------------------------------------------------------------------
65 */
66 
67 
68 /*----------------------------------------------------------------------------
69 ; INCLUDES
70 ----------------------------------------------------------------------------*/
71 
72 #include "pv_amr_wb_type_defs.h"
73 #include "pvamrwbdecoder_mem_funcs.h"
74 #include "pvamrwbdecoder_basic_op.h"
75 #include "pvamrwbdecoder_cnst.h"
76 #include "pvamrwbdecoder_acelp.h"
77 #include "e_pv_amrwbdec.h"
78 #include "get_amr_wb_bits.h"
79 #include "pvamrwb_math_op.h"
80 #include "pvamrwbdecoder_api.h"
81 #include "pvamrwbdecoder.h"
82 #include "synthesis_amr_wb.h"
83 
84 
85 /*----------------------------------------------------------------------------
86 ; MACROS
87 ; Define module specific macros here
88 ----------------------------------------------------------------------------*/
89 
90 
91 /*----------------------------------------------------------------------------
92 ; DEFINES
93 ; Include all pre-processor statements here. Include conditional
94 ; compile variables also.
95 ----------------------------------------------------------------------------*/
96 
97 /*----------------------------------------------------------------------------
98 ; LOCAL STORE/BUFFER/POINTER DEFINITIONS
99 ; Variable declaration - defined here and used outside this module
100 ----------------------------------------------------------------------------*/
101 
102 /* LPC interpolation coef {0.45, 0.8, 0.96, 1.0}; in Q15 */
103 static const int16 interpol_frac[NB_SUBFR] = {14746, 26214, 31457, 32767};
104 
105 
106 /* isp tables for initialization */
107 
108 static const int16 isp_init[M] =
109 {
110     32138, 30274, 27246, 23170, 18205, 12540, 6393, 0,
111     -6393, -12540, -18205, -23170, -27246, -30274, -32138, 1475
112 };
113 
114 static const int16 isf_init[M] =
115 {
116     1024, 2048, 3072, 4096, 5120, 6144, 7168, 8192,
117     9216, 10240, 11264, 12288, 13312, 14336, 15360, 3840
118 };
119 
120 /*----------------------------------------------------------------------------
121 ; EXTERNAL FUNCTION REFERENCES
122 ; Declare functions defined elsewhere and referenced in this module
123 ----------------------------------------------------------------------------*/
124 
125 /*----------------------------------------------------------------------------
126 ; EXTERNAL GLOBAL STORE/BUFFER/POINTER REFERENCES
127 ; Declare variables used in this module but defined elsewhere
128 ----------------------------------------------------------------------------*/
129 
130 /*----------------------------------------------------------------------------
131 ; FUNCTION CODE
132 ----------------------------------------------------------------------------*/
133 
134 /*----------------------------------------------------------------------------
135  FUNCTION DESCRIPTION   pvDecoder_AmrWb_Init
136 
137    Initialization of variables for the decoder section.
138 
139 ----------------------------------------------------------------------------*/
140 
141 
142 
143 
pvDecoder_AmrWb_Init(void ** spd_state,void * pt_st,int16 ** ScratchMem)144 void pvDecoder_AmrWb_Init(void **spd_state, void *pt_st, int16 **ScratchMem)
145 {
146     /* Decoder states */
147     Decoder_State *st = &(((PV_AmrWbDec *)pt_st)->state);
148 
149     *ScratchMem = ((PV_AmrWbDec *)pt_st)->ScratchMem;
150     /*
151      *  Init dtx decoding
152      */
153     dtx_dec_amr_wb_reset(&(st->dtx_decSt), isf_init);
154 
155     pvDecoder_AmrWb_Reset((void *) st, 1);
156 
157     *spd_state = (void *) st;
158 
159     return;
160 }
161 
162 /*----------------------------------------------------------------------------
163 ; FUNCTION CODE
164 ----------------------------------------------------------------------------*/
165 
pvDecoder_AmrWb_Reset(void * st,int16 reset_all)166 void pvDecoder_AmrWb_Reset(void *st, int16 reset_all)
167 {
168     int16 i;
169 
170     Decoder_State *dec_state;
171 
172     dec_state = (Decoder_State *) st;
173 
174     pv_memset((void *)dec_state->old_exc,
175               0,
176               (PIT_MAX + L_INTERPOL)*sizeof(*dec_state->old_exc));
177 
178     pv_memset((void *)dec_state->past_isfq,
179               0,
180               M*sizeof(*dec_state->past_isfq));
181 
182 
183     dec_state->old_T0_frac = 0;               /* old pitch value = 64.0 */
184     dec_state->old_T0 = 64;
185     dec_state->first_frame = 1;
186     dec_state->L_gc_thres = 0;
187     dec_state->tilt_code = 0;
188 
189     pv_memset((void *)dec_state->disp_mem,
190               0,
191               8*sizeof(*dec_state->disp_mem));
192 
193 
194     /* scaling memories for excitation */
195     dec_state->Q_old = Q_MAX;
196     dec_state->Qsubfr[3] = Q_MAX;
197     dec_state->Qsubfr[2] = Q_MAX;
198     dec_state->Qsubfr[1] = Q_MAX;
199     dec_state->Qsubfr[0] = Q_MAX;
200 
201     if (reset_all != 0)
202     {
203         /* routines initialization */
204 
205         dec_gain2_amr_wb_init(dec_state->dec_gain);
206         oversamp_12k8_to_16k_init(dec_state->mem_oversamp);
207         band_pass_6k_7k_init(dec_state->mem_hf);
208         low_pass_filt_7k_init(dec_state->mem_hf3);
209         highpass_50Hz_at_12k8_init(dec_state->mem_sig_out);
210         highpass_400Hz_at_12k8_init(dec_state->mem_hp400);
211         Init_Lagconc(dec_state->lag_hist);
212 
213         /* isp initialization */
214 
215         pv_memcpy((void *)dec_state->ispold, (void *)isp_init, M*sizeof(*isp_init));
216 
217         pv_memcpy((void *)dec_state->isfold, (void *)isf_init, M*sizeof(*isf_init));
218         for (i = 0; i < L_MEANBUF; i++)
219         {
220             pv_memcpy((void *)&dec_state->isf_buf[i * M],
221                       (void *)isf_init,
222                       M*sizeof(*isf_init));
223         }
224         /* variable initialization */
225 
226         dec_state->mem_deemph = 0;
227 
228         dec_state->seed  = 21845;              /* init random with 21845 */
229         dec_state->seed2 = 21845;
230         dec_state->seed3 = 21845;
231 
232         dec_state->state = 0;
233         dec_state->prev_bfi = 0;
234 
235         /* Static vectors to zero */
236 
237         pv_memset((void *)dec_state->mem_syn_hf,
238                   0,
239                   M16k*sizeof(*dec_state->mem_syn_hf));
240 
241         pv_memset((void *)dec_state->mem_syn_hi,
242                   0,
243                   M*sizeof(*dec_state->mem_syn_hi));
244 
245         pv_memset((void *)dec_state->mem_syn_lo,
246                   0,
247                   M*sizeof(*dec_state->mem_syn_lo));
248 
249 
250         dtx_dec_amr_wb_reset(&(dec_state->dtx_decSt), isf_init);
251         dec_state->vad_hist = 0;
252 
253     }
254     return;
255 }
256 
257 /*----------------------------------------------------------------------------
258 ; FUNCTION CODE
259 ----------------------------------------------------------------------------*/
260 
pvDecoder_AmrWbMemRequirements()261 int32 pvDecoder_AmrWbMemRequirements()
262 {
263     return(sizeof(PV_AmrWbDec));
264 }
265 
266 
267 /*----------------------------------------------------------------------------
268 ; FUNCTION CODE
269 ----------------------------------------------------------------------------*/
270 
271 /*              Main decoder routine.                                       */
272 
pvDecoder_AmrWb(int16 mode,int16 prms[],int16 synth16k[],int16 * frame_length,void * spd_state,int16 frame_type,int16 ScratchMem[])273 int32 pvDecoder_AmrWb(
274     int16 mode,              /* input : used mode                     */
275     int16 prms[],            /* input : parameter vector              */
276     int16 synth16k[],        /* output: synthesis speech              */
277     int16 * frame_length,    /* output:  lenght of the frame          */
278     void *spd_state,         /* i/o   : State structure               */
279     int16 frame_type,        /* input : received frame type           */
280     int16 ScratchMem[]
281 )
282 {
283 
284     /* Decoder states */
285     Decoder_State *st;
286 
287     int16 *ScratchMem2 = &ScratchMem[ L_SUBFR + L_SUBFR16k + ((L_SUBFR + M + M16k +1)<<1)];
288 
289 
290     /* Excitation vector */
291 
292 
293     int16 *old_exc = ScratchMem2;
294 
295     int16 *Aq = &old_exc[(L_FRAME + 1) + PIT_MAX + L_INTERPOL];/* A(z)   quantized for the 4 subframes */
296 
297     int16 *ispnew  = &Aq[NB_SUBFR * (M + 1)];/* immittance spectral pairs at 4nd sfr */
298     int16 *isf     = &ispnew[M];             /* ISF (frequency domain) at 4nd sfr    */
299     int16 *isf_tmp = &isf[M];
300     int16 *code    = &isf_tmp[M];             /* algebraic codevector                 */
301     int16 *excp    = &code[L_SUBFR];
302     int16 *exc2    = &excp[L_SUBFR];         /* excitation vector                    */
303     int16 *HfIsf   = &exc2[L_FRAME];
304 
305 
306     int16 *exc;
307 
308     /* LPC coefficients */
309 
310     int16 *p_Aq;                          /* ptr to A(z) for the 4 subframes      */
311 
312 
313 
314     int16 fac, stab_fac, voice_fac, Q_new = 0;
315     int32 L_tmp, L_gain_code;
316 
317     /* Scalars */
318 
319     int16 i, j, i_subfr, index, ind[8], tmp;
320     int32 max;
321     int16 T0, T0_frac, pit_flag, T0_max, select, T0_min = 0;
322     int16 gain_pit, gain_code;
323     int16 newDTXState, bfi, unusable_frame, nb_bits;
324     int16 vad_flag;
325     int16 pit_sharp;
326 
327     int16 corr_gain = 0;
328 
329     st = (Decoder_State *) spd_state;
330 
331     /* mode verification */
332 
333     nb_bits = AMR_WB_COMPRESSED[mode];
334 
335     *frame_length = AMR_WB_PCM_FRAME;
336 
337     /* find the new  DTX state  SPEECH OR DTX */
338     newDTXState = rx_amr_wb_dtx_handler(&(st->dtx_decSt), frame_type);
339 
340 
341     if (newDTXState != SPEECH)
342     {
343         dtx_dec_amr_wb(&(st->dtx_decSt), exc2, newDTXState, isf, &prms);
344     }
345     /* SPEECH action state machine  */
346 
347     if ((frame_type == RX_SPEECH_BAD) ||
348             (frame_type == RX_SPEECH_PROBABLY_DEGRADED))
349     {
350         /* bfi for all index, bits are not usable */
351         bfi = 1;
352         unusable_frame = 0;
353     }
354     else if ((frame_type == RX_NO_DATA) ||
355              (frame_type == RX_SPEECH_LOST))
356     {
357         /* bfi only for lsf, gains and pitch period */
358         bfi = 1;
359         unusable_frame = 1;
360     }
361     else
362     {
363         bfi = 0;
364         unusable_frame = 0;
365     }
366 
367     if (bfi != 0)
368     {
369         st->state += 1;
370 
371         if (st->state > 6)
372         {
373             st->state = 6;
374         }
375     }
376     else
377     {
378         st->state >>=  1;
379     }
380 
381     /* If this frame is the first speech frame after CNI period,
382      * set the BFH state machine to an appropriate state depending
383      * on whether there was DTX muting before start of speech or not
384      * If there was DTX muting, the first speech frame is muted.
385      * If there was no DTX muting, the first speech frame is not
386      * muted. The BFH state machine starts from state 5, however, to
387      * keep the audible noise resulting from a SID frame which is
388      * erroneously interpreted as a good speech frame as small as
389      * possible (the decoder output in this case is quickly muted)
390      */
391 
392     if (st->dtx_decSt.dtxGlobalState == DTX)
393     {
394         st->state = 5;
395         st->prev_bfi = 0;
396     }
397     else if (st->dtx_decSt.dtxGlobalState == DTX_MUTE)
398     {
399         st->state = 5;
400         st->prev_bfi = 1;
401     }
402 
403     if (newDTXState == SPEECH)
404     {
405         vad_flag = Serial_parm_1bit(&prms);
406 
407         if (bfi == 0)
408         {
409             if (vad_flag == 0)
410             {
411                 st->vad_hist = add_int16(st->vad_hist, 1);
412             }
413             else
414             {
415                 st->vad_hist = 0;
416             }
417         }
418     }
419     /*
420      *  DTX-CNG
421      */
422 
423     if (newDTXState != SPEECH)     /* CNG mode */
424     {
425         /* increase slightly energy of noise below 200 Hz */
426 
427         /* Convert ISFs to the cosine domain */
428         Isf_isp(isf, ispnew, M);
429 
430         Isp_Az(ispnew, Aq, M, 1);
431 
432         pv_memcpy((void *)isf_tmp, (void *)st->isfold, M*sizeof(*isf_tmp));
433 
434 
435         for (i_subfr = 0; i_subfr < L_FRAME; i_subfr += L_SUBFR)
436         {
437             j = i_subfr >> 6;
438 
439             for (i = 0; i < M; i++)
440             {
441                 L_tmp = mul_16by16_to_int32(isf_tmp[i], sub_int16(32767, interpol_frac[j]));
442                 L_tmp = mac_16by16_to_int32(L_tmp, isf[i], interpol_frac[j]);
443                 HfIsf[i] = amr_wb_round(L_tmp);
444             }
445 
446             synthesis_amr_wb(Aq,
447                              &exc2[i_subfr],
448                              0,
449                              &synth16k[i_subfr *5/4],
450                              (short) 1,
451                              HfIsf,
452                              nb_bits,
453                              newDTXState,
454                              st,
455                              bfi,
456                              ScratchMem);
457         }
458 
459         /* reset speech coder memories */
460         pvDecoder_AmrWb_Reset(st, 0);
461 
462         pv_memcpy((void *)st->isfold, (void *)isf, M*sizeof(*isf));
463 
464         st->prev_bfi = bfi;
465         st->dtx_decSt.dtxGlobalState = newDTXState;
466 
467         return 0;
468     }
469     /*
470      *  ACELP
471      */
472 
473     /* copy coder memory state into working space (internal memory for DSP) */
474 
475     pv_memcpy((void *)old_exc, (void *)st->old_exc, (PIT_MAX + L_INTERPOL)*sizeof(*old_exc));
476 
477     exc = old_exc + PIT_MAX + L_INTERPOL;
478 
479     /* Decode the ISFs */
480 
481     if (nb_bits > NBBITS_7k)        /* all rates but 6.6 Kbps */
482     {
483         ind[0] = Serial_parm(8, &prms);     /* index of 1st ISP subvector */
484         ind[1] = Serial_parm(8, &prms);     /* index of 2nd ISP subvector */
485         ind[2] = Serial_parm(6, &prms);     /* index of 3rd ISP subvector */
486         ind[3] = Serial_parm(7, &prms);     /* index of 4th ISP subvector */
487         ind[4] = Serial_parm(7, &prms);     /* index of 5th ISP subvector */
488         ind[5] = Serial_parm(5, &prms);     /* index of 6th ISP subvector */
489         ind[6] = Serial_parm(5, &prms);     /* index of 7th ISP subvector */
490 
491         Dpisf_2s_46b(ind, isf, st->past_isfq, st->isfold, st->isf_buf, bfi, 1);
492     }
493     else
494     {
495         ind[0] = Serial_parm(8, &prms);
496         ind[1] = Serial_parm(8, &prms);
497         ind[2] = Serial_parm(14, &prms);
498         ind[3] = ind[2] & 0x007F;
499         ind[2] >>= 7;
500         ind[4] = Serial_parm(6, &prms);
501 
502         Dpisf_2s_36b(ind, isf, st->past_isfq, st->isfold, st->isf_buf, bfi, 1);
503     }
504 
505     /* Convert ISFs to the cosine domain */
506 
507     Isf_isp(isf, ispnew, M);
508 
509     if (st->first_frame != 0)
510     {
511         st->first_frame = 0;
512         pv_memcpy((void *)st->ispold, (void *)ispnew, M*sizeof(*ispnew));
513 
514     }
515     /* Find the interpolated ISPs and convert to a[] for all subframes */
516     interpolate_isp(st->ispold, ispnew, interpol_frac, Aq);
517 
518     /* update ispold[] for the next frame */
519     pv_memcpy((void *)st->ispold, (void *)ispnew, M*sizeof(*ispnew));
520 
521     /* Check stability on isf : distance between old isf and current isf */
522 
523     L_tmp = 0;
524     for (i = 0; i < M - 1; i++)
525     {
526         tmp = sub_int16(isf[i], st->isfold[i]);
527         L_tmp = mac_16by16_to_int32(L_tmp, tmp, tmp);
528     }
529     tmp = extract_h(shl_int32(L_tmp, 8));
530     tmp = mult_int16(tmp, 26214);                /* tmp = L_tmp*0.8/256 */
531 
532     tmp = 20480 - tmp;                 /* 1.25 - tmp */
533     stab_fac = shl_int16(tmp, 1);                /* Q14 -> Q15 with saturation */
534 
535     if (stab_fac < 0)
536     {
537         stab_fac = 0;
538     }
539     pv_memcpy((void *)isf_tmp, (void *)st->isfold, M*sizeof(*isf_tmp));
540 
541     pv_memcpy((void *)st->isfold, (void *)isf, M*sizeof(*isf));
542 
543     /*
544      *          Loop for every subframe in the analysis frame
545      *
546      * The subframe size is L_SUBFR and the loop is repeated L_FRAME/L_SUBFR
547      *  times
548      *     - decode the pitch delay and filter mode
549      *     - decode algebraic code
550      *     - decode pitch and codebook gains
551      *     - find voicing factor and tilt of code for next subframe.
552      *     - find the excitation and compute synthesis speech
553      */
554 
555     p_Aq = Aq;                                /* pointer to interpolated LPC parameters */
556 
557 
558     /*
559      *   Sub process next 3 subframes
560      */
561 
562 
563     for (i_subfr = 0; i_subfr < L_FRAME; i_subfr += L_SUBFR)
564     {
565         pit_flag = i_subfr;
566 
567 
568         if ((i_subfr == 2*L_SUBFR) && (nb_bits > NBBITS_7k))
569         {
570             pit_flag = 0;        /* set to 0 for 3rd subframe, <=> is not 6.6 kbps */
571         }
572         /*-------------------------------------------------*
573          * - Decode pitch lag                              *
574          * Lag indeces received also in case of BFI,       *
575          * so that the parameter pointer stays in sync.    *
576          *-------------------------------------------------*/
577 
578         if (pit_flag == 0)
579         {
580 
581             if (nb_bits <= NBBITS_9k)
582             {
583                 index = Serial_parm(8, &prms);
584 
585                 if (index < (PIT_FR1_8b - PIT_MIN) * 2)
586                 {
587                     T0 = PIT_MIN + (index >> 1);
588                     T0_frac = sub_int16(index, shl_int16(sub_int16(T0, PIT_MIN), 1));
589                     T0_frac = shl_int16(T0_frac, 1);
590                 }
591                 else
592                 {
593                     T0 = add_int16(index, PIT_FR1_8b - ((PIT_FR1_8b - PIT_MIN) * 2));
594                     T0_frac = 0;
595                 }
596             }
597             else
598             {
599                 index = Serial_parm(9, &prms);
600 
601                 if (index < (PIT_FR2 - PIT_MIN) * 4)
602                 {
603                     T0 = PIT_MIN + (index >> 2);
604                     T0_frac = sub_int16(index, shl_int16(sub_int16(T0, PIT_MIN), 2));
605                 }
606                 else if (index < (((PIT_FR2 - PIT_MIN) << 2) + ((PIT_FR1_9b - PIT_FR2) << 1)))
607                 {
608                     index -= (PIT_FR2 - PIT_MIN) << 2;
609                     T0 = PIT_FR2 + (index >> 1);
610                     T0_frac = sub_int16(index, shl_int16(sub_int16(T0, PIT_FR2), 1));
611                     T0_frac = shl_int16(T0_frac, 1);
612                 }
613                 else
614                 {
615                     T0 = add_int16(index, (PIT_FR1_9b - ((PIT_FR2 - PIT_MIN) * 4) - ((PIT_FR1_9b - PIT_FR2) * 2)));
616                     T0_frac = 0;
617                 }
618             }
619 
620             /* find T0_min and T0_max for subframe 2 and 4 */
621 
622             T0_min = T0 - 8;
623 
624             if (T0_min < PIT_MIN)
625             {
626                 T0_min = PIT_MIN;
627             }
628             T0_max = T0_min + 15;
629 
630             if (T0_max > PIT_MAX)
631             {
632                 T0_max = PIT_MAX;
633                 T0_min = PIT_MAX - 15;
634             }
635         }
636         else
637         {                                  /* if subframe 2 or 4 */
638 
639             if (nb_bits <= NBBITS_9k)
640             {
641                 index = Serial_parm(5, &prms);
642 
643                 T0 = T0_min + (index >> 1);
644                 T0_frac = sub_int16(index, shl_int16(T0 - T0_min, 1));
645                 T0_frac = shl_int16(T0_frac, 1);
646             }
647             else
648             {
649                 index = Serial_parm(6, &prms);
650 
651                 T0 = T0_min + (index >> 2);
652                 T0_frac = sub_int16(index, shl_int16(T0 - T0_min, 2));
653             }
654         }
655 
656         /* check BFI after pitch lag decoding */
657 
658         if (bfi != 0)                      /* if frame erasure */
659         {
660             lagconceal(&(st->dec_gain[17]), st->lag_hist, &T0, &(st->old_T0), &(st->seed3), unusable_frame);
661             T0_frac = 0;
662         }
663         /*
664          *  Find the pitch gain, the interpolation filter
665          *  and the adaptive codebook vector.
666          */
667 
668         Pred_lt4(&exc[i_subfr], T0, T0_frac, L_SUBFR + 1);
669 
670 
671         if (unusable_frame)
672         {
673             select = 1;
674         }
675         else
676         {
677 
678             if (nb_bits <= NBBITS_9k)
679             {
680                 select = 0;
681             }
682             else
683             {
684                 select = Serial_parm_1bit(&prms);
685             }
686         }
687 
688 
689         if (select == 0)
690         {
691             /* find pitch excitation with lp filter */
692             for (i = 0; i < L_SUBFR; i++)
693             {
694                 L_tmp  = ((int32) exc[i-1+i_subfr] + exc[i+1+i_subfr]);
695                 L_tmp *= 5898;
696                 L_tmp += ((int32) exc[i+i_subfr] * 20972);
697 
698                 code[i] = amr_wb_round(L_tmp << 1);
699             }
700             pv_memcpy((void *)&exc[i_subfr], (void *)code, L_SUBFR*sizeof(*code));
701 
702         }
703         /*
704          * Decode innovative codebook.
705          * Add the fixed-gain pitch contribution to code[].
706          */
707 
708         if (unusable_frame != 0)
709         {
710             /* the innovative code doesn't need to be scaled (see Q_gain2) */
711             for (i = 0; i < L_SUBFR; i++)
712             {
713                 code[i] = noise_gen_amrwb(&(st->seed)) >> 3;
714             }
715         }
716         else if (nb_bits <= NBBITS_7k)
717         {
718             ind[0] = Serial_parm(12, &prms);
719             dec_acelp_2p_in_64(ind[0], code);
720         }
721         else if (nb_bits <= NBBITS_9k)
722         {
723             for (i = 0; i < 4; i++)
724             {
725                 ind[i] = Serial_parm(5, &prms);
726             }
727             dec_acelp_4p_in_64(ind, 20, code);
728         }
729         else if (nb_bits <= NBBITS_12k)
730         {
731             for (i = 0; i < 4; i++)
732             {
733                 ind[i] = Serial_parm(9, &prms);
734             }
735             dec_acelp_4p_in_64(ind, 36, code);
736         }
737         else if (nb_bits <= NBBITS_14k)
738         {
739             ind[0] = Serial_parm(13, &prms);
740             ind[1] = Serial_parm(13, &prms);
741             ind[2] = Serial_parm(9, &prms);
742             ind[3] = Serial_parm(9, &prms);
743             dec_acelp_4p_in_64(ind, 44, code);
744         }
745         else if (nb_bits <= NBBITS_16k)
746         {
747             for (i = 0; i < 4; i++)
748             {
749                 ind[i] = Serial_parm(13, &prms);
750             }
751             dec_acelp_4p_in_64(ind, 52, code);
752         }
753         else if (nb_bits <= NBBITS_18k)
754         {
755             for (i = 0; i < 4; i++)
756             {
757                 ind[i] = Serial_parm(2, &prms);
758             }
759             for (i = 4; i < 8; i++)
760             {
761                 ind[i] = Serial_parm(14, &prms);
762             }
763             dec_acelp_4p_in_64(ind, 64, code);
764         }
765         else if (nb_bits <= NBBITS_20k)
766         {
767             ind[0] = Serial_parm(10, &prms);
768             ind[1] = Serial_parm(10, &prms);
769             ind[2] = Serial_parm(2, &prms);
770             ind[3] = Serial_parm(2, &prms);
771             ind[4] = Serial_parm(10, &prms);
772             ind[5] = Serial_parm(10, &prms);
773             ind[6] = Serial_parm(14, &prms);
774             ind[7] = Serial_parm(14, &prms);
775             dec_acelp_4p_in_64(ind, 72, code);
776         }
777         else
778         {
779             for (i = 0; i < 8; i++)
780             {
781                 ind[i] = Serial_parm(11, &prms);
782             }
783 
784             dec_acelp_4p_in_64(ind, 88, code);
785         }
786 
787         preemph_amrwb_dec(code, st->tilt_code, L_SUBFR);
788 
789         tmp = T0;
790 
791         if (T0_frac > 2)
792         {
793             tmp++;
794         }
795         Pit_shrp(code, tmp, PIT_SHARP, L_SUBFR);
796 
797         /*
798          *  Decode codebooks gains.
799          */
800 
801         if (nb_bits <= NBBITS_9k)
802         {
803             index = Serial_parm(6, &prms); /* codebook gain index */
804 
805             dec_gain2_amr_wb(index,
806                              6,
807                              code,
808                              L_SUBFR,
809                              &gain_pit,
810                              &L_gain_code,
811                              bfi,
812                              st->prev_bfi,
813                              st->state,
814                              unusable_frame,
815                              st->vad_hist,
816                              st->dec_gain);
817         }
818         else
819         {
820             index = Serial_parm(7, &prms); /* codebook gain index */
821 
822             dec_gain2_amr_wb(index,
823                              7,
824                              code,
825                              L_SUBFR,
826                              &gain_pit,
827                              &L_gain_code,
828                              bfi,
829                              st->prev_bfi,
830                              st->state,
831                              unusable_frame,
832                              st->vad_hist,
833                              st->dec_gain);
834         }
835 
836         /* find best scaling to perform on excitation (Q_new) */
837 
838         tmp = st->Qsubfr[0];
839         for (i = 1; i < 4; i++)
840         {
841             if (st->Qsubfr[i] < tmp)
842             {
843                 tmp = st->Qsubfr[i];
844             }
845         }
846 
847         /* limit scaling (Q_new) to Q_MAX: see pv_amr_wb_cnst.h and syn_filt_32() */
848 
849         if (tmp > Q_MAX)
850         {
851             tmp = Q_MAX;
852         }
853         Q_new = 0;
854         L_tmp = L_gain_code;                  /* L_gain_code in Q16 */
855 
856 
857         while ((L_tmp < 0x08000000L) && (Q_new < tmp))
858         {
859             L_tmp <<= 1;
860             Q_new += 1;
861 
862         }
863         gain_code = amr_wb_round(L_tmp);          /* scaled gain_code with Qnew */
864 
865         scale_signal(exc + i_subfr - (PIT_MAX + L_INTERPOL),
866                      PIT_MAX + L_INTERPOL + L_SUBFR,
867                      (int16)(Q_new - st->Q_old));
868 
869         st->Q_old = Q_new;
870 
871 
872         /*
873          * Update parameters for the next subframe.
874          * - tilt of code: 0.0 (unvoiced) to 0.5 (voiced)
875          */
876 
877 
878         if (bfi == 0)
879         {
880             /* LTP-Lag history update */
881             for (i = 4; i > 0; i--)
882             {
883                 st->lag_hist[i] = st->lag_hist[i - 1];
884             }
885             st->lag_hist[0] = T0;
886 
887             st->old_T0 = T0;
888             st->old_T0_frac = 0;              /* Remove fraction in case of BFI */
889         }
890         /* find voice factor in Q15 (1=voiced, -1=unvoiced) */
891 
892         /*
893          * Scale down by 1/8
894          */
895         for (i = L_SUBFR - 1; i >= 0; i--)
896         {
897             exc2[i] = (exc[i_subfr + i] + (0x0004 * (exc[i_subfr + i] != MAX_16))) >> 3;
898         }
899 
900 
901         /* post processing of excitation elements */
902 
903         if (nb_bits <= NBBITS_9k)
904         {
905             pit_sharp = shl_int16(gain_pit, 1);
906 
907             if (pit_sharp > 16384)
908             {
909                 for (i = 0; i < L_SUBFR; i++)
910                 {
911                     tmp = mult_int16(exc2[i], pit_sharp);
912                     L_tmp = mul_16by16_to_int32(tmp, gain_pit);
913                     L_tmp >>= 1;
914                     excp[i] = amr_wb_round(L_tmp);
915                 }
916             }
917         }
918         else
919         {
920             pit_sharp = 0;
921         }
922 
923         voice_fac = voice_factor(exc2, -3, gain_pit, code, gain_code, L_SUBFR);
924 
925         /* tilt of code for next subframe: 0.5=voiced, 0=unvoiced */
926 
927         st->tilt_code = (voice_fac >> 2) + 8192;
928 
929         /*
930          * - Find the total excitation.
931          * - Find synthesis speech corresponding to exc[].
932          * - Find maximum value of excitation for next scaling
933          */
934 
935         pv_memcpy((void *)exc2, (void *)&exc[i_subfr], L_SUBFR*sizeof(*exc2));
936         max = 1;
937 
938         for (i = 0; i < L_SUBFR; i++)
939         {
940             L_tmp = mul_16by16_to_int32(code[i], gain_code);
941             L_tmp = shl_int32(L_tmp, 5);
942             L_tmp = mac_16by16_to_int32(L_tmp, exc[i + i_subfr], gain_pit);
943             L_tmp = shl_int32(L_tmp, 1);
944             tmp = amr_wb_round(L_tmp);
945             exc[i + i_subfr] = tmp;
946             tmp = tmp - (tmp < 0);
947             max |= tmp ^(tmp >> 15);  /* |= tmp ^sign(tmp) */
948         }
949 
950 
951         /* tmp = scaling possible according to max value of excitation */
952         tmp = add_int16(norm_s(max), Q_new) - 1;
953 
954         st->Qsubfr[3] = st->Qsubfr[2];
955         st->Qsubfr[2] = st->Qsubfr[1];
956         st->Qsubfr[1] = st->Qsubfr[0];
957         st->Qsubfr[0] = tmp;
958 
959         /*
960          * phase dispersion to enhance noise in low bit rate
961          */
962 
963 
964         if (nb_bits <= NBBITS_7k)
965         {
966             j = 0;      /* high dispersion for rate <= 7.5 kbit/s */
967         }
968         else if (nb_bits <= NBBITS_9k)
969         {
970             j = 1;      /* low dispersion for rate <= 9.6 kbit/s */
971         }
972         else
973         {
974             j = 2;      /* no dispersion for rate > 9.6 kbit/s */
975         }
976 
977         /* L_gain_code in Q16 */
978 
979         phase_dispersion((int16)(L_gain_code >> 16),
980                          gain_pit,
981                          code,
982                          j,
983                          st->disp_mem,
984                          ScratchMem);
985 
986         /*
987          * noise enhancer
988          * - Enhance excitation on noise. (modify gain of code)
989          *   If signal is noisy and LPC filter is stable, move gain
990          *   of code 1.5 dB toward gain of code threshold.
991          *   This decrease by 3 dB noise energy variation.
992          */
993 
994         tmp = 16384 - (voice_fac >> 1);  /* 1=unvoiced, 0=voiced */
995         fac = mult_int16(stab_fac, tmp);
996 
997         L_tmp = L_gain_code;
998 
999         if (L_tmp < st->L_gc_thres)
1000         {
1001             L_tmp += fxp_mul32_by_16b(L_gain_code, 6226) << 1;
1002 
1003             if (L_tmp > st->L_gc_thres)
1004             {
1005                 L_tmp = st->L_gc_thres;
1006             }
1007         }
1008         else
1009         {
1010             L_tmp = fxp_mul32_by_16b(L_gain_code, 27536) << 1;
1011 
1012             if (L_tmp < st->L_gc_thres)
1013             {
1014                 L_tmp = st->L_gc_thres;
1015             }
1016         }
1017         st->L_gc_thres = L_tmp;
1018 
1019         L_gain_code = fxp_mul32_by_16b(L_gain_code, (32767 - fac)) << 1;
1020 
1021 
1022         L_gain_code = add_int32(L_gain_code, fxp_mul32_by_16b(L_tmp, fac) << 1);
1023 
1024         /*
1025          * pitch enhancer
1026          * - Enhance excitation on voice. (HP filtering of code)
1027          *   On voiced signal, filtering of code by a smooth fir HP
1028          *   filter to decrease energy of code in low frequency.
1029          */
1030 
1031         tmp = (voice_fac >> 3) + 4096;/* 0.25=voiced, 0=unvoiced */
1032 
1033         /* build excitation */
1034 
1035         gain_code = amr_wb_round(shl_int32(L_gain_code, Q_new));
1036 
1037         L_tmp = (int32)(code[0] << 16);
1038         L_tmp = msu_16by16_from_int32(L_tmp, code[1], tmp);
1039         L_tmp = mul_16by16_to_int32(amr_wb_round(L_tmp), gain_code);
1040         L_tmp = shl_int32(L_tmp, 5);
1041         L_tmp = mac_16by16_to_int32(L_tmp, exc2[0], gain_pit);
1042         L_tmp = shl_int32(L_tmp, 1);       /* saturation can occur here */
1043         exc2[0] = amr_wb_round(L_tmp);
1044 
1045 
1046         for (i = 1; i < L_SUBFR - 1; i++)
1047         {
1048             L_tmp = (int32)(code[i] << 16);
1049             L_tmp = msu_16by16_from_int32(L_tmp, (code[i + 1] + code[i - 1]), tmp);
1050             L_tmp = mul_16by16_to_int32(amr_wb_round(L_tmp), gain_code);
1051             L_tmp = shl_int32(L_tmp, 5);
1052             L_tmp = mac_16by16_to_int32(L_tmp, exc2[i], gain_pit);
1053             L_tmp = shl_int32(L_tmp, 1);       /* saturation can occur here */
1054             exc2[i] = amr_wb_round(L_tmp);
1055         }
1056 
1057         L_tmp = (int32)(code[L_SUBFR - 1] << 16);
1058         L_tmp = msu_16by16_from_int32(L_tmp, code[L_SUBFR - 2], tmp);
1059         L_tmp = mul_16by16_to_int32(amr_wb_round(L_tmp), gain_code);
1060         L_tmp = shl_int32(L_tmp, 5);
1061         L_tmp = mac_16by16_to_int32(L_tmp, exc2[L_SUBFR - 1], gain_pit);
1062         L_tmp = shl_int32(L_tmp, 1);       /* saturation can occur here */
1063         exc2[L_SUBFR - 1] = amr_wb_round(L_tmp);
1064 
1065 
1066 
1067         if (nb_bits <= NBBITS_9k)
1068         {
1069             if (pit_sharp > 16384)
1070             {
1071                 for (i = 0; i < L_SUBFR; i++)
1072                 {
1073                     excp[i] = add_int16(excp[i], exc2[i]);
1074                 }
1075                 agc2_amr_wb(exc2, excp, L_SUBFR);
1076                 pv_memcpy((void *)exc2, (void *)excp, L_SUBFR*sizeof(*exc2));
1077 
1078             }
1079         }
1080         if (nb_bits <= NBBITS_7k)
1081         {
1082             j = i_subfr >> 6;
1083             for (i = 0; i < M; i++)
1084             {
1085                 L_tmp = mul_16by16_to_int32(isf_tmp[i], sub_int16(32767, interpol_frac[j]));
1086                 L_tmp = mac_16by16_to_int32(L_tmp, isf[i], interpol_frac[j]);
1087                 HfIsf[i] = amr_wb_round(L_tmp);
1088             }
1089         }
1090         else
1091         {
1092             pv_memset((void *)st->mem_syn_hf,
1093                       0,
1094                       (M16k - M)*sizeof(*st->mem_syn_hf));
1095         }
1096 
1097         if (nb_bits >= NBBITS_24k)
1098         {
1099             corr_gain = Serial_parm(4, &prms);
1100         }
1101         else
1102         {
1103             corr_gain = 0;
1104         }
1105 
1106         synthesis_amr_wb(p_Aq,
1107                          exc2,
1108                          Q_new,
1109                          &synth16k[i_subfr + (i_subfr>>2)],
1110                          corr_gain,
1111                          HfIsf,
1112                          nb_bits,
1113                          newDTXState,
1114                          st,
1115                          bfi,
1116                          ScratchMem);
1117 
1118         p_Aq += (M + 1);                   /* interpolated LPC parameters for next subframe */
1119     }
1120 
1121     /*
1122      *   Update signal for next frame.
1123      *   -> save past of exc[]
1124      *   -> save pitch parameters
1125      */
1126 
1127     pv_memcpy((void *)st->old_exc,
1128               (void *)&old_exc[L_FRAME],
1129               (PIT_MAX + L_INTERPOL)*sizeof(*old_exc));
1130 
1131     scale_signal(exc, L_FRAME, (int16)(-Q_new));
1132 
1133     dtx_dec_amr_wb_activity_update(&(st->dtx_decSt), isf, exc);
1134 
1135     st->dtx_decSt.dtxGlobalState = newDTXState;
1136 
1137     st->prev_bfi = bfi;
1138 
1139     return 0;
1140 }
1141 
1142