LCOV - code coverage report
Current view: top level - lib_enc - ivas_stereo_mdct_stereo_enc.c (source / functions) Hit Total Coverage
Test: Coverage on main @ 6baab0c613aa6c7100498ed7b93676aa8198a493 Lines: 389 401 97.0 %
Date: 2025-05-28 04:28:20 Functions: 17 17 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 "ivas_cnst.h"
      38             : #include "ivas_prot.h"
      39             : #include "prot.h"
      40             : #include "ivas_rom_com.h"
      41             : #include "ivas_rom_enc.h"
      42             : #include "wmc_auto.h"
      43             : #include "stat_enc.h"
      44             : #ifdef DEBUG_PLOT
      45             : #include "deb_out.h"
      46             : #endif
      47             : 
      48             : 
      49             : /*-------------------------------------------------------------------*
      50             :  * Local constants
      51             :  *-------------------------------------------------------------------*/
      52             : 
      53             : #define OFFSET_BITS_TCX20 126
      54             : #define OFFSET_BITS_TCX10 222
      55             : 
      56             : 
      57             : /*-------------------------------------------------------------------*
      58             :  * Local function prototypes
      59             :  *-------------------------------------------------------------------*/
      60             : 
      61             : static void convertToBwMS( const int16_t startLine, const int16_t stopLine, float x0[], float x1[], const float norm_fac );
      62             : 
      63             : void convertToMS( const int16_t L_frame, float x0[], float x1[], const float norm_fac );
      64             : 
      65             : static float GetChannelEnergyRatio( Encoder_State **st, const int16_t iFirstSubframe, const int16_t iLastSubframe, const uint8_t ratioInRmsDomain );
      66             : 
      67             : static void MsStereoDecision( STEREO_MDCT_BAND_PARAMETERS *sfbParam, float *specL, float *specR, float *specM, float *specS, int16_t *mdct_stereo_mode, int16_t *msMask, const int16_t nBitsAvailable );
      68             : 
      69             : 
      70             : /*-------------------------------------------------------------------*
      71             :  * dft_ana_init()
      72             :  *
      73             :  * Initialization function for dft analysis handle within
      74             :  * MDCT-stereo
      75             :  *-------------------------------------------------------------------*/
      76             : 
      77        1662 : static void dft_ana_init(
      78             :     DFT_ANA_HANDLE hDft_ana, /*i  : DFT analysis handle         */
      79             :     const int32_t input_Fs   /*i  : Input sampling frequency    */
      80             : )
      81             : {
      82        1662 :     hDft_ana->N = (int16_t) ( STEREO_DFT_HOP_MAX_ENC * input_Fs / 48000 );
      83        1662 :     hDft_ana->NFFT = (int16_t) ( STEREO_DFT_N_MAX_ENC * input_Fs / 48000 );
      84        1662 :     hDft_ana->dft_ovl = (int16_t) ( STEREO_DFT_OVL_MAX * input_Fs / 48000 );
      85        1662 :     hDft_ana->dft_zp = (int16_t) ( STEREO_DFT_ZP_MAX_ENC * input_Fs / 48000 );
      86             : 
      87        1662 :     hDft_ana->dft_trigo_32k = dft_trigo_32k;
      88             : 
      89        1662 :     if ( input_Fs == 16000 )
      90             :     {
      91          16 :         hDft_ana->dft_trigo = dft_trigo_32k;
      92          16 :         hDft_ana->dft_trigo_step = STEREO_DFT_TRIGO_SRATE_16k_STEP;
      93          16 :         hDft_ana->win_ana = win_ana_16k;
      94             :     }
      95        1646 :     else if ( input_Fs == 32000 )
      96             :     {
      97         623 :         hDft_ana->dft_trigo = dft_trigo_32k;
      98         623 :         hDft_ana->dft_trigo_step = STEREO_DFT_TRIGO_SRATE_32k_STEP;
      99         623 :         hDft_ana->win_ana = win_ana_32k;
     100             :     }
     101             :     else
     102             :     {
     103        1023 :         assert( input_Fs == 48000 );
     104        1023 :         hDft_ana->dft_trigo = dft_trigo_48k;
     105        1023 :         hDft_ana->dft_trigo_step = STEREO_DFT_TRIGO_SRATE_48k_STEP;
     106        1023 :         hDft_ana->win_ana = win_ana_48k;
     107             :     }
     108             : 
     109        1662 :     return;
     110             : }
     111             : 
     112             : 
     113             : /*-------------------------------------------------------------------*
     114             :  * write_itd_data()
     115             :  *
     116             :  * Bitstream writing of ITDs
     117             :  *-------------------------------------------------------------------*/
     118             : 
     119      147220 : static void write_itd_data(
     120             :     ITD_DATA_HANDLE hItd, /* i  : ITD data handle     */
     121             :     BSTR_ENC_HANDLE hBstr /* i/o: bitstream handle    */
     122             : )
     123             : {
     124             :     int16_t k_offset;
     125             :     int16_t itd;
     126             : 
     127      147220 :     k_offset = 1;
     128             : 
     129      147220 :     push_next_indice( hBstr, ( hItd->itd[k_offset] != 0 ), STEREO_DFT_ITD_MODE_NBITS );
     130             : 
     131      147220 :     if ( hItd->itd[k_offset] )
     132             :     {
     133       30134 :         itd = hItd->itd_index[k_offset];
     134       30134 :         if ( itd > 255 )
     135             :         {
     136       12989 :             itd -= 256;
     137             : 
     138       12989 :             if ( itd < 20 )
     139             :             {
     140        9579 :                 push_next_indice( hBstr, 1, 1 ); /* use Huffman*/
     141        9579 :                 push_next_indice( hBstr, 1, 1 ); /* negative */
     142        9579 :                 push_next_indice( hBstr, dft_code_itd[itd], dft_len_itd[itd] );
     143             :             }
     144             :             else
     145             :             {
     146        3410 :                 push_next_indice( hBstr, 0, 1 ); /* don't use Huffman */
     147        3410 :                 push_next_indice( hBstr, 1, 1 ); /* negative */
     148        3410 :                 push_next_indice( hBstr, itd, STEREO_DFT_ITD_NBITS - 1 );
     149             :             }
     150             :         }
     151             :         else
     152             :         {
     153       17145 :             if ( itd < 20 )
     154             :             {
     155       13941 :                 push_next_indice( hBstr, 1, 1 ); /* use Huffman*/
     156       13941 :                 push_next_indice( hBstr, 0, 1 ); /* positive */
     157       13941 :                 push_next_indice( hBstr, dft_code_itd[itd], dft_len_itd[itd] );
     158             :             }
     159             :             else
     160             :             {
     161             :                 /* don't use Huffman and positive*/
     162        3204 :                 push_next_indice( hBstr, itd, STEREO_DFT_ITD_NBITS + 1 );
     163             :             }
     164             :         }
     165             :     }
     166      147220 :     return;
     167             : }
     168             : 
     169             : 
     170             : /*-------------------------------------------------------------------*
     171             :  * stereo_coder_tcx()
     172             :  *
     173             :  *
     174             :  *-------------------------------------------------------------------*/
     175             : 
     176     2343231 : void stereo_coder_tcx(
     177             :     STEREO_MDCT_ENC_DATA_HANDLE hStereoMdct,        /* i/o: Stereo MDCT encoder structure   */
     178             :     Encoder_State **sts,                            /* i/o: encoder state structure         */
     179             :     int16_t ms_mask[NB_DIV][MAX_SFB],               /* i  : bandwise MS mask                */
     180             :     float *mdst_spectrum[CPE_CHANNELS][NB_DIV],     /* i/o: MDST spectrum                   */
     181             :     float *inv_spectrum[CPE_CHANNELS][NB_DIV],      /* i/o: inverse spectrum                */
     182             :     float *inv_mdst_spectrum[CPE_CHANNELS][NB_DIV], /* i/o: inverse MDST spectrum           */
     183             :     const int16_t mct_on                            /* i  : flag mct block (1) or stereo (0)  */
     184             : )
     185             : {
     186     2343231 :     STEREO_MDCT_BAND_PARAMETERS *sfbConf = NULL;
     187             :     float nrgRatio[CPE_CHANNELS];
     188             :     float nonQNrgRatio[CPE_CHANNELS];
     189             :     int16_t k;
     190             :     int16_t nSubframes, L_frameTCX;
     191             :     int16_t nAvailBitsMS[NB_DIV];
     192     2343231 :     push_wmops( "stereo_coder_tcx" );
     193             : 
     194     2343231 :     set_s( nAvailBitsMS, 0, NB_DIV );
     195             : 
     196     2343231 :     nSubframes = ( sts[0]->core == TCX_20_CORE && sts[1]->core == TCX_20_CORE ) ? 1 : NB_DIV;
     197     2343231 :     L_frameTCX = sts[0]->hTcxEnc->L_frameTCX / nSubframes;
     198             : 
     199     2343231 :     set_s( &ms_mask[0][0], 0, MAX_SFB );
     200     2343231 :     set_s( &ms_mask[1][0], 0, MAX_SFB );
     201             : 
     202     2343231 :     if ( !mct_on )
     203             :     {
     204      786993 :         if ( sts[0]->core == sts[1]->core )
     205             :         {
     206     1583828 :             for ( k = 0; k < nSubframes; k++ )
     207             :             {
     208      801813 :                 nonQNrgRatio[k] = GetChannelEnergyRatio( sts, k, k, 1 );
     209             : 
     210      801813 :                 hStereoMdct->global_ild[k] = max( 1, min( SMDCT_ILD_RANGE - 1, (int16_t) ( SMDCT_ILD_RANGE * nonQNrgRatio[k] + 0.5f ) ) );
     211      801813 :                 nrgRatio[k] = (float) SMDCT_ILD_RANGE / hStereoMdct->global_ild[k] - 1; /* nrgRatio = nrg[1]/nrg[0] */
     212             : 
     213      801813 :                 nonQNrgRatio[k] = max( 0.5f / SMDCT_ILD_RANGE, min( ( SMDCT_ILD_RANGE - 0.5f ) / SMDCT_ILD_RANGE, nonQNrgRatio[k] ) );
     214      801813 :                 nonQNrgRatio[k] = 1.0f / nonQNrgRatio[k] - 1.0f;
     215             :             }
     216             : #ifdef DEBUG_MODE_MDCT
     217             :             {
     218             :                 float tmp;
     219             :                 for ( k = 0; k < 2; k++ )
     220             :                 {
     221             :                     tmp = GetChannelEnergyRatio( sts, k, k, 1 );
     222             :                     if ( nSubframes == 1 && k == 1 )
     223             :                     {
     224             :                         tmp = 0;
     225             :                     }
     226             :                     dbgwrite( &tmp, sizeof( float ), 1, 1, "./res/ild" );
     227             :                 }
     228             :             }
     229             : #endif
     230             :         }
     231             :         else
     232             :         {
     233        4978 :             nonQNrgRatio[0] = nonQNrgRatio[1] = GetChannelEnergyRatio( sts, 0, nSubframes - 1,
     234             :                                                                        1 );
     235        4978 :             hStereoMdct->global_ild[0] = max( 1, min( SMDCT_ILD_RANGE - 1, (int16_t) ( SMDCT_ILD_RANGE * nonQNrgRatio[0] + 0.5f ) ) );
     236        4978 :             nrgRatio[0] = nrgRatio[1] = (float) SMDCT_ILD_RANGE / hStereoMdct->global_ild[0] - 1; /* nrgRatio = nrg[1]/nrg[0] */
     237        4978 :             nonQNrgRatio[0] = nonQNrgRatio[1] = max( 0.5f / SMDCT_ILD_RANGE, min( ( SMDCT_ILD_RANGE - 0.5f ) / SMDCT_ILD_RANGE, nonQNrgRatio[0] ) );
     238        4978 :             nonQNrgRatio[0] = nonQNrgRatio[1] = 1.0f / nonQNrgRatio[0] - 1.0f;
     239             : #ifdef DEBUG_MODE_MDCT
     240             :             {
     241             :                 float tmp;
     242             :                 for ( k = 0; k < 2; k++ )
     243             :                 {
     244             :                     tmp = GetChannelEnergyRatio( sts, 0, nSubframes - 1, 1 );
     245             :                     if ( nSubframes == 1 && k == 1 )
     246             :                     {
     247             :                         tmp = 0;
     248             :                     }
     249             :                     dbgwrite( &tmp, sizeof( float ), 1, 1, "./res/ild" );
     250             :                 }
     251             :             }
     252             : #endif
     253             :         }
     254             : 
     255     1598762 :         for ( k = 0; k < nSubframes; k++ )
     256             :         {
     257      811769 :             if ( ( nrgRatio[k] > 1.0f ) && ( k < ( ( sts[1]->core == TCX_20_CORE ) ? 1 : NB_DIV ) ) )
     258             :             {
     259      264721 :                 L_frameTCX = sts[1]->hTcxEnc->L_frameTCX + ( ( sts[1]->last_core == 0 ) ? sts[1]->hTcxEnc->L_frameTCX / 4 : 0 );
     260      264721 :                 L_frameTCX /= ( ( sts[1]->core == TCX_20_CORE ) ? 1 : NB_DIV );
     261             : 
     262      264721 :                 v_multc( sts[1]->hTcxEnc->spectrum[k], 1.0f / nrgRatio[k], sts[1]->hTcxEnc->spectrum[k], L_frameTCX );
     263      264721 :                 v_multc( mdst_spectrum[1][k], 1.0f / nrgRatio[k], mdst_spectrum[1][k], L_frameTCX );
     264             :             }
     265      547048 :             else if ( ( nrgRatio[k] < 1.0f ) && k < ( ( sts[0]->core == TCX_20_CORE ) ? 1 : NB_DIV ) )
     266             :             {
     267      206124 :                 L_frameTCX = sts[0]->hTcxEnc->L_frameTCX + ( ( sts[0]->last_core == 0 ) ? sts[0]->hTcxEnc->L_frameTCX / 4 : 0 );
     268      206124 :                 L_frameTCX /= ( ( sts[0]->core == TCX_20_CORE ) ? 1 : NB_DIV );
     269             : 
     270      206124 :                 v_multc( sts[0]->hTcxEnc->spectrum[k], nrgRatio[k], sts[0]->hTcxEnc->spectrum[k], L_frameTCX );
     271      206124 :                 v_multc( mdst_spectrum[0][k], nrgRatio[k], mdst_spectrum[0][k], L_frameTCX );
     272             :             }
     273             :         }
     274             :     }
     275             : 
     276     2343231 :     if (
     277             : #ifdef DEBUG_FORCE_MDCT_STEREO_MODE
     278             :         hStereoMdct->fDualMono ||
     279             : #endif
     280     2343231 :         ( sts[0]->hTcxEnc->transform_type[0] != sts[1]->hTcxEnc->transform_type[0] ) || ( sts[0]->hTcxEnc->transform_type[1] != sts[1]->hTcxEnc->transform_type[1] ) || ( sts[0]->last_core != sts[1]->last_core && ( sts[0]->last_core == ACELP_CORE || sts[1]->last_core == ACELP_CORE ) ) || sts[0]->last_core == ACELP_CORE || sts[1]->last_core == ACELP_CORE )
     281             :     {
     282       15966 :         hStereoMdct->mdct_stereo_mode[0] = SMDCT_DUAL_MONO;
     283       15966 :         hStereoMdct->mdct_stereo_mode[1] = SMDCT_DUAL_MONO;
     284             : 
     285             : #ifdef DEBUG_MODE_MDCT
     286             :         for ( k = 0; k < 2; k++ )
     287             :         {
     288             :             nAvailBitsMS[k] = -1;
     289             :             dbgwrite( &nAvailBitsMS[k], sizeof( int16_t ), 1, 1, "./res/nAvailBitsMS" );
     290             :         }
     291             : #endif
     292             : 
     293       15966 :         if ( sts[0]->igf )
     294             :         {
     295       11117 :             hStereoMdct->IGFStereoMode[0] = SMDCT_DUAL_MONO;
     296       11117 :             hStereoMdct->IGFStereoMode[1] = SMDCT_DUAL_MONO;
     297             :         }
     298             : #ifdef DEBUG_MODE_MDCT
     299             :         /* MDCT stereo data */
     300             :         {
     301             :             float Em[2];
     302             :             int16_t ch;
     303             :             getChannelEnergies( sts, Em, 2 );
     304             :             dbgwrite( Em, sizeof( float ), 2, 1, "./res/Ech" );
     305             :             for ( k = 0; k < 2; k++ )
     306             :             {
     307             :                 dbgwrite( &hStereoMdct->global_ild[k], sizeof( int16_t ), 1, 1, "./res/ild_q" );
     308             :                 dbgwrite( &hStereoMdct->mdct_stereo_mode[k], sizeof( int16_t ), 1, 1, "./res/stereo_mode" );
     309             :                 dbgwrite( &hStereoMdct->IGFStereoMode[k], sizeof( int16_t ), 1, 1, "./res/stereo_mode_ifg" );
     310             :                 dbgwrite( ms_mask[k], sizeof( int16_t ), 70, 1, "./res/ms_mask" );
     311             :             }
     312             :             for ( ch = 0; ch < CPE_CHANNELS; ch++ )
     313             :             {
     314             :                 for ( k = 0; k < 2; k++ )
     315             :                 {
     316             :                     dbgwrite( sts[ch]->hTcxEnc->spectrum[k], sizeof( float ), 640, 1, "./res/MDCT_spec_after_stereo" );
     317             :                     dbgwrite( mdst_spectrum[ch][k], sizeof( float ), 640, 1, "./res/MDST_spec_after_stereo" );
     318             :                 }
     319             :             }
     320             :         }
     321             : #endif
     322       15966 :         hStereoMdct->sw_uncorr = 1;
     323             : 
     324       15966 :         pop_wmops();
     325       15966 :         return;
     326             :     }
     327             : #ifdef DEBUG_FORCE_MDCT_STEREO_MODE
     328             :     else if ( hStereoMdct->fMSstereo )
     329             :     {
     330             :         hStereoMdct->mdct_stereo_mode[0] = SMDCT_MS_FULL;
     331             :         hStereoMdct->mdct_stereo_mode[1] = SMDCT_MS_FULL;
     332             :         if ( sts[0]->igf )
     333             :         {
     334             :             hStereoMdct->IGFStereoMode[0] = SMDCT_MS_FULL;
     335             :             hStereoMdct->IGFStereoMode[1] = SMDCT_MS_FULL;
     336             :         }
     337             :         for ( k = 0; k < nSubframes; k++ )
     338             :         {
     339             :             convertToMS( L_frameTCX, sts[0]->hTcxEnc->spectrum[k], sts[1]->hTcxEnc->spectrum[k], SQRT2_OVER_2 );
     340             : 
     341             :             /* Make sure that the MDST is processed in the correct way also */
     342             :             set_s( &ms_mask[k][0], 1, MAX_SFB );
     343             :         }
     344             : 
     345             :         pop_wmops();
     346             :         return;
     347             :     }
     348             : #endif
     349             :     else /* decide based on signal */
     350             :     {
     351     4709747 :         for ( k = 0; k < nSubframes; k++ )
     352             :         {
     353     2382482 :             sfbConf = ( sts[0]->core == TCX_20_CORE ) ? &hStereoMdct->stbParamsTCX20 : &hStereoMdct->stbParamsTCX10;
     354     2382482 :             if ( sts[0]->last_core == ACELP_CORE )
     355             :             {
     356           0 :                 sfbConf = &hStereoMdct->stbParamsTCX20afterACELP;
     357             :             }
     358     2382482 :             if ( ( sts[0]->element_brate < IVAS_80k ) && ( sts[0]->core == sts[1]->core ) && !mct_on ) /* band-wise HF ILD alignment to increase channel compaction */
     359             :             {
     360      432273 :                 int16_t sfb = 1;
     361             : 
     362     7150897 :                 while ( ( sfb < sfbConf->nBandsStereoCore ) && ( sfbConf->sfbOffset[sfb + 1] - sfbConf->sfbOffset[sfb] < 12 ) )
     363             :                 {
     364     6718624 :                     sfb++; /* find start offset */
     365             :                 }
     366             : 
     367     7284094 :                 for ( sfb--; sfb < sfbConf->nBandsStereoCore; sfb++ ) /* start one SFB early for the fade-in */
     368             :                 {
     369     6851821 :                     const int16_t startLine = sfbConf->sfbOffset[sfb];
     370     6851821 :                     const int16_t endLine = sfbConf->sfbOffset[sfb + 1];
     371     6851821 :                     const int16_t sfbWidth = endLine - startLine;
     372             : 
     373     6851821 :                     nrgRatio[0] = sum2_f( &sts[0]->hTcxEnc->spectrum[k][startLine], sfbWidth );
     374     6851821 :                     nrgRatio[1] = sum2_f( &sts[1]->hTcxEnc->spectrum[k][startLine], sfbWidth );
     375             : 
     376     6851821 :                     if ( !sts[0]->hTcxEnc->fUseTns[k] && !sts[1]->hTcxEnc->fUseTns[k] ) /* no TNS in either ch */
     377             :                     {
     378     5761009 :                         nrgRatio[0] += sum2_f( &mdst_spectrum[0][k][startLine], sfbWidth );
     379     5761009 :                         nrgRatio[1] += sum2_f( &mdst_spectrum[1][k][startLine], sfbWidth );
     380             :                     }
     381     6851821 :                     if ( ( nrgRatio[0] > 0.f ) && ( nrgRatio[1] > 0.f ) && ( nrgRatio[0] != nrgRatio[1] ) )
     382             :                     {
     383     6468142 :                         float fTemp = 0.5f * ( nrgRatio[0] + nrgRatio[1] );
     384     6468142 :                         nrgRatio[0] = sqrtf( fTemp / nrgRatio[0] );
     385     6468142 :                         nrgRatio[1] = sqrtf( fTemp / nrgRatio[1] );
     386             : 
     387     6468142 :                         nrgRatio[0] = max( 0.25f, min( 4.f, nrgRatio[0] ) );
     388     6468142 :                         nrgRatio[1] = max( 0.25f, min( 4.f, nrgRatio[1] ) );
     389             : 
     390     6468142 :                         if ( ( sfbWidth < 12 && sfb + 1 < sfbConf->nBandsStereoCore ) || ( sts[0]->element_brate > IVAS_48k ) ) /* attenuate ILD alignment in the first SFB or at 64k */
     391             :                         {
     392     3505941 :                             nrgRatio[0] = powf( nrgRatio[0], 0.25f );
     393     3505941 :                             nrgRatio[1] = powf( nrgRatio[1], 0.25f );
     394             :                         }
     395             :                         else
     396             :                         {
     397     2962201 :                             nrgRatio[0] = sqrtf( nrgRatio[0] );
     398     2962201 :                             nrgRatio[1] = sqrtf( nrgRatio[1] );
     399             :                         }
     400     6468142 :                         v_multc( &sts[0]->hTcxEnc->spectrum[k][startLine], nrgRatio[0], &sts[0]->hTcxEnc->spectrum[k][startLine], sfbWidth );
     401     6468142 :                         v_multc( &mdst_spectrum[0][k][startLine], nrgRatio[0], &mdst_spectrum[0][k][startLine], sfbWidth );
     402             : 
     403     6468142 :                         v_multc( &sts[1]->hTcxEnc->spectrum[k][startLine], nrgRatio[1], &sts[1]->hTcxEnc->spectrum[k][startLine], sfbWidth );
     404     6468142 :                         v_multc( &mdst_spectrum[1][k][startLine], nrgRatio[1], &mdst_spectrum[1][k][startLine], sfbWidth );
     405             :                     }
     406             :                 }
     407             :             }
     408             : 
     409             :             /* set mask to zero */
     410     2382482 :             set_s( &ms_mask[k][0], 0, MAX_SFB );
     411             : 
     412     2382482 :             nAvailBitsMS[k] = ( ( mct_on ? 2 * sts[0]->bits_frame_channel : sts[0]->bits_frame_nominal ) - sts[0]->side_bits_frame_channel - sts[1]->side_bits_frame_channel - ( nSubframes == 2 ? OFFSET_BITS_TCX10 : OFFSET_BITS_TCX20 ) ) / nSubframes;
     413             : 
     414     2382482 :             MsStereoDecision( sfbConf, sts[0]->hTcxEnc->spectrum[k], sts[1]->hTcxEnc->spectrum[k], inv_spectrum[0][k], inv_spectrum[1][k], &hStereoMdct->mdct_stereo_mode[k], &ms_mask[k][0], nAvailBitsMS[k] );
     415             : 
     416     2382482 :             if ( sts[0]->igf )
     417             :             {
     418     1699494 :                 IGFEncStereoEncoder( sfbConf, sts[0]->hIGFEnc, sts[0]->hTcxEnc->spectrum[k], sts[1]->hTcxEnc->spectrum[k], &ms_mask[k][0],
     419     1699494 :                                      &hStereoMdct->IGFStereoMode[k], hStereoMdct->mdct_stereo_mode[k], sts[0]->core == TCX_20_CORE, sts[0]->last_core == ACELP_CORE );
     420             :             }
     421             :             else
     422             :             {
     423      682988 :                 hStereoMdct->IGFStereoMode[k] = hStereoMdct->mdct_stereo_mode[k];
     424             :             }
     425             : 
     426     2382482 :             if ( hStereoMdct->mdct_stereo_mode[k] != SMDCT_DUAL_MONO || hStereoMdct->IGFStereoMode[k] != SMDCT_DUAL_MONO )
     427             :             {
     428     2265769 :                 ms_inv_mask_processing( hStereoMdct, sts, ms_mask, k, sts[0]->hTcxEnc->spectrum[k], sts[1]->hTcxEnc->spectrum[k], inv_spectrum[0][k], inv_spectrum[1][k], sfbConf->sfbCnt );
     429     2265769 :                 ms_processing( hStereoMdct, sts, ms_mask, k, sts[0]->hTcxEnc->spectrum[k], sts[1]->hTcxEnc->spectrum[k], sfbConf->sfbCnt );
     430             : 
     431     2265769 :                 if ( !sts[0]->hTcxEnc->fUseTns[k] && !sts[1]->hTcxEnc->fUseTns[k] )
     432             :                 {
     433     1951547 :                     sts[0]->hTcxEnc->tns_ms_flag[k] = 1;
     434     1951547 :                     sts[1]->hTcxEnc->tns_ms_flag[k] = 1;
     435     1951547 :                     ms_inv_mask_processing( hStereoMdct, sts, ms_mask, k, mdst_spectrum[0][k], mdst_spectrum[1][k], inv_mdst_spectrum[0][k], inv_mdst_spectrum[1][k], -1 );
     436     1951547 :                     ms_processing( hStereoMdct, sts, ms_mask, k, mdst_spectrum[0][k], mdst_spectrum[1][k], sfbConf->sfbCnt );
     437             :                 }
     438             :             }
     439             :         } /* for k */
     440             : 
     441             : #ifdef DEBUG_MODE_MDCT
     442             :         for ( k = 0; k < 2; k++ )
     443             :         {
     444             :             dbgwrite( &nAvailBitsMS[k], sizeof( int16_t ), 1, 1, "./res/nAvailBitsMS" );
     445             :         }
     446             : #endif
     447             :     }
     448             :     /*  for bitrate switching determine correlation depending on m/s decision */
     449             :     {
     450             :         int16_t ms_bands[2];
     451             :         float sw_uncorr[2], sw_uncorr_mean;
     452     4709747 :         for ( k = 0; k < nSubframes; k++ )
     453             :         {
     454     2382482 :             sfbConf = ( sts[0]->core == TCX_20_CORE ) ? &hStereoMdct->stbParamsTCX20 : &hStereoMdct->stbParamsTCX10;
     455     2382482 :             ms_bands[k] = sum_s( ms_mask[k], sfbConf->nBandsStereoCore );
     456     2382482 :             sw_uncorr[k] = ( 1.0f - (float) ms_bands[k] / sfbConf->nBandsStereoCore );
     457             :         }
     458     2327265 :         if ( sts[0]->core == TCX_20_CORE )
     459             :         {
     460     2272048 :             sw_uncorr_mean = sw_uncorr[0];
     461             :         }
     462             :         else
     463             :         {
     464       55217 :             sw_uncorr_mean = ( sw_uncorr[0] + sw_uncorr[1] ) * 0.5f;
     465             :         }
     466     2327265 :         hStereoMdct->sw_uncorr = ( sw_uncorr_mean > 0.6f );
     467             :     }
     468             : 
     469     2327265 :     pop_wmops();
     470             : 
     471             : #ifdef DEBUG_MODE_MDCT
     472             :     /* MDCT stereo data */
     473             :     {
     474             :         float Em[2];
     475             :         int16_t ch;
     476             :         getChannelEnergies( sts, Em, 2 );
     477             :         dbgwrite( Em, sizeof( float ), 2, 1, "./res/Ech" );
     478             :         for ( k = 0; k < 2; k++ )
     479             :         {
     480             :             dbgwrite( &hStereoMdct->global_ild[k], sizeof( int16_t ), 1, 1, "./res/ild_q" );
     481             :             dbgwrite( &hStereoMdct->mdct_stereo_mode[k], sizeof( int16_t ), 1, 1, "./res/stereo_mode" );
     482             :             dbgwrite( &hStereoMdct->IGFStereoMode[k], sizeof( int16_t ), 1, 1, "./res/stereo_mode_ifg" );
     483             :             dbgwrite( ms_mask[k], sizeof( int16_t ), 70, 1, "./res/ms_mask" );
     484             :         }
     485             :         for ( ch = 0; ch < CPE_CHANNELS; ch++ )
     486             :         {
     487             :             for ( k = 0; k < 2; k++ )
     488             :             {
     489             :                 dbgwrite( sts[ch]->hTcxEnc->spectrum[k], sizeof( float ), 640, 1, "./res/MDCT_spec_after_stereo" );
     490             :                 dbgwrite( mdst_spectrum[ch][k], sizeof( float ), 640, 1, "./res/MDST_spec_after_stereo" );
     491             :             }
     492             :         }
     493             :     }
     494             : #endif
     495             : 
     496     2327265 :     return;
     497             : }
     498             : 
     499             : 
     500             : /*-------------------------------------------------------------------*
     501             :  * ms_processing()
     502             :  *
     503             :  *
     504             :  *-------------------------------------------------------------------*/
     505             : 
     506     4217316 : void ms_processing(
     507             :     STEREO_MDCT_ENC_DATA_HANDLE hStereoMdct, /* i/o: Stereo MDCT encoder structure   */
     508             :     Encoder_State **sts,                     /* i/o: Encoder state structure         */
     509             :     int16_t ms_mask[NB_DIV][MAX_SFB],        /* i  : bandwise MS mask                */
     510             :     const int16_t iSubframe,                 /* i  : subframe number                 */
     511             :     float x_0[],                             /* i/o: spectrum 1                      */
     512             :     float x_1[],                             /* i/o: spectrum 1                      */
     513             :     int16_t maxSfb                           /* i  : number of stereo frequency bands*/
     514             : )
     515             : {
     516             :     int16_t sfb;
     517             :     STEREO_MDCT_BAND_PARAMETERS *sfbConf;
     518             : 
     519     4217316 :     sfbConf = ( sts[0]->core == TCX_20_CORE ) ? &hStereoMdct->stbParamsTCX20 : &hStereoMdct->stbParamsTCX10;
     520             : 
     521     4217316 :     if ( sts[0]->last_core == ACELP_CORE )
     522             :     {
     523           0 :         assert( sts[1]->last_core == ACELP_CORE );
     524           0 :         sfbConf = &hStereoMdct->stbParamsTCX20afterACELP;
     525             :     }
     526             : 
     527     4217316 :     if ( maxSfb == -1 )
     528             :     {
     529           0 :         maxSfb = sfbConf->sfbCnt;
     530             :     }
     531             : 
     532   183583611 :     for ( sfb = 0; sfb < maxSfb; sfb++ )
     533             :     {
     534   179366295 :         if ( ms_mask[iSubframe][sfb] )
     535             :         {
     536   155614628 :             convertToBwMS( sfbConf->sfbOffset[sfb], sfbConf->sfbOffset[sfb + 1], x_0, x_1, SQRT2_OVER_2 );
     537             :         }
     538             :     }
     539             : 
     540     4217316 :     return;
     541             : }
     542             : 
     543             : 
     544             : /*-------------------------------------------------------------------*
     545             :  * ms_inv_mask_processing()
     546             :  *
     547             :  *
     548             :  *-------------------------------------------------------------------*/
     549             : 
     550     4217316 : void ms_inv_mask_processing(
     551             :     STEREO_MDCT_ENC_DATA_HANDLE hStereoMdct, /* i/o: Stereo MDCT encoder structure   */
     552             :     Encoder_State **sts,                     /* i/o: Encoder state structure         */
     553             :     int16_t ms_mask[NB_DIV][MAX_SFB],        /* i  : bandwise MS mask                */
     554             :     const int16_t iSubframe,                 /* i  : subframe number                 */
     555             :     const float x_0[],                       /* i  : spectrum 1                      */
     556             :     const float x_1[],                       /* i  : spectrum 2                      */
     557             :     float x_inv_0[],                         /* o  : inverse spectrum 1              */
     558             :     float x_inv_1[],                         /* o  : inverse spectrum 2              */
     559             :     int16_t maxSfb                           /* i  : number of stereo frequency bands*/
     560             : )
     561             : {
     562             :     int16_t sfb;
     563             :     STEREO_MDCT_BAND_PARAMETERS *sfbConf;
     564             :     int16_t nSubframes, L_subframeTCX;
     565             : 
     566     4217316 :     nSubframes = ( sts[0]->hTcxEnc->tcxMode == TCX_20 ) ? 1 : NB_DIV;
     567     4217316 :     L_subframeTCX = sts[0]->hTcxEnc->L_frameTCX / nSubframes;
     568     4217316 :     sfbConf = ( sts[0]->core == TCX_20_CORE ) ? &hStereoMdct->stbParamsTCX20 : &hStereoMdct->stbParamsTCX10;
     569             : 
     570     4217316 :     if ( sts[0]->last_core == ACELP_CORE )
     571             :     {
     572           0 :         assert( sts[1]->last_core == ACELP_CORE );
     573           0 :         sfbConf = &hStereoMdct->stbParamsTCX20afterACELP;
     574             :     }
     575             : 
     576     4217316 :     if ( maxSfb == -1 )
     577             :     {
     578     1951547 :         maxSfb = sfbConf->sfbCnt;
     579             :     }
     580             : 
     581   183583611 :     for ( sfb = 0; sfb < maxSfb; sfb++ )
     582             :     {
     583   179366295 :         mvr2r( &x_0[sfbConf->sfbOffset[sfb]], &x_inv_0[sfbConf->sfbOffset[sfb]], sfbConf->sfbOffset[sfb + 1] - sfbConf->sfbOffset[sfb] );
     584   179366295 :         mvr2r( &x_1[sfbConf->sfbOffset[sfb]], &x_inv_1[sfbConf->sfbOffset[sfb]], sfbConf->sfbOffset[sfb + 1] - sfbConf->sfbOffset[sfb] );
     585             : 
     586   179366295 :         if ( ms_mask[iSubframe][sfb] == 0 )
     587             :         {
     588    23751667 :             convertToBwMS( sfbConf->sfbOffset[sfb], sfbConf->sfbOffset[sfb + 1], x_inv_0, x_inv_1, SQRT2_OVER_2 );
     589             :         }
     590             :     }
     591             : 
     592             :     /* set rest of inverse spectrum to zero */
     593     4217316 :     if ( L_subframeTCX > sfbConf->sfbOffset[maxSfb] )
     594             :     {
     595     2933898 :         set_zero( &x_inv_0[sfbConf->sfbOffset[maxSfb]], L_subframeTCX - sfbConf->sfbOffset[maxSfb] );
     596     2933898 :         set_zero( &x_inv_1[sfbConf->sfbOffset[maxSfb]], L_subframeTCX - sfbConf->sfbOffset[maxSfb] );
     597             :     }
     598             : 
     599     4217316 :     return;
     600             : }
     601             : 
     602             : 
     603             : /*-------------------------------------------------------------------*
     604             :  * write_stereo_to_bitstream()
     605             :  *
     606             :  *
     607             :  *-------------------------------------------------------------------*/
     608             : 
     609     2305868 : int16_t write_stereo_to_bitstream(
     610             :     STEREO_MDCT_ENC_DATA_HANDLE hStereoMdct, /* i/o: Stereo MDCT encoder structure   */
     611             :     Encoder_State **sts,                     /* i/o: Encoder state structure         */
     612             :     int16_t ms_mask[NB_DIV][MAX_SFB],        /* i  : bandwise MS mask                */
     613             :     const int16_t mct_on,                    /* i  : flag mct block (1) or stereo (0)*/
     614             :     BSTR_ENC_HANDLE hBstr                    /* i/o: bitstream handle                */
     615             : )
     616             : {
     617             :     int16_t i, k, nSubframes;
     618             :     uint16_t mdct_stereo_mode, stereo_mode_bits;
     619             :     STEREO_MDCT_BAND_PARAMETERS *sfbConf;
     620     2305868 :     int16_t start_bits = hBstr->nb_bits_tot;
     621             : 
     622     2305868 :     if ( !mct_on )
     623             :     {
     624      786993 :         assert( ( ( sts[0]->hTcxEnc->transform_type[0] == sts[1]->hTcxEnc->transform_type[0] ) && ( sts[0]->hTcxEnc->transform_type[1] == sts[1]->hTcxEnc->transform_type[1] ) ) || ( ( hStereoMdct->mdct_stereo_mode[0] == SMDCT_DUAL_MONO ) && ( hStereoMdct->mdct_stereo_mode[1] == SMDCT_DUAL_MONO ) ) );
     625             :     }
     626             : 
     627     2305868 :     nSubframes = ( sts[0]->core == TCX_10_CORE || ( sts[0]->core != sts[1]->core ) ) ? NB_DIV : 1;
     628     2305868 :     sfbConf = ( sts[0]->core == TCX_20_CORE ) ? &hStereoMdct->stbParamsTCX20 : &hStereoMdct->stbParamsTCX10;
     629             : 
     630     2305868 :     if ( sts[0]->last_core == ACELP_CORE )
     631             :     {
     632       10464 :         assert( sts[1]->ini_frame == 0 || sts[1]->last_core == ACELP_CORE );
     633       10464 :         sfbConf = &hStereoMdct->stbParamsTCX20afterACELP;
     634             :     }
     635             : 
     636     2305868 :     if ( hStereoMdct->hItd != NULL )
     637             :     {
     638      147220 :         write_itd_data( hStereoMdct->hItd, hBstr );
     639             :     }
     640             : 
     641     4671975 :     for ( k = 0; k < nSubframes; k++ )
     642             :     {
     643     2366107 :         mdct_stereo_mode = 0;
     644     2366107 :         stereo_mode_bits = 1;
     645             : 
     646     2366107 :         switch ( hStereoMdct->mdct_stereo_mode[k] )
     647             :         {
     648      103923 :             case SMDCT_DUAL_MONO:
     649      103923 :                 mdct_stereo_mode = 0;
     650      103923 :                 stereo_mode_bits = 1;
     651      103923 :                 break;
     652     1638630 :             case SMDCT_MS_FULL:
     653     1638630 :                 mdct_stereo_mode = 2;
     654     1638630 :                 stereo_mode_bits = 2;
     655     1638630 :                 break;
     656      623554 :             case SMDCT_BW_MS:
     657      623554 :                 mdct_stereo_mode = 3;
     658      623554 :                 stereo_mode_bits = 2;
     659      623554 :                 break;
     660           0 :             default:
     661           0 :                 assert( !"Not supported MDCT stereo mode\n" );
     662             :         }
     663             : 
     664     2366107 :         push_next_indice( hBstr, mdct_stereo_mode, stereo_mode_bits );
     665             : 
     666     2366107 :         if ( !mct_on )
     667             :         {
     668      811769 :             if ( ( sts[0]->core == sts[1]->core ) || ( k == 0 ) )
     669             :             {
     670      806791 :                 push_next_indice( hBstr, hStereoMdct->global_ild[k], SMDCT_GLOBAL_ILD_BITS );
     671             :             }
     672             :         }
     673             : 
     674     2366107 :         if ( hStereoMdct->mdct_stereo_mode[k] == SMDCT_BW_MS )
     675             :         {
     676    25183884 :             for ( i = 0; i < sfbConf->nBandsStereoCore; i++ )
     677             :             {
     678    24560330 :                 push_next_indice( hBstr, ms_mask[k][i] ? 1 : 0, 1 );
     679             :             }
     680             :         }
     681             : 
     682     2366107 :         if ( sts[0]->igf )
     683             :         {
     684     1701229 :             mdct_stereo_mode = 0;
     685     1701229 :             stereo_mode_bits = 1;
     686     1701229 :             switch ( hStereoMdct->IGFStereoMode[k] )
     687             :             {
     688      568022 :                 case SMDCT_DUAL_MONO:
     689      568022 :                     mdct_stereo_mode = 0;
     690      568022 :                     stereo_mode_bits = 1;
     691      568022 :                     break;
     692      766053 :                 case SMDCT_MS_FULL:
     693      766053 :                     mdct_stereo_mode = 2;
     694      766053 :                     stereo_mode_bits = 2;
     695      766053 :                     break;
     696      367154 :                 case SMDCT_BW_MS:
     697      367154 :                     mdct_stereo_mode = 3;
     698      367154 :                     stereo_mode_bits = 2;
     699      367154 :                     break;
     700           0 :                 default:
     701           0 :                     assert( !"Not supported MDCT stereo mode\n" );
     702             :             }
     703             : 
     704     1701229 :             push_next_indice( hBstr, mdct_stereo_mode, stereo_mode_bits );
     705             : 
     706             : 
     707     1701229 :             if ( hStereoMdct->IGFStereoMode[k] == SMDCT_BW_MS )
     708             :             {
     709     2482438 :                 for ( i = sfbConf->nBandsStereoCore; i < sfbConf->sfbCnt; i++ )
     710             :                 {
     711     2115284 :                     push_next_indice( hBstr, ms_mask[k][i] ? 1 : 0, 1 );
     712             :                 }
     713             :             }
     714             :         }
     715             :     }
     716             : 
     717     2305868 :     return ( hBstr->nb_bits_tot - start_bits );
     718             : }
     719             : 
     720             : /*-------------------------------------------------------------------*
     721             :  * Band-wise M/S stereo processing
     722             :  *
     723             :  *
     724             :  *-------------------------------------------------------------------*/
     725             : 
     726   180658839 : static void convertToBwMS(
     727             :     const int16_t startLine, /* i  : start line of sfb               */
     728             :     const int16_t stopLine,  /* i  : stop line of sfb                */
     729             :     float x0[],              /* i/o: mid/left channel coefficients   */
     730             :     float x1[],              /* i/o: side/right channel coefficients */
     731             :     const float norm_fac     /* i  : normalization factor            */
     732             : )
     733             : {
     734             :     int16_t j;
     735             :     float tmpValue;
     736             : 
     737  3354894091 :     for ( j = startLine; j < stopLine; j++ )
     738             :     {
     739  3174235252 :         tmpValue = x0[j];
     740  3174235252 :         x0[j] = ( x0[j] + x1[j] ) * norm_fac;
     741  3174235252 :         x1[j] = ( tmpValue - x1[j] ) * norm_fac;
     742             :     }
     743             : 
     744   180658839 :     return;
     745             : }
     746             : 
     747             : /*-------------------------------------------------------------------*
     748             :  * convertToMS()
     749             :  *
     750             :  *
     751             :  *-------------------------------------------------------------------*/
     752             : 
     753     1292544 : void convertToMS(
     754             :     const int16_t L_frame, /* i  : frame length                    */
     755             :     float x0[],            /* i/o: mid/left channel coefficients   */
     756             :     float x1[],            /* i/o: side/right channel coefficients */
     757             :     const float norm_fac   /* i  : normalization factor            */
     758             : )
     759             : {
     760     1292544 :     convertToBwMS( 0, L_frame, x0, x1, norm_fac );
     761             : 
     762     1292544 :     return;
     763             : }
     764             : 
     765             : /*-------------------------------------------------------------------*
     766             :  * SQ_gain_estimate_stereo()
     767             :  *
     768             :  *
     769             :  *-------------------------------------------------------------------*/
     770             : 
     771             : /*! r: SQ gain */
     772     2382482 : static float SQ_gain_estimate_stereo(
     773             :     float xL[],            /* i  : L vector to quantize        */
     774             :     float xR[],            /* i  : R vector to quantize        */
     775             :     const int16_t nbitsSQ, /* i  : number of bits targeted     */
     776             :     const int16_t lg       /* i  : vector size (2048 max)      */
     777             : )
     778             : {
     779             :     int16_t i, q, iter;
     780             :     float ener, tmp, target, fac, offset;
     781             :     float en[N_MAX / 2];
     782             :     int16_t lg2, lg_4, lg2_4;
     783             : 
     784     2382482 :     lg_4 = lg >> 2;
     785     2382482 :     lg2_4 = 2 * lg_4;
     786     2382482 :     lg2 = lg2_4 << 2;
     787     2382482 :     i = 0;
     788             : 
     789     2382482 :     set_f( en, 0.01f, N_MAX / 2 );
     790             : 
     791             :     /* energy of quadruples with 9dB offset */
     792             :     /* ignore that we may take no all lines into account, max. 3 lines at the upper end of the spectrum can be missed (if lg is not a multiple of 4, happens also in SQGain()*/
     793   319444742 :     for ( q = 0; q < lg_4; q++ )
     794             :     {
     795   317062260 :         ener = 0.01f + xL[i] * xL[i] + xL[i + 1] * xL[i + 1] + xL[i + 2] * xL[i + 2] + xL[i + 3] * xL[i + 3];
     796   317062260 :         en[q] = log10f( ener ); /* saves a MAC */
     797   317062260 :         i += 4;
     798             :     }
     799     2382482 :     i = 0;
     800   319444742 :     for ( ; q < lg2_4; q++ )
     801             :     {
     802   317062260 :         ener = 0.01f + xR[i] * xR[i] + xR[i + 1] * xR[i + 1] + xR[i + 2] * xR[i + 2] + xR[i + 3] * xR[i + 3];
     803   317062260 :         en[q] = log10f( ener ); /* saves a MAC */
     804   317062260 :         i += 4;
     805             :     }
     806             : 
     807             :     /* SQ scale: 4 bits / 6 dB per quadruple */
     808     2382482 :     target = 0.15f * (float) ( nbitsSQ - ( lg2 >> 4 ) );
     809     2382482 :     fac = 12.8f;
     810     2382482 :     offset = fac;
     811             : 
     812             :     /* find offset (0 to 128 dB with step of 0.125dB) */
     813    26207302 :     for ( iter = 0; iter < 10; iter++ )
     814             :     {
     815    23824820 :         fac *= 0.5f;
     816    23824820 :         offset -= fac;
     817    23824820 :         ener = 0.0f;
     818             : 
     819  5762710452 :         for ( i = 0; i < lg2_4; i++ )
     820             :         {
     821  5748667220 :             tmp = en[i] - offset;
     822             : 
     823             :             /* avoid SV with 1 bin of amp < 0.5f */
     824  5748667220 :             if ( tmp > 0.3f )
     825             :             {
     826  3236678432 :                 ener += tmp;
     827             : 
     828             :                 /* if ener is above target -> break and increase offset */
     829  3236678432 :                 if ( ener > target )
     830             :                 {
     831     9781588 :                     offset += fac;
     832     9781588 :                     break;
     833             :                 }
     834             :             }
     835             :         }
     836             :     }
     837             : 
     838             :     /* return gain */
     839     2382482 :     return powf( 10.0f, 0.45f + 0.5f * offset );
     840             : }
     841             : 
     842             : /*-------------------------------------------------------------------*
     843             :  * QuantSpecEstimateBits()
     844             :  *
     845             :  *
     846             :  *-------------------------------------------------------------------*/
     847             : 
     848     9529928 : static int16_t QuantSpecEstimateBits(
     849             :     float *spec,
     850             :     float G,
     851             :     const int16_t length,
     852             :     const int16_t nBitsAvailable,
     853             :     int16_t sqQ[] )
     854             : {
     855             :     int16_t stop, sqBits, nEncoded;
     856             :     int16_t lastnz;
     857             : 
     858     9529928 :     tcx_scalar_quantization( spec, sqQ, length, G, 0.5f, NULL, 1 );
     859             : 
     860     9529928 :     stop = 0;
     861             : 
     862     9529928 :     sqBits = RCcontextMapping_encode2_estimate_no_mem_s17_LCS( sqQ, length, &lastnz, &nEncoded, nBitsAvailable, &stop, 0, NULL );
     863             : 
     864     9529928 :     if ( stop != 0 )
     865             :     {
     866     1267794 :         sqBits = stop;
     867             :     }
     868             : 
     869     9529928 :     return sqBits;
     870             : }
     871             : 
     872             : /*-------------------------------------------------------------------*
     873             :  * context_update()
     874             :  *
     875             :  *
     876             :  *-------------------------------------------------------------------*/
     877             : 
     878   183779556 : static void context_update(
     879             :     HANDLE_RC_CONTEXT_MEM ctxSrc,
     880             :     HANDLE_RC_CONTEXT_MEM ctxTarget,
     881             :     const int16_t endLine )
     882             : {
     883             :     int16_t last_nz;
     884             : 
     885             :     /* check if last_nz of target is smaller than endLine, save and update */
     886   183779556 :     last_nz = max( ctxTarget->lastnz, endLine );
     887             : 
     888   183779556 :     mvc2c( (uint8_t *) ctxSrc, (uint8_t *) ctxTarget, sizeof( RC_CONTEXT_MEM ) );
     889   183779556 :     ctxTarget->lastnz = last_nz;
     890             : 
     891   183779556 :     return;
     892             : }
     893             : 
     894             : /*-------------------------------------------------------------------*
     895             :  * GetChannelEnergyRatio()
     896             :  *
     897             :  *
     898             :  *-------------------------------------------------------------------*/
     899             : 
     900     2230408 : static float GetChannelEnergyRatio(
     901             :     Encoder_State **sts, /* i/o: Encoder state structure */
     902             :     const int16_t iFirstSubframe,
     903             :     const int16_t iLastSubframe,
     904             :     const uint8_t ratioInRmsDomain )
     905             : {
     906             :     int16_t ch, n, i;
     907             :     float nrg[2];
     908             : 
     909             :     /* Calculate energies per channel */
     910     6691224 :     for ( ch = 0; ch < CPE_CHANNELS; ch++ )
     911             :     {
     912     4460816 :         const Encoder_State *st = sts[ch];
     913     4460816 :         const int16_t nSubframes = ( st->hTcxEnc->tcxMode == TCX_20 ) ? 1 : NB_DIV;
     914     4460816 :         int16_t L_subframeTCX = st->hTcxEnc->L_frameTCX / nSubframes;
     915             : 
     916     4460816 :         if ( st->last_core == ACELP_CORE )
     917             :         {
     918       42152 :             L_subframeTCX += L_subframeTCX / 4;
     919             :         }
     920     4460816 :         assert( iFirstSubframe >= 0 && iFirstSubframe <= iLastSubframe );
     921             : 
     922     4460816 :         nrg[ch] = 0;
     923     9000377 :         for ( n = iFirstSubframe; n <= min( nSubframes - 1, iLastSubframe ); n++ )
     924             :         {
     925  3566301801 :             for ( i = 0; i < L_subframeTCX; i++ )
     926             :             {
     927  3561762240 :                 nrg[ch] += st->hTcxEnc->spectrum[n][i] * st->hTcxEnc->spectrum[n][i];
     928             :             }
     929             :         }
     930     4460816 :         if ( ratioInRmsDomain )
     931             :         {
     932     4211964 :             nrg[ch] = sqrtf( nrg[ch] );
     933             :         }
     934             :     }
     935             : 
     936     2230408 :     return ( nrg[0] + nrg[1] ) > 0 ? nrg[0] / ( nrg[0] + nrg[1] ) : -1.0f;
     937             : }
     938             : 
     939             : /*-------------------------------------------------------------------*
     940             :  * FindSplitRatio()
     941             :  *
     942             :  *
     943             :  *-------------------------------------------------------------------*/
     944             : 
     945     1423617 : void FindSplitRatio(
     946             :     CPE_ENC_HANDLE hCPE, /* i/o: CPE encoder structure       */
     947             :     Encoder_State **sts  /* i/o: Encoder state structure     */
     948             : )
     949             : {
     950     1423617 :     const uint8_t highRateMdctStereo = ( sts[0]->element_brate < IVAS_80k && sts[0]->core == sts[1]->core && sts[0]->element_mode == IVAS_CPE_MDCT && sts[0]->hTcxEnc->enc_ste_pre_corr_past ? 0 : 1 );
     951             :     float ratio;
     952             : 
     953             :     /* Calculate split ratio and quantize it */
     954     1423617 :     hCPE->hStereoMdct->split_ratio = SMDCT_EQUAL_RATIO_RANGE; /* Equal bits to both channels */
     955             : 
     956     1423617 :     ratio = GetChannelEnergyRatio( sts, 0, 1, highRateMdctStereo );
     957             : 
     958     1423617 :     if ( ratio >= 0 )
     959             :     {
     960     1408424 :         hCPE->hStereoMdct->split_ratio = (uint16_t) ( SMDCT_BITRATE_RATIO_RANGE * ratio + 0.5f );
     961             :         /* Tuning to get closer to the optimal split ratio */
     962     1408424 :         if ( ratio < 8.0f / 9.0f && hCPE->hStereoMdct->split_ratio > SMDCT_EQUAL_RATIO_RANGE + ( SMDCT_BITRATE_RATIO_RANGE >> 4 ) )
     963             :         {
     964      993088 :             hCPE->hStereoMdct->split_ratio -= SMDCT_BITRATE_RATIO_RANGE >> 3;
     965             :         }
     966     1408424 :         if ( ratio > 1.0f / 9.0f && hCPE->hStereoMdct->split_ratio < SMDCT_EQUAL_RATIO_RANGE - ( SMDCT_BITRATE_RATIO_RANGE >> 4 ) )
     967             :         {
     968       12327 :             hCPE->hStereoMdct->split_ratio += SMDCT_BITRATE_RATIO_RANGE >> 3;
     969             :         }
     970             : 
     971     1408424 :         hCPE->hStereoMdct->split_ratio = min( SMDCT_BITRATE_RATIO_RANGE - 1, max( 1, hCPE->hStereoMdct->split_ratio ) );
     972             :     }
     973             : 
     974     1423617 :     return;
     975             : }
     976             : 
     977             : /*-------------------------------------------------------------------*
     978             :  * MsStereoDecision()
     979             :  *
     980             :  *
     981             :  *-------------------------------------------------------------------*/
     982             : 
     983     2382482 : static void MsStereoDecision(
     984             :     STEREO_MDCT_BAND_PARAMETERS *sfbParam,
     985             :     float *specL,
     986             :     float *specR,
     987             :     float *specM,              /* scratch buffer for M, use buffer for inverse MS mask spectrum */
     988             :     float *specS,              /* scratch buffer for M, use buffer for inverse MS mask spectrum */
     989             :     int16_t *mdct_stereo_mode, /* output */
     990             :     int16_t *msMask,           /* output */
     991             :     const int16_t nBitsAvailable )
     992             : {
     993     2382482 :     int16_t length = sfbParam->sfbOffset[sfbParam->nBandsStereoCore];
     994             : 
     995             :     int16_t bitsL, bitsR, bitsM, bitsS;
     996             :     int16_t bitsBW, bitsLR, bitsMS;
     997             :     float G;
     998             :     float GLR;
     999             :     int16_t sfb, i;
    1000             :     int16_t nMSOn; /* Number of MS active bands */
    1001             :     int16_t quantSpecL[N_MAX];
    1002             :     int16_t quantSpecR[N_MAX];
    1003             :     int16_t quantSpecM[N_MAX];
    1004             :     int16_t quantSpecS[N_MAX];
    1005             :     RC_CONTEXT_MEM ctxMem[4];
    1006             :     HANDLE_RC_CONTEXT_MEM ctxL, ctxR, ctxM, ctxS;
    1007             : 
    1008     2382482 :     set_s( quantSpecL, 0, N_MAX );
    1009     2382482 :     set_s( quantSpecR, 0, N_MAX );
    1010     2382482 :     set_s( quantSpecM, 0, N_MAX );
    1011     2382482 :     set_s( quantSpecS, 0, N_MAX );
    1012             : 
    1013     2382482 :     assert( nBitsAvailable > 0 );
    1014             : 
    1015     2382482 :     ctxL = &ctxMem[0];
    1016     2382482 :     ctxR = &ctxMem[1];
    1017     2382482 :     ctxM = &ctxMem[2];
    1018     2382482 :     ctxS = &ctxMem[3];
    1019             : 
    1020     2382482 :     GLR = SQ_gain_estimate_stereo( specL, specR, nBitsAvailable, length );
    1021             : 
    1022  1270631522 :     for ( i = 0; i < length; i++ )
    1023             :     {
    1024  1268249040 :         specM[i] = ( specL[i] + specR[i] ) * SQRT2_OVER_2;
    1025  1268249040 :         specS[i] = ( specL[i] - specR[i] ) * SQRT2_OVER_2;
    1026             :     }
    1027             : 
    1028     2382482 :     G = 0.5f * GLR; /* seems to be favourable to underestimate a bit */
    1029             : 
    1030             :     /* do the full spectrum estimates already here, as side effect we get the quantized spectra... */
    1031     2382482 :     bitsLR = QuantSpecEstimateBits( specL, G, length, nBitsAvailable, quantSpecL ) + QuantSpecEstimateBits( specR, G, length, nBitsAvailable, quantSpecR );
    1032     2382482 :     bitsMS = QuantSpecEstimateBits( specM, G, length, nBitsAvailable, quantSpecM ) + QuantSpecEstimateBits( specS, G, length, nBitsAvailable, quantSpecS );
    1033             : 
    1034             :     /* clean-up MS scratch buffers */
    1035     2382482 :     set_zero( specM, length );
    1036     2382482 :     set_zero( specS, length );
    1037             : 
    1038     2382482 :     nMSOn = 0;
    1039     2382482 :     bitsBW = 0;
    1040             : 
    1041     2382482 :     RCcontextMapping_encode2_estimate_bandWise_start( quantSpecL, length, nBitsAvailable, ctxL );
    1042     2382482 :     RCcontextMapping_encode2_estimate_bandWise_start( quantSpecR, length, nBitsAvailable, ctxR );
    1043             : 
    1044     2382482 :     bitsBW += RCcontextMapping_encode2_estimate_bandWise_start( quantSpecM, length, nBitsAvailable, ctxM );
    1045     2382482 :     bitsBW += RCcontextMapping_encode2_estimate_bandWise_start( quantSpecS, length, nBitsAvailable, ctxS );
    1046             : 
    1047             :     /*find_max_lastnz(ctxL,ctxR,ctxM,ctxS);*/
    1048             : 
    1049    94272260 :     for ( sfb = 0; sfb < sfbParam->nBandsStereoCore; sfb++ )
    1050             :     {
    1051    91889778 :         const int16_t startline = sfbParam->sfbOffset[sfb];
    1052    91889778 :         const int16_t endline = sfbParam->sfbOffset[sfb + 1];
    1053             : 
    1054    91889778 :         bitsL = RCcontextMapping_encode2_estimate_bandWise( quantSpecL, startline, endline, ctxL );
    1055    91889778 :         bitsR = RCcontextMapping_encode2_estimate_bandWise( quantSpecR, startline, endline, ctxR );
    1056    91889778 :         bitsM = RCcontextMapping_encode2_estimate_bandWise( quantSpecM, startline, endline, ctxM );
    1057    91889778 :         bitsS = RCcontextMapping_encode2_estimate_bandWise( quantSpecS, startline, endline, ctxS );
    1058             : 
    1059    91889778 :         if ( bitsM + bitsS <= bitsL + bitsR )
    1060             :         {
    1061    72741355 :             msMask[sfb] = 1;
    1062    72741355 :             ++nMSOn;
    1063    72741355 :             context_update( ctxM, ctxL, endline );
    1064    72741355 :             context_update( ctxS, ctxR, endline );
    1065    72741355 :             bitsBW += bitsM + bitsS;
    1066             :         }
    1067             :         else
    1068             :         {
    1069    19148423 :             msMask[sfb] = 0;
    1070    19148423 :             context_update( ctxL, ctxM, endline );
    1071    19148423 :             context_update( ctxR, ctxS, endline );
    1072    19148423 :             bitsBW += bitsL + bitsR;
    1073             :         }
    1074             : #ifdef DEBUG_MODE_MDCT
    1075             :         dbgwrite( &bitsL, sizeof( int16_t ), 1, 1, "./res/bitsL" );
    1076             :         dbgwrite( &bitsR, sizeof( int16_t ), 1, 1, "./res/bitsR" );
    1077             :         dbgwrite( &bitsM, sizeof( int16_t ), 1, 1, "./res/bitsM" );
    1078             :         dbgwrite( &bitsS, sizeof( int16_t ), 1, 1, "./res/bitsS" );
    1079             : #endif
    1080             :     }
    1081             : 
    1082     2382482 :     bitsBW += sfbParam->nBandsStereoCore; /* Signaling bits */
    1083             : 
    1084     2382482 :     if ( bitsLR < bitsBW )
    1085             :     {
    1086      135486 :         nMSOn = 0;
    1087      135486 :         set_s( msMask, 0, sfbParam->sfbCnt );
    1088      135486 :         bitsBW = bitsLR;
    1089             :     }
    1090             : 
    1091     2382482 :     if ( bitsMS < bitsBW )
    1092             :     {
    1093     1638630 :         nMSOn = sfbParam->nBandsStereoCore;
    1094     1638630 :         set_s( msMask, 1, sfbParam->sfbCnt );
    1095             :     }
    1096             : 
    1097     2382482 :     *mdct_stereo_mode = SMDCT_BW_MS;
    1098     2382482 :     if ( nMSOn == sfbParam->nBandsStereoCore )
    1099             :     {
    1100     1638630 :         *mdct_stereo_mode = SMDCT_MS_FULL;
    1101             :     }
    1102      743852 :     else if ( nMSOn == 0 )
    1103             :     {
    1104      120298 :         *mdct_stereo_mode = SMDCT_DUAL_MONO;
    1105             :     }
    1106             : 
    1107     2382482 :     return;
    1108             : }
    1109             : 
    1110             : 
    1111             : /*-----------------------------------------------------------------------*
    1112             :  * initMdctStereoEncData()
    1113             :  *
    1114             :  * initialize encoder mdct stereo structure
    1115             :  *-----------------------------------------------------------------------*/
    1116             : 
    1117     1199360 : void initMdctStereoEncData(
    1118             :     STEREO_MDCT_ENC_DATA *hStereoMdct, /* i/o: mdct stereo parameters structure   */
    1119             :     const IVAS_FORMAT ivas_format,     /* i  : IVAS format                        */
    1120             :     const int16_t element_mode,        /* i  : element mode                       */
    1121             :     const int32_t element_brate,       /* i  : element bitrate                    */
    1122             :     const int16_t bwidth,              /* i  : bandwidth                          */
    1123             :     const int16_t igf,                 /* i  : flag indicating IGF activity       */
    1124             :     const H_IGF_GRID hIgfGrid,         /* i  : IGF grid setup                     */
    1125             :     const int16_t mem_init             /* i  : initialize memory after malloc     */
    1126             : )
    1127             : {
    1128             :     int16_t tcx_coded_lines;
    1129             : 
    1130     1199360 :     tcx_coded_lines = getNumTcxCodedLines( bwidth );
    1131             : 
    1132             :     /*initialize mdct stereo mode*/
    1133     1199360 :     set_s( hStereoMdct->mdct_stereo_mode, -1, 2 );
    1134             : 
    1135             :     /*Initialize sfb parameteres for TCX20 */
    1136     1199360 :     stereo_mdct_init_bands( tcx_coded_lines, TCX_20_CORE, element_brate, igf, igf ? &hIgfGrid[IGF_GRID_LB_NORM] : NULL, &hStereoMdct->stbParamsTCX20.sfbOffset[0], &hStereoMdct->stbParamsTCX20.sfbCnt );
    1137             : 
    1138             :     /*Initialize sfb parameteres for TCX10 */
    1139     1199360 :     stereo_mdct_init_bands( tcx_coded_lines, TCX_10_CORE, element_brate, igf, igf ? &hIgfGrid[IGF_GRID_LB_SHORT] : NULL,
    1140             :                             &hStereoMdct->stbParamsTCX10.sfbOffset[0], &hStereoMdct->stbParamsTCX10.sfbCnt );
    1141             : 
    1142             :     /*Initialize sfb parameteres for transitions */
    1143     1199360 :     stereo_mdct_init_bands( tcx_coded_lines, -1, element_brate, igf, igf ? &hIgfGrid[IGF_GRID_LB_TRAN] : NULL,
    1144             :                             &hStereoMdct->stbParamsTCX20afterACELP.sfbOffset[0], &hStereoMdct->stbParamsTCX20afterACELP.sfbCnt );
    1145             : 
    1146     1199360 :     set_s( hStereoMdct->IGFStereoMode, -1, 2 );
    1147             : 
    1148             : #ifdef DEBUG_FORCE_MDCT_STEREO_MODE
    1149             :     /*set all other members to defined states */
    1150             :     hStereoMdct->fDualMono = 0;
    1151             :     hStereoMdct->fMSstereo = 0;
    1152             : 
    1153             :     if ( hStereoMdct->mdct_stereo_mode_cmdl == SMDCT_FORCE_LR )
    1154             :     {
    1155             :         hStereoMdct->fDualMono = 1;
    1156             :     }
    1157             :     else if ( hStereoMdct->mdct_stereo_mode_cmdl == SMDCT_FORCE_MS )
    1158             :     {
    1159             :         hStereoMdct->fMSstereo = 1;
    1160             :     }
    1161             : #endif
    1162             : 
    1163     1199360 :     hStereoMdct->split_ratio = SMDCT_EQUAL_RATIO_RANGE;
    1164     1199360 :     set_s( hStereoMdct->global_ild, SMDCT_ILD_RANGE >> 1, 2 );
    1165             : 
    1166     1199360 :     if ( mem_init )
    1167             :     {
    1168       55732 :         hStereoMdct->hItd = NULL;
    1169       55732 :         hStereoMdct->hDft_ana = NULL;
    1170             :     }
    1171             : 
    1172     1199360 :     if ( !( element_mode == IVAS_CPE_MDCT && element_brate <= MAX_MDCT_ITD_BRATE && ivas_format == STEREO_FORMAT ) )
    1173             :     {
    1174      406036 :         if ( hStereoMdct->hDft_ana != NULL )
    1175             :         {
    1176         278 :             free( hStereoMdct->hDft_ana );
    1177         278 :             hStereoMdct->hDft_ana = NULL;
    1178             :         }
    1179             : 
    1180      406036 :         if ( hStereoMdct->hItd != NULL )
    1181             :         {
    1182         278 :             free( hStereoMdct->hItd );
    1183         278 :             hStereoMdct->hItd = NULL;
    1184             :         }
    1185             :     }
    1186             : 
    1187     1199360 :     return;
    1188             : }
    1189             : 
    1190             : /*-----------------------------------------------------------------------*
    1191             :  * initMdctItdHandling()
    1192             :  *
    1193             :  * initialize encoder mdct ITD handling structures
    1194             :  *-----------------------------------------------------------------------*/
    1195             : 
    1196        1662 : ivas_error initMdctItdHandling(
    1197             :     STEREO_MDCT_ENC_DATA *hStereoMdct, /* i/o: mdct stereo parameters structure */
    1198             :     const int32_t input_Fs             /* i  : input sampling rate              */
    1199             : )
    1200             : {
    1201        1662 :     if ( hStereoMdct->hItd == NULL )
    1202             :     {
    1203         635 :         if ( ( hStereoMdct->hItd = (ITD_DATA_HANDLE) malloc( sizeof( ITD_DATA ) ) ) == NULL )
    1204             :         {
    1205           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for ITD data\n" ) );
    1206             :         }
    1207             :     }
    1208             : 
    1209        1662 :     if ( hStereoMdct->hDft_ana == NULL )
    1210             :     {
    1211         635 :         if ( ( hStereoMdct->hDft_ana = (DFT_ANA_HANDLE) malloc( sizeof( DFT_ANA ) ) ) == NULL )
    1212             :         {
    1213           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for ITD data\n" ) );
    1214             :         }
    1215             :     }
    1216             : 
    1217             :     /*Initialize ITD parameters*/
    1218        1662 :     stereo_enc_itd_init( hStereoMdct->hItd );
    1219             : 
    1220             :     /*Initialize DFT analysis parameters*/
    1221        1662 :     dft_ana_init( hStereoMdct->hDft_ana, input_Fs );
    1222             : 
    1223        1662 :     return IVAS_ERR_OK;
    1224             : }
    1225             : 
    1226             : /*-------------------------------------------------------------------------
    1227             :  * stereo_mdct_enc_destroy()
    1228             :  *
    1229             :  * destroy MDCT stereo handle
    1230             :  *-------------------------------------------------------------------------*/
    1231             : 
    1232       21586 : void stereo_mdct_enc_destroy(
    1233             :     STEREO_MDCT_ENC_DATA_HANDLE *hStereoMdct /* i/o: encoder MDCT stereo handle */
    1234             : )
    1235             : {
    1236       21586 :     if ( ( *hStereoMdct )->hDft_ana != NULL )
    1237             :     {
    1238         357 :         free( ( *hStereoMdct )->hDft_ana );
    1239         357 :         ( *hStereoMdct )->hDft_ana = NULL;
    1240             :     }
    1241             : 
    1242       21586 :     if ( ( *hStereoMdct )->hItd != NULL )
    1243             :     {
    1244         357 :         free( ( *hStereoMdct )->hItd );
    1245         357 :         ( *hStereoMdct )->hItd = NULL;
    1246             :     }
    1247             : 
    1248       21586 :     free( *hStereoMdct );
    1249       21586 :     *hStereoMdct = NULL;
    1250             : 
    1251       21586 :     return;
    1252             : }

Generated by: LCOV version 1.14