LCOV - code coverage report
Current view: top level - lib_com - hq_bit_allocation.c (source / functions) Hit Total Coverage
Test: Coverage on main -- short test vectors @ 6c9ddc4024a9c0e1ecb8f643f114a84a0e26ec6b Lines: 65 66 98.5 %
Date: 2025-05-23 08:37:30 Functions: 1 1 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 "cnst.h"
      43             : #include "prot.h"
      44             : #include "rom_com.h"
      45             : #include "wmc_auto.h"
      46             : 
      47             : /*--------------------------------------------------------------------------*
      48             :  * hq_bit_allocation()
      49             :  *
      50             :  * Assign bits for HQ fine structure coding with PVQ
      51             :  *--------------------------------------------------------------------------*/
      52             : 
      53       33128 : void hq_bit_allocation(
      54             :     const int32_t core_brate,   /* i  : Core bitrate                                   */
      55             :     const int16_t length,       /* i  : Frame length                                   */
      56             :     const int16_t hqswb_clas,   /* i  : HQ class                                       */
      57             :     int16_t *num_bits,          /* i/o: Remaining bit budget                           */
      58             :     const int16_t *normqlg2,    /* i  : Quantized norms                                */
      59             :     const int16_t nb_sfm,       /* i  : Number sub bands to be encoded                 */
      60             :     const int16_t *sfmsize,     /* i  : Sub band bandwidths                            */
      61             :     float *noise_level,         /* o  : HVQ noise level                                */
      62             :     int16_t *R,                 /* o  : Bit allocation per sub band                    */
      63             :     int16_t *Rsubband,          /* o  : Fractional bit allocation (Q3)                 */
      64             :     int16_t *sum,               /* o  : Sum of allocated shape bits                    */
      65             :     int16_t *core_sfm,          /* o  : Last coded band in core                        */
      66             :     const int16_t num_env_bands /* i  : Number sub bands to be encoded for HQ_SWB_BWE  */
      67             : )
      68             : {
      69       33128 :     int16_t sfm_limit = nb_sfm;
      70             :     int16_t i;
      71             :     int16_t idx[NB_SFM];
      72             :     int16_t wnorm[NB_SFM];
      73             :     int16_t avrg_wnorm;
      74             :     int16_t E_low;
      75             :     int16_t E_hb_mean;
      76             :     int16_t E_max;
      77             :     int16_t i_max;
      78             : 
      79             : 
      80       33128 :     set_s( R, 0, NB_SFM );
      81     1276930 :     for ( i = 0; i < nb_sfm; i++ )
      82             :     {
      83     1243802 :         idx[i] = i;
      84             :     }
      85       33128 :     if ( hqswb_clas != HQ_TRANSIENT && hqswb_clas != HQ_HVQ && !( length == L_FRAME16k && core_brate <= HQ_32k ) )
      86             :     {
      87             :         /* 'nf_idx' 2-bits index written later */
      88       23870 :         *num_bits -= 2;
      89             :     }
      90             : 
      91       33128 :     if ( hqswb_clas == HQ_GEN_SWB || hqswb_clas == HQ_GEN_FB )
      92             :     {
      93       12072 :         if ( core_brate >= HQ_32k )
      94             :         {
      95           0 :             *num_bits -= HQ_GENERIC_SWB_NBITS2;
      96             :         }
      97             :         else
      98             :         {
      99       12072 :             *num_bits -= HQ_GENERIC_SWB_NBITS;
     100             :         }
     101             : 
     102       12072 :         if ( length == L_SPEC48k )
     103             :         {
     104        9074 :             *num_bits -= HQ_GENERIC_FB_NBITS;
     105             :         }
     106             :     }
     107             : 
     108       33128 :     if ( ( length == L_SPEC48k ) && ( hqswb_clas != HQ_HARMONIC ) && ( hqswb_clas != HQ_HVQ ) )
     109             :     {
     110       16880 :         map_quant_weight( normqlg2, wnorm, hqswb_clas == HQ_TRANSIENT );
     111             :     }
     112             :     else
     113             :     {
     114       16248 :         mvs2s( normqlg2, wnorm, nb_sfm );
     115             :     }
     116             : 
     117             : 
     118       33128 :     if ( hqswb_clas == HQ_HARMONIC )
     119             :     {
     120             :         /* classification and limit bandwidth for bit allocation */
     121        3158 :         sfm_limit -= 2;
     122        3158 :         limit_band_noise_level_calc( wnorm, &sfm_limit, core_brate, noise_level );
     123             : 
     124             :         /* Detect important band in high frequency region */
     125        3158 :         E_low = sum_s( wnorm, SFM_G1 );
     126        3158 :         i_max = 0;
     127        3158 :         E_max = MIN16B;
     128        3158 :         E_hb_mean = 0;
     129       55938 :         for ( i = SFM_G1; i < nb_sfm; i++ )
     130             :         {
     131       52780 :             E_hb_mean += wnorm[i];
     132       52780 :             if ( wnorm[i] > E_max )
     133             :             {
     134        7098 :                 E_max = wnorm[i];
     135        7098 :                 i_max = i;
     136             :             }
     137             :         }
     138        3158 :         E_hb_mean = E_hb_mean >> 4; /* Truncated division by SFM_G1 */
     139        3158 :         if ( E_max * 15 < E_low || E_max * 0.67 < E_hb_mean || i_max < sfm_limit )
     140             :         {
     141        3138 :             i_max = 0;
     142             :         }
     143             : 
     144        3158 :         set_s( wnorm + sfm_limit, -20, nb_sfm - sfm_limit );
     145        3158 :         if ( i_max > 0 )
     146             :         {
     147          20 :             wnorm[i_max] = E_max;
     148             :         }
     149             :     }
     150             : 
     151       33128 :     if ( hqswb_clas == HQ_HVQ )
     152             :     {
     153        5194 :         *sum = 0;
     154             :     }
     155       27934 :     else if ( hqswb_clas == HQ_GEN_SWB || ( hqswb_clas == HQ_TRANSIENT && length == L_FRAME32k && core_brate <= HQ_32k ) )
     156             :     {
     157        3369 :         *sum = BitAllocF( wnorm, core_brate, *num_bits, nb_sfm, R, Rsubband, hqswb_clas, num_env_bands );
     158             :     }
     159       24565 :     else if ( length == L_FRAME16k && core_brate <= HQ_32k )
     160             :     {
     161        1929 :         if ( hqswb_clas != HQ_TRANSIENT )
     162             :         {
     163        1885 :             avrg_wnorm = wnorm[10];
     164       15080 :             for ( i = 11; i < 18; i++ )
     165             :             {
     166       13195 :                 avrg_wnorm += wnorm[i];
     167             :             }
     168             : 
     169        1885 :             avrg_wnorm /= 8;
     170        9425 :             for ( i = 0; i < 4; i++ )
     171             :             {
     172        7540 :                 if ( wnorm[i] < avrg_wnorm )
     173             :                 {
     174         704 :                     wnorm[i] = avrg_wnorm;
     175             :                 }
     176             :             }
     177             : 
     178             :             /* Estimate number of bits per band */
     179        1885 :             *sum = BitAllocWB( wnorm, *num_bits, nb_sfm, R, Rsubband );
     180             :         }
     181             :         else
     182             :         {
     183          44 :             reordvct( wnorm, nb_sfm, idx );
     184          44 :             bitalloc( wnorm, idx, *num_bits, nb_sfm, QBIT_MAX2, R, sfmsize, hqswb_clas );
     185          44 :             bitallocsum( R, nb_sfm, sum, Rsubband, *num_bits, length, sfmsize );
     186             :         }
     187             :     }
     188             :     else
     189             :     {
     190       22636 :         reordvct( wnorm, nb_sfm, idx );
     191             : 
     192             :         /* enlarge the wnorm value so that more bits can be allocated to (sfm_limit/2 ~ sfm_limit) range */
     193       22636 :         if ( hqswb_clas == HQ_HARMONIC )
     194             :         {
     195       44351 :             for ( i = sfm_limit / 2; i < sfm_limit; i++ )
     196             :             {
     197       41193 :                 wnorm[i] = wnorm[sfm_limit / 2 - 1];
     198             :             }
     199             :         }
     200             : 
     201       22636 :         bitalloc( wnorm, idx, *num_bits, nb_sfm, QBIT_MAX2, R, sfmsize, hqswb_clas );
     202       22636 :         bitallocsum( R, nb_sfm, sum, Rsubband, *num_bits, length, sfmsize );
     203             :     }
     204             : 
     205             :     /* Find last coded core band */
     206       33128 :     *core_sfm = nb_sfm - 1;
     207       33128 :     if ( hqswb_clas == HQ_NORMAL || hqswb_clas == HQ_HARMONIC )
     208             :     {
     209       13683 :         *core_sfm = find_last_band( R, nb_sfm );
     210             :     }
     211       19445 :     else if ( hqswb_clas == HQ_GEN_SWB || hqswb_clas == HQ_GEN_FB )
     212             :     {
     213       12072 :         *core_sfm = find_last_band( R, nb_sfm );
     214       12072 :         if ( *core_sfm < num_env_bands )
     215             :         {
     216       12060 :             *core_sfm = num_env_bands - 1;
     217             :         }
     218             :     }
     219             : 
     220       33128 :     *num_bits -= *sum;
     221             : 
     222       33128 :     return;
     223             : }

Generated by: LCOV version 1.14