• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 "modules/audio_coding/codecs/ilbc/defines.h"
20 #include "modules/audio_coding/codecs/ilbc/constants.h"
21 #include "modules/audio_coding/codecs/ilbc/smooth_out_data.h"
22 
23 /*----------------------------------------------------------------*
24  * find the smoothed output data
25  *---------------------------------------------------------------*/
26 
WebRtcIlbcfix_Smooth(int16_t * odata,int16_t * current,int16_t * surround)27 void WebRtcIlbcfix_Smooth(
28     int16_t *odata,   /* (o) smoothed output */
29     int16_t *current,  /* (i) the un enhanced residual for
30                                 this block */
31     int16_t *surround  /* (i) The approximation from the
32                                 surrounding sequences */
33                           ) {
34   int16_t scale, scale1, scale2;
35   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;
43   uint32_t max1, max2, max12;
44 
45   /* compute some inner products (ensure no overflow by first calculating proper scale factor) */
46 
47   w00 = w10 = w11 = 0;
48 
49   // 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);
58   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 
82   w00prim = w00 << scale1;
83   w11prim = (int16_t) WEBRTC_SPL_SHIFT_W32(w11, scale2);
84 
85   /* Perform C = sqrt(w11/w00) (C is in Q11 since (16+6)/2=11) */
86   if (w11prim>64) {
87     endiff = WebRtcSpl_DivW32W16(w00prim, w11prim) << 6;
88     C = (int16_t)WebRtcSpl_SqrtFloor(endiff); /* C is in Q11 */
89   } 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(
106         WEBRTC_SPL_MUL(ENH_A0, w00prim >> 14),
107         -(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 
127     w11w00 = (int16_t)WEBRTC_SPL_SHIFT_W32(w11, -scale) *
128         (int16_t)WEBRTC_SPL_SHIFT_W32(w00, -scale);
129 
130     w10w10 = (int16_t)WEBRTC_SPL_SHIFT_W32(w10, -scale) *
131         (int16_t)WEBRTC_SPL_SHIFT_W32(w10, -scale);
132 
133     w00w00 = (int16_t)WEBRTC_SPL_SHIFT_W32(w00, -scale) *
134         (int16_t)WEBRTC_SPL_SHIFT_W32(w00, -scale);
135 
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 */
141       denom = WebRtcSpl_DivW32W16(endiff, (int16_t)(w00w00 >> 16));
142     } 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) */
153         denomW16 = (int16_t)(denom >> scale);
154 
155         /* num in Q(34-scale) */
156         num = ENH_A0_MINUS_A0A0DIV4 >> scale;
157       } else {
158         /* denomW16 is in Q16 */
159         denomW16=(int16_t)denom;
160 
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 */
166       A = (int16_t)WebRtcSpl_SqrtFloor(WebRtcSpl_DivW32W16(num, denomW16));
167 
168       /* B_W32 is in Q30 ( B = 1 - ENH_A0/2 - A * w10/w00 ) */
169       scale1 = 31-bitsw10;
170       scale2 = 21-scale1;
171       w10prim = w10 == 0 ? 0 : w10 * (1 << scale1);
172       w00prim = WEBRTC_SPL_SHIFT_W32(w00, -scale2);
173       scale = bitsw00-scale2-15;
174 
175       if (scale>0) {
176         w10prim >>= scale;
177         w00prim >>= scale;
178       }
179 
180       if ((w00prim>0)&&(w10prim>0)) {
181         w11_div_w00=WebRtcSpl_DivW32W16(w10prim, (int16_t)w00prim);
182 
183         if (WebRtcSpl_GetSizeInBits(w11_div_w00)+WebRtcSpl_GetSizeInBits(A)>31) {
184           B_W32 = 0;
185         } else {
186           B_W32 = (int32_t)1073741824 - (int32_t)ENH_A0DIV2 -
187               WEBRTC_SPL_MUL(A, w11_div_w00);
188         }
189         B = (int16_t)(B_W32 >> 16);  /* B in Q14. */
190       } 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 }
211