LCOV - code coverage report
Current view: top level - lib_com - swb_bwe_com_hr.c (source / functions) Hit Total Coverage
Test: Coverage on main @ 6baab0c613aa6c7100498ed7b93676aa8198a493 Lines: 96 98 98.0 %
Date: 2025-05-29 08:28:55 Functions: 3 3 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 <stdint.h>
      38             : #include "options.h"
      39             : #ifdef DEBUGGING
      40             : #include "debug.h"
      41             : #endif
      42             : #include <math.h>
      43             : #include "prot.h"
      44             : #include "wmc_auto.h"
      45             : 
      46             : /*-------------------------------------------------------------------*
      47             :  * swb_hr_nonzero_subband_noise_fill()
      48             :  *
      49             :  * SWB BWE HR noise filling of zero subbands
      50             :  *-------------------------------------------------------------------*/
      51             : 
      52       13478 : static void swb_hr_nonzero_subband_noise_fill(
      53             :     const float tilt_wb,        /* i  : tilt of wideband signal      */
      54             :     float *t_audio,             /* i/o: mdct spectrum                */
      55             :     int16_t *bwe_highrate_seed, /* i/o: seed of random noise         */
      56             :     const int16_t N,            /* i  : length of subband            */
      57             :     const int16_t Nsv           /* i  : number of subband            */
      58             : )
      59             : {
      60             :     int16_t i, j;
      61             :     float *ptr;
      62             :     float min_bwe, max_bwe, tmpF;
      63             : 
      64       13478 :     if ( tilt_wb > 5.0f )
      65             :     {
      66       48232 :         for ( i = 0; i < Nsv; i++ )
      67             :         {
      68       46328 :             min_bwe = FLT_MAX;
      69       46328 :             max_bwe = FLT_MIN;
      70      416952 :             for ( j = 0; j < N; j++ )
      71             :             {
      72      370624 :                 tmpF = (float) fabs( t_audio[i * N + j] );
      73             : 
      74      370624 :                 if ( tmpF > 0 )
      75             :                 {
      76      258054 :                     min_bwe = min( tmpF, min_bwe );
      77      258054 :                     max_bwe = max( tmpF, max_bwe );
      78             :                 }
      79             :             }
      80             : 
      81       46328 :             if ( max_bwe == min_bwe && min_bwe > 1.0f )
      82             :             {
      83       18604 :                 min_bwe *= 0.5f;
      84             :             }
      85             : 
      86       46328 :             ptr = &t_audio[i * N];
      87      416952 :             for ( j = 0; j < N; j++ )
      88             :             {
      89      370624 :                 if ( *ptr == 0 )
      90             :                 {
      91      112570 :                     if ( own_random( bwe_highrate_seed ) > 0 )
      92             :                     {
      93       56183 :                         *ptr = 0.5f * min_bwe;
      94             :                     }
      95             :                     else
      96             :                     {
      97       56387 :                         *ptr = -0.5f * min_bwe;
      98             :                     }
      99             :                 }
     100      370624 :                 ptr++;
     101             :             }
     102             :         }
     103             :     }
     104             : 
     105       13478 :     return;
     106             : }
     107             : 
     108             : /*-------------------------------------------------------------------*
     109             :  * swb_hr_noise_fill()
     110             :  *
     111             :  * SWB BWE HR noise filling
     112             :  *-------------------------------------------------------------------*/
     113             : 
     114       13478 : void swb_hr_noise_fill(
     115             :     const int16_t is_transient, /* i  : transient flag          */
     116             :     const int16_t spect_start,  /* i  : spectrum start point    */
     117             :     const int16_t spect_end,    /* i  : spectrum end point      */
     118             :     const float tilt_wb,        /* i  : tilt of wideband signal */
     119             :     const float pitch,          /* i  : pitch value             */
     120             :     const int16_t nq[],         /* i  : AVQ nq index            */
     121             :     int16_t Nsv,                /* i  : number of subband       */
     122             :     int16_t *bwe_highrate_seed, /* i/o: seed of random noise    */
     123             :     float *t_audio              /* i/o: mdct spectrum           */
     124             : )
     125             : {
     126             :     int16_t i, j, k;
     127             :     float alpha, beta;
     128             :     int16_t pos_start, pos_end, incr;
     129             : 
     130       13478 :     if ( is_transient )
     131             :     {
     132       32256 :         for ( i = 0; i < Nsv; i++ )
     133             :         {
     134       28672 :             if ( nq[i] == 0 )
     135             :             {
     136       73404 :                 for ( j = 0; j < WIDTH_BAND; j++ )
     137             :                 {
     138       65248 :                     t_audio[i * WIDTH_BAND + j] = own_random( bwe_highrate_seed ) / 65536.0f;
     139             :                 }
     140             :             }
     141             :         }
     142             :     }
     143             :     else
     144             :     {
     145        9894 :         Nsv = ( spect_end - spect_start ) / WIDTH_BAND;
     146        9894 :         alpha = 0.25f;
     147        9894 :         if ( tilt_wb > 5.0f )
     148             :         {
     149        1196 :             beta = 0.25f;
     150             :         }
     151             :         else
     152             :         {
     153        8698 :             if ( 100 > 0.25f * pitch )
     154             :             {
     155        3809 :                 beta = 0.25f;
     156             :             }
     157             :             else
     158             :             {
     159        4889 :                 beta = 100 / pitch;
     160             :             }
     161             :         }
     162             : 
     163        9894 :         i = 0;
     164        9894 :         if ( nq[i] == 0 )
     165             :         {
     166         125 :             i = 1;
     167         168 :             while ( i < Nsv && nq[i] == 0 )
     168             :             {
     169          43 :                 i++;
     170             :             }
     171             : 
     172         125 :             pos_start = i;
     173             : 
     174             : 
     175        2236 :             while ( i < Nsv && nq[i] != 0 )
     176             :             {
     177        2111 :                 i++;
     178             :             }
     179             : 
     180         125 :             pos_end = i - 1;
     181             : 
     182         125 :             if ( pos_end - pos_start > pos_start )
     183             :             {
     184         113 :                 pos_end = 2 * pos_start - 1;
     185             :             }
     186             : 
     187         125 :             incr = pos_end;
     188             : 
     189         293 :             for ( j = pos_start - 1; j >= 0; j-- )
     190             :             {
     191        1512 :                 for ( k = 0; k < WIDTH_BAND; k++ )
     192             :                 {
     193        1344 :                     t_audio[j * WIDTH_BAND + k] = alpha * t_audio[incr * WIDTH_BAND + k] + beta * own_random( bwe_highrate_seed ) / PCM16_TO_FLT_FAC;
     194             :                 }
     195             : 
     196         168 :                 incr = ( incr < pos_start ) ? pos_end : incr - 1;
     197             :             }
     198             :         }
     199             : 
     200      338943 :         while ( i < Nsv )
     201             :         {
     202      329049 :             if ( nq[i] == 0 )
     203             :             {
     204       12596 :                 pos_start = i;
     205       12596 :                 pos_end = i;
     206             : 
     207       30260 :                 for ( j = pos_start; j < Nsv; j++ )
     208             :                 {
     209       28529 :                     if ( nq[j] != 0 )
     210             :                     {
     211       10865 :                         i = j;
     212       10865 :                         pos_end = j;
     213       10865 :                         break;
     214             :                     }
     215             :                 }
     216             : 
     217       12596 :                 if ( pos_start == pos_end )
     218             :                 {
     219        1731 :                     i = Nsv;
     220        1731 :                     pos_end = Nsv;
     221             :                 }
     222             : 
     223       12596 :                 incr = ( pos_start - 1 );
     224             : 
     225       30260 :                 for ( j = pos_end - 1; j >= pos_start; j-- )
     226             :                 {
     227      158976 :                     for ( k = 0; k < WIDTH_BAND; k++ )
     228             :                     {
     229      141312 :                         t_audio[j * WIDTH_BAND + k] = alpha * t_audio[incr * WIDTH_BAND + k] + beta * own_random( bwe_highrate_seed ) / PCM16_TO_FLT_FAC;
     230             :                     }
     231       17664 :                     incr = ( incr == 0 ) ? ( pos_start - 1 ) : incr - 1;
     232             :                 }
     233             :             }
     234             :             else
     235             :             {
     236      316453 :                 i++;
     237             :             }
     238             :         }
     239             :     }
     240             : 
     241       13478 :     swb_hr_nonzero_subband_noise_fill( tilt_wb, t_audio, bwe_highrate_seed, WIDTH_BAND, Nsv );
     242             : 
     243       13478 :     return;
     244             : }
     245             : 
     246             : 
     247             : /*-------------------------------------------------------------------*
     248             :  * td_postprocess()
     249             :  *
     250             :  * post processing in time domain for td/fd switching
     251             :  *-------------------------------------------------------------------*/
     252             : 
     253             : /*! r: gain */
     254         143 : float td_postprocess(
     255             :     float hb_synth[],          /* i/o: high-band synthesis         */
     256             :     const int16_t input_frame, /* i  : frame length                */
     257             :     const int16_t last_extl    /* i  : last extension layer        */
     258             : )
     259             : {
     260             :     int16_t i;
     261             :     int16_t pos, ind1, ind2;
     262             :     float max_samp, gain, tmpF;
     263             :     float alpha, beta, step;
     264             : 
     265         143 :     max_samp = (float) fabs( hb_synth[0] );
     266         143 :     pos = 0;
     267      123840 :     for ( i = 1; i < input_frame; i++ )
     268             :     {
     269      123697 :         if ( fabs( hb_synth[i] ) > max_samp )
     270             :         {
     271        3957 :             max_samp = (float) fabs( hb_synth[i] );
     272        3957 :             pos = i;
     273             :         }
     274             :     }
     275             : 
     276         143 :     if ( pos < 160 )
     277             :     {
     278           2 :         gain = sum2_f( hb_synth + input_frame - 80, 80 ) + EPSILON;
     279             :     }
     280             :     else
     281             :     {
     282         141 :         gain = sum2_f( hb_synth, 80 ) + EPSILON;
     283             :     }
     284             : 
     285         143 :     gain = (float) sqrt( gain / 80 );
     286             : 
     287         143 :     ind1 = max( 0, pos - 40 );
     288         143 :     ind2 = min( input_frame, pos + 40 );
     289         143 :     tmpF = sum2_f( hb_synth + ind1, ind2 - ind1 ) + EPSILON;
     290         143 :     tmpF = (float) sqrt( tmpF / ( ind2 - ind1 ) );
     291             : 
     292         143 :     gain = min( 1.0f, gain / tmpF );
     293             : 
     294         143 :     if ( last_extl == SWB_BWE || last_extl == FB_BWE )
     295             :     {
     296           0 :         for ( i = ind1; i < input_frame; i++ )
     297             :         {
     298           0 :             hb_synth[i] *= gain;
     299             :         }
     300             :     }
     301             :     else
     302             :     {
     303       11423 :         for ( i = ind1; i < ind2; i++ )
     304             :         {
     305       11280 :             hb_synth[i] *= gain;
     306             :         }
     307             : 
     308         143 :         if ( ind2 != input_frame )
     309             :         {
     310         138 :             step = 0.0f;
     311         138 :             alpha = ( gain > 0.5f ) ? 1.0f : 0.5f;
     312         138 :             beta = ( alpha - gain ) / ( input_frame - ind2 );
     313             : 
     314       47990 :             for ( i = ind2; i < input_frame; i++ )
     315             :             {
     316       47852 :                 hb_synth[i] *= ( gain + step );
     317       47852 :                 step += beta;
     318             :             }
     319             :         }
     320             :     }
     321             : 
     322         143 :     return ( gain );
     323             : }

Generated by: LCOV version 1.14