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_DoThePlc.c
16 
17 ******************************************************************/
18 
19 #include "defines.h"
20 #include "constants.h"
21 #include "comp_corr.h"
22 #include "bw_expand.h"
23 
24 /*----------------------------------------------------------------*
25  *  Packet loss concealment routine. Conceals a residual signal
26  *  and LP parameters. If no packet loss, update state.
27  *---------------------------------------------------------------*/
28 
WebRtcIlbcfix_DoThePlc(int16_t * PLCresidual,int16_t * PLClpc,int16_t PLI,int16_t * decresidual,int16_t * lpc,size_t inlag,IlbcDecoder * iLBCdec_inst)29 void WebRtcIlbcfix_DoThePlc(
30     int16_t *PLCresidual,  /* (o) concealed residual */
31     int16_t *PLClpc,    /* (o) concealed LP parameters */
32     int16_t PLI,     /* (i) packet loss indicator
33                                                            0 - no PL, 1 = PL */
34     int16_t *decresidual,  /* (i) decoded residual */
35     int16_t *lpc,    /* (i) decoded LPC (only used for no PL) */
36     size_t inlag,    /* (i) pitch lag */
37     IlbcDecoder *iLBCdec_inst
38     /* (i/o) decoder instance */
39                             ){
40   size_t i;
41   int32_t cross, ener, cross_comp, ener_comp = 0;
42   int32_t measure, maxMeasure, energy;
43   int16_t max, crossSquareMax, crossSquare;
44   size_t j, lag, randlag;
45   int16_t tmp1, tmp2;
46   int16_t shift1, shift2, shift3, shiftMax;
47   int16_t scale3;
48   size_t corrLen;
49   int32_t tmpW32, tmp2W32;
50   int16_t use_gain;
51   int16_t tot_gain;
52   int16_t max_perSquare;
53   int16_t scale1, scale2;
54   int16_t totscale;
55   int32_t nom;
56   int16_t denom;
57   int16_t pitchfact;
58   size_t use_lag;
59   int ind;
60   int16_t randvec[BLOCKL_MAX];
61 
62   /* Packet Loss */
63   if (PLI == 1) {
64 
65     (*iLBCdec_inst).consPLICount += 1;
66 
67     /* if previous frame not lost,
68        determine pitch pred. gain */
69 
70     if (iLBCdec_inst->prevPLI != 1) {
71 
72       /* Maximum 60 samples are correlated, preserve as high accuracy
73          as possible without getting overflow */
74       max = WebRtcSpl_MaxAbsValueW16((*iLBCdec_inst).prevResidual,
75                                      iLBCdec_inst->blockl);
76       scale3 = (WebRtcSpl_GetSizeInBits(max)<<1) - 25;
77       if (scale3 < 0) {
78         scale3 = 0;
79       }
80 
81       /* Store scale for use when interpolating between the
82        * concealment and the received packet */
83       iLBCdec_inst->prevScale = scale3;
84 
85       /* Search around the previous lag +/-3 to find the
86          best pitch period */
87       lag = inlag - 3;
88 
89       /* Guard against getting outside the frame */
90       corrLen = (size_t)WEBRTC_SPL_MIN(60, iLBCdec_inst->blockl-(inlag+3));
91 
92       WebRtcIlbcfix_CompCorr( &cross, &ener,
93                               iLBCdec_inst->prevResidual, lag, iLBCdec_inst->blockl, corrLen, scale3);
94 
95       /* Normalize and store cross^2 and the number of shifts */
96       shiftMax = WebRtcSpl_GetSizeInBits(WEBRTC_SPL_ABS_W32(cross))-15;
97       crossSquareMax = (int16_t)((
98           (int16_t)WEBRTC_SPL_SHIFT_W32(cross, -shiftMax) *
99           (int16_t)WEBRTC_SPL_SHIFT_W32(cross, -shiftMax)) >> 15);
100 
101       for (j=inlag-2;j<=inlag+3;j++) {
102         WebRtcIlbcfix_CompCorr( &cross_comp, &ener_comp,
103                                 iLBCdec_inst->prevResidual, j, iLBCdec_inst->blockl, corrLen, scale3);
104 
105         /* Use the criteria (corr*corr)/energy to compare if
106            this lag is better or not. To avoid the division,
107            do a cross multiplication */
108         shift1 = WebRtcSpl_GetSizeInBits(WEBRTC_SPL_ABS_W32(cross_comp))-15;
109         crossSquare = (int16_t)((
110             (int16_t)WEBRTC_SPL_SHIFT_W32(cross_comp, -shift1) *
111             (int16_t)WEBRTC_SPL_SHIFT_W32(cross_comp, -shift1)) >> 15);
112 
113         shift2 = WebRtcSpl_GetSizeInBits(ener)-15;
114         measure = (int16_t)WEBRTC_SPL_SHIFT_W32(ener, -shift2) * crossSquare;
115 
116         shift3 = WebRtcSpl_GetSizeInBits(ener_comp)-15;
117         maxMeasure = (int16_t)WEBRTC_SPL_SHIFT_W32(ener_comp, -shift3) *
118             crossSquareMax;
119 
120         /* Calculate shift value, so that the two measures can
121            be put in the same Q domain */
122         if(((shiftMax<<1)+shift3) > ((shift1<<1)+shift2)) {
123           tmp1 = WEBRTC_SPL_MIN(31, (shiftMax<<1)+shift3-(shift1<<1)-shift2);
124           tmp2 = 0;
125         } else {
126           tmp1 = 0;
127           tmp2 = WEBRTC_SPL_MIN(31, (shift1<<1)+shift2-(shiftMax<<1)-shift3);
128         }
129 
130         if ((measure>>tmp1) > (maxMeasure>>tmp2)) {
131           /* New lag is better => record lag, measure and domain */
132           lag = j;
133           crossSquareMax = crossSquare;
134           cross = cross_comp;
135           shiftMax = shift1;
136           ener = ener_comp;
137         }
138       }
139 
140       /* Calculate the periodicity for the lag with the maximum correlation.
141 
142          Definition of the periodicity:
143          abs(corr(vec1, vec2))/(sqrt(energy(vec1))*sqrt(energy(vec2)))
144 
145          Work in the Square domain to simplify the calculations
146          max_perSquare is less than 1 (in Q15)
147       */
148       tmp2W32=WebRtcSpl_DotProductWithScale(&iLBCdec_inst->prevResidual[iLBCdec_inst->blockl-corrLen],
149                                             &iLBCdec_inst->prevResidual[iLBCdec_inst->blockl-corrLen],
150                                             corrLen, scale3);
151 
152       if ((tmp2W32>0)&&(ener_comp>0)) {
153         /* norm energies to int16_t, compute the product of the energies and
154            use the upper int16_t as the denominator */
155 
156         scale1=(int16_t)WebRtcSpl_NormW32(tmp2W32)-16;
157         tmp1=(int16_t)WEBRTC_SPL_SHIFT_W32(tmp2W32, scale1);
158 
159         scale2=(int16_t)WebRtcSpl_NormW32(ener)-16;
160         tmp2=(int16_t)WEBRTC_SPL_SHIFT_W32(ener, scale2);
161         denom = (int16_t)((tmp1 * tmp2) >> 16);  /* in Q(scale1+scale2-16) */
162 
163         /* Square the cross correlation and norm it such that max_perSquare
164            will be in Q15 after the division */
165 
166         totscale = scale1+scale2-1;
167         tmp1 = (int16_t)WEBRTC_SPL_SHIFT_W32(cross, (totscale>>1));
168         tmp2 = (int16_t)WEBRTC_SPL_SHIFT_W32(cross, totscale-(totscale>>1));
169 
170         nom = tmp1 * tmp2;
171         max_perSquare = (int16_t)WebRtcSpl_DivW32W16(nom, denom);
172 
173       } else {
174         max_perSquare = 0;
175       }
176     }
177 
178     /* previous frame lost, use recorded lag and gain */
179 
180     else {
181       lag = iLBCdec_inst->prevLag;
182       max_perSquare = iLBCdec_inst->perSquare;
183     }
184 
185     /* Attenuate signal and scale down pitch pred gain if
186        several frames lost consecutively */
187 
188     use_gain = 32767;   /* 1.0 in Q15 */
189 
190     if (iLBCdec_inst->consPLICount*iLBCdec_inst->blockl>320) {
191       use_gain = 29491;  /* 0.9 in Q15 */
192     } else if (iLBCdec_inst->consPLICount*iLBCdec_inst->blockl>640) {
193       use_gain = 22938;  /* 0.7 in Q15 */
194     } else if (iLBCdec_inst->consPLICount*iLBCdec_inst->blockl>960) {
195       use_gain = 16384;  /* 0.5 in Q15 */
196     } else if (iLBCdec_inst->consPLICount*iLBCdec_inst->blockl>1280) {
197       use_gain = 0;   /* 0.0 in Q15 */
198     }
199 
200     /* Compute mixing factor of picth repeatition and noise:
201        for max_per>0.7 set periodicity to 1.0
202        0.4<max_per<0.7 set periodicity to (maxper-0.4)/0.7-0.4)
203        max_per<0.4 set periodicity to 0.0
204     */
205 
206     if (max_perSquare>7868) { /* periodicity > 0.7  (0.7^4=0.2401 in Q15) */
207       pitchfact = 32767;
208     } else if (max_perSquare>839) { /* 0.4 < periodicity < 0.7 (0.4^4=0.0256 in Q15) */
209       /* find best index and interpolate from that */
210       ind = 5;
211       while ((max_perSquare<WebRtcIlbcfix_kPlcPerSqr[ind])&&(ind>0)) {
212         ind--;
213       }
214       /* pitch fact is approximated by first order */
215       tmpW32 = (int32_t)WebRtcIlbcfix_kPlcPitchFact[ind] +
216           ((WebRtcIlbcfix_kPlcPfSlope[ind] *
217               (max_perSquare - WebRtcIlbcfix_kPlcPerSqr[ind])) >> 11);
218 
219       pitchfact = (int16_t)WEBRTC_SPL_MIN(tmpW32, 32767); /* guard against overflow */
220 
221     } else { /* periodicity < 0.4 */
222       pitchfact = 0;
223     }
224 
225     /* avoid repetition of same pitch cycle (buzzyness) */
226     use_lag = lag;
227     if (lag<80) {
228       use_lag = 2*lag;
229     }
230 
231     /* compute concealed residual */
232     energy = 0;
233 
234     for (i=0; i<iLBCdec_inst->blockl; i++) {
235 
236       /* noise component -  52 < randlagFIX < 117 */
237       iLBCdec_inst->seed = (int16_t)(iLBCdec_inst->seed * 31821 + 13849);
238       randlag = 53 + (iLBCdec_inst->seed & 63);
239       if (randlag > i) {
240         randvec[i] =
241             iLBCdec_inst->prevResidual[iLBCdec_inst->blockl + i - randlag];
242       } else {
243         randvec[i] = iLBCdec_inst->prevResidual[i - randlag];
244       }
245 
246       /* pitch repeatition component */
247       if (use_lag > i) {
248         PLCresidual[i] =
249             iLBCdec_inst->prevResidual[iLBCdec_inst->blockl + i - use_lag];
250       } else {
251         PLCresidual[i] = PLCresidual[i - use_lag];
252       }
253 
254       /* Attinuate total gain for each 10 ms */
255       if (i<80) {
256         tot_gain=use_gain;
257       } else if (i<160) {
258         tot_gain = (int16_t)((31130 * use_gain) >> 15);  /* 0.95*use_gain */
259       } else {
260         tot_gain = (int16_t)((29491 * use_gain) >> 15);  /* 0.9*use_gain */
261       }
262 
263 
264       /* mix noise and pitch repeatition */
265       PLCresidual[i] = (int16_t)((tot_gain *
266           ((pitchfact * PLCresidual[i] + (32767 - pitchfact) * randvec[i] +
267               16384) >> 15)) >> 15);
268 
269       /* Shifting down the result one step extra to ensure that no overflow
270          will occur */
271       energy += (PLCresidual[i] * PLCresidual[i]) >>
272           (iLBCdec_inst->prevScale + 1);
273     }
274 
275     /* less than 30 dB, use only noise */
276     if (energy < (WEBRTC_SPL_SHIFT_W32(((int32_t)iLBCdec_inst->blockl*900),-(iLBCdec_inst->prevScale+1)))) {
277       energy = 0;
278       for (i=0; i<iLBCdec_inst->blockl; i++) {
279         PLCresidual[i] = randvec[i];
280       }
281     }
282 
283     /* use the old LPC */
284     WEBRTC_SPL_MEMCPY_W16(PLClpc, (*iLBCdec_inst).prevLpc, LPC_FILTERORDER+1);
285 
286     /* Update state in case there are multiple frame losses */
287     iLBCdec_inst->prevLag = lag;
288     iLBCdec_inst->perSquare = max_perSquare;
289   }
290 
291   /* no packet loss, copy input */
292 
293   else {
294     WEBRTC_SPL_MEMCPY_W16(PLCresidual, decresidual, iLBCdec_inst->blockl);
295     WEBRTC_SPL_MEMCPY_W16(PLClpc, lpc, (LPC_FILTERORDER+1));
296     iLBCdec_inst->consPLICount = 0;
297   }
298 
299   /* update state */
300   iLBCdec_inst->prevPLI = PLI;
301   WEBRTC_SPL_MEMCPY_W16(iLBCdec_inst->prevLpc, PLClpc, (LPC_FILTERORDER+1));
302   WEBRTC_SPL_MEMCPY_W16(iLBCdec_inst->prevResidual, PLCresidual, iLBCdec_inst->blockl);
303 
304   return;
305 }
306