Chris@69: /*********************************************************************** Chris@69: Copyright (c) 2006-2011, Skype Limited. All rights reserved. Chris@69: Redistribution and use in source and binary forms, with or without Chris@69: modification, are permitted provided that the following conditions Chris@69: are met: Chris@69: - Redistributions of source code must retain the above copyright notice, Chris@69: this list of conditions and the following disclaimer. Chris@69: - Redistributions in binary form must reproduce the above copyright Chris@69: notice, this list of conditions and the following disclaimer in the Chris@69: documentation and/or other materials provided with the distribution. Chris@69: - Neither the name of Internet Society, IETF or IETF Trust, nor the Chris@69: names of specific contributors, may be used to endorse or promote Chris@69: products derived from this software without specific prior written Chris@69: permission. Chris@69: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" Chris@69: AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE Chris@69: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE Chris@69: ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE Chris@69: LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR Chris@69: CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF Chris@69: SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS Chris@69: INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN Chris@69: CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) Chris@69: ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE Chris@69: POSSIBILITY OF SUCH DAMAGE. Chris@69: ***********************************************************************/ Chris@69: Chris@69: #ifdef HAVE_CONFIG_H Chris@69: #include "config.h" Chris@69: #endif Chris@69: Chris@69: #include "main.h" Chris@69: #include "tuning_parameters.h" Chris@69: Chris@69: /* Control internal sampling rate */ Chris@69: opus_int silk_control_audio_bandwidth( Chris@69: silk_encoder_state *psEncC, /* I/O Pointer to Silk encoder state */ Chris@69: silk_EncControlStruct *encControl /* I Control structure */ Chris@69: ) Chris@69: { Chris@69: opus_int fs_kHz; Chris@69: opus_int orig_kHz; Chris@69: opus_int32 fs_Hz; Chris@69: Chris@69: orig_kHz = psEncC->fs_kHz; Chris@69: /* Handle a bandwidth-switching reset where we need to be aware what the last sampling rate was. */ Chris@69: if( orig_kHz == 0 ) { Chris@69: orig_kHz = psEncC->sLP.saved_fs_kHz; Chris@69: } Chris@69: fs_kHz = orig_kHz; Chris@69: fs_Hz = silk_SMULBB( fs_kHz, 1000 ); Chris@69: if( fs_Hz == 0 ) { Chris@69: /* Encoder has just been initialized */ Chris@69: fs_Hz = silk_min( psEncC->desiredInternal_fs_Hz, psEncC->API_fs_Hz ); Chris@69: fs_kHz = silk_DIV32_16( fs_Hz, 1000 ); Chris@69: } else if( fs_Hz > psEncC->API_fs_Hz || fs_Hz > psEncC->maxInternal_fs_Hz || fs_Hz < psEncC->minInternal_fs_Hz ) { Chris@69: /* Make sure internal rate is not higher than external rate or maximum allowed, or lower than minimum allowed */ Chris@69: fs_Hz = psEncC->API_fs_Hz; Chris@69: fs_Hz = silk_min( fs_Hz, psEncC->maxInternal_fs_Hz ); Chris@69: fs_Hz = silk_max( fs_Hz, psEncC->minInternal_fs_Hz ); Chris@69: fs_kHz = silk_DIV32_16( fs_Hz, 1000 ); Chris@69: } else { Chris@69: /* State machine for the internal sampling rate switching */ Chris@69: if( psEncC->sLP.transition_frame_no >= TRANSITION_FRAMES ) { Chris@69: /* Stop transition phase */ Chris@69: psEncC->sLP.mode = 0; Chris@69: } Chris@69: if( psEncC->allow_bandwidth_switch || encControl->opusCanSwitch ) { Chris@69: /* Check if we should switch down */ Chris@69: if( silk_SMULBB( orig_kHz, 1000 ) > psEncC->desiredInternal_fs_Hz ) Chris@69: { Chris@69: /* Switch down */ Chris@69: if( psEncC->sLP.mode == 0 ) { Chris@69: /* New transition */ Chris@69: psEncC->sLP.transition_frame_no = TRANSITION_FRAMES; Chris@69: Chris@69: /* Reset transition filter state */ Chris@69: silk_memset( psEncC->sLP.In_LP_State, 0, sizeof( psEncC->sLP.In_LP_State ) ); Chris@69: } Chris@69: if( encControl->opusCanSwitch ) { Chris@69: /* Stop transition phase */ Chris@69: psEncC->sLP.mode = 0; Chris@69: Chris@69: /* Switch to a lower sample frequency */ Chris@69: fs_kHz = orig_kHz == 16 ? 12 : 8; Chris@69: } else { Chris@69: if( psEncC->sLP.transition_frame_no <= 0 ) { Chris@69: encControl->switchReady = 1; Chris@69: /* Make room for redundancy */ Chris@69: encControl->maxBits -= encControl->maxBits * 5 / ( encControl->payloadSize_ms + 5 ); Chris@69: } else { Chris@69: /* Direction: down (at double speed) */ Chris@69: psEncC->sLP.mode = -2; Chris@69: } Chris@69: } Chris@69: } Chris@69: else Chris@69: /* Check if we should switch up */ Chris@69: if( silk_SMULBB( orig_kHz, 1000 ) < psEncC->desiredInternal_fs_Hz ) Chris@69: { Chris@69: /* Switch up */ Chris@69: if( encControl->opusCanSwitch ) { Chris@69: /* Switch to a higher sample frequency */ Chris@69: fs_kHz = orig_kHz == 8 ? 12 : 16; Chris@69: Chris@69: /* New transition */ Chris@69: psEncC->sLP.transition_frame_no = 0; Chris@69: Chris@69: /* Reset transition filter state */ Chris@69: silk_memset( psEncC->sLP.In_LP_State, 0, sizeof( psEncC->sLP.In_LP_State ) ); Chris@69: Chris@69: /* Direction: up */ Chris@69: psEncC->sLP.mode = 1; Chris@69: } else { Chris@69: if( psEncC->sLP.mode == 0 ) { Chris@69: encControl->switchReady = 1; Chris@69: /* Make room for redundancy */ Chris@69: encControl->maxBits -= encControl->maxBits * 5 / ( encControl->payloadSize_ms + 5 ); Chris@69: } else { Chris@69: /* Direction: up */ Chris@69: psEncC->sLP.mode = 1; Chris@69: } Chris@69: } Chris@69: } else { Chris@69: if (psEncC->sLP.mode<0) Chris@69: psEncC->sLP.mode = 1; Chris@69: } Chris@69: } Chris@69: } Chris@69: Chris@69: return fs_kHz; Chris@69: }