gain_quant.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. #define OFFSET ( ( MIN_QGAIN_DB * 128 ) / 6 + 16 * 128 )
  32. #define SCALE_Q16 ( ( 65536 * ( N_LEVELS_QGAIN - 1 ) ) / ( ( ( MAX_QGAIN_DB - MIN_QGAIN_DB ) * 128 ) / 6 ) )
  33. #define INV_SCALE_Q16 ( ( 65536 * ( ( ( MAX_QGAIN_DB - MIN_QGAIN_DB ) * 128 ) / 6 ) ) / ( N_LEVELS_QGAIN - 1 ) )
  34. /* Gain scalar quantization with hysteresis, uniform on log scale */
  35. void silk_gains_quant(
  36. opus_int8 ind[ MAX_NB_SUBFR ], /* O gain indices */
  37. opus_int32 gain_Q16[ MAX_NB_SUBFR ], /* I/O gains (quantized out) */
  38. opus_int8 *prev_ind, /* I/O last index in previous frame */
  39. const opus_int conditional, /* I first gain is delta coded if 1 */
  40. const opus_int nb_subfr /* I number of subframes */
  41. )
  42. {
  43. opus_int k, double_step_size_threshold;
  44. for( k = 0; k < nb_subfr; k++ ) {
  45. /* Convert to log scale, scale, floor() */
  46. ind[ k ] = silk_SMULWB( SCALE_Q16, silk_lin2log( gain_Q16[ k ] ) - OFFSET );
  47. /* Round towards previous quantized gain (hysteresis) */
  48. if( ind[ k ] < *prev_ind ) {
  49. ind[ k ]++;
  50. }
  51. ind[ k ] = silk_LIMIT_int( ind[ k ], 0, N_LEVELS_QGAIN - 1 );
  52. /* Compute delta indices and limit */
  53. if( k == 0 && conditional == 0 ) {
  54. /* Full index */
  55. ind[ k ] = silk_LIMIT_int( ind[ k ], *prev_ind + MIN_DELTA_GAIN_QUANT, N_LEVELS_QGAIN - 1 );
  56. *prev_ind = ind[ k ];
  57. } else {
  58. /* Delta index */
  59. ind[ k ] = ind[ k ] - *prev_ind;
  60. /* Double the quantization step size for large gain increases, so that the max gain level can be reached */
  61. double_step_size_threshold = 2 * MAX_DELTA_GAIN_QUANT - N_LEVELS_QGAIN + *prev_ind;
  62. if( ind[ k ] > double_step_size_threshold ) {
  63. ind[ k ] = double_step_size_threshold + silk_RSHIFT( ind[ k ] - double_step_size_threshold + 1, 1 );
  64. }
  65. ind[ k ] = silk_LIMIT_int( ind[ k ], MIN_DELTA_GAIN_QUANT, MAX_DELTA_GAIN_QUANT );
  66. /* Accumulate deltas */
  67. if( ind[ k ] > double_step_size_threshold ) {
  68. *prev_ind += silk_LSHIFT( ind[ k ], 1 ) - double_step_size_threshold;
  69. *prev_ind = silk_min_int( *prev_ind, N_LEVELS_QGAIN - 1 );
  70. } else {
  71. *prev_ind += ind[ k ];
  72. }
  73. /* Shift to make non-negative */
  74. ind[ k ] -= MIN_DELTA_GAIN_QUANT;
  75. }
  76. /* Scale and convert to linear scale */
  77. gain_Q16[ k ] = silk_log2lin( silk_min_32( silk_SMULWB( INV_SCALE_Q16, *prev_ind ) + OFFSET, 3967 ) ); /* 3967 = 31 in Q7 */
  78. }
  79. }
  80. /* Gains scalar dequantization, uniform on log scale */
  81. void silk_gains_dequant(
  82. opus_int32 gain_Q16[ MAX_NB_SUBFR ], /* O quantized gains */
  83. const opus_int8 ind[ MAX_NB_SUBFR ], /* I gain indices */
  84. opus_int8 *prev_ind, /* I/O last index in previous frame */
  85. const opus_int conditional, /* I first gain is delta coded if 1 */
  86. const opus_int nb_subfr /* I number of subframes */
  87. )
  88. {
  89. opus_int k, ind_tmp, double_step_size_threshold;
  90. for( k = 0; k < nb_subfr; k++ ) {
  91. if( k == 0 && conditional == 0 ) {
  92. /* Gain index is not allowed to go down more than 16 steps (~21.8 dB) */
  93. *prev_ind = silk_max_int( ind[ k ], *prev_ind - 16 );
  94. } else {
  95. /* Delta index */
  96. ind_tmp = ind[ k ] + MIN_DELTA_GAIN_QUANT;
  97. /* Accumulate deltas */
  98. double_step_size_threshold = 2 * MAX_DELTA_GAIN_QUANT - N_LEVELS_QGAIN + *prev_ind;
  99. if( ind_tmp > double_step_size_threshold ) {
  100. *prev_ind += silk_LSHIFT( ind_tmp, 1 ) - double_step_size_threshold;
  101. } else {
  102. *prev_ind += ind_tmp;
  103. }
  104. }
  105. *prev_ind = silk_LIMIT_int( *prev_ind, 0, N_LEVELS_QGAIN - 1 );
  106. /* Scale and convert to linear scale */
  107. gain_Q16[ k ] = silk_log2lin( silk_min_32( silk_SMULWB( INV_SCALE_Q16, *prev_ind ) + OFFSET, 3967 ) ); /* 3967 = 31 in Q7 */
  108. }
  109. }
  110. /* Compute unique identifier of gain indices vector */
  111. opus_int32 silk_gains_ID( /* O returns unique identifier of gains */
  112. const opus_int8 ind[ MAX_NB_SUBFR ], /* I gain indices */
  113. const opus_int nb_subfr /* I number of subframes */
  114. )
  115. {
  116. opus_int k;
  117. opus_int32 gainsID;
  118. gainsID = 0;
  119. for( k = 0; k < nb_subfr; k++ ) {
  120. gainsID = silk_ADD_LSHIFT32( ind[ k ], gainsID, 8 );
  121. }
  122. return gainsID;
  123. }