LCOV - code coverage report
Current view: top level - lib_enc - evs_enc_fx.c (source / functions) Hit Total Coverage
Test: Coverage on main @ f4104b811e982d2a75d42ab9f366302fa8564693 Lines: 215 293 73.4 %
Date: 2025-11-13 02:39:34 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 <assert.h>
       7             : #include "options.h" /* Compilation switches                   */
       8             : #include "prot_fx.h"
       9             : #include "cnst.h"       /* Common constants                       */
      10             : #include "rom_com.h"    /* Common constants                       */
      11             : #include "basop_util.h" /* Function prototypes                    */
      12             : #include "prot_fx_enc.h"
      13             : #ifdef DEBUGGING
      14             : #include "debug.h"
      15             : #endif
      16             : 
      17             : /*-------------------------------------------------------------------*
      18             :  *  Local function prototypes
      19             :  *-------------------------------------------------------------------*/
      20             : 
      21             : static void initFrameHeader_loc( Encoder_State *st );
      22             : static void writeFrameHeader_loc( Encoder_State *st );
      23             : static void configure_core_coder_loc( Encoder_State *st );
      24             : 
      25             : /*-------------------------------------------------------------------*
      26             :  * evs_enc()
      27             :  *
      28             :  * Principal encoder routine
      29             :  *-------------------------------------------------------------------*/
      30             : 
      31        5150 : ivas_error evs_enc_fx(
      32             :     Encoder_State *st,      /* i/o: encoder state structure     */
      33             :     const Word16 *data,     /* i  : input signal                Q0*/
      34             :     Word32 *mem_hp20_in_fx, /* i/o: hp20 filter memory          Qx*/
      35             :     const Word16 n_samples  /* i  : number of input samples     Q0*/
      36             : )
      37             : {
      38             :     Word16 i, input_frame, delay;
      39        5150 :     Word16 old_inp_12k8[L_INP_12k8], *inp = 0; /* buffer of input signal @ 12k8            */
      40             :     Word16 old_inp_16k[L_INP];                 /* buffer of input signal @ 16kHz           */
      41             :     Word32 fr_bands[2 * NB_BANDS];             /* energy in frequency bands                */
      42             :     Word16 Etot;                               /* total energy; correlation shift          */
      43             :     Word32 ener;                               /* residual energy from Levinson-Durbin     */
      44             :     Word16 A[NB_SUBFR16k * ( M + 1 )];         /* A(z) unquantized for subframes           */
      45             :     Word16 Aw[NB_SUBFR16k * ( M + 1 )];        /* weighted A(z) unquantized for subframes  */
      46             :     Word16 epsP_h[M + 1];                      /* LP prediction errors                     */
      47             :     Word16 epsP_l[M + 1];                      /* LP prediction errors                     */
      48             :     Word32 epsP[M + 1];                        /* LP prediction errors                     */
      49             :     Word16 lsp_new[M];                         /* LSPs at the end of the frame             */
      50             :     Word16 lsp_mid[M];                         /* ISPs in the middle of the frame          */
      51             :     Word16 vad_hover_flag;
      52             :     Word16 hq_core_type; /* HQ core type (HQ, or LR-MDCT)            */
      53             : 
      54             :     Word16 attack_flag;                   /* attack flag (GSC or TC) */
      55             :     Word16 new_inp_resamp16k[L_FRAME16k]; /* new input signal @16kHz, non pre-emphasised, used by the WB TBE/BWE */
      56             :     Word16 old_syn_12k8_16k[L_FRAME16k];  /* ACELP core synthesis at 12.8kHz or 16kHz to be used by the SWB BWE */
      57             :     Word16 shb_speech[L_FRAME16k];
      58             :     Word16 hb_speech[L_FRAME16k / 4];
      59             :     Word16 new_swb_speech[L_FRAME48k];
      60             :     Word32 bwe_exc_extended[L_FRAME32k + NL_BUFF_OFFSET]; /* non-linear bandwidth extension buffer */
      61             :     Word16 voice_factors[NB_SUBFR16k];
      62             :     Word16 fb_exc[L_FRAME16k];
      63             :     Word16 Voicing_flag;
      64        5150 :     Word16 pitch_buf[NB_SUBFR16k] = { 0 };
      65             :     Word16 unbits;
      66             : 
      67             :     Word16 padBits;
      68             :     Word32 realBuffer[CLDFB_NO_COL_MAX][CLDFB_NO_CHANNELS_MAX]; /* real buffer */
      69             :     Word32 imagBuffer[CLDFB_NO_COL_MAX][CLDFB_NO_CHANNELS_MAX]; /* imag buffer */
      70             :     CLDFB_SCALE_FACTOR cldfbScale;
      71             :     Word16 Q_new, shift, Q_synth;
      72             :     Word16 Q_r[2];
      73             :     Word16 Q_shb_spch, Q_fb_exc;
      74             :     Word32 L_tmp;
      75             :     UWord16 lsb;
      76             :     Word16 tmp;
      77             : #ifndef FIX_I4_OL_PITCH
      78             :     Word16 pitch_orig[3]; /* original open-loop pitch values that might be altered in core_acelp_tcx20_switching_fx() within MODE2 */
      79             : #endif
      80             :     ivas_error error;
      81        5150 :     SC_VBR_ENC_HANDLE hSC_VBR = st->hSC_VBR;
      82             :     // NOISE_EST_HANDLE hNoiseEst = st->hNoiseEst;
      83             :     // VAD_HANDLE hVAD = st->hVAD;
      84             :     // DTX_ENC_HANDLE  hDtxEnc = st->hDtxEnc;
      85        5150 :     TD_CNG_ENC_HANDLE hTdCngEnc = st->hTdCngEnc;
      86        5150 :     LPD_state_HANDLE hLPDmem = st->hLPDmem;
      87        5150 :     TD_BWE_ENC_HANDLE hBWE_TD = st->hBWE_TD;
      88        5150 :     BSTR_ENC_HANDLE hBstr = st->hBstr;
      89        5150 :     RF_ENC_HANDLE hRF = st->hRF;
      90             : 
      91        5150 :     error = IVAS_ERR_OK;
      92             : 
      93        5150 :     push_wmops( "evs_enc_fx" );
      94             : 
      95        5150 :     Q_shb_spch = 0;
      96        5150 :     move16(); /* to avoid compiler warnings */
      97             : 
      98             : 
      99             :     /*------------------------------------------------------------------*
     100             :      * Initializiation
     101             :      *-----------------------------------------------------------------*/
     102             : 
     103        5150 :     input_frame = extract_l( Mpy_32_32_r( st->input_Fs, ONE_BY_FRAMES_PER_SEC_Q31 ) );
     104        5150 :     move16();
     105        5150 :     st->core = -1;
     106        5150 :     move16();
     107        5150 :     st->extl = -1;
     108        5150 :     move16();
     109        5150 :     st->core_brate = -1;
     110        5150 :     move32();
     111        5150 :     st->input_bwidth = st->last_input_bwidth;
     112        5150 :     move16();
     113        5150 :     st->bwidth = st->last_bwidth;
     114        5150 :     move16();
     115        5150 :     hq_core_type = -1;
     116        5150 :     move16();
     117        5150 :     unbits = 0;
     118        5150 :     move16();
     119             : 
     120        5150 :     st->bits_frame_core = 0;
     121        5150 :     move16(); /* For getting bit consumption in core coder */
     122        5150 :     hTdCngEnc->lp_cng_mode2 = 0;
     123        5150 :     move16();
     124        5150 :     st->mdct_sw_enable = 0;
     125        5150 :     move16();
     126        5150 :     st->mdct_sw = 0;
     127        5150 :     move16();
     128        5150 :     shift = st->old_wsp_shift;
     129        5150 :     move16();
     130        5150 :     st->rate_switching_reset = 0;
     131        5150 :     move16();
     132             : 
     133        5150 :     st->idchan = 0;
     134        5150 :     move16();
     135        5150 :     st->flag_ACELP16k = set_ACELP_flag( EVS_MONO, -1, st->total_brate, 0, 0, -1, -1 );
     136        5150 :     move16();
     137             : 
     138             : 
     139             :     /*----------------------------------------------------------------*
     140             :      * set input samples buffer
     141             :      *----------------------------------------------------------------*/
     142             : 
     143             :     /* get delay to synchronize ACELP and MDCT frame */
     144        5150 :     delay = NS2SA_FX2( st->input_Fs, DELAY_FIR_RESAMPL_NS );
     145             : 
     146        5150 :     Copy( st->input_fx - delay, st->old_input_signal_fx, input_frame + delay ); /* st->q_inp */
     147             : 
     148             :     /*----------------------------------------------------------------*
     149             :      * convert 'short' input data to 'float'
     150             :      *----------------------------------------------------------------*/
     151             : 
     152        5150 :     Copy( data, st->input_fx, input_frame ); /* st-> q_inp */
     153        5150 :     IF( LT_16( n_samples, input_frame ) )
     154             :     {
     155           0 :         set16_fx( st->input_fx + n_samples, 0, sub( input_frame, n_samples ) );
     156             :     }
     157             : 
     158             :     /*----------------------------------------------------------------*
     159             :      * HP filtering
     160             :      *----------------------------------------------------------------*/
     161             : 
     162        5150 :     hp20( st->input_fx, 1, input_frame, mem_hp20_in_fx, st->input_Fs );
     163             : 
     164             :     /*----------------------------------------------------------------*
     165             :      * Updates in case of AMR-WB IO mode -> EVS primary mode switching
     166             :      *----------------------------------------------------------------*/
     167             : 
     168        5150 :     IF( EQ_16( st->last_core, AMR_WB_CORE ) )
     169             :     {
     170           0 :         updt_IO_switch_enc_fx( st, input_frame );
     171           0 :         set16_fx( hBWE_TD->old_speech_shb_fx, 0, L_LOOK_16k + L_SUBFR16k );
     172           0 :         cldfb_reset_memory( st->cldfbAnaEnc );
     173           0 :         cldfb_reset_memory( st->cldfbSynTd );
     174             :     }
     175             : 
     176             :     /*---------------------------------------------------------------------*
     177             :      * Pre-processing
     178             :      *---------------------------------------------------------------------*/
     179             : #ifdef FIX_I4_OL_PITCH
     180             :     pre_proc( st, input_frame, old_inp_12k8, old_inp_16k, &inp, fr_bands, &Etot, &ener, A, Aw, epsP, lsp_new, lsp_mid, &vad_hover_flag, &attack_flag, new_inp_resamp16k, &Voicing_flag, realBuffer, imagBuffer, &hq_core_type );
     181             : #else
     182        5150 :     pre_proc_fx( st, input_frame, old_inp_12k8, old_inp_16k, &inp, fr_bands, &Etot, &ener,
     183             :                  pitch_orig, A, Aw, epsP_h, epsP_l, epsP, lsp_new, lsp_mid, &vad_hover_flag,
     184        5150 :                  &attack_flag, new_inp_resamp16k, &Voicing_flag, realBuffer, imagBuffer, &cldfbScale, hLPDmem->old_exc,
     185             :                  &hq_core_type, &Q_new, &shift, Q_r );
     186             : #endif
     187             :     // st->sharpFlag = sharpFlag;
     188             : 
     189        5150 :     IF( EQ_16( st->mdct_sw, MODE2 ) )
     190             :     {
     191             : 
     192         132 :         Mpy_32_16_ss( st->total_brate, 5243, &L_tmp, &lsb );     /* 5243 is 1/50 in Q18. (0+18-15=3) */
     193         132 :         st->bits_frame_nominal = extract_l( L_shr( L_tmp, 3 ) ); /* Q0 */
     194             : 
     195         132 :         initFrameHeader_loc( st );
     196         132 :         writeFrameHeader_loc( st );
     197             : 
     198         132 :         test();
     199         132 :         test();
     200         132 :         test();
     201         132 :         IF( ( GT_32( st->total_brate, ACELP_24k40 ) && LT_32( st->total_brate, HQ_96k ) ) || ( EQ_32( st->total_brate, ACELP_24k40 ) && GE_16( st->bwidth, WB ) ) )
     202             :         {
     203         132 :             st->L_frame = L_FRAME16k;
     204         132 :             move16();
     205         132 :             st->gamma = GAMMA16k;
     206         132 :             move16();
     207         132 :             st->preemph_fac = PREEMPH_FAC_16k;
     208         132 :             move16();
     209             : 
     210         132 :             weight_a_subfr_fx( NB_SUBFR16k, A, Aw, GAMMA16k, M );
     211         132 :             test();
     212         132 :             IF( EQ_16( st->last_L_frame, L_FRAME ) && st->ini_frame != 0 )
     213             :             {
     214             :                 /* this is just an approximation, but it is sufficient */
     215           0 :                 Copy( st->lsp_old1_fx, st->lspold_enc_fx, M ); /* Q15 */
     216             :             }
     217             :         }
     218             :         ELSE
     219             :         {
     220           0 :             st->L_frame = L_FRAME;
     221           0 :             move16();
     222           0 :             st->gamma = GAMMA1;
     223           0 :             move16();
     224           0 :             st->preemph_fac = PREEMPH_FAC;
     225           0 :             move16();
     226             :         }
     227             : 
     228         132 :         st->sr_core = L_mult0( FRAMES_PER_SEC, st->L_frame ); /* Q0 */
     229         132 :         st->core_brate = st->total_brate;
     230         132 :         move32();
     231             : 
     232         132 :         st->igf = 0;
     233         132 :         move16();
     234         132 :         hq_core_type = NORMAL_HQ_CORE;
     235         132 :         move16();
     236         132 :         test();
     237         132 :         test();
     238         132 :         IF( ( EQ_16( st->bwidth, SWB ) || EQ_16( st->bwidth, WB ) ) && LE_32( st->total_brate, LRMDCT_CROSSOVER_POINT ) )
     239             :         {
     240             :             /* note that FB (bitrate >= 24400bps) is always coded with NORMAL_HQ_CORE */
     241           0 :             hq_core_type = LOW_RATE_HQ_CORE;
     242           0 :             move16();
     243             :         }
     244         132 :         ELSE IF( EQ_16( st->bwidth, NB ) )
     245             :         {
     246           0 :             hq_core_type = LOW_RATE_HQ_CORE;
     247           0 :             move16();
     248             :         }
     249             :     }
     250             : 
     251        5150 :     IF( EQ_16( st->codec_mode, MODE1 ) )
     252             :     {
     253             :         /*---------------------------------------------------------------------*
     254             :          * Write signalling info into the bitstream
     255             :          *---------------------------------------------------------------------*/
     256             : 
     257        2615 :         signalling_enc_fx( st );
     258             : 
     259             :         /*---------------------------------------------------------------------*
     260             :          * Preprocessing (preparing) for ACELP/HQ core switching
     261             :          *---------------------------------------------------------------------*/
     262             : 
     263        2615 :         core_switching_pre_enc_fx( st, old_inp_12k8, old_inp_16k, 0, 0 );
     264             : 
     265             :         /*---------------------------------------------------------------------*
     266             :          * ACELP core encoding
     267             :          *---------------------------------------------------------------------*/
     268             : 
     269        2615 :         IF( EQ_16( st->core, ACELP_CORE ) )
     270             :         {
     271        2097 :             acelp_core_enc_fx( st, inp, ener, A, Aw, epsP_h, epsP_l, lsp_new, lsp_mid, vad_hover_flag,
     272             :                                attack_flag, bwe_exc_extended, voice_factors, old_syn_12k8_16k, pitch_buf, &unbits, NULL, Q_new, shift );
     273             :         }
     274             :         /*---------------------------------------------------------------------*
     275             :          * HQ core encoding
     276             :          *---------------------------------------------------------------------*/
     277             : 
     278        2615 :         IF( EQ_16( st->core, HQ_CORE ) )
     279             :         {
     280         518 :             hq_core_enc_fx( st, st->input_fx - delay, input_frame, hq_core_type, Voicing_flag, vad_hover_flag );
     281             :         }
     282             : 
     283             :         /*---------------------------------------------------------------------*
     284             :          * Postprocessing for ACELP/HQ core switching
     285             :          *---------------------------------------------------------------------*/
     286        2615 :         core_switching_post_enc_fx( st, old_inp_12k8, old_inp_16k, A, shift, Q_new, st->Q_syn2, &Q_synth );
     287             : #ifndef FIX_I4_OL_PITCH
     288        2615 :         IF( EQ_16( st->core, HQ_CORE ) )
     289             :         {
     290         518 :             Copy( pitch_orig, st->pitch, 3 ); /* original open-loop pitch values might be altered in core_acelp_tcx20_switching_fx() */
     291             :         }
     292             : #endif
     293             :     }
     294             :     ELSE /* MODE2 */
     295             :     {
     296             :         /*----------------------------------------------------------------*
     297             :          * Configuration of core coder/SID
     298             :          * Write Frame Header
     299             :          *----------------------------------------------------------------*/
     300             : 
     301        2535 :         configure_core_coder_loc( st );
     302             : 
     303        2535 :         IF( st->mdct_sw != MODE1 )
     304             :         {
     305        1968 :             writeFrameHeader_loc( st );
     306             :         }
     307             : 
     308             :         /*----------------------------------------------------------------*
     309             :          * Core-Coder
     310             :          *----------------------------------------------------------------*/
     311             : 
     312             :         /* Call main encoding function */
     313        2535 :         enc_acelp_tcx_main_fx( old_inp_16k + L_INP_MEM, st, Aw, lsp_new, lsp_mid,
     314             :                                bwe_exc_extended, voice_factors, pitch_buf,
     315             :                                vad_hover_flag, &Q_new, &shift );
     316             : #ifndef FIX_I4_OL_PITCH
     317        2535 :         Copy( pitch_orig, st->pitch, 3 ); /* populate the original OL pitch values back        Q0*/
     318             : #endif
     319             : 
     320             :         /*---------------------------------------------------------------------*
     321             :          * Postprocessing for codec switching
     322             :          *---------------------------------------------------------------------*/
     323             :         /* TBE interface */
     324        2535 :         test();
     325        2535 :         IF( st->igf != 0 && GT_32( st->core_brate, SID_2k40 ) )
     326             :         {
     327        2535 :             IF( EQ_16( st->core, ACELP_CORE ) )
     328             :             {
     329        1224 :                 SWITCH( st->bwidth )
     330             :                 {
     331           0 :                     case WB:
     332           0 :                         st->extl = WB_TBE;
     333           0 :                         move16();
     334           0 :                         st->extl_brate = WB_TBE_0k35;
     335           0 :                         move32();
     336           0 :                         BREAK;
     337             : 
     338        1224 :                     case SWB:
     339        1224 :                         st->extl = SWB_TBE;
     340        1224 :                         move16();
     341        1224 :                         st->extl_brate = SWB_TBE_1k6;
     342        1224 :                         move32();
     343        1224 :                         IF( LT_32( st->total_brate, ACELP_13k20 ) )
     344             :                         {
     345           0 :                             st->extl_brate = SWB_TBE_0k95;
     346           0 :                             move32();
     347             :                         }
     348        1224 :                         ELSE IF( GE_32( st->total_brate, ACELP_24k40 ) )
     349             :                         {
     350        1224 :                             st->extl_brate = SWB_TBE_2k8;
     351        1224 :                             move32();
     352             :                         }
     353        1224 :                         BREAK;
     354           0 :                     case FB:
     355           0 :                         st->extl = FB_TBE;
     356           0 :                         move16();
     357           0 :                         st->extl_brate = FB_TBE_1k8;
     358           0 :                         move32();
     359             : 
     360           0 :                         if ( GE_32( st->total_brate, ACELP_24k40 ) )
     361             :                         {
     362           0 :                             st->extl_brate = FB_TBE_3k0;
     363           0 :                             move32();
     364             :                         }
     365           0 :                         BREAK;
     366             :                 }
     367        1224 :             }
     368             :             ELSE
     369             :             {
     370        1311 :                 st->coder_type = -1;
     371        1311 :                 move16();
     372        1311 :                 st->extl = IGF_BWE;
     373        1311 :                 move16();
     374        1311 :                 st->extl_brate = 0;
     375        1311 :                 move32();
     376             :             }
     377             : 
     378        2535 :             st->core_brate = L_sub( st->total_brate, st->extl_brate ); /* Q0 */
     379             : 
     380        2535 :             IF( EQ_16( st->tec_tfa, 1 ) )
     381             :             {
     382        1968 :                 st->core_brate = L_sub( st->core_brate, BITS_TEC ); /* Q0 */
     383        1968 :                 st->core_brate = L_sub( st->core_brate, BITS_TFA ); /* Q0 */
     384             :             }
     385             :         }
     386             : 
     387             :         /*----------------------------------------------------------------*
     388             :          * Complete Bitstream Writing
     389             :          *----------------------------------------------------------------*/
     390             : 
     391        2535 :         padBits = 0;
     392             : 
     393        2535 :         test();
     394        2535 :         test();
     395        2535 :         IF( st->igf != 0 && EQ_16( st->core, ACELP_CORE ) && GT_32( st->core_brate, SID_2k40 ) )
     396             :         {
     397             :             /* padBits = ((st->bits_frame+7)/8)*8 - (st->nb_bits_tot + (st->rf_target_bits_write - ((st->Opt_RF_ON==1)?1:0) ) + get_tbe_bits(st->total_brate, st->bwidth, st->rf_mode )); */
     398        1224 :             tmp = add( get_tbe_bits_fx( st->total_brate, st->bwidth, st->rf_mode ), sub( st->rf_target_bits_write, st->rf_mode ) ); /* Q0 */
     399        1224 :             padBits = sub( sub( shl( shr( add( st->bits_frame, 7 ), 3 ), 3 ), hBstr->nb_bits_tot ), tmp );                          /* Q0 */
     400             :         }
     401             :         ELSE
     402             :         {
     403             :             /* padBits = ((st->bits_frame+7)/8)*8 - (st->nb_bits_tot + (st->rf_target_bits_write - ((st->Opt_RF_ON==1)?1:0) )); */
     404        1311 :             tmp = sub( st->rf_target_bits_write, st->rf_mode );                                            /* Q0 */
     405        1311 :             padBits = sub( shl( shr( add( st->bits_frame, 7 ), 3 ), 3 ), add( hBstr->nb_bits_tot, tmp ) ); /* Q0 */
     406             :         }
     407             : 
     408        3183 :         FOR( i = 0; i < padBits; i++ )
     409             :         {
     410         648 :             push_next_indice( hBstr, 0, 1 );
     411             :         }
     412             :     }
     413             : 
     414             :     /*---------------------------------------------------------------------*
     415             :      * WB TBE encoding
     416             :      * WB BWE encoding
     417             :      *---------------------------------------------------------------------*/
     418             : 
     419        5150 :     test();
     420        5150 :     IF( GE_32( st->input_Fs, 16000 ) && ( LT_16( st->bwidth, SWB ) ) )
     421             :     {
     422             :         /* Common pre-processing for WB TBE and WB BWE */
     423           0 :         wb_pre_proc_fx( st, new_inp_resamp16k, hb_speech );
     424             :         /* o: new_inp_resamp16k at Q = -1 */
     425             :     }
     426             : 
     427        5150 :     IF( EQ_16( st->extl, WB_TBE ) )
     428             :     {
     429             :         /* WB TBE encoder */
     430           0 :         wb_tbe_enc_fx( st, st->coder_type, hb_speech, bwe_exc_extended, Q_new, voice_factors, pitch_buf, st->voicing_fx );
     431             : 
     432           0 :         IF( EQ_16( st->codec_mode, MODE2 ) )
     433             :         {
     434           0 :             tbe_write_bitstream_fx( st );
     435             :         }
     436             :     }
     437        5150 :     ELSE IF( EQ_16( st->extl, WB_BWE ) )
     438             :     {
     439             :         /* WB BWE encoder */
     440           0 :         wb_bwe_enc_fx( st, new_inp_resamp16k, st->coder_type );
     441             :     }
     442             : 
     443             :     /*---------------------------------------------------------------------*
     444             :      * SWB(FB) TBE encoding
     445             :      * SWB BWE encoding
     446             :      *---------------------------------------------------------------------*/
     447        5150 :     test();
     448        5150 :     IF( !st->Opt_SC_VBR && GE_32( st->input_Fs, 32000 ) )
     449             :     {
     450             :         /* Common pre-processing for SWB(FB) TBE and SWB BWE */
     451        5150 :         swb_pre_proc_fx( st, st->input_fx, new_swb_speech, shb_speech, &Q_shb_spch, realBuffer, imagBuffer, &cldfbScale );
     452             :     }
     453           0 :     ELSE IF( GE_32( st->input_Fs, 32000 ) )
     454             :     {
     455           0 :         set16_fx( hBWE_TD->old_speech_shb_fx, 0, L_LOOK_16k + L_SUBFR16k );
     456           0 :         set16_fx( shb_speech, 0, L_FRAME16k );
     457             :     }
     458             : 
     459             :     /* SWB TBE encoder */
     460        5150 :     test();
     461        5150 :     test();
     462        5150 :     test();
     463        5150 :     test();
     464        5150 :     test();
     465        5150 :     test();
     466        5150 :     IF( EQ_16( st->extl, SWB_TBE ) || EQ_16( st->extl, FB_TBE ) || ( st->igf != 0 && EQ_16( st->core, ACELP_CORE ) && NE_16( st->extl, WB_TBE ) ) )
     467             :     {
     468        2639 :         test();
     469        2639 :         IF( NE_32( st->core_brate, FRAME_NO_DATA ) && NE_32( st->core_brate, SID_2k40 ) )
     470             :         {
     471        2639 :             swb_tbe_enc_fx( st, st->coder_type, shb_speech, bwe_exc_extended, voice_factors, fb_exc, &Q_fb_exc, Q_new, Q_shb_spch, st->voicing_fx, pitch_buf );
     472             : 
     473        2639 :             IF( EQ_16( st->extl, FB_TBE ) )
     474             :             {
     475             :                 /* FB TBE encoder */
     476           0 :                 fb_tbe_enc_fx( st, st->input_fx, fb_exc, Q_fb_exc );
     477             :             }
     478             : 
     479        2639 :             IF( EQ_16( st->codec_mode, MODE2 ) )
     480             :             {
     481        1224 :                 IF( EQ_16( st->tec_tfa, 1 ) )
     482             :                 {
     483        1224 :                     tecEnc_TBE_fx( &( st->hTECEnc->corrFlag ), st->voicing_fx, st->coder_type );
     484             : 
     485        1224 :                     IF( EQ_16( st->coder_type, INACTIVE ) )
     486             :                     {
     487           0 :                         st->tec_flag = 0;
     488           0 :                         move16();
     489           0 :                         st->hTECEnc->corrFlag = 0;
     490           0 :                         move16();
     491             :                     }
     492        1224 :                     st->tfa_flag = tfaEnc_TBE_fx( st->tfa_enr, st->last_core, st->voicing_fx, pitch_buf, shl( Q_shb_spch, 1 ) ); /* Q0 */
     493        1224 :                     set_TEC_TFA_code_fx( st->hTECEnc->corrFlag, &st->tec_flag, &st->tfa_flag );
     494             :                 }
     495             :                 ELSE
     496             :                 {
     497           0 :                     st->tec_flag = 0;
     498           0 :                     move16();
     499           0 :                     st->hTECEnc->corrFlag = 0;
     500           0 :                     move16();
     501           0 :                     st->tfa_flag = 0;
     502           0 :                     move16();
     503             :                 }
     504             : 
     505        1224 :                 tbe_write_bitstream_fx( st );
     506             :             }
     507             :         }
     508             :     }
     509        2511 :     ELSE IF( EQ_16( st->extl, SWB_BWE ) || EQ_16( st->extl, FB_BWE ) )
     510             :     {
     511             :         /* SWB BWE encoder */
     512           8 :         swb_bwe_enc_fx( st, old_inp_12k8, old_inp_16k, old_syn_12k8_16k, new_swb_speech, shb_speech, st->coder_type, Q_shb_spch, sub( Q_new, 1 ) );
     513             :     }
     514        2503 :     ELSE IF( EQ_16( st->extl, SWB_BWE_HIGHRATE ) || EQ_16( st->extl, FB_BWE_HIGHRATE ) )
     515             :     {
     516             :         /* SWB HR BWE encoder */
     517         674 :         swb_bwe_enc_hr_fx( st, st->input_fx - delay, st->Q_syn2, input_frame, unbits );
     518             :     }
     519             : 
     520             :     /*---------------------------------------------------------------------*
     521             :      * SWB DTX/CNG encoding
     522             :      *---------------------------------------------------------------------*/
     523             : 
     524        5150 :     test();
     525        5150 :     IF( st->Opt_DTX_ON && GE_16( input_frame, L_FRAME32k ) )
     526             :     {
     527           0 :         swb_CNG_enc_fx( st, shb_speech, old_syn_12k8_16k );
     528             :     }
     529             : 
     530             :     /*---------------------------------------------------------------------*
     531             :      * Channel-aware mode - write signaling information into the bitstream
     532             :      *---------------------------------------------------------------------*/
     533             : 
     534        5150 :     signalling_enc_rf_fx( st );
     535             : 
     536        5150 :     updt_enc_common_fx( st, Etot, Q_new );
     537             : 
     538        5150 :     if ( EQ_16( st->mdct_sw, MODE1 ) )
     539             :     {
     540         567 :         st->codec_mode = MODE1;
     541         567 :         move16();
     542             :     }
     543        5150 :     if ( hTdCngEnc->lp_cng_mode2 )
     544             :     {
     545           0 :         st->codec_mode = MODE2;
     546           0 :         move16();
     547             :     }
     548             : 
     549        5150 :     IF( EQ_16( st->rf_mode, 1 ) )
     550             :     {
     551           0 :         IF( EQ_16( hRF->rf_frame_type, RF_NELP ) )
     552             :         {
     553           0 :             hSC_VBR->last_nelp_mode = 1;
     554             :         }
     555             :         ELSE
     556             :         {
     557           0 :             hSC_VBR->last_nelp_mode = 0;
     558             :         }
     559             :     }
     560             : 
     561             :     /* RF mode updates */
     562        5150 :     st->rf_mode_last = st->rf_mode;
     563        5150 :     IF( EQ_16( st->Opt_RF_ON, 1 ) )
     564             :     {
     565           0 :         st->L_frame = L_FRAME;
     566           0 :         st->rf_mode = 1;
     567             :     }
     568             : 
     569             : #ifdef DEBUG_MODE_INFO
     570             :     dbgwrite( &st->codec_mode, sizeof( short ), 1, input_frame, "res/codec" );
     571             :     dbgwrite( &st->core, sizeof( short ), 1, input_frame, "res/core" );
     572             :     dbgwrite( &st->extl, sizeof( short ), 1, input_frame, "res/extl" );
     573             :     dbgwrite( &st->bwidth, sizeof( short ), 1, input_frame, "res/bwidth" );
     574             :     float ener_flt = st->total_brate / 1000.0f;
     575             :     dbgwrite( &ener_flt, sizeof( float ), 1, input_frame, "res/total_brate" );
     576             :     ener_flt = st->core_brate / 1000.0f;
     577             :     dbgwrite( &ener_flt, sizeof( float ), 1, input_frame, "res/core_brate" );
     578             :     ener_flt = st->extl_brate / 1000.0f;
     579             :     dbgwrite( &ener_flt, sizeof( float ), 1, input_frame, "res/extl_brate" );
     580             :     dbgwrite( &st->coder_type, sizeof( short ), 1, input_frame, "res/coder_type" );
     581             :     dbgwrite( &st->clas, sizeof( short ), 1, input_frame, "res/clas" );
     582             :     dbgwrite( &st->cng_type, sizeof( short ), 1, input_frame, "res/cng_type" );
     583             :     dbgwrite( &st->L_frame, sizeof( short ), 1, input_frame, "res/L_frame" );
     584             :     dbgwrite( &st->vad_flag, sizeof( short ), 1, input_frame, "res/vad_flag" );
     585             : #endif
     586             : 
     587        5150 :     pop_wmops();
     588             : 
     589        5150 :     return error;
     590             : }
     591             : 
     592             : 
     593             : /*-------------------------------------------------------------------*
     594             :  * initFrameHeader()
     595             :  *
     596             :  * Initialize MODE2 frame header
     597             :  *-------------------------------------------------------------------*/
     598        2667 : static void initFrameHeader_loc( Encoder_State *st )
     599             : {
     600             : 
     601             :     Word16 n;
     602             : 
     603        2667 :     IF( EQ_32( st->core_brate, SID_2k40 ) )
     604             :     {
     605             :         /*Get size of frame*/
     606           0 :         st->bits_frame = FRAME_2_4;
     607           0 :         move16();
     608           0 :         st->bits_frame_core = add( st->bits_frame_core, FRAME_2_4 - 4 ); /*1 bit for SID on/off + 3 bits for bandwith in case of SID.*/
     609           0 :         st->frame_size_index = 2;
     610           0 :         move16();
     611             :     }
     612        2667 :     ELSE IF( EQ_32( st->core_brate, FRAME_NO_DATA ) )
     613             :     {
     614           0 :         st->bits_frame = FRAME_0;
     615           0 :         move16();
     616           0 :         st->bits_frame_core = add( st->bits_frame_core, st->bits_frame );
     617           0 :         st->frame_size_index = 0;
     618           0 :         move16();
     619             :     }
     620             :     ELSE
     621             :     {
     622       20202 :         FOR( n = 0; n < FRAME_SIZE_NB; n++ )
     623             :         {
     624       20202 :             IF( EQ_16( FrameSizeConfig[n].frame_bits, st->bits_frame_nominal ) )
     625             :             {
     626        2667 :                 st->frame_size_index = n;
     627        2667 :                 move16();
     628        2667 :                 st->bits_frame = FrameSizeConfig[n].frame_bits;
     629        2667 :                 move16();
     630        2667 :                 st->bits_frame_core = FrameSizeConfig[n].frame_net_bits;
     631        2667 :                 move16();
     632        2667 :                 BREAK;
     633             :             }
     634             :         }
     635             :     }
     636             : 
     637        2667 :     return;
     638             : }
     639             : 
     640             : /*-------------------------------------------------------------------*
     641             :  * writeFrameHeader()
     642             :  *
     643             :  * Write MODE2 frame header
     644             :  *-------------------------------------------------------------------*/
     645        2100 : static void writeFrameHeader_loc( Encoder_State *st )
     646             : {
     647             : 
     648        2100 :     BSTR_ENC_HANDLE hBstr = st->hBstr;
     649             : 
     650        2100 :     IF( NE_32( st->core_brate, FRAME_NO_DATA ) )
     651             :     {
     652             :         /* SID flag at 2.4kbps */
     653        2100 :         IF( EQ_32( st->core_brate, SID_2k40 ) )
     654             :         {
     655           0 :             IF( EQ_16( st->cng_type, FD_CNG ) )
     656             :             {
     657             :                 /* write SID/CNG type flag */
     658           0 :                 push_next_indice( hBstr, 1, 1 );
     659             : 
     660             :                 /* write bandwidth mode */
     661           0 :                 push_next_indice( hBstr, st->bwidth, 2 );
     662             : 
     663             :                 /* write L_frame */
     664           0 :                 IF( EQ_16( st->L_frame, L_FRAME ) )
     665             :                 {
     666           0 :                     push_next_indice( hBstr, 0, 1 );
     667             :                 }
     668             :                 ELSE
     669             :                 {
     670           0 :                     push_next_indice( hBstr, 1, 1 );
     671             :                 }
     672             :             }
     673             :         }
     674             :         ELSE /* active frames */
     675             :         {
     676        2100 :             IF( st->rf_mode == 0 )
     677             :             {
     678        2100 :                 push_next_indice( hBstr, sub( st->bwidth, FrameSizeConfig[st->frame_size_index].bandwidth_min ), FrameSizeConfig[st->frame_size_index].bandwidth_bits );
     679             :             }
     680             :         }
     681             : 
     682             :         /* Write reserved bit */
     683        2100 :         test();
     684        2100 :         IF( FrameSizeConfig[st->frame_size_index].reserved_bits && st->rf_mode == 0 )
     685             :         {
     686        2100 :             push_next_indice( hBstr, 0, FrameSizeConfig[st->frame_size_index].reserved_bits );
     687             :         }
     688             :     }
     689             : 
     690        2100 :     return;
     691             : }
     692             : 
     693             : /*------------------------------------------------------------------------*
     694             :  * Configuration of core coder/SID
     695             :  *------------------------------------------------------------------------*/
     696             : 
     697        2535 : static void configure_core_coder_loc(
     698             :     Encoder_State *st /* i/o: encoder state structure         */
     699             : )
     700             : {
     701             : 
     702        2535 :     initFrameHeader_loc( st );
     703             : 
     704        2535 :     test();
     705        2535 :     IF( NE_32( st->core_brate, SID_2k40 ) && NE_32( st->core_brate, FRAME_NO_DATA ) )
     706             :     {
     707        2535 :         if ( st->tcxonly )
     708             :         {
     709           0 :             st->coder_type = GENERIC;
     710           0 :             move16();
     711             :         }
     712             : 
     713        2535 :         st->hTcxCfg->coder_type = st->coder_type;
     714        2535 :         move16();
     715             : 
     716             : 
     717        2535 :         test();
     718        2535 :         test();
     719        2535 :         if ( !st->tcxonly && !st->localVAD && EQ_16( st->hTcxCfg->coder_type, GENERIC ) )
     720             :         {
     721          94 :             st->hTcxCfg->coder_type = UNVOICED;
     722          94 :             move16();
     723             :         }
     724             :     }
     725             : 
     726        2535 :     st->igf = getIgfPresent_fx( st->element_mode, st->total_brate, st->bwidth, st->rf_mode );
     727             : 
     728        2535 :     test();
     729        2535 :     if ( NE_32( st->core_brate, SID_2k40 ) && NE_32( st->core_brate, FRAME_NO_DATA ) )
     730             :     {
     731        2535 :         st->core_brate = st->total_brate;
     732        2535 :         move32();
     733             :     }
     734             : 
     735        2535 :     return;
     736             : }

Generated by: LCOV version 1.14