Coverage Report

Created: 2026-05-24 07:02

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/speex/libspeex/nb_celp.c
Line
Count
Source
1
/* Copyright (C) 2002-2006 Jean-Marc Valin
2
   File: nb_celp.c
3
4
   Redistribution and use in source and binary forms, with or without
5
   modification, are permitted provided that the following conditions
6
   are met:
7
8
   - Redistributions of source code must retain the above copyright
9
   notice, this list of conditions and the following disclaimer.
10
11
   - Redistributions in binary form must reproduce the above copyright
12
   notice, this list of conditions and the following disclaimer in the
13
   documentation and/or other materials provided with the distribution.
14
15
   - Neither the name of the Xiph.org Foundation nor the names of its
16
   contributors may be used to endorse or promote products derived from
17
   this software without specific prior written permission.
18
19
   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20
   ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21
   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22
   A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
23
   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24
   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25
   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26
   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27
   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28
   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29
   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
*/
31
32
#ifdef HAVE_CONFIG_H
33
#include "config.h"
34
#endif
35
36
#include <math.h>
37
#include "nb_celp.h"
38
#include "lpc.h"
39
#include "lsp.h"
40
#include "ltp.h"
41
#include "quant_lsp.h"
42
#include "cb_search.h"
43
#include "filters.h"
44
#include "stack_alloc.h"
45
#include "vq.h"
46
#include "vbr.h"
47
#include "arch.h"
48
#include "math_approx.h"
49
#include "os_support.h"
50
51
#ifdef VORBIS_PSYCHO
52
#include "vorbis_psy.h"
53
#endif
54
55
#ifndef NULL
56
#define NULL 0
57
#endif
58
59
2.20M
#define SUBMODE(x) st->submodes[st->submodeID]->x
60
61
/* Default size for the encoder and decoder stack (can be changed at compile time).
62
   This does not apply when using variable-size arrays or alloca. */
63
#ifndef NB_ENC_STACK
64
#define NB_ENC_STACK (8000*sizeof(spx_sig_t))
65
#endif
66
67
#ifndef NB_DEC_STACK
68
#define NB_DEC_STACK (4000*sizeof(spx_sig_t))
69
#endif
70
71
72
#ifdef FIXED_POINT
73
static const spx_word32_t ol_gain_table[32]={18900, 25150, 33468, 44536, 59265, 78865, 104946, 139653, 185838, 247297, 329081, 437913, 582736, 775454, 1031906, 1373169, 1827293, 2431601, 3235761, 4305867, 5729870, 7624808, 10146425, 13501971, 17967238, 23909222, 31816294, 42338330, 56340132, 74972501, 99766822, 132760927};
74
static const spx_word16_t exc_gain_quant_scal3_bound[7]={1841, 3883, 6051, 8062, 10444, 13580, 18560};
75
static const spx_word16_t exc_gain_quant_scal3[8]={1002, 2680, 5086, 7016, 9108, 11781, 15380, 21740};
76
static const spx_word16_t exc_gain_quant_scal1_bound[1]={14385};
77
static const spx_word16_t exc_gain_quant_scal1[2]={11546, 17224};
78
79
202k
#define LSP_MARGIN 16
80
17.9k
#define LSP_DELTA1 6553
81
#define LSP_DELTA2 1638
82
83
#else
84
85
static const float exc_gain_quant_scal3_bound[7]={0.112338f, 0.236980f, 0.369316f, 0.492054f, 0.637471f, 0.828874f, 1.132784f};
86
static const float exc_gain_quant_scal3[8]={0.061130f, 0.163546f, 0.310413f, 0.428220f, 0.555887f, 0.719055f, 0.938694f, 1.326874f};
87
static const float exc_gain_quant_scal1_bound[1]={0.87798f};
88
static const float exc_gain_quant_scal1[2]={0.70469f, 1.05127f};
89
90
110k
#define LSP_MARGIN .002f
91
8.63k
#define LSP_DELTA1 .2f
92
#define LSP_DELTA2 .05f
93
94
#endif
95
96
extern const spx_word16_t lag_window[];
97
extern const spx_word16_t lpc_window[];
98
99
#ifndef DISABLE_ENCODER
100
void *nb_encoder_init(const SpeexMode *m)
101
7.52k
{
102
7.52k
   EncState *st;
103
7.52k
   const SpeexNBMode *mode;
104
7.52k
   int i;
105
106
7.52k
   mode=(const SpeexNBMode *)m->mode;
107
7.52k
   st = (EncState*)speex_alloc(sizeof(EncState));
108
7.52k
   if (!st)
109
0
      return NULL;
110
7.52k
#if defined(VAR_ARRAYS) || defined (USE_ALLOCA)
111
7.52k
   st->stack = NULL;
112
#else
113
   st->stack = (char*)speex_alloc_scratch(NB_ENC_STACK);
114
#endif
115
116
7.52k
   st->mode=m;
117
118
7.52k
   st->gamma1=mode->gamma1;
119
7.52k
   st->gamma2=mode->gamma2;
120
7.52k
   st->lpc_floor = mode->lpc_floor;
121
122
7.52k
   st->submodes=mode->submodes;
123
7.52k
   st->submodeID=st->submodeSelect=mode->defaultSubmode;
124
7.52k
   st->bounded_pitch = 1;
125
126
7.52k
   st->encode_submode = 1;
127
128
#ifdef VORBIS_PSYCHO
129
   st->psy = vorbis_psy_init(8000, 256);
130
   st->curve = (float*)speex_alloc(128*sizeof(float));
131
   st->old_curve = (float*)speex_alloc(128*sizeof(float));
132
   st->psy_window = (float*)speex_alloc(256*sizeof(float));
133
#endif
134
135
7.52k
   st->cumul_gain = 1024;
136
137
7.52k
   st->window= lpc_window;
138
139
   /* Create the window for autocorrelation (lag-windowing) */
140
7.52k
   st->lagWindow = lag_window;
141
142
7.52k
   st->first = 1;
143
82.8k
   for (i=0;i<NB_ORDER;i++)
144
75.2k
      st->old_lsp[i]= DIV32(MULT16_16(QCONST16(3.1415927f, LSP_SHIFT), i+1), NB_ORDER+1);
145
146
7.52k
   st->innov_rms_save = NULL;
147
148
7.52k
#ifndef DISABLE_VBR
149
7.52k
   vbr_init(&st->vbr);
150
7.52k
   st->vbr_quality = 8;
151
7.52k
   st->vbr_enabled = 0;
152
7.52k
   st->vbr_max = 0;
153
7.52k
   st->vad_enabled = 0;
154
7.52k
   st->dtx_enabled = 0;
155
7.52k
   st->dtx_count=0;
156
7.52k
   st->abr_enabled = 0;
157
7.52k
   st->abr_drift = 0;
158
7.52k
   st->abr_drift2 = 0;
159
7.52k
#endif /* #ifndef DISABLE_VBR */
160
161
7.52k
   st->plc_tuning = 2;
162
7.52k
   st->complexity=2;
163
7.52k
   st->sampling_rate=8000;
164
7.52k
   st->isWideband = 0;
165
7.52k
   st->highpass_enabled = 1;
166
167
#ifdef ENABLE_VALGRIND
168
   VALGRIND_MAKE_MEM_DEFINED(st, NB_ENC_STACK);
169
#endif
170
7.52k
   return st;
171
7.52k
}
nb_encoder_init
Line
Count
Source
101
3.76k
{
102
3.76k
   EncState *st;
103
3.76k
   const SpeexNBMode *mode;
104
3.76k
   int i;
105
106
3.76k
   mode=(const SpeexNBMode *)m->mode;
107
3.76k
   st = (EncState*)speex_alloc(sizeof(EncState));
108
3.76k
   if (!st)
109
0
      return NULL;
110
3.76k
#if defined(VAR_ARRAYS) || defined (USE_ALLOCA)
111
3.76k
   st->stack = NULL;
112
#else
113
   st->stack = (char*)speex_alloc_scratch(NB_ENC_STACK);
114
#endif
115
116
3.76k
   st->mode=m;
117
118
3.76k
   st->gamma1=mode->gamma1;
119
3.76k
   st->gamma2=mode->gamma2;
120
3.76k
   st->lpc_floor = mode->lpc_floor;
121
122
3.76k
   st->submodes=mode->submodes;
123
3.76k
   st->submodeID=st->submodeSelect=mode->defaultSubmode;
124
3.76k
   st->bounded_pitch = 1;
125
126
3.76k
   st->encode_submode = 1;
127
128
#ifdef VORBIS_PSYCHO
129
   st->psy = vorbis_psy_init(8000, 256);
130
   st->curve = (float*)speex_alloc(128*sizeof(float));
131
   st->old_curve = (float*)speex_alloc(128*sizeof(float));
132
   st->psy_window = (float*)speex_alloc(256*sizeof(float));
133
#endif
134
135
3.76k
   st->cumul_gain = 1024;
136
137
3.76k
   st->window= lpc_window;
138
139
   /* Create the window for autocorrelation (lag-windowing) */
140
3.76k
   st->lagWindow = lag_window;
141
142
3.76k
   st->first = 1;
143
41.4k
   for (i=0;i<NB_ORDER;i++)
144
37.6k
      st->old_lsp[i]= DIV32(MULT16_16(QCONST16(3.1415927f, LSP_SHIFT), i+1), NB_ORDER+1);
145
146
3.76k
   st->innov_rms_save = NULL;
147
148
3.76k
#ifndef DISABLE_VBR
149
3.76k
   vbr_init(&st->vbr);
150
3.76k
   st->vbr_quality = 8;
151
3.76k
   st->vbr_enabled = 0;
152
3.76k
   st->vbr_max = 0;
153
3.76k
   st->vad_enabled = 0;
154
3.76k
   st->dtx_enabled = 0;
155
3.76k
   st->dtx_count=0;
156
3.76k
   st->abr_enabled = 0;
157
3.76k
   st->abr_drift = 0;
158
3.76k
   st->abr_drift2 = 0;
159
3.76k
#endif /* #ifndef DISABLE_VBR */
160
161
3.76k
   st->plc_tuning = 2;
162
3.76k
   st->complexity=2;
163
3.76k
   st->sampling_rate=8000;
164
3.76k
   st->isWideband = 0;
165
3.76k
   st->highpass_enabled = 1;
166
167
#ifdef ENABLE_VALGRIND
168
   VALGRIND_MAKE_MEM_DEFINED(st, NB_ENC_STACK);
169
#endif
170
3.76k
   return st;
171
3.76k
}
nb_encoder_init
Line
Count
Source
101
3.76k
{
102
3.76k
   EncState *st;
103
3.76k
   const SpeexNBMode *mode;
104
3.76k
   int i;
105
106
3.76k
   mode=(const SpeexNBMode *)m->mode;
107
3.76k
   st = (EncState*)speex_alloc(sizeof(EncState));
108
3.76k
   if (!st)
109
0
      return NULL;
110
3.76k
#if defined(VAR_ARRAYS) || defined (USE_ALLOCA)
111
3.76k
   st->stack = NULL;
112
#else
113
   st->stack = (char*)speex_alloc_scratch(NB_ENC_STACK);
114
#endif
115
116
3.76k
   st->mode=m;
117
118
3.76k
   st->gamma1=mode->gamma1;
119
3.76k
   st->gamma2=mode->gamma2;
120
3.76k
   st->lpc_floor = mode->lpc_floor;
121
122
3.76k
   st->submodes=mode->submodes;
123
3.76k
   st->submodeID=st->submodeSelect=mode->defaultSubmode;
124
3.76k
   st->bounded_pitch = 1;
125
126
3.76k
   st->encode_submode = 1;
127
128
#ifdef VORBIS_PSYCHO
129
   st->psy = vorbis_psy_init(8000, 256);
130
   st->curve = (float*)speex_alloc(128*sizeof(float));
131
   st->old_curve = (float*)speex_alloc(128*sizeof(float));
132
   st->psy_window = (float*)speex_alloc(256*sizeof(float));
133
#endif
134
135
3.76k
   st->cumul_gain = 1024;
136
137
3.76k
   st->window= lpc_window;
138
139
   /* Create the window for autocorrelation (lag-windowing) */
140
3.76k
   st->lagWindow = lag_window;
141
142
3.76k
   st->first = 1;
143
41.4k
   for (i=0;i<NB_ORDER;i++)
144
37.6k
      st->old_lsp[i]= DIV32(MULT16_16(QCONST16(3.1415927f, LSP_SHIFT), i+1), NB_ORDER+1);
145
146
3.76k
   st->innov_rms_save = NULL;
147
148
3.76k
#ifndef DISABLE_VBR
149
3.76k
   vbr_init(&st->vbr);
150
3.76k
   st->vbr_quality = 8;
151
3.76k
   st->vbr_enabled = 0;
152
3.76k
   st->vbr_max = 0;
153
3.76k
   st->vad_enabled = 0;
154
3.76k
   st->dtx_enabled = 0;
155
3.76k
   st->dtx_count=0;
156
3.76k
   st->abr_enabled = 0;
157
3.76k
   st->abr_drift = 0;
158
3.76k
   st->abr_drift2 = 0;
159
3.76k
#endif /* #ifndef DISABLE_VBR */
160
161
3.76k
   st->plc_tuning = 2;
162
3.76k
   st->complexity=2;
163
3.76k
   st->sampling_rate=8000;
164
3.76k
   st->isWideband = 0;
165
3.76k
   st->highpass_enabled = 1;
166
167
#ifdef ENABLE_VALGRIND
168
   VALGRIND_MAKE_MEM_DEFINED(st, NB_ENC_STACK);
169
#endif
170
3.76k
   return st;
171
3.76k
}
172
173
void nb_encoder_destroy(void *state)
174
3.76k
{
175
3.76k
   EncState *st=(EncState *)state;
176
   /* Free all allocated memory */
177
#if !(defined(VAR_ARRAYS) || defined (USE_ALLOCA))
178
   speex_free_scratch(st->stack);
179
#endif
180
181
3.76k
#ifndef DISABLE_VBR
182
3.76k
   vbr_destroy(&st->vbr);
183
3.76k
#endif /* #ifndef DISABLE_VBR */
184
185
#ifdef VORBIS_PSYCHO
186
   vorbis_psy_destroy(st->psy);
187
   speex_free (st->curve);
188
   speex_free (st->old_curve);
189
   speex_free (st->psy_window);
190
#endif
191
192
   /*Free state memory... should be last*/
193
3.76k
   speex_free(st);
194
3.76k
}
195
196
197
int nb_encoder_ctl(void *state, int request, void *ptr)
198
588k
{
199
588k
   EncState *st;
200
588k
   st=(EncState*)state;
201
588k
   switch(request)
202
588k
   {
203
15.6k
   case SPEEX_GET_FRAME_SIZE:
204
15.6k
      (*(spx_int32_t*)ptr) = NB_FRAME_SIZE;
205
15.6k
      break;
206
0
   case SPEEX_SET_LOW_MODE:
207
79.3k
   case SPEEX_SET_MODE:
208
79.3k
      st->submodeSelect = st->submodeID = (*(spx_int32_t*)ptr);
209
79.3k
      break;
210
20.3k
   case SPEEX_GET_LOW_MODE:
211
20.3k
   case SPEEX_GET_MODE:
212
20.3k
      (*(spx_int32_t*)ptr) = st->submodeID;
213
20.3k
      break;
214
0
#ifndef DISABLE_VBR
215
4.78k
      case SPEEX_SET_VBR:
216
4.78k
      st->vbr_enabled = (*(spx_int32_t*)ptr);
217
4.78k
      break;
218
0
   case SPEEX_GET_VBR:
219
0
      (*(spx_int32_t*)ptr) = st->vbr_enabled;
220
0
      break;
221
3.45k
   case SPEEX_SET_VAD:
222
3.45k
      st->vad_enabled = (*(spx_int32_t*)ptr);
223
3.45k
      break;
224
0
   case SPEEX_GET_VAD:
225
0
      (*(spx_int32_t*)ptr) = st->vad_enabled;
226
0
      break;
227
3.30k
   case SPEEX_SET_DTX:
228
3.30k
      st->dtx_enabled = (*(spx_int32_t*)ptr);
229
3.30k
      break;
230
0
   case SPEEX_GET_DTX:
231
0
      (*(spx_int32_t*)ptr) = st->dtx_enabled;
232
0
      break;
233
1.04k
   case SPEEX_SET_ABR:
234
1.04k
      st->abr_enabled = (*(spx_int32_t*)ptr);
235
1.04k
      st->vbr_enabled = st->abr_enabled!=0;
236
1.04k
      if (st->vbr_enabled)
237
1.04k
      {
238
1.04k
         spx_int32_t i=10;
239
1.04k
         spx_int32_t rate, target;
240
1.04k
         float vbr_qual;
241
1.04k
         target = (*(spx_int32_t*)ptr);
242
12.5k
         while (i>=0)
243
11.5k
         {
244
11.5k
            speex_encoder_ctl(st, SPEEX_SET_QUALITY, &i);
245
11.5k
            speex_encoder_ctl(st, SPEEX_GET_BITRATE, &rate);
246
11.5k
            if (rate <= target)
247
0
               break;
248
11.5k
            i--;
249
11.5k
         }
250
1.04k
         vbr_qual=i;
251
1.04k
         if (vbr_qual<0)
252
1.04k
            vbr_qual=0;
253
1.04k
         speex_encoder_ctl(st, SPEEX_SET_VBR_QUALITY, &vbr_qual);
254
1.04k
         st->abr_count=0;
255
1.04k
         st->abr_drift=0;
256
1.04k
         st->abr_drift2=0;
257
1.04k
      }
258
259
1.04k
      break;
260
0
   case SPEEX_GET_ABR:
261
0
      (*(spx_int32_t*)ptr) = st->abr_enabled;
262
0
      break;
263
0
#endif /* #ifndef DISABLE_VBR */
264
0
#if !defined(DISABLE_VBR) && !defined(DISABLE_FLOAT_API)
265
5.83k
   case SPEEX_SET_VBR_QUALITY:
266
5.83k
      st->vbr_quality = (*(float*)ptr);
267
5.83k
      if (st->vbr_quality < 0)
268
0
          st->vbr_quality = 0;
269
5.83k
      else if (st->vbr_quality > 10)
270
0
          st->vbr_quality = 10;
271
5.83k
      break;
272
0
   case SPEEX_GET_VBR_QUALITY:
273
0
      (*(float*)ptr) = st->vbr_quality;
274
0
      break;
275
0
#endif /* !defined(DISABLE_VBR) && !defined(DISABLE_FLOAT_API) */
276
134k
   case SPEEX_SET_QUALITY:
277
134k
      {
278
134k
         int quality = (*(spx_int32_t*)ptr);
279
134k
         if (quality < 0)
280
0
            quality = 0;
281
134k
         if (quality > 10)
282
0
            quality = 10;
283
134k
         st->submodeSelect = st->submodeID = ((const SpeexNBMode*)(st->mode->mode))->quality_map[quality];
284
134k
      }
285
134k
      break;
286
7.52k
   case SPEEX_SET_COMPLEXITY:
287
7.52k
      st->complexity = (*(spx_int32_t*)ptr);
288
7.52k
      if (st->complexity<0)
289
0
         st->complexity=0;
290
7.52k
      break;
291
0
   case SPEEX_GET_COMPLEXITY:
292
0
      (*(spx_int32_t*)ptr) = st->complexity;
293
0
      break;
294
12.4k
   case SPEEX_SET_BITRATE:
295
12.4k
      {
296
12.4k
         spx_int32_t i=10;
297
12.4k
         spx_int32_t rate, target;
298
12.4k
         target = (*(spx_int32_t*)ptr);
299
124k
         while (i>=0)
300
117k
         {
301
117k
            speex_encoder_ctl(st, SPEEX_SET_QUALITY, &i);
302
117k
            speex_encoder_ctl(st, SPEEX_GET_BITRATE, &rate);
303
117k
            if (rate <= target)
304
5.50k
               break;
305
111k
            i--;
306
111k
         }
307
12.4k
      }
308
12.4k
      break;
309
207k
   case SPEEX_GET_BITRATE:
310
207k
      if (st->submodes[st->submodeID])
311
202k
         (*(spx_int32_t*)ptr) = st->sampling_rate*SUBMODE(bits_per_frame)/NB_FRAME_SIZE;
312
5.06k
      else
313
5.06k
         (*(spx_int32_t*)ptr) = st->sampling_rate*(NB_SUBMODE_BITS+1)/NB_FRAME_SIZE;
314
207k
      break;
315
7.52k
   case SPEEX_SET_SAMPLING_RATE:
316
7.52k
      st->sampling_rate = (*(spx_int32_t*)ptr);
317
7.52k
      break;
318
3.74k
   case SPEEX_GET_SAMPLING_RATE:
319
3.74k
      (*(spx_int32_t*)ptr)=st->sampling_rate;
320
3.74k
      break;
321
0
   case SPEEX_RESET_STATE:
322
0
      {
323
0
         int i;
324
0
         st->bounded_pitch = 1;
325
0
         st->first = 1;
326
0
         for (i=0;i<NB_ORDER;i++)
327
0
            st->old_lsp[i]= DIV32(MULT16_16(QCONST16(3.1415927f, LSP_SHIFT), i+1), NB_ORDER+1);
328
0
         for (i=0;i<NB_ORDER;i++)
329
0
            st->mem_sw[i]=st->mem_sw_whole[i]=st->mem_sp[i]=st->mem_exc[i]=0;
330
0
         for (i=0;i<NB_FRAME_SIZE+NB_PITCH_END+1;i++)
331
0
            st->excBuf[i]=st->swBuf[i]=0;
332
0
         for (i=0;i<NB_WINDOW_SIZE-NB_FRAME_SIZE;i++)
333
0
            st->winBuf[i]=0;
334
0
      }
335
0
      break;
336
0
   case SPEEX_SET_SUBMODE_ENCODING:
337
0
      st->encode_submode = (*(spx_int32_t*)ptr);
338
0
      break;
339
0
   case SPEEX_GET_SUBMODE_ENCODING:
340
0
      (*(spx_int32_t*)ptr) = st->encode_submode;
341
0
      break;
342
7.52k
   case SPEEX_GET_LOOKAHEAD:
343
7.52k
      (*(spx_int32_t*)ptr)=(NB_WINDOW_SIZE-NB_FRAME_SIZE);
344
7.52k
      break;
345
0
   case SPEEX_SET_PLC_TUNING:
346
0
      st->plc_tuning = (*(spx_int32_t*)ptr);
347
0
      if (st->plc_tuning>100)
348
0
         st->plc_tuning=100;
349
0
      break;
350
0
   case SPEEX_GET_PLC_TUNING:
351
0
      (*(spx_int32_t*)ptr)=(st->plc_tuning);
352
0
      break;
353
0
#ifndef DISABLE_VBR
354
3.52k
   case SPEEX_SET_VBR_MAX_BITRATE:
355
3.52k
      st->vbr_max = (*(spx_int32_t*)ptr);
356
3.52k
      break;
357
0
   case SPEEX_GET_VBR_MAX_BITRATE:
358
0
      (*(spx_int32_t*)ptr) = st->vbr_max;
359
0
      break;
360
0
#endif /* #ifndef DISABLE_VBR */
361
7.52k
   case SPEEX_SET_HIGHPASS:
362
7.52k
      st->highpass_enabled = (*(spx_int32_t*)ptr);
363
7.52k
      break;
364
0
   case SPEEX_GET_HIGHPASS:
365
0
      (*(spx_int32_t*)ptr) = st->highpass_enabled;
366
0
      break;
367
368
   /* This is all internal stuff past this point */
369
15.4k
   case SPEEX_GET_PI_GAIN:
370
15.4k
      {
371
15.4k
         int i;
372
15.4k
         spx_word32_t *g = (spx_word32_t*)ptr;
373
77.2k
         for (i=0;i<NB_NB_SUBFRAMES;i++)
374
61.7k
            g[i]=st->pi_gain[i];
375
15.4k
      }
376
15.4k
      break;
377
15.4k
   case SPEEX_GET_EXC:
378
15.4k
      {
379
15.4k
         int i;
380
77.2k
         for (i=0;i<NB_NB_SUBFRAMES;i++)
381
61.7k
            ((spx_word16_t*)ptr)[i] = compute_rms16(st->exc+i*NB_SUBFRAME_SIZE, NB_SUBFRAME_SIZE);
382
15.4k
      }
383
15.4k
      break;
384
0
#ifndef DISABLE_VBR
385
10.5k
   case SPEEX_GET_RELATIVE_QUALITY:
386
10.5k
      (*(float*)ptr)=st->relative_quality;
387
10.5k
      break;
388
0
#endif /* #ifndef DISABLE_VBR */
389
15.4k
   case SPEEX_SET_INNOVATION_SAVE:
390
15.4k
      st->innov_rms_save = (spx_word16_t*)ptr;
391
15.4k
      break;
392
5.39k
   case SPEEX_SET_WIDEBAND:
393
5.39k
      st->isWideband = *((spx_int32_t*)ptr);
394
5.39k
      break;
395
0
   case SPEEX_GET_STACK:
396
0
      *((char**)ptr) = st->stack;
397
0
      break;
398
0
   default:
399
0
      speex_warning_int("Unknown nb_ctl request: ", request);
400
0
      return -1;
401
588k
   }
402
588k
   return 0;
403
588k
}
nb_encoder_ctl
Line
Count
Source
198
294k
{
199
294k
   EncState *st;
200
294k
   st=(EncState*)state;
201
294k
   switch(request)
202
294k
   {
203
7.83k
   case SPEEX_GET_FRAME_SIZE:
204
7.83k
      (*(spx_int32_t*)ptr) = NB_FRAME_SIZE;
205
7.83k
      break;
206
0
   case SPEEX_SET_LOW_MODE:
207
39.6k
   case SPEEX_SET_MODE:
208
39.6k
      st->submodeSelect = st->submodeID = (*(spx_int32_t*)ptr);
209
39.6k
      break;
210
10.1k
   case SPEEX_GET_LOW_MODE:
211
10.1k
   case SPEEX_GET_MODE:
212
10.1k
      (*(spx_int32_t*)ptr) = st->submodeID;
213
10.1k
      break;
214
0
#ifndef DISABLE_VBR
215
2.39k
      case SPEEX_SET_VBR:
216
2.39k
      st->vbr_enabled = (*(spx_int32_t*)ptr);
217
2.39k
      break;
218
0
   case SPEEX_GET_VBR:
219
0
      (*(spx_int32_t*)ptr) = st->vbr_enabled;
220
0
      break;
221
1.72k
   case SPEEX_SET_VAD:
222
1.72k
      st->vad_enabled = (*(spx_int32_t*)ptr);
223
1.72k
      break;
224
0
   case SPEEX_GET_VAD:
225
0
      (*(spx_int32_t*)ptr) = st->vad_enabled;
226
0
      break;
227
1.65k
   case SPEEX_SET_DTX:
228
1.65k
      st->dtx_enabled = (*(spx_int32_t*)ptr);
229
1.65k
      break;
230
0
   case SPEEX_GET_DTX:
231
0
      (*(spx_int32_t*)ptr) = st->dtx_enabled;
232
0
      break;
233
523
   case SPEEX_SET_ABR:
234
523
      st->abr_enabled = (*(spx_int32_t*)ptr);
235
523
      st->vbr_enabled = st->abr_enabled!=0;
236
523
      if (st->vbr_enabled)
237
523
      {
238
523
         spx_int32_t i=10;
239
523
         spx_int32_t rate, target;
240
523
         float vbr_qual;
241
523
         target = (*(spx_int32_t*)ptr);
242
6.27k
         while (i>=0)
243
5.75k
         {
244
5.75k
            speex_encoder_ctl(st, SPEEX_SET_QUALITY, &i);
245
5.75k
            speex_encoder_ctl(st, SPEEX_GET_BITRATE, &rate);
246
5.75k
            if (rate <= target)
247
0
               break;
248
5.75k
            i--;
249
5.75k
         }
250
523
         vbr_qual=i;
251
523
         if (vbr_qual<0)
252
523
            vbr_qual=0;
253
523
         speex_encoder_ctl(st, SPEEX_SET_VBR_QUALITY, &vbr_qual);
254
523
         st->abr_count=0;
255
523
         st->abr_drift=0;
256
523
         st->abr_drift2=0;
257
523
      }
258
259
523
      break;
260
0
   case SPEEX_GET_ABR:
261
0
      (*(spx_int32_t*)ptr) = st->abr_enabled;
262
0
      break;
263
0
#endif /* #ifndef DISABLE_VBR */
264
0
#if !defined(DISABLE_VBR) && !defined(DISABLE_FLOAT_API)
265
2.91k
   case SPEEX_SET_VBR_QUALITY:
266
2.91k
      st->vbr_quality = (*(float*)ptr);
267
2.91k
      if (st->vbr_quality < 0)
268
0
          st->vbr_quality = 0;
269
2.91k
      else if (st->vbr_quality > 10)
270
0
          st->vbr_quality = 10;
271
2.91k
      break;
272
0
   case SPEEX_GET_VBR_QUALITY:
273
0
      (*(float*)ptr) = st->vbr_quality;
274
0
      break;
275
0
#endif /* !defined(DISABLE_VBR) && !defined(DISABLE_FLOAT_API) */
276
67.1k
   case SPEEX_SET_QUALITY:
277
67.1k
      {
278
67.1k
         int quality = (*(spx_int32_t*)ptr);
279
67.1k
         if (quality < 0)
280
0
            quality = 0;
281
67.1k
         if (quality > 10)
282
0
            quality = 10;
283
67.1k
         st->submodeSelect = st->submodeID = ((const SpeexNBMode*)(st->mode->mode))->quality_map[quality];
284
67.1k
      }
285
67.1k
      break;
286
3.76k
   case SPEEX_SET_COMPLEXITY:
287
3.76k
      st->complexity = (*(spx_int32_t*)ptr);
288
3.76k
      if (st->complexity<0)
289
0
         st->complexity=0;
290
3.76k
      break;
291
0
   case SPEEX_GET_COMPLEXITY:
292
0
      (*(spx_int32_t*)ptr) = st->complexity;
293
0
      break;
294
6.23k
   case SPEEX_SET_BITRATE:
295
6.23k
      {
296
6.23k
         spx_int32_t i=10;
297
6.23k
         spx_int32_t rate, target;
298
6.23k
         target = (*(spx_int32_t*)ptr);
299
62.1k
         while (i>=0)
300
58.7k
         {
301
58.7k
            speex_encoder_ctl(st, SPEEX_SET_QUALITY, &i);
302
58.7k
            speex_encoder_ctl(st, SPEEX_GET_BITRATE, &rate);
303
58.7k
            if (rate <= target)
304
2.75k
               break;
305
55.9k
            i--;
306
55.9k
         }
307
6.23k
      }
308
6.23k
      break;
309
103k
   case SPEEX_GET_BITRATE:
310
103k
      if (st->submodes[st->submodeID])
311
101k
         (*(spx_int32_t*)ptr) = st->sampling_rate*SUBMODE(bits_per_frame)/NB_FRAME_SIZE;
312
2.53k
      else
313
2.53k
         (*(spx_int32_t*)ptr) = st->sampling_rate*(NB_SUBMODE_BITS+1)/NB_FRAME_SIZE;
314
103k
      break;
315
3.76k
   case SPEEX_SET_SAMPLING_RATE:
316
3.76k
      st->sampling_rate = (*(spx_int32_t*)ptr);
317
3.76k
      break;
318
1.87k
   case SPEEX_GET_SAMPLING_RATE:
319
1.87k
      (*(spx_int32_t*)ptr)=st->sampling_rate;
320
1.87k
      break;
321
0
   case SPEEX_RESET_STATE:
322
0
      {
323
0
         int i;
324
0
         st->bounded_pitch = 1;
325
0
         st->first = 1;
326
0
         for (i=0;i<NB_ORDER;i++)
327
0
            st->old_lsp[i]= DIV32(MULT16_16(QCONST16(3.1415927f, LSP_SHIFT), i+1), NB_ORDER+1);
328
0
         for (i=0;i<NB_ORDER;i++)
329
0
            st->mem_sw[i]=st->mem_sw_whole[i]=st->mem_sp[i]=st->mem_exc[i]=0;
330
0
         for (i=0;i<NB_FRAME_SIZE+NB_PITCH_END+1;i++)
331
0
            st->excBuf[i]=st->swBuf[i]=0;
332
0
         for (i=0;i<NB_WINDOW_SIZE-NB_FRAME_SIZE;i++)
333
0
            st->winBuf[i]=0;
334
0
      }
335
0
      break;
336
0
   case SPEEX_SET_SUBMODE_ENCODING:
337
0
      st->encode_submode = (*(spx_int32_t*)ptr);
338
0
      break;
339
0
   case SPEEX_GET_SUBMODE_ENCODING:
340
0
      (*(spx_int32_t*)ptr) = st->encode_submode;
341
0
      break;
342
3.76k
   case SPEEX_GET_LOOKAHEAD:
343
3.76k
      (*(spx_int32_t*)ptr)=(NB_WINDOW_SIZE-NB_FRAME_SIZE);
344
3.76k
      break;
345
0
   case SPEEX_SET_PLC_TUNING:
346
0
      st->plc_tuning = (*(spx_int32_t*)ptr);
347
0
      if (st->plc_tuning>100)
348
0
         st->plc_tuning=100;
349
0
      break;
350
0
   case SPEEX_GET_PLC_TUNING:
351
0
      (*(spx_int32_t*)ptr)=(st->plc_tuning);
352
0
      break;
353
0
#ifndef DISABLE_VBR
354
1.76k
   case SPEEX_SET_VBR_MAX_BITRATE:
355
1.76k
      st->vbr_max = (*(spx_int32_t*)ptr);
356
1.76k
      break;
357
0
   case SPEEX_GET_VBR_MAX_BITRATE:
358
0
      (*(spx_int32_t*)ptr) = st->vbr_max;
359
0
      break;
360
0
#endif /* #ifndef DISABLE_VBR */
361
3.76k
   case SPEEX_SET_HIGHPASS:
362
3.76k
      st->highpass_enabled = (*(spx_int32_t*)ptr);
363
3.76k
      break;
364
0
   case SPEEX_GET_HIGHPASS:
365
0
      (*(spx_int32_t*)ptr) = st->highpass_enabled;
366
0
      break;
367
368
   /* This is all internal stuff past this point */
369
7.72k
   case SPEEX_GET_PI_GAIN:
370
7.72k
      {
371
7.72k
         int i;
372
7.72k
         spx_word32_t *g = (spx_word32_t*)ptr;
373
38.6k
         for (i=0;i<NB_NB_SUBFRAMES;i++)
374
30.8k
            g[i]=st->pi_gain[i];
375
7.72k
      }
376
7.72k
      break;
377
7.72k
   case SPEEX_GET_EXC:
378
7.72k
      {
379
7.72k
         int i;
380
38.6k
         for (i=0;i<NB_NB_SUBFRAMES;i++)
381
30.8k
            ((spx_word16_t*)ptr)[i] = compute_rms16(st->exc+i*NB_SUBFRAME_SIZE, NB_SUBFRAME_SIZE);
382
7.72k
      }
383
7.72k
      break;
384
0
#ifndef DISABLE_VBR
385
5.26k
   case SPEEX_GET_RELATIVE_QUALITY:
386
5.26k
      (*(float*)ptr)=st->relative_quality;
387
5.26k
      break;
388
0
#endif /* #ifndef DISABLE_VBR */
389
7.72k
   case SPEEX_SET_INNOVATION_SAVE:
390
7.72k
      st->innov_rms_save = (spx_word16_t*)ptr;
391
7.72k
      break;
392
2.69k
   case SPEEX_SET_WIDEBAND:
393
2.69k
      st->isWideband = *((spx_int32_t*)ptr);
394
2.69k
      break;
395
0
   case SPEEX_GET_STACK:
396
0
      *((char**)ptr) = st->stack;
397
0
      break;
398
0
   default:
399
0
      speex_warning_int("Unknown nb_ctl request: ", request);
400
0
      return -1;
401
294k
   }
402
294k
   return 0;
403
294k
}
nb_encoder_ctl
Line
Count
Source
198
294k
{
199
294k
   EncState *st;
200
294k
   st=(EncState*)state;
201
294k
   switch(request)
202
294k
   {
203
7.83k
   case SPEEX_GET_FRAME_SIZE:
204
7.83k
      (*(spx_int32_t*)ptr) = NB_FRAME_SIZE;
205
7.83k
      break;
206
0
   case SPEEX_SET_LOW_MODE:
207
39.6k
   case SPEEX_SET_MODE:
208
39.6k
      st->submodeSelect = st->submodeID = (*(spx_int32_t*)ptr);
209
39.6k
      break;
210
10.1k
   case SPEEX_GET_LOW_MODE:
211
10.1k
   case SPEEX_GET_MODE:
212
10.1k
      (*(spx_int32_t*)ptr) = st->submodeID;
213
10.1k
      break;
214
0
#ifndef DISABLE_VBR
215
2.39k
      case SPEEX_SET_VBR:
216
2.39k
      st->vbr_enabled = (*(spx_int32_t*)ptr);
217
2.39k
      break;
218
0
   case SPEEX_GET_VBR:
219
0
      (*(spx_int32_t*)ptr) = st->vbr_enabled;
220
0
      break;
221
1.72k
   case SPEEX_SET_VAD:
222
1.72k
      st->vad_enabled = (*(spx_int32_t*)ptr);
223
1.72k
      break;
224
0
   case SPEEX_GET_VAD:
225
0
      (*(spx_int32_t*)ptr) = st->vad_enabled;
226
0
      break;
227
1.65k
   case SPEEX_SET_DTX:
228
1.65k
      st->dtx_enabled = (*(spx_int32_t*)ptr);
229
1.65k
      break;
230
0
   case SPEEX_GET_DTX:
231
0
      (*(spx_int32_t*)ptr) = st->dtx_enabled;
232
0
      break;
233
523
   case SPEEX_SET_ABR:
234
523
      st->abr_enabled = (*(spx_int32_t*)ptr);
235
523
      st->vbr_enabled = st->abr_enabled!=0;
236
523
      if (st->vbr_enabled)
237
523
      {
238
523
         spx_int32_t i=10;
239
523
         spx_int32_t rate, target;
240
523
         float vbr_qual;
241
523
         target = (*(spx_int32_t*)ptr);
242
6.27k
         while (i>=0)
243
5.75k
         {
244
5.75k
            speex_encoder_ctl(st, SPEEX_SET_QUALITY, &i);
245
5.75k
            speex_encoder_ctl(st, SPEEX_GET_BITRATE, &rate);
246
5.75k
            if (rate <= target)
247
0
               break;
248
5.75k
            i--;
249
5.75k
         }
250
523
         vbr_qual=i;
251
523
         if (vbr_qual<0)
252
523
            vbr_qual=0;
253
523
         speex_encoder_ctl(st, SPEEX_SET_VBR_QUALITY, &vbr_qual);
254
523
         st->abr_count=0;
255
523
         st->abr_drift=0;
256
523
         st->abr_drift2=0;
257
523
      }
258
259
523
      break;
260
0
   case SPEEX_GET_ABR:
261
0
      (*(spx_int32_t*)ptr) = st->abr_enabled;
262
0
      break;
263
0
#endif /* #ifndef DISABLE_VBR */
264
0
#if !defined(DISABLE_VBR) && !defined(DISABLE_FLOAT_API)
265
2.91k
   case SPEEX_SET_VBR_QUALITY:
266
2.91k
      st->vbr_quality = (*(float*)ptr);
267
2.91k
      if (st->vbr_quality < 0)
268
0
          st->vbr_quality = 0;
269
2.91k
      else if (st->vbr_quality > 10)
270
0
          st->vbr_quality = 10;
271
2.91k
      break;
272
0
   case SPEEX_GET_VBR_QUALITY:
273
0
      (*(float*)ptr) = st->vbr_quality;
274
0
      break;
275
0
#endif /* !defined(DISABLE_VBR) && !defined(DISABLE_FLOAT_API) */
276
67.1k
   case SPEEX_SET_QUALITY:
277
67.1k
      {
278
67.1k
         int quality = (*(spx_int32_t*)ptr);
279
67.1k
         if (quality < 0)
280
0
            quality = 0;
281
67.1k
         if (quality > 10)
282
0
            quality = 10;
283
67.1k
         st->submodeSelect = st->submodeID = ((const SpeexNBMode*)(st->mode->mode))->quality_map[quality];
284
67.1k
      }
285
67.1k
      break;
286
3.76k
   case SPEEX_SET_COMPLEXITY:
287
3.76k
      st->complexity = (*(spx_int32_t*)ptr);
288
3.76k
      if (st->complexity<0)
289
0
         st->complexity=0;
290
3.76k
      break;
291
0
   case SPEEX_GET_COMPLEXITY:
292
0
      (*(spx_int32_t*)ptr) = st->complexity;
293
0
      break;
294
6.23k
   case SPEEX_SET_BITRATE:
295
6.23k
      {
296
6.23k
         spx_int32_t i=10;
297
6.23k
         spx_int32_t rate, target;
298
6.23k
         target = (*(spx_int32_t*)ptr);
299
62.1k
         while (i>=0)
300
58.7k
         {
301
58.7k
            speex_encoder_ctl(st, SPEEX_SET_QUALITY, &i);
302
58.7k
            speex_encoder_ctl(st, SPEEX_GET_BITRATE, &rate);
303
58.7k
            if (rate <= target)
304
2.75k
               break;
305
55.9k
            i--;
306
55.9k
         }
307
6.23k
      }
308
6.23k
      break;
309
103k
   case SPEEX_GET_BITRATE:
310
103k
      if (st->submodes[st->submodeID])
311
101k
         (*(spx_int32_t*)ptr) = st->sampling_rate*SUBMODE(bits_per_frame)/NB_FRAME_SIZE;
312
2.53k
      else
313
2.53k
         (*(spx_int32_t*)ptr) = st->sampling_rate*(NB_SUBMODE_BITS+1)/NB_FRAME_SIZE;
314
103k
      break;
315
3.76k
   case SPEEX_SET_SAMPLING_RATE:
316
3.76k
      st->sampling_rate = (*(spx_int32_t*)ptr);
317
3.76k
      break;
318
1.87k
   case SPEEX_GET_SAMPLING_RATE:
319
1.87k
      (*(spx_int32_t*)ptr)=st->sampling_rate;
320
1.87k
      break;
321
0
   case SPEEX_RESET_STATE:
322
0
      {
323
0
         int i;
324
0
         st->bounded_pitch = 1;
325
0
         st->first = 1;
326
0
         for (i=0;i<NB_ORDER;i++)
327
0
            st->old_lsp[i]= DIV32(MULT16_16(QCONST16(3.1415927f, LSP_SHIFT), i+1), NB_ORDER+1);
328
0
         for (i=0;i<NB_ORDER;i++)
329
0
            st->mem_sw[i]=st->mem_sw_whole[i]=st->mem_sp[i]=st->mem_exc[i]=0;
330
0
         for (i=0;i<NB_FRAME_SIZE+NB_PITCH_END+1;i++)
331
0
            st->excBuf[i]=st->swBuf[i]=0;
332
0
         for (i=0;i<NB_WINDOW_SIZE-NB_FRAME_SIZE;i++)
333
0
            st->winBuf[i]=0;
334
0
      }
335
0
      break;
336
0
   case SPEEX_SET_SUBMODE_ENCODING:
337
0
      st->encode_submode = (*(spx_int32_t*)ptr);
338
0
      break;
339
0
   case SPEEX_GET_SUBMODE_ENCODING:
340
0
      (*(spx_int32_t*)ptr) = st->encode_submode;
341
0
      break;
342
3.76k
   case SPEEX_GET_LOOKAHEAD:
343
3.76k
      (*(spx_int32_t*)ptr)=(NB_WINDOW_SIZE-NB_FRAME_SIZE);
344
3.76k
      break;
345
0
   case SPEEX_SET_PLC_TUNING:
346
0
      st->plc_tuning = (*(spx_int32_t*)ptr);
347
0
      if (st->plc_tuning>100)
348
0
         st->plc_tuning=100;
349
0
      break;
350
0
   case SPEEX_GET_PLC_TUNING:
351
0
      (*(spx_int32_t*)ptr)=(st->plc_tuning);
352
0
      break;
353
0
#ifndef DISABLE_VBR
354
1.76k
   case SPEEX_SET_VBR_MAX_BITRATE:
355
1.76k
      st->vbr_max = (*(spx_int32_t*)ptr);
356
1.76k
      break;
357
0
   case SPEEX_GET_VBR_MAX_BITRATE:
358
0
      (*(spx_int32_t*)ptr) = st->vbr_max;
359
0
      break;
360
0
#endif /* #ifndef DISABLE_VBR */
361
3.76k
   case SPEEX_SET_HIGHPASS:
362
3.76k
      st->highpass_enabled = (*(spx_int32_t*)ptr);
363
3.76k
      break;
364
0
   case SPEEX_GET_HIGHPASS:
365
0
      (*(spx_int32_t*)ptr) = st->highpass_enabled;
366
0
      break;
367
368
   /* This is all internal stuff past this point */
369
7.72k
   case SPEEX_GET_PI_GAIN:
370
7.72k
      {
371
7.72k
         int i;
372
7.72k
         spx_word32_t *g = (spx_word32_t*)ptr;
373
38.6k
         for (i=0;i<NB_NB_SUBFRAMES;i++)
374
30.8k
            g[i]=st->pi_gain[i];
375
7.72k
      }
376
7.72k
      break;
377
7.72k
   case SPEEX_GET_EXC:
378
7.72k
      {
379
7.72k
         int i;
380
38.6k
         for (i=0;i<NB_NB_SUBFRAMES;i++)
381
30.8k
            ((spx_word16_t*)ptr)[i] = compute_rms16(st->exc+i*NB_SUBFRAME_SIZE, NB_SUBFRAME_SIZE);
382
7.72k
      }
383
7.72k
      break;
384
0
#ifndef DISABLE_VBR
385
5.26k
   case SPEEX_GET_RELATIVE_QUALITY:
386
5.26k
      (*(float*)ptr)=st->relative_quality;
387
5.26k
      break;
388
0
#endif /* #ifndef DISABLE_VBR */
389
7.72k
   case SPEEX_SET_INNOVATION_SAVE:
390
7.72k
      st->innov_rms_save = (spx_word16_t*)ptr;
391
7.72k
      break;
392
2.69k
   case SPEEX_SET_WIDEBAND:
393
2.69k
      st->isWideband = *((spx_int32_t*)ptr);
394
2.69k
      break;
395
0
   case SPEEX_GET_STACK:
396
0
      *((char**)ptr) = st->stack;
397
0
      break;
398
0
   default:
399
0
      speex_warning_int("Unknown nb_ctl request: ", request);
400
0
      return -1;
401
294k
   }
402
294k
   return 0;
403
294k
}
404
405
406
int nb_encode(void *state, void *vin, SpeexBits *bits)
407
26.6k
{
408
26.6k
   EncState *st;
409
26.6k
   int i, sub, roots;
410
26.6k
   int ol_pitch;
411
26.6k
   spx_word16_t ol_pitch_coef;
412
26.6k
   spx_word32_t ol_gain;
413
26.6k
   VARDECL(spx_word16_t *target);
414
26.6k
   VARDECL(spx_sig_t *innov);
415
26.6k
   VARDECL(spx_word32_t *exc32);
416
26.6k
   VARDECL(spx_mem_t *mem);
417
26.6k
   VARDECL(spx_coef_t *bw_lpc1);
418
26.6k
   VARDECL(spx_coef_t *bw_lpc2);
419
26.6k
   VARDECL(spx_coef_t *lpc);
420
26.6k
   VARDECL(spx_lsp_t *lsp);
421
26.6k
   VARDECL(spx_lsp_t *qlsp);
422
26.6k
   VARDECL(spx_lsp_t *interp_lsp);
423
26.6k
   VARDECL(spx_lsp_t *interp_qlsp);
424
26.6k
   VARDECL(spx_coef_t *interp_lpc);
425
26.6k
   VARDECL(spx_coef_t *interp_qlpc);
426
26.6k
   char *stack;
427
26.6k
   VARDECL(spx_word16_t *syn_resp);
428
429
26.6k
   spx_word32_t ener=0;
430
26.6k
   spx_word16_t fine_gain;
431
26.6k
   spx_word16_t *in = (spx_word16_t*)vin;
432
433
26.6k
   st=(EncState *)state;
434
26.6k
   stack=st->stack;
435
436
26.6k
   ALLOC(lpc, NB_ORDER, spx_coef_t);
437
26.6k
   ALLOC(bw_lpc1, NB_ORDER, spx_coef_t);
438
26.6k
   ALLOC(bw_lpc2, NB_ORDER, spx_coef_t);
439
26.6k
   ALLOC(lsp, NB_ORDER, spx_lsp_t);
440
26.6k
   ALLOC(qlsp, NB_ORDER, spx_lsp_t);
441
26.6k
   ALLOC(interp_lsp, NB_ORDER, spx_lsp_t);
442
26.6k
   ALLOC(interp_qlsp, NB_ORDER, spx_lsp_t);
443
26.6k
   ALLOC(interp_lpc, NB_ORDER, spx_coef_t);
444
26.6k
   ALLOC(interp_qlpc, NB_ORDER, spx_coef_t);
445
446
26.6k
   st->exc = st->excBuf + NB_PITCH_END + 2;
447
26.6k
   st->sw = st->swBuf + NB_PITCH_END + 2;
448
   /* Move signals 1 frame towards the past */
449
26.6k
   SPEEX_MOVE(st->excBuf, st->excBuf+NB_FRAME_SIZE, NB_PITCH_END+2);
450
26.6k
   SPEEX_MOVE(st->swBuf, st->swBuf+NB_FRAME_SIZE, NB_PITCH_END+2);
451
452
26.6k
   if (st->highpass_enabled)
453
11.6k
      highpass(in, in, NB_FRAME_SIZE, (st->isWideband?HIGHPASS_WIDEBAND:HIGHPASS_NARROWBAND)|HIGHPASS_INPUT, st->mem_hp);
454
455
26.6k
   {
456
26.6k
      VARDECL(spx_word16_t *w_sig);
457
26.6k
      VARDECL(spx_word16_t *autocorr);
458
26.6k
      ALLOC(w_sig, NB_WINDOW_SIZE, spx_word16_t);
459
26.6k
      ALLOC(autocorr, NB_ORDER+1, spx_word16_t);
460
      /* Window for analysis */
461
1.09M
      for (i=0;i<NB_WINDOW_SIZE-NB_FRAME_SIZE;i++)
462
1.06M
         w_sig[i] = MULT16_16_Q15(st->winBuf[i],st->window[i]);
463
4.28M
      for (;i<NB_WINDOW_SIZE;i++)
464
4.25M
         w_sig[i] = MULT16_16_Q15(in[i-NB_WINDOW_SIZE+NB_FRAME_SIZE],st->window[i]);
465
      /* Compute auto-correlation */
466
26.6k
      _spx_autocorr(w_sig, autocorr, NB_ORDER+1, NB_WINDOW_SIZE);
467
26.6k
      autocorr[0] = ADD16(autocorr[0],MULT16_16_Q15(autocorr[0],st->lpc_floor)); /* Noise floor in auto-correlation domain */
468
469
      /* Lag windowing: equivalent to filtering in the power-spectrum domain */
470
319k
      for (i=0;i<NB_ORDER+1;i++)
471
292k
         autocorr[i] = MULT16_16_Q15(autocorr[i],st->lagWindow[i]);
472
26.6k
      autocorr[0] = ADD16(autocorr[0],1);
473
474
      /* Levinson-Durbin */
475
26.6k
      _spx_lpc(lpc, autocorr, NB_ORDER);
476
      /* LPC to LSPs (x-domain) transform */
477
26.6k
      roots=lpc_to_lsp (lpc, NB_ORDER, lsp, 10, LSP_DELTA1, stack);
478
      /* Check if we found all the roots */
479
26.6k
      if (roots!=NB_ORDER)
480
49
      {
481
         /*If we can't find all LSP's, do some damage control and use previous filter*/
482
539
         for (i=0;i<NB_ORDER;i++)
483
490
         {
484
490
            lsp[i]=st->old_lsp[i];
485
490
         }
486
49
      }
487
26.6k
   }
488
489
490
491
492
   /* Whole frame analysis (open-loop estimation of pitch and excitation gain) */
493
26.6k
   {
494
26.6k
      int diff = NB_WINDOW_SIZE-NB_FRAME_SIZE;
495
26.6k
      if (st->first)
496
61.6k
         for (i=0;i<NB_ORDER;i++)
497
56.0k
            interp_lsp[i] = lsp[i];
498
21.0k
      else
499
21.0k
         lsp_interpolate(st->old_lsp, lsp, interp_lsp, NB_ORDER, NB_NB_SUBFRAMES, NB_NB_SUBFRAMES<<1, LSP_MARGIN);
500
501
      /* Compute interpolated LPCs (unquantized) for whole frame*/
502
26.6k
      lsp_to_lpc(interp_lsp, interp_lpc, NB_ORDER,stack);
503
504
505
      /*Open-loop pitch*/
506
26.6k
      if (!st->submodes[st->submodeID] || (st->complexity>2 && SUBMODE(have_subframe_gain)<3) || SUBMODE(forced_pitch_gain) || SUBMODE(lbr_pitch) != -1
507
5.76k
#ifndef DISABLE_VBR
508
5.76k
           || st->vbr_enabled || st->vad_enabled
509
26.6k
#endif
510
26.6k
                  )
511
24.5k
      {
512
24.5k
         int nol_pitch[6];
513
24.5k
         spx_word16_t nol_pitch_coef[6];
514
515
24.5k
         bw_lpc(QCONST16(0.9,15), interp_lpc, bw_lpc1, NB_ORDER);
516
24.5k
         bw_lpc(QCONST16(0.55,15), interp_lpc, bw_lpc2, NB_ORDER);
517
518
24.5k
         SPEEX_COPY(st->sw, st->winBuf, diff);
519
24.5k
         SPEEX_COPY(st->sw+diff, in, NB_FRAME_SIZE-diff);
520
24.5k
         filter10(st->sw, bw_lpc1, bw_lpc2, st->sw, NB_FRAME_SIZE, st->mem_sw_whole, stack);
521
522
24.5k
         open_loop_nbest_pitch(st->sw, NB_PITCH_START, NB_PITCH_END, NB_FRAME_SIZE,
523
24.5k
                               nol_pitch, nol_pitch_coef, 6, stack);
524
24.5k
         ol_pitch=nol_pitch[0];
525
24.5k
         ol_pitch_coef = nol_pitch_coef[0];
526
         /*Try to remove pitch multiples*/
527
147k
         for (i=1;i<6;i++)
528
122k
         {
529
#ifdef FIXED_POINT
530
83.2k
            if ((nol_pitch_coef[i]>MULT16_16_Q15(nol_pitch_coef[0],27853)) &&
531
#else
532
39.5k
            if ((nol_pitch_coef[i]>.85*nol_pitch_coef[0]) &&
533
10.6k
#endif
534
28.0k
                (ABS(2*nol_pitch[i]-ol_pitch)<=2 || ABS(3*nol_pitch[i]-ol_pitch)<=3 ||
535
27.4k
                 ABS(4*nol_pitch[i]-ol_pitch)<=4 || ABS(5*nol_pitch[i]-ol_pitch)<=5))
536
942
            {
537
               /*ol_pitch_coef=nol_pitch_coef[i];*/
538
942
               ol_pitch = nol_pitch[i];
539
942
            }
540
122k
         }
541
         /*if (ol_pitch>50)
542
           ol_pitch/=2;*/
543
         /*ol_pitch_coef = sqrt(ol_pitch_coef);*/
544
545
24.5k
      } else {
546
2.06k
         ol_pitch=0;
547
2.06k
         ol_pitch_coef=0;
548
2.06k
      }
549
550
      /*Compute "real" excitation*/
551
      /*SPEEX_COPY(st->exc, st->winBuf, diff);
552
      SPEEX_COPY(st->exc+diff, in, NB_FRAME_SIZE-diff);*/
553
26.6k
      fir_mem16(st->winBuf, interp_lpc, st->exc, diff, NB_ORDER, st->mem_exc, stack);
554
26.6k
      fir_mem16(in, interp_lpc, st->exc+diff, NB_FRAME_SIZE-diff, NB_ORDER, st->mem_exc, stack);
555
556
      /* Compute open-loop excitation gain */
557
26.6k
      {
558
26.6k
         spx_word16_t g = compute_rms16(st->exc, NB_FRAME_SIZE);
559
26.6k
         if (st->submodeID!=1 && ol_pitch>0)
560
12.1k
            ol_gain = MULT16_16(g, MULT16_16_Q14(QCONST16(1.1,14),
561
26.6k
                                spx_sqrt(QCONST32(1.,28)-MULT16_32_Q15(QCONST16(.8,15),SHL32(MULT16_16(ol_pitch_coef,ol_pitch_coef),16)))));
562
14.4k
         else
563
14.4k
            ol_gain = SHL32(EXTEND32(g),SIG_SHIFT);
564
26.6k
      }
565
26.6k
   }
566
567
#ifdef VORBIS_PSYCHO
568
   SPEEX_MOVE(st->psy_window, st->psy_window+NB_FRAME_SIZE, 256-NB_FRAME_SIZE);
569
   SPEEX_COPY(&st->psy_window[256-NB_FRAME_SIZE], in, NB_FRAME_SIZE);
570
   compute_curve(st->psy, st->psy_window, st->curve);
571
   /*print_vec(st->curve, 128, "curve");*/
572
   if (st->first)
573
      SPEEX_COPY(st->old_curve, st->curve, 128);
574
#endif
575
576
   /*VBR stuff*/
577
26.6k
#ifndef DISABLE_VBR
578
26.6k
   if (st->vbr_enabled||st->vad_enabled)
579
20.9k
   {
580
20.9k
      float lsp_dist=0;
581
230k
      for (i=0;i<NB_ORDER;i++)
582
209k
         lsp_dist += (st->old_lsp[i] - lsp[i])*(st->old_lsp[i] - lsp[i]);
583
20.9k
      lsp_dist /= LSP_SCALING*LSP_SCALING;
584
585
20.9k
      if (st->abr_enabled)
586
5.23k
      {
587
5.23k
         float qual_change=0;
588
5.23k
         if (st->abr_drift2 * st->abr_drift > 0)
589
4.70k
         {
590
            /* Only adapt if long-term and short-term drift are the same sign */
591
4.70k
            qual_change = -.00001*st->abr_drift/(1+st->abr_count);
592
4.70k
            if (qual_change>.05)
593
0
               qual_change=.05;
594
4.70k
            if (qual_change<-.05)
595
1.26k
               qual_change=-.05;
596
4.70k
         }
597
5.23k
         st->vbr_quality += qual_change;
598
5.23k
         if (st->vbr_quality>10)
599
0
            st->vbr_quality=10;
600
5.23k
         if (st->vbr_quality<0)
601
4.70k
            st->vbr_quality=0;
602
5.23k
      }
603
604
20.9k
      st->relative_quality = vbr_analysis(&st->vbr, in, NB_FRAME_SIZE, ol_pitch, GAIN_SCALING_1*ol_pitch_coef);
605
      /*if (delta_qual<0)*/
606
      /*  delta_qual*=.1*(3+st->vbr_quality);*/
607
20.9k
      if (st->vbr_enabled)
608
16.4k
      {
609
16.4k
         spx_int32_t mode;
610
16.4k
         int choice=0;
611
16.4k
         float min_diff=100;
612
16.4k
         mode = 8;
613
148k
         while (mode)
614
131k
         {
615
131k
            int v1;
616
131k
            float thresh;
617
131k
            v1=(int)floor(st->vbr_quality);
618
131k
            if (v1==10)
619
7.30k
               thresh = vbr_nb_thresh[mode][v1];
620
124k
            else
621
124k
               thresh = (st->vbr_quality-v1)*vbr_nb_thresh[mode][v1+1] + (1+v1-st->vbr_quality)*vbr_nb_thresh[mode][v1];
622
131k
            if (st->relative_quality > thresh &&
623
35.3k
                st->relative_quality-thresh<min_diff)
624
16.8k
            {
625
16.8k
               choice = mode;
626
16.8k
               min_diff = st->relative_quality-thresh;
627
16.8k
            }
628
131k
            mode--;
629
131k
         }
630
16.4k
         mode=choice;
631
16.4k
         if (mode==0)
632
4.86k
         {
633
4.86k
            if (st->dtx_count==0 || lsp_dist>.05 || !st->dtx_enabled || st->dtx_count>20)
634
2.92k
            {
635
2.92k
               mode=1;
636
2.92k
               st->dtx_count=1;
637
2.92k
            } else {
638
1.94k
               mode=0;
639
1.94k
               st->dtx_count++;
640
1.94k
            }
641
11.5k
         } else {
642
11.5k
            st->dtx_count=0;
643
11.5k
         }
644
645
16.4k
         speex_encoder_ctl(state, SPEEX_SET_MODE, &mode);
646
16.4k
         if (st->vbr_max>0)
647
12.3k
         {
648
12.3k
            spx_int32_t rate;
649
12.3k
            speex_encoder_ctl(state, SPEEX_GET_BITRATE, &rate);
650
12.3k
            if (rate > st->vbr_max)
651
4.34k
            {
652
4.34k
               rate = st->vbr_max;
653
4.34k
               speex_encoder_ctl(state, SPEEX_SET_BITRATE, &rate);
654
4.34k
            }
655
12.3k
         }
656
657
16.4k
         if (st->abr_enabled)
658
5.23k
         {
659
5.23k
            spx_int32_t bitrate;
660
5.23k
            speex_encoder_ctl(state, SPEEX_GET_BITRATE, &bitrate);
661
5.23k
            st->abr_drift+=(bitrate-st->abr_enabled);
662
5.23k
            st->abr_drift2 = .95*st->abr_drift2 + .05*(bitrate-st->abr_enabled);
663
5.23k
            st->abr_count += 1.0;
664
5.23k
         }
665
666
16.4k
      } else {
667
         /*VAD only case*/
668
4.50k
         int mode;
669
4.50k
         if (st->relative_quality<2)
670
1.48k
         {
671
1.48k
            if (st->dtx_count==0 || lsp_dist>.05 || !st->dtx_enabled || st->dtx_count>20)
672
866
            {
673
866
               st->dtx_count=1;
674
866
               mode=1;
675
866
            } else {
676
616
               mode=0;
677
616
               st->dtx_count++;
678
616
            }
679
3.01k
         } else {
680
3.01k
            st->dtx_count = 0;
681
3.01k
            mode=st->submodeSelect;
682
3.01k
         }
683
         /*speex_encoder_ctl(state, SPEEX_SET_MODE, &mode);*/
684
4.50k
         st->submodeID=mode;
685
4.50k
      }
686
20.9k
   } else {
687
5.67k
      st->relative_quality = -1;
688
5.67k
   }
689
26.6k
#endif /* #ifndef DISABLE_VBR */
690
691
26.6k
   if (st->encode_submode)
692
26.6k
   {
693
      /* First, transmit a zero for narrowband */
694
26.6k
      speex_bits_pack(bits, 0, 1);
695
696
      /* Transmit the sub-mode we use for this frame */
697
26.6k
      speex_bits_pack(bits, st->submodeID, NB_SUBMODE_BITS);
698
699
26.6k
   }
700
701
   /* If null mode (no transmission), just set a couple things to zero*/
702
26.6k
   if (st->submodes[st->submodeID] == NULL)
703
2.42k
   {
704
390k
      for (i=0;i<NB_FRAME_SIZE;i++)
705
388k
         st->exc[i]=st->sw[i]=VERY_SMALL;
706
707
26.6k
      for (i=0;i<NB_ORDER;i++)
708
24.2k
         st->mem_sw[i]=0;
709
2.42k
      st->first=1;
710
2.42k
      st->bounded_pitch = 1;
711
712
2.42k
      SPEEX_COPY(st->winBuf, in+2*NB_FRAME_SIZE-NB_WINDOW_SIZE, NB_WINDOW_SIZE-NB_FRAME_SIZE);
713
714
      /* Clear memory (no need to really compute it) */
715
26.6k
      for (i=0;i<NB_ORDER;i++)
716
24.2k
         st->mem_sp[i] = 0;
717
2.42k
      return 0;
718
719
2.42k
   }
720
721
   /* LSP Quantization */
722
24.1k
   if (st->first)
723
3.84k
   {
724
42.3k
      for (i=0;i<NB_ORDER;i++)
725
38.4k
         st->old_lsp[i] = lsp[i];
726
3.84k
   }
727
728
729
   /*Quantize LSPs*/
730
24.1k
#if 1 /*0 for unquantized*/
731
24.1k
   SUBMODE(lsp_quant)(lsp, qlsp, NB_ORDER, bits);
732
#else
733
   for (i=0;i<NB_ORDER;i++)
734
     qlsp[i]=lsp[i];
735
#endif
736
737
   /*If we use low bit-rate pitch mode, transmit open-loop pitch*/
738
24.1k
   if (SUBMODE(lbr_pitch)!=-1)
739
16.3k
   {
740
16.3k
      speex_bits_pack(bits, ol_pitch-NB_PITCH_START, 7);
741
16.3k
   }
742
743
24.1k
   if (SUBMODE(forced_pitch_gain))
744
14.8k
   {
745
14.8k
      int quant;
746
      /* This just damps the pitch a bit, because it tends to be too aggressive when forced */
747
14.8k
      ol_pitch_coef = MULT16_16_Q15(QCONST16(.9,15), ol_pitch_coef);
748
#ifdef FIXED_POINT
749
10.1k
      quant = PSHR16(MULT16_16_16(15, ol_pitch_coef),GAIN_SHIFT);
750
#else
751
4.68k
      quant = (int)floor(.5+15*ol_pitch_coef*GAIN_SCALING_1);
752
#endif
753
14.8k
      if (quant>15)
754
98
         quant=15;
755
14.8k
      if (quant<0)
756
0
         quant=0;
757
14.8k
      speex_bits_pack(bits, quant, 4);
758
14.8k
      ol_pitch_coef=MULT16_16_P15(QCONST16(0.066667,15),SHL16(quant,GAIN_SHIFT));
759
14.8k
   }
760
761
762
   /*Quantize and transmit open-loop excitation gain*/
763
#ifdef FIXED_POINT
764
   {
765
      int qe = scal_quant32(ol_gain, ol_gain_table, 32);
766
      /*ol_gain = exp(qe/3.5)*SIG_SCALING;*/
767
16.7k
      ol_gain = MULT16_32_Q15(28406,ol_gain_table[qe]);
768
      speex_bits_pack(bits, qe, 5);
769
   }
770
#else
771
   {
772
7.39k
      int qe = (int)(floor(.5+3.5*log(ol_gain*1.0/SIG_SCALING)));
773
7.39k
      if (qe<0)
774
4.79k
         qe=0;
775
7.39k
      if (qe>31)
776
453
         qe=31;
777
7.39k
      ol_gain = exp(qe/3.5)*SIG_SCALING;
778
      speex_bits_pack(bits, qe, 5);
779
   }
780
#endif
781
782
783
784
   /* Special case for first frame */
785
24.1k
   if (st->first)
786
3.84k
   {
787
42.3k
      for (i=0;i<NB_ORDER;i++)
788
38.4k
         st->old_qlsp[i] = qlsp[i];
789
3.84k
   }
790
791
   /* Target signal */
792
24.1k
   ALLOC(target, NB_SUBFRAME_SIZE, spx_word16_t);
793
24.1k
   ALLOC(innov, NB_SUBFRAME_SIZE, spx_sig_t);
794
24.1k
   ALLOC(exc32, NB_SUBFRAME_SIZE, spx_word32_t);
795
24.1k
   ALLOC(syn_resp, NB_SUBFRAME_SIZE, spx_word16_t);
796
24.1k
   ALLOC(mem, NB_ORDER, spx_mem_t);
797
798
   /* Loop on sub-frames */
799
120k
   for (sub=0;sub<NB_NB_SUBFRAMES;sub++)
800
96.7k
   {
801
96.7k
      int   offset;
802
96.7k
      spx_word16_t *sw;
803
96.7k
      spx_word16_t *exc, *inBuf;
804
96.7k
      int pitch;
805
96.7k
      int response_bound = NB_SUBFRAME_SIZE;
806
807
      /* Offset relative to start of frame */
808
96.7k
      offset = NB_SUBFRAME_SIZE*sub;
809
      /* Excitation */
810
96.7k
      exc=st->exc+offset;
811
      /* Weighted signal */
812
96.7k
      sw=st->sw+offset;
813
814
      /* LSP interpolation (quantized and unquantized) */
815
96.7k
      lsp_interpolate(st->old_lsp, lsp, interp_lsp, NB_ORDER, sub, NB_NB_SUBFRAMES, LSP_MARGIN);
816
96.7k
      lsp_interpolate(st->old_qlsp, qlsp, interp_qlsp, NB_ORDER, sub, NB_NB_SUBFRAMES, LSP_MARGIN);
817
818
      /* Compute interpolated LPCs (quantized and unquantized) */
819
96.7k
      lsp_to_lpc(interp_lsp, interp_lpc, NB_ORDER,stack);
820
821
96.7k
      lsp_to_lpc(interp_qlsp, interp_qlpc, NB_ORDER, stack);
822
823
      /* Compute analysis filter gain at w=pi (for use in SB-CELP) */
824
96.7k
      {
825
96.7k
         spx_word32_t pi_g=LPC_SCALING;
826
580k
         for (i=0;i<NB_ORDER;i+=2)
827
483k
         {
828
            /*pi_g += -st->interp_qlpc[i] +  st->interp_qlpc[i+1];*/
829
483k
            pi_g = ADD32(pi_g, SUB32(EXTEND32(interp_qlpc[i+1]),EXTEND32(interp_qlpc[i])));
830
483k
         }
831
96.7k
         st->pi_gain[sub] = pi_g;
832
96.7k
      }
833
834
#ifdef VORBIS_PSYCHO
835
      {
836
         float curr_curve[128];
837
         float fact = ((float)sub+1.0f)/NB_NB_SUBFRAMES;
838
         for (i=0;i<128;i++)
839
            curr_curve[i] = (1.0f-fact)*st->old_curve[i] + fact*st->curve[i];
840
         curve_to_lpc(st->psy, curr_curve, bw_lpc1, bw_lpc2, 10);
841
      }
842
#else
843
      /* Compute bandwidth-expanded (unquantized) LPCs for perceptual weighting */
844
96.7k
      bw_lpc(st->gamma1, interp_lpc, bw_lpc1, NB_ORDER);
845
96.7k
      bw_lpc(st->gamma2, interp_lpc, bw_lpc2, NB_ORDER);
846
      /*print_vec(st->bw_lpc1, 10, "bw_lpc");*/
847
96.7k
#endif
848
849
      /*FIXME: This will break if we change the window size */
850
96.7k
      speex_assert(NB_WINDOW_SIZE-NB_FRAME_SIZE == NB_SUBFRAME_SIZE);
851
96.7k
      if (sub==0)
852
24.1k
         inBuf = st->winBuf;
853
72.5k
      else
854
72.5k
         inBuf = &in[((sub-1)*NB_SUBFRAME_SIZE)];
855
3.96M
      for (i=0;i<NB_SUBFRAME_SIZE;i++)
856
3.87M
         sw[i] = inBuf[i];
857
858
96.7k
      if (st->complexity==0)
859
20.4k
         response_bound >>= 1;
860
96.7k
      compute_impulse_response(interp_qlpc, bw_lpc1, bw_lpc2, syn_resp, response_bound, NB_ORDER, stack);
861
506k
      for (i=response_bound;i<NB_SUBFRAME_SIZE;i++)
862
409k
         syn_resp[i]=VERY_SMALL;
863
864
      /* Compute zero response of A(z/g1) / ( A(z/g2) * A(z) ) */
865
1.06M
      for (i=0;i<NB_ORDER;i++)
866
967k
         mem[i]=SHL32(st->mem_sp[i],1);
867
3.96M
      for (i=0;i<NB_SUBFRAME_SIZE;i++)
868
3.87M
         exc[i] = VERY_SMALL;
869
#ifdef SHORTCUTS2
870
      iir_mem16(exc, interp_qlpc, exc, response_bound, NB_ORDER, mem, stack);
871
      for (i=0;i<NB_ORDER;i++)
872
         mem[i]=SHL32(st->mem_sw[i],1);
873
      filter10(exc, st->bw_lpc1, st->bw_lpc2, exc, response_bound, mem, stack);
874
      SPEEX_MEMSET(&exc[response_bound], 0, NB_SUBFRAME_SIZE-response_bound);
875
#else
876
96.7k
      iir_mem16(exc, interp_qlpc, exc, NB_SUBFRAME_SIZE, NB_ORDER, mem, stack);
877
1.06M
      for (i=0;i<NB_ORDER;i++)
878
967k
         mem[i]=SHL32(st->mem_sw[i],1);
879
96.7k
      filter10(exc, bw_lpc1, bw_lpc2, exc, NB_SUBFRAME_SIZE, mem, stack);
880
96.7k
#endif
881
882
      /* Compute weighted signal */
883
1.06M
      for (i=0;i<NB_ORDER;i++)
884
967k
         mem[i]=st->mem_sw[i];
885
96.7k
      filter10(sw, bw_lpc1, bw_lpc2, sw, NB_SUBFRAME_SIZE, mem, stack);
886
887
96.7k
      if (st->complexity==0)
888
225k
         for (i=0;i<NB_ORDER;i++)
889
204k
            st->mem_sw[i]=mem[i];
890
891
      /* Compute target signal (saturation prevents overflows on clipped input speech) */
892
3.96M
      for (i=0;i<NB_SUBFRAME_SIZE;i++)
893
3.87M
         target[i]=EXTRACT16(SATURATE(SUB32(sw[i],PSHR32(exc[i],1)),32767));
894
895
3.96M
      for (i=0;i<NB_SUBFRAME_SIZE;i++)
896
3.87M
         exc[i] = inBuf[i];
897
96.7k
      fir_mem16(exc, interp_qlpc, exc, NB_SUBFRAME_SIZE, NB_ORDER, st->mem_exc2, stack);
898
      /* If we have a long-term predictor (otherwise, something's wrong) */
899
96.7k
      speex_assert (SUBMODE(ltp_quant));
900
96.7k
      {
901
96.7k
         int pit_min, pit_max;
902
         /* Long-term prediction */
903
96.7k
         if (SUBMODE(lbr_pitch) != -1)
904
65.3k
         {
905
            /* Low bit-rate pitch handling */
906
65.3k
            int margin;
907
65.3k
            margin = SUBMODE(lbr_pitch);
908
65.3k
            if (margin)
909
0
            {
910
0
               if (ol_pitch < NB_PITCH_START+margin-1)
911
0
                  ol_pitch=NB_PITCH_START+margin-1;
912
0
               if (ol_pitch > NB_PITCH_END-margin)
913
0
                  ol_pitch=NB_PITCH_END-margin;
914
0
               pit_min = ol_pitch-margin+1;
915
0
               pit_max = ol_pitch+margin;
916
65.3k
            } else {
917
65.3k
               pit_min=pit_max=ol_pitch;
918
65.3k
            }
919
65.3k
         } else {
920
31.4k
            pit_min = NB_PITCH_START;
921
31.4k
            pit_max = NB_PITCH_END;
922
31.4k
         }
923
924
         /* Force pitch to use only the current frame if needed */
925
96.7k
         if (st->bounded_pitch && pit_max>offset)
926
29.7k
            pit_max=offset;
927
928
         /* Perform pitch search */
929
96.7k
         pitch = SUBMODE(ltp_quant)(target, sw, interp_qlpc, bw_lpc1, bw_lpc2,
930
96.7k
                                    exc32, SUBMODE(ltp_params), pit_min, pit_max, ol_pitch_coef,
931
96.7k
                                    NB_ORDER, NB_SUBFRAME_SIZE, bits, stack,
932
96.7k
                                    exc, syn_resp, st->complexity, 0, st->plc_tuning, &st->cumul_gain);
933
934
96.7k
         st->pitch[sub]=pitch;
935
96.7k
      }
936
      /* Quantization of innovation */
937
96.7k
      SPEEX_MEMSET(innov, 0, NB_SUBFRAME_SIZE);
938
939
      /* FIXME: Make sure this is safe from overflows (so far so good) */
940
3.96M
      for (i=0;i<NB_SUBFRAME_SIZE;i++)
941
3.87M
         exc[i] = EXTRACT16(SUB32(EXTEND32(exc[i]), PSHR32(exc32[i],SIG_SHIFT-1)));
942
943
96.7k
      ener = SHL32(EXTEND32(compute_rms16(exc, NB_SUBFRAME_SIZE)),SIG_SHIFT);
944
945
      /*FIXME: Should use DIV32_16 and make sure result fits in 16 bits */
946
#ifdef FIXED_POINT
947
      {
948
67.1k
         spx_word32_t f = PDIV32(ener,PSHR32(ol_gain,SIG_SHIFT));
949
67.1k
         if (f<=32767)
950
62.1k
            fine_gain = f;
951
5.07k
         else
952
5.07k
            fine_gain = 32767;
953
      }
954
#else
955
29.5k
      fine_gain = PDIV32_16(ener,PSHR32(ol_gain,SIG_SHIFT));
956
#endif
957
      /* Calculate gain correction for the sub-frame (if any) */
958
96.7k
      if (SUBMODE(have_subframe_gain))
959
31.4k
      {
960
31.4k
         int qe;
961
31.4k
         if (SUBMODE(have_subframe_gain)==3)
962
18.3k
         {
963
18.3k
            qe = scal_quant(fine_gain, exc_gain_quant_scal3_bound, 8);
964
18.3k
            speex_bits_pack(bits, qe, 3);
965
18.3k
            ener=MULT16_32_Q14(exc_gain_quant_scal3[qe],ol_gain);
966
18.3k
         } else {
967
13.0k
            qe = scal_quant(fine_gain, exc_gain_quant_scal1_bound, 2);
968
13.0k
            speex_bits_pack(bits, qe, 1);
969
13.0k
            ener=MULT16_32_Q14(exc_gain_quant_scal1[qe],ol_gain);
970
13.0k
         }
971
65.3k
      } else {
972
65.3k
         ener=ol_gain;
973
65.3k
      }
974
975
      /*printf ("%f %f\n", ener, ol_gain);*/
976
977
      /* Normalize innovation */
978
96.7k
      signal_div(target, target, ener, NB_SUBFRAME_SIZE);
979
980
      /* Quantize innovation */
981
96.7k
      speex_assert (SUBMODE(innovation_quant));
982
96.7k
      {
983
         /* Codebook search */
984
96.7k
         SUBMODE(innovation_quant)(target, interp_qlpc, bw_lpc1, bw_lpc2,
985
96.7k
                  SUBMODE(innovation_params), NB_ORDER, NB_SUBFRAME_SIZE,
986
96.7k
                  innov, syn_resp, bits, stack, st->complexity, SUBMODE(double_codebook));
987
988
         /* De-normalize innovation and update excitation */
989
96.7k
         signal_mul(innov, innov, ener, NB_SUBFRAME_SIZE);
990
991
         /* In some (rare) modes, we do a second search (more bits) to reduce noise even more */
992
96.7k
         if (SUBMODE(double_codebook)) {
993
3.50k
            char *tmp_stack=stack;
994
3.50k
            VARDECL(spx_sig_t *innov2);
995
3.50k
            ALLOC(innov2, NB_SUBFRAME_SIZE, spx_sig_t);
996
3.50k
            SPEEX_MEMSET(innov2, 0, NB_SUBFRAME_SIZE);
997
143k
            for (i=0;i<NB_SUBFRAME_SIZE;i++)
998
140k
               target[i]=MULT16_16_P13(QCONST16(2.2f,13), target[i]);
999
3.50k
            SUBMODE(innovation_quant)(target, interp_qlpc, bw_lpc1, bw_lpc2,
1000
3.50k
                                      SUBMODE(innovation_params), NB_ORDER, NB_SUBFRAME_SIZE,
1001
3.50k
                                      innov2, syn_resp, bits, stack, st->complexity, 0);
1002
3.50k
            signal_mul(innov2, innov2, MULT16_32_Q15(QCONST16(0.454545f,15),ener), NB_SUBFRAME_SIZE);
1003
143k
            for (i=0;i<NB_SUBFRAME_SIZE;i++)
1004
140k
               innov[i] = ADD32(innov[i],innov2[i]);
1005
3.50k
            stack = tmp_stack;
1006
3.50k
         }
1007
3.96M
         for (i=0;i<NB_SUBFRAME_SIZE;i++)
1008
3.87M
            exc[i] = EXTRACT16(SATURATE32(PSHR32(ADD32(SHL32(exc32[i],1),innov[i]),SIG_SHIFT),32767));
1009
96.7k
         if (st->innov_rms_save)
1010
29.0k
            st->innov_rms_save[sub] = compute_rms(innov, NB_SUBFRAME_SIZE);
1011
96.7k
      }
1012
1013
      /* Final signal synthesis from excitation */
1014
96.7k
      iir_mem16(exc, interp_qlpc, sw, NB_SUBFRAME_SIZE, NB_ORDER, st->mem_sp, stack);
1015
1016
      /* Compute weighted signal again, from synthesized speech (not sure it's the right thing) */
1017
96.7k
      if (st->complexity!=0)
1018
76.3k
         filter10(sw, bw_lpc1, bw_lpc2, sw, NB_SUBFRAME_SIZE, st->mem_sw, stack);
1019
1020
96.7k
   }
1021
1022
   /* Store the LSPs for interpolation in the next frame */
1023
24.1k
   if (st->submodeID>=1)
1024
24.1k
   {
1025
266k
      for (i=0;i<NB_ORDER;i++)
1026
241k
         st->old_lsp[i] = lsp[i];
1027
266k
      for (i=0;i<NB_ORDER;i++)
1028
241k
         st->old_qlsp[i] = qlsp[i];
1029
24.1k
   }
1030
1031
#ifdef VORBIS_PSYCHO
1032
   if (st->submodeID>=1)
1033
      SPEEX_COPY(st->old_curve, st->curve, 128);
1034
#endif
1035
1036
24.1k
   if (st->submodeID==1)
1037
12.2k
   {
1038
12.2k
#ifndef DISABLE_VBR
1039
12.2k
      if (st->dtx_count)
1040
3.92k
         speex_bits_pack(bits, 15, 4);
1041
8.30k
      else
1042
8.30k
#endif
1043
8.30k
         speex_bits_pack(bits, 0, 4);
1044
12.2k
   }
1045
1046
   /* The next frame will not be the first (Duh!) */
1047
24.1k
   st->first = 0;
1048
24.1k
   SPEEX_COPY(st->winBuf, in+2*NB_FRAME_SIZE-NB_WINDOW_SIZE, NB_WINDOW_SIZE-NB_FRAME_SIZE);
1049
1050
24.1k
   if (SUBMODE(innovation_quant) == noise_codebook_quant || st->submodeID==0)
1051
12.2k
      st->bounded_pitch = 1;
1052
11.9k
   else
1053
11.9k
      st->bounded_pitch = 0;
1054
1055
24.1k
   return 1;
1056
26.6k
}
nb_encode
Line
Count
Source
407
8.63k
{
408
8.63k
   EncState *st;
409
8.63k
   int i, sub, roots;
410
8.63k
   int ol_pitch;
411
8.63k
   spx_word16_t ol_pitch_coef;
412
8.63k
   spx_word32_t ol_gain;
413
8.63k
   VARDECL(spx_word16_t *target);
414
8.63k
   VARDECL(spx_sig_t *innov);
415
8.63k
   VARDECL(spx_word32_t *exc32);
416
8.63k
   VARDECL(spx_mem_t *mem);
417
8.63k
   VARDECL(spx_coef_t *bw_lpc1);
418
8.63k
   VARDECL(spx_coef_t *bw_lpc2);
419
8.63k
   VARDECL(spx_coef_t *lpc);
420
8.63k
   VARDECL(spx_lsp_t *lsp);
421
8.63k
   VARDECL(spx_lsp_t *qlsp);
422
8.63k
   VARDECL(spx_lsp_t *interp_lsp);
423
8.63k
   VARDECL(spx_lsp_t *interp_qlsp);
424
8.63k
   VARDECL(spx_coef_t *interp_lpc);
425
8.63k
   VARDECL(spx_coef_t *interp_qlpc);
426
8.63k
   char *stack;
427
8.63k
   VARDECL(spx_word16_t *syn_resp);
428
429
8.63k
   spx_word32_t ener=0;
430
8.63k
   spx_word16_t fine_gain;
431
8.63k
   spx_word16_t *in = (spx_word16_t*)vin;
432
433
8.63k
   st=(EncState *)state;
434
8.63k
   stack=st->stack;
435
436
8.63k
   ALLOC(lpc, NB_ORDER, spx_coef_t);
437
8.63k
   ALLOC(bw_lpc1, NB_ORDER, spx_coef_t);
438
8.63k
   ALLOC(bw_lpc2, NB_ORDER, spx_coef_t);
439
8.63k
   ALLOC(lsp, NB_ORDER, spx_lsp_t);
440
8.63k
   ALLOC(qlsp, NB_ORDER, spx_lsp_t);
441
8.63k
   ALLOC(interp_lsp, NB_ORDER, spx_lsp_t);
442
8.63k
   ALLOC(interp_qlsp, NB_ORDER, spx_lsp_t);
443
8.63k
   ALLOC(interp_lpc, NB_ORDER, spx_coef_t);
444
8.63k
   ALLOC(interp_qlpc, NB_ORDER, spx_coef_t);
445
446
8.63k
   st->exc = st->excBuf + NB_PITCH_END + 2;
447
8.63k
   st->sw = st->swBuf + NB_PITCH_END + 2;
448
   /* Move signals 1 frame towards the past */
449
8.63k
   SPEEX_MOVE(st->excBuf, st->excBuf+NB_FRAME_SIZE, NB_PITCH_END+2);
450
8.63k
   SPEEX_MOVE(st->swBuf, st->swBuf+NB_FRAME_SIZE, NB_PITCH_END+2);
451
452
8.63k
   if (st->highpass_enabled)
453
4.82k
      highpass(in, in, NB_FRAME_SIZE, (st->isWideband?HIGHPASS_WIDEBAND:HIGHPASS_NARROWBAND)|HIGHPASS_INPUT, st->mem_hp);
454
455
8.63k
   {
456
8.63k
      VARDECL(spx_word16_t *w_sig);
457
8.63k
      VARDECL(spx_word16_t *autocorr);
458
8.63k
      ALLOC(w_sig, NB_WINDOW_SIZE, spx_word16_t);
459
8.63k
      ALLOC(autocorr, NB_ORDER+1, spx_word16_t);
460
      /* Window for analysis */
461
354k
      for (i=0;i<NB_WINDOW_SIZE-NB_FRAME_SIZE;i++)
462
345k
         w_sig[i] = MULT16_16_Q15(st->winBuf[i],st->window[i]);
463
1.39M
      for (;i<NB_WINDOW_SIZE;i++)
464
1.38M
         w_sig[i] = MULT16_16_Q15(in[i-NB_WINDOW_SIZE+NB_FRAME_SIZE],st->window[i]);
465
      /* Compute auto-correlation */
466
8.63k
      _spx_autocorr(w_sig, autocorr, NB_ORDER+1, NB_WINDOW_SIZE);
467
8.63k
      autocorr[0] = ADD16(autocorr[0],MULT16_16_Q15(autocorr[0],st->lpc_floor)); /* Noise floor in auto-correlation domain */
468
469
      /* Lag windowing: equivalent to filtering in the power-spectrum domain */
470
103k
      for (i=0;i<NB_ORDER+1;i++)
471
95.0k
         autocorr[i] = MULT16_16_Q15(autocorr[i],st->lagWindow[i]);
472
8.63k
      autocorr[0] = ADD16(autocorr[0],1);
473
474
      /* Levinson-Durbin */
475
8.63k
      _spx_lpc(lpc, autocorr, NB_ORDER);
476
      /* LPC to LSPs (x-domain) transform */
477
8.63k
      roots=lpc_to_lsp (lpc, NB_ORDER, lsp, 10, LSP_DELTA1, stack);
478
      /* Check if we found all the roots */
479
8.63k
      if (roots!=NB_ORDER)
480
4
      {
481
         /*If we can't find all LSP's, do some damage control and use previous filter*/
482
44
         for (i=0;i<NB_ORDER;i++)
483
40
         {
484
40
            lsp[i]=st->old_lsp[i];
485
40
         }
486
4
      }
487
8.63k
   }
488
489
490
491
492
   /* Whole frame analysis (open-loop estimation of pitch and excitation gain) */
493
8.63k
   {
494
8.63k
      int diff = NB_WINDOW_SIZE-NB_FRAME_SIZE;
495
8.63k
      if (st->first)
496
24.2k
         for (i=0;i<NB_ORDER;i++)
497
22.0k
            interp_lsp[i] = lsp[i];
498
6.43k
      else
499
6.43k
         lsp_interpolate(st->old_lsp, lsp, interp_lsp, NB_ORDER, NB_NB_SUBFRAMES, NB_NB_SUBFRAMES<<1, LSP_MARGIN);
500
501
      /* Compute interpolated LPCs (unquantized) for whole frame*/
502
8.63k
      lsp_to_lpc(interp_lsp, interp_lpc, NB_ORDER,stack);
503
504
505
      /*Open-loop pitch*/
506
8.63k
      if (!st->submodes[st->submodeID] || (st->complexity>2 && SUBMODE(have_subframe_gain)<3) || SUBMODE(forced_pitch_gain) || SUBMODE(lbr_pitch) != -1
507
1.77k
#ifndef DISABLE_VBR
508
1.77k
           || st->vbr_enabled || st->vad_enabled
509
8.63k
#endif
510
8.63k
                  )
511
7.91k
      {
512
7.91k
         int nol_pitch[6];
513
7.91k
         spx_word16_t nol_pitch_coef[6];
514
515
7.91k
         bw_lpc(QCONST16(0.9,15), interp_lpc, bw_lpc1, NB_ORDER);
516
7.91k
         bw_lpc(QCONST16(0.55,15), interp_lpc, bw_lpc2, NB_ORDER);
517
518
7.91k
         SPEEX_COPY(st->sw, st->winBuf, diff);
519
7.91k
         SPEEX_COPY(st->sw+diff, in, NB_FRAME_SIZE-diff);
520
7.91k
         filter10(st->sw, bw_lpc1, bw_lpc2, st->sw, NB_FRAME_SIZE, st->mem_sw_whole, stack);
521
522
7.91k
         open_loop_nbest_pitch(st->sw, NB_PITCH_START, NB_PITCH_END, NB_FRAME_SIZE,
523
7.91k
                               nol_pitch, nol_pitch_coef, 6, stack);
524
7.91k
         ol_pitch=nol_pitch[0];
525
7.91k
         ol_pitch_coef = nol_pitch_coef[0];
526
         /*Try to remove pitch multiples*/
527
47.5k
         for (i=1;i<6;i++)
528
39.5k
         {
529
#ifdef FIXED_POINT
530
            if ((nol_pitch_coef[i]>MULT16_16_Q15(nol_pitch_coef[0],27853)) &&
531
#else
532
39.5k
            if ((nol_pitch_coef[i]>.85*nol_pitch_coef[0]) &&
533
10.6k
#endif
534
10.6k
                (ABS(2*nol_pitch[i]-ol_pitch)<=2 || ABS(3*nol_pitch[i]-ol_pitch)<=3 ||
535
10.5k
                 ABS(4*nol_pitch[i]-ol_pitch)<=4 || ABS(5*nol_pitch[i]-ol_pitch)<=5))
536
240
            {
537
               /*ol_pitch_coef=nol_pitch_coef[i];*/
538
240
               ol_pitch = nol_pitch[i];
539
240
            }
540
39.5k
         }
541
         /*if (ol_pitch>50)
542
           ol_pitch/=2;*/
543
         /*ol_pitch_coef = sqrt(ol_pitch_coef);*/
544
545
7.91k
      } else {
546
720
         ol_pitch=0;
547
720
         ol_pitch_coef=0;
548
720
      }
549
550
      /*Compute "real" excitation*/
551
      /*SPEEX_COPY(st->exc, st->winBuf, diff);
552
      SPEEX_COPY(st->exc+diff, in, NB_FRAME_SIZE-diff);*/
553
8.63k
      fir_mem16(st->winBuf, interp_lpc, st->exc, diff, NB_ORDER, st->mem_exc, stack);
554
8.63k
      fir_mem16(in, interp_lpc, st->exc+diff, NB_FRAME_SIZE-diff, NB_ORDER, st->mem_exc, stack);
555
556
      /* Compute open-loop excitation gain */
557
8.63k
      {
558
8.63k
         spx_word16_t g = compute_rms16(st->exc, NB_FRAME_SIZE);
559
8.63k
         if (st->submodeID!=1 && ol_pitch>0)
560
4.09k
            ol_gain = MULT16_16(g, MULT16_16_Q14(QCONST16(1.1,14),
561
8.63k
                                spx_sqrt(QCONST32(1.,28)-MULT16_32_Q15(QCONST16(.8,15),SHL32(MULT16_16(ol_pitch_coef,ol_pitch_coef),16)))));
562
4.54k
         else
563
4.54k
            ol_gain = SHL32(EXTEND32(g),SIG_SHIFT);
564
8.63k
      }
565
8.63k
   }
566
567
#ifdef VORBIS_PSYCHO
568
   SPEEX_MOVE(st->psy_window, st->psy_window+NB_FRAME_SIZE, 256-NB_FRAME_SIZE);
569
   SPEEX_COPY(&st->psy_window[256-NB_FRAME_SIZE], in, NB_FRAME_SIZE);
570
   compute_curve(st->psy, st->psy_window, st->curve);
571
   /*print_vec(st->curve, 128, "curve");*/
572
   if (st->first)
573
      SPEEX_COPY(st->old_curve, st->curve, 128);
574
#endif
575
576
   /*VBR stuff*/
577
8.63k
#ifndef DISABLE_VBR
578
8.63k
   if (st->vbr_enabled||st->vad_enabled)
579
6.97k
   {
580
6.97k
      float lsp_dist=0;
581
76.7k
      for (i=0;i<NB_ORDER;i++)
582
69.7k
         lsp_dist += (st->old_lsp[i] - lsp[i])*(st->old_lsp[i] - lsp[i]);
583
6.97k
      lsp_dist /= LSP_SCALING*LSP_SCALING;
584
585
6.97k
      if (st->abr_enabled)
586
1.67k
      {
587
1.67k
         float qual_change=0;
588
1.67k
         if (st->abr_drift2 * st->abr_drift > 0)
589
1.50k
         {
590
            /* Only adapt if long-term and short-term drift are the same sign */
591
1.50k
            qual_change = -.00001*st->abr_drift/(1+st->abr_count);
592
1.50k
            if (qual_change>.05)
593
0
               qual_change=.05;
594
1.50k
            if (qual_change<-.05)
595
248
               qual_change=-.05;
596
1.50k
         }
597
1.67k
         st->vbr_quality += qual_change;
598
1.67k
         if (st->vbr_quality>10)
599
0
            st->vbr_quality=10;
600
1.67k
         if (st->vbr_quality<0)
601
1.50k
            st->vbr_quality=0;
602
1.67k
      }
603
604
6.97k
      st->relative_quality = vbr_analysis(&st->vbr, in, NB_FRAME_SIZE, ol_pitch, GAIN_SCALING_1*ol_pitch_coef);
605
      /*if (delta_qual<0)*/
606
      /*  delta_qual*=.1*(3+st->vbr_quality);*/
607
6.97k
      if (st->vbr_enabled)
608
5.56k
      {
609
5.56k
         spx_int32_t mode;
610
5.56k
         int choice=0;
611
5.56k
         float min_diff=100;
612
5.56k
         mode = 8;
613
50.0k
         while (mode)
614
44.4k
         {
615
44.4k
            int v1;
616
44.4k
            float thresh;
617
44.4k
            v1=(int)floor(st->vbr_quality);
618
44.4k
            if (v1==10)
619
4.12k
               thresh = vbr_nb_thresh[mode][v1];
620
40.3k
            else
621
40.3k
               thresh = (st->vbr_quality-v1)*vbr_nb_thresh[mode][v1+1] + (1+v1-st->vbr_quality)*vbr_nb_thresh[mode][v1];
622
44.4k
            if (st->relative_quality > thresh &&
623
10.2k
                st->relative_quality-thresh<min_diff)
624
4.76k
            {
625
4.76k
               choice = mode;
626
4.76k
               min_diff = st->relative_quality-thresh;
627
4.76k
            }
628
44.4k
            mode--;
629
44.4k
         }
630
5.56k
         mode=choice;
631
5.56k
         if (mode==0)
632
2.20k
         {
633
2.20k
            if (st->dtx_count==0 || lsp_dist>.05 || !st->dtx_enabled || st->dtx_count>20)
634
1.22k
            {
635
1.22k
               mode=1;
636
1.22k
               st->dtx_count=1;
637
1.22k
            } else {
638
980
               mode=0;
639
980
               st->dtx_count++;
640
980
            }
641
3.35k
         } else {
642
3.35k
            st->dtx_count=0;
643
3.35k
         }
644
645
5.56k
         speex_encoder_ctl(state, SPEEX_SET_MODE, &mode);
646
5.56k
         if (st->vbr_max>0)
647
4.30k
         {
648
4.30k
            spx_int32_t rate;
649
4.30k
            speex_encoder_ctl(state, SPEEX_GET_BITRATE, &rate);
650
4.30k
            if (rate > st->vbr_max)
651
1.18k
            {
652
1.18k
               rate = st->vbr_max;
653
1.18k
               speex_encoder_ctl(state, SPEEX_SET_BITRATE, &rate);
654
1.18k
            }
655
4.30k
         }
656
657
5.56k
         if (st->abr_enabled)
658
1.67k
         {
659
1.67k
            spx_int32_t bitrate;
660
1.67k
            speex_encoder_ctl(state, SPEEX_GET_BITRATE, &bitrate);
661
1.67k
            st->abr_drift+=(bitrate-st->abr_enabled);
662
1.67k
            st->abr_drift2 = .95*st->abr_drift2 + .05*(bitrate-st->abr_enabled);
663
1.67k
            st->abr_count += 1.0;
664
1.67k
         }
665
666
5.56k
      } else {
667
         /*VAD only case*/
668
1.41k
         int mode;
669
1.41k
         if (st->relative_quality<2)
670
735
         {
671
735
            if (st->dtx_count==0 || lsp_dist>.05 || !st->dtx_enabled || st->dtx_count>20)
672
428
            {
673
428
               st->dtx_count=1;
674
428
               mode=1;
675
428
            } else {
676
307
               mode=0;
677
307
               st->dtx_count++;
678
307
            }
679
735
         } else {
680
678
            st->dtx_count = 0;
681
678
            mode=st->submodeSelect;
682
678
         }
683
         /*speex_encoder_ctl(state, SPEEX_SET_MODE, &mode);*/
684
1.41k
         st->submodeID=mode;
685
1.41k
      }
686
6.97k
   } else {
687
1.66k
      st->relative_quality = -1;
688
1.66k
   }
689
8.63k
#endif /* #ifndef DISABLE_VBR */
690
691
8.63k
   if (st->encode_submode)
692
8.63k
   {
693
      /* First, transmit a zero for narrowband */
694
8.63k
      speex_bits_pack(bits, 0, 1);
695
696
      /* Transmit the sub-mode we use for this frame */
697
8.63k
      speex_bits_pack(bits, st->submodeID, NB_SUBMODE_BITS);
698
699
8.63k
   }
700
701
   /* If null mode (no transmission), just set a couple things to zero*/
702
8.63k
   if (st->submodes[st->submodeID] == NULL)
703
1.23k
   {
704
199k
      for (i=0;i<NB_FRAME_SIZE;i++)
705
198k
         st->exc[i]=st->sw[i]=VERY_SMALL;
706
707
13.6k
      for (i=0;i<NB_ORDER;i++)
708
12.3k
         st->mem_sw[i]=0;
709
1.23k
      st->first=1;
710
1.23k
      st->bounded_pitch = 1;
711
712
1.23k
      SPEEX_COPY(st->winBuf, in+2*NB_FRAME_SIZE-NB_WINDOW_SIZE, NB_WINDOW_SIZE-NB_FRAME_SIZE);
713
714
      /* Clear memory (no need to really compute it) */
715
13.6k
      for (i=0;i<NB_ORDER;i++)
716
12.3k
         st->mem_sp[i] = 0;
717
1.23k
      return 0;
718
719
1.23k
   }
720
721
   /* LSP Quantization */
722
7.39k
   if (st->first)
723
1.29k
   {
724
14.1k
      for (i=0;i<NB_ORDER;i++)
725
12.9k
         st->old_lsp[i] = lsp[i];
726
1.29k
   }
727
728
729
   /*Quantize LSPs*/
730
7.39k
#if 1 /*0 for unquantized*/
731
7.39k
   SUBMODE(lsp_quant)(lsp, qlsp, NB_ORDER, bits);
732
#else
733
   for (i=0;i<NB_ORDER;i++)
734
     qlsp[i]=lsp[i];
735
#endif
736
737
   /*If we use low bit-rate pitch mode, transmit open-loop pitch*/
738
7.39k
   if (SUBMODE(lbr_pitch)!=-1)
739
5.05k
   {
740
5.05k
      speex_bits_pack(bits, ol_pitch-NB_PITCH_START, 7);
741
5.05k
   }
742
743
7.39k
   if (SUBMODE(forced_pitch_gain))
744
4.68k
   {
745
4.68k
      int quant;
746
      /* This just damps the pitch a bit, because it tends to be too aggressive when forced */
747
4.68k
      ol_pitch_coef = MULT16_16_Q15(QCONST16(.9,15), ol_pitch_coef);
748
#ifdef FIXED_POINT
749
      quant = PSHR16(MULT16_16_16(15, ol_pitch_coef),GAIN_SHIFT);
750
#else
751
4.68k
      quant = (int)floor(.5+15*ol_pitch_coef*GAIN_SCALING_1);
752
4.68k
#endif
753
4.68k
      if (quant>15)
754
36
         quant=15;
755
4.68k
      if (quant<0)
756
0
         quant=0;
757
4.68k
      speex_bits_pack(bits, quant, 4);
758
4.68k
      ol_pitch_coef=MULT16_16_P15(QCONST16(0.066667,15),SHL16(quant,GAIN_SHIFT));
759
4.68k
   }
760
761
762
   /*Quantize and transmit open-loop excitation gain*/
763
#ifdef FIXED_POINT
764
   {
765
      int qe = scal_quant32(ol_gain, ol_gain_table, 32);
766
      /*ol_gain = exp(qe/3.5)*SIG_SCALING;*/
767
      ol_gain = MULT16_32_Q15(28406,ol_gain_table[qe]);
768
      speex_bits_pack(bits, qe, 5);
769
   }
770
#else
771
7.39k
   {
772
7.39k
      int qe = (int)(floor(.5+3.5*log(ol_gain*1.0/SIG_SCALING)));
773
7.39k
      if (qe<0)
774
4.79k
         qe=0;
775
7.39k
      if (qe>31)
776
453
         qe=31;
777
7.39k
      ol_gain = exp(qe/3.5)*SIG_SCALING;
778
7.39k
      speex_bits_pack(bits, qe, 5);
779
7.39k
   }
780
7.39k
#endif
781
782
783
784
   /* Special case for first frame */
785
7.39k
   if (st->first)
786
1.29k
   {
787
14.1k
      for (i=0;i<NB_ORDER;i++)
788
12.9k
         st->old_qlsp[i] = qlsp[i];
789
1.29k
   }
790
791
   /* Target signal */
792
7.39k
   ALLOC(target, NB_SUBFRAME_SIZE, spx_word16_t);
793
7.39k
   ALLOC(innov, NB_SUBFRAME_SIZE, spx_sig_t);
794
7.39k
   ALLOC(exc32, NB_SUBFRAME_SIZE, spx_word32_t);
795
7.39k
   ALLOC(syn_resp, NB_SUBFRAME_SIZE, spx_word16_t);
796
7.39k
   ALLOC(mem, NB_ORDER, spx_mem_t);
797
798
   /* Loop on sub-frames */
799
36.9k
   for (sub=0;sub<NB_NB_SUBFRAMES;sub++)
800
29.5k
   {
801
29.5k
      int   offset;
802
29.5k
      spx_word16_t *sw;
803
29.5k
      spx_word16_t *exc, *inBuf;
804
29.5k
      int pitch;
805
29.5k
      int response_bound = NB_SUBFRAME_SIZE;
806
807
      /* Offset relative to start of frame */
808
29.5k
      offset = NB_SUBFRAME_SIZE*sub;
809
      /* Excitation */
810
29.5k
      exc=st->exc+offset;
811
      /* Weighted signal */
812
29.5k
      sw=st->sw+offset;
813
814
      /* LSP interpolation (quantized and unquantized) */
815
29.5k
      lsp_interpolate(st->old_lsp, lsp, interp_lsp, NB_ORDER, sub, NB_NB_SUBFRAMES, LSP_MARGIN);
816
29.5k
      lsp_interpolate(st->old_qlsp, qlsp, interp_qlsp, NB_ORDER, sub, NB_NB_SUBFRAMES, LSP_MARGIN);
817
818
      /* Compute interpolated LPCs (quantized and unquantized) */
819
29.5k
      lsp_to_lpc(interp_lsp, interp_lpc, NB_ORDER,stack);
820
821
29.5k
      lsp_to_lpc(interp_qlsp, interp_qlpc, NB_ORDER, stack);
822
823
      /* Compute analysis filter gain at w=pi (for use in SB-CELP) */
824
29.5k
      {
825
29.5k
         spx_word32_t pi_g=LPC_SCALING;
826
177k
         for (i=0;i<NB_ORDER;i+=2)
827
147k
         {
828
            /*pi_g += -st->interp_qlpc[i] +  st->interp_qlpc[i+1];*/
829
147k
            pi_g = ADD32(pi_g, SUB32(EXTEND32(interp_qlpc[i+1]),EXTEND32(interp_qlpc[i])));
830
147k
         }
831
29.5k
         st->pi_gain[sub] = pi_g;
832
29.5k
      }
833
834
#ifdef VORBIS_PSYCHO
835
      {
836
         float curr_curve[128];
837
         float fact = ((float)sub+1.0f)/NB_NB_SUBFRAMES;
838
         for (i=0;i<128;i++)
839
            curr_curve[i] = (1.0f-fact)*st->old_curve[i] + fact*st->curve[i];
840
         curve_to_lpc(st->psy, curr_curve, bw_lpc1, bw_lpc2, 10);
841
      }
842
#else
843
      /* Compute bandwidth-expanded (unquantized) LPCs for perceptual weighting */
844
29.5k
      bw_lpc(st->gamma1, interp_lpc, bw_lpc1, NB_ORDER);
845
29.5k
      bw_lpc(st->gamma2, interp_lpc, bw_lpc2, NB_ORDER);
846
      /*print_vec(st->bw_lpc1, 10, "bw_lpc");*/
847
29.5k
#endif
848
849
      /*FIXME: This will break if we change the window size */
850
29.5k
      speex_assert(NB_WINDOW_SIZE-NB_FRAME_SIZE == NB_SUBFRAME_SIZE);
851
29.5k
      if (sub==0)
852
7.39k
         inBuf = st->winBuf;
853
22.1k
      else
854
22.1k
         inBuf = &in[((sub-1)*NB_SUBFRAME_SIZE)];
855
1.21M
      for (i=0;i<NB_SUBFRAME_SIZE;i++)
856
1.18M
         sw[i] = inBuf[i];
857
858
29.5k
      if (st->complexity==0)
859
6.57k
         response_bound >>= 1;
860
29.5k
      compute_impulse_response(interp_qlpc, bw_lpc1, bw_lpc2, syn_resp, response_bound, NB_ORDER, stack);
861
161k
      for (i=response_bound;i<NB_SUBFRAME_SIZE;i++)
862
131k
         syn_resp[i]=VERY_SMALL;
863
864
      /* Compute zero response of A(z/g1) / ( A(z/g2) * A(z) ) */
865
325k
      for (i=0;i<NB_ORDER;i++)
866
295k
         mem[i]=SHL32(st->mem_sp[i],1);
867
1.21M
      for (i=0;i<NB_SUBFRAME_SIZE;i++)
868
1.18M
         exc[i] = VERY_SMALL;
869
#ifdef SHORTCUTS2
870
      iir_mem16(exc, interp_qlpc, exc, response_bound, NB_ORDER, mem, stack);
871
      for (i=0;i<NB_ORDER;i++)
872
         mem[i]=SHL32(st->mem_sw[i],1);
873
      filter10(exc, st->bw_lpc1, st->bw_lpc2, exc, response_bound, mem, stack);
874
      SPEEX_MEMSET(&exc[response_bound], 0, NB_SUBFRAME_SIZE-response_bound);
875
#else
876
29.5k
      iir_mem16(exc, interp_qlpc, exc, NB_SUBFRAME_SIZE, NB_ORDER, mem, stack);
877
325k
      for (i=0;i<NB_ORDER;i++)
878
295k
         mem[i]=SHL32(st->mem_sw[i],1);
879
29.5k
      filter10(exc, bw_lpc1, bw_lpc2, exc, NB_SUBFRAME_SIZE, mem, stack);
880
29.5k
#endif
881
882
      /* Compute weighted signal */
883
325k
      for (i=0;i<NB_ORDER;i++)
884
295k
         mem[i]=st->mem_sw[i];
885
29.5k
      filter10(sw, bw_lpc1, bw_lpc2, sw, NB_SUBFRAME_SIZE, mem, stack);
886
887
29.5k
      if (st->complexity==0)
888
72.2k
         for (i=0;i<NB_ORDER;i++)
889
65.7k
            st->mem_sw[i]=mem[i];
890
891
      /* Compute target signal (saturation prevents overflows on clipped input speech) */
892
1.21M
      for (i=0;i<NB_SUBFRAME_SIZE;i++)
893
1.18M
         target[i]=EXTRACT16(SATURATE(SUB32(sw[i],PSHR32(exc[i],1)),32767));
894
895
1.21M
      for (i=0;i<NB_SUBFRAME_SIZE;i++)
896
1.18M
         exc[i] = inBuf[i];
897
29.5k
      fir_mem16(exc, interp_qlpc, exc, NB_SUBFRAME_SIZE, NB_ORDER, st->mem_exc2, stack);
898
      /* If we have a long-term predictor (otherwise, something's wrong) */
899
29.5k
      speex_assert (SUBMODE(ltp_quant));
900
29.5k
      {
901
29.5k
         int pit_min, pit_max;
902
         /* Long-term prediction */
903
29.5k
         if (SUBMODE(lbr_pitch) != -1)
904
20.2k
         {
905
            /* Low bit-rate pitch handling */
906
20.2k
            int margin;
907
20.2k
            margin = SUBMODE(lbr_pitch);
908
20.2k
            if (margin)
909
0
            {
910
0
               if (ol_pitch < NB_PITCH_START+margin-1)
911
0
                  ol_pitch=NB_PITCH_START+margin-1;
912
0
               if (ol_pitch > NB_PITCH_END-margin)
913
0
                  ol_pitch=NB_PITCH_END-margin;
914
0
               pit_min = ol_pitch-margin+1;
915
0
               pit_max = ol_pitch+margin;
916
20.2k
            } else {
917
20.2k
               pit_min=pit_max=ol_pitch;
918
20.2k
            }
919
20.2k
         } else {
920
9.37k
            pit_min = NB_PITCH_START;
921
9.37k
            pit_max = NB_PITCH_END;
922
9.37k
         }
923
924
         /* Force pitch to use only the current frame if needed */
925
29.5k
         if (st->bounded_pitch && pit_max>offset)
926
9.00k
            pit_max=offset;
927
928
         /* Perform pitch search */
929
29.5k
         pitch = SUBMODE(ltp_quant)(target, sw, interp_qlpc, bw_lpc1, bw_lpc2,
930
29.5k
                                    exc32, SUBMODE(ltp_params), pit_min, pit_max, ol_pitch_coef,
931
29.5k
                                    NB_ORDER, NB_SUBFRAME_SIZE, bits, stack,
932
29.5k
                                    exc, syn_resp, st->complexity, 0, st->plc_tuning, &st->cumul_gain);
933
934
29.5k
         st->pitch[sub]=pitch;
935
29.5k
      }
936
      /* Quantization of innovation */
937
29.5k
      SPEEX_MEMSET(innov, 0, NB_SUBFRAME_SIZE);
938
939
      /* FIXME: Make sure this is safe from overflows (so far so good) */
940
1.21M
      for (i=0;i<NB_SUBFRAME_SIZE;i++)
941
1.18M
         exc[i] = EXTRACT16(SUB32(EXTEND32(exc[i]), PSHR32(exc32[i],SIG_SHIFT-1)));
942
943
29.5k
      ener = SHL32(EXTEND32(compute_rms16(exc, NB_SUBFRAME_SIZE)),SIG_SHIFT);
944
945
      /*FIXME: Should use DIV32_16 and make sure result fits in 16 bits */
946
#ifdef FIXED_POINT
947
      {
948
         spx_word32_t f = PDIV32(ener,PSHR32(ol_gain,SIG_SHIFT));
949
         if (f<=32767)
950
            fine_gain = f;
951
         else
952
            fine_gain = 32767;
953
      }
954
#else
955
29.5k
      fine_gain = PDIV32_16(ener,PSHR32(ol_gain,SIG_SHIFT));
956
29.5k
#endif
957
      /* Calculate gain correction for the sub-frame (if any) */
958
29.5k
      if (SUBMODE(have_subframe_gain))
959
9.37k
      {
960
9.37k
         int qe;
961
9.37k
         if (SUBMODE(have_subframe_gain)==3)
962
5.42k
         {
963
5.42k
            qe = scal_quant(fine_gain, exc_gain_quant_scal3_bound, 8);
964
5.42k
            speex_bits_pack(bits, qe, 3);
965
5.42k
            ener=MULT16_32_Q14(exc_gain_quant_scal3[qe],ol_gain);
966
5.42k
         } else {
967
3.94k
            qe = scal_quant(fine_gain, exc_gain_quant_scal1_bound, 2);
968
3.94k
            speex_bits_pack(bits, qe, 1);
969
3.94k
            ener=MULT16_32_Q14(exc_gain_quant_scal1[qe],ol_gain);
970
3.94k
         }
971
20.2k
      } else {
972
20.2k
         ener=ol_gain;
973
20.2k
      }
974
975
      /*printf ("%f %f\n", ener, ol_gain);*/
976
977
      /* Normalize innovation */
978
29.5k
      signal_div(target, target, ener, NB_SUBFRAME_SIZE);
979
980
      /* Quantize innovation */
981
29.5k
      speex_assert (SUBMODE(innovation_quant));
982
29.5k
      {
983
         /* Codebook search */
984
29.5k
         SUBMODE(innovation_quant)(target, interp_qlpc, bw_lpc1, bw_lpc2,
985
29.5k
                  SUBMODE(innovation_params), NB_ORDER, NB_SUBFRAME_SIZE,
986
29.5k
                  innov, syn_resp, bits, stack, st->complexity, SUBMODE(double_codebook));
987
988
         /* De-normalize innovation and update excitation */
989
29.5k
         signal_mul(innov, innov, ener, NB_SUBFRAME_SIZE);
990
991
         /* In some (rare) modes, we do a second search (more bits) to reduce noise even more */
992
29.5k
         if (SUBMODE(double_codebook)) {
993
1.28k
            char *tmp_stack=stack;
994
1.28k
            VARDECL(spx_sig_t *innov2);
995
1.28k
            ALLOC(innov2, NB_SUBFRAME_SIZE, spx_sig_t);
996
1.28k
            SPEEX_MEMSET(innov2, 0, NB_SUBFRAME_SIZE);
997
52.4k
            for (i=0;i<NB_SUBFRAME_SIZE;i++)
998
51.2k
               target[i]=MULT16_16_P13(QCONST16(2.2f,13), target[i]);
999
1.28k
            SUBMODE(innovation_quant)(target, interp_qlpc, bw_lpc1, bw_lpc2,
1000
1.28k
                                      SUBMODE(innovation_params), NB_ORDER, NB_SUBFRAME_SIZE,
1001
1.28k
                                      innov2, syn_resp, bits, stack, st->complexity, 0);
1002
1.28k
            signal_mul(innov2, innov2, MULT16_32_Q15(QCONST16(0.454545f,15),ener), NB_SUBFRAME_SIZE);
1003
52.4k
            for (i=0;i<NB_SUBFRAME_SIZE;i++)
1004
51.2k
               innov[i] = ADD32(innov[i],innov2[i]);
1005
1.28k
            stack = tmp_stack;
1006
1.28k
         }
1007
1.21M
         for (i=0;i<NB_SUBFRAME_SIZE;i++)
1008
1.18M
            exc[i] = EXTRACT16(SATURATE32(PSHR32(ADD32(SHL32(exc32[i],1),innov[i]),SIG_SHIFT),32767));
1009
29.5k
         if (st->innov_rms_save)
1010
9.92k
            st->innov_rms_save[sub] = compute_rms(innov, NB_SUBFRAME_SIZE);
1011
29.5k
      }
1012
1013
      /* Final signal synthesis from excitation */
1014
29.5k
      iir_mem16(exc, interp_qlpc, sw, NB_SUBFRAME_SIZE, NB_ORDER, st->mem_sp, stack);
1015
1016
      /* Compute weighted signal again, from synthesized speech (not sure it's the right thing) */
1017
29.5k
      if (st->complexity!=0)
1018
23.0k
         filter10(sw, bw_lpc1, bw_lpc2, sw, NB_SUBFRAME_SIZE, st->mem_sw, stack);
1019
1020
29.5k
   }
1021
1022
   /* Store the LSPs for interpolation in the next frame */
1023
7.39k
   if (st->submodeID>=1)
1024
7.39k
   {
1025
81.3k
      for (i=0;i<NB_ORDER;i++)
1026
73.9k
         st->old_lsp[i] = lsp[i];
1027
81.3k
      for (i=0;i<NB_ORDER;i++)
1028
73.9k
         st->old_qlsp[i] = qlsp[i];
1029
7.39k
   }
1030
1031
#ifdef VORBIS_PSYCHO
1032
   if (st->submodeID>=1)
1033
      SPEEX_COPY(st->old_curve, st->curve, 128);
1034
#endif
1035
1036
7.39k
   if (st->submodeID==1)
1037
3.81k
   {
1038
3.81k
#ifndef DISABLE_VBR
1039
3.81k
      if (st->dtx_count)
1040
1.70k
         speex_bits_pack(bits, 15, 4);
1041
2.11k
      else
1042
2.11k
#endif
1043
2.11k
         speex_bits_pack(bits, 0, 4);
1044
3.81k
   }
1045
1046
   /* The next frame will not be the first (Duh!) */
1047
7.39k
   st->first = 0;
1048
7.39k
   SPEEX_COPY(st->winBuf, in+2*NB_FRAME_SIZE-NB_WINDOW_SIZE, NB_WINDOW_SIZE-NB_FRAME_SIZE);
1049
1050
7.39k
   if (SUBMODE(innovation_quant) == noise_codebook_quant || st->submodeID==0)
1051
3.81k
      st->bounded_pitch = 1;
1052
3.58k
   else
1053
3.58k
      st->bounded_pitch = 0;
1054
1055
7.39k
   return 1;
1056
8.63k
}
nb_encode
Line
Count
Source
407
17.9k
{
408
17.9k
   EncState *st;
409
17.9k
   int i, sub, roots;
410
17.9k
   int ol_pitch;
411
17.9k
   spx_word16_t ol_pitch_coef;
412
17.9k
   spx_word32_t ol_gain;
413
17.9k
   VARDECL(spx_word16_t *target);
414
17.9k
   VARDECL(spx_sig_t *innov);
415
17.9k
   VARDECL(spx_word32_t *exc32);
416
17.9k
   VARDECL(spx_mem_t *mem);
417
17.9k
   VARDECL(spx_coef_t *bw_lpc1);
418
17.9k
   VARDECL(spx_coef_t *bw_lpc2);
419
17.9k
   VARDECL(spx_coef_t *lpc);
420
17.9k
   VARDECL(spx_lsp_t *lsp);
421
17.9k
   VARDECL(spx_lsp_t *qlsp);
422
17.9k
   VARDECL(spx_lsp_t *interp_lsp);
423
17.9k
   VARDECL(spx_lsp_t *interp_qlsp);
424
17.9k
   VARDECL(spx_coef_t *interp_lpc);
425
17.9k
   VARDECL(spx_coef_t *interp_qlpc);
426
17.9k
   char *stack;
427
17.9k
   VARDECL(spx_word16_t *syn_resp);
428
429
17.9k
   spx_word32_t ener=0;
430
17.9k
   spx_word16_t fine_gain;
431
17.9k
   spx_word16_t *in = (spx_word16_t*)vin;
432
433
17.9k
   st=(EncState *)state;
434
17.9k
   stack=st->stack;
435
436
17.9k
   ALLOC(lpc, NB_ORDER, spx_coef_t);
437
17.9k
   ALLOC(bw_lpc1, NB_ORDER, spx_coef_t);
438
17.9k
   ALLOC(bw_lpc2, NB_ORDER, spx_coef_t);
439
17.9k
   ALLOC(lsp, NB_ORDER, spx_lsp_t);
440
17.9k
   ALLOC(qlsp, NB_ORDER, spx_lsp_t);
441
17.9k
   ALLOC(interp_lsp, NB_ORDER, spx_lsp_t);
442
17.9k
   ALLOC(interp_qlsp, NB_ORDER, spx_lsp_t);
443
17.9k
   ALLOC(interp_lpc, NB_ORDER, spx_coef_t);
444
17.9k
   ALLOC(interp_qlpc, NB_ORDER, spx_coef_t);
445
446
17.9k
   st->exc = st->excBuf + NB_PITCH_END + 2;
447
17.9k
   st->sw = st->swBuf + NB_PITCH_END + 2;
448
   /* Move signals 1 frame towards the past */
449
17.9k
   SPEEX_MOVE(st->excBuf, st->excBuf+NB_FRAME_SIZE, NB_PITCH_END+2);
450
17.9k
   SPEEX_MOVE(st->swBuf, st->swBuf+NB_FRAME_SIZE, NB_PITCH_END+2);
451
452
17.9k
   if (st->highpass_enabled)
453
6.78k
      highpass(in, in, NB_FRAME_SIZE, (st->isWideband?HIGHPASS_WIDEBAND:HIGHPASS_NARROWBAND)|HIGHPASS_INPUT, st->mem_hp);
454
455
17.9k
   {
456
17.9k
      VARDECL(spx_word16_t *w_sig);
457
17.9k
      VARDECL(spx_word16_t *autocorr);
458
17.9k
      ALLOC(w_sig, NB_WINDOW_SIZE, spx_word16_t);
459
17.9k
      ALLOC(autocorr, NB_ORDER+1, spx_word16_t);
460
      /* Window for analysis */
461
737k
      for (i=0;i<NB_WINDOW_SIZE-NB_FRAME_SIZE;i++)
462
719k
         w_sig[i] = MULT16_16_Q15(st->winBuf[i],st->window[i]);
463
2.89M
      for (;i<NB_WINDOW_SIZE;i++)
464
2.87M
         w_sig[i] = MULT16_16_Q15(in[i-NB_WINDOW_SIZE+NB_FRAME_SIZE],st->window[i]);
465
      /* Compute auto-correlation */
466
17.9k
      _spx_autocorr(w_sig, autocorr, NB_ORDER+1, NB_WINDOW_SIZE);
467
17.9k
      autocorr[0] = ADD16(autocorr[0],MULT16_16_Q15(autocorr[0],st->lpc_floor)); /* Noise floor in auto-correlation domain */
468
469
      /* Lag windowing: equivalent to filtering in the power-spectrum domain */
470
215k
      for (i=0;i<NB_ORDER+1;i++)
471
197k
         autocorr[i] = MULT16_16_Q15(autocorr[i],st->lagWindow[i]);
472
17.9k
      autocorr[0] = ADD16(autocorr[0],1);
473
474
      /* Levinson-Durbin */
475
17.9k
      _spx_lpc(lpc, autocorr, NB_ORDER);
476
      /* LPC to LSPs (x-domain) transform */
477
17.9k
      roots=lpc_to_lsp (lpc, NB_ORDER, lsp, 10, LSP_DELTA1, stack);
478
      /* Check if we found all the roots */
479
17.9k
      if (roots!=NB_ORDER)
480
45
      {
481
         /*If we can't find all LSP's, do some damage control and use previous filter*/
482
495
         for (i=0;i<NB_ORDER;i++)
483
450
         {
484
450
            lsp[i]=st->old_lsp[i];
485
450
         }
486
45
      }
487
17.9k
   }
488
489
490
491
492
   /* Whole frame analysis (open-loop estimation of pitch and excitation gain) */
493
17.9k
   {
494
17.9k
      int diff = NB_WINDOW_SIZE-NB_FRAME_SIZE;
495
17.9k
      if (st->first)
496
37.3k
         for (i=0;i<NB_ORDER;i++)
497
33.9k
            interp_lsp[i] = lsp[i];
498
14.5k
      else
499
14.5k
         lsp_interpolate(st->old_lsp, lsp, interp_lsp, NB_ORDER, NB_NB_SUBFRAMES, NB_NB_SUBFRAMES<<1, LSP_MARGIN);
500
501
      /* Compute interpolated LPCs (unquantized) for whole frame*/
502
17.9k
      lsp_to_lpc(interp_lsp, interp_lpc, NB_ORDER,stack);
503
504
505
      /*Open-loop pitch*/
506
17.9k
      if (!st->submodes[st->submodeID] || (st->complexity>2 && SUBMODE(have_subframe_gain)<3) || SUBMODE(forced_pitch_gain) || SUBMODE(lbr_pitch) != -1
507
3.99k
#ifndef DISABLE_VBR
508
3.99k
           || st->vbr_enabled || st->vad_enabled
509
17.9k
#endif
510
17.9k
                  )
511
16.6k
      {
512
16.6k
         int nol_pitch[6];
513
16.6k
         spx_word16_t nol_pitch_coef[6];
514
515
16.6k
         bw_lpc(QCONST16(0.9,15), interp_lpc, bw_lpc1, NB_ORDER);
516
16.6k
         bw_lpc(QCONST16(0.55,15), interp_lpc, bw_lpc2, NB_ORDER);
517
518
16.6k
         SPEEX_COPY(st->sw, st->winBuf, diff);
519
16.6k
         SPEEX_COPY(st->sw+diff, in, NB_FRAME_SIZE-diff);
520
16.6k
         filter10(st->sw, bw_lpc1, bw_lpc2, st->sw, NB_FRAME_SIZE, st->mem_sw_whole, stack);
521
522
16.6k
         open_loop_nbest_pitch(st->sw, NB_PITCH_START, NB_PITCH_END, NB_FRAME_SIZE,
523
16.6k
                               nol_pitch, nol_pitch_coef, 6, stack);
524
16.6k
         ol_pitch=nol_pitch[0];
525
16.6k
         ol_pitch_coef = nol_pitch_coef[0];
526
         /*Try to remove pitch multiples*/
527
99.8k
         for (i=1;i<6;i++)
528
83.2k
         {
529
83.2k
#ifdef FIXED_POINT
530
83.2k
            if ((nol_pitch_coef[i]>MULT16_16_Q15(nol_pitch_coef[0],27853)) &&
531
#else
532
            if ((nol_pitch_coef[i]>.85*nol_pitch_coef[0]) &&
533
#endif
534
17.3k
                (ABS(2*nol_pitch[i]-ol_pitch)<=2 || ABS(3*nol_pitch[i]-ol_pitch)<=3 ||
535
16.9k
                 ABS(4*nol_pitch[i]-ol_pitch)<=4 || ABS(5*nol_pitch[i]-ol_pitch)<=5))
536
702
            {
537
               /*ol_pitch_coef=nol_pitch_coef[i];*/
538
702
               ol_pitch = nol_pitch[i];
539
702
            }
540
83.2k
         }
541
         /*if (ol_pitch>50)
542
           ol_pitch/=2;*/
543
         /*ol_pitch_coef = sqrt(ol_pitch_coef);*/
544
545
16.6k
      } else {
546
1.34k
         ol_pitch=0;
547
1.34k
         ol_pitch_coef=0;
548
1.34k
      }
549
550
      /*Compute "real" excitation*/
551
      /*SPEEX_COPY(st->exc, st->winBuf, diff);
552
      SPEEX_COPY(st->exc+diff, in, NB_FRAME_SIZE-diff);*/
553
17.9k
      fir_mem16(st->winBuf, interp_lpc, st->exc, diff, NB_ORDER, st->mem_exc, stack);
554
17.9k
      fir_mem16(in, interp_lpc, st->exc+diff, NB_FRAME_SIZE-diff, NB_ORDER, st->mem_exc, stack);
555
556
      /* Compute open-loop excitation gain */
557
17.9k
      {
558
17.9k
         spx_word16_t g = compute_rms16(st->exc, NB_FRAME_SIZE);
559
17.9k
         if (st->submodeID!=1 && ol_pitch>0)
560
8.08k
            ol_gain = MULT16_16(g, MULT16_16_Q14(QCONST16(1.1,14),
561
17.9k
                                spx_sqrt(QCONST32(1.,28)-MULT16_32_Q15(QCONST16(.8,15),SHL32(MULT16_16(ol_pitch_coef,ol_pitch_coef),16)))));
562
9.89k
         else
563
9.89k
            ol_gain = SHL32(EXTEND32(g),SIG_SHIFT);
564
17.9k
      }
565
17.9k
   }
566
567
#ifdef VORBIS_PSYCHO
568
   SPEEX_MOVE(st->psy_window, st->psy_window+NB_FRAME_SIZE, 256-NB_FRAME_SIZE);
569
   SPEEX_COPY(&st->psy_window[256-NB_FRAME_SIZE], in, NB_FRAME_SIZE);
570
   compute_curve(st->psy, st->psy_window, st->curve);
571
   /*print_vec(st->curve, 128, "curve");*/
572
   if (st->first)
573
      SPEEX_COPY(st->old_curve, st->curve, 128);
574
#endif
575
576
   /*VBR stuff*/
577
17.9k
#ifndef DISABLE_VBR
578
17.9k
   if (st->vbr_enabled||st->vad_enabled)
579
13.9k
   {
580
13.9k
      float lsp_dist=0;
581
153k
      for (i=0;i<NB_ORDER;i++)
582
139k
         lsp_dist += (st->old_lsp[i] - lsp[i])*(st->old_lsp[i] - lsp[i]);
583
13.9k
      lsp_dist /= LSP_SCALING*LSP_SCALING;
584
585
13.9k
      if (st->abr_enabled)
586
3.56k
      {
587
3.56k
         float qual_change=0;
588
3.56k
         if (st->abr_drift2 * st->abr_drift > 0)
589
3.20k
         {
590
            /* Only adapt if long-term and short-term drift are the same sign */
591
3.20k
            qual_change = -.00001*st->abr_drift/(1+st->abr_count);
592
3.20k
            if (qual_change>.05)
593
0
               qual_change=.05;
594
3.20k
            if (qual_change<-.05)
595
1.01k
               qual_change=-.05;
596
3.20k
         }
597
3.56k
         st->vbr_quality += qual_change;
598
3.56k
         if (st->vbr_quality>10)
599
0
            st->vbr_quality=10;
600
3.56k
         if (st->vbr_quality<0)
601
3.20k
            st->vbr_quality=0;
602
3.56k
      }
603
604
13.9k
      st->relative_quality = vbr_analysis(&st->vbr, in, NB_FRAME_SIZE, ol_pitch, GAIN_SCALING_1*ol_pitch_coef);
605
      /*if (delta_qual<0)*/
606
      /*  delta_qual*=.1*(3+st->vbr_quality);*/
607
13.9k
      if (st->vbr_enabled)
608
10.8k
      {
609
10.8k
         spx_int32_t mode;
610
10.8k
         int choice=0;
611
10.8k
         float min_diff=100;
612
10.8k
         mode = 8;
613
97.9k
         while (mode)
614
87.0k
         {
615
87.0k
            int v1;
616
87.0k
            float thresh;
617
87.0k
            v1=(int)floor(st->vbr_quality);
618
87.0k
            if (v1==10)
619
3.17k
               thresh = vbr_nb_thresh[mode][v1];
620
83.8k
            else
621
83.8k
               thresh = (st->vbr_quality-v1)*vbr_nb_thresh[mode][v1+1] + (1+v1-st->vbr_quality)*vbr_nb_thresh[mode][v1];
622
87.0k
            if (st->relative_quality > thresh &&
623
25.1k
                st->relative_quality-thresh<min_diff)
624
12.0k
            {
625
12.0k
               choice = mode;
626
12.0k
               min_diff = st->relative_quality-thresh;
627
12.0k
            }
628
87.0k
            mode--;
629
87.0k
         }
630
10.8k
         mode=choice;
631
10.8k
         if (mode==0)
632
2.66k
         {
633
2.66k
            if (st->dtx_count==0 || lsp_dist>.05 || !st->dtx_enabled || st->dtx_count>20)
634
1.69k
            {
635
1.69k
               mode=1;
636
1.69k
               st->dtx_count=1;
637
1.69k
            } else {
638
964
               mode=0;
639
964
               st->dtx_count++;
640
964
            }
641
8.22k
         } else {
642
8.22k
            st->dtx_count=0;
643
8.22k
         }
644
645
10.8k
         speex_encoder_ctl(state, SPEEX_SET_MODE, &mode);
646
10.8k
         if (st->vbr_max>0)
647
8.08k
         {
648
8.08k
            spx_int32_t rate;
649
8.08k
            speex_encoder_ctl(state, SPEEX_GET_BITRATE, &rate);
650
8.08k
            if (rate > st->vbr_max)
651
3.15k
            {
652
3.15k
               rate = st->vbr_max;
653
3.15k
               speex_encoder_ctl(state, SPEEX_SET_BITRATE, &rate);
654
3.15k
            }
655
8.08k
         }
656
657
10.8k
         if (st->abr_enabled)
658
3.56k
         {
659
3.56k
            spx_int32_t bitrate;
660
3.56k
            speex_encoder_ctl(state, SPEEX_GET_BITRATE, &bitrate);
661
3.56k
            st->abr_drift+=(bitrate-st->abr_enabled);
662
3.56k
            st->abr_drift2 = .95*st->abr_drift2 + .05*(bitrate-st->abr_enabled);
663
3.56k
            st->abr_count += 1.0;
664
3.56k
         }
665
666
10.8k
      } else {
667
         /*VAD only case*/
668
3.08k
         int mode;
669
3.08k
         if (st->relative_quality<2)
670
747
         {
671
747
            if (st->dtx_count==0 || lsp_dist>.05 || !st->dtx_enabled || st->dtx_count>20)
672
438
            {
673
438
               st->dtx_count=1;
674
438
               mode=1;
675
438
            } else {
676
309
               mode=0;
677
309
               st->dtx_count++;
678
309
            }
679
2.34k
         } else {
680
2.34k
            st->dtx_count = 0;
681
2.34k
            mode=st->submodeSelect;
682
2.34k
         }
683
         /*speex_encoder_ctl(state, SPEEX_SET_MODE, &mode);*/
684
3.08k
         st->submodeID=mode;
685
3.08k
      }
686
13.9k
   } else {
687
4.01k
      st->relative_quality = -1;
688
4.01k
   }
689
17.9k
#endif /* #ifndef DISABLE_VBR */
690
691
17.9k
   if (st->encode_submode)
692
17.9k
   {
693
      /* First, transmit a zero for narrowband */
694
17.9k
      speex_bits_pack(bits, 0, 1);
695
696
      /* Transmit the sub-mode we use for this frame */
697
17.9k
      speex_bits_pack(bits, st->submodeID, NB_SUBMODE_BITS);
698
699
17.9k
   }
700
701
   /* If null mode (no transmission), just set a couple things to zero*/
702
17.9k
   if (st->submodes[st->submodeID] == NULL)
703
1.18k
   {
704
191k
      for (i=0;i<NB_FRAME_SIZE;i++)
705
190k
         st->exc[i]=st->sw[i]=VERY_SMALL;
706
707
13.0k
      for (i=0;i<NB_ORDER;i++)
708
11.8k
         st->mem_sw[i]=0;
709
1.18k
      st->first=1;
710
1.18k
      st->bounded_pitch = 1;
711
712
1.18k
      SPEEX_COPY(st->winBuf, in+2*NB_FRAME_SIZE-NB_WINDOW_SIZE, NB_WINDOW_SIZE-NB_FRAME_SIZE);
713
714
      /* Clear memory (no need to really compute it) */
715
13.0k
      for (i=0;i<NB_ORDER;i++)
716
11.8k
         st->mem_sp[i] = 0;
717
1.18k
      return 0;
718
719
1.18k
   }
720
721
   /* LSP Quantization */
722
16.7k
   if (st->first)
723
2.55k
   {
724
28.1k
      for (i=0;i<NB_ORDER;i++)
725
25.5k
         st->old_lsp[i] = lsp[i];
726
2.55k
   }
727
728
729
   /*Quantize LSPs*/
730
16.7k
#if 1 /*0 for unquantized*/
731
16.7k
   SUBMODE(lsp_quant)(lsp, qlsp, NB_ORDER, bits);
732
#else
733
   for (i=0;i<NB_ORDER;i++)
734
     qlsp[i]=lsp[i];
735
#endif
736
737
   /*If we use low bit-rate pitch mode, transmit open-loop pitch*/
738
16.7k
   if (SUBMODE(lbr_pitch)!=-1)
739
11.2k
   {
740
11.2k
      speex_bits_pack(bits, ol_pitch-NB_PITCH_START, 7);
741
11.2k
   }
742
743
16.7k
   if (SUBMODE(forced_pitch_gain))
744
10.1k
   {
745
10.1k
      int quant;
746
      /* This just damps the pitch a bit, because it tends to be too aggressive when forced */
747
10.1k
      ol_pitch_coef = MULT16_16_Q15(QCONST16(.9,15), ol_pitch_coef);
748
10.1k
#ifdef FIXED_POINT
749
10.1k
      quant = PSHR16(MULT16_16_16(15, ol_pitch_coef),GAIN_SHIFT);
750
#else
751
      quant = (int)floor(.5+15*ol_pitch_coef*GAIN_SCALING_1);
752
#endif
753
10.1k
      if (quant>15)
754
62
         quant=15;
755
10.1k
      if (quant<0)
756
0
         quant=0;
757
10.1k
      speex_bits_pack(bits, quant, 4);
758
10.1k
      ol_pitch_coef=MULT16_16_P15(QCONST16(0.066667,15),SHL16(quant,GAIN_SHIFT));
759
10.1k
   }
760
761
762
   /*Quantize and transmit open-loop excitation gain*/
763
16.7k
#ifdef FIXED_POINT
764
16.7k
   {
765
16.7k
      int qe = scal_quant32(ol_gain, ol_gain_table, 32);
766
      /*ol_gain = exp(qe/3.5)*SIG_SCALING;*/
767
16.7k
      ol_gain = MULT16_32_Q15(28406,ol_gain_table[qe]);
768
16.7k
      speex_bits_pack(bits, qe, 5);
769
16.7k
   }
770
#else
771
   {
772
      int qe = (int)(floor(.5+3.5*log(ol_gain*1.0/SIG_SCALING)));
773
      if (qe<0)
774
         qe=0;
775
      if (qe>31)
776
         qe=31;
777
      ol_gain = exp(qe/3.5)*SIG_SCALING;
778
      speex_bits_pack(bits, qe, 5);
779
   }
780
#endif
781
782
783
784
   /* Special case for first frame */
785
16.7k
   if (st->first)
786
2.55k
   {
787
28.1k
      for (i=0;i<NB_ORDER;i++)
788
25.5k
         st->old_qlsp[i] = qlsp[i];
789
2.55k
   }
790
791
   /* Target signal */
792
16.7k
   ALLOC(target, NB_SUBFRAME_SIZE, spx_word16_t);
793
16.7k
   ALLOC(innov, NB_SUBFRAME_SIZE, spx_sig_t);
794
16.7k
   ALLOC(exc32, NB_SUBFRAME_SIZE, spx_word32_t);
795
16.7k
   ALLOC(syn_resp, NB_SUBFRAME_SIZE, spx_word16_t);
796
16.7k
   ALLOC(mem, NB_ORDER, spx_mem_t);
797
798
   /* Loop on sub-frames */
799
83.9k
   for (sub=0;sub<NB_NB_SUBFRAMES;sub++)
800
67.1k
   {
801
67.1k
      int   offset;
802
67.1k
      spx_word16_t *sw;
803
67.1k
      spx_word16_t *exc, *inBuf;
804
67.1k
      int pitch;
805
67.1k
      int response_bound = NB_SUBFRAME_SIZE;
806
807
      /* Offset relative to start of frame */
808
67.1k
      offset = NB_SUBFRAME_SIZE*sub;
809
      /* Excitation */
810
67.1k
      exc=st->exc+offset;
811
      /* Weighted signal */
812
67.1k
      sw=st->sw+offset;
813
814
      /* LSP interpolation (quantized and unquantized) */
815
67.1k
      lsp_interpolate(st->old_lsp, lsp, interp_lsp, NB_ORDER, sub, NB_NB_SUBFRAMES, LSP_MARGIN);
816
67.1k
      lsp_interpolate(st->old_qlsp, qlsp, interp_qlsp, NB_ORDER, sub, NB_NB_SUBFRAMES, LSP_MARGIN);
817
818
      /* Compute interpolated LPCs (quantized and unquantized) */
819
67.1k
      lsp_to_lpc(interp_lsp, interp_lpc, NB_ORDER,stack);
820
821
67.1k
      lsp_to_lpc(interp_qlsp, interp_qlpc, NB_ORDER, stack);
822
823
      /* Compute analysis filter gain at w=pi (for use in SB-CELP) */
824
67.1k
      {
825
67.1k
         spx_word32_t pi_g=LPC_SCALING;
826
403k
         for (i=0;i<NB_ORDER;i+=2)
827
335k
         {
828
            /*pi_g += -st->interp_qlpc[i] +  st->interp_qlpc[i+1];*/
829
335k
            pi_g = ADD32(pi_g, SUB32(EXTEND32(interp_qlpc[i+1]),EXTEND32(interp_qlpc[i])));
830
335k
         }
831
67.1k
         st->pi_gain[sub] = pi_g;
832
67.1k
      }
833
834
#ifdef VORBIS_PSYCHO
835
      {
836
         float curr_curve[128];
837
         float fact = ((float)sub+1.0f)/NB_NB_SUBFRAMES;
838
         for (i=0;i<128;i++)
839
            curr_curve[i] = (1.0f-fact)*st->old_curve[i] + fact*st->curve[i];
840
         curve_to_lpc(st->psy, curr_curve, bw_lpc1, bw_lpc2, 10);
841
      }
842
#else
843
      /* Compute bandwidth-expanded (unquantized) LPCs for perceptual weighting */
844
67.1k
      bw_lpc(st->gamma1, interp_lpc, bw_lpc1, NB_ORDER);
845
67.1k
      bw_lpc(st->gamma2, interp_lpc, bw_lpc2, NB_ORDER);
846
      /*print_vec(st->bw_lpc1, 10, "bw_lpc");*/
847
67.1k
#endif
848
849
      /*FIXME: This will break if we change the window size */
850
67.1k
      speex_assert(NB_WINDOW_SIZE-NB_FRAME_SIZE == NB_SUBFRAME_SIZE);
851
67.1k
      if (sub==0)
852
16.7k
         inBuf = st->winBuf;
853
50.3k
      else
854
50.3k
         inBuf = &in[((sub-1)*NB_SUBFRAME_SIZE)];
855
2.75M
      for (i=0;i<NB_SUBFRAME_SIZE;i++)
856
2.68M
         sw[i] = inBuf[i];
857
858
67.1k
      if (st->complexity==0)
859
13.9k
         response_bound >>= 1;
860
67.1k
      compute_impulse_response(interp_qlpc, bw_lpc1, bw_lpc2, syn_resp, response_bound, NB_ORDER, stack);
861
345k
      for (i=response_bound;i<NB_SUBFRAME_SIZE;i++)
862
278k
         syn_resp[i]=VERY_SMALL;
863
864
      /* Compute zero response of A(z/g1) / ( A(z/g2) * A(z) ) */
865
739k
      for (i=0;i<NB_ORDER;i++)
866
671k
         mem[i]=SHL32(st->mem_sp[i],1);
867
2.75M
      for (i=0;i<NB_SUBFRAME_SIZE;i++)
868
2.68M
         exc[i] = VERY_SMALL;
869
#ifdef SHORTCUTS2
870
      iir_mem16(exc, interp_qlpc, exc, response_bound, NB_ORDER, mem, stack);
871
      for (i=0;i<NB_ORDER;i++)
872
         mem[i]=SHL32(st->mem_sw[i],1);
873
      filter10(exc, st->bw_lpc1, st->bw_lpc2, exc, response_bound, mem, stack);
874
      SPEEX_MEMSET(&exc[response_bound], 0, NB_SUBFRAME_SIZE-response_bound);
875
#else
876
67.1k
      iir_mem16(exc, interp_qlpc, exc, NB_SUBFRAME_SIZE, NB_ORDER, mem, stack);
877
739k
      for (i=0;i<NB_ORDER;i++)
878
671k
         mem[i]=SHL32(st->mem_sw[i],1);
879
67.1k
      filter10(exc, bw_lpc1, bw_lpc2, exc, NB_SUBFRAME_SIZE, mem, stack);
880
67.1k
#endif
881
882
      /* Compute weighted signal */
883
739k
      for (i=0;i<NB_ORDER;i++)
884
671k
         mem[i]=st->mem_sw[i];
885
67.1k
      filter10(sw, bw_lpc1, bw_lpc2, sw, NB_SUBFRAME_SIZE, mem, stack);
886
887
67.1k
      if (st->complexity==0)
888
152k
         for (i=0;i<NB_ORDER;i++)
889
139k
            st->mem_sw[i]=mem[i];
890
891
      /* Compute target signal (saturation prevents overflows on clipped input speech) */
892
2.75M
      for (i=0;i<NB_SUBFRAME_SIZE;i++)
893
2.68M
         target[i]=EXTRACT16(SATURATE(SUB32(sw[i],PSHR32(exc[i],1)),32767));
894
895
2.75M
      for (i=0;i<NB_SUBFRAME_SIZE;i++)
896
2.68M
         exc[i] = inBuf[i];
897
67.1k
      fir_mem16(exc, interp_qlpc, exc, NB_SUBFRAME_SIZE, NB_ORDER, st->mem_exc2, stack);
898
      /* If we have a long-term predictor (otherwise, something's wrong) */
899
67.1k
      speex_assert (SUBMODE(ltp_quant));
900
67.1k
      {
901
67.1k
         int pit_min, pit_max;
902
         /* Long-term prediction */
903
67.1k
         if (SUBMODE(lbr_pitch) != -1)
904
45.0k
         {
905
            /* Low bit-rate pitch handling */
906
45.0k
            int margin;
907
45.0k
            margin = SUBMODE(lbr_pitch);
908
45.0k
            if (margin)
909
0
            {
910
0
               if (ol_pitch < NB_PITCH_START+margin-1)
911
0
                  ol_pitch=NB_PITCH_START+margin-1;
912
0
               if (ol_pitch > NB_PITCH_END-margin)
913
0
                  ol_pitch=NB_PITCH_END-margin;
914
0
               pit_min = ol_pitch-margin+1;
915
0
               pit_max = ol_pitch+margin;
916
45.0k
            } else {
917
45.0k
               pit_min=pit_max=ol_pitch;
918
45.0k
            }
919
45.0k
         } else {
920
22.0k
            pit_min = NB_PITCH_START;
921
22.0k
            pit_max = NB_PITCH_END;
922
22.0k
         }
923
924
         /* Force pitch to use only the current frame if needed */
925
67.1k
         if (st->bounded_pitch && pit_max>offset)
926
20.7k
            pit_max=offset;
927
928
         /* Perform pitch search */
929
67.1k
         pitch = SUBMODE(ltp_quant)(target, sw, interp_qlpc, bw_lpc1, bw_lpc2,
930
67.1k
                                    exc32, SUBMODE(ltp_params), pit_min, pit_max, ol_pitch_coef,
931
67.1k
                                    NB_ORDER, NB_SUBFRAME_SIZE, bits, stack,
932
67.1k
                                    exc, syn_resp, st->complexity, 0, st->plc_tuning, &st->cumul_gain);
933
934
67.1k
         st->pitch[sub]=pitch;
935
67.1k
      }
936
      /* Quantization of innovation */
937
67.1k
      SPEEX_MEMSET(innov, 0, NB_SUBFRAME_SIZE);
938
939
      /* FIXME: Make sure this is safe from overflows (so far so good) */
940
2.75M
      for (i=0;i<NB_SUBFRAME_SIZE;i++)
941
2.68M
         exc[i] = EXTRACT16(SUB32(EXTEND32(exc[i]), PSHR32(exc32[i],SIG_SHIFT-1)));
942
943
67.1k
      ener = SHL32(EXTEND32(compute_rms16(exc, NB_SUBFRAME_SIZE)),SIG_SHIFT);
944
945
      /*FIXME: Should use DIV32_16 and make sure result fits in 16 bits */
946
67.1k
#ifdef FIXED_POINT
947
67.1k
      {
948
67.1k
         spx_word32_t f = PDIV32(ener,PSHR32(ol_gain,SIG_SHIFT));
949
67.1k
         if (f<=32767)
950
62.1k
            fine_gain = f;
951
5.07k
         else
952
5.07k
            fine_gain = 32767;
953
67.1k
      }
954
#else
955
      fine_gain = PDIV32_16(ener,PSHR32(ol_gain,SIG_SHIFT));
956
#endif
957
      /* Calculate gain correction for the sub-frame (if any) */
958
67.1k
      if (SUBMODE(have_subframe_gain))
959
22.0k
      {
960
22.0k
         int qe;
961
22.0k
         if (SUBMODE(have_subframe_gain)==3)
962
12.9k
         {
963
12.9k
            qe = scal_quant(fine_gain, exc_gain_quant_scal3_bound, 8);
964
12.9k
            speex_bits_pack(bits, qe, 3);
965
12.9k
            ener=MULT16_32_Q14(exc_gain_quant_scal3[qe],ol_gain);
966
12.9k
         } else {
967
9.12k
            qe = scal_quant(fine_gain, exc_gain_quant_scal1_bound, 2);
968
9.12k
            speex_bits_pack(bits, qe, 1);
969
9.12k
            ener=MULT16_32_Q14(exc_gain_quant_scal1[qe],ol_gain);
970
9.12k
         }
971
45.0k
      } else {
972
45.0k
         ener=ol_gain;
973
45.0k
      }
974
975
      /*printf ("%f %f\n", ener, ol_gain);*/
976
977
      /* Normalize innovation */
978
67.1k
      signal_div(target, target, ener, NB_SUBFRAME_SIZE);
979
980
      /* Quantize innovation */
981
67.1k
      speex_assert (SUBMODE(innovation_quant));
982
67.1k
      {
983
         /* Codebook search */
984
67.1k
         SUBMODE(innovation_quant)(target, interp_qlpc, bw_lpc1, bw_lpc2,
985
67.1k
                  SUBMODE(innovation_params), NB_ORDER, NB_SUBFRAME_SIZE,
986
67.1k
                  innov, syn_resp, bits, stack, st->complexity, SUBMODE(double_codebook));
987
988
         /* De-normalize innovation and update excitation */
989
67.1k
         signal_mul(innov, innov, ener, NB_SUBFRAME_SIZE);
990
991
         /* In some (rare) modes, we do a second search (more bits) to reduce noise even more */
992
67.1k
         if (SUBMODE(double_codebook)) {
993
2.22k
            char *tmp_stack=stack;
994
2.22k
            VARDECL(spx_sig_t *innov2);
995
2.22k
            ALLOC(innov2, NB_SUBFRAME_SIZE, spx_sig_t);
996
2.22k
            SPEEX_MEMSET(innov2, 0, NB_SUBFRAME_SIZE);
997
91.3k
            for (i=0;i<NB_SUBFRAME_SIZE;i++)
998
89.1k
               target[i]=MULT16_16_P13(QCONST16(2.2f,13), target[i]);
999
2.22k
            SUBMODE(innovation_quant)(target, interp_qlpc, bw_lpc1, bw_lpc2,
1000
2.22k
                                      SUBMODE(innovation_params), NB_ORDER, NB_SUBFRAME_SIZE,
1001
2.22k
                                      innov2, syn_resp, bits, stack, st->complexity, 0);
1002
2.22k
            signal_mul(innov2, innov2, MULT16_32_Q15(QCONST16(0.454545f,15),ener), NB_SUBFRAME_SIZE);
1003
91.3k
            for (i=0;i<NB_SUBFRAME_SIZE;i++)
1004
89.1k
               innov[i] = ADD32(innov[i],innov2[i]);
1005
2.22k
            stack = tmp_stack;
1006
2.22k
         }
1007
2.75M
         for (i=0;i<NB_SUBFRAME_SIZE;i++)
1008
2.68M
            exc[i] = EXTRACT16(SATURATE32(PSHR32(ADD32(SHL32(exc32[i],1),innov[i]),SIG_SHIFT),32767));
1009
67.1k
         if (st->innov_rms_save)
1010
19.0k
            st->innov_rms_save[sub] = compute_rms(innov, NB_SUBFRAME_SIZE);
1011
67.1k
      }
1012
1013
      /* Final signal synthesis from excitation */
1014
67.1k
      iir_mem16(exc, interp_qlpc, sw, NB_SUBFRAME_SIZE, NB_ORDER, st->mem_sp, stack);
1015
1016
      /* Compute weighted signal again, from synthesized speech (not sure it's the right thing) */
1017
67.1k
      if (st->complexity!=0)
1018
53.2k
         filter10(sw, bw_lpc1, bw_lpc2, sw, NB_SUBFRAME_SIZE, st->mem_sw, stack);
1019
1020
67.1k
   }
1021
1022
   /* Store the LSPs for interpolation in the next frame */
1023
16.7k
   if (st->submodeID>=1)
1024
16.7k
   {
1025
184k
      for (i=0;i<NB_ORDER;i++)
1026
167k
         st->old_lsp[i] = lsp[i];
1027
184k
      for (i=0;i<NB_ORDER;i++)
1028
167k
         st->old_qlsp[i] = qlsp[i];
1029
16.7k
   }
1030
1031
#ifdef VORBIS_PSYCHO
1032
   if (st->submodeID>=1)
1033
      SPEEX_COPY(st->old_curve, st->curve, 128);
1034
#endif
1035
1036
16.7k
   if (st->submodeID==1)
1037
8.41k
   {
1038
8.41k
#ifndef DISABLE_VBR
1039
8.41k
      if (st->dtx_count)
1040
2.21k
         speex_bits_pack(bits, 15, 4);
1041
6.19k
      else
1042
6.19k
#endif
1043
6.19k
         speex_bits_pack(bits, 0, 4);
1044
8.41k
   }
1045
1046
   /* The next frame will not be the first (Duh!) */
1047
16.7k
   st->first = 0;
1048
16.7k
   SPEEX_COPY(st->winBuf, in+2*NB_FRAME_SIZE-NB_WINDOW_SIZE, NB_WINDOW_SIZE-NB_FRAME_SIZE);
1049
1050
16.7k
   if (SUBMODE(innovation_quant) == noise_codebook_quant || st->submodeID==0)
1051
8.41k
      st->bounded_pitch = 1;
1052
8.38k
   else
1053
8.38k
      st->bounded_pitch = 0;
1054
1055
16.7k
   return 1;
1056
17.9k
}
1057
#endif /* DISABLE_ENCODER */
1058
1059
1060
#ifndef DISABLE_DECODER
1061
void *nb_decoder_init(const SpeexMode *m)
1062
4.03k
{
1063
4.03k
   DecState *st;
1064
4.03k
   const SpeexNBMode *mode;
1065
4.03k
   int i;
1066
1067
4.03k
   mode=(const SpeexNBMode*)m->mode;
1068
4.03k
   st = (DecState *)speex_alloc(sizeof(DecState));
1069
4.03k
   if (!st)
1070
0
      return NULL;
1071
4.03k
#if defined(VAR_ARRAYS) || defined (USE_ALLOCA)
1072
4.03k
   st->stack = NULL;
1073
#else
1074
   st->stack = (char*)speex_alloc_scratch(NB_DEC_STACK);
1075
#endif
1076
1077
4.03k
   st->mode=m;
1078
1079
1080
4.03k
   st->encode_submode = 1;
1081
1082
4.03k
   st->first=1;
1083
   /* Codec parameters, should eventually have several "modes"*/
1084
1085
4.03k
   st->submodes=mode->submodes;
1086
4.03k
   st->submodeID=mode->defaultSubmode;
1087
1088
4.03k
   st->lpc_enh_enabled=1;
1089
1090
4.03k
   SPEEX_MEMSET(st->excBuf, 0, NB_FRAME_SIZE + NB_PITCH_END);
1091
1092
4.03k
   st->last_pitch = 40;
1093
4.03k
   st->count_lost=0;
1094
4.03k
   st->pitch_gain_buf[0] = st->pitch_gain_buf[1] = st->pitch_gain_buf[2] = 0;
1095
4.03k
   st->pitch_gain_buf_idx = 0;
1096
4.03k
   st->seed = 1000;
1097
1098
4.03k
   st->sampling_rate=8000;
1099
4.03k
   st->last_ol_gain = 0;
1100
1101
4.03k
   st->user_callback.func = &speex_default_user_handler;
1102
4.03k
   st->user_callback.data = NULL;
1103
68.5k
   for (i=0;i<16;i++)
1104
64.5k
      st->speex_callbacks[i].func = NULL;
1105
1106
4.03k
   st->voc_m1=st->voc_m2=st->voc_mean=0;
1107
4.03k
   st->voc_offset=0;
1108
4.03k
   st->dtx_enabled=0;
1109
4.03k
   st->isWideband = 0;
1110
4.03k
   st->highpass_enabled = 1;
1111
1112
#ifdef ENABLE_VALGRIND
1113
   VALGRIND_MAKE_MEM_DEFINED(st, NB_DEC_STACK);
1114
#endif
1115
4.03k
   return st;
1116
4.03k
}
1117
1118
void nb_decoder_destroy(void *state)
1119
4.03k
{
1120
#if !(defined(VAR_ARRAYS) || defined (USE_ALLOCA))
1121
   DecState *st;
1122
   st=(DecState*)state;
1123
1124
   speex_free_scratch(st->stack);
1125
#endif
1126
1127
4.03k
   speex_free(state);
1128
4.03k
}
1129
1130
int nb_decoder_ctl(void *state, int request, void *ptr)
1131
170k
{
1132
170k
   DecState *st;
1133
170k
   st=(DecState*)state;
1134
170k
   switch(request)
1135
170k
   {
1136
0
   case SPEEX_SET_LOW_MODE:
1137
0
   case SPEEX_SET_MODE:
1138
0
      st->submodeID = (*(spx_int32_t*)ptr);
1139
0
      break;
1140
0
   case SPEEX_GET_LOW_MODE:
1141
0
   case SPEEX_GET_MODE:
1142
0
      (*(spx_int32_t*)ptr) = st->submodeID;
1143
0
      break;
1144
4.03k
   case SPEEX_SET_ENH:
1145
4.03k
      st->lpc_enh_enabled = *((spx_int32_t*)ptr);
1146
4.03k
      break;
1147
0
   case SPEEX_GET_ENH:
1148
0
      *((spx_int32_t*)ptr) = st->lpc_enh_enabled;
1149
0
      break;
1150
9.96k
   case SPEEX_GET_FRAME_SIZE:
1151
9.96k
      (*(spx_int32_t*)ptr) = NB_FRAME_SIZE;
1152
9.96k
      break;
1153
0
   case SPEEX_GET_BITRATE:
1154
0
      if (st->submodes[st->submodeID])
1155
0
         (*(spx_int32_t*)ptr) = st->sampling_rate*SUBMODE(bits_per_frame)/NB_FRAME_SIZE;
1156
0
      else
1157
0
         (*(spx_int32_t*)ptr) = st->sampling_rate*(NB_SUBMODE_BITS+1)/NB_FRAME_SIZE;
1158
0
      break;
1159
4.03k
   case SPEEX_SET_SAMPLING_RATE:
1160
4.03k
      st->sampling_rate = (*(spx_int32_t*)ptr);
1161
4.03k
      break;
1162
2.88k
   case SPEEX_GET_SAMPLING_RATE:
1163
2.88k
      (*(spx_int32_t*)ptr)=st->sampling_rate;
1164
2.88k
      break;
1165
2.02k
   case SPEEX_SET_HANDLER:
1166
2.02k
      {
1167
2.02k
         SpeexCallback *c = (SpeexCallback*)ptr;
1168
2.02k
         st->speex_callbacks[c->callback_id].func=c->func;
1169
2.02k
         st->speex_callbacks[c->callback_id].data=c->data;
1170
2.02k
         st->speex_callbacks[c->callback_id].callback_id=c->callback_id;
1171
2.02k
      }
1172
2.02k
      break;
1173
0
   case SPEEX_SET_USER_HANDLER:
1174
0
      {
1175
0
         SpeexCallback *c = (SpeexCallback*)ptr;
1176
0
         st->user_callback.func=c->func;
1177
0
         st->user_callback.data=c->data;
1178
0
         st->user_callback.callback_id=c->callback_id;
1179
0
      }
1180
0
      break;
1181
0
   case SPEEX_RESET_STATE:
1182
0
      {
1183
0
         int i;
1184
0
         for (i=0;i<NB_ORDER;i++)
1185
0
            st->mem_sp[i]=0;
1186
0
         for (i=0;i<NB_FRAME_SIZE + NB_PITCH_END + 1;i++)
1187
0
            st->excBuf[i]=0;
1188
0
      }
1189
0
      break;
1190
0
   case SPEEX_SET_SUBMODE_ENCODING:
1191
0
      st->encode_submode = (*(spx_int32_t*)ptr);
1192
0
      break;
1193
0
   case SPEEX_GET_SUBMODE_ENCODING:
1194
0
      (*(spx_int32_t*)ptr) = st->encode_submode;
1195
0
      break;
1196
3.88k
   case SPEEX_GET_LOOKAHEAD:
1197
3.88k
      (*(spx_int32_t*)ptr)=NB_SUBFRAME_SIZE;
1198
3.88k
      break;
1199
0
   case SPEEX_SET_HIGHPASS:
1200
0
      st->highpass_enabled = (*(spx_int32_t*)ptr);
1201
0
      break;
1202
0
   case SPEEX_GET_HIGHPASS:
1203
0
      (*(spx_int32_t*)ptr) = st->highpass_enabled;
1204
0
      break;
1205
      /* FIXME: Convert to fixed-point and re-enable even when float API is disabled */
1206
0
#ifndef DISABLE_FLOAT_API
1207
0
   case SPEEX_GET_ACTIVITY:
1208
0
   {
1209
0
      float ret;
1210
0
      ret = log(st->level/st->min_level)/log(st->max_level/st->min_level);
1211
0
      if (ret>1)
1212
0
         ret = 1;
1213
      /* Done in a strange way to catch NaNs as well */
1214
0
      if (!(ret > 0))
1215
0
         ret = 0;
1216
      /*printf ("%f %f %f %f\n", st->level, st->min_level, st->max_level, ret);*/
1217
0
      (*(spx_int32_t*)ptr) = (int)(100*ret);
1218
0
   }
1219
0
   break;
1220
0
#endif
1221
4.38k
   case SPEEX_GET_PI_GAIN:
1222
4.38k
      {
1223
4.38k
         int i;
1224
4.38k
         spx_word32_t *g = (spx_word32_t*)ptr;
1225
21.9k
         for (i=0;i<NB_NB_SUBFRAMES;i++)
1226
17.5k
            g[i]=st->pi_gain[i];
1227
4.38k
      }
1228
4.38k
      break;
1229
4.38k
   case SPEEX_GET_EXC:
1230
4.38k
      {
1231
4.38k
         int i;
1232
21.9k
         for (i=0;i<NB_NB_SUBFRAMES;i++)
1233
17.5k
            ((spx_word16_t*)ptr)[i] = compute_rms16(st->exc+i*NB_SUBFRAME_SIZE, NB_SUBFRAME_SIZE);
1234
4.38k
      }
1235
4.38k
      break;
1236
80.9k
   case SPEEX_GET_DTX_STATUS:
1237
80.9k
      *((spx_int32_t*)ptr) = st->dtx_enabled;
1238
80.9k
      break;
1239
48.7k
   case SPEEX_SET_INNOVATION_SAVE:
1240
48.7k
      st->innov_save = (spx_word16_t*)ptr;
1241
48.7k
      break;
1242
4.79k
   case SPEEX_SET_WIDEBAND:
1243
4.79k
      st->isWideband = *((spx_int32_t*)ptr);
1244
4.79k
      break;
1245
0
   case SPEEX_GET_STACK:
1246
0
      *((char**)ptr) = st->stack;
1247
0
      break;
1248
0
   default:
1249
0
      speex_warning_int("Unknown nb_ctl request: ", request);
1250
0
      return -1;
1251
170k
   }
1252
170k
   return 0;
1253
170k
}
1254
1255
1256
0
#define median3(a, b, c)  ((a) < (b) ? ((b) < (c) ? (b) : ((a) < (c) ? (c) : (a))) : ((c) < (b) ? (b) : ((c) < (a) ? (c) : (a))))
1257
1258
#ifdef FIXED_POINT
1259
const spx_word16_t attenuation[10] = {32767, 31483, 27923, 22861, 17278, 12055, 7764, 4616, 2533, 1283};
1260
#else
1261
const spx_word16_t attenuation[10] = {1., 0.961, 0.852, 0.698, 0.527, 0.368, 0.237, 0.141, 0.077, 0.039};
1262
1263
#endif
1264
1265
static void nb_decode_lost(DecState *st, spx_word16_t *out, char *stack)
1266
0
{
1267
0
   int i;
1268
0
   int pitch_val;
1269
0
   spx_word16_t pitch_gain;
1270
0
   spx_word16_t fact;
1271
0
   spx_word16_t gain_med;
1272
0
   spx_word16_t innov_gain;
1273
0
   spx_word16_t noise_gain;
1274
1275
0
   st->exc = st->excBuf + 2*NB_PITCH_END + NB_SUBFRAME_SIZE + 6;
1276
1277
0
   if (st->count_lost<10)
1278
0
      fact = attenuation[st->count_lost];
1279
0
   else
1280
0
      fact = 0;
1281
1282
0
   gain_med = median3(st->pitch_gain_buf[0], st->pitch_gain_buf[1], st->pitch_gain_buf[2]);
1283
0
   if (gain_med < st->last_pitch_gain)
1284
0
      st->last_pitch_gain = gain_med;
1285
1286
#ifdef FIXED_POINT
1287
   pitch_gain = st->last_pitch_gain;
1288
0
   if (pitch_gain>54)
1289
0
      pitch_gain = 54;
1290
0
   pitch_gain = SHL16(pitch_gain, 9);
1291
#else
1292
0
   pitch_gain = GAIN_SCALING_1*st->last_pitch_gain;
1293
0
   if (pitch_gain>.85)
1294
0
      pitch_gain=.85;
1295
#endif
1296
0
   pitch_gain = MULT16_16_Q15(fact,pitch_gain) + VERY_SMALL;
1297
   /* FIXME: This was rms of innovation (not exc) */
1298
0
   innov_gain = compute_rms16(st->exc, NB_FRAME_SIZE);
1299
0
   noise_gain = MULT16_16_Q15(innov_gain, MULT16_16_Q15(fact, SUB16(Q15ONE,MULT16_16_Q15(pitch_gain,pitch_gain))));
1300
   /* Shift all buffers by one frame */
1301
0
   SPEEX_MOVE(st->excBuf, st->excBuf+NB_FRAME_SIZE, 2*NB_PITCH_END + NB_SUBFRAME_SIZE + 12);
1302
1303
1304
0
   pitch_val = st->last_pitch + SHR32((spx_int32_t)speex_rand(1+st->count_lost, &st->seed),SIG_SHIFT);
1305
0
   if (pitch_val > NB_PITCH_END)
1306
0
      pitch_val = NB_PITCH_END;
1307
0
   if (pitch_val < NB_PITCH_START)
1308
0
      pitch_val = NB_PITCH_START;
1309
0
   for (i=0;i<NB_FRAME_SIZE;i++)
1310
0
   {
1311
0
      st->exc[i]= MULT16_16_Q15(pitch_gain, (st->exc[i-pitch_val]+VERY_SMALL)) +
1312
0
            speex_rand(noise_gain, &st->seed);
1313
0
   }
1314
1315
0
   bw_lpc(QCONST16(.98,15), st->interp_qlpc, st->interp_qlpc, NB_ORDER);
1316
0
   iir_mem16(&st->exc[-NB_SUBFRAME_SIZE], st->interp_qlpc, out, NB_FRAME_SIZE,
1317
0
             NB_ORDER, st->mem_sp, stack);
1318
0
   highpass(out, out, NB_FRAME_SIZE, HIGHPASS_NARROWBAND|HIGHPASS_OUTPUT, st->mem_hp);
1319
1320
0
   st->first = 0;
1321
0
   st->count_lost++;
1322
0
   st->pitch_gain_buf[st->pitch_gain_buf_idx++] = PSHR16(pitch_gain,9);
1323
0
   if (st->pitch_gain_buf_idx > 2) /* rollover */
1324
0
      st->pitch_gain_buf_idx = 0;
1325
0
}
Unexecuted instantiation: nb_celp.c:nb_decode_lost
Unexecuted instantiation: nb_celp.c:nb_decode_lost
1326
1327
/* Just so we don't need to carry the complete wideband mode information */
1328
static const int wb_skip_table[8] = {0, 36, 112, 192, 352, 0, 0, 0};
1329
1330
int nb_decode(void *state, SpeexBits *bits, void *vout)
1331
65.0k
{
1332
65.0k
   DecState *st;
1333
65.0k
   int i, sub;
1334
65.0k
   int pitch;
1335
65.0k
   spx_word16_t pitch_gain[3];
1336
65.0k
   spx_word32_t ol_gain=0;
1337
65.0k
   int ol_pitch=0;
1338
65.0k
   spx_word16_t ol_pitch_coef=0;
1339
65.0k
   int best_pitch=40;
1340
65.0k
   spx_word16_t best_pitch_gain=0;
1341
65.0k
   int wideband;
1342
65.0k
   int m;
1343
65.0k
   char *stack;
1344
65.0k
   VARDECL(spx_sig_t *innov);
1345
65.0k
   VARDECL(spx_word32_t *exc32);
1346
65.0k
   VARDECL(spx_coef_t *ak);
1347
65.0k
   VARDECL(spx_lsp_t *qlsp);
1348
65.0k
   spx_word16_t pitch_average=0;
1349
1350
65.0k
   spx_word16_t *out = (spx_word16_t*)vout;
1351
65.0k
   VARDECL(spx_lsp_t *interp_qlsp);
1352
1353
65.0k
   st=(DecState*)state;
1354
65.0k
   stack=st->stack;
1355
1356
65.0k
   st->exc = st->excBuf + 2*NB_PITCH_END + NB_SUBFRAME_SIZE + 6;
1357
1358
   /* Check if we're in DTX mode*/
1359
65.0k
   if (!bits && st->dtx_enabled)
1360
0
   {
1361
0
      st->submodeID=0;
1362
0
   } else
1363
65.0k
   {
1364
      /* If bits is NULL, consider the packet to be lost (what could we do anyway) */
1365
65.0k
      if (!bits)
1366
0
      {
1367
0
         nb_decode_lost(st, out, stack);
1368
0
         return 0;
1369
0
      }
1370
1371
65.0k
      if (st->encode_submode)
1372
65.0k
      {
1373
1374
      /* Search for next narrowband block (handle requests, skip wideband blocks) */
1375
69.5k
      do {
1376
69.5k
         if (speex_bits_remaining(bits)<5)
1377
12.6k
            return -1;
1378
56.9k
         wideband = speex_bits_unpack_unsigned(bits, 1);
1379
56.9k
         if (wideband) /* Skip wideband block (for compatibility) */
1380
3.45k
         {
1381
3.45k
            int submode;
1382
3.45k
            int advance;
1383
3.45k
            advance = submode = speex_bits_unpack_unsigned(bits, SB_SUBMODE_BITS);
1384
            /*speex_mode_query(&speex_wb_mode, SPEEX_SUBMODE_BITS_PER_FRAME, &advance);*/
1385
3.45k
            advance = wb_skip_table[submode];
1386
3.45k
            if (advance < 0)
1387
0
            {
1388
0
               speex_notify("Invalid mode encountered. The stream is corrupted.");
1389
0
               return -2;
1390
0
            }
1391
3.45k
            advance -= (SB_SUBMODE_BITS+1);
1392
3.45k
            speex_bits_advance(bits, advance);
1393
1394
3.45k
            if (speex_bits_remaining(bits)<5)
1395
863
               return -1;
1396
2.58k
            wideband = speex_bits_unpack_unsigned(bits, 1);
1397
2.58k
            if (wideband)
1398
2.02k
            {
1399
2.02k
               advance = submode = speex_bits_unpack_unsigned(bits, SB_SUBMODE_BITS);
1400
               /*speex_mode_query(&speex_wb_mode, SPEEX_SUBMODE_BITS_PER_FRAME, &advance);*/
1401
2.02k
               advance = wb_skip_table[submode];
1402
2.02k
               if (advance < 0)
1403
0
               {
1404
0
                  speex_notify("Invalid mode encountered. The stream is corrupted.");
1405
0
                  return -2;
1406
0
               }
1407
2.02k
               advance -= (SB_SUBMODE_BITS+1);
1408
2.02k
               speex_bits_advance(bits, advance);
1409
2.02k
               wideband = speex_bits_unpack_unsigned(bits, 1);
1410
2.02k
               if (wideband)
1411
1.56k
               {
1412
1.56k
                  speex_notify("More than two wideband layers found. The stream is corrupted.");
1413
1.56k
                  return -2;
1414
1.56k
               }
1415
1416
2.02k
            }
1417
2.58k
         }
1418
54.4k
         if (speex_bits_remaining(bits)<4)
1419
455
            return -1;
1420
         /* FIXME: Check for overflow */
1421
54.0k
         m = speex_bits_unpack_unsigned(bits, 4);
1422
54.0k
         if (m==15) /* We found a terminator */
1423
722
         {
1424
722
            return -1;
1425
53.3k
         } else if (m==14) /* Speex in-band request */
1426
3.92k
         {
1427
3.92k
            int ret = speex_inband_handler(bits, st->speex_callbacks, state);
1428
3.92k
            if (ret)
1429
0
               return ret;
1430
49.3k
         } else if (m==13) /* User in-band request */
1431
587
         {
1432
587
            int ret = st->user_callback.func(bits, state, st->user_callback.data);
1433
587
            if (ret)
1434
0
               return ret;
1435
48.7k
         } else if (m>8) /* Invalid mode */
1436
3.15k
         {
1437
3.15k
            speex_notify("Invalid mode encountered. The stream is corrupted.");
1438
3.15k
            return -2;
1439
3.15k
         }
1440
1441
54.0k
      } while (m>8);
1442
1443
      /* Get the sub-mode that was used */
1444
45.6k
      st->submodeID = m;
1445
45.6k
      }
1446
1447
65.0k
   }
1448
1449
   /* Shift all buffers by one frame */
1450
45.6k
   SPEEX_MOVE(st->excBuf, st->excBuf+NB_FRAME_SIZE, 2*NB_PITCH_END + NB_SUBFRAME_SIZE + 12);
1451
1452
   /* If null mode (no transmission), just set a couple things to zero*/
1453
45.6k
   if (st->submodes[st->submodeID] == NULL)
1454
20.9k
   {
1455
20.9k
      VARDECL(spx_coef_t *lpc);
1456
20.9k
      ALLOC(lpc, NB_ORDER, spx_coef_t);
1457
20.9k
      bw_lpc(QCONST16(0.93f,15), st->interp_qlpc, lpc, NB_ORDER);
1458
20.9k
      {
1459
20.9k
         spx_word16_t innov_gain=0;
1460
         /* FIXME: This was innov, not exc */
1461
20.9k
         innov_gain = compute_rms16(st->exc, NB_FRAME_SIZE);
1462
3.37M
         for (i=0;i<NB_FRAME_SIZE;i++)
1463
3.34M
            st->exc[i]=speex_rand(innov_gain, &st->seed);
1464
20.9k
      }
1465
1466
1467
20.9k
      st->first=1;
1468
1469
      /* Final signal synthesis from excitation */
1470
20.9k
      iir_mem16(st->exc, lpc, out, NB_FRAME_SIZE, NB_ORDER, st->mem_sp, stack);
1471
1472
      /* Normally this is written to later but since this is returning early,
1473
         avoid reading uninitialized memory in caller */
1474
20.9k
      if (st->innov_save)
1475
16.8k
         SPEEX_MEMSET(st->innov_save, 0, NB_NB_SUBFRAMES*NB_SUBFRAME_SIZE);
1476
1477
20.9k
      st->count_lost=0;
1478
20.9k
      return 0;
1479
20.9k
   }
1480
1481
24.7k
   ALLOC(qlsp, NB_ORDER, spx_lsp_t);
1482
1483
   /* Unquantize LSPs */
1484
24.7k
   SUBMODE(lsp_unquant)(qlsp, NB_ORDER, bits);
1485
1486
   /*Damp memory if a frame was lost and the LSP changed too much*/
1487
24.7k
   if (st->count_lost)
1488
0
   {
1489
0
      spx_word16_t fact;
1490
0
      spx_word32_t lsp_dist=0;
1491
0
      for (i=0;i<NB_ORDER;i++)
1492
0
         lsp_dist = ADD32(lsp_dist, EXTEND32(ABS(st->old_qlsp[i] - qlsp[i])));
1493
#ifdef FIXED_POINT
1494
0
      fact = SHR16(19661,SHR32(lsp_dist,LSP_SHIFT+2));
1495
#else
1496
      fact = .6*exp(-.2*lsp_dist);
1497
#endif
1498
0
      for (i=0;i<NB_ORDER;i++)
1499
0
         st->mem_sp[i] = MULT16_32_Q15(fact,st->mem_sp[i]);
1500
0
   }
1501
1502
1503
   /* Handle first frame and lost-packet case */
1504
24.7k
   if (st->first || st->count_lost)
1505
4.78k
   {
1506
52.6k
      for (i=0;i<NB_ORDER;i++)
1507
47.8k
         st->old_qlsp[i] = qlsp[i];
1508
4.78k
   }
1509
1510
   /* Get open-loop pitch estimation for low bit-rate pitch coding */
1511
24.7k
   if (SUBMODE(lbr_pitch)!=-1)
1512
11.2k
   {
1513
11.2k
      ol_pitch = NB_PITCH_START+speex_bits_unpack_unsigned(bits, 7);
1514
11.2k
   }
1515
1516
24.7k
   if (SUBMODE(forced_pitch_gain))
1517
8.86k
   {
1518
8.86k
      int quant;
1519
8.86k
      quant = speex_bits_unpack_unsigned(bits, 4);
1520
8.86k
      ol_pitch_coef=MULT16_16_P15(QCONST16(0.066667,15),SHL16(quant,GAIN_SHIFT));
1521
8.86k
   }
1522
1523
   /* Get global excitation gain */
1524
24.7k
   {
1525
24.7k
      int qe;
1526
24.7k
      qe = speex_bits_unpack_unsigned(bits, 5);
1527
#ifdef FIXED_POINT
1528
      /* FIXME: Perhaps we could slightly lower the gain here when the output is going to saturate? */
1529
13.4k
      ol_gain = MULT16_32_Q15(28406,ol_gain_table[qe]);
1530
#else
1531
11.3k
      ol_gain = SIG_SCALING*exp(qe/3.5);
1532
#endif
1533
24.7k
   }
1534
1535
24.7k
   ALLOC(ak, NB_ORDER, spx_coef_t);
1536
24.7k
   ALLOC(innov, NB_SUBFRAME_SIZE, spx_sig_t);
1537
24.7k
   ALLOC(exc32, NB_SUBFRAME_SIZE, spx_word32_t);
1538
1539
24.7k
   if (st->submodeID==1)
1540
4.38k
   {
1541
4.38k
      int extra;
1542
4.38k
      extra = speex_bits_unpack_unsigned(bits, 4);
1543
1544
4.38k
      if (extra==15)
1545
707
         st->dtx_enabled=1;
1546
3.67k
      else
1547
3.67k
         st->dtx_enabled=0;
1548
4.38k
   }
1549
24.7k
   if (st->submodeID>1)
1550
20.3k
      st->dtx_enabled=0;
1551
1552
   /*Loop on subframes */
1553
123k
   for (sub=0;sub<NB_NB_SUBFRAMES;sub++)
1554
98.8k
   {
1555
98.8k
      int offset;
1556
98.8k
      spx_word16_t *exc;
1557
98.8k
      spx_word16_t *innov_save = NULL;
1558
98.8k
      spx_word16_t tmp;
1559
1560
      /* Offset relative to start of frame */
1561
98.8k
      offset = NB_SUBFRAME_SIZE*sub;
1562
      /* Excitation */
1563
98.8k
      exc=st->exc+offset;
1564
      /* Original signal */
1565
98.8k
      if (st->innov_save)
1566
68.4k
         innov_save = st->innov_save+offset;
1567
1568
1569
      /* Reset excitation */
1570
98.8k
      SPEEX_MEMSET(exc, 0, NB_SUBFRAME_SIZE);
1571
1572
      /*Adaptive codebook contribution*/
1573
98.8k
      speex_assert (SUBMODE(ltp_unquant));
1574
98.8k
      {
1575
98.8k
         int pit_min, pit_max;
1576
         /* Handle pitch constraints if any */
1577
98.8k
         if (SUBMODE(lbr_pitch) != -1)
1578
45.1k
         {
1579
45.1k
            int margin;
1580
45.1k
            margin = SUBMODE(lbr_pitch);
1581
45.1k
            if (margin)
1582
0
            {
1583
/* GT - need optimization?
1584
               if (ol_pitch < NB_PITCH_START+margin-1)
1585
                  ol_pitch=NB_PITCH_START+margin-1;
1586
               if (ol_pitch > NB_PITCH_END-margin)
1587
                  ol_pitch=NB_PITCH_END-margin;
1588
               pit_min = ol_pitch-margin+1;
1589
               pit_max = ol_pitch+margin;
1590
*/
1591
0
               pit_min = ol_pitch-margin+1;
1592
0
               if (pit_min < NB_PITCH_START)
1593
0
      pit_min = NB_PITCH_START;
1594
0
               pit_max = ol_pitch+margin;
1595
0
               if (pit_max > NB_PITCH_END)
1596
0
      pit_max = NB_PITCH_END;
1597
45.1k
            } else {
1598
45.1k
               pit_min = pit_max = ol_pitch;
1599
45.1k
            }
1600
53.7k
         } else {
1601
53.7k
            pit_min = NB_PITCH_START;
1602
53.7k
            pit_max = NB_PITCH_END;
1603
53.7k
         }
1604
1605
1606
1607
98.8k
         SUBMODE(ltp_unquant)(exc, exc32, pit_min, pit_max, ol_pitch_coef, SUBMODE(ltp_params),
1608
98.8k
                 NB_SUBFRAME_SIZE, &pitch, &pitch_gain[0], bits, stack,
1609
98.8k
                 st->count_lost, offset, st->last_pitch_gain, 0);
1610
1611
         /* Ensuring that things aren't blowing up as would happen if e.g. an encoder is
1612
         crafting packets to make us produce NaNs and slow down the decoder (vague DoS threat).
1613
         We can probably be even more aggressive and limit to 15000 or so. */
1614
98.8k
         sanitize_values32(exc32, NEG32(QCONST32(32000,SIG_SHIFT-1)), QCONST32(32000,SIG_SHIFT-1), NB_SUBFRAME_SIZE);
1615
1616
98.8k
         tmp = gain_3tap_to_1tap(pitch_gain);
1617
1618
98.8k
         pitch_average += tmp;
1619
98.8k
         if ((tmp>best_pitch_gain&&ABS(2*best_pitch-pitch)>=3&&ABS(3*best_pitch-pitch)>=4&&ABS(4*best_pitch-pitch)>=5)
1620
89.1k
              || (tmp>MULT16_16_Q15(QCONST16(.6,15),best_pitch_gain)&&(ABS(best_pitch-2*pitch)<3||ABS(best_pitch-3*pitch)<4||ABS(best_pitch-4*pitch)<5))
1621
87.7k
              || (MULT16_16_Q15(QCONST16(.67,15),tmp)>best_pitch_gain&&(ABS(2*best_pitch-pitch)<3||ABS(3*best_pitch-pitch)<4||ABS(4*best_pitch-pitch)<5)) )
1622
14.8k
         {
1623
14.8k
            best_pitch = pitch;
1624
14.8k
            if (tmp > best_pitch_gain)
1625
13.4k
               best_pitch_gain = tmp;
1626
14.8k
         }
1627
98.8k
      }
1628
1629
      /* Unquantize the innovation */
1630
98.8k
      {
1631
98.8k
         int q_energy;
1632
98.8k
         spx_word32_t ener;
1633
1634
98.8k
         SPEEX_MEMSET(innov, 0, NB_SUBFRAME_SIZE);
1635
1636
         /* Decode sub-frame gain correction */
1637
98.8k
         if (SUBMODE(have_subframe_gain)==3)
1638
19.8k
         {
1639
19.8k
            q_energy = speex_bits_unpack_unsigned(bits, 3);
1640
19.8k
            ener = MULT16_32_Q14(exc_gain_quant_scal3[q_energy],ol_gain);
1641
78.9k
         } else if (SUBMODE(have_subframe_gain)==1)
1642
33.8k
         {
1643
33.8k
            q_energy = speex_bits_unpack_unsigned(bits, 1);
1644
33.8k
            ener = MULT16_32_Q14(exc_gain_quant_scal1[q_energy],ol_gain);
1645
45.1k
         } else {
1646
45.1k
            ener = ol_gain;
1647
45.1k
         }
1648
1649
98.8k
         speex_assert (SUBMODE(innovation_unquant));
1650
98.8k
         {
1651
            /*Fixed codebook contribution*/
1652
98.8k
            SUBMODE(innovation_unquant)(innov, SUBMODE(innovation_params), NB_SUBFRAME_SIZE, bits, stack, &st->seed);
1653
            /* De-normalize innovation and update excitation */
1654
1655
98.8k
            signal_mul(innov, innov, ener, NB_SUBFRAME_SIZE);
1656
1657
            /* Decode second codebook (only for some modes) */
1658
98.8k
            if (SUBMODE(double_codebook))
1659
6.61k
            {
1660
6.61k
               char *tmp_stack=stack;
1661
6.61k
               VARDECL(spx_sig_t *innov2);
1662
6.61k
               ALLOC(innov2, NB_SUBFRAME_SIZE, spx_sig_t);
1663
6.61k
               SPEEX_MEMSET(innov2, 0, NB_SUBFRAME_SIZE);
1664
6.61k
               SUBMODE(innovation_unquant)(innov2, SUBMODE(innovation_params), NB_SUBFRAME_SIZE, bits, stack, &st->seed);
1665
6.61k
               signal_mul(innov2, innov2, MULT16_32_Q15(QCONST16(0.454545f,15),ener), NB_SUBFRAME_SIZE);
1666
271k
               for (i=0;i<NB_SUBFRAME_SIZE;i++)
1667
264k
                  innov[i] = ADD32(innov[i], innov2[i]);
1668
6.61k
               stack = tmp_stack;
1669
6.61k
            }
1670
4.05M
            for (i=0;i<NB_SUBFRAME_SIZE;i++)
1671
3.95M
               exc[i]=EXTRACT16(SATURATE32(PSHR32(ADD32(SHL32(exc32[i],1),innov[i]),SIG_SHIFT),32767));
1672
            /*print_vec(exc, 40, "innov");*/
1673
98.8k
            if (innov_save)
1674
68.4k
            {
1675
2.80M
               for (i=0;i<NB_SUBFRAME_SIZE;i++)
1676
2.73M
                  innov_save[i] = EXTRACT16(PSHR32(innov[i], SIG_SHIFT));
1677
68.4k
            }
1678
98.8k
         }
1679
1680
         /*Vocoder mode*/
1681
98.8k
         if (st->submodeID==1)
1682
17.5k
         {
1683
17.5k
            spx_word16_t g=ol_pitch_coef;
1684
17.5k
            g=MULT16_16_P14(QCONST16(1.5f,14),(g-QCONST16(.2f,6)));
1685
17.5k
            if (g<0)
1686
11.7k
               g=0;
1687
17.5k
            if (g>GAIN_SCALING)
1688
1.76k
               g=GAIN_SCALING;
1689
1690
17.5k
            SPEEX_MEMSET(exc, 0, NB_SUBFRAME_SIZE);
1691
45.7k
            while (st->voc_offset<NB_SUBFRAME_SIZE)
1692
28.1k
            {
1693
               /* exc[st->voc_offset]= g*sqrt(2*ol_pitch)*ol_gain;
1694
                  Not quite sure why we need the factor of two in the sqrt */
1695
28.1k
               if (st->voc_offset>=0)
1696
28.1k
                  exc[st->voc_offset]=MULT16_16(spx_sqrt(MULT16_16_16(2,ol_pitch)),EXTRACT16(PSHR32(MULT16_16(g,PSHR32(ol_gain,SIG_SHIFT)),6)));
1697
28.1k
               st->voc_offset+=ol_pitch;
1698
28.1k
            }
1699
17.5k
            st->voc_offset -= NB_SUBFRAME_SIZE;
1700
1701
718k
            for (i=0;i<NB_SUBFRAME_SIZE;i++)
1702
701k
            {
1703
701k
               spx_word16_t exci=exc[i];
1704
701k
               exc[i]= ADD16(ADD16(MULT16_16_Q15(QCONST16(.7f,15),exc[i]) , MULT16_16_Q15(QCONST16(.3f,15),st->voc_m1)),
1705
701k
                             SUB16(MULT16_16_Q15(Q15_ONE-MULT16_16_16(QCONST16(.85f,9),g),EXTRACT16(PSHR32(innov[i],SIG_SHIFT))),
1706
701k
                                   MULT16_16_Q15(MULT16_16_16(QCONST16(.15f,9),g),EXTRACT16(PSHR32(st->voc_m2,SIG_SHIFT)))
1707
701k
                                  ));
1708
701k
               st->voc_m1 = exci;
1709
701k
               st->voc_m2=innov[i];
1710
701k
               st->voc_mean = EXTRACT16(PSHR32(ADD32(MULT16_16(QCONST16(.8f,15),st->voc_mean), MULT16_16(QCONST16(.2f,15),exc[i])), 15));
1711
701k
               exc[i]-=st->voc_mean;
1712
701k
            }
1713
17.5k
         }
1714
1715
98.8k
      }
1716
98.8k
   }
1717
1718
24.7k
   ALLOC(interp_qlsp, NB_ORDER, spx_lsp_t);
1719
1720
24.7k
   if (st->lpc_enh_enabled && SUBMODE(comb_gain)>0 && !st->count_lost)
1721
20.3k
   {
1722
20.3k
      multicomb(st->exc-NB_SUBFRAME_SIZE, out, st->interp_qlpc, NB_ORDER, 2*NB_SUBFRAME_SIZE, best_pitch, 40, SUBMODE(comb_gain), stack);
1723
20.3k
      multicomb(st->exc+NB_SUBFRAME_SIZE, out+2*NB_SUBFRAME_SIZE, st->interp_qlpc, NB_ORDER, 2*NB_SUBFRAME_SIZE, best_pitch, 40, SUBMODE(comb_gain), stack);
1724
20.3k
   } else {
1725
4.38k
      SPEEX_COPY(out, &st->exc[-NB_SUBFRAME_SIZE], NB_FRAME_SIZE);
1726
4.38k
   }
1727
1728
   /* If the last packet was lost, re-scale the excitation to obtain the same energy as encoded in ol_gain */
1729
24.7k
   if (st->count_lost)
1730
0
   {
1731
0
      spx_word16_t exc_ener;
1732
0
      spx_word32_t gain32;
1733
0
      spx_word16_t gain;
1734
0
      exc_ener = compute_rms16 (st->exc, NB_FRAME_SIZE);
1735
0
      gain32 = PDIV32(ol_gain, ADD16(exc_ener,1));
1736
#ifdef FIXED_POINT
1737
0
      if (gain32 > 32767)
1738
0
         gain32 = 32767;
1739
0
      gain = EXTRACT16(gain32);
1740
#else
1741
0
      if (gain32 > 2)
1742
0
         gain32=2;
1743
      gain = gain32;
1744
#endif
1745
0
      for (i=0;i<NB_FRAME_SIZE;i++)
1746
0
      {
1747
0
         st->exc[i] = MULT16_16_Q14(gain, st->exc[i]);
1748
0
         out[i]=st->exc[i-NB_SUBFRAME_SIZE];
1749
0
      }
1750
0
   }
1751
1752
   /*Loop on subframes */
1753
123k
   for (sub=0;sub<NB_NB_SUBFRAMES;sub++)
1754
98.8k
   {
1755
98.8k
      int offset;
1756
98.8k
      spx_word16_t *sp;
1757
1758
      /* Offset relative to start of frame */
1759
98.8k
      offset = NB_SUBFRAME_SIZE*sub;
1760
      /* Original signal */
1761
98.8k
      sp=out+offset;
1762
1763
      /* LSP interpolation (quantized and unquantized) */
1764
98.8k
      lsp_interpolate(st->old_qlsp, qlsp, interp_qlsp, NB_ORDER, sub, NB_NB_SUBFRAMES, LSP_MARGIN);
1765
1766
      /* Compute interpolated LPCs (unquantized) */
1767
98.8k
      lsp_to_lpc(interp_qlsp, ak, NB_ORDER, stack);
1768
1769
      /* Compute analysis filter at w=pi */
1770
98.8k
      {
1771
98.8k
         spx_word32_t pi_g=LPC_SCALING;
1772
592k
         for (i=0;i<NB_ORDER;i+=2)
1773
494k
         {
1774
            /*pi_g += -st->interp_qlpc[i] +  st->interp_qlpc[i+1];*/
1775
494k
            pi_g = ADD32(pi_g, SUB32(EXTEND32(ak[i+1]),EXTEND32(ak[i])));
1776
494k
         }
1777
98.8k
         st->pi_gain[sub] = pi_g;
1778
98.8k
      }
1779
1780
98.8k
      iir_mem16(sp, st->interp_qlpc, sp, NB_SUBFRAME_SIZE, NB_ORDER,
1781
98.8k
                st->mem_sp, stack);
1782
1783
1.08M
      for (i=0;i<NB_ORDER;i++)
1784
988k
         st->interp_qlpc[i] = ak[i];
1785
1786
98.8k
   }
1787
1788
24.7k
   if (st->highpass_enabled)
1789
24.7k
      highpass(out, out, NB_FRAME_SIZE, (st->isWideband?HIGHPASS_WIDEBAND:HIGHPASS_NARROWBAND)|HIGHPASS_OUTPUT, st->mem_hp);
1790
   /*for (i=0;i<NB_FRAME_SIZE;i++)
1791
     printf ("%d\n", (int)st->frame[i]);*/
1792
1793
   /* Tracking output level */
1794
24.7k
   st->level = 1+PSHR32(ol_gain,SIG_SHIFT);
1795
24.7k
   st->max_level = MAX16(MULT16_16_Q15(QCONST16(.99f,15), st->max_level), st->level);
1796
24.7k
   st->min_level = MIN16(ADD16(1,MULT16_16_Q14(QCONST16(1.01f,14), st->min_level)), st->level);
1797
24.7k
   if (st->max_level < st->min_level+1)
1798
7.35k
      st->max_level = st->min_level+1;
1799
   /*printf ("%f %f %f %d\n", og, st->min_level, st->max_level, update);*/
1800
1801
   /* Store the LSPs for interpolation in the next frame */
1802
271k
   for (i=0;i<NB_ORDER;i++)
1803
247k
      st->old_qlsp[i] = qlsp[i];
1804
1805
   /* The next frame will not be the first (Duh!) */
1806
24.7k
   st->first = 0;
1807
24.7k
   st->count_lost=0;
1808
24.7k
   st->last_pitch = best_pitch;
1809
#ifdef FIXED_POINT
1810
13.4k
   st->last_pitch_gain = PSHR16(pitch_average,2);
1811
#else
1812
   st->last_pitch_gain = .25*pitch_average;
1813
#endif
1814
24.7k
   st->pitch_gain_buf[st->pitch_gain_buf_idx++] = st->last_pitch_gain;
1815
24.7k
   if (st->pitch_gain_buf_idx > 2) /* rollover */
1816
7.36k
      st->pitch_gain_buf_idx = 0;
1817
1818
24.7k
   st->last_ol_gain = ol_gain;
1819
1820
24.7k
   return 0;
1821
45.6k
}
nb_decode
Line
Count
Source
1331
31.1k
{
1332
31.1k
   DecState *st;
1333
31.1k
   int i, sub;
1334
31.1k
   int pitch;
1335
31.1k
   spx_word16_t pitch_gain[3];
1336
31.1k
   spx_word32_t ol_gain=0;
1337
31.1k
   int ol_pitch=0;
1338
31.1k
   spx_word16_t ol_pitch_coef=0;
1339
31.1k
   int best_pitch=40;
1340
31.1k
   spx_word16_t best_pitch_gain=0;
1341
31.1k
   int wideband;
1342
31.1k
   int m;
1343
31.1k
   char *stack;
1344
31.1k
   VARDECL(spx_sig_t *innov);
1345
31.1k
   VARDECL(spx_word32_t *exc32);
1346
31.1k
   VARDECL(spx_coef_t *ak);
1347
31.1k
   VARDECL(spx_lsp_t *qlsp);
1348
31.1k
   spx_word16_t pitch_average=0;
1349
1350
31.1k
   spx_word16_t *out = (spx_word16_t*)vout;
1351
31.1k
   VARDECL(spx_lsp_t *interp_qlsp);
1352
1353
31.1k
   st=(DecState*)state;
1354
31.1k
   stack=st->stack;
1355
1356
31.1k
   st->exc = st->excBuf + 2*NB_PITCH_END + NB_SUBFRAME_SIZE + 6;
1357
1358
   /* Check if we're in DTX mode*/
1359
31.1k
   if (!bits && st->dtx_enabled)
1360
0
   {
1361
0
      st->submodeID=0;
1362
0
   } else
1363
31.1k
   {
1364
      /* If bits is NULL, consider the packet to be lost (what could we do anyway) */
1365
31.1k
      if (!bits)
1366
0
      {
1367
0
         nb_decode_lost(st, out, stack);
1368
0
         return 0;
1369
0
      }
1370
1371
31.1k
      if (st->encode_submode)
1372
31.1k
      {
1373
1374
      /* Search for next narrowband block (handle requests, skip wideband blocks) */
1375
33.5k
      do {
1376
33.5k
         if (speex_bits_remaining(bits)<5)
1377
7.55k
            return -1;
1378
25.9k
         wideband = speex_bits_unpack_unsigned(bits, 1);
1379
25.9k
         if (wideband) /* Skip wideband block (for compatibility) */
1380
1.65k
         {
1381
1.65k
            int submode;
1382
1.65k
            int advance;
1383
1.65k
            advance = submode = speex_bits_unpack_unsigned(bits, SB_SUBMODE_BITS);
1384
            /*speex_mode_query(&speex_wb_mode, SPEEX_SUBMODE_BITS_PER_FRAME, &advance);*/
1385
1.65k
            advance = wb_skip_table[submode];
1386
1.65k
            if (advance < 0)
1387
0
            {
1388
0
               speex_notify("Invalid mode encountered. The stream is corrupted.");
1389
0
               return -2;
1390
0
            }
1391
1.65k
            advance -= (SB_SUBMODE_BITS+1);
1392
1.65k
            speex_bits_advance(bits, advance);
1393
1394
1.65k
            if (speex_bits_remaining(bits)<5)
1395
428
               return -1;
1396
1.22k
            wideband = speex_bits_unpack_unsigned(bits, 1);
1397
1.22k
            if (wideband)
1398
969
            {
1399
969
               advance = submode = speex_bits_unpack_unsigned(bits, SB_SUBMODE_BITS);
1400
               /*speex_mode_query(&speex_wb_mode, SPEEX_SUBMODE_BITS_PER_FRAME, &advance);*/
1401
969
               advance = wb_skip_table[submode];
1402
969
               if (advance < 0)
1403
0
               {
1404
0
                  speex_notify("Invalid mode encountered. The stream is corrupted.");
1405
0
                  return -2;
1406
0
               }
1407
969
               advance -= (SB_SUBMODE_BITS+1);
1408
969
               speex_bits_advance(bits, advance);
1409
969
               wideband = speex_bits_unpack_unsigned(bits, 1);
1410
969
               if (wideband)
1411
712
               {
1412
712
                  speex_notify("More than two wideband layers found. The stream is corrupted.");
1413
712
                  return -2;
1414
712
               }
1415
1416
969
            }
1417
1.22k
         }
1418
24.8k
         if (speex_bits_remaining(bits)<4)
1419
251
            return -1;
1420
         /* FIXME: Check for overflow */
1421
24.5k
         m = speex_bits_unpack_unsigned(bits, 4);
1422
24.5k
         if (m==15) /* We found a terminator */
1423
333
         {
1424
333
            return -1;
1425
24.2k
         } else if (m==14) /* Speex in-band request */
1426
2.02k
         {
1427
2.02k
            int ret = speex_inband_handler(bits, st->speex_callbacks, state);
1428
2.02k
            if (ret)
1429
0
               return ret;
1430
22.2k
         } else if (m==13) /* User in-band request */
1431
333
         {
1432
333
            int ret = st->user_callback.func(bits, state, st->user_callback.data);
1433
333
            if (ret)
1434
0
               return ret;
1435
21.9k
         } else if (m>8) /* Invalid mode */
1436
1.48k
         {
1437
1.48k
            speex_notify("Invalid mode encountered. The stream is corrupted.");
1438
1.48k
            return -2;
1439
1.48k
         }
1440
1441
24.5k
      } while (m>8);
1442
1443
      /* Get the sub-mode that was used */
1444
20.4k
      st->submodeID = m;
1445
20.4k
      }
1446
1447
31.1k
   }
1448
1449
   /* Shift all buffers by one frame */
1450
20.4k
   SPEEX_MOVE(st->excBuf, st->excBuf+NB_FRAME_SIZE, 2*NB_PITCH_END + NB_SUBFRAME_SIZE + 12);
1451
1452
   /* If null mode (no transmission), just set a couple things to zero*/
1453
20.4k
   if (st->submodes[st->submodeID] == NULL)
1454
9.11k
   {
1455
9.11k
      VARDECL(spx_coef_t *lpc);
1456
9.11k
      ALLOC(lpc, NB_ORDER, spx_coef_t);
1457
9.11k
      bw_lpc(QCONST16(0.93f,15), st->interp_qlpc, lpc, NB_ORDER);
1458
9.11k
      {
1459
9.11k
         spx_word16_t innov_gain=0;
1460
         /* FIXME: This was innov, not exc */
1461
9.11k
         innov_gain = compute_rms16(st->exc, NB_FRAME_SIZE);
1462
1.46M
         for (i=0;i<NB_FRAME_SIZE;i++)
1463
1.45M
            st->exc[i]=speex_rand(innov_gain, &st->seed);
1464
9.11k
      }
1465
1466
1467
9.11k
      st->first=1;
1468
1469
      /* Final signal synthesis from excitation */
1470
9.11k
      iir_mem16(st->exc, lpc, out, NB_FRAME_SIZE, NB_ORDER, st->mem_sp, stack);
1471
1472
      /* Normally this is written to later but since this is returning early,
1473
         avoid reading uninitialized memory in caller */
1474
9.11k
      if (st->innov_save)
1475
7.61k
         SPEEX_MEMSET(st->innov_save, 0, NB_NB_SUBFRAMES*NB_SUBFRAME_SIZE);
1476
1477
9.11k
      st->count_lost=0;
1478
9.11k
      return 0;
1479
9.11k
   }
1480
1481
11.3k
   ALLOC(qlsp, NB_ORDER, spx_lsp_t);
1482
1483
   /* Unquantize LSPs */
1484
11.3k
   SUBMODE(lsp_unquant)(qlsp, NB_ORDER, bits);
1485
1486
   /*Damp memory if a frame was lost and the LSP changed too much*/
1487
11.3k
   if (st->count_lost)
1488
0
   {
1489
0
      spx_word16_t fact;
1490
0
      spx_word32_t lsp_dist=0;
1491
0
      for (i=0;i<NB_ORDER;i++)
1492
0
         lsp_dist = ADD32(lsp_dist, EXTEND32(ABS(st->old_qlsp[i] - qlsp[i])));
1493
#ifdef FIXED_POINT
1494
      fact = SHR16(19661,SHR32(lsp_dist,LSP_SHIFT+2));
1495
#else
1496
0
      fact = .6*exp(-.2*lsp_dist);
1497
0
#endif
1498
0
      for (i=0;i<NB_ORDER;i++)
1499
0
         st->mem_sp[i] = MULT16_32_Q15(fact,st->mem_sp[i]);
1500
0
   }
1501
1502
1503
   /* Handle first frame and lost-packet case */
1504
11.3k
   if (st->first || st->count_lost)
1505
1.90k
   {
1506
20.9k
      for (i=0;i<NB_ORDER;i++)
1507
19.0k
         st->old_qlsp[i] = qlsp[i];
1508
1.90k
   }
1509
1510
   /* Get open-loop pitch estimation for low bit-rate pitch coding */
1511
11.3k
   if (SUBMODE(lbr_pitch)!=-1)
1512
5.02k
   {
1513
5.02k
      ol_pitch = NB_PITCH_START+speex_bits_unpack_unsigned(bits, 7);
1514
5.02k
   }
1515
1516
11.3k
   if (SUBMODE(forced_pitch_gain))
1517
4.40k
   {
1518
4.40k
      int quant;
1519
4.40k
      quant = speex_bits_unpack_unsigned(bits, 4);
1520
4.40k
      ol_pitch_coef=MULT16_16_P15(QCONST16(0.066667,15),SHL16(quant,GAIN_SHIFT));
1521
4.40k
   }
1522
1523
   /* Get global excitation gain */
1524
11.3k
   {
1525
11.3k
      int qe;
1526
11.3k
      qe = speex_bits_unpack_unsigned(bits, 5);
1527
#ifdef FIXED_POINT
1528
      /* FIXME: Perhaps we could slightly lower the gain here when the output is going to saturate? */
1529
      ol_gain = MULT16_32_Q15(28406,ol_gain_table[qe]);
1530
#else
1531
11.3k
      ol_gain = SIG_SCALING*exp(qe/3.5);
1532
11.3k
#endif
1533
11.3k
   }
1534
1535
11.3k
   ALLOC(ak, NB_ORDER, spx_coef_t);
1536
11.3k
   ALLOC(innov, NB_SUBFRAME_SIZE, spx_sig_t);
1537
11.3k
   ALLOC(exc32, NB_SUBFRAME_SIZE, spx_word32_t);
1538
1539
11.3k
   if (st->submodeID==1)
1540
1.79k
   {
1541
1.79k
      int extra;
1542
1.79k
      extra = speex_bits_unpack_unsigned(bits, 4);
1543
1544
1.79k
      if (extra==15)
1545
395
         st->dtx_enabled=1;
1546
1.39k
      else
1547
1.39k
         st->dtx_enabled=0;
1548
1.79k
   }
1549
11.3k
   if (st->submodeID>1)
1550
9.51k
      st->dtx_enabled=0;
1551
1552
   /*Loop on subframes */
1553
56.5k
   for (sub=0;sub<NB_NB_SUBFRAMES;sub++)
1554
45.2k
   {
1555
45.2k
      int offset;
1556
45.2k
      spx_word16_t *exc;
1557
45.2k
      spx_word16_t *innov_save = NULL;
1558
45.2k
      spx_word16_t tmp;
1559
1560
      /* Offset relative to start of frame */
1561
45.2k
      offset = NB_SUBFRAME_SIZE*sub;
1562
      /* Excitation */
1563
45.2k
      exc=st->exc+offset;
1564
      /* Original signal */
1565
45.2k
      if (st->innov_save)
1566
30.0k
         innov_save = st->innov_save+offset;
1567
1568
1569
      /* Reset excitation */
1570
45.2k
      SPEEX_MEMSET(exc, 0, NB_SUBFRAME_SIZE);
1571
1572
      /*Adaptive codebook contribution*/
1573
45.2k
      speex_assert (SUBMODE(ltp_unquant));
1574
45.2k
      {
1575
45.2k
         int pit_min, pit_max;
1576
         /* Handle pitch constraints if any */
1577
45.2k
         if (SUBMODE(lbr_pitch) != -1)
1578
20.0k
         {
1579
20.0k
            int margin;
1580
20.0k
            margin = SUBMODE(lbr_pitch);
1581
20.0k
            if (margin)
1582
0
            {
1583
/* GT - need optimization?
1584
               if (ol_pitch < NB_PITCH_START+margin-1)
1585
                  ol_pitch=NB_PITCH_START+margin-1;
1586
               if (ol_pitch > NB_PITCH_END-margin)
1587
                  ol_pitch=NB_PITCH_END-margin;
1588
               pit_min = ol_pitch-margin+1;
1589
               pit_max = ol_pitch+margin;
1590
*/
1591
0
               pit_min = ol_pitch-margin+1;
1592
0
               if (pit_min < NB_PITCH_START)
1593
0
      pit_min = NB_PITCH_START;
1594
0
               pit_max = ol_pitch+margin;
1595
0
               if (pit_max > NB_PITCH_END)
1596
0
      pit_max = NB_PITCH_END;
1597
20.0k
            } else {
1598
20.0k
               pit_min = pit_max = ol_pitch;
1599
20.0k
            }
1600
25.1k
         } else {
1601
25.1k
            pit_min = NB_PITCH_START;
1602
25.1k
            pit_max = NB_PITCH_END;
1603
25.1k
         }
1604
1605
1606
1607
45.2k
         SUBMODE(ltp_unquant)(exc, exc32, pit_min, pit_max, ol_pitch_coef, SUBMODE(ltp_params),
1608
45.2k
                 NB_SUBFRAME_SIZE, &pitch, &pitch_gain[0], bits, stack,
1609
45.2k
                 st->count_lost, offset, st->last_pitch_gain, 0);
1610
1611
         /* Ensuring that things aren't blowing up as would happen if e.g. an encoder is
1612
         crafting packets to make us produce NaNs and slow down the decoder (vague DoS threat).
1613
         We can probably be even more aggressive and limit to 15000 or so. */
1614
45.2k
         sanitize_values32(exc32, NEG32(QCONST32(32000,SIG_SHIFT-1)), QCONST32(32000,SIG_SHIFT-1), NB_SUBFRAME_SIZE);
1615
1616
45.2k
         tmp = gain_3tap_to_1tap(pitch_gain);
1617
1618
45.2k
         pitch_average += tmp;
1619
45.2k
         if ((tmp>best_pitch_gain&&ABS(2*best_pitch-pitch)>=3&&ABS(3*best_pitch-pitch)>=4&&ABS(4*best_pitch-pitch)>=5)
1620
41.9k
              || (tmp>MULT16_16_Q15(QCONST16(.6,15),best_pitch_gain)&&(ABS(best_pitch-2*pitch)<3||ABS(best_pitch-3*pitch)<4||ABS(best_pitch-4*pitch)<5))
1621
41.2k
              || (MULT16_16_Q15(QCONST16(.67,15),tmp)>best_pitch_gain&&(ABS(2*best_pitch-pitch)<3||ABS(3*best_pitch-pitch)<4||ABS(4*best_pitch-pitch)<5)) )
1622
5.68k
         {
1623
5.68k
            best_pitch = pitch;
1624
5.68k
            if (tmp > best_pitch_gain)
1625
4.96k
               best_pitch_gain = tmp;
1626
5.68k
         }
1627
45.2k
      }
1628
1629
      /* Unquantize the innovation */
1630
45.2k
      {
1631
45.2k
         int q_energy;
1632
45.2k
         spx_word32_t ener;
1633
1634
45.2k
         SPEEX_MEMSET(innov, 0, NB_SUBFRAME_SIZE);
1635
1636
         /* Decode sub-frame gain correction */
1637
45.2k
         if (SUBMODE(have_subframe_gain)==3)
1638
9.52k
         {
1639
9.52k
            q_energy = speex_bits_unpack_unsigned(bits, 3);
1640
9.52k
            ener = MULT16_32_Q14(exc_gain_quant_scal3[q_energy],ol_gain);
1641
35.6k
         } else if (SUBMODE(have_subframe_gain)==1)
1642
15.6k
         {
1643
15.6k
            q_energy = speex_bits_unpack_unsigned(bits, 1);
1644
15.6k
            ener = MULT16_32_Q14(exc_gain_quant_scal1[q_energy],ol_gain);
1645
20.0k
         } else {
1646
20.0k
            ener = ol_gain;
1647
20.0k
         }
1648
1649
45.2k
         speex_assert (SUBMODE(innovation_unquant));
1650
45.2k
         {
1651
            /*Fixed codebook contribution*/
1652
45.2k
            SUBMODE(innovation_unquant)(innov, SUBMODE(innovation_params), NB_SUBFRAME_SIZE, bits, stack, &st->seed);
1653
            /* De-normalize innovation and update excitation */
1654
1655
45.2k
            signal_mul(innov, innov, ener, NB_SUBFRAME_SIZE);
1656
1657
            /* Decode second codebook (only for some modes) */
1658
45.2k
            if (SUBMODE(double_codebook))
1659
3.24k
            {
1660
3.24k
               char *tmp_stack=stack;
1661
3.24k
               VARDECL(spx_sig_t *innov2);
1662
3.24k
               ALLOC(innov2, NB_SUBFRAME_SIZE, spx_sig_t);
1663
3.24k
               SPEEX_MEMSET(innov2, 0, NB_SUBFRAME_SIZE);
1664
3.24k
               SUBMODE(innovation_unquant)(innov2, SUBMODE(innovation_params), NB_SUBFRAME_SIZE, bits, stack, &st->seed);
1665
3.24k
               signal_mul(innov2, innov2, MULT16_32_Q15(QCONST16(0.454545f,15),ener), NB_SUBFRAME_SIZE);
1666
133k
               for (i=0;i<NB_SUBFRAME_SIZE;i++)
1667
129k
                  innov[i] = ADD32(innov[i], innov2[i]);
1668
3.24k
               stack = tmp_stack;
1669
3.24k
            }
1670
1.85M
            for (i=0;i<NB_SUBFRAME_SIZE;i++)
1671
1.80M
               exc[i]=EXTRACT16(SATURATE32(PSHR32(ADD32(SHL32(exc32[i],1),innov[i]),SIG_SHIFT),32767));
1672
            /*print_vec(exc, 40, "innov");*/
1673
45.2k
            if (innov_save)
1674
30.0k
            {
1675
1.23M
               for (i=0;i<NB_SUBFRAME_SIZE;i++)
1676
1.20M
                  innov_save[i] = EXTRACT16(PSHR32(innov[i], SIG_SHIFT));
1677
30.0k
            }
1678
45.2k
         }
1679
1680
         /*Vocoder mode*/
1681
45.2k
         if (st->submodeID==1)
1682
7.16k
         {
1683
7.16k
            spx_word16_t g=ol_pitch_coef;
1684
7.16k
            g=MULT16_16_P14(QCONST16(1.5f,14),(g-QCONST16(.2f,6)));
1685
7.16k
            if (g<0)
1686
4.78k
               g=0;
1687
7.16k
            if (g>GAIN_SCALING)
1688
748
               g=GAIN_SCALING;
1689
1690
7.16k
            SPEEX_MEMSET(exc, 0, NB_SUBFRAME_SIZE);
1691
18.6k
            while (st->voc_offset<NB_SUBFRAME_SIZE)
1692
11.5k
            {
1693
               /* exc[st->voc_offset]= g*sqrt(2*ol_pitch)*ol_gain;
1694
                  Not quite sure why we need the factor of two in the sqrt */
1695
11.5k
               if (st->voc_offset>=0)
1696
11.5k
                  exc[st->voc_offset]=MULT16_16(spx_sqrt(MULT16_16_16(2,ol_pitch)),EXTRACT16(PSHR32(MULT16_16(g,PSHR32(ol_gain,SIG_SHIFT)),6)));
1697
11.5k
               st->voc_offset+=ol_pitch;
1698
11.5k
            }
1699
7.16k
            st->voc_offset -= NB_SUBFRAME_SIZE;
1700
1701
293k
            for (i=0;i<NB_SUBFRAME_SIZE;i++)
1702
286k
            {
1703
286k
               spx_word16_t exci=exc[i];
1704
286k
               exc[i]= ADD16(ADD16(MULT16_16_Q15(QCONST16(.7f,15),exc[i]) , MULT16_16_Q15(QCONST16(.3f,15),st->voc_m1)),
1705
286k
                             SUB16(MULT16_16_Q15(Q15_ONE-MULT16_16_16(QCONST16(.85f,9),g),EXTRACT16(PSHR32(innov[i],SIG_SHIFT))),
1706
286k
                                   MULT16_16_Q15(MULT16_16_16(QCONST16(.15f,9),g),EXTRACT16(PSHR32(st->voc_m2,SIG_SHIFT)))
1707
286k
                                  ));
1708
286k
               st->voc_m1 = exci;
1709
286k
               st->voc_m2=innov[i];
1710
286k
               st->voc_mean = EXTRACT16(PSHR32(ADD32(MULT16_16(QCONST16(.8f,15),st->voc_mean), MULT16_16(QCONST16(.2f,15),exc[i])), 15));
1711
286k
               exc[i]-=st->voc_mean;
1712
286k
            }
1713
7.16k
         }
1714
1715
45.2k
      }
1716
45.2k
   }
1717
1718
11.3k
   ALLOC(interp_qlsp, NB_ORDER, spx_lsp_t);
1719
1720
11.3k
   if (st->lpc_enh_enabled && SUBMODE(comb_gain)>0 && !st->count_lost)
1721
9.51k
   {
1722
9.51k
      multicomb(st->exc-NB_SUBFRAME_SIZE, out, st->interp_qlpc, NB_ORDER, 2*NB_SUBFRAME_SIZE, best_pitch, 40, SUBMODE(comb_gain), stack);
1723
9.51k
      multicomb(st->exc+NB_SUBFRAME_SIZE, out+2*NB_SUBFRAME_SIZE, st->interp_qlpc, NB_ORDER, 2*NB_SUBFRAME_SIZE, best_pitch, 40, SUBMODE(comb_gain), stack);
1724
9.51k
   } else {
1725
1.79k
      SPEEX_COPY(out, &st->exc[-NB_SUBFRAME_SIZE], NB_FRAME_SIZE);
1726
1.79k
   }
1727
1728
   /* If the last packet was lost, re-scale the excitation to obtain the same energy as encoded in ol_gain */
1729
11.3k
   if (st->count_lost)
1730
0
   {
1731
0
      spx_word16_t exc_ener;
1732
0
      spx_word32_t gain32;
1733
0
      spx_word16_t gain;
1734
0
      exc_ener = compute_rms16 (st->exc, NB_FRAME_SIZE);
1735
0
      gain32 = PDIV32(ol_gain, ADD16(exc_ener,1));
1736
#ifdef FIXED_POINT
1737
      if (gain32 > 32767)
1738
         gain32 = 32767;
1739
      gain = EXTRACT16(gain32);
1740
#else
1741
0
      if (gain32 > 2)
1742
0
         gain32=2;
1743
0
      gain = gain32;
1744
0
#endif
1745
0
      for (i=0;i<NB_FRAME_SIZE;i++)
1746
0
      {
1747
0
         st->exc[i] = MULT16_16_Q14(gain, st->exc[i]);
1748
0
         out[i]=st->exc[i-NB_SUBFRAME_SIZE];
1749
0
      }
1750
0
   }
1751
1752
   /*Loop on subframes */
1753
56.5k
   for (sub=0;sub<NB_NB_SUBFRAMES;sub++)
1754
45.2k
   {
1755
45.2k
      int offset;
1756
45.2k
      spx_word16_t *sp;
1757
1758
      /* Offset relative to start of frame */
1759
45.2k
      offset = NB_SUBFRAME_SIZE*sub;
1760
      /* Original signal */
1761
45.2k
      sp=out+offset;
1762
1763
      /* LSP interpolation (quantized and unquantized) */
1764
45.2k
      lsp_interpolate(st->old_qlsp, qlsp, interp_qlsp, NB_ORDER, sub, NB_NB_SUBFRAMES, LSP_MARGIN);
1765
1766
      /* Compute interpolated LPCs (unquantized) */
1767
45.2k
      lsp_to_lpc(interp_qlsp, ak, NB_ORDER, stack);
1768
1769
      /* Compute analysis filter at w=pi */
1770
45.2k
      {
1771
45.2k
         spx_word32_t pi_g=LPC_SCALING;
1772
271k
         for (i=0;i<NB_ORDER;i+=2)
1773
226k
         {
1774
            /*pi_g += -st->interp_qlpc[i] +  st->interp_qlpc[i+1];*/
1775
226k
            pi_g = ADD32(pi_g, SUB32(EXTEND32(ak[i+1]),EXTEND32(ak[i])));
1776
226k
         }
1777
45.2k
         st->pi_gain[sub] = pi_g;
1778
45.2k
      }
1779
1780
45.2k
      iir_mem16(sp, st->interp_qlpc, sp, NB_SUBFRAME_SIZE, NB_ORDER,
1781
45.2k
                st->mem_sp, stack);
1782
1783
497k
      for (i=0;i<NB_ORDER;i++)
1784
452k
         st->interp_qlpc[i] = ak[i];
1785
1786
45.2k
   }
1787
1788
11.3k
   if (st->highpass_enabled)
1789
11.3k
      highpass(out, out, NB_FRAME_SIZE, (st->isWideband?HIGHPASS_WIDEBAND:HIGHPASS_NARROWBAND)|HIGHPASS_OUTPUT, st->mem_hp);
1790
   /*for (i=0;i<NB_FRAME_SIZE;i++)
1791
     printf ("%d\n", (int)st->frame[i]);*/
1792
1793
   /* Tracking output level */
1794
11.3k
   st->level = 1+PSHR32(ol_gain,SIG_SHIFT);
1795
11.3k
   st->max_level = MAX16(MULT16_16_Q15(QCONST16(.99f,15), st->max_level), st->level);
1796
11.3k
   st->min_level = MIN16(ADD16(1,MULT16_16_Q14(QCONST16(1.01f,14), st->min_level)), st->level);
1797
11.3k
   if (st->max_level < st->min_level+1)
1798
3.23k
      st->max_level = st->min_level+1;
1799
   /*printf ("%f %f %f %d\n", og, st->min_level, st->max_level, update);*/
1800
1801
   /* Store the LSPs for interpolation in the next frame */
1802
124k
   for (i=0;i<NB_ORDER;i++)
1803
113k
      st->old_qlsp[i] = qlsp[i];
1804
1805
   /* The next frame will not be the first (Duh!) */
1806
11.3k
   st->first = 0;
1807
11.3k
   st->count_lost=0;
1808
11.3k
   st->last_pitch = best_pitch;
1809
#ifdef FIXED_POINT
1810
   st->last_pitch_gain = PSHR16(pitch_average,2);
1811
#else
1812
11.3k
   st->last_pitch_gain = .25*pitch_average;
1813
11.3k
#endif
1814
11.3k
   st->pitch_gain_buf[st->pitch_gain_buf_idx++] = st->last_pitch_gain;
1815
11.3k
   if (st->pitch_gain_buf_idx > 2) /* rollover */
1816
3.51k
      st->pitch_gain_buf_idx = 0;
1817
1818
11.3k
   st->last_ol_gain = ol_gain;
1819
1820
11.3k
   return 0;
1821
20.4k
}
nb_decode
Line
Count
Source
1331
33.9k
{
1332
33.9k
   DecState *st;
1333
33.9k
   int i, sub;
1334
33.9k
   int pitch;
1335
33.9k
   spx_word16_t pitch_gain[3];
1336
33.9k
   spx_word32_t ol_gain=0;
1337
33.9k
   int ol_pitch=0;
1338
33.9k
   spx_word16_t ol_pitch_coef=0;
1339
33.9k
   int best_pitch=40;
1340
33.9k
   spx_word16_t best_pitch_gain=0;
1341
33.9k
   int wideband;
1342
33.9k
   int m;
1343
33.9k
   char *stack;
1344
33.9k
   VARDECL(spx_sig_t *innov);
1345
33.9k
   VARDECL(spx_word32_t *exc32);
1346
33.9k
   VARDECL(spx_coef_t *ak);
1347
33.9k
   VARDECL(spx_lsp_t *qlsp);
1348
33.9k
   spx_word16_t pitch_average=0;
1349
1350
33.9k
   spx_word16_t *out = (spx_word16_t*)vout;
1351
33.9k
   VARDECL(spx_lsp_t *interp_qlsp);
1352
1353
33.9k
   st=(DecState*)state;
1354
33.9k
   stack=st->stack;
1355
1356
33.9k
   st->exc = st->excBuf + 2*NB_PITCH_END + NB_SUBFRAME_SIZE + 6;
1357
1358
   /* Check if we're in DTX mode*/
1359
33.9k
   if (!bits && st->dtx_enabled)
1360
0
   {
1361
0
      st->submodeID=0;
1362
0
   } else
1363
33.9k
   {
1364
      /* If bits is NULL, consider the packet to be lost (what could we do anyway) */
1365
33.9k
      if (!bits)
1366
0
      {
1367
0
         nb_decode_lost(st, out, stack);
1368
0
         return 0;
1369
0
      }
1370
1371
33.9k
      if (st->encode_submode)
1372
33.9k
      {
1373
1374
      /* Search for next narrowband block (handle requests, skip wideband blocks) */
1375
36.0k
      do {
1376
36.0k
         if (speex_bits_remaining(bits)<5)
1377
5.14k
            return -1;
1378
30.9k
         wideband = speex_bits_unpack_unsigned(bits, 1);
1379
30.9k
         if (wideband) /* Skip wideband block (for compatibility) */
1380
1.80k
         {
1381
1.80k
            int submode;
1382
1.80k
            int advance;
1383
1.80k
            advance = submode = speex_bits_unpack_unsigned(bits, SB_SUBMODE_BITS);
1384
            /*speex_mode_query(&speex_wb_mode, SPEEX_SUBMODE_BITS_PER_FRAME, &advance);*/
1385
1.80k
            advance = wb_skip_table[submode];
1386
1.80k
            if (advance < 0)
1387
0
            {
1388
0
               speex_notify("Invalid mode encountered. The stream is corrupted.");
1389
0
               return -2;
1390
0
            }
1391
1.80k
            advance -= (SB_SUBMODE_BITS+1);
1392
1.80k
            speex_bits_advance(bits, advance);
1393
1394
1.80k
            if (speex_bits_remaining(bits)<5)
1395
435
               return -1;
1396
1.36k
            wideband = speex_bits_unpack_unsigned(bits, 1);
1397
1.36k
            if (wideband)
1398
1.05k
            {
1399
1.05k
               advance = submode = speex_bits_unpack_unsigned(bits, SB_SUBMODE_BITS);
1400
               /*speex_mode_query(&speex_wb_mode, SPEEX_SUBMODE_BITS_PER_FRAME, &advance);*/
1401
1.05k
               advance = wb_skip_table[submode];
1402
1.05k
               if (advance < 0)
1403
0
               {
1404
0
                  speex_notify("Invalid mode encountered. The stream is corrupted.");
1405
0
                  return -2;
1406
0
               }
1407
1.05k
               advance -= (SB_SUBMODE_BITS+1);
1408
1.05k
               speex_bits_advance(bits, advance);
1409
1.05k
               wideband = speex_bits_unpack_unsigned(bits, 1);
1410
1.05k
               if (wideband)
1411
849
               {
1412
849
                  speex_notify("More than two wideband layers found. The stream is corrupted.");
1413
849
                  return -2;
1414
849
               }
1415
1416
1.05k
            }
1417
1.36k
         }
1418
29.6k
         if (speex_bits_remaining(bits)<4)
1419
204
            return -1;
1420
         /* FIXME: Check for overflow */
1421
29.4k
         m = speex_bits_unpack_unsigned(bits, 4);
1422
29.4k
         if (m==15) /* We found a terminator */
1423
389
         {
1424
389
            return -1;
1425
29.0k
         } else if (m==14) /* Speex in-band request */
1426
1.89k
         {
1427
1.89k
            int ret = speex_inband_handler(bits, st->speex_callbacks, state);
1428
1.89k
            if (ret)
1429
0
               return ret;
1430
27.1k
         } else if (m==13) /* User in-band request */
1431
254
         {
1432
254
            int ret = st->user_callback.func(bits, state, st->user_callback.data);
1433
254
            if (ret)
1434
0
               return ret;
1435
26.8k
         } else if (m>8) /* Invalid mode */
1436
1.66k
         {
1437
1.66k
            speex_notify("Invalid mode encountered. The stream is corrupted.");
1438
1.66k
            return -2;
1439
1.66k
         }
1440
1441
29.4k
      } while (m>8);
1442
1443
      /* Get the sub-mode that was used */
1444
25.2k
      st->submodeID = m;
1445
25.2k
      }
1446
1447
33.9k
   }
1448
1449
   /* Shift all buffers by one frame */
1450
25.2k
   SPEEX_MOVE(st->excBuf, st->excBuf+NB_FRAME_SIZE, 2*NB_PITCH_END + NB_SUBFRAME_SIZE + 12);
1451
1452
   /* If null mode (no transmission), just set a couple things to zero*/
1453
25.2k
   if (st->submodes[st->submodeID] == NULL)
1454
11.8k
   {
1455
11.8k
      VARDECL(spx_coef_t *lpc);
1456
11.8k
      ALLOC(lpc, NB_ORDER, spx_coef_t);
1457
11.8k
      bw_lpc(QCONST16(0.93f,15), st->interp_qlpc, lpc, NB_ORDER);
1458
11.8k
      {
1459
11.8k
         spx_word16_t innov_gain=0;
1460
         /* FIXME: This was innov, not exc */
1461
11.8k
         innov_gain = compute_rms16(st->exc, NB_FRAME_SIZE);
1462
1.90M
         for (i=0;i<NB_FRAME_SIZE;i++)
1463
1.89M
            st->exc[i]=speex_rand(innov_gain, &st->seed);
1464
11.8k
      }
1465
1466
1467
11.8k
      st->first=1;
1468
1469
      /* Final signal synthesis from excitation */
1470
11.8k
      iir_mem16(st->exc, lpc, out, NB_FRAME_SIZE, NB_ORDER, st->mem_sp, stack);
1471
1472
      /* Normally this is written to later but since this is returning early,
1473
         avoid reading uninitialized memory in caller */
1474
11.8k
      if (st->innov_save)
1475
9.27k
         SPEEX_MEMSET(st->innov_save, 0, NB_NB_SUBFRAMES*NB_SUBFRAME_SIZE);
1476
1477
11.8k
      st->count_lost=0;
1478
11.8k
      return 0;
1479
11.8k
   }
1480
1481
13.4k
   ALLOC(qlsp, NB_ORDER, spx_lsp_t);
1482
1483
   /* Unquantize LSPs */
1484
13.4k
   SUBMODE(lsp_unquant)(qlsp, NB_ORDER, bits);
1485
1486
   /*Damp memory if a frame was lost and the LSP changed too much*/
1487
13.4k
   if (st->count_lost)
1488
0
   {
1489
0
      spx_word16_t fact;
1490
0
      spx_word32_t lsp_dist=0;
1491
0
      for (i=0;i<NB_ORDER;i++)
1492
0
         lsp_dist = ADD32(lsp_dist, EXTEND32(ABS(st->old_qlsp[i] - qlsp[i])));
1493
0
#ifdef FIXED_POINT
1494
0
      fact = SHR16(19661,SHR32(lsp_dist,LSP_SHIFT+2));
1495
#else
1496
      fact = .6*exp(-.2*lsp_dist);
1497
#endif
1498
0
      for (i=0;i<NB_ORDER;i++)
1499
0
         st->mem_sp[i] = MULT16_32_Q15(fact,st->mem_sp[i]);
1500
0
   }
1501
1502
1503
   /* Handle first frame and lost-packet case */
1504
13.4k
   if (st->first || st->count_lost)
1505
2.88k
   {
1506
31.7k
      for (i=0;i<NB_ORDER;i++)
1507
28.8k
         st->old_qlsp[i] = qlsp[i];
1508
2.88k
   }
1509
1510
   /* Get open-loop pitch estimation for low bit-rate pitch coding */
1511
13.4k
   if (SUBMODE(lbr_pitch)!=-1)
1512
6.26k
   {
1513
6.26k
      ol_pitch = NB_PITCH_START+speex_bits_unpack_unsigned(bits, 7);
1514
6.26k
   }
1515
1516
13.4k
   if (SUBMODE(forced_pitch_gain))
1517
4.46k
   {
1518
4.46k
      int quant;
1519
4.46k
      quant = speex_bits_unpack_unsigned(bits, 4);
1520
4.46k
      ol_pitch_coef=MULT16_16_P15(QCONST16(0.066667,15),SHL16(quant,GAIN_SHIFT));
1521
4.46k
   }
1522
1523
   /* Get global excitation gain */
1524
13.4k
   {
1525
13.4k
      int qe;
1526
13.4k
      qe = speex_bits_unpack_unsigned(bits, 5);
1527
13.4k
#ifdef FIXED_POINT
1528
      /* FIXME: Perhaps we could slightly lower the gain here when the output is going to saturate? */
1529
13.4k
      ol_gain = MULT16_32_Q15(28406,ol_gain_table[qe]);
1530
#else
1531
      ol_gain = SIG_SCALING*exp(qe/3.5);
1532
#endif
1533
13.4k
   }
1534
1535
13.4k
   ALLOC(ak, NB_ORDER, spx_coef_t);
1536
13.4k
   ALLOC(innov, NB_SUBFRAME_SIZE, spx_sig_t);
1537
13.4k
   ALLOC(exc32, NB_SUBFRAME_SIZE, spx_word32_t);
1538
1539
13.4k
   if (st->submodeID==1)
1540
2.59k
   {
1541
2.59k
      int extra;
1542
2.59k
      extra = speex_bits_unpack_unsigned(bits, 4);
1543
1544
2.59k
      if (extra==15)
1545
312
         st->dtx_enabled=1;
1546
2.28k
      else
1547
2.28k
         st->dtx_enabled=0;
1548
2.59k
   }
1549
13.4k
   if (st->submodeID>1)
1550
10.8k
      st->dtx_enabled=0;
1551
1552
   /*Loop on subframes */
1553
67.0k
   for (sub=0;sub<NB_NB_SUBFRAMES;sub++)
1554
53.6k
   {
1555
53.6k
      int offset;
1556
53.6k
      spx_word16_t *exc;
1557
53.6k
      spx_word16_t *innov_save = NULL;
1558
53.6k
      spx_word16_t tmp;
1559
1560
      /* Offset relative to start of frame */
1561
53.6k
      offset = NB_SUBFRAME_SIZE*sub;
1562
      /* Excitation */
1563
53.6k
      exc=st->exc+offset;
1564
      /* Original signal */
1565
53.6k
      if (st->innov_save)
1566
38.4k
         innov_save = st->innov_save+offset;
1567
1568
1569
      /* Reset excitation */
1570
53.6k
      SPEEX_MEMSET(exc, 0, NB_SUBFRAME_SIZE);
1571
1572
      /*Adaptive codebook contribution*/
1573
53.6k
      speex_assert (SUBMODE(ltp_unquant));
1574
53.6k
      {
1575
53.6k
         int pit_min, pit_max;
1576
         /* Handle pitch constraints if any */
1577
53.6k
         if (SUBMODE(lbr_pitch) != -1)
1578
25.0k
         {
1579
25.0k
            int margin;
1580
25.0k
            margin = SUBMODE(lbr_pitch);
1581
25.0k
            if (margin)
1582
0
            {
1583
/* GT - need optimization?
1584
               if (ol_pitch < NB_PITCH_START+margin-1)
1585
                  ol_pitch=NB_PITCH_START+margin-1;
1586
               if (ol_pitch > NB_PITCH_END-margin)
1587
                  ol_pitch=NB_PITCH_END-margin;
1588
               pit_min = ol_pitch-margin+1;
1589
               pit_max = ol_pitch+margin;
1590
*/
1591
0
               pit_min = ol_pitch-margin+1;
1592
0
               if (pit_min < NB_PITCH_START)
1593
0
      pit_min = NB_PITCH_START;
1594
0
               pit_max = ol_pitch+margin;
1595
0
               if (pit_max > NB_PITCH_END)
1596
0
      pit_max = NB_PITCH_END;
1597
25.0k
            } else {
1598
25.0k
               pit_min = pit_max = ol_pitch;
1599
25.0k
            }
1600
28.5k
         } else {
1601
28.5k
            pit_min = NB_PITCH_START;
1602
28.5k
            pit_max = NB_PITCH_END;
1603
28.5k
         }
1604
1605
1606
1607
53.6k
         SUBMODE(ltp_unquant)(exc, exc32, pit_min, pit_max, ol_pitch_coef, SUBMODE(ltp_params),
1608
53.6k
                 NB_SUBFRAME_SIZE, &pitch, &pitch_gain[0], bits, stack,
1609
53.6k
                 st->count_lost, offset, st->last_pitch_gain, 0);
1610
1611
         /* Ensuring that things aren't blowing up as would happen if e.g. an encoder is
1612
         crafting packets to make us produce NaNs and slow down the decoder (vague DoS threat).
1613
         We can probably be even more aggressive and limit to 15000 or so. */
1614
53.6k
         sanitize_values32(exc32, NEG32(QCONST32(32000,SIG_SHIFT-1)), QCONST32(32000,SIG_SHIFT-1), NB_SUBFRAME_SIZE);
1615
1616
53.6k
         tmp = gain_3tap_to_1tap(pitch_gain);
1617
1618
53.6k
         pitch_average += tmp;
1619
53.6k
         if ((tmp>best_pitch_gain&&ABS(2*best_pitch-pitch)>=3&&ABS(3*best_pitch-pitch)>=4&&ABS(4*best_pitch-pitch)>=5)
1620
47.2k
              || (tmp>MULT16_16_Q15(QCONST16(.6,15),best_pitch_gain)&&(ABS(best_pitch-2*pitch)<3||ABS(best_pitch-3*pitch)<4||ABS(best_pitch-4*pitch)<5))
1621
46.5k
              || (MULT16_16_Q15(QCONST16(.67,15),tmp)>best_pitch_gain&&(ABS(2*best_pitch-pitch)<3||ABS(3*best_pitch-pitch)<4||ABS(4*best_pitch-pitch)<5)) )
1622
9.17k
         {
1623
9.17k
            best_pitch = pitch;
1624
9.17k
            if (tmp > best_pitch_gain)
1625
8.49k
               best_pitch_gain = tmp;
1626
9.17k
         }
1627
53.6k
      }
1628
1629
      /* Unquantize the innovation */
1630
53.6k
      {
1631
53.6k
         int q_energy;
1632
53.6k
         spx_word32_t ener;
1633
1634
53.6k
         SPEEX_MEMSET(innov, 0, NB_SUBFRAME_SIZE);
1635
1636
         /* Decode sub-frame gain correction */
1637
53.6k
         if (SUBMODE(have_subframe_gain)==3)
1638
10.3k
         {
1639
10.3k
            q_energy = speex_bits_unpack_unsigned(bits, 3);
1640
10.3k
            ener = MULT16_32_Q14(exc_gain_quant_scal3[q_energy],ol_gain);
1641
43.2k
         } else if (SUBMODE(have_subframe_gain)==1)
1642
18.2k
         {
1643
18.2k
            q_energy = speex_bits_unpack_unsigned(bits, 1);
1644
18.2k
            ener = MULT16_32_Q14(exc_gain_quant_scal1[q_energy],ol_gain);
1645
25.0k
         } else {
1646
25.0k
            ener = ol_gain;
1647
25.0k
         }
1648
1649
53.6k
         speex_assert (SUBMODE(innovation_unquant));
1650
53.6k
         {
1651
            /*Fixed codebook contribution*/
1652
53.6k
            SUBMODE(innovation_unquant)(innov, SUBMODE(innovation_params), NB_SUBFRAME_SIZE, bits, stack, &st->seed);
1653
            /* De-normalize innovation and update excitation */
1654
1655
53.6k
            signal_mul(innov, innov, ener, NB_SUBFRAME_SIZE);
1656
1657
            /* Decode second codebook (only for some modes) */
1658
53.6k
            if (SUBMODE(double_codebook))
1659
3.36k
            {
1660
3.36k
               char *tmp_stack=stack;
1661
3.36k
               VARDECL(spx_sig_t *innov2);
1662
3.36k
               ALLOC(innov2, NB_SUBFRAME_SIZE, spx_sig_t);
1663
3.36k
               SPEEX_MEMSET(innov2, 0, NB_SUBFRAME_SIZE);
1664
3.36k
               SUBMODE(innovation_unquant)(innov2, SUBMODE(innovation_params), NB_SUBFRAME_SIZE, bits, stack, &st->seed);
1665
3.36k
               signal_mul(innov2, innov2, MULT16_32_Q15(QCONST16(0.454545f,15),ener), NB_SUBFRAME_SIZE);
1666
137k
               for (i=0;i<NB_SUBFRAME_SIZE;i++)
1667
134k
                  innov[i] = ADD32(innov[i], innov2[i]);
1668
3.36k
               stack = tmp_stack;
1669
3.36k
            }
1670
2.19M
            for (i=0;i<NB_SUBFRAME_SIZE;i++)
1671
2.14M
               exc[i]=EXTRACT16(SATURATE32(PSHR32(ADD32(SHL32(exc32[i],1),innov[i]),SIG_SHIFT),32767));
1672
            /*print_vec(exc, 40, "innov");*/
1673
53.6k
            if (innov_save)
1674
38.4k
            {
1675
1.57M
               for (i=0;i<NB_SUBFRAME_SIZE;i++)
1676
1.53M
                  innov_save[i] = EXTRACT16(PSHR32(innov[i], SIG_SHIFT));
1677
38.4k
            }
1678
53.6k
         }
1679
1680
         /*Vocoder mode*/
1681
53.6k
         if (st->submodeID==1)
1682
10.3k
         {
1683
10.3k
            spx_word16_t g=ol_pitch_coef;
1684
10.3k
            g=MULT16_16_P14(QCONST16(1.5f,14),(g-QCONST16(.2f,6)));
1685
10.3k
            if (g<0)
1686
6.93k
               g=0;
1687
10.3k
            if (g>GAIN_SCALING)
1688
1.01k
               g=GAIN_SCALING;
1689
1690
10.3k
            SPEEX_MEMSET(exc, 0, NB_SUBFRAME_SIZE);
1691
27.0k
            while (st->voc_offset<NB_SUBFRAME_SIZE)
1692
16.6k
            {
1693
               /* exc[st->voc_offset]= g*sqrt(2*ol_pitch)*ol_gain;
1694
                  Not quite sure why we need the factor of two in the sqrt */
1695
16.6k
               if (st->voc_offset>=0)
1696
16.6k
                  exc[st->voc_offset]=MULT16_16(spx_sqrt(MULT16_16_16(2,ol_pitch)),EXTRACT16(PSHR32(MULT16_16(g,PSHR32(ol_gain,SIG_SHIFT)),6)));
1697
16.6k
               st->voc_offset+=ol_pitch;
1698
16.6k
            }
1699
10.3k
            st->voc_offset -= NB_SUBFRAME_SIZE;
1700
1701
425k
            for (i=0;i<NB_SUBFRAME_SIZE;i++)
1702
415k
            {
1703
415k
               spx_word16_t exci=exc[i];
1704
415k
               exc[i]= ADD16(ADD16(MULT16_16_Q15(QCONST16(.7f,15),exc[i]) , MULT16_16_Q15(QCONST16(.3f,15),st->voc_m1)),
1705
415k
                             SUB16(MULT16_16_Q15(Q15_ONE-MULT16_16_16(QCONST16(.85f,9),g),EXTRACT16(PSHR32(innov[i],SIG_SHIFT))),
1706
415k
                                   MULT16_16_Q15(MULT16_16_16(QCONST16(.15f,9),g),EXTRACT16(PSHR32(st->voc_m2,SIG_SHIFT)))
1707
415k
                                  ));
1708
415k
               st->voc_m1 = exci;
1709
415k
               st->voc_m2=innov[i];
1710
415k
               st->voc_mean = EXTRACT16(PSHR32(ADD32(MULT16_16(QCONST16(.8f,15),st->voc_mean), MULT16_16(QCONST16(.2f,15),exc[i])), 15));
1711
415k
               exc[i]-=st->voc_mean;
1712
415k
            }
1713
10.3k
         }
1714
1715
53.6k
      }
1716
53.6k
   }
1717
1718
13.4k
   ALLOC(interp_qlsp, NB_ORDER, spx_lsp_t);
1719
1720
13.4k
   if (st->lpc_enh_enabled && SUBMODE(comb_gain)>0 && !st->count_lost)
1721
10.8k
   {
1722
10.8k
      multicomb(st->exc-NB_SUBFRAME_SIZE, out, st->interp_qlpc, NB_ORDER, 2*NB_SUBFRAME_SIZE, best_pitch, 40, SUBMODE(comb_gain), stack);
1723
10.8k
      multicomb(st->exc+NB_SUBFRAME_SIZE, out+2*NB_SUBFRAME_SIZE, st->interp_qlpc, NB_ORDER, 2*NB_SUBFRAME_SIZE, best_pitch, 40, SUBMODE(comb_gain), stack);
1724
10.8k
   } else {
1725
2.59k
      SPEEX_COPY(out, &st->exc[-NB_SUBFRAME_SIZE], NB_FRAME_SIZE);
1726
2.59k
   }
1727
1728
   /* If the last packet was lost, re-scale the excitation to obtain the same energy as encoded in ol_gain */
1729
13.4k
   if (st->count_lost)
1730
0
   {
1731
0
      spx_word16_t exc_ener;
1732
0
      spx_word32_t gain32;
1733
0
      spx_word16_t gain;
1734
0
      exc_ener = compute_rms16 (st->exc, NB_FRAME_SIZE);
1735
0
      gain32 = PDIV32(ol_gain, ADD16(exc_ener,1));
1736
0
#ifdef FIXED_POINT
1737
0
      if (gain32 > 32767)
1738
0
         gain32 = 32767;
1739
0
      gain = EXTRACT16(gain32);
1740
#else
1741
      if (gain32 > 2)
1742
         gain32=2;
1743
      gain = gain32;
1744
#endif
1745
0
      for (i=0;i<NB_FRAME_SIZE;i++)
1746
0
      {
1747
0
         st->exc[i] = MULT16_16_Q14(gain, st->exc[i]);
1748
0
         out[i]=st->exc[i-NB_SUBFRAME_SIZE];
1749
0
      }
1750
0
   }
1751
1752
   /*Loop on subframes */
1753
67.0k
   for (sub=0;sub<NB_NB_SUBFRAMES;sub++)
1754
53.6k
   {
1755
53.6k
      int offset;
1756
53.6k
      spx_word16_t *sp;
1757
1758
      /* Offset relative to start of frame */
1759
53.6k
      offset = NB_SUBFRAME_SIZE*sub;
1760
      /* Original signal */
1761
53.6k
      sp=out+offset;
1762
1763
      /* LSP interpolation (quantized and unquantized) */
1764
53.6k
      lsp_interpolate(st->old_qlsp, qlsp, interp_qlsp, NB_ORDER, sub, NB_NB_SUBFRAMES, LSP_MARGIN);
1765
1766
      /* Compute interpolated LPCs (unquantized) */
1767
53.6k
      lsp_to_lpc(interp_qlsp, ak, NB_ORDER, stack);
1768
1769
      /* Compute analysis filter at w=pi */
1770
53.6k
      {
1771
53.6k
         spx_word32_t pi_g=LPC_SCALING;
1772
321k
         for (i=0;i<NB_ORDER;i+=2)
1773
268k
         {
1774
            /*pi_g += -st->interp_qlpc[i] +  st->interp_qlpc[i+1];*/
1775
268k
            pi_g = ADD32(pi_g, SUB32(EXTEND32(ak[i+1]),EXTEND32(ak[i])));
1776
268k
         }
1777
53.6k
         st->pi_gain[sub] = pi_g;
1778
53.6k
      }
1779
1780
53.6k
      iir_mem16(sp, st->interp_qlpc, sp, NB_SUBFRAME_SIZE, NB_ORDER,
1781
53.6k
                st->mem_sp, stack);
1782
1783
589k
      for (i=0;i<NB_ORDER;i++)
1784
536k
         st->interp_qlpc[i] = ak[i];
1785
1786
53.6k
   }
1787
1788
13.4k
   if (st->highpass_enabled)
1789
13.4k
      highpass(out, out, NB_FRAME_SIZE, (st->isWideband?HIGHPASS_WIDEBAND:HIGHPASS_NARROWBAND)|HIGHPASS_OUTPUT, st->mem_hp);
1790
   /*for (i=0;i<NB_FRAME_SIZE;i++)
1791
     printf ("%d\n", (int)st->frame[i]);*/
1792
1793
   /* Tracking output level */
1794
13.4k
   st->level = 1+PSHR32(ol_gain,SIG_SHIFT);
1795
13.4k
   st->max_level = MAX16(MULT16_16_Q15(QCONST16(.99f,15), st->max_level), st->level);
1796
13.4k
   st->min_level = MIN16(ADD16(1,MULT16_16_Q14(QCONST16(1.01f,14), st->min_level)), st->level);
1797
13.4k
   if (st->max_level < st->min_level+1)
1798
4.11k
      st->max_level = st->min_level+1;
1799
   /*printf ("%f %f %f %d\n", og, st->min_level, st->max_level, update);*/
1800
1801
   /* Store the LSPs for interpolation in the next frame */
1802
147k
   for (i=0;i<NB_ORDER;i++)
1803
134k
      st->old_qlsp[i] = qlsp[i];
1804
1805
   /* The next frame will not be the first (Duh!) */
1806
13.4k
   st->first = 0;
1807
13.4k
   st->count_lost=0;
1808
13.4k
   st->last_pitch = best_pitch;
1809
13.4k
#ifdef FIXED_POINT
1810
13.4k
   st->last_pitch_gain = PSHR16(pitch_average,2);
1811
#else
1812
   st->last_pitch_gain = .25*pitch_average;
1813
#endif
1814
13.4k
   st->pitch_gain_buf[st->pitch_gain_buf_idx++] = st->last_pitch_gain;
1815
13.4k
   if (st->pitch_gain_buf_idx > 2) /* rollover */
1816
3.84k
      st->pitch_gain_buf_idx = 0;
1817
1818
13.4k
   st->last_ol_gain = ol_gain;
1819
1820
13.4k
   return 0;
1821
25.2k
}
1822
#endif /* DISABLE_DECODER */
1823