aboutsummaryrefslogtreecommitdiffstats
path: root/codecs/ilbc/StateSearchW.c
blob: e1134ae56b13c37ca3442a192d271908915eaa04 (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
 
/****************************************************************** 
 
    iLBC Speech Coder ANSI-C Source Code 
 
    StateSearchW.c  
 
    Copyright (c) 2001, 
    Global IP Sound AB. 
    All rights reserved. 
 
******************************************************************/ 
 
#include <math.h>  
#include <string.h> 
 
#include "iLBC_define.h" 
#include "constants.h" 
#include "filter.h" 
#include "helpfun.h" 
#include "StateSearchW.h"
 
/*----------------------------------------------------------------* 
 *  predictive noise shaping encoding of scaled start state  
 *  (subrutine for StateSearchW)  
 *---------------------------------------------------------------*/ 
 
void AbsQuantW( 
    float *in,          /* (i) vector to encode */ 
    float *syntDenum,   /* (i) denominator of synthesis filter */ 
    float *weightDenum, /* (i) denominator of weighting filter */ 
    int *out,           /* (o) vector of quantizer indexes */ 
    int len,        /* (i) length of vector to encode and  
                               vector of quantizer indexes */ 
    int state_first     /* (i) position of start state in the  
                               80 vec */ 
){ 
    float *syntOut, syntOutBuf[LPC_FILTERORDER+STATE_SHORT_LEN]; 
    float toQ, xq; 
    int n; 
    int index; 
 
    /* initialization of buffer for filtering */ 
             
    memset(syntOutBuf, 0, LPC_FILTERORDER*sizeof(float)); 
 
    /* initialization of pointer for filtering */ 
 
    syntOut = &syntOutBuf[LPC_FILTERORDER]; 
     
    /* synthesis and weighting filters on input */ 
     
    if (state_first) { 
        AllPoleFilter (in, weightDenum, SUBL, LPC_FILTERORDER); 
    } else { 
        AllPoleFilter (in, weightDenum, STATE_SHORT_LEN-SUBL,  
            LPC_FILTERORDER); 
    } 
 
    /* encoding loop */ 
 
    for(n=0;n<len;n++){ 
         
        /* time update of filter coefficients */   
         
        if ((state_first)&&(n==SUBL)){ 
            syntDenum += (LPC_FILTERORDER+1); 
            weightDenum += (LPC_FILTERORDER+1); 
 
            /* synthesis and weighting filters on input */ 
            AllPoleFilter (&in[n], weightDenum, len-n,  
                LPC_FILTERORDER); 
 
        } else if ((state_first==0)&&(n==(STATE_SHORT_LEN-SUBL))) { 
            syntDenum += (LPC_FILTERORDER+1); 
            weightDenum += (LPC_FILTERORDER+1); 
 
            /* synthesis and weighting filters on input */ 
            AllPoleFilter (&in[n], weightDenum, len-n,  
                LPC_FILTERORDER); 
             
        } 
         
        /* prediction of synthesized and weighted input */ 
 
        syntOut[n] = 0.0; 
        AllPoleFilter (&syntOut[n], weightDenum, 1, LPC_FILTERORDER); 
         
        /* quantization */       
 
        toQ = in[n]-syntOut[n]; 
        sort_sq(&xq, &index, toQ, state_sq3Tbl, 8); 
        out[n]=index; 
        syntOut[n] = state_sq3Tbl[out[n]]; 
 
        /* update of the prediction filter */ 
 
        AllPoleFilter(&syntOut[n], weightDenum, 1, LPC_FILTERORDER); 
    } 
} 
 
/*----------------------------------------------------------------* 
 *  encoding of start state                           
 *---------------------------------------------------------------*/ 
 
void StateSearchW(  
    float *residual,/* (i) target residual vector */ 
    float *syntDenum,   /* (i) lpc synthesis filter */ 
    float *weightDenum, /* (i) weighting filter denuminator */ 
    int *idxForMax,     /* (o) quantizer index for maximum  
                               amplitude */ 
    int *idxVec,    /* (o) vector of quantization indexes */ 
    int len,        /* (i) length of all vectors */ 
    int state_first     /* (i) position of start state in the  
                               80 vec */ 
){   
    float dtmp, maxVal, tmpbuf[LPC_FILTERORDER+2*STATE_SHORT_LEN]; 
    float *tmp, numerator[1+LPC_FILTERORDER];  
    float foutbuf[LPC_FILTERORDER+2*STATE_SHORT_LEN], *fout; 
    int k; 
    float qmax, scal; 
     
    /* initialization of buffers and filter coefficients */ 
 
    memset(tmpbuf, 0, LPC_FILTERORDER*sizeof(float)); 
    memset(foutbuf, 0, LPC_FILTERORDER*sizeof(float)); 
    for(k=0; k<LPC_FILTERORDER; k++){ 
        numerator[k]=syntDenum[LPC_FILTERORDER-k]; 
    } 
    numerator[LPC_FILTERORDER]=syntDenum[0]; 
    tmp = &tmpbuf[LPC_FILTERORDER]; 
    fout = &foutbuf[LPC_FILTERORDER]; 
 
    /* circular convolution with the all-pass filter */ 
     
    memcpy(tmp, residual, len*sizeof(float)); 
    memset(tmp+len, 0, len*sizeof(float)); 
    ZeroPoleFilter(tmp, numerator, syntDenum, 2*len,  
        LPC_FILTERORDER, fout); 
    for(k=0;k<len;k++){ 
        fout[k] += fout[k+len]; 
    }    
         
    /* identification of the maximum amplitude value */ 
     
    maxVal = fout[0]; 
    for(k=1; k<len; k++){ 
         
        if(fout[k]*fout[k] > maxVal*maxVal){ 
            maxVal = fout[k]; 
        } 
    } 
    maxVal=(float)fabs(maxVal); 
         
    /* encoding of the maximum amplitude value */ 
     
    if(maxVal < 10.0){ 
        maxVal = 10.0; 
    } 
    maxVal = (float)log10(maxVal); 
    sort_sq(&dtmp, idxForMax, maxVal, state_frgqTbl, 64); 
 
    /* decoding of the maximum amplitude representation value, 
       and corresponding scaling of start state */ 
 
    maxVal=state_frgqTbl[*idxForMax]; 
    qmax = (float)pow(10,maxVal); 
    scal = (float)(4.5)/qmax; 
    for(k=0;k<len;k++){ 
        fout[k] *= scal; 
    } 
 
    /* predictive noise shaping encoding of scaled start state */ 
 
    AbsQuantW(fout,syntDenum,weightDenum,idxVec, len, state_first); 
}