LCOV - code coverage report
Current view: top level - lib_com - ivas_cov_smooth.c (source / functions) Hit Total Coverage
Test: Coverage on main @ 6baab0c613aa6c7100498ed7b93676aa8198a493 Lines: 88 91 96.7 %
Date: 2025-05-29 08:28:55 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      440552 : static float ivas_calculate_update_factor(
      56             :     float *p_bin_to_band,
      57             :     int16_t active_bins )
      58             : {
      59      440552 :     float update_factor_temp = 0.0f;
      60             :     int16_t k;
      61             : 
      62    14167728 :     for ( k = 0; k < active_bins; k++ )
      63             :     {
      64    13727176 :         update_factor_temp += p_bin_to_band[k];
      65             :     }
      66             : 
      67      440552 :     return update_factor_temp;
      68             : }
      69             : 
      70             : 
      71             : /*-----------------------------------------------------------------------------------------*
      72             :  * Function ivas_calculate_smoothning_factor()
      73             :  *
      74             :  * To calculate the Smoothning factor
      75             :  *-----------------------------------------------------------------------------------------*/
      76             : 
      77      440552 : 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      440552 :     *Smoothing_factor = update_factor / min_pool_size;
      88      440552 :     if ( smooth_mode != COV_SMOOTH_MC )
      89             :     {
      90      376344 :         if ( ivas_total_brate < IVAS_24k4 )
      91             :         {
      92       62952 :             smooth_fact = 0.5f;
      93             :         }
      94             :         else
      95             :         {
      96      313392 :             smooth_fact = 0.75f;
      97             :         }
      98      376344 :         *Smoothing_factor *= ( j + 1 ) * smooth_fact;
      99             :     }
     100             : 
     101      440552 :     if ( *Smoothing_factor > max_update_rate )
     102             :     {
     103      240189 :         *Smoothing_factor = max_update_rate;
     104             :     }
     105             : 
     106      440552 :     return;
     107             : }
     108             : 
     109             : 
     110             : /*-----------------------------------------------------------------------------------------*
     111             :  * Function ivas_set_up_cov_smoothing()
     112             :  *
     113             :  * Setup for covariance smoothing
     114             :  *-----------------------------------------------------------------------------------------*/
     115             : 
     116       36926 : 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       36926 :     if ( smooth_mode == COV_SMOOTH_MC )
     127             :     {
     128       69560 :         for ( j = 0; j < pFb->filterbank_num_bands; j++ )
     129             :         {
     130       64208 :             int16_t active_bins = pFb->fb_bin_to_band.pFb_active_bins_per_band[j];
     131       64208 :             update_factor = ivas_calculate_update_factor( pFb->fb_bin_to_band.pFb_bin_to_band[j], active_bins );
     132       64208 :             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      407918 :         for ( j = 0; j < pFb->filterbank_num_bands; j++ )
     138             :         {
     139      376344 :             float *p_bin_to_band = pFb->fb_bin_to_band.pp_short_stride_bin_to_band[j];
     140      376344 :             int16_t active_bins = pFb->fb_bin_to_band.p_short_stride_num_bins_per_band[j];
     141      376344 :             update_factor = ivas_calculate_update_factor( p_bin_to_band, active_bins );
     142      376344 :             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       36926 :     hCovState->prior_bank_idx = -1;
     147             : 
     148       36926 :     return;
     149             : }
     150             : 
     151             : 
     152             : /*-------------------------------------------------------------------------
     153             :  * ivas_spar_covar_smooth_enc_open()
     154             :  *
     155             :  * Allocate and initialize SPAR Covar. smoothing handle
     156             :  *------------------------------------------------------------------------*/
     157             : 
     158       36926 : 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       36926 :     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       36926 :     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      203652 :     for ( i = 0; i < nchan_inp; i++ )
     181             :     {
     182     1042560 :         for ( j = 0; j < nchan_inp; j++ )
     183             :         {
     184      875834 :             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      875834 :             set_zero( hCovState->pPrior_cov_real[i][j], cov_smooth_cfg->max_bands );
     189             :         }
     190             :     }
     191             : 
     192       36926 :     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       36926 :     *hCovState_out = hCovState;
     195             : 
     196       36926 :     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       36926 : 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       36926 :     hCovState = *hCovState_out;
     215             : 
     216       36926 :     if ( hCovState != NULL )
     217             :     {
     218       36926 :         free( hCovState->pSmoothing_factor );
     219       36926 :         hCovState->pSmoothing_factor = NULL;
     220             : 
     221      203652 :         for ( i = 0; i < nchan_inp; i++ )
     222             :         {
     223     1042560 :             for ( j = 0; j < nchan_inp; j++ )
     224             :             {
     225      875834 :                 free( hCovState->pPrior_cov_real[i][j] );
     226      875834 :                 hCovState->pPrior_cov_real[i][j] = NULL;
     227             :             }
     228             :         }
     229             : 
     230       36926 :         free( hCovState );
     231       36926 :         hCovState_out = NULL;
     232             :     }
     233             : 
     234       36926 :     return;
     235             : }
     236             : 
     237             : 
     238             : /*-----------------------------------------------------------------------------------------*
     239             :  * Function ivas_compute_smooth_cov()
     240             :  *
     241             :  * Compute smooth covariance real/imag.
     242             :  *-----------------------------------------------------------------------------------------*/
     243             : 
     244     2808287 : 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     2808287 :     int16_t prev_idx = hCovState->prior_bank_idx;
     257     2808287 :     float factor = 0;
     258             :     int16_t sm_b;
     259             :     int16_t non_sm_b_idx;
     260     2808287 :     sm_b = BAND_SMOOTH_REST_START_IDX;
     261             : 
     262     2808287 :     assert( end_band <= pFb->filterbank_num_bands );
     263             : 
     264     2808287 :     if ( prev_idx == -1 || transient_det[1] == 1 )
     265             :     {
     266      768215 :         for ( i = 0; i < num_ch; i++ )
     267             :         {
     268     7888386 :             for ( k = start_band; k < end_band; k++ )
     269             :             {
     270     7271032 :                 pCov_buf[i][i][k] += ( hCovState->pSmoothing_factor[k] * fac );
     271             :             }
     272             :         }
     273             :     }
     274     2657426 :     else if ( transient_det[0] == 1 )
     275             :     {
     276      149837 :         non_sm_b_idx = min( sm_b, end_band );
     277      805681 :         for ( i = 0; i < num_ch; i++ )
     278             :         {
     279     3812094 :             for ( j = 0; j < num_ch; j++ )
     280             :             {
     281     3156250 :                 if ( i == j )
     282             :                 {
     283      655844 :                     factor = fac;
     284             :                 }
     285             :                 else
     286             :                 {
     287     2500406 :                     factor = 0.0f;
     288             :                 }
     289             : 
     290     9468750 :                 for ( k = start_band; k < non_sm_b_idx; k++ )
     291             :                 {
     292     6312500 :                     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      805681 :         for ( i = 0; i < num_ch; i++ )
     297             :         {
     298     7049444 :             for ( k = non_sm_b_idx; k < end_band; k++ )
     299             :             {
     300     6393600 :                 pCov_buf[i][i][k] += ( hCovState->pSmoothing_factor[k] * fac );
     301             :             }
     302             :         }
     303             :     }
     304     2507589 :     else if ( prev_idx == 0 )
     305             :     {
     306    12865810 :         for ( i = 0; i < num_ch; i++ )
     307             :         {
     308    58773216 :             for ( j = 0; j < num_ch; j++ )
     309             :             {
     310    48414995 :                 if ( i == j )
     311             :                 {
     312    10358221 :                     factor = fac;
     313             :                 }
     314             :                 else
     315             :                 {
     316    38056774 :                     factor = 0.0f;
     317             :                 }
     318             : 
     319   618553479 :                 for ( k = start_band; k < end_band; k++ )
     320             :                 {
     321   570138484 :                     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     2808287 :     return;
     328             : }
     329             : 
     330             : 
     331             : /*-----------------------------------------------------------------------------------------*
     332             :  * Function ivas_cov_smooth_process()
     333             :  *
     334             :  * Covariance smoothing process
     335             :  *-----------------------------------------------------------------------------------------*/
     336             : 
     337     2808287 : 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     2808287 :     int16_t num_bands = end_band - start_band;
     348             : 
     349     2808287 :     ivas_compute_smooth_cov( hCovState, pFb, cov_real, hCovState->pPrior_cov_real, 1e-20f, start_band, end_band, num_ch, transient_det );
     350             : 
     351    14439706 :     for ( i = 0; i < num_ch; i++ )
     352             :     {
     353    66160312 :         for ( j = 0; j < num_ch; j++ )
     354             :         {
     355    54528893 :             mvr2r( &cov_real[i][j][start_band], &hCovState->pPrior_cov_real[i][j][start_band], num_bands );
     356             :         }
     357             :     }
     358             : 
     359     2808287 :     hCovState->prior_bank_idx = 0;
     360             : 
     361     2808287 :     return;
     362             : }

Generated by: LCOV version 1.14