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