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

Generated by: LCOV version 1.14