Coverage Report

Created: 2025-11-09 06:35

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/speex/libspeex/ltp.c
Line
Count
Source
1
/* Copyright (C) 2002-2006 Jean-Marc Valin
2
   File: ltp.c
3
   Long-Term Prediction functions
4
5
   Redistribution and use in source and binary forms, with or without
6
   modification, are permitted provided that the following conditions
7
   are met:
8
9
   - Redistributions of source code must retain the above copyright
10
   notice, this list of conditions and the following disclaimer.
11
12
   - Redistributions in binary form must reproduce the above copyright
13
   notice, this list of conditions and the following disclaimer in the
14
   documentation and/or other materials provided with the distribution.
15
16
   - Neither the name of the Xiph.org Foundation nor the names of its
17
   contributors may be used to endorse or promote products derived from
18
   this software without specific prior written permission.
19
20
   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21
   ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22
   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23
   A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
24
   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25
   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26
   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27
   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28
   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29
   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30
   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
*/
32
33
#ifdef HAVE_CONFIG_H
34
#include "config.h"
35
#endif
36
37
#include <math.h>
38
#include "ltp.h"
39
#include "stack_alloc.h"
40
#include "filters.h"
41
#include "math_approx.h"
42
#include "os_support.h"
43
44
#ifndef NULL
45
#define NULL 0
46
#endif
47
48
49
#ifdef _USE_SSE
50
#include "ltp_sse.h"
51
#elif defined (ARM4_ASM) || defined(ARM5E_ASM)
52
#include "ltp_arm4.h"
53
#elif defined (BFIN_ASM)
54
#include "ltp_bfin.h"
55
#endif
56
57
#ifndef OVERRIDE_INNER_PROD
58
spx_word32_t inner_prod(const spx_word16_t *x, const spx_word16_t *y, int len)
59
1.11M
{
60
1.11M
   spx_word32_t sum=0;
61
1.11M
   len >>= 2;
62
24.3M
   while(len--)
63
23.2M
   {
64
23.2M
      spx_word32_t part=0;
65
23.2M
      part = MAC16_16(part,*x++,*y++);
66
23.2M
      part = MAC16_16(part,*x++,*y++);
67
23.2M
      part = MAC16_16(part,*x++,*y++);
68
23.2M
      part = MAC16_16(part,*x++,*y++);
69
      /* HINT: If you had a 40-bit accumulator, you could shift only at the end */
70
23.2M
      sum = ADD32(sum,SHR32(part,6));
71
23.2M
   }
72
1.11M
   return sum;
73
1.11M
}
74
#endif
75
76
#ifndef DISABLE_ENCODER
77
78
#ifndef OVERRIDE_PITCH_XCORR
79
#if 0 /* HINT: Enable this for machines with enough registers (i.e. not x86) */
80
static void pitch_xcorr(const spx_word16_t *_x, const spx_word16_t *_y, spx_word32_t *corr, int len, int nb_pitch, char *stack)
81
{
82
   int i,j;
83
   for (i=0;i<nb_pitch;i+=4)
84
   {
85
      /* Compute correlation*/
86
      /*corr[nb_pitch-1-i]=inner_prod(x, _y+i, len);*/
87
      spx_word32_t sum1=0;
88
      spx_word32_t sum2=0;
89
      spx_word32_t sum3=0;
90
      spx_word32_t sum4=0;
91
      const spx_word16_t *y = _y+i;
92
      const spx_word16_t *x = _x;
93
      spx_word16_t y0, y1, y2, y3;
94
      /*y0=y[0];y1=y[1];y2=y[2];y3=y[3];*/
95
      y0=*y++;
96
      y1=*y++;
97
      y2=*y++;
98
      y3=*y++;
99
      for (j=0;j<len;j+=4)
100
      {
101
         spx_word32_t part1;
102
         spx_word32_t part2;
103
         spx_word32_t part3;
104
         spx_word32_t part4;
105
         part1 = MULT16_16(*x,y0);
106
         part2 = MULT16_16(*x,y1);
107
         part3 = MULT16_16(*x,y2);
108
         part4 = MULT16_16(*x,y3);
109
         x++;
110
         y0=*y++;
111
         part1 = MAC16_16(part1,*x,y1);
112
         part2 = MAC16_16(part2,*x,y2);
113
         part3 = MAC16_16(part3,*x,y3);
114
         part4 = MAC16_16(part4,*x,y0);
115
         x++;
116
         y1=*y++;
117
         part1 = MAC16_16(part1,*x,y2);
118
         part2 = MAC16_16(part2,*x,y3);
119
         part3 = MAC16_16(part3,*x,y0);
120
         part4 = MAC16_16(part4,*x,y1);
121
         x++;
122
         y2=*y++;
123
         part1 = MAC16_16(part1,*x,y3);
124
         part2 = MAC16_16(part2,*x,y0);
125
         part3 = MAC16_16(part3,*x,y1);
126
         part4 = MAC16_16(part4,*x,y2);
127
         x++;
128
         y3=*y++;
129
130
         sum1 = ADD32(sum1,SHR32(part1,6));
131
         sum2 = ADD32(sum2,SHR32(part2,6));
132
         sum3 = ADD32(sum3,SHR32(part3,6));
133
         sum4 = ADD32(sum4,SHR32(part4,6));
134
      }
135
      corr[nb_pitch-1-i]=sum1;
136
      corr[nb_pitch-2-i]=sum2;
137
      corr[nb_pitch-3-i]=sum3;
138
      corr[nb_pitch-4-i]=sum4;
139
   }
140
141
}
142
#else
143
static void pitch_xcorr(const spx_word16_t *_x, const spx_word16_t *_y, spx_word32_t *corr, int len, int nb_pitch, char *stack)
144
4.54k
{
145
4.54k
   int i;
146
555k
   for (i=0;i<nb_pitch;i++)
147
551k
   {
148
      /* Compute correlation*/
149
551k
      corr[nb_pitch-1-i]=inner_prod(_x, _y+i, len);
150
551k
   }
151
152
4.54k
}
153
#endif
154
#endif
155
156
#ifndef OVERRIDE_COMPUTE_PITCH_ERROR
157
static inline spx_word32_t compute_pitch_error(spx_word16_t *C, spx_word16_t *g, spx_word16_t pitch_control)
158
10.6M
{
159
10.6M
   spx_word32_t sum = 0;
160
10.6M
   sum = ADD32(sum,MULT16_16(MULT16_16_16(g[0],pitch_control),C[0]));
161
10.6M
   sum = ADD32(sum,MULT16_16(MULT16_16_16(g[1],pitch_control),C[1]));
162
10.6M
   sum = ADD32(sum,MULT16_16(MULT16_16_16(g[2],pitch_control),C[2]));
163
10.6M
   sum = SUB32(sum,MULT16_16(MULT16_16_16(g[0],g[1]),C[3]));
164
10.6M
   sum = SUB32(sum,MULT16_16(MULT16_16_16(g[2],g[1]),C[4]));
165
10.6M
   sum = SUB32(sum,MULT16_16(MULT16_16_16(g[2],g[0]),C[5]));
166
10.6M
   sum = SUB32(sum,MULT16_16(MULT16_16_16(g[0],g[0]),C[6]));
167
10.6M
   sum = SUB32(sum,MULT16_16(MULT16_16_16(g[1],g[1]),C[7]));
168
10.6M
   sum = SUB32(sum,MULT16_16(MULT16_16_16(g[2],g[2]),C[8]));
169
10.6M
   return sum;
170
10.6M
}
ltp.c:compute_pitch_error
Line
Count
Source
158
5.33M
{
159
5.33M
   spx_word32_t sum = 0;
160
5.33M
   sum = ADD32(sum,MULT16_16(MULT16_16_16(g[0],pitch_control),C[0]));
161
5.33M
   sum = ADD32(sum,MULT16_16(MULT16_16_16(g[1],pitch_control),C[1]));
162
5.33M
   sum = ADD32(sum,MULT16_16(MULT16_16_16(g[2],pitch_control),C[2]));
163
5.33M
   sum = SUB32(sum,MULT16_16(MULT16_16_16(g[0],g[1]),C[3]));
164
5.33M
   sum = SUB32(sum,MULT16_16(MULT16_16_16(g[2],g[1]),C[4]));
165
5.33M
   sum = SUB32(sum,MULT16_16(MULT16_16_16(g[2],g[0]),C[5]));
166
5.33M
   sum = SUB32(sum,MULT16_16(MULT16_16_16(g[0],g[0]),C[6]));
167
5.33M
   sum = SUB32(sum,MULT16_16(MULT16_16_16(g[1],g[1]),C[7]));
168
5.33M
   sum = SUB32(sum,MULT16_16(MULT16_16_16(g[2],g[2]),C[8]));
169
5.33M
   return sum;
170
5.33M
}
ltp.c:compute_pitch_error
Line
Count
Source
158
5.33M
{
159
5.33M
   spx_word32_t sum = 0;
160
5.33M
   sum = ADD32(sum,MULT16_16(MULT16_16_16(g[0],pitch_control),C[0]));
161
5.33M
   sum = ADD32(sum,MULT16_16(MULT16_16_16(g[1],pitch_control),C[1]));
162
5.33M
   sum = ADD32(sum,MULT16_16(MULT16_16_16(g[2],pitch_control),C[2]));
163
5.33M
   sum = SUB32(sum,MULT16_16(MULT16_16_16(g[0],g[1]),C[3]));
164
5.33M
   sum = SUB32(sum,MULT16_16(MULT16_16_16(g[2],g[1]),C[4]));
165
5.33M
   sum = SUB32(sum,MULT16_16(MULT16_16_16(g[2],g[0]),C[5]));
166
5.33M
   sum = SUB32(sum,MULT16_16(MULT16_16_16(g[0],g[0]),C[6]));
167
5.33M
   sum = SUB32(sum,MULT16_16(MULT16_16_16(g[1],g[1]),C[7]));
168
5.33M
   sum = SUB32(sum,MULT16_16(MULT16_16_16(g[2],g[2]),C[8]));
169
5.33M
   return sum;
170
5.33M
}
171
#endif
172
173
#ifndef OVERRIDE_OPEN_LOOP_NBEST_PITCH
174
void open_loop_nbest_pitch(spx_word16_t *sw, int start, int end, int len, int *pitch, spx_word16_t *gain, int N, char *stack)
175
20.6k
{
176
20.6k
   int i,j,k;
177
20.6k
   VARDECL(spx_word32_t *best_score);
178
20.6k
   VARDECL(spx_word32_t *best_ener);
179
20.6k
   spx_word32_t e0;
180
20.6k
   VARDECL(spx_word32_t *corr);
181
#ifdef FIXED_POINT
182
   /* In fixed-point, we need only one (temporary) array of 32-bit values and two (corr16, ener16)
183
      arrays for (normalized) 16-bit values */
184
   VARDECL(spx_word16_t *corr16);
185
   VARDECL(spx_word16_t *ener16);
186
   spx_word32_t *energy;
187
   int cshift=0, eshift=0;
188
   int scaledown = 0;
189
4.54k
   ALLOC(corr16, end-start+1, spx_word16_t);
190
4.54k
   ALLOC(ener16, end-start+1, spx_word16_t);
191
4.54k
   ALLOC(corr, end-start+1, spx_word32_t);
192
   energy = corr;
193
#else
194
   /* In floating-point, we need to float arrays and no normalized copies */
195
   VARDECL(spx_word32_t *energy);
196
   spx_word16_t *corr16;
197
   spx_word16_t *ener16;
198
16.1k
   ALLOC(energy, end-start+2, spx_word32_t);
199
16.1k
   ALLOC(corr, end-start+1, spx_word32_t);
200
   corr16 = corr;
201
   ener16 = energy;
202
#endif
203
204
20.6k
   ALLOC(best_score, N, spx_word32_t);
205
20.6k
   ALLOC(best_ener, N, spx_word32_t);
206
140k
   for (i=0;i<N;i++)
207
119k
   {
208
119k
        best_score[i]=-1;
209
119k
        best_ener[i]=0;
210
119k
        pitch[i]=start;
211
119k
   }
212
213
#ifdef FIXED_POINT
214
530k
   for (i=-end;i<len;i++)
215
529k
   {
216
529k
      if (ABS16(sw[i])>16383)
217
3.14k
      {
218
3.14k
         scaledown=1;
219
3.14k
         break;
220
3.14k
      }
221
529k
   }
222
   /* If the weighted input is close to saturation, then we scale it down */
223
4.54k
   if (scaledown)
224
3.14k
   {
225
736k
      for (i=-end;i<len;i++)
226
733k
      {
227
733k
         sw[i]=SHR16(sw[i],1);
228
733k
      }
229
3.14k
   }
230
#endif
231
20.6k
   energy[0]=inner_prod(sw-start, sw-start, len);
232
20.6k
   e0=inner_prod(sw, sw, len);
233
2.49M
   for (i=start;i<end;i++)
234
2.47M
   {
235
      /* Update energy for next pitch*/
236
2.47M
      energy[i-start+1] = SUB32(ADD32(energy[i-start],SHR32(MULT16_16(sw[-i-1],sw[-i-1]),6)), SHR32(MULT16_16(sw[-i+len-1],sw[-i+len-1]),6));
237
2.47M
      if (energy[i-start+1] < 0)
238
7.25k
         energy[i-start+1] = 0;
239
2.47M
   }
240
241
#ifdef FIXED_POINT
242
   eshift = normalize16(energy, ener16, 32766, end-start+1);
243
#endif
244
245
   /* In fixed-point, this actually overrites the energy array (aliased to corr) */
246
20.6k
   pitch_xcorr(sw, sw-end, corr, len, end-start+1, stack);
247
248
#ifdef FIXED_POINT
249
   /* Normalize to 180 so we can square it and it still fits in 16 bits */
250
   cshift = normalize16(corr, corr16, 180, end-start+1);
251
   /* If we scaled weighted input down, we need to scale it up again (OK, so we've just lost the LSB, who cares?) */
252
4.54k
   if (scaledown)
253
3.14k
   {
254
736k
      for (i=-end;i<len;i++)
255
733k
      {
256
733k
         sw[i]=SHL16(sw[i],1);
257
733k
      }
258
3.14k
   }
259
#endif
260
261
   /* Search for the best pitch prediction gain */
262
2.52M
   for (i=start;i<=end;i++)
263
2.49M
   {
264
2.49M
      spx_word16_t tmp = MULT16_16_16(corr16[i-start],corr16[i-start]);
265
      /* Instead of dividing the tmp by the energy, we multiply on the other side */
266
2.49M
      if (MULT16_16(tmp,best_ener[N-1])>MULT16_16(best_score[N-1],ADD16(1,ener16[i-start])))
267
441k
      {
268
         /* We can safely put it last and then check */
269
441k
         best_score[N-1]=tmp;
270
441k
         best_ener[N-1]=ener16[i-start]+1;
271
441k
         pitch[N-1]=i;
272
         /* Check if it comes in front of others */
273
1.24M
         for (j=0;j<N-1;j++)
274
1.18M
         {
275
1.18M
            if (MULT16_16(tmp,best_ener[j])>MULT16_16(best_score[j],ADD16(1,ener16[i-start])))
276
380k
            {
277
1.99M
               for (k=N-1;k>j;k--)
278
1.61M
               {
279
1.61M
                  best_score[k]=best_score[k-1];
280
1.61M
                  best_ener[k]=best_ener[k-1];
281
1.61M
                  pitch[k]=pitch[k-1];
282
1.61M
               }
283
380k
               best_score[j]=tmp;
284
380k
               best_ener[j]=ener16[i-start]+1;
285
380k
               pitch[j]=i;
286
380k
               break;
287
380k
            }
288
1.18M
         }
289
441k
      }
290
2.49M
   }
291
292
   /* Compute open-loop gain if necessary */
293
20.6k
   if (gain)
294
9.67k
   {
295
67.7k
      for (j=0;j<N;j++)
296
58.0k
      {
297
58.0k
         spx_word16_t g;
298
58.0k
         i=pitch[j];
299
58.0k
         g = DIV32(SHL32(EXTEND32(corr16[i-start]),cshift), 10+SHR32(MULT16_16(spx_sqrt(e0),spx_sqrt(SHL32(EXTEND32(ener16[i-start]),eshift))),6));
300
         /* FIXME: g = max(g,corr/energy) */
301
58.0k
         if (g<0)
302
16.5k
            g = 0;
303
58.0k
         gain[j]=g;
304
58.0k
      }
305
9.67k
   }
306
307
308
20.6k
}
open_loop_nbest_pitch
Line
Count
Source
175
16.1k
{
176
16.1k
   int i,j,k;
177
16.1k
   VARDECL(spx_word32_t *best_score);
178
16.1k
   VARDECL(spx_word32_t *best_ener);
179
16.1k
   spx_word32_t e0;
180
16.1k
   VARDECL(spx_word32_t *corr);
181
#ifdef FIXED_POINT
182
   /* In fixed-point, we need only one (temporary) array of 32-bit values and two (corr16, ener16)
183
      arrays for (normalized) 16-bit values */
184
   VARDECL(spx_word16_t *corr16);
185
   VARDECL(spx_word16_t *ener16);
186
   spx_word32_t *energy;
187
   int cshift=0, eshift=0;
188
   int scaledown = 0;
189
   ALLOC(corr16, end-start+1, spx_word16_t);
190
   ALLOC(ener16, end-start+1, spx_word16_t);
191
   ALLOC(corr, end-start+1, spx_word32_t);
192
   energy = corr;
193
#else
194
   /* In floating-point, we need to float arrays and no normalized copies */
195
16.1k
   VARDECL(spx_word32_t *energy);
196
16.1k
   spx_word16_t *corr16;
197
16.1k
   spx_word16_t *ener16;
198
16.1k
   ALLOC(energy, end-start+2, spx_word32_t);
199
16.1k
   ALLOC(corr, end-start+1, spx_word32_t);
200
16.1k
   corr16 = corr;
201
16.1k
   ener16 = energy;
202
16.1k
#endif
203
204
16.1k
   ALLOC(best_score, N, spx_word32_t);
205
16.1k
   ALLOC(best_ener, N, spx_word32_t);
206
111k
   for (i=0;i<N;i++)
207
94.9k
   {
208
94.9k
        best_score[i]=-1;
209
94.9k
        best_ener[i]=0;
210
94.9k
        pitch[i]=start;
211
94.9k
   }
212
213
#ifdef FIXED_POINT
214
   for (i=-end;i<len;i++)
215
   {
216
      if (ABS16(sw[i])>16383)
217
      {
218
         scaledown=1;
219
         break;
220
      }
221
   }
222
   /* If the weighted input is close to saturation, then we scale it down */
223
   if (scaledown)
224
   {
225
      for (i=-end;i<len;i++)
226
      {
227
         sw[i]=SHR16(sw[i],1);
228
      }
229
   }
230
#endif
231
16.1k
   energy[0]=inner_prod(sw-start, sw-start, len);
232
16.1k
   e0=inner_prod(sw, sw, len);
233
1.94M
   for (i=start;i<end;i++)
234
1.93M
   {
235
      /* Update energy for next pitch*/
236
1.93M
      energy[i-start+1] = SUB32(ADD32(energy[i-start],SHR32(MULT16_16(sw[-i-1],sw[-i-1]),6)), SHR32(MULT16_16(sw[-i+len-1],sw[-i+len-1]),6));
237
1.93M
      if (energy[i-start+1] < 0)
238
7.25k
         energy[i-start+1] = 0;
239
1.93M
   }
240
241
#ifdef FIXED_POINT
242
   eshift = normalize16(energy, ener16, 32766, end-start+1);
243
#endif
244
245
   /* In fixed-point, this actually overrites the energy array (aliased to corr) */
246
16.1k
   pitch_xcorr(sw, sw-end, corr, len, end-start+1, stack);
247
248
#ifdef FIXED_POINT
249
   /* Normalize to 180 so we can square it and it still fits in 16 bits */
250
   cshift = normalize16(corr, corr16, 180, end-start+1);
251
   /* If we scaled weighted input down, we need to scale it up again (OK, so we've just lost the LSB, who cares?) */
252
   if (scaledown)
253
   {
254
      for (i=-end;i<len;i++)
255
      {
256
         sw[i]=SHL16(sw[i],1);
257
      }
258
   }
259
#endif
260
261
   /* Search for the best pitch prediction gain */
262
1.96M
   for (i=start;i<=end;i++)
263
1.94M
   {
264
1.94M
      spx_word16_t tmp = MULT16_16_16(corr16[i-start],corr16[i-start]);
265
      /* Instead of dividing the tmp by the energy, we multiply on the other side */
266
1.94M
      if (MULT16_16(tmp,best_ener[N-1])>MULT16_16(best_score[N-1],ADD16(1,ener16[i-start])))
267
348k
      {
268
         /* We can safely put it last and then check */
269
348k
         best_score[N-1]=tmp;
270
348k
         best_ener[N-1]=ener16[i-start]+1;
271
348k
         pitch[N-1]=i;
272
         /* Check if it comes in front of others */
273
948k
         for (j=0;j<N-1;j++)
274
902k
         {
275
902k
            if (MULT16_16(tmp,best_ener[j])>MULT16_16(best_score[j],ADD16(1,ener16[i-start])))
276
302k
            {
277
1.62M
               for (k=N-1;k>j;k--)
278
1.32M
               {
279
1.32M
                  best_score[k]=best_score[k-1];
280
1.32M
                  best_ener[k]=best_ener[k-1];
281
1.32M
                  pitch[k]=pitch[k-1];
282
1.32M
               }
283
302k
               best_score[j]=tmp;
284
302k
               best_ener[j]=ener16[i-start]+1;
285
302k
               pitch[j]=i;
286
302k
               break;
287
302k
            }
288
902k
         }
289
348k
      }
290
1.94M
   }
291
292
   /* Compute open-loop gain if necessary */
293
16.1k
   if (gain)
294
7.64k
   {
295
53.5k
      for (j=0;j<N;j++)
296
45.8k
      {
297
45.8k
         spx_word16_t g;
298
45.8k
         i=pitch[j];
299
45.8k
         g = DIV32(SHL32(EXTEND32(corr16[i-start]),cshift), 10+SHR32(MULT16_16(spx_sqrt(e0),spx_sqrt(SHL32(EXTEND32(ener16[i-start]),eshift))),6));
300
         /* FIXME: g = max(g,corr/energy) */
301
45.8k
         if (g<0)
302
13.3k
            g = 0;
303
45.8k
         gain[j]=g;
304
45.8k
      }
305
7.64k
   }
306
307
308
16.1k
}
open_loop_nbest_pitch
Line
Count
Source
175
4.54k
{
176
4.54k
   int i,j,k;
177
4.54k
   VARDECL(spx_word32_t *best_score);
178
4.54k
   VARDECL(spx_word32_t *best_ener);
179
4.54k
   spx_word32_t e0;
180
4.54k
   VARDECL(spx_word32_t *corr);
181
4.54k
#ifdef FIXED_POINT
182
   /* In fixed-point, we need only one (temporary) array of 32-bit values and two (corr16, ener16)
183
      arrays for (normalized) 16-bit values */
184
4.54k
   VARDECL(spx_word16_t *corr16);
185
4.54k
   VARDECL(spx_word16_t *ener16);
186
4.54k
   spx_word32_t *energy;
187
4.54k
   int cshift=0, eshift=0;
188
4.54k
   int scaledown = 0;
189
4.54k
   ALLOC(corr16, end-start+1, spx_word16_t);
190
4.54k
   ALLOC(ener16, end-start+1, spx_word16_t);
191
4.54k
   ALLOC(corr, end-start+1, spx_word32_t);
192
4.54k
   energy = corr;
193
#else
194
   /* In floating-point, we need to float arrays and no normalized copies */
195
   VARDECL(spx_word32_t *energy);
196
   spx_word16_t *corr16;
197
   spx_word16_t *ener16;
198
   ALLOC(energy, end-start+2, spx_word32_t);
199
   ALLOC(corr, end-start+1, spx_word32_t);
200
   corr16 = corr;
201
   ener16 = energy;
202
#endif
203
204
4.54k
   ALLOC(best_score, N, spx_word32_t);
205
4.54k
   ALLOC(best_ener, N, spx_word32_t);
206
29.5k
   for (i=0;i<N;i++)
207
25.0k
   {
208
25.0k
        best_score[i]=-1;
209
25.0k
        best_ener[i]=0;
210
25.0k
        pitch[i]=start;
211
25.0k
   }
212
213
4.54k
#ifdef FIXED_POINT
214
530k
   for (i=-end;i<len;i++)
215
529k
   {
216
529k
      if (ABS16(sw[i])>16383)
217
3.14k
      {
218
3.14k
         scaledown=1;
219
3.14k
         break;
220
3.14k
      }
221
529k
   }
222
   /* If the weighted input is close to saturation, then we scale it down */
223
4.54k
   if (scaledown)
224
3.14k
   {
225
736k
      for (i=-end;i<len;i++)
226
733k
      {
227
733k
         sw[i]=SHR16(sw[i],1);
228
733k
      }
229
3.14k
   }
230
4.54k
#endif
231
4.54k
   energy[0]=inner_prod(sw-start, sw-start, len);
232
4.54k
   e0=inner_prod(sw, sw, len);
233
551k
   for (i=start;i<end;i++)
234
546k
   {
235
      /* Update energy for next pitch*/
236
546k
      energy[i-start+1] = SUB32(ADD32(energy[i-start],SHR32(MULT16_16(sw[-i-1],sw[-i-1]),6)), SHR32(MULT16_16(sw[-i+len-1],sw[-i+len-1]),6));
237
546k
      if (energy[i-start+1] < 0)
238
0
         energy[i-start+1] = 0;
239
546k
   }
240
241
4.54k
#ifdef FIXED_POINT
242
4.54k
   eshift = normalize16(energy, ener16, 32766, end-start+1);
243
4.54k
#endif
244
245
   /* In fixed-point, this actually overrites the energy array (aliased to corr) */
246
4.54k
   pitch_xcorr(sw, sw-end, corr, len, end-start+1, stack);
247
248
4.54k
#ifdef FIXED_POINT
249
   /* Normalize to 180 so we can square it and it still fits in 16 bits */
250
4.54k
   cshift = normalize16(corr, corr16, 180, end-start+1);
251
   /* If we scaled weighted input down, we need to scale it up again (OK, so we've just lost the LSB, who cares?) */
252
4.54k
   if (scaledown)
253
3.14k
   {
254
736k
      for (i=-end;i<len;i++)
255
733k
      {
256
733k
         sw[i]=SHL16(sw[i],1);
257
733k
      }
258
3.14k
   }
259
4.54k
#endif
260
261
   /* Search for the best pitch prediction gain */
262
555k
   for (i=start;i<=end;i++)
263
551k
   {
264
551k
      spx_word16_t tmp = MULT16_16_16(corr16[i-start],corr16[i-start]);
265
      /* Instead of dividing the tmp by the energy, we multiply on the other side */
266
551k
      if (MULT16_16(tmp,best_ener[N-1])>MULT16_16(best_score[N-1],ADD16(1,ener16[i-start])))
267
92.8k
      {
268
         /* We can safely put it last and then check */
269
92.8k
         best_score[N-1]=tmp;
270
92.8k
         best_ener[N-1]=ener16[i-start]+1;
271
92.8k
         pitch[N-1]=i;
272
         /* Check if it comes in front of others */
273
294k
         for (j=0;j<N-1;j++)
274
279k
         {
275
279k
            if (MULT16_16(tmp,best_ener[j])>MULT16_16(best_score[j],ADD16(1,ener16[i-start])))
276
78.1k
            {
277
366k
               for (k=N-1;k>j;k--)
278
288k
               {
279
288k
                  best_score[k]=best_score[k-1];
280
288k
                  best_ener[k]=best_ener[k-1];
281
288k
                  pitch[k]=pitch[k-1];
282
288k
               }
283
78.1k
               best_score[j]=tmp;
284
78.1k
               best_ener[j]=ener16[i-start]+1;
285
78.1k
               pitch[j]=i;
286
78.1k
               break;
287
78.1k
            }
288
279k
         }
289
92.8k
      }
290
551k
   }
291
292
   /* Compute open-loop gain if necessary */
293
4.54k
   if (gain)
294
2.03k
   {
295
14.2k
      for (j=0;j<N;j++)
296
12.1k
      {
297
12.1k
         spx_word16_t g;
298
12.1k
         i=pitch[j];
299
12.1k
         g = DIV32(SHL32(EXTEND32(corr16[i-start]),cshift), 10+SHR32(MULT16_16(spx_sqrt(e0),spx_sqrt(SHL32(EXTEND32(ener16[i-start]),eshift))),6));
300
         /* FIXME: g = max(g,corr/energy) */
301
12.1k
         if (g<0)
302
3.20k
            g = 0;
303
12.1k
         gain[j]=g;
304
12.1k
      }
305
2.03k
   }
306
307
308
4.54k
}
309
#endif
310
311
#ifndef OVERRIDE_PITCH_GAIN_SEARCH_3TAP_VQ
312
static int pitch_gain_search_3tap_vq(
313
  const signed char *gain_cdbk,
314
  int                gain_cdbk_size,
315
  spx_word16_t      *C16,
316
  spx_word16_t       max_gain
317
)
318
127k
{
319
127k
  const signed char *ptr=gain_cdbk;
320
127k
  int                best_cdbk=0;
321
127k
  spx_word32_t       best_sum=-VERY_LARGE32;
322
127k
  spx_word32_t       sum=0;
323
127k
  spx_word16_t       g[3];
324
127k
  spx_word16_t       pitch_control=64;
325
127k
  spx_word16_t       gain_sum;
326
127k
  int                i;
327
328
10.8M
  for (i=0;i<gain_cdbk_size;i++) {
329
330
10.6M
    ptr = gain_cdbk+4*i;
331
10.6M
    g[0]=ADD16((spx_word16_t)ptr[0],32);
332
10.6M
    g[1]=ADD16((spx_word16_t)ptr[1],32);
333
10.6M
    g[2]=ADD16((spx_word16_t)ptr[2],32);
334
10.6M
    gain_sum = (spx_word16_t)ptr[3];
335
336
10.6M
    sum = compute_pitch_error(C16, g, pitch_control);
337
338
10.6M
    if (sum>best_sum && gain_sum<=max_gain) {
339
351k
      best_sum=sum;
340
351k
      best_cdbk=i;
341
351k
    }
342
10.6M
  }
343
344
127k
  return best_cdbk;
345
127k
}
ltp.c:pitch_gain_search_3tap_vq
Line
Count
Source
318
63.8k
{
319
63.8k
  const signed char *ptr=gain_cdbk;
320
63.8k
  int                best_cdbk=0;
321
63.8k
  spx_word32_t       best_sum=-VERY_LARGE32;
322
63.8k
  spx_word32_t       sum=0;
323
63.8k
  spx_word16_t       g[3];
324
63.8k
  spx_word16_t       pitch_control=64;
325
63.8k
  spx_word16_t       gain_sum;
326
63.8k
  int                i;
327
328
5.40M
  for (i=0;i<gain_cdbk_size;i++) {
329
330
5.33M
    ptr = gain_cdbk+4*i;
331
5.33M
    g[0]=ADD16((spx_word16_t)ptr[0],32);
332
5.33M
    g[1]=ADD16((spx_word16_t)ptr[1],32);
333
5.33M
    g[2]=ADD16((spx_word16_t)ptr[2],32);
334
5.33M
    gain_sum = (spx_word16_t)ptr[3];
335
336
5.33M
    sum = compute_pitch_error(C16, g, pitch_control);
337
338
5.33M
    if (sum>best_sum && gain_sum<=max_gain) {
339
175k
      best_sum=sum;
340
175k
      best_cdbk=i;
341
175k
    }
342
5.33M
  }
343
344
63.8k
  return best_cdbk;
345
63.8k
}
ltp.c:pitch_gain_search_3tap_vq
Line
Count
Source
318
63.8k
{
319
63.8k
  const signed char *ptr=gain_cdbk;
320
63.8k
  int                best_cdbk=0;
321
63.8k
  spx_word32_t       best_sum=-VERY_LARGE32;
322
63.8k
  spx_word32_t       sum=0;
323
63.8k
  spx_word16_t       g[3];
324
63.8k
  spx_word16_t       pitch_control=64;
325
63.8k
  spx_word16_t       gain_sum;
326
63.8k
  int                i;
327
328
5.40M
  for (i=0;i<gain_cdbk_size;i++) {
329
330
5.33M
    ptr = gain_cdbk+4*i;
331
5.33M
    g[0]=ADD16((spx_word16_t)ptr[0],32);
332
5.33M
    g[1]=ADD16((spx_word16_t)ptr[1],32);
333
5.33M
    g[2]=ADD16((spx_word16_t)ptr[2],32);
334
5.33M
    gain_sum = (spx_word16_t)ptr[3];
335
336
5.33M
    sum = compute_pitch_error(C16, g, pitch_control);
337
338
5.33M
    if (sum>best_sum && gain_sum<=max_gain) {
339
175k
      best_sum=sum;
340
175k
      best_cdbk=i;
341
175k
    }
342
5.33M
  }
343
344
63.8k
  return best_cdbk;
345
63.8k
}
346
#endif
347
348
/** Finds the best quantized 3-tap pitch predictor by analysis by synthesis */
349
static spx_word32_t pitch_gain_search_3tap(
350
const spx_word16_t target[],       /* Target vector */
351
const spx_coef_t ak[],          /* LPCs for this subframe */
352
const spx_coef_t awk1[],        /* Weighted LPCs #1 for this subframe */
353
const spx_coef_t awk2[],        /* Weighted LPCs #2 for this subframe */
354
spx_sig_t exc[],                /* Excitation */
355
const signed char *gain_cdbk,
356
int gain_cdbk_size,
357
int   pitch,                    /* Pitch value */
358
int   p,                        /* Number of LPC coeffs */
359
int   nsf,                      /* Number of samples in subframe */
360
SpeexBits *bits,
361
char *stack,
362
const spx_word16_t *exc2,
363
const spx_word16_t *r,
364
spx_word16_t *new_target,
365
int  *cdbk_index,
366
int plc_tuning,
367
spx_word32_t cumul_gain,
368
int scaledown
369
)
370
63.8k
{
371
63.8k
   int i,j;
372
63.8k
   VARDECL(spx_word16_t *tmp1);
373
63.8k
   VARDECL(spx_word16_t *e);
374
63.8k
   spx_word16_t *x[3];
375
63.8k
   spx_word32_t corr[3];
376
63.8k
   spx_word32_t A[3][3];
377
63.8k
   spx_word16_t gain[3];
378
63.8k
   spx_word32_t err;
379
63.8k
   spx_word16_t max_gain=128;
380
63.8k
   int          best_cdbk=0;
381
382
63.8k
   ALLOC(tmp1, 3*nsf, spx_word16_t);
383
63.8k
   ALLOC(e, nsf, spx_word16_t);
384
385
63.8k
   if (cumul_gain > 262144)
386
968
      max_gain = 31;
387
388
63.8k
   x[0]=tmp1;
389
63.8k
   x[1]=tmp1+nsf;
390
63.8k
   x[2]=tmp1+2*nsf;
391
392
2.61M
   for (j=0;j<nsf;j++)
393
2.55M
      new_target[j] = target[j];
394
395
63.8k
   {
396
63.8k
      int bound;
397
63.8k
      VARDECL(spx_mem_t *mm);
398
63.8k
      int pp=pitch-1;
399
63.8k
      ALLOC(mm, p, spx_mem_t);
400
63.8k
      bound = nsf;
401
63.8k
      if (nsf-pp>0)
402
28.8k
         bound = pp;
403
2.15M
      for (j=0;j<bound;j++)
404
2.09M
         e[j]=exc2[j-pp];
405
63.8k
      bound = nsf;
406
63.8k
      if (nsf-pp-pitch>0)
407
9.59k
         bound = pp+pitch;
408
479k
      for (;j<bound;j++)
409
415k
         e[j]=exc2[j-pp-pitch];
410
107k
      for (;j<nsf;j++)
411
43.2k
         e[j]=0;
412
#ifdef FIXED_POINT
413
      /* Scale target and excitation down if needed (avoiding overflow) */
414
13.5k
      if (scaledown)
415
9.45k
      {
416
387k
         for (j=0;j<nsf;j++)
417
378k
            e[j] = SHR16(e[j],1);
418
387k
         for (j=0;j<nsf;j++)
419
378k
            new_target[j] = SHR16(new_target[j],1);
420
9.45k
      }
421
#endif
422
702k
      for (j=0;j<p;j++)
423
638k
         mm[j] = 0;
424
63.8k
      iir_mem16(e, ak, e, nsf, p, mm, stack);
425
702k
      for (j=0;j<p;j++)
426
638k
         mm[j] = 0;
427
63.8k
      filter10(e, awk1, awk2, e, nsf, mm, stack);
428
2.61M
      for (j=0;j<nsf;j++)
429
2.55M
         x[2][j] = e[j];
430
63.8k
   }
431
191k
   for (i=1;i>=0;i--)
432
127k
   {
433
127k
      spx_word16_t e0=exc2[-pitch-1+i];
434
#ifdef FIXED_POINT
435
      /* Scale excitation down if needed (avoiding overflow) */
436
27.1k
      if (scaledown)
437
18.9k
         e0 = SHR16(e0,1);
438
#endif
439
127k
      x[i][0]=MULT16_16_Q14(r[0], e0);
440
5.10M
      for (j=0;j<nsf-1;j++)
441
4.97M
         x[i][j+1]=ADD32(x[i+1][j],MULT16_16_P14(r[j+1], e0));
442
127k
   }
443
444
255k
   for (i=0;i<3;i++)
445
191k
      corr[i]=inner_prod(x[i],new_target,nsf);
446
255k
   for (i=0;i<3;i++)
447
574k
      for (j=0;j<=i;j++)
448
382k
         A[i][j]=A[j][i]=inner_prod(x[i],x[j],nsf);
449
450
63.8k
   {
451
63.8k
      spx_word32_t C[9];
452
#ifdef FIXED_POINT
453
      spx_word16_t C16[9];
454
#else
455
      spx_word16_t *C16=C;
456
#endif
457
63.8k
      C[0]=corr[2];
458
63.8k
      C[1]=corr[1];
459
63.8k
      C[2]=corr[0];
460
63.8k
      C[3]=A[1][2];
461
63.8k
      C[4]=A[0][1];
462
63.8k
      C[5]=A[0][2];
463
63.8k
      C[6]=A[2][2];
464
63.8k
      C[7]=A[1][1];
465
63.8k
      C[8]=A[0][0];
466
467
      /*plc_tuning *= 2;*/
468
63.8k
      if (plc_tuning<2)
469
0
         plc_tuning=2;
470
63.8k
      if (plc_tuning>30)
471
0
         plc_tuning=30;
472
#ifdef FIXED_POINT
473
13.5k
      C[0] = SHL32(C[0],1);
474
13.5k
      C[1] = SHL32(C[1],1);
475
13.5k
      C[2] = SHL32(C[2],1);
476
13.5k
      C[3] = SHL32(C[3],1);
477
13.5k
      C[4] = SHL32(C[4],1);
478
13.5k
      C[5] = SHL32(C[5],1);
479
13.5k
      C[6] = MAC16_32_Q15(C[6],MULT16_16_16(plc_tuning,655),C[6]);
480
13.5k
      C[7] = MAC16_32_Q15(C[7],MULT16_16_16(plc_tuning,655),C[7]);
481
13.5k
      C[8] = MAC16_32_Q15(C[8],MULT16_16_16(plc_tuning,655),C[8]);
482
      normalize16(C, C16, 32767, 9);
483
#else
484
      C[6]*=.5*(1+.02*plc_tuning);
485
      C[7]*=.5*(1+.02*plc_tuning);
486
      C[8]*=.5*(1+.02*plc_tuning);
487
#endif
488
489
63.8k
      best_cdbk = pitch_gain_search_3tap_vq(gain_cdbk, gain_cdbk_size, C16, max_gain);
490
491
#ifdef FIXED_POINT
492
13.5k
      gain[0] = ADD16(32,(spx_word16_t)gain_cdbk[best_cdbk*4]);
493
13.5k
      gain[1] = ADD16(32,(spx_word16_t)gain_cdbk[best_cdbk*4+1]);
494
13.5k
      gain[2] = ADD16(32,(spx_word16_t)gain_cdbk[best_cdbk*4+2]);
495
      /*printf ("%d %d %d %d\n",gain[0],gain[1],gain[2], best_cdbk);*/
496
#else
497
      gain[0] = 0.015625*gain_cdbk[best_cdbk*4]  + .5;
498
      gain[1] = 0.015625*gain_cdbk[best_cdbk*4+1]+ .5;
499
      gain[2] = 0.015625*gain_cdbk[best_cdbk*4+2]+ .5;
500
#endif
501
63.8k
      *cdbk_index=best_cdbk;
502
63.8k
   }
503
504
63.8k
   SPEEX_MEMSET(exc, 0, nsf);
505
255k
   for (i=0;i<3;i++)
506
191k
   {
507
191k
      int j;
508
191k
      int tmp1, tmp3;
509
191k
      int pp=pitch+1-i;
510
191k
      tmp1=nsf;
511
191k
      if (tmp1>pp)
512
84.7k
         tmp1=pp;
513
6.56M
      for (j=0;j<tmp1;j++)
514
6.36M
         exc[j]=MAC16_16(exc[j],SHL16(gain[2-i],7),exc2[j-pp]);
515
191k
      tmp3=nsf;
516
191k
      if (tmp3>pp+pitch)
517
25.1k
         tmp3=pp+pitch;
518
1.37M
      for (j=tmp1;j<tmp3;j++)
519
1.18M
         exc[j]=MAC16_16(exc[j],SHL16(gain[2-i],7),exc2[j-pp-pitch]);
520
191k
   }
521
2.61M
   for (i=0;i<nsf;i++)
522
2.55M
   {
523
2.55M
      spx_word32_t tmp = ADD32(ADD32(MULT16_16(gain[0],x[2][i]),MULT16_16(gain[1],x[1][i])),
524
2.55M
                            MULT16_16(gain[2],x[0][i]));
525
2.55M
      new_target[i] = SUB16(new_target[i], EXTRACT16(PSHR32(tmp,6)));
526
2.55M
   }
527
63.8k
   err = inner_prod(new_target, new_target, nsf);
528
529
63.8k
   return err;
530
63.8k
}
ltp.c:pitch_gain_search_3tap
Line
Count
Source
370
50.2k
{
371
50.2k
   int i,j;
372
50.2k
   VARDECL(spx_word16_t *tmp1);
373
50.2k
   VARDECL(spx_word16_t *e);
374
50.2k
   spx_word16_t *x[3];
375
50.2k
   spx_word32_t corr[3];
376
50.2k
   spx_word32_t A[3][3];
377
50.2k
   spx_word16_t gain[3];
378
50.2k
   spx_word32_t err;
379
50.2k
   spx_word16_t max_gain=128;
380
50.2k
   int          best_cdbk=0;
381
382
50.2k
   ALLOC(tmp1, 3*nsf, spx_word16_t);
383
50.2k
   ALLOC(e, nsf, spx_word16_t);
384
385
50.2k
   if (cumul_gain > 262144)
386
567
      max_gain = 31;
387
388
50.2k
   x[0]=tmp1;
389
50.2k
   x[1]=tmp1+nsf;
390
50.2k
   x[2]=tmp1+2*nsf;
391
392
2.05M
   for (j=0;j<nsf;j++)
393
2.00M
      new_target[j] = target[j];
394
395
50.2k
   {
396
50.2k
      int bound;
397
50.2k
      VARDECL(spx_mem_t *mm);
398
50.2k
      int pp=pitch-1;
399
50.2k
      ALLOC(mm, p, spx_mem_t);
400
50.2k
      bound = nsf;
401
50.2k
      if (nsf-pp>0)
402
24.1k
         bound = pp;
403
1.66M
      for (j=0;j<bound;j++)
404
1.61M
         e[j]=exc2[j-pp];
405
50.2k
      bound = nsf;
406
50.2k
      if (nsf-pp-pitch>0)
407
8.15k
         bound = pp+pitch;
408
405k
      for (;j<bound;j++)
409
354k
         e[j]=exc2[j-pp-pitch];
410
87.3k
      for (;j<nsf;j++)
411
37.1k
         e[j]=0;
412
#ifdef FIXED_POINT
413
      /* Scale target and excitation down if needed (avoiding overflow) */
414
      if (scaledown)
415
      {
416
         for (j=0;j<nsf;j++)
417
            e[j] = SHR16(e[j],1);
418
         for (j=0;j<nsf;j++)
419
            new_target[j] = SHR16(new_target[j],1);
420
      }
421
#endif
422
552k
      for (j=0;j<p;j++)
423
502k
         mm[j] = 0;
424
50.2k
      iir_mem16(e, ak, e, nsf, p, mm, stack);
425
552k
      for (j=0;j<p;j++)
426
502k
         mm[j] = 0;
427
50.2k
      filter10(e, awk1, awk2, e, nsf, mm, stack);
428
2.05M
      for (j=0;j<nsf;j++)
429
2.00M
         x[2][j] = e[j];
430
50.2k
   }
431
150k
   for (i=1;i>=0;i--)
432
100k
   {
433
100k
      spx_word16_t e0=exc2[-pitch-1+i];
434
#ifdef FIXED_POINT
435
      /* Scale excitation down if needed (avoiding overflow) */
436
      if (scaledown)
437
         e0 = SHR16(e0,1);
438
#endif
439
100k
      x[i][0]=MULT16_16_Q14(r[0], e0);
440
4.01M
      for (j=0;j<nsf-1;j++)
441
3.91M
         x[i][j+1]=ADD32(x[i+1][j],MULT16_16_P14(r[j+1], e0));
442
100k
   }
443
444
200k
   for (i=0;i<3;i++)
445
150k
      corr[i]=inner_prod(x[i],new_target,nsf);
446
200k
   for (i=0;i<3;i++)
447
452k
      for (j=0;j<=i;j++)
448
301k
         A[i][j]=A[j][i]=inner_prod(x[i],x[j],nsf);
449
450
50.2k
   {
451
50.2k
      spx_word32_t C[9];
452
#ifdef FIXED_POINT
453
      spx_word16_t C16[9];
454
#else
455
50.2k
      spx_word16_t *C16=C;
456
50.2k
#endif
457
50.2k
      C[0]=corr[2];
458
50.2k
      C[1]=corr[1];
459
50.2k
      C[2]=corr[0];
460
50.2k
      C[3]=A[1][2];
461
50.2k
      C[4]=A[0][1];
462
50.2k
      C[5]=A[0][2];
463
50.2k
      C[6]=A[2][2];
464
50.2k
      C[7]=A[1][1];
465
50.2k
      C[8]=A[0][0];
466
467
      /*plc_tuning *= 2;*/
468
50.2k
      if (plc_tuning<2)
469
0
         plc_tuning=2;
470
50.2k
      if (plc_tuning>30)
471
0
         plc_tuning=30;
472
#ifdef FIXED_POINT
473
      C[0] = SHL32(C[0],1);
474
      C[1] = SHL32(C[1],1);
475
      C[2] = SHL32(C[2],1);
476
      C[3] = SHL32(C[3],1);
477
      C[4] = SHL32(C[4],1);
478
      C[5] = SHL32(C[5],1);
479
      C[6] = MAC16_32_Q15(C[6],MULT16_16_16(plc_tuning,655),C[6]);
480
      C[7] = MAC16_32_Q15(C[7],MULT16_16_16(plc_tuning,655),C[7]);
481
      C[8] = MAC16_32_Q15(C[8],MULT16_16_16(plc_tuning,655),C[8]);
482
      normalize16(C, C16, 32767, 9);
483
#else
484
50.2k
      C[6]*=.5*(1+.02*plc_tuning);
485
50.2k
      C[7]*=.5*(1+.02*plc_tuning);
486
50.2k
      C[8]*=.5*(1+.02*plc_tuning);
487
50.2k
#endif
488
489
50.2k
      best_cdbk = pitch_gain_search_3tap_vq(gain_cdbk, gain_cdbk_size, C16, max_gain);
490
491
#ifdef FIXED_POINT
492
      gain[0] = ADD16(32,(spx_word16_t)gain_cdbk[best_cdbk*4]);
493
      gain[1] = ADD16(32,(spx_word16_t)gain_cdbk[best_cdbk*4+1]);
494
      gain[2] = ADD16(32,(spx_word16_t)gain_cdbk[best_cdbk*4+2]);
495
      /*printf ("%d %d %d %d\n",gain[0],gain[1],gain[2], best_cdbk);*/
496
#else
497
50.2k
      gain[0] = 0.015625*gain_cdbk[best_cdbk*4]  + .5;
498
50.2k
      gain[1] = 0.015625*gain_cdbk[best_cdbk*4+1]+ .5;
499
50.2k
      gain[2] = 0.015625*gain_cdbk[best_cdbk*4+2]+ .5;
500
50.2k
#endif
501
50.2k
      *cdbk_index=best_cdbk;
502
50.2k
   }
503
504
50.2k
   SPEEX_MEMSET(exc, 0, nsf);
505
200k
   for (i=0;i<3;i++)
506
150k
   {
507
150k
      int j;
508
150k
      int tmp1, tmp3;
509
150k
      int pp=pitch+1-i;
510
150k
      tmp1=nsf;
511
150k
      if (tmp1>pp)
512
71.3k
         tmp1=pp;
513
5.07M
      for (j=0;j<tmp1;j++)
514
4.92M
         exc[j]=MAC16_16(exc[j],SHL16(gain[2-i],7),exc2[j-pp]);
515
150k
      tmp3=nsf;
516
150k
      if (tmp3>pp+pitch)
517
21.5k
         tmp3=pp+pitch;
518
1.16M
      for (j=tmp1;j<tmp3;j++)
519
1.01M
         exc[j]=MAC16_16(exc[j],SHL16(gain[2-i],7),exc2[j-pp-pitch]);
520
150k
   }
521
2.05M
   for (i=0;i<nsf;i++)
522
2.00M
   {
523
2.00M
      spx_word32_t tmp = ADD32(ADD32(MULT16_16(gain[0],x[2][i]),MULT16_16(gain[1],x[1][i])),
524
2.00M
                            MULT16_16(gain[2],x[0][i]));
525
2.00M
      new_target[i] = SUB16(new_target[i], EXTRACT16(PSHR32(tmp,6)));
526
2.00M
   }
527
50.2k
   err = inner_prod(new_target, new_target, nsf);
528
529
50.2k
   return err;
530
50.2k
}
ltp.c:pitch_gain_search_3tap
Line
Count
Source
370
13.5k
{
371
13.5k
   int i,j;
372
13.5k
   VARDECL(spx_word16_t *tmp1);
373
13.5k
   VARDECL(spx_word16_t *e);
374
13.5k
   spx_word16_t *x[3];
375
13.5k
   spx_word32_t corr[3];
376
13.5k
   spx_word32_t A[3][3];
377
13.5k
   spx_word16_t gain[3];
378
13.5k
   spx_word32_t err;
379
13.5k
   spx_word16_t max_gain=128;
380
13.5k
   int          best_cdbk=0;
381
382
13.5k
   ALLOC(tmp1, 3*nsf, spx_word16_t);
383
13.5k
   ALLOC(e, nsf, spx_word16_t);
384
385
13.5k
   if (cumul_gain > 262144)
386
401
      max_gain = 31;
387
388
13.5k
   x[0]=tmp1;
389
13.5k
   x[1]=tmp1+nsf;
390
13.5k
   x[2]=tmp1+2*nsf;
391
392
557k
   for (j=0;j<nsf;j++)
393
543k
      new_target[j] = target[j];
394
395
13.5k
   {
396
13.5k
      int bound;
397
13.5k
      VARDECL(spx_mem_t *mm);
398
13.5k
      int pp=pitch-1;
399
13.5k
      ALLOC(mm, p, spx_mem_t);
400
13.5k
      bound = nsf;
401
13.5k
      if (nsf-pp>0)
402
4.69k
         bound = pp;
403
491k
      for (j=0;j<bound;j++)
404
477k
         e[j]=exc2[j-pp];
405
13.5k
      bound = nsf;
406
13.5k
      if (nsf-pp-pitch>0)
407
1.43k
         bound = pp+pitch;
408
73.8k
      for (;j<bound;j++)
409
60.2k
         e[j]=exc2[j-pp-pitch];
410
19.7k
      for (;j<nsf;j++)
411
6.17k
         e[j]=0;
412
13.5k
#ifdef FIXED_POINT
413
      /* Scale target and excitation down if needed (avoiding overflow) */
414
13.5k
      if (scaledown)
415
9.45k
      {
416
387k
         for (j=0;j<nsf;j++)
417
378k
            e[j] = SHR16(e[j],1);
418
387k
         for (j=0;j<nsf;j++)
419
378k
            new_target[j] = SHR16(new_target[j],1);
420
9.45k
      }
421
13.5k
#endif
422
149k
      for (j=0;j<p;j++)
423
135k
         mm[j] = 0;
424
13.5k
      iir_mem16(e, ak, e, nsf, p, mm, stack);
425
149k
      for (j=0;j<p;j++)
426
135k
         mm[j] = 0;
427
13.5k
      filter10(e, awk1, awk2, e, nsf, mm, stack);
428
557k
      for (j=0;j<nsf;j++)
429
543k
         x[2][j] = e[j];
430
13.5k
   }
431
40.7k
   for (i=1;i>=0;i--)
432
27.1k
   {
433
27.1k
      spx_word16_t e0=exc2[-pitch-1+i];
434
27.1k
#ifdef FIXED_POINT
435
      /* Scale excitation down if needed (avoiding overflow) */
436
27.1k
      if (scaledown)
437
18.9k
         e0 = SHR16(e0,1);
438
27.1k
#endif
439
27.1k
      x[i][0]=MULT16_16_Q14(r[0], e0);
440
1.08M
      for (j=0;j<nsf-1;j++)
441
1.06M
         x[i][j+1]=ADD32(x[i+1][j],MULT16_16_P14(r[j+1], e0));
442
27.1k
   }
443
444
54.3k
   for (i=0;i<3;i++)
445
40.7k
      corr[i]=inner_prod(x[i],new_target,nsf);
446
54.3k
   for (i=0;i<3;i++)
447
122k
      for (j=0;j<=i;j++)
448
81.5k
         A[i][j]=A[j][i]=inner_prod(x[i],x[j],nsf);
449
450
13.5k
   {
451
13.5k
      spx_word32_t C[9];
452
13.5k
#ifdef FIXED_POINT
453
13.5k
      spx_word16_t C16[9];
454
#else
455
      spx_word16_t *C16=C;
456
#endif
457
13.5k
      C[0]=corr[2];
458
13.5k
      C[1]=corr[1];
459
13.5k
      C[2]=corr[0];
460
13.5k
      C[3]=A[1][2];
461
13.5k
      C[4]=A[0][1];
462
13.5k
      C[5]=A[0][2];
463
13.5k
      C[6]=A[2][2];
464
13.5k
      C[7]=A[1][1];
465
13.5k
      C[8]=A[0][0];
466
467
      /*plc_tuning *= 2;*/
468
13.5k
      if (plc_tuning<2)
469
0
         plc_tuning=2;
470
13.5k
      if (plc_tuning>30)
471
0
         plc_tuning=30;
472
13.5k
#ifdef FIXED_POINT
473
13.5k
      C[0] = SHL32(C[0],1);
474
13.5k
      C[1] = SHL32(C[1],1);
475
13.5k
      C[2] = SHL32(C[2],1);
476
13.5k
      C[3] = SHL32(C[3],1);
477
13.5k
      C[4] = SHL32(C[4],1);
478
13.5k
      C[5] = SHL32(C[5],1);
479
13.5k
      C[6] = MAC16_32_Q15(C[6],MULT16_16_16(plc_tuning,655),C[6]);
480
13.5k
      C[7] = MAC16_32_Q15(C[7],MULT16_16_16(plc_tuning,655),C[7]);
481
13.5k
      C[8] = MAC16_32_Q15(C[8],MULT16_16_16(plc_tuning,655),C[8]);
482
13.5k
      normalize16(C, C16, 32767, 9);
483
#else
484
      C[6]*=.5*(1+.02*plc_tuning);
485
      C[7]*=.5*(1+.02*plc_tuning);
486
      C[8]*=.5*(1+.02*plc_tuning);
487
#endif
488
489
13.5k
      best_cdbk = pitch_gain_search_3tap_vq(gain_cdbk, gain_cdbk_size, C16, max_gain);
490
491
13.5k
#ifdef FIXED_POINT
492
13.5k
      gain[0] = ADD16(32,(spx_word16_t)gain_cdbk[best_cdbk*4]);
493
13.5k
      gain[1] = ADD16(32,(spx_word16_t)gain_cdbk[best_cdbk*4+1]);
494
13.5k
      gain[2] = ADD16(32,(spx_word16_t)gain_cdbk[best_cdbk*4+2]);
495
      /*printf ("%d %d %d %d\n",gain[0],gain[1],gain[2], best_cdbk);*/
496
#else
497
      gain[0] = 0.015625*gain_cdbk[best_cdbk*4]  + .5;
498
      gain[1] = 0.015625*gain_cdbk[best_cdbk*4+1]+ .5;
499
      gain[2] = 0.015625*gain_cdbk[best_cdbk*4+2]+ .5;
500
#endif
501
13.5k
      *cdbk_index=best_cdbk;
502
13.5k
   }
503
504
13.5k
   SPEEX_MEMSET(exc, 0, nsf);
505
54.3k
   for (i=0;i<3;i++)
506
40.7k
   {
507
40.7k
      int j;
508
40.7k
      int tmp1, tmp3;
509
40.7k
      int pp=pitch+1-i;
510
40.7k
      tmp1=nsf;
511
40.7k
      if (tmp1>pp)
512
13.3k
         tmp1=pp;
513
1.48M
      for (j=0;j<tmp1;j++)
514
1.44M
         exc[j]=MAC16_16(exc[j],SHL16(gain[2-i],7),exc2[j-pp]);
515
40.7k
      tmp3=nsf;
516
40.7k
      if (tmp3>pp+pitch)
517
3.62k
         tmp3=pp+pitch;
518
211k
      for (j=tmp1;j<tmp3;j++)
519
170k
         exc[j]=MAC16_16(exc[j],SHL16(gain[2-i],7),exc2[j-pp-pitch]);
520
40.7k
   }
521
557k
   for (i=0;i<nsf;i++)
522
543k
   {
523
543k
      spx_word32_t tmp = ADD32(ADD32(MULT16_16(gain[0],x[2][i]),MULT16_16(gain[1],x[1][i])),
524
543k
                            MULT16_16(gain[2],x[0][i]));
525
543k
      new_target[i] = SUB16(new_target[i], EXTRACT16(PSHR32(tmp,6)));
526
543k
   }
527
13.5k
   err = inner_prod(new_target, new_target, nsf);
528
529
13.5k
   return err;
530
13.5k
}
531
532
/** Finds the best quantized 3-tap pitch predictor by analysis by synthesis */
533
int pitch_search_3tap(
534
spx_word16_t target[],                 /* Target vector */
535
spx_word16_t *sw,
536
spx_coef_t ak[],                     /* LPCs for this subframe */
537
spx_coef_t awk1[],                   /* Weighted LPCs #1 for this subframe */
538
spx_coef_t awk2[],                   /* Weighted LPCs #2 for this subframe */
539
spx_sig_t exc[],                    /* Excitation */
540
const void *par,
541
int   start,                    /* Smallest pitch value allowed */
542
int   end,                      /* Largest pitch value allowed */
543
spx_word16_t pitch_coef,               /* Voicing (pitch) coefficient */
544
int   p,                        /* Number of LPC coeffs */
545
int   nsf,                      /* Number of samples in subframe */
546
SpeexBits *bits,
547
char *stack,
548
spx_word16_t *exc2,
549
spx_word16_t *r,
550
int complexity,
551
int cdbk_offset,
552
int plc_tuning,
553
spx_word32_t *cumul_gain
554
)
555
13.8k
{
556
13.8k
   int i;
557
13.8k
   int cdbk_index, pitch=0, best_gain_index=0;
558
13.8k
   VARDECL(spx_sig_t *best_exc);
559
13.8k
   VARDECL(spx_word16_t *new_target);
560
13.8k
   VARDECL(spx_word16_t *best_target);
561
13.8k
   int best_pitch=0;
562
13.8k
   spx_word32_t err, best_err=-1;
563
13.8k
   int N;
564
13.8k
   const ltp_params *params;
565
13.8k
   const signed char *gain_cdbk;
566
13.8k
   int   gain_cdbk_size;
567
13.8k
   int scaledown=0;
568
569
13.8k
   VARDECL(int *nbest);
570
571
13.8k
   params = (const ltp_params*) par;
572
13.8k
   gain_cdbk_size = 1<<params->gain_bits;
573
13.8k
   gain_cdbk = params->gain_cdbk + 4*gain_cdbk_size*cdbk_offset;
574
575
13.8k
   N=complexity;
576
13.8k
   if (N>10)
577
0
      N=10;
578
13.8k
   if (N<1)
579
2.50k
      N=1;
580
581
13.8k
   ALLOC(nbest, N, int);
582
13.8k
   params = (const ltp_params*) par;
583
584
13.8k
   if (end<start)
585
945
   {
586
945
      speex_bits_pack(bits, 0, params->pitch_bits);
587
945
      speex_bits_pack(bits, 0, params->gain_bits);
588
945
      SPEEX_MEMSET(exc, 0, nsf);
589
945
      return start;
590
945
   }
591
592
#ifdef FIXED_POINT
593
   /* Check if we need to scale everything down in the pitch search to avoid overflows */
594
78.1k
   for (i=0;i<nsf;i++)
595
76.6k
   {
596
76.6k
      if (ABS16(target[i])>16383)
597
1.72k
      {
598
1.72k
         scaledown=1;
599
1.72k
         break;
600
1.72k
      }
601
76.6k
   }
602
228k
   for (i=-end;i<0;i++)
603
226k
   {
604
226k
      if (ABS16(exc2[i])>16383)
605
1.28k
      {
606
1.28k
         scaledown=1;
607
1.28k
         break;
608
1.28k
      }
609
226k
   }
610
#endif
611
12.9k
   if (N>end-start+1)
612
1.11k
      N=end-start+1;
613
12.9k
   if (end != start)
614
10.9k
      open_loop_nbest_pitch(sw, start, end, nsf, nbest, NULL, N, stack);
615
1.93k
   else
616
1.93k
      nbest[0] = start;
617
618
12.9k
   ALLOC(best_exc, nsf, spx_sig_t);
619
12.9k
   ALLOC(new_target, nsf, spx_word16_t);
620
12.9k
   ALLOC(best_target, nsf, spx_word16_t);
621
622
76.7k
   for (i=0;i<N;i++)
623
63.8k
   {
624
63.8k
      pitch=nbest[i];
625
63.8k
      SPEEX_MEMSET(exc, 0, nsf);
626
63.8k
      err=pitch_gain_search_3tap(target, ak, awk1, awk2, exc, gain_cdbk, gain_cdbk_size, pitch, p, nsf,
627
63.8k
                                 bits, stack, exc2, r, new_target, &cdbk_index, plc_tuning, *cumul_gain, scaledown);
628
63.8k
      if (err<best_err || best_err<0)
629
16.9k
      {
630
16.9k
         SPEEX_COPY(best_exc, exc, nsf);
631
16.9k
         SPEEX_COPY(best_target, new_target, nsf);
632
16.9k
         best_err=err;
633
16.9k
         best_pitch=pitch;
634
16.9k
         best_gain_index=cdbk_index;
635
16.9k
      }
636
63.8k
   }
637
   /*printf ("pitch: %d %d\n", best_pitch, best_gain_index);*/
638
9.67k
   speex_bits_pack(bits, best_pitch-start, params->pitch_bits);
639
9.67k
   speex_bits_pack(bits, best_gain_index, params->gain_bits);
640
#ifdef FIXED_POINT
641
3.25k
   *cumul_gain = MULT16_32_Q13(SHL16(params->gain_cdbk[4*best_gain_index+3],8), MAX32(1024,*cumul_gain));
642
#else
643
9.67k
   *cumul_gain = 0.03125*MAX32(1024,*cumul_gain)*params->gain_cdbk[4*best_gain_index+3];
644
#endif
645
   /*printf ("%f\n", cumul_gain);*/
646
   /*printf ("encode pitch: %d %d\n", best_pitch, best_gain_index);*/
647
12.9k
   SPEEX_COPY(exc, best_exc, nsf);
648
12.9k
   SPEEX_COPY(target, best_target, nsf);
649
#ifdef FIXED_POINT
650
   /* Scale target back up if needed */
651
3.25k
   if (scaledown)
652
2.02k
   {
653
83.0k
      for (i=0;i<nsf;i++)
654
81.0k
         target[i]=SHL16(target[i],1);
655
2.02k
   }
656
#endif
657
9.67k
   return pitch;
658
13.8k
}
pitch_search_3tap
Line
Count
Source
555
10.4k
{
556
10.4k
   int i;
557
10.4k
   int cdbk_index, pitch=0, best_gain_index=0;
558
10.4k
   VARDECL(spx_sig_t *best_exc);
559
10.4k
   VARDECL(spx_word16_t *new_target);
560
10.4k
   VARDECL(spx_word16_t *best_target);
561
10.4k
   int best_pitch=0;
562
10.4k
   spx_word32_t err, best_err=-1;
563
10.4k
   int N;
564
10.4k
   const ltp_params *params;
565
10.4k
   const signed char *gain_cdbk;
566
10.4k
   int   gain_cdbk_size;
567
10.4k
   int scaledown=0;
568
569
10.4k
   VARDECL(int *nbest);
570
571
10.4k
   params = (const ltp_params*) par;
572
10.4k
   gain_cdbk_size = 1<<params->gain_bits;
573
10.4k
   gain_cdbk = params->gain_cdbk + 4*gain_cdbk_size*cdbk_offset;
574
575
10.4k
   N=complexity;
576
10.4k
   if (N>10)
577
0
      N=10;
578
10.4k
   if (N<1)
579
1.71k
      N=1;
580
581
10.4k
   ALLOC(nbest, N, int);
582
10.4k
   params = (const ltp_params*) par;
583
584
10.4k
   if (end<start)
585
731
   {
586
731
      speex_bits_pack(bits, 0, params->pitch_bits);
587
731
      speex_bits_pack(bits, 0, params->gain_bits);
588
731
      SPEEX_MEMSET(exc, 0, nsf);
589
731
      return start;
590
731
   }
591
592
#ifdef FIXED_POINT
593
   /* Check if we need to scale everything down in the pitch search to avoid overflows */
594
   for (i=0;i<nsf;i++)
595
   {
596
      if (ABS16(target[i])>16383)
597
      {
598
         scaledown=1;
599
         break;
600
      }
601
   }
602
   for (i=-end;i<0;i++)
603
   {
604
      if (ABS16(exc2[i])>16383)
605
      {
606
         scaledown=1;
607
         break;
608
      }
609
   }
610
#endif
611
9.67k
   if (N>end-start+1)
612
940
      N=end-start+1;
613
9.67k
   if (end != start)
614
8.46k
      open_loop_nbest_pitch(sw, start, end, nsf, nbest, NULL, N, stack);
615
1.20k
   else
616
1.20k
      nbest[0] = start;
617
618
9.67k
   ALLOC(best_exc, nsf, spx_sig_t);
619
9.67k
   ALLOC(new_target, nsf, spx_word16_t);
620
9.67k
   ALLOC(best_target, nsf, spx_word16_t);
621
622
59.9k
   for (i=0;i<N;i++)
623
50.2k
   {
624
50.2k
      pitch=nbest[i];
625
50.2k
      SPEEX_MEMSET(exc, 0, nsf);
626
50.2k
      err=pitch_gain_search_3tap(target, ak, awk1, awk2, exc, gain_cdbk, gain_cdbk_size, pitch, p, nsf,
627
50.2k
                                 bits, stack, exc2, r, new_target, &cdbk_index, plc_tuning, *cumul_gain, scaledown);
628
50.2k
      if (err<best_err || best_err<0)
629
12.1k
      {
630
12.1k
         SPEEX_COPY(best_exc, exc, nsf);
631
12.1k
         SPEEX_COPY(best_target, new_target, nsf);
632
12.1k
         best_err=err;
633
12.1k
         best_pitch=pitch;
634
12.1k
         best_gain_index=cdbk_index;
635
12.1k
      }
636
50.2k
   }
637
   /*printf ("pitch: %d %d\n", best_pitch, best_gain_index);*/
638
9.67k
   speex_bits_pack(bits, best_pitch-start, params->pitch_bits);
639
9.67k
   speex_bits_pack(bits, best_gain_index, params->gain_bits);
640
#ifdef FIXED_POINT
641
   *cumul_gain = MULT16_32_Q13(SHL16(params->gain_cdbk[4*best_gain_index+3],8), MAX32(1024,*cumul_gain));
642
#else
643
9.67k
   *cumul_gain = 0.03125*MAX32(1024,*cumul_gain)*params->gain_cdbk[4*best_gain_index+3];
644
9.67k
#endif
645
   /*printf ("%f\n", cumul_gain);*/
646
   /*printf ("encode pitch: %d %d\n", best_pitch, best_gain_index);*/
647
9.67k
   SPEEX_COPY(exc, best_exc, nsf);
648
9.67k
   SPEEX_COPY(target, best_target, nsf);
649
#ifdef FIXED_POINT
650
   /* Scale target back up if needed */
651
   if (scaledown)
652
   {
653
      for (i=0;i<nsf;i++)
654
         target[i]=SHL16(target[i],1);
655
   }
656
#endif
657
9.67k
   return pitch;
658
10.4k
}
pitch_search_3tap
Line
Count
Source
555
3.46k
{
556
3.46k
   int i;
557
3.46k
   int cdbk_index, pitch=0, best_gain_index=0;
558
3.46k
   VARDECL(spx_sig_t *best_exc);
559
3.46k
   VARDECL(spx_word16_t *new_target);
560
3.46k
   VARDECL(spx_word16_t *best_target);
561
3.46k
   int best_pitch=0;
562
3.46k
   spx_word32_t err, best_err=-1;
563
3.46k
   int N;
564
3.46k
   const ltp_params *params;
565
3.46k
   const signed char *gain_cdbk;
566
3.46k
   int   gain_cdbk_size;
567
3.46k
   int scaledown=0;
568
569
3.46k
   VARDECL(int *nbest);
570
571
3.46k
   params = (const ltp_params*) par;
572
3.46k
   gain_cdbk_size = 1<<params->gain_bits;
573
3.46k
   gain_cdbk = params->gain_cdbk + 4*gain_cdbk_size*cdbk_offset;
574
575
3.46k
   N=complexity;
576
3.46k
   if (N>10)
577
0
      N=10;
578
3.46k
   if (N<1)
579
788
      N=1;
580
581
3.46k
   ALLOC(nbest, N, int);
582
3.46k
   params = (const ltp_params*) par;
583
584
3.46k
   if (end<start)
585
214
   {
586
214
      speex_bits_pack(bits, 0, params->pitch_bits);
587
214
      speex_bits_pack(bits, 0, params->gain_bits);
588
214
      SPEEX_MEMSET(exc, 0, nsf);
589
214
      return start;
590
214
   }
591
592
3.25k
#ifdef FIXED_POINT
593
   /* Check if we need to scale everything down in the pitch search to avoid overflows */
594
78.1k
   for (i=0;i<nsf;i++)
595
76.6k
   {
596
76.6k
      if (ABS16(target[i])>16383)
597
1.72k
      {
598
1.72k
         scaledown=1;
599
1.72k
         break;
600
1.72k
      }
601
76.6k
   }
602
228k
   for (i=-end;i<0;i++)
603
226k
   {
604
226k
      if (ABS16(exc2[i])>16383)
605
1.28k
      {
606
1.28k
         scaledown=1;
607
1.28k
         break;
608
1.28k
      }
609
226k
   }
610
3.25k
#endif
611
3.25k
   if (N>end-start+1)
612
176
      N=end-start+1;
613
3.25k
   if (end != start)
614
2.51k
      open_loop_nbest_pitch(sw, start, end, nsf, nbest, NULL, N, stack);
615
735
   else
616
735
      nbest[0] = start;
617
618
3.25k
   ALLOC(best_exc, nsf, spx_sig_t);
619
3.25k
   ALLOC(new_target, nsf, spx_word16_t);
620
3.25k
   ALLOC(best_target, nsf, spx_word16_t);
621
622
16.8k
   for (i=0;i<N;i++)
623
13.5k
   {
624
13.5k
      pitch=nbest[i];
625
13.5k
      SPEEX_MEMSET(exc, 0, nsf);
626
13.5k
      err=pitch_gain_search_3tap(target, ak, awk1, awk2, exc, gain_cdbk, gain_cdbk_size, pitch, p, nsf,
627
13.5k
                                 bits, stack, exc2, r, new_target, &cdbk_index, plc_tuning, *cumul_gain, scaledown);
628
13.5k
      if (err<best_err || best_err<0)
629
4.80k
      {
630
4.80k
         SPEEX_COPY(best_exc, exc, nsf);
631
4.80k
         SPEEX_COPY(best_target, new_target, nsf);
632
4.80k
         best_err=err;
633
4.80k
         best_pitch=pitch;
634
4.80k
         best_gain_index=cdbk_index;
635
4.80k
      }
636
13.5k
   }
637
   /*printf ("pitch: %d %d\n", best_pitch, best_gain_index);*/
638
3.25k
   speex_bits_pack(bits, best_pitch-start, params->pitch_bits);
639
3.25k
   speex_bits_pack(bits, best_gain_index, params->gain_bits);
640
3.25k
#ifdef FIXED_POINT
641
3.25k
   *cumul_gain = MULT16_32_Q13(SHL16(params->gain_cdbk[4*best_gain_index+3],8), MAX32(1024,*cumul_gain));
642
#else
643
   *cumul_gain = 0.03125*MAX32(1024,*cumul_gain)*params->gain_cdbk[4*best_gain_index+3];
644
#endif
645
   /*printf ("%f\n", cumul_gain);*/
646
   /*printf ("encode pitch: %d %d\n", best_pitch, best_gain_index);*/
647
3.25k
   SPEEX_COPY(exc, best_exc, nsf);
648
3.25k
   SPEEX_COPY(target, best_target, nsf);
649
3.25k
#ifdef FIXED_POINT
650
   /* Scale target back up if needed */
651
3.25k
   if (scaledown)
652
2.02k
   {
653
83.0k
      for (i=0;i<nsf;i++)
654
81.0k
         target[i]=SHL16(target[i],1);
655
2.02k
   }
656
3.25k
#endif
657
3.25k
   return pitch;
658
3.46k
}
659
#endif /* DISABLE_ENCODER */
660
661
#ifndef DISABLE_DECODER
662
void pitch_unquant_3tap(
663
spx_word16_t exc[],             /* Input excitation */
664
spx_word32_t exc_out[],         /* Output excitation */
665
int   start,                    /* Smallest pitch value allowed */
666
int   end,                      /* Largest pitch value allowed */
667
spx_word16_t pitch_coef,        /* Voicing (pitch) coefficient */
668
const void *par,
669
int   nsf,                      /* Number of samples in subframe */
670
int *pitch_val,
671
spx_word16_t *gain_val,
672
SpeexBits *bits,
673
char *stack,
674
int count_lost,
675
int subframe_offset,
676
spx_word16_t last_pitch_gain,
677
int cdbk_offset
678
)
679
128k
{
680
128k
   int i;
681
128k
   int pitch;
682
128k
   int gain_index;
683
128k
   spx_word16_t gain[3];
684
128k
   const signed char *gain_cdbk;
685
128k
   int gain_cdbk_size;
686
128k
   const ltp_params *params;
687
688
128k
   params = (const ltp_params*) par;
689
128k
   gain_cdbk_size = 1<<params->gain_bits;
690
128k
   gain_cdbk = params->gain_cdbk + 4*gain_cdbk_size*cdbk_offset;
691
692
128k
   pitch = speex_bits_unpack_unsigned(bits, params->pitch_bits);
693
128k
   pitch += start;
694
128k
   gain_index = speex_bits_unpack_unsigned(bits, params->gain_bits);
695
   /*printf ("decode pitch: %d %d\n", pitch, gain_index);*/
696
#ifdef FIXED_POINT
697
64.2k
   gain[0] = ADD16(32,(spx_word16_t)gain_cdbk[gain_index*4]);
698
64.2k
   gain[1] = ADD16(32,(spx_word16_t)gain_cdbk[gain_index*4+1]);
699
64.2k
   gain[2] = ADD16(32,(spx_word16_t)gain_cdbk[gain_index*4+2]);
700
#else
701
   gain[0] = 0.015625*gain_cdbk[gain_index*4]+.5;
702
   gain[1] = 0.015625*gain_cdbk[gain_index*4+1]+.5;
703
   gain[2] = 0.015625*gain_cdbk[gain_index*4+2]+.5;
704
#endif
705
706
128k
   if (count_lost && pitch > subframe_offset)
707
0
   {
708
0
      spx_word16_t gain_sum;
709
0
      if (1) {
710
#ifdef FIXED_POINT
711
0
         spx_word16_t tmp = count_lost < 4 ? last_pitch_gain : SHR16(last_pitch_gain,1);
712
0
         if (tmp>62)
713
0
            tmp=62;
714
#else
715
0
         spx_word16_t tmp = count_lost < 4 ? last_pitch_gain : 0.5 * last_pitch_gain;
716
0
         if (tmp>.95)
717
0
            tmp=.95;
718
#endif
719
0
         gain_sum = gain_3tap_to_1tap(gain);
720
721
0
         if (gain_sum > tmp)
722
0
         {
723
0
            spx_word16_t fact = DIV32_16(SHL32(EXTEND32(tmp),14),gain_sum);
724
0
            for (i=0;i<3;i++)
725
0
               gain[i]=MULT16_16_Q14(fact,gain[i]);
726
0
         }
727
728
0
      }
729
730
0
   }
731
732
128k
   *pitch_val = pitch;
733
128k
   gain_val[0]=gain[0];
734
128k
   gain_val[1]=gain[1];
735
128k
   gain_val[2]=gain[2];
736
128k
   gain[0] = SHL16(gain[0],7);
737
128k
   gain[1] = SHL16(gain[1],7);
738
128k
   gain[2] = SHL16(gain[2],7);
739
128k
   SPEEX_MEMSET(exc_out, 0, nsf);
740
514k
   for (i=0;i<3;i++)
741
385k
   {
742
385k
      int j;
743
385k
      int tmp1, tmp3;
744
385k
      int pp=pitch+1-i;
745
385k
      tmp1=nsf;
746
385k
      if (tmp1>pp)
747
318k
         tmp1=pp;
748
8.72M
      for (j=0;j<tmp1;j++)
749
8.33M
         exc_out[j]=MAC16_16(exc_out[j],gain[2-i],exc[j-pp]);
750
385k
      tmp3=nsf;
751
385k
      if (tmp3>pp+pitch)
752
295k
         tmp3=pp+pitch;
753
5.70M
      for (j=tmp1;j<tmp3;j++)
754
5.31M
         exc_out[j]=MAC16_16(exc_out[j],gain[2-i],exc[j-pp-pitch]);
755
385k
   }
756
   /*for (i=0;i<nsf;i++)
757
   exc[i]=PSHR32(exc32[i],13);*/
758
128k
}
pitch_unquant_3tap
Line
Count
Source
679
64.2k
{
680
64.2k
   int i;
681
64.2k
   int pitch;
682
64.2k
   int gain_index;
683
64.2k
   spx_word16_t gain[3];
684
64.2k
   const signed char *gain_cdbk;
685
64.2k
   int gain_cdbk_size;
686
64.2k
   const ltp_params *params;
687
688
64.2k
   params = (const ltp_params*) par;
689
64.2k
   gain_cdbk_size = 1<<params->gain_bits;
690
64.2k
   gain_cdbk = params->gain_cdbk + 4*gain_cdbk_size*cdbk_offset;
691
692
64.2k
   pitch = speex_bits_unpack_unsigned(bits, params->pitch_bits);
693
64.2k
   pitch += start;
694
64.2k
   gain_index = speex_bits_unpack_unsigned(bits, params->gain_bits);
695
   /*printf ("decode pitch: %d %d\n", pitch, gain_index);*/
696
#ifdef FIXED_POINT
697
   gain[0] = ADD16(32,(spx_word16_t)gain_cdbk[gain_index*4]);
698
   gain[1] = ADD16(32,(spx_word16_t)gain_cdbk[gain_index*4+1]);
699
   gain[2] = ADD16(32,(spx_word16_t)gain_cdbk[gain_index*4+2]);
700
#else
701
64.2k
   gain[0] = 0.015625*gain_cdbk[gain_index*4]+.5;
702
64.2k
   gain[1] = 0.015625*gain_cdbk[gain_index*4+1]+.5;
703
64.2k
   gain[2] = 0.015625*gain_cdbk[gain_index*4+2]+.5;
704
64.2k
#endif
705
706
64.2k
   if (count_lost && pitch > subframe_offset)
707
0
   {
708
0
      spx_word16_t gain_sum;
709
0
      if (1) {
710
#ifdef FIXED_POINT
711
         spx_word16_t tmp = count_lost < 4 ? last_pitch_gain : SHR16(last_pitch_gain,1);
712
         if (tmp>62)
713
            tmp=62;
714
#else
715
0
         spx_word16_t tmp = count_lost < 4 ? last_pitch_gain : 0.5 * last_pitch_gain;
716
0
         if (tmp>.95)
717
0
            tmp=.95;
718
0
#endif
719
0
         gain_sum = gain_3tap_to_1tap(gain);
720
721
0
         if (gain_sum > tmp)
722
0
         {
723
0
            spx_word16_t fact = DIV32_16(SHL32(EXTEND32(tmp),14),gain_sum);
724
0
            for (i=0;i<3;i++)
725
0
               gain[i]=MULT16_16_Q14(fact,gain[i]);
726
0
         }
727
728
0
      }
729
730
0
   }
731
732
64.2k
   *pitch_val = pitch;
733
64.2k
   gain_val[0]=gain[0];
734
64.2k
   gain_val[1]=gain[1];
735
64.2k
   gain_val[2]=gain[2];
736
64.2k
   gain[0] = SHL16(gain[0],7);
737
64.2k
   gain[1] = SHL16(gain[1],7);
738
64.2k
   gain[2] = SHL16(gain[2],7);
739
64.2k
   SPEEX_MEMSET(exc_out, 0, nsf);
740
257k
   for (i=0;i<3;i++)
741
192k
   {
742
192k
      int j;
743
192k
      int tmp1, tmp3;
744
192k
      int pp=pitch+1-i;
745
192k
      tmp1=nsf;
746
192k
      if (tmp1>pp)
747
159k
         tmp1=pp;
748
4.36M
      for (j=0;j<tmp1;j++)
749
4.16M
         exc_out[j]=MAC16_16(exc_out[j],gain[2-i],exc[j-pp]);
750
192k
      tmp3=nsf;
751
192k
      if (tmp3>pp+pitch)
752
147k
         tmp3=pp+pitch;
753
2.85M
      for (j=tmp1;j<tmp3;j++)
754
2.65M
         exc_out[j]=MAC16_16(exc_out[j],gain[2-i],exc[j-pp-pitch]);
755
192k
   }
756
   /*for (i=0;i<nsf;i++)
757
   exc[i]=PSHR32(exc32[i],13);*/
758
64.2k
}
pitch_unquant_3tap
Line
Count
Source
679
64.2k
{
680
64.2k
   int i;
681
64.2k
   int pitch;
682
64.2k
   int gain_index;
683
64.2k
   spx_word16_t gain[3];
684
64.2k
   const signed char *gain_cdbk;
685
64.2k
   int gain_cdbk_size;
686
64.2k
   const ltp_params *params;
687
688
64.2k
   params = (const ltp_params*) par;
689
64.2k
   gain_cdbk_size = 1<<params->gain_bits;
690
64.2k
   gain_cdbk = params->gain_cdbk + 4*gain_cdbk_size*cdbk_offset;
691
692
64.2k
   pitch = speex_bits_unpack_unsigned(bits, params->pitch_bits);
693
64.2k
   pitch += start;
694
64.2k
   gain_index = speex_bits_unpack_unsigned(bits, params->gain_bits);
695
   /*printf ("decode pitch: %d %d\n", pitch, gain_index);*/
696
64.2k
#ifdef FIXED_POINT
697
64.2k
   gain[0] = ADD16(32,(spx_word16_t)gain_cdbk[gain_index*4]);
698
64.2k
   gain[1] = ADD16(32,(spx_word16_t)gain_cdbk[gain_index*4+1]);
699
64.2k
   gain[2] = ADD16(32,(spx_word16_t)gain_cdbk[gain_index*4+2]);
700
#else
701
   gain[0] = 0.015625*gain_cdbk[gain_index*4]+.5;
702
   gain[1] = 0.015625*gain_cdbk[gain_index*4+1]+.5;
703
   gain[2] = 0.015625*gain_cdbk[gain_index*4+2]+.5;
704
#endif
705
706
64.2k
   if (count_lost && pitch > subframe_offset)
707
0
   {
708
0
      spx_word16_t gain_sum;
709
0
      if (1) {
710
0
#ifdef FIXED_POINT
711
0
         spx_word16_t tmp = count_lost < 4 ? last_pitch_gain : SHR16(last_pitch_gain,1);
712
0
         if (tmp>62)
713
0
            tmp=62;
714
#else
715
         spx_word16_t tmp = count_lost < 4 ? last_pitch_gain : 0.5 * last_pitch_gain;
716
         if (tmp>.95)
717
            tmp=.95;
718
#endif
719
0
         gain_sum = gain_3tap_to_1tap(gain);
720
721
0
         if (gain_sum > tmp)
722
0
         {
723
0
            spx_word16_t fact = DIV32_16(SHL32(EXTEND32(tmp),14),gain_sum);
724
0
            for (i=0;i<3;i++)
725
0
               gain[i]=MULT16_16_Q14(fact,gain[i]);
726
0
         }
727
728
0
      }
729
730
0
   }
731
732
64.2k
   *pitch_val = pitch;
733
64.2k
   gain_val[0]=gain[0];
734
64.2k
   gain_val[1]=gain[1];
735
64.2k
   gain_val[2]=gain[2];
736
64.2k
   gain[0] = SHL16(gain[0],7);
737
64.2k
   gain[1] = SHL16(gain[1],7);
738
64.2k
   gain[2] = SHL16(gain[2],7);
739
64.2k
   SPEEX_MEMSET(exc_out, 0, nsf);
740
257k
   for (i=0;i<3;i++)
741
192k
   {
742
192k
      int j;
743
192k
      int tmp1, tmp3;
744
192k
      int pp=pitch+1-i;
745
192k
      tmp1=nsf;
746
192k
      if (tmp1>pp)
747
159k
         tmp1=pp;
748
4.36M
      for (j=0;j<tmp1;j++)
749
4.16M
         exc_out[j]=MAC16_16(exc_out[j],gain[2-i],exc[j-pp]);
750
192k
      tmp3=nsf;
751
192k
      if (tmp3>pp+pitch)
752
147k
         tmp3=pp+pitch;
753
2.85M
      for (j=tmp1;j<tmp3;j++)
754
2.65M
         exc_out[j]=MAC16_16(exc_out[j],gain[2-i],exc[j-pp-pitch]);
755
192k
   }
756
   /*for (i=0;i<nsf;i++)
757
   exc[i]=PSHR32(exc32[i],13);*/
758
64.2k
}
759
#endif /* DISABLE_DECODER */
760
761
#ifndef DISABLE_ENCODER
762
/** Forced pitch delay and gain */
763
int forced_pitch_quant(
764
spx_word16_t target[],                 /* Target vector */
765
spx_word16_t *sw,
766
spx_coef_t ak[],                     /* LPCs for this subframe */
767
spx_coef_t awk1[],                   /* Weighted LPCs #1 for this subframe */
768
spx_coef_t awk2[],                   /* Weighted LPCs #2 for this subframe */
769
spx_sig_t exc[],                    /* Excitation */
770
const void *par,
771
int   start,                    /* Smallest pitch value allowed */
772
int   end,                      /* Largest pitch value allowed */
773
spx_word16_t pitch_coef,               /* Voicing (pitch) coefficient */
774
int   p,                        /* Number of LPC coeffs */
775
int   nsf,                      /* Number of samples in subframe */
776
SpeexBits *bits,
777
char *stack,
778
spx_word16_t *exc2,
779
spx_word16_t *r,
780
int complexity,
781
int cdbk_offset,
782
int plc_tuning,
783
spx_word32_t *cumul_gain
784
)
785
22.8k
{
786
22.8k
   int i;
787
22.8k
   VARDECL(spx_word16_t *res);
788
22.8k
   ALLOC(res, nsf, spx_word16_t);
789
#ifdef FIXED_POINT
790
4.63k
   if (pitch_coef>63)
791
92
      pitch_coef=63;
792
#else
793
18.2k
   if (pitch_coef>.99)
794
192
      pitch_coef=.99;
795
#endif
796
637k
   for (i=0;i<nsf&&i<start;i++)
797
615k
   {
798
615k
      exc[i]=MULT16_16(SHL16(pitch_coef, 7),exc2[i-start]);
799
615k
   }
800
321k
   for (;i<nsf;i++)
801
298k
   {
802
298k
      exc[i]=MULT16_32_Q15(SHL16(pitch_coef, 9),exc[i-start]);
803
298k
   }
804
936k
   for (i=0;i<nsf;i++)
805
913k
      res[i] = EXTRACT16(PSHR32(exc[i], SIG_SHIFT-1));
806
22.8k
   syn_percep_zero16(res, ak, awk1, awk2, res, nsf, p, stack);
807
936k
   for (i=0;i<nsf;i++)
808
913k
      target[i]=EXTRACT16(SATURATE(SUB32(EXTEND32(target[i]),EXTEND32(res[i])),32700));
809
22.8k
   return start;
810
22.8k
}
forced_pitch_quant
Line
Count
Source
785
18.2k
{
786
18.2k
   int i;
787
18.2k
   VARDECL(spx_word16_t *res);
788
18.2k
   ALLOC(res, nsf, spx_word16_t);
789
#ifdef FIXED_POINT
790
   if (pitch_coef>63)
791
      pitch_coef=63;
792
#else
793
18.2k
   if (pitch_coef>.99)
794
192
      pitch_coef=.99;
795
18.2k
#endif
796
493k
   for (i=0;i<nsf&&i<start;i++)
797
474k
   {
798
474k
      exc[i]=MULT16_16(SHL16(pitch_coef, 7),exc2[i-start]);
799
474k
   }
800
271k
   for (;i<nsf;i++)
801
253k
   {
802
253k
      exc[i]=MULT16_32_Q15(SHL16(pitch_coef, 9),exc[i-start]);
803
253k
   }
804
746k
   for (i=0;i<nsf;i++)
805
728k
      res[i] = EXTRACT16(PSHR32(exc[i], SIG_SHIFT-1));
806
18.2k
   syn_percep_zero16(res, ak, awk1, awk2, res, nsf, p, stack);
807
746k
   for (i=0;i<nsf;i++)
808
728k
      target[i]=EXTRACT16(SATURATE(SUB32(EXTEND32(target[i]),EXTEND32(res[i])),32700));
809
18.2k
   return start;
810
18.2k
}
forced_pitch_quant
Line
Count
Source
785
4.63k
{
786
4.63k
   int i;
787
4.63k
   VARDECL(spx_word16_t *res);
788
4.63k
   ALLOC(res, nsf, spx_word16_t);
789
4.63k
#ifdef FIXED_POINT
790
4.63k
   if (pitch_coef>63)
791
92
      pitch_coef=63;
792
#else
793
   if (pitch_coef>.99)
794
      pitch_coef=.99;
795
#endif
796
144k
   for (i=0;i<nsf&&i<start;i++)
797
140k
   {
798
140k
      exc[i]=MULT16_16(SHL16(pitch_coef, 7),exc2[i-start]);
799
140k
   }
800
49.8k
   for (;i<nsf;i++)
801
45.2k
   {
802
45.2k
      exc[i]=MULT16_32_Q15(SHL16(pitch_coef, 9),exc[i-start]);
803
45.2k
   }
804
190k
   for (i=0;i<nsf;i++)
805
185k
      res[i] = EXTRACT16(PSHR32(exc[i], SIG_SHIFT-1));
806
4.63k
   syn_percep_zero16(res, ak, awk1, awk2, res, nsf, p, stack);
807
190k
   for (i=0;i<nsf;i++)
808
185k
      target[i]=EXTRACT16(SATURATE(SUB32(EXTEND32(target[i]),EXTEND32(res[i])),32700));
809
4.63k
   return start;
810
4.63k
}
811
#endif /* DISABLE_ENCODER */
812
813
#ifndef DISABLE_DECODER
814
/** Unquantize forced pitch delay and gain */
815
void forced_pitch_unquant(
816
spx_word16_t exc[],             /* Input excitation */
817
spx_word32_t exc_out[],         /* Output excitation */
818
int   start,                    /* Smallest pitch value allowed */
819
int   end,                      /* Largest pitch value allowed */
820
spx_word16_t pitch_coef,        /* Voicing (pitch) coefficient */
821
const void *par,
822
int   nsf,                      /* Number of samples in subframe */
823
int *pitch_val,
824
spx_word16_t *gain_val,
825
SpeexBits *bits,
826
char *stack,
827
int count_lost,
828
int subframe_offset,
829
spx_word16_t last_pitch_gain,
830
int cdbk_offset
831
)
832
60.4k
{
833
60.4k
   int i;
834
#ifdef FIXED_POINT
835
30.2k
   if (pitch_coef>63)
836
3.66k
      pitch_coef=63;
837
#else
838
30.2k
   if (pitch_coef>.99)
839
3.66k
      pitch_coef=.99;
840
#endif
841
2.47M
   for (i=0;i<nsf;i++)
842
2.41M
   {
843
2.41M
      exc_out[i]=MULT16_16(exc[i-start],SHL16(pitch_coef,7));
844
2.41M
      exc[i] = EXTRACT16(PSHR32(exc_out[i],13));
845
2.41M
   }
846
60.4k
   *pitch_val = start;
847
60.4k
   gain_val[0]=gain_val[2]=0;
848
60.4k
   gain_val[1] = pitch_coef;
849
60.4k
}
forced_pitch_unquant
Line
Count
Source
832
30.2k
{
833
30.2k
   int i;
834
#ifdef FIXED_POINT
835
   if (pitch_coef>63)
836
      pitch_coef=63;
837
#else
838
30.2k
   if (pitch_coef>.99)
839
3.66k
      pitch_coef=.99;
840
30.2k
#endif
841
1.23M
   for (i=0;i<nsf;i++)
842
1.20M
   {
843
1.20M
      exc_out[i]=MULT16_16(exc[i-start],SHL16(pitch_coef,7));
844
1.20M
      exc[i] = EXTRACT16(PSHR32(exc_out[i],13));
845
1.20M
   }
846
30.2k
   *pitch_val = start;
847
30.2k
   gain_val[0]=gain_val[2]=0;
848
30.2k
   gain_val[1] = pitch_coef;
849
30.2k
}
forced_pitch_unquant
Line
Count
Source
832
30.2k
{
833
30.2k
   int i;
834
30.2k
#ifdef FIXED_POINT
835
30.2k
   if (pitch_coef>63)
836
3.66k
      pitch_coef=63;
837
#else
838
   if (pitch_coef>.99)
839
      pitch_coef=.99;
840
#endif
841
1.23M
   for (i=0;i<nsf;i++)
842
1.20M
   {
843
1.20M
      exc_out[i]=MULT16_16(exc[i-start],SHL16(pitch_coef,7));
844
1.20M
      exc[i] = EXTRACT16(PSHR32(exc_out[i],13));
845
1.20M
   }
846
30.2k
   *pitch_val = start;
847
30.2k
   gain_val[0]=gain_val[2]=0;
848
30.2k
   gain_val[1] = pitch_coef;
849
30.2k
}
850
#endif /* DISABLE_DECODER */