media/libopus/silk/encode_pulses.c

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /***********************************************************************
michael@0 2 Copyright (c) 2006-2011, Skype Limited. All rights reserved.
michael@0 3 Redistribution and use in source and binary forms, with or without
michael@0 4 modification, are permitted provided that the following conditions
michael@0 5 are met:
michael@0 6 - Redistributions of source code must retain the above copyright notice,
michael@0 7 this list of conditions and the following disclaimer.
michael@0 8 - Redistributions in binary form must reproduce the above copyright
michael@0 9 notice, this list of conditions and the following disclaimer in the
michael@0 10 documentation and/or other materials provided with the distribution.
michael@0 11 - Neither the name of Internet Society, IETF or IETF Trust, nor the
michael@0 12 names of specific contributors, may be used to endorse or promote
michael@0 13 products derived from this software without specific prior written
michael@0 14 permission.
michael@0 15 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
michael@0 16 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
michael@0 17 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
michael@0 18 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
michael@0 19 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
michael@0 20 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
michael@0 21 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
michael@0 22 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
michael@0 23 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
michael@0 24 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
michael@0 25 POSSIBILITY OF SUCH DAMAGE.
michael@0 26 ***********************************************************************/
michael@0 27
michael@0 28 #ifdef HAVE_CONFIG_H
michael@0 29 #include "config.h"
michael@0 30 #endif
michael@0 31
michael@0 32 #include "main.h"
michael@0 33 #include "stack_alloc.h"
michael@0 34
michael@0 35 /*********************************************/
michael@0 36 /* Encode quantization indices of excitation */
michael@0 37 /*********************************************/
michael@0 38
michael@0 39 static OPUS_INLINE opus_int combine_and_check( /* return ok */
michael@0 40 opus_int *pulses_comb, /* O */
michael@0 41 const opus_int *pulses_in, /* I */
michael@0 42 opus_int max_pulses, /* I max value for sum of pulses */
michael@0 43 opus_int len /* I number of output values */
michael@0 44 )
michael@0 45 {
michael@0 46 opus_int k, sum;
michael@0 47
michael@0 48 for( k = 0; k < len; k++ ) {
michael@0 49 sum = pulses_in[ 2 * k ] + pulses_in[ 2 * k + 1 ];
michael@0 50 if( sum > max_pulses ) {
michael@0 51 return 1;
michael@0 52 }
michael@0 53 pulses_comb[ k ] = sum;
michael@0 54 }
michael@0 55
michael@0 56 return 0;
michael@0 57 }
michael@0 58
michael@0 59 /* Encode quantization indices of excitation */
michael@0 60 void silk_encode_pulses(
michael@0 61 ec_enc *psRangeEnc, /* I/O compressor data structure */
michael@0 62 const opus_int signalType, /* I Signal type */
michael@0 63 const opus_int quantOffsetType, /* I quantOffsetType */
michael@0 64 opus_int8 pulses[], /* I quantization indices */
michael@0 65 const opus_int frame_length /* I Frame length */
michael@0 66 )
michael@0 67 {
michael@0 68 opus_int i, k, j, iter, bit, nLS, scale_down, RateLevelIndex = 0;
michael@0 69 opus_int32 abs_q, minSumBits_Q5, sumBits_Q5;
michael@0 70 VARDECL( opus_int, abs_pulses );
michael@0 71 VARDECL( opus_int, sum_pulses );
michael@0 72 VARDECL( opus_int, nRshifts );
michael@0 73 opus_int pulses_comb[ 8 ];
michael@0 74 opus_int *abs_pulses_ptr;
michael@0 75 const opus_int8 *pulses_ptr;
michael@0 76 const opus_uint8 *cdf_ptr;
michael@0 77 const opus_uint8 *nBits_ptr;
michael@0 78 SAVE_STACK;
michael@0 79
michael@0 80 silk_memset( pulses_comb, 0, 8 * sizeof( opus_int ) ); /* Fixing Valgrind reported problem*/
michael@0 81
michael@0 82 /****************************/
michael@0 83 /* Prepare for shell coding */
michael@0 84 /****************************/
michael@0 85 /* Calculate number of shell blocks */
michael@0 86 silk_assert( 1 << LOG2_SHELL_CODEC_FRAME_LENGTH == SHELL_CODEC_FRAME_LENGTH );
michael@0 87 iter = silk_RSHIFT( frame_length, LOG2_SHELL_CODEC_FRAME_LENGTH );
michael@0 88 if( iter * SHELL_CODEC_FRAME_LENGTH < frame_length ) {
michael@0 89 silk_assert( frame_length == 12 * 10 ); /* Make sure only happens for 10 ms @ 12 kHz */
michael@0 90 iter++;
michael@0 91 silk_memset( &pulses[ frame_length ], 0, SHELL_CODEC_FRAME_LENGTH * sizeof(opus_int8));
michael@0 92 }
michael@0 93
michael@0 94 /* Take the absolute value of the pulses */
michael@0 95 ALLOC( abs_pulses, iter * SHELL_CODEC_FRAME_LENGTH, opus_int );
michael@0 96 silk_assert( !( SHELL_CODEC_FRAME_LENGTH & 3 ) );
michael@0 97 for( i = 0; i < iter * SHELL_CODEC_FRAME_LENGTH; i+=4 ) {
michael@0 98 abs_pulses[i+0] = ( opus_int )silk_abs( pulses[ i + 0 ] );
michael@0 99 abs_pulses[i+1] = ( opus_int )silk_abs( pulses[ i + 1 ] );
michael@0 100 abs_pulses[i+2] = ( opus_int )silk_abs( pulses[ i + 2 ] );
michael@0 101 abs_pulses[i+3] = ( opus_int )silk_abs( pulses[ i + 3 ] );
michael@0 102 }
michael@0 103
michael@0 104 /* Calc sum pulses per shell code frame */
michael@0 105 ALLOC( sum_pulses, iter, opus_int );
michael@0 106 ALLOC( nRshifts, iter, opus_int );
michael@0 107 abs_pulses_ptr = abs_pulses;
michael@0 108 for( i = 0; i < iter; i++ ) {
michael@0 109 nRshifts[ i ] = 0;
michael@0 110
michael@0 111 while( 1 ) {
michael@0 112 /* 1+1 -> 2 */
michael@0 113 scale_down = combine_and_check( pulses_comb, abs_pulses_ptr, silk_max_pulses_table[ 0 ], 8 );
michael@0 114 /* 2+2 -> 4 */
michael@0 115 scale_down += combine_and_check( pulses_comb, pulses_comb, silk_max_pulses_table[ 1 ], 4 );
michael@0 116 /* 4+4 -> 8 */
michael@0 117 scale_down += combine_and_check( pulses_comb, pulses_comb, silk_max_pulses_table[ 2 ], 2 );
michael@0 118 /* 8+8 -> 16 */
michael@0 119 scale_down += combine_and_check( &sum_pulses[ i ], pulses_comb, silk_max_pulses_table[ 3 ], 1 );
michael@0 120
michael@0 121 if( scale_down ) {
michael@0 122 /* We need to downscale the quantization signal */
michael@0 123 nRshifts[ i ]++;
michael@0 124 for( k = 0; k < SHELL_CODEC_FRAME_LENGTH; k++ ) {
michael@0 125 abs_pulses_ptr[ k ] = silk_RSHIFT( abs_pulses_ptr[ k ], 1 );
michael@0 126 }
michael@0 127 } else {
michael@0 128 /* Jump out of while(1) loop and go to next shell coding frame */
michael@0 129 break;
michael@0 130 }
michael@0 131 }
michael@0 132 abs_pulses_ptr += SHELL_CODEC_FRAME_LENGTH;
michael@0 133 }
michael@0 134
michael@0 135 /**************/
michael@0 136 /* Rate level */
michael@0 137 /**************/
michael@0 138 /* find rate level that leads to fewest bits for coding of pulses per block info */
michael@0 139 minSumBits_Q5 = silk_int32_MAX;
michael@0 140 for( k = 0; k < N_RATE_LEVELS - 1; k++ ) {
michael@0 141 nBits_ptr = silk_pulses_per_block_BITS_Q5[ k ];
michael@0 142 sumBits_Q5 = silk_rate_levels_BITS_Q5[ signalType >> 1 ][ k ];
michael@0 143 for( i = 0; i < iter; i++ ) {
michael@0 144 if( nRshifts[ i ] > 0 ) {
michael@0 145 sumBits_Q5 += nBits_ptr[ MAX_PULSES + 1 ];
michael@0 146 } else {
michael@0 147 sumBits_Q5 += nBits_ptr[ sum_pulses[ i ] ];
michael@0 148 }
michael@0 149 }
michael@0 150 if( sumBits_Q5 < minSumBits_Q5 ) {
michael@0 151 minSumBits_Q5 = sumBits_Q5;
michael@0 152 RateLevelIndex = k;
michael@0 153 }
michael@0 154 }
michael@0 155 ec_enc_icdf( psRangeEnc, RateLevelIndex, silk_rate_levels_iCDF[ signalType >> 1 ], 8 );
michael@0 156
michael@0 157 /***************************************************/
michael@0 158 /* Sum-Weighted-Pulses Encoding */
michael@0 159 /***************************************************/
michael@0 160 cdf_ptr = silk_pulses_per_block_iCDF[ RateLevelIndex ];
michael@0 161 for( i = 0; i < iter; i++ ) {
michael@0 162 if( nRshifts[ i ] == 0 ) {
michael@0 163 ec_enc_icdf( psRangeEnc, sum_pulses[ i ], cdf_ptr, 8 );
michael@0 164 } else {
michael@0 165 ec_enc_icdf( psRangeEnc, MAX_PULSES + 1, cdf_ptr, 8 );
michael@0 166 for( k = 0; k < nRshifts[ i ] - 1; k++ ) {
michael@0 167 ec_enc_icdf( psRangeEnc, MAX_PULSES + 1, silk_pulses_per_block_iCDF[ N_RATE_LEVELS - 1 ], 8 );
michael@0 168 }
michael@0 169 ec_enc_icdf( psRangeEnc, sum_pulses[ i ], silk_pulses_per_block_iCDF[ N_RATE_LEVELS - 1 ], 8 );
michael@0 170 }
michael@0 171 }
michael@0 172
michael@0 173 /******************/
michael@0 174 /* Shell Encoding */
michael@0 175 /******************/
michael@0 176 for( i = 0; i < iter; i++ ) {
michael@0 177 if( sum_pulses[ i ] > 0 ) {
michael@0 178 silk_shell_encoder( psRangeEnc, &abs_pulses[ i * SHELL_CODEC_FRAME_LENGTH ] );
michael@0 179 }
michael@0 180 }
michael@0 181
michael@0 182 /****************/
michael@0 183 /* LSB Encoding */
michael@0 184 /****************/
michael@0 185 for( i = 0; i < iter; i++ ) {
michael@0 186 if( nRshifts[ i ] > 0 ) {
michael@0 187 pulses_ptr = &pulses[ i * SHELL_CODEC_FRAME_LENGTH ];
michael@0 188 nLS = nRshifts[ i ] - 1;
michael@0 189 for( k = 0; k < SHELL_CODEC_FRAME_LENGTH; k++ ) {
michael@0 190 abs_q = (opus_int8)silk_abs( pulses_ptr[ k ] );
michael@0 191 for( j = nLS; j > 0; j-- ) {
michael@0 192 bit = silk_RSHIFT( abs_q, j ) & 1;
michael@0 193 ec_enc_icdf( psRangeEnc, bit, silk_lsb_iCDF, 8 );
michael@0 194 }
michael@0 195 bit = abs_q & 1;
michael@0 196 ec_enc_icdf( psRangeEnc, bit, silk_lsb_iCDF, 8 );
michael@0 197 }
michael@0 198 }
michael@0 199 }
michael@0 200
michael@0 201 /****************/
michael@0 202 /* Encode signs */
michael@0 203 /****************/
michael@0 204 silk_encode_signs( psRangeEnc, pulses, frame_length, signalType, quantOffsetType, sum_pulses );
michael@0 205 RESTORE_STACK;
michael@0 206 }

mercurial