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

          Line data    Source code
       1             : /******************************************************************************************************
       2             : 
       3             :    (C) 2022-2025 IVAS codec Public Collaboration with portions copyright Dolby International AB, Ericsson AB,
       4             :    Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
       5             :    Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
       6             :    Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
       7             :    contributors to this repository. All Rights Reserved.
       8             : 
       9             :    This software is protected by copyright law and by international treaties.
      10             :    The IVAS codec Public Collaboration consisting of Dolby International AB, Ericsson AB,
      11             :    Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
      12             :    Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
      13             :    Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
      14             :    contributors to this repository retain full ownership rights in their respective contributions in
      15             :    the software. This notice grants no license of any kind, including but not limited to patent
      16             :    license, nor is any license granted by implication, estoppel or otherwise.
      17             : 
      18             :    Contributors are required to enter into the IVAS codec Public Collaboration agreement before making
      19             :    contributions.
      20             : 
      21             :    This software is provided "AS IS", without any express or implied warranties. The software is in the
      22             :    development stage. It is intended exclusively for experts who have experience with such software and
      23             :    solely for the purpose of inspection. All implied warranties of non-infringement, merchantability
      24             :    and fitness for a particular purpose are hereby disclaimed and excluded.
      25             : 
      26             :    Any dispute, controversy or claim arising under or in relation to providing this software shall be
      27             :    submitted to and settled by the final, binding jurisdiction of the courts of Munich, Germany in
      28             :    accordance with the laws of the Federal Republic of Germany excluding its conflict of law rules and
      29             :    the United Nations Convention on Contracts on the International Sales of Goods.
      30             : 
      31             : *******************************************************************************************************/
      32             : 
      33             : #include <assert.h>
      34             : #include <stdint.h>
      35             : #include "options.h"
      36             : #include "cnst.h"
      37             : #include "ivas_cnst.h"
      38             : #include "rom_com.h"
      39             : #include "prot_fx.h"
      40             : #include "prot_fx_enc.h"
      41             : #include "ivas_rom_com.h"
      42             : #ifdef DEBUGGING
      43             : #include "debug.h"
      44             : #endif
      45             : #include "wmc_auto.h"
      46             : #include "ivas_prot_fx.h"
      47             : #include "ivas_rom_enc.h"
      48             : #include "prot_fx_enc.h"
      49             : 
      50             : 
      51             : /*--------------------------------------------------------------------------*
      52             :  * Local function prototypes
      53             :  *--------------------------------------------------------------------------*/
      54             : static void stereo_mode_combined_format_enc_fx( const Encoder_Struct *st_ivas, CPE_ENC_HANDLE hCPE );
      55             : 
      56             : /*-------------------------------------------------------------------*
      57             :  * ivas_cpe_enc()
      58             :  *
      59             :  * Channel Pair Element (CPE) encoding routine
      60             :  *-------------------------------------------------------------------*/
      61      410255 : ivas_error ivas_cpe_enc_fx(
      62             :     Encoder_Struct *st_ivas,       /* i/o: IVAS encoder structure                                               */
      63             :     const Word16 cpe_id,           /* i  : CPE # identifier                                                             Q0*/
      64             :     Word32 data_fx_ch0[],          /* i  : input signal for channel 0                                   Q(q_data_fx)*/
      65             :     Word32 data_fx_ch1[],          /* i  : input signal for channel 1                                   Q(q_data_fx)*/
      66             :     Word16 q_data_fx,              /* i  : Q-factor of input signal for both channels   */
      67             :     const Word16 input_frame,      /* i  : input frame length per channel                               Q0*/
      68             :     const Word16 nb_bits_metadata, /* i  : number of metadata bits                                              Q0*/
      69             :     Word16 Q_new_out[] )
      70             : {
      71             :     CPE_ENC_HANDLE hCPE;
      72             :     Encoder_State **sts;
      73             :     Word16 n, n_CoreChannels;
      74             :     Word32 old_inp_12k8_fx[CPE_CHANNELS][L_INP_12k8];   /* buffer of input signal @ 12k8        Q15 */
      75             :     Word16 old_inp_12k8_16fx[CPE_CHANNELS][L_INP_12k8]; /* buffer of input signal @ 12k8    Q_new-1 */
      76             :     Word32 old_inp_16k_fx[CPE_CHANNELS][L_INP];         /* buffer of input signal @ 16kHz       Q15 */
      77             :     Word16 old_inp_16k_16fx[CPE_CHANNELS][L_INP];       /* buffer of input signal @ 16kHz   Q_new-1 */
      78      410255 :     Word16 Q_new[CPE_CHANNELS] = { 0 };
      79             :     Word16 fft_buff_fx[CPE_CHANNELS][2 * L_FFT]; /* FFT buffer                               */
      80             :     Word16 fft_buff_fx_q[CPE_CHANNELS];          /* FFT buffer                               */
      81      410255 :     set16_fx( fft_buff_fx_q, 0, CPE_CHANNELS );
      82      410255 :     Word16 fft_buff_fx_final_q = MAX_16;
      83      410255 :     move16();
      84             :     Word32 ener_fx[CPE_CHANNELS];                                /* residual energy from Levinson-Durbin  Q6 */
      85             :     Word16 relE_fx[CPE_CHANNELS];                                /* frame relative energy                  Q8 */
      86      410255 :     Word16 A_fx[CPE_CHANNELS][NB_SUBFR16k * ( M + 1 )] = { 0 };  /* A(z) unquantized for subframes           Q12 */
      87      410255 :     Word16 Aw_fx[CPE_CHANNELS][NB_SUBFR16k * ( M + 1 )] = { 0 }; /* weighted A(z) unquantized for subframes  Q12 */
      88             :     Word32 epsP_fx[CPE_CHANNELS][M + 1];                         /* LP prediction errors                     */
      89             :     Word16 epsP_fx_q[CPE_CHANNELS];
      90             :     Word16 lsp_new_fx[CPE_CHANNELS][M];                                          /* LSPs at the end of the frame           Q15  */
      91             :     Word16 lsp_mid_fx[CPE_CHANNELS][M];                                          /* ISPs in the middle of the frame        Q15  */
      92             :     Word16 vad_hover_flag[CPE_CHANNELS];                                         /* VAD hangover flag                        */
      93             :     Word16 attack_flag[CPE_CHANNELS];                                            /* attack flag (GSC or TC)                  */
      94             :     Word32 realBuffer_fx[CPE_CHANNELS][CLDFB_NO_COL_MAX][CLDFB_NO_CHANNELS_MAX]; /* real buffer                              */
      95             :     Word32 imagBuffer_fx[CPE_CHANNELS][CLDFB_NO_COL_MAX][CLDFB_NO_CHANNELS_MAX]; /* imag buffer                              */
      96             :     Word16 q_re_im_buf[CPE_CHANNELS];
      97             :     Word16 pitch_fr_fx[CPE_CHANNELS][NB_SUBFR];                /* Q6, fractional pitch values              */
      98             :     Word16 voicing_fr_fx[CPE_CHANNELS][NB_SUBFR];              /* Q15, fractional pitch gains              */
      99             :     Word16 loc_harm[CPE_CHANNELS];                             /* harmonicity flag                         */
     100             :     Word16 cor_map_sum_fx[CPE_CHANNELS];                       /* speech/music clasif. parameter        Q8 */
     101             :     Word16 vad_flag_dtx[CPE_CHANNELS];                         /* HE-SAD flag with additional DTX HO       */
     102             :     Word32 enerBuffer_fx[CPE_CHANNELS][CLDFB_NO_CHANNELS_MAX]; /* energy buffer                            */
     103             :     Word16 enerBuffer_fx_exp[CPE_CHANNELS];                    /* energy buffer                            */
     104             :     Word32 currFlatness_fx[CPE_CHANNELS];                      /* flatness parameter                       Q21 */
     105             :     Word16 tdm_ratio_idx, tdm_ratio_idx_SM;                    /* temp. TD stereo parameters               */
     106             :     Word16 tdm_SM_or_LRTD_Pri;                                 /* temp. TD stereo parameters               */
     107             : 
     108             :     Word16 nb_bits;                                 /* number of DFT stereo side bits           */
     109             :     Word32 fr_bands_fx[CPE_CHANNELS][2 * NB_BANDS]; /* energy in frequency bands  (Q_buffer + QSCALE) */
     110             :     Word16 Etot_LR_fx[CPE_CHANNELS];                /* total energy                             Q8 */
     111             :     Word32 lf_E_fx[CPE_CHANNELS][2 * VOIC_BINS];    /* per bin spectrum energy in lf (Q_buffer + QSCALE - 2) */
     112             :     Word16 localVAD_HE_SAD[CPE_CHANNELS];           /* HE-SAD flag without hangover, LR channels */
     113             :     Word32 band_energies_LR_fx[2 * NB_BANDS];       /* energy in critical bands without minimum noise floor E_MIN (Q_buffer + QSCALE - band_ener_guardbits) */
     114             :     Word16 Q_buffer[2];
     115             :     Word16 orig_input_fx[CPE_CHANNELS][L_FRAME48k];
     116             :     Word16 Q_orig_inp[CPE_CHANNELS];
     117             :     Word16 Etot_last_fx[CPE_CHANNELS];
     118             :     Word32 tmp, input_Fs;
     119             :     Word16 max_bwidth, ivas_format;
     120             :     ENCODER_CONFIG_HANDLE hEncoderConfig;
     121             :     Word32 ivas_total_brate;
     122             :     ivas_error error;
     123             :     Word32 cpe_brate;
     124             :     Word32 element_brate_ref;
     125             :     Word16 last_bits_frame_nominal; /* last_bits_frame_nominal for M or PCh channel */
     126             :     Word16 old_wsp_fx[CPE_CHANNELS][L_WSP];
     127             :     Word16 e_old_wsp[CPE_CHANNELS], q_old_wsp;
     128      410255 :     move16(); // Q_new
     129      410255 :     move16(); // Q_new
     130             :     Word16 q_com;
     131             : 
     132      410255 :     error = IVAS_ERR_OK;
     133      410255 :     move32();
     134      410255 :     push_wmops( "ivas_cpe_enc" );
     135             : 
     136      410255 :     hCPE = st_ivas->hCPE[cpe_id];
     137      410255 :     sts = hCPE->hCoreCoder;
     138      410255 :     hEncoderConfig = st_ivas->hEncoderConfig;
     139      410255 :     max_bwidth = hEncoderConfig->max_bwidth;
     140      410255 :     ivas_format = hEncoderConfig->ivas_format;
     141      410255 :     input_Fs = hEncoderConfig->input_Fs;
     142      410255 :     ivas_total_brate = hEncoderConfig->ivas_total_brate;
     143      410255 :     element_brate_ref = hCPE->element_brate;
     144      410255 :     last_bits_frame_nominal = sts[0]->bits_frame_nominal;
     145      410255 :     move16();
     146      410255 :     move16();
     147      410255 :     move16();
     148      410255 :     move32();
     149      410255 :     move32();
     150      410255 :     move32();
     151             : 
     152             :     /*------------------------------------------------------------------*
     153             :      * Initialization - general
     154             :      *-----------------------------------------------------------------*/
     155             : 
     156      410255 :     set16_fx( q_re_im_buf, 0, CPE_CHANNELS );
     157             : 
     158      410255 :     tdm_SM_or_LRTD_Pri = 0;
     159      410255 :     tdm_ratio_idx = -1;
     160      410255 :     tdm_ratio_idx_SM = -1;
     161             :     // tdm_last_ratio = 0;
     162      410255 :     move16();
     163      410255 :     move16();
     164      410255 :     move16();
     165             : 
     166             : #ifdef DEBUGGING
     167             :     if ( hCPE->hCoreCoder[0]->ini_frame == 0 )
     168             :     {
     169             :         hCPE->stereo_mode_cmdl = hEncoderConfig->stereo_mode_cmdl;
     170             :     }
     171             : #endif
     172             : 
     173      410255 :     set16_fx( pitch_fr_fx[0], 0, NB_SUBFR );
     174      410255 :     set16_fx( pitch_fr_fx[1], 0, NB_SUBFR );
     175      410255 :     set16_fx( voicing_fr_fx[0], 0, NB_SUBFR );
     176      410255 :     set16_fx( voicing_fr_fx[1], 0, NB_SUBFR );
     177             : 
     178     1230765 :     FOR( Word16 i = 0; i < CPE_CHANNELS; i++ )
     179             :     {
     180      820510 :         set16_zero_fx( fft_buff_fx[i], 2 * L_FFT );
     181      820510 :         set16_zero_fx( old_inp_16k_16fx[i], L_INP );
     182      820510 :         set_zero_fx( old_inp_16k_fx[i], L_INP );
     183      820510 :         set16_zero_fx( old_inp_12k8_16fx[i], L_INP_12k8 );
     184      820510 :         set_zero_fx( old_inp_12k8_fx[i], L_INP_12k8 );
     185             :     }
     186             :     /*------------------------------------------------------------------*
     187             :      * CPE initialization - core coder
     188             :      *-----------------------------------------------------------------*/
     189             : 
     190     1230765 :     FOR( n = 0; n < CPE_CHANNELS; n++ )
     191             :     {
     192      820510 :         sts[n]->idchan = n;              /* Q0 */
     193      820510 :         sts[n]->core = -1;               /* Q0 */
     194      820510 :         sts[n]->core_brate = -1;         /* updated in dtx() Q0*/
     195      820510 :         sts[n]->max_bwidth = max_bwidth; /* Q0 */
     196      820510 :         move32();
     197      820510 :         move16();
     198      820510 :         move16();
     199      820510 :         move16();
     200             : 
     201      820510 :         IF( st_ivas->hMCT == NULL ) /*already updated before CPE call*/
     202             :         {
     203      298174 :             sts[n]->input_bwidth = sts[n]->last_input_bwidth; /* updated in BWD           Q0*/
     204      298174 :             sts[n]->bwidth = sts[n]->last_bwidth;             /* updated in BWD           Q0*/
     205      298174 :             move16();
     206      298174 :             move16();
     207             :         }
     208      820510 :         sts[n]->rate_switching_reset = 0;
     209      820510 :         move16();
     210             : 
     211             : #ifdef DEBUGGING
     212             :         sts[n]->force = hEncoderConfig->force;
     213             :         sts[n]->id_element = cpe_id + st_ivas->nSCE;
     214             : #ifdef DEBUG_FORCE_DIR
     215             :         sts[n]->force_dir = hEncoderConfig->force_dir;
     216             : #endif
     217             : #endif
     218             :     }
     219             : 
     220      410255 :     Word16 Q_min = s_min( q_data_fx, add( sts[0]->q_inp32, L_norm_arr( sts[0]->input32_fx - input_frame, input_frame ) ) );
     221      410255 :     scale_sig32( sts[0]->input32_fx - input_frame, input_frame, sub( Q_min, sts[0]->q_inp32 ) );
     222      410255 :     Copy_Scale_sig32( data_fx_ch0, sts[0]->input32_fx, input_frame, sub( Q_min, q_data_fx ) ); // Q(Q_min)
     223      410255 :     sts[0]->q_inp32 = Q_min;
     224      410255 :     move16();
     225             : 
     226      410255 :     Word16 norm = L_norm_arr( sts[0]->input32_fx, input_frame );
     227             : 
     228      410255 :     Copy_Scale_sig32_16( sts[0]->input32_fx, sts[0]->input_fx, input_frame, norm );
     229      410255 :     sts[0]->q_inp = add( sub( sts[0]->q_inp32, Q16 ), norm );
     230      410255 :     move16();
     231      410255 :     IF( data_fx_ch1 != NULL ) /*this may happen for cases with odd number of channels*/
     232             :     {
     233      347616 :         Q_min = s_min( q_data_fx, add( sts[1]->q_inp32, L_norm_arr( sts[1]->input32_fx - input_frame, input_frame ) ) );
     234      347616 :         scale_sig32( sts[1]->input32_fx - input_frame, input_frame, sub( Q_min, sts[1]->q_inp32 ) );
     235      347616 :         Copy_Scale_sig32( data_fx_ch1, sts[1]->input32_fx, input_frame, sub( Q_min, q_data_fx ) ); // Q(Q_min)
     236      347616 :         sts[1]->q_inp32 = Q_min;
     237      347616 :         move16();
     238             : 
     239      347616 :         norm = L_norm_arr( sts[1]->input32_fx, input_frame );
     240             : 
     241      347616 :         Copy_Scale_sig32_16( sts[1]->input32_fx, sts[1]->input_fx, input_frame, norm );
     242      347616 :         sts[1]->q_inp = add( sub( sts[1]->q_inp32, Q16 ), norm );
     243      347616 :         move16();
     244             :     }
     245             : 
     246     1230765 :     FOR( Word16 i = 0; i < CPE_CHANNELS; i++ )
     247             :     {
     248    13948670 :         FOR( Word16 j = 0; j < CLDFB_NO_COL_MAX; j++ )
     249             :         {
     250    13128160 :             set_zero_fx( realBuffer_fx[i][j], CLDFB_NO_CHANNELS_MAX );
     251    13128160 :             set_zero_fx( imagBuffer_fx[i][j], CLDFB_NO_CHANNELS_MAX );
     252             :         }
     253             :     }
     254             : 
     255             :     /*----------------------------------------------------------------*
     256             :      * Stereo technology selection
     257             :      * Front-VAD on input L and R channels
     258             :      *----------------------------------------------------------------*/
     259      410255 :     test();
     260      410255 :     IF( sts[0]->ini_frame > 0 && st_ivas->hMCT == NULL )
     261             :     {
     262      148636 :         hCPE->element_mode = select_stereo_mode_fx( hCPE, ivas_format );
     263             :     }
     264             : #ifdef DEBUG_FORCE_DIR
     265             :     else
     266             :     {
     267             :         if ( hCPE->hCoreCoder[0]->force_dir[0] != '\0' )
     268             :         {
     269             :             dbgread( &hCPE->element_mode, sizeof( int16_t ), 1, fname( hCPE->hCoreCoder[0]->force_dir, "force_element_mode.enf", -1, -1, -1 ) );
     270             :         }
     271             :         else
     272             :         {
     273             :             dbgwrite( &hCPE->element_mode, sizeof( int16_t ), 1, 1, "res/force_element_mode.enf" );
     274             :         }
     275             :     }
     276             : #endif
     277             : 
     278      410255 :     stereo_mode_combined_format_enc_fx( st_ivas, hCPE );
     279             : 
     280      410255 :     Word16 Q_inp = 0;
     281      410255 :     move16();
     282             : 
     283      410255 :     Word16 Q_add = 2;
     284      410255 :     move16();
     285      410255 :     Word16 front_create_flag = 0;
     286      410255 :     move16();
     287      410255 :     set32_fx( band_energies_LR_fx, 0, 2 * NB_BANDS );
     288             : 
     289      410255 :     IF( hCPE->hFrontVad[0] != NULL && NE_16( hCPE->element_mode, IVAS_CPE_MDCT ) )
     290             :     {
     291       31908 :         Word16 scale_inp = Q15;
     292       31908 :         Q_add = 0;
     293       31908 :         move16();
     294       95724 :         FOR( n = 0; n < CPE_CHANNELS; n++ )
     295             :         {
     296       63816 :             scale_inp = s_min( scale_inp, s_min( sts[n]->q_inp, hCPE->hFrontVad[n]->q_mem_decim ) );
     297             :         }
     298       95724 :         FOR( n = 0; n < CPE_CHANNELS; n++ )
     299             :         {
     300       63816 :             Scale_sig( sts[n]->input_fx, input_frame, sub( scale_inp, sts[n]->q_inp ) );
     301       63816 :             Scale_sig( hCPE->hFrontVad[n]->mem_decim_fx, 2 * L_FILT_MAX, sub( scale_inp, hCPE->hFrontVad[n]->q_mem_decim ) );
     302       63816 :             sts[n]->q_inp = scale_inp;
     303       63816 :             move16();
     304       63816 :             hCPE->hFrontVad[n]->q_mem_decim = scale_inp;
     305             :         }
     306       31908 :         Q_inp = scale_inp;
     307       31908 :         move16();
     308             :     }
     309             : 
     310     1230765 :     FOR( n = 0; n < CPE_CHANNELS; n++ )
     311             :     {
     312      820510 :         Q_buffer[n] = 15;
     313      820510 :         move16();
     314             :     }
     315      410255 :     test();
     316      410255 :     IF( hCPE->hFrontVad[0] != NULL && NE_16( hCPE->element_mode, IVAS_CPE_MDCT ) )
     317             :     {
     318       95724 :         FOR( n = 0; n < CPE_CHANNELS; n++ )
     319             :         {
     320       63816 :             Q_buffer[n] = hCPE->hFrontVad[n]->q_buffer_12k8;
     321       63816 :             move16();
     322             :         }
     323             :     }
     324             : 
     325             :     Word16 q_band_energies_LR;
     326             :     Word16 q_fr_bands[2], q_lf_E[2];
     327      410255 :     IF( NE_32( ( error = front_vad_fx( hCPE, NULL, hEncoderConfig, &hCPE->hFrontVad[0], st_ivas->hMCT != NULL, input_frame, vad_flag_dtx,
     328             :                                        fr_bands_fx, q_fr_bands, Etot_LR_fx, lf_E_fx, q_lf_E, localVAD_HE_SAD, vad_hover_flag, band_energies_LR_fx,
     329             :                                        &q_band_energies_LR, NULL, NULL, NULL, Q_inp, Q_buffer, Q_add, &front_create_flag ) ),
     330             :                IVAS_ERR_OK ) )
     331             :     {
     332           0 :         return error;
     333             :     }
     334             : 
     335      410255 :     IF( hCPE->hFrontVad[0] != NULL && NE_16( hCPE->element_mode, IVAS_CPE_MDCT ) )
     336             :     {
     337       95904 :         FOR( n = 0; n < CPE_CHANNELS; n++ )
     338             :         {
     339             :             // Scale_sig( hCPE->hFrontVad[n]->buffer_12k8_fx + 384, 3 * L_FRAME / 2 - 384, sub( Q_buffer[n], hCPE->hFrontVad[n]->q_buffer_12k8 ) );
     340       63936 :             hCPE->hFrontVad[n]->q_buffer_12k8 = Q_buffer[n];
     341       63936 :             move16();
     342             :         }
     343             :     }
     344      410255 :     sts[0]->element_mode = hCPE->element_mode;
     345      410255 :     sts[1]->element_mode = hCPE->element_mode;
     346      410255 :     move16();
     347      410255 :     move16();
     348             : 
     349      410255 :     n_CoreChannels = 2;
     350      410255 :     move16();
     351      410255 :     IF( EQ_16( hCPE->element_mode, IVAS_CPE_DFT ) )
     352             :     {
     353       59643 :         n_CoreChannels = 1; /* in DFT stereo, only M channel is coded */
     354       59643 :         move16();
     355             : 
     356       59643 :         sts[1]->vad_flag = 0;
     357       59643 :         move16();
     358             :     }
     359             : 
     360             :     /*----------------------------------------------------------------*
     361             :      * dynamically allocate data structures depending on the actual stereo mode
     362             :      *----------------------------------------------------------------*/
     363      410255 :     IF( ( error = stereo_memory_enc_fx( hCPE, input_Fs, max_bwidth, ivas_format, st_ivas->nchan_transport ) ) != IVAS_ERR_OK )
     364             :     {
     365           0 :         return error;
     366             :     }
     367             : 
     368             : 
     369             :     /*----------------------------------------------------------------*
     370             :      * Set TD stereo parameters
     371             :      *----------------------------------------------------------------*/
     372             : 
     373      410255 :     IF( ( error = stereo_set_tdm_fx( hCPE, input_frame, sts[1]->q_inp32 ) ) != IVAS_ERR_OK )
     374             :     {
     375           0 :         return error;
     376             :     }
     377             : 
     378             : 
     379             :     /*----------------------------------------------------------------*
     380             :      * Resets/updates in case of stereo switching
     381             :      *----------------------------------------------------------------*/
     382      410255 :     Word16 shift = norm_arr( sts[1]->old_input_signal_fx, input_frame );
     383      410255 :     Scale_sig( sts[1]->old_input_signal_fx, input_frame, shift ); /* sts[1]->q_old_inp + shift  */
     384      410255 :     sts[1]->q_old_inp = add( sts[1]->q_old_inp, shift );
     385      410255 :     move16();
     386      410255 :     shift = norm_arr( sts[1]->input_fx, input_frame );
     387      410255 :     Scale_sig( sts[1]->input_fx, input_frame, shift ); /* sts[1]->q_inp + shift  */
     388      410255 :     sts[1]->q_inp = add( sts[1]->q_inp, shift );
     389      410255 :     move16();
     390             : 
     391      410255 :     shift = norm_arr( sts[0]->old_input_signal_fx, input_frame );
     392      410255 :     Scale_sig( sts[0]->old_input_signal_fx, input_frame, shift ); /* sts[1]->q_old_inp + shift  */
     393      410255 :     sts[0]->q_old_inp = add( sts[0]->q_old_inp, shift );
     394      410255 :     move16();
     395      410255 :     shift = norm_arr( sts[0]->input_fx, input_frame );
     396      410255 :     Scale_sig( sts[0]->input_fx, input_frame, shift ); /* sts[1]->q_inp, shift  */
     397      410255 :     sts[0]->q_inp = add( sts[0]->q_inp, shift );
     398      410255 :     move16();
     399             : 
     400      410255 :     Word16 q_inp = s_min( s_min( sts[0]->q_inp, sts[0]->q_old_inp ), s_min( sts[1]->q_inp, sts[1]->q_old_inp ) );
     401             : 
     402      410255 :     Scale_sig( sts[1]->input_fx, input_frame, sub( q_inp, sts[1]->q_inp ) );                /* q_inp */
     403      410255 :     Scale_sig( sts[1]->old_input_signal_fx, input_frame, sub( q_inp, sts[1]->q_old_inp ) ); /* q_inp */
     404      410255 :     sts[1]->q_inp = q_inp;
     405      410255 :     sts[1]->q_old_inp = q_inp;
     406      410255 :     move16();
     407      410255 :     move16();
     408             : 
     409      410255 :     Scale_sig( sts[0]->input_fx, input_frame, sub( q_inp, sts[0]->q_inp ) );                /* q_inp */
     410      410255 :     Scale_sig( sts[0]->old_input_signal_fx, input_frame, sub( q_inp, sts[0]->q_old_inp ) ); /* q_inp */
     411      410255 :     sts[0]->q_inp = q_inp;
     412      410255 :     move16();
     413      410255 :     sts[0]->q_old_inp = q_inp;
     414      410255 :     move16();
     415      410255 :     Scale_sig( sts[0]->buf_speech_enc, L_PAST_MAX_32k + L_FRAME32k + L_NEXT_MAX_32k, sub( q_inp, sub( Q15, sts[0]->exp_buf_speech_enc ) ) ); // q_inp
     416      410255 :     sts[0]->exp_buf_speech_enc = sub( Q15, q_inp );
     417      410255 :     move16();
     418      410255 :     stereo_switching_enc_fx( hCPE, sts[0]->old_input_signal_fx, input_frame, q_inp );
     419             : 
     420             :     /*----------------------------------------------------------------*
     421             :      * Temporal inter-channel alignment, stereo adjustment
     422             :      *----------------------------------------------------------------*/
     423             : 
     424      410255 :     Copy_Scale_sig_16_32_DEPREC( sts[0]->input_buff_fx, sts[0]->input_buff32_fx, L_FRAME48k + L_FRAME48k + NS2SA( 48000, DELAY_FIR_RESAMPL_NS ), Q6 ); /* Q6 + sts[0]->q_inp */
     425      410255 :     sts[0]->q_inp32 = add( Q6, sts[0]->q_inp );
     426      410255 :     move16();
     427      410255 :     Copy_Scale_sig_16_32_DEPREC( sts[1]->input_buff_fx, sts[1]->input_buff32_fx, L_FRAME48k + L_FRAME48k + NS2SA( 48000, DELAY_FIR_RESAMPL_NS ), Q6 ); /* Q6 + sts[0]->q_inp */
     428      410255 :     sts[1]->q_inp32 = add( Q6, sts[1]->q_inp );
     429      410255 :     move16();
     430             : 
     431      410255 :     stereo_tca_enc_fx( hCPE, input_frame );
     432             : 
     433      410255 :     shift = sub( add( L_norm_arr( sts[0]->input_buff32_fx, L_FRAME48k + L_FRAME48k + NS2SA( 48000, DELAY_FIR_RESAMPL_NS ) ), sts[0]->q_inp32 ), 16 );
     434      410255 :     shift = s_min( shift, sub( add( L_norm_arr( sts[1]->input_buff32_fx, L_FRAME48k + L_FRAME48k + NS2SA( 48000, DELAY_FIR_RESAMPL_NS ) ), sts[1]->q_inp32 ), 16 ) );
     435      410255 :     Copy_Scale_sig32_16( sts[0]->input_buff32_fx, sts[0]->input_buff_fx, L_FRAME48k + L_FRAME48k + NS2SA( 48000, DELAY_FIR_RESAMPL_NS ), sub( add( Q16, shift ), sts[0]->q_inp32 ) ); // shift
     436      410255 :     Copy_Scale_sig32_16( sts[1]->input_buff32_fx, sts[1]->input_buff_fx, L_FRAME48k + L_FRAME48k + NS2SA( 48000, DELAY_FIR_RESAMPL_NS ), sub( add( Q16, shift ), sts[1]->q_inp32 ) ); // shift
     437             : 
     438      410255 :     sts[0]->q_inp = sts[1]->q_inp = sts[0]->q_old_inp = sts[1]->q_old_inp = shift;
     439      410255 :     move16();
     440      410255 :     move16();
     441      410255 :     move16();
     442      410255 :     move16();
     443             :     /*----------------------------------------------------------------*
     444             :      * Input signal buffering - needed in IC-BWE and TD ITD in MDCT stereo
     445             :      *----------------------------------------------------------------*/
     446             : 
     447     1230765 :     FOR( n = 0; n < CPE_CHANNELS; n++ )
     448             :     {
     449      820510 :         Copy( sts[n]->input_fx, orig_input_fx[n], input_frame );              /* sts->q_inp */
     450      820510 :         Scale_sig( orig_input_fx[n], input_frame, sub( -1, sts[n]->q_inp ) ); // Q(-1)
     451      820510 :         Q_orig_inp[n] = -1;
     452      820510 :         move16();
     453             : 
     454      820510 :         IF( hCPE->hStereoICBWE != NULL )
     455             :         {
     456      119544 :             hCPE->hStereoICBWE->dataChan_fx[n] = &orig_input_fx[n][0]; // Q_orig_inp
     457      119544 :             hCPE->hStereoICBWE->q_dataChan_fx = Q_orig_inp[n];
     458      119544 :             move16();
     459             :         }
     460             :     }
     461             : 
     462             :     /*---------------------------------------------------------------*
     463             :      * Time Domain Transient Detector
     464             :      *---------------------------------------------------------------*/
     465             : 
     466     1230765 :     FOR( n = 0; n < CPE_CHANNELS; n++ )
     467             :     {
     468      820510 :         IF( sts[n]->hTranDet == NULL )
     469             :         {
     470           0 :             currFlatness_fx[n] = 0;
     471           0 :             move32();
     472           0 :             CONTINUE;
     473             :         }
     474             : 
     475      820510 :         test();
     476      820510 :         IF( !( EQ_32( ivas_format, MC_FORMAT ) && EQ_32( st_ivas->mc_mode, MC_MODE_PARAMMC ) ) )
     477             :         {
     478      800370 :             RunTransientDetection_ivas_fx( sts[n]->input_fx, input_frame, sts[n]->hTranDet, sts[n]->q_inp ); // Note q of sts[n]->input_fx changes inside function
     479             :         }
     480             : 
     481      820510 :         currFlatness_fx[n] = GetTCXAvgTemporalFlatnessMeasure_ivas_fx( (const TransientDetection *) sts[n]->hTranDet, NSUBBLOCKS, 0 ); // Q21
     482      820510 :         move32();
     483             :     }
     484             : 
     485             :     /* Synchonize detection for downmix-based stereo */
     486      410255 :     test();
     487      410255 :     IF( EQ_16( hCPE->element_mode, IVAS_CPE_DFT ) || EQ_16( hCPE->element_mode, IVAS_CPE_TD ) )
     488             :     {
     489       63470 :         set_transient_stereo_fx( hCPE, currFlatness_fx );
     490             :     }
     491             : 
     492             :     /*----------------------------------------------------------------*
     493             :      * Configuration of stereo encoder
     494             :      *----------------------------------------------------------------*/
     495             : 
     496     1171122 :     FOR( n = 0; n < n_CoreChannels; n++ )
     497             :     {
     498             :         /* Force to MODE1 in IVAS */
     499      760867 :         sts[n]->codec_mode = MODE1;
     500      760867 :         move16();
     501             : 
     502      760867 :         sts[n]->element_mode = hCPE->element_mode;
     503      760867 :         move16();
     504             :     }
     505             : 
     506      410255 :     test();
     507      410255 :     test();
     508      410255 :     test();
     509      410255 :     test();
     510      410255 :     test();
     511             :     /* No Basop - sts[0]->ini_frame == 0 as comparison with 0 */
     512      410255 :     IF( NE_16( hCPE->element_mode, IVAS_CPE_MDCT ) && ( NE_32( hCPE->element_brate, hCPE->last_element_brate ) || NE_16( hCPE->last_element_mode, hCPE->element_mode ) || sts[0]->ini_frame == 0 ||
     513             :                                                         ( NE_32( ivas_total_brate, st_ivas->hEncoderConfig->last_ivas_total_brate ) ) || LE_32( sts[0]->last_core_brate, SID_2k40 ) ) ) /* If the last frame was SID or NO_DATA, we need to run stereo_dft_config here since VAD decision is not known yet */
     514             :     {
     515             :         STEREO_DFT_CONFIG_DATA_HANDLE hConfig;
     516       11747 :         IF( hCPE->hStereoDft == NULL )
     517             :         {
     518          66 :             hConfig = NULL;
     519             :         }
     520             :         ELSE
     521             :         {
     522       11681 :             hConfig = hCPE->hStereoDft->hConfig;
     523             :         }
     524       11747 :         IF( st_ivas->hQMetaData != NULL )
     525             :         {
     526        1720 :             test();
     527             :             /* No Basop - st_ivas->ism_mode != ISM_MODE_NONE as comparison with 0 */
     528        1720 :             IF( EQ_16( ivas_format, MASA_ISM_FORMAT ) && st_ivas->ism_mode != ISM_MODE_NONE )
     529             :             {
     530         501 :                 stereo_dft_config_fx( hConfig, L_mult0( st_ivas->hQMetaData->bits_frame_nominal, 35 /* 0.70f * FRAMES_PER_SEC */ ), &sts[0]->bits_frame_nominal, &sts[1]->bits_frame_nominal );
     531             :             }
     532             :             ELSE
     533             :             {
     534        1219 :                 stereo_dft_config_fx( hConfig, L_mult0( st_ivas->hQMetaData->bits_frame_nominal, FRAMES_PER_SEC ), &sts[0]->bits_frame_nominal, &sts[1]->bits_frame_nominal );
     535             :             }
     536             :         }
     537             :         ELSE
     538             :         {
     539             :             /* note; "bits_frame_nominal" needed in TD stereo as well */
     540       10027 :             stereo_dft_config_fx( hConfig, hCPE->element_brate, &sts[0]->bits_frame_nominal, &sts[1]->bits_frame_nominal );
     541             :         }
     542             :     }
     543             : 
     544      410255 :     IF( EQ_16( hCPE->element_mode, IVAS_CPE_TD ) )
     545             :     {
     546        3827 :         IF( hCPE->hStereoTD->tdm_LRTD_flag )
     547             :         {
     548        3698 :             sts[0]->bits_frame_nominal = extract_l( Mult_32_32( L_shr( hCPE->element_brate, 1 ), ONE_BY_FRAMES_PER_SEC_Q31 ) ); /* Q0 */
     549        3698 :             sts[1]->bits_frame_nominal = extract_l( Mult_32_32( L_shr( hCPE->element_brate, 1 ), ONE_BY_FRAMES_PER_SEC_Q31 ) ); /* Q0 */
     550             :         }
     551             :         ELSE
     552             :         {
     553         129 :             stereo_dft_config_fx( NULL, hCPE->element_brate, &sts[0]->bits_frame_nominal, &sts[1]->bits_frame_nominal );
     554             :         }
     555             :     }
     556             : 
     557      410255 :     IF( EQ_16( hCPE->element_mode, IVAS_CPE_MDCT ) )
     558             :     {
     559             :         /* compute bit-rate surplus per channel in combined format coding */
     560             :         Word32 brate_surplus[CPE_CHANNELS];
     561      346785 :         test();
     562      346785 :         test();
     563      346785 :         IF( EQ_32( st_ivas->ism_mode, ISM_MASA_MODE_MASA_ONE_OBJ ) || EQ_32( st_ivas->ism_mode, ISM_MASA_MODE_PARAM_ONE_OBJ ) || EQ_32( st_ivas->ism_mode, ISM_MASA_MODE_DISC ) )
     564             :         {
     565        4274 :             brate_surplus[0] = imult3216( L_shr( Mpy_32_32_r( hCPE->brate_surplus, ONE_BY_FRAMES_PER_SEC_Q31 ), 1 ), FRAMES_PER_SEC ); /* Q0 */
     566        4274 :             brate_surplus[1] = L_sub( hCPE->brate_surplus, brate_surplus[0] );                                                         /* Q0 */
     567        4274 :             move32();
     568        4274 :             move32();
     569             :         }
     570             : 
     571             :         /* this is just for initialization, the true values of "total_brate" and "bits_frame_channel" are set later */
     572     1040355 :         FOR( n = 0; n < n_CoreChannels; n++ )
     573             :         {
     574      693570 :             IF( st_ivas->hMCT )
     575             :             {
     576             :                 Word16 lfe_bits;
     577      522336 :                 test();
     578      522336 :                 IF( EQ_16( ivas_format, MC_FORMAT ) && EQ_16( st_ivas->mc_mode, MC_MODE_MCT ) )
     579             :                 {
     580      166800 :                     lfe_bits = st_ivas->hLFE->lfe_bits;
     581      166800 :                     move16();
     582             :                 }
     583             :                 ELSE
     584             :                 {
     585      355536 :                     lfe_bits = 0;
     586      355536 :                     move16();
     587             :                 }
     588      522336 :                 sts[n]->total_brate = hCPE->element_brate;                                                                                                                                                      /* Q0 */
     589      522336 :                 sts[n]->bits_frame_nominal = extract_l( Mpy_32_32_r( hCPE->element_brate, ONE_BY_FRAMES_PER_SEC_Q31 ) );                                                                                        /* Q0 */
     590      522336 :                 sts[n]->bits_frame_channel = ( idiv1616( sub( sub( extract_l( Mpy_32_32_r( ivas_total_brate, ONE_BY_FRAMES_PER_SEC_Q31 ) ), lfe_bits ), nb_bits_metadata ), st_ivas->hMCT->nchan_out_woLFE ) ); /* Q0 */
     591      522336 :                 move32();
     592      522336 :                 move16();
     593      522336 :                 move16();
     594             :             }
     595             :             ELSE
     596             :             {
     597      171234 :                 sts[n]->bits_frame_nominal = extract_l( Mpy_32_32_r( hCPE->element_brate, ONE_BY_FRAMES_PER_SEC_Q31 ) );                             /* Q0 */
     598      171234 :                 sts[n]->bits_frame_channel = idiv1616( extract_l( Mpy_32_32_r( hCPE->element_brate, ONE_BY_FRAMES_PER_SEC_Q31 ) ), n_CoreChannels ); /* Q0 */
     599      171234 :                 sts[n]->total_brate = L_shl( div_l( hCPE->element_brate, n_CoreChannels ), 1 );                                                      /* Q0 */
     600      171234 :                 move32();
     601      171234 :                 move16();
     602      171234 :                 move16();
     603             : 
     604             :                 /* subtract bit-rate for combined format coding */
     605      171234 :                 IF( EQ_32( st_ivas->ism_mode, ISM_MASA_MODE_MASA_ONE_OBJ ) || EQ_32( st_ivas->ism_mode, ISM_MASA_MODE_PARAM_ONE_OBJ ) || EQ_32( st_ivas->ism_mode, ISM_MASA_MODE_DISC ) )
     606             :                 {
     607        8548 :                     sts[n]->bits_frame_channel = add( sts[n]->bits_frame_channel, extract_l( Mpy_32_32_r( brate_surplus[n], ONE_BY_FRAMES_PER_SEC_Q31 ) ) ); /* Q0 */
     608        8548 :                     sts[n]->total_brate = L_add( sts[n]->total_brate, brate_surplus[n] );                                                                    /* Q0 */
     609        8548 :                     move32();
     610        8548 :                     move16();
     611             :                 }
     612             :             }
     613             :         }
     614             : 
     615             :         /* reconfiguration in case of bitrate switching */
     616      346785 :         IF( NE_32( hCPE->element_brate, hCPE->last_element_brate ) && st_ivas->hMCT == NULL )
     617             :         {
     618             : #ifdef DEBUGGING
     619             :             hCPE->hStereoMdct->mdct_stereo_mode_cmdl = hEncoderConfig->mdct_stereo_mode_cmdl;
     620             : #endif
     621        1649 :             initMdctStereoEncData_fx( hCPE->hStereoMdct, ivas_format, hCPE->element_mode, hCPE->element_brate, max_bwidth, 0, NULL, 0 );
     622        1649 :             test();
     623        1649 :             test();
     624        1649 :             hCPE->hStereoMdct->isSBAStereoMode = ( ( EQ_16( ivas_format, SBA_FORMAT ) || EQ_16( ivas_format, SBA_ISM_FORMAT ) ) && ( EQ_16( st_ivas->nchan_transport, 2 ) ) ); /* Q0 */
     625        1649 :             move16();
     626             :         }
     627             :     }
     628             : 
     629             :     /*----------------------------------------------------------------*
     630             :      * Stereo processing
     631             :      * Stereo down-mix
     632             :      *----------------------------------------------------------------*/
     633             :     // printf("\n%f %f ", hCPE->hStereoClassif->is_speech, hCPE->hCoreCoder[0]->hSpMusClas->past_dlp[0]);
     634      410255 :     IF( EQ_16( hCPE->element_mode, IVAS_CPE_DFT ) )
     635             :     {
     636             :         /*flt2fix: dft_synthesize*/
     637       59643 :         test();
     638       59643 :         if ( EQ_16( hCPE->element_mode, IVAS_CPE_DFT ) && hCPE->hStereoDft->res_cod_mode[STEREO_DFT_OFFSET] )
     639             :         {
     640       17586 :             sts[1]->exp_old_inp_12k8 = Q15;
     641       17586 :             move16();
     642             :         }
     643       59643 :         Word16 temp = getScaleFactor32( hCPE->hStereoDft->Spd_L_smooth_fx, STEREO_DFT_N_32k_ENC / 2 );
     644       59643 :         scale_sig32( hCPE->hStereoDft->Spd_L_smooth_fx, STEREO_DFT_N_32k_ENC / 2, temp ); /* exp(hCPE->hStereoDft->Spd_L_smooth_fx_e - temp)*/
     645       59643 :         hCPE->hStereoDft->Spd_L_smooth_fx_e = sub( hCPE->hStereoDft->Spd_L_smooth_fx_e, temp );
     646       59643 :         move16();
     647             : 
     648       59643 :         temp = getScaleFactor32( hCPE->hStereoDft->Spd_R_smooth_fx, STEREO_DFT_N_32k_ENC / 2 );
     649       59643 :         scale_sig32( hCPE->hStereoDft->Spd_R_smooth_fx, STEREO_DFT_N_32k_ENC / 2, temp ); /* exp(hCPE->hStereoDft->Spd_R_smooth_fx_e - temp) */
     650       59643 :         hCPE->hStereoDft->Spd_R_smooth_fx_e = sub( hCPE->hStereoDft->Spd_R_smooth_fx_e, temp );
     651       59643 :         move16();
     652             : 
     653             : 
     654      178929 :         FOR( n = 0; n < CPE_CHANNELS; n++ )
     655             :         {
     656      119286 :             q_com = sub( add( L_norm_arr( sts[n]->input32_fx, input_frame ), sts[n]->q_inp32 ), 16 );
     657      119286 :             q_com = s_min( q_com, add( norm_arr( sts[n]->old_input_signal_fx, input_frame ), sts[n]->q_old_inp ) );
     658      119286 :             q_com = s_min( q_com, add( norm_arr( hCPE->input_mem_fx[n], hCPE->hStereoDft->dft_ovl ), hCPE->q_input_mem[n] ) );
     659             : 
     660      119286 :             if ( EQ_16( q_com, Q15 ) )
     661             :             {
     662           0 :                 q_com = 0;
     663           0 :                 move16();
     664             :             }
     665             : 
     666      119286 :             Copy_Scale_sig_32_16( sts[n]->input32_fx, sts[n]->input_fx, input_frame, sub( q_com, sts[n]->q_inp32 ) );
     667      119286 :             sts[n]->q_inp = q_com;
     668      119286 :             move16();
     669             : 
     670      119286 :             scale_sig( sts[n]->old_input_signal_fx, input_frame, sub( q_com, sts[n]->q_old_inp ) );
     671      119286 :             sts[n]->q_old_inp = q_com;
     672      119286 :             move16();
     673             : 
     674      119286 :             scale_sig( hCPE->input_mem_fx[n], hCPE->hStereoDft->dft_ovl, sub( q_com, hCPE->q_input_mem[n] ) );
     675      119286 :             hCPE->q_input_mem[n] = q_com;
     676      119286 :             move16();
     677             :         }
     678             : 
     679       59643 :         stereo_dft_hybrid_ITD_flag_fx( hCPE->hStereoDft->hConfig, input_Fs, hCPE->hStereoDft->hItd->hybrid_itd_max );
     680             : 
     681             :         /* Time Domain ITD compensation using extrapolation */
     682       59643 :         stereo_td_itd_fx( hCPE->hStereoDft->hItd, hCPE->hStereoDft->input_mem_itd_fx, hCPE->hStereoDft->q_input_mem_itd, hCPE->hStereoDft->hConfig->hybrid_itd_flag, hCPE->hStereoDft->dft_ovl, sts, input_frame, hCPE->input_mem_fx, hCPE->q_input_mem );
     683             : 
     684       59643 :         stereo_dft_enc_analyze_fx( sts, CPE_CHANNELS, input_frame, hCPE->hStereoDft, NULL, hCPE->hStereoDft->DFT_fx, hCPE->hStereoDft->DFT_fx_e, hCPE->input_mem_fx, hCPE->q_input_mem );
     685             : 
     686       59643 :         sts[0]->total_brate = L_mult0( add( sts[0]->bits_frame_nominal, 10 ), FRAMES_PER_SEC ); /* add small overhead; st[0]->total_brate used in coder_type_modif()   Q0*/
     687             :         /* Update DFT Stereo memories */
     688             : 
     689       59643 :         stereo_dft_enc_update_fx( hCPE->hStereoDft, sts[0]->max_bwidth );
     690             : 
     691             :         /* DFT stereo processing */
     692       59643 :         stereo_dft_enc_process_fx( hCPE, vad_flag_dtx, vad_hover_flag, input_frame );
     693             :     }
     694      350612 :     ELSE IF( EQ_16( hCPE->element_mode, IVAS_CPE_TD ) )
     695             :     {
     696        3827 :         Scale_sig( sts[0]->input_fx, input_frame, negate( sts[0]->q_inp ) ); /* Q0 */
     697        3827 :         sts[0]->q_inp = 0;
     698        3827 :         move16();
     699        3827 :         Scale_sig( sts[1]->input_fx, input_frame, negate( sts[1]->q_inp ) ); /* Q0 */
     700        3827 :         sts[1]->q_inp = 0;
     701        3827 :         move16();
     702             : 
     703             :         /* Determine the energy ratio between the 2 channels */
     704        3827 :         tdm_ratio_idx = stereo_tdm_ener_analysis_fx(
     705             :             ivas_format,
     706             :             hCPE, input_frame, &tdm_SM_or_LRTD_Pri, &tdm_ratio_idx_SM ); /* Q0 */
     707             : 
     708             :         /* Compute the downmix signal based on the ratio index */
     709             : 
     710             :         Word16 tdm_SM_flag;
     711        3827 :         IF( hCPE->hStereoTD->tdm_LRTD_flag == 0 )
     712             :         {
     713         129 :             tdm_SM_flag = tdm_SM_or_LRTD_Pri; /* Q0 */
     714         129 :             move16();
     715             :         }
     716             :         ELSE
     717             :         {
     718        3698 :             tdm_SM_flag = 0;
     719        3698 :             move16();
     720             :         }
     721        3827 :         stereo_tdm_downmix_ivas_fx( hCPE->hStereoTD, sts[0]->input_fx, sts[1]->input_fx, input_frame, tdm_ratio_idx, tdm_SM_flag, tdm_ratio_idx_SM );
     722        3827 :         Copy_Scale_sig_16_32_no_sat( sts[0]->input_fx, sts[0]->input32_fx, input_frame, sub( sts[0]->q_inp32, sts[0]->q_inp ) );
     723        3827 :         Copy_Scale_sig_16_32_no_sat( sts[1]->input_fx, sts[1]->input32_fx, input_frame, sub( sts[1]->q_inp32, sts[1]->q_inp ) );
     724             : 
     725             :         /* signal the bitrate for BW selection in the SCh */
     726        3827 :         sts[0]->bits_frame_channel = 0;
     727        3827 :         sts[1]->bits_frame_channel = extract_l( Mpy_32_32_r( hCPE->element_brate, ONE_BY_FRAMES_PER_SEC_Q31 ) );                                    /* Q0 */
     728        3827 :         sts[1]->bits_frame_channel = add( sts[1]->bits_frame_channel, extract_l( Mpy_32_32_r( hCPE->brate_surplus, ONE_BY_FRAMES_PER_SEC_Q31 ) ) ); /* Q0 */
     729        3827 :         move16();
     730        3827 :         move16();
     731        3827 :         move16();
     732        3827 :         if ( st_ivas->hQMetaData != NULL )
     733             :         {
     734          60 :             sts[1]->bits_frame_channel = sub( sts[1]->bits_frame_channel, st_ivas->hQMetaData->metadata_max_bits ); /* Q0 */
     735          60 :             move16();
     736             :         }
     737             : 
     738        3827 :         Etot_last_fx[0] = extract_h( sts[0]->hNoiseEst->Etot_last_32fx ); /* Q8 */
     739        3827 :         Etot_last_fx[1] = extract_h( sts[1]->hNoiseEst->Etot_last_32fx ); /* Q8 */
     740        3827 :         move16();
     741        3827 :         move16();
     742             :     }
     743      346785 :     ELSE IF( EQ_16( hCPE->element_mode, IVAS_CPE_MDCT ) )
     744             :     {
     745      346785 :         test();
     746      346785 :         IF( hCPE->hStereoMdct != NULL && hCPE->hStereoMdct->hItd != NULL )
     747             :         {
     748       14690 :             test();
     749       14690 :             if ( EQ_16( hCPE->element_mode, IVAS_CPE_DFT ) && hCPE->hStereoDft->res_cod_mode[STEREO_DFT_OFFSET] )
     750             :             {
     751           0 :                 sts[1]->exp_old_inp_12k8 = Q15;
     752           0 :                 move16();
     753             :             }
     754             :         }
     755      346785 :         test();
     756      346785 :         IF( hCPE->hStereoMdct != NULL && hCPE->hStereoMdct->hItd != NULL )
     757             :         {
     758             :             Word16 q_min;
     759       14690 :             q_min = sts[1]->q_old_inp;
     760       14690 :             move16();
     761       14690 :             q_min = s_min( q_min, sts[1]->q_inp );
     762       14690 :             q_min = s_min( q_min, sts[0]->q_old_inp );
     763       14690 :             q_min = s_min( q_min, sts[0]->q_inp );
     764             : 
     765       14690 :             shift = norm_arr( sts[1]->old_input_signal_fx, input_frame );
     766       14690 :             shift = s_min( shift, norm_arr( sts[1]->input_fx, input_frame ) );
     767       14690 :             shift = s_min( shift, norm_arr( sts[0]->old_input_signal_fx, input_frame ) );
     768       14690 :             shift = s_min( shift, norm_arr( sts[0]->input_fx, input_frame ) );
     769             : 
     770       14690 :             q_min = add( q_min, shift );
     771             : 
     772       14690 :             scale_sig( sts[1]->input_fx, input_frame, sub( q_min, sts[1]->q_inp ) );                /* q_min */
     773       14690 :             scale_sig( sts[1]->old_input_signal_fx, input_frame, sub( q_min, sts[1]->q_old_inp ) ); /* q_min */
     774       14690 :             scale_sig( sts[0]->input_fx, input_frame, sub( q_min, sts[0]->q_inp ) );                /* q_min */
     775       14690 :             scale_sig( sts[0]->old_input_signal_fx, input_frame, sub( q_min, sts[0]->q_old_inp ) ); /* q_min */
     776             : 
     777       14690 :             sts[1]->q_old_inp = q_min;
     778       14690 :             sts[0]->q_old_inp = q_min;
     779       14690 :             sts[1]->q_inp = q_min;
     780       14690 :             sts[0]->q_inp = q_min;
     781       14690 :             move16();
     782       14690 :             move16();
     783       14690 :             move16();
     784       14690 :             move16();
     785             :         }
     786             : 
     787      346785 :         stereo_td_itd_mdct_stereo_fx( hCPE, vad_flag_dtx, vad_hover_flag, input_frame );
     788             : 
     789      346785 :         Copy_Scale_sig_16_32_no_sat( sts[0]->input_fx - input_frame, sts[0]->input32_fx - input_frame, shl( input_frame, 1 ), sub( sts[0]->q_inp32, sts[0]->q_inp ) );
     790      346785 :         Copy_Scale_sig_16_32_no_sat( sts[1]->input_fx - input_frame, sts[1]->input32_fx - input_frame, shl( input_frame, 1 ), sub( sts[1]->q_inp32, sts[1]->q_inp ) );
     791             :     }
     792             : 
     793             :     /*----------------------------------------------------------------*
     794             :      * DFT stereo: iDFT and resampling on both channels
     795             :      *----------------------------------------------------------------*/
     796      410255 :     IF( EQ_16( hCPE->element_mode, IVAS_CPE_DFT ) )
     797             :     {
     798             :         Word32 internal_Fs;
     799             : 
     800       59643 :         IF( getTcxonly_ivas_fx( IVAS_CPE_MDCT, imult1616( sts[0]->bits_frame_nominal, FRAMES_PER_SEC ), 0, sts[0]->is_ism_format ) == 0 )
     801             :         {
     802       59643 :             internal_Fs = INT_FS_16k;
     803       59643 :             move32();
     804             :         }
     805             :         ELSE
     806             :         {
     807           0 :             internal_Fs = L_max( INT_FS_16k, sts[0]->sr_core );
     808             :         }
     809             : 
     810             :         /* iDFT at input sampling rate */
     811             :         Word16 out_start_ind, out_end_ind;
     812             :         Word16 out_12k8_start_ind[CPE_CHANNELS], out_12k8_end_ind[CPE_CHANNELS];
     813       59643 :         Word16 out_16k_start_ind = 0, out_16k_end_ind = 0;
     814       59643 :         move16();
     815       59643 :         move16();
     816       59643 :         stereo_dft_enc_synthesize_fx( hCPE->hStereoDft, sts[0]->input32_fx, &out_start_ind, &out_end_ind, 0, input_Fs, input_Fs, 0, NULL );
     817       59643 :         Scale_sig32( sts[0]->input32_fx - input_frame, add( out_start_ind, input_frame ), sub( Q15, sts[0]->q_inp32 ) ); // scaling initial part of the input buffer
     818       59643 :         sts[0]->q_inp32 = Q15;
     819       59643 :         move16();
     820             : 
     821             :         // Normalise the input buffer from Q15
     822             :         Word16 input_norm, q_inp32, common_q, fir_delay_len;
     823       59643 :         input_norm = L_norm_arr( sts[0]->input32_fx + out_start_ind, sub( out_end_ind, out_start_ind ) );
     824       59643 :         q_inp32 = add( Q15, input_norm );
     825       59643 :         fir_delay_len = NS2SA_FX2( sts[0]->input_Fs, DELAY_FIR_RESAMPL_NS );
     826       59643 :         move16();
     827             : 
     828             :         // Find common Q-factor between { q_inp, q_old_inp and q_inp32-16 }
     829       59643 :         common_q = s_min( s_min( sub( q_inp32, 16 ), sts[0]->q_inp ), sts[0]->q_old_inp );
     830             : 
     831             :         // Rescale the old input, input and FIR delay section of input buffer
     832       59643 :         scale_sig( sts[0]->old_input_signal_fx, input_frame, sub( common_q, sts[0]->q_old_inp ) );                                                                        // q_old_inp -> common_q
     833       59643 :         Copy_Scale_sig32_16( sts[0]->input32_fx + out_start_ind, sts[0]->input_fx + out_start_ind, sub( out_end_ind, out_start_ind ), sub( add( Q16, common_q ), Q15 ) ); // Q15 -> common_q
     834       59643 :         scale_sig( sts[0]->input_fx + out_end_ind, add( sub( input_frame, out_end_ind ), fir_delay_len ), sub( common_q, sts[0]->q_inp ) );                               // q_inp -> common_q
     835             : 
     836             :         // Update the Q-factors
     837       59643 :         sts[0]->q_inp = common_q;
     838       59643 :         move16();
     839       59643 :         sts[0]->q_old_inp = common_q;
     840       59643 :         move16();
     841             : 
     842             :         /* iDFT & resampling to 12.8kHz internal sampling rate */
     843       59643 :         stereo_dft_enc_synthesize_fx( hCPE->hStereoDft, old_inp_12k8_fx[0] + L_INP_MEM, &out_12k8_start_ind[0], &out_12k8_end_ind[0], 0, input_Fs, INT_FS_12k8, 0, NULL );
     844       59643 :         Copy_Scale_sig_32_16( old_inp_12k8_fx[0], old_inp_12k8_16fx[0], L_INP_12k8, -Q16 ); // Q-1
     845             : 
     846             :         /* iDFT & resampling to 16kHz internal sampling rate for M channel */
     847       59643 :         IF( EQ_32( input_Fs, internal_Fs ) )
     848             :         {
     849       11486 :             Copy32( sts[0]->input32_fx - STEREO_DFT_OVL_16k, old_inp_16k_fx[0] + L_INP_MEM - STEREO_DFT_OVL_16k, add( input_frame, STEREO_DFT_OVL_16k ) ); /* Q_inp */
     850       11486 :             out_16k_start_ind = -STEREO_DFT_OVL_16k;
     851       11486 :             out_16k_end_ind = add( out_16k_start_ind, add( input_frame, STEREO_DFT_OVL_16k ) );
     852       11486 :             move16();
     853       11486 :             move16();
     854             :         }
     855             :         ELSE
     856             :         {
     857       48157 :             stereo_dft_enc_synthesize_fx( hCPE->hStereoDft, old_inp_16k_fx[0] + L_INP_MEM, &out_16k_start_ind, &out_16k_end_ind, 0, input_Fs, internal_Fs, 0, NULL );
     858             :         }
     859       59643 :         Copy_Scale_sig32_16( old_inp_16k_fx[0], old_inp_16k_16fx[0], L_INP, 0 ); // Q15->Q-1
     860             : 
     861             :         /* DFT Stereo: iDFT of residual signal at 8kHz sampling rate */
     862       59643 :         test();
     863       59643 :         IF( EQ_16( hCPE->element_mode, IVAS_CPE_DFT ) && hCPE->hStereoDft->res_cod_mode[STEREO_DFT_OFFSET] )
     864             :         {
     865       19921 :             Copy_Scale_sig_16_32_no_sat( sts[1]->old_inp_12k8_fx, old_inp_12k8_fx[1], L_INP_MEM, 15 - 0 ); // Q15
     866       19921 :             stereo_dft_enc_synthesize_fx( hCPE->hStereoDft, old_inp_12k8_fx[1] + L_INP_MEM, &out_12k8_start_ind[1], &out_12k8_end_ind[1], 1, input_Fs, 8000, 0, NULL );
     867             : 
     868             :             /* update old input signal buffer */
     869       19921 :             Copy_Scale_sig_32_16( old_inp_12k8_fx[1] + L_FRAME8k, sts[1]->old_inp_12k8_fx, L_INP_MEM, 0 - 15 ); // Q0
     870       19921 :             sts[1]->exp_old_inp_12k8 = Q15;
     871       19921 :             move16();
     872       19921 :             Copy_Scale_sig_32_16( old_inp_12k8_fx[1], old_inp_12k8_16fx[1], L_INP_12k8, -Q16 ); // Q-1
     873             :         }
     874             : 
     875             :         /* no iDFT at input sampling rate for Side channel -> reset the buffer */
     876       59643 :         set16_fx( sts[1]->input_fx, 0, input_frame );
     877       59643 :         sts[1]->q_inp = Q15;
     878       59643 :         move16();
     879       59643 :         set32_fx( sts[1]->input32_fx, 0, input_frame );
     880       59643 :         sts[1]->q_inp32 = Q31;
     881       59643 :         move16();
     882             :     }
     883             : 
     884             : #ifdef DEBUG_MODE_INFO
     885             :     dbgwrite( &hCPE->element_mode, sizeof( int16_t ), 1, input_frame, fname( debug_dir, "element_mode", 0, sts[0]->id_element, ENC ) );
     886             : #endif
     887             : 
     888             :     /*----------------------------------------------------------------*
     889             :      * Front Pre-processing
     890             :      *----------------------------------------------------------------*/
     891     1171122 :     FOR( n = 0; n < n_CoreChannels; n++ )
     892             :     {
     893      760867 :         set16_fx( old_wsp_fx[n], 0, L_WSP );
     894      760867 :         q_old_wsp = Q15;
     895      760867 :         move16();
     896             :         Word16 front_vad_flag;
     897      760867 :         IF( st_ivas->hSpar != NULL )
     898             :         {
     899      429116 :             front_vad_flag = st_ivas->hSpar->front_vad_flag;
     900             :         }
     901             :         ELSE
     902             :         {
     903      331751 :             front_vad_flag = 0;
     904             :         }
     905             : 
     906      760867 :         error = pre_proc_front_ivas_fx( NULL, hCPE, hCPE->element_brate, nb_bits_metadata, input_frame, n, old_inp_12k8_16fx[n], old_inp_16k_16fx[n],
     907      760867 :                                         &ener_fx[n], &relE_fx[n], A_fx[n], Aw_fx[n], epsP_fx[n], &epsP_fx_q[n], lsp_new_fx[n], lsp_mid_fx[n], &vad_hover_flag[n], &attack_flag[n],
     908      760867 :                                         realBuffer_fx[n], imagBuffer_fx[n], &q_re_im_buf[n], old_wsp_fx[n], &q_old_wsp, pitch_fr_fx[n], voicing_fr_fx[n], &loc_harm[n], &cor_map_sum_fx[n], &vad_flag_dtx[n], enerBuffer_fx[n], &enerBuffer_fx_exp[n],
     909      760867 :                                         fft_buff_fx[n], &fft_buff_fx_q[n], A_fx[0], lsp_new_fx[0], currFlatness_fx[n], tdm_ratio_idx, fr_bands_fx, q_fr_bands, Etot_LR_fx, lf_E_fx, q_lf_E[n], localVAD_HE_SAD,
     910      760867 :                                         band_energies_LR_fx, q_band_energies_LR, 0, front_vad_flag, 0, 0, ivas_format, st_ivas->hMCT != NULL, st_ivas->hEncoderConfig->last_ivas_total_brate, ivas_total_brate, &Q_new[n]
     911             : #ifdef DEBUG_MODE_INFO
     912             :                                         ,
     913             :                                         ( st_ivas->nSCE + ( cpe_id * CPE_CHANNELS ) + n )
     914             : #endif
     915             :         );
     916      760867 :         e_old_wsp[n] = sub( Q15, q_old_wsp );
     917      760867 :         move16();
     918      760867 :         IF( error != IVAS_ERR_OK )
     919             :         {
     920           0 :             return error;
     921             :         }
     922             :     }
     923             : 
     924             :     /*making Q common*/
     925     1171122 :     FOR( n = 0; n < n_CoreChannels; n++ )
     926             :     {
     927      760867 :         fft_buff_fx_final_q = s_min( fft_buff_fx_final_q, fft_buff_fx_q[n] );
     928             :     }
     929      410255 :     fft_buff_fx_final_q = sub( fft_buff_fx_final_q, 2 ); // headroom
     930     1171122 :     FOR( n = 0; n < n_CoreChannels; n++ )
     931             :     {
     932      760867 :         Scale_sig( fft_buff_fx[n], 2 * L_FFT, sub( fft_buff_fx_final_q, fft_buff_fx_q[n] ) ); /* fft_buff_fx_final_q */
     933             :     }
     934             : 
     935             :     /* sanity check -> DTX not supported for more than one SCEs/CPEs */
     936      410255 :     IF( GT_16( add( st_ivas->nSCE, st_ivas->nCPE ), 1 ) )
     937             :     {
     938      268851 :         test();
     939      268851 :         IF( EQ_32( sts[0]->core_brate, SID_2k40 ) || sts[0]->core_brate == FRAME_NO_DATA )
     940             :         {
     941          48 :             sts[0]->core_brate = -1;                   /* Q0 */
     942          48 :             sts[0]->total_brate = hCPE->element_brate; /* Q0 */
     943          48 :             move32();
     944          48 :             move32();
     945             :         }
     946             :     }
     947             : 
     948             :     /*----------------------------------------------------------------*
     949             :      * Stereo DTX updates
     950             :      *----------------------------------------------------------------*/
     951      410255 :     test();
     952      410255 :     test();
     953      410255 :     IF( EQ_16( ivas_format, MASA_FORMAT ) && nb_bits_metadata > 0 && hCPE->hCoreCoder[0]->Opt_DTX_ON )
     954             :     {
     955        4016 :         test();
     956        4016 :         IF( EQ_16( hCPE->element_mode, IVAS_CPE_DFT ) || EQ_16( hCPE->element_mode, IVAS_CPE_TD ) )
     957             :         {
     958        2368 :             reset_metadata_spatial_fx( ivas_format, hCPE->hMetaData, hCPE->element_brate, &tmp, sts[0]->core_brate, nb_bits_metadata );
     959             :         }
     960             :     }
     961             : 
     962             :     /* MDCT stereo DTX: active/inactive frame decision; compute FD CNG coherence */
     963      410255 :     test();
     964      410255 :     IF( EQ_16( hCPE->element_mode, IVAS_CPE_MDCT ) && hEncoderConfig->Opt_DTX_ON )
     965             :     {
     966       18228 :         stereoFdCngCoherence_fx( sts, hCPE->last_element_mode, fft_buff_fx, sub( 15, fft_buff_fx_final_q ) /*fft exp*/ );
     967             : 
     968             :         /* Reset metadata */
     969       18228 :         test();
     970       18228 :         IF( sts[0]->cng_sba_flag || EQ_32( ivas_format, SBA_FORMAT ) )
     971             :         {
     972        8278 :             reset_metadata_spatial_fx( ivas_format, hCPE->hMetaData, hCPE->element_brate, &tmp, sts[0]->core_brate, nb_bits_metadata );
     973             :         }
     974             :     }
     975             : 
     976             :     /*----------------------------------------------------------------*
     977             :      * Core codec configuration
     978             :      *----------------------------------------------------------------*/
     979             :     /* IGF reconfiguration */
     980     1171122 :     FOR( n = 0; n < n_CoreChannels; n++ )
     981             :     {
     982      760867 :         test();
     983      760867 :         test();
     984      760867 :         test();
     985      760867 :         test();
     986      760867 :         test();
     987      760867 :         test();
     988      760867 :         IF( ( NE_32( hCPE->last_element_brate, hCPE->element_brate ) || NE_16( hCPE->element_mode, hCPE->last_element_mode ) || ( EQ_16( hCPE->element_mode, IVAS_CPE_TD ) && NE_16( sts[0]->bits_frame_nominal, last_bits_frame_nominal ) ) || NE_16( sts[n]->last_bwidth, sts[n]->bwidth ) ) && ( n == 0 || EQ_16( hCPE->element_mode, IVAS_CPE_MDCT ) ) )
     989             :         {
     990             :             Word16 igf;
     991        8212 :             igf = getIgfPresent_fx( sts[n]->element_mode, L_mult0( sts[n]->bits_frame_nominal, FRAMES_PER_SEC ), sts[n]->max_bwidth, sts[n]->rf_mode ); /* Q0 */
     992        8212 :             IF( ( error = IGF_Reconfig_fx( &sts[n]->hIGFEnc, igf, 0, L_mult0( sts[n]->bits_frame_nominal, FRAMES_PER_SEC ), sts[n]->max_bwidth, sts[n]->element_mode, sts[n]->rf_mode ) ) != IVAS_ERR_OK )
     993             :             {
     994           0 :                 return error;
     995             :             }
     996             :         }
     997             :     }
     998      410255 :     test();
     999      410255 :     IF( EQ_16( hCPE->element_mode, IVAS_CPE_MDCT ) && st_ivas->hMCT == NULL )
    1000             :     {
    1001             :         /* set coded BW for MDCT stereo */
    1002       85617 :         set_bw_stereo_fx( hCPE );
    1003             : 
    1004             :         /* reconfiguration of MDCT stereo */
    1005       85617 :         test();
    1006       85617 :         test();
    1007       85617 :         test();
    1008       85617 :         IF( sts[0]->bwidth != sts[0]->last_bwidth || ( ( hCPE->last_element_brate != hCPE->element_brate || hCPE->last_element_mode != hCPE->element_mode ) && sts[0]->bwidth != sts[0]->max_bwidth ) )
    1009             :         {
    1010         614 :             initMdctStereoEncData_fx( hCPE->hStereoMdct, ivas_format, hCPE->element_mode, hCPE->element_brate, sts[0]->bwidth, 0, NULL, 0 );
    1011             : 
    1012         614 :             test();
    1013         614 :             test();
    1014         614 :             test();
    1015         614 :             hCPE->hStereoMdct->isSBAStereoMode = ( ( EQ_16( ivas_format, SBA_FORMAT ) || EQ_16( ivas_format, SBA_ISM_FORMAT ) ) && ( EQ_16( st_ivas->nchan_transport, 2 ) ) ); /* Q0 */
    1016             : 
    1017         614 :             test();
    1018         614 :             IF( LE_32( hCPE->element_brate, MAX_MDCT_ITD_BRATE ) && EQ_16( ivas_format, STEREO_FORMAT ) )
    1019             :             {
    1020          84 :                 IF( ( error = initMdctItdHandling_fx( hCPE->hStereoMdct, input_Fs ) ) != IVAS_ERR_OK )
    1021             :                 {
    1022           0 :                     return error;
    1023             :                 }
    1024             :             }
    1025             :         }
    1026             :     }
    1027             : 
    1028             :     /* set ACELP@12k8 / ACELP@16k flag for flexible ACELP core */
    1029     1171122 :     FOR( n = 0; n < n_CoreChannels; n++ )
    1030             :     {
    1031      760867 :         test();
    1032      760867 :         test();
    1033      760867 :         IF( ( EQ_32( sts[0]->core_brate, SID_2k40 ) || sts[0]->core_brate == FRAME_NO_DATA ) && EQ_16( hCPE->element_mode, IVAS_CPE_DFT ) )
    1034             :         {
    1035       10307 :             sts[n]->flag_ACELP16k = set_ACELP_flag_IVAS( hCPE->element_mode, hCPE->element_brate, sts[n]->core_brate, n, sts[0]->tdm_LRTD_flag, sts[n]->bwidth, sts[n]->cng_type ); /* Q0 */
    1036       10307 :             move16();
    1037             :         }
    1038             :         ELSE
    1039             :         {
    1040      750560 :             sts[n]->flag_ACELP16k = set_ACELP_flag_IVAS( hCPE->element_mode, hCPE->element_brate, sts[n]->total_brate, n, sts[0]->tdm_LRTD_flag, sts[n]->bwidth, sts[n]->cng_type ); /* Q0 */
    1041      750560 :             move16();
    1042             :         }
    1043             :     }
    1044             : 
    1045             :     /* configure TD stereo encoder */
    1046      410255 :     IF( EQ_16( hCPE->element_mode, IVAS_CPE_TD ) )
    1047             :     {
    1048        3827 :         tdm_ol_pitch_comparison_fx( hCPE, pitch_fr_fx, voicing_fr_fx );
    1049        3827 :         tdm_configure_enc_fx( ivas_format, st_ivas->ism_mode, hCPE, Etot_last_fx, tdm_SM_or_LRTD_Pri, tdm_ratio_idx, tdm_ratio_idx_SM, attack_flag[0], nb_bits_metadata );
    1050             : 
    1051        3827 :         IF( hEncoderConfig->Opt_DTX_ON )
    1052             :         {
    1053        1669 :             stereo_cng_upd_counters_fx( hCPE->hStereoCng, hCPE->element_mode, -1, NULL, sts[0]->hTdCngEnc->burst_ho_cnt, NULL );
    1054             :         }
    1055             :     }
    1056             : 
    1057             :     /* modify the coder_type depending on the total_brate per channel */
    1058     1171122 :     FOR( n = 0; n < n_CoreChannels; n++ )
    1059             :     {
    1060      760867 :         test();
    1061      760867 :         test();
    1062      760867 :         IF( ( NE_16( hCPE->element_mode, IVAS_CPE_DFT ) && NE_16( hCPE->element_mode, IVAS_CPE_TD ) ) || n == 0 ) /* modify coder_type of primary channel */
    1063             :         {
    1064             :             /* limit coder_type depending on the bitrate */
    1065      757040 :             coder_type_modif_ivas_fx( sts[n], relE_fx[n] );
    1066             :         }
    1067             :     }
    1068             : 
    1069             :     /*----------------------------------------------------------------*
    1070             :      * Write IVAS format signaling in SID frames
    1071             :      *----------------------------------------------------------------*/
    1072             : 
    1073      410255 :     IF( EQ_32( sts[0]->core_brate, SID_2k40 ) )
    1074             :     {
    1075        2062 :         ivas_write_format_sid_fx( ivas_format, hCPE->element_mode, sts[0]->hBstr );
    1076             :     }
    1077             : 
    1078             :     /*----------------------------------------------------------------*
    1079             :      * DFT Stereo residual coding
    1080             :      * DFT Stereo parameters writing into the bitstream
    1081             :      *----------------------------------------------------------------*/
    1082             : 
    1083      410255 :     cpe_brate = 0;
    1084      410255 :     move16();
    1085      410255 :     IF( EQ_16( hCPE->element_mode, IVAS_CPE_DFT ) )
    1086             :     {
    1087       59643 :         IF( hEncoderConfig->Opt_DTX_ON )
    1088             :         {
    1089       30299 :             test();
    1090       30299 :             IF( EQ_32( sts[0]->core_brate, SID_2k40 ) || sts[0]->core_brate == FRAME_NO_DATA )
    1091             :             {
    1092             :                 /* Reconfigure DFT Stereo for inactive frames */
    1093       10307 :                 IF( EQ_32( sts[0]->core_brate, SID_2k40 ) )
    1094             :                 {
    1095        1523 :                     stereo_dft_config_fx( hCPE->hStereoDft->hConfig, IVAS_SID_5k2, &sts[0]->bits_frame_nominal, &sts[1]->bits_frame_nominal );
    1096             :                 }
    1097             :                 ELSE
    1098             :                 {
    1099        8784 :                     stereo_dft_config_fx( hCPE->hStereoDft->hConfig, FRAME_NO_DATA, &sts[0]->bits_frame_nominal, &sts[1]->bits_frame_nominal );
    1100             :                 }
    1101             : 
    1102       10307 :                 stereo_dft_cng_side_gain_fx( hCPE->hStereoDft, hCPE->hStereoCng, sts[0]->core_brate, sts[0]->last_core_brate, sts[0]->bwidth );
    1103             :             }
    1104             :             ELSE
    1105             :             {
    1106       19992 :                 stereo_cng_upd_counters_fx( hCPE->hStereoCng, hCPE->element_mode, hCPE->hStereoDft->nbands, hCPE->hStereoDft->sidSideGain_fx, sts[0]->hTdCngEnc->burst_ho_cnt, &hCPE->hStereoDft->coh_fade_counter );
    1107             :             }
    1108             :         }
    1109             : 
    1110             :         /* Write stereo bitstream */
    1111       59643 :         cpe_brate = st_ivas->hCPE[0]->element_brate; /* Q0 */
    1112       59643 :         move16();
    1113             : 
    1114             :         /* DFT stereo side bits */
    1115       59643 :         test();
    1116       59643 :         test();
    1117       59643 :         test();
    1118       59643 :         test();
    1119       59643 :         test();
    1120       59643 :         test();
    1121       59643 :         test();
    1122       59643 :         IF( ( EQ_16( ivas_format, MASA_FORMAT ) || EQ_16( ivas_format, MASA_ISM_FORMAT ) ) && LT_32( cpe_brate, MASA_STEREO_MIN_BITRATE ) && NE_32( sts[0]->core_brate, SID_2k40 ) && sts[0]->core_brate != FRAME_NO_DATA )
    1123             :         {
    1124        5639 :             nb_bits = 0; /* Only mono downmix is transmitted in this case */
    1125        5639 :             move16();
    1126             :         }
    1127       54004 :         ELSE IF( ( EQ_16( ivas_format, MASA_FORMAT ) || EQ_16( ivas_format, MASA_ISM_FORMAT ) ) && ( EQ_32( sts[0]->core_brate, SID_2k40 ) || sts[0]->core_brate == FRAME_NO_DATA ) )
    1128             :         {
    1129         528 :             nb_bits = hCPE->hMetaData->nb_bits_tot; /* Q0 */
    1130         528 :             move16();
    1131             :         }
    1132             :         ELSE
    1133             :         {
    1134       53476 :             stereo_dft_enc_write_BS_fx( hCPE, &nb_bits );
    1135             :         }
    1136             : 
    1137             :         /* Residual coding in MDCT domain */
    1138       59643 :         test();
    1139       59643 :         test();
    1140       59643 :         IF( !( ( EQ_16( ivas_format, MASA_FORMAT ) || EQ_16( ivas_format, MASA_ISM_FORMAT ) ) && ( EQ_32( sts[0]->core_brate, SID_2k40 ) || sts[0]->core_brate == FRAME_NO_DATA ) ) )
    1141             :         {
    1142       59115 :             Word16 max_bits = extract_l( W_extract_h( W_sub( W_mult_32_32( hCPE->element_brate, ONE_BY_FRAMES_PER_SEC_Q31 ), W_mult_32_32( L_deposit_l( sts[0]->bits_frame_nominal ), 1717986918 /* 0.8f in Q31 */ ) ) ) );
    1143       59115 :             test();
    1144       59115 :             IF( EQ_16( ivas_format, MASA_FORMAT ) || EQ_16( ivas_format, MASA_ISM_FORMAT ) )
    1145             :             {
    1146       11396 :                 max_bits = sub( max_bits, nb_bits_metadata );
    1147       11396 :                 IF( hCPE->brate_surplus < 0 )
    1148             :                 {
    1149        2293 :                     max_bits = add( max_bits, extract_l( Mpy_32_32_r( hCPE->brate_surplus, ONE_BY_FRAMES_PER_SEC_Q31 ) ) );
    1150             :                 }
    1151             :             }
    1152             : 
    1153      177345 :             FOR( Word16 i = 0; i < CPE_CHANNELS; i++ )
    1154             :             {
    1155      118230 :                 Copy_Scale_sig_16_32_no_sat( old_inp_12k8_16fx[i], old_inp_12k8_fx[i], L_INP_12k8, Q16 ); // Q(-1) -> Q15
    1156             :             }
    1157       59115 :             stereo_dft_enc_res_fx( hCPE->hStereoDft, old_inp_12k8_fx[1] + L_INP_MEM - STEREO_DFT_OVL_8k, hCPE->hMetaData, &nb_bits, max_bits );
    1158             :         }
    1159             : 
    1160       59643 :         IF( sts[0]->core_brate == FRAME_NO_DATA || EQ_32( sts[0]->core_brate, SID_2k40 ) )
    1161             :         {
    1162       10307 :             assert( ( nb_bits <= ( ( IVAS_SID_5k2 - SID_2k40 ) / FRAMES_PER_SEC - SID_FORMAT_NBITS ) ) && "Stereo DFT CNG: bit budget is violated" );
    1163             :         }
    1164             :         ELSE
    1165             :         {
    1166             :             /* Flexible total bitrate in M channel */
    1167       49336 :             sts[0]->total_brate = L_sub( hCPE->element_brate, L_deposit_l( imult1616( nb_bits, FRAMES_PER_SEC ) ) );
    1168       49336 :             move32();
    1169             :         }
    1170             : 
    1171             :         /* subtract metadata bitbudget */
    1172       59643 :         sts[0]->total_brate = L_sub( sts[0]->total_brate, L_deposit_l( imult1616( nb_bits_metadata, FRAMES_PER_SEC ) ) );
    1173       59643 :         move32();
    1174             : 
    1175             :         /* subtract bit-rate for combined format coding */
    1176       59643 :         test();
    1177       59643 :         test();
    1178       59643 :         test();
    1179       59643 :         if ( EQ_16( ivas_format, MASA_ISM_FORMAT ) && ( EQ_32( st_ivas->ism_mode, ISM_MASA_MODE_MASA_ONE_OBJ ) || EQ_32( st_ivas->ism_mode, ISM_MASA_MODE_PARAM_ONE_OBJ ) || EQ_32( st_ivas->ism_mode, ISM_MASA_MODE_DISC ) ) )
    1180             :         {
    1181        2784 :             sts[0]->total_brate = L_add( sts[0]->total_brate, hCPE->brate_surplus );
    1182        2784 :             move32();
    1183             :         }
    1184             :     }
    1185             : 
    1186             :     /*----------------------------------------------------------------*
    1187             :      * Core Encoder
    1188             :      *----------------------------------------------------------------*/
    1189             : 
    1190     1171122 :     FOR( n = 0; n < n_CoreChannels; n++ )
    1191             :     {
    1192      760867 :         Scale_sig( old_wsp_fx[n], L_WSP, sub( e_old_wsp[n], Q16 ) ); /* exp(16) */
    1193      760867 :         e_old_wsp[n] = 16;
    1194      760867 :         move16();
    1195             :     }
    1196      410255 :     IF( NE_32( ( error = ivas_core_enc_fx( NULL, hCPE, st_ivas->hMCT, n_CoreChannels, old_inp_12k8_16fx, old_inp_16k_16fx, Q_new, ener_fx, A_fx, Aw_fx, epsP_fx, epsP_fx_q, lsp_new_fx, lsp_mid_fx, vad_hover_flag, attack_flag, realBuffer_fx, imagBuffer_fx, q_re_im_buf, old_wsp_fx, e_old_wsp, loc_harm, cor_map_sum_fx, vad_flag_dtx, enerBuffer_fx, enerBuffer_fx_exp, fft_buff_fx, tdm_SM_or_LRTD_Pri, ivas_format, 0 ) ), IVAS_ERR_OK ) )
    1197             :     {
    1198           0 :         return error;
    1199             :     }
    1200             : 
    1201             :     /*----------------------------------------------------------------*
    1202             :      * Common updates
    1203             :      *----------------------------------------------------------------*/
    1204             : 
    1205      410255 :     hCPE->last_element_brate = hCPE->element_brate; /* Q0 */
    1206      410255 :     move32();
    1207      410255 :     hCPE->last_element_mode = hCPE->element_mode; /* Q0 */
    1208      410255 :     move16();
    1209             : 
    1210      410255 :     if ( EQ_16( ivas_format, MASA_ISM_FORMAT ) )
    1211             :     {
    1212        9000 :         hCPE->element_brate = element_brate_ref; /* Q0 */
    1213        9000 :         move32();
    1214             :     }
    1215             : 
    1216      410255 :     IF( EQ_16( hCPE->element_mode, IVAS_CPE_MDCT ) && hCPE->hStereoMdct != NULL && hCPE->hStereoMdct->hItd != NULL )
    1217             :     {
    1218             :         /* update input samples buffer */
    1219       44070 :         FOR( n = 0; n < CPE_CHANNELS; n++ )
    1220             :         {
    1221       29380 :             Copy( orig_input_fx[n], sts[n]->old_input_signal_fx, input_frame ); /* Q_orig_inp */
    1222       29380 :             sts[n]->q_old_inp = Q_orig_inp[n];
    1223       29380 :             move16();
    1224       29380 :             Copy_Scale_sig_16_32_no_sat( sts[n]->input_fx - input_frame, sts[n]->input32_fx - input_frame, input_frame, 16 ); // duplicating the data for input32_fx
    1225       29380 :             sts[n]->q_inp32 = add( Q_orig_inp[n], 16 );
    1226       29380 :             move16();
    1227             :         }
    1228             :     }
    1229      395565 :     ELSE IF( EQ_16( hCPE->element_mode, IVAS_CPE_DFT ) )
    1230             :     {
    1231       59643 :         Copy( sts[0]->input_fx, sts[0]->old_input_signal_fx, input_frame ); /* sts[n]->q_inp */
    1232       59643 :         sts[0]->q_old_inp = sts[0]->q_inp;
    1233       59643 :         move16();
    1234       59643 :         Copy32( sts[0]->input32_fx, sts[0]->input32_fx - input_frame, input_frame ); /* st->q_inp32 */
    1235             :     }
    1236      335922 :     ELSE IF( st_ivas->hMCT == NULL ) /* note: in MCT, input buffers are updated later in ivas_mct_enc() */
    1237             :     {
    1238             :         /* update input samples buffer */
    1239      224262 :         FOR( n = 0; n < CPE_CHANNELS; n++ )
    1240             :         {
    1241      149508 :             Copy( sts[n]->input_fx, sts[n]->old_input_signal_fx, input_frame ); /* sts[n]->q_inp */
    1242      149508 :             sts[n]->q_old_inp = sts[n]->q_inp;
    1243      149508 :             move16();
    1244      149508 :             Copy32( sts[n]->input32_fx, sts[n]->input32_fx - input_frame, input_frame ); /* st->q_inp32 */
    1245             :         }
    1246             :     }
    1247             : 
    1248      410255 :     IF( hCPE->hFrontVad[0] != NULL )
    1249             :     {
    1250       31968 :         hCPE->hFrontVad[0]->ini_frame = add( hCPE->hFrontVad[0]->ini_frame, 1 );
    1251       31968 :         hCPE->hFrontVad[0]->ini_frame = s_min( hCPE->hFrontVad[0]->ini_frame, MAX_FRAME_COUNTER );
    1252       31968 :         move16();
    1253       31968 :         move16();
    1254             :     }
    1255             : 
    1256             :     /* Store previous attack detection flag */
    1257     1230765 :     FOR( n = 0; n < CPE_CHANNELS; n++ )
    1258             :     {
    1259      820510 :         sts[n]->hTranDet->transientDetector.prev_bIsAttackPresent = sts[n]->hTranDet->transientDetector.bIsAttackPresent; /* Q0 */
    1260      820510 :         move16();
    1261             :     }
    1262             : 
    1263      410255 :     IF( Q_new_out )
    1264             :     {
    1265      261168 :         Copy( Q_new, Q_new_out, CPE_CHANNELS );
    1266             :     }
    1267             : 
    1268             : 
    1269             : #ifdef DEBUG_MODE_INFO
    1270             :     {
    1271             :         float tmpF = ivas_total_brate / 1000.0f;
    1272             :         dbgwrite( &tmpF, sizeof( float ), 1, input_frame, fname( debug_dir, "element_brate", 0, cpe_id, ENC ) );
    1273             :     }
    1274             : #endif
    1275             : 
    1276      410255 :     pop_wmops();
    1277      410255 :     return error;
    1278             : }
    1279             : 
    1280             : /*-------------------------------------------------------------------------
    1281             :  * create_cpe_enc()
    1282             :  *
    1283             :  * Create, allocate and initialize IVAS encoder CPE handle
    1284             :  *-------------------------------------------------------------------------*/
    1285        2612 : ivas_error create_cpe_enc_fx(
    1286             :     Encoder_Struct *st_ivas,   /* i/o: IVAS encoder structure      */
    1287             :     const Word16 cpe_id,       /* i  : CPE # identifier            Q0*/
    1288             :     const Word32 element_brate /* i  : element bitrate             Q0*/
    1289             : )
    1290             : {
    1291             :     Word16 n;
    1292             :     Word16 ivas_format, element_mode_init, max_bwidth;
    1293             :     Word32 input_Fs;
    1294             :     CPE_ENC_HANDLE hCPE;
    1295             :     Encoder_State *st;
    1296             :     ENCODER_CONFIG_HANDLE hEncoderConfig;
    1297             :     ivas_error error;
    1298             : 
    1299        2612 :     error = IVAS_ERR_OK;
    1300             : 
    1301        2612 :     hEncoderConfig = st_ivas->hEncoderConfig;
    1302             : 
    1303        2612 :     ivas_format = hEncoderConfig->ivas_format;
    1304        2612 :     move16();
    1305        2612 :     element_mode_init = hEncoderConfig->element_mode_init; /* Q0 */
    1306        2612 :     move16();
    1307        2612 :     input_Fs = hEncoderConfig->input_Fs; /* Q0 */
    1308        2612 :     move16();
    1309        2612 :     max_bwidth = hEncoderConfig->max_bwidth; /* Q0 */
    1310        2612 :     move16();
    1311             : 
    1312             :     /*-----------------------------------------------------------------*
    1313             :      * Allocate CPE handle
    1314             :      *-----------------------------------------------------------------*/
    1315             : 
    1316        2612 :     IF( ( hCPE = (CPE_ENC_HANDLE) malloc( sizeof( CPE_ENC_DATA ) ) ) == NULL )
    1317             :     {
    1318           0 :         return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for CPE\n" ) );
    1319             :     }
    1320             : 
    1321             :     /*-----------------------------------------------------------------*
    1322             :      * Initialization - general parameters
    1323             :      *-----------------------------------------------------------------*/
    1324             : 
    1325        2612 :     hCPE->cpe_id = cpe_id; /* Q0 */
    1326        2612 :     move16();
    1327        2612 :     hCPE->element_brate = element_brate; /* Q0 */
    1328        2612 :     move16();
    1329        2612 :     hCPE->last_element_brate = hCPE->element_brate; /* Q0 */
    1330        2612 :     move16();
    1331        2612 :     hCPE->element_mode = element_mode_init; /* Q0 */
    1332        2612 :     move16();
    1333        2612 :     hCPE->last_element_mode = element_mode_init; /* Q0 */
    1334        2612 :     move16();
    1335             : 
    1336        2612 :     hCPE->hStereoDft = NULL;
    1337        2612 :     hCPE->hStereoTD = NULL;
    1338        2612 :     hCPE->hStereoMdct = NULL;
    1339        2612 :     hCPE->hStereoTCA = NULL;
    1340        2612 :     hCPE->hStereoICBWE = NULL;
    1341        2612 :     hCPE->hMetaData = NULL;
    1342             : 
    1343        2612 :     hCPE->hStereoCng = NULL;
    1344        2612 :     hCPE->hFrontVad[0] = NULL;
    1345        2612 :     hCPE->hFrontVad[1] = NULL;
    1346             : 
    1347        2612 :     hCPE->brate_surplus = 0;
    1348        2612 :     move16();
    1349             :     /*-----------------------------------------------------------------*
    1350             :      * Input memory buffer: allocate and initialize
    1351             :      *-----------------------------------------------------------------*/
    1352             : 
    1353        7836 :     FOR( n = 0; n < CPE_CHANNELS; n++ )
    1354             :     {
    1355        5224 :         test();
    1356        5224 :         test();
    1357        5224 :         test();
    1358        5224 :         test();
    1359        5224 :         IF( EQ_16( ivas_format, STEREO_FORMAT ) || EQ_16( ivas_format, MASA_FORMAT ) || ( EQ_16( ivas_format, MC_FORMAT ) && EQ_16( st_ivas->mc_mode, MC_MODE_MCMASA ) ) || EQ_16( ivas_format, MASA_ISM_FORMAT ) )
    1360             :         {
    1361         358 :             IF( ( hCPE->input_mem_fx[n] = (Word16 *) malloc( sizeof( Word16 ) * NS2SA_FX2( input_Fs, STEREO_DFT_OVL_NS ) ) ) == NULL )
    1362             :             {
    1363           0 :                 return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DFT stereo memory\n" ) );
    1364             :             }
    1365             : 
    1366         358 :             set16_zero_fx( hCPE->input_mem_fx[n], NS2SA_FX2( input_Fs, STEREO_DFT_OVL_NS ) );
    1367             :         }
    1368             :         ELSE
    1369             :         {
    1370        4866 :             hCPE->input_mem_fx[n] = NULL;
    1371             :         }
    1372             :     }
    1373        2612 :     set16_fx( hCPE->q_input_mem, Q15, CPE_CHANNELS );
    1374             : 
    1375             :     /*-----------------------------------------------------------------*
    1376             :      * stereo classifier: allocate and initialize
    1377             :      *-----------------------------------------------------------------*/
    1378             : 
    1379        2612 :     IF( ( hCPE->hStereoClassif = (STEREO_CLASSIF_HANDLE) malloc( sizeof( STEREO_CLASSIF_DATA ) ) ) == NULL )
    1380             :     {
    1381           0 :         return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for stereo classifier structure\n" ) );
    1382             :     }
    1383             : 
    1384        2612 :     stereo_classifier_init_fx( hCPE->hStereoClassif );
    1385             : 
    1386             :     /*-----------------------------------------------------------------*
    1387             :      * Metadata: allocate and initialize
    1388             :      *-----------------------------------------------------------------*/
    1389             : 
    1390        2612 :     IF( EQ_16( cpe_id, sub( st_ivas->nCPE, 1 ) ) )
    1391             :     {
    1392        1379 :         IF( NE_32( ( error = ivas_initialize_MD_bstr_enc_fx( &( hCPE->hMetaData ), st_ivas ) ), IVAS_ERR_OK ) )
    1393             :         {
    1394           0 :             return error;
    1395             :         }
    1396             :     }
    1397             : 
    1398             :     /*-----------------------------------------------------------------*
    1399             :      * CoreCoder, 2 instances: allocate and initialize
    1400             :      *-----------------------------------------------------------------*/
    1401             : 
    1402        7836 :     FOR( n = 0; n < CPE_CHANNELS; n++ )
    1403             :     {
    1404        5224 :         IF( ( st = (ENC_CORE_HANDLE) malloc( sizeof( Encoder_State ) ) ) == NULL )
    1405             :         {
    1406           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for CoreCoder structure\n" ) );
    1407             :         }
    1408             : 
    1409        5224 :         copy_encoder_config_fx( st_ivas, st, 1 );
    1410             : 
    1411        5224 :         IF( GT_16( st_ivas->nCPE, 1 ) )
    1412             :         {
    1413        4322 :             st->total_brate = hCPE->element_brate; /* dummy initialization for getting right pointers initialization of input buffers in init_coder_ace_plus()    Q0*/
    1414        4322 :             move32();
    1415             :         }
    1416             :         ELSE
    1417             :         {
    1418         902 :             st->total_brate = L_shr( hCPE->element_brate, 1 ); /* dummy initialization for getting right pointers initialization of input buffers in init_coder_ace_plus()        Q0*/
    1419         902 :             move32();
    1420             :         }
    1421        5224 :         st->mct_chan_mode = MCT_CHAN_MODE_REGULAR;
    1422        5224 :         move16();
    1423        5224 :         IF( NE_32( ( error = init_encoder_ivas_fx( st, st_ivas, n, hEncoderConfig->var_SID_rate_flag, hEncoderConfig->interval_SID, 0, ISM_MODE_NONE, hCPE->element_brate ) ), IVAS_ERR_OK ) )
    1424             :         {
    1425           0 :             return error;
    1426             :         }
    1427             : 
    1428        5224 :         IF( st->hFdCngEnc != NULL )
    1429             :         {
    1430         145 :             st->hFdCngEnc->hFdCngCom->CngBitrate = L_sub( hCPE->element_brate, 1 ); /* Q0 */
    1431         145 :             move32();
    1432             :         }
    1433             : 
    1434        5224 :         hCPE->hCoreCoder[n] = st;
    1435             :     }
    1436             : 
    1437             :     /*-----------------------------------------------------------------*
    1438             :      * LR VAD initialization
    1439             :      *-----------------------------------------------------------------*/
    1440             : 
    1441             : #ifdef DEBUGGING
    1442             :     if ( hEncoderConfig->Opt_DTX_ON && ( hCPE->element_mode == IVAS_CPE_TD || hEncoderConfig->stereo_mode_cmdl == 1 ) && !( ivas_format == MASA_FORMAT && element_mode_init == IVAS_CPE_MDCT ) )
    1443             : #else
    1444        2612 :     IF( hEncoderConfig->Opt_DTX_ON )
    1445             : #endif
    1446             :     {
    1447          88 :         test();
    1448          88 :         IF( EQ_16( hCPE->element_mode, IVAS_CPE_TD ) || EQ_16( hCPE->element_mode, IVAS_CPE_DFT ) )
    1449             :         {
    1450          31 :             IF( ( hCPE->hStereoCng = (STEREO_CNG_ENC_HANDLE) malloc( sizeof( STEREO_CNG_ENC ) ) ) == NULL )
    1451             :             {
    1452           0 :                 return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Stereo Cng for Unified/TD \n" ) );
    1453             :             }
    1454             : 
    1455          31 :             stereo_enc_cng_init_fx( hCPE->hStereoCng );
    1456             :         }
    1457             :     }
    1458             :     ELSE
    1459             :     {
    1460        2524 :         hCPE->hStereoCng = NULL;
    1461             :     }
    1462             : 
    1463        2612 :     IF( hEncoderConfig->Opt_DTX_ON && NE_16( element_mode_init, IVAS_CPE_MDCT ) )
    1464             :     {
    1465          93 :         FOR( n = 0; n < CPE_CHANNELS; n++ )
    1466             :         {
    1467          62 :             IF( ( error = front_vad_create_fx( &( hCPE->hFrontVad[n] ), hEncoderConfig ) ) != IVAS_ERR_OK )
    1468             :             {
    1469           0 :                 return error;
    1470             :             }
    1471             :         }
    1472             :     }
    1473             :     ELSE
    1474             :     {
    1475        7743 :         FOR( n = 0; n < CPE_CHANNELS; n++ )
    1476             :         {
    1477        5162 :             hCPE->hFrontVad[n] = NULL;
    1478             :         }
    1479             :     }
    1480             : 
    1481             :     /*-----------------------------------------------------------------*
    1482             :      * DFT stereo initialization
    1483             :      *-----------------------------------------------------------------*/
    1484             : 
    1485        2612 :     IF( EQ_16( hCPE->element_mode, IVAS_CPE_DFT ) )
    1486             :     {
    1487          99 :         IF( ( error = stereo_dft_enc_create_fx( &( hCPE->hStereoDft ), input_Fs, max_bwidth ) ) != IVAS_ERR_OK )
    1488             :         {
    1489           0 :             return error;
    1490             :         }
    1491             :     }
    1492             : 
    1493             :     /*-----------------------------------------------------------------*
    1494             :      * Temporal inter-channel alignment initialization
    1495             :      *-----------------------------------------------------------------*/
    1496             : 
    1497        2612 :     IF( NE_16( hCPE->element_mode, IVAS_CPE_MDCT ) )
    1498             :     {
    1499          99 :         IF( ( hCPE->hStereoTCA = (STEREO_TCA_ENC_HANDLE) malloc( sizeof( STEREO_TCA_ENC_DATA ) ) ) == NULL )
    1500             :         {
    1501           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Stereo TCA\n" ) );
    1502             :         }
    1503             : 
    1504          99 :         stereo_tca_init_enc_fx( hCPE->hStereoTCA, input_Fs );
    1505             :     }
    1506             : 
    1507             :     /*-----------------------------------------------------------------*
    1508             :      * Stereo IC BWE initialization
    1509             :      *-----------------------------------------------------------------*/
    1510             : 
    1511        2612 :     IF( NE_16( hCPE->element_mode, IVAS_CPE_MDCT ) )
    1512             :     {
    1513          99 :         IF( ( hCPE->hStereoICBWE = (STEREO_ICBWE_ENC_HANDLE) malloc( sizeof( STEREO_ICBWE_ENC_DATA ) ) ) == NULL )
    1514             :         {
    1515           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Stereo ICBWE \n" ) );
    1516             :         }
    1517             : 
    1518          99 :         stereo_icBWE_init_enc_fx( hCPE->hStereoICBWE );
    1519             :     }
    1520             : 
    1521             :     /*-----------------------------------------------------------------*
    1522             :      * TD stereo initialization
    1523             :      *-----------------------------------------------------------------*/
    1524             : 
    1525        2612 :     IF( EQ_16( hCPE->element_mode, IVAS_CPE_TD ) )
    1526             :     {
    1527           0 :         IF( ( hCPE->hStereoTD = (STEREO_TD_ENC_DATA_HANDLE) malloc( sizeof( STEREO_TD_ENC_DATA ) ) ) == NULL )
    1528             :         {
    1529           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for TD Stereo\n" ) );
    1530             :         }
    1531           0 :         stereo_td_init_enc_fx( hCPE->hStereoTD, hCPE->last_element_mode );
    1532             :     }
    1533             : 
    1534             :     /*-----------------------------------------------------------------*
    1535             :      * MDCT stereo initialization
    1536             :      *-----------------------------------------------------------------*/
    1537             : 
    1538        2612 :     test();
    1539        2612 :     IF( EQ_16( hCPE->element_mode, IVAS_CPE_MDCT ) && EQ_16( st_ivas->nCPE, 1 ) )
    1540             :     {
    1541         352 :         IF( ( hCPE->hStereoMdct = (STEREO_MDCT_ENC_DATA_HANDLE) malloc( sizeof( STEREO_MDCT_ENC_DATA ) ) ) == NULL )
    1542             :         {
    1543           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for MDCT Stereo \n" ) );
    1544             :         }
    1545             : 
    1546             : #ifdef DEBUGGING
    1547             :         hCPE->hStereoMdct->mdct_stereo_mode_cmdl = st_ivas->hEncoderConfig->mdct_stereo_mode_cmdl;
    1548             : #ifdef DEBUG_FORCE_MDCT_STEREO_MODE
    1549             :         /*set all other members to defined states */
    1550             :         hCPE->hStereoMdct->fDualMono = 0;
    1551             :         hCPE->hStereoMdct->fMSstereo = 0;
    1552             : 
    1553             :         if ( hCPE->hStereoMdct->mdct_stereo_mode_cmdl == SMDCT_FORCE_LR )
    1554             :         {
    1555             :             hCPE->hStereoMdct->fDualMono = 1;
    1556             :         }
    1557             :         else if ( hCPE->hStereoMdct->mdct_stereo_mode_cmdl == SMDCT_FORCE_MS )
    1558             :         {
    1559             :             hCPE->hStereoMdct->fMSstereo = 1;
    1560             :         }
    1561             : #endif
    1562             : #endif
    1563             : 
    1564         352 :         initMdctStereoEncData_fx( hCPE->hStereoMdct, ivas_format, hCPE->element_mode, hCPE->element_brate, max_bwidth, 0, NULL, 1 );
    1565             : 
    1566         352 :         test();
    1567         352 :         test();
    1568         352 :         hCPE->hStereoMdct->isSBAStereoMode = ( ( EQ_16( ivas_format, SBA_FORMAT ) || EQ_16( ivas_format, SBA_ISM_FORMAT ) ) && EQ_16( st_ivas->nchan_transport, 2 ) );
    1569         352 :         move16();
    1570             : 
    1571         352 :         test();
    1572         352 :         test();
    1573         352 :         IF( EQ_16( hCPE->element_mode, IVAS_CPE_MDCT ) && LE_32( element_brate, MAX_MDCT_ITD_BRATE ) && EQ_16( ivas_format, STEREO_FORMAT ) )
    1574             :         {
    1575          13 :             IF( ( error = initMdctItdHandling_fx( hCPE->hStereoMdct, input_Fs ) ) != IVAS_ERR_OK )
    1576             :             {
    1577           0 :                 return error;
    1578             :             }
    1579             :         }
    1580             :     }
    1581             : 
    1582        2612 :     st_ivas->hCPE[cpe_id] = hCPE;
    1583             : 
    1584        2612 :     return error;
    1585             : }
    1586             : /*-------------------------------------------------------------------------
    1587             :  * destroy_cpe_enc()
    1588             :  *
    1589             :  * Destroy and deallocate IVAS encoder CPE handle
    1590             :  *-------------------------------------------------------------------------*/
    1591             : 
    1592        2612 : void destroy_cpe_enc(
    1593             :     CPE_ENC_HANDLE hCPE /* i/o: CPE encoder structure       */
    1594             : )
    1595             : {
    1596             :     Word16 n;
    1597             :     Encoder_State *st;
    1598             : 
    1599        2612 :     IF( hCPE->hStereoClassif != NULL )
    1600             :     {
    1601        2612 :         free( hCPE->hStereoClassif );
    1602        2612 :         hCPE->hStereoClassif = NULL;
    1603             :     }
    1604             : 
    1605        7836 :     FOR( n = 0; n < CPE_CHANNELS; n++ )
    1606             :     {
    1607        5224 :         IF( hCPE->input_mem_fx[n] != NULL )
    1608             :         {
    1609         358 :             free( hCPE->input_mem_fx[n] );
    1610         358 :             hCPE->input_mem_fx[n] = NULL;
    1611             :         }
    1612             :     }
    1613             : 
    1614        2612 :     ivas_destroy_MD_bstr_enc_fx( &( hCPE->hMetaData ) );
    1615             : 
    1616        7836 :     FOR( n = 0; n < CPE_CHANNELS; n++ )
    1617             :     {
    1618        5224 :         st = hCPE->hCoreCoder[n];
    1619             : 
    1620        5224 :         IF( st != NULL )
    1621             :         {
    1622        5224 :             destroy_core_enc_fx( st );
    1623        5224 :             st = NULL;
    1624             :         }
    1625             :     }
    1626             : 
    1627        2612 :     IF( hCPE->hStereoDft != NULL )
    1628             :     {
    1629          81 :         stereo_dft_enc_destroy_fx( &hCPE->hStereoDft );
    1630          81 :         hCPE->hStereoDft = NULL;
    1631             :     }
    1632             : 
    1633        2612 :     IF( hCPE->hStereoTD != NULL )
    1634             :     {
    1635           1 :         free( hCPE->hStereoTD );
    1636           1 :         hCPE->hStereoTD = NULL;
    1637             :     }
    1638             : 
    1639        2612 :     IF( hCPE->hStereoMdct != NULL )
    1640             :     {
    1641         373 :         stereo_mdct_enc_destroy_fx( &hCPE->hStereoMdct );
    1642         373 :         hCPE->hStereoMdct = NULL;
    1643             :     }
    1644             : 
    1645        2612 :     IF( hCPE->hStereoTCA != NULL )
    1646             :     {
    1647          82 :         free( hCPE->hStereoTCA );
    1648          82 :         hCPE->hStereoTCA = NULL;
    1649             :     }
    1650             : 
    1651        2612 :     IF( hCPE->hStereoICBWE != NULL )
    1652             :     {
    1653          82 :         free( hCPE->hStereoICBWE );
    1654          82 :         hCPE->hStereoICBWE = NULL;
    1655             :     }
    1656             : 
    1657        2612 :     IF( hCPE->hStereoCng != NULL )
    1658             :     {
    1659          57 :         free( hCPE->hStereoCng );
    1660          57 :         hCPE->hStereoCng = NULL;
    1661             :     }
    1662             : 
    1663        2612 :     IF( hCPE->hFrontVad[0] != NULL )
    1664             :     {
    1665          84 :         FOR( n = 0; n < CPE_CHANNELS; n++ )
    1666             :         {
    1667          56 :             front_vad_destroy_fx( &hCPE->hFrontVad[n] );
    1668          56 :             hCPE->hFrontVad[n] = NULL;
    1669             :         }
    1670             :     }
    1671             : 
    1672        2612 :     free( hCPE );
    1673             : 
    1674        2612 :     return;
    1675             : }
    1676             : 
    1677             : 
    1678             : /*-------------------------------------------------------------------------
    1679             :  * stereo_mode_combined_format_enc()
    1680             :  *
    1681             :  * Set stereo format in a combined format
    1682             :  *-------------------------------------------------------------------------*/
    1683             : 
    1684      410255 : static void stereo_mode_combined_format_enc_fx(
    1685             :     const Encoder_Struct *st_ivas, /* i  : encoder main structure */
    1686             :     CPE_ENC_HANDLE hCPE            /* i/o: CPE handle             */
    1687             : )
    1688             : {
    1689             :     ENCODER_CONFIG_HANDLE hEncoderConfig;
    1690             :     Word32 element_brate_ref;
    1691             : 
    1692      410255 :     hEncoderConfig = st_ivas->hEncoderConfig;
    1693             : 
    1694      410255 :     IF( EQ_16( hEncoderConfig->ivas_format, MASA_ISM_FORMAT ) )
    1695             :     {
    1696        9000 :         element_brate_ref = hCPE->element_brate; /* Q0 */
    1697        9000 :         move32();
    1698             : 
    1699        9000 :         test();
    1700        9000 :         test();
    1701        9000 :         test();
    1702        9000 :         test();
    1703        9000 :         IF( EQ_16( st_ivas->ism_mode, ISM_MASA_MODE_DISC ) &&
    1704             :             ( ( EQ_16( hEncoderConfig->nchan_ism, 3 ) && EQ_32( hEncoderConfig->ivas_total_brate, IVAS_96k ) ) ||
    1705             :               ( EQ_16( hEncoderConfig->nchan_ism, 4 ) && EQ_32( hEncoderConfig->ivas_total_brate, IVAS_128k ) ) ) )
    1706             :         {
    1707         426 :             IF( GT_32( L_add( hCPE->element_brate, hCPE->brate_surplus ), IVAS_64k ) )
    1708             :             {
    1709           0 :                 st_ivas->hMasa->data.hOmasaData->omasa_stereo_sw_cnt = 0;
    1710           0 :                 move16();
    1711             :             }
    1712             :             ELSE
    1713             :             {
    1714         426 :                 st_ivas->hMasa->data.hOmasaData->omasa_stereo_sw_cnt = add( st_ivas->hMasa->data.hOmasaData->omasa_stereo_sw_cnt, 1 ); /* Q0 */
    1715         426 :                 move16();
    1716         426 :                 st_ivas->hMasa->data.hOmasaData->omasa_stereo_sw_cnt = s_min( st_ivas->hMasa->data.hOmasaData->omasa_stereo_sw_cnt, OMASA_STEREO_SW_CNT_MAX ); /* Q0 */
    1717         426 :                 move16();
    1718             :             }
    1719             : 
    1720         426 :             IF( LT_16( st_ivas->hMasa->data.hOmasaData->omasa_stereo_sw_cnt, OMASA_STEREO_SW_CNT_MAX ) )
    1721             :             {
    1722           0 :                 hCPE->element_mode = IVAS_CPE_MDCT;
    1723           0 :                 move16();
    1724           0 :                 hCPE->element_brate = IVAS_64k;
    1725           0 :                 move32();
    1726           0 :                 hCPE->brate_surplus = L_sub( hCPE->brate_surplus, L_sub( hCPE->element_brate, element_brate_ref ) ); /* Q0 */
    1727           0 :                 move32();
    1728             :             }
    1729             : 
    1730             :             /* write OMASA stereo mode signalling */
    1731         426 :             IF( EQ_16( hCPE->element_mode, IVAS_CPE_MDCT ) )
    1732             :             {
    1733           0 :                 push_indice( hCPE->hCoreCoder[0]->hBstr, IND_SMODE_OMASA, 1, NBITS_ELEMENT_MODE );
    1734             :             }
    1735             :             ELSE
    1736             :             {
    1737         426 :                 push_indice( hCPE->hCoreCoder[0]->hBstr, IND_SMODE_OMASA, 0, NBITS_ELEMENT_MODE );
    1738             :             }
    1739             :         }
    1740             :     }
    1741      410255 :     return;
    1742             : }

Generated by: LCOV version 1.14