blob: 660f93db771bd84e65cdfd336eafcc7e544e544e [file] [log] [blame]
Gregory Maxwellae231142011-07-30 08:18:48 -04001/***********************************************************************
2Copyright (c) 2006-2011, Skype Limited. All rights reserved.
3Redistribution and use in source and binary forms, with or without
Jean-Marc Valinae00e602012-04-20 16:31:04 -04004modification, are permitted provided that the following conditions
5are met:
Gregory Maxwellae231142011-07-30 08:18:48 -04006- Redistributions of source code must retain the above copyright notice,
7this list of conditions and the following disclaimer.
8- Redistributions in binary form must reproduce the above copyright
9notice, this list of conditions and the following disclaimer in the
10documentation and/or other materials provided with the distribution.
Ralph Gilesf2446c22013-09-16 14:40:04 -070011- Neither the name of Internet Society, IETF or IETF Trust, nor the
Jean-Marc Valinae00e602012-04-20 16:31:04 -040012names of specific contributors, may be used to endorse or promote
13products derived from this software without specific prior written
14permission.
Timothy B. Terriberry80ad3832013-05-19 18:00:39 -070015THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
Jean-Marc Valinae00e602012-04-20 16:31:04 -040016AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25POSSIBILITY OF SUCH DAMAGE.
Gregory Maxwellae231142011-07-30 08:18:48 -040026***********************************************************************/
27
28#ifdef HAVE_CONFIG_H
29#include "config.h"
30#endif
Jean-Marc Valin1c2f5632011-09-16 01:16:53 -070031#include "API.h"
32#include "main.h"
Timothy B. Terriberry6f2d9f52012-09-05 07:35:49 -070033#include "stack_alloc.h"
Gregory Maxwellae231142011-07-30 08:18:48 -040034
35/************************/
36/* Decoder Super Struct */
37/************************/
38typedef struct {
39 silk_decoder_state channel_state[ DECODER_NUM_CHANNELS ];
40 stereo_dec_state sStereo;
41 opus_int nChannelsAPI;
42 opus_int nChannelsInternal;
Jean-Marc Valinb24e5742011-10-11 21:09:14 -040043 opus_int prev_decode_only_middle;
Gregory Maxwellae231142011-07-30 08:18:48 -040044} silk_decoder;
45
46/*********************/
47/* Decoder functions */
48/*********************/
49
Koen Vosacc7a6c2011-10-28 19:44:26 -040050opus_int silk_Get_Decoder_Size( /* O Returns error code */
51 opus_int *decSizeBytes /* O Number of bytes in SILK decoder state */
52)
Gregory Maxwellae231142011-07-30 08:18:48 -040053{
54 opus_int ret = SILK_NO_ERROR;
55
56 *decSizeBytes = sizeof( silk_decoder );
57
58 return ret;
59}
60
61/* Reset decoder state */
Koen Vosacc7a6c2011-10-28 19:44:26 -040062opus_int silk_InitDecoder( /* O Returns error code */
63 void *decState /* I/O State */
Gregory Maxwellae231142011-07-30 08:18:48 -040064)
65{
66 opus_int n, ret = SILK_NO_ERROR;
67 silk_decoder_state *channel_state = ((silk_decoder *)decState)->channel_state;
68
69 for( n = 0; n < DECODER_NUM_CHANNELS; n++ ) {
70 ret = silk_init_decoder( &channel_state[ n ] );
71 }
Jean-Marc Valine51a3f32013-02-06 23:48:09 -050072 silk_memset(&((silk_decoder *)decState)->sStereo, 0, sizeof(((silk_decoder *)decState)->sStereo));
73 /* Not strictly needed, but it's cleaner that way */
74 ((silk_decoder *)decState)->prev_decode_only_middle = 0;
Gregory Maxwellae231142011-07-30 08:18:48 -040075
76 return ret;
77}
78
79/* Decode a frame */
Koen Vosacc7a6c2011-10-28 19:44:26 -040080opus_int silk_Decode( /* O Returns error code */
81 void* decState, /* I/O State */
82 silk_DecControlStruct* decControl, /* I/O Control Structure */
83 opus_int lostFlag, /* I 0: no loss, 1 loss, 2 decode fec */
84 opus_int newPacketFlag, /* I Indicates first decoder call for this packet */
85 ec_dec *psRangeDec, /* I/O Compressor data structure */
86 opus_int16 *samplesOut, /* O Decoded output speech vector */
87 opus_int32 *nSamplesOut /* O Number of samples decoded */
Gregory Maxwellae231142011-07-30 08:18:48 -040088)
89{
Koen Vosbf75c8e2011-12-13 14:47:31 -050090 opus_int i, n, decode_only_middle = 0, ret = SILK_NO_ERROR;
Gregory Maxwellae231142011-07-30 08:18:48 -040091 opus_int32 nSamplesOutDec, LBRR_symbol;
Timothy B. Terriberry6f2d9f52012-09-05 07:35:49 -070092 opus_int16 *samplesOut1_tmp[ 2 ];
93 VARDECL( opus_int16, samplesOut1_tmp_storage );
94 VARDECL( opus_int16, samplesOut2_tmp );
Gregory Maxwell64a35412011-09-02 10:31:17 -040095 opus_int32 MS_pred_Q13[ 2 ] = { 0 };
Gregory Maxwellae231142011-07-30 08:18:48 -040096 opus_int16 *resample_out_ptr;
97 silk_decoder *psDec = ( silk_decoder * )decState;
98 silk_decoder_state *channel_state = psDec->channel_state;
Jean-Marc Valinec1ebf82011-10-24 20:07:00 -040099 opus_int has_side;
Jean-Marc Valin17c59662012-02-17 16:09:21 -0500100 opus_int stereo_to_mono;
Timothy B. Terriberry6f2d9f52012-09-05 07:35:49 -0700101 SAVE_STACK;
Gregory Maxwellae231142011-07-30 08:18:48 -0400102
Philip Jägenstedtb7b58722012-10-11 11:27:12 +0200103 silk_assert( decControl->nChannelsInternal == 1 || decControl->nChannelsInternal == 2 );
104
Gregory Maxwellae231142011-07-30 08:18:48 -0400105 /**********************************/
106 /* Test if first frame in payload */
107 /**********************************/
108 if( newPacketFlag ) {
109 for( n = 0; n < decControl->nChannelsInternal; n++ ) {
110 channel_state[ n ].nFramesDecoded = 0; /* Used to count frames in packet */
111 }
112 }
113
Gregory Maxwellae231142011-07-30 08:18:48 -0400114 /* If Mono -> Stereo transition in bitstream: init state of second channel */
115 if( decControl->nChannelsInternal > psDec->nChannelsInternal ) {
116 ret += silk_init_decoder( &channel_state[ 1 ] );
Gregory Maxwellae231142011-07-30 08:18:48 -0400117 }
118
Jean-Marc Valin17c59662012-02-17 16:09:21 -0500119 stereo_to_mono = decControl->nChannelsInternal == 1 && psDec->nChannelsInternal == 2 &&
120 ( decControl->internalSampleRate == 1000*channel_state[ 0 ].fs_kHz );
121
Timothy B. Terriberry1e03a6e2011-10-14 16:14:36 -0700122 if( channel_state[ 0 ].nFramesDecoded == 0 ) {
123 for( n = 0; n < decControl->nChannelsInternal; n++ ) {
Gregory Maxwellae231142011-07-30 08:18:48 -0400124 opus_int fs_kHz_dec;
125 if( decControl->payloadSize_ms == 0 ) {
126 /* Assuming packet loss, use 10 ms */
127 channel_state[ n ].nFramesPerPacket = 1;
128 channel_state[ n ].nb_subfr = 2;
129 } else if( decControl->payloadSize_ms == 10 ) {
130 channel_state[ n ].nFramesPerPacket = 1;
131 channel_state[ n ].nb_subfr = 2;
132 } else if( decControl->payloadSize_ms == 20 ) {
133 channel_state[ n ].nFramesPerPacket = 1;
134 channel_state[ n ].nb_subfr = 4;
135 } else if( decControl->payloadSize_ms == 40 ) {
136 channel_state[ n ].nFramesPerPacket = 2;
137 channel_state[ n ].nb_subfr = 4;
138 } else if( decControl->payloadSize_ms == 60 ) {
139 channel_state[ n ].nFramesPerPacket = 3;
140 channel_state[ n ].nb_subfr = 4;
141 } else {
Jean-Marc Valinfb3a4372011-09-16 00:58:26 -0700142 silk_assert( 0 );
Timothy B. Terriberry6f2d9f52012-09-05 07:35:49 -0700143 RESTORE_STACK;
Gregory Maxwellae231142011-07-30 08:18:48 -0400144 return SILK_DEC_INVALID_FRAME_SIZE;
145 }
146 fs_kHz_dec = ( decControl->internalSampleRate >> 10 ) + 1;
147 if( fs_kHz_dec != 8 && fs_kHz_dec != 12 && fs_kHz_dec != 16 ) {
Jean-Marc Valinfb3a4372011-09-16 00:58:26 -0700148 silk_assert( 0 );
Timothy B. Terriberry6f2d9f52012-09-05 07:35:49 -0700149 RESTORE_STACK;
Gregory Maxwellae231142011-07-30 08:18:48 -0400150 return SILK_DEC_INVALID_SAMPLING_FREQUENCY;
151 }
Jean-Marc Valinb24e5742011-10-11 21:09:14 -0400152 ret += silk_decoder_set_fs( &channel_state[ n ], fs_kHz_dec, decControl->API_sampleRate );
Gregory Maxwellae231142011-07-30 08:18:48 -0400153 }
154 }
155
Gregory Maxwellae231142011-07-30 08:18:48 -0400156 if( decControl->nChannelsAPI == 2 && decControl->nChannelsInternal == 2 && ( psDec->nChannelsAPI == 1 || psDec->nChannelsInternal == 1 ) ) {
Jean-Marc Valinfb3a4372011-09-16 00:58:26 -0700157 silk_memset( psDec->sStereo.pred_prev_Q13, 0, sizeof( psDec->sStereo.pred_prev_Q13 ) );
158 silk_memset( psDec->sStereo.sSide, 0, sizeof( psDec->sStereo.sSide ) );
Jean-Marc Valinb24e5742011-10-11 21:09:14 -0400159 silk_memcpy( &channel_state[ 1 ].resampler_state, &channel_state[ 0 ].resampler_state, sizeof( silk_resampler_state_struct ) );
Gregory Maxwellae231142011-07-30 08:18:48 -0400160 }
161 psDec->nChannelsAPI = decControl->nChannelsAPI;
162 psDec->nChannelsInternal = decControl->nChannelsInternal;
163
Jean-Marc Valin905197d2012-03-08 14:09:09 -0500164 if( decControl->API_sampleRate > (opus_int32)MAX_API_FS_KHZ * 1000 || decControl->API_sampleRate < 8000 ) {
Gregory Maxwellae231142011-07-30 08:18:48 -0400165 ret = SILK_DEC_INVALID_SAMPLING_FREQUENCY;
Timothy B. Terriberry6f2d9f52012-09-05 07:35:49 -0700166 RESTORE_STACK;
Gregory Maxwellae231142011-07-30 08:18:48 -0400167 return( ret );
168 }
169
170 if( lostFlag != FLAG_PACKET_LOST && channel_state[ 0 ].nFramesDecoded == 0 ) {
171 /* First decoder call for this payload */
172 /* Decode VAD flags and LBRR flag */
173 for( n = 0; n < decControl->nChannelsInternal; n++ ) {
174 for( i = 0; i < channel_state[ n ].nFramesPerPacket; i++ ) {
175 channel_state[ n ].VAD_flags[ i ] = ec_dec_bit_logp(psRangeDec, 1);
176 }
177 channel_state[ n ].LBRR_flag = ec_dec_bit_logp(psRangeDec, 1);
178 }
179 /* Decode LBRR flags */
180 for( n = 0; n < decControl->nChannelsInternal; n++ ) {
Jean-Marc Valinfb3a4372011-09-16 00:58:26 -0700181 silk_memset( channel_state[ n ].LBRR_flags, 0, sizeof( channel_state[ n ].LBRR_flags ) );
Gregory Maxwellae231142011-07-30 08:18:48 -0400182 if( channel_state[ n ].LBRR_flag ) {
183 if( channel_state[ n ].nFramesPerPacket == 1 ) {
184 channel_state[ n ].LBRR_flags[ 0 ] = 1;
185 } else {
186 LBRR_symbol = ec_dec_icdf( psRangeDec, silk_LBRR_flags_iCDF_ptr[ channel_state[ n ].nFramesPerPacket - 2 ], 8 ) + 1;
187 for( i = 0; i < channel_state[ n ].nFramesPerPacket; i++ ) {
Jean-Marc Valinfb3a4372011-09-16 00:58:26 -0700188 channel_state[ n ].LBRR_flags[ i ] = silk_RSHIFT( LBRR_symbol, i ) & 1;
Gregory Maxwellae231142011-07-30 08:18:48 -0400189 }
190 }
191 }
192 }
193
194 if( lostFlag == FLAG_DECODE_NORMAL ) {
195 /* Regular decoding: skip all LBRR data */
196 for( i = 0; i < channel_state[ 0 ].nFramesPerPacket; i++ ) {
197 for( n = 0; n < decControl->nChannelsInternal; n++ ) {
198 if( channel_state[ n ].LBRR_flags[ i ] ) {
Jean-Marc Valindce69d22014-01-06 21:59:48 -0500199 opus_int16 pulses[ MAX_FRAME_LENGTH ];
Timothy B. Terriberry53cc1a02011-10-14 13:38:24 -0700200 opus_int condCoding;
201
Gregory Maxwellae231142011-07-30 08:18:48 -0400202 if( decControl->nChannelsInternal == 2 && n == 0 ) {
Koen Vos4e1ce382011-08-25 13:50:21 -0400203 silk_stereo_decode_pred( psRangeDec, MS_pred_Q13 );
204 if( channel_state[ 1 ].LBRR_flags[ i ] == 0 ) {
205 silk_stereo_decode_mid_only( psRangeDec, &decode_only_middle );
206 }
Gregory Maxwellae231142011-07-30 08:18:48 -0400207 }
Timothy B. Terriberry53cc1a02011-10-14 13:38:24 -0700208 /* Use conditional coding if previous frame available */
209 if( i > 0 && channel_state[ n ].LBRR_flags[ i - 1 ] ) {
210 condCoding = CODE_CONDITIONALLY;
211 } else {
212 condCoding = CODE_INDEPENDENTLY;
213 }
214 silk_decode_indices( &channel_state[ n ], psRangeDec, i, 1, condCoding );
Gregory Maxwellae231142011-07-30 08:18:48 -0400215 silk_decode_pulses( psRangeDec, pulses, channel_state[ n ].indices.signalType,
216 channel_state[ n ].indices.quantOffsetType, channel_state[ n ].frame_length );
217 }
218 }
219 }
220 }
221 }
222
223 /* Get MS predictor index */
224 if( decControl->nChannelsInternal == 2 ) {
225 if( lostFlag == FLAG_DECODE_NORMAL ||
226 ( lostFlag == FLAG_DECODE_LBRR && channel_state[ 0 ].LBRR_flags[ channel_state[ 0 ].nFramesDecoded ] == 1 ) )
227 {
Koen Vos4e1ce382011-08-25 13:50:21 -0400228 silk_stereo_decode_pred( psRangeDec, MS_pred_Q13 );
Koen Vos3195f6c2011-10-10 20:46:32 -0400229 /* For LBRR data, decode mid-only flag only if side-channel's LBRR flag is false */
Timothy B. Terriberry6559d362011-10-17 14:20:55 -0700230 if( ( lostFlag == FLAG_DECODE_NORMAL && channel_state[ 1 ].VAD_flags[ channel_state[ 0 ].nFramesDecoded ] == 0 ) ||
Koen Vos4e1ce382011-08-25 13:50:21 -0400231 ( lostFlag == FLAG_DECODE_LBRR && channel_state[ 1 ].LBRR_flags[ channel_state[ 0 ].nFramesDecoded ] == 0 ) )
232 {
233 silk_stereo_decode_mid_only( psRangeDec, &decode_only_middle );
234 } else {
235 decode_only_middle = 0;
236 }
Gregory Maxwellae231142011-07-30 08:18:48 -0400237 } else {
Gregory Maxwell5c3d1552011-08-15 13:18:52 -0400238 for( n = 0; n < 2; n++ ) {
Jean-Marc Valinb24e5742011-10-11 21:09:14 -0400239 MS_pred_Q13[ n ] = psDec->sStereo.pred_prev_Q13[ n ];
Gregory Maxwell5c3d1552011-08-15 13:18:52 -0400240 }
Gregory Maxwellae231142011-07-30 08:18:48 -0400241 }
242 }
243
Jean-Marc Valinb24e5742011-10-11 21:09:14 -0400244 /* Reset side channel decoder prediction memory for first frame with side coding */
245 if( decControl->nChannelsInternal == 2 && decode_only_middle == 0 && psDec->prev_decode_only_middle == 1 ) {
246 silk_memset( psDec->channel_state[ 1 ].outBuf, 0, sizeof(psDec->channel_state[ 1 ].outBuf) );
247 silk_memset( psDec->channel_state[ 1 ].sLPC_Q14_buf, 0, sizeof(psDec->channel_state[ 1 ].sLPC_Q14_buf) );
248 psDec->channel_state[ 1 ].lagPrev = 100;
249 psDec->channel_state[ 1 ].LastGainIndex = 10;
250 psDec->channel_state[ 1 ].prevSignalType = TYPE_NO_VOICE_ACTIVITY;
Gregory Maxwell52de5362011-10-19 00:20:46 -0400251 psDec->channel_state[ 1 ].first_frame_after_reset = 1;
Jean-Marc Valinb24e5742011-10-11 21:09:14 -0400252 }
253
Timothy B. Terriberry6f2d9f52012-09-05 07:35:49 -0700254 ALLOC( samplesOut1_tmp_storage,
255 decControl->nChannelsInternal*(
256 channel_state[ 0 ].frame_length + 2 ),
257 opus_int16 );
258 samplesOut1_tmp[ 0 ] = samplesOut1_tmp_storage;
259 samplesOut1_tmp[ 1 ] = samplesOut1_tmp_storage
260 + channel_state[ 0 ].frame_length + 2;
261
Koen Vosacc7a6c2011-10-28 19:44:26 -0400262 if( lostFlag == FLAG_DECODE_NORMAL ) {
Jean-Marc Valinec1ebf82011-10-24 20:07:00 -0400263 has_side = !decode_only_middle;
264 } else {
265 has_side = !psDec->prev_decode_only_middle
266 || (decControl->nChannelsInternal == 2 && lostFlag == FLAG_DECODE_LBRR && channel_state[1].LBRR_flags[ channel_state[1].nFramesDecoded ] == 1 );
267 }
Gregory Maxwellae231142011-07-30 08:18:48 -0400268 /* Call decoder for one frame */
269 for( n = 0; n < decControl->nChannelsInternal; n++ ) {
Jean-Marc Valinec1ebf82011-10-24 20:07:00 -0400270 if( n == 0 || has_side ) {
Timothy B. Terriberry53cc1a02011-10-14 13:38:24 -0700271 opus_int FrameIndex;
272 opus_int condCoding;
273
274 FrameIndex = channel_state[ 0 ].nFramesDecoded - n;
275 /* Use independent coding if no previous frame available */
276 if( FrameIndex <= 0 ) {
277 condCoding = CODE_INDEPENDENTLY;
278 } else if( lostFlag == FLAG_DECODE_LBRR ) {
279 condCoding = channel_state[ n ].LBRR_flags[ FrameIndex - 1 ] ? CODE_CONDITIONALLY : CODE_INDEPENDENTLY;
280 } else if( n > 0 && psDec->prev_decode_only_middle ) {
281 /* If we skipped a side frame in this packet, we don't
282 need LTP scaling; the LTP state is well-defined. */
283 condCoding = CODE_INDEPENDENTLY_NO_LTP_SCALING;
284 } else {
285 condCoding = CODE_CONDITIONALLY;
286 }
Koen Vosbf75c8e2011-12-13 14:47:31 -0500287 ret += silk_decode_frame( &channel_state[ n ], psRangeDec, &samplesOut1_tmp[ n ][ 2 ], &nSamplesOutDec, lostFlag, condCoding);
Gregory Maxwellae231142011-07-30 08:18:48 -0400288 } else {
Koen Vosbf75c8e2011-12-13 14:47:31 -0500289 silk_memset( &samplesOut1_tmp[ n ][ 2 ], 0, nSamplesOutDec * sizeof( opus_int16 ) );
Gregory Maxwellae231142011-07-30 08:18:48 -0400290 }
Timothy B. Terriberry1e03a6e2011-10-14 16:14:36 -0700291 channel_state[ n ].nFramesDecoded++;
Gregory Maxwellae231142011-07-30 08:18:48 -0400292 }
293
294 if( decControl->nChannelsAPI == 2 && decControl->nChannelsInternal == 2 ) {
295 /* Convert Mid/Side to Left/Right */
Koen Vosbf75c8e2011-12-13 14:47:31 -0500296 silk_stereo_MS_to_LR( &psDec->sStereo, samplesOut1_tmp[ 0 ], samplesOut1_tmp[ 1 ], MS_pred_Q13, channel_state[ 0 ].fs_kHz, nSamplesOutDec );
Gregory Maxwellae231142011-07-30 08:18:48 -0400297 } else {
298 /* Buffering */
Koen Vosbf75c8e2011-12-13 14:47:31 -0500299 silk_memcpy( samplesOut1_tmp[ 0 ], psDec->sStereo.sMid, 2 * sizeof( opus_int16 ) );
300 silk_memcpy( psDec->sStereo.sMid, &samplesOut1_tmp[ 0 ][ nSamplesOutDec ], 2 * sizeof( opus_int16 ) );
Gregory Maxwellae231142011-07-30 08:18:48 -0400301 }
302
303 /* Number of output samples */
Jean-Marc Valinfb3a4372011-09-16 00:58:26 -0700304 *nSamplesOut = silk_DIV32( nSamplesOutDec * decControl->API_sampleRate, silk_SMULBB( channel_state[ 0 ].fs_kHz, 1000 ) );
Gregory Maxwellae231142011-07-30 08:18:48 -0400305
306 /* Set up pointers to temp buffers */
Timothy B. Terriberry6f2d9f52012-09-05 07:35:49 -0700307 ALLOC( samplesOut2_tmp,
Jean-Marc Valinca6fac02013-11-09 18:28:40 -0500308 decControl->nChannelsAPI == 2 ? *nSamplesOut : ALLOC_NONE, opus_int16 );
Gregory Maxwellae231142011-07-30 08:18:48 -0400309 if( decControl->nChannelsAPI == 2 ) {
310 resample_out_ptr = samplesOut2_tmp;
311 } else {
312 resample_out_ptr = samplesOut;
313 }
314
Jean-Marc Valinfb3a4372011-09-16 00:58:26 -0700315 for( n = 0; n < silk_min( decControl->nChannelsAPI, decControl->nChannelsInternal ); n++ ) {
Jean-Marc Valinb5972382011-10-07 08:38:27 -0400316
Gregory Maxwellae231142011-07-30 08:18:48 -0400317 /* Resample decoded signal to API_sampleRate */
318 ret += silk_resampler( &channel_state[ n ].resampler_state, resample_out_ptr, &samplesOut1_tmp[ n ][ 1 ], nSamplesOutDec );
319
320 /* Interleave if stereo output and stereo stream */
Jean-Marc Valin17c59662012-02-17 16:09:21 -0500321 if( decControl->nChannelsAPI == 2 ) {
Gregory Maxwellae231142011-07-30 08:18:48 -0400322 for( i = 0; i < *nSamplesOut; i++ ) {
323 samplesOut[ n + 2 * i ] = resample_out_ptr[ i ];
324 }
325 }
326 }
327
328 /* Create two channel output from mono stream */
329 if( decControl->nChannelsAPI == 2 && decControl->nChannelsInternal == 1 ) {
Jean-Marc Valin17c59662012-02-17 16:09:21 -0500330 if ( stereo_to_mono ){
331 /* Resample right channel for newly collapsed stereo just in case
332 we weren't doing collapsing when switching to mono */
333 ret += silk_resampler( &channel_state[ 1 ].resampler_state, resample_out_ptr, &samplesOut1_tmp[ 0 ][ 1 ], nSamplesOutDec );
334
335 for( i = 0; i < *nSamplesOut; i++ ) {
336 samplesOut[ 1 + 2 * i ] = resample_out_ptr[ i ];
337 }
338 } else {
339 for( i = 0; i < *nSamplesOut; i++ ) {
340 samplesOut[ 1 + 2 * i ] = samplesOut[ 0 + 2 * i ];
341 }
Gregory Maxwellae231142011-07-30 08:18:48 -0400342 }
343 }
344
Jean-Marc Valinb24e5742011-10-11 21:09:14 -0400345 /* Export pitch lag, measured at 48 kHz sampling rate */
346 if( channel_state[ 0 ].prevSignalType == TYPE_VOICED ) {
347 int mult_tab[ 3 ] = { 6, 4, 3 };
348 decControl->prevPitchLag = channel_state[ 0 ].lagPrev * mult_tab[ ( channel_state[ 0 ].fs_kHz - 8 ) >> 2 ];
349 } else {
350 decControl->prevPitchLag = 0;
351 }
352
Jean-Marc Valinee8adbe2012-01-24 14:45:08 +1300353 if( lostFlag == FLAG_PACKET_LOST ) {
354 /* On packet loss, remove the gain clamping to prevent having the energy "bounce back"
355 if we lose packets when the energy is going down */
356 for ( i = 0; i < psDec->nChannelsInternal; i++ )
357 psDec->channel_state[ i ].LastGainIndex = 10;
358 } else {
Jean-Marc Valin4f1b7da2011-10-12 15:09:13 -0400359 psDec->prev_decode_only_middle = decode_only_middle;
360 }
Timothy B. Terriberry6f2d9f52012-09-05 07:35:49 -0700361 RESTORE_STACK;
Gregory Maxwellae231142011-07-30 08:18:48 -0400362 return ret;
363}
364
Gregory Maxwell0d57a5d2012-08-06 10:52:27 -0400365#if 0
Gregory Maxwellae231142011-07-30 08:18:48 -0400366/* Getting table of contents for a packet */
367opus_int silk_get_TOC(
Koen Vosacc7a6c2011-10-28 19:44:26 -0400368 const opus_uint8 *payload, /* I Payload data */
369 const opus_int nBytesIn, /* I Number of input bytes */
370 const opus_int nFramesPerPayload, /* I Number of SILK frames per payload */
371 silk_TOC_struct *Silk_TOC /* O Type of content */
Gregory Maxwellae231142011-07-30 08:18:48 -0400372)
373{
374 opus_int i, flags, ret = SILK_NO_ERROR;
375
376 if( nBytesIn < 1 ) {
377 return -1;
378 }
379 if( nFramesPerPayload < 0 || nFramesPerPayload > 3 ) {
380 return -1;
381 }
382
Cyril Lashkevich622046c2012-08-06 16:38:18 +0300383 silk_memset( Silk_TOC, 0, sizeof( *Silk_TOC ) );
Gregory Maxwellae231142011-07-30 08:18:48 -0400384
385 /* For stereo, extract the flags for the mid channel */
Jean-Marc Valinfb3a4372011-09-16 00:58:26 -0700386 flags = silk_RSHIFT( payload[ 0 ], 7 - nFramesPerPayload ) & ( silk_LSHIFT( 1, nFramesPerPayload + 1 ) - 1 );
Gregory Maxwellae231142011-07-30 08:18:48 -0400387
388 Silk_TOC->inbandFECFlag = flags & 1;
389 for( i = nFramesPerPayload - 1; i >= 0 ; i-- ) {
Jean-Marc Valinfb3a4372011-09-16 00:58:26 -0700390 flags = silk_RSHIFT( flags, 1 );
Gregory Maxwellae231142011-07-30 08:18:48 -0400391 Silk_TOC->VADFlags[ i ] = flags & 1;
392 Silk_TOC->VADFlag |= flags & 1;
393 }
394
395 return ret;
396}
Gregory Maxwell0d57a5d2012-08-06 10:52:27 -0400397#endif