LCOV - code coverage report
Current view: top level - lib_com - core_com_config.c (source / functions) Hit Total Coverage
Test: Coverage on main -- short test vectors @ 1ecb9137d23f3dad766c8f6f3eb1e829e795f071 Lines: 319 332 96.1 %
Date: 2025-10-29 06:44:26 Functions: 19 19 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 <assert.h>
      38             : #include <stdint.h>
      39             : #include "options.h"
      40             : #ifdef DEBUGGING
      41             : #include "debug.h"
      42             : #endif
      43             : #include "rom_com.h"
      44             : #include "prot.h"
      45             : #include "wmc_auto.h"
      46             : #include "ivas_prot.h"
      47             : 
      48             : 
      49             : /*-------------------------------------------------------------------*
      50             :  * is_EVS_bitrate()
      51             :  *
      52             :  * check if the bitrate is EVS supported active bitrate
      53             :  *-------------------------------------------------------------------*/
      54             : 
      55             : /*! r: flag indicating a valid bitrate */
      56        2695 : int16_t is_EVS_bitrate(
      57             :     const int32_t ivas_total_brate, /* i  : IVAS total bitrate  */
      58             :     int16_t *Opt_AMR_WB             /* i  : AMR-WB IO flag      */
      59             : )
      60             : {
      61             :     int16_t j;
      62             : 
      63        2695 :     j = 0;
      64       18661 :     while ( j < SIZE_BRATE_TBL && ivas_total_brate != brate_tbl[j] )
      65             :     {
      66       15966 :         j++;
      67             :     }
      68             : 
      69             :     /* AMR-WB IO mode/EVS primary mode determination */
      70        2695 :     if ( j >= SIZE_BRATE_TBL )
      71             :     {
      72         284 :         switch ( ivas_total_brate )
      73             :         {
      74         284 :             case ACELP_6k60:
      75             :             case ACELP_8k85:
      76             :             case ACELP_12k65:
      77             :             case ACELP_14k25:
      78             :             case ACELP_15k85:
      79             :             case ACELP_18k25:
      80             :             case ACELP_19k85:
      81             :             case ACELP_23k05:
      82             :             case ACELP_23k85:
      83         284 :                 break;
      84           0 :             default:
      85             :             {
      86           0 :                 return 0;
      87             :             }
      88             :             break;
      89             :         }
      90             : 
      91         284 :         *Opt_AMR_WB = 1;
      92             :     }
      93             :     else
      94             :     {
      95        2411 :         *Opt_AMR_WB = 0;
      96             :     }
      97             : 
      98        2695 :     return 1;
      99             : }
     100             : 
     101             : 
     102             : /*-------------------------------------------------------------------*
     103             :  * get_codec_mode()
     104             :  *
     105             :  * Get codec_mode (MODE1 or MODE2)
     106             :  *-------------------------------------------------------------------*/
     107             : 
     108             : /*! r: codec mode */
     109       35381 : int16_t get_codec_mode(
     110             :     const int32_t total_brate /* i  : total bitrate   */
     111             : )
     112             : {
     113       35381 :     int16_t codec_mode = -1;
     114             : 
     115       35381 :     switch ( total_brate )
     116             :     {
     117           0 :         case 5900:
     118           0 :             codec_mode = MODE1;
     119           0 :             break;
     120        1251 :         case 7200:
     121        1251 :             codec_mode = MODE1;
     122        1251 :             break;
     123         765 :         case 8000:
     124         765 :             codec_mode = MODE1;
     125         765 :             break;
     126        1353 :         case 9600:
     127        1353 :             codec_mode = MODE2;
     128        1353 :             break;
     129       10541 :         case 13200:
     130       10541 :             codec_mode = MODE1;
     131       10541 :             break;
     132        1712 :         case 16400:
     133        1712 :             codec_mode = MODE2;
     134        1712 :             break;
     135        8058 :         case 24400:
     136        8058 :             codec_mode = MODE2;
     137        8058 :             break;
     138        2000 :         case 32000:
     139        2000 :             codec_mode = MODE1;
     140        2000 :             break;
     141        1694 :         case 48000:
     142        1694 :             codec_mode = MODE2;
     143        1694 :             break;
     144        4847 :         case 64000:
     145        4847 :             codec_mode = MODE1;
     146        4847 :             break;
     147        1512 :         case 96000:
     148        1512 :             codec_mode = MODE2;
     149        1512 :             break;
     150        1570 :         case 128000:
     151        1570 :             codec_mode = MODE2;
     152        1570 :             break;
     153             :     }
     154             : 
     155       35381 :     return codec_mode;
     156             : }
     157             : 
     158             : 
     159             : /*-------------------------------------------------------------------*
     160             :  * getTcxonly()
     161             :  *
     162             :  *
     163             :  *-------------------------------------------------------------------*/
     164             : 
     165     4607762 : int16_t getTcxonly(
     166             :     const int16_t element_mode, /* i  : IVAS element mode                   */
     167             :     const int32_t total_brate,  /* i  : total bitrate                       */
     168             :     const int16_t MCT_flag,     /* i  : hMCT handle allocated (1) or not (0)*/
     169             :     const int16_t is_ism_format /* i  : flag indicating ISM format          */
     170             : )
     171             : {
     172     4607762 :     int16_t tcxonly = 0;
     173             : 
     174     4607762 :     switch ( element_mode )
     175             :     {
     176        2123 :         case EVS_MONO:
     177        2123 :             if ( total_brate > ACELP_32k )
     178             :             {
     179         311 :                 tcxonly = 1;
     180             :             }
     181        2123 :             break;
     182     3844492 :         case IVAS_SCE:
     183     3844492 :             if ( is_ism_format )
     184             :             {
     185     3123394 :                 if ( total_brate > MAX_ACELP_BRATE_ISM )
     186             :                 {
     187     1580888 :                     tcxonly = 1;
     188             :                 }
     189             :             }
     190             :             else
     191             :             {
     192      721098 :                 if ( total_brate > MAX_ACELP_BRATE )
     193             :                 {
     194      105410 :                     tcxonly = 1;
     195             :                 }
     196             :             }
     197     3844492 :             break;
     198             : 
     199       36236 :         case IVAS_CPE_DFT:
     200             :         case IVAS_CPE_TD:
     201       36236 :             if ( total_brate > MAX_ACELP_BRATE )
     202             :             {
     203           0 :                 tcxonly = 1;
     204             :             }
     205       36236 :             break;
     206      663158 :         case IVAS_CPE_MDCT:
     207      663158 :             if ( total_brate >= ( MCT_flag ? IVAS_32k : IVAS_48k ) )
     208             :             {
     209      338288 :                 tcxonly = 1;
     210             :             }
     211      663158 :             break;
     212             :     }
     213             : 
     214     4607762 :     return tcxonly;
     215             : }
     216             : 
     217             : 
     218             : /*-------------------------------------------------------------------*
     219             :  * getCtxHm()
     220             :  *
     221             :  *
     222             :  *-------------------------------------------------------------------*/
     223             : 
     224    18372327 : int16_t getCtxHm(
     225             :     const int16_t element_mode, /* i  : IVAS element mode           */
     226             :     const int32_t total_brate,  /* i  : total bitrate               */
     227             :     const int16_t rf_flag       /* i  : flag to signal the RF mode  */
     228             : )
     229             : {
     230    18372327 :     int16_t ctx_hm = 0;
     231             : 
     232    18372327 :     if ( ( ( element_mode <= IVAS_SCE && total_brate > LPC_SHAPED_ARI_MAX_RATE ) || ( element_mode > IVAS_SCE && total_brate > LPC_SHAPED_ARI_MAX_RATE_CPE ) ) && !rf_flag && ( ( total_brate <= IVAS_64k && element_mode != IVAS_CPE_DFT ) || ( total_brate < 24400 && element_mode == IVAS_CPE_DFT ) ) && element_mode != IVAS_CPE_MDCT )
     233             :     {
     234     3277097 :         ctx_hm = 1;
     235             :     }
     236             : 
     237    18372327 :     return ctx_hm;
     238             : }
     239             : 
     240             : /*-------------------------------------------------------------------*
     241             :  * getResq()
     242             :  *
     243             :  *
     244             :  *-------------------------------------------------------------------*/
     245             : 
     246    18372327 : int16_t getResq(
     247             :     const int32_t total_brate /* i  : total bitrate               */
     248             : )
     249             : {
     250    18372327 :     int16_t resq = 0;
     251             : 
     252    18372327 :     if ( total_brate <= IVAS_64k )
     253             :     {
     254     7411263 :         resq = 1;
     255             :     }
     256             : 
     257    18372327 :     return resq;
     258             : }
     259             : 
     260             : /*-------------------------------------------------------------------*
     261             :  * getTnsAllowed()
     262             :  *
     263             :  *
     264             :  *-------------------------------------------------------------------*/
     265             : 
     266    19096955 : int16_t getTnsAllowed(
     267             :     const int32_t total_brate, /* i  : total bitrate               */
     268             :     const int16_t igf,         /* i  : flag indicating IGF activity*/
     269             :     const int16_t element_mode /* i  : IVAS element mode           */
     270             : )
     271             : {
     272    19096955 :     int16_t tnsAllowed = 0;
     273             : 
     274    19096955 :     if ( igf )
     275             :     {
     276    10123279 :         if ( total_brate > HQ_16k40 || ( ( total_brate > HQ_13k20 ) && element_mode == IVAS_CPE_DFT ) )
     277             :         {
     278     9085757 :             tnsAllowed = 1;
     279             :         }
     280             :     }
     281     8973676 :     else if ( total_brate > HQ_32k )
     282             :     {
     283     8081688 :         tnsAllowed = 1;
     284             :     }
     285             : 
     286    19096955 :     return tnsAllowed;
     287             : }
     288             : 
     289             : /*-------------------------------------------------------------------*
     290             :  * getRestrictedMode()
     291             :  *
     292             :  *
     293             :  *-------------------------------------------------------------------*/
     294             : 
     295       93494 : int16_t getRestrictedMode(
     296             :     const int16_t element_mode, /* i  : IVAS element mode               */
     297             :     const int32_t total_brate,  /* i  : total bitrate                   */
     298             :     const int16_t Opt_AMR_WB    /* i  : flag indicating AMR-WB IO mode  */
     299             : )
     300             : {
     301       93494 :     int16_t restrictedMode = 3;
     302             : 
     303       93494 :     if ( !Opt_AMR_WB && ( total_brate > HQ_32k && element_mode != IVAS_SCE ) )
     304             :     {
     305       48345 :         restrictedMode = 6;
     306             :     }
     307       45149 :     else if ( !Opt_AMR_WB && ( total_brate > HQ_48k && element_mode == IVAS_SCE ) )
     308             :     {
     309        3329 :         restrictedMode = 6;
     310             :     }
     311       41820 :     else if ( Opt_AMR_WB )
     312             :     {
     313          44 :         restrictedMode = 1;
     314             :     }
     315             : 
     316       93494 :     return restrictedMode;
     317             : }
     318             : 
     319             : /*-------------------------------------------------------------------*
     320             :  * getMdctWindowLength()
     321             :  *
     322             :  *
     323             :  *-------------------------------------------------------------------*/
     324             : 
     325      608492 : int16_t getMdctWindowLength(
     326             :     const int16_t fscale )
     327             : {
     328             :     int16_t mdctWindowLength;
     329             : 
     330      608492 :     mdctWindowLength = ( L_LOOK_12k8 * fscale ) / FSCALE_DENOM;
     331             : 
     332      608492 :     return mdctWindowLength;
     333             : }
     334             : 
     335             : /*-------------------------------------------------------------------*
     336             :  * sr2fscale()
     337             :  *
     338             :  *
     339             :  *-------------------------------------------------------------------*/
     340             : 
     341    36076830 : int16_t sr2fscale(
     342             :     const int32_t sr_core /* i  : internal sampling rate    */
     343             : )
     344             : {
     345             : 
     346    36076830 :     return (int16_t) ( ( FSCALE_DENOM * sr_core ) / 12800 );
     347             : }
     348             : 
     349             : /*-------------------------------------------------------------------*
     350             :  * getCoreSamplerateMode2()
     351             :  *
     352             :  *
     353             :  *-------------------------------------------------------------------*/
     354             : 
     355    21705708 : int32_t getCoreSamplerateMode2(
     356             :     const int16_t element_mode,     /* i  : IVAS element mode              */
     357             :     const int32_t total_brate,      /* i  : total bitrate                  */
     358             :     const int16_t bwidth,           /* i  : audio bandwidth                */
     359             :     const int16_t flag_ACELP16k,    /* i  : ACELP@16kHz flag               */
     360             :     const int16_t rf_mode,          /* i  : flag to signal the RF mode     */
     361             :     const IVAS_FORMAT is_ism_format /* i  : flag indicating ISM format     */
     362             : )
     363             : {
     364    21705708 :     int32_t sr_core = 0;
     365             : 
     366    21705708 :     if ( bwidth == NB )
     367             :     {
     368      314917 :         sr_core = INT_FS_12k8;
     369             :     }
     370    21390791 :     else if ( element_mode == EVS_MONO && ( ( bwidth == WB && total_brate < ACELP_13k20 ) || ( bwidth == SWB && total_brate <= ACELP_13k20 ) || ( rf_mode == 1 ) ) )
     371             :     {
     372        1110 :         sr_core = INT_FS_12k8;
     373             :     }
     374    21389681 :     else if ( element_mode > EVS_MONO && flag_ACELP16k == 0 )
     375             :     {
     376     1043320 :         sr_core = INT_FS_12k8;
     377             :     }
     378    20346361 :     else if ( bwidth == WB || ( bwidth == SWB && total_brate <= ACELP_32k ) || ( bwidth == FB && total_brate <= ACELP_32k ) )
     379             :     {
     380     5564079 :         sr_core = INT_FS_16k;
     381             :     }
     382    14782282 :     else if ( ( bwidth == SWB || bwidth == FB ) && total_brate <= MAX_ACELP_BRATE && element_mode == IVAS_SCE && !is_ism_format )
     383             :     {
     384        8074 :         sr_core = INT_FS_16k;
     385             :     }
     386    14774208 :     else if ( ( bwidth == SWB || bwidth == FB ) && total_brate <= MAX_ACELP_BRATE_ISM && element_mode == IVAS_SCE && is_ism_format )
     387             :     {
     388      155998 :         sr_core = INT_FS_16k;
     389             :     }
     390    14618210 :     else if ( ( bwidth == SWB || bwidth == FB ) && total_brate <= MAX_ACELP_BRATE && element_mode == IVAS_SCE && is_ism_format )
     391             :     {
     392      321654 :         sr_core = 25600;
     393             :     }
     394    14296556 :     else if ( ( ( bwidth == SWB || bwidth == FB ) && element_mode == EVS_MONO && total_brate <= HQ_64k ) || ( element_mode > IVAS_SCE && ( ( bwidth == SWB && total_brate <= IVAS_96k ) || ( bwidth == FB && total_brate <= IVAS_96k ) ) ) )
     395             :     {
     396     5719333 :         sr_core = 25600;
     397             :     }
     398     8577223 :     else if ( bwidth == SWB || bwidth == FB )
     399             :     {
     400     8577223 :         sr_core = 32000;
     401             :     }
     402             : 
     403    21705708 :     return sr_core;
     404             : }
     405             : 
     406             : /*-------------------------------------------------------------------*
     407             :  * getTcxBandwidth()
     408             :  *
     409             :  *
     410             :  *-------------------------------------------------------------------*/
     411             : 
     412     1340170 : float getTcxBandwidth(
     413             :     const int16_t bwidth /* i  : audio bandwidth        */
     414             : )
     415             : {
     416     1340170 :     float tcxBandwidth = 0.5f;
     417             : 
     418     1340170 :     if ( bwidth == NB )
     419             :     {
     420      412116 :         tcxBandwidth = 0.3125f;
     421             :     }
     422             : 
     423     1340170 :     return tcxBandwidth;
     424             : }
     425             : 
     426             : /*-------------------------------------------------------------------*
     427             :  * getIgfPresent()
     428             :  *
     429             :  *
     430             :  *-------------------------------------------------------------------*/
     431             : 
     432    18337354 : int16_t getIgfPresent(
     433             :     const int16_t element_mode, /* i  : IVAS element mode           */
     434             :     const int32_t total_brate,  /* i  : total bitrate               */
     435             :     const int16_t bwidth,       /* i  : audio bandwidth             */
     436             :     const int16_t rf_mode       /* i  : flag to signal the RF mode  */
     437             : )
     438             : {
     439    18337354 :     int16_t igfPresent = 0;
     440             : 
     441             : 
     442    18337354 :     if ( bwidth == SWB )
     443             :     {
     444     5249342 :         switch ( element_mode )
     445             :         {
     446     1547274 :             case EVS_MONO:
     447             :             case IVAS_SCE:
     448     1547274 :                 if ( total_brate <= HQ_64k )
     449             :                 {
     450     1309168 :                     igfPresent = 1;
     451             :                 }
     452     1547274 :                 break;
     453      121376 :             case IVAS_CPE_DFT:
     454             :             case IVAS_CPE_TD:
     455      121376 :                 if ( total_brate <= HQ_48k )
     456             :                 {
     457      121376 :                     igfPresent = 1;
     458             :                 }
     459      121376 :                 break;
     460     3580692 :             case IVAS_CPE_MDCT:
     461     3580692 :                 if ( total_brate <= IVAS_96k )
     462             :                 {
     463     1745369 :                     igfPresent = 1;
     464             :                 }
     465     3580692 :                 break;
     466             :         }
     467     5249342 :     }
     468    13088012 :     else if ( bwidth == FB )
     469             :     {
     470     8974547 :         switch ( element_mode )
     471             :         {
     472     1735204 :             case EVS_MONO:
     473             :             case IVAS_SCE:
     474     1735204 :                 if ( total_brate <= IVAS_128k )
     475             :                 {
     476     1718190 :                     igfPresent = 1;
     477             :                 }
     478     1735204 :                 break;
     479       36358 :             case IVAS_CPE_DFT:
     480             :             case IVAS_CPE_TD:
     481       36358 :                 if ( total_brate <= IVAS_48k )
     482             :                 {
     483       36358 :                     igfPresent = 1;
     484             :                 }
     485       36358 :                 break;
     486     7202985 :             case IVAS_CPE_MDCT:
     487     7202985 :                 if ( total_brate <= IVAS_128k )
     488             :                 {
     489     4952749 :                     igfPresent = 1;
     490             :                 }
     491     7202985 :                 break;
     492             :         }
     493     8974547 :     }
     494     4113465 :     else if ( bwidth == WB )
     495             :     {
     496     4112796 :         switch ( element_mode )
     497             :         {
     498      960102 :             case EVS_MONO:
     499             :             case IVAS_SCE:
     500      960102 :                 if ( total_brate <= ACELP_9k60 )
     501             :                 {
     502       41875 :                     igfPresent = 1;
     503             :                 }
     504      960102 :                 break;
     505     3152694 :             case IVAS_CPE_DFT:
     506             :             case IVAS_CPE_TD:
     507             :             case IVAS_CPE_MDCT:
     508     3152694 :                 if ( total_brate <= ACELP_13k20 )
     509             :                 {
     510        8986 :                     igfPresent = 1;
     511             :                 }
     512     3152694 :                 break;
     513             :         }
     514         669 :     }
     515             : 
     516    18337354 :     if ( ( ( bwidth == WB ) || ( bwidth == SWB ) ) && ( rf_mode == 1 ) && ( total_brate == ACELP_13k20 ) )
     517             :     {
     518        1696 :         igfPresent = 1;
     519             :     }
     520             : 
     521    18337354 :     return igfPresent;
     522             : }
     523             : 
     524             : /*-------------------------------------------------------------------*
     525             :  * getCnaPresent()
     526             :  *
     527             :  *
     528             :  *-------------------------------------------------------------------*/
     529             : 
     530      274987 : int16_t getCnaPresent(
     531             :     const int16_t element_mode,  /* i  : element mode        */
     532             :     const int32_t element_brate, /* i  : element bitrate     */
     533             :     const int32_t total_brate,   /* i  : total bitrate       */
     534             :     const int16_t bwidth         /* i  : audio bandwidth     */
     535             : )
     536             : {
     537      274987 :     int16_t flag_cna = 0;
     538             : 
     539      274987 :     if ( element_mode == IVAS_CPE_DFT && element_brate <= CNA_MAX_BRATE_DFT_STEREO )
     540             :     {
     541       10671 :         flag_cna = 1;
     542             :     }
     543             :     else
     544             :     {
     545      264316 :         if ( bwidth == NB && total_brate <= ACELP_13k20 )
     546             :         {
     547          10 :             flag_cna = 1;
     548             :         }
     549             : 
     550      264316 :         if ( bwidth == WB && total_brate <= ACELP_13k20 )
     551             :         {
     552          60 :             flag_cna = 1;
     553             :         }
     554             : 
     555      264316 :         if ( bwidth == SWB && total_brate <= ACELP_13k20 )
     556             :         {
     557         157 :             flag_cna = 1;
     558             :         }
     559             :     }
     560             : 
     561      274987 :     return flag_cna;
     562             : }
     563             : 
     564             : /*-------------------------------------------------------------------*
     565             :  * getTcxLtp()
     566             :  *
     567             :  *
     568             :  *-------------------------------------------------------------------*/
     569             : 
     570    16491782 : int16_t getTcxLtp(
     571             :     const int32_t sr_core /* i  : internal sampling rate */
     572             : )
     573             : {
     574    16491782 :     int16_t tcxltp = 0;
     575             : 
     576    16491782 :     if ( sr_core <= 25600 )
     577             :     {
     578    10488142 :         tcxltp = 1;
     579             :     }
     580             : 
     581    16491782 :     return tcxltp;
     582             : }
     583             : 
     584             : /*-------------------------------------------------------------------*
     585             :  * initPitchLagParameters()
     586             :  *
     587             :  *
     588             :  *-------------------------------------------------------------------*/
     589             : 
     590      825946 : int16_t initPitchLagParameters(
     591             :     const int32_t sr_core,
     592             :     int16_t *pit_min,
     593             :     int16_t *pit_fr1,
     594             :     int16_t *pit_fr1b,
     595             :     int16_t *pit_fr2,
     596             :     int16_t *pit_max )
     597             : {
     598             :     int16_t pit_res_max;
     599             : 
     600      825946 :     if ( sr_core == INT_FS_12k8 )
     601             :     {
     602      690952 :         *pit_min = PIT_MIN_12k8;
     603      690952 :         *pit_max = PIT_MAX_12k8;
     604      690952 :         *pit_fr2 = PIT_FR2_12k8;
     605      690952 :         *pit_fr1 = PIT_FR1_12k8;
     606      690952 :         *pit_fr1b = PIT_FR1_8b_12k8;
     607      690952 :         pit_res_max = 4;
     608             :     }
     609      134994 :     else if ( sr_core == INT_FS_16k )
     610             :     {
     611      103426 :         *pit_min = PIT_MIN_16k;
     612      103426 :         *pit_max = PIT16k_MAX;
     613      103426 :         *pit_fr2 = PIT_FR2_16k;
     614      103426 :         *pit_fr1 = PIT_FR1_16k;
     615      103426 :         *pit_fr1b = PIT_FR1_8b_16k;
     616      103426 :         pit_res_max = 6;
     617             :     }
     618       31568 :     else if ( sr_core == 25600 )
     619             :     {
     620        7202 :         *pit_min = PIT_MIN_25k6;
     621        7202 :         *pit_max = PIT_MAX_25k6;
     622        7202 :         *pit_fr2 = PIT_FR2_25k6;
     623        7202 :         *pit_fr1 = PIT_FR1_25k6;
     624        7202 :         *pit_fr1b = PIT_FR1_8b_25k6;
     625        7202 :         pit_res_max = 4;
     626             :     }
     627             :     else /* sr_core==32000 */
     628             :     {
     629       24366 :         *pit_min = PIT_MIN_32k;
     630       24366 :         *pit_max = PIT_MAX_32k;
     631       24366 :         *pit_fr2 = PIT_FR2_32k;
     632       24366 :         *pit_fr1 = PIT_FR1_32k;
     633       24366 :         *pit_fr1b = PIT_FR1_8b_32k;
     634       24366 :         pit_res_max = 6;
     635             :     }
     636             : 
     637      825946 :     return pit_res_max;
     638             : }
     639             : 
     640             : /*-------------------------------------------------------------------*
     641             :  * getNumTcxCodedLines()
     642             :  *
     643             :  *
     644             :  *-------------------------------------------------------------------*/
     645             : 
     646     6366078 : int16_t getNumTcxCodedLines(
     647             :     const int16_t bwidth /* i  : audio bandwidth        */
     648             : )
     649             : {
     650             :     int16_t tcx_coded_lines;
     651             : 
     652     6366078 :     switch ( bwidth )
     653             :     {
     654       97245 :         case NB:
     655       97245 :             tcx_coded_lines = 160;
     656       97245 :             break;
     657     1552162 :         case WB:
     658     1552162 :             tcx_coded_lines = 320;
     659     1552162 :             break;
     660     1902873 :         case SWB:
     661     1902873 :             tcx_coded_lines = 640;
     662     1902873 :             break;
     663     2813798 :         case FB:
     664     2813798 :             tcx_coded_lines = 960;
     665     2813798 :             break;
     666           0 :         default:
     667           0 :             tcx_coded_lines = 0;
     668           0 :             break;
     669             :     }
     670             : 
     671     6366078 :     return tcx_coded_lines;
     672             : }
     673             : 
     674             : /*-------------------------------------------------------------------*
     675             :  * getTcxLpcShapedAri()
     676             :  *
     677             :  *
     678             :  *-------------------------------------------------------------------*/
     679             : 
     680    18583209 : int16_t getTcxLpcShapedAri(
     681             :     const int32_t total_brate, /* i  : total bitrate                */
     682             :     const int16_t rf_mode,     /* i  :  flag to signal the RF mode  */
     683             :     const int16_t element_mode /* i  : IVAS element mode            */
     684             : )
     685             : {
     686    18583209 :     int16_t tcx_lpc_shaped_ari = 0;
     687             : 
     688    18583209 :     if ( ( ( element_mode <= IVAS_SCE && total_brate <= LPC_SHAPED_ARI_MAX_RATE ) || ( element_mode > IVAS_SCE && total_brate <= LPC_SHAPED_ARI_MAX_RATE_CPE ) ) || rf_mode )
     689             :     {
     690      545976 :         tcx_lpc_shaped_ari = 1;
     691             :     }
     692             : 
     693    18583209 :     return tcx_lpc_shaped_ari;
     694             : }
     695             : 
     696             : 
     697             : /*-----------------------------------------------------------------------*
     698             :  * init_tcx_window_cfg()
     699             :  *
     700             :  * Initialization of TCX windows
     701             :  *-----------------------------------------------------------------------*/
     702             : 
     703      608492 : void init_tcx_window_cfg(
     704             :     TCX_CONFIG_HANDLE hTcxCfg,          /* i/o: TCX Config handle        */
     705             :     const int32_t sr_core,              /* i  : SR core                  */
     706             :     const int32_t input_Fs,             /* i  : input/output SR          */
     707             :     const int16_t L_frame,              /* i  : L_frame at sr_core       */
     708             :     const int16_t L_frameTCX,           /* i  : L_frame at i/o SR        */
     709             :     const int16_t encoderLookahead_enc, /* i  : encoder LA at sr_core    */
     710             :     const int16_t encoderLookahead_FB,  /* i  : encoder LA at i/o SR     */
     711             :     const int16_t mdctWindowLength,     /* i  : window length at sr_core */
     712             :     const int16_t mdctWindowLengthFB,   /* i  : window length at i/o SR  */
     713             :     const int16_t element_mode          /* i  : mode of CPE/SCE          */
     714             : )
     715             : {
     716             :     /* Symmetric window = sine LD window*/
     717      608492 :     hTcxCfg->tcx_mdct_window_delay = mdctWindowLength;
     718      608492 :     hTcxCfg->tcx_mdct_window_delayFB = mdctWindowLengthFB;
     719      608492 :     hTcxCfg->tcx_mdct_window_length = mdctWindowLength;
     720      608492 :     hTcxCfg->tcx_mdct_window_lengthFB = mdctWindowLengthFB;
     721             : 
     722      608492 :     mdct_window_sine( hTcxCfg->tcx_mdct_window, sr_core, hTcxCfg->tcx_mdct_window_length, FULL_OVERLAP, element_mode );
     723      608492 :     if ( hTcxCfg->tcx_mdct_window_length == hTcxCfg->tcx_mdct_window_lengthFB )
     724             :     {
     725      163477 :         mvr2r( hTcxCfg->tcx_mdct_window, hTcxCfg->tcx_mdct_windowFB, hTcxCfg->tcx_mdct_window_length );
     726             :     }
     727             :     else
     728             :     {
     729      445015 :         mdct_window_sine( hTcxCfg->tcx_mdct_windowFB, input_Fs, hTcxCfg->tcx_mdct_window_lengthFB, FULL_OVERLAP, element_mode );
     730             :     }
     731             : 
     732             :     /*ALDO windows for MODE2*/
     733      608492 :     mdct_window_aldo( hTcxCfg->tcx_aldo_window_1, hTcxCfg->tcx_aldo_window_2, L_frame );
     734      608492 :     mdct_window_aldo( hTcxCfg->tcx_aldo_window_1_FB, hTcxCfg->tcx_aldo_window_2_FB, NS2SA( input_Fs, FRAME_SIZE_NS ) );
     735      608492 :     hTcxCfg->tcx_aldo_window_1_trunc = hTcxCfg->tcx_aldo_window_1 + NS2SA( sr_core, N_ZERO_MDCT_NS );
     736      608492 :     hTcxCfg->tcx_aldo_window_1_FB_trunc = hTcxCfg->tcx_aldo_window_1_FB + NS2SA( input_Fs, N_ZERO_MDCT_NS );
     737             : 
     738             :     /*1.25ms transition window for ACELP->TCX*/
     739      608492 :     hTcxCfg->tcx_mdct_window_trans_length = NS2SA( sr_core, ACELP_TCX_TRANS_NS );
     740      608492 :     mdct_window_sine( hTcxCfg->tcx_mdct_window_trans, sr_core, hTcxCfg->tcx_mdct_window_trans_length, TRANSITION_OVERLAP, element_mode );
     741      608492 :     hTcxCfg->tcx_mdct_window_trans_lengthFB = NS2SA( input_Fs, ACELP_TCX_TRANS_NS );
     742      608492 :     if ( hTcxCfg->tcx_mdct_window_trans_length == hTcxCfg->tcx_mdct_window_trans_lengthFB )
     743             :     {
     744      163477 :         mvr2r( hTcxCfg->tcx_mdct_window_trans, hTcxCfg->tcx_mdct_window_transFB, hTcxCfg->tcx_mdct_window_trans_length );
     745             :     }
     746             :     else
     747             :     {
     748      445015 :         mdct_window_sine( hTcxCfg->tcx_mdct_window_transFB, input_Fs, hTcxCfg->tcx_mdct_window_trans_lengthFB, TRANSITION_OVERLAP, element_mode );
     749             :     }
     750             : 
     751             :     /*Mid-OLA*/
     752             :     /*compute minimum length for "half" window: lookahead - 5ms. It must be also multiple of 2*/
     753      608492 :     hTcxCfg->tcx_mdct_window_half_length = 2 * ( ( encoderLookahead_enc - (int16_t) ( 0.005f * sr_core + 0.5f ) ) >> 1 );
     754      608492 :     hTcxCfg->tcx_mdct_window_half_lengthFB = 2 * ( ( encoderLookahead_FB - (int16_t) ( 0.005f * input_Fs + 0.5f ) ) >> 1 );
     755      608492 :     assert( ( hTcxCfg->tcx_mdct_window_half_length > 16 ) && "Half window can not be large enough!" );
     756             : 
     757      608492 :     mdct_window_sine( hTcxCfg->tcx_mdct_window_half, sr_core, hTcxCfg->tcx_mdct_window_half_length, HALF_OVERLAP, element_mode );
     758      608492 :     if ( hTcxCfg->tcx_mdct_window_half_length == hTcxCfg->tcx_mdct_window_half_lengthFB )
     759             :     {
     760      163477 :         mvr2r( hTcxCfg->tcx_mdct_window_half, hTcxCfg->tcx_mdct_window_halfFB, hTcxCfg->tcx_mdct_window_half_length );
     761             :     }
     762             :     else
     763             :     {
     764      445015 :         mdct_window_sine( hTcxCfg->tcx_mdct_window_halfFB, input_Fs, hTcxCfg->tcx_mdct_window_half_lengthFB, HALF_OVERLAP, element_mode );
     765             :     }
     766             : 
     767             :     /* minimum overlap 1.25 ms */
     768      608492 :     hTcxCfg->tcx_mdct_window_min_length = (int16_t) ( sr_core * INV_CLDFB_BANDWIDTH );
     769      608492 :     hTcxCfg->tcx_mdct_window_min_lengthFB = (int16_t) ( input_Fs * INV_CLDFB_BANDWIDTH );
     770             :     /* save complexity by copying the small windows if they have the same length */
     771      608492 :     if ( hTcxCfg->tcx_mdct_window_min_length == hTcxCfg->tcx_mdct_window_trans_length )
     772             :     {
     773      608492 :         mvr2r( hTcxCfg->tcx_mdct_window_trans, hTcxCfg->tcx_mdct_window_minimum, hTcxCfg->tcx_mdct_window_min_length );
     774             :     }
     775             :     else
     776             :     {
     777           0 :         mdct_window_sine( hTcxCfg->tcx_mdct_window_minimum, sr_core, hTcxCfg->tcx_mdct_window_min_length, MIN_OVERLAP, element_mode );
     778             :     }
     779             : 
     780      608492 :     if ( hTcxCfg->tcx_mdct_window_min_lengthFB == hTcxCfg->tcx_mdct_window_trans_lengthFB )
     781             :     {
     782      608492 :         mvr2r( hTcxCfg->tcx_mdct_window_transFB, hTcxCfg->tcx_mdct_window_minimumFB, hTcxCfg->tcx_mdct_window_min_lengthFB );
     783             :     }
     784           0 :     else if ( hTcxCfg->tcx_mdct_window_min_length == hTcxCfg->tcx_mdct_window_min_lengthFB )
     785             :     {
     786           0 :         mvr2r( hTcxCfg->tcx_mdct_window_minimum, hTcxCfg->tcx_mdct_window_minimumFB, hTcxCfg->tcx_mdct_window_min_lengthFB );
     787             :     }
     788             :     else
     789             :     {
     790           0 :         mdct_window_sine( hTcxCfg->tcx_mdct_window_minimumFB, input_Fs, hTcxCfg->tcx_mdct_window_min_lengthFB, MIN_OVERLAP, element_mode );
     791             :     }
     792             : 
     793             :     /* TCX Offset */
     794      608492 :     hTcxCfg->tcx_offset = ( hTcxCfg->tcx_mdct_window_delay >> 1 );
     795      608492 :     hTcxCfg->tcx_offsetFB = ( hTcxCfg->tcx_mdct_window_delayFB >> 1 );
     796             :     /*<0 rectangular transition with optimized window size = L_frame+L_frame/4*/
     797      608492 :     hTcxCfg->lfacNext = hTcxCfg->tcx_offset - L_frame / 4;
     798      608492 :     hTcxCfg->lfacNextFB = hTcxCfg->tcx_offsetFB - L_frameTCX / 4;
     799             : 
     800      608492 :     return;
     801             : }
     802             : 
     803             : /*-----------------------------------------------------------------------*
     804             :  * init_tcx()
     805             :  *
     806             :  * Initialization of TCX
     807             :  *-----------------------------------------------------------------------*/
     808             : 
     809      607614 : void init_tcx_cfg(
     810             :     TCX_CONFIG_HANDLE hTcxCfg,
     811             :     const int32_t total_brate,
     812             :     const int32_t sr_core,
     813             :     const int32_t input_Fs,
     814             :     const int16_t L_frame,
     815             :     const int16_t bwidth,
     816             :     const int16_t L_frameTCX,
     817             :     const int16_t fscale,
     818             :     const int16_t encoderLookahead_enc,
     819             :     const int16_t encoderLookahead_FB,
     820             :     const float preemph_fac,
     821             :     const int16_t tcxonly,
     822             :     const int16_t rf_mode,
     823             :     const int16_t igf,
     824             :     const int16_t infoIGFStopFreq,
     825             :     const int16_t element_mode,
     826             :     const int16_t ini_frame,
     827             :     const int16_t MCT_flag )
     828             : {
     829             :     int16_t i;
     830             :     int16_t mdctWindowLength;
     831             :     int16_t mdctWindowLengthFB;
     832             :     int16_t na_scale_bwidth;
     833             : 
     834      607614 :     hTcxCfg->preemph_fac = preemph_fac;
     835      607614 :     hTcxCfg->tcx5Size = NS2SA( sr_core, FRAME_SIZE_NS / 4 );    /* Always 5 ms */
     836      607614 :     hTcxCfg->tcx5SizeFB = NS2SA( input_Fs, FRAME_SIZE_NS / 4 ); /* Always 5 ms */
     837             : 
     838      607614 :     hTcxCfg->tcx_mdct_window_length_old = hTcxCfg->tcx_mdct_window_length;
     839      607614 :     mdctWindowLength = getMdctWindowLength( fscale );
     840      607614 :     mdctWindowLengthFB = (int16_t) ( mdctWindowLength * input_Fs / sr_core );
     841             : 
     842      607614 :     init_tcx_window_cfg( hTcxCfg, sr_core, input_Fs, L_frame, L_frameTCX, encoderLookahead_enc, encoderLookahead_FB, mdctWindowLength, mdctWindowLengthFB, element_mode );
     843             : 
     844             :     /* SQ deadzone & memory quantization*/
     845      607614 :     hTcxCfg->sq_rounding = 0.375f; /*deadzone of 1.25->rounding=1-1.25/2 (No deadzone=0.5)*/
     846             : 
     847             :     /* TCX rate loop */
     848      607614 :     hTcxCfg->tcxRateLoopOpt = ( tcxonly ) ? 2 : 0;
     849      607614 :     hTcxCfg->tcxRateLoopOpt = ( element_mode == IVAS_CPE_MDCT ) ? 3 : hTcxCfg->tcxRateLoopOpt;
     850             : 
     851             :     /* TCX bandwidth */
     852      607614 :     hTcxCfg->bandwidth = getTcxBandwidth( bwidth );
     853             : 
     854             :     /* set number of coded lines */
     855      607614 :     hTcxCfg->tcx_coded_lines = getNumTcxCodedLines( bwidth );
     856             : 
     857             :     /* TNS in TCX */
     858      607614 :     hTcxCfg->pCurrentTnsConfig = NULL;
     859      607614 :     hTcxCfg->fIsTNSAllowed = getTnsAllowed( total_brate, igf, element_mode );
     860             : 
     861      607614 :     if ( hTcxCfg->fIsTNSAllowed )
     862             :     {
     863      422170 :         InitTnsConfigs( bwidth, hTcxCfg->tcx_coded_lines, hTcxCfg->tnsConfig, infoIGFStopFreq, total_brate, element_mode, MCT_flag );
     864             : 
     865      422170 :         SetAllowTnsOnWhite( hTcxCfg->tnsConfig, element_mode == IVAS_CPE_MDCT );
     866             :     }
     867             : 
     868      607614 :     if ( ini_frame == 0 )
     869             :     {
     870      391502 :         hTcxCfg->tcx_curr_overlap_mode = hTcxCfg->tcx_last_overlap_mode = ALDO_WINDOW;
     871      391502 :         hTcxCfg->last_aldo = 1;
     872             :     }
     873             : 
     874             :     /* Context HM*/
     875      607614 :     hTcxCfg->ctx_hm = getCtxHm( element_mode, total_brate, rf_mode );
     876             : 
     877             :     /* Residual Coding*/
     878      607614 :     hTcxCfg->resq = getResq( total_brate );
     879      607614 :     hTcxCfg->tcxRateLoopOpt = ( hTcxCfg->resq && !tcxonly ) ? 1 : hTcxCfg->tcxRateLoopOpt;
     880             : 
     881             :     /*Set bandwidth scale*/
     882      607614 :     if ( bwidth == NB )
     883             :     {
     884       97227 :         na_scale_bwidth = NB;
     885             :     }
     886      510387 :     else if ( sr_core <= INT_FS_16k )
     887             :     {
     888      245523 :         na_scale_bwidth = WB;
     889             :     }
     890             :     else
     891             :     {
     892      264864 :         na_scale_bwidth = SWB;
     893             :     }
     894             : 
     895             :     /* Scale TCX for non-active frames to adjust loudness with ACELP*/
     896      607614 :     hTcxCfg->na_scale = 1.f;
     897             : 
     898      607614 :     if ( na_scale_bwidth < SWB && !tcxonly )
     899             :     {
     900     1862475 :         for ( i = 0; i < SIZE_SCALE_TABLE_TCX; i++ )
     901             :         {
     902     1851977 :             if ( ( na_scale_bwidth == scaleTcxTable[i].bwmode ) &&
     903      912143 :                  ( total_brate >= scaleTcxTable[i].bitrateFrom ) &&
     904      912143 :                  ( total_brate < scaleTcxTable[i].bitrateTo ) )
     905             :             {
     906      243368 :                 if ( rf_mode )
     907             :                 {
     908          96 :                     i--;
     909             :                 }
     910      243368 :                 hTcxCfg->na_scale = scaleTcxTable[i].scale;
     911      243368 :                 break;
     912             :             }
     913             :         }
     914             :     }
     915             : 
     916      607614 :     if ( tcxonly )
     917             :     {
     918      351682 :         InitPsychLPC( sr_core, L_frame, hTcxCfg );
     919             :     }
     920             :     else
     921             :     {
     922      255932 :         hTcxCfg->psychParamsCurrent = NULL;
     923             :     }
     924             : 
     925      607614 :     return;
     926             : }

Generated by: LCOV version 1.14