| OLD | NEW |
| (Empty) | |
| 1 /*********************************************************************** |
| 2 Copyright (c) 2006-2011, Skype Limited. All rights reserved. |
| 3 Redistribution and use in source and binary forms, with or without |
| 4 modification, are permitted provided that the following conditions |
| 5 are met: |
| 6 - Redistributions of source code must retain the above copyright notice, |
| 7 this list of conditions and the following disclaimer. |
| 8 - Redistributions in binary form must reproduce the above copyright |
| 9 notice, this list of conditions and the following disclaimer in the |
| 10 documentation and/or other materials provided with the distribution. |
| 11 - Neither the name of Internet Society, IETF or IETF Trust, nor the |
| 12 names of specific contributors, may be used to endorse or promote |
| 13 products derived from this software without specific prior written |
| 14 permission. |
| 15 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” |
| 16 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 17 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 18 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 19 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 20 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 21 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 22 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 23 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 24 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 25 POSSIBILITY OF SUCH DAMAGE. |
| 26 ***********************************************************************/ |
| 27 |
| 28 #ifdef HAVE_CONFIG_H |
| 29 #include "config.h" |
| 30 #endif |
| 31 #include "define.h" |
| 32 #include "API.h" |
| 33 #include "control.h" |
| 34 #include "typedef.h" |
| 35 #include "structs.h" |
| 36 #include "tuning_parameters.h" |
| 37 #ifdef FIXED_POINT |
| 38 #include "main_FIX.h" |
| 39 #else |
| 40 #include "main_FLP.h" |
| 41 #endif |
| 42 |
| 43 /****************************************/ |
| 44 /* Encoder functions */ |
| 45 /****************************************/ |
| 46 |
| 47 opus_int silk_Get_Encoder_Size( /* O Returns error co
de */ |
| 48 opus_int *encSizeBytes /* O Number of bytes
in SILK encoder state */ |
| 49 ) |
| 50 { |
| 51 opus_int ret = SILK_NO_ERROR; |
| 52 |
| 53 *encSizeBytes = sizeof( silk_encoder ); |
| 54 |
| 55 return ret; |
| 56 } |
| 57 |
| 58 /*************************/ |
| 59 /* Init or Reset encoder */ |
| 60 /*************************/ |
| 61 opus_int silk_InitEncoder( /* O Returns error co
de */ |
| 62 void *encState, /* I/O State
*/ |
| 63 silk_EncControlStruct *encStatus /* O Encoder Status
*/ |
| 64 ) |
| 65 { |
| 66 silk_encoder *psEnc; |
| 67 opus_int n, ret = SILK_NO_ERROR; |
| 68 |
| 69 psEnc = (silk_encoder *)encState; |
| 70 |
| 71 /* Reset encoder */ |
| 72 silk_memset( psEnc, 0, sizeof( silk_encoder ) ); |
| 73 for( n = 0; n < ENCODER_NUM_CHANNELS; n++ ) { |
| 74 if( ret += silk_init_encoder( &psEnc->state_Fxx[ n ] ) ) { |
| 75 silk_assert( 0 ); |
| 76 } |
| 77 } |
| 78 |
| 79 psEnc->nChannelsAPI = 1; |
| 80 psEnc->nChannelsInternal = 1; |
| 81 |
| 82 /* Read control structure */ |
| 83 if( ret += silk_QueryEncoder( encState, encStatus ) ) { |
| 84 silk_assert( 0 ); |
| 85 } |
| 86 |
| 87 return ret; |
| 88 } |
| 89 |
| 90 /***************************************/ |
| 91 /* Read control structure from encoder */ |
| 92 /***************************************/ |
| 93 opus_int silk_QueryEncoder( /* O Returns error co
de */ |
| 94 const void *encState, /* I State
*/ |
| 95 silk_EncControlStruct *encStatus /* O Encoder Status
*/ |
| 96 ) |
| 97 { |
| 98 opus_int ret = SILK_NO_ERROR; |
| 99 silk_encoder_state_Fxx *state_Fxx; |
| 100 silk_encoder *psEnc = (silk_encoder *)encState; |
| 101 |
| 102 state_Fxx = psEnc->state_Fxx; |
| 103 |
| 104 encStatus->nChannelsAPI = psEnc->nChannelsAPI; |
| 105 encStatus->nChannelsInternal = psEnc->nChannelsInternal; |
| 106 encStatus->API_sampleRate = state_Fxx[ 0 ].sCmn.API_fs_Hz; |
| 107 encStatus->maxInternalSampleRate = state_Fxx[ 0 ].sCmn.maxInternal_fs_Hz
; |
| 108 encStatus->minInternalSampleRate = state_Fxx[ 0 ].sCmn.minInternal_fs_Hz
; |
| 109 encStatus->desiredInternalSampleRate = state_Fxx[ 0 ].sCmn.desiredInternal_f
s_Hz; |
| 110 encStatus->payloadSize_ms = state_Fxx[ 0 ].sCmn.PacketSize_ms; |
| 111 encStatus->bitRate = state_Fxx[ 0 ].sCmn.TargetRate_bps; |
| 112 encStatus->packetLossPercentage = state_Fxx[ 0 ].sCmn.PacketLoss_perc; |
| 113 encStatus->complexity = state_Fxx[ 0 ].sCmn.Complexity; |
| 114 encStatus->useInBandFEC = state_Fxx[ 0 ].sCmn.useInBandFEC; |
| 115 encStatus->useDTX = state_Fxx[ 0 ].sCmn.useDTX; |
| 116 encStatus->useCBR = state_Fxx[ 0 ].sCmn.useCBR; |
| 117 encStatus->internalSampleRate = silk_SMULBB( state_Fxx[ 0 ].sCmn.fs_k
Hz, 1000 ); |
| 118 encStatus->allowBandwidthSwitch = state_Fxx[ 0 ].sCmn.allow_bandwidth_s
witch; |
| 119 encStatus->inWBmodeWithoutVariableLP = state_Fxx[ 0 ].sCmn.fs_kHz == 16 && s
tate_Fxx[ 0 ].sCmn.sLP.mode == 0; |
| 120 |
| 121 return ret; |
| 122 } |
| 123 |
| 124 |
| 125 /**************************/ |
| 126 /* Encode frame with Silk */ |
| 127 /**************************/ |
| 128 /* Note: if prefillFlag is set, the input must contain 10 ms of audio, irrespect
ive of what */ |
| 129 /* encControl->payloadSize_ms is set to
*/ |
| 130 opus_int silk_Encode( /* O Returns error co
de */ |
| 131 void *encState, /* I/O State
*/ |
| 132 silk_EncControlStruct *encControl, /* I Control status
*/ |
| 133 const opus_int16 *samplesIn, /* I Speech sample in
put vector */ |
| 134 opus_int nSamplesIn, /* I Number of sample
s in input vector */ |
| 135 ec_enc *psRangeEnc, /* I/O Compressor data
structure */ |
| 136 opus_int32 *nBytesOut, /* I/O Number of bytes
in payload (input: Max bytes) */ |
| 137 const opus_int prefillFlag /* I Flag to indicate
prefilling buffers no coding */ |
| 138 ) |
| 139 { |
| 140 opus_int n, i, nBits, flags, tmp_payloadSize_ms = 0, tmp_complexity = 0, r
et = 0; |
| 141 opus_int nSamplesToBuffer, nBlocksOf10ms, nSamplesFromInput = 0; |
| 142 opus_int speech_act_thr_for_switch_Q8; |
| 143 opus_int32 TargetRate_bps, MStargetRates_bps[ 2 ], channelRate_bps, LBRR_sym
bol, sum; |
| 144 silk_encoder *psEnc = ( silk_encoder * )encState; |
| 145 opus_int16 buf[ MAX_FRAME_LENGTH_MS * MAX_API_FS_KHZ ]; |
| 146 opus_int transition, curr_block, tot_blocks; |
| 147 |
| 148 psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded = psEnc->state_Fxx[ 1 ].sCmn.nFram
esEncoded = 0; |
| 149 |
| 150 /* Check values in encoder control structure */ |
| 151 if( ( ret = check_control_input( encControl ) != 0 ) ) { |
| 152 silk_assert( 0 ); |
| 153 return ret; |
| 154 } |
| 155 |
| 156 encControl->switchReady = 0; |
| 157 |
| 158 if( encControl->nChannelsInternal > psEnc->nChannelsInternal ) { |
| 159 /* Mono -> Stereo transition: init state of second channel and stereo st
ate */ |
| 160 ret += silk_init_encoder( &psEnc->state_Fxx[ 1 ] ); |
| 161 silk_memset( psEnc->sStereo.pred_prev_Q13, 0, sizeof( psEnc->sStereo.pre
d_prev_Q13 ) ); |
| 162 silk_memset( psEnc->sStereo.sSide, 0, sizeof( psEnc->sStereo.sSide ) ); |
| 163 psEnc->sStereo.mid_side_amp_Q0[ 0 ] = 0; |
| 164 psEnc->sStereo.mid_side_amp_Q0[ 1 ] = 1; |
| 165 psEnc->sStereo.mid_side_amp_Q0[ 2 ] = 0; |
| 166 psEnc->sStereo.mid_side_amp_Q0[ 3 ] = 1; |
| 167 psEnc->sStereo.width_prev_Q14 = 0; |
| 168 psEnc->sStereo.smth_width_Q14 = SILK_FIX_CONST( 1, 14 ); |
| 169 if( psEnc->nChannelsAPI == 2 ) { |
| 170 silk_memcpy( &psEnc->state_Fxx[ 1 ].sCmn.resampler_state, &psEnc->st
ate_Fxx[ 0 ].sCmn.resampler_state, sizeof( silk_resampler_state_struct ) ); |
| 171 silk_memcpy( &psEnc->state_Fxx[ 1 ].sCmn.In_HP_State, &psEnc->st
ate_Fxx[ 0 ].sCmn.In_HP_State, sizeof( psEnc->state_Fxx[ 1 ].sCmn.In_HP_Stat
e ) ); |
| 172 } |
| 173 } |
| 174 |
| 175 transition = (encControl->payloadSize_ms != psEnc->state_Fxx[ 0 ].sCmn.Packe
tSize_ms) || (psEnc->nChannelsInternal != encControl->nChannelsInternal); |
| 176 |
| 177 psEnc->nChannelsAPI = encControl->nChannelsAPI; |
| 178 psEnc->nChannelsInternal = encControl->nChannelsInternal; |
| 179 |
| 180 nBlocksOf10ms = silk_DIV32( 100 * nSamplesIn, encControl->API_sampleRate ); |
| 181 tot_blocks = ( nBlocksOf10ms > 1 ) ? nBlocksOf10ms >> 1 : 1; |
| 182 curr_block = 0; |
| 183 if( prefillFlag ) { |
| 184 /* Only accept input length of 10 ms */ |
| 185 if( nBlocksOf10ms != 1 ) { |
| 186 ret = SILK_ENC_INPUT_INVALID_NO_OF_SAMPLES; |
| 187 silk_assert( 0 ); |
| 188 return ret; |
| 189 } |
| 190 /* Reset Encoder */ |
| 191 for( n = 0; n < encControl->nChannelsInternal; n++ ) { |
| 192 if( (ret = silk_init_encoder( &psEnc->state_Fxx[ n ] ) ) != 0 ) { |
| 193 silk_assert( 0 ); |
| 194 } |
| 195 } |
| 196 tmp_payloadSize_ms = encControl->payloadSize_ms; |
| 197 encControl->payloadSize_ms = 10; |
| 198 tmp_complexity = encControl->complexity; |
| 199 encControl->complexity = 0; |
| 200 for( n = 0; n < encControl->nChannelsInternal; n++ ) { |
| 201 psEnc->state_Fxx[ n ].sCmn.controlled_since_last_payload = 0; |
| 202 psEnc->state_Fxx[ n ].sCmn.prefillFlag = 1; |
| 203 } |
| 204 } else { |
| 205 /* Only accept input lengths that are a multiple of 10 ms */ |
| 206 if( nBlocksOf10ms * encControl->API_sampleRate != 100 * nSamplesIn || nS
amplesIn < 0 ) { |
| 207 ret = SILK_ENC_INPUT_INVALID_NO_OF_SAMPLES; |
| 208 silk_assert( 0 ); |
| 209 return ret; |
| 210 } |
| 211 /* Make sure no more than one packet can be produced */ |
| 212 if( 1000 * (opus_int32)nSamplesIn > encControl->payloadSize_ms * encCont
rol->API_sampleRate ) { |
| 213 ret = SILK_ENC_INPUT_INVALID_NO_OF_SAMPLES; |
| 214 silk_assert( 0 ); |
| 215 return ret; |
| 216 } |
| 217 } |
| 218 |
| 219 TargetRate_bps = silk_RSHIFT32( encControl->bitRate, encControl->nChannelsIn
ternal - 1 ); |
| 220 for( n = 0; n < encControl->nChannelsInternal; n++ ) { |
| 221 /* Force the side channel to the same rate as the mid */ |
| 222 opus_int force_fs_kHz = (n==1) ? psEnc->state_Fxx[0].sCmn.fs_kHz : 0; |
| 223 if( ( ret = silk_control_encoder( &psEnc->state_Fxx[ n ], encControl, Ta
rgetRate_bps, psEnc->allowBandwidthSwitch, n, force_fs_kHz ) ) != 0 ) { |
| 224 silk_assert( 0 ); |
| 225 return ret; |
| 226 } |
| 227 if( psEnc->state_Fxx[n].sCmn.first_frame_after_reset || transition ) { |
| 228 for( i = 0; i < psEnc->state_Fxx[ 0 ].sCmn.nFramesPerPacket; i++ ) { |
| 229 psEnc->state_Fxx[ n ].sCmn.LBRR_flags[ i ] = 0; |
| 230 } |
| 231 } |
| 232 psEnc->state_Fxx[ n ].sCmn.inDTX = psEnc->state_Fxx[ n ].sCmn.useDTX; |
| 233 } |
| 234 silk_assert( encControl->nChannelsInternal == 1 || psEnc->state_Fxx[ 0 ].sCm
n.fs_kHz == psEnc->state_Fxx[ 1 ].sCmn.fs_kHz ); |
| 235 |
| 236 /* Input buffering/resampling and encoding */ |
| 237 while( 1 ) { |
| 238 nSamplesToBuffer = psEnc->state_Fxx[ 0 ].sCmn.frame_length - psEnc->sta
te_Fxx[ 0 ].sCmn.inputBufIx; |
| 239 nSamplesToBuffer = silk_min( nSamplesToBuffer, 10 * nBlocksOf10ms * psE
nc->state_Fxx[ 0 ].sCmn.fs_kHz ); |
| 240 nSamplesFromInput = silk_DIV32_16( nSamplesToBuffer * psEnc->state_Fxx[
0 ].sCmn.API_fs_Hz, psEnc->state_Fxx[ 0 ].sCmn.fs_kHz * 1000 ); |
| 241 /* Resample and write to buffer */ |
| 242 if( encControl->nChannelsAPI == 2 && encControl->nChannelsInternal == 2
) { |
| 243 opus_int id = psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded; |
| 244 for( n = 0; n < nSamplesFromInput; n++ ) { |
| 245 buf[ n ] = samplesIn[ 2 * n ]; |
| 246 } |
| 247 /* Making sure to start both resamplers from the same state when swi
tching from mono to stereo */ |
| 248 if( psEnc->nPrevChannelsInternal == 1 && id==0 ) { |
| 249 silk_memcpy( &psEnc->state_Fxx[ 1 ].sCmn.resampler_state, &psEnc-
>state_Fxx[ 0 ].sCmn.resampler_state, sizeof(psEnc->state_Fxx[ 1 ].sCmn.resample
r_state)); |
| 250 } |
| 251 |
| 252 ret += silk_resampler( &psEnc->state_Fxx[ 0 ].sCmn.resampler_state, |
| 253 &psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ psEnc->state_Fxx[ 0 ].sCmn
.inputBufIx + 2 ], buf, nSamplesFromInput ); |
| 254 psEnc->state_Fxx[ 0 ].sCmn.inputBufIx += nSamplesToBuffer; |
| 255 |
| 256 nSamplesToBuffer = psEnc->state_Fxx[ 1 ].sCmn.frame_length - psEnc-
>state_Fxx[ 1 ].sCmn.inputBufIx; |
| 257 nSamplesToBuffer = silk_min( nSamplesToBuffer, 10 * nBlocksOf10ms *
psEnc->state_Fxx[ 1 ].sCmn.fs_kHz ); |
| 258 for( n = 0; n < nSamplesFromInput; n++ ) { |
| 259 buf[ n ] = samplesIn[ 2 * n + 1 ]; |
| 260 } |
| 261 ret += silk_resampler( &psEnc->state_Fxx[ 1 ].sCmn.resampler_state, |
| 262 &psEnc->state_Fxx[ 1 ].sCmn.inputBuf[ psEnc->state_Fxx[ 1 ].sCmn
.inputBufIx + 2 ], buf, nSamplesFromInput ); |
| 263 |
| 264 psEnc->state_Fxx[ 1 ].sCmn.inputBufIx += nSamplesToBuffer; |
| 265 } else if( encControl->nChannelsAPI == 2 && encControl->nChannelsInterna
l == 1 ) { |
| 266 /* Combine left and right channels before resampling */ |
| 267 for( n = 0; n < nSamplesFromInput; n++ ) { |
| 268 sum = samplesIn[ 2 * n ] + samplesIn[ 2 * n + 1 ]; |
| 269 buf[ n ] = (opus_int16)silk_RSHIFT_ROUND( sum, 1 ); |
| 270 } |
| 271 ret += silk_resampler( &psEnc->state_Fxx[ 0 ].sCmn.resampler_state, |
| 272 &psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ psEnc->state_Fxx[ 0 ].sCmn
.inputBufIx + 2 ], buf, nSamplesFromInput ); |
| 273 /* On the first mono frame, average the results for the two resample
r states */ |
| 274 if( psEnc->nPrevChannelsInternal == 2 && psEnc->state_Fxx[ 0 ].sCmn.
nFramesEncoded == 0 ) { |
| 275 ret += silk_resampler( &psEnc->state_Fxx[ 1 ].sCmn.resampler_stat
e, |
| 276 &psEnc->state_Fxx[ 1 ].sCmn.inputBuf[ psEnc->state_Fxx[ 1 ].s
Cmn.inputBufIx + 2 ], buf, nSamplesFromInput ); |
| 277 for( n = 0; n < psEnc->state_Fxx[ 0 ].sCmn.frame_length; n++ ) { |
| 278 psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ psEnc->state_Fxx[ 0 ].sCm
n.inputBufIx+n+2 ] = |
| 279 silk_RSHIFT(psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ psEnc->
state_Fxx[ 0 ].sCmn.inputBufIx+n+2 ] |
| 280 + psEnc->state_Fxx[ 1 ].sCmn.inputBuf[ psEnc->
state_Fxx[ 1 ].sCmn.inputBufIx+n+2 ], 1); |
| 281 } |
| 282 } |
| 283 psEnc->state_Fxx[ 0 ].sCmn.inputBufIx += nSamplesToBuffer; |
| 284 } else { |
| 285 silk_assert( encControl->nChannelsAPI == 1 && encControl->nChannelsI
nternal == 1 ); |
| 286 silk_memcpy(buf, samplesIn, nSamplesFromInput*sizeof(opus_int16)); |
| 287 ret += silk_resampler( &psEnc->state_Fxx[ 0 ].sCmn.resampler_state, |
| 288 &psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ psEnc->state_Fxx[ 0 ].sCmn
.inputBufIx + 2 ], buf, nSamplesFromInput ); |
| 289 psEnc->state_Fxx[ 0 ].sCmn.inputBufIx += nSamplesToBuffer; |
| 290 } |
| 291 |
| 292 samplesIn += nSamplesFromInput * encControl->nChannelsAPI; |
| 293 nSamplesIn -= nSamplesFromInput; |
| 294 |
| 295 /* Default */ |
| 296 psEnc->allowBandwidthSwitch = 0; |
| 297 |
| 298 /* Silk encoder */ |
| 299 if( psEnc->state_Fxx[ 0 ].sCmn.inputBufIx >= psEnc->state_Fxx[ 0 ].sCmn.
frame_length ) { |
| 300 /* Enough data in input buffer, so encode */ |
| 301 silk_assert( psEnc->state_Fxx[ 0 ].sCmn.inputBufIx == psEnc->state_F
xx[ 0 ].sCmn.frame_length ); |
| 302 silk_assert( encControl->nChannelsInternal == 1 || psEnc->state_Fxx[
1 ].sCmn.inputBufIx == psEnc->state_Fxx[ 1 ].sCmn.frame_length ); |
| 303 |
| 304 /* Deal with LBRR data */ |
| 305 if( psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded == 0 && !prefillFlag )
{ |
| 306 /* Create space at start of payload for VAD and FEC flags */ |
| 307 opus_uint8 iCDF[ 2 ] = { 0, 0 }; |
| 308 iCDF[ 0 ] = 256 - silk_RSHIFT( 256, ( psEnc->state_Fxx[ 0 ].sCmn
.nFramesPerPacket + 1 ) * encControl->nChannelsInternal ); |
| 309 ec_enc_icdf( psRangeEnc, 0, iCDF, 8 ); |
| 310 |
| 311 /* Encode any LBRR data from previous packet */ |
| 312 /* Encode LBRR flags */ |
| 313 for( n = 0; n < encControl->nChannelsInternal; n++ ) { |
| 314 LBRR_symbol = 0; |
| 315 for( i = 0; i < psEnc->state_Fxx[ n ].sCmn.nFramesPerPacket;
i++ ) { |
| 316 LBRR_symbol |= silk_LSHIFT( psEnc->state_Fxx[ n ].sCmn.L
BRR_flags[ i ], i ); |
| 317 } |
| 318 psEnc->state_Fxx[ n ].sCmn.LBRR_flag = LBRR_symbol > 0 ? 1 :
0; |
| 319 if( LBRR_symbol && psEnc->state_Fxx[ n ].sCmn.nFramesPerPack
et > 1 ) { |
| 320 ec_enc_icdf( psRangeEnc, LBRR_symbol - 1, silk_LBRR_flag
s_iCDF_ptr[ psEnc->state_Fxx[ n ].sCmn.nFramesPerPacket - 2 ], 8 ); |
| 321 } |
| 322 } |
| 323 |
| 324 /* Code LBRR indices and excitation signals */ |
| 325 for( i = 0; i < psEnc->state_Fxx[ 0 ].sCmn.nFramesPerPacket; i++
) { |
| 326 for( n = 0; n < encControl->nChannelsInternal; n++ ) { |
| 327 if( psEnc->state_Fxx[ n ].sCmn.LBRR_flags[ i ] ) { |
| 328 opus_int condCoding; |
| 329 |
| 330 if( encControl->nChannelsInternal == 2 && n == 0 ) { |
| 331 silk_stereo_encode_pred( psRangeEnc, psEnc->sSte
reo.predIx[ i ] ); |
| 332 /* For LBRR data there's no need to code the mid
-only flag if the side-channel LBRR flag is set */ |
| 333 if( psEnc->state_Fxx[ 1 ].sCmn.LBRR_flags[ i ] =
= 0 ) { |
| 334 silk_stereo_encode_mid_only( psRangeEnc, psE
nc->sStereo.mid_only_flags[ i ] ); |
| 335 } |
| 336 } |
| 337 /* Use conditional coding if previous frame availabl
e */ |
| 338 if( i > 0 && psEnc->state_Fxx[ n ].sCmn.LBRR_flags[
i - 1 ] ) { |
| 339 condCoding = CODE_CONDITIONALLY; |
| 340 } else { |
| 341 condCoding = CODE_INDEPENDENTLY; |
| 342 } |
| 343 silk_encode_indices( &psEnc->state_Fxx[ n ].sCmn, ps
RangeEnc, i, 1, condCoding ); |
| 344 silk_encode_pulses( psRangeEnc, psEnc->state_Fxx[ n
].sCmn.indices_LBRR[i].signalType, psEnc->state_Fxx[ n ].sCmn.indices_LBRR[i].qu
antOffsetType, |
| 345 psEnc->state_Fxx[ n ].sCmn.pulses_LBRR[ i ], psE
nc->state_Fxx[ n ].sCmn.frame_length ); |
| 346 } |
| 347 } |
| 348 } |
| 349 |
| 350 /* Reset LBRR flags */ |
| 351 for( n = 0; n < encControl->nChannelsInternal; n++ ) { |
| 352 silk_memset( psEnc->state_Fxx[ n ].sCmn.LBRR_flags, 0, sizeo
f( psEnc->state_Fxx[ n ].sCmn.LBRR_flags ) ); |
| 353 } |
| 354 } |
| 355 |
| 356 silk_HP_variable_cutoff( psEnc->state_Fxx ); |
| 357 |
| 358 /* Total target bits for packet */ |
| 359 nBits = silk_DIV32_16( silk_MUL( encControl->bitRate, encControl->pa
yloadSize_ms ), 1000 ); |
| 360 /* Subtract half of the bits already used */ |
| 361 if( !prefillFlag ) { |
| 362 nBits -= ec_tell( psRangeEnc ) >> 1; |
| 363 } |
| 364 /* Divide by number of uncoded frames left in packet */ |
| 365 nBits = silk_DIV32_16( nBits, psEnc->state_Fxx[ 0 ].sCmn.nFramesPerP
acket - psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ); |
| 366 /* Convert to bits/second */ |
| 367 if( encControl->payloadSize_ms == 10 ) { |
| 368 TargetRate_bps = silk_SMULBB( nBits, 100 ); |
| 369 } else { |
| 370 TargetRate_bps = silk_SMULBB( nBits, 50 ); |
| 371 } |
| 372 /* Subtract fraction of bits in excess of target in previous packets
*/ |
| 373 TargetRate_bps -= silk_DIV32_16( silk_MUL( psEnc->nBitsExceeded, 100
0 ), BITRESERVOIR_DECAY_TIME_MS ); |
| 374 /* Never exceed input bitrate */ |
| 375 TargetRate_bps = silk_LIMIT( TargetRate_bps, encControl->bitRate, 50
00 ); |
| 376 |
| 377 /* Convert Left/Right to Mid/Side */ |
| 378 if( encControl->nChannelsInternal == 2 ) { |
| 379 silk_stereo_LR_to_MS( &psEnc->sStereo, &psEnc->state_Fxx[ 0 ].sC
mn.inputBuf[ 2 ], &psEnc->state_Fxx[ 1 ].sCmn.inputBuf[ 2 ], |
| 380 psEnc->sStereo.predIx[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEnc
oded ], &psEnc->sStereo.mid_only_flags[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncode
d ], |
| 381 MStargetRates_bps, TargetRate_bps, psEnc->state_Fxx[ 0 ].sCm
n.speech_activity_Q8, encControl->toMono, |
| 382 psEnc->state_Fxx[ 0 ].sCmn.fs_kHz, psEnc->state_Fxx[ 0 ].sCm
n.frame_length ); |
| 383 if( psEnc->sStereo.mid_only_flags[ psEnc->state_Fxx[ 0 ].sCmn.nF
ramesEncoded ] == 0 ) { |
| 384 /* Reset side channel encoder memory for first frame with si
de coding */ |
| 385 if( psEnc->prev_decode_only_middle == 1 ) { |
| 386 silk_memset( &psEnc->state_Fxx[ 1 ].sShape,
0, sizeof( psEnc->state_Fxx[ 1 ].sShape ) ); |
| 387 silk_memset( &psEnc->state_Fxx[ 1 ].sPrefilt,
0, sizeof( psEnc->state_Fxx[ 1 ].sPrefilt ) ); |
| 388 silk_memset( &psEnc->state_Fxx[ 1 ].sCmn.sNSQ,
0, sizeof( psEnc->state_Fxx[ 1 ].sCmn.sNSQ ) ); |
| 389 silk_memset( psEnc->state_Fxx[ 1 ].sCmn.prev_NLSFq_Q15,
0, sizeof( psEnc->state_Fxx[ 1 ].sCmn.prev_NLSFq_Q15 ) ); |
| 390 silk_memset( &psEnc->state_Fxx[ 1 ].sCmn.sLP.In_LP_State
, 0, sizeof( psEnc->state_Fxx[ 1 ].sCmn.sLP.In_LP_State ) ); |
| 391 psEnc->state_Fxx[ 1 ].sCmn.prevLag = 100
; |
| 392 psEnc->state_Fxx[ 1 ].sCmn.sNSQ.lagPrev = 100
; |
| 393 psEnc->state_Fxx[ 1 ].sShape.LastGainIndex = 10; |
| 394 psEnc->state_Fxx[ 1 ].sCmn.prevSignalType = TYP
E_NO_VOICE_ACTIVITY; |
| 395 psEnc->state_Fxx[ 1 ].sCmn.sNSQ.prev_gain_Q16 = 655
36; |
| 396 psEnc->state_Fxx[ 1 ].sCmn.first_frame_after_reset = 1; |
| 397 } |
| 398 silk_encode_do_VAD_Fxx( &psEnc->state_Fxx[ 1 ] ); |
| 399 } else { |
| 400 psEnc->state_Fxx[ 1 ].sCmn.VAD_flags[ psEnc->state_Fxx[ 0 ].
sCmn.nFramesEncoded ] = 0; |
| 401 } |
| 402 if( !prefillFlag ) { |
| 403 silk_stereo_encode_pred( psRangeEnc, psEnc->sStereo.predIx[
psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ] ); |
| 404 if( psEnc->state_Fxx[ 1 ].sCmn.VAD_flags[ psEnc->state_Fxx[
0 ].sCmn.nFramesEncoded ] == 0 ) { |
| 405 silk_stereo_encode_mid_only( psRangeEnc, psEnc->sStereo.
mid_only_flags[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ] ); |
| 406 } |
| 407 } |
| 408 } else { |
| 409 /* Buffering */ |
| 410 silk_memcpy( psEnc->state_Fxx[ 0 ].sCmn.inputBuf, psEnc->sStereo
.sMid, 2 * sizeof( opus_int16 ) ); |
| 411 silk_memcpy( psEnc->sStereo.sMid, &psEnc->state_Fxx[ 0 ].sCmn.in
putBuf[ psEnc->state_Fxx[ 0 ].sCmn.frame_length ], 2 * sizeof( opus_int16 ) ); |
| 412 } |
| 413 silk_encode_do_VAD_Fxx( &psEnc->state_Fxx[ 0 ] ); |
| 414 |
| 415 /* Encode */ |
| 416 for( n = 0; n < encControl->nChannelsInternal; n++ ) { |
| 417 opus_int maxBits, useCBR; |
| 418 |
| 419 /* Handling rate constraints */ |
| 420 maxBits = encControl->maxBits; |
| 421 if( tot_blocks == 2 && curr_block == 0 ) { |
| 422 maxBits = maxBits * 3 / 5; |
| 423 } else if( tot_blocks == 3 ) { |
| 424 if( curr_block == 0 ) { |
| 425 maxBits = maxBits * 2 / 5; |
| 426 } else if( curr_block == 1 ) { |
| 427 maxBits = maxBits * 3 / 4; |
| 428 } |
| 429 } |
| 430 useCBR = encControl->useCBR && curr_block == tot_blocks - 1; |
| 431 |
| 432 if( encControl->nChannelsInternal == 1 ) { |
| 433 channelRate_bps = TargetRate_bps; |
| 434 } else { |
| 435 channelRate_bps = MStargetRates_bps[ n ]; |
| 436 if( n == 0 && MStargetRates_bps[ 1 ] > 0 ) { |
| 437 useCBR = 0; |
| 438 /* Give mid up to 1/2 of the max bits for that frame */ |
| 439 maxBits -= encControl->maxBits / ( tot_blocks * 2 ); |
| 440 } |
| 441 } |
| 442 |
| 443 if( channelRate_bps > 0 ) { |
| 444 opus_int condCoding; |
| 445 |
| 446 silk_control_SNR( &psEnc->state_Fxx[ n ].sCmn, channelRate_b
ps ); |
| 447 |
| 448 /* Use independent coding if no previous frame available */ |
| 449 if( psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded - n <= 0 ) { |
| 450 condCoding = CODE_INDEPENDENTLY; |
| 451 } else if( n > 0 && psEnc->prev_decode_only_middle ) { |
| 452 /* If we skipped a side frame in this packet, we don't |
| 453 need LTP scaling; the LTP state is well-defined. */ |
| 454 condCoding = CODE_INDEPENDENTLY_NO_LTP_SCALING; |
| 455 } else { |
| 456 condCoding = CODE_CONDITIONALLY; |
| 457 } |
| 458 if( ( ret = silk_encode_frame_Fxx( &psEnc->state_Fxx[ n ], n
BytesOut, psRangeEnc, condCoding, maxBits, useCBR ) ) != 0 ) { |
| 459 silk_assert( 0 ); |
| 460 } |
| 461 } |
| 462 psEnc->state_Fxx[ n ].sCmn.controlled_since_last_payload = 0; |
| 463 psEnc->state_Fxx[ n ].sCmn.inputBufIx = 0; |
| 464 psEnc->state_Fxx[ n ].sCmn.nFramesEncoded++; |
| 465 } |
| 466 psEnc->prev_decode_only_middle = psEnc->sStereo.mid_only_flags[ psEn
c->state_Fxx[ 0 ].sCmn.nFramesEncoded - 1 ]; |
| 467 |
| 468 /* Insert VAD and FEC flags at beginning of bitstream */ |
| 469 if( *nBytesOut > 0 && psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded == p
sEnc->state_Fxx[ 0 ].sCmn.nFramesPerPacket) { |
| 470 flags = 0; |
| 471 for( n = 0; n < encControl->nChannelsInternal; n++ ) { |
| 472 for( i = 0; i < psEnc->state_Fxx[ n ].sCmn.nFramesPerPacket;
i++ ) { |
| 473 flags = silk_LSHIFT( flags, 1 ); |
| 474 flags |= psEnc->state_Fxx[ n ].sCmn.VAD_flags[ i ]; |
| 475 } |
| 476 flags = silk_LSHIFT( flags, 1 ); |
| 477 flags |= psEnc->state_Fxx[ n ].sCmn.LBRR_flag; |
| 478 } |
| 479 if( !prefillFlag ) { |
| 480 ec_enc_patch_initial_bits( psRangeEnc, flags, ( psEnc->state
_Fxx[ 0 ].sCmn.nFramesPerPacket + 1 ) * encControl->nChannelsInternal ); |
| 481 } |
| 482 |
| 483 /* Return zero bytes if all channels DTXed */ |
| 484 if( psEnc->state_Fxx[ 0 ].sCmn.inDTX && ( encControl->nChannelsI
nternal == 1 || psEnc->state_Fxx[ 1 ].sCmn.inDTX ) ) { |
| 485 *nBytesOut = 0; |
| 486 } |
| 487 |
| 488 psEnc->nBitsExceeded += *nBytesOut * 8; |
| 489 psEnc->nBitsExceeded -= silk_DIV32_16( silk_MUL( encControl->bit
Rate, encControl->payloadSize_ms ), 1000 ); |
| 490 psEnc->nBitsExceeded = silk_LIMIT( psEnc->nBitsExceeded, 0, 100
00 ); |
| 491 |
| 492 /* Update flag indicating if bandwidth switching is allowed */ |
| 493 speech_act_thr_for_switch_Q8 = silk_SMLAWB( SILK_FIX_CONST( SPEE
CH_ACTIVITY_DTX_THRES, 8 ), |
| 494 SILK_FIX_CONST( ( 1 - SPEECH_ACTIVITY_DTX_THRES ) / MAX_BAND
WIDTH_SWITCH_DELAY_MS, 16 + 8 ), psEnc->timeSinceSwitchAllowed_ms ); |
| 495 if( psEnc->state_Fxx[ 0 ].sCmn.speech_activity_Q8 < speech_act_t
hr_for_switch_Q8 ) { |
| 496 psEnc->allowBandwidthSwitch = 1; |
| 497 psEnc->timeSinceSwitchAllowed_ms = 0; |
| 498 } else { |
| 499 psEnc->allowBandwidthSwitch = 0; |
| 500 psEnc->timeSinceSwitchAllowed_ms += encControl->payloadSize_
ms; |
| 501 } |
| 502 } |
| 503 |
| 504 if( nSamplesIn == 0 ) { |
| 505 break; |
| 506 } |
| 507 } else { |
| 508 break; |
| 509 } |
| 510 curr_block++; |
| 511 } |
| 512 |
| 513 psEnc->nPrevChannelsInternal = encControl->nChannelsInternal; |
| 514 |
| 515 encControl->allowBandwidthSwitch = psEnc->allowBandwidthSwitch; |
| 516 encControl->inWBmodeWithoutVariableLP = psEnc->state_Fxx[ 0 ].sCmn.fs_kHz ==
16 && psEnc->state_Fxx[ 0 ].sCmn.sLP.mode == 0; |
| 517 encControl->internalSampleRate = silk_SMULBB( psEnc->state_Fxx[ 0 ].sCmn.fs_
kHz, 1000 ); |
| 518 encControl->stereoWidth_Q14 = encControl->toMono ? 0 : psEnc->sStereo.smth_w
idth_Q14; |
| 519 if( prefillFlag ) { |
| 520 encControl->payloadSize_ms = tmp_payloadSize_ms; |
| 521 encControl->complexity = tmp_complexity; |
| 522 for( n = 0; n < encControl->nChannelsInternal; n++ ) { |
| 523 psEnc->state_Fxx[ n ].sCmn.controlled_since_last_payload = 0; |
| 524 psEnc->state_Fxx[ n ].sCmn.prefillFlag = 0; |
| 525 } |
| 526 } |
| 527 |
| 528 return ret; |
| 529 } |
| 530 |
| OLD | NEW |