Coverage Report

Created: 2025-08-03 06:04

/src/speex/libspeex/nb_celp.c
Line
Count
Source (jump to first uncovered line)
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.28M
#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
217k
#define LSP_MARGIN 16
80
19.8k
#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
112k
#define LSP_MARGIN .002f
91
8.75k
#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.99k
{
102
7.99k
   EncState *st;
103
7.99k
   const SpeexNBMode *mode;
104
7.99k
   int i;
105
106
7.99k
   mode=(const SpeexNBMode *)m->mode;
107
7.99k
   st = (EncState*)speex_alloc(sizeof(EncState));
108
7.99k
   if (!st)
109
0
      return NULL;
110
7.99k
#if defined(VAR_ARRAYS) || defined (USE_ALLOCA)
111
7.99k
   st->stack = NULL;
112
#else
113
   st->stack = (char*)speex_alloc_scratch(NB_ENC_STACK);
114
#endif
115
116
7.99k
   st->mode=m;
117
118
7.99k
   st->gamma1=mode->gamma1;
119
7.99k
   st->gamma2=mode->gamma2;
120
7.99k
   st->lpc_floor = mode->lpc_floor;
121
122
7.99k
   st->submodes=mode->submodes;
123
7.99k
   st->submodeID=st->submodeSelect=mode->defaultSubmode;
124
7.99k
   st->bounded_pitch = 1;
125
126
7.99k
   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.99k
   st->cumul_gain = 1024;
136
137
7.99k
   st->window= lpc_window;
138
139
   /* Create the window for autocorrelation (lag-windowing) */
140
7.99k
   st->lagWindow = lag_window;
141
142
7.99k
   st->first = 1;
143
87.9k
   for (i=0;i<NB_ORDER;i++)
144
79.9k
      st->old_lsp[i]= DIV32(MULT16_16(QCONST16(3.1415927f, LSP_SHIFT), i+1), NB_ORDER+1);
145
146
7.99k
   st->innov_rms_save = NULL;
147
148
7.99k
#ifndef DISABLE_VBR
149
7.99k
   vbr_init(&st->vbr);
150
7.99k
   st->vbr_quality = 8;
151
7.99k
   st->vbr_enabled = 0;
152
7.99k
   st->vbr_max = 0;
153
7.99k
   st->vad_enabled = 0;
154
7.99k
   st->dtx_enabled = 0;
155
7.99k
   st->dtx_count=0;
156
7.99k
   st->abr_enabled = 0;
157
7.99k
   st->abr_drift = 0;
158
7.99k
   st->abr_drift2 = 0;
159
7.99k
#endif /* #ifndef DISABLE_VBR */
160
161
7.99k
   st->plc_tuning = 2;
162
7.99k
   st->complexity=2;
163
7.99k
   st->sampling_rate=8000;
164
7.99k
   st->isWideband = 0;
165
7.99k
   st->highpass_enabled = 1;
166
167
#ifdef ENABLE_VALGRIND
168
   VALGRIND_MAKE_MEM_DEFINED(st, NB_ENC_STACK);
169
#endif
170
7.99k
   return st;
171
7.99k
}
nb_encoder_init
Line
Count
Source
101
3.99k
{
102
3.99k
   EncState *st;
103
3.99k
   const SpeexNBMode *mode;
104
3.99k
   int i;
105
106
3.99k
   mode=(const SpeexNBMode *)m->mode;
107
3.99k
   st = (EncState*)speex_alloc(sizeof(EncState));
108
3.99k
   if (!st)
109
0
      return NULL;
110
3.99k
#if defined(VAR_ARRAYS) || defined (USE_ALLOCA)
111
3.99k
   st->stack = NULL;
112
#else
113
   st->stack = (char*)speex_alloc_scratch(NB_ENC_STACK);
114
#endif
115
116
3.99k
   st->mode=m;
117
118
3.99k
   st->gamma1=mode->gamma1;
119
3.99k
   st->gamma2=mode->gamma2;
120
3.99k
   st->lpc_floor = mode->lpc_floor;
121
122
3.99k
   st->submodes=mode->submodes;
123
3.99k
   st->submodeID=st->submodeSelect=mode->defaultSubmode;
124
3.99k
   st->bounded_pitch = 1;
125
126
3.99k
   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.99k
   st->cumul_gain = 1024;
136
137
3.99k
   st->window= lpc_window;
138
139
   /* Create the window for autocorrelation (lag-windowing) */
140
3.99k
   st->lagWindow = lag_window;
141
142
3.99k
   st->first = 1;
143
43.9k
   for (i=0;i<NB_ORDER;i++)
144
39.9k
      st->old_lsp[i]= DIV32(MULT16_16(QCONST16(3.1415927f, LSP_SHIFT), i+1), NB_ORDER+1);
145
146
3.99k
   st->innov_rms_save = NULL;
147
148
3.99k
#ifndef DISABLE_VBR
149
3.99k
   vbr_init(&st->vbr);
150
3.99k
   st->vbr_quality = 8;
151
3.99k
   st->vbr_enabled = 0;
152
3.99k
   st->vbr_max = 0;
153
3.99k
   st->vad_enabled = 0;
154
3.99k
   st->dtx_enabled = 0;
155
3.99k
   st->dtx_count=0;
156
3.99k
   st->abr_enabled = 0;
157
3.99k
   st->abr_drift = 0;
158
3.99k
   st->abr_drift2 = 0;
159
3.99k
#endif /* #ifndef DISABLE_VBR */
160
161
3.99k
   st->plc_tuning = 2;
162
3.99k
   st->complexity=2;
163
3.99k
   st->sampling_rate=8000;
164
3.99k
   st->isWideband = 0;
165
3.99k
   st->highpass_enabled = 1;
166
167
#ifdef ENABLE_VALGRIND
168
   VALGRIND_MAKE_MEM_DEFINED(st, NB_ENC_STACK);
169
#endif
170
3.99k
   return st;
171
3.99k
}
nb_encoder_init
Line
Count
Source
101
3.99k
{
102
3.99k
   EncState *st;
103
3.99k
   const SpeexNBMode *mode;
104
3.99k
   int i;
105
106
3.99k
   mode=(const SpeexNBMode *)m->mode;
107
3.99k
   st = (EncState*)speex_alloc(sizeof(EncState));
108
3.99k
   if (!st)
109
0
      return NULL;
110
3.99k
#if defined(VAR_ARRAYS) || defined (USE_ALLOCA)
111
3.99k
   st->stack = NULL;
112
#else
113
   st->stack = (char*)speex_alloc_scratch(NB_ENC_STACK);
114
#endif
115
116
3.99k
   st->mode=m;
117
118
3.99k
   st->gamma1=mode->gamma1;
119
3.99k
   st->gamma2=mode->gamma2;
120
3.99k
   st->lpc_floor = mode->lpc_floor;
121
122
3.99k
   st->submodes=mode->submodes;
123
3.99k
   st->submodeID=st->submodeSelect=mode->defaultSubmode;
124
3.99k
   st->bounded_pitch = 1;
125
126
3.99k
   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.99k
   st->cumul_gain = 1024;
136
137
3.99k
   st->window= lpc_window;
138
139
   /* Create the window for autocorrelation (lag-windowing) */
140
3.99k
   st->lagWindow = lag_window;
141
142
3.99k
   st->first = 1;
143
43.9k
   for (i=0;i<NB_ORDER;i++)
144
39.9k
      st->old_lsp[i]= DIV32(MULT16_16(QCONST16(3.1415927f, LSP_SHIFT), i+1), NB_ORDER+1);
145
146
3.99k
   st->innov_rms_save = NULL;
147
148
3.99k
#ifndef DISABLE_VBR
149
3.99k
   vbr_init(&st->vbr);
150
3.99k
   st->vbr_quality = 8;
151
3.99k
   st->vbr_enabled = 0;
152
3.99k
   st->vbr_max = 0;
153
3.99k
   st->vad_enabled = 0;
154
3.99k
   st->dtx_enabled = 0;
155
3.99k
   st->dtx_count=0;
156
3.99k
   st->abr_enabled = 0;
157
3.99k
   st->abr_drift = 0;
158
3.99k
   st->abr_drift2 = 0;
159
3.99k
#endif /* #ifndef DISABLE_VBR */
160
161
3.99k
   st->plc_tuning = 2;
162
3.99k
   st->complexity=2;
163
3.99k
   st->sampling_rate=8000;
164
3.99k
   st->isWideband = 0;
165
3.99k
   st->highpass_enabled = 1;
166
167
#ifdef ENABLE_VALGRIND
168
   VALGRIND_MAKE_MEM_DEFINED(st, NB_ENC_STACK);
169
#endif
170
3.99k
   return st;
171
3.99k
}
172
173
void nb_encoder_destroy(void *state)
174
3.99k
{
175
3.99k
   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.99k
#ifndef DISABLE_VBR
182
3.99k
   vbr_destroy(&st->vbr);
183
3.99k
#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.99k
   speex_free(st);
194
3.99k
}
195
196
197
int nb_encoder_ctl(void *state, int request, void *ptr)
198
618k
{
199
618k
   EncState *st;
200
618k
   st=(EncState*)state;
201
618k
   switch(request)
202
618k
   {
203
16.0k
   case SPEEX_GET_FRAME_SIZE:
204
16.0k
      (*(spx_int32_t*)ptr) = NB_FRAME_SIZE;
205
16.0k
      break;
206
0
   case SPEEX_SET_LOW_MODE:
207
85.1k
   case SPEEX_SET_MODE:
208
85.1k
      st->submodeSelect = st->submodeID = (*(spx_int32_t*)ptr);
209
85.1k
      break;
210
21.0k
   case SPEEX_GET_LOW_MODE:
211
21.0k
   case SPEEX_GET_MODE:
212
21.0k
      (*(spx_int32_t*)ptr) = st->submodeID;
213
21.0k
      break;
214
0
#ifndef DISABLE_VBR
215
4.96k
      case SPEEX_SET_VBR:
216
4.96k
      st->vbr_enabled = (*(spx_int32_t*)ptr);
217
4.96k
      break;
218
0
   case SPEEX_GET_VBR:
219
0
      (*(spx_int32_t*)ptr) = st->vbr_enabled;
220
0
      break;
221
3.65k
   case SPEEX_SET_VAD:
222
3.65k
      st->vad_enabled = (*(spx_int32_t*)ptr);
223
3.65k
      break;
224
0
   case SPEEX_GET_VAD:
225
0
      (*(spx_int32_t*)ptr) = st->vad_enabled;
226
0
      break;
227
3.50k
   case SPEEX_SET_DTX:
228
3.50k
      st->dtx_enabled = (*(spx_int32_t*)ptr);
229
3.50k
      break;
230
0
   case SPEEX_GET_DTX:
231
0
      (*(spx_int32_t*)ptr) = st->dtx_enabled;
232
0
      break;
233
1.09k
   case SPEEX_SET_ABR:
234
1.09k
      st->abr_enabled = (*(spx_int32_t*)ptr);
235
1.09k
      st->vbr_enabled = st->abr_enabled!=0;
236
1.09k
      if (st->vbr_enabled)
237
1.09k
      {
238
1.09k
         spx_int32_t i=10;
239
1.09k
         spx_int32_t rate, target;
240
1.09k
         float vbr_qual;
241
1.09k
         target = (*(spx_int32_t*)ptr);
242
13.0k
         while (i>=0)
243
11.9k
         {
244
11.9k
            speex_encoder_ctl(st, SPEEX_SET_QUALITY, &i);
245
11.9k
            speex_encoder_ctl(st, SPEEX_GET_BITRATE, &rate);
246
11.9k
            if (rate <= target)
247
0
               break;
248
11.9k
            i--;
249
11.9k
         }
250
1.09k
         vbr_qual=i;
251
1.09k
         if (vbr_qual<0)
252
1.09k
            vbr_qual=0;
253
1.09k
         speex_encoder_ctl(st, SPEEX_SET_VBR_QUALITY, &vbr_qual);
254
1.09k
         st->abr_count=0;
255
1.09k
         st->abr_drift=0;
256
1.09k
         st->abr_drift2=0;
257
1.09k
      }
258
259
1.09k
      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
6.05k
   case SPEEX_SET_VBR_QUALITY:
266
6.05k
      st->vbr_quality = (*(float*)ptr);
267
6.05k
      if (st->vbr_quality < 0)
268
0
          st->vbr_quality = 0;
269
6.05k
      else if (st->vbr_quality > 10)
270
0
          st->vbr_quality = 10;
271
6.05k
      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
140k
   case SPEEX_SET_QUALITY:
277
140k
      {
278
140k
         int quality = (*(spx_int32_t*)ptr);
279
140k
         if (quality < 0)
280
0
            quality = 0;
281
140k
         if (quality > 10)
282
0
            quality = 10;
283
140k
         st->submodeSelect = st->submodeID = ((const SpeexNBMode*)(st->mode->mode))->quality_map[quality];
284
140k
      }
285
140k
      break;
286
7.99k
   case SPEEX_SET_COMPLEXITY:
287
7.99k
      st->complexity = (*(spx_int32_t*)ptr);
288
7.99k
      if (st->complexity<0)
289
0
         st->complexity=0;
290
7.99k
      break;
291
0
   case SPEEX_GET_COMPLEXITY:
292
0
      (*(spx_int32_t*)ptr) = st->complexity;
293
0
      break;
294
13.0k
   case SPEEX_SET_BITRATE:
295
13.0k
      {
296
13.0k
         spx_int32_t i=10;
297
13.0k
         spx_int32_t rate, target;
298
13.0k
         target = (*(spx_int32_t*)ptr);
299
128k
         while (i>=0)
300
122k
         {
301
122k
            speex_encoder_ctl(st, SPEEX_SET_QUALITY, &i);
302
122k
            speex_encoder_ctl(st, SPEEX_GET_BITRATE, &rate);
303
122k
            if (rate <= target)
304
6.95k
               break;
305
115k
            i--;
306
115k
         }
307
13.0k
      }
308
13.0k
      break;
309
218k
   case SPEEX_GET_BITRATE:
310
218k
      if (st->submodes[st->submodeID])
311
213k
         (*(spx_int32_t*)ptr) = st->sampling_rate*SUBMODE(bits_per_frame)/NB_FRAME_SIZE;
312
5.16k
      else
313
5.16k
         (*(spx_int32_t*)ptr) = st->sampling_rate*(NB_SUBMODE_BITS+1)/NB_FRAME_SIZE;
314
218k
      break;
315
7.99k
   case SPEEX_SET_SAMPLING_RATE:
316
7.99k
      st->sampling_rate = (*(spx_int32_t*)ptr);
317
7.99k
      break;
318
3.90k
   case SPEEX_GET_SAMPLING_RATE:
319
3.90k
      (*(spx_int32_t*)ptr)=st->sampling_rate;
320
3.90k
      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.99k
   case SPEEX_GET_LOOKAHEAD:
343
7.99k
      (*(spx_int32_t*)ptr)=(NB_WINDOW_SIZE-NB_FRAME_SIZE);
344
7.99k
      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.58k
   case SPEEX_SET_VBR_MAX_BITRATE:
355
3.58k
      st->vbr_max = (*(spx_int32_t*)ptr);
356
3.58k
      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.99k
   case SPEEX_SET_HIGHPASS:
362
7.99k
      st->highpass_enabled = (*(spx_int32_t*)ptr);
363
7.99k
      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
16.3k
   case SPEEX_GET_PI_GAIN:
370
16.3k
      {
371
16.3k
         int i;
372
16.3k
         spx_word32_t *g = (spx_word32_t*)ptr;
373
81.8k
         for (i=0;i<NB_NB_SUBFRAMES;i++)
374
65.4k
            g[i]=st->pi_gain[i];
375
16.3k
      }
376
16.3k
      break;
377
16.3k
   case SPEEX_GET_EXC:
378
16.3k
      {
379
16.3k
         int i;
380
81.8k
         for (i=0;i<NB_NB_SUBFRAMES;i++)
381
65.4k
            ((spx_word16_t*)ptr)[i] = compute_rms16(st->exc+i*NB_SUBFRAME_SIZE, NB_SUBFRAME_SIZE);
382
16.3k
      }
383
16.3k
      break;
384
0
#ifndef DISABLE_VBR
385
11.3k
   case SPEEX_GET_RELATIVE_QUALITY:
386
11.3k
      (*(float*)ptr)=st->relative_quality;
387
11.3k
      break;
388
0
#endif /* #ifndef DISABLE_VBR */
389
16.3k
   case SPEEX_SET_INNOVATION_SAVE:
390
16.3k
      st->innov_rms_save = (spx_word16_t*)ptr;
391
16.3k
      break;
392
5.46k
   case SPEEX_SET_WIDEBAND:
393
5.46k
      st->isWideband = *((spx_int32_t*)ptr);
394
5.46k
      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
618k
   }
402
618k
   return 0;
403
618k
}
nb_encoder_ctl
Line
Count
Source
198
309k
{
199
309k
   EncState *st;
200
309k
   st=(EncState*)state;
201
309k
   switch(request)
202
309k
   {
203
8.00k
   case SPEEX_GET_FRAME_SIZE:
204
8.00k
      (*(spx_int32_t*)ptr) = NB_FRAME_SIZE;
205
8.00k
      break;
206
0
   case SPEEX_SET_LOW_MODE:
207
42.5k
   case SPEEX_SET_MODE:
208
42.5k
      st->submodeSelect = st->submodeID = (*(spx_int32_t*)ptr);
209
42.5k
      break;
210
10.5k
   case SPEEX_GET_LOW_MODE:
211
10.5k
   case SPEEX_GET_MODE:
212
10.5k
      (*(spx_int32_t*)ptr) = st->submodeID;
213
10.5k
      break;
214
0
#ifndef DISABLE_VBR
215
2.48k
      case SPEEX_SET_VBR:
216
2.48k
      st->vbr_enabled = (*(spx_int32_t*)ptr);
217
2.48k
      break;
218
0
   case SPEEX_GET_VBR:
219
0
      (*(spx_int32_t*)ptr) = st->vbr_enabled;
220
0
      break;
221
1.82k
   case SPEEX_SET_VAD:
222
1.82k
      st->vad_enabled = (*(spx_int32_t*)ptr);
223
1.82k
      break;
224
0
   case SPEEX_GET_VAD:
225
0
      (*(spx_int32_t*)ptr) = st->vad_enabled;
226
0
      break;
227
1.75k
   case SPEEX_SET_DTX:
228
1.75k
      st->dtx_enabled = (*(spx_int32_t*)ptr);
229
1.75k
      break;
230
0
   case SPEEX_GET_DTX:
231
0
      (*(spx_int32_t*)ptr) = st->dtx_enabled;
232
0
      break;
233
545
   case SPEEX_SET_ABR:
234
545
      st->abr_enabled = (*(spx_int32_t*)ptr);
235
545
      st->vbr_enabled = st->abr_enabled!=0;
236
545
      if (st->vbr_enabled)
237
545
      {
238
545
         spx_int32_t i=10;
239
545
         spx_int32_t rate, target;
240
545
         float vbr_qual;
241
545
         target = (*(spx_int32_t*)ptr);
242
6.54k
         while (i>=0)
243
5.99k
         {
244
5.99k
            speex_encoder_ctl(st, SPEEX_SET_QUALITY, &i);
245
5.99k
            speex_encoder_ctl(st, SPEEX_GET_BITRATE, &rate);
246
5.99k
            if (rate <= target)
247
0
               break;
248
5.99k
            i--;
249
5.99k
         }
250
545
         vbr_qual=i;
251
545
         if (vbr_qual<0)
252
545
            vbr_qual=0;
253
545
         speex_encoder_ctl(st, SPEEX_SET_VBR_QUALITY, &vbr_qual);
254
545
         st->abr_count=0;
255
545
         st->abr_drift=0;
256
545
         st->abr_drift2=0;
257
545
      }
258
259
545
      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
3.02k
   case SPEEX_SET_VBR_QUALITY:
266
3.02k
      st->vbr_quality = (*(float*)ptr);
267
3.02k
      if (st->vbr_quality < 0)
268
0
          st->vbr_quality = 0;
269
3.02k
      else if (st->vbr_quality > 10)
270
0
          st->vbr_quality = 10;
271
3.02k
      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
70.1k
   case SPEEX_SET_QUALITY:
277
70.1k
      {
278
70.1k
         int quality = (*(spx_int32_t*)ptr);
279
70.1k
         if (quality < 0)
280
0
            quality = 0;
281
70.1k
         if (quality > 10)
282
0
            quality = 10;
283
70.1k
         st->submodeSelect = st->submodeID = ((const SpeexNBMode*)(st->mode->mode))->quality_map[quality];
284
70.1k
      }
285
70.1k
      break;
286
3.99k
   case SPEEX_SET_COMPLEXITY:
287
3.99k
      st->complexity = (*(spx_int32_t*)ptr);
288
3.99k
      if (st->complexity<0)
289
0
         st->complexity=0;
290
3.99k
      break;
291
0
   case SPEEX_GET_COMPLEXITY:
292
0
      (*(spx_int32_t*)ptr) = st->complexity;
293
0
      break;
294
6.53k
   case SPEEX_SET_BITRATE:
295
6.53k
      {
296
6.53k
         spx_int32_t i=10;
297
6.53k
         spx_int32_t rate, target;
298
6.53k
         target = (*(spx_int32_t*)ptr);
299
64.2k
         while (i>=0)
300
61.2k
         {
301
61.2k
            speex_encoder_ctl(st, SPEEX_SET_QUALITY, &i);
302
61.2k
            speex_encoder_ctl(st, SPEEX_GET_BITRATE, &rate);
303
61.2k
            if (rate <= target)
304
3.47k
               break;
305
57.7k
            i--;
306
57.7k
         }
307
6.53k
      }
308
6.53k
      break;
309
109k
   case SPEEX_GET_BITRATE:
310
109k
      if (st->submodes[st->submodeID])
311
106k
         (*(spx_int32_t*)ptr) = st->sampling_rate*SUBMODE(bits_per_frame)/NB_FRAME_SIZE;
312
2.58k
      else
313
2.58k
         (*(spx_int32_t*)ptr) = st->sampling_rate*(NB_SUBMODE_BITS+1)/NB_FRAME_SIZE;
314
109k
      break;
315
3.99k
   case SPEEX_SET_SAMPLING_RATE:
316
3.99k
      st->sampling_rate = (*(spx_int32_t*)ptr);
317
3.99k
      break;
318
1.95k
   case SPEEX_GET_SAMPLING_RATE:
319
1.95k
      (*(spx_int32_t*)ptr)=st->sampling_rate;
320
1.95k
      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.99k
   case SPEEX_GET_LOOKAHEAD:
343
3.99k
      (*(spx_int32_t*)ptr)=(NB_WINDOW_SIZE-NB_FRAME_SIZE);
344
3.99k
      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.79k
   case SPEEX_SET_VBR_MAX_BITRATE:
355
1.79k
      st->vbr_max = (*(spx_int32_t*)ptr);
356
1.79k
      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.99k
   case SPEEX_SET_HIGHPASS:
362
3.99k
      st->highpass_enabled = (*(spx_int32_t*)ptr);
363
3.99k
      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
8.18k
   case SPEEX_GET_PI_GAIN:
370
8.18k
      {
371
8.18k
         int i;
372
8.18k
         spx_word32_t *g = (spx_word32_t*)ptr;
373
40.9k
         for (i=0;i<NB_NB_SUBFRAMES;i++)
374
32.7k
            g[i]=st->pi_gain[i];
375
8.18k
      }
376
8.18k
      break;
377
8.18k
   case SPEEX_GET_EXC:
378
8.18k
      {
379
8.18k
         int i;
380
40.9k
         for (i=0;i<NB_NB_SUBFRAMES;i++)
381
32.7k
            ((spx_word16_t*)ptr)[i] = compute_rms16(st->exc+i*NB_SUBFRAME_SIZE, NB_SUBFRAME_SIZE);
382
8.18k
      }
383
8.18k
      break;
384
0
#ifndef DISABLE_VBR
385
5.65k
   case SPEEX_GET_RELATIVE_QUALITY:
386
5.65k
      (*(float*)ptr)=st->relative_quality;
387
5.65k
      break;
388
0
#endif /* #ifndef DISABLE_VBR */
389
8.18k
   case SPEEX_SET_INNOVATION_SAVE:
390
8.18k
      st->innov_rms_save = (spx_word16_t*)ptr;
391
8.18k
      break;
392
2.73k
   case SPEEX_SET_WIDEBAND:
393
2.73k
      st->isWideband = *((spx_int32_t*)ptr);
394
2.73k
      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
309k
   }
402
309k
   return 0;
403
309k
}
nb_encoder_ctl
Line
Count
Source
198
309k
{
199
309k
   EncState *st;
200
309k
   st=(EncState*)state;
201
309k
   switch(request)
202
309k
   {
203
8.00k
   case SPEEX_GET_FRAME_SIZE:
204
8.00k
      (*(spx_int32_t*)ptr) = NB_FRAME_SIZE;
205
8.00k
      break;
206
0
   case SPEEX_SET_LOW_MODE:
207
42.5k
   case SPEEX_SET_MODE:
208
42.5k
      st->submodeSelect = st->submodeID = (*(spx_int32_t*)ptr);
209
42.5k
      break;
210
10.5k
   case SPEEX_GET_LOW_MODE:
211
10.5k
   case SPEEX_GET_MODE:
212
10.5k
      (*(spx_int32_t*)ptr) = st->submodeID;
213
10.5k
      break;
214
0
#ifndef DISABLE_VBR
215
2.48k
      case SPEEX_SET_VBR:
216
2.48k
      st->vbr_enabled = (*(spx_int32_t*)ptr);
217
2.48k
      break;
218
0
   case SPEEX_GET_VBR:
219
0
      (*(spx_int32_t*)ptr) = st->vbr_enabled;
220
0
      break;
221
1.82k
   case SPEEX_SET_VAD:
222
1.82k
      st->vad_enabled = (*(spx_int32_t*)ptr);
223
1.82k
      break;
224
0
   case SPEEX_GET_VAD:
225
0
      (*(spx_int32_t*)ptr) = st->vad_enabled;
226
0
      break;
227
1.75k
   case SPEEX_SET_DTX:
228
1.75k
      st->dtx_enabled = (*(spx_int32_t*)ptr);
229
1.75k
      break;
230
0
   case SPEEX_GET_DTX:
231
0
      (*(spx_int32_t*)ptr) = st->dtx_enabled;
232
0
      break;
233
545
   case SPEEX_SET_ABR:
234
545
      st->abr_enabled = (*(spx_int32_t*)ptr);
235
545
      st->vbr_enabled = st->abr_enabled!=0;
236
545
      if (st->vbr_enabled)
237
545
      {
238
545
         spx_int32_t i=10;
239
545
         spx_int32_t rate, target;
240
545
         float vbr_qual;
241
545
         target = (*(spx_int32_t*)ptr);
242
6.54k
         while (i>=0)
243
5.99k
         {
244
5.99k
            speex_encoder_ctl(st, SPEEX_SET_QUALITY, &i);
245
5.99k
            speex_encoder_ctl(st, SPEEX_GET_BITRATE, &rate);
246
5.99k
            if (rate <= target)
247
0
               break;
248
5.99k
            i--;
249
5.99k
         }
250
545
         vbr_qual=i;
251
545
         if (vbr_qual<0)
252
545
            vbr_qual=0;
253
545
         speex_encoder_ctl(st, SPEEX_SET_VBR_QUALITY, &vbr_qual);
254
545
         st->abr_count=0;
255
545
         st->abr_drift=0;
256
545
         st->abr_drift2=0;
257
545
      }
258
259
545
      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
3.02k
   case SPEEX_SET_VBR_QUALITY:
266
3.02k
      st->vbr_quality = (*(float*)ptr);
267
3.02k
      if (st->vbr_quality < 0)
268
0
          st->vbr_quality = 0;
269
3.02k
      else if (st->vbr_quality > 10)
270
0
          st->vbr_quality = 10;
271
3.02k
      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
70.1k
   case SPEEX_SET_QUALITY:
277
70.1k
      {
278
70.1k
         int quality = (*(spx_int32_t*)ptr);
279
70.1k
         if (quality < 0)
280
0
            quality = 0;
281
70.1k
         if (quality > 10)
282
0
            quality = 10;
283
70.1k
         st->submodeSelect = st->submodeID = ((const SpeexNBMode*)(st->mode->mode))->quality_map[quality];
284
70.1k
      }
285
70.1k
      break;
286
3.99k
   case SPEEX_SET_COMPLEXITY:
287
3.99k
      st->complexity = (*(spx_int32_t*)ptr);
288
3.99k
      if (st->complexity<0)
289
0
         st->complexity=0;
290
3.99k
      break;
291
0
   case SPEEX_GET_COMPLEXITY:
292
0
      (*(spx_int32_t*)ptr) = st->complexity;
293
0
      break;
294
6.53k
   case SPEEX_SET_BITRATE:
295
6.53k
      {
296
6.53k
         spx_int32_t i=10;
297
6.53k
         spx_int32_t rate, target;
298
6.53k
         target = (*(spx_int32_t*)ptr);
299
64.2k
         while (i>=0)
300
61.2k
         {
301
61.2k
            speex_encoder_ctl(st, SPEEX_SET_QUALITY, &i);
302
61.2k
            speex_encoder_ctl(st, SPEEX_GET_BITRATE, &rate);
303
61.2k
            if (rate <= target)
304
3.47k
               break;
305
57.7k
            i--;
306
57.7k
         }
307
6.53k
      }
308
6.53k
      break;
309
109k
   case SPEEX_GET_BITRATE:
310
109k
      if (st->submodes[st->submodeID])
311
106k
         (*(spx_int32_t*)ptr) = st->sampling_rate*SUBMODE(bits_per_frame)/NB_FRAME_SIZE;
312
2.58k
      else
313
2.58k
         (*(spx_int32_t*)ptr) = st->sampling_rate*(NB_SUBMODE_BITS+1)/NB_FRAME_SIZE;
314
109k
      break;
315
3.99k
   case SPEEX_SET_SAMPLING_RATE:
316
3.99k
      st->sampling_rate = (*(spx_int32_t*)ptr);
317
3.99k
      break;
318
1.95k
   case SPEEX_GET_SAMPLING_RATE:
319
1.95k
      (*(spx_int32_t*)ptr)=st->sampling_rate;
320
1.95k
      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.99k
   case SPEEX_GET_LOOKAHEAD:
343
3.99k
      (*(spx_int32_t*)ptr)=(NB_WINDOW_SIZE-NB_FRAME_SIZE);
344
3.99k
      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.79k
   case SPEEX_SET_VBR_MAX_BITRATE:
355
1.79k
      st->vbr_max = (*(spx_int32_t*)ptr);
356
1.79k
      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.99k
   case SPEEX_SET_HIGHPASS:
362
3.99k
      st->highpass_enabled = (*(spx_int32_t*)ptr);
363
3.99k
      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
8.18k
   case SPEEX_GET_PI_GAIN:
370
8.18k
      {
371
8.18k
         int i;
372
8.18k
         spx_word32_t *g = (spx_word32_t*)ptr;
373
40.9k
         for (i=0;i<NB_NB_SUBFRAMES;i++)
374
32.7k
            g[i]=st->pi_gain[i];
375
8.18k
      }
376
8.18k
      break;
377
8.18k
   case SPEEX_GET_EXC:
378
8.18k
      {
379
8.18k
         int i;
380
40.9k
         for (i=0;i<NB_NB_SUBFRAMES;i++)
381
32.7k
            ((spx_word16_t*)ptr)[i] = compute_rms16(st->exc+i*NB_SUBFRAME_SIZE, NB_SUBFRAME_SIZE);
382
8.18k
      }
383
8.18k
      break;
384
0
#ifndef DISABLE_VBR
385
5.65k
   case SPEEX_GET_RELATIVE_QUALITY:
386
5.65k
      (*(float*)ptr)=st->relative_quality;
387
5.65k
      break;
388
0
#endif /* #ifndef DISABLE_VBR */
389
8.18k
   case SPEEX_SET_INNOVATION_SAVE:
390
8.18k
      st->innov_rms_save = (spx_word16_t*)ptr;
391
8.18k
      break;
392
2.73k
   case SPEEX_SET_WIDEBAND:
393
2.73k
      st->isWideband = *((spx_int32_t*)ptr);
394
2.73k
      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
309k
   }
402
309k
   return 0;
403
309k
}
404
405
406
int nb_encode(void *state, void *vin, SpeexBits *bits)
407
28.6k
{
408
28.6k
   EncState *st;
409
28.6k
   int i, sub, roots;
410
28.6k
   int ol_pitch;
411
28.6k
   spx_word16_t ol_pitch_coef;
412
28.6k
   spx_word32_t ol_gain;
413
28.6k
   VARDECL(spx_word16_t *target);
414
28.6k
   VARDECL(spx_sig_t *innov);
415
28.6k
   VARDECL(spx_word32_t *exc32);
416
28.6k
   VARDECL(spx_mem_t *mem);
417
28.6k
   VARDECL(spx_coef_t *bw_lpc1);
418
28.6k
   VARDECL(spx_coef_t *bw_lpc2);
419
28.6k
   VARDECL(spx_coef_t *lpc);
420
28.6k
   VARDECL(spx_lsp_t *lsp);
421
28.6k
   VARDECL(spx_lsp_t *qlsp);
422
28.6k
   VARDECL(spx_lsp_t *interp_lsp);
423
28.6k
   VARDECL(spx_lsp_t *interp_qlsp);
424
28.6k
   VARDECL(spx_coef_t *interp_lpc);
425
28.6k
   VARDECL(spx_coef_t *interp_qlpc);
426
28.6k
   char *stack;
427
28.6k
   VARDECL(spx_word16_t *syn_resp);
428
429
28.6k
   spx_word32_t ener=0;
430
28.6k
   spx_word16_t fine_gain;
431
28.6k
   spx_word16_t *in = (spx_word16_t*)vin;
432
433
28.6k
   st=(EncState *)state;
434
28.6k
   stack=st->stack;
435
436
28.6k
   ALLOC(lpc, NB_ORDER, spx_coef_t);
437
28.6k
   ALLOC(bw_lpc1, NB_ORDER, spx_coef_t);
438
28.6k
   ALLOC(bw_lpc2, NB_ORDER, spx_coef_t);
439
28.6k
   ALLOC(lsp, NB_ORDER, spx_lsp_t);
440
28.6k
   ALLOC(qlsp, NB_ORDER, spx_lsp_t);
441
28.6k
   ALLOC(interp_lsp, NB_ORDER, spx_lsp_t);
442
28.6k
   ALLOC(interp_qlsp, NB_ORDER, spx_lsp_t);
443
28.6k
   ALLOC(interp_lpc, NB_ORDER, spx_coef_t);
444
28.6k
   ALLOC(interp_qlpc, NB_ORDER, spx_coef_t);
445
446
28.6k
   st->exc = st->excBuf + NB_PITCH_END + 2;
447
28.6k
   st->sw = st->swBuf + NB_PITCH_END + 2;
448
   /* Move signals 1 frame towards the past */
449
28.6k
   SPEEX_MOVE(st->excBuf, st->excBuf+NB_FRAME_SIZE, NB_PITCH_END+2);
450
28.6k
   SPEEX_MOVE(st->swBuf, st->swBuf+NB_FRAME_SIZE, NB_PITCH_END+2);
451
452
28.6k
   if (st->highpass_enabled)
453
11.8k
      highpass(in, in, NB_FRAME_SIZE, (st->isWideband?HIGHPASS_WIDEBAND:HIGHPASS_NARROWBAND)|HIGHPASS_INPUT, st->mem_hp);
454
455
28.6k
   {
456
28.6k
      VARDECL(spx_word16_t *w_sig);
457
28.6k
      VARDECL(spx_word16_t *autocorr);
458
28.6k
      ALLOC(w_sig, NB_WINDOW_SIZE, spx_word16_t);
459
28.6k
      ALLOC(autocorr, NB_ORDER+1, spx_word16_t);
460
      /* Window for analysis */
461
1.17M
      for (i=0;i<NB_WINDOW_SIZE-NB_FRAME_SIZE;i++)
462
1.14M
         w_sig[i] = MULT16_16_Q15(st->winBuf[i],st->window[i]);
463
4.61M
      for (;i<NB_WINDOW_SIZE;i++)
464
4.58M
         w_sig[i] = MULT16_16_Q15(in[i-NB_WINDOW_SIZE+NB_FRAME_SIZE],st->window[i]);
465
      /* Compute auto-correlation */
466
28.6k
      _spx_autocorr(w_sig, autocorr, NB_ORDER+1, NB_WINDOW_SIZE);
467
28.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
343k
      for (i=0;i<NB_ORDER+1;i++)
471
315k
         autocorr[i] = MULT16_16_Q15(autocorr[i],st->lagWindow[i]);
472
28.6k
      autocorr[0] = ADD16(autocorr[0],1);
473
474
      /* Levinson-Durbin */
475
28.6k
      _spx_lpc(lpc, autocorr, NB_ORDER);
476
      /* LPC to LSPs (x-domain) transform */
477
28.6k
      roots=lpc_to_lsp (lpc, NB_ORDER, lsp, 10, LSP_DELTA1, stack);
478
      /* Check if we found all the roots */
479
28.6k
      if (roots!=NB_ORDER)
480
66
      {
481
         /*If we can't find all LSP's, do some damage control and use previous filter*/
482
726
         for (i=0;i<NB_ORDER;i++)
483
660
         {
484
660
            lsp[i]=st->old_lsp[i];
485
660
         }
486
66
      }
487
28.6k
   }
488
489
490
491
492
   /* Whole frame analysis (open-loop estimation of pitch and excitation gain) */
493
28.6k
   {
494
28.6k
      int diff = NB_WINDOW_SIZE-NB_FRAME_SIZE;
495
28.6k
      if (st->first)
496
63.5k
         for (i=0;i<NB_ORDER;i++)
497
57.7k
            interp_lsp[i] = lsp[i];
498
22.8k
      else
499
22.8k
         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
28.6k
      lsp_to_lpc(interp_lsp, interp_lpc, NB_ORDER,stack);
503
504
505
      /*Open-loop pitch*/
506
28.6k
      if (!st->submodes[st->submodeID] || (st->complexity>2 && SUBMODE(have_subframe_gain)<3) || SUBMODE(forced_pitch_gain) || SUBMODE(lbr_pitch) != -1
507
28.6k
#ifndef DISABLE_VBR
508
28.6k
           || st->vbr_enabled || st->vad_enabled
509
28.6k
#endif
510
28.6k
                  )
511
26.4k
      {
512
26.4k
         int nol_pitch[6];
513
26.4k
         spx_word16_t nol_pitch_coef[6];
514
515
26.4k
         bw_lpc(QCONST16(0.9,15), interp_lpc, bw_lpc1, NB_ORDER);
516
26.4k
         bw_lpc(QCONST16(0.55,15), interp_lpc, bw_lpc2, NB_ORDER);
517
518
26.4k
         SPEEX_COPY(st->sw, st->winBuf, diff);
519
26.4k
         SPEEX_COPY(st->sw+diff, in, NB_FRAME_SIZE-diff);
520
26.4k
         filter10(st->sw, bw_lpc1, bw_lpc2, st->sw, NB_FRAME_SIZE, st->mem_sw_whole, stack);
521
522
26.4k
         open_loop_nbest_pitch(st->sw, NB_PITCH_START, NB_PITCH_END, NB_FRAME_SIZE,
523
26.4k
                               nol_pitch, nol_pitch_coef, 6, stack);
524
26.4k
         ol_pitch=nol_pitch[0];
525
26.4k
         ol_pitch_coef = nol_pitch_coef[0];
526
         /*Try to remove pitch multiples*/
527
158k
         for (i=1;i<6;i++)
528
132k
         {
529
#ifdef FIXED_POINT
530
91.7k
            if ((nol_pitch_coef[i]>MULT16_16_Q15(nol_pitch_coef[0],27853)) &&
531
#else
532
40.5k
            if ((nol_pitch_coef[i]>.85*nol_pitch_coef[0]) &&
533
#endif
534
40.5k
                (ABS(2*nol_pitch[i]-ol_pitch)<=2 || ABS(3*nol_pitch[i]-ol_pitch)<=3 ||
535
30.1k
                 ABS(4*nol_pitch[i]-ol_pitch)<=4 || ABS(5*nol_pitch[i]-ol_pitch)<=5))
536
1.14k
            {
537
               /*ol_pitch_coef=nol_pitch_coef[i];*/
538
1.14k
               ol_pitch = nol_pitch[i];
539
1.14k
            }
540
132k
         }
541
         /*if (ol_pitch>50)
542
           ol_pitch/=2;*/
543
         /*ol_pitch_coef = sqrt(ol_pitch_coef);*/
544
545
26.4k
      } else {
546
2.18k
         ol_pitch=0;
547
2.18k
         ol_pitch_coef=0;
548
2.18k
      }
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
28.6k
      fir_mem16(st->winBuf, interp_lpc, st->exc, diff, NB_ORDER, st->mem_exc, stack);
554
28.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
28.6k
      {
558
28.6k
         spx_word16_t g = compute_rms16(st->exc, NB_FRAME_SIZE);
559
28.6k
         if (st->submodeID!=1 && ol_pitch>0)
560
13.1k
            ol_gain = MULT16_16(g, MULT16_16_Q14(QCONST16(1.1,14),
561
28.6k
                                spx_sqrt(QCONST32(1.,28)-MULT16_32_Q15(QCONST16(.8,15),SHL32(MULT16_16(ol_pitch_coef,ol_pitch_coef),16)))));
562
15.5k
         else
563
15.5k
            ol_gain = SHL32(EXTEND32(g),SIG_SHIFT);
564
28.6k
      }
565
28.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
28.6k
#ifndef DISABLE_VBR
578
28.6k
   if (st->vbr_enabled||st->vad_enabled)
579
21.9k
   {
580
21.9k
      float lsp_dist=0;
581
241k
      for (i=0;i<NB_ORDER;i++)
582
219k
         lsp_dist += (st->old_lsp[i] - lsp[i])*(st->old_lsp[i] - lsp[i]);
583
21.9k
      lsp_dist /= LSP_SCALING*LSP_SCALING;
584
585
21.9k
      if (st->abr_enabled)
586
5.45k
      {
587
5.45k
         float qual_change=0;
588
5.45k
         if (st->abr_drift2 * st->abr_drift > 0)
589
4.90k
         {
590
            /* Only adapt if long-term and short-term drift are the same sign */
591
4.90k
            qual_change = -.00001*st->abr_drift/(1+st->abr_count);
592
4.90k
            if (qual_change>.05)
593
0
               qual_change=.05;
594
4.90k
            if (qual_change<-.05)
595
1.34k
               qual_change=-.05;
596
4.90k
         }
597
5.45k
         st->vbr_quality += qual_change;
598
5.45k
         if (st->vbr_quality>10)
599
0
            st->vbr_quality=10;
600
5.45k
         if (st->vbr_quality<0)
601
4.90k
            st->vbr_quality=0;
602
5.45k
      }
603
604
21.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
21.9k
      if (st->vbr_enabled)
608
17.3k
      {
609
17.3k
         spx_int32_t mode;
610
17.3k
         int choice=0;
611
17.3k
         float min_diff=100;
612
17.3k
         mode = 8;
613
155k
         while (mode)
614
138k
         {
615
138k
            int v1;
616
138k
            float thresh;
617
138k
            v1=(int)floor(st->vbr_quality);
618
138k
            if (v1==10)
619
8.30k
               thresh = vbr_nb_thresh[mode][v1];
620
130k
            else
621
130k
               thresh = (st->vbr_quality-v1)*vbr_nb_thresh[mode][v1+1] + (1+v1-st->vbr_quality)*vbr_nb_thresh[mode][v1];
622
138k
            if (st->relative_quality > thresh &&
623
138k
                st->relative_quality-thresh<min_diff)
624
18.2k
            {
625
18.2k
               choice = mode;
626
18.2k
               min_diff = st->relative_quality-thresh;
627
18.2k
            }
628
138k
            mode--;
629
138k
         }
630
17.3k
         mode=choice;
631
17.3k
         if (mode==0)
632
4.81k
         {
633
4.81k
            if (st->dtx_count==0 || lsp_dist>.05 || !st->dtx_enabled || st->dtx_count>20)
634
2.83k
            {
635
2.83k
               mode=1;
636
2.83k
               st->dtx_count=1;
637
2.83k
            } else {
638
1.98k
               mode=0;
639
1.98k
               st->dtx_count++;
640
1.98k
            }
641
12.5k
         } else {
642
12.5k
            st->dtx_count=0;
643
12.5k
         }
644
645
17.3k
         speex_encoder_ctl(state, SPEEX_SET_MODE, &mode);
646
17.3k
         if (st->vbr_max>0)
647
12.5k
         {
648
12.5k
            spx_int32_t rate;
649
12.5k
            speex_encoder_ctl(state, SPEEX_GET_BITRATE, &rate);
650
12.5k
            if (rate > st->vbr_max)
651
4.48k
            {
652
4.48k
               rate = st->vbr_max;
653
4.48k
               speex_encoder_ctl(state, SPEEX_SET_BITRATE, &rate);
654
4.48k
            }
655
12.5k
         }
656
657
17.3k
         if (st->abr_enabled)
658
5.45k
         {
659
5.45k
            spx_int32_t bitrate;
660
5.45k
            speex_encoder_ctl(state, SPEEX_GET_BITRATE, &bitrate);
661
5.45k
            st->abr_drift+=(bitrate-st->abr_enabled);
662
5.45k
            st->abr_drift2 = .95*st->abr_drift2 + .05*(bitrate-st->abr_enabled);
663
5.45k
            st->abr_count += 1.0;
664
5.45k
         }
665
666
17.3k
      } else {
667
         /*VAD only case*/
668
4.62k
         int mode;
669
4.62k
         if (st->relative_quality<2)
670
1.39k
         {
671
1.39k
            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
532
               mode=0;
677
532
               st->dtx_count++;
678
532
            }
679
3.22k
         } else {
680
3.22k
            st->dtx_count = 0;
681
3.22k
            mode=st->submodeSelect;
682
3.22k
         }
683
         /*speex_encoder_ctl(state, SPEEX_SET_MODE, &mode);*/
684
4.62k
         st->submodeID=mode;
685
4.62k
      }
686
21.9k
   } else {
687
6.70k
      st->relative_quality = -1;
688
6.70k
   }
689
28.6k
#endif /* #ifndef DISABLE_VBR */
690
691
28.6k
   if (st->encode_submode)
692
28.6k
   {
693
      /* First, transmit a zero for narrowband */
694
28.6k
      speex_bits_pack(bits, 0, 1);
695
696
      /* Transmit the sub-mode we use for this frame */
697
28.6k
      speex_bits_pack(bits, st->submodeID, NB_SUBMODE_BITS);
698
699
28.6k
   }
700
701
   /* If null mode (no transmission), just set a couple things to zero*/
702
28.6k
   if (st->submodes[st->submodeID] == NULL)
703
2.38k
   {
704
383k
      for (i=0;i<NB_FRAME_SIZE;i++)
705
381k
         st->exc[i]=st->sw[i]=VERY_SMALL;
706
707
26.2k
      for (i=0;i<NB_ORDER;i++)
708
23.8k
         st->mem_sw[i]=0;
709
2.38k
      st->first=1;
710
2.38k
      st->bounded_pitch = 1;
711
712
2.38k
      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.2k
      for (i=0;i<NB_ORDER;i++)
716
23.8k
         st->mem_sp[i] = 0;
717
2.38k
      return 0;
718
719
2.38k
   }
720
721
   /* LSP Quantization */
722
26.2k
   if (st->first)
723
4.07k
   {
724
44.8k
      for (i=0;i<NB_ORDER;i++)
725
40.7k
         st->old_lsp[i] = lsp[i];
726
4.07k
   }
727
728
729
   /*Quantize LSPs*/
730
26.2k
#if 1 /*0 for unquantized*/
731
26.2k
   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
26.2k
   if (SUBMODE(lbr_pitch)!=-1)
739
17.8k
   {
740
17.8k
      speex_bits_pack(bits, ol_pitch-NB_PITCH_START, 7);
741
17.8k
   }
742
743
26.2k
   if (SUBMODE(forced_pitch_gain))
744
16.2k
   {
745
16.2k
      int quant;
746
      /* This just damps the pitch a bit, because it tends to be too aggressive when forced */
747
16.2k
      ol_pitch_coef = MULT16_16_Q15(QCONST16(.9,15), ol_pitch_coef);
748
#ifdef FIXED_POINT
749
11.3k
      quant = PSHR16(MULT16_16_16(15, ol_pitch_coef),GAIN_SHIFT);
750
#else
751
4.91k
      quant = (int)floor(.5+15*ol_pitch_coef*GAIN_SCALING_1);
752
#endif
753
16.2k
      if (quant>15)
754
120
         quant=15;
755
16.2k
      if (quant<0)
756
0
         quant=0;
757
16.2k
      speex_bits_pack(bits, quant, 4);
758
16.2k
      ol_pitch_coef=MULT16_16_P15(QCONST16(0.066667,15),SHL16(quant,GAIN_SHIFT));
759
16.2k
   }
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
18.6k
      ol_gain = MULT16_32_Q15(28406,ol_gain_table[qe]);
768
      speex_bits_pack(bits, qe, 5);
769
   }
770
#else
771
   {
772
7.61k
      int qe = (int)(floor(.5+3.5*log(ol_gain*1.0/SIG_SCALING)));
773
7.61k
      if (qe<0)
774
4.74k
         qe=0;
775
7.61k
      if (qe>31)
776
597
         qe=31;
777
7.61k
      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
26.2k
   if (st->first)
786
4.07k
   {
787
44.8k
      for (i=0;i<NB_ORDER;i++)
788
40.7k
         st->old_qlsp[i] = qlsp[i];
789
4.07k
   }
790
791
   /* Target signal */
792
26.2k
   ALLOC(target, NB_SUBFRAME_SIZE, spx_word16_t);
793
26.2k
   ALLOC(innov, NB_SUBFRAME_SIZE, spx_sig_t);
794
26.2k
   ALLOC(exc32, NB_SUBFRAME_SIZE, spx_word32_t);
795
26.2k
   ALLOC(syn_resp, NB_SUBFRAME_SIZE, spx_word16_t);
796
26.2k
   ALLOC(mem, NB_ORDER, spx_mem_t);
797
798
   /* Loop on sub-frames */
799
131k
   for (sub=0;sub<NB_NB_SUBFRAMES;sub++)
800
105k
   {
801
105k
      int   offset;
802
105k
      spx_word16_t *sw;
803
105k
      spx_word16_t *exc, *inBuf;
804
105k
      int pitch;
805
105k
      int response_bound = NB_SUBFRAME_SIZE;
806
807
      /* Offset relative to start of frame */
808
105k
      offset = NB_SUBFRAME_SIZE*sub;
809
      /* Excitation */
810
105k
      exc=st->exc+offset;
811
      /* Weighted signal */
812
105k
      sw=st->sw+offset;
813
814
      /* LSP interpolation (quantized and unquantized) */
815
105k
      lsp_interpolate(st->old_lsp, lsp, interp_lsp, NB_ORDER, sub, NB_NB_SUBFRAMES, LSP_MARGIN);
816
105k
      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
105k
      lsp_to_lpc(interp_lsp, interp_lpc, NB_ORDER,stack);
820
821
105k
      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
105k
      {
825
105k
         spx_word32_t pi_g=LPC_SCALING;
826
630k
         for (i=0;i<NB_ORDER;i+=2)
827
525k
         {
828
            /*pi_g += -st->interp_qlpc[i] +  st->interp_qlpc[i+1];*/
829
525k
            pi_g = ADD32(pi_g, SUB32(EXTEND32(interp_qlpc[i+1]),EXTEND32(interp_qlpc[i])));
830
525k
         }
831
105k
         st->pi_gain[sub] = pi_g;
832
105k
      }
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
105k
      bw_lpc(st->gamma1, interp_lpc, bw_lpc1, NB_ORDER);
845
105k
      bw_lpc(st->gamma2, interp_lpc, bw_lpc2, NB_ORDER);
846
      /*print_vec(st->bw_lpc1, 10, "bw_lpc");*/
847
105k
#endif
848
849
      /*FIXME: This will break if we change the window size */
850
105k
      speex_assert(NB_WINDOW_SIZE-NB_FRAME_SIZE == NB_SUBFRAME_SIZE);
851
105k
      if (sub==0)
852
26.2k
         inBuf = st->winBuf;
853
78.7k
      else
854
78.7k
         inBuf = &in[((sub-1)*NB_SUBFRAME_SIZE)];
855
4.30M
      for (i=0;i<NB_SUBFRAME_SIZE;i++)
856
4.20M
         sw[i] = inBuf[i];
857
858
105k
      if (st->complexity==0)
859
24.5k
         response_bound >>= 1;
860
105k
      compute_impulse_response(interp_qlpc, bw_lpc1, bw_lpc2, syn_resp, response_bound, NB_ORDER, stack);
861
595k
      for (i=response_bound;i<NB_SUBFRAME_SIZE;i++)
862
490k
         syn_resp[i]=VERY_SMALL;
863
864
      /* Compute zero response of A(z/g1) / ( A(z/g2) * A(z) ) */
865
1.15M
      for (i=0;i<NB_ORDER;i++)
866
1.05M
         mem[i]=SHL32(st->mem_sp[i],1);
867
4.30M
      for (i=0;i<NB_SUBFRAME_SIZE;i++)
868
4.20M
         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
105k
      iir_mem16(exc, interp_qlpc, exc, NB_SUBFRAME_SIZE, NB_ORDER, mem, stack);
877
1.15M
      for (i=0;i<NB_ORDER;i++)
878
1.05M
         mem[i]=SHL32(st->mem_sw[i],1);
879
105k
      filter10(exc, bw_lpc1, bw_lpc2, exc, NB_SUBFRAME_SIZE, mem, stack);
880
105k
#endif
881
882
      /* Compute weighted signal */
883
1.15M
      for (i=0;i<NB_ORDER;i++)
884
1.05M
         mem[i]=st->mem_sw[i];
885
105k
      filter10(sw, bw_lpc1, bw_lpc2, sw, NB_SUBFRAME_SIZE, mem, stack);
886
887
105k
      if (st->complexity==0)
888
269k
         for (i=0;i<NB_ORDER;i++)
889
245k
            st->mem_sw[i]=mem[i];
890
891
      /* Compute target signal (saturation prevents overflows on clipped input speech) */
892
4.30M
      for (i=0;i<NB_SUBFRAME_SIZE;i++)
893
4.20M
         target[i]=EXTRACT16(SATURATE(SUB32(sw[i],PSHR32(exc[i],1)),32767));
894
895
4.30M
      for (i=0;i<NB_SUBFRAME_SIZE;i++)
896
4.20M
         exc[i] = inBuf[i];
897
105k
      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
105k
      speex_assert (SUBMODE(ltp_quant));
900
105k
      {
901
105k
         int pit_min, pit_max;
902
         /* Long-term prediction */
903
105k
         if (SUBMODE(lbr_pitch) != -1)
904
71.4k
         {
905
            /* Low bit-rate pitch handling */
906
71.4k
            int margin;
907
71.4k
            margin = SUBMODE(lbr_pitch);
908
71.4k
            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
71.4k
            } else {
917
71.4k
               pit_min=pit_max=ol_pitch;
918
71.4k
            }
919
71.4k
         } else {
920
33.5k
            pit_min = NB_PITCH_START;
921
33.5k
            pit_max = NB_PITCH_END;
922
33.5k
         }
923
924
         /* Force pitch to use only the current frame if needed */
925
105k
         if (st->bounded_pitch && pit_max>offset)
926
31.6k
            pit_max=offset;
927
928
         /* Perform pitch search */
929
105k
         pitch = SUBMODE(ltp_quant)(target, sw, interp_qlpc, bw_lpc1, bw_lpc2,
930
105k
                                    exc32, SUBMODE(ltp_params), pit_min, pit_max, ol_pitch_coef,
931
105k
                                    NB_ORDER, NB_SUBFRAME_SIZE, bits, stack,
932
105k
                                    exc, syn_resp, st->complexity, 0, st->plc_tuning, &st->cumul_gain);
933
934
105k
         st->pitch[sub]=pitch;
935
105k
      }
936
      /* Quantization of innovation */
937
105k
      SPEEX_MEMSET(innov, 0, NB_SUBFRAME_SIZE);
938
939
      /* FIXME: Make sure this is safe from overflows (so far so good) */
940
4.30M
      for (i=0;i<NB_SUBFRAME_SIZE;i++)
941
4.20M
         exc[i] = EXTRACT16(SUB32(EXTEND32(exc[i]), PSHR32(exc32[i],SIG_SHIFT-1)));
942
943
105k
      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
74.6k
         spx_word32_t f = PDIV32(ener,PSHR32(ol_gain,SIG_SHIFT));
949
74.6k
         if (f<=32767)
950
69.1k
            fine_gain = f;
951
5.49k
         else
952
5.49k
            fine_gain = 32767;
953
      }
954
#else
955
30.4k
      fine_gain = PDIV32_16(ener,PSHR32(ol_gain,SIG_SHIFT));
956
#endif
957
      /* Calculate gain correction for the sub-frame (if any) */
958
105k
      if (SUBMODE(have_subframe_gain))
959
33.5k
      {
960
33.5k
         int qe;
961
33.5k
         if (SUBMODE(have_subframe_gain)==3)
962
19.2k
         {
963
19.2k
            qe = scal_quant(fine_gain, exc_gain_quant_scal3_bound, 8);
964
19.2k
            speex_bits_pack(bits, qe, 3);
965
19.2k
            ener=MULT16_32_Q14(exc_gain_quant_scal3[qe],ol_gain);
966
19.2k
         } else {
967
14.3k
            qe = scal_quant(fine_gain, exc_gain_quant_scal1_bound, 2);
968
14.3k
            speex_bits_pack(bits, qe, 1);
969
14.3k
            ener=MULT16_32_Q14(exc_gain_quant_scal1[qe],ol_gain);
970
14.3k
         }
971
71.4k
      } else {
972
71.4k
         ener=ol_gain;
973
71.4k
      }
974
975
      /*printf ("%f %f\n", ener, ol_gain);*/
976
977
      /* Normalize innovation */
978
105k
      signal_div(target, target, ener, NB_SUBFRAME_SIZE);
979
980
      /* Quantize innovation */
981
105k
      speex_assert (SUBMODE(innovation_quant));
982
105k
      {
983
         /* Codebook search */
984
105k
         SUBMODE(innovation_quant)(target, interp_qlpc, bw_lpc1, bw_lpc2,
985
105k
                  SUBMODE(innovation_params), NB_ORDER, NB_SUBFRAME_SIZE,
986
105k
                  innov, syn_resp, bits, stack, st->complexity, SUBMODE(double_codebook));
987
988
         /* De-normalize innovation and update excitation */
989
105k
         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
105k
         if (SUBMODE(double_codebook)) {
993
3.31k
            char *tmp_stack=stack;
994
3.31k
            VARDECL(spx_sig_t *innov2);
995
3.31k
            ALLOC(innov2, NB_SUBFRAME_SIZE, spx_sig_t);
996
3.31k
            SPEEX_MEMSET(innov2, 0, NB_SUBFRAME_SIZE);
997
135k
            for (i=0;i<NB_SUBFRAME_SIZE;i++)
998
132k
               target[i]=MULT16_16_P13(QCONST16(2.2f,13), target[i]);
999
3.31k
            SUBMODE(innovation_quant)(target, interp_qlpc, bw_lpc1, bw_lpc2,
1000
3.31k
                                      SUBMODE(innovation_params), NB_ORDER, NB_SUBFRAME_SIZE,
1001
3.31k
                                      innov2, syn_resp, bits, stack, st->complexity, 0);
1002
3.31k
            signal_mul(innov2, innov2, MULT16_32_Q15(QCONST16(0.454545f,15),ener), NB_SUBFRAME_SIZE);
1003
135k
            for (i=0;i<NB_SUBFRAME_SIZE;i++)
1004
132k
               innov[i] = ADD32(innov[i],innov2[i]);
1005
3.31k
            stack = tmp_stack;
1006
3.31k
         }
1007
4.30M
         for (i=0;i<NB_SUBFRAME_SIZE;i++)
1008
4.20M
            exc[i] = EXTRACT16(SATURATE32(PSHR32(ADD32(SHL32(exc32[i],1),innov[i]),SIG_SHIFT),32767));
1009
105k
         if (st->innov_rms_save)
1010
30.7k
            st->innov_rms_save[sub] = compute_rms(innov, NB_SUBFRAME_SIZE);
1011
105k
      }
1012
1013
      /* Final signal synthesis from excitation */
1014
105k
      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
105k
      if (st->complexity!=0)
1018
80.5k
         filter10(sw, bw_lpc1, bw_lpc2, sw, NB_SUBFRAME_SIZE, st->mem_sw, stack);
1019
1020
105k
   }
1021
1022
   /* Store the LSPs for interpolation in the next frame */
1023
26.2k
   if (st->submodeID>=1)
1024
26.2k
   {
1025
288k
      for (i=0;i<NB_ORDER;i++)
1026
262k
         st->old_lsp[i] = lsp[i];
1027
288k
      for (i=0;i<NB_ORDER;i++)
1028
262k
         st->old_qlsp[i] = qlsp[i];
1029
26.2k
   }
1030
1031
#ifdef VORBIS_PSYCHO
1032
   if (st->submodeID>=1)
1033
      SPEEX_COPY(st->old_curve, st->curve, 128);
1034
#endif
1035
1036
26.2k
   if (st->submodeID==1)
1037
13.1k
   {
1038
13.1k
#ifndef DISABLE_VBR
1039
13.1k
      if (st->dtx_count)
1040
3.82k
         speex_bits_pack(bits, 15, 4);
1041
9.27k
      else
1042
9.27k
#endif
1043
9.27k
         speex_bits_pack(bits, 0, 4);
1044
13.1k
   }
1045
1046
   /* The next frame will not be the first (Duh!) */
1047
26.2k
   st->first = 0;
1048
26.2k
   SPEEX_COPY(st->winBuf, in+2*NB_FRAME_SIZE-NB_WINDOW_SIZE, NB_WINDOW_SIZE-NB_FRAME_SIZE);
1049
1050
26.2k
   if (SUBMODE(innovation_quant) == noise_codebook_quant || st->submodeID==0)
1051
13.1k
      st->bounded_pitch = 1;
1052
13.1k
   else
1053
13.1k
      st->bounded_pitch = 0;
1054
1055
26.2k
   return 1;
1056
28.6k
}
nb_encode
Line
Count
Source
407
8.75k
{
408
8.75k
   EncState *st;
409
8.75k
   int i, sub, roots;
410
8.75k
   int ol_pitch;
411
8.75k
   spx_word16_t ol_pitch_coef;
412
8.75k
   spx_word32_t ol_gain;
413
8.75k
   VARDECL(spx_word16_t *target);
414
8.75k
   VARDECL(spx_sig_t *innov);
415
8.75k
   VARDECL(spx_word32_t *exc32);
416
8.75k
   VARDECL(spx_mem_t *mem);
417
8.75k
   VARDECL(spx_coef_t *bw_lpc1);
418
8.75k
   VARDECL(spx_coef_t *bw_lpc2);
419
8.75k
   VARDECL(spx_coef_t *lpc);
420
8.75k
   VARDECL(spx_lsp_t *lsp);
421
8.75k
   VARDECL(spx_lsp_t *qlsp);
422
8.75k
   VARDECL(spx_lsp_t *interp_lsp);
423
8.75k
   VARDECL(spx_lsp_t *interp_qlsp);
424
8.75k
   VARDECL(spx_coef_t *interp_lpc);
425
8.75k
   VARDECL(spx_coef_t *interp_qlpc);
426
8.75k
   char *stack;
427
8.75k
   VARDECL(spx_word16_t *syn_resp);
428
429
8.75k
   spx_word32_t ener=0;
430
8.75k
   spx_word16_t fine_gain;
431
8.75k
   spx_word16_t *in = (spx_word16_t*)vin;
432
433
8.75k
   st=(EncState *)state;
434
8.75k
   stack=st->stack;
435
436
8.75k
   ALLOC(lpc, NB_ORDER, spx_coef_t);
437
8.75k
   ALLOC(bw_lpc1, NB_ORDER, spx_coef_t);
438
8.75k
   ALLOC(bw_lpc2, NB_ORDER, spx_coef_t);
439
8.75k
   ALLOC(lsp, NB_ORDER, spx_lsp_t);
440
8.75k
   ALLOC(qlsp, NB_ORDER, spx_lsp_t);
441
8.75k
   ALLOC(interp_lsp, NB_ORDER, spx_lsp_t);
442
8.75k
   ALLOC(interp_qlsp, NB_ORDER, spx_lsp_t);
443
8.75k
   ALLOC(interp_lpc, NB_ORDER, spx_coef_t);
444
8.75k
   ALLOC(interp_qlpc, NB_ORDER, spx_coef_t);
445
446
8.75k
   st->exc = st->excBuf + NB_PITCH_END + 2;
447
8.75k
   st->sw = st->swBuf + NB_PITCH_END + 2;
448
   /* Move signals 1 frame towards the past */
449
8.75k
   SPEEX_MOVE(st->excBuf, st->excBuf+NB_FRAME_SIZE, NB_PITCH_END+2);
450
8.75k
   SPEEX_MOVE(st->swBuf, st->swBuf+NB_FRAME_SIZE, NB_PITCH_END+2);
451
452
8.75k
   if (st->highpass_enabled)
453
4.90k
      highpass(in, in, NB_FRAME_SIZE, (st->isWideband?HIGHPASS_WIDEBAND:HIGHPASS_NARROWBAND)|HIGHPASS_INPUT, st->mem_hp);
454
455
8.75k
   {
456
8.75k
      VARDECL(spx_word16_t *w_sig);
457
8.75k
      VARDECL(spx_word16_t *autocorr);
458
8.75k
      ALLOC(w_sig, NB_WINDOW_SIZE, spx_word16_t);
459
8.75k
      ALLOC(autocorr, NB_ORDER+1, spx_word16_t);
460
      /* Window for analysis */
461
359k
      for (i=0;i<NB_WINDOW_SIZE-NB_FRAME_SIZE;i++)
462
350k
         w_sig[i] = MULT16_16_Q15(st->winBuf[i],st->window[i]);
463
1.40M
      for (;i<NB_WINDOW_SIZE;i++)
464
1.40M
         w_sig[i] = MULT16_16_Q15(in[i-NB_WINDOW_SIZE+NB_FRAME_SIZE],st->window[i]);
465
      /* Compute auto-correlation */
466
8.75k
      _spx_autocorr(w_sig, autocorr, NB_ORDER+1, NB_WINDOW_SIZE);
467
8.75k
      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
105k
      for (i=0;i<NB_ORDER+1;i++)
471
96.3k
         autocorr[i] = MULT16_16_Q15(autocorr[i],st->lagWindow[i]);
472
8.75k
      autocorr[0] = ADD16(autocorr[0],1);
473
474
      /* Levinson-Durbin */
475
8.75k
      _spx_lpc(lpc, autocorr, NB_ORDER);
476
      /* LPC to LSPs (x-domain) transform */
477
8.75k
      roots=lpc_to_lsp (lpc, NB_ORDER, lsp, 10, LSP_DELTA1, stack);
478
      /* Check if we found all the roots */
479
8.75k
      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.75k
   }
488
489
490
491
492
   /* Whole frame analysis (open-loop estimation of pitch and excitation gain) */
493
8.75k
   {
494
8.75k
      int diff = NB_WINDOW_SIZE-NB_FRAME_SIZE;
495
8.75k
      if (st->first)
496
23.3k
         for (i=0;i<NB_ORDER;i++)
497
21.2k
            interp_lsp[i] = lsp[i];
498
6.63k
      else
499
6.63k
         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.75k
      lsp_to_lpc(interp_lsp, interp_lpc, NB_ORDER,stack);
503
504
505
      /*Open-loop pitch*/
506
8.75k
      if (!st->submodes[st->submodeID] || (st->complexity>2 && SUBMODE(have_subframe_gain)<3) || SUBMODE(forced_pitch_gain) || SUBMODE(lbr_pitch) != -1
507
8.75k
#ifndef DISABLE_VBR
508
8.75k
           || st->vbr_enabled || st->vad_enabled
509
8.75k
#endif
510
8.75k
                  )
511
8.10k
      {
512
8.10k
         int nol_pitch[6];
513
8.10k
         spx_word16_t nol_pitch_coef[6];
514
515
8.10k
         bw_lpc(QCONST16(0.9,15), interp_lpc, bw_lpc1, NB_ORDER);
516
8.10k
         bw_lpc(QCONST16(0.55,15), interp_lpc, bw_lpc2, NB_ORDER);
517
518
8.10k
         SPEEX_COPY(st->sw, st->winBuf, diff);
519
8.10k
         SPEEX_COPY(st->sw+diff, in, NB_FRAME_SIZE-diff);
520
8.10k
         filter10(st->sw, bw_lpc1, bw_lpc2, st->sw, NB_FRAME_SIZE, st->mem_sw_whole, stack);
521
522
8.10k
         open_loop_nbest_pitch(st->sw, NB_PITCH_START, NB_PITCH_END, NB_FRAME_SIZE,
523
8.10k
                               nol_pitch, nol_pitch_coef, 6, stack);
524
8.10k
         ol_pitch=nol_pitch[0];
525
8.10k
         ol_pitch_coef = nol_pitch_coef[0];
526
         /*Try to remove pitch multiples*/
527
48.6k
         for (i=1;i<6;i++)
528
40.5k
         {
529
#ifdef FIXED_POINT
530
            if ((nol_pitch_coef[i]>MULT16_16_Q15(nol_pitch_coef[0],27853)) &&
531
#else
532
40.5k
            if ((nol_pitch_coef[i]>.85*nol_pitch_coef[0]) &&
533
40.5k
#endif
534
40.5k
                (ABS(2*nol_pitch[i]-ol_pitch)<=2 || ABS(3*nol_pitch[i]-ol_pitch)<=3 ||
535
11.1k
                 ABS(4*nol_pitch[i]-ol_pitch)<=4 || ABS(5*nol_pitch[i]-ol_pitch)<=5))
536
266
            {
537
               /*ol_pitch_coef=nol_pitch_coef[i];*/
538
266
               ol_pitch = nol_pitch[i];
539
266
            }
540
40.5k
         }
541
         /*if (ol_pitch>50)
542
           ol_pitch/=2;*/
543
         /*ol_pitch_coef = sqrt(ol_pitch_coef);*/
544
545
8.10k
      } else {
546
657
         ol_pitch=0;
547
657
         ol_pitch_coef=0;
548
657
      }
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.75k
      fir_mem16(st->winBuf, interp_lpc, st->exc, diff, NB_ORDER, st->mem_exc, stack);
554
8.75k
      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.75k
      {
558
8.75k
         spx_word16_t g = compute_rms16(st->exc, NB_FRAME_SIZE);
559
8.75k
         if (st->submodeID!=1 && ol_pitch>0)
560
4.09k
            ol_gain = MULT16_16(g, MULT16_16_Q14(QCONST16(1.1,14),
561
8.75k
                                spx_sqrt(QCONST32(1.,28)-MULT16_32_Q15(QCONST16(.8,15),SHL32(MULT16_16(ol_pitch_coef,ol_pitch_coef),16)))));
562
4.65k
         else
563
4.65k
            ol_gain = SHL32(EXTEND32(g),SIG_SHIFT);
564
8.75k
      }
565
8.75k
   }
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.75k
#ifndef DISABLE_VBR
578
8.75k
   if (st->vbr_enabled||st->vad_enabled)
579
7.08k
   {
580
7.08k
      float lsp_dist=0;
581
77.9k
      for (i=0;i<NB_ORDER;i++)
582
70.8k
         lsp_dist += (st->old_lsp[i] - lsp[i])*(st->old_lsp[i] - lsp[i]);
583
7.08k
      lsp_dist /= LSP_SCALING*LSP_SCALING;
584
585
7.08k
      if (st->abr_enabled)
586
1.88k
      {
587
1.88k
         float qual_change=0;
588
1.88k
         if (st->abr_drift2 * st->abr_drift > 0)
589
1.69k
         {
590
            /* Only adapt if long-term and short-term drift are the same sign */
591
1.69k
            qual_change = -.00001*st->abr_drift/(1+st->abr_count);
592
1.69k
            if (qual_change>.05)
593
0
               qual_change=.05;
594
1.69k
            if (qual_change<-.05)
595
398
               qual_change=-.05;
596
1.69k
         }
597
1.88k
         st->vbr_quality += qual_change;
598
1.88k
         if (st->vbr_quality>10)
599
0
            st->vbr_quality=10;
600
1.88k
         if (st->vbr_quality<0)
601
1.69k
            st->vbr_quality=0;
602
1.88k
      }
603
604
7.08k
      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
7.08k
      if (st->vbr_enabled)
608
5.69k
      {
609
5.69k
         spx_int32_t mode;
610
5.69k
         int choice=0;
611
5.69k
         float min_diff=100;
612
5.69k
         mode = 8;
613
51.2k
         while (mode)
614
45.5k
         {
615
45.5k
            int v1;
616
45.5k
            float thresh;
617
45.5k
            v1=(int)floor(st->vbr_quality);
618
45.5k
            if (v1==10)
619
4.20k
               thresh = vbr_nb_thresh[mode][v1];
620
41.3k
            else
621
41.3k
               thresh = (st->vbr_quality-v1)*vbr_nb_thresh[mode][v1+1] + (1+v1-st->vbr_quality)*vbr_nb_thresh[mode][v1];
622
45.5k
            if (st->relative_quality > thresh &&
623
45.5k
                st->relative_quality-thresh<min_diff)
624
4.97k
            {
625
4.97k
               choice = mode;
626
4.97k
               min_diff = st->relative_quality-thresh;
627
4.97k
            }
628
45.5k
            mode--;
629
45.5k
         }
630
5.69k
         mode=choice;
631
5.69k
         if (mode==0)
632
2.08k
         {
633
2.08k
            if (st->dtx_count==0 || lsp_dist>.05 || !st->dtx_enabled || st->dtx_count>20)
634
1.12k
            {
635
1.12k
               mode=1;
636
1.12k
               st->dtx_count=1;
637
1.12k
            } else {
638
955
               mode=0;
639
955
               st->dtx_count++;
640
955
            }
641
3.60k
         } else {
642
3.60k
            st->dtx_count=0;
643
3.60k
         }
644
645
5.69k
         speex_encoder_ctl(state, SPEEX_SET_MODE, &mode);
646
5.69k
         if (st->vbr_max>0)
647
4.23k
         {
648
4.23k
            spx_int32_t rate;
649
4.23k
            speex_encoder_ctl(state, SPEEX_GET_BITRATE, &rate);
650
4.23k
            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.23k
         }
656
657
5.69k
         if (st->abr_enabled)
658
1.88k
         {
659
1.88k
            spx_int32_t bitrate;
660
1.88k
            speex_encoder_ctl(state, SPEEX_GET_BITRATE, &bitrate);
661
1.88k
            st->abr_drift+=(bitrate-st->abr_enabled);
662
1.88k
            st->abr_drift2 = .95*st->abr_drift2 + .05*(bitrate-st->abr_enabled);
663
1.88k
            st->abr_count += 1.0;
664
1.88k
         }
665
666
5.69k
      } else {
667
         /*VAD only case*/
668
1.39k
         int mode;
669
1.39k
         if (st->relative_quality<2)
670
688
         {
671
688
            if (st->dtx_count==0 || lsp_dist>.05 || !st->dtx_enabled || st->dtx_count>20)
672
447
            {
673
447
               st->dtx_count=1;
674
447
               mode=1;
675
447
            } else {
676
241
               mode=0;
677
241
               st->dtx_count++;
678
241
            }
679
710
         } else {
680
710
            st->dtx_count = 0;
681
710
            mode=st->submodeSelect;
682
710
         }
683
         /*speex_encoder_ctl(state, SPEEX_SET_MODE, &mode);*/
684
1.39k
         st->submodeID=mode;
685
1.39k
      }
686
7.08k
   } else {
687
1.66k
      st->relative_quality = -1;
688
1.66k
   }
689
8.75k
#endif /* #ifndef DISABLE_VBR */
690
691
8.75k
   if (st->encode_submode)
692
8.75k
   {
693
      /* First, transmit a zero for narrowband */
694
8.75k
      speex_bits_pack(bits, 0, 1);
695
696
      /* Transmit the sub-mode we use for this frame */
697
8.75k
      speex_bits_pack(bits, st->submodeID, NB_SUBMODE_BITS);
698
699
8.75k
   }
700
701
   /* If null mode (no transmission), just set a couple things to zero*/
702
8.75k
   if (st->submodes[st->submodeID] == NULL)
703
1.14k
   {
704
184k
      for (i=0;i<NB_FRAME_SIZE;i++)
705
183k
         st->exc[i]=st->sw[i]=VERY_SMALL;
706
707
12.6k
      for (i=0;i<NB_ORDER;i++)
708
11.4k
         st->mem_sw[i]=0;
709
1.14k
      st->first=1;
710
1.14k
      st->bounded_pitch = 1;
711
712
1.14k
      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
12.6k
      for (i=0;i<NB_ORDER;i++)
716
11.4k
         st->mem_sp[i] = 0;
717
1.14k
      return 0;
718
719
1.14k
   }
720
721
   /* LSP Quantization */
722
7.61k
   if (st->first)
723
1.28k
   {
724
14.1k
      for (i=0;i<NB_ORDER;i++)
725
12.8k
         st->old_lsp[i] = lsp[i];
726
1.28k
   }
727
728
729
   /*Quantize LSPs*/
730
7.61k
#if 1 /*0 for unquantized*/
731
7.61k
   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.61k
   if (SUBMODE(lbr_pitch)!=-1)
739
5.28k
   {
740
5.28k
      speex_bits_pack(bits, ol_pitch-NB_PITCH_START, 7);
741
5.28k
   }
742
743
7.61k
   if (SUBMODE(forced_pitch_gain))
744
4.91k
   {
745
4.91k
      int quant;
746
      /* This just damps the pitch a bit, because it tends to be too aggressive when forced */
747
4.91k
      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.91k
      quant = (int)floor(.5+15*ol_pitch_coef*GAIN_SCALING_1);
752
4.91k
#endif
753
4.91k
      if (quant>15)
754
38
         quant=15;
755
4.91k
      if (quant<0)
756
0
         quant=0;
757
4.91k
      speex_bits_pack(bits, quant, 4);
758
4.91k
      ol_pitch_coef=MULT16_16_P15(QCONST16(0.066667,15),SHL16(quant,GAIN_SHIFT));
759
4.91k
   }
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.61k
   {
772
7.61k
      int qe = (int)(floor(.5+3.5*log(ol_gain*1.0/SIG_SCALING)));
773
7.61k
      if (qe<0)
774
4.74k
         qe=0;
775
7.61k
      if (qe>31)
776
597
         qe=31;
777
7.61k
      ol_gain = exp(qe/3.5)*SIG_SCALING;
778
7.61k
      speex_bits_pack(bits, qe, 5);
779
7.61k
   }
780
7.61k
#endif
781
782
783
784
   /* Special case for first frame */
785
7.61k
   if (st->first)
786
1.28k
   {
787
14.1k
      for (i=0;i<NB_ORDER;i++)
788
12.8k
         st->old_qlsp[i] = qlsp[i];
789
1.28k
   }
790
791
   /* Target signal */
792
7.61k
   ALLOC(target, NB_SUBFRAME_SIZE, spx_word16_t);
793
7.61k
   ALLOC(innov, NB_SUBFRAME_SIZE, spx_sig_t);
794
7.61k
   ALLOC(exc32, NB_SUBFRAME_SIZE, spx_word32_t);
795
7.61k
   ALLOC(syn_resp, NB_SUBFRAME_SIZE, spx_word16_t);
796
7.61k
   ALLOC(mem, NB_ORDER, spx_mem_t);
797
798
   /* Loop on sub-frames */
799
38.0k
   for (sub=0;sub<NB_NB_SUBFRAMES;sub++)
800
30.4k
   {
801
30.4k
      int   offset;
802
30.4k
      spx_word16_t *sw;
803
30.4k
      spx_word16_t *exc, *inBuf;
804
30.4k
      int pitch;
805
30.4k
      int response_bound = NB_SUBFRAME_SIZE;
806
807
      /* Offset relative to start of frame */
808
30.4k
      offset = NB_SUBFRAME_SIZE*sub;
809
      /* Excitation */
810
30.4k
      exc=st->exc+offset;
811
      /* Weighted signal */
812
30.4k
      sw=st->sw+offset;
813
814
      /* LSP interpolation (quantized and unquantized) */
815
30.4k
      lsp_interpolate(st->old_lsp, lsp, interp_lsp, NB_ORDER, sub, NB_NB_SUBFRAMES, LSP_MARGIN);
816
30.4k
      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
30.4k
      lsp_to_lpc(interp_lsp, interp_lpc, NB_ORDER,stack);
820
821
30.4k
      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
30.4k
      {
825
30.4k
         spx_word32_t pi_g=LPC_SCALING;
826
182k
         for (i=0;i<NB_ORDER;i+=2)
827
152k
         {
828
            /*pi_g += -st->interp_qlpc[i] +  st->interp_qlpc[i+1];*/
829
152k
            pi_g = ADD32(pi_g, SUB32(EXTEND32(interp_qlpc[i+1]),EXTEND32(interp_qlpc[i])));
830
152k
         }
831
30.4k
         st->pi_gain[sub] = pi_g;
832
30.4k
      }
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
30.4k
      bw_lpc(st->gamma1, interp_lpc, bw_lpc1, NB_ORDER);
845
30.4k
      bw_lpc(st->gamma2, interp_lpc, bw_lpc2, NB_ORDER);
846
      /*print_vec(st->bw_lpc1, 10, "bw_lpc");*/
847
30.4k
#endif
848
849
      /*FIXME: This will break if we change the window size */
850
30.4k
      speex_assert(NB_WINDOW_SIZE-NB_FRAME_SIZE == NB_SUBFRAME_SIZE);
851
30.4k
      if (sub==0)
852
7.61k
         inBuf = st->winBuf;
853
22.8k
      else
854
22.8k
         inBuf = &in[((sub-1)*NB_SUBFRAME_SIZE)];
855
1.24M
      for (i=0;i<NB_SUBFRAME_SIZE;i++)
856
1.21M
         sw[i] = inBuf[i];
857
858
30.4k
      if (st->complexity==0)
859
7.32k
         response_bound >>= 1;
860
30.4k
      compute_impulse_response(interp_qlpc, bw_lpc1, bw_lpc2, syn_resp, response_bound, NB_ORDER, stack);
861
176k
      for (i=response_bound;i<NB_SUBFRAME_SIZE;i++)
862
146k
         syn_resp[i]=VERY_SMALL;
863
864
      /* Compute zero response of A(z/g1) / ( A(z/g2) * A(z) ) */
865
334k
      for (i=0;i<NB_ORDER;i++)
866
304k
         mem[i]=SHL32(st->mem_sp[i],1);
867
1.24M
      for (i=0;i<NB_SUBFRAME_SIZE;i++)
868
1.21M
         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
30.4k
      iir_mem16(exc, interp_qlpc, exc, NB_SUBFRAME_SIZE, NB_ORDER, mem, stack);
877
334k
      for (i=0;i<NB_ORDER;i++)
878
304k
         mem[i]=SHL32(st->mem_sw[i],1);
879
30.4k
      filter10(exc, bw_lpc1, bw_lpc2, exc, NB_SUBFRAME_SIZE, mem, stack);
880
30.4k
#endif
881
882
      /* Compute weighted signal */
883
334k
      for (i=0;i<NB_ORDER;i++)
884
304k
         mem[i]=st->mem_sw[i];
885
30.4k
      filter10(sw, bw_lpc1, bw_lpc2, sw, NB_SUBFRAME_SIZE, mem, stack);
886
887
30.4k
      if (st->complexity==0)
888
80.5k
         for (i=0;i<NB_ORDER;i++)
889
73.2k
            st->mem_sw[i]=mem[i];
890
891
      /* Compute target signal (saturation prevents overflows on clipped input speech) */
892
1.24M
      for (i=0;i<NB_SUBFRAME_SIZE;i++)
893
1.21M
         target[i]=EXTRACT16(SATURATE(SUB32(sw[i],PSHR32(exc[i],1)),32767));
894
895
1.24M
      for (i=0;i<NB_SUBFRAME_SIZE;i++)
896
1.21M
         exc[i] = inBuf[i];
897
30.4k
      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
30.4k
      speex_assert (SUBMODE(ltp_quant));
900
30.4k
      {
901
30.4k
         int pit_min, pit_max;
902
         /* Long-term prediction */
903
30.4k
         if (SUBMODE(lbr_pitch) != -1)
904
21.1k
         {
905
            /* Low bit-rate pitch handling */
906
21.1k
            int margin;
907
21.1k
            margin = SUBMODE(lbr_pitch);
908
21.1k
            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
21.1k
            } else {
917
21.1k
               pit_min=pit_max=ol_pitch;
918
21.1k
            }
919
21.1k
         } else {
920
9.30k
            pit_min = NB_PITCH_START;
921
9.30k
            pit_max = NB_PITCH_END;
922
9.30k
         }
923
924
         /* Force pitch to use only the current frame if needed */
925
30.4k
         if (st->bounded_pitch && pit_max>offset)
926
9.15k
            pit_max=offset;
927
928
         /* Perform pitch search */
929
30.4k
         pitch = SUBMODE(ltp_quant)(target, sw, interp_qlpc, bw_lpc1, bw_lpc2,
930
30.4k
                                    exc32, SUBMODE(ltp_params), pit_min, pit_max, ol_pitch_coef,
931
30.4k
                                    NB_ORDER, NB_SUBFRAME_SIZE, bits, stack,
932
30.4k
                                    exc, syn_resp, st->complexity, 0, st->plc_tuning, &st->cumul_gain);
933
934
30.4k
         st->pitch[sub]=pitch;
935
30.4k
      }
936
      /* Quantization of innovation */
937
30.4k
      SPEEX_MEMSET(innov, 0, NB_SUBFRAME_SIZE);
938
939
      /* FIXME: Make sure this is safe from overflows (so far so good) */
940
1.24M
      for (i=0;i<NB_SUBFRAME_SIZE;i++)
941
1.21M
         exc[i] = EXTRACT16(SUB32(EXTEND32(exc[i]), PSHR32(exc32[i],SIG_SHIFT-1)));
942
943
30.4k
      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
30.4k
      fine_gain = PDIV32_16(ener,PSHR32(ol_gain,SIG_SHIFT));
956
30.4k
#endif
957
      /* Calculate gain correction for the sub-frame (if any) */
958
30.4k
      if (SUBMODE(have_subframe_gain))
959
9.30k
      {
960
9.30k
         int qe;
961
9.30k
         if (SUBMODE(have_subframe_gain)==3)
962
5.56k
         {
963
5.56k
            qe = scal_quant(fine_gain, exc_gain_quant_scal3_bound, 8);
964
5.56k
            speex_bits_pack(bits, qe, 3);
965
5.56k
            ener=MULT16_32_Q14(exc_gain_quant_scal3[qe],ol_gain);
966
5.56k
         } else {
967
3.74k
            qe = scal_quant(fine_gain, exc_gain_quant_scal1_bound, 2);
968
3.74k
            speex_bits_pack(bits, qe, 1);
969
3.74k
            ener=MULT16_32_Q14(exc_gain_quant_scal1[qe],ol_gain);
970
3.74k
         }
971
21.1k
      } else {
972
21.1k
         ener=ol_gain;
973
21.1k
      }
974
975
      /*printf ("%f %f\n", ener, ol_gain);*/
976
977
      /* Normalize innovation */
978
30.4k
      signal_div(target, target, ener, NB_SUBFRAME_SIZE);
979
980
      /* Quantize innovation */
981
30.4k
      speex_assert (SUBMODE(innovation_quant));
982
30.4k
      {
983
         /* Codebook search */
984
30.4k
         SUBMODE(innovation_quant)(target, interp_qlpc, bw_lpc1, bw_lpc2,
985
30.4k
                  SUBMODE(innovation_params), NB_ORDER, NB_SUBFRAME_SIZE,
986
30.4k
                  innov, syn_resp, bits, stack, st->complexity, SUBMODE(double_codebook));
987
988
         /* De-normalize innovation and update excitation */
989
30.4k
         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
30.4k
         if (SUBMODE(double_codebook)) {
993
1.22k
            char *tmp_stack=stack;
994
1.22k
            VARDECL(spx_sig_t *innov2);
995
1.22k
            ALLOC(innov2, NB_SUBFRAME_SIZE, spx_sig_t);
996
1.22k
            SPEEX_MEMSET(innov2, 0, NB_SUBFRAME_SIZE);
997
50.3k
            for (i=0;i<NB_SUBFRAME_SIZE;i++)
998
49.1k
               target[i]=MULT16_16_P13(QCONST16(2.2f,13), target[i]);
999
1.22k
            SUBMODE(innovation_quant)(target, interp_qlpc, bw_lpc1, bw_lpc2,
1000
1.22k
                                      SUBMODE(innovation_params), NB_ORDER, NB_SUBFRAME_SIZE,
1001
1.22k
                                      innov2, syn_resp, bits, stack, st->complexity, 0);
1002
1.22k
            signal_mul(innov2, innov2, MULT16_32_Q15(QCONST16(0.454545f,15),ener), NB_SUBFRAME_SIZE);
1003
50.3k
            for (i=0;i<NB_SUBFRAME_SIZE;i++)
1004
49.1k
               innov[i] = ADD32(innov[i],innov2[i]);
1005
1.22k
            stack = tmp_stack;
1006
1.22k
         }
1007
1.24M
         for (i=0;i<NB_SUBFRAME_SIZE;i++)
1008
1.21M
            exc[i] = EXTRACT16(SATURATE32(PSHR32(ADD32(SHL32(exc32[i],1),innov[i]),SIG_SHIFT),32767));
1009
30.4k
         if (st->innov_rms_save)
1010
10.2k
            st->innov_rms_save[sub] = compute_rms(innov, NB_SUBFRAME_SIZE);
1011
30.4k
      }
1012
1013
      /* Final signal synthesis from excitation */
1014
30.4k
      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
30.4k
      if (st->complexity!=0)
1018
23.1k
         filter10(sw, bw_lpc1, bw_lpc2, sw, NB_SUBFRAME_SIZE, st->mem_sw, stack);
1019
1020
30.4k
   }
1021
1022
   /* Store the LSPs for interpolation in the next frame */
1023
7.61k
   if (st->submodeID>=1)
1024
7.61k
   {
1025
83.7k
      for (i=0;i<NB_ORDER;i++)
1026
76.1k
         st->old_lsp[i] = lsp[i];
1027
83.7k
      for (i=0;i<NB_ORDER;i++)
1028
76.1k
         st->old_qlsp[i] = qlsp[i];
1029
7.61k
   }
1030
1031
#ifdef VORBIS_PSYCHO
1032
   if (st->submodeID>=1)
1033
      SPEEX_COPY(st->old_curve, st->curve, 128);
1034
#endif
1035
1036
7.61k
   if (st->submodeID==1)
1037
3.96k
   {
1038
3.96k
#ifndef DISABLE_VBR
1039
3.96k
      if (st->dtx_count)
1040
1.62k
         speex_bits_pack(bits, 15, 4);
1041
2.33k
      else
1042
2.33k
#endif
1043
2.33k
         speex_bits_pack(bits, 0, 4);
1044
3.96k
   }
1045
1046
   /* The next frame will not be the first (Duh!) */
1047
7.61k
   st->first = 0;
1048
7.61k
   SPEEX_COPY(st->winBuf, in+2*NB_FRAME_SIZE-NB_WINDOW_SIZE, NB_WINDOW_SIZE-NB_FRAME_SIZE);
1049
1050
7.61k
   if (SUBMODE(innovation_quant) == noise_codebook_quant || st->submodeID==0)
1051
3.96k
      st->bounded_pitch = 1;
1052
3.65k
   else
1053
3.65k
      st->bounded_pitch = 0;
1054
1055
7.61k
   return 1;
1056
8.75k
}
nb_encode
Line
Count
Source
407
19.8k
{
408
19.8k
   EncState *st;
409
19.8k
   int i, sub, roots;
410
19.8k
   int ol_pitch;
411
19.8k
   spx_word16_t ol_pitch_coef;
412
19.8k
   spx_word32_t ol_gain;
413
19.8k
   VARDECL(spx_word16_t *target);
414
19.8k
   VARDECL(spx_sig_t *innov);
415
19.8k
   VARDECL(spx_word32_t *exc32);
416
19.8k
   VARDECL(spx_mem_t *mem);
417
19.8k
   VARDECL(spx_coef_t *bw_lpc1);
418
19.8k
   VARDECL(spx_coef_t *bw_lpc2);
419
19.8k
   VARDECL(spx_coef_t *lpc);
420
19.8k
   VARDECL(spx_lsp_t *lsp);
421
19.8k
   VARDECL(spx_lsp_t *qlsp);
422
19.8k
   VARDECL(spx_lsp_t *interp_lsp);
423
19.8k
   VARDECL(spx_lsp_t *interp_qlsp);
424
19.8k
   VARDECL(spx_coef_t *interp_lpc);
425
19.8k
   VARDECL(spx_coef_t *interp_qlpc);
426
19.8k
   char *stack;
427
19.8k
   VARDECL(spx_word16_t *syn_resp);
428
429
19.8k
   spx_word32_t ener=0;
430
19.8k
   spx_word16_t fine_gain;
431
19.8k
   spx_word16_t *in = (spx_word16_t*)vin;
432
433
19.8k
   st=(EncState *)state;
434
19.8k
   stack=st->stack;
435
436
19.8k
   ALLOC(lpc, NB_ORDER, spx_coef_t);
437
19.8k
   ALLOC(bw_lpc1, NB_ORDER, spx_coef_t);
438
19.8k
   ALLOC(bw_lpc2, NB_ORDER, spx_coef_t);
439
19.8k
   ALLOC(lsp, NB_ORDER, spx_lsp_t);
440
19.8k
   ALLOC(qlsp, NB_ORDER, spx_lsp_t);
441
19.8k
   ALLOC(interp_lsp, NB_ORDER, spx_lsp_t);
442
19.8k
   ALLOC(interp_qlsp, NB_ORDER, spx_lsp_t);
443
19.8k
   ALLOC(interp_lpc, NB_ORDER, spx_coef_t);
444
19.8k
   ALLOC(interp_qlpc, NB_ORDER, spx_coef_t);
445
446
19.8k
   st->exc = st->excBuf + NB_PITCH_END + 2;
447
19.8k
   st->sw = st->swBuf + NB_PITCH_END + 2;
448
   /* Move signals 1 frame towards the past */
449
19.8k
   SPEEX_MOVE(st->excBuf, st->excBuf+NB_FRAME_SIZE, NB_PITCH_END+2);
450
19.8k
   SPEEX_MOVE(st->swBuf, st->swBuf+NB_FRAME_SIZE, NB_PITCH_END+2);
451
452
19.8k
   if (st->highpass_enabled)
453
6.95k
      highpass(in, in, NB_FRAME_SIZE, (st->isWideband?HIGHPASS_WIDEBAND:HIGHPASS_NARROWBAND)|HIGHPASS_INPUT, st->mem_hp);
454
455
19.8k
   {
456
19.8k
      VARDECL(spx_word16_t *w_sig);
457
19.8k
      VARDECL(spx_word16_t *autocorr);
458
19.8k
      ALLOC(w_sig, NB_WINDOW_SIZE, spx_word16_t);
459
19.8k
      ALLOC(autocorr, NB_ORDER+1, spx_word16_t);
460
      /* Window for analysis */
461
815k
      for (i=0;i<NB_WINDOW_SIZE-NB_FRAME_SIZE;i++)
462
795k
         w_sig[i] = MULT16_16_Q15(st->winBuf[i],st->window[i]);
463
3.20M
      for (;i<NB_WINDOW_SIZE;i++)
464
3.18M
         w_sig[i] = MULT16_16_Q15(in[i-NB_WINDOW_SIZE+NB_FRAME_SIZE],st->window[i]);
465
      /* Compute auto-correlation */
466
19.8k
      _spx_autocorr(w_sig, autocorr, NB_ORDER+1, NB_WINDOW_SIZE);
467
19.8k
      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
238k
      for (i=0;i<NB_ORDER+1;i++)
471
218k
         autocorr[i] = MULT16_16_Q15(autocorr[i],st->lagWindow[i]);
472
19.8k
      autocorr[0] = ADD16(autocorr[0],1);
473
474
      /* Levinson-Durbin */
475
19.8k
      _spx_lpc(lpc, autocorr, NB_ORDER);
476
      /* LPC to LSPs (x-domain) transform */
477
19.8k
      roots=lpc_to_lsp (lpc, NB_ORDER, lsp, 10, LSP_DELTA1, stack);
478
      /* Check if we found all the roots */
479
19.8k
      if (roots!=NB_ORDER)
480
62
      {
481
         /*If we can't find all LSP's, do some damage control and use previous filter*/
482
682
         for (i=0;i<NB_ORDER;i++)
483
620
         {
484
620
            lsp[i]=st->old_lsp[i];
485
620
         }
486
62
      }
487
19.8k
   }
488
489
490
491
492
   /* Whole frame analysis (open-loop estimation of pitch and excitation gain) */
493
19.8k
   {
494
19.8k
      int diff = NB_WINDOW_SIZE-NB_FRAME_SIZE;
495
19.8k
      if (st->first)
496
40.1k
         for (i=0;i<NB_ORDER;i++)
497
36.5k
            interp_lsp[i] = lsp[i];
498
16.2k
      else
499
16.2k
         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
19.8k
      lsp_to_lpc(interp_lsp, interp_lpc, NB_ORDER,stack);
503
504
505
      /*Open-loop pitch*/
506
19.8k
      if (!st->submodes[st->submodeID] || (st->complexity>2 && SUBMODE(have_subframe_gain)<3) || SUBMODE(forced_pitch_gain) || SUBMODE(lbr_pitch) != -1
507
19.8k
#ifndef DISABLE_VBR
508
19.8k
           || st->vbr_enabled || st->vad_enabled
509
19.8k
#endif
510
19.8k
                  )
511
18.3k
      {
512
18.3k
         int nol_pitch[6];
513
18.3k
         spx_word16_t nol_pitch_coef[6];
514
515
18.3k
         bw_lpc(QCONST16(0.9,15), interp_lpc, bw_lpc1, NB_ORDER);
516
18.3k
         bw_lpc(QCONST16(0.55,15), interp_lpc, bw_lpc2, NB_ORDER);
517
518
18.3k
         SPEEX_COPY(st->sw, st->winBuf, diff);
519
18.3k
         SPEEX_COPY(st->sw+diff, in, NB_FRAME_SIZE-diff);
520
18.3k
         filter10(st->sw, bw_lpc1, bw_lpc2, st->sw, NB_FRAME_SIZE, st->mem_sw_whole, stack);
521
522
18.3k
         open_loop_nbest_pitch(st->sw, NB_PITCH_START, NB_PITCH_END, NB_FRAME_SIZE,
523
18.3k
                               nol_pitch, nol_pitch_coef, 6, stack);
524
18.3k
         ol_pitch=nol_pitch[0];
525
18.3k
         ol_pitch_coef = nol_pitch_coef[0];
526
         /*Try to remove pitch multiples*/
527
110k
         for (i=1;i<6;i++)
528
91.7k
         {
529
91.7k
#ifdef FIXED_POINT
530
91.7k
            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
91.7k
                (ABS(2*nol_pitch[i]-ol_pitch)<=2 || ABS(3*nol_pitch[i]-ol_pitch)<=3 ||
535
19.0k
                 ABS(4*nol_pitch[i]-ol_pitch)<=4 || ABS(5*nol_pitch[i]-ol_pitch)<=5))
536
877
            {
537
               /*ol_pitch_coef=nol_pitch_coef[i];*/
538
877
               ol_pitch = nol_pitch[i];
539
877
            }
540
91.7k
         }
541
         /*if (ol_pitch>50)
542
           ol_pitch/=2;*/
543
         /*ol_pitch_coef = sqrt(ol_pitch_coef);*/
544
545
18.3k
      } else {
546
1.53k
         ol_pitch=0;
547
1.53k
         ol_pitch_coef=0;
548
1.53k
      }
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
19.8k
      fir_mem16(st->winBuf, interp_lpc, st->exc, diff, NB_ORDER, st->mem_exc, stack);
554
19.8k
      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
19.8k
      {
558
19.8k
         spx_word16_t g = compute_rms16(st->exc, NB_FRAME_SIZE);
559
19.8k
         if (st->submodeID!=1 && ol_pitch>0)
560
9.01k
            ol_gain = MULT16_16(g, MULT16_16_Q14(QCONST16(1.1,14),
561
19.8k
                                spx_sqrt(QCONST32(1.,28)-MULT16_32_Q15(QCONST16(.8,15),SHL32(MULT16_16(ol_pitch_coef,ol_pitch_coef),16)))));
562
10.8k
         else
563
10.8k
            ol_gain = SHL32(EXTEND32(g),SIG_SHIFT);
564
19.8k
      }
565
19.8k
   }
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
19.8k
#ifndef DISABLE_VBR
578
19.8k
   if (st->vbr_enabled||st->vad_enabled)
579
14.8k
   {
580
14.8k
      float lsp_dist=0;
581
163k
      for (i=0;i<NB_ORDER;i++)
582
148k
         lsp_dist += (st->old_lsp[i] - lsp[i])*(st->old_lsp[i] - lsp[i]);
583
14.8k
      lsp_dist /= LSP_SCALING*LSP_SCALING;
584
585
14.8k
      if (st->abr_enabled)
586
3.57k
      {
587
3.57k
         float qual_change=0;
588
3.57k
         if (st->abr_drift2 * st->abr_drift > 0)
589
3.21k
         {
590
            /* Only adapt if long-term and short-term drift are the same sign */
591
3.21k
            qual_change = -.00001*st->abr_drift/(1+st->abr_count);
592
3.21k
            if (qual_change>.05)
593
0
               qual_change=.05;
594
3.21k
            if (qual_change<-.05)
595
944
               qual_change=-.05;
596
3.21k
         }
597
3.57k
         st->vbr_quality += qual_change;
598
3.57k
         if (st->vbr_quality>10)
599
0
            st->vbr_quality=10;
600
3.57k
         if (st->vbr_quality<0)
601
3.21k
            st->vbr_quality=0;
602
3.57k
      }
603
604
14.8k
      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
14.8k
      if (st->vbr_enabled)
608
11.6k
      {
609
11.6k
         spx_int32_t mode;
610
11.6k
         int choice=0;
611
11.6k
         float min_diff=100;
612
11.6k
         mode = 8;
613
104k
         while (mode)
614
93.0k
         {
615
93.0k
            int v1;
616
93.0k
            float thresh;
617
93.0k
            v1=(int)floor(st->vbr_quality);
618
93.0k
            if (v1==10)
619
4.10k
               thresh = vbr_nb_thresh[mode][v1];
620
88.9k
            else
621
88.9k
               thresh = (st->vbr_quality-v1)*vbr_nb_thresh[mode][v1+1] + (1+v1-st->vbr_quality)*vbr_nb_thresh[mode][v1];
622
93.0k
            if (st->relative_quality > thresh &&
623
93.0k
                st->relative_quality-thresh<min_diff)
624
13.2k
            {
625
13.2k
               choice = mode;
626
13.2k
               min_diff = st->relative_quality-thresh;
627
13.2k
            }
628
93.0k
            mode--;
629
93.0k
         }
630
11.6k
         mode=choice;
631
11.6k
         if (mode==0)
632
2.72k
         {
633
2.72k
            if (st->dtx_count==0 || lsp_dist>.05 || !st->dtx_enabled || st->dtx_count>20)
634
1.70k
            {
635
1.70k
               mode=1;
636
1.70k
               st->dtx_count=1;
637
1.70k
            } else {
638
1.02k
               mode=0;
639
1.02k
               st->dtx_count++;
640
1.02k
            }
641
8.89k
         } else {
642
8.89k
            st->dtx_count=0;
643
8.89k
         }
644
645
11.6k
         speex_encoder_ctl(state, SPEEX_SET_MODE, &mode);
646
11.6k
         if (st->vbr_max>0)
647
8.31k
         {
648
8.31k
            spx_int32_t rate;
649
8.31k
            speex_encoder_ctl(state, SPEEX_GET_BITRATE, &rate);
650
8.31k
            if (rate > st->vbr_max)
651
3.30k
            {
652
3.30k
               rate = st->vbr_max;
653
3.30k
               speex_encoder_ctl(state, SPEEX_SET_BITRATE, &rate);
654
3.30k
            }
655
8.31k
         }
656
657
11.6k
         if (st->abr_enabled)
658
3.57k
         {
659
3.57k
            spx_int32_t bitrate;
660
3.57k
            speex_encoder_ctl(state, SPEEX_GET_BITRATE, &bitrate);
661
3.57k
            st->abr_drift+=(bitrate-st->abr_enabled);
662
3.57k
            st->abr_drift2 = .95*st->abr_drift2 + .05*(bitrate-st->abr_enabled);
663
3.57k
            st->abr_count += 1.0;
664
3.57k
         }
665
666
11.6k
      } else {
667
         /*VAD only case*/
668
3.22k
         int mode;
669
3.22k
         if (st->relative_quality<2)
670
710
         {
671
710
            if (st->dtx_count==0 || lsp_dist>.05 || !st->dtx_enabled || st->dtx_count>20)
672
419
            {
673
419
               st->dtx_count=1;
674
419
               mode=1;
675
419
            } else {
676
291
               mode=0;
677
291
               st->dtx_count++;
678
291
            }
679
2.51k
         } else {
680
2.51k
            st->dtx_count = 0;
681
2.51k
            mode=st->submodeSelect;
682
2.51k
         }
683
         /*speex_encoder_ctl(state, SPEEX_SET_MODE, &mode);*/
684
3.22k
         st->submodeID=mode;
685
3.22k
      }
686
14.8k
   } else {
687
5.04k
      st->relative_quality = -1;
688
5.04k
   }
689
19.8k
#endif /* #ifndef DISABLE_VBR */
690
691
19.8k
   if (st->encode_submode)
692
19.8k
   {
693
      /* First, transmit a zero for narrowband */
694
19.8k
      speex_bits_pack(bits, 0, 1);
695
696
      /* Transmit the sub-mode we use for this frame */
697
19.8k
      speex_bits_pack(bits, st->submodeID, NB_SUBMODE_BITS);
698
699
19.8k
   }
700
701
   /* If null mode (no transmission), just set a couple things to zero*/
702
19.8k
   if (st->submodes[st->submodeID] == NULL)
703
1.23k
   {
704
198k
      for (i=0;i<NB_FRAME_SIZE;i++)
705
197k
         st->exc[i]=st->sw[i]=VERY_SMALL;
706
707
13.5k
      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.5k
      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
18.6k
   if (st->first)
723
2.78k
   {
724
30.6k
      for (i=0;i<NB_ORDER;i++)
725
27.8k
         st->old_lsp[i] = lsp[i];
726
2.78k
   }
727
728
729
   /*Quantize LSPs*/
730
18.6k
#if 1 /*0 for unquantized*/
731
18.6k
   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
18.6k
   if (SUBMODE(lbr_pitch)!=-1)
739
12.5k
   {
740
12.5k
      speex_bits_pack(bits, ol_pitch-NB_PITCH_START, 7);
741
12.5k
   }
742
743
18.6k
   if (SUBMODE(forced_pitch_gain))
744
11.3k
   {
745
11.3k
      int quant;
746
      /* This just damps the pitch a bit, because it tends to be too aggressive when forced */
747
11.3k
      ol_pitch_coef = MULT16_16_Q15(QCONST16(.9,15), ol_pitch_coef);
748
11.3k
#ifdef FIXED_POINT
749
11.3k
      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
11.3k
      if (quant>15)
754
82
         quant=15;
755
11.3k
      if (quant<0)
756
0
         quant=0;
757
11.3k
      speex_bits_pack(bits, quant, 4);
758
11.3k
      ol_pitch_coef=MULT16_16_P15(QCONST16(0.066667,15),SHL16(quant,GAIN_SHIFT));
759
11.3k
   }
760
761
762
   /*Quantize and transmit open-loop excitation gain*/
763
18.6k
#ifdef FIXED_POINT
764
18.6k
   {
765
18.6k
      int qe = scal_quant32(ol_gain, ol_gain_table, 32);
766
      /*ol_gain = exp(qe/3.5)*SIG_SCALING;*/
767
18.6k
      ol_gain = MULT16_32_Q15(28406,ol_gain_table[qe]);
768
18.6k
      speex_bits_pack(bits, qe, 5);
769
18.6k
   }
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
18.6k
   if (st->first)
786
2.78k
   {
787
30.6k
      for (i=0;i<NB_ORDER;i++)
788
27.8k
         st->old_qlsp[i] = qlsp[i];
789
2.78k
   }
790
791
   /* Target signal */
792
18.6k
   ALLOC(target, NB_SUBFRAME_SIZE, spx_word16_t);
793
18.6k
   ALLOC(innov, NB_SUBFRAME_SIZE, spx_sig_t);
794
18.6k
   ALLOC(exc32, NB_SUBFRAME_SIZE, spx_word32_t);
795
18.6k
   ALLOC(syn_resp, NB_SUBFRAME_SIZE, spx_word16_t);
796
18.6k
   ALLOC(mem, NB_ORDER, spx_mem_t);
797
798
   /* Loop on sub-frames */
799
93.2k
   for (sub=0;sub<NB_NB_SUBFRAMES;sub++)
800
74.6k
   {
801
74.6k
      int   offset;
802
74.6k
      spx_word16_t *sw;
803
74.6k
      spx_word16_t *exc, *inBuf;
804
74.6k
      int pitch;
805
74.6k
      int response_bound = NB_SUBFRAME_SIZE;
806
807
      /* Offset relative to start of frame */
808
74.6k
      offset = NB_SUBFRAME_SIZE*sub;
809
      /* Excitation */
810
74.6k
      exc=st->exc+offset;
811
      /* Weighted signal */
812
74.6k
      sw=st->sw+offset;
813
814
      /* LSP interpolation (quantized and unquantized) */
815
74.6k
      lsp_interpolate(st->old_lsp, lsp, interp_lsp, NB_ORDER, sub, NB_NB_SUBFRAMES, LSP_MARGIN);
816
74.6k
      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
74.6k
      lsp_to_lpc(interp_lsp, interp_lpc, NB_ORDER,stack);
820
821
74.6k
      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
74.6k
      {
825
74.6k
         spx_word32_t pi_g=LPC_SCALING;
826
447k
         for (i=0;i<NB_ORDER;i+=2)
827
373k
         {
828
            /*pi_g += -st->interp_qlpc[i] +  st->interp_qlpc[i+1];*/
829
373k
            pi_g = ADD32(pi_g, SUB32(EXTEND32(interp_qlpc[i+1]),EXTEND32(interp_qlpc[i])));
830
373k
         }
831
74.6k
         st->pi_gain[sub] = pi_g;
832
74.6k
      }
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
74.6k
      bw_lpc(st->gamma1, interp_lpc, bw_lpc1, NB_ORDER);
845
74.6k
      bw_lpc(st->gamma2, interp_lpc, bw_lpc2, NB_ORDER);
846
      /*print_vec(st->bw_lpc1, 10, "bw_lpc");*/
847
74.6k
#endif
848
849
      /*FIXME: This will break if we change the window size */
850
74.6k
      speex_assert(NB_WINDOW_SIZE-NB_FRAME_SIZE == NB_SUBFRAME_SIZE);
851
74.6k
      if (sub==0)
852
18.6k
         inBuf = st->winBuf;
853
55.9k
      else
854
55.9k
         inBuf = &in[((sub-1)*NB_SUBFRAME_SIZE)];
855
3.05M
      for (i=0;i<NB_SUBFRAME_SIZE;i++)
856
2.98M
         sw[i] = inBuf[i];
857
858
74.6k
      if (st->complexity==0)
859
17.1k
         response_bound >>= 1;
860
74.6k
      compute_impulse_response(interp_qlpc, bw_lpc1, bw_lpc2, syn_resp, response_bound, NB_ORDER, stack);
861
418k
      for (i=response_bound;i<NB_SUBFRAME_SIZE;i++)
862
343k
         syn_resp[i]=VERY_SMALL;
863
864
      /* Compute zero response of A(z/g1) / ( A(z/g2) * A(z) ) */
865
820k
      for (i=0;i<NB_ORDER;i++)
866
746k
         mem[i]=SHL32(st->mem_sp[i],1);
867
3.05M
      for (i=0;i<NB_SUBFRAME_SIZE;i++)
868
2.98M
         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
74.6k
      iir_mem16(exc, interp_qlpc, exc, NB_SUBFRAME_SIZE, NB_ORDER, mem, stack);
877
820k
      for (i=0;i<NB_ORDER;i++)
878
746k
         mem[i]=SHL32(st->mem_sw[i],1);
879
74.6k
      filter10(exc, bw_lpc1, bw_lpc2, exc, NB_SUBFRAME_SIZE, mem, stack);
880
74.6k
#endif
881
882
      /* Compute weighted signal */
883
820k
      for (i=0;i<NB_ORDER;i++)
884
746k
         mem[i]=st->mem_sw[i];
885
74.6k
      filter10(sw, bw_lpc1, bw_lpc2, sw, NB_SUBFRAME_SIZE, mem, stack);
886
887
74.6k
      if (st->complexity==0)
888
189k
         for (i=0;i<NB_ORDER;i++)
889
171k
            st->mem_sw[i]=mem[i];
890
891
      /* Compute target signal (saturation prevents overflows on clipped input speech) */
892
3.05M
      for (i=0;i<NB_SUBFRAME_SIZE;i++)
893
2.98M
         target[i]=EXTRACT16(SATURATE(SUB32(sw[i],PSHR32(exc[i],1)),32767));
894
895
3.05M
      for (i=0;i<NB_SUBFRAME_SIZE;i++)
896
2.98M
         exc[i] = inBuf[i];
897
74.6k
      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
74.6k
      speex_assert (SUBMODE(ltp_quant));
900
74.6k
      {
901
74.6k
         int pit_min, pit_max;
902
         /* Long-term prediction */
903
74.6k
         if (SUBMODE(lbr_pitch) != -1)
904
50.3k
         {
905
            /* Low bit-rate pitch handling */
906
50.3k
            int margin;
907
50.3k
            margin = SUBMODE(lbr_pitch);
908
50.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
50.3k
            } else {
917
50.3k
               pit_min=pit_max=ol_pitch;
918
50.3k
            }
919
50.3k
         } else {
920
24.2k
            pit_min = NB_PITCH_START;
921
24.2k
            pit_max = NB_PITCH_END;
922
24.2k
         }
923
924
         /* Force pitch to use only the current frame if needed */
925
74.6k
         if (st->bounded_pitch && pit_max>offset)
926
22.4k
            pit_max=offset;
927
928
         /* Perform pitch search */
929
74.6k
         pitch = SUBMODE(ltp_quant)(target, sw, interp_qlpc, bw_lpc1, bw_lpc2,
930
74.6k
                                    exc32, SUBMODE(ltp_params), pit_min, pit_max, ol_pitch_coef,
931
74.6k
                                    NB_ORDER, NB_SUBFRAME_SIZE, bits, stack,
932
74.6k
                                    exc, syn_resp, st->complexity, 0, st->plc_tuning, &st->cumul_gain);
933
934
74.6k
         st->pitch[sub]=pitch;
935
74.6k
      }
936
      /* Quantization of innovation */
937
74.6k
      SPEEX_MEMSET(innov, 0, NB_SUBFRAME_SIZE);
938
939
      /* FIXME: Make sure this is safe from overflows (so far so good) */
940
3.05M
      for (i=0;i<NB_SUBFRAME_SIZE;i++)
941
2.98M
         exc[i] = EXTRACT16(SUB32(EXTEND32(exc[i]), PSHR32(exc32[i],SIG_SHIFT-1)));
942
943
74.6k
      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
74.6k
#ifdef FIXED_POINT
947
74.6k
      {
948
74.6k
         spx_word32_t f = PDIV32(ener,PSHR32(ol_gain,SIG_SHIFT));
949
74.6k
         if (f<=32767)
950
69.1k
            fine_gain = f;
951
5.49k
         else
952
5.49k
            fine_gain = 32767;
953
74.6k
      }
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
74.6k
      if (SUBMODE(have_subframe_gain))
959
24.2k
      {
960
24.2k
         int qe;
961
24.2k
         if (SUBMODE(have_subframe_gain)==3)
962
13.6k
         {
963
13.6k
            qe = scal_quant(fine_gain, exc_gain_quant_scal3_bound, 8);
964
13.6k
            speex_bits_pack(bits, qe, 3);
965
13.6k
            ener=MULT16_32_Q14(exc_gain_quant_scal3[qe],ol_gain);
966
13.6k
         } else {
967
10.5k
            qe = scal_quant(fine_gain, exc_gain_quant_scal1_bound, 2);
968
10.5k
            speex_bits_pack(bits, qe, 1);
969
10.5k
            ener=MULT16_32_Q14(exc_gain_quant_scal1[qe],ol_gain);
970
10.5k
         }
971
50.3k
      } else {
972
50.3k
         ener=ol_gain;
973
50.3k
      }
974
975
      /*printf ("%f %f\n", ener, ol_gain);*/
976
977
      /* Normalize innovation */
978
74.6k
      signal_div(target, target, ener, NB_SUBFRAME_SIZE);
979
980
      /* Quantize innovation */
981
74.6k
      speex_assert (SUBMODE(innovation_quant));
982
74.6k
      {
983
         /* Codebook search */
984
74.6k
         SUBMODE(innovation_quant)(target, interp_qlpc, bw_lpc1, bw_lpc2,
985
74.6k
                  SUBMODE(innovation_params), NB_ORDER, NB_SUBFRAME_SIZE,
986
74.6k
                  innov, syn_resp, bits, stack, st->complexity, SUBMODE(double_codebook));
987
988
         /* De-normalize innovation and update excitation */
989
74.6k
         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
74.6k
         if (SUBMODE(double_codebook)) {
993
2.08k
            char *tmp_stack=stack;
994
2.08k
            VARDECL(spx_sig_t *innov2);
995
2.08k
            ALLOC(innov2, NB_SUBFRAME_SIZE, spx_sig_t);
996
2.08k
            SPEEX_MEMSET(innov2, 0, NB_SUBFRAME_SIZE);
997
85.4k
            for (i=0;i<NB_SUBFRAME_SIZE;i++)
998
83.3k
               target[i]=MULT16_16_P13(QCONST16(2.2f,13), target[i]);
999
2.08k
            SUBMODE(innovation_quant)(target, interp_qlpc, bw_lpc1, bw_lpc2,
1000
2.08k
                                      SUBMODE(innovation_params), NB_ORDER, NB_SUBFRAME_SIZE,
1001
2.08k
                                      innov2, syn_resp, bits, stack, st->complexity, 0);
1002
2.08k
            signal_mul(innov2, innov2, MULT16_32_Q15(QCONST16(0.454545f,15),ener), NB_SUBFRAME_SIZE);
1003
85.4k
            for (i=0;i<NB_SUBFRAME_SIZE;i++)
1004
83.3k
               innov[i] = ADD32(innov[i],innov2[i]);
1005
2.08k
            stack = tmp_stack;
1006
2.08k
         }
1007
3.05M
         for (i=0;i<NB_SUBFRAME_SIZE;i++)
1008
2.98M
            exc[i] = EXTRACT16(SATURATE32(PSHR32(ADD32(SHL32(exc32[i],1),innov[i]),SIG_SHIFT),32767));
1009
74.6k
         if (st->innov_rms_save)
1010
20.4k
            st->innov_rms_save[sub] = compute_rms(innov, NB_SUBFRAME_SIZE);
1011
74.6k
      }
1012
1013
      /* Final signal synthesis from excitation */
1014
74.6k
      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
74.6k
      if (st->complexity!=0)
1018
57.4k
         filter10(sw, bw_lpc1, bw_lpc2, sw, NB_SUBFRAME_SIZE, st->mem_sw, stack);
1019
1020
74.6k
   }
1021
1022
   /* Store the LSPs for interpolation in the next frame */
1023
18.6k
   if (st->submodeID>=1)
1024
18.6k
   {
1025
205k
      for (i=0;i<NB_ORDER;i++)
1026
186k
         st->old_lsp[i] = lsp[i];
1027
205k
      for (i=0;i<NB_ORDER;i++)
1028
186k
         st->old_qlsp[i] = qlsp[i];
1029
18.6k
   }
1030
1031
#ifdef VORBIS_PSYCHO
1032
   if (st->submodeID>=1)
1033
      SPEEX_COPY(st->old_curve, st->curve, 128);
1034
#endif
1035
1036
18.6k
   if (st->submodeID==1)
1037
9.14k
   {
1038
9.14k
#ifndef DISABLE_VBR
1039
9.14k
      if (st->dtx_count)
1040
2.20k
         speex_bits_pack(bits, 15, 4);
1041
6.94k
      else
1042
6.94k
#endif
1043
6.94k
         speex_bits_pack(bits, 0, 4);
1044
9.14k
   }
1045
1046
   /* The next frame will not be the first (Duh!) */
1047
18.6k
   st->first = 0;
1048
18.6k
   SPEEX_COPY(st->winBuf, in+2*NB_FRAME_SIZE-NB_WINDOW_SIZE, NB_WINDOW_SIZE-NB_FRAME_SIZE);
1049
1050
18.6k
   if (SUBMODE(innovation_quant) == noise_codebook_quant || st->submodeID==0)
1051
9.14k
      st->bounded_pitch = 1;
1052
9.51k
   else
1053
9.51k
      st->bounded_pitch = 0;
1054
1055
18.6k
   return 1;
1056
19.8k
}
1057
#endif /* DISABLE_ENCODER */
1058
1059
1060
#ifndef DISABLE_DECODER
1061
void *nb_decoder_init(const SpeexMode *m)
1062
3.83k
{
1063
3.83k
   DecState *st;
1064
3.83k
   const SpeexNBMode *mode;
1065
3.83k
   int i;
1066
1067
3.83k
   mode=(const SpeexNBMode*)m->mode;
1068
3.83k
   st = (DecState *)speex_alloc(sizeof(DecState));
1069
3.83k
   if (!st)
1070
0
      return NULL;
1071
3.83k
#if defined(VAR_ARRAYS) || defined (USE_ALLOCA)
1072
3.83k
   st->stack = NULL;
1073
#else
1074
   st->stack = (char*)speex_alloc_scratch(NB_DEC_STACK);
1075
#endif
1076
1077
3.83k
   st->mode=m;
1078
1079
1080
3.83k
   st->encode_submode = 1;
1081
1082
3.83k
   st->first=1;
1083
   /* Codec parameters, should eventually have several "modes"*/
1084
1085
3.83k
   st->submodes=mode->submodes;
1086
3.83k
   st->submodeID=mode->defaultSubmode;
1087
1088
3.83k
   st->lpc_enh_enabled=1;
1089
1090
3.83k
   SPEEX_MEMSET(st->excBuf, 0, NB_FRAME_SIZE + NB_PITCH_END);
1091
1092
3.83k
   st->last_pitch = 40;
1093
3.83k
   st->count_lost=0;
1094
3.83k
   st->pitch_gain_buf[0] = st->pitch_gain_buf[1] = st->pitch_gain_buf[2] = 0;
1095
3.83k
   st->pitch_gain_buf_idx = 0;
1096
3.83k
   st->seed = 1000;
1097
1098
3.83k
   st->sampling_rate=8000;
1099
3.83k
   st->last_ol_gain = 0;
1100
1101
3.83k
   st->user_callback.func = &speex_default_user_handler;
1102
3.83k
   st->user_callback.data = NULL;
1103
65.1k
   for (i=0;i<16;i++)
1104
61.2k
      st->speex_callbacks[i].func = NULL;
1105
1106
3.83k
   st->voc_m1=st->voc_m2=st->voc_mean=0;
1107
3.83k
   st->voc_offset=0;
1108
3.83k
   st->dtx_enabled=0;
1109
3.83k
   st->isWideband = 0;
1110
3.83k
   st->highpass_enabled = 1;
1111
1112
#ifdef ENABLE_VALGRIND
1113
   VALGRIND_MAKE_MEM_DEFINED(st, NB_DEC_STACK);
1114
#endif
1115
3.83k
   return st;
1116
3.83k
}
1117
1118
void nb_decoder_destroy(void *state)
1119
3.83k
{
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
3.83k
   speex_free(state);
1128
3.83k
}
1129
1130
int nb_decoder_ctl(void *state, int request, void *ptr)
1131
176k
{
1132
176k
   DecState *st;
1133
176k
   st=(DecState*)state;
1134
176k
   switch(request)
1135
176k
   {
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
3.83k
   case SPEEX_SET_ENH:
1145
3.83k
      st->lpc_enh_enabled = *((spx_int32_t*)ptr);
1146
3.83k
      break;
1147
0
   case SPEEX_GET_ENH:
1148
0
      *((spx_int32_t*)ptr) = st->lpc_enh_enabled;
1149
0
      break;
1150
9.59k
   case SPEEX_GET_FRAME_SIZE:
1151
9.59k
      (*(spx_int32_t*)ptr) = NB_FRAME_SIZE;
1152
9.59k
      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
3.83k
   case SPEEX_SET_SAMPLING_RATE:
1160
3.83k
      st->sampling_rate = (*(spx_int32_t*)ptr);
1161
3.83k
      break;
1162
2.63k
   case SPEEX_GET_SAMPLING_RATE:
1163
2.63k
      (*(spx_int32_t*)ptr)=st->sampling_rate;
1164
2.63k
      break;
1165
1.96k
   case SPEEX_SET_HANDLER:
1166
1.96k
      {
1167
1.96k
         SpeexCallback *c = (SpeexCallback*)ptr;
1168
1.96k
         st->speex_callbacks[c->callback_id].func=c->func;
1169
1.96k
         st->speex_callbacks[c->callback_id].data=c->data;
1170
1.96k
         st->speex_callbacks[c->callback_id].callback_id=c->callback_id;
1171
1.96k
      }
1172
1.96k
      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.66k
   case SPEEX_GET_LOOKAHEAD:
1197
3.66k
      (*(spx_int32_t*)ptr)=NB_SUBFRAME_SIZE;
1198
3.66k
      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.67k
   case SPEEX_GET_PI_GAIN:
1222
4.67k
      {
1223
4.67k
         int i;
1224
4.67k
         spx_word32_t *g = (spx_word32_t*)ptr;
1225
23.3k
         for (i=0;i<NB_NB_SUBFRAMES;i++)
1226
18.7k
            g[i]=st->pi_gain[i];
1227
4.67k
      }
1228
4.67k
      break;
1229
4.67k
   case SPEEX_GET_EXC:
1230
4.67k
      {
1231
4.67k
         int i;
1232
23.3k
         for (i=0;i<NB_NB_SUBFRAMES;i++)
1233
18.7k
            ((spx_word16_t*)ptr)[i] = compute_rms16(st->exc+i*NB_SUBFRAME_SIZE, NB_SUBFRAME_SIZE);
1234
4.67k
      }
1235
4.67k
      break;
1236
86.4k
   case SPEEX_GET_DTX_STATUS:
1237
86.4k
      *((spx_int32_t*)ptr) = st->dtx_enabled;
1238
86.4k
      break;
1239
50.7k
   case SPEEX_SET_INNOVATION_SAVE:
1240
50.7k
      st->innov_save = (spx_word16_t*)ptr;
1241
50.7k
      break;
1242
4.31k
   case SPEEX_SET_WIDEBAND:
1243
4.31k
      st->isWideband = *((spx_int32_t*)ptr);
1244
4.31k
      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
176k
   }
1252
176k
   return 0;
1253
176k
}
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
67.9k
{
1332
67.9k
   DecState *st;
1333
67.9k
   int i, sub;
1334
67.9k
   int pitch;
1335
67.9k
   spx_word16_t pitch_gain[3];
1336
67.9k
   spx_word32_t ol_gain=0;
1337
67.9k
   int ol_pitch=0;
1338
67.9k
   spx_word16_t ol_pitch_coef=0;
1339
67.9k
   int best_pitch=40;
1340
67.9k
   spx_word16_t best_pitch_gain=0;
1341
67.9k
   int wideband;
1342
67.9k
   int m;
1343
67.9k
   char *stack;
1344
67.9k
   VARDECL(spx_sig_t *innov);
1345
67.9k
   VARDECL(spx_word32_t *exc32);
1346
67.9k
   VARDECL(spx_coef_t *ak);
1347
67.9k
   VARDECL(spx_lsp_t *qlsp);
1348
67.9k
   spx_word16_t pitch_average=0;
1349
1350
67.9k
   spx_word16_t *out = (spx_word16_t*)vout;
1351
67.9k
   VARDECL(spx_lsp_t *interp_qlsp);
1352
1353
67.9k
   st=(DecState*)state;
1354
67.9k
   stack=st->stack;
1355
1356
67.9k
   st->exc = st->excBuf + 2*NB_PITCH_END + NB_SUBFRAME_SIZE + 6;
1357
1358
   /* Check if we're in DTX mode*/
1359
67.9k
   if (!bits && st->dtx_enabled)
1360
0
   {
1361
0
      st->submodeID=0;
1362
0
   } else
1363
67.9k
   {
1364
      /* If bits is NULL, consider the packet to be lost (what could we do anyway) */
1365
67.9k
      if (!bits)
1366
0
      {
1367
0
         nb_decode_lost(st, out, stack);
1368
0
         return 0;
1369
0
      }
1370
1371
67.9k
      if (st->encode_submode)
1372
67.9k
      {
1373
1374
      /* Search for next narrowband block (handle requests, skip wideband blocks) */
1375
73.2k
      do {
1376
73.2k
         if (speex_bits_remaining(bits)<5)
1377
13.3k
            return -1;
1378
59.8k
         wideband = speex_bits_unpack_unsigned(bits, 1);
1379
59.8k
         if (wideband) /* Skip wideband block (for compatibility) */
1380
4.43k
         {
1381
4.43k
            int submode;
1382
4.43k
            int advance;
1383
4.43k
            advance = submode = speex_bits_unpack_unsigned(bits, SB_SUBMODE_BITS);
1384
            /*speex_mode_query(&speex_wb_mode, SPEEX_SUBMODE_BITS_PER_FRAME, &advance);*/
1385
4.43k
            advance = wb_skip_table[submode];
1386
4.43k
            if (advance < 0)
1387
0
            {
1388
0
               speex_notify("Invalid mode encountered. The stream is corrupted.");
1389
0
               return -2;
1390
0
            }
1391
4.43k
            advance -= (SB_SUBMODE_BITS+1);
1392
4.43k
            speex_bits_advance(bits, advance);
1393
1394
4.43k
            if (speex_bits_remaining(bits)<5)
1395
1.11k
               return -1;
1396
3.32k
            wideband = speex_bits_unpack_unsigned(bits, 1);
1397
3.32k
            if (wideband)
1398
2.57k
            {
1399
2.57k
               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.57k
               advance = wb_skip_table[submode];
1402
2.57k
               if (advance < 0)
1403
0
               {
1404
0
                  speex_notify("Invalid mode encountered. The stream is corrupted.");
1405
0
                  return -2;
1406
0
               }
1407
2.57k
               advance -= (SB_SUBMODE_BITS+1);
1408
2.57k
               speex_bits_advance(bits, advance);
1409
2.57k
               wideband = speex_bits_unpack_unsigned(bits, 1);
1410
2.57k
               if (wideband)
1411
2.13k
               {
1412
2.13k
                  speex_notify("More than two wideband layers found. The stream is corrupted.");
1413
2.13k
                  return -2;
1414
2.13k
               }
1415
1416
2.57k
            }
1417
3.32k
         }
1418
56.6k
         if (speex_bits_remaining(bits)<4)
1419
424
            return -1;
1420
         /* FIXME: Check for overflow */
1421
56.2k
         m = speex_bits_unpack_unsigned(bits, 4);
1422
56.2k
         if (m==15) /* We found a terminator */
1423
752
         {
1424
752
            return -1;
1425
55.4k
         } else if (m==14) /* Speex in-band request */
1426
4.43k
         {
1427
4.43k
            int ret = speex_inband_handler(bits, st->speex_callbacks, state);
1428
4.43k
            if (ret)
1429
0
               return ret;
1430
51.0k
         } else if (m==13) /* User in-band request */
1431
826
         {
1432
826
            int ret = st->user_callback.func(bits, state, st->user_callback.data);
1433
826
            if (ret)
1434
0
               return ret;
1435
50.2k
         } else if (m>8) /* Invalid mode */
1436
3.43k
         {
1437
3.43k
            speex_notify("Invalid mode encountered. The stream is corrupted.");
1438
3.43k
            return -2;
1439
3.43k
         }
1440
1441
56.2k
      } while (m>8);
1442
1443
      /* Get the sub-mode that was used */
1444
46.7k
      st->submodeID = m;
1445
46.7k
      }
1446
1447
67.9k
   }
1448
1449
   /* Shift all buffers by one frame */
1450
46.7k
   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
46.7k
   if (st->submodes[st->submodeID] == NULL)
1454
22.5k
   {
1455
22.5k
      VARDECL(spx_coef_t *lpc);
1456
22.5k
      ALLOC(lpc, NB_ORDER, spx_coef_t);
1457
22.5k
      bw_lpc(QCONST16(0.93f,15), st->interp_qlpc, lpc, NB_ORDER);
1458
22.5k
      {
1459
22.5k
         spx_word16_t innov_gain=0;
1460
         /* FIXME: This was innov, not exc */
1461
22.5k
         innov_gain = compute_rms16(st->exc, NB_FRAME_SIZE);
1462
3.63M
         for (i=0;i<NB_FRAME_SIZE;i++)
1463
3.60M
            st->exc[i]=speex_rand(innov_gain, &st->seed);
1464
22.5k
      }
1465
1466
1467
22.5k
      st->first=1;
1468
1469
      /* Final signal synthesis from excitation */
1470
22.5k
      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
22.5k
      if (st->innov_save)
1475
18.6k
         SPEEX_MEMSET(st->innov_save, 0, NB_NB_SUBFRAMES*NB_SUBFRAME_SIZE);
1476
1477
22.5k
      st->count_lost=0;
1478
22.5k
      return 0;
1479
22.5k
   }
1480
1481
24.2k
   ALLOC(qlsp, NB_ORDER, spx_lsp_t);
1482
1483
   /* Unquantize LSPs */
1484
24.2k
   SUBMODE(lsp_unquant)(qlsp, NB_ORDER, bits);
1485
1486
   /*Damp memory if a frame was lost and the LSP changed too much*/
1487
24.2k
   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.2k
   if (st->first || st->count_lost)
1505
4.81k
   {
1506
53.0k
      for (i=0;i<NB_ORDER;i++)
1507
48.1k
         st->old_qlsp[i] = qlsp[i];
1508
4.81k
   }
1509
1510
   /* Get open-loop pitch estimation for low bit-rate pitch coding */
1511
24.2k
   if (SUBMODE(lbr_pitch)!=-1)
1512
10.0k
   {
1513
10.0k
      ol_pitch = NB_PITCH_START+speex_bits_unpack_unsigned(bits, 7);
1514
10.0k
   }
1515
1516
24.2k
   if (SUBMODE(forced_pitch_gain))
1517
8.22k
   {
1518
8.22k
      int quant;
1519
8.22k
      quant = speex_bits_unpack_unsigned(bits, 4);
1520
8.22k
      ol_pitch_coef=MULT16_16_P15(QCONST16(0.066667,15),SHL16(quant,GAIN_SHIFT));
1521
8.22k
   }
1522
1523
   /* Get global excitation gain */
1524
24.2k
   {
1525
24.2k
      int qe;
1526
24.2k
      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.0k
      ol_gain = MULT16_32_Q15(28406,ol_gain_table[qe]);
1530
#else
1531
11.1k
      ol_gain = SIG_SCALING*exp(qe/3.5);
1532
#endif
1533
24.2k
   }
1534
1535
24.2k
   ALLOC(ak, NB_ORDER, spx_coef_t);
1536
24.2k
   ALLOC(innov, NB_SUBFRAME_SIZE, spx_sig_t);
1537
24.2k
   ALLOC(exc32, NB_SUBFRAME_SIZE, spx_word32_t);
1538
1539
24.2k
   if (st->submodeID==1)
1540
3.82k
   {
1541
3.82k
      int extra;
1542
3.82k
      extra = speex_bits_unpack_unsigned(bits, 4);
1543
1544
3.82k
      if (extra==15)
1545
606
         st->dtx_enabled=1;
1546
3.21k
      else
1547
3.21k
         st->dtx_enabled=0;
1548
3.82k
   }
1549
24.2k
   if (st->submodeID>1)
1550
20.3k
      st->dtx_enabled=0;
1551
1552
   /*Loop on subframes */
1553
121k
   for (sub=0;sub<NB_NB_SUBFRAMES;sub++)
1554
96.8k
   {
1555
96.8k
      int offset;
1556
96.8k
      spx_word16_t *exc;
1557
96.8k
      spx_word16_t *innov_save = NULL;
1558
96.8k
      spx_word16_t tmp;
1559
1560
      /* Offset relative to start of frame */
1561
96.8k
      offset = NB_SUBFRAME_SIZE*sub;
1562
      /* Excitation */
1563
96.8k
      exc=st->exc+offset;
1564
      /* Original signal */
1565
96.8k
      if (st->innov_save)
1566
66.7k
         innov_save = st->innov_save+offset;
1567
1568
1569
      /* Reset excitation */
1570
96.8k
      SPEEX_MEMSET(exc, 0, NB_SUBFRAME_SIZE);
1571
1572
      /*Adaptive codebook contribution*/
1573
96.8k
      speex_assert (SUBMODE(ltp_unquant));
1574
96.8k
      {
1575
96.8k
         int pit_min, pit_max;
1576
         /* Handle pitch constraints if any */
1577
96.8k
         if (SUBMODE(lbr_pitch) != -1)
1578
40.1k
         {
1579
40.1k
            int margin;
1580
40.1k
            margin = SUBMODE(lbr_pitch);
1581
40.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
40.1k
            } else {
1598
40.1k
               pit_min = pit_max = ol_pitch;
1599
40.1k
            }
1600
56.7k
         } else {
1601
56.7k
            pit_min = NB_PITCH_START;
1602
56.7k
            pit_max = NB_PITCH_END;
1603
56.7k
         }
1604
1605
1606
1607
96.8k
         SUBMODE(ltp_unquant)(exc, exc32, pit_min, pit_max, ol_pitch_coef, SUBMODE(ltp_params),
1608
96.8k
                 NB_SUBFRAME_SIZE, &pitch, &pitch_gain[0], bits, stack,
1609
96.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
96.8k
         sanitize_values32(exc32, NEG32(QCONST32(32000,SIG_SHIFT-1)), QCONST32(32000,SIG_SHIFT-1), NB_SUBFRAME_SIZE);
1615
1616
96.8k
         tmp = gain_3tap_to_1tap(pitch_gain);
1617
1618
96.8k
         pitch_average += tmp;
1619
96.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
96.8k
              || (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
96.8k
              || (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.5k
         {
1623
14.5k
            best_pitch = pitch;
1624
14.5k
            if (tmp > best_pitch_gain)
1625
13.3k
               best_pitch_gain = tmp;
1626
14.5k
         }
1627
96.8k
      }
1628
1629
      /* Unquantize the innovation */
1630
96.8k
      {
1631
96.8k
         int q_energy;
1632
96.8k
         spx_word32_t ener;
1633
1634
96.8k
         SPEEX_MEMSET(innov, 0, NB_SUBFRAME_SIZE);
1635
1636
         /* Decode sub-frame gain correction */
1637
96.8k
         if (SUBMODE(have_subframe_gain)==3)
1638
22.1k
         {
1639
22.1k
            q_energy = speex_bits_unpack_unsigned(bits, 3);
1640
22.1k
            ener = MULT16_32_Q14(exc_gain_quant_scal3[q_energy],ol_gain);
1641
74.7k
         } else if (SUBMODE(have_subframe_gain)==1)
1642
34.5k
         {
1643
34.5k
            q_energy = speex_bits_unpack_unsigned(bits, 1);
1644
34.5k
            ener = MULT16_32_Q14(exc_gain_quant_scal1[q_energy],ol_gain);
1645
40.1k
         } else {
1646
40.1k
            ener = ol_gain;
1647
40.1k
         }
1648
1649
96.8k
         speex_assert (SUBMODE(innovation_unquant));
1650
96.8k
         {
1651
            /*Fixed codebook contribution*/
1652
96.8k
            SUBMODE(innovation_unquant)(innov, SUBMODE(innovation_params), NB_SUBFRAME_SIZE, bits, stack, &st->seed);
1653
            /* De-normalize innovation and update excitation */
1654
1655
96.8k
            signal_mul(innov, innov, ener, NB_SUBFRAME_SIZE);
1656
1657
            /* Decode second codebook (only for some modes) */
1658
96.8k
            if (SUBMODE(double_codebook))
1659
6.96k
            {
1660
6.96k
               char *tmp_stack=stack;
1661
6.96k
               VARDECL(spx_sig_t *innov2);
1662
6.96k
               ALLOC(innov2, NB_SUBFRAME_SIZE, spx_sig_t);
1663
6.96k
               SPEEX_MEMSET(innov2, 0, NB_SUBFRAME_SIZE);
1664
6.96k
               SUBMODE(innovation_unquant)(innov2, SUBMODE(innovation_params), NB_SUBFRAME_SIZE, bits, stack, &st->seed);
1665
6.96k
               signal_mul(innov2, innov2, MULT16_32_Q15(QCONST16(0.454545f,15),ener), NB_SUBFRAME_SIZE);
1666
285k
               for (i=0;i<NB_SUBFRAME_SIZE;i++)
1667
278k
                  innov[i] = ADD32(innov[i], innov2[i]);
1668
6.96k
               stack = tmp_stack;
1669
6.96k
            }
1670
3.97M
            for (i=0;i<NB_SUBFRAME_SIZE;i++)
1671
3.87M
               exc[i]=EXTRACT16(SATURATE32(PSHR32(ADD32(SHL32(exc32[i],1),innov[i]),SIG_SHIFT),32767));
1672
            /*print_vec(exc, 40, "innov");*/
1673
96.8k
            if (innov_save)
1674
66.7k
            {
1675
2.73M
               for (i=0;i<NB_SUBFRAME_SIZE;i++)
1676
2.66M
                  innov_save[i] = EXTRACT16(PSHR32(innov[i], SIG_SHIFT));
1677
66.7k
            }
1678
96.8k
         }
1679
1680
         /*Vocoder mode*/
1681
96.8k
         if (st->submodeID==1)
1682
15.2k
         {
1683
15.2k
            spx_word16_t g=ol_pitch_coef;
1684
15.2k
            g=MULT16_16_P14(QCONST16(1.5f,14),(g-QCONST16(.2f,6)));
1685
15.2k
            if (g<0)
1686
9.12k
               g=0;
1687
15.2k
            if (g>GAIN_SCALING)
1688
1.78k
               g=GAIN_SCALING;
1689
1690
15.2k
            SPEEX_MEMSET(exc, 0, NB_SUBFRAME_SIZE);
1691
36.6k
            while (st->voc_offset<NB_SUBFRAME_SIZE)
1692
21.3k
            {
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
21.3k
               if (st->voc_offset>=0)
1696
21.3k
                  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
21.3k
               st->voc_offset+=ol_pitch;
1698
21.3k
            }
1699
15.2k
            st->voc_offset -= NB_SUBFRAME_SIZE;
1700
1701
626k
            for (i=0;i<NB_SUBFRAME_SIZE;i++)
1702
611k
            {
1703
611k
               spx_word16_t exci=exc[i];
1704
611k
               exc[i]= ADD16(ADD16(MULT16_16_Q15(QCONST16(.7f,15),exc[i]) , MULT16_16_Q15(QCONST16(.3f,15),st->voc_m1)),
1705
611k
                             SUB16(MULT16_16_Q15(Q15_ONE-MULT16_16_16(QCONST16(.85f,9),g),EXTRACT16(PSHR32(innov[i],SIG_SHIFT))),
1706
611k
                                   MULT16_16_Q15(MULT16_16_16(QCONST16(.15f,9),g),EXTRACT16(PSHR32(st->voc_m2,SIG_SHIFT)))
1707
611k
                                  ));
1708
611k
               st->voc_m1 = exci;
1709
611k
               st->voc_m2=innov[i];
1710
611k
               st->voc_mean = EXTRACT16(PSHR32(ADD32(MULT16_16(QCONST16(.8f,15),st->voc_mean), MULT16_16(QCONST16(.2f,15),exc[i])), 15));
1711
611k
               exc[i]-=st->voc_mean;
1712
611k
            }
1713
15.2k
         }
1714
1715
96.8k
      }
1716
96.8k
   }
1717
1718
24.2k
   ALLOC(interp_qlsp, NB_ORDER, spx_lsp_t);
1719
1720
24.2k
   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
3.82k
      SPEEX_COPY(out, &st->exc[-NB_SUBFRAME_SIZE], NB_FRAME_SIZE);
1726
3.82k
   }
1727
1728
   /* If the last packet was lost, re-scale the excitation to obtain the same energy as encoded in ol_gain */
1729
24.2k
   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
121k
   for (sub=0;sub<NB_NB_SUBFRAMES;sub++)
1754
96.8k
   {
1755
96.8k
      int offset;
1756
96.8k
      spx_word16_t *sp;
1757
1758
      /* Offset relative to start of frame */
1759
96.8k
      offset = NB_SUBFRAME_SIZE*sub;
1760
      /* Original signal */
1761
96.8k
      sp=out+offset;
1762
1763
      /* LSP interpolation (quantized and unquantized) */
1764
96.8k
      lsp_interpolate(st->old_qlsp, qlsp, interp_qlsp, NB_ORDER, sub, NB_NB_SUBFRAMES, LSP_MARGIN);
1765
1766
      /* Compute interpolated LPCs (unquantized) */
1767
96.8k
      lsp_to_lpc(interp_qlsp, ak, NB_ORDER, stack);
1768
1769
      /* Compute analysis filter at w=pi */
1770
96.8k
      {
1771
96.8k
         spx_word32_t pi_g=LPC_SCALING;
1772
581k
         for (i=0;i<NB_ORDER;i+=2)
1773
484k
         {
1774
            /*pi_g += -st->interp_qlpc[i] +  st->interp_qlpc[i+1];*/
1775
484k
            pi_g = ADD32(pi_g, SUB32(EXTEND32(ak[i+1]),EXTEND32(ak[i])));
1776
484k
         }
1777
96.8k
         st->pi_gain[sub] = pi_g;
1778
96.8k
      }
1779
1780
96.8k
      iir_mem16(sp, st->interp_qlpc, sp, NB_SUBFRAME_SIZE, NB_ORDER,
1781
96.8k
                st->mem_sp, stack);
1782
1783
1.06M
      for (i=0;i<NB_ORDER;i++)
1784
968k
         st->interp_qlpc[i] = ak[i];
1785
1786
96.8k
   }
1787
1788
24.2k
   if (st->highpass_enabled)
1789
24.2k
      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.2k
   st->level = 1+PSHR32(ol_gain,SIG_SHIFT);
1795
24.2k
   st->max_level = MAX16(MULT16_16_Q15(QCONST16(.99f,15), st->max_level), st->level);
1796
24.2k
   st->min_level = MIN16(ADD16(1,MULT16_16_Q14(QCONST16(1.01f,14), st->min_level)), st->level);
1797
24.2k
   if (st->max_level < st->min_level+1)
1798
7.91k
      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
266k
   for (i=0;i<NB_ORDER;i++)
1803
242k
      st->old_qlsp[i] = qlsp[i];
1804
1805
   /* The next frame will not be the first (Duh!) */
1806
24.2k
   st->first = 0;
1807
24.2k
   st->count_lost=0;
1808
24.2k
   st->last_pitch = best_pitch;
1809
#ifdef FIXED_POINT
1810
13.0k
   st->last_pitch_gain = PSHR16(pitch_average,2);
1811
#else
1812
   st->last_pitch_gain = .25*pitch_average;
1813
#endif
1814
24.2k
   st->pitch_gain_buf[st->pitch_gain_buf_idx++] = st->last_pitch_gain;
1815
24.2k
   if (st->pitch_gain_buf_idx > 2) /* rollover */
1816
7.26k
      st->pitch_gain_buf_idx = 0;
1817
1818
24.2k
   st->last_ol_gain = ol_gain;
1819
1820
24.2k
   return 0;
1821
46.7k
}
nb_decode
Line
Count
Source
1331
32.1k
{
1332
32.1k
   DecState *st;
1333
32.1k
   int i, sub;
1334
32.1k
   int pitch;
1335
32.1k
   spx_word16_t pitch_gain[3];
1336
32.1k
   spx_word32_t ol_gain=0;
1337
32.1k
   int ol_pitch=0;
1338
32.1k
   spx_word16_t ol_pitch_coef=0;
1339
32.1k
   int best_pitch=40;
1340
32.1k
   spx_word16_t best_pitch_gain=0;
1341
32.1k
   int wideband;
1342
32.1k
   int m;
1343
32.1k
   char *stack;
1344
32.1k
   VARDECL(spx_sig_t *innov);
1345
32.1k
   VARDECL(spx_word32_t *exc32);
1346
32.1k
   VARDECL(spx_coef_t *ak);
1347
32.1k
   VARDECL(spx_lsp_t *qlsp);
1348
32.1k
   spx_word16_t pitch_average=0;
1349
1350
32.1k
   spx_word16_t *out = (spx_word16_t*)vout;
1351
32.1k
   VARDECL(spx_lsp_t *interp_qlsp);
1352
1353
32.1k
   st=(DecState*)state;
1354
32.1k
   stack=st->stack;
1355
1356
32.1k
   st->exc = st->excBuf + 2*NB_PITCH_END + NB_SUBFRAME_SIZE + 6;
1357
1358
   /* Check if we're in DTX mode*/
1359
32.1k
   if (!bits && st->dtx_enabled)
1360
0
   {
1361
0
      st->submodeID=0;
1362
0
   } else
1363
32.1k
   {
1364
      /* If bits is NULL, consider the packet to be lost (what could we do anyway) */
1365
32.1k
      if (!bits)
1366
0
      {
1367
0
         nb_decode_lost(st, out, stack);
1368
0
         return 0;
1369
0
      }
1370
1371
32.1k
      if (st->encode_submode)
1372
32.1k
      {
1373
1374
      /* Search for next narrowband block (handle requests, skip wideband blocks) */
1375
34.7k
      do {
1376
34.7k
         if (speex_bits_remaining(bits)<5)
1377
7.95k
            return -1;
1378
26.7k
         wideband = speex_bits_unpack_unsigned(bits, 1);
1379
26.7k
         if (wideband) /* Skip wideband block (for compatibility) */
1380
1.97k
         {
1381
1.97k
            int submode;
1382
1.97k
            int advance;
1383
1.97k
            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.97k
            advance = wb_skip_table[submode];
1386
1.97k
            if (advance < 0)
1387
0
            {
1388
0
               speex_notify("Invalid mode encountered. The stream is corrupted.");
1389
0
               return -2;
1390
0
            }
1391
1.97k
            advance -= (SB_SUBMODE_BITS+1);
1392
1.97k
            speex_bits_advance(bits, advance);
1393
1394
1.97k
            if (speex_bits_remaining(bits)<5)
1395
444
               return -1;
1396
1.53k
            wideband = speex_bits_unpack_unsigned(bits, 1);
1397
1.53k
            if (wideband)
1398
1.10k
            {
1399
1.10k
               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.10k
               advance = wb_skip_table[submode];
1402
1.10k
               if (advance < 0)
1403
0
               {
1404
0
                  speex_notify("Invalid mode encountered. The stream is corrupted.");
1405
0
                  return -2;
1406
0
               }
1407
1.10k
               advance -= (SB_SUBMODE_BITS+1);
1408
1.10k
               speex_bits_advance(bits, advance);
1409
1.10k
               wideband = speex_bits_unpack_unsigned(bits, 1);
1410
1.10k
               if (wideband)
1411
879
               {
1412
879
                  speex_notify("More than two wideband layers found. The stream is corrupted.");
1413
879
                  return -2;
1414
879
               }
1415
1416
1.10k
            }
1417
1.53k
         }
1418
25.4k
         if (speex_bits_remaining(bits)<4)
1419
218
            return -1;
1420
         /* FIXME: Check for overflow */
1421
25.2k
         m = speex_bits_unpack_unsigned(bits, 4);
1422
25.2k
         if (m==15) /* We found a terminator */
1423
292
         {
1424
292
            return -1;
1425
24.9k
         } else if (m==14) /* Speex in-band request */
1426
2.06k
         {
1427
2.06k
            int ret = speex_inband_handler(bits, st->speex_callbacks, state);
1428
2.06k
            if (ret)
1429
0
               return ret;
1430
22.8k
         } else if (m==13) /* User in-band request */
1431
523
         {
1432
523
            int ret = st->user_callback.func(bits, state, st->user_callback.data);
1433
523
            if (ret)
1434
0
               return ret;
1435
22.3k
         } else if (m>8) /* Invalid mode */
1436
1.45k
         {
1437
1.45k
            speex_notify("Invalid mode encountered. The stream is corrupted.");
1438
1.45k
            return -2;
1439
1.45k
         }
1440
1441
25.2k
      } while (m>8);
1442
1443
      /* Get the sub-mode that was used */
1444
20.8k
      st->submodeID = m;
1445
20.8k
      }
1446
1447
32.1k
   }
1448
1449
   /* Shift all buffers by one frame */
1450
20.8k
   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.8k
   if (st->submodes[st->submodeID] == NULL)
1454
9.72k
   {
1455
9.72k
      VARDECL(spx_coef_t *lpc);
1456
9.72k
      ALLOC(lpc, NB_ORDER, spx_coef_t);
1457
9.72k
      bw_lpc(QCONST16(0.93f,15), st->interp_qlpc, lpc, NB_ORDER);
1458
9.72k
      {
1459
9.72k
         spx_word16_t innov_gain=0;
1460
         /* FIXME: This was innov, not exc */
1461
9.72k
         innov_gain = compute_rms16(st->exc, NB_FRAME_SIZE);
1462
1.56M
         for (i=0;i<NB_FRAME_SIZE;i++)
1463
1.55M
            st->exc[i]=speex_rand(innov_gain, &st->seed);
1464
9.72k
      }
1465
1466
1467
9.72k
      st->first=1;
1468
1469
      /* Final signal synthesis from excitation */
1470
9.72k
      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.72k
      if (st->innov_save)
1475
8.04k
         SPEEX_MEMSET(st->innov_save, 0, NB_NB_SUBFRAMES*NB_SUBFRAME_SIZE);
1476
1477
9.72k
      st->count_lost=0;
1478
9.72k
      return 0;
1479
9.72k
   }
1480
1481
11.1k
   ALLOC(qlsp, NB_ORDER, spx_lsp_t);
1482
1483
   /* Unquantize LSPs */
1484
11.1k
   SUBMODE(lsp_unquant)(qlsp, NB_ORDER, bits);
1485
1486
   /*Damp memory if a frame was lost and the LSP changed too much*/
1487
11.1k
   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.1k
   if (st->first || st->count_lost)
1505
1.98k
   {
1506
21.8k
      for (i=0;i<NB_ORDER;i++)
1507
19.8k
         st->old_qlsp[i] = qlsp[i];
1508
1.98k
   }
1509
1510
   /* Get open-loop pitch estimation for low bit-rate pitch coding */
1511
11.1k
   if (SUBMODE(lbr_pitch)!=-1)
1512
4.68k
   {
1513
4.68k
      ol_pitch = NB_PITCH_START+speex_bits_unpack_unsigned(bits, 7);
1514
4.68k
   }
1515
1516
11.1k
   if (SUBMODE(forced_pitch_gain))
1517
4.09k
   {
1518
4.09k
      int quant;
1519
4.09k
      quant = speex_bits_unpack_unsigned(bits, 4);
1520
4.09k
      ol_pitch_coef=MULT16_16_P15(QCONST16(0.066667,15),SHL16(quant,GAIN_SHIFT));
1521
4.09k
   }
1522
1523
   /* Get global excitation gain */
1524
11.1k
   {
1525
11.1k
      int qe;
1526
11.1k
      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.1k
      ol_gain = SIG_SCALING*exp(qe/3.5);
1532
11.1k
#endif
1533
11.1k
   }
1534
1535
11.1k
   ALLOC(ak, NB_ORDER, spx_coef_t);
1536
11.1k
   ALLOC(innov, NB_SUBFRAME_SIZE, spx_sig_t);
1537
11.1k
   ALLOC(exc32, NB_SUBFRAME_SIZE, spx_word32_t);
1538
1539
11.1k
   if (st->submodeID==1)
1540
1.89k
   {
1541
1.89k
      int extra;
1542
1.89k
      extra = speex_bits_unpack_unsigned(bits, 4);
1543
1544
1.89k
      if (extra==15)
1545
254
         st->dtx_enabled=1;
1546
1.63k
      else
1547
1.63k
         st->dtx_enabled=0;
1548
1.89k
   }
1549
11.1k
   if (st->submodeID>1)
1550
9.27k
      st->dtx_enabled=0;
1551
1552
   /*Loop on subframes */
1553
55.8k
   for (sub=0;sub<NB_NB_SUBFRAMES;sub++)
1554
44.6k
   {
1555
44.6k
      int offset;
1556
44.6k
      spx_word16_t *exc;
1557
44.6k
      spx_word16_t *innov_save = NULL;
1558
44.6k
      spx_word16_t tmp;
1559
1560
      /* Offset relative to start of frame */
1561
44.6k
      offset = NB_SUBFRAME_SIZE*sub;
1562
      /* Excitation */
1563
44.6k
      exc=st->exc+offset;
1564
      /* Original signal */
1565
44.6k
      if (st->innov_save)
1566
28.8k
         innov_save = st->innov_save+offset;
1567
1568
1569
      /* Reset excitation */
1570
44.6k
      SPEEX_MEMSET(exc, 0, NB_SUBFRAME_SIZE);
1571
1572
      /*Adaptive codebook contribution*/
1573
44.6k
      speex_assert (SUBMODE(ltp_unquant));
1574
44.6k
      {
1575
44.6k
         int pit_min, pit_max;
1576
         /* Handle pitch constraints if any */
1577
44.6k
         if (SUBMODE(lbr_pitch) != -1)
1578
18.7k
         {
1579
18.7k
            int margin;
1580
18.7k
            margin = SUBMODE(lbr_pitch);
1581
18.7k
            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
18.7k
            } else {
1598
18.7k
               pit_min = pit_max = ol_pitch;
1599
18.7k
            }
1600
25.9k
         } else {
1601
25.9k
            pit_min = NB_PITCH_START;
1602
25.9k
            pit_max = NB_PITCH_END;
1603
25.9k
         }
1604
1605
1606
1607
44.6k
         SUBMODE(ltp_unquant)(exc, exc32, pit_min, pit_max, ol_pitch_coef, SUBMODE(ltp_params),
1608
44.6k
                 NB_SUBFRAME_SIZE, &pitch, &pitch_gain[0], bits, stack,
1609
44.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
44.6k
         sanitize_values32(exc32, NEG32(QCONST32(32000,SIG_SHIFT-1)), QCONST32(32000,SIG_SHIFT-1), NB_SUBFRAME_SIZE);
1615
1616
44.6k
         tmp = gain_3tap_to_1tap(pitch_gain);
1617
1618
44.6k
         pitch_average += tmp;
1619
44.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
44.6k
              || (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
44.6k
              || (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.30k
         {
1623
5.30k
            best_pitch = pitch;
1624
5.30k
            if (tmp > best_pitch_gain)
1625
4.76k
               best_pitch_gain = tmp;
1626
5.30k
         }
1627
44.6k
      }
1628
1629
      /* Unquantize the innovation */
1630
44.6k
      {
1631
44.6k
         int q_energy;
1632
44.6k
         spx_word32_t ener;
1633
1634
44.6k
         SPEEX_MEMSET(innov, 0, NB_SUBFRAME_SIZE);
1635
1636
         /* Decode sub-frame gain correction */
1637
44.6k
         if (SUBMODE(have_subframe_gain)==3)
1638
11.9k
         {
1639
11.9k
            q_energy = speex_bits_unpack_unsigned(bits, 3);
1640
11.9k
            ener = MULT16_32_Q14(exc_gain_quant_scal3[q_energy],ol_gain);
1641
32.7k
         } else if (SUBMODE(have_subframe_gain)==1)
1642
13.9k
         {
1643
13.9k
            q_energy = speex_bits_unpack_unsigned(bits, 1);
1644
13.9k
            ener = MULT16_32_Q14(exc_gain_quant_scal1[q_energy],ol_gain);
1645
18.7k
         } else {
1646
18.7k
            ener = ol_gain;
1647
18.7k
         }
1648
1649
44.6k
         speex_assert (SUBMODE(innovation_unquant));
1650
44.6k
         {
1651
            /*Fixed codebook contribution*/
1652
44.6k
            SUBMODE(innovation_unquant)(innov, SUBMODE(innovation_params), NB_SUBFRAME_SIZE, bits, stack, &st->seed);
1653
            /* De-normalize innovation and update excitation */
1654
1655
44.6k
            signal_mul(innov, innov, ener, NB_SUBFRAME_SIZE);
1656
1657
            /* Decode second codebook (only for some modes) */
1658
44.6k
            if (SUBMODE(double_codebook))
1659
3.48k
            {
1660
3.48k
               char *tmp_stack=stack;
1661
3.48k
               VARDECL(spx_sig_t *innov2);
1662
3.48k
               ALLOC(innov2, NB_SUBFRAME_SIZE, spx_sig_t);
1663
3.48k
               SPEEX_MEMSET(innov2, 0, NB_SUBFRAME_SIZE);
1664
3.48k
               SUBMODE(innovation_unquant)(innov2, SUBMODE(innovation_params), NB_SUBFRAME_SIZE, bits, stack, &st->seed);
1665
3.48k
               signal_mul(innov2, innov2, MULT16_32_Q15(QCONST16(0.454545f,15),ener), NB_SUBFRAME_SIZE);
1666
143k
               for (i=0;i<NB_SUBFRAME_SIZE;i++)
1667
139k
                  innov[i] = ADD32(innov[i], innov2[i]);
1668
3.48k
               stack = tmp_stack;
1669
3.48k
            }
1670
1.83M
            for (i=0;i<NB_SUBFRAME_SIZE;i++)
1671
1.78M
               exc[i]=EXTRACT16(SATURATE32(PSHR32(ADD32(SHL32(exc32[i],1),innov[i]),SIG_SHIFT),32767));
1672
            /*print_vec(exc, 40, "innov");*/
1673
44.6k
            if (innov_save)
1674
28.8k
            {
1675
1.18M
               for (i=0;i<NB_SUBFRAME_SIZE;i++)
1676
1.15M
                  innov_save[i] = EXTRACT16(PSHR32(innov[i], SIG_SHIFT));
1677
28.8k
            }
1678
44.6k
         }
1679
1680
         /*Vocoder mode*/
1681
44.6k
         if (st->submodeID==1)
1682
7.56k
         {
1683
7.56k
            spx_word16_t g=ol_pitch_coef;
1684
7.56k
            g=MULT16_16_P14(QCONST16(1.5f,14),(g-QCONST16(.2f,6)));
1685
7.56k
            if (g<0)
1686
5.51k
               g=0;
1687
7.56k
            if (g>GAIN_SCALING)
1688
668
               g=GAIN_SCALING;
1689
1690
7.56k
            SPEEX_MEMSET(exc, 0, NB_SUBFRAME_SIZE);
1691
20.5k
            while (st->voc_offset<NB_SUBFRAME_SIZE)
1692
12.9k
            {
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
12.9k
               if (st->voc_offset>=0)
1696
12.9k
                  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
12.9k
               st->voc_offset+=ol_pitch;
1698
12.9k
            }
1699
7.56k
            st->voc_offset -= NB_SUBFRAME_SIZE;
1700
1701
310k
            for (i=0;i<NB_SUBFRAME_SIZE;i++)
1702
302k
            {
1703
302k
               spx_word16_t exci=exc[i];
1704
302k
               exc[i]= ADD16(ADD16(MULT16_16_Q15(QCONST16(.7f,15),exc[i]) , MULT16_16_Q15(QCONST16(.3f,15),st->voc_m1)),
1705
302k
                             SUB16(MULT16_16_Q15(Q15_ONE-MULT16_16_16(QCONST16(.85f,9),g),EXTRACT16(PSHR32(innov[i],SIG_SHIFT))),
1706
302k
                                   MULT16_16_Q15(MULT16_16_16(QCONST16(.15f,9),g),EXTRACT16(PSHR32(st->voc_m2,SIG_SHIFT)))
1707
302k
                                  ));
1708
302k
               st->voc_m1 = exci;
1709
302k
               st->voc_m2=innov[i];
1710
302k
               st->voc_mean = EXTRACT16(PSHR32(ADD32(MULT16_16(QCONST16(.8f,15),st->voc_mean), MULT16_16(QCONST16(.2f,15),exc[i])), 15));
1711
302k
               exc[i]-=st->voc_mean;
1712
302k
            }
1713
7.56k
         }
1714
1715
44.6k
      }
1716
44.6k
   }
1717
1718
11.1k
   ALLOC(interp_qlsp, NB_ORDER, spx_lsp_t);
1719
1720
11.1k
   if (st->lpc_enh_enabled && SUBMODE(comb_gain)>0 && !st->count_lost)
1721
9.27k
   {
1722
9.27k
      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.27k
      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.27k
   } else {
1725
1.89k
      SPEEX_COPY(out, &st->exc[-NB_SUBFRAME_SIZE], NB_FRAME_SIZE);
1726
1.89k
   }
1727
1728
   /* If the last packet was lost, re-scale the excitation to obtain the same energy as encoded in ol_gain */
1729
11.1k
   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
55.8k
   for (sub=0;sub<NB_NB_SUBFRAMES;sub++)
1754
44.6k
   {
1755
44.6k
      int offset;
1756
44.6k
      spx_word16_t *sp;
1757
1758
      /* Offset relative to start of frame */
1759
44.6k
      offset = NB_SUBFRAME_SIZE*sub;
1760
      /* Original signal */
1761
44.6k
      sp=out+offset;
1762
1763
      /* LSP interpolation (quantized and unquantized) */
1764
44.6k
      lsp_interpolate(st->old_qlsp, qlsp, interp_qlsp, NB_ORDER, sub, NB_NB_SUBFRAMES, LSP_MARGIN);
1765
1766
      /* Compute interpolated LPCs (unquantized) */
1767
44.6k
      lsp_to_lpc(interp_qlsp, ak, NB_ORDER, stack);
1768
1769
      /* Compute analysis filter at w=pi */
1770
44.6k
      {
1771
44.6k
         spx_word32_t pi_g=LPC_SCALING;
1772
267k
         for (i=0;i<NB_ORDER;i+=2)
1773
223k
         {
1774
            /*pi_g += -st->interp_qlpc[i] +  st->interp_qlpc[i+1];*/
1775
223k
            pi_g = ADD32(pi_g, SUB32(EXTEND32(ak[i+1]),EXTEND32(ak[i])));
1776
223k
         }
1777
44.6k
         st->pi_gain[sub] = pi_g;
1778
44.6k
      }
1779
1780
44.6k
      iir_mem16(sp, st->interp_qlpc, sp, NB_SUBFRAME_SIZE, NB_ORDER,
1781
44.6k
                st->mem_sp, stack);
1782
1783
491k
      for (i=0;i<NB_ORDER;i++)
1784
446k
         st->interp_qlpc[i] = ak[i];
1785
1786
44.6k
   }
1787
1788
11.1k
   if (st->highpass_enabled)
1789
11.1k
      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.1k
   st->level = 1+PSHR32(ol_gain,SIG_SHIFT);
1795
11.1k
   st->max_level = MAX16(MULT16_16_Q15(QCONST16(.99f,15), st->max_level), st->level);
1796
11.1k
   st->min_level = MIN16(ADD16(1,MULT16_16_Q14(QCONST16(1.01f,14), st->min_level)), st->level);
1797
11.1k
   if (st->max_level < st->min_level+1)
1798
4.02k
      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
122k
   for (i=0;i<NB_ORDER;i++)
1803
111k
      st->old_qlsp[i] = qlsp[i];
1804
1805
   /* The next frame will not be the first (Duh!) */
1806
11.1k
   st->first = 0;
1807
11.1k
   st->count_lost=0;
1808
11.1k
   st->last_pitch = best_pitch;
1809
#ifdef FIXED_POINT
1810
   st->last_pitch_gain = PSHR16(pitch_average,2);
1811
#else
1812
11.1k
   st->last_pitch_gain = .25*pitch_average;
1813
11.1k
#endif
1814
11.1k
   st->pitch_gain_buf[st->pitch_gain_buf_idx++] = st->last_pitch_gain;
1815
11.1k
   if (st->pitch_gain_buf_idx > 2) /* rollover */
1816
3.46k
      st->pitch_gain_buf_idx = 0;
1817
1818
11.1k
   st->last_ol_gain = ol_gain;
1819
1820
11.1k
   return 0;
1821
20.8k
}
nb_decode
Line
Count
Source
1331
35.8k
{
1332
35.8k
   DecState *st;
1333
35.8k
   int i, sub;
1334
35.8k
   int pitch;
1335
35.8k
   spx_word16_t pitch_gain[3];
1336
35.8k
   spx_word32_t ol_gain=0;
1337
35.8k
   int ol_pitch=0;
1338
35.8k
   spx_word16_t ol_pitch_coef=0;
1339
35.8k
   int best_pitch=40;
1340
35.8k
   spx_word16_t best_pitch_gain=0;
1341
35.8k
   int wideband;
1342
35.8k
   int m;
1343
35.8k
   char *stack;
1344
35.8k
   VARDECL(spx_sig_t *innov);
1345
35.8k
   VARDECL(spx_word32_t *exc32);
1346
35.8k
   VARDECL(spx_coef_t *ak);
1347
35.8k
   VARDECL(spx_lsp_t *qlsp);
1348
35.8k
   spx_word16_t pitch_average=0;
1349
1350
35.8k
   spx_word16_t *out = (spx_word16_t*)vout;
1351
35.8k
   VARDECL(spx_lsp_t *interp_qlsp);
1352
1353
35.8k
   st=(DecState*)state;
1354
35.8k
   stack=st->stack;
1355
1356
35.8k
   st->exc = st->excBuf + 2*NB_PITCH_END + NB_SUBFRAME_SIZE + 6;
1357
1358
   /* Check if we're in DTX mode*/
1359
35.8k
   if (!bits && st->dtx_enabled)
1360
0
   {
1361
0
      st->submodeID=0;
1362
0
   } else
1363
35.8k
   {
1364
      /* If bits is NULL, consider the packet to be lost (what could we do anyway) */
1365
35.8k
      if (!bits)
1366
0
      {
1367
0
         nb_decode_lost(st, out, stack);
1368
0
         return 0;
1369
0
      }
1370
1371
35.8k
      if (st->encode_submode)
1372
35.8k
      {
1373
1374
      /* Search for next narrowband block (handle requests, skip wideband blocks) */
1375
38.5k
      do {
1376
38.5k
         if (speex_bits_remaining(bits)<5)
1377
5.40k
            return -1;
1378
33.1k
         wideband = speex_bits_unpack_unsigned(bits, 1);
1379
33.1k
         if (wideband) /* Skip wideband block (for compatibility) */
1380
2.45k
         {
1381
2.45k
            int submode;
1382
2.45k
            int advance;
1383
2.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
2.45k
            advance = wb_skip_table[submode];
1386
2.45k
            if (advance < 0)
1387
0
            {
1388
0
               speex_notify("Invalid mode encountered. The stream is corrupted.");
1389
0
               return -2;
1390
0
            }
1391
2.45k
            advance -= (SB_SUBMODE_BITS+1);
1392
2.45k
            speex_bits_advance(bits, advance);
1393
1394
2.45k
            if (speex_bits_remaining(bits)<5)
1395
666
               return -1;
1396
1.79k
            wideband = speex_bits_unpack_unsigned(bits, 1);
1397
1.79k
            if (wideband)
1398
1.46k
            {
1399
1.46k
               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.46k
               advance = wb_skip_table[submode];
1402
1.46k
               if (advance < 0)
1403
0
               {
1404
0
                  speex_notify("Invalid mode encountered. The stream is corrupted.");
1405
0
                  return -2;
1406
0
               }
1407
1.46k
               advance -= (SB_SUBMODE_BITS+1);
1408
1.46k
               speex_bits_advance(bits, advance);
1409
1.46k
               wideband = speex_bits_unpack_unsigned(bits, 1);
1410
1.46k
               if (wideband)
1411
1.25k
               {
1412
1.25k
                  speex_notify("More than two wideband layers found. The stream is corrupted.");
1413
1.25k
                  return -2;
1414
1.25k
               }
1415
1416
1.46k
            }
1417
1.79k
         }
1418
31.2k
         if (speex_bits_remaining(bits)<4)
1419
206
            return -1;
1420
         /* FIXME: Check for overflow */
1421
30.9k
         m = speex_bits_unpack_unsigned(bits, 4);
1422
30.9k
         if (m==15) /* We found a terminator */
1423
460
         {
1424
460
            return -1;
1425
30.5k
         } else if (m==14) /* Speex in-band request */
1426
2.37k
         {
1427
2.37k
            int ret = speex_inband_handler(bits, st->speex_callbacks, state);
1428
2.37k
            if (ret)
1429
0
               return ret;
1430
28.1k
         } else if (m==13) /* User in-band request */
1431
303
         {
1432
303
            int ret = st->user_callback.func(bits, state, st->user_callback.data);
1433
303
            if (ret)
1434
0
               return ret;
1435
27.8k
         } else if (m>8) /* Invalid mode */
1436
1.97k
         {
1437
1.97k
            speex_notify("Invalid mode encountered. The stream is corrupted.");
1438
1.97k
            return -2;
1439
1.97k
         }
1440
1441
30.9k
      } while (m>8);
1442
1443
      /* Get the sub-mode that was used */
1444
25.8k
      st->submodeID = m;
1445
25.8k
      }
1446
1447
35.8k
   }
1448
1449
   /* Shift all buffers by one frame */
1450
25.8k
   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.8k
   if (st->submodes[st->submodeID] == NULL)
1454
12.8k
   {
1455
12.8k
      VARDECL(spx_coef_t *lpc);
1456
12.8k
      ALLOC(lpc, NB_ORDER, spx_coef_t);
1457
12.8k
      bw_lpc(QCONST16(0.93f,15), st->interp_qlpc, lpc, NB_ORDER);
1458
12.8k
      {
1459
12.8k
         spx_word16_t innov_gain=0;
1460
         /* FIXME: This was innov, not exc */
1461
12.8k
         innov_gain = compute_rms16(st->exc, NB_FRAME_SIZE);
1462
2.06M
         for (i=0;i<NB_FRAME_SIZE;i++)
1463
2.05M
            st->exc[i]=speex_rand(innov_gain, &st->seed);
1464
12.8k
      }
1465
1466
1467
12.8k
      st->first=1;
1468
1469
      /* Final signal synthesis from excitation */
1470
12.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
12.8k
      if (st->innov_save)
1475
10.6k
         SPEEX_MEMSET(st->innov_save, 0, NB_NB_SUBFRAMES*NB_SUBFRAME_SIZE);
1476
1477
12.8k
      st->count_lost=0;
1478
12.8k
      return 0;
1479
12.8k
   }
1480
1481
13.0k
   ALLOC(qlsp, NB_ORDER, spx_lsp_t);
1482
1483
   /* Unquantize LSPs */
1484
13.0k
   SUBMODE(lsp_unquant)(qlsp, NB_ORDER, bits);
1485
1486
   /*Damp memory if a frame was lost and the LSP changed too much*/
1487
13.0k
   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.0k
   if (st->first || st->count_lost)
1505
2.83k
   {
1506
31.1k
      for (i=0;i<NB_ORDER;i++)
1507
28.3k
         st->old_qlsp[i] = qlsp[i];
1508
2.83k
   }
1509
1510
   /* Get open-loop pitch estimation for low bit-rate pitch coding */
1511
13.0k
   if (SUBMODE(lbr_pitch)!=-1)
1512
5.34k
   {
1513
5.34k
      ol_pitch = NB_PITCH_START+speex_bits_unpack_unsigned(bits, 7);
1514
5.34k
   }
1515
1516
13.0k
   if (SUBMODE(forced_pitch_gain))
1517
4.12k
   {
1518
4.12k
      int quant;
1519
4.12k
      quant = speex_bits_unpack_unsigned(bits, 4);
1520
4.12k
      ol_pitch_coef=MULT16_16_P15(QCONST16(0.066667,15),SHL16(quant,GAIN_SHIFT));
1521
4.12k
   }
1522
1523
   /* Get global excitation gain */
1524
13.0k
   {
1525
13.0k
      int qe;
1526
13.0k
      qe = speex_bits_unpack_unsigned(bits, 5);
1527
13.0k
#ifdef FIXED_POINT
1528
      /* FIXME: Perhaps we could slightly lower the gain here when the output is going to saturate? */
1529
13.0k
      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.0k
   }
1534
1535
13.0k
   ALLOC(ak, NB_ORDER, spx_coef_t);
1536
13.0k
   ALLOC(innov, NB_SUBFRAME_SIZE, spx_sig_t);
1537
13.0k
   ALLOC(exc32, NB_SUBFRAME_SIZE, spx_word32_t);
1538
1539
13.0k
   if (st->submodeID==1)
1540
1.92k
   {
1541
1.92k
      int extra;
1542
1.92k
      extra = speex_bits_unpack_unsigned(bits, 4);
1543
1544
1.92k
      if (extra==15)
1545
352
         st->dtx_enabled=1;
1546
1.57k
      else
1547
1.57k
         st->dtx_enabled=0;
1548
1.92k
   }
1549
13.0k
   if (st->submodeID>1)
1550
11.1k
      st->dtx_enabled=0;
1551
1552
   /*Loop on subframes */
1553
65.2k
   for (sub=0;sub<NB_NB_SUBFRAMES;sub++)
1554
52.2k
   {
1555
52.2k
      int offset;
1556
52.2k
      spx_word16_t *exc;
1557
52.2k
      spx_word16_t *innov_save = NULL;
1558
52.2k
      spx_word16_t tmp;
1559
1560
      /* Offset relative to start of frame */
1561
52.2k
      offset = NB_SUBFRAME_SIZE*sub;
1562
      /* Excitation */
1563
52.2k
      exc=st->exc+offset;
1564
      /* Original signal */
1565
52.2k
      if (st->innov_save)
1566
37.8k
         innov_save = st->innov_save+offset;
1567
1568
1569
      /* Reset excitation */
1570
52.2k
      SPEEX_MEMSET(exc, 0, NB_SUBFRAME_SIZE);
1571
1572
      /*Adaptive codebook contribution*/
1573
52.2k
      speex_assert (SUBMODE(ltp_unquant));
1574
52.2k
      {
1575
52.2k
         int pit_min, pit_max;
1576
         /* Handle pitch constraints if any */
1577
52.2k
         if (SUBMODE(lbr_pitch) != -1)
1578
21.3k
         {
1579
21.3k
            int margin;
1580
21.3k
            margin = SUBMODE(lbr_pitch);
1581
21.3k
            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
21.3k
            } else {
1598
21.3k
               pit_min = pit_max = ol_pitch;
1599
21.3k
            }
1600
30.8k
         } else {
1601
30.8k
            pit_min = NB_PITCH_START;
1602
30.8k
            pit_max = NB_PITCH_END;
1603
30.8k
         }
1604
1605
1606
1607
52.2k
         SUBMODE(ltp_unquant)(exc, exc32, pit_min, pit_max, ol_pitch_coef, SUBMODE(ltp_params),
1608
52.2k
                 NB_SUBFRAME_SIZE, &pitch, &pitch_gain[0], bits, stack,
1609
52.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
52.2k
         sanitize_values32(exc32, NEG32(QCONST32(32000,SIG_SHIFT-1)), QCONST32(32000,SIG_SHIFT-1), NB_SUBFRAME_SIZE);
1615
1616
52.2k
         tmp = gain_3tap_to_1tap(pitch_gain);
1617
1618
52.2k
         pitch_average += tmp;
1619
52.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
52.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
52.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
9.27k
         {
1623
9.27k
            best_pitch = pitch;
1624
9.27k
            if (tmp > best_pitch_gain)
1625
8.61k
               best_pitch_gain = tmp;
1626
9.27k
         }
1627
52.2k
      }
1628
1629
      /* Unquantize the innovation */
1630
52.2k
      {
1631
52.2k
         int q_energy;
1632
52.2k
         spx_word32_t ener;
1633
1634
52.2k
         SPEEX_MEMSET(innov, 0, NB_SUBFRAME_SIZE);
1635
1636
         /* Decode sub-frame gain correction */
1637
52.2k
         if (SUBMODE(have_subframe_gain)==3)
1638
10.2k
         {
1639
10.2k
            q_energy = speex_bits_unpack_unsigned(bits, 3);
1640
10.2k
            ener = MULT16_32_Q14(exc_gain_quant_scal3[q_energy],ol_gain);
1641
41.9k
         } else if (SUBMODE(have_subframe_gain)==1)
1642
20.5k
         {
1643
20.5k
            q_energy = speex_bits_unpack_unsigned(bits, 1);
1644
20.5k
            ener = MULT16_32_Q14(exc_gain_quant_scal1[q_energy],ol_gain);
1645
21.3k
         } else {
1646
21.3k
            ener = ol_gain;
1647
21.3k
         }
1648
1649
52.2k
         speex_assert (SUBMODE(innovation_unquant));
1650
52.2k
         {
1651
            /*Fixed codebook contribution*/
1652
52.2k
            SUBMODE(innovation_unquant)(innov, SUBMODE(innovation_params), NB_SUBFRAME_SIZE, bits, stack, &st->seed);
1653
            /* De-normalize innovation and update excitation */
1654
1655
52.2k
            signal_mul(innov, innov, ener, NB_SUBFRAME_SIZE);
1656
1657
            /* Decode second codebook (only for some modes) */
1658
52.2k
            if (SUBMODE(double_codebook))
1659
3.47k
            {
1660
3.47k
               char *tmp_stack=stack;
1661
3.47k
               VARDECL(spx_sig_t *innov2);
1662
3.47k
               ALLOC(innov2, NB_SUBFRAME_SIZE, spx_sig_t);
1663
3.47k
               SPEEX_MEMSET(innov2, 0, NB_SUBFRAME_SIZE);
1664
3.47k
               SUBMODE(innovation_unquant)(innov2, SUBMODE(innovation_params), NB_SUBFRAME_SIZE, bits, stack, &st->seed);
1665
3.47k
               signal_mul(innov2, innov2, MULT16_32_Q15(QCONST16(0.454545f,15),ener), NB_SUBFRAME_SIZE);
1666
142k
               for (i=0;i<NB_SUBFRAME_SIZE;i++)
1667
139k
                  innov[i] = ADD32(innov[i], innov2[i]);
1668
3.47k
               stack = tmp_stack;
1669
3.47k
            }
1670
2.14M
            for (i=0;i<NB_SUBFRAME_SIZE;i++)
1671
2.08M
               exc[i]=EXTRACT16(SATURATE32(PSHR32(ADD32(SHL32(exc32[i],1),innov[i]),SIG_SHIFT),32767));
1672
            /*print_vec(exc, 40, "innov");*/
1673
52.2k
            if (innov_save)
1674
37.8k
            {
1675
1.55M
               for (i=0;i<NB_SUBFRAME_SIZE;i++)
1676
1.51M
                  innov_save[i] = EXTRACT16(PSHR32(innov[i], SIG_SHIFT));
1677
37.8k
            }
1678
52.2k
         }
1679
1680
         /*Vocoder mode*/
1681
52.2k
         if (st->submodeID==1)
1682
7.71k
         {
1683
7.71k
            spx_word16_t g=ol_pitch_coef;
1684
7.71k
            g=MULT16_16_P14(QCONST16(1.5f,14),(g-QCONST16(.2f,6)));
1685
7.71k
            if (g<0)
1686
3.61k
               g=0;
1687
7.71k
            if (g>GAIN_SCALING)
1688
1.11k
               g=GAIN_SCALING;
1689
1690
7.71k
            SPEEX_MEMSET(exc, 0, NB_SUBFRAME_SIZE);
1691
16.1k
            while (st->voc_offset<NB_SUBFRAME_SIZE)
1692
8.43k
            {
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
8.43k
               if (st->voc_offset>=0)
1696
8.43k
                  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
8.43k
               st->voc_offset+=ol_pitch;
1698
8.43k
            }
1699
7.71k
            st->voc_offset -= NB_SUBFRAME_SIZE;
1700
1701
316k
            for (i=0;i<NB_SUBFRAME_SIZE;i++)
1702
308k
            {
1703
308k
               spx_word16_t exci=exc[i];
1704
308k
               exc[i]= ADD16(ADD16(MULT16_16_Q15(QCONST16(.7f,15),exc[i]) , MULT16_16_Q15(QCONST16(.3f,15),st->voc_m1)),
1705
308k
                             SUB16(MULT16_16_Q15(Q15_ONE-MULT16_16_16(QCONST16(.85f,9),g),EXTRACT16(PSHR32(innov[i],SIG_SHIFT))),
1706
308k
                                   MULT16_16_Q15(MULT16_16_16(QCONST16(.15f,9),g),EXTRACT16(PSHR32(st->voc_m2,SIG_SHIFT)))
1707
308k
                                  ));
1708
308k
               st->voc_m1 = exci;
1709
308k
               st->voc_m2=innov[i];
1710
308k
               st->voc_mean = EXTRACT16(PSHR32(ADD32(MULT16_16(QCONST16(.8f,15),st->voc_mean), MULT16_16(QCONST16(.2f,15),exc[i])), 15));
1711
308k
               exc[i]-=st->voc_mean;
1712
308k
            }
1713
7.71k
         }
1714
1715
52.2k
      }
1716
52.2k
   }
1717
1718
13.0k
   ALLOC(interp_qlsp, NB_ORDER, spx_lsp_t);
1719
1720
13.0k
   if (st->lpc_enh_enabled && SUBMODE(comb_gain)>0 && !st->count_lost)
1721
11.1k
   {
1722
11.1k
      multicomb(st->exc-NB_SUBFRAME_SIZE, out, st->interp_qlpc, NB_ORDER, 2*NB_SUBFRAME_SIZE, best_pitch, 40, SUBMODE(comb_gain), stack);
1723
11.1k
      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
11.1k
   } else {
1725
1.92k
      SPEEX_COPY(out, &st->exc[-NB_SUBFRAME_SIZE], NB_FRAME_SIZE);
1726
1.92k
   }
1727
1728
   /* If the last packet was lost, re-scale the excitation to obtain the same energy as encoded in ol_gain */
1729
13.0k
   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
65.2k
   for (sub=0;sub<NB_NB_SUBFRAMES;sub++)
1754
52.2k
   {
1755
52.2k
      int offset;
1756
52.2k
      spx_word16_t *sp;
1757
1758
      /* Offset relative to start of frame */
1759
52.2k
      offset = NB_SUBFRAME_SIZE*sub;
1760
      /* Original signal */
1761
52.2k
      sp=out+offset;
1762
1763
      /* LSP interpolation (quantized and unquantized) */
1764
52.2k
      lsp_interpolate(st->old_qlsp, qlsp, interp_qlsp, NB_ORDER, sub, NB_NB_SUBFRAMES, LSP_MARGIN);
1765
1766
      /* Compute interpolated LPCs (unquantized) */
1767
52.2k
      lsp_to_lpc(interp_qlsp, ak, NB_ORDER, stack);
1768
1769
      /* Compute analysis filter at w=pi */
1770
52.2k
      {
1771
52.2k
         spx_word32_t pi_g=LPC_SCALING;
1772
313k
         for (i=0;i<NB_ORDER;i+=2)
1773
261k
         {
1774
            /*pi_g += -st->interp_qlpc[i] +  st->interp_qlpc[i+1];*/
1775
261k
            pi_g = ADD32(pi_g, SUB32(EXTEND32(ak[i+1]),EXTEND32(ak[i])));
1776
261k
         }
1777
52.2k
         st->pi_gain[sub] = pi_g;
1778
52.2k
      }
1779
1780
52.2k
      iir_mem16(sp, st->interp_qlpc, sp, NB_SUBFRAME_SIZE, NB_ORDER,
1781
52.2k
                st->mem_sp, stack);
1782
1783
574k
      for (i=0;i<NB_ORDER;i++)
1784
522k
         st->interp_qlpc[i] = ak[i];
1785
1786
52.2k
   }
1787
1788
13.0k
   if (st->highpass_enabled)
1789
13.0k
      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.0k
   st->level = 1+PSHR32(ol_gain,SIG_SHIFT);
1795
13.0k
   st->max_level = MAX16(MULT16_16_Q15(QCONST16(.99f,15), st->max_level), st->level);
1796
13.0k
   st->min_level = MIN16(ADD16(1,MULT16_16_Q14(QCONST16(1.01f,14), st->min_level)), st->level);
1797
13.0k
   if (st->max_level < st->min_level+1)
1798
3.89k
      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
143k
   for (i=0;i<NB_ORDER;i++)
1803
130k
      st->old_qlsp[i] = qlsp[i];
1804
1805
   /* The next frame will not be the first (Duh!) */
1806
13.0k
   st->first = 0;
1807
13.0k
   st->count_lost=0;
1808
13.0k
   st->last_pitch = best_pitch;
1809
13.0k
#ifdef FIXED_POINT
1810
13.0k
   st->last_pitch_gain = PSHR16(pitch_average,2);
1811
#else
1812
   st->last_pitch_gain = .25*pitch_average;
1813
#endif
1814
13.0k
   st->pitch_gain_buf[st->pitch_gain_buf_idx++] = st->last_pitch_gain;
1815
13.0k
   if (st->pitch_gain_buf_idx > 2) /* rollover */
1816
3.79k
      st->pitch_gain_buf_idx = 0;
1817
1818
13.0k
   st->last_ol_gain = ol_gain;
1819
1820
13.0k
   return 0;
1821
25.8k
}
1822
#endif /* DISABLE_DECODER */
1823