LCOV - code coverage report
Current view: top level - lib_com - ivas_cov_smooth.c (source / functions) Hit Total Coverage
Test: Coverage on main -- short test vectors @ 6c9ddc4024a9c0e1ecb8f643f114a84a0e26ec6b Lines: 88 91 96.7 %
Date: 2025-05-23 08:37:30 Functions: 7 7 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             : #include <stdint.h>
      34             : #include "options.h"
      35             : #ifdef DEBUGGING
      36             : #include "debug.h"
      37             : #endif
      38             : #include "cnst.h"
      39             : #include "ivas_prot.h"
      40             : #include "wmc_auto.h"
      41             : #include "prot.h"
      42             : 
      43             : /*-----------------------------------------------------------------------------------------*
      44             :  * Local constants
      45             :  *-----------------------------------------------------------------------------------------*/
      46             : 
      47             : #define BAND_SMOOTH_REST_START_IDX ( 2 )
      48             : 
      49             : /*-----------------------------------------------------------------------------------------*
      50             :  * Function ivas_calculate_update_factor()
      51             :  *
      52             :  * To calculate the update factor
      53             :  *-----------------------------------------------------------------------------------------*/
      54             : 
      55       40260 : static float ivas_calculate_update_factor(
      56             :     float *p_bin_to_band,
      57             :     int16_t active_bins )
      58             : {
      59       40260 :     float update_factor_temp = 0.0f;
      60             :     int16_t k;
      61             : 
      62      901120 :     for ( k = 0; k < active_bins; k++ )
      63             :     {
      64      860860 :         update_factor_temp += p_bin_to_band[k];
      65             :     }
      66             : 
      67       40260 :     return update_factor_temp;
      68             : }
      69             : 
      70             : 
      71             : /*-----------------------------------------------------------------------------------------*
      72             :  * Function ivas_calculate_smoothning_factor()
      73             :  *
      74             :  * To calculate the Smoothning factor
      75             :  *-----------------------------------------------------------------------------------------*/
      76             : 
      77       40260 : static void ivas_calculate_smoothning_factor(
      78             :     float *Smoothing_factor,
      79             :     float update_factor,
      80             :     const int16_t min_pool_size,
      81             :     const float max_update_rate,
      82             :     const COV_SMOOTHING_TYPE smooth_mode,
      83             :     const int32_t ivas_total_brate,
      84             :     int16_t j )
      85             : {
      86             :     float smooth_fact;
      87       40260 :     *Smoothing_factor = update_factor / min_pool_size;
      88       40260 :     if ( smooth_mode != COV_SMOOTH_MC )
      89             :     {
      90       39108 :         if ( ivas_total_brate < IVAS_24k4 )
      91             :         {
      92        7704 :             smooth_fact = 0.5f;
      93             :         }
      94             :         else
      95             :         {
      96       31404 :             smooth_fact = 0.75f;
      97             :         }
      98       39108 :         *Smoothing_factor *= ( j + 1 ) * smooth_fact;
      99             :     }
     100             : 
     101       40260 :     if ( *Smoothing_factor > max_update_rate )
     102             :     {
     103       21238 :         *Smoothing_factor = max_update_rate;
     104             :     }
     105             : 
     106       40260 :     return;
     107             : }
     108             : 
     109             : 
     110             : /*-----------------------------------------------------------------------------------------*
     111             :  * Function ivas_set_up_cov_smoothing()
     112             :  *
     113             :  * Setup for covariance smoothing
     114             :  *-----------------------------------------------------------------------------------------*/
     115             : 
     116        3368 : static void ivas_set_up_cov_smoothing(
     117             :     ivas_cov_smooth_state_t *hCovState,
     118             :     ivas_filterbank_t *pFb,
     119             :     const float max_update_rate,
     120             :     const int16_t min_pool_size,
     121             :     const COV_SMOOTHING_TYPE smooth_mode, /* i  : flag multichannel vs SPAR       */
     122             :     const int32_t ivas_total_brate )
     123             : {
     124             :     int16_t j;
     125             :     float update_factor;
     126        3368 :     if ( smooth_mode == COV_SMOOTH_MC )
     127             :     {
     128        1248 :         for ( j = 0; j < pFb->filterbank_num_bands; j++ )
     129             :         {
     130        1152 :             int16_t active_bins = pFb->fb_bin_to_band.pFb_active_bins_per_band[j];
     131        1152 :             update_factor = ivas_calculate_update_factor( pFb->fb_bin_to_band.pFb_bin_to_band[j], active_bins );
     132        1152 :             ivas_calculate_smoothning_factor( &hCovState->pSmoothing_factor[j], update_factor, min_pool_size, max_update_rate, smooth_mode, ivas_total_brate, j );
     133             :         }
     134             :     }
     135             :     else
     136             :     {
     137       42380 :         for ( j = 0; j < pFb->filterbank_num_bands; j++ )
     138             :         {
     139       39108 :             float *p_bin_to_band = pFb->fb_bin_to_band.pp_short_stride_bin_to_band[j];
     140       39108 :             int16_t active_bins = pFb->fb_bin_to_band.p_short_stride_num_bins_per_band[j];
     141       39108 :             update_factor = ivas_calculate_update_factor( p_bin_to_band, active_bins );
     142       39108 :             ivas_calculate_smoothning_factor( &hCovState->pSmoothing_factor[j], update_factor, min_pool_size, max_update_rate, smooth_mode, ivas_total_brate, j );
     143             :         }
     144             :     }
     145             : 
     146        3368 :     hCovState->prior_bank_idx = -1;
     147             : 
     148        3368 :     return;
     149             : }
     150             : 
     151             : 
     152             : /*-------------------------------------------------------------------------
     153             :  * ivas_spar_covar_smooth_enc_open()
     154             :  *
     155             :  * Allocate and initialize SPAR Covar. smoothing handle
     156             :  *------------------------------------------------------------------------*/
     157             : 
     158        3368 : ivas_error ivas_spar_covar_smooth_enc_open(
     159             :     ivas_cov_smooth_state_t **hCovState_out,     /* i/o: SPAR Covar. smoothing handle       */
     160             :     const ivas_cov_smooth_cfg_t *cov_smooth_cfg, /* i  : SPAR config. handle                */
     161             :     ivas_filterbank_t *pFb,                      /* i/o: FB handle                          */
     162             :     const int16_t nchan_inp,                     /* i  : number of input channels           */
     163             :     const COV_SMOOTHING_TYPE smooth_mode,        /* i  : Smooth covariance for SPAR or MC   */
     164             :     const int32_t ivas_total_brate               /* i  : IVAS total bitrate                 */
     165             : )
     166             : {
     167             :     ivas_cov_smooth_state_t *hCovState;
     168             :     int16_t i, j;
     169             : 
     170        3368 :     if ( ( hCovState = (ivas_cov_smooth_state_t *) malloc( sizeof( ivas_cov_smooth_state_t ) ) ) == NULL )
     171             :     {
     172           0 :         return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for SPAR COV encoder" );
     173             :     }
     174             : 
     175        3368 :     if ( ( hCovState->pSmoothing_factor = (float *) malloc( sizeof( float ) * cov_smooth_cfg->max_bands ) ) == NULL )
     176             :     {
     177           0 :         return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for SPAR COV encoder" );
     178             :     }
     179             : 
     180       19094 :     for ( i = 0; i < nchan_inp; i++ )
     181             :     {
     182       99972 :         for ( j = 0; j < nchan_inp; j++ )
     183             :         {
     184       84246 :             if ( ( hCovState->pPrior_cov_real[i][j] = (float *) malloc( sizeof( float ) * cov_smooth_cfg->max_bands ) ) == NULL )
     185             :             {
     186           0 :                 return IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for SPAR COV encoder" );
     187             :             }
     188       84246 :             set_zero( hCovState->pPrior_cov_real[i][j], cov_smooth_cfg->max_bands );
     189             :         }
     190             :     }
     191             : 
     192        3368 :     ivas_set_up_cov_smoothing( hCovState, pFb, cov_smooth_cfg->max_update_rate, cov_smooth_cfg->min_pool_size, smooth_mode, ivas_total_brate );
     193             : 
     194        3368 :     *hCovState_out = hCovState;
     195             : 
     196        3368 :     return IVAS_ERR_OK;
     197             : }
     198             : 
     199             : 
     200             : /*-------------------------------------------------------------------------
     201             :  * ivas_spar_covar_smooth_enc_close()
     202             :  *
     203             :  * Deallocate SPAR Covar. smoothing handle
     204             :  *------------------------------------------------------------------------*/
     205             : 
     206        3368 : void ivas_spar_covar_smooth_enc_close(
     207             :     ivas_cov_smooth_state_t **hCovState_out, /* i/o: SPAR Covar. smoothing handle   */
     208             :     const int16_t nchan_inp                  /* i  : number of input channels       */
     209             : )
     210             : {
     211             :     ivas_cov_smooth_state_t *hCovState;
     212             :     int16_t i, j;
     213             : 
     214        3368 :     hCovState = *hCovState_out;
     215             : 
     216        3368 :     if ( hCovState != NULL )
     217             :     {
     218        3368 :         free( hCovState->pSmoothing_factor );
     219        3368 :         hCovState->pSmoothing_factor = NULL;
     220             : 
     221       19094 :         for ( i = 0; i < nchan_inp; i++ )
     222             :         {
     223       99972 :             for ( j = 0; j < nchan_inp; j++ )
     224             :             {
     225       84246 :                 free( hCovState->pPrior_cov_real[i][j] );
     226       84246 :                 hCovState->pPrior_cov_real[i][j] = NULL;
     227             :             }
     228             :         }
     229             : 
     230        3368 :         free( hCovState );
     231        3368 :         hCovState_out = NULL;
     232             :     }
     233             : 
     234        3368 :     return;
     235             : }
     236             : 
     237             : 
     238             : /*-----------------------------------------------------------------------------------------*
     239             :  * Function ivas_compute_smooth_cov()
     240             :  *
     241             :  * Compute smooth covariance real/imag.
     242             :  *-----------------------------------------------------------------------------------------*/
     243             : 
     244      168583 : static void ivas_compute_smooth_cov(
     245             :     ivas_cov_smooth_state_t *hCovState,
     246             :     ivas_filterbank_t *pFb,
     247             :     float *pCov_buf[IVAS_SPAR_MAX_CH][IVAS_SPAR_MAX_CH],
     248             :     float *pPrior_cov_buf[IVAS_SPAR_MAX_CH][IVAS_SPAR_MAX_CH],
     249             :     const float fac,
     250             :     const int16_t start_band,
     251             :     const int16_t end_band,
     252             :     const int16_t num_ch,
     253             :     const int16_t transient_det[2] )
     254             : {
     255             :     int16_t i, j, k;
     256      168583 :     int16_t prev_idx = hCovState->prior_bank_idx;
     257      168583 :     float factor = 0;
     258             :     int16_t sm_b;
     259             :     int16_t non_sm_b_idx;
     260      168583 :     sm_b = BAND_SMOOTH_REST_START_IDX;
     261             : 
     262      168583 :     assert( end_band <= pFb->filterbank_num_bands );
     263             : 
     264      168583 :     if ( prev_idx == -1 || transient_det[1] == 1 )
     265             :     {
     266       42780 :         for ( i = 0; i < num_ch; i++ )
     267             :         {
     268      447627 :             for ( k = start_band; k < end_band; k++ )
     269             :             {
     270      412760 :                 pCov_buf[i][i][k] += ( hCovState->pSmoothing_factor[k] * fac );
     271             :             }
     272             :         }
     273             :     }
     274      160670 :     else if ( transient_det[0] == 1 )
     275             :     {
     276       10274 :         non_sm_b_idx = min( sm_b, end_band );
     277       57267 :         for ( i = 0; i < num_ch; i++ )
     278             :         {
     279      290558 :             for ( j = 0; j < num_ch; j++ )
     280             :             {
     281      243565 :                 if ( i == j )
     282             :                 {
     283       46993 :                     factor = fac;
     284             :                 }
     285             :                 else
     286             :                 {
     287      196572 :                     factor = 0.0f;
     288             :                 }
     289             : 
     290      730695 :                 for ( k = start_band; k < non_sm_b_idx; k++ )
     291             :                 {
     292      487130 :                     pCov_buf[i][j][k] = pPrior_cov_buf[i][j][k] + ( hCovState->pSmoothing_factor[k] * ( pCov_buf[i][j][k] - pPrior_cov_buf[i][j][k] + factor ) );
     293             :                 }
     294             :             }
     295             :         }
     296       57267 :         for ( i = 0; i < num_ch; i++ )
     297             :         {
     298      511235 :             for ( k = non_sm_b_idx; k < end_band; k++ )
     299             :             {
     300      464242 :                 pCov_buf[i][i][k] += ( hCovState->pSmoothing_factor[k] * fac );
     301             :             }
     302             :         }
     303             :     }
     304      150396 :     else if ( prev_idx == 0 )
     305             :     {
     306      833452 :         for ( i = 0; i < num_ch; i++ )
     307             :         {
     308     4265192 :             for ( j = 0; j < num_ch; j++ )
     309             :             {
     310     3582136 :                 if ( i == j )
     311             :                 {
     312      683056 :                     factor = fac;
     313             :                 }
     314             :                 else
     315             :                 {
     316     2899080 :                     factor = 0.0f;
     317             :                 }
     318             : 
     319    46191328 :                 for ( k = start_band; k < end_band; k++ )
     320             :                 {
     321    42609192 :                     pCov_buf[i][j][k] = pPrior_cov_buf[i][j][k] + ( hCovState->pSmoothing_factor[k] * ( pCov_buf[i][j][k] - pPrior_cov_buf[i][j][k] + factor ) );
     322             :                 }
     323             :             }
     324             :         }
     325             :     }
     326             : 
     327      168583 :     return;
     328             : }
     329             : 
     330             : 
     331             : /*-----------------------------------------------------------------------------------------*
     332             :  * Function ivas_cov_smooth_process()
     333             :  *
     334             :  * Covariance smoothing process
     335             :  *-----------------------------------------------------------------------------------------*/
     336             : 
     337      168583 : void ivas_cov_smooth_process(
     338             :     ivas_cov_smooth_state_t *hCovState, /* i/o: Covariance state handle */
     339             :     float *cov_real[IVAS_SPAR_MAX_CH][IVAS_SPAR_MAX_CH],
     340             :     ivas_filterbank_t *pFb, /* i/o: FB handle               */
     341             :     const int16_t start_band,
     342             :     const int16_t end_band,
     343             :     const int16_t num_ch,
     344             :     const int16_t transient_det[2] )
     345             : {
     346             :     int16_t i, j;
     347      168583 :     int16_t num_bands = end_band - start_band;
     348             : 
     349      168583 :     ivas_compute_smooth_cov( hCovState, pFb, cov_real, hCovState->pPrior_cov_real, 1e-20f, start_band, end_band, num_ch, transient_det );
     350             : 
     351      933499 :     for ( i = 0; i < num_ch; i++ )
     352             :     {
     353     4769008 :         for ( j = 0; j < num_ch; j++ )
     354             :         {
     355     4004092 :             mvr2r( &cov_real[i][j][start_band], &hCovState->pPrior_cov_real[i][j][start_band], num_bands );
     356             :         }
     357             :     }
     358             : 
     359      168583 :     hCovState->prior_bank_idx = 0;
     360             : 
     361      168583 :     return;
     362             : }

Generated by: LCOV version 1.14