LCOV - code coverage report
Current view: top level - lib_isar - isar_MSPred.c (source / functions) Hit Total Coverage
Test: Coverage on main -- merged total coverage @ 9b04ec3cb36f5e8dc438cf854fa3e349998fa1e9 Lines: 122 124 98.4 %
Date: 2025-10-31 05:45:46 Functions: 12 13 92.3 %

          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 "options.h"
      34             : #include <stdint.h>
      35             : #include "isar_lcld_prot.h"
      36             : #include "isar_prot.h"
      37             : #include "wmc_auto.h"
      38             : 
      39             : 
      40             : /*-------------------------------------------------------------------*
      41             :  * Function _round()
      42             :  *
      43             :  *
      44             :  *-------------------------------------------------------------------*/
      45             : 
      46    18082265 : static int32_t _round(
      47             :     float val )
      48             : {
      49    18082265 :     return ( val > 0.0f ? (int32_t) ( val + 0.5f ) : (int32_t) ( val - 0.5f ) );
      50             : }
      51             : 
      52             : 
      53             : /*-------------------------------------------------------------------*
      54             :  * Function quantPhase()
      55             :  *
      56             :  *
      57             :  *-------------------------------------------------------------------*/
      58             : 
      59     3781869 : int32_t quantPhase(
      60             :     float phase )
      61             : {
      62             :     int32_t phaseQ;
      63             : 
      64     3781869 :     phaseQ = _round( phase * PHASE_QUANT_FACTOR );
      65     3781869 :     if ( phaseQ == PHASE_MAX_VAL )
      66             :     {
      67       37029 :         phaseQ = PHASE_MIN_VAL;
      68             :     }
      69     3781869 :     return phaseQ;
      70             : }
      71             : 
      72             : 
      73             : /*-------------------------------------------------------------------*
      74             :  * Function cplxmult_lcld()
      75             :  *
      76             :  *
      77             :  *-------------------------------------------------------------------*/
      78             : 
      79   220288382 : void cplxmult_lcld(
      80             :     float *pr1,
      81             :     float *pi1,
      82             :     const float r2,
      83             :     const float i2 )
      84             : {
      85   220288382 :     float r1 = *pr1, i1 = *pi1;
      86             : 
      87   220288382 :     *pr1 = r1 * r2 - i1 * i2;
      88   220288382 :     *pi1 = r1 * i2 + i1 * r2;
      89             : 
      90   220288382 :     return;
      91             : }
      92             : 
      93             : 
      94             : /*-------------------------------------------------------------------*
      95             :  * Function requantPhase()
      96             :  *
      97             :  *
      98             :  *-------------------------------------------------------------------*/
      99             : 
     100    17565235 : int32_t requantPhase(
     101             :     int32_t phaseQ )
     102             : {
     103             : 
     104    17565235 :     if ( phaseQ >= PHASE_MAX_VAL )
     105             :     {
     106      760068 :         phaseQ += PHASE_MAX_VAL;
     107      760068 :         phaseQ %= ( 2 * PHASE_MAX_VAL );
     108      760068 :         phaseQ -= PHASE_MAX_VAL;
     109             :     }
     110    16805167 :     else if ( phaseQ < PHASE_MIN_VAL )
     111             :     {
     112      644376 :         phaseQ -= PHASE_MAX_VAL;
     113      644376 :         phaseQ %= ( 2 * PHASE_MAX_VAL );
     114      644376 :         phaseQ += PHASE_MAX_VAL;
     115      644376 :         if ( phaseQ == PHASE_MAX_VAL )
     116             :         {
     117       10422 :             phaseQ = PHASE_MIN_VAL;
     118             :         }
     119             :     }
     120             : 
     121    17565235 :     return phaseQ;
     122             : }
     123             : 
     124             : 
     125             : /*-------------------------------------------------------------------*
     126             :  * Function quantPred()
     127             :  *
     128             :  *
     129             :  *-------------------------------------------------------------------*/
     130             : 
     131    14300396 : int32_t quantPred(
     132             :     const float pred )
     133             : {
     134    14300396 :     int32_t predQ = _round( pred * PRED_QUANT_FACTOR );
     135    14300396 :     predQ = predQ > PRED_MAX_VAL ? PRED_MAX_VAL : predQ;
     136    14300396 :     predQ = predQ < PRED_MIN_VAL ? PRED_MIN_VAL : predQ;
     137             : 
     138    14300396 :     return predQ;
     139             : }
     140             : 
     141             : 
     142             : /*-------------------------------------------------------------------*
     143             :  * Function dequantPhase()
     144             :  *
     145             :  *
     146             :  *-------------------------------------------------------------------*/
     147             : 
     148           0 : float dequantPhase(
     149             :     const int32_t phaseQ )
     150             : {
     151           0 :     return (float) phaseQ / PHASE_QUANT_FACTOR;
     152             : }
     153             : 
     154             : 
     155             : /*-------------------------------------------------------------------*
     156             :  * Function dequantPred()
     157             :  *
     158             :  *
     159             :  *-------------------------------------------------------------------*/
     160             : 
     161    24039706 : float dequantPred( int32_t predQ )
     162             : {
     163    24039706 :     return (float) predQ / PRED_QUANT_FACTOR;
     164             : }
     165             : 
     166             : 
     167             : /*-------------------------------------------------------------------*
     168             :  * Function PrepEncode()
     169             :  *
     170             :  *
     171             :  *-------------------------------------------------------------------*/
     172             : 
     173     2343878 : int32_t PrepEncode(
     174             :     int32_t *piQuant,
     175             :     const int32_t *piMSFlags,
     176             :     const int32_t numBands )
     177             : {
     178     2343878 :     int32_t b, numMSBands = 0;
     179             : 
     180    53909194 :     for ( b = 0; b < numBands; b++ )
     181             :     {
     182    51565316 :         if ( piMSFlags[b] )
     183             :         {
     184    33074998 :             piQuant[numMSBands++] = piQuant[b];
     185             :         }
     186             :     }
     187             : 
     188    20834196 :     for ( b = numMSBands; b < numBands; b++ )
     189             :     {
     190    18490318 :         piQuant[b] = 0;
     191             :     }
     192             : 
     193     2343878 :     return numMSBands;
     194             : }
     195             : 
     196             : 
     197             : /*-------------------------------------------------------------------*
     198             :  * Function EncodePhase()
     199             :  *
     200             :  *
     201             :  *-------------------------------------------------------------------*/
     202             : 
     203     1171939 : void EncodePhase(
     204             :     int32_t *phaseQuant,
     205             :     const int32_t numMSBands,
     206             :     const int32_t diffDim )
     207             : {
     208             :     int32_t b;
     209             : 
     210     1171939 :     if ( diffDim > 0 )
     211             :     {
     212             :         int32_t tmp1, tmp2;
     213     1171939 :         tmp1 = phaseQuant[0];
     214    16537499 :         for ( b = 1; b < numMSBands; b++ )
     215             :         {
     216    15365560 :             tmp2 = phaseQuant[b];
     217    15365560 :             phaseQuant[b] -= tmp1;
     218    15365560 :             tmp1 = tmp2;
     219             :         }
     220             :     }
     221             : 
     222     1171939 :     if ( diffDim > 1 )
     223             :     {
     224             :         int32_t tmp1, tmp2;
     225     1171939 :         tmp1 = phaseQuant[1];
     226    15375636 :         for ( b = 2; b < numMSBands; b++ )
     227             :         {
     228    14203697 :             tmp2 = phaseQuant[b];
     229    14203697 :             phaseQuant[b] -= tmp1;
     230    14203697 :             tmp1 = tmp2;
     231             :         }
     232             :     }
     233    16537499 :     for ( b = 1; b < numMSBands; b++ )
     234             :     {
     235    15365560 :         phaseQuant[b] = requantPhase( phaseQuant[b] );
     236             :     }
     237             : 
     238     1171939 :     return;
     239             : }
     240             : 
     241             : 
     242             : /*-------------------------------------------------------------------*
     243             :  * Function DecodePhase()
     244             :  *
     245             :  *
     246             :  *-------------------------------------------------------------------*/
     247             : 
     248      153529 : void DecodePhase(
     249             :     int32_t *phaseQuant,
     250             :     const int32_t numMSBands,
     251             :     const int32_t diffDim )
     252             : {
     253             :     int32_t b;
     254             : 
     255      153529 :     if ( diffDim > 1 )
     256             :     {
     257     2200333 :         for ( b = 2; b < numMSBands; b++ )
     258             :         {
     259     2046804 :             phaseQuant[b] += phaseQuant[b - 1];
     260             :         }
     261             :     }
     262             : 
     263      153529 :     if ( diffDim > 0 )
     264             :     {
     265     2353204 :         for ( b = 1; b < numMSBands; b++ )
     266             :         {
     267     2199675 :             phaseQuant[b] += phaseQuant[b - 1];
     268             :         }
     269             :     }
     270             : 
     271     2353204 :     for ( b = 1; b < numMSBands; b++ )
     272             :     {
     273     2199675 :         phaseQuant[b] = requantPhase( phaseQuant[b] );
     274             :     }
     275             : 
     276      153529 :     return;
     277             : }
     278             : 
     279             : 
     280             : /*-------------------------------------------------------------------*
     281             :  * Function EncodePredCoef()
     282             :  *
     283             :  *
     284             :  *-------------------------------------------------------------------*/
     285             : 
     286     1171939 : int32_t EncodePredCoef(
     287             :     int32_t *predQuant,
     288             :     const int32_t numMSBands )
     289             : {
     290             :     int32_t b, tmp1, tmp2;
     291             : 
     292     1171939 :     tmp1 = predQuant[0];
     293    16537499 :     for ( b = 1; b < numMSBands; b++ )
     294             :     {
     295    15365560 :         tmp2 = predQuant[b];
     296    15365560 :         predQuant[b] -= tmp1;
     297    15365560 :         tmp1 = tmp2;
     298             :     }
     299             : 
     300     1171939 :     return numMSBands;
     301             : }
     302             : 
     303             : 
     304             : /*-------------------------------------------------------------------*
     305             :  * Function DecodePredCoef()
     306             :  *
     307             :  *
     308             :  *-------------------------------------------------------------------*/
     309             : 
     310      192865 : void DecodePredCoef(
     311             :     int32_t *phaseQuant,
     312             :     const int32_t numMSBands )
     313             : {
     314             :     int32_t b;
     315             : 
     316     3106654 :     for ( b = 1; b < numMSBands; b++ )
     317             :     {
     318     2913789 :         phaseQuant[b] += phaseQuant[b - 1];
     319             :     }
     320             : 
     321      192865 :     return;
     322             : }
     323             : 
     324             : 
     325             : /*-------------------------------------------------------------------*
     326             :  * Function CountMSBits()
     327             :  *
     328             :  *
     329             :  *-------------------------------------------------------------------*/
     330             : 
     331     1300036 : int32_t CountMSBits(
     332             :     int32_t iNumBands,
     333             :     const int32_t iMSMode,
     334             :     const int32_t *piMSFlags,
     335             :     const int32_t *piLRPhaseDiff,
     336             :     const int32_t *piMSPredCoef )
     337             : {
     338     1300036 :     int32_t iBitsWritten = 0;
     339             :     int32_t b;
     340             :     int32_t anyNonZero;
     341     1300036 :     int32_t iNumMSBands = 0;
     342             :     int32_t piPhaseCopy[MAX_BANDS_48];
     343             :     int32_t piPredCopy[MAX_BANDS_48];
     344             : 
     345    31200864 :     for ( b = 0; b < MAX_BANDS_48; b++ )
     346             :     {
     347    29900828 :         piPhaseCopy[b] = 0;
     348    29900828 :         piPredCopy[b] = 0;
     349             :     }
     350             : 
     351     1300036 :     iBitsWritten += 2; /* iMSMode */
     352    29900828 :     for ( b = 0; b < iNumBands; b++ )
     353             :     {
     354    28600792 :         iNumMSBands += ( piMSFlags[b] != 0 );
     355             :     }
     356             : 
     357     1300036 :     if ( iNumMSBands == 0 )
     358             :     {
     359        5480 :         return iBitsWritten;
     360             :     }
     361             : 
     362     1294556 :     if ( iNumMSBands < iNumBands )
     363             :     {
     364     1220254 :         iBitsWritten += iNumBands; /* piMSFlags */
     365             :     }
     366             : 
     367     1294556 :     if ( iMSMode < 3 )
     368             :     {
     369      323511 :         return iBitsWritten;
     370             :     }
     371             : 
     372             :     /* prepare arrays for coding evaluation */
     373    22334035 :     for ( b = 0; b < iNumBands; b++ )
     374             :     {
     375    21362990 :         piPhaseCopy[b] = piLRPhaseDiff[b];
     376    21362990 :         piPredCopy[b] = piMSPredCoef[b];
     377             :     }
     378             : 
     379             :     /* Differential Coding of Phase Data*/
     380      971045 :     PrepEncode( piPhaseCopy, piMSFlags, iNumBands );
     381      971045 :     PrepEncode( piPredCopy, piMSFlags, iNumBands );
     382      971045 :     EncodePhase( piPhaseCopy, iNumMSBands, PHASE_DIFF_DIM );
     383      971045 :     EncodePredCoef( piPredCopy, iNumMSBands );
     384             : 
     385      971045 :     iBitsWritten += 1; /* iMSPredAll */
     386      971045 :     anyNonZero = 0;    /* phase */
     387     6096089 :     for ( b = 0; b < iNumMSBands; b++ )
     388             :     {
     389     5749557 :         if ( piPhaseCopy[b] != 0 )
     390             :         {
     391      624513 :             anyNonZero = 1;
     392      624513 :             break;
     393             :         }
     394             :     }
     395      971045 :     iBitsWritten++; /*anyNonZero Phase*/
     396      971045 :     if ( anyNonZero )
     397             :     {
     398      624513 :         iBitsWritten += PHASE_BAND0_BITS;
     399     8681075 :         for ( b = 1; b < iNumMSBands; b++ )
     400             :         {
     401     8056562 :             int32_t tabIdx = piPhaseCopy[b] - ENV_DELTA_MIN;
     402     8056562 :             iBitsWritten += c_aaiRMSEnvHuffEnc[tabIdx][0];
     403             :         }
     404             :     }
     405      971045 :     anyNonZero = 0; /* prediction */
     406     5192150 :     for ( b = 0; b < iNumMSBands; b++ )
     407             :     {
     408     4864781 :         if ( piPredCopy[b] != 0 )
     409             :         {
     410      643676 :             anyNonZero = 1;
     411      643676 :             break;
     412             :         }
     413             :     }
     414             : 
     415      971045 :     iBitsWritten++; /* any nonZero Pred */
     416      971045 :     if ( anyNonZero )
     417             :     {
     418      643676 :         iBitsWritten += PRED_BAND0_BITS;
     419     9473514 :         for ( b = 1; b < iNumMSBands; b++ )
     420             :         {
     421     8829838 :             int32_t tabIdx = piPredCopy[b] - ENV_DELTA_MIN;
     422     8829838 :             iBitsWritten += c_aaiRMSEnvHuffEnc[tabIdx][0];
     423             :         }
     424             :     }
     425             : 
     426      971045 :     return iBitsWritten;
     427             : }
     428             : 
     429             : 
     430             : #ifdef DEBUG_WRITE_MS_PRED
     431             : void writeMSPred(
     432             :     int32_t *phaseQuant,
     433             :     int32_t *predQuant,
     434             :     int32_t MSMode,
     435             :     int32_t numMSBands,
     436             :     int32_t numBands,
     437             :     void *fid,
     438             :     int32_t *piMsFlags )
     439             : {
     440             :     int32_t b;
     441             : 
     442             :     fid = (FILE *) fid;
     443             :     fprintf( fid, "%d %d", MSMode, numMSBands );
     444             :     for ( b = 0; b < numMSBands; b++ )
     445             :     {
     446             :         fprintf( fid, " %d", phaseQuant[b] );
     447             :     }
     448             :     for ( b = numMSBands; b < numBands; b++ )
     449             :     {
     450             :         fprintf( fid, " %d", 0 );
     451             :     }
     452             :     for ( b = 0; b < numMSBands; b++ )
     453             :     {
     454             :         fprintf( fid, " %d", predQuant[b] );
     455             :     }
     456             :     for ( b = numMSBands; b < numBands; b++ )
     457             :     {
     458             :         fprintf( fid, " %d", 0 );
     459             :     }
     460             :     for ( b = 0; b < numBands; b++ )
     461             :     {
     462             :         fprintf( fid, " %d", piMsFlags[b] );
     463             :     }
     464             :     fprintf( fid, "\n" );
     465             : 
     466             :     return;
     467             : }
     468             : #endif

Generated by: LCOV version 1.14