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