LCOV - code coverage report
Current view: top level - lib_enc - arith_coder_enc.c (source / functions) Hit Total Coverage
Test: Coverage on main -- short test vectors @ 6c9ddc4024a9c0e1ecb8f643f114a84a0e26ec6b Lines: 220 226 97.3 %
Date: 2025-05-23 08:37:30 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       17457 : 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       17457 :     scale = 0.01f;
      88    11009457 :     for ( k = 0; k < L_frame; k++ )
      89             :     {
      90    10992000 :         tmp = abs_spectrum[k] * envelope[k];
      91    10992000 :         scale += tmp * tmp;
      92             :     }
      93       17457 :     tmp = (float) ( 1 << ( 15 - envelope_e ) );
      94       17457 :     scale = (float) sqrt( ( L_frame * tmp * tmp * 4.0f ) / scale );
      95             : 
      96             : 
      97       17457 :     return scale;
      98             : }
      99             : 
     100             : 
     101             : /*-------------------------------------------------------------------*
     102             :  * MinHeapify_i()
     103             :  *
     104             :  *
     105             :  *-------------------------------------------------------------------*/
     106             : 
     107      691919 : static void MinHeapify_i(
     108             :     Heap *H,
     109             :     int16_t i )
     110             : {
     111             :     int16_t left, right, largest;
     112             :     HeapElem T;
     113             : 
     114      691919 :     left = 2 * i + 1;
     115      691919 :     right = left + 1;
     116      691919 :     largest = i;
     117             : 
     118      691919 :     if ( H->mElem[left].mScore < H->mElem[largest].mScore )
     119             :     {
     120      597631 :         largest = left;
     121             :     }
     122      691919 :     if ( H->mElem[right].mScore < H->mElem[largest].mScore )
     123             :     {
     124      223542 :         largest = right;
     125             :     }
     126     2070532 :     while ( largest != i )
     127             :     {
     128     1378613 :         T.mIndex = H->mElem[i].mIndex;
     129     1378613 :         T.mScore = H->mElem[i].mScore;
     130             : 
     131     1378613 :         H->mElem[i].mIndex = H->mElem[largest].mIndex;
     132     1378613 :         H->mElem[i].mScore = H->mElem[largest].mScore;
     133             : 
     134     1378613 :         H->mElem[largest].mIndex = T.mIndex;
     135     1378613 :         H->mElem[largest].mScore = T.mScore;
     136             : 
     137     1378613 :         i = largest;
     138             : 
     139     1378613 :         left = 2 * i + 1;
     140     1378613 :         right = left + 1;
     141             : 
     142     1378613 :         if ( H->mElem[left].mScore < H->mElem[largest].mScore )
     143             :         {
     144      633242 :             largest = left;
     145             :         }
     146     1378613 :         if ( H->mElem[right].mScore < H->mElem[largest].mScore )
     147             :         {
     148      293189 :             largest = right;
     149             :         }
     150             :     }
     151             : 
     152      691919 :     return;
     153             : }
     154             : 
     155             : 
     156             : /*-------------------------------------------------------------------*
     157             :  * tcx_arith_find_max_scale()
     158             :  *
     159             :  *
     160             :  *-------------------------------------------------------------------*/
     161             : 
     162       17457 : 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       17457 :     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       17457 :     heap.mElem[0].mScore = 0; /* mal: just to silnce the compiler */
     180             : 
     181      192027 :     for ( i = 0; i < kMaxNumHeapElems; ++i )
     182             :     {
     183      174570 :         heap.mElem[i].mIndex = 0;
     184      174570 :         heap.mElem[i].mScore = 0;
     185             :     }
     186      209484 :     for ( ; i < 2 * kMaxNumHeapElems + 1; ++i )
     187             :     {
     188      192027 :         heap.mElem[i].mScore = FLT_MAX;
     189             :     }
     190    11009457 :     for ( k = 0; k < L_frame; ++k )
     191             :     {
     192    10992000 :         p = envelope[k] * abs_spectrum[k];
     193    10992000 :         if ( p > heap.mElem[0].mScore )
     194             :         {
     195      691919 :             heap.mElem[0].mScore = p;
     196      691919 :             heap.mElem[0].mIndex = k;
     197      691919 :             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       17457 :     scale = 1.0f / (float) sqrt( L_frame * 0.5f );
     204       17457 :     envelope_scale = -(float) pow( 2, envelope_e - 16 );
     205      192027 :     for ( i = 0; i < kMaxNumHeapElems; ++i )
     206             :     {
     207      174570 :         k = heap.mElem[i].mIndex;
     208             : 
     209             :         /* Get approximate maximum allowed magnitude */
     210      174570 :         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      174570 :         powfp_odd2( exps[k], q, &tmpi1, &tmpi2 );
     214      174570 :         if ( tmpi1 - tmpi2 >= 2 )
     215             :         {
     216             :             /* q may be too low */
     217      174518 :             powfp_odd2( exps[k], q + 1, &tmpi1, &tmpi2 );
     218      197411 :             while ( tmpi1 - tmpi2 >= 2 )
     219             :             {
     220       22893 :                 ++q;
     221       22893 :                 powfp_odd2( exps[k], q + 1, &tmpi1, &tmpi2 );
     222             :             }
     223             :         }
     224             :         else
     225             :         {
     226             :             /* q is too high */
     227          52 :             --q;
     228          52 :             powfp_odd2( exps[k], q, &tmpi1, &tmpi2 );
     229          52 :             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      174570 :         p = ( q + 0.99f - deadzone ) / ( abs_spectrum[k] + 0.000001f );
     238      174570 :         assert( (int16_t) ( abs_spectrum[k] * p + deadzone ) <= q );
     239      174570 :         scale = min( scale, p );
     240             :     }
     241             : 
     242       17457 :     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      285931 : 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      285931 :     kMax = L_frame - 1;
     264   144196674 :     while ( ( kMax >= 0 ) && ( abs_spectrum[kMax] * scale < ( 1.0f - deadzone ) + deadzone * deadzone_flags[kMax] ) )
     265             :     {
     266   143910743 :         kMax--;
     267             :     }
     268             : 
     269      285931 :     return kMax;
     270             : }
     271             : 
     272             : 
     273             : /*-------------------------------------------------------------------*
     274             :  * tcx_arith_rateloop()
     275             :  *
     276             :  *
     277             :  *-------------------------------------------------------------------*/
     278             : 
     279             : /*! r: best scale */
     280       17457 : 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       17457 :     scale = tcx_arith_estimate_scale( abs_spectrum, L_frame, envelope, envelope_e );
     310       17457 :     scale *= *target_bits_fac;
     311             : 
     312       17457 :     scale_max = tcx_arith_find_max_scale( abs_spectrum, L_frame, envelope, envelope_e, exps, deadzone );
     313       17457 :     if ( scale > scale_max )
     314             :     {
     315       12497 :         scale = scale_max;
     316             :     }
     317             : 
     318       17457 :     scale_best = scale;
     319       17457 :     lob = 0.0f;
     320       17457 :     hib = 0.0f;
     321       17457 :     flag = 0;
     322       17457 :     complexity = 0;
     323       17457 :     bits = 0;
     324       17457 :     max_complexity = 96.0f * L_frame;
     325       17457 :     iter = 0;
     326       17457 :     envelope_scale = (float) pow( 2, envelope_e - 15 );
     327             : 
     328       17457 :     estimator_undershoot = 0;
     329             :     /* Precalculate fixed bit costs */
     330    11009457 :     for ( k = 0; k < L_frame; ++k )
     331             :     {
     332    10992000 :         s = envelope[k] * envelope_scale;
     333             : 
     334    10992000 :         fixed_bits[0][k] = -log2_f( 1 - exps[k] / PCM16_TO_FLT_FAC );
     335    10992000 :         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      285931 :     while ( complexity + 48 + L_frame * 11 < max_complexity )
     339             :     {
     340      268474 :         kMax = tcx_arith_find_kMax( abs_spectrum, L_frame, scale, deadzone, deadzone_flags );
     341      268474 :         complexity += 16 + ( L_frame - kMax ) * 5 + ( kMax + 1 ) * 2;
     342             : 
     343      268474 :         bits = estimator_undershoot * kMax + 1;
     344             : 
     345    34881907 :         for ( k = 0; k <= kMax; ++k )
     346             :         {
     347    34613433 :             s = envelope[k] * envelope_scale;
     348    34613433 :             q = (int16_t) ( abs_spectrum[k] * scale + deadzone );
     349    34613433 :             idx = min( 1, q );
     350    34613433 :             bits += fixed_bits[idx][k];
     351    34613433 :             bits += s * q * INV_LOG_2;
     352             :         }
     353      268474 :         complexity += 32 + 6 * kMax;
     354      268474 :         if ( iter == 0 )
     355             :         {
     356             :             /* First rate loop iteration */
     357       17457 :             if ( scale < scale_max )
     358             :             {
     359             :                 /* Only update in non-degenerate case */
     360             :                 /* Update estimator temporal compensation factor */
     361        4960 :                 *target_bits_fac *= target_bits / (float) bits;
     362        4960 :                 if ( *target_bits_fac > 1.25f )
     363             :                 {
     364           0 :                     *target_bits_fac = 1.25f;
     365             :                 }
     366        4960 :                 if ( *target_bits_fac < 0.75f )
     367             :                 {
     368        4795 :                     *target_bits_fac = 0.75f;
     369             :                 }
     370             :             }
     371             :         }
     372             : 
     373      268474 :         if ( bits <= target_bits )
     374             :         {
     375             :             /* Bits leftover => scale is too small */
     376      150058 :             if ( flag <= 0 || scale >= scale_best )
     377             :             {
     378      150058 :                 scale_best = scale;
     379      150058 :                 flag = 1;
     380             :             }
     381             : 
     382      150058 :             lob = scale;
     383      150058 :             if ( hib > 0 )
     384             :             {
     385             :                 /* Bisection search */
     386      113048 :                 scale = ( lob + hib ) * 0.5f;
     387             :             }
     388             :             else
     389             :             {
     390             :                 /* Initial scale adaptation */
     391       37010 :                 adjust = 1.25f * target_bits / (float) bits;
     392       37010 :                 if ( adjust > 2.0f )
     393             :                 {
     394       15616 :                     adjust = 2.0f;
     395             :                 }
     396       37010 :                 scale *= adjust;
     397       37010 :                 if ( scale > scale_max )
     398             :                 {
     399       36922 :                     scale = scale_max;
     400             :                 }
     401             :             }
     402             :         }
     403             :         else
     404             :         {
     405             :             /* Ran out of bits => scale is too large */
     406      118416 :             hib = scale;
     407      118416 :             if ( lob > 0 )
     408             :             {
     409             :                 /* Bisection search */
     410      102611 :                 scale = ( lob + hib ) * 0.5f;
     411             :             }
     412             :             else
     413             :             {
     414             :                 /* Initial scale adaptation */
     415       15805 :                 adjust = 0.8f * target_bits / (float) bits;
     416       15805 :                 if ( adjust < 0.5f )
     417             :                 {
     418        7912 :                     adjust = 0.5f;
     419             :                 }
     420       15805 :                 scale *= adjust;
     421             :             }
     422      118416 :             if ( flag <= 0 )
     423             :             {
     424       15805 :                 scale_best = scale;
     425       15805 :                 flag = 0;
     426             :             }
     427             :         }
     428      268474 :         ++iter;
     429             :     }
     430             : 
     431             : 
     432       17457 :     return scale_best;
     433             : }
     434             : 
     435             : 
     436             : /*-------------------------------------------------------------------*
     437             :  * tcx_arith_encode()
     438             :  *
     439             :  *
     440             :  *-------------------------------------------------------------------*/
     441             : 
     442             : /*! r: number of bits consumed */
     443       17457 : 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       17457 :     ari_start_encoding_14bits( &as );
     461       17457 :     ari_copy_states( &as, &as_lastgood );
     462       17457 :     bp = bp_lastgood = 0;
     463       17457 :     kEncoded = kMax;
     464     2211695 :     for ( k = 0; k <= kMax; ++k )
     465             :     {
     466     2194304 :         if ( q_abs_spectrum[k] == 0 )
     467             :         {
     468     1689359 :             assert( exps[k] >= 2 );
     469     1689359 :             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      504945 :             powfp_odd2( exps[k], (Word16) q_abs_spectrum[k], &tmpi1, &tmpi2 );
     475      504945 :             while ( tmpi1 < tmpi2 + 2 )
     476             :             {
     477           0 :                 --q_abs_spectrum[k];
     478           0 :                 powfp_odd2( exps[k], (Word16) q_abs_spectrum[k], &tmpi1, &tmpi2 );
     479             :             }
     480      504945 :             bp = ari_encode_14bits_range( prm, bp, target_bits, &as, tmpi2 >> 1, tmpi1 >> 1 );
     481      504945 :             bp = ari_encode_14bits_sign( prm, bp, target_bits, &as, signs[k] );
     482             :         }
     483             :         /* Check bit budget status */
     484     2194304 :         if ( as.high <= as.low )
     485             :         {
     486             :             /* no bits left */
     487             :             /* printf("\noverflow at %d\n\n", k); */
     488          66 :             if ( q_abs_spectrum[k] > 1 ) /* Lower magnitude is still > 0 */
     489             :             {
     490             :                 /* Restore state */
     491          10 :                 ari_copy_states( &as_lastgood, &as );
     492          10 :                 bp = bp_lastgood;
     493             : 
     494             :                 /* Quantize to lower magnitude */
     495          10 :                 --q_abs_spectrum[k];
     496             : 
     497             :                 /* Retry encoding */
     498          10 :                 powfp_odd2( exps[k], (Word16) q_abs_spectrum[k], &tmpi1, &tmpi2 );
     499          10 :                 bp = ari_encode_14bits_range( prm, bp, target_bits, &as, tmpi2 >> 1, tmpi1 >> 1 );
     500          10 :                 bp = ari_encode_14bits_sign( prm, bp, target_bits, &as, signs[k] );
     501          10 :                 if ( as.high > as.low ) /* Success */
     502             :                 {
     503          10 :                     ari_copy_states( &as, &as_lastgood );
     504          10 :                     bp_lastgood = bp;
     505          10 :                     kEncoded = k;
     506          10 :                     for ( ++k; k <= kMax; k++ )
     507             :                     {
     508           0 :                         q_abs_spectrum[k] = 0;
     509             :                     }
     510          10 :                     break;
     511             :                 }
     512             :             }
     513          56 :             ari_copy_states( &as_lastgood, &as );
     514          56 :             bp = bp_lastgood;
     515          56 :             kEncoded = k - 1;
     516         112 :             for ( ; k <= kMax; k++ )
     517             :             {
     518          56 :                 q_abs_spectrum[k] = 0;
     519             :             }
     520          56 :             break;
     521             :         }
     522             :         else
     523             :         {
     524     2194238 :             ari_copy_states( &as, &as_lastgood );
     525     2194238 :             bp_lastgood = bp;
     526             :         }
     527             :     }
     528             : 
     529             :     /* Send zeros until L_frame */
     530     8649113 :     for ( k = kEncoded + 1, kEncoded = L_frame - 1; k < L_frame; ++k )
     531             :     {
     532     8632073 :         assert( exps[k] >= 2 );
     533     8632073 :         bp = ari_encode_14bits_range( prm, bp, target_bits, &as, exps[k] >> 1, 16384 );
     534             :         /* Check bit budget status */
     535     8632073 :         if ( as.high <= as.low )
     536             :         {
     537             :             /* no bits left */
     538         417 :             ari_copy_states( &as_lastgood, &as );
     539         417 :             bp = bp_lastgood;
     540         417 :             kEncoded = k - 1;
     541         417 :             break;
     542             :         }
     543             :         else
     544             :         {
     545     8631656 :             ari_copy_states( &as, &as_lastgood );
     546     8631656 :             bp_lastgood = bp;
     547             :         }
     548             :     }
     549             : 
     550       17457 :     if ( kEncoded == L_frame - 1 )
     551             :     {
     552             :         /* RESQ bits possibly available */
     553             :         /* Limit target bits to actually needed bits */
     554       17040 :         bp = ari_done_cbr_encoding_14bits( prm, bp, bp + 16 + as.bits_to_follow, &as );
     555             :     }
     556             :     else
     557             :     {
     558         417 :         bp = ari_done_cbr_encoding_14bits( prm, bp, target_bits, &as );
     559             :     }
     560             : 
     561       17457 :     return bp;
     562             : }
     563             : 
     564             : 
     565             : /*-------------------------------------------------------------------*
     566             :  * tcx_arith_encode_envelope()
     567             :  *
     568             :  *
     569             :  *-------------------------------------------------------------------*/
     570             : 
     571       17457 : 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       17457 :     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       17457 :     assert( L_spec <= N_MAX_ARI );
     605             : 
     606       17457 :     hTcxCfg = st->hTcxCfg;
     607       17457 :     deadzone = hTcxCfg->sq_rounding;
     608       17457 :     deadzone_flags = hTcxEnc->memQuantZeros;
     609       17457 :     *signaling_bits = 0;
     610       17457 :     assert( st->enableTcxLpc );
     611       17457 :     gamma_w = 1.0f;
     612       17457 :     gamma_uw = 1.0f / st->gamma;
     613             : 
     614             : #define WMC_TOOL_SKIP
     615       17457 :     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    11009457 :     for ( k = 0; k < L_spec; ++k )
     619             :     {
     620    10992000 :         if ( spectrum[k] < 0 )
     621             :         {
     622     1820604 :             spectrum[k] = -spectrum[k];
     623     1820604 :             signs[k] = 1;
     624             :         }
     625             :         else
     626             :         {
     627     9171396 :             signs[k] = 0;
     628             :         }
     629             :     }
     630             : 
     631       17457 :     if ( use_hm )
     632             :     {
     633       15771 :         tcx_hm_analyse( spectrum, L_spec, env, target_bits, hTcxCfg->coder_type, prm_hm, tcxltp_pitch, hTcxEnc->tcxltp_gain, &hm_bits );
     634             : 
     635       15771 :         target_bits -= hm_bits;
     636       15771 :         *signaling_bits += hm_bits;
     637             :     }
     638             :     else
     639             :     {
     640        1686 :         prm_hm[0] = 0; /* just to be sure */
     641        1686 :         hm_bits = 0;
     642             :     }
     643             : 
     644       17457 :     L_spec_core = L_spec;
     645       17457 :     if ( st->igf )
     646             :     {
     647       17457 :         L_spec_core = min( L_spec_core, st->hIGFEnc->infoStartLine );
     648             :     }
     649       17457 :     envelope = (Word16 *) env;
     650       17457 :     tcx_arith_scale_envelope( L_spec, L_spec_core, env, target_bits, low_complexity, envelope, &envelope_e );
     651             : 
     652             : #define WMC_TOOL_SKIP
     653       17457 :     tmp = sub( envelope_e, 1 );
     654    11009457 :     FOR( k = 0; k < L_spec; k++ )
     655             :     {
     656    10992000 :         exponents[k] = expfp( negate( envelope[k] ), tmp );
     657             :     }
     658             : #undef WMC_TOOL_SKIP
     659       17457 :     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       17457 :     kMax = tcx_arith_find_kMax( spectrum, L_spec, scale, deadzone, deadzone_flags );
     663             : 
     664       17457 :     q_spectrum = env; /* Reuse buffer */
     665     2211761 :     for ( k = 0; k <= kMax; ++k )
     666             :     {
     667             :         /* quantise using dead-zone */
     668     2194304 :         q_spectrum[k] = (int32_t) ( spectrum[k] * scale + deadzone );
     669             :     }
     670             : 
     671             :     /* Final encoding */
     672       17457 :     *arith_bits = tcx_arith_encode( q_spectrum, signs, kMax, L_spec, exponents, target_bits, prm );
     673             : 
     674             :     /* Multiply back the signs */
     675     2211761 :     for ( k = 0; k <= kMax; ++k )
     676             :     {
     677     2194304 :         spectrum[k] = (float) ( q_spectrum[k] * ( 1 - 2 * signs[k] ) );
     678             :     }
     679     8815153 :     for ( ; k < max( L_frame, L_spec ); ++k )
     680             :     {
     681     8797696 :         spectrum[k] = 0;
     682             :     }
     683             : 
     684       17457 :     return;
     685             : }

Generated by: LCOV version 1.14