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