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