12bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian/***********************************************************************
22bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh VenkatasubramanianCopyright (c) 2006-2011, Skype Limited. All rights reserved.
32bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh VenkatasubramanianRedistribution and use in source and binary forms, with or without
42bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanianmodification, are permitted provided that the following conditions
52bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanianare met:
62bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian- Redistributions of source code must retain the above copyright notice,
72bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanianthis list of conditions and the following disclaimer.
82bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian- Redistributions in binary form must reproduce the above copyright
92bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramaniannotice, this list of conditions and the following disclaimer in the
102bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramaniandocumentation and/or other materials provided with the distribution.
112bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian- Neither the name of Internet Society, IETF or IETF Trust, nor the
122bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramaniannames of specific contributors, may be used to endorse or promote
132bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanianproducts derived from this software without specific prior written
142bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanianpermission.
152bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh VenkatasubramanianTHIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
162bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh VenkatasubramanianAND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
172bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh VenkatasubramanianIMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
182bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh VenkatasubramanianARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
192bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh VenkatasubramanianLIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
202bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh VenkatasubramanianCONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
212bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh VenkatasubramanianSUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
222bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh VenkatasubramanianINTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
232bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh VenkatasubramanianCONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
242bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh VenkatasubramanianARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
252bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh VenkatasubramanianPOSSIBILITY OF SUCH DAMAGE.
262bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian***********************************************************************/
272bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian
282bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian#ifdef HAVE_CONFIG_H
292bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian#include "config.h"
302bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian#endif
312bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian
322bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian#include "main.h"
332bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian
342bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian/* Set decoder sampling rate */
352bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanianopus_int silk_decoder_set_fs(
362bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian    silk_decoder_state          *psDec,                         /* I/O  Decoder state pointer                       */
372bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian    opus_int                    fs_kHz,                         /* I    Sampling frequency (kHz)                    */
382bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian    opus_int32                  fs_API_Hz                       /* I    API Sampling frequency (Hz)                 */
392bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian)
402bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian{
412bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian    opus_int frame_length, ret = 0;
422bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian
432bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian    silk_assert( fs_kHz == 8 || fs_kHz == 12 || fs_kHz == 16 );
442bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian    silk_assert( psDec->nb_subfr == MAX_NB_SUBFR || psDec->nb_subfr == MAX_NB_SUBFR/2 );
452bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian
462bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian    /* New (sub)frame length */
472bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian    psDec->subfr_length = silk_SMULBB( SUB_FRAME_LENGTH_MS, fs_kHz );
482bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian    frame_length = silk_SMULBB( psDec->nb_subfr, psDec->subfr_length );
492bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian
502bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian    /* Initialize resampler when switching internal or external sampling frequency */
512bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian    if( psDec->fs_kHz != fs_kHz || psDec->fs_API_hz != fs_API_Hz ) {
522bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian        /* Initialize the resampler for dec_API.c preparing resampling from fs_kHz to API_fs_Hz */
532bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian        ret += silk_resampler_init( &psDec->resampler_state, silk_SMULBB( fs_kHz, 1000 ), fs_API_Hz, 0 );
542bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian
552bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian        psDec->fs_API_hz = fs_API_Hz;
562bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian    }
572bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian
582bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian    if( psDec->fs_kHz != fs_kHz || frame_length != psDec->frame_length ) {
592bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian        if( fs_kHz == 8 ) {
602bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian            if( psDec->nb_subfr == MAX_NB_SUBFR ) {
612bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian                psDec->pitch_contour_iCDF = silk_pitch_contour_NB_iCDF;
622bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian            } else {
632bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian                psDec->pitch_contour_iCDF = silk_pitch_contour_10_ms_NB_iCDF;
642bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian            }
652bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian        } else {
662bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian            if( psDec->nb_subfr == MAX_NB_SUBFR ) {
672bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian                psDec->pitch_contour_iCDF = silk_pitch_contour_iCDF;
682bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian            } else {
692bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian                psDec->pitch_contour_iCDF = silk_pitch_contour_10_ms_iCDF;
702bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian            }
712bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian        }
722bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian        if( psDec->fs_kHz != fs_kHz ) {
732bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian            psDec->ltp_mem_length = silk_SMULBB( LTP_MEM_LENGTH_MS, fs_kHz );
742bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian            if( fs_kHz == 8 || fs_kHz == 12 ) {
752bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian                psDec->LPC_order = MIN_LPC_ORDER;
762bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian                psDec->psNLSF_CB = &silk_NLSF_CB_NB_MB;
772bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian            } else {
782bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian                psDec->LPC_order = MAX_LPC_ORDER;
792bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian                psDec->psNLSF_CB = &silk_NLSF_CB_WB;
802bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian            }
812bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian            if( fs_kHz == 16 ) {
822bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian                psDec->pitch_lag_low_bits_iCDF = silk_uniform8_iCDF;
832bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian            } else if( fs_kHz == 12 ) {
842bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian                psDec->pitch_lag_low_bits_iCDF = silk_uniform6_iCDF;
852bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian            } else if( fs_kHz == 8 ) {
862bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian                psDec->pitch_lag_low_bits_iCDF = silk_uniform4_iCDF;
872bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian            } else {
882bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian                /* unsupported sampling rate */
892bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian                silk_assert( 0 );
902bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian            }
912bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian            psDec->first_frame_after_reset = 1;
922bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian            psDec->lagPrev                 = 100;
932bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian            psDec->LastGainIndex           = 10;
942bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian            psDec->prevSignalType          = TYPE_NO_VOICE_ACTIVITY;
952bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian            silk_memset( psDec->outBuf, 0, sizeof(psDec->outBuf));
962bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian            silk_memset( psDec->sLPC_Q14_buf, 0, sizeof(psDec->sLPC_Q14_buf) );
972bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian        }
982bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian
992bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian        psDec->fs_kHz       = fs_kHz;
1002bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian        psDec->frame_length = frame_length;
1012bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian    }
1022bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian
1032bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian    /* Check that settings are valid */
1042bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian    silk_assert( psDec->frame_length > 0 && psDec->frame_length <= MAX_FRAME_LENGTH );
1052bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian
1062bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian    return ret;
1072bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian}
1082bd8b54017b5320bc0c1df9bf86f4cdc9f8db242Vignesh Venkatasubramanian
109