decode_indices.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /***********************************************************************
  2. Copyright (c) 2006-2011, Skype Limited. All rights reserved.
  3. Redistribution and use in source and binary forms, with or without
  4. modification, are permitted provided that the following conditions
  5. are met:
  6. - Redistributions of source code must retain the above copyright notice,
  7. this list of conditions and the following disclaimer.
  8. - Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in the
  10. documentation and/or other materials provided with the distribution.
  11. - Neither the name of Internet Society, IETF or IETF Trust, nor the
  12. names of specific contributors, may be used to endorse or promote
  13. products derived from this software without specific prior written
  14. permission.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  19. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  20. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  21. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  22. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  23. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  24. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  25. POSSIBILITY OF SUCH DAMAGE.
  26. ***********************************************************************/
  27. #ifdef HAVE_CONFIG_H
  28. #include "config.h"
  29. #endif
  30. #include "main.h"
  31. /* Decode side-information parameters from payload */
  32. void silk_decode_indices(
  33. silk_decoder_state *psDec, /* I/O State */
  34. ec_dec *psRangeDec, /* I/O Compressor data structure */
  35. opus_int FrameIndex, /* I Frame number */
  36. opus_int decode_LBRR, /* I Flag indicating LBRR data is being decoded */
  37. opus_int condCoding /* I The type of conditional coding to use */
  38. )
  39. {
  40. opus_int i, k, Ix;
  41. opus_int decode_absolute_lagIndex, delta_lagIndex;
  42. opus_int16 ec_ix[ MAX_LPC_ORDER ];
  43. opus_uint8 pred_Q8[ MAX_LPC_ORDER ];
  44. /*******************************************/
  45. /* Decode signal type and quantizer offset */
  46. /*******************************************/
  47. if( decode_LBRR || psDec->VAD_flags[ FrameIndex ] ) {
  48. Ix = ec_dec_icdf( psRangeDec, silk_type_offset_VAD_iCDF, 8 ) + 2;
  49. } else {
  50. Ix = ec_dec_icdf( psRangeDec, silk_type_offset_no_VAD_iCDF, 8 );
  51. }
  52. psDec->indices.signalType = (opus_int8)silk_RSHIFT( Ix, 1 );
  53. psDec->indices.quantOffsetType = (opus_int8)( Ix & 1 );
  54. /****************/
  55. /* Decode gains */
  56. /****************/
  57. /* First subframe */
  58. if( condCoding == CODE_CONDITIONALLY ) {
  59. /* Conditional coding */
  60. psDec->indices.GainsIndices[ 0 ] = (opus_int8)ec_dec_icdf( psRangeDec, silk_delta_gain_iCDF, 8 );
  61. } else {
  62. /* Independent coding, in two stages: MSB bits followed by 3 LSBs */
  63. psDec->indices.GainsIndices[ 0 ] = (opus_int8)silk_LSHIFT( ec_dec_icdf( psRangeDec, silk_gain_iCDF[ psDec->indices.signalType ], 8 ), 3 );
  64. psDec->indices.GainsIndices[ 0 ] += (opus_int8)ec_dec_icdf( psRangeDec, silk_uniform8_iCDF, 8 );
  65. }
  66. /* Remaining subframes */
  67. for( i = 1; i < psDec->nb_subfr; i++ ) {
  68. psDec->indices.GainsIndices[ i ] = (opus_int8)ec_dec_icdf( psRangeDec, silk_delta_gain_iCDF, 8 );
  69. }
  70. /**********************/
  71. /* Decode LSF Indices */
  72. /**********************/
  73. psDec->indices.NLSFIndices[ 0 ] = (opus_int8)ec_dec_icdf( psRangeDec, &psDec->psNLSF_CB->CB1_iCDF[ ( psDec->indices.signalType >> 1 ) * psDec->psNLSF_CB->nVectors ], 8 );
  74. silk_NLSF_unpack( ec_ix, pred_Q8, psDec->psNLSF_CB, psDec->indices.NLSFIndices[ 0 ] );
  75. silk_assert( psDec->psNLSF_CB->order == psDec->LPC_order );
  76. for( i = 0; i < psDec->psNLSF_CB->order; i++ ) {
  77. Ix = ec_dec_icdf( psRangeDec, &psDec->psNLSF_CB->ec_iCDF[ ec_ix[ i ] ], 8 );
  78. if( Ix == 0 ) {
  79. Ix -= ec_dec_icdf( psRangeDec, silk_NLSF_EXT_iCDF, 8 );
  80. } else if( Ix == 2 * NLSF_QUANT_MAX_AMPLITUDE ) {
  81. Ix += ec_dec_icdf( psRangeDec, silk_NLSF_EXT_iCDF, 8 );
  82. }
  83. psDec->indices.NLSFIndices[ i+1 ] = (opus_int8)( Ix - NLSF_QUANT_MAX_AMPLITUDE );
  84. }
  85. /* Decode LSF interpolation factor */
  86. if( psDec->nb_subfr == MAX_NB_SUBFR ) {
  87. psDec->indices.NLSFInterpCoef_Q2 = (opus_int8)ec_dec_icdf( psRangeDec, silk_NLSF_interpolation_factor_iCDF, 8 );
  88. } else {
  89. psDec->indices.NLSFInterpCoef_Q2 = 4;
  90. }
  91. if( psDec->indices.signalType == TYPE_VOICED )
  92. {
  93. /*********************/
  94. /* Decode pitch lags */
  95. /*********************/
  96. /* Get lag index */
  97. decode_absolute_lagIndex = 1;
  98. if( condCoding == CODE_CONDITIONALLY && psDec->ec_prevSignalType == TYPE_VOICED ) {
  99. /* Decode Delta index */
  100. delta_lagIndex = (opus_int16)ec_dec_icdf( psRangeDec, silk_pitch_delta_iCDF, 8 );
  101. if( delta_lagIndex > 0 ) {
  102. delta_lagIndex = delta_lagIndex - 9;
  103. psDec->indices.lagIndex = (opus_int16)( psDec->ec_prevLagIndex + delta_lagIndex );
  104. decode_absolute_lagIndex = 0;
  105. }
  106. }
  107. if( decode_absolute_lagIndex ) {
  108. /* Absolute decoding */
  109. psDec->indices.lagIndex = (opus_int16)ec_dec_icdf( psRangeDec, silk_pitch_lag_iCDF, 8 ) * silk_RSHIFT( psDec->fs_kHz, 1 );
  110. psDec->indices.lagIndex += (opus_int16)ec_dec_icdf( psRangeDec, psDec->pitch_lag_low_bits_iCDF, 8 );
  111. }
  112. psDec->ec_prevLagIndex = psDec->indices.lagIndex;
  113. /* Get countour index */
  114. psDec->indices.contourIndex = (opus_int8)ec_dec_icdf( psRangeDec, psDec->pitch_contour_iCDF, 8 );
  115. /********************/
  116. /* Decode LTP gains */
  117. /********************/
  118. /* Decode PERIndex value */
  119. psDec->indices.PERIndex = (opus_int8)ec_dec_icdf( psRangeDec, silk_LTP_per_index_iCDF, 8 );
  120. for( k = 0; k < psDec->nb_subfr; k++ ) {
  121. psDec->indices.LTPIndex[ k ] = (opus_int8)ec_dec_icdf( psRangeDec, silk_LTP_gain_iCDF_ptrs[ psDec->indices.PERIndex ], 8 );
  122. }
  123. /**********************/
  124. /* Decode LTP scaling */
  125. /**********************/
  126. if( condCoding == CODE_INDEPENDENTLY ) {
  127. psDec->indices.LTP_scaleIndex = (opus_int8)ec_dec_icdf( psRangeDec, silk_LTPscale_iCDF, 8 );
  128. } else {
  129. psDec->indices.LTP_scaleIndex = 0;
  130. }
  131. }
  132. psDec->ec_prevSignalType = psDec->indices.signalType;
  133. /***************/
  134. /* Decode seed */
  135. /***************/
  136. psDec->indices.Seed = (opus_int8)ec_dec_icdf( psRangeDec, silk_uniform4_iCDF, 8 );
  137. }