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

Generated by: LCOV version 1.14