LCOV - code coverage report
Current view: top level - lib_enc - cod4t64.c (source / functions) Hit Total Coverage
Test: Coverage on main -- short test vectors @ 6c9ddc4024a9c0e1ecb8f643f114a84a0e26ec6b Lines: 155 443 35.0 %
Date: 2025-05-23 08:37:30 Functions: 8 14 57.1 %

          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       64559 : 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       64559 :     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       64559 :     switch ( nbbits )
     113             :     {
     114         689 :         case 20:               /* EVS/AMR-WB pulse indexing: 20 bits, 4 pulses, 4 tracks  */
     115         689 :             config.nbiter = 4; /* 4x12x16=768 loop                                        */
     116         689 :             config.alp = 2.0f;
     117         689 :             config.nb_pulse = 4;
     118         689 :             config.fixedpulses = 0;
     119         689 :             config.nbpos[0] = 4;
     120         689 :             config.nbpos[1] = 8;
     121         689 :             break;
     122             : 
     123        2277 :         case 28:               /* EVS pulse indexing: 28 bits, 6 pulses, 4 tracks     */
     124        2277 :             config.nbiter = 4; /* 4x20x16=1280 loops                                  */
     125        2277 :             config.alp = 1.0f; /* coeff for sign setting                              */
     126        2277 :             config.nb_pulse = 6;
     127        2277 :             config.fixedpulses = 0;
     128        2277 :             config.nbpos[0] = 6;
     129        2277 :             config.nbpos[1] = 6;
     130        2277 :             config.nbpos[2] = 8;
     131        2277 :             break;
     132             : 
     133       61573 :         case 36:               /* EVS/AMR-WB pulse indexing: 36 bits, 8 pulses, 4 tracks  */
     134       61573 :             config.nbiter = 4; /* 4x20x16=1280 loops                                      */
     135       61573 :             config.alp = 1.0f; /* coeff for sign setting                                  */
     136       61573 :             config.nb_pulse = 8;
     137       61573 :             config.fixedpulses = 2;
     138       61573 :             config.nbpos[0] = 4;
     139       61573 :             config.nbpos[1] = 8;
     140       61573 :             config.nbpos[2] = 8;
     141       61573 :             break;
     142             : 
     143           0 :         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           0 :             config.nbiter = 4; /* 4x26x16=1664 loops                                  */
     146           0 :             config.alp = 1.0f;
     147           0 :             config.nb_pulse = 10;
     148           0 :             config.fixedpulses = 2;
     149           0 :             config.nbpos[0] = 4;
     150           0 :             config.nbpos[1] = 6;
     151           0 :             config.nbpos[2] = 8;
     152           0 :             config.nbpos[3] = 8;
     153           0 :             break;
     154             : 
     155           0 :         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           0 :             config.nbiter = 4; /* 4x26x16=1664 loops                                  */
     158           0 :             config.alp = 1.0f;
     159           0 :             config.nb_pulse = 12;
     160           0 :             config.fixedpulses = 4;
     161           0 :             config.nbpos[0] = 4;
     162           0 :             config.nbpos[1] = 6;
     163           0 :             config.nbpos[2] = 8;
     164           0 :             config.nbpos[3] = 8;
     165           0 :             break;
     166             : 
     167          20 :         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          20 :             config.nbiter = 3; /* 3x36x16=1728 loops                                  */
     170          20 :             config.alp = 0.8F;
     171          20 :             config.nb_pulse = 16;
     172          20 :             config.fixedpulses = 4;
     173          20 :             config.nbpos[0] = 4;
     174          20 :             config.nbpos[1] = 4;
     175          20 :             config.nbpos[2] = 6;
     176          20 :             config.nbpos[3] = 6;
     177          20 :             config.nbpos[4] = 8;
     178          20 :             config.nbpos[5] = 8;
     179          20 :             break;
     180             : 
     181           0 :         case 72:               /* AMR-WB pulse indexing: 72 bits, 18 pulses, 4 tracks */
     182           0 :             config.nbiter = 3; /* 3x35x16=1680 loops                                  */
     183           0 :             config.alp = 0.75F;
     184           0 :             config.nb_pulse = 18;
     185           0 :             config.fixedpulses = 4;
     186           0 :             config.nbpos[0] = 2;
     187           0 :             config.nbpos[1] = 3;
     188           0 :             config.nbpos[2] = 4;
     189           0 :             config.nbpos[3] = 5;
     190           0 :             config.nbpos[4] = 6;
     191           0 :             config.nbpos[5] = 7;
     192           0 :             config.nbpos[6] = 8;
     193           0 :             break;
     194             : 
     195           0 :         case 88:               /* AMR-WB pulse indexing: 88 bits, 24 pulses, 4 tracks */
     196           0 :             config.nbiter = 2; /* 2x53x16=1696 loop                                   */
     197           0 :             config.alp = 0.5f;
     198           0 :             config.nb_pulse = 24;
     199           0 :             config.fixedpulses = 4;
     200           0 :             config.nbpos[0] = 2;
     201           0 :             config.nbpos[1] = 2;
     202           0 :             config.nbpos[2] = 3;
     203           0 :             config.nbpos[3] = 4;
     204           0 :             config.nbpos[4] = 5;
     205           0 :             config.nbpos[5] = 6;
     206           0 :             config.nbpos[6] = 7;
     207           0 :             config.nbpos[7] = 8;
     208           0 :             config.nbpos[8] = 8;
     209           0 :             config.nbpos[9] = 8;
     210           0 :             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       64559 :     if ( cmpl_flag > 0 )
     233             :     {
     234       61080 :         config.nbiter = cmpl_flag;
     235             :     }
     236             : 
     237       64559 :     config.codetrackpos = TRACKPOS_FIXED_FIRST;
     238       64559 :     config.bits = nbbits;
     239             : 
     240             : 
     241             :     /*-----------------------------------------------------------------*
     242             :      * Search
     243             :      *-----------------------------------------------------------------*/
     244             : 
     245       64559 :     if ( acelpautoc )
     246             :     {
     247       60925 :         E_ACELP_4tsearchx( dn, cn, R, code, &config, ind );
     248             : 
     249             :         /* Generate weighted code */
     250       60925 :         set_f( y, 0.0f, L_SUBFR );
     251     3960125 :         for ( i = 0; i < L_SUBFR; i++ )
     252             :         {
     253             :             /* Code is sparse, so check which samples are non-zero */
     254     3899200 :             if ( code[i] != 0 )
     255             :             {
     256    15489681 :                 for ( k = 0; k < L_SUBFR - i; k++ )
     257             :                 {
     258    15010270 :                     y[i + k] += code[i] * H[k];
     259             :                 }
     260             :             }
     261             :         }
     262             :     }
     263             :     else
     264             :     {
     265        3634 :         E_ACELP_4tsearch( dn, cn, H, code, &config, ind, y );
     266             :     }
     267             : 
     268             :     /*-----------------------------------------------------------------*
     269             :      * Indexing
     270             :      *-----------------------------------------------------------------*/
     271             : 
     272       64559 :     if ( !Opt_AMR_WB )
     273             :     {
     274             :         /* EVS pulse indexing */
     275             : 
     276       64559 :         saved_bits = E_ACELP_indexing( code, config, NB_TRACK_FCB_4T, indexing_indices );
     277             : 
     278       64559 :         saved_bits = 0;
     279             : 
     280       64559 :         wordcnt = nbbits >> 4;
     281       64559 :         bitcnt = nbbits & 15;
     282      190731 :         for ( i = 0; i < wordcnt; i++ )
     283             :         {
     284      126172 :             push_indice( hBstr, IND_ALG_CDBK_4T64, indexing_indices[i], 16 );
     285             :         }
     286       64559 :         if ( bitcnt )
     287             :         {
     288       64559 :             push_indice( hBstr, IND_ALG_CDBK_4T64, indexing_indices[i], bitcnt );
     289             :         }
     290             :     }
     291             :     else
     292             :     {
     293             :         /* AMR-WB pulse indexing */
     294             : 
     295           0 :         if ( nbbits == 20 )
     296             :         {
     297           0 :             for ( track = 0; track < NB_TRACK_FCB_4T; track++ )
     298             :             {
     299           0 :                 k = track * NPMAXPT;
     300           0 :                 index = quant_1p_N1( ind[k], 4 );
     301           0 :                 push_indice( hBstr, IND_ALG_CDBK_4T64, index, 5 );
     302             :             }
     303             :         }
     304           0 :         else if ( nbbits == 36 )
     305             :         {
     306           0 :             for ( track = 0; track < NB_TRACK_FCB_4T; track++ )
     307             :             {
     308           0 :                 k = track * NPMAXPT;
     309           0 :                 index = quant_2p_2N1( ind[k], ind[k + 1], 4 );
     310           0 :                 push_indice( hBstr, IND_ALG_CDBK_4T64, index, 9 );
     311             :             }
     312             :         }
     313           0 :         else if ( nbbits == 44 )
     314             :         {
     315           0 :             for ( track = 0; track < ( NB_TRACK_FCB_4T - 2 ); track++ )
     316             :             {
     317           0 :                 k = track * NPMAXPT;
     318           0 :                 index = quant_3p_3N1( ind[k], ind[k + 1], ind[k + 2], 4 );
     319           0 :                 push_indice( hBstr, IND_ALG_CDBK_4T64, index, 13 );
     320             :             }
     321             : 
     322           0 :             for ( track = 2; track < NB_TRACK_FCB_4T; track++ )
     323             :             {
     324           0 :                 k = track * NPMAXPT;
     325           0 :                 index = quant_2p_2N1( ind[k], ind[k + 1], 4 );
     326           0 :                 push_indice( hBstr, IND_ALG_CDBK_4T64, index, 9 );
     327             :             }
     328             :         }
     329           0 :         else if ( nbbits == 52 )
     330             :         {
     331           0 :             for ( track = 0; track < NB_TRACK_FCB_4T; track++ )
     332             :             {
     333           0 :                 k = track * NPMAXPT;
     334           0 :                 index = quant_3p_3N1( ind[k], ind[k + 1], ind[k + 2], 4 );
     335           0 :                 push_indice( hBstr, IND_ALG_CDBK_4T64, index, 13 );
     336             :             }
     337             :         }
     338           0 :         else if ( nbbits == 64 )
     339             :         {
     340           0 :             for ( track = 0; track < NB_TRACK_FCB_4T; track++ )
     341             :             {
     342           0 :                 k = track * NPMAXPT;
     343           0 :                 L_index = quant_4p_4N( &ind[k], 4 );
     344           0 :                 index = ( ( L_index >> 14 ) & 3 );
     345           0 :                 push_indice( hBstr, IND_ALG_CDBK_4T64_1, index, 2 );
     346             :             }
     347             : 
     348           0 :             for ( track = 0; track < NB_TRACK_FCB_4T; track++ )
     349             :             {
     350           0 :                 k = track * NPMAXPT;
     351           0 :                 L_index = quant_4p_4N( &ind[k], 4 );
     352           0 :                 index = ( L_index & 0x3FFF );
     353           0 :                 push_indice( hBstr, IND_ALG_CDBK_4T64_2, index, 14 );
     354             :             }
     355             :         }
     356           0 :         else if ( nbbits == 72 )
     357             :         {
     358           0 :             for ( track = 0; track < ( NB_TRACK_FCB_4T - 2 ); track++ )
     359             :             {
     360           0 :                 k = track * NPMAXPT;
     361           0 :                 L_index = quant_5p_5N( &ind[k], 4 );
     362           0 :                 index = ( ( L_index >> 10 ) & 0x03FF );
     363           0 :                 push_indice( hBstr, IND_ALG_CDBK_4T64_1, index, 10 );
     364             :             }
     365             : 
     366           0 :             for ( track = 2; track < NB_TRACK_FCB_4T; track++ )
     367             :             {
     368           0 :                 k = track * NPMAXPT;
     369           0 :                 L_index = quant_4p_4N( &ind[k], 4 );
     370           0 :                 index = ( ( L_index >> 14 ) & 3 );
     371           0 :                 push_indice( hBstr, IND_ALG_CDBK_4T64_1, index, 2 );
     372             :             }
     373             : 
     374           0 :             for ( track = 0; track < ( NB_TRACK_FCB_4T - 2 ); track++ )
     375             :             {
     376           0 :                 k = track * NPMAXPT;
     377           0 :                 L_index = quant_5p_5N( &ind[k], 4 );
     378           0 :                 index = ( L_index & 0x03FF );
     379           0 :                 push_indice( hBstr, IND_ALG_CDBK_4T64_2, index, 10 );
     380             :             }
     381             : 
     382           0 :             for ( track = 2; track < NB_TRACK_FCB_4T; track++ )
     383             :             {
     384           0 :                 k = track * NPMAXPT;
     385           0 :                 L_index = quant_4p_4N( &ind[k], 4 );
     386           0 :                 index = ( L_index & 0x3FFF );
     387           0 :                 push_indice( hBstr, IND_ALG_CDBK_4T64_2, index, 14 );
     388             :             }
     389             :         }
     390           0 :         else if ( nbbits == 88 )
     391             :         {
     392           0 :             for ( track = 0; track < NB_TRACK_FCB_4T; track++ )
     393             :             {
     394           0 :                 k = track * NPMAXPT;
     395           0 :                 L_index = quant_6p_6N_2( &ind[k], 4 );
     396           0 :                 index = ( ( L_index >> 11 ) & 0x07FF );
     397           0 :                 push_indice( hBstr, IND_ALG_CDBK_4T64_1, index, 11 );
     398             :             }
     399             : 
     400           0 :             for ( track = 0; track < NB_TRACK_FCB_4T; track++ )
     401             :             {
     402           0 :                 k = track * NPMAXPT;
     403           0 :                 L_index = quant_6p_6N_2( &ind[k], 4 );
     404           0 :                 index = ( L_index & 0x07FF );
     405           0 :                 push_indice( hBstr, IND_ALG_CDBK_4T64_2, index, 11 );
     406             :             }
     407             :         }
     408             :     }
     409             : 
     410       64559 :     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           0 : 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           0 :     mask = ( ( 1 << N ) - 1 );
     427             : 
     428           0 :     index = ( pos & mask );
     429             : 
     430           0 :     if ( ( pos & NB_POS_FCB_4T ) != 0 )
     431             :     {
     432           0 :         index += 1 << N;
     433             :     }
     434             : 
     435           0 :     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       68221 : 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       68221 :     mask = ( ( 1 << N ) - 1 );
     453             : 
     454             :     /*-----------------------------------------------------------------*
     455             :      * sign of 1st pulse == sign of 2nd pulse
     456             :      *-----------------------------------------------------------------*/
     457             : 
     458       68221 :     if ( ( ( pos2 ^ pos1 ) & NB_POS_FCB_4T ) == 0 )
     459             :     {
     460       35487 :         if ( ( pos1 - pos2 ) <= 0 )
     461             :         {
     462       35329 :             index = ( ( pos1 & mask ) << N ) + ( pos2 & mask );
     463             :         }
     464             :         else
     465             :         {
     466         158 :             index = ( ( pos2 & mask ) << N ) + ( pos1 & mask );
     467             :         }
     468       35487 :         if ( ( pos1 & NB_POS_FCB_4T ) != 0 )
     469             :         {
     470       17668 :             index += 1 << ( 2 * N );
     471             :         }
     472             :     }
     473             :     else
     474             :     {
     475             : 
     476             :         /*-----------------------------------------------------------------*
     477             :          * sign of 1st pulse != sign of 2nd pulse
     478             :          *-----------------------------------------------------------------*/
     479       32734 :         if ( ( ( pos1 & mask ) - ( pos2 & mask ) ) <= 0 )
     480             :         {
     481       32507 :             index = ( ( pos2 & mask ) << N ) + ( pos1 & mask );
     482       32507 :             if ( ( pos2 & NB_POS_FCB_4T ) != 0 )
     483             :             {
     484       15768 :                 index += 1 << ( 2 * N );
     485             :             }
     486             :         }
     487             :         else
     488             :         {
     489         227 :             index = ( ( pos1 & mask ) << N ) + ( pos2 & mask );
     490         227 :             if ( ( pos1 & NB_POS_FCB_4T ) != 0 )
     491             :             {
     492         112 :                 index += 1 << ( 2 * N );
     493             :             }
     494             :         }
     495             :     }
     496             : 
     497       68221 :     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           0 : 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           0 :     nb_pos = ( 1 << ( N - 1 ) );
     515             : 
     516             :     /* Quantization of 3 pulses with 3*N+1 bits */
     517           0 :     if ( ( ( pos1 ^ pos2 ) & nb_pos ) == 0 )
     518             :     {
     519           0 :         index = quant_2p_2N1( pos1, pos2, ( N - 1 ) );
     520           0 :         index += ( pos1 & nb_pos ) << N;
     521           0 :         index += quant_1p_N1( pos3, N ) << ( 2 * N );
     522             :     }
     523           0 :     else if ( ( ( pos1 ^ pos3 ) & nb_pos ) == 0 )
     524             :     {
     525           0 :         index = quant_2p_2N1( pos1, pos3, ( N - 1 ) );
     526           0 :         index += ( pos1 & nb_pos ) << N;
     527           0 :         index += quant_1p_N1( pos2, N ) << ( 2 * N );
     528             :     }
     529             :     else
     530             :     {
     531           0 :         index = quant_2p_2N1( pos2, pos3, ( N - 1 ) );
     532           0 :         index += ( pos2 & nb_pos ) << N;
     533           0 :         index += quant_1p_N1( pos1, N ) << ( 2 * N );
     534             :     }
     535             : 
     536           0 :     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           0 : 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           0 :     nb_pos = ( 1 << ( N - 1 ) );
     555             : 
     556             :     /* Quantization of 4 pulses with 4*N+1 bits */
     557           0 :     if ( ( ( pos1 ^ pos2 ) & nb_pos ) == 0 )
     558             :     {
     559           0 :         index = quant_2p_2N1( pos1, pos2, ( N - 1 ) );
     560           0 :         index += ( pos1 & nb_pos ) << N;
     561           0 :         index += quant_2p_2N1( pos3, pos4, N ) << ( 2 * N );
     562             :     }
     563           0 :     else if ( ( ( pos1 ^ pos3 ) & nb_pos ) == 0 )
     564             :     {
     565           0 :         index = quant_2p_2N1( pos1, pos3, ( N - 1 ) );
     566           0 :         index += ( pos1 & nb_pos ) << N;
     567           0 :         index += quant_2p_2N1( pos2, pos4, N ) << ( 2 * N );
     568             :     }
     569             :     else
     570             :     {
     571           0 :         index = quant_2p_2N1( pos2, pos3, ( N - 1 ) );
     572           0 :         index += ( pos2 & nb_pos ) << N;
     573           0 :         index += quant_2p_2N1( pos1, pos4, N ) << ( 2 * N );
     574             :     }
     575             : 
     576           0 :     return ( index );
     577             : }
     578             : 
     579             : /*---------------------------------------------------------------------*
     580             :  * Quantization of 4 pulses with 4*N bits:                             *
     581             :  *---------------------------------------------------------------------*/
     582             : 
     583             : /*! r: return 4*N bits */
     584           0 : 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           0 :     int32_t nb_pos, index = 0;
     592             : 
     593           0 :     n_1 = N - 1;
     594           0 :     nb_pos = ( 1 << n_1 );
     595             : 
     596           0 :     i = 0;
     597           0 :     j = 0;
     598           0 :     for ( k = 0; k < 4; k++ )
     599             :     {
     600           0 :         if ( ( pos[k] & nb_pos ) == 0 )
     601             :         {
     602           0 :             posA[i++] = pos[k];
     603             :         }
     604             :         else
     605             :         {
     606           0 :             posB[j++] = pos[k];
     607             :         }
     608             :     }
     609             : 
     610           0 :     switch ( i )
     611             :     {
     612           0 :         case 0:
     613           0 :             index = 1 << ( ( 4 * N ) - 3 );
     614           0 :             index += quant_4p_4N1( posB[0], posB[1], posB[2], posB[3], n_1 );
     615           0 :             break;
     616           0 :         case 1:
     617           0 :             index = quant_1p_N1( posA[0], n_1 ) << ( ( 3 * n_1 ) + 1 );
     618           0 :             index += quant_3p_3N1( posB[0], posB[1], posB[2], n_1 );
     619           0 :             break;
     620           0 :         case 2:
     621           0 :             index = quant_2p_2N1( posA[0], posA[1], n_1 ) << ( ( 2 * n_1 ) + 1 );
     622           0 :             index += quant_2p_2N1( posB[0], posB[1], n_1 );
     623           0 :             break;
     624           0 :         case 3:
     625           0 :             index = quant_3p_3N1( posA[0], posA[1], posA[2], n_1 ) << N;
     626           0 :             index += quant_1p_N1( posB[0], n_1 );
     627           0 :             break;
     628           0 :         case 4:
     629           0 :             index = quant_4p_4N1( posA[0], posA[1], posA[2], posA[3], n_1 );
     630           0 :             break;
     631             :     }
     632           0 :     index += ( i & 3 ) << ( ( 4 * N ) - 2 );
     633             : 
     634           0 :     return ( index );
     635             : }
     636             : 
     637             : /*---------------------------------------------------------------------*
     638             :  * Quantization of 5 pulses with 5*N bits:                             *
     639             :  *---------------------------------------------------------------------*/
     640             : 
     641             : /*! r: return 5*N bits */
     642           0 : 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           0 :     int32_t index = 0;
     650             : 
     651           0 :     n_1 = N - 1;
     652           0 :     nb_pos = ( 1 << n_1 );
     653             : 
     654           0 :     i = 0;
     655           0 :     j = 0;
     656           0 :     for ( k = 0; k < 5; k++ )
     657             :     {
     658           0 :         if ( ( pos[k] & nb_pos ) == 0 )
     659             :         {
     660           0 :             posA[i++] = pos[k];
     661             :         }
     662             :         else
     663             :         {
     664           0 :             posB[j++] = pos[k];
     665             :         }
     666             :     }
     667           0 :     switch ( i )
     668             :     {
     669           0 :         case 0:
     670           0 :             index = 1 << ( ( 5 * N ) - 1 );
     671           0 :             index += quant_3p_3N1( posB[0], posB[1], posB[2], n_1 ) << ( ( 2 * N ) + 1 );
     672           0 :             index += quant_2p_2N1( posB[3], posB[4], N );
     673           0 :             break;
     674           0 :         case 1:
     675           0 :             index = 1 << ( ( 5 * N ) - 1 );
     676           0 :             index += quant_3p_3N1( posB[0], posB[1], posB[2], n_1 ) << ( ( 2 * N ) + 1 );
     677           0 :             index += quant_2p_2N1( posB[3], posA[0], N );
     678           0 :             break;
     679           0 :         case 2:
     680           0 :             index = 1 << ( ( 5 * N ) - 1 );
     681           0 :             index += quant_3p_3N1( posB[0], posB[1], posB[2], n_1 ) << ( ( 2 * N ) + 1 );
     682           0 :             index += quant_2p_2N1( posA[0], posA[1], N );
     683           0 :             break;
     684           0 :         case 3:
     685           0 :             index = quant_3p_3N1( posA[0], posA[1], posA[2], n_1 ) << ( ( 2 * N ) + 1 );
     686           0 :             index += quant_2p_2N1( posB[0], posB[1], N );
     687           0 :             break;
     688           0 :         case 4:
     689           0 :             index = quant_3p_3N1( posA[0], posA[1], posA[2], n_1 ) << ( ( 2 * N ) + 1 );
     690           0 :             index += quant_2p_2N1( posA[3], posB[0], N );
     691           0 :             break;
     692           0 :         case 5:
     693           0 :             index = quant_3p_3N1( posA[0], posA[1], posA[2], n_1 ) << ( ( 2 * N ) + 1 );
     694           0 :             index += quant_2p_2N1( posA[3], posA[4], N );
     695           0 :             break;
     696             :     }
     697             : 
     698           0 :     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           0 : 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           0 :     int32_t nb_pos, index = 0;
     714             : 
     715           0 :     n_1 = N - 1;
     716           0 :     nb_pos = 1 << n_1;
     717             : 
     718           0 :     i = 0;
     719           0 :     j = 0;
     720           0 :     for ( k = 0; k < 6; k++ )
     721             :     {
     722           0 :         if ( ( pos[k] & nb_pos ) == 0 )
     723             :         {
     724           0 :             posA[i++] = pos[k];
     725             :         }
     726             :         else
     727             :         {
     728           0 :             posB[j++] = pos[k];
     729             :         }
     730             :     }
     731           0 :     switch ( i )
     732             :     {
     733           0 :         case 0:
     734           0 :             index = 1 << ( ( 6 * N ) - 5 );
     735           0 :             index += quant_5p_5N( posB, n_1 ) << N;
     736           0 :             index += quant_1p_N1( posB[5], n_1 );
     737           0 :             break;
     738           0 :         case 1:
     739           0 :             index = 1 << ( ( 6 * N ) - 5 );
     740           0 :             index += quant_5p_5N( posB, n_1 ) << N;
     741           0 :             index += quant_1p_N1( posA[0], n_1 );
     742           0 :             break;
     743           0 :         case 2:
     744           0 :             index = 1 << ( ( 6 * N ) - 5 );
     745           0 :             index += quant_4p_4N( posB, n_1 ) << ( ( 2 * n_1 ) + 1 );
     746           0 :             index += quant_2p_2N1( posA[0], posA[1], n_1 );
     747           0 :             break;
     748           0 :         case 3:
     749           0 :             index = quant_3p_3N1( posA[0], posA[1], posA[2], n_1 ) << ( ( 3 * n_1 ) + 1 );
     750           0 :             index += quant_3p_3N1( posB[0], posB[1], posB[2], n_1 );
     751           0 :             break;
     752           0 :         case 4:
     753           0 :             i = 2;
     754           0 :             index = quant_4p_4N( posA, n_1 ) << ( ( 2 * n_1 ) + 1 );
     755           0 :             index += quant_2p_2N1( posB[0], posB[1], n_1 );
     756           0 :             break;
     757           0 :         case 5:
     758           0 :             i = 1;
     759           0 :             index = quant_5p_5N( posA, n_1 ) << N;
     760           0 :             index += quant_1p_N1( posB[0], n_1 );
     761           0 :             break;
     762           0 :         case 6:
     763           0 :             i = 0;
     764           0 :             index = quant_5p_5N( posA, n_1 ) << N;
     765           0 :             index += quant_1p_N1( posA[5], n_1 );
     766           0 :             break;
     767             :     }
     768             : 
     769           0 :     index += ( i & 3 ) << ( ( 6 * N ) - 4 );
     770             : 
     771           0 :     return ( index );
     772             : }
     773             : 
     774             : /*---------------------------------------------------------------------*
     775             :  *order the pulse position                                             *
     776             :  *---------------------------------------------------------------------*/
     777             : 
     778             : /*! r: return sign value of pulse on a track */
     779       67296 : 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       67296 :     sign = 0;
     790       67296 :     j = 0;
     791     1144032 :     for ( k = 0; k < 64; k += 4 )
     792             :     {
     793     1076736 :         if ( v[k] )
     794             :         {
     795      194286 :             pos_vector[j] = k >> 2;
     796      194286 :             pos_vector_num[j] = (int16_t) fabsf( v[k] );
     797      194286 :             if ( v[k] > 0 )
     798             :             {
     799       97733 :                 sign = sign << 1;
     800             :             }
     801             :             else
     802             :             {
     803       96553 :                 sign = ( sign << 1 ) + 1;
     804             :             }
     805      194286 :             j++;
     806             :         }
     807             :     }
     808       67296 :     *pulse_pos_num = j;
     809             : 
     810       67296 :     return sign;
     811             : }
     812             : 
     813             : /*---------------------------------------------------------------------*
     814             :  *encode the position                                                  *
     815             :  *---------------------------------------------------------------------*/
     816             : 
     817             : /*! r: return index of the positions which have pulse */
     818       67296 : 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       67296 :     mmm1 = PI_select_table[n][pos_num] - 1;
     829       67296 :     temp2 = pos_num;
     830             : 
     831       67296 :     if ( flag ) /* no decrease */
     832             :     {
     833      261582 :         for ( i = 0; i < pos_num; i++ )
     834             :         {
     835      194286 :             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       67296 :     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        7215 : 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        7215 :     temp1 = pos_num + pulse_num - 1;
     865        7215 :     temp2 = pulse_num;
     866        7215 :     k = PI_select_table[temp1][pulse_num] - 1;
     867        7215 :     temp1--;
     868       14817 :     for ( i = 0; i < pulse_num; i++ )
     869             :     {
     870        7602 :         k -= PI_select_table[temp1 - buffer[i]][temp2--];
     871        7602 :         temp1--;
     872             :     }
     873             : 
     874        7215 :     return k;
     875             : }
     876             : 
     877             : /*---------------------------------------------------------------------*
     878             :  *encode the class and compute class offset                            *
     879             :  *---------------------------------------------------------------------*/
     880             : 
     881             : /*! r: class offset */
     882       67296 : 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       67296 :     mn9_offet = 0;
     894             : 
     895       67296 :     if ( pulse_pos_num < pulse_num )
     896             :     {
     897        7215 :         vector_class_ptr = vector_class;
     898       21258 :         for ( i = 0; i < pulse_pos_num; i++ )
     899             :         {
     900       21645 :             for ( j = 0; j < ( sector_6p_num[i] - 1 ); j++ )
     901             :             {
     902        7602 :                 *vector_class_ptr++ = i;
     903             :             }
     904             :         }
     905        7215 :         k = fcb_encode_cl( vector_class, pulse_num - pulse_pos_num, pulse_pos_num );
     906        7215 :         mn9_offet = PI_factor[pulse_pos_num] * k;
     907             :     }
     908             : 
     909       67296 :     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       67296 : 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       67296 :     sign = pre_process( v, vector_p, vector_p_num, &pulse_pos_num );
     931             : 
     932             :     /*encode the position*/
     933       67296 :     code_index = fcb_encode_position( vector_p, 16, pulse_pos_num, 1 );
     934             : 
     935             :     /*encode the class and compute class offset*/
     936       67296 :     code_index += fcb_encode_class( vector_p_num, pulse_num, pulse_pos_num );
     937             : 
     938       67296 :     code_index = PI_offset[pulse_num][pulse_num + 1 - pulse_pos_num] + ( code_index << pulse_pos_num ) + sign;
     939             : 
     940       67296 :     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       33648 : 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       33648 :     int32_t joint_offset = 3611648; /*offset for 3 pulses per track*/
     967       33648 :     int16_t saved_bits = 0;
     968             : 
     969      100944 :     for ( track = 0; track < 2; track++ )
     970             :     {
     971       67296 :         k = track * NPMAXPT;
     972       67296 :         ps[track] = fcb_encode_PI( code + track, 3 );
     973       67296 :         p[track] = 3;
     974             :     }
     975             : 
     976      100944 :     for ( track = 2; track < NB_TRACK_FCB_4T; track++ )
     977             :     {
     978       67296 :         j = track * NPMAXPT;
     979     1123516 :         for ( k = track; k < 64; k += 4 )
     980             :         {
     981     1058861 :             if ( code[k] )
     982             :             {
     983      131951 :                 tmp = k >> 2;
     984      131951 :                 if ( code[k] < 0 )
     985             :                 {
     986       65762 :                     tmp += 16;
     987             :                 }
     988      131951 :                 if ( fabs( code[k] ) > 1 )
     989             :                 {
     990        2641 :                     ind[j] = tmp;
     991        2641 :                     ind[j + 1] = tmp;
     992        2641 :                     break;
     993             :                 }
     994             :                 else
     995             :                 {
     996      129310 :                     ind[j] = tmp;
     997      129310 :                     j++;
     998             :                 }
     999             :             }
    1000             :         }
    1001       67296 :         k = track * NPMAXPT;
    1002       67296 :         ps[track] = quant_2p_2N1( ind[k], ind[k + 1], 4 );
    1003       67296 :         p[track] = 2;
    1004             :     }
    1005       33648 :     joint_index = ps[0] * 5472 + ps[1];
    1006       33648 :     if ( joint_index >= joint_offset )
    1007             :     {
    1008       28053 :         joint_index += joint_offset;
    1009             :     }
    1010             :     else
    1011             :     {
    1012        5595 :         saved_bits += 1;
    1013             :     }
    1014             : 
    1015       33648 :     idxs[0] = ( ( ps[2] << 9 ) + ps[3] ) & 0xffff;
    1016       33648 :     idxs[1] = ( ( joint_index << 2 ) + ( ps[2] >> 7 ) ) & 0xffff;
    1017       33648 :     idxs[2] = (uint16_t) ( joint_index >> 14 );
    1018             : 
    1019       33648 :     return saved_bits;
    1020             : }

Generated by: LCOV version 1.14