LCOV - code coverage report
Current view: top level - lib_enc - cod4t64.c (source / functions) Hit Total Coverage
Test: Coverage on main -- short test vectors @ a21f94bc6bac334fe001a5bad2f7b32b79038097 Lines: 423 443 95.5 %
Date: 2025-11-02 05:54:52 Functions: 14 14 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 "rom_enc.h"
      45             : #include "prot.h"
      46             : #include "rom_com.h"
      47             : #include "wmc_auto.h"
      48             : 
      49             : /*---------------------------------------------------------------------*
      50             :  * Local function prototypes
      51             :  *---------------------------------------------------------------------*/
      52             : 
      53             : static int16_t quant_1p_N1( const int16_t pos, const int16_t N );
      54             : static int16_t quant_3p_3N1( const int16_t pos1, const int16_t pos2, const int16_t pos3, const int16_t N );
      55             : static int32_t quant_4p_4N( const int16_t pos[], const int16_t N );
      56             : static int32_t quant_5p_5N( const int16_t pos[], const int16_t N );
      57             : static int32_t quant_6p_6N_2( const int16_t pos[], const int16_t N );
      58             : static int16_t pre_process( const float v[], int16_t pos_vector[], int16_t pos_vector_num[], int16_t *pulse_pos_num );
      59             : static int32_t fcb_encode_position( const int16_t pos_vector[], int16_t n, const int16_t pos_num, const int16_t flag );
      60             : static int32_t fcb_encode_class( const int16_t buffer[], const int16_t pulse_num, const int16_t pos_num );
      61             : static int32_t fcb_encode_PI( const float v[], const int16_t pulse_num );
      62             : 
      63             : 
      64             : /*---------------------------------------------------------------------*
      65             :  * ACELP_4t64()
      66             :  *
      67             :  * 20, 36, 44, 52, 64, 72, 88 bits algebraic codebook.
      68             :  * 4 tracks x 16 positions per track = 64 samples.
      69             :  *
      70             :  * 20 bits --> 4 pulses in a frame of 64 samples.
      71             :  * 36 bits --> 8 pulses in a frame of 64 samples.
      72             :  * 44 bits 13 + 9 + 13 + 9 --> 10 pulses in a frame of 64 samples.
      73             :  * 52 bits 13 + 13 + 13 + 13 --> 12 pulses in a frame of 64 samples.
      74             :  * 64 bits 2 + 2 + 2 + 2 + 14 + 14 + 14 + 14 -->
      75             :  *                               16 pulses in a frame of 64 samples.
      76             :  * 72 bits 10 + 2 + 10 + 2 + 10 + 14 + 10 + 14 -->
      77             :  *                               18 pulses in a frame of 64 samples.
      78             :  * 88 bits 11 + 11 + 11 + 11 + 11 + 11 + 11 + 11 -->
      79             :  *                               24 pulses in a frame of 64 samples.
      80             :  * All pulses can have two (2) possible amplitudes: +1 or -1.
      81             :  * Each pulse can have sixteen (16) possible positions.
      82             :  *---------------------------------------------------------------------*/
      83             : 
      84      182031 : int16_t acelp_4t64(
      85             :     BSTR_ENC_HANDLE hBstr,    /* i/o: encoder bitstream handle                      */
      86             :     float dn[],               /* i  : corr. between target and h[].                 */
      87             :     const float cn[],         /* i  : residual after long term prediction           */
      88             :     const float H[],          /* i  : impulse response of weighted synthesis filter */
      89             :     float R[],                /* i  : autocorrelation values                        */
      90             :     const int16_t acelpautoc, /* i  : autocorrealtion flag                          */
      91             :     float code[],             /* o  : algebraic (fixed) codebook excitation         */
      92             :     float y[],                /* o  : filtered fixed codebook excitation            */
      93             :     int16_t nbbits,           /* i  : number of bits per codebook                   */
      94             :     const int16_t cmpl_flag,  /* i  : coomplexity reduction flag                    */
      95             :     const int16_t Opt_AMR_WB  /* i  : flag indicating AMR-WB IO mode                */
      96             : )
      97             : {
      98             :     int16_t i, k, index, track;
      99             :     int32_t L_index;
     100             : 
     101             :     int16_t ind[NPMAXPT * NB_TRACK_FCB_4T + 32];
     102      182031 :     int16_t saved_bits = 0;
     103             :     PulseConfig config;
     104             :     int16_t wordcnt, bitcnt;
     105             :     int16_t indexing_indices[6];
     106             : 
     107             :     /*-----------------------------------------------------------------*
     108             :      * Configuration
     109             :      *-----------------------------------------------------------------*/
     110             : 
     111             : 
     112      182031 :     switch ( nbbits )
     113             :     {
     114        3111 :         case 20:               /* EVS/AMR-WB pulse indexing: 20 bits, 4 pulses, 4 tracks  */
     115        3111 :             config.nbiter = 4; /* 4x12x16=768 loop                                        */
     116        3111 :             config.alp = 2.0f;
     117        3111 :             config.nb_pulse = 4;
     118        3111 :             config.fixedpulses = 0;
     119        3111 :             config.nbpos[0] = 4;
     120        3111 :             config.nbpos[1] = 8;
     121        3111 :             break;
     122             : 
     123        6546 :         case 28:               /* EVS pulse indexing: 28 bits, 6 pulses, 4 tracks     */
     124        6546 :             config.nbiter = 4; /* 4x20x16=1280 loops                                  */
     125        6546 :             config.alp = 1.0f; /* coeff for sign setting                              */
     126        6546 :             config.nb_pulse = 6;
     127        6546 :             config.fixedpulses = 0;
     128        6546 :             config.nbpos[0] = 6;
     129        6546 :             config.nbpos[1] = 6;
     130        6546 :             config.nbpos[2] = 8;
     131        6546 :             break;
     132             : 
     133      166872 :         case 36:               /* EVS/AMR-WB pulse indexing: 36 bits, 8 pulses, 4 tracks  */
     134      166872 :             config.nbiter = 4; /* 4x20x16=1280 loops                                      */
     135      166872 :             config.alp = 1.0f; /* coeff for sign setting                                  */
     136      166872 :             config.nb_pulse = 8;
     137      166872 :             config.fixedpulses = 2;
     138      166872 :             config.nbpos[0] = 4;
     139      166872 :             config.nbpos[1] = 8;
     140      166872 :             config.nbpos[2] = 8;
     141      166872 :             break;
     142             : 
     143        1144 :         case 43:               /* EVS pulse indexing:    43 bits, 10 pulses, 4 tracks */
     144             :         case 44:               /* AMR-WB pulse indexing: 44 bits, 10 pulses, 4 tracks */
     145        1144 :             config.nbiter = 4; /* 4x26x16=1664 loops                                  */
     146        1144 :             config.alp = 1.0f;
     147        1144 :             config.nb_pulse = 10;
     148        1144 :             config.fixedpulses = 2;
     149        1144 :             config.nbpos[0] = 4;
     150        1144 :             config.nbpos[1] = 6;
     151        1144 :             config.nbpos[2] = 8;
     152        1144 :             config.nbpos[3] = 8;
     153        1144 :             break;
     154             : 
     155         800 :         case 50:               /* EVS pulse indexing:    50 bits, 12 pulses, 4 tracks */
     156             :         case 52:               /* AMR-WB pulse indexing: 52 bits, 12 pulses, 4 tracks */
     157         800 :             config.nbiter = 4; /* 4x26x16=1664 loops                                  */
     158         800 :             config.alp = 1.0f;
     159         800 :             config.nb_pulse = 12;
     160         800 :             config.fixedpulses = 4;
     161         800 :             config.nbpos[0] = 4;
     162         800 :             config.nbpos[1] = 6;
     163         800 :             config.nbpos[2] = 8;
     164         800 :             config.nbpos[3] = 8;
     165         800 :             break;
     166             : 
     167         838 :         case 62:               /* EVS pulse indexing:    62 bits, 16 pulses, 4 tracks */
     168             :         case 64:               /* AMR-WB pulse indexing: 64 bits, 16 pulses, 4 tracks */
     169         838 :             config.nbiter = 3; /* 3x36x16=1728 loops                                  */
     170         838 :             config.alp = 0.8F;
     171         838 :             config.nb_pulse = 16;
     172         838 :             config.fixedpulses = 4;
     173         838 :             config.nbpos[0] = 4;
     174         838 :             config.nbpos[1] = 4;
     175         838 :             config.nbpos[2] = 6;
     176         838 :             config.nbpos[3] = 6;
     177         838 :             config.nbpos[4] = 8;
     178         838 :             config.nbpos[5] = 8;
     179         838 :             break;
     180             : 
     181         960 :         case 72:               /* AMR-WB pulse indexing: 72 bits, 18 pulses, 4 tracks */
     182         960 :             config.nbiter = 3; /* 3x35x16=1680 loops                                  */
     183         960 :             config.alp = 0.75F;
     184         960 :             config.nb_pulse = 18;
     185         960 :             config.fixedpulses = 4;
     186         960 :             config.nbpos[0] = 2;
     187         960 :             config.nbpos[1] = 3;
     188         960 :             config.nbpos[2] = 4;
     189         960 :             config.nbpos[3] = 5;
     190         960 :             config.nbpos[4] = 6;
     191         960 :             config.nbpos[5] = 7;
     192         960 :             config.nbpos[6] = 8;
     193         960 :             break;
     194             : 
     195        1760 :         case 88:               /* AMR-WB pulse indexing: 88 bits, 24 pulses, 4 tracks */
     196        1760 :             config.nbiter = 2; /* 2x53x16=1696 loop                                   */
     197        1760 :             config.alp = 0.5f;
     198        1760 :             config.nb_pulse = 24;
     199        1760 :             config.fixedpulses = 4;
     200        1760 :             config.nbpos[0] = 2;
     201        1760 :             config.nbpos[1] = 2;
     202        1760 :             config.nbpos[2] = 3;
     203        1760 :             config.nbpos[3] = 4;
     204        1760 :             config.nbpos[4] = 5;
     205        1760 :             config.nbpos[5] = 6;
     206        1760 :             config.nbpos[6] = 7;
     207        1760 :             config.nbpos[7] = 8;
     208        1760 :             config.nbpos[8] = 8;
     209        1760 :             config.nbpos[9] = 8;
     210        1760 :             break;
     211             : 
     212           0 :         case 87: /* EVS pulse indexing:   87 bits, 26 pulses, 4 tracks  */
     213           0 :             config.nbiter = 1;
     214           0 :             config.alp = 0.5F;
     215           0 :             config.nb_pulse = 26;
     216           0 :             config.fixedpulses = 4;
     217           0 :             config.nbpos[0] = 4;
     218           0 :             config.nbpos[1] = 6;
     219           0 :             config.nbpos[2] = 6;
     220           0 :             config.nbpos[3] = 8;
     221           0 :             config.nbpos[4] = 8;
     222           0 :             config.nbpos[5] = 8;
     223           0 :             config.nbpos[6] = 8;
     224           0 :             config.nbpos[7] = 8;
     225           0 :             config.nbpos[8] = 8;
     226           0 :             config.nbpos[9] = 8;
     227           0 :             config.nbpos[10] = 8;
     228           0 :             break;
     229             :     }
     230             : 
     231             :     /* reduce the number of iterations as a compromise between the performance and complexity */
     232      182031 :     if ( cmpl_flag > 0 )
     233             :     {
     234      164607 :         config.nbiter = cmpl_flag;
     235             :     }
     236             : 
     237      182031 :     config.codetrackpos = TRACKPOS_FIXED_FIRST;
     238      182031 :     config.bits = nbbits;
     239             : 
     240             : 
     241             :     /*-----------------------------------------------------------------*
     242             :      * Search
     243             :      *-----------------------------------------------------------------*/
     244             : 
     245      182031 :     if ( acelpautoc )
     246             :     {
     247      164053 :         E_ACELP_4tsearchx( dn, cn, R, code, &config, ind );
     248             : 
     249             :         /* Generate weighted code */
     250      164053 :         set_f( y, 0.0f, L_SUBFR );
     251    10663445 :         for ( i = 0; i < L_SUBFR; i++ )
     252             :         {
     253             :             /* Code is sparse, so check which samples are non-zero */
     254    10499392 :             if ( code[i] != 0 )
     255             :             {
     256    41577740 :                 for ( k = 0; k < L_SUBFR - i; k++ )
     257             :                 {
     258    40289119 :                     y[i + k] += code[i] * H[k];
     259             :                 }
     260             :             }
     261             :         }
     262             :     }
     263             :     else
     264             :     {
     265       17978 :         E_ACELP_4tsearch( dn, cn, H, code, &config, ind, y );
     266             :     }
     267             : 
     268             :     /*-----------------------------------------------------------------*
     269             :      * Indexing
     270             :      *-----------------------------------------------------------------*/
     271             : 
     272      182031 :     if ( !Opt_AMR_WB )
     273             :     {
     274             :         /* EVS pulse indexing */
     275             : 
     276      174831 :         saved_bits = E_ACELP_indexing( code, config, NB_TRACK_FCB_4T, indexing_indices );
     277             : 
     278      174831 :         saved_bits = 0;
     279             : 
     280      174831 :         wordcnt = nbbits >> 4;
     281      174831 :         bitcnt = nbbits & 15;
     282      515834 :         for ( i = 0; i < wordcnt; i++ )
     283             :         {
     284      341003 :             push_indice( hBstr, IND_ALG_CDBK_4T64, indexing_indices[i], 16 );
     285             :         }
     286      174831 :         if ( bitcnt )
     287             :         {
     288      174831 :             push_indice( hBstr, IND_ALG_CDBK_4T64, indexing_indices[i], bitcnt );
     289             :         }
     290             :     }
     291             :     else
     292             :     {
     293             :         /* AMR-WB pulse indexing */
     294             : 
     295        7200 :         if ( nbbits == 20 )
     296             :         {
     297        4800 :             for ( track = 0; track < NB_TRACK_FCB_4T; track++ )
     298             :             {
     299        3840 :                 k = track * NPMAXPT;
     300        3840 :                 index = quant_1p_N1( ind[k], 4 );
     301        3840 :                 push_indice( hBstr, IND_ALG_CDBK_4T64, index, 5 );
     302             :             }
     303             :         }
     304        6240 :         else if ( nbbits == 36 )
     305             :         {
     306        4800 :             for ( track = 0; track < NB_TRACK_FCB_4T; track++ )
     307             :             {
     308        3840 :                 k = track * NPMAXPT;
     309        3840 :                 index = quant_2p_2N1( ind[k], ind[k + 1], 4 );
     310        3840 :                 push_indice( hBstr, IND_ALG_CDBK_4T64, index, 9 );
     311             :             }
     312             :         }
     313        5280 :         else if ( nbbits == 44 )
     314             :         {
     315        2880 :             for ( track = 0; track < ( NB_TRACK_FCB_4T - 2 ); track++ )
     316             :             {
     317        1920 :                 k = track * NPMAXPT;
     318        1920 :                 index = quant_3p_3N1( ind[k], ind[k + 1], ind[k + 2], 4 );
     319        1920 :                 push_indice( hBstr, IND_ALG_CDBK_4T64, index, 13 );
     320             :             }
     321             : 
     322        2880 :             for ( track = 2; track < NB_TRACK_FCB_4T; track++ )
     323             :             {
     324        1920 :                 k = track * NPMAXPT;
     325        1920 :                 index = quant_2p_2N1( ind[k], ind[k + 1], 4 );
     326        1920 :                 push_indice( hBstr, IND_ALG_CDBK_4T64, index, 9 );
     327             :             }
     328             :         }
     329        4320 :         else if ( nbbits == 52 )
     330             :         {
     331        4000 :             for ( track = 0; track < NB_TRACK_FCB_4T; track++ )
     332             :             {
     333        3200 :                 k = track * NPMAXPT;
     334        3200 :                 index = quant_3p_3N1( ind[k], ind[k + 1], ind[k + 2], 4 );
     335        3200 :                 push_indice( hBstr, IND_ALG_CDBK_4T64, index, 13 );
     336             :             }
     337             :         }
     338        3520 :         else if ( nbbits == 64 )
     339             :         {
     340        4000 :             for ( track = 0; track < NB_TRACK_FCB_4T; track++ )
     341             :             {
     342        3200 :                 k = track * NPMAXPT;
     343        3200 :                 L_index = quant_4p_4N( &ind[k], 4 );
     344        3200 :                 index = ( ( L_index >> 14 ) & 3 );
     345        3200 :                 push_indice( hBstr, IND_ALG_CDBK_4T64_1, index, 2 );
     346             :             }
     347             : 
     348        4000 :             for ( track = 0; track < NB_TRACK_FCB_4T; track++ )
     349             :             {
     350        3200 :                 k = track * NPMAXPT;
     351        3200 :                 L_index = quant_4p_4N( &ind[k], 4 );
     352        3200 :                 index = ( L_index & 0x3FFF );
     353        3200 :                 push_indice( hBstr, IND_ALG_CDBK_4T64_2, index, 14 );
     354             :             }
     355             :         }
     356        2720 :         else if ( nbbits == 72 )
     357             :         {
     358        2880 :             for ( track = 0; track < ( NB_TRACK_FCB_4T - 2 ); track++ )
     359             :             {
     360        1920 :                 k = track * NPMAXPT;
     361        1920 :                 L_index = quant_5p_5N( &ind[k], 4 );
     362        1920 :                 index = ( ( L_index >> 10 ) & 0x03FF );
     363        1920 :                 push_indice( hBstr, IND_ALG_CDBK_4T64_1, index, 10 );
     364             :             }
     365             : 
     366        2880 :             for ( track = 2; track < NB_TRACK_FCB_4T; track++ )
     367             :             {
     368        1920 :                 k = track * NPMAXPT;
     369        1920 :                 L_index = quant_4p_4N( &ind[k], 4 );
     370        1920 :                 index = ( ( L_index >> 14 ) & 3 );
     371        1920 :                 push_indice( hBstr, IND_ALG_CDBK_4T64_1, index, 2 );
     372             :             }
     373             : 
     374        2880 :             for ( track = 0; track < ( NB_TRACK_FCB_4T - 2 ); track++ )
     375             :             {
     376        1920 :                 k = track * NPMAXPT;
     377        1920 :                 L_index = quant_5p_5N( &ind[k], 4 );
     378        1920 :                 index = ( L_index & 0x03FF );
     379        1920 :                 push_indice( hBstr, IND_ALG_CDBK_4T64_2, index, 10 );
     380             :             }
     381             : 
     382        2880 :             for ( track = 2; track < NB_TRACK_FCB_4T; track++ )
     383             :             {
     384        1920 :                 k = track * NPMAXPT;
     385        1920 :                 L_index = quant_4p_4N( &ind[k], 4 );
     386        1920 :                 index = ( L_index & 0x3FFF );
     387        1920 :                 push_indice( hBstr, IND_ALG_CDBK_4T64_2, index, 14 );
     388             :             }
     389             :         }
     390        1760 :         else if ( nbbits == 88 )
     391             :         {
     392        8800 :             for ( track = 0; track < NB_TRACK_FCB_4T; track++ )
     393             :             {
     394        7040 :                 k = track * NPMAXPT;
     395        7040 :                 L_index = quant_6p_6N_2( &ind[k], 4 );
     396        7040 :                 index = ( ( L_index >> 11 ) & 0x07FF );
     397        7040 :                 push_indice( hBstr, IND_ALG_CDBK_4T64_1, index, 11 );
     398             :             }
     399             : 
     400        8800 :             for ( track = 0; track < NB_TRACK_FCB_4T; track++ )
     401             :             {
     402        7040 :                 k = track * NPMAXPT;
     403        7040 :                 L_index = quant_6p_6N_2( &ind[k], 4 );
     404        7040 :                 index = ( L_index & 0x07FF );
     405        7040 :                 push_indice( hBstr, IND_ALG_CDBK_4T64_2, index, 11 );
     406             :             }
     407             :         }
     408             :     }
     409             : 
     410      182031 :     return saved_bits;
     411             : }
     412             : 
     413             : 
     414             : /*---------------------------------------------------------------------*
     415             :  * Quantization of 1 pulse with N+1 bits:                              *
     416             :  *---------------------------------------------------------------------*/
     417             : 
     418             : /*! r: return N+1 bits */
     419       44696 : static int16_t quant_1p_N1(
     420             :     const int16_t pos, /* i  : position of the pulse       */
     421             :     const int16_t N    /* i  : number of bits for position */
     422             : )
     423             : {
     424             :     int16_t mask, index;
     425             : 
     426       44696 :     mask = ( ( 1 << N ) - 1 );
     427             : 
     428       44696 :     index = ( pos & mask );
     429             : 
     430       44696 :     if ( ( pos & NB_POS_FCB_4T ) != 0 )
     431             :     {
     432       22524 :         index += 1 << N;
     433             :     }
     434             : 
     435       44696 :     return index;
     436             : }
     437             : 
     438             : 
     439             : /*---------------------------------------------------------------------*
     440             :  * Quantization of 2 pulses with 2*N+1 bits:                           *
     441             :  *---------------------------------------------------------------------*/
     442             : 
     443             : /*! r: return (2*N)+1 bits */
     444      160277 : int16_t quant_2p_2N1(
     445             :     const int16_t pos1, /* i  : position of the pulse 1     */
     446             :     const int16_t pos2, /* i  : position of the pulse 2     */
     447             :     const int16_t N     /* i  : number of bits for position */
     448             : )
     449             : {
     450             :     int16_t mask, index;
     451             : 
     452      160277 :     mask = ( ( 1 << N ) - 1 );
     453             : 
     454             :     /*-----------------------------------------------------------------*
     455             :      * sign of 1st pulse == sign of 2nd pulse
     456             :      *-----------------------------------------------------------------*/
     457             : 
     458      160277 :     if ( ( ( pos2 ^ pos1 ) & NB_POS_FCB_4T ) == 0 )
     459             :     {
     460       85857 :         if ( ( pos1 - pos2 ) <= 0 )
     461             :         {
     462       72550 :             index = ( ( pos1 & mask ) << N ) + ( pos2 & mask );
     463             :         }
     464             :         else
     465             :         {
     466       13307 :             index = ( ( pos2 & mask ) << N ) + ( pos1 & mask );
     467             :         }
     468       85857 :         if ( ( pos1 & NB_POS_FCB_4T ) != 0 )
     469             :         {
     470       42816 :             index += 1 << ( 2 * N );
     471             :         }
     472             :     }
     473             :     else
     474             :     {
     475             : 
     476             :         /*-----------------------------------------------------------------*
     477             :          * sign of 1st pulse != sign of 2nd pulse
     478             :          *-----------------------------------------------------------------*/
     479       74420 :         if ( ( ( pos1 & mask ) - ( pos2 & mask ) ) <= 0 )
     480             :         {
     481       60751 :             index = ( ( pos2 & mask ) << N ) + ( pos1 & mask );
     482       60751 :             if ( ( pos2 & NB_POS_FCB_4T ) != 0 )
     483             :             {
     484       29667 :                 index += 1 << ( 2 * N );
     485             :             }
     486             :         }
     487             :         else
     488             :         {
     489       13669 :             index = ( ( pos1 & mask ) << N ) + ( pos2 & mask );
     490       13669 :             if ( ( pos1 & NB_POS_FCB_4T ) != 0 )
     491             :             {
     492        6563 :                 index += 1 << ( 2 * N );
     493             :             }
     494             :         }
     495             :     }
     496             : 
     497      160277 :     return index;
     498             : }
     499             : 
     500             : /*---------------------------------------------------------------------*
     501             :  * Quantization of 3 pulses with 3*N+1 bits:                           *
     502             :  *---------------------------------------------------------------------*/
     503             : 
     504             : /*! r: return (3*N)+1 bits */
     505       29512 : static int16_t quant_3p_3N1(
     506             :     const int16_t pos1, /* i  : position of the pulse 1     */
     507             :     const int16_t pos2, /* i  : position of the pulse 2     */
     508             :     const int16_t pos3, /* i  : position of the pulse 3     */
     509             :     const int16_t N     /* i  : number of bits for position */
     510             : )
     511             : {
     512             :     int16_t index, nb_pos;
     513             : 
     514       29512 :     nb_pos = ( 1 << ( N - 1 ) );
     515             : 
     516             :     /* Quantization of 3 pulses with 3*N+1 bits */
     517       29512 :     if ( ( ( pos1 ^ pos2 ) & nb_pos ) == 0 )
     518             :     {
     519       14634 :         index = quant_2p_2N1( pos1, pos2, ( N - 1 ) );
     520       14634 :         index += ( pos1 & nb_pos ) << N;
     521       14634 :         index += quant_1p_N1( pos3, N ) << ( 2 * N );
     522             :     }
     523       14878 :     else if ( ( ( pos1 ^ pos3 ) & nb_pos ) == 0 )
     524             :     {
     525        8080 :         index = quant_2p_2N1( pos1, pos3, ( N - 1 ) );
     526        8080 :         index += ( pos1 & nb_pos ) << N;
     527        8080 :         index += quant_1p_N1( pos2, N ) << ( 2 * N );
     528             :     }
     529             :     else
     530             :     {
     531        6798 :         index = quant_2p_2N1( pos2, pos3, ( N - 1 ) );
     532        6798 :         index += ( pos2 & nb_pos ) << N;
     533        6798 :         index += quant_1p_N1( pos1, N ) << ( 2 * N );
     534             :     }
     535             : 
     536       29512 :     return index;
     537             : }
     538             : 
     539             : /*---------------------------------------------------------------------*
     540             :  * Quantization of 4 pulses with 4*N+1 bits:                           *
     541             :  *---------------------------------------------------------------------*/
     542             : 
     543             : /*! r: return (4*N)+1 bits */
     544        1804 : static int32_t quant_4p_4N1(
     545             :     const int16_t pos1, /* i  : position of the pulse 1     */
     546             :     const int16_t pos2, /* i  : position of the pulse 2     */
     547             :     const int16_t pos3, /* i  : position of the pulse 3     */
     548             :     const int16_t pos4, /* i  : position of the pulse 4     */
     549             :     const int16_t N     /* i  : number of bits for position */
     550             : )
     551             : {
     552             :     int32_t index, nb_pos;
     553             : 
     554        1804 :     nb_pos = ( 1 << ( N - 1 ) );
     555             : 
     556             :     /* Quantization of 4 pulses with 4*N+1 bits */
     557        1804 :     if ( ( ( pos1 ^ pos2 ) & nb_pos ) == 0 )
     558             :     {
     559         960 :         index = quant_2p_2N1( pos1, pos2, ( N - 1 ) );
     560         960 :         index += ( pos1 & nb_pos ) << N;
     561         960 :         index += quant_2p_2N1( pos3, pos4, N ) << ( 2 * N );
     562             :     }
     563         844 :     else if ( ( ( pos1 ^ pos3 ) & nb_pos ) == 0 )
     564             :     {
     565         464 :         index = quant_2p_2N1( pos1, pos3, ( N - 1 ) );
     566         464 :         index += ( pos1 & nb_pos ) << N;
     567         464 :         index += quant_2p_2N1( pos2, pos4, N ) << ( 2 * N );
     568             :     }
     569             :     else
     570             :     {
     571         380 :         index = quant_2p_2N1( pos2, pos3, ( N - 1 ) );
     572         380 :         index += ( pos2 & nb_pos ) << N;
     573         380 :         index += quant_2p_2N1( pos1, pos4, N ) << ( 2 * N );
     574             :     }
     575             : 
     576        1804 :     return ( index );
     577             : }
     578             : 
     579             : /*---------------------------------------------------------------------*
     580             :  * Quantization of 4 pulses with 4*N bits:                             *
     581             :  *---------------------------------------------------------------------*/
     582             : 
     583             : /*! r: return 4*N bits */
     584       16844 : static int32_t quant_4p_4N(
     585             :     const int16_t pos[], /* i  : position of the pulse 1..4  */
     586             :     const int16_t N      /* i  : number of bits for position */
     587             : )
     588             : {
     589             :     int16_t i, j, k, n_1;
     590             :     int16_t posA[4], posB[4];
     591       16844 :     int32_t nb_pos, index = 0;
     592             : 
     593       16844 :     n_1 = N - 1;
     594       16844 :     nb_pos = ( 1 << n_1 );
     595             : 
     596       16844 :     i = 0;
     597       16844 :     j = 0;
     598       84220 :     for ( k = 0; k < 4; k++ )
     599             :     {
     600       67376 :         if ( ( pos[k] & nb_pos ) == 0 )
     601             :         {
     602       33488 :             posA[i++] = pos[k];
     603             :         }
     604             :         else
     605             :         {
     606       33888 :             posB[j++] = pos[k];
     607             :         }
     608             :     }
     609             : 
     610       16844 :     switch ( i )
     611             :     {
     612         944 :         case 0:
     613         944 :             index = 1 << ( ( 4 * N ) - 3 );
     614         944 :             index += quant_4p_4N1( posB[0], posB[1], posB[2], posB[3], n_1 );
     615         944 :             break;
     616        4252 :         case 1:
     617        4252 :             index = quant_1p_N1( posA[0], n_1 ) << ( ( 3 * n_1 ) + 1 );
     618        4252 :             index += quant_3p_3N1( posB[0], posB[1], posB[2], n_1 );
     619        4252 :             break;
     620        6568 :         case 2:
     621        6568 :             index = quant_2p_2N1( posA[0], posA[1], n_1 ) << ( ( 2 * n_1 ) + 1 );
     622        6568 :             index += quant_2p_2N1( posB[0], posB[1], n_1 );
     623        6568 :             break;
     624        4220 :         case 3:
     625        4220 :             index = quant_3p_3N1( posA[0], posA[1], posA[2], n_1 ) << N;
     626        4220 :             index += quant_1p_N1( posB[0], n_1 );
     627        4220 :             break;
     628         860 :         case 4:
     629         860 :             index = quant_4p_4N1( posA[0], posA[1], posA[2], posA[3], n_1 );
     630         860 :             break;
     631             :     }
     632       16844 :     index += ( i & 3 ) << ( ( 4 * N ) - 2 );
     633             : 
     634       16844 :     return ( index );
     635             : }
     636             : 
     637             : /*---------------------------------------------------------------------*
     638             :  * Quantization of 5 pulses with 5*N bits:                             *
     639             :  *---------------------------------------------------------------------*/
     640             : 
     641             : /*! r: return 5*N bits */
     642        6712 : static int32_t quant_5p_5N(
     643             :     const int16_t pos[], /* i  : position of the pulse 1..5  */
     644             :     const int16_t N      /* i  : number of bits for position */
     645             : )
     646             : {
     647             :     int16_t i, j, k, n_1, nb_pos;
     648             :     int16_t posA[5], posB[5];
     649        6712 :     int32_t index = 0;
     650             : 
     651        6712 :     n_1 = N - 1;
     652        6712 :     nb_pos = ( 1 << n_1 );
     653             : 
     654        6712 :     i = 0;
     655        6712 :     j = 0;
     656       40272 :     for ( k = 0; k < 5; k++ )
     657             :     {
     658       33560 :         if ( ( pos[k] & nb_pos ) == 0 )
     659             :         {
     660       16376 :             posA[i++] = pos[k];
     661             :         }
     662             :         else
     663             :         {
     664       17184 :             posB[j++] = pos[k];
     665             :         }
     666             :     }
     667        6712 :     switch ( i )
     668             :     {
     669         216 :         case 0:
     670         216 :             index = 1 << ( ( 5 * N ) - 1 );
     671         216 :             index += quant_3p_3N1( posB[0], posB[1], posB[2], n_1 ) << ( ( 2 * N ) + 1 );
     672         216 :             index += quant_2p_2N1( posB[3], posB[4], N );
     673         216 :             break;
     674        1076 :         case 1:
     675        1076 :             index = 1 << ( ( 5 * N ) - 1 );
     676        1076 :             index += quant_3p_3N1( posB[0], posB[1], posB[2], n_1 ) << ( ( 2 * N ) + 1 );
     677        1076 :             index += quant_2p_2N1( posB[3], posA[0], N );
     678        1076 :             break;
     679        2236 :         case 2:
     680        2236 :             index = 1 << ( ( 5 * N ) - 1 );
     681        2236 :             index += quant_3p_3N1( posB[0], posB[1], posB[2], n_1 ) << ( ( 2 * N ) + 1 );
     682        2236 :             index += quant_2p_2N1( posA[0], posA[1], N );
     683        2236 :             break;
     684        2100 :         case 3:
     685        2100 :             index = quant_3p_3N1( posA[0], posA[1], posA[2], n_1 ) << ( ( 2 * N ) + 1 );
     686        2100 :             index += quant_2p_2N1( posB[0], posB[1], N );
     687        2100 :             break;
     688         892 :         case 4:
     689         892 :             index = quant_3p_3N1( posA[0], posA[1], posA[2], n_1 ) << ( ( 2 * N ) + 1 );
     690         892 :             index += quant_2p_2N1( posA[3], posB[0], N );
     691         892 :             break;
     692         192 :         case 5:
     693         192 :             index = quant_3p_3N1( posA[0], posA[1], posA[2], n_1 ) << ( ( 2 * N ) + 1 );
     694         192 :             index += quant_2p_2N1( posA[3], posA[4], N );
     695         192 :             break;
     696             :     }
     697             : 
     698        6712 :     return ( index );
     699             : }
     700             : 
     701             : /*---------------------------------------------------------------------*
     702             :  * Quantization of 6 pulses with 6*N-2 bits:                           *
     703             :  *---------------------------------------------------------------------*/
     704             : 
     705             : /*! r: return 6*N-2 bits */
     706       14080 : static int32_t quant_6p_6N_2(
     707             :     const int16_t pos[], /* i  : position of the pulse 1..6  */
     708             :     const int16_t N      /* i  : number of bits for position */
     709             : )
     710             : {
     711             :     int16_t i, j, k, n_1;
     712             :     int16_t posA[6], posB[6];
     713       14080 :     int32_t nb_pos, index = 0;
     714             : 
     715       14080 :     n_1 = N - 1;
     716       14080 :     nb_pos = 1 << n_1;
     717             : 
     718       14080 :     i = 0;
     719       14080 :     j = 0;
     720       98560 :     for ( k = 0; k < 6; k++ )
     721             :     {
     722       84480 :         if ( ( pos[k] & nb_pos ) == 0 )
     723             :         {
     724       40876 :             posA[i++] = pos[k];
     725             :         }
     726             :         else
     727             :         {
     728       43604 :             posB[j++] = pos[k];
     729             :         }
     730             :     }
     731       14080 :     switch ( i )
     732             :     {
     733         248 :         case 0:
     734         248 :             index = 1 << ( ( 6 * N ) - 5 );
     735         248 :             index += quant_5p_5N( posB, n_1 ) << N;
     736         248 :             index += quant_1p_N1( posB[5], n_1 );
     737         248 :             break;
     738        1364 :         case 1:
     739        1364 :             index = 1 << ( ( 6 * N ) - 5 );
     740        1364 :             index += quant_5p_5N( posB, n_1 ) << N;
     741        1364 :             index += quant_1p_N1( posA[0], n_1 );
     742        1364 :             break;
     743        3604 :         case 2:
     744        3604 :             index = 1 << ( ( 6 * N ) - 5 );
     745        3604 :             index += quant_4p_4N( posB, n_1 ) << ( ( 2 * n_1 ) + 1 );
     746        3604 :             index += quant_2p_2N1( posA[0], posA[1], n_1 );
     747        3604 :             break;
     748        4604 :         case 3:
     749        4604 :             index = quant_3p_3N1( posA[0], posA[1], posA[2], n_1 ) << ( ( 3 * n_1 ) + 1 );
     750        4604 :             index += quant_3p_3N1( posB[0], posB[1], posB[2], n_1 );
     751        4604 :             break;
     752        3000 :         case 4:
     753        3000 :             i = 2;
     754        3000 :             index = quant_4p_4N( posA, n_1 ) << ( ( 2 * n_1 ) + 1 );
     755        3000 :             index += quant_2p_2N1( posB[0], posB[1], n_1 );
     756        3000 :             break;
     757        1068 :         case 5:
     758        1068 :             i = 1;
     759        1068 :             index = quant_5p_5N( posA, n_1 ) << N;
     760        1068 :             index += quant_1p_N1( posB[0], n_1 );
     761        1068 :             break;
     762         192 :         case 6:
     763         192 :             i = 0;
     764         192 :             index = quant_5p_5N( posA, n_1 ) << N;
     765         192 :             index += quant_1p_N1( posA[5], n_1 );
     766         192 :             break;
     767             :     }
     768             : 
     769       14080 :     index += ( i & 3 ) << ( ( 6 * N ) - 4 );
     770             : 
     771       14080 :     return ( index );
     772             : }
     773             : 
     774             : /*---------------------------------------------------------------------*
     775             :  *order the pulse position                                             *
     776             :  *---------------------------------------------------------------------*/
     777             : 
     778             : /*! r: return sign value of pulse on a track */
     779       93876 : static int16_t pre_process(
     780             :     const float v[],          /* i  : the pulse vector                                   */
     781             :     int16_t pos_vector[],     /* o  : position of the pulse on a track                   */
     782             :     int16_t pos_vector_num[], /* o  : the pulse number on the position which have pulse  */
     783             :     int16_t *pulse_pos_num    /* i  : the number of position which have pulse            */
     784             : )
     785             : {
     786             :     int16_t j, k;
     787             :     int16_t sign;
     788             : 
     789       93876 :     sign = 0;
     790       93876 :     j = 0;
     791     1595892 :     for ( k = 0; k < 64; k += 4 )
     792             :     {
     793     1502016 :         if ( v[k] )
     794             :         {
     795      270637 :             pos_vector[j] = k >> 2;
     796      270637 :             pos_vector_num[j] = (int16_t) fabsf( v[k] );
     797      270637 :             if ( v[k] > 0 )
     798             :             {
     799      136325 :                 sign = sign << 1;
     800             :             }
     801             :             else
     802             :             {
     803      134312 :                 sign = ( sign << 1 ) + 1;
     804             :             }
     805      270637 :             j++;
     806             :         }
     807             :     }
     808       93876 :     *pulse_pos_num = j;
     809             : 
     810       93876 :     return sign;
     811             : }
     812             : 
     813             : /*---------------------------------------------------------------------*
     814             :  *encode the position                                                  *
     815             :  *---------------------------------------------------------------------*/
     816             : 
     817             : /*! r: return index of the positions which have pulse */
     818       93876 : static int32_t fcb_encode_position(
     819             :     const int16_t pos_vector[], /* i  : position of the pulse on a track                */
     820             :     int16_t n,
     821             :     const int16_t pos_num, /* i  : the number of position which have pulse         */
     822             :     const int16_t flag )
     823             : {
     824             :     int16_t i;
     825             :     int32_t mmm1;
     826             :     int16_t temp2;
     827             : 
     828       93876 :     mmm1 = PI_select_table[n][pos_num] - 1;
     829       93876 :     temp2 = pos_num;
     830             : 
     831       93876 :     if ( flag ) /* no decrease */
     832             :     {
     833      364513 :         for ( i = 0; i < pos_num; i++ )
     834             :         {
     835      270637 :             mmm1 -= PI_select_table[n - pos_vector[i] - 1][temp2--];
     836             :         }
     837             :     }
     838             :     else
     839             :     {
     840           0 :         for ( i = 0; i < pos_num; i++ )
     841             :         {
     842           0 :             mmm1 -= PI_select_table[n - pos_vector[i] - 1][temp2--];
     843           0 :             n--;
     844             :         }
     845             :     }
     846             : 
     847       93876 :     return mmm1;
     848             : }
     849             : 
     850             : /*---------------------------------------------------------------------*
     851             :  *encode class for 3p 4p 5p 6p/track                                   *
     852             :  *---------------------------------------------------------------------*/
     853             : 
     854             : /*! r: class index of the pulse on a track */
     855       10347 : static int32_t fcb_encode_cl(
     856             :     const int16_t buffer[],  /* i  : pulses on a track                         */
     857             :     const int16_t pulse_num, /* i  : pulses number on a track                  */
     858             :     const int16_t pos_num    /* i  : number of the position which have pulse   */
     859             : )
     860             : {
     861             :     int32_t k;
     862             :     int16_t i, temp1, temp2;
     863             : 
     864       10347 :     temp1 = pos_num + pulse_num - 1;
     865       10347 :     temp2 = pulse_num;
     866       10347 :     k = PI_select_table[temp1][pulse_num] - 1;
     867       10347 :     temp1--;
     868       21338 :     for ( i = 0; i < pulse_num; i++ )
     869             :     {
     870       10991 :         k -= PI_select_table[temp1 - buffer[i]][temp2--];
     871       10991 :         temp1--;
     872             :     }
     873             : 
     874       10347 :     return k;
     875             : }
     876             : 
     877             : /*---------------------------------------------------------------------*
     878             :  *encode the class and compute class offset                            *
     879             :  *---------------------------------------------------------------------*/
     880             : 
     881             : /*! r: class offset */
     882       93876 : static int32_t fcb_encode_class(
     883             :     const int16_t sector_6p_num[], /* i  : position which have pulse on a track             */
     884             :     const int16_t pulse_num,       /* i  : pulse number on a track                          */
     885             :     const int16_t pulse_pos_num    /* i  : number of position which have pulse on a track   */
     886             : )
     887             : {
     888             :     int16_t i, j;
     889             :     int32_t k, mn9_offet;
     890             :     int16_t vector_class[6];
     891             :     int16_t *vector_class_ptr;
     892             : 
     893       93876 :     mn9_offet = 0;
     894             : 
     895       93876 :     if ( pulse_pos_num < pulse_num )
     896             :     {
     897       10347 :         vector_class_ptr = vector_class;
     898       30397 :         for ( i = 0; i < pulse_pos_num; i++ )
     899             :         {
     900       31041 :             for ( j = 0; j < ( sector_6p_num[i] - 1 ); j++ )
     901             :             {
     902       10991 :                 *vector_class_ptr++ = i;
     903             :             }
     904             :         }
     905       10347 :         k = fcb_encode_cl( vector_class, pulse_num - pulse_pos_num, pulse_pos_num );
     906       10347 :         mn9_offet = PI_factor[pulse_pos_num] * k;
     907             :     }
     908             : 
     909       93876 :     return mn9_offet;
     910             : }
     911             : 
     912             : 
     913             : /*---------------------------------------------------------------------*
     914             :  *encode fcb pulse index                                               *
     915             :  *---------------------------------------------------------------------*/
     916             : 
     917             : /*! r: return index of the  pulse on a track */
     918       93876 : static int32_t fcb_encode_PI(
     919             :     const float v[],        /* i  : the pulse vector                      */
     920             :     const int16_t pulse_num /* i  : number of the pulse on a track        */
     921             : )
     922             : {
     923             :     int16_t vector_p[7];
     924             :     int16_t pulse_pos_num;
     925             :     int16_t vector_p_num[7];
     926             :     int32_t code_index;
     927             :     int16_t sign;
     928             : 
     929             :     /*order the pulse position*/
     930       93876 :     sign = pre_process( v, vector_p, vector_p_num, &pulse_pos_num );
     931             : 
     932             :     /*encode the position*/
     933       93876 :     code_index = fcb_encode_position( vector_p, 16, pulse_pos_num, 1 );
     934             : 
     935             :     /*encode the class and compute class offset*/
     936       93876 :     code_index += fcb_encode_class( vector_p_num, pulse_num, pulse_pos_num );
     937             : 
     938       93876 :     code_index = PI_offset[pulse_num][pulse_num + 1 - pulse_pos_num] + ( code_index << pulse_pos_num ) + sign;
     939             : 
     940       93876 :     return code_index;
     941             : }
     942             : 
     943             : 
     944             : /*--------------------------------------------------------------------------*
     945             :  * E_ACELP_code43bit
     946             :  *
     947             :  * Fixed bit-length arithmetic coding of pulses
     948             :  * v - (input) pulse vector
     949             :  * s - (output) encoded state
     950             :  * n - (output) range of possible states (0...n-1)
     951             :  * p - (output) number of pulses found
     952             :  * len - (input) length of pulse vector
     953             :  * trackstep - (input) step between tracks
     954             :  *--------------------------------------------------------------------------*/
     955             : 
     956       46938 : int16_t E_ACELP_code43bit(
     957             :     const float code[],
     958             :     uint32_t *ps,
     959             :     int16_t *p,
     960             :     uint16_t idxs[] )
     961             : {
     962             :     int16_t j, k, track;
     963             :     int16_t ind[32];
     964             :     int16_t tmp;
     965             :     int32_t joint_index;
     966       46938 :     int32_t joint_offset = 3611648; /*offset for 3 pulses per track*/
     967       46938 :     int16_t saved_bits = 0;
     968             : 
     969      140814 :     for ( track = 0; track < 2; track++ )
     970             :     {
     971       93876 :         k = track * NPMAXPT;
     972       93876 :         ps[track] = fcb_encode_PI( code + track, 3 );
     973       93876 :         p[track] = 3;
     974             :     }
     975             : 
     976      140814 :     for ( track = 2; track < NB_TRACK_FCB_4T; track++ )
     977             :     {
     978       93876 :         j = track * NPMAXPT;
     979     1566164 :         for ( k = track; k < 64; k += 4 )
     980             :         {
     981     1476076 :             if ( code[k] )
     982             :             {
     983      183964 :                 tmp = k >> 2;
     984      183964 :                 if ( code[k] < 0 )
     985             :                 {
     986       91811 :                     tmp += 16;
     987             :                 }
     988      183964 :                 if ( fabs( code[k] ) > 1 )
     989             :                 {
     990        3788 :                     ind[j] = tmp;
     991        3788 :                     ind[j + 1] = tmp;
     992        3788 :                     break;
     993             :                 }
     994             :                 else
     995             :                 {
     996      180176 :                     ind[j] = tmp;
     997      180176 :                     j++;
     998             :                 }
     999             :             }
    1000             :         }
    1001       93876 :         k = track * NPMAXPT;
    1002       93876 :         ps[track] = quant_2p_2N1( ind[k], ind[k + 1], 4 );
    1003       93876 :         p[track] = 2;
    1004             :     }
    1005       46938 :     joint_index = ps[0] * 5472 + ps[1];
    1006       46938 :     if ( joint_index >= joint_offset )
    1007             :     {
    1008       39086 :         joint_index += joint_offset;
    1009             :     }
    1010             :     else
    1011             :     {
    1012        7852 :         saved_bits += 1;
    1013             :     }
    1014             : 
    1015       46938 :     idxs[0] = ( ( ps[2] << 9 ) + ps[3] ) & 0xffff;
    1016       46938 :     idxs[1] = ( ( joint_index << 2 ) + ( ps[2] >> 7 ) ) & 0xffff;
    1017       46938 :     idxs[2] = (uint16_t) ( joint_index >> 14 );
    1018             : 
    1019       46938 :     return saved_bits;
    1020             : }

Generated by: LCOV version 1.14