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: /* NLSF stabilizer: */ Chris@69: /* */ Chris@69: /* - Moves NLSFs further apart if they are too close */ Chris@69: /* - Moves NLSFs away from borders if they are too close */ Chris@69: /* - High effort to achieve a modification with minimum */ Chris@69: /* Euclidean distance to input vector */ Chris@69: /* - Output are sorted NLSF coefficients */ Chris@69: /* */ Chris@69: Chris@69: #include "SigProc_FIX.h" Chris@69: Chris@69: /* Constant Definitions */ Chris@69: #define MAX_LOOPS 20 Chris@69: Chris@69: /* NLSF stabilizer, for a single input data vector */ Chris@69: void silk_NLSF_stabilize( Chris@69: opus_int16 *NLSF_Q15, /* I/O Unstable/stabilized normalized LSF vector in Q15 [L] */ Chris@69: const opus_int16 *NDeltaMin_Q15, /* I Min distance vector, NDeltaMin_Q15[L] must be >= 1 [L+1] */ Chris@69: const opus_int L /* I Number of NLSF parameters in the input vector */ Chris@69: ) Chris@69: { Chris@69: opus_int i, I=0, k, loops; Chris@69: opus_int16 center_freq_Q15; Chris@69: opus_int32 diff_Q15, min_diff_Q15, min_center_Q15, max_center_Q15; Chris@69: Chris@69: /* This is necessary to ensure an output within range of a opus_int16 */ Chris@69: silk_assert( NDeltaMin_Q15[L] >= 1 ); Chris@69: Chris@69: for( loops = 0; loops < MAX_LOOPS; loops++ ) { Chris@69: /**************************/ Chris@69: /* Find smallest distance */ Chris@69: /**************************/ Chris@69: /* First element */ Chris@69: min_diff_Q15 = NLSF_Q15[0] - NDeltaMin_Q15[0]; Chris@69: I = 0; Chris@69: /* Middle elements */ Chris@69: for( i = 1; i <= L-1; i++ ) { Chris@69: diff_Q15 = NLSF_Q15[i] - ( NLSF_Q15[i-1] + NDeltaMin_Q15[i] ); Chris@69: if( diff_Q15 < min_diff_Q15 ) { Chris@69: min_diff_Q15 = diff_Q15; Chris@69: I = i; Chris@69: } Chris@69: } Chris@69: /* Last element */ Chris@69: diff_Q15 = ( 1 << 15 ) - ( NLSF_Q15[L-1] + NDeltaMin_Q15[L] ); Chris@69: if( diff_Q15 < min_diff_Q15 ) { Chris@69: min_diff_Q15 = diff_Q15; Chris@69: I = L; Chris@69: } Chris@69: Chris@69: /***************************************************/ Chris@69: /* Now check if the smallest distance non-negative */ Chris@69: /***************************************************/ Chris@69: if( min_diff_Q15 >= 0 ) { Chris@69: return; Chris@69: } Chris@69: Chris@69: if( I == 0 ) { Chris@69: /* Move away from lower limit */ Chris@69: NLSF_Q15[0] = NDeltaMin_Q15[0]; Chris@69: Chris@69: } else if( I == L) { Chris@69: /* Move away from higher limit */ Chris@69: NLSF_Q15[L-1] = ( 1 << 15 ) - NDeltaMin_Q15[L]; Chris@69: Chris@69: } else { Chris@69: /* Find the lower extreme for the location of the current center frequency */ Chris@69: min_center_Q15 = 0; Chris@69: for( k = 0; k < I; k++ ) { Chris@69: min_center_Q15 += NDeltaMin_Q15[k]; Chris@69: } Chris@69: min_center_Q15 += silk_RSHIFT( NDeltaMin_Q15[I], 1 ); Chris@69: Chris@69: /* Find the upper extreme for the location of the current center frequency */ Chris@69: max_center_Q15 = 1 << 15; Chris@69: for( k = L; k > I; k-- ) { Chris@69: max_center_Q15 -= NDeltaMin_Q15[k]; Chris@69: } Chris@69: max_center_Q15 -= silk_RSHIFT( NDeltaMin_Q15[I], 1 ); Chris@69: Chris@69: /* Move apart, sorted by value, keeping the same center frequency */ Chris@69: center_freq_Q15 = (opus_int16)silk_LIMIT_32( silk_RSHIFT_ROUND( (opus_int32)NLSF_Q15[I-1] + (opus_int32)NLSF_Q15[I], 1 ), Chris@69: min_center_Q15, max_center_Q15 ); Chris@69: NLSF_Q15[I-1] = center_freq_Q15 - silk_RSHIFT( NDeltaMin_Q15[I], 1 ); Chris@69: NLSF_Q15[I] = NLSF_Q15[I-1] + NDeltaMin_Q15[I]; Chris@69: } Chris@69: } Chris@69: Chris@69: /* Safe and simple fall back method, which is less ideal than the above */ Chris@69: if( loops == MAX_LOOPS ) Chris@69: { Chris@69: /* Insertion sort (fast for already almost sorted arrays): */ Chris@69: /* Best case: O(n) for an already sorted array */ Chris@69: /* Worst case: O(n^2) for an inversely sorted array */ Chris@69: silk_insertion_sort_increasing_all_values_int16( &NLSF_Q15[0], L ); Chris@69: Chris@69: /* First NLSF should be no less than NDeltaMin[0] */ Chris@69: NLSF_Q15[0] = silk_max_int( NLSF_Q15[0], NDeltaMin_Q15[0] ); Chris@69: Chris@69: /* Keep delta_min distance between the NLSFs */ Chris@69: for( i = 1; i < L; i++ ) Chris@69: NLSF_Q15[i] = silk_max_int( NLSF_Q15[i], silk_ADD_SAT16( NLSF_Q15[i-1], NDeltaMin_Q15[i] ) ); Chris@69: Chris@69: /* Last NLSF should be no higher than 1 - NDeltaMin[L] */ Chris@69: NLSF_Q15[L-1] = silk_min_int( NLSF_Q15[L-1], (1<<15) - NDeltaMin_Q15[L] ); Chris@69: Chris@69: /* Keep NDeltaMin distance between the NLSFs */ Chris@69: for( i = L-2; i >= 0; i-- ) Chris@69: NLSF_Q15[i] = silk_min_int( NLSF_Q15[i], NLSF_Q15[i+1] - NDeltaMin_Q15[i+1] ); Chris@69: } Chris@69: }