LCOV - code coverage report
Current view: top level - lib_enc - arith_coder_enc.c (source / functions) Hit Total Coverage
Test: Coverage on main @ 6baab0c613aa6c7100498ed7b93676aa8198a493 Lines: 223 226 98.7 %
Date: 2025-05-28 04:28:20 Functions: 7 7 100.0 %

          Line data    Source code
       1             : /******************************************************************************************************
       2             : 
       3             :    (C) 2022-2025 IVAS codec Public Collaboration with portions copyright Dolby International AB, Ericsson AB,
       4             :    Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
       5             :    Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
       6             :    Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
       7             :    contributors to this repository. All Rights Reserved.
       8             : 
       9             :    This software is protected by copyright law and by international treaties.
      10             :    The IVAS codec Public Collaboration consisting of Dolby International AB, Ericsson AB,
      11             :    Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
      12             :    Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
      13             :    Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
      14             :    contributors to this repository retain full ownership rights in their respective contributions in
      15             :    the software. This notice grants no license of any kind, including but not limited to patent
      16             :    license, nor is any license granted by implication, estoppel or otherwise.
      17             : 
      18             :    Contributors are required to enter into the IVAS codec Public Collaboration agreement before making
      19             :    contributions.
      20             : 
      21             :    This software is provided "AS IS", without any express or implied warranties. The software is in the
      22             :    development stage. It is intended exclusively for experts who have experience with such software and
      23             :    solely for the purpose of inspection. All implied warranties of non-infringement, merchantability
      24             :    and fitness for a particular purpose are hereby disclaimed and excluded.
      25             : 
      26             :    Any dispute, controversy or claim arising under or in relation to providing this software shall be
      27             :    submitted to and settled by the final, binding jurisdiction of the courts of Munich, Germany in
      28             :    accordance with the laws of the Federal Republic of Germany excluding its conflict of law rules and
      29             :    the United Nations Convention on Contracts on the International Sales of Goods.
      30             : 
      31             : *******************************************************************************************************/
      32             : 
      33             : /*====================================================================================
      34             :     EVS Codec 3GPP TS26.443 Nov 04, 2021. Version 12.14.0 / 13.10.0 / 14.6.0 / 15.4.0 / 16.3.0
      35             :   ====================================================================================*/
      36             : 
      37             : #include <assert.h>
      38             : #include <stdint.h>
      39             : #include "options.h"
      40             : #ifdef DEBUGGING
      41             : #include "debug.h"
      42             : #endif
      43             : #include <math.h>
      44             : #include "cnst.h"
      45             : #include "prot.h"
      46             : #include "rom_com.h"
      47             : #include "basop_util.h"
      48             : #include "basop_proto_func.h"
      49             : #include "wmc_auto.h"
      50             : 
      51             : /*-------------------------------------------------------------------*
      52             :  * Local constants
      53             :  *-------------------------------------------------------------------*/
      54             : 
      55             : #define kMaxNumHeapElems 10
      56             : 
      57             : typedef struct HeapElem
      58             : {
      59             :     float mScore;   /* Sort key                    */
      60             :     int16_t mIndex; /* Original index              */
      61             : } HeapElem;
      62             : 
      63             : typedef struct Heap
      64             : {
      65             :     HeapElem mElem[2 * kMaxNumHeapElems + 1];
      66             : } Heap;
      67             : 
      68             : 
      69             : /*-------------------------------------------------------------------*
      70             :  * tcx_arith_estimate_scale()
      71             :  *
      72             :  *
      73             :  *-------------------------------------------------------------------*/
      74             : 
      75             : /*! r: estimated SQ scale */
      76      263941 : static float tcx_arith_estimate_scale(
      77             :     const float abs_spectrum[], /* i  : absolute MDCT coefficients    */
      78             :     const int16_t L_frame,      /* i  : number of spectral lines      */
      79             :     const Word16 envelope[],    /* i  : scaled envelope (Q15-e)       */
      80             :     const Word16 envelope_e     /* i  : scaled envelope exponent (Q0) */
      81             : )
      82             : {
      83             :     float scale, tmp;
      84             :     int16_t k;
      85             : 
      86             :     /* compute normalised standard deviation and determine approximate scale */
      87      263941 :     scale = 0.01f;
      88   157699101 :     for ( k = 0; k < L_frame; k++ )
      89             :     {
      90   157435160 :         tmp = abs_spectrum[k] * envelope[k];
      91   157435160 :         scale += tmp * tmp;
      92             :     }
      93      263941 :     tmp = (float) ( 1 << ( 15 - envelope_e ) );
      94      263941 :     scale = (float) sqrt( ( L_frame * tmp * tmp * 4.0f ) / scale );
      95             : 
      96             : 
      97      263941 :     return scale;
      98             : }
      99             : 
     100             : 
     101             : /*-------------------------------------------------------------------*
     102             :  * MinHeapify_i()
     103             :  *
     104             :  *
     105             :  *-------------------------------------------------------------------*/
     106             : 
     107    11306527 : static void MinHeapify_i(
     108             :     Heap *H,
     109             :     int16_t i )
     110             : {
     111             :     int16_t left, right, largest;
     112             :     HeapElem T;
     113             : 
     114    11306527 :     left = 2 * i + 1;
     115    11306527 :     right = left + 1;
     116    11306527 :     largest = i;
     117             : 
     118    11306527 :     if ( H->mElem[left].mScore < H->mElem[largest].mScore )
     119             :     {
     120     9830280 :         largest = left;
     121             :     }
     122    11306527 :     if ( H->mElem[right].mScore < H->mElem[largest].mScore )
     123             :     {
     124     3635547 :         largest = right;
     125             :     }
     126    34068567 :     while ( largest != i )
     127             :     {
     128    22762040 :         T.mIndex = H->mElem[i].mIndex;
     129    22762040 :         T.mScore = H->mElem[i].mScore;
     130             : 
     131    22762040 :         H->mElem[i].mIndex = H->mElem[largest].mIndex;
     132    22762040 :         H->mElem[i].mScore = H->mElem[largest].mScore;
     133             : 
     134    22762040 :         H->mElem[largest].mIndex = T.mIndex;
     135    22762040 :         H->mElem[largest].mScore = T.mScore;
     136             : 
     137    22762040 :         i = largest;
     138             : 
     139    22762040 :         left = 2 * i + 1;
     140    22762040 :         right = left + 1;
     141             : 
     142    22762040 :         if ( H->mElem[left].mScore < H->mElem[largest].mScore )
     143             :         {
     144    10522989 :             largest = left;
     145             :         }
     146    22762040 :         if ( H->mElem[right].mScore < H->mElem[largest].mScore )
     147             :         {
     148     4912871 :             largest = right;
     149             :         }
     150             :     }
     151             : 
     152    11306527 :     return;
     153             : }
     154             : 
     155             : 
     156             : /*-------------------------------------------------------------------*
     157             :  * tcx_arith_find_max_scale()
     158             :  *
     159             :  *
     160             :  *-------------------------------------------------------------------*/
     161             : 
     162      263941 : static float tcx_arith_find_max_scale(
     163             :     const float abs_spectrum[], /* i  : absolute MDCT coefficients    */
     164             :     const int16_t L_frame,      /* i  : number of spectral lines      */
     165             :     const Word16 envelope[],    /* i  : scaled envelope (Q15-e)       */
     166             :     const Word16 envelope_e,    /* i  : scaled envelope exponent (Q0) */
     167             :     const Word16 exps[],        /* i  : expfp(-(integer)envelope[]/2) */
     168             :     const float deadzone        /* i  : deadzone (0.5f = no deadzone) */
     169             : )
     170             : {
     171             :     int16_t i, k, q;
     172             :     float p, scale;
     173             :     Heap heap;
     174             :     Word16 tmpi1, tmpi2;
     175             :     float envelope_scale;
     176      263941 :     const float limit = -9.70406052784f; /* = ln(1/16384): log of smallest allowed probability */
     177             : 
     178             :     /* Find the top most offending lines according to probability estimates */
     179      263941 :     heap.mElem[0].mScore = 0; /* mal: just to silnce the compiler */
     180             : 
     181     2903351 :     for ( i = 0; i < kMaxNumHeapElems; ++i )
     182             :     {
     183     2639410 :         heap.mElem[i].mIndex = 0;
     184     2639410 :         heap.mElem[i].mScore = 0;
     185             :     }
     186     3167292 :     for ( ; i < 2 * kMaxNumHeapElems + 1; ++i )
     187             :     {
     188     2903351 :         heap.mElem[i].mScore = FLT_MAX;
     189             :     }
     190   157699101 :     for ( k = 0; k < L_frame; ++k )
     191             :     {
     192   157435160 :         p = envelope[k] * abs_spectrum[k];
     193   157435160 :         if ( p > heap.mElem[0].mScore )
     194             :         {
     195    11306527 :             heap.mElem[0].mScore = p;
     196    11306527 :             heap.mElem[0].mIndex = k;
     197    11306527 :             MinHeapify_i( &heap, 0 );
     198             :         }
     199             :     }
     200             : 
     201             :     /* Make sure the scale is limited so that the offending lines don't cause probability underflow. */
     202             :     /* Also limit scale to avoiding saturation of the gain quantizer */
     203      263941 :     scale = 1.0f / (float) sqrt( L_frame * 0.5f );
     204      263941 :     envelope_scale = -(float) pow( 2, envelope_e - 16 );
     205     2903351 :     for ( i = 0; i < kMaxNumHeapElems; ++i )
     206             :     {
     207     2639410 :         k = heap.mElem[i].mIndex;
     208             : 
     209             :         /* Get approximate maximum allowed magnitude */
     210     2639410 :         q = (int16_t) ceil( ( ( limit - log( 1.0f - ( exps[k] / PCM16_TO_FLT_FAC ) * ( exps[k] / PCM16_TO_FLT_FAC ) ) ) / ( envelope[k] * envelope_scale ) - 1 ) / 2.0f );
     211             : 
     212             :         /* Refinement: get the exact q */
     213     2639410 :         powfp_odd2( exps[k], q, &tmpi1, &tmpi2 );
     214     2639410 :         if ( tmpi1 - tmpi2 >= 2 )
     215             :         {
     216             :             /* q may be too low */
     217     2638375 :             powfp_odd2( exps[k], q + 1, &tmpi1, &tmpi2 );
     218     3095374 :             while ( tmpi1 - tmpi2 >= 2 )
     219             :             {
     220      456999 :                 ++q;
     221      456999 :                 powfp_odd2( exps[k], q + 1, &tmpi1, &tmpi2 );
     222             :             }
     223             :         }
     224             :         else
     225             :         {
     226             :             /* q is too high */
     227        1035 :             --q;
     228        1035 :             powfp_odd2( exps[k], q, &tmpi1, &tmpi2 );
     229        1035 :             while ( tmpi1 - tmpi2 < 2 )
     230             :             {
     231           0 :                 --q;
     232           0 :                 powfp_odd2( exps[k], q, &tmpi1, &tmpi2 );
     233             :             }
     234             :         }
     235             : 
     236             :         /* Find the largest scale so that the quantized magnitude is at most q */
     237     2639410 :         p = ( q + 0.99f - deadzone ) / ( abs_spectrum[k] + 0.000001f );
     238     2639410 :         assert( (int16_t) ( abs_spectrum[k] * p + deadzone ) <= q );
     239     2639410 :         scale = min( scale, p );
     240             :     }
     241             : 
     242      263941 :     return scale;
     243             : }
     244             : 
     245             : 
     246             : /*-------------------------------------------------------------------*
     247             :  * tcx_arith_find_kMax()
     248             :  *
     249             :  *
     250             :  *-------------------------------------------------------------------*/
     251             : 
     252             : /*! r: index of highest freq. nonzero line (-1 if all zeros) */
     253     4327955 : static int16_t tcx_arith_find_kMax(
     254             :     const float abs_spectrum[],    /* i  : absolute MDCT coefficients    */
     255             :     const int16_t L_frame,         /* i  : number of spectral lines      */
     256             :     const float scale,             /* i  : scalar quantizer scale        */
     257             :     const float deadzone,          /* i  : deadzone (0.5f = no deadzone) */
     258             :     const int16_t deadzone_flags[] /* i  : line-wise deadzone control    */
     259             : )
     260             : {
     261             :     int16_t kMax;
     262             : 
     263     4327955 :     kMax = L_frame - 1;
     264  2109872073 :     while ( ( kMax >= 0 ) && ( abs_spectrum[kMax] * scale < ( 1.0f - deadzone ) + deadzone * deadzone_flags[kMax] ) )
     265             :     {
     266  2105544118 :         kMax--;
     267             :     }
     268             : 
     269     4327955 :     return kMax;
     270             : }
     271             : 
     272             : 
     273             : /*-------------------------------------------------------------------*
     274             :  * tcx_arith_rateloop()
     275             :  *
     276             :  *
     277             :  *-------------------------------------------------------------------*/
     278             : 
     279             : /*! r: best scale */
     280      263941 : static float tcx_arith_rateloop(
     281             :     const float abs_spectrum[],     /* i  : absolute MDCT coefficients       */
     282             :     const int16_t L_frame,          /* i  : number of spectral lines         */
     283             :     const Word16 envelope[],        /* i  : scaled envelope (Q15-e)          */
     284             :     const Word16 envelope_e,        /* i  : scaled envelope exponent (Q0)    */
     285             :     const Word16 exps[],            /* i  : expfp(-(integer)envelope[]/2)    */
     286             :     const int16_t target_bits,      /* i  : target bit budget                */
     287             :     const float deadzone,           /* i  : deadzone (0.5f = no deadzone)    */
     288             :     const int16_t deadzone_flags[], /* i  : line-wise deadzone control       */
     289             :     float *target_bits_fac          /* i/o: scale estimator compensation     */
     290             : )
     291             : {
     292             :     int16_t k, idx, kMax, q;
     293             :     float s, adjust;
     294             :     float fixed_bits[2][N_MAX_ARI];
     295             :     float estimator_undershoot;
     296             :     float max_complexity;
     297             :     int16_t iter;     /* rate loop iteration counter               */
     298             :     float scale;      /* SQ scale factor to try next               */
     299             :     float scale_best; /* best SQ scale factor                      */
     300             :     float scale_max;  /* maximum allowable scale factor            */
     301             :     float lob;        /* lower bound of SQ scale factor            */
     302             :     float hib;        /* upper bound of SQ scale factor            */
     303             :     int16_t flag;     /* 1:bit surplus, -1:bit deficit, 0:unknown  */
     304             :     float complexity; /* cumulative rate loop complexity           */
     305             :     float bits;       /* number of bits (approximate)              */
     306             :     float envelope_scale;
     307             : 
     308             : 
     309      263941 :     scale = tcx_arith_estimate_scale( abs_spectrum, L_frame, envelope, envelope_e );
     310      263941 :     scale *= *target_bits_fac;
     311             : 
     312      263941 :     scale_max = tcx_arith_find_max_scale( abs_spectrum, L_frame, envelope, envelope_e, exps, deadzone );
     313      263941 :     if ( scale > scale_max )
     314             :     {
     315      178144 :         scale = scale_max;
     316             :     }
     317             : 
     318      263941 :     scale_best = scale;
     319      263941 :     lob = 0.0f;
     320      263941 :     hib = 0.0f;
     321      263941 :     flag = 0;
     322      263941 :     complexity = 0;
     323      263941 :     bits = 0;
     324      263941 :     max_complexity = 96.0f * L_frame;
     325      263941 :     iter = 0;
     326      263941 :     envelope_scale = (float) pow( 2, envelope_e - 15 );
     327             : 
     328      263941 :     estimator_undershoot = 0;
     329             :     /* Precalculate fixed bit costs */
     330   157699101 :     for ( k = 0; k < L_frame; ++k )
     331             :     {
     332   157435160 :         s = envelope[k] * envelope_scale;
     333             : 
     334   157435160 :         fixed_bits[0][k] = -log2_f( 1 - exps[k] / PCM16_TO_FLT_FAC );
     335   157435160 :         fixed_bits[1][k] = 1 - s * 0.5f * INV_LOG_2 - log2_f( 1 - ( exps[k] / PCM16_TO_FLT_FAC ) * ( exps[k] / PCM16_TO_FLT_FAC ) );
     336             :     }
     337             : 
     338     4327955 :     while ( complexity + 48 + L_frame * 11 < max_complexity )
     339             :     {
     340     4064014 :         kMax = tcx_arith_find_kMax( abs_spectrum, L_frame, scale, deadzone, deadzone_flags );
     341     4064014 :         complexity += 16 + ( L_frame - kMax ) * 5 + ( kMax + 1 ) * 2;
     342             : 
     343     4064014 :         bits = estimator_undershoot * kMax + 1;
     344             : 
     345   470749519 :         for ( k = 0; k <= kMax; ++k )
     346             :         {
     347   466685505 :             s = envelope[k] * envelope_scale;
     348   466685505 :             q = (int16_t) ( abs_spectrum[k] * scale + deadzone );
     349   466685505 :             idx = min( 1, q );
     350   466685505 :             bits += fixed_bits[idx][k];
     351   466685505 :             bits += s * q * INV_LOG_2;
     352             :         }
     353     4064014 :         complexity += 32 + 6 * kMax;
     354     4064014 :         if ( iter == 0 )
     355             :         {
     356             :             /* First rate loop iteration */
     357      263941 :             if ( scale < scale_max )
     358             :             {
     359             :                 /* Only update in non-degenerate case */
     360             :                 /* Update estimator temporal compensation factor */
     361       85797 :                 *target_bits_fac *= target_bits / (float) bits;
     362       85797 :                 if ( *target_bits_fac > 1.25f )
     363             :                 {
     364          64 :                     *target_bits_fac = 1.25f;
     365             :                 }
     366       85797 :                 if ( *target_bits_fac < 0.75f )
     367             :                 {
     368       80927 :                     *target_bits_fac = 0.75f;
     369             :                 }
     370             :             }
     371             :         }
     372             : 
     373     4064014 :         if ( bits <= target_bits )
     374             :         {
     375             :             /* Bits leftover => scale is too small */
     376     2682716 :             if ( flag <= 0 || scale >= scale_best )
     377             :             {
     378     2682716 :                 scale_best = scale;
     379     2682716 :                 flag = 1;
     380             :             }
     381             : 
     382     2682716 :             lob = scale;
     383     2682716 :             if ( hib > 0 )
     384             :             {
     385             :                 /* Bisection search */
     386     1322496 :                 scale = ( lob + hib ) * 0.5f;
     387             :             }
     388             :             else
     389             :             {
     390             :                 /* Initial scale adaptation */
     391     1360220 :                 adjust = 1.25f * target_bits / (float) bits;
     392     1360220 :                 if ( adjust > 2.0f )
     393             :                 {
     394      790296 :                     adjust = 2.0f;
     395             :                 }
     396     1360220 :                 scale *= adjust;
     397     1360220 :                 if ( scale > scale_max )
     398             :                 {
     399     1358009 :                     scale = scale_max;
     400             :                 }
     401             :             }
     402             :         }
     403             :         else
     404             :         {
     405             :             /* Ran out of bits => scale is too large */
     406     1381298 :             hib = scale;
     407     1381298 :             if ( lob > 0 )
     408             :             {
     409             :                 /* Bisection search */
     410     1197754 :                 scale = ( lob + hib ) * 0.5f;
     411             :             }
     412             :             else
     413             :             {
     414             :                 /* Initial scale adaptation */
     415      183544 :                 adjust = 0.8f * target_bits / (float) bits;
     416      183544 :                 if ( adjust < 0.5f )
     417             :                 {
     418       77424 :                     adjust = 0.5f;
     419             :                 }
     420      183544 :                 scale *= adjust;
     421             :             }
     422     1381298 :             if ( flag <= 0 )
     423             :             {
     424      183544 :                 scale_best = scale;
     425      183544 :                 flag = 0;
     426             :             }
     427             :         }
     428     4064014 :         ++iter;
     429             :     }
     430             : 
     431             : 
     432      263941 :     return scale_best;
     433             : }
     434             : 
     435             : 
     436             : /*-------------------------------------------------------------------*
     437             :  * tcx_arith_encode()
     438             :  *
     439             :  *
     440             :  *-------------------------------------------------------------------*/
     441             : 
     442             : /*! r: number of bits consumed */
     443      263941 : static int16_t tcx_arith_encode(
     444             :     int32_t q_abs_spectrum[],  /* i/o: scalar quantized absolute spectrum        */
     445             :     const int16_t signs[],     /* i  : signs                                     */
     446             :     const int16_t kMax,        /* i  : number of nonzero spectral lines to code  */
     447             :     const int16_t L_frame,     /* i  : nominal number of spectral lines          */
     448             :     const Word16 exps[],       /* i  : expfp(-(integer)envelope[]/2)             */
     449             :     const int16_t target_bits, /* i  : target bit budget                         */
     450             :     int16_t prm[]              /* o  : bitstream                                 */
     451             : )
     452             : {
     453             :     Tastat as, as_lastgood;
     454             :     int16_t bp, bp_lastgood;
     455             :     int16_t k;
     456             :     int16_t kEncoded;
     457             :     Word16 tmpi1, tmpi2;
     458             : 
     459             :     /* Final coding */
     460      263941 :     ari_start_encoding_14bits( &as );
     461      263941 :     ari_copy_states( &as, &as_lastgood );
     462      263941 :     bp = bp_lastgood = 0;
     463      263941 :     kEncoded = kMax;
     464    30646787 :     for ( k = 0; k <= kMax; ++k )
     465             :     {
     466    30384097 :         if ( q_abs_spectrum[k] == 0 )
     467             :         {
     468    23146942 :             assert( exps[k] >= 2 );
     469    23146942 :             bp = ari_encode_14bits_range( prm, bp, target_bits, &as, exps[k] >> 1, 16384 );
     470             :         }
     471             :         else
     472             :         {
     473             :             /* q_abs_spectrum[k] != 0 */
     474     7237155 :             powfp_odd2( exps[k], (Word16) q_abs_spectrum[k], &tmpi1, &tmpi2 );
     475     7237398 :             while ( tmpi1 < tmpi2 + 2 )
     476             :             {
     477         243 :                 --q_abs_spectrum[k];
     478         243 :                 powfp_odd2( exps[k], (Word16) q_abs_spectrum[k], &tmpi1, &tmpi2 );
     479             :             }
     480     7237155 :             bp = ari_encode_14bits_range( prm, bp, target_bits, &as, tmpi2 >> 1, tmpi1 >> 1 );
     481     7237155 :             bp = ari_encode_14bits_sign( prm, bp, target_bits, &as, signs[k] );
     482             :         }
     483             :         /* Check bit budget status */
     484    30384097 :         if ( as.high <= as.low )
     485             :         {
     486             :             /* no bits left */
     487             :             /* printf("\noverflow at %d\n\n", k); */
     488        1251 :             if ( q_abs_spectrum[k] > 1 ) /* Lower magnitude is still > 0 */
     489             :             {
     490             :                 /* Restore state */
     491         284 :                 ari_copy_states( &as_lastgood, &as );
     492         284 :                 bp = bp_lastgood;
     493             : 
     494             :                 /* Quantize to lower magnitude */
     495         284 :                 --q_abs_spectrum[k];
     496             : 
     497             :                 /* Retry encoding */
     498         284 :                 powfp_odd2( exps[k], (Word16) q_abs_spectrum[k], &tmpi1, &tmpi2 );
     499         284 :                 bp = ari_encode_14bits_range( prm, bp, target_bits, &as, tmpi2 >> 1, tmpi1 >> 1 );
     500         284 :                 bp = ari_encode_14bits_sign( prm, bp, target_bits, &as, signs[k] );
     501         284 :                 if ( as.high > as.low ) /* Success */
     502             :                 {
     503         284 :                     ari_copy_states( &as, &as_lastgood );
     504         284 :                     bp_lastgood = bp;
     505         284 :                     kEncoded = k;
     506         284 :                     for ( ++k; k <= kMax; k++ )
     507             :                     {
     508           0 :                         q_abs_spectrum[k] = 0;
     509             :                     }
     510         284 :                     break;
     511             :                 }
     512             :             }
     513         967 :             ari_copy_states( &as_lastgood, &as );
     514         967 :             bp = bp_lastgood;
     515         967 :             kEncoded = k - 1;
     516        1934 :             for ( ; k <= kMax; k++ )
     517             :             {
     518         967 :                 q_abs_spectrum[k] = 0;
     519             :             }
     520         967 :             break;
     521             :         }
     522             :         else
     523             :         {
     524    30382846 :             ari_copy_states( &as, &as_lastgood );
     525    30382846 :             bp_lastgood = bp;
     526             :         }
     527             :     }
     528             : 
     529             :     /* Send zeros until L_frame */
     530   117448653 :     for ( k = kEncoded + 1, kEncoded = L_frame - 1; k < L_frame; ++k )
     531             :     {
     532   117212824 :         assert( exps[k] >= 2 );
     533   117212824 :         bp = ari_encode_14bits_range( prm, bp, target_bits, &as, exps[k] >> 1, 16384 );
     534             :         /* Check bit budget status */
     535   117212824 :         if ( as.high <= as.low )
     536             :         {
     537             :             /* no bits left */
     538       28112 :             ari_copy_states( &as_lastgood, &as );
     539       28112 :             bp = bp_lastgood;
     540       28112 :             kEncoded = k - 1;
     541       28112 :             break;
     542             :         }
     543             :         else
     544             :         {
     545   117184712 :             ari_copy_states( &as, &as_lastgood );
     546   117184712 :             bp_lastgood = bp;
     547             :         }
     548             :     }
     549             : 
     550      263941 :     if ( kEncoded == L_frame - 1 )
     551             :     {
     552             :         /* RESQ bits possibly available */
     553             :         /* Limit target bits to actually needed bits */
     554      235829 :         bp = ari_done_cbr_encoding_14bits( prm, bp, bp + 16 + as.bits_to_follow, &as );
     555             :     }
     556             :     else
     557             :     {
     558       28112 :         bp = ari_done_cbr_encoding_14bits( prm, bp, target_bits, &as );
     559             :     }
     560             : 
     561      263941 :     return bp;
     562             : }
     563             : 
     564             : 
     565             : /*-------------------------------------------------------------------*
     566             :  * tcx_arith_encode_envelope()
     567             :  *
     568             :  *
     569             :  *-------------------------------------------------------------------*/
     570             : 
     571      263941 : void tcx_arith_encode_envelope(
     572             :     float spectrum[],            /* i/o: MDCT coefficients               */
     573             :     int16_t signs[],             /* o  : signs (spectrum[.]<0)           */
     574             :     const int16_t L_frame,       /* i  : frame or MDCT length            */
     575             :     const int16_t L_spec,        /* i  : length w/o BW limitation        */
     576             :     Encoder_State *st,           /* i/o: coder state                     */
     577             :     const Word16 A_ind[],        /* i  : quantised LPC coefficients      */
     578             :     int16_t target_bits,         /* i  : number of available bits        */
     579             :     int16_t prm[],               /* o  : bitstream parameters            */
     580             :     const int16_t use_hm,        /* i  : use HM in current frame?        */
     581             :     int16_t prm_hm[],            /* o  : HM parameter area               */
     582             :     const int16_t tcxltp_pitch,  /* i  : TCX LTP pitch in FD, -1 if n/a  */
     583             :     int16_t *arith_bits,         /* o  : bits used for ari. coding       */
     584             :     int16_t *signaling_bits,     /* o  : bits used for signaling         */
     585             :     const int16_t low_complexity /* i  : low-complexity flag             */
     586             : )
     587             : {
     588      263941 :     TCX_ENC_HANDLE hTcxEnc = st->hTcxEnc;
     589             :     Word16 tmp;
     590             :     Word32 env[N_MAX_ARI]; /* unscaled envelope (Q16) */
     591             :     Word16 *envelope;      /* scaled envelope (Q15-e) */
     592             :     Word16 envelope_e;
     593             :     Word16 exponents[N_MAX_ARI]; /* Q15 */
     594             :     int16_t L_spec_core;
     595             :     int32_t *q_spectrum;
     596             :     TCX_CONFIG_HANDLE hTcxCfg;
     597             :     float scale;
     598             :     int16_t k, kMax;
     599             :     float deadzone;
     600             :     const int16_t *deadzone_flags;
     601             :     float gamma_w, gamma_uw;
     602             :     int16_t hm_bits;
     603             : 
     604      263941 :     assert( L_spec <= N_MAX_ARI );
     605             : 
     606      263941 :     hTcxCfg = st->hTcxCfg;
     607      263941 :     deadzone = hTcxCfg->sq_rounding;
     608      263941 :     deadzone_flags = hTcxEnc->memQuantZeros;
     609      263941 :     *signaling_bits = 0;
     610      263941 :     assert( st->enableTcxLpc );
     611      263941 :     gamma_w = 1.0f;
     612      263941 :     gamma_uw = 1.0f / st->gamma;
     613             : 
     614             : #define WMC_TOOL_SKIP
     615      263941 :     tcx_arith_render_envelope( A_ind, L_frame, L_spec, FL2WORD16( hTcxCfg->preemph_fac ), FL2WORD16( gamma_w ), FL2WORD16( 0.5f * gamma_uw ), env );
     616             : #undef WMC_TOOL_SKIP
     617             : 
     618   157699101 :     for ( k = 0; k < L_spec; ++k )
     619             :     {
     620   157435160 :         if ( spectrum[k] < 0 )
     621             :         {
     622    27842991 :             spectrum[k] = -spectrum[k];
     623    27842991 :             signs[k] = 1;
     624             :         }
     625             :         else
     626             :         {
     627   129592169 :             signs[k] = 0;
     628             :         }
     629             :     }
     630             : 
     631      263941 :     if ( use_hm )
     632             :     {
     633      249266 :         tcx_hm_analyse( spectrum, L_spec, env, target_bits, hTcxCfg->coder_type, prm_hm, tcxltp_pitch, hTcxEnc->tcxltp_gain, &hm_bits );
     634             : 
     635      249266 :         target_bits -= hm_bits;
     636      249266 :         *signaling_bits += hm_bits;
     637             :     }
     638             :     else
     639             :     {
     640       14675 :         prm_hm[0] = 0; /* just to be sure */
     641       14675 :         hm_bits = 0;
     642             :     }
     643             : 
     644      263941 :     L_spec_core = L_spec;
     645      263941 :     if ( st->igf )
     646             :     {
     647      263730 :         L_spec_core = min( L_spec_core, st->hIGFEnc->infoStartLine );
     648             :     }
     649      263941 :     envelope = (Word16 *) env;
     650      263941 :     tcx_arith_scale_envelope( L_spec, L_spec_core, env, target_bits, low_complexity, envelope, &envelope_e );
     651             : 
     652             : #define WMC_TOOL_SKIP
     653      263941 :     tmp = sub( envelope_e, 1 );
     654   157699101 :     FOR( k = 0; k < L_spec; k++ )
     655             :     {
     656   157435160 :         exponents[k] = expfp( negate( envelope[k] ), tmp );
     657             :     }
     658             : #undef WMC_TOOL_SKIP
     659      263941 :     scale = tcx_arith_rateloop( spectrum, L_spec, envelope, envelope_e, exponents, target_bits, deadzone, deadzone_flags, &( hTcxEnc->tcx_target_bits_fac ) );
     660             : 
     661             :     /* Final quantization */
     662      263941 :     kMax = tcx_arith_find_kMax( spectrum, L_spec, scale, deadzone, deadzone_flags );
     663             : 
     664      263941 :     q_spectrum = env; /* Reuse buffer */
     665    30648038 :     for ( k = 0; k <= kMax; ++k )
     666             :     {
     667             :         /* quantise using dead-zone */
     668    30384097 :         q_spectrum[k] = (int32_t) ( spectrum[k] * scale + deadzone );
     669             :     }
     670             : 
     671             :     /* Final encoding */
     672      263941 :     *arith_bits = tcx_arith_encode( q_spectrum, signs, kMax, L_spec, exponents, target_bits, prm );
     673             : 
     674             :     /* Multiply back the signs */
     675    30648038 :     for ( k = 0; k <= kMax; ++k )
     676             :     {
     677    30384097 :         spectrum[k] = (float) ( q_spectrum[k] * ( 1 - 2 * signs[k] ) );
     678             :     }
     679   127335572 :     for ( ; k < max( L_frame, L_spec ); ++k )
     680             :     {
     681   127071631 :         spectrum[k] = 0;
     682             :     }
     683             : 
     684      263941 :     return;
     685             : }

Generated by: LCOV version 1.14