Tristan Matthews | 0a329cc | 2013-07-17 13:20:14 -0400 | [diff] [blame] | 1 | |
| 2 | /****************************************************************** |
| 3 | |
| 4 | iLBC Speech Coder ANSI-C Source Code |
| 5 | |
| 6 | StateSearchW.c |
| 7 | |
| 8 | Copyright (C) The Internet Society (2004). |
| 9 | All Rights Reserved. |
| 10 | |
| 11 | ******************************************************************/ |
| 12 | |
| 13 | #include <math.h> |
| 14 | #include <string.h> |
| 15 | |
| 16 | #include "iLBC_define.h" |
| 17 | #include "constants.h" |
| 18 | #include "filter.h" |
| 19 | #include "helpfun.h" |
| 20 | |
| 21 | /*----------------------------------------------------------------* |
| 22 | * predictive noise shaping encoding of scaled start state |
| 23 | * (subrutine for StateSearchW) |
| 24 | *---------------------------------------------------------------*/ |
| 25 | |
| 26 | void AbsQuantW( |
| 27 | iLBC_Enc_Inst_t *iLBCenc_inst, |
| 28 | /* (i) Encoder instance */ |
| 29 | float *in, /* (i) vector to encode */ |
| 30 | float *syntDenum, /* (i) denominator of synthesis filter */ |
| 31 | float *weightDenum, /* (i) denominator of weighting filter */ |
| 32 | int *out, /* (o) vector of quantizer indexes */ |
| 33 | int len, /* (i) length of vector to encode and |
| 34 | vector of quantizer indexes */ |
| 35 | int state_first /* (i) position of start state in the |
| 36 | 80 vec */ |
| 37 | ){ |
| 38 | float *syntOut; |
| 39 | float syntOutBuf[LPC_FILTERORDER+STATE_SHORT_LEN_30MS]; |
| 40 | float toQ, xq; |
| 41 | int n; |
| 42 | int index; |
| 43 | |
| 44 | /* initialization of buffer for filtering */ |
| 45 | |
| 46 | memset(syntOutBuf, 0, LPC_FILTERORDER*sizeof(float)); |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | /* initialization of pointer for filtering */ |
| 54 | |
| 55 | syntOut = &syntOutBuf[LPC_FILTERORDER]; |
| 56 | |
| 57 | /* synthesis and weighting filters on input */ |
| 58 | |
| 59 | if (state_first) { |
| 60 | AllPoleFilter (in, weightDenum, SUBL, LPC_FILTERORDER); |
| 61 | } else { |
| 62 | AllPoleFilter (in, weightDenum, |
| 63 | iLBCenc_inst->state_short_len-SUBL, |
| 64 | LPC_FILTERORDER); |
| 65 | } |
| 66 | |
| 67 | /* encoding loop */ |
| 68 | |
| 69 | for (n=0; n<len; n++) { |
| 70 | |
| 71 | /* time update of filter coefficients */ |
| 72 | |
| 73 | if ((state_first)&&(n==SUBL)){ |
| 74 | syntDenum += (LPC_FILTERORDER+1); |
| 75 | weightDenum += (LPC_FILTERORDER+1); |
| 76 | |
| 77 | /* synthesis and weighting filters on input */ |
| 78 | AllPoleFilter (&in[n], weightDenum, len-n, |
| 79 | LPC_FILTERORDER); |
| 80 | |
| 81 | } else if ((state_first==0)&& |
| 82 | (n==(iLBCenc_inst->state_short_len-SUBL))) { |
| 83 | syntDenum += (LPC_FILTERORDER+1); |
| 84 | weightDenum += (LPC_FILTERORDER+1); |
| 85 | |
| 86 | /* synthesis and weighting filters on input */ |
| 87 | AllPoleFilter (&in[n], weightDenum, len-n, |
| 88 | LPC_FILTERORDER); |
| 89 | |
| 90 | } |
| 91 | |
| 92 | /* prediction of synthesized and weighted input */ |
| 93 | |
| 94 | syntOut[n] = 0.0; |
| 95 | AllPoleFilter (&syntOut[n], weightDenum, 1, |
| 96 | LPC_FILTERORDER); |
| 97 | |
| 98 | /* quantization */ |
| 99 | |
| 100 | toQ = in[n]-syntOut[n]; |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | sort_sq(&xq, &index, toQ, state_sq3Tbl, 8); |
| 107 | out[n]=index; |
| 108 | syntOut[n] = state_sq3Tbl[out[n]]; |
| 109 | |
| 110 | /* update of the prediction filter */ |
| 111 | |
| 112 | AllPoleFilter(&syntOut[n], weightDenum, 1, |
| 113 | LPC_FILTERORDER); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | /*----------------------------------------------------------------* |
| 118 | * encoding of start state |
| 119 | *---------------------------------------------------------------*/ |
| 120 | |
| 121 | void StateSearchW( |
| 122 | iLBC_Enc_Inst_t *iLBCenc_inst, |
| 123 | /* (i) Encoder instance */ |
| 124 | float *residual,/* (i) target residual vector */ |
| 125 | float *syntDenum, /* (i) lpc synthesis filter */ |
| 126 | float *weightDenum, /* (i) weighting filter denuminator */ |
| 127 | int *idxForMax, /* (o) quantizer index for maximum |
| 128 | amplitude */ |
| 129 | int *idxVec, /* (o) vector of quantization indexes */ |
| 130 | int len, /* (i) length of all vectors */ |
| 131 | int state_first /* (i) position of start state in the |
| 132 | 80 vec */ |
| 133 | ){ |
| 134 | float dtmp, maxVal; |
| 135 | float tmpbuf[LPC_FILTERORDER+2*STATE_SHORT_LEN_30MS]; |
| 136 | float *tmp, numerator[1+LPC_FILTERORDER]; |
| 137 | float foutbuf[LPC_FILTERORDER+2*STATE_SHORT_LEN_30MS], *fout; |
| 138 | int k; |
| 139 | float qmax, scal; |
| 140 | |
| 141 | /* initialization of buffers and filter coefficients */ |
| 142 | |
| 143 | memset(tmpbuf, 0, LPC_FILTERORDER*sizeof(float)); |
| 144 | memset(foutbuf, 0, LPC_FILTERORDER*sizeof(float)); |
| 145 | for (k=0; k<LPC_FILTERORDER; k++) { |
| 146 | numerator[k]=syntDenum[LPC_FILTERORDER-k]; |
| 147 | } |
| 148 | numerator[LPC_FILTERORDER]=syntDenum[0]; |
| 149 | tmp = &tmpbuf[LPC_FILTERORDER]; |
| 150 | fout = &foutbuf[LPC_FILTERORDER]; |
| 151 | |
| 152 | /* circular convolution with the all-pass filter */ |
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | |
| 158 | |
| 159 | memcpy(tmp, residual, len*sizeof(float)); |
| 160 | memset(tmp+len, 0, len*sizeof(float)); |
| 161 | ZeroPoleFilter(tmp, numerator, syntDenum, 2*len, |
| 162 | LPC_FILTERORDER, fout); |
| 163 | for (k=0; k<len; k++) { |
| 164 | fout[k] += fout[k+len]; |
| 165 | } |
| 166 | |
| 167 | /* identification of the maximum amplitude value */ |
| 168 | |
| 169 | maxVal = fout[0]; |
| 170 | for (k=1; k<len; k++) { |
| 171 | |
| 172 | if (fout[k]*fout[k] > maxVal*maxVal){ |
| 173 | maxVal = fout[k]; |
| 174 | } |
| 175 | } |
| 176 | maxVal=(float)fabs(maxVal); |
| 177 | |
| 178 | /* encoding of the maximum amplitude value */ |
| 179 | |
| 180 | if (maxVal < 10.0) { |
| 181 | maxVal = 10.0; |
| 182 | } |
| 183 | maxVal = (float)log10(maxVal); |
| 184 | sort_sq(&dtmp, idxForMax, maxVal, state_frgqTbl, 64); |
| 185 | |
| 186 | /* decoding of the maximum amplitude representation value, |
| 187 | and corresponding scaling of start state */ |
| 188 | |
| 189 | maxVal=state_frgqTbl[*idxForMax]; |
| 190 | qmax = (float)pow(10,maxVal); |
| 191 | scal = (float)(4.5)/qmax; |
| 192 | for (k=0; k<len; k++){ |
| 193 | fout[k] *= scal; |
| 194 | } |
| 195 | |
| 196 | /* predictive noise shaping encoding of scaled start state */ |
| 197 | |
| 198 | AbsQuantW(iLBCenc_inst, fout,syntDenum, |
| 199 | weightDenum,idxVec, len, state_first); |
| 200 | } |
| 201 | |
| 202 | |
| 203 | |
| 204 | |
| 205 | |
| 206 | |
| 207 | |
| 208 | |
| 209 | |
| 210 | |
| 211 | |