LCOV - code coverage report
Current view: top level - lib_enc - decision_matrix_enc.c (source / functions) Hit Total Coverage
Test: Coverage on main -- long test vectors @ a21f94bc6bac334fe001a5bad2f7b32b79038097 Lines: 215 225 95.6 %
Date: 2025-11-01 09:50:07 Functions: 4 4 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 "prot.h"
      44             : #include "stat_enc.h"
      45             : #include "stat_dec.h"
      46             : #include "rom_com.h"
      47             : #include "wmc_auto.h"
      48             : 
      49             : 
      50             : /*-----------------------------------------------------------------*
      51             :  * decision_matrix_enc()
      52             :  *
      53             :  * Select operating point (combination of technologies) based on input signal properties and command-line parameters:
      54             :  *
      55             :  *             7.20        8.00        9.60        13.20        16.40         24.40            32               48               64               96      128
      56             :  *  Mode       1           1           2           1            2             2                1                2                1                2       2
      57             :  * ----------------------------------------------------------------------------------------------------------------------------------------------------------------
      58             :  *  NB
      59             :  *  speech     ACELP@12k8  ACELP@12k8  ACELP@12k8  ACELP@12k8
      60             :  *  audio      LR MDCT     LR MDCT     TCX         LR MDCT
      61             :  *  inactive   GSC@12k8    GSC@12k8    TCX         GSC@12k8
      62             :  * ----------------------------------------------------------------------------------------------------------------------------------------------------------------
      63             :  *  WB
      64             :  *  speech     ACELP@12k8  ACELP@12k8  ACELP@12k8  ACELP@12k8   ACELP@16k     ACELP@16k        ACELP@16k        TCX              ACELP@16k        TCX     TCX
      65             :  *             +0b WB BWE  +0b WB BWE  +TD WB BWE  +TD WB BWE
      66             :  *  audio      GSC@12k8    GSC@12k8    TCX         LR MDCT      TCX           TCX              HQ               TCX              HQ               TCX     TCX
      67             :  *             +0b WB BWE  +0b WB BWE  +IGF
      68             :  *  inactive   GSC@12k8    GSC@12k8    TCX         GSC@12k8     TCX           TCX              AVQ@16k          TCX              AVQ@16k          TCX     TCX
      69             :  *             +0b WB BWE  +0b WB BWE  +IGF        +FD WB BWE
      70             :  * ----------------------------------------------------------------------------------------------------------------------------------------------------------------
      71             :  *  SWB
      72             :  *  speech                                         ACELP@12k8   ACELP@16k     ACELP@16k        ACELP@16k        TCX              ACELP@16k        TCX     TCX
      73             :  *                                                 +TD SWB BWE  +TD SWB BWE   +TD SWB BWE      +TD SWB BWE      +IGF             +HR SWB BWE
      74             :  *  audio                                          LR MDCT/GSC  TCX           TCX              HQ               TCX              HQ               TCX     TCX
      75             :  *                                                 +FD SWB BWE  +IGF          +IGF             +FD SWB BWE      +IGF
      76             :  *  inactive                                       GSC@12k8     TCX           TCX              AVQ@16k          TCX              AVQ@16k          TCX     TCX
      77             :  *                                                 +FD SWB BWE  +IGF          +IGF             +FD SWB BWE      +IGF             +HR SWB BWE
      78             :  * ----------------------------------------------------------------------------------------------------------------------------------------------------------------
      79             :  *  FB
      80             :  *  speech                                                      ACELP@16k     ACELP@16k        ACELP@16k        TCX              ACELP@16k        TCX     TCX
      81             :  *                                                              +TD FB BWE    +TD FB BWE       +TD FB BWE       +IGF             +HR FB BWE
      82             :  *  audio                                                       TCX           TCX              HQ               TCX              HQ               TCX     TCX
      83             :  *                                                              +IGF          +IGF             +FD FB BWE       +IGF
      84             :  *  inactive                                                    TCX           TCX              AVQ@16k          TCX              AVQ@16k          TCX     TCX
      85             :  *                                                              +IGF          +IGF             +FD FB BWE       +IGF             +HR FB BWE
      86             :  * -----------------------------------------------------------------------------------------------------------------------------------------------------------------
      87             :  *
      88             :  * Note: the GSC technology is part of the ACELP core as AUDIO coder_type (it is used also at 13.2 for SWB unvoiced noisy speech)
      89             :  * Note2: FB processing is optional and is activated via "-band FB" option on the encoder command line
      90             :  * Note3: NB (0-4kHz), WB (0-8kHz), SWB (0-16kHz), FB (0-20kHz)
      91             :  *
      92             :  * Signalling of modes (x marks a mode that must be signalled in the bitstream)
      93             :  *
      94             :  *                     7.20           8.00           9.6            13.2           16.4           24.4           32             48             64
      95             :  *                     NB WB SWB FB   NB WB SWB FB   NB WB SWB FB   NB WB SWB FB   NB WB SWB FB   NB WB SWB FB   NB WB SWB FB   NB WB SWB FB   NB WB SWB FB
      96             :  * GC, 12k8            x  x           x  x           x  x           x  x   x       x  x   x       x
      97             :  * UC, 12k8            x  x           x  x           x  x
      98             :  * VC, 12k8            x  x           x  x           x  x           x  x   x       x  x   x       x
      99             :  * TC, 12k8            x  x           x  x           x  x           x  x   x       x  x   x       x
     100             :  * GC, 16k                                                                                           x   x   x      x   x   x      x   x   x      x   x   x
     101             :  * TC, 16k                                                                                           x   x   x      x   x   x      x   x   x      x   x   x
     102             :  * AC(GSC)             x  x           x  x           x  x           x  x   x       x  x   x       x
     103             :  * IC                  x  x           x  x           x  x           x  x   x       x  x   x       x  x   x   x      x   x   x      x   x   x      x   x   x
     104             :  *
     105             :  * GC, 12k8, FS        x  x           x  x           x  x           x  x   x       x  x   x       x
     106             :  * GC, 16k, FS                                                                                       x   x   x      x   x   x      x   x   x          x   x
     107             :  * VC, 12k8, FS                                                     x  x   x       x  x   x       x
     108             :  * TC, 12k8, FS                                                                                   x
     109             :  * TC, 16k, FS                                                                                       x   x   x      x   x   x      x   x   x          x   x
     110             :  *
     111             :  * LR MDCT             x              x              x              x  x   x   x   x  x   x   x
     112             :  *
     113             :  *-----------------------------------------------------------------*/
     114             : 
     115       50033 : void decision_matrix_enc(
     116             :     Encoder_State *st,    /* i  : encoder state structure                 */
     117             :     int16_t *hq_core_type /* o  : HQ core type                            */
     118             : )
     119             : {
     120             :     /* initialization */
     121       50033 :     st->core = -1;
     122       50033 :     st->extl = -1;
     123       50033 :     st->extl_brate = 0;
     124       50033 :     *hq_core_type = -1;
     125       50033 :     st->igf = 0;
     126             : 
     127             :     /* SID and FRAME_NO_DATA frames */
     128       50033 :     if ( st->Opt_DTX_ON && ( st->core_brate == SID_2k40 || st->core_brate == FRAME_NO_DATA ) )
     129             :     {
     130        2028 :         st->core = ACELP_CORE;
     131             : 
     132        2028 :         if ( st->input_Fs >= 32000 && st->bwidth >= SWB )
     133             :         {
     134           0 :             st->extl = SWB_CNG;
     135             :         }
     136             : 
     137        2028 :         st->rf_mode = 0;
     138             : 
     139        2028 :         return;
     140             :     }
     141             : 
     142       48005 :     st->core_brate = 0;
     143             : 
     144             :     /* SC-VBR */
     145       48005 :     if ( st->Opt_SC_VBR )
     146             :     {
     147             :         /* SC-VBR */
     148        2190 :         st->core = ACELP_CORE;
     149        2190 :         st->core_brate = ACELP_7k20;
     150        2190 :         st->total_brate = ACELP_7k20;
     151             : 
     152        2190 :         if ( st->hSC_VBR->ppp_mode == 1 )
     153             :         {
     154             :             /* PPP mode */
     155         372 :             st->core_brate = PPP_NELP_2k80;
     156             :         }
     157        1818 :         else if ( ( ( st->coder_type == UNVOICED || st->coder_type == TRANSITION ) && !st->sp_aud_decision1 ) || st->bwidth != NB )
     158             :         {
     159        1183 :             if ( st->coder_type == UNVOICED && st->vad_flag == 1 && ( ( st->last_bwidth >= SWB && st->last_Opt_SC_VBR ) || st->last_bwidth < SWB ) && ( st->last_core != HQ_CORE || st->bwidth != NB ) )
     160             :             {
     161             :                 /* NELP mode */
     162         268 :                 st->hSC_VBR->nelp_mode = 1;
     163         268 :                 st->core_brate = PPP_NELP_2k80;
     164             :             }
     165         915 :             else if ( st->coder_type == TRANSITION || ( st->coder_type == UNVOICED && st->hSC_VBR->nelp_mode != 1 ) || ( ( st->coder_type == AUDIO || st->coder_type == INACTIVE ) && st->bwidth != NB ) )
     166             :             {
     167             :                 /* silence portions */
     168         476 :                 st->core_brate = ACELP_8k00;
     169         476 :                 st->total_brate = ACELP_8k00;
     170             :             }
     171             :         }
     172             : 
     173             :         /* set inactive coder_type flag in ACELP core to GSC */
     174        2190 :         st->inactive_coder_type_flag = 1;
     175             : 
     176        2190 :         return;
     177             :     }
     178             : 
     179             :     /*---------------------------------------------------------------------*
     180             :      * NB
     181             :      *---------------------------------------------------------------------*/
     182             : 
     183       45815 :     else if ( st->bwidth == NB )
     184             :     {
     185        3818 :         st->core = ACELP_CORE;
     186             : 
     187             : #ifdef DEBUGGING
     188             :         if ( st->total_brate >= HQCORE_NB_MIN_RATE && ( st->force == FORCE_MUSIC || ( st->force == -1 && st->sp_aud_decision1 == 1 ) ) )
     189             :         {
     190             :             st->core = HQ_CORE;
     191             :         }
     192             : #else
     193        3818 :         if ( st->total_brate >= HQCORE_NB_MIN_RATE && st->sp_aud_decision1 == 1 )
     194             :         {
     195        1064 :             st->core = HQ_CORE;
     196             :         }
     197             : #endif
     198             :     }
     199             : 
     200             :     /*---------------------------------------------------------------------*
     201             :      * WB
     202             :      *---------------------------------------------------------------------*/
     203             : 
     204       41997 :     else if ( st->bwidth == WB )
     205             :     {
     206       10113 :         st->core = ACELP_CORE;
     207             : 
     208             : #ifdef DEBUGGING
     209             :         if ( ( st->total_brate >= HQCORE_WB_MIN_RATE && ( st->force == FORCE_MUSIC || ( st->force == -1 && st->sp_aud_decision1 == 1 ) ) ) || st->total_brate >= HQ_96k )
     210             : #else
     211       10113 :         if ( ( st->total_brate >= HQCORE_WB_MIN_RATE && st->sp_aud_decision1 == 1 ) || st->total_brate >= HQ_96k )
     212             : #endif
     213             :         {
     214        1535 :             st->core = HQ_CORE;
     215             :         }
     216             :         else
     217             :         {
     218        8578 :             if ( st->bwidth == WB && st->total_brate < ACELP_9k60 )
     219             :             {
     220        4238 :                 st->extl = WB_BWE;
     221             :             }
     222        4340 :             else if ( st->bwidth == WB && st->total_brate >= ACELP_9k60 && st->total_brate <= ACELP_16k40 )
     223             :             {
     224             :                 /* Note: WB BWE is used exceptionally at 13.2 kbps if GSC is selected instead of LR-MDCT */
     225        1705 :                 if ( st->sp_aud_decision1 == 1 || st->coder_type == INACTIVE || ( st->sp_aud_decision1 == 0 && st->sp_aud_decision2 == 1 ) )
     226             :                 {
     227         251 :                     st->extl = WB_BWE;
     228         251 :                     st->extl_brate = WB_BWE_0k35;
     229             :                 }
     230             :                 else
     231             :                 {
     232        1454 :                     st->extl = WB_TBE;
     233        1454 :                     st->extl_brate = WB_TBE_1k05;
     234             :                 }
     235             :             }
     236             :         }
     237             :     }
     238             : 
     239             :     /*---------------------------------------------------------------------*
     240             :      * SWB and FB
     241             :      *---------------------------------------------------------------------*/
     242             : 
     243       31884 :     else if ( st->bwidth == SWB || st->bwidth == FB )
     244             :     {
     245             : #ifdef DEBUGGING
     246             :         if ( ( st->total_brate >= HQCORE_WB_MIN_RATE && ( st->force == FORCE_MUSIC || ( st->force == -1 && st->sp_aud_decision1 == 1 ) ) ) || st->total_brate >= HQ_96k )
     247             : #else
     248       31884 :         if ( ( st->total_brate >= HQCORE_SWB_MIN_RATE && st->sp_aud_decision1 == 1 ) || st->total_brate >= HQ_96k )
     249             : #endif
     250             :         {
     251       11139 :             st->core = HQ_CORE;
     252             :         }
     253             :         else
     254             :         {
     255       20745 :             st->core = ACELP_CORE;
     256             : 
     257       20745 :             if ( st->total_brate >= ACELP_13k20 && st->total_brate < ACELP_48k )
     258             :             {
     259             :                 /* Note: SWB BWE is not used in case of GSC noisy speech */
     260             :                 /* Note: SWB BWE is used exceptionally at 13.2 kbps if GSC is selected instead of LR-MDCT */
     261       12882 :                 if ( ( st->sp_aud_decision1 == 1 || st->coder_type == INACTIVE || ( st->sp_aud_decision1 == 0 && st->sp_aud_decision2 == 1 ) ) && !st->GSC_noisy_speech )
     262             :                 {
     263        1368 :                     st->extl = SWB_BWE;
     264        1368 :                     st->extl_brate = SWB_BWE_1k6;
     265             : 
     266        1368 :                     if ( st->bwidth == FB && st->total_brate >= ACELP_24k40 )
     267             :                     {
     268           0 :                         st->extl = FB_BWE;
     269           0 :                         st->extl_brate = FB_BWE_1k8;
     270             :                     }
     271             :                 }
     272             :                 else
     273             :                 {
     274       11514 :                     st->extl = SWB_TBE;
     275       11514 :                     st->extl_brate = SWB_TBE_1k6;
     276             : 
     277       11514 :                     if ( st->total_brate >= ACELP_24k40 )
     278             :                     {
     279        2161 :                         st->extl_brate = SWB_TBE_2k8;
     280             :                     }
     281             : 
     282       11514 :                     if ( st->bwidth == FB && st->total_brate >= ACELP_24k40 )
     283             :                     {
     284         672 :                         st->extl = FB_TBE;
     285         672 :                         st->extl_brate = FB_TBE_3k0;
     286             :                     }
     287             :                 }
     288             :             }
     289        7863 :             else if ( st->total_brate >= ACELP_48k )
     290             :             {
     291        7863 :                 st->extl = SWB_BWE_HIGHRATE;
     292        7863 :                 st->extl_brate = SWB_BWE_16k;
     293             : 
     294        7863 :                 if ( st->bwidth == FB )
     295             :                 {
     296         669 :                     st->extl = FB_BWE_HIGHRATE;
     297             :                 }
     298             :             }
     299             :         }
     300             :     }
     301             : 
     302             :     /*-----------------------------------------------------------------*
     303             :      * Set HQ core type
     304             :      *-----------------------------------------------------------------*/
     305             : 
     306       45815 :     if ( st->core == HQ_CORE )
     307             :     {
     308       13738 :         *hq_core_type = NORMAL_HQ_CORE;
     309             : 
     310       13738 :         if ( ( st->bwidth == SWB || st->bwidth == WB ) && st->total_brate <= LRMDCT_CROSSOVER_POINT )
     311             :         {
     312             :             /* note that FB (bitrate >= 24400 bps) is always coded with NORMAL_HQ_CORE */
     313        5789 :             *hq_core_type = LOW_RATE_HQ_CORE;
     314             :         }
     315        7949 :         else if ( st->bwidth == NB )
     316             :         {
     317        1064 :             *hq_core_type = LOW_RATE_HQ_CORE;
     318             :         }
     319             :     }
     320             : 
     321             :     /* set core bitrate */
     322       45815 :     st->core_brate = st->total_brate - st->extl_brate;
     323             : 
     324       45815 :     if ( st->ini_frame == 0 )
     325             :     {
     326             :         /* avoid switching in the very first frame */
     327          27 :         st->last_core = st->core;
     328          27 :         st->last_core_brate = st->core_brate;
     329          27 :         st->last_extl = st->extl;
     330             :     }
     331             : 
     332             :     /*-----------------------------------------------------------------*
     333             :      * set inactive coder_type flag in ACELP core
     334             :      *-----------------------------------------------------------------*/
     335             : 
     336       45815 :     st->inactive_coder_type_flag = 0; /* AVQ by default */
     337       45815 :     if ( st->total_brate <= MAX_GSC_INACTIVE_BRATE )
     338             :     {
     339       26271 :         st->inactive_coder_type_flag = 1; /* GSC */
     340             :     }
     341             : 
     342       45815 :     return;
     343             : }
     344             : 
     345             : 
     346             : /*---------------------------------------------------------------------*
     347             :  * signaling_mode1_tcx20_enc()
     348             :  *
     349             :  * write MODE1 TCX20 signaling information into the bitstream
     350             :  *---------------------------------------------------------------------*/
     351             : 
     352       10246 : int16_t signaling_mode1_tcx20_enc(
     353             :     Encoder_State *st, /* i/o: encoder state structure   */
     354             :     const int16_t push /* i  : flag to push indice       */
     355             : )
     356             : {
     357             :     int16_t num_bits;
     358             :     int16_t nBits, idx, start_idx;
     359       10246 :     BSTR_ENC_HANDLE hBstr = st->hBstr;
     360             : 
     361       10246 :     assert( st->core == TCX_20_CORE );
     362             : 
     363       10246 :     num_bits = 0;
     364             : 
     365             :     /* Use ACELP signaling for LR MDCT */
     366       10246 :     if ( st->total_brate <= ACELP_16k40 )
     367             :     {
     368             :         /* find the section in the ACELP signaling table corresponding to bitrate */
     369        9994 :         idx = 0;
     370      349790 :         while ( acelp_sig_tbl[idx] != st->total_brate )
     371             :         {
     372      339796 :             idx++;
     373             :         }
     374             : 
     375             :         /* retrieve the number of bits for signaling */
     376        9994 :         nBits = (int16_t) acelp_sig_tbl[++idx];
     377             : 
     378             :         /* retrieve the signaling index */
     379        9994 :         start_idx = ++idx;
     380      316834 :         while ( acelp_sig_tbl[idx] != SIG2IND( LR_MDCT, st->bwidth, 0, 0 ) )
     381             :         {
     382      306840 :             idx++;
     383             :         }
     384             : 
     385        9994 :         num_bits += nBits;
     386        9994 :         if ( push )
     387             :         {
     388        4997 :             push_indice( hBstr, IND_ACELP_SIGNALLING, idx - start_idx, nBits );
     389             :         }
     390             : 
     391             :         /* HQ/TCX core switching flag */
     392        9994 :         ++num_bits;
     393        9994 :         if ( push )
     394             :         {
     395        4997 :             push_indice( hBstr, IND_MDCT_CORE, 1, 1 );
     396             :         }
     397             :     }
     398             :     else
     399             :     {
     400         252 :         if ( st->core_brate <= ACELP_64k )
     401             :         {
     402             :             /* write ACELP/HQ core indication flag */
     403         252 :             ++num_bits;
     404         252 :             if ( push )
     405             :             {
     406         126 :                 push_indice( hBstr, IND_CORE, 1, 1 );
     407             :             }
     408             :         }
     409             : 
     410             :         /* HQ/TCX core switching flag */
     411         252 :         ++num_bits;
     412         252 :         if ( push )
     413             :         {
     414         126 :             push_indice( hBstr, IND_MDCT_CORE, 1, 1 );
     415             :         }
     416             : 
     417         252 :         num_bits += 2;
     418         252 :         if ( push )
     419             :         {
     420             :             /* write band-width (needed for different I/O sampling rate support) */
     421         126 :             if ( st->bwidth == NB )
     422             :             {
     423           0 :                 push_indice( hBstr, IND_HQ_BWIDTH, 0, 2 );
     424             :             }
     425         126 :             else if ( st->bwidth == WB )
     426             :             {
     427           0 :                 push_indice( hBstr, IND_HQ_BWIDTH, 1, 2 );
     428             :             }
     429         126 :             else if ( st->bwidth == SWB )
     430             :             {
     431         103 :                 push_indice( hBstr, IND_HQ_BWIDTH, 2, 2 );
     432             :             }
     433             :             else /* st->bwidth == FB */
     434             :             {
     435          23 :                 push_indice( hBstr, IND_HQ_BWIDTH, 3, 2 );
     436             :             }
     437             :         }
     438             :     }
     439             : 
     440       10246 :     return num_bits;
     441             : }
     442             : 
     443             : 
     444             : /*---------------------------------------------------------------------*
     445             :  * signaling_enc()
     446             :  *
     447             :  * write signaling information into the bitstream
     448             :  *---------------------------------------------------------------------*/
     449             : 
     450       45917 : void signaling_enc(
     451             :     Encoder_State *st /* i  : encoder state structure   */
     452             : )
     453             : {
     454             :     int16_t nBits, idx, start_idx;
     455             :     int32_t total_brate_temp;
     456             :     int16_t sig;
     457       45917 :     BSTR_ENC_HANDLE hBstr = st->hBstr;
     458             : 
     459       45917 :     if ( st->mdct_sw == MODE2 )
     460             :     {
     461             : 
     462         722 :         assert( !st->tcxonly );
     463         722 :         assert( st->core == HQ_CORE );
     464             : 
     465         722 :         push_next_indice( hBstr, 1, 1 ); /* TCX */
     466         722 :         push_next_indice( hBstr, 1, 1 ); /* HQ_CORE */
     467             : 
     468             :         /* write ACELP->HQ core switching flag */
     469         722 :         if ( st->last_core == ACELP_CORE || st->last_core == AMR_WB_CORE )
     470             :         {
     471          50 :             push_indice( hBstr, IND_HQ_SWITCHING_FLG, 1, 1 );
     472             : 
     473             :             /* write ACELP L_frame info */
     474          50 :             if ( st->last_L_frame == L_FRAME )
     475             :             {
     476           0 :                 push_indice( hBstr, IND_LAST_L_FRAME, 0, 1 );
     477             :             }
     478             :             else
     479             :             {
     480          50 :                 push_indice( hBstr, IND_LAST_L_FRAME, 1, 1 );
     481             :             }
     482             :         }
     483             :         else
     484             :         {
     485         672 :             push_indice( hBstr, IND_HQ_SWITCHING_FLG, 0, 1 );
     486             :         }
     487             : 
     488         722 :         return;
     489             :     }
     490             : 
     491       45195 :     if ( st->core == ACELP_CORE )
     492             :     {
     493             :         int16_t ppp_mode, nelp_mode;
     494             : 
     495       36580 :         if ( st->Opt_SC_VBR )
     496             :         {
     497        2475 :             ppp_mode = st->hSC_VBR->ppp_mode;
     498        2475 :             nelp_mode = st->hSC_VBR->nelp_mode;
     499             :         }
     500             :         else
     501             :         {
     502       34105 :             ppp_mode = 0;
     503       34105 :             nelp_mode = 0;
     504             :         }
     505             : 
     506       36580 :         if ( ppp_mode == 1 || nelp_mode == 1 )
     507             :         {
     508             :             /* 1 bit to distinguish between 2.8kbps PPP/NELP frame and SID frame */
     509         640 :             push_indice( hBstr, IND_CORE, 0, 1 );
     510             : 
     511             :             /* SC-VBR: 0 - PPP_NB, 1 - PPP_WB, 2 - NELP_NB, 3 - NELP_WB */
     512         640 :             if ( st->coder_type == VOICED && st->bwidth == NB && ppp_mode == 1 )
     513             :             {
     514         176 :                 push_indice( hBstr, IND_PPP_NELP_MODE, 0, 2 );
     515             :             }
     516         464 :             else if ( st->coder_type == VOICED && st->bwidth != NB && ppp_mode == 1 )
     517             :             {
     518         196 :                 push_indice( hBstr, IND_PPP_NELP_MODE, 1, 2 );
     519             :             }
     520         268 :             else if ( st->coder_type == UNVOICED && st->bwidth == NB && nelp_mode == 1 )
     521             :             {
     522         152 :                 push_indice( hBstr, IND_PPP_NELP_MODE, 2, 2 );
     523             :             }
     524         116 :             else if ( st->coder_type == UNVOICED && st->bwidth != NB && nelp_mode == 1 )
     525             :             {
     526         116 :                 push_indice( hBstr, IND_PPP_NELP_MODE, 3, 2 );
     527             :             }
     528             :         }
     529       35940 :         else if ( st->core_brate != SID_2k40 && st->core_brate != FRAME_NO_DATA )
     530             :         {
     531             :             /* write the ACELP/HQ core selection bit */
     532       33912 :             if ( st->total_brate >= ACELP_24k40 )
     533             :             {
     534       12659 :                 push_indice( hBstr, IND_CORE, 0, 1 );
     535             :             }
     536             : 
     537             :             /* find the section in the ACELP signaling table corresponding to bitrate */
     538       33912 :             idx = 0;
     539      173787 :             while ( idx < MAX_ACELP_SIG )
     540             :             {
     541      173787 :                 if ( st->total_brate <= brate_tbl[idx] )
     542             :                 {
     543       33912 :                     break;
     544             :                 }
     545      139875 :                 idx++;
     546             :             }
     547       33912 :             total_brate_temp = brate_tbl[idx];
     548             : 
     549       33912 :             idx = 0;
     550     1581252 :             while ( acelp_sig_tbl[idx] != total_brate_temp )
     551             :             {
     552     1547340 :                 idx++;
     553             :             }
     554             : 
     555             :             /* retrieve the number of bits for signaling */
     556       33912 :             nBits = (int16_t) acelp_sig_tbl[++idx];
     557             : 
     558             :             /* retrieve the signaling index */
     559       33912 :             start_idx = ++idx;
     560       33912 :             if ( st->element_mode == IVAS_CPE_TD && st->bwidth == SWB && st->total_brate <= ACELP_9k60 )
     561             :             {
     562             :                 /* patch to signal SWB as NB in Stereo */
     563           0 :                 sig = SIG2IND( st->coder_type, NB, st->sharpFlag, st->rf_mode );
     564             :             }
     565             :             else
     566             :             {
     567       33912 :                 sig = SIG2IND( st->coder_type, st->bwidth, st->sharpFlag, st->rf_mode );
     568             :             }
     569             : 
     570      260632 :             while ( acelp_sig_tbl[idx] != sig )
     571             :             {
     572      226720 :                 idx++;
     573             :             }
     574             : 
     575       33912 :             push_indice( hBstr, IND_ACELP_SIGNALLING, idx - start_idx, nBits );
     576             :         }
     577             : 
     578             :         /* write extension layer flag to distinguish between TBE (0) and BWE (1) */
     579       36580 :         if ( st->extl_brate > 0 )
     580             :         {
     581       22450 :             if ( st->extl == WB_TBE || st->extl == SWB_TBE || st->extl == FB_TBE )
     582             :             {
     583       12968 :                 push_indice( hBstr, IND_BWE_FLAG, 0, 1 );
     584             :             }
     585        9482 :             else if ( st->extl == WB_BWE || st->extl == SWB_BWE || st->extl == FB_BWE )
     586             :             {
     587        1619 :                 push_indice( hBstr, IND_BWE_FLAG, 1, 1 );
     588             :             }
     589             :         }
     590             :     }
     591             :     else /* HQ core */
     592             :     {
     593             :         /* write ACELP->HQ core switching flag */
     594        8615 :         if ( st->last_core == ACELP_CORE || st->last_core == AMR_WB_CORE )
     595             :         {
     596         244 :             push_indice( hBstr, IND_HQ_SWITCHING_FLG, 1, 1 );
     597             : 
     598             :             /* write ACELP L_frame info */
     599         244 :             if ( st->last_L_frame == L_FRAME )
     600             :             {
     601          64 :                 push_indice( hBstr, IND_LAST_L_FRAME, 0, 1 );
     602             :             }
     603             :             else
     604             :             {
     605         180 :                 push_indice( hBstr, IND_LAST_L_FRAME, 1, 1 );
     606             :             }
     607             :         }
     608             :         else
     609             :         {
     610        8371 :             push_indice( hBstr, IND_HQ_SWITCHING_FLG, 0, 1 );
     611             :         }
     612             : 
     613             :         /* HQ/TCX core switching flag */
     614        8615 :         push_indice( hBstr, IND_MDCT_CORE, 0, 1 );
     615             : 
     616             :         /* Use ACELP signaling for LR MDCT */
     617        8615 :         if ( st->total_brate <= ACELP_16k40 )
     618             :         {
     619             :             /* find the section in the ACELP signaling table corresponding to bitrate */
     620        1856 :             idx = 0;
     621       55474 :             while ( acelp_sig_tbl[idx] != st->total_brate )
     622             :             {
     623       53618 :                 idx++;
     624             :             }
     625             : 
     626             :             /* retrieve the number of bits for signaling */
     627        1856 :             nBits = (int16_t) acelp_sig_tbl[++idx];
     628             : 
     629             :             /* retrieve the signaling index */
     630        1856 :             start_idx = ++idx;
     631       54439 :             while ( acelp_sig_tbl[idx] != SIG2IND( LR_MDCT, st->bwidth, 0, 0 ) )
     632             :             {
     633       52583 :                 idx++;
     634             :             }
     635             : 
     636        1856 :             push_indice( hBstr, IND_ACELP_SIGNALLING, idx - start_idx, nBits );
     637             :         }
     638             :         else
     639             :         {
     640        6759 :             if ( st->core_brate <= ACELP_64k )
     641             :             {
     642             :                 /* write ACELP/HQ core indication flag */
     643        6759 :                 push_indice( hBstr, IND_CORE, 1, 1 );
     644             :             }
     645             : 
     646             :             /* write band-width (needed for different I/O sampling rate support) */
     647        6759 :             if ( st->bwidth == NB )
     648             :             {
     649           0 :                 push_indice( hBstr, IND_HQ_BWIDTH, 0, 2 );
     650             :             }
     651        6759 :             else if ( st->bwidth == WB )
     652             :             {
     653        1408 :                 push_indice( hBstr, IND_HQ_BWIDTH, 1, 2 );
     654             :             }
     655        5351 :             else if ( st->bwidth == SWB )
     656             :             {
     657        4715 :                 push_indice( hBstr, IND_HQ_BWIDTH, 2, 2 );
     658             :             }
     659             :             else /* st->bwidth == FB */
     660             :             {
     661         636 :                 push_indice( hBstr, IND_HQ_BWIDTH, 3, 2 );
     662             :             }
     663             :         }
     664             :     }
     665             : 
     666       45195 :     return;
     667             : }
     668             : 
     669             : /*---------------------------------------------------------------------*
     670             :  * signaling_enc_rf()
     671             :  *
     672             :  * write channel-aware signaling information into the bitstream
     673             :  *---------------------------------------------------------------------*/
     674             : 
     675    13965052 : void signaling_enc_rf(
     676             :     Encoder_State *st /* i/o: encoder state structure */
     677             : )
     678             : {
     679             :     int16_t i, sfr;
     680    13965052 :     RF_ENC_HANDLE hRF = st->hRF;
     681             : 
     682             :     /* write partial copy into bitstream */
     683    13965052 :     if ( st->rf_mode == 1 )
     684             :     {
     685           0 :         enc_prm_rf( st, hRF->rf_indx_frametype[st->rf_fec_offset], st->rf_fec_offset );
     686           0 :         hRF->rf_indx_tbeGainFr[0] = hRF->RF_bwe_gainFr_ind;
     687             :     }
     688             : 
     689    13965052 :     if ( hRF != NULL )
     690             :     {
     691             :         /* Shift the RF indices such that the partial copy associated with
     692             :            (n-fec_offset)th frame is included in the bitstream in nth frame. */
     693      167716 :         for ( i = st->rf_fec_offset; i >= 0; i-- )
     694             :         {
     695             :             /* RF frame type */
     696       83858 :             hRF->rf_indx_frametype[i + 1] = hRF->rf_indx_frametype[i];
     697             : 
     698             :             /* RF target bits buffer */
     699       83858 :             hRF->rf_targetbits_buff[i + 1] = hRF->rf_targetbits_buff[i];
     700             : 
     701             :             /* lsf indx */
     702       83858 :             hRF->rf_indx_lsf[i + 1][0] = hRF->rf_indx_lsf[i][0];
     703       83858 :             hRF->rf_indx_lsf[i + 1][1] = hRF->rf_indx_lsf[i][1];
     704       83858 :             hRF->rf_indx_lsf[i + 1][2] = hRF->rf_indx_lsf[i][2];
     705             : 
     706             :             /* ES pred energy */
     707       83858 :             hRF->rf_indx_EsPred[i + 1] = hRF->rf_indx_EsPred[i];
     708             : 
     709             :             /* LTF mode, sfr params: pitch, fcb and gain */
     710      446200 :             for ( sfr = 0; sfr < st->nb_subfr; sfr++ )
     711             :             {
     712      362342 :                 hRF->rf_indx_ltfMode[i + 1][sfr] = hRF->rf_indx_ltfMode[i][sfr];
     713      362342 :                 hRF->rf_indx_pitch[i + 1][sfr] = hRF->rf_indx_pitch[i][sfr];
     714      362342 :                 hRF->rf_indx_fcb[i + 1][sfr] = hRF->rf_indx_fcb[i][sfr];
     715      362342 :                 hRF->rf_indx_gain[i + 1][sfr] = hRF->rf_indx_gain[i][sfr];
     716             :             }
     717             : 
     718             :             /* shift the nelp indices */
     719       83858 :             hRF->rf_indx_nelp_iG1[i + 1] = hRF->rf_indx_nelp_iG1[i];
     720       83858 :             hRF->rf_indx_nelp_iG2[i + 1][0] = hRF->rf_indx_nelp_iG2[i][0];
     721       83858 :             hRF->rf_indx_nelp_iG2[i + 1][1] = hRF->rf_indx_nelp_iG2[i][1];
     722       83858 :             hRF->rf_indx_nelp_fid[i + 1] = hRF->rf_indx_nelp_fid[i];
     723             : 
     724             :             /* tbe gain Fr shift */
     725       83858 :             hRF->rf_indx_tbeGainFr[i + 1] = hRF->rf_indx_tbeGainFr[i];
     726       83858 :             hRF->rf_clas[i + 1] = hRF->rf_clas[i];
     727       83858 :             hRF->rf_gain_tcx[i + 1] = hRF->rf_gain_tcx[i];
     728       83858 :             hRF->rf_tcxltp_param[i + 1] = hRF->rf_tcxltp_param[i];
     729             :         }
     730             :     }
     731             : 
     732    13965052 :     return;
     733             : }

Generated by: LCOV version 1.14