aboutsummaryrefslogtreecommitdiffstats
path: root/codecs/ilbc/createCB.c
blob: 4d95b7513cb0fd14f4e596bb9c00eec770f1c045 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
 
/****************************************************************** 
 
    iLBC Speech Coder ANSI-C Source Code 
 
    createCB.c 
 
    Copyright (c) 2001, 
    Global IP Sound AB. 
    All rights reserved. 
 
******************************************************************/ 
 
#include "iLBC_define.h" 
#include "constants.h" 
#include "createCB.h"
#include <string.h> 
#include <math.h> 
 
/*----------------------------------------------------------------* 
 *  Construct an additional codebook vector by filtering the 
 *  initial codebook buffer. This vector is then used to expand 
 *  the codebook with an additional section. 
 *---------------------------------------------------------------*/ 
 
void filteredCBvecs( 
    float *cbvectors,   /* (o) Codebook vectors for the higher 
section */ 
    float *mem,         /* (i) Buffer to create codebook vector from 
*/ 
    int lMem        /* (i) Length of buffer */ 
){ 
    int j, k; 
    float *pp, *pp1; 
    float tempbuff2[CB_MEML+CB_FILTERLEN]; 
    float *pos; 
 
    memset(tempbuff2, 0, (CB_HALFFILTERLEN-1)*sizeof(float)); 
    memcpy(&tempbuff2[CB_HALFFILTERLEN-1], mem, lMem*sizeof(float)); 
    memset(&tempbuff2[lMem+CB_HALFFILTERLEN-1], 0,  
        (CB_HALFFILTERLEN+1)*sizeof(float)); 
 
    /* Create codebook vector for higher section by filtering */ 
 
    /* do filtering */ 
    pos=cbvectors; 
    memset(pos, 0, lMem*sizeof(float)); 
    for (k=0; k<lMem; k++) { 
        pp=&tempbuff2[k]; 
        pp1=&cbfiltersTbl[0]; 
        for (j=0;j<CB_FILTERLEN;j++) { 
            (*pos)+=(*pp++)*(*pp1++); 
        } 
        pos++; 
    } 
} 
 
/*----------------------------------------------------------------* 
 *  Search the augmented part of the codebook to find the best 
 *  measure. 
 *----------------------------------------------------------------*/ 
 
void searchAugmentedCB( 
    int low,        /* (i) Start index for the search */ 
    int high,           /* (i) End index for the search */ 
    int stage,          /* (i) Current stage */ 
    int startIndex,     /* (i) Codebook index for the first  
                               aug vector */ 
    float *target,      /* (i) Target vector for encoding */ 
    float *buffer,      /* (i) Pointer to the end of the buffer for 
                               augmented codebook construction */ 
    float *max_measure, /* (i/o) Currently maximum measure */ 
    int *best_index,/* (o) Currently the best index */ 
    float *gain,    /* (o) Currently the best gain */ 
    float *energy,      /* (o) Energy of augmented codebook  
                               vectors */ 
    float *invenergy/* (o) Inv energy of augmented codebook  
                               vectors */ 
) { 
    int lagcount, ilow, j, tmpIndex; 
    float *pp, *ppo, *ppi, *ppe, crossDot, alfa;  
    float weighted, measure, nrjRecursive; 
    float ftmp; 
 
    /* Compute the energy for the first (low-5)  
       noninterpolated samples */ 
    nrjRecursive = (float) 0.0; 
    pp = buffer - low + 1; 
    for (j=0; j<(low-5); j++) { 
        nrjRecursive += ( (*pp)*(*pp) ); 
        pp++; 
    } 
    ppe = buffer - low; 
 
 
    for (lagcount=low; lagcount<=high; lagcount++) { 
 
        /* Index of the codebook vector used for retrieving  
           energy values */ 
        tmpIndex = startIndex+lagcount-20; 
 
        ilow = lagcount-4; 
             
        /* Update the energy recursively to save complexity */ 
        nrjRecursive = nrjRecursive + (*ppe)*(*ppe); 
        ppe--; 
        energy[tmpIndex] = nrjRecursive; 
 
        /* Compute cross dot product for the first (low-5) samples */ 
        crossDot = (float) 0.0; 
        pp = buffer-lagcount; 
        for (j=0; j<ilow; j++) { 
            crossDot += target[j]*(*pp++); 
        } 
 
        /* interpolation */ 
        alfa = (float) 0.2; 
        ppo = buffer-4; 
        ppi = buffer-lagcount-4; 
        for (j=ilow; j<lagcount; j++) { 
            weighted = ((float)1.0-alfa)*(*ppo)+alfa*(*ppi); 
            ppo++; 
            ppi++; 
            energy[tmpIndex] += weighted*weighted; 
            crossDot += target[j]*weighted; 
            alfa += (float)0.2; 
        } 
 
        /* Compute energy and cross dot product for the  
           remaining samples */ 
        pp = buffer - lagcount; 
        for (j=lagcount; j<SUBL; j++) { 
            energy[tmpIndex] += (*pp)*(*pp); 
            crossDot += target[j]*(*pp++); 
        } 
         
        if(energy[tmpIndex]>0.0) { 
            invenergy[tmpIndex]=(float)1.0/(energy[tmpIndex]+EPS); 
        } else { 
            invenergy[tmpIndex] = (float) 0.0; 
        } 
         
        if (stage==0) { 
            measure = (float)-10000000.0; 
             
            if (crossDot > 0.0) { 
                measure = crossDot*crossDot*invenergy[tmpIndex]; 
            } 
        } 
        else { 
            measure = crossDot*crossDot*invenergy[tmpIndex]; 
        } 
     
        /* check if measure is better */ 
        ftmp = crossDot*invenergy[tmpIndex]; 
         
        if ((measure>*max_measure) && (fabs(ftmp)<CB_MAXGAIN)) { 
            *best_index = tmpIndex; 
            *max_measure = measure; 
            *gain = ftmp; 
        } 
    } 
} 
 
 
/*----------------------------------------------------------------* 
 *  Recreate a specific codebook vector from the augmented part. 
 * 
 *----------------------------------------------------------------*/ 
 
void createAugmentedVec( 
    int index,      /* (i) Index for the augmented vector  
                           to be created */ 
    float *buffer,  /* (i) Pointer to the end of the buffer for 
                           augmented codebook construction */ 
    float *cbVec/* (o) The construced codebook vector */ 
) { 
    int ilow, j; 
    float *pp, *ppo, *ppi, alfa, alfa1, weighted; 
 
    ilow = index-5; 
             
    /* copy the first noninterpolated part */ 
 
    pp = buffer-index; 
    memcpy(cbVec,pp,sizeof(float)*index); 
 
    /* interpolation */ 
 
    alfa1 = (float)0.2; 
    alfa = 0.0; 
    ppo = buffer-5; 
    ppi = buffer-index-5; 
    for (j=ilow; j<index; j++) { 
        weighted = ((float)1.0-alfa)*(*ppo)+alfa*(*ppi); 
        ppo++; 
        ppi++; 
        cbVec[j] = weighted; 
        alfa += alfa1; 
    } 
 
    /* copy the second noninterpolated part */ 
 
    pp = buffer - index; 
    memcpy(cbVec+index,pp,sizeof(float)*(SUBL-index)); 
}