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

Generated by: LCOV version 1.14