LCOV - code coverage report
Current view: top level - lib_dec - ivas_cpe_dec_fx.c (source / functions) Hit Total Coverage
Test: Coverage on main -- dec/rend @ 633e3f2e309758d10805ef21e0436356fe719b7a Lines: 644 687 93.7 %
Date: 2025-08-23 01:22:27 Functions: 5 5 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 "ivas_prot_fx.h"
      41             : #include "ivas_rom_com.h"
      42             : #include "wmc_auto.h"
      43             : #include <math.h>
      44             : 
      45             : 
      46             : /*--------------------------------------------------------------------------*
      47             :  * Local function prototypes
      48             :  *--------------------------------------------------------------------------*/
      49             : 
      50             : static void read_stereo_mode_and_bwidth_fx( CPE_DEC_HANDLE hCPE, const Decoder_Struct *st_ivas );
      51             : 
      52             : static void stereo_mode_combined_format_dec_fx( const Decoder_Struct *st_ivas, CPE_DEC_HANDLE hCPE );
      53             : 
      54             : 
      55             : /*--------------------------------------------------------------------------*
      56             :  * ivas_cpe_dec_fx()
      57             :  *
      58             :  * Channel Pair Element (CPE) decoding routine
      59             :  *--------------------------------------------------------------------------*/
      60             : 
      61      410722 : ivas_error ivas_cpe_dec_fx(
      62             :     Decoder_Struct *st_ivas,      /* i/o: IVAS decoder structure          */
      63             :     const Word16 cpe_id,          /* i  : CPE # identifier                */
      64             :     Word32 *output[CPE_CHANNELS], /* o  : output synthesis signal         Q11*/
      65             :     const Word16 output_frame,    /* i  : output frame length per channel */
      66             :     const Word16 nb_bits_metadata /* i  : number of metadata bits         */
      67             : )
      68             : {
      69             :     Word16 i, n, n_channels;
      70             :     Word16 nb_bits, last_core;
      71             :     Word16 last_bwidth;
      72             :     Word16 tdm_ratio_idx;
      73             :     Word32 outputHB_fx[CPE_CHANNELS][L_FRAME48k]; /* buffer for output HB synthesis, both channels */ /* Q11 */
      74             :     Word16 q_res_buf;
      75             :     Word32 res_buf_fx[STEREO_DFT_N_8k]; /* Q(q_res_buf) */
      76             :     CPE_DEC_HANDLE hCPE;
      77             :     Decoder_State **sts;
      78             :     Word32 ivas_total_brate;
      79             :     ivas_error error;
      80             :     Word32 cpe_brate;
      81             :     Word32 element_brate_ref;
      82             :     Word32 quo, rem;
      83             : 
      84      410722 :     error = IVAS_ERR_OK;
      85      410722 :     move32();
      86      410722 :     q_res_buf = Q8;
      87      410722 :     move16();
      88             : 
      89      410722 :     push_wmops( "ivas_cpe_dec" );
      90             : 
      91      410722 :     ivas_total_brate = st_ivas->hDecoderConfig->ivas_total_brate;
      92      410722 :     move32();
      93             : 
      94      410722 :     hCPE = st_ivas->hCPE[cpe_id];
      95      410722 :     sts = hCPE->hCoreCoder;
      96             : 
      97      410722 :     last_core = sts[0]->last_core;
      98      410722 :     move16();
      99      410722 :     last_bwidth = sts[0]->last_bwidth;
     100      410722 :     move16();
     101             : 
     102      410722 :     sts[0]->BER_detect = s_or( sts[0]->BER_detect, st_ivas->BER_detect );
     103      410722 :     sts[1]->BER_detect = s_or( sts[1]->BER_detect, st_ivas->BER_detect );
     104      410722 :     move16();
     105      410722 :     move16();
     106             : 
     107      410722 :     element_brate_ref = hCPE->element_brate;
     108      410722 :     move32();
     109             : 
     110             :     /*------------------------------------------------------------------*
     111             :      * Read stereo technology info & audio bandwidth
     112             :      *-----------------------------------------------------------------*/
     113             : 
     114      410722 :     stereo_mode_combined_format_dec_fx( st_ivas, hCPE );
     115             : 
     116      410722 :     read_stereo_mode_and_bwidth_fx( hCPE, st_ivas );
     117             : 
     118             :     /*----------------------------------------------------------------*
     119             :      * dynamically allocate data structures depending on the actual stereo mode
     120             :      *----------------------------------------------------------------*/
     121             : 
     122     1232166 :     FOR( Word16 ind1 = 0; ind1 < 2; ind1++ )
     123             :     {
     124      821444 :         test();
     125      821444 :         IF( hCPE->hCoreCoder[ind1] && hCPE->hCoreCoder[ind1]->hHQ_core )
     126             :         {
     127      758049 :             Copy_Scale_sig_16_32_no_sat( hCPE->hCoreCoder[ind1]->hHQ_core->old_out_fx, hCPE->hCoreCoder[ind1]->hHQ_core->old_out_fx32, L_FRAME48k, sub( Q11, hCPE->hCoreCoder[ind1]->hHQ_core->Q_old_wtda ) ); // Q11
     128             :         }
     129             :     }
     130     1232166 :     FOR( Word16 ind1 = 0; ind1 < 2; ind1++ )
     131             :     {
     132      821444 :         IF( hCPE->hCoreCoder[ind1]->hHQ_core )
     133             :         {
     134      758049 :             Copy_Scale_sig_16_32_no_sat( hCPE->hCoreCoder[ind1]->hHQ_core->old_out_LB_fx, hCPE->hCoreCoder[ind1]->hHQ_core->old_out_LB_fx32, L_FRAME32k, sub( Q11, hCPE->hCoreCoder[ind1]->hHQ_core->Q_old_wtda_LB ) ); // Q11
     135      758049 :             hCPE->hCoreCoder[ind1]->hHQ_core->Q_old_out_fx32 = Q11;
     136      758049 :             move16();
     137             :         }
     138             :     }
     139             : 
     140      410722 :     IF( NE_32( ( error = stereo_memory_dec_fx( ivas_total_brate, hCPE, nb_bits_metadata, st_ivas->hDecoderConfig->output_Fs, st_ivas->ivas_format, st_ivas->mc_mode, st_ivas->nchan_transport ) ), IVAS_ERR_OK ) )
     141             :     {
     142           0 :         return error;
     143             :     }
     144             : 
     145     1232166 :     FOR( Word16 ind1 = 0; ind1 < 2; ind1++ )
     146             :     {
     147      821444 :         test();
     148      821444 :         IF( hCPE->hCoreCoder[ind1] && hCPE->hCoreCoder[ind1]->hHQ_core )
     149             :         {
     150      758066 :             Copy_Scale_sig_32_16( hCPE->hCoreCoder[ind1]->hHQ_core->old_out_fx32, hCPE->hCoreCoder[ind1]->hHQ_core->old_out_fx, L_FRAME48k, sub( hCPE->hCoreCoder[ind1]->hHQ_core->Q_old_wtda, Q11 ) ); // Q_old_wtda
     151             :         }
     152             :     }
     153     1232166 :     FOR( Word16 ind1 = 0; ind1 < 2; ind1++ )
     154             :     {
     155      821444 :         test();
     156      821444 :         IF( hCPE->hCoreCoder[ind1] && hCPE->hCoreCoder[ind1]->hHQ_core )
     157             :         {
     158      758066 :             Copy_Scale_sig_32_16( hCPE->hCoreCoder[ind1]->hHQ_core->old_out_LB_fx32, hCPE->hCoreCoder[ind1]->hHQ_core->old_out_LB_fx, L_FRAME32k, sub( hCPE->hCoreCoder[ind1]->hHQ_core->Q_old_wtda_LB, Q11 ) ); // Q_old_wtda_LB
     159             :         }
     160             :     }
     161             : 
     162             :     /*------------------------------------------------------------------*
     163             :      * Initialization
     164             :      *-----------------------------------------------------------------*/
     165             : 
     166      410722 :     n_channels = CPE_CHANNELS;
     167      410722 :     move16();
     168      410722 :     if ( EQ_16( hCPE->element_mode, IVAS_CPE_DFT ) )
     169             :     {
     170       59692 :         n_channels = 1; /* in DFT stereo, only M channel is coded */
     171       59692 :         move16();
     172             :     }
     173             : 
     174      410722 :     tdm_ratio_idx = LRTD_STEREO_RIGHT_IS_PRIM;
     175      410722 :     move16();
     176             : 
     177     1172474 :     FOR( n = 0; n < n_channels; n++ )
     178             :     {
     179      761752 :         sts[n]->idchan = n;
     180      761752 :         move16();
     181      761752 :         sts[n]->element_mode = hCPE->element_mode;
     182      761752 :         move16();
     183             : 
     184      761752 :         if ( !st_ivas->bfi )
     185             :         {
     186      749344 :             sts[n]->tdm_LRTD_flag = 0;
     187      749344 :             move16();
     188             :         }
     189             : 
     190             :         /* TD stereo parameters */
     191      761752 :         IF( hCPE->hStereoTD != NULL )
     192             :         {
     193        7372 :             hCPE->hStereoTD->tdm_lp_reuse_flag = 0;
     194        7372 :             move16();
     195        7372 :             hCPE->hStereoTD->tdm_low_rate_mode = 0;
     196        7372 :             move16();
     197        7372 :             hCPE->hStereoTD->tdm_Pitch_reuse_flag = 0;
     198        7372 :             move16();
     199             :         }
     200             :     }
     201             : 
     202             :     /*----------------------------------------------------------------*
     203             :      * Resets/updates in case of stereo switching
     204             :      *----------------------------------------------------------------*/
     205             : 
     206      410722 :     stereo_switching_dec( hCPE, ivas_total_brate );
     207             : 
     208             :     /*----------------------------------------------------------------*
     209             :      * Configuration of stereo decoder
     210             :      *----------------------------------------------------------------*/
     211             : 
     212             :     /* Force to MODE1 in IVAS */
     213     1172474 :     FOR( n = 0; n < n_channels; n++ )
     214             :     {
     215      761752 :         sts[n]->codec_mode = MODE1;
     216      761752 :         move16();
     217             :     }
     218             : 
     219      410722 :     test();
     220      410722 :     test();
     221      410722 :     test();
     222      410722 :     test();
     223      410722 :     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 || ( NE_32( ivas_total_brate, st_ivas->hDecoderConfig->last_ivas_total_brate ) ) ) )
     224             :     {
     225             :         STEREO_DFT_CONFIG_DATA_HANDLE hConfig;
     226        4574 :         IF( hCPE->hStereoDft == NULL )
     227             :         {
     228          58 :             hConfig = NULL;
     229             :         }
     230             :         ELSE
     231             :         {
     232        4516 :             hConfig = hCPE->hStereoDft->hConfig;
     233             :         }
     234        4574 :         test();
     235        4574 :         IF( st_ivas->hQMetaData != NULL && GT_32( ivas_total_brate, IVAS_SID_5k2 ) )
     236             :         {
     237        1138 :             IF( EQ_32( st_ivas->ivas_format, MASA_ISM_FORMAT ) )
     238             :             {
     239         461 :                 stereo_dft_config_fx( hConfig, L_mult0( st_ivas->hQMetaData->bits_frame_nominal, 35 /* 0.7f * FRAMES_PER_SEC */ ), &sts[0]->bits_frame_nominal, &sts[1]->bits_frame_nominal );
     240             :             }
     241             :             ELSE
     242             :             {
     243         677 :                 stereo_dft_config_fx( hConfig, L_mult0( st_ivas->hQMetaData->bits_frame_nominal, FRAMES_PER_SEC ), &sts[0]->bits_frame_nominal, &sts[1]->bits_frame_nominal );
     244             :             }
     245             :         }
     246             :         ELSE
     247             :         {
     248             :             /* Note: This only works for stereo operation. If DTX would be applied for multiple CPEs a different bitrate signaling is needed */
     249        3436 :             IF( LE_32( ivas_total_brate, IVAS_SID_5k2 ) )
     250             :             {
     251        2859 :                 stereo_dft_config_fx( hConfig, ivas_total_brate, &sts[0]->bits_frame_nominal, &sts[1]->bits_frame_nominal );
     252             :             }
     253             :             ELSE
     254             :             {
     255         577 :                 stereo_dft_config_fx( hConfig, hCPE->element_brate, &sts[0]->bits_frame_nominal, &sts[1]->bits_frame_nominal );
     256             :             }
     257             :         }
     258             :     }
     259             : 
     260      410722 :     IF( EQ_16( hCPE->element_mode, IVAS_CPE_TD ) )
     261             :     {
     262        3686 :         IF( hCPE->hStereoTD->tdm_LRTD_flag )
     263             :         {
     264        3605 :             iDiv_and_mod_32( L_shr( hCPE->element_brate, 1 ), FRAMES_PER_SEC, &quo, &rem, 0 );
     265        3605 :             sts[0]->bits_frame_nominal = extract_l( quo );
     266        3605 :             sts[1]->bits_frame_nominal = extract_l( quo );
     267        3605 :             move16();
     268        3605 :             move16();
     269             :         }
     270             :         ELSE
     271             :         {
     272          81 :             stereo_dft_config_fx( NULL, hCPE->element_brate, &sts[0]->bits_frame_nominal, &sts[1]->bits_frame_nominal );
     273             :         }
     274             :     }
     275             : 
     276             :     /*----------------------------------------------------------------*
     277             :      * Set bitrates per channel
     278             :      * Set bitstream buffers per channel
     279             :      *----------------------------------------------------------------*/
     280             : 
     281      410722 :     IF( EQ_16( hCPE->element_mode, IVAS_CPE_DFT ) )
     282             :     {
     283       59692 :         IF( !st_ivas->bfi )
     284             :         {
     285             :             /* Update DFT Stereo memories */
     286       58804 :             stereo_dft_dec_update_fx( hCPE->hStereoDft, output_frame, 0 );
     287             : 
     288       58804 :             test();
     289       58804 :             IF( EQ_32( st_ivas->ivas_format, MASA_FORMAT ) && LE_32( ivas_total_brate, IVAS_SID_5k2 ) )
     290             :             {
     291         530 :                 IF( ivas_total_brate == FRAME_NO_DATA )
     292             :                 {
     293         456 :                     hCPE->hCoreCoder[n]->core_brate = ivas_total_brate;
     294         456 :                     move32();
     295         456 :                     hCPE->hCoreCoder[0]->total_brate = ivas_total_brate;
     296         456 :                     move32();
     297             :                 }
     298             :                 ELSE
     299             :                 {
     300          74 :                     hCPE->hCoreCoder[n]->core_brate = SID_2k40;
     301          74 :                     move32();
     302             :                 }
     303             :             }
     304             : 
     305             :             /* read DFT Stereo side info */
     306       58804 :             nb_bits = extract_h( L_msu( Mpy_32_16_1( L_shl( hCPE->element_brate, 15 ), 1311 /* 1/FRAMES_PER_SEC in Q16*/ ), 26214, sts[0]->bits_frame_nominal ) );
     307       58804 :             cpe_brate = st_ivas->hCPE[0]->element_brate;
     308       58804 :             move32();
     309       58804 :             IF( EQ_32( st_ivas->ivas_format, MASA_ISM_FORMAT ) )
     310             :             {
     311        2683 :                 iDiv_and_mod_32( cpe_brate, FRAMES_PER_SEC, &quo, &rem, 0 );
     312        2683 :                 sts[1]->bit_stream = sts[0]->bit_stream + sub( sub( extract_l( quo ), 1 ), nb_bits_metadata );
     313        2683 :                 IF( hCPE->brate_surplus < 0 )
     314             :                 {
     315        2201 :                     iDiv_and_mod_32( L_abs( hCPE->brate_surplus ), FRAMES_PER_SEC, &quo, &rem, 0 );
     316        2201 :                     quo = L_negate( quo );
     317             :                 }
     318             :                 ELSE
     319             :                 {
     320         482 :                     iDiv_and_mod_32( hCPE->brate_surplus, FRAMES_PER_SEC, &quo, &rem, 0 );
     321             :                 }
     322        2683 :                 sts[1]->bit_stream = sts[1]->bit_stream + extract_l( quo );
     323             :             }
     324             :             ELSE
     325             :             {
     326       56121 :                 iDiv_and_mod_32( ivas_total_brate, FRAMES_PER_SEC, &quo, &rem, 0 );
     327       56121 :                 sts[1]->bit_stream = sts[0]->bit_stream + sub( sub( extract_l( quo ), 1 ), nb_bits_metadata );
     328             :             }
     329             : 
     330       58804 :             IF( EQ_32( ivas_total_brate, IVAS_SID_5k2 ) )
     331             :             {
     332        1492 :                 nb_bits = sub( nb_bits, SID_FORMAT_NBITS );
     333        1492 :                 sts[1]->bit_stream = sts[1]->bit_stream - SID_FORMAT_NBITS;
     334             :                 /* set total bitrate of Stereo CNG parameters for BER detection */
     335        1492 :                 sts[1]->total_brate = IVAS_SID_5k2 - SID_2k40;
     336        1492 :                 move32();
     337             :             }
     338             : 
     339       58804 :             test();
     340       58804 :             test();
     341       58804 :             test();
     342       58804 :             test();
     343       58804 :             IF( ( ( EQ_32( st_ivas->ivas_format, MASA_FORMAT ) && LT_32( ivas_total_brate, MASA_STEREO_MIN_BITRATE ) ) || ( EQ_32( st_ivas->ivas_format, MASA_ISM_FORMAT ) && LT_32( cpe_brate, MASA_STEREO_MIN_BITRATE ) ) ) && GT_32( ivas_total_brate, IVAS_SID_5k2 ) )
     344             :             {
     345        5470 :                 sts[0]->total_brate = hCPE->element_brate; /* Only mono downmix was transmitted in this case */
     346        5470 :                 move32();
     347             :             }
     348             :             ELSE
     349             :             {
     350       53334 :                 test();
     351       53334 :                 IF( EQ_32( st_ivas->ivas_format, MASA_FORMAT ) || EQ_32( st_ivas->ivas_format, MASA_ISM_FORMAT ) )
     352             :                 {
     353        6197 :                     nb_bits = sub( nb_bits, nb_bits_metadata );
     354        6197 :                     IF( hCPE->brate_surplus < 0 )
     355             :                     {
     356         930 :                         iDiv_and_mod_32( L_abs( hCPE->brate_surplus ), FRAMES_PER_SEC, &quo, &rem, 0 );
     357         930 :                         quo = L_negate( quo );
     358         930 :                         nb_bits = add( nb_bits, extract_l( quo ) );
     359             :                     }
     360             :                 }
     361             : 
     362       53334 :                 stereo_dft_dec_read_BS_fx( ivas_total_brate, hCPE->element_brate, &sts[0]->total_brate, sts[1], hCPE->hStereoDft, sts[0]->bwidth, output_frame, res_buf_fx, &nb_bits, hCPE->hStereoCng->coh_fx, st_ivas->ivas_format );
     363             :             }
     364             : 
     365             :             /* subtract metadata bitbudget */
     366       58804 :             sts[0]->total_brate = L_sub( sts[0]->total_brate, L_mult0( nb_bits_metadata, FRAMES_PER_SEC ) ); // Q0
     367       58804 :             move32();
     368             : 
     369             :             /* subtract bit-rate for combined format coding */
     370       58804 :             test();
     371       58804 :             test();
     372       58804 :             test();
     373       58804 :             IF( EQ_32( st_ivas->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 ) ) )
     374             :             {
     375        2683 :                 sts[0]->total_brate = L_add( sts[0]->total_brate, hCPE->brate_surplus );
     376        2683 :                 move32();
     377             :             }
     378             :         }
     379             :         ELSE
     380             :         {
     381         888 :             hCPE->hStereoDft->sg_mem_corrupt = 1;
     382         888 :             move16();
     383             :         }
     384             :     }
     385      351030 :     ELSE IF( EQ_16( hCPE->element_mode, IVAS_CPE_TD ) )
     386             :     {
     387             :         /* signal bitrate for BW selection in the SCh */
     388        3686 :         sts[0]->bits_frame_channel = 0;
     389        3686 :         move16();
     390        3686 :         iDiv_and_mod_32( hCPE->element_brate, FRAMES_PER_SEC, &quo, &rem, 0 );
     391        3686 :         sts[1]->bits_frame_channel = extract_l( quo );
     392        3686 :         move16();
     393        3686 :         IF( hCPE->brate_surplus < 0 )
     394             :         {
     395           0 :             iDiv_and_mod_32( L_abs( hCPE->brate_surplus ), FRAMES_PER_SEC, &quo, &rem, 0 );
     396           0 :             quo = L_negate( quo );
     397             :         }
     398             :         ELSE
     399             :         {
     400        3686 :             iDiv_and_mod_32( hCPE->brate_surplus, FRAMES_PER_SEC, &quo, &rem, 0 );
     401             :         }
     402        3686 :         sts[1]->bits_frame_channel = add( sts[1]->bits_frame_channel, extract_l( quo ) );
     403        3686 :         move16();
     404        3686 :         IF( st_ivas->hQMetaData != NULL )
     405             :         {
     406          60 :             sts[1]->bits_frame_channel = sub( sts[1]->bits_frame_channel, st_ivas->hQMetaData->metadata_max_bits );
     407          60 :             move16();
     408             :         }
     409             :     }
     410      347344 :     ELSE IF( EQ_16( hCPE->element_mode, IVAS_CPE_MDCT ) )
     411             :     {
     412             :         /* compute bit-rate surplus per channel in combined format coding */
     413             :         Word32 brate_surplus[CPE_CHANNELS];
     414      347344 :         test();
     415      347344 :         test();
     416      347344 :         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 ) )
     417             :         {
     418        3794 :             IF( hCPE->brate_surplus < 0 )
     419             :             {
     420        3669 :                 iDiv_and_mod_32( L_abs( hCPE->brate_surplus ), FRAMES_PER_SEC, &quo, &rem, 0 );
     421        3669 :                 quo = L_negate( quo );
     422             :             }
     423             :             ELSE
     424             :             {
     425         125 :                 iDiv_and_mod_32( hCPE->brate_surplus, FRAMES_PER_SEC, &quo, &rem, 0 );
     426             :             }
     427        3794 :             brate_surplus[0] = L_shr( L_mult( extract_l( L_shr( quo, 1 ) ), FRAMES_PER_SEC ), 1 );
     428        3794 :             move32();
     429        3794 :             brate_surplus[1] = L_sub( hCPE->brate_surplus, brate_surplus[0] );
     430        3794 :             move32();
     431             :         }
     432             : 
     433      347344 :         test();
     434      347344 :         test();
     435      347344 :         IF( EQ_16( is_DTXrate( ivas_total_brate ), 1 ) && ( sts[0]->first_CNG == 0 || sts[1]->first_CNG == 0 ) )
     436             :         {
     437          30 :             IF( NE_32( ( error = initMdctStereoDtxData_fx( hCPE ) ), IVAS_ERR_OK ) )
     438             :             {
     439           0 :                 return error;
     440             :             }
     441             :         }
     442             : 
     443             :         /* this is just for initialization, the true values of "total_brate" and "bits_frame_channel" are set later */
     444     1042032 :         FOR( n = 0; n < n_channels; n++ )
     445             :         {
     446      694688 :             IF( EQ_32( ivas_total_brate, IVAS_SID_5k2 ) )
     447             :             {
     448        1154 :                 sts[n]->total_brate = SID_2k40;
     449        1154 :                 move32();
     450        1154 :                 sts[1]->bit_stream = sts[0]->bit_stream + SID_2k40 / FRAMES_PER_SEC;
     451             :             }
     452             :             ELSE
     453             :             {
     454             :                 /*total bitrate must be set to the element bitrate to avoid false BER IF bits read are larger than half the bitrate*/
     455      693534 :                 sts[n]->total_brate = hCPE->element_brate;
     456      693534 :                 move32();
     457             :             }
     458      694688 :             iDiv_and_mod_32( sts[n]->total_brate, FRAMES_PER_SEC, &quo, &rem, 0 );
     459      694688 :             sts[n]->bits_frame_nominal = extract_l( quo );
     460      694688 :             iDiv_and_mod_32( hCPE->element_brate, FRAMES_PER_SEC, &quo, &rem, 0 );
     461      694688 :             sts[n]->bits_frame_channel = extract_l( L_shr( quo, sub( n_channels, 1 ) ) );
     462      694688 :             move16();
     463      694688 :             move16();
     464             : 
     465             :             /* subtract bit-rate for combined format coding */
     466      694688 :             test();
     467      694688 :             test();
     468      694688 :             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 ) )
     469             :             {
     470        7588 :                 IF( brate_surplus[n] < 0 )
     471             :                 {
     472        7338 :                     iDiv_and_mod_32( L_abs( brate_surplus[n] ), FRAMES_PER_SEC, &quo, &rem, 0 );
     473        7338 :                     quo = L_negate( quo );
     474             :                 }
     475             :                 ELSE
     476             :                 {
     477         250 :                     iDiv_and_mod_32( brate_surplus[n], FRAMES_PER_SEC, &quo, &rem, 0 );
     478             :                 }
     479        7588 :                 sts[n]->bits_frame_channel = add( sts[n]->bits_frame_channel, extract_l( quo ) );
     480        7588 :                 sts[n]->total_brate = L_add( sts[n]->total_brate, brate_surplus[n] );
     481        7588 :                 move16();
     482        7588 :                 move32();
     483             :             }
     484             :         }
     485             : 
     486      347344 :         IF( !st_ivas->hMCT )
     487             :         {
     488       85900 :             test();
     489       85900 :             IF( EQ_32( st_ivas->ivas_format, SBA_FORMAT ) && EQ_32( ivas_total_brate, IVAS_SID_5k2 ) )
     490             :             {
     491         501 :                 FOR( n = 0; n < n_channels; n++ )
     492             :                 {
     493         334 :                     sts[n]->bits_frame_channel = sub( sts[n]->bits_frame_channel, shr( nb_bits_metadata, sub( n_channels, 1 ) ) );
     494         334 :                     move16();
     495             :                 }
     496             :             }
     497             :             ELSE
     498             :             {
     499             :                 /* subtract metadata bitbudget */
     500       85733 :                 sts[0]->bits_frame_channel = sub( sts[0]->bits_frame_channel, nb_bits_metadata );
     501       85733 :                 move16();
     502             :             }
     503             :         }
     504             :     }
     505             : 
     506             :     /*----------------------------------------------------------------*
     507             :      * Core codec configuration
     508             :      *----------------------------------------------------------------*/
     509             : 
     510     1172474 :     FOR( n = 0; n < n_channels; n++ )
     511             :     {
     512             :         /* set ACELP12k8 / ACELP16k flag for flexible ACELP core */
     513      761752 :         IF( hCPE->hStereoTD != NULL )
     514             :         {
     515        7372 :             sts[n]->flag_ACELP16k = set_ACELP_flag_IVAS( hCPE->element_mode, hCPE->element_brate, sts[n]->total_brate, n, hCPE->hStereoTD->tdm_LRTD_flag, sts[n]->bwidth, sts[n]->cng_type );
     516        7372 :             move16();
     517             :         }
     518             :         ELSE
     519             :         {
     520      754380 :             sts[n]->flag_ACELP16k = set_ACELP_flag_IVAS( hCPE->element_mode, hCPE->element_brate, sts[n]->total_brate, n, 0, sts[n]->bwidth, sts[n]->cng_type );
     521      754380 :             move16();
     522             :         }
     523             :     }
     524             : 
     525     1172474 :     FOR( n = 0; n < n_channels; n++ )
     526             :     {
     527             :         /* set VAD flag */
     528      761752 :         IF( EQ_16( is_DTXrate( ivas_total_brate ), 1 ) )
     529             :         {
     530       18695 :             sts[n]->VAD = 0;
     531       18695 :             move16();
     532       18695 :             sts[n]->active_cnt = 0;
     533       18695 :             move16();
     534       18695 :             IF( sts[1] != NULL )
     535             :             {
     536       18695 :                 sts[1]->active_cnt = 0;
     537       18695 :                 move16();
     538             : #ifdef NONBE_1325_TD_STEREO_QUANT_LSF_SEC
     539       18695 :                 if ( sts[1]->ini_frame == 0 )
     540             :                 {
     541       10082 :                     sts[1]->active_cnt = CNG_TYPE_HO;
     542       10082 :                     move16();
     543             :                 }
     544             : #endif
     545             :             }
     546             :         }
     547             :         ELSE
     548             :         {
     549      743057 :             sts[n]->VAD = 1;
     550      743057 :             move16();
     551      743057 :             sts[n]->active_cnt = add( sts[n]->active_cnt, 1 );
     552             : #ifdef NONBE_1325_TD_STEREO_QUANT_LSF_SEC
     553      743057 :             sts[n]->active_cnt = s_min( sts[n]->active_cnt, 200 );
     554             : #else
     555             :             sts[n]->active_cnt = s_min( sts[n]->active_cnt, 100 );
     556             : #endif
     557      743057 :             move16();
     558      743057 :             move16();
     559             :         }
     560             : 
     561             :         /* set CNA flag */
     562      761752 :         test();
     563      761752 :         test();
     564      761752 :         IF( ( EQ_16( hCPE->element_mode, IVAS_CPE_DFT ) && LE_32( hCPE->element_brate, CNA_MAX_BRATE_DFT_STEREO ) ) || LE_32( hCPE->element_brate, CNA_MAX_BRATE_STEREO ) )
     565             :         {
     566       68800 :             sts[n]->flag_cna = 1;
     567       68800 :             move16();
     568             :         }
     569             :         ELSE
     570             :         {
     571      692952 :             sts[n]->flag_cna = 0;
     572      692952 :             move16();
     573             :         }
     574             :     }
     575             : 
     576             :     /* configure TD stereo decoder */
     577      410722 :     IF( EQ_16( hCPE->element_mode, IVAS_CPE_TD ) )
     578             :     {
     579        3686 :         IF( !st_ivas->bfi )
     580             :         {
     581        3630 :             tdm_configure_dec_fx( st_ivas->ivas_format, st_ivas->ism_mode, hCPE, &tdm_ratio_idx, nb_bits_metadata );
     582             : 
     583        3630 :             iDiv_and_mod_32( sts[0]->total_brate, FRAMES_PER_SEC, &quo, &rem, 0 );
     584        3630 :             sts[1]->bit_stream = sts[0]->bit_stream + extract_l( quo );
     585             :         }
     586             :         ELSE
     587             :         {
     588          56 :             sts[1]->coder_type = sts[1]->last_coder_type;
     589          56 :             move16();
     590          56 :             tdm_ratio_idx = hCPE->hStereoTD->tdm_last_ratio_idx;
     591          56 :             move16();
     592             :         }
     593             :     }
     594             : 
     595             :     /*----------------------------------------------------------------*
     596             :      * Core Decoder
     597             :      *----------------------------------------------------------------*/
     598             : 
     599      410722 :     test();
     600      410722 :     test();
     601      410722 :     IF( NE_16( hCPE->element_mode, IVAS_CPE_DFT ) || ( EQ_16( hCPE->nchan_out, 1 ) && EQ_16( hCPE->hStereoDft->hConfig->res_cod_mode, STEREO_DFT_RES_COD_OFF ) ) )
     602             :     {
     603      368080 :         IF( NE_32( ( error = ivas_core_dec_fx( st_ivas, NULL, hCPE, st_ivas->hMCT, n_channels, output, outputHB_fx, NULL, st_ivas->sba_dirac_stereo_flag ) ), IVAS_ERR_OK ) )
     604             :         {
     605           0 :             return error;
     606             :         }
     607             :     }
     608             : 
     609      410722 :     IF( st_ivas->hMCT )
     610             :     {
     611      261444 :         pop_wmops();
     612             : 
     613      261444 :         return error;
     614             :     }
     615             : 
     616             :     /*----------------------------------------------------------------*
     617             :      * Stereo decoder & upmixing
     618             :      *----------------------------------------------------------------*/
     619      149278 :     test();
     620      149278 :     test();
     621      149278 :     IF( EQ_16( hCPE->element_mode, IVAS_CPE_DFT ) && !( EQ_16( hCPE->nchan_out, 1 ) && EQ_16( hCPE->hStereoDft->hConfig->res_cod_mode, STEREO_DFT_RES_COD_OFF ) ) )
     622       42642 :     {
     623             :         Word32 DFT_fx[CPE_CHANNELS][STEREO_DFT_BUF_MAX]; // q_dft
     624       42642 :         set32_fx( DFT_fx[0], 0, STEREO_DFT_BUF_MAX );
     625       42642 :         set32_fx( DFT_fx[1], 0, STEREO_DFT_BUF_MAX );
     626             : 
     627             :         /* core decoder */
     628       42642 :         IF( NE_32( ( error = ivas_core_dec_fx( NULL, NULL, hCPE, st_ivas->hMCT, n_channels, output, outputHB_fx, DFT_fx, 0 ) ), IVAS_ERR_OK ) )
     629             :         {
     630           0 :             return error;
     631             :         }
     632             : 
     633             :         // Scaling of DFT's
     634             :         Word16 shift;
     635             :         Word32 tmp1, tmp2;
     636             :         Word16 shift1, shift2;
     637       42642 :         maximum_abs_32_fx( DFT_fx[0], STEREO_DFT_BUF_MAX, &tmp1 );
     638       42642 :         maximum_abs_32_fx( DFT_fx[1], STEREO_DFT_BUF_MAX, &tmp2 );
     639             : 
     640       42642 :         IF( tmp1 == 0 )
     641             :         {
     642        5497 :             shift1 = Q31;
     643        5497 :             move16();
     644             :         }
     645             :         ELSE
     646             :         {
     647       37145 :             shift1 = norm_l( tmp1 );
     648             :         }
     649       42642 :         IF( tmp2 == 0 )
     650             :         {
     651       22975 :             shift2 = Q31;
     652       22975 :             move16();
     653             :         }
     654             :         ELSE
     655             :         {
     656       19667 :             shift2 = norm_l( tmp2 );
     657             :         }
     658       42642 :         shift = s_min( shift1, shift2 );
     659             : 
     660       42642 :         IF( NE_16( shift, 31 ) )
     661             :         {
     662       37149 :             shift = sub( add( hCPE->hStereoDft->q_dft, shift ), Q16 ); /* Q16 for guard bits */
     663             : 
     664       37149 :             IF( GT_16( shift, hCPE->hStereoDft->q_dft ) )
     665             :             {
     666         624 :                 Scale_sig32( DFT_fx[0], STEREO_DFT_BUF_MAX, sub( shift, hCPE->hStereoDft->q_dft ) ); // shift
     667         624 :                 Scale_sig32( DFT_fx[1], STEREO_DFT_BUF_MAX, sub( shift, hCPE->hStereoDft->q_dft ) ); // shift
     668         624 :                 hCPE->hStereoDft->q_dft = shift;
     669         624 :                 move16();
     670             :             }
     671             :         }
     672             :         ELSE
     673             :         {
     674        5493 :             hCPE->hStereoDft->q_dft = Q8;
     675        5493 :             move16();
     676             :         }
     677             : 
     678             :         /* DFT Stereo residual decoding */
     679       42642 :         test();
     680       42642 :         IF( hCPE->hStereoDft->res_cod_band_max > 0 && !st_ivas->bfi )
     681             :         {
     682             :             Word16 q;
     683             :             Word16 q_out_DFT[2];
     684             : 
     685       17017 :             q = Q11;
     686       17017 :             move16();
     687             : 
     688       17017 :             Copy_Scale_sig_32_16( hCPE->hCoreCoder[0]->old_pitch_buf_fx, hCPE->hCoreCoder[0]->old_pitch_buf_16_fx, ( ( 2 * NB_SUBFR16k ) + 2 ), -( Q10 ) ); // Q16->Q6
     689             : 
     690       17017 :             stereo_dft_dec_res_fx( hCPE, res_buf_fx, q_res_buf, output[1] );
     691             : 
     692       17017 :             Copy_Scale_sig_16_32_DEPREC( hCPE->hCoreCoder[0]->old_pitch_buf_16_fx, hCPE->hCoreCoder[0]->old_pitch_buf_fx, ( ( 2 * NB_SUBFR16k ) + 2 ), 10 ); // Q6->Q16
     693             : 
     694       17017 :             Scale_sig32( output[1], L_FRAME8k, ( Q11 - Q15 ) ); // Q15 -> Q11
     695             : 
     696       17017 :             q_out_DFT[0] = q_out_DFT[1] = hCPE->hStereoDft->q_dft;
     697       17017 :             move16();
     698       17017 :             move16();
     699             : 
     700       17017 :             stereo_dft_dec_analyze_fx( hCPE, output[1], DFT_fx, 1, L_FRAME8k, output_frame, DFT_STEREO_DEC_ANA_LB, 0, 0, &q, q_out_DFT );
     701             : 
     702       17017 :             Scale_sig32( DFT_fx[1], STEREO_DFT_BUF_MAX, sub( hCPE->hStereoDft->q_dft, q_out_DFT[1] ) ); // q_dft
     703             :         }
     704             : 
     705             :         /* DFT stereo CNG */
     706             :         {
     707             :             Word16 q_dft;
     708             : 
     709       42642 :             q_dft = hCPE->hStereoDft->q_dft;
     710       42642 :             move16();
     711             : 
     712       42642 :             stereo_dtf_cng_fx( hCPE, ivas_total_brate, DFT_fx, output_frame, q_dft );
     713             :         }
     714             : 
     715             :         /* decoding */
     716       42642 :         IF( EQ_16( hCPE->nchan_out, 1 ) )
     717             :         {
     718        5355 :             IF( EQ_16( hCPE->hStereoDft->first_frame, 1 ) )
     719             :             {
     720          21 :                 hCPE->hStereoDft->q_smoothed_nrg = hCPE->hStereoDft->q_dft;
     721          21 :                 move16();
     722         105 :                 FOR( Word16 ii = 0; ii < (Word16) ( sizeof( hCPE->hStereoDft->q_DFT_past_DMX_fx ) / sizeof( hCPE->hStereoDft->q_DFT_past_DMX_fx[0] ) ); ii++ )
     723             :                 {
     724          84 :                     hCPE->hStereoDft->q_DFT_past_DMX_fx[ii] = hCPE->hStereoDft->q_dft;
     725          84 :                     move16();
     726             :                 }
     727          21 :                 hCPE->hStereoDft->first_frame = 0;
     728          21 :                 move16();
     729             :             }
     730             : 
     731        5355 :             scale_sig32( hCPE->hStereoDft->res_cod_mem_fx, STEREO_DFT_OVL_8k, sub( hCPE->hStereoDft->q_dft, hCPE->hStereoDft->q_res_cod_mem_fx ) ); // q_dft
     732        5355 :             hCPE->hStereoDft->q_res_cod_mem_fx = hCPE->hStereoDft->q_dft;
     733        5355 :             move16();
     734        5355 :             stereo_dft_unify_dmx_fx( hCPE->hStereoDft, sts[0], DFT_fx, hCPE->input_mem_fx[1], hCPE->hStereoCng->prev_sid_nodata );
     735        5355 :             scale_sig32( hCPE->hStereoDft->res_cod_mem_fx, STEREO_DFT_OVL_8k, sub( Q15, hCPE->hStereoDft->q_res_cod_mem_fx ) ); // Q15
     736        5355 :             hCPE->hStereoDft->q_res_cod_mem_fx = Q15;
     737        5355 :             move16();
     738             :         }
     739             :         ELSE
     740             :         {
     741             :             {
     742       37287 :                 IF( EQ_16( hCPE->hStereoDft->first_frame, 1 ) )
     743             :                 {
     744         452 :                     hCPE->hStereoDft->q_smoothed_nrg = hCPE->hStereoDft->q_dft;
     745         452 :                     move16();
     746        2260 :                     FOR( Word16 ii = 0; ii < (Word16) ( sizeof( hCPE->hStereoDft->q_DFT_past_DMX_fx ) / sizeof( hCPE->hStereoDft->q_DFT_past_DMX_fx[0] ) ); ii++ )
     747             :                     {
     748        1808 :                         hCPE->hStereoDft->q_DFT_past_DMX_fx[ii] = hCPE->hStereoDft->q_dft;
     749        1808 :                         move16();
     750             :                     }
     751         452 :                     hCPE->hStereoDft->first_frame = 0;
     752         452 :                     move16();
     753             :                 }
     754             : 
     755       37287 :                 scale_sig32( hCPE->hStereoDft->res_cod_mem_fx, STEREO_DFT_OVL_8k, sub( hCPE->hStereoDft->q_dft, hCPE->hStereoDft->q_res_cod_mem_fx ) ); // q_dft
     756       37287 :                 hCPE->hStereoDft->q_res_cod_mem_fx = hCPE->hStereoDft->q_dft;
     757       37287 :                 move16();
     758             :             }
     759       37287 :             stereo_dft_dec_fx( hCPE->hStereoDft, sts[0], DFT_fx, hCPE->input_mem_fx[1], hCPE->hStereoCng, 0, 0, 0, 0, 0, 0, MAX_PARAM_SPATIAL_SUBFRAMES );
     760       37287 :             scale_sig32( hCPE->hStereoDft->res_cod_mem_fx, STEREO_DFT_OVL_8k, sub( Q15, hCPE->hStereoDft->q_res_cod_mem_fx ) ); // Q15
     761       37287 :             hCPE->hStereoDft->q_res_cod_mem_fx = Q15;
     762       37287 :             move16();
     763             :         }
     764             : 
     765      122571 :         FOR( n = 0; n < hCPE->nchan_out; n++ )
     766             :         {
     767       79929 :             Scale_sig32( output[n], L_FRAME48k, sub( hCPE->hStereoDft->q_dft, Q11 ) );                                                                          // q_dft
     768       79929 :             scale_sig32( hCPE->output_mem_fx[n], NS2SA_FX2( st_ivas->hDecoderConfig->output_Fs, STEREO_DFT32MS_OVL_NS ), sub( hCPE->hStereoDft->q_dft, Q11 ) ); // q_dft
     769       79929 :             hCPE->q_output_mem_fx[n] = hCPE->hStereoDft->q_dft;
     770       79929 :             move16();
     771             :         }
     772             : 
     773             :         /* synthesis iFFT */
     774      122571 :         FOR( n = 0; n < hCPE->nchan_out; n++ )
     775             :         {
     776       79929 :             stereo_dft_dec_synthesize_fx( hCPE, DFT_fx, n, output[n], output_frame );
     777             :         }
     778             : 
     779             :         // delete below
     780      122571 :         FOR( n = 0; n < hCPE->nchan_out; n++ )
     781             :         {
     782       79929 :             Scale_sig32( output[n], L_FRAME48k, sub( Q11, hCPE->hStereoDft->q_dft ) );                                                                          // Q11
     783       79929 :             scale_sig32( hCPE->output_mem_fx[n], NS2SA_FX2( st_ivas->hDecoderConfig->output_Fs, STEREO_DFT32MS_OVL_NS ), sub( Q11, hCPE->hStereoDft->q_dft ) ); // Q11
     784       79929 :             hCPE->q_output_mem_fx[n] = Q11;
     785       79929 :             move16();
     786             :         }
     787             :     }
     788      106636 :     ELSE IF( EQ_16( hCPE->element_mode, IVAS_CPE_TD ) )
     789             :     {
     790        3686 :         test();
     791        3686 :         if ( NE_16( hCPE->last_element_mode, IVAS_CPE_TD ) && sts[0]->tdm_LRTD_flag )
     792             :         {
     793          56 :             hCPE->hStereoTD->tdm_last_ratio_idx = tdm_ratio_idx;
     794          56 :             move16();
     795             :         }
     796             : 
     797             :         /* TD stereo upmixing */
     798        3686 :         stereo_tdm_combine_fx( hCPE, output[0], output[1], output_frame, 0, tdm_ratio_idx );
     799        3686 :         IF( sts[0]->tdm_LRTD_flag )
     800             :         {
     801        3605 :             stereo_tdm_combine_fx( hCPE, outputHB_fx[0], outputHB_fx[1], output_frame, 0, tdm_ratio_idx );
     802             :         }
     803             : 
     804        3686 :         hCPE->hStereoCng->last_tdm_idx = hCPE->hStereoTD->tdm_last_ratio_idx;
     805        3686 :         move16();
     806        3686 :         hCPE->hStereoTD->tdm_last_ratio_idx = tdm_ratio_idx;
     807        3686 :         move16();
     808             : 
     809        3686 :         IF( EQ_16( hCPE->nchan_out, 1 ) )
     810             :         {
     811             :             /* Scale the Right channel with the gain */
     812        1332 :             stereo_tca_scale_R_channel_fx( hCPE, output[1], output_frame );
     813             :             /* stereo to mono downmix */
     814      793972 :             FOR( i = 0; i < output_frame; i++ )
     815             :             {
     816      792640 :                 output[0][i] = L_shr( L_add( output[0][i], output[1][i] ), 1 );
     817      792640 :                 move32();
     818             :             }
     819             :         }
     820             :     }
     821             : 
     822             :     /*----------------------------------------------------------------*
     823             :      * Update parameters for stereo CNA
     824             :      *----------------------------------------------------------------*/
     825             : 
     826             :     // stereo_cna_update_params( hCPE, output_flt, output_frame, tdm_ratio_idx );
     827      149278 :     stereo_cna_update_params_fx( hCPE, output, output_frame, tdm_ratio_idx );
     828             : 
     829             :     /*----------------------------------------------------------------*
     830             :      * Synthesis synchronization between CPE modes
     831             :      *----------------------------------------------------------------*/
     832             : 
     833      149278 :     IF( !st_ivas->sba_dirac_stereo_flag )
     834             :     {
     835      147940 :         synchro_synthesis_fx( ivas_total_brate, hCPE, output, output_frame, 0, Q11 );
     836             :     }
     837             : 
     838      149278 :     test();
     839      149278 :     test();
     840      149278 :     test();
     841      149278 :     test();
     842      149278 :     IF( EQ_16( hCPE->element_mode, IVAS_CPE_MDCT ) && EQ_16( hCPE->nchan_out, 1 ) && ( is_DTXrate( ivas_total_brate ) == 0 || ( EQ_16( is_DTXrate( ivas_total_brate ), 1 ) && is_DTXrate( st_ivas->hDecoderConfig->last_ivas_total_brate ) == 0 ) ) )
     843             :     {
     844        5910 :         applyDmxMdctStereo_fx( hCPE, output, output_frame );
     845             :     }
     846             : 
     847             :     /*----------------------------------------------------------------*
     848             :      * IC-BWE: output LB and HB mix in ACELP mode
     849             :      *----------------------------------------------------------------*/
     850             : 
     851      149278 :     stereo_icBWE_decproc_fx( hCPE, output, outputHB_fx, last_core, last_bwidth, output_frame );
     852      149278 :     smooth_dft2td_transition_fx( hCPE, output, output_frame );
     853             : 
     854             :     /*----------------------------------------------------------------*
     855             :      * Temporal ICA, stereo adjustment and upmix
     856             :      *----------------------------------------------------------------*/
     857             : 
     858      149278 :     stereo_tca_dec_fx( hCPE, output, output_frame );
     859             : 
     860             : 
     861             :     /*----------------------------------------------------------------*
     862             :      * Common Stereo updates
     863             :      *----------------------------------------------------------------*/
     864             : 
     865      149278 :     hCPE->last_element_brate = hCPE->element_brate;
     866      149278 :     move32();
     867      149278 :     hCPE->last_element_mode = hCPE->element_mode;
     868      149278 :     move32();
     869             : 
     870      149278 :     if ( EQ_32( st_ivas->ivas_format, MASA_ISM_FORMAT ) )
     871             :     {
     872        6530 :         hCPE->element_brate = element_brate_ref;
     873        6530 :         move32();
     874             :     }
     875             : 
     876      149278 :     test();
     877      149278 :     IF( EQ_16( hCPE->element_mode, IVAS_CPE_DFT ) || EQ_16( hCPE->element_mode, IVAS_CPE_TD ) )
     878             :     {
     879       63378 :         stereo_cng_dec_update( hCPE, ivas_total_brate );
     880             :     }
     881             : 
     882      149278 :     st_ivas->BER_detect = s_or( st_ivas->BER_detect, sts[0]->BER_detect );
     883      149278 :     st_ivas->BER_detect = s_or( st_ivas->BER_detect, sts[1]->BER_detect );
     884      149278 :     move16();
     885      149278 :     move16();
     886             : 
     887             : 
     888      149278 :     pop_wmops();
     889      149278 :     return error;
     890             : }
     891             : 
     892             : 
     893             : /*-------------------------------------------------------------------------
     894             :  * create_cpe_dec_fx()
     895             :  *
     896             :  * Create, allocate and initialize IVAS decoder CPE handle
     897             :  *-------------------------------------------------------------------------*/
     898             : 
     899        2826 : ivas_error create_cpe_dec(
     900             :     Decoder_Struct *st_ivas,   /* i/o: IVAS decoder structure      */
     901             :     const Word16 cpe_id,       /* i  : CPE # identifier            */
     902             :     const Word32 element_brate /* i  : element bitrate             */
     903             : )
     904             : {
     905             :     Word16 i, n;
     906             :     CPE_DEC_HANDLE hCPE;
     907             :     Decoder_State *st;
     908             :     Word32 output_Fs;
     909             :     ivas_error error;
     910             :     Word32 cpe_brate;
     911             : 
     912        2826 :     error = IVAS_ERR_OK;
     913        2826 :     move32();
     914             : 
     915             :     /*-----------------------------------------------------------------*
     916             :      * Allocate CPE handle
     917             :      *-----------------------------------------------------------------*/
     918             : 
     919        2826 :     IF( ( hCPE = (CPE_DEC_HANDLE) malloc( sizeof( CPE_DEC_DATA ) ) ) == NULL )
     920             :     {
     921           0 :         return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for CPE\n" ) );
     922             :     }
     923             : 
     924             :     /*-----------------------------------------------------------------*
     925             :      * Initialization - general parameters
     926             :      *-----------------------------------------------------------------*/
     927             : 
     928        2826 :     output_Fs = st_ivas->hDecoderConfig->output_Fs;
     929        2826 :     move32();
     930        2826 :     hCPE->cpe_id = cpe_id;
     931        2826 :     move16();
     932        2826 :     hCPE->element_brate = element_brate;
     933        2826 :     move32();
     934        2826 :     hCPE->last_element_brate = hCPE->element_brate;
     935        2826 :     move32();
     936        2826 :     hCPE->element_mode = st_ivas->element_mode_init;
     937        2826 :     move16();
     938        2826 :     hCPE->last_element_mode = st_ivas->element_mode_init;
     939        2826 :     move16();
     940             : 
     941        2826 :     hCPE->hStereoDft = NULL;
     942        2826 :     hCPE->hStereoDftDmx = NULL;
     943        2826 :     hCPE->hStereoTD = NULL;
     944        2826 :     hCPE->hStereoMdct = NULL;
     945        2826 :     hCPE->hStereoTCA = NULL;
     946        2826 :     hCPE->hStereoICBWE = NULL;
     947        2826 :     hCPE->hStereoCng = NULL;
     948             : 
     949        2826 :     hCPE->stereo_switching_counter = 10;
     950        2826 :     move16();
     951        2826 :     hCPE->NbFrameMod = 7;
     952        2826 :     move16();
     953        2826 :     hCPE->lt_es_em_fx = 0;
     954        2826 :     move32();
     955             : 
     956             :     /* Note: nchan_out is considered to be related to the structure. This is nchan_out for CPE and for MASA_format is always 2. */
     957        2826 :     test();
     958        2826 :     test();
     959        2826 :     test();
     960        2826 :     IF( EQ_16( (Word16) st_ivas->ivas_format, SBA_FORMAT ) || EQ_16( (Word16) st_ivas->ivas_format, MASA_FORMAT ) || EQ_16( (Word16) st_ivas->ivas_format, MASA_ISM_FORMAT ) || EQ_16( (Word16) st_ivas->ivas_format, MC_FORMAT ) )
     961             :     {
     962        2087 :         hCPE->nchan_out = CPE_CHANNELS;
     963        2087 :         move16();
     964             :     }
     965             :     ELSE
     966             :     {
     967         739 :         hCPE->nchan_out = s_min( CPE_CHANNELS, st_ivas->hDecoderConfig->nchan_out );
     968         739 :         move16();
     969             :     }
     970             : 
     971        2826 :     test();
     972        2826 :     test();
     973        2826 :     test();
     974        2826 :     IF( EQ_16( extract_l( st_ivas->ivas_format ), MASA_ISM_FORMAT ) && ( EQ_16( (Word16) st_ivas->ism_mode, ISM_MASA_MODE_MASA_ONE_OBJ ) || EQ_16( (Word16) st_ivas->ism_mode, ISM_MASA_MODE_PARAM_ONE_OBJ ) || EQ_16( (Word16) st_ivas->ism_mode, ISM_MASA_MODE_DISC ) ) )
     975             :     {
     976          33 :         cpe_brate = element_brate;
     977          33 :         move32();
     978             :     }
     979             :     ELSE
     980             :     {
     981        2793 :         cpe_brate = st_ivas->hDecoderConfig->ivas_total_brate;
     982        2793 :         move32();
     983             :     }
     984             : 
     985        2826 :     test();
     986        2826 :     test();
     987        2826 :     test();
     988        2826 :     test();
     989        5633 :     if ( ( ( EQ_16( (Word16) st_ivas->ivas_format, MASA_FORMAT ) && LT_32( st_ivas->hDecoderConfig->ivas_total_brate, MASA_STEREO_MIN_BITRATE ) ) ||
     990        2868 :            ( EQ_16( (Word16) st_ivas->ivas_format, MASA_ISM_FORMAT ) && LT_32( cpe_brate, MASA_STEREO_MIN_BITRATE ) ) ) &&
     991          28 :          GT_32( st_ivas->hDecoderConfig->ivas_total_brate, IVAS_SID_5k2 ) )
     992             :     {
     993          28 :         hCPE->nchan_out = 1;
     994          28 :         move16();
     995             :     }
     996             : 
     997        8478 :     FOR( n = 0; n < CPE_CHANNELS; n++ )
     998             :     {
     999        5652 :         set32_fx( hCPE->prev_hb_synth_fx[n], 0, NS2SA_FX2( output_Fs, L_sub( IVAS_DEC_DELAY_NS, DELAY_BWE_TOTAL_NS ) ) );
    1000        5652 :         set32_fx( hCPE->prev_synth_fx[n], 0, NS2SA_FX2( output_Fs, L_sub( IVAS_DEC_DELAY_NS, STEREO_DFT32MS_OVL_NS ) ) );
    1001             :     }
    1002        2826 :     hCPE->q_prev_synth_fx = 0;
    1003        2826 :     move16();
    1004        2826 :     hCPE->brate_surplus = 0;
    1005        2826 :     move32();
    1006             : 
    1007             :     /*-----------------------------------------------------------------*
    1008             :      * DFT stereo I/O Buffers: allocate and initialize
    1009             :      *-----------------------------------------------------------------*/
    1010             : 
    1011        8478 :     FOR( i = 0; i < CPE_CHANNELS; i++ )
    1012             :     {
    1013        5652 :         test();
    1014        5652 :         test();
    1015        5652 :         test();
    1016        5652 :         test();
    1017        5652 :         test();
    1018        5652 :         IF( EQ_16( (Word16) st_ivas->ivas_format, STEREO_FORMAT ) || EQ_16( (Word16) st_ivas->ivas_format, MASA_FORMAT ) || EQ_16( (Word16) st_ivas->ivas_format, MASA_ISM_FORMAT ) ||
    1019             :             ( EQ_16( (Word16) st_ivas->ivas_format, MC_FORMAT ) && EQ_16( (Word16) st_ivas->mc_mode, MC_MODE_MCMASA ) ) || st_ivas->sba_dirac_stereo_flag )
    1020             :         {
    1021         778 :             IF( ( hCPE->input_mem_fx[i] = (Word32 *) malloc( sizeof( Word32 ) * NS2SA_FX2( output_Fs, STEREO_DFT32MS_OVL_NS ) ) ) == NULL )
    1022             :             {
    1023           0 :                 return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DFT stereo memory\n" ) );
    1024             :             }
    1025         778 :             set32_fx( hCPE->input_mem_fx[i], 0, NS2SA_FX2( output_Fs, STEREO_DFT32MS_OVL_NS ) );
    1026             : 
    1027         778 :             IF( ( hCPE->input_mem_LB_fx[i] = (Word32 *) malloc( sizeof( Word32 ) * STEREO_DFT32MS_OVL_16k ) ) == NULL )
    1028             :             {
    1029           0 :                 return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DFT stereo memory\n" ) );
    1030             :             }
    1031         778 :             set32_fx( hCPE->input_mem_LB_fx[i], 0, STEREO_DFT32MS_OVL_16k );
    1032             : 
    1033         778 :             IF( i == 0 )
    1034             :             {
    1035         389 :                 IF( ( hCPE->input_mem_BPF_fx[0] = (Word32 *) malloc( sizeof( Word32 ) * STEREO_DFT32MS_OVL_16k ) ) == NULL )
    1036             :                 {
    1037           0 :                     return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DFT stereo memory\n" ) );
    1038             :                 }
    1039         389 :                 set32_fx( hCPE->input_mem_BPF_fx[0], 0, STEREO_DFT32MS_OVL_16k );
    1040             :             }
    1041             : 
    1042         778 :             IF( ( hCPE->output_mem_fx[i] = (Word32 *) malloc( sizeof( Word32 ) * NS2SA_FX2( output_Fs, STEREO_DFT32MS_OVL_NS ) ) ) == NULL )
    1043             :             {
    1044           0 :                 return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DFT stereo memory\n" ) );
    1045             :             }
    1046         778 :             set32_fx( hCPE->output_mem_fx[i], 0, NS2SA_FX2( output_Fs, STEREO_DFT32MS_OVL_NS ) );
    1047         778 :             hCPE->q_output_mem_fx[i] = Q11;
    1048         778 :             move16();
    1049         778 :             IF( LT_16( i, hCPE->nchan_out ) )
    1050             :             {
    1051         728 :                 IF( ( hCPE->prev_synth_chs_fx[i] = (Word32 *) malloc( sizeof( Word32 ) * NS2SA_FX2( output_Fs, FRAME_SIZE_NS ) ) ) == NULL )
    1052             :                 {
    1053           0 :                     return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DFT stereo memory\n" ) );
    1054             :                 }
    1055         728 :                 set32_fx( hCPE->prev_synth_chs_fx[i], 0, NS2SA_FX2( output_Fs, FRAME_SIZE_NS ) );
    1056             :             }
    1057             :             ELSE
    1058             :             {
    1059          50 :                 hCPE->prev_synth_chs_fx[i] = NULL;
    1060             :             }
    1061             :         }
    1062             :         ELSE
    1063             :         {
    1064        4874 :             hCPE->input_mem_fx[i] = NULL;
    1065        4874 :             hCPE->input_mem_LB_fx[i] = NULL;
    1066        4874 :             if ( i == 0 )
    1067             :             {
    1068        2437 :                 hCPE->input_mem_BPF_fx[0] = NULL;
    1069             :             }
    1070        4874 :             hCPE->output_mem_fx[i] = NULL;
    1071        4874 :             hCPE->q_output_mem_fx[i] = 0;
    1072        4874 :             move16();
    1073        4874 :             hCPE->prev_synth_chs_fx[i] = NULL;
    1074             :         }
    1075             :     }
    1076             : 
    1077             :     /*-----------------------------------------------------------------*
    1078             :      * CoreCoder, 2 instances: allocate and initialize
    1079             :      *-----------------------------------------------------------------*/
    1080             : 
    1081        8404 :     FOR( n = 0; n < CPE_CHANNELS; n++ )
    1082             :     {
    1083        5615 :         IF( st_ivas->sba_dirac_stereo_flag && EQ_16( st_ivas->nchan_transport, 1 ) )
    1084             :         {
    1085             :             /* for SBA DirAC stereo output CPE element is only used for upmix, core coder is found in SCE element used for core decoding */
    1086          37 :             BREAK;
    1087             :         }
    1088             : 
    1089        5578 :         IF( ( st = (DEC_CORE_HANDLE) malloc( sizeof( Decoder_State ) ) ) == NULL )
    1090             :         {
    1091           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for CoreCoder structure\n" ) );
    1092             :         }
    1093             : 
    1094        5578 :         copy_decoder_config( st_ivas, st );
    1095             : 
    1096             :         // st->total_brate = hCPE->element_brate / ( CPE_CHANNELS );
    1097        5578 :         st->total_brate = L_shr( hCPE->element_brate, 1 ); /* dummy initialization for getting right pointers initialization of input buffers in init_coder_ace_plus() */
    1098        5578 :         move32();
    1099        5578 :         st->mct_chan_mode = MCT_CHAN_MODE_REGULAR;
    1100        5578 :         move32();
    1101        5578 :         st->is_ism_format = 0;
    1102        5578 :         move16();
    1103             : 
    1104        5578 :         IF( NE_32( ( error = init_decoder_ivas_fx( st, n, st_ivas->mc_mode ) ), IVAS_ERR_OK ) )
    1105             :         {
    1106           0 :             return error;
    1107             :         }
    1108             : 
    1109        5578 :         hCPE->hCoreCoder[n] = st;
    1110             :     }
    1111             : 
    1112             :     /*-----------------------------------------------------------------*
    1113             :      * DFT stereo initialization
    1114             :      *-----------------------------------------------------------------*/
    1115             : 
    1116             : 
    1117        2826 :     test();
    1118        2826 :     IF( EQ_16( hCPE->element_mode, IVAS_CPE_DFT ) || ( st_ivas->sba_dirac_stereo_flag && hCPE->cpe_id == 0 ) )
    1119             :     {
    1120         194 :         IF( NE_32( ( error = stereo_dft_dec_create_fx( &( hCPE->hStereoDft ), hCPE->element_brate, output_Fs, st_ivas->sba_dirac_stereo_flag, st_ivas->nchan_transport ) ), IVAS_ERR_OK ) )
    1121             :         {
    1122           0 :             return error;
    1123             :         }
    1124             :     }
    1125             : 
    1126             :     /*-----------------------------------------------------------------*
    1127             :      * DFT stereo mono DMX initialization
    1128             :      *-----------------------------------------------------------------*/
    1129             : 
    1130        2826 :     test();
    1131        2826 :     IF( NE_16( hCPE->element_mode, IVAS_CPE_MDCT ) && EQ_16( hCPE->nchan_out, 1 ) )
    1132             :     {
    1133          45 :         IF( ( hCPE->hStereoDftDmx = (STEREO_DFT_DMX_DATA_HANDLE) malloc( sizeof( STEREO_DFT_DMX_DATA ) ) ) == NULL )
    1134             :         {
    1135           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Stereo DFT mono output\n" ) );
    1136             :         }
    1137             : 
    1138          45 :         stereo_dft_dmx_out_reset_fx( hCPE->hStereoDftDmx );
    1139             :     }
    1140             : 
    1141             :     /*-----------------------------------------------------------------*
    1142             :      * Temporal inter-channel alignment initialization
    1143             :      *-----------------------------------------------------------------*/
    1144             : 
    1145        2826 :     test();
    1146        2826 :     test();
    1147        2826 :     test();
    1148        2826 :     IF( ( NE_16( hCPE->element_mode, IVAS_CPE_MDCT ) || ( EQ_16( (Word16) st_ivas->ivas_format, STEREO_FORMAT ) && LE_32( hCPE->element_brate, MAX_MDCT_ITD_BRATE ) ) ) && NE_16( hCPE->nchan_out, 1 ) )
    1149             :     {
    1150          99 :         IF( ( hCPE->hStereoTCA = (STEREO_TCA_DEC_HANDLE) malloc( sizeof( STEREO_TCA_DEC_DATA ) ) ) == NULL )
    1151             :         {
    1152           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Stereo TCA\n" ) );
    1153             :         }
    1154             : 
    1155          99 :         stereo_tca_init_dec_fx( hCPE->hStereoTCA );
    1156             :     }
    1157             : 
    1158             :     /*-----------------------------------------------------------------*
    1159             :      * Stereo IC BWE initialization
    1160             :      *-----------------------------------------------------------------*/
    1161             : 
    1162        2826 :     test();
    1163        2826 :     test();
    1164        2826 :     IF( NE_16( hCPE->element_mode, IVAS_CPE_MDCT ) && !( EQ_16( hCPE->element_mode, IVAS_CPE_DFT ) && EQ_16( hCPE->nchan_out, 1 ) ) )
    1165             :     {
    1166          90 :         IF( ( hCPE->hStereoICBWE = (STEREO_ICBWE_DEC_HANDLE) malloc( sizeof( STEREO_ICBWE_DEC_DATA ) ) ) == NULL )
    1167             :         {
    1168           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Stereo ICBWE\n" ) );
    1169             :         }
    1170             : 
    1171          90 :         stereo_icBWE_init_dec_fx( hCPE->hStereoICBWE );
    1172             :     }
    1173             : 
    1174             :     /*-----------------------------------------------------------------*
    1175             :      * TD stereo initialization
    1176             :      *-----------------------------------------------------------------*/
    1177             : 
    1178        2826 :     IF( EQ_16( hCPE->element_mode, IVAS_CPE_TD ) )
    1179             :     {
    1180           0 :         IF( ( hCPE->hStereoTD = (STEREO_TD_DEC_DATA_HANDLE) malloc( sizeof( STEREO_TD_DEC_DATA ) ) ) == NULL )
    1181             :         {
    1182           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for TD Stereo\n" ) );
    1183             :         }
    1184             : 
    1185           0 :         stereo_td_init_dec_fx( hCPE->hStereoTD, hCPE->last_element_mode );
    1186             :     }
    1187             : 
    1188             :     /*-----------------------------------------------------------------*
    1189             :      * MDCT stereo initialization
    1190             :      *-----------------------------------------------------------------*/
    1191             : 
    1192        2826 :     test();
    1193        2826 :     IF( EQ_16( hCPE->element_mode, IVAS_CPE_MDCT ) && EQ_16( st_ivas->nCPE, 1 ) )
    1194             :     {
    1195         371 :         IF( ( hCPE->hStereoMdct = (STEREO_MDCT_DEC_DATA_HANDLE) malloc( sizeof( STEREO_MDCT_DEC_DATA ) ) ) == NULL )
    1196             :         {
    1197           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Stereo MDCT\n" ) );
    1198             :         }
    1199             : 
    1200         371 :         test();
    1201         371 :         IF( EQ_32( st_ivas->ivas_format, STEREO_FORMAT ) && LE_32( hCPE->element_brate, MAX_MDCT_ITD_BRATE ) )
    1202             :         {
    1203          13 :             hCPE->hStereoMdct->use_itd = 1;
    1204          13 :             move16();
    1205             :         }
    1206             :         ELSE
    1207             :         {
    1208         358 :             hCPE->hStereoMdct->use_itd = 0;
    1209         358 :             move16();
    1210             :         }
    1211             : 
    1212         371 :         hCPE->hStereoMdct->reverse_dmx = 0;
    1213         371 :         move16();
    1214         371 :         hCPE->hStereoMdct->smooth_ratio_fx = ONE_IN_Q26;
    1215         371 :         move32();
    1216         371 :         set16_fx( hCPE->hStereoMdct->prev_ms_mask[0], 0, MAX_SFB );
    1217         371 :         set16_fx( hCPE->hStereoMdct->prev_ms_mask[1], 0, MAX_SFB );
    1218         371 :         hCPE->hStereoMdct->lastCoh_fx = ONE_IN_Q14;
    1219         371 :         move16();
    1220         371 :         hCPE->hStereoMdct->mdct_stereo_mode[0] = SMDCT_DUAL_MONO;
    1221         371 :         move16();
    1222         371 :         hCPE->hStereoMdct->mdct_stereo_mode[1] = SMDCT_DUAL_MONO;
    1223         371 :         move16();
    1224         371 :         hCPE->hStereoMdct->IGFStereoMode[0] = -1;
    1225         371 :         move16();
    1226         371 :         hCPE->hStereoMdct->IGFStereoMode[1] = -1;
    1227         371 :         move16();
    1228             :     }
    1229             : 
    1230             :     /*-----------------------------------------------------------------*
    1231             :      * Stereo CNG initialization
    1232             :      *-----------------------------------------------------------------*/
    1233             : 
    1234        2826 :     test();
    1235        2826 :     IF( EQ_16( hCPE->element_mode, IVAS_CPE_TD ) || EQ_16( hCPE->element_mode, IVAS_CPE_DFT ) )
    1236             :     {
    1237          98 :         IF( ( hCPE->hStereoCng = (STEREO_CNG_DEC_HANDLE) malloc( sizeof( STEREO_CNG_DEC ) ) ) == NULL )
    1238             :         {
    1239           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Stereo CNG\n" ) );
    1240             :         }
    1241          98 :         stereo_cng_init_dec_fx( hCPE->hStereoCng, &hCPE->hCoreCoder[0]->hFdCngDec->hFdCngCom->frameSize );
    1242             :     }
    1243             : 
    1244        2826 :     st_ivas->hCPE[cpe_id] = hCPE;
    1245             : 
    1246        2826 :     return error;
    1247             : }
    1248             : 
    1249             : 
    1250             : /*-------------------------------------------------------------------------
    1251             :  * destroy_cpe_dec_fx()
    1252             :  *
    1253             :  * Destroy and deallocate IVAS decoder CPE handle
    1254             :  *-------------------------------------------------------------------------*/
    1255             : 
    1256        2826 : void destroy_cpe_dec(
    1257             :     CPE_DEC_HANDLE hCPE /* i/o: CPE decoder structure       */
    1258             : )
    1259             : {
    1260             :     Word16 n;
    1261             :     Decoder_State *st;
    1262             : 
    1263        8478 :     FOR( n = 0; n < CPE_CHANNELS; n++ )
    1264             :     {
    1265        5652 :         st = hCPE->hCoreCoder[n];
    1266             : 
    1267        5652 :         IF( st != NULL )
    1268             :         {
    1269        5474 :             destroy_core_dec_fx( st );
    1270             : 
    1271        5474 :             free( st );
    1272        5474 :             st = NULL;
    1273             :         }
    1274             :     }
    1275             : 
    1276        2826 :     IF( hCPE->hStereoDft != NULL )
    1277             :     {
    1278         177 :         stereo_dft_dec_destroy_fx( &( hCPE->hStereoDft ) );
    1279         177 :         hCPE->hStereoDft = NULL;
    1280             :     }
    1281             : 
    1282        2826 :     IF( hCPE->hStereoDftDmx != NULL )
    1283             :     {
    1284          31 :         free( hCPE->hStereoDftDmx );
    1285          31 :         hCPE->hStereoDftDmx = NULL;
    1286             :     }
    1287             : 
    1288        2826 :     IF( hCPE->hStereoTD != NULL )
    1289             :     {
    1290           0 :         free( hCPE->hStereoTD );
    1291           0 :         hCPE->hStereoTD = NULL;
    1292             :     }
    1293             : 
    1294        2826 :     IF( hCPE->hStereoMdct != NULL )
    1295             :     {
    1296         401 :         free( hCPE->hStereoMdct );
    1297         401 :         hCPE->hStereoMdct = NULL;
    1298             :     }
    1299             : 
    1300        2826 :     IF( hCPE->hStereoTCA != NULL )
    1301             :     {
    1302          98 :         free( hCPE->hStereoTCA );
    1303          98 :         hCPE->hStereoTCA = NULL;
    1304             :     }
    1305             : 
    1306        2826 :     IF( hCPE->hStereoICBWE != NULL )
    1307             :     {
    1308         103 :         free( hCPE->hStereoICBWE );
    1309         103 :         hCPE->hStereoICBWE = NULL;
    1310             :     }
    1311             : 
    1312        2826 :     IF( hCPE->input_mem_LB_fx[0] != NULL )
    1313             :     {
    1314        1167 :         FOR( n = 0; n < CPE_CHANNELS; n++ )
    1315             :         {
    1316         778 :             free( hCPE->input_mem_LB_fx[n] );
    1317         778 :             hCPE->input_mem_LB_fx[n] = NULL;
    1318         778 :             free( hCPE->input_mem_fx[n] );
    1319         778 :             hCPE->input_mem_fx[n] = NULL;
    1320         778 :             free( hCPE->output_mem_fx[n] );
    1321         778 :             hCPE->output_mem_fx[n] = NULL;
    1322             : 
    1323         778 :             IF( hCPE->prev_synth_chs_fx[n] != NULL )
    1324             :             {
    1325         741 :                 free( hCPE->prev_synth_chs_fx[n] );
    1326         741 :                 hCPE->prev_synth_chs_fx[n] = NULL;
    1327             :             }
    1328             :         }
    1329         389 :         free( hCPE->input_mem_BPF_fx[0] );
    1330         389 :         hCPE->input_mem_BPF_fx[0] = NULL;
    1331             :     }
    1332             : 
    1333        2826 :     IF( hCPE->hStereoCng != NULL )
    1334             :     {
    1335          81 :         free( hCPE->hStereoCng );
    1336          81 :         hCPE->hStereoCng = NULL;
    1337             :     }
    1338             : 
    1339        2826 :     free( hCPE );
    1340             : 
    1341        2826 :     return;
    1342             : }
    1343             : 
    1344             : 
    1345             : /*-------------------------------------------------------------------------
    1346             :  * read_stereo_mode_and_bwidth_fx()
    1347             :  *
    1348             :  * Read stereo technology info & audio bandwidth
    1349             :  *-------------------------------------------------------------------------*/
    1350             : 
    1351      410722 : static void read_stereo_mode_and_bwidth_fx(
    1352             :     CPE_DEC_HANDLE hCPE,          /* i/o: CPE handle             */
    1353             :     const Decoder_Struct *st_ivas /* i  : decoder main structure */
    1354             : )
    1355             : {
    1356             :     Decoder_State **sts;
    1357             : 
    1358             :     /*-----------------------------------------------------------------*
    1359             :      * BFI or NO_DATA frame: Use stereo parameters from last (active) frame
    1360             :      *-----------------------------------------------------------------*/
    1361             : 
    1362      410722 :     test();
    1363      410722 :     if ( st_ivas->bfi || LT_32( st_ivas->hDecoderConfig->ivas_total_brate, IVAS_SID_5k2 ) )
    1364             :     {
    1365       19102 :         hCPE->element_mode = hCPE->last_element_mode;
    1366       19102 :         move16();
    1367             :     }
    1368             : 
    1369             :     /*-----------------------------------------------------------------*
    1370             :      * SID frame: get element mode from SID side info
    1371             :      *-----------------------------------------------------------------*/
    1372             : 
    1373      391620 :     ELSE IF( EQ_32( st_ivas->hDecoderConfig->ivas_total_brate, IVAS_SID_5k2 ) )
    1374             :     {
    1375        2069 :         SWITCH( st_ivas->sid_format )
    1376             :         {
    1377        1418 :             case SID_DFT_STEREO:
    1378        1418 :                 hCPE->element_mode = IVAS_CPE_DFT;
    1379        1418 :                 move16();
    1380             :                 /* Read CNG type */
    1381        1418 :                 hCPE->hCoreCoder[0]->cng_type = get_next_indice_fx( hCPE->hCoreCoder[0], 1 );
    1382             : 
    1383             :                 /* Read BW information in SID */
    1384        1418 :                 hCPE->hCoreCoder[0]->bwidth = get_next_indice_fx( hCPE->hCoreCoder[0], 2 );
    1385        1418 :                 move16();
    1386        1418 :                 move16();
    1387             : 
    1388        1418 :                 BREAK;
    1389         577 :             case SID_MDCT_STEREO:
    1390             :                 /* 2TC SBA DTX also uses MDCT-Stereo DTX */
    1391             :             case SID_SBA_2TC:
    1392         577 :                 hCPE->element_mode = IVAS_CPE_MDCT;
    1393         577 :                 move16();
    1394         577 :                 BREAK;
    1395           0 :             case SID_SBA_1TC:
    1396           0 :                 assert( !"Forbidden value for SID format in CPE (SBA 1TC), should have already been adressed earlier" );
    1397             :                 BREAK;
    1398           0 :             case SID_MASA_1TC:
    1399           0 :                 hCPE->element_mode = IVAS_SCE;
    1400           0 :                 move16();
    1401           0 :                 BREAK;
    1402          74 :             case SID_MASA_2TC:
    1403             :                 /* 2TC MASA DTX uses MDCT or DFT based core */
    1404          74 :                 if ( EQ_16( hCPE->element_mode, IVAS_CPE_TD ) )
    1405             :                 {
    1406           0 :                     hCPE->element_mode = IVAS_CPE_DFT;
    1407           0 :                     move16();
    1408             :                 }
    1409          74 :                 IF( EQ_16( hCPE->element_mode, IVAS_CPE_DFT ) )
    1410             :                 {
    1411             :                     /* Read CNG type */
    1412          74 :                     hCPE->hCoreCoder[0]->cng_type = get_next_indice_fx( hCPE->hCoreCoder[0], 1 );
    1413             : 
    1414             :                     /* Read BW information in SID */
    1415          74 :                     hCPE->hCoreCoder[0]->bwidth = get_next_indice_fx( hCPE->hCoreCoder[0], 2 );
    1416          74 :                     move16();
    1417          74 :                     move16();
    1418             :                 }
    1419          74 :                 BREAK;
    1420           0 :             default:
    1421             :                 /* this is what has been done for all modes previously, may need adaptation in the future */
    1422           0 :                 hCPE->element_mode = hCPE->last_element_mode;
    1423           0 :                 move16();
    1424           0 :                 BREAK;
    1425             :         }
    1426             :     }
    1427             : 
    1428             :     /*-----------------------------------------------------------------*
    1429             :      * active frame: read element mode and audio bandwidth info
    1430             :      *-----------------------------------------------------------------*/
    1431             : 
    1432             :     ELSE
    1433             :     {
    1434      389551 :         sts = hCPE->hCoreCoder;
    1435      389551 :         test();
    1436      389551 :         IF( st_ivas->hMCT && hCPE->cpe_id != 0 )
    1437             :         {
    1438      161050 :             sts[0]->bwidth = st_ivas->hCPE[0]->hCoreCoder[0]->bwidth;
    1439      161050 :             move16();
    1440      161050 :             sts[1]->bwidth = st_ivas->hCPE[0]->hCoreCoder[0]->bwidth;
    1441      161050 :             move16();
    1442             :         }
    1443             :         ELSE
    1444             :         {
    1445             :             /* read stereo technology info */
    1446      228501 :             test();
    1447      228501 :             IF( LT_32( hCPE->element_brate, MIN_BRATE_MDCT_STEREO ) && st_ivas->hMCT == NULL )
    1448             :             {
    1449       52083 :                 hCPE->element_mode = add( get_next_indice_fx( sts[0], NBITS_ELEMENT_MODE ), IVAS_CPE_DFT );
    1450       52083 :                 move16();
    1451             :             }
    1452             :             ELSE
    1453             :             {
    1454      176418 :                 hCPE->element_mode = IVAS_CPE_MDCT;
    1455      176418 :                 move16();
    1456             :             }
    1457             : 
    1458             :             /* read the bandwidth */
    1459      228501 :             IF( LT_32( hCPE->element_brate, MIN_BRATE_FB_STEREO ) )
    1460             :             {
    1461             :                 /* WB and SWB are supported */
    1462       32110 :                 sts[0]->bwidth = add( get_next_indice_fx( sts[0], 1 ), WB );
    1463       32110 :                 move16();
    1464       32110 :                 sts[1]->bwidth = sts[0]->bwidth;
    1465       32110 :                 move16();
    1466             :             }
    1467             :             ELSE
    1468             :             {
    1469             :                 /* WB, SWB and FB are supported */
    1470      196391 :                 sts[0]->bwidth = get_next_indice_fx( sts[0], NBITS_BWIDTH );
    1471      196391 :                 move16();
    1472      196391 :                 sts[1]->bwidth = sts[0]->bwidth;
    1473      196391 :                 move16();
    1474             :             }
    1475             :         }
    1476             :     }
    1477             : 
    1478      410722 :     return;
    1479             : }
    1480             : 
    1481             : 
    1482             : /*-------------------------------------------------------------------------
    1483             :  * stereo_mode_combined_format_dec_fx()
    1484             :  *
    1485             :  * Set stereo format in a combined format
    1486             :  *-------------------------------------------------------------------------*/
    1487             : 
    1488      410722 : static void stereo_mode_combined_format_dec_fx(
    1489             :     const Decoder_Struct *st_ivas, /* i  : decoder main structure */
    1490             :     CPE_DEC_HANDLE hCPE            /* i/o: CPE handle             */
    1491             : )
    1492             : {
    1493             :     Word32 element_brate_ref;
    1494             : 
    1495      410722 :     IF( EQ_32( st_ivas->ivas_format, MASA_ISM_FORMAT ) )
    1496             :     {
    1497        6530 :         element_brate_ref = hCPE->element_brate;
    1498        6530 :         move32();
    1499             : 
    1500        6530 :         test();
    1501        6530 :         test();
    1502        6530 :         test();
    1503        6530 :         test();
    1504        6530 :         IF( EQ_32( st_ivas->ism_mode, ISM_MASA_MODE_DISC ) &&
    1505             :             ( ( EQ_16( st_ivas->nchan_ism, 3 ) && EQ_32( st_ivas->hDecoderConfig->ivas_total_brate, IVAS_96k ) ) ||
    1506             :               ( EQ_16( st_ivas->nchan_ism, 4 ) && EQ_32( st_ivas->hDecoderConfig->ivas_total_brate, IVAS_128k ) ) ) )
    1507             :         {
    1508         428 :             IF( !st_ivas->bfi )
    1509             :             {
    1510             :                 /* read OMASA stereo mode signalling */
    1511         426 :                 IF( get_next_indice_fx( hCPE->hCoreCoder[0], NBITS_ELEMENT_MODE ) )
    1512             :                 {
    1513           0 :                     hCPE->element_mode = IVAS_CPE_MDCT;
    1514           0 :                     move16();
    1515             :                 }
    1516             :                 ELSE
    1517             :                 {
    1518         426 :                     hCPE->element_mode = IVAS_CPE_DFT;
    1519         426 :                     move16();
    1520             :                 }
    1521             :             }
    1522             : 
    1523         428 :             IF( EQ_16( hCPE->element_mode, IVAS_CPE_MDCT ) )
    1524             :             {
    1525           0 :                 hCPE->element_brate = IVAS_64k;
    1526           0 :                 move32();
    1527           0 :                 hCPE->brate_surplus = L_sub( hCPE->brate_surplus, L_sub( hCPE->element_brate, element_brate_ref ) );
    1528           0 :                 move32();
    1529             :             }
    1530             :         }
    1531             :     }
    1532             : 
    1533      410722 :     return;
    1534             : }

Generated by: LCOV version 1.14