LCOV - code coverage report
Current view: top level - lib_enc - bw_detect.c (source / functions) Hit Total Coverage
Test: Coverage on main -- long test vectors @ efe53129c9ed87a5067dd0a8fb9dca41db9c4add Lines: 269 273 98.5 %
Date: 2026-02-12 08:06:16 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    13347180 : 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    13347180 :     int16_t cldfb_bin_width = 4;
      92             :     int16_t bwd_count_wider_bw, l_frame;
      93             : 
      94    13347180 :     bwd_count_wider_bw = BWD_COUNT_WIDER_BW;
      95    13347180 :     if ( st->ini_frame > 0 && ( ( st->element_mode == IVAS_CPE_MDCT && ( st->element_brate >= BWD_MIN_BRATE_WIDER_BW_MDCT || mct_on ) ) ||
      96     3892422 :                                 ( st->is_ism_format && st->element_brate >= BWD_MIN_BRATE_WIDER_BW_ISM ) ||
      97      314392 :                                 ( ivas_format == MASA_FORMAT && st->element_brate >= BWD_MIN_BRATE_WIDER_BW_MASA ) ) )
      98             :     {
      99    10446762 :         bwd_count_wider_bw = BWD_COUNT_WIDER_BW_MDCT;
     100             :     }
     101             : 
     102    13347180 :     if ( st->input_Fs > 8000 )
     103             :     {
     104    13340270 :         if ( enerBuffer != NULL )
     105             :         {
     106             :             float ScalFac;
     107             : 
     108     3933735 :             ScalFac = 1 / ( st->cldfbAnaEnc->scale * st->cldfbAnaEnc->scale * 8.f );
     109     3933735 :             set_f( cldfb_bin, 0.001f, 9 );
     110             : 
     111             :             /* NB: 1.2 - 2.8 kHz, 4 cldfb-bands*/
     112     3933735 :             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     3933735 :             if ( st->input_Fs >= 16000 )
     116             :             {
     117     3933735 :                 cldfb_bin[1] += sum_f( &( enerBuffer[11] ), cldfb_bin_width );
     118     3933735 :                 cldfb_bin[2] += sum_f( &( enerBuffer[14] ), cldfb_bin_width );
     119             :             }
     120             : 
     121             :             /* SWB: 9.2 - 15.6 kHz, 16 cldfb-bands */
     122     3933735 :             if ( st->input_Fs >= 32000 )
     123             :             {
     124     3630981 :                 cldfb_bin[3] += sum_f( &( enerBuffer[23] ), cldfb_bin_width );
     125     3630981 :                 cldfb_bin[4] += sum_f( &( enerBuffer[27] ), cldfb_bin_width );
     126     3630981 :                 cldfb_bin[5] += sum_f( &( enerBuffer[31] ), cldfb_bin_width );
     127     3630981 :                 cldfb_bin[6] += sum_f( &( enerBuffer[35] ), cldfb_bin_width );
     128             :             }
     129             : 
     130             :             /* FB:  16.8 - 20.0 kHz, 8 cldfb-bands */
     131     3933735 :             if ( st->input_Fs >= 48000 )
     132             :             {
     133     2913720 :                 cldfb_bin[7] += sum_f( &( enerBuffer[42] ), cldfb_bin_width );
     134     2913720 :                 cldfb_bin[8] += sum_f( &( enerBuffer[46] ), cldfb_bin_width );
     135             :             }
     136             : 
     137    39337350 :             for ( i = 0; i < 9; i++ )
     138             :             {
     139    35403615 :                 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     9406535 :             if ( st->input_Fs == 16000 )
     146             :             {
     147      751393 :                 bw_max = WB;
     148      751393 :                 bin_width = 60;
     149      751393 :                 n_bins = 5; /* spectrum to 7.5 kHz */
     150             :             }
     151     8655142 :             else if ( st->input_Fs == 32000 )
     152             :             {
     153     2010424 :                 bw_max = SWB;
     154     2010424 :                 bin_width = 30;
     155     2010424 :                 n_bins = 10; /* spectrum to 15 kHz */
     156             :             }
     157             :             else /* st->input_Fs == 48000 */
     158             :             {
     159     6644718 :                 bw_max = FB;
     160     6644718 :                 bin_width = 20;
     161     6644718 :                 n_bins = BWD_N_BINS_MAX; /* spectrum to 19.5 kHz */
     162             :             }
     163             : 
     164     9406535 :             if ( signal_in != NULL )
     165             :             {
     166             :                 /*---------------------------------------------------------------------*
     167             :                  * windowing of the input signal
     168             :                  *---------------------------------------------------------------------*/
     169             : 
     170        3256 :                 pt = signal_in;
     171        3256 :                 pt1 = hann_window_320;
     172             : 
     173             :                 /* 1st half of the window */
     174      524216 :                 for ( i = 0; i < BWD_TOTAL_WIDTH / 2; i++ )
     175             :                 {
     176      520960 :                     in_win[i] = *pt++ * *pt1++;
     177             :                 }
     178        3256 :                 pt1--;
     179             : 
     180             :                 /* 2nd half of the window */
     181      524216 :                 for ( ; i < BWD_TOTAL_WIDTH; i++ )
     182             :                 {
     183      520960 :                     in_win[i] = *pt++ * *pt1--;
     184             :                 }
     185             : 
     186             :                 /*---------------------------------------------------------------------*
     187             :                  * tranform into frequency domain
     188             :                  *---------------------------------------------------------------------*/
     189             : 
     190        3256 :                 edct( in_win, spect, BWD_TOTAL_WIDTH, st->element_mode );
     191             :             }
     192             :             else
     193             :             {
     194     9403279 :                 l_frame = (int16_t) ( st->input_Fs / FRAMES_PER_SEC );
     195     9403279 :                 if ( st->core == TCX_10_CORE )
     196             :                 {
     197      415138 :                     l_frame /= 2;
     198             :                 }
     199             : 
     200     9403279 :                 bin_width *= ( l_frame / BWD_TOTAL_WIDTH );
     201     9403279 :                 mvr2r( spectrum, spect, l_frame );
     202             :             }
     203             :             /*---------------------------------------------------------------------*
     204             :              * compute energy per spectral bins
     205             :              *---------------------------------------------------------------------*/
     206             : 
     207     9406535 :             set_f( spect_bin, 0.001f, n_bins );
     208             : 
     209    43519465 :             for ( k = 0; k <= bw_max; k++ )
     210             :             {
     211   110242539 :                 for ( i = bwd_start_bin[k]; i <= bwd_end_bin[k]; i++ )
     212             :                 {
     213  4511447749 :                     for ( j = 0; j < bin_width; j++ )
     214             :                     {
     215  4435318140 :                         spect_bin[i] += spect[i * bin_width + j] * spect[i * bin_width + j];
     216             :                     }
     217    76129609 :                     spect_bin[i] = (float) log10( spect_bin[i] );
     218             :                 }
     219             :             }
     220             :         }
     221             : 
     222    13340270 :         if ( enerBuffer != NULL )
     223             :         {
     224             :             /* cldfb detections */
     225     3933735 :             mean_NB = mean( cldfb_bin, 1 ); /* NB: 1.2 - 2.8 kHz, 4 cldfb-bands (1 bin)   */
     226     3933735 :             maximum( cldfb_bin, 1, &max_NB );
     227     3933735 :             mean_WB = mean( cldfb_bin + 1, 2 ); /* WB: 4.4 - 7.2 kHz, 8 cldfb-bands (2 bins)  */
     228     3933735 :             maximum( cldfb_bin + 1, 2, &max_WB );
     229             : 
     230     3933735 :             mean_NB += CLDFB_ENER_OFFSET;
     231     3933735 :             max_NB += CLDFB_ENER_OFFSET;
     232     3933735 :             mean_WB += CLDFB_ENER_OFFSET;
     233     3933735 :             max_WB += CLDFB_ENER_OFFSET;
     234             : 
     235     3933735 :             if ( st->input_Fs == 16000 )
     236             :             {
     237             :                 /* for 16kHz sampled inputs, do not check SWB & FB */
     238      302754 :                 mean_SWB = 0.0f;
     239      302754 :                 max_SWB = 0.0f;
     240      302754 :                 mean_FB = 0.0f;
     241      302754 :                 max_FB = 0.0f;
     242             :             }
     243     3630981 :             else if ( st->input_Fs == 32000 )
     244             :             {
     245             :                 /* for 32kHz sampled inputs, do not check FB */
     246      717261 :                 mean_FB = 0.0f;
     247      717261 :                 max_FB = 0.0f;
     248      717261 :                 mean_SWB = mean( cldfb_bin + 3, 4 ); /*  SWB: 9.2 - 15.6 kHz, 16 cldfb-bands (4 bins) */
     249      717261 :                 maximum( cldfb_bin + 3, 4, &max_SWB );
     250      717261 :                 mean_SWB += CLDFB_ENER_OFFSET;
     251      717261 :                 max_SWB += CLDFB_ENER_OFFSET;
     252             :             }
     253             :             else
     254             :             {
     255     2913720 :                 mean_SWB = mean( cldfb_bin + 3, 4 ); /*  SWB: 9.2 - 15.6 kHz, 16 cldfb-bands (4 bins) */
     256     2913720 :                 maximum( cldfb_bin + 3, 4, &max_SWB );
     257     2913720 :                 mean_FB = mean( cldfb_bin + 7, 2 ); /*  FB: 16.8 - 20.0 kHz, 8 cldfb-bands (2 bins) */
     258     2913720 :                 maximum( cldfb_bin + 7, 2, &max_FB );
     259             : 
     260     2913720 :                 mean_SWB += CLDFB_ENER_OFFSET;
     261     2913720 :                 max_SWB += CLDFB_ENER_OFFSET;
     262     2913720 :                 mean_FB += CLDFB_ENER_OFFSET;
     263     2913720 :                 max_FB += CLDFB_ENER_OFFSET;
     264             :             }
     265             :         }
     266             :         else
     267             :         {
     268     9406535 :             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     9406535 :             maximum( spect_bin + bwd_start_bin[0], bwd_end_bin[0] - bwd_start_bin[0] + 1, &max_NB );
     270     9406535 :             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     9406535 :             maximum( spect_bin + bwd_start_bin[1], bwd_end_bin[1] - bwd_start_bin[1] + 1, &max_WB );
     272             : 
     273     9406535 :             if ( st->input_Fs == 16000 )
     274             :             {
     275             :                 /* for 16kHz sampled inputs, do not check SWB & FB */
     276      751393 :                 mean_SWB = 0.0f;
     277      751393 :                 max_SWB = 0.0f;
     278      751393 :                 mean_FB = 0.0f;
     279      751393 :                 max_FB = 0.0f;
     280             :             }
     281     8655142 :             else if ( st->input_Fs == 32000 )
     282             :             {
     283     2010424 :                 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     2010424 :                 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     2010424 :                 mean_FB = 0.0f;
     288     2010424 :                 max_FB = 0.0f;
     289             :             }
     290             :             else
     291             :             {
     292     6644718 :                 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     6644718 :                 maximum( spect_bin + bwd_start_bin[2], bwd_end_bin[2] - bwd_start_bin[2] + 1, &max_SWB );
     294     6644718 :                 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     6644718 :                 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    13340270 :         if ( st->localVAD || st->lp_noise > 30 )
     304             :         {
     305    11656776 :             st->lt_mean_NB = ALPHA_BWD * st->lt_mean_NB + ( 1 - ALPHA_BWD ) * mean_NB;
     306    11656776 :             st->lt_mean_WB = ALPHA_BWD * st->lt_mean_WB + ( 1 - ALPHA_BWD ) * mean_WB;
     307    11656776 :             st->lt_mean_SWB = ALPHA_BWD * st->lt_mean_SWB + ( 1 - ALPHA_BWD ) * mean_SWB;
     308             : 
     309    11656776 :             if ( enerBuffer != NULL )
     310             :             {
     311     2955303 :                 if ( 0.9f * max_WB > BWD_LT_THRESH * st->lt_mean_NB )
     312             :                 {
     313     2072379 :                     if ( 2.5f * max_WB > max_NB )
     314             :                     {
     315     2070720 :                         st->count_WB++;
     316             :                     }
     317             :                 }
     318             :                 else
     319             :                 {
     320      882924 :                     if ( 3.5f * mean_WB < mean_NB )
     321             :                     {
     322      106305 :                         st->count_WB--;
     323             :                     }
     324             :                 }
     325             : 
     326     2955303 :                 if ( 0.83f * max_SWB > BWD_LT_THRESH * st->lt_mean_WB && max_WB > BWD_LT_THRESH * st->lt_mean_NB )
     327             :                 {
     328     1939050 :                     if ( 2 * max_SWB > max_WB )
     329             :                     {
     330     1935604 :                         st->count_SWB++;
     331             :                     }
     332             :                 }
     333             :                 else
     334             :                 {
     335     1016253 :                     if ( 3 * mean_SWB < mean_WB )
     336             :                     {
     337      352051 :                         st->count_SWB--;
     338             :                     }
     339             :                 }
     340             : 
     341     2955303 :                 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     1245626 :                     if ( 3 * max_FB > max_SWB )
     344             :                     {
     345     1242955 :                         st->count_FB++;
     346             :                     }
     347             :                 }
     348             :                 else
     349             :                 {
     350     1709677 :                     if ( 4.1f * mean_FB < mean_SWB )
     351             :                     {
     352      817195 :                         st->count_FB--;
     353             :                     }
     354             :                 }
     355             :             }
     356             :             else
     357             :             {
     358     8701473 :                 if ( max_WB > BWD_LT_THRESH * st->lt_mean_NB )
     359             :                 {
     360     7729700 :                     if ( 2 * max_WB > max_NB )
     361             :                     {
     362     7719662 :                         st->count_WB++;
     363             :                     }
     364             :                 }
     365             :                 else
     366             :                 {
     367      971773 :                     if ( 2.6f * mean_WB < mean_NB )
     368             :                     {
     369      391910 :                         st->count_WB--;
     370             :                     }
     371             :                 }
     372             : 
     373     8701473 :                 if ( max_SWB > BWD_LT_THRESH * st->lt_mean_WB && max_WB > BWD_LT_THRESH * st->lt_mean_NB )
     374             :                 {
     375     6973394 :                     if ( 2 * max_SWB > max_WB )
     376             :                     {
     377     6954597 :                         st->count_SWB++;
     378             :                     }
     379             :                 }
     380             :                 else
     381             :                 {
     382     1728079 :                     if ( 3 * mean_SWB < mean_WB )
     383             :                     {
     384      742541 :                         st->count_SWB--;
     385             :                     }
     386             :                 }
     387             : 
     388     8701473 :                 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     5161065 :                     if ( 2 * max_FB > max_SWB )
     391             :                     {
     392     5117277 :                         st->count_FB++;
     393             :                     }
     394             :                 }
     395             :                 else
     396             :                 {
     397     3540408 :                     if ( 3 * mean_FB < mean_SWB )
     398             :                     {
     399     1920595 :                         st->count_FB--;
     400             :                     }
     401             :                 }
     402             :             }
     403             : 
     404    11656776 :             st->count_WB = min( st->count_WB, BWD_COUNT_MAX );
     405    11656776 :             st->count_SWB = min( st->count_SWB, BWD_COUNT_MAX );
     406    11656776 :             st->count_FB = min( st->count_FB, BWD_COUNT_MAX );
     407    11656776 :             st->count_WB = max( st->count_WB, 0 );
     408    11656776 :             st->count_SWB = max( st->count_SWB, 0 );
     409    11656776 :             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    11656776 :             if ( st->last_input_bwidth == NB )
     418             :             {
     419      137111 :                 if ( st->count_WB > bwd_count_wider_bw )
     420             :                 {
     421        1487 :                     st->input_bwidth = WB;
     422        1487 :                     st->count_WB = BWD_COUNT_MAX;
     423             : 
     424        1487 :                     if ( st->count_SWB > bwd_count_wider_bw )
     425             :                     {
     426        1219 :                         st->input_bwidth = SWB;
     427        1219 :                         st->count_SWB = BWD_COUNT_MAX;
     428             : 
     429        1219 :                         if ( st->count_FB > bwd_count_wider_bw )
     430             :                         {
     431         874 :                             st->input_bwidth = FB;
     432         874 :                             st->count_FB = BWD_COUNT_MAX;
     433             :                         }
     434             :                     }
     435             :                 }
     436             :             }
     437             : 
     438    11656776 :             if ( st->last_input_bwidth == WB && st->input_Fs > 16000 )
     439             :             {
     440      569695 :                 if ( st->count_SWB > bwd_count_wider_bw )
     441             :                 {
     442      555666 :                     st->input_bwidth = SWB;
     443      555666 :                     st->count_SWB = BWD_COUNT_MAX;
     444             : 
     445      555666 :                     if ( st->count_FB > bwd_count_wider_bw )
     446             :                     {
     447      452351 :                         st->input_bwidth = FB;
     448      452351 :                         st->count_FB = BWD_COUNT_MAX;
     449             :                     }
     450             :                 }
     451             :             }
     452             : 
     453    11656776 :             if ( st->last_input_bwidth == SWB && st->input_Fs > 32000 )
     454             :             {
     455      968473 :                 if ( st->count_FB > bwd_count_wider_bw )
     456             :                 {
     457      949939 :                     st->input_bwidth = FB;
     458      949939 :                     st->count_FB = BWD_COUNT_MAX;
     459             :                 }
     460             :             }
     461             : 
     462             :             /* switching to a lower BW */
     463    11656776 :             if ( st->last_input_bwidth == FB )
     464             :             {
     465     6691721 :                 if ( st->count_FB < 10 )
     466             :                 {
     467          24 :                     st->input_bwidth = SWB;
     468          24 :                     st->count_FB = 0;
     469             :                 }
     470     6691721 :                 if ( st->count_SWB < 10 )
     471             :                 {
     472          99 :                     st->input_bwidth = WB;
     473          99 :                     st->count_SWB = 0;
     474          99 :                     st->count_FB = 0;
     475             :                 }
     476     6691721 :                 if ( st->count_WB < 10 )
     477             :                 {
     478        1012 :                     st->input_bwidth = NB;
     479        1012 :                     st->count_WB = 0;
     480        1012 :                     st->count_SWB = 0;
     481        1012 :                     st->count_FB = 0;
     482             :                 }
     483             :             }
     484             : 
     485    11656776 :             if ( st->last_input_bwidth == SWB )
     486             :             {
     487     3287808 :                 if ( st->count_SWB < 10 )
     488             :                 {
     489         112 :                     st->input_bwidth = WB;
     490         112 :                     st->count_SWB = 0;
     491         112 :                     st->count_FB = 0;
     492             :                 }
     493     3287808 :                 if ( st->count_WB < 10 )
     494             :                 {
     495         394 :                     st->input_bwidth = NB;
     496         394 :                     st->count_WB = 0;
     497         394 :                     st->count_SWB = 0;
     498         394 :                     st->count_FB = 0;
     499             :                 }
     500             :             }
     501             : 
     502    11656776 :             if ( st->last_input_bwidth == WB )
     503             :             {
     504     1540136 :                 if ( st->count_WB < 10 )
     505             :                 {
     506         343 :                     st->input_bwidth = NB;
     507         343 :                     st->count_WB = 0;
     508         343 :                     st->count_SWB = 0;
     509         343 :                     st->count_FB = 0;
     510             :                 }
     511             :             }
     512             :         }
     513             :     }
     514             : 
     515             :     /* verify that maximum encoded bandwidth (specified on the command line) is not exceeded */
     516    13347180 :     if ( st->input_bwidth > st->max_bwidth )
     517             :     {
     518     1506501 :         st->input_bwidth = st->max_bwidth;
     519             :     }
     520             : 
     521    13347180 :     if ( st->element_mode == EVS_MONO )
     522             :     {
     523       87114 :         set_bw( -1, -1, st, st->codec_mode );
     524             :     }
     525             : 
     526    13347180 :     return;
     527             : }
     528             : 
     529             : /*-------------------------------------------------------------------*
     530             :  * set_bw()
     531             :  *
     532             :  * Set and limit the encoded bandwidth
     533             :  *-------------------------------------------------------------------*/
     534             : 
     535     4253641 : 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     4253641 :     st->bwidth = st->input_bwidth;
     544             : 
     545     4253641 :     if ( codec_mode == MODE1 )
     546             :     {
     547             :         int32_t total_brate;
     548             : 
     549     4219508 :         st->bwidth = st->input_bwidth;
     550     4219508 :         total_brate = st->total_brate;
     551             : 
     552     4219508 :         if ( element_mode > IVAS_SCE )
     553             :         {
     554      776860 :             if ( element_brate < MIN_BRATE_SWB_STEREO )
     555             :             {
     556           0 :                 st->bwidth = WB;
     557             :             }
     558             :             else
     559             :             {
     560      776860 :                 if ( st->idchan == 0 || element_mode == IVAS_CPE_MDCT )
     561             :                 {
     562      752336 :                     if ( element_brate >= MIN_BRATE_FB_STEREO )
     563             :                     {
     564      288251 :                         st->bwidth = min( st->bwidth, FB );
     565             :                     }
     566             :                     else
     567             :                     {
     568      464085 :                         st->bwidth = min( st->bwidth, SWB );
     569             :                     }
     570      752336 :                     st->bwidth = max( st->bwidth, WB );
     571             :                 }
     572             :                 else
     573             :                 {
     574       24524 :                     st->bwidth = WB;
     575             :                 }
     576             :             }
     577             :         }
     578     3442648 :         else if ( element_mode == IVAS_SCE )
     579             :         {
     580     3389667 :             if ( element_brate < MIN_BRATE_SWB_SCE || st->bwidth < WB )
     581             :             {
     582       58345 :                 st->bwidth = WB;
     583             :             }
     584     3331322 :             else if ( st->bwidth > SWB && ( ( element_brate < MIN_BRATE_FB_STEREO && !st->is_ism_format ) ||
     585       38010 :                                             ( element_brate < MIN_BRATE_FB_ISM && st->is_ism_format ) ) )
     586             :             {
     587       72258 :                 st->bwidth = SWB;
     588             :             }
     589     3259064 :             else if ( element_brate > BWD_MAX_BRATE_WIDER_BW_ISM )
     590             :             {
     591      218381 :                 st->bwidth = st->max_bwidth;
     592             :             }
     593             :         }
     594             :         /* element_mode == EVS_MONO */
     595       52981 :         else if ( total_brate <= ACELP_9k60 && st->bwidth > WB )
     596             :         {
     597        2426 :             st->bwidth = WB;
     598             :         }
     599       50555 :         else if ( total_brate >= ACELP_13k20 && total_brate <= ACELP_16k40 && st->bwidth > SWB )
     600             :         {
     601           0 :             st->bwidth = SWB;
     602             :         }
     603       50555 :         else if ( total_brate >= ACELP_32k && st->bwidth < WB )
     604             :         {
     605         302 :             st->bwidth = WB;
     606             :         }
     607             :     }
     608       34133 :     else if ( codec_mode == MODE2 )
     609             :     {
     610             :         int16_t n, bits_frame_nominal, tmpBandwidthMin;
     611             : 
     612       34133 :         bits_frame_nominal = (int16_t) ( st->total_brate / FRAMES_PER_SEC );
     613      278918 :         for ( n = 0; n < FRAME_SIZE_NB; n++ )
     614             :         {
     615      278918 :             if ( FrameSizeConfig[n].frame_bits == bits_frame_nominal )
     616             :             {
     617       34133 :                 break;
     618             :             }
     619             :         }
     620       34133 :         if ( n == FRAME_SIZE_NB )
     621             :         {
     622           0 :             assert( !"Bitrate not supported: not part of EVS" );
     623             :         }
     624             : 
     625       34133 :         tmpBandwidthMin = FrameSizeConfig[n].bandwidth_min;
     626             : 
     627       34133 :         if ( st->rf_mode )
     628             :         {
     629           0 :             tmpBandwidthMin = WB;
     630             :         }
     631             : 
     632       34133 :         st->bwidth = max( min( st->input_bwidth, FrameSizeConfig[n].bandwidth_max ), tmpBandwidthMin );
     633             :     }
     634             : 
     635     4253641 :     return;
     636             : }
     637             : 
     638             : /*-------------------------------------------------------------------*
     639             :  * set_bw_stereo()
     640             :  *
     641             :  * Set encoded bandwidth for stereo (CPE) channels
     642             :  *-------------------------------------------------------------------*/
     643             : 
     644     1310285 : void set_bw_stereo(
     645             :     CPE_ENC_HANDLE hCPE /* i/o: CPE encoder structures     */
     646             : )
     647             : {
     648     1310285 :     Encoder_State **sts = hCPE->hCoreCoder;
     649             : 
     650     1310285 :     if ( hCPE->element_brate > BWD_MAX_BRATE_WIDER_BW_MDCT )
     651             :     {
     652      289807 :         sts[0]->bwidth = sts[0]->max_bwidth;
     653      289807 :         sts[1]->bwidth = sts[1]->max_bwidth;
     654             :     }
     655     1020478 :     else if ( hCPE->element_mode == IVAS_CPE_MDCT )
     656             :     {
     657             :         /* ensure that both CPE channels have the same audio band-width */
     658     1020478 :         if ( sts[0]->input_bwidth == sts[1]->input_bwidth )
     659             :         {
     660     1008182 :             sts[0]->bwidth = sts[0]->input_bwidth;
     661     1008182 :             sts[1]->bwidth = sts[0]->input_bwidth;
     662             :         }
     663             :         else
     664             :         {
     665       12296 :             sts[0]->bwidth = max( sts[0]->input_bwidth, sts[1]->input_bwidth );
     666       12296 :             sts[1]->bwidth = max( sts[0]->input_bwidth, sts[1]->input_bwidth );
     667             :         }
     668             :     }
     669             : 
     670     1310285 :     sts[0]->bwidth = max( sts[0]->bwidth, WB );
     671     1310285 :     sts[1]->bwidth = max( sts[1]->bwidth, WB );
     672             : 
     673     1310285 :     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     1319864 : 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     1319864 :     mct_bwidth = WB;                                       /* minimum coded audio band-width */
     693     1319864 :     last_mct_bwidth = hCPE[0]->hCoreCoder[0]->last_bwidth; /* supposes that LFE is not in the first channel */
     694             : 
     695     5014592 :     for ( cpe_id = 0; cpe_id < nCPE; cpe_id++ )
     696             :     {
     697    11084184 :         for ( ch = 0; ch < CPE_CHANNELS; ch++ )
     698             :         {
     699     7389456 :             st = hCPE[cpe_id]->hCoreCoder[ch];
     700     7389456 :             if ( st->mct_chan_mode == MCT_CHAN_MODE_IGNORE )
     701             :             {
     702      794486 :                 continue;
     703             :             }
     704             : 
     705     6594970 :             mct_bwidth = max( mct_bwidth, st->input_bwidth );
     706             :         }
     707             :     }
     708             : 
     709     5014592 :     for ( cpe_id = 0; cpe_id < nCPE; cpe_id++ )
     710             :     {
     711     3694728 :         if ( hCPE[cpe_id]->element_brate > BWD_MAX_BRATE_WIDER_BW_MDCT )
     712             :         {
     713     2788030 :             mct_bwidth = max( mct_bwidth, hCPE[cpe_id]->hCoreCoder[0]->max_bwidth );
     714             :         }
     715             :     }
     716             : 
     717     1319864 :     bw_changed = 0;
     718     1319864 :     if ( mct_bwidth != last_mct_bwidth )
     719             :     {
     720        2260 :         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     5014592 :     for ( cpe_id = 0; cpe_id < nCPE; cpe_id++ )
     729             :     {
     730    11084184 :         for ( ch = 0; ch < CPE_CHANNELS; ch++ )
     731             :         {
     732     7389456 :             st = hCPE[cpe_id]->hCoreCoder[ch];
     733     7389456 :             st->bwidth = mct_bwidth;
     734             :         }
     735             :     }
     736             : 
     737     1319864 :     return bw_changed;
     738             : }

Generated by: LCOV version 1.14