LCOV - code coverage report
Current view: top level - lib_dec - gs_dec_fx.c (source / functions) Hit Total Coverage
Test: Coverage on main enc/dec/rend @ 3b2f07138c61dcf997bbf4165d0882f794b2995f Lines: 389 413 94.2 %
Date: 2025-05-03 01:55:50 Functions: 4 4 100.0 %

          Line data    Source code
       1             : /*====================================================================================
       2             :     EVS Codec 3GPP TS26.452 Aug 12, 2021. Version 16.3.0
       3             :   ====================================================================================*/
       4             : 
       5             : #include <stdint.h>
       6             : #include "options.h"
       7             : #include "cnst.h"
       8             : #include "rom_com.h"
       9             : #include "prot_fx.h"
      10             : #include "ivas_cnst.h"
      11             : /*=========================================================================*/
      12             : /* FUNCTION : void decod_audio_fx();                                                                   */
      13             : /*-------------------------------------------------------------------------*/
      14             : /* PURPOSE :  Decode audio (AC) frames                                             */
      15             : /*-------------------------------------------------------------------------*/
      16             : /*    INPUT ARGUMENTS :                                                                                                */
      17             : /* _ (Word16[]) Aq                      : LP filter coefficient         Q12                        */
      18             : /* _ (Word16) coder_type                : coding type                           Q0                         */
      19             : /* _(Word16) Q_exc              :Q format of excitation                    */
      20             : /*-------------------------------------------------------------------------*/
      21             : /* OUTPUT ARGUMENTS :                                                                                                      */
      22             : /* _ (Word16[]) pitch_buf_fx    : floating pitch values for each subframe Q6*/
      23             : /* _ (Word16[]) voice_factors_fx: frame error rate                              Q15                 */
      24             : /*--------------------------------------------------------------------------*/
      25             : /* INPUT/OUTPUT ARGUMENTS :                                                                                         */
      26             : /*  Decoder_State *st_fx     : decoder  memory structure                 */
      27             : /* _ (Word16[]) exc_fx                  : adapt. excitation exc (Q_exc)                     */
      28             : /* _ (Word16[]) exc2_fx                 : adapt. excitation/total exc (Q_exc)       */
      29             : /*--------------------------------------------------------------------------*/
      30             : /* RETURN ARGUMENTS :                                                                                                       */
      31             : /* _ None                                                                                                                                   */
      32             : /*==========================================================================*/
      33             : 
      34             : #ifndef REMOVE_EVS_DUPLICATES
      35             : void decod_audio_fx(
      36             :     Decoder_State *st_fx,  /* i/o: decoder static memory                     */
      37             :     Word16 dct_epit[],     /* o  : GSC excitation in DCT domain              Qx*/
      38             :     const Word16 *Aq,      /* i  : LP filter coefficient                     Q12*/
      39             :     Word16 *pitch_buf,     /* o  : floating pitch values for each subframe   Q6*/
      40             :     Word16 *voice_factors, /* o  : voicing factors                           Q15*/
      41             :     Word16 *exc,           /* i/o: adapt. excitation exc                     Q_exc*/
      42             :     Word16 *exc2,          /* i/o: adapt. excitation/total exc               Q_exc*/
      43             :     Word16 *bwe_exc,       /* o  : excitation for SWB TBE                    Q_exc*/
      44             :     Word16 *lsf_new        /* i  : ISFs at the end of the frame              Qx*/
      45             :     ,
      46             :     Word16 *gain_buf /*Q14*/
      47             : )
      48             : {
      49             :     Word16 tmp_nb_bits_tot, pit_band_idx;
      50             :     Word16 code[4 * L_SUBFR];
      51             :     Word16 Diff_len, nb_subfr, i;
      52             :     Word16 nb_frame_flg;
      53             :     Word16 Es_pred = 0;
      54             :     Word16 Len, max_len;
      55             :     Word16 gsc_attack_flag;
      56             : 
      57             :     Word16 low_pit;
      58             :     Word16 last_bin;
      59             :     Word16 nbits;
      60             : 
      61             :     Word16 exc_wo_nf[L_FRAME16k];
      62             :     GSC_DEC_HANDLE hGSCDec;
      63             :     hGSCDec = st_fx->hGSCDec;
      64             : 
      65             : 
      66             :     /*---------------------------------------------------------------*
      67             :      * Initialization
      68             :      *---------------------------------------------------------------*/
      69             :     move16(); // corresponding to initialization of Es_pred
      70             :     Diff_len = 0;
      71             :     move16();
      72             : 
      73             :     /* decode GSC attack flag (used to reduce possible pre-echo) */
      74             :     gsc_attack_flag = (Word16) get_next_indice_fx( st_fx, 1 ); /* Q0 */
      75             :     move16();
      76             : 
      77             :     /* decode GSC SWB speech flag */
      78             :     test();
      79             :     IF( st_fx->coder_type != INACTIVE && GE_32( st_fx->total_brate, ACELP_13k20 ) )
      80             :     {
      81             :         st_fx->GSC_noisy_speech = (Word16) get_next_indice_fx( st_fx, 1 ); /* Q0 */
      82             :         move16();
      83             :     }
      84             : 
      85             :     /* safety check in case of bit errors */
      86             :     test();
      87             :     test();
      88             :     IF( st_fx->GSC_noisy_speech && LT_16( st_fx->bwidth, SWB ) && st_fx->GSC_IVAS_mode == 0 )
      89             :     {
      90             :         st_fx->BER_detect = 1; /* Q0 */
      91             :         move16();
      92             :         st_fx->GSC_noisy_speech = 0; /* Q0 */
      93             :         move16();
      94             :     }
      95             : 
      96             :     /* set bit-allocation */
      97             : #ifdef REMOVE_EVS_DUPLICATES
      98             :     config_acelp1_IVAS( DEC, st_fx->total_brate, st_fx->core_brate, st_fx->core, st_fx->extl_orig, st_fx->extl_brate_orig, st_fx->L_frame, st_fx->GSC_noisy_speech, &( st_fx->acelp_cfg ), st_fx->next_bit_pos, st_fx->coder_type, st_fx->inactive_coder_type_flag, -1, 1, &nbits, NULL, st_fx->element_mode, &nbits /*dummy*/, 0, 0, st_fx->idchan, st_fx->active_cnt, 0, st_fx->tdm_LRTD_flag, st_fx->GSC_IVAS_mode );
      99             : #else
     100             :     config_acelp1( DEC, st_fx->total_brate, st_fx->core_brate, st_fx->core, st_fx->extl_orig, st_fx->extl_brate_orig, st_fx->L_frame, st_fx->GSC_noisy_speech, &( st_fx->acelp_cfg ), st_fx->next_bit_pos, st_fx->coder_type, st_fx->inactive_coder_type_flag, -1, 1, &nbits, NULL, st_fx->element_mode, &nbits /*dummy*/, 0, 0, st_fx->idchan, st_fx->active_cnt, 0, st_fx->tdm_LRTD_flag, st_fx->GSC_IVAS_mode );
     101             : #endif
     102             : 
     103             :     /*---------------------------------------------------------------*
     104             :      * Decode energy dynamics
     105             :      *---------------------------------------------------------------*/
     106             : 
     107             :     IF( EQ_16( st_fx->GSC_noisy_speech, 1 ) )
     108             :     {
     109             :         nb_subfr = NB_SUBFR; /* Q0 */
     110             :         move16();
     111             :         hGSCDec->cor_strong_limit = 0;
     112             :         move16();
     113             :         hGSCDec->noise_lev = NOISE_LEVEL_SP3; /* Q0 */
     114             :         move16();
     115             :     }
     116             :     ELSE
     117             :     {
     118             :         IF( LE_32( st_fx->core_brate, ACELP_8k00 ) )
     119             :         {
     120             :             hGSCDec->noise_lev = add( (Word16) get_next_indice_fx( st_fx, 2 ), NOISE_LEVEL_SP2 ); /* Q0 */
     121             :             move16();
     122             :         }
     123             :         ELSE
     124             :         {
     125             :             hGSCDec->noise_lev = add( (Word16) get_next_indice_fx( st_fx, 3 ), NOISE_LEVEL_SP0 ); /* Q0 */
     126             :             move16();
     127             :         }
     128             : 
     129             :         /*---------------------------------------------------------------*
     130             :          * Decode number of subframes
     131             :          *---------------------------------------------------------------*/
     132             : 
     133             : 
     134             :         hGSCDec->cor_strong_limit = 1; /* Q0 */
     135             :         move16();
     136             :         nb_subfr = SWNB_SUBFR;
     137             :         move16();
     138             : 
     139             :         IF( GE_32( st_fx->core_brate, ACELP_9k60 ) )
     140             :         {
     141             :             nbits = 1;
     142             :             move16();
     143             :             nb_frame_flg = (Word16) get_next_indice_fx( st_fx, nbits ); /* Q0 */
     144             :             move16();
     145             : 
     146             :             IF( s_and( nb_frame_flg, 0x1 ) == 0 )
     147             :             {
     148             :                 nb_subfr = 2 * SWNB_SUBFR; /* Q0 */
     149             :                 move16();
     150             :                 hGSCDec->cor_strong_limit = 0;
     151             :                 move16();
     152             :             }
     153             :         }
     154             :     }
     155             : 
     156             :     /*---------------------------------------------------------------*
     157             :      * Decode the last band where the adaptive (pitch) contribution is significant
     158             :      *---------------------------------------------------------------*/
     159             : 
     160             :     IF( LT_32( st_fx->core_brate, CFREQ_BITRATE ) )
     161             :     {
     162             :         nbits = 3; /* Q0 */
     163             :         move16();
     164             :         test();
     165             :         if ( LT_32( st_fx->core_brate, ACELP_9k60 ) && st_fx->coder_type == INACTIVE )
     166             :         {
     167             :             nbits = 1; /* Q0 */
     168             :             move16();
     169             :         }
     170             :     }
     171             :     ELSE
     172             :     {
     173             :         nbits = 4; /* Q0 */
     174             :         move16();
     175             :     }
     176             :     test();
     177             :     IF( LT_32( st_fx->core_brate, ACELP_9k60 ) && st_fx->coder_type != INACTIVE )
     178             :     {
     179             :         pit_band_idx = 1; /* Q0 */
     180             :         move16();
     181             :     }
     182             :     ELSE
     183             :     {
     184             :         pit_band_idx = (Word16) get_next_indice_fx( st_fx, nbits ); /* Q0 */
     185             :         move16();
     186             :     }
     187             : 
     188             :     IF( pit_band_idx != 0 )
     189             :     {
     190             :         IF( LT_32( st_fx->core_brate, ACELP_9k60 ) )
     191             :         {
     192             :             pit_band_idx = 7 + BAND1k2; /* Q0 */
     193             :             move16();                   /* At low rate, if pitch model is chosen, then for to be use on extented and constant frequency range */
     194             :         }
     195             :         ELSE
     196             :         {
     197             :             pit_band_idx = add( pit_band_idx, BAND1k2 ); /* Q0 */
     198             :         }
     199             : 
     200             :         /* detect bit errors in the bitstream */
     201             :         IF( GT_16( pit_band_idx, 13 ) ) /* The maximum decodable index is 10 + BAND1k2 (3) = 13 */
     202             :         {
     203             :             pit_band_idx = 13; /* Q0 */
     204             :             move16();
     205             :             st_fx->BER_detect = 1; /* Q0 */
     206             :             move16();
     207             :         }
     208             :         Diff_len = mfreq_loc_div_25[pit_band_idx]; /* Q0 */
     209             :         move16();
     210             :     }
     211             :     hGSCDec->Last_GSC_pit_band_idx = pit_band_idx; /* Q0 */
     212             :     move16();
     213             : 
     214             : 
     215             :     /*--------------------------------------------------------------------------------------*
     216             :      * Decode adaptive (pitch) excitation contribution
     217             :      * Reset unvaluable part of the adaptive (pitch) excitation contribution
     218             :      *--------------------------------------------------------------------------------------*/
     219             :     IF( GT_16( pit_band_idx, BAND1k2 ) )
     220             :     {
     221             :         /*---------------------------------------------------------------*
     222             :          * Decode adaptive (pitch) excitation contribution
     223             :          *---------------------------------------------------------------*/
     224             :         test();
     225             :         IF( EQ_16( st_fx->GSC_noisy_speech, 1 ) && EQ_16( nb_subfr, NB_SUBFR ) )
     226             :         {
     227             :             Word16 indice;
     228             :             nbits = Es_pred_bits_tbl[BIT_ALLOC_IDX_fx( st_fx->core_brate, GENERIC, -1, -1 )]; /* Q0 */
     229             :             move16();
     230             :             if ( st_fx->element_mode > EVS_MONO )
     231             :             {
     232             :                 nbits = 5;
     233             :                 move16();
     234             :             }
     235             : 
     236             :             indice = get_next_indice_fx( st_fx, nbits ); /* Q0 */
     237             : 
     238             :             Es_pred_dec_fx( &Es_pred, indice, nbits, 0 );
     239             :         }
     240             : 
     241             :         dec_pit_exc_fx( st_fx, Aq, st_fx->coder_type, Es_pred, pitch_buf, code, exc, bwe_exc, nb_subfr, gain_buf );
     242             : 
     243             :         IF( LT_32( st_fx->core_brate, ACELP_9k60 ) )
     244             :         {
     245             :             minimum_fx( pitch_buf, shr( st_fx->L_frame, 6 ), &low_pit );
     246             :             low_pit = shr( low_pit, 6 ); /*Q6 -> Q0 */
     247             : 
     248             :             IF( LT_16( low_pit, 64 ) )
     249             :             {
     250             :                 pit_band_idx = 9 + BAND1k2; /* Q0 */
     251             :                 move16();
     252             :                 if ( st_fx->bwidth == NB )
     253             :                 {
     254             :                     pit_band_idx = 7 + BAND1k2; /* Q0 */
     255             :                     move16();
     256             :                 }
     257             :             }
     258             :             ELSE IF( LT_16( low_pit, 128 ) )
     259             :             {
     260             :                 pit_band_idx = 5 + BAND1k2; /* Q0 */
     261             :                 move16();
     262             :             }
     263             :             ELSE
     264             :             {
     265             :                 pit_band_idx = 3 + BAND1k2; /* Q0 */
     266             :                 move16();
     267             :             }
     268             : 
     269             :             Diff_len = mfreq_loc_div_25[pit_band_idx]; /* Q0 */
     270             :             move16();
     271             :             hGSCDec->Last_GSC_pit_band_idx = pit_band_idx; /* Q0 */
     272             :             move16();
     273             :         }
     274             : 
     275             :         /*---------------------------------------------------------------*
     276             :          * DCT transform
     277             :          *---------------------------------------------------------------*/
     278             :         edct_16fx( exc, dct_epit, st_fx->L_frame, 7, st_fx->element_mode );
     279             : 
     280             :         /*---------------------------------------------------------------*
     281             :          * Reset unvaluable part of the adaptive (pitch) excitation contribution
     282             :          *---------------------------------------------------------------*/
     283             : 
     284             :         max_len = sub( st_fx->L_frame, Diff_len ); /* Q0 */
     285             : 
     286             :         if ( st_fx->bwidth == NB )
     287             :         {
     288             :             max_len = sub( 160, Diff_len ); /* Q0 */
     289             :         }
     290             : 
     291             :         Len = 80;
     292             :         move16();
     293             :         if ( LT_16( max_len, 80 ) )
     294             :         {
     295             :             Len = max_len; /* Q0 */
     296             :             move16();
     297             :         }
     298             : 
     299             :         test();
     300             :         IF( EQ_32( st_fx->core_brate, ACELP_8k00 ) && NE_16( st_fx->bwidth, NB ) )
     301             :         {
     302             :             FOR( i = 0; i < max_len; i++ )
     303             :             {
     304             :                 dct_epit[i + Diff_len] = 0;
     305             :                 move16();
     306             :             }
     307             :         }
     308             :         ELSE
     309             :         {
     310             :             FOR( i = 0; i < Len; i++ )
     311             :             {
     312             :                 dct_epit[i + Diff_len] = mult_r( dct_epit[i + Diff_len], sm_table_fx[i] ); /* Qx */
     313             :                 move16();
     314             :             }
     315             : 
     316             :             FOR( ; i < max_len; i++ )
     317             :             {
     318             :                 dct_epit[i + Diff_len] = 0;
     319             :                 move16();
     320             :             }
     321             :         }
     322             :         // PMT("in the rare case of 4 subfr, bfi_pitch_fx might be wrong")
     323             :         st_fx->bfi_pitch_fx = mean_fx( pitch_buf, nb_subfr ); /* Q6 */
     324             :         move16();
     325             :         st_fx->bfi_pitch_frame = st_fx->L_frame; /* Q0 */
     326             :         move16();
     327             : 
     328             :         Diff_len = add( Diff_len, 1 ); /* Q0 */
     329             :         st_fx->bpf_off = 0;
     330             :         move16();
     331             :     }
     332             :     ELSE
     333             :     {
     334             :         /* No adaptive (pitch) excitation contribution */
     335             :         st_fx->bpf_off = 1; /* Q0 */
     336             :         move16();
     337             :         set16_fx( dct_epit, 0, st_fx->L_frame );
     338             : 
     339             :         IF( EQ_16( st_fx->L_frame, L_FRAME16k ) )
     340             :         {
     341             :             set16_fx( pitch_buf, L_SUBFR16k * 64, NB_SUBFR16k );
     342             :         }
     343             :         ELSE
     344             :         {
     345             :             set16_fx( pitch_buf, L_SUBFR * 64, NB_SUBFR );
     346             :         }
     347             : 
     348             :         set16_fx( gain_buf, 0, NB_SUBFR16k );
     349             : 
     350             :         st_fx->bfi_pitch_fx = L_SUBFR * 64;
     351             :         move16();
     352             :         st_fx->bfi_pitch_frame = st_fx->L_frame; /* Q0 */
     353             :         move16();
     354             :         st_fx->lp_gainp_fx = 0;
     355             :         move16();
     356             :         st_fx->lp_gainc_fx = 0;
     357             :         move16();
     358             :         st_fx->tilt_code_fx = 0;
     359             :         move16();
     360             :         pit_band_idx = 0;
     361             :         move16();
     362             :         Diff_len = 0;
     363             :         move16();
     364             :     }
     365             : 
     366             :     /*--------------------------------------------------------------------------------------*
     367             :      * GSC decoder
     368             :      *--------------------------------------------------------------------------------------*/
     369             : 
     370             :     /* find the current total number of bits used */
     371             : 
     372             :     tmp_nb_bits_tot = st_fx->next_bit_pos; /* Q0 */
     373             :     move16();
     374             : 
     375             :     if ( st_fx->extl_brate > 0 )
     376             :     {
     377             :         /* subtract 1 bit for TBE/BWE BWE flag (bit counted in extl_brate) */
     378             :         tmp_nb_bits_tot = sub( tmp_nb_bits_tot, 1 ); /* Q0 */
     379             :     }
     380             : 
     381             :     test();
     382             :     if ( st_fx->coder_type == INACTIVE && LE_32( st_fx->core_brate, ACELP_9k60 ) )
     383             :     {
     384             :         tmp_nb_bits_tot = add( tmp_nb_bits_tot, 5 ); /* Q0 */
     385             :     }
     386             : 
     387             :     gsc_dec_fx( st_fx, dct_epit, pit_band_idx, Diff_len, tmp_nb_bits_tot, nb_subfr, st_fx->coder_type, &last_bin, lsf_new, exc_wo_nf, st_fx->Q_exc );
     388             :     /*--------------------------------------------------------------------------------------*
     389             :      * iDCT transform
     390             :      *--------------------------------------------------------------------------------------*/
     391             : 
     392             :     edct_16fx( dct_epit, exc, st_fx->L_frame, 7, st_fx->element_mode );
     393             :     edct_16fx( exc_wo_nf, exc_wo_nf, st_fx->L_frame, 7, st_fx->element_mode );
     394             :     /*----------------------------------------------------------------------*
     395             :      * Remove potential pre-echo in case an onset has been detected
     396             :      *----------------------------------------------------------------------*/
     397             : 
     398             :     pre_echo_att_fx( &hGSCDec->Last_frame_ener_fx, exc, gsc_attack_flag, st_fx->Q_exc, st_fx->last_coder_type, st_fx->L_frame );
     399             : 
     400             :     /*--------------------------------------------------------------------------------------*
     401             :      * Update BWE excitation
     402             :      *--------------------------------------------------------------------------------------*/
     403             : 
     404             :     IF( st_fx->hBWE_TD != NULL )
     405             :     {
     406             :         set16_fx( voice_factors, 0, NB_SUBFR16k );
     407             :         IF( EQ_16( st_fx->L_frame, L_FRAME16k ) )
     408             :         {
     409             :             interp_code_4over2_fx( exc, bwe_exc, st_fx->L_frame );
     410             :         }
     411             :         ELSE
     412             :         {
     413             :             interp_code_5over2_fx( exc, bwe_exc, L_FRAME );
     414             :         }
     415             :     }
     416             :     /*--------------------------------------------------------------------------------------*
     417             :      * Updates
     418             :      *--------------------------------------------------------------------------------------*/
     419             : 
     420             :     Copy( exc, exc2, st_fx->L_frame );      /* Q_exc */
     421             :     Copy( exc_wo_nf, exc, st_fx->L_frame ); /* Q_exc */
     422             : 
     423             :     /*--------------------------------------------------------------------------------------*
     424             :      * Channel aware mode parameters
     425             :      *--------------------------------------------------------------------------------------*/
     426             : 
     427             :     set16_fx( st_fx->tilt_code_dec_fx, 0, NB_SUBFR16k );
     428             : 
     429             :     return;
     430             : }
     431             : 
     432             : /*=========================================================================*/
     433             : /* FUNCTION : void decod_audio_ivas_fx();                                                                      */
     434             : /*-------------------------------------------------------------------------*/
     435             : /* PURPOSE :  Decode audio (AC) frames                                             */
     436             : /*-------------------------------------------------------------------------*/
     437             : /*    INPUT ARGUMENTS :                                                                                                */
     438             : /* _ (Word16[]) Aq                      : LP filter coefficient         Q12                        */
     439             : /* _ (Word16) coder_type                : coding type                           Q0                         */
     440             : /* _(Word16) Q_exc              :Q format of excitation                    */
     441             : /*-------------------------------------------------------------------------*/
     442             : /* OUTPUT ARGUMENTS :                                                                                                      */
     443             : /* _ (Word16[]) pitch_buf_fx    : Word16 pitch values for each subframe Q6*/
     444             : /* _ (Word16[]) voice_factors_fx: frame error rate                              Q15                 */
     445             : /*--------------------------------------------------------------------------*/
     446             : /* INPUT/OUTPUT ARGUMENTS :                                                                                         */
     447             : /*  Decoder_State *st_fx     : decoder  memory structure                 */
     448             : /* _ (Word16[]) exc_fx                  : adapt. excitation exc (Q_exc)                     */
     449             : /* _ (Word16[]) exc2_fx                 : adapt. excitation/total exc (Q_exc)       */
     450             : /*--------------------------------------------------------------------------*/
     451             : /* RETURN ARGUMENTS :                                                                                                       */
     452             : /* _ None                                                                                                                                   */
     453             : /*==========================================================================*/
     454             : #endif
     455       13797 : void decod_audio_ivas_fx(
     456             :     Decoder_State *st_fx,  /* i/o: decoder static memory                                                */
     457             :     Word16 dct_epit[],     /* o  : GSC excitation in DCT domain              Qx*/
     458             :     const Word16 *Aq,      /* i  : LP filter coefficient                     Q12*/
     459             :     Word16 *pitch_buf,     /* o  : Word16 pitch values for each subframe      Q6*/
     460             :     Word16 *voice_factors, /* o  : voicing factors                           Q15*/
     461             :     Word16 *exc,           /* i/o: adapt. excitation exc                     Q_exc*/
     462             :     Word16 *exc2,          /* i/o: adapt. excitation/total exc               Q_exc*/
     463             :     Word16 *bwe_exc,       /* o  : excitation for SWB TBE                    Q_exc*/
     464             :     Word16 *lsf_new        /* i  : ISFs at the end of the frame              Qx*/
     465             :     ,
     466             :     Word16 *gain_buf,                  /*Q14*/
     467             :     const Word16 tdm_lp_reuse_flag,    /* i  : LPC reuse flag                            Q0*/
     468             :     const Word16 tdm_low_rate_mode,    /* i  : secondary channel low rate mode flag      Q0*/
     469             :     const Word16 tdm_Pitch_reuse_flag, /* i  : primary channel pitch reuse flag          Q0*/
     470             :     const Word16 tdm_Pri_pitch_buf[]   /* i  : primary channel pitch buffer              Q6*/
     471             : )
     472             : {
     473             :     Word16 tmp_nb_bits_tot, pit_band_idx;
     474             :     Word16 code[4 * L_SUBFR];
     475             :     Word16 Diff_len, nb_subfr, i;
     476             :     Word16 nb_frame_flg;
     477       13797 :     Word16 Es_pred = 0;
     478             :     Word16 Len, max_len;
     479             :     Word16 gsc_attack_flag;
     480             : 
     481             :     Word16 low_pit;
     482             :     Word16 last_bin;
     483             :     Word16 nbits;
     484             : 
     485             :     Word16 exc_wo_nf[L_FRAME16k];
     486             :     GSC_DEC_HANDLE hGSCDec;
     487       13797 :     hGSCDec = st_fx->hGSCDec;
     488             : 
     489       13797 :     move16(); // Es_pred
     490             : 
     491             :     /*---------------------------------------------------------------*
     492             :      * Initialization
     493             :      *---------------------------------------------------------------*/
     494       13797 :     Diff_len = 0;
     495       13797 :     move16();
     496             : 
     497             :     /* decode GSC attack flag (used to reduce possible pre-echo) */
     498       13797 :     gsc_attack_flag = (Word16) get_next_indice_fx( st_fx, 1 ); /* Q0 */
     499       13797 :     move16();
     500             : 
     501             :     /* decode GSC SWB speech flag */
     502       13797 :     test();
     503       13797 :     test();
     504       13797 :     test();
     505       13797 :     test();
     506       13797 :     test();
     507       13797 :     test();
     508       13797 :     test();
     509       13797 :     IF( GE_16( st_fx->GSC_IVAS_mode, 1 ) || ( st_fx->coder_type != INACTIVE && ( ( st_fx->element_mode == EVS_MONO && GE_32( st_fx->total_brate, ACELP_13k20 ) ) ||
     510             :                                                                                  ( st_fx->element_mode > EVS_MONO && GT_32( st_fx->total_brate, MIN_BRATE_GSC_NOISY_FLAG ) && GE_16( st_fx->bwidth, SWB ) && !st_fx->flag_ACELP16k ) ) ) )
     511             :     {
     512        4823 :         st_fx->GSC_noisy_speech = (Word16) get_next_indice_fx( st_fx, 1 ); /* Q0 */
     513        4823 :         move16();
     514             :     }
     515             : 
     516             :     /* safety check in case of bit errors */
     517       13797 :     test();
     518       13797 :     test();
     519       13797 :     IF( st_fx->GSC_noisy_speech && LT_16( st_fx->bwidth, SWB ) && st_fx->GSC_IVAS_mode == 0 )
     520             :     {
     521           0 :         st_fx->BER_detect = 1; /* Q0 */
     522           0 :         move16();
     523           0 :         st_fx->GSC_noisy_speech = 0;
     524           0 :         move16();
     525             :     }
     526             : 
     527             :     /* set bit-allocation */
     528       13797 :     config_acelp1_IVAS( DEC, st_fx->total_brate, st_fx->core_brate, st_fx->core, st_fx->extl_orig, st_fx->extl_brate_orig, st_fx->L_frame, st_fx->GSC_noisy_speech, &( st_fx->acelp_cfg ), st_fx->next_bit_pos, st_fx->coder_type, st_fx->inactive_coder_type_flag, -1, 1, &nbits, NULL, st_fx->element_mode, &nbits /*dummy*/, tdm_lp_reuse_flag, tdm_low_rate_mode, st_fx->idchan, st_fx->active_cnt, tdm_Pitch_reuse_flag, st_fx->tdm_LRTD_flag, st_fx->GSC_IVAS_mode );
     529             : 
     530             :     /*---------------------------------------------------------------*
     531             :      * Decode energy dynamics
     532             :      *---------------------------------------------------------------*/
     533             : 
     534       13797 :     test();
     535       13797 :     test();
     536       13797 :     IF( GE_16( st_fx->GSC_IVAS_mode, 1 ) || ( EQ_16( st_fx->GSC_noisy_speech, 1 ) && st_fx->GSC_IVAS_mode == 0 ) )
     537             :     {
     538        4823 :         nb_subfr = NB_SUBFR; /* Q0 */
     539        4823 :         move16();
     540        4823 :         hGSCDec->cor_strong_limit = 0;
     541        4823 :         move16();
     542        4823 :         hGSCDec->noise_lev = NOISE_LEVEL_SP3; /* Q0 */
     543        4823 :         move16();
     544             : 
     545        4823 :         IF( GE_16( st_fx->GSC_IVAS_mode, 1 ) )
     546             :         {
     547        4128 :             test();
     548        4128 :             if ( LT_32( st_fx->core_brate, GSC_L_RATE_STG ) && LT_32( st_fx->GSC_IVAS_mode, 3 ) )
     549             :             {
     550        1822 :                 nb_subfr = 2; /* Q0 */
     551        1822 :                 move16();
     552             :             }
     553        4128 :             hGSCDec->noise_lev = NOISE_LEVEL_SP2; /* Q0 */
     554        4128 :             move16();
     555             : 
     556        4128 :             IF( EQ_16( st_fx->GSC_IVAS_mode, 3 ) ) /* Music like */
     557             :             {
     558        2279 :                 hGSCDec->noise_lev = NOISE_LEVEL_SP0; /* Q0 */
     559        2279 :                 move16();
     560             :             }
     561        1849 :             ELSE IF( st_fx->GSC_noisy_speech == 0 ) /* speech like but not noisy */
     562             :             {
     563        1750 :                 hGSCDec->noise_lev = NOISE_LEVEL_SP3; /* Q0 */
     564        1750 :                 move16();
     565             :             }
     566             :         }
     567             :     }
     568             :     ELSE
     569             :     {
     570        8974 :         IF( LE_32( st_fx->core_brate, ACELP_8k00 ) )
     571             :         {
     572        4715 :             hGSCDec->noise_lev = add( (Word16) get_next_indice_fx( st_fx, 2 ), NOISE_LEVEL_SP2 ); /* Q0 */
     573        4715 :             move16();
     574             :         }
     575             :         ELSE
     576             :         {
     577        4259 :             hGSCDec->noise_lev = add( (Word16) get_next_indice_fx( st_fx, 3 ), NOISE_LEVEL_SP0 ); /* Q0 */
     578        4259 :             move16();
     579             :         }
     580             : 
     581             :         /*---------------------------------------------------------------*
     582             :          * Decode number of subframes
     583             :          *---------------------------------------------------------------*/
     584             : 
     585        8974 :         test();
     586        8974 :         test();
     587        8974 :         IF( EQ_16( st_fx->L_frame, L_FRAME16k ) && ( LE_32( st_fx->core_brate, ACELP_13k20 ) || st_fx->coder_type == INACTIVE ) )
     588             :         {
     589        2525 :             hGSCDec->cor_strong_limit = 0;
     590        2525 :             move16();
     591        2525 :             nb_subfr = 1; /* Q0 */
     592        2525 :             move16();
     593             :         }
     594             :         ELSE
     595             :         {
     596        6449 :             hGSCDec->cor_strong_limit = 1; /* Q0 */
     597        6449 :             move16();
     598        6449 :             nb_subfr = SWNB_SUBFR; /* Q0 */
     599        6449 :             move16();
     600             : 
     601        6449 :             IF( GE_32( st_fx->core_brate, ACELP_9k60 ) )
     602             :             {
     603        1245 :                 nbits = 1; /* Q0 */
     604        1245 :                 move16();
     605             : 
     606        1245 :                 test();
     607        1245 :                 if ( EQ_16( st_fx->L_frame, L_FRAME16k ) && GE_32( st_fx->core_brate, MIN_RATE_4SBFR ) )
     608             :                 {
     609           0 :                     nbits = 2; /* Q0 */
     610           0 :                     move16();
     611             :                 }
     612             : 
     613        1245 :                 nb_frame_flg = (Word16) get_next_indice_fx( st_fx, nbits ); /* Q0 */
     614        1245 :                 move16();
     615             : 
     616        1245 :                 test();
     617        1245 :                 IF( s_and( nb_frame_flg, 0x1 ) == 0 )
     618             :                 {
     619        1238 :                     nb_subfr = 2 * SWNB_SUBFR;
     620        1238 :                     move16();
     621        1238 :                     hGSCDec->cor_strong_limit = 0;
     622        1238 :                     move16();
     623             :                 }
     624           7 :                 ELSE IF( EQ_16( st_fx->L_frame, L_FRAME16k ) && GE_32( st_fx->core_brate, MIN_RATE_4SBFR ) )
     625             :                 {
     626           0 :                     nb_subfr = 2 * SWNB_SUBFR; /* cor_strong already set to 1 */
     627           0 :                     move16();
     628             :                 }
     629             : 
     630        1245 :                 if ( EQ_16( shr( nb_frame_flg, 1 ), 1 ) )
     631             :                 {
     632           0 :                     nb_subfr = shl( nb_subfr, 1 ); /* Q0 */
     633             :                 }
     634             :             }
     635             :         }
     636             :     }
     637             : 
     638       13797 :     test();
     639       13797 :     if ( EQ_16( st_fx->L_frame, L_FRAME16k ) && EQ_16( nb_subfr, NB_SUBFR ) )
     640             :     {
     641           0 :         nb_subfr = NB_SUBFR16k; /* Q0 */
     642           0 :         move16();
     643             :     }
     644             : 
     645             :     /*---------------------------------------------------------------*
     646             :      * Decode the last band where the adaptive (pitch) contribution is significant
     647             :      *---------------------------------------------------------------*/
     648             : 
     649       13797 :     IF( LT_32( st_fx->core_brate, CFREQ_BITRATE ) )
     650             :     {
     651        8726 :         nbits = 3; /* Q0 */
     652        8726 :         move16();
     653        8726 :         test();
     654        8726 :         if ( LT_32( st_fx->core_brate, ACELP_9k60 ) && ( st_fx->coder_type == INACTIVE ) )
     655             :         {
     656         322 :             nbits = 1; /* Q0 */
     657         322 :             move16();
     658             :         }
     659             :     }
     660             :     ELSE
     661             :     {
     662        5071 :         nbits = 4; /* Q0 */
     663        5071 :         move16();
     664             :     }
     665       13797 :     test();
     666       13797 :     IF( LT_32( st_fx->core_brate, ACELP_9k60 ) && st_fx->coder_type != INACTIVE )
     667             :     {
     668        6778 :         pit_band_idx = 1; /* Q0 */
     669        6778 :         move16();
     670             :     }
     671             :     ELSE
     672             :     {
     673        7019 :         pit_band_idx = (Word16) get_next_indice_fx( st_fx, nbits ); /* Q0 */
     674        7019 :         move16();
     675             :     }
     676             : 
     677       13797 :     IF( pit_band_idx != 0 )
     678             :     {
     679       11717 :         IF( LT_32( st_fx->core_brate, ACELP_9k60 ) )
     680             :         {
     681        6992 :             pit_band_idx = 7 + BAND1k2; /* Q0 */
     682        6992 :             move16();                   /* At low rate, if pitch model is chosen, then for to be use on extented and constant frequency range */
     683             :         }
     684             :         ELSE
     685             :         {
     686        4725 :             pit_band_idx = add( pit_band_idx, BAND1k2 ); /* Q0 */
     687             :         }
     688             : 
     689             :         /* detect bit errors in the bitstream */
     690       11717 :         IF( GT_16( pit_band_idx, 13 ) ) /* The maximum decodable index is 10 + BAND1k2 (3) = 13 */
     691             :         {
     692           0 :             pit_band_idx = 13; /* Q0 */
     693           0 :             move16();
     694           0 :             st_fx->BER_detect = 1; /* Q0 */
     695           0 :             move16();
     696             :         }
     697       11717 :         Diff_len = mfreq_loc_div_25[pit_band_idx]; /* Q0 */
     698       11717 :         move16();
     699             :     }
     700       13797 :     hGSCDec->Last_GSC_pit_band_idx = pit_band_idx; /* Q0 */
     701       13797 :     move16();
     702             : 
     703             :     /*--------------------------------------------------------------------------------------*
     704             :      * Decode adaptive (pitch) excitation contribution
     705             :      * Reset unvaluable part of the adaptive (pitch) excitation contribution
     706             :      *--------------------------------------------------------------------------------------*/
     707       13797 :     IF( GT_16( pit_band_idx, BAND1k2 ) )
     708             :     {
     709             :         /*---------------------------------------------------------------*
     710             :          * Decode adaptive (pitch) excitation contribution
     711             :          *---------------------------------------------------------------*/
     712       11717 :         test();
     713       11717 :         test();
     714       11717 :         test();
     715       11717 :         test();
     716       11717 :         test();
     717       11717 :         test();
     718       11717 :         test();
     719       11717 :         test();
     720       11717 :         IF( !( ( st_fx->GSC_IVAS_mode > 0 ) && EQ_16( idiv1616( st_fx->L_frame, nb_subfr ), 2 * L_SUBFR ) && LT_16( st_fx->GSC_IVAS_mode, 3 ) ) &&
     721             :             ( ( GE_32( st_fx->core_brate, MIN_RATE_FCB ) || st_fx->GSC_noisy_speech ) &&
     722             :               ( ( EQ_16( nb_subfr, NB_SUBFR ) && EQ_16( st_fx->L_frame, L_FRAME ) ) || ( EQ_16( nb_subfr, NB_SUBFR16k ) && EQ_16( st_fx->L_frame, L_FRAME16k ) ) ) ) )
     723             :         {
     724             :             Word16 indice;
     725         695 :             nbits = Es_pred_bits_tbl[BIT_ALLOC_IDX_fx( st_fx->core_brate, GENERIC, -1, -1 )]; /* Q0 */
     726         695 :             move16();
     727         695 :             if ( st_fx->element_mode > EVS_MONO )
     728             :             {
     729         695 :                 nbits = 5; /* Q0 */
     730         695 :                 move16();
     731             :             }
     732             : 
     733         695 :             indice = get_next_indice_fx( st_fx, nbits ); /* Q0 */
     734             : 
     735         695 :             Es_pred_dec_fx( &Es_pred, indice, nbits, 0 );
     736             :         }
     737             : 
     738       11717 :         dec_pit_exc_ivas_fx( st_fx, Aq, st_fx->coder_type, Es_pred, pitch_buf, code, exc, bwe_exc, nb_subfr, gain_buf, tdm_Pitch_reuse_flag, tdm_Pri_pitch_buf );
     739             : 
     740       11717 :         IF( LT_32( st_fx->core_brate, ACELP_9k60 ) )
     741             :         {
     742        6992 :             minimum_fx( pitch_buf, shr( st_fx->L_frame, 6 ), &low_pit );
     743        6992 :             low_pit = shr( low_pit, 6 ); /*Q6 -> Q0 */
     744             : 
     745        6992 :             IF( LT_16( low_pit, 64 ) )
     746             :             {
     747        3062 :                 pit_band_idx = 9 + BAND1k2; /* Q0 */
     748        3062 :                 move16();
     749        3062 :                 if ( st_fx->bwidth == NB )
     750             :                 {
     751           0 :                     pit_band_idx = 7 + BAND1k2; /* Q0 */
     752           0 :                     move16();
     753             :                 }
     754             :             }
     755        3930 :             ELSE IF( LT_16( low_pit, 128 ) )
     756             :             {
     757        2209 :                 pit_band_idx = 5 + BAND1k2; /* Q0 */
     758        2209 :                 move16();
     759             :             }
     760             :             ELSE
     761             :             {
     762        1721 :                 pit_band_idx = 3 + BAND1k2; /* Q0 */
     763        1721 :                 move16();
     764             :             }
     765             : 
     766        6992 :             Diff_len = mfreq_loc_div_25[pit_band_idx]; /* Q0 */
     767        6992 :             move16();
     768        6992 :             hGSCDec->Last_GSC_pit_band_idx = pit_band_idx; /* Q0 */
     769        6992 :             move16();
     770             :         }
     771             : 
     772             :         /*---------------------------------------------------------------*
     773             :          * DCT transform
     774             :          *---------------------------------------------------------------*/
     775       11717 :         edct_16fx( exc, dct_epit, st_fx->L_frame, 7, st_fx->element_mode );
     776             : 
     777             :         /*---------------------------------------------------------------*
     778             :          * Reset unvaluable part of the adaptive (pitch) excitation contribution
     779             :          *---------------------------------------------------------------*/
     780             : 
     781       11717 :         max_len = sub( st_fx->L_frame, Diff_len ); /* Q0 */
     782             : 
     783       11717 :         if ( st_fx->bwidth == NB )
     784             :         {
     785           0 :             max_len = sub( 160, Diff_len ); /* Q0 */
     786             :         }
     787             : 
     788       11717 :         Len = 80;
     789       11717 :         move16();
     790       11717 :         if ( LT_16( max_len, 80 ) )
     791             :         {
     792        3312 :             Len = max_len; /* Q0 */
     793        3312 :             move16();
     794             :         }
     795             : 
     796       11717 :         test();
     797       11717 :         IF( EQ_32( st_fx->core_brate, ACELP_8k00 ) && st_fx->bwidth != NB )
     798             :         {
     799        1960 :             FOR( i = 0; i < max_len; i++ )
     800             :             {
     801        1940 :                 dct_epit[i + Diff_len] = 0;
     802        1940 :                 move16();
     803             :             }
     804             :         }
     805             :         ELSE
     806             :         {
     807      895126 :             FOR( i = 0; i < Len; i++ )
     808             :             {
     809      883429 :                 dct_epit[i + Diff_len] = mult_r( dct_epit[i + Diff_len], sm_table_fx[i] ); /* Qx */
     810      883429 :                 move16();
     811             :             }
     812             : 
     813      616205 :             FOR( ; i < max_len; i++ )
     814             :             {
     815      604508 :                 dct_epit[i + Diff_len] = 0;
     816      604508 :                 move16();
     817             :             }
     818             :         }
     819             :         // PMT("in the rare case of 4 subfr, bfi_pitch_fx might be wrong")
     820       11717 :         st_fx->bfi_pitch_fx = mean_fx( pitch_buf, nb_subfr ); /* Q6 */
     821       11717 :         move16();
     822       11717 :         st_fx->bfi_pitch_frame = st_fx->L_frame; /* Q0 */
     823       11717 :         move16();
     824             : 
     825       11717 :         Diff_len = add( Diff_len, 1 ); /* Q0 */
     826       11717 :         st_fx->bpf_off = 0;
     827       11717 :         move16();
     828             :     }
     829             :     ELSE
     830             :     {
     831             :         /* No adaptive (pitch) excitation contribution */
     832        2080 :         st_fx->bpf_off = 1;
     833        2080 :         move16();
     834        2080 :         set16_fx( dct_epit, 0, st_fx->L_frame );
     835             : 
     836        2080 :         IF( EQ_16( st_fx->L_frame, L_FRAME16k ) )
     837             :         {
     838        1501 :             set16_fx( pitch_buf, ( L_SUBFR16k * 64 ), NB_SUBFR16k );
     839             :         }
     840             :         ELSE
     841             :         {
     842         579 :             set16_fx( pitch_buf, L_SUBFR * 64, NB_SUBFR );
     843             :         }
     844             : 
     845        2080 :         set16_fx( gain_buf, 0, NB_SUBFR16k );
     846             : 
     847        2080 :         st_fx->bfi_pitch_fx = L_SUBFR * 64;
     848        2080 :         move16();
     849        2080 :         st_fx->bfi_pitch_frame = st_fx->L_frame; /* Q0 */
     850        2080 :         move16();
     851        2080 :         st_fx->lp_gainp_fx = 0;
     852        2080 :         move16();
     853        2080 :         st_fx->lp_gainc_fx = 0;
     854        2080 :         move16();
     855        2080 :         st_fx->tilt_code_fx = 0;
     856        2080 :         move16();
     857        2080 :         pit_band_idx = 0;
     858        2080 :         move16();
     859        2080 :         Diff_len = 0;
     860        2080 :         move16();
     861             :     }
     862             : 
     863             :     /*--------------------------------------------------------------------------------------*
     864             :      * GSC decoder
     865             :      *--------------------------------------------------------------------------------------*/
     866             : 
     867             :     /* find the current total number of bits used */
     868             : 
     869       13797 :     tmp_nb_bits_tot = st_fx->next_bit_pos; /* Q0 */
     870       13797 :     move16();
     871             : 
     872       13797 :     if ( st_fx->extl_brate_orig > 0 )
     873             :     {
     874             :         /* subtract 1 bit for TBE/BWE BWE flag (bit counted in extl_brate) */
     875       13682 :         tmp_nb_bits_tot = sub( tmp_nb_bits_tot, 1 ); /* Q0 */
     876             :     }
     877             : 
     878       13797 :     test();
     879       13797 :     test();
     880       13797 :     if ( st_fx->coder_type == INACTIVE && LE_32( st_fx->core_brate, ACELP_9k60 ) && st_fx->idchan == 0 )
     881             :     {
     882         337 :         tmp_nb_bits_tot = add( tmp_nb_bits_tot, 5 ); /* Q0 */
     883             :     }
     884             : 
     885       13797 :     IF( EQ_16( st_fx->idchan, 1 ) )
     886             :     {
     887           0 :         tmp_nb_bits_tot = add( tmp_nb_bits_tot, TDM_SIGNAL_BITS_READ_FROM_THE_END_OF_BS ); /* Q0 */
     888           0 :         if ( EQ_16( st_fx->tdm_LRTD_flag, 1 ) )
     889             :         {
     890           0 :             tmp_nb_bits_tot = sub( tmp_nb_bits_tot, STEREO_BITS_TCA ); /* Q0 */
     891             :         }
     892             :     }
     893             : 
     894             : 
     895       13797 :     Word16 Q_exc_old = st_fx->Q_exc;
     896       13797 :     move16();
     897       13797 :     gsc_dec_ivas_fx( st_fx, dct_epit, pit_band_idx, Diff_len, tmp_nb_bits_tot, nb_subfr, st_fx->coder_type, &last_bin, lsf_new, exc_wo_nf, &st_fx->Q_exc );
     898             : 
     899       13797 :     IF( NE_16( Q_exc_old, st_fx->Q_exc ) )
     900             :     {
     901           4 :         Q_exc_old = sub( Q_exc_old, st_fx->Q_exc );
     902           4 :         IF( st_fx->hGSCDec )
     903             :         {
     904           4 :             Scale_sig( st_fx->hGSCDec->last_exc_dct_in_fx, L_FRAME16k, Q_exc_old ); /* Q_exc_old */
     905             :         }
     906           4 :         IF( bwe_exc )
     907             :         {
     908           4 :             Scale_sig( bwe_exc - PIT16k_MAX * 2, ( PIT16k_MAX + ( L_FRAME16k + 1 ) + L_SUBFR16k ) * 2, Q_exc_old ); /* Q_exc_old */
     909             :         }
     910             :     }
     911             :     /*--------------------------------------------------------------------------------------*
     912             :      * iDCT transform
     913             :      *--------------------------------------------------------------------------------------*/
     914             : 
     915       13797 :     edct_16fx( dct_epit, exc, st_fx->L_frame, 7, st_fx->element_mode );
     916       13797 :     edct_16fx( exc_wo_nf, exc_wo_nf, st_fx->L_frame, 7, st_fx->element_mode );
     917             :     /*----------------------------------------------------------------------*
     918             :      * Remove potential pre-echo in case an onset has been detected
     919             :      *----------------------------------------------------------------------*/
     920             : 
     921       13797 :     pre_echo_att_fx( &hGSCDec->Last_frame_ener_fx, exc, gsc_attack_flag, st_fx->Q_exc, st_fx->last_coder_type, st_fx->L_frame );
     922             : 
     923             :     /*--------------------------------------------------------------------------------------*
     924             :      * Update BWE excitation
     925             :      *--------------------------------------------------------------------------------------*/
     926             : 
     927       13797 :     IF( st_fx->hBWE_TD != NULL )
     928             :     {
     929       13797 :         set16_fx( voice_factors, 0, NB_SUBFR16k );
     930       13797 :         IF( EQ_16( st_fx->L_frame, L_FRAME16k ) )
     931             :         {
     932        2525 :             interp_code_4over2_fx( exc, bwe_exc, st_fx->L_frame );
     933             :         }
     934             :         ELSE
     935             :         {
     936       11272 :             interp_code_5over2_fx( exc, bwe_exc, L_FRAME );
     937             :         }
     938             :     }
     939             :     /*--------------------------------------------------------------------------------------*
     940             :      * Updates
     941             :      *--------------------------------------------------------------------------------------*/
     942             : 
     943       13797 :     Copy( exc, exc2, st_fx->L_frame );      /* Q_exc */
     944       13797 :     Copy( exc_wo_nf, exc, st_fx->L_frame ); /* Q_exc */
     945             : 
     946             :     /*--------------------------------------------------------------------------------------*
     947             :      * Channel aware mode parameters
     948             :      *--------------------------------------------------------------------------------------*/
     949             : 
     950       13797 :     set16_fx( st_fx->tilt_code_dec_fx, 0, NB_SUBFR16k );
     951             : 
     952       13797 :     return;
     953             : }
     954             : 
     955             : /*==========================================================================*/
     956             : /* FUNCTION : void gsc_dec_fx   ()                                                                  */
     957             : /*--------------------------------------------------------------------------*/
     958             : /* PURPOSE  :  Generic audio signal decoder                                 */
     959             : /*--------------------------------------------------------------------------*/
     960             : /*    INPUT ARGUMENTS :                                                                                                 */
     961             : /* _ (Word16) pit_band_idx       : bin position of the cut-off frequency Q0 */
     962             : /* _ (Word16) Diff_len           : Lenght of the difference signal       Q0 */
     963             : /* _ (Word16) coder_type         : coding type                                       Q0 */
     964             : /* _ (Word16) bits_used          : Number of bit used before frequency Q Q0 */
     965             : /* _ (Word16) nb_subfr           : Number of subframe considered         Q0 */
     966             : /* _ (Word16)  Qexc              : Q format of exc_dct_in                               */
     967             : /*--------------------------------------------------------------------------*/
     968             : /* OUTPUT ARGUMENTS :                                                                                                       */
     969             : /* _ None                                                                   */
     970             : /*--------------------------------------------------------------------------*/
     971             : /* INPUT/OUTPUT ARGUMENTS :                                                                                         */
     972             : /*   Decoder_State *st_fx:Decoder State Structure                        */
     973             : /* _ (Word16[]) exc_dct_in : dctof pitch-only excitation / total excitation Qexc*/
     974             : /*--------------------------------------------------------------------------*/
     975             : /* RETURN ARGUMENTS :                                                                                                       */
     976             : /* _None                                                                            */
     977             : /*==========================================================================*/
     978             : 
     979             : #ifndef REMOVE_EVS_DUPLICATES
     980             : void gsc_dec_fx(
     981             :     Decoder_State *st_fx,      /* i/o: State structure                                     */
     982             :     Word16 exc_dct_in[],       /* i/o: dct of pitch-only excitation / total excitation     Q_exc*/
     983             :     const Word16 pit_band_idx, /* i  : bin position of the cut-off frequency               Q0*/
     984             :     const Word16 Diff_len,     /* i  : Lenght of the difference signal (before pure spectral)   Q0*/
     985             :     const Word16 bits_used,    /* i  : Number of bit used before frequency Q               Q0*/
     986             :     const Word16 nb_subfr,     /* i  : Number of subframe considered                       Q0*/
     987             :     const Word16 coder_type,   /* i  : coding type                                         Q0*/
     988             :     Word16 *last_bin,          /* i  : last bin of bit allocation                          Q0*/
     989             :     const Word16 *lsf_new,     /* i  : ISFs at the end of the frame                        Qx*/
     990             :     Word16 *exc_wo_nf,         /* o  : excitation (in f domain) without noisefill          Q_exc*/
     991             :     Word16 Q_exc )
     992             : {
     993             :     Word16 i, j, bit, nb_subbands, pvq_len;
     994             :     Word16 bitallocation_band[MBANDS_GN_BITALLOC16k];
     995             :     Word16 bitallocation_exc[2];
     996             :     Word16 Ener_per_bd_iQ[MBANDS_GN_BITALLOC16k];
     997             :     Word16 max_ener_band[MBANDS_GN_BITALLOC16k];
     998             :     Word16 exc_diffQ[L_FRAME16k];
     999             :     Word16 bits_per_bands[MBANDS_GN_BITALLOC16k];
    1000             :     Word16 concat_out[L_FRAME16k];
    1001             :     Word16 inpulses_fx[NB_SFM];
    1002             :     Word16 imaxpulse_fx[NB_SFM];
    1003             :     Word16 mean_gain;
    1004             :     Word16 Mbands_gn = 16;
    1005             :     Word16 Qexc_diffQ = Q_PVQ_OUT;
    1006             :     Word32 L_tmp;
    1007             :     Word16 Q_tmp;
    1008             :     Word16 seed_init;
    1009             :     GSC_DEC_HANDLE hGSCDec;
    1010             :     hGSCDec = st_fx->hGSCDec;
    1011             :     move16();
    1012             :     move16();
    1013             : 
    1014             :     move16(); // for Mbands_gn
    1015             :     move16(); // for Qexc_diffQ
    1016             :     set16_fx( inpulses_fx, 0, NB_SFM );
    1017             :     set16_fx( imaxpulse_fx, 0, NB_SFM );
    1018             : 
    1019             :     /*--------------------------------------------------------------------------------------*
    1020             :      * Initialization
    1021             :      *--------------------------------------------------------------------------------------*/
    1022             :     bit = bits_used;
    1023             :     move16();
    1024             : 
    1025             :     set16_fx( exc_diffQ, 0, st_fx->L_frame );
    1026             : 
    1027             :     /*--------------------------------------------------------------------------------------*
    1028             :      * Gain decoding
    1029             :      *--------------------------------------------------------------------------------------*/
    1030             : 
    1031             :     test();
    1032             :     IF( st_fx->bfi || st_fx->BER_detect )
    1033             :     {
    1034             :         /* copy old gain */
    1035             :         Copy( hGSCDec->old_y_gain_fx, Ener_per_bd_iQ, Mbands_gn ); /* Q_old_gain */
    1036             :         mean_gain = mult_r( st_fx->lp_gainc_fx, 3277 );            /*Q3*/
    1037             :         FOR( i = 0; i < Mbands_gn; i++ )
    1038             :         {
    1039             :             Ener_per_bd_iQ[i] = add( Ener_per_bd_iQ[i], shl( mean_gain, 9 ) ); /*Q12*/
    1040             :             move16();
    1041             :         }
    1042             : 
    1043             :         st_fx->lp_gainc_fx = mult_r( st_fx->lp_gainc_fx, 32112 ); /*Q3*/
    1044             :         move16();
    1045             :     }
    1046             :     ELSE
    1047             :     {
    1048             :         mean_gain = gsc_gaindec_fx( st_fx, Ener_per_bd_iQ, st_fx->core_brate, hGSCDec->old_y_gain_fx, coder_type, st_fx->bwidth ); /* Q12 */
    1049             : 
    1050             :         st_fx->lp_gainc_fx = mult_r( 640, mean_gain ); /*10 in Q6 x Q12 -> lp_gainc in Q3 */
    1051             :         move16();
    1052             :     }
    1053             : 
    1054             :     *last_bin = 0;
    1055             :     move16();
    1056             :     test();
    1057             :     IF( EQ_32( st_fx->core_brate, ACELP_8k00 ) && st_fx->bwidth != NB )
    1058             :     {
    1059             :         bitallocation_exc[0] = 0;
    1060             :         move16();
    1061             :         bitallocation_exc[1] = 0;
    1062             :         move16();
    1063             :     }
    1064             : 
    1065             :     set16_fx( bitallocation_band, 0, MBANDS_GN );
    1066             : 
    1067             :     test();
    1068             :     IF( ( EQ_16( st_fx->bfi, 1 ) ) || st_fx->BER_detect )
    1069             :     {
    1070             :         /*--------------------------------------------------------------------------------------*
    1071             :          * Copy old spectrum
    1072             :          * reduce spectral dynamic
    1073             :          * save spectrum
    1074             :          *--------------------------------------------------------------------------------------*/
    1075             : 
    1076             :         test();
    1077             :         IF( EQ_16( st_fx->last_good, INACTIVE_CLAS ) || EQ_16( st_fx->Last_GSC_noisy_speech_flag, 1 ) )
    1078             :         {
    1079             :             FOR( i = 0; i < st_fx->L_frame; i++ )
    1080             :             {
    1081             :                 L_tmp = L_shr( L_mult( Random( &hGSCDec->seed_tcx ), 26214 ), 5 ); /*Q10*/
    1082             :                 L_tmp = L_mac( L_tmp, hGSCDec->Last_GSC_spectrum_fx[i], 6554 );    /* Q10 */
    1083             :                 hGSCDec->Last_GSC_spectrum_fx[i] = round_fx( L_tmp );              /*Q10*/
    1084             :                 move16();
    1085             :             }
    1086             :         }
    1087             : 
    1088             :         Copy( hGSCDec->Last_GSC_spectrum_fx, exc_diffQ, st_fx->L_frame ); /* Q10 */
    1089             : 
    1090             :         FOR( i = 0; i < st_fx->L_frame; i++ )
    1091             :         {
    1092             :             hGSCDec->Last_GSC_spectrum_fx[i] = mult_r( hGSCDec->Last_GSC_spectrum_fx[i], 24576 ); /*Q10*/
    1093             :             move16();
    1094             :         }
    1095             :     }
    1096             :     ELSE
    1097             :     {
    1098             :         /*--------------------------------------------------------------------------------------*
    1099             :          * PVQ decoder
    1100             :          *--------------------------------------------------------------------------------------*/
    1101             : 
    1102             :         bands_and_bit_alloc_fx( hGSCDec->cor_strong_limit, hGSCDec->noise_lev, st_fx->core_brate, Diff_len, bit, &bit, Ener_per_bd_iQ,
    1103             :                                 max_ener_band, bits_per_bands, &nb_subbands, NULL, NULL, &pvq_len, coder_type, st_fx->bwidth, st_fx->GSC_noisy_speech,
    1104             :                                 st_fx->L_frame, st_fx->element_mode, st_fx->GSC_IVAS_mode );
    1105             : 
    1106             :         {
    1107             :             pvq_core_dec_fx( st_fx, gsc_sfm_start, gsc_sfm_end, gsc_sfm_size, concat_out, &Q_tmp, bit, nb_subbands, bits_per_bands, NULL, inpulses_fx, imaxpulse_fx, ACELP_CORE );
    1108             :             Scale_sig( concat_out, gsc_sfm_end[nb_subbands - 1], sub( Q_PVQ_OUT, Q_tmp ) ); /* Q_PVQ_OUT */
    1109             :         }
    1110             :         seed_init = 0;
    1111             :         move16();
    1112             : 
    1113             :         /* Reorder Q bands */
    1114             :         FOR( j = 0; j < nb_subbands; j++ )
    1115             :         {
    1116             :             Copy( concat_out + shl( j, 4 ), exc_diffQ + shl( max_ener_band[j], 4 ), 16 ); /* Q_PVQ_OUT */
    1117             : 
    1118             :             *last_bin = s_max( *last_bin, max_ener_band[j] ); /* Q0 */
    1119             :             move16();
    1120             : 
    1121             :             bitallocation_band[max_ener_band[j]] = 1; /* Q0 */
    1122             :             move16();
    1123             : 
    1124             :             seed_init = add( seed_init, inpulses_fx[j] ); /* Q0 */
    1125             :         }
    1126             :         test();
    1127             :         IF( NE_16( st_fx->last_coder_type, AUDIO )         /* First audio frame */
    1128             :             && NE_16( st_fx->last_coder_type, UNVOICED ) ) /* last_coder_type == INACTIVE is overwritten in update_dec to UNVOICED */
    1129             :         {
    1130             :             FOR( j = 0; j < nb_subbands * 16; j++ )
    1131             :             {
    1132             :                 IF( concat_out[j] > 0 )
    1133             :                 {
    1134             :                     seed_init = extract_l( L_shl( seed_init, 3 ) ); /* Q0 */
    1135             :                 }
    1136             :                 if ( concat_out[j] < 0 )
    1137             :                 {
    1138             :                     seed_init = add( seed_init, 3 ); /* Q0 */
    1139             :                     move16();
    1140             :                 }
    1141             :             }
    1142             : 
    1143             :             hGSCDec->seed_tcx = seed_init; /* Q0 */
    1144             :             move16();
    1145             :         }
    1146             :         test();
    1147             :         IF( EQ_32( st_fx->core_brate, ACELP_8k00 ) && st_fx->bwidth != NB )
    1148             :         {
    1149             :             if ( exc_diffQ[L_FRAME8k - 2] != 0 )
    1150             :             {
    1151             :                 bitallocation_exc[0] = 1; /* Q0 */
    1152             :                 move16();
    1153             :             }
    1154             : 
    1155             :             if ( exc_diffQ[L_FRAME8k - 1] != 0 )
    1156             :             {
    1157             :                 bitallocation_exc[1] = 1; /* Q0 */
    1158             :                 move16();
    1159             :             }
    1160             :         }
    1161             : 
    1162             :         Copy( exc_diffQ, hGSCDec->Last_GSC_spectrum_fx, st_fx->L_frame ); /* Q_PVQ_OUT */
    1163             : 
    1164             :         /*--------------------------------------------------------------------------------------*
    1165             :          * Skip adaptive (pitch) contribution frequency band (no noise added over the time contribution)
    1166             :          * Find x pulses between 1.6-3.2kHz to code in the spectrum of the residual signal
    1167             :          * Gain is based on the inter-correlation gain between the pulses found and residual signal
    1168             :          *--------------------------------------------------------------------------------------*/
    1169             : 
    1170             :         freq_dnw_scaling_fx( hGSCDec->cor_strong_limit, st_fx->coder_type, hGSCDec->noise_lev, st_fx->core_brate, exc_diffQ, Qexc_diffQ, st_fx->L_frame );
    1171             :     }
    1172             : 
    1173             :     /*--------------------------------------------------------------------------------------*
    1174             :      * Estimate noise level
    1175             :      *--------------------------------------------------------------------------------------*/
    1176             : 
    1177             :     highband_exc_dct_in_fx( st_fx->core_brate, mfreq_bindiv_loc, *last_bin, Diff_len, hGSCDec->noise_lev, pit_band_idx, exc_diffQ,
    1178             :                             &hGSCDec->seed_tcx, Ener_per_bd_iQ, nb_subfr, exc_dct_in, st_fx->last_coder_type, bitallocation_band, lsf_new,
    1179             :                             hGSCDec->last_exc_dct_in_fx, &hGSCDec->last_ener_fx, hGSCDec->last_bitallocation_band, bitallocation_exc, st_fx->bfi, coder_type,
    1180             :                             st_fx->bwidth, exc_wo_nf, Qexc_diffQ, Q_exc, st_fx->GSC_noisy_speech, hGSCDec->lt_ener_per_band_fx, st_fx->L_frame, st_fx->element_mode, st_fx->GSC_IVAS_mode );
    1181             : 
    1182             :     exc_dct_in[0] = 0;
    1183             :     move16();
    1184             : 
    1185             :     return;
    1186             : }
    1187             : 
    1188             : /*==========================================================================*/
    1189             : /* FUNCTION : void gsc_dec_ivas_fx      ()                                                                  */
    1190             : /*--------------------------------------------------------------------------*/
    1191             : /* PURPOSE  :  Generic audio signal decoder                                 */
    1192             : /*--------------------------------------------------------------------------*/
    1193             : /*    INPUT ARGUMENTS :                                                                                                 */
    1194             : /* _ (Word16) pit_band_idx       : bin position of the cut-off frequency Q0 */
    1195             : /* _ (Word16) Diff_len           : Lenght of the difference signal       Q0 */
    1196             : /* _ (Word16) coder_type         : coding type                                       Q0 */
    1197             : /* _ (Word16) bits_used          : Number of bit used before frequency Q Q0 */
    1198             : /* _ (Word16) nb_subfr           : Number of subframe considered         Q0 */
    1199             : /* _ (Word16)  Qexc              : Q format of exc_dct_in                               */
    1200             : /*--------------------------------------------------------------------------*/
    1201             : /* OUTPUT ARGUMENTS :                                                                                                       */
    1202             : /* _ None                                                                   */
    1203             : /*--------------------------------------------------------------------------*/
    1204             : /* INPUT/OUTPUT ARGUMENTS :                                                                                         */
    1205             : /*   Decoder_State *st_fx:Decoder State Structure                        */
    1206             : /* _ (Word16[]) exc_dct_in : dctof pitch-only excitation / total excitation Qexc*/
    1207             : /*--------------------------------------------------------------------------*/
    1208             : /* RETURN ARGUMENTS :                                                                                                       */
    1209             : /* _None                                                                            */
    1210             : /*==========================================================================*/
    1211             : #endif
    1212       20762 : void gsc_dec_ivas_fx(
    1213             :     Decoder_State *st_fx,      /* i/o: State structure                                     */
    1214             :     Word16 exc_dct_in[],       /* i/o: dct of pitch-only excitation / total excitation     Q_exc*/
    1215             :     const Word16 pit_band_idx, /* i  : bin position of the cut-off frequency               `    Q0*/
    1216             :     const Word16 Diff_len,     /* i  : Lenght of the difference signal (before pure spectral)   Q0*/
    1217             :     const Word16 bits_used,    /* i  : Number of bit used before frequency Q                                    Q0*/
    1218             :     const Word16 nb_subfr,     /* i  : Number of subframe considered                                                    Q0*/
    1219             :     const Word16 coder_type,   /* i  : coding type                                                                                              Q0*/
    1220             :     Word16 *last_bin,          /* i  : last bin of bit allocation                                                               Q0*/
    1221             :     const Word16 *lsf_new,     /* i  : ISFs at the end of the frame                        Qx*/
    1222             :     Word16 *exc_wo_nf,         /* o  : excitation (in f domain) without noisefill          Q_exc*/
    1223             :     Word16 *Q_exc )
    1224             : {
    1225             :     Word16 i, j, bit, nb_subbands, pvq_len;
    1226             :     Word16 bitallocation_band[MBANDS_GN_BITALLOC16k];
    1227             :     Word16 bitallocation_exc[2];
    1228             :     Word16 Ener_per_bd_iQ[MBANDS_GN_BITALLOC16k];
    1229             :     Word16 max_ener_band[MBANDS_GN_BITALLOC16k];
    1230             :     Word16 exc_diffQ[L_FRAME16k];
    1231             :     Word16 bits_per_bands[MBANDS_GN_BITALLOC16k];
    1232             :     Word16 concat_out[L_FRAME16k];
    1233             :     Word16 max_eq, max_eq_val;
    1234             :     Word16 inpulses_fx[NB_SFM];
    1235             :     Word16 imaxpulse_fx[NB_SFM];
    1236             :     Word16 mean_gain;
    1237       20762 :     Word16 Mbands_gn = MBANDS_GN;
    1238       20762 :     Word16 Qexc_diffQ = Q_PVQ_OUT;
    1239             :     Word32 L_tmp;
    1240             :     Word16 Q_tmp;
    1241             :     Word16 seed_init;
    1242             :     GSC_DEC_HANDLE hGSCDec;
    1243       20762 :     hGSCDec = st_fx->hGSCDec;
    1244             : 
    1245       20762 :     move16(); /* for Mbands_gn */
    1246       20762 :     move16(); /* for Qexc_diffQ */
    1247       20762 :     set16_fx( inpulses_fx, 0, NB_SFM );
    1248       20762 :     set16_fx( imaxpulse_fx, 0, NB_SFM );
    1249             : 
    1250             :     /*--------------------------------------------------------------------------------------*
    1251             :      * Initialization
    1252             :      *--------------------------------------------------------------------------------------*/
    1253       20762 :     bit = bits_used;
    1254       20762 :     move16();
    1255             : 
    1256       20762 :     test();
    1257       20762 :     test();
    1258       20762 :     test();
    1259       20762 :     if ( coder_type == INACTIVE && ( EQ_16( st_fx->tdm_LRTD_flag, 1 ) || EQ_16( st_fx->element_mode, IVAS_SCE ) ) && LE_32( st_fx->core_brate, GSC_LRES_GAINQ_LIMIT ) )
    1260             :     {
    1261        6314 :         bit = add( bit, GSC_LRES_NB_NITS ); /* Q0 */
    1262             :     }
    1263             : 
    1264       20762 :     if ( EQ_16( st_fx->L_frame, L_FRAME16k ) )
    1265             :     {
    1266        2529 :         Mbands_gn = MBANDS_GN16k; /* Q0 */
    1267        2529 :         move16();
    1268             :     }
    1269             : 
    1270       20762 :     set16_fx( exc_diffQ, 0, st_fx->L_frame );
    1271             : 
    1272             :     /*--------------------------------------------------------------------------------------*
    1273             :      * Gain decoding
    1274             :      *--------------------------------------------------------------------------------------*/
    1275             : 
    1276       20762 :     test();
    1277       20762 :     IF( st_fx->bfi || st_fx->BER_detect )
    1278             :     {
    1279             :         /* copy old gain */
    1280         202 :         Copy( hGSCDec->old_y_gain_fx, Ener_per_bd_iQ, Mbands_gn ); /* Q_old_gain */
    1281         202 :         mean_gain = mult_r( st_fx->lp_gainc_fx, 3277 );            /*Q3*/
    1282        3442 :         FOR( i = 0; i < Mbands_gn; i++ )
    1283             :         {
    1284        3240 :             Ener_per_bd_iQ[i] = add( Ener_per_bd_iQ[i], shl( mean_gain, 9 ) ); /*Q12*/
    1285        3240 :             move16();
    1286             :         }
    1287             : 
    1288         202 :         st_fx->lp_gainc_fx = mult_r( st_fx->lp_gainc_fx, 32112 ); /*Q3*/
    1289         202 :         move16();
    1290             :     }
    1291             :     ELSE
    1292             :     {
    1293       20560 :         i = 0;
    1294       20560 :         move16();
    1295       97130 :         WHILE( LT_16( i, SIZE_BRATE_INTERMED_TBL ) )
    1296             :         {
    1297       97130 :             if ( LE_32( st_fx->core_brate, brate_intermed_tbl[i] ) )
    1298             :             {
    1299       20560 :                 break;
    1300             :             }
    1301       76570 :             i++;
    1302             :         }
    1303             : 
    1304       20560 :         test();
    1305       20560 :         test();
    1306       20560 :         test();
    1307       30333 :         if ( st_fx->element_mode > EVS_MONO && EQ_16( coder_type, AUDIO ) &&
    1308       16177 :              LE_32( st_fx->core_brate, STEREO_GSC_BIT_RATE_ALLOC ) && EQ_32( brate_intermed_tbl[i], ACELP_9k60 ) ) /* Bit allocation is mapped to 8 kb/s instead of 9.6 kb/s in this case */
    1309             :         {
    1310        1230 :             i--;
    1311             :         }
    1312             : 
    1313             : #ifdef REMOVE_EVS_DUPLICATES
    1314       20560 :         IF( EQ_16( st_fx->element_mode, EVS_MONO ) )
    1315             :         {
    1316           6 :             mean_gain = gsc_gaindec_fx( st_fx, Ener_per_bd_iQ, brate_intermed_tbl[i], hGSCDec->old_y_gain_fx, coder_type, st_fx->bwidth ); /* Q3 */
    1317             :         }
    1318             :         ELSE
    1319             : #endif
    1320             :         {
    1321       20554 :             mean_gain = gsc_gaindec_ivas_fx( st_fx, Ener_per_bd_iQ, brate_intermed_tbl[i], hGSCDec->old_y_gain_fx, coder_type, st_fx->bwidth ); /* Q3 */
    1322             :         }
    1323             : 
    1324       20560 :         st_fx->lp_gainc_fx = mult_r( 640, mean_gain ); /*10 in Q6 x Q12 -> lp_gainc in Q3 */
    1325       20560 :         move16();
    1326             :     }
    1327             : 
    1328       20762 :     *last_bin = 0;
    1329       20762 :     move16();
    1330       20762 :     test();
    1331       20762 :     IF( EQ_32( st_fx->core_brate, ACELP_8k00 ) && st_fx->bwidth != NB )
    1332             :     {
    1333          20 :         bitallocation_exc[0] = 0;
    1334          20 :         move16();
    1335          20 :         bitallocation_exc[1] = 0;
    1336          20 :         move16();
    1337             :     }
    1338             : 
    1339       20762 :     set16_fx( bitallocation_band, 0, MBANDS_GN );
    1340             : 
    1341       20762 :     test();
    1342       20762 :     IF( ( EQ_16( st_fx->bfi, 1 ) ) || st_fx->BER_detect )
    1343             :     {
    1344             :         /*--------------------------------------------------------------------------------------*
    1345             :          * Copy old spectrum
    1346             :          * reduce spectral dynamic
    1347             :          * save spectrum
    1348             :          *--------------------------------------------------------------------------------------*/
    1349             : 
    1350         202 :         max_eq = 32767;
    1351         202 :         move16();
    1352             : 
    1353         202 :         test();
    1354         202 :         IF( EQ_16( st_fx->last_good, INACTIVE_CLAS ) || EQ_16( st_fx->Last_GSC_noisy_speech_flag, 1 ) )
    1355             :         {
    1356       31096 :             FOR( i = 0; i < st_fx->L_frame; i++ )
    1357             :             {
    1358       30976 :                 L_tmp = L_shr( L_mult( Random( &hGSCDec->seed_tcx ), 26214 ), 5 ); /*Q10*/
    1359       30976 :                 L_tmp = L_mac( L_tmp, hGSCDec->Last_GSC_spectrum_fx[i], 6554 );    /*Q10*/
    1360       30976 :                 hGSCDec->Last_GSC_spectrum_fx[i] = round_fx( L_tmp );              /*Q10*/
    1361       30976 :                 move16();
    1362             :             }
    1363             :         }
    1364             : 
    1365         202 :         Copy( hGSCDec->Last_GSC_spectrum_fx, exc_diffQ, st_fx->L_frame ); /* Q10 */
    1366             : 
    1367       52170 :         FOR( i = 0; i < st_fx->L_frame; i++ )
    1368             :         {
    1369       51968 :             hGSCDec->Last_GSC_spectrum_fx[i] = mult_r( hGSCDec->Last_GSC_spectrum_fx[i], 24576 ); /*Q10*/
    1370       51968 :             move16();
    1371             :         }
    1372             :     }
    1373             :     ELSE
    1374             :     {
    1375             :         /*--------------------------------------------------------------------------------------*
    1376             :          * PVQ decoder
    1377             :          *--------------------------------------------------------------------------------------*/
    1378             : 
    1379       20560 :         bands_and_bit_alloc_ivas_fx( hGSCDec->cor_strong_limit, hGSCDec->noise_lev, st_fx->core_brate, Diff_len, bit, &bit, Ener_per_bd_iQ,
    1380       20560 :                                      max_ener_band, bits_per_bands, &nb_subbands, NULL, NULL, &pvq_len, coder_type, st_fx->bwidth, st_fx->GSC_noisy_speech,
    1381       20560 :                                      st_fx->L_frame, st_fx->element_mode, st_fx->GSC_IVAS_mode );
    1382             : 
    1383       20560 :         IF( bit == 0 )
    1384             :         {
    1385           0 :             set16_fx( concat_out, 0, L_FRAME16k );
    1386             :         }
    1387             : 
    1388       20560 :         pvq_core_dec_fx( st_fx, gsc_sfm_start, gsc_sfm_end, gsc_sfm_size, concat_out, &Q_tmp, bit, nb_subbands, bits_per_bands, NULL, inpulses_fx, imaxpulse_fx, ACELP_CORE );
    1389       20560 :         IF( nb_subbands > 0 )
    1390             :         {
    1391       20560 :             Scale_sig( concat_out, gsc_sfm_end[nb_subbands - 1], sub( Q_PVQ_OUT, Q_tmp ) ); /* Q_PVQ_OUT */
    1392             :         }
    1393             : 
    1394       20560 :         seed_init = 0;
    1395       20560 :         move16();
    1396             : 
    1397       20560 :         max_eq = 0;
    1398       20560 :         move16();
    1399       20560 :         max_eq_val = 32767; /* 1.0f in Q15 */
    1400       20560 :         move16();
    1401             : 
    1402       20560 :         test();
    1403       20560 :         test();
    1404       20560 :         test();
    1405       20560 :         IF( ( ( LT_32( st_fx->core_brate, ACELP_7k20 ) && EQ_16( st_fx->GSC_noisy_speech, 1 ) ) || LT_32( st_fx->core_brate, 6000 ) ) && LE_16( coder_type, UNVOICED ) )
    1406             :         {
    1407        6701 :             j = maximum_fx( concat_out, imult1616( nb_subbands, 16 ), &max_eq );
    1408        6701 :             Word16 temp_max_eq = add( abs_s( concat_out[j] ), 10 ) /*0.01f in Q10*/;
    1409        6701 :             if ( LE_16( temp_max_eq, ONE_IN_Q10 ) )
    1410             :             {
    1411        6322 :                 max_eq = max_eq_val; /* Q15 */
    1412        6322 :                 move16();
    1413             :             }
    1414             :             ELSE
    1415             :             {
    1416         379 :                 Word16 exp = 5;
    1417         379 :                 move16();
    1418         379 :                 max_eq = Inv16( temp_max_eq, &exp );
    1419         379 :                 max_eq = shl( max_eq, exp ); /* Q15 */
    1420             :             }
    1421             :         }
    1422             : 
    1423             :         /* Reorder Q bands */
    1424      167312 :         FOR( j = 0; j < nb_subbands; j++ )
    1425             :         {
    1426      146752 :             Copy( concat_out + imult1616( j, 16 ), exc_diffQ + imult1616( max_ener_band[j], 16 ), 16 ); /* Q_PVQ_OUT */
    1427             : 
    1428      146752 :             *last_bin = s_max( *last_bin, max_ener_band[j] ); /* Q0 */
    1429      146752 :             move16();
    1430             : 
    1431      146752 :             bitallocation_band[max_ener_band[j]] = 1; /* Q0 */
    1432      146752 :             move16();
    1433             : 
    1434      146752 :             seed_init = add( seed_init, inpulses_fx[j] ); /* Q0 */
    1435             :         }
    1436       20560 :         test();
    1437       20560 :         IF( NE_16( st_fx->last_coder_type, AUDIO )         /* First audio frame */
    1438             :             && NE_16( st_fx->last_coder_type, UNVOICED ) ) /* last_coder_type == INACTIVE is overwritten in update_dec to UNVOICED */
    1439             :         {
    1440      376067 :             FOR( j = 0; j < shl( nb_subbands, 4 ); j++ )
    1441             :             {
    1442      373264 :                 IF( concat_out[j] > 0 )
    1443             :                 {
    1444       57426 :                     seed_init = extract_l( L_shl( seed_init, 3 ) ); /* Q0 */
    1445             :                 }
    1446      373264 :                 if ( concat_out[j] < 0 )
    1447             :                 {
    1448       57183 :                     seed_init = add_sat( seed_init, 3 ); /* Q0 */
    1449             :                 }
    1450             :             }
    1451             : 
    1452        2803 :             hGSCDec->seed_tcx = seed_init; /* Q0 */
    1453        2803 :             move16();
    1454             :         }
    1455       20560 :         test();
    1456       20560 :         IF( EQ_32( st_fx->core_brate, ACELP_8k00 ) && st_fx->bwidth != NB )
    1457             :         {
    1458          20 :             if ( exc_diffQ[L_FRAME8k - 2] != 0 )
    1459             :             {
    1460           0 :                 bitallocation_exc[0] = 1; /* Q0 */
    1461           0 :                 move16();
    1462             :             }
    1463             : 
    1464          20 :             if ( exc_diffQ[L_FRAME8k - 1] != 0 )
    1465             :             {
    1466           2 :                 bitallocation_exc[1] = 1; /* Q0 */
    1467           2 :                 move16();
    1468             :             }
    1469             :         }
    1470             : 
    1471       20560 :         Copy( exc_diffQ, hGSCDec->Last_GSC_spectrum_fx, st_fx->L_frame ); /* Q_PVQ_OUT */
    1472             : 
    1473             :         /*--------------------------------------------------------------------------------------*
    1474             :          * Skip adaptive (pitch) contribution frequency band (no noise added over the time contribution)
    1475             :          * Find x pulses between 1.6-3.2kHz to code in the spectrum of the residual signal
    1476             :          * Gain is based on the inter-correlation gain between the pulses found and residual signal
    1477             :          *--------------------------------------------------------------------------------------*/
    1478             : 
    1479       20560 :         test();
    1480       20560 :         test();
    1481       20560 :         test();
    1482       20560 :         test();
    1483       20560 :         IF( GE_16( st_fx->GSC_IVAS_mode, 1 ) && EQ_16( st_fx->GSC_noisy_speech, 1 ) )
    1484             :         {
    1485       19107 :             FOR( i = 64; i < st_fx->L_frame; i++ )
    1486             :             {
    1487       19008 :                 exc_diffQ[i] = mult_r( exc_diffQ[i], max_eq ); /* Q_PVQ_OUT */
    1488       19008 :                 move16();
    1489             :             }
    1490             :         }
    1491       20461 :         ELSE IF( ( ( LT_32( st_fx->core_brate, ACELP_7k20 ) && EQ_16( st_fx->GSC_noisy_speech, 1 ) ) || LT_32( st_fx->core_brate, 6000 ) ) && LE_16( st_fx->coder_type, UNVOICED ) )
    1492             :         {
    1493     1722157 :             FOR( i = 0; i < L_FRAME; i++ )
    1494             :             {
    1495     1715456 :                 exc_diffQ[i] = mult_r( exc_diffQ[i], max_eq ); /* Q_PVQ_OUT */
    1496     1715456 :                 move16();
    1497             :             }
    1498             :         }
    1499             :         ELSE
    1500             :         {
    1501       13760 :             freq_dnw_scaling_fx( hGSCDec->cor_strong_limit, st_fx->coder_type, hGSCDec->noise_lev, st_fx->core_brate, exc_diffQ, Qexc_diffQ, st_fx->L_frame );
    1502             :         }
    1503             :     }
    1504             : 
    1505             :     /*--------------------------------------------------------------------------------------*
    1506             :      * Estimate noise level
    1507             :      *--------------------------------------------------------------------------------------*/
    1508             : 
    1509       20762 :     highband_exc_dct_in_ivas_fx( st_fx->core_brate, mfreq_bindiv_loc, *last_bin, Diff_len, hGSCDec->noise_lev, pit_band_idx, exc_diffQ,
    1510       20762 :                                  &hGSCDec->seed_tcx, Ener_per_bd_iQ, nb_subfr, exc_dct_in, st_fx->last_coder_type, bitallocation_band, lsf_new,
    1511       20762 :                                  hGSCDec->last_exc_dct_in_fx, &hGSCDec->last_ener_fx, hGSCDec->last_bitallocation_band, bitallocation_exc, st_fx->bfi, coder_type,
    1512       20762 :                                  st_fx->bwidth, exc_wo_nf, Qexc_diffQ, Q_exc, st_fx->GSC_noisy_speech, hGSCDec->lt_ener_per_band_fx, st_fx->L_frame, st_fx->element_mode, st_fx->GSC_IVAS_mode );
    1513             : 
    1514       20762 :     exc_dct_in[0] = 0;
    1515       20762 :     move16();
    1516             : 
    1517       20762 :     return;
    1518             : }
    1519             : 
    1520             : /*-------------------------------------------------------------------*
    1521             :  * GSC_dec_init()
    1522             :  *
    1523             :  * Initialize GSC decoder state structure
    1524             :  *-------------------------------------------------------------------*/
    1525             : 
    1526           3 : void GSC_dec_init(
    1527             :     GSC_DEC_HANDLE hGSCDec /* i/o: GSC data handle  */
    1528             : )
    1529             : {
    1530           3 :     hGSCDec->seed_tcx = 15687; /* Q0 */
    1531           3 :     hGSCDec->cor_strong_limit = 1;
    1532           3 :     move16();
    1533           3 :     move16();
    1534             : 
    1535           3 :     hGSCDec->noise_lev = NOISE_LEVEL_SP0; /* Q0 */
    1536           3 :     hGSCDec->Last_GSC_pit_band_idx = 0;
    1537           3 :     move16();
    1538           3 :     move16();
    1539             : 
    1540           3 :     set16_fx( hGSCDec->Last_GSC_spectrum_fx, 0, L_FRAME );
    1541           3 :     set16_fx( hGSCDec->last_exc_dct_in_fx, 0, L_FRAME );
    1542           3 :     set16_fx( hGSCDec->old_y_gain_fx, 0, MBANDS_GN );
    1543           3 :     set16_fx( hGSCDec->lt_ener_per_band_fx, 4096, MBANDS_GN ); /*Q12*/
    1544             : 
    1545           3 :     hGSCDec->last_ener_fx = 0;
    1546           3 :     move16();
    1547           3 :     set16_fx( hGSCDec->last_bitallocation_band, 0, 6 );
    1548             : 
    1549           3 :     hGSCDec->Last_frame_ener_fx = MAX_32;
    1550           3 :     move32();
    1551           3 :     return;
    1552             : }
    1553             : 
    1554        3253 : void GSC_dec_init_ivas_fx(
    1555             :     GSC_DEC_HANDLE hGSCDec /* i/o: GSC data handle  */
    1556             : )
    1557             : {
    1558        3253 :     hGSCDec->seed_tcx = 15687; /* Q0 */
    1559        3253 :     move16();
    1560        3253 :     hGSCDec->cor_strong_limit = 1;
    1561        3253 :     move16();
    1562        3253 :     set16_fx( hGSCDec->old_y_gain_fx, 0, MBANDS_GN );
    1563        3253 :     hGSCDec->noise_lev = NOISE_LEVEL_SP0;
    1564        3253 :     move16();
    1565        3253 :     set16_fx( hGSCDec->Last_GSC_spectrum_fx, 0, L_FRAME16k );
    1566        3253 :     hGSCDec->Last_GSC_pit_band_idx = 0;
    1567        3253 :     move16();
    1568             : 
    1569        3253 :     set16_fx( hGSCDec->lt_ener_per_band_fx, 4096, MBANDS_GN16k ); /*Q12*/
    1570        3253 :     set16_fx( hGSCDec->last_exc_dct_in_fx, 0, L_FRAME16k );
    1571        3253 :     hGSCDec->last_ener_fx = 0;
    1572        3253 :     move16();
    1573        3253 :     set16_fx( hGSCDec->last_bitallocation_band, 0, 6 );
    1574             : 
    1575        3253 :     hGSCDec->Last_frame_ener_fx = MAX_32;
    1576        3253 :     move32();
    1577             : 
    1578        3253 :     return;
    1579             : }

Generated by: LCOV version 1.14