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

Generated by: LCOV version 1.14