Gregory Maxwell | ae23114 | 2011-07-30 08:18:48 -0400 | [diff] [blame] | 1 | /*********************************************************************** |
| 2 | Copyright (c) 2006-2011, Skype Limited. All rights reserved. |
| 3 | Redistribution and use in source and binary forms, with or without |
Jean-Marc Valin | ae00e60 | 2012-04-20 16:31:04 -0400 | [diff] [blame] | 4 | modification, are permitted provided that the following conditions |
| 5 | are met: |
Gregory Maxwell | ae23114 | 2011-07-30 08:18:48 -0400 | [diff] [blame] | 6 | - Redistributions of source code must retain the above copyright notice, |
| 7 | this list of conditions and the following disclaimer. |
| 8 | - Redistributions in binary form must reproduce the above copyright |
| 9 | notice, this list of conditions and the following disclaimer in the |
| 10 | documentation and/or other materials provided with the distribution. |
Ralph Giles | f2446c2 | 2013-09-16 14:40:04 -0700 | [diff] [blame] | 11 | - Neither the name of Internet Society, IETF or IETF Trust, nor the |
Jean-Marc Valin | ae00e60 | 2012-04-20 16:31:04 -0400 | [diff] [blame] | 12 | names of specific contributors, may be used to endorse or promote |
| 13 | products derived from this software without specific prior written |
| 14 | permission. |
Timothy B. Terriberry | 80ad383 | 2013-05-19 18:00:39 -0700 | [diff] [blame] | 15 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
Jean-Marc Valin | ae00e60 | 2012-04-20 16:31:04 -0400 | [diff] [blame] | 16 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 17 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 18 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 19 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 20 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 21 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 22 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 23 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 24 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 25 | POSSIBILITY OF SUCH DAMAGE. |
Gregory Maxwell | ae23114 | 2011-07-30 08:18:48 -0400 | [diff] [blame] | 26 | ***********************************************************************/ |
| 27 | |
Jean-Marc Valin | 5a48412 | 2011-08-15 10:49:53 -0400 | [diff] [blame] | 28 | #ifdef HAVE_CONFIG_H |
| 29 | #include "config.h" |
| 30 | #endif |
| 31 | |
Gregory Maxwell | ae23114 | 2011-07-30 08:18:48 -0400 | [diff] [blame] | 32 | /* |
Gregory Maxwell | ae23114 | 2011-07-30 08:18:48 -0400 | [diff] [blame] | 33 | Elliptic/Cauer filters designed with 0.1 dB passband ripple, |
Koen Vos | acc7a6c | 2011-10-28 19:44:26 -0400 | [diff] [blame] | 34 | 80 dB minimum stopband attenuation, and |
| 35 | [0.95 : 0.15 : 0.35] normalized cut off frequencies. |
Gregory Maxwell | ae23114 | 2011-07-30 08:18:48 -0400 | [diff] [blame] | 36 | */ |
Koen Vos | acc7a6c | 2011-10-28 19:44:26 -0400 | [diff] [blame] | 37 | |
Jean-Marc Valin | 1c2f563 | 2011-09-16 01:16:53 -0700 | [diff] [blame] | 38 | #include "main.h" |
Gregory Maxwell | ae23114 | 2011-07-30 08:18:48 -0400 | [diff] [blame] | 39 | |
| 40 | /* Helper function, interpolates the filter taps */ |
Jean-Marc Valin | 4dc0b39 | 2011-08-15 11:24:37 -0400 | [diff] [blame] | 41 | static inline void silk_LP_interpolate_filter_taps( |
Gregory Maxwell | ae23114 | 2011-07-30 08:18:48 -0400 | [diff] [blame] | 42 | opus_int32 B_Q28[ TRANSITION_NB ], |
| 43 | opus_int32 A_Q28[ TRANSITION_NA ], |
| 44 | const opus_int ind, |
| 45 | const opus_int32 fac_Q16 |
| 46 | ) |
| 47 | { |
| 48 | opus_int nb, na; |
| 49 | |
| 50 | if( ind < TRANSITION_INT_NUM - 1 ) { |
| 51 | if( fac_Q16 > 0 ) { |
| 52 | if( fac_Q16 < 32768 ) { /* fac_Q16 is in range of a 16-bit int */ |
| 53 | /* Piece-wise linear interpolation of B and A */ |
| 54 | for( nb = 0; nb < TRANSITION_NB; nb++ ) { |
Jean-Marc Valin | fb3a437 | 2011-09-16 00:58:26 -0700 | [diff] [blame] | 55 | B_Q28[ nb ] = silk_SMLAWB( |
Gregory Maxwell | ae23114 | 2011-07-30 08:18:48 -0400 | [diff] [blame] | 56 | silk_Transition_LP_B_Q28[ ind ][ nb ], |
| 57 | silk_Transition_LP_B_Q28[ ind + 1 ][ nb ] - |
| 58 | silk_Transition_LP_B_Q28[ ind ][ nb ], |
| 59 | fac_Q16 ); |
| 60 | } |
| 61 | for( na = 0; na < TRANSITION_NA; na++ ) { |
Jean-Marc Valin | fb3a437 | 2011-09-16 00:58:26 -0700 | [diff] [blame] | 62 | A_Q28[ na ] = silk_SMLAWB( |
Gregory Maxwell | ae23114 | 2011-07-30 08:18:48 -0400 | [diff] [blame] | 63 | silk_Transition_LP_A_Q28[ ind ][ na ], |
| 64 | silk_Transition_LP_A_Q28[ ind + 1 ][ na ] - |
| 65 | silk_Transition_LP_A_Q28[ ind ][ na ], |
| 66 | fac_Q16 ); |
| 67 | } |
| 68 | } else { /* ( fac_Q16 - ( 1 << 16 ) ) is in range of a 16-bit int */ |
Jean-Marc Valin | fb3a437 | 2011-09-16 00:58:26 -0700 | [diff] [blame] | 69 | silk_assert( fac_Q16 - ( 1 << 16 ) == silk_SAT16( fac_Q16 - ( 1 << 16 ) ) ); |
Gregory Maxwell | ae23114 | 2011-07-30 08:18:48 -0400 | [diff] [blame] | 70 | /* Piece-wise linear interpolation of B and A */ |
| 71 | for( nb = 0; nb < TRANSITION_NB; nb++ ) { |
Jean-Marc Valin | fb3a437 | 2011-09-16 00:58:26 -0700 | [diff] [blame] | 72 | B_Q28[ nb ] = silk_SMLAWB( |
Gregory Maxwell | ae23114 | 2011-07-30 08:18:48 -0400 | [diff] [blame] | 73 | silk_Transition_LP_B_Q28[ ind + 1 ][ nb ], |
| 74 | silk_Transition_LP_B_Q28[ ind + 1 ][ nb ] - |
| 75 | silk_Transition_LP_B_Q28[ ind ][ nb ], |
Jean-Marc Valin | 905197d | 2012-03-08 14:09:09 -0500 | [diff] [blame] | 76 | fac_Q16 - ( (opus_int32)1 << 16 ) ); |
Gregory Maxwell | ae23114 | 2011-07-30 08:18:48 -0400 | [diff] [blame] | 77 | } |
| 78 | for( na = 0; na < TRANSITION_NA; na++ ) { |
Jean-Marc Valin | fb3a437 | 2011-09-16 00:58:26 -0700 | [diff] [blame] | 79 | A_Q28[ na ] = silk_SMLAWB( |
Gregory Maxwell | ae23114 | 2011-07-30 08:18:48 -0400 | [diff] [blame] | 80 | silk_Transition_LP_A_Q28[ ind + 1 ][ na ], |
| 81 | silk_Transition_LP_A_Q28[ ind + 1 ][ na ] - |
| 82 | silk_Transition_LP_A_Q28[ ind ][ na ], |
Jean-Marc Valin | 905197d | 2012-03-08 14:09:09 -0500 | [diff] [blame] | 83 | fac_Q16 - ( (opus_int32)1 << 16 ) ); |
Gregory Maxwell | ae23114 | 2011-07-30 08:18:48 -0400 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | } else { |
Jean-Marc Valin | fb3a437 | 2011-09-16 00:58:26 -0700 | [diff] [blame] | 87 | silk_memcpy( B_Q28, silk_Transition_LP_B_Q28[ ind ], TRANSITION_NB * sizeof( opus_int32 ) ); |
| 88 | silk_memcpy( A_Q28, silk_Transition_LP_A_Q28[ ind ], TRANSITION_NA * sizeof( opus_int32 ) ); |
Gregory Maxwell | ae23114 | 2011-07-30 08:18:48 -0400 | [diff] [blame] | 89 | } |
| 90 | } else { |
Jean-Marc Valin | fb3a437 | 2011-09-16 00:58:26 -0700 | [diff] [blame] | 91 | silk_memcpy( B_Q28, silk_Transition_LP_B_Q28[ TRANSITION_INT_NUM - 1 ], TRANSITION_NB * sizeof( opus_int32 ) ); |
| 92 | silk_memcpy( A_Q28, silk_Transition_LP_A_Q28[ TRANSITION_INT_NUM - 1 ], TRANSITION_NA * sizeof( opus_int32 ) ); |
Gregory Maxwell | ae23114 | 2011-07-30 08:18:48 -0400 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | |
| 96 | /* Low-pass filter with variable cutoff frequency based on */ |
| 97 | /* piece-wise linear interpolation between elliptic filters */ |
| 98 | /* Start by setting psEncC->mode <> 0; */ |
| 99 | /* Deactivate by setting psEncC->mode = 0; */ |
| 100 | void silk_LP_variable_cutoff( |
Koen Vos | acc7a6c | 2011-10-28 19:44:26 -0400 | [diff] [blame] | 101 | silk_LP_state *psLP, /* I/O LP filter state */ |
| 102 | opus_int16 *frame, /* I/O Low-pass filtered output signal */ |
| 103 | const opus_int frame_length /* I Frame length */ |
Gregory Maxwell | ae23114 | 2011-07-30 08:18:48 -0400 | [diff] [blame] | 104 | ) |
| 105 | { |
| 106 | opus_int32 B_Q28[ TRANSITION_NB ], A_Q28[ TRANSITION_NA ], fac_Q16 = 0; |
| 107 | opus_int ind = 0; |
| 108 | |
Jean-Marc Valin | fb3a437 | 2011-09-16 00:58:26 -0700 | [diff] [blame] | 109 | silk_assert( psLP->transition_frame_no >= 0 && psLP->transition_frame_no <= TRANSITION_FRAMES ); |
Gregory Maxwell | ae23114 | 2011-07-30 08:18:48 -0400 | [diff] [blame] | 110 | |
| 111 | /* Run filter if needed */ |
| 112 | if( psLP->mode != 0 ) { |
| 113 | /* Calculate index and interpolation factor for interpolation */ |
| 114 | #if( TRANSITION_INT_STEPS == 64 ) |
Jean-Marc Valin | fb3a437 | 2011-09-16 00:58:26 -0700 | [diff] [blame] | 115 | fac_Q16 = silk_LSHIFT( TRANSITION_FRAMES - psLP->transition_frame_no, 16 - 6 ); |
Gregory Maxwell | ae23114 | 2011-07-30 08:18:48 -0400 | [diff] [blame] | 116 | #else |
Jean-Marc Valin | fb3a437 | 2011-09-16 00:58:26 -0700 | [diff] [blame] | 117 | fac_Q16 = silk_DIV32_16( silk_LSHIFT( TRANSITION_FRAMES - psLP->transition_frame_no, 16 ), TRANSITION_FRAMES ); |
Gregory Maxwell | ae23114 | 2011-07-30 08:18:48 -0400 | [diff] [blame] | 118 | #endif |
Jean-Marc Valin | fb3a437 | 2011-09-16 00:58:26 -0700 | [diff] [blame] | 119 | ind = silk_RSHIFT( fac_Q16, 16 ); |
| 120 | fac_Q16 -= silk_LSHIFT( ind, 16 ); |
Gregory Maxwell | ae23114 | 2011-07-30 08:18:48 -0400 | [diff] [blame] | 121 | |
Jean-Marc Valin | fb3a437 | 2011-09-16 00:58:26 -0700 | [diff] [blame] | 122 | silk_assert( ind >= 0 ); |
| 123 | silk_assert( ind < TRANSITION_INT_NUM ); |
Gregory Maxwell | ae23114 | 2011-07-30 08:18:48 -0400 | [diff] [blame] | 124 | |
| 125 | /* Interpolate filter coefficients */ |
| 126 | silk_LP_interpolate_filter_taps( B_Q28, A_Q28, ind, fac_Q16 ); |
| 127 | |
| 128 | /* Update transition frame number for next frame */ |
Jean-Marc Valin | fb3a437 | 2011-09-16 00:58:26 -0700 | [diff] [blame] | 129 | psLP->transition_frame_no = silk_LIMIT( psLP->transition_frame_no + psLP->mode, 0, TRANSITION_FRAMES ); |
Gregory Maxwell | ae23114 | 2011-07-30 08:18:48 -0400 | [diff] [blame] | 130 | |
| 131 | /* ARMA low-pass filtering */ |
Jean-Marc Valin | fb3a437 | 2011-09-16 00:58:26 -0700 | [diff] [blame] | 132 | silk_assert( TRANSITION_NB == 3 && TRANSITION_NA == 2 ); |
Jean-Marc Valin | 957f7f1 | 2011-09-01 18:02:43 -0400 | [diff] [blame] | 133 | silk_biquad_alt( frame, B_Q28, A_Q28, psLP->In_LP_State, frame, frame_length, 1); |
Gregory Maxwell | ae23114 | 2011-07-30 08:18:48 -0400 | [diff] [blame] | 134 | } |
| 135 | } |