annotate src/opus-1.3/silk/NLSF_stabilize.c @ 169:223a55898ab9 tip default

Add null config files
author Chris Cannam <cannam@all-day-breakfast.com>
date Mon, 02 Mar 2020 14:03:47 +0000
parents 4664ac0c1032
children
rev   line source
cannam@154 1 /***********************************************************************
cannam@154 2 Copyright (c) 2006-2011, Skype Limited. All rights reserved.
cannam@154 3 Redistribution and use in source and binary forms, with or without
cannam@154 4 modification, are permitted provided that the following conditions
cannam@154 5 are met:
cannam@154 6 - Redistributions of source code must retain the above copyright notice,
cannam@154 7 this list of conditions and the following disclaimer.
cannam@154 8 - Redistributions in binary form must reproduce the above copyright
cannam@154 9 notice, this list of conditions and the following disclaimer in the
cannam@154 10 documentation and/or other materials provided with the distribution.
cannam@154 11 - Neither the name of Internet Society, IETF or IETF Trust, nor the
cannam@154 12 names of specific contributors, may be used to endorse or promote
cannam@154 13 products derived from this software without specific prior written
cannam@154 14 permission.
cannam@154 15 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
cannam@154 16 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
cannam@154 17 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
cannam@154 18 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
cannam@154 19 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
cannam@154 20 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
cannam@154 21 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
cannam@154 22 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
cannam@154 23 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
cannam@154 24 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
cannam@154 25 POSSIBILITY OF SUCH DAMAGE.
cannam@154 26 ***********************************************************************/
cannam@154 27
cannam@154 28 #ifdef HAVE_CONFIG_H
cannam@154 29 #include "config.h"
cannam@154 30 #endif
cannam@154 31
cannam@154 32 /* NLSF stabilizer: */
cannam@154 33 /* */
cannam@154 34 /* - Moves NLSFs further apart if they are too close */
cannam@154 35 /* - Moves NLSFs away from borders if they are too close */
cannam@154 36 /* - High effort to achieve a modification with minimum */
cannam@154 37 /* Euclidean distance to input vector */
cannam@154 38 /* - Output are sorted NLSF coefficients */
cannam@154 39 /* */
cannam@154 40
cannam@154 41 #include "SigProc_FIX.h"
cannam@154 42
cannam@154 43 /* Constant Definitions */
cannam@154 44 #define MAX_LOOPS 20
cannam@154 45
cannam@154 46 /* NLSF stabilizer, for a single input data vector */
cannam@154 47 void silk_NLSF_stabilize(
cannam@154 48 opus_int16 *NLSF_Q15, /* I/O Unstable/stabilized normalized LSF vector in Q15 [L] */
cannam@154 49 const opus_int16 *NDeltaMin_Q15, /* I Min distance vector, NDeltaMin_Q15[L] must be >= 1 [L+1] */
cannam@154 50 const opus_int L /* I Number of NLSF parameters in the input vector */
cannam@154 51 )
cannam@154 52 {
cannam@154 53 opus_int i, I=0, k, loops;
cannam@154 54 opus_int16 center_freq_Q15;
cannam@154 55 opus_int32 diff_Q15, min_diff_Q15, min_center_Q15, max_center_Q15;
cannam@154 56
cannam@154 57 /* This is necessary to ensure an output within range of a opus_int16 */
cannam@154 58 silk_assert( NDeltaMin_Q15[L] >= 1 );
cannam@154 59
cannam@154 60 for( loops = 0; loops < MAX_LOOPS; loops++ ) {
cannam@154 61 /**************************/
cannam@154 62 /* Find smallest distance */
cannam@154 63 /**************************/
cannam@154 64 /* First element */
cannam@154 65 min_diff_Q15 = NLSF_Q15[0] - NDeltaMin_Q15[0];
cannam@154 66 I = 0;
cannam@154 67 /* Middle elements */
cannam@154 68 for( i = 1; i <= L-1; i++ ) {
cannam@154 69 diff_Q15 = NLSF_Q15[i] - ( NLSF_Q15[i-1] + NDeltaMin_Q15[i] );
cannam@154 70 if( diff_Q15 < min_diff_Q15 ) {
cannam@154 71 min_diff_Q15 = diff_Q15;
cannam@154 72 I = i;
cannam@154 73 }
cannam@154 74 }
cannam@154 75 /* Last element */
cannam@154 76 diff_Q15 = ( 1 << 15 ) - ( NLSF_Q15[L-1] + NDeltaMin_Q15[L] );
cannam@154 77 if( diff_Q15 < min_diff_Q15 ) {
cannam@154 78 min_diff_Q15 = diff_Q15;
cannam@154 79 I = L;
cannam@154 80 }
cannam@154 81
cannam@154 82 /***************************************************/
cannam@154 83 /* Now check if the smallest distance non-negative */
cannam@154 84 /***************************************************/
cannam@154 85 if( min_diff_Q15 >= 0 ) {
cannam@154 86 return;
cannam@154 87 }
cannam@154 88
cannam@154 89 if( I == 0 ) {
cannam@154 90 /* Move away from lower limit */
cannam@154 91 NLSF_Q15[0] = NDeltaMin_Q15[0];
cannam@154 92
cannam@154 93 } else if( I == L) {
cannam@154 94 /* Move away from higher limit */
cannam@154 95 NLSF_Q15[L-1] = ( 1 << 15 ) - NDeltaMin_Q15[L];
cannam@154 96
cannam@154 97 } else {
cannam@154 98 /* Find the lower extreme for the location of the current center frequency */
cannam@154 99 min_center_Q15 = 0;
cannam@154 100 for( k = 0; k < I; k++ ) {
cannam@154 101 min_center_Q15 += NDeltaMin_Q15[k];
cannam@154 102 }
cannam@154 103 min_center_Q15 += silk_RSHIFT( NDeltaMin_Q15[I], 1 );
cannam@154 104
cannam@154 105 /* Find the upper extreme for the location of the current center frequency */
cannam@154 106 max_center_Q15 = 1 << 15;
cannam@154 107 for( k = L; k > I; k-- ) {
cannam@154 108 max_center_Q15 -= NDeltaMin_Q15[k];
cannam@154 109 }
cannam@154 110 max_center_Q15 -= silk_RSHIFT( NDeltaMin_Q15[I], 1 );
cannam@154 111
cannam@154 112 /* Move apart, sorted by value, keeping the same center frequency */
cannam@154 113 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 114 min_center_Q15, max_center_Q15 );
cannam@154 115 NLSF_Q15[I-1] = center_freq_Q15 - silk_RSHIFT( NDeltaMin_Q15[I], 1 );
cannam@154 116 NLSF_Q15[I] = NLSF_Q15[I-1] + NDeltaMin_Q15[I];
cannam@154 117 }
cannam@154 118 }
cannam@154 119
cannam@154 120 /* Safe and simple fall back method, which is less ideal than the above */
cannam@154 121 if( loops == MAX_LOOPS )
cannam@154 122 {
cannam@154 123 /* Insertion sort (fast for already almost sorted arrays): */
cannam@154 124 /* Best case: O(n) for an already sorted array */
cannam@154 125 /* Worst case: O(n^2) for an inversely sorted array */
cannam@154 126 silk_insertion_sort_increasing_all_values_int16( &NLSF_Q15[0], L );
cannam@154 127
cannam@154 128 /* First NLSF should be no less than NDeltaMin[0] */
cannam@154 129 NLSF_Q15[0] = silk_max_int( NLSF_Q15[0], NDeltaMin_Q15[0] );
cannam@154 130
cannam@154 131 /* Keep delta_min distance between the NLSFs */
cannam@154 132 for( i = 1; i < L; i++ )
cannam@154 133 NLSF_Q15[i] = silk_max_int( NLSF_Q15[i], silk_ADD_SAT16( NLSF_Q15[i-1], NDeltaMin_Q15[i] ) );
cannam@154 134
cannam@154 135 /* Last NLSF should be no higher than 1 - NDeltaMin[L] */
cannam@154 136 NLSF_Q15[L-1] = silk_min_int( NLSF_Q15[L-1], (1<<15) - NDeltaMin_Q15[L] );
cannam@154 137
cannam@154 138 /* Keep NDeltaMin distance between the NLSFs */
cannam@154 139 for( i = L-2; i >= 0; i-- )
cannam@154 140 NLSF_Q15[i] = silk_min_int( NLSF_Q15[i], NLSF_Q15[i+1] - NDeltaMin_Q15[i+1] );
cannam@154 141 }
cannam@154 142 }