LCOV - code coverage report
Current view: top level - lib_com - core_com_config.c (source / functions) Hit Total Coverage
Test: Coverage on main @ 6baab0c613aa6c7100498ed7b93676aa8198a493 Lines: 319 332 96.1 %
Date: 2025-05-29 08:28:55 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        7747 : 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        7747 :     j = 0;
      64       48297 :     while ( j < SIZE_BRATE_TBL && ivas_total_brate != brate_tbl[j] )
      65             :     {
      66       40550 :         j++;
      67             :     }
      68             : 
      69             :     /* AMR-WB IO mode/EVS primary mode determination */
      70        7747 :     if ( j >= SIZE_BRATE_TBL )
      71             :     {
      72         518 :         switch ( ivas_total_brate )
      73             :         {
      74         518 :             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         518 :                 break;
      84           0 :             default:
      85             :             {
      86           0 :                 return 0;
      87             :             }
      88             :             break;
      89             :         }
      90             : 
      91         518 :         *Opt_AMR_WB = 1;
      92             :     }
      93             :     else
      94             :     {
      95        7229 :         *Opt_AMR_WB = 0;
      96             :     }
      97             : 
      98        7747 :     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      117048 : int16_t get_codec_mode(
     110             :     const int32_t total_brate /* i  : total bitrate   */
     111             : )
     112             : {
     113      117048 :     int16_t codec_mode = -1;
     114             : 
     115      117048 :     switch ( total_brate )
     116             :     {
     117           0 :         case 5900:
     118           0 :             codec_mode = MODE1;
     119           0 :             break;
     120        6371 :         case 7200:
     121        6371 :             codec_mode = MODE1;
     122        6371 :             break;
     123        4014 :         case 8000:
     124        4014 :             codec_mode = MODE1;
     125        4014 :             break;
     126        5933 :         case 9600:
     127        5933 :             codec_mode = MODE2;
     128        5933 :             break;
     129       28454 :         case 13200:
     130       28454 :             codec_mode = MODE1;
     131       28454 :             break;
     132        8152 :         case 16400:
     133        8152 :             codec_mode = MODE2;
     134        8152 :             break;
     135       19192 :         case 24400:
     136       19192 :             codec_mode = MODE2;
     137       19192 :             break;
     138        7063 :         case 32000:
     139        7063 :             codec_mode = MODE1;
     140        7063 :             break;
     141        6696 :         case 48000:
     142        6696 :             codec_mode = MODE2;
     143        6696 :             break;
     144       20130 :         case 64000:
     145       20130 :             codec_mode = MODE1;
     146       20130 :             break;
     147        5964 :         case 96000:
     148        5964 :             codec_mode = MODE2;
     149        5964 :             break;
     150        1991 :         case 128000:
     151        1991 :             codec_mode = MODE2;
     152        1991 :             break;
     153             :     }
     154             : 
     155      117048 :     return codec_mode;
     156             : }
     157             : 
     158             : 
     159             : /*-------------------------------------------------------------------*
     160             :  * getTcxonly()
     161             :  *
     162             :  *
     163             :  *-------------------------------------------------------------------*/
     164             : 
     165     8967037 : 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     8967037 :     int16_t tcxonly = 0;
     173             : 
     174     8967037 :     switch ( element_mode )
     175             :     {
     176        5045 :         case EVS_MONO:
     177        5045 :             if ( total_brate > ACELP_32k )
     178             :             {
     179         585 :                 tcxonly = 1;
     180             :             }
     181        5045 :             break;
     182     6718584 :         case IVAS_SCE:
     183     6718584 :             if ( is_ism_format )
     184             :             {
     185     4659288 :                 if ( total_brate > MAX_ACELP_BRATE_ISM )
     186             :                 {
     187     2228143 :                     tcxonly = 1;
     188             :                 }
     189             :             }
     190             :             else
     191             :             {
     192     2059296 :                 if ( total_brate > MAX_ACELP_BRATE )
     193             :                 {
     194      194731 :                     tcxonly = 1;
     195             :                 }
     196             :             }
     197     6718584 :             break;
     198             : 
     199      133543 :         case IVAS_CPE_DFT:
     200             :         case IVAS_CPE_TD:
     201      133543 :             if ( total_brate > MAX_ACELP_BRATE )
     202             :             {
     203           0 :                 tcxonly = 1;
     204             :             }
     205      133543 :             break;
     206     2010354 :         case IVAS_CPE_MDCT:
     207     2010354 :             if ( total_brate >= ( MCT_flag ? IVAS_32k : IVAS_48k ) )
     208             :             {
     209      919156 :                 tcxonly = 1;
     210             :             }
     211     2010354 :             break;
     212             :     }
     213             : 
     214     8967037 :     return tcxonly;
     215             : }
     216             : 
     217             : 
     218             : /*-------------------------------------------------------------------*
     219             :  * getCtxHm()
     220             :  *
     221             :  *
     222             :  *-------------------------------------------------------------------*/
     223             : 
     224    45907845 : 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    45907845 :     int16_t ctx_hm = 0;
     231             : 
     232    45907845 :     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     7490416 :         ctx_hm = 1;
     235             :     }
     236             : 
     237    45907845 :     return ctx_hm;
     238             : }
     239             : 
     240             : /*-------------------------------------------------------------------*
     241             :  * getResq()
     242             :  *
     243             :  *
     244             :  *-------------------------------------------------------------------*/
     245             : 
     246    45907845 : int16_t getResq(
     247             :     const int32_t total_brate /* i  : total bitrate               */
     248             : )
     249             : {
     250    45907845 :     int16_t resq = 0;
     251             : 
     252    45907845 :     if ( total_brate <= IVAS_64k )
     253             :     {
     254    20511303 :         resq = 1;
     255             :     }
     256             : 
     257    45907845 :     return resq;
     258             : }
     259             : 
     260             : /*-------------------------------------------------------------------*
     261             :  * getTnsAllowed()
     262             :  *
     263             :  *
     264             :  *-------------------------------------------------------------------*/
     265             : 
     266    47058698 : 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    47058698 :     int16_t tnsAllowed = 0;
     273             : 
     274    47058698 :     if ( igf )
     275             :     {
     276    27442855 :         if ( total_brate > HQ_16k40 || ( ( total_brate > HQ_13k20 ) && element_mode == IVAS_CPE_DFT ) )
     277             :         {
     278    24898736 :             tnsAllowed = 1;
     279             :         }
     280             :     }
     281    19615843 :     else if ( total_brate > HQ_32k )
     282             :     {
     283    17905790 :         tnsAllowed = 1;
     284             :     }
     285             : 
     286    47058698 :     return tnsAllowed;
     287             : }
     288             : 
     289             : /*-------------------------------------------------------------------*
     290             :  * getRestrictedMode()
     291             :  *
     292             :  *
     293             :  *-------------------------------------------------------------------*/
     294             : 
     295      563510 : 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      563510 :     int16_t restrictedMode = 3;
     302             : 
     303      563510 :     if ( !Opt_AMR_WB && ( total_brate > HQ_32k && element_mode != IVAS_SCE ) )
     304             :     {
     305      280539 :         restrictedMode = 6;
     306             :     }
     307      282971 :     else if ( !Opt_AMR_WB && ( total_brate > HQ_48k && element_mode == IVAS_SCE ) )
     308             :     {
     309       23761 :         restrictedMode = 6;
     310             :     }
     311      259210 :     else if ( Opt_AMR_WB )
     312             :     {
     313          48 :         restrictedMode = 1;
     314             :     }
     315             : 
     316      563510 :     return restrictedMode;
     317             : }
     318             : 
     319             : /*-------------------------------------------------------------------*
     320             :  * getMdctWindowLength()
     321             :  *
     322             :  *
     323             :  *-------------------------------------------------------------------*/
     324             : 
     325     1451338 : int16_t getMdctWindowLength(
     326             :     const int16_t fscale )
     327             : {
     328             :     int16_t mdctWindowLength;
     329             : 
     330     1451338 :     mdctWindowLength = ( L_LOOK_12k8 * fscale ) / FSCALE_DENOM;
     331             : 
     332     1451338 :     return mdctWindowLength;
     333             : }
     334             : 
     335             : /*-------------------------------------------------------------------*
     336             :  * sr2fscale()
     337             :  *
     338             :  *
     339             :  *-------------------------------------------------------------------*/
     340             : 
     341    81559482 : int16_t sr2fscale(
     342             :     const int32_t sr_core /* i  : internal sampling rate    */
     343             : )
     344             : {
     345             : 
     346    81559482 :     return (int16_t) ( ( FSCALE_DENOM * sr_core ) / 12800 );
     347             : }
     348             : 
     349             : /*-------------------------------------------------------------------*
     350             :  * getCoreSamplerateMode2()
     351             :  *
     352             :  *
     353             :  *-------------------------------------------------------------------*/
     354             : 
     355    61173268 : 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    61173268 :     int32_t sr_core = 0;
     365             : 
     366    61173268 :     if ( bwidth == NB )
     367             :     {
     368      419782 :         sr_core = INT_FS_12k8;
     369             :     }
     370    60753486 :     else if ( element_mode == EVS_MONO && ( ( bwidth == WB && total_brate < ACELP_13k20 ) || ( bwidth == SWB && total_brate <= ACELP_13k20 ) || ( rf_mode == 1 ) ) )
     371             :     {
     372        5708 :         sr_core = INT_FS_12k8;
     373             :     }
     374    60747778 :     else if ( element_mode > EVS_MONO && flag_ACELP16k == 0 )
     375             :     {
     376     2793141 :         sr_core = INT_FS_12k8;
     377             :     }
     378    57954637 :     else if ( bwidth == WB || ( bwidth == SWB && total_brate <= ACELP_32k ) || ( bwidth == FB && total_brate <= ACELP_32k ) )
     379             :     {
     380    15335441 :         sr_core = INT_FS_16k;
     381             :     }
     382    42619196 :     else if ( ( bwidth == SWB || bwidth == FB ) && total_brate <= MAX_ACELP_BRATE && element_mode == IVAS_SCE && !is_ism_format )
     383             :     {
     384       28456 :         sr_core = INT_FS_16k;
     385             :     }
     386    42590740 :     else if ( ( bwidth == SWB || bwidth == FB ) && total_brate <= MAX_ACELP_BRATE_ISM && element_mode == IVAS_SCE && is_ism_format )
     387             :     {
     388      298259 :         sr_core = INT_FS_16k;
     389             :     }
     390    42292481 :     else if ( ( bwidth == SWB || bwidth == FB ) && total_brate <= MAX_ACELP_BRATE && element_mode == IVAS_SCE && is_ism_format )
     391             :     {
     392      454101 :         sr_core = 25600;
     393             :     }
     394    41838380 :     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    19750435 :         sr_core = 25600;
     397             :     }
     398    22087945 :     else if ( bwidth == SWB || bwidth == FB )
     399             :     {
     400    22087945 :         sr_core = 32000;
     401             :     }
     402             : 
     403    61173268 :     return sr_core;
     404             : }
     405             : 
     406             : /*-------------------------------------------------------------------*
     407             :  * getTcxBandwidth()
     408             :  *
     409             :  *
     410             :  *-------------------------------------------------------------------*/
     411             : 
     412     2606995 : float getTcxBandwidth(
     413             :     const int16_t bwidth /* i  : audio bandwidth        */
     414             : )
     415             : {
     416     2606995 :     float tcxBandwidth = 0.5f;
     417             : 
     418     2606995 :     if ( bwidth == NB )
     419             :     {
     420      564391 :         tcxBandwidth = 0.3125f;
     421             :     }
     422             : 
     423     2606995 :     return tcxBandwidth;
     424             : }
     425             : 
     426             : /*-------------------------------------------------------------------*
     427             :  * getIgfPresent()
     428             :  *
     429             :  *
     430             :  *-------------------------------------------------------------------*/
     431             : 
     432    46193822 : 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    46193822 :     int16_t igfPresent = 0;
     440             : 
     441             : 
     442    46193822 :     if ( bwidth == SWB )
     443             :     {
     444    12819678 :         switch ( element_mode )
     445             :         {
     446     2971344 :             case EVS_MONO:
     447             :             case IVAS_SCE:
     448     2971344 :                 if ( total_brate <= HQ_64k )
     449             :                 {
     450     2705470 :                     igfPresent = 1;
     451             :                 }
     452     2971344 :                 break;
     453      486245 :             case IVAS_CPE_DFT:
     454             :             case IVAS_CPE_TD:
     455      486245 :                 if ( total_brate <= HQ_48k )
     456             :                 {
     457      486245 :                     igfPresent = 1;
     458             :                 }
     459      486245 :                 break;
     460     9362089 :             case IVAS_CPE_MDCT:
     461     9362089 :                 if ( total_brate <= IVAS_96k )
     462             :                 {
     463     5009152 :                     igfPresent = 1;
     464             :                 }
     465     9362089 :                 break;
     466             :         }
     467    12819678 :     }
     468    33374144 :     else if ( bwidth == FB )
     469             :     {
     470    24084888 :         switch ( element_mode )
     471             :         {
     472     4466189 :             case EVS_MONO:
     473             :             case IVAS_SCE:
     474     4466189 :                 if ( total_brate <= IVAS_128k )
     475             :                 {
     476     4381684 :                     igfPresent = 1;
     477             :                 }
     478     4466189 :                 break;
     479      223246 :             case IVAS_CPE_DFT:
     480             :             case IVAS_CPE_TD:
     481      223246 :                 if ( total_brate <= IVAS_48k )
     482             :                 {
     483      223246 :                     igfPresent = 1;
     484             :                 }
     485      223246 :                 break;
     486    19395453 :             case IVAS_CPE_MDCT:
     487    19395453 :                 if ( total_brate <= IVAS_128k )
     488             :                 {
     489    14351516 :                     igfPresent = 1;
     490             :                 }
     491    19395453 :                 break;
     492             :         }
     493    24084888 :     }
     494     9289256 :     else if ( bwidth == WB )
     495             :     {
     496     9281730 :         switch ( element_mode )
     497             :         {
     498     1558738 :             case EVS_MONO:
     499             :             case IVAS_SCE:
     500     1558738 :                 if ( total_brate <= ACELP_9k60 )
     501             :                 {
     502       59328 :                     igfPresent = 1;
     503             :                 }
     504     1558738 :                 break;
     505     7722992 :             case IVAS_CPE_DFT:
     506             :             case IVAS_CPE_TD:
     507             :             case IVAS_CPE_MDCT:
     508     7722992 :                 if ( total_brate <= ACELP_13k20 )
     509             :                 {
     510       74019 :                     igfPresent = 1;
     511             :                 }
     512     7722992 :                 break;
     513             :         }
     514        7526 :     }
     515             : 
     516    46193822 :     if ( ( ( bwidth == WB ) || ( bwidth == SWB ) ) && ( rf_mode == 1 ) && ( total_brate == ACELP_13k20 ) )
     517             :     {
     518        1697 :         igfPresent = 1;
     519             :     }
     520             : 
     521    46193822 :     return igfPresent;
     522             : }
     523             : 
     524             : /*-------------------------------------------------------------------*
     525             :  * getCnaPresent()
     526             :  *
     527             :  *
     528             :  *-------------------------------------------------------------------*/
     529             : 
     530      477431 : 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      477431 :     int16_t flag_cna = 0;
     538             : 
     539      477431 :     if ( element_mode == IVAS_CPE_DFT && element_brate <= CNA_MAX_BRATE_DFT_STEREO )
     540             :     {
     541       31356 :         flag_cna = 1;
     542             :     }
     543             :     else
     544             :     {
     545      446075 :         if ( bwidth == NB && total_brate <= ACELP_13k20 )
     546             :         {
     547         120 :             flag_cna = 1;
     548             :         }
     549             : 
     550      446075 :         if ( bwidth == WB && total_brate <= ACELP_13k20 )
     551             :         {
     552          97 :             flag_cna = 1;
     553             :         }
     554             : 
     555      446075 :         if ( bwidth == SWB && total_brate <= ACELP_13k20 )
     556             :         {
     557         337 :             flag_cna = 1;
     558             :         }
     559             :     }
     560             : 
     561      477431 :     return flag_cna;
     562             : }
     563             : 
     564             : /*-------------------------------------------------------------------*
     565             :  * getTcxLtp()
     566             :  *
     567             :  *
     568             :  *-------------------------------------------------------------------*/
     569             : 
     570    33791276 : int16_t getTcxLtp(
     571             :     const int32_t sr_core /* i  : internal sampling rate */
     572             : )
     573             : {
     574    33791276 :     int16_t tcxltp = 0;
     575             : 
     576    33791276 :     if ( sr_core <= 25600 )
     577             :     {
     578    22088602 :         tcxltp = 1;
     579             :     }
     580             : 
     581    33791276 :     return tcxltp;
     582             : }
     583             : 
     584             : /*-------------------------------------------------------------------*
     585             :  * initPitchLagParameters()
     586             :  *
     587             :  *
     588             :  *-------------------------------------------------------------------*/
     589             : 
     590     1722109 : 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     1722109 :     if ( sr_core == INT_FS_12k8 )
     601             :     {
     602     1294645 :         *pit_min = PIT_MIN_12k8;
     603     1294645 :         *pit_max = PIT_MAX_12k8;
     604     1294645 :         *pit_fr2 = PIT_FR2_12k8;
     605     1294645 :         *pit_fr1 = PIT_FR1_12k8;
     606     1294645 :         *pit_fr1b = PIT_FR1_8b_12k8;
     607     1294645 :         pit_res_max = 4;
     608             :     }
     609      427464 :     else if ( sr_core == INT_FS_16k )
     610             :     {
     611      345756 :         *pit_min = PIT_MIN_16k;
     612      345756 :         *pit_max = PIT16k_MAX;
     613      345756 :         *pit_fr2 = PIT_FR2_16k;
     614      345756 :         *pit_fr1 = PIT_FR1_16k;
     615      345756 :         *pit_fr1b = PIT_FR1_8b_16k;
     616      345756 :         pit_res_max = 6;
     617             :     }
     618       81708 :     else if ( sr_core == 25600 )
     619             :     {
     620       24683 :         *pit_min = PIT_MIN_25k6;
     621       24683 :         *pit_max = PIT_MAX_25k6;
     622       24683 :         *pit_fr2 = PIT_FR2_25k6;
     623       24683 :         *pit_fr1 = PIT_FR1_25k6;
     624       24683 :         *pit_fr1b = PIT_FR1_8b_25k6;
     625       24683 :         pit_res_max = 4;
     626             :     }
     627             :     else /* sr_core==32000 */
     628             :     {
     629       57025 :         *pit_min = PIT_MIN_32k;
     630       57025 :         *pit_max = PIT_MAX_32k;
     631       57025 :         *pit_fr2 = PIT_FR2_32k;
     632       57025 :         *pit_fr1 = PIT_FR1_32k;
     633       57025 :         *pit_fr1b = PIT_FR1_8b_32k;
     634       57025 :         pit_res_max = 6;
     635             :     }
     636             : 
     637     1722109 :     return pit_res_max;
     638             : }
     639             : 
     640             : /*-------------------------------------------------------------------*
     641             :  * getNumTcxCodedLines()
     642             :  *
     643             :  *
     644             :  *-------------------------------------------------------------------*/
     645             : 
     646    14305636 : int16_t getNumTcxCodedLines(
     647             :     const int16_t bwidth /* i  : audio bandwidth        */
     648             : )
     649             : {
     650             :     int16_t tcx_coded_lines;
     651             : 
     652    14305636 :     switch ( bwidth )
     653             :     {
     654      145551 :         case NB:
     655      145551 :             tcx_coded_lines = 160;
     656      145551 :             break;
     657     3007635 :         case WB:
     658     3007635 :             tcx_coded_lines = 320;
     659     3007635 :             break;
     660     4199758 :         case SWB:
     661     4199758 :             tcx_coded_lines = 640;
     662     4199758 :             break;
     663     6952692 :         case FB:
     664     6952692 :             tcx_coded_lines = 960;
     665     6952692 :             break;
     666           0 :         default:
     667           0 :             tcx_coded_lines = 0;
     668           0 :             break;
     669             :     }
     670             : 
     671    14305636 :     return tcx_coded_lines;
     672             : }
     673             : 
     674             : /*-------------------------------------------------------------------*
     675             :  * getTcxLpcShapedAri()
     676             :  *
     677             :  *
     678             :  *-------------------------------------------------------------------*/
     679             : 
     680    46174849 : 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    46174849 :     int16_t tcx_lpc_shaped_ari = 0;
     687             : 
     688    46174849 :     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     1117170 :         tcx_lpc_shaped_ari = 1;
     691             :     }
     692             : 
     693    46174849 :     return tcx_lpc_shaped_ari;
     694             : }
     695             : 
     696             : 
     697             : /*-----------------------------------------------------------------------*
     698             :  * init_tcx_window_cfg()
     699             :  *
     700             :  * Initialization of TCX windows
     701             :  *-----------------------------------------------------------------------*/
     702             : 
     703     1451338 : 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     1451338 :     hTcxCfg->tcx_mdct_window_delay = mdctWindowLength;
     718     1451338 :     hTcxCfg->tcx_mdct_window_delayFB = mdctWindowLengthFB;
     719     1451338 :     hTcxCfg->tcx_mdct_window_length = mdctWindowLength;
     720     1451338 :     hTcxCfg->tcx_mdct_window_lengthFB = mdctWindowLengthFB;
     721             : 
     722     1451338 :     mdct_window_sine( hTcxCfg->tcx_mdct_window, sr_core, hTcxCfg->tcx_mdct_window_length, FULL_OVERLAP, element_mode );
     723     1451338 :     if ( hTcxCfg->tcx_mdct_window_length == hTcxCfg->tcx_mdct_window_lengthFB )
     724             :     {
     725      222802 :         mvr2r( hTcxCfg->tcx_mdct_window, hTcxCfg->tcx_mdct_windowFB, hTcxCfg->tcx_mdct_window_length );
     726             :     }
     727             :     else
     728             :     {
     729     1228536 :         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     1451338 :     mdct_window_aldo( hTcxCfg->tcx_aldo_window_1, hTcxCfg->tcx_aldo_window_2, L_frame );
     734     1451338 :     mdct_window_aldo( hTcxCfg->tcx_aldo_window_1_FB, hTcxCfg->tcx_aldo_window_2_FB, NS2SA( input_Fs, FRAME_SIZE_NS ) );
     735     1451338 :     hTcxCfg->tcx_aldo_window_1_trunc = hTcxCfg->tcx_aldo_window_1 + NS2SA( sr_core, N_ZERO_MDCT_NS );
     736     1451338 :     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     1451338 :     hTcxCfg->tcx_mdct_window_trans_length = NS2SA( sr_core, ACELP_TCX_TRANS_NS );
     740     1451338 :     mdct_window_sine( hTcxCfg->tcx_mdct_window_trans, sr_core, hTcxCfg->tcx_mdct_window_trans_length, TRANSITION_OVERLAP, element_mode );
     741     1451338 :     hTcxCfg->tcx_mdct_window_trans_lengthFB = NS2SA( input_Fs, ACELP_TCX_TRANS_NS );
     742     1451338 :     if ( hTcxCfg->tcx_mdct_window_trans_length == hTcxCfg->tcx_mdct_window_trans_lengthFB )
     743             :     {
     744      222802 :         mvr2r( hTcxCfg->tcx_mdct_window_trans, hTcxCfg->tcx_mdct_window_transFB, hTcxCfg->tcx_mdct_window_trans_length );
     745             :     }
     746             :     else
     747             :     {
     748     1228536 :         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     1451338 :     hTcxCfg->tcx_mdct_window_half_length = 2 * ( ( encoderLookahead_enc - (int16_t) ( 0.005f * sr_core + 0.5f ) ) >> 1 );
     754     1451338 :     hTcxCfg->tcx_mdct_window_half_lengthFB = 2 * ( ( encoderLookahead_FB - (int16_t) ( 0.005f * input_Fs + 0.5f ) ) >> 1 );
     755     1451338 :     assert( ( hTcxCfg->tcx_mdct_window_half_length > 16 ) && "Half window can not be large enough!" );
     756             : 
     757     1451338 :     mdct_window_sine( hTcxCfg->tcx_mdct_window_half, sr_core, hTcxCfg->tcx_mdct_window_half_length, HALF_OVERLAP, element_mode );
     758     1451338 :     if ( hTcxCfg->tcx_mdct_window_half_length == hTcxCfg->tcx_mdct_window_half_lengthFB )
     759             :     {
     760      222802 :         mvr2r( hTcxCfg->tcx_mdct_window_half, hTcxCfg->tcx_mdct_window_halfFB, hTcxCfg->tcx_mdct_window_half_length );
     761             :     }
     762             :     else
     763             :     {
     764     1228536 :         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     1451338 :     hTcxCfg->tcx_mdct_window_min_length = (int16_t) ( sr_core * INV_CLDFB_BANDWIDTH );
     769     1451338 :     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     1451338 :     if ( hTcxCfg->tcx_mdct_window_min_length == hTcxCfg->tcx_mdct_window_trans_length )
     772             :     {
     773     1451338 :         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     1451338 :     if ( hTcxCfg->tcx_mdct_window_min_lengthFB == hTcxCfg->tcx_mdct_window_trans_lengthFB )
     781             :     {
     782     1451338 :         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     1451338 :     hTcxCfg->tcx_offset = ( hTcxCfg->tcx_mdct_window_delay >> 1 );
     795     1451338 :     hTcxCfg->tcx_offsetFB = ( hTcxCfg->tcx_mdct_window_delayFB >> 1 );
     796             :     /*<0 rectangular transition with optimized window size = L_frame+L_frame/4*/
     797     1451338 :     hTcxCfg->lfacNext = hTcxCfg->tcx_offset - L_frame / 4;
     798     1451338 :     hTcxCfg->lfacNextFB = hTcxCfg->tcx_offsetFB - L_frameTCX / 4;
     799             : 
     800     1451338 :     return;
     801             : }
     802             : 
     803             : /*-----------------------------------------------------------------------*
     804             :  * init_tcx()
     805             :  *
     806             :  * Initialization of TCX
     807             :  *-----------------------------------------------------------------------*/
     808             : 
     809     1448165 : 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     1448165 :     hTcxCfg->preemph_fac = preemph_fac;
     835     1448165 :     hTcxCfg->tcx5Size = NS2SA( sr_core, FRAME_SIZE_NS / 4 );    /* Always 5 ms */
     836     1448165 :     hTcxCfg->tcx5SizeFB = NS2SA( input_Fs, FRAME_SIZE_NS / 4 ); /* Always 5 ms */
     837             : 
     838     1448165 :     hTcxCfg->tcx_mdct_window_length_old = hTcxCfg->tcx_mdct_window_length;
     839     1448165 :     mdctWindowLength = getMdctWindowLength( fscale );
     840     1448165 :     mdctWindowLengthFB = (int16_t) ( mdctWindowLength * input_Fs / sr_core );
     841             : 
     842     1448165 :     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     1448165 :     hTcxCfg->sq_rounding = 0.375f; /*deadzone of 1.25->rounding=1-1.25/2 (No deadzone=0.5)*/
     846             : 
     847             :     /* TCX rate loop */
     848     1448165 :     hTcxCfg->tcxRateLoopOpt = ( tcxonly ) ? 2 : 0;
     849     1448165 :     hTcxCfg->tcxRateLoopOpt = ( element_mode == IVAS_CPE_MDCT ) ? 3 : hTcxCfg->tcxRateLoopOpt;
     850             : 
     851             :     /* TCX bandwidth */
     852     1448165 :     hTcxCfg->bandwidth = getTcxBandwidth( bwidth );
     853             : 
     854             :     /* set number of coded lines */
     855     1448165 :     hTcxCfg->tcx_coded_lines = getNumTcxCodedLines( bwidth );
     856             : 
     857             :     /* TNS in TCX */
     858     1448165 :     hTcxCfg->pCurrentTnsConfig = NULL;
     859     1448165 :     hTcxCfg->fIsTNSAllowed = getTnsAllowed( total_brate, igf, element_mode );
     860             : 
     861     1448165 :     if ( hTcxCfg->fIsTNSAllowed )
     862             :     {
     863     1069878 :         InitTnsConfigs( bwidth, hTcxCfg->tcx_coded_lines, hTcxCfg->tnsConfig, infoIGFStopFreq, total_brate, element_mode, MCT_flag );
     864             : 
     865     1069878 :         SetAllowTnsOnWhite( hTcxCfg->tnsConfig, element_mode == IVAS_CPE_MDCT );
     866             :     }
     867             : 
     868     1448165 :     if ( ini_frame == 0 )
     869             :     {
     870      712001 :         hTcxCfg->tcx_curr_overlap_mode = hTcxCfg->tcx_last_overlap_mode = ALDO_WINDOW;
     871      712001 :         hTcxCfg->last_aldo = 1;
     872             :     }
     873             : 
     874             :     /* Context HM*/
     875     1448165 :     hTcxCfg->ctx_hm = getCtxHm( element_mode, total_brate, rf_mode );
     876             : 
     877             :     /* Residual Coding*/
     878     1448165 :     hTcxCfg->resq = getResq( total_brate );
     879     1448165 :     hTcxCfg->tcxRateLoopOpt = ( hTcxCfg->resq && !tcxonly ) ? 1 : hTcxCfg->tcxRateLoopOpt;
     880             : 
     881             :     /*Set bandwidth scale*/
     882     1448165 :     if ( bwidth == NB )
     883             :     {
     884      145420 :         na_scale_bwidth = NB;
     885             :     }
     886     1302745 :     else if ( sr_core <= INT_FS_16k )
     887             :     {
     888      596107 :         na_scale_bwidth = WB;
     889             :     }
     890             :     else
     891             :     {
     892      706638 :         na_scale_bwidth = SWB;
     893             :     }
     894             : 
     895             :     /* Scale TCX for non-active frames to adjust loudness with ACELP*/
     896     1448165 :     hTcxCfg->na_scale = 1.f;
     897             : 
     898     1448165 :     if ( na_scale_bwidth < SWB && !tcxonly )
     899             :     {
     900     5591648 :         for ( i = 0; i < SIZE_SCALE_TABLE_TCX; i++ )
     901             :         {
     902     5574074 :             if ( ( na_scale_bwidth == scaleTcxTable[i].bwmode ) &&
     903     2656964 :                  ( total_brate >= scaleTcxTable[i].bitrateFrom ) &&
     904     2656964 :                  ( total_brate < scaleTcxTable[i].bitrateTo ) )
     905             :             {
     906      614031 :                 if ( rf_mode )
     907             :                 {
     908          97 :                     i--;
     909             :                 }
     910      614031 :                 hTcxCfg->na_scale = scaleTcxTable[i].scale;
     911      614031 :                 break;
     912             :             }
     913             :         }
     914             :     }
     915             : 
     916     1448165 :     if ( tcxonly )
     917             :     {
     918      799326 :         InitPsychLPC( sr_core, L_frame, hTcxCfg );
     919             :     }
     920             :     else
     921             :     {
     922      648839 :         hTcxCfg->psychParamsCurrent = NULL;
     923             :     }
     924             : 
     925     1448165 :     return;
     926             : }

Generated by: LCOV version 1.14