1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/media/libopus/silk/enc_API.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,556 @@ 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 +#include "define.h" 1.35 +#include "API.h" 1.36 +#include "control.h" 1.37 +#include "typedef.h" 1.38 +#include "stack_alloc.h" 1.39 +#include "structs.h" 1.40 +#include "tuning_parameters.h" 1.41 +#ifdef FIXED_POINT 1.42 +#include "main_FIX.h" 1.43 +#else 1.44 +#include "main_FLP.h" 1.45 +#endif 1.46 + 1.47 +/***************************************/ 1.48 +/* Read control structure from encoder */ 1.49 +/***************************************/ 1.50 +static opus_int silk_QueryEncoder( /* O Returns error code */ 1.51 + const void *encState, /* I State */ 1.52 + silk_EncControlStruct *encStatus /* O Encoder Status */ 1.53 +); 1.54 + 1.55 +/****************************************/ 1.56 +/* Encoder functions */ 1.57 +/****************************************/ 1.58 + 1.59 +opus_int silk_Get_Encoder_Size( /* O Returns error code */ 1.60 + opus_int *encSizeBytes /* O Number of bytes in SILK encoder state */ 1.61 +) 1.62 +{ 1.63 + opus_int ret = SILK_NO_ERROR; 1.64 + 1.65 + *encSizeBytes = sizeof( silk_encoder ); 1.66 + 1.67 + return ret; 1.68 +} 1.69 + 1.70 +/*************************/ 1.71 +/* Init or Reset encoder */ 1.72 +/*************************/ 1.73 +opus_int silk_InitEncoder( /* O Returns error code */ 1.74 + void *encState, /* I/O State */ 1.75 + int arch, /* I Run-time architecture */ 1.76 + silk_EncControlStruct *encStatus /* O Encoder Status */ 1.77 +) 1.78 +{ 1.79 + silk_encoder *psEnc; 1.80 + opus_int n, ret = SILK_NO_ERROR; 1.81 + 1.82 + psEnc = (silk_encoder *)encState; 1.83 + 1.84 + /* Reset encoder */ 1.85 + silk_memset( psEnc, 0, sizeof( silk_encoder ) ); 1.86 + for( n = 0; n < ENCODER_NUM_CHANNELS; n++ ) { 1.87 + if( ret += silk_init_encoder( &psEnc->state_Fxx[ n ], arch ) ) { 1.88 + silk_assert( 0 ); 1.89 + } 1.90 + } 1.91 + 1.92 + psEnc->nChannelsAPI = 1; 1.93 + psEnc->nChannelsInternal = 1; 1.94 + 1.95 + /* Read control structure */ 1.96 + if( ret += silk_QueryEncoder( encState, encStatus ) ) { 1.97 + silk_assert( 0 ); 1.98 + } 1.99 + 1.100 + return ret; 1.101 +} 1.102 + 1.103 +/***************************************/ 1.104 +/* Read control structure from encoder */ 1.105 +/***************************************/ 1.106 +static opus_int silk_QueryEncoder( /* O Returns error code */ 1.107 + const void *encState, /* I State */ 1.108 + silk_EncControlStruct *encStatus /* O Encoder Status */ 1.109 +) 1.110 +{ 1.111 + opus_int ret = SILK_NO_ERROR; 1.112 + silk_encoder_state_Fxx *state_Fxx; 1.113 + silk_encoder *psEnc = (silk_encoder *)encState; 1.114 + 1.115 + state_Fxx = psEnc->state_Fxx; 1.116 + 1.117 + encStatus->nChannelsAPI = psEnc->nChannelsAPI; 1.118 + encStatus->nChannelsInternal = psEnc->nChannelsInternal; 1.119 + encStatus->API_sampleRate = state_Fxx[ 0 ].sCmn.API_fs_Hz; 1.120 + encStatus->maxInternalSampleRate = state_Fxx[ 0 ].sCmn.maxInternal_fs_Hz; 1.121 + encStatus->minInternalSampleRate = state_Fxx[ 0 ].sCmn.minInternal_fs_Hz; 1.122 + encStatus->desiredInternalSampleRate = state_Fxx[ 0 ].sCmn.desiredInternal_fs_Hz; 1.123 + encStatus->payloadSize_ms = state_Fxx[ 0 ].sCmn.PacketSize_ms; 1.124 + encStatus->bitRate = state_Fxx[ 0 ].sCmn.TargetRate_bps; 1.125 + encStatus->packetLossPercentage = state_Fxx[ 0 ].sCmn.PacketLoss_perc; 1.126 + encStatus->complexity = state_Fxx[ 0 ].sCmn.Complexity; 1.127 + encStatus->useInBandFEC = state_Fxx[ 0 ].sCmn.useInBandFEC; 1.128 + encStatus->useDTX = state_Fxx[ 0 ].sCmn.useDTX; 1.129 + encStatus->useCBR = state_Fxx[ 0 ].sCmn.useCBR; 1.130 + encStatus->internalSampleRate = silk_SMULBB( state_Fxx[ 0 ].sCmn.fs_kHz, 1000 ); 1.131 + encStatus->allowBandwidthSwitch = state_Fxx[ 0 ].sCmn.allow_bandwidth_switch; 1.132 + encStatus->inWBmodeWithoutVariableLP = state_Fxx[ 0 ].sCmn.fs_kHz == 16 && state_Fxx[ 0 ].sCmn.sLP.mode == 0; 1.133 + 1.134 + return ret; 1.135 +} 1.136 + 1.137 + 1.138 +/**************************/ 1.139 +/* Encode frame with Silk */ 1.140 +/**************************/ 1.141 +/* Note: if prefillFlag is set, the input must contain 10 ms of audio, irrespective of what */ 1.142 +/* encControl->payloadSize_ms is set to */ 1.143 +opus_int silk_Encode( /* O Returns error code */ 1.144 + void *encState, /* I/O State */ 1.145 + silk_EncControlStruct *encControl, /* I Control status */ 1.146 + const opus_int16 *samplesIn, /* I Speech sample input vector */ 1.147 + opus_int nSamplesIn, /* I Number of samples in input vector */ 1.148 + ec_enc *psRangeEnc, /* I/O Compressor data structure */ 1.149 + opus_int32 *nBytesOut, /* I/O Number of bytes in payload (input: Max bytes) */ 1.150 + const opus_int prefillFlag /* I Flag to indicate prefilling buffers no coding */ 1.151 +) 1.152 +{ 1.153 + opus_int n, i, nBits, flags, tmp_payloadSize_ms = 0, tmp_complexity = 0, ret = 0; 1.154 + opus_int nSamplesToBuffer, nSamplesToBufferMax, nBlocksOf10ms; 1.155 + opus_int nSamplesFromInput = 0, nSamplesFromInputMax; 1.156 + opus_int speech_act_thr_for_switch_Q8; 1.157 + opus_int32 TargetRate_bps, MStargetRates_bps[ 2 ], channelRate_bps, LBRR_symbol, sum; 1.158 + silk_encoder *psEnc = ( silk_encoder * )encState; 1.159 + VARDECL( opus_int16, buf ); 1.160 + opus_int transition, curr_block, tot_blocks; 1.161 + SAVE_STACK; 1.162 + 1.163 + if (encControl->reducedDependency) 1.164 + { 1.165 + psEnc->state_Fxx[0].sCmn.first_frame_after_reset = 1; 1.166 + psEnc->state_Fxx[1].sCmn.first_frame_after_reset = 1; 1.167 + } 1.168 + psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded = psEnc->state_Fxx[ 1 ].sCmn.nFramesEncoded = 0; 1.169 + 1.170 + /* Check values in encoder control structure */ 1.171 + if( ( ret = check_control_input( encControl ) != 0 ) ) { 1.172 + silk_assert( 0 ); 1.173 + RESTORE_STACK; 1.174 + return ret; 1.175 + } 1.176 + 1.177 + encControl->switchReady = 0; 1.178 + 1.179 + if( encControl->nChannelsInternal > psEnc->nChannelsInternal ) { 1.180 + /* Mono -> Stereo transition: init state of second channel and stereo state */ 1.181 + ret += silk_init_encoder( &psEnc->state_Fxx[ 1 ], psEnc->state_Fxx[ 0 ].sCmn.arch ); 1.182 + silk_memset( psEnc->sStereo.pred_prev_Q13, 0, sizeof( psEnc->sStereo.pred_prev_Q13 ) ); 1.183 + silk_memset( psEnc->sStereo.sSide, 0, sizeof( psEnc->sStereo.sSide ) ); 1.184 + psEnc->sStereo.mid_side_amp_Q0[ 0 ] = 0; 1.185 + psEnc->sStereo.mid_side_amp_Q0[ 1 ] = 1; 1.186 + psEnc->sStereo.mid_side_amp_Q0[ 2 ] = 0; 1.187 + psEnc->sStereo.mid_side_amp_Q0[ 3 ] = 1; 1.188 + psEnc->sStereo.width_prev_Q14 = 0; 1.189 + psEnc->sStereo.smth_width_Q14 = SILK_FIX_CONST( 1, 14 ); 1.190 + if( psEnc->nChannelsAPI == 2 ) { 1.191 + silk_memcpy( &psEnc->state_Fxx[ 1 ].sCmn.resampler_state, &psEnc->state_Fxx[ 0 ].sCmn.resampler_state, sizeof( silk_resampler_state_struct ) ); 1.192 + silk_memcpy( &psEnc->state_Fxx[ 1 ].sCmn.In_HP_State, &psEnc->state_Fxx[ 0 ].sCmn.In_HP_State, sizeof( psEnc->state_Fxx[ 1 ].sCmn.In_HP_State ) ); 1.193 + } 1.194 + } 1.195 + 1.196 + transition = (encControl->payloadSize_ms != psEnc->state_Fxx[ 0 ].sCmn.PacketSize_ms) || (psEnc->nChannelsInternal != encControl->nChannelsInternal); 1.197 + 1.198 + psEnc->nChannelsAPI = encControl->nChannelsAPI; 1.199 + psEnc->nChannelsInternal = encControl->nChannelsInternal; 1.200 + 1.201 + nBlocksOf10ms = silk_DIV32( 100 * nSamplesIn, encControl->API_sampleRate ); 1.202 + tot_blocks = ( nBlocksOf10ms > 1 ) ? nBlocksOf10ms >> 1 : 1; 1.203 + curr_block = 0; 1.204 + if( prefillFlag ) { 1.205 + /* Only accept input length of 10 ms */ 1.206 + if( nBlocksOf10ms != 1 ) { 1.207 + silk_assert( 0 ); 1.208 + RESTORE_STACK; 1.209 + return SILK_ENC_INPUT_INVALID_NO_OF_SAMPLES; 1.210 + } 1.211 + /* Reset Encoder */ 1.212 + for( n = 0; n < encControl->nChannelsInternal; n++ ) { 1.213 + ret = silk_init_encoder( &psEnc->state_Fxx[ n ], psEnc->state_Fxx[ n ].sCmn.arch ); 1.214 + silk_assert( !ret ); 1.215 + } 1.216 + tmp_payloadSize_ms = encControl->payloadSize_ms; 1.217 + encControl->payloadSize_ms = 10; 1.218 + tmp_complexity = encControl->complexity; 1.219 + encControl->complexity = 0; 1.220 + for( n = 0; n < encControl->nChannelsInternal; n++ ) { 1.221 + psEnc->state_Fxx[ n ].sCmn.controlled_since_last_payload = 0; 1.222 + psEnc->state_Fxx[ n ].sCmn.prefillFlag = 1; 1.223 + } 1.224 + } else { 1.225 + /* Only accept input lengths that are a multiple of 10 ms */ 1.226 + if( nBlocksOf10ms * encControl->API_sampleRate != 100 * nSamplesIn || nSamplesIn < 0 ) { 1.227 + silk_assert( 0 ); 1.228 + RESTORE_STACK; 1.229 + return SILK_ENC_INPUT_INVALID_NO_OF_SAMPLES; 1.230 + } 1.231 + /* Make sure no more than one packet can be produced */ 1.232 + if( 1000 * (opus_int32)nSamplesIn > encControl->payloadSize_ms * encControl->API_sampleRate ) { 1.233 + silk_assert( 0 ); 1.234 + RESTORE_STACK; 1.235 + return SILK_ENC_INPUT_INVALID_NO_OF_SAMPLES; 1.236 + } 1.237 + } 1.238 + 1.239 + TargetRate_bps = silk_RSHIFT32( encControl->bitRate, encControl->nChannelsInternal - 1 ); 1.240 + for( n = 0; n < encControl->nChannelsInternal; n++ ) { 1.241 + /* Force the side channel to the same rate as the mid */ 1.242 + opus_int force_fs_kHz = (n==1) ? psEnc->state_Fxx[0].sCmn.fs_kHz : 0; 1.243 + if( ( ret = silk_control_encoder( &psEnc->state_Fxx[ n ], encControl, TargetRate_bps, psEnc->allowBandwidthSwitch, n, force_fs_kHz ) ) != 0 ) { 1.244 + silk_assert( 0 ); 1.245 + RESTORE_STACK; 1.246 + return ret; 1.247 + } 1.248 + if( psEnc->state_Fxx[n].sCmn.first_frame_after_reset || transition ) { 1.249 + for( i = 0; i < psEnc->state_Fxx[ 0 ].sCmn.nFramesPerPacket; i++ ) { 1.250 + psEnc->state_Fxx[ n ].sCmn.LBRR_flags[ i ] = 0; 1.251 + } 1.252 + } 1.253 + psEnc->state_Fxx[ n ].sCmn.inDTX = psEnc->state_Fxx[ n ].sCmn.useDTX; 1.254 + } 1.255 + silk_assert( encControl->nChannelsInternal == 1 || psEnc->state_Fxx[ 0 ].sCmn.fs_kHz == psEnc->state_Fxx[ 1 ].sCmn.fs_kHz ); 1.256 + 1.257 + /* Input buffering/resampling and encoding */ 1.258 + nSamplesToBufferMax = 1.259 + 10 * nBlocksOf10ms * psEnc->state_Fxx[ 0 ].sCmn.fs_kHz; 1.260 + nSamplesFromInputMax = 1.261 + silk_DIV32_16( nSamplesToBufferMax * 1.262 + psEnc->state_Fxx[ 0 ].sCmn.API_fs_Hz, 1.263 + psEnc->state_Fxx[ 0 ].sCmn.fs_kHz * 1000 ); 1.264 + ALLOC( buf, nSamplesFromInputMax, opus_int16 ); 1.265 + while( 1 ) { 1.266 + nSamplesToBuffer = psEnc->state_Fxx[ 0 ].sCmn.frame_length - psEnc->state_Fxx[ 0 ].sCmn.inputBufIx; 1.267 + nSamplesToBuffer = silk_min( nSamplesToBuffer, nSamplesToBufferMax ); 1.268 + nSamplesFromInput = silk_DIV32_16( nSamplesToBuffer * psEnc->state_Fxx[ 0 ].sCmn.API_fs_Hz, psEnc->state_Fxx[ 0 ].sCmn.fs_kHz * 1000 ); 1.269 + /* Resample and write to buffer */ 1.270 + if( encControl->nChannelsAPI == 2 && encControl->nChannelsInternal == 2 ) { 1.271 + opus_int id = psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded; 1.272 + for( n = 0; n < nSamplesFromInput; n++ ) { 1.273 + buf[ n ] = samplesIn[ 2 * n ]; 1.274 + } 1.275 + /* Making sure to start both resamplers from the same state when switching from mono to stereo */ 1.276 + if( psEnc->nPrevChannelsInternal == 1 && id==0 ) { 1.277 + silk_memcpy( &psEnc->state_Fxx[ 1 ].sCmn.resampler_state, &psEnc->state_Fxx[ 0 ].sCmn.resampler_state, sizeof(psEnc->state_Fxx[ 1 ].sCmn.resampler_state)); 1.278 + } 1.279 + 1.280 + ret += silk_resampler( &psEnc->state_Fxx[ 0 ].sCmn.resampler_state, 1.281 + &psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ psEnc->state_Fxx[ 0 ].sCmn.inputBufIx + 2 ], buf, nSamplesFromInput ); 1.282 + psEnc->state_Fxx[ 0 ].sCmn.inputBufIx += nSamplesToBuffer; 1.283 + 1.284 + nSamplesToBuffer = psEnc->state_Fxx[ 1 ].sCmn.frame_length - psEnc->state_Fxx[ 1 ].sCmn.inputBufIx; 1.285 + nSamplesToBuffer = silk_min( nSamplesToBuffer, 10 * nBlocksOf10ms * psEnc->state_Fxx[ 1 ].sCmn.fs_kHz ); 1.286 + for( n = 0; n < nSamplesFromInput; n++ ) { 1.287 + buf[ n ] = samplesIn[ 2 * n + 1 ]; 1.288 + } 1.289 + ret += silk_resampler( &psEnc->state_Fxx[ 1 ].sCmn.resampler_state, 1.290 + &psEnc->state_Fxx[ 1 ].sCmn.inputBuf[ psEnc->state_Fxx[ 1 ].sCmn.inputBufIx + 2 ], buf, nSamplesFromInput ); 1.291 + 1.292 + psEnc->state_Fxx[ 1 ].sCmn.inputBufIx += nSamplesToBuffer; 1.293 + } else if( encControl->nChannelsAPI == 2 && encControl->nChannelsInternal == 1 ) { 1.294 + /* Combine left and right channels before resampling */ 1.295 + for( n = 0; n < nSamplesFromInput; n++ ) { 1.296 + sum = samplesIn[ 2 * n ] + samplesIn[ 2 * n + 1 ]; 1.297 + buf[ n ] = (opus_int16)silk_RSHIFT_ROUND( sum, 1 ); 1.298 + } 1.299 + ret += silk_resampler( &psEnc->state_Fxx[ 0 ].sCmn.resampler_state, 1.300 + &psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ psEnc->state_Fxx[ 0 ].sCmn.inputBufIx + 2 ], buf, nSamplesFromInput ); 1.301 + /* On the first mono frame, average the results for the two resampler states */ 1.302 + if( psEnc->nPrevChannelsInternal == 2 && psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded == 0 ) { 1.303 + ret += silk_resampler( &psEnc->state_Fxx[ 1 ].sCmn.resampler_state, 1.304 + &psEnc->state_Fxx[ 1 ].sCmn.inputBuf[ psEnc->state_Fxx[ 1 ].sCmn.inputBufIx + 2 ], buf, nSamplesFromInput ); 1.305 + for( n = 0; n < psEnc->state_Fxx[ 0 ].sCmn.frame_length; n++ ) { 1.306 + psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ psEnc->state_Fxx[ 0 ].sCmn.inputBufIx+n+2 ] = 1.307 + silk_RSHIFT(psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ psEnc->state_Fxx[ 0 ].sCmn.inputBufIx+n+2 ] 1.308 + + psEnc->state_Fxx[ 1 ].sCmn.inputBuf[ psEnc->state_Fxx[ 1 ].sCmn.inputBufIx+n+2 ], 1); 1.309 + } 1.310 + } 1.311 + psEnc->state_Fxx[ 0 ].sCmn.inputBufIx += nSamplesToBuffer; 1.312 + } else { 1.313 + silk_assert( encControl->nChannelsAPI == 1 && encControl->nChannelsInternal == 1 ); 1.314 + silk_memcpy(buf, samplesIn, nSamplesFromInput*sizeof(opus_int16)); 1.315 + ret += silk_resampler( &psEnc->state_Fxx[ 0 ].sCmn.resampler_state, 1.316 + &psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ psEnc->state_Fxx[ 0 ].sCmn.inputBufIx + 2 ], buf, nSamplesFromInput ); 1.317 + psEnc->state_Fxx[ 0 ].sCmn.inputBufIx += nSamplesToBuffer; 1.318 + } 1.319 + 1.320 + samplesIn += nSamplesFromInput * encControl->nChannelsAPI; 1.321 + nSamplesIn -= nSamplesFromInput; 1.322 + 1.323 + /* Default */ 1.324 + psEnc->allowBandwidthSwitch = 0; 1.325 + 1.326 + /* Silk encoder */ 1.327 + if( psEnc->state_Fxx[ 0 ].sCmn.inputBufIx >= psEnc->state_Fxx[ 0 ].sCmn.frame_length ) { 1.328 + /* Enough data in input buffer, so encode */ 1.329 + silk_assert( psEnc->state_Fxx[ 0 ].sCmn.inputBufIx == psEnc->state_Fxx[ 0 ].sCmn.frame_length ); 1.330 + silk_assert( encControl->nChannelsInternal == 1 || psEnc->state_Fxx[ 1 ].sCmn.inputBufIx == psEnc->state_Fxx[ 1 ].sCmn.frame_length ); 1.331 + 1.332 + /* Deal with LBRR data */ 1.333 + if( psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded == 0 && !prefillFlag ) { 1.334 + /* Create space at start of payload for VAD and FEC flags */ 1.335 + opus_uint8 iCDF[ 2 ] = { 0, 0 }; 1.336 + iCDF[ 0 ] = 256 - silk_RSHIFT( 256, ( psEnc->state_Fxx[ 0 ].sCmn.nFramesPerPacket + 1 ) * encControl->nChannelsInternal ); 1.337 + ec_enc_icdf( psRangeEnc, 0, iCDF, 8 ); 1.338 + 1.339 + /* Encode any LBRR data from previous packet */ 1.340 + /* Encode LBRR flags */ 1.341 + for( n = 0; n < encControl->nChannelsInternal; n++ ) { 1.342 + LBRR_symbol = 0; 1.343 + for( i = 0; i < psEnc->state_Fxx[ n ].sCmn.nFramesPerPacket; i++ ) { 1.344 + LBRR_symbol |= silk_LSHIFT( psEnc->state_Fxx[ n ].sCmn.LBRR_flags[ i ], i ); 1.345 + } 1.346 + psEnc->state_Fxx[ n ].sCmn.LBRR_flag = LBRR_symbol > 0 ? 1 : 0; 1.347 + if( LBRR_symbol && psEnc->state_Fxx[ n ].sCmn.nFramesPerPacket > 1 ) { 1.348 + ec_enc_icdf( psRangeEnc, LBRR_symbol - 1, silk_LBRR_flags_iCDF_ptr[ psEnc->state_Fxx[ n ].sCmn.nFramesPerPacket - 2 ], 8 ); 1.349 + } 1.350 + } 1.351 + 1.352 + /* Code LBRR indices and excitation signals */ 1.353 + for( i = 0; i < psEnc->state_Fxx[ 0 ].sCmn.nFramesPerPacket; i++ ) { 1.354 + for( n = 0; n < encControl->nChannelsInternal; n++ ) { 1.355 + if( psEnc->state_Fxx[ n ].sCmn.LBRR_flags[ i ] ) { 1.356 + opus_int condCoding; 1.357 + 1.358 + if( encControl->nChannelsInternal == 2 && n == 0 ) { 1.359 + silk_stereo_encode_pred( psRangeEnc, psEnc->sStereo.predIx[ i ] ); 1.360 + /* For LBRR data there's no need to code the mid-only flag if the side-channel LBRR flag is set */ 1.361 + if( psEnc->state_Fxx[ 1 ].sCmn.LBRR_flags[ i ] == 0 ) { 1.362 + silk_stereo_encode_mid_only( psRangeEnc, psEnc->sStereo.mid_only_flags[ i ] ); 1.363 + } 1.364 + } 1.365 + /* Use conditional coding if previous frame available */ 1.366 + if( i > 0 && psEnc->state_Fxx[ n ].sCmn.LBRR_flags[ i - 1 ] ) { 1.367 + condCoding = CODE_CONDITIONALLY; 1.368 + } else { 1.369 + condCoding = CODE_INDEPENDENTLY; 1.370 + } 1.371 + silk_encode_indices( &psEnc->state_Fxx[ n ].sCmn, psRangeEnc, i, 1, condCoding ); 1.372 + silk_encode_pulses( psRangeEnc, psEnc->state_Fxx[ n ].sCmn.indices_LBRR[i].signalType, psEnc->state_Fxx[ n ].sCmn.indices_LBRR[i].quantOffsetType, 1.373 + psEnc->state_Fxx[ n ].sCmn.pulses_LBRR[ i ], psEnc->state_Fxx[ n ].sCmn.frame_length ); 1.374 + } 1.375 + } 1.376 + } 1.377 + 1.378 + /* Reset LBRR flags */ 1.379 + for( n = 0; n < encControl->nChannelsInternal; n++ ) { 1.380 + silk_memset( psEnc->state_Fxx[ n ].sCmn.LBRR_flags, 0, sizeof( psEnc->state_Fxx[ n ].sCmn.LBRR_flags ) ); 1.381 + } 1.382 + } 1.383 + 1.384 + silk_HP_variable_cutoff( psEnc->state_Fxx ); 1.385 + 1.386 + /* Total target bits for packet */ 1.387 + nBits = silk_DIV32_16( silk_MUL( encControl->bitRate, encControl->payloadSize_ms ), 1000 ); 1.388 + /* Subtract half of the bits already used */ 1.389 + if( !prefillFlag ) { 1.390 + nBits -= ec_tell( psRangeEnc ) >> 1; 1.391 + } 1.392 + /* Divide by number of uncoded frames left in packet */ 1.393 + nBits = silk_DIV32_16( nBits, psEnc->state_Fxx[ 0 ].sCmn.nFramesPerPacket - psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ); 1.394 + /* Convert to bits/second */ 1.395 + if( encControl->payloadSize_ms == 10 ) { 1.396 + TargetRate_bps = silk_SMULBB( nBits, 100 ); 1.397 + } else { 1.398 + TargetRate_bps = silk_SMULBB( nBits, 50 ); 1.399 + } 1.400 + /* Subtract fraction of bits in excess of target in previous packets */ 1.401 + TargetRate_bps -= silk_DIV32_16( silk_MUL( psEnc->nBitsExceeded, 1000 ), BITRESERVOIR_DECAY_TIME_MS ); 1.402 + /* Never exceed input bitrate */ 1.403 + TargetRate_bps = silk_LIMIT( TargetRate_bps, encControl->bitRate, 5000 ); 1.404 + 1.405 + /* Convert Left/Right to Mid/Side */ 1.406 + if( encControl->nChannelsInternal == 2 ) { 1.407 + silk_stereo_LR_to_MS( &psEnc->sStereo, &psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ 2 ], &psEnc->state_Fxx[ 1 ].sCmn.inputBuf[ 2 ], 1.408 + psEnc->sStereo.predIx[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ], &psEnc->sStereo.mid_only_flags[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ], 1.409 + MStargetRates_bps, TargetRate_bps, psEnc->state_Fxx[ 0 ].sCmn.speech_activity_Q8, encControl->toMono, 1.410 + psEnc->state_Fxx[ 0 ].sCmn.fs_kHz, psEnc->state_Fxx[ 0 ].sCmn.frame_length ); 1.411 + if( psEnc->sStereo.mid_only_flags[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ] == 0 ) { 1.412 + /* Reset side channel encoder memory for first frame with side coding */ 1.413 + if( psEnc->prev_decode_only_middle == 1 ) { 1.414 + silk_memset( &psEnc->state_Fxx[ 1 ].sShape, 0, sizeof( psEnc->state_Fxx[ 1 ].sShape ) ); 1.415 + silk_memset( &psEnc->state_Fxx[ 1 ].sPrefilt, 0, sizeof( psEnc->state_Fxx[ 1 ].sPrefilt ) ); 1.416 + silk_memset( &psEnc->state_Fxx[ 1 ].sCmn.sNSQ, 0, sizeof( psEnc->state_Fxx[ 1 ].sCmn.sNSQ ) ); 1.417 + silk_memset( psEnc->state_Fxx[ 1 ].sCmn.prev_NLSFq_Q15, 0, sizeof( psEnc->state_Fxx[ 1 ].sCmn.prev_NLSFq_Q15 ) ); 1.418 + silk_memset( &psEnc->state_Fxx[ 1 ].sCmn.sLP.In_LP_State, 0, sizeof( psEnc->state_Fxx[ 1 ].sCmn.sLP.In_LP_State ) ); 1.419 + psEnc->state_Fxx[ 1 ].sCmn.prevLag = 100; 1.420 + psEnc->state_Fxx[ 1 ].sCmn.sNSQ.lagPrev = 100; 1.421 + psEnc->state_Fxx[ 1 ].sShape.LastGainIndex = 10; 1.422 + psEnc->state_Fxx[ 1 ].sCmn.prevSignalType = TYPE_NO_VOICE_ACTIVITY; 1.423 + psEnc->state_Fxx[ 1 ].sCmn.sNSQ.prev_gain_Q16 = 65536; 1.424 + psEnc->state_Fxx[ 1 ].sCmn.first_frame_after_reset = 1; 1.425 + } 1.426 + silk_encode_do_VAD_Fxx( &psEnc->state_Fxx[ 1 ] ); 1.427 + } else { 1.428 + psEnc->state_Fxx[ 1 ].sCmn.VAD_flags[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ] = 0; 1.429 + } 1.430 + if( !prefillFlag ) { 1.431 + silk_stereo_encode_pred( psRangeEnc, psEnc->sStereo.predIx[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ] ); 1.432 + if( psEnc->state_Fxx[ 1 ].sCmn.VAD_flags[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ] == 0 ) { 1.433 + silk_stereo_encode_mid_only( psRangeEnc, psEnc->sStereo.mid_only_flags[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ] ); 1.434 + } 1.435 + } 1.436 + } else { 1.437 + /* Buffering */ 1.438 + silk_memcpy( psEnc->state_Fxx[ 0 ].sCmn.inputBuf, psEnc->sStereo.sMid, 2 * sizeof( opus_int16 ) ); 1.439 + silk_memcpy( psEnc->sStereo.sMid, &psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ psEnc->state_Fxx[ 0 ].sCmn.frame_length ], 2 * sizeof( opus_int16 ) ); 1.440 + } 1.441 + silk_encode_do_VAD_Fxx( &psEnc->state_Fxx[ 0 ] ); 1.442 + 1.443 + /* Encode */ 1.444 + for( n = 0; n < encControl->nChannelsInternal; n++ ) { 1.445 + opus_int maxBits, useCBR; 1.446 + 1.447 + /* Handling rate constraints */ 1.448 + maxBits = encControl->maxBits; 1.449 + if( tot_blocks == 2 && curr_block == 0 ) { 1.450 + maxBits = maxBits * 3 / 5; 1.451 + } else if( tot_blocks == 3 ) { 1.452 + if( curr_block == 0 ) { 1.453 + maxBits = maxBits * 2 / 5; 1.454 + } else if( curr_block == 1 ) { 1.455 + maxBits = maxBits * 3 / 4; 1.456 + } 1.457 + } 1.458 + useCBR = encControl->useCBR && curr_block == tot_blocks - 1; 1.459 + 1.460 + if( encControl->nChannelsInternal == 1 ) { 1.461 + channelRate_bps = TargetRate_bps; 1.462 + } else { 1.463 + channelRate_bps = MStargetRates_bps[ n ]; 1.464 + if( n == 0 && MStargetRates_bps[ 1 ] > 0 ) { 1.465 + useCBR = 0; 1.466 + /* Give mid up to 1/2 of the max bits for that frame */ 1.467 + maxBits -= encControl->maxBits / ( tot_blocks * 2 ); 1.468 + } 1.469 + } 1.470 + 1.471 + if( channelRate_bps > 0 ) { 1.472 + opus_int condCoding; 1.473 + 1.474 + silk_control_SNR( &psEnc->state_Fxx[ n ].sCmn, channelRate_bps ); 1.475 + 1.476 + /* Use independent coding if no previous frame available */ 1.477 + if( psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded - n <= 0 ) { 1.478 + condCoding = CODE_INDEPENDENTLY; 1.479 + } else if( n > 0 && psEnc->prev_decode_only_middle ) { 1.480 + /* If we skipped a side frame in this packet, we don't 1.481 + need LTP scaling; the LTP state is well-defined. */ 1.482 + condCoding = CODE_INDEPENDENTLY_NO_LTP_SCALING; 1.483 + } else { 1.484 + condCoding = CODE_CONDITIONALLY; 1.485 + } 1.486 + if( ( ret = silk_encode_frame_Fxx( &psEnc->state_Fxx[ n ], nBytesOut, psRangeEnc, condCoding, maxBits, useCBR ) ) != 0 ) { 1.487 + silk_assert( 0 ); 1.488 + } 1.489 + } 1.490 + psEnc->state_Fxx[ n ].sCmn.controlled_since_last_payload = 0; 1.491 + psEnc->state_Fxx[ n ].sCmn.inputBufIx = 0; 1.492 + psEnc->state_Fxx[ n ].sCmn.nFramesEncoded++; 1.493 + } 1.494 + psEnc->prev_decode_only_middle = psEnc->sStereo.mid_only_flags[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded - 1 ]; 1.495 + 1.496 + /* Insert VAD and FEC flags at beginning of bitstream */ 1.497 + if( *nBytesOut > 0 && psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded == psEnc->state_Fxx[ 0 ].sCmn.nFramesPerPacket) { 1.498 + flags = 0; 1.499 + for( n = 0; n < encControl->nChannelsInternal; n++ ) { 1.500 + for( i = 0; i < psEnc->state_Fxx[ n ].sCmn.nFramesPerPacket; i++ ) { 1.501 + flags = silk_LSHIFT( flags, 1 ); 1.502 + flags |= psEnc->state_Fxx[ n ].sCmn.VAD_flags[ i ]; 1.503 + } 1.504 + flags = silk_LSHIFT( flags, 1 ); 1.505 + flags |= psEnc->state_Fxx[ n ].sCmn.LBRR_flag; 1.506 + } 1.507 + if( !prefillFlag ) { 1.508 + ec_enc_patch_initial_bits( psRangeEnc, flags, ( psEnc->state_Fxx[ 0 ].sCmn.nFramesPerPacket + 1 ) * encControl->nChannelsInternal ); 1.509 + } 1.510 + 1.511 + /* Return zero bytes if all channels DTXed */ 1.512 + if( psEnc->state_Fxx[ 0 ].sCmn.inDTX && ( encControl->nChannelsInternal == 1 || psEnc->state_Fxx[ 1 ].sCmn.inDTX ) ) { 1.513 + *nBytesOut = 0; 1.514 + } 1.515 + 1.516 + psEnc->nBitsExceeded += *nBytesOut * 8; 1.517 + psEnc->nBitsExceeded -= silk_DIV32_16( silk_MUL( encControl->bitRate, encControl->payloadSize_ms ), 1000 ); 1.518 + psEnc->nBitsExceeded = silk_LIMIT( psEnc->nBitsExceeded, 0, 10000 ); 1.519 + 1.520 + /* Update flag indicating if bandwidth switching is allowed */ 1.521 + speech_act_thr_for_switch_Q8 = silk_SMLAWB( SILK_FIX_CONST( SPEECH_ACTIVITY_DTX_THRES, 8 ), 1.522 + SILK_FIX_CONST( ( 1 - SPEECH_ACTIVITY_DTX_THRES ) / MAX_BANDWIDTH_SWITCH_DELAY_MS, 16 + 8 ), psEnc->timeSinceSwitchAllowed_ms ); 1.523 + if( psEnc->state_Fxx[ 0 ].sCmn.speech_activity_Q8 < speech_act_thr_for_switch_Q8 ) { 1.524 + psEnc->allowBandwidthSwitch = 1; 1.525 + psEnc->timeSinceSwitchAllowed_ms = 0; 1.526 + } else { 1.527 + psEnc->allowBandwidthSwitch = 0; 1.528 + psEnc->timeSinceSwitchAllowed_ms += encControl->payloadSize_ms; 1.529 + } 1.530 + } 1.531 + 1.532 + if( nSamplesIn == 0 ) { 1.533 + break; 1.534 + } 1.535 + } else { 1.536 + break; 1.537 + } 1.538 + curr_block++; 1.539 + } 1.540 + 1.541 + psEnc->nPrevChannelsInternal = encControl->nChannelsInternal; 1.542 + 1.543 + encControl->allowBandwidthSwitch = psEnc->allowBandwidthSwitch; 1.544 + encControl->inWBmodeWithoutVariableLP = psEnc->state_Fxx[ 0 ].sCmn.fs_kHz == 16 && psEnc->state_Fxx[ 0 ].sCmn.sLP.mode == 0; 1.545 + encControl->internalSampleRate = silk_SMULBB( psEnc->state_Fxx[ 0 ].sCmn.fs_kHz, 1000 ); 1.546 + encControl->stereoWidth_Q14 = encControl->toMono ? 0 : psEnc->sStereo.smth_width_Q14; 1.547 + if( prefillFlag ) { 1.548 + encControl->payloadSize_ms = tmp_payloadSize_ms; 1.549 + encControl->complexity = tmp_complexity; 1.550 + for( n = 0; n < encControl->nChannelsInternal; n++ ) { 1.551 + psEnc->state_Fxx[ n ].sCmn.controlled_since_last_payload = 0; 1.552 + psEnc->state_Fxx[ n ].sCmn.prefillFlag = 0; 1.553 + } 1.554 + } 1.555 + 1.556 + RESTORE_STACK; 1.557 + return ret; 1.558 +} 1.559 +