LCOV - code coverage report
Current view: top level - lib_dec - dec_acelp.c (source / functions) Hit Total Coverage
Test: Coverage on main @ fec202a8f89be4a2f278a9fc377bfb58b58fab11 Lines: 127 138 92.0 %
Date: 2025-09-14 08:49:17 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 <assert.h>
      38             : #include <stdint.h>
      39             : #include "options.h"
      40             : #include "prot.h"
      41             : #include "rom_com.h"
      42             : #include "wmc_auto.h"
      43             : 
      44             : /*---------------------------------------------------------------------*
      45             :  * Local function prototypes
      46             :  *---------------------------------------------------------------------*/
      47             : 
      48             : static void D_ACELP_decode_arithtrack( float v[], uint32_t s, int16_t p, const int16_t trackstep, const int16_t tracklen );
      49             : 
      50             : /*---------------------------------------------------------------------*
      51             :  * Function D_ACELP_indexing()
      52             :  *
      53             :  *---------------------------------------------------------------------*/
      54             : 
      55    11047996 : void D_ACELP_indexing(
      56             :     float code[],
      57             :     PulseConfig config,
      58             :     const int16_t num_tracks,
      59             :     int16_t index[],
      60             :     int16_t *BER_detect )
      61             : {
      62             :     int16_t track, pulses, k, pulsestrack[NB_TRACK_FCB_4T];
      63             :     uint32_t s;
      64             :     uint32_t index_n[NB_TRACK_FCB_4T];
      65             :     uint16_t trackpos, idxs[MAX_IDX_LEN];
      66             :     int16_t restpulses, wordcnt;
      67             : 
      68    11047996 :     assert( num_tracks == NB_TRACK_FCB_4T );
      69             : 
      70    11047996 :     wordcnt = ( config.bits + 15 ) >> 4; /* ceil(bits/16) */
      71             : 
      72             :     /* check if some tracks have more pulses */
      73    11047996 :     restpulses = config.nb_pulse & ( num_tracks - 1 );
      74             : 
      75             :     /* cast to short */
      76    47939335 :     for ( k = 0; k < wordcnt; k++ )
      77             :     {
      78    36891339 :         idxs[k] = (uint16_t) index[k];
      79             :     }
      80             : 
      81    11047996 :     if ( restpulses )
      82             :     {
      83             :         /* check if we need to code track positions */
      84     7904362 :         switch ( config.codetrackpos )
      85             :         {
      86     1473381 :             case TRACKPOS_FREE_THREE:
      87             :                 /* Find track with less pulses */
      88     1473381 :                 trackpos = idxs[0] & 3;
      89     1473381 :                 longshiftright( idxs, 2, idxs, wordcnt, wordcnt );
      90             : 
      91             :                 /* set number of pulses per track */
      92     1473381 :                 set_s( pulsestrack, ( config.nb_pulse >> 2 ) + 1, 4 );
      93     1473381 :                 pulsestrack[trackpos]--; /* this one has less pulses */
      94     1473381 :                 break;
      95      490112 :             case TRACKPOS_FREE_ONE:
      96             :                 /* Find track with more pulses */
      97      490112 :                 trackpos = idxs[0] & 3;
      98      490112 :                 longshiftright( idxs, 2, idxs, wordcnt, wordcnt );
      99             : 
     100             :                 /* set number of pulses per track */
     101      490112 :                 set_s( pulsestrack, ( config.nb_pulse >> 2 ), 4 );
     102      490112 :                 pulsestrack[trackpos]++; /* this one has more pulses */
     103      490112 :                 break;
     104           0 :             case TRACKPOS_FIXED_EVEN:
     105             :                 /* Pulses on even tracks */
     106           0 :                 pulsestrack[0] = ( config.nb_pulse + 1 ) >> 1;
     107           0 :                 pulsestrack[1] = 0;
     108           0 :                 pulsestrack[2] = config.nb_pulse >> 1;
     109           0 :                 pulsestrack[3] = 0;
     110           0 :                 break;
     111     4807758 :             case TRACKPOS_FIXED_FIRST:
     112             :                 /* set number of pulses per track */
     113     4807758 :                 set_s( pulsestrack, config.nb_pulse / num_tracks, 4 );
     114    14383342 :                 for ( k = 0; k < restpulses; k++ )
     115             :                 {
     116     9575584 :                     pulsestrack[k]++;
     117             :                 }
     118     4807758 :                 break;
     119     1133111 :             case TRACKPOS_FIXED_TWO:
     120             :                 /* 1100, 0110, 0011, 1001 */
     121             :                 /* Find track with less pulses */
     122     1133111 :                 trackpos = idxs[0] & 3;
     123     1133111 :                 longshiftright( idxs, 2, idxs, wordcnt, wordcnt );
     124             : 
     125             :                 /* set number of pulses per track */
     126     1133111 :                 set_s( pulsestrack, ( config.nb_pulse >> 2 ), 4 );
     127     1133111 :                 pulsestrack[trackpos]++;
     128     1133111 :                 trackpos++;
     129     1133111 :                 trackpos &= 3;
     130     1133111 :                 pulsestrack[trackpos]++;
     131     1133111 :                 break;
     132           0 :             default:
     133           0 :                 assert( 0 );
     134             :                 break;
     135             :         }
     136             :     }
     137             :     else
     138             :     {
     139             :         /* set number of pulses per track */
     140     3143634 :         set_s( pulsestrack, ( config.nb_pulse / num_tracks ), num_tracks );
     141             :     }
     142             : 
     143    11047996 :     if ( config.bits == 43 )
     144             :     {
     145      469817 :         D_ACELP_decode_43bit( idxs, code, pulsestrack );
     146             :     }
     147             :     else
     148             :     {
     149    10578179 :         fcb_pulse_track_joint_decode( idxs, wordcnt, index_n, pulsestrack, num_tracks );
     150             : 
     151    42312716 :         for ( track = num_tracks - 1; track >= 1; track-- )
     152             :         {
     153    31734537 :             pulses = pulsestrack[track];
     154             : 
     155    31734537 :             if ( pulses )
     156             :             {
     157    31221321 :                 s = index_n[track];
     158             : 
     159             :                 /* decode state to actual pulse positions on track */
     160             :                 /*D_ACELP_decode_arithtrack_old(code+track, s, pulses, 4);                    */
     161    31221321 :                 D_ACELP_decode_arithtrack( code + track, s, pulses, num_tracks, 16 );
     162             :             }
     163             :             else
     164             :             {
     165             :                 /* track is empty */
     166     8724672 :                 for ( k = track; k < 16 * num_tracks; k += num_tracks )
     167             :                 {
     168     8211456 :                     code[k] = 0.0f;
     169             :                 }
     170             :             }
     171             :         }
     172    10578179 :         s = index_n[0];
     173    10578179 :         pulses = pulsestrack[0];
     174             :         /* safety check in case of bit errors */
     175    10578179 :         if ( s >= pulsestostates[16][pulses - 1] )
     176             :         {
     177           0 :             set_f( code, 0.0f, L_SUBFR );
     178           0 :             *BER_detect = 1;
     179           0 :             return;
     180             :         }
     181    10578179 :         if ( pulses )
     182             :         {
     183    10422887 :             D_ACELP_decode_arithtrack( code, s, pulses, num_tracks, 16 );
     184             :         }
     185             :         else
     186             :         {
     187             :             /* track is empty */
     188     2639964 :             for ( k = 0; k < 16 * num_tracks; k += num_tracks )
     189             :             {
     190     2484672 :                 code[k] = 0.0f;
     191             :             }
     192             :         }
     193             :     }
     194             : 
     195    11047996 :     return;
     196             : }
     197             : 
     198             : 
     199    41644208 : static void D_ACELP_decode_arithtrack(
     200             :     float v[],
     201             :     uint32_t s,
     202             :     int16_t p,
     203             :     const int16_t trackstep,
     204             :     const int16_t tracklen )
     205             : {
     206             :     int16_t k;
     207             : 
     208   707951536 :     for ( k = (tracklen) -1; k >= 0; k-- )
     209             :     {
     210   666307328 :         v[k * trackstep] = 0.0f; /* default: there is no pulse here */
     211   782187427 :         while ( ( p ) && ( s >= pulsestostates[k][p - 1] ) )
     212             :         {
     213   115880099 :             s -= pulsestostates[k][p - 1];
     214   115880099 :             if ( v[k * trackstep] )
     215             :             {
     216             :                 /* there is a pulse here already = sign is known */
     217     7827865 :                 if ( v[k * trackstep] > 0.0f )
     218             :                 {
     219     3768254 :                     v[k * trackstep]++; /* place one more pulse here */
     220             :                 }
     221             :                 else
     222             :                 {
     223     4059611 :                     v[k * trackstep]--; /* place one more pulse here */
     224             :                 }
     225             :             }
     226             :             else
     227             :             {
     228             :                 /* this is the first pulse here -> determine sign */
     229   108052234 :                 if ( s & 1 )
     230             :                 {
     231    54036129 :                     v[k * trackstep] = -1.0f; /* place a negative pulse here */
     232             :                 }
     233             :                 else
     234             :                 {
     235    54016105 :                     v[k * trackstep] = +1.0f; /* place a negative pulse here */
     236             :                 }
     237   108052234 :                 s >>= 1;
     238             :             }
     239   115880099 :             p--; /* one pulse placed, so one less left */
     240             :         }
     241             :     }
     242             : 
     243    41644208 :     return;
     244             : }
     245             : 
     246             : 
     247    10578179 : void fcb_pulse_track_joint_decode(
     248             :     uint16_t *idxs,
     249             :     const int16_t wordcnt,
     250             :     uint32_t *index_n,
     251             :     const int16_t *pulse_num,
     252             :     const int16_t track_num )
     253             : {
     254    10578179 :     int16_t hi_to_low[10] = { 0, 0, 0, 3, 9, 5, 3, 1, 8, 8 };
     255             : 
     256             :     uint64_t index;
     257             :     int16_t indx_flag, indx_flag_1;
     258             :     int16_t track, track_num1, pulse_num0, pulse_num1;
     259             :     int64_t indx_tmp, div_tmp; /* must be long */
     260             :     int16_t indx_flag_2;
     261             : 
     262    10578179 :     indx_flag = 0;
     263    10578179 :     indx_flag_1 = 0;
     264    10578179 :     indx_flag_2 = 0;
     265    52890895 :     for ( track = 0; track < track_num; track++ )
     266             :     {
     267    42312716 :         indx_flag += ( pulse_num[track] >> 2 );
     268    42312716 :         indx_flag_1 += ( pulse_num[track] >> 1 );
     269    42312716 :         indx_flag_2 += ( pulse_num[track] >> 3 );
     270             :     }
     271             : 
     272    10578179 :     if ( indx_flag >= track_num )
     273             :     {
     274     2156917 :         hi_to_low[4] = 9;
     275             :     }
     276             :     else
     277             :     {
     278     8421262 :         hi_to_low[4] = 1;
     279             :     }
     280             : 
     281    10578179 :     if ( indx_flag_2 >= 1 )
     282             :     {
     283        1134 :         hi_to_low[7] = 9;
     284             :     }
     285             :     else
     286             :     {
     287    10577045 :         hi_to_low[7] = 1;
     288             :     }
     289             : 
     290    10578179 :     if ( indx_flag_1 >= track_num )
     291             :     {
     292     7183333 :         if ( indx_flag >= track_num )
     293             :         {
     294     2156917 :             index = 0;
     295     2156917 :             if ( indx_flag_2 >= 1 )
     296             :             {
     297        1240 :                 for ( track = ( wordcnt - 1 ); track >= 6; track-- )
     298             :                 {
     299         106 :                     index = ( index << 16 ) + idxs[track];
     300             :                 }
     301        1134 :                 index_n[3] = ( ( (uint32_t) idxs[5] ) << 8 ) + ( ( idxs[4] >> 8 ) & 0xff );
     302        1134 :                 index_n[2] = ( ( ( (uint32_t) idxs[4] ) << 16 ) + idxs[3] ) & 0xffffffUL;
     303        1134 :                 index_n[1] = ( ( (uint32_t) idxs[2] ) << 8 ) + ( ( idxs[1] >> 8 ) & 0xff );
     304        1134 :                 index_n[0] = ( ( ( (uint32_t) idxs[1] ) << 16 ) + idxs[0] ) & 0xffffffUL;
     305             :             }
     306             :             else
     307             :             {
     308     4131489 :                 for ( track = ( wordcnt - 1 ); track >= track_num; track-- )
     309             :                 {
     310     1975706 :                     index = ( index << 16 ) + idxs[track];
     311             :                 }
     312    10778915 :                 for ( track = 0; track < track_num; track++ )
     313             :                 {
     314     8623132 :                     index_n[track] = idxs[track];
     315             :                 }
     316             :             }
     317             :         }
     318             :         else
     319             :         {
     320     5026416 :             index = 0;
     321    12751582 :             for ( track = ( wordcnt - 1 ); track >= 2; track-- )
     322             :             {
     323     7725166 :                 index = ( index << 16 ) + idxs[track];
     324             :             }
     325             : 
     326     5026416 :             index_n[3] = idxs[1] & 0xff;
     327     5026416 :             index_n[2] = idxs[1] >> 8;
     328     5026416 :             index_n[1] = idxs[0] & 0xff;
     329     5026416 :             index_n[0] = idxs[0] >> 8;
     330             :         }
     331             : 
     332     7183333 :         track_num1 = track_num - 1;
     333     7183333 :         pulse_num1 = pulse_num[track_num1];
     334     7183333 :         index = ( index << hi_to_low[pulse_num1] ) + ( index_n[track_num1] >> low_len[pulse_num1] );
     335    28733332 :         for ( track = ( track_num - 1 ); track > 0; track-- )
     336             :         {
     337    21549999 :             track_num1 = track - 1;
     338    21549999 :             pulse_num0 = pulse_num[track_num1];
     339    21549999 :             pulse_num1 = pulse_num[track];
     340    21549999 :             index = ( index << hi_to_low[pulse_num0] ) + ( index_n[track_num1] >> low_len[pulse_num0] );
     341             : 
     342    21549999 :             div_tmp = index / indx_fact[pulse_num1];
     343    21549999 :             indx_tmp = index - div_tmp * indx_fact[pulse_num1];
     344    21549999 :             index_n[track] = (uint32_t) ( ( index_n[track] & low_mask[pulse_num1] ) + ( indx_tmp << low_len[pulse_num1] ) );
     345    21549999 :             index = div_tmp;
     346             :         }
     347     7183333 :         pulse_num1 = pulse_num[0];
     348     7183333 :         index_n[0] = (uint32_t) ( ( index_n[0] & low_mask[pulse_num1] ) + ( index << low_len[pulse_num1] ) );
     349             :     }
     350             :     else
     351             :     {
     352     3394846 :         index = 0;
     353    10492988 :         for ( track = ( wordcnt - 1 ); track >= 0; track-- )
     354             :         {
     355     7098142 :             index = ( index << 16 ) + idxs[track];
     356             :         }
     357    13579384 :         for ( track = 3; track > 0; track-- )
     358             :         {
     359    10184538 :             pulse_num1 = pulse_num[track];
     360    10184538 :             index_n[track] = index & index_mask_ACELP[pulse_num1];
     361    10184538 :             index = index >> index_len[pulse_num1];
     362             :         }
     363     3394846 :         index_n[0] = (uint32_t) index;
     364             :     }
     365             : 
     366    10578179 :     return;
     367             : }

Generated by: LCOV version 1.14