media/libopus/silk/NLSF_stabilize.c

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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

mercurial