2
/******************************************************************
4
iLBC Speech Coder ANSI-C Source Code
8
Copyright (C) The Internet Society (2004).
11
******************************************************************/
21
#include "iLBC_define.h"
23
/*----------------------------------------------------------------*
24
* Compute cross correlation and pitch gain for pitch prediction
25
* of last subframe at given lag.
26
*---------------------------------------------------------------*/
29
float *cc, /* (o) cross correlation coefficient */
30
float *gc, /* (o) gain */
32
float *buffer, /* (i) signal buffer */
33
int lag, /* (i) pitch lag */
34
int bLen, /* (i) length of buffer */
35
int sRange /* (i) correlation search length */
38
float ftmp1, ftmp2, ftmp3;
40
/* Guard against getting outside buffer */
41
if ((bLen-sRange-lag)<0) {
48
for (i=0; i<sRange; i++) {
49
ftmp1 += buffer[bLen-sRange+i] *
50
buffer[bLen-sRange+i-lag];
51
ftmp2 += buffer[bLen-sRange+i-lag] *
52
buffer[bLen-sRange+i-lag];
53
ftmp3 += buffer[bLen-sRange+i] *
54
buffer[bLen-sRange+i];
58
*cc = ftmp1*ftmp1/ftmp2;
59
*gc = (float)fabs(ftmp1/ftmp2);
60
*pm=(float)fabs(ftmp1)/
61
((float)sqrt(ftmp2)*(float)sqrt(ftmp3));
74
/*----------------------------------------------------------------*
75
* Packet loss concealment routine. Conceals a residual signal
76
* and LP parameters. If no packet loss, update state.
77
*---------------------------------------------------------------*/
80
float *PLCresidual, /* (o) concealed residual */
81
float *PLClpc, /* (o) concealed LP parameters */
82
int PLI, /* (i) packet loss indicator
84
float *decresidual, /* (i) decoded residual */
85
float *lpc, /* (i) decoded LPC (only used for no PL) */
86
int inlag, /* (i) pitch lag */
87
iLBC_Dec_Inst_t *iLBCdec_inst
88
/* (i/o) decoder instance */
93
float gain_comp, maxcc_comp, per, max_per=0;
95
float ftmp, randvec[BLOCKL_MAX], pitchfact, energy;
101
iLBCdec_inst->consPLICount += 1;
103
/* if previous frame not lost,
104
determine pitch pred. gain */
106
if (iLBCdec_inst->prevPLI != 1) {
108
/* Search around the previous lag to find the
112
compCorr(&maxcc, &gain, &max_per,
113
iLBCdec_inst->prevResidual,
114
lag, iLBCdec_inst->blockl, 60);
115
for (i=inlag-2;i<=inlag+3;i++) {
116
compCorr(&maxcc_comp, &gain_comp, &per,
117
iLBCdec_inst->prevResidual,
118
i, iLBCdec_inst->blockl, 60);
120
if (maxcc_comp>maxcc) {
135
/* previous frame lost, use recorded lag and periodicity */
138
lag=iLBCdec_inst->prevLag;
139
max_per=iLBCdec_inst->per;
145
if (iLBCdec_inst->consPLICount*iLBCdec_inst->blockl>320)
147
else if (iLBCdec_inst->consPLICount*
148
iLBCdec_inst->blockl>2*320)
150
else if (iLBCdec_inst->consPLICount*
151
iLBCdec_inst->blockl>3*320)
153
else if (iLBCdec_inst->consPLICount*
154
iLBCdec_inst->blockl>4*320)
157
/* mix noise and pitch repeatition */
158
ftmp=(float)sqrt(max_per);
160
pitchfact=(float)1.0;
161
else if (ftmp>(float)0.4)
162
pitchfact=(ftmp-(float)0.4)/((float)0.7-(float)0.4);
167
/* avoid repetition of same pitch cycle */
173
/* compute concealed residual */
181
for (i=0; i<iLBCdec_inst->blockl; i++) {
183
/* noise component */
185
iLBCdec_inst->seed=(iLBCdec_inst->seed*69069L+1) &
187
randlag = 50 + ((signed long) iLBCdec_inst->seed)%70;
192
iLBCdec_inst->prevResidual[
193
iLBCdec_inst->blockl+pick];
195
randvec[i] = randvec[pick];
198
/* pitch repeatition component */
203
iLBCdec_inst->prevResidual[
204
iLBCdec_inst->blockl+pick];
206
PLCresidual[i] = PLCresidual[pick];
209
/* mix random and periodicity component */
212
PLCresidual[i] = use_gain*(pitchfact *
214
((float)1.0 - pitchfact) * randvec[i]);
216
PLCresidual[i] = (float)0.95*use_gain*(pitchfact *
218
((float)1.0 - pitchfact) * randvec[i]);
220
PLCresidual[i] = (float)0.9*use_gain*(pitchfact *
222
((float)1.0 - pitchfact) * randvec[i]);
224
energy += PLCresidual[i] * PLCresidual[i];
227
/* less than 30 dB, use only noise */
234
if (sqrt(energy/(float)iLBCdec_inst->blockl) < 30.0) {
236
for (i=0; i<iLBCdec_inst->blockl; i++) {
237
PLCresidual[i] = randvec[i];
243
memcpy(PLClpc,iLBCdec_inst->prevLpc,
244
(LPC_FILTERORDER+1)*sizeof(float));
248
/* no packet loss, copy input */
251
memcpy(PLCresidual, decresidual,
252
iLBCdec_inst->blockl*sizeof(float));
253
memcpy(PLClpc, lpc, (LPC_FILTERORDER+1)*sizeof(float));
254
iLBCdec_inst->consPLICount = 0;
260
iLBCdec_inst->prevLag = lag;
261
iLBCdec_inst->per=max_per;
264
iLBCdec_inst->prevPLI = PLI;
265
memcpy(iLBCdec_inst->prevLpc, PLClpc,
266
(LPC_FILTERORDER+1)*sizeof(float));
267
memcpy(iLBCdec_inst->prevResidual, PLCresidual,
268
iLBCdec_inst->blockl*sizeof(float));