LCOV - code coverage report
Current view: top level - lib_isar - isar_lcld_encoder.c (source / functions) Hit Total Coverage
Test: Coverage on main -- conformance test test_26252.py @ a21f94bc6bac334fe001a5bad2f7b32b79038097 Lines: 629 709 88.7 %
Date: 2025-11-01 05:07:43 Functions: 16 17 94.1 %

          Line data    Source code
       1             : /******************************************************************************************************
       2             : 
       3             :    (C) 2022-2025 IVAS codec Public Collaboration with portions copyright Dolby International AB, Ericsson AB,
       4             :    Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
       5             :    Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
       6             :    Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
       7             :    contributors to this repository. All Rights Reserved.
       8             : 
       9             :    This software is protected by copyright law and by international treaties.
      10             :    The IVAS codec Public Collaboration consisting of Dolby International AB, Ericsson AB,
      11             :    Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
      12             :    Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
      13             :    Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
      14             :    contributors to this repository retain full ownership rights in their respective contributions in
      15             :    the software. This notice grants no license of any kind, including but not limited to patent
      16             :    license, nor is any license granted by implication, estoppel or otherwise.
      17             : 
      18             :    Contributors are required to enter into the IVAS codec Public Collaboration agreement before making
      19             :    contributions.
      20             : 
      21             :    This software is provided "AS IS", without any express or implied warranties. The software is in the
      22             :    development stage. It is intended exclusively for experts who have experience with such software and
      23             :    solely for the purpose of inspection. All implied warranties of non-infringement, merchantability
      24             :    and fitness for a particular purpose are hereby disclaimed and excluded.
      25             : 
      26             :    Any dispute, controversy or claim arising under or in relation to providing this software shall be
      27             :    submitted to and settled by the final, binding jurisdiction of the courts of Munich, Germany in
      28             :    accordance with the laws of the Federal Republic of Germany excluding its conflict of law rules and
      29             :    the United Nations Convention on Contracts on the International Sales of Goods.
      30             : 
      31             : *******************************************************************************************************/
      32             : 
      33             : #include <stdint.h>
      34             : #include "options.h"
      35             : #include <math.h>
      36             : #include <assert.h>
      37             : #include "prot.h"
      38             : #include "isar_prot.h"
      39             : #include "wmc_auto.h"
      40             : 
      41             : /*------------------------------------------------------------------------------------------*
      42             :  * Local structures
      43             :  *------------------------------------------------------------------------------------------*/
      44             : 
      45             : struct LCLD_ENCODER
      46             : {
      47             :     int32_t iSampleRate;
      48             :     int32_t iChannels;
      49             :     int32_t iNumBlocks;
      50             : 
      51             :     int32_t iTargetBitRate;
      52             : 
      53             :     int32_t iNumBands;
      54             :     const int32_t *piBandwidths;
      55             : 
      56             :     int32_t iMSMode;
      57             :     int32_t *piMSFlags;
      58             :     int32_t piMSPredCoefs[MAX_BANDS];
      59             :     int32_t piLRPhaseDiffs[MAX_BANDS];
      60             :     int32_t iAllowSidePred;
      61             : 
      62             :     int32_t iRealOnlyOut;
      63             : 
      64             :     RMSEnvelopeGrouping *psRMSEnvelopeGrouping;
      65             : 
      66             :     int32_t iCommonGrouping;
      67             :     int32_t *piNumGroups;
      68             :     int32_t **ppiGroupLengths;
      69             : 
      70             :     int32_t ***pppiRMSEnvelope;
      71             :     int32_t ***pppiSMR;
      72             :     int32_t ***pppiExcitation;
      73             :     int32_t ***pppiAlloc;
      74             : 
      75             :     int32_t iAllocOffset;
      76             : 
      77             :     int32_t ***pppiLCLDSignReal;
      78             :     int32_t ***pppiLCLDSignImag;
      79             :     int32_t ***pppiQLCLDReal;
      80             :     int32_t ***pppiQLCLDImag;
      81             : 
      82             :     PredictionEncoder *psPredictionEncoder;
      83             : };
      84             : 
      85             : 
      86             : /*------------------------------------------------------------------------------------------*
      87             :  * Function Quantize()
      88             :  *
      89             :  *
      90             :  *------------------------------------------------------------------------------------------*/
      91             : 
      92  3894339200 : static int32_t Quantize(
      93             :     const float fVal,
      94             :     const float fScale,
      95             :     int32_t *iSign,
      96             :     const int32_t iMaxVal )
      97             : {
      98             :     int32_t iVal;
      99             : 
     100  3894339200 :     if ( fVal > 0.0f )
     101             :     {
     102  1945060990 :         iVal = (int32_t) ( fScale * fVal + 0.5f );
     103  1945060990 :         *iSign = 0;
     104             :     }
     105             :     else
     106             :     {
     107  1949278210 :         iVal = (int32_t) ( -fScale * fVal + 0.5f );
     108  1949278210 :         *iSign = 1;
     109             :     }
     110  3894339200 :     iVal = ( iVal < iMaxVal ) ? iVal : iMaxVal;
     111             : 
     112  3894339200 :     return iVal;
     113             : }
     114             : 
     115             : 
     116             : /*------------------------------------------------------------------------------------------*
     117             :  * Function UnQuantize()
     118             :  *
     119             :  *
     120             :  *------------------------------------------------------------------------------------------*/
     121             : 
     122   222548712 : static float UnQuantize(
     123             :     const int32_t iVal,
     124             :     const float fScale,
     125             :     const int32_t iSign )
     126             : {
     127             :     float fVal;
     128             : 
     129   222548712 :     if ( iSign == 0 )
     130             :     {
     131   111296093 :         fVal = fScale * (float) iVal;
     132             :     }
     133             :     else
     134             :     {
     135   111252619 :         fVal = -fScale * (float) iVal;
     136             :     }
     137             : 
     138   222548712 :     return fVal;
     139             : }
     140             : 
     141             : 
     142           0 : static void PackReal(
     143             :     const int32_t iChannels,
     144             :     const int32_t iNumBlocks,
     145             :     float ***pppfReal,
     146             :     float ***pppfImag )
     147             : {
     148             :     int32_t ch, b, n;
     149             : 
     150           0 :     for ( ch = 0; ch < iChannels; ch++ )
     151             :     {
     152           0 :         for ( b = 0; b < LCLD_BANDS; b++ )
     153             :         {
     154           0 :             int32_t iRealBlock = 0;
     155           0 :             for ( n = 0; n < iNumBlocks; n += 2 )
     156             :             {
     157           0 :                 pppfImag[ch][iRealBlock][b] = pppfReal[ch][n + 1][b];
     158           0 :                 pppfReal[ch][iRealBlock][b] = pppfReal[ch][n][b];
     159           0 :                 iRealBlock++;
     160             :             }
     161             :         }
     162             :     }
     163             : 
     164           0 :     return;
     165             : }
     166             : 
     167             : /*------------------------------------------------------------------------------------------*
     168             :  * Function CreateLCLDEncoder()
     169             :  *
     170             :  *
     171             :  *------------------------------------------------------------------------------------------*/
     172             : 
     173         664 : ivas_error CreateLCLDEncoder(
     174             :     LCLDEncoder **psLCLDEncoder_out,
     175             :     const int32_t iSampleRate,
     176             :     const int32_t iChannels,
     177             :     const int32_t iTargetBitRate,
     178             :     const int32_t iAllowSidePred,
     179             :     const int16_t iNumBlocks,
     180             :     const int16_t iNumSubSets,
     181             :     const int32_t iRealOnlyOut )
     182             : {
     183             :     int32_t n;
     184             :     LCLDEncoder *psLCLDEncoder;
     185             :     ivas_error error;
     186         664 :     int32_t iMaxNumPredBands = 0;
     187             : 
     188         664 :     assert( iSampleRate == 48000 );
     189         664 :     assert( iNumBlocks == 16 || iNumBlocks == 8 || iNumBlocks == 4 );
     190         664 :     assert( iNumSubSets > 0 && iNumSubSets <= LCLD_MAX_NUM_PRED_SUBSETS );
     191             : 
     192         664 :     if ( ( psLCLDEncoder = (LCLDEncoder *) malloc( sizeof( LCLDEncoder ) ) ) == NULL )
     193             :     {
     194           0 :         return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD encoder Module \n" ) );
     195             :     }
     196             : 
     197         664 :     psLCLDEncoder->iSampleRate = iSampleRate;
     198         664 :     psLCLDEncoder->iChannels = iChannels;
     199         664 :     psLCLDEncoder->iRealOnlyOut = iRealOnlyOut;
     200         664 :     psLCLDEncoder->iAllocOffset = 0;
     201             : 
     202         664 :     psLCLDEncoder->iTargetBitRate = iTargetBitRate;
     203             : 
     204         664 :     psLCLDEncoder->piBandwidths = c_aiBandwidths48;
     205         664 :     psLCLDEncoder->iNumBands = DEF_BANDS_48; /* 22 bands = 50 CLDFB bands (rather than 23 bands) */
     206         664 :     iMaxNumPredBands = min( c_aiNumLcldBandsPerBand[psLCLDEncoder->iNumBands - 1], 50 );
     207         664 :     if ( iRealOnlyOut == 1 )
     208             :     {
     209           0 :         iMaxNumPredBands = 0;
     210           0 :         assert( iNumSubSets == 1 );
     211           0 :         psLCLDEncoder->iNumBlocks = iNumBlocks / 2;
     212             :     }
     213             :     else
     214             :     {
     215         664 :         psLCLDEncoder->iNumBlocks = iNumBlocks;
     216             :     }
     217         664 :     psLCLDEncoder->iMSMode = 0;
     218             : 
     219         664 :     if ( ( psLCLDEncoder->piMSFlags = (int32_t *) malloc( MAX_BANDS * sizeof( int32_t ) ) ) == NULL )
     220             :     {
     221           0 :         return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD encoder Module \n" ) );
     222             :     }
     223             : 
     224       15936 :     for ( n = 0; n < MAX_BANDS; n++ )
     225             :     {
     226       15272 :         psLCLDEncoder->piLRPhaseDiffs[n] = 0;
     227       15272 :         psLCLDEncoder->piMSPredCoefs[n] = 0;
     228             :     }
     229         664 :     psLCLDEncoder->iAllowSidePred = iAllowSidePred;
     230         664 :     psLCLDEncoder->psRMSEnvelopeGrouping = CreateRMSEnvelopeGrouping( psLCLDEncoder->iNumBlocks );
     231             : 
     232         664 :     psLCLDEncoder->iCommonGrouping = 1; /*Common grouping always on only impacts stereo */
     233         664 :     if ( ( psLCLDEncoder->piNumGroups = (int32_t *) malloc( psLCLDEncoder->iChannels * sizeof( int32_t ) ) ) == NULL )
     234             :     {
     235           0 :         return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD encoder Module \n" ) );
     236             :     }
     237             : 
     238         664 :     if ( ( psLCLDEncoder->ppiGroupLengths = (int32_t **) malloc( psLCLDEncoder->iChannels * sizeof( int32_t * ) ) ) == NULL )
     239             :     {
     240           0 :         return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD encoder Module \n" ) );
     241             :     }
     242             : 
     243         664 :     if ( ( psLCLDEncoder->pppiRMSEnvelope = (int32_t ***) malloc( psLCLDEncoder->iChannels * sizeof( int32_t ** ) ) ) == NULL )
     244             :     {
     245           0 :         return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD encoder Module \n" ) );
     246             :     }
     247             : 
     248         664 :     if ( ( psLCLDEncoder->pppiSMR = (int32_t ***) malloc( psLCLDEncoder->iChannels * sizeof( int32_t ** ) ) ) == NULL )
     249             :     {
     250           0 :         return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD encoder Module \n" ) );
     251             :     }
     252             : 
     253         664 :     if ( ( psLCLDEncoder->pppiExcitation = (int32_t ***) malloc( psLCLDEncoder->iChannels * sizeof( int32_t ** ) ) ) == NULL )
     254             :     {
     255           0 :         return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD encoder Module \n" ) );
     256             :     }
     257             : 
     258         664 :     if ( ( psLCLDEncoder->pppiAlloc = (int32_t ***) malloc( psLCLDEncoder->iChannels * sizeof( int32_t ** ) ) ) == NULL )
     259             :     {
     260           0 :         return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD encoder Module \n" ) );
     261             :     }
     262             : 
     263         664 :     if ( ( psLCLDEncoder->pppiLCLDSignReal = (int32_t ***) malloc( psLCLDEncoder->iChannels * sizeof( int32_t ** ) ) ) == NULL )
     264             :     {
     265           0 :         return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD encoder Module \n" ) );
     266             :     }
     267             : 
     268         664 :     if ( ( psLCLDEncoder->pppiLCLDSignImag = (int32_t ***) malloc( psLCLDEncoder->iChannels * sizeof( int32_t ** ) ) ) == NULL )
     269             :     {
     270           0 :         return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD encoder Module \n" ) );
     271             :     }
     272             : 
     273         664 :     if ( ( psLCLDEncoder->pppiQLCLDReal = (int32_t ***) malloc( psLCLDEncoder->iChannels * sizeof( int32_t ** ) ) ) == NULL )
     274             :     {
     275           0 :         return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD encoder Module \n" ) );
     276             :     }
     277             : 
     278         664 :     if ( ( psLCLDEncoder->pppiQLCLDImag = (int32_t ***) malloc( psLCLDEncoder->iChannels * sizeof( int32_t ** ) ) ) == NULL )
     279             :     {
     280           0 :         return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD encoder Module \n" ) );
     281             :     }
     282             : 
     283        1992 :     for ( n = 0; n < iChannels; n++ )
     284             :     {
     285             :         int32_t k;
     286        1328 :         if ( ( psLCLDEncoder->ppiGroupLengths[n] = (int32_t *) malloc( LCLD_BLOCKS_PER_FRAME * sizeof( int32_t ) ) ) == NULL )
     287             :         {
     288           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD encoder Module \n" ) );
     289             :         }
     290             : 
     291        1328 :         if ( ( psLCLDEncoder->pppiRMSEnvelope[n] = (int32_t **) malloc( LCLD_BLOCKS_PER_FRAME * sizeof( int32_t * ) ) ) == NULL )
     292             :         {
     293           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD encoder Module \n" ) );
     294             :         }
     295             : 
     296        1328 :         if ( ( psLCLDEncoder->pppiSMR[n] = (int32_t **) malloc( LCLD_BLOCKS_PER_FRAME * sizeof( int32_t * ) ) ) == NULL )
     297             :         {
     298           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD encoder Module \n" ) );
     299             :         }
     300             : 
     301        1328 :         if ( ( psLCLDEncoder->pppiExcitation[n] = (int32_t **) malloc( LCLD_BLOCKS_PER_FRAME * sizeof( int32_t * ) ) ) == NULL )
     302             :         {
     303           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD encoder Module \n" ) );
     304             :         }
     305             : 
     306        1328 :         if ( ( psLCLDEncoder->pppiAlloc[n] = (int32_t **) malloc( LCLD_BLOCKS_PER_FRAME * sizeof( int32_t * ) ) ) == NULL )
     307             :         {
     308           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD encoder Module \n" ) );
     309             :         }
     310             : 
     311             : 
     312        1328 :         if ( ( psLCLDEncoder->pppiLCLDSignReal[n] = (int32_t **) malloc( LCLD_BLOCKS_PER_FRAME * sizeof( int32_t * ) ) ) == NULL )
     313             :         {
     314           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD encoder Module \n" ) );
     315             :         }
     316             : 
     317        1328 :         if ( ( psLCLDEncoder->pppiLCLDSignImag[n] = (int32_t **) malloc( LCLD_BLOCKS_PER_FRAME * sizeof( int32_t * ) ) ) == NULL )
     318             :         {
     319           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD encoder Module \n" ) );
     320             :         }
     321             : 
     322        1328 :         if ( ( psLCLDEncoder->pppiQLCLDReal[n] = (int32_t **) malloc( LCLD_BLOCKS_PER_FRAME * sizeof( int32_t * ) ) ) == NULL )
     323             :         {
     324           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD encoder Module \n" ) );
     325             :         }
     326             : 
     327        1328 :         if ( ( psLCLDEncoder->pppiQLCLDImag[n] = (int32_t **) malloc( LCLD_BLOCKS_PER_FRAME * sizeof( int32_t * ) ) ) == NULL )
     328             :         {
     329           0 :             return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD encoder Module \n" ) );
     330             :         }
     331             : 
     332       22576 :         for ( k = 0; k < LCLD_BLOCKS_PER_FRAME; k++ )
     333             :         {
     334       21248 :             if ( ( psLCLDEncoder->pppiRMSEnvelope[n][k] = (int32_t *) malloc( MAX_BANDS * sizeof( int32_t ) ) ) == NULL )
     335             :             {
     336           0 :                 return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD encoder Module \n" ) );
     337             :             }
     338             : 
     339       21248 :             if ( ( psLCLDEncoder->pppiSMR[n][k] = (int32_t *) malloc( MAX_BANDS * sizeof( int32_t ) ) ) == NULL )
     340             :             {
     341           0 :                 return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD encoder Module \n" ) );
     342             :             }
     343             : 
     344       21248 :             if ( ( psLCLDEncoder->pppiExcitation[n][k] = (int32_t *) malloc( MAX_BANDS * sizeof( int32_t ) ) ) == NULL )
     345             :             {
     346           0 :                 return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD encoder Module \n" ) );
     347             :             }
     348             : 
     349       21248 :             if ( ( psLCLDEncoder->pppiAlloc[n][k] = (int32_t *) malloc( MAX_BANDS * sizeof( int32_t ) ) ) == NULL )
     350             :             {
     351           0 :                 return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD encoder Module \n" ) );
     352             :             }
     353             : 
     354             : 
     355       21248 :             if ( ( psLCLDEncoder->pppiLCLDSignReal[n][k] = (int32_t *) malloc( LCLD_BANDS * sizeof( int32_t ) ) ) == NULL )
     356             :             {
     357           0 :                 return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD encoder Module \n" ) );
     358             :             }
     359             : 
     360       21248 :             if ( ( psLCLDEncoder->pppiLCLDSignImag[n][k] = (int32_t *) malloc( LCLD_BANDS * sizeof( int32_t ) ) ) == NULL )
     361             :             {
     362           0 :                 return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD encoder Module \n" ) );
     363             :             }
     364             : 
     365       21248 :             if ( ( psLCLDEncoder->pppiQLCLDReal[n][k] = (int32_t *) malloc( LCLD_BANDS * sizeof( int32_t ) ) ) == NULL )
     366             :             {
     367           0 :                 return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD encoder Module \n" ) );
     368             :             }
     369             : 
     370       21248 :             if ( ( psLCLDEncoder->pppiQLCLDImag[n][k] = (int32_t *) malloc( LCLD_BANDS * sizeof( int32_t ) ) ) == NULL )
     371             :             {
     372           0 :                 return ( IVAS_ERROR( IVAS_ERR_FAILED_ALLOC, "Can not allocate memory for LCLD encoder Module \n" ) );
     373             :             }
     374             :         }
     375             :     }
     376             : 
     377         664 :     if ( ( error = CreatePredictionEncoder( &( psLCLDEncoder->psPredictionEncoder ), iChannels, psLCLDEncoder->iNumBlocks, (int32_t) iNumSubSets, iMaxNumPredBands ) ) != IVAS_ERR_OK )
     378             :     {
     379           0 :         return error;
     380             :     }
     381             : 
     382         664 :     *psLCLDEncoder_out = psLCLDEncoder;
     383             : 
     384         664 :     return IVAS_ERR_OK;
     385             : }
     386             : 
     387             : 
     388             : /*------------------------------------------------------------------------------------------*
     389             :  * Function DeleteLCLDEncoder()
     390             :  *
     391             :  *
     392             :  *------------------------------------------------------------------------------------------*/
     393             : 
     394         664 : void DeleteLCLDEncoder(
     395             :     LCLDEncoder *psLCLDEncoder )
     396             : {
     397             :     int32_t k, n;
     398             : 
     399         664 :     if ( psLCLDEncoder != NULL )
     400             :     {
     401             : 
     402         664 :         if ( psLCLDEncoder->piMSFlags != NULL )
     403             :         {
     404         664 :             free( psLCLDEncoder->piMSFlags );
     405             :         }
     406             : 
     407         664 :         if ( psLCLDEncoder->piNumGroups != NULL )
     408             :         {
     409         664 :             free( psLCLDEncoder->piNumGroups );
     410             :         }
     411             : 
     412         664 :         if ( psLCLDEncoder->psRMSEnvelopeGrouping != NULL )
     413             :         {
     414         664 :             DeleteRMSEnvelopeGrouping( psLCLDEncoder->psRMSEnvelopeGrouping );
     415             :         }
     416             : 
     417         664 :         if ( psLCLDEncoder->ppiGroupLengths != NULL )
     418             :         {
     419        1992 :             for ( n = 0; n < psLCLDEncoder->iChannels; n++ )
     420             :             {
     421        1328 :                 free( psLCLDEncoder->ppiGroupLengths[n] );
     422             :             }
     423         664 :             free( psLCLDEncoder->ppiGroupLengths );
     424             :         }
     425             : 
     426         664 :         if ( psLCLDEncoder->pppiRMSEnvelope != NULL )
     427             :         {
     428        1992 :             for ( n = 0; n < psLCLDEncoder->iChannels; n++ )
     429             :             {
     430       22576 :                 for ( k = 0; k < LCLD_BLOCKS_PER_FRAME; k++ )
     431             :                 {
     432       21248 :                     free( psLCLDEncoder->pppiRMSEnvelope[n][k] );
     433             :                 }
     434        1328 :                 free( psLCLDEncoder->pppiRMSEnvelope[n] );
     435             :             }
     436         664 :             free( psLCLDEncoder->pppiRMSEnvelope );
     437             :         }
     438             : 
     439         664 :         if ( psLCLDEncoder->pppiSMR != NULL )
     440             :         {
     441        1992 :             for ( n = 0; n < psLCLDEncoder->iChannels; n++ )
     442             :             {
     443       22576 :                 for ( k = 0; k < LCLD_BLOCKS_PER_FRAME; k++ )
     444             :                 {
     445       21248 :                     free( psLCLDEncoder->pppiSMR[n][k] );
     446             :                 }
     447        1328 :                 free( psLCLDEncoder->pppiSMR[n] );
     448             :             }
     449         664 :             free( psLCLDEncoder->pppiSMR );
     450             :         }
     451             : 
     452         664 :         if ( psLCLDEncoder->pppiExcitation != NULL )
     453             :         {
     454        1992 :             for ( n = 0; n < psLCLDEncoder->iChannels; n++ )
     455             :             {
     456       22576 :                 for ( k = 0; k < LCLD_BLOCKS_PER_FRAME; k++ )
     457             :                 {
     458       21248 :                     free( psLCLDEncoder->pppiExcitation[n][k] );
     459             :                 }
     460        1328 :                 free( psLCLDEncoder->pppiExcitation[n] );
     461             :             }
     462         664 :             free( psLCLDEncoder->pppiExcitation );
     463             :         }
     464             : 
     465         664 :         if ( psLCLDEncoder->pppiAlloc != NULL )
     466             :         {
     467        1992 :             for ( n = 0; n < psLCLDEncoder->iChannels; n++ )
     468             :             {
     469       22576 :                 for ( k = 0; k < LCLD_BLOCKS_PER_FRAME; k++ )
     470             :                 {
     471       21248 :                     free( psLCLDEncoder->pppiAlloc[n][k] );
     472             :                 }
     473        1328 :                 free( psLCLDEncoder->pppiAlloc[n] );
     474             :             }
     475         664 :             free( psLCLDEncoder->pppiAlloc );
     476             :         }
     477             : 
     478         664 :         if ( psLCLDEncoder->pppiLCLDSignReal != NULL )
     479             :         {
     480        1992 :             for ( n = 0; n < psLCLDEncoder->iChannels; n++ )
     481             :             {
     482       22576 :                 for ( k = 0; k < LCLD_BLOCKS_PER_FRAME; k++ )
     483             :                 {
     484       21248 :                     free( psLCLDEncoder->pppiLCLDSignReal[n][k] );
     485             :                 }
     486        1328 :                 free( psLCLDEncoder->pppiLCLDSignReal[n] );
     487             :             }
     488         664 :             free( psLCLDEncoder->pppiLCLDSignReal );
     489             :         }
     490             : 
     491         664 :         if ( psLCLDEncoder->pppiLCLDSignImag != NULL )
     492             :         {
     493        1992 :             for ( n = 0; n < psLCLDEncoder->iChannels; n++ )
     494             :             {
     495       22576 :                 for ( k = 0; k < LCLD_BLOCKS_PER_FRAME; k++ )
     496             :                 {
     497       21248 :                     free( psLCLDEncoder->pppiLCLDSignImag[n][k] );
     498             :                 }
     499        1328 :                 free( psLCLDEncoder->pppiLCLDSignImag[n] );
     500             :             }
     501         664 :             free( psLCLDEncoder->pppiLCLDSignImag );
     502             :         }
     503             : 
     504         664 :         if ( psLCLDEncoder->pppiQLCLDReal != NULL )
     505             :         {
     506        1992 :             for ( n = 0; n < psLCLDEncoder->iChannels; n++ )
     507             :             {
     508       22576 :                 for ( k = 0; k < LCLD_BLOCKS_PER_FRAME; k++ )
     509             :                 {
     510       21248 :                     free( psLCLDEncoder->pppiQLCLDReal[n][k] );
     511             :                 }
     512        1328 :                 free( psLCLDEncoder->pppiQLCLDReal[n] );
     513             :             }
     514         664 :             free( psLCLDEncoder->pppiQLCLDReal );
     515             :         }
     516             : 
     517         664 :         if ( psLCLDEncoder->pppiQLCLDImag != NULL )
     518             :         {
     519        1992 :             for ( n = 0; n < psLCLDEncoder->iChannels; n++ )
     520             :             {
     521       22576 :                 for ( k = 0; k < LCLD_BLOCKS_PER_FRAME; k++ )
     522             :                 {
     523       21248 :                     free( psLCLDEncoder->pppiQLCLDImag[n][k] );
     524             :                 }
     525        1328 :                 free( psLCLDEncoder->pppiQLCLDImag[n] );
     526             :             }
     527         664 :             free( psLCLDEncoder->pppiQLCLDImag );
     528             :         }
     529             : 
     530         664 :         DeletePredictionEncoder( psLCLDEncoder->psPredictionEncoder );
     531         664 :         free( psLCLDEncoder );
     532             :     }
     533             : 
     534         664 :     return;
     535             : }
     536             : 
     537             : 
     538             : /*------------------------------------------------------------------------------------------*
     539             :  * Local function declarations
     540             :  *------------------------------------------------------------------------------------------*/
     541             : 
     542             : static int32_t MSModeCalculation( const int32_t iNumBlocks, const int32_t iNumBands, const int32_t *piBandwidths, float ***pppfReal, float ***pppfImag, int32_t *piMSMode, int32_t *piLRPhaseDiff, int32_t *piMSPredCoef, const int32_t iAllowSidePred, const int32_t iRealOnlyOut, int32_t *piMSFlags );
     543             : 
     544             : static void RemoveRMSEnvelope( const int32_t iNumBands, const int32_t *piBandwidths, const int32_t iNumGroups, const int32_t *piGroupLengths, int32_t **ppiRMSEnvelope, float **ppfReal, float **ppfImag );
     545             : 
     546             : static int32_t CountLCLDBits( const int32_t iNumGroups, const int32_t *piGroupLengths, const int32_t iNumBands, const int32_t *piBandwidths, const int32_t *piPredEnable, int32_t **ppiAlloc, int32_t **ppiQReal, int32_t **ppiQImag );
     547             : 
     548             : static int32_t WriteHeaderInformation( const int32_t iNumBands, ISAR_SPLIT_REND_BITS_HANDLE pBits );
     549             : 
     550             : static int32_t WriteMSInformation( const int32_t iNumBands, const int32_t iMSMode, const int32_t *piMSFlags, const int32_t *piLRPhaseDiffs, const int32_t *piMSPredCoefs, const int32_t iNumMSPredBands, ISAR_SPLIT_REND_BITS_HANDLE pBits );
     551             : 
     552             : static int32_t WriteGroupInformation( const int32_t iChannels, const int32_t iCommonGrouping, const int32_t *piNumGroups, int32_t **ppiGroupLengths, ISAR_SPLIT_REND_BITS_HANDLE pBits );
     553             : 
     554             : static int32_t WriteRMSEnvelope( const int32_t iChannels, const int32_t *piNumGroups, const int32_t iNumBands, int32_t ***pppiRMSEnvelope, ISAR_SPLIT_REND_BITS_HANDLE pBits );
     555             : 
     556             : static int32_t WriteAllocInformation( const int32_t iAllocOffset, ISAR_SPLIT_REND_BITS_HANDLE pBits );
     557             : 
     558             : static int32_t WriteLCLDData( const int32_t *piNumGroups, int32_t **ppiGroupLengths, const int32_t iNumBands, const int32_t iNumChannels, int32_t **ppiPredEnable, const int32_t iNumSubSets, const int32_t iSubSetId, int32_t ***pppiAlloc, int32_t ***pppiSignReal, int32_t ***pppiSignImag, int32_t ***pppiQReal, int32_t ***pppiQImag, ISAR_SPLIT_REND_BITS_HANDLE pBits );
     559             : 
     560             : static int32_t ComputeAllocation( const int32_t iChannels, const int32_t *piNumGroups, int32_t **ppiGroupLengths, const int32_t iNumBands, const int32_t *piBandwidths, float ***pppfReal, float ***pppfImag, int32_t ***pppiSMR, const int32_t iAvailableBits, int32_t *piAllocOffset, int32_t ***pppiAlloc, int32_t ***pppiQReal, int32_t ***pppiQImag, int32_t ***pppiSignReal, int32_t ***pppiSignImag, PredictionEncoder *psPredictionEncoder );
     561             : 
     562             : 
     563             : /*------------------------------------------------------------------------------------------*
     564             :  * Function EncodeLCLDFrame()
     565             :  *
     566             :  *
     567             :  *------------------------------------------------------------------------------------------*/
     568             : 
     569      133227 : int32_t EncodeLCLDFrame(
     570             :     LCLDEncoder *psLCLDEncoder,
     571             :     float ***pppfLCLDReal,
     572             :     float ***pppfLCLDImag,
     573             :     int32_t *piBitsWritten,
     574             :     const int32_t available_bits,
     575             :     ISAR_SPLIT_REND_BITS_HANDLE pBits )
     576             : {
     577             :     int32_t k, n;
     578             :     int32_t iAvailableBits, iBitsWritten;
     579      133227 :     int32_t iNumMSBands = 0;
     580             :     int32_t iAudioBitsWritten;
     581             : 
     582      133227 :     iAvailableBits = available_bits; /* HCBR for now*/
     583      133227 :     iBitsWritten = 0;
     584      133227 :     assert( available_bits <= pBits->buf_len * 8 );
     585             : 
     586      133227 :     if ( psLCLDEncoder->iRealOnlyOut == 1 )
     587             :     {
     588           0 :         PackReal( psLCLDEncoder->iChannels, psLCLDEncoder->iNumBlocks * 2, pppfLCLDReal, pppfLCLDImag );
     589             :     }
     590             : 
     591             :     /* Do MS calc here */
     592      133227 :     if ( psLCLDEncoder->iChannels == 2 )
     593             :     {
     594      133227 :         iNumMSBands = MSModeCalculation( psLCLDEncoder->iNumBlocks,
     595             :                                          psLCLDEncoder->iNumBands,
     596             :                                          psLCLDEncoder->piBandwidths,
     597             :                                          pppfLCLDReal,
     598             :                                          pppfLCLDImag,
     599             :                                          &psLCLDEncoder->iMSMode,
     600      133227 :                                          psLCLDEncoder->piLRPhaseDiffs,
     601      133227 :                                          psLCLDEncoder->piMSPredCoefs,
     602             :                                          psLCLDEncoder->iAllowSidePred,
     603             :                                          psLCLDEncoder->iRealOnlyOut,
     604             :                                          psLCLDEncoder->piMSFlags );
     605             : 
     606      133227 :         if ( psLCLDEncoder->iMSMode > 0 )
     607             :         {
     608      133216 :             psLCLDEncoder->iCommonGrouping = 1; /* Make sure common grouping is enabled when MS is in use */
     609             :         }
     610             :     }
     611             : 
     612             :     /* Compute Grouping and RMS Envelopes */
     613      133227 :     if ( psLCLDEncoder->iChannels == 2 && psLCLDEncoder->iCommonGrouping == 1 )
     614             :     {
     615      133227 :         ComputeEnvelopeGrouping( psLCLDEncoder->psRMSEnvelopeGrouping,
     616             :                                  psLCLDEncoder->iChannels,
     617             :                                  psLCLDEncoder->iNumBands,
     618             :                                  psLCLDEncoder->piBandwidths,
     619             :                                  pppfLCLDReal,
     620             :                                  pppfLCLDImag,
     621             :                                  &psLCLDEncoder->piNumGroups[0],
     622      133227 :                                  psLCLDEncoder->ppiGroupLengths[0],
     623             :                                  psLCLDEncoder->pppiRMSEnvelope );
     624             : 
     625      133227 :         psLCLDEncoder->piNumGroups[1] = psLCLDEncoder->piNumGroups[0];
     626      331078 :         for ( n = 0; n < psLCLDEncoder->piNumGroups[0]; n++ )
     627             :         {
     628      197851 :             psLCLDEncoder->ppiGroupLengths[1][n] = psLCLDEncoder->ppiGroupLengths[0][n];
     629             :         }
     630             :     }
     631             :     else
     632             :     {
     633           0 :         for ( n = 0; n < psLCLDEncoder->iChannels; n++ )
     634             :         {
     635           0 :             ComputeEnvelopeGrouping( psLCLDEncoder->psRMSEnvelopeGrouping,
     636             :                                      psLCLDEncoder->iChannels,
     637             :                                      psLCLDEncoder->iNumBands,
     638             :                                      psLCLDEncoder->piBandwidths,
     639           0 :                                      &pppfLCLDReal[n],
     640           0 :                                      &pppfLCLDImag[n],
     641           0 :                                      &psLCLDEncoder->piNumGroups[n],
     642           0 :                                      psLCLDEncoder->ppiGroupLengths[n],
     643           0 :                                      &psLCLDEncoder->pppiRMSEnvelope[n] );
     644             :         }
     645             :     }
     646             : 
     647      399681 :     for ( n = 0; n < psLCLDEncoder->iChannels; n++ )
     648             :     {
     649      266454 :         RemoveRMSEnvelope( psLCLDEncoder->iNumBands,
     650             :                            psLCLDEncoder->piBandwidths,
     651      266454 :                            psLCLDEncoder->piNumGroups[n],
     652      266454 :                            (const int32_t *) psLCLDEncoder->ppiGroupLengths[n],
     653      266454 :                            psLCLDEncoder->pppiRMSEnvelope[n],
     654      266454 :                            pppfLCLDReal[n],
     655      266454 :                            pppfLCLDImag[n] );
     656             :     }
     657             : 
     658      133227 :     ComputePredictors( psLCLDEncoder->psPredictionEncoder, pppfLCLDReal, pppfLCLDImag );
     659             : 
     660      133227 :     iBitsWritten += WriteHeaderInformation( psLCLDEncoder->iNumBands, pBits );
     661             : 
     662      133227 :     if ( psLCLDEncoder->iChannels == 2 )
     663             :     {
     664      133227 :         iBitsWritten += WriteMSInformation( psLCLDEncoder->iNumBands,
     665             :                                             psLCLDEncoder->iMSMode,
     666      133227 :                                             (const int32_t *) psLCLDEncoder->piMSFlags,
     667      133227 :                                             (const int32_t *) psLCLDEncoder->piLRPhaseDiffs,
     668      133227 :                                             (const int32_t *) psLCLDEncoder->piMSPredCoefs,
     669             :                                             iNumMSBands,
     670             :                                             pBits );
     671             :     }
     672             : 
     673      133227 :     iBitsWritten += WritePredictors( psLCLDEncoder->psPredictionEncoder, pBits );
     674             : 
     675      133227 :     iBitsWritten += WriteGroupInformation( psLCLDEncoder->iChannels, psLCLDEncoder->iCommonGrouping, (const int32_t *) psLCLDEncoder->piNumGroups, psLCLDEncoder->ppiGroupLengths, pBits );
     676             : 
     677      133227 :     iBitsWritten += WriteRMSEnvelope( psLCLDEncoder->iChannels, (const int32_t *) psLCLDEncoder->piNumGroups, psLCLDEncoder->iNumBands, psLCLDEncoder->pppiRMSEnvelope, pBits );
     678             : 
     679      133227 :     if ( psLCLDEncoder->iChannels == 2 && psLCLDEncoder->iCommonGrouping == 1 )
     680             :     {
     681      331078 :         for ( k = 0; k < psLCLDEncoder->piNumGroups[0]; k++ )
     682             :         {
     683      197851 :             PerceptualModelStereo( psLCLDEncoder->iNumBands,
     684      197851 :                                    psLCLDEncoder->piMSFlags,
     685      197851 :                                    psLCLDEncoder->pppiRMSEnvelope[0][k],
     686      197851 :                                    psLCLDEncoder->pppiRMSEnvelope[1][k],
     687      197851 :                                    psLCLDEncoder->pppiExcitation[0][k],
     688      197851 :                                    psLCLDEncoder->pppiExcitation[1][k],
     689      197851 :                                    psLCLDEncoder->pppiSMR[0][k],
     690      197851 :                                    psLCLDEncoder->pppiSMR[1][k] );
     691             :         }
     692             :     }
     693             :     else
     694             :     {
     695           0 :         for ( n = 0; n < psLCLDEncoder->iChannels; n++ )
     696             :         {
     697           0 :             for ( k = 0; k < psLCLDEncoder->piNumGroups[n]; k++ )
     698             :             {
     699           0 :                 PerceptualModel( psLCLDEncoder->iNumBands,
     700           0 :                                  psLCLDEncoder->pppiRMSEnvelope[n][k],
     701           0 :                                  psLCLDEncoder->pppiExcitation[n][k],
     702           0 :                                  psLCLDEncoder->pppiSMR[n][k] );
     703             :             }
     704             :         }
     705             :     }
     706             : #ifdef DEBUG_WRITE_PREDICTORS
     707             :     {
     708             :         static FILE *fid;
     709             :         if ( !fid )
     710             :             fid = fopen( "pred_enc.txt", "wt" );
     711             :         for ( n = 0; n < psLCLDEncoder->iChannels; n++ )
     712             :         {
     713             :             int16_t b;
     714             :             for ( b = 0; b < 60; b++ )
     715             :                 fprintf( fid, "%.5f ", (float) psLCLDEncoder->psPredictionEncoder->ppiPredBandEnable[n][b] * psLCLDEncoder->psPredictionEncoder->ppfA1Imag[n][b] );
     716             :         }
     717             :         fprintf( fid, "%d %d\n", psLCLDEncoder->psPredictionEncoder->iSubSetId, psLCLDEncoder->psPredictionEncoder->piPredChanEnable[n] );
     718             :     }
     719             : #endif
     720             : 
     721      133227 :     iAvailableBits -= iBitsWritten;
     722      133227 :     ComputeAllocation( psLCLDEncoder->iChannels,
     723      133227 :                        (const int32_t *) psLCLDEncoder->piNumGroups,
     724             :                        psLCLDEncoder->ppiGroupLengths,
     725             :                        psLCLDEncoder->iNumBands,
     726             :                        psLCLDEncoder->piBandwidths,
     727             :                        pppfLCLDReal,
     728             :                        pppfLCLDImag,
     729             :                        psLCLDEncoder->pppiSMR,
     730             :                        iAvailableBits,
     731             :                        &psLCLDEncoder->iAllocOffset,
     732             :                        psLCLDEncoder->pppiAlloc,
     733             :                        psLCLDEncoder->pppiQLCLDReal,
     734             :                        psLCLDEncoder->pppiQLCLDImag,
     735             :                        psLCLDEncoder->pppiLCLDSignReal,
     736             :                        psLCLDEncoder->pppiLCLDSignImag,
     737             :                        psLCLDEncoder->psPredictionEncoder );
     738             : 
     739      133227 :     iBitsWritten += WriteAllocInformation( psLCLDEncoder->iAllocOffset, pBits );
     740             : 
     741      133227 :     iAudioBitsWritten = iBitsWritten;
     742      266454 :     iBitsWritten += WriteLCLDData( psLCLDEncoder->piNumGroups,
     743             :                                    psLCLDEncoder->ppiGroupLengths,
     744             :                                    psLCLDEncoder->iNumBands,
     745             :                                    psLCLDEncoder->iChannels,
     746      133227 :                                    psLCLDEncoder->psPredictionEncoder->ppiPredBandEnable,
     747      133227 :                                    psLCLDEncoder->psPredictionEncoder->iNumSubSets,
     748      133227 :                                    psLCLDEncoder->psPredictionEncoder->iSubSetId,
     749             :                                    psLCLDEncoder->pppiAlloc,
     750             :                                    psLCLDEncoder->pppiLCLDSignReal,
     751             :                                    psLCLDEncoder->pppiLCLDSignImag,
     752             :                                    psLCLDEncoder->pppiQLCLDReal,
     753             :                                    psLCLDEncoder->pppiQLCLDImag,
     754             :                                    pBits );
     755      133227 :     *piBitsWritten = iBitsWritten;
     756      133227 :     iAudioBitsWritten = iBitsWritten - iAudioBitsWritten;
     757             : 
     758      133227 :     UpdatePredictionSubSetId( psLCLDEncoder->psPredictionEncoder );
     759             : 
     760      133227 :     return 0;
     761             : }
     762             : 
     763             : 
     764             : /*------------------------------------------------------------------------------------------*
     765             :  * Local functions
     766             :  *
     767             :  *
     768             :  *------------------------------------------------------------------------------------------*/
     769             : 
     770             : enum MSPred_Types
     771             : {
     772             :     MS_PHASE_AND_PRED = 0, /* LR phase alignment + real-valued M/S prediction */
     773             :     MS_PRED_ONLY = 1,      /* real-valued M/S prediction */
     774             :     MS_PHASE_ONLY = 2      /* LR phase alignment + M/S */
     775             : };
     776             : 
     777             : enum MS_BS_TYPES
     778             : {
     779             :     MS_OFF = 0,
     780             :     MS_ALL = 1,
     781             :     MS_SOME = 2,
     782             :     MS_PRED = 3
     783             : };
     784             : 
     785      133227 : static int32_t MSModeCalculation(
     786             :     const int32_t iNumBlocks,
     787             :     const int32_t iNumBands,
     788             :     const int32_t *piBandwidths,
     789             :     float ***pppfReal,
     790             :     float ***pppfImag,
     791             :     int32_t *piMSMode,
     792             :     int32_t *piLRPhaseDiffs,
     793             :     int32_t *piMSPredCoefs,
     794             :     const int32_t iAllowSidePred,
     795             :     const int32_t iRealOnlyOut,
     796             :     int32_t *piMSFlags )
     797             : {
     798             :     int32_t b;
     799             :     int32_t iFBOffset;
     800             :     int32_t iNumMSBands;
     801             :     int32_t iMSPredType;
     802      133227 :     float fMSBitGain = 0.0f;
     803             :     float pfMSPredBitGain[3];
     804             :     float fPred;
     805             :     int32_t piMSPredFlags0[MAX_BANDS];
     806             :     int32_t piMSPredFlags1[MAX_BANDS];
     807             :     int32_t piMSPredFlags2[MAX_BANDS];
     808             :     int32_t *ppiMSPredFlags[3];
     809             :     int32_t piMSPredCoefs0[MAX_BANDS];
     810             :     int32_t piMSPredCoefs1[MAX_BANDS];
     811             :     int32_t piMSPredCoefs2[MAX_BANDS];
     812             :     int32_t *ppiMSPredCoefs[3];
     813             :     int32_t piMSPredPhase0[MAX_BANDS];
     814             :     int32_t piMSPredPhase1[MAX_BANDS];
     815             :     int32_t piMSPredPhase2[MAX_BANDS];
     816             :     int32_t *ppiMSPredPhase[3];
     817             :     int32_t iMsInfoBits;
     818             :     int32_t piMsPredInfoBits[3];
     819             : 
     820      133227 :     const float feps = 1e-12f;
     821      133227 :     float fBitsFactor = 3.32192809488736f; /* = 1/log10(2), from dB/10 to bits assuming 1 bit per log2(SNR) or 1 bit per 3dB SNR */
     822             : 
     823      133227 :     set_zero( pfMSPredBitGain, 3 );
     824      133227 :     set_l( piMsPredInfoBits, 0, 3 );
     825             : 
     826      133227 :     set_l( piMSPredFlags0, 0, MAX_BANDS );
     827      133227 :     set_l( piMSPredFlags1, 0, MAX_BANDS );
     828      133227 :     set_l( piMSPredFlags2, 0, MAX_BANDS );
     829      133227 :     set_l( piMSPredCoefs0, 0, MAX_BANDS );
     830      133227 :     set_l( piMSPredCoefs1, 0, MAX_BANDS );
     831      133227 :     set_l( piMSPredCoefs2, 0, MAX_BANDS );
     832      133227 :     set_l( piMSPredPhase0, 0, MAX_BANDS );
     833      133227 :     set_l( piMSPredPhase1, 0, MAX_BANDS );
     834      133227 :     set_l( piMSPredPhase2, 0, MAX_BANDS );
     835             : 
     836      133227 :     if ( iNumBlocks < LCLD_BLOCKS_PER_FRAME )
     837             :     {
     838        8049 :         fBitsFactor *= ( 0.7f + (float) ( iNumBlocks - 4 ) / (float) ( LCLD_BLOCKS_PER_FRAME - 4 ) * ( 1.0f - 0.7f ) ); /* Tuning for relatively higher side rate due to shorter frame length */
     839             :     }
     840             : 
     841      133227 :     ppiMSPredFlags[0] = piMSPredFlags0;
     842      133227 :     ppiMSPredFlags[1] = piMSPredFlags1;
     843      133227 :     ppiMSPredFlags[2] = piMSPredFlags2;
     844             : 
     845      133227 :     ppiMSPredCoefs[0] = piMSPredCoefs0;
     846      133227 :     ppiMSPredCoefs[1] = piMSPredCoefs1;
     847      133227 :     ppiMSPredCoefs[2] = piMSPredCoefs2;
     848             : 
     849      133227 :     ppiMSPredPhase[0] = piMSPredPhase0;
     850      133227 :     ppiMSPredPhase[1] = piMSPredPhase1;
     851      133227 :     ppiMSPredPhase[2] = piMSPredPhase2;
     852             : 
     853      133227 :     *piMSMode = MS_OFF;
     854      133227 :     iFBOffset = 0;
     855      133227 :     iNumMSBands = 0;
     856     3064221 :     for ( b = 0; b < iNumBands; b++ )
     857             :     {
     858             :         int32_t n;
     859             :         float fLeftEnergy;
     860             :         float fRightEnergy;
     861             :         float fMidEnergy;
     862             :         float fSideEnergy;
     863             :         float fLRRatio;
     864             :         float fMSRatio;
     865     2930994 :         float pfMSPredRatio[3] = { 0.0f };
     866             :         float fMidEnergyPred;
     867             :         float fSideEnergyPred;
     868     2930994 :         float fLRCovReal = 0.0f;
     869     2930994 :         float fLRCovImag = 0.0f;
     870             :         int32_t iPhase;
     871             :         int32_t iPred;
     872     2930994 :         int32_t tabIdx = 0;
     873     2930994 :         float fNumLines = (float) ( iRealOnlyOut == 1 ? iNumBlocks * piBandwidths[b] * 4 : iNumBlocks * piBandwidths[b] * 2 ); /* per band per channel */
     874     2930994 :         float fLevelToSMRdBFactor = (float) c_aiDefaultTheta48[b] / (float) ( 1 << PERCEPTUAL_MODEL_SLGAIN_SHIFT );            /* frequency dependent SMR slope in psy model */
     875     2930994 :         fLeftEnergy = 0.0f;
     876     2930994 :         fRightEnergy = 0.0f;
     877     2930994 :         fMidEnergy = 0.0f;
     878     2930994 :         fSideEnergy = 0.0f;
     879             : 
     880     9592344 :         for ( n = 0; n < piBandwidths[b]; n++ )
     881             :         {
     882             :             int32_t k;
     883   109218350 :             for ( k = 0; k < iNumBlocks; k++ )
     884             :             {
     885             :                 float fMidReal;
     886             :                 float fMidImag;
     887             :                 float fSideReal;
     888             :                 float fSideImag;
     889             : 
     890   102557000 :                 fMidReal = 0.5f * ( pppfReal[0][k][iFBOffset] + pppfReal[1][k][iFBOffset] );
     891   102557000 :                 fMidImag = 0.5f * ( pppfImag[0][k][iFBOffset] + pppfImag[1][k][iFBOffset] );
     892   102557000 :                 fSideReal = 0.5f * ( pppfReal[0][k][iFBOffset] - pppfReal[1][k][iFBOffset] );
     893   102557000 :                 fSideImag = 0.5f * ( pppfImag[0][k][iFBOffset] - pppfImag[1][k][iFBOffset] );
     894             : 
     895   102557000 :                 fLeftEnergy += ( pppfReal[0][k][iFBOffset] * pppfReal[0][k][iFBOffset] + pppfImag[0][k][iFBOffset] * pppfImag[0][k][iFBOffset] );
     896   102557000 :                 fRightEnergy += ( pppfReal[1][k][iFBOffset] * pppfReal[1][k][iFBOffset] + pppfImag[1][k][iFBOffset] * pppfImag[1][k][iFBOffset] );
     897   102557000 :                 fMidEnergy += ( fMidReal * fMidReal + fMidImag * fMidImag );
     898   102557000 :                 fSideEnergy += ( fSideReal * fSideReal + fSideImag * fSideImag );
     899             : 
     900   102557000 :                 fLRCovReal += ( pppfReal[0][k][iFBOffset] * pppfReal[1][k][iFBOffset] + pppfImag[0][k][iFBOffset] * pppfImag[1][k][iFBOffset] );
     901   102557000 :                 fLRCovImag += ( pppfImag[0][k][iFBOffset] * pppfReal[1][k][iFBOffset] - pppfImag[1][k][iFBOffset] * pppfReal[0][k][iFBOffset] );
     902             :             }
     903             : 
     904     6661350 :             iFBOffset++;
     905             :         }
     906             : 
     907             :         /* M/S prediction without phase alignment*/
     908     2930994 :         fPred = 0.25f * ( fLeftEnergy - fRightEnergy ) / ( fMidEnergy + feps );
     909     2930994 :         iPred = quantPred( fPred );
     910     2930994 :         fPred = dequantPred( iPred );
     911     2930994 :         fSideEnergyPred = fSideEnergy + ( fPred * fPred * fMidEnergy - 2.0f * fPred * 0.25f * ( fLeftEnergy - fRightEnergy ) );
     912             : 
     913     2930994 :         ppiMSPredCoefs[MS_PRED_ONLY][b] = iPred;
     914     2930994 :         ppiMSPredPhase[MS_PRED_ONLY][b] = 0;
     915     2930994 :         pfMSPredRatio[MS_PRED_ONLY] = log10f( ( fMidEnergy + feps ) / ( fSideEnergyPred + feps ) );
     916             : 
     917             :         /* Phase alignment*/
     918     2930994 :         iPhase = 0;
     919     2930994 :         if ( fLRCovReal * fLRCovReal + fLRCovImag * fLRCovImag > 0.5f * fLeftEnergy * fRightEnergy )
     920             :         {
     921     1467850 :             float fPhase = atan2f( fLRCovImag, fLRCovReal );
     922     1467850 :             iPhase = quantPhase( fPhase );
     923             :         }
     924             : 
     925             :         /* adjust covariance */
     926     2930994 :         tabIdx = iPhase - PHASE_MIN_VAL;
     927     2930994 :         cplxmult_lcld( &fLRCovReal, &fLRCovImag, c_afRotRealImag[tabIdx][0], -c_afRotRealImag[tabIdx][1] );
     928             : 
     929             :         /* compute MS prediction coefficient based on adjusted covariance */
     930     2930994 :         fMidEnergyPred = 0.25f * ( fLeftEnergy + fRightEnergy + 2.0f * fLRCovReal );
     931     2930994 :         fSideEnergyPred = 0.25f * ( fLeftEnergy + fRightEnergy - 2.0f * fLRCovReal );
     932             : 
     933             :         /* M/S with LR phase alignment but without prediction */
     934     2930994 :         ppiMSPredCoefs[MS_PHASE_ONLY][b] = 0;
     935     2930994 :         ppiMSPredPhase[MS_PHASE_ONLY][b] = iPhase;
     936     2930994 :         pfMSPredRatio[MS_PHASE_ONLY] = log10f( ( fMidEnergyPred + feps ) / ( fSideEnergyPred + feps ) );
     937             : 
     938             :         /* M/S with LR phase alignment and prediction */
     939     2930994 :         fPred = fMidEnergyPred == 0.0f ? 0.0f : 0.25f * ( fLeftEnergy - fRightEnergy ) / fMidEnergyPred;
     940     2930994 :         iPred = quantPred( fPred );
     941     2930994 :         fPred = dequantPred( iPred );
     942     2930994 :         fSideEnergyPred += ( fPred * fPred * fMidEnergyPred - 2.0f * fPred * 0.25f * ( fLeftEnergy - fRightEnergy ) );
     943             :         /* -= fPred * fPred * fMidEnergyPred doesn't work because fPred is quantized and does not match MS/MM exactly */
     944     2930994 :         ppiMSPredCoefs[MS_PHASE_AND_PRED][b] = iPred;
     945     2930994 :         ppiMSPredPhase[MS_PHASE_AND_PRED][b] = iPhase;
     946     2930994 :         pfMSPredRatio[MS_PHASE_AND_PRED] = log10f( ( fMidEnergyPred + feps ) / ( fSideEnergyPred + feps ) );
     947             : 
     948             :         /* Plain M/S */
     949     2930994 :         fLeftEnergy = log10f( fLeftEnergy + feps );
     950     2930994 :         fRightEnergy = log10f( fRightEnergy + feps );
     951     2930994 :         fMidEnergy = log10f( fMidEnergy + feps );
     952     2930994 :         fSideEnergy = log10f( fSideEnergy + feps );
     953             : 
     954     2930994 :         fLRRatio = ( fLeftEnergy > fRightEnergy ? fLeftEnergy - fRightEnergy : fRightEnergy - fLeftEnergy );
     955     2930994 :         fMSRatio = ( fMidEnergy > fSideEnergy ? fMidEnergy - fSideEnergy : fSideEnergy - fMidEnergy );
     956             : 
     957     2930994 :         if ( fMSRatio > fLRRatio )
     958             :         {
     959     1527711 :             iNumMSBands++;
     960     1527711 :             piMSFlags[b] = 1;
     961     1527711 :             fMSBitGain += fNumLines * ( fMSRatio - fLRRatio ) * fLevelToSMRdBFactor * fBitsFactor;
     962             :         }
     963             :         else
     964             :         {
     965     1403283 :             piMSFlags[b] = 0;
     966             :         }
     967     2930994 :         piLRPhaseDiffs[b] = 0;
     968     2930994 :         piMSPredCoefs[b] = 0;
     969             : 
     970             :         /* MSPred bit gains based on increase of level ratio compared to L/R ratio and the level dependent psy-model */
     971    11723976 :         for ( iMSPredType = 0; iMSPredType < 3; iMSPredType++ )
     972             :         {
     973     8792982 :             if ( pfMSPredRatio[iMSPredType] > fLRRatio )
     974             :             {
     975     5473214 :                 ppiMSPredFlags[iMSPredType][b] = 1;
     976     5473214 :                 pfMSPredBitGain[iMSPredType] += fNumLines * ( pfMSPredRatio[iMSPredType] - fLRRatio ) * fLevelToSMRdBFactor * fBitsFactor;
     977             :             }
     978             :         }
     979             :     }
     980             : 
     981             :     /* remove signalling cost from bit gains  */
     982      532908 :     for ( iMSPredType = 0; iMSPredType < 3; iMSPredType++ )
     983             :     {
     984      399681 :         piMsPredInfoBits[iMSPredType] = CountMSBits( iNumBands, MS_PRED, ppiMSPredFlags[iMSPredType], ppiMSPredPhase[iMSPredType], ppiMSPredCoefs[iMSPredType] );
     985      399681 :         pfMSPredBitGain[iMSPredType] = max( pfMSPredBitGain[iMSPredType] - piMsPredInfoBits[iMSPredType], 0.0f );
     986             :     }
     987             : 
     988             :     /* find the best M/S Pred type */
     989      133227 :     if ( iRealOnlyOut == 1 )
     990             :     {
     991           0 :         iMSPredType = MS_PRED_ONLY;
     992             :     }
     993             :     else
     994             :     {
     995      133227 :         iMSPredType = MS_PHASE_AND_PRED;
     996      133227 :         iMSPredType = ( pfMSPredBitGain[MS_PRED_ONLY] > pfMSPredBitGain[iMSPredType] ? MS_PRED_ONLY : iMSPredType );
     997      133227 :         iMSPredType = ( pfMSPredBitGain[MS_PHASE_ONLY] > pfMSPredBitGain[iMSPredType] ? MS_PHASE_ONLY : iMSPredType );
     998             :     }
     999             : 
    1000             :     /* plain M/S */
    1001      133227 :     iMsInfoBits = CountMSBits( iNumBands, MS_SOME, piMSFlags, NULL, NULL );
    1002      133227 :     fMSBitGain = max( fMSBitGain - iMsInfoBits, 0.0f );
    1003      133227 :     if ( iAllowSidePred && pfMSPredBitGain[iMSPredType] > 1.1f * fMSBitGain )
    1004             :     {
    1005       80485 :         *piMSMode = MS_PRED;
    1006       80485 :         iNumMSBands = 0;
    1007     1851155 :         for ( b = 0; b < iNumBands; b++ )
    1008             :         {
    1009     1770670 :             piMSFlags[b] = ppiMSPredFlags[iMSPredType][b];
    1010     1770670 :             if ( piMSFlags[b] == 1 )
    1011             :             {
    1012     1237229 :                 piMSPredCoefs[b] = ppiMSPredCoefs[iMSPredType][b];
    1013     1237229 :                 piLRPhaseDiffs[b] = ppiMSPredPhase[iMSPredType][b];
    1014     1237229 :                 iNumMSBands++;
    1015             :             }
    1016             :             else
    1017             :             {
    1018      533441 :                 piMSPredCoefs[b] = 0;
    1019      533441 :                 piLRPhaseDiffs[b] = 0;
    1020             :             }
    1021             :         }
    1022             :     }
    1023       52742 :     else if ( iNumMSBands == iNumBands )
    1024             :     {
    1025        1650 :         *piMSMode = MS_ALL;
    1026             :     }
    1027       51092 :     else if ( iNumMSBands > 0 )
    1028             :     {
    1029       51081 :         *piMSMode = MS_SOME;
    1030             :     }
    1031             :     else
    1032             :     {
    1033          11 :         *piMSMode = MS_OFF;
    1034             :     }
    1035             : #ifdef DEBUG_WRITE_MS_PRED
    1036             :     {
    1037             :         static FILE *fid;
    1038             :         int32_t iActualInfoBits = CountMSBits( iNumBands, *piMSMode, piMSFlags, piLRPhaseDiffs, piMSPredCoefs );
    1039             :         if ( !fid )
    1040             :             fid = fopen( "ms_info_bits.txt", "wt" );
    1041             :         fprintf( fid, "%d %d %d %d %d\n", iMsInfoBits, piMsPredInfoBits[MS_PHASE_AND_PRED], piMsPredInfoBits[MS_PRED_ONLY], piMsPredInfoBits[MS_PHASE_ONLY], iActualInfoBits );
    1042             :     }
    1043             : #endif
    1044             : 
    1045      133227 :     if ( *piMSMode != MS_OFF )
    1046             :     {
    1047      133216 :         iFBOffset = 0;
    1048     3063968 :         for ( b = 0; b < iNumBands; b++ )
    1049             :         {
    1050     2930752 :             if ( piMSFlags[b] == 1 )
    1051             :             {
    1052             :                 int32_t n;
    1053             :                 int32_t phaseIdx;
    1054     1968751 :                 phaseIdx = piLRPhaseDiffs[b] - PHASE_MIN_VAL;
    1055     1968751 :                 fPred = dequantPred( piMSPredCoefs[b] );
    1056     6205817 :                 for ( n = 0; n < piBandwidths[b]; n++ )
    1057             :                 {
    1058             :                     int32_t k;
    1059    69536790 :                     for ( k = 0; k < iNumBlocks; k++ )
    1060             :                     {
    1061             :                         float fMidReal;
    1062             :                         float fMidImag;
    1063             :                         float fSideReal;
    1064             :                         float fSideImag;
    1065             : 
    1066    65299724 :                         if ( *piMSMode == MS_PRED )
    1067             :                         {
    1068    40977852 :                             cplxmult_lcld( &pppfReal[1][k][iFBOffset], &pppfImag[1][k][iFBOffset], c_afRotRealImag[phaseIdx][0], c_afRotRealImag[phaseIdx][1] );
    1069             :                         }
    1070             : 
    1071    65299724 :                         fMidReal = 0.5f * ( pppfReal[0][k][iFBOffset] + pppfReal[1][k][iFBOffset] );
    1072    65299724 :                         fMidImag = 0.5f * ( pppfImag[0][k][iFBOffset] + pppfImag[1][k][iFBOffset] );
    1073    65299724 :                         fSideReal = 0.5f * ( pppfReal[0][k][iFBOffset] - pppfReal[1][k][iFBOffset] );
    1074    65299724 :                         fSideImag = 0.5f * ( pppfImag[0][k][iFBOffset] - pppfImag[1][k][iFBOffset] );
    1075             : 
    1076    65299724 :                         if ( *piMSMode == MS_PRED )
    1077             :                         {
    1078    40977852 :                             fSideReal -= fPred * fMidReal;
    1079    40977852 :                             fSideImag -= fPred * fMidImag;
    1080             :                         }
    1081             : 
    1082    65299724 :                         pppfReal[0][k][iFBOffset] = fMidReal;
    1083    65299724 :                         pppfReal[1][k][iFBOffset] = fSideReal;
    1084    65299724 :                         pppfImag[0][k][iFBOffset] = fMidImag;
    1085    65299724 :                         pppfImag[1][k][iFBOffset] = fSideImag;
    1086             :                     }
    1087     4237066 :                     iFBOffset++;
    1088             :                 }
    1089             :             }
    1090             :             else
    1091             :             {
    1092      962001 :                 iFBOffset += piBandwidths[b];
    1093             :             }
    1094             :         }
    1095             :     }
    1096             : #ifdef DEBUG_WRITE_MS_PRED
    1097             :     {
    1098             :         static FILE *fid;
    1099             :         if ( !fid )
    1100             :             fid = fopen( "ms_enc.txt", "wt" );
    1101             :         writeMSPred( piLRPhaseDiffs, piMSPredCoefs, *piMSMode, iNumMSBands, iNumBands, fid, piMSFlags );
    1102             :     }
    1103             : #endif
    1104      133227 :     if ( *piMSMode == MS_PRED )
    1105             :     {
    1106             :         /* Differential Coding of Phase Data*/
    1107       80485 :         PrepEncode( piLRPhaseDiffs, piMSFlags, iNumBands );
    1108       80485 :         PrepEncode( piMSPredCoefs, piMSFlags, iNumBands );
    1109             : #ifdef DEBUG_WRITE_MS_PRED
    1110             :         {
    1111             :             static FILE *fid;
    1112             :             if ( !fid )
    1113             :                 fid = fopen( "ms_pred_enc.txt", "wt" );
    1114             :             writeMSPred( piLRPhaseDiffs, piMSPredCoefs, *piMSMode, iNumMSBands, iNumBands, fid, piMSFlags );
    1115             :         }
    1116             : #endif
    1117             :         /* Differential Coding*/
    1118       80485 :         EncodePhase( piLRPhaseDiffs, iNumMSBands, PHASE_DIFF_DIM );
    1119       80485 :         EncodePredCoef( piMSPredCoefs, iNumMSBands );
    1120             :     }
    1121             : 
    1122      133227 :     return iNumMSBands;
    1123             : }
    1124             : 
    1125             : 
    1126      266454 : static void RemoveRMSEnvelope(
    1127             :     const int32_t iNumBands,
    1128             :     const int32_t *piBandwidths,
    1129             :     const int32_t iNumGroups,
    1130             :     const int32_t *piGroupLengths,
    1131             :     int32_t **ppiRMSEnvelope,
    1132             :     float **ppfReal,
    1133             :     float **ppfImag )
    1134             : {
    1135             :     int32_t k, n, b, iFBOffset, m, iRMSEnv;
    1136             :     int32_t iBlockOffset;
    1137             :     float fGain;
    1138             : 
    1139      266454 :     iBlockOffset = 0;
    1140      662156 :     for ( n = 0; n < iNumGroups; n++ )
    1141             :     {
    1142     4497982 :         for ( k = 0; k < piGroupLengths[n]; k++ )
    1143             :         {
    1144     4102280 :             iFBOffset = 0;
    1145    94352440 :             for ( b = 0; b < iNumBands; b++ )
    1146             :             {
    1147    90250160 :                 iRMSEnv = ppiRMSEnvelope[n][b];
    1148    90250160 :                 fGain = c_afRMSEnvReconstructTable[ENV_RECONSTRUCT_TABLE_CENTER - iRMSEnv];
    1149   295364160 :                 for ( m = 0; m < piBandwidths[b]; m++ )
    1150             :                 {
    1151   205114000 :                     ppfReal[iBlockOffset][iFBOffset] *= fGain;
    1152   205114000 :                     ppfImag[iBlockOffset][iFBOffset] *= fGain;
    1153   205114000 :                     iFBOffset++;
    1154             :                 }
    1155             :             }
    1156     4102280 :             iBlockOffset++;
    1157             :         }
    1158             :     }
    1159             : 
    1160      266454 :     return;
    1161             : }
    1162             : 
    1163             : 
    1164     2529470 : static void QuantizeSpectrumDPCM_Opt(
    1165             :     const int32_t iNumGroups,
    1166             :     const int32_t *piGroupLengths,
    1167             :     const int32_t iNumBands,
    1168             :     const int32_t *piBandwidths,
    1169             :     int32_t **ppiAlloc,
    1170             :     float **ppfReal,
    1171             :     float **ppfImag,
    1172             :     int32_t **ppiQReal,
    1173             :     int32_t **ppiQImag,
    1174             :     int32_t **ppiSignReal,
    1175             :     int32_t **ppiSignImag,
    1176             :     const int32_t iNumSubSets,
    1177             :     const int32_t iSubSetId,
    1178             :     const int32_t *piPredEnable,
    1179             :     float *pfA1Real,
    1180             :     float *pfA1Imag,
    1181             :     float *pfPredStateReal,
    1182             :     float *pfPredStateImag )
    1183             : {
    1184             :     int32_t b, n;
    1185             :     int32_t iFBOffset;
    1186             :     int32_t k, iAlloc, iMaxQuantVal;
    1187             :     float fSCFGain, fInvSCFGain;
    1188             : 
    1189     2529470 :     iFBOffset = 0;
    1190    58177810 :     for ( b = 0; b < iNumBands; b++ )
    1191             :     {
    1192             :         int32_t m;
    1193   182121840 :         for ( m = 0; m < piBandwidths[b]; m++ )
    1194             :         {
    1195   126473500 :             int32_t iBlockOffset = 0;
    1196   126473500 :             if ( piPredEnable[iFBOffset] == 1 )
    1197             :             {
    1198             :                 float fReal;
    1199             :                 float fImag;
    1200     7123950 :                 int32_t iSubset = iFBOffset % iNumSubSets;
    1201     7123950 :                 float fPrevReal = 0.0f;
    1202     7123950 :                 float fPrevImag = 0.0f;
    1203     7123950 :                 if ( iSubset != iSubSetId )
    1204             :                 {
    1205             :                     /* run predictors across sub-frames */
    1206      169326 :                     fPrevReal = pfPredStateReal[iFBOffset];
    1207      169326 :                     fPrevImag = pfPredStateImag[iFBOffset];
    1208             :                 }
    1209    16701497 :                 for ( n = 0; n < iNumGroups; n++ )
    1210             :                 {
    1211     9577547 :                     iAlloc = ppiAlloc[n][b];
    1212     9577547 :                     iMaxQuantVal = c_aiQuantMaxValues[iAlloc];
    1213     9577547 :                     fSCFGain = c_afScaleFactor[iAlloc];
    1214     9577547 :                     fInvSCFGain = c_afInvScaleFactor[iAlloc];
    1215   120851903 :                     for ( k = 0; k < piGroupLengths[n]; k++ )
    1216             :                     {
    1217             :                         /* prediction */
    1218   111274356 :                         fReal = pfA1Real[iFBOffset] * fPrevReal - pfA1Imag[iFBOffset] * fPrevImag;
    1219   111274356 :                         fImag = pfA1Real[iFBOffset] * fPrevImag + pfA1Imag[iFBOffset] * fPrevReal;
    1220             : 
    1221   222548712 :                         ppiQReal[iBlockOffset][iFBOffset] = Quantize( ppfReal[iBlockOffset][iFBOffset] + fReal, /* quantize residual */
    1222             :                                                                       fSCFGain,
    1223   111274356 :                                                                       &ppiSignReal[iBlockOffset][iFBOffset],
    1224             :                                                                       iMaxQuantVal );
    1225             : 
    1226   222548712 :                         ppiQImag[iBlockOffset][iFBOffset] = Quantize( ppfImag[iBlockOffset][iFBOffset] + fImag,
    1227             :                                                                       fSCFGain,
    1228   111274356 :                                                                       &ppiSignImag[iBlockOffset][iFBOffset],
    1229             :                                                                       iMaxQuantVal );
    1230             : 
    1231   222548712 :                         fPrevReal = UnQuantize( ppiQReal[iBlockOffset][iFBOffset],
    1232             :                                                 fInvSCFGain,
    1233   111274356 :                                                 ppiSignReal[iBlockOffset][iFBOffset] ) -
    1234             :                                     fReal; /* add prediction to quantized residual = reconstructed sample */
    1235             : 
    1236   222548712 :                         fPrevImag = UnQuantize( ppiQImag[iBlockOffset][iFBOffset],
    1237             :                                                 fInvSCFGain,
    1238   111274356 :                                                 ppiSignImag[iBlockOffset][iFBOffset] ) -
    1239             :                                     fImag;
    1240             : 
    1241   111274356 :                         iBlockOffset++;
    1242             :                     } /* group length */
    1243             :                 }     /* groups */
    1244     7123950 :                 pfPredStateReal[iFBOffset] = fPrevReal;
    1245     7123950 :                 pfPredStateImag[iFBOffset] = fPrevImag;
    1246             :             } /* predEnable */
    1247             :             else
    1248             :             { /* no prediction */
    1249   297582203 :                 for ( n = 0; n < iNumGroups; n++ )
    1250             :                 {
    1251   178232653 :                     iAlloc = ppiAlloc[n][b];
    1252   178232653 :                     iMaxQuantVal = c_aiQuantMaxValues[iAlloc];
    1253   178232653 :                     fSCFGain = c_afScaleFactor[iAlloc];
    1254   178232653 :                     fInvSCFGain = c_afInvScaleFactor[iAlloc];
    1255  2014127897 :                     for ( k = 0; k < piGroupLengths[n]; k++ )
    1256             :                     {
    1257  3671790488 :                         ppiQReal[iBlockOffset][iFBOffset] = Quantize( ppfReal[iBlockOffset][iFBOffset],
    1258             :                                                                       fSCFGain,
    1259  1835895244 :                                                                       &ppiSignReal[iBlockOffset][iFBOffset],
    1260             :                                                                       iMaxQuantVal );
    1261             : 
    1262  3671790488 :                         ppiQImag[iBlockOffset][iFBOffset] = Quantize( ppfImag[iBlockOffset][iFBOffset],
    1263             :                                                                       fSCFGain,
    1264  1835895244 :                                                                       &ppiSignImag[iBlockOffset][iFBOffset],
    1265             :                                                                       iMaxQuantVal );
    1266             : 
    1267  1835895244 :                         iBlockOffset++;
    1268             :                     } /* group length */
    1269             :                 }     /* groups */
    1270             :             }         /* predEnable */
    1271   126473500 :             iFBOffset++;
    1272             :         } /* bandwidth */
    1273             :     }     /* bands */
    1274             : 
    1275     2529470 :     return;
    1276             : }
    1277             : 
    1278             : 
    1279     2529470 : static int32_t CountLCLDBits(
    1280             :     const int32_t iNumGroups,
    1281             :     const int32_t *piGroupLengths,
    1282             :     const int32_t iNumBands,
    1283             :     const int32_t *piBandwidths,
    1284             :     const int32_t *piPredEnable,
    1285             :     int32_t **ppiAlloc,
    1286             :     int32_t **ppiQReal,
    1287             :     int32_t **ppiQImag )
    1288             : {
    1289             :     int32_t k, n, b, iFBOffset;
    1290             :     int32_t iBits, iBlockOffest;
    1291             :     int32_t m, iAlloc, iHuffDim, iHuffMod;
    1292             : 
    1293     2529470 :     iBits = 0;
    1294     2529470 :     iBlockOffest = 0;
    1295     6285674 :     for ( n = 0; n < iNumGroups; n++ )
    1296             :     {
    1297    42699596 :         for ( k = 0; k < piGroupLengths[n]; k++ )
    1298             :         {
    1299    38943392 :             iFBOffset = 0;
    1300   895698016 :             for ( b = 0; b < iNumBands; b++ )
    1301             :             {
    1302   856754624 :                 iAlloc = ppiAlloc[n][b];
    1303             : 
    1304   856754624 :                 iHuffDim = c_aiHuffmanDim[iAlloc];
    1305   856754624 :                 iHuffMod = c_aiHuffmanMod[iAlloc];
    1306             : 
    1307   856754624 :                 if ( iAlloc > 0 )
    1308             :                 {
    1309   749833018 :                     const uint16_t( *pauiHuffmanTable )[2] = NULL;
    1310   749833018 :                     const uint16_t( *pauiHuffmanTableDPCM )[2] = NULL;
    1311             : 
    1312   749833018 :                     pauiHuffmanTable = c_apauiHuffEncTabels[iAlloc];
    1313   749833018 :                     pauiHuffmanTableDPCM = c_apauiHuffEncTabels[ALLOC_TABLE_SIZE + iAlloc];
    1314             : 
    1315  2366876302 :                     for ( m = 0; m < piBandwidths[b]; m++ )
    1316             :                     {
    1317             :                         int32_t iQuantValue1;
    1318             :                         int32_t iQuantValue2;
    1319             : 
    1320  1617043284 :                         iQuantValue1 = ppiQReal[iBlockOffest][iFBOffset];
    1321  1617043284 :                         iQuantValue2 = ppiQImag[iBlockOffest][iFBOffset];
    1322             : 
    1323  1617043284 :                         iBits += ( iQuantValue1 > 0 ) ? 1 : 0; /* Sign bit for vals > 0 */
    1324  1617043284 :                         iBits += ( iQuantValue2 > 0 ) ? 1 : 0; /* Sign bit for vals > 0 */
    1325             : 
    1326  1617043284 :                         if ( piPredEnable[iFBOffset] == 1 )
    1327             :                         {
    1328    99300440 :                             if ( iHuffDim == 2 )
    1329             :                             {
    1330    33507688 :                                 iQuantValue1 *= iHuffMod;
    1331    33507688 :                                 iQuantValue1 += iQuantValue2;
    1332    33507688 :                                 iBits += pauiHuffmanTableDPCM[iQuantValue1][0];
    1333             :                             }
    1334             :                             else
    1335             :                             {
    1336    65792752 :                                 iBits += pauiHuffmanTableDPCM[iQuantValue1][0];
    1337    65792752 :                                 iBits += pauiHuffmanTableDPCM[iQuantValue2][0];
    1338             :                             }
    1339             :                         }
    1340             :                         else
    1341             :                         {
    1342  1517742844 :                             if ( iHuffDim == 2 )
    1343             :                             {
    1344  1044782870 :                                 iQuantValue1 *= iHuffMod;
    1345  1044782870 :                                 iQuantValue1 += iQuantValue2;
    1346  1044782870 :                                 iBits += pauiHuffmanTable[iQuantValue1][0];
    1347             :                             }
    1348             :                             else
    1349             :                             {
    1350   472959974 :                                 iBits += pauiHuffmanTable[iQuantValue1][0];
    1351   472959974 :                                 iBits += pauiHuffmanTable[iQuantValue2][0];
    1352             :                             }
    1353             :                         }
    1354             : 
    1355  1617043284 :                         iFBOffset++;
    1356             :                     }
    1357             :                 }
    1358             :                 else
    1359             :                 {
    1360   106921606 :                     iFBOffset += piBandwidths[b];
    1361             :                 }
    1362             :             }
    1363             : 
    1364    38943392 :             iBlockOffest++;
    1365             :         }
    1366             :     }
    1367             : 
    1368     2529470 :     return iBits;
    1369             : }
    1370             : 
    1371             : 
    1372             : /* Currently only the number of bands in frame */
    1373      133227 : static int32_t WriteHeaderInformation(
    1374             :     const int32_t iNumBands,
    1375             :     ISAR_SPLIT_REND_BITS_HANDLE pBits )
    1376             : {
    1377             :     int32_t iBitsWritten;
    1378             : 
    1379      133227 :     iBitsWritten = 0;
    1380      133227 :     ISAR_SPLIT_REND_BITStream_write_int32( pBits, iNumBands, 5 );
    1381      133227 :     iBitsWritten += 5;
    1382             : 
    1383      133227 :     return iBitsWritten;
    1384             : }
    1385             : 
    1386             : 
    1387      133227 : static int32_t WriteMSInformation(
    1388             :     const int32_t iNumBands,
    1389             :     const int32_t iMSMode,
    1390             :     const int32_t *piMSFlags,
    1391             :     const int32_t *piLRPhaseDiff,
    1392             :     const int32_t *piMSPredCoef,
    1393             :     const int32_t iNumMSPredBands,
    1394             :     ISAR_SPLIT_REND_BITS_HANDLE pBits )
    1395             : {
    1396             :     int32_t iBitsWritten;
    1397      133227 :     int32_t iMSPredAll = ( iNumMSPredBands == iNumBands );
    1398             : #ifdef DEBUG_WRITE_MS_PRED
    1399             :     int32_t iBitsWrittenTmp = 0;
    1400             : #endif
    1401             : 
    1402      133227 :     iBitsWritten = 0;
    1403      133227 :     ISAR_SPLIT_REND_BITStream_write_int32( pBits, iMSMode, 2 );
    1404      133227 :     iBitsWritten += 2;
    1405             : 
    1406      133227 :     if ( iMSMode == 3 )
    1407             :     {
    1408       80485 :         ISAR_SPLIT_REND_BITStream_write_int32( pBits, iMSPredAll, 1 );
    1409       80485 :         iBitsWritten += 1;
    1410             :     }
    1411             : 
    1412      133227 :     if ( iMSMode == 2 || ( iMSMode == 3 && !iMSPredAll ) )
    1413             :     {
    1414             :         int32_t n;
    1415     2820030 :         for ( n = 0; n < iNumBands; n++ )
    1416             :         {
    1417     2697420 :             ISAR_SPLIT_REND_BITStream_write_int32( pBits, piMSFlags[n], 1 );
    1418     2697420 :             iBitsWritten += 1;
    1419             :         }
    1420             :     }
    1421             : 
    1422             : #ifdef DEBUG_WRITE_MS_PRED
    1423             :     iBitsWrittenTmp = iBitsWritten;
    1424             : #endif
    1425      133227 :     if ( iMSMode == 3 )
    1426             :     {
    1427             :         int32_t b;
    1428             :         int32_t anyNonZero;
    1429       80485 :         anyNonZero = 0;
    1430      405571 :         for ( b = 0; b < iNumMSPredBands; b++ )
    1431             :         {
    1432      387371 :             if ( piLRPhaseDiff[b] != 0 )
    1433             :             {
    1434       62285 :                 anyNonZero = 1;
    1435       62285 :                 break;
    1436             :             }
    1437             :         }
    1438             : 
    1439       80485 :         ISAR_SPLIT_REND_BITStream_write_int32( pBits, anyNonZero, 1 );
    1440       80485 :         iBitsWritten++;
    1441             : 
    1442       80485 :         if ( anyNonZero )
    1443             :         {
    1444       62285 :             ISAR_SPLIT_REND_BITStream_write_int32( pBits, piLRPhaseDiff[0] - PHASE_MIN_VAL, PHASE_BAND0_BITS );
    1445       62285 :             iBitsWritten += PHASE_BAND0_BITS;
    1446      925771 :             for ( b = 1; b < iNumMSPredBands; b++ )
    1447             :             {
    1448      863486 :                 int32_t tabIdx = piLRPhaseDiff[b] - ENV_DELTA_MIN;
    1449      863486 :                 ISAR_SPLIT_REND_BITStream_write_int32( pBits, c_aaiRMSEnvHuffEnc[tabIdx][1], c_aaiRMSEnvHuffEnc[tabIdx][0] );
    1450      863486 :                 iBitsWritten += c_aaiRMSEnvHuffEnc[tabIdx][0];
    1451             :             }
    1452             :         }
    1453             : 
    1454       80485 :         anyNonZero = 0;
    1455      133024 :         for ( b = 0; b < iNumMSPredBands; b++ )
    1456             :         {
    1457      129492 :             if ( piMSPredCoef[b] != 0 )
    1458             :             {
    1459       76953 :                 anyNonZero = 1;
    1460       76953 :                 break;
    1461             :             }
    1462             :         }
    1463             : 
    1464       80485 :         ISAR_SPLIT_REND_BITStream_write_int32( pBits, anyNonZero, 1 );
    1465       80485 :         iBitsWritten++;
    1466             : 
    1467       80485 :         if ( anyNonZero )
    1468             :         {
    1469       76953 :             ISAR_SPLIT_REND_BITStream_write_int32( pBits, piMSPredCoef[0] - PRED_MIN_VAL, PRED_BAND0_BITS );
    1470       76953 :             iBitsWritten += PRED_BAND0_BITS;
    1471     1215045 :             for ( b = 1; b < iNumMSPredBands; b++ )
    1472             :             {
    1473     1138092 :                 int32_t tabIdx = piMSPredCoef[b] - ENV_DELTA_MIN;
    1474     1138092 :                 ISAR_SPLIT_REND_BITStream_write_int32( pBits, c_aaiRMSEnvHuffEnc[tabIdx][1], c_aaiRMSEnvHuffEnc[tabIdx][0] );
    1475     1138092 :                 iBitsWritten += c_aaiRMSEnvHuffEnc[tabIdx][0];
    1476             :             }
    1477             :         }
    1478             :     }
    1479             : #ifdef DEBUG_WRITE_MS_PRED
    1480             :     {
    1481             :         static FILE *fid = 0;
    1482             :         if ( !fid )
    1483             :         {
    1484             :             fid = fopen( "ms_pred_bitrate.txt", "wt" );
    1485             :         }
    1486             :         fprintf( fid, "%f\n", (float) ( ( iBitsWritten - iBitsWrittenTmp ) * ( iMSMode == 3 ) * 50 ) / 1000.0f ); /*kb/s*/
    1487             :     }
    1488             : #endif
    1489             : 
    1490      133227 :     return iBitsWritten;
    1491             : }
    1492             : 
    1493             : 
    1494      133227 : static int32_t WriteGroupInformation(
    1495             :     const int32_t iChannels,
    1496             :     const int32_t iCommonGrouping,
    1497             :     const int32_t *piNumGroups,
    1498             :     int32_t **ppiGroupLengths,
    1499             :     ISAR_SPLIT_REND_BITS_HANDLE pBits )
    1500             : {
    1501             :     int32_t c, k, n, iBitsWritten;
    1502             : 
    1503      133227 :     iBitsWritten = 0;
    1504      133227 :     if ( iChannels == 2 && iCommonGrouping == 1 )
    1505             :     {
    1506      133227 :         ISAR_SPLIT_REND_BITStream_write_int32( pBits, iCommonGrouping, 1 );
    1507      133227 :         iBitsWritten += 1;
    1508             : 
    1509      331078 :         for ( n = 0; n < piNumGroups[0]; n++ )
    1510             :         {
    1511     2051140 :             for ( k = 1; k < ppiGroupLengths[0][n]; k++ )
    1512             :             {
    1513     1853289 :                 ISAR_SPLIT_REND_BITStream_write_int32( pBits, 0, 1 );
    1514     1853289 :                 iBitsWritten += 1;
    1515             :             }
    1516      197851 :             if ( n < ( piNumGroups[0] - 1 ) )
    1517             :             {
    1518       64624 :                 ISAR_SPLIT_REND_BITStream_write_int32( pBits, 1, 1 );
    1519       64624 :                 iBitsWritten += 1;
    1520             :             }
    1521             :         }
    1522             :     }
    1523           0 :     else if ( iChannels == 2 )
    1524             :     {
    1525           0 :         ISAR_SPLIT_REND_BITStream_write_int32( pBits, iCommonGrouping, 1 );
    1526           0 :         iBitsWritten += 1;
    1527             : 
    1528           0 :         for ( c = 0; c < iChannels; c++ )
    1529             :         {
    1530           0 :             for ( n = 0; n < piNumGroups[c]; n++ )
    1531             :             {
    1532           0 :                 for ( k = 1; k < ppiGroupLengths[c][n]; k++ )
    1533             :                 {
    1534           0 :                     ISAR_SPLIT_REND_BITStream_write_int32( pBits, 0, 1 );
    1535           0 :                     iBitsWritten += 1;
    1536             :                 }
    1537           0 :                 if ( n < ( piNumGroups[c] - 1 ) )
    1538             :                 {
    1539           0 :                     ISAR_SPLIT_REND_BITStream_write_int32( pBits, 1, 1 );
    1540           0 :                     iBitsWritten += 1;
    1541             :                 }
    1542             :             }
    1543             :         }
    1544             :     }
    1545             :     else
    1546             :     {
    1547           0 :         for ( c = 0; c < iChannels; c++ )
    1548             :         {
    1549           0 :             for ( n = 0; n < piNumGroups[c]; n++ )
    1550             :             {
    1551           0 :                 for ( k = 1; k < ppiGroupLengths[c][n]; k++ )
    1552             :                 {
    1553           0 :                     ISAR_SPLIT_REND_BITStream_write_int32( pBits, 0, 1 );
    1554           0 :                     iBitsWritten += 1;
    1555             :                 }
    1556             : 
    1557           0 :                 if ( n < ( piNumGroups[c] - 1 ) )
    1558             :                 {
    1559           0 :                     ISAR_SPLIT_REND_BITStream_write_int32( pBits, 1, 1 );
    1560           0 :                     iBitsWritten += 1;
    1561             :                 }
    1562             :             }
    1563             :         }
    1564             :     }
    1565             : 
    1566      133227 :     return iBitsWritten;
    1567             : }
    1568             : 
    1569             : 
    1570      133227 : static int32_t WriteRMSEnvelope(
    1571             :     const int32_t iChannels,
    1572             :     const int32_t *piNumGroups,
    1573             :     const int32_t iNumBands,
    1574             :     int32_t ***pppiRMSEnvelope,
    1575             :     ISAR_SPLIT_REND_BITS_HANDLE pBits )
    1576             : {
    1577             :     int32_t k, n;
    1578             :     int32_t iBitsWritten;
    1579             : 
    1580      133227 :     iBitsWritten = 0;
    1581      399681 :     for ( n = 0; n < iChannels; n++ )
    1582             :     {
    1583      662156 :         for ( k = 0; k < piNumGroups[n]; k++ )
    1584             :         {
    1585             :             int32_t b;
    1586             :             int32_t iLastRMSVal;
    1587             : 
    1588      395702 :             iLastRMSVal = pppiRMSEnvelope[n][k][0];
    1589      395702 :             iLastRMSVal = ( iLastRMSVal > ENV_MIN ) ? iLastRMSVal : ENV_MIN;
    1590      395702 :             iLastRMSVal = ( iLastRMSVal < ENV_MAX ) ? iLastRMSVal : ENV_MAX;
    1591      395702 :             ISAR_SPLIT_REND_BITStream_write_int32( pBits, ( iLastRMSVal - ENV_MIN ), ENV0_BITS );
    1592      395702 :             iBitsWritten += ENV0_BITS;
    1593             : 
    1594     8705444 :             for ( b = 1; b < iNumBands; b++ )
    1595             :             {
    1596             :                 int32_t iDelta;
    1597             : 
    1598     8309742 :                 iDelta = pppiRMSEnvelope[n][k][b] - iLastRMSVal;
    1599     8309742 :                 iDelta = ( iDelta > ENV_DELTA_MIN ) ? iDelta : ENV_DELTA_MIN;
    1600     8309742 :                 iDelta = ( iDelta < ENV_DELTA_MAX ) ? iDelta : ENV_DELTA_MAX;
    1601     8309742 :                 iDelta -= ENV_DELTA_MIN;
    1602     8309742 :                 ISAR_SPLIT_REND_BITStream_write_int32( pBits, c_aaiRMSEnvHuffEnc[iDelta][1], c_aaiRMSEnvHuffEnc[iDelta][0] );
    1603     8309742 :                 iBitsWritten += c_aaiRMSEnvHuffEnc[iDelta][0];
    1604             : 
    1605     8309742 :                 iLastRMSVal = pppiRMSEnvelope[n][k][b];
    1606             :             }
    1607             :         }
    1608             :     }
    1609             : 
    1610      133227 :     return iBitsWritten;
    1611             : }
    1612             : 
    1613             : 
    1614      133227 : static int32_t WriteAllocInformation(
    1615             :     const int32_t iAllocOffset,
    1616             :     ISAR_SPLIT_REND_BITS_HANDLE pBits )
    1617             : {
    1618             :     int32_t iBitsWritten;
    1619             : 
    1620      133227 :     iBitsWritten = 0;
    1621             : 
    1622      133227 :     if ( iAllocOffset < MIN_ALLOC_OFFSET || iAllocOffset > MAX_ALLOC_OFFSET )
    1623             :     {
    1624           0 :         printf( "Serious error\n" );
    1625             :     }
    1626             : 
    1627      133227 :     ISAR_SPLIT_REND_BITStream_write_int32( pBits, ( iAllocOffset - MIN_ALLOC_OFFSET ), ALLOC_OFFSET_BITS );
    1628      133227 :     iBitsWritten += ALLOC_OFFSET_BITS;
    1629             : 
    1630      133227 :     return iBitsWritten;
    1631             : }
    1632             : 
    1633             : 
    1634      133227 : static int32_t WriteLCLDData(
    1635             :     const int32_t *piNumGroups,
    1636             :     int32_t **ppiGroupLengths,
    1637             :     const int32_t iNumBands,
    1638             :     const int32_t iNumChannels,
    1639             :     int32_t **ppiPredEnable,
    1640             :     const int32_t iNumSubSets,
    1641             :     const int32_t iSubSetId,
    1642             :     int32_t ***pppiAlloc,
    1643             :     int32_t ***pppiSignReal,
    1644             :     int32_t ***pppiSignImag,
    1645             :     int32_t ***pppiQReal,
    1646             :     int32_t ***pppiQImag,
    1647             :     ISAR_SPLIT_REND_BITS_HANDLE pBits )
    1648             : {
    1649             :     int32_t iBitsWritten;
    1650      133227 :     int32_t iNumLcldBands = c_aiNumLcldBandsPerBand[iNumBands - 1];
    1651             :     int32_t s;
    1652      133227 :     int32_t iSet = iSubSetId;
    1653             : 
    1654      133227 :     iBitsWritten = 0;
    1655      282553 :     for ( s = 0; s < iNumSubSets; s++, iSet-- )
    1656             :     {
    1657             :         int32_t ch;
    1658      149326 :         if ( iSet < 0 )
    1659             :         {
    1660        5031 :             iSet = iNumSubSets - 1;
    1661             :         }
    1662             : 
    1663      447978 :         for ( ch = 0; ch < iNumChannels; ch++ )
    1664             :         {
    1665      298652 :             int32_t iBlockOffest = 0;
    1666             :             int32_t n;
    1667      726980 :             for ( n = 0; n < piNumGroups[ch]; n++ )
    1668             :             {
    1669             :                 int32_t k;
    1670     4691592 :                 for ( k = 0; k < ppiGroupLengths[ch][n]; k++ )
    1671             :                 {
    1672             :                     int32_t iFBOffset;
    1673   209377264 :                     for ( iFBOffset = iSet; iFBOffset < iNumLcldBands; iFBOffset += iNumSubSets )
    1674             :                     {
    1675             :                         int32_t b;
    1676             :                         int32_t iAlloc;
    1677             :                         int32_t iHuffDim;
    1678             :                         int32_t iHuffMod;
    1679             : 
    1680   205114000 :                         b = c_aiBandIdPerLcldBand[iFBOffset];
    1681             : 
    1682   205114000 :                         iAlloc = pppiAlloc[ch][n][b];
    1683             : 
    1684   205114000 :                         iHuffDim = c_aiHuffmanDim[iAlloc];
    1685   205114000 :                         iHuffMod = c_aiHuffmanMod[iAlloc];
    1686             : 
    1687   205114000 :                         if ( iAlloc > 0 )
    1688             :                         {
    1689   188115418 :                             const uint16_t( *pauiHuffmanTable )[2] = NULL;
    1690   188115418 :                             const uint16_t( *pauiHuffmanTableDPCM )[2] = NULL;
    1691             :                             int32_t iQuantValue1;
    1692             :                             int32_t iQuantValue2;
    1693   188115418 :                             pauiHuffmanTable = c_apauiHuffEncTabels[iAlloc];
    1694   188115418 :                             pauiHuffmanTableDPCM = c_apauiHuffEncTabels[ALLOC_TABLE_SIZE + iAlloc];
    1695             : 
    1696   188115418 :                             iQuantValue1 = pppiQReal[ch][iBlockOffest][iFBOffset];
    1697   188115418 :                             iQuantValue2 = pppiQImag[ch][iBlockOffest][iFBOffset];
    1698             : #ifdef LCLD_HANDLE_PRED_START_SAMPLE
    1699             :                             if ( ppiPredEnable[ch][iFBOffset] == 1 && ( iBlockOffest > 0 || iSet != iSubSetId ) )
    1700             : #else
    1701   188115418 :                             if ( ppiPredEnable[ch][iFBOffset] == 1 )
    1702             : #endif
    1703             :                             {
    1704    11016700 :                                 if ( iHuffDim == 2 )
    1705             :                                 {
    1706             :                                     int32_t iSymbol;
    1707     3826848 :                                     iSymbol = iQuantValue1;
    1708     3826848 :                                     iSymbol *= iHuffMod;
    1709     3826848 :                                     iSymbol += iQuantValue2;
    1710     3826848 :                                     ISAR_SPLIT_REND_BITStream_write_int32( pBits, pauiHuffmanTableDPCM[iSymbol][1], pauiHuffmanTableDPCM[iSymbol][0] );
    1711     3826848 :                                     iBitsWritten += pauiHuffmanTableDPCM[iSymbol][0];
    1712             :                                 }
    1713             :                                 else
    1714             :                                 {
    1715     7189852 :                                     ISAR_SPLIT_REND_BITStream_write_int32( pBits, pauiHuffmanTableDPCM[iQuantValue1][1], pauiHuffmanTableDPCM[iQuantValue1][0] );
    1716     7189852 :                                     iBitsWritten += pauiHuffmanTableDPCM[iQuantValue1][0];
    1717             : 
    1718     7189852 :                                     ISAR_SPLIT_REND_BITStream_write_int32( pBits, pauiHuffmanTableDPCM[iQuantValue2][1], pauiHuffmanTableDPCM[iQuantValue2][0] );
    1719     7189852 :                                     iBitsWritten += pauiHuffmanTableDPCM[iQuantValue2][0];
    1720             :                                 }
    1721             :                             }
    1722             :                             else
    1723             :                             {
    1724   177098718 :                                 if ( iHuffDim == 2 )
    1725             :                                 {
    1726             :                                     int32_t iSymbol;
    1727   131605180 :                                     iSymbol = iQuantValue1;
    1728   131605180 :                                     iSymbol *= iHuffMod;
    1729   131605180 :                                     iSymbol += iQuantValue2;
    1730   131605180 :                                     ISAR_SPLIT_REND_BITStream_write_int32( pBits, pauiHuffmanTable[iSymbol][1], pauiHuffmanTable[iSymbol][0] );
    1731   131605180 :                                     iBitsWritten += pauiHuffmanTable[iSymbol][0];
    1732             :                                 }
    1733             :                                 else
    1734             :                                 {
    1735    45493538 :                                     ISAR_SPLIT_REND_BITStream_write_int32( pBits, pauiHuffmanTable[iQuantValue1][1], pauiHuffmanTable[iQuantValue1][0] );
    1736    45493538 :                                     iBitsWritten += pauiHuffmanTable[iQuantValue1][0];
    1737             : 
    1738    45493538 :                                     ISAR_SPLIT_REND_BITStream_write_int32( pBits, pauiHuffmanTable[iQuantValue2][1], pauiHuffmanTable[iQuantValue2][0] );
    1739    45493538 :                                     iBitsWritten += pauiHuffmanTable[iQuantValue2][0];
    1740             :                                 }
    1741             :                             }
    1742             : 
    1743   188115418 :                             if ( iQuantValue1 > 0 )
    1744             :                             {
    1745   127673299 :                                 ISAR_SPLIT_REND_BITStream_write_int32( pBits, pppiSignReal[ch][iBlockOffest][iFBOffset], 1 );
    1746   127673299 :                                 iBitsWritten += 1;
    1747             :                             }
    1748   188115418 :                             if ( iQuantValue2 > 0 )
    1749             :                             {
    1750   127474297 :                                 ISAR_SPLIT_REND_BITStream_write_int32( pBits, pppiSignImag[ch][iBlockOffest][iFBOffset], 1 );
    1751   127474297 :                                 iBitsWritten += 1;
    1752             :                             }
    1753             :                         }
    1754             :                     }
    1755     4263264 :                     iBlockOffest++;
    1756             :                 }
    1757             :             }
    1758             :         }
    1759             :     }
    1760             : 
    1761      133227 :     return iBitsWritten;
    1762             : }
    1763             : 
    1764             : 
    1765      133227 : static int32_t ComputeAllocation(
    1766             :     const int32_t iChannels,
    1767             :     const int32_t *piNumGroups,
    1768             :     int32_t **ppiGroupLengths,
    1769             :     const int32_t iNumBands,
    1770             :     const int32_t *piBandwidths,
    1771             :     float ***pppfReal,
    1772             :     float ***pppfImag,
    1773             :     int32_t ***pppiSMR,
    1774             :     const int32_t iAvailableBits,
    1775             :     int32_t *piAllocOffset,
    1776             :     int32_t ***pppiAlloc,
    1777             :     int32_t ***pppiQReal,
    1778             :     int32_t ***pppiQImag,
    1779             :     int32_t ***pppiSignReal,
    1780             :     int32_t ***pppiSignImag,
    1781             :     PredictionEncoder *psPredictionEncoder )
    1782             : {
    1783             :     int32_t iBitsUsed, iDone, iDelta;
    1784             :     int32_t b, k, n;
    1785             :     int32_t iLimitAllocOffset;
    1786             : 
    1787      133227 :     iBitsUsed = ALLOC_OFFSET_BITS; /* Bits used for Alloc Offset */
    1788             : 
    1789      133227 :     iDone = 0;
    1790      133227 :     iDelta = -MIN_ALLOC_OFFSET;
    1791      133227 :     *piAllocOffset = 0;
    1792             : 
    1793     1397962 :     while ( iDone == 0 )
    1794             :     {
    1795     1264735 :         iBitsUsed = ALLOC_OFFSET_BITS;
    1796             : 
    1797     1264735 :         iLimitAllocOffset = *piAllocOffset;
    1798     1264735 :         iLimitAllocOffset = ( iLimitAllocOffset > MIN_ALLOC_OFFSET ) ? iLimitAllocOffset : MIN_ALLOC_OFFSET;
    1799     1264735 :         iLimitAllocOffset = ( iLimitAllocOffset < MAX_ALLOC_OFFSET ) ? iLimitAllocOffset : MAX_ALLOC_OFFSET;
    1800             : 
    1801     3794205 :         for ( n = 0; n < iChannels; n++ )
    1802             :         {
    1803     6285674 :             for ( k = 0; k < piNumGroups[n]; k++ )
    1804             :             {
    1805    86392692 :                 for ( b = 0; b < iNumBands; b++ )
    1806             :                 {
    1807             :                     int32_t iAlloc;
    1808    82636488 :                     iAlloc = ( ( pppiSMR[n][k][b] + iLimitAllocOffset * ALLOC_OFFSET_SCALE ) >> 5 );
    1809    82636488 :                     iAlloc = ( iAlloc > MIN_ALLOC ) ? iAlloc : MIN_ALLOC;
    1810    82636488 :                     iAlloc = ( iAlloc < MAX_ALLOC ) ? iAlloc : MAX_ALLOC;
    1811    82636488 :                     pppiAlloc[n][k][b] = iAlloc;
    1812             :                 }
    1813             :             }
    1814             : 
    1815     2529470 :             if ( psPredictionEncoder->iNumSubSets > 1 )
    1816             :             {
    1817      152840 :                 mvr2r( psPredictionEncoder->ppfPredStateReal[n], psPredictionEncoder->ppfPredStateRealTmp[n], LCLD_BANDS );
    1818      152840 :                 mvr2r( psPredictionEncoder->ppfPredStateImag[n], psPredictionEncoder->ppfPredStateImagTmp[n], LCLD_BANDS );
    1819             :             }
    1820             : 
    1821     2529470 :             QuantizeSpectrumDPCM_Opt( piNumGroups[n],
    1822     2529470 :                                       (const int32_t *) ppiGroupLengths[n],
    1823             :                                       iNumBands,
    1824             :                                       piBandwidths,
    1825     2529470 :                                       pppiAlloc[n],
    1826     2529470 :                                       pppfReal[n],
    1827     2529470 :                                       pppfImag[n],
    1828     2529470 :                                       pppiQReal[n],
    1829     2529470 :                                       pppiQImag[n],
    1830     2529470 :                                       pppiSignReal[n],
    1831     2529470 :                                       pppiSignImag[n],
    1832             :                                       psPredictionEncoder->iNumSubSets,
    1833             :                                       psPredictionEncoder->iSubSetId,
    1834     2529470 :                                       psPredictionEncoder->ppiPredBandEnable[n],
    1835     2529470 :                                       psPredictionEncoder->ppfA1Real[n],
    1836     2529470 :                                       psPredictionEncoder->ppfA1Imag[n],
    1837     2529470 :                                       psPredictionEncoder->ppfPredStateRealTmp[n],
    1838     2529470 :                                       psPredictionEncoder->ppfPredStateImagTmp[n] );
    1839             : 
    1840     2529470 :             iBitsUsed += CountLCLDBits( piNumGroups[n],
    1841     2529470 :                                         (const int32_t *) ppiGroupLengths[n],
    1842             :                                         iNumBands,
    1843             :                                         piBandwidths,
    1844     2529470 :                                         (const int32_t *) psPredictionEncoder->ppiPredBandEnable[n],
    1845     2529470 :                                         pppiAlloc[n],
    1846     2529470 :                                         pppiQReal[n],
    1847     2529470 :                                         pppiQImag[n] );
    1848             :         }
    1849             : 
    1850     1264735 :         if ( *piAllocOffset <= MIN_ALLOC_OFFSET && iBitsUsed > iAvailableBits )
    1851             :         {
    1852             : #ifdef DEBUG_VERBOSE
    1853             :             printf( "Frame can not be coded with the number of bits available\n" );
    1854             : #endif
    1855             :             /* iLastError = ENC_ERROR_STREAM_FAILURE;*/
    1856           0 :             return -1;
    1857             :         }
    1858     1264735 :         else if ( *piAllocOffset >= MAX_ALLOC_OFFSET && iBitsUsed < iAvailableBits )
    1859             :         {
    1860           0 :             *piAllocOffset = MAX_ALLOC_OFFSET;
    1861           0 :             iDone++;
    1862             :         }
    1863             :         else
    1864             :         {
    1865     1264735 :             if ( iDelta == 0 && iBitsUsed > iAvailableBits )
    1866             :             {
    1867       66650 :                 iDelta = 1;
    1868             :             }
    1869     1198085 :             else if ( iDelta == 0 && iBitsUsed < iAvailableBits )
    1870             :             {
    1871      132307 :                 iDone++;
    1872             :             }
    1873     1065778 :             else if ( iBitsUsed == iAvailableBits )
    1874             :             {
    1875         920 :                 iDone++;
    1876             :             }
    1877             : 
    1878     1264735 :             if ( iBitsUsed > iAvailableBits )
    1879             :             {
    1880      565936 :                 *piAllocOffset -= iDelta;
    1881      565936 :                 iDelta >>= 1;
    1882             :             }
    1883      698799 :             else if ( iBitsUsed < iAvailableBits )
    1884             :             {
    1885      697879 :                 *piAllocOffset += iDelta;
    1886      697879 :                 iDelta >>= 1;
    1887             :             }
    1888             :         }
    1889             :     }
    1890             : 
    1891      133227 :     if ( psPredictionEncoder->iNumSubSets > 1 )
    1892             :     {
    1893       24147 :         for ( n = 0; n < iChannels; n++ )
    1894             :         {
    1895       16098 :             mvr2r( psPredictionEncoder->ppfPredStateRealTmp[n], psPredictionEncoder->ppfPredStateReal[n], LCLD_BANDS );
    1896       16098 :             mvr2r( psPredictionEncoder->ppfPredStateImagTmp[n], psPredictionEncoder->ppfPredStateImag[n], LCLD_BANDS );
    1897             :         }
    1898             :     }
    1899             : 
    1900             : #ifdef DEBUGGING
    1901             :     /*
    1902             :         printf("%d\n",*piAllocOffset);
    1903             :     printf("%d\t%d\t%d\n",pppiAlloc[0][0][0],pppiAlloc[0][0][1],pppiAlloc[0][0][22]);
    1904             : 
    1905             :     printf("%d\t%d\t%d\t%d\n",*piAllocOffset,iAvailableBits,iBitsUsed,iAvailableBits - iBitsUsed);
    1906             :         */
    1907             : #endif
    1908      133227 :     return iBitsUsed;
    1909             : }

Generated by: LCOV version 1.14