LCOV - code coverage report
Current view: top level - lib_enc - bw_detect.c (source / functions) Hit Total Coverage
Test: Coverage on main -- conformance test test_26252.py @ efe53129c9ed87a5067dd0a8fb9dca41db9c4add Lines: 251 273 91.9 %
Date: 2026-02-12 06:30:15 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      812262 : 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      812262 :     int16_t cldfb_bin_width = 4;
      92             :     int16_t bwd_count_wider_bw, l_frame;
      93             : 
      94      812262 :     bwd_count_wider_bw = BWD_COUNT_WIDER_BW;
      95      812262 :     if ( st->ini_frame > 0 && ( ( st->element_mode == IVAS_CPE_MDCT && ( st->element_brate >= BWD_MIN_BRATE_WIDER_BW_MDCT || mct_on ) ) ||
      96      321744 :                                 ( st->is_ism_format && st->element_brate >= BWD_MIN_BRATE_WIDER_BW_ISM ) ||
      97       20222 :                                 ( ivas_format == MASA_FORMAT && st->element_brate >= BWD_MIN_BRATE_WIDER_BW_MASA ) ) )
      98             :     {
      99      599966 :         bwd_count_wider_bw = BWD_COUNT_WIDER_BW_MDCT;
     100             :     }
     101             : 
     102      812262 :     if ( st->input_Fs > 8000 )
     103             :     {
     104      812262 :         if ( enerBuffer != NULL )
     105             :         {
     106             :             float ScalFac;
     107             : 
     108      327317 :             ScalFac = 1 / ( st->cldfbAnaEnc->scale * st->cldfbAnaEnc->scale * 8.f );
     109      327317 :             set_f( cldfb_bin, 0.001f, 9 );
     110             : 
     111             :             /* NB: 1.2 - 2.8 kHz, 4 cldfb-bands*/
     112      327317 :             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      327317 :             if ( st->input_Fs >= 16000 )
     116             :             {
     117      327317 :                 cldfb_bin[1] += sum_f( &( enerBuffer[11] ), cldfb_bin_width );
     118      327317 :                 cldfb_bin[2] += sum_f( &( enerBuffer[14] ), cldfb_bin_width );
     119             :             }
     120             : 
     121             :             /* SWB: 9.2 - 15.6 kHz, 16 cldfb-bands */
     122      327317 :             if ( st->input_Fs >= 32000 )
     123             :             {
     124      315367 :                 cldfb_bin[3] += sum_f( &( enerBuffer[23] ), cldfb_bin_width );
     125      315367 :                 cldfb_bin[4] += sum_f( &( enerBuffer[27] ), cldfb_bin_width );
     126      315367 :                 cldfb_bin[5] += sum_f( &( enerBuffer[31] ), cldfb_bin_width );
     127      315367 :                 cldfb_bin[6] += sum_f( &( enerBuffer[35] ), cldfb_bin_width );
     128             :             }
     129             : 
     130             :             /* FB:  16.8 - 20.0 kHz, 8 cldfb-bands */
     131      327317 :             if ( st->input_Fs >= 48000 )
     132             :             {
     133      289787 :                 cldfb_bin[7] += sum_f( &( enerBuffer[42] ), cldfb_bin_width );
     134      289787 :                 cldfb_bin[8] += sum_f( &( enerBuffer[46] ), cldfb_bin_width );
     135             :             }
     136             : 
     137     3273170 :             for ( i = 0; i < 9; i++ )
     138             :             {
     139     2945853 :                 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      484945 :             if ( st->input_Fs == 16000 )
     146             :             {
     147       30607 :                 bw_max = WB;
     148       30607 :                 bin_width = 60;
     149       30607 :                 n_bins = 5; /* spectrum to 7.5 kHz */
     150             :             }
     151      454338 :             else if ( st->input_Fs == 32000 )
     152             :             {
     153       96674 :                 bw_max = SWB;
     154       96674 :                 bin_width = 30;
     155       96674 :                 n_bins = 10; /* spectrum to 15 kHz */
     156             :             }
     157             :             else /* st->input_Fs == 48000 */
     158             :             {
     159      357664 :                 bw_max = FB;
     160      357664 :                 bin_width = 20;
     161      357664 :                 n_bins = BWD_N_BINS_MAX; /* spectrum to 19.5 kHz */
     162             :             }
     163             : 
     164      484945 :             if ( signal_in != NULL )
     165             :             {
     166             :                 /*---------------------------------------------------------------------*
     167             :                  * windowing of the input signal
     168             :                  *---------------------------------------------------------------------*/
     169             : 
     170           0 :                 pt = signal_in;
     171           0 :                 pt1 = hann_window_320;
     172             : 
     173             :                 /* 1st half of the window */
     174           0 :                 for ( i = 0; i < BWD_TOTAL_WIDTH / 2; i++ )
     175             :                 {
     176           0 :                     in_win[i] = *pt++ * *pt1++;
     177             :                 }
     178           0 :                 pt1--;
     179             : 
     180             :                 /* 2nd half of the window */
     181           0 :                 for ( ; i < BWD_TOTAL_WIDTH; i++ )
     182             :                 {
     183           0 :                     in_win[i] = *pt++ * *pt1--;
     184             :                 }
     185             : 
     186             :                 /*---------------------------------------------------------------------*
     187             :                  * tranform into frequency domain
     188             :                  *---------------------------------------------------------------------*/
     189             : 
     190           0 :                 edct( in_win, spect, BWD_TOTAL_WIDTH, st->element_mode );
     191             :             }
     192             :             else
     193             :             {
     194      484945 :                 l_frame = (int16_t) ( st->input_Fs / FRAMES_PER_SEC );
     195      484945 :                 if ( st->core == TCX_10_CORE )
     196             :                 {
     197       22550 :                     l_frame /= 2;
     198             :                 }
     199             : 
     200      484945 :                 bin_width *= ( l_frame / BWD_TOTAL_WIDTH );
     201      484945 :                 mvr2r( spectrum, spect, l_frame );
     202             :             }
     203             :             /*---------------------------------------------------------------------*
     204             :              * compute energy per spectral bins
     205             :              *---------------------------------------------------------------------*/
     206             : 
     207      484945 :             set_f( spect_bin, 0.001f, n_bins );
     208             : 
     209     2266837 :             for ( k = 0; k <= bw_max; k++ )
     210             :             {
     211     5769407 :                 for ( i = bwd_start_bin[k]; i <= bwd_end_bin[k]; i++ )
     212             :                 {
     213   236109335 :                     for ( j = 0; j < bin_width; j++ )
     214             :                     {
     215   232121820 :                         spect_bin[i] += spect[i * bin_width + j] * spect[i * bin_width + j];
     216             :                     }
     217     3987515 :                     spect_bin[i] = (float) log10( spect_bin[i] );
     218             :                 }
     219             :             }
     220             :         }
     221             : 
     222      812262 :         if ( enerBuffer != NULL )
     223             :         {
     224             :             /* cldfb detections */
     225      327317 :             mean_NB = mean( cldfb_bin, 1 ); /* NB: 1.2 - 2.8 kHz, 4 cldfb-bands (1 bin)   */
     226      327317 :             maximum( cldfb_bin, 1, &max_NB );
     227      327317 :             mean_WB = mean( cldfb_bin + 1, 2 ); /* WB: 4.4 - 7.2 kHz, 8 cldfb-bands (2 bins)  */
     228      327317 :             maximum( cldfb_bin + 1, 2, &max_WB );
     229             : 
     230      327317 :             mean_NB += CLDFB_ENER_OFFSET;
     231      327317 :             max_NB += CLDFB_ENER_OFFSET;
     232      327317 :             mean_WB += CLDFB_ENER_OFFSET;
     233      327317 :             max_WB += CLDFB_ENER_OFFSET;
     234             : 
     235      327317 :             if ( st->input_Fs == 16000 )
     236             :             {
     237             :                 /* for 16kHz sampled inputs, do not check SWB & FB */
     238       11950 :                 mean_SWB = 0.0f;
     239       11950 :                 max_SWB = 0.0f;
     240       11950 :                 mean_FB = 0.0f;
     241       11950 :                 max_FB = 0.0f;
     242             :             }
     243      315367 :             else if ( st->input_Fs == 32000 )
     244             :             {
     245             :                 /* for 32kHz sampled inputs, do not check FB */
     246       25580 :                 mean_FB = 0.0f;
     247       25580 :                 max_FB = 0.0f;
     248       25580 :                 mean_SWB = mean( cldfb_bin + 3, 4 ); /*  SWB: 9.2 - 15.6 kHz, 16 cldfb-bands (4 bins) */
     249       25580 :                 maximum( cldfb_bin + 3, 4, &max_SWB );
     250       25580 :                 mean_SWB += CLDFB_ENER_OFFSET;
     251       25580 :                 max_SWB += CLDFB_ENER_OFFSET;
     252             :             }
     253             :             else
     254             :             {
     255      289787 :                 mean_SWB = mean( cldfb_bin + 3, 4 ); /*  SWB: 9.2 - 15.6 kHz, 16 cldfb-bands (4 bins) */
     256      289787 :                 maximum( cldfb_bin + 3, 4, &max_SWB );
     257      289787 :                 mean_FB = mean( cldfb_bin + 7, 2 ); /*  FB: 16.8 - 20.0 kHz, 8 cldfb-bands (2 bins) */
     258      289787 :                 maximum( cldfb_bin + 7, 2, &max_FB );
     259             : 
     260      289787 :                 mean_SWB += CLDFB_ENER_OFFSET;
     261      289787 :                 max_SWB += CLDFB_ENER_OFFSET;
     262      289787 :                 mean_FB += CLDFB_ENER_OFFSET;
     263      289787 :                 max_FB += CLDFB_ENER_OFFSET;
     264             :             }
     265             :         }
     266             :         else
     267             :         {
     268      484945 :             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      484945 :             maximum( spect_bin + bwd_start_bin[0], bwd_end_bin[0] - bwd_start_bin[0] + 1, &max_NB );
     270      484945 :             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      484945 :             maximum( spect_bin + bwd_start_bin[1], bwd_end_bin[1] - bwd_start_bin[1] + 1, &max_WB );
     272             : 
     273      484945 :             if ( st->input_Fs == 16000 )
     274             :             {
     275             :                 /* for 16kHz sampled inputs, do not check SWB & FB */
     276       30607 :                 mean_SWB = 0.0f;
     277       30607 :                 max_SWB = 0.0f;
     278       30607 :                 mean_FB = 0.0f;
     279       30607 :                 max_FB = 0.0f;
     280             :             }
     281      454338 :             else if ( st->input_Fs == 32000 )
     282             :             {
     283       96674 :                 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       96674 :                 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       96674 :                 mean_FB = 0.0f;
     288       96674 :                 max_FB = 0.0f;
     289             :             }
     290             :             else
     291             :             {
     292      357664 :                 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      357664 :                 maximum( spect_bin + bwd_start_bin[2], bwd_end_bin[2] - bwd_start_bin[2] + 1, &max_SWB );
     294      357664 :                 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      357664 :                 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      812262 :         if ( st->localVAD || st->lp_noise > 30 )
     304             :         {
     305      747562 :             st->lt_mean_NB = ALPHA_BWD * st->lt_mean_NB + ( 1 - ALPHA_BWD ) * mean_NB;
     306      747562 :             st->lt_mean_WB = ALPHA_BWD * st->lt_mean_WB + ( 1 - ALPHA_BWD ) * mean_WB;
     307      747562 :             st->lt_mean_SWB = ALPHA_BWD * st->lt_mean_SWB + ( 1 - ALPHA_BWD ) * mean_SWB;
     308             : 
     309      747562 :             if ( enerBuffer != NULL )
     310             :             {
     311      284574 :                 if ( 0.9f * max_WB > BWD_LT_THRESH * st->lt_mean_NB )
     312             :                 {
     313      204986 :                     if ( 2.5f * max_WB > max_NB )
     314             :                     {
     315      204833 :                         st->count_WB++;
     316             :                     }
     317             :                 }
     318             :                 else
     319             :                 {
     320       79588 :                     if ( 3.5f * mean_WB < mean_NB )
     321             :                     {
     322        2629 :                         st->count_WB--;
     323             :                     }
     324             :                 }
     325             : 
     326      284574 :                 if ( 0.83f * max_SWB > BWD_LT_THRESH * st->lt_mean_WB && max_WB > BWD_LT_THRESH * st->lt_mean_NB )
     327             :                 {
     328      207750 :                     if ( 2 * max_SWB > max_WB )
     329             :                     {
     330      207369 :                         st->count_SWB++;
     331             :                     }
     332             :                 }
     333             :                 else
     334             :                 {
     335       76824 :                     if ( 3 * mean_SWB < mean_WB )
     336             :                     {
     337       15022 :                         st->count_SWB--;
     338             :                     }
     339             :                 }
     340             : 
     341      284574 :                 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      147705 :                     if ( 3 * max_FB > max_SWB )
     344             :                     {
     345      147350 :                         st->count_FB++;
     346             :                     }
     347             :                 }
     348             :                 else
     349             :                 {
     350      136869 :                     if ( 4.1f * mean_FB < mean_SWB )
     351             :                     {
     352       59706 :                         st->count_FB--;
     353             :                     }
     354             :                 }
     355             :             }
     356             :             else
     357             :             {
     358      462988 :                 if ( max_WB > BWD_LT_THRESH * st->lt_mean_NB )
     359             :                 {
     360      409151 :                     if ( 2 * max_WB > max_NB )
     361             :                     {
     362      408354 :                         st->count_WB++;
     363             :                     }
     364             :                 }
     365             :                 else
     366             :                 {
     367       53837 :                     if ( 2.6f * mean_WB < mean_NB )
     368             :                     {
     369        7700 :                         st->count_WB--;
     370             :                     }
     371             :                 }
     372             : 
     373      462988 :                 if ( max_SWB > BWD_LT_THRESH * st->lt_mean_WB && max_WB > BWD_LT_THRESH * st->lt_mean_NB )
     374             :                 {
     375      374282 :                     if ( 2 * max_SWB > max_WB )
     376             :                     {
     377      372943 :                         st->count_SWB++;
     378             :                     }
     379             :                 }
     380             :                 else
     381             :                 {
     382       88706 :                     if ( 3 * mean_SWB < mean_WB )
     383             :                     {
     384       29714 :                         st->count_SWB--;
     385             :                     }
     386             :                 }
     387             : 
     388      462988 :                 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      275406 :                     if ( 2 * max_FB > max_SWB )
     391             :                     {
     392      272720 :                         st->count_FB++;
     393             :                     }
     394             :                 }
     395             :                 else
     396             :                 {
     397      187582 :                     if ( 3 * mean_FB < mean_SWB )
     398             :                     {
     399       94913 :                         st->count_FB--;
     400             :                     }
     401             :                 }
     402             :             }
     403             : 
     404      747562 :             st->count_WB = min( st->count_WB, BWD_COUNT_MAX );
     405      747562 :             st->count_SWB = min( st->count_SWB, BWD_COUNT_MAX );
     406      747562 :             st->count_FB = min( st->count_FB, BWD_COUNT_MAX );
     407      747562 :             st->count_WB = max( st->count_WB, 0 );
     408      747562 :             st->count_SWB = max( st->count_SWB, 0 );
     409      747562 :             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      747562 :             if ( st->last_input_bwidth == NB )
     418             :             {
     419         485 :                 if ( st->count_WB > bwd_count_wider_bw )
     420             :                 {
     421           6 :                     st->input_bwidth = WB;
     422           6 :                     st->count_WB = BWD_COUNT_MAX;
     423             : 
     424           6 :                     if ( st->count_SWB > bwd_count_wider_bw )
     425             :                     {
     426           2 :                         st->input_bwidth = SWB;
     427           2 :                         st->count_SWB = BWD_COUNT_MAX;
     428             : 
     429           2 :                         if ( st->count_FB > bwd_count_wider_bw )
     430             :                         {
     431           0 :                             st->input_bwidth = FB;
     432           0 :                             st->count_FB = BWD_COUNT_MAX;
     433             :                         }
     434             :                     }
     435             :                 }
     436             :             }
     437             : 
     438      747562 :             if ( st->last_input_bwidth == WB && st->input_Fs > 16000 )
     439             :             {
     440       30453 :                 if ( st->count_SWB > bwd_count_wider_bw )
     441             :                 {
     442       30451 :                     st->input_bwidth = SWB;
     443       30451 :                     st->count_SWB = BWD_COUNT_MAX;
     444             : 
     445       30451 :                     if ( st->count_FB > bwd_count_wider_bw )
     446             :                     {
     447       30449 :                         st->input_bwidth = FB;
     448       30449 :                         st->count_FB = BWD_COUNT_MAX;
     449             :                     }
     450             :                 }
     451             :             }
     452             : 
     453      747562 :             if ( st->last_input_bwidth == SWB && st->input_Fs > 32000 )
     454             :             {
     455       94662 :                 if ( st->count_FB > bwd_count_wider_bw )
     456             :                 {
     457       85607 :                     st->input_bwidth = FB;
     458       85607 :                     st->count_FB = BWD_COUNT_MAX;
     459             :                 }
     460             :             }
     461             : 
     462             :             /* switching to a lower BW */
     463      747562 :             if ( st->last_input_bwidth == FB )
     464             :             {
     465      473123 :                 if ( st->count_FB < 10 )
     466             :                 {
     467          20 :                     st->input_bwidth = SWB;
     468          20 :                     st->count_FB = 0;
     469             :                 }
     470      473123 :                 if ( st->count_SWB < 10 )
     471             :                 {
     472           0 :                     st->input_bwidth = WB;
     473           0 :                     st->count_SWB = 0;
     474           0 :                     st->count_FB = 0;
     475             :                 }
     476      473123 :                 if ( st->count_WB < 10 )
     477             :                 {
     478           3 :                     st->input_bwidth = NB;
     479           3 :                     st->count_WB = 0;
     480           3 :                     st->count_SWB = 0;
     481           3 :                     st->count_FB = 0;
     482             :                 }
     483             :             }
     484             : 
     485      747562 :             if ( st->last_input_bwidth == SWB )
     486             :             {
     487      202685 :                 if ( st->count_SWB < 10 )
     488             :                 {
     489           0 :                     st->input_bwidth = WB;
     490           0 :                     st->count_SWB = 0;
     491           0 :                     st->count_FB = 0;
     492             :                 }
     493      202685 :                 if ( st->count_WB < 10 )
     494             :                 {
     495           7 :                     st->input_bwidth = NB;
     496           7 :                     st->count_WB = 0;
     497           7 :                     st->count_SWB = 0;
     498           7 :                     st->count_FB = 0;
     499             :                 }
     500             :             }
     501             : 
     502      747562 :             if ( st->last_input_bwidth == WB )
     503             :             {
     504       71269 :                 if ( st->count_WB < 10 )
     505             :                 {
     506           3 :                     st->input_bwidth = NB;
     507           3 :                     st->count_WB = 0;
     508           3 :                     st->count_SWB = 0;
     509           3 :                     st->count_FB = 0;
     510             :                 }
     511             :             }
     512             :         }
     513             :     }
     514             : 
     515             :     /* verify that maximum encoded bandwidth (specified on the command line) is not exceeded */
     516      812262 :     if ( st->input_bwidth > st->max_bwidth )
     517             :     {
     518      116190 :         st->input_bwidth = st->max_bwidth;
     519             :     }
     520             : 
     521      812262 :     if ( st->element_mode == EVS_MONO )
     522             :     {
     523        5150 :         set_bw( -1, -1, st, st->codec_mode );
     524             :     }
     525             : 
     526      812262 :     return;
     527             : }
     528             : 
     529             : /*-------------------------------------------------------------------*
     530             :  * set_bw()
     531             :  *
     532             :  * Set and limit the encoded bandwidth
     533             :  *-------------------------------------------------------------------*/
     534             : 
     535      362876 : 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      362876 :     st->bwidth = st->input_bwidth;
     544             : 
     545      362876 :     if ( codec_mode == MODE1 )
     546             :     {
     547             :         int32_t total_brate;
     548             : 
     549      360776 :         st->bwidth = st->input_bwidth;
     550      360776 :         total_brate = st->total_brate;
     551             : 
     552      360776 :         if ( element_mode > IVAS_SCE )
     553             :         {
     554       54476 :             if ( element_brate < MIN_BRATE_SWB_STEREO )
     555             :             {
     556           0 :                 st->bwidth = WB;
     557             :             }
     558             :             else
     559             :             {
     560       54476 :                 if ( st->idchan == 0 || element_mode == IVAS_CPE_MDCT )
     561             :                 {
     562       52414 :                     if ( element_brate >= MIN_BRATE_FB_STEREO )
     563             :                     {
     564       16898 :                         st->bwidth = min( st->bwidth, FB );
     565             :                     }
     566             :                     else
     567             :                     {
     568       35516 :                         st->bwidth = min( st->bwidth, SWB );
     569             :                     }
     570       52414 :                     st->bwidth = max( st->bwidth, WB );
     571             :                 }
     572             :                 else
     573             :                 {
     574        2062 :                     st->bwidth = WB;
     575             :                 }
     576             :             }
     577             :         }
     578      306300 :         else if ( element_mode == IVAS_SCE )
     579             :         {
     580      303250 :             if ( element_brate < MIN_BRATE_SWB_SCE || st->bwidth < WB )
     581             :             {
     582        4013 :                 st->bwidth = WB;
     583             :             }
     584      299237 :             else if ( st->bwidth > SWB && ( ( element_brate < MIN_BRATE_FB_STEREO && !st->is_ism_format ) ||
     585        5876 :                                             ( element_brate < MIN_BRATE_FB_ISM && st->is_ism_format ) ) )
     586             :             {
     587        6301 :                 st->bwidth = SWB;
     588             :             }
     589      292936 :             else if ( element_brate > BWD_MAX_BRATE_WIDER_BW_ISM )
     590             :             {
     591       28166 :                 st->bwidth = st->max_bwidth;
     592             :             }
     593             :         }
     594             :         /* element_mode == EVS_MONO */
     595        3050 :         else if ( total_brate <= ACELP_9k60 && st->bwidth > WB )
     596             :         {
     597           0 :             st->bwidth = WB;
     598             :         }
     599        3050 :         else if ( total_brate >= ACELP_13k20 && total_brate <= ACELP_16k40 && st->bwidth > SWB )
     600             :         {
     601           0 :             st->bwidth = SWB;
     602             :         }
     603        3050 :         else if ( total_brate >= ACELP_32k && st->bwidth < WB )
     604             :         {
     605           0 :             st->bwidth = WB;
     606             :         }
     607             :     }
     608        2100 :     else if ( codec_mode == MODE2 )
     609             :     {
     610             :         int16_t n, bits_frame_nominal, tmpBandwidthMin;
     611             : 
     612        2100 :         bits_frame_nominal = (int16_t) ( st->total_brate / FRAMES_PER_SEC );
     613       16800 :         for ( n = 0; n < FRAME_SIZE_NB; n++ )
     614             :         {
     615       16800 :             if ( FrameSizeConfig[n].frame_bits == bits_frame_nominal )
     616             :             {
     617        2100 :                 break;
     618             :             }
     619             :         }
     620        2100 :         if ( n == FRAME_SIZE_NB )
     621             :         {
     622           0 :             assert( !"Bitrate not supported: not part of EVS" );
     623             :         }
     624             : 
     625        2100 :         tmpBandwidthMin = FrameSizeConfig[n].bandwidth_min;
     626             : 
     627        2100 :         if ( st->rf_mode )
     628             :         {
     629           0 :             tmpBandwidthMin = WB;
     630             :         }
     631             : 
     632        2100 :         st->bwidth = max( min( st->input_bwidth, FrameSizeConfig[n].bandwidth_max ), tmpBandwidthMin );
     633             :     }
     634             : 
     635      362876 :     return;
     636             : }
     637             : 
     638             : /*-------------------------------------------------------------------*
     639             :  * set_bw_stereo()
     640             :  *
     641             :  * Set encoded bandwidth for stereo (CPE) channels
     642             :  *-------------------------------------------------------------------*/
     643             : 
     644       68460 : void set_bw_stereo(
     645             :     CPE_ENC_HANDLE hCPE /* i/o: CPE encoder structures     */
     646             : )
     647             : {
     648       68460 :     Encoder_State **sts = hCPE->hCoreCoder;
     649             : 
     650       68460 :     if ( hCPE->element_brate > BWD_MAX_BRATE_WIDER_BW_MDCT )
     651             :     {
     652       24579 :         sts[0]->bwidth = sts[0]->max_bwidth;
     653       24579 :         sts[1]->bwidth = sts[1]->max_bwidth;
     654             :     }
     655       43881 :     else if ( hCPE->element_mode == IVAS_CPE_MDCT )
     656             :     {
     657             :         /* ensure that both CPE channels have the same audio band-width */
     658       43881 :         if ( sts[0]->input_bwidth == sts[1]->input_bwidth )
     659             :         {
     660       42978 :             sts[0]->bwidth = sts[0]->input_bwidth;
     661       42978 :             sts[1]->bwidth = sts[0]->input_bwidth;
     662             :         }
     663             :         else
     664             :         {
     665         903 :             sts[0]->bwidth = max( sts[0]->input_bwidth, sts[1]->input_bwidth );
     666         903 :             sts[1]->bwidth = max( sts[0]->input_bwidth, sts[1]->input_bwidth );
     667             :         }
     668             :     }
     669             : 
     670       68460 :     sts[0]->bwidth = max( sts[0]->bwidth, WB );
     671       68460 :     sts[1]->bwidth = max( sts[1]->bwidth, WB );
     672             : 
     673       68460 :     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       69829 : 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       69829 :     mct_bwidth = WB;                                       /* minimum coded audio band-width */
     693       69829 :     last_mct_bwidth = hCPE[0]->hCoreCoder[0]->last_bwidth; /* supposes that LFE is not in the first channel */
     694             : 
     695      256267 :     for ( cpe_id = 0; cpe_id < nCPE; cpe_id++ )
     696             :     {
     697      559314 :         for ( ch = 0; ch < CPE_CHANNELS; ch++ )
     698             :         {
     699      372876 :             st = hCPE[cpe_id]->hCoreCoder[ch];
     700      372876 :             if ( st->mct_chan_mode == MCT_CHAN_MODE_IGNORE )
     701             :             {
     702       33579 :                 continue;
     703             :             }
     704             : 
     705      339297 :             mct_bwidth = max( mct_bwidth, st->input_bwidth );
     706             :         }
     707             :     }
     708             : 
     709      256267 :     for ( cpe_id = 0; cpe_id < nCPE; cpe_id++ )
     710             :     {
     711      186438 :         if ( hCPE[cpe_id]->element_brate > BWD_MAX_BRATE_WIDER_BW_MDCT )
     712             :         {
     713      154798 :             mct_bwidth = max( mct_bwidth, hCPE[cpe_id]->hCoreCoder[0]->max_bwidth );
     714             :         }
     715             :     }
     716             : 
     717       69829 :     bw_changed = 0;
     718       69829 :     if ( mct_bwidth != last_mct_bwidth )
     719             :     {
     720         179 :         bw_changed = 1;
     721             :     }
     722             : 
     723             :     /*
     724             :      * always set bw for all CPEs even if it is the same value as before,
     725             :      * in case of bw + br switching when changing to MCT, this overwrites
     726             :      * potentially incorrect initial values
     727             :      */
     728      256267 :     for ( cpe_id = 0; cpe_id < nCPE; cpe_id++ )
     729             :     {
     730      559314 :         for ( ch = 0; ch < CPE_CHANNELS; ch++ )
     731             :         {
     732      372876 :             st = hCPE[cpe_id]->hCoreCoder[ch];
     733      372876 :             st->bwidth = mct_bwidth;
     734             :         }
     735             :     }
     736             : 
     737       69829 :     return bw_changed;
     738             : }

Generated by: LCOV version 1.14