Line | Count | Source (jump to first uncovered line) |
1 | | /*********************************************************************** |
2 | | Copyright (c) 2006-2011, Skype Limited. All rights reserved. |
3 | | Redistribution and use in source and binary forms, with or without |
4 | | modification, are permitted provided that the following conditions |
5 | | are met: |
6 | | - Redistributions of source code must retain the above copyright notice, |
7 | | this list of conditions and the following disclaimer. |
8 | | - Redistributions in binary form must reproduce the above copyright |
9 | | notice, this list of conditions and the following disclaimer in the |
10 | | documentation and/or other materials provided with the distribution. |
11 | | - Neither the name of Internet Society, IETF or IETF Trust, nor the |
12 | | names of specific contributors, may be used to endorse or promote |
13 | | products derived from this software without specific prior written |
14 | | permission. |
15 | | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
16 | | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
17 | | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
18 | | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
19 | | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
20 | | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
21 | | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
22 | | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
23 | | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
24 | | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
25 | | POSSIBILITY OF SUCH DAMAGE. |
26 | | ***********************************************************************/ |
27 | | |
28 | | #ifdef HAVE_CONFIG_H |
29 | | #include "config.h" |
30 | | #endif |
31 | | #include "define.h" |
32 | | #include "API.h" |
33 | | #include "control.h" |
34 | | #include "typedef.h" |
35 | | #include "stack_alloc.h" |
36 | | #include "structs.h" |
37 | | #include "tuning_parameters.h" |
38 | | #ifdef FIXED_POINT |
39 | | #include "main_FIX.h" |
40 | | #else |
41 | | #include "main_FLP.h" |
42 | | #endif |
43 | | |
44 | | #ifdef ENABLE_DRED |
45 | | #include "dred_encoder.h" |
46 | | #endif |
47 | | |
48 | | /***************************************/ |
49 | | /* Read control structure from encoder */ |
50 | | /***************************************/ |
51 | | static opus_int silk_QueryEncoder( /* O Returns error code */ |
52 | | const void *encState, /* I State */ |
53 | | silk_EncControlStruct *encStatus /* O Encoder Status */ |
54 | | ); |
55 | | |
56 | | /****************************************/ |
57 | | /* Encoder functions */ |
58 | | /****************************************/ |
59 | | |
60 | | opus_int silk_Get_Encoder_Size( /* O Returns error code */ |
61 | | opus_int *encSizeBytes /* O Number of bytes in SILK encoder state */ |
62 | | ) |
63 | 3.11M | { |
64 | 3.11M | opus_int ret = SILK_NO_ERROR; |
65 | | |
66 | 3.11M | *encSizeBytes = sizeof( silk_encoder ); |
67 | | |
68 | 3.11M | return ret; |
69 | 3.11M | } |
70 | | |
71 | | /*************************/ |
72 | | /* Init or Reset encoder */ |
73 | | /*************************/ |
74 | | opus_int silk_InitEncoder( /* O Returns error code */ |
75 | | void *encState, /* I/O State */ |
76 | | int arch, /* I Run-time architecture */ |
77 | | silk_EncControlStruct *encStatus /* O Encoder Status */ |
78 | | ) |
79 | 512k | { |
80 | 512k | silk_encoder *psEnc; |
81 | 512k | opus_int n, ret = SILK_NO_ERROR; |
82 | | |
83 | 512k | psEnc = (silk_encoder *)encState; |
84 | | |
85 | | /* Reset encoder */ |
86 | 512k | silk_memset( psEnc, 0, sizeof( silk_encoder ) ); |
87 | 1.53M | for( n = 0; n < ENCODER_NUM_CHANNELS; n++ ) { |
88 | 1.02M | if( ret += silk_init_encoder( &psEnc->state_Fxx[ n ], arch ) ) { |
89 | 0 | celt_assert( 0 ); |
90 | 0 | } |
91 | 1.02M | } |
92 | | |
93 | 512k | psEnc->nChannelsAPI = 1; |
94 | 512k | psEnc->nChannelsInternal = 1; |
95 | | |
96 | | /* Read control structure */ |
97 | 512k | if( ret += silk_QueryEncoder( encState, encStatus ) ) { |
98 | 0 | celt_assert( 0 ); |
99 | 0 | } |
100 | | |
101 | 512k | return ret; |
102 | 512k | } |
103 | | |
104 | | /***************************************/ |
105 | | /* Read control structure from encoder */ |
106 | | /***************************************/ |
107 | | static opus_int silk_QueryEncoder( /* O Returns error code */ |
108 | | const void *encState, /* I State */ |
109 | | silk_EncControlStruct *encStatus /* O Encoder Status */ |
110 | | ) |
111 | 1.02M | { |
112 | 1.02M | opus_int ret = SILK_NO_ERROR; |
113 | 1.02M | silk_encoder_state_Fxx *state_Fxx; |
114 | 1.02M | silk_encoder *psEnc = (silk_encoder *)encState; |
115 | | |
116 | 1.02M | state_Fxx = psEnc->state_Fxx; |
117 | | |
118 | 1.02M | encStatus->nChannelsAPI = psEnc->nChannelsAPI; |
119 | 1.02M | encStatus->nChannelsInternal = psEnc->nChannelsInternal; |
120 | 1.02M | encStatus->API_sampleRate = state_Fxx[ 0 ].sCmn.API_fs_Hz; |
121 | 1.02M | encStatus->maxInternalSampleRate = state_Fxx[ 0 ].sCmn.maxInternal_fs_Hz; |
122 | 1.02M | encStatus->minInternalSampleRate = state_Fxx[ 0 ].sCmn.minInternal_fs_Hz; |
123 | 1.02M | encStatus->desiredInternalSampleRate = state_Fxx[ 0 ].sCmn.desiredInternal_fs_Hz; |
124 | 1.02M | encStatus->payloadSize_ms = state_Fxx[ 0 ].sCmn.PacketSize_ms; |
125 | 1.02M | encStatus->bitRate = state_Fxx[ 0 ].sCmn.TargetRate_bps; |
126 | 1.02M | encStatus->packetLossPercentage = state_Fxx[ 0 ].sCmn.PacketLoss_perc; |
127 | 1.02M | encStatus->complexity = state_Fxx[ 0 ].sCmn.Complexity; |
128 | 1.02M | encStatus->useInBandFEC = state_Fxx[ 0 ].sCmn.useInBandFEC; |
129 | 1.02M | encStatus->useDTX = state_Fxx[ 0 ].sCmn.useDTX; |
130 | 1.02M | encStatus->useCBR = state_Fxx[ 0 ].sCmn.useCBR; |
131 | 1.02M | encStatus->internalSampleRate = silk_SMULBB( state_Fxx[ 0 ].sCmn.fs_kHz, 1000 ); |
132 | 1.02M | encStatus->allowBandwidthSwitch = state_Fxx[ 0 ].sCmn.allow_bandwidth_switch; |
133 | 1.02M | encStatus->inWBmodeWithoutVariableLP = state_Fxx[ 0 ].sCmn.fs_kHz == 16 && state_Fxx[ 0 ].sCmn.sLP.mode == 0; |
134 | | |
135 | 1.02M | return ret; |
136 | 1.02M | } enc_API.c:silk_QueryEncoder Line | Count | Source | 111 | 512k | { | 112 | 512k | opus_int ret = SILK_NO_ERROR; | 113 | 512k | silk_encoder_state_Fxx *state_Fxx; | 114 | 512k | silk_encoder *psEnc = (silk_encoder *)encState; | 115 | | | 116 | 512k | state_Fxx = psEnc->state_Fxx; | 117 | | | 118 | 512k | encStatus->nChannelsAPI = psEnc->nChannelsAPI; | 119 | 512k | encStatus->nChannelsInternal = psEnc->nChannelsInternal; | 120 | 512k | encStatus->API_sampleRate = state_Fxx[ 0 ].sCmn.API_fs_Hz; | 121 | 512k | encStatus->maxInternalSampleRate = state_Fxx[ 0 ].sCmn.maxInternal_fs_Hz; | 122 | 512k | encStatus->minInternalSampleRate = state_Fxx[ 0 ].sCmn.minInternal_fs_Hz; | 123 | 512k | encStatus->desiredInternalSampleRate = state_Fxx[ 0 ].sCmn.desiredInternal_fs_Hz; | 124 | 512k | encStatus->payloadSize_ms = state_Fxx[ 0 ].sCmn.PacketSize_ms; | 125 | 512k | encStatus->bitRate = state_Fxx[ 0 ].sCmn.TargetRate_bps; | 126 | 512k | encStatus->packetLossPercentage = state_Fxx[ 0 ].sCmn.PacketLoss_perc; | 127 | 512k | encStatus->complexity = state_Fxx[ 0 ].sCmn.Complexity; | 128 | 512k | encStatus->useInBandFEC = state_Fxx[ 0 ].sCmn.useInBandFEC; | 129 | 512k | encStatus->useDTX = state_Fxx[ 0 ].sCmn.useDTX; | 130 | 512k | encStatus->useCBR = state_Fxx[ 0 ].sCmn.useCBR; | 131 | 512k | encStatus->internalSampleRate = silk_SMULBB( state_Fxx[ 0 ].sCmn.fs_kHz, 1000 ); | 132 | 512k | encStatus->allowBandwidthSwitch = state_Fxx[ 0 ].sCmn.allow_bandwidth_switch; | 133 | 512k | encStatus->inWBmodeWithoutVariableLP = state_Fxx[ 0 ].sCmn.fs_kHz == 16 && state_Fxx[ 0 ].sCmn.sLP.mode == 0; | 134 | | | 135 | 512k | return ret; | 136 | 512k | } |
enc_API.c:silk_QueryEncoder Line | Count | Source | 111 | 512k | { | 112 | 512k | opus_int ret = SILK_NO_ERROR; | 113 | 512k | silk_encoder_state_Fxx *state_Fxx; | 114 | 512k | silk_encoder *psEnc = (silk_encoder *)encState; | 115 | | | 116 | 512k | state_Fxx = psEnc->state_Fxx; | 117 | | | 118 | 512k | encStatus->nChannelsAPI = psEnc->nChannelsAPI; | 119 | 512k | encStatus->nChannelsInternal = psEnc->nChannelsInternal; | 120 | 512k | encStatus->API_sampleRate = state_Fxx[ 0 ].sCmn.API_fs_Hz; | 121 | 512k | encStatus->maxInternalSampleRate = state_Fxx[ 0 ].sCmn.maxInternal_fs_Hz; | 122 | 512k | encStatus->minInternalSampleRate = state_Fxx[ 0 ].sCmn.minInternal_fs_Hz; | 123 | 512k | encStatus->desiredInternalSampleRate = state_Fxx[ 0 ].sCmn.desiredInternal_fs_Hz; | 124 | 512k | encStatus->payloadSize_ms = state_Fxx[ 0 ].sCmn.PacketSize_ms; | 125 | 512k | encStatus->bitRate = state_Fxx[ 0 ].sCmn.TargetRate_bps; | 126 | 512k | encStatus->packetLossPercentage = state_Fxx[ 0 ].sCmn.PacketLoss_perc; | 127 | 512k | encStatus->complexity = state_Fxx[ 0 ].sCmn.Complexity; | 128 | 512k | encStatus->useInBandFEC = state_Fxx[ 0 ].sCmn.useInBandFEC; | 129 | 512k | encStatus->useDTX = state_Fxx[ 0 ].sCmn.useDTX; | 130 | 512k | encStatus->useCBR = state_Fxx[ 0 ].sCmn.useCBR; | 131 | 512k | encStatus->internalSampleRate = silk_SMULBB( state_Fxx[ 0 ].sCmn.fs_kHz, 1000 ); | 132 | 512k | encStatus->allowBandwidthSwitch = state_Fxx[ 0 ].sCmn.allow_bandwidth_switch; | 133 | 512k | encStatus->inWBmodeWithoutVariableLP = state_Fxx[ 0 ].sCmn.fs_kHz == 16 && state_Fxx[ 0 ].sCmn.sLP.mode == 0; | 134 | | | 135 | 512k | return ret; | 136 | 512k | } |
|
137 | | |
138 | | |
139 | | /**************************/ |
140 | | /* Encode frame with Silk */ |
141 | | /**************************/ |
142 | | /* Note: if prefillFlag is set, the input must contain 10 ms of audio, irrespective of what */ |
143 | | /* encControl->payloadSize_ms is set to */ |
144 | | opus_int silk_Encode( /* O Returns error code */ |
145 | | void *encState, /* I/O State */ |
146 | | silk_EncControlStruct *encControl, /* I Control status */ |
147 | | const opus_res *samplesIn, /* I Speech sample input vector */ |
148 | | opus_int nSamplesIn, /* I Number of samples in input vector */ |
149 | | ec_enc *psRangeEnc, /* I/O Compressor data structure */ |
150 | | opus_int32 *nBytesOut, /* I/O Number of bytes in payload (input: Max bytes) */ |
151 | | const opus_int prefillFlag, /* I Flag to indicate prefilling buffers no coding */ |
152 | | opus_int activity /* I Decision of Opus voice activity detector */ |
153 | | ) |
154 | 76.6M | { |
155 | 76.6M | opus_int n, i, nBits, flags, tmp_payloadSize_ms = 0, tmp_complexity = 0, ret = 0; |
156 | 76.6M | opus_int nSamplesToBuffer, nSamplesToBufferMax, nBlocksOf10ms; |
157 | 76.6M | opus_int nSamplesFromInput = 0, nSamplesFromInputMax; |
158 | 76.6M | opus_int speech_act_thr_for_switch_Q8; |
159 | 76.6M | opus_int32 TargetRate_bps, MStargetRates_bps[ 2 ], channelRate_bps, LBRR_symbol, sum; |
160 | 76.6M | silk_encoder *psEnc = ( silk_encoder * )encState; |
161 | 76.6M | VARDECL( opus_int16, buf ); |
162 | 76.6M | opus_int transition, curr_block, tot_blocks; |
163 | 76.6M | SAVE_STACK; |
164 | | |
165 | 76.6M | if (encControl->reducedDependency) |
166 | 23.1M | { |
167 | 23.1M | psEnc->state_Fxx[0].sCmn.first_frame_after_reset = 1; |
168 | 23.1M | psEnc->state_Fxx[1].sCmn.first_frame_after_reset = 1; |
169 | 23.1M | } |
170 | 76.6M | psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded = psEnc->state_Fxx[ 1 ].sCmn.nFramesEncoded = 0; |
171 | | |
172 | | /* Check values in encoder control structure */ |
173 | 76.6M | if( ( ret = check_control_input( encControl ) ) != 0 ) { |
174 | 0 | celt_assert( 0 ); |
175 | 0 | RESTORE_STACK; |
176 | 0 | return ret; |
177 | 0 | } |
178 | | |
179 | 76.6M | encControl->switchReady = 0; |
180 | | |
181 | 76.6M | if( encControl->nChannelsInternal > psEnc->nChannelsInternal ) { |
182 | | /* Mono -> Stereo transition: init state of second channel and stereo state */ |
183 | 172k | ret += silk_init_encoder( &psEnc->state_Fxx[ 1 ], psEnc->state_Fxx[ 0 ].sCmn.arch ); |
184 | 172k | silk_memset( psEnc->sStereo.pred_prev_Q13, 0, sizeof( psEnc->sStereo.pred_prev_Q13 ) ); |
185 | 172k | silk_memset( psEnc->sStereo.sSide, 0, sizeof( psEnc->sStereo.sSide ) ); |
186 | 172k | psEnc->sStereo.mid_side_amp_Q0[ 0 ] = 0; |
187 | 172k | psEnc->sStereo.mid_side_amp_Q0[ 1 ] = 1; |
188 | 172k | psEnc->sStereo.mid_side_amp_Q0[ 2 ] = 0; |
189 | 172k | psEnc->sStereo.mid_side_amp_Q0[ 3 ] = 1; |
190 | 172k | psEnc->sStereo.width_prev_Q14 = 0; |
191 | 172k | psEnc->sStereo.smth_width_Q14 = SILK_FIX_CONST( 1, 14 ); |
192 | 172k | if( psEnc->nChannelsAPI == 2 ) { |
193 | 0 | silk_memcpy( &psEnc->state_Fxx[ 1 ].sCmn.resampler_state, &psEnc->state_Fxx[ 0 ].sCmn.resampler_state, sizeof( silk_resampler_state_struct ) ); |
194 | 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 ) ); |
195 | 0 | } |
196 | 172k | } |
197 | | |
198 | 76.6M | transition = (encControl->payloadSize_ms != psEnc->state_Fxx[ 0 ].sCmn.PacketSize_ms) || (psEnc->nChannelsInternal != encControl->nChannelsInternal); |
199 | | |
200 | 76.6M | psEnc->nChannelsAPI = encControl->nChannelsAPI; |
201 | 76.6M | psEnc->nChannelsInternal = encControl->nChannelsInternal; |
202 | | |
203 | 76.6M | nBlocksOf10ms = silk_DIV32( 100 * nSamplesIn, encControl->API_sampleRate ); |
204 | 76.6M | tot_blocks = ( nBlocksOf10ms > 1 ) ? nBlocksOf10ms >> 1 : 1; |
205 | 76.6M | curr_block = 0; |
206 | 76.6M | if( prefillFlag ) { |
207 | 1.94k | silk_LP_state save_LP; |
208 | | /* Only accept input length of 10 ms */ |
209 | 1.94k | if( nBlocksOf10ms != 1 ) { |
210 | 0 | celt_assert( 0 ); |
211 | 0 | RESTORE_STACK; |
212 | 0 | return SILK_ENC_INPUT_INVALID_NO_OF_SAMPLES; |
213 | 0 | } |
214 | 1.94k | if ( prefillFlag == 2 ) { |
215 | 609 | save_LP = psEnc->state_Fxx[ 0 ].sCmn.sLP; |
216 | | /* Save the sampling rate so the bandwidth switching code can keep handling transitions. */ |
217 | 609 | save_LP.saved_fs_kHz = psEnc->state_Fxx[ 0 ].sCmn.fs_kHz; |
218 | 609 | } |
219 | | /* Reset Encoder */ |
220 | 4.34k | for( n = 0; n < encControl->nChannelsInternal; n++ ) { |
221 | 2.40k | ret = silk_init_encoder( &psEnc->state_Fxx[ n ], psEnc->state_Fxx[ n ].sCmn.arch ); |
222 | | /* Restore the variable LP state. */ |
223 | 2.40k | if ( prefillFlag == 2 ) { |
224 | 735 | psEnc->state_Fxx[ n ].sCmn.sLP = save_LP; |
225 | 735 | } |
226 | 2.40k | celt_assert( !ret ); |
227 | 2.40k | } |
228 | 1.94k | tmp_payloadSize_ms = encControl->payloadSize_ms; |
229 | 1.94k | encControl->payloadSize_ms = 10; |
230 | 1.94k | tmp_complexity = encControl->complexity; |
231 | 1.94k | encControl->complexity = 0; |
232 | 4.34k | for( n = 0; n < encControl->nChannelsInternal; n++ ) { |
233 | 2.40k | psEnc->state_Fxx[ n ].sCmn.controlled_since_last_payload = 0; |
234 | 2.40k | psEnc->state_Fxx[ n ].sCmn.prefillFlag = 1; |
235 | 2.40k | } |
236 | 76.6M | } else { |
237 | | /* Only accept input lengths that are a multiple of 10 ms */ |
238 | 76.6M | if( nBlocksOf10ms * encControl->API_sampleRate != 100 * nSamplesIn || nSamplesIn < 0 ) { |
239 | 0 | celt_assert( 0 ); |
240 | 0 | RESTORE_STACK; |
241 | 0 | return SILK_ENC_INPUT_INVALID_NO_OF_SAMPLES; |
242 | 0 | } |
243 | | /* Make sure no more than one packet can be produced */ |
244 | 76.6M | if( 1000 * (opus_int32)nSamplesIn > encControl->payloadSize_ms * encControl->API_sampleRate ) { |
245 | 0 | celt_assert( 0 ); |
246 | 0 | RESTORE_STACK; |
247 | 0 | return SILK_ENC_INPUT_INVALID_NO_OF_SAMPLES; |
248 | 0 | } |
249 | 76.6M | } |
250 | | |
251 | 176M | for( n = 0; n < encControl->nChannelsInternal; n++ ) { |
252 | | /* Force the side channel to the same rate as the mid */ |
253 | 99.3M | opus_int force_fs_kHz = (n==1) ? psEnc->state_Fxx[0].sCmn.fs_kHz : 0; |
254 | 99.3M | if( ( ret = silk_control_encoder( &psEnc->state_Fxx[ n ], encControl, psEnc->allowBandwidthSwitch, n, force_fs_kHz ) ) != 0 ) { |
255 | 0 | silk_assert( 0 ); |
256 | 0 | RESTORE_STACK; |
257 | 0 | return ret; |
258 | 0 | } |
259 | 99.3M | if( psEnc->state_Fxx[n].sCmn.first_frame_after_reset || transition ) { |
260 | 70.7M | for( i = 0; i < psEnc->state_Fxx[ 0 ].sCmn.nFramesPerPacket; i++ ) { |
261 | 38.5M | psEnc->state_Fxx[ n ].sCmn.LBRR_flags[ i ] = 0; |
262 | 38.5M | } |
263 | 32.1M | } |
264 | 99.3M | psEnc->state_Fxx[ n ].sCmn.inDTX = psEnc->state_Fxx[ n ].sCmn.useDTX; |
265 | 99.3M | } |
266 | 76.6M | celt_assert( encControl->nChannelsInternal == 1 || psEnc->state_Fxx[ 0 ].sCmn.fs_kHz == psEnc->state_Fxx[ 1 ].sCmn.fs_kHz ); |
267 | | |
268 | | /* Input buffering/resampling and encoding */ |
269 | 76.6M | nSamplesToBufferMax = |
270 | 76.6M | 10 * nBlocksOf10ms * psEnc->state_Fxx[ 0 ].sCmn.fs_kHz; |
271 | 76.6M | nSamplesFromInputMax = |
272 | 76.6M | silk_DIV32_16( nSamplesToBufferMax * |
273 | 76.6M | psEnc->state_Fxx[ 0 ].sCmn.API_fs_Hz, |
274 | 76.6M | psEnc->state_Fxx[ 0 ].sCmn.fs_kHz * 1000 ); |
275 | 76.6M | ALLOC( buf, nSamplesFromInputMax, opus_int16 ); |
276 | 89.6M | while( 1 ) { |
277 | 89.6M | int curr_nBitsUsedLBRR = 0; |
278 | 89.6M | nSamplesToBuffer = psEnc->state_Fxx[ 0 ].sCmn.frame_length - psEnc->state_Fxx[ 0 ].sCmn.inputBufIx; |
279 | 89.6M | nSamplesToBuffer = silk_min( nSamplesToBuffer, nSamplesToBufferMax ); |
280 | 89.6M | nSamplesFromInput = silk_DIV32_16( nSamplesToBuffer * psEnc->state_Fxx[ 0 ].sCmn.API_fs_Hz, psEnc->state_Fxx[ 0 ].sCmn.fs_kHz * 1000 ); |
281 | | /* Resample and write to buffer */ |
282 | 89.6M | if( encControl->nChannelsAPI == 2 && encControl->nChannelsInternal == 2 ) { |
283 | 26.5M | opus_int id = psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded; |
284 | 7.95G | for( n = 0; n < nSamplesFromInput; n++ ) { |
285 | 7.93G | buf[ n ] = RES2INT16(samplesIn[ 2 * n ]); |
286 | 7.93G | } |
287 | | /* Making sure to start both resamplers from the same state when switching from mono to stereo */ |
288 | 26.5M | if( psEnc->nPrevChannelsInternal == 1 && id==0 ) { |
289 | 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)); |
290 | 0 | } |
291 | | |
292 | 26.5M | ret += silk_resampler( &psEnc->state_Fxx[ 0 ].sCmn.resampler_state, |
293 | 26.5M | &psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ psEnc->state_Fxx[ 0 ].sCmn.inputBufIx + 2 ], buf, nSamplesFromInput ); |
294 | 26.5M | psEnc->state_Fxx[ 0 ].sCmn.inputBufIx += nSamplesToBuffer; |
295 | | |
296 | 26.5M | nSamplesToBuffer = psEnc->state_Fxx[ 1 ].sCmn.frame_length - psEnc->state_Fxx[ 1 ].sCmn.inputBufIx; |
297 | 26.5M | nSamplesToBuffer = silk_min( nSamplesToBuffer, 10 * nBlocksOf10ms * psEnc->state_Fxx[ 1 ].sCmn.fs_kHz ); |
298 | 7.95G | for( n = 0; n < nSamplesFromInput; n++ ) { |
299 | 7.93G | buf[ n ] = RES2INT16(samplesIn[ 2 * n + 1 ]); |
300 | 7.93G | } |
301 | 26.5M | ret += silk_resampler( &psEnc->state_Fxx[ 1 ].sCmn.resampler_state, |
302 | 26.5M | &psEnc->state_Fxx[ 1 ].sCmn.inputBuf[ psEnc->state_Fxx[ 1 ].sCmn.inputBufIx + 2 ], buf, nSamplesFromInput ); |
303 | | |
304 | 26.5M | psEnc->state_Fxx[ 1 ].sCmn.inputBufIx += nSamplesToBuffer; |
305 | 63.1M | } else if( encControl->nChannelsAPI == 2 && encControl->nChannelsInternal == 1 ) { |
306 | | /* Combine left and right channels before resampling */ |
307 | 2.56G | for( n = 0; n < nSamplesFromInput; n++ ) { |
308 | 2.55G | sum = RES2INT16(samplesIn[ 2 * n ] + samplesIn[ 2 * n + 1 ]); |
309 | 2.55G | buf[ n ] = (opus_int16)silk_RSHIFT_ROUND( sum, 1 ); |
310 | 2.55G | } |
311 | 8.16M | ret += silk_resampler( &psEnc->state_Fxx[ 0 ].sCmn.resampler_state, |
312 | 8.16M | &psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ psEnc->state_Fxx[ 0 ].sCmn.inputBufIx + 2 ], buf, nSamplesFromInput ); |
313 | | /* On the first mono frame, average the results for the two resampler states */ |
314 | 8.16M | if( psEnc->nPrevChannelsInternal == 2 && psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded == 0 ) { |
315 | 0 | ret += silk_resampler( &psEnc->state_Fxx[ 1 ].sCmn.resampler_state, |
316 | 0 | &psEnc->state_Fxx[ 1 ].sCmn.inputBuf[ psEnc->state_Fxx[ 1 ].sCmn.inputBufIx + 2 ], buf, nSamplesFromInput ); |
317 | 0 | for( n = 0; n < psEnc->state_Fxx[ 0 ].sCmn.frame_length; n++ ) { |
318 | 0 | psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ psEnc->state_Fxx[ 0 ].sCmn.inputBufIx+n+2 ] = |
319 | 0 | silk_RSHIFT(psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ psEnc->state_Fxx[ 0 ].sCmn.inputBufIx+n+2 ] |
320 | 0 | + psEnc->state_Fxx[ 1 ].sCmn.inputBuf[ psEnc->state_Fxx[ 1 ].sCmn.inputBufIx+n+2 ], 1); |
321 | 0 | } |
322 | 0 | } |
323 | 8.16M | psEnc->state_Fxx[ 0 ].sCmn.inputBufIx += nSamplesToBuffer; |
324 | 54.9M | } else { |
325 | 54.9M | celt_assert( encControl->nChannelsAPI == 1 && encControl->nChannelsInternal == 1 ); |
326 | 17.2G | for( n = 0; n < nSamplesFromInput; n++ ) { |
327 | 17.2G | buf[n] = RES2INT16(samplesIn[n]); |
328 | 17.2G | } |
329 | 54.9M | ret += silk_resampler( &psEnc->state_Fxx[ 0 ].sCmn.resampler_state, |
330 | 54.9M | &psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ psEnc->state_Fxx[ 0 ].sCmn.inputBufIx + 2 ], buf, nSamplesFromInput ); |
331 | 54.9M | psEnc->state_Fxx[ 0 ].sCmn.inputBufIx += nSamplesToBuffer; |
332 | 54.9M | } |
333 | | |
334 | 89.6M | samplesIn += nSamplesFromInput * encControl->nChannelsAPI; |
335 | 89.6M | nSamplesIn -= nSamplesFromInput; |
336 | | |
337 | | /* Default */ |
338 | 89.6M | psEnc->allowBandwidthSwitch = 0; |
339 | | |
340 | | /* Silk encoder */ |
341 | 89.6M | if( psEnc->state_Fxx[ 0 ].sCmn.inputBufIx >= psEnc->state_Fxx[ 0 ].sCmn.frame_length ) { |
342 | | /* Enough data in input buffer, so encode */ |
343 | 89.6M | celt_assert( psEnc->state_Fxx[ 0 ].sCmn.inputBufIx == psEnc->state_Fxx[ 0 ].sCmn.frame_length ); |
344 | 89.6M | celt_assert( encControl->nChannelsInternal == 1 || psEnc->state_Fxx[ 1 ].sCmn.inputBufIx == psEnc->state_Fxx[ 1 ].sCmn.frame_length ); |
345 | | |
346 | | /* Deal with LBRR data */ |
347 | 89.6M | if( psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded == 0 && !prefillFlag ) { |
348 | | /* Create space at start of payload for VAD and FEC flags */ |
349 | 76.6M | opus_uint8 iCDF[ 2 ] = { 0, 0 }; |
350 | 76.6M | iCDF[ 0 ] = 256 - silk_RSHIFT( 256, ( psEnc->state_Fxx[ 0 ].sCmn.nFramesPerPacket + 1 ) * encControl->nChannelsInternal ); |
351 | 76.6M | ec_enc_icdf( psRangeEnc, 0, iCDF, 8 ); |
352 | 76.6M | curr_nBitsUsedLBRR = ec_tell( psRangeEnc ); |
353 | | |
354 | | /* Encode any LBRR data from previous packet */ |
355 | | /* Encode LBRR flags */ |
356 | 176M | for( n = 0; n < encControl->nChannelsInternal; n++ ) { |
357 | 99.3M | LBRR_symbol = 0; |
358 | 215M | for( i = 0; i < psEnc->state_Fxx[ n ].sCmn.nFramesPerPacket; i++ ) { |
359 | 116M | LBRR_symbol |= silk_LSHIFT( psEnc->state_Fxx[ n ].sCmn.LBRR_flags[ i ], i ); |
360 | 116M | } |
361 | 99.3M | psEnc->state_Fxx[ n ].sCmn.LBRR_flag = LBRR_symbol > 0 ? 1 : 0; |
362 | 99.3M | if( LBRR_symbol && psEnc->state_Fxx[ n ].sCmn.nFramesPerPacket > 1 ) { |
363 | 157k | ec_enc_icdf( psRangeEnc, LBRR_symbol - 1, silk_LBRR_flags_iCDF_ptr[ psEnc->state_Fxx[ n ].sCmn.nFramesPerPacket - 2 ], 8 ); |
364 | 157k | } |
365 | 99.3M | } |
366 | | |
367 | | /* Code LBRR indices and excitation signals */ |
368 | 166M | for( i = 0; i < psEnc->state_Fxx[ 0 ].sCmn.nFramesPerPacket; i++ ) { |
369 | 205M | for( n = 0; n < encControl->nChannelsInternal; n++ ) { |
370 | 116M | if( psEnc->state_Fxx[ n ].sCmn.LBRR_flags[ i ] ) { |
371 | 907k | opus_int condCoding; |
372 | | |
373 | 907k | if( encControl->nChannelsInternal == 2 && n == 0 ) { |
374 | 242k | silk_stereo_encode_pred( psRangeEnc, psEnc->sStereo.predIx[ i ] ); |
375 | | /* For LBRR data there's no need to code the mid-only flag if the side-channel LBRR flag is set */ |
376 | 242k | if( psEnc->state_Fxx[ 1 ].sCmn.LBRR_flags[ i ] == 0 ) { |
377 | 89.5k | silk_stereo_encode_mid_only( psRangeEnc, psEnc->sStereo.mid_only_flags[ i ] ); |
378 | 89.5k | } |
379 | 242k | } |
380 | | /* Use conditional coding if previous frame available */ |
381 | 907k | if( i > 0 && psEnc->state_Fxx[ n ].sCmn.LBRR_flags[ i - 1 ] ) { |
382 | 177k | condCoding = CODE_CONDITIONALLY; |
383 | 729k | } else { |
384 | 729k | condCoding = CODE_INDEPENDENTLY; |
385 | 729k | } |
386 | 907k | silk_encode_indices( &psEnc->state_Fxx[ n ].sCmn, psRangeEnc, i, 1, condCoding ); |
387 | 907k | silk_encode_pulses( psRangeEnc, psEnc->state_Fxx[ n ].sCmn.indices_LBRR[i].signalType, psEnc->state_Fxx[ n ].sCmn.indices_LBRR[i].quantOffsetType, |
388 | 907k | psEnc->state_Fxx[ n ].sCmn.pulses_LBRR[ i ], psEnc->state_Fxx[ n ].sCmn.frame_length ); |
389 | 907k | } |
390 | 116M | } |
391 | 89.6M | } |
392 | | |
393 | | /* Reset LBRR flags */ |
394 | 176M | for( n = 0; n < encControl->nChannelsInternal; n++ ) { |
395 | 99.3M | silk_memset( psEnc->state_Fxx[ n ].sCmn.LBRR_flags, 0, sizeof( psEnc->state_Fxx[ n ].sCmn.LBRR_flags ) ); |
396 | 99.3M | } |
397 | 76.6M | curr_nBitsUsedLBRR = ec_tell( psRangeEnc ) - curr_nBitsUsedLBRR; |
398 | 76.6M | } |
399 | | |
400 | 89.6M | silk_HP_variable_cutoff( psEnc->state_Fxx ); |
401 | | |
402 | | /* Total target bits for packet */ |
403 | 89.6M | nBits = silk_DIV32_16( silk_MUL( encControl->bitRate, encControl->payloadSize_ms ), 1000 ); |
404 | | /* Subtract bits used for LBRR */ |
405 | 89.6M | if( !prefillFlag ) { |
406 | | /* psEnc->nBitsUsedLBRR is an exponential moving average of the LBRR usage, |
407 | | except that for the first LBRR frame it does no averaging and for the first |
408 | | frame after after LBRR, it goes back to zero immediately. */ |
409 | 89.6M | if ( curr_nBitsUsedLBRR < 10 ) { |
410 | 89.0M | psEnc->nBitsUsedLBRR = 0; |
411 | 89.0M | } else if ( psEnc->nBitsUsedLBRR < 10) { |
412 | 177k | psEnc->nBitsUsedLBRR = curr_nBitsUsedLBRR; |
413 | 427k | } else { |
414 | 427k | psEnc->nBitsUsedLBRR = ( psEnc->nBitsUsedLBRR + curr_nBitsUsedLBRR ) / 2; |
415 | 427k | } |
416 | 89.6M | nBits -= psEnc->nBitsUsedLBRR; |
417 | 89.6M | } |
418 | | /* Divide by number of uncoded frames left in packet */ |
419 | 89.6M | nBits = silk_DIV32_16( nBits, psEnc->state_Fxx[ 0 ].sCmn.nFramesPerPacket ); |
420 | | /* Convert to bits/second */ |
421 | 89.6M | if( encControl->payloadSize_ms == 10 ) { |
422 | 35.5M | TargetRate_bps = silk_SMULBB( nBits, 100 ); |
423 | 54.0M | } else { |
424 | 54.0M | TargetRate_bps = silk_SMULBB( nBits, 50 ); |
425 | 54.0M | } |
426 | | /* Subtract fraction of bits in excess of target in previous frames and packets */ |
427 | 89.6M | TargetRate_bps -= silk_DIV32_16( silk_MUL( psEnc->nBitsExceeded, 1000 ), BITRESERVOIR_DECAY_TIME_MS ); |
428 | 89.6M | if( !prefillFlag && psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded > 0 ) { |
429 | | /* Compare actual vs target bits so far in this packet */ |
430 | 12.9M | opus_int32 bitsBalance = ec_tell( psRangeEnc ) - psEnc->nBitsUsedLBRR - nBits * psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded; |
431 | 12.9M | TargetRate_bps -= silk_DIV32_16( silk_MUL( bitsBalance, 1000 ), BITRESERVOIR_DECAY_TIME_MS ); |
432 | 12.9M | } |
433 | | /* Never exceed input bitrate */ |
434 | 89.6M | TargetRate_bps = silk_LIMIT( TargetRate_bps, encControl->bitRate, 5000 ); |
435 | | |
436 | | /* Convert Left/Right to Mid/Side */ |
437 | 89.6M | if( encControl->nChannelsInternal == 2 ) { |
438 | 26.5M | silk_stereo_LR_to_MS( &psEnc->sStereo, &psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ 2 ], &psEnc->state_Fxx[ 1 ].sCmn.inputBuf[ 2 ], |
439 | 26.5M | psEnc->sStereo.predIx[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ], &psEnc->sStereo.mid_only_flags[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ], |
440 | 26.5M | MStargetRates_bps, TargetRate_bps, psEnc->state_Fxx[ 0 ].sCmn.speech_activity_Q8, encControl->toMono, |
441 | 26.5M | psEnc->state_Fxx[ 0 ].sCmn.fs_kHz, psEnc->state_Fxx[ 0 ].sCmn.frame_length ); |
442 | 26.5M | if( psEnc->sStereo.mid_only_flags[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ] == 0 ) { |
443 | | /* Reset side channel encoder memory for first frame with side coding */ |
444 | 22.0M | if( psEnc->prev_decode_only_middle == 1 ) { |
445 | 9.07k | silk_memset( &psEnc->state_Fxx[ 1 ].sShape, 0, sizeof( psEnc->state_Fxx[ 1 ].sShape ) ); |
446 | 9.07k | silk_memset( &psEnc->state_Fxx[ 1 ].sCmn.sNSQ, 0, sizeof( psEnc->state_Fxx[ 1 ].sCmn.sNSQ ) ); |
447 | 9.07k | silk_memset( psEnc->state_Fxx[ 1 ].sCmn.prev_NLSFq_Q15, 0, sizeof( psEnc->state_Fxx[ 1 ].sCmn.prev_NLSFq_Q15 ) ); |
448 | 9.07k | silk_memset( &psEnc->state_Fxx[ 1 ].sCmn.sLP.In_LP_State, 0, sizeof( psEnc->state_Fxx[ 1 ].sCmn.sLP.In_LP_State ) ); |
449 | 9.07k | psEnc->state_Fxx[ 1 ].sCmn.prevLag = 100; |
450 | 9.07k | psEnc->state_Fxx[ 1 ].sCmn.sNSQ.lagPrev = 100; |
451 | 9.07k | psEnc->state_Fxx[ 1 ].sShape.LastGainIndex = 10; |
452 | 9.07k | psEnc->state_Fxx[ 1 ].sCmn.prevSignalType = TYPE_NO_VOICE_ACTIVITY; |
453 | 9.07k | psEnc->state_Fxx[ 1 ].sCmn.sNSQ.prev_gain_Q16 = 65536; |
454 | 9.07k | psEnc->state_Fxx[ 1 ].sCmn.first_frame_after_reset = 1; |
455 | 9.07k | } |
456 | 22.0M | silk_encode_do_VAD_Fxx( &psEnc->state_Fxx[ 1 ], activity ); |
457 | 22.0M | } else { |
458 | 4.42M | psEnc->state_Fxx[ 1 ].sCmn.VAD_flags[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ] = 0; |
459 | 4.42M | } |
460 | 26.5M | if( !prefillFlag ) { |
461 | 26.5M | silk_stereo_encode_pred( psRangeEnc, psEnc->sStereo.predIx[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ] ); |
462 | 26.5M | if( psEnc->state_Fxx[ 1 ].sCmn.VAD_flags[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ] == 0 ) { |
463 | 25.7M | silk_stereo_encode_mid_only( psRangeEnc, psEnc->sStereo.mid_only_flags[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ] ); |
464 | 25.7M | } |
465 | 26.5M | } |
466 | 63.1M | } else { |
467 | | /* Buffering */ |
468 | 63.1M | silk_memcpy( psEnc->state_Fxx[ 0 ].sCmn.inputBuf, psEnc->sStereo.sMid, 2 * sizeof( opus_int16 ) ); |
469 | 63.1M | silk_memcpy( psEnc->sStereo.sMid, &psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ psEnc->state_Fxx[ 0 ].sCmn.frame_length ], 2 * sizeof( opus_int16 ) ); |
470 | 63.1M | } |
471 | 89.6M | silk_encode_do_VAD_Fxx( &psEnc->state_Fxx[ 0 ], activity ); |
472 | | |
473 | | /* Encode */ |
474 | 205M | for( n = 0; n < encControl->nChannelsInternal; n++ ) { |
475 | 116M | opus_int maxBits, useCBR; |
476 | | |
477 | | /* Handling rate constraints */ |
478 | 116M | maxBits = encControl->maxBits; |
479 | 116M | if( tot_blocks == 2 && curr_block == 0 ) { |
480 | 8.38M | maxBits = maxBits * 3 / 5; |
481 | 107M | } else if( tot_blocks == 3 ) { |
482 | 12.5M | if( curr_block == 0 ) { |
483 | 4.19M | maxBits = maxBits * 2 / 5; |
484 | 8.39M | } else if( curr_block == 1 ) { |
485 | 4.19M | maxBits = maxBits * 3 / 4; |
486 | 4.19M | } |
487 | 12.5M | } |
488 | 116M | useCBR = encControl->useCBR && curr_block == tot_blocks - 1; |
489 | | |
490 | 116M | if( encControl->nChannelsInternal == 1 ) { |
491 | 63.1M | channelRate_bps = TargetRate_bps; |
492 | 63.1M | } else { |
493 | 53.0M | channelRate_bps = MStargetRates_bps[ n ]; |
494 | 53.0M | if( n == 0 && MStargetRates_bps[ 1 ] > 0 ) { |
495 | 22.0M | useCBR = 0; |
496 | | /* Give mid up to 1/2 of the max bits for that frame */ |
497 | 22.0M | maxBits -= encControl->maxBits / ( tot_blocks * 2 ); |
498 | 22.0M | } |
499 | 53.0M | } |
500 | | |
501 | 116M | if( channelRate_bps > 0 ) { |
502 | 111M | opus_int condCoding; |
503 | | |
504 | 111M | silk_control_SNR( &psEnc->state_Fxx[ n ].sCmn, channelRate_bps ); |
505 | | |
506 | | /* Use independent coding if no previous frame available */ |
507 | 111M | if( psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded - n <= 0 ) { |
508 | 95.5M | condCoding = CODE_INDEPENDENTLY; |
509 | 95.5M | } else if( n > 0 && psEnc->prev_decode_only_middle ) { |
510 | | /* If we skipped a side frame in this packet, we don't |
511 | | need LTP scaling; the LTP state is well-defined. */ |
512 | 5.11k | condCoding = CODE_INDEPENDENTLY_NO_LTP_SCALING; |
513 | 16.1M | } else { |
514 | 16.1M | condCoding = CODE_CONDITIONALLY; |
515 | 16.1M | } |
516 | 111M | if( ( ret = silk_encode_frame_Fxx( &psEnc->state_Fxx[ n ], nBytesOut, psRangeEnc, condCoding, maxBits, useCBR ) ) != 0 ) { |
517 | 0 | silk_assert( 0 ); |
518 | 0 | } |
519 | 111M | } |
520 | 116M | psEnc->state_Fxx[ n ].sCmn.controlled_since_last_payload = 0; |
521 | 116M | psEnc->state_Fxx[ n ].sCmn.inputBufIx = 0; |
522 | 116M | psEnc->state_Fxx[ n ].sCmn.nFramesEncoded++; |
523 | 116M | } |
524 | 89.6M | psEnc->prev_decode_only_middle = psEnc->sStereo.mid_only_flags[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded - 1 ]; |
525 | | |
526 | | /* Insert VAD and FEC flags at beginning of bitstream */ |
527 | 89.6M | if( *nBytesOut > 0 && psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded == psEnc->state_Fxx[ 0 ].sCmn.nFramesPerPacket) { |
528 | 76.6M | flags = 0; |
529 | 176M | for( n = 0; n < encControl->nChannelsInternal; n++ ) { |
530 | 215M | for( i = 0; i < psEnc->state_Fxx[ n ].sCmn.nFramesPerPacket; i++ ) { |
531 | 116M | flags = silk_LSHIFT( flags, 1 ); |
532 | 116M | flags |= psEnc->state_Fxx[ n ].sCmn.VAD_flags[ i ]; |
533 | 116M | } |
534 | 99.3M | flags = silk_LSHIFT( flags, 1 ); |
535 | 99.3M | flags |= psEnc->state_Fxx[ n ].sCmn.LBRR_flag; |
536 | 99.3M | } |
537 | 76.6M | if( !prefillFlag ) { |
538 | 76.6M | ec_enc_patch_initial_bits( psRangeEnc, flags, ( psEnc->state_Fxx[ 0 ].sCmn.nFramesPerPacket + 1 ) * encControl->nChannelsInternal ); |
539 | 76.6M | } |
540 | | |
541 | | /* Return zero bytes if all channels DTXed */ |
542 | 76.6M | if( psEnc->state_Fxx[ 0 ].sCmn.inDTX && ( encControl->nChannelsInternal == 1 || psEnc->state_Fxx[ 1 ].sCmn.inDTX ) ) { |
543 | 25.1M | *nBytesOut = 0; |
544 | 25.1M | } |
545 | | |
546 | 76.6M | psEnc->nBitsExceeded += *nBytesOut * 8; |
547 | 76.6M | psEnc->nBitsExceeded -= silk_DIV32_16( silk_MUL( encControl->bitRate, encControl->payloadSize_ms ), 1000 ); |
548 | 76.6M | psEnc->nBitsExceeded = silk_LIMIT( psEnc->nBitsExceeded, 0, 10000 ); |
549 | | |
550 | | /* Update flag indicating if bandwidth switching is allowed */ |
551 | 76.6M | speech_act_thr_for_switch_Q8 = silk_SMLAWB( SILK_FIX_CONST( SPEECH_ACTIVITY_DTX_THRES, 8 ), |
552 | 76.6M | SILK_FIX_CONST( ( 1 - SPEECH_ACTIVITY_DTX_THRES ) / MAX_BANDWIDTH_SWITCH_DELAY_MS, 16 + 8 ), psEnc->timeSinceSwitchAllowed_ms ); |
553 | 76.6M | if( psEnc->state_Fxx[ 0 ].sCmn.speech_activity_Q8 < speech_act_thr_for_switch_Q8 ) { |
554 | 74.0M | psEnc->allowBandwidthSwitch = 1; |
555 | 74.0M | psEnc->timeSinceSwitchAllowed_ms = 0; |
556 | 74.0M | } else { |
557 | 2.58M | psEnc->allowBandwidthSwitch = 0; |
558 | 2.58M | psEnc->timeSinceSwitchAllowed_ms += encControl->payloadSize_ms; |
559 | 2.58M | } |
560 | 76.6M | } |
561 | | |
562 | 89.6M | if( nSamplesIn == 0 ) { |
563 | 76.6M | break; |
564 | 76.6M | } |
565 | 89.6M | } else { |
566 | 0 | break; |
567 | 0 | } |
568 | 12.9M | curr_block++; |
569 | 12.9M | } |
570 | | |
571 | 76.6M | psEnc->nPrevChannelsInternal = encControl->nChannelsInternal; |
572 | | |
573 | 76.6M | encControl->allowBandwidthSwitch = psEnc->allowBandwidthSwitch; |
574 | 76.6M | encControl->inWBmodeWithoutVariableLP = psEnc->state_Fxx[ 0 ].sCmn.fs_kHz == 16 && psEnc->state_Fxx[ 0 ].sCmn.sLP.mode == 0; |
575 | 76.6M | encControl->internalSampleRate = silk_SMULBB( psEnc->state_Fxx[ 0 ].sCmn.fs_kHz, 1000 ); |
576 | 76.6M | encControl->stereoWidth_Q14 = encControl->toMono ? 0 : psEnc->sStereo.smth_width_Q14; |
577 | 76.6M | if( prefillFlag ) { |
578 | 1.94k | encControl->payloadSize_ms = tmp_payloadSize_ms; |
579 | 1.94k | encControl->complexity = tmp_complexity; |
580 | 4.34k | for( n = 0; n < encControl->nChannelsInternal; n++ ) { |
581 | 2.40k | psEnc->state_Fxx[ n ].sCmn.controlled_since_last_payload = 0; |
582 | 2.40k | psEnc->state_Fxx[ n ].sCmn.prefillFlag = 0; |
583 | 2.40k | } |
584 | 1.94k | } |
585 | | |
586 | 76.6M | encControl->signalType = psEnc->state_Fxx[0].sCmn.indices.signalType; |
587 | 76.6M | encControl->offset = silk_Quantization_Offsets_Q10 |
588 | 76.6M | [ psEnc->state_Fxx[0].sCmn.indices.signalType >> 1 ] |
589 | 76.6M | [ psEnc->state_Fxx[0].sCmn.indices.quantOffsetType ]; |
590 | 76.6M | RESTORE_STACK; |
591 | 76.6M | return ret; |
592 | 76.6M | } Line | Count | Source | 154 | 25.5M | { | 155 | 25.5M | opus_int n, i, nBits, flags, tmp_payloadSize_ms = 0, tmp_complexity = 0, ret = 0; | 156 | 25.5M | opus_int nSamplesToBuffer, nSamplesToBufferMax, nBlocksOf10ms; | 157 | 25.5M | opus_int nSamplesFromInput = 0, nSamplesFromInputMax; | 158 | 25.5M | opus_int speech_act_thr_for_switch_Q8; | 159 | 25.5M | opus_int32 TargetRate_bps, MStargetRates_bps[ 2 ], channelRate_bps, LBRR_symbol, sum; | 160 | 25.5M | silk_encoder *psEnc = ( silk_encoder * )encState; | 161 | 25.5M | VARDECL( opus_int16, buf ); | 162 | 25.5M | opus_int transition, curr_block, tot_blocks; | 163 | 25.5M | SAVE_STACK; | 164 | | | 165 | 25.5M | if (encControl->reducedDependency) | 166 | 7.70M | { | 167 | 7.70M | psEnc->state_Fxx[0].sCmn.first_frame_after_reset = 1; | 168 | 7.70M | psEnc->state_Fxx[1].sCmn.first_frame_after_reset = 1; | 169 | 7.70M | } | 170 | 25.5M | psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded = psEnc->state_Fxx[ 1 ].sCmn.nFramesEncoded = 0; | 171 | | | 172 | | /* Check values in encoder control structure */ | 173 | 25.5M | if( ( ret = check_control_input( encControl ) ) != 0 ) { | 174 | 0 | celt_assert( 0 ); | 175 | 0 | RESTORE_STACK; | 176 | 0 | return ret; | 177 | 0 | } | 178 | | | 179 | 25.5M | encControl->switchReady = 0; | 180 | | | 181 | 25.5M | if( encControl->nChannelsInternal > psEnc->nChannelsInternal ) { | 182 | | /* Mono -> Stereo transition: init state of second channel and stereo state */ | 183 | 57.4k | ret += silk_init_encoder( &psEnc->state_Fxx[ 1 ], psEnc->state_Fxx[ 0 ].sCmn.arch ); | 184 | 57.4k | silk_memset( psEnc->sStereo.pred_prev_Q13, 0, sizeof( psEnc->sStereo.pred_prev_Q13 ) ); | 185 | 57.4k | silk_memset( psEnc->sStereo.sSide, 0, sizeof( psEnc->sStereo.sSide ) ); | 186 | 57.4k | psEnc->sStereo.mid_side_amp_Q0[ 0 ] = 0; | 187 | 57.4k | psEnc->sStereo.mid_side_amp_Q0[ 1 ] = 1; | 188 | 57.4k | psEnc->sStereo.mid_side_amp_Q0[ 2 ] = 0; | 189 | 57.4k | psEnc->sStereo.mid_side_amp_Q0[ 3 ] = 1; | 190 | 57.4k | psEnc->sStereo.width_prev_Q14 = 0; | 191 | 57.4k | psEnc->sStereo.smth_width_Q14 = SILK_FIX_CONST( 1, 14 ); | 192 | 57.4k | if( psEnc->nChannelsAPI == 2 ) { | 193 | 0 | silk_memcpy( &psEnc->state_Fxx[ 1 ].sCmn.resampler_state, &psEnc->state_Fxx[ 0 ].sCmn.resampler_state, sizeof( silk_resampler_state_struct ) ); | 194 | 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 ) ); | 195 | 0 | } | 196 | 57.4k | } | 197 | | | 198 | 25.5M | transition = (encControl->payloadSize_ms != psEnc->state_Fxx[ 0 ].sCmn.PacketSize_ms) || (psEnc->nChannelsInternal != encControl->nChannelsInternal); | 199 | | | 200 | 25.5M | psEnc->nChannelsAPI = encControl->nChannelsAPI; | 201 | 25.5M | psEnc->nChannelsInternal = encControl->nChannelsInternal; | 202 | | | 203 | 25.5M | nBlocksOf10ms = silk_DIV32( 100 * nSamplesIn, encControl->API_sampleRate ); | 204 | 25.5M | tot_blocks = ( nBlocksOf10ms > 1 ) ? nBlocksOf10ms >> 1 : 1; | 205 | 25.5M | curr_block = 0; | 206 | 25.5M | if( prefillFlag ) { | 207 | 647 | silk_LP_state save_LP; | 208 | | /* Only accept input length of 10 ms */ | 209 | 647 | if( nBlocksOf10ms != 1 ) { | 210 | 0 | celt_assert( 0 ); | 211 | 0 | RESTORE_STACK; | 212 | 0 | return SILK_ENC_INPUT_INVALID_NO_OF_SAMPLES; | 213 | 0 | } | 214 | 647 | if ( prefillFlag == 2 ) { | 215 | 203 | save_LP = psEnc->state_Fxx[ 0 ].sCmn.sLP; | 216 | | /* Save the sampling rate so the bandwidth switching code can keep handling transitions. */ | 217 | 203 | save_LP.saved_fs_kHz = psEnc->state_Fxx[ 0 ].sCmn.fs_kHz; | 218 | 203 | } | 219 | | /* Reset Encoder */ | 220 | 1.44k | for( n = 0; n < encControl->nChannelsInternal; n++ ) { | 221 | 801 | ret = silk_init_encoder( &psEnc->state_Fxx[ n ], psEnc->state_Fxx[ n ].sCmn.arch ); | 222 | | /* Restore the variable LP state. */ | 223 | 801 | if ( prefillFlag == 2 ) { | 224 | 245 | psEnc->state_Fxx[ n ].sCmn.sLP = save_LP; | 225 | 245 | } | 226 | 801 | celt_assert( !ret ); | 227 | 801 | } | 228 | 647 | tmp_payloadSize_ms = encControl->payloadSize_ms; | 229 | 647 | encControl->payloadSize_ms = 10; | 230 | 647 | tmp_complexity = encControl->complexity; | 231 | 647 | encControl->complexity = 0; | 232 | 1.44k | for( n = 0; n < encControl->nChannelsInternal; n++ ) { | 233 | 801 | psEnc->state_Fxx[ n ].sCmn.controlled_since_last_payload = 0; | 234 | 801 | psEnc->state_Fxx[ n ].sCmn.prefillFlag = 1; | 235 | 801 | } | 236 | 25.5M | } else { | 237 | | /* Only accept input lengths that are a multiple of 10 ms */ | 238 | 25.5M | if( nBlocksOf10ms * encControl->API_sampleRate != 100 * nSamplesIn || nSamplesIn < 0 ) { | 239 | 0 | celt_assert( 0 ); | 240 | 0 | RESTORE_STACK; | 241 | 0 | return SILK_ENC_INPUT_INVALID_NO_OF_SAMPLES; | 242 | 0 | } | 243 | | /* Make sure no more than one packet can be produced */ | 244 | 25.5M | if( 1000 * (opus_int32)nSamplesIn > encControl->payloadSize_ms * encControl->API_sampleRate ) { | 245 | 0 | celt_assert( 0 ); | 246 | 0 | RESTORE_STACK; | 247 | 0 | return SILK_ENC_INPUT_INVALID_NO_OF_SAMPLES; | 248 | 0 | } | 249 | 25.5M | } | 250 | | | 251 | 58.6M | for( n = 0; n < encControl->nChannelsInternal; n++ ) { | 252 | | /* Force the side channel to the same rate as the mid */ | 253 | 33.1M | opus_int force_fs_kHz = (n==1) ? psEnc->state_Fxx[0].sCmn.fs_kHz : 0; | 254 | 33.1M | if( ( ret = silk_control_encoder( &psEnc->state_Fxx[ n ], encControl, psEnc->allowBandwidthSwitch, n, force_fs_kHz ) ) != 0 ) { | 255 | 0 | silk_assert( 0 ); | 256 | 0 | RESTORE_STACK; | 257 | 0 | return ret; | 258 | 0 | } | 259 | 33.1M | if( psEnc->state_Fxx[n].sCmn.first_frame_after_reset || transition ) { | 260 | 23.5M | for( i = 0; i < psEnc->state_Fxx[ 0 ].sCmn.nFramesPerPacket; i++ ) { | 261 | 12.8M | psEnc->state_Fxx[ n ].sCmn.LBRR_flags[ i ] = 0; | 262 | 12.8M | } | 263 | 10.7M | } | 264 | 33.1M | psEnc->state_Fxx[ n ].sCmn.inDTX = psEnc->state_Fxx[ n ].sCmn.useDTX; | 265 | 33.1M | } | 266 | 25.5M | celt_assert( encControl->nChannelsInternal == 1 || psEnc->state_Fxx[ 0 ].sCmn.fs_kHz == psEnc->state_Fxx[ 1 ].sCmn.fs_kHz ); | 267 | | | 268 | | /* Input buffering/resampling and encoding */ | 269 | 25.5M | nSamplesToBufferMax = | 270 | 25.5M | 10 * nBlocksOf10ms * psEnc->state_Fxx[ 0 ].sCmn.fs_kHz; | 271 | 25.5M | nSamplesFromInputMax = | 272 | 25.5M | silk_DIV32_16( nSamplesToBufferMax * | 273 | 25.5M | psEnc->state_Fxx[ 0 ].sCmn.API_fs_Hz, | 274 | 25.5M | psEnc->state_Fxx[ 0 ].sCmn.fs_kHz * 1000 ); | 275 | 25.5M | ALLOC( buf, nSamplesFromInputMax, opus_int16 ); | 276 | 29.8M | while( 1 ) { | 277 | 29.8M | int curr_nBitsUsedLBRR = 0; | 278 | 29.8M | nSamplesToBuffer = psEnc->state_Fxx[ 0 ].sCmn.frame_length - psEnc->state_Fxx[ 0 ].sCmn.inputBufIx; | 279 | 29.8M | nSamplesToBuffer = silk_min( nSamplesToBuffer, nSamplesToBufferMax ); | 280 | 29.8M | nSamplesFromInput = silk_DIV32_16( nSamplesToBuffer * psEnc->state_Fxx[ 0 ].sCmn.API_fs_Hz, psEnc->state_Fxx[ 0 ].sCmn.fs_kHz * 1000 ); | 281 | | /* Resample and write to buffer */ | 282 | 29.8M | if( encControl->nChannelsAPI == 2 && encControl->nChannelsInternal == 2 ) { | 283 | 8.84M | opus_int id = psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded; | 284 | 2.65G | for( n = 0; n < nSamplesFromInput; n++ ) { | 285 | 2.64G | buf[ n ] = RES2INT16(samplesIn[ 2 * n ]); | 286 | 2.64G | } | 287 | | /* Making sure to start both resamplers from the same state when switching from mono to stereo */ | 288 | 8.84M | if( psEnc->nPrevChannelsInternal == 1 && id==0 ) { | 289 | 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)); | 290 | 0 | } | 291 | | | 292 | 8.84M | ret += silk_resampler( &psEnc->state_Fxx[ 0 ].sCmn.resampler_state, | 293 | 8.84M | &psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ psEnc->state_Fxx[ 0 ].sCmn.inputBufIx + 2 ], buf, nSamplesFromInput ); | 294 | 8.84M | psEnc->state_Fxx[ 0 ].sCmn.inputBufIx += nSamplesToBuffer; | 295 | | | 296 | 8.84M | nSamplesToBuffer = psEnc->state_Fxx[ 1 ].sCmn.frame_length - psEnc->state_Fxx[ 1 ].sCmn.inputBufIx; | 297 | 8.84M | nSamplesToBuffer = silk_min( nSamplesToBuffer, 10 * nBlocksOf10ms * psEnc->state_Fxx[ 1 ].sCmn.fs_kHz ); | 298 | 2.65G | for( n = 0; n < nSamplesFromInput; n++ ) { | 299 | 2.64G | buf[ n ] = RES2INT16(samplesIn[ 2 * n + 1 ]); | 300 | 2.64G | } | 301 | 8.84M | ret += silk_resampler( &psEnc->state_Fxx[ 1 ].sCmn.resampler_state, | 302 | 8.84M | &psEnc->state_Fxx[ 1 ].sCmn.inputBuf[ psEnc->state_Fxx[ 1 ].sCmn.inputBufIx + 2 ], buf, nSamplesFromInput ); | 303 | | | 304 | 8.84M | psEnc->state_Fxx[ 1 ].sCmn.inputBufIx += nSamplesToBuffer; | 305 | 21.0M | } else if( encControl->nChannelsAPI == 2 && encControl->nChannelsInternal == 1 ) { | 306 | | /* Combine left and right channels before resampling */ | 307 | 853M | for( n = 0; n < nSamplesFromInput; n++ ) { | 308 | 851M | sum = RES2INT16(samplesIn[ 2 * n ] + samplesIn[ 2 * n + 1 ]); | 309 | 851M | buf[ n ] = (opus_int16)silk_RSHIFT_ROUND( sum, 1 ); | 310 | 851M | } | 311 | 2.72M | ret += silk_resampler( &psEnc->state_Fxx[ 0 ].sCmn.resampler_state, | 312 | 2.72M | &psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ psEnc->state_Fxx[ 0 ].sCmn.inputBufIx + 2 ], buf, nSamplesFromInput ); | 313 | | /* On the first mono frame, average the results for the two resampler states */ | 314 | 2.72M | if( psEnc->nPrevChannelsInternal == 2 && psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded == 0 ) { | 315 | 0 | ret += silk_resampler( &psEnc->state_Fxx[ 1 ].sCmn.resampler_state, | 316 | 0 | &psEnc->state_Fxx[ 1 ].sCmn.inputBuf[ psEnc->state_Fxx[ 1 ].sCmn.inputBufIx + 2 ], buf, nSamplesFromInput ); | 317 | 0 | for( n = 0; n < psEnc->state_Fxx[ 0 ].sCmn.frame_length; n++ ) { | 318 | 0 | psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ psEnc->state_Fxx[ 0 ].sCmn.inputBufIx+n+2 ] = | 319 | 0 | silk_RSHIFT(psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ psEnc->state_Fxx[ 0 ].sCmn.inputBufIx+n+2 ] | 320 | 0 | + psEnc->state_Fxx[ 1 ].sCmn.inputBuf[ psEnc->state_Fxx[ 1 ].sCmn.inputBufIx+n+2 ], 1); | 321 | 0 | } | 322 | 0 | } | 323 | 2.72M | psEnc->state_Fxx[ 0 ].sCmn.inputBufIx += nSamplesToBuffer; | 324 | 18.3M | } else { | 325 | 18.3M | celt_assert( encControl->nChannelsAPI == 1 && encControl->nChannelsInternal == 1 ); | 326 | 5.76G | for( n = 0; n < nSamplesFromInput; n++ ) { | 327 | 5.74G | buf[n] = RES2INT16(samplesIn[n]); | 328 | 5.74G | } | 329 | 18.3M | ret += silk_resampler( &psEnc->state_Fxx[ 0 ].sCmn.resampler_state, | 330 | 18.3M | &psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ psEnc->state_Fxx[ 0 ].sCmn.inputBufIx + 2 ], buf, nSamplesFromInput ); | 331 | 18.3M | psEnc->state_Fxx[ 0 ].sCmn.inputBufIx += nSamplesToBuffer; | 332 | 18.3M | } | 333 | | | 334 | 29.8M | samplesIn += nSamplesFromInput * encControl->nChannelsAPI; | 335 | 29.8M | nSamplesIn -= nSamplesFromInput; | 336 | | | 337 | | /* Default */ | 338 | 29.8M | psEnc->allowBandwidthSwitch = 0; | 339 | | | 340 | | /* Silk encoder */ | 341 | 29.8M | if( psEnc->state_Fxx[ 0 ].sCmn.inputBufIx >= psEnc->state_Fxx[ 0 ].sCmn.frame_length ) { | 342 | | /* Enough data in input buffer, so encode */ | 343 | 29.8M | celt_assert( psEnc->state_Fxx[ 0 ].sCmn.inputBufIx == psEnc->state_Fxx[ 0 ].sCmn.frame_length ); | 344 | 29.8M | celt_assert( encControl->nChannelsInternal == 1 || psEnc->state_Fxx[ 1 ].sCmn.inputBufIx == psEnc->state_Fxx[ 1 ].sCmn.frame_length ); | 345 | | | 346 | | /* Deal with LBRR data */ | 347 | 29.8M | if( psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded == 0 && !prefillFlag ) { | 348 | | /* Create space at start of payload for VAD and FEC flags */ | 349 | 25.5M | opus_uint8 iCDF[ 2 ] = { 0, 0 }; | 350 | 25.5M | iCDF[ 0 ] = 256 - silk_RSHIFT( 256, ( psEnc->state_Fxx[ 0 ].sCmn.nFramesPerPacket + 1 ) * encControl->nChannelsInternal ); | 351 | 25.5M | ec_enc_icdf( psRangeEnc, 0, iCDF, 8 ); | 352 | 25.5M | curr_nBitsUsedLBRR = ec_tell( psRangeEnc ); | 353 | | | 354 | | /* Encode any LBRR data from previous packet */ | 355 | | /* Encode LBRR flags */ | 356 | 58.6M | for( n = 0; n < encControl->nChannelsInternal; n++ ) { | 357 | 33.1M | LBRR_symbol = 0; | 358 | 71.8M | for( i = 0; i < psEnc->state_Fxx[ n ].sCmn.nFramesPerPacket; i++ ) { | 359 | 38.7M | LBRR_symbol |= silk_LSHIFT( psEnc->state_Fxx[ n ].sCmn.LBRR_flags[ i ], i ); | 360 | 38.7M | } | 361 | 33.1M | psEnc->state_Fxx[ n ].sCmn.LBRR_flag = LBRR_symbol > 0 ? 1 : 0; | 362 | 33.1M | if( LBRR_symbol && psEnc->state_Fxx[ n ].sCmn.nFramesPerPacket > 1 ) { | 363 | 52.6k | ec_enc_icdf( psRangeEnc, LBRR_symbol - 1, silk_LBRR_flags_iCDF_ptr[ psEnc->state_Fxx[ n ].sCmn.nFramesPerPacket - 2 ], 8 ); | 364 | 52.6k | } | 365 | 33.1M | } | 366 | | | 367 | | /* Code LBRR indices and excitation signals */ | 368 | 55.4M | for( i = 0; i < psEnc->state_Fxx[ 0 ].sCmn.nFramesPerPacket; i++ ) { | 369 | 68.5M | for( n = 0; n < encControl->nChannelsInternal; n++ ) { | 370 | 38.7M | if( psEnc->state_Fxx[ n ].sCmn.LBRR_flags[ i ] ) { | 371 | 302k | opus_int condCoding; | 372 | | | 373 | 302k | if( encControl->nChannelsInternal == 2 && n == 0 ) { | 374 | 80.8k | silk_stereo_encode_pred( psRangeEnc, psEnc->sStereo.predIx[ i ] ); | 375 | | /* For LBRR data there's no need to code the mid-only flag if the side-channel LBRR flag is set */ | 376 | 80.8k | if( psEnc->state_Fxx[ 1 ].sCmn.LBRR_flags[ i ] == 0 ) { | 377 | 29.8k | silk_stereo_encode_mid_only( psRangeEnc, psEnc->sStereo.mid_only_flags[ i ] ); | 378 | 29.8k | } | 379 | 80.8k | } | 380 | | /* Use conditional coding if previous frame available */ | 381 | 302k | if( i > 0 && psEnc->state_Fxx[ n ].sCmn.LBRR_flags[ i - 1 ] ) { | 382 | 59.3k | condCoding = CODE_CONDITIONALLY; | 383 | 243k | } else { | 384 | 243k | condCoding = CODE_INDEPENDENTLY; | 385 | 243k | } | 386 | 302k | silk_encode_indices( &psEnc->state_Fxx[ n ].sCmn, psRangeEnc, i, 1, condCoding ); | 387 | 302k | silk_encode_pulses( psRangeEnc, psEnc->state_Fxx[ n ].sCmn.indices_LBRR[i].signalType, psEnc->state_Fxx[ n ].sCmn.indices_LBRR[i].quantOffsetType, | 388 | 302k | psEnc->state_Fxx[ n ].sCmn.pulses_LBRR[ i ], psEnc->state_Fxx[ n ].sCmn.frame_length ); | 389 | 302k | } | 390 | 38.7M | } | 391 | 29.8M | } | 392 | | | 393 | | /* Reset LBRR flags */ | 394 | 58.6M | for( n = 0; n < encControl->nChannelsInternal; n++ ) { | 395 | 33.1M | silk_memset( psEnc->state_Fxx[ n ].sCmn.LBRR_flags, 0, sizeof( psEnc->state_Fxx[ n ].sCmn.LBRR_flags ) ); | 396 | 33.1M | } | 397 | 25.5M | curr_nBitsUsedLBRR = ec_tell( psRangeEnc ) - curr_nBitsUsedLBRR; | 398 | 25.5M | } | 399 | | | 400 | 29.8M | silk_HP_variable_cutoff( psEnc->state_Fxx ); | 401 | | | 402 | | /* Total target bits for packet */ | 403 | 29.8M | nBits = silk_DIV32_16( silk_MUL( encControl->bitRate, encControl->payloadSize_ms ), 1000 ); | 404 | | /* Subtract bits used for LBRR */ | 405 | 29.8M | if( !prefillFlag ) { | 406 | | /* psEnc->nBitsUsedLBRR is an exponential moving average of the LBRR usage, | 407 | | except that for the first LBRR frame it does no averaging and for the first | 408 | | frame after after LBRR, it goes back to zero immediately. */ | 409 | 29.8M | if ( curr_nBitsUsedLBRR < 10 ) { | 410 | 29.6M | psEnc->nBitsUsedLBRR = 0; | 411 | 29.6M | } else if ( psEnc->nBitsUsedLBRR < 10) { | 412 | 59.2k | psEnc->nBitsUsedLBRR = curr_nBitsUsedLBRR; | 413 | 142k | } else { | 414 | 142k | psEnc->nBitsUsedLBRR = ( psEnc->nBitsUsedLBRR + curr_nBitsUsedLBRR ) / 2; | 415 | 142k | } | 416 | 29.8M | nBits -= psEnc->nBitsUsedLBRR; | 417 | 29.8M | } | 418 | | /* Divide by number of uncoded frames left in packet */ | 419 | 29.8M | nBits = silk_DIV32_16( nBits, psEnc->state_Fxx[ 0 ].sCmn.nFramesPerPacket ); | 420 | | /* Convert to bits/second */ | 421 | 29.8M | if( encControl->payloadSize_ms == 10 ) { | 422 | 11.8M | TargetRate_bps = silk_SMULBB( nBits, 100 ); | 423 | 18.0M | } else { | 424 | 18.0M | TargetRate_bps = silk_SMULBB( nBits, 50 ); | 425 | 18.0M | } | 426 | | /* Subtract fraction of bits in excess of target in previous frames and packets */ | 427 | 29.8M | TargetRate_bps -= silk_DIV32_16( silk_MUL( psEnc->nBitsExceeded, 1000 ), BITRESERVOIR_DECAY_TIME_MS ); | 428 | 29.8M | if( !prefillFlag && psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded > 0 ) { | 429 | | /* Compare actual vs target bits so far in this packet */ | 430 | 4.32M | opus_int32 bitsBalance = ec_tell( psRangeEnc ) - psEnc->nBitsUsedLBRR - nBits * psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded; | 431 | 4.32M | TargetRate_bps -= silk_DIV32_16( silk_MUL( bitsBalance, 1000 ), BITRESERVOIR_DECAY_TIME_MS ); | 432 | 4.32M | } | 433 | | /* Never exceed input bitrate */ | 434 | 29.8M | TargetRate_bps = silk_LIMIT( TargetRate_bps, encControl->bitRate, 5000 ); | 435 | | | 436 | | /* Convert Left/Right to Mid/Side */ | 437 | 29.8M | if( encControl->nChannelsInternal == 2 ) { | 438 | 8.84M | silk_stereo_LR_to_MS( &psEnc->sStereo, &psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ 2 ], &psEnc->state_Fxx[ 1 ].sCmn.inputBuf[ 2 ], | 439 | 8.84M | psEnc->sStereo.predIx[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ], &psEnc->sStereo.mid_only_flags[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ], | 440 | 8.84M | MStargetRates_bps, TargetRate_bps, psEnc->state_Fxx[ 0 ].sCmn.speech_activity_Q8, encControl->toMono, | 441 | 8.84M | psEnc->state_Fxx[ 0 ].sCmn.fs_kHz, psEnc->state_Fxx[ 0 ].sCmn.frame_length ); | 442 | 8.84M | if( psEnc->sStereo.mid_only_flags[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ] == 0 ) { | 443 | | /* Reset side channel encoder memory for first frame with side coding */ | 444 | 7.36M | if( psEnc->prev_decode_only_middle == 1 ) { | 445 | 3.02k | silk_memset( &psEnc->state_Fxx[ 1 ].sShape, 0, sizeof( psEnc->state_Fxx[ 1 ].sShape ) ); | 446 | 3.02k | silk_memset( &psEnc->state_Fxx[ 1 ].sCmn.sNSQ, 0, sizeof( psEnc->state_Fxx[ 1 ].sCmn.sNSQ ) ); | 447 | 3.02k | silk_memset( psEnc->state_Fxx[ 1 ].sCmn.prev_NLSFq_Q15, 0, sizeof( psEnc->state_Fxx[ 1 ].sCmn.prev_NLSFq_Q15 ) ); | 448 | 3.02k | silk_memset( &psEnc->state_Fxx[ 1 ].sCmn.sLP.In_LP_State, 0, sizeof( psEnc->state_Fxx[ 1 ].sCmn.sLP.In_LP_State ) ); | 449 | 3.02k | psEnc->state_Fxx[ 1 ].sCmn.prevLag = 100; | 450 | 3.02k | psEnc->state_Fxx[ 1 ].sCmn.sNSQ.lagPrev = 100; | 451 | 3.02k | psEnc->state_Fxx[ 1 ].sShape.LastGainIndex = 10; | 452 | 3.02k | psEnc->state_Fxx[ 1 ].sCmn.prevSignalType = TYPE_NO_VOICE_ACTIVITY; | 453 | 3.02k | psEnc->state_Fxx[ 1 ].sCmn.sNSQ.prev_gain_Q16 = 65536; | 454 | 3.02k | psEnc->state_Fxx[ 1 ].sCmn.first_frame_after_reset = 1; | 455 | 3.02k | } | 456 | 7.36M | silk_encode_do_VAD_Fxx( &psEnc->state_Fxx[ 1 ], activity ); | 457 | 7.36M | } else { | 458 | 1.47M | psEnc->state_Fxx[ 1 ].sCmn.VAD_flags[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ] = 0; | 459 | 1.47M | } | 460 | 8.84M | if( !prefillFlag ) { | 461 | 8.83M | silk_stereo_encode_pred( psRangeEnc, psEnc->sStereo.predIx[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ] ); | 462 | 8.83M | if( psEnc->state_Fxx[ 1 ].sCmn.VAD_flags[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ] == 0 ) { | 463 | 8.59M | silk_stereo_encode_mid_only( psRangeEnc, psEnc->sStereo.mid_only_flags[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ] ); | 464 | 8.59M | } | 465 | 8.83M | } | 466 | 21.0M | } else { | 467 | | /* Buffering */ | 468 | 21.0M | silk_memcpy( psEnc->state_Fxx[ 0 ].sCmn.inputBuf, psEnc->sStereo.sMid, 2 * sizeof( opus_int16 ) ); | 469 | 21.0M | silk_memcpy( psEnc->sStereo.sMid, &psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ psEnc->state_Fxx[ 0 ].sCmn.frame_length ], 2 * sizeof( opus_int16 ) ); | 470 | 21.0M | } | 471 | 29.8M | silk_encode_do_VAD_Fxx( &psEnc->state_Fxx[ 0 ], activity ); | 472 | | | 473 | | /* Encode */ | 474 | 68.5M | for( n = 0; n < encControl->nChannelsInternal; n++ ) { | 475 | 38.7M | opus_int maxBits, useCBR; | 476 | | | 477 | | /* Handling rate constraints */ | 478 | 38.7M | maxBits = encControl->maxBits; | 479 | 38.7M | if( tot_blocks == 2 && curr_block == 0 ) { | 480 | 2.79M | maxBits = maxBits * 3 / 5; | 481 | 35.9M | } else if( tot_blocks == 3 ) { | 482 | 4.19M | if( curr_block == 0 ) { | 483 | 1.39M | maxBits = maxBits * 2 / 5; | 484 | 2.79M | } else if( curr_block == 1 ) { | 485 | 1.39M | maxBits = maxBits * 3 / 4; | 486 | 1.39M | } | 487 | 4.19M | } | 488 | 38.7M | useCBR = encControl->useCBR && curr_block == tot_blocks - 1; | 489 | | | 490 | 38.7M | if( encControl->nChannelsInternal == 1 ) { | 491 | 21.0M | channelRate_bps = TargetRate_bps; | 492 | 21.0M | } else { | 493 | 17.6M | channelRate_bps = MStargetRates_bps[ n ]; | 494 | 17.6M | if( n == 0 && MStargetRates_bps[ 1 ] > 0 ) { | 495 | 7.36M | useCBR = 0; | 496 | | /* Give mid up to 1/2 of the max bits for that frame */ | 497 | 7.36M | maxBits -= encControl->maxBits / ( tot_blocks * 2 ); | 498 | 7.36M | } | 499 | 17.6M | } | 500 | | | 501 | 38.7M | if( channelRate_bps > 0 ) { | 502 | 37.2M | opus_int condCoding; | 503 | | | 504 | 37.2M | silk_control_SNR( &psEnc->state_Fxx[ n ].sCmn, channelRate_bps ); | 505 | | | 506 | | /* Use independent coding if no previous frame available */ | 507 | 37.2M | if( psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded - n <= 0 ) { | 508 | 31.8M | condCoding = CODE_INDEPENDENTLY; | 509 | 31.8M | } else if( n > 0 && psEnc->prev_decode_only_middle ) { | 510 | | /* If we skipped a side frame in this packet, we don't | 511 | | need LTP scaling; the LTP state is well-defined. */ | 512 | 1.70k | condCoding = CODE_INDEPENDENTLY_NO_LTP_SCALING; | 513 | 5.39M | } else { | 514 | 5.39M | condCoding = CODE_CONDITIONALLY; | 515 | 5.39M | } | 516 | 37.2M | if( ( ret = silk_encode_frame_Fxx( &psEnc->state_Fxx[ n ], nBytesOut, psRangeEnc, condCoding, maxBits, useCBR ) ) != 0 ) { | 517 | 0 | silk_assert( 0 ); | 518 | 0 | } | 519 | 37.2M | } | 520 | 38.7M | psEnc->state_Fxx[ n ].sCmn.controlled_since_last_payload = 0; | 521 | 38.7M | psEnc->state_Fxx[ n ].sCmn.inputBufIx = 0; | 522 | 38.7M | psEnc->state_Fxx[ n ].sCmn.nFramesEncoded++; | 523 | 38.7M | } | 524 | 29.8M | psEnc->prev_decode_only_middle = psEnc->sStereo.mid_only_flags[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded - 1 ]; | 525 | | | 526 | | /* Insert VAD and FEC flags at beginning of bitstream */ | 527 | 29.8M | if( *nBytesOut > 0 && psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded == psEnc->state_Fxx[ 0 ].sCmn.nFramesPerPacket) { | 528 | 25.5M | flags = 0; | 529 | 58.6M | for( n = 0; n < encControl->nChannelsInternal; n++ ) { | 530 | 71.8M | for( i = 0; i < psEnc->state_Fxx[ n ].sCmn.nFramesPerPacket; i++ ) { | 531 | 38.7M | flags = silk_LSHIFT( flags, 1 ); | 532 | 38.7M | flags |= psEnc->state_Fxx[ n ].sCmn.VAD_flags[ i ]; | 533 | 38.7M | } | 534 | 33.1M | flags = silk_LSHIFT( flags, 1 ); | 535 | 33.1M | flags |= psEnc->state_Fxx[ n ].sCmn.LBRR_flag; | 536 | 33.1M | } | 537 | 25.5M | if( !prefillFlag ) { | 538 | 25.5M | ec_enc_patch_initial_bits( psRangeEnc, flags, ( psEnc->state_Fxx[ 0 ].sCmn.nFramesPerPacket + 1 ) * encControl->nChannelsInternal ); | 539 | 25.5M | } | 540 | | | 541 | | /* Return zero bytes if all channels DTXed */ | 542 | 25.5M | if( psEnc->state_Fxx[ 0 ].sCmn.inDTX && ( encControl->nChannelsInternal == 1 || psEnc->state_Fxx[ 1 ].sCmn.inDTX ) ) { | 543 | 8.37M | *nBytesOut = 0; | 544 | 8.37M | } | 545 | | | 546 | 25.5M | psEnc->nBitsExceeded += *nBytesOut * 8; | 547 | 25.5M | psEnc->nBitsExceeded -= silk_DIV32_16( silk_MUL( encControl->bitRate, encControl->payloadSize_ms ), 1000 ); | 548 | 25.5M | psEnc->nBitsExceeded = silk_LIMIT( psEnc->nBitsExceeded, 0, 10000 ); | 549 | | | 550 | | /* Update flag indicating if bandwidth switching is allowed */ | 551 | 25.5M | speech_act_thr_for_switch_Q8 = silk_SMLAWB( SILK_FIX_CONST( SPEECH_ACTIVITY_DTX_THRES, 8 ), | 552 | 25.5M | SILK_FIX_CONST( ( 1 - SPEECH_ACTIVITY_DTX_THRES ) / MAX_BANDWIDTH_SWITCH_DELAY_MS, 16 + 8 ), psEnc->timeSinceSwitchAllowed_ms ); | 553 | 25.5M | if( psEnc->state_Fxx[ 0 ].sCmn.speech_activity_Q8 < speech_act_thr_for_switch_Q8 ) { | 554 | 24.6M | psEnc->allowBandwidthSwitch = 1; | 555 | 24.6M | psEnc->timeSinceSwitchAllowed_ms = 0; | 556 | 24.6M | } else { | 557 | 860k | psEnc->allowBandwidthSwitch = 0; | 558 | 860k | psEnc->timeSinceSwitchAllowed_ms += encControl->payloadSize_ms; | 559 | 860k | } | 560 | 25.5M | } | 561 | | | 562 | 29.8M | if( nSamplesIn == 0 ) { | 563 | 25.5M | break; | 564 | 25.5M | } | 565 | 29.8M | } else { | 566 | 0 | break; | 567 | 0 | } | 568 | 4.32M | curr_block++; | 569 | 4.32M | } | 570 | | | 571 | 25.5M | psEnc->nPrevChannelsInternal = encControl->nChannelsInternal; | 572 | | | 573 | 25.5M | encControl->allowBandwidthSwitch = psEnc->allowBandwidthSwitch; | 574 | 25.5M | encControl->inWBmodeWithoutVariableLP = psEnc->state_Fxx[ 0 ].sCmn.fs_kHz == 16 && psEnc->state_Fxx[ 0 ].sCmn.sLP.mode == 0; | 575 | 25.5M | encControl->internalSampleRate = silk_SMULBB( psEnc->state_Fxx[ 0 ].sCmn.fs_kHz, 1000 ); | 576 | 25.5M | encControl->stereoWidth_Q14 = encControl->toMono ? 0 : psEnc->sStereo.smth_width_Q14; | 577 | 25.5M | if( prefillFlag ) { | 578 | 647 | encControl->payloadSize_ms = tmp_payloadSize_ms; | 579 | 647 | encControl->complexity = tmp_complexity; | 580 | 1.44k | for( n = 0; n < encControl->nChannelsInternal; n++ ) { | 581 | 801 | psEnc->state_Fxx[ n ].sCmn.controlled_since_last_payload = 0; | 582 | 801 | psEnc->state_Fxx[ n ].sCmn.prefillFlag = 0; | 583 | 801 | } | 584 | 647 | } | 585 | | | 586 | 25.5M | encControl->signalType = psEnc->state_Fxx[0].sCmn.indices.signalType; | 587 | 25.5M | encControl->offset = silk_Quantization_Offsets_Q10 | 588 | 25.5M | [ psEnc->state_Fxx[0].sCmn.indices.signalType >> 1 ] | 589 | 25.5M | [ psEnc->state_Fxx[0].sCmn.indices.quantOffsetType ]; | 590 | 25.5M | RESTORE_STACK; | 591 | 25.5M | return ret; | 592 | 25.5M | } |
Line | Count | Source | 154 | 25.5M | { | 155 | 25.5M | opus_int n, i, nBits, flags, tmp_payloadSize_ms = 0, tmp_complexity = 0, ret = 0; | 156 | 25.5M | opus_int nSamplesToBuffer, nSamplesToBufferMax, nBlocksOf10ms; | 157 | 25.5M | opus_int nSamplesFromInput = 0, nSamplesFromInputMax; | 158 | 25.5M | opus_int speech_act_thr_for_switch_Q8; | 159 | 25.5M | opus_int32 TargetRate_bps, MStargetRates_bps[ 2 ], channelRate_bps, LBRR_symbol, sum; | 160 | 25.5M | silk_encoder *psEnc = ( silk_encoder * )encState; | 161 | 25.5M | VARDECL( opus_int16, buf ); | 162 | 25.5M | opus_int transition, curr_block, tot_blocks; | 163 | 25.5M | SAVE_STACK; | 164 | | | 165 | 25.5M | if (encControl->reducedDependency) | 166 | 7.70M | { | 167 | 7.70M | psEnc->state_Fxx[0].sCmn.first_frame_after_reset = 1; | 168 | 7.70M | psEnc->state_Fxx[1].sCmn.first_frame_after_reset = 1; | 169 | 7.70M | } | 170 | 25.5M | psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded = psEnc->state_Fxx[ 1 ].sCmn.nFramesEncoded = 0; | 171 | | | 172 | | /* Check values in encoder control structure */ | 173 | 25.5M | if( ( ret = check_control_input( encControl ) ) != 0 ) { | 174 | 0 | celt_assert( 0 ); | 175 | 0 | RESTORE_STACK; | 176 | 0 | return ret; | 177 | 0 | } | 178 | | | 179 | 25.5M | encControl->switchReady = 0; | 180 | | | 181 | 25.5M | if( encControl->nChannelsInternal > psEnc->nChannelsInternal ) { | 182 | | /* Mono -> Stereo transition: init state of second channel and stereo state */ | 183 | 57.4k | ret += silk_init_encoder( &psEnc->state_Fxx[ 1 ], psEnc->state_Fxx[ 0 ].sCmn.arch ); | 184 | 57.4k | silk_memset( psEnc->sStereo.pred_prev_Q13, 0, sizeof( psEnc->sStereo.pred_prev_Q13 ) ); | 185 | 57.4k | silk_memset( psEnc->sStereo.sSide, 0, sizeof( psEnc->sStereo.sSide ) ); | 186 | 57.4k | psEnc->sStereo.mid_side_amp_Q0[ 0 ] = 0; | 187 | 57.4k | psEnc->sStereo.mid_side_amp_Q0[ 1 ] = 1; | 188 | 57.4k | psEnc->sStereo.mid_side_amp_Q0[ 2 ] = 0; | 189 | 57.4k | psEnc->sStereo.mid_side_amp_Q0[ 3 ] = 1; | 190 | 57.4k | psEnc->sStereo.width_prev_Q14 = 0; | 191 | 57.4k | psEnc->sStereo.smth_width_Q14 = SILK_FIX_CONST( 1, 14 ); | 192 | 57.4k | if( psEnc->nChannelsAPI == 2 ) { | 193 | 0 | silk_memcpy( &psEnc->state_Fxx[ 1 ].sCmn.resampler_state, &psEnc->state_Fxx[ 0 ].sCmn.resampler_state, sizeof( silk_resampler_state_struct ) ); | 194 | 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 ) ); | 195 | 0 | } | 196 | 57.4k | } | 197 | | | 198 | 25.5M | transition = (encControl->payloadSize_ms != psEnc->state_Fxx[ 0 ].sCmn.PacketSize_ms) || (psEnc->nChannelsInternal != encControl->nChannelsInternal); | 199 | | | 200 | 25.5M | psEnc->nChannelsAPI = encControl->nChannelsAPI; | 201 | 25.5M | psEnc->nChannelsInternal = encControl->nChannelsInternal; | 202 | | | 203 | 25.5M | nBlocksOf10ms = silk_DIV32( 100 * nSamplesIn, encControl->API_sampleRate ); | 204 | 25.5M | tot_blocks = ( nBlocksOf10ms > 1 ) ? nBlocksOf10ms >> 1 : 1; | 205 | 25.5M | curr_block = 0; | 206 | 25.5M | if( prefillFlag ) { | 207 | 647 | silk_LP_state save_LP; | 208 | | /* Only accept input length of 10 ms */ | 209 | 647 | if( nBlocksOf10ms != 1 ) { | 210 | 0 | celt_assert( 0 ); | 211 | 0 | RESTORE_STACK; | 212 | 0 | return SILK_ENC_INPUT_INVALID_NO_OF_SAMPLES; | 213 | 0 | } | 214 | 647 | if ( prefillFlag == 2 ) { | 215 | 203 | save_LP = psEnc->state_Fxx[ 0 ].sCmn.sLP; | 216 | | /* Save the sampling rate so the bandwidth switching code can keep handling transitions. */ | 217 | 203 | save_LP.saved_fs_kHz = psEnc->state_Fxx[ 0 ].sCmn.fs_kHz; | 218 | 203 | } | 219 | | /* Reset Encoder */ | 220 | 1.44k | for( n = 0; n < encControl->nChannelsInternal; n++ ) { | 221 | 801 | ret = silk_init_encoder( &psEnc->state_Fxx[ n ], psEnc->state_Fxx[ n ].sCmn.arch ); | 222 | | /* Restore the variable LP state. */ | 223 | 801 | if ( prefillFlag == 2 ) { | 224 | 245 | psEnc->state_Fxx[ n ].sCmn.sLP = save_LP; | 225 | 245 | } | 226 | 801 | celt_assert( !ret ); | 227 | 801 | } | 228 | 647 | tmp_payloadSize_ms = encControl->payloadSize_ms; | 229 | 647 | encControl->payloadSize_ms = 10; | 230 | 647 | tmp_complexity = encControl->complexity; | 231 | 647 | encControl->complexity = 0; | 232 | 1.44k | for( n = 0; n < encControl->nChannelsInternal; n++ ) { | 233 | 801 | psEnc->state_Fxx[ n ].sCmn.controlled_since_last_payload = 0; | 234 | 801 | psEnc->state_Fxx[ n ].sCmn.prefillFlag = 1; | 235 | 801 | } | 236 | 25.5M | } else { | 237 | | /* Only accept input lengths that are a multiple of 10 ms */ | 238 | 25.5M | if( nBlocksOf10ms * encControl->API_sampleRate != 100 * nSamplesIn || nSamplesIn < 0 ) { | 239 | 0 | celt_assert( 0 ); | 240 | 0 | RESTORE_STACK; | 241 | 0 | return SILK_ENC_INPUT_INVALID_NO_OF_SAMPLES; | 242 | 0 | } | 243 | | /* Make sure no more than one packet can be produced */ | 244 | 25.5M | if( 1000 * (opus_int32)nSamplesIn > encControl->payloadSize_ms * encControl->API_sampleRate ) { | 245 | 0 | celt_assert( 0 ); | 246 | 0 | RESTORE_STACK; | 247 | 0 | return SILK_ENC_INPUT_INVALID_NO_OF_SAMPLES; | 248 | 0 | } | 249 | 25.5M | } | 250 | | | 251 | 58.6M | for( n = 0; n < encControl->nChannelsInternal; n++ ) { | 252 | | /* Force the side channel to the same rate as the mid */ | 253 | 33.1M | opus_int force_fs_kHz = (n==1) ? psEnc->state_Fxx[0].sCmn.fs_kHz : 0; | 254 | 33.1M | if( ( ret = silk_control_encoder( &psEnc->state_Fxx[ n ], encControl, psEnc->allowBandwidthSwitch, n, force_fs_kHz ) ) != 0 ) { | 255 | 0 | silk_assert( 0 ); | 256 | 0 | RESTORE_STACK; | 257 | 0 | return ret; | 258 | 0 | } | 259 | 33.1M | if( psEnc->state_Fxx[n].sCmn.first_frame_after_reset || transition ) { | 260 | 23.5M | for( i = 0; i < psEnc->state_Fxx[ 0 ].sCmn.nFramesPerPacket; i++ ) { | 261 | 12.8M | psEnc->state_Fxx[ n ].sCmn.LBRR_flags[ i ] = 0; | 262 | 12.8M | } | 263 | 10.7M | } | 264 | 33.1M | psEnc->state_Fxx[ n ].sCmn.inDTX = psEnc->state_Fxx[ n ].sCmn.useDTX; | 265 | 33.1M | } | 266 | 25.5M | celt_assert( encControl->nChannelsInternal == 1 || psEnc->state_Fxx[ 0 ].sCmn.fs_kHz == psEnc->state_Fxx[ 1 ].sCmn.fs_kHz ); | 267 | | | 268 | | /* Input buffering/resampling and encoding */ | 269 | 25.5M | nSamplesToBufferMax = | 270 | 25.5M | 10 * nBlocksOf10ms * psEnc->state_Fxx[ 0 ].sCmn.fs_kHz; | 271 | 25.5M | nSamplesFromInputMax = | 272 | 25.5M | silk_DIV32_16( nSamplesToBufferMax * | 273 | 25.5M | psEnc->state_Fxx[ 0 ].sCmn.API_fs_Hz, | 274 | 25.5M | psEnc->state_Fxx[ 0 ].sCmn.fs_kHz * 1000 ); | 275 | 25.5M | ALLOC( buf, nSamplesFromInputMax, opus_int16 ); | 276 | 29.8M | while( 1 ) { | 277 | 29.8M | int curr_nBitsUsedLBRR = 0; | 278 | 29.8M | nSamplesToBuffer = psEnc->state_Fxx[ 0 ].sCmn.frame_length - psEnc->state_Fxx[ 0 ].sCmn.inputBufIx; | 279 | 29.8M | nSamplesToBuffer = silk_min( nSamplesToBuffer, nSamplesToBufferMax ); | 280 | 29.8M | nSamplesFromInput = silk_DIV32_16( nSamplesToBuffer * psEnc->state_Fxx[ 0 ].sCmn.API_fs_Hz, psEnc->state_Fxx[ 0 ].sCmn.fs_kHz * 1000 ); | 281 | | /* Resample and write to buffer */ | 282 | 29.8M | if( encControl->nChannelsAPI == 2 && encControl->nChannelsInternal == 2 ) { | 283 | 8.84M | opus_int id = psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded; | 284 | 2.65G | for( n = 0; n < nSamplesFromInput; n++ ) { | 285 | 2.64G | buf[ n ] = RES2INT16(samplesIn[ 2 * n ]); | 286 | 2.64G | } | 287 | | /* Making sure to start both resamplers from the same state when switching from mono to stereo */ | 288 | 8.84M | if( psEnc->nPrevChannelsInternal == 1 && id==0 ) { | 289 | 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)); | 290 | 0 | } | 291 | | | 292 | 8.84M | ret += silk_resampler( &psEnc->state_Fxx[ 0 ].sCmn.resampler_state, | 293 | 8.84M | &psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ psEnc->state_Fxx[ 0 ].sCmn.inputBufIx + 2 ], buf, nSamplesFromInput ); | 294 | 8.84M | psEnc->state_Fxx[ 0 ].sCmn.inputBufIx += nSamplesToBuffer; | 295 | | | 296 | 8.84M | nSamplesToBuffer = psEnc->state_Fxx[ 1 ].sCmn.frame_length - psEnc->state_Fxx[ 1 ].sCmn.inputBufIx; | 297 | 8.84M | nSamplesToBuffer = silk_min( nSamplesToBuffer, 10 * nBlocksOf10ms * psEnc->state_Fxx[ 1 ].sCmn.fs_kHz ); | 298 | 2.65G | for( n = 0; n < nSamplesFromInput; n++ ) { | 299 | 2.64G | buf[ n ] = RES2INT16(samplesIn[ 2 * n + 1 ]); | 300 | 2.64G | } | 301 | 8.84M | ret += silk_resampler( &psEnc->state_Fxx[ 1 ].sCmn.resampler_state, | 302 | 8.84M | &psEnc->state_Fxx[ 1 ].sCmn.inputBuf[ psEnc->state_Fxx[ 1 ].sCmn.inputBufIx + 2 ], buf, nSamplesFromInput ); | 303 | | | 304 | 8.84M | psEnc->state_Fxx[ 1 ].sCmn.inputBufIx += nSamplesToBuffer; | 305 | 21.0M | } else if( encControl->nChannelsAPI == 2 && encControl->nChannelsInternal == 1 ) { | 306 | | /* Combine left and right channels before resampling */ | 307 | 853M | for( n = 0; n < nSamplesFromInput; n++ ) { | 308 | 851M | sum = RES2INT16(samplesIn[ 2 * n ] + samplesIn[ 2 * n + 1 ]); | 309 | 851M | buf[ n ] = (opus_int16)silk_RSHIFT_ROUND( sum, 1 ); | 310 | 851M | } | 311 | 2.72M | ret += silk_resampler( &psEnc->state_Fxx[ 0 ].sCmn.resampler_state, | 312 | 2.72M | &psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ psEnc->state_Fxx[ 0 ].sCmn.inputBufIx + 2 ], buf, nSamplesFromInput ); | 313 | | /* On the first mono frame, average the results for the two resampler states */ | 314 | 2.72M | if( psEnc->nPrevChannelsInternal == 2 && psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded == 0 ) { | 315 | 0 | ret += silk_resampler( &psEnc->state_Fxx[ 1 ].sCmn.resampler_state, | 316 | 0 | &psEnc->state_Fxx[ 1 ].sCmn.inputBuf[ psEnc->state_Fxx[ 1 ].sCmn.inputBufIx + 2 ], buf, nSamplesFromInput ); | 317 | 0 | for( n = 0; n < psEnc->state_Fxx[ 0 ].sCmn.frame_length; n++ ) { | 318 | 0 | psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ psEnc->state_Fxx[ 0 ].sCmn.inputBufIx+n+2 ] = | 319 | 0 | silk_RSHIFT(psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ psEnc->state_Fxx[ 0 ].sCmn.inputBufIx+n+2 ] | 320 | 0 | + psEnc->state_Fxx[ 1 ].sCmn.inputBuf[ psEnc->state_Fxx[ 1 ].sCmn.inputBufIx+n+2 ], 1); | 321 | 0 | } | 322 | 0 | } | 323 | 2.72M | psEnc->state_Fxx[ 0 ].sCmn.inputBufIx += nSamplesToBuffer; | 324 | 18.3M | } else { | 325 | 18.3M | celt_assert( encControl->nChannelsAPI == 1 && encControl->nChannelsInternal == 1 ); | 326 | 5.76G | for( n = 0; n < nSamplesFromInput; n++ ) { | 327 | 5.74G | buf[n] = RES2INT16(samplesIn[n]); | 328 | 5.74G | } | 329 | 18.3M | ret += silk_resampler( &psEnc->state_Fxx[ 0 ].sCmn.resampler_state, | 330 | 18.3M | &psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ psEnc->state_Fxx[ 0 ].sCmn.inputBufIx + 2 ], buf, nSamplesFromInput ); | 331 | 18.3M | psEnc->state_Fxx[ 0 ].sCmn.inputBufIx += nSamplesToBuffer; | 332 | 18.3M | } | 333 | | | 334 | 29.8M | samplesIn += nSamplesFromInput * encControl->nChannelsAPI; | 335 | 29.8M | nSamplesIn -= nSamplesFromInput; | 336 | | | 337 | | /* Default */ | 338 | 29.8M | psEnc->allowBandwidthSwitch = 0; | 339 | | | 340 | | /* Silk encoder */ | 341 | 29.8M | if( psEnc->state_Fxx[ 0 ].sCmn.inputBufIx >= psEnc->state_Fxx[ 0 ].sCmn.frame_length ) { | 342 | | /* Enough data in input buffer, so encode */ | 343 | 29.8M | celt_assert( psEnc->state_Fxx[ 0 ].sCmn.inputBufIx == psEnc->state_Fxx[ 0 ].sCmn.frame_length ); | 344 | 29.8M | celt_assert( encControl->nChannelsInternal == 1 || psEnc->state_Fxx[ 1 ].sCmn.inputBufIx == psEnc->state_Fxx[ 1 ].sCmn.frame_length ); | 345 | | | 346 | | /* Deal with LBRR data */ | 347 | 29.8M | if( psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded == 0 && !prefillFlag ) { | 348 | | /* Create space at start of payload for VAD and FEC flags */ | 349 | 25.5M | opus_uint8 iCDF[ 2 ] = { 0, 0 }; | 350 | 25.5M | iCDF[ 0 ] = 256 - silk_RSHIFT( 256, ( psEnc->state_Fxx[ 0 ].sCmn.nFramesPerPacket + 1 ) * encControl->nChannelsInternal ); | 351 | 25.5M | ec_enc_icdf( psRangeEnc, 0, iCDF, 8 ); | 352 | 25.5M | curr_nBitsUsedLBRR = ec_tell( psRangeEnc ); | 353 | | | 354 | | /* Encode any LBRR data from previous packet */ | 355 | | /* Encode LBRR flags */ | 356 | 58.6M | for( n = 0; n < encControl->nChannelsInternal; n++ ) { | 357 | 33.1M | LBRR_symbol = 0; | 358 | 71.8M | for( i = 0; i < psEnc->state_Fxx[ n ].sCmn.nFramesPerPacket; i++ ) { | 359 | 38.7M | LBRR_symbol |= silk_LSHIFT( psEnc->state_Fxx[ n ].sCmn.LBRR_flags[ i ], i ); | 360 | 38.7M | } | 361 | 33.1M | psEnc->state_Fxx[ n ].sCmn.LBRR_flag = LBRR_symbol > 0 ? 1 : 0; | 362 | 33.1M | if( LBRR_symbol && psEnc->state_Fxx[ n ].sCmn.nFramesPerPacket > 1 ) { | 363 | 52.6k | ec_enc_icdf( psRangeEnc, LBRR_symbol - 1, silk_LBRR_flags_iCDF_ptr[ psEnc->state_Fxx[ n ].sCmn.nFramesPerPacket - 2 ], 8 ); | 364 | 52.6k | } | 365 | 33.1M | } | 366 | | | 367 | | /* Code LBRR indices and excitation signals */ | 368 | 55.4M | for( i = 0; i < psEnc->state_Fxx[ 0 ].sCmn.nFramesPerPacket; i++ ) { | 369 | 68.5M | for( n = 0; n < encControl->nChannelsInternal; n++ ) { | 370 | 38.7M | if( psEnc->state_Fxx[ n ].sCmn.LBRR_flags[ i ] ) { | 371 | 302k | opus_int condCoding; | 372 | | | 373 | 302k | if( encControl->nChannelsInternal == 2 && n == 0 ) { | 374 | 80.8k | silk_stereo_encode_pred( psRangeEnc, psEnc->sStereo.predIx[ i ] ); | 375 | | /* For LBRR data there's no need to code the mid-only flag if the side-channel LBRR flag is set */ | 376 | 80.8k | if( psEnc->state_Fxx[ 1 ].sCmn.LBRR_flags[ i ] == 0 ) { | 377 | 29.8k | silk_stereo_encode_mid_only( psRangeEnc, psEnc->sStereo.mid_only_flags[ i ] ); | 378 | 29.8k | } | 379 | 80.8k | } | 380 | | /* Use conditional coding if previous frame available */ | 381 | 302k | if( i > 0 && psEnc->state_Fxx[ n ].sCmn.LBRR_flags[ i - 1 ] ) { | 382 | 59.3k | condCoding = CODE_CONDITIONALLY; | 383 | 243k | } else { | 384 | 243k | condCoding = CODE_INDEPENDENTLY; | 385 | 243k | } | 386 | 302k | silk_encode_indices( &psEnc->state_Fxx[ n ].sCmn, psRangeEnc, i, 1, condCoding ); | 387 | 302k | silk_encode_pulses( psRangeEnc, psEnc->state_Fxx[ n ].sCmn.indices_LBRR[i].signalType, psEnc->state_Fxx[ n ].sCmn.indices_LBRR[i].quantOffsetType, | 388 | 302k | psEnc->state_Fxx[ n ].sCmn.pulses_LBRR[ i ], psEnc->state_Fxx[ n ].sCmn.frame_length ); | 389 | 302k | } | 390 | 38.7M | } | 391 | 29.8M | } | 392 | | | 393 | | /* Reset LBRR flags */ | 394 | 58.6M | for( n = 0; n < encControl->nChannelsInternal; n++ ) { | 395 | 33.1M | silk_memset( psEnc->state_Fxx[ n ].sCmn.LBRR_flags, 0, sizeof( psEnc->state_Fxx[ n ].sCmn.LBRR_flags ) ); | 396 | 33.1M | } | 397 | 25.5M | curr_nBitsUsedLBRR = ec_tell( psRangeEnc ) - curr_nBitsUsedLBRR; | 398 | 25.5M | } | 399 | | | 400 | 29.8M | silk_HP_variable_cutoff( psEnc->state_Fxx ); | 401 | | | 402 | | /* Total target bits for packet */ | 403 | 29.8M | nBits = silk_DIV32_16( silk_MUL( encControl->bitRate, encControl->payloadSize_ms ), 1000 ); | 404 | | /* Subtract bits used for LBRR */ | 405 | 29.8M | if( !prefillFlag ) { | 406 | | /* psEnc->nBitsUsedLBRR is an exponential moving average of the LBRR usage, | 407 | | except that for the first LBRR frame it does no averaging and for the first | 408 | | frame after after LBRR, it goes back to zero immediately. */ | 409 | 29.8M | if ( curr_nBitsUsedLBRR < 10 ) { | 410 | 29.6M | psEnc->nBitsUsedLBRR = 0; | 411 | 29.6M | } else if ( psEnc->nBitsUsedLBRR < 10) { | 412 | 59.2k | psEnc->nBitsUsedLBRR = curr_nBitsUsedLBRR; | 413 | 142k | } else { | 414 | 142k | psEnc->nBitsUsedLBRR = ( psEnc->nBitsUsedLBRR + curr_nBitsUsedLBRR ) / 2; | 415 | 142k | } | 416 | 29.8M | nBits -= psEnc->nBitsUsedLBRR; | 417 | 29.8M | } | 418 | | /* Divide by number of uncoded frames left in packet */ | 419 | 29.8M | nBits = silk_DIV32_16( nBits, psEnc->state_Fxx[ 0 ].sCmn.nFramesPerPacket ); | 420 | | /* Convert to bits/second */ | 421 | 29.8M | if( encControl->payloadSize_ms == 10 ) { | 422 | 11.8M | TargetRate_bps = silk_SMULBB( nBits, 100 ); | 423 | 18.0M | } else { | 424 | 18.0M | TargetRate_bps = silk_SMULBB( nBits, 50 ); | 425 | 18.0M | } | 426 | | /* Subtract fraction of bits in excess of target in previous frames and packets */ | 427 | 29.8M | TargetRate_bps -= silk_DIV32_16( silk_MUL( psEnc->nBitsExceeded, 1000 ), BITRESERVOIR_DECAY_TIME_MS ); | 428 | 29.8M | if( !prefillFlag && psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded > 0 ) { | 429 | | /* Compare actual vs target bits so far in this packet */ | 430 | 4.32M | opus_int32 bitsBalance = ec_tell( psRangeEnc ) - psEnc->nBitsUsedLBRR - nBits * psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded; | 431 | 4.32M | TargetRate_bps -= silk_DIV32_16( silk_MUL( bitsBalance, 1000 ), BITRESERVOIR_DECAY_TIME_MS ); | 432 | 4.32M | } | 433 | | /* Never exceed input bitrate */ | 434 | 29.8M | TargetRate_bps = silk_LIMIT( TargetRate_bps, encControl->bitRate, 5000 ); | 435 | | | 436 | | /* Convert Left/Right to Mid/Side */ | 437 | 29.8M | if( encControl->nChannelsInternal == 2 ) { | 438 | 8.84M | silk_stereo_LR_to_MS( &psEnc->sStereo, &psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ 2 ], &psEnc->state_Fxx[ 1 ].sCmn.inputBuf[ 2 ], | 439 | 8.84M | psEnc->sStereo.predIx[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ], &psEnc->sStereo.mid_only_flags[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ], | 440 | 8.84M | MStargetRates_bps, TargetRate_bps, psEnc->state_Fxx[ 0 ].sCmn.speech_activity_Q8, encControl->toMono, | 441 | 8.84M | psEnc->state_Fxx[ 0 ].sCmn.fs_kHz, psEnc->state_Fxx[ 0 ].sCmn.frame_length ); | 442 | 8.84M | if( psEnc->sStereo.mid_only_flags[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ] == 0 ) { | 443 | | /* Reset side channel encoder memory for first frame with side coding */ | 444 | 7.36M | if( psEnc->prev_decode_only_middle == 1 ) { | 445 | 3.02k | silk_memset( &psEnc->state_Fxx[ 1 ].sShape, 0, sizeof( psEnc->state_Fxx[ 1 ].sShape ) ); | 446 | 3.02k | silk_memset( &psEnc->state_Fxx[ 1 ].sCmn.sNSQ, 0, sizeof( psEnc->state_Fxx[ 1 ].sCmn.sNSQ ) ); | 447 | 3.02k | silk_memset( psEnc->state_Fxx[ 1 ].sCmn.prev_NLSFq_Q15, 0, sizeof( psEnc->state_Fxx[ 1 ].sCmn.prev_NLSFq_Q15 ) ); | 448 | 3.02k | silk_memset( &psEnc->state_Fxx[ 1 ].sCmn.sLP.In_LP_State, 0, sizeof( psEnc->state_Fxx[ 1 ].sCmn.sLP.In_LP_State ) ); | 449 | 3.02k | psEnc->state_Fxx[ 1 ].sCmn.prevLag = 100; | 450 | 3.02k | psEnc->state_Fxx[ 1 ].sCmn.sNSQ.lagPrev = 100; | 451 | 3.02k | psEnc->state_Fxx[ 1 ].sShape.LastGainIndex = 10; | 452 | 3.02k | psEnc->state_Fxx[ 1 ].sCmn.prevSignalType = TYPE_NO_VOICE_ACTIVITY; | 453 | 3.02k | psEnc->state_Fxx[ 1 ].sCmn.sNSQ.prev_gain_Q16 = 65536; | 454 | 3.02k | psEnc->state_Fxx[ 1 ].sCmn.first_frame_after_reset = 1; | 455 | 3.02k | } | 456 | 7.36M | silk_encode_do_VAD_Fxx( &psEnc->state_Fxx[ 1 ], activity ); | 457 | 7.36M | } else { | 458 | 1.47M | psEnc->state_Fxx[ 1 ].sCmn.VAD_flags[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ] = 0; | 459 | 1.47M | } | 460 | 8.84M | if( !prefillFlag ) { | 461 | 8.83M | silk_stereo_encode_pred( psRangeEnc, psEnc->sStereo.predIx[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ] ); | 462 | 8.83M | if( psEnc->state_Fxx[ 1 ].sCmn.VAD_flags[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ] == 0 ) { | 463 | 8.59M | silk_stereo_encode_mid_only( psRangeEnc, psEnc->sStereo.mid_only_flags[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ] ); | 464 | 8.59M | } | 465 | 8.83M | } | 466 | 21.0M | } else { | 467 | | /* Buffering */ | 468 | 21.0M | silk_memcpy( psEnc->state_Fxx[ 0 ].sCmn.inputBuf, psEnc->sStereo.sMid, 2 * sizeof( opus_int16 ) ); | 469 | 21.0M | silk_memcpy( psEnc->sStereo.sMid, &psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ psEnc->state_Fxx[ 0 ].sCmn.frame_length ], 2 * sizeof( opus_int16 ) ); | 470 | 21.0M | } | 471 | 29.8M | silk_encode_do_VAD_Fxx( &psEnc->state_Fxx[ 0 ], activity ); | 472 | | | 473 | | /* Encode */ | 474 | 68.5M | for( n = 0; n < encControl->nChannelsInternal; n++ ) { | 475 | 38.7M | opus_int maxBits, useCBR; | 476 | | | 477 | | /* Handling rate constraints */ | 478 | 38.7M | maxBits = encControl->maxBits; | 479 | 38.7M | if( tot_blocks == 2 && curr_block == 0 ) { | 480 | 2.79M | maxBits = maxBits * 3 / 5; | 481 | 35.9M | } else if( tot_blocks == 3 ) { | 482 | 4.19M | if( curr_block == 0 ) { | 483 | 1.39M | maxBits = maxBits * 2 / 5; | 484 | 2.79M | } else if( curr_block == 1 ) { | 485 | 1.39M | maxBits = maxBits * 3 / 4; | 486 | 1.39M | } | 487 | 4.19M | } | 488 | 38.7M | useCBR = encControl->useCBR && curr_block == tot_blocks - 1; | 489 | | | 490 | 38.7M | if( encControl->nChannelsInternal == 1 ) { | 491 | 21.0M | channelRate_bps = TargetRate_bps; | 492 | 21.0M | } else { | 493 | 17.6M | channelRate_bps = MStargetRates_bps[ n ]; | 494 | 17.6M | if( n == 0 && MStargetRates_bps[ 1 ] > 0 ) { | 495 | 7.36M | useCBR = 0; | 496 | | /* Give mid up to 1/2 of the max bits for that frame */ | 497 | 7.36M | maxBits -= encControl->maxBits / ( tot_blocks * 2 ); | 498 | 7.36M | } | 499 | 17.6M | } | 500 | | | 501 | 38.7M | if( channelRate_bps > 0 ) { | 502 | 37.2M | opus_int condCoding; | 503 | | | 504 | 37.2M | silk_control_SNR( &psEnc->state_Fxx[ n ].sCmn, channelRate_bps ); | 505 | | | 506 | | /* Use independent coding if no previous frame available */ | 507 | 37.2M | if( psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded - n <= 0 ) { | 508 | 31.8M | condCoding = CODE_INDEPENDENTLY; | 509 | 31.8M | } else if( n > 0 && psEnc->prev_decode_only_middle ) { | 510 | | /* If we skipped a side frame in this packet, we don't | 511 | | need LTP scaling; the LTP state is well-defined. */ | 512 | 1.70k | condCoding = CODE_INDEPENDENTLY_NO_LTP_SCALING; | 513 | 5.39M | } else { | 514 | 5.39M | condCoding = CODE_CONDITIONALLY; | 515 | 5.39M | } | 516 | 37.2M | if( ( ret = silk_encode_frame_Fxx( &psEnc->state_Fxx[ n ], nBytesOut, psRangeEnc, condCoding, maxBits, useCBR ) ) != 0 ) { | 517 | 0 | silk_assert( 0 ); | 518 | 0 | } | 519 | 37.2M | } | 520 | 38.7M | psEnc->state_Fxx[ n ].sCmn.controlled_since_last_payload = 0; | 521 | 38.7M | psEnc->state_Fxx[ n ].sCmn.inputBufIx = 0; | 522 | 38.7M | psEnc->state_Fxx[ n ].sCmn.nFramesEncoded++; | 523 | 38.7M | } | 524 | 29.8M | psEnc->prev_decode_only_middle = psEnc->sStereo.mid_only_flags[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded - 1 ]; | 525 | | | 526 | | /* Insert VAD and FEC flags at beginning of bitstream */ | 527 | 29.8M | if( *nBytesOut > 0 && psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded == psEnc->state_Fxx[ 0 ].sCmn.nFramesPerPacket) { | 528 | 25.5M | flags = 0; | 529 | 58.6M | for( n = 0; n < encControl->nChannelsInternal; n++ ) { | 530 | 71.8M | for( i = 0; i < psEnc->state_Fxx[ n ].sCmn.nFramesPerPacket; i++ ) { | 531 | 38.7M | flags = silk_LSHIFT( flags, 1 ); | 532 | 38.7M | flags |= psEnc->state_Fxx[ n ].sCmn.VAD_flags[ i ]; | 533 | 38.7M | } | 534 | 33.1M | flags = silk_LSHIFT( flags, 1 ); | 535 | 33.1M | flags |= psEnc->state_Fxx[ n ].sCmn.LBRR_flag; | 536 | 33.1M | } | 537 | 25.5M | if( !prefillFlag ) { | 538 | 25.5M | ec_enc_patch_initial_bits( psRangeEnc, flags, ( psEnc->state_Fxx[ 0 ].sCmn.nFramesPerPacket + 1 ) * encControl->nChannelsInternal ); | 539 | 25.5M | } | 540 | | | 541 | | /* Return zero bytes if all channels DTXed */ | 542 | 25.5M | if( psEnc->state_Fxx[ 0 ].sCmn.inDTX && ( encControl->nChannelsInternal == 1 || psEnc->state_Fxx[ 1 ].sCmn.inDTX ) ) { | 543 | 8.37M | *nBytesOut = 0; | 544 | 8.37M | } | 545 | | | 546 | 25.5M | psEnc->nBitsExceeded += *nBytesOut * 8; | 547 | 25.5M | psEnc->nBitsExceeded -= silk_DIV32_16( silk_MUL( encControl->bitRate, encControl->payloadSize_ms ), 1000 ); | 548 | 25.5M | psEnc->nBitsExceeded = silk_LIMIT( psEnc->nBitsExceeded, 0, 10000 ); | 549 | | | 550 | | /* Update flag indicating if bandwidth switching is allowed */ | 551 | 25.5M | speech_act_thr_for_switch_Q8 = silk_SMLAWB( SILK_FIX_CONST( SPEECH_ACTIVITY_DTX_THRES, 8 ), | 552 | 25.5M | SILK_FIX_CONST( ( 1 - SPEECH_ACTIVITY_DTX_THRES ) / MAX_BANDWIDTH_SWITCH_DELAY_MS, 16 + 8 ), psEnc->timeSinceSwitchAllowed_ms ); | 553 | 25.5M | if( psEnc->state_Fxx[ 0 ].sCmn.speech_activity_Q8 < speech_act_thr_for_switch_Q8 ) { | 554 | 24.6M | psEnc->allowBandwidthSwitch = 1; | 555 | 24.6M | psEnc->timeSinceSwitchAllowed_ms = 0; | 556 | 24.6M | } else { | 557 | 860k | psEnc->allowBandwidthSwitch = 0; | 558 | 860k | psEnc->timeSinceSwitchAllowed_ms += encControl->payloadSize_ms; | 559 | 860k | } | 560 | 25.5M | } | 561 | | | 562 | 29.8M | if( nSamplesIn == 0 ) { | 563 | 25.5M | break; | 564 | 25.5M | } | 565 | 29.8M | } else { | 566 | 0 | break; | 567 | 0 | } | 568 | 4.32M | curr_block++; | 569 | 4.32M | } | 570 | | | 571 | 25.5M | psEnc->nPrevChannelsInternal = encControl->nChannelsInternal; | 572 | | | 573 | 25.5M | encControl->allowBandwidthSwitch = psEnc->allowBandwidthSwitch; | 574 | 25.5M | encControl->inWBmodeWithoutVariableLP = psEnc->state_Fxx[ 0 ].sCmn.fs_kHz == 16 && psEnc->state_Fxx[ 0 ].sCmn.sLP.mode == 0; | 575 | 25.5M | encControl->internalSampleRate = silk_SMULBB( psEnc->state_Fxx[ 0 ].sCmn.fs_kHz, 1000 ); | 576 | 25.5M | encControl->stereoWidth_Q14 = encControl->toMono ? 0 : psEnc->sStereo.smth_width_Q14; | 577 | 25.5M | if( prefillFlag ) { | 578 | 647 | encControl->payloadSize_ms = tmp_payloadSize_ms; | 579 | 647 | encControl->complexity = tmp_complexity; | 580 | 1.44k | for( n = 0; n < encControl->nChannelsInternal; n++ ) { | 581 | 801 | psEnc->state_Fxx[ n ].sCmn.controlled_since_last_payload = 0; | 582 | 801 | psEnc->state_Fxx[ n ].sCmn.prefillFlag = 0; | 583 | 801 | } | 584 | 647 | } | 585 | | | 586 | 25.5M | encControl->signalType = psEnc->state_Fxx[0].sCmn.indices.signalType; | 587 | 25.5M | encControl->offset = silk_Quantization_Offsets_Q10 | 588 | 25.5M | [ psEnc->state_Fxx[0].sCmn.indices.signalType >> 1 ] | 589 | 25.5M | [ psEnc->state_Fxx[0].sCmn.indices.quantOffsetType ]; | 590 | 25.5M | RESTORE_STACK; | 591 | 25.5M | return ret; | 592 | 25.5M | } |
Line | Count | Source | 154 | 25.5M | { | 155 | 25.5M | opus_int n, i, nBits, flags, tmp_payloadSize_ms = 0, tmp_complexity = 0, ret = 0; | 156 | 25.5M | opus_int nSamplesToBuffer, nSamplesToBufferMax, nBlocksOf10ms; | 157 | 25.5M | opus_int nSamplesFromInput = 0, nSamplesFromInputMax; | 158 | 25.5M | opus_int speech_act_thr_for_switch_Q8; | 159 | 25.5M | opus_int32 TargetRate_bps, MStargetRates_bps[ 2 ], channelRate_bps, LBRR_symbol, sum; | 160 | 25.5M | silk_encoder *psEnc = ( silk_encoder * )encState; | 161 | 25.5M | VARDECL( opus_int16, buf ); | 162 | 25.5M | opus_int transition, curr_block, tot_blocks; | 163 | 25.5M | SAVE_STACK; | 164 | | | 165 | 25.5M | if (encControl->reducedDependency) | 166 | 7.70M | { | 167 | 7.70M | psEnc->state_Fxx[0].sCmn.first_frame_after_reset = 1; | 168 | 7.70M | psEnc->state_Fxx[1].sCmn.first_frame_after_reset = 1; | 169 | 7.70M | } | 170 | 25.5M | psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded = psEnc->state_Fxx[ 1 ].sCmn.nFramesEncoded = 0; | 171 | | | 172 | | /* Check values in encoder control structure */ | 173 | 25.5M | if( ( ret = check_control_input( encControl ) ) != 0 ) { | 174 | 0 | celt_assert( 0 ); | 175 | 0 | RESTORE_STACK; | 176 | 0 | return ret; | 177 | 0 | } | 178 | | | 179 | 25.5M | encControl->switchReady = 0; | 180 | | | 181 | 25.5M | if( encControl->nChannelsInternal > psEnc->nChannelsInternal ) { | 182 | | /* Mono -> Stereo transition: init state of second channel and stereo state */ | 183 | 57.4k | ret += silk_init_encoder( &psEnc->state_Fxx[ 1 ], psEnc->state_Fxx[ 0 ].sCmn.arch ); | 184 | 57.4k | silk_memset( psEnc->sStereo.pred_prev_Q13, 0, sizeof( psEnc->sStereo.pred_prev_Q13 ) ); | 185 | 57.4k | silk_memset( psEnc->sStereo.sSide, 0, sizeof( psEnc->sStereo.sSide ) ); | 186 | 57.4k | psEnc->sStereo.mid_side_amp_Q0[ 0 ] = 0; | 187 | 57.4k | psEnc->sStereo.mid_side_amp_Q0[ 1 ] = 1; | 188 | 57.4k | psEnc->sStereo.mid_side_amp_Q0[ 2 ] = 0; | 189 | 57.4k | psEnc->sStereo.mid_side_amp_Q0[ 3 ] = 1; | 190 | 57.4k | psEnc->sStereo.width_prev_Q14 = 0; | 191 | 57.4k | psEnc->sStereo.smth_width_Q14 = SILK_FIX_CONST( 1, 14 ); | 192 | 57.4k | if( psEnc->nChannelsAPI == 2 ) { | 193 | 0 | silk_memcpy( &psEnc->state_Fxx[ 1 ].sCmn.resampler_state, &psEnc->state_Fxx[ 0 ].sCmn.resampler_state, sizeof( silk_resampler_state_struct ) ); | 194 | 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 ) ); | 195 | 0 | } | 196 | 57.4k | } | 197 | | | 198 | 25.5M | transition = (encControl->payloadSize_ms != psEnc->state_Fxx[ 0 ].sCmn.PacketSize_ms) || (psEnc->nChannelsInternal != encControl->nChannelsInternal); | 199 | | | 200 | 25.5M | psEnc->nChannelsAPI = encControl->nChannelsAPI; | 201 | 25.5M | psEnc->nChannelsInternal = encControl->nChannelsInternal; | 202 | | | 203 | 25.5M | nBlocksOf10ms = silk_DIV32( 100 * nSamplesIn, encControl->API_sampleRate ); | 204 | 25.5M | tot_blocks = ( nBlocksOf10ms > 1 ) ? nBlocksOf10ms >> 1 : 1; | 205 | 25.5M | curr_block = 0; | 206 | 25.5M | if( prefillFlag ) { | 207 | 647 | silk_LP_state save_LP; | 208 | | /* Only accept input length of 10 ms */ | 209 | 647 | if( nBlocksOf10ms != 1 ) { | 210 | 0 | celt_assert( 0 ); | 211 | 0 | RESTORE_STACK; | 212 | 0 | return SILK_ENC_INPUT_INVALID_NO_OF_SAMPLES; | 213 | 0 | } | 214 | 647 | if ( prefillFlag == 2 ) { | 215 | 203 | save_LP = psEnc->state_Fxx[ 0 ].sCmn.sLP; | 216 | | /* Save the sampling rate so the bandwidth switching code can keep handling transitions. */ | 217 | 203 | save_LP.saved_fs_kHz = psEnc->state_Fxx[ 0 ].sCmn.fs_kHz; | 218 | 203 | } | 219 | | /* Reset Encoder */ | 220 | 1.44k | for( n = 0; n < encControl->nChannelsInternal; n++ ) { | 221 | 801 | ret = silk_init_encoder( &psEnc->state_Fxx[ n ], psEnc->state_Fxx[ n ].sCmn.arch ); | 222 | | /* Restore the variable LP state. */ | 223 | 801 | if ( prefillFlag == 2 ) { | 224 | 245 | psEnc->state_Fxx[ n ].sCmn.sLP = save_LP; | 225 | 245 | } | 226 | 801 | celt_assert( !ret ); | 227 | 801 | } | 228 | 647 | tmp_payloadSize_ms = encControl->payloadSize_ms; | 229 | 647 | encControl->payloadSize_ms = 10; | 230 | 647 | tmp_complexity = encControl->complexity; | 231 | 647 | encControl->complexity = 0; | 232 | 1.44k | for( n = 0; n < encControl->nChannelsInternal; n++ ) { | 233 | 801 | psEnc->state_Fxx[ n ].sCmn.controlled_since_last_payload = 0; | 234 | 801 | psEnc->state_Fxx[ n ].sCmn.prefillFlag = 1; | 235 | 801 | } | 236 | 25.5M | } else { | 237 | | /* Only accept input lengths that are a multiple of 10 ms */ | 238 | 25.5M | if( nBlocksOf10ms * encControl->API_sampleRate != 100 * nSamplesIn || nSamplesIn < 0 ) { | 239 | 0 | celt_assert( 0 ); | 240 | 0 | RESTORE_STACK; | 241 | 0 | return SILK_ENC_INPUT_INVALID_NO_OF_SAMPLES; | 242 | 0 | } | 243 | | /* Make sure no more than one packet can be produced */ | 244 | 25.5M | if( 1000 * (opus_int32)nSamplesIn > encControl->payloadSize_ms * encControl->API_sampleRate ) { | 245 | 0 | celt_assert( 0 ); | 246 | 0 | RESTORE_STACK; | 247 | 0 | return SILK_ENC_INPUT_INVALID_NO_OF_SAMPLES; | 248 | 0 | } | 249 | 25.5M | } | 250 | | | 251 | 58.6M | for( n = 0; n < encControl->nChannelsInternal; n++ ) { | 252 | | /* Force the side channel to the same rate as the mid */ | 253 | 33.1M | opus_int force_fs_kHz = (n==1) ? psEnc->state_Fxx[0].sCmn.fs_kHz : 0; | 254 | 33.1M | if( ( ret = silk_control_encoder( &psEnc->state_Fxx[ n ], encControl, psEnc->allowBandwidthSwitch, n, force_fs_kHz ) ) != 0 ) { | 255 | 0 | silk_assert( 0 ); | 256 | 0 | RESTORE_STACK; | 257 | 0 | return ret; | 258 | 0 | } | 259 | 33.1M | if( psEnc->state_Fxx[n].sCmn.first_frame_after_reset || transition ) { | 260 | 23.5M | for( i = 0; i < psEnc->state_Fxx[ 0 ].sCmn.nFramesPerPacket; i++ ) { | 261 | 12.8M | psEnc->state_Fxx[ n ].sCmn.LBRR_flags[ i ] = 0; | 262 | 12.8M | } | 263 | 10.7M | } | 264 | 33.1M | psEnc->state_Fxx[ n ].sCmn.inDTX = psEnc->state_Fxx[ n ].sCmn.useDTX; | 265 | 33.1M | } | 266 | 25.5M | celt_assert( encControl->nChannelsInternal == 1 || psEnc->state_Fxx[ 0 ].sCmn.fs_kHz == psEnc->state_Fxx[ 1 ].sCmn.fs_kHz ); | 267 | | | 268 | | /* Input buffering/resampling and encoding */ | 269 | 25.5M | nSamplesToBufferMax = | 270 | 25.5M | 10 * nBlocksOf10ms * psEnc->state_Fxx[ 0 ].sCmn.fs_kHz; | 271 | 25.5M | nSamplesFromInputMax = | 272 | 25.5M | silk_DIV32_16( nSamplesToBufferMax * | 273 | 25.5M | psEnc->state_Fxx[ 0 ].sCmn.API_fs_Hz, | 274 | 25.5M | psEnc->state_Fxx[ 0 ].sCmn.fs_kHz * 1000 ); | 275 | 25.5M | ALLOC( buf, nSamplesFromInputMax, opus_int16 ); | 276 | 29.8M | while( 1 ) { | 277 | 29.8M | int curr_nBitsUsedLBRR = 0; | 278 | 29.8M | nSamplesToBuffer = psEnc->state_Fxx[ 0 ].sCmn.frame_length - psEnc->state_Fxx[ 0 ].sCmn.inputBufIx; | 279 | 29.8M | nSamplesToBuffer = silk_min( nSamplesToBuffer, nSamplesToBufferMax ); | 280 | 29.8M | nSamplesFromInput = silk_DIV32_16( nSamplesToBuffer * psEnc->state_Fxx[ 0 ].sCmn.API_fs_Hz, psEnc->state_Fxx[ 0 ].sCmn.fs_kHz * 1000 ); | 281 | | /* Resample and write to buffer */ | 282 | 29.8M | if( encControl->nChannelsAPI == 2 && encControl->nChannelsInternal == 2 ) { | 283 | 8.84M | opus_int id = psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded; | 284 | 2.65G | for( n = 0; n < nSamplesFromInput; n++ ) { | 285 | 2.64G | buf[ n ] = RES2INT16(samplesIn[ 2 * n ]); | 286 | 2.64G | } | 287 | | /* Making sure to start both resamplers from the same state when switching from mono to stereo */ | 288 | 8.84M | if( psEnc->nPrevChannelsInternal == 1 && id==0 ) { | 289 | 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)); | 290 | 0 | } | 291 | | | 292 | 8.84M | ret += silk_resampler( &psEnc->state_Fxx[ 0 ].sCmn.resampler_state, | 293 | 8.84M | &psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ psEnc->state_Fxx[ 0 ].sCmn.inputBufIx + 2 ], buf, nSamplesFromInput ); | 294 | 8.84M | psEnc->state_Fxx[ 0 ].sCmn.inputBufIx += nSamplesToBuffer; | 295 | | | 296 | 8.84M | nSamplesToBuffer = psEnc->state_Fxx[ 1 ].sCmn.frame_length - psEnc->state_Fxx[ 1 ].sCmn.inputBufIx; | 297 | 8.84M | nSamplesToBuffer = silk_min( nSamplesToBuffer, 10 * nBlocksOf10ms * psEnc->state_Fxx[ 1 ].sCmn.fs_kHz ); | 298 | 2.65G | for( n = 0; n < nSamplesFromInput; n++ ) { | 299 | 2.64G | buf[ n ] = RES2INT16(samplesIn[ 2 * n + 1 ]); | 300 | 2.64G | } | 301 | 8.84M | ret += silk_resampler( &psEnc->state_Fxx[ 1 ].sCmn.resampler_state, | 302 | 8.84M | &psEnc->state_Fxx[ 1 ].sCmn.inputBuf[ psEnc->state_Fxx[ 1 ].sCmn.inputBufIx + 2 ], buf, nSamplesFromInput ); | 303 | | | 304 | 8.84M | psEnc->state_Fxx[ 1 ].sCmn.inputBufIx += nSamplesToBuffer; | 305 | 21.0M | } else if( encControl->nChannelsAPI == 2 && encControl->nChannelsInternal == 1 ) { | 306 | | /* Combine left and right channels before resampling */ | 307 | 853M | for( n = 0; n < nSamplesFromInput; n++ ) { | 308 | 851M | sum = RES2INT16(samplesIn[ 2 * n ] + samplesIn[ 2 * n + 1 ]); | 309 | 851M | buf[ n ] = (opus_int16)silk_RSHIFT_ROUND( sum, 1 ); | 310 | 851M | } | 311 | 2.72M | ret += silk_resampler( &psEnc->state_Fxx[ 0 ].sCmn.resampler_state, | 312 | 2.72M | &psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ psEnc->state_Fxx[ 0 ].sCmn.inputBufIx + 2 ], buf, nSamplesFromInput ); | 313 | | /* On the first mono frame, average the results for the two resampler states */ | 314 | 2.72M | if( psEnc->nPrevChannelsInternal == 2 && psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded == 0 ) { | 315 | 0 | ret += silk_resampler( &psEnc->state_Fxx[ 1 ].sCmn.resampler_state, | 316 | 0 | &psEnc->state_Fxx[ 1 ].sCmn.inputBuf[ psEnc->state_Fxx[ 1 ].sCmn.inputBufIx + 2 ], buf, nSamplesFromInput ); | 317 | 0 | for( n = 0; n < psEnc->state_Fxx[ 0 ].sCmn.frame_length; n++ ) { | 318 | 0 | psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ psEnc->state_Fxx[ 0 ].sCmn.inputBufIx+n+2 ] = | 319 | 0 | silk_RSHIFT(psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ psEnc->state_Fxx[ 0 ].sCmn.inputBufIx+n+2 ] | 320 | 0 | + psEnc->state_Fxx[ 1 ].sCmn.inputBuf[ psEnc->state_Fxx[ 1 ].sCmn.inputBufIx+n+2 ], 1); | 321 | 0 | } | 322 | 0 | } | 323 | 2.72M | psEnc->state_Fxx[ 0 ].sCmn.inputBufIx += nSamplesToBuffer; | 324 | 18.3M | } else { | 325 | 18.3M | celt_assert( encControl->nChannelsAPI == 1 && encControl->nChannelsInternal == 1 ); | 326 | 5.76G | for( n = 0; n < nSamplesFromInput; n++ ) { | 327 | 5.74G | buf[n] = RES2INT16(samplesIn[n]); | 328 | 5.74G | } | 329 | 18.3M | ret += silk_resampler( &psEnc->state_Fxx[ 0 ].sCmn.resampler_state, | 330 | 18.3M | &psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ psEnc->state_Fxx[ 0 ].sCmn.inputBufIx + 2 ], buf, nSamplesFromInput ); | 331 | 18.3M | psEnc->state_Fxx[ 0 ].sCmn.inputBufIx += nSamplesToBuffer; | 332 | 18.3M | } | 333 | | | 334 | 29.8M | samplesIn += nSamplesFromInput * encControl->nChannelsAPI; | 335 | 29.8M | nSamplesIn -= nSamplesFromInput; | 336 | | | 337 | | /* Default */ | 338 | 29.8M | psEnc->allowBandwidthSwitch = 0; | 339 | | | 340 | | /* Silk encoder */ | 341 | 29.8M | if( psEnc->state_Fxx[ 0 ].sCmn.inputBufIx >= psEnc->state_Fxx[ 0 ].sCmn.frame_length ) { | 342 | | /* Enough data in input buffer, so encode */ | 343 | 29.8M | celt_assert( psEnc->state_Fxx[ 0 ].sCmn.inputBufIx == psEnc->state_Fxx[ 0 ].sCmn.frame_length ); | 344 | 29.8M | celt_assert( encControl->nChannelsInternal == 1 || psEnc->state_Fxx[ 1 ].sCmn.inputBufIx == psEnc->state_Fxx[ 1 ].sCmn.frame_length ); | 345 | | | 346 | | /* Deal with LBRR data */ | 347 | 29.8M | if( psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded == 0 && !prefillFlag ) { | 348 | | /* Create space at start of payload for VAD and FEC flags */ | 349 | 25.5M | opus_uint8 iCDF[ 2 ] = { 0, 0 }; | 350 | 25.5M | iCDF[ 0 ] = 256 - silk_RSHIFT( 256, ( psEnc->state_Fxx[ 0 ].sCmn.nFramesPerPacket + 1 ) * encControl->nChannelsInternal ); | 351 | 25.5M | ec_enc_icdf( psRangeEnc, 0, iCDF, 8 ); | 352 | 25.5M | curr_nBitsUsedLBRR = ec_tell( psRangeEnc ); | 353 | | | 354 | | /* Encode any LBRR data from previous packet */ | 355 | | /* Encode LBRR flags */ | 356 | 58.6M | for( n = 0; n < encControl->nChannelsInternal; n++ ) { | 357 | 33.1M | LBRR_symbol = 0; | 358 | 71.8M | for( i = 0; i < psEnc->state_Fxx[ n ].sCmn.nFramesPerPacket; i++ ) { | 359 | 38.7M | LBRR_symbol |= silk_LSHIFT( psEnc->state_Fxx[ n ].sCmn.LBRR_flags[ i ], i ); | 360 | 38.7M | } | 361 | 33.1M | psEnc->state_Fxx[ n ].sCmn.LBRR_flag = LBRR_symbol > 0 ? 1 : 0; | 362 | 33.1M | if( LBRR_symbol && psEnc->state_Fxx[ n ].sCmn.nFramesPerPacket > 1 ) { | 363 | 52.6k | ec_enc_icdf( psRangeEnc, LBRR_symbol - 1, silk_LBRR_flags_iCDF_ptr[ psEnc->state_Fxx[ n ].sCmn.nFramesPerPacket - 2 ], 8 ); | 364 | 52.6k | } | 365 | 33.1M | } | 366 | | | 367 | | /* Code LBRR indices and excitation signals */ | 368 | 55.4M | for( i = 0; i < psEnc->state_Fxx[ 0 ].sCmn.nFramesPerPacket; i++ ) { | 369 | 68.5M | for( n = 0; n < encControl->nChannelsInternal; n++ ) { | 370 | 38.7M | if( psEnc->state_Fxx[ n ].sCmn.LBRR_flags[ i ] ) { | 371 | 302k | opus_int condCoding; | 372 | | | 373 | 302k | if( encControl->nChannelsInternal == 2 && n == 0 ) { | 374 | 80.8k | silk_stereo_encode_pred( psRangeEnc, psEnc->sStereo.predIx[ i ] ); | 375 | | /* For LBRR data there's no need to code the mid-only flag if the side-channel LBRR flag is set */ | 376 | 80.8k | if( psEnc->state_Fxx[ 1 ].sCmn.LBRR_flags[ i ] == 0 ) { | 377 | 29.8k | silk_stereo_encode_mid_only( psRangeEnc, psEnc->sStereo.mid_only_flags[ i ] ); | 378 | 29.8k | } | 379 | 80.8k | } | 380 | | /* Use conditional coding if previous frame available */ | 381 | 302k | if( i > 0 && psEnc->state_Fxx[ n ].sCmn.LBRR_flags[ i - 1 ] ) { | 382 | 59.3k | condCoding = CODE_CONDITIONALLY; | 383 | 243k | } else { | 384 | 243k | condCoding = CODE_INDEPENDENTLY; | 385 | 243k | } | 386 | 302k | silk_encode_indices( &psEnc->state_Fxx[ n ].sCmn, psRangeEnc, i, 1, condCoding ); | 387 | 302k | silk_encode_pulses( psRangeEnc, psEnc->state_Fxx[ n ].sCmn.indices_LBRR[i].signalType, psEnc->state_Fxx[ n ].sCmn.indices_LBRR[i].quantOffsetType, | 388 | 302k | psEnc->state_Fxx[ n ].sCmn.pulses_LBRR[ i ], psEnc->state_Fxx[ n ].sCmn.frame_length ); | 389 | 302k | } | 390 | 38.7M | } | 391 | 29.8M | } | 392 | | | 393 | | /* Reset LBRR flags */ | 394 | 58.6M | for( n = 0; n < encControl->nChannelsInternal; n++ ) { | 395 | 33.1M | silk_memset( psEnc->state_Fxx[ n ].sCmn.LBRR_flags, 0, sizeof( psEnc->state_Fxx[ n ].sCmn.LBRR_flags ) ); | 396 | 33.1M | } | 397 | 25.5M | curr_nBitsUsedLBRR = ec_tell( psRangeEnc ) - curr_nBitsUsedLBRR; | 398 | 25.5M | } | 399 | | | 400 | 29.8M | silk_HP_variable_cutoff( psEnc->state_Fxx ); | 401 | | | 402 | | /* Total target bits for packet */ | 403 | 29.8M | nBits = silk_DIV32_16( silk_MUL( encControl->bitRate, encControl->payloadSize_ms ), 1000 ); | 404 | | /* Subtract bits used for LBRR */ | 405 | 29.8M | if( !prefillFlag ) { | 406 | | /* psEnc->nBitsUsedLBRR is an exponential moving average of the LBRR usage, | 407 | | except that for the first LBRR frame it does no averaging and for the first | 408 | | frame after after LBRR, it goes back to zero immediately. */ | 409 | 29.8M | if ( curr_nBitsUsedLBRR < 10 ) { | 410 | 29.6M | psEnc->nBitsUsedLBRR = 0; | 411 | 29.6M | } else if ( psEnc->nBitsUsedLBRR < 10) { | 412 | 59.2k | psEnc->nBitsUsedLBRR = curr_nBitsUsedLBRR; | 413 | 142k | } else { | 414 | 142k | psEnc->nBitsUsedLBRR = ( psEnc->nBitsUsedLBRR + curr_nBitsUsedLBRR ) / 2; | 415 | 142k | } | 416 | 29.8M | nBits -= psEnc->nBitsUsedLBRR; | 417 | 29.8M | } | 418 | | /* Divide by number of uncoded frames left in packet */ | 419 | 29.8M | nBits = silk_DIV32_16( nBits, psEnc->state_Fxx[ 0 ].sCmn.nFramesPerPacket ); | 420 | | /* Convert to bits/second */ | 421 | 29.8M | if( encControl->payloadSize_ms == 10 ) { | 422 | 11.8M | TargetRate_bps = silk_SMULBB( nBits, 100 ); | 423 | 18.0M | } else { | 424 | 18.0M | TargetRate_bps = silk_SMULBB( nBits, 50 ); | 425 | 18.0M | } | 426 | | /* Subtract fraction of bits in excess of target in previous frames and packets */ | 427 | 29.8M | TargetRate_bps -= silk_DIV32_16( silk_MUL( psEnc->nBitsExceeded, 1000 ), BITRESERVOIR_DECAY_TIME_MS ); | 428 | 29.8M | if( !prefillFlag && psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded > 0 ) { | 429 | | /* Compare actual vs target bits so far in this packet */ | 430 | 4.32M | opus_int32 bitsBalance = ec_tell( psRangeEnc ) - psEnc->nBitsUsedLBRR - nBits * psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded; | 431 | 4.32M | TargetRate_bps -= silk_DIV32_16( silk_MUL( bitsBalance, 1000 ), BITRESERVOIR_DECAY_TIME_MS ); | 432 | 4.32M | } | 433 | | /* Never exceed input bitrate */ | 434 | 29.8M | TargetRate_bps = silk_LIMIT( TargetRate_bps, encControl->bitRate, 5000 ); | 435 | | | 436 | | /* Convert Left/Right to Mid/Side */ | 437 | 29.8M | if( encControl->nChannelsInternal == 2 ) { | 438 | 8.84M | silk_stereo_LR_to_MS( &psEnc->sStereo, &psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ 2 ], &psEnc->state_Fxx[ 1 ].sCmn.inputBuf[ 2 ], | 439 | 8.84M | psEnc->sStereo.predIx[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ], &psEnc->sStereo.mid_only_flags[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ], | 440 | 8.84M | MStargetRates_bps, TargetRate_bps, psEnc->state_Fxx[ 0 ].sCmn.speech_activity_Q8, encControl->toMono, | 441 | 8.84M | psEnc->state_Fxx[ 0 ].sCmn.fs_kHz, psEnc->state_Fxx[ 0 ].sCmn.frame_length ); | 442 | 8.84M | if( psEnc->sStereo.mid_only_flags[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ] == 0 ) { | 443 | | /* Reset side channel encoder memory for first frame with side coding */ | 444 | 7.36M | if( psEnc->prev_decode_only_middle == 1 ) { | 445 | 3.02k | silk_memset( &psEnc->state_Fxx[ 1 ].sShape, 0, sizeof( psEnc->state_Fxx[ 1 ].sShape ) ); | 446 | 3.02k | silk_memset( &psEnc->state_Fxx[ 1 ].sCmn.sNSQ, 0, sizeof( psEnc->state_Fxx[ 1 ].sCmn.sNSQ ) ); | 447 | 3.02k | silk_memset( psEnc->state_Fxx[ 1 ].sCmn.prev_NLSFq_Q15, 0, sizeof( psEnc->state_Fxx[ 1 ].sCmn.prev_NLSFq_Q15 ) ); | 448 | 3.02k | silk_memset( &psEnc->state_Fxx[ 1 ].sCmn.sLP.In_LP_State, 0, sizeof( psEnc->state_Fxx[ 1 ].sCmn.sLP.In_LP_State ) ); | 449 | 3.02k | psEnc->state_Fxx[ 1 ].sCmn.prevLag = 100; | 450 | 3.02k | psEnc->state_Fxx[ 1 ].sCmn.sNSQ.lagPrev = 100; | 451 | 3.02k | psEnc->state_Fxx[ 1 ].sShape.LastGainIndex = 10; | 452 | 3.02k | psEnc->state_Fxx[ 1 ].sCmn.prevSignalType = TYPE_NO_VOICE_ACTIVITY; | 453 | 3.02k | psEnc->state_Fxx[ 1 ].sCmn.sNSQ.prev_gain_Q16 = 65536; | 454 | 3.02k | psEnc->state_Fxx[ 1 ].sCmn.first_frame_after_reset = 1; | 455 | 3.02k | } | 456 | 7.36M | silk_encode_do_VAD_Fxx( &psEnc->state_Fxx[ 1 ], activity ); | 457 | 7.36M | } else { | 458 | 1.47M | psEnc->state_Fxx[ 1 ].sCmn.VAD_flags[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ] = 0; | 459 | 1.47M | } | 460 | 8.84M | if( !prefillFlag ) { | 461 | 8.83M | silk_stereo_encode_pred( psRangeEnc, psEnc->sStereo.predIx[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ] ); | 462 | 8.83M | if( psEnc->state_Fxx[ 1 ].sCmn.VAD_flags[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ] == 0 ) { | 463 | 8.59M | silk_stereo_encode_mid_only( psRangeEnc, psEnc->sStereo.mid_only_flags[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded ] ); | 464 | 8.59M | } | 465 | 8.83M | } | 466 | 21.0M | } else { | 467 | | /* Buffering */ | 468 | 21.0M | silk_memcpy( psEnc->state_Fxx[ 0 ].sCmn.inputBuf, psEnc->sStereo.sMid, 2 * sizeof( opus_int16 ) ); | 469 | 21.0M | silk_memcpy( psEnc->sStereo.sMid, &psEnc->state_Fxx[ 0 ].sCmn.inputBuf[ psEnc->state_Fxx[ 0 ].sCmn.frame_length ], 2 * sizeof( opus_int16 ) ); | 470 | 21.0M | } | 471 | 29.8M | silk_encode_do_VAD_Fxx( &psEnc->state_Fxx[ 0 ], activity ); | 472 | | | 473 | | /* Encode */ | 474 | 68.5M | for( n = 0; n < encControl->nChannelsInternal; n++ ) { | 475 | 38.7M | opus_int maxBits, useCBR; | 476 | | | 477 | | /* Handling rate constraints */ | 478 | 38.7M | maxBits = encControl->maxBits; | 479 | 38.7M | if( tot_blocks == 2 && curr_block == 0 ) { | 480 | 2.79M | maxBits = maxBits * 3 / 5; | 481 | 35.9M | } else if( tot_blocks == 3 ) { | 482 | 4.19M | if( curr_block == 0 ) { | 483 | 1.39M | maxBits = maxBits * 2 / 5; | 484 | 2.79M | } else if( curr_block == 1 ) { | 485 | 1.39M | maxBits = maxBits * 3 / 4; | 486 | 1.39M | } | 487 | 4.19M | } | 488 | 38.7M | useCBR = encControl->useCBR && curr_block == tot_blocks - 1; | 489 | | | 490 | 38.7M | if( encControl->nChannelsInternal == 1 ) { | 491 | 21.0M | channelRate_bps = TargetRate_bps; | 492 | 21.0M | } else { | 493 | 17.6M | channelRate_bps = MStargetRates_bps[ n ]; | 494 | 17.6M | if( n == 0 && MStargetRates_bps[ 1 ] > 0 ) { | 495 | 7.36M | useCBR = 0; | 496 | | /* Give mid up to 1/2 of the max bits for that frame */ | 497 | 7.36M | maxBits -= encControl->maxBits / ( tot_blocks * 2 ); | 498 | 7.36M | } | 499 | 17.6M | } | 500 | | | 501 | 38.7M | if( channelRate_bps > 0 ) { | 502 | 37.2M | opus_int condCoding; | 503 | | | 504 | 37.2M | silk_control_SNR( &psEnc->state_Fxx[ n ].sCmn, channelRate_bps ); | 505 | | | 506 | | /* Use independent coding if no previous frame available */ | 507 | 37.2M | if( psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded - n <= 0 ) { | 508 | 31.8M | condCoding = CODE_INDEPENDENTLY; | 509 | 31.8M | } else if( n > 0 && psEnc->prev_decode_only_middle ) { | 510 | | /* If we skipped a side frame in this packet, we don't | 511 | | need LTP scaling; the LTP state is well-defined. */ | 512 | 1.70k | condCoding = CODE_INDEPENDENTLY_NO_LTP_SCALING; | 513 | 5.39M | } else { | 514 | 5.39M | condCoding = CODE_CONDITIONALLY; | 515 | 5.39M | } | 516 | 37.2M | if( ( ret = silk_encode_frame_Fxx( &psEnc->state_Fxx[ n ], nBytesOut, psRangeEnc, condCoding, maxBits, useCBR ) ) != 0 ) { | 517 | 0 | silk_assert( 0 ); | 518 | 0 | } | 519 | 37.2M | } | 520 | 38.7M | psEnc->state_Fxx[ n ].sCmn.controlled_since_last_payload = 0; | 521 | 38.7M | psEnc->state_Fxx[ n ].sCmn.inputBufIx = 0; | 522 | 38.7M | psEnc->state_Fxx[ n ].sCmn.nFramesEncoded++; | 523 | 38.7M | } | 524 | 29.8M | psEnc->prev_decode_only_middle = psEnc->sStereo.mid_only_flags[ psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded - 1 ]; | 525 | | | 526 | | /* Insert VAD and FEC flags at beginning of bitstream */ | 527 | 29.8M | if( *nBytesOut > 0 && psEnc->state_Fxx[ 0 ].sCmn.nFramesEncoded == psEnc->state_Fxx[ 0 ].sCmn.nFramesPerPacket) { | 528 | 25.5M | flags = 0; | 529 | 58.6M | for( n = 0; n < encControl->nChannelsInternal; n++ ) { | 530 | 71.8M | for( i = 0; i < psEnc->state_Fxx[ n ].sCmn.nFramesPerPacket; i++ ) { | 531 | 38.7M | flags = silk_LSHIFT( flags, 1 ); | 532 | 38.7M | flags |= psEnc->state_Fxx[ n ].sCmn.VAD_flags[ i ]; | 533 | 38.7M | } | 534 | 33.1M | flags = silk_LSHIFT( flags, 1 ); | 535 | 33.1M | flags |= psEnc->state_Fxx[ n ].sCmn.LBRR_flag; | 536 | 33.1M | } | 537 | 25.5M | if( !prefillFlag ) { | 538 | 25.5M | ec_enc_patch_initial_bits( psRangeEnc, flags, ( psEnc->state_Fxx[ 0 ].sCmn.nFramesPerPacket + 1 ) * encControl->nChannelsInternal ); | 539 | 25.5M | } | 540 | | | 541 | | /* Return zero bytes if all channels DTXed */ | 542 | 25.5M | if( psEnc->state_Fxx[ 0 ].sCmn.inDTX && ( encControl->nChannelsInternal == 1 || psEnc->state_Fxx[ 1 ].sCmn.inDTX ) ) { | 543 | 8.37M | *nBytesOut = 0; | 544 | 8.37M | } | 545 | | | 546 | 25.5M | psEnc->nBitsExceeded += *nBytesOut * 8; | 547 | 25.5M | psEnc->nBitsExceeded -= silk_DIV32_16( silk_MUL( encControl->bitRate, encControl->payloadSize_ms ), 1000 ); | 548 | 25.5M | psEnc->nBitsExceeded = silk_LIMIT( psEnc->nBitsExceeded, 0, 10000 ); | 549 | | | 550 | | /* Update flag indicating if bandwidth switching is allowed */ | 551 | 25.5M | speech_act_thr_for_switch_Q8 = silk_SMLAWB( SILK_FIX_CONST( SPEECH_ACTIVITY_DTX_THRES, 8 ), | 552 | 25.5M | SILK_FIX_CONST( ( 1 - SPEECH_ACTIVITY_DTX_THRES ) / MAX_BANDWIDTH_SWITCH_DELAY_MS, 16 + 8 ), psEnc->timeSinceSwitchAllowed_ms ); | 553 | 25.5M | if( psEnc->state_Fxx[ 0 ].sCmn.speech_activity_Q8 < speech_act_thr_for_switch_Q8 ) { | 554 | 24.6M | psEnc->allowBandwidthSwitch = 1; | 555 | 24.6M | psEnc->timeSinceSwitchAllowed_ms = 0; | 556 | 24.6M | } else { | 557 | 860k | psEnc->allowBandwidthSwitch = 0; | 558 | 860k | psEnc->timeSinceSwitchAllowed_ms += encControl->payloadSize_ms; | 559 | 860k | } | 560 | 25.5M | } | 561 | | | 562 | 29.8M | if( nSamplesIn == 0 ) { | 563 | 25.5M | break; | 564 | 25.5M | } | 565 | 29.8M | } else { | 566 | 0 | break; | 567 | 0 | } | 568 | 4.32M | curr_block++; | 569 | 4.32M | } | 570 | | | 571 | 25.5M | psEnc->nPrevChannelsInternal = encControl->nChannelsInternal; | 572 | | | 573 | 25.5M | encControl->allowBandwidthSwitch = psEnc->allowBandwidthSwitch; | 574 | 25.5M | encControl->inWBmodeWithoutVariableLP = psEnc->state_Fxx[ 0 ].sCmn.fs_kHz == 16 && psEnc->state_Fxx[ 0 ].sCmn.sLP.mode == 0; | 575 | 25.5M | encControl->internalSampleRate = silk_SMULBB( psEnc->state_Fxx[ 0 ].sCmn.fs_kHz, 1000 ); | 576 | 25.5M | encControl->stereoWidth_Q14 = encControl->toMono ? 0 : psEnc->sStereo.smth_width_Q14; | 577 | 25.5M | if( prefillFlag ) { | 578 | 647 | encControl->payloadSize_ms = tmp_payloadSize_ms; | 579 | 647 | encControl->complexity = tmp_complexity; | 580 | 1.44k | for( n = 0; n < encControl->nChannelsInternal; n++ ) { | 581 | 801 | psEnc->state_Fxx[ n ].sCmn.controlled_since_last_payload = 0; | 582 | 801 | psEnc->state_Fxx[ n ].sCmn.prefillFlag = 0; | 583 | 801 | } | 584 | 647 | } | 585 | | | 586 | 25.5M | encControl->signalType = psEnc->state_Fxx[0].sCmn.indices.signalType; | 587 | 25.5M | encControl->offset = silk_Quantization_Offsets_Q10 | 588 | 25.5M | [ psEnc->state_Fxx[0].sCmn.indices.signalType >> 1 ] | 589 | 25.5M | [ psEnc->state_Fxx[0].sCmn.indices.quantOffsetType ]; | 590 | 25.5M | RESTORE_STACK; | 591 | 25.5M | return ret; | 592 | 25.5M | } |
|