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