blob: 269331cce4d4b7d00f4daccc0c9819ee6afd5c97 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
2 * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11/******************************************************************
12
13 iLBC Speech Coder ANSI-C Source Code
14
15 WebRtcIlbcfix_Smooth.c
16
17******************************************************************/
18
19#include "defines.h"
20#include "constants.h"
21#include "smooth_out_data.h"
22
23/*----------------------------------------------------------------*
24 * find the smoothed output data
25 *---------------------------------------------------------------*/
26
27void WebRtcIlbcfix_Smooth(
pbos@webrtc.org0946a562013-04-09 00:28:06 +000028 int16_t *odata, /* (o) smoothed output */
29 int16_t *current, /* (i) the un enhanced residual for
niklase@google.com470e71d2011-07-07 08:21:25 +000030 this block */
pbos@webrtc.org0946a562013-04-09 00:28:06 +000031 int16_t *surround /* (i) The approximation from the
niklase@google.com470e71d2011-07-07 08:21:25 +000032 surrounding sequences */
33 ) {
kwiberga1074022016-06-08 05:24:40 -070034 int16_t scale, scale1, scale2;
pbos@webrtc.org0946a562013-04-09 00:28:06 +000035 int16_t A, B, C, denomW16;
36 int32_t B_W32, denom, num;
37 int32_t errs;
38 int32_t w00,w10,w11, endiff, crit;
39 int32_t w00prim, w10prim, w11_div_w00;
40 int16_t w11prim;
41 int16_t bitsw00, bitsw10, bitsw11;
42 int32_t w11w00, w10w10, w00w00;
kwiberga1074022016-06-08 05:24:40 -070043 uint32_t max1, max2, max12;
niklase@google.com470e71d2011-07-07 08:21:25 +000044
45 /* compute some inner products (ensure no overflow by first calculating proper scale factor) */
46
47 w00 = w10 = w11 = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +000048
kwiberga1074022016-06-08 05:24:40 -070049 // Calculate a right shift that will let us sum ENH_BLOCKL pairwise products
50 // of values from the two sequences without overflowing an int32_t. (The +1
51 // in max1 and max2 are because WebRtcSpl_MaxAbsValueW16 will return 2**15 -
52 // 1 if the input array contains -2**15.)
53 max1 = WebRtcSpl_MaxAbsValueW16(current, ENH_BLOCKL) + 1;
54 max2 = WebRtcSpl_MaxAbsValueW16(surround, ENH_BLOCKL) + 1;
55 max12 = WEBRTC_SPL_MAX(max1, max2);
56 scale = (64 - 31) -
57 WebRtcSpl_CountLeadingZeros64((max12 * max12) * (uint64_t)ENH_BLOCKL);
niklase@google.com470e71d2011-07-07 08:21:25 +000058 scale=WEBRTC_SPL_MAX(0, scale);
59
60 w00=WebRtcSpl_DotProductWithScale(current,current,ENH_BLOCKL,scale);
61 w11=WebRtcSpl_DotProductWithScale(surround,surround,ENH_BLOCKL,scale);
62 w10=WebRtcSpl_DotProductWithScale(surround,current,ENH_BLOCKL,scale);
63
64 if (w00<0) w00 = WEBRTC_SPL_WORD32_MAX;
65 if (w11<0) w11 = WEBRTC_SPL_WORD32_MAX;
66
67 /* Rescale w00 and w11 to w00prim and w11prim, so that w00prim/w11prim
68 is in Q16 */
69
70 bitsw00 = WebRtcSpl_GetSizeInBits(w00);
71 bitsw11 = WebRtcSpl_GetSizeInBits(w11);
72 bitsw10 = WebRtcSpl_GetSizeInBits(WEBRTC_SPL_ABS_W32(w10));
73 scale1 = 31 - bitsw00;
74 scale2 = 15 - bitsw11;
75
76 if (scale2>(scale1-16)) {
77 scale2 = scale1 - 16;
78 } else {
79 scale1 = scale2 + 16;
80 }
81
bjornv@webrtc.org4ab23d02015-03-20 06:01:06 +000082 w00prim = w00 << scale1;
pbos@webrtc.org0946a562013-04-09 00:28:06 +000083 w11prim = (int16_t) WEBRTC_SPL_SHIFT_W32(w11, scale2);
niklase@google.com470e71d2011-07-07 08:21:25 +000084
85 /* Perform C = sqrt(w11/w00) (C is in Q11 since (16+6)/2=11) */
86 if (w11prim>64) {
bjornv@webrtc.org4ab23d02015-03-20 06:01:06 +000087 endiff = WebRtcSpl_DivW32W16(w00prim, w11prim) << 6;
pbos@webrtc.org0946a562013-04-09 00:28:06 +000088 C = (int16_t)WebRtcSpl_SqrtFloor(endiff); /* C is in Q11 */
niklase@google.com470e71d2011-07-07 08:21:25 +000089 } else {
90 C = 1;
91 }
92
93 /* first try enhancement without power-constraint */
94
95 errs = WebRtcIlbcfix_Smooth_odata(odata, current, surround, C);
96
97
98
99 /* if constraint violated by first try, add constraint */
100
101 if ( (6-scale+scale1) > 31) {
102 crit=0;
103 } else {
104 /* crit = 0.05 * w00 (Result in Q-6) */
105 crit = WEBRTC_SPL_SHIFT_W32(
bjornv@webrtc.org78ea06d2014-10-21 07:17:24 +0000106 WEBRTC_SPL_MUL(ENH_A0, w00prim >> 14),
niklase@google.com470e71d2011-07-07 08:21:25 +0000107 -(6-scale+scale1));
108 }
109
110 if (errs > crit) {
111
112 if( w00 < 1) {
113 w00=1;
114 }
115
116 /* Calculate w11*w00, w10*w10 and w00*w00 in the same Q domain */
117
118 scale1 = bitsw00-15;
119 scale2 = bitsw11-15;
120
121 if (scale2>scale1) {
122 scale = scale2;
123 } else {
124 scale = scale1;
125 }
126
bjornv@webrtc.orgba97ea62015-02-13 09:51:40 +0000127 w11w00 = (int16_t)WEBRTC_SPL_SHIFT_W32(w11, -scale) *
128 (int16_t)WEBRTC_SPL_SHIFT_W32(w00, -scale);
niklase@google.com470e71d2011-07-07 08:21:25 +0000129
bjornv@webrtc.orgba97ea62015-02-13 09:51:40 +0000130 w10w10 = (int16_t)WEBRTC_SPL_SHIFT_W32(w10, -scale) *
131 (int16_t)WEBRTC_SPL_SHIFT_W32(w10, -scale);
niklase@google.com470e71d2011-07-07 08:21:25 +0000132
bjornv@webrtc.orgba97ea62015-02-13 09:51:40 +0000133 w00w00 = (int16_t)WEBRTC_SPL_SHIFT_W32(w00, -scale) *
134 (int16_t)WEBRTC_SPL_SHIFT_W32(w00, -scale);
niklase@google.com470e71d2011-07-07 08:21:25 +0000135
136 /* Calculate (w11*w00-w10*w10)/(w00*w00) in Q16 */
137 if (w00w00>65536) {
138 endiff = (w11w00-w10w10);
139 endiff = WEBRTC_SPL_MAX(0, endiff);
140 /* denom is in Q16 */
bjornv@webrtc.org78ea06d2014-10-21 07:17:24 +0000141 denom = WebRtcSpl_DivW32W16(endiff, (int16_t)(w00w00 >> 16));
niklase@google.com470e71d2011-07-07 08:21:25 +0000142 } else {
143 denom = 65536;
144 }
145
146 if( denom > 7){ /* eliminates numerical problems
147 for if smooth */
148
149 scale=WebRtcSpl_GetSizeInBits(denom)-15;
150
151 if (scale>0) {
152 /* denomW16 is in Q(16+scale) */
bjornv@webrtc.org78ea06d2014-10-21 07:17:24 +0000153 denomW16 = (int16_t)(denom >> scale);
niklase@google.com470e71d2011-07-07 08:21:25 +0000154
155 /* num in Q(34-scale) */
bjornv@webrtc.org78ea06d2014-10-21 07:17:24 +0000156 num = ENH_A0_MINUS_A0A0DIV4 >> scale;
niklase@google.com470e71d2011-07-07 08:21:25 +0000157 } else {
158 /* denomW16 is in Q16 */
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000159 denomW16=(int16_t)denom;
niklase@google.com470e71d2011-07-07 08:21:25 +0000160
161 /* num in Q34 */
162 num=ENH_A0_MINUS_A0A0DIV4;
163 }
164
165 /* A sqrt( (ENH_A0-(ENH_A0^2)/4)*(w00*w00)/(w11*w00 + w10*w10) ) in Q9 */
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000166 A = (int16_t)WebRtcSpl_SqrtFloor(WebRtcSpl_DivW32W16(num, denomW16));
niklase@google.com470e71d2011-07-07 08:21:25 +0000167
168 /* B_W32 is in Q30 ( B = 1 - ENH_A0/2 - A * w10/w00 ) */
169 scale1 = 31-bitsw10;
170 scale2 = 21-scale1;
kwiberg4f6c2b62016-05-26 03:40:51 -0700171 w10prim = w10 * (1 << scale1);
niklase@google.com470e71d2011-07-07 08:21:25 +0000172 w00prim = WEBRTC_SPL_SHIFT_W32(w00, -scale2);
173 scale = bitsw00-scale2-15;
174
175 if (scale>0) {
bjornv@webrtc.org78ea06d2014-10-21 07:17:24 +0000176 w10prim >>= scale;
177 w00prim >>= scale;
niklase@google.com470e71d2011-07-07 08:21:25 +0000178 }
179
180 if ((w00prim>0)&&(w10prim>0)) {
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000181 w11_div_w00=WebRtcSpl_DivW32W16(w10prim, (int16_t)w00prim);
niklase@google.com470e71d2011-07-07 08:21:25 +0000182
183 if (WebRtcSpl_GetSizeInBits(w11_div_w00)+WebRtcSpl_GetSizeInBits(A)>31) {
184 B_W32 = 0;
185 } else {
pbos@webrtc.org0946a562013-04-09 00:28:06 +0000186 B_W32 = (int32_t)1073741824 - (int32_t)ENH_A0DIV2 -
niklase@google.com470e71d2011-07-07 08:21:25 +0000187 WEBRTC_SPL_MUL(A, w11_div_w00);
188 }
bjornv@webrtc.org78ea06d2014-10-21 07:17:24 +0000189 B = (int16_t)(B_W32 >> 16); /* B in Q14. */
niklase@google.com470e71d2011-07-07 08:21:25 +0000190 } else {
191 /* No smoothing */
192 A = 0;
193 B = 16384; /* 1 in Q14 */
194 }
195 }
196 else{ /* essentially no difference between cycles;
197 smoothing not needed */
198
199 A = 0;
200 B = 16384; /* 1 in Q14 */
201 }
202
203 /* create smoothed sequence */
204
205 WebRtcSpl_ScaleAndAddVectors(surround, A, 9,
206 current, B, 14,
207 odata, ENH_BLOCKL);
208 }
209 return;
210}