annotate src/opus-1.3/silk/control_audio_bandwidth.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
Chris@69 32 #include "main.h"
Chris@69 33 #include "tuning_parameters.h"
Chris@69 34
Chris@69 35 /* Control internal sampling rate */
Chris@69 36 opus_int silk_control_audio_bandwidth(
Chris@69 37 silk_encoder_state *psEncC, /* I/O Pointer to Silk encoder state */
Chris@69 38 silk_EncControlStruct *encControl /* I Control structure */
Chris@69 39 )
Chris@69 40 {
Chris@69 41 opus_int fs_kHz;
Chris@69 42 opus_int orig_kHz;
Chris@69 43 opus_int32 fs_Hz;
Chris@69 44
Chris@69 45 orig_kHz = psEncC->fs_kHz;
Chris@69 46 /* Handle a bandwidth-switching reset where we need to be aware what the last sampling rate was. */
Chris@69 47 if( orig_kHz == 0 ) {
Chris@69 48 orig_kHz = psEncC->sLP.saved_fs_kHz;
Chris@69 49 }
Chris@69 50 fs_kHz = orig_kHz;
Chris@69 51 fs_Hz = silk_SMULBB( fs_kHz, 1000 );
Chris@69 52 if( fs_Hz == 0 ) {
Chris@69 53 /* Encoder has just been initialized */
Chris@69 54 fs_Hz = silk_min( psEncC->desiredInternal_fs_Hz, psEncC->API_fs_Hz );
Chris@69 55 fs_kHz = silk_DIV32_16( fs_Hz, 1000 );
Chris@69 56 } else if( fs_Hz > psEncC->API_fs_Hz || fs_Hz > psEncC->maxInternal_fs_Hz || fs_Hz < psEncC->minInternal_fs_Hz ) {
Chris@69 57 /* Make sure internal rate is not higher than external rate or maximum allowed, or lower than minimum allowed */
Chris@69 58 fs_Hz = psEncC->API_fs_Hz;
Chris@69 59 fs_Hz = silk_min( fs_Hz, psEncC->maxInternal_fs_Hz );
Chris@69 60 fs_Hz = silk_max( fs_Hz, psEncC->minInternal_fs_Hz );
Chris@69 61 fs_kHz = silk_DIV32_16( fs_Hz, 1000 );
Chris@69 62 } else {
Chris@69 63 /* State machine for the internal sampling rate switching */
Chris@69 64 if( psEncC->sLP.transition_frame_no >= TRANSITION_FRAMES ) {
Chris@69 65 /* Stop transition phase */
Chris@69 66 psEncC->sLP.mode = 0;
Chris@69 67 }
Chris@69 68 if( psEncC->allow_bandwidth_switch || encControl->opusCanSwitch ) {
Chris@69 69 /* Check if we should switch down */
Chris@69 70 if( silk_SMULBB( orig_kHz, 1000 ) > psEncC->desiredInternal_fs_Hz )
Chris@69 71 {
Chris@69 72 /* Switch down */
Chris@69 73 if( psEncC->sLP.mode == 0 ) {
Chris@69 74 /* New transition */
Chris@69 75 psEncC->sLP.transition_frame_no = TRANSITION_FRAMES;
Chris@69 76
Chris@69 77 /* Reset transition filter state */
Chris@69 78 silk_memset( psEncC->sLP.In_LP_State, 0, sizeof( psEncC->sLP.In_LP_State ) );
Chris@69 79 }
Chris@69 80 if( encControl->opusCanSwitch ) {
Chris@69 81 /* Stop transition phase */
Chris@69 82 psEncC->sLP.mode = 0;
Chris@69 83
Chris@69 84 /* Switch to a lower sample frequency */
Chris@69 85 fs_kHz = orig_kHz == 16 ? 12 : 8;
Chris@69 86 } else {
Chris@69 87 if( psEncC->sLP.transition_frame_no <= 0 ) {
Chris@69 88 encControl->switchReady = 1;
Chris@69 89 /* Make room for redundancy */
Chris@69 90 encControl->maxBits -= encControl->maxBits * 5 / ( encControl->payloadSize_ms + 5 );
Chris@69 91 } else {
Chris@69 92 /* Direction: down (at double speed) */
Chris@69 93 psEncC->sLP.mode = -2;
Chris@69 94 }
Chris@69 95 }
Chris@69 96 }
Chris@69 97 else
Chris@69 98 /* Check if we should switch up */
Chris@69 99 if( silk_SMULBB( orig_kHz, 1000 ) < psEncC->desiredInternal_fs_Hz )
Chris@69 100 {
Chris@69 101 /* Switch up */
Chris@69 102 if( encControl->opusCanSwitch ) {
Chris@69 103 /* Switch to a higher sample frequency */
Chris@69 104 fs_kHz = orig_kHz == 8 ? 12 : 16;
Chris@69 105
Chris@69 106 /* New transition */
Chris@69 107 psEncC->sLP.transition_frame_no = 0;
Chris@69 108
Chris@69 109 /* Reset transition filter state */
Chris@69 110 silk_memset( psEncC->sLP.In_LP_State, 0, sizeof( psEncC->sLP.In_LP_State ) );
Chris@69 111
Chris@69 112 /* Direction: up */
Chris@69 113 psEncC->sLP.mode = 1;
Chris@69 114 } else {
Chris@69 115 if( psEncC->sLP.mode == 0 ) {
Chris@69 116 encControl->switchReady = 1;
Chris@69 117 /* Make room for redundancy */
Chris@69 118 encControl->maxBits -= encControl->maxBits * 5 / ( encControl->payloadSize_ms + 5 );
Chris@69 119 } else {
Chris@69 120 /* Direction: up */
Chris@69 121 psEncC->sLP.mode = 1;
Chris@69 122 }
Chris@69 123 }
Chris@69 124 } else {
Chris@69 125 if (psEncC->sLP.mode<0)
Chris@69 126 psEncC->sLP.mode = 1;
Chris@69 127 }
Chris@69 128 }
Chris@69 129 }
Chris@69 130
Chris@69 131 return fs_kHz;
Chris@69 132 }