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

          Line data    Source code
       1             : /******************************************************************************************************
       2             : 
       3             :    (C) 2022-2025 IVAS codec Public Collaboration with portions copyright Dolby International AB, Ericsson AB,
       4             :    Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
       5             :    Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
       6             :    Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
       7             :    contributors to this repository. All Rights Reserved.
       8             : 
       9             :    This software is protected by copyright law and by international treaties.
      10             :    The IVAS codec Public Collaboration consisting of Dolby International AB, Ericsson AB,
      11             :    Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
      12             :    Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
      13             :    Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
      14             :    contributors to this repository retain full ownership rights in their respective contributions in
      15             :    the software. This notice grants no license of any kind, including but not limited to patent
      16             :    license, nor is any license granted by implication, estoppel or otherwise.
      17             : 
      18             :    Contributors are required to enter into the IVAS codec Public Collaboration agreement before making
      19             :    contributions.
      20             : 
      21             :    This software is provided "AS IS", without any express or implied warranties. The software is in the
      22             :    development stage. It is intended exclusively for experts who have experience with such software and
      23             :    solely for the purpose of inspection. All implied warranties of non-infringement, merchantability
      24             :    and fitness for a particular purpose are hereby disclaimed and excluded.
      25             : 
      26             :    Any dispute, controversy or claim arising under or in relation to providing this software shall be
      27             :    submitted to and settled by the final, binding jurisdiction of the courts of Munich, Germany in
      28             :    accordance with the laws of the Federal Republic of Germany excluding its conflict of law rules and
      29             :    the United Nations Convention on Contracts on the International Sales of Goods.
      30             : 
      31             : *******************************************************************************************************/
      32             : 
      33             : #include <assert.h>
      34             : #include <stdint.h>
      35             : #include "options.h"
      36             : #include <math.h>
      37             : #include "cnst.h"
      38             : #include "prot.h"
      39             : #include "ivas_prot.h"
      40             : #include "rom_com.h"
      41             : #ifdef DEBUGGING
      42             : #include "debug.h"
      43             : #endif
      44             : #include "wmc_auto.h"
      45             : 
      46             : /*-------------------------------------------------------------------*
      47             :  * sync_tcx_mode()
      48             :  *
      49             :  * Synchronize under certain conditions the tcx modes and set overlap
      50             :  * windows accordingly
      51             :  *-------------------------------------------------------------------*/
      52             : 
      53       81183 : static void sync_tcx_mode(
      54             :     Encoder_State **st /* i/o: Encoder state */
      55             : )
      56             : {
      57       81183 :     const float prevAccNrg0 = st[0]->hTranDet->subblockEnergies.accSubblockNrg[st[0]->hTranDet->subblockEnergies.nDelay];
      58       81183 :     const float prevAccNrg1 = st[1]->hTranDet->subblockEnergies.accSubblockNrg[st[1]->hTranDet->subblockEnergies.nDelay];
      59       81183 :     const float lastAccNrg0 = st[0]->hTranDet->subblockEnergies.accSubblockNrg[st[0]->hTranDet->subblockEnergies.nDelay + st[0]->hTranDet->transientDetector.nSubblocksToCheck];
      60       81183 :     const float lastAccNrg1 = st[1]->hTranDet->subblockEnergies.accSubblockNrg[st[1]->hTranDet->subblockEnergies.nDelay + st[1]->hTranDet->transientDetector.nSubblocksToCheck];
      61             : 
      62       81183 :     if ( ( st[0]->hTcxEnc->tcxMode == st[1]->hTcxEnc->tcxMode ) && ( st[0]->element_brate == IVAS_48k ) &&
      63       32659 :          ( st[0]->hTcxCfg->tcx_curr_overlap_mode != st[1]->hTcxCfg->tcx_curr_overlap_mode ) )
      64             :     {
      65         400 :         if ( ( st[0]->hTcxCfg->tcx_curr_overlap_mode == FULL_OVERLAP ) || ( st[1]->hTcxCfg->tcx_curr_overlap_mode == FULL_OVERLAP ) ) /* synchronize overlaps */
      66             :         {
      67          53 :             st[0]->hTcxCfg->tcx_curr_overlap_mode = st[1]->hTcxCfg->tcx_curr_overlap_mode = max( st[0]->hTcxCfg->tcx_curr_overlap_mode, st[1]->hTcxCfg->tcx_curr_overlap_mode );
      68             :         }
      69             :         else
      70             :         {
      71         347 :             st[0]->hTcxCfg->tcx_curr_overlap_mode = st[1]->hTcxCfg->tcx_curr_overlap_mode = min( st[0]->hTcxCfg->tcx_curr_overlap_mode, st[1]->hTcxCfg->tcx_curr_overlap_mode );
      72             :         }
      73             :     }
      74             : 
      75       81183 :     if ( ( prevAccNrg0 * 25.0f > prevAccNrg1 ) && ( prevAccNrg1 * 25.0f > prevAccNrg0 ) && /* 14 dB */
      76       77189 :          ( lastAccNrg0 * 16.0f > lastAccNrg1 ) && ( lastAccNrg1 * 16.0f > lastAccNrg0 ) )  /* 12 dB */
      77             :     {
      78       74735 :         st[0]->hTcxEnc->tcxMode = st[1]->hTcxEnc->tcxMode = max( st[0]->hTcxEnc->tcxMode, st[1]->hTcxEnc->tcxMode ); /* synchronize TCX mode of both channels */
      79             : 
      80       74735 :         if ( st[0]->hTcxEnc->tcxMode == TCX_10 )
      81             :         {
      82        1690 :             if ( st[0]->hTcxCfg->tcx_curr_overlap_mode == ALDO_WINDOW )
      83             :             {
      84         360 :                 st[0]->hTcxCfg->tcx_curr_overlap_mode = st[1]->hTcxCfg->tcx_curr_overlap_mode;
      85             :             }
      86             : 
      87        1690 :             if ( st[0]->hTcxCfg->tcx_last_overlap_mode == ALDO_WINDOW )
      88             :             {
      89         344 :                 st[0]->hTcxCfg->tcx_last_overlap_mode = st[1]->hTcxCfg->tcx_last_overlap_mode;
      90             :             }
      91             :         }
      92             : 
      93       74735 :         if ( st[1]->hTcxEnc->tcxMode == TCX_10 )
      94             :         {
      95        1690 :             if ( st[1]->hTcxCfg->tcx_curr_overlap_mode == ALDO_WINDOW )
      96             :             {
      97         398 :                 st[1]->hTcxCfg->tcx_curr_overlap_mode = st[0]->hTcxCfg->tcx_curr_overlap_mode;
      98             :             }
      99             : 
     100        1690 :             if ( st[1]->hTcxCfg->tcx_last_overlap_mode == ALDO_WINDOW )
     101             :             {
     102         363 :                 st[1]->hTcxCfg->tcx_last_overlap_mode = st[0]->hTcxCfg->tcx_last_overlap_mode;
     103             :             }
     104             :         }
     105             :     }
     106             : 
     107       81183 :     return;
     108             : }
     109             : 
     110             : 
     111             : /*-------------------------------------------------------------------*
     112             :  * stereo_mdct_core_enc()
     113             :  *
     114             :  * joint stereo mdct core encoder
     115             :  *-------------------------------------------------------------------*/
     116             : 
     117       82641 : void stereo_mdct_core_enc(
     118             :     CPE_ENC_HANDLE hCPE,                       /* i/o: CPE encoder structure               */
     119             :     float new_samples[CPE_CHANNELS][L_INP],    /* i  : new samples                         */
     120             :     float old_wsp[CPE_CHANNELS][L_WSP],        /* i  : 12.8kHz weighted speech (for LTP    */
     121             :     float pitch_buf[CPE_CHANNELS][NB_SUBFR16k] /* o  : floating pitch for each subframe    */
     122             : )
     123             : {
     124             :     int16_t i, ch, nSubframes, L_subframeTCX;
     125             :     int16_t n, nAvailBits;
     126             :     int16_t tnsSize[CPE_CHANNELS][NB_DIV];                                              /* number of tns parameters put into prm */
     127             :     int16_t tnsBits[CPE_CHANNELS][NB_DIV];                                              /* number of tns bits in the frame */
     128             :     float *p_orig_spectrum_long[CPE_CHANNELS], orig_spectrum_long[CPE_CHANNELS][N_MAX]; /* MDCT output (L/R). */
     129             :     float *orig_spectrum[CPE_CHANNELS][NB_DIV];                                         /* Pointers to MDCT output for a short block (L/R) */
     130             :     float powerSpec[CPE_CHANNELS][N_MAX];
     131             :     float *p_powerSpec[CPE_CHANNELS];
     132             :     float powerSpecMsInv_long[CPE_CHANNELS][N_MAX]; /* MS inv power spectrum, also inverse MDST spectrum */
     133             :     float *powerSpecMsInv[CPE_CHANNELS][NB_DIV];
     134             :     float quantized_spectrum_long[CPE_CHANNELS][N_MAX]; /* quantized MDCT spectrum, inv ms mask mdst spectrum, scratch for MS spectra in the MS decision */
     135             :     float *quantized_spectrum[CPE_CHANNELS][NB_DIV];
     136             :     float *inv_mdst_spectrum[CPE_CHANNELS][NB_DIV];
     137             :     float *inv_spectrum[CPE_CHANNELS][NB_DIV];
     138             :     float *p_mdst_spectrum_long[CPE_CHANNELS], mdst_spectrum_long[CPE_CHANNELS][N_MAX];
     139             :     float *mdst_spectrum[CPE_CHANNELS][NB_DIV];
     140             :     Encoder_State *st, **sts;
     141             :     STEREO_MDCT_ENC_DATA_HANDLE hStereoMdct;
     142             :     BSTR_ENC_HANDLE hBstr;
     143             :     int16_t ms_mask[2][MAX_SFB];
     144             :     int16_t p_param[CPE_CHANNELS][NB_DIV];
     145             :     int16_t stereo_bits;
     146             :     int16_t meta_bits, signal_bits;
     147             : 
     148       82641 :     push_wmops( "stereo_mdct_core_enc" );
     149             : 
     150       82641 :     L_subframeTCX = 0; /* to avoid compilation warning */
     151             : 
     152             :     /*--------------------------------------------------------------*
     153             :      * Set pointers
     154             :      *---------------------------------------------------------------*/
     155             : 
     156      247923 :     for ( ch = 0; ch < CPE_CHANNELS; ch++ )
     157             :     {
     158      165282 :         p_mdst_spectrum_long[ch] = mdst_spectrum_long[ch];
     159      165282 :         p_orig_spectrum_long[ch] = orig_spectrum_long[ch];
     160             :     }
     161             : 
     162       82641 :     sts = hCPE->hCoreCoder;
     163       82641 :     hStereoMdct = hCPE->hStereoMdct;
     164       82641 :     hBstr = sts[0]->hBstr;
     165             : 
     166       82641 :     meta_bits = hCPE->hMetaData->nb_bits_tot;
     167       82641 :     signal_bits = hBstr->nb_bits_tot;
     168             : 
     169             :     /*--------------------------------------------------------------*
     170             :      * Initialization
     171             :      *---------------------------------------------------------------*/
     172             : 
     173       82641 :     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 */
     174       82641 :     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 );
     175             : 
     176      247923 :     for ( ch = 0; ch < CPE_CHANNELS; ch++ )
     177             :     {
     178      165282 :         st = sts[ch];
     179      165282 :         SetTCXModeInfo( st, st->hTranDet, &st->hTcxCfg->tcx_curr_overlap_mode );
     180             :     }
     181             : 
     182             :     /* adaptively sync tcx modes*/
     183       82641 :     if ( ( hCPE->last_element_mode == IVAS_CPE_MDCT ) && ( abs( sts[0]->hTcxCfg->tcx_last_overlap_mode - sts[1]->hTcxCfg->tcx_last_overlap_mode ) > 2 ||
     184       80788 :                                                            sts[0]->hTcxCfg->tcx_last_overlap_mode == sts[1]->hTcxCfg->tcx_last_overlap_mode ||
     185         757 :                                                            sts[0]->hTcxCfg->tcx_last_overlap_mode + sts[1]->hTcxCfg->tcx_last_overlap_mode == 5 ) ) /* disable syncing for stereo switching or large diff in last frame overlap */
     186             :     {
     187       81183 :         sync_tcx_mode( sts );
     188             :     }
     189             : 
     190       82641 :     if ( hCPE->hCoreCoder[0]->igf )
     191             :     {
     192             : #ifdef DEBUGGING
     193             :         int16_t orig_mdct_stereo_mode_cmdl = hCPE->hStereoMdct->mdct_stereo_mode_cmdl;
     194             :         hCPE->hStereoMdct->mdct_stereo_mode_cmdl = SMDCT_MS_DECISION;
     195             : #endif
     196       65446 :         initMdctStereoEncData( hCPE->hStereoMdct, STEREO_FORMAT, IVAS_CPE_MDCT, hCPE->element_brate, hCPE->hCoreCoder[0]->bwidth, hCPE->hCoreCoder[0]->igf, hCPE->hCoreCoder[0]->hIGFEnc->igfData.igfInfo.grid, 0 );
     197             : #ifdef DEBUGGING
     198             :         hCPE->hStereoMdct->mdct_stereo_mode_cmdl = orig_mdct_stereo_mode_cmdl;
     199             : #endif
     200             : 
     201       65446 :         stereo_mdct_init_igf_start_band( &( hCPE->hStereoMdct->stbParamsTCX20 ), 1.0f, hCPE->hCoreCoder[0]->bwidth, hCPE->element_brate );
     202       65446 :         stereo_mdct_init_igf_start_band( &( hCPE->hStereoMdct->stbParamsTCX10 ), 0.5f, hCPE->hCoreCoder[0]->bwidth, hCPE->element_brate );
     203       65446 :         stereo_mdct_init_igf_start_band( &( hCPE->hStereoMdct->stbParamsTCX20afterACELP ), 1.25f, hCPE->hCoreCoder[0]->bwidth, hCPE->element_brate );
     204             :     }
     205             :     else
     206             :     {
     207       17195 :         hCPE->hStereoMdct->stbParamsTCX20.sfbIgfStart = -1;
     208       17195 :         hCPE->hStereoMdct->stbParamsTCX10.sfbIgfStart = -1;
     209       17195 :         hCPE->hStereoMdct->stbParamsTCX10.nBandsStereoCore = hCPE->hStereoMdct->stbParamsTCX10.sfbCnt;
     210       17195 :         hCPE->hStereoMdct->stbParamsTCX20.nBandsStereoCore = hCPE->hStereoMdct->stbParamsTCX20.sfbCnt;
     211             :     }
     212             : 
     213      247923 :     for ( ch = 0; ch < CPE_CHANNELS; ch++ )
     214             :     {
     215      165282 :         orig_spectrum[ch][0] = orig_spectrum_long[ch];
     216      165282 :         orig_spectrum[ch][1] = orig_spectrum_long[ch] + N_TCX10_MAX;
     217      165282 :         quantized_spectrum[ch][0] = quantized_spectrum_long[ch];
     218      165282 :         quantized_spectrum[ch][1] = quantized_spectrum_long[ch] + N_TCX10_MAX;
     219      165282 :         inv_mdst_spectrum[ch][0] = powerSpecMsInv[ch][0] = powerSpecMsInv_long[ch];
     220      165282 :         inv_mdst_spectrum[ch][1] = powerSpecMsInv[ch][1] = powerSpecMsInv_long[ch] + N_TCX10_MAX;
     221      165282 :         inv_spectrum[ch][0] = quantized_spectrum[ch][0];
     222      165282 :         inv_spectrum[ch][1] = quantized_spectrum[ch][1];
     223      165282 :         mdst_spectrum[ch][0] = mdst_spectrum_long[ch];
     224      165282 :         mdst_spectrum[ch][1] = mdst_spectrum_long[ch] + N_TCX10_MAX;
     225      165282 :         sts[ch]->hTcxEnc->tns_ms_flag[0] = 0;
     226      165282 :         sts[ch]->hTcxEnc->tns_ms_flag[1] = 0;
     227             :     }
     228             : 
     229             :     /*--------------------------------------------------------------*
     230             :      * MDCT core preprocessing:
     231             :      * - initialization
     232             :      * - Core Signal Analysis: MDCT, TNS
     233             :      * - Envelope Quantization and FDNS
     234             :      * - TNS
     235             :      *---------------------------------------------------------------*/
     236             : 
     237       82641 :     ivas_mdct_core_whitening_enc( hCPE, new_samples, old_wsp, pitch_buf, p_mdst_spectrum_long,
     238             :                                   tnsBits, p_orig_spectrum_long, tnsSize, p_param, hBstr, 0, CPE_CHANNELS );
     239             : 
     240      247923 :     for ( ch = 0; ch < CPE_CHANNELS; ch++ )
     241             :     {
     242      165282 :         st = sts[ch];
     243      165282 :         nSubframes = ( st->hTcxEnc->tcxMode == TCX_20 ) ? 1 : NB_DIV;
     244             : 
     245      334729 :         for ( n = 0; n < nSubframes; n++ )
     246             :         {
     247      169447 :             st->side_bits_frame_channel += NBITS_TCX_GAIN + NOISE_FILL_RANGES * NBITS_NOISE_FILL_LEVEL;
     248             :         }
     249             :     }
     250             : 
     251       82641 :     sts[0]->side_bits_frame_channel += SMDCT_NBBITS_SPLIT_RATIO;
     252             : 
     253             : 
     254             :     /*--------------------------------------------------------------*
     255             :      * Stereo Processing
     256             :      *---------------------------------------------------------------*/
     257             : 
     258       82641 :     if ( !hStereoMdct->isSBAStereoMode )
     259             :     {
     260       42789 :         stereo_coder_tcx( hStereoMdct, sts, ms_mask, mdst_spectrum, inv_spectrum, inv_mdst_spectrum, 0 );
     261             :     }
     262             : 
     263             :     /*--------------------------------------------------------------*
     264             :      * Power spectrum calculation
     265             :      *---------------------------------------------------------------*/
     266             : 
     267      247923 :     for ( ch = 0; ch < CPE_CHANNELS; ch++ )
     268             :     {
     269      165282 :         nSubframes = ( sts[ch]->hTcxEnc->tcxMode == TCX_20 ) ? 1 : NB_DIV;
     270      165282 :         L_subframeTCX = sts[ch]->hTcxEnc->L_frameTCX / nSubframes;
     271      165282 :         if ( sts[ch]->last_core == ACELP_CORE )
     272             :         {
     273        1385 :             L_subframeTCX += L_subframeTCX / 4;
     274             :         }
     275             : 
     276      334729 :         for ( n = 0; n < nSubframes; n++ )
     277             :         {
     278      169447 :             if ( sts[ch]->hTcxEnc->tns_ms_flag[n] )
     279             :             {
     280             :                 /* power spectrum: MDCT^2 + MDST^2 */
     281    54304346 :                 for ( i = 0; i < L_subframeTCX; i++ )
     282             :                 {
     283    54235520 :                     powerSpecMsInv[ch][n][i] = inv_mdst_spectrum[ch][n][i] * inv_mdst_spectrum[ch][n][i] + inv_spectrum[ch][n][i] * inv_spectrum[ch][n][i];
     284    54235520 :                     powerSpec[ch][i + n * L_subframeTCX] = mdst_spectrum[ch][n][i] * mdst_spectrum[ch][n][i] + sts[ch]->hTcxEnc->spectrum[n][i] * sts[ch]->hTcxEnc->spectrum[n][i];
     285             :                 }
     286             :             }
     287             :             else
     288             :             {
     289      100621 :                 if ( hStereoMdct->mdct_stereo_mode[n] != SMDCT_DUAL_MONO )
     290             :                 {
     291             :                     /* power spectrum: MDCT^2 + MDST^2 */
     292       93469 :                     powerSpecMsInv[ch][n][0] = inv_spectrum[ch][n][0] * inv_spectrum[ch][n][0];
     293    66968931 :                     for ( i = 1; i < L_subframeTCX - 1; i++ )
     294             :                     {
     295    66875462 :                         float mdst = ( inv_spectrum[ch][n][i + 1] - inv_spectrum[ch][n][i - 1] ); /* An MDST estimate */
     296    66875462 :                         powerSpecMsInv[ch][n][i] = mdst * mdst + inv_spectrum[ch][n][i] * inv_spectrum[ch][n][i];
     297             :                     }
     298       93469 :                     powerSpecMsInv[ch][n][L_subframeTCX - 1] = inv_spectrum[ch][n][L_subframeTCX - 1] * inv_spectrum[ch][n][L_subframeTCX - 1];
     299             :                 }
     300             : 
     301             :                 /* power spectrum: MDCT^2 + MDST^2 */
     302      100621 :                 powerSpec[ch][n * L_subframeTCX] = sts[ch]->hTcxEnc->spectrum[n][0] * sts[ch]->hTcxEnc->spectrum[n][0];
     303    71913299 :                 for ( i = 1; i < L_subframeTCX - 1; i++ )
     304             :                 {
     305    71812678 :                     float mdst = ( sts[ch]->hTcxEnc->spectrum[n][i + 1] - sts[ch]->hTcxEnc->spectrum[n][i - 1] ); /* An MDST estimate */
     306    71812678 :                     powerSpec[ch][i + n * L_subframeTCX] = mdst * mdst + sts[ch]->hTcxEnc->spectrum[n][i] * sts[ch]->hTcxEnc->spectrum[n][i];
     307             :                 }
     308      100621 :                 powerSpec[ch][L_subframeTCX - 1 + n * L_subframeTCX] = sts[ch]->hTcxEnc->spectrum[n][L_subframeTCX - 1] * sts[ch]->hTcxEnc->spectrum[n][L_subframeTCX - 1];
     309             :             }
     310             :         }
     311             :     }
     312             : 
     313      247923 :     for ( ch = 0; ch < CPE_CHANNELS; ch++ )
     314             :     {
     315      165282 :         st = sts[ch];
     316      165282 :         nSubframes = ( st->hTcxEnc->tcxMode == TCX_20 ) ? 1 : NB_DIV;
     317      165282 :         L_subframeTCX = st->hTcxEnc->L_frameTCX / nSubframes;
     318             : #ifdef DEBUG_MODE_MDCT
     319             :         dbgwrite( powerSpec[ch], sizeof( float ), 640, 1, "./res/powerSpec" );
     320             : 
     321             : #endif
     322      165282 :         if ( st->last_core == ACELP_CORE )
     323             :         {
     324        1385 :             L_subframeTCX += L_subframeTCX / 4;
     325             :         }
     326             : 
     327      165282 :         if ( ( st->hTcxEnc->tcxMode == TCX_20 ) && ( st->element_brate < HQ_96k || st->igf ) )
     328             :         {
     329             :             /*-----------------------------------------------------------*
     330             :              * Compute noise-measure flags for spectrum filling          *
     331             :              * and quantization (0: tonal, 1: noise-like).               *
     332             :              * Detect low pass if present.                               *
     333             :              *-----------------------------------------------------------*/
     334             : 
     335      144799 :             ComputeSpectrumNoiseMeasure( powerSpec[ch], L_subframeTCX, st->hTcxEnc->nmStartLine * ( L_subframeTCX / st->hTcxEnc->L_frameTCX ),
     336      144799 :                                          ( st->L_frame * st->last_sr_core != st->L_frame_past * st->sr_core ) || ( st->last_core != TCX_20_CORE ), st->hTcxEnc->memQuantZeros, L_subframeTCX );
     337             :         }
     338             : 
     339      165282 :         st->hTcxEnc->measuredBwRatio = 1.f; /* No bandwidth limit for the noise filling */
     340             :     }
     341             : 
     342             :     /*--------------------------------------------------------------*
     343             :      * IGF
     344             :      *---------------------------------------------------------------*/
     345             : 
     346       82641 :     if ( sts[0]->igf || sts[1]->igf )
     347             :     {
     348       65446 :         if ( sts[0]->hTcxEnc->tcxMode == sts[1]->hTcxEnc->tcxMode )
     349             :         {
     350             :             /* Band-wise M/S for MDST */
     351       64950 :             nSubframes = ( sts[0]->hTcxEnc->tcxMode == TCX_20 || sts[1]->hTcxEnc->tcxMode == TCX_20 ) ? 1 : NB_DIV;
     352      131340 :             for ( n = 0; n < nSubframes; n++ )
     353             :             {
     354       66390 :                 if ( ( hStereoMdct->mdct_stereo_mode[n] != hStereoMdct->IGFStereoMode[n] || hStereoMdct->mdct_stereo_mode[n] == SMDCT_BW_MS ) && !hStereoMdct->isSBAStereoMode )
     355             :                 {
     356       18394 :                     p_powerSpec[0] = powerSpec[0];
     357       18394 :                     p_powerSpec[1] = powerSpec[1];
     358       18394 :                     ProcessStereoIGF( hStereoMdct, sts, ms_mask, orig_spectrum, p_powerSpec, powerSpecMsInv, inv_spectrum, n, hCPE->hCoreCoder[0]->sp_aud_decision0, hCPE->hCoreCoder[0]->element_brate, 0 );
     359             :                 }
     360             :                 else
     361             :                 {
     362      143988 :                     for ( ch = 0; ch < CPE_CHANNELS; ch++ )
     363             :                     {
     364       95992 :                         st = sts[ch];
     365             :                         /* update the pointer to the buffer of indices of the second channel */
     366       95992 :                         if ( ch == 1 )
     367             :                         {
     368       47996 :                             st->hBstr->ind_list = sts[0]->hBstr->ind_list + sts[0]->hBstr->nb_ind_tot;
     369             :                         }
     370       95992 :                         ProcessIGF( st, st->hTcxEnc->spectrum[n], orig_spectrum[ch][n], &powerSpec[ch][n * L_subframeTCX], st->core == TCX_20_CORE, n, hCPE->hCoreCoder[0]->sp_aud_decision0, 0 );
     371             :                     }
     372             :                 }
     373             :             }
     374             :         }
     375             :         else
     376             :         {
     377        1488 :             for ( ch = 0; ch < CPE_CHANNELS; ch++ )
     378             :             {
     379         992 :                 st = sts[ch];
     380             : 
     381             :                 /* update the pointer to the buffer of indices of the second channel */
     382         992 :                 if ( ch == 1 )
     383             :                 {
     384         496 :                     st->hBstr->ind_list = sts[0]->hBstr->ind_list + sts[0]->hBstr->nb_ind_tot;
     385             :                 }
     386             : 
     387         992 :                 nSubframes = ( st->hTcxEnc->tcxMode == TCX_20 ) ? 1 : NB_DIV;
     388         992 :                 if ( st->igf )
     389             :                 {
     390        2480 :                     for ( n = 0; n < nSubframes; n++ )
     391             :                     {
     392        1488 :                         ProcessIGF( st, st->hTcxEnc->spectrum[n], orig_spectrum[ch][n], &powerSpec[ch][n * L_subframeTCX], st->core == TCX_20_CORE, n, hCPE->hCoreCoder[0]->sp_aud_decision0, 0 );
     393             :                     }
     394             :                 }
     395             :             }
     396             :         }
     397             : 
     398             :         /*write IGF data to bitstream*/
     399      196338 :         for ( ch = 0; ch < CPE_CHANNELS; ch++ )
     400             :         {
     401             : #ifdef DEBUG_PLOT_BITS
     402             :             int16_t tmp = hBstr->nb_bits_tot;
     403             : #endif
     404      130892 :             st = sts[ch];
     405      130892 :             enc_prm_igf_mdct( st, hBstr );
     406             : #ifdef DEBUG_PLOT_BITS
     407             :             tmp = hBstr->nb_bits_tot - tmp;
     408             :             dbgwrite( &tmp, sizeof( int16_t ), 1, 1, "./res/bits_IGF" );
     409             : #endif
     410             :         }
     411             :     }
     412             : 
     413             :     /* correct side bits per channel*/
     414       82641 :     sts[0]->side_bits_frame_channel -= SMDCT_NBBITS_SPLIT_RATIO;
     415       82641 :     if ( !hStereoMdct->isSBAStereoMode )
     416             :     {
     417       42789 :         stereo_bits = write_stereo_to_bitstream( hStereoMdct, sts, ms_mask, 0, hBstr );
     418             :     }
     419             :     else
     420             :     {
     421       39852 :         stereo_bits = 0;
     422             :     }
     423             : 
     424             :     /*--------------------------------------------------------------*
     425             :      * Split available bits between channels
     426             :      *---------------------------------------------------------------*/
     427             : 
     428       82641 :     FindSplitRatio( hCPE, sts );
     429             : 
     430       82641 :     assert( hStereoMdct->split_ratio > 0 && hStereoMdct->split_ratio < SMDCT_BITRATE_RATIO_RANGE );
     431             : 
     432             :     /* Split available bits between channels */
     433       82641 :     nAvailBits = sts[0]->bits_frame_channel + sts[1]->bits_frame_channel - hBstr->nb_bits_tot - sts[0]->core * ( NBITS_TCX_GAIN + NOISE_FILL_RANGES * NBITS_NOISE_FILL_LEVEL + SMDCT_MINIMUM_ARITH_BITS ) - sts[1]->core * ( NBITS_TCX_GAIN + NOISE_FILL_RANGES * NBITS_NOISE_FILL_LEVEL + SMDCT_MINIMUM_ARITH_BITS );
     434             : 
     435             :     /* remove metadata bits */
     436       82641 :     nAvailBits -= meta_bits;
     437             : 
     438       82641 :     nAvailBits -= SMDCT_NBBITS_SPLIT_RATIO;
     439       82641 :     splitAvailableBits( nAvailBits, hStereoMdct->split_ratio, hStereoMdct->isSBAStereoMode, &sts[0]->bits_frame_channel, &sts[1]->bits_frame_channel );
     440             : #ifdef DEBUG_MODE_MDCT
     441             :     dbgwrite( &nAvailBits, sizeof( int16_t ), 1, 1, "./res/nAvailBits" );
     442             : #endif
     443             : 
     444       82641 :     sts[0]->bits_frame_channel += sts[0]->core * SMDCT_MINIMUM_ARITH_BITS;
     445       82641 :     sts[1]->bits_frame_channel += sts[1]->core * SMDCT_MINIMUM_ARITH_BITS;
     446             : 
     447      247923 :     for ( ch = 0; ch < CPE_CHANNELS; ch++ )
     448             :     {
     449      165282 :         sts[ch]->total_brate = ( sts[ch]->bits_frame_channel + sts[ch]->side_bits_frame_channel ) * FRAMES_PER_SEC;
     450             :     }
     451       82641 :     stereo_bits += SMDCT_NBBITS_SPLIT_RATIO;
     452       82641 :     assert( ( sts[0]->total_brate + sts[1]->total_brate + ( stereo_bits + signal_bits + meta_bits ) * FRAMES_PER_SEC ) == hCPE->element_brate + hCPE->brate_surplus );
     453       82641 :     assert( hStereoMdct->split_ratio > 0 && hStereoMdct->split_ratio < SMDCT_BITRATE_RATIO_RANGE );
     454             : 
     455       82641 :     push_next_indice( hBstr, hStereoMdct->split_ratio, SMDCT_NBBITS_SPLIT_RATIO );
     456             : 
     457             : #ifdef DEBUG_MODE_MDCT
     458             :     dbgwrite( &hStereoMdct->split_ratio, sizeof( int16_t ), 1, 1, "./res/split_ratio" );
     459             :     for ( ch = 0; ch < CPE_CHANNELS; ch++ )
     460             :     {
     461             :         dbgwrite( &sts[ch]->bits_frame_channel, sizeof( int16_t ), 1, 1, "./res/bit_frame_channel" );
     462             :     }
     463             : #endif
     464             : 
     465       82641 :     ivas_mdct_quant_coder( hCPE, tnsBits, tnsSize, p_param, 0 );
     466             : 
     467       82641 :     pop_wmops();
     468             : 
     469       82641 :     return;
     470             : }

Generated by: LCOV version 1.14