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

Generated by: LCOV version 1.14