LCOV - code coverage report
Current view: top level - lib_dec - ivas_cpe_dec.c (source / functions) Hit Total Coverage
Test: Coverage on main -- short test vectors @ 6c9ddc4024a9c0e1ecb8f643f114a84a0e26ec6b Lines: 358 392 91.3 %
Date: 2025-05-23 08:37:30 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.h"
      40             : #include "ivas_prot.h"
      41             : #include "ivas_rom_com.h"
      42             : #ifdef DEBUGGING
      43             : #include "debug.h"
      44             : #endif
      45             : #include "wmc_auto.h"
      46             : #include <math.h>
      47             : 
      48             : 
      49             : /*--------------------------------------------------------------------------*
      50             :  * Local function prototypes
      51             :  *--------------------------------------------------------------------------*/
      52             : 
      53             : static void read_stereo_mode_and_bwidth( CPE_DEC_HANDLE hCPE, const Decoder_Struct *st_ivas );
      54             : 
      55             : static void stereo_mode_combined_format_dec( const Decoder_Struct *st_ivas, CPE_DEC_HANDLE hCPE );
      56             : 
      57             : #ifdef FIX_1320_STACK_CPE_DECODER
      58             : static ivas_error stereo_dft_dec_main( CPE_DEC_HANDLE hCPE, const int32_t ivas_total_brate, const int16_t n_channels, float res_buf[STEREO_DFT_N_8k], float *output[], float outputHB[][L_FRAME48k], const int16_t output_frame );
      59             : 
      60             : #endif
      61             : 
      62             : /*--------------------------------------------------------------------------*
      63             :  * ivas_cpe_dec()
      64             :  *
      65             :  * Channel Pair Element (CPE) decoding routine
      66             :  *--------------------------------------------------------------------------*/
      67             : 
      68     1261158 : ivas_error ivas_cpe_dec(
      69             :     Decoder_Struct *st_ivas,       /* i/o: IVAS decoder structure          */
      70             :     const int16_t cpe_id,          /* i  : CPE # identifier                */
      71             :     float *output[CPE_CHANNELS],   /* o  : output synthesis signal         */
      72             :     const int16_t output_frame,    /* i  : output frame length per channel */
      73             :     const int16_t nb_bits_metadata /* i  : number of metadata bits         */
      74             : )
      75             : {
      76             :     int16_t i, n, n_channels;
      77             :     int16_t nb_bits, last_core;
      78             :     int16_t last_bwidth;
      79             :     int16_t tdm_ratio_idx;
      80             :     float outputHB[CPE_CHANNELS][L_FRAME48k]; /* 'float' buffer for output HB synthesis, both channels */
      81             : #ifdef FIX_1320_STACK_CPE_DECODER
      82     1261158 :     float *res_buf = NULL;
      83             : #else
      84             :     float res_buf[STEREO_DFT_N_8k];
      85             : #endif
      86             :     CPE_DEC_HANDLE hCPE;
      87             :     Decoder_State **sts;
      88             :     STEREO_DFT_CONFIG_DATA_HANDLE hConfigDft;
      89             :     int32_t ivas_total_brate;
      90             :     ivas_error error;
      91             :     int32_t cpe_brate;
      92             :     int32_t element_brate_ref;
      93             : 
      94     1261158 :     error = IVAS_ERR_OK;
      95             : 
      96     1261158 :     push_wmops( "ivas_cpe_dec" );
      97             : 
      98     1261158 :     ivas_total_brate = st_ivas->hDecoderConfig->ivas_total_brate;
      99             : 
     100     1261158 :     hCPE = st_ivas->hCPE[cpe_id];
     101     1261158 :     sts = hCPE->hCoreCoder;
     102             : 
     103     1261158 :     last_core = sts[0]->last_core;
     104     1261158 :     last_bwidth = sts[0]->last_bwidth;
     105             : 
     106     1261158 :     sts[0]->BER_detect |= st_ivas->BER_detect;
     107     1261158 :     sts[1]->BER_detect |= st_ivas->BER_detect;
     108             : 
     109     1261158 :     element_brate_ref = hCPE->element_brate;
     110             : 
     111             :     /*------------------------------------------------------------------*
     112             :      * Read stereo technology info & audio bandwidth
     113             :      *-----------------------------------------------------------------*/
     114             : 
     115     1261158 :     stereo_mode_combined_format_dec( st_ivas, hCPE );
     116             : 
     117     1261158 :     read_stereo_mode_and_bwidth( hCPE, st_ivas );
     118             : 
     119             :     /*----------------------------------------------------------------*
     120             :      * dynamically allocate data structures depending on the actual stereo mode
     121             :      *----------------------------------------------------------------*/
     122             : 
     123     1261158 :     if ( ( error = stereo_memory_dec( 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 )
     124             :     {
     125           0 :         return error;
     126             :     }
     127             : 
     128     1261158 :     hConfigDft = NULL;
     129     1261158 :     if ( hCPE->hStereoDft != NULL )
     130             :     {
     131      197886 :         hConfigDft = hCPE->hStereoDft->hConfig;
     132             :     }
     133             : 
     134             :     /*------------------------------------------------------------------*
     135             :      * Initialization
     136             :      *-----------------------------------------------------------------*/
     137             : 
     138     1261158 :     n_channels = CPE_CHANNELS;
     139     1261158 :     if ( hCPE->element_mode == IVAS_CPE_DFT )
     140             :     {
     141      179058 :         n_channels = 1; /* in DFT stereo, only M channel is coded */
     142             :     }
     143             : 
     144     1261158 :     tdm_ratio_idx = LRTD_STEREO_RIGHT_IS_PRIM;
     145             : 
     146     3604416 :     for ( n = 0; n < n_channels; n++ )
     147             :     {
     148     2343258 :         sts[n]->idchan = n;
     149     2343258 :         sts[n]->element_mode = hCPE->element_mode;
     150             : 
     151     2343258 :         if ( !st_ivas->bfi )
     152             :         {
     153     2297607 :             sts[n]->tdm_LRTD_flag = 0;
     154             :         }
     155             : 
     156             : #ifdef DEBUGGING
     157             :         sts[n]->id_element = cpe_id + st_ivas->nSCE;
     158             : #endif
     159             :         /* TD stereo parameters */
     160     2343258 :         if ( hCPE->hStereoTD != NULL )
     161             :         {
     162       22680 :             hCPE->hStereoTD->tdm_lp_reuse_flag = 0;
     163       22680 :             hCPE->hStereoTD->tdm_low_rate_mode = 0;
     164       22680 :             hCPE->hStereoTD->tdm_Pitch_reuse_flag = 0;
     165             :         }
     166             :     }
     167             : 
     168             :     /*----------------------------------------------------------------*
     169             :      * Resets/updates in case of stereo switching
     170             :      *----------------------------------------------------------------*/
     171             : 
     172     1261158 :     stereo_switching_dec( hCPE, ivas_total_brate );
     173             : 
     174             :     /*----------------------------------------------------------------*
     175             :      * Configuration of stereo decoder
     176             :      *----------------------------------------------------------------*/
     177             : 
     178             :     /* Force to MODE1 in IVAS */
     179     3604416 :     for ( n = 0; n < n_channels; n++ )
     180             :     {
     181     2343258 :         sts[n]->codec_mode = MODE1;
     182             :     }
     183             : 
     184     1261158 :     if ( hCPE->element_mode != IVAS_CPE_MDCT && ( hCPE->element_brate != hCPE->last_element_brate || hCPE->last_element_mode != hCPE->element_mode || sts[0]->ini_frame == 0 || ( ivas_total_brate != st_ivas->hDecoderConfig->last_ivas_total_brate ) ) )
     185             :     {
     186       13515 :         if ( st_ivas->hQMetaData != NULL && ivas_total_brate > IVAS_SID_5k2 )
     187             :         {
     188        3588 :             if ( st_ivas->ivas_format == MASA_ISM_FORMAT )
     189             :             {
     190        1482 :                 stereo_dft_config( hConfigDft, (int32_t) ( 0.7f * st_ivas->hQMetaData->bits_frame_nominal * FRAMES_PER_SEC ), &sts[0]->bits_frame_nominal, &sts[1]->bits_frame_nominal );
     191             :             }
     192             :             else
     193             :             {
     194        2106 :                 stereo_dft_config( hConfigDft, st_ivas->hQMetaData->bits_frame_nominal * FRAMES_PER_SEC, &sts[0]->bits_frame_nominal, &sts[1]->bits_frame_nominal );
     195             :             }
     196             :         }
     197             :         else
     198             :         {
     199             :             /* Note: This only works for stereo operation. If DTX would be applied for multiple CPEs a different bitrate signaling is needed */
     200        9927 :             if ( ivas_total_brate <= IVAS_SID_5k2 )
     201             :             {
     202        8163 :                 stereo_dft_config( hConfigDft, ivas_total_brate, &sts[0]->bits_frame_nominal, &sts[1]->bits_frame_nominal );
     203             :             }
     204             :             else
     205             :             {
     206        1764 :                 stereo_dft_config( hConfigDft, hCPE->element_brate, &sts[0]->bits_frame_nominal, &sts[1]->bits_frame_nominal );
     207             :             }
     208             :         }
     209             :     }
     210             : 
     211     1261158 :     if ( hCPE->element_mode == IVAS_CPE_TD )
     212             :     {
     213       11340 :         if ( hCPE->hStereoTD->tdm_LRTD_flag )
     214             :         {
     215       10965 :             sts[0]->bits_frame_nominal = (int16_t) ( ( hCPE->element_brate >> 1 ) / FRAMES_PER_SEC );
     216       10965 :             sts[1]->bits_frame_nominal = (int16_t) ( ( hCPE->element_brate >> 1 ) / FRAMES_PER_SEC );
     217             :         }
     218             :         else
     219             :         {
     220         375 :             stereo_dft_config( NULL, hCPE->element_brate, &sts[0]->bits_frame_nominal, &sts[1]->bits_frame_nominal );
     221             :         }
     222             :     }
     223             : 
     224             :     /*----------------------------------------------------------------*
     225             :      * Set bitrates per channel
     226             :      * Set bitstream buffers per channel
     227             :      *----------------------------------------------------------------*/
     228             : 
     229     1261158 :     if ( hCPE->element_mode == IVAS_CPE_DFT )
     230             :     {
     231      179058 :         if ( !st_ivas->bfi )
     232             :         {
     233             :             /* Update DFT Stereo memories */
     234      174639 :             stereo_dft_dec_update( hCPE->hStereoDft, output_frame, 0 );
     235             : 
     236      174639 :             if ( st_ivas->ivas_format == MASA_FORMAT && ivas_total_brate <= IVAS_SID_5k2 )
     237             :             {
     238        1590 :                 if ( ivas_total_brate == FRAME_NO_DATA )
     239             :                 {
     240        1368 :                     hCPE->hCoreCoder[n]->core_brate = ivas_total_brate;
     241        1368 :                     hCPE->hCoreCoder[0]->total_brate = ivas_total_brate;
     242             :                 }
     243             :                 else
     244             :                 {
     245         222 :                     hCPE->hCoreCoder[n]->core_brate = SID_2k40;
     246             :                 }
     247             :             }
     248             : 
     249             :             /* read DFT Stereo side info */
     250      174639 :             nb_bits = (int16_t) ( ( hCPE->element_brate ) / FRAMES_PER_SEC - 0.8f * sts[0]->bits_frame_nominal );
     251      174639 :             cpe_brate = st_ivas->hCPE[0]->element_brate;
     252      174639 :             if ( st_ivas->ivas_format == MASA_ISM_FORMAT )
     253             :             {
     254        8217 :                 sts[1]->bit_stream = sts[0]->bit_stream + cpe_brate / FRAMES_PER_SEC - 1 - nb_bits_metadata;
     255        8217 :                 sts[1]->bit_stream += hCPE->brate_surplus / FRAMES_PER_SEC;
     256             :             }
     257             :             else
     258             :             {
     259      166422 :                 sts[1]->bit_stream = sts[0]->bit_stream + ivas_total_brate / FRAMES_PER_SEC - 1 - nb_bits_metadata;
     260             :             }
     261             : 
     262      174639 :             if ( ivas_total_brate == IVAS_SID_5k2 )
     263             :             {
     264        4287 :                 nb_bits -= SID_FORMAT_NBITS;
     265        4287 :                 sts[1]->bit_stream -= SID_FORMAT_NBITS;
     266             :                 /* set total bitrate of Stereo CNG parameters for BER detection */
     267        4287 :                 sts[1]->total_brate = IVAS_SID_5k2 - SID_2k40;
     268             :             }
     269             : 
     270      174639 :             if ( ( ( st_ivas->ivas_format == MASA_FORMAT && ivas_total_brate < MASA_STEREO_MIN_BITRATE ) || ( st_ivas->ivas_format == MASA_ISM_FORMAT && cpe_brate < MASA_STEREO_MIN_BITRATE ) ) && ivas_total_brate > IVAS_SID_5k2 )
     271             :             {
     272       16614 :                 sts[0]->total_brate = hCPE->element_brate; /* Only mono downmix was transmitted in this case */
     273             :             }
     274             :             else
     275             :             {
     276             : #ifdef FIX_1320_STACK_CPE_DECODER
     277      158025 :                 res_buf = outputHB[0]; /* note: temporarily reused buffer */
     278             : 
     279             : #endif
     280      158025 :                 if ( st_ivas->ivas_format == MASA_FORMAT || st_ivas->ivas_format == MASA_ISM_FORMAT )
     281             :                 {
     282       18597 :                     nb_bits -= nb_bits_metadata;
     283       18597 :                     if ( hCPE->brate_surplus < 0 )
     284             :                     {
     285        2805 :                         nb_bits += (int16_t) ( hCPE->brate_surplus / FRAMES_PER_SEC );
     286             :                     }
     287             :                 }
     288             : 
     289      158025 :                 stereo_dft_dec_read_BS( ivas_total_brate, hCPE->element_brate, &sts[0]->total_brate, sts[1], hCPE->hStereoDft, sts[0]->bwidth, output_frame, res_buf, &nb_bits, hCPE->hStereoCng->coh, st_ivas->ivas_format );
     290             :             }
     291             : 
     292             :             /* subtract metadata bitbudget */
     293      174639 :             sts[0]->total_brate -= ( nb_bits_metadata * FRAMES_PER_SEC );
     294             : 
     295             :             /* subtract bit-rate for combined format coding */
     296      174639 :             if ( st_ivas->ivas_format == MASA_ISM_FORMAT && ( st_ivas->ism_mode == ISM_MASA_MODE_MASA_ONE_OBJ || st_ivas->ism_mode == ISM_MASA_MODE_PARAM_ONE_OBJ || st_ivas->ism_mode == ISM_MASA_MODE_DISC ) )
     297             :             {
     298        8217 :                 sts[0]->total_brate += hCPE->brate_surplus;
     299             :             }
     300             :         }
     301             :         else
     302             :         {
     303        4419 :             hCPE->hStereoDft->sg_mem_corrupt = 1;
     304             :         }
     305             :     }
     306     1082100 :     else if ( hCPE->element_mode == IVAS_CPE_TD )
     307             :     {
     308             :         /* signal bitrate for BW selection in the SCh */
     309       11340 :         sts[0]->bits_frame_channel = 0;
     310       11340 :         sts[1]->bits_frame_channel = (int16_t) ( hCPE->element_brate / FRAMES_PER_SEC );
     311       11340 :         sts[1]->bits_frame_channel += (int16_t) ( hCPE->brate_surplus / FRAMES_PER_SEC );
     312       11340 :         if ( st_ivas->hQMetaData != NULL )
     313             :         {
     314         180 :             sts[1]->bits_frame_channel -= st_ivas->hQMetaData->metadata_max_bits;
     315             :         }
     316             :     }
     317     1070760 :     else if ( hCPE->element_mode == IVAS_CPE_MDCT )
     318             :     {
     319             :         /* compute bit-rate surplus per channel in combined format coding */
     320             :         int32_t brate_surplus[CPE_CHANNELS];
     321     1070760 :         if ( st_ivas->ism_mode == ISM_MASA_MODE_MASA_ONE_OBJ || st_ivas->ism_mode == ISM_MASA_MODE_PARAM_ONE_OBJ || st_ivas->ism_mode == ISM_MASA_MODE_DISC )
     322             :         {
     323       12846 :             brate_surplus[0] = ( ( hCPE->brate_surplus / FRAMES_PER_SEC ) >> 1 ) * FRAMES_PER_SEC;
     324       12846 :             brate_surplus[1] = hCPE->brate_surplus - brate_surplus[0];
     325             :         }
     326             : 
     327     1070760 :         if ( is_DTXrate( ivas_total_brate ) == 1 && ( sts[0]->first_CNG == 0 || sts[1]->first_CNG == 0 ) )
     328             :         {
     329          90 :             if ( ( error = initMdctStereoDtxData( hCPE ) ) != IVAS_ERR_OK )
     330             :             {
     331           0 :                 return error;
     332             :             }
     333             :         }
     334             : 
     335             :         /* this is just for initialization, the true values of "total_brate" and "bits_frame_channel" are set later */
     336     3212280 :         for ( n = 0; n < n_channels; n++ )
     337             :         {
     338     2141520 :             if ( ivas_total_brate == IVAS_SID_5k2 )
     339             : 
     340             :             {
     341        3408 :                 sts[n]->total_brate = SID_2k40;
     342        3408 :                 sts[1]->bit_stream = sts[0]->bit_stream + SID_2k40 / FRAMES_PER_SEC;
     343             :             }
     344             :             else
     345             :             {
     346             :                 /*total bitrate must be set to the element bitrate to avoid false BER if bits read are larger than half the bitrate*/
     347     2138112 :                 sts[n]->total_brate = hCPE->element_brate;
     348             :             }
     349     2141520 :             sts[n]->bits_frame_nominal = (int16_t) ( sts[n]->total_brate / FRAMES_PER_SEC );
     350     2141520 :             sts[n]->bits_frame_channel = (int16_t) ( ( hCPE->element_brate / FRAMES_PER_SEC ) / n_channels );
     351             : 
     352             :             /* subtract bit-rate for combined format coding */
     353     2141520 :             if ( st_ivas->ism_mode == ISM_MASA_MODE_MASA_ONE_OBJ || st_ivas->ism_mode == ISM_MASA_MODE_PARAM_ONE_OBJ || st_ivas->ism_mode == ISM_MASA_MODE_DISC )
     354             :             {
     355       25692 :                 sts[n]->bits_frame_channel += (int16_t) ( brate_surplus[n] / FRAMES_PER_SEC );
     356       25692 :                 sts[n]->total_brate += brate_surplus[n];
     357             :             }
     358             :         }
     359             : 
     360     1070760 :         if ( !st_ivas->hMCT )
     361             :         {
     362      259752 :             if ( st_ivas->ivas_format == SBA_FORMAT && ivas_total_brate == IVAS_SID_5k2 )
     363             :             {
     364        1503 :                 for ( n = 0; n < n_channels; n++ )
     365             :                 {
     366        1002 :                     sts[n]->bits_frame_channel -= nb_bits_metadata / n_channels;
     367             :                 }
     368             :             }
     369             :             else
     370             :             {
     371             :                 /* subtract metadata bitbudget */
     372      259251 :                 sts[0]->bits_frame_channel -= nb_bits_metadata;
     373             :             }
     374             :         }
     375             :     }
     376             : 
     377             :     /*----------------------------------------------------------------*
     378             :      * Core codec configuration
     379             :      *----------------------------------------------------------------*/
     380             : 
     381     3604416 :     for ( n = 0; n < n_channels; n++ )
     382             :     {
     383             :         /* set ACELP12k8 / ACELP16k flag for flexible ACELP core */
     384     2343258 :         sts[n]->flag_ACELP16k = set_ACELP_flag( hCPE->element_mode, hCPE->element_brate, sts[n]->total_brate, n, ( hCPE->hStereoTD != NULL ? hCPE->hStereoTD->tdm_LRTD_flag : 0 ), sts[n]->bwidth, sts[n]->cng_type );
     385             :     }
     386             : 
     387     3604416 :     for ( n = 0; n < n_channels; n++ )
     388             :     {
     389             :         /* set VAD flag */
     390     2343258 :         if ( is_DTXrate( ivas_total_brate ) == 1 )
     391             :         {
     392       55782 :             sts[n]->VAD = 0;
     393       55782 :             sts[n]->active_cnt = 0;
     394       55782 :             if ( sts[1] != NULL )
     395             :             {
     396       55782 :                 sts[1]->active_cnt = 0;
     397             :             }
     398             :         }
     399             :         else
     400             :         {
     401     2287476 :             sts[n]->VAD = 1;
     402     2287476 :             sts[n]->active_cnt++;
     403     2287476 :             sts[n]->active_cnt = min( sts[n]->active_cnt, 100 );
     404             :         }
     405             : 
     406             :         /* set CNA flag */
     407     2343258 :         if ( ( hCPE->element_mode == IVAS_CPE_DFT && hCPE->element_brate <= CNA_MAX_BRATE_DFT_STEREO ) || hCPE->element_brate <= CNA_MAX_BRATE_STEREO )
     408             :         {
     409      206328 :             sts[n]->flag_cna = 1;
     410             :         }
     411             :         else
     412             :         {
     413     2136930 :             sts[n]->flag_cna = 0;
     414             :         }
     415             :     }
     416             : 
     417             :     /* configure TD stereo decoder */
     418     1261158 :     if ( hCPE->element_mode == IVAS_CPE_TD )
     419             :     {
     420       11340 :         if ( !st_ivas->bfi )
     421             :         {
     422       10956 :             tdm_configure_dec( st_ivas->ivas_format, st_ivas->ism_mode, hCPE, &tdm_ratio_idx, nb_bits_metadata );
     423             : 
     424       10956 :             sts[1]->bit_stream = sts[0]->bit_stream + ( sts[0]->total_brate / FRAMES_PER_SEC );
     425             :         }
     426             :         else
     427             :         {
     428         384 :             sts[1]->coder_type = sts[1]->last_coder_type;
     429         384 :             tdm_ratio_idx = hCPE->hStereoTD->tdm_last_ratio_idx;
     430             :         }
     431             :     }
     432             : 
     433             :     /*----------------------------------------------------------------*
     434             :      * Core Decoder
     435             :      *----------------------------------------------------------------*/
     436             : 
     437     1261158 :     if ( hCPE->element_mode != IVAS_CPE_DFT || ( hCPE->nchan_out == 1 && hConfigDft->res_cod_mode == STEREO_DFT_RES_COD_OFF ) )
     438             :     {
     439     1133439 :         if ( ( error = ivas_core_dec( st_ivas, NULL, hCPE, st_ivas->hMCT, n_channels, output, outputHB, NULL, st_ivas->sba_dirac_stereo_flag ) ) != IVAS_ERR_OK )
     440             :         {
     441           0 :             return error;
     442             :         }
     443             :     }
     444             : 
     445     1261158 :     if ( st_ivas->hMCT )
     446             :     {
     447      811008 :         pop_wmops();
     448             : 
     449      811008 :         return error;
     450             :     }
     451             : 
     452             :     /*----------------------------------------------------------------*
     453             :      * Stereo decoder & upmixing
     454             :      *----------------------------------------------------------------*/
     455             : 
     456      450150 :     if ( hCPE->element_mode == IVAS_CPE_DFT && !( hCPE->nchan_out == 1 && hConfigDft->res_cod_mode == STEREO_DFT_RES_COD_OFF ) )
     457             :     {
     458             : #ifdef FIX_1320_STACK_CPE_DECODER
     459      127719 :         if ( ( error = stereo_dft_dec_main( hCPE, ivas_total_brate, n_channels, res_buf, output, outputHB, output_frame ) ) != IVAS_ERR_OK )
     460             :         {
     461           0 :             return error;
     462             :         }
     463             : #else
     464             :         float DFT[CPE_CHANNELS][STEREO_DFT_BUF_MAX];
     465             : 
     466             :         /* core decoder */
     467             :         if ( ( error = ivas_core_dec( NULL, NULL, hCPE, st_ivas->hMCT, n_channels, output, outputHB, DFT, 0 ) ) != IVAS_ERR_OK )
     468             :         {
     469             :             return error;
     470             :         }
     471             : 
     472             :         /* DFT Stereo residual decoding */
     473             :         if ( hCPE->hStereoDft->res_cod_band_max > 0 && !st_ivas->bfi )
     474             :         {
     475             :             stereo_dft_dec_res( hCPE, res_buf, output[1] );
     476             : 
     477             :             stereo_dft_dec_analyze( hCPE, output[1], DFT, 1, L_FRAME8k, output_frame, DFT_STEREO_DEC_ANA_LB, 0, 0 );
     478             :         }
     479             : 
     480             :         /* DFT stereo CNG */
     481             :         stereo_dtf_cng( hCPE, ivas_total_brate, DFT, output_frame );
     482             : 
     483             :         /* decoding */
     484             :         if ( hCPE->nchan_out == 1 )
     485             :         {
     486             :             stereo_dft_unify_dmx( hCPE->hStereoDft, sts[0], DFT, hCPE->input_mem[1], hCPE->hStereoCng->prev_sid_nodata );
     487             :         }
     488             :         else
     489             :         {
     490             :             stereo_dft_dec( hCPE->hStereoDft, sts[0], DFT, hCPE->input_mem[1], hCPE->hStereoCng, 0, 0, 0, 0, 0, 0, MAX_PARAM_SPATIAL_SUBFRAMES );
     491             :         }
     492             : 
     493             :         /* synthesis iFFT */
     494             :         for ( n = 0; n < hCPE->nchan_out; n++ )
     495             :         {
     496             :             stereo_dft_dec_synthesize( hCPE, DFT, n, output[n], output_frame );
     497             :         }
     498             : #endif
     499             :     }
     500      322431 :     else if ( hCPE->element_mode == IVAS_CPE_TD )
     501             :     {
     502       11340 :         if ( hCPE->last_element_mode != IVAS_CPE_TD && sts[0]->tdm_LRTD_flag )
     503             :         {
     504         177 :             hCPE->hStereoTD->tdm_last_ratio_idx = tdm_ratio_idx;
     505             :         }
     506             : 
     507             :         /* TD stereo upmixing */
     508       11340 :         stereo_tdm_combine( hCPE, output[0], output[1], output_frame, 0, tdm_ratio_idx );
     509       11340 :         if ( sts[0]->tdm_LRTD_flag )
     510             :         {
     511       10965 :             stereo_tdm_combine( hCPE, outputHB[0], outputHB[1], output_frame, 1, tdm_ratio_idx );
     512             :         }
     513             : 
     514       11340 :         hCPE->hStereoCng->last_tdm_idx = hCPE->hStereoTD->tdm_last_ratio_idx;
     515       11340 :         hCPE->hStereoTD->tdm_last_ratio_idx = tdm_ratio_idx;
     516             : 
     517       11340 :         if ( hCPE->nchan_out == 1 )
     518             :         {
     519             :             /* Scale the Right channel with the gain */
     520        4059 :             stereo_tca_scale_R_channel( hCPE, output[1], output_frame );
     521             : 
     522             :             /* stereo to mono downmix */
     523     2434779 :             for ( i = 0; i < output_frame; i++ )
     524             :             {
     525     2430720 :                 output[0][i] = ( output[0][i] + output[1][i] ) * 0.5f;
     526             :             }
     527             :         }
     528             :     }
     529             : 
     530             :     /*----------------------------------------------------------------*
     531             :      * Update parameters for stereo CNA
     532             :      *----------------------------------------------------------------*/
     533             : 
     534      450150 :     stereo_cna_update_params( hCPE, output, output_frame, tdm_ratio_idx );
     535             : 
     536             :     /*----------------------------------------------------------------*
     537             :      * Synthesis synchronization between CPE modes
     538             :      *----------------------------------------------------------------*/
     539             : 
     540      450150 :     if ( !st_ivas->sba_dirac_stereo_flag )
     541             :     {
     542      446139 :         synchro_synthesis( ivas_total_brate, hCPE, output, output_frame, 0 );
     543             :     }
     544             : 
     545      450150 :     if ( hCPE->element_mode == IVAS_CPE_MDCT && hCPE->nchan_out == 1 && ( is_DTXrate( ivas_total_brate ) == 0 || ( is_DTXrate( ivas_total_brate ) == 1 && is_DTXrate( st_ivas->hDecoderConfig->last_ivas_total_brate ) == 0 ) ) )
     546             :     {
     547       17754 :         applyDmxMdctStereo( hCPE, output, output_frame );
     548             :     }
     549             : 
     550             : #ifndef DEBUG_STEREO_DFT_OUTRESPRED
     551             :     /*----------------------------------------------------------------*
     552             :      * IC-BWE: output LB and HB mix in ACELP mode
     553             :      *----------------------------------------------------------------*/
     554             : 
     555             : #ifndef DEBUG_STEREO_DFT_NOCORE
     556      450150 :     stereo_icBWE_decproc( hCPE, output, outputHB, last_core, last_bwidth, output_frame );
     557             : #endif
     558             : 
     559      450150 :     smooth_dft2td_transition( hCPE, output, output_frame );
     560             : 
     561             :     /*----------------------------------------------------------------*
     562             :      * Temporal ICA, stereo adjustment and upmix
     563             :      *----------------------------------------------------------------*/
     564             : 
     565      450150 :     stereo_tca_dec( hCPE, output, output_frame );
     566             : #endif
     567             : 
     568             :     /*----------------------------------------------------------------*
     569             :      * Common Stereo updates
     570             :      *----------------------------------------------------------------*/
     571             : 
     572      450150 :     hCPE->last_element_brate = hCPE->element_brate;
     573      450150 :     hCPE->last_element_mode = hCPE->element_mode;
     574             : 
     575      450150 :     if ( st_ivas->ivas_format == MASA_ISM_FORMAT )
     576             :     {
     577       21252 :         hCPE->element_brate = element_brate_ref;
     578             :     }
     579             : 
     580      450150 :     if ( hCPE->element_mode == IVAS_CPE_DFT || hCPE->element_mode == IVAS_CPE_TD )
     581             :     {
     582      190398 :         stereo_cng_dec_update( hCPE, ivas_total_brate );
     583             :     }
     584             : 
     585      450150 :     st_ivas->BER_detect |= sts[0]->BER_detect;
     586      450150 :     st_ivas->BER_detect |= sts[1]->BER_detect;
     587             : 
     588             : #ifdef DEBUG_MODE_INFO
     589             :     {
     590             :         float tmpF = hCPE->element_brate / 1000.0f;
     591             : 
     592             :         n = 1;
     593             :         if ( st_ivas->ini_frame == 0 && frame > 0 )
     594             :         {
     595             :             /* in case first frame(s) is/are lost, write info several times */
     596             :             n = (int16_t) frame - st_ivas->ini_frame + 1;
     597             :         }
     598             : 
     599             :         for ( i = 0; i < n; i++ )
     600             :         {
     601             :             dbgwrite( &tmpF, sizeof( float ), 1, output_frame, fname( debug_dir, "element_brate", 0, cpe_id, DEC ) );
     602             :             dbgwrite( &hCPE->element_mode, sizeof( int16_t ), 1, output_frame, fname( debug_dir, "element_mode", 0, cpe_id, DEC ) );
     603             : 
     604             :             for ( int16_t j = 0; j < CPE_CHANNELS; j++ )
     605             :             {
     606             :                 dbgwrite( output[j], sizeof( float ), output_frame, 1, fname( debug_dir, "output.cpe", j, cpe_id, DEC ) );
     607             :             }
     608             : 
     609             :             if ( st_ivas->ivas_format != MASA_ISM_FORMAT )
     610             :             {
     611             :                 tmpF = 0;
     612             :                 dbgwrite( &tmpF, sizeof( float ), 1, output_frame, fname( debug_dir, "output.sce", 0, cpe_id, DEC ) );
     613             :                 dbgwrite( &tmpF, sizeof( float ), 1, output_frame, fname( debug_dir, "output.mct", 0, cpe_id, DEC ) );
     614             :             }
     615             :         }
     616             :     }
     617             : #endif
     618             : 
     619      450150 :     pop_wmops();
     620      450150 :     return error;
     621             : }
     622             : 
     623             : #ifdef FIX_1320_STACK_CPE_DECODER
     624             : 
     625             : /*-------------------------------------------------------------------------
     626             :  * stereo_dft_dec_main()
     627             :  *
     628             :  * DFT decoder main function
     629             :  *-------------------------------------------------------------------------*/
     630             : 
     631      127719 : static ivas_error stereo_dft_dec_main(
     632             :     CPE_DEC_HANDLE hCPE,            /* i/o: CPE decoder structure               */
     633             :     const int32_t ivas_total_brate, /* i  : IVAS total bitrate                  */
     634             :     const int16_t n_channels,       /* i  : number of channels to be decoded    */
     635             :     float *p_res_buf,               /* i  : DFT stereo residual S signal        */
     636             :     float *output[],                /* o  : output synthesis signal             */
     637             :     float outputHB[][L_FRAME48k],   /* o  : output HB synthesis signal          */
     638             :     const int16_t output_frame      /* i  : output frame length per channel     */
     639             : )
     640             : {
     641             :     float DFT[CPE_CHANNELS][STEREO_DFT_BUF_MAX];
     642             :     int16_t n;
     643             :     Decoder_State *st0;
     644             :     ivas_error error;
     645             : 
     646      127719 :     st0 = hCPE->hCoreCoder[0];
     647             : 
     648             :     /* copy from temporary buffer */
     649      127719 :     if ( hCPE->hStereoDft->res_cod_band_max > 0 && !st0->bfi )
     650             :     {
     651       50169 :         mvr2r( p_res_buf, DFT[1], STEREO_DFT_N_8k );
     652             :     }
     653             : 
     654             :     /* core decoder */
     655      127719 :     if ( ( error = ivas_core_dec( NULL, NULL, hCPE, NULL, n_channels, output, outputHB, DFT, 0 ) ) != IVAS_ERR_OK )
     656             :     {
     657           0 :         return error;
     658             :     }
     659             : 
     660             :     /* DFT Stereo residual decoding */
     661      127719 :     if ( hCPE->hStereoDft->res_cod_band_max > 0 && !st0->bfi )
     662             :     {
     663       50169 :         stereo_dft_dec_res( hCPE, DFT[1] /*res_buf*/, output[1] );
     664             : 
     665       50169 :         stereo_dft_dec_analyze( hCPE, output[1], DFT, 1, L_FRAME8k, output_frame, DFT_STEREO_DEC_ANA_LB, 0, 0 );
     666             :     }
     667             : 
     668             :     /* DFT stereo CNG */
     669      127719 :     stereo_dtf_cng( hCPE, ivas_total_brate, DFT, output_frame );
     670             : 
     671             :     /* decoding */
     672      127719 :     if ( hCPE->nchan_out == 1 )
     673             :     {
     674       16026 :         stereo_dft_unify_dmx( hCPE->hStereoDft, st0, DFT, hCPE->input_mem[1], hCPE->hStereoCng->prev_sid_nodata );
     675             :     }
     676             :     else
     677             :     {
     678      111693 :         stereo_dft_dec( hCPE->hStereoDft, st0, DFT, hCPE->input_mem[1], hCPE->hStereoCng, 0, 0, 0, 0, 0, 0, MAX_PARAM_SPATIAL_SUBFRAMES );
     679             :     }
     680             : 
     681             :     /* synthesis iFFT */
     682      367131 :     for ( n = 0; n < hCPE->nchan_out; n++ )
     683             :     {
     684      239412 :         stereo_dft_dec_synthesize( hCPE, DFT, n, output[n], output_frame );
     685             :     }
     686             : 
     687      127719 :     return IVAS_ERR_OK;
     688             : }
     689             : 
     690             : #endif
     691             : 
     692             : /*-------------------------------------------------------------------------
     693             :  * create_cpe_dec()
     694             :  *
     695             :  * Create, allocate and initialize IVAS decoder CPE handle
     696             :  *-------------------------------------------------------------------------*/
     697             : 
     698        8883 : ivas_error create_cpe_dec(
     699             :     Decoder_Struct *st_ivas,    /* i/o: IVAS decoder structure      */
     700             :     const int16_t cpe_id,       /* i  : CPE # identifier            */
     701             :     const int32_t element_brate /* i  : element bitrate             */
     702             : )
     703             : {
     704             :     int16_t i, n;
     705             :     CPE_DEC_HANDLE hCPE;
     706             :     Decoder_State *st;
     707             :     int32_t output_Fs;
     708             :     ivas_error error;
     709             :     int32_t cpe_brate;
     710             : 
     711        8883 :     error = IVAS_ERR_OK;
     712             : 
     713             :     /*-----------------------------------------------------------------*
     714             :      * Allocate CPE handle
     715             :      *-----------------------------------------------------------------*/
     716             : 
     717        8883 :     if ( ( hCPE = (CPE_DEC_HANDLE) malloc( sizeof( CPE_DEC_DATA ) ) ) == NULL )
     718             :     {
     719           0 :         return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for CPE\n" ) );
     720             :     }
     721             : 
     722             :     /*-----------------------------------------------------------------*
     723             :      * Initialization - general parameters
     724             :      *-----------------------------------------------------------------*/
     725             : 
     726        8883 :     output_Fs = st_ivas->hDecoderConfig->output_Fs;
     727             : 
     728        8883 :     hCPE->cpe_id = cpe_id;
     729             : 
     730        8883 :     hCPE->element_brate = element_brate;
     731        8883 :     hCPE->last_element_brate = hCPE->element_brate;
     732        8883 :     hCPE->element_mode = st_ivas->element_mode_init;
     733        8883 :     hCPE->last_element_mode = st_ivas->element_mode_init;
     734             : 
     735        8883 :     hCPE->hStereoDft = NULL;
     736        8883 :     hCPE->hStereoDftDmx = NULL;
     737        8883 :     hCPE->hStereoTD = NULL;
     738        8883 :     hCPE->hStereoMdct = NULL;
     739        8883 :     hCPE->hStereoTCA = NULL;
     740        8883 :     hCPE->hStereoICBWE = NULL;
     741        8883 :     hCPE->hStereoCng = NULL;
     742             : 
     743        8883 :     hCPE->stereo_switching_counter = 10;
     744        8883 :     hCPE->NbFrameMod = 7;
     745        8883 :     hCPE->lt_es_em = 0.0f;
     746             : 
     747             :     /* Note: nchan_out is considered to be related to the structure. This is nchan_out for CPE and for MASA_format is always 2. */
     748        8883 :     if ( st_ivas->ivas_format == SBA_FORMAT || st_ivas->ivas_format == MASA_FORMAT || st_ivas->ivas_format == MASA_ISM_FORMAT || st_ivas->ivas_format == MC_FORMAT )
     749             :     {
     750        6357 :         hCPE->nchan_out = CPE_CHANNELS;
     751             :     }
     752             :     else
     753             :     {
     754        2526 :         hCPE->nchan_out = min( CPE_CHANNELS, st_ivas->hDecoderConfig->nchan_out );
     755             :     }
     756             : 
     757        8883 :     if ( st_ivas->ivas_format == MASA_ISM_FORMAT && ( st_ivas->ism_mode == ISM_MASA_MODE_MASA_ONE_OBJ || st_ivas->ism_mode == ISM_MASA_MODE_PARAM_ONE_OBJ || st_ivas->ism_mode == ISM_MASA_MODE_DISC ) )
     758             :     {
     759         111 :         cpe_brate = element_brate;
     760             :     }
     761             :     else
     762             :     {
     763        8772 :         cpe_brate = st_ivas->hDecoderConfig->ivas_total_brate;
     764             :     }
     765             : 
     766        8883 :     if ( ( ( st_ivas->ivas_format == MASA_FORMAT && st_ivas->hDecoderConfig->ivas_total_brate < MASA_STEREO_MIN_BITRATE ) ||
     767        8826 :            ( st_ivas->ivas_format == MASA_ISM_FORMAT && cpe_brate < MASA_STEREO_MIN_BITRATE ) ) &&
     768          84 :          st_ivas->hDecoderConfig->ivas_total_brate > IVAS_SID_5k2 )
     769             :     {
     770          84 :         hCPE->nchan_out = 1;
     771             :     }
     772             : 
     773       26649 :     for ( n = 0; n < CPE_CHANNELS; n++ )
     774             :     {
     775       17766 :         set_f( hCPE->prev_hb_synth[n], 0, NS2SA( output_Fs, IVAS_DEC_DELAY_NS - DELAY_BWE_TOTAL_NS ) );
     776       17766 :         set_f( hCPE->prev_synth[n], 0, NS2SA( output_Fs, IVAS_DEC_DELAY_NS - STEREO_DFT32MS_OVL_NS ) );
     777             :     }
     778             : 
     779        8883 :     hCPE->brate_surplus = 0;
     780             : 
     781             :     /*-----------------------------------------------------------------*
     782             :      * DFT stereo I/O Buffers: allocate and initialize
     783             :      *-----------------------------------------------------------------*/
     784             : 
     785       26649 :     for ( i = 0; i < CPE_CHANNELS; i++ )
     786             :     {
     787       17766 :         if ( st_ivas->ivas_format == STEREO_FORMAT || st_ivas->ivas_format == MASA_FORMAT || st_ivas->ivas_format == MASA_ISM_FORMAT ||
     788       16830 :              ( st_ivas->ivas_format == MC_FORMAT && st_ivas->mc_mode == MC_MODE_MCMASA ) || st_ivas->sba_dirac_stereo_flag )
     789             :         {
     790        2382 :             if ( ( hCPE->input_mem[i] = (float *) malloc( sizeof( float ) * NS2SA( output_Fs, STEREO_DFT32MS_OVL_NS ) ) ) == NULL )
     791             :             {
     792           0 :                 return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DFT stereo memory\n" ) );
     793             :             }
     794        2382 :             set_zero( hCPE->input_mem[i], NS2SA( output_Fs, STEREO_DFT32MS_OVL_NS ) );
     795             : 
     796        2382 :             if ( ( hCPE->input_mem_LB[i] = (float *) malloc( sizeof( float ) * STEREO_DFT32MS_OVL_16k ) ) == NULL )
     797             :             {
     798           0 :                 return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DFT stereo memory\n" ) );
     799             :             }
     800        2382 :             set_zero( hCPE->input_mem_LB[i], STEREO_DFT32MS_OVL_16k );
     801             : 
     802        2382 :             if ( i == 0 )
     803             :             {
     804        1191 :                 if ( ( hCPE->input_mem_BPF[0] = (float *) malloc( sizeof( float ) * STEREO_DFT32MS_OVL_16k ) ) == NULL )
     805             :                 {
     806           0 :                     return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DFT stereo memory\n" ) );
     807             :                 }
     808        1191 :                 set_zero( hCPE->input_mem_BPF[0], STEREO_DFT32MS_OVL_16k );
     809             :             }
     810             : 
     811        2382 :             if ( ( hCPE->output_mem[i] = (float *) malloc( sizeof( float ) * NS2SA( output_Fs, STEREO_DFT32MS_OVL_NS ) ) ) == NULL )
     812             :             {
     813           0 :                 return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DFT stereo memory\n" ) );
     814             :             }
     815        2382 :             set_zero( hCPE->output_mem[i], NS2SA( output_Fs, STEREO_DFT32MS_OVL_NS ) );
     816             : 
     817        2382 :             if ( i < hCPE->nchan_out )
     818             :             {
     819        2232 :                 if ( ( hCPE->prev_synth_chs[i] = (float *) malloc( sizeof( float ) * NS2SA( output_Fs, FRAME_SIZE_NS ) ) ) == NULL )
     820             :                 {
     821           0 :                     return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for DFT stereo memory\n" ) );
     822             :                 }
     823        2232 :                 set_zero( hCPE->prev_synth_chs[i], NS2SA( output_Fs, FRAME_SIZE_NS ) );
     824             :             }
     825             :             else
     826             :             {
     827         150 :                 hCPE->prev_synth_chs[i] = NULL;
     828             :             }
     829             :         }
     830             :         else
     831             :         {
     832       15384 :             hCPE->input_mem[i] = NULL;
     833       15384 :             hCPE->input_mem_LB[i] = NULL;
     834       15384 :             if ( i == 0 )
     835             :             {
     836        7692 :                 hCPE->input_mem_BPF[0] = NULL;
     837             :             }
     838       15384 :             hCPE->output_mem[i] = NULL;
     839       15384 :             hCPE->prev_synth_chs[i] = NULL;
     840             :         }
     841             :     }
     842             : 
     843             :     /*-----------------------------------------------------------------*
     844             :      * CoreCoder, 2 instances: allocate and initialize
     845             :      *-----------------------------------------------------------------*/
     846             : 
     847       26427 :     for ( n = 0; n < CPE_CHANNELS; n++ )
     848             :     {
     849       17655 :         if ( st_ivas->sba_dirac_stereo_flag && st_ivas->nchan_transport == 1 )
     850             :         {
     851             :             /* for SBA DirAC stereo output CPE element is only used for upmix, core coder is found in SCE element used for core decoding */
     852         111 :             break;
     853             :         }
     854             : 
     855       17544 :         if ( ( st = (DEC_CORE_HANDLE) malloc( sizeof( Decoder_State ) ) ) == NULL )
     856             :         {
     857           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for CoreCoder structure\n" ) );
     858             :         }
     859             : 
     860       17544 :         copy_decoder_config( st_ivas, st );
     861             : 
     862       17544 :         st->total_brate = hCPE->element_brate / ( CPE_CHANNELS ); /* dummy initialization for getting right pointers initialization of input buffers in init_coder_ace_plus() */
     863       17544 :         st->mct_chan_mode = MCT_CHAN_MODE_REGULAR;
     864       17544 :         st->is_ism_format = 0;
     865             : 
     866       17544 :         if ( ( error = init_decoder( st, n, st_ivas->mc_mode ) ) != IVAS_ERR_OK )
     867             :         {
     868           0 :             return error;
     869             :         }
     870             : 
     871       17544 :         hCPE->hCoreCoder[n] = st;
     872             :     }
     873             : 
     874             :     /*-----------------------------------------------------------------*
     875             :      * DFT stereo initialization
     876             :      *-----------------------------------------------------------------*/
     877             : 
     878        8883 :     if ( hCPE->element_mode == IVAS_CPE_DFT || ( st_ivas->sba_dirac_stereo_flag && hCPE->cpe_id == 0 ) )
     879             :     {
     880         585 :         if ( ( error = stereo_dft_dec_create( &( hCPE->hStereoDft ), hCPE->element_brate, output_Fs, st_ivas->sba_dirac_stereo_flag, st_ivas->nchan_transport ) ) != IVAS_ERR_OK )
     881             :         {
     882           0 :             return error;
     883             :         }
     884             :     }
     885             : 
     886             :     /*-----------------------------------------------------------------*
     887             :      * DFT stereo mono DMX initialization
     888             :      *-----------------------------------------------------------------*/
     889             : 
     890        8883 :     if ( hCPE->element_mode != IVAS_CPE_MDCT && hCPE->nchan_out == 1 )
     891             :     {
     892         135 :         if ( ( hCPE->hStereoDftDmx = (STEREO_DFT_DMX_DATA_HANDLE) malloc( sizeof( STEREO_DFT_DMX_DATA ) ) ) == NULL )
     893             :         {
     894           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Stereo DFT mono output\n" ) );
     895             :         }
     896             : 
     897         135 :         stereo_dft_dmx_out_reset( hCPE->hStereoDftDmx );
     898             :     }
     899             : 
     900             :     /*-----------------------------------------------------------------*
     901             :      * Temporal inter-channel alignment initialization
     902             :      *-----------------------------------------------------------------*/
     903             : 
     904        8883 :     if ( ( hCPE->element_mode != IVAS_CPE_MDCT || ( st_ivas->ivas_format == STEREO_FORMAT && hCPE->element_brate <= MAX_MDCT_ITD_BRATE ) ) && hCPE->nchan_out != 1 )
     905             :     {
     906         300 :         if ( ( hCPE->hStereoTCA = (STEREO_TCA_DEC_HANDLE) malloc( sizeof( STEREO_TCA_DEC_DATA ) ) ) == NULL )
     907             :         {
     908           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Stereo TCA\n" ) );
     909             :         }
     910             : 
     911         300 :         stereo_tca_init_dec( hCPE->hStereoTCA );
     912             :     }
     913             : 
     914             :     /*-----------------------------------------------------------------*
     915             :      * Stereo IC BWE initialization
     916             :      *-----------------------------------------------------------------*/
     917             : 
     918        8883 :     if ( hCPE->element_mode != IVAS_CPE_MDCT && !( hCPE->element_mode == IVAS_CPE_DFT && hCPE->nchan_out == 1 ) )
     919             :     {
     920         273 :         if ( ( hCPE->hStereoICBWE = (STEREO_ICBWE_DEC_HANDLE) malloc( sizeof( STEREO_ICBWE_DEC_DATA ) ) ) == NULL )
     921             :         {
     922           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Stereo ICBWE\n" ) );
     923             :         }
     924             : 
     925         273 :         stereo_icBWE_init_dec( hCPE->hStereoICBWE );
     926             :     }
     927             : 
     928             :     /*-----------------------------------------------------------------*
     929             :      * TD stereo initialization
     930             :      *-----------------------------------------------------------------*/
     931             : 
     932        8883 :     if ( hCPE->element_mode == IVAS_CPE_TD )
     933             :     {
     934           0 :         if ( ( hCPE->hStereoTD = (STEREO_TD_DEC_DATA_HANDLE) malloc( sizeof( STEREO_TD_DEC_DATA ) ) ) == NULL )
     935             :         {
     936           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for TD Stereo\n" ) );
     937             :         }
     938             : 
     939           0 :         stereo_td_init_dec( hCPE->hStereoTD, hCPE->last_element_mode );
     940             :     }
     941             : 
     942             :     /*-----------------------------------------------------------------*
     943             :      * MDCT stereo initialization
     944             :      *-----------------------------------------------------------------*/
     945             : 
     946        8883 :     if ( hCPE->element_mode == IVAS_CPE_MDCT && st_ivas->nCPE == 1 )
     947             :     {
     948        1155 :         if ( ( hCPE->hStereoMdct = (STEREO_MDCT_DEC_DATA_HANDLE) malloc( sizeof( STEREO_MDCT_DEC_DATA ) ) ) == NULL )
     949             :         {
     950           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Stereo MDCT\n" ) );
     951             :         }
     952             : 
     953        1155 :         if ( st_ivas->ivas_format == STEREO_FORMAT && hCPE->element_brate <= MAX_MDCT_ITD_BRATE )
     954             :         {
     955          39 :             hCPE->hStereoMdct->use_itd = 1;
     956             :         }
     957             :         else
     958             :         {
     959        1116 :             hCPE->hStereoMdct->use_itd = 0;
     960             :         }
     961             : 
     962        1155 :         hCPE->hStereoMdct->reverse_dmx = 0;
     963        1155 :         hCPE->hStereoMdct->smooth_ratio = 1.f;
     964        1155 :         set_s( hCPE->hStereoMdct->prev_ms_mask[0], 0, MAX_SFB );
     965        1155 :         set_s( hCPE->hStereoMdct->prev_ms_mask[1], 0, MAX_SFB );
     966        1155 :         hCPE->hStereoMdct->lastCoh = 1.f;
     967        1155 :         hCPE->hStereoMdct->mdct_stereo_mode[0] = SMDCT_DUAL_MONO;
     968        1155 :         hCPE->hStereoMdct->mdct_stereo_mode[1] = SMDCT_DUAL_MONO;
     969        1155 :         hCPE->hStereoMdct->IGFStereoMode[0] = -1;
     970        1155 :         hCPE->hStereoMdct->IGFStereoMode[1] = -1;
     971             :     }
     972             : 
     973             :     /*-----------------------------------------------------------------*
     974             :      * Stereo CNG initialization
     975             :      *-----------------------------------------------------------------*/
     976             : 
     977        8883 :     if ( hCPE->element_mode == IVAS_CPE_TD || hCPE->element_mode == IVAS_CPE_DFT )
     978             :     {
     979         297 :         if ( ( hCPE->hStereoCng = (STEREO_CNG_DEC_HANDLE) malloc( sizeof( STEREO_CNG_DEC ) ) ) == NULL )
     980             :         {
     981           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for Stereo CNG\n" ) );
     982             :         }
     983         297 :         stereo_cng_init_dec( hCPE->hStereoCng, &hCPE->hCoreCoder[0]->hFdCngDec->hFdCngCom->frameSize );
     984             :     }
     985             : 
     986        8883 :     st_ivas->hCPE[cpe_id] = hCPE;
     987             : 
     988        8883 :     return error;
     989             : }
     990             : 
     991             : 
     992             : /*-------------------------------------------------------------------------
     993             :  * destroy_cpe_dec()
     994             :  *
     995             :  * Destroy and deallocate IVAS decoder CPE handle
     996             :  *-------------------------------------------------------------------------*/
     997             : 
     998        8883 : void destroy_cpe_dec(
     999             :     CPE_DEC_HANDLE hCPE /* i/o: CPE decoder structure       */
    1000             : )
    1001             : {
    1002             :     int16_t n;
    1003             :     Decoder_State *st;
    1004             : 
    1005       26649 :     for ( n = 0; n < CPE_CHANNELS; n++ )
    1006             :     {
    1007       17766 :         st = hCPE->hCoreCoder[n];
    1008             : 
    1009       17766 :         if ( st != NULL )
    1010             :         {
    1011       17232 :             destroy_core_dec( st );
    1012             : 
    1013       17232 :             free( st );
    1014       17232 :             st = NULL;
    1015             :         }
    1016             :     }
    1017             : 
    1018        8883 :     if ( hCPE->hStereoDft != NULL )
    1019             :     {
    1020         531 :         stereo_dft_dec_destroy( &( hCPE->hStereoDft ) );
    1021         531 :         hCPE->hStereoDft = NULL;
    1022             :     }
    1023             : 
    1024        8883 :     if ( hCPE->hStereoDftDmx != NULL )
    1025             :     {
    1026          96 :         free( hCPE->hStereoDftDmx );
    1027          96 :         hCPE->hStereoDftDmx = NULL;
    1028             :     }
    1029             : 
    1030        8883 :     if ( hCPE->hStereoTD != NULL )
    1031             :     {
    1032           3 :         free( hCPE->hStereoTD );
    1033           3 :         hCPE->hStereoTD = NULL;
    1034             :     }
    1035             : 
    1036        8883 :     if ( hCPE->hStereoMdct != NULL )
    1037             :     {
    1038        1245 :         free( hCPE->hStereoMdct );
    1039        1245 :         hCPE->hStereoMdct = NULL;
    1040             :     }
    1041             : 
    1042        8883 :     if ( hCPE->hStereoTCA != NULL )
    1043             :     {
    1044         294 :         free( hCPE->hStereoTCA );
    1045         294 :         hCPE->hStereoTCA = NULL;
    1046             :     }
    1047             : 
    1048        8883 :     if ( hCPE->hStereoICBWE != NULL )
    1049             :     {
    1050         312 :         free( hCPE->hStereoICBWE );
    1051         312 :         hCPE->hStereoICBWE = NULL;
    1052             :     }
    1053             : 
    1054        8883 :     if ( hCPE->input_mem_LB[0] != NULL )
    1055             :     {
    1056        3573 :         for ( n = 0; n < CPE_CHANNELS; n++ )
    1057             :         {
    1058        2382 :             free( hCPE->input_mem_LB[n] );
    1059        2382 :             hCPE->input_mem_LB[n] = NULL;
    1060        2382 :             free( hCPE->input_mem[n] );
    1061        2382 :             hCPE->input_mem[n] = NULL;
    1062        2382 :             free( hCPE->output_mem[n] );
    1063        2382 :             hCPE->output_mem[n] = NULL;
    1064        2382 :             if ( hCPE->prev_synth_chs[n] != NULL )
    1065             :             {
    1066        2268 :                 free( hCPE->prev_synth_chs[n] );
    1067        2268 :                 hCPE->prev_synth_chs[n] = NULL;
    1068             :             }
    1069             :         }
    1070        1191 :         free( hCPE->input_mem_BPF[0] );
    1071        1191 :         hCPE->input_mem_BPF[0] = NULL;
    1072             :     }
    1073             : 
    1074        8883 :     if ( hCPE->hStereoCng != NULL )
    1075             :     {
    1076         246 :         free( hCPE->hStereoCng );
    1077         246 :         hCPE->hStereoCng = NULL;
    1078             :     }
    1079             : 
    1080        8883 :     free( hCPE );
    1081             : 
    1082        8883 :     return;
    1083             : }
    1084             : 
    1085             : 
    1086             : /*-------------------------------------------------------------------------
    1087             :  * read_stereo_mode_and_bwidth()
    1088             :  *
    1089             :  * Read stereo technology info & audio bandwidth
    1090             :  *-------------------------------------------------------------------------*/
    1091             : 
    1092     1261158 : static void read_stereo_mode_and_bwidth(
    1093             :     CPE_DEC_HANDLE hCPE,          /* i/o: CPE handle             */
    1094             :     const Decoder_Struct *st_ivas /* i  : decoder main structure */
    1095             : )
    1096             : {
    1097             :     Decoder_State **sts;
    1098             : 
    1099             :     /*-----------------------------------------------------------------*
    1100             :      * BFI or NO_DATA frame: Use stereo parameters from last (active) frame
    1101             :      *-----------------------------------------------------------------*/
    1102             : 
    1103     1261158 :     if ( st_ivas->bfi || st_ivas->hDecoderConfig->ivas_total_brate < IVAS_SID_5k2 )
    1104             :     {
    1105       62337 :         hCPE->element_mode = hCPE->last_element_mode;
    1106             :     }
    1107             : 
    1108             :     /*-----------------------------------------------------------------*
    1109             :      * SID frame: get element mode from SID side info
    1110             :      *-----------------------------------------------------------------*/
    1111             : 
    1112     1198821 :     else if ( st_ivas->hDecoderConfig->ivas_total_brate == IVAS_SID_5k2 )
    1113             :     {
    1114        5991 :         switch ( st_ivas->sid_format )
    1115             :         {
    1116        4065 :             case SID_DFT_STEREO:
    1117        4065 :                 hCPE->element_mode = IVAS_CPE_DFT;
    1118             :                 /* Read CNG type */
    1119        4065 :                 hCPE->hCoreCoder[0]->cng_type = get_next_indice( hCPE->hCoreCoder[0], 1 );
    1120             : 
    1121             :                 /* Read BW information in SID */
    1122        4065 :                 hCPE->hCoreCoder[0]->bwidth = get_next_indice( hCPE->hCoreCoder[0], 2 );
    1123        4065 :                 break;
    1124        1704 :             case SID_MDCT_STEREO:
    1125             :                 /* 2TC SBA DTX also uses MDCT-Stereo DTX */
    1126             :             case SID_SBA_2TC:
    1127        1704 :                 hCPE->element_mode = IVAS_CPE_MDCT;
    1128        1704 :                 break;
    1129           0 :             case SID_SBA_1TC:
    1130           0 :                 assert( !"Forbidden value for SID format in CPE (SBA 1TC), should have already been adressed earlier" );
    1131             :                 break;
    1132           0 :             case SID_MASA_1TC:
    1133           0 :                 hCPE->element_mode = IVAS_SCE;
    1134           0 :                 break;
    1135         222 :             case SID_MASA_2TC:
    1136             :                 /* 2TC MASA DTX uses MDCT or DFT based core */
    1137         222 :                 if ( hCPE->element_mode == IVAS_CPE_TD )
    1138             :                 {
    1139           0 :                     hCPE->element_mode = IVAS_CPE_DFT;
    1140             :                 }
    1141         222 :                 if ( hCPE->element_mode == IVAS_CPE_DFT )
    1142             :                 {
    1143             :                     /* Read CNG type */
    1144         222 :                     hCPE->hCoreCoder[0]->cng_type = get_next_indice( hCPE->hCoreCoder[0], 1 );
    1145             : 
    1146             :                     /* Read BW information in SID */
    1147         222 :                     hCPE->hCoreCoder[0]->bwidth = get_next_indice( hCPE->hCoreCoder[0], 2 );
    1148             :                 }
    1149         222 :                 break;
    1150           0 :             default:
    1151             :                 /* this is what has been done for all modes previously, may need adaptation in the future */
    1152           0 :                 hCPE->element_mode = hCPE->last_element_mode;
    1153           0 :                 break;
    1154             :         }
    1155             :     }
    1156             : 
    1157             :     /*-----------------------------------------------------------------*
    1158             :      * active frame: read element mode and audio bandwidth info
    1159             :      *-----------------------------------------------------------------*/
    1160             : 
    1161             :     else
    1162             :     {
    1163     1192830 :         sts = hCPE->hCoreCoder;
    1164     1192830 :         if ( st_ivas->hMCT && hCPE->cpe_id != 0 )
    1165             :         {
    1166      500982 :             sts[0]->bwidth = st_ivas->hCPE[0]->hCoreCoder[0]->bwidth;
    1167      500982 :             sts[1]->bwidth = st_ivas->hCPE[0]->hCoreCoder[0]->bwidth;
    1168             :         }
    1169             :         else
    1170             :         {
    1171             :             /* read stereo technology info */
    1172      691848 :             if ( hCPE->element_brate < MIN_BRATE_MDCT_STEREO && st_ivas->hMCT == NULL )
    1173             :             {
    1174      154791 :                 hCPE->element_mode = get_next_indice( sts[0], NBITS_ELEMENT_MODE ) + IVAS_CPE_DFT;
    1175             :             }
    1176             :             else
    1177             :             {
    1178      537057 :                 hCPE->element_mode = IVAS_CPE_MDCT;
    1179             :             }
    1180             : 
    1181             :             /* read the bandwidth */
    1182      691848 :             if ( hCPE->element_brate < MIN_BRATE_FB_STEREO )
    1183             :             {
    1184             :                 /* WB and SWB are supported */
    1185       95658 :                 sts[0]->bwidth = get_next_indice( sts[0], 1 ) + WB;
    1186       95658 :                 sts[1]->bwidth = sts[0]->bwidth;
    1187             :             }
    1188             :             else
    1189             :             {
    1190             :                 /* WB, SWB and FB are supported */
    1191      596190 :                 sts[0]->bwidth = get_next_indice( sts[0], NBITS_BWIDTH );
    1192      596190 :                 sts[1]->bwidth = sts[0]->bwidth;
    1193             :             }
    1194             :         }
    1195             :     }
    1196             : 
    1197     1261158 :     return;
    1198             : }
    1199             : 
    1200             : 
    1201             : /*-------------------------------------------------------------------------
    1202             :  * stereo_mode_combined_format_dec()
    1203             :  *
    1204             :  * Set stereo format in a combined format
    1205             :  *-------------------------------------------------------------------------*/
    1206             : 
    1207     1261158 : static void stereo_mode_combined_format_dec(
    1208             :     const Decoder_Struct *st_ivas, /* i  : decoder main structure */
    1209             :     CPE_DEC_HANDLE hCPE            /* i/o: CPE handle             */
    1210             : )
    1211             : {
    1212             :     int32_t element_brate_ref;
    1213             : 
    1214     1261158 :     if ( st_ivas->ivas_format == MASA_ISM_FORMAT )
    1215             :     {
    1216       21252 :         element_brate_ref = hCPE->element_brate;
    1217             : 
    1218       21252 :         if ( st_ivas->ism_mode == ISM_MASA_MODE_DISC &&
    1219        9447 :              ( ( st_ivas->nchan_ism == 3 && st_ivas->hDecoderConfig->ivas_total_brate == IVAS_96k ) ||
    1220        8367 :                ( st_ivas->nchan_ism == 4 && st_ivas->hDecoderConfig->ivas_total_brate == IVAS_128k ) ) )
    1221             :         {
    1222        1278 :             if ( !st_ivas->bfi )
    1223             :             {
    1224             :                 /* read OMASA stereo mode signalling */
    1225        1272 :                 if ( get_next_indice( hCPE->hCoreCoder[0], NBITS_ELEMENT_MODE ) )
    1226             :                 {
    1227           0 :                     hCPE->element_mode = IVAS_CPE_MDCT;
    1228             :                 }
    1229             :                 else
    1230             :                 {
    1231        1272 :                     hCPE->element_mode = IVAS_CPE_DFT;
    1232             :                 }
    1233             :             }
    1234             : 
    1235        1278 :             if ( hCPE->element_mode == IVAS_CPE_MDCT )
    1236             :             {
    1237           0 :                 hCPE->element_brate = IVAS_64k;
    1238           0 :                 hCPE->brate_surplus -= ( hCPE->element_brate - element_brate_ref );
    1239             :             }
    1240             :         }
    1241             :     }
    1242             : 
    1243     1261158 :     return;
    1244             : }

Generated by: LCOV version 1.14