cannam@154: /*********************************************************************** cannam@154: Copyright (c) 2006-2011, Skype Limited. All rights reserved. cannam@154: Redistribution and use in source and binary forms, with or without cannam@154: modification, are permitted provided that the following conditions cannam@154: are met: cannam@154: - Redistributions of source code must retain the above copyright notice, cannam@154: this list of conditions and the following disclaimer. cannam@154: - Redistributions in binary form must reproduce the above copyright cannam@154: notice, this list of conditions and the following disclaimer in the cannam@154: documentation and/or other materials provided with the distribution. cannam@154: - Neither the name of Internet Society, IETF or IETF Trust, nor the cannam@154: names of specific contributors, may be used to endorse or promote cannam@154: products derived from this software without specific prior written cannam@154: permission. cannam@154: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" cannam@154: AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE cannam@154: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE cannam@154: ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE cannam@154: LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR cannam@154: CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF cannam@154: SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS cannam@154: INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN cannam@154: CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) cannam@154: ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE cannam@154: POSSIBILITY OF SUCH DAMAGE. cannam@154: ***********************************************************************/ cannam@154: cannam@154: #ifdef HAVE_CONFIG_H cannam@154: #include "config.h" cannam@154: #endif cannam@154: cannam@154: /* NLSF stabilizer: */ cannam@154: /* */ cannam@154: /* - Moves NLSFs further apart if they are too close */ cannam@154: /* - Moves NLSFs away from borders if they are too close */ cannam@154: /* - High effort to achieve a modification with minimum */ cannam@154: /* Euclidean distance to input vector */ cannam@154: /* - Output are sorted NLSF coefficients */ cannam@154: /* */ cannam@154: cannam@154: #include "SigProc_FIX.h" cannam@154: cannam@154: /* Constant Definitions */ cannam@154: #define MAX_LOOPS 20 cannam@154: cannam@154: /* NLSF stabilizer, for a single input data vector */ cannam@154: void silk_NLSF_stabilize( cannam@154: opus_int16 *NLSF_Q15, /* I/O Unstable/stabilized normalized LSF vector in Q15 [L] */ cannam@154: const opus_int16 *NDeltaMin_Q15, /* I Min distance vector, NDeltaMin_Q15[L] must be >= 1 [L+1] */ cannam@154: const opus_int L /* I Number of NLSF parameters in the input vector */ cannam@154: ) cannam@154: { cannam@154: opus_int i, I=0, k, loops; cannam@154: opus_int16 center_freq_Q15; cannam@154: opus_int32 diff_Q15, min_diff_Q15, min_center_Q15, max_center_Q15; cannam@154: cannam@154: /* This is necessary to ensure an output within range of a opus_int16 */ cannam@154: silk_assert( NDeltaMin_Q15[L] >= 1 ); cannam@154: cannam@154: for( loops = 0; loops < MAX_LOOPS; loops++ ) { cannam@154: /**************************/ cannam@154: /* Find smallest distance */ cannam@154: /**************************/ cannam@154: /* First element */ cannam@154: min_diff_Q15 = NLSF_Q15[0] - NDeltaMin_Q15[0]; cannam@154: I = 0; cannam@154: /* Middle elements */ cannam@154: for( i = 1; i <= L-1; i++ ) { cannam@154: diff_Q15 = NLSF_Q15[i] - ( NLSF_Q15[i-1] + NDeltaMin_Q15[i] ); cannam@154: if( diff_Q15 < min_diff_Q15 ) { cannam@154: min_diff_Q15 = diff_Q15; cannam@154: I = i; cannam@154: } cannam@154: } cannam@154: /* Last element */ cannam@154: diff_Q15 = ( 1 << 15 ) - ( NLSF_Q15[L-1] + NDeltaMin_Q15[L] ); cannam@154: if( diff_Q15 < min_diff_Q15 ) { cannam@154: min_diff_Q15 = diff_Q15; cannam@154: I = L; cannam@154: } cannam@154: cannam@154: /***************************************************/ cannam@154: /* Now check if the smallest distance non-negative */ cannam@154: /***************************************************/ cannam@154: if( min_diff_Q15 >= 0 ) { cannam@154: return; cannam@154: } cannam@154: cannam@154: if( I == 0 ) { cannam@154: /* Move away from lower limit */ cannam@154: NLSF_Q15[0] = NDeltaMin_Q15[0]; cannam@154: cannam@154: } else if( I == L) { cannam@154: /* Move away from higher limit */ cannam@154: NLSF_Q15[L-1] = ( 1 << 15 ) - NDeltaMin_Q15[L]; cannam@154: cannam@154: } else { cannam@154: /* Find the lower extreme for the location of the current center frequency */ cannam@154: min_center_Q15 = 0; cannam@154: for( k = 0; k < I; k++ ) { cannam@154: min_center_Q15 += NDeltaMin_Q15[k]; cannam@154: } cannam@154: min_center_Q15 += silk_RSHIFT( NDeltaMin_Q15[I], 1 ); cannam@154: cannam@154: /* Find the upper extreme for the location of the current center frequency */ cannam@154: max_center_Q15 = 1 << 15; cannam@154: for( k = L; k > I; k-- ) { cannam@154: max_center_Q15 -= NDeltaMin_Q15[k]; cannam@154: } cannam@154: max_center_Q15 -= silk_RSHIFT( NDeltaMin_Q15[I], 1 ); cannam@154: cannam@154: /* Move apart, sorted by value, keeping the same center frequency */ cannam@154: center_freq_Q15 = (opus_int16)silk_LIMIT_32( silk_RSHIFT_ROUND( (opus_int32)NLSF_Q15[I-1] + (opus_int32)NLSF_Q15[I], 1 ), cannam@154: min_center_Q15, max_center_Q15 ); cannam@154: NLSF_Q15[I-1] = center_freq_Q15 - silk_RSHIFT( NDeltaMin_Q15[I], 1 ); cannam@154: NLSF_Q15[I] = NLSF_Q15[I-1] + NDeltaMin_Q15[I]; cannam@154: } cannam@154: } cannam@154: cannam@154: /* Safe and simple fall back method, which is less ideal than the above */ cannam@154: if( loops == MAX_LOOPS ) cannam@154: { cannam@154: /* Insertion sort (fast for already almost sorted arrays): */ cannam@154: /* Best case: O(n) for an already sorted array */ cannam@154: /* Worst case: O(n^2) for an inversely sorted array */ cannam@154: silk_insertion_sort_increasing_all_values_int16( &NLSF_Q15[0], L ); cannam@154: cannam@154: /* First NLSF should be no less than NDeltaMin[0] */ cannam@154: NLSF_Q15[0] = silk_max_int( NLSF_Q15[0], NDeltaMin_Q15[0] ); cannam@154: cannam@154: /* Keep delta_min distance between the NLSFs */ cannam@154: for( i = 1; i < L; i++ ) cannam@154: NLSF_Q15[i] = silk_max_int( NLSF_Q15[i], silk_ADD_SAT16( NLSF_Q15[i-1], NDeltaMin_Q15[i] ) ); cannam@154: cannam@154: /* Last NLSF should be no higher than 1 - NDeltaMin[L] */ cannam@154: NLSF_Q15[L-1] = silk_min_int( NLSF_Q15[L-1], (1<<15) - NDeltaMin_Q15[L] ); cannam@154: cannam@154: /* Keep NDeltaMin distance between the NLSFs */ cannam@154: for( i = L-2; i >= 0; i-- ) cannam@154: NLSF_Q15[i] = silk_min_int( NLSF_Q15[i], NLSF_Q15[i+1] - NDeltaMin_Q15[i+1] ); cannam@154: } cannam@154: }