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

Generated by: LCOV version 1.14