LCOV - code coverage report
Current view: top level - lib_com - codec_tcx_common.c (source / functions) Hit Total Coverage
Test: Coverage on main @ 6baab0c613aa6c7100498ed7b93676aa8198a493 Lines: 94 96 97.9 %
Date: 2025-05-29 08:28:55 Functions: 7 7 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             : /*====================================================================================
      34             :     EVS Codec 3GPP TS26.443 Nov 04, 2021. Version 12.14.0 / 13.10.0 / 14.6.0 / 15.4.0 / 16.3.0
      35             :   ====================================================================================*/
      36             : 
      37             : #include <stdint.h>
      38             : #include "options.h"
      39             : #include <math.h>
      40             : #include "prot.h"
      41             : #ifdef DEBUGGING
      42             : #include "debug.h"
      43             : #endif
      44             : #include "wmc_auto.h"
      45             : 
      46             : /*-------------------------------------------------------------------*
      47             :  * tcxGetNoiseFillingTilt()
      48             :  *
      49             :  *
      50             :  *-------------------------------------------------------------------*/
      51             : 
      52    51659798 : int16_t tcxGetNoiseFillingTilt(
      53             :     const float A[],
      54             :     const int16_t L_frame,
      55             :     const int16_t mode,
      56             :     float *noiseTiltFactor )
      57             : {
      58             :     int16_t firstLine;
      59             : 
      60    51659798 :     if ( mode )
      61             :     {
      62    50322974 :         firstLine = L_frame / 6;
      63    50322974 :         *noiseTiltFactor = 0.5625f;
      64             :     }
      65             :     else
      66             :     {
      67     1336824 :         firstLine = L_frame / 8;
      68     1336824 :         *noiseTiltFactor = get_gain( A + 1, A, M, NULL );
      69     1336824 :         *noiseTiltFactor = min( 1.0f, ( *noiseTiltFactor ) + 0.09375f );
      70             :     }
      71             : 
      72    51659798 :     return firstLine;
      73             : }
      74             : 
      75             : /*-------------------------------------------------------------------*
      76             :  * tcxFormantEnhancement()
      77             :  *
      78             :  *
      79             :  *-------------------------------------------------------------------*/
      80             : 
      81     1005675 : void tcxFormantEnhancement(
      82             :     float xn_buf[],
      83             :     const float *gainlpc,
      84             :     float spectrum[],
      85             :     const int16_t L_frame )
      86             : {
      87     1005675 :     int16_t k, i, j, l = 0;
      88             :     float fac, step;
      89             : 
      90     1005675 :     k = L_frame / FDNS_NPTS;
      91             : 
      92             :     /* Formant enhancement via square root of the LPC gains */
      93     1005675 :     xn_buf[0] = (float) sqrt( gainlpc[0] );
      94     1005675 :     xn_buf[1] = (float) sqrt( gainlpc[1] );
      95     1005675 :     fac = 1.0f / min( xn_buf[0], xn_buf[1] );
      96             : 
      97    63357525 :     for ( i = 1; i < FDNS_NPTS - 1; i++ )
      98             :     {
      99    62351850 :         xn_buf[i + 1] = (float) sqrt( gainlpc[i + 1] );
     100             : 
     101    62351850 :         if ( ( xn_buf[i - 1] <= xn_buf[i] ) && ( xn_buf[i + 1] <= xn_buf[i] ) )
     102             :         {
     103     3070321 :             step = max( xn_buf[i - 1], xn_buf[i + 1] );
     104     3070321 :             step = ( 1.0f / step - fac ) / (float) ( i - l );
     105     3070321 :             xn_buf[l] = 1.0f;
     106     3070321 :             fac += step;
     107    45467804 :             for ( j = l + 1; j < i; j++ )
     108             :             {
     109    42397483 :                 xn_buf[j] = min( 1.0f, xn_buf[j] * fac );
     110    42397483 :                 fac += step;
     111             :             }
     112     3070321 :             l = i;
     113             :         }
     114             :     }
     115             : 
     116             :     /* i = hTcxCfg->fdns_npts - 1; Completing changes to gains */
     117     1005675 :     step = min( xn_buf[i - 1], xn_buf[i] );
     118     1005675 :     step = ( 1.0f / step - fac ) / (float) ( i - l );
     119     1005675 :     xn_buf[l] = 1.0f;
     120     1005675 :     fac += step;
     121    17889721 :     for ( j = l + 1; j < i; j++ )
     122             :     {
     123    16884046 :         xn_buf[j] = min( 1.0f, xn_buf[j] * fac );
     124    16884046 :         fac += step;
     125             :     }
     126     1005675 :     xn_buf[i] = 1.0f;
     127             : 
     128             :     /* Application of changed gains onto decoded MDCT lines */
     129    65368875 :     for ( i = j = 0; i < L_frame; j++ )
     130             :     {
     131   325499392 :         for ( l = 0; l < k; i++, l++ )
     132             :         {
     133   261136192 :             spectrum[i] *= xn_buf[j];
     134             :         }
     135             :     }
     136             : 
     137     1005675 :     return;
     138             : }
     139             : 
     140             : /*-------------------------------------------------------------------*
     141             :  * tcxInvertWindowGrouping()
     142             :  *
     143             :  *
     144             :  *-------------------------------------------------------------------*/
     145             : 
     146       26582 : void tcxInvertWindowGrouping(
     147             :     TCX_CONFIG_HANDLE hTcxCfg,
     148             :     float xn_buf[],
     149             :     float spectrum[],
     150             :     const int16_t L_frame,
     151             :     const int16_t fUseTns,
     152             :     const int16_t last_core,
     153             :     const int16_t index,
     154             :     const int16_t frame_cnt,
     155             :     const int16_t bfi )
     156             : {
     157             :     int16_t i, w, t_integer;
     158             :     int16_t L_win, L_spec;
     159             : 
     160       26582 :     if ( frame_cnt && !bfi && last_core != ACELP_CORE )
     161             :     {
     162             :         /* fix sub-window overlap */
     163       13291 :         hTcxCfg->tcx_last_overlap_mode = hTcxCfg->tcx_curr_overlap_mode;
     164             :     }
     165             : 
     166       26582 :     if ( ( ( !bfi ) && ( ( hTcxCfg->tcx_last_overlap_mode != FULL_OVERLAP ) ||
     167       19205 :                          ( ( hTcxCfg->tcx_curr_overlap_mode == FULL_OVERLAP ) && ( frame_cnt == 0 ) && ( index == 0 ) ) ) ) ||
     168           0 :          ( ( bfi ) && ( ( hTcxCfg->tcx_last_overlap_mode != FULL_OVERLAP ) &&
     169           0 :                         !( hTcxCfg->tcx_curr_overlap_mode == FULL_OVERLAP ) ) ) )
     170             :     {
     171             : 
     172             :         /* ungroup sub-windows: deinterleave MDCT bins into separate windows */
     173       41331 :         for ( t_integer = w = 0; w < 2; w++ )
     174             :         {
     175     6193794 :             for ( i = w; i < L_frame; i += 2 )
     176             :             {
     177     6166240 :                 xn_buf[t_integer++] = spectrum[i];
     178             :             }
     179             :         }
     180             : 
     181       13777 :         mvr2r( xn_buf, spectrum, L_frame );
     182             : 
     183       13777 :         if ( hTcxCfg->fIsTNSAllowed && !bfi && fUseTns )
     184             :         {
     185       11025 :             L_win = L_frame >> 1;
     186       11025 :             L_spec = hTcxCfg->tnsConfig[0][0].iFilterBorders[0];
     187             : 
     188             :             /* rearrange LF sub-window lines prior to TNS synthesis filtering */
     189       11025 :             if ( L_spec < L_frame )
     190             :             {
     191        8981 :                 mvr2r( spectrum + 8, spectrum + 16, L_spec / 2 - 8 );
     192        8981 :                 mvr2r( spectrum + L_frame / 2, spectrum + 8, 8 );
     193        8981 :                 mvr2r( spectrum + L_frame / 2 + 8, spectrum + L_spec / 2 + 8, L_spec / 2 - 8 );
     194             :             }
     195             :             else
     196             :             {
     197        2044 :                 mvr2r( spectrum + 8, xn_buf, L_win );
     198        2044 :                 mvr2r( xn_buf, spectrum + 16, L_win - 8 );
     199        2044 :                 mvr2r( xn_buf + L_win - 8, spectrum + 8, 8 );
     200             :             }
     201             :         }
     202             :     }
     203             : 
     204       26582 :     return;
     205             : }
     206             : 
     207             : 
     208             : /*-------------------------------------------------------------------*
     209             :  * tcx5SpectrumInterleaving()
     210             :  *
     211             :  *
     212             :  *-------------------------------------------------------------------*/
     213             : 
     214     1618303 : void tcx5SpectrumInterleaving(
     215             :     const int16_t tcx5Size,
     216             :     float *spectrum )
     217             : {
     218             :     int16_t i;
     219             :     float interleaveBuf[N_TCX10_MAX];
     220             : 
     221     1618303 :     set_f( interleaveBuf, 0.0f, N_TCX10_MAX );
     222             : 
     223             :     /* group sub-windows: interleave bins according to their frequencies */
     224   325887503 :     for ( i = 0; i < tcx5Size; i++ )
     225             :     {
     226   324269200 :         interleaveBuf[2 * i] = spectrum[i];
     227   324269200 :         interleaveBuf[2 * i + 1] = spectrum[tcx5Size + i];
     228             :     }
     229             : 
     230     1618303 :     mvr2r( interleaveBuf, spectrum, 2 * tcx5Size );
     231             : 
     232     1618303 :     return;
     233             : }
     234             : 
     235             : 
     236             : /*-------------------------------------------------------------------*
     237             :  * tcx5SpectrumDeinterleaving()
     238             :  *
     239             :  *
     240             :  *-------------------------------------------------------------------*/
     241             : 
     242     1789870 : void tcx5SpectrumDeinterleaving(
     243             :     const int16_t tcx5Size,
     244             :     float *spectrum )
     245             : {
     246             :     int16_t i;
     247             :     float interleaveBuf[N_TCX10_MAX];
     248             : 
     249     1789870 :     set_f( interleaveBuf, 0.0f, N_TCX10_MAX );
     250             : 
     251             :     /* ungroup sub-windows: interleave bins according to their frequencies */
     252   342948430 :     for ( i = 0; i < tcx5Size; i++ )
     253             :     {
     254   341158560 :         interleaveBuf[i] = spectrum[2 * i];
     255   341158560 :         interleaveBuf[tcx5Size + i] = spectrum[2 * i + 1];
     256             :     }
     257             : 
     258     1789870 :     mvr2r( interleaveBuf, spectrum, 2 * tcx5Size );
     259             : 
     260     1789870 :     return;
     261             : }
     262             : 
     263             : 
     264             : /*-------------------------------------------------------------------*
     265             :  * tcx5TnsGrouping()
     266             :  *
     267             :  *
     268             :  *-------------------------------------------------------------------*/
     269             : 
     270      746800 : void tcx5TnsGrouping(
     271             :     const int16_t L_frame, /* i  : frame length (TCX5)                                 */
     272             :     const int16_t L_spec,  /* i  : coded spec length (TCX5, derived from filter borders*/
     273             :     float *spectrum )
     274             : {
     275             :     /* rearrange LF sub-window lines prior to TNS synthesis filtering */
     276      746800 :     if ( L_spec < L_frame )
     277             :     {
     278      335924 :         mvr2r( spectrum + 8, spectrum + 16, L_spec - 8 );
     279      335924 :         mvr2r( spectrum + L_frame, spectrum + 8, 8 );
     280      335924 :         mvr2r( spectrum + L_frame + 8, spectrum + L_spec + 8, L_spec - 8 );
     281             :     }
     282             :     else
     283             :     {
     284             :         float buff[8]; /* Buffer for the rearrangement of LF TCX5 */
     285      410876 :         mvr2r( spectrum + L_spec, buff, 8 );
     286      410876 :         mvr2r( spectrum + 8, spectrum + 16, L_spec - 8 );
     287      410876 :         mvr2r( buff, spectrum + 8, 8 );
     288             :     }
     289             : 
     290      746800 :     return;
     291             : }
     292             : 
     293             : 
     294             : /*-------------------------------------------------------------------*
     295             :  * tcx5TnsUngrouping()
     296             :  *
     297             :  *
     298             :  *-------------------------------------------------------------------*/
     299             : 
     300      746800 : void tcx5TnsUngrouping(
     301             :     const int16_t L_frame, /* i  : frame length (TCX5)                                 */
     302             :     const int16_t L_spec,  /* i  : coded spec length (TCX5, derived from filter borders*/
     303             :     float *spectrum,
     304             :     const int16_t enc_dec /* i  : 0: encoder, else decoder                            */
     305             : )
     306             : {
     307             :     /* undo rearrangement of LF sub-window lines prior to TNS analysis */
     308      746800 :     if ( L_spec < L_frame )
     309             :     {
     310      361745 :         mvr2r( spectrum + L_spec + 8, spectrum + L_frame + 8, L_spec - 8 );
     311      361745 :         mvr2r( spectrum + 8, spectrum + L_frame, 8 );
     312      361745 :         mvr2r( spectrum + 16, spectrum + 8, L_spec - 8 );
     313      361745 :         set_zero( spectrum + L_spec, L_frame - L_spec );
     314      361745 :         set_zero( spectrum + L_frame + L_spec, L_frame - L_spec );
     315             :     }
     316             :     else
     317             :     {
     318             :         float buff[8]; /* Buffer for the rearrangement of LF TCX5 */
     319             : 
     320      385055 :         mvr2r( spectrum + 8, buff, 8 );
     321             : 
     322      385055 :         if ( enc_dec == ENC )
     323             :         {
     324       99036 :             mvr2r( spectrum + 16, spectrum + 8, L_frame - 8 );
     325       99036 :             mvr2r( buff, spectrum + L_frame, 8 );
     326             :         }
     327             :         else
     328             :         {
     329      286019 :             mvr2r( spectrum + 16, spectrum + 8, L_spec - 8 );
     330      286019 :             mvr2r( buff, spectrum + L_spec, 8 );
     331             :         }
     332             :     }
     333             : 
     334      746800 :     return;
     335             : }

Generated by: LCOV version 1.14