LCOV - code coverage report
Current view: top level - lib_enc - cod2t32.c (source / functions) Hit Total Coverage
Test: Coverage on main -- short test vectors @ 6c9ddc4024a9c0e1ecb8f643f114a84a0e26ec6b Lines: 116 117 99.1 %
Date: 2025-05-23 08:37:30 Functions: 2 2 100.0 %

          Line data    Source code
       1             : /******************************************************************************************************
       2             : 
       3             :    (C) 2022-2025 IVAS codec Public Collaboration with portions copyright Dolby International AB, Ericsson AB,
       4             :    Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
       5             :    Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
       6             :    Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
       7             :    contributors to this repository. All Rights Reserved.
       8             : 
       9             :    This software is protected by copyright law and by international treaties.
      10             :    The IVAS codec Public Collaboration consisting of Dolby International AB, Ericsson AB,
      11             :    Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
      12             :    Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
      13             :    Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
      14             :    contributors to this repository retain full ownership rights in their respective contributions in
      15             :    the software. This notice grants no license of any kind, including but not limited to patent
      16             :    license, nor is any license granted by implication, estoppel or otherwise.
      17             : 
      18             :    Contributors are required to enter into the IVAS codec Public Collaboration agreement before making
      19             :    contributions.
      20             : 
      21             :    This software is provided "AS IS", without any express or implied warranties. The software is in the
      22             :    development stage. It is intended exclusively for experts who have experience with such software and
      23             :    solely for the purpose of inspection. All implied warranties of non-infringement, merchantability
      24             :    and fitness for a particular purpose are hereby disclaimed and excluded.
      25             : 
      26             :    Any dispute, controversy or claim arising under or in relation to providing this software shall be
      27             :    submitted to and settled by the final, binding jurisdiction of the courts of Munich, Germany in
      28             :    accordance with the laws of the Federal Republic of Germany excluding its conflict of law rules and
      29             :    the United Nations Convention on Contracts on the International Sales of Goods.
      30             : 
      31             : *******************************************************************************************************/
      32             : 
      33             : /*====================================================================================
      34             :     EVS Codec 3GPP TS26.443 Nov 04, 2021. Version 12.14.0 / 13.10.0 / 14.6.0 / 15.4.0 / 16.3.0
      35             :   ====================================================================================*/
      36             : 
      37             : #include <stdint.h>
      38             : #include "options.h"
      39             : #ifdef DEBUGGING
      40             : #include "debug.h"
      41             : #endif
      42             : #include "cnst.h"
      43             : #include "prot.h"
      44             : #include "wmc_auto.h"
      45             : 
      46             : /*---------------------------------------------------------------------*
      47             :  * Local constants
      48             :  *---------------------------------------------------------------------*/
      49             : 
      50             : #define STEP  2
      51             : #define MSIZE 1024
      52             : 
      53             : 
      54             : /*----------------------------------------------------------------------------------
      55             :  * Function  acelp_2t32()
      56             :  *
      57             :  * 12 bits algebraic codebook.
      58             :  * 2 tracks x 32 positions per track = 64 samples.
      59             :  *
      60             :  * 12 bits --> 2 pulses in a frame of 64 samples.
      61             :  *
      62             :  * All pulses can have two (2) possible amplitudes: +1 or -1.
      63             :  * Each pulse can have 32 possible positions.
      64             :  *----------------------------------------------------------------------------------*/
      65             : 
      66       15097 : void acelp_2t32(
      67             :     BSTR_ENC_HANDLE hBstr, /* i/o: encoder bitstream handle                      */
      68             :     const float dn[],      /* i  : corr. between target and h[].                 */
      69             :     const float h[],       /* i  : impulse response of weighted synthesis filter */
      70             :     float code[],          /* o  : algebraic (fixed) codebook excitation         */
      71             :     float y[]              /* o  : filtered fixed codebook excitation            */
      72             : )
      73             : {
      74             :     int16_t i, j, k, i0, i1, ix, iy, pos, pos2, index;
      75             :     float psk, ps1, ps2, alpk, alp1, alp2, sq;
      76             :     float pol[L_SUBFR], dn_p[L_SUBFR], r0;
      77             :     int16_t ii, jj;
      78             :     float s, cor, sign0, sign1;
      79             :     float *p0, *p1, *p2;
      80             :     const float *ptr_h1, *ptr_h2, *ptr_hf;
      81             :     float rrixix[NB_TRACK_FCB_2T][NB_POS_FCB_2T];
      82             :     float rrixiy[MSIZE];
      83             : 
      84             : 
      85             :     /*----------------------------------------------------------------*
      86             :      * Compute rrixix[][] needed for the codebook search.
      87             :      *----------------------------------------------------------------*/
      88             : 
      89             :     /* Init pointers to last position of rrixix[] */
      90       15097 :     p0 = &rrixix[0][NB_POS_FCB_2T - 1];
      91       15097 :     p1 = &rrixix[1][NB_POS_FCB_2T - 1];
      92             : 
      93       15097 :     ptr_h1 = h;
      94       15097 :     cor = 0.0f;
      95      498201 :     for ( i = 0; i < NB_POS_FCB_2T; i++ )
      96             :     {
      97      483104 :         cor += *ptr_h1 * *ptr_h1;
      98      483104 :         ptr_h1++;
      99      483104 :         *p1-- = cor;
     100      483104 :         cor += *ptr_h1 * *ptr_h1;
     101      483104 :         ptr_h1++;
     102      483104 :         *p0-- = cor;
     103             :     }
     104             : 
     105       15097 :     p0 = rrixix[0];
     106       15097 :     p1 = rrixix[1];
     107             : 
     108      498201 :     for ( i = 0; i < NB_POS_FCB_2T; i++ )
     109             :     {
     110      483104 :         *p0 = 0.5f * ( *p0 );
     111      483104 :         p0++;
     112      483104 :         *p1 = 0.5f * ( *p1 );
     113      483104 :         p1++;
     114             :     }
     115             : 
     116             :     /*------------------------------------------------------------*
     117             :      * Compute rrixiy[][] needed for the codebook search.
     118             :      *------------------------------------------------------------*/
     119             : 
     120       15097 :     pos = MSIZE - 1;
     121       15097 :     pos2 = MSIZE - 2;
     122       15097 :     ptr_hf = h + 1;
     123             : 
     124      498201 :     for ( k = 0; k < NB_POS_FCB_2T; k++ )
     125             :     {
     126             :         /* Init pointers to last position of diagonals */
     127      483104 :         p1 = &rrixiy[pos];
     128      483104 :         p0 = &rrixiy[pos2];
     129             : 
     130      483104 :         cor = 0.0f;
     131      483104 :         ptr_h1 = h;
     132      483104 :         ptr_h2 = ptr_hf;
     133             : 
     134     7971216 :         for ( i = k + 1; i < NB_POS_FCB_2T; i++ )
     135             :         {
     136     7488112 :             cor += *ptr_h1++ * *ptr_h2++;
     137     7488112 :             *p1 = cor;
     138             : 
     139     7488112 :             cor += *ptr_h1++ * *ptr_h2++;
     140     7488112 :             *p0 = cor;
     141             : 
     142     7488112 :             p1 -= ( NB_POS_FCB_2T + 1 );
     143     7488112 :             p0 -= ( NB_POS_FCB_2T + 1 );
     144             :         }
     145             : 
     146      483104 :         cor += *ptr_h1++ * *ptr_h2;
     147      483104 :         *p1 = cor;
     148             : 
     149      483104 :         pos -= NB_POS_FCB_2T;
     150      483104 :         pos2--;
     151      483104 :         ptr_hf += STEP;
     152             :     }
     153             : 
     154             :     /*----------------------------------------------------------------*
     155             :      * computing reference vector and pre-selection of polarities
     156             :      *----------------------------------------------------------------*/
     157             : 
     158      981305 :     for ( i = 0; i < L_SUBFR; i++ )
     159             :     {
     160             :         /* FIR high-pass filtering */
     161      966208 :         if ( i == 0 )
     162             :         {
     163       15097 :             r0 = dn[i] - dn[i + 1] * 0.35f;
     164             :         }
     165      951111 :         else if ( i == L_SUBFR - 1 )
     166             :         {
     167       15097 :             r0 = -dn[i - 1] * 0.35f + dn[i];
     168             :         }
     169             :         else
     170             :         {
     171      936014 :             r0 = -dn[i - 1] * 0.35f + dn[i] - dn[i + 1] * 0.35f;
     172             :         }
     173             : 
     174             :         /* pre-selection of polarities */
     175      966208 :         if ( r0 >= 0.0f )
     176             :         {
     177      484724 :             pol[i] = 1.0f;
     178             :         }
     179             :         else
     180             :         {
     181      481484 :             pol[i] = -1.0f;
     182             :         }
     183             : 
     184             :         /* including polarities into dn[] */
     185      966208 :         dn_p[i] = dn[i] * pol[i];
     186             :     }
     187             : 
     188             :     /*----------------------------------------------------------------*
     189             :      * compute denominator ( multiplied by polarity )
     190             :      *----------------------------------------------------------------*/
     191             : 
     192       15097 :     k = 0;
     193       15097 :     ii = 0;
     194      498201 :     for ( i = 0; i < NB_POS_FCB_2T; i++ )
     195             :     {
     196      483104 :         jj = 1;
     197    15942432 :         for ( j = 0; j < NB_POS_FCB_2T; j++ )
     198             :         {
     199    15459328 :             rrixiy[k + j] *= pol[ii] * pol[jj];
     200    15459328 :             jj += 2;
     201             :         }
     202      483104 :         ii += 2;
     203      483104 :         k += NB_POS_FCB_2T;
     204             :     }
     205             : 
     206             :     /*----------------------------------------------------------------*
     207             :      * search 2 pulses
     208             :      * All combinaisons are tested:
     209             :      * 32 pos x 32 pos x 2 signs = 2048 tests
     210             :      *----------------------------------------------------------------*/
     211             : 
     212       15097 :     p0 = rrixix[0];
     213       15097 :     p1 = rrixix[1];
     214       15097 :     p2 = rrixiy;
     215             : 
     216       15097 :     psk = -1;
     217       15097 :     alpk = 1;
     218       15097 :     ix = 0;
     219       15097 :     iy = 1;
     220      498201 :     for ( i0 = 0; i0 < L_SUBFR; i0 += STEP )
     221             :     {
     222      483104 :         ps1 = dn_p[i0];
     223      483104 :         alp1 = *p0++;
     224      483104 :         pos = -1;
     225    15942432 :         for ( i1 = 1; i1 < L_SUBFR; i1 += STEP )
     226             :         {
     227    15459328 :             ps2 = ps1 + dn_p[i1];
     228    15459328 :             alp2 = alp1 + *p1++ + *p2++;
     229    15459328 :             sq = ps2 * ps2;
     230    15459328 :             s = alpk * sq - psk * alp2;
     231    15459328 :             if ( s > 0 )
     232             :             {
     233      199657 :                 psk = sq;
     234      199657 :                 alpk = alp2;
     235      199657 :                 pos = i1;
     236             :             }
     237             :         }
     238      483104 :         p1 -= NB_POS_FCB_2T;
     239      483104 :         if ( pos >= 0 )
     240             :         {
     241       82107 :             ix = i0;
     242       82107 :             iy = pos;
     243             :         }
     244             :     }
     245             : 
     246       15097 :     i0 = ix / STEP;
     247       15097 :     i1 = iy / STEP;
     248       15097 :     sign0 = pol[ix];
     249       15097 :     sign1 = pol[iy];
     250             : 
     251             : 
     252             :     /*-------------------------------------------------------------------*
     253             :      * Build the codeword, the filtered codeword and index of codevector.
     254             :      *-------------------------------------------------------------------*/
     255             : 
     256       15097 :     set_f( code, 0.0f, L_SUBFR );
     257             : 
     258       15097 :     code[ix] = sign0;
     259       15097 :     code[iy] = sign1;
     260       15097 :     index = ( i0 << 6 ) + i1;
     261             : 
     262       15097 :     if ( sign0 < 0.0f )
     263             :     {
     264        7598 :         index += 0x800;
     265             :     }
     266             : 
     267       15097 :     if ( sign1 < 0.0f )
     268             :     {
     269        7424 :         index += 0x20;
     270             :     }
     271             : 
     272       15097 :     set_f( y, 0.0f, L_SUBFR );
     273      440925 :     for ( i = ix; i < L_SUBFR; i++ )
     274             :     {
     275      425828 :         y[i] = ( sign0 * h[i - ix] );
     276             :     }
     277             : 
     278      394890 :     for ( i = iy; i < L_SUBFR; i++ )
     279             :     {
     280      379793 :         y[i] += ( sign1 * h[i - iy] );
     281             :     }
     282             : 
     283             :     /* write index to array of indices */
     284       15097 :     push_indice( hBstr, IND_ALG_CDBK_2T32, index, 12 );
     285             : 
     286       15097 :     return;
     287             : }
     288             : 
     289             : /*----------------------------------------------------------------------------------
     290             :  * acelp_1t64()
     291             :  *
     292             :  * 7 bits algebraic codebook.
     293             :  * 1 track x 64 positions per track = 64 samples.
     294             :  *
     295             :  * The pulse can have 64 possible positions and two (2) possible amplitudes: +1 or -1.
     296             :  *----------------------------------------------------------------------------------*/
     297             : 
     298         382 : void acelp_1t64(
     299             :     BSTR_ENC_HANDLE hBstr, /* i/o: encoder bitstream handle                      */
     300             :     const float dn[],      /* i  : corr. between target and h[].                 */
     301             :     const float h[],       /* i  : impulse response of weighted synthesis filter */
     302             :     float code[],          /* o  : algebraic (fixed) codebook excitation         */
     303             :     float y[],             /* o  : filtered fixed codebook excitation            */
     304             :     const int16_t L_subfr  /* i  : subframe length                               */
     305             : )
     306             : {
     307             :     int16_t i, pos, sgn, index;
     308             :     float tmp;
     309             : 
     310             :     /*-------------------------------------------------------------------*
     311             :      * Find position and sign of maximum impulse.
     312             :      *-------------------------------------------------------------------*/
     313             : 
     314         382 :     pos = emaximum( dn, L_subfr, &tmp );
     315         382 :     sgn = (int16_t) sign( dn[pos] );
     316             : 
     317             :     /*-------------------------------------------------------------------*
     318             :      * Build the codeword, the filtered codeword and index of codevector.
     319             :      *-------------------------------------------------------------------*/
     320             : 
     321         382 :     set_f( code, 0.0f, L_subfr );
     322         382 :     code[pos] = sgn;
     323             : 
     324         382 :     set_f( y, 0.0f, L_subfr );
     325             : 
     326       17076 :     for ( i = pos; i < L_subfr; i++ )
     327             :     {
     328       16694 :         y[i] = ( sgn * h[i - pos] );
     329             :     }
     330             : 
     331         382 :     index = pos;
     332             : 
     333         382 :     if ( sgn > 0 )
     334             :     {
     335         168 :         index += L_subfr;
     336             :     }
     337             : 
     338         382 :     if ( L_subfr == L_SUBFR )
     339             :     {
     340         382 :         push_indice( hBstr, IND_ALG_CDBK_1T64, index, 7 );
     341             :     }
     342             :     else /* L_subfr == L_SUBFR */
     343             :     {
     344           0 :         push_indice( hBstr, IND_ALG_CDBK_1T64, index, 8 );
     345             :     }
     346             : 
     347         382 :     return;
     348             : }

Generated by: LCOV version 1.14