Thu, 15 Jan 2015 15:59:08 +0100
Implement a real Private Browsing Mode condition by changing the API/ABI;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.
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_FLP.h" |
michael@0 | 33 | #include "tuning_parameters.h" |
michael@0 | 34 | |
michael@0 | 35 | /* Low Bitrate Redundancy (LBRR) encoding. Reuse all parameters but encode with lower bitrate */ |
michael@0 | 36 | static OPUS_INLINE void silk_LBRR_encode_FLP( |
michael@0 | 37 | silk_encoder_state_FLP *psEnc, /* I/O Encoder state FLP */ |
michael@0 | 38 | silk_encoder_control_FLP *psEncCtrl, /* I/O Encoder control FLP */ |
michael@0 | 39 | const silk_float xfw[], /* I Input signal */ |
michael@0 | 40 | opus_int condCoding /* I The type of conditional coding used so far for this frame */ |
michael@0 | 41 | ); |
michael@0 | 42 | |
michael@0 | 43 | void silk_encode_do_VAD_FLP( |
michael@0 | 44 | silk_encoder_state_FLP *psEnc /* I/O Encoder state FLP */ |
michael@0 | 45 | ) |
michael@0 | 46 | { |
michael@0 | 47 | /****************************/ |
michael@0 | 48 | /* Voice Activity Detection */ |
michael@0 | 49 | /****************************/ |
michael@0 | 50 | silk_VAD_GetSA_Q8( &psEnc->sCmn, psEnc->sCmn.inputBuf + 1 ); |
michael@0 | 51 | |
michael@0 | 52 | /**************************************************/ |
michael@0 | 53 | /* Convert speech activity into VAD and DTX flags */ |
michael@0 | 54 | /**************************************************/ |
michael@0 | 55 | if( psEnc->sCmn.speech_activity_Q8 < SILK_FIX_CONST( SPEECH_ACTIVITY_DTX_THRES, 8 ) ) { |
michael@0 | 56 | psEnc->sCmn.indices.signalType = TYPE_NO_VOICE_ACTIVITY; |
michael@0 | 57 | psEnc->sCmn.noSpeechCounter++; |
michael@0 | 58 | if( psEnc->sCmn.noSpeechCounter < NB_SPEECH_FRAMES_BEFORE_DTX ) { |
michael@0 | 59 | psEnc->sCmn.inDTX = 0; |
michael@0 | 60 | } else if( psEnc->sCmn.noSpeechCounter > MAX_CONSECUTIVE_DTX + NB_SPEECH_FRAMES_BEFORE_DTX ) { |
michael@0 | 61 | psEnc->sCmn.noSpeechCounter = NB_SPEECH_FRAMES_BEFORE_DTX; |
michael@0 | 62 | psEnc->sCmn.inDTX = 0; |
michael@0 | 63 | } |
michael@0 | 64 | psEnc->sCmn.VAD_flags[ psEnc->sCmn.nFramesEncoded ] = 0; |
michael@0 | 65 | } else { |
michael@0 | 66 | psEnc->sCmn.noSpeechCounter = 0; |
michael@0 | 67 | psEnc->sCmn.inDTX = 0; |
michael@0 | 68 | psEnc->sCmn.indices.signalType = TYPE_UNVOICED; |
michael@0 | 69 | psEnc->sCmn.VAD_flags[ psEnc->sCmn.nFramesEncoded ] = 1; |
michael@0 | 70 | } |
michael@0 | 71 | } |
michael@0 | 72 | |
michael@0 | 73 | /****************/ |
michael@0 | 74 | /* Encode frame */ |
michael@0 | 75 | /****************/ |
michael@0 | 76 | opus_int silk_encode_frame_FLP( |
michael@0 | 77 | silk_encoder_state_FLP *psEnc, /* I/O Encoder state FLP */ |
michael@0 | 78 | opus_int32 *pnBytesOut, /* O Number of payload bytes; */ |
michael@0 | 79 | ec_enc *psRangeEnc, /* I/O compressor data structure */ |
michael@0 | 80 | opus_int condCoding, /* I The type of conditional coding to use */ |
michael@0 | 81 | opus_int maxBits, /* I If > 0: maximum number of output bits */ |
michael@0 | 82 | opus_int useCBR /* I Flag to force constant-bitrate operation */ |
michael@0 | 83 | ) |
michael@0 | 84 | { |
michael@0 | 85 | silk_encoder_control_FLP sEncCtrl; |
michael@0 | 86 | opus_int i, iter, maxIter, found_upper, found_lower, ret = 0; |
michael@0 | 87 | silk_float *x_frame, *res_pitch_frame; |
michael@0 | 88 | silk_float xfw[ MAX_FRAME_LENGTH ]; |
michael@0 | 89 | silk_float res_pitch[ 2 * MAX_FRAME_LENGTH + LA_PITCH_MAX ]; |
michael@0 | 90 | ec_enc sRangeEnc_copy, sRangeEnc_copy2; |
michael@0 | 91 | silk_nsq_state sNSQ_copy, sNSQ_copy2; |
michael@0 | 92 | opus_int32 seed_copy, nBits, nBits_lower, nBits_upper, gainMult_lower, gainMult_upper; |
michael@0 | 93 | opus_int32 gainsID, gainsID_lower, gainsID_upper; |
michael@0 | 94 | opus_int16 gainMult_Q8; |
michael@0 | 95 | opus_int16 ec_prevLagIndex_copy; |
michael@0 | 96 | opus_int ec_prevSignalType_copy; |
michael@0 | 97 | opus_int8 LastGainIndex_copy2; |
michael@0 | 98 | opus_int32 pGains_Q16[ MAX_NB_SUBFR ]; |
michael@0 | 99 | opus_uint8 ec_buf_copy[ 1275 ]; |
michael@0 | 100 | |
michael@0 | 101 | /* This is totally unnecessary but many compilers (including gcc) are too dumb to realise it */ |
michael@0 | 102 | LastGainIndex_copy2 = nBits_lower = nBits_upper = gainMult_lower = gainMult_upper = 0; |
michael@0 | 103 | |
michael@0 | 104 | psEnc->sCmn.indices.Seed = psEnc->sCmn.frameCounter++ & 3; |
michael@0 | 105 | |
michael@0 | 106 | /**************************************************************/ |
michael@0 | 107 | /* Set up Input Pointers, and insert frame in input buffer */ |
michael@0 | 108 | /**************************************************************/ |
michael@0 | 109 | /* pointers aligned with start of frame to encode */ |
michael@0 | 110 | x_frame = psEnc->x_buf + psEnc->sCmn.ltp_mem_length; /* start of frame to encode */ |
michael@0 | 111 | res_pitch_frame = res_pitch + psEnc->sCmn.ltp_mem_length; /* start of pitch LPC residual frame */ |
michael@0 | 112 | |
michael@0 | 113 | /***************************************/ |
michael@0 | 114 | /* Ensure smooth bandwidth transitions */ |
michael@0 | 115 | /***************************************/ |
michael@0 | 116 | silk_LP_variable_cutoff( &psEnc->sCmn.sLP, psEnc->sCmn.inputBuf + 1, psEnc->sCmn.frame_length ); |
michael@0 | 117 | |
michael@0 | 118 | /*******************************************/ |
michael@0 | 119 | /* Copy new frame to front of input buffer */ |
michael@0 | 120 | /*******************************************/ |
michael@0 | 121 | silk_short2float_array( x_frame + LA_SHAPE_MS * psEnc->sCmn.fs_kHz, psEnc->sCmn.inputBuf + 1, psEnc->sCmn.frame_length ); |
michael@0 | 122 | |
michael@0 | 123 | /* Add tiny signal to avoid high CPU load from denormalized floating point numbers */ |
michael@0 | 124 | for( i = 0; i < 8; i++ ) { |
michael@0 | 125 | x_frame[ LA_SHAPE_MS * psEnc->sCmn.fs_kHz + i * ( psEnc->sCmn.frame_length >> 3 ) ] += ( 1 - ( i & 2 ) ) * 1e-6f; |
michael@0 | 126 | } |
michael@0 | 127 | |
michael@0 | 128 | if( !psEnc->sCmn.prefillFlag ) { |
michael@0 | 129 | /*****************************************/ |
michael@0 | 130 | /* Find pitch lags, initial LPC analysis */ |
michael@0 | 131 | /*****************************************/ |
michael@0 | 132 | silk_find_pitch_lags_FLP( psEnc, &sEncCtrl, res_pitch, x_frame, psEnc->sCmn.arch ); |
michael@0 | 133 | |
michael@0 | 134 | /************************/ |
michael@0 | 135 | /* Noise shape analysis */ |
michael@0 | 136 | /************************/ |
michael@0 | 137 | silk_noise_shape_analysis_FLP( psEnc, &sEncCtrl, res_pitch_frame, x_frame ); |
michael@0 | 138 | |
michael@0 | 139 | /***************************************************/ |
michael@0 | 140 | /* Find linear prediction coefficients (LPC + LTP) */ |
michael@0 | 141 | /***************************************************/ |
michael@0 | 142 | silk_find_pred_coefs_FLP( psEnc, &sEncCtrl, res_pitch, x_frame, condCoding ); |
michael@0 | 143 | |
michael@0 | 144 | /****************************************/ |
michael@0 | 145 | /* Process gains */ |
michael@0 | 146 | /****************************************/ |
michael@0 | 147 | silk_process_gains_FLP( psEnc, &sEncCtrl, condCoding ); |
michael@0 | 148 | |
michael@0 | 149 | /*****************************************/ |
michael@0 | 150 | /* Prefiltering for noise shaper */ |
michael@0 | 151 | /*****************************************/ |
michael@0 | 152 | silk_prefilter_FLP( psEnc, &sEncCtrl, xfw, x_frame ); |
michael@0 | 153 | |
michael@0 | 154 | /****************************************/ |
michael@0 | 155 | /* Low Bitrate Redundant Encoding */ |
michael@0 | 156 | /****************************************/ |
michael@0 | 157 | silk_LBRR_encode_FLP( psEnc, &sEncCtrl, xfw, condCoding ); |
michael@0 | 158 | |
michael@0 | 159 | /* Loop over quantizer and entroy coding to control bitrate */ |
michael@0 | 160 | maxIter = 6; |
michael@0 | 161 | gainMult_Q8 = SILK_FIX_CONST( 1, 8 ); |
michael@0 | 162 | found_lower = 0; |
michael@0 | 163 | found_upper = 0; |
michael@0 | 164 | gainsID = silk_gains_ID( psEnc->sCmn.indices.GainsIndices, psEnc->sCmn.nb_subfr ); |
michael@0 | 165 | gainsID_lower = -1; |
michael@0 | 166 | gainsID_upper = -1; |
michael@0 | 167 | /* Copy part of the input state */ |
michael@0 | 168 | silk_memcpy( &sRangeEnc_copy, psRangeEnc, sizeof( ec_enc ) ); |
michael@0 | 169 | silk_memcpy( &sNSQ_copy, &psEnc->sCmn.sNSQ, sizeof( silk_nsq_state ) ); |
michael@0 | 170 | seed_copy = psEnc->sCmn.indices.Seed; |
michael@0 | 171 | ec_prevLagIndex_copy = psEnc->sCmn.ec_prevLagIndex; |
michael@0 | 172 | ec_prevSignalType_copy = psEnc->sCmn.ec_prevSignalType; |
michael@0 | 173 | for( iter = 0; ; iter++ ) { |
michael@0 | 174 | if( gainsID == gainsID_lower ) { |
michael@0 | 175 | nBits = nBits_lower; |
michael@0 | 176 | } else if( gainsID == gainsID_upper ) { |
michael@0 | 177 | nBits = nBits_upper; |
michael@0 | 178 | } else { |
michael@0 | 179 | /* Restore part of the input state */ |
michael@0 | 180 | if( iter > 0 ) { |
michael@0 | 181 | silk_memcpy( psRangeEnc, &sRangeEnc_copy, sizeof( ec_enc ) ); |
michael@0 | 182 | silk_memcpy( &psEnc->sCmn.sNSQ, &sNSQ_copy, sizeof( silk_nsq_state ) ); |
michael@0 | 183 | psEnc->sCmn.indices.Seed = seed_copy; |
michael@0 | 184 | psEnc->sCmn.ec_prevLagIndex = ec_prevLagIndex_copy; |
michael@0 | 185 | psEnc->sCmn.ec_prevSignalType = ec_prevSignalType_copy; |
michael@0 | 186 | } |
michael@0 | 187 | |
michael@0 | 188 | /*****************************************/ |
michael@0 | 189 | /* Noise shaping quantization */ |
michael@0 | 190 | /*****************************************/ |
michael@0 | 191 | silk_NSQ_wrapper_FLP( psEnc, &sEncCtrl, &psEnc->sCmn.indices, &psEnc->sCmn.sNSQ, psEnc->sCmn.pulses, xfw ); |
michael@0 | 192 | |
michael@0 | 193 | /****************************************/ |
michael@0 | 194 | /* Encode Parameters */ |
michael@0 | 195 | /****************************************/ |
michael@0 | 196 | silk_encode_indices( &psEnc->sCmn, psRangeEnc, psEnc->sCmn.nFramesEncoded, 0, condCoding ); |
michael@0 | 197 | |
michael@0 | 198 | /****************************************/ |
michael@0 | 199 | /* Encode Excitation Signal */ |
michael@0 | 200 | /****************************************/ |
michael@0 | 201 | silk_encode_pulses( psRangeEnc, psEnc->sCmn.indices.signalType, psEnc->sCmn.indices.quantOffsetType, |
michael@0 | 202 | psEnc->sCmn.pulses, psEnc->sCmn.frame_length ); |
michael@0 | 203 | |
michael@0 | 204 | nBits = ec_tell( psRangeEnc ); |
michael@0 | 205 | |
michael@0 | 206 | if( useCBR == 0 && iter == 0 && nBits <= maxBits ) { |
michael@0 | 207 | break; |
michael@0 | 208 | } |
michael@0 | 209 | } |
michael@0 | 210 | |
michael@0 | 211 | if( iter == maxIter ) { |
michael@0 | 212 | if( found_lower && ( gainsID == gainsID_lower || nBits > maxBits ) ) { |
michael@0 | 213 | /* Restore output state from earlier iteration that did meet the bitrate budget */ |
michael@0 | 214 | silk_memcpy( psRangeEnc, &sRangeEnc_copy2, sizeof( ec_enc ) ); |
michael@0 | 215 | silk_assert( sRangeEnc_copy2.offs <= 1275 ); |
michael@0 | 216 | silk_memcpy( psRangeEnc->buf, ec_buf_copy, sRangeEnc_copy2.offs ); |
michael@0 | 217 | silk_memcpy( &psEnc->sCmn.sNSQ, &sNSQ_copy2, sizeof( silk_nsq_state ) ); |
michael@0 | 218 | psEnc->sShape.LastGainIndex = LastGainIndex_copy2; |
michael@0 | 219 | } |
michael@0 | 220 | break; |
michael@0 | 221 | } |
michael@0 | 222 | |
michael@0 | 223 | if( nBits > maxBits ) { |
michael@0 | 224 | if( found_lower == 0 && iter >= 2 ) { |
michael@0 | 225 | /* Adjust the quantizer's rate/distortion tradeoff and discard previous "upper" results */ |
michael@0 | 226 | sEncCtrl.Lambda *= 1.5f; |
michael@0 | 227 | found_upper = 0; |
michael@0 | 228 | gainsID_upper = -1; |
michael@0 | 229 | } else { |
michael@0 | 230 | found_upper = 1; |
michael@0 | 231 | nBits_upper = nBits; |
michael@0 | 232 | gainMult_upper = gainMult_Q8; |
michael@0 | 233 | gainsID_upper = gainsID; |
michael@0 | 234 | } |
michael@0 | 235 | } else if( nBits < maxBits - 5 ) { |
michael@0 | 236 | found_lower = 1; |
michael@0 | 237 | nBits_lower = nBits; |
michael@0 | 238 | gainMult_lower = gainMult_Q8; |
michael@0 | 239 | if( gainsID != gainsID_lower ) { |
michael@0 | 240 | gainsID_lower = gainsID; |
michael@0 | 241 | /* Copy part of the output state */ |
michael@0 | 242 | silk_memcpy( &sRangeEnc_copy2, psRangeEnc, sizeof( ec_enc ) ); |
michael@0 | 243 | silk_assert( psRangeEnc->offs <= 1275 ); |
michael@0 | 244 | silk_memcpy( ec_buf_copy, psRangeEnc->buf, psRangeEnc->offs ); |
michael@0 | 245 | silk_memcpy( &sNSQ_copy2, &psEnc->sCmn.sNSQ, sizeof( silk_nsq_state ) ); |
michael@0 | 246 | LastGainIndex_copy2 = psEnc->sShape.LastGainIndex; |
michael@0 | 247 | } |
michael@0 | 248 | } else { |
michael@0 | 249 | /* Within 5 bits of budget: close enough */ |
michael@0 | 250 | break; |
michael@0 | 251 | } |
michael@0 | 252 | |
michael@0 | 253 | if( ( found_lower & found_upper ) == 0 ) { |
michael@0 | 254 | /* Adjust gain according to high-rate rate/distortion curve */ |
michael@0 | 255 | opus_int32 gain_factor_Q16; |
michael@0 | 256 | gain_factor_Q16 = silk_log2lin( silk_LSHIFT( nBits - maxBits, 7 ) / psEnc->sCmn.frame_length + SILK_FIX_CONST( 16, 7 ) ); |
michael@0 | 257 | gain_factor_Q16 = silk_min_32( gain_factor_Q16, SILK_FIX_CONST( 2, 16 ) ); |
michael@0 | 258 | if( nBits > maxBits ) { |
michael@0 | 259 | gain_factor_Q16 = silk_max_32( gain_factor_Q16, SILK_FIX_CONST( 1.3, 16 ) ); |
michael@0 | 260 | } |
michael@0 | 261 | gainMult_Q8 = silk_SMULWB( gain_factor_Q16, gainMult_Q8 ); |
michael@0 | 262 | } else { |
michael@0 | 263 | /* Adjust gain by interpolating */ |
michael@0 | 264 | gainMult_Q8 = gainMult_lower + ( ( gainMult_upper - gainMult_lower ) * ( maxBits - nBits_lower ) ) / ( nBits_upper - nBits_lower ); |
michael@0 | 265 | /* New gain multplier must be between 25% and 75% of old range (note that gainMult_upper < gainMult_lower) */ |
michael@0 | 266 | if( gainMult_Q8 > silk_ADD_RSHIFT32( gainMult_lower, gainMult_upper - gainMult_lower, 2 ) ) { |
michael@0 | 267 | gainMult_Q8 = silk_ADD_RSHIFT32( gainMult_lower, gainMult_upper - gainMult_lower, 2 ); |
michael@0 | 268 | } else |
michael@0 | 269 | if( gainMult_Q8 < silk_SUB_RSHIFT32( gainMult_upper, gainMult_upper - gainMult_lower, 2 ) ) { |
michael@0 | 270 | gainMult_Q8 = silk_SUB_RSHIFT32( gainMult_upper, gainMult_upper - gainMult_lower, 2 ); |
michael@0 | 271 | } |
michael@0 | 272 | } |
michael@0 | 273 | |
michael@0 | 274 | for( i = 0; i < psEnc->sCmn.nb_subfr; i++ ) { |
michael@0 | 275 | pGains_Q16[ i ] = silk_LSHIFT_SAT32( silk_SMULWB( sEncCtrl.GainsUnq_Q16[ i ], gainMult_Q8 ), 8 ); |
michael@0 | 276 | } |
michael@0 | 277 | |
michael@0 | 278 | /* Quantize gains */ |
michael@0 | 279 | psEnc->sShape.LastGainIndex = sEncCtrl.lastGainIndexPrev; |
michael@0 | 280 | silk_gains_quant( psEnc->sCmn.indices.GainsIndices, pGains_Q16, |
michael@0 | 281 | &psEnc->sShape.LastGainIndex, condCoding == CODE_CONDITIONALLY, psEnc->sCmn.nb_subfr ); |
michael@0 | 282 | |
michael@0 | 283 | /* Unique identifier of gains vector */ |
michael@0 | 284 | gainsID = silk_gains_ID( psEnc->sCmn.indices.GainsIndices, psEnc->sCmn.nb_subfr ); |
michael@0 | 285 | |
michael@0 | 286 | /* Overwrite unquantized gains with quantized gains and convert back to Q0 from Q16 */ |
michael@0 | 287 | for( i = 0; i < psEnc->sCmn.nb_subfr; i++ ) { |
michael@0 | 288 | sEncCtrl.Gains[ i ] = pGains_Q16[ i ] / 65536.0f; |
michael@0 | 289 | } |
michael@0 | 290 | } |
michael@0 | 291 | } |
michael@0 | 292 | |
michael@0 | 293 | /* Update input buffer */ |
michael@0 | 294 | silk_memmove( psEnc->x_buf, &psEnc->x_buf[ psEnc->sCmn.frame_length ], |
michael@0 | 295 | ( psEnc->sCmn.ltp_mem_length + LA_SHAPE_MS * psEnc->sCmn.fs_kHz ) * sizeof( silk_float ) ); |
michael@0 | 296 | |
michael@0 | 297 | /* Exit without entropy coding */ |
michael@0 | 298 | if( psEnc->sCmn.prefillFlag ) { |
michael@0 | 299 | /* No payload */ |
michael@0 | 300 | *pnBytesOut = 0; |
michael@0 | 301 | return ret; |
michael@0 | 302 | } |
michael@0 | 303 | |
michael@0 | 304 | /* Parameters needed for next frame */ |
michael@0 | 305 | psEnc->sCmn.prevLag = sEncCtrl.pitchL[ psEnc->sCmn.nb_subfr - 1 ]; |
michael@0 | 306 | psEnc->sCmn.prevSignalType = psEnc->sCmn.indices.signalType; |
michael@0 | 307 | |
michael@0 | 308 | /****************************************/ |
michael@0 | 309 | /* Finalize payload */ |
michael@0 | 310 | /****************************************/ |
michael@0 | 311 | psEnc->sCmn.first_frame_after_reset = 0; |
michael@0 | 312 | /* Payload size */ |
michael@0 | 313 | *pnBytesOut = silk_RSHIFT( ec_tell( psRangeEnc ) + 7, 3 ); |
michael@0 | 314 | |
michael@0 | 315 | return ret; |
michael@0 | 316 | } |
michael@0 | 317 | |
michael@0 | 318 | /* Low-Bitrate Redundancy (LBRR) encoding. Reuse all parameters but encode excitation at lower bitrate */ |
michael@0 | 319 | static OPUS_INLINE void silk_LBRR_encode_FLP( |
michael@0 | 320 | silk_encoder_state_FLP *psEnc, /* I/O Encoder state FLP */ |
michael@0 | 321 | silk_encoder_control_FLP *psEncCtrl, /* I/O Encoder control FLP */ |
michael@0 | 322 | const silk_float xfw[], /* I Input signal */ |
michael@0 | 323 | opus_int condCoding /* I The type of conditional coding used so far for this frame */ |
michael@0 | 324 | ) |
michael@0 | 325 | { |
michael@0 | 326 | opus_int k; |
michael@0 | 327 | opus_int32 Gains_Q16[ MAX_NB_SUBFR ]; |
michael@0 | 328 | silk_float TempGains[ MAX_NB_SUBFR ]; |
michael@0 | 329 | SideInfoIndices *psIndices_LBRR = &psEnc->sCmn.indices_LBRR[ psEnc->sCmn.nFramesEncoded ]; |
michael@0 | 330 | silk_nsq_state sNSQ_LBRR; |
michael@0 | 331 | |
michael@0 | 332 | /*******************************************/ |
michael@0 | 333 | /* Control use of inband LBRR */ |
michael@0 | 334 | /*******************************************/ |
michael@0 | 335 | if( psEnc->sCmn.LBRR_enabled && psEnc->sCmn.speech_activity_Q8 > SILK_FIX_CONST( LBRR_SPEECH_ACTIVITY_THRES, 8 ) ) { |
michael@0 | 336 | psEnc->sCmn.LBRR_flags[ psEnc->sCmn.nFramesEncoded ] = 1; |
michael@0 | 337 | |
michael@0 | 338 | /* Copy noise shaping quantizer state and quantization indices from regular encoding */ |
michael@0 | 339 | silk_memcpy( &sNSQ_LBRR, &psEnc->sCmn.sNSQ, sizeof( silk_nsq_state ) ); |
michael@0 | 340 | silk_memcpy( psIndices_LBRR, &psEnc->sCmn.indices, sizeof( SideInfoIndices ) ); |
michael@0 | 341 | |
michael@0 | 342 | /* Save original gains */ |
michael@0 | 343 | silk_memcpy( TempGains, psEncCtrl->Gains, psEnc->sCmn.nb_subfr * sizeof( silk_float ) ); |
michael@0 | 344 | |
michael@0 | 345 | if( psEnc->sCmn.nFramesEncoded == 0 || psEnc->sCmn.LBRR_flags[ psEnc->sCmn.nFramesEncoded - 1 ] == 0 ) { |
michael@0 | 346 | /* First frame in packet or previous frame not LBRR coded */ |
michael@0 | 347 | psEnc->sCmn.LBRRprevLastGainIndex = psEnc->sShape.LastGainIndex; |
michael@0 | 348 | |
michael@0 | 349 | /* Increase Gains to get target LBRR rate */ |
michael@0 | 350 | psIndices_LBRR->GainsIndices[ 0 ] += psEnc->sCmn.LBRR_GainIncreases; |
michael@0 | 351 | psIndices_LBRR->GainsIndices[ 0 ] = silk_min_int( psIndices_LBRR->GainsIndices[ 0 ], N_LEVELS_QGAIN - 1 ); |
michael@0 | 352 | } |
michael@0 | 353 | |
michael@0 | 354 | /* Decode to get gains in sync with decoder */ |
michael@0 | 355 | silk_gains_dequant( Gains_Q16, psIndices_LBRR->GainsIndices, |
michael@0 | 356 | &psEnc->sCmn.LBRRprevLastGainIndex, condCoding == CODE_CONDITIONALLY, psEnc->sCmn.nb_subfr ); |
michael@0 | 357 | |
michael@0 | 358 | /* Overwrite unquantized gains with quantized gains and convert back to Q0 from Q16 */ |
michael@0 | 359 | for( k = 0; k < psEnc->sCmn.nb_subfr; k++ ) { |
michael@0 | 360 | psEncCtrl->Gains[ k ] = Gains_Q16[ k ] * ( 1.0f / 65536.0f ); |
michael@0 | 361 | } |
michael@0 | 362 | |
michael@0 | 363 | /*****************************************/ |
michael@0 | 364 | /* Noise shaping quantization */ |
michael@0 | 365 | /*****************************************/ |
michael@0 | 366 | silk_NSQ_wrapper_FLP( psEnc, psEncCtrl, psIndices_LBRR, &sNSQ_LBRR, |
michael@0 | 367 | psEnc->sCmn.pulses_LBRR[ psEnc->sCmn.nFramesEncoded ], xfw ); |
michael@0 | 368 | |
michael@0 | 369 | /* Restore original gains */ |
michael@0 | 370 | silk_memcpy( psEncCtrl->Gains, TempGains, psEnc->sCmn.nb_subfr * sizeof( silk_float ) ); |
michael@0 | 371 | } |
michael@0 | 372 | } |