LCOV - code coverage report
Current view: top level - lib_dec - ivas_stereo_adapt_GR_dec_fx.c (source / functions) Hit Total Coverage
Test: Coverage on main -- dec/rend @ 633e3f2e309758d10805ef21e0436356fe719b7a Lines: 194 195 99.5 %
Date: 2025-08-23 01:22:27 Functions: 11 11 100.0 %

          Line data    Source code
       1             : /******************************************************************************************************
       2             : 
       3             :    (C) 2022-2025 IVAS codec Public Collaboration with portions copyright Dolby International AB, Ericsson AB,
       4             :    Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
       5             :    Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
       6             :    Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
       7             :    contributors to this repository. All Rights Reserved.
       8             : 
       9             :    This software is protected by copyright law and by international treaties.
      10             :    The IVAS codec Public Collaboration consisting of Dolby International AB, Ericsson AB,
      11             :    Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V., Huawei Technologies Co. LTD.,
      12             :    Koninklijke Philips N.V., Nippon Telegraph and Telephone Corporation, Nokia Technologies Oy, Orange,
      13             :    Panasonic Holdings Corporation, Qualcomm Technologies, Inc., VoiceAge Corporation, and other
      14             :    contributors to this repository retain full ownership rights in their respective contributions in
      15             :    the software. This notice grants no license of any kind, including but not limited to patent
      16             :    license, nor is any license granted by implication, estoppel or otherwise.
      17             : 
      18             :    Contributors are required to enter into the IVAS codec Public Collaboration agreement before making
      19             :    contributions.
      20             : 
      21             :    This software is provided "AS IS", without any express or implied warranties. The software is in the
      22             :    development stage. It is intended exclusively for experts who have experience with such software and
      23             :    solely for the purpose of inspection. All implied warranties of non-infringement, merchantability
      24             :    and fitness for a particular purpose are hereby disclaimed and excluded.
      25             : 
      26             :    Any dispute, controversy or claim arising under or in relation to providing this software shall be
      27             :    submitted to and settled by the final, binding jurisdiction of the courts of Munich, Germany in
      28             :    accordance with the laws of the Federal Republic of Germany excluding its conflict of law rules and
      29             :    the United Nations Convention on Contracts on the International Sales of Goods.
      30             : 
      31             : *******************************************************************************************************/
      32             : 
      33             : #include <stdint.h>
      34             : #include "options.h"
      35             : #include "prot_fx.h"
      36             : #include "wmc_auto.h"
      37             : #include "ivas_rom_com.h"
      38             : #include "rom_dec.h"
      39             : #include "ivas_prot_fx.h"
      40             : 
      41             : /*---------------------------------------------------------------------*
      42             :  * read_GR2()
      43             :  *
      44             :  * reading a bitstream of data encoded with GR of order 2
      45             :  *---------------------------------------------------------------------*/
      46             : 
      47             : /*! r: number of bits read */
      48        2753 : static Word16 read_GR2(
      49             :     const UWord16 *bit_stream, /* i  : bitstream to be read                     Q0*/
      50             :     Word16 *ind,               /* o  : parameters read                          Q0*/
      51             :     const Word16 len           /* i  : number of params to be read      Q0*/
      52             : )
      53             : {
      54             :     Word16 i;
      55             :     UWord16 nb, ready, temp, b;
      56             :     const UWord16 *p;
      57             : 
      58        2753 :     p = bit_stream;
      59        2753 :     nb = 0;
      60        2753 :     move16();
      61             : 
      62       25278 :     FOR( i = 0; i < len; i++ )
      63             :     {
      64       22525 :         ready = 0;
      65       22525 :         move16();
      66       22525 :         temp = 0;
      67       22525 :         move16();
      68             :         DO
      69             :         {
      70       41940 :             b = *p++;
      71       41940 :             move16();
      72       41940 :             IF( b == 0 )
      73             :             {
      74       22525 :                 ready = 1;
      75       22525 :                 move16();
      76             :             }
      77             :             ELSE
      78             :             {
      79       19415 :                 temp = (UWord16) ( L_add( temp, 1 ) );
      80       19415 :                 move16();
      81             :             }
      82             :         }
      83       41940 :         WHILE( ready == 0 );
      84       22525 :         b = *p++;
      85       22525 :         move16();
      86       22525 :         b = (UWord16) L_add( L_shl( b, 1 ), *p++ );
      87       22525 :         move16();
      88       22525 :         ind[i] = extract_l( L_add( L_shl( temp, 2 ), b ) );
      89       22525 :         move16();
      90       22525 :         nb = (UWord16) L_add( nb, L_add( temp, 3 ) );
      91       22525 :         move16();
      92             :     }
      93             : 
      94        2753 :     return nb;
      95             : }
      96             : 
      97             : 
      98             : /*---------------------------------------------------------------------*
      99             :  * read_GR1()
     100             :  *
     101             :  * reading a bitstream of data encoded with GR of order 1
     102             :  *---------------------------------------------------------------------*/
     103             : 
     104             : /*! r: number of bits read */
     105       67776 : static Word16 read_GR1(
     106             :     const UWord16 *bit_stream, /* i  : bitstream to be read                     Q0*/
     107             :     Word16 *ind,               /* o  : parameters read                          Q0*/
     108             :     const Word16 len           /* i  : number of params to be read      Q0*/
     109             : )
     110             : {
     111             :     Word16 i;
     112             :     UWord16 nb, ready, temp, b;
     113             :     const UWord16 *p;
     114             : 
     115       67776 :     p = bit_stream;
     116       67776 :     nb = 0;
     117       67776 :     move16();
     118             : 
     119      323800 :     FOR( i = 0; i < len; i++ )
     120             :     {
     121      256024 :         ready = 0;
     122      256024 :         move16();
     123      256024 :         temp = 0;
     124      256024 :         move16();
     125             :         DO
     126             :         {
     127      514077 :             b = *p++;
     128      514077 :             move16();
     129      514077 :             IF( b == 0 )
     130             :             {
     131      256024 :                 ready = 1;
     132      256024 :                 move16();
     133             :             }
     134             :             ELSE
     135             :             {
     136      258053 :                 temp = (UWord16) ( L_add( temp, 1 ) );
     137      258053 :                 move16();
     138             :             }
     139             :         }
     140      514077 :         WHILE( ready == 0 );
     141             : 
     142      256024 :         b = *p++;
     143      256024 :         move16();
     144      256024 :         ind[i] = extract_l( L_add( L_shl( temp, 1 ), b ) ); /* Q0 */
     145      256024 :         move16();
     146      256024 :         nb = (UWord16) L_add( nb, L_add( temp, 2 ) );
     147      256024 :         move16();
     148             :     }
     149             : 
     150       67776 :     return nb;
     151             : }
     152             : 
     153             : 
     154             : /*---------------------------------------------------------------------*
     155             :  * read_GR0()
     156             :  *
     157             :  *  reading a bitstream of data encoded with GR of order 0
     158             :  *---------------------------------------------------------------------*/
     159             : 
     160             : /*! r: number of bits read */
     161       61237 : Word16 read_GR0(
     162             :     const UWord16 *bit_stream, /* i  : bitstream to be read                     Q0*/
     163             :     Word16 *ind,               /* o  : parameters read                          Q0*/
     164             :     const Word16 len           /* i  : number of params to be read      Q0*/
     165             : )
     166             : {
     167             :     Word16 i;
     168             :     UWord16 nb, ready, b, temp;
     169             :     const UWord16 *p;
     170             : 
     171       61237 :     p = bit_stream;
     172       61237 :     nb = 0;
     173       61237 :     move16();
     174             : 
     175      465551 :     FOR( i = 0; i < len; i++ )
     176             :     {
     177      404314 :         ready = 0;
     178      404314 :         move16();
     179      404314 :         temp = 0;
     180      404314 :         move16();
     181             :         DO
     182             :         {
     183      660558 :             b = *p++;
     184      660558 :             move16();
     185      660558 :             IF( b == 0 )
     186             :             {
     187      404314 :                 ready = 1;
     188      404314 :                 move16();
     189             :             }
     190             :             ELSE
     191             :             {
     192      256244 :                 temp = (UWord16) L_add( temp, 1 );
     193      256244 :                 move16();
     194             :             }
     195             :         }
     196      660558 :         WHILE( ready == 0 );
     197      404314 :         ind[i] = temp;
     198      404314 :         move16();
     199      404314 :         nb = (UWord16) L_add( nb, L_add( temp, 1 ) );
     200      404314 :         move16();
     201             :     }
     202             : 
     203       61237 :     return nb;
     204             : }
     205             : 
     206             : 
     207             : /*---------------------------------------------------------------------*
     208             :  * find_map()
     209             :  *
     210             :  * find the position of the value 'val' in the array 'map'
     211             :  *---------------------------------------------------------------------*/
     212             : 
     213             : /*! r: index in array */
     214      384613 : static ivas_error find_map(
     215             :     Word16 *map_idx,   /* Q0 */
     216             :     const Word16 *map, /* i  : array to look into             Q0*/
     217             :     const Word16 val,  /* i  : value to look for              Q0*/
     218             :     const Word16 len   /* i  : length of array                Q0*/
     219             : )
     220             : {
     221      384613 :     *map_idx = 0;
     222      384613 :     move16();
     223             : 
     224     5263321 :     WHILE( ( NE_16( map[*map_idx], val ) ) && ( NE_16( *map_idx, len ) ) )
     225             :     {
     226     4878708 :         test();
     227     4878708 :         *map_idx = add( *map_idx, 1 ); /* Q0 */
     228     4878708 :         move16();
     229             :     }
     230             : 
     231             : 
     232      384613 :     return IVAS_ERR_OK;
     233             : }
     234             : 
     235             : 
     236             : /*---------------------------------------------------------------------*
     237             :  * decode_adapt_GR_indices1()
     238             :  *
     239             :  * decodes input array using the adaptive GR procedure
     240             :  *---------------------------------------------------------------------*/
     241             : 
     242       42031 : static void decode_adapt_GR_indices1(
     243             :     const Word16 *ind,    /* i  : array of input encoded symbols                                Q0*/
     244             :     const Word16 len,     /* i  : number of parameters to decode                                Q0*/
     245             :     const Word16 no_symb, /* i  : number of possible symbols in GR coding               Q0*/
     246             :     Word16 *out,          /* o  : array of decoded parameters                                   Q0*/
     247             :     const Word16 *map0    /* i  : initial mapping array for the adaptive GR             Q0*/
     248             : )
     249             : {
     250             :     const Word16 *map;
     251             :     Word16 map_symb, i;
     252             : 
     253       42031 :     IF( EQ_16( no_symb, NO_SYMB_GR_SIDE_G ) )
     254             :     {
     255       34648 :         map = &map0[15 * no_symb];
     256             :     }
     257             :     ELSE
     258             :     {
     259        7383 :         map = &( map0[8 * no_symb] );
     260             :     }
     261             : 
     262      426644 :     FOR( i = 0; i < len; i++ )
     263             :     {
     264      384613 :         map_symb = ind[i];
     265      384613 :         move16();
     266      384613 :         find_map( out + i, map, map_symb, no_symb );
     267      384613 :         map = &( map0[out[i] * no_symb] );
     268             :     }
     269             : 
     270       42031 :     return;
     271             : }
     272             : 
     273             : 
     274             : /*---------------------------------------------------------------------*
     275             :  * get_value()
     276             :  *
     277             :  * reads a value on 'nbits' from a bitstream
     278             :  *---------------------------------------------------------------------*/
     279             : 
     280             : /*! r: read value */
     281       29871 : Word16 get_value(
     282             :     const UWord16 *bit_stream, /* i  : bitstream                                        Q0*/
     283             :     const Word16 nbits         /* i  : number of bits to be read        Q0*/
     284             : )
     285             : {
     286             :     Word16 i;
     287       29871 :     UWord16 mask = 0, val = 0;
     288       29871 :     move16();
     289       29871 :     move16();
     290             : 
     291      135009 :     FOR( i = nbits - 1; i >= 0; i-- )
     292             :     {
     293      105138 :         val = (UWord16) L_add( val, L_shl( bit_stream[i], mask ) );
     294      105138 :         mask = (UWord16) L_add( mask, 1 );
     295      105138 :         move16();
     296      105138 :         move16();
     297             :     }
     298             : 
     299       29871 :     return val;
     300             : }
     301             : 
     302             : 
     303             : /*---------------------------------------------------------------------*
     304             :  * read_BS_GR()
     305             :  *
     306             :  * decode simple GR code from a bitstream
     307             :  *---------------------------------------------------------------------*/
     308             : 
     309             : /*! r: number of bits read */
     310       38540 : Word16 read_BS_GR(
     311             :     const UWord16 *bit_stream, /* i  : bitstream to be read         Q0*/
     312             :     const Word16 nb,           /* i  : starting point in bitstream  Q0*/
     313             :     Word16 *ind1,              /* o  : data array read              Q0*/
     314             :     const Word16 len,          /* i  : number of params to be read  Q0*/
     315             :     Word16 *GR_ord             /* o  : GR order to be used          Q0*/
     316             : )
     317             : {
     318             :     Word16 b, ind1_tmp[STEREO_DFT_BAND_MAX], tmp, i;
     319             : 
     320       38540 :     *GR_ord = bit_stream[nb];
     321       38540 :     move16();
     322       38540 :     b = 1;
     323       38540 :     move16();
     324             : 
     325       38540 :     IF( *GR_ord == 0 )
     326             :     {
     327       37861 :         b = add( b, read_GR0( &bit_stream[nb + b], ind1_tmp, len ) ); /* Q0 */
     328             :     }
     329             :     ELSE
     330             :     {
     331         679 :         b = add( b, read_GR1( &bit_stream[nb + b], ind1_tmp, len ) ); /* Q0 */
     332             :     }
     333             : 
     334      327626 :     FOR( i = 0; i < len; i++ )
     335             :     {
     336      289086 :         tmp = add( ind1_tmp[i], 1 );
     337      289086 :         IF( s_and( tmp, 1 ) ) /* if odd number */
     338             :         {
     339      234736 :             ind1[i] = negate( shr( ind1_tmp[i], 1 ) ); /* Q0 */
     340      234736 :             move16();
     341             :         }
     342             :         ELSE
     343             :         {
     344       54350 :             ind1[i] = shr( tmp, 1 ); /* Q0 */
     345       54350 :             move16();
     346             :         }
     347             :     }
     348             : 
     349       38540 :     return b;
     350             : }
     351             : 
     352             : 
     353             : /*---------------------------------------------------------------------*
     354             :  * read_BS_adapt_GR_sg()
     355             :  *
     356             :  * read and decode with adaptive GR the bitstream containing side gains values
     357             :  *---------------------------------------------------------------------*/
     358             : 
     359             : /*! r: number of bits read */
     360       34648 : Word16 read_BS_adapt_GR_sg(
     361             :     const UWord16 *bit_stream, /* i  : bitstream to be read                                     Q0*/
     362             :     const Word16 nb,           /* i  : starting position in bitstream           Q0*/
     363             :     Word16 *ind1,              /* o  : decoded side gain values                         Q0*/
     364             :     const Word16 len,          /* i  : number of params to be read                      Q0*/
     365             :     Word16 *GR_ord,            /* o  : GR order used (read from bitstream)      Q0*/
     366             :     const Word16 *map0         /* i  : initial map                                                      Q0*/
     367             : )
     368             : {
     369             :     Word16 b, ind1_tmp[STEREO_DFT_BAND_MAX], ord;
     370             : 
     371             :     /* read first component */
     372       34648 :     b = 0;
     373       34648 :     move16();
     374       34648 :     b = add( b, read_GR1( &bit_stream[nb], ind1_tmp, 1 ) );
     375             :     /* read GR ord */
     376       34648 :     ord = bit_stream[nb + b];
     377       34648 :     move16();
     378       34648 :     b = add( b, 1 );
     379             : 
     380       34648 :     IF( ord == 0 )
     381             :     {
     382       22907 :         *GR_ord = 1;
     383       22907 :         move16();
     384       22907 :         b = add( b, read_GR1( &bit_stream[nb + b], &ind1_tmp[1], sub( len, 1 ) ) ); /* Q0 */
     385             :     }
     386             :     ELSE
     387             :     {
     388       11741 :         ord = bit_stream[nb + b];
     389       11741 :         move16();
     390       11741 :         b = b + 1;
     391       11741 :         IF( ord == 0 )
     392             :         {
     393        8988 :             *GR_ord = 0;
     394        8988 :             move16();
     395             : 
     396        8988 :             b = add( b, read_GR0( &bit_stream[nb + b], &ind1_tmp[1], sub( len, 1 ) ) ); /* Q0 */
     397             :         }
     398             :         ELSE
     399             :         {
     400        2753 :             *GR_ord = 2;
     401        2753 :             move16();
     402        2753 :             b = add( b, read_GR2( &bit_stream[nb + b], &ind1_tmp[1], sub( len, 1 ) ) ); /* Q0 */
     403             :         }
     404             :     }
     405             : 
     406       34648 :     decode_adapt_GR_indices1( ind1_tmp, len, NO_SYMB_GR_SIDE_G, ind1, map0 );
     407             : 
     408       34648 :     return b;
     409             : }
     410             : 
     411             : 
     412             : /*---------------------------------------------------------------------*
     413             :  * read_itd()
     414             :  *
     415             :  * read and decode ITD in DFT mode
     416             :  *---------------------------------------------------------------------*/
     417             : 
     418             : /*! r: number of bits read */
     419       29255 : Word16 read_itd(
     420             :     Decoder_State *st, /* i  : Decoder state           */
     421             :     Word16 *pI         /* o  : ITD value             Q0*/
     422             : )
     423             : {
     424       29255 :     Word16 huff_flag, sign_flag, I, i, nb = 0, ready;
     425       29255 :     move16();
     426             : 
     427       29255 :     huff_flag = extract_l( get_next_indice_fx( st, 1 ) );
     428       29255 :     sign_flag = extract_l( get_next_indice_fx( st, 1 ) );
     429       29255 :     nb = add( nb, 2 );
     430             : 
     431       29255 :     IF( EQ_16( huff_flag, 1 ) )
     432             :     {
     433       15925 :         ready = 0;
     434       15925 :         move16();
     435       15925 :         I = 0;
     436       15925 :         move16();
     437             : 
     438      111611 :         WHILE( ( ready == 0 ) && ( LT_16( nb, 10 ) ) )
     439             :         {
     440       95686 :             test();
     441       95686 :             I = add( shl( I, 1 ), extract_l( get_next_indice_fx( st, 1 ) ) );
     442       95686 :             nb = add( nb, 1 );
     443             : 
     444     1894236 :             FOR( i = 0; i < 20; i++ )
     445             :             {
     446     1814475 :                 test();
     447     1814475 :                 IF( ( EQ_16( I, dft_code_itd[i] ) ) && ( EQ_16( dft_len_itd[i], ( sub( nb, 2 ) ) ) ) )
     448             :                 {
     449       15925 :                     I = i;
     450       15925 :                     move16();
     451       15925 :                     ready = 1;
     452       15925 :                     move16();
     453       15925 :                     BREAK;
     454             :                 }
     455             :             }
     456             :         }
     457       15925 :         IF( ready == 0 )
     458             :         {
     459           0 :             printf( "Error reading Huffman code for ITD: \n" );
     460             :         }
     461             :     }
     462             :     ELSE
     463             :     {
     464       13330 :         I = extract_l( get_next_indice_fx( st, STEREO_DFT_ITD_NBITS - 1 ) );
     465       13330 :         nb = add( nb, STEREO_DFT_ITD_NBITS - 1 );
     466             :     }
     467             : 
     468       29255 :     I = add( I, shl( sign_flag, 8 ) );
     469       29255 :     *pI = I;
     470       29255 :     move16();
     471             : 
     472       29255 :     return nb;
     473             : }
     474             : 
     475             : 
     476             : /*---------------------------------------------------------------------*
     477             :  * read_BS_adapt_GR_rpg()
     478             :  *
     479             :  * read and decode residual prediction gain values using adaptive GR
     480             :  *---------------------------------------------------------------------*/
     481             : 
     482             : /*! r: number of bits read */
     483        8355 : Word16 read_BS_adapt_GR_rpg(
     484             :     const UWord16 *bit_stream, /* i  : bitstream to be read         Q0*/
     485             :     const Word16 nb,           /* i  : starting point in bitstream  Q0*/
     486             :     Word16 *ind1_pred,         /* o  : decoded res pred gains       Q0*/
     487             :     const Word16 start,        /* i  : starting subband             Q0*/
     488             :     const Word16 total_no,     /* i  : number of params to be read  Q0*/
     489             :     Word16 *GR_ord             /* o  : GR order - read              Q0*/
     490             : )
     491             : {
     492             :     Word16 b, ind1_tmp[STEREO_DFT_BAND_MAX], i, len;
     493        8355 :     len = sub( total_no, start );
     494             : 
     495             :     /* read first band */
     496        8355 :     b = read_GR1( &bit_stream[nb], ind1_tmp, 1 ); /* Q0 */
     497             : 
     498        8355 :     IF( EQ_16( ind1_tmp[0], dft_maps_rpg[8 * NO_SYMB_GR_PRED_G] ) )
     499             :     {
     500        6818 :         FOR( i = start; i < total_no; i++ )
     501             :         {
     502        5846 :             ind1_pred[i] = 0;
     503        5846 :             move16();
     504             :         }
     505             :     }
     506             :     ELSE
     507             :     {
     508        7383 :         *GR_ord = bit_stream[nb + b]; /* GR order */
     509        7383 :         move16();
     510        7383 :         b = add( b, 1 );
     511        7383 :         IF( *GR_ord == 0 )
     512             :         {
     513        6196 :             b = add( b, read_GR0( &bit_stream[nb + b], &ind1_tmp[1], sub( len, 1 ) ) ); /* Q0 */
     514             :         }
     515             :         ELSE
     516             :         {
     517             :             /* GR ord 1 */
     518        1187 :             b = add( b, read_GR1( &bit_stream[nb + b], &ind1_tmp[1], sub( len, 1 ) ) ); /* Q0 */
     519             :         }
     520        7383 :         decode_adapt_GR_indices1( ind1_tmp, sub( total_no, start ), NO_SYMB_GR_PRED_G, &ind1_pred[start], dft_maps_rpg );
     521             :     }
     522             : 
     523        8355 :     return b;
     524             : }
     525             : 
     526             : 
     527             : /*---------------------------------------------------------------------*
     528             :  * read_flag_EC_DFT()
     529             :  *
     530             :  * read flag differentiating between: CBR/EC with adaptive GR/GR for differential coding
     531             :  *---------------------------------------------------------------------*/
     532             : 
     533             : /*! r: number of bits read */
     534       85833 : Word16 read_flag_EC_DFT(
     535             :     const UWord16 *bit_stream, /* i  : bitstream            Q0*/
     536             :     Word16 *flag               /* o  : flag value           Q0*/
     537             : )
     538             : {
     539             :     Word16 flg;
     540             : 
     541       85833 :     flg = bit_stream[0];
     542       85833 :     move16();
     543       85833 :     IF( flg == 0 )
     544             :     {
     545       43003 :         *flag = flg;
     546       43003 :         move16();
     547       43003 :         return 1;
     548             :     }
     549             :     ELSE
     550             :     {
     551       42830 :         *flag = add( 2, bit_stream[1] );
     552       42830 :         move16();
     553       42830 :         return 2;
     554             :     }
     555             : }

Generated by: LCOV version 1.14