1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/media/libopus/silk/encode_pulses.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,206 @@ 1.4 +/*********************************************************************** 1.5 +Copyright (c) 2006-2011, Skype Limited. All rights reserved. 1.6 +Redistribution and use in source and binary forms, with or without 1.7 +modification, are permitted provided that the following conditions 1.8 +are met: 1.9 +- Redistributions of source code must retain the above copyright notice, 1.10 +this list of conditions and the following disclaimer. 1.11 +- Redistributions in binary form must reproduce the above copyright 1.12 +notice, this list of conditions and the following disclaimer in the 1.13 +documentation and/or other materials provided with the distribution. 1.14 +- Neither the name of Internet Society, IETF or IETF Trust, nor the 1.15 +names of specific contributors, may be used to endorse or promote 1.16 +products derived from this software without specific prior written 1.17 +permission. 1.18 +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 1.19 +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1.20 +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1.21 +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 1.22 +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 1.23 +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 1.24 +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 1.25 +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 1.26 +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 1.27 +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 1.28 +POSSIBILITY OF SUCH DAMAGE. 1.29 +***********************************************************************/ 1.30 + 1.31 +#ifdef HAVE_CONFIG_H 1.32 +#include "config.h" 1.33 +#endif 1.34 + 1.35 +#include "main.h" 1.36 +#include "stack_alloc.h" 1.37 + 1.38 +/*********************************************/ 1.39 +/* Encode quantization indices of excitation */ 1.40 +/*********************************************/ 1.41 + 1.42 +static OPUS_INLINE opus_int combine_and_check( /* return ok */ 1.43 + opus_int *pulses_comb, /* O */ 1.44 + const opus_int *pulses_in, /* I */ 1.45 + opus_int max_pulses, /* I max value for sum of pulses */ 1.46 + opus_int len /* I number of output values */ 1.47 +) 1.48 +{ 1.49 + opus_int k, sum; 1.50 + 1.51 + for( k = 0; k < len; k++ ) { 1.52 + sum = pulses_in[ 2 * k ] + pulses_in[ 2 * k + 1 ]; 1.53 + if( sum > max_pulses ) { 1.54 + return 1; 1.55 + } 1.56 + pulses_comb[ k ] = sum; 1.57 + } 1.58 + 1.59 + return 0; 1.60 +} 1.61 + 1.62 +/* Encode quantization indices of excitation */ 1.63 +void silk_encode_pulses( 1.64 + ec_enc *psRangeEnc, /* I/O compressor data structure */ 1.65 + const opus_int signalType, /* I Signal type */ 1.66 + const opus_int quantOffsetType, /* I quantOffsetType */ 1.67 + opus_int8 pulses[], /* I quantization indices */ 1.68 + const opus_int frame_length /* I Frame length */ 1.69 +) 1.70 +{ 1.71 + opus_int i, k, j, iter, bit, nLS, scale_down, RateLevelIndex = 0; 1.72 + opus_int32 abs_q, minSumBits_Q5, sumBits_Q5; 1.73 + VARDECL( opus_int, abs_pulses ); 1.74 + VARDECL( opus_int, sum_pulses ); 1.75 + VARDECL( opus_int, nRshifts ); 1.76 + opus_int pulses_comb[ 8 ]; 1.77 + opus_int *abs_pulses_ptr; 1.78 + const opus_int8 *pulses_ptr; 1.79 + const opus_uint8 *cdf_ptr; 1.80 + const opus_uint8 *nBits_ptr; 1.81 + SAVE_STACK; 1.82 + 1.83 + silk_memset( pulses_comb, 0, 8 * sizeof( opus_int ) ); /* Fixing Valgrind reported problem*/ 1.84 + 1.85 + /****************************/ 1.86 + /* Prepare for shell coding */ 1.87 + /****************************/ 1.88 + /* Calculate number of shell blocks */ 1.89 + silk_assert( 1 << LOG2_SHELL_CODEC_FRAME_LENGTH == SHELL_CODEC_FRAME_LENGTH ); 1.90 + iter = silk_RSHIFT( frame_length, LOG2_SHELL_CODEC_FRAME_LENGTH ); 1.91 + if( iter * SHELL_CODEC_FRAME_LENGTH < frame_length ) { 1.92 + silk_assert( frame_length == 12 * 10 ); /* Make sure only happens for 10 ms @ 12 kHz */ 1.93 + iter++; 1.94 + silk_memset( &pulses[ frame_length ], 0, SHELL_CODEC_FRAME_LENGTH * sizeof(opus_int8)); 1.95 + } 1.96 + 1.97 + /* Take the absolute value of the pulses */ 1.98 + ALLOC( abs_pulses, iter * SHELL_CODEC_FRAME_LENGTH, opus_int ); 1.99 + silk_assert( !( SHELL_CODEC_FRAME_LENGTH & 3 ) ); 1.100 + for( i = 0; i < iter * SHELL_CODEC_FRAME_LENGTH; i+=4 ) { 1.101 + abs_pulses[i+0] = ( opus_int )silk_abs( pulses[ i + 0 ] ); 1.102 + abs_pulses[i+1] = ( opus_int )silk_abs( pulses[ i + 1 ] ); 1.103 + abs_pulses[i+2] = ( opus_int )silk_abs( pulses[ i + 2 ] ); 1.104 + abs_pulses[i+3] = ( opus_int )silk_abs( pulses[ i + 3 ] ); 1.105 + } 1.106 + 1.107 + /* Calc sum pulses per shell code frame */ 1.108 + ALLOC( sum_pulses, iter, opus_int ); 1.109 + ALLOC( nRshifts, iter, opus_int ); 1.110 + abs_pulses_ptr = abs_pulses; 1.111 + for( i = 0; i < iter; i++ ) { 1.112 + nRshifts[ i ] = 0; 1.113 + 1.114 + while( 1 ) { 1.115 + /* 1+1 -> 2 */ 1.116 + scale_down = combine_and_check( pulses_comb, abs_pulses_ptr, silk_max_pulses_table[ 0 ], 8 ); 1.117 + /* 2+2 -> 4 */ 1.118 + scale_down += combine_and_check( pulses_comb, pulses_comb, silk_max_pulses_table[ 1 ], 4 ); 1.119 + /* 4+4 -> 8 */ 1.120 + scale_down += combine_and_check( pulses_comb, pulses_comb, silk_max_pulses_table[ 2 ], 2 ); 1.121 + /* 8+8 -> 16 */ 1.122 + scale_down += combine_and_check( &sum_pulses[ i ], pulses_comb, silk_max_pulses_table[ 3 ], 1 ); 1.123 + 1.124 + if( scale_down ) { 1.125 + /* We need to downscale the quantization signal */ 1.126 + nRshifts[ i ]++; 1.127 + for( k = 0; k < SHELL_CODEC_FRAME_LENGTH; k++ ) { 1.128 + abs_pulses_ptr[ k ] = silk_RSHIFT( abs_pulses_ptr[ k ], 1 ); 1.129 + } 1.130 + } else { 1.131 + /* Jump out of while(1) loop and go to next shell coding frame */ 1.132 + break; 1.133 + } 1.134 + } 1.135 + abs_pulses_ptr += SHELL_CODEC_FRAME_LENGTH; 1.136 + } 1.137 + 1.138 + /**************/ 1.139 + /* Rate level */ 1.140 + /**************/ 1.141 + /* find rate level that leads to fewest bits for coding of pulses per block info */ 1.142 + minSumBits_Q5 = silk_int32_MAX; 1.143 + for( k = 0; k < N_RATE_LEVELS - 1; k++ ) { 1.144 + nBits_ptr = silk_pulses_per_block_BITS_Q5[ k ]; 1.145 + sumBits_Q5 = silk_rate_levels_BITS_Q5[ signalType >> 1 ][ k ]; 1.146 + for( i = 0; i < iter; i++ ) { 1.147 + if( nRshifts[ i ] > 0 ) { 1.148 + sumBits_Q5 += nBits_ptr[ MAX_PULSES + 1 ]; 1.149 + } else { 1.150 + sumBits_Q5 += nBits_ptr[ sum_pulses[ i ] ]; 1.151 + } 1.152 + } 1.153 + if( sumBits_Q5 < minSumBits_Q5 ) { 1.154 + minSumBits_Q5 = sumBits_Q5; 1.155 + RateLevelIndex = k; 1.156 + } 1.157 + } 1.158 + ec_enc_icdf( psRangeEnc, RateLevelIndex, silk_rate_levels_iCDF[ signalType >> 1 ], 8 ); 1.159 + 1.160 + /***************************************************/ 1.161 + /* Sum-Weighted-Pulses Encoding */ 1.162 + /***************************************************/ 1.163 + cdf_ptr = silk_pulses_per_block_iCDF[ RateLevelIndex ]; 1.164 + for( i = 0; i < iter; i++ ) { 1.165 + if( nRshifts[ i ] == 0 ) { 1.166 + ec_enc_icdf( psRangeEnc, sum_pulses[ i ], cdf_ptr, 8 ); 1.167 + } else { 1.168 + ec_enc_icdf( psRangeEnc, MAX_PULSES + 1, cdf_ptr, 8 ); 1.169 + for( k = 0; k < nRshifts[ i ] - 1; k++ ) { 1.170 + ec_enc_icdf( psRangeEnc, MAX_PULSES + 1, silk_pulses_per_block_iCDF[ N_RATE_LEVELS - 1 ], 8 ); 1.171 + } 1.172 + ec_enc_icdf( psRangeEnc, sum_pulses[ i ], silk_pulses_per_block_iCDF[ N_RATE_LEVELS - 1 ], 8 ); 1.173 + } 1.174 + } 1.175 + 1.176 + /******************/ 1.177 + /* Shell Encoding */ 1.178 + /******************/ 1.179 + for( i = 0; i < iter; i++ ) { 1.180 + if( sum_pulses[ i ] > 0 ) { 1.181 + silk_shell_encoder( psRangeEnc, &abs_pulses[ i * SHELL_CODEC_FRAME_LENGTH ] ); 1.182 + } 1.183 + } 1.184 + 1.185 + /****************/ 1.186 + /* LSB Encoding */ 1.187 + /****************/ 1.188 + for( i = 0; i < iter; i++ ) { 1.189 + if( nRshifts[ i ] > 0 ) { 1.190 + pulses_ptr = &pulses[ i * SHELL_CODEC_FRAME_LENGTH ]; 1.191 + nLS = nRshifts[ i ] - 1; 1.192 + for( k = 0; k < SHELL_CODEC_FRAME_LENGTH; k++ ) { 1.193 + abs_q = (opus_int8)silk_abs( pulses_ptr[ k ] ); 1.194 + for( j = nLS; j > 0; j-- ) { 1.195 + bit = silk_RSHIFT( abs_q, j ) & 1; 1.196 + ec_enc_icdf( psRangeEnc, bit, silk_lsb_iCDF, 8 ); 1.197 + } 1.198 + bit = abs_q & 1; 1.199 + ec_enc_icdf( psRangeEnc, bit, silk_lsb_iCDF, 8 ); 1.200 + } 1.201 + } 1.202 + } 1.203 + 1.204 + /****************/ 1.205 + /* Encode signs */ 1.206 + /****************/ 1.207 + silk_encode_signs( psRangeEnc, pulses, frame_length, signalType, quantOffsetType, sum_pulses ); 1.208 + RESTORE_STACK; 1.209 +}