annotate src/opus-1.3/silk/dec_API.c @ 69:7aeed7906520

Add Opus sources and macOS builds
author Chris Cannam
date Wed, 23 Jan 2019 13:48:08 +0000
parents
children
rev   line source
Chris@69 1 /***********************************************************************
Chris@69 2 Copyright (c) 2006-2011, Skype Limited. All rights reserved.
Chris@69 3 Redistribution and use in source and binary forms, with or without
Chris@69 4 modification, are permitted provided that the following conditions
Chris@69 5 are met:
Chris@69 6 - Redistributions of source code must retain the above copyright notice,
Chris@69 7 this list of conditions and the following disclaimer.
Chris@69 8 - Redistributions in binary form must reproduce the above copyright
Chris@69 9 notice, this list of conditions and the following disclaimer in the
Chris@69 10 documentation and/or other materials provided with the distribution.
Chris@69 11 - Neither the name of Internet Society, IETF or IETF Trust, nor the
Chris@69 12 names of specific contributors, may be used to endorse or promote
Chris@69 13 products derived from this software without specific prior written
Chris@69 14 permission.
Chris@69 15 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
Chris@69 16 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Chris@69 17 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Chris@69 18 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
Chris@69 19 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
Chris@69 20 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
Chris@69 21 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
Chris@69 22 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
Chris@69 23 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
Chris@69 24 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
Chris@69 25 POSSIBILITY OF SUCH DAMAGE.
Chris@69 26 ***********************************************************************/
Chris@69 27
Chris@69 28 #ifdef HAVE_CONFIG_H
Chris@69 29 #include "config.h"
Chris@69 30 #endif
Chris@69 31 #include "API.h"
Chris@69 32 #include "main.h"
Chris@69 33 #include "stack_alloc.h"
Chris@69 34 #include "os_support.h"
Chris@69 35
Chris@69 36 /************************/
Chris@69 37 /* Decoder Super Struct */
Chris@69 38 /************************/
Chris@69 39 typedef struct {
Chris@69 40 silk_decoder_state channel_state[ DECODER_NUM_CHANNELS ];
Chris@69 41 stereo_dec_state sStereo;
Chris@69 42 opus_int nChannelsAPI;
Chris@69 43 opus_int nChannelsInternal;
Chris@69 44 opus_int prev_decode_only_middle;
Chris@69 45 } silk_decoder;
Chris@69 46
Chris@69 47 /*********************/
Chris@69 48 /* Decoder functions */
Chris@69 49 /*********************/
Chris@69 50
Chris@69 51 opus_int silk_Get_Decoder_Size( /* O Returns error code */
Chris@69 52 opus_int *decSizeBytes /* O Number of bytes in SILK decoder state */
Chris@69 53 )
Chris@69 54 {
Chris@69 55 opus_int ret = SILK_NO_ERROR;
Chris@69 56
Chris@69 57 *decSizeBytes = sizeof( silk_decoder );
Chris@69 58
Chris@69 59 return ret;
Chris@69 60 }
Chris@69 61
Chris@69 62 /* Reset decoder state */
Chris@69 63 opus_int silk_InitDecoder( /* O Returns error code */
Chris@69 64 void *decState /* I/O State */
Chris@69 65 )
Chris@69 66 {
Chris@69 67 opus_int n, ret = SILK_NO_ERROR;
Chris@69 68 silk_decoder_state *channel_state = ((silk_decoder *)decState)->channel_state;
Chris@69 69
Chris@69 70 for( n = 0; n < DECODER_NUM_CHANNELS; n++ ) {
Chris@69 71 ret = silk_init_decoder( &channel_state[ n ] );
Chris@69 72 }
Chris@69 73 silk_memset(&((silk_decoder *)decState)->sStereo, 0, sizeof(((silk_decoder *)decState)->sStereo));
Chris@69 74 /* Not strictly needed, but it's cleaner that way */
Chris@69 75 ((silk_decoder *)decState)->prev_decode_only_middle = 0;
Chris@69 76
Chris@69 77 return ret;
Chris@69 78 }
Chris@69 79
Chris@69 80 /* Decode a frame */
Chris@69 81 opus_int silk_Decode( /* O Returns error code */
Chris@69 82 void* decState, /* I/O State */
Chris@69 83 silk_DecControlStruct* decControl, /* I/O Control Structure */
Chris@69 84 opus_int lostFlag, /* I 0: no loss, 1 loss, 2 decode fec */
Chris@69 85 opus_int newPacketFlag, /* I Indicates first decoder call for this packet */
Chris@69 86 ec_dec *psRangeDec, /* I/O Compressor data structure */
Chris@69 87 opus_int16 *samplesOut, /* O Decoded output speech vector */
Chris@69 88 opus_int32 *nSamplesOut, /* O Number of samples decoded */
Chris@69 89 int arch /* I Run-time architecture */
Chris@69 90 )
Chris@69 91 {
Chris@69 92 opus_int i, n, decode_only_middle = 0, ret = SILK_NO_ERROR;
Chris@69 93 opus_int32 nSamplesOutDec, LBRR_symbol;
Chris@69 94 opus_int16 *samplesOut1_tmp[ 2 ];
Chris@69 95 VARDECL( opus_int16, samplesOut1_tmp_storage1 );
Chris@69 96 VARDECL( opus_int16, samplesOut1_tmp_storage2 );
Chris@69 97 VARDECL( opus_int16, samplesOut2_tmp );
Chris@69 98 opus_int32 MS_pred_Q13[ 2 ] = { 0 };
Chris@69 99 opus_int16 *resample_out_ptr;
Chris@69 100 silk_decoder *psDec = ( silk_decoder * )decState;
Chris@69 101 silk_decoder_state *channel_state = psDec->channel_state;
Chris@69 102 opus_int has_side;
Chris@69 103 opus_int stereo_to_mono;
Chris@69 104 int delay_stack_alloc;
Chris@69 105 SAVE_STACK;
Chris@69 106
Chris@69 107 celt_assert( decControl->nChannelsInternal == 1 || decControl->nChannelsInternal == 2 );
Chris@69 108
Chris@69 109 /**********************************/
Chris@69 110 /* Test if first frame in payload */
Chris@69 111 /**********************************/
Chris@69 112 if( newPacketFlag ) {
Chris@69 113 for( n = 0; n < decControl->nChannelsInternal; n++ ) {
Chris@69 114 channel_state[ n ].nFramesDecoded = 0; /* Used to count frames in packet */
Chris@69 115 }
Chris@69 116 }
Chris@69 117
Chris@69 118 /* If Mono -> Stereo transition in bitstream: init state of second channel */
Chris@69 119 if( decControl->nChannelsInternal > psDec->nChannelsInternal ) {
Chris@69 120 ret += silk_init_decoder( &channel_state[ 1 ] );
Chris@69 121 }
Chris@69 122
Chris@69 123 stereo_to_mono = decControl->nChannelsInternal == 1 && psDec->nChannelsInternal == 2 &&
Chris@69 124 ( decControl->internalSampleRate == 1000*channel_state[ 0 ].fs_kHz );
Chris@69 125
Chris@69 126 if( channel_state[ 0 ].nFramesDecoded == 0 ) {
Chris@69 127 for( n = 0; n < decControl->nChannelsInternal; n++ ) {
Chris@69 128 opus_int fs_kHz_dec;
Chris@69 129 if( decControl->payloadSize_ms == 0 ) {
Chris@69 130 /* Assuming packet loss, use 10 ms */
Chris@69 131 channel_state[ n ].nFramesPerPacket = 1;
Chris@69 132 channel_state[ n ].nb_subfr = 2;
Chris@69 133 } else if( decControl->payloadSize_ms == 10 ) {
Chris@69 134 channel_state[ n ].nFramesPerPacket = 1;
Chris@69 135 channel_state[ n ].nb_subfr = 2;
Chris@69 136 } else if( decControl->payloadSize_ms == 20 ) {
Chris@69 137 channel_state[ n ].nFramesPerPacket = 1;
Chris@69 138 channel_state[ n ].nb_subfr = 4;
Chris@69 139 } else if( decControl->payloadSize_ms == 40 ) {
Chris@69 140 channel_state[ n ].nFramesPerPacket = 2;
Chris@69 141 channel_state[ n ].nb_subfr = 4;
Chris@69 142 } else if( decControl->payloadSize_ms == 60 ) {
Chris@69 143 channel_state[ n ].nFramesPerPacket = 3;
Chris@69 144 channel_state[ n ].nb_subfr = 4;
Chris@69 145 } else {
Chris@69 146 celt_assert( 0 );
Chris@69 147 RESTORE_STACK;
Chris@69 148 return SILK_DEC_INVALID_FRAME_SIZE;
Chris@69 149 }
Chris@69 150 fs_kHz_dec = ( decControl->internalSampleRate >> 10 ) + 1;
Chris@69 151 if( fs_kHz_dec != 8 && fs_kHz_dec != 12 && fs_kHz_dec != 16 ) {
Chris@69 152 celt_assert( 0 );
Chris@69 153 RESTORE_STACK;
Chris@69 154 return SILK_DEC_INVALID_SAMPLING_FREQUENCY;
Chris@69 155 }
Chris@69 156 ret += silk_decoder_set_fs( &channel_state[ n ], fs_kHz_dec, decControl->API_sampleRate );
Chris@69 157 }
Chris@69 158 }
Chris@69 159
Chris@69 160 if( decControl->nChannelsAPI == 2 && decControl->nChannelsInternal == 2 && ( psDec->nChannelsAPI == 1 || psDec->nChannelsInternal == 1 ) ) {
Chris@69 161 silk_memset( psDec->sStereo.pred_prev_Q13, 0, sizeof( psDec->sStereo.pred_prev_Q13 ) );
Chris@69 162 silk_memset( psDec->sStereo.sSide, 0, sizeof( psDec->sStereo.sSide ) );
Chris@69 163 silk_memcpy( &channel_state[ 1 ].resampler_state, &channel_state[ 0 ].resampler_state, sizeof( silk_resampler_state_struct ) );
Chris@69 164 }
Chris@69 165 psDec->nChannelsAPI = decControl->nChannelsAPI;
Chris@69 166 psDec->nChannelsInternal = decControl->nChannelsInternal;
Chris@69 167
Chris@69 168 if( decControl->API_sampleRate > (opus_int32)MAX_API_FS_KHZ * 1000 || decControl->API_sampleRate < 8000 ) {
Chris@69 169 ret = SILK_DEC_INVALID_SAMPLING_FREQUENCY;
Chris@69 170 RESTORE_STACK;
Chris@69 171 return( ret );
Chris@69 172 }
Chris@69 173
Chris@69 174 if( lostFlag != FLAG_PACKET_LOST && channel_state[ 0 ].nFramesDecoded == 0 ) {
Chris@69 175 /* First decoder call for this payload */
Chris@69 176 /* Decode VAD flags and LBRR flag */
Chris@69 177 for( n = 0; n < decControl->nChannelsInternal; n++ ) {
Chris@69 178 for( i = 0; i < channel_state[ n ].nFramesPerPacket; i++ ) {
Chris@69 179 channel_state[ n ].VAD_flags[ i ] = ec_dec_bit_logp(psRangeDec, 1);
Chris@69 180 }
Chris@69 181 channel_state[ n ].LBRR_flag = ec_dec_bit_logp(psRangeDec, 1);
Chris@69 182 }
Chris@69 183 /* Decode LBRR flags */
Chris@69 184 for( n = 0; n < decControl->nChannelsInternal; n++ ) {
Chris@69 185 silk_memset( channel_state[ n ].LBRR_flags, 0, sizeof( channel_state[ n ].LBRR_flags ) );
Chris@69 186 if( channel_state[ n ].LBRR_flag ) {
Chris@69 187 if( channel_state[ n ].nFramesPerPacket == 1 ) {
Chris@69 188 channel_state[ n ].LBRR_flags[ 0 ] = 1;
Chris@69 189 } else {
Chris@69 190 LBRR_symbol = ec_dec_icdf( psRangeDec, silk_LBRR_flags_iCDF_ptr[ channel_state[ n ].nFramesPerPacket - 2 ], 8 ) + 1;
Chris@69 191 for( i = 0; i < channel_state[ n ].nFramesPerPacket; i++ ) {
Chris@69 192 channel_state[ n ].LBRR_flags[ i ] = silk_RSHIFT( LBRR_symbol, i ) & 1;
Chris@69 193 }
Chris@69 194 }
Chris@69 195 }
Chris@69 196 }
Chris@69 197
Chris@69 198 if( lostFlag == FLAG_DECODE_NORMAL ) {
Chris@69 199 /* Regular decoding: skip all LBRR data */
Chris@69 200 for( i = 0; i < channel_state[ 0 ].nFramesPerPacket; i++ ) {
Chris@69 201 for( n = 0; n < decControl->nChannelsInternal; n++ ) {
Chris@69 202 if( channel_state[ n ].LBRR_flags[ i ] ) {
Chris@69 203 opus_int16 pulses[ MAX_FRAME_LENGTH ];
Chris@69 204 opus_int condCoding;
Chris@69 205
Chris@69 206 if( decControl->nChannelsInternal == 2 && n == 0 ) {
Chris@69 207 silk_stereo_decode_pred( psRangeDec, MS_pred_Q13 );
Chris@69 208 if( channel_state[ 1 ].LBRR_flags[ i ] == 0 ) {
Chris@69 209 silk_stereo_decode_mid_only( psRangeDec, &decode_only_middle );
Chris@69 210 }
Chris@69 211 }
Chris@69 212 /* Use conditional coding if previous frame available */
Chris@69 213 if( i > 0 && channel_state[ n ].LBRR_flags[ i - 1 ] ) {
Chris@69 214 condCoding = CODE_CONDITIONALLY;
Chris@69 215 } else {
Chris@69 216 condCoding = CODE_INDEPENDENTLY;
Chris@69 217 }
Chris@69 218 silk_decode_indices( &channel_state[ n ], psRangeDec, i, 1, condCoding );
Chris@69 219 silk_decode_pulses( psRangeDec, pulses, channel_state[ n ].indices.signalType,
Chris@69 220 channel_state[ n ].indices.quantOffsetType, channel_state[ n ].frame_length );
Chris@69 221 }
Chris@69 222 }
Chris@69 223 }
Chris@69 224 }
Chris@69 225 }
Chris@69 226
Chris@69 227 /* Get MS predictor index */
Chris@69 228 if( decControl->nChannelsInternal == 2 ) {
Chris@69 229 if( lostFlag == FLAG_DECODE_NORMAL ||
Chris@69 230 ( lostFlag == FLAG_DECODE_LBRR && channel_state[ 0 ].LBRR_flags[ channel_state[ 0 ].nFramesDecoded ] == 1 ) )
Chris@69 231 {
Chris@69 232 silk_stereo_decode_pred( psRangeDec, MS_pred_Q13 );
Chris@69 233 /* For LBRR data, decode mid-only flag only if side-channel's LBRR flag is false */
Chris@69 234 if( ( lostFlag == FLAG_DECODE_NORMAL && channel_state[ 1 ].VAD_flags[ channel_state[ 0 ].nFramesDecoded ] == 0 ) ||
Chris@69 235 ( lostFlag == FLAG_DECODE_LBRR && channel_state[ 1 ].LBRR_flags[ channel_state[ 0 ].nFramesDecoded ] == 0 ) )
Chris@69 236 {
Chris@69 237 silk_stereo_decode_mid_only( psRangeDec, &decode_only_middle );
Chris@69 238 } else {
Chris@69 239 decode_only_middle = 0;
Chris@69 240 }
Chris@69 241 } else {
Chris@69 242 for( n = 0; n < 2; n++ ) {
Chris@69 243 MS_pred_Q13[ n ] = psDec->sStereo.pred_prev_Q13[ n ];
Chris@69 244 }
Chris@69 245 }
Chris@69 246 }
Chris@69 247
Chris@69 248 /* Reset side channel decoder prediction memory for first frame with side coding */
Chris@69 249 if( decControl->nChannelsInternal == 2 && decode_only_middle == 0 && psDec->prev_decode_only_middle == 1 ) {
Chris@69 250 silk_memset( psDec->channel_state[ 1 ].outBuf, 0, sizeof(psDec->channel_state[ 1 ].outBuf) );
Chris@69 251 silk_memset( psDec->channel_state[ 1 ].sLPC_Q14_buf, 0, sizeof(psDec->channel_state[ 1 ].sLPC_Q14_buf) );
Chris@69 252 psDec->channel_state[ 1 ].lagPrev = 100;
Chris@69 253 psDec->channel_state[ 1 ].LastGainIndex = 10;
Chris@69 254 psDec->channel_state[ 1 ].prevSignalType = TYPE_NO_VOICE_ACTIVITY;
Chris@69 255 psDec->channel_state[ 1 ].first_frame_after_reset = 1;
Chris@69 256 }
Chris@69 257
Chris@69 258 /* Check if the temp buffer fits into the output PCM buffer. If it fits,
Chris@69 259 we can delay allocating the temp buffer until after the SILK peak stack
Chris@69 260 usage. We need to use a < and not a <= because of the two extra samples. */
Chris@69 261 delay_stack_alloc = decControl->internalSampleRate*decControl->nChannelsInternal
Chris@69 262 < decControl->API_sampleRate*decControl->nChannelsAPI;
Chris@69 263 ALLOC( samplesOut1_tmp_storage1, delay_stack_alloc ? ALLOC_NONE
Chris@69 264 : decControl->nChannelsInternal*(channel_state[ 0 ].frame_length + 2 ),
Chris@69 265 opus_int16 );
Chris@69 266 if ( delay_stack_alloc )
Chris@69 267 {
Chris@69 268 samplesOut1_tmp[ 0 ] = samplesOut;
Chris@69 269 samplesOut1_tmp[ 1 ] = samplesOut + channel_state[ 0 ].frame_length + 2;
Chris@69 270 } else {
Chris@69 271 samplesOut1_tmp[ 0 ] = samplesOut1_tmp_storage1;
Chris@69 272 samplesOut1_tmp[ 1 ] = samplesOut1_tmp_storage1 + channel_state[ 0 ].frame_length + 2;
Chris@69 273 }
Chris@69 274
Chris@69 275 if( lostFlag == FLAG_DECODE_NORMAL ) {
Chris@69 276 has_side = !decode_only_middle;
Chris@69 277 } else {
Chris@69 278 has_side = !psDec->prev_decode_only_middle
Chris@69 279 || (decControl->nChannelsInternal == 2 && lostFlag == FLAG_DECODE_LBRR && channel_state[1].LBRR_flags[ channel_state[1].nFramesDecoded ] == 1 );
Chris@69 280 }
Chris@69 281 /* Call decoder for one frame */
Chris@69 282 for( n = 0; n < decControl->nChannelsInternal; n++ ) {
Chris@69 283 if( n == 0 || has_side ) {
Chris@69 284 opus_int FrameIndex;
Chris@69 285 opus_int condCoding;
Chris@69 286
Chris@69 287 FrameIndex = channel_state[ 0 ].nFramesDecoded - n;
Chris@69 288 /* Use independent coding if no previous frame available */
Chris@69 289 if( FrameIndex <= 0 ) {
Chris@69 290 condCoding = CODE_INDEPENDENTLY;
Chris@69 291 } else if( lostFlag == FLAG_DECODE_LBRR ) {
Chris@69 292 condCoding = channel_state[ n ].LBRR_flags[ FrameIndex - 1 ] ? CODE_CONDITIONALLY : CODE_INDEPENDENTLY;
Chris@69 293 } else if( n > 0 && psDec->prev_decode_only_middle ) {
Chris@69 294 /* If we skipped a side frame in this packet, we don't
Chris@69 295 need LTP scaling; the LTP state is well-defined. */
Chris@69 296 condCoding = CODE_INDEPENDENTLY_NO_LTP_SCALING;
Chris@69 297 } else {
Chris@69 298 condCoding = CODE_CONDITIONALLY;
Chris@69 299 }
Chris@69 300 ret += silk_decode_frame( &channel_state[ n ], psRangeDec, &samplesOut1_tmp[ n ][ 2 ], &nSamplesOutDec, lostFlag, condCoding, arch);
Chris@69 301 } else {
Chris@69 302 silk_memset( &samplesOut1_tmp[ n ][ 2 ], 0, nSamplesOutDec * sizeof( opus_int16 ) );
Chris@69 303 }
Chris@69 304 channel_state[ n ].nFramesDecoded++;
Chris@69 305 }
Chris@69 306
Chris@69 307 if( decControl->nChannelsAPI == 2 && decControl->nChannelsInternal == 2 ) {
Chris@69 308 /* Convert Mid/Side to Left/Right */
Chris@69 309 silk_stereo_MS_to_LR( &psDec->sStereo, samplesOut1_tmp[ 0 ], samplesOut1_tmp[ 1 ], MS_pred_Q13, channel_state[ 0 ].fs_kHz, nSamplesOutDec );
Chris@69 310 } else {
Chris@69 311 /* Buffering */
Chris@69 312 silk_memcpy( samplesOut1_tmp[ 0 ], psDec->sStereo.sMid, 2 * sizeof( opus_int16 ) );
Chris@69 313 silk_memcpy( psDec->sStereo.sMid, &samplesOut1_tmp[ 0 ][ nSamplesOutDec ], 2 * sizeof( opus_int16 ) );
Chris@69 314 }
Chris@69 315
Chris@69 316 /* Number of output samples */
Chris@69 317 *nSamplesOut = silk_DIV32( nSamplesOutDec * decControl->API_sampleRate, silk_SMULBB( channel_state[ 0 ].fs_kHz, 1000 ) );
Chris@69 318
Chris@69 319 /* Set up pointers to temp buffers */
Chris@69 320 ALLOC( samplesOut2_tmp,
Chris@69 321 decControl->nChannelsAPI == 2 ? *nSamplesOut : ALLOC_NONE, opus_int16 );
Chris@69 322 if( decControl->nChannelsAPI == 2 ) {
Chris@69 323 resample_out_ptr = samplesOut2_tmp;
Chris@69 324 } else {
Chris@69 325 resample_out_ptr = samplesOut;
Chris@69 326 }
Chris@69 327
Chris@69 328 ALLOC( samplesOut1_tmp_storage2, delay_stack_alloc
Chris@69 329 ? decControl->nChannelsInternal*(channel_state[ 0 ].frame_length + 2 )
Chris@69 330 : ALLOC_NONE,
Chris@69 331 opus_int16 );
Chris@69 332 if ( delay_stack_alloc ) {
Chris@69 333 OPUS_COPY(samplesOut1_tmp_storage2, samplesOut, decControl->nChannelsInternal*(channel_state[ 0 ].frame_length + 2));
Chris@69 334 samplesOut1_tmp[ 0 ] = samplesOut1_tmp_storage2;
Chris@69 335 samplesOut1_tmp[ 1 ] = samplesOut1_tmp_storage2 + channel_state[ 0 ].frame_length + 2;
Chris@69 336 }
Chris@69 337 for( n = 0; n < silk_min( decControl->nChannelsAPI, decControl->nChannelsInternal ); n++ ) {
Chris@69 338
Chris@69 339 /* Resample decoded signal to API_sampleRate */
Chris@69 340 ret += silk_resampler( &channel_state[ n ].resampler_state, resample_out_ptr, &samplesOut1_tmp[ n ][ 1 ], nSamplesOutDec );
Chris@69 341
Chris@69 342 /* Interleave if stereo output and stereo stream */
Chris@69 343 if( decControl->nChannelsAPI == 2 ) {
Chris@69 344 for( i = 0; i < *nSamplesOut; i++ ) {
Chris@69 345 samplesOut[ n + 2 * i ] = resample_out_ptr[ i ];
Chris@69 346 }
Chris@69 347 }
Chris@69 348 }
Chris@69 349
Chris@69 350 /* Create two channel output from mono stream */
Chris@69 351 if( decControl->nChannelsAPI == 2 && decControl->nChannelsInternal == 1 ) {
Chris@69 352 if ( stereo_to_mono ){
Chris@69 353 /* Resample right channel for newly collapsed stereo just in case
Chris@69 354 we weren't doing collapsing when switching to mono */
Chris@69 355 ret += silk_resampler( &channel_state[ 1 ].resampler_state, resample_out_ptr, &samplesOut1_tmp[ 0 ][ 1 ], nSamplesOutDec );
Chris@69 356
Chris@69 357 for( i = 0; i < *nSamplesOut; i++ ) {
Chris@69 358 samplesOut[ 1 + 2 * i ] = resample_out_ptr[ i ];
Chris@69 359 }
Chris@69 360 } else {
Chris@69 361 for( i = 0; i < *nSamplesOut; i++ ) {
Chris@69 362 samplesOut[ 1 + 2 * i ] = samplesOut[ 0 + 2 * i ];
Chris@69 363 }
Chris@69 364 }
Chris@69 365 }
Chris@69 366
Chris@69 367 /* Export pitch lag, measured at 48 kHz sampling rate */
Chris@69 368 if( channel_state[ 0 ].prevSignalType == TYPE_VOICED ) {
Chris@69 369 int mult_tab[ 3 ] = { 6, 4, 3 };
Chris@69 370 decControl->prevPitchLag = channel_state[ 0 ].lagPrev * mult_tab[ ( channel_state[ 0 ].fs_kHz - 8 ) >> 2 ];
Chris@69 371 } else {
Chris@69 372 decControl->prevPitchLag = 0;
Chris@69 373 }
Chris@69 374
Chris@69 375 if( lostFlag == FLAG_PACKET_LOST ) {
Chris@69 376 /* On packet loss, remove the gain clamping to prevent having the energy "bounce back"
Chris@69 377 if we lose packets when the energy is going down */
Chris@69 378 for ( i = 0; i < psDec->nChannelsInternal; i++ )
Chris@69 379 psDec->channel_state[ i ].LastGainIndex = 10;
Chris@69 380 } else {
Chris@69 381 psDec->prev_decode_only_middle = decode_only_middle;
Chris@69 382 }
Chris@69 383 RESTORE_STACK;
Chris@69 384 return ret;
Chris@69 385 }
Chris@69 386
Chris@69 387 #if 0
Chris@69 388 /* Getting table of contents for a packet */
Chris@69 389 opus_int silk_get_TOC(
Chris@69 390 const opus_uint8 *payload, /* I Payload data */
Chris@69 391 const opus_int nBytesIn, /* I Number of input bytes */
Chris@69 392 const opus_int nFramesPerPayload, /* I Number of SILK frames per payload */
Chris@69 393 silk_TOC_struct *Silk_TOC /* O Type of content */
Chris@69 394 )
Chris@69 395 {
Chris@69 396 opus_int i, flags, ret = SILK_NO_ERROR;
Chris@69 397
Chris@69 398 if( nBytesIn < 1 ) {
Chris@69 399 return -1;
Chris@69 400 }
Chris@69 401 if( nFramesPerPayload < 0 || nFramesPerPayload > 3 ) {
Chris@69 402 return -1;
Chris@69 403 }
Chris@69 404
Chris@69 405 silk_memset( Silk_TOC, 0, sizeof( *Silk_TOC ) );
Chris@69 406
Chris@69 407 /* For stereo, extract the flags for the mid channel */
Chris@69 408 flags = silk_RSHIFT( payload[ 0 ], 7 - nFramesPerPayload ) & ( silk_LSHIFT( 1, nFramesPerPayload + 1 ) - 1 );
Chris@69 409
Chris@69 410 Silk_TOC->inbandFECFlag = flags & 1;
Chris@69 411 for( i = nFramesPerPayload - 1; i >= 0 ; i-- ) {
Chris@69 412 flags = silk_RSHIFT( flags, 1 );
Chris@69 413 Silk_TOC->VADFlags[ i ] = flags & 1;
Chris@69 414 Silk_TOC->VADFlag |= flags & 1;
Chris@69 415 }
Chris@69 416
Chris@69 417 return ret;
Chris@69 418 }
Chris@69 419 #endif