LCOV - code coverage report
Current view: top level - lib_enc - bw_detect.c (source / functions) Hit Total Coverage
Test: Coverage on main -- short test vectors @ e4dd50d43e5522084cbfc5cb7dcda17ea6ff534d Lines: 261 273 95.6 %
Date: 2026-01-27 05:59:54 Functions: 4 4 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             : /*====================================================================================
      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 <math.h>
      44             : #include "cnst.h"
      45             : #include "rom_enc.h"
      46             : #include "rom_com.h"
      47             : #include "prot.h"
      48             : #include "ivas_prot.h"
      49             : #include "wmc_auto.h"
      50             : 
      51             : /*-------------------------------------------------------------------*
      52             :  * Local constants
      53             :  *-------------------------------------------------------------------*/
      54             : 
      55             : #define BWD_MIN_BRATE_WIDER_BW_MDCT IVAS_48k
      56             : #define BWD_MIN_BRATE_WIDER_BW_MASA IVAS_48k
      57             : #define BWD_MIN_BRATE_WIDER_BW_ISM  IVAS_32k
      58             : #define BWD_MAX_BRATE_WIDER_BW_MDCT IVAS_80k
      59             : #define BWD_MAX_BRATE_WIDER_BW_ISM  IVAS_64k
      60             : 
      61             : #define ALPHA_BWD     0.75f
      62             : #define BWD_LT_THRESH 0.6f
      63             : 
      64             : #define BWD_COUNT_MAX           100
      65             : #define BWD_COUNT_WIDER_BW      10
      66             : #define BWD_COUNT_WIDER_BW_MDCT 0
      67             : 
      68             : #define CLDFB_ENER_OFFSET 1.6f
      69             : 
      70             : /*-------------------------------------------------------------------*
      71             :  * bw_detect()
      72             :  *
      73             :  * bandwidth detector
      74             :  *-------------------------------------------------------------------*/
      75             : 
      76     2983174 : void bw_detect(
      77             :     Encoder_State *st,             /* i/o: Encoder State       */
      78             :     const float signal_in[],       /* i  : input signal        */
      79             :     float *spectrum,               /* i  : MDCT spectrum       */
      80             :     const float *enerBuffer,       /* i  : energy buffer       */
      81             :     const IVAS_FORMAT ivas_format, /* i  : IVAS format         */
      82             :     const int16_t mct_on           /* i  : flag MCT mode       */
      83             : )
      84             : {
      85             :     int16_t i, j, k, bw_max, bin_width, n_bins;
      86             :     float spect[L_FRAME48k], in_win[BWD_TOTAL_WIDTH];
      87             :     float spect_bin[BWD_N_BINS_MAX];
      88             :     float cldfb_bin[9];
      89             :     const float *pt, *pt1;
      90             :     float max_NB, max_WB, max_SWB, max_FB, mean_NB, mean_WB, mean_SWB, mean_FB;
      91     2983174 :     int16_t cldfb_bin_width = 4;
      92             :     int16_t bwd_count_wider_bw, l_frame;
      93             : 
      94     2983174 :     bwd_count_wider_bw = BWD_COUNT_WIDER_BW;
      95     2983174 :     if ( st->ini_frame > 0 && ( ( st->element_mode == IVAS_CPE_MDCT && ( st->element_brate >= BWD_MIN_BRATE_WIDER_BW_MDCT || mct_on ) ) ||
      96      876920 :                                 ( st->is_ism_format && st->element_brate >= BWD_MIN_BRATE_WIDER_BW_ISM ) ||
      97       72700 :                                 ( ivas_format == MASA_FORMAT && st->element_brate >= BWD_MIN_BRATE_WIDER_BW_MASA ) ) )
      98             :     {
      99     2443297 :         bwd_count_wider_bw = BWD_COUNT_WIDER_BW_MDCT;
     100             :     }
     101             : 
     102     2983174 :     if ( st->input_Fs > 8000 )
     103             :     {
     104     2982374 :         if ( enerBuffer != NULL )
     105             :         {
     106             :             float ScalFac;
     107             : 
     108      887006 :             ScalFac = 1 / ( st->cldfbAnaEnc->scale * st->cldfbAnaEnc->scale * 8.f );
     109      887006 :             set_f( cldfb_bin, 0.001f, 9 );
     110             : 
     111             :             /* NB: 1.2 - 2.8 kHz, 4 cldfb-bands*/
     112      887006 :             cldfb_bin[0] += sum_f( &( enerBuffer[3] ), cldfb_bin_width );
     113             : 
     114             :             /* WB: 4.4 - 7.2 kHz, 8 cldfb-bands, mid band(14) counted twice */
     115      887006 :             if ( st->input_Fs >= 16000 )
     116             :             {
     117      887006 :                 cldfb_bin[1] += sum_f( &( enerBuffer[11] ), cldfb_bin_width );
     118      887006 :                 cldfb_bin[2] += sum_f( &( enerBuffer[14] ), cldfb_bin_width );
     119             :             }
     120             : 
     121             :             /* SWB: 9.2 - 15.6 kHz, 16 cldfb-bands */
     122      887006 :             if ( st->input_Fs >= 32000 )
     123             :             {
     124      820036 :                 cldfb_bin[3] += sum_f( &( enerBuffer[23] ), cldfb_bin_width );
     125      820036 :                 cldfb_bin[4] += sum_f( &( enerBuffer[27] ), cldfb_bin_width );
     126      820036 :                 cldfb_bin[5] += sum_f( &( enerBuffer[31] ), cldfb_bin_width );
     127      820036 :                 cldfb_bin[6] += sum_f( &( enerBuffer[35] ), cldfb_bin_width );
     128             :             }
     129             : 
     130             :             /* FB:  16.8 - 20.0 kHz, 8 cldfb-bands */
     131      887006 :             if ( st->input_Fs >= 48000 )
     132             :             {
     133      714778 :                 cldfb_bin[7] += sum_f( &( enerBuffer[42] ), cldfb_bin_width );
     134      714778 :                 cldfb_bin[8] += sum_f( &( enerBuffer[46] ), cldfb_bin_width );
     135             :             }
     136             : 
     137     8870060 :             for ( i = 0; i < 9; i++ )
     138             :             {
     139     7983054 :                 cldfb_bin[i] = (float) log10( cldfb_bin[i] * ScalFac ); /* see formula used in perform_noise_estimation_enc() for CNG */
     140             :             }
     141             :         }
     142             :         else
     143             :         {
     144             :             /* set width of a speactral bin (corresponds to 1.5kHz) */
     145     2095368 :             if ( st->input_Fs == 16000 )
     146             :             {
     147      135320 :                 bw_max = WB;
     148      135320 :                 bin_width = 60;
     149      135320 :                 n_bins = 5; /* spectrum to 7.5 kHz */
     150             :             }
     151     1960048 :             else if ( st->input_Fs == 32000 )
     152             :             {
     153      255577 :                 bw_max = SWB;
     154      255577 :                 bin_width = 30;
     155      255577 :                 n_bins = 10; /* spectrum to 15 kHz */
     156             :             }
     157             :             else /* st->input_Fs == 48000 */
     158             :             {
     159     1704471 :                 bw_max = FB;
     160     1704471 :                 bin_width = 20;
     161     1704471 :                 n_bins = BWD_N_BINS_MAX; /* spectrum to 19.5 kHz */
     162             :             }
     163             : 
     164     2095368 :             if ( signal_in != NULL )
     165             :             {
     166             :                 /*---------------------------------------------------------------------*
     167             :                  * windowing of the input signal
     168             :                  *---------------------------------------------------------------------*/
     169             : 
     170        2040 :                 pt = signal_in;
     171        2040 :                 pt1 = hann_window_320;
     172             : 
     173             :                 /* 1st half of the window */
     174      328440 :                 for ( i = 0; i < BWD_TOTAL_WIDTH / 2; i++ )
     175             :                 {
     176      326400 :                     in_win[i] = *pt++ * *pt1++;
     177             :                 }
     178        2040 :                 pt1--;
     179             : 
     180             :                 /* 2nd half of the window */
     181      328440 :                 for ( ; i < BWD_TOTAL_WIDTH; i++ )
     182             :                 {
     183      326400 :                     in_win[i] = *pt++ * *pt1--;
     184             :                 }
     185             : 
     186             :                 /*---------------------------------------------------------------------*
     187             :                  * tranform into frequency domain
     188             :                  *---------------------------------------------------------------------*/
     189             : 
     190        2040 :                 edct( in_win, spect, BWD_TOTAL_WIDTH, st->element_mode );
     191             :             }
     192             :             else
     193             :             {
     194     2093328 :                 l_frame = (int16_t) ( st->input_Fs / FRAMES_PER_SEC );
     195     2093328 :                 if ( st->core == TCX_10_CORE )
     196             :                 {
     197      132352 :                     l_frame /= 2;
     198             :                 }
     199             : 
     200     2093328 :                 bin_width *= ( l_frame / BWD_TOTAL_WIDTH );
     201     2093328 :                 mvr2r( spectrum, spect, l_frame );
     202             :             }
     203             :             /*---------------------------------------------------------------------*
     204             :              * compute energy per spectral bins
     205             :              *---------------------------------------------------------------------*/
     206             : 
     207     2095368 :             set_f( spect_bin, 0.001f, n_bins );
     208             : 
     209     9950623 :             for ( k = 0; k <= bw_max; k++ )
     210             :             {
     211    25390493 :                 for ( i = bwd_start_bin[k]; i <= bwd_end_bin[k]; i++ )
     212             :                 {
     213  1025665858 :                     for ( j = 0; j < bin_width; j++ )
     214             :                     {
     215  1008130620 :                         spect_bin[i] += spect[i * bin_width + j] * spect[i * bin_width + j];
     216             :                     }
     217    17535238 :                     spect_bin[i] = (float) log10( spect_bin[i] );
     218             :                 }
     219             :             }
     220             :         }
     221             : 
     222     2982374 :         if ( enerBuffer != NULL )
     223             :         {
     224             :             /* cldfb detections */
     225      887006 :             mean_NB = mean( cldfb_bin, 1 ); /* NB: 1.2 - 2.8 kHz, 4 cldfb-bands (1 bin)   */
     226      887006 :             maximum( cldfb_bin, 1, &max_NB );
     227      887006 :             mean_WB = mean( cldfb_bin + 1, 2 ); /* WB: 4.4 - 7.2 kHz, 8 cldfb-bands (2 bins)  */
     228      887006 :             maximum( cldfb_bin + 1, 2, &max_WB );
     229             : 
     230      887006 :             mean_NB += CLDFB_ENER_OFFSET;
     231      887006 :             max_NB += CLDFB_ENER_OFFSET;
     232      887006 :             mean_WB += CLDFB_ENER_OFFSET;
     233      887006 :             max_WB += CLDFB_ENER_OFFSET;
     234             : 
     235      887006 :             if ( st->input_Fs == 16000 )
     236             :             {
     237             :                 /* for 16kHz sampled inputs, do not check SWB & FB */
     238       66970 :                 mean_SWB = 0.0f;
     239       66970 :                 max_SWB = 0.0f;
     240       66970 :                 mean_FB = 0.0f;
     241       66970 :                 max_FB = 0.0f;
     242             :             }
     243      820036 :             else if ( st->input_Fs == 32000 )
     244             :             {
     245             :                 /* for 32kHz sampled inputs, do not check FB */
     246      105258 :                 mean_FB = 0.0f;
     247      105258 :                 max_FB = 0.0f;
     248      105258 :                 mean_SWB = mean( cldfb_bin + 3, 4 ); /*  SWB: 9.2 - 15.6 kHz, 16 cldfb-bands (4 bins) */
     249      105258 :                 maximum( cldfb_bin + 3, 4, &max_SWB );
     250      105258 :                 mean_SWB += CLDFB_ENER_OFFSET;
     251      105258 :                 max_SWB += CLDFB_ENER_OFFSET;
     252             :             }
     253             :             else
     254             :             {
     255      714778 :                 mean_SWB = mean( cldfb_bin + 3, 4 ); /*  SWB: 9.2 - 15.6 kHz, 16 cldfb-bands (4 bins) */
     256      714778 :                 maximum( cldfb_bin + 3, 4, &max_SWB );
     257      714778 :                 mean_FB = mean( cldfb_bin + 7, 2 ); /*  FB: 16.8 - 20.0 kHz, 8 cldfb-bands (2 bins) */
     258      714778 :                 maximum( cldfb_bin + 7, 2, &max_FB );
     259             : 
     260      714778 :                 mean_SWB += CLDFB_ENER_OFFSET;
     261      714778 :                 max_SWB += CLDFB_ENER_OFFSET;
     262      714778 :                 mean_FB += CLDFB_ENER_OFFSET;
     263      714778 :                 max_FB += CLDFB_ENER_OFFSET;
     264             :             }
     265             :         }
     266             :         else
     267             :         {
     268     2095368 :             mean_NB = mean( spect_bin + bwd_start_bin[0], bwd_end_bin[0] - bwd_start_bin[0] + 1 ); /* NB:  1.5-3.0kHz (1 bin)   */
     269     2095368 :             maximum( spect_bin + bwd_start_bin[0], bwd_end_bin[0] - bwd_start_bin[0] + 1, &max_NB );
     270     2095368 :             mean_WB = mean( spect_bin + bwd_start_bin[1], bwd_end_bin[1] - bwd_start_bin[1] + 1 ); /* WB:  4.5-7.5kHz (2 bins)  */
     271     2095368 :             maximum( spect_bin + bwd_start_bin[1], bwd_end_bin[1] - bwd_start_bin[1] + 1, &max_WB );
     272             : 
     273     2095368 :             if ( st->input_Fs == 16000 )
     274             :             {
     275             :                 /* for 16kHz sampled inputs, do not check SWB & FB */
     276      135320 :                 mean_SWB = 0.0f;
     277      135320 :                 max_SWB = 0.0f;
     278      135320 :                 mean_FB = 0.0f;
     279      135320 :                 max_FB = 0.0f;
     280             :             }
     281     1960048 :             else if ( st->input_Fs == 32000 )
     282             :             {
     283      255577 :                 mean_SWB = mean( spect_bin + bwd_start_bin[2], bwd_end_bin[2] - bwd_start_bin[2] + 1 ); /* SWB: 9.0-15.0kHz (4 bins) */
     284      255577 :                 maximum( spect_bin + bwd_start_bin[2], bwd_end_bin[2] - bwd_start_bin[2] + 1, &max_SWB );
     285             : 
     286             :                 /* for 32kHz sampled inputs, do not check FB */
     287      255577 :                 mean_FB = 0.0f;
     288      255577 :                 max_FB = 0.0f;
     289             :             }
     290             :             else
     291             :             {
     292     1704471 :                 mean_SWB = mean( spect_bin + bwd_start_bin[2], bwd_end_bin[2] - bwd_start_bin[2] + 1 ); /* SWB: 9.0-15.0kHz (4 bins) */
     293     1704471 :                 maximum( spect_bin + bwd_start_bin[2], bwd_end_bin[2] - bwd_start_bin[2] + 1, &max_SWB );
     294     1704471 :                 mean_FB = mean( spect_bin + bwd_start_bin[3], bwd_end_bin[3] - bwd_start_bin[3] + 1 ); /* FB: 16.5-19.5kHz (2 bins) */
     295     1704471 :                 maximum( spect_bin + bwd_start_bin[3], bwd_end_bin[3] - bwd_start_bin[3] + 1, &max_FB );
     296             :             }
     297             :         }
     298             : 
     299             :         /*---------------------------------------------------------------------*
     300             :          * update LT counters and energies
     301             :          *---------------------------------------------------------------------*/
     302             : 
     303     2982374 :         if ( st->localVAD || st->lp_noise > 30 )
     304             :         {
     305     2724884 :             st->lt_mean_NB = ALPHA_BWD * st->lt_mean_NB + ( 1 - ALPHA_BWD ) * mean_NB;
     306     2724884 :             st->lt_mean_WB = ALPHA_BWD * st->lt_mean_WB + ( 1 - ALPHA_BWD ) * mean_WB;
     307     2724884 :             st->lt_mean_SWB = ALPHA_BWD * st->lt_mean_SWB + ( 1 - ALPHA_BWD ) * mean_SWB;
     308             : 
     309     2724884 :             if ( enerBuffer != NULL )
     310             :             {
     311      796346 :                 if ( 0.9f * max_WB > BWD_LT_THRESH * st->lt_mean_NB )
     312             :                 {
     313      572749 :                     if ( 2.5f * max_WB > max_NB )
     314             :                     {
     315      572384 :                         st->count_WB++;
     316             :                     }
     317             :                 }
     318             :                 else
     319             :                 {
     320      223597 :                     if ( 3.5f * mean_WB < mean_NB )
     321             :                     {
     322       12286 :                         st->count_WB--;
     323             :                     }
     324             :                 }
     325             : 
     326      796346 :                 if ( 0.83f * max_SWB > BWD_LT_THRESH * st->lt_mean_WB && max_WB > BWD_LT_THRESH * st->lt_mean_NB )
     327             :                 {
     328      564813 :                     if ( 2 * max_SWB > max_WB )
     329             :                     {
     330      563201 :                         st->count_SWB++;
     331             :                     }
     332             :                 }
     333             :                 else
     334             :                 {
     335      231533 :                     if ( 3 * mean_SWB < mean_WB )
     336             :                     {
     337       79749 :                         st->count_SWB--;
     338             :                     }
     339             :                 }
     340             : 
     341      796346 :                 if ( max_FB > BWD_LT_THRESH * st->lt_mean_SWB && 0.83f * max_SWB > BWD_LT_THRESH * st->lt_mean_WB && max_WB > BWD_LT_THRESH * st->lt_mean_NB )
     342             :                 {
     343      384533 :                     if ( 3 * max_FB > max_SWB )
     344             :                     {
     345      383951 :                         st->count_FB++;
     346             :                     }
     347             :                 }
     348             :                 else
     349             :                 {
     350      411813 :                     if ( 4.1f * mean_FB < mean_SWB )
     351             :                     {
     352      202725 :                         st->count_FB--;
     353             :                     }
     354             :                 }
     355             :             }
     356             :             else
     357             :             {
     358     1928538 :                 if ( max_WB > BWD_LT_THRESH * st->lt_mean_NB )
     359             :                 {
     360     1638653 :                     if ( 2 * max_WB > max_NB )
     361             :                     {
     362     1635318 :                         st->count_WB++;
     363             :                     }
     364             :                 }
     365             :                 else
     366             :                 {
     367      289885 :                     if ( 2.6f * mean_WB < mean_NB )
     368             :                     {
     369       75766 :                         st->count_WB--;
     370             :                     }
     371             :                 }
     372             : 
     373     1928538 :                 if ( max_SWB > BWD_LT_THRESH * st->lt_mean_WB && max_WB > BWD_LT_THRESH * st->lt_mean_NB )
     374             :                 {
     375     1494578 :                     if ( 2 * max_SWB > max_WB )
     376             :                     {
     377     1481649 :                         st->count_SWB++;
     378             :                     }
     379             :                 }
     380             :                 else
     381             :                 {
     382      433960 :                     if ( 3 * mean_SWB < mean_WB )
     383             :                     {
     384      176143 :                         st->count_SWB--;
     385             :                     }
     386             :                 }
     387             : 
     388     1928538 :                 if ( max_FB > BWD_LT_THRESH * st->lt_mean_SWB && max_SWB > BWD_LT_THRESH * st->lt_mean_WB && max_WB > BWD_LT_THRESH * st->lt_mean_NB )
     389             :                 {
     390     1219490 :                     if ( 2 * max_FB > max_SWB )
     391             :                     {
     392     1209232 :                         st->count_FB++;
     393             :                     }
     394             :                 }
     395             :                 else
     396             :                 {
     397      709048 :                     if ( 3 * mean_FB < mean_SWB )
     398             :                     {
     399      293004 :                         st->count_FB--;
     400             :                     }
     401             :                 }
     402             :             }
     403             : 
     404     2724884 :             st->count_WB = min( st->count_WB, BWD_COUNT_MAX );
     405     2724884 :             st->count_SWB = min( st->count_SWB, BWD_COUNT_MAX );
     406     2724884 :             st->count_FB = min( st->count_FB, BWD_COUNT_MAX );
     407     2724884 :             st->count_WB = max( st->count_WB, 0 );
     408     2724884 :             st->count_SWB = max( st->count_SWB, 0 );
     409     2724884 :             st->count_FB = max( st->count_FB, 0 );
     410             : 
     411             :             /*---------------------------------------------------------------------*
     412             :              * check against thresholds
     413             :              * detect a band-width change
     414             :              *---------------------------------------------------------------------*/
     415             : 
     416             :             /* switching to a higher BW */
     417     2724884 :             if ( st->last_input_bwidth == NB )
     418             :             {
     419        1923 :                 if ( st->count_WB > bwd_count_wider_bw )
     420             :                 {
     421          47 :                     st->input_bwidth = WB;
     422          47 :                     st->count_WB = BWD_COUNT_MAX;
     423             : 
     424          47 :                     if ( st->count_SWB > bwd_count_wider_bw )
     425             :                     {
     426          27 :                         st->input_bwidth = SWB;
     427          27 :                         st->count_SWB = BWD_COUNT_MAX;
     428             : 
     429          27 :                         if ( st->count_FB > bwd_count_wider_bw )
     430             :                         {
     431           0 :                             st->input_bwidth = FB;
     432           0 :                             st->count_FB = BWD_COUNT_MAX;
     433             :                         }
     434             :                     }
     435             :                 }
     436             :             }
     437             : 
     438     2724884 :             if ( st->last_input_bwidth == WB && st->input_Fs > 16000 )
     439             :             {
     440       30473 :                 if ( st->count_SWB > bwd_count_wider_bw )
     441             :                 {
     442       30461 :                     st->input_bwidth = SWB;
     443       30461 :                     st->count_SWB = BWD_COUNT_MAX;
     444             : 
     445       30461 :                     if ( st->count_FB > bwd_count_wider_bw )
     446             :                     {
     447       30449 :                         st->input_bwidth = FB;
     448       30449 :                         st->count_FB = BWD_COUNT_MAX;
     449             :                     }
     450             :                 }
     451             :             }
     452             : 
     453     2724884 :             if ( st->last_input_bwidth == SWB && st->input_Fs > 32000 )
     454             :             {
     455      223164 :                 if ( st->count_FB > bwd_count_wider_bw )
     456             :                 {
     457      192939 :                     st->input_bwidth = FB;
     458      192939 :                     st->count_FB = BWD_COUNT_MAX;
     459             :                 }
     460             :             }
     461             : 
     462             :             /* switching to a lower BW */
     463     2724884 :             if ( st->last_input_bwidth == FB )
     464             :             {
     465     1935533 :                 if ( st->count_FB < 10 )
     466             :                 {
     467          52 :                     st->input_bwidth = SWB;
     468          52 :                     st->count_FB = 0;
     469             :                 }
     470     1935533 :                 if ( st->count_SWB < 10 )
     471             :                 {
     472           0 :                     st->input_bwidth = WB;
     473           0 :                     st->count_SWB = 0;
     474           0 :                     st->count_FB = 0;
     475             :                 }
     476     1935533 :                 if ( st->count_WB < 10 )
     477             :                 {
     478          42 :                     st->input_bwidth = NB;
     479          42 :                     st->count_WB = 0;
     480          42 :                     st->count_SWB = 0;
     481          42 :                     st->count_FB = 0;
     482             :                 }
     483             :             }
     484             : 
     485     2724884 :             if ( st->last_input_bwidth == SWB )
     486             :             {
     487      560943 :                 if ( st->count_SWB < 10 )
     488             :                 {
     489           0 :                     st->input_bwidth = WB;
     490           0 :                     st->count_SWB = 0;
     491           0 :                     st->count_FB = 0;
     492             :                 }
     493      560943 :                 if ( st->count_WB < 10 )
     494             :                 {
     495          24 :                     st->input_bwidth = NB;
     496          24 :                     st->count_WB = 0;
     497          24 :                     st->count_SWB = 0;
     498          24 :                     st->count_FB = 0;
     499             :                 }
     500             :             }
     501             : 
     502     2724884 :             if ( st->last_input_bwidth == WB )
     503             :             {
     504      226485 :                 if ( st->count_WB < 10 )
     505             :                 {
     506          12 :                     st->input_bwidth = NB;
     507          12 :                     st->count_WB = 0;
     508          12 :                     st->count_SWB = 0;
     509          12 :                     st->count_FB = 0;
     510             :                 }
     511             :             }
     512             :         }
     513             :     }
     514             : 
     515             :     /* verify that maximum encoded bandwidth (specified on the command line) is not exceeded */
     516     2983174 :     if ( st->input_bwidth > st->max_bwidth )
     517             :     {
     518      223484 :         st->input_bwidth = st->max_bwidth;
     519             :     }
     520             : 
     521     2983174 :     if ( st->element_mode == EVS_MONO )
     522             :     {
     523       18500 :         set_bw( -1, -1, st, st->codec_mode );
     524             :     }
     525             : 
     526     2983174 :     return;
     527             : }
     528             : 
     529             : /*-------------------------------------------------------------------*
     530             :  * set_bw()
     531             :  *
     532             :  * Set and limit the encoded bandwidth
     533             :  *-------------------------------------------------------------------*/
     534             : 
     535      985472 : void set_bw(
     536             :     const int16_t element_mode,  /* i  : element mode            */
     537             :     const int32_t element_brate, /* i  : element bitrate         */
     538             :     Encoder_State *st,           /* i/o: Encoder State           */
     539             :     const int16_t codec_mode     /* i  : codec mode              */
     540             : )
     541             : {
     542             :     /* initialization */
     543      985472 :     st->bwidth = st->input_bwidth;
     544             : 
     545      985472 :     if ( codec_mode == MODE1 )
     546             :     {
     547             :         int32_t total_brate;
     548             : 
     549      976142 :         st->bwidth = st->input_bwidth;
     550      976142 :         total_brate = st->total_brate;
     551             : 
     552      976142 :         if ( element_mode > IVAS_SCE )
     553             :         {
     554      143807 :             if ( element_brate < MIN_BRATE_SWB_STEREO )
     555             :             {
     556           0 :                 st->bwidth = WB;
     557             :             }
     558             :             else
     559             :             {
     560      143807 :                 if ( st->idchan == 0 || element_mode == IVAS_CPE_MDCT )
     561             :                 {
     562      139216 :                     if ( element_brate >= MIN_BRATE_FB_STEREO )
     563             :                     {
     564       38701 :                         st->bwidth = min( st->bwidth, FB );
     565             :                     }
     566             :                     else
     567             :                     {
     568      100515 :                         st->bwidth = min( st->bwidth, SWB );
     569             :                     }
     570      139216 :                     st->bwidth = max( st->bwidth, WB );
     571             :                 }
     572             :                 else
     573             :                 {
     574        4591 :                     st->bwidth = WB;
     575             :                 }
     576             :             }
     577             :         }
     578      832335 :         else if ( element_mode == IVAS_SCE )
     579             :         {
     580      823165 :             if ( element_brate < MIN_BRATE_SWB_SCE || st->bwidth < WB )
     581             :             {
     582       10437 :                 st->bwidth = WB;
     583             :             }
     584      812728 :             else if ( st->bwidth > SWB && ( ( element_brate < MIN_BRATE_FB_STEREO && !st->is_ism_format ) ||
     585       21736 :                                             ( element_brate < MIN_BRATE_FB_ISM && st->is_ism_format ) ) )
     586             :             {
     587       23011 :                 st->bwidth = SWB;
     588             :             }
     589      789717 :             else if ( element_brate > BWD_MAX_BRATE_WIDER_BW_ISM )
     590             :             {
     591       89124 :                 st->bwidth = st->max_bwidth;
     592             :             }
     593             :         }
     594             :         /* element_mode == EVS_MONO */
     595        9170 :         else if ( total_brate <= ACELP_9k60 && st->bwidth > WB )
     596             :         {
     597          80 :             st->bwidth = WB;
     598             :         }
     599        9090 :         else if ( total_brate >= ACELP_13k20 && total_brate <= ACELP_16k40 && st->bwidth > SWB )
     600             :         {
     601           0 :             st->bwidth = SWB;
     602             :         }
     603        9090 :         else if ( total_brate >= ACELP_32k && st->bwidth < WB )
     604             :         {
     605           0 :             st->bwidth = WB;
     606             :         }
     607             :     }
     608        9330 :     else if ( codec_mode == MODE2 )
     609             :     {
     610             :         int16_t n, bits_frame_nominal, tmpBandwidthMin;
     611             : 
     612        9330 :         bits_frame_nominal = (int16_t) ( st->total_brate / FRAMES_PER_SEC );
     613       78420 :         for ( n = 0; n < FRAME_SIZE_NB; n++ )
     614             :         {
     615       78420 :             if ( FrameSizeConfig[n].frame_bits == bits_frame_nominal )
     616             :             {
     617        9330 :                 break;
     618             :             }
     619             :         }
     620        9330 :         if ( n == FRAME_SIZE_NB )
     621             :         {
     622           0 :             assert( !"Bitrate not supported: not part of EVS" );
     623             :         }
     624             : 
     625        9330 :         tmpBandwidthMin = FrameSizeConfig[n].bandwidth_min;
     626             : 
     627        9330 :         if ( st->rf_mode )
     628             :         {
     629        1600 :             tmpBandwidthMin = WB;
     630             :         }
     631             : 
     632        9330 :         st->bwidth = max( min( st->input_bwidth, FrameSizeConfig[n].bandwidth_max ), tmpBandwidthMin );
     633             :     }
     634             : 
     635      985472 :     return;
     636             : }
     637             : 
     638             : /*-------------------------------------------------------------------*
     639             :  * set_bw_stereo()
     640             :  *
     641             :  * Set encoded bandwidth for stereo (CPE) channels
     642             :  *-------------------------------------------------------------------*/
     643             : 
     644      220516 : void set_bw_stereo(
     645             :     CPE_ENC_HANDLE hCPE /* i/o: CPE encoder structures     */
     646             : )
     647             : {
     648      220516 :     Encoder_State **sts = hCPE->hCoreCoder;
     649             : 
     650      220516 :     if ( hCPE->element_brate > BWD_MAX_BRATE_WIDER_BW_MDCT )
     651             :     {
     652       70105 :         sts[0]->bwidth = sts[0]->max_bwidth;
     653       70105 :         sts[1]->bwidth = sts[1]->max_bwidth;
     654             :     }
     655      150411 :     else if ( hCPE->element_mode == IVAS_CPE_MDCT )
     656             :     {
     657             :         /* ensure that both CPE channels have the same audio band-width */
     658      150411 :         if ( sts[0]->input_bwidth == sts[1]->input_bwidth )
     659             :         {
     660      148626 :             sts[0]->bwidth = sts[0]->input_bwidth;
     661      148626 :             sts[1]->bwidth = sts[0]->input_bwidth;
     662             :         }
     663             :         else
     664             :         {
     665        1785 :             sts[0]->bwidth = max( sts[0]->input_bwidth, sts[1]->input_bwidth );
     666        1785 :             sts[1]->bwidth = max( sts[0]->input_bwidth, sts[1]->input_bwidth );
     667             :         }
     668             :     }
     669             : 
     670      220516 :     sts[0]->bwidth = max( sts[0]->bwidth, WB );
     671      220516 :     sts[1]->bwidth = max( sts[1]->bwidth, WB );
     672             : 
     673      220516 :     return;
     674             : }
     675             : 
     676             : /*-------------------------------------------------------------------*
     677             :  * set_bw_mct()
     678             :  *
     679             :  * Set encoded bandwidth for MCT
     680             :  *-------------------------------------------------------------------*/
     681             : 
     682             : /*! r: flag indicating whether the coded BW has changed */
     683      306279 : int16_t set_bw_mct(
     684             :     CPE_ENC_HANDLE hCPE[MCT_MAX_BLOCKS], /* i/o: CPE encoder structures      */
     685             :     const int16_t nCPE                   /* i  : number of CPEs              */
     686             : )
     687             : {
     688             :     Encoder_State *st;
     689             :     int16_t ch, cpe_id;
     690             :     int16_t mct_bwidth, last_mct_bwidth, bw_changed;
     691             : 
     692      306279 :     mct_bwidth = WB;                                       /* minimum coded audio band-width */
     693      306279 :     last_mct_bwidth = hCPE[0]->hCoreCoder[0]->last_bwidth; /* supposes that LFE is not in the first channel */
     694             : 
     695     1194647 :     for ( cpe_id = 0; cpe_id < nCPE; cpe_id++ )
     696             :     {
     697     2665104 :         for ( ch = 0; ch < CPE_CHANNELS; ch++ )
     698             :         {
     699     1776736 :             st = hCPE[cpe_id]->hCoreCoder[ch];
     700     1776736 :             if ( st->mct_chan_mode == MCT_CHAN_MODE_IGNORE )
     701             :             {
     702      186499 :                 continue;
     703             :             }
     704             : 
     705     1590237 :             mct_bwidth = max( mct_bwidth, st->input_bwidth );
     706             :         }
     707             :     }
     708             : 
     709     1194647 :     for ( cpe_id = 0; cpe_id < nCPE; cpe_id++ )
     710             :     {
     711      888368 :         if ( hCPE[cpe_id]->element_brate > BWD_MAX_BRATE_WIDER_BW_MDCT )
     712             :         {
     713      643348 :             mct_bwidth = max( mct_bwidth, hCPE[cpe_id]->hCoreCoder[0]->max_bwidth );
     714             :         }
     715             :     }
     716             : 
     717      306279 :     bw_changed = 0;
     718      306279 :     if ( mct_bwidth != last_mct_bwidth )
     719             :     {
     720         179 :         bw_changed = 1;
     721             :     }
     722             : 
     723             :     /*
     724             :      * always set bw for all CPEs even if it is the same value as before,
     725             :      * in case of bw + br switching when changing to MCT, this overwrites
     726             :      * potentially incorrect initial values
     727             :      */
     728     1194647 :     for ( cpe_id = 0; cpe_id < nCPE; cpe_id++ )
     729             :     {
     730     2665104 :         for ( ch = 0; ch < CPE_CHANNELS; ch++ )
     731             :         {
     732     1776736 :             st = hCPE[cpe_id]->hCoreCoder[ch];
     733     1776736 :             st->bwidth = mct_bwidth;
     734             :         }
     735             :     }
     736             : 
     737      306279 :     return bw_changed;
     738             : }

Generated by: LCOV version 1.14