diff options
Diffstat (limited to 'lib/rbcodec/codecs/libopus/silk/sum_sqr_shift.c')
-rw-r--r-- | lib/rbcodec/codecs/libopus/silk/sum_sqr_shift.c | 51 |
1 files changed, 24 insertions, 27 deletions
diff --git a/lib/rbcodec/codecs/libopus/silk/sum_sqr_shift.c b/lib/rbcodec/codecs/libopus/silk/sum_sqr_shift.c index 129df191d8..4fd0c3d7d5 100644 --- a/lib/rbcodec/codecs/libopus/silk/sum_sqr_shift.c +++ b/lib/rbcodec/codecs/libopus/silk/sum_sqr_shift.c | |||
@@ -41,43 +41,40 @@ void silk_sum_sqr_shift( | |||
41 | ) | 41 | ) |
42 | { | 42 | { |
43 | opus_int i, shft; | 43 | opus_int i, shft; |
44 | opus_int32 nrg_tmp, nrg; | 44 | opus_uint32 nrg_tmp; |
45 | opus_int32 nrg; | ||
45 | 46 | ||
46 | nrg = 0; | 47 | /* Do a first run with the maximum shift we could have. */ |
47 | shft = 0; | 48 | shft = 31-silk_CLZ32(len); |
48 | len--; | 49 | /* Let's be conservative with rounding and start with nrg=len. */ |
49 | for( i = 0; i < len; i += 2 ) { | 50 | nrg = len; |
50 | nrg = silk_SMLABB_ovflw( nrg, x[ i ], x[ i ] ); | 51 | for( i = 0; i < len - 1; i += 2 ) { |
51 | nrg = silk_SMLABB_ovflw( nrg, x[ i + 1 ], x[ i + 1 ] ); | 52 | nrg_tmp = silk_SMULBB( x[ i ], x[ i ] ); |
52 | if( nrg < 0 ) { | 53 | nrg_tmp = silk_SMLABB_ovflw( nrg_tmp, x[ i + 1 ], x[ i + 1 ] ); |
53 | /* Scale down */ | 54 | nrg = (opus_int32)silk_ADD_RSHIFT_uint( nrg, nrg_tmp, shft ); |
54 | nrg = (opus_int32)silk_RSHIFT_uint( (opus_uint32)nrg, 2 ); | ||
55 | shft = 2; | ||
56 | i+=2; | ||
57 | break; | ||
58 | } | ||
59 | } | 55 | } |
60 | for( ; i < len; i += 2 ) { | 56 | if( i < len ) { |
57 | /* One sample left to process */ | ||
58 | nrg_tmp = silk_SMULBB( x[ i ], x[ i ] ); | ||
59 | nrg = (opus_int32)silk_ADD_RSHIFT_uint( nrg, nrg_tmp, shft ); | ||
60 | } | ||
61 | silk_assert( nrg >= 0 ); | ||
62 | /* Make sure the result will fit in a 32-bit signed integer with two bits | ||
63 | of headroom. */ | ||
64 | shft = silk_max_32(0, shft+3 - silk_CLZ32(nrg)); | ||
65 | nrg = 0; | ||
66 | for( i = 0 ; i < len - 1; i += 2 ) { | ||
61 | nrg_tmp = silk_SMULBB( x[ i ], x[ i ] ); | 67 | nrg_tmp = silk_SMULBB( x[ i ], x[ i ] ); |
62 | nrg_tmp = silk_SMLABB_ovflw( nrg_tmp, x[ i + 1 ], x[ i + 1 ] ); | 68 | nrg_tmp = silk_SMLABB_ovflw( nrg_tmp, x[ i + 1 ], x[ i + 1 ] ); |
63 | nrg = (opus_int32)silk_ADD_RSHIFT_uint( nrg, (opus_uint32)nrg_tmp, shft ); | 69 | nrg = (opus_int32)silk_ADD_RSHIFT_uint( nrg, nrg_tmp, shft ); |
64 | if( nrg < 0 ) { | ||
65 | /* Scale down */ | ||
66 | nrg = (opus_int32)silk_RSHIFT_uint( (opus_uint32)nrg, 2 ); | ||
67 | shft += 2; | ||
68 | } | ||
69 | } | 70 | } |
70 | if( i == len ) { | 71 | if( i < len ) { |
71 | /* One sample left to process */ | 72 | /* One sample left to process */ |
72 | nrg_tmp = silk_SMULBB( x[ i ], x[ i ] ); | 73 | nrg_tmp = silk_SMULBB( x[ i ], x[ i ] ); |
73 | nrg = (opus_int32)silk_ADD_RSHIFT_uint( nrg, nrg_tmp, shft ); | 74 | nrg = (opus_int32)silk_ADD_RSHIFT_uint( nrg, nrg_tmp, shft ); |
74 | } | 75 | } |
75 | 76 | ||
76 | /* Make sure to have at least one extra leading zero (two leading zeros in total) */ | 77 | silk_assert( nrg >= 0 ); |
77 | if( nrg & 0xC0000000 ) { | ||
78 | nrg = silk_RSHIFT_uint( (opus_uint32)nrg, 2 ); | ||
79 | shft += 2; | ||
80 | } | ||
81 | 78 | ||
82 | /* Output arguments */ | 79 | /* Output arguments */ |
83 | *shift = shft; | 80 | *shift = shft; |