michael@0: /*********************************************************************** michael@0: Copyright (c) 2006-2011, Skype Limited. All rights reserved. michael@0: Redistribution and use in source and binary forms, with or without michael@0: modification, are permitted provided that the following conditions michael@0: are met: michael@0: - Redistributions of source code must retain the above copyright notice, michael@0: this list of conditions and the following disclaimer. michael@0: - Redistributions in binary form must reproduce the above copyright michael@0: notice, this list of conditions and the following disclaimer in the michael@0: documentation and/or other materials provided with the distribution. michael@0: - Neither the name of Internet Society, IETF or IETF Trust, nor the michael@0: names of specific contributors, may be used to endorse or promote michael@0: products derived from this software without specific prior written michael@0: permission. michael@0: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" michael@0: AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE michael@0: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE michael@0: ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE michael@0: LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR michael@0: CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF michael@0: SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS michael@0: INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN michael@0: CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) michael@0: ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE michael@0: POSSIBILITY OF SUCH DAMAGE. michael@0: ***********************************************************************/ michael@0: michael@0: #ifdef HAVE_CONFIG_H michael@0: #include "config.h" michael@0: #endif michael@0: michael@0: #include "main.h" michael@0: #include "stack_alloc.h" michael@0: #include "PLC.h" michael@0: michael@0: /****************/ michael@0: /* Decode frame */ michael@0: /****************/ michael@0: opus_int silk_decode_frame( michael@0: silk_decoder_state *psDec, /* I/O Pointer to Silk decoder state */ michael@0: ec_dec *psRangeDec, /* I/O Compressor data structure */ michael@0: opus_int16 pOut[], /* O Pointer to output speech frame */ michael@0: opus_int32 *pN, /* O Pointer to size of output frame */ michael@0: opus_int lostFlag, /* I 0: no loss, 1 loss, 2 decode fec */ michael@0: opus_int condCoding /* I The type of conditional coding to use */ michael@0: ) michael@0: { michael@0: VARDECL( silk_decoder_control, psDecCtrl ); michael@0: opus_int L, mv_len, ret = 0; michael@0: VARDECL( opus_int, pulses ); michael@0: SAVE_STACK; michael@0: michael@0: L = psDec->frame_length; michael@0: ALLOC( psDecCtrl, 1, silk_decoder_control ); michael@0: ALLOC( pulses, (L + SHELL_CODEC_FRAME_LENGTH - 1) & michael@0: ~(SHELL_CODEC_FRAME_LENGTH - 1), opus_int ); michael@0: psDecCtrl->LTP_scale_Q14 = 0; michael@0: michael@0: /* Safety checks */ michael@0: silk_assert( L > 0 && L <= MAX_FRAME_LENGTH ); michael@0: michael@0: if( lostFlag == FLAG_DECODE_NORMAL || michael@0: ( lostFlag == FLAG_DECODE_LBRR && psDec->LBRR_flags[ psDec->nFramesDecoded ] == 1 ) ) michael@0: { michael@0: /*********************************************/ michael@0: /* Decode quantization indices of side info */ michael@0: /*********************************************/ michael@0: silk_decode_indices( psDec, psRangeDec, psDec->nFramesDecoded, lostFlag, condCoding ); michael@0: michael@0: /*********************************************/ michael@0: /* Decode quantization indices of excitation */ michael@0: /*********************************************/ michael@0: silk_decode_pulses( psRangeDec, pulses, psDec->indices.signalType, michael@0: psDec->indices.quantOffsetType, psDec->frame_length ); michael@0: michael@0: /********************************************/ michael@0: /* Decode parameters and pulse signal */ michael@0: /********************************************/ michael@0: silk_decode_parameters( psDec, psDecCtrl, condCoding ); michael@0: michael@0: /********************************************************/ michael@0: /* Run inverse NSQ */ michael@0: /********************************************************/ michael@0: silk_decode_core( psDec, psDecCtrl, pOut, pulses ); michael@0: michael@0: /********************************************************/ michael@0: /* Update PLC state */ michael@0: /********************************************************/ michael@0: silk_PLC( psDec, psDecCtrl, pOut, 0 ); michael@0: michael@0: psDec->lossCnt = 0; michael@0: psDec->prevSignalType = psDec->indices.signalType; michael@0: silk_assert( psDec->prevSignalType >= 0 && psDec->prevSignalType <= 2 ); michael@0: michael@0: /* A frame has been decoded without errors */ michael@0: psDec->first_frame_after_reset = 0; michael@0: } else { michael@0: /* Handle packet loss by extrapolation */ michael@0: silk_PLC( psDec, psDecCtrl, pOut, 1 ); michael@0: } michael@0: michael@0: /*************************/ michael@0: /* Update output buffer. */ michael@0: /*************************/ michael@0: silk_assert( psDec->ltp_mem_length >= psDec->frame_length ); michael@0: mv_len = psDec->ltp_mem_length - psDec->frame_length; michael@0: silk_memmove( psDec->outBuf, &psDec->outBuf[ psDec->frame_length ], mv_len * sizeof(opus_int16) ); michael@0: silk_memcpy( &psDec->outBuf[ mv_len ], pOut, psDec->frame_length * sizeof( opus_int16 ) ); michael@0: michael@0: /****************************************************************/ michael@0: /* Ensure smooth connection of extrapolated and good frames */ michael@0: /****************************************************************/ michael@0: silk_PLC_glue_frames( psDec, pOut, L ); michael@0: michael@0: /************************************************/ michael@0: /* Comfort noise generation / estimation */ michael@0: /************************************************/ michael@0: silk_CNG( psDec, psDecCtrl, pOut, L ); michael@0: michael@0: /* Update some decoder state variables */ michael@0: psDec->lagPrev = psDecCtrl->pitchL[ psDec->nb_subfr - 1 ]; michael@0: michael@0: /* Set output frame length */ michael@0: *pN = L; michael@0: michael@0: RESTORE_STACK; michael@0: return ret; michael@0: }