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

Generated by: LCOV version 1.14