Chris@69: /*********************************************************************** Chris@69: Copyright (c) 2006-2011, Skype Limited. All rights reserved. Chris@69: Redistribution and use in source and binary forms, with or without Chris@69: modification, are permitted provided that the following conditions Chris@69: are met: Chris@69: - Redistributions of source code must retain the above copyright notice, Chris@69: this list of conditions and the following disclaimer. Chris@69: - Redistributions in binary form must reproduce the above copyright Chris@69: notice, this list of conditions and the following disclaimer in the Chris@69: documentation and/or other materials provided with the distribution. Chris@69: - Neither the name of Internet Society, IETF or IETF Trust, nor the Chris@69: names of specific contributors, may be used to endorse or promote Chris@69: products derived from this software without specific prior written Chris@69: permission. Chris@69: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" Chris@69: AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE Chris@69: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE Chris@69: ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE Chris@69: LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR Chris@69: CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF Chris@69: SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS Chris@69: INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN Chris@69: CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) Chris@69: ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE Chris@69: POSSIBILITY OF SUCH DAMAGE. Chris@69: ***********************************************************************/ Chris@69: Chris@69: #ifdef HAVE_CONFIG_H Chris@69: #include "config.h" Chris@69: #endif Chris@69: #include "define.h" Chris@69: #include "API.h" Chris@69: #include "control.h" Chris@69: #include "typedef.h" Chris@69: #include "stack_alloc.h" Chris@69: #include "structs.h" Chris@69: #include "tuning_parameters.h" Chris@69: #ifdef FIXED_POINT Chris@69: #include "main_FIX.h" Chris@69: #else Chris@69: #include "main_FLP.h" Chris@69: #endif Chris@69: Chris@69: /***************************************/ Chris@69: /* Read control structure from encoder */ Chris@69: /***************************************/ Chris@69: static opus_int silk_QueryEncoder( /* O Returns error code */ Chris@69: const void *encState, /* I State */ Chris@69: silk_EncControlStruct *encStatus /* O Encoder Status */ Chris@69: ); Chris@69: Chris@69: /****************************************/ Chris@69: /* Encoder functions */ Chris@69: /****************************************/ Chris@69: Chris@69: opus_int silk_Get_Encoder_Size( /* O Returns error code */ Chris@69: opus_int *encSizeBytes /* O Number of bytes in SILK encoder state */ Chris@69: ) Chris@69: { Chris@69: opus_int ret = SILK_NO_ERROR; Chris@69: Chris@69: *encSizeBytes = sizeof( silk_encoder ); Chris@69: Chris@69: return ret; Chris@69: } Chris@69: Chris@69: /*************************/ Chris@69: /* Init or Reset encoder */ Chris@69: /*************************/ Chris@69: opus_int silk_InitEncoder( /* O Returns error code */ Chris@69: void *encState, /* I/O State */ Chris@69: int arch, /* I Run-time architecture */ Chris@69: silk_EncControlStruct *encStatus /* O Encoder Status */ Chris@69: ) Chris@69: { Chris@69: silk_encoder *psEnc; Chris@69: opus_int n, ret = SILK_NO_ERROR; Chris@69: Chris@69: psEnc = (silk_encoder *)encState; Chris@69: Chris@69: /* Reset encoder */ Chris@69: silk_memset( psEnc, 0, sizeof( silk_encoder ) ); Chris@69: for( n = 0; n < ENCODER_NUM_CHANNELS; n++ ) { Chris@69: if( ret += silk_init_encoder( &psEnc->state_Fxx[ n ], arch ) ) { Chris@69: celt_assert( 0 ); Chris@69: } Chris@69: } Chris@69: Chris@69: psEnc->nChannelsAPI = 1; Chris@69: psEnc->nChannelsInternal = 1; Chris@69: Chris@69: /* Read control structure */ Chris@69: if( ret += silk_QueryEncoder( encState, encStatus ) ) { Chris@69: celt_assert( 0 ); Chris@69: } Chris@69: Chris@69: return ret; Chris@69: } Chris@69: Chris@69: /***************************************/ Chris@69: /* Read control structure from encoder */ Chris@69: /***************************************/ Chris@69: static opus_int silk_QueryEncoder( /* O Returns error code */ Chris@69: const void *encState, /* I State */ Chris@69: silk_EncControlStruct *encStatus /* O Encoder Status */ Chris@69: ) Chris@69: { Chris@69: opus_int ret = SILK_NO_ERROR; Chris@69: silk_encoder_state_Fxx *state_Fxx; Chris@69: silk_encoder *psEnc = (silk_encoder *)encState; Chris@69: Chris@69: state_Fxx = psEnc->state_Fxx; Chris@69: Chris@69: encStatus->nChannelsAPI = psEnc->nChannelsAPI; Chris@69: encStatus->nChannelsInternal = psEnc->nChannelsInternal; Chris@69: encStatus->API_sampleRate = state_Fxx[ 0 ].sCmn.API_fs_Hz; Chris@69: encStatus->maxInternalSampleRate = state_Fxx[ 0 ].sCmn.maxInternal_fs_Hz; Chris@69: encStatus->minInternalSampleRate = state_Fxx[ 0 ].sCmn.minInternal_fs_Hz; Chris@69: encStatus->desiredInternalSampleRate = state_Fxx[ 0 ].sCmn.desiredInternal_fs_Hz; Chris@69: encStatus->payloadSize_ms = state_Fxx[ 0 ].sCmn.PacketSize_ms; Chris@69: encStatus->bitRate = state_Fxx[ 0 ].sCmn.TargetRate_bps; Chris@69: encStatus->packetLossPercentage = state_Fxx[ 0 ].sCmn.PacketLoss_perc; Chris@69: encStatus->complexity = state_Fxx[ 0 ].sCmn.Complexity; Chris@69: encStatus->useInBandFEC = state_Fxx[ 0 ].sCmn.useInBandFEC; Chris@69: encStatus->useDTX = state_Fxx[ 0 ].sCmn.useDTX; Chris@69: encStatus->useCBR = state_Fxx[ 0 ].sCmn.useCBR; Chris@69: encStatus->internalSampleRate = silk_SMULBB( state_Fxx[ 0 ].sCmn.fs_kHz, 1000 ); Chris@69: encStatus->allowBandwidthSwitch = state_Fxx[ 0 ].sCmn.allow_bandwidth_switch; Chris@69: encStatus->inWBmodeWithoutVariableLP = state_Fxx[ 0 ].sCmn.fs_kHz == 16 && state_Fxx[ 0 ].sCmn.sLP.mode == 0; Chris@69: Chris@69: return ret; Chris@69: } Chris@69: Chris@69: Chris@69: /**************************/ Chris@69: /* Encode frame with Silk */ Chris@69: /**************************/ Chris@69: /* Note: if prefillFlag is set, the input must contain 10 ms of audio, irrespective of what */ Chris@69: /* encControl->payloadSize_ms is set to */ Chris@69: opus_int silk_Encode( /* O Returns error code */ Chris@69: void *encState, /* I/O State */ Chris@69: silk_EncControlStruct *encControl, /* I Control status */ Chris@69: const opus_int16 *samplesIn, /* I Speech sample input vector */ Chris@69: opus_int nSamplesIn, /* I Number of samples in input vector */ Chris@69: ec_enc *psRangeEnc, /* I/O Compressor data structure */ Chris@69: opus_int32 *nBytesOut, /* I/O Number of bytes in payload (input: Max bytes) */ Chris@69: const opus_int prefillFlag, /* I Flag to indicate prefilling buffers no coding */ Chris@69: opus_int activity /* I Decision of Opus voice activity detector */ Chris@69: ) Chris@69: { Chris@69: opus_int n, i, nBits, flags, tmp_payloadSize_ms = 0, tmp_complexity = 0, ret = 0; Chris@69: opus_int nSamplesToBuffer, nSamplesToBufferMax, nBlocksOf10ms; Chris@69: opus_int nSamplesFromInput = 0, nSamplesFromInputMax; Chris@69: opus_int speech_act_thr_for_switch_Q8; Chris@69: opus_int32 TargetRate_bps, MStargetRates_bps[ 2 ], channelRate_bps, LBRR_symbol, sum; Chris@69: silk_encoder *psEnc = ( silk_encoder * )encState; Chris@69: VARDECL( opus_int16, buf ); Chris@69: opus_int transition, curr_block, tot_blocks; Chris@69: SAVE_STACK; Chris@69: Chris@69: if (encControl->reducedDependency) Chris@69: { Chris@69: psEnc->state_Fxx[0].sCmn.first_frame_after_reset = 1; Chris@69: psEnc->state_Fxx[1].sCmn.first_frame_after_reset = 1; Chris@69: } Chris@69: psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded = psEnc->state_Fxx[ 1 ].sCmn.nFramesEncoded = 0; Chris@69: Chris@69: /* Check values in encoder control structure */ Chris@69: if( ( ret = check_control_input( encControl ) ) != 0 ) { Chris@69: celt_assert( 0 ); Chris@69: RESTORE_STACK; Chris@69: return ret; Chris@69: } Chris@69: Chris@69: encControl->switchReady = 0; Chris@69: Chris@69: if( encControl->nChannelsInternal > psEnc->nChannelsInternal ) { Chris@69: /* Mono -> Stereo transition: init state of second channel and stereo state */ Chris@69: ret += silk_init_encoder( &psEnc->state_Fxx[ 1 ], psEnc->state_Fxx[ 0 ].sCmn.arch ); Chris@69: silk_memset( psEnc->sStereo.pred_prev_Q13, 0, sizeof( psEnc->sStereo.pred_prev_Q13 ) ); Chris@69: silk_memset( psEnc->sStereo.sSide, 0, sizeof( psEnc->sStereo.sSide ) ); Chris@69: psEnc->sStereo.mid_side_amp_Q0[ 0 ] = 0; Chris@69: psEnc->sStereo.mid_side_amp_Q0[ 1 ] = 1; Chris@69: psEnc->sStereo.mid_side_amp_Q0[ 2 ] = 0; Chris@69: psEnc->sStereo.mid_side_amp_Q0[ 3 ] = 1; Chris@69: psEnc->sStereo.width_prev_Q14 = 0; Chris@69: psEnc->sStereo.smth_width_Q14 = SILK_FIX_CONST( 1, 14 ); Chris@69: if( psEnc->nChannelsAPI == 2 ) { Chris@69: silk_memcpy( &psEnc->state_Fxx[ 1 ].sCmn.resampler_state, &psEnc->state_Fxx[ 0 ].sCmn.resampler_state, sizeof( silk_resampler_state_struct ) ); Chris@69: 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 ) ); Chris@69: } Chris@69: } Chris@69: Chris@69: transition = (encControl->payloadSize_ms != psEnc->state_Fxx[ 0 ].sCmn.PacketSize_ms) || (psEnc->nChannelsInternal != encControl->nChannelsInternal); Chris@69: Chris@69: psEnc->nChannelsAPI = encControl->nChannelsAPI; Chris@69: psEnc->nChannelsInternal = encControl->nChannelsInternal; Chris@69: Chris@69: nBlocksOf10ms = silk_DIV32( 100 * nSamplesIn, encControl->API_sampleRate ); Chris@69: tot_blocks = ( nBlocksOf10ms > 1 ) ? nBlocksOf10ms >> 1 : 1; Chris@69: curr_block = 0; Chris@69: if( prefillFlag ) { Chris@69: silk_LP_state save_LP; Chris@69: /* Only accept input length of 10 ms */ Chris@69: if( nBlocksOf10ms != 1 ) { Chris@69: celt_assert( 0 ); Chris@69: RESTORE_STACK; Chris@69: return SILK_ENC_INPUT_INVALID_NO_OF_SAMPLES; Chris@69: } Chris@69: if ( prefillFlag == 2 ) { Chris@69: save_LP = psEnc->state_Fxx[ 0 ].sCmn.sLP; Chris@69: /* Save the sampling rate so the bandwidth switching code can keep handling transitions. */ Chris@69: save_LP.saved_fs_kHz = psEnc->state_Fxx[ 0 ].sCmn.fs_kHz; Chris@69: } Chris@69: /* Reset Encoder */ Chris@69: for( n = 0; n < encControl->nChannelsInternal; n++ ) { Chris@69: ret = silk_init_encoder( &psEnc->state_Fxx[ n ], psEnc->state_Fxx[ n ].sCmn.arch ); Chris@69: /* Restore the variable LP state. */ Chris@69: if ( prefillFlag == 2 ) { Chris@69: psEnc->state_Fxx[ n ].sCmn.sLP = save_LP; Chris@69: } Chris@69: celt_assert( !ret ); Chris@69: } Chris@69: tmp_payloadSize_ms = encControl->payloadSize_ms; Chris@69: encControl->payloadSize_ms = 10; Chris@69: tmp_complexity = encControl->complexity; Chris@69: encControl->complexity = 0; Chris@69: for( n = 0; n < encControl->nChannelsInternal; n++ ) { Chris@69: psEnc->state_Fxx[ n ].sCmn.controlled_since_last_payload = 0; Chris@69: psEnc->state_Fxx[ n ].sCmn.prefillFlag = 1; Chris@69: } Chris@69: } else { Chris@69: /* Only accept input lengths that are a multiple of 10 ms */ Chris@69: if( nBlocksOf10ms * encControl->API_sampleRate != 100 * nSamplesIn || nSamplesIn < 0 ) { Chris@69: celt_assert( 0 ); Chris@69: RESTORE_STACK; Chris@69: return SILK_ENC_INPUT_INVALID_NO_OF_SAMPLES; Chris@69: } Chris@69: /* Make sure no more than one packet can be produced */ Chris@69: if( 1000 * (opus_int32)nSamplesIn > encControl->payloadSize_ms * encControl->API_sampleRate ) { Chris@69: celt_assert( 0 ); Chris@69: RESTORE_STACK; Chris@69: return SILK_ENC_INPUT_INVALID_NO_OF_SAMPLES; Chris@69: } Chris@69: } Chris@69: Chris@69: for( n = 0; n < encControl->nChannelsInternal; n++ ) { Chris@69: /* Force the side channel to the same rate as the mid */ Chris@69: opus_int force_fs_kHz = (n==1) ? psEnc->state_Fxx[0].sCmn.fs_kHz : 0; Chris@69: if( ( ret = silk_control_encoder( &psEnc->state_Fxx[ n ], encControl, psEnc->allowBandwidthSwitch, n, force_fs_kHz ) ) != 0 ) { Chris@69: silk_assert( 0 ); Chris@69: RESTORE_STACK; Chris@69: return ret; Chris@69: } Chris@69: if( psEnc->state_Fxx[n].sCmn.first_frame_after_reset || transition ) { Chris@69: for( i = 0; i < psEnc->state_Fxx[ 0 ].sCmn.nFramesPerPacket; i++ ) { Chris@69: psEnc->state_Fxx[ n ].sCmn.LBRR_flags[ i ] = 0; Chris@69: } Chris@69: } Chris@69: psEnc->state_Fxx[ n ].sCmn.inDTX = psEnc->state_Fxx[ n ].sCmn.useDTX; Chris@69: } Chris@69: celt_assert( encControl->nChannelsInternal == 1 || psEnc->state_Fxx[ 0 ].sCmn.fs_kHz == psEnc->state_Fxx[ 1 ].sCmn.fs_kHz ); Chris@69: Chris@69: /* Input buffering/resampling and encoding */ Chris@69: nSamplesToBufferMax = Chris@69: 10 * nBlocksOf10ms * psEnc->state_Fxx[ 0 ].sCmn.fs_kHz; Chris@69: nSamplesFromInputMax = Chris@69: silk_DIV32_16( nSamplesToBufferMax * Chris@69: psEnc->state_Fxx[ 0 ].sCmn.API_fs_Hz, Chris@69: psEnc->state_Fxx[ 0 ].sCmn.fs_kHz * 1000 ); Chris@69: ALLOC( buf, nSamplesFromInputMax, opus_int16 ); Chris@69: while( 1 ) { Chris@69: nSamplesToBuffer = psEnc->state_Fxx[ 0 ].sCmn.frame_length - psEnc->state_Fxx[ 0 ].sCmn.inputBufIx; Chris@69: nSamplesToBuffer = silk_min( nSamplesToBuffer, nSamplesToBufferMax ); Chris@69: nSamplesFromInput = silk_DIV32_16( nSamplesToBuffer * psEnc->state_Fxx[ 0 ].sCmn.API_fs_Hz, psEnc->state_Fxx[ 0 ].sCmn.fs_kHz * 1000 ); Chris@69: /* Resample and write to buffer */ Chris@69: if( encControl->nChannelsAPI == 2 && encControl->nChannelsInternal == 2 ) { Chris@69: opus_int id = psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded; Chris@69: for( n = 0; n < nSamplesFromInput; n++ ) { Chris@69: buf[ n ] = samplesIn[ 2 * n ]; Chris@69: } Chris@69: /* Making sure to start both resamplers from the same state when switching from mono to stereo */ Chris@69: if( psEnc->nPrevChannelsInternal == 1 && id==0 ) { Chris@69: silk_memcpy( &psEnc->state_Fxx[ 1 ].sCmn.resampler_state, &psEnc->state_Fxx[ 0 ].sCmn.resampler_state, sizeof(psEnc->state_Fxx[ 1 ].sCmn.resampler_state)); Chris@69: } Chris@69: Chris@69: ret += silk_resampler( &psEnc->state_Fxx[ 0 ].sCmn.resampler_state, Chris@69: &psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ psEnc->state_Fxx[ 0 ].sCmn.inputBufIx + 2 ], buf, nSamplesFromInput ); Chris@69: psEnc->state_Fxx[ 0 ].sCmn.inputBufIx += nSamplesToBuffer; Chris@69: Chris@69: nSamplesToBuffer = psEnc->state_Fxx[ 1 ].sCmn.frame_length - psEnc->state_Fxx[ 1 ].sCmn.inputBufIx; Chris@69: nSamplesToBuffer = silk_min( nSamplesToBuffer, 10 * nBlocksOf10ms * psEnc->state_Fxx[ 1 ].sCmn.fs_kHz ); Chris@69: for( n = 0; n < nSamplesFromInput; n++ ) { Chris@69: buf[ n ] = samplesIn[ 2 * n + 1 ]; Chris@69: } Chris@69: ret += silk_resampler( &psEnc->state_Fxx[ 1 ].sCmn.resampler_state, Chris@69: &psEnc->state_Fxx[ 1 ].sCmn.inputBuf[ psEnc->state_Fxx[ 1 ].sCmn.inputBufIx + 2 ], buf, nSamplesFromInput ); Chris@69: Chris@69: psEnc->state_Fxx[ 1 ].sCmn.inputBufIx += nSamplesToBuffer; Chris@69: } else if( encControl->nChannelsAPI == 2 && encControl->nChannelsInternal == 1 ) { Chris@69: /* Combine left and right channels before resampling */ Chris@69: for( n = 0; n < nSamplesFromInput; n++ ) { Chris@69: sum = samplesIn[ 2 * n ] + samplesIn[ 2 * n + 1 ]; Chris@69: buf[ n ] = (opus_int16)silk_RSHIFT_ROUND( sum, 1 ); Chris@69: } Chris@69: ret += silk_resampler( &psEnc->state_Fxx[ 0 ].sCmn.resampler_state, Chris@69: &psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ psEnc->state_Fxx[ 0 ].sCmn.inputBufIx + 2 ], buf, nSamplesFromInput ); Chris@69: /* On the first mono frame, average the results for the two resampler states */ Chris@69: if( psEnc->nPrevChannelsInternal == 2 && psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded == 0 ) { Chris@69: ret += silk_resampler( &psEnc->state_Fxx[ 1 ].sCmn.resampler_state, Chris@69: &psEnc->state_Fxx[ 1 ].sCmn.inputBuf[ psEnc->state_Fxx[ 1 ].sCmn.inputBufIx + 2 ], buf, nSamplesFromInput ); Chris@69: for( n = 0; n < psEnc->state_Fxx[ 0 ].sCmn.frame_length; n++ ) { Chris@69: psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ psEnc->state_Fxx[ 0 ].sCmn.inputBufIx+n+2 ] = Chris@69: silk_RSHIFT(psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ psEnc->state_Fxx[ 0 ].sCmn.inputBufIx+n+2 ] Chris@69: + psEnc->state_Fxx[ 1 ].sCmn.inputBuf[ psEnc->state_Fxx[ 1 ].sCmn.inputBufIx+n+2 ], 1); Chris@69: } Chris@69: } Chris@69: psEnc->state_Fxx[ 0 ].sCmn.inputBufIx += nSamplesToBuffer; Chris@69: } else { Chris@69: celt_assert( encControl->nChannelsAPI == 1 && encControl->nChannelsInternal == 1 ); Chris@69: silk_memcpy(buf, samplesIn, nSamplesFromInput*sizeof(opus_int16)); Chris@69: ret += silk_resampler( &psEnc->state_Fxx[ 0 ].sCmn.resampler_state, Chris@69: &psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ psEnc->state_Fxx[ 0 ].sCmn.inputBufIx + 2 ], buf, nSamplesFromInput ); Chris@69: psEnc->state_Fxx[ 0 ].sCmn.inputBufIx += nSamplesToBuffer; Chris@69: } Chris@69: Chris@69: samplesIn += nSamplesFromInput * encControl->nChannelsAPI; Chris@69: nSamplesIn -= nSamplesFromInput; Chris@69: Chris@69: /* Default */ Chris@69: psEnc->allowBandwidthSwitch = 0; Chris@69: Chris@69: /* Silk encoder */ Chris@69: if( psEnc->state_Fxx[ 0 ].sCmn.inputBufIx >= psEnc->state_Fxx[ 0 ].sCmn.frame_length ) { Chris@69: /* Enough data in input buffer, so encode */ Chris@69: celt_assert( psEnc->state_Fxx[ 0 ].sCmn.inputBufIx == psEnc->state_Fxx[ 0 ].sCmn.frame_length ); Chris@69: celt_assert( encControl->nChannelsInternal == 1 || psEnc->state_Fxx[ 1 ].sCmn.inputBufIx == psEnc->state_Fxx[ 1 ].sCmn.frame_length ); Chris@69: Chris@69: /* Deal with LBRR data */ Chris@69: if( psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded == 0 && !prefillFlag ) { Chris@69: /* Create space at start of payload for VAD and FEC flags */ Chris@69: opus_uint8 iCDF[ 2 ] = { 0, 0 }; Chris@69: iCDF[ 0 ] = 256 - silk_RSHIFT( 256, ( psEnc->state_Fxx[ 0 ].sCmn.nFramesPerPacket + 1 ) * encControl->nChannelsInternal ); Chris@69: ec_enc_icdf( psRangeEnc, 0, iCDF, 8 ); Chris@69: Chris@69: /* Encode any LBRR data from previous packet */ Chris@69: /* Encode LBRR flags */ Chris@69: for( n = 0; n < encControl->nChannelsInternal; n++ ) { Chris@69: LBRR_symbol = 0; Chris@69: for( i = 0; i < psEnc->state_Fxx[ n ].sCmn.nFramesPerPacket; i++ ) { Chris@69: LBRR_symbol |= silk_LSHIFT( psEnc->state_Fxx[ n ].sCmn.LBRR_flags[ i ], i ); Chris@69: } Chris@69: psEnc->state_Fxx[ n ].sCmn.LBRR_flag = LBRR_symbol > 0 ? 1 : 0; Chris@69: if( LBRR_symbol && psEnc->state_Fxx[ n ].sCmn.nFramesPerPacket > 1 ) { Chris@69: ec_enc_icdf( psRangeEnc, LBRR_symbol - 1, silk_LBRR_flags_iCDF_ptr[ psEnc->state_Fxx[ n ].sCmn.nFramesPerPacket - 2 ], 8 ); Chris@69: } Chris@69: } Chris@69: Chris@69: /* Code LBRR indices and excitation signals */ Chris@69: for( i = 0; i < psEnc->state_Fxx[ 0 ].sCmn.nFramesPerPacket; i++ ) { Chris@69: for( n = 0; n < encControl->nChannelsInternal; n++ ) { Chris@69: if( psEnc->state_Fxx[ n ].sCmn.LBRR_flags[ i ] ) { Chris@69: opus_int condCoding; Chris@69: Chris@69: if( encControl->nChannelsInternal == 2 && n == 0 ) { Chris@69: silk_stereo_encode_pred( psRangeEnc, psEnc->sStereo.predIx[ i ] ); Chris@69: /* For LBRR data there's no need to code the mid-only flag if the side-channel LBRR flag is set */ Chris@69: if( psEnc->state_Fxx[ 1 ].sCmn.LBRR_flags[ i ] == 0 ) { Chris@69: silk_stereo_encode_mid_only( psRangeEnc, psEnc->sStereo.mid_only_flags[ i ] ); Chris@69: } Chris@69: } Chris@69: /* Use conditional coding if previous frame available */ Chris@69: if( i > 0 && psEnc->state_Fxx[ n ].sCmn.LBRR_flags[ i - 1 ] ) { Chris@69: condCoding = CODE_CONDITIONALLY; Chris@69: } else { Chris@69: condCoding = CODE_INDEPENDENTLY; Chris@69: } Chris@69: silk_encode_indices( &psEnc->state_Fxx[ n ].sCmn, psRangeEnc, i, 1, condCoding ); Chris@69: silk_encode_pulses( psRangeEnc, psEnc->state_Fxx[ n ].sCmn.indices_LBRR[i].signalType, psEnc->state_Fxx[ n ].sCmn.indices_LBRR[i].quantOffsetType, Chris@69: psEnc->state_Fxx[ n ].sCmn.pulses_LBRR[ i ], psEnc->state_Fxx[ n ].sCmn.frame_length ); Chris@69: } Chris@69: } Chris@69: } Chris@69: Chris@69: /* Reset LBRR flags */ Chris@69: for( n = 0; n < encControl->nChannelsInternal; n++ ) { Chris@69: silk_memset( psEnc->state_Fxx[ n ].sCmn.LBRR_flags, 0, sizeof( psEnc->state_Fxx[ n ].sCmn.LBRR_flags ) ); Chris@69: } Chris@69: Chris@69: psEnc->nBitsUsedLBRR = ec_tell( psRangeEnc ); Chris@69: } Chris@69: Chris@69: silk_HP_variable_cutoff( psEnc->state_Fxx ); Chris@69: Chris@69: /* Total target bits for packet */ Chris@69: nBits = silk_DIV32_16( silk_MUL( encControl->bitRate, encControl->payloadSize_ms ), 1000 ); Chris@69: /* Subtract bits used for LBRR */ Chris@69: if( !prefillFlag ) { Chris@69: nBits -= psEnc->nBitsUsedLBRR; Chris@69: } Chris@69: /* Divide by number of uncoded frames left in packet */ Chris@69: nBits = silk_DIV32_16( nBits, psEnc->state_Fxx[ 0 ].sCmn.nFramesPerPacket ); Chris@69: /* Convert to bits/second */ Chris@69: if( encControl->payloadSize_ms == 10 ) { Chris@69: TargetRate_bps = silk_SMULBB( nBits, 100 ); Chris@69: } else { Chris@69: TargetRate_bps = silk_SMULBB( nBits, 50 ); Chris@69: } Chris@69: /* Subtract fraction of bits in excess of target in previous frames and packets */ Chris@69: TargetRate_bps -= silk_DIV32_16( silk_MUL( psEnc->nBitsExceeded, 1000 ), BITRESERVOIR_DECAY_TIME_MS ); Chris@69: if( !prefillFlag && psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded > 0 ) { Chris@69: /* Compare actual vs target bits so far in this packet */ Chris@69: opus_int32 bitsBalance = ec_tell( psRangeEnc ) - psEnc->nBitsUsedLBRR - nBits * psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded; Chris@69: TargetRate_bps -= silk_DIV32_16( silk_MUL( bitsBalance, 1000 ), BITRESERVOIR_DECAY_TIME_MS ); Chris@69: } Chris@69: /* Never exceed input bitrate */ Chris@69: TargetRate_bps = silk_LIMIT( TargetRate_bps, encControl->bitRate, 5000 ); Chris@69: Chris@69: /* Convert Left/Right to Mid/Side */ Chris@69: if( encControl->nChannelsInternal == 2 ) { Chris@69: silk_stereo_LR_to_MS( &psEnc->sStereo, &psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ 2 ], &psEnc->state_Fxx[ 1 ].sCmn.inputBuf[ 2 ], Chris@69: psEnc->sStereo.predIx[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ], &psEnc->sStereo.mid_only_flags[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ], Chris@69: MStargetRates_bps, TargetRate_bps, psEnc->state_Fxx[ 0 ].sCmn.speech_activity_Q8, encControl->toMono, Chris@69: psEnc->state_Fxx[ 0 ].sCmn.fs_kHz, psEnc->state_Fxx[ 0 ].sCmn.frame_length ); Chris@69: if( psEnc->sStereo.mid_only_flags[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ] == 0 ) { Chris@69: /* Reset side channel encoder memory for first frame with side coding */ Chris@69: if( psEnc->prev_decode_only_middle == 1 ) { Chris@69: silk_memset( &psEnc->state_Fxx[ 1 ].sShape, 0, sizeof( psEnc->state_Fxx[ 1 ].sShape ) ); Chris@69: silk_memset( &psEnc->state_Fxx[ 1 ].sCmn.sNSQ, 0, sizeof( psEnc->state_Fxx[ 1 ].sCmn.sNSQ ) ); Chris@69: silk_memset( psEnc->state_Fxx[ 1 ].sCmn.prev_NLSFq_Q15, 0, sizeof( psEnc->state_Fxx[ 1 ].sCmn.prev_NLSFq_Q15 ) ); Chris@69: silk_memset( &psEnc->state_Fxx[ 1 ].sCmn.sLP.In_LP_State, 0, sizeof( psEnc->state_Fxx[ 1 ].sCmn.sLP.In_LP_State ) ); Chris@69: psEnc->state_Fxx[ 1 ].sCmn.prevLag = 100; Chris@69: psEnc->state_Fxx[ 1 ].sCmn.sNSQ.lagPrev = 100; Chris@69: psEnc->state_Fxx[ 1 ].sShape.LastGainIndex = 10; Chris@69: psEnc->state_Fxx[ 1 ].sCmn.prevSignalType = TYPE_NO_VOICE_ACTIVITY; Chris@69: psEnc->state_Fxx[ 1 ].sCmn.sNSQ.prev_gain_Q16 = 65536; Chris@69: psEnc->state_Fxx[ 1 ].sCmn.first_frame_after_reset = 1; Chris@69: } Chris@69: silk_encode_do_VAD_Fxx( &psEnc->state_Fxx[ 1 ], activity ); Chris@69: } else { Chris@69: psEnc->state_Fxx[ 1 ].sCmn.VAD_flags[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ] = 0; Chris@69: } Chris@69: if( !prefillFlag ) { Chris@69: silk_stereo_encode_pred( psRangeEnc, psEnc->sStereo.predIx[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ] ); Chris@69: if( psEnc->state_Fxx[ 1 ].sCmn.VAD_flags[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ] == 0 ) { Chris@69: silk_stereo_encode_mid_only( psRangeEnc, psEnc->sStereo.mid_only_flags[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ] ); Chris@69: } Chris@69: } Chris@69: } else { Chris@69: /* Buffering */ Chris@69: silk_memcpy( psEnc->state_Fxx[ 0 ].sCmn.inputBuf, psEnc->sStereo.sMid, 2 * sizeof( opus_int16 ) ); Chris@69: silk_memcpy( psEnc->sStereo.sMid, &psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ psEnc->state_Fxx[ 0 ].sCmn.frame_length ], 2 * sizeof( opus_int16 ) ); Chris@69: } Chris@69: silk_encode_do_VAD_Fxx( &psEnc->state_Fxx[ 0 ], activity ); Chris@69: Chris@69: /* Encode */ Chris@69: for( n = 0; n < encControl->nChannelsInternal; n++ ) { Chris@69: opus_int maxBits, useCBR; Chris@69: Chris@69: /* Handling rate constraints */ Chris@69: maxBits = encControl->maxBits; Chris@69: if( tot_blocks == 2 && curr_block == 0 ) { Chris@69: maxBits = maxBits * 3 / 5; Chris@69: } else if( tot_blocks == 3 ) { Chris@69: if( curr_block == 0 ) { Chris@69: maxBits = maxBits * 2 / 5; Chris@69: } else if( curr_block == 1 ) { Chris@69: maxBits = maxBits * 3 / 4; Chris@69: } Chris@69: } Chris@69: useCBR = encControl->useCBR && curr_block == tot_blocks - 1; Chris@69: Chris@69: if( encControl->nChannelsInternal == 1 ) { Chris@69: channelRate_bps = TargetRate_bps; Chris@69: } else { Chris@69: channelRate_bps = MStargetRates_bps[ n ]; Chris@69: if( n == 0 && MStargetRates_bps[ 1 ] > 0 ) { Chris@69: useCBR = 0; Chris@69: /* Give mid up to 1/2 of the max bits for that frame */ Chris@69: maxBits -= encControl->maxBits / ( tot_blocks * 2 ); Chris@69: } Chris@69: } Chris@69: Chris@69: if( channelRate_bps > 0 ) { Chris@69: opus_int condCoding; Chris@69: Chris@69: silk_control_SNR( &psEnc->state_Fxx[ n ].sCmn, channelRate_bps ); Chris@69: Chris@69: /* Use independent coding if no previous frame available */ Chris@69: if( psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded - n <= 0 ) { Chris@69: condCoding = CODE_INDEPENDENTLY; Chris@69: } else if( n > 0 && psEnc->prev_decode_only_middle ) { Chris@69: /* If we skipped a side frame in this packet, we don't Chris@69: need LTP scaling; the LTP state is well-defined. */ Chris@69: condCoding = CODE_INDEPENDENTLY_NO_LTP_SCALING; Chris@69: } else { Chris@69: condCoding = CODE_CONDITIONALLY; Chris@69: } Chris@69: if( ( ret = silk_encode_frame_Fxx( &psEnc->state_Fxx[ n ], nBytesOut, psRangeEnc, condCoding, maxBits, useCBR ) ) != 0 ) { Chris@69: silk_assert( 0 ); Chris@69: } Chris@69: } Chris@69: psEnc->state_Fxx[ n ].sCmn.controlled_since_last_payload = 0; Chris@69: psEnc->state_Fxx[ n ].sCmn.inputBufIx = 0; Chris@69: psEnc->state_Fxx[ n ].sCmn.nFramesEncoded++; Chris@69: } Chris@69: psEnc->prev_decode_only_middle = psEnc->sStereo.mid_only_flags[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded - 1 ]; Chris@69: Chris@69: /* Insert VAD and FEC flags at beginning of bitstream */ Chris@69: if( *nBytesOut > 0 && psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded == psEnc->state_Fxx[ 0 ].sCmn.nFramesPerPacket) { Chris@69: flags = 0; Chris@69: for( n = 0; n < encControl->nChannelsInternal; n++ ) { Chris@69: for( i = 0; i < psEnc->state_Fxx[ n ].sCmn.nFramesPerPacket; i++ ) { Chris@69: flags = silk_LSHIFT( flags, 1 ); Chris@69: flags |= psEnc->state_Fxx[ n ].sCmn.VAD_flags[ i ]; Chris@69: } Chris@69: flags = silk_LSHIFT( flags, 1 ); Chris@69: flags |= psEnc->state_Fxx[ n ].sCmn.LBRR_flag; Chris@69: } Chris@69: if( !prefillFlag ) { Chris@69: ec_enc_patch_initial_bits( psRangeEnc, flags, ( psEnc->state_Fxx[ 0 ].sCmn.nFramesPerPacket + 1 ) * encControl->nChannelsInternal ); Chris@69: } Chris@69: Chris@69: /* Return zero bytes if all channels DTXed */ Chris@69: if( psEnc->state_Fxx[ 0 ].sCmn.inDTX && ( encControl->nChannelsInternal == 1 || psEnc->state_Fxx[ 1 ].sCmn.inDTX ) ) { Chris@69: *nBytesOut = 0; Chris@69: } Chris@69: Chris@69: psEnc->nBitsExceeded += *nBytesOut * 8; Chris@69: psEnc->nBitsExceeded -= silk_DIV32_16( silk_MUL( encControl->bitRate, encControl->payloadSize_ms ), 1000 ); Chris@69: psEnc->nBitsExceeded = silk_LIMIT( psEnc->nBitsExceeded, 0, 10000 ); Chris@69: Chris@69: /* Update flag indicating if bandwidth switching is allowed */ Chris@69: speech_act_thr_for_switch_Q8 = silk_SMLAWB( SILK_FIX_CONST( SPEECH_ACTIVITY_DTX_THRES, 8 ), Chris@69: SILK_FIX_CONST( ( 1 - SPEECH_ACTIVITY_DTX_THRES ) / MAX_BANDWIDTH_SWITCH_DELAY_MS, 16 + 8 ), psEnc->timeSinceSwitchAllowed_ms ); Chris@69: if( psEnc->state_Fxx[ 0 ].sCmn.speech_activity_Q8 < speech_act_thr_for_switch_Q8 ) { Chris@69: psEnc->allowBandwidthSwitch = 1; Chris@69: psEnc->timeSinceSwitchAllowed_ms = 0; Chris@69: } else { Chris@69: psEnc->allowBandwidthSwitch = 0; Chris@69: psEnc->timeSinceSwitchAllowed_ms += encControl->payloadSize_ms; Chris@69: } Chris@69: } Chris@69: Chris@69: if( nSamplesIn == 0 ) { Chris@69: break; Chris@69: } Chris@69: } else { Chris@69: break; Chris@69: } Chris@69: curr_block++; Chris@69: } Chris@69: Chris@69: psEnc->nPrevChannelsInternal = encControl->nChannelsInternal; Chris@69: Chris@69: encControl->allowBandwidthSwitch = psEnc->allowBandwidthSwitch; Chris@69: encControl->inWBmodeWithoutVariableLP = psEnc->state_Fxx[ 0 ].sCmn.fs_kHz == 16 && psEnc->state_Fxx[ 0 ].sCmn.sLP.mode == 0; Chris@69: encControl->internalSampleRate = silk_SMULBB( psEnc->state_Fxx[ 0 ].sCmn.fs_kHz, 1000 ); Chris@69: encControl->stereoWidth_Q14 = encControl->toMono ? 0 : psEnc->sStereo.smth_width_Q14; Chris@69: if( prefillFlag ) { Chris@69: encControl->payloadSize_ms = tmp_payloadSize_ms; Chris@69: encControl->complexity = tmp_complexity; Chris@69: for( n = 0; n < encControl->nChannelsInternal; n++ ) { Chris@69: psEnc->state_Fxx[ n ].sCmn.controlled_since_last_payload = 0; Chris@69: psEnc->state_Fxx[ n ].sCmn.prefillFlag = 0; Chris@69: } Chris@69: } Chris@69: Chris@69: encControl->signalType = psEnc->state_Fxx[0].sCmn.indices.signalType; Chris@69: encControl->offset = silk_Quantization_Offsets_Q10 Chris@69: [ psEnc->state_Fxx[0].sCmn.indices.signalType >> 1 ] Chris@69: [ psEnc->state_Fxx[0].sCmn.indices.quantOffsetType ]; Chris@69: RESTORE_STACK; Chris@69: return ret; Chris@69: } Chris@69: