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 "API.h" |
32 | | #include "main.h" |
33 | | #include "stack_alloc.h" |
34 | | #include "os_support.h" |
35 | | |
36 | | #ifdef ENABLE_OSCE |
37 | | #include "osce.h" |
38 | | #include "osce_structs.h" |
39 | | #endif |
40 | | |
41 | | /************************/ |
42 | | /* Decoder Super Struct */ |
43 | | /************************/ |
44 | | typedef struct { |
45 | | silk_decoder_state channel_state[ DECODER_NUM_CHANNELS ]; |
46 | | stereo_dec_state sStereo; |
47 | | opus_int nChannelsAPI; |
48 | | opus_int nChannelsInternal; |
49 | | opus_int prev_decode_only_middle; |
50 | | #ifdef ENABLE_OSCE |
51 | | OSCEModel osce_model; |
52 | | #endif |
53 | | } silk_decoder; |
54 | | |
55 | | /*********************/ |
56 | | /* Decoder functions */ |
57 | | /*********************/ |
58 | | |
59 | | |
60 | | |
61 | | opus_int silk_LoadOSCEModels(void *decState, const unsigned char *data, int len) |
62 | 0 | { |
63 | | #ifdef ENABLE_OSCE |
64 | | opus_int ret = SILK_NO_ERROR; |
65 | | |
66 | | ret = osce_load_models(&((silk_decoder *)decState)->osce_model, data, len); |
67 | | ((silk_decoder *)decState)->osce_model.loaded = (ret == 0); |
68 | | return ret; |
69 | | #else |
70 | 0 | (void) decState; |
71 | 0 | (void) data; |
72 | 0 | (void) len; |
73 | 0 | return SILK_NO_ERROR; |
74 | 0 | #endif |
75 | 0 | } |
76 | | |
77 | | opus_int silk_Get_Decoder_Size( /* O Returns error code */ |
78 | | opus_int *decSizeBytes /* O Number of bytes in SILK decoder state */ |
79 | | ) |
80 | 0 | { |
81 | 0 | opus_int ret = SILK_NO_ERROR; |
82 | |
|
83 | 0 | *decSizeBytes = sizeof( silk_decoder ); |
84 | |
|
85 | 0 | return ret; |
86 | 0 | } |
87 | | |
88 | | /* Reset decoder state */ |
89 | | opus_int silk_ResetDecoder( /* O Returns error code */ |
90 | | void *decState /* I/O State */ |
91 | | ) |
92 | 0 | { |
93 | 0 | opus_int n, ret = SILK_NO_ERROR; |
94 | 0 | silk_decoder_state *channel_state = ((silk_decoder *)decState)->channel_state; |
95 | |
|
96 | 0 | for( n = 0; n < DECODER_NUM_CHANNELS; n++ ) { |
97 | 0 | ret = silk_reset_decoder( &channel_state[ n ] ); |
98 | 0 | } |
99 | 0 | silk_memset(&((silk_decoder *)decState)->sStereo, 0, sizeof(((silk_decoder *)decState)->sStereo)); |
100 | | /* Not strictly needed, but it's cleaner that way */ |
101 | 0 | ((silk_decoder *)decState)->prev_decode_only_middle = 0; |
102 | |
|
103 | 0 | return ret; |
104 | 0 | } |
105 | | |
106 | | |
107 | | opus_int silk_InitDecoder( /* O Returns error code */ |
108 | | void *decState /* I/O State */ |
109 | | ) |
110 | 0 | { |
111 | 0 | opus_int n, ret = SILK_NO_ERROR; |
112 | 0 | silk_decoder_state *channel_state = ((silk_decoder *)decState)->channel_state; |
113 | | #ifdef ENABLE_OSCE |
114 | | ((silk_decoder *)decState)->osce_model.loaded = 0; |
115 | | #endif |
116 | 0 | #ifndef USE_WEIGHTS_FILE |
117 | | /* load osce models */ |
118 | 0 | silk_LoadOSCEModels(decState, NULL, 0); |
119 | 0 | #endif |
120 | |
|
121 | 0 | for( n = 0; n < DECODER_NUM_CHANNELS; n++ ) { |
122 | 0 | ret = silk_init_decoder( &channel_state[ n ] ); |
123 | 0 | } |
124 | 0 | silk_memset(&((silk_decoder *)decState)->sStereo, 0, sizeof(((silk_decoder *)decState)->sStereo)); |
125 | | /* Not strictly needed, but it's cleaner that way */ |
126 | 0 | ((silk_decoder *)decState)->prev_decode_only_middle = 0; |
127 | |
|
128 | 0 | return ret; |
129 | 0 | } |
130 | | |
131 | | /* Decode a frame */ |
132 | | opus_int silk_Decode( /* O Returns error code */ |
133 | | void* decState, /* I/O State */ |
134 | | silk_DecControlStruct* decControl, /* I/O Control Structure */ |
135 | | opus_int lostFlag, /* I 0: no loss, 1 loss, 2 decode fec */ |
136 | | opus_int newPacketFlag, /* I Indicates first decoder call for this packet */ |
137 | | ec_dec *psRangeDec, /* I/O Compressor data structure */ |
138 | | opus_int16 *samplesOut, /* O Decoded output speech vector */ |
139 | | opus_int32 *nSamplesOut, /* O Number of samples decoded */ |
140 | | #ifdef ENABLE_DEEP_PLC |
141 | | LPCNetPLCState *lpcnet, |
142 | | #endif |
143 | | int arch /* I Run-time architecture */ |
144 | | ) |
145 | 0 | { |
146 | 0 | opus_int i, n, decode_only_middle = 0, ret = SILK_NO_ERROR; |
147 | 0 | opus_int32 nSamplesOutDec, LBRR_symbol; |
148 | 0 | opus_int16 *samplesOut1_tmp[ 2 ]; |
149 | 0 | VARDECL( opus_int16, samplesOut1_tmp_storage1 ); |
150 | 0 | VARDECL( opus_int16, samplesOut1_tmp_storage2 ); |
151 | 0 | VARDECL( opus_int16, samplesOut2_tmp ); |
152 | 0 | opus_int32 MS_pred_Q13[ 2 ] = { 0 }; |
153 | 0 | opus_int16 *resample_out_ptr; |
154 | 0 | silk_decoder *psDec = ( silk_decoder * )decState; |
155 | 0 | silk_decoder_state *channel_state = psDec->channel_state; |
156 | 0 | opus_int has_side; |
157 | 0 | opus_int stereo_to_mono; |
158 | 0 | int delay_stack_alloc; |
159 | 0 | SAVE_STACK; |
160 | |
|
161 | 0 | celt_assert( decControl->nChannelsInternal == 1 || decControl->nChannelsInternal == 2 ); |
162 | | |
163 | | /**********************************/ |
164 | | /* Test if first frame in payload */ |
165 | | /**********************************/ |
166 | 0 | if( newPacketFlag ) { |
167 | 0 | for( n = 0; n < decControl->nChannelsInternal; n++ ) { |
168 | 0 | channel_state[ n ].nFramesDecoded = 0; /* Used to count frames in packet */ |
169 | 0 | } |
170 | 0 | } |
171 | | |
172 | | /* If Mono -> Stereo transition in bitstream: init state of second channel */ |
173 | 0 | if( decControl->nChannelsInternal > psDec->nChannelsInternal ) { |
174 | 0 | ret += silk_init_decoder( &channel_state[ 1 ] ); |
175 | 0 | } |
176 | |
|
177 | 0 | stereo_to_mono = decControl->nChannelsInternal == 1 && psDec->nChannelsInternal == 2 && |
178 | 0 | ( decControl->internalSampleRate == 1000*channel_state[ 0 ].fs_kHz ); |
179 | |
|
180 | 0 | if( channel_state[ 0 ].nFramesDecoded == 0 ) { |
181 | 0 | for( n = 0; n < decControl->nChannelsInternal; n++ ) { |
182 | 0 | opus_int fs_kHz_dec; |
183 | 0 | if( decControl->payloadSize_ms == 0 ) { |
184 | | /* Assuming packet loss, use 10 ms */ |
185 | 0 | channel_state[ n ].nFramesPerPacket = 1; |
186 | 0 | channel_state[ n ].nb_subfr = 2; |
187 | 0 | } else if( decControl->payloadSize_ms == 10 ) { |
188 | 0 | channel_state[ n ].nFramesPerPacket = 1; |
189 | 0 | channel_state[ n ].nb_subfr = 2; |
190 | 0 | } else if( decControl->payloadSize_ms == 20 ) { |
191 | 0 | channel_state[ n ].nFramesPerPacket = 1; |
192 | 0 | channel_state[ n ].nb_subfr = 4; |
193 | 0 | } else if( decControl->payloadSize_ms == 40 ) { |
194 | 0 | channel_state[ n ].nFramesPerPacket = 2; |
195 | 0 | channel_state[ n ].nb_subfr = 4; |
196 | 0 | } else if( decControl->payloadSize_ms == 60 ) { |
197 | 0 | channel_state[ n ].nFramesPerPacket = 3; |
198 | 0 | channel_state[ n ].nb_subfr = 4; |
199 | 0 | } else { |
200 | 0 | celt_assert( 0 ); |
201 | 0 | RESTORE_STACK; |
202 | 0 | return SILK_DEC_INVALID_FRAME_SIZE; |
203 | 0 | } |
204 | 0 | fs_kHz_dec = ( decControl->internalSampleRate >> 10 ) + 1; |
205 | 0 | if( fs_kHz_dec != 8 && fs_kHz_dec != 12 && fs_kHz_dec != 16 ) { |
206 | 0 | celt_assert( 0 ); |
207 | 0 | RESTORE_STACK; |
208 | 0 | return SILK_DEC_INVALID_SAMPLING_FREQUENCY; |
209 | 0 | } |
210 | 0 | ret += silk_decoder_set_fs( &channel_state[ n ], fs_kHz_dec, decControl->API_sampleRate ); |
211 | 0 | } |
212 | 0 | } |
213 | | |
214 | 0 | if( decControl->nChannelsAPI == 2 && decControl->nChannelsInternal == 2 && ( psDec->nChannelsAPI == 1 || psDec->nChannelsInternal == 1 ) ) { |
215 | 0 | silk_memset( psDec->sStereo.pred_prev_Q13, 0, sizeof( psDec->sStereo.pred_prev_Q13 ) ); |
216 | 0 | silk_memset( psDec->sStereo.sSide, 0, sizeof( psDec->sStereo.sSide ) ); |
217 | 0 | silk_memcpy( &channel_state[ 1 ].resampler_state, &channel_state[ 0 ].resampler_state, sizeof( silk_resampler_state_struct ) ); |
218 | 0 | } |
219 | 0 | psDec->nChannelsAPI = decControl->nChannelsAPI; |
220 | 0 | psDec->nChannelsInternal = decControl->nChannelsInternal; |
221 | |
|
222 | 0 | if( decControl->API_sampleRate > (opus_int32)MAX_API_FS_KHZ * 1000 || decControl->API_sampleRate < 8000 ) { |
223 | 0 | ret = SILK_DEC_INVALID_SAMPLING_FREQUENCY; |
224 | 0 | RESTORE_STACK; |
225 | 0 | return( ret ); |
226 | 0 | } |
227 | | |
228 | 0 | if( lostFlag != FLAG_PACKET_LOST && channel_state[ 0 ].nFramesDecoded == 0 ) { |
229 | | /* First decoder call for this payload */ |
230 | | /* Decode VAD flags and LBRR flag */ |
231 | 0 | for( n = 0; n < decControl->nChannelsInternal; n++ ) { |
232 | 0 | for( i = 0; i < channel_state[ n ].nFramesPerPacket; i++ ) { |
233 | 0 | channel_state[ n ].VAD_flags[ i ] = ec_dec_bit_logp(psRangeDec, 1); |
234 | 0 | } |
235 | 0 | channel_state[ n ].LBRR_flag = ec_dec_bit_logp(psRangeDec, 1); |
236 | 0 | } |
237 | | /* Decode LBRR flags */ |
238 | 0 | for( n = 0; n < decControl->nChannelsInternal; n++ ) { |
239 | 0 | silk_memset( channel_state[ n ].LBRR_flags, 0, sizeof( channel_state[ n ].LBRR_flags ) ); |
240 | 0 | if( channel_state[ n ].LBRR_flag ) { |
241 | 0 | if( channel_state[ n ].nFramesPerPacket == 1 ) { |
242 | 0 | channel_state[ n ].LBRR_flags[ 0 ] = 1; |
243 | 0 | } else { |
244 | 0 | LBRR_symbol = ec_dec_icdf( psRangeDec, silk_LBRR_flags_iCDF_ptr[ channel_state[ n ].nFramesPerPacket - 2 ], 8 ) + 1; |
245 | 0 | for( i = 0; i < channel_state[ n ].nFramesPerPacket; i++ ) { |
246 | 0 | channel_state[ n ].LBRR_flags[ i ] = silk_RSHIFT( LBRR_symbol, i ) & 1; |
247 | 0 | } |
248 | 0 | } |
249 | 0 | } |
250 | 0 | } |
251 | |
|
252 | 0 | if( lostFlag == FLAG_DECODE_NORMAL ) { |
253 | | /* Regular decoding: skip all LBRR data */ |
254 | 0 | for( i = 0; i < channel_state[ 0 ].nFramesPerPacket; i++ ) { |
255 | 0 | for( n = 0; n < decControl->nChannelsInternal; n++ ) { |
256 | 0 | if( channel_state[ n ].LBRR_flags[ i ] ) { |
257 | 0 | opus_int16 pulses[ MAX_FRAME_LENGTH ]; |
258 | 0 | opus_int condCoding; |
259 | |
|
260 | 0 | if( decControl->nChannelsInternal == 2 && n == 0 ) { |
261 | 0 | silk_stereo_decode_pred( psRangeDec, MS_pred_Q13 ); |
262 | 0 | if( channel_state[ 1 ].LBRR_flags[ i ] == 0 ) { |
263 | 0 | silk_stereo_decode_mid_only( psRangeDec, &decode_only_middle ); |
264 | 0 | } |
265 | 0 | } |
266 | | /* Use conditional coding if previous frame available */ |
267 | 0 | if( i > 0 && channel_state[ n ].LBRR_flags[ i - 1 ] ) { |
268 | 0 | condCoding = CODE_CONDITIONALLY; |
269 | 0 | } else { |
270 | 0 | condCoding = CODE_INDEPENDENTLY; |
271 | 0 | } |
272 | 0 | silk_decode_indices( &channel_state[ n ], psRangeDec, i, 1, condCoding ); |
273 | 0 | silk_decode_pulses( psRangeDec, pulses, channel_state[ n ].indices.signalType, |
274 | 0 | channel_state[ n ].indices.quantOffsetType, channel_state[ n ].frame_length ); |
275 | 0 | } |
276 | 0 | } |
277 | 0 | } |
278 | 0 | } |
279 | 0 | } |
280 | | |
281 | | /* Get MS predictor index */ |
282 | 0 | if( decControl->nChannelsInternal == 2 ) { |
283 | 0 | if( lostFlag == FLAG_DECODE_NORMAL || |
284 | 0 | ( lostFlag == FLAG_DECODE_LBRR && channel_state[ 0 ].LBRR_flags[ channel_state[ 0 ].nFramesDecoded ] == 1 ) ) |
285 | 0 | { |
286 | 0 | silk_stereo_decode_pred( psRangeDec, MS_pred_Q13 ); |
287 | | /* For LBRR data, decode mid-only flag only if side-channel's LBRR flag is false */ |
288 | 0 | if( ( lostFlag == FLAG_DECODE_NORMAL && channel_state[ 1 ].VAD_flags[ channel_state[ 0 ].nFramesDecoded ] == 0 ) || |
289 | 0 | ( lostFlag == FLAG_DECODE_LBRR && channel_state[ 1 ].LBRR_flags[ channel_state[ 0 ].nFramesDecoded ] == 0 ) ) |
290 | 0 | { |
291 | 0 | silk_stereo_decode_mid_only( psRangeDec, &decode_only_middle ); |
292 | 0 | } else { |
293 | 0 | decode_only_middle = 0; |
294 | 0 | } |
295 | 0 | } else { |
296 | 0 | for( n = 0; n < 2; n++ ) { |
297 | 0 | MS_pred_Q13[ n ] = psDec->sStereo.pred_prev_Q13[ n ]; |
298 | 0 | } |
299 | 0 | } |
300 | 0 | } |
301 | | |
302 | | /* Reset side channel decoder prediction memory for first frame with side coding */ |
303 | 0 | if( decControl->nChannelsInternal == 2 && decode_only_middle == 0 && psDec->prev_decode_only_middle == 1 ) { |
304 | 0 | silk_memset( psDec->channel_state[ 1 ].outBuf, 0, sizeof(psDec->channel_state[ 1 ].outBuf) ); |
305 | 0 | silk_memset( psDec->channel_state[ 1 ].sLPC_Q14_buf, 0, sizeof(psDec->channel_state[ 1 ].sLPC_Q14_buf) ); |
306 | 0 | psDec->channel_state[ 1 ].lagPrev = 100; |
307 | 0 | psDec->channel_state[ 1 ].LastGainIndex = 10; |
308 | 0 | psDec->channel_state[ 1 ].prevSignalType = TYPE_NO_VOICE_ACTIVITY; |
309 | 0 | psDec->channel_state[ 1 ].first_frame_after_reset = 1; |
310 | 0 | } |
311 | | |
312 | | /* Check if the temp buffer fits into the output PCM buffer. If it fits, |
313 | | we can delay allocating the temp buffer until after the SILK peak stack |
314 | | usage. We need to use a < and not a <= because of the two extra samples. */ |
315 | 0 | delay_stack_alloc = decControl->internalSampleRate*decControl->nChannelsInternal |
316 | 0 | < decControl->API_sampleRate*decControl->nChannelsAPI; |
317 | 0 | ALLOC( samplesOut1_tmp_storage1, delay_stack_alloc ? ALLOC_NONE |
318 | 0 | : decControl->nChannelsInternal*(channel_state[ 0 ].frame_length + 2 ), |
319 | 0 | opus_int16 ); |
320 | 0 | if ( delay_stack_alloc ) |
321 | 0 | { |
322 | 0 | samplesOut1_tmp[ 0 ] = samplesOut; |
323 | 0 | samplesOut1_tmp[ 1 ] = samplesOut + channel_state[ 0 ].frame_length + 2; |
324 | 0 | } else { |
325 | 0 | samplesOut1_tmp[ 0 ] = samplesOut1_tmp_storage1; |
326 | 0 | samplesOut1_tmp[ 1 ] = samplesOut1_tmp_storage1 + channel_state[ 0 ].frame_length + 2; |
327 | 0 | } |
328 | |
|
329 | 0 | if( lostFlag == FLAG_DECODE_NORMAL ) { |
330 | 0 | has_side = !decode_only_middle; |
331 | 0 | } else { |
332 | 0 | has_side = !psDec->prev_decode_only_middle |
333 | 0 | || (decControl->nChannelsInternal == 2 && lostFlag == FLAG_DECODE_LBRR && channel_state[1].LBRR_flags[ channel_state[1].nFramesDecoded ] == 1 ); |
334 | 0 | } |
335 | 0 | channel_state[ 0 ].sPLC.enable_deep_plc = decControl->enable_deep_plc; |
336 | | /* Call decoder for one frame */ |
337 | 0 | for( n = 0; n < decControl->nChannelsInternal; n++ ) { |
338 | 0 | if( n == 0 || has_side ) { |
339 | 0 | opus_int FrameIndex; |
340 | 0 | opus_int condCoding; |
341 | |
|
342 | 0 | FrameIndex = channel_state[ 0 ].nFramesDecoded - n; |
343 | | /* Use independent coding if no previous frame available */ |
344 | 0 | if( FrameIndex <= 0 ) { |
345 | 0 | condCoding = CODE_INDEPENDENTLY; |
346 | 0 | } else if( lostFlag == FLAG_DECODE_LBRR ) { |
347 | 0 | condCoding = channel_state[ n ].LBRR_flags[ FrameIndex - 1 ] ? CODE_CONDITIONALLY : CODE_INDEPENDENTLY; |
348 | 0 | } else if( n > 0 && psDec->prev_decode_only_middle ) { |
349 | | /* If we skipped a side frame in this packet, we don't |
350 | | need LTP scaling; the LTP state is well-defined. */ |
351 | 0 | condCoding = CODE_INDEPENDENTLY_NO_LTP_SCALING; |
352 | 0 | } else { |
353 | 0 | condCoding = CODE_CONDITIONALLY; |
354 | 0 | } |
355 | | #ifdef ENABLE_OSCE |
356 | | if ( channel_state[n].osce.method != decControl->osce_method ) { |
357 | | osce_reset( &channel_state[n].osce, decControl->osce_method ); |
358 | | } |
359 | | #endif |
360 | 0 | ret += silk_decode_frame( &channel_state[ n ], psRangeDec, &samplesOut1_tmp[ n ][ 2 ], &nSamplesOutDec, lostFlag, condCoding, |
361 | | #ifdef ENABLE_DEEP_PLC |
362 | | n == 0 ? lpcnet : NULL, |
363 | | #endif |
364 | | #ifdef ENABLE_OSCE |
365 | | &psDec->osce_model, |
366 | | #endif |
367 | 0 | arch); |
368 | 0 | } else { |
369 | 0 | silk_memset( &samplesOut1_tmp[ n ][ 2 ], 0, nSamplesOutDec * sizeof( opus_int16 ) ); |
370 | 0 | } |
371 | 0 | channel_state[ n ].nFramesDecoded++; |
372 | 0 | } |
373 | |
|
374 | 0 | if( decControl->nChannelsAPI == 2 && decControl->nChannelsInternal == 2 ) { |
375 | | /* Convert Mid/Side to Left/Right */ |
376 | 0 | silk_stereo_MS_to_LR( &psDec->sStereo, samplesOut1_tmp[ 0 ], samplesOut1_tmp[ 1 ], MS_pred_Q13, channel_state[ 0 ].fs_kHz, nSamplesOutDec ); |
377 | 0 | } else { |
378 | | /* Buffering */ |
379 | 0 | silk_memcpy( samplesOut1_tmp[ 0 ], psDec->sStereo.sMid, 2 * sizeof( opus_int16 ) ); |
380 | 0 | silk_memcpy( psDec->sStereo.sMid, &samplesOut1_tmp[ 0 ][ nSamplesOutDec ], 2 * sizeof( opus_int16 ) ); |
381 | 0 | } |
382 | | |
383 | | /* Number of output samples */ |
384 | 0 | *nSamplesOut = silk_DIV32( nSamplesOutDec * decControl->API_sampleRate, silk_SMULBB( channel_state[ 0 ].fs_kHz, 1000 ) ); |
385 | | |
386 | | /* Set up pointers to temp buffers */ |
387 | 0 | ALLOC( samplesOut2_tmp, |
388 | 0 | decControl->nChannelsAPI == 2 ? *nSamplesOut : ALLOC_NONE, opus_int16 ); |
389 | 0 | if( decControl->nChannelsAPI == 2 ) { |
390 | 0 | resample_out_ptr = samplesOut2_tmp; |
391 | 0 | } else { |
392 | 0 | resample_out_ptr = samplesOut; |
393 | 0 | } |
394 | |
|
395 | 0 | ALLOC( samplesOut1_tmp_storage2, delay_stack_alloc |
396 | 0 | ? decControl->nChannelsInternal*(channel_state[ 0 ].frame_length + 2 ) |
397 | 0 | : ALLOC_NONE, |
398 | 0 | opus_int16 ); |
399 | 0 | if ( delay_stack_alloc ) { |
400 | 0 | OPUS_COPY(samplesOut1_tmp_storage2, samplesOut, decControl->nChannelsInternal*(channel_state[ 0 ].frame_length + 2)); |
401 | 0 | samplesOut1_tmp[ 0 ] = samplesOut1_tmp_storage2; |
402 | 0 | samplesOut1_tmp[ 1 ] = samplesOut1_tmp_storage2 + channel_state[ 0 ].frame_length + 2; |
403 | 0 | } |
404 | 0 | for( n = 0; n < silk_min( decControl->nChannelsAPI, decControl->nChannelsInternal ); n++ ) { |
405 | | |
406 | | /* Resample decoded signal to API_sampleRate */ |
407 | 0 | ret += silk_resampler( &channel_state[ n ].resampler_state, resample_out_ptr, &samplesOut1_tmp[ n ][ 1 ], nSamplesOutDec ); |
408 | | |
409 | | /* Interleave if stereo output and stereo stream */ |
410 | 0 | if( decControl->nChannelsAPI == 2 ) { |
411 | 0 | for( i = 0; i < *nSamplesOut; i++ ) { |
412 | 0 | samplesOut[ n + 2 * i ] = resample_out_ptr[ i ]; |
413 | 0 | } |
414 | 0 | } |
415 | 0 | } |
416 | | |
417 | | /* Create two channel output from mono stream */ |
418 | 0 | if( decControl->nChannelsAPI == 2 && decControl->nChannelsInternal == 1 ) { |
419 | 0 | if ( stereo_to_mono ){ |
420 | | /* Resample right channel for newly collapsed stereo just in case |
421 | | we weren't doing collapsing when switching to mono */ |
422 | 0 | ret += silk_resampler( &channel_state[ 1 ].resampler_state, resample_out_ptr, &samplesOut1_tmp[ 0 ][ 1 ], nSamplesOutDec ); |
423 | |
|
424 | 0 | for( i = 0; i < *nSamplesOut; i++ ) { |
425 | 0 | samplesOut[ 1 + 2 * i ] = resample_out_ptr[ i ]; |
426 | 0 | } |
427 | 0 | } else { |
428 | 0 | for( i = 0; i < *nSamplesOut; i++ ) { |
429 | 0 | samplesOut[ 1 + 2 * i ] = samplesOut[ 0 + 2 * i ]; |
430 | 0 | } |
431 | 0 | } |
432 | 0 | } |
433 | | |
434 | | /* Export pitch lag, measured at 48 kHz sampling rate */ |
435 | 0 | if( channel_state[ 0 ].prevSignalType == TYPE_VOICED ) { |
436 | 0 | int mult_tab[ 3 ] = { 6, 4, 3 }; |
437 | 0 | decControl->prevPitchLag = channel_state[ 0 ].lagPrev * mult_tab[ ( channel_state[ 0 ].fs_kHz - 8 ) >> 2 ]; |
438 | 0 | } else { |
439 | 0 | decControl->prevPitchLag = 0; |
440 | 0 | } |
441 | |
|
442 | 0 | if( lostFlag == FLAG_PACKET_LOST ) { |
443 | | /* On packet loss, remove the gain clamping to prevent having the energy "bounce back" |
444 | | if we lose packets when the energy is going down */ |
445 | 0 | for ( i = 0; i < psDec->nChannelsInternal; i++ ) |
446 | 0 | psDec->channel_state[ i ].LastGainIndex = 10; |
447 | 0 | } else { |
448 | 0 | psDec->prev_decode_only_middle = decode_only_middle; |
449 | 0 | } |
450 | 0 | RESTORE_STACK; |
451 | 0 | return ret; |
452 | 0 | } |
453 | | |
454 | | #if 0 |
455 | | /* Getting table of contents for a packet */ |
456 | | opus_int silk_get_TOC( |
457 | | const opus_uint8 *payload, /* I Payload data */ |
458 | | const opus_int nBytesIn, /* I Number of input bytes */ |
459 | | const opus_int nFramesPerPayload, /* I Number of SILK frames per payload */ |
460 | | silk_TOC_struct *Silk_TOC /* O Type of content */ |
461 | | ) |
462 | | { |
463 | | opus_int i, flags, ret = SILK_NO_ERROR; |
464 | | |
465 | | if( nBytesIn < 1 ) { |
466 | | return -1; |
467 | | } |
468 | | if( nFramesPerPayload < 0 || nFramesPerPayload > 3 ) { |
469 | | return -1; |
470 | | } |
471 | | |
472 | | silk_memset( Silk_TOC, 0, sizeof( *Silk_TOC ) ); |
473 | | |
474 | | /* For stereo, extract the flags for the mid channel */ |
475 | | flags = silk_RSHIFT( payload[ 0 ], 7 - nFramesPerPayload ) & ( silk_LSHIFT( 1, nFramesPerPayload + 1 ) - 1 ); |
476 | | |
477 | | Silk_TOC->inbandFECFlag = flags & 1; |
478 | | for( i = nFramesPerPayload - 1; i >= 0 ; i-- ) { |
479 | | flags = silk_RSHIFT( flags, 1 ); |
480 | | Silk_TOC->VADFlags[ i ] = flags & 1; |
481 | | Silk_TOC->VADFlag |= flags & 1; |
482 | | } |
483 | | |
484 | | return ret; |
485 | | } |
486 | | #endif |