encode_pulses.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. #include "stack_alloc.h"
  32. /*********************************************/
  33. /* Encode quantization indices of excitation */
  34. /*********************************************/
  35. static OPUS_INLINE opus_int combine_and_check( /* return ok */
  36. opus_int *pulses_comb, /* O */
  37. const opus_int *pulses_in, /* I */
  38. opus_int max_pulses, /* I max value for sum of pulses */
  39. opus_int len /* I number of output values */
  40. )
  41. {
  42. opus_int k, sum;
  43. for( k = 0; k < len; k++ ) {
  44. sum = pulses_in[ 2 * k ] + pulses_in[ 2 * k + 1 ];
  45. if( sum > max_pulses ) {
  46. return 1;
  47. }
  48. pulses_comb[ k ] = sum;
  49. }
  50. return 0;
  51. }
  52. /* Encode quantization indices of excitation */
  53. void silk_encode_pulses(
  54. ec_enc *psRangeEnc, /* I/O compressor data structure */
  55. const opus_int signalType, /* I Signal type */
  56. const opus_int quantOffsetType, /* I quantOffsetType */
  57. opus_int8 pulses[], /* I quantization indices */
  58. const opus_int frame_length /* I Frame length */
  59. )
  60. {
  61. opus_int i, k, j, iter, bit, nLS, scale_down, RateLevelIndex = 0;
  62. opus_int32 abs_q, minSumBits_Q5, sumBits_Q5;
  63. VARDECL( opus_int, abs_pulses );
  64. VARDECL( opus_int, sum_pulses );
  65. VARDECL( opus_int, nRshifts );
  66. opus_int pulses_comb[ 8 ];
  67. opus_int *abs_pulses_ptr;
  68. const opus_int8 *pulses_ptr;
  69. const opus_uint8 *cdf_ptr;
  70. const opus_uint8 *nBits_ptr;
  71. SAVE_STACK;
  72. silk_memset( pulses_comb, 0, 8 * sizeof( opus_int ) ); /* Fixing Valgrind reported problem*/
  73. /****************************/
  74. /* Prepare for shell coding */
  75. /****************************/
  76. /* Calculate number of shell blocks */
  77. silk_assert( 1 << LOG2_SHELL_CODEC_FRAME_LENGTH == SHELL_CODEC_FRAME_LENGTH );
  78. iter = silk_RSHIFT( frame_length, LOG2_SHELL_CODEC_FRAME_LENGTH );
  79. if( iter * SHELL_CODEC_FRAME_LENGTH < frame_length ) {
  80. silk_assert( frame_length == 12 * 10 ); /* Make sure only happens for 10 ms @ 12 kHz */
  81. iter++;
  82. silk_memset( &pulses[ frame_length ], 0, SHELL_CODEC_FRAME_LENGTH * sizeof(opus_int8));
  83. }
  84. /* Take the absolute value of the pulses */
  85. ALLOC( abs_pulses, iter * SHELL_CODEC_FRAME_LENGTH, opus_int );
  86. silk_assert( !( SHELL_CODEC_FRAME_LENGTH & 3 ) );
  87. for( i = 0; i < iter * SHELL_CODEC_FRAME_LENGTH; i+=4 ) {
  88. abs_pulses[i+0] = ( opus_int )silk_abs( pulses[ i + 0 ] );
  89. abs_pulses[i+1] = ( opus_int )silk_abs( pulses[ i + 1 ] );
  90. abs_pulses[i+2] = ( opus_int )silk_abs( pulses[ i + 2 ] );
  91. abs_pulses[i+3] = ( opus_int )silk_abs( pulses[ i + 3 ] );
  92. }
  93. /* Calc sum pulses per shell code frame */
  94. ALLOC( sum_pulses, iter, opus_int );
  95. ALLOC( nRshifts, iter, opus_int );
  96. abs_pulses_ptr = abs_pulses;
  97. for( i = 0; i < iter; i++ ) {
  98. nRshifts[ i ] = 0;
  99. while( 1 ) {
  100. /* 1+1 -> 2 */
  101. scale_down = combine_and_check( pulses_comb, abs_pulses_ptr, silk_max_pulses_table[ 0 ], 8 );
  102. /* 2+2 -> 4 */
  103. scale_down += combine_and_check( pulses_comb, pulses_comb, silk_max_pulses_table[ 1 ], 4 );
  104. /* 4+4 -> 8 */
  105. scale_down += combine_and_check( pulses_comb, pulses_comb, silk_max_pulses_table[ 2 ], 2 );
  106. /* 8+8 -> 16 */
  107. scale_down += combine_and_check( &sum_pulses[ i ], pulses_comb, silk_max_pulses_table[ 3 ], 1 );
  108. if( scale_down ) {
  109. /* We need to downscale the quantization signal */
  110. nRshifts[ i ]++;
  111. for( k = 0; k < SHELL_CODEC_FRAME_LENGTH; k++ ) {
  112. abs_pulses_ptr[ k ] = silk_RSHIFT( abs_pulses_ptr[ k ], 1 );
  113. }
  114. } else {
  115. /* Jump out of while(1) loop and go to next shell coding frame */
  116. break;
  117. }
  118. }
  119. abs_pulses_ptr += SHELL_CODEC_FRAME_LENGTH;
  120. }
  121. /**************/
  122. /* Rate level */
  123. /**************/
  124. /* find rate level that leads to fewest bits for coding of pulses per block info */
  125. minSumBits_Q5 = silk_int32_MAX;
  126. for( k = 0; k < N_RATE_LEVELS - 1; k++ ) {
  127. nBits_ptr = silk_pulses_per_block_BITS_Q5[ k ];
  128. sumBits_Q5 = silk_rate_levels_BITS_Q5[ signalType >> 1 ][ k ];
  129. for( i = 0; i < iter; i++ ) {
  130. if( nRshifts[ i ] > 0 ) {
  131. sumBits_Q5 += nBits_ptr[ SILK_MAX_PULSES + 1 ];
  132. } else {
  133. sumBits_Q5 += nBits_ptr[ sum_pulses[ i ] ];
  134. }
  135. }
  136. if( sumBits_Q5 < minSumBits_Q5 ) {
  137. minSumBits_Q5 = sumBits_Q5;
  138. RateLevelIndex = k;
  139. }
  140. }
  141. ec_enc_icdf( psRangeEnc, RateLevelIndex, silk_rate_levels_iCDF[ signalType >> 1 ], 8 );
  142. /***************************************************/
  143. /* Sum-Weighted-Pulses Encoding */
  144. /***************************************************/
  145. cdf_ptr = silk_pulses_per_block_iCDF[ RateLevelIndex ];
  146. for( i = 0; i < iter; i++ ) {
  147. if( nRshifts[ i ] == 0 ) {
  148. ec_enc_icdf( psRangeEnc, sum_pulses[ i ], cdf_ptr, 8 );
  149. } else {
  150. ec_enc_icdf( psRangeEnc, SILK_MAX_PULSES + 1, cdf_ptr, 8 );
  151. for( k = 0; k < nRshifts[ i ] - 1; k++ ) {
  152. ec_enc_icdf( psRangeEnc, SILK_MAX_PULSES + 1, silk_pulses_per_block_iCDF[ N_RATE_LEVELS - 1 ], 8 );
  153. }
  154. ec_enc_icdf( psRangeEnc, sum_pulses[ i ], silk_pulses_per_block_iCDF[ N_RATE_LEVELS - 1 ], 8 );
  155. }
  156. }
  157. /******************/
  158. /* Shell Encoding */
  159. /******************/
  160. for( i = 0; i < iter; i++ ) {
  161. if( sum_pulses[ i ] > 0 ) {
  162. silk_shell_encoder( psRangeEnc, &abs_pulses[ i * SHELL_CODEC_FRAME_LENGTH ] );
  163. }
  164. }
  165. /****************/
  166. /* LSB Encoding */
  167. /****************/
  168. for( i = 0; i < iter; i++ ) {
  169. if( nRshifts[ i ] > 0 ) {
  170. pulses_ptr = &pulses[ i * SHELL_CODEC_FRAME_LENGTH ];
  171. nLS = nRshifts[ i ] - 1;
  172. for( k = 0; k < SHELL_CODEC_FRAME_LENGTH; k++ ) {
  173. abs_q = (opus_int8)silk_abs( pulses_ptr[ k ] );
  174. for( j = nLS; j > 0; j-- ) {
  175. bit = silk_RSHIFT( abs_q, j ) & 1;
  176. ec_enc_icdf( psRangeEnc, bit, silk_lsb_iCDF, 8 );
  177. }
  178. bit = abs_q & 1;
  179. ec_enc_icdf( psRangeEnc, bit, silk_lsb_iCDF, 8 );
  180. }
  181. }
  182. }
  183. /****************/
  184. /* Encode signs */
  185. /****************/
  186. silk_encode_signs( psRangeEnc, pulses, frame_length, signalType, quantOffsetType, sum_pulses );
  187. RESTORE_STACK;
  188. }