LCOV - code coverage report
Current view: top level - lib_dec - ivas_decision_matrix_dec.c (source / functions) Hit Total Coverage
Test: Coverage on main -- merged total coverage @ a21f94bc6bac334fe001a5bad2f7b32b79038097 Lines: 152 155 98.1 %
Date: 2025-11-01 08:50:45 Functions: 1 1 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             : #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    58154734 : 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    58154734 :     icbwe_brate = 0;
      66    58154734 :     st->core = -1;
      67    58154734 :     st->core_brate = 0;
      68    58154734 :     st->extl = -1;
      69    58154734 :     st->extl_brate = 0;
      70    58154734 :     st->ppp_mode_dec = 0;
      71    58154734 :     st->nelp_mode_dec = 0;
      72    58154734 :     st->igf = 0;
      73    58154734 :     st->vbr_hw_BWE_disable_dec = 0;
      74             : 
      75             :     /*-----------------------------------------------------------------*
      76             :      * Read SID signaling bits from the bitstream
      77             :      *-----------------------------------------------------------------*/
      78             : 
      79    58154734 :     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      322965 :         st->core = ACELP_CORE;
      82      322965 :         st->core_brate = st->total_brate;
      83             : 
      84      322965 :         if ( st->total_brate == SID_2k40 && !( st->idchan == 1 && st->element_mode == IVAS_CPE_MDCT ) )
      85             :         {
      86       36585 :             if ( st->element_mode != IVAS_CPE_DFT )
      87             :             {
      88       19103 :                 st->cng_type = get_next_indice( st, 1 );
      89             : 
      90       19103 :                 if ( st->cng_type == FD_CNG )
      91             :                 {
      92       18689 :                     st->bwidth = get_next_indice( st, 2 );
      93             :                 }
      94             :             }
      95       36585 :             if ( get_next_indice( st, 1 ) )
      96             :             {
      97       22055 :                 st->L_frame = L_FRAME16k;
      98             :             }
      99             :             else
     100             :             {
     101       14530 :                 st->L_frame = L_FRAME;
     102             :             }
     103             :         }
     104      286380 :         else if ( st->total_brate == SID_2k40 && st->idchan == 1 && st->element_mode == IVAS_CPE_MDCT && st->cng_sba_flag == 0 )
     105             :         {
     106             :             /* read channel coherence */
     107             : #ifdef NONBE_MDCT_ST_DTX_FIX_SUBOPT_SPATIAL_CNG
     108        4933 :             st->hFdCngDec->hFdCngCom->coherence[0] = (float) get_next_indice( st, 4 ) / 15.f;
     109             : #else
     110             :             st->hFdCngDec->hFdCngCom->coherence = (float) get_next_indice( st, 4 ) / 15.f;
     111             : #endif
     112             : 
     113             :             /* read flag for no side noise shape */
     114        4933 :             st->hFdCngDec->hFdCngCom->no_side_flag = get_next_indice( st, 1 );
     115             :         }
     116             : 
     117      322965 :         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 ) )
     118             :         {
     119      234249 :             st->extl = SWB_CNG;
     120             :         }
     121             : 
     122      322965 :         if ( st->total_brate == FRAME_NO_DATA && st->prev_bfi && !st->bfi && st->L_frame > L_FRAME16k )
     123             :         {
     124           0 :             st->L_frame = st->last_CNG_L_frame;
     125             :         }
     126             : 
     127      322965 :         return;
     128             :     }
     129             : 
     130             :     /*---------------------------------------------------------------------*
     131             :      * ACELP/HQ core selection
     132             :      *---------------------------------------------------------------------*/
     133             : 
     134    57831769 :     if ( st->element_mode == IVAS_CPE_TD && st->idchan == 1 )
     135             :     {
     136             :         /* minimal signaling for the secondary channel, most of the parameters are deduced from the primary channel */
     137       90133 :         st->core = ACELP_CORE;
     138             :     }
     139    57741636 :     else if ( st->element_mode == IVAS_SCE && st->low_rate_mode )
     140             :     {
     141             :         /* ISM Low-rate mode -> always WB, ACELP core, IC coder_type */
     142      800570 :         st->core = ACELP_CORE;
     143             :     }
     144    56941066 :     else if ( st->element_mode == IVAS_CPE_MDCT )
     145             :     {
     146    41475394 :         st->core = TCX_20_CORE;
     147             :     }
     148             :     else
     149             :     {
     150    15465672 :         st->core = ACELP_CORE;
     151             : 
     152    15465672 :         if ( st->element_mode == IVAS_CPE_TD || st->total_brate >= STEREO_TCX_MIN_RATE )
     153             :         {
     154             :             /* ACELP/transform core selection bit */
     155    15323675 :             if ( get_next_indice( st, 1 ) )
     156             :             {
     157    10657287 :                 st->core = HQ_CORE;
     158             :             }
     159             :             else
     160             :             {
     161     4666388 :                 st->core = ACELP_CORE;
     162             :             }
     163             :         }
     164             :     }
     165             : 
     166             :     /*-----------------------------------------------------------------*
     167             :      * Read ACELP signaling bits from the bitstream
     168             :      *-----------------------------------------------------------------*/
     169             : 
     170    57831769 :     if ( st->core == ACELP_CORE )
     171             :     {
     172     5699088 :         if ( st->element_mode == IVAS_SCE && st->low_rate_mode )
     173             :         {
     174             :             /* ISM Low-rate mode */
     175      800570 :             st->bwidth = WB;
     176      800570 :             st->coder_type = INACTIVE;
     177      800570 :             *sharpFlag = 0;
     178             :         }
     179     4898518 :         else if ( st->idchan == 1 && st->element_mode == IVAS_CPE_TD )
     180             :         {
     181       90133 :             *sharpFlag = 0;
     182       90133 :             if ( st->coder_type == GENERIC || st->coder_type == VOICED )
     183             :             {
     184       68195 :                 *sharpFlag = 1;
     185             :             }
     186             : 
     187       90133 :             st->core_brate = st->total_brate;
     188       90133 :             st->codec_mode = MODE1;
     189             : 
     190       90133 :             if ( st->idchan == 1 && ( st->tdm_LRTD_flag == 0 || st->bits_frame_channel < IVAS_16k4 / FRAMES_PER_SEC ) )
     191             :             {
     192        7069 :                 st->bwidth = WB; /* only WB in the secondary channel */
     193             :             }
     194             :         }
     195             :         else
     196             :         {
     197     4808385 :             if ( element_brate < FRMT_SHP_MIN_BRATE_IVAS )
     198             :             {
     199     2311142 :                 st->coder_type = get_next_indice( st, 3 );
     200     2311142 :                 *sharpFlag = 0;
     201             : 
     202     2311142 :                 if ( element_brate < IVAS_24k4 && ( st->coder_type == VOICED || st->coder_type == GENERIC || st->coder_type == TRANSITION ) )
     203             :                 {
     204     1960858 :                     *sharpFlag = 1;
     205             :                 }
     206             :             }
     207             :             else
     208             :             {
     209             :                 /* get coder_type info */
     210     2497243 :                 st->coder_type = get_next_indice( st, 3 );
     211             : 
     212             :                 /* get sharpening flag */
     213     2497243 :                 *sharpFlag = get_next_indice( st, 1 );
     214             :             }
     215             :         }
     216             :     }
     217             : 
     218             :     /*-----------------------------------------------------------------*
     219             :      * Set extension layers
     220             :      *-----------------------------------------------------------------*/
     221             : 
     222    57831769 :     if ( st->core == ACELP_CORE )
     223             :     {
     224     5699088 :         if ( st->bwidth == WB && st->low_rate_mode )
     225             :         {
     226      800570 :             st->extl = WB_BWE;
     227      800570 :             if ( st->total_brate >= MIN_BRATE_WB_BWE )
     228             :             {
     229       21359 :                 st->extl_brate = WB_BWE_0k35;
     230             :             }
     231             :         }
     232     4898518 :         else if ( st->bwidth == WB && ( st->total_brate < MIN_BRATE_WB_BWE || ( st->idchan == 1 && st->element_mode == IVAS_CPE_TD ) ) )
     233             :         {
     234       15150 :             if ( st->vbr_hw_BWE_disable_dec == 0 )
     235             :             {
     236       15150 :                 st->extl = WB_BWE;
     237             :             }
     238             :         }
     239     4883368 :         else if ( st->bwidth == WB && st->total_brate >= MIN_BRATE_WB_BWE && !st->flag_ACELP16k )
     240             :         {
     241             :             /* read the WB TBE/BWE selection bit */
     242      517279 :             if ( get_next_indice( st, 1 ) )
     243             :             {
     244       99791 :                 st->extl = WB_BWE;
     245       99791 :                 st->extl_brate = WB_BWE_0k35;
     246             :             }
     247             :             else
     248             :             {
     249      417488 :                 st->extl = WB_TBE;
     250      417488 :                 if ( st->total_brate < MIN_BRATE_WB_TBE_1k05 || ( st->element_mode == IVAS_CPE_TD && st->total_brate < MIN_TDM_BRATE_WB_TBE_1k05 ) )
     251             :                 {
     252       81886 :                     st->extl_brate = WB_TBE_0k35;
     253             :                 }
     254             :                 else
     255             :                 {
     256      335602 :                     st->extl_brate = WB_TBE_1k05;
     257             :                 }
     258             :             }
     259             :         }
     260     4366089 :         else if ( st->bwidth == SWB || st->bwidth == FB )
     261             :         {
     262     3902444 :             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 ) )
     263             :             {
     264             :                 /* read the SWB TBE/BWE selection bit */
     265     3888643 :                 tmp = get_next_indice( st, 1 );
     266             : 
     267     3888643 :                 if ( tmp )
     268             :                 {
     269      335038 :                     st->extl = SWB_BWE;
     270      335038 :                     st->extl_brate = SWB_BWE_1k6;
     271             :                 }
     272             :                 else
     273             :                 {
     274     3553605 :                     st->extl = SWB_TBE;
     275     3553605 :                     st->extl_brate = SWB_TBE_1k6;
     276     3553605 :                     if ( st->total_brate >= MIN_BRATE_SWB_TBE_2k80 && st->flag_ACELP16k && st->element_mode == IVAS_SCE )
     277             :                     {
     278      722606 :                         st->extl_brate = SWB_TBE_2k8;
     279             :                     }
     280     2830999 :                     else if ( st->tdm_LRTD_flag == 1 && st->element_mode == IVAS_CPE_TD )
     281             :                     {
     282      133481 :                         if ( st->element_brate < IVAS_24k4 )
     283             :                         {
     284        6645 :                             st->extl_brate = SWB_TBE_1k10;
     285             :                         }
     286             :                         else
     287             :                         {
     288      126836 :                             st->extl_brate = SWB_TBE_1k75;
     289             :                         }
     290             :                     }
     291     2697518 :                     else if ( st->total_brate < MIN_BRATE_SWB_TBE_1k60 )
     292             :                     {
     293      986188 :                         st->extl_brate = SWB_TBE_0k95;
     294             :                     }
     295             :                 }
     296             :             }
     297             :             else
     298             :             {
     299       13801 :                 st->extl = WB_BWE;
     300       13801 :                 st->extl_brate = 0;
     301             :             }
     302             : 
     303             :             /* set FB TBE and FB BWE extension layers */
     304     3902444 :             if ( st->bwidth == FB )
     305             :             {
     306     1215442 :                 if ( st->extl == SWB_BWE )
     307             :                 {
     308       70655 :                     st->extl = FB_BWE;
     309       70655 :                     st->extl_brate = FB_BWE_1k8;
     310             :                 }
     311     1144787 :                 else if ( st->extl == SWB_TBE )
     312             :                 {
     313     1134133 :                     st->extl = FB_TBE;
     314     1134133 :                     st->extl_brate = FB_TBE_1k8;
     315     1134133 :                     if ( st->total_brate >= MIN_BRATE_SWB_TBE_2k80 && st->flag_ACELP16k && st->element_mode == IVAS_SCE )
     316             :                     {
     317      517074 :                         st->extl_brate = FB_TBE_3k0;
     318             :                     }
     319             :                 }
     320             :             }
     321             : 
     322             :             /* set IC-BWE bitrate */
     323     3902444 :             if ( st->element_mode == IVAS_CPE_TD && !( st->bwidth >= SWB && st->tdm_LRTD_flag ) )
     324             :             {
     325        4955 :                 icbwe_brate = STEREO_BITS_ICBWE * FRAMES_PER_SEC;
     326        4955 :                 if ( st->flag_ACELP16k == 0 )
     327             :                 {
     328          99 :                     icbwe_brate = ( STEREO_BITS_ICBWE - STEREO_ICBWE_SPBITS ) * FRAMES_PER_SEC;
     329             :                 }
     330             :             }
     331     3897489 :             else if ( st->element_mode == IVAS_CPE_DFT )
     332             :             {
     333     1086662 :                 icbwe_brate = STEREO_BITS_ICBWE_DFT * FRAMES_PER_SEC;
     334     1086662 :                 if ( st->flag_ACELP16k == 0 )
     335             :                 {
     336      614492 :                     icbwe_brate = ( STEREO_BITS_ICBWE_DFT - STEREO_ICBWE_SPBITS ) * FRAMES_PER_SEC;
     337             :                 }
     338             :             }
     339             : 
     340     3902444 :             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 ) )
     341             :             {
     342      918060 :                 icbwe_brate += STEREO_ICBWE_MSFLAG_BITS * FRAMES_PER_SEC;
     343             :             }
     344             :         }
     345             :     }
     346             : 
     347             :     /* set core bitrate */
     348    57831769 :     st->core_brate = st->total_brate - st->extl_brate - icbwe_brate;
     349             : 
     350             :     /*-----------------------------------------------------------------*
     351             :      * Read transform core (TCX vs. HQ) signaling bit from the bitstream
     352             :      *-----------------------------------------------------------------*/
     353             : 
     354    57831769 :     if ( st->element_mode != IVAS_CPE_MDCT && !( st->idchan == 1 && st->element_mode == IVAS_CPE_TD ) && st->core == HQ_CORE )
     355             :     {
     356    10657287 :         if ( get_next_indice( st, 1 ) )
     357             :         {
     358    10426246 :             st->core = TCX_20_CORE;
     359             :         }
     360             :         else
     361             :         {
     362      231041 :             st->core = HQ_CORE;
     363             :         }
     364             :     }
     365             : 
     366    57831769 :     if ( st->element_mode != IVAS_CPE_MDCT && st->core == TCX_20_CORE )
     367             :     {
     368    10426246 :         st->extl = IGF_BWE;
     369    10426246 :         st->extl_brate = 0;
     370             :     }
     371             : 
     372             :     /*-----------------------------------------------------------------*
     373             :      * Read ACELP->HQ core switching flag
     374             :      *-----------------------------------------------------------------*/
     375             : 
     376    57831769 :     if ( st->core == HQ_CORE || st->core == TCX_20_CORE )
     377             :     {
     378    52132681 :         if ( st->core == HQ_CORE )
     379             :         {
     380             :             /* read ACELP->HQ core switching flag */
     381      231041 :             *core_switching_flag = get_next_indice( st, 1 );
     382             :         }
     383             :         else
     384             :         {
     385    51901640 :             *core_switching_flag = 0;
     386             :         }
     387             : 
     388    52132681 :         if ( *core_switching_flag == 1 )
     389             :         {
     390        4896 :             st->last_core_from_bs = ACELP_CORE;
     391             : 
     392        4896 :             if ( st->core == st->last_core )
     393             :             {
     394             :                 /* A mismatch between the core_switching_flag and the st->core/st->last_core
     395             :                    indicates a frame was lost. If prev_bfi is not set the frame loss
     396             :                    occured during CNG and the prev_bfi needs to be set. */
     397         112 :                 st->prev_bfi = 1;
     398             :             }
     399             :         }
     400             :         else
     401             :         {
     402    52127785 :             st->last_core_from_bs = HQ_CORE; /* Could also be TCX, but it does not make any difference */
     403             :         }
     404             : 
     405    52132681 :         st->last_L_frame_ori = st->last_L_frame;
     406             :     }
     407             : 
     408             :     /*-----------------------------------------------------------------*
     409             :      * Set ACELP frame length
     410             :      *-----------------------------------------------------------------*/
     411             : 
     412    57831769 :     if ( st->core_brate == FRAME_NO_DATA )
     413             :     {
     414             :         /* prevent "L_frame" changes in CNG segments */
     415           0 :         st->L_frame = st->last_L_frame;
     416             :     }
     417    57831769 :     else if ( st->core_brate == SID_2k40 && st->bwidth == WB && st->first_CNG && st->hTdCngDec->act_cnt2 < MIN_ACT_CNG_UPD )
     418             :     {
     419             :         /* prevent "L_frame" changes in SID frame after short segment of active frames */
     420           0 :         st->L_frame = st->last_CNG_L_frame;
     421             :     }
     422    57831769 :     else if ( ( st->core_brate == SID_2k40 && st->total_brate >= ACELP_9k60 && st->bwidth == WB ) || st->flag_ACELP16k )
     423             :     {
     424    52226701 :         st->L_frame = L_FRAME16k;
     425             :     }
     426             :     else
     427             :     {
     428     5605068 :         st->L_frame = L_FRAME;
     429             :     }
     430             : 
     431    57831769 :     if ( st->L_frame == L_FRAME16k )
     432             :     {
     433    52226701 :         st->nb_subfr = NB_SUBFR16k;
     434             :     }
     435             :     else
     436             :     {
     437     5605068 :         st->nb_subfr = NB_SUBFR;
     438             :     }
     439             : 
     440             :     /*-----------------------------------------------------------------*
     441             :      * set inactive coder_type flag in ACELP core
     442             :      *-----------------------------------------------------------------*/
     443             : 
     444    57831769 :     st->inactive_coder_type_flag = 0; /* AVQ by default */
     445             : 
     446    57831769 :     if ( st->total_brate <= MAX_GSC_INACTIVE_BRATE )
     447             :     {
     448    10418136 :         st->inactive_coder_type_flag = 1; /* GSC */
     449             :     }
     450             : 
     451             :     /*-----------------------------------------------------------------*
     452             :      * Reconfigure in case when output_Fs < input_Fs
     453             :      *-----------------------------------------------------------------*/
     454             : 
     455    57831769 :     st->extl_orig = st->extl;
     456    57831769 :     st->extl_brate_orig = st->extl_brate;
     457             : 
     458    57831769 :     if ( st->output_Fs == 16000 && st->L_frame == L_FRAME16k && st->extl != IGF_BWE )
     459             :     {
     460     6418989 :         st->extl = -1;
     461     6418989 :         st->extl_brate = 0;
     462             :     }
     463             : 
     464    57831769 :     if ( st->ini_frame == 0 )
     465             :     {
     466             :         /* avoid switching of internal ACELP Fs in the very first frame */
     467      670217 :         st->last_L_frame = st->L_frame;
     468      670217 :         st->last_core = st->core;
     469      670217 :         st->last_core_brate = st->core_brate;
     470      670217 :         st->last_extl = st->extl;
     471             :     }
     472             : 
     473    57831769 :     return;
     474             : }

Generated by: LCOV version 1.14