| OLD | NEW |
| 1 /*********************************************************************** | 1 /*********************************************************************** |
| 2 Copyright (c) 2006-2011, Skype Limited. All rights reserved. | 2 Copyright (c) 2006-2011, Skype Limited. All rights reserved. |
| 3 Redistribution and use in source and binary forms, with or without | 3 Redistribution and use in source and binary forms, with or without |
| 4 modification, are permitted provided that the following conditions | 4 modification, are permitted provided that the following conditions |
| 5 are met: | 5 are met: |
| 6 - Redistributions of source code must retain the above copyright notice, | 6 - Redistributions of source code must retain the above copyright notice, |
| 7 this list of conditions and the following disclaimer. | 7 this list of conditions and the following disclaimer. |
| 8 - Redistributions in binary form must reproduce the above copyright | 8 - Redistributions in binary form must reproduce the above copyright |
| 9 notice, this list of conditions and the following disclaimer in the | 9 notice, this list of conditions and the following disclaimer in the |
| 10 documentation and/or other materials provided with the distribution. | 10 documentation and/or other materials provided with the distribution. |
| 11 - Neither the name of Internet Society, IETF or IETF Trust, nor the | 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 | 12 names of specific contributors, may be used to endorse or promote |
| 13 products derived from this software without specific prior written | 13 products derived from this software without specific prior written |
| 14 permission. | 14 permission. |
| 15 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” | 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 | 16 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 17 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | 17 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 18 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | 18 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 19 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | 19 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 20 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | 20 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 21 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | 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 | 22 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 23 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | 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 | 24 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 25 POSSIBILITY OF SUCH DAMAGE. | 25 POSSIBILITY OF SUCH DAMAGE. |
| 26 ***********************************************************************/ | 26 ***********************************************************************/ |
| 27 | 27 |
| 28 #ifdef HAVE_CONFIG_H | 28 #ifdef HAVE_CONFIG_H |
| 29 #include "config.h" | 29 #include "config.h" |
| 30 #endif | 30 #endif |
| 31 #include "define.h" | 31 #include "define.h" |
| 32 #include "API.h" | 32 #include "API.h" |
| 33 #include "control.h" | 33 #include "control.h" |
| 34 #include "typedef.h" | 34 #include "typedef.h" |
| 35 #include "stack_alloc.h" |
| 35 #include "structs.h" | 36 #include "structs.h" |
| 36 #include "tuning_parameters.h" | 37 #include "tuning_parameters.h" |
| 37 #ifdef FIXED_POINT | 38 #ifdef FIXED_POINT |
| 38 #include "main_FIX.h" | 39 #include "main_FIX.h" |
| 39 #else | 40 #else |
| 40 #include "main_FLP.h" | 41 #include "main_FLP.h" |
| 41 #endif | 42 #endif |
| 42 | 43 |
| 43 /***************************************/ | 44 /***************************************/ |
| 44 /* Read control structure from encoder */ | 45 /* Read control structure from encoder */ |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 139 void *encState, /* I/O State
*/ | 140 void *encState, /* I/O State
*/ |
| 140 silk_EncControlStruct *encControl, /* I Control status
*/ | 141 silk_EncControlStruct *encControl, /* I Control status
*/ |
| 141 const opus_int16 *samplesIn, /* I Speech sample in
put vector */ | 142 const opus_int16 *samplesIn, /* I Speech sample in
put vector */ |
| 142 opus_int nSamplesIn, /* I Number of sample
s in input vector */ | 143 opus_int nSamplesIn, /* I Number of sample
s in input vector */ |
| 143 ec_enc *psRangeEnc, /* I/O Compressor data
structure */ | 144 ec_enc *psRangeEnc, /* I/O Compressor data
structure */ |
| 144 opus_int32 *nBytesOut, /* I/O Number of bytes
in payload (input: Max bytes) */ | 145 opus_int32 *nBytesOut, /* I/O Number of bytes
in payload (input: Max bytes) */ |
| 145 const opus_int prefillFlag /* I Flag to indicate
prefilling buffers no coding */ | 146 const opus_int prefillFlag /* I Flag to indicate
prefilling buffers no coding */ |
| 146 ) | 147 ) |
| 147 { | 148 { |
| 148 opus_int n, i, nBits, flags, tmp_payloadSize_ms = 0, tmp_complexity = 0, r
et = 0; | 149 opus_int n, i, nBits, flags, tmp_payloadSize_ms = 0, tmp_complexity = 0, r
et = 0; |
| 149 opus_int nSamplesToBuffer, nBlocksOf10ms, nSamplesFromInput = 0; | 150 opus_int nSamplesToBuffer, nSamplesToBufferMax, nBlocksOf10ms; |
| 151 opus_int nSamplesFromInput = 0, nSamplesFromInputMax; |
| 150 opus_int speech_act_thr_for_switch_Q8; | 152 opus_int speech_act_thr_for_switch_Q8; |
| 151 opus_int32 TargetRate_bps, MStargetRates_bps[ 2 ], channelRate_bps, LBRR_sym
bol, sum; | 153 opus_int32 TargetRate_bps, MStargetRates_bps[ 2 ], channelRate_bps, LBRR_sym
bol, sum; |
| 152 silk_encoder *psEnc = ( silk_encoder * )encState; | 154 silk_encoder *psEnc = ( silk_encoder * )encState; |
| 153 opus_int16 buf[ MAX_FRAME_LENGTH_MS * MAX_API_FS_KHZ ]; | 155 VARDECL( opus_int16, buf ); |
| 154 opus_int transition, curr_block, tot_blocks; | 156 opus_int transition, curr_block, tot_blocks; |
| 157 SAVE_STACK; |
| 155 | 158 |
| 156 psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded = psEnc->state_Fxx[ 1 ].sCmn.nFram
esEncoded = 0; | 159 psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded = psEnc->state_Fxx[ 1 ].sCmn.nFram
esEncoded = 0; |
| 157 | 160 |
| 158 /* Check values in encoder control structure */ | 161 /* Check values in encoder control structure */ |
| 159 if( ( ret = check_control_input( encControl ) != 0 ) ) { | 162 if( ( ret = check_control_input( encControl ) != 0 ) ) { |
| 160 silk_assert( 0 ); | 163 silk_assert( 0 ); |
| 164 RESTORE_STACK; |
| 161 return ret; | 165 return ret; |
| 162 } | 166 } |
| 163 | 167 |
| 164 encControl->switchReady = 0; | 168 encControl->switchReady = 0; |
| 165 | 169 |
| 166 if( encControl->nChannelsInternal > psEnc->nChannelsInternal ) { | 170 if( encControl->nChannelsInternal > psEnc->nChannelsInternal ) { |
| 167 /* Mono -> Stereo transition: init state of second channel and stereo st
ate */ | 171 /* Mono -> Stereo transition: init state of second channel and stereo st
ate */ |
| 168 ret += silk_init_encoder( &psEnc->state_Fxx[ 1 ] ); | 172 ret += silk_init_encoder( &psEnc->state_Fxx[ 1 ] ); |
| 169 silk_memset( psEnc->sStereo.pred_prev_Q13, 0, sizeof( psEnc->sStereo.pre
d_prev_Q13 ) ); | 173 silk_memset( psEnc->sStereo.pred_prev_Q13, 0, sizeof( psEnc->sStereo.pre
d_prev_Q13 ) ); |
| 170 silk_memset( psEnc->sStereo.sSide, 0, sizeof( psEnc->sStereo.sSide ) ); | 174 silk_memset( psEnc->sStereo.sSide, 0, sizeof( psEnc->sStereo.sSide ) ); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 184 | 188 |
| 185 psEnc->nChannelsAPI = encControl->nChannelsAPI; | 189 psEnc->nChannelsAPI = encControl->nChannelsAPI; |
| 186 psEnc->nChannelsInternal = encControl->nChannelsInternal; | 190 psEnc->nChannelsInternal = encControl->nChannelsInternal; |
| 187 | 191 |
| 188 nBlocksOf10ms = silk_DIV32( 100 * nSamplesIn, encControl->API_sampleRate ); | 192 nBlocksOf10ms = silk_DIV32( 100 * nSamplesIn, encControl->API_sampleRate ); |
| 189 tot_blocks = ( nBlocksOf10ms > 1 ) ? nBlocksOf10ms >> 1 : 1; | 193 tot_blocks = ( nBlocksOf10ms > 1 ) ? nBlocksOf10ms >> 1 : 1; |
| 190 curr_block = 0; | 194 curr_block = 0; |
| 191 if( prefillFlag ) { | 195 if( prefillFlag ) { |
| 192 /* Only accept input length of 10 ms */ | 196 /* Only accept input length of 10 ms */ |
| 193 if( nBlocksOf10ms != 1 ) { | 197 if( nBlocksOf10ms != 1 ) { |
| 194 ret = SILK_ENC_INPUT_INVALID_NO_OF_SAMPLES; | |
| 195 silk_assert( 0 ); | 198 silk_assert( 0 ); |
| 196 return ret; | 199 RESTORE_STACK; |
| 200 return SILK_ENC_INPUT_INVALID_NO_OF_SAMPLES; |
| 197 } | 201 } |
| 198 /* Reset Encoder */ | 202 /* Reset Encoder */ |
| 199 for( n = 0; n < encControl->nChannelsInternal; n++ ) { | 203 for( n = 0; n < encControl->nChannelsInternal; n++ ) { |
| 200 if( (ret = silk_init_encoder( &psEnc->state_Fxx[ n ] ) ) != 0 ) { | 204 if( (ret = silk_init_encoder( &psEnc->state_Fxx[ n ] ) ) != 0 ) { |
| 201 silk_assert( 0 ); | 205 silk_assert( 0 ); |
| 202 } | 206 } |
| 203 } | 207 } |
| 204 tmp_payloadSize_ms = encControl->payloadSize_ms; | 208 tmp_payloadSize_ms = encControl->payloadSize_ms; |
| 205 encControl->payloadSize_ms = 10; | 209 encControl->payloadSize_ms = 10; |
| 206 tmp_complexity = encControl->complexity; | 210 tmp_complexity = encControl->complexity; |
| 207 encControl->complexity = 0; | 211 encControl->complexity = 0; |
| 208 for( n = 0; n < encControl->nChannelsInternal; n++ ) { | 212 for( n = 0; n < encControl->nChannelsInternal; n++ ) { |
| 209 psEnc->state_Fxx[ n ].sCmn.controlled_since_last_payload = 0; | 213 psEnc->state_Fxx[ n ].sCmn.controlled_since_last_payload = 0; |
| 210 psEnc->state_Fxx[ n ].sCmn.prefillFlag = 1; | 214 psEnc->state_Fxx[ n ].sCmn.prefillFlag = 1; |
| 211 } | 215 } |
| 212 } else { | 216 } else { |
| 213 /* Only accept input lengths that are a multiple of 10 ms */ | 217 /* Only accept input lengths that are a multiple of 10 ms */ |
| 214 if( nBlocksOf10ms * encControl->API_sampleRate != 100 * nSamplesIn || nS
amplesIn < 0 ) { | 218 if( nBlocksOf10ms * encControl->API_sampleRate != 100 * nSamplesIn || nS
amplesIn < 0 ) { |
| 215 ret = SILK_ENC_INPUT_INVALID_NO_OF_SAMPLES; | |
| 216 silk_assert( 0 ); | 219 silk_assert( 0 ); |
| 217 return ret; | 220 RESTORE_STACK; |
| 221 return SILK_ENC_INPUT_INVALID_NO_OF_SAMPLES; |
| 218 } | 222 } |
| 219 /* Make sure no more than one packet can be produced */ | 223 /* Make sure no more than one packet can be produced */ |
| 220 if( 1000 * (opus_int32)nSamplesIn > encControl->payloadSize_ms * encCont
rol->API_sampleRate ) { | 224 if( 1000 * (opus_int32)nSamplesIn > encControl->payloadSize_ms * encCont
rol->API_sampleRate ) { |
| 221 ret = SILK_ENC_INPUT_INVALID_NO_OF_SAMPLES; | |
| 222 silk_assert( 0 ); | 225 silk_assert( 0 ); |
| 223 return ret; | 226 RESTORE_STACK; |
| 227 return SILK_ENC_INPUT_INVALID_NO_OF_SAMPLES; |
| 224 } | 228 } |
| 225 } | 229 } |
| 226 | 230 |
| 227 TargetRate_bps = silk_RSHIFT32( encControl->bitRate, encControl->nChannelsIn
ternal - 1 ); | 231 TargetRate_bps = silk_RSHIFT32( encControl->bitRate, encControl->nChannelsIn
ternal - 1 ); |
| 228 for( n = 0; n < encControl->nChannelsInternal; n++ ) { | 232 for( n = 0; n < encControl->nChannelsInternal; n++ ) { |
| 229 /* Force the side channel to the same rate as the mid */ | 233 /* Force the side channel to the same rate as the mid */ |
| 230 opus_int force_fs_kHz = (n==1) ? psEnc->state_Fxx[0].sCmn.fs_kHz : 0; | 234 opus_int force_fs_kHz = (n==1) ? psEnc->state_Fxx[0].sCmn.fs_kHz : 0; |
| 231 if( ( ret = silk_control_encoder( &psEnc->state_Fxx[ n ], encControl, Ta
rgetRate_bps, psEnc->allowBandwidthSwitch, n, force_fs_kHz ) ) != 0 ) { | 235 if( ( ret = silk_control_encoder( &psEnc->state_Fxx[ n ], encControl, Ta
rgetRate_bps, psEnc->allowBandwidthSwitch, n, force_fs_kHz ) ) != 0 ) { |
| 232 silk_assert( 0 ); | 236 silk_assert( 0 ); |
| 237 RESTORE_STACK; |
| 233 return ret; | 238 return ret; |
| 234 } | 239 } |
| 235 if( psEnc->state_Fxx[n].sCmn.first_frame_after_reset || transition ) { | 240 if( psEnc->state_Fxx[n].sCmn.first_frame_after_reset || transition ) { |
| 236 for( i = 0; i < psEnc->state_Fxx[ 0 ].sCmn.nFramesPerPacket; i++ ) { | 241 for( i = 0; i < psEnc->state_Fxx[ 0 ].sCmn.nFramesPerPacket; i++ ) { |
| 237 psEnc->state_Fxx[ n ].sCmn.LBRR_flags[ i ] = 0; | 242 psEnc->state_Fxx[ n ].sCmn.LBRR_flags[ i ] = 0; |
| 238 } | 243 } |
| 239 } | 244 } |
| 240 psEnc->state_Fxx[ n ].sCmn.inDTX = psEnc->state_Fxx[ n ].sCmn.useDTX; | 245 psEnc->state_Fxx[ n ].sCmn.inDTX = psEnc->state_Fxx[ n ].sCmn.useDTX; |
| 241 } | 246 } |
| 242 silk_assert( encControl->nChannelsInternal == 1 || psEnc->state_Fxx[ 0 ].sCm
n.fs_kHz == psEnc->state_Fxx[ 1 ].sCmn.fs_kHz ); | 247 silk_assert( encControl->nChannelsInternal == 1 || psEnc->state_Fxx[ 0 ].sCm
n.fs_kHz == psEnc->state_Fxx[ 1 ].sCmn.fs_kHz ); |
| 243 | 248 |
| 244 /* Input buffering/resampling and encoding */ | 249 /* Input buffering/resampling and encoding */ |
| 250 nSamplesToBufferMax = |
| 251 10 * nBlocksOf10ms * psEnc->state_Fxx[ 0 ].sCmn.fs_kHz; |
| 252 nSamplesFromInputMax = |
| 253 silk_DIV32_16( nSamplesToBufferMax * |
| 254 psEnc->state_Fxx[ 0 ].sCmn.API_fs_Hz, |
| 255 psEnc->state_Fxx[ 0 ].sCmn.fs_kHz * 1000 ); |
| 256 ALLOC( buf, nSamplesFromInputMax, opus_int16 ); |
| 245 while( 1 ) { | 257 while( 1 ) { |
| 246 nSamplesToBuffer = psEnc->state_Fxx[ 0 ].sCmn.frame_length - psEnc->sta
te_Fxx[ 0 ].sCmn.inputBufIx; | 258 nSamplesToBuffer = psEnc->state_Fxx[ 0 ].sCmn.frame_length - psEnc->sta
te_Fxx[ 0 ].sCmn.inputBufIx; |
| 247 nSamplesToBuffer = silk_min( nSamplesToBuffer, 10 * nBlocksOf10ms * psE
nc->state_Fxx[ 0 ].sCmn.fs_kHz ); | 259 nSamplesToBuffer = silk_min( nSamplesToBuffer, nSamplesToBufferMax ); |
| 248 nSamplesFromInput = silk_DIV32_16( nSamplesToBuffer * psEnc->state_Fxx[
0 ].sCmn.API_fs_Hz, psEnc->state_Fxx[ 0 ].sCmn.fs_kHz * 1000 ); | 260 nSamplesFromInput = silk_DIV32_16( nSamplesToBuffer * psEnc->state_Fxx[
0 ].sCmn.API_fs_Hz, psEnc->state_Fxx[ 0 ].sCmn.fs_kHz * 1000 ); |
| 249 /* Resample and write to buffer */ | 261 /* Resample and write to buffer */ |
| 250 if( encControl->nChannelsAPI == 2 && encControl->nChannelsInternal == 2
) { | 262 if( encControl->nChannelsAPI == 2 && encControl->nChannelsInternal == 2
) { |
| 251 opus_int id = psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded; | 263 opus_int id = psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded; |
| 252 for( n = 0; n < nSamplesFromInput; n++ ) { | 264 for( n = 0; n < nSamplesFromInput; n++ ) { |
| 253 buf[ n ] = samplesIn[ 2 * n ]; | 265 buf[ n ] = samplesIn[ 2 * n ]; |
| 254 } | 266 } |
| 255 /* Making sure to start both resamplers from the same state when swi
tching from mono to stereo */ | 267 /* Making sure to start both resamplers from the same state when swi
tching from mono to stereo */ |
| 256 if( psEnc->nPrevChannelsInternal == 1 && id==0 ) { | 268 if( psEnc->nPrevChannelsInternal == 1 && id==0 ) { |
| 257 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)); | 269 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)); |
| (...skipping 268 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 526 encControl->stereoWidth_Q14 = encControl->toMono ? 0 : psEnc->sStereo.smth_w
idth_Q14; | 538 encControl->stereoWidth_Q14 = encControl->toMono ? 0 : psEnc->sStereo.smth_w
idth_Q14; |
| 527 if( prefillFlag ) { | 539 if( prefillFlag ) { |
| 528 encControl->payloadSize_ms = tmp_payloadSize_ms; | 540 encControl->payloadSize_ms = tmp_payloadSize_ms; |
| 529 encControl->complexity = tmp_complexity; | 541 encControl->complexity = tmp_complexity; |
| 530 for( n = 0; n < encControl->nChannelsInternal; n++ ) { | 542 for( n = 0; n < encControl->nChannelsInternal; n++ ) { |
| 531 psEnc->state_Fxx[ n ].sCmn.controlled_since_last_payload = 0; | 543 psEnc->state_Fxx[ n ].sCmn.controlled_since_last_payload = 0; |
| 532 psEnc->state_Fxx[ n ].sCmn.prefillFlag = 0; | 544 psEnc->state_Fxx[ n ].sCmn.prefillFlag = 0; |
| 533 } | 545 } |
| 534 } | 546 } |
| 535 | 547 |
| 548 RESTORE_STACK; |
| 536 return ret; | 549 return ret; |
| 537 } | 550 } |
| 538 | 551 |
| OLD | NEW |