LCOV - code coverage report
Current view: top level - lib_dec - swb_bwe_dec_lr.c (source / functions) Hit Total Coverage
Test: Coverage on main -- short test vectors @ 6c9ddc4024a9c0e1ecb8f643f114a84a0e26ec6b Lines: 53 70 75.7 %
Date: 2025-05-23 08:37:30 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 "cnst.h"
      44             : #include "prot.h"
      45             : #include "rom_com.h"
      46             : #include "stat_com.h"
      47             : #include "wmc_auto.h"
      48             : 
      49             : 
      50             : /*-------------------------------------------------------------------*
      51             :  * DecodeSWBGenericParameters()
      52             :  *
      53             :  * Decoding of generic subband coding parameters
      54             :  *-------------------------------------------------------------------*/
      55             : 
      56          93 : static void DecodeSWBGenericParameters(
      57             :     Decoder_State *st,           /* i/o: decoder state structure                     */
      58             :     int16_t *lagIndices,         /* o  : lowband index for each subband              */
      59             :     const int16_t nBands_search, /* i  : number of subbnads for SSearch              */
      60             :     const int16_t BANDS,         /* i  : total number of subbands per frame          */
      61             :     const int16_t *p2a_flags,    /* i  : HF tonal flag                               */
      62             :     const int16_t hq_swb_clas    /* i  : mode of operation HQ_NORMAL or HQ_HARMONIC  */
      63             : )
      64             : {
      65             :     int16_t sb;
      66             : 
      67             :     /* lag index for each subband (except last two) */
      68         465 :     for ( sb = 0; sb < nBands_search; sb++ )
      69             :     {
      70         372 :         if ( hq_swb_clas == HQ_HARMONIC )
      71             :         {
      72           0 :             lagIndices[sb] = get_next_indice( st, bits_lagIndices_mode0_Har[sb] );
      73             :         }
      74             :         else
      75             :         {
      76         372 :             if ( p2a_flags[BANDS - NB_SWB_SUBBANDS + sb] == 0 )
      77             :             {
      78         366 :                 lagIndices[sb] = get_next_indice( st, bits_lagIndices_modeNormal[sb] );
      79             :             }
      80             :             else
      81             :             {
      82           6 :                 lagIndices[sb] = 0;
      83             :             }
      84             :         }
      85             :     }
      86             : 
      87          93 :     return;
      88             : }
      89             : 
      90             : 
      91             : /*-------------------------------------------------------------------*
      92             :  * DecodeSWBSubbands()
      93             :  *
      94             :  * Main routine for generic SWB coding
      95             :  *
      96             :  * High-frequency subbands are replicated based on the lowband signal using a lowband index denoting
      97             :  * the selected lowband subband as well as linear and logarithmic domain gains
      98             :  *-------------------------------------------------------------------*/
      99             : 
     100          93 : static void DecodeSWBSubbands(
     101             :     Decoder_State *st,      /* i/o: decoder state structure   */
     102             :     HQ_DEC_HANDLE hHQ_core, /* i/o: HQ decoder handle                */
     103             :     float *spectra,         /* i/o: MDCT domain spectrum             */
     104             :     const int16_t fLenLow,  /* i  : lowband length                   */
     105             :     const int16_t fLenHigh, /* i  : highband length                  */
     106             :     const int16_t nBands,   /* i  : number of subbands               */
     107             :     const int16_t *sbWidth, /* i  : subband lengths                  */
     108             :     int16_t *lagIndices,
     109             :     float *lagGains,          /* i  : first gain for each subband      */
     110             :     int16_t BANDS,            /* i  : number subbands per frame        */
     111             :     int16_t *band_start,      /* i  : band start of each SB            */
     112             :     int16_t *band_end,        /* i  : band end of each SB              */
     113             :     float *band_energy,       /* i  : band energy of each SB           */
     114             :     int16_t *p2a_flags,       /* i  : HF tonal indicator               */
     115             :     const int16_t hqswb_clas, /* i  : class information                */
     116             :     const int16_t har_bands,  /* i  : number of LF harmonic bands      */
     117             :     const int16_t *subband_search_offset,
     118             :     int16_t *prev_frm_hfe2,
     119             :     int16_t *prev_stab_hfe2,
     120             :     int16_t band_width[],           /* i  :  subband band widths                */
     121             :     const int16_t *subband_offsets, /* i  :  subband offsets for sparse filling */
     122             :     const float spectra_ni[],       /* i  :  core coder with sparseness filled  */
     123             :     int16_t *ni_seed                /* i/o: random seed for search buffer NI    */
     124             : )
     125             : {
     126             :     int16_t i;
     127             :     int16_t k;
     128             :     float sspectra[L_FRAME32k];
     129             :     float sspectra_ni[L_FRAME32k], sspectra_diff[L_FRAME32k], th_g[NB_SWB_SUBBANDS];
     130          93 :     float ss_min = 1.0f, g, be_tonal[SWB_HAR_RAN1], xSynth_har[L_FRAME32k];
     131             :     GainItem pk_sf[(NB_SWB_SUBBANDS) *8];
     132             :     int16_t lagIndices_real[NB_SWB_SUBBANDS];
     133             :     int16_t pul_res[NB_SWB_SUBBANDS], imin;
     134          93 :     int16_t har_freq_est1 = 0;
     135          93 :     int16_t har_freq_est2 = 0;
     136          93 :     int16_t flag_dis = 1;
     137          93 :     int16_t pos_max_hfe2 = 0;
     138             : 
     139          93 :     set_s( pul_res, 0, NB_SWB_SUBBANDS );
     140          93 :     set_f( xSynth_har, 0.0f, fLenHigh );
     141             : 
     142             : 
     143          93 :     if ( hqswb_clas == HQ_HARMONIC )
     144             :     {
     145             :         /* Harmonic Structure analysis */
     146           0 :         pos_max_hfe2 = har_est( spectra, fLenLow, &har_freq_est1, &har_freq_est2, &flag_dis, prev_frm_hfe2, subband_search_offset, sbWidth, prev_stab_hfe2 );
     147             :         /* Spectrum normalization for the corecoder */
     148           0 :         noise_extr_corcod( spectra, spectra_ni, sspectra, sspectra_diff, sspectra_ni, fLenLow, hHQ_core->prev_hqswb_clas, &hHQ_core->prev_ni_ratio );
     149             :         /* Harmonic Structure analysis */
     150             : 
     151           0 :         if ( flag_dis == 0 )
     152             :         {
     153           0 :             if ( har_freq_est2 != SWB_HAR_RAN1 || har_freq_est2 != *prev_frm_hfe2 )
     154             :             {
     155           0 :                 har_freq_est2 += lagIndices[0];
     156             :             }
     157             :         }
     158             :         /*Generate HF noise*/
     159           0 :         genhf_noise( sspectra_diff, xSynth_har, sspectra, BANDS, har_bands, har_freq_est2, pos_max_hfe2, pul_res, pk_sf, fLenLow, fLenHigh, sbWidth, lagIndices, subband_offsets, subband_search_offset );
     160             : 
     161           0 :         imin = get_next_indice( st, 2 );
     162           0 :         g = (float) pow( 10.0f, gain_table_SWB_BWE[imin] );
     163             :         /* tonal energy estimation */
     164           0 :         ton_ene_est( xSynth_har, be_tonal, band_energy, band_start, band_end, band_width, fLenLow, fLenHigh, BANDS, har_bands, g, pk_sf, pul_res );
     165             : 
     166             :         /* HF Spectrum Generation */
     167           0 :         Gettonl_scalfact( xSynth_har, spectra_ni, fLenLow, fLenHigh, har_bands, BANDS, band_energy, band_start, band_end, p2a_flags, be_tonal, pk_sf, pul_res );
     168           0 :         if ( flag_dis == 0 )
     169             :         {
     170           0 :             *prev_frm_hfe2 = 0;
     171             :         }
     172             :         else
     173             :         {
     174           0 :             *prev_frm_hfe2 = har_freq_est2;
     175             :         }
     176             : 
     177           0 :         for ( k = har_bands; k < BANDS; k++ )
     178             :         {
     179           0 :             for ( i = band_start[k]; i <= band_end[k]; i++ )
     180             :             {
     181           0 :                 spectra[i] = xSynth_har[i - fLenLow];
     182             :             }
     183             :         }
     184             :     }
     185          93 :     else if ( hqswb_clas == HQ_NORMAL )
     186             :     {
     187             :         /* Spectrum normalization for the corecoder */
     188          93 :         ss_min = spectrumsmooth_noiseton( spectra, spectra_ni, sspectra, sspectra_diff, sspectra_ni, fLenLow, ni_seed );
     189             : 
     190          93 :         convert_lagIndices_pls2smp( (int16_t *) lagIndices, nBands, lagIndices_real, sspectra, sbWidth, fLenLow );
     191             : 
     192         465 :         for ( k = 0; k < nBands; k++ )
     193             :         {
     194         372 :             if ( p2a_flags[BANDS - NB_SWB_SUBBANDS + k] == 1 )
     195             :             {
     196           6 :                 lagIndices_real[k] = 0;
     197             :             }
     198             :         }
     199             :         /*get levels for missing bands*/
     200          93 :         GetlagGains( sspectra_ni, &band_energy[BANDS - NB_SWB_SUBBANDS], nBands, sbWidth, lagIndices_real, fLenLow, lagGains );
     201         465 :         for ( k = 0; k < NB_SWB_SUBBANDS; k++ )
     202             :         {
     203         372 :             lagGains[k] *= 0.9f;
     204             :         }
     205         465 :         for ( k = 0; k < NB_SWB_SUBBANDS; k++ )
     206             :         {
     207         372 :             th_g[k] = 0.0f;
     208         372 :             if ( p2a_flags[BANDS - NB_SWB_SUBBANDS + k] == 0 )
     209             :             {
     210         366 :                 th_g[k] = lagGains[k] * ss_min;
     211             :             }
     212             :         }
     213             :         /* Construct spectrum */
     214          93 :         GetSynthesizedSpecThinOut( sspectra_ni, xSynth_har, nBands, sbWidth, lagIndices_real, lagGains, fLenLow );
     215             : 
     216             :         /*Level adjustment for the missing bands*/
     217          93 :         noiseinj_hf( xSynth_har, th_g, band_energy, hHQ_core->prev_En_sb, p2a_flags, BANDS, band_start, band_end, fLenLow );
     218             :         /* xSynth is the reconstructed high-band */
     219         465 :         for ( k = BANDS - NB_SWB_SUBBANDS; k < BANDS; k++ )
     220             :         {
     221         372 :             if ( p2a_flags[k] == 0 )
     222             :             {
     223       28815 :                 for ( i = band_start[k]; i <= band_end[k]; i++ )
     224             :                 {
     225       28449 :                     spectra[i] = xSynth_har[i - fLenLow];
     226             :                 }
     227             :             }
     228             :             else
     229             :             {
     230         573 :                 for ( i = band_start[k]; i <= band_end[k]; i++ )
     231             :                 {
     232         567 :                     spectra[i] = spectra_ni[i];
     233             :                 }
     234             :             }
     235             :         }
     236             :     }
     237             : 
     238          93 :     return;
     239             : }
     240             : 
     241             : 
     242             : /*-------------------------------------------------------------------*
     243             :  * swb_bwe_dec_lr()
     244             :  *
     245             :  * Main decoding routine of SWB BWE for the LR MDCT core
     246             :  *-------------------------------------------------------------------*/
     247             : 
     248          93 : void swb_bwe_dec_lr(
     249             :     Decoder_State *st,         /* i/o: decoder state structure   */
     250             :     const float m_core[],      /* i  : lowband synthesis                       */
     251             :     float m[],                 /* o  : highband synthesis with lowband zeroed  */
     252             :     const int32_t total_brate, /* i  : total bitrate for selecting subband pattern */
     253             :     int16_t BANDS,             /* i  : Number subbands/Frame */
     254             :     int16_t *band_start,       /* i  : Band Start of each SB */
     255             :     int16_t *band_end,         /* i  : Band end of each SB */
     256             :     float *band_energy,        /* i  : BAnd energy of each SB */
     257             :     int16_t *p2a_flags,        /* i  : HF tonal Indicator */
     258             :     const int16_t hqswb_clas,  /* i  : class information */
     259             :     int16_t lowlength,         /* i  : Lowband Length */
     260             :     int16_t highlength,        /* i  : Highband Length */
     261             :     const int16_t har_bands,   /* i  : Number of LF harmonic bands */
     262             :     int16_t *prev_frm_hfe2,
     263             :     int16_t *prev_stab_hfe2,
     264             :     int16_t band_width[], /* i  : subband bandwidth                           */
     265             :     const float y2_ni[],  /* i/o: Sparse filled corecoder                     */
     266             :     int16_t *ni_seed      /* i/o: random seed */
     267             : )
     268             : {
     269             :     int16_t k;
     270             :     int16_t nBands;
     271             :     int16_t nBands_search;
     272             :     int16_t wBands[NB_SWB_SUBBANDS];
     273             :     int16_t lagIndices[NB_SWB_SUBBANDS];
     274             :     float lagGains[NB_SWB_SUBBANDS];
     275             :     int16_t swb_lowband, swb_highband;
     276             :     const int16_t *subband_search_offset;
     277             : 
     278             :     const int16_t *subband_offsets;
     279             : 
     280          93 :     subband_search_offset = subband_search_offsets_13p2kbps_Har;
     281          93 :     subband_offsets = subband_offsets_sub5_13p2kbps_Har;
     282          93 :     hf_parinitiz( total_brate, hqswb_clas, lowlength, highlength, wBands, &subband_search_offset, &subband_offsets, &nBands, &nBands_search, &swb_lowband, &swb_highband );
     283             : 
     284             :     /* Decoding of the SWB parameters */
     285          93 :     DecodeSWBGenericParameters( st, lagIndices, nBands_search, BANDS, p2a_flags, hqswb_clas );
     286             : 
     287             :     /* Copy WB synthesis for SWB decoding */
     288          93 :     mvr2r( m_core, m, swb_lowband + swb_highband );
     289             : 
     290             :     /* Generic subband processing */
     291          93 :     DecodeSWBSubbands( st, st->hHQ_core, m, swb_lowband, swb_highband, nBands, wBands, lagIndices, lagGains, BANDS, band_start, band_end, band_energy, p2a_flags, hqswb_clas, har_bands, subband_search_offset, prev_frm_hfe2, prev_stab_hfe2, band_width, subband_offsets, y2_ni, ni_seed );
     292             : 
     293             :     /* Smoothen highest frequencies */
     294          93 :     m[swb_lowband + swb_highband - 1] *= 0.0625f;
     295          93 :     m[swb_lowband + swb_highband - 2] *= 0.125f;
     296          93 :     m[swb_lowband + swb_highband - 3] *= 0.25f;
     297          93 :     m[swb_lowband + swb_highband - 4] *= 0.5f;
     298             : 
     299             :     /* Set frequencies below 6.4 kHz to zero */
     300          93 :     if ( hqswb_clas == HQ_NORMAL )
     301             :     {
     302       23901 :         for ( k = 0; k < swb_lowband; k++ )
     303             :         {
     304       23808 :             m[k] = 0.0f;
     305             :         }
     306             :     }
     307             : 
     308          93 :     return;
     309             : }

Generated by: LCOV version 1.14