Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
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 | #include "define.h" |
michael@0 | 32 | #include "API.h" |
michael@0 | 33 | #include "control.h" |
michael@0 | 34 | #include "typedef.h" |
michael@0 | 35 | #include "stack_alloc.h" |
michael@0 | 36 | #include "structs.h" |
michael@0 | 37 | #include "tuning_parameters.h" |
michael@0 | 38 | #ifdef FIXED_POINT |
michael@0 | 39 | #include "main_FIX.h" |
michael@0 | 40 | #else |
michael@0 | 41 | #include "main_FLP.h" |
michael@0 | 42 | #endif |
michael@0 | 43 | |
michael@0 | 44 | /***************************************/ |
michael@0 | 45 | /* Read control structure from encoder */ |
michael@0 | 46 | /***************************************/ |
michael@0 | 47 | static opus_int silk_QueryEncoder( /* O Returns error code */ |
michael@0 | 48 | const void *encState, /* I State */ |
michael@0 | 49 | silk_EncControlStruct *encStatus /* O Encoder Status */ |
michael@0 | 50 | ); |
michael@0 | 51 | |
michael@0 | 52 | /****************************************/ |
michael@0 | 53 | /* Encoder functions */ |
michael@0 | 54 | /****************************************/ |
michael@0 | 55 | |
michael@0 | 56 | opus_int silk_Get_Encoder_Size( /* O Returns error code */ |
michael@0 | 57 | opus_int *encSizeBytes /* O Number of bytes in SILK encoder state */ |
michael@0 | 58 | ) |
michael@0 | 59 | { |
michael@0 | 60 | opus_int ret = SILK_NO_ERROR; |
michael@0 | 61 | |
michael@0 | 62 | *encSizeBytes = sizeof( silk_encoder ); |
michael@0 | 63 | |
michael@0 | 64 | return ret; |
michael@0 | 65 | } |
michael@0 | 66 | |
michael@0 | 67 | /*************************/ |
michael@0 | 68 | /* Init or Reset encoder */ |
michael@0 | 69 | /*************************/ |
michael@0 | 70 | opus_int silk_InitEncoder( /* O Returns error code */ |
michael@0 | 71 | void *encState, /* I/O State */ |
michael@0 | 72 | int arch, /* I Run-time architecture */ |
michael@0 | 73 | silk_EncControlStruct *encStatus /* O Encoder Status */ |
michael@0 | 74 | ) |
michael@0 | 75 | { |
michael@0 | 76 | silk_encoder *psEnc; |
michael@0 | 77 | opus_int n, ret = SILK_NO_ERROR; |
michael@0 | 78 | |
michael@0 | 79 | psEnc = (silk_encoder *)encState; |
michael@0 | 80 | |
michael@0 | 81 | /* Reset encoder */ |
michael@0 | 82 | silk_memset( psEnc, 0, sizeof( silk_encoder ) ); |
michael@0 | 83 | for( n = 0; n < ENCODER_NUM_CHANNELS; n++ ) { |
michael@0 | 84 | if( ret += silk_init_encoder( &psEnc->state_Fxx[ n ], arch ) ) { |
michael@0 | 85 | silk_assert( 0 ); |
michael@0 | 86 | } |
michael@0 | 87 | } |
michael@0 | 88 | |
michael@0 | 89 | psEnc->nChannelsAPI = 1; |
michael@0 | 90 | psEnc->nChannelsInternal = 1; |
michael@0 | 91 | |
michael@0 | 92 | /* Read control structure */ |
michael@0 | 93 | if( ret += silk_QueryEncoder( encState, encStatus ) ) { |
michael@0 | 94 | silk_assert( 0 ); |
michael@0 | 95 | } |
michael@0 | 96 | |
michael@0 | 97 | return ret; |
michael@0 | 98 | } |
michael@0 | 99 | |
michael@0 | 100 | /***************************************/ |
michael@0 | 101 | /* Read control structure from encoder */ |
michael@0 | 102 | /***************************************/ |
michael@0 | 103 | static opus_int silk_QueryEncoder( /* O Returns error code */ |
michael@0 | 104 | const void *encState, /* I State */ |
michael@0 | 105 | silk_EncControlStruct *encStatus /* O Encoder Status */ |
michael@0 | 106 | ) |
michael@0 | 107 | { |
michael@0 | 108 | opus_int ret = SILK_NO_ERROR; |
michael@0 | 109 | silk_encoder_state_Fxx *state_Fxx; |
michael@0 | 110 | silk_encoder *psEnc = (silk_encoder *)encState; |
michael@0 | 111 | |
michael@0 | 112 | state_Fxx = psEnc->state_Fxx; |
michael@0 | 113 | |
michael@0 | 114 | encStatus->nChannelsAPI = psEnc->nChannelsAPI; |
michael@0 | 115 | encStatus->nChannelsInternal = psEnc->nChannelsInternal; |
michael@0 | 116 | encStatus->API_sampleRate = state_Fxx[ 0 ].sCmn.API_fs_Hz; |
michael@0 | 117 | encStatus->maxInternalSampleRate = state_Fxx[ 0 ].sCmn.maxInternal_fs_Hz; |
michael@0 | 118 | encStatus->minInternalSampleRate = state_Fxx[ 0 ].sCmn.minInternal_fs_Hz; |
michael@0 | 119 | encStatus->desiredInternalSampleRate = state_Fxx[ 0 ].sCmn.desiredInternal_fs_Hz; |
michael@0 | 120 | encStatus->payloadSize_ms = state_Fxx[ 0 ].sCmn.PacketSize_ms; |
michael@0 | 121 | encStatus->bitRate = state_Fxx[ 0 ].sCmn.TargetRate_bps; |
michael@0 | 122 | encStatus->packetLossPercentage = state_Fxx[ 0 ].sCmn.PacketLoss_perc; |
michael@0 | 123 | encStatus->complexity = state_Fxx[ 0 ].sCmn.Complexity; |
michael@0 | 124 | encStatus->useInBandFEC = state_Fxx[ 0 ].sCmn.useInBandFEC; |
michael@0 | 125 | encStatus->useDTX = state_Fxx[ 0 ].sCmn.useDTX; |
michael@0 | 126 | encStatus->useCBR = state_Fxx[ 0 ].sCmn.useCBR; |
michael@0 | 127 | encStatus->internalSampleRate = silk_SMULBB( state_Fxx[ 0 ].sCmn.fs_kHz, 1000 ); |
michael@0 | 128 | encStatus->allowBandwidthSwitch = state_Fxx[ 0 ].sCmn.allow_bandwidth_switch; |
michael@0 | 129 | encStatus->inWBmodeWithoutVariableLP = state_Fxx[ 0 ].sCmn.fs_kHz == 16 && state_Fxx[ 0 ].sCmn.sLP.mode == 0; |
michael@0 | 130 | |
michael@0 | 131 | return ret; |
michael@0 | 132 | } |
michael@0 | 133 | |
michael@0 | 134 | |
michael@0 | 135 | /**************************/ |
michael@0 | 136 | /* Encode frame with Silk */ |
michael@0 | 137 | /**************************/ |
michael@0 | 138 | /* Note: if prefillFlag is set, the input must contain 10 ms of audio, irrespective of what */ |
michael@0 | 139 | /* encControl->payloadSize_ms is set to */ |
michael@0 | 140 | opus_int silk_Encode( /* O Returns error code */ |
michael@0 | 141 | void *encState, /* I/O State */ |
michael@0 | 142 | silk_EncControlStruct *encControl, /* I Control status */ |
michael@0 | 143 | const opus_int16 *samplesIn, /* I Speech sample input vector */ |
michael@0 | 144 | opus_int nSamplesIn, /* I Number of samples in input vector */ |
michael@0 | 145 | ec_enc *psRangeEnc, /* I/O Compressor data structure */ |
michael@0 | 146 | opus_int32 *nBytesOut, /* I/O Number of bytes in payload (input: Max bytes) */ |
michael@0 | 147 | const opus_int prefillFlag /* I Flag to indicate prefilling buffers no coding */ |
michael@0 | 148 | ) |
michael@0 | 149 | { |
michael@0 | 150 | opus_int n, i, nBits, flags, tmp_payloadSize_ms = 0, tmp_complexity = 0, ret = 0; |
michael@0 | 151 | opus_int nSamplesToBuffer, nSamplesToBufferMax, nBlocksOf10ms; |
michael@0 | 152 | opus_int nSamplesFromInput = 0, nSamplesFromInputMax; |
michael@0 | 153 | opus_int speech_act_thr_for_switch_Q8; |
michael@0 | 154 | opus_int32 TargetRate_bps, MStargetRates_bps[ 2 ], channelRate_bps, LBRR_symbol, sum; |
michael@0 | 155 | silk_encoder *psEnc = ( silk_encoder * )encState; |
michael@0 | 156 | VARDECL( opus_int16, buf ); |
michael@0 | 157 | opus_int transition, curr_block, tot_blocks; |
michael@0 | 158 | SAVE_STACK; |
michael@0 | 159 | |
michael@0 | 160 | if (encControl->reducedDependency) |
michael@0 | 161 | { |
michael@0 | 162 | psEnc->state_Fxx[0].sCmn.first_frame_after_reset = 1; |
michael@0 | 163 | psEnc->state_Fxx[1].sCmn.first_frame_after_reset = 1; |
michael@0 | 164 | } |
michael@0 | 165 | psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded = psEnc->state_Fxx[ 1 ].sCmn.nFramesEncoded = 0; |
michael@0 | 166 | |
michael@0 | 167 | /* Check values in encoder control structure */ |
michael@0 | 168 | if( ( ret = check_control_input( encControl ) != 0 ) ) { |
michael@0 | 169 | silk_assert( 0 ); |
michael@0 | 170 | RESTORE_STACK; |
michael@0 | 171 | return ret; |
michael@0 | 172 | } |
michael@0 | 173 | |
michael@0 | 174 | encControl->switchReady = 0; |
michael@0 | 175 | |
michael@0 | 176 | if( encControl->nChannelsInternal > psEnc->nChannelsInternal ) { |
michael@0 | 177 | /* Mono -> Stereo transition: init state of second channel and stereo state */ |
michael@0 | 178 | ret += silk_init_encoder( &psEnc->state_Fxx[ 1 ], psEnc->state_Fxx[ 0 ].sCmn.arch ); |
michael@0 | 179 | silk_memset( psEnc->sStereo.pred_prev_Q13, 0, sizeof( psEnc->sStereo.pred_prev_Q13 ) ); |
michael@0 | 180 | silk_memset( psEnc->sStereo.sSide, 0, sizeof( psEnc->sStereo.sSide ) ); |
michael@0 | 181 | psEnc->sStereo.mid_side_amp_Q0[ 0 ] = 0; |
michael@0 | 182 | psEnc->sStereo.mid_side_amp_Q0[ 1 ] = 1; |
michael@0 | 183 | psEnc->sStereo.mid_side_amp_Q0[ 2 ] = 0; |
michael@0 | 184 | psEnc->sStereo.mid_side_amp_Q0[ 3 ] = 1; |
michael@0 | 185 | psEnc->sStereo.width_prev_Q14 = 0; |
michael@0 | 186 | psEnc->sStereo.smth_width_Q14 = SILK_FIX_CONST( 1, 14 ); |
michael@0 | 187 | if( psEnc->nChannelsAPI == 2 ) { |
michael@0 | 188 | silk_memcpy( &psEnc->state_Fxx[ 1 ].sCmn.resampler_state, &psEnc->state_Fxx[ 0 ].sCmn.resampler_state, sizeof( silk_resampler_state_struct ) ); |
michael@0 | 189 | 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 ) ); |
michael@0 | 190 | } |
michael@0 | 191 | } |
michael@0 | 192 | |
michael@0 | 193 | transition = (encControl->payloadSize_ms != psEnc->state_Fxx[ 0 ].sCmn.PacketSize_ms) || (psEnc->nChannelsInternal != encControl->nChannelsInternal); |
michael@0 | 194 | |
michael@0 | 195 | psEnc->nChannelsAPI = encControl->nChannelsAPI; |
michael@0 | 196 | psEnc->nChannelsInternal = encControl->nChannelsInternal; |
michael@0 | 197 | |
michael@0 | 198 | nBlocksOf10ms = silk_DIV32( 100 * nSamplesIn, encControl->API_sampleRate ); |
michael@0 | 199 | tot_blocks = ( nBlocksOf10ms > 1 ) ? nBlocksOf10ms >> 1 : 1; |
michael@0 | 200 | curr_block = 0; |
michael@0 | 201 | if( prefillFlag ) { |
michael@0 | 202 | /* Only accept input length of 10 ms */ |
michael@0 | 203 | if( nBlocksOf10ms != 1 ) { |
michael@0 | 204 | silk_assert( 0 ); |
michael@0 | 205 | RESTORE_STACK; |
michael@0 | 206 | return SILK_ENC_INPUT_INVALID_NO_OF_SAMPLES; |
michael@0 | 207 | } |
michael@0 | 208 | /* Reset Encoder */ |
michael@0 | 209 | for( n = 0; n < encControl->nChannelsInternal; n++ ) { |
michael@0 | 210 | ret = silk_init_encoder( &psEnc->state_Fxx[ n ], psEnc->state_Fxx[ n ].sCmn.arch ); |
michael@0 | 211 | silk_assert( !ret ); |
michael@0 | 212 | } |
michael@0 | 213 | tmp_payloadSize_ms = encControl->payloadSize_ms; |
michael@0 | 214 | encControl->payloadSize_ms = 10; |
michael@0 | 215 | tmp_complexity = encControl->complexity; |
michael@0 | 216 | encControl->complexity = 0; |
michael@0 | 217 | for( n = 0; n < encControl->nChannelsInternal; n++ ) { |
michael@0 | 218 | psEnc->state_Fxx[ n ].sCmn.controlled_since_last_payload = 0; |
michael@0 | 219 | psEnc->state_Fxx[ n ].sCmn.prefillFlag = 1; |
michael@0 | 220 | } |
michael@0 | 221 | } else { |
michael@0 | 222 | /* Only accept input lengths that are a multiple of 10 ms */ |
michael@0 | 223 | if( nBlocksOf10ms * encControl->API_sampleRate != 100 * nSamplesIn || nSamplesIn < 0 ) { |
michael@0 | 224 | silk_assert( 0 ); |
michael@0 | 225 | RESTORE_STACK; |
michael@0 | 226 | return SILK_ENC_INPUT_INVALID_NO_OF_SAMPLES; |
michael@0 | 227 | } |
michael@0 | 228 | /* Make sure no more than one packet can be produced */ |
michael@0 | 229 | if( 1000 * (opus_int32)nSamplesIn > encControl->payloadSize_ms * encControl->API_sampleRate ) { |
michael@0 | 230 | silk_assert( 0 ); |
michael@0 | 231 | RESTORE_STACK; |
michael@0 | 232 | return SILK_ENC_INPUT_INVALID_NO_OF_SAMPLES; |
michael@0 | 233 | } |
michael@0 | 234 | } |
michael@0 | 235 | |
michael@0 | 236 | TargetRate_bps = silk_RSHIFT32( encControl->bitRate, encControl->nChannelsInternal - 1 ); |
michael@0 | 237 | for( n = 0; n < encControl->nChannelsInternal; n++ ) { |
michael@0 | 238 | /* Force the side channel to the same rate as the mid */ |
michael@0 | 239 | opus_int force_fs_kHz = (n==1) ? psEnc->state_Fxx[0].sCmn.fs_kHz : 0; |
michael@0 | 240 | if( ( ret = silk_control_encoder( &psEnc->state_Fxx[ n ], encControl, TargetRate_bps, psEnc->allowBandwidthSwitch, n, force_fs_kHz ) ) != 0 ) { |
michael@0 | 241 | silk_assert( 0 ); |
michael@0 | 242 | RESTORE_STACK; |
michael@0 | 243 | return ret; |
michael@0 | 244 | } |
michael@0 | 245 | if( psEnc->state_Fxx[n].sCmn.first_frame_after_reset || transition ) { |
michael@0 | 246 | for( i = 0; i < psEnc->state_Fxx[ 0 ].sCmn.nFramesPerPacket; i++ ) { |
michael@0 | 247 | psEnc->state_Fxx[ n ].sCmn.LBRR_flags[ i ] = 0; |
michael@0 | 248 | } |
michael@0 | 249 | } |
michael@0 | 250 | psEnc->state_Fxx[ n ].sCmn.inDTX = psEnc->state_Fxx[ n ].sCmn.useDTX; |
michael@0 | 251 | } |
michael@0 | 252 | silk_assert( encControl->nChannelsInternal == 1 || psEnc->state_Fxx[ 0 ].sCmn.fs_kHz == psEnc->state_Fxx[ 1 ].sCmn.fs_kHz ); |
michael@0 | 253 | |
michael@0 | 254 | /* Input buffering/resampling and encoding */ |
michael@0 | 255 | nSamplesToBufferMax = |
michael@0 | 256 | 10 * nBlocksOf10ms * psEnc->state_Fxx[ 0 ].sCmn.fs_kHz; |
michael@0 | 257 | nSamplesFromInputMax = |
michael@0 | 258 | silk_DIV32_16( nSamplesToBufferMax * |
michael@0 | 259 | psEnc->state_Fxx[ 0 ].sCmn.API_fs_Hz, |
michael@0 | 260 | psEnc->state_Fxx[ 0 ].sCmn.fs_kHz * 1000 ); |
michael@0 | 261 | ALLOC( buf, nSamplesFromInputMax, opus_int16 ); |
michael@0 | 262 | while( 1 ) { |
michael@0 | 263 | nSamplesToBuffer = psEnc->state_Fxx[ 0 ].sCmn.frame_length - psEnc->state_Fxx[ 0 ].sCmn.inputBufIx; |
michael@0 | 264 | nSamplesToBuffer = silk_min( nSamplesToBuffer, nSamplesToBufferMax ); |
michael@0 | 265 | nSamplesFromInput = silk_DIV32_16( nSamplesToBuffer * psEnc->state_Fxx[ 0 ].sCmn.API_fs_Hz, psEnc->state_Fxx[ 0 ].sCmn.fs_kHz * 1000 ); |
michael@0 | 266 | /* Resample and write to buffer */ |
michael@0 | 267 | if( encControl->nChannelsAPI == 2 && encControl->nChannelsInternal == 2 ) { |
michael@0 | 268 | opus_int id = psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded; |
michael@0 | 269 | for( n = 0; n < nSamplesFromInput; n++ ) { |
michael@0 | 270 | buf[ n ] = samplesIn[ 2 * n ]; |
michael@0 | 271 | } |
michael@0 | 272 | /* Making sure to start both resamplers from the same state when switching from mono to stereo */ |
michael@0 | 273 | if( psEnc->nPrevChannelsInternal == 1 && id==0 ) { |
michael@0 | 274 | silk_memcpy( &psEnc->state_Fxx[ 1 ].sCmn.resampler_state, &psEnc->state_Fxx[ 0 ].sCmn.resampler_state, sizeof(psEnc->state_Fxx[ 1 ].sCmn.resampler_state)); |
michael@0 | 275 | } |
michael@0 | 276 | |
michael@0 | 277 | ret += silk_resampler( &psEnc->state_Fxx[ 0 ].sCmn.resampler_state, |
michael@0 | 278 | &psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ psEnc->state_Fxx[ 0 ].sCmn.inputBufIx + 2 ], buf, nSamplesFromInput ); |
michael@0 | 279 | psEnc->state_Fxx[ 0 ].sCmn.inputBufIx += nSamplesToBuffer; |
michael@0 | 280 | |
michael@0 | 281 | nSamplesToBuffer = psEnc->state_Fxx[ 1 ].sCmn.frame_length - psEnc->state_Fxx[ 1 ].sCmn.inputBufIx; |
michael@0 | 282 | nSamplesToBuffer = silk_min( nSamplesToBuffer, 10 * nBlocksOf10ms * psEnc->state_Fxx[ 1 ].sCmn.fs_kHz ); |
michael@0 | 283 | for( n = 0; n < nSamplesFromInput; n++ ) { |
michael@0 | 284 | buf[ n ] = samplesIn[ 2 * n + 1 ]; |
michael@0 | 285 | } |
michael@0 | 286 | ret += silk_resampler( &psEnc->state_Fxx[ 1 ].sCmn.resampler_state, |
michael@0 | 287 | &psEnc->state_Fxx[ 1 ].sCmn.inputBuf[ psEnc->state_Fxx[ 1 ].sCmn.inputBufIx + 2 ], buf, nSamplesFromInput ); |
michael@0 | 288 | |
michael@0 | 289 | psEnc->state_Fxx[ 1 ].sCmn.inputBufIx += nSamplesToBuffer; |
michael@0 | 290 | } else if( encControl->nChannelsAPI == 2 && encControl->nChannelsInternal == 1 ) { |
michael@0 | 291 | /* Combine left and right channels before resampling */ |
michael@0 | 292 | for( n = 0; n < nSamplesFromInput; n++ ) { |
michael@0 | 293 | sum = samplesIn[ 2 * n ] + samplesIn[ 2 * n + 1 ]; |
michael@0 | 294 | buf[ n ] = (opus_int16)silk_RSHIFT_ROUND( sum, 1 ); |
michael@0 | 295 | } |
michael@0 | 296 | ret += silk_resampler( &psEnc->state_Fxx[ 0 ].sCmn.resampler_state, |
michael@0 | 297 | &psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ psEnc->state_Fxx[ 0 ].sCmn.inputBufIx + 2 ], buf, nSamplesFromInput ); |
michael@0 | 298 | /* On the first mono frame, average the results for the two resampler states */ |
michael@0 | 299 | if( psEnc->nPrevChannelsInternal == 2 && psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded == 0 ) { |
michael@0 | 300 | ret += silk_resampler( &psEnc->state_Fxx[ 1 ].sCmn.resampler_state, |
michael@0 | 301 | &psEnc->state_Fxx[ 1 ].sCmn.inputBuf[ psEnc->state_Fxx[ 1 ].sCmn.inputBufIx + 2 ], buf, nSamplesFromInput ); |
michael@0 | 302 | for( n = 0; n < psEnc->state_Fxx[ 0 ].sCmn.frame_length; n++ ) { |
michael@0 | 303 | psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ psEnc->state_Fxx[ 0 ].sCmn.inputBufIx+n+2 ] = |
michael@0 | 304 | silk_RSHIFT(psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ psEnc->state_Fxx[ 0 ].sCmn.inputBufIx+n+2 ] |
michael@0 | 305 | + psEnc->state_Fxx[ 1 ].sCmn.inputBuf[ psEnc->state_Fxx[ 1 ].sCmn.inputBufIx+n+2 ], 1); |
michael@0 | 306 | } |
michael@0 | 307 | } |
michael@0 | 308 | psEnc->state_Fxx[ 0 ].sCmn.inputBufIx += nSamplesToBuffer; |
michael@0 | 309 | } else { |
michael@0 | 310 | silk_assert( encControl->nChannelsAPI == 1 && encControl->nChannelsInternal == 1 ); |
michael@0 | 311 | silk_memcpy(buf, samplesIn, nSamplesFromInput*sizeof(opus_int16)); |
michael@0 | 312 | ret += silk_resampler( &psEnc->state_Fxx[ 0 ].sCmn.resampler_state, |
michael@0 | 313 | &psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ psEnc->state_Fxx[ 0 ].sCmn.inputBufIx + 2 ], buf, nSamplesFromInput ); |
michael@0 | 314 | psEnc->state_Fxx[ 0 ].sCmn.inputBufIx += nSamplesToBuffer; |
michael@0 | 315 | } |
michael@0 | 316 | |
michael@0 | 317 | samplesIn += nSamplesFromInput * encControl->nChannelsAPI; |
michael@0 | 318 | nSamplesIn -= nSamplesFromInput; |
michael@0 | 319 | |
michael@0 | 320 | /* Default */ |
michael@0 | 321 | psEnc->allowBandwidthSwitch = 0; |
michael@0 | 322 | |
michael@0 | 323 | /* Silk encoder */ |
michael@0 | 324 | if( psEnc->state_Fxx[ 0 ].sCmn.inputBufIx >= psEnc->state_Fxx[ 0 ].sCmn.frame_length ) { |
michael@0 | 325 | /* Enough data in input buffer, so encode */ |
michael@0 | 326 | silk_assert( psEnc->state_Fxx[ 0 ].sCmn.inputBufIx == psEnc->state_Fxx[ 0 ].sCmn.frame_length ); |
michael@0 | 327 | silk_assert( encControl->nChannelsInternal == 1 || psEnc->state_Fxx[ 1 ].sCmn.inputBufIx == psEnc->state_Fxx[ 1 ].sCmn.frame_length ); |
michael@0 | 328 | |
michael@0 | 329 | /* Deal with LBRR data */ |
michael@0 | 330 | if( psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded == 0 && !prefillFlag ) { |
michael@0 | 331 | /* Create space at start of payload for VAD and FEC flags */ |
michael@0 | 332 | opus_uint8 iCDF[ 2 ] = { 0, 0 }; |
michael@0 | 333 | iCDF[ 0 ] = 256 - silk_RSHIFT( 256, ( psEnc->state_Fxx[ 0 ].sCmn.nFramesPerPacket + 1 ) * encControl->nChannelsInternal ); |
michael@0 | 334 | ec_enc_icdf( psRangeEnc, 0, iCDF, 8 ); |
michael@0 | 335 | |
michael@0 | 336 | /* Encode any LBRR data from previous packet */ |
michael@0 | 337 | /* Encode LBRR flags */ |
michael@0 | 338 | for( n = 0; n < encControl->nChannelsInternal; n++ ) { |
michael@0 | 339 | LBRR_symbol = 0; |
michael@0 | 340 | for( i = 0; i < psEnc->state_Fxx[ n ].sCmn.nFramesPerPacket; i++ ) { |
michael@0 | 341 | LBRR_symbol |= silk_LSHIFT( psEnc->state_Fxx[ n ].sCmn.LBRR_flags[ i ], i ); |
michael@0 | 342 | } |
michael@0 | 343 | psEnc->state_Fxx[ n ].sCmn.LBRR_flag = LBRR_symbol > 0 ? 1 : 0; |
michael@0 | 344 | if( LBRR_symbol && psEnc->state_Fxx[ n ].sCmn.nFramesPerPacket > 1 ) { |
michael@0 | 345 | ec_enc_icdf( psRangeEnc, LBRR_symbol - 1, silk_LBRR_flags_iCDF_ptr[ psEnc->state_Fxx[ n ].sCmn.nFramesPerPacket - 2 ], 8 ); |
michael@0 | 346 | } |
michael@0 | 347 | } |
michael@0 | 348 | |
michael@0 | 349 | /* Code LBRR indices and excitation signals */ |
michael@0 | 350 | for( i = 0; i < psEnc->state_Fxx[ 0 ].sCmn.nFramesPerPacket; i++ ) { |
michael@0 | 351 | for( n = 0; n < encControl->nChannelsInternal; n++ ) { |
michael@0 | 352 | if( psEnc->state_Fxx[ n ].sCmn.LBRR_flags[ i ] ) { |
michael@0 | 353 | opus_int condCoding; |
michael@0 | 354 | |
michael@0 | 355 | if( encControl->nChannelsInternal == 2 && n == 0 ) { |
michael@0 | 356 | silk_stereo_encode_pred( psRangeEnc, psEnc->sStereo.predIx[ i ] ); |
michael@0 | 357 | /* For LBRR data there's no need to code the mid-only flag if the side-channel LBRR flag is set */ |
michael@0 | 358 | if( psEnc->state_Fxx[ 1 ].sCmn.LBRR_flags[ i ] == 0 ) { |
michael@0 | 359 | silk_stereo_encode_mid_only( psRangeEnc, psEnc->sStereo.mid_only_flags[ i ] ); |
michael@0 | 360 | } |
michael@0 | 361 | } |
michael@0 | 362 | /* Use conditional coding if previous frame available */ |
michael@0 | 363 | if( i > 0 && psEnc->state_Fxx[ n ].sCmn.LBRR_flags[ i - 1 ] ) { |
michael@0 | 364 | condCoding = CODE_CONDITIONALLY; |
michael@0 | 365 | } else { |
michael@0 | 366 | condCoding = CODE_INDEPENDENTLY; |
michael@0 | 367 | } |
michael@0 | 368 | silk_encode_indices( &psEnc->state_Fxx[ n ].sCmn, psRangeEnc, i, 1, condCoding ); |
michael@0 | 369 | silk_encode_pulses( psRangeEnc, psEnc->state_Fxx[ n ].sCmn.indices_LBRR[i].signalType, psEnc->state_Fxx[ n ].sCmn.indices_LBRR[i].quantOffsetType, |
michael@0 | 370 | psEnc->state_Fxx[ n ].sCmn.pulses_LBRR[ i ], psEnc->state_Fxx[ n ].sCmn.frame_length ); |
michael@0 | 371 | } |
michael@0 | 372 | } |
michael@0 | 373 | } |
michael@0 | 374 | |
michael@0 | 375 | /* Reset LBRR flags */ |
michael@0 | 376 | for( n = 0; n < encControl->nChannelsInternal; n++ ) { |
michael@0 | 377 | silk_memset( psEnc->state_Fxx[ n ].sCmn.LBRR_flags, 0, sizeof( psEnc->state_Fxx[ n ].sCmn.LBRR_flags ) ); |
michael@0 | 378 | } |
michael@0 | 379 | } |
michael@0 | 380 | |
michael@0 | 381 | silk_HP_variable_cutoff( psEnc->state_Fxx ); |
michael@0 | 382 | |
michael@0 | 383 | /* Total target bits for packet */ |
michael@0 | 384 | nBits = silk_DIV32_16( silk_MUL( encControl->bitRate, encControl->payloadSize_ms ), 1000 ); |
michael@0 | 385 | /* Subtract half of the bits already used */ |
michael@0 | 386 | if( !prefillFlag ) { |
michael@0 | 387 | nBits -= ec_tell( psRangeEnc ) >> 1; |
michael@0 | 388 | } |
michael@0 | 389 | /* Divide by number of uncoded frames left in packet */ |
michael@0 | 390 | nBits = silk_DIV32_16( nBits, psEnc->state_Fxx[ 0 ].sCmn.nFramesPerPacket - psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ); |
michael@0 | 391 | /* Convert to bits/second */ |
michael@0 | 392 | if( encControl->payloadSize_ms == 10 ) { |
michael@0 | 393 | TargetRate_bps = silk_SMULBB( nBits, 100 ); |
michael@0 | 394 | } else { |
michael@0 | 395 | TargetRate_bps = silk_SMULBB( nBits, 50 ); |
michael@0 | 396 | } |
michael@0 | 397 | /* Subtract fraction of bits in excess of target in previous packets */ |
michael@0 | 398 | TargetRate_bps -= silk_DIV32_16( silk_MUL( psEnc->nBitsExceeded, 1000 ), BITRESERVOIR_DECAY_TIME_MS ); |
michael@0 | 399 | /* Never exceed input bitrate */ |
michael@0 | 400 | TargetRate_bps = silk_LIMIT( TargetRate_bps, encControl->bitRate, 5000 ); |
michael@0 | 401 | |
michael@0 | 402 | /* Convert Left/Right to Mid/Side */ |
michael@0 | 403 | if( encControl->nChannelsInternal == 2 ) { |
michael@0 | 404 | silk_stereo_LR_to_MS( &psEnc->sStereo, &psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ 2 ], &psEnc->state_Fxx[ 1 ].sCmn.inputBuf[ 2 ], |
michael@0 | 405 | psEnc->sStereo.predIx[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ], &psEnc->sStereo.mid_only_flags[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ], |
michael@0 | 406 | MStargetRates_bps, TargetRate_bps, psEnc->state_Fxx[ 0 ].sCmn.speech_activity_Q8, encControl->toMono, |
michael@0 | 407 | psEnc->state_Fxx[ 0 ].sCmn.fs_kHz, psEnc->state_Fxx[ 0 ].sCmn.frame_length ); |
michael@0 | 408 | if( psEnc->sStereo.mid_only_flags[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ] == 0 ) { |
michael@0 | 409 | /* Reset side channel encoder memory for first frame with side coding */ |
michael@0 | 410 | if( psEnc->prev_decode_only_middle == 1 ) { |
michael@0 | 411 | silk_memset( &psEnc->state_Fxx[ 1 ].sShape, 0, sizeof( psEnc->state_Fxx[ 1 ].sShape ) ); |
michael@0 | 412 | silk_memset( &psEnc->state_Fxx[ 1 ].sPrefilt, 0, sizeof( psEnc->state_Fxx[ 1 ].sPrefilt ) ); |
michael@0 | 413 | silk_memset( &psEnc->state_Fxx[ 1 ].sCmn.sNSQ, 0, sizeof( psEnc->state_Fxx[ 1 ].sCmn.sNSQ ) ); |
michael@0 | 414 | silk_memset( psEnc->state_Fxx[ 1 ].sCmn.prev_NLSFq_Q15, 0, sizeof( psEnc->state_Fxx[ 1 ].sCmn.prev_NLSFq_Q15 ) ); |
michael@0 | 415 | silk_memset( &psEnc->state_Fxx[ 1 ].sCmn.sLP.In_LP_State, 0, sizeof( psEnc->state_Fxx[ 1 ].sCmn.sLP.In_LP_State ) ); |
michael@0 | 416 | psEnc->state_Fxx[ 1 ].sCmn.prevLag = 100; |
michael@0 | 417 | psEnc->state_Fxx[ 1 ].sCmn.sNSQ.lagPrev = 100; |
michael@0 | 418 | psEnc->state_Fxx[ 1 ].sShape.LastGainIndex = 10; |
michael@0 | 419 | psEnc->state_Fxx[ 1 ].sCmn.prevSignalType = TYPE_NO_VOICE_ACTIVITY; |
michael@0 | 420 | psEnc->state_Fxx[ 1 ].sCmn.sNSQ.prev_gain_Q16 = 65536; |
michael@0 | 421 | psEnc->state_Fxx[ 1 ].sCmn.first_frame_after_reset = 1; |
michael@0 | 422 | } |
michael@0 | 423 | silk_encode_do_VAD_Fxx( &psEnc->state_Fxx[ 1 ] ); |
michael@0 | 424 | } else { |
michael@0 | 425 | psEnc->state_Fxx[ 1 ].sCmn.VAD_flags[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ] = 0; |
michael@0 | 426 | } |
michael@0 | 427 | if( !prefillFlag ) { |
michael@0 | 428 | silk_stereo_encode_pred( psRangeEnc, psEnc->sStereo.predIx[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ] ); |
michael@0 | 429 | if( psEnc->state_Fxx[ 1 ].sCmn.VAD_flags[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ] == 0 ) { |
michael@0 | 430 | silk_stereo_encode_mid_only( psRangeEnc, psEnc->sStereo.mid_only_flags[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ] ); |
michael@0 | 431 | } |
michael@0 | 432 | } |
michael@0 | 433 | } else { |
michael@0 | 434 | /* Buffering */ |
michael@0 | 435 | silk_memcpy( psEnc->state_Fxx[ 0 ].sCmn.inputBuf, psEnc->sStereo.sMid, 2 * sizeof( opus_int16 ) ); |
michael@0 | 436 | silk_memcpy( psEnc->sStereo.sMid, &psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ psEnc->state_Fxx[ 0 ].sCmn.frame_length ], 2 * sizeof( opus_int16 ) ); |
michael@0 | 437 | } |
michael@0 | 438 | silk_encode_do_VAD_Fxx( &psEnc->state_Fxx[ 0 ] ); |
michael@0 | 439 | |
michael@0 | 440 | /* Encode */ |
michael@0 | 441 | for( n = 0; n < encControl->nChannelsInternal; n++ ) { |
michael@0 | 442 | opus_int maxBits, useCBR; |
michael@0 | 443 | |
michael@0 | 444 | /* Handling rate constraints */ |
michael@0 | 445 | maxBits = encControl->maxBits; |
michael@0 | 446 | if( tot_blocks == 2 && curr_block == 0 ) { |
michael@0 | 447 | maxBits = maxBits * 3 / 5; |
michael@0 | 448 | } else if( tot_blocks == 3 ) { |
michael@0 | 449 | if( curr_block == 0 ) { |
michael@0 | 450 | maxBits = maxBits * 2 / 5; |
michael@0 | 451 | } else if( curr_block == 1 ) { |
michael@0 | 452 | maxBits = maxBits * 3 / 4; |
michael@0 | 453 | } |
michael@0 | 454 | } |
michael@0 | 455 | useCBR = encControl->useCBR && curr_block == tot_blocks - 1; |
michael@0 | 456 | |
michael@0 | 457 | if( encControl->nChannelsInternal == 1 ) { |
michael@0 | 458 | channelRate_bps = TargetRate_bps; |
michael@0 | 459 | } else { |
michael@0 | 460 | channelRate_bps = MStargetRates_bps[ n ]; |
michael@0 | 461 | if( n == 0 && MStargetRates_bps[ 1 ] > 0 ) { |
michael@0 | 462 | useCBR = 0; |
michael@0 | 463 | /* Give mid up to 1/2 of the max bits for that frame */ |
michael@0 | 464 | maxBits -= encControl->maxBits / ( tot_blocks * 2 ); |
michael@0 | 465 | } |
michael@0 | 466 | } |
michael@0 | 467 | |
michael@0 | 468 | if( channelRate_bps > 0 ) { |
michael@0 | 469 | opus_int condCoding; |
michael@0 | 470 | |
michael@0 | 471 | silk_control_SNR( &psEnc->state_Fxx[ n ].sCmn, channelRate_bps ); |
michael@0 | 472 | |
michael@0 | 473 | /* Use independent coding if no previous frame available */ |
michael@0 | 474 | if( psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded - n <= 0 ) { |
michael@0 | 475 | condCoding = CODE_INDEPENDENTLY; |
michael@0 | 476 | } else if( n > 0 && psEnc->prev_decode_only_middle ) { |
michael@0 | 477 | /* If we skipped a side frame in this packet, we don't |
michael@0 | 478 | need LTP scaling; the LTP state is well-defined. */ |
michael@0 | 479 | condCoding = CODE_INDEPENDENTLY_NO_LTP_SCALING; |
michael@0 | 480 | } else { |
michael@0 | 481 | condCoding = CODE_CONDITIONALLY; |
michael@0 | 482 | } |
michael@0 | 483 | if( ( ret = silk_encode_frame_Fxx( &psEnc->state_Fxx[ n ], nBytesOut, psRangeEnc, condCoding, maxBits, useCBR ) ) != 0 ) { |
michael@0 | 484 | silk_assert( 0 ); |
michael@0 | 485 | } |
michael@0 | 486 | } |
michael@0 | 487 | psEnc->state_Fxx[ n ].sCmn.controlled_since_last_payload = 0; |
michael@0 | 488 | psEnc->state_Fxx[ n ].sCmn.inputBufIx = 0; |
michael@0 | 489 | psEnc->state_Fxx[ n ].sCmn.nFramesEncoded++; |
michael@0 | 490 | } |
michael@0 | 491 | psEnc->prev_decode_only_middle = psEnc->sStereo.mid_only_flags[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded - 1 ]; |
michael@0 | 492 | |
michael@0 | 493 | /* Insert VAD and FEC flags at beginning of bitstream */ |
michael@0 | 494 | if( *nBytesOut > 0 && psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded == psEnc->state_Fxx[ 0 ].sCmn.nFramesPerPacket) { |
michael@0 | 495 | flags = 0; |
michael@0 | 496 | for( n = 0; n < encControl->nChannelsInternal; n++ ) { |
michael@0 | 497 | for( i = 0; i < psEnc->state_Fxx[ n ].sCmn.nFramesPerPacket; i++ ) { |
michael@0 | 498 | flags = silk_LSHIFT( flags, 1 ); |
michael@0 | 499 | flags |= psEnc->state_Fxx[ n ].sCmn.VAD_flags[ i ]; |
michael@0 | 500 | } |
michael@0 | 501 | flags = silk_LSHIFT( flags, 1 ); |
michael@0 | 502 | flags |= psEnc->state_Fxx[ n ].sCmn.LBRR_flag; |
michael@0 | 503 | } |
michael@0 | 504 | if( !prefillFlag ) { |
michael@0 | 505 | ec_enc_patch_initial_bits( psRangeEnc, flags, ( psEnc->state_Fxx[ 0 ].sCmn.nFramesPerPacket + 1 ) * encControl->nChannelsInternal ); |
michael@0 | 506 | } |
michael@0 | 507 | |
michael@0 | 508 | /* Return zero bytes if all channels DTXed */ |
michael@0 | 509 | if( psEnc->state_Fxx[ 0 ].sCmn.inDTX && ( encControl->nChannelsInternal == 1 || psEnc->state_Fxx[ 1 ].sCmn.inDTX ) ) { |
michael@0 | 510 | *nBytesOut = 0; |
michael@0 | 511 | } |
michael@0 | 512 | |
michael@0 | 513 | psEnc->nBitsExceeded += *nBytesOut * 8; |
michael@0 | 514 | psEnc->nBitsExceeded -= silk_DIV32_16( silk_MUL( encControl->bitRate, encControl->payloadSize_ms ), 1000 ); |
michael@0 | 515 | psEnc->nBitsExceeded = silk_LIMIT( psEnc->nBitsExceeded, 0, 10000 ); |
michael@0 | 516 | |
michael@0 | 517 | /* Update flag indicating if bandwidth switching is allowed */ |
michael@0 | 518 | speech_act_thr_for_switch_Q8 = silk_SMLAWB( SILK_FIX_CONST( SPEECH_ACTIVITY_DTX_THRES, 8 ), |
michael@0 | 519 | SILK_FIX_CONST( ( 1 - SPEECH_ACTIVITY_DTX_THRES ) / MAX_BANDWIDTH_SWITCH_DELAY_MS, 16 + 8 ), psEnc->timeSinceSwitchAllowed_ms ); |
michael@0 | 520 | if( psEnc->state_Fxx[ 0 ].sCmn.speech_activity_Q8 < speech_act_thr_for_switch_Q8 ) { |
michael@0 | 521 | psEnc->allowBandwidthSwitch = 1; |
michael@0 | 522 | psEnc->timeSinceSwitchAllowed_ms = 0; |
michael@0 | 523 | } else { |
michael@0 | 524 | psEnc->allowBandwidthSwitch = 0; |
michael@0 | 525 | psEnc->timeSinceSwitchAllowed_ms += encControl->payloadSize_ms; |
michael@0 | 526 | } |
michael@0 | 527 | } |
michael@0 | 528 | |
michael@0 | 529 | if( nSamplesIn == 0 ) { |
michael@0 | 530 | break; |
michael@0 | 531 | } |
michael@0 | 532 | } else { |
michael@0 | 533 | break; |
michael@0 | 534 | } |
michael@0 | 535 | curr_block++; |
michael@0 | 536 | } |
michael@0 | 537 | |
michael@0 | 538 | psEnc->nPrevChannelsInternal = encControl->nChannelsInternal; |
michael@0 | 539 | |
michael@0 | 540 | encControl->allowBandwidthSwitch = psEnc->allowBandwidthSwitch; |
michael@0 | 541 | encControl->inWBmodeWithoutVariableLP = psEnc->state_Fxx[ 0 ].sCmn.fs_kHz == 16 && psEnc->state_Fxx[ 0 ].sCmn.sLP.mode == 0; |
michael@0 | 542 | encControl->internalSampleRate = silk_SMULBB( psEnc->state_Fxx[ 0 ].sCmn.fs_kHz, 1000 ); |
michael@0 | 543 | encControl->stereoWidth_Q14 = encControl->toMono ? 0 : psEnc->sStereo.smth_width_Q14; |
michael@0 | 544 | if( prefillFlag ) { |
michael@0 | 545 | encControl->payloadSize_ms = tmp_payloadSize_ms; |
michael@0 | 546 | encControl->complexity = tmp_complexity; |
michael@0 | 547 | for( n = 0; n < encControl->nChannelsInternal; n++ ) { |
michael@0 | 548 | psEnc->state_Fxx[ n ].sCmn.controlled_since_last_payload = 0; |
michael@0 | 549 | psEnc->state_Fxx[ n ].sCmn.prefillFlag = 0; |
michael@0 | 550 | } |
michael@0 | 551 | } |
michael@0 | 552 | |
michael@0 | 553 | RESTORE_STACK; |
michael@0 | 554 | return ret; |
michael@0 | 555 | } |
michael@0 | 556 |