1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/media/libopus/silk/shell_coder.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,151 @@ 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 + 1.37 +/* shell coder; pulse-subframe length is hardcoded */ 1.38 + 1.39 +static OPUS_INLINE void combine_pulses( 1.40 + opus_int *out, /* O combined pulses vector [len] */ 1.41 + const opus_int *in, /* I input vector [2 * len] */ 1.42 + const opus_int len /* I number of OUTPUT samples */ 1.43 +) 1.44 +{ 1.45 + opus_int k; 1.46 + for( k = 0; k < len; k++ ) { 1.47 + out[ k ] = in[ 2 * k ] + in[ 2 * k + 1 ]; 1.48 + } 1.49 +} 1.50 + 1.51 +static OPUS_INLINE void encode_split( 1.52 + ec_enc *psRangeEnc, /* I/O compressor data structure */ 1.53 + const opus_int p_child1, /* I pulse amplitude of first child subframe */ 1.54 + const opus_int p, /* I pulse amplitude of current subframe */ 1.55 + const opus_uint8 *shell_table /* I table of shell cdfs */ 1.56 +) 1.57 +{ 1.58 + if( p > 0 ) { 1.59 + ec_enc_icdf( psRangeEnc, p_child1, &shell_table[ silk_shell_code_table_offsets[ p ] ], 8 ); 1.60 + } 1.61 +} 1.62 + 1.63 +static OPUS_INLINE void decode_split( 1.64 + opus_int *p_child1, /* O pulse amplitude of first child subframe */ 1.65 + opus_int *p_child2, /* O pulse amplitude of second child subframe */ 1.66 + ec_dec *psRangeDec, /* I/O Compressor data structure */ 1.67 + const opus_int p, /* I pulse amplitude of current subframe */ 1.68 + const opus_uint8 *shell_table /* I table of shell cdfs */ 1.69 +) 1.70 +{ 1.71 + if( p > 0 ) { 1.72 + p_child1[ 0 ] = ec_dec_icdf( psRangeDec, &shell_table[ silk_shell_code_table_offsets[ p ] ], 8 ); 1.73 + p_child2[ 0 ] = p - p_child1[ 0 ]; 1.74 + } else { 1.75 + p_child1[ 0 ] = 0; 1.76 + p_child2[ 0 ] = 0; 1.77 + } 1.78 +} 1.79 + 1.80 +/* Shell encoder, operates on one shell code frame of 16 pulses */ 1.81 +void silk_shell_encoder( 1.82 + ec_enc *psRangeEnc, /* I/O compressor data structure */ 1.83 + const opus_int *pulses0 /* I data: nonnegative pulse amplitudes */ 1.84 +) 1.85 +{ 1.86 + opus_int pulses1[ 8 ], pulses2[ 4 ], pulses3[ 2 ], pulses4[ 1 ]; 1.87 + 1.88 + /* this function operates on one shell code frame of 16 pulses */ 1.89 + silk_assert( SHELL_CODEC_FRAME_LENGTH == 16 ); 1.90 + 1.91 + /* tree representation per pulse-subframe */ 1.92 + combine_pulses( pulses1, pulses0, 8 ); 1.93 + combine_pulses( pulses2, pulses1, 4 ); 1.94 + combine_pulses( pulses3, pulses2, 2 ); 1.95 + combine_pulses( pulses4, pulses3, 1 ); 1.96 + 1.97 + encode_split( psRangeEnc, pulses3[ 0 ], pulses4[ 0 ], silk_shell_code_table3 ); 1.98 + 1.99 + encode_split( psRangeEnc, pulses2[ 0 ], pulses3[ 0 ], silk_shell_code_table2 ); 1.100 + 1.101 + encode_split( psRangeEnc, pulses1[ 0 ], pulses2[ 0 ], silk_shell_code_table1 ); 1.102 + encode_split( psRangeEnc, pulses0[ 0 ], pulses1[ 0 ], silk_shell_code_table0 ); 1.103 + encode_split( psRangeEnc, pulses0[ 2 ], pulses1[ 1 ], silk_shell_code_table0 ); 1.104 + 1.105 + encode_split( psRangeEnc, pulses1[ 2 ], pulses2[ 1 ], silk_shell_code_table1 ); 1.106 + encode_split( psRangeEnc, pulses0[ 4 ], pulses1[ 2 ], silk_shell_code_table0 ); 1.107 + encode_split( psRangeEnc, pulses0[ 6 ], pulses1[ 3 ], silk_shell_code_table0 ); 1.108 + 1.109 + encode_split( psRangeEnc, pulses2[ 2 ], pulses3[ 1 ], silk_shell_code_table2 ); 1.110 + 1.111 + encode_split( psRangeEnc, pulses1[ 4 ], pulses2[ 2 ], silk_shell_code_table1 ); 1.112 + encode_split( psRangeEnc, pulses0[ 8 ], pulses1[ 4 ], silk_shell_code_table0 ); 1.113 + encode_split( psRangeEnc, pulses0[ 10 ], pulses1[ 5 ], silk_shell_code_table0 ); 1.114 + 1.115 + encode_split( psRangeEnc, pulses1[ 6 ], pulses2[ 3 ], silk_shell_code_table1 ); 1.116 + encode_split( psRangeEnc, pulses0[ 12 ], pulses1[ 6 ], silk_shell_code_table0 ); 1.117 + encode_split( psRangeEnc, pulses0[ 14 ], pulses1[ 7 ], silk_shell_code_table0 ); 1.118 +} 1.119 + 1.120 + 1.121 +/* Shell decoder, operates on one shell code frame of 16 pulses */ 1.122 +void silk_shell_decoder( 1.123 + opus_int *pulses0, /* O data: nonnegative pulse amplitudes */ 1.124 + ec_dec *psRangeDec, /* I/O Compressor data structure */ 1.125 + const opus_int pulses4 /* I number of pulses per pulse-subframe */ 1.126 +) 1.127 +{ 1.128 + opus_int pulses3[ 2 ], pulses2[ 4 ], pulses1[ 8 ]; 1.129 + 1.130 + /* this function operates on one shell code frame of 16 pulses */ 1.131 + silk_assert( SHELL_CODEC_FRAME_LENGTH == 16 ); 1.132 + 1.133 + decode_split( &pulses3[ 0 ], &pulses3[ 1 ], psRangeDec, pulses4, silk_shell_code_table3 ); 1.134 + 1.135 + decode_split( &pulses2[ 0 ], &pulses2[ 1 ], psRangeDec, pulses3[ 0 ], silk_shell_code_table2 ); 1.136 + 1.137 + decode_split( &pulses1[ 0 ], &pulses1[ 1 ], psRangeDec, pulses2[ 0 ], silk_shell_code_table1 ); 1.138 + decode_split( &pulses0[ 0 ], &pulses0[ 1 ], psRangeDec, pulses1[ 0 ], silk_shell_code_table0 ); 1.139 + decode_split( &pulses0[ 2 ], &pulses0[ 3 ], psRangeDec, pulses1[ 1 ], silk_shell_code_table0 ); 1.140 + 1.141 + decode_split( &pulses1[ 2 ], &pulses1[ 3 ], psRangeDec, pulses2[ 1 ], silk_shell_code_table1 ); 1.142 + decode_split( &pulses0[ 4 ], &pulses0[ 5 ], psRangeDec, pulses1[ 2 ], silk_shell_code_table0 ); 1.143 + decode_split( &pulses0[ 6 ], &pulses0[ 7 ], psRangeDec, pulses1[ 3 ], silk_shell_code_table0 ); 1.144 + 1.145 + decode_split( &pulses2[ 2 ], &pulses2[ 3 ], psRangeDec, pulses3[ 1 ], silk_shell_code_table2 ); 1.146 + 1.147 + decode_split( &pulses1[ 4 ], &pulses1[ 5 ], psRangeDec, pulses2[ 2 ], silk_shell_code_table1 ); 1.148 + decode_split( &pulses0[ 8 ], &pulses0[ 9 ], psRangeDec, pulses1[ 4 ], silk_shell_code_table0 ); 1.149 + decode_split( &pulses0[ 10 ], &pulses0[ 11 ], psRangeDec, pulses1[ 5 ], silk_shell_code_table0 ); 1.150 + 1.151 + decode_split( &pulses1[ 6 ], &pulses1[ 7 ], psRangeDec, pulses2[ 3 ], silk_shell_code_table1 ); 1.152 + decode_split( &pulses0[ 12 ], &pulses0[ 13 ], psRangeDec, pulses1[ 6 ], silk_shell_code_table0 ); 1.153 + decode_split( &pulses0[ 14 ], &pulses0[ 15 ], psRangeDec, pulses1[ 7 ], silk_shell_code_table0 ); 1.154 +}