1 
2    /******************************************************************
3 
4        iLBC Speech Coder ANSI-C Source Code
5 
6        LPCencode.c
7 
8        Copyright (C) The Internet Society (2004).
9        All Rights Reserved.
10 
11    ******************************************************************/
12 
13    #include <string.h>
14 
15    #include "iLBC_define.h"
16    #include "helpfun.h"
17    #include "lsf.h"
18    #include "constants.h"
19 
20 
21 
22 
23 
24    /*----------------------------------------------------------------*
25     *  lpc analysis (subrutine to LPCencode)
26     *---------------------------------------------------------------*/
27 
SimpleAnalysis(float * lsf,float * data,iLBC_Enc_Inst_t * iLBCenc_inst)28    void SimpleAnalysis(
29        float *lsf,         /* (o) lsf coefficients */
30        float *data,    /* (i) new data vector */
31        iLBC_Enc_Inst_t *iLBCenc_inst
32                            /* (i/o) the encoder state structure */
33    ){
34        int k, is;
35        float temp[BLOCKL_MAX], lp[LPC_FILTERORDER + 1];
36        float lp2[LPC_FILTERORDER + 1];
37        float r[LPC_FILTERORDER + 1];
38 
39        is=LPC_LOOKBACK+BLOCKL_MAX-iLBCenc_inst->blockl;
40        memcpy(iLBCenc_inst->lpc_buffer+is,data,
41            iLBCenc_inst->blockl*sizeof(float));
42 
43        /* No lookahead, last window is asymmetric */
44 
45        for (k = 0; k < iLBCenc_inst->lpc_n; k++) {
46 
47            is = LPC_LOOKBACK;
48 
49            if (k < (iLBCenc_inst->lpc_n - 1)) {
50                window(temp, lpc_winTbl,
51                    iLBCenc_inst->lpc_buffer, BLOCKL_MAX);
52            } else {
53                window(temp, lpc_asymwinTbl,
54                    iLBCenc_inst->lpc_buffer + is, BLOCKL_MAX);
55            }
56 
57            autocorr(r, temp, BLOCKL_MAX, LPC_FILTERORDER);
58            window(r, r, lpc_lagwinTbl, LPC_FILTERORDER + 1);
59 
60            levdurb(lp, temp, r, LPC_FILTERORDER);
61            bwexpand(lp2, lp, LPC_CHIRP_SYNTDENUM, LPC_FILTERORDER+1);
62 
63            a2lsf(lsf + k*LPC_FILTERORDER, lp2);
64        }
65        is=LPC_LOOKBACK+BLOCKL_MAX-iLBCenc_inst->blockl;
66        memmove(iLBCenc_inst->lpc_buffer,
67            iLBCenc_inst->lpc_buffer+LPC_LOOKBACK+BLOCKL_MAX-is,
68            is*sizeof(float));
69    }
70 
71    /*----------------------------------------------------------------*
72 
73 
74 
75 
76 
77     *  lsf interpolator and conversion from lsf to a coefficients
78     *  (subrutine to SimpleInterpolateLSF)
79     *---------------------------------------------------------------*/
80 
LSFinterpolate2a_enc(float * a,float * lsf1,float * lsf2,float coef,long length)81    void LSFinterpolate2a_enc(
82        float *a,       /* (o) lpc coefficients */
83        float *lsf1,/* (i) first set of lsf coefficients */
84        float *lsf2,/* (i) second set of lsf coefficients */
85        float coef,     /* (i) weighting coefficient to use between
86                               lsf1 and lsf2 */
87        long length      /* (i) length of coefficient vectors */
88    ){
89        float  lsftmp[LPC_FILTERORDER];
90 
91        interpolate(lsftmp, lsf1, lsf2, coef, length);
92        lsf2a(a, lsftmp);
93    }
94 
95    /*----------------------------------------------------------------*
96     *  lsf interpolator (subrutine to LPCencode)
97     *---------------------------------------------------------------*/
98 
SimpleInterpolateLSF(float * syntdenum,float * weightdenum,float * lsf,float * lsfdeq,float * lsfold,float * lsfdeqold,int length,iLBC_Enc_Inst_t * iLBCenc_inst)99    void SimpleInterpolateLSF(
100        float *syntdenum,   /* (o) the synthesis filter denominator
101                                   resulting from the quantized
102                                   interpolated lsf */
103        float *weightdenum, /* (o) the weighting filter denominator
104                                   resulting from the unquantized
105                                   interpolated lsf */
106        float *lsf,         /* (i) the unquantized lsf coefficients */
107        float *lsfdeq,      /* (i) the dequantized lsf coefficients */
108        float *lsfold,      /* (i) the unquantized lsf coefficients of
109                                   the previous signal frame */
110        float *lsfdeqold, /* (i) the dequantized lsf coefficients of
111                                   the previous signal frame */
112        int length,         /* (i) should equate LPC_FILTERORDER */
113        iLBC_Enc_Inst_t *iLBCenc_inst
114                            /* (i/o) the encoder state structure */
115    ){
116        int    i, pos, lp_length;
117        float  lp[LPC_FILTERORDER + 1], *lsf2, *lsfdeq2;
118 
119        lsf2 = lsf + length;
120        lsfdeq2 = lsfdeq + length;
121        lp_length = length + 1;
122 
123        if (iLBCenc_inst->mode==30) {
124            /* sub-frame 1: Interpolation between old and first
125 
126 
127 
128 
129 
130               set of lsf coefficients */
131 
132            LSFinterpolate2a_enc(lp, lsfdeqold, lsfdeq,
133                lsf_weightTbl_30ms[0], length);
134            memcpy(syntdenum,lp,lp_length*sizeof(float));
135            LSFinterpolate2a_enc(lp, lsfold, lsf,
136                lsf_weightTbl_30ms[0], length);
137            bwexpand(weightdenum, lp, LPC_CHIRP_WEIGHTDENUM, lp_length);
138 
139            /* sub-frame 2 to 6: Interpolation between first
140               and second set of lsf coefficients */
141 
142            pos = lp_length;
143            for (i = 1; i < iLBCenc_inst->nsub; i++) {
144                LSFinterpolate2a_enc(lp, lsfdeq, lsfdeq2,
145                    lsf_weightTbl_30ms[i], length);
146                memcpy(syntdenum + pos,lp,lp_length*sizeof(float));
147 
148                LSFinterpolate2a_enc(lp, lsf, lsf2,
149                    lsf_weightTbl_30ms[i], length);
150                bwexpand(weightdenum + pos, lp,
151                    LPC_CHIRP_WEIGHTDENUM, lp_length);
152                pos += lp_length;
153            }
154        }
155        else {
156            pos = 0;
157            for (i = 0; i < iLBCenc_inst->nsub; i++) {
158                LSFinterpolate2a_enc(lp, lsfdeqold, lsfdeq,
159                    lsf_weightTbl_20ms[i], length);
160                memcpy(syntdenum+pos,lp,lp_length*sizeof(float));
161                LSFinterpolate2a_enc(lp, lsfold, lsf,
162                    lsf_weightTbl_20ms[i], length);
163                bwexpand(weightdenum+pos, lp,
164                    LPC_CHIRP_WEIGHTDENUM, lp_length);
165                pos += lp_length;
166            }
167        }
168 
169        /* update memory */
170 
171        if (iLBCenc_inst->mode==30) {
172            memcpy(lsfold, lsf2, length*sizeof(float));
173            memcpy(lsfdeqold, lsfdeq2, length*sizeof(float));
174        }
175        else {
176            memcpy(lsfold, lsf, length*sizeof(float));
177            memcpy(lsfdeqold, lsfdeq, length*sizeof(float));
178 
179 
180 
181 
182 
183        }
184    }
185 
186    /*----------------------------------------------------------------*
187     *  lsf quantizer (subrutine to LPCencode)
188     *---------------------------------------------------------------*/
189 
SimplelsfQ(float * lsfdeq,int * index,float * lsf,int lpc_n)190    void SimplelsfQ(
191        float *lsfdeq,    /* (o) dequantized lsf coefficients
192                               (dimension FILTERORDER) */
193        int *index,     /* (o) quantization index */
194        float *lsf,      /* (i) the lsf coefficient vector to be
195                               quantized (dimension FILTERORDER ) */
196        int lpc_n     /* (i) number of lsf sets to quantize */
197    ){
198        /* Quantize first LSF with memoryless split VQ */
199        SplitVQ(lsfdeq, index, lsf, lsfCbTbl, LSF_NSPLIT,
200            dim_lsfCbTbl, size_lsfCbTbl);
201 
202        if (lpc_n==2) {
203            /* Quantize second LSF with memoryless split VQ */
204            SplitVQ(lsfdeq + LPC_FILTERORDER, index + LSF_NSPLIT,
205                lsf + LPC_FILTERORDER, lsfCbTbl, LSF_NSPLIT,
206                dim_lsfCbTbl, size_lsfCbTbl);
207        }
208    }
209 
210    /*----------------------------------------------------------------*
211     *  lpc encoder
212     *---------------------------------------------------------------*/
213 
LPCencode(float * syntdenum,float * weightdenum,int * lsf_index,float * data,iLBC_Enc_Inst_t * iLBCenc_inst)214    void LPCencode(
215        float *syntdenum, /* (i/o) synthesis filter coefficients
216                                   before/after encoding */
217        float *weightdenum, /* (i/o) weighting denumerator
218                                   coefficients before/after
219                                   encoding */
220        int *lsf_index,     /* (o) lsf quantization index */
221        float *data,    /* (i) lsf coefficients to quantize */
222        iLBC_Enc_Inst_t *iLBCenc_inst
223                            /* (i/o) the encoder state structure */
224    ){
225        float lsf[LPC_FILTERORDER * LPC_N_MAX];
226        float lsfdeq[LPC_FILTERORDER * LPC_N_MAX];
227        int change=0;
228 
229        SimpleAnalysis(lsf, data, iLBCenc_inst);
230        SimplelsfQ(lsfdeq, lsf_index, lsf, iLBCenc_inst->lpc_n);
231 
232 
233 
234 
235 
236        change=LSF_check(lsfdeq, LPC_FILTERORDER, iLBCenc_inst->lpc_n);
237        SimpleInterpolateLSF(syntdenum, weightdenum,
238            lsf, lsfdeq, iLBCenc_inst->lsfold,
239            iLBCenc_inst->lsfdeqold, LPC_FILTERORDER, iLBCenc_inst);
240    }
241 
242