LCOV - code coverage report
Current view: top level - lib_dec - ivas_stereo_icbwe_dec.c (source / functions) Hit Total Coverage
Test: Coverage on main -- short test vectors @ 6c9ddc4024a9c0e1ecb8f643f114a84a0e26ec6b Lines: 308 316 97.5 %
Date: 2025-05-23 08:37:30 Functions: 4 4 100.0 %

          Line data    Source code
       1             : /******************************************************************************************************
       2             : 
       3             :    (C) 2022-2025 IVAS codec Public Collaboration with portions copyright Dolby International AB, Ericsson AB,
       4             :    Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
       5             :    Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
       6             :    Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
       7             :    contributors to this repository. All Rights Reserved.
       8             : 
       9             :    This software is protected by copyright law and by international treaties.
      10             :    The IVAS codec Public Collaboration consisting of Dolby International AB, Ericsson AB,
      11             :    Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
      12             :    Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
      13             :    Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
      14             :    contributors to this repository retain full ownership rights in their respective contributions in
      15             :    the software. This notice grants no license of any kind, including but not limited to patent
      16             :    license, nor is any license granted by implication, estoppel or otherwise.
      17             : 
      18             :    Contributors are required to enter into the IVAS codec Public Collaboration agreement before making
      19             :    contributions.
      20             : 
      21             :    This software is provided "AS IS", without any express or implied warranties. The software is in the
      22             :    development stage. It is intended exclusively for experts who have experience with such software and
      23             :    solely for the purpose of inspection. All implied warranties of non-infringement, merchantability
      24             :    and fitness for a particular purpose are hereby disclaimed and excluded.
      25             : 
      26             :    Any dispute, controversy or claim arising under or in relation to providing this software shall be
      27             :    submitted to and settled by the final, binding jurisdiction of the courts of Munich, Germany in
      28             :    accordance with the laws of the Federal Republic of Germany excluding its conflict of law rules and
      29             :    the United Nations Convention on Contracts on the International Sales of Goods.
      30             : 
      31             : *******************************************************************************************************/
      32             : 
      33             : #include <assert.h>
      34             : #include <stdint.h>
      35             : #include "options.h"
      36             : #include <math.h>
      37             : #include "cnst.h"
      38             : #include "ivas_cnst.h"
      39             : #include "prot.h"
      40             : #include "ivas_prot.h"
      41             : #include "wmc_auto.h"
      42             : #include "rom_com.h"
      43             : #include "ivas_rom_com.h"
      44             : #ifdef DEBUGGING
      45             : #include "debug.h"
      46             : #endif
      47             : 
      48             : /*-------------------------------------------------------------------*
      49             :  * ic_bwe_dec_reset()
      50             :  *
      51             :  * core switching reset of IC BWE memory
      52             :  *-------------------------------------------------------------------*/
      53             : 
      54       49083 : static void ic_bwe_dec_reset(
      55             :     STEREO_ICBWE_DEC_HANDLE hStereoICBWE /* i/o: Stereo ICBWE handle */
      56             : )
      57             : {
      58             :     /* unscaled & scaled SHB synthesis memory */
      59       49083 :     set_f( hStereoICBWE->mem_syn_shb_nonref, 0, L_SHB_LAHEAD ); /* use samples from !acelp) */
      60       49083 :     set_f( hStereoICBWE->mem_lpc_shbsynth_nonref, 0, LPC_SHB_ORDER );
      61       49083 :     set_f( hStereoICBWE->mem_syn_shb_ola_nonref, 0, L_SHB_LAHEAD ); /* use samples from !acelp) */
      62             : 
      63             :     /* inter-channel BWE SP and GSP mem reset */
      64       49083 :     hStereoICBWE->memShbSpecMapping = 0;
      65             : 
      66       49083 :     set_f( hStereoICBWE->memShbHilbert_nonref, 0, HILBERT_MEM_SIZE );
      67       49083 :     set_f( hStereoICBWE->memShbInterp_nonref, 0, ( 2 * ALLPASSSECTIONS_STEEP + 1 ) );
      68       49083 :     set_f( hStereoICBWE->memShb_fsout_nonref, 0, INTERP_3_2_MEM_LEN );
      69       49083 :     hStereoICBWE->syn_dm_phase_nonref = 0;
      70             : 
      71       49083 :     return;
      72             : }
      73             : 
      74             : 
      75             : /*-------------------------------------------------------------------*
      76             :  * stereo_icBWE_dec()
      77             :  *
      78             :  * Spatial mapping of reference to the non-reference channels in SHB
      79             :  *-------------------------------------------------------------------*/
      80             : 
      81      450150 : void stereo_icBWE_dec(
      82             :     CPE_DEC_HANDLE hCPE,        /* i/o: CPE decoder structure                           */
      83             :     float *synthRef,            /* i/o: Reference channel HB synthesis at output Fs     */
      84             :     float *synth,               /* o  : Non reference channel HB synthesis at output Fs */
      85             :     const float *fb_synth_ref,  /* i  : ref. high-band synthesis 16-20 kHz              */
      86             :     const float *voice_factors, /* i  : voicing factors                                 */
      87             :     const int16_t output_frame  /* i  : frame length                                    */
      88             : )
      89             : {
      90             :     int16_t i, j, k, nbSubFr;
      91             :     Decoder_State *st; /* i/o: decoder state structure, primary channel        */
      92             :     int16_t spIndx, gsIndx;
      93             :     float excSHB_nonref[L_FRAME16k];
      94             :     float shb_synth_nonref[L_FRAME16k + L_SHB_LAHEAD];
      95             :     float error[L_FRAME32k];
      96             :     float nlMixFac[NB_SUBFR16k];
      97             :     float gsMapping, specMapping;
      98             :     float fb_synth_nonref[L_FRAME48k];
      99             :     float scale, prev_pow, curr_pow, temp;
     100             :     float alpha, winSlope, winLen, prevgsMapping;
     101             :     float temp1, temp2;
     102             :     float icbweM2Ref, ratio_L;
     103             : 
     104      450150 :     STEREO_DFT_DEC_DATA_HANDLE hStereoDft = hCPE->hStereoDft;
     105      450150 :     STEREO_ICBWE_DEC_HANDLE hStereoICBWE = hCPE->hStereoICBWE;
     106      450150 :     st = hCPE->hCoreCoder[0];
     107             : 
     108             :     /*--------------------------------------------------------------------*
     109             :      * skip IC-BWE in case of mono DMX output                             *
     110             :      * -------------------------------------------------------------------*/
     111             : 
     112      450150 :     if ( hCPE->element_mode == IVAS_CPE_DFT && hCPE->nchan_out == 1 && hCPE->hStereoDft->hConfig->res_cod_mode > STEREO_DFT_RES_COD_OFF )
     113             :     {
     114       16026 :         hCPE->hStereoDft->core_hist[0] = st->core;
     115             : 
     116       16026 :         return;
     117             :     }
     118      434124 :     else if ( hCPE->element_mode == IVAS_CPE_DFT && hCPE->nchan_out == 1 )
     119             :     {
     120             : 
     121       51339 :         return;
     122             :     }
     123             : 
     124             :     /*--------------------------------------------------------------------*
     125             :      * skip IC-BWE in case of SID or NO_DATA frame
     126             :      * -------------------------------------------------------------------*/
     127             : 
     128      382785 :     if ( st->core_brate <= SID_2k40 )
     129             :     {
     130       28794 :         return;
     131             :     }
     132             : 
     133             :     /*--------------------------------------------------------------------*
     134             :      * TD high band stereo filling                                        *
     135             :      * -------------------------------------------------------------------*/
     136             : 
     137             :     /* update buffers for TD stereo filling */
     138      353991 :     if ( hCPE->element_mode == IVAS_CPE_DFT )
     139             :     {
     140       95388 :         float hb_nrg = EPSILON;
     141       95388 :         float hb_nrg2 = EPSILON;
     142             : 
     143       95388 :         if ( st->core == ACELP_CORE || st->last_core == ACELP_CORE )
     144             :         {
     145    21077403 :             for ( i = 0; i < output_frame / 2; i++ )
     146             :             {
     147    21012960 :                 hb_nrg2 += synthRef[i] * synthRef[i];
     148             :             }
     149             : 
     150       64443 :             hCPE->hStereoDft->hb_nrg_subr[0] = hb_nrg2;
     151       64443 :             hb_nrg += hb_nrg2;
     152       64443 :             hb_nrg2 = EPSILON;
     153             : 
     154    21077403 :             for ( ; i < output_frame; i++ )
     155             :             {
     156    21012960 :                 hb_nrg2 += synthRef[i] * synthRef[i];
     157             :             }
     158             : 
     159       64443 :             hCPE->hStereoDft->hb_nrg_subr[1] = hb_nrg2;
     160       64443 :             hb_nrg += hb_nrg2;
     161             : 
     162       64443 :             mvr2r( synthRef, hCPE->hStereoDft->hb_stefi_sig + hCPE->hStereoDft->hb_stefi_delay, output_frame );
     163             :         }
     164             :         else
     165             :         {
     166       30945 :             set_zero( hCPE->hStereoDft->hb_stefi_sig + hCPE->hStereoDft->hb_stefi_delay, output_frame );
     167             :         }
     168       95388 :         hCPE->hStereoDft->hb_nrg_subr[0] *= hCPE->hStereoDft->NFFT / 2;
     169       95388 :         hCPE->hStereoDft->hb_nrg_subr[1] *= hCPE->hStereoDft->NFFT / 2;
     170       95388 :         hCPE->hStereoDft->hb_nrg[0] = hb_nrg;
     171       95388 :         hCPE->hStereoDft->td_gain[0] = 0;
     172       95388 :         hCPE->hStereoDft->core_hist[0] = st->core;
     173             : #ifdef DEBUG_MODE_DFT
     174             :         {
     175             :             int16_t tmp[L_FRAME48k];
     176             :             for ( i = 0; i < output_frame; i++ )
     177             :             {
     178             :                 tmp[i] = (int16_t) synthRef[i];
     179             :             }
     180             :             dbgwrite( tmp, sizeof( int16_t ), output_frame, 1, "res/hb_synth.pcm" );
     181             :             for ( i = 0; i < output_frame; i++ )
     182             :             {
     183             :                 tmp[i] = (int16_t) hCPE->hStereoDft->hb_stefi_sig[i + output_frame / 2];
     184             :             }
     185             :             dbgwrite( tmp, sizeof( int16_t ), output_frame, 1, "res/hb_stefi_sig.pcm" );
     186             :         }
     187             : #endif /* DEBUG_MODE_DFT */
     188             :     }
     189             : 
     190             :     /*--------------------------------------------------------------------*
     191             :      * IC-BWE                                                             *
     192             :      * -------------------------------------------------------------------*/
     193             : 
     194      353991 :     if ( st->core != ACELP_CORE || st->extl == -1 || ( hCPE->element_mode == IVAS_CPE_TD && hCPE->hCoreCoder[0]->tdm_LRTD_flag ) )
     195             :     {
     196      299505 :         return;
     197             :     }
     198       54486 :     else if ( hCPE->element_mode == IVAS_CPE_DFT && st->core_brate <= SID_2k40 )
     199             :     {
     200           0 :         mvr2r( synthRef, synth, output_frame );
     201           0 :         return;
     202             :     }
     203             : 
     204             : 
     205       54486 :     set_f( fb_synth_nonref, 0, L_FRAME48k );
     206             : 
     207             :     /* core switching reset */
     208       54486 :     if ( st->last_core != ACELP_CORE || st->bwidth == WB )
     209             :     {
     210        9795 :         ic_bwe_dec_reset( hStereoICBWE );
     211             : 
     212        9795 :         if ( st->last_core != ACELP_CORE )
     213             :         {
     214        1890 :             hStereoICBWE->prevSpecMapping = 0.0f;
     215        1890 :             hStereoICBWE->prevgsMapping = 1.0f;
     216        1890 :             hStereoICBWE->icbweM2Ref_prev = 1.0f;
     217             :         }
     218             : 
     219        9795 :         if ( st->bwidth == WB )
     220             :         {
     221             :             /* copy to outputHB and reset hb_synth values */
     222        8067 :             mvr2r( synthRef, synth, output_frame );
     223             : 
     224        8067 :             if ( st->element_mode == IVAS_CPE_TD )
     225             :             {
     226           9 :                 hStereoICBWE->prevSpecMapping = 0.0f;
     227           9 :                 hStereoICBWE->prevgsMapping = 1.0f;
     228           9 :                 hStereoICBWE->icbweM2Ref_prev = 1.0f;
     229             :             }
     230        8058 :             else if ( st->element_mode == IVAS_CPE_DFT )
     231             :             {
     232        8058 :                 hStereoICBWE->refChanIndx_bwe = L_CH_INDX;
     233        8058 :                 hStereoICBWE->prevSpecMapping = 0.0f;
     234             : 
     235        8058 :                 prevgsMapping = hStereoICBWE->prevgsMapping;
     236        8058 :                 temp1 = hStereoDft->side_gain[2 * STEREO_DFT_BAND_MAX + hStereoDft->nbands - 1];
     237        8058 :                 icbweM2Ref = 1.f + temp1;
     238        8058 :                 gsMapping = 1.f - temp1;
     239             : 
     240        8058 :                 winLen = (int16_t) ( ( SHB_OVERLAP_LEN * st->output_Fs ) / 16000 );
     241        8058 :                 winSlope = 1.0f / winLen;
     242        8058 :                 alpha = winSlope;
     243      180438 :                 for ( i = 0; i < winLen; i++ )
     244             :                 {
     245      172380 :                     synthRef[i] *= ( alpha * ( icbweM2Ref ) + ( 1.0f - alpha ) * ( hStereoICBWE->icbweM2Ref_prev ) );
     246      172380 :                     synth[i] *= ( alpha * ( gsMapping ) + ( 1.0f - alpha ) * ( prevgsMapping ) );
     247      172380 :                     alpha += winSlope;
     248             :                 }
     249     2593758 :                 for ( ; i < NS2SA( st->output_Fs, FRAME_SIZE_NS ); i++ )
     250             :                 {
     251     2585700 :                     synthRef[i] *= ( icbweM2Ref );
     252     2585700 :                     synth[i] *= ( gsMapping );
     253             :                 }
     254        8058 :                 hStereoICBWE->icbweM2Ref_prev = icbweM2Ref;
     255        8058 :                 hStereoICBWE->prevgsMapping = gsMapping;
     256             :             }
     257             : 
     258        8067 :             return;
     259             :         }
     260             :     }
     261             : 
     262       46419 :     if ( !st->bfi )
     263             :     {
     264       45408 :         hStereoICBWE->refChanIndx_bwe = get_next_indice( st, STEREO_ICBWE_REFBITS );
     265       45408 :         if ( st->flag_ACELP16k == 1 )
     266             :         {
     267       26550 :             spIndx = get_next_indice( st, STEREO_ICBWE_SPBITS );
     268             :         }
     269             :         else
     270             :         {
     271       18858 :             spIndx = 3;
     272             :         }
     273       45408 :         if ( st->element_mode == IVAS_CPE_TD )
     274             :         {
     275          78 :             gsIndx = get_next_indice( st, STEREO_ICBWE_GSBITS );
     276             :         }
     277             :         else
     278             :         {
     279       45330 :             gsIndx = get_next_indice( st, STEREO_ICBWE_GSBITS_DFT );
     280             :         }
     281             : 
     282             :         /* Store indices in case of frame loss */
     283       45408 :         hStereoICBWE->prev_spIndx = spIndx;
     284       45408 :         hStereoICBWE->prev_gsIndx = gsIndx;
     285             :     }
     286             :     else /*bfi*/
     287             :     {
     288             :         /* Retrieve last decoded indices */
     289        1011 :         spIndx = hStereoICBWE->prev_spIndx;
     290        1011 :         gsIndx = hStereoICBWE->prev_gsIndx;
     291        1011 :         hStereoICBWE->refChanIndx_bwe = hStereoICBWE->prev_refChanIndx_bwe;
     292             :     }
     293             : 
     294             :     /* IC-BWE parameter de-quant */
     295             :     /* sp Mapping */
     296       46419 :     hStereoICBWE->prevSpecMapping = usdequant( spIndx, -0.6f, 0.2f );
     297             : 
     298             :     /* gs Mapping */
     299       46419 :     prevgsMapping = hStereoICBWE->prevgsMapping;
     300             : 
     301       46419 :     if ( st->element_mode == IVAS_CPE_TD )
     302             :     {
     303          99 :         hStereoICBWE->prevgsMapping = icbwe_gsMapping_tbl[gsIndx];
     304             :     }
     305             :     else
     306             :     {
     307       46320 :         hStereoICBWE->prevgsMapping = icbwe_gsMappingDFT_tbl[gsIndx];
     308             :     }
     309             : 
     310       46419 :     hStereoICBWE->prevgsMapping = powf( 10, hStereoICBWE->prevgsMapping );
     311             : 
     312       46419 :     specMapping = hStereoICBWE->prevSpecMapping;
     313       46419 :     gsMapping = hStereoICBWE->prevgsMapping;
     314             : 
     315       46419 :     if ( ( st->extl == SWB_TBE || st->extl == FB_TBE ) && st->flag_ACELP16k == 1 )
     316             :     {
     317       26040 :         mvr2r( voice_factors, nlMixFac, NB_SUBFR16k );
     318       26040 :         if ( hCPE->hStereoDftDmx != NULL )
     319             :         {
     320          18 :             if ( hCPE->hStereoDftDmx->targetGain < 0.5f || hCPE->hStereoDftDmx->targetGain > 2.0f )
     321             :             {
     322           0 :                 v_multc( voice_factors, 0.5f, nlMixFac, NB_SUBFR16k );
     323             :             }
     324             :         }
     325             :         else
     326             :         {
     327       26022 :             if ( hCPE->hStereoTCA->targetGain < 0.5f || hCPE->hStereoTCA->targetGain > 2.0f )
     328             :             {
     329           0 :                 v_multc( voice_factors, 0.5f, nlMixFac, NB_SUBFR16k );
     330             :             }
     331             :         }
     332             : 
     333       26040 :         nbSubFr = ( st->flag_ACELP16k == 0 ) ? NB_SUBFR : NB_SUBFR16k;
     334      156240 :         for ( i = 0, k = 0; i < nbSubFr; i++ )
     335             :         {
     336      130200 :             if ( hCPE->hCoreCoder[0]->coder_type == UNVOICED || hStereoICBWE->MSFlag == 1 )
     337             :             {
     338       28230 :                 temp1 = 0;
     339       28230 :                 temp2 = 1.0f;
     340             :             }
     341             :             else
     342             :             {
     343      101970 :                 temp1 = sqrtf( nlMixFac[i] );
     344      101970 :                 temp2 = sqrtf( 1.0f - nlMixFac[i] );
     345             :             }
     346             : 
     347     8463000 :             for ( j = 0; j < L_FRAME16k / nbSubFr; j++, k++ )
     348             :             {
     349     8332800 :                 excSHB_nonref[k] = temp1 * hStereoICBWE->nlExc16k[k] + temp2 * hStereoICBWE->mixExc16k[k];
     350             :             }
     351             :         }
     352             : 
     353             :         /* LP synthesis */
     354       26040 :         mvr2r( hStereoICBWE->mem_syn_shb_nonref, shb_synth_nonref, L_SHB_LAHEAD );
     355       26040 :         syn_filt( hStereoICBWE->lpSHBRef, LPC_SHB_ORDER, excSHB_nonref, shb_synth_nonref + L_SHB_LAHEAD, L_FRAME16k, hStereoICBWE->mem_lpc_shbsynth_nonref, 1 );
     356             : 
     357       26040 :         prev_pow = sum2_f( shb_synth_nonref, L_SHB_LAHEAD + 10 );
     358       26040 :         curr_pow = sum2_f( shb_synth_nonref + L_SHB_LAHEAD + 10, L_SHB_LAHEAD + 10 );
     359             : 
     360       26040 :         if ( prev_pow == 0 )
     361             :         {
     362           0 :             scale = 0;
     363             :         }
     364             :         else
     365             :         {
     366       26040 :             scale = sqrtf( curr_pow / prev_pow );
     367             :         }
     368             : 
     369      546840 :         for ( i = 0; i < L_SHB_LAHEAD; i++ )
     370             :         {
     371      520800 :             shb_synth_nonref[i] *= scale;
     372             :         }
     373             : 
     374      286440 :         for ( ; i < L_SHB_LAHEAD + 10; i++ )
     375             :         {
     376      260400 :             temp = ( i - 19 ) / 10.0f;
     377      260400 :             shb_synth_nonref[i] *= ( temp * 1.0f + ( 1.0f - temp ) * scale );
     378             :         }
     379             : 
     380             :         /* spec and gs adjustment */
     381       26040 :         deemph( shb_synth_nonref + L_SHB_LAHEAD, specMapping, L_FRAME16k, &( hStereoICBWE->memShbSpecMapping ) );
     382       26040 :         mvr2r( shb_synth_nonref + L_FRAME16k, hStereoICBWE->mem_syn_shb_nonref, L_SHB_LAHEAD );
     383             : 
     384       26040 :         ScaleShapedSHB( SHB_OVERLAP_LEN, shb_synth_nonref, hStereoICBWE->mem_syn_shb_ola_nonref, hStereoICBWE->gshapeRef, ( hStereoICBWE->gFrameRef * gsMapping * 0.9f ), window_shb, subwin_shb );
     385             : 
     386       26040 :         if ( st->extl == FB_TBE )
     387             :         {
     388       12384 :             v_multc( fb_synth_ref, gsMapping, fb_synth_nonref, L_FRAME48k );
     389             :         }
     390             : 
     391             :         /* generate 32kHz SHB synthesis from 12.8(16)kHz signal */
     392       26040 :         GenSHBSynth( shb_synth_nonref, error, hStereoICBWE->memShbHilbert_nonref, hStereoICBWE->memShbInterp_nonref, st->L_frame, &( hStereoICBWE->syn_dm_phase_nonref ) );
     393             :     }
     394             :     else
     395             :     {
     396       20379 :         mvr2r( synthRef, synth, output_frame );
     397             : 
     398       20379 :         winLen = (int16_t) ( ( SHB_OVERLAP_LEN * st->output_Fs ) / 16000 );
     399       20379 :         winSlope = 1.0f / winLen;
     400       20379 :         alpha = winSlope;
     401             : 
     402       20379 :         ratio_L = ( hCPE->element_mode == IVAS_CPE_DFT ) ? ( 0.5f ) : ( tdm_ratio_tabl[hCPE->hStereoTD->tdm_last_ratio_idx] );
     403             : 
     404       20379 :         icbweM2Ref = gsMapping;
     405       20379 :         if ( hStereoICBWE->refChanIndx_bwe == L_CH_INDX )
     406             :         {
     407        6426 :             if ( ratio_L >= 0.1f )
     408             :             {
     409        6414 :                 icbweM2Ref = sqrtf( 0.5f - min( 0.5f, ( 1 - ratio_L ) * ( 1 - ratio_L ) * gsMapping * gsMapping ) ) / ratio_L;
     410             :             }
     411             :         }
     412             :         else
     413             :         {
     414       13953 :             if ( ratio_L <= 0.9f )
     415             :             {
     416       13953 :                 icbweM2Ref = sqrtf( 0.5f - min( 0.5f, ratio_L * ratio_L * gsMapping * gsMapping ) ) / ( 1 - ratio_L );
     417             :             }
     418             :         }
     419             : 
     420       20379 :         icbweM2Ref = max( gsMapping, icbweM2Ref );
     421             : 
     422      779439 :         for ( i = 0; i < winLen; i++ )
     423             :         {
     424      759060 :             synthRef[i] *= ( alpha * ( icbweM2Ref ) + ( 1.0f - alpha ) * ( hStereoICBWE->icbweM2Ref_prev ) );
     425      759060 :             synth[i] *= ( alpha * ( gsMapping ) + ( 1.0f - alpha ) * ( prevgsMapping ) );
     426      759060 :             alpha += winSlope;
     427             :         }
     428    11406279 :         for ( ; i < NS2SA( st->output_Fs, FRAME_SIZE_NS ); i++ )
     429             :         {
     430    11385900 :             synthRef[i] *= ( icbweM2Ref );
     431    11385900 :             synth[i] *= ( gsMapping );
     432             :         }
     433       20379 :         hStereoICBWE->icbweM2Ref_prev = icbweM2Ref;
     434             : 
     435       20379 :         ic_bwe_dec_reset( hStereoICBWE );
     436       20379 :         hStereoICBWE->prevSpecMapping = 0.0f;
     437             : 
     438       20379 :         return;
     439             :     }
     440             : 
     441             :     /* resample to output FS */
     442       26040 :     if ( st->output_Fs == 48000 )
     443             :     {
     444       15996 :         interpolate_3_over_2_allpass( error, L_FRAME32k, synth, hStereoICBWE->memShb_fsout_nonref );
     445             :     }
     446       10044 :     else if ( st->output_Fs == 32000 )
     447             :     {
     448       10044 :         mvr2r( error, synth, L_FRAME32k );
     449             :     }
     450           0 :     else if ( st->output_Fs == 16000 )
     451             :     {
     452           0 :         Decimate_allpass_steep( error, hStereoICBWE->memShb_fsout_nonref, L_FRAME32k, synth );
     453             :     }
     454             : 
     455             : 
     456       26040 :     if ( st->extl == FB_TBE && st->output_Fs == 48000 )
     457             :     {
     458        9573 :         v_add( fb_synth_nonref, synth, synth, L_FRAME48k );
     459             :     }
     460             : 
     461             :     /* copy to outputHB and reset hb_synth values */
     462       26040 :     ratio_L = ( hCPE->element_mode == IVAS_CPE_DFT ) ? ( 0.5f ) : ( tdm_ratio_tabl[hCPE->hStereoTD->tdm_last_ratio_idx] );
     463             : 
     464       26040 :     icbweM2Ref = gsMapping;
     465       26040 :     if ( hStereoICBWE->refChanIndx_bwe == L_CH_INDX )
     466             :     {
     467        8340 :         if ( ratio_L >= 0.1f )
     468             :         {
     469        8316 :             icbweM2Ref = sqrtf( 0.5f - min( 0.5f, ( 1 - ratio_L ) * ( 1 - ratio_L ) * gsMapping * gsMapping ) ) / ratio_L;
     470             :         }
     471             :     }
     472             :     else
     473             :     {
     474       17700 :         if ( ratio_L <= 0.9f )
     475             :         {
     476       17700 :             icbweM2Ref = sqrtf( 0.5f - min( 0.5f, ratio_L * ratio_L * gsMapping * gsMapping ) ) / ( 1 - ratio_L );
     477             :         }
     478             :     }
     479             : 
     480       26040 :     icbweM2Ref = max( gsMapping, icbweM2Ref );
     481             : 
     482       26040 :     winLen = (int16_t) ( ( SHB_OVERLAP_LEN * st->output_Fs ) / 16000 );
     483       26040 :     winSlope = 1.0f / winLen;
     484       26040 :     alpha = winSlope;
     485     1387560 :     for ( i = 0; i < winLen; i++ )
     486             :     {
     487     1361520 :         synthRef[i] *= ( alpha * ( icbweM2Ref ) + ( 1.0f - alpha ) * ( hStereoICBWE->icbweM2Ref_prev ) );
     488     1361520 :         alpha += winSlope;
     489             :     }
     490             : 
     491    20448840 :     for ( ; i < NS2SA( st->output_Fs, FRAME_SIZE_NS ); i++ )
     492             :     {
     493    20422800 :         synthRef[i] *= ( icbweM2Ref );
     494             :     }
     495             : 
     496       26040 :     hStereoICBWE->icbweM2Ref_prev = icbweM2Ref;
     497             : 
     498       26040 :     return;
     499             : }
     500             : 
     501             : /*-------------------------------------------------------------------*
     502             :  * stereo_icBWE_decproc()
     503             :  *
     504             :  * Stereo (inter-channel) BWE mapping - decoder initialization
     505             :  *-------------------------------------------------------------------*/
     506             : 
     507      450150 : void stereo_icBWE_decproc(
     508             :     CPE_DEC_HANDLE hCPE,                      /* i/o: CPE decoder structure       */
     509             :     float *output[CPE_CHANNELS],              /* i/o: output synthesis            */
     510             :     float outputHB[CPE_CHANNELS][L_FRAME48k], /* i  : HB synthesis                */
     511             :     const int16_t last_core,                  /* i  : last core, primary channel  */
     512             :     const int16_t last_bwidth,                /* i  : last bandwidth              */
     513             :     const int16_t output_frame                /* i  : frame length                */
     514             : )
     515             : {
     516             :     int16_t i, j, n, decoderDelay, icbweOLASize, dftOvlLen;
     517             :     int16_t core, memOffset, refChanIndx_bwe;
     518             :     float temp0[L_FRAME48k + NS2SA( 48000, IVAS_DEC_DELAY_NS - DELAY_BWE_TOTAL_NS )], temp1[L_FRAME48k + NS2SA( 48000, IVAS_DEC_DELAY_NS - DELAY_BWE_TOTAL_NS )];
     519             :     float winSlope, alpha;
     520             :     const float *win_dft;
     521             :     int32_t extl_brate, output_Fs;
     522             : 
     523      450150 :     STEREO_ICBWE_DEC_HANDLE hStereoICBWE = hCPE->hStereoICBWE;
     524             : 
     525             : 
     526             :     /*--------------------------------------------------------------------*
     527             :      * skip IC-BWE in case of SID or NO_DATA frame
     528             :      * -------------------------------------------------------------------*/
     529             : 
     530      450150 :     if ( ( hCPE->element_mode == IVAS_CPE_TD || hCPE->element_mode == IVAS_CPE_DFT ) && hCPE->nchan_out == 2 /*&& hCPE->hCoreCoder[0]->core_brate > SID_2k40*/ && hCPE->hCoreCoder[0]->last_core_brate <= SID_2k40 )
     531             :     {
     532       16305 :         stereo_icBWE_init_dec( hCPE->hStereoICBWE );
     533             :     }
     534             : 
     535      450150 :     if ( hCPE->hCoreCoder[0]->core_brate <= SID_2k40 )
     536             :     {
     537       43293 :         return;
     538             :     }
     539             : 
     540             :     /*--------------------------------------------------------------------*
     541             :      * skip IC-BWE in case of mono DMX output                             *
     542             :      * -------------------------------------------------------------------*/
     543             : 
     544      406857 :     if ( hCPE->nchan_out == 1 && hCPE->element_mode == IVAS_CPE_DFT )
     545             :     {
     546       52866 :         add_HB_to_mono_dmx( hCPE, output[0], outputHB[0], last_core, output_frame );
     547             : 
     548       52866 :         return;
     549             :     }
     550      353991 :     else if ( hCPE->nchan_out == 1 && hCPE->element_mode != IVAS_CPE_TD )
     551             :     {
     552       17637 :         return;
     553             :     }
     554             : 
     555             :     /*--------------------------------------------------------------------*
     556             :      * IC-BWE processing
     557             :      * -------------------------------------------------------------------*/
     558             : 
     559      336354 :     core = hCPE->hCoreCoder[0]->core;
     560      336354 :     extl_brate = hCPE->hCoreCoder[0]->extl_brate;
     561      336354 :     output_Fs = hCPE->hCoreCoder[0]->output_Fs;
     562             : 
     563      336354 :     memOffset = NS2SA( output_Fs, IVAS_DEC_DELAY_NS - DELAY_BWE_TOTAL_NS );
     564             : 
     565             :     /* LRTD stereo mode - 2xBWEs used */
     566      336354 :     if ( hCPE->element_mode == IVAS_CPE_TD && hCPE->hCoreCoder[0]->tdm_LRTD_flag )
     567             :     {
     568             :         /* delay HB synth */
     569       32895 :         for ( n = 0; n < CPE_CHANNELS; n++ )
     570             :         {
     571       21930 :             mvr2r( outputHB[n] + output_frame - memOffset, temp0, memOffset );
     572       21930 :             mvr2r( outputHB[n], outputHB[n] + memOffset, output_frame - memOffset );
     573       21930 :             mvr2r( hCPE->prev_hb_synth[n], outputHB[n], memOffset );
     574       21930 :             mvr2r( temp0, hCPE->prev_hb_synth[n], memOffset );
     575             :         }
     576             : 
     577       10965 :         if ( hCPE->nchan_out == 1 )
     578             :         {
     579             :             /* stereo to mono downmix */
     580     2383815 :             for ( i = 0; i < output_frame; i++ )
     581             :             {
     582     2379840 :                 outputHB[0][i] = ( outputHB[0][i] + outputHB[1][i] ) * 0.5f;
     583             :             }
     584        3975 :             v_add( output[0], outputHB[0], output[0], output_frame );
     585             :         }
     586             :         else
     587             :         {
     588             :             /* Add the delayed hb_synth component to the delayed ACELP synthesis */
     589       20970 :             for ( n = 0; n < CPE_CHANNELS; n++ )
     590             :             {
     591       13980 :                 v_add( output[n], outputHB[n], output[n], output_frame );
     592             :             }
     593             :         }
     594             :     }
     595             :     else
     596             :     {
     597      976167 :         for ( n = 0; n < CPE_CHANNELS; n++ )
     598             :         {
     599      650778 :             set_f( hCPE->prev_hb_synth[n], 0, memOffset );
     600             :         }
     601             :     }
     602             : 
     603      336354 :     if ( hCPE->element_mode != IVAS_CPE_MDCT && !( hCPE->element_mode == IVAS_CPE_TD && hCPE->hCoreCoder[0]->tdm_LRTD_flag ) )
     604             :     {
     605       95763 :         if ( hCPE->last_element_mode == IVAS_CPE_MDCT )
     606             :         {
     607         873 :             set_f( hStereoICBWE->memOutHB[0], 0, memOffset );
     608         873 :             set_f( hStereoICBWE->memOutHB[1], 0, memOffset );
     609             : 
     610         873 :             set_f( hStereoICBWE->memTransitionHB[0], 0, NS2SA( output_Fs, STEREO_DFT32MS_OVL_NS ) );
     611         873 :             set_f( hStereoICBWE->memTransitionHB[1], 0, NS2SA( output_Fs, STEREO_DFT32MS_OVL_NS ) );
     612             :         }
     613             : 
     614       95763 :         if ( core == ACELP_CORE && extl_brate > 0 )
     615             :         {
     616       54375 :             refChanIndx_bwe = hStereoICBWE->refChanIndx_bwe;
     617             : 
     618       54375 :             if ( hCPE->element_mode == IVAS_CPE_DFT && hCPE->hCoreCoder[0]->bwidth > WB && last_bwidth == WB && hCPE->hCoreCoder[0]->ini_frame > 1 /* counter wass already updated */ )
     619             :             {
     620             :                 /* fad-in reference HB signal */
     621          57 :                 winSlope = ( memOffset > 0 ) ? ( 1.0f / memOffset ) : 0;
     622        2217 :                 for ( i = 0; i < memOffset; i++ )
     623             :                 {
     624        2160 :                     outputHB[refChanIndx_bwe][i] *= ( i + 1 ) * winSlope;
     625             :                 }
     626             :             }
     627             :             /* Resampled LB and HB offset */
     628       54375 :             mvr2r( outputHB[refChanIndx_bwe], temp0 + memOffset, output_frame - memOffset );
     629       54375 :             mvr2r( outputHB[!refChanIndx_bwe], temp1 + memOffset, output_frame - memOffset );
     630             : 
     631       54375 :             decoderDelay = NS2SA( output_Fs, IVAS_DEC_DELAY_NS );
     632             : 
     633       54375 :             if ( last_core != ACELP_CORE && hCPE->element_mode == IVAS_CPE_DFT )
     634             :             {
     635             :                 /* hb_synth of mid band is faded out in the 1.25 ms prior to DFT analysis and the icbwe is faded in time domain */
     636        1905 :                 icbweOLASize = NS2SA( output_Fs, STEREO_DFT_DELAY_DEC_BWE_NS );
     637             : 
     638      226077 :                 for ( i = 0; i < decoderDelay; i++ )
     639             :                 {
     640      224172 :                     temp0[i] = 0;
     641      224172 :                     temp1[i] = 0;
     642             :                 }
     643             : 
     644        1905 :                 assert( icbweOLASize > 0 );
     645        1905 :                 winSlope = 1.0f / icbweOLASize;
     646        1905 :                 alpha = winSlope;
     647       88125 :                 for ( ; i < decoderDelay + icbweOLASize; i++ )
     648             :                 {
     649       86220 :                     temp0[i] *= alpha;
     650       86220 :                     temp1[i] *= alpha;
     651       86220 :                     alpha += winSlope;
     652             :                 }
     653             :             }
     654             :             else
     655             :             {
     656       52470 :                 if ( refChanIndx_bwe != hStereoICBWE->prev_refChanIndx_bwe )
     657             :                 {
     658        6939 :                     winSlope = ( memOffset > 0 ) ? ( 1.0f / memOffset ) : 0;
     659      251559 :                     for ( i = 0; i < memOffset; i++ )
     660             :                     {
     661      244620 :                         temp0[i] = ( ( i + 1 ) * winSlope ) * hStereoICBWE->memOutHB[refChanIndx_bwe][i] +
     662      244620 :                                    ( 1 - ( i + 1 ) * winSlope ) * hStereoICBWE->memOutHB[hStereoICBWE->prev_refChanIndx_bwe][i];
     663      244620 :                         temp1[i] = ( ( i + 1 ) * winSlope ) * hStereoICBWE->memOutHB[hStereoICBWE->prev_refChanIndx_bwe][i] +
     664      244620 :                                    ( 1 - ( i + 1 ) * winSlope ) * hStereoICBWE->memOutHB[refChanIndx_bwe][i];
     665             :                     }
     666             :                 }
     667             :                 else
     668             :                 {
     669       45531 :                     mvr2r( hStereoICBWE->memOutHB[refChanIndx_bwe], temp0, memOffset );
     670       45531 :                     mvr2r( hStereoICBWE->memOutHB[!refChanIndx_bwe], temp1, memOffset );
     671             :                 }
     672             :             }
     673             : 
     674       54375 :             if ( hCPE->nchan_out == 1 )
     675             :             {
     676             :                 /* stereo to mono downmix */
     677       17307 :                 for ( i = 0; i < output_frame; i++ )
     678             :                 {
     679       17280 :                     temp0[i] = ( temp0[i] + temp1[i] ) * 0.5f;
     680       17280 :                     output[0][i] += temp0[i];
     681             :                 }
     682             :             }
     683             :             else
     684             :             {
     685             : 
     686       54348 :                 v_add( temp0, output[0], output[0], output_frame );
     687             : #ifdef DEBUG_STEREO_DFT_NOSTEREO
     688             :                 v_add( temp0, output[1], output[1], output_frame );
     689             : #else
     690       54348 :                 v_add( temp1, output[1], output[1], output_frame );
     691             : #endif
     692             :             }
     693       54375 :             mvr2r( outputHB[0] + output_frame - memOffset, hStereoICBWE->memOutHB[0], memOffset );
     694       54375 :             mvr2r( outputHB[1] + output_frame - memOffset, hStereoICBWE->memOutHB[1], memOffset );
     695             : 
     696       54375 :             if ( hCPE->element_mode == IVAS_CPE_DFT )
     697             :             {
     698       54267 :                 win_dft = hCPE->hStereoDft->win32ms;
     699       54267 :                 dftOvlLen = hCPE->hStereoDft->dft32ms_ovl;
     700             : 
     701             :                 /* Preparing buffers in anticipation of an ACELP to TCX switch */
     702       54267 :                 j = 0;
     703     1767417 :                 for ( i = 0; i < memOffset; i++ )
     704             :                 {
     705     1713150 :                     hStereoICBWE->memTransitionHB[0][i] = hStereoICBWE->memOutHB[0][i] * win_dft[STEREO_DFT32MS_STEP * ( dftOvlLen - 1 - j )];
     706     1713150 :                     hStereoICBWE->memTransitionHB[1][i] = hStereoICBWE->memOutHB[1][i] * win_dft[STEREO_DFT32MS_STEP * ( dftOvlLen - 1 - j )];
     707     1713150 :                     j++;
     708             :                 }
     709             : 
     710     4051617 :                 for ( i = 0; j < dftOvlLen; i++ )
     711             :                 {
     712     3997350 :                     hStereoICBWE->memTransitionHB[0][memOffset + i] = outputHB[0][output_frame - i - 1] * win_dft[STEREO_DFT32MS_STEP * ( dftOvlLen - 1 - j )];
     713     3997350 :                     hStereoICBWE->memTransitionHB[1][memOffset + i] = outputHB[1][output_frame - i - 1] * win_dft[STEREO_DFT32MS_STEP * ( dftOvlLen - 1 - j )];
     714     3997350 :                     j++;
     715             :                 }
     716             :             }
     717             : 
     718       54375 :             hStereoICBWE->prev_refChanIndx_bwe = refChanIndx_bwe;
     719             :         }
     720             :         else
     721             :         {
     722       41388 :             if ( last_core == ACELP_CORE )
     723             :             {
     724        9771 :                 if ( hCPE->element_mode == IVAS_CPE_TD )
     725             :                 {
     726          69 :                     v_add( output[0], hStereoICBWE->memOutHB[hStereoICBWE->prev_refChanIndx_bwe], output[0], memOffset );
     727          69 :                     v_add( output[1], hStereoICBWE->memOutHB[!hStereoICBWE->prev_refChanIndx_bwe], output[1], memOffset );
     728             :                 }
     729             :                 else
     730             :                 {
     731             :                     /* This is generated in the ACELP frame and windowed. This process is akin to GenTransition for IC-BWE */
     732        9702 :                     v_add( output[0], hStereoICBWE->memTransitionHB[hStereoICBWE->prev_refChanIndx_bwe], output[0], NS2SA( output_Fs, STEREO_DFT32MS_OVL_NS ) );
     733        9702 :                     v_add( output[1], hStereoICBWE->memTransitionHB[!hStereoICBWE->prev_refChanIndx_bwe], output[1], NS2SA( output_Fs, STEREO_DFT32MS_OVL_NS ) );
     734             :                 }
     735             : 
     736        9771 :                 set_f( hStereoICBWE->memOutHB[0], 0, memOffset );
     737        9771 :                 set_f( hStereoICBWE->memOutHB[1], 0, memOffset );
     738             : 
     739        9771 :                 set_f( hStereoICBWE->memTransitionHB[0], 0, NS2SA( output_Fs, STEREO_DFT32MS_OVL_NS ) );
     740        9771 :                 set_f( hStereoICBWE->memTransitionHB[1], 0, NS2SA( output_Fs, STEREO_DFT32MS_OVL_NS ) );
     741             :             }
     742             :         }
     743             :     }
     744      240591 :     else if ( hCPE->element_mode == IVAS_CPE_TD && hCPE->hCoreCoder[0]->tdm_LRTD_flag && hStereoICBWE != NULL )
     745             :     {
     746           0 :         stereo_icBWE_init_dec( hStereoICBWE );
     747             :     }
     748      240591 :     else if ( hCPE->element_mode == IVAS_CPE_MDCT && hCPE->last_element_mode == IVAS_CPE_DFT && last_core == ACELP_CORE )
     749             :     {
     750        1656 :         int16_t delay_tdbwe = NS2SA( output_Fs, DELAY_BWE_TOTAL_NS );
     751             : 
     752        4968 :         for ( n = 0; n < hCPE->nchan_out; n++ )
     753             :         {
     754      331872 :             for ( i = 0; i < delay_tdbwe; i++ )
     755             :             {
     756      328560 :                 output[n][NS2SA( output_Fs, STEREO_DFT32MS_OVL_NS ) - delay_tdbwe + i] += outputHB[0][i];
     757             :             }
     758             :         }
     759             :         /* reset BWE structs as they are only needed in the transition frame in MDCT Stereo */
     760        1656 :         td_bwe_dec_init( hCPE->hCoreCoder[0]->hBWE_TD, -1, output_Fs );
     761        1656 :         fd_bwe_dec_init( hCPE->hCoreCoder[0]->hBWE_FD );
     762             :     }
     763             : 
     764      336354 :     if ( hCPE->element_mode == IVAS_CPE_DFT && ( max( hCPE->hStereoDft->td_gain[0], hCPE->hStereoDft->td_gain[1] ) > 0 ) )
     765             :     {
     766             :         float win_in, win_out, tmp;
     767             : 
     768       51684 :         win_dft = hCPE->hStereoDft->win32ms;
     769       51684 :         dftOvlLen = hCPE->hStereoDft->dft32ms_ovl;
     770             : 
     771     5460984 :         for ( i = 0; i < dftOvlLen; i++ )
     772             :         {
     773     5409300 :             win_in = win_dft[STEREO_DFT32MS_STEP * i] * win_dft[STEREO_DFT32MS_STEP * i];
     774     5409300 :             win_out = 1 - win_in;
     775     5409300 :             tmp = ( win_in * hCPE->hStereoDft->td_gain[0] + win_out * hCPE->hStereoDft->td_gain[1] ) * hCPE->hStereoDft->hb_stefi_sig[i];
     776             : 
     777     5409300 :             output[0][i] += tmp;
     778     5409300 :             output[1][i] -= tmp;
     779             :         }
     780    29261904 :         for ( i = dftOvlLen; i < output_frame; i++ )
     781             :         {
     782    29210220 :             tmp = hCPE->hStereoDft->td_gain[0] * hCPE->hStereoDft->hb_stefi_sig[i];
     783    29210220 :             output[0][i] += tmp;
     784    29210220 :             output[1][i] -= tmp;
     785             :         }
     786             :     }
     787             : 
     788      336354 :     return;
     789             : }
     790             : 
     791             : /*-------------------------------------------------------------------*
     792             :  * stereo_icBWE_init_dec()
     793             :  *
     794             :  * Stereo (inter-channel) BWE mapping - decoder initialization
     795             :  *-------------------------------------------------------------------*/
     796             : 
     797       18909 : void stereo_icBWE_init_dec(
     798             :     STEREO_ICBWE_DEC_HANDLE hStereoICBWE /* i/o: Stereo inter-channel BWE handle   */
     799             : )
     800             : {
     801             :     /* BWE ref channel */
     802       18909 :     hStereoICBWE->refChanIndx_bwe = L_CH_INDX;
     803       18909 :     hStereoICBWE->prev_refChanIndx_bwe = L_CH_INDX;
     804             : 
     805             :     /* SHB output memory */
     806       18909 :     set_f( hStereoICBWE->memOutHB[0], 0, NS2SA( 48000, IVAS_DEC_DELAY_NS - DELAY_BWE_TOTAL_NS ) );
     807       18909 :     set_f( hStereoICBWE->memOutHB[1], 0, NS2SA( 48000, IVAS_DEC_DELAY_NS - DELAY_BWE_TOTAL_NS ) );
     808             : 
     809             : 
     810             :     /* SHB output memory */
     811       18909 :     set_f( hStereoICBWE->memTransitionHB[0], 0, NS2SA( 48000, STEREO_DFT32MS_OVL_NS ) );
     812       18909 :     set_f( hStereoICBWE->memTransitionHB[1], 0, NS2SA( 48000, STEREO_DFT32MS_OVL_NS ) );
     813             : 
     814             :     /* inter-channel BWE spectral shape adj. */
     815       18909 :     hStereoICBWE->prevSpecMapping = 0;
     816       18909 :     hStereoICBWE->prevgsMapping = 1.0f;
     817             : 
     818       18909 :     hStereoICBWE->icbweM2Ref_prev = 1.0f;
     819             : 
     820       18909 :     hStereoICBWE->prev_spIndx = 0;
     821       18909 :     hStereoICBWE->prev_gsIndx = 0;
     822             : 
     823       18909 :     ic_bwe_dec_reset( hStereoICBWE );
     824             : 
     825       18909 :     return;
     826             : }

Generated by: LCOV version 1.14