LCOV - code coverage report
Current view: top level - lib_enc - ivas_mdct_core_enc.c (source / functions) Hit Total Coverage
Test: Coverage on main @ 6baab0c613aa6c7100498ed7b93676aa8198a493 Lines: 531 531 100.0 %
Date: 2025-05-28 04:28:20 Functions: 10 10 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 "ivas_cnst.h"
      34             : #include <assert.h>
      35             : #include <stdint.h>
      36             : #include "options.h"
      37             : #include <math.h>
      38             : #include "cnst.h"
      39             : #include "prot.h"
      40             : #include "ivas_prot.h"
      41             : #include "rom_com.h"
      42             : #include "ivas_rom_com.h"
      43             : #ifdef DEBUGGING
      44             : #include "debug.h"
      45             : #endif
      46             : #include "wmc_auto.h"
      47             : 
      48             : /*--------------------------------------------------------------*
      49             :  * Local constants
      50             :  *---------------------------------------------------------------*/
      51             : 
      52             : #define MAX_STE_PRE_NRG      0.875f
      53             : #define MIN_STE_PRE_LEN      36
      54             : #define SILENT_CHANNEL_THRES 100
      55             : 
      56             : 
      57             : /*--------------------------------------------------------------*
      58             :  * enc_prm_pre_mdct()
      59             :  *
      60             :  * encode all side parameters up to MDCT whitening
      61             :  *---------------------------------------------------------------*/
      62             : 
      63     9990652 : static void enc_prm_pre_mdct(
      64             :     Encoder_State *st,           /* i/o: encoder state structure                            */
      65             :     int16_t param[],             /* i  : parameters                                         */
      66             :     const int16_t *no_param_tns, /* i  : number of TNS parameters per subframe              */
      67             :     int16_t p_param[2],          /* o  : pointer to parameters for next round of bs writing */
      68             :     const int16_t MCT_flag,      /* i  : hMCT handle allocated (1) or not (0)               */
      69             :     BSTR_ENC_HANDLE hBstr        /* i/o: encoder bitstream handle                           */
      70             : )
      71             : {
      72             :     int16_t nbits_start;
      73             : #ifdef DEBUG_PLOT_BITS
      74             :     int16_t tmp;
      75             : #endif
      76             : 
      77     9990652 :     nbits_start = hBstr->nb_bits_tot;
      78             : 
      79             :     /*--------------------------------------------------------------------------------*
      80             :      * Header
      81             :      *--------------------------------------------------------------------------------*/
      82             : 
      83     9990652 :     writeTCXMode( st, hBstr, MCT_flag, &nbits_start );
      84             : 
      85             :     /* write last_core for core switching and error concealment */
      86     9990652 :     push_next_indice( hBstr, st->last_core != ACELP_CORE, 1 );
      87             : 
      88     9990652 :     writeTCXWindowing( hBstr, st->hTcxCfg->tcx_curr_overlap_mode );
      89     9990652 :     if ( st->core == TCX_10_CORE )
      90             :     {
      91      232723 :         writeTCXWindowing( hBstr, st->hTcxCfg->tcx_last_overlap_mode );
      92             :     }
      93     9990652 :     push_next_indice( hBstr, st->hTcxEnc->kernel_type[0], st->last_core != ACELP_CORE ? 2 : 1 );
      94     9990652 :     if ( st->core == TCX_10_CORE )
      95             :     {
      96      232723 :         assert( ( st->hTcxEnc->kernel_type[0] & 1 ) == ( st->hTcxEnc->kernel_type[1] >> 1 ) );
      97      232723 :         push_next_indice( hBstr, st->hTcxEnc->kernel_type[1] & 1, 1 );
      98             :     }
      99             : 
     100     9990652 :     st->glr_reset = 0;
     101             : #ifdef DEBUG_PLOT_BITS
     102             :     tmp = hBstr->nb_bits_tot - nbits_start;
     103             :     dbgwrite( &tmp, sizeof( int16_t ), 1, 1, "./res/bits_TCX" );
     104             :     tmp = hBstr->nb_bits_tot;
     105             : #endif
     106             : 
     107             :     /*--------------------------------------------------------------------------------*
     108             :      * TCX20/TCX10 parameters
     109             :      *--------------------------------------------------------------------------------*/
     110             : 
     111     9990652 :     writeTCXparam( st, hBstr, NULL, param, 0, 0, 0, no_param_tns, p_param, NULL, 0 );
     112             : 
     113     9990652 :     st->side_bits_frame_channel = hBstr->nb_bits_tot - nbits_start;
     114             : 
     115             : #ifdef DEBUG_PLOT_BITS
     116             :     dbgwrite( &st->side_bits_frame_channel, sizeof( int16_t ), 1, 1, "./res/side_bits_pre" );
     117             : #endif
     118             : 
     119     9990652 :     return;
     120             : }
     121             : 
     122             : /*--------------------------------------------------------------*
     123             :  * kernel_switch_detect()
     124             :  *
     125             :  * complex-valued detection method for transform kernel switching
     126             :  *---------------------------------------------------------------*/
     127             : 
     128      438090 : static int16_t kernel_switch_detect(
     129             :     float *sigR0,               /* i/o: MDCT samples of the 1st (left) channel  */
     130             :     float *sigR1,               /* i/o: MDCT samples of the 2nd (right) channel */
     131             :     float *sigI0,               /* i/o: MDST samples of the 1st (left) channel  */
     132             :     float *sigI1,               /* i/o: MDST samples of the 2nd (right) channel */
     133             :     const int16_t nSamplesCore, /* i  : number of core-coded samples below IGF  */
     134             :     const int16_t nSamplesMax,  /* i  : transform length (number of MCLT lines) */
     135             :     const int16_t tcxTransType, /* i  : TCX transform type (2*5, 1*10, or 1*20) */
     136             :     float *switchCovPrev,       /* i/o: previous transform's correlation value  */
     137             :     const uint32_t bitRateMode  /* i  : stereo bitrate mode (bps * L_frame)     */
     138             : )
     139             : {
     140             :     int16_t maxLength, anaLength, s;
     141             :     float cov00, cov90, sumR0, sumR1, sumI0, sumI1;
     142             : 
     143      438090 :     maxLength = ( nSamplesCore > 0 && nSamplesCore < nSamplesMax ) ? nSamplesCore : nSamplesMax;
     144      438090 :     anaLength = min( maxLength, (int16_t) ( bitRateMode >> 17 ) & 0xFFFE );
     145      438090 :     s = ( nSamplesMax < 512 ? 2 : 4 ); /* exclude DC offset */
     146             : 
     147      438090 :     cov00 = 0.f;
     148      438090 :     cov90 = 0.f;
     149      438090 :     sumR0 = 0.f;
     150      438090 :     sumR1 = 0.f;
     151      438090 :     sumI0 = 0.f;
     152      438090 :     sumI1 = 0.f;
     153             : 
     154      438090 :     if ( tcxTransType == TCX_5 )
     155             :     {
     156       13086 :         assert( nSamplesMax < 512 );
     157       13086 :         anaLength >>= 1;
     158      687251 :         for ( s = 1; s < anaLength; s++ )
     159             :         {
     160      674165 :             cov00 += sigR0[s] * sigR1[s] + sigI0[s] * sigI1[s];
     161      674165 :             cov90 += sigR0[s] * sigI1[s] - sigI0[s] * sigR1[s];
     162      674165 :             sumR0 += sigR0[s] * sigR0[s];
     163      674165 :             sumR1 += sigR1[s] * sigR1[s];
     164      674165 :             sumI0 += sigI0[s] * sigI0[s];
     165      674165 :             sumI1 += sigI1[s] * sigI1[s];
     166             :         }
     167       13086 :         anaLength += ( nSamplesMax >> 1 );
     168       13086 :         s = 1 + ( nSamplesMax >> 1 ); /* 2nd TCX-5 spectrum is stacked onto 1st */
     169             :     }
     170    89905041 :     for ( ; s < anaLength; s++ )
     171             :     {
     172    89466951 :         cov00 += sigR0[s] * sigR1[s] + sigI0[s] * sigI1[s];
     173    89466951 :         cov90 += sigR0[s] * sigI1[s] - sigI0[s] * sigR1[s];
     174    89466951 :         sumR0 += sigR0[s] * sigR0[s];
     175    89466951 :         sumR1 += sigR1[s] * sigR1[s];
     176    89466951 :         sumI0 += sigI0[s] * sigI0[s];
     177    89466951 :         sumI1 += sigI1[s] * sigI1[s];
     178             :     }
     179             : 
     180      438090 :     cov00 /= ( sqrtf( sumR0 * sumR1 ) + sqrtf( sumI0 * sumI1 ) + 1.f );
     181      438090 :     cov90 /= ( sqrtf( sumR0 * sumI1 ) + sqrtf( sumI0 * sumR1 ) + 1.f );
     182      438090 :     sumI0 = max( 0.f, fabsf( cov90 ) - fabsf( cov00 ) );
     183      438090 :     sumI1 = ( switchCovPrev != NULL ? fabsf( *switchCovPrev ) : 0.f );
     184      438090 :     s = ( cov90 < 0.f ? -1 : 1 );
     185             : 
     186      438090 :     if ( switchCovPrev != NULL ) /* update the decision smoothing history */
     187             :     {
     188      438090 :         *switchCovPrev = ( 0.875f * sumI1 + 0.125f * sumI0 ) * s;
     189             :     }
     190             : 
     191      438090 :     return ( ( sumI0 > 0.5f - 0.25f * sumI1 || fabsf( cov90 ) > 0.75f - 0.5f * sumI1 ) && ( sumI1 > 0.0625f ) ? s : 0 ); /* final decision */
     192             : }
     193             : 
     194             : 
     195         202 : static void kernel_switch_trafo(
     196             :     const float *x,
     197             :     float *y,
     198             :     const int16_t l,
     199             :     const int16_t m,
     200             :     const int16_t r,
     201             :     const uint16_t kernelType )
     202             : {
     203         202 :     const float signLeft = ( kernelType >= MDCT_II ? -1.f : 1.f );
     204         202 :     const float signRight = ( kernelType & 1 ? 1.f : -1.f );
     205             :     int16_t i;
     206             :     float inputBuffer[N_MAX];
     207             : 
     208             :     /* Init */
     209       14362 :     for ( i = 0; i < m / 2; i++ )
     210             :     {
     211       14160 :         inputBuffer[m / 2 + r / 2 + i] = -1.0f * x[l + m / 2 - 1 - i];
     212             :     }
     213             : 
     214        8392 :     for ( i = 0; i < l / 2; i++ )
     215             :     {
     216        8190 :         inputBuffer[m / 2 + r / 2 + m / 2 + i] = signLeft * x[i] - x[l - 1 - i];
     217             :     }
     218             : 
     219       14362 :     for ( i = 0; i < m / 2; i++ )
     220             :     {
     221       14160 :         inputBuffer[m / 2 + r / 2 - 1 - i] = -1.0f * x[l + m / 2 + i];
     222             :     }
     223             : 
     224       11532 :     for ( i = 0; i < r / 2; i++ )
     225             :     {
     226       11330 :         inputBuffer[m / 2 + r / 2 - 1 - m / 2 - i] = -1.0f * x[l + m + i] + signRight * x[l + m + r - 1 - i];
     227             :     }
     228             : 
     229         202 :     edxt( inputBuffer, y, l / 2 + m + r / 2, kernelType, FALSE );
     230             : 
     231         202 :     v_multc( y, sqrtf( (float) NORM_MDCT_FACTOR / ( l / 2 + m + r / 2 ) ), y, l / 2 + m + r / 2 );
     232             : 
     233         202 :     return;
     234             : }
     235             : 
     236             : 
     237    10378274 : static void kernel_switch_update_transforms(
     238             :     float *sigR,                /* i/o: MDCT samples of the given channel */
     239             :     float *sigI,                /* i/o: MDST samples of the given channel */
     240             :     const int16_t tcxTransType, /* i  : TCX transform type, cf also above */
     241             :     TCX_CONFIG_HANDLE hTcxCfg,  /* i  : TCX configuration handle, pointer */
     242             :     const int16_t bwidthSwCnt,  /* i  : bandwidth switching counter in st */
     243             :     const uint16_t kernelType,  /* i  : TCX transform kernel type (0 - 3) */
     244             :     const float *tcxTimeSignal, /* i  : hTcxEnc->new_speech_TCX buf in st */
     245             :     const float *speech_TCX,    /* i  : hTcxEnc->speech_TCX buffer in st  */
     246             :     float *windowedTimeSignal,  /* i/o: windowed input and scratch buffer */
     247             :     const int16_t L_subframe    /* i  : transform length (number of bins) */
     248             : )
     249             : {
     250             :     int16_t s, nSubframes;
     251             : 
     252    10378274 :     s = L_subframe;
     253    10378274 :     nSubframes = ( tcxTransType == TCX_20 ? 1 : NB_DIV );
     254             : 
     255    10378274 :     if ( kernelType == MDCT_IV ) /* no updates required here! */
     256             :     {
     257    10372482 :         return;
     258             :     }
     259             : 
     260        5792 :     if ( kernelType == MDST_IV ) /* swap real and imag. parts */
     261             :     {
     262     1103672 :         for ( s = L_subframe - 1; s >= 0; s-- )
     263             :         {
     264     1101920 :             const float sigTemp = -sigR[s];
     265             : 
     266     1101920 :             sigR[s] = sigI[s];
     267     1101920 :             sigI[s] = sigTemp;
     268             :         }
     269             : 
     270        1752 :         return;
     271             :     }
     272             : 
     273             :     /* MDCT/MDST-II as real part, keep imag. part for speedup */
     274        4040 :     if ( tcxTransType == TCX_20 && hTcxCfg->tcx_last_overlap_mode != TRANSITION_OVERLAP )
     275             :     {
     276        3838 :         wtda_ext( tcxTimeSignal, windowedTimeSignal, (int16_t) windowedTimeSignal[0], (int16_t) windowedTimeSignal[1], s, kernelType );
     277             : 
     278        3838 :         edxt( windowedTimeSignal, sigR, s, kernelType, FALSE );
     279             : 
     280        3838 :         v_multc( sigR, sqrtf( (float) NORM_MDCT_FACTOR / s ), sigR, s );
     281             :     }
     282             :     else /* 2 TCX5 subframes or 1 TCX10 or 1 transitory TCX20 */
     283             :     {
     284         202 :         const float sign = ( kernelType >= MDCT_II ? 1.f : -1.f );
     285         202 :         const int16_t minWindowLen = hTcxCfg->tcx_mdct_window_min_lengthFB - 1;
     286         202 :         int16_t i, leftOverlap = 0, rightOverlap = 0;
     287             :         const float *left_win, *right_win;
     288             : 
     289         202 :         tcx_get_windows( hTcxCfg, (int16_t) windowedTimeSignal[0], (int16_t) windowedTimeSignal[1], &leftOverlap, &left_win, &rightOverlap, &right_win, 1 );
     290         202 :         if ( speech_TCX != NULL && tcxTransType != TCX_20 && (int16_t) windowedTimeSignal[0] == FULL_OVERLAP && s - leftOverlap > minWindowLen )
     291             :         {
     292        3019 :             for ( i = minWindowLen; i >= 0; i-- ) /* outer left folding of shortened long ALDO slope */
     293             :             {
     294        2940 :                 windowedTimeSignal[2 + leftOverlap + i] += sign * speech_TCX[-1 - i] * hTcxCfg->tcx_aldo_window_1_FB[leftOverlap / 2 + minWindowLen - i] * hTcxCfg->tcx_mdct_window_minimumFB[minWindowLen - i];
     295             :             }
     296             :         }
     297             : 
     298         202 :         if ( tcxTransType == TCX_5 )
     299             :         {
     300             :             float tcx5Win[N_TCX10_MAX / 2 + L_MDCT_OVLP_MAX]; /* temporary buffer for TCX5 windowing */
     301             : 
     302          94 :             assert( L_subframe == nSubframes * hTcxCfg->tcx5SizeFB );
     303             : 
     304             :             /* Outer left folding */
     305        6134 :             for ( i = 0; i < leftOverlap / 2; i++ )
     306             :             {
     307        6040 :                 windowedTimeSignal[2 + leftOverlap / 2 + i] += sign * windowedTimeSignal[2 + leftOverlap / 2 - 1 - i];
     308             :             }
     309             : 
     310          94 :             s = hTcxCfg->tcx5SizeFB; /* obtain 1st TCX5 again */
     311          94 :             nSubframes *= 2;
     312          94 :             WindowSignal( hTcxCfg, leftOverlap / 2, RECTANGULAR_OVERLAP, MIN_OVERLAP, &leftOverlap, &rightOverlap, windowedTimeSignal + 2, &s, tcx5Win, 0, 1 );
     313          94 :             kernel_switch_trafo( tcx5Win, sigR, leftOverlap, s /* L_subfr. */ - ( leftOverlap + rightOverlap ) / 2, rightOverlap, kernelType );
     314             : 
     315          94 :             if ( kernelType & 1 ) /* 2nd TCX5 is kernelType 3 */
     316             :             {
     317        7319 :                 for ( i = L_subframe - 1; i >= s; i-- )
     318             :                 {
     319        7280 :                     const float sigTemp = -sigR[i];
     320             : 
     321        7280 :                     sigR[i] = sigI[i];
     322        7280 :                     sigI[i] = sigTemp;
     323             :                 }
     324             :             }
     325             :         }
     326             :         else /* tcxTransType != TCX_5 */
     327             :         {
     328         108 :             kernel_switch_trafo( windowedTimeSignal + 2, sigR, leftOverlap, s - ( leftOverlap + rightOverlap ) / 2, rightOverlap, kernelType );
     329             :         }
     330             :     }
     331             : 
     332             :     /* high-band gain control, in case of bandwidth switching */
     333        4040 :     if ( bwidthSwCnt > 0 )
     334             :     {
     335          55 :         v_multc( sigR + L_FRAME16k / nSubframes, (float) bwidthSwCnt / (float) BWS_TRAN_PERIOD, sigR + L_FRAME16k / nSubframes, s - L_FRAME16k / nSubframes );
     336             :     }
     337             : 
     338        4040 :     return;
     339             : }
     340             : 
     341             : 
     342    35792454 : static void applyStereoPreProcessingCplx(
     343             :     float *mdctSample1,
     344             :     float *mdctSample2,
     345             :     float *mdstSample1,
     346             :     float *mdstSample2,
     347             :     const float factIn,
     348             :     const float factDe,
     349             :     const float sign )
     350             : {
     351    35792454 :     const float valI1 = *mdstSample1;
     352    35792454 :     const float valI2 = *mdstSample2;
     353    35792454 :     const float valR1 = *mdctSample1;
     354    35792454 :     const float valR2 = *mdctSample2;
     355             :     float absR1, absR2;
     356             :     float dmxI1, dmxR1, dmxI2, dmxR2;
     357             :     float n, d;
     358             : 
     359    35792454 :     absR1 = fabsf( valR1 );
     360    35792454 :     absR2 = fabsf( valR2 );
     361    35792454 :     dmxR1 = valR1 * factDe + sign * valR2 * factIn; /* cross */
     362    35792454 :     dmxR2 = valR2 * factDe + sign * valR1 * factIn; /* -talk */
     363             : 
     364    35792454 :     if ( fabsf( dmxR1 ) < absR1 + absR2 ) /* avoid destructive summations */
     365             :     {
     366       85602 :         if ( absR1 * factDe < absR2 * factIn )
     367             :         {
     368       43321 :             dmxR1 = valR2 * factIn - sign * valR1 * factDe;
     369       43321 :             dmxI1 = valI2 * factIn - sign * valI1 * factDe;
     370             :         }
     371             :         else
     372             :         {
     373       42281 :             dmxR1 = valR1 * factDe - sign * valR2 * factIn;
     374       42281 :             dmxI1 = valI1 * factDe - sign * valI2 * factIn;
     375             :         }
     376             :     }
     377             :     else
     378             :     {
     379    35706852 :         dmxI1 = valI1 * factDe + sign * valI2 * factIn;
     380             :     }
     381             : 
     382    35792454 :     if ( fabsf( dmxR2 ) < absR1 + absR2 ) /* avoid destructive summations */
     383             :     {
     384       84391 :         if ( absR1 * factIn < absR2 * factDe )
     385             :         {
     386       42500 :             dmxR2 = valR2 * factDe - sign * valR1 * factIn;
     387       42500 :             dmxI2 = valI2 * factDe - sign * valI1 * factIn;
     388             :         }
     389             :         else
     390             :         {
     391       41891 :             dmxR2 = valR1 * factIn - sign * valR2 * factDe;
     392       41891 :             dmxI2 = valI1 * factIn - sign * valI2 * factDe;
     393             :         }
     394             :     }
     395             :     else
     396             :     {
     397    35708063 :         dmxI2 = valI2 * factDe + sign * valI1 * factIn;
     398             :     }
     399             : 
     400    35792454 :     n = valR1 * valR1 + valI1 * valI1; /* maintain spectral magnitude (1) */
     401    35792454 :     d = dmxR1 * dmxR1 + dmxI1 * dmxI1;
     402    35792454 :     d = sqrtf( n / max( 1.f, d ) );
     403    35792454 :     *mdctSample1 = dmxR1 * d;
     404    35792454 :     *mdstSample1 = dmxI1 * d;
     405             : 
     406    35792454 :     n = valR2 * valR2 + valI2 * valI2; /* maintain spectral magnitude (2) */
     407    35792454 :     d = dmxR2 * dmxR2 + dmxI2 * dmxI2;
     408    35792454 :     d = sqrtf( n / max( 1.f, d ) );
     409    35792454 :     *mdctSample2 = dmxR2 * d;
     410    35792454 :     *mdstSample2 = dmxI2 * d;
     411             : 
     412    35792454 :     return;
     413             : }
     414             : 
     415             : 
     416             : /*--------------------------------------------------------------*
     417             :  * enc_ste_pre_mdct()
     418             :  *
     419             :  * encoder-side complex-valued stereo pre-processing (crosstalk)
     420             :  *---------------------------------------------------------------*/
     421             : 
     422      447560 : static uint16_t enc_ste_pre_mdct(
     423             :     float *sigR0,               /* i/o: MDCT samples of the 1st (left) channel  */
     424             :     float *sigR1,               /* i/o: MDCT samples of the 2nd (right) channel */
     425             :     float *sigI0,               /* i/o: MDST samples of the 1st (left) channel  */
     426             :     float *sigI1,               /* i/o: MDST samples of the 2nd (right) channel */
     427             :     const int16_t nSamplesCore, /* i  : number of core-coded samples below IGF  */
     428             :     const int16_t nSamplesMax,  /* i  : transform length (number of MCLT lines) */
     429             :     const int16_t limitWeight,  /* i  : 1 means the pre-processing is softened  */
     430             :     const uint16_t corrIdxPrev, /* i  : previous transform's correlation index  */
     431             :     const uint32_t bitRateMode  /* i  : stereo bitrate mode (bps * L_frame)     */
     432             : )
     433             : {
     434             :     int16_t fadeInLen, fadeInOff, nSampProc, preproLen;
     435             :     float maxSqrValue, stepWeightI, stepWeightD;
     436             :     float corr, sumL, sumR, chanCorrSign, xTalkI, xTalkD;
     437             :     float sumMagnL, sumMagnR, sumPrdLR, sumPrdLL, sumPrdRR;
     438             :     int16_t s;
     439             : 
     440      447560 :     fadeInLen = ( nSamplesMax < 512 ? ( nSamplesMax < 256 ? 10 : 20 ) : 40 );            /* 1000Hz transition */
     441      447560 :     fadeInOff = ( ( bitRateMode * 3 ) >> 19 ) & ( nSamplesMax < 512 ? 0xFFFF : 0xFFFE ); /* offset */
     442      447560 :     nSampProc = ( nSamplesCore > 0 && nSamplesCore < nSamplesMax ) ? nSamplesCore : nSamplesMax;
     443      447560 :     preproLen = nSampProc - fadeInOff;
     444      447560 :     maxSqrValue = 16777216.f * preproLen * preproLen;
     445      447560 :     stepWeightI = ( limitWeight > 0 ? 1.f : 2.f ); /* crosstalk weight */
     446      447560 :     stepWeightD = 4.f - stepWeightI;               /* decrement, i.e., 1 - crosstalk */
     447      447560 :     corr = 0.f, sumL = 0.f, sumR = 0.f;
     448      447560 :     sumMagnL = 0.f, sumMagnR = 0.f, sumPrdLR = 0.f, sumPrdLL = 0.f, sumPrdRR = 0.f;
     449             : 
     450      447560 :     if ( fadeInOff + fadeInLen + MIN_STE_PRE_LEN >= nSampProc )
     451             :     {
     452       14763 :         return 0;
     453             :     }
     454             : 
     455    88003229 :     for ( s = fadeInOff; s < nSampProc; s++ )
     456             :     {
     457             :         float absMagnL, absMagnR;
     458    87570432 :         absMagnL = sqrtf( sigR0[s] * sigR0[s] + sigI0[s] * sigI0[s] );
     459    87570432 :         absMagnR = sqrtf( sigR1[s] * sigR1[s] + sigI1[s] * sigI1[s] );
     460             : 
     461    87570432 :         corr += sigR0[s] * sigR1[s] + sigI0[s] * sigI1[s];
     462    87570432 :         sumL += sigR0[s] + sigI0[s];
     463    87570432 :         sumR += sigR1[s] + sigI1[s];
     464             : 
     465    87570432 :         sumMagnL += absMagnL;
     466    87570432 :         sumMagnR += absMagnR;
     467    87570432 :         sumPrdLR += absMagnL * absMagnR;
     468    87570432 :         sumPrdLL += absMagnL * absMagnL;
     469    87570432 :         sumPrdRR += absMagnR * absMagnR;
     470             :     }
     471      432797 :     corr *= 2.f * preproLen;
     472      432797 :     corr -= sumL * sumR;
     473      432797 :     chanCorrSign = ( corr < -maxSqrValue ) ? -1.f : 1.f;
     474             : 
     475      432797 :     sumL = sumMagnL / (float) preproLen; /* inter-channel correlation of magnitude */
     476      432797 :     sumR = sumMagnR / (float) preproLen;
     477      432797 :     corr = sumPrdLR + sumL * sumR * preproLen - sumMagnL * sumR - sumMagnR * sumL;
     478      432797 :     sumL = sumPrdLL + sumL * sumL * preproLen - sumMagnL * sumL - sumMagnL * sumL;
     479      432797 :     sumR = sumPrdRR + sumR * sumR * preproLen - sumMagnR * sumR - sumMagnR * sumR;
     480      432797 :     corr = ( ( corr <= 0.f ) || ( sumL * sumR <= 0.f ) ? 0.f : ( corr * corr ) / ( sumL * sumR ) );
     481             : 
     482      432797 :     if ( ( corr > 0.75f && corrIdxPrev == 0 ) || /* processing weight fade-in/-out */
     483      298695 :          ( corr <= 0.75f && corrIdxPrev > 0 ) )
     484             :     {
     485       55051 :         stepWeightI = 1.f;
     486       55051 :         stepWeightD = 4.f - stepWeightI;
     487             :     }
     488             : 
     489      432797 :     if ( corr <= 0.75f && corrIdxPrev == 0 ) /* bypass all processing, just return */
     490             :     {
     491      276359 :         return 0;
     492             :     }
     493             : 
     494      156438 :     sigR0 += fadeInOff + 1; /* stereo pre-processing starts with an offset of one! */
     495      156438 :     sigR1 += fadeInOff + 1;
     496      156438 :     sigI0 += fadeInOff + 1;
     497      156438 :     sigI1 += fadeInOff + 1;
     498      156438 :     xTalkI = stepWeightI;
     499      156438 :     xTalkD = stepWeightD * ( 2 * fadeInLen - 1 );
     500             : 
     501     5496170 :     for ( s = fadeInLen - 1; s > 0; s--, sigR0++, sigR1++, sigI0++, sigI1++ )
     502             :     {
     503     5339732 :         applyStereoPreProcessingCplx( sigR0, sigR1, sigI0, sigI1, xTalkI, xTalkD, chanCorrSign );
     504     5339732 :         xTalkI += stepWeightI;
     505     5339732 :         xTalkD -= stepWeightD;
     506             :     }
     507             : 
     508    25560499 :     for ( s = preproLen - fadeInLen; s > 0; s--, sigR0++, sigR1++, sigI0++, sigI1++ )
     509             :     {
     510    25404061 :         applyStereoPreProcessingCplx( sigR0, sigR1, sigI0, sigI1, xTalkI, xTalkD, chanCorrSign );
     511             :     }
     512             : 
     513      156438 :     if ( nSampProc < nSamplesMax ) /* fade-out of processing at start of IGF range */
     514             :     {
     515     5189630 :         for ( s = min( fadeInLen, nSamplesMax - nSampProc ) - 1; s > 0; s--, sigR0++, sigR1++, sigI0++, sigI1++ )
     516             :         {
     517     5048661 :             xTalkI -= stepWeightI;
     518     5048661 :             xTalkD += stepWeightD;
     519     5048661 :             applyStereoPreProcessingCplx( sigR0, sigR1, sigI0, sigI1, xTalkI, xTalkD, chanCorrSign );
     520             :         }
     521             :     }
     522             : 
     523      156438 :     return ( corr > 0.75f ? 1 : 0 );
     524             : }
     525             : 
     526             : 
     527             : /*--------------------------------------------------------------*
     528             :  * enc_prm_igf_mdct()
     529             :  *
     530             :  * write igf parameters to bitstream
     531             :  *---------------------------------------------------------------*/
     532             : 
     533     6123578 : void enc_prm_igf_mdct(
     534             :     Encoder_State *st,    /* i  : Encoder state handle    */
     535             :     BSTR_ENC_HANDLE hBstr /* i/o: Bitstream handle        */
     536             : )
     537             : {
     538             :     int16_t nbits_start, total_nbbits;
     539             : 
     540     6123578 :     nbits_start = hBstr->nb_bits_tot;
     541             : 
     542     6123578 :     if ( st->core == TCX_20_CORE )
     543             :     {
     544     5966755 :         st->hIGFEnc->infoTotalBitsPerFrameWritten = 0;
     545             : 
     546     5966755 :         IGFEncWriteBitstream( st->hIGFEnc, hBstr, &st->hIGFEnc->infoTotalBitsPerFrameWritten, ( st->last_core == ACELP_CORE ) ? IGF_GRID_LB_TRAN : IGF_GRID_LB_NORM, 1 );
     547             :     }
     548             :     else
     549             :     {
     550      156823 :         IGFEncWriteConcatenatedBitstream( st->hIGFEnc, hBstr );
     551             :     }
     552             : 
     553     6123578 :     total_nbbits = hBstr->nb_bits_tot - nbits_start;
     554     6123578 :     st->side_bits_frame_channel += total_nbbits;
     555             : 
     556     6123578 :     return;
     557             : }
     558             : 
     559             : 
     560             : /*-------------------------------------------------------------------*
     561             :  * ivas_mdct_core_whitening_enc()
     562             :  *
     563             :  * MCT preprocessing up to whitening the core spectrum
     564             :  *-------------------------------------------------------------------*/
     565             : 
     566     5511273 : void ivas_mdct_core_whitening_enc(
     567             :     CPE_ENC_HANDLE hCPE,                        /* i/o: CPE encoder structure                 */
     568             :     float new_samples[CPE_CHANNELS][L_INP],     /* i  : new samples                           */
     569             :     float old_wsp[CPE_CHANNELS][L_WSP],         /* i  : 12.8kHz weighted speech (for LTP      */
     570             :     float pitch_buf[CPE_CHANNELS][NB_SUBFR16k], /* o  : floating pitch for each subframe      */
     571             :     float *mdst_spectrum_long[CPE_CHANNELS],    /* o  : buffer for MDST spectrum              */
     572             :     int16_t tnsBits[CPE_CHANNELS][NB_DIV],      /* o  : buffer TNS bits                       */
     573             :     float *orig_spectrum_long[CPE_CHANNELS],    /* o  : origingal spectrum w/o whitening      */
     574             :     int16_t tnsSize[CPE_CHANNELS][NB_DIV],      /* o  : number of tns parameters put into prm */
     575             :     int16_t p_param[CPE_CHANNELS][NB_DIV],      /* o  : pointer to the parameter table        */
     576             :     BSTR_ENC_HANDLE hBstr,                      /* i/o: encoder bitstream handle              */
     577             :     const int16_t mct_on,                       /* i  : flag mct block (1) or stereo (0)      */
     578             :     const int16_t nChannels                     /* i  : total number of coded channels        */
     579             : )
     580             : {
     581             :     int16_t n, ch, nSubframes, L_subframe, L_subframeTCX, tcx_subframe_coded_lines;
     582             :     float A_q[CPE_CHANNELS][NB_DIV][M + 1];
     583             :     int16_t sns_vq_indices[CPE_CHANNELS * NB_DIV * SNS_MSVQ_NSTAGES_TCX10];
     584             :     int16_t sns_stereo_mode[NB_DIV];
     585             :     int16_t idx;
     586             :     int16_t param_lpc[CPE_CHANNELS][NPRM_LPC_NEW];
     587             :     int16_t param_core[CPE_CHANNELS][2 * NPRM_DIV];
     588             :     int16_t ltpBits[CPE_CHANNELS];
     589             :     int16_t i, T_op[CPE_CHANNELS][3];
     590             :     float *orig_spectrum[CPE_CHANNELS][NB_DIV]; /* Pointers to MDCT output for a short block (L/R) */
     591             :     float temp_buffer[15 * L_FRAME48k / 8];
     592             :     float *windowedSignal[CPE_CHANNELS];
     593     5511273 :     float *powerSpec = orig_spectrum_long[0];
     594             :     float *mdst_spectrum[CPE_CHANNELS][NB_DIV];
     595             :     float nrg;
     596             :     Encoder_State *st, **sts;
     597             :     float scf[CPE_CHANNELS][NB_DIV][M];
     598             :     float scf_q[CPE_CHANNELS][NB_DIV][M];
     599             :     float chE[2], chE_tot;
     600             :     int8_t sns_low_br_mode;
     601             :     int16_t nbits_start_sns;
     602             :     int16_t num_sns;
     603             :     int8_t skipped_first_channel;
     604             :     int16_t zero_side_flag[NB_DIV];
     605             : 
     606     5511273 :     push_wmops( "mdct_core_whitening" );
     607             : 
     608             :     /*--------------------------------------------------------------*
     609             :      * Initialization
     610             :      *---------------------------------------------------------------*/
     611             : 
     612     5511273 :     sts = hCPE->hCoreCoder;
     613             : 
     614    16533819 :     for ( ch = 0; ch < CPE_CHANNELS; ch++ )
     615             :     {
     616    11022546 :         stereo_tcx_init_enc( sts[ch] );
     617             : 
     618    11022546 :         set_s( tnsSize[ch], 0, 2 );
     619    11022546 :         set_s( tnsBits[ch], 0, 2 );
     620    11022546 :         ltpBits[ch] = 0;
     621             : 
     622    44090184 :         for ( i = 0; i < 3; i++ )
     623             :         {
     624    33067638 :             T_op[ch][i] = sts[ch]->pitch[i];
     625             : 
     626             :             /* check minimum pitch for quantization */
     627    33067638 :             if ( T_op[ch][i] < PIT_MIN_SHORTER )
     628             :             {
     629     6086112 :                 T_op[ch][i] *= 2;
     630             :             }
     631             :         }
     632             :     }
     633             : 
     634    16533819 :     for ( ch = 0; ch < CPE_CHANNELS; ch++ )
     635             :     {
     636    11022546 :         orig_spectrum[ch][0] = orig_spectrum_long[ch];
     637    11022546 :         orig_spectrum[ch][1] = orig_spectrum_long[ch] + N_TCX10_MAX;
     638    11022546 :         mdst_spectrum[ch][0] = mdst_spectrum_long[ch];
     639    11022546 :         mdst_spectrum[ch][1] = mdst_spectrum_long[ch] + N_TCX10_MAX;
     640             :     }
     641             : 
     642     5511273 :     windowedSignal[0] = orig_spectrum_long[0]; /* NOTE temporarily available */
     643     5511273 :     windowedSignal[1] = temp_buffer;           /* orig_spectrum_long isn't long enough */
     644             : 
     645             :     /*--------------------------------------------------------------*
     646             :      * TCX20/TCX10 switching decision
     647             :      *---------------------------------------------------------------*/
     648             : 
     649     5511273 :     if ( mct_on )
     650             :     {
     651     4087656 :         sts[0]->hTcxEnc->tfm_mem = sts[1]->hTcxEnc->tfm_mem = sqrtf( 0.5f * ( sts[0]->hTcxEnc->tfm_mem * sts[0]->hTcxEnc->tfm_mem + sts[1]->hTcxEnc->tfm_mem * sts[1]->hTcxEnc->tfm_mem ) ); /* RMS */
     652     4087656 :         sts[0]->hTcxEnc->tcxltp_norm_corr_past = sts[1]->hTcxEnc->tcxltp_norm_corr_past = 0.5f * ( sts[0]->hTcxEnc->tcxltp_norm_corr_past + sts[1]->hTcxEnc->tcxltp_norm_corr_past );
     653    12262968 :         for ( ch = 0; ch < CPE_CHANNELS; ch++ )
     654             :         {
     655     8175312 :             st = sts[ch];
     656     8175312 :             SetTCXModeInfo( st, st->hTranDet, &st->hTcxCfg->tcx_curr_overlap_mode );
     657             :         }
     658             :     }
     659             : 
     660     5511273 :     sts[0]->core = sts[0]->hTcxEnc->tcxMode;
     661     5511273 :     sts[1]->core = sts[1]->hTcxEnc->tcxMode;
     662             : 
     663             :     /*--------------------------------------------------------------*
     664             :      * Core Signal Analysis: MDCT, TNS
     665             :      *---------------------------------------------------------------*/
     666             : 
     667    16533819 :     for ( ch = 0; ch < CPE_CHANNELS; ch++ )
     668             :     {
     669    11022546 :         st = sts[ch];
     670    11022546 :         if ( ( hCPE->cpe_id * CPE_CHANNELS + ch ) >= nChannels )
     671             :         {
     672      876995 :             continue;
     673             :         }
     674    10145551 :         SetCurrentPsychParams( st->core, 0, st->hTcxCfg );
     675             : 
     676             :         /* tcx ltp analysis on the 12.8kHz weighted speech, saves preproc resampling to sr_core */
     677    10145551 :         tcx_ltp_encode( st, st->hTcxEnc->tcxMode, L_FRAME, old_wsp[ch] + L_WSP_MEM + L_LOOK_12k8, NULL, old_wsp[ch] + L_WSP_MEM + L_LOOK_12k8, T_op[ch], &param_core[ch][1 + NOISE_FILL_RANGES], &ltpBits[ch], NULL, 0, IVAS_CPE_MDCT );
     678             : 
     679    10145551 :         core_signal_analysis_high_bitrate( new_samples[ch] + L_INP_MEM, T_op[ch], NULL, NULL, st, mdst_spectrum[ch], tnsSize[ch], tnsBits[ch], param_core[ch], &ltpBits[ch], windowedSignal[ch], st->L_frame, st->hTcxEnc->L_frameTCX, hCPE->last_element_mode, 0 );
     680             : 
     681             :         /* BWD in MDCT domain */
     682    10145551 :         if ( st->hTcxCfg->tcx_last_overlap_mode != TRANSITION_OVERLAP )
     683             :         {
     684    10124245 :             nSubframes = ( st->hTcxEnc->tcxMode == TCX_20 ) ? 1 : NB_DIV;
     685             : 
     686    20481213 :             for ( n = 0; n < nSubframes; n++ )
     687             :             {
     688    10356968 :                 bw_detect( st, NULL, st->hTcxEnc->spectrum[n], NULL, MC_FORMAT /*just cannot be ISM_FORMAT*/, mct_on );
     689             : 
     690    10356968 :                 if ( nSubframes == NB_DIV && n == 0 )
     691             :                 {
     692      232723 :                     st->last_input_bwidth = st->input_bwidth;
     693             :                 }
     694             :             }
     695             :         }
     696             : 
     697    10145551 :         if ( st->last_core == ACELP_CORE ) /* reset past kernel info */
     698             :         {
     699       21306 :             st->hTcxEnc->kernel_switch_corr_past = 0.f;
     700       21306 :             st->hTcxEnc->kernel_symmetry_past = 0;
     701             :         }
     702             :     }
     703             : 
     704             :     /*--------------------------------------------------------------*
     705             :      * Transform Kernel Switching, Stereo Pre-Processing, and TNS
     706             :      *---------------------------------------------------------------*/
     707             : 
     708     5511273 :     sts[0]->hTcxEnc->fUseTns[1] = 0;
     709             : 
     710     5511273 :     if ( sts[0]->element_brate < IVAS_80k && sts[0]->core == sts[1]->core && sts[0]->element_mode == IVAS_CPE_MDCT && !mct_on && !hCPE->hStereoMdct->isSBAStereoMode )
     711      425769 :     {
     712             :         int16_t nSampCore;
     713             :         int32_t totalRate;
     714             :         TCX_ENC_HANDLE hTcxEnc0, hTcxEnc1;
     715             : 
     716      425769 :         nSampCore = ( sts[0]->igf ) ? max( sts[0]->hIGFEnc->infoStartLine, sts[1]->hIGFEnc->infoStartLine ) : 0;
     717      425769 :         totalRate = sts[0]->element_brate;
     718      425769 :         hTcxEnc0 = sts[0]->hTcxEnc;
     719      425769 :         hTcxEnc1 = sts[1]->hTcxEnc;
     720             : 
     721      425769 :         init_tcx_enc_info( sts[0], &L_subframe, &L_subframeTCX, &tcx_subframe_coded_lines );
     722             : 
     723      425769 :         if ( nSampCore == 0 )
     724             :         {
     725       74555 :             nSampCore = tcx_subframe_coded_lines;
     726             :         }
     727             : 
     728      425769 :         nrg = 0.25f * ( hTcxEnc0->tcxltp_norm_corr_past + hTcxEnc0->tcxltp_norm_corr_mem +  /* tcxltp_norm_corr_past already contains the */
     729      425769 :                         hTcxEnc1->tcxltp_norm_corr_past + hTcxEnc1->tcxltp_norm_corr_mem ); /* normalized correlation of the current frame */
     730             : 
     731      425769 :         L_subframe = max( 512, L_subframe );
     732      425769 :         nSubframes = ( hTcxEnc0->tcxMode == TCX_20 ) ? 1 : NB_DIV;
     733             : 
     734      863995 :         for ( n = 0; n < nSubframes; n++ )
     735             :         {
     736      438226 :             if ( hTcxEnc0->transform_type[n] == hTcxEnc1->transform_type[n] )
     737             :             {
     738             :                 const int16_t switchKernel = /* these 4 transform types can be applied: 0 = MDCT-IV, 1 = MDST-II, 2 = MDCT-II, 3 = MDST-IV */
     739      438090 :                     kernel_switch_detect( hTcxEnc0->spectrum[n], hTcxEnc1->spectrum[n], mdst_spectrum[0][n], mdst_spectrum[1][n], nSampCore / nSubframes,
     740      438090 :                                           L_subframeTCX / nSubframes, hTcxEnc0->transform_type[n], &hTcxEnc0->kernel_switch_corr_past, ( totalRate * L_subframe ) / nSubframes );
     741      438090 :                 if ( switchKernel ) /* apply MDST-IV coding in one of the channels */
     742             :                 {
     743        3653 :                     hTcxEnc0->kernel_type[n] = ( hTcxEnc0->kernel_symmetry_past ? 3 : 1 ) - max( 0, switchKernel );
     744        3653 :                     hTcxEnc1->kernel_type[n] = ( hTcxEnc1->kernel_symmetry_past ? 2 : 0 ) + max( 0, switchKernel );
     745             :                 }
     746             :                 else /* switchKernel == 0, switch back to MDCT-IV in both channels */
     747             :                 {
     748      434437 :                     hTcxEnc0->kernel_type[n] = ( hTcxEnc0->kernel_symmetry_past ? 2 : 0 );
     749      434437 :                     hTcxEnc1->kernel_type[n] = ( hTcxEnc1->kernel_symmetry_past ? 2 : 0 );
     750             :                 }
     751             :             }
     752             :             else
     753             :             {
     754         136 :                 hTcxEnc0->kernel_switch_corr_past = 0.f; /* don't update the kernel switching state, postpone it to when data is available */
     755         136 :                 hTcxEnc0->kernel_type[n] = ( hTcxEnc0->kernel_symmetry_past ? 3 : 0 );
     756         136 :                 hTcxEnc1->kernel_type[n] = ( hTcxEnc1->kernel_symmetry_past ? 3 : 0 );
     757             :             }
     758      438226 :             hTcxEnc0->kernel_symmetry_past = hTcxEnc0->kernel_type[n] & 1;
     759      438226 :             hTcxEnc1->kernel_symmetry_past = hTcxEnc1->kernel_type[n] & 1;
     760             : 
     761      438226 :             kernel_switch_update_transforms( hTcxEnc0->spectrum[n], mdst_spectrum[0][n], hTcxEnc0->transform_type[n], sts[0]->hTcxCfg, sts[0]->bwidth_sw_cnt, hTcxEnc0->kernel_type[n],
     762      876452 :                                              hTcxEnc0->new_speech_TCX, ( n == 1 ? NULL : hTcxEnc0->speech_TCX ), windowedSignal[0] + n * L_FRAME48k, L_subframeTCX / nSubframes );
     763      438226 :             kernel_switch_update_transforms( hTcxEnc1->spectrum[n], mdst_spectrum[1][n], hTcxEnc1->transform_type[n], sts[1]->hTcxCfg, sts[1]->bwidth_sw_cnt, hTcxEnc1->kernel_type[n],
     764      876452 :                                              hTcxEnc1->new_speech_TCX, ( n == 1 ? NULL : hTcxEnc1->speech_TCX ), windowedSignal[1] + n * L_FRAME48k, L_subframeTCX / nSubframes );
     765             :         }
     766      863995 :         for ( n = 0; n < nSubframes; n++ )
     767             :         {
     768      438226 :             if ( hTcxEnc0->transform_type[n] == hTcxEnc1->transform_type[n] && /* do not combine TCX-5 and TCX-10 as this causes artifacts */
     769      438090 :                  sts[0]->hTcxCfg->tcx_curr_overlap_mode == sts[1]->hTcxCfg->tcx_curr_overlap_mode &&
     770      436498 :                  sts[0]->hTcxCfg->tcx_last_overlap_mode == sts[1]->hTcxCfg->tcx_last_overlap_mode )
     771             :             {
     772      434736 :                 if ( hTcxEnc0->transform_type[n] == TCX_5 )
     773             :                 {
     774       12824 :                     const int16_t tcx5SizeFB = sts[1]->hTcxCfg->tcx5SizeFB;
     775             : 
     776       12824 :                     hTcxEnc0->enc_ste_pre_corr_past =
     777       12824 :                         enc_ste_pre_mdct( hTcxEnc0->spectrum[n], hTcxEnc1->spectrum[n], mdst_spectrum[0][n], mdst_spectrum[1][n], nSampCore / ( 2 * NB_DIV ),
     778       12824 :                                           L_subframeTCX / ( 2 * NB_DIV ), ( nrg > MAX_STE_PRE_NRG ? 1 : 0 ), hTcxEnc0->enc_ste_pre_corr_past, ( totalRate * L_subframe ) / ( 2 * NB_DIV ) );
     779             : 
     780       12824 :                     hTcxEnc0->enc_ste_pre_corr_past =
     781       12824 :                         enc_ste_pre_mdct( hTcxEnc0->spectrum[n] + tcx5SizeFB, hTcxEnc1->spectrum[n] + tcx5SizeFB, mdst_spectrum[0][n] + tcx5SizeFB, mdst_spectrum[1][n] + tcx5SizeFB, nSampCore / ( 2 * NB_DIV ),
     782       12824 :                                           L_subframeTCX / ( 2 * NB_DIV ), ( nrg > MAX_STE_PRE_NRG ? 1 : 0 ), hTcxEnc0->enc_ste_pre_corr_past, ( totalRate * L_subframe ) / ( 2 * NB_DIV ) );
     783             :                 }
     784             :                 else
     785             :                 {
     786      421912 :                     hTcxEnc0->enc_ste_pre_corr_past =
     787      421912 :                         enc_ste_pre_mdct( hTcxEnc0->spectrum[n], hTcxEnc1->spectrum[n], mdst_spectrum[0][n], mdst_spectrum[1][n], nSampCore / nSubframes,
     788      421912 :                                           L_subframeTCX / nSubframes, ( nrg > MAX_STE_PRE_NRG ? 1 : 0 ), hTcxEnc0->enc_ste_pre_corr_past, ( totalRate * L_subframe ) / nSubframes );
     789             :                 }
     790             :             }
     791             :             else
     792             :             {
     793        3490 :                 hTcxEnc0->enc_ste_pre_corr_past = 0;
     794             :             }
     795             :         }
     796             :     }
     797             :     else
     798             :     {
     799     5085504 :         sts[0]->hTcxEnc->enc_ste_pre_corr_past = 0;
     800     5085504 :         sts[0]->hTcxEnc->kernel_switch_corr_past = 0.f;
     801             : 
     802    15256512 :         for ( ch = 0; ch < CPE_CHANNELS; ch++ )
     803             :         {
     804    10171008 :             TCX_ENC_HANDLE hTcxEncCh = sts[ch]->hTcxEnc;
     805             : 
     806    10171008 :             if ( ( hCPE->cpe_id * CPE_CHANNELS + ch ) >= nChannels )
     807             :             {
     808      876995 :                 hTcxEncCh->kernel_symmetry_past = hTcxEncCh->kernel_type[0] = 0;
     809             : 
     810      876995 :                 continue;
     811             :             }
     812             : 
     813     9294013 :             init_tcx_enc_info( sts[ch], &L_subframe, &L_subframeTCX, &tcx_subframe_coded_lines );
     814     9294013 :             nSubframes = ( hTcxEncCh->tcxMode == TCX_20 ) ? 1 : NB_DIV;
     815             : 
     816    18795835 :             for ( n = 0; n < nSubframes; n++ )
     817             :             {
     818     9501822 :                 hTcxEncCh->kernel_type[n] = ( hTcxEncCh->kernel_symmetry_past && sts[ch]->element_mode == IVAS_CPE_MDCT ? 3 - mct_on : 0 );
     819     9501822 :                 hTcxEncCh->kernel_symmetry_past = hTcxEncCh->kernel_type[n] & 1;
     820             : 
     821     9501822 :                 kernel_switch_update_transforms( hTcxEncCh->spectrum[n], mdst_spectrum[ch][n], hTcxEncCh->transform_type[n], sts[ch]->hTcxCfg, sts[ch]->bwidth_sw_cnt, hTcxEncCh->kernel_type[n],
     822    19003644 :                                                  hTcxEncCh->new_speech_TCX, ( n /*1*/ ? NULL : hTcxEncCh->speech_TCX ), windowedSignal[ch] + n * L_FRAME48k, L_subframeTCX / nSubframes );
     823             :             }
     824             :         }
     825             :     }
     826             : 
     827     5511273 :     TNSAnalysisStereo( sts, mdst_spectrum, 0, tnsSize, tnsBits, param_core, mct_on );
     828             : 
     829             :     /*--------------------------------------------------------------*
     830             :      * Envelope Quantization and FDNS
     831             :      *---------------------------------------------------------------*/
     832             : 
     833    16533819 :     for ( ch = 0; ch < CPE_CHANNELS; ch++ )
     834             :     {
     835    11022546 :         if ( ( hCPE->cpe_id * CPE_CHANNELS + ch ) >= nChannels )
     836             :         {
     837      876995 :             continue;
     838             :         }
     839             : 
     840    10145551 :         st = sts[ch];
     841    10145551 :         nSubframes = ( st->hTcxEnc->tcxMode == TCX_20 ) ? 1 : NB_DIV;
     842             : 
     843    10145551 :         if ( mct_on )
     844             :         {
     845     7298317 :             set_zero( chE, NB_DIV );
     846             :         }
     847             : 
     848    10145551 :         init_tcx_enc_info( st, &L_subframe, &L_subframeTCX, &tcx_subframe_coded_lines );
     849             : 
     850    10145551 :         L_subframe = L_subframe / nSubframes;
     851    10145551 :         L_subframeTCX = ( mct_on ? L_subframeTCX / nSubframes : L_subframe );
     852    10145551 :         tcx_subframe_coded_lines = tcx_subframe_coded_lines / nSubframes;
     853             : 
     854    20523825 :         for ( n = 0; n < nSubframes; n++ )
     855             :         {
     856    10378274 :             if ( st->hTcxEnc->fUseTns[n] )
     857             :             {
     858   129122546 :                 for ( i = 0; i < L_subframeTCX; i++ )
     859             :                 {
     860   128853984 :                     powerSpec[i] = ( st->hTcxEnc->spectrum[n][i] * st->hTcxEnc->spectrum[n][i] );
     861             :                 }
     862             :             }
     863             :             else
     864             :             {
     865  7553711072 :                 for ( i = 0; i < L_subframeTCX; i++ )
     866             :                 {
     867  7543601360 :                     powerSpec[i] = ( mdst_spectrum[ch][n][i] * mdst_spectrum[ch][n][i] + st->hTcxEnc->spectrum[n][i] * st->hTcxEnc->spectrum[n][i] );
     868             :                 }
     869             :             }
     870             : 
     871    10378274 :             if ( mct_on )
     872             :             {
     873     7457273 :                 chE[n] = sum_f( powerSpec, L_subframeTCX );
     874             :             }
     875             : 
     876    10378274 :             sns_compute_scf( powerSpec, st->hTcxCfg->psychParamsCurrent, st->L_frame, scf[ch][n] );
     877             :         }
     878             : 
     879             :         /* MCT: detect whether there are silent channels and set mct_chan_mode accordingly */
     880    10145551 :         if ( mct_on )
     881             :         {
     882     7298317 :             chE_tot = sum_f( chE, NB_DIV );
     883             : 
     884     7298317 :             if ( chE_tot < SILENT_CHANNEL_THRES && nSubframes == 1 )
     885             :             {
     886      154899 :                 st->mct_chan_mode = MCT_CHAN_MODE_IGNORE;
     887      154899 :                 st->bits_frame_channel = 0;
     888             :             }
     889             :             else
     890             :             {
     891     7143418 :                 st->mct_chan_mode = MCT_CHAN_MODE_REGULAR;
     892             :             }
     893             :         }
     894             :     }
     895             : 
     896             :     /* set low br mode, if possible. Can later be discarded, depending on the stereo mode used for SNS parameter decoding */
     897     5511273 :     if ( hCPE->element_brate == IVAS_48k && !( ( sts[0]->core == TCX_20 && sts[1]->core == TCX_20 ) ) )
     898             :     {
     899       14367 :         sns_low_br_mode = !sts[0]->sp_aud_decision0;
     900             :     }
     901             :     else
     902             :     {
     903     5496906 :         sns_low_br_mode = 0;
     904             :     }
     905             : 
     906     5511273 :     if ( !mct_on && sts[0]->sr_core == 25600 && ( ( hCPE->element_brate == IVAS_48k || hCPE->element_brate == IVAS_64k ) ) )
     907             :     {
     908      751300 :         quantize_sns( scf, scf_q, sts, sns_vq_indices, zero_side_flag, sns_stereo_mode );
     909             :     }
     910             :     else
     911             :     {
     912     4759973 :         if ( sts[0]->hTcxEnc->tcxMode == TCX_20 && sts[1]->hTcxEnc->tcxMode == TCX_20 &&
     913     4629810 :              sts[0]->mct_chan_mode == MCT_CHAN_MODE_REGULAR && sts[1]->mct_chan_mode == MCT_CHAN_MODE_REGULAR )
     914             :         {
     915     3673955 :             sns_avq_cod_stereo( scf[0][0], scf[1][0], sts[0]->L_frame, scf_q[0][0], scf_q[1][0], param_lpc[0], param_lpc[1] );
     916             :         }
     917             :         else
     918             :         {
     919             : #ifdef DEBUG_MODE_MDCT
     920             :             {
     921             :                 float ener_side = 0;
     922             :                 float ener_mid = 0;
     923             :                 dbgwrite( &ener_side, sizeof( float ), 1, 1, "./res/ener_side" );
     924             :                 dbgwrite( &ener_mid, sizeof( float ), 1, 1, "./res/ener_mid" );
     925             :             }
     926             : #endif
     927     3258054 :             for ( ch = 0; ch < CPE_CHANNELS; ch++ )
     928             :             {
     929     2172036 :                 param_lpc[ch][0] = ch;
     930     2172036 :                 if ( sts[ch]->mct_chan_mode == MCT_CHAN_MODE_IGNORE )
     931             :                 {
     932     1031894 :                     continue;
     933             :                 }
     934     1140142 :                 st = sts[ch];
     935             : 
     936     1140142 :                 if ( st->hTcxEnc->tcxMode == TCX_20 )
     937             :                 {
     938      947994 :                     sns_avq_cod( scf[ch][0], NULL, scf_q[ch][0], NULL, &param_lpc[ch][1], st->hTcxEnc->tcxMode, st->L_frame, sns_low_br_mode );
     939             :                 }
     940             :                 else
     941             :                 {
     942      192148 :                     sns_avq_cod( scf[ch][1], scf[ch][0], scf_q[ch][1], scf_q[ch][0], &param_lpc[ch][1], st->hTcxEnc->tcxMode, st->L_frame, sns_low_br_mode );
     943             :                 }
     944             :             }
     945             :         }
     946             :     }
     947             : 
     948    16533819 :     for ( ch = 0; ch < CPE_CHANNELS; ch++ )
     949             :     {
     950    11022546 :         if ( sts[ch]->mct_chan_mode == MCT_CHAN_MODE_IGNORE )
     951             :         {
     952     1031894 :             continue;
     953             :         }
     954     9990652 :         st = sts[ch];
     955     9990652 :         nSubframes = ( st->hTcxEnc->tcxMode == TCX_20 ) ? 1 : NB_DIV;
     956     9990652 :         init_tcx_enc_info( st, &L_subframe, &L_subframeTCX, &tcx_subframe_coded_lines );
     957             : 
     958     9990652 :         L_subframe = L_subframe / nSubframes;
     959     9990652 :         L_subframeTCX = L_subframeTCX / nSubframes;
     960     9990652 :         tcx_subframe_coded_lines = tcx_subframe_coded_lines / nSubframes;
     961             : 
     962    20214027 :         for ( n = 0; n < nSubframes; n++ )
     963             :         {
     964    10223375 :             mvr2r( st->hTcxEnc->spectrum[n], orig_spectrum[ch][n], L_subframeTCX );
     965             : 
     966             :             /* Shape spectrum */
     967    10223375 :             ShapeSpectrum( st->hTcxCfg, A_q[ch][n], NULL, L_subframe, tcx_subframe_coded_lines, st->hTcxEnc->spectrum[n], st->hTcxEnc->fUseTns[n], st, scf_q[ch][n] );
     968             :         }
     969             :     }
     970             : 
     971             :     /*--------------------------------------------------------------*
     972             :      * TNS
     973             :      *---------------------------------------------------------------*/
     974             : 
     975             :     /* first deinterleave once more */
     976    16533819 :     for ( ch = 0; ch < CPE_CHANNELS; ch++ )
     977             :     {
     978    11022546 :         if ( sts[ch]->mct_chan_mode == MCT_CHAN_MODE_IGNORE )
     979             :         {
     980     1031894 :             continue;
     981             :         }
     982     9990652 :         st = sts[ch];
     983     9990652 :         nSubframes = ( st->hTcxEnc->tcxMode == TCX_20 ) ? 1 : NB_DIV;
     984             : 
     985    20214027 :         for ( n = 0; n < nSubframes; n++ )
     986             :         {
     987    10223375 :             if ( st->hTcxEnc->transform_type[n] == TCX_5 )
     988             :             {
     989      240389 :                 tcx5SpectrumDeinterleaving( st->hTcxCfg->tcx5SizeFB, st->hTcxEnc->spectrum[n] );
     990      240389 :                 tcx5SpectrumDeinterleaving( st->hTcxCfg->tcx5SizeFB, mdst_spectrum[ch][n] );
     991             :             }
     992             :         }
     993             :     }
     994             : 
     995     5511273 :     TNSAnalysisStereo( sts, mdst_spectrum, 1, tnsSize, tnsBits, param_core, mct_on );
     996             : 
     997    16533819 :     for ( ch = 0; ch < CPE_CHANNELS; ch++ )
     998             :     {
     999    11022546 :         if ( sts[ch]->mct_chan_mode == MCT_CHAN_MODE_IGNORE )
    1000             :         {
    1001     1031894 :             continue;
    1002             :         }
    1003             : 
    1004     9990652 :         st = sts[ch];
    1005     9990652 :         nSubframes = ( st->hTcxEnc->tcxMode == TCX_20 ) ? 1 : NB_DIV;
    1006             : 
    1007     9990652 :         init_tcx_enc_info( st, &L_subframe, &L_subframeTCX, &tcx_subframe_coded_lines );
    1008             : 
    1009     9990652 :         L_subframe = L_subframe / nSubframes;
    1010     9990652 :         L_subframeTCX = L_subframeTCX / nSubframes;
    1011     9990652 :         tcx_subframe_coded_lines = tcx_subframe_coded_lines / nSubframes;
    1012             : 
    1013    20214027 :         for ( n = 0; n < nSubframes; n++ )
    1014             :         {
    1015    10223375 :             if ( !st->hTcxEnc->fUseTns[n] )
    1016             :             {
    1017     9222155 :                 ShapeSpectrum( st->hTcxCfg, A_q[ch][n], NULL, L_subframe, tcx_subframe_coded_lines, mdst_spectrum[ch][n], st->hTcxEnc->fUseTns[n], st, scf_q[ch][n] );
    1018             :             }
    1019             :         }
    1020             :     }
    1021    16533819 :     for ( ch = 0; ch < CPE_CHANNELS; ch++ )
    1022             :     {
    1023             :         /*no need to write last channel bit in case of odd channels*/
    1024    11022546 :         if ( ( hCPE->cpe_id * CPE_CHANNELS + ch ) >= nChannels )
    1025             :         {
    1026      876995 :             continue;
    1027             :         }
    1028             : 
    1029    10145551 :         st = sts[ch];
    1030             : 
    1031    10145551 :         if ( mct_on ) /* signal bits should be written only for MCT*/
    1032             :         {
    1033     7298317 :             if ( st->mct_chan_mode == MCT_CHAN_MODE_IGNORE )
    1034             :             {
    1035      154899 :                 push_next_indice( hBstr, 1, 1 );
    1036             :             }
    1037             :             else
    1038             :             {
    1039     7143418 :                 assert( st->mct_chan_mode == MCT_CHAN_MODE_REGULAR );
    1040     7143418 :                 push_next_indice( hBstr, 0, 1 );
    1041             :             }
    1042             :         }
    1043             :     }
    1044             : 
    1045             :     /* write bitstream with info up to here */
    1046    16533819 :     for ( ch = 0; ch < CPE_CHANNELS; ch++ )
    1047             :     {
    1048    11022546 :         st = sts[ch];
    1049             : 
    1050    11022546 :         if ( st->mct_chan_mode == MCT_CHAN_MODE_IGNORE )
    1051             :         {
    1052     1031894 :             st->side_bits_frame_channel = 0;
    1053             :             /*dummy initialization to prevent range coder crashing in case all channels are silent and bits are distributed to channel 0 */
    1054     1031894 :             *p_param[ch] = 1 + NOISE_FILL_RANGES + LTPSIZE + tnsSize[ch][0] + NPRM_CTX_HM;
    1055     1031894 :             continue;
    1056             :         }
    1057             : 
    1058     9990652 :         enc_prm_pre_mdct( st, param_core[ch], ( ( ( ch > 0 ) && ( sts[0]->hTcxEnc->fUseTns[0] + sts[0]->hTcxEnc->fUseTns[1] > 0 ) && !mct_on ) ? tnsSize[ch] : NULL ), p_param[ch], mct_on, hBstr );
    1059             : 
    1060     9990652 :         if ( ch > 0 && sts[0]->hTcxEnc->fUseTns[0] + sts[0]->hTcxEnc->fUseTns[1] > 0 && !mct_on )
    1061             :         {
    1062      182730 :             if ( st->hTcxEnc->tnsData[0].nFilters < 0 )
    1063             :             {
    1064       53025 :                 tnsBits[ch][0] = 1;
    1065             :             }
    1066             :             else
    1067             :             {
    1068      129705 :                 tnsBits[ch][0]++;
    1069             :             }
    1070             : 
    1071      182730 :             if ( st->core == TCX_10_CORE )
    1072             :             {
    1073       26644 :                 if ( st->hTcxEnc->tnsData[1].nFilters < 0 )
    1074             :                 {
    1075        5223 :                     tnsBits[ch][1] = 1;
    1076             :                 }
    1077             :                 else
    1078             :                 {
    1079       21421 :                     tnsBits[ch][1]++;
    1080             :                 }
    1081             :             }
    1082             :         }
    1083             :     }
    1084             : 
    1085             :     /*--------------------------------------------------------------------------------*
    1086             :      * SNS parameters
    1087             :      *--------------------------------------------------------------------------------*/
    1088             : 
    1089     5511273 :     if ( !mct_on && sts[0]->sr_core == 25600 && ( ( hCPE->element_brate == IVAS_48k || hCPE->element_brate == IVAS_64k ) ) )
    1090             :     {
    1091      751300 :         idx = 0;
    1092             : 
    1093      751300 :         if ( sts[0]->core == sts[1]->core )
    1094             :         {
    1095      746251 :             nSubframes = ( sts[0]->core == TCX_20_CORE ) ? 1 : NB_DIV;
    1096             : 
    1097             :             /* push all stereo mode bits first */
    1098     1510265 :             for ( n = 0; n < nSubframes; ++n )
    1099             :             {
    1100      764014 :                 push_next_indice( hBstr, sns_stereo_mode[n], 1 );
    1101      764014 :                 sts[0]->side_bits_frame_channel++;
    1102             :             }
    1103             : 
    1104             :             /* zero side flags only get transmitted if needed */
    1105     1510265 :             for ( n = 0; n < nSubframes; ++n )
    1106             :             {
    1107      764014 :                 if ( sns_stereo_mode[n] == SNS_STEREO_MODE_MS )
    1108             :                 {
    1109      644120 :                     push_next_indice( hBstr, zero_side_flag[n], 1 );
    1110      644120 :                     sts[0]->side_bits_frame_channel++;
    1111             :                 }
    1112             :             }
    1113             :         }
    1114             : 
    1115     2253900 :         for ( ch = 0; ch < CPE_CHANNELS; ++ch )
    1116             :         {
    1117     1502600 :             st = sts[ch];
    1118     1502600 :             nbits_start_sns = hBstr->nb_bits_tot;
    1119     1502600 :             nSubframes = ( st->hTcxEnc->tcxMode == TCX_20 ) ? 1 : NB_DIV;
    1120     3045775 :             for ( n = 0; n < nSubframes; ++n )
    1121             :             {
    1122     1543175 :                 const int16_t is_side = ch == 1 && sns_stereo_mode[n] == SNS_STEREO_MODE_MS;
    1123     1543175 :                 const int16_t *bits = ( nSubframes == 1 ) ? ivas_sns_cdbks_tcx20_bits : ivas_sns_cdbks_tcx10_bits;
    1124     1543175 :                 int16_t nStages = ( ( nSubframes == 1 ) ? SNS_MSVQ_NSTAGES_TCX20 : SNS_MSVQ_NSTAGES_TCX10 );
    1125             : 
    1126     1543175 :                 if ( is_side )
    1127             :                 {
    1128      644120 :                     if ( zero_side_flag[n] )
    1129             :                     {
    1130      159682 :                         continue;
    1131             :                     }
    1132      484438 :                     nStages = SNS_MSVQ_NSTAGES_SIDE;
    1133      484438 :                     bits = ( sts[ch]->core == TCX_20_CORE ) ? ivas_sns_cdbks_side_tcx20_bits : ivas_sns_cdbks_side_tcx10_bits;
    1134             :                 }
    1135     5899601 :                 for ( int16_t j = 0; j < nStages; ++j, ++idx )
    1136             :                 {
    1137     4516108 :                     push_next_indice( hBstr, sns_vq_indices[idx], bits[j] );
    1138             :                 }
    1139             :             }
    1140     1502600 :             st->side_bits_frame_channel += hBstr->nb_bits_tot - nbits_start_sns;
    1141             :         }
    1142             :     }
    1143             :     else
    1144             :     {
    1145             :         /* write SNS parameter separately since at the decoder, both channels' cores need to be decoded before, so the joint SNS decoding can be done */
    1146     4759973 :         skipped_first_channel = 0;
    1147    14279919 :         for ( ch = 0; ch < CPE_CHANNELS; ch++ )
    1148             :         {
    1149     9519946 :             st = sts[ch];
    1150             : 
    1151     9519946 :             if ( st->mct_chan_mode == MCT_CHAN_MODE_IGNORE )
    1152             :             {
    1153     1031894 :                 skipped_first_channel = 1;
    1154     1031894 :                 continue;
    1155             :             }
    1156             : 
    1157     8488052 :             nbits_start_sns = hBstr->nb_bits_tot;
    1158             : 
    1159     8488052 :             num_sns = ( st->core == TCX_20_CORE ) ? 1 : NB_DIV;
    1160             : 
    1161     8488052 :             if ( ch == 0 || skipped_first_channel )
    1162             :             {
    1163     4707942 :                 push_next_indice( hBstr, param_lpc[0][0] >> 1, 1 );
    1164             : 
    1165     4707942 :                 if ( st->element_brate == IVAS_48k && !( ( sts[0]->core == TCX_20 && sts[1]->core == TCX_20 ) ) )
    1166             :                 {
    1167             :                     /* write classifier decision to signal low br mode for SNS encoding, for all other configs, low_br mode is not possible */
    1168        1603 :                     push_next_indice( hBstr, sns_low_br_mode, 1 );
    1169             :                 }
    1170             :             }
    1171     8488052 :             encode_lpc_avq( hBstr, num_sns, param_lpc[ch], st->core, st->element_mode );
    1172             : 
    1173     8488052 :             st->side_bits_frame_channel += hBstr->nb_bits_tot - nbits_start_sns;
    1174             :         }
    1175             :     }
    1176             : 
    1177             : 
    1178             :     /*update pitch buffer*/
    1179    16533819 :     for ( ch = 0; ch < CPE_CHANNELS; ch++ )
    1180             :     {
    1181    11022546 :         st = sts[ch];
    1182    11022546 :         if ( st->mct_chan_mode == MCT_CHAN_MODE_IGNORE )
    1183             :         {
    1184     1031894 :             continue;
    1185             :         }
    1186     9990652 :         if ( param_core[ch][1 + NOISE_FILL_RANGES] != 0 )
    1187             :         {
    1188     5398349 :             set_f( pitch_buf[ch], ( st->hTcxEnc->tcxltp_pitch_int + (float) st->hTcxEnc->tcxltp_pitch_fr / (float) st->pit_res_max ) * (float) st->sr_core / (float) INT_FS_12k8, NB_SUBFR16k );
    1189             :         }
    1190             :         else
    1191             :         {
    1192     4592303 :             set_f( pitch_buf[ch], L_SUBFR, NB_SUBFR16k );
    1193             :         }
    1194             :     }
    1195             : 
    1196     5511273 :     pop_wmops();
    1197     5511273 :     return;
    1198             : }
    1199             : 
    1200             : 
    1201             : /*--------------------------------------------------------------*
    1202             :  * ivas_mdct_quant_coder()
    1203             :  *
    1204             :  * Spectrum quantization and coding
    1205             :  *---------------------------------------------------------------*/
    1206             : 
    1207     5511273 : void ivas_mdct_quant_coder(
    1208             :     CPE_ENC_HANDLE hCPE,                   /* i/o: Encoder CPE handle                   */
    1209             :     int16_t tnsBits[CPE_CHANNELS][NB_DIV], /* i  : bits needed for TNS parameters       */
    1210             :     int16_t tnsSize[CPE_CHANNELS][NB_DIV], /* i  : size of TNS                          */
    1211             :     int16_t p_param[CPE_CHANNELS][NB_DIV], /* i  : pointer to parameter array           */
    1212             :     const int16_t MCT_flag                 /* i  : hMCT handle allocated (1) or not (0) */
    1213             : )
    1214             : {
    1215             :     Encoder_State *st, **sts;
    1216             :     int16_t bitsAvailable, target_bits, nSubframes, ch, n;
    1217             :     int16_t L_frameTCX[CPE_CHANNELS][NB_DIV];           /* full frame length */
    1218             :     int16_t L_frame[CPE_CHANNELS][NB_DIV];              /* frame length */
    1219             :     int16_t L_spec[CPE_CHANNELS][NB_DIV];               /* length of the coded spectrum */
    1220             :     int16_t tcx_offset[CPE_CHANNELS][NB_DIV];           /* folding point offset relative to the end of the previous frame */
    1221             :     int16_t noiseFillingBorder[CPE_CHANNELS][NB_DIV];   /* noise filling border */
    1222             :     float fac_ns[CPE_CHANNELS][NB_DIV];                 /* noise filling level */
    1223             :     int16_t nf_seed[CPE_CHANNELS][NB_DIV];              /* noise filling random seed */
    1224             :     int16_t hm_active[CPE_CHANNELS][NB_DIV];            /* flag indicating if the harmonic model is active */
    1225             :     float ener[CPE_CHANNELS][NB_DIV];                   /* energy of the quantized spectrum */
    1226             :     float gain_tcx[CPE_CHANNELS][NB_DIV];               /* global gain */
    1227             :     float quantized_spectrum_long[CPE_CHANNELS][N_MAX]; /* quantized MDCT spectrum, inv ms mask mdst spectrum, scratch for MS spectra in the MS decision */
    1228             :     float *quantized_spectrum[CPE_CHANNELS][NB_DIV];
    1229             :     int16_t param_core[CPE_CHANNELS][2 * NPRM_DIV];
    1230             :     int16_t ignore_chan[CPE_CHANNELS];
    1231             :     int16_t target_bitsTCX10[CPE_CHANNELS][NB_DIV];
    1232             :     int16_t nbits_start, total_nbbits;
    1233             : 
    1234     5511273 :     push_wmops( "mdct_core_Q" );
    1235     5511273 :     sts = hCPE->hCoreCoder;
    1236             : 
    1237    16533819 :     for ( ch = 0; ch < CPE_CHANNELS; ch++ )
    1238             :     {
    1239    11022546 :         set_f( fac_ns[ch], 0.0f, NB_DIV );
    1240    11022546 :         set_s( nf_seed[ch], 0, NB_DIV );
    1241    11022546 :         set_s( hm_active[ch], 0, NB_DIV );
    1242    11022546 :         set_f( ener[ch], 0.0f, NB_DIV );
    1243    11022546 :         set_f( gain_tcx[ch], 0.0f, NB_DIV );
    1244             :     }
    1245             : 
    1246    16533819 :     for ( ch = 0; ch < CPE_CHANNELS; ch++ )
    1247             :     {
    1248    11022546 :         quantized_spectrum[ch][0] = quantized_spectrum_long[ch];
    1249    11022546 :         quantized_spectrum[ch][1] = quantized_spectrum_long[ch] + N_TCX10_MAX;
    1250             : 
    1251    11022546 :         st = sts[ch];
    1252             : 
    1253    11022546 :         if ( st->mct_chan_mode == MCT_CHAN_MODE_IGNORE )
    1254             :         {
    1255     1030548 :             ignore_chan[ch] = 1;
    1256     1030548 :             continue;
    1257             :         }
    1258             : 
    1259     9991998 :         nSubframes = ( st->hTcxEnc->tcxMode == TCX_20 ) ? 1 : NB_DIV;
    1260     9991998 :         ignore_chan[ch] = 0;
    1261             : 
    1262     9991998 :         bitsAvailable = st->bits_frame_channel + nSubframes * ( NBITS_TCX_GAIN + NOISE_FILL_RANGES * NBITS_NOISE_FILL_LEVEL ) + tnsBits[ch][0] + tnsBits[ch][1];
    1263             : 
    1264             :         /* calculate TCX10 target bits before to assure minimum amount is distributed between subframes */
    1265     9991998 :         if ( st->hTcxEnc->tcxMode == TCX_10 )
    1266             :         {
    1267             :             int16_t nTnsBitsTCX10Tmp[2];
    1268      232723 :             nTnsBitsTCX10Tmp[0] = tnsBits[ch][0];
    1269      232723 :             nTnsBitsTCX10Tmp[1] = tnsBits[ch][1];
    1270             : 
    1271      232723 :             ivas_mdct_tcx10_bit_distribution( target_bitsTCX10[ch], bitsAvailable, nTnsBitsTCX10Tmp );
    1272             :         }
    1273             : #ifdef DEBUG_PLOT_BITS
    1274             :         if ( st->hTcxEnc->tcxMode == TCX_20 )
    1275             :         {
    1276             :             set_s( &target_bitsTCX10[ch][0], 0, NB_DIV );
    1277             :         }
    1278             :         for ( n = 0; n < NB_DIV; n++ )
    1279             :         {
    1280             :             dbgwrite( &target_bitsTCX10[ch][n], sizeof( int16_t ), 1, 1, "./res/bits_tarrget_TCX10" );
    1281             :         }
    1282             : #endif
    1283             : 
    1284    20216719 :         for ( n = 0; n < nSubframes; n++ )
    1285             :         {
    1286    10224721 :             if ( nSubframes == 2 )
    1287             :             {
    1288      465446 :                 target_bits = target_bitsTCX10[ch][n];
    1289             :             }
    1290             :             else
    1291             :             {
    1292     9759275 :                 target_bits = bitsAvailable / nSubframes - tnsBits[ch][n];
    1293             :             }
    1294             : 
    1295    10224721 :             assert( target_bits >= NBITS_TCX_GAIN + NOISE_FILL_RANGES * NBITS_NOISE_FILL_LEVEL + SMDCT_MINIMUM_ARITH_BITS );
    1296             : 
    1297             :             /*-----------------------------------------------------------*
    1298             :              * Quantize the MDCT spectrum                                *
    1299             :              *-----------------------------------------------------------*/
    1300             : 
    1301    10224721 :             QuantizeTCXSpectrum( st, n, st->hTcxEnc->spectrum[n], NULL, NULL, tnsSize[ch][n], target_bits, 0, &L_frameTCX[ch][n],
    1302    10224721 :                                  &L_frame[ch][n], &L_spec[ch][n], &tcx_offset[ch][n], &noiseFillingBorder[ch][n], quantized_spectrum[ch][n], NULL,
    1303    10224721 :                                  &hm_active[ch][n], NULL, &nf_seed[ch][n], &ener[ch][n], &gain_tcx[ch][n], param_core[ch] + n * NPRM_DIV );
    1304             :         }
    1305             :     }
    1306             : 
    1307     5511273 :     EstimateStereoTCXNoiseLevel( sts, quantized_spectrum, gain_tcx, L_frame, noiseFillingBorder, hm_active, ignore_chan, fac_ns, param_core, MCT_flag );
    1308             : 
    1309    16533819 :     for ( ch = 0; ch < CPE_CHANNELS; ch++ )
    1310             :     {
    1311    11022546 :         st = sts[ch];
    1312             : 
    1313             :         /* update the pointer to the buffer of indices of the second channel */
    1314    11022546 :         if ( ch > 0 )
    1315             :         {
    1316     5511273 :             st->hBstr->ind_list = sts[0]->hBstr->ind_list + sts[0]->hBstr->nb_ind_tot;
    1317             :         }
    1318    11022546 :         if ( st->mct_chan_mode == MCT_CHAN_MODE_IGNORE )
    1319             :         {
    1320             :             /*Enable appropriate upadte of tcx_curr_overlap_mode even for uncoded channel index 1*/
    1321     1030548 :             L_frameTCX[ch][0] = ( st->core == TCX_10_CORE ) ? st->hTcxEnc->L_frameTCX >> 1 : st->hTcxEnc->L_frameTCX;
    1322     1030548 :             L_frameTCX[ch][1] = ( st->core == TCX_10_CORE ) ? st->hTcxEnc->L_frameTCX >> 1 : st->hTcxEnc->L_frameTCX;
    1323             : 
    1324     1030548 :             L_frame[ch][0] = st->L_frame;
    1325     1030548 :             L_frame[ch][1] = st->L_frame;
    1326             :         }
    1327    11022546 :         nSubframes = ( st->hTcxEnc->tcxMode == TCX_20 ) ? 1 : NB_DIV;
    1328             : 
    1329    22277884 :         for ( n = 0; n < nSubframes; n++ )
    1330             :         {
    1331             :             /* Update L_frame_past */
    1332    11255338 :             st->L_frame_past = L_frame[ch][n];
    1333             : 
    1334             :             /* Update overlap */
    1335    11255338 :             if ( ( ( ( L_frameTCX[ch][n] == st->hTcxEnc->L_frameTCX >> 1 ) && n > 0 ) || ( st->hTcxCfg->tcx_last_overlap_mode == TRANSITION_OVERLAP ) ) && ( st->hTcxCfg->tcx_curr_overlap_mode == FULL_OVERLAP ) )
    1336             :             {
    1337      162191 :                 st->hTcxCfg->tcx_curr_overlap_mode = ALDO_WINDOW;
    1338             :             }
    1339             :         }
    1340             : 
    1341             :         /*--------------------------------------------------------------*
    1342             :          * Generate Bitstream
    1343             :          *---------------------------------------------------------------*/
    1344             : 
    1345    11022546 :         if ( st->mct_chan_mode == MCT_CHAN_MODE_IGNORE )
    1346             :         {
    1347     1030548 :             continue;
    1348             :         }
    1349     9991998 :         nbits_start = st->hBstr->nb_bits_tot;
    1350             : 
    1351     9991998 :         writeTCXparam( st, st->hBstr, NULL, param_core[ch], 0, 0, 0, NULL, p_param[ch], target_bitsTCX10[ch], 1 );
    1352             : 
    1353     9991998 :         total_nbbits = st->hBstr->nb_bits_tot - nbits_start - nSubframes * ( NBITS_TCX_GAIN + NOISE_FILL_RANGES * NBITS_NOISE_FILL_LEVEL );
    1354             : 
    1355     9991998 :         assert( st->bits_frame_channel == total_nbbits );
    1356             :     }
    1357             : 
    1358     5511273 :     pop_wmops();
    1359     5511273 :     return;
    1360             : }
    1361             : 
    1362             : 
    1363             : /*--------------------------------------------------------------*
    1364             :  * init_tcx_enc_info()
    1365             :  *
    1366             :  * Initialize TCX parameters
    1367             :  *---------------------------------------------------------------*/
    1368             : 
    1369    39846637 : void init_tcx_enc_info(
    1370             :     Encoder_State *st, /* i/o: coder memory state      */
    1371             :     int16_t *L_frame,
    1372             :     int16_t *L_frameTCX,
    1373             :     int16_t *L_spec )
    1374             : {
    1375    39846637 :     TCX_ENC_HANDLE hTcxEnc = st->hTcxEnc;
    1376    39846637 :     TCX_CONFIG_HANDLE hTcxCfg = st->hTcxCfg;
    1377             :     int16_t tcx_offset, tcx_offsetFB;
    1378             : 
    1379             :     /* Init lengths */
    1380    39846637 :     tcx_offset = hTcxCfg->tcx_offset;
    1381    39846637 :     tcx_offsetFB = hTcxCfg->tcx_offsetFB;
    1382    39846637 :     *L_frame = st->L_frame;
    1383    39846637 :     *L_frameTCX = hTcxEnc->L_frameTCX;
    1384    39846637 :     *L_spec = st->hTcxCfg->tcx_coded_lines;
    1385             : 
    1386    39846637 :     if ( st->last_core == ACELP_CORE )
    1387             :     {
    1388             :         /* if past frame is ACELP */
    1389       79482 :         *L_frame += tcx_offset;
    1390       79482 :         *L_frameTCX += tcx_offsetFB;
    1391       79482 :         *L_spec += st->hTcxCfg->tcx_coded_lines >> 2;
    1392             : 
    1393       79482 :         assert( hTcxCfg->lfacNext <= 0 );
    1394       79482 :         *L_frame -= hTcxCfg->lfacNext;
    1395       79482 :         *L_frameTCX -= hTcxCfg->lfacNextFB;
    1396             :     }
    1397             : 
    1398    39846637 :     return;
    1399             : }

Generated by: LCOV version 1.14