LCOV - code coverage report
Current view: top level - lib_dec - dec_acelp.c (source / functions) Hit Total Coverage
Test: Coverage on main -- merged total coverage @ 9b04ec3cb36f5e8dc438cf854fa3e349998fa1e9 Lines: 127 138 92.0 %
Date: 2025-10-31 05:45:46 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    19593817 : 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    19593817 :     assert( num_tracks == NB_TRACK_FCB_4T );
      69             : 
      70    19593817 :     wordcnt = ( config.bits + 15 ) >> 4; /* ceil(bits/16) */
      71             : 
      72             :     /* check if some tracks have more pulses */
      73    19593817 :     restpulses = config.nb_pulse & ( num_tracks - 1 );
      74             : 
      75             :     /* cast to short */
      76    83890985 :     for ( k = 0; k < wordcnt; k++ )
      77             :     {
      78    64297168 :         idxs[k] = (uint16_t) index[k];
      79             :     }
      80             : 
      81    19593817 :     if ( restpulses )
      82             :     {
      83             :         /* check if we need to code track positions */
      84    13731789 :         switch ( config.codetrackpos )
      85             :         {
      86     2368581 :             case TRACKPOS_FREE_THREE:
      87             :                 /* Find track with less pulses */
      88     2368581 :                 trackpos = idxs[0] & 3;
      89     2368581 :                 longshiftright( idxs, 2, idxs, wordcnt, wordcnt );
      90             : 
      91             :                 /* set number of pulses per track */
      92     2368581 :                 set_s( pulsestrack, ( config.nb_pulse >> 2 ) + 1, 4 );
      93     2368581 :                 pulsestrack[trackpos]--; /* this one has less pulses */
      94     2368581 :                 break;
      95      920559 :             case TRACKPOS_FREE_ONE:
      96             :                 /* Find track with more pulses */
      97      920559 :                 trackpos = idxs[0] & 3;
      98      920559 :                 longshiftright( idxs, 2, idxs, wordcnt, wordcnt );
      99             : 
     100             :                 /* set number of pulses per track */
     101      920559 :                 set_s( pulsestrack, ( config.nb_pulse >> 2 ), 4 );
     102      920559 :                 pulsestrack[trackpos]++; /* this one has more pulses */
     103      920559 :                 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     8442493 :             case TRACKPOS_FIXED_FIRST:
     112             :                 /* set number of pulses per track */
     113     8442493 :                 set_s( pulsestrack, config.nb_pulse / num_tracks, 4 );
     114    25021326 :                 for ( k = 0; k < restpulses; k++ )
     115             :                 {
     116    16578833 :                     pulsestrack[k]++;
     117             :                 }
     118     8442493 :                 break;
     119     2000156 :             case TRACKPOS_FIXED_TWO:
     120             :                 /* 1100, 0110, 0011, 1001 */
     121             :                 /* Find track with less pulses */
     122     2000156 :                 trackpos = idxs[0] & 3;
     123     2000156 :                 longshiftright( idxs, 2, idxs, wordcnt, wordcnt );
     124             : 
     125             :                 /* set number of pulses per track */
     126     2000156 :                 set_s( pulsestrack, ( config.nb_pulse >> 2 ), 4 );
     127     2000156 :                 pulsestrack[trackpos]++;
     128     2000156 :                 trackpos++;
     129     2000156 :                 trackpos &= 3;
     130     2000156 :                 pulsestrack[trackpos]++;
     131     2000156 :                 break;
     132           0 :             default:
     133           0 :                 assert( 0 );
     134             :                 break;
     135             :         }
     136             :     }
     137             :     else
     138             :     {
     139             :         /* set number of pulses per track */
     140     5862028 :         set_s( pulsestrack, ( config.nb_pulse / num_tracks ), num_tracks );
     141             :     }
     142             : 
     143    19593817 :     if ( config.bits == 43 )
     144             :     {
     145      890721 :         D_ACELP_decode_43bit( idxs, code, pulsestrack );
     146             :     }
     147             :     else
     148             :     {
     149    18703096 :         fcb_pulse_track_joint_decode( idxs, wordcnt, index_n, pulsestrack, num_tracks );
     150             : 
     151    74812384 :         for ( track = num_tracks - 1; track >= 1; track-- )
     152             :         {
     153    56109288 :             pulses = pulsestrack[track];
     154             : 
     155    56109288 :             if ( pulses )
     156             :             {
     157    55357407 :                 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    55357407 :                 D_ACELP_decode_arithtrack( code + track, s, pulses, num_tracks, 16 );
     162             :             }
     163             :             else
     164             :             {
     165             :                 /* track is empty */
     166    12781977 :                 for ( k = track; k < 16 * num_tracks; k += num_tracks )
     167             :                 {
     168    12030096 :                     code[k] = 0.0f;
     169             :                 }
     170             :             }
     171             :         }
     172    18703096 :         s = index_n[0];
     173    18703096 :         pulses = pulsestrack[0];
     174             :         /* safety check in case of bit errors */
     175    18703096 :         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    18703096 :         if ( pulses )
     182             :         {
     183    18474361 :             D_ACELP_decode_arithtrack( code, s, pulses, num_tracks, 16 );
     184             :         }
     185             :         else
     186             :         {
     187             :             /* track is empty */
     188     3888495 :             for ( k = 0; k < 16 * num_tracks; k += num_tracks )
     189             :             {
     190     3659760 :                 code[k] = 0.0f;
     191             :             }
     192             :         }
     193             :     }
     194             : 
     195    19593817 :     return;
     196             : }
     197             : 
     198             : 
     199    73831768 : 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  1255140056 :     for ( k = (tracklen) -1; k >= 0; k-- )
     209             :     {
     210  1181308288 :         v[k * trackstep] = 0.0f; /* default: there is no pulse here */
     211  1380767841 :         while ( ( p ) && ( s >= pulsestostates[k][p - 1] ) )
     212             :         {
     213   199459553 :             s -= pulsestostates[k][p - 1];
     214   199459553 :             if ( v[k * trackstep] )
     215             :             {
     216             :                 /* there is a pulse here already = sign is known */
     217    12985305 :                 if ( v[k * trackstep] > 0.0f )
     218             :                 {
     219     6173915 :                     v[k * trackstep]++; /* place one more pulse here */
     220             :                 }
     221             :                 else
     222             :                 {
     223     6811390 :                     v[k * trackstep]--; /* place one more pulse here */
     224             :                 }
     225             :             }
     226             :             else
     227             :             {
     228             :                 /* this is the first pulse here -> determine sign */
     229   186474248 :                 if ( s & 1 )
     230             :                 {
     231    93270196 :                     v[k * trackstep] = -1.0f; /* place a negative pulse here */
     232             :                 }
     233             :                 else
     234             :                 {
     235    93204052 :                     v[k * trackstep] = +1.0f; /* place a negative pulse here */
     236             :                 }
     237   186474248 :                 s >>= 1;
     238             :             }
     239   199459553 :             p--; /* one pulse placed, so one less left */
     240             :         }
     241             :     }
     242             : 
     243    73831768 :     return;
     244             : }
     245             : 
     246             : 
     247    18703096 : 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    18703096 :     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    18703096 :     indx_flag = 0;
     263    18703096 :     indx_flag_1 = 0;
     264    18703096 :     indx_flag_2 = 0;
     265    93515480 :     for ( track = 0; track < track_num; track++ )
     266             :     {
     267    74812384 :         indx_flag += ( pulse_num[track] >> 2 );
     268    74812384 :         indx_flag_1 += ( pulse_num[track] >> 1 );
     269    74812384 :         indx_flag_2 += ( pulse_num[track] >> 3 );
     270             :     }
     271             : 
     272    18703096 :     if ( indx_flag >= track_num )
     273             :     {
     274     3426717 :         hi_to_low[4] = 9;
     275             :     }
     276             :     else
     277             :     {
     278    15276379 :         hi_to_low[4] = 1;
     279             :     }
     280             : 
     281    18703096 :     if ( indx_flag_2 >= 1 )
     282             :     {
     283        2160 :         hi_to_low[7] = 9;
     284             :     }
     285             :     else
     286             :     {
     287    18700936 :         hi_to_low[7] = 1;
     288             :     }
     289             : 
     290    18703096 :     if ( indx_flag_1 >= track_num )
     291             :     {
     292    12395594 :         if ( indx_flag >= track_num )
     293             :         {
     294     3426717 :             index = 0;
     295     3426717 :             if ( indx_flag_2 >= 1 )
     296             :             {
     297        2278 :                 for ( track = ( wordcnt - 1 ); track >= 6; track-- )
     298             :                 {
     299         118 :                     index = ( index << 16 ) + idxs[track];
     300             :                 }
     301        2160 :                 index_n[3] = ( ( (uint32_t) idxs[5] ) << 8 ) + ( ( idxs[4] >> 8 ) & 0xff );
     302        2160 :                 index_n[2] = ( ( ( (uint32_t) idxs[4] ) << 16 ) + idxs[3] ) & 0xffffffUL;
     303        2160 :                 index_n[1] = ( ( (uint32_t) idxs[2] ) << 8 ) + ( ( idxs[1] >> 8 ) & 0xff );
     304        2160 :                 index_n[0] = ( ( ( (uint32_t) idxs[1] ) << 16 ) + idxs[0] ) & 0xffffffUL;
     305             :             }
     306             :             else
     307             :             {
     308     6746185 :                 for ( track = ( wordcnt - 1 ); track >= track_num; track-- )
     309             :                 {
     310     3321628 :                     index = ( index << 16 ) + idxs[track];
     311             :                 }
     312    17122785 :                 for ( track = 0; track < track_num; track++ )
     313             :                 {
     314    13698228 :                     index_n[track] = idxs[track];
     315             :                 }
     316             :             }
     317             :         }
     318             :         else
     319             :         {
     320     8968877 :             index = 0;
     321    22360256 :             for ( track = ( wordcnt - 1 ); track >= 2; track-- )
     322             :             {
     323    13391379 :                 index = ( index << 16 ) + idxs[track];
     324             :             }
     325             : 
     326     8968877 :             index_n[3] = idxs[1] & 0xff;
     327     8968877 :             index_n[2] = idxs[1] >> 8;
     328     8968877 :             index_n[1] = idxs[0] & 0xff;
     329     8968877 :             index_n[0] = idxs[0] >> 8;
     330             :         }
     331             : 
     332    12395594 :         track_num1 = track_num - 1;
     333    12395594 :         pulse_num1 = pulse_num[track_num1];
     334    12395594 :         index = ( index << hi_to_low[pulse_num1] ) + ( index_n[track_num1] >> low_len[pulse_num1] );
     335    49582376 :         for ( track = ( track_num - 1 ); track > 0; track-- )
     336             :         {
     337    37186782 :             track_num1 = track - 1;
     338    37186782 :             pulse_num0 = pulse_num[track_num1];
     339    37186782 :             pulse_num1 = pulse_num[track];
     340    37186782 :             index = ( index << hi_to_low[pulse_num0] ) + ( index_n[track_num1] >> low_len[pulse_num0] );
     341             : 
     342    37186782 :             div_tmp = index / indx_fact[pulse_num1];
     343    37186782 :             indx_tmp = index - div_tmp * indx_fact[pulse_num1];
     344    37186782 :             index_n[track] = (uint32_t) ( ( index_n[track] & low_mask[pulse_num1] ) + ( indx_tmp << low_len[pulse_num1] ) );
     345    37186782 :             index = div_tmp;
     346             :         }
     347    12395594 :         pulse_num1 = pulse_num[0];
     348    12395594 :         index_n[0] = (uint32_t) ( ( index_n[0] & low_mask[pulse_num1] ) + ( index << low_len[pulse_num1] ) );
     349             :     }
     350             :     else
     351             :     {
     352     6307502 :         index = 0;
     353    19570440 :         for ( track = ( wordcnt - 1 ); track >= 0; track-- )
     354             :         {
     355    13262938 :             index = ( index << 16 ) + idxs[track];
     356             :         }
     357    25230008 :         for ( track = 3; track > 0; track-- )
     358             :         {
     359    18922506 :             pulse_num1 = pulse_num[track];
     360    18922506 :             index_n[track] = index & index_mask_ACELP[pulse_num1];
     361    18922506 :             index = index >> index_len[pulse_num1];
     362             :         }
     363     6307502 :         index_n[0] = (uint32_t) index;
     364             :     }
     365             : 
     366    18703096 :     return;
     367             : }

Generated by: LCOV version 1.14