LCOV - code coverage report
Current view: top level - lib_dec - ivas_decision_matrix_dec.c (source / functions) Hit Total Coverage
Test: Coverage on main -- long test vectors @ efe53129c9ed87a5067dd0a8fb9dca41db9c4add Lines: 154 157 98.1 %
Date: 2026-02-12 08:06:16 Functions: 1 1 100.0 %

          Line data    Source code
       1             : /******************************************************************************************************
       2             : 
       3             :    (C) 2022-2026 IVAS codec Public Collaboration with portions copyright Dolby International AB, Ericsson AB,
       4             :    Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
       5             :    Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
       6             :    Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
       7             :    contributors to this repository. All Rights Reserved.
       8             : 
       9             :    This software is protected by copyright law and by international treaties.
      10             :    The IVAS codec Public Collaboration consisting of Dolby International AB, Ericsson AB,
      11             :    Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
      12             :    Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
      13             :    Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
      14             :    contributors to this repository retain full ownership rights in their respective contributions in
      15             :    the software. This notice grants no license of any kind, including but not limited to patent
      16             :    license, nor is any license granted by implication, estoppel or otherwise.
      17             : 
      18             :    Contributors are required to enter into the IVAS codec Public Collaboration agreement before making
      19             :    contributions.
      20             : 
      21             :    This software is provided "AS IS", without any express or implied warranties. The software is in the
      22             :    development stage. It is intended exclusively for experts who have experience with such software and
      23             :    solely for the purpose of inspection. All implied warranties of non-infringement, merchantability
      24             :    and fitness for a particular purpose are hereby disclaimed and excluded.
      25             : 
      26             :    Any dispute, controversy or claim arising under or in relation to providing this software shall be
      27             :    submitted to and settled by the final, binding jurisdiction of the courts of Munich, Germany in
      28             :    accordance with the laws of the Federal Republic of Germany excluding its conflict of law rules and
      29             :    the United Nations Convention on Contracts on the International Sales of Goods.
      30             : 
      31             : *******************************************************************************************************/
      32             : 
      33             : #include <stdint.h>
      34             : #include "options.h"
      35             : #ifdef DEBUGGING
      36             : #include "debug.h"
      37             : #endif
      38             : #include "stat_dec.h"
      39             : #include "rom_com.h"
      40             : #include "ivas_prot.h"
      41             : #include "prot.h"
      42             : #include "ivas_cnst.h"
      43             : #include "wmc_auto.h"
      44             : 
      45             : /*-----------------------------------------------------------------*
      46             :  * ivas_decision_matrix_dec()
      47             :  *
      48             :  * ACELP/TCX/HQ core selection
      49             :  * Read ACELP signaling bits from the bitstream
      50             :  * Set extension layers
      51             :  *-----------------------------------------------------------------*/
      52             : 
      53    39937680 : void ivas_decision_matrix_dec(
      54             :     Decoder_State *st,            /* i/o: decoder state structure             */
      55             :     int16_t *sharpFlag,           /* o  : formant sharpening flag             */
      56             :     int16_t *core_switching_flag, /* o  : ACELP->HQ switching frame flag      */
      57             :     const int32_t element_brate,  /* i  : element bitrate                     */
      58             :     const int16_t nchan_out       /* i  : Number of output channels           */
      59             : )
      60             : {
      61             :     int16_t tmp;
      62             :     int32_t icbwe_brate;
      63             : 
      64             :     /* init */
      65    39937680 :     icbwe_brate = 0;
      66    39937680 :     st->core = -1;
      67    39937680 :     st->core_brate = 0;
      68    39937680 :     st->extl = -1;
      69    39937680 :     st->extl_brate = 0;
      70    39937680 :     st->ppp_mode_dec = 0;
      71    39937680 :     st->nelp_mode_dec = 0;
      72    39937680 :     st->igf = 0;
      73    39937680 :     st->vbr_hw_BWE_disable_dec = 0;
      74             : 
      75             :     /*-----------------------------------------------------------------*
      76             :      * Read SID signaling bits from the bitstream
      77             :      *-----------------------------------------------------------------*/
      78             : 
      79    39937680 :     if ( ( st->idchan == 0 && ( st->total_brate == FRAME_NO_DATA || st->total_brate == SID_2k40 ) ) || ( st->element_mode == IVAS_CPE_MDCT && st->total_brate <= SID_2k40 ) )
      80             :     {
      81      213315 :         st->core = ACELP_CORE;
      82      213315 :         st->core_brate = st->total_brate;
      83             : 
      84      213315 :         if ( st->total_brate == SID_2k40 && !( st->idchan == 1 && st->element_mode == IVAS_CPE_MDCT ) )
      85             :         {
      86       24501 :             if ( st->element_mode != IVAS_CPE_DFT )
      87             :             {
      88       12969 :                 st->cng_type = get_next_indice( st, 1 );
      89             : 
      90       12969 :                 if ( st->cng_type == FD_CNG )
      91             :                 {
      92       12555 :                     st->bwidth = get_next_indice( st, 2 );
      93             :                 }
      94             :             }
      95       24501 :             if ( get_next_indice( st, 1 ) )
      96             :             {
      97       15207 :                 st->L_frame = L_FRAME16k;
      98       15207 :                 st->nb_subfr = NB_SUBFR16k;
      99       15207 :                 st->bwidth = max( st->bwidth, WB );
     100             :             }
     101             :             else
     102             :             {
     103        9294 :                 st->L_frame = L_FRAME;
     104             :             }
     105             :         }
     106      188814 :         else if ( st->total_brate == SID_2k40 && st->idchan == 1 && st->element_mode == IVAS_CPE_MDCT && st->cng_sba_flag == 0 )
     107             :         {
     108             :             /* read channel coherence */
     109        3219 :             st->hFdCngDec->hFdCngCom->coherence[0] = (float) get_next_indice( st, 4 ) / 15.f;
     110             : 
     111             :             /* read flag for no side noise shape */
     112        3219 :             st->hFdCngDec->hFdCngCom->no_side_flag = get_next_indice( st, 1 );
     113             :         }
     114             : 
     115      213315 :         if ( ( st->output_Fs >= 32000 && st->bwidth >= SWB ) || ( st->element_mode == IVAS_CPE_DFT && st->bwidth >= SWB && nchan_out == 2 && st->L_frame < L_FRAME16k ) )
     116             :         {
     117      154911 :             st->extl = SWB_CNG;
     118             :         }
     119             : 
     120      213315 :         if ( st->total_brate == FRAME_NO_DATA && st->prev_bfi && !st->bfi && st->L_frame > L_FRAME16k )
     121             :         {
     122           0 :             st->L_frame = st->last_CNG_L_frame;
     123             :         }
     124             : 
     125      213315 :         return;
     126             :     }
     127             : 
     128             :     /*---------------------------------------------------------------------*
     129             :      * ACELP/HQ core selection
     130             :      *---------------------------------------------------------------------*/
     131             : 
     132    39724365 :     if ( st->element_mode == IVAS_CPE_TD && st->idchan == 1 )
     133             :     {
     134             :         /* minimal signaling for the secondary channel, most of the parameters are deduced from the primary channel */
     135       72741 :         st->core = ACELP_CORE;
     136             :     }
     137    39651624 :     else if ( st->element_mode == IVAS_SCE && st->low_rate_mode )
     138             :     {
     139             :         /* ISM Low-rate mode -> always WB, ACELP core, IC coder_type */
     140      766908 :         st->core = ACELP_CORE;
     141             :     }
     142    38884716 :     else if ( st->element_mode == IVAS_CPE_MDCT )
     143             :     {
     144    28882776 :         st->core = TCX_20_CORE;
     145             :     }
     146             :     else
     147             :     {
     148    10001940 :         st->core = ACELP_CORE;
     149             : 
     150    10001940 :         if ( st->element_mode == IVAS_CPE_TD || st->total_brate >= STEREO_TCX_MIN_RATE )
     151             :         {
     152             :             /* ACELP/transform core selection bit */
     153     9944064 :             if ( get_next_indice( st, 1 ) )
     154             :             {
     155     6872907 :                 st->core = HQ_CORE;
     156             :             }
     157             :             else
     158             :             {
     159     3071157 :                 st->core = ACELP_CORE;
     160             :             }
     161             :         }
     162             :     }
     163             : 
     164             :     /*-----------------------------------------------------------------*
     165             :      * Read ACELP signaling bits from the bitstream
     166             :      *-----------------------------------------------------------------*/
     167             : 
     168    39724365 :     if ( st->core == ACELP_CORE )
     169             :     {
     170     3968682 :         if ( st->element_mode == IVAS_SCE && st->low_rate_mode )
     171             :         {
     172             :             /* ISM Low-rate mode */
     173      766908 :             st->bwidth = WB;
     174      766908 :             st->coder_type = INACTIVE;
     175      766908 :             *sharpFlag = 0;
     176             :         }
     177     3201774 :         else if ( st->idchan == 1 && st->element_mode == IVAS_CPE_TD )
     178             :         {
     179       72741 :             *sharpFlag = 0;
     180       72741 :             if ( st->coder_type == GENERIC || st->coder_type == VOICED )
     181             :             {
     182       51219 :                 *sharpFlag = 1;
     183             :             }
     184             : 
     185       72741 :             st->core_brate = st->total_brate;
     186       72741 :             st->codec_mode = MODE1;
     187             : 
     188       72741 :             if ( st->idchan == 1 && ( st->tdm_LRTD_flag == 0 || st->bits_frame_channel < IVAS_16k4 / FRAMES_PER_SEC ) )
     189             :             {
     190        6024 :                 st->bwidth = WB; /* only WB in the secondary channel */
     191             :             }
     192             :         }
     193             :         else
     194             :         {
     195     3129033 :             if ( element_brate < FRMT_SHP_MIN_BRATE_IVAS )
     196             :             {
     197     1394700 :                 st->coder_type = get_next_indice( st, 3 );
     198     1394700 :                 *sharpFlag = 0;
     199             : 
     200     1394700 :                 if ( element_brate < IVAS_24k4 && ( st->coder_type == VOICED || st->coder_type == GENERIC || st->coder_type == TRANSITION ) )
     201             :                 {
     202     1176852 :                     *sharpFlag = 1;
     203             :                 }
     204             :             }
     205             :             else
     206             :             {
     207             :                 /* get coder_type info */
     208     1734333 :                 st->coder_type = get_next_indice( st, 3 );
     209             : 
     210             :                 /* get sharpening flag */
     211     1734333 :                 *sharpFlag = get_next_indice( st, 1 );
     212             :             }
     213             :         }
     214             :     }
     215             : 
     216             :     /*-----------------------------------------------------------------*
     217             :      * Set extension layers
     218             :      *-----------------------------------------------------------------*/
     219             : 
     220    39724365 :     if ( st->core == ACELP_CORE )
     221             :     {
     222     3968682 :         if ( st->bwidth == WB && st->low_rate_mode )
     223             :         {
     224      766908 :             st->extl = WB_BWE;
     225      766908 :             if ( st->total_brate >= MIN_BRATE_WB_BWE )
     226             :             {
     227       21087 :                 st->extl_brate = WB_BWE_0k35;
     228             :             }
     229             :         }
     230     3201774 :         else if ( st->bwidth == WB && ( st->total_brate < MIN_BRATE_WB_BWE || ( st->idchan == 1 && st->element_mode == IVAS_CPE_TD ) ) )
     231             :         {
     232       10401 :             if ( st->vbr_hw_BWE_disable_dec == 0 )
     233             :             {
     234       10401 :                 st->extl = WB_BWE;
     235             :             }
     236             :         }
     237     3191373 :         else if ( st->bwidth == WB && st->total_brate >= MIN_BRATE_WB_BWE && !st->flag_ACELP16k )
     238             :         {
     239             :             /* read the WB TBE/BWE selection bit */
     240      207498 :             if ( get_next_indice( st, 1 ) )
     241             :             {
     242       44559 :                 st->extl = WB_BWE;
     243       44559 :                 st->extl_brate = WB_BWE_0k35;
     244             :             }
     245             :             else
     246             :             {
     247      162939 :                 st->extl = WB_TBE;
     248      162939 :                 if ( st->total_brate < MIN_BRATE_WB_TBE_1k05 || ( st->element_mode == IVAS_CPE_TD && st->total_brate < MIN_TDM_BRATE_WB_TBE_1k05 ) )
     249             :                 {
     250       21336 :                     st->extl_brate = WB_TBE_0k35;
     251             :                 }
     252             :                 else
     253             :                 {
     254      141603 :                     st->extl_brate = WB_TBE_1k05;
     255             :                 }
     256             :             }
     257             :         }
     258     2983875 :         else if ( st->bwidth == SWB || st->bwidth == FB )
     259             :         {
     260     2714541 :             if ( st->total_brate >= MIN_BRATE_SWB_BWE || ( st->total_brate >= MIN_MIN_BRATE_LRTD_SWB_BWE && st->element_mode == IVAS_CPE_TD && st->bwidth == SWB && st->tdm_LRTD_flag ) || ( element_brate < IVAS_16k4 && st->total_brate >= MIN_MIN_BRATE_LRTD_SWB_BWE && st->element_mode == IVAS_CPE_TD && st->bwidth == SWB ) )
     261             :             {
     262             :                 /* read the SWB TBE/BWE selection bit */
     263     2701128 :                 tmp = get_next_indice( st, 1 );
     264             : 
     265     2701128 :                 if ( tmp )
     266             :                 {
     267      225912 :                     st->extl = SWB_BWE;
     268      225912 :                     st->extl_brate = SWB_BWE_1k6;
     269             :                 }
     270             :                 else
     271             :                 {
     272     2475216 :                     st->extl = SWB_TBE;
     273     2475216 :                     st->extl_brate = SWB_TBE_1k6;
     274     2475216 :                     if ( st->total_brate >= MIN_BRATE_SWB_TBE_2k80 && st->flag_ACELP16k && st->element_mode == IVAS_SCE )
     275             :                     {
     276      485754 :                         st->extl_brate = SWB_TBE_2k8;
     277             :                     }
     278     1989462 :                     else if ( st->tdm_LRTD_flag == 1 && st->element_mode == IVAS_CPE_TD )
     279             :                     {
     280      108627 :                         if ( st->element_brate < IVAS_24k4 )
     281             :                         {
     282        3165 :                             st->extl_brate = SWB_TBE_1k10;
     283             :                         }
     284             :                         else
     285             :                         {
     286      105462 :                             st->extl_brate = SWB_TBE_1k75;
     287             :                         }
     288             :                     }
     289     1880835 :                     else if ( st->total_brate < MIN_BRATE_SWB_TBE_1k60 )
     290             :                     {
     291      618531 :                         st->extl_brate = SWB_TBE_0k95;
     292             :                     }
     293             :                 }
     294             :             }
     295             :             else
     296             :             {
     297       13413 :                 st->extl = WB_BWE;
     298       13413 :                 st->extl_brate = 0;
     299             :             }
     300             : 
     301             :             /* set FB TBE and FB BWE extension layers */
     302     2714541 :             if ( st->bwidth == FB )
     303             :             {
     304      890550 :                 if ( st->extl == SWB_BWE )
     305             :                 {
     306       40269 :                     st->extl = FB_BWE;
     307       40269 :                     st->extl_brate = FB_BWE_1k8;
     308             :                 }
     309      850281 :                 else if ( st->extl == SWB_TBE )
     310             :                 {
     311      839643 :                     st->extl = FB_TBE;
     312      839643 :                     st->extl_brate = FB_TBE_1k8;
     313      839643 :                     if ( st->total_brate >= MIN_BRATE_SWB_TBE_2k80 && st->flag_ACELP16k && st->element_mode == IVAS_SCE )
     314             :                     {
     315      359733 :                         st->extl_brate = FB_TBE_3k0;
     316             :                     }
     317             :                 }
     318             :             }
     319             : 
     320             :             /* set IC-BWE bitrate */
     321     2714541 :             if ( st->element_mode == IVAS_CPE_TD && !( st->bwidth >= SWB && st->tdm_LRTD_flag ) )
     322             :             {
     323        4482 :                 icbwe_brate = STEREO_BITS_ICBWE * FRAMES_PER_SEC;
     324        4482 :                 if ( st->flag_ACELP16k == 0 )
     325             :                 {
     326          63 :                     icbwe_brate = ( STEREO_BITS_ICBWE - STEREO_ICBWE_SPBITS ) * FRAMES_PER_SEC;
     327             :                 }
     328             :             }
     329     2710059 :             else if ( st->element_mode == IVAS_CPE_DFT )
     330             :             {
     331      828933 :                 icbwe_brate = STEREO_BITS_ICBWE_DFT * FRAMES_PER_SEC;
     332      828933 :                 if ( st->flag_ACELP16k == 0 )
     333             :                 {
     334      433101 :                     icbwe_brate = ( STEREO_BITS_ICBWE_DFT - STEREO_ICBWE_SPBITS ) * FRAMES_PER_SEC;
     335             :                 }
     336             :             }
     337             : 
     338     2714541 :             if ( st->element_mode >= IVAS_CPE_DFT && st->core == ACELP_CORE && ( st->extl == SWB_TBE || st->extl == FB_TBE ) && !( st->element_mode == IVAS_CPE_TD && st->tdm_LRTD_flag ) )
     339             :             {
     340      700938 :                 icbwe_brate += STEREO_ICBWE_MSFLAG_BITS * FRAMES_PER_SEC;
     341             :             }
     342             :         }
     343             :     }
     344             : 
     345             :     /* set core bitrate */
     346    39724365 :     st->core_brate = st->total_brate - st->extl_brate - icbwe_brate;
     347             : 
     348             :     /*-----------------------------------------------------------------*
     349             :      * Read transform core (TCX vs. HQ) signaling bit from the bitstream
     350             :      *-----------------------------------------------------------------*/
     351             : 
     352    39724365 :     if ( st->element_mode != IVAS_CPE_MDCT && !( st->idchan == 1 && st->element_mode == IVAS_CPE_TD ) && st->core == HQ_CORE )
     353             :     {
     354     6872907 :         if ( get_next_indice( st, 1 ) )
     355             :         {
     356     6688689 :             st->core = TCX_20_CORE;
     357             :         }
     358             :         else
     359             :         {
     360      184218 :             st->core = HQ_CORE;
     361             :         }
     362             :     }
     363             : 
     364    39724365 :     if ( st->element_mode != IVAS_CPE_MDCT && st->core == TCX_20_CORE )
     365             :     {
     366     6688689 :         st->extl = IGF_BWE;
     367     6688689 :         st->extl_brate = 0;
     368             :     }
     369             : 
     370             :     /*-----------------------------------------------------------------*
     371             :      * Read ACELP->HQ core switching flag
     372             :      *-----------------------------------------------------------------*/
     373             : 
     374    39724365 :     if ( st->core == HQ_CORE || st->core == TCX_20_CORE )
     375             :     {
     376    35755683 :         if ( st->core == HQ_CORE )
     377             :         {
     378             :             /* read ACELP->HQ core switching flag */
     379      184218 :             *core_switching_flag = get_next_indice( st, 1 );
     380             :         }
     381             :         else
     382             :         {
     383    35571465 :             *core_switching_flag = 0;
     384             :         }
     385             : 
     386    35755683 :         if ( *core_switching_flag == 1 )
     387             :         {
     388        3936 :             st->last_core_from_bs = ACELP_CORE;
     389             : 
     390        3936 :             if ( st->core == st->last_core )
     391             :             {
     392             :                 /* A mismatch between the core_switching_flag and the st->core/st->last_core
     393             :                    indicates a frame was lost. If prev_bfi is not set the frame loss
     394             :                    occured during CNG and the prev_bfi needs to be set. */
     395          96 :                 st->prev_bfi = 1;
     396             :             }
     397             :         }
     398             :         else
     399             :         {
     400    35751747 :             st->last_core_from_bs = HQ_CORE; /* Could also be TCX, but it does not make any difference */
     401             :         }
     402             : 
     403    35755683 :         st->last_L_frame_ori = st->last_L_frame;
     404             :     }
     405             : 
     406             :     /*-----------------------------------------------------------------*
     407             :      * Set ACELP frame length
     408             :      *-----------------------------------------------------------------*/
     409             : 
     410    39724365 :     if ( st->core_brate == FRAME_NO_DATA )
     411             :     {
     412             :         /* prevent "L_frame" changes in CNG segments */
     413           0 :         st->L_frame = st->last_L_frame;
     414             :     }
     415    39724365 :     else if ( st->core_brate == SID_2k40 && st->bwidth == WB && st->first_CNG && st->hTdCngDec->act_cnt2 < MIN_ACT_CNG_UPD )
     416             :     {
     417             :         /* prevent "L_frame" changes in SID frame after short segment of active frames */
     418           0 :         st->L_frame = st->last_CNG_L_frame;
     419             :     }
     420    39724365 :     else if ( ( st->core_brate == SID_2k40 && st->total_brate >= ACELP_9k60 && st->bwidth == WB ) || st->flag_ACELP16k )
     421             :     {
     422    35870946 :         st->L_frame = L_FRAME16k;
     423             :     }
     424             :     else
     425             :     {
     426     3853419 :         st->L_frame = L_FRAME;
     427             :     }
     428             : 
     429    39724365 :     if ( st->L_frame == L_FRAME16k )
     430             :     {
     431    35870946 :         st->nb_subfr = NB_SUBFR16k;
     432             :     }
     433             :     else
     434             :     {
     435     3853419 :         st->nb_subfr = NB_SUBFR;
     436             :     }
     437             : 
     438             :     /*-----------------------------------------------------------------*
     439             :      * set inactive coder_type flag in ACELP core
     440             :      *-----------------------------------------------------------------*/
     441             : 
     442    39724365 :     st->inactive_coder_type_flag = 0; /* AVQ by default */
     443             : 
     444    39724365 :     if ( st->total_brate <= MAX_GSC_INACTIVE_BRATE )
     445             :     {
     446     7371741 :         st->inactive_coder_type_flag = 1; /* GSC */
     447             :     }
     448             : 
     449             :     /*-----------------------------------------------------------------*
     450             :      * Reconfigure in case when output_Fs < input_Fs
     451             :      *-----------------------------------------------------------------*/
     452             : 
     453    39724365 :     st->extl_orig = st->extl;
     454    39724365 :     st->extl_brate_orig = st->extl_brate;
     455             : 
     456    39724365 :     if ( st->output_Fs == 16000 && st->L_frame == L_FRAME16k && st->extl != IGF_BWE )
     457             :     {
     458     3395454 :         st->extl = -1;
     459     3395454 :         st->extl_brate = 0;
     460             :     }
     461             : 
     462    39724365 :     if ( st->ini_frame == 0 )
     463             :     {
     464             :         /* avoid switching of internal ACELP Fs in the very first frame */
     465      347343 :         st->last_L_frame = st->L_frame;
     466      347343 :         st->last_core = st->core;
     467      347343 :         st->last_core_brate = st->core_brate;
     468      347343 :         st->last_extl = st->extl;
     469             :     }
     470             : 
     471    39724365 :     return;
     472             : }

Generated by: LCOV version 1.14