Coverage Report

Created: 2023-09-25 07:07

/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
833k
#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
#define LSP_MARGIN 16
80
#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
84.6k
#define LSP_MARGIN .002f
91
0
#define LSP_DELTA1 .2f
92
#define LSP_DELTA2 .05f
93
94
#endif
95
96
#ifdef VORBIS_PSYCHO
97
#define EXTRA_BUFFER 100
98
#else
99
#define EXTRA_BUFFER 0
100
#endif
101
102
103
extern const spx_word16_t lag_window[];
104
extern const spx_word16_t lpc_window[];
105
106
#ifndef DISABLE_ENCODER
107
void *nb_encoder_init(const SpeexMode *m)
108
0
{
109
0
   EncState *st;
110
0
   const SpeexNBMode *mode;
111
0
   int i;
112
113
0
   mode=(const SpeexNBMode *)m->mode;
114
0
   st = (EncState*)speex_alloc(sizeof(EncState));
115
0
   if (!st)
116
0
      return NULL;
117
0
#if defined(VAR_ARRAYS) || defined (USE_ALLOCA)
118
0
   st->stack = NULL;
119
#else
120
   st->stack = (char*)speex_alloc_scratch(NB_ENC_STACK);
121
#endif
122
123
0
   st->mode=m;
124
125
0
   st->gamma1=mode->gamma1;
126
0
   st->gamma2=mode->gamma2;
127
0
   st->lpc_floor = mode->lpc_floor;
128
129
0
   st->submodes=mode->submodes;
130
0
   st->submodeID=st->submodeSelect=mode->defaultSubmode;
131
0
   st->bounded_pitch = 1;
132
133
0
   st->encode_submode = 1;
134
135
#ifdef VORBIS_PSYCHO
136
   st->psy = vorbis_psy_init(8000, 256);
137
   st->curve = (float*)speex_alloc(128*sizeof(float));
138
   st->old_curve = (float*)speex_alloc(128*sizeof(float));
139
   st->psy_window = (float*)speex_alloc(256*sizeof(float));
140
#endif
141
142
0
   st->cumul_gain = 1024;
143
144
0
   st->window= lpc_window;
145
146
   /* Create the window for autocorrelation (lag-windowing) */
147
0
   st->lagWindow = lag_window;
148
149
0
   st->first = 1;
150
0
   for (i=0;i<NB_ORDER;i++)
151
0
      st->old_lsp[i]= DIV32(MULT16_16(QCONST16(3.1415927f, LSP_SHIFT), i+1), NB_ORDER+1);
152
153
0
   st->innov_rms_save = NULL;
154
155
0
#ifndef DISABLE_VBR
156
0
   vbr_init(&st->vbr);
157
0
   st->vbr_quality = 8;
158
0
   st->vbr_enabled = 0;
159
0
   st->vbr_max = 0;
160
0
   st->vad_enabled = 0;
161
0
   st->dtx_enabled = 0;
162
0
   st->dtx_count=0;
163
0
   st->abr_enabled = 0;
164
0
   st->abr_drift = 0;
165
0
   st->abr_drift2 = 0;
166
0
#endif /* #ifndef DISABLE_VBR */
167
168
0
   st->plc_tuning = 2;
169
0
   st->complexity=2;
170
0
   st->sampling_rate=8000;
171
0
   st->isWideband = 0;
172
0
   st->highpass_enabled = 1;
173
174
#ifdef ENABLE_VALGRIND
175
   VALGRIND_MAKE_MEM_DEFINED(st, NB_ENC_STACK);
176
#endif
177
0
   return st;
178
0
}
179
180
void nb_encoder_destroy(void *state)
181
0
{
182
0
   EncState *st=(EncState *)state;
183
   /* Free all allocated memory */
184
#if !(defined(VAR_ARRAYS) || defined (USE_ALLOCA))
185
   speex_free_scratch(st->stack);
186
#endif
187
188
0
#ifndef DISABLE_VBR
189
0
   vbr_destroy(&st->vbr);
190
0
#endif /* #ifndef DISABLE_VBR */
191
192
#ifdef VORBIS_PSYCHO
193
   vorbis_psy_destroy(st->psy);
194
   speex_free (st->curve);
195
   speex_free (st->old_curve);
196
   speex_free (st->psy_window);
197
#endif
198
199
   /*Free state memory... should be last*/
200
0
   speex_free(st);
201
0
}
202
203
204
int nb_encoder_ctl(void *state, int request, void *ptr)
205
0
{
206
0
   EncState *st;
207
0
   st=(EncState*)state;
208
0
   switch(request)
209
0
   {
210
0
   case SPEEX_GET_FRAME_SIZE:
211
0
      (*(spx_int32_t*)ptr) = NB_FRAME_SIZE;
212
0
      break;
213
0
   case SPEEX_SET_LOW_MODE:
214
0
   case SPEEX_SET_MODE:
215
0
      st->submodeSelect = st->submodeID = (*(spx_int32_t*)ptr);
216
0
      break;
217
0
   case SPEEX_GET_LOW_MODE:
218
0
   case SPEEX_GET_MODE:
219
0
      (*(spx_int32_t*)ptr) = st->submodeID;
220
0
      break;
221
0
#ifndef DISABLE_VBR
222
0
      case SPEEX_SET_VBR:
223
0
      st->vbr_enabled = (*(spx_int32_t*)ptr);
224
0
      break;
225
0
   case SPEEX_GET_VBR:
226
0
      (*(spx_int32_t*)ptr) = st->vbr_enabled;
227
0
      break;
228
0
   case SPEEX_SET_VAD:
229
0
      st->vad_enabled = (*(spx_int32_t*)ptr);
230
0
      break;
231
0
   case SPEEX_GET_VAD:
232
0
      (*(spx_int32_t*)ptr) = st->vad_enabled;
233
0
      break;
234
0
   case SPEEX_SET_DTX:
235
0
      st->dtx_enabled = (*(spx_int32_t*)ptr);
236
0
      break;
237
0
   case SPEEX_GET_DTX:
238
0
      (*(spx_int32_t*)ptr) = st->dtx_enabled;
239
0
      break;
240
0
   case SPEEX_SET_ABR:
241
0
      st->abr_enabled = (*(spx_int32_t*)ptr);
242
0
      st->vbr_enabled = st->abr_enabled!=0;
243
0
      if (st->vbr_enabled)
244
0
      {
245
0
         spx_int32_t i=10;
246
0
         spx_int32_t rate, target;
247
0
         float vbr_qual;
248
0
         target = (*(spx_int32_t*)ptr);
249
0
         while (i>=0)
250
0
         {
251
0
            speex_encoder_ctl(st, SPEEX_SET_QUALITY, &i);
252
0
            speex_encoder_ctl(st, SPEEX_GET_BITRATE, &rate);
253
0
            if (rate <= target)
254
0
               break;
255
0
            i--;
256
0
         }
257
0
         vbr_qual=i;
258
0
         if (vbr_qual<0)
259
0
            vbr_qual=0;
260
0
         speex_encoder_ctl(st, SPEEX_SET_VBR_QUALITY, &vbr_qual);
261
0
         st->abr_count=0;
262
0
         st->abr_drift=0;
263
0
         st->abr_drift2=0;
264
0
      }
265
266
0
      break;
267
0
   case SPEEX_GET_ABR:
268
0
      (*(spx_int32_t*)ptr) = st->abr_enabled;
269
0
      break;
270
0
#endif /* #ifndef DISABLE_VBR */
271
0
#if !defined(DISABLE_VBR) && !defined(DISABLE_FLOAT_API)
272
0
   case SPEEX_SET_VBR_QUALITY:
273
0
      st->vbr_quality = (*(float*)ptr);
274
0
      break;
275
0
   case SPEEX_GET_VBR_QUALITY:
276
0
      (*(float*)ptr) = st->vbr_quality;
277
0
      break;
278
0
#endif /* !defined(DISABLE_VBR) && !defined(DISABLE_FLOAT_API) */
279
0
   case SPEEX_SET_QUALITY:
280
0
      {
281
0
         int quality = (*(spx_int32_t*)ptr);
282
0
         if (quality < 0)
283
0
            quality = 0;
284
0
         if (quality > 10)
285
0
            quality = 10;
286
0
         st->submodeSelect = st->submodeID = ((const SpeexNBMode*)(st->mode->mode))->quality_map[quality];
287
0
      }
288
0
      break;
289
0
   case SPEEX_SET_COMPLEXITY:
290
0
      st->complexity = (*(spx_int32_t*)ptr);
291
0
      if (st->complexity<0)
292
0
         st->complexity=0;
293
0
      break;
294
0
   case SPEEX_GET_COMPLEXITY:
295
0
      (*(spx_int32_t*)ptr) = st->complexity;
296
0
      break;
297
0
   case SPEEX_SET_BITRATE:
298
0
      {
299
0
         spx_int32_t i=10;
300
0
         spx_int32_t rate, target;
301
0
         target = (*(spx_int32_t*)ptr);
302
0
         while (i>=0)
303
0
         {
304
0
            speex_encoder_ctl(st, SPEEX_SET_QUALITY, &i);
305
0
            speex_encoder_ctl(st, SPEEX_GET_BITRATE, &rate);
306
0
            if (rate <= target)
307
0
               break;
308
0
            i--;
309
0
         }
310
0
      }
311
0
      break;
312
0
   case SPEEX_GET_BITRATE:
313
0
      if (st->submodes[st->submodeID])
314
0
         (*(spx_int32_t*)ptr) = st->sampling_rate*SUBMODE(bits_per_frame)/NB_FRAME_SIZE;
315
0
      else
316
0
         (*(spx_int32_t*)ptr) = st->sampling_rate*(NB_SUBMODE_BITS+1)/NB_FRAME_SIZE;
317
0
      break;
318
0
   case SPEEX_SET_SAMPLING_RATE:
319
0
      st->sampling_rate = (*(spx_int32_t*)ptr);
320
0
      break;
321
0
   case SPEEX_GET_SAMPLING_RATE:
322
0
      (*(spx_int32_t*)ptr)=st->sampling_rate;
323
0
      break;
324
0
   case SPEEX_RESET_STATE:
325
0
      {
326
0
         int i;
327
0
         st->bounded_pitch = 1;
328
0
         st->first = 1;
329
0
         for (i=0;i<NB_ORDER;i++)
330
0
            st->old_lsp[i]= DIV32(MULT16_16(QCONST16(3.1415927f, LSP_SHIFT), i+1), NB_ORDER+1);
331
0
         for (i=0;i<NB_ORDER;i++)
332
0
            st->mem_sw[i]=st->mem_sw_whole[i]=st->mem_sp[i]=st->mem_exc[i]=0;
333
0
         for (i=0;i<NB_FRAME_SIZE+NB_PITCH_END+1;i++)
334
0
            st->excBuf[i]=st->swBuf[i]=0;
335
0
         for (i=0;i<NB_WINDOW_SIZE-NB_FRAME_SIZE;i++)
336
0
            st->winBuf[i]=0;
337
0
      }
338
0
      break;
339
0
   case SPEEX_SET_SUBMODE_ENCODING:
340
0
      st->encode_submode = (*(spx_int32_t*)ptr);
341
0
      break;
342
0
   case SPEEX_GET_SUBMODE_ENCODING:
343
0
      (*(spx_int32_t*)ptr) = st->encode_submode;
344
0
      break;
345
0
   case SPEEX_GET_LOOKAHEAD:
346
0
      (*(spx_int32_t*)ptr)=(NB_WINDOW_SIZE-NB_FRAME_SIZE);
347
0
      break;
348
0
   case SPEEX_SET_PLC_TUNING:
349
0
      st->plc_tuning = (*(spx_int32_t*)ptr);
350
0
      if (st->plc_tuning>100)
351
0
         st->plc_tuning=100;
352
0
      break;
353
0
   case SPEEX_GET_PLC_TUNING:
354
0
      (*(spx_int32_t*)ptr)=(st->plc_tuning);
355
0
      break;
356
0
#ifndef DISABLE_VBR
357
0
   case SPEEX_SET_VBR_MAX_BITRATE:
358
0
      st->vbr_max = (*(spx_int32_t*)ptr);
359
0
      break;
360
0
   case SPEEX_GET_VBR_MAX_BITRATE:
361
0
      (*(spx_int32_t*)ptr) = st->vbr_max;
362
0
      break;
363
0
#endif /* #ifndef DISABLE_VBR */
364
0
   case SPEEX_SET_HIGHPASS:
365
0
      st->highpass_enabled = (*(spx_int32_t*)ptr);
366
0
      break;
367
0
   case SPEEX_GET_HIGHPASS:
368
0
      (*(spx_int32_t*)ptr) = st->highpass_enabled;
369
0
      break;
370
371
   /* This is all internal stuff past this point */
372
0
   case SPEEX_GET_PI_GAIN:
373
0
      {
374
0
         int i;
375
0
         spx_word32_t *g = (spx_word32_t*)ptr;
376
0
         for (i=0;i<NB_NB_SUBFRAMES;i++)
377
0
            g[i]=st->pi_gain[i];
378
0
      }
379
0
      break;
380
0
   case SPEEX_GET_EXC:
381
0
      {
382
0
         int i;
383
0
         for (i=0;i<NB_NB_SUBFRAMES;i++)
384
0
            ((spx_word16_t*)ptr)[i] = compute_rms16(st->exc+i*NB_SUBFRAME_SIZE, NB_SUBFRAME_SIZE);
385
0
      }
386
0
      break;
387
0
#ifndef DISABLE_VBR
388
0
   case SPEEX_GET_RELATIVE_QUALITY:
389
0
      (*(float*)ptr)=st->relative_quality;
390
0
      break;
391
0
#endif /* #ifndef DISABLE_VBR */
392
0
   case SPEEX_SET_INNOVATION_SAVE:
393
0
      st->innov_rms_save = (spx_word16_t*)ptr;
394
0
      break;
395
0
   case SPEEX_SET_WIDEBAND:
396
0
      st->isWideband = *((spx_int32_t*)ptr);
397
0
      break;
398
0
   case SPEEX_GET_STACK:
399
0
      *((char**)ptr) = st->stack;
400
0
      break;
401
0
   default:
402
0
      speex_warning_int("Unknown nb_ctl request: ", request);
403
0
      return -1;
404
0
   }
405
0
   return 0;
406
0
}
407
408
409
int nb_encode(void *state, void *vin, SpeexBits *bits)
410
0
{
411
0
   EncState *st;
412
0
   int i, sub, roots;
413
0
   int ol_pitch;
414
0
   spx_word16_t ol_pitch_coef;
415
0
   spx_word32_t ol_gain;
416
0
   VARDECL(spx_word16_t *target);
417
0
   VARDECL(spx_sig_t *innov);
418
0
   VARDECL(spx_word32_t *exc32);
419
0
   VARDECL(spx_mem_t *mem);
420
0
   VARDECL(spx_coef_t *bw_lpc1);
421
0
   VARDECL(spx_coef_t *bw_lpc2);
422
0
   VARDECL(spx_coef_t *lpc);
423
0
   VARDECL(spx_lsp_t *lsp);
424
0
   VARDECL(spx_lsp_t *qlsp);
425
0
   VARDECL(spx_lsp_t *interp_lsp);
426
0
   VARDECL(spx_lsp_t *interp_qlsp);
427
0
   VARDECL(spx_coef_t *interp_lpc);
428
0
   VARDECL(spx_coef_t *interp_qlpc);
429
0
   char *stack;
430
0
   VARDECL(spx_word16_t *syn_resp);
431
432
0
   spx_word32_t ener=0;
433
0
   spx_word16_t fine_gain;
434
0
   spx_word16_t *in = (spx_word16_t*)vin;
435
436
0
   st=(EncState *)state;
437
0
   stack=st->stack;
438
439
0
   ALLOC(lpc, NB_ORDER, spx_coef_t);
440
0
   ALLOC(bw_lpc1, NB_ORDER, spx_coef_t);
441
0
   ALLOC(bw_lpc2, NB_ORDER, spx_coef_t);
442
0
   ALLOC(lsp, NB_ORDER, spx_lsp_t);
443
0
   ALLOC(qlsp, NB_ORDER, spx_lsp_t);
444
0
   ALLOC(interp_lsp, NB_ORDER, spx_lsp_t);
445
0
   ALLOC(interp_qlsp, NB_ORDER, spx_lsp_t);
446
0
   ALLOC(interp_lpc, NB_ORDER, spx_coef_t);
447
0
   ALLOC(interp_qlpc, NB_ORDER, spx_coef_t);
448
449
0
   st->exc = st->excBuf + NB_PITCH_END + 2;
450
0
   st->sw = st->swBuf + NB_PITCH_END + 2;
451
   /* Move signals 1 frame towards the past */
452
0
   SPEEX_MOVE(st->excBuf, st->excBuf+NB_FRAME_SIZE, NB_PITCH_END+2);
453
0
   SPEEX_MOVE(st->swBuf, st->swBuf+NB_FRAME_SIZE, NB_PITCH_END+2);
454
455
0
   if (st->highpass_enabled)
456
0
      highpass(in, in, NB_FRAME_SIZE, (st->isWideband?HIGHPASS_WIDEBAND:HIGHPASS_NARROWBAND)|HIGHPASS_INPUT, st->mem_hp);
457
458
0
   {
459
0
      VARDECL(spx_word16_t *w_sig);
460
0
      VARDECL(spx_word16_t *autocorr);
461
0
      ALLOC(w_sig, NB_WINDOW_SIZE, spx_word16_t);
462
0
      ALLOC(autocorr, NB_ORDER+1, spx_word16_t);
463
      /* Window for analysis */
464
0
      for (i=0;i<NB_WINDOW_SIZE-NB_FRAME_SIZE;i++)
465
0
         w_sig[i] = MULT16_16_Q15(st->winBuf[i],st->window[i]);
466
0
      for (;i<NB_WINDOW_SIZE;i++)
467
0
         w_sig[i] = MULT16_16_Q15(in[i-NB_WINDOW_SIZE+NB_FRAME_SIZE],st->window[i]);
468
      /* Compute auto-correlation */
469
0
      _spx_autocorr(w_sig, autocorr, NB_ORDER+1, NB_WINDOW_SIZE);
470
0
      autocorr[0] = ADD16(autocorr[0],MULT16_16_Q15(autocorr[0],st->lpc_floor)); /* Noise floor in auto-correlation domain */
471
472
      /* Lag windowing: equivalent to filtering in the power-spectrum domain */
473
0
      for (i=0;i<NB_ORDER+1;i++)
474
0
         autocorr[i] = MULT16_16_Q15(autocorr[i],st->lagWindow[i]);
475
0
      autocorr[0] = ADD16(autocorr[0],1);
476
477
      /* Levinson-Durbin */
478
0
      _spx_lpc(lpc, autocorr, NB_ORDER);
479
      /* LPC to LSPs (x-domain) transform */
480
0
      roots=lpc_to_lsp (lpc, NB_ORDER, lsp, 10, LSP_DELTA1, stack);
481
      /* Check if we found all the roots */
482
0
      if (roots!=NB_ORDER)
483
0
      {
484
         /*If we can't find all LSP's, do some damage control and use previous filter*/
485
0
         for (i=0;i<NB_ORDER;i++)
486
0
         {
487
0
            lsp[i]=st->old_lsp[i];
488
0
         }
489
0
      }
490
0
   }
491
492
493
494
495
   /* Whole frame analysis (open-loop estimation of pitch and excitation gain) */
496
0
   {
497
0
      int diff = NB_WINDOW_SIZE-NB_FRAME_SIZE;
498
0
      if (st->first)
499
0
         for (i=0;i<NB_ORDER;i++)
500
0
            interp_lsp[i] = lsp[i];
501
0
      else
502
0
         lsp_interpolate(st->old_lsp, lsp, interp_lsp, NB_ORDER, NB_NB_SUBFRAMES, NB_NB_SUBFRAMES<<1, LSP_MARGIN);
503
504
      /* Compute interpolated LPCs (unquantized) for whole frame*/
505
0
      lsp_to_lpc(interp_lsp, interp_lpc, NB_ORDER,stack);
506
507
508
      /*Open-loop pitch*/
509
0
      if (!st->submodes[st->submodeID] || (st->complexity>2 && SUBMODE(have_subframe_gain)<3) || SUBMODE(forced_pitch_gain) || SUBMODE(lbr_pitch) != -1
510
0
#ifndef DISABLE_VBR
511
0
           || st->vbr_enabled || st->vad_enabled
512
0
#endif
513
0
                  )
514
0
      {
515
0
         int nol_pitch[6];
516
0
         spx_word16_t nol_pitch_coef[6];
517
518
0
         bw_lpc(0.9, interp_lpc, bw_lpc1, NB_ORDER);
519
0
         bw_lpc(0.55, interp_lpc, bw_lpc2, NB_ORDER);
520
521
0
         SPEEX_COPY(st->sw, st->winBuf, diff);
522
0
         SPEEX_COPY(st->sw+diff, in, NB_FRAME_SIZE-diff);
523
0
         filter10(st->sw, bw_lpc1, bw_lpc2, st->sw, NB_FRAME_SIZE, st->mem_sw_whole, stack);
524
525
0
         open_loop_nbest_pitch(st->sw, NB_PITCH_START, NB_PITCH_END, NB_FRAME_SIZE,
526
0
                               nol_pitch, nol_pitch_coef, 6, stack);
527
0
         ol_pitch=nol_pitch[0];
528
0
         ol_pitch_coef = nol_pitch_coef[0];
529
         /*Try to remove pitch multiples*/
530
0
         for (i=1;i<6;i++)
531
0
         {
532
#ifdef FIXED_POINT
533
            if ((nol_pitch_coef[i]>MULT16_16_Q15(nol_pitch_coef[0],27853)) &&
534
#else
535
0
            if ((nol_pitch_coef[i]>.85*nol_pitch_coef[0]) &&
536
0
#endif
537
0
                (ABS(2*nol_pitch[i]-ol_pitch)<=2 || ABS(3*nol_pitch[i]-ol_pitch)<=3 ||
538
0
                 ABS(4*nol_pitch[i]-ol_pitch)<=4 || ABS(5*nol_pitch[i]-ol_pitch)<=5))
539
0
            {
540
               /*ol_pitch_coef=nol_pitch_coef[i];*/
541
0
               ol_pitch = nol_pitch[i];
542
0
            }
543
0
         }
544
         /*if (ol_pitch>50)
545
           ol_pitch/=2;*/
546
         /*ol_pitch_coef = sqrt(ol_pitch_coef);*/
547
548
0
      } else {
549
0
         ol_pitch=0;
550
0
         ol_pitch_coef=0;
551
0
      }
552
553
      /*Compute "real" excitation*/
554
      /*SPEEX_COPY(st->exc, st->winBuf, diff);
555
      SPEEX_COPY(st->exc+diff, in, NB_FRAME_SIZE-diff);*/
556
0
      fir_mem16(st->winBuf, interp_lpc, st->exc, diff, NB_ORDER, st->mem_exc, stack);
557
0
      fir_mem16(in, interp_lpc, st->exc+diff, NB_FRAME_SIZE-diff, NB_ORDER, st->mem_exc, stack);
558
559
      /* Compute open-loop excitation gain */
560
0
      {
561
0
         spx_word16_t g = compute_rms16(st->exc, NB_FRAME_SIZE);
562
0
         if (st->submodeID!=1 && ol_pitch>0)
563
0
            ol_gain = MULT16_16(g, MULT16_16_Q14(QCONST16(1.1,14),
564
0
                                spx_sqrt(QCONST32(1.,28)-MULT16_32_Q15(QCONST16(.8,15),SHL32(MULT16_16(ol_pitch_coef,ol_pitch_coef),16)))));
565
0
         else
566
0
            ol_gain = SHL32(EXTEND32(g),SIG_SHIFT);
567
0
      }
568
0
   }
569
570
#ifdef VORBIS_PSYCHO
571
   SPEEX_MOVE(st->psy_window, st->psy_window+NB_FRAME_SIZE, 256-NB_FRAME_SIZE);
572
   SPEEX_COPY(&st->psy_window[256-NB_FRAME_SIZE], in, NB_FRAME_SIZE);
573
   compute_curve(st->psy, st->psy_window, st->curve);
574
   /*print_vec(st->curve, 128, "curve");*/
575
   if (st->first)
576
      SPEEX_COPY(st->old_curve, st->curve, 128);
577
#endif
578
579
   /*VBR stuff*/
580
0
#ifndef DISABLE_VBR
581
0
   if (st->vbr_enabled||st->vad_enabled)
582
0
   {
583
0
      float lsp_dist=0;
584
0
      for (i=0;i<NB_ORDER;i++)
585
0
         lsp_dist += (st->old_lsp[i] - lsp[i])*(st->old_lsp[i] - lsp[i]);
586
0
      lsp_dist /= LSP_SCALING*LSP_SCALING;
587
588
0
      if (st->abr_enabled)
589
0
      {
590
0
         float qual_change=0;
591
0
         if (st->abr_drift2 * st->abr_drift > 0)
592
0
         {
593
            /* Only adapt if long-term and short-term drift are the same sign */
594
0
            qual_change = -.00001*st->abr_drift/(1+st->abr_count);
595
0
            if (qual_change>.05)
596
0
               qual_change=.05;
597
0
            if (qual_change<-.05)
598
0
               qual_change=-.05;
599
0
         }
600
0
         st->vbr_quality += qual_change;
601
0
         if (st->vbr_quality>10)
602
0
            st->vbr_quality=10;
603
0
         if (st->vbr_quality<0)
604
0
            st->vbr_quality=0;
605
0
      }
606
607
0
      st->relative_quality = vbr_analysis(&st->vbr, in, NB_FRAME_SIZE, ol_pitch, GAIN_SCALING_1*ol_pitch_coef);
608
      /*if (delta_qual<0)*/
609
      /*  delta_qual*=.1*(3+st->vbr_quality);*/
610
0
      if (st->vbr_enabled)
611
0
      {
612
0
         spx_int32_t mode;
613
0
         int choice=0;
614
0
         float min_diff=100;
615
0
         mode = 8;
616
0
         while (mode)
617
0
         {
618
0
            int v1;
619
0
            float thresh;
620
0
            v1=(int)floor(st->vbr_quality);
621
0
            if (v1==10)
622
0
               thresh = vbr_nb_thresh[mode][v1];
623
0
            else
624
0
               thresh = (st->vbr_quality-v1)*vbr_nb_thresh[mode][v1+1] + (1+v1-st->vbr_quality)*vbr_nb_thresh[mode][v1];
625
0
            if (st->relative_quality > thresh &&
626
0
                st->relative_quality-thresh<min_diff)
627
0
            {
628
0
               choice = mode;
629
0
               min_diff = st->relative_quality-thresh;
630
0
            }
631
0
            mode--;
632
0
         }
633
0
         mode=choice;
634
0
         if (mode==0)
635
0
         {
636
0
            if (st->dtx_count==0 || lsp_dist>.05 || !st->dtx_enabled || st->dtx_count>20)
637
0
            {
638
0
               mode=1;
639
0
               st->dtx_count=1;
640
0
            } else {
641
0
               mode=0;
642
0
               st->dtx_count++;
643
0
            }
644
0
         } else {
645
0
            st->dtx_count=0;
646
0
         }
647
648
0
         speex_encoder_ctl(state, SPEEX_SET_MODE, &mode);
649
0
         if (st->vbr_max>0)
650
0
         {
651
0
            spx_int32_t rate;
652
0
            speex_encoder_ctl(state, SPEEX_GET_BITRATE, &rate);
653
0
            if (rate > st->vbr_max)
654
0
            {
655
0
               rate = st->vbr_max;
656
0
               speex_encoder_ctl(state, SPEEX_SET_BITRATE, &rate);
657
0
            }
658
0
         }
659
660
0
         if (st->abr_enabled)
661
0
         {
662
0
            spx_int32_t bitrate;
663
0
            speex_encoder_ctl(state, SPEEX_GET_BITRATE, &bitrate);
664
0
            st->abr_drift+=(bitrate-st->abr_enabled);
665
0
            st->abr_drift2 = .95*st->abr_drift2 + .05*(bitrate-st->abr_enabled);
666
0
            st->abr_count += 1.0;
667
0
         }
668
669
0
      } else {
670
         /*VAD only case*/
671
0
         int mode;
672
0
         if (st->relative_quality<2)
673
0
         {
674
0
            if (st->dtx_count==0 || lsp_dist>.05 || !st->dtx_enabled || st->dtx_count>20)
675
0
            {
676
0
               st->dtx_count=1;
677
0
               mode=1;
678
0
            } else {
679
0
               mode=0;
680
0
               st->dtx_count++;
681
0
            }
682
0
         } else {
683
0
            st->dtx_count = 0;
684
0
            mode=st->submodeSelect;
685
0
         }
686
         /*speex_encoder_ctl(state, SPEEX_SET_MODE, &mode);*/
687
0
         st->submodeID=mode;
688
0
      }
689
0
   } else {
690
0
      st->relative_quality = -1;
691
0
   }
692
0
#endif /* #ifndef DISABLE_VBR */
693
694
0
   if (st->encode_submode)
695
0
   {
696
      /* First, transmit a zero for narrowband */
697
0
      speex_bits_pack(bits, 0, 1);
698
699
      /* Transmit the sub-mode we use for this frame */
700
0
      speex_bits_pack(bits, st->submodeID, NB_SUBMODE_BITS);
701
702
0
   }
703
704
   /* If null mode (no transmission), just set a couple things to zero*/
705
0
   if (st->submodes[st->submodeID] == NULL)
706
0
   {
707
0
      for (i=0;i<NB_FRAME_SIZE;i++)
708
0
         st->exc[i]=st->sw[i]=VERY_SMALL;
709
710
0
      for (i=0;i<NB_ORDER;i++)
711
0
         st->mem_sw[i]=0;
712
0
      st->first=1;
713
0
      st->bounded_pitch = 1;
714
715
0
      SPEEX_COPY(st->winBuf, in+2*NB_FRAME_SIZE-NB_WINDOW_SIZE, NB_WINDOW_SIZE-NB_FRAME_SIZE);
716
717
      /* Clear memory (no need to really compute it) */
718
0
      for (i=0;i<NB_ORDER;i++)
719
0
         st->mem_sp[i] = 0;
720
0
      return 0;
721
722
0
   }
723
724
   /* LSP Quantization */
725
0
   if (st->first)
726
0
   {
727
0
      for (i=0;i<NB_ORDER;i++)
728
0
         st->old_lsp[i] = lsp[i];
729
0
   }
730
731
732
   /*Quantize LSPs*/
733
0
#if 1 /*0 for unquantized*/
734
0
   SUBMODE(lsp_quant)(lsp, qlsp, NB_ORDER, bits);
735
#else
736
   for (i=0;i<NB_ORDER;i++)
737
     qlsp[i]=lsp[i];
738
#endif
739
740
   /*If we use low bit-rate pitch mode, transmit open-loop pitch*/
741
0
   if (SUBMODE(lbr_pitch)!=-1)
742
0
   {
743
0
      speex_bits_pack(bits, ol_pitch-NB_PITCH_START, 7);
744
0
   }
745
746
0
   if (SUBMODE(forced_pitch_gain))
747
0
   {
748
0
      int quant;
749
      /* This just damps the pitch a bit, because it tends to be too aggressive when forced */
750
0
      ol_pitch_coef = MULT16_16_Q15(QCONST16(.9,15), ol_pitch_coef);
751
#ifdef FIXED_POINT
752
      quant = PSHR16(MULT16_16_16(15, ol_pitch_coef),GAIN_SHIFT);
753
#else
754
0
      quant = (int)floor(.5+15*ol_pitch_coef*GAIN_SCALING_1);
755
0
#endif
756
0
      if (quant>15)
757
0
         quant=15;
758
0
      if (quant<0)
759
0
         quant=0;
760
0
      speex_bits_pack(bits, quant, 4);
761
0
      ol_pitch_coef=MULT16_16_P15(QCONST16(0.066667,15),SHL16(quant,GAIN_SHIFT));
762
0
   }
763
764
765
   /*Quantize and transmit open-loop excitation gain*/
766
#ifdef FIXED_POINT
767
   {
768
      int qe = scal_quant32(ol_gain, ol_gain_table, 32);
769
      /*ol_gain = exp(qe/3.5)*SIG_SCALING;*/
770
      ol_gain = MULT16_32_Q15(28406,ol_gain_table[qe]);
771
      speex_bits_pack(bits, qe, 5);
772
   }
773
#else
774
0
   {
775
0
      int qe = (int)(floor(.5+3.5*log(ol_gain*1.0/SIG_SCALING)));
776
0
      if (qe<0)
777
0
         qe=0;
778
0
      if (qe>31)
779
0
         qe=31;
780
0
      ol_gain = exp(qe/3.5)*SIG_SCALING;
781
0
      speex_bits_pack(bits, qe, 5);
782
0
   }
783
0
#endif
784
785
786
787
   /* Special case for first frame */
788
0
   if (st->first)
789
0
   {
790
0
      for (i=0;i<NB_ORDER;i++)
791
0
         st->old_qlsp[i] = qlsp[i];
792
0
   }
793
794
   /* Target signal */
795
0
   ALLOC(target, NB_SUBFRAME_SIZE, spx_word16_t);
796
0
   ALLOC(innov, NB_SUBFRAME_SIZE, spx_sig_t);
797
0
   ALLOC(exc32, NB_SUBFRAME_SIZE, spx_word32_t);
798
0
   ALLOC(syn_resp, NB_SUBFRAME_SIZE, spx_word16_t);
799
0
   ALLOC(mem, NB_ORDER, spx_mem_t);
800
801
   /* Loop on sub-frames */
802
0
   for (sub=0;sub<NB_NB_SUBFRAMES;sub++)
803
0
   {
804
0
      int   offset;
805
0
      spx_word16_t *sw;
806
0
      spx_word16_t *exc, *inBuf;
807
0
      int pitch;
808
0
      int response_bound = NB_SUBFRAME_SIZE;
809
810
      /* Offset relative to start of frame */
811
0
      offset = NB_SUBFRAME_SIZE*sub;
812
      /* Excitation */
813
0
      exc=st->exc+offset;
814
      /* Weighted signal */
815
0
      sw=st->sw+offset;
816
817
      /* LSP interpolation (quantized and unquantized) */
818
0
      lsp_interpolate(st->old_lsp, lsp, interp_lsp, NB_ORDER, sub, NB_NB_SUBFRAMES, LSP_MARGIN);
819
0
      lsp_interpolate(st->old_qlsp, qlsp, interp_qlsp, NB_ORDER, sub, NB_NB_SUBFRAMES, LSP_MARGIN);
820
821
      /* Compute interpolated LPCs (quantized and unquantized) */
822
0
      lsp_to_lpc(interp_lsp, interp_lpc, NB_ORDER,stack);
823
824
0
      lsp_to_lpc(interp_qlsp, interp_qlpc, NB_ORDER, stack);
825
826
      /* Compute analysis filter gain at w=pi (for use in SB-CELP) */
827
0
      {
828
0
         spx_word32_t pi_g=LPC_SCALING;
829
0
         for (i=0;i<NB_ORDER;i+=2)
830
0
         {
831
            /*pi_g += -st->interp_qlpc[i] +  st->interp_qlpc[i+1];*/
832
0
            pi_g = ADD32(pi_g, SUB32(EXTEND32(interp_qlpc[i+1]),EXTEND32(interp_qlpc[i])));
833
0
         }
834
0
         st->pi_gain[sub] = pi_g;
835
0
      }
836
837
#ifdef VORBIS_PSYCHO
838
      {
839
         float curr_curve[128];
840
         float fact = ((float)sub+1.0f)/NB_NB_SUBFRAMES;
841
         for (i=0;i<128;i++)
842
            curr_curve[i] = (1.0f-fact)*st->old_curve[i] + fact*st->curve[i];
843
         curve_to_lpc(st->psy, curr_curve, bw_lpc1, bw_lpc2, 10);
844
      }
845
#else
846
      /* Compute bandwidth-expanded (unquantized) LPCs for perceptual weighting */
847
0
      bw_lpc(st->gamma1, interp_lpc, bw_lpc1, NB_ORDER);
848
0
      bw_lpc(st->gamma2, interp_lpc, bw_lpc2, NB_ORDER);
849
      /*print_vec(st->bw_lpc1, 10, "bw_lpc");*/
850
0
#endif
851
852
      /*FIXME: This will break if we change the window size */
853
0
      speex_assert(NB_WINDOW_SIZE-NB_FRAME_SIZE == NB_SUBFRAME_SIZE);
854
0
      if (sub==0)
855
0
         inBuf = st->winBuf;
856
0
      else
857
0
         inBuf = &in[((sub-1)*NB_SUBFRAME_SIZE)];
858
0
      for (i=0;i<NB_SUBFRAME_SIZE;i++)
859
0
         sw[i] = inBuf[i];
860
861
0
      if (st->complexity==0)
862
0
         response_bound >>= 1;
863
0
      compute_impulse_response(interp_qlpc, bw_lpc1, bw_lpc2, syn_resp, response_bound, NB_ORDER, stack);
864
0
      for (i=response_bound;i<NB_SUBFRAME_SIZE;i++)
865
0
         syn_resp[i]=VERY_SMALL;
866
867
      /* Compute zero response of A(z/g1) / ( A(z/g2) * A(z) ) */
868
0
      for (i=0;i<NB_ORDER;i++)
869
0
         mem[i]=SHL32(st->mem_sp[i],1);
870
0
      for (i=0;i<NB_SUBFRAME_SIZE;i++)
871
0
         exc[i] = VERY_SMALL;
872
#ifdef SHORTCUTS2
873
      iir_mem16(exc, interp_qlpc, exc, response_bound, NB_ORDER, mem, stack);
874
      for (i=0;i<NB_ORDER;i++)
875
         mem[i]=SHL32(st->mem_sw[i],1);
876
      filter10(exc, st->bw_lpc1, st->bw_lpc2, exc, response_bound, mem, stack);
877
      SPEEX_MEMSET(&exc[response_bound], 0, NB_SUBFRAME_SIZE-response_bound);
878
#else
879
0
      iir_mem16(exc, interp_qlpc, exc, NB_SUBFRAME_SIZE, NB_ORDER, mem, stack);
880
0
      for (i=0;i<NB_ORDER;i++)
881
0
         mem[i]=SHL32(st->mem_sw[i],1);
882
0
      filter10(exc, bw_lpc1, bw_lpc2, exc, NB_SUBFRAME_SIZE, mem, stack);
883
0
#endif
884
885
      /* Compute weighted signal */
886
0
      for (i=0;i<NB_ORDER;i++)
887
0
         mem[i]=st->mem_sw[i];
888
0
      filter10(sw, bw_lpc1, bw_lpc2, sw, NB_SUBFRAME_SIZE, mem, stack);
889
890
0
      if (st->complexity==0)
891
0
         for (i=0;i<NB_ORDER;i++)
892
0
            st->mem_sw[i]=mem[i];
893
894
      /* Compute target signal (saturation prevents overflows on clipped input speech) */
895
0
      for (i=0;i<NB_SUBFRAME_SIZE;i++)
896
0
         target[i]=EXTRACT16(SATURATE(SUB32(sw[i],PSHR32(exc[i],1)),32767));
897
898
0
      for (i=0;i<NB_SUBFRAME_SIZE;i++)
899
0
         exc[i] = inBuf[i];
900
0
      fir_mem16(exc, interp_qlpc, exc, NB_SUBFRAME_SIZE, NB_ORDER, st->mem_exc2, stack);
901
      /* If we have a long-term predictor (otherwise, something's wrong) */
902
0
      speex_assert (SUBMODE(ltp_quant));
903
0
      {
904
0
         int pit_min, pit_max;
905
         /* Long-term prediction */
906
0
         if (SUBMODE(lbr_pitch) != -1)
907
0
         {
908
            /* Low bit-rate pitch handling */
909
0
            int margin;
910
0
            margin = SUBMODE(lbr_pitch);
911
0
            if (margin)
912
0
            {
913
0
               if (ol_pitch < NB_PITCH_START+margin-1)
914
0
                  ol_pitch=NB_PITCH_START+margin-1;
915
0
               if (ol_pitch > NB_PITCH_END-margin)
916
0
                  ol_pitch=NB_PITCH_END-margin;
917
0
               pit_min = ol_pitch-margin+1;
918
0
               pit_max = ol_pitch+margin;
919
0
            } else {
920
0
               pit_min=pit_max=ol_pitch;
921
0
            }
922
0
         } else {
923
0
            pit_min = NB_PITCH_START;
924
0
            pit_max = NB_PITCH_END;
925
0
         }
926
927
         /* Force pitch to use only the current frame if needed */
928
0
         if (st->bounded_pitch && pit_max>offset)
929
0
            pit_max=offset;
930
931
         /* Perform pitch search */
932
0
         pitch = SUBMODE(ltp_quant)(target, sw, interp_qlpc, bw_lpc1, bw_lpc2,
933
0
                                    exc32, SUBMODE(ltp_params), pit_min, pit_max, ol_pitch_coef,
934
0
                                    NB_ORDER, NB_SUBFRAME_SIZE, bits, stack,
935
0
                                    exc, syn_resp, st->complexity, 0, st->plc_tuning, &st->cumul_gain);
936
937
0
         st->pitch[sub]=pitch;
938
0
      }
939
      /* Quantization of innovation */
940
0
      SPEEX_MEMSET(innov, 0, NB_SUBFRAME_SIZE);
941
942
      /* FIXME: Make sure this is safe from overflows (so far so good) */
943
0
      for (i=0;i<NB_SUBFRAME_SIZE;i++)
944
0
         exc[i] = EXTRACT16(SUB32(EXTEND32(exc[i]), PSHR32(exc32[i],SIG_SHIFT-1)));
945
946
0
      ener = SHL32(EXTEND32(compute_rms16(exc, NB_SUBFRAME_SIZE)),SIG_SHIFT);
947
948
      /*FIXME: Should use DIV32_16 and make sure result fits in 16 bits */
949
#ifdef FIXED_POINT
950
      {
951
         spx_word32_t f = PDIV32(ener,PSHR32(ol_gain,SIG_SHIFT));
952
         if (f<=32767)
953
            fine_gain = f;
954
         else
955
            fine_gain = 32767;
956
      }
957
#else
958
0
      fine_gain = PDIV32_16(ener,PSHR32(ol_gain,SIG_SHIFT));
959
0
#endif
960
      /* Calculate gain correction for the sub-frame (if any) */
961
0
      if (SUBMODE(have_subframe_gain))
962
0
      {
963
0
         int qe;
964
0
         if (SUBMODE(have_subframe_gain)==3)
965
0
         {
966
0
            qe = scal_quant(fine_gain, exc_gain_quant_scal3_bound, 8);
967
0
            speex_bits_pack(bits, qe, 3);
968
0
            ener=MULT16_32_Q14(exc_gain_quant_scal3[qe],ol_gain);
969
0
         } else {
970
0
            qe = scal_quant(fine_gain, exc_gain_quant_scal1_bound, 2);
971
0
            speex_bits_pack(bits, qe, 1);
972
0
            ener=MULT16_32_Q14(exc_gain_quant_scal1[qe],ol_gain);
973
0
         }
974
0
      } else {
975
0
         ener=ol_gain;
976
0
      }
977
978
      /*printf ("%f %f\n", ener, ol_gain);*/
979
980
      /* Normalize innovation */
981
0
      signal_div(target, target, ener, NB_SUBFRAME_SIZE);
982
983
      /* Quantize innovation */
984
0
      speex_assert (SUBMODE(innovation_quant));
985
0
      {
986
         /* Codebook search */
987
0
         SUBMODE(innovation_quant)(target, interp_qlpc, bw_lpc1, bw_lpc2,
988
0
                  SUBMODE(innovation_params), NB_ORDER, NB_SUBFRAME_SIZE,
989
0
                  innov, syn_resp, bits, stack, st->complexity, SUBMODE(double_codebook));
990
991
         /* De-normalize innovation and update excitation */
992
0
         signal_mul(innov, innov, ener, NB_SUBFRAME_SIZE);
993
994
         /* In some (rare) modes, we do a second search (more bits) to reduce noise even more */
995
0
         if (SUBMODE(double_codebook)) {
996
0
            char *tmp_stack=stack;
997
0
            VARDECL(spx_sig_t *innov2);
998
0
            ALLOC(innov2, NB_SUBFRAME_SIZE, spx_sig_t);
999
0
            SPEEX_MEMSET(innov2, 0, NB_SUBFRAME_SIZE);
1000
0
            for (i=0;i<NB_SUBFRAME_SIZE;i++)
1001
0
               target[i]=MULT16_16_P13(QCONST16(2.2f,13), target[i]);
1002
0
            SUBMODE(innovation_quant)(target, interp_qlpc, bw_lpc1, bw_lpc2,
1003
0
                                      SUBMODE(innovation_params), NB_ORDER, NB_SUBFRAME_SIZE,
1004
0
                                      innov2, syn_resp, bits, stack, st->complexity, 0);
1005
0
            signal_mul(innov2, innov2, MULT16_32_Q15(QCONST16(0.454545f,15),ener), NB_SUBFRAME_SIZE);
1006
0
            for (i=0;i<NB_SUBFRAME_SIZE;i++)
1007
0
               innov[i] = ADD32(innov[i],innov2[i]);
1008
0
            stack = tmp_stack;
1009
0
         }
1010
0
         for (i=0;i<NB_SUBFRAME_SIZE;i++)
1011
0
            exc[i] = EXTRACT16(SATURATE32(PSHR32(ADD32(SHL32(exc32[i],1),innov[i]),SIG_SHIFT),32767));
1012
0
         if (st->innov_rms_save)
1013
0
            st->innov_rms_save[sub] = compute_rms(innov, NB_SUBFRAME_SIZE);
1014
0
      }
1015
1016
      /* Final signal synthesis from excitation */
1017
0
      iir_mem16(exc, interp_qlpc, sw, NB_SUBFRAME_SIZE, NB_ORDER, st->mem_sp, stack);
1018
1019
      /* Compute weighted signal again, from synthesized speech (not sure it's the right thing) */
1020
0
      if (st->complexity!=0)
1021
0
         filter10(sw, bw_lpc1, bw_lpc2, sw, NB_SUBFRAME_SIZE, st->mem_sw, stack);
1022
1023
0
   }
1024
1025
   /* Store the LSPs for interpolation in the next frame */
1026
0
   if (st->submodeID>=1)
1027
0
   {
1028
0
      for (i=0;i<NB_ORDER;i++)
1029
0
         st->old_lsp[i] = lsp[i];
1030
0
      for (i=0;i<NB_ORDER;i++)
1031
0
         st->old_qlsp[i] = qlsp[i];
1032
0
   }
1033
1034
#ifdef VORBIS_PSYCHO
1035
   if (st->submodeID>=1)
1036
      SPEEX_COPY(st->old_curve, st->curve, 128);
1037
#endif
1038
1039
0
   if (st->submodeID==1)
1040
0
   {
1041
0
#ifndef DISABLE_VBR
1042
0
      if (st->dtx_count)
1043
0
         speex_bits_pack(bits, 15, 4);
1044
0
      else
1045
0
#endif
1046
0
         speex_bits_pack(bits, 0, 4);
1047
0
   }
1048
1049
   /* The next frame will not be the first (Duh!) */
1050
0
   st->first = 0;
1051
0
   SPEEX_COPY(st->winBuf, in+2*NB_FRAME_SIZE-NB_WINDOW_SIZE, NB_WINDOW_SIZE-NB_FRAME_SIZE);
1052
1053
0
   if (SUBMODE(innovation_quant) == noise_codebook_quant || st->submodeID==0)
1054
0
      st->bounded_pitch = 1;
1055
0
   else
1056
0
      st->bounded_pitch = 0;
1057
1058
0
   return 1;
1059
0
}
1060
#endif /* DISABLE_ENCODER */
1061
1062
1063
#ifndef DISABLE_DECODER
1064
void *nb_decoder_init(const SpeexMode *m)
1065
3.07k
{
1066
3.07k
   DecState *st;
1067
3.07k
   const SpeexNBMode *mode;
1068
3.07k
   int i;
1069
1070
3.07k
   mode=(const SpeexNBMode*)m->mode;
1071
3.07k
   st = (DecState *)speex_alloc(sizeof(DecState));
1072
3.07k
   if (!st)
1073
0
      return NULL;
1074
3.07k
#if defined(VAR_ARRAYS) || defined (USE_ALLOCA)
1075
3.07k
   st->stack = NULL;
1076
#else
1077
   st->stack = (char*)speex_alloc_scratch(NB_DEC_STACK);
1078
#endif
1079
1080
3.07k
   st->mode=m;
1081
1082
1083
3.07k
   st->encode_submode = 1;
1084
1085
3.07k
   st->first=1;
1086
   /* Codec parameters, should eventually have several "modes"*/
1087
1088
3.07k
   st->submodes=mode->submodes;
1089
3.07k
   st->submodeID=mode->defaultSubmode;
1090
1091
3.07k
   st->lpc_enh_enabled=1;
1092
1093
3.07k
   SPEEX_MEMSET(st->excBuf, 0, NB_FRAME_SIZE + NB_PITCH_END);
1094
1095
3.07k
   st->last_pitch = 40;
1096
3.07k
   st->count_lost=0;
1097
3.07k
   st->pitch_gain_buf[0] = st->pitch_gain_buf[1] = st->pitch_gain_buf[2] = 0;
1098
3.07k
   st->pitch_gain_buf_idx = 0;
1099
3.07k
   st->seed = 1000;
1100
1101
3.07k
   st->sampling_rate=8000;
1102
3.07k
   st->last_ol_gain = 0;
1103
1104
3.07k
   st->user_callback.func = &speex_default_user_handler;
1105
3.07k
   st->user_callback.data = NULL;
1106
52.1k
   for (i=0;i<16;i++)
1107
49.1k
      st->speex_callbacks[i].func = NULL;
1108
1109
3.07k
   st->voc_m1=st->voc_m2=st->voc_mean=0;
1110
3.07k
   st->voc_offset=0;
1111
3.07k
   st->dtx_enabled=0;
1112
3.07k
   st->isWideband = 0;
1113
3.07k
   st->highpass_enabled = 1;
1114
1115
#ifdef ENABLE_VALGRIND
1116
   VALGRIND_MAKE_MEM_DEFINED(st, NB_DEC_STACK);
1117
#endif
1118
3.07k
   return st;
1119
3.07k
}
1120
1121
void nb_decoder_destroy(void *state)
1122
3.07k
{
1123
#if !(defined(VAR_ARRAYS) || defined (USE_ALLOCA))
1124
   DecState *st;
1125
   st=(DecState*)state;
1126
1127
   speex_free_scratch(st->stack);
1128
#endif
1129
1130
3.07k
   speex_free(state);
1131
3.07k
}
1132
1133
int nb_decoder_ctl(void *state, int request, void *ptr)
1134
134k
{
1135
134k
   DecState *st;
1136
134k
   st=(DecState*)state;
1137
134k
   switch(request)
1138
134k
   {
1139
0
   case SPEEX_SET_LOW_MODE:
1140
0
   case SPEEX_SET_MODE:
1141
0
      st->submodeID = (*(spx_int32_t*)ptr);
1142
0
      break;
1143
0
   case SPEEX_GET_LOW_MODE:
1144
0
   case SPEEX_GET_MODE:
1145
0
      (*(spx_int32_t*)ptr) = st->submodeID;
1146
0
      break;
1147
3.07k
   case SPEEX_SET_ENH:
1148
3.07k
      st->lpc_enh_enabled = *((spx_int32_t*)ptr);
1149
3.07k
      break;
1150
0
   case SPEEX_GET_ENH:
1151
0
      *((spx_int32_t*)ptr) = st->lpc_enh_enabled;
1152
0
      break;
1153
1.39k
   case SPEEX_GET_FRAME_SIZE:
1154
1.39k
      (*(spx_int32_t*)ptr) = NB_FRAME_SIZE;
1155
1.39k
      break;
1156
0
   case SPEEX_GET_BITRATE:
1157
0
      if (st->submodes[st->submodeID])
1158
0
         (*(spx_int32_t*)ptr) = st->sampling_rate*SUBMODE(bits_per_frame)/NB_FRAME_SIZE;
1159
0
      else
1160
0
         (*(spx_int32_t*)ptr) = st->sampling_rate*(NB_SUBMODE_BITS+1)/NB_FRAME_SIZE;
1161
0
      break;
1162
3.07k
   case SPEEX_SET_SAMPLING_RATE:
1163
3.07k
      st->sampling_rate = (*(spx_int32_t*)ptr);
1164
3.07k
      break;
1165
1.67k
   case SPEEX_GET_SAMPLING_RATE:
1166
1.67k
      (*(spx_int32_t*)ptr)=st->sampling_rate;
1167
1.67k
      break;
1168
1.29k
   case SPEEX_SET_HANDLER:
1169
1.29k
      {
1170
1.29k
         SpeexCallback *c = (SpeexCallback*)ptr;
1171
1.29k
         st->speex_callbacks[c->callback_id].func=c->func;
1172
1.29k
         st->speex_callbacks[c->callback_id].data=c->data;
1173
1.29k
         st->speex_callbacks[c->callback_id].callback_id=c->callback_id;
1174
1.29k
      }
1175
1.29k
      break;
1176
0
   case SPEEX_SET_USER_HANDLER:
1177
0
      {
1178
0
         SpeexCallback *c = (SpeexCallback*)ptr;
1179
0
         st->user_callback.func=c->func;
1180
0
         st->user_callback.data=c->data;
1181
0
         st->user_callback.callback_id=c->callback_id;
1182
0
      }
1183
0
      break;
1184
0
   case SPEEX_RESET_STATE:
1185
0
      {
1186
0
         int i;
1187
0
         for (i=0;i<NB_ORDER;i++)
1188
0
            st->mem_sp[i]=0;
1189
0
         for (i=0;i<NB_FRAME_SIZE + NB_PITCH_END + 1;i++)
1190
0
            st->excBuf[i]=0;
1191
0
      }
1192
0
      break;
1193
0
   case SPEEX_SET_SUBMODE_ENCODING:
1194
0
      st->encode_submode = (*(spx_int32_t*)ptr);
1195
0
      break;
1196
0
   case SPEEX_GET_SUBMODE_ENCODING:
1197
0
      (*(spx_int32_t*)ptr) = st->encode_submode;
1198
0
      break;
1199
2.91k
   case SPEEX_GET_LOOKAHEAD:
1200
2.91k
      (*(spx_int32_t*)ptr)=NB_SUBFRAME_SIZE;
1201
2.91k
      break;
1202
0
   case SPEEX_SET_HIGHPASS:
1203
0
      st->highpass_enabled = (*(spx_int32_t*)ptr);
1204
0
      break;
1205
0
   case SPEEX_GET_HIGHPASS:
1206
0
      (*(spx_int32_t*)ptr) = st->highpass_enabled;
1207
0
      break;
1208
      /* FIXME: Convert to fixed-point and re-enable even when float API is disabled */
1209
0
#ifndef DISABLE_FLOAT_API
1210
0
   case SPEEX_GET_ACTIVITY:
1211
0
   {
1212
0
      float ret;
1213
0
      ret = log(st->level/st->min_level)/log(st->max_level/st->min_level);
1214
0
      if (ret>1)
1215
0
         ret = 1;
1216
      /* Done in a strange way to catch NaNs as well */
1217
0
      if (!(ret > 0))
1218
0
         ret = 0;
1219
      /*printf ("%f %f %f %f\n", st->level, st->min_level, st->max_level, ret);*/
1220
0
      (*(spx_int32_t*)ptr) = (int)(100*ret);
1221
0
   }
1222
0
   break;
1223
0
#endif
1224
3.56k
   case SPEEX_GET_PI_GAIN:
1225
3.56k
      {
1226
3.56k
         int i;
1227
3.56k
         spx_word32_t *g = (spx_word32_t*)ptr;
1228
17.8k
         for (i=0;i<NB_NB_SUBFRAMES;i++)
1229
14.2k
            g[i]=st->pi_gain[i];
1230
3.56k
      }
1231
3.56k
      break;
1232
3.56k
   case SPEEX_GET_EXC:
1233
3.56k
      {
1234
3.56k
         int i;
1235
17.8k
         for (i=0;i<NB_NB_SUBFRAMES;i++)
1236
14.2k
            ((spx_word16_t*)ptr)[i] = compute_rms16(st->exc+i*NB_SUBFRAME_SIZE, NB_SUBFRAME_SIZE);
1237
3.56k
      }
1238
3.56k
      break;
1239
68.6k
   case SPEEX_GET_DTX_STATUS:
1240
68.6k
      *((spx_int32_t*)ptr) = st->dtx_enabled;
1241
68.6k
      break;
1242
42.4k
   case SPEEX_SET_INNOVATION_SAVE:
1243
42.4k
      st->innov_save = (spx_word16_t*)ptr;
1244
42.4k
      break;
1245
2.65k
   case SPEEX_SET_WIDEBAND:
1246
2.65k
      st->isWideband = *((spx_int32_t*)ptr);
1247
2.65k
      break;
1248
0
   case SPEEX_GET_STACK:
1249
0
      *((char**)ptr) = st->stack;
1250
0
      break;
1251
0
   default:
1252
0
      speex_warning_int("Unknown nb_ctl request: ", request);
1253
0
      return -1;
1254
134k
   }
1255
134k
   return 0;
1256
134k
}
1257
1258
1259
0
#define median3(a, b, c)  ((a) < (b) ? ((b) < (c) ? (b) : ((a) < (c) ? (c) : (a))) : ((c) < (b) ? (b) : ((c) < (a) ? (c) : (a))))
1260
1261
#ifdef FIXED_POINT
1262
const spx_word16_t attenuation[10] = {32767, 31483, 27923, 22861, 17278, 12055, 7764, 4616, 2533, 1283};
1263
#else
1264
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};
1265
1266
#endif
1267
1268
static void nb_decode_lost(DecState *st, spx_word16_t *out, char *stack)
1269
0
{
1270
0
   int i;
1271
0
   int pitch_val;
1272
0
   spx_word16_t pitch_gain;
1273
0
   spx_word16_t fact;
1274
0
   spx_word16_t gain_med;
1275
0
   spx_word16_t innov_gain;
1276
0
   spx_word16_t noise_gain;
1277
1278
0
   st->exc = st->excBuf + 2*NB_PITCH_END + NB_SUBFRAME_SIZE + 6;
1279
1280
0
   if (st->count_lost<10)
1281
0
      fact = attenuation[st->count_lost];
1282
0
   else
1283
0
      fact = 0;
1284
1285
0
   gain_med = median3(st->pitch_gain_buf[0], st->pitch_gain_buf[1], st->pitch_gain_buf[2]);
1286
0
   if (gain_med < st->last_pitch_gain)
1287
0
      st->last_pitch_gain = gain_med;
1288
1289
#ifdef FIXED_POINT
1290
   pitch_gain = st->last_pitch_gain;
1291
   if (pitch_gain>54)
1292
      pitch_gain = 54;
1293
   pitch_gain = SHL16(pitch_gain, 9);
1294
#else
1295
0
   pitch_gain = GAIN_SCALING_1*st->last_pitch_gain;
1296
0
   if (pitch_gain>.85)
1297
0
      pitch_gain=.85;
1298
0
#endif
1299
0
   pitch_gain = MULT16_16_Q15(fact,pitch_gain) + VERY_SMALL;
1300
   /* FIXME: This was rms of innovation (not exc) */
1301
0
   innov_gain = compute_rms16(st->exc, NB_FRAME_SIZE);
1302
0
   noise_gain = MULT16_16_Q15(innov_gain, MULT16_16_Q15(fact, SUB16(Q15ONE,MULT16_16_Q15(pitch_gain,pitch_gain))));
1303
   /* Shift all buffers by one frame */
1304
0
   SPEEX_MOVE(st->excBuf, st->excBuf+NB_FRAME_SIZE, 2*NB_PITCH_END + NB_SUBFRAME_SIZE + 12);
1305
1306
1307
0
   pitch_val = st->last_pitch + SHR32((spx_int32_t)speex_rand(1+st->count_lost, &st->seed),SIG_SHIFT);
1308
0
   if (pitch_val > NB_PITCH_END)
1309
0
      pitch_val = NB_PITCH_END;
1310
0
   if (pitch_val < NB_PITCH_START)
1311
0
      pitch_val = NB_PITCH_START;
1312
0
   for (i=0;i<NB_FRAME_SIZE;i++)
1313
0
   {
1314
0
      st->exc[i]= MULT16_16_Q15(pitch_gain, (st->exc[i-pitch_val]+VERY_SMALL)) +
1315
0
            speex_rand(noise_gain, &st->seed);
1316
0
   }
1317
1318
0
   bw_lpc(QCONST16(.98,15), st->interp_qlpc, st->interp_qlpc, NB_ORDER);
1319
0
   iir_mem16(&st->exc[-NB_SUBFRAME_SIZE], st->interp_qlpc, out, NB_FRAME_SIZE,
1320
0
             NB_ORDER, st->mem_sp, stack);
1321
0
   highpass(out, out, NB_FRAME_SIZE, HIGHPASS_NARROWBAND|HIGHPASS_OUTPUT, st->mem_hp);
1322
1323
0
   st->first = 0;
1324
0
   st->count_lost++;
1325
0
   st->pitch_gain_buf[st->pitch_gain_buf_idx++] = PSHR16(pitch_gain,9);
1326
0
   if (st->pitch_gain_buf_idx > 2) /* rollover */
1327
0
      st->pitch_gain_buf_idx = 0;
1328
0
}
1329
1330
/* Just so we don't need to carry the complete wideband mode information */
1331
static const int wb_skip_table[8] = {0, 36, 112, 192, 352, 0, 0, 0};
1332
1333
int nb_decode(void *state, SpeexBits *bits, void *vout)
1334
58.2k
{
1335
58.2k
   DecState *st;
1336
58.2k
   int i, sub;
1337
58.2k
   int pitch;
1338
58.2k
   spx_word16_t pitch_gain[3];
1339
58.2k
   spx_word32_t ol_gain=0;
1340
58.2k
   int ol_pitch=0;
1341
58.2k
   spx_word16_t ol_pitch_coef=0;
1342
58.2k
   int best_pitch=40;
1343
58.2k
   spx_word16_t best_pitch_gain=0;
1344
58.2k
   int wideband;
1345
58.2k
   int m;
1346
58.2k
   char *stack;
1347
58.2k
   VARDECL(spx_sig_t *innov);
1348
58.2k
   VARDECL(spx_word32_t *exc32);
1349
58.2k
   VARDECL(spx_coef_t *ak);
1350
58.2k
   VARDECL(spx_lsp_t *qlsp);
1351
58.2k
   spx_word16_t pitch_average=0;
1352
1353
58.2k
   spx_word16_t *out = (spx_word16_t*)vout;
1354
58.2k
   VARDECL(spx_lsp_t *interp_qlsp);
1355
1356
58.2k
   st=(DecState*)state;
1357
58.2k
   stack=st->stack;
1358
1359
58.2k
   st->exc = st->excBuf + 2*NB_PITCH_END + NB_SUBFRAME_SIZE + 6;
1360
1361
   /* Check if we're in DTX mode*/
1362
58.2k
   if (!bits && st->dtx_enabled)
1363
0
   {
1364
0
      st->submodeID=0;
1365
0
   } else
1366
58.2k
   {
1367
      /* If bits is NULL, consider the packet to be lost (what could we do anyway) */
1368
58.2k
      if (!bits)
1369
0
      {
1370
0
         nb_decode_lost(st, out, stack);
1371
0
         return 0;
1372
0
      }
1373
1374
58.2k
      if (st->encode_submode)
1375
58.2k
      {
1376
1377
      /* Search for next narrowband block (handle requests, skip wideband blocks) */
1378
63.1k
      do {
1379
63.1k
         if (speex_bits_remaining(bits)<5)
1380
11.3k
            return -1;
1381
51.8k
         wideband = speex_bits_unpack_unsigned(bits, 1);
1382
51.8k
         if (wideband) /* Skip wideband block (for compatibility) */
1383
3.53k
         {
1384
3.53k
            int submode;
1385
3.53k
            int advance;
1386
3.53k
            advance = submode = speex_bits_unpack_unsigned(bits, SB_SUBMODE_BITS);
1387
            /*speex_mode_query(&speex_wb_mode, SPEEX_SUBMODE_BITS_PER_FRAME, &advance);*/
1388
3.53k
            advance = wb_skip_table[submode];
1389
3.53k
            if (advance < 0)
1390
0
            {
1391
0
               speex_notify("Invalid mode encountered. The stream is corrupted.");
1392
0
               return -2;
1393
0
            }
1394
3.53k
            advance -= (SB_SUBMODE_BITS+1);
1395
3.53k
            speex_bits_advance(bits, advance);
1396
1397
3.53k
            if (speex_bits_remaining(bits)<5)
1398
831
               return -1;
1399
2.70k
            wideband = speex_bits_unpack_unsigned(bits, 1);
1400
2.70k
            if (wideband)
1401
2.02k
            {
1402
2.02k
               advance = submode = speex_bits_unpack_unsigned(bits, SB_SUBMODE_BITS);
1403
               /*speex_mode_query(&speex_wb_mode, SPEEX_SUBMODE_BITS_PER_FRAME, &advance);*/
1404
2.02k
               advance = wb_skip_table[submode];
1405
2.02k
               if (advance < 0)
1406
0
               {
1407
0
                  speex_notify("Invalid mode encountered. The stream is corrupted.");
1408
0
                  return -2;
1409
0
               }
1410
2.02k
               advance -= (SB_SUBMODE_BITS+1);
1411
2.02k
               speex_bits_advance(bits, advance);
1412
2.02k
               wideband = speex_bits_unpack_unsigned(bits, 1);
1413
2.02k
               if (wideband)
1414
1.53k
               {
1415
1.53k
                  speex_notify("More than two wideband layers found. The stream is corrupted.");
1416
1.53k
                  return -2;
1417
1.53k
               }
1418
1419
2.02k
            }
1420
2.70k
         }
1421
49.4k
         if (speex_bits_remaining(bits)<4)
1422
463
            return -1;
1423
         /* FIXME: Check for overflow */
1424
48.9k
         m = speex_bits_unpack_unsigned(bits, 4);
1425
48.9k
         if (m==15) /* We found a terminator */
1426
598
         {
1427
598
            return -1;
1428
48.3k
         } else if (m==14) /* Speex in-band request */
1429
4.08k
         {
1430
4.08k
            int ret = speex_inband_handler(bits, st->speex_callbacks, state);
1431
4.08k
            if (ret)
1432
0
               return ret;
1433
44.2k
         } else if (m==13) /* User in-band request */
1434
819
         {
1435
819
            int ret = st->user_callback.func(bits, state, st->user_callback.data);
1436
819
            if (ret)
1437
0
               return ret;
1438
43.4k
         } else if (m>8) /* Invalid mode */
1439
3.29k
         {
1440
3.29k
            speex_notify("Invalid mode encountered. The stream is corrupted.");
1441
3.29k
            return -2;
1442
3.29k
         }
1443
1444
48.9k
      } while (m>8);
1445
1446
      /* Get the sub-mode that was used */
1447
40.1k
      st->submodeID = m;
1448
40.1k
      }
1449
1450
58.2k
   }
1451
1452
   /* Shift all buffers by one frame */
1453
40.1k
   SPEEX_MOVE(st->excBuf, st->excBuf+NB_FRAME_SIZE, 2*NB_PITCH_END + NB_SUBFRAME_SIZE + 12);
1454
1455
   /* If null mode (no transmission), just set a couple things to zero*/
1456
40.1k
   if (st->submodes[st->submodeID] == NULL)
1457
19.0k
   {
1458
19.0k
      VARDECL(spx_coef_t *lpc);
1459
19.0k
      ALLOC(lpc, NB_ORDER, spx_coef_t);
1460
19.0k
      bw_lpc(QCONST16(0.93f,15), st->interp_qlpc, lpc, NB_ORDER);
1461
19.0k
      {
1462
19.0k
         spx_word16_t innov_gain=0;
1463
         /* FIXME: This was innov, not exc */
1464
19.0k
         innov_gain = compute_rms16(st->exc, NB_FRAME_SIZE);
1465
3.06M
         for (i=0;i<NB_FRAME_SIZE;i++)
1466
3.04M
            st->exc[i]=speex_rand(innov_gain, &st->seed);
1467
19.0k
      }
1468
1469
1470
19.0k
      st->first=1;
1471
1472
      /* Final signal synthesis from excitation */
1473
19.0k
      iir_mem16(st->exc, lpc, out, NB_FRAME_SIZE, NB_ORDER, st->mem_sp, stack);
1474
1475
19.0k
      st->count_lost=0;
1476
19.0k
      return 0;
1477
19.0k
   }
1478
1479
21.1k
   ALLOC(qlsp, NB_ORDER, spx_lsp_t);
1480
1481
   /* Unquantize LSPs */
1482
21.1k
   SUBMODE(lsp_unquant)(qlsp, NB_ORDER, bits);
1483
1484
   /*Damp memory if a frame was lost and the LSP changed too much*/
1485
21.1k
   if (st->count_lost)
1486
0
   {
1487
0
      spx_word16_t fact;
1488
0
      spx_word32_t lsp_dist=0;
1489
0
      for (i=0;i<NB_ORDER;i++)
1490
0
         lsp_dist = ADD32(lsp_dist, EXTEND32(ABS(st->old_qlsp[i] - qlsp[i])));
1491
#ifdef FIXED_POINT
1492
      fact = SHR16(19661,SHR32(lsp_dist,LSP_SHIFT+2));
1493
#else
1494
0
      fact = .6*exp(-.2*lsp_dist);
1495
0
#endif
1496
0
      for (i=0;i<NB_ORDER;i++)
1497
0
         st->mem_sp[i] = MULT16_32_Q15(fact,st->mem_sp[i]);
1498
0
   }
1499
1500
1501
   /* Handle first frame and lost-packet case */
1502
21.1k
   if (st->first || st->count_lost)
1503
3.85k
   {
1504
42.4k
      for (i=0;i<NB_ORDER;i++)
1505
38.5k
         st->old_qlsp[i] = qlsp[i];
1506
3.85k
   }
1507
1508
   /* Get open-loop pitch estimation for low bit-rate pitch coding */
1509
21.1k
   if (SUBMODE(lbr_pitch)!=-1)
1510
8.15k
   {
1511
8.15k
      ol_pitch = NB_PITCH_START+speex_bits_unpack_unsigned(bits, 7);
1512
8.15k
   }
1513
1514
21.1k
   if (SUBMODE(forced_pitch_gain))
1515
6.31k
   {
1516
6.31k
      int quant;
1517
6.31k
      quant = speex_bits_unpack_unsigned(bits, 4);
1518
6.31k
      ol_pitch_coef=MULT16_16_P15(QCONST16(0.066667,15),SHL16(quant,GAIN_SHIFT));
1519
6.31k
   }
1520
1521
   /* Get global excitation gain */
1522
21.1k
   {
1523
21.1k
      int qe;
1524
21.1k
      qe = speex_bits_unpack_unsigned(bits, 5);
1525
#ifdef FIXED_POINT
1526
      /* FIXME: Perhaps we could slightly lower the gain here when the output is going to saturate? */
1527
      ol_gain = MULT16_32_Q15(28406,ol_gain_table[qe]);
1528
#else
1529
21.1k
      ol_gain = SIG_SCALING*exp(qe/3.5);
1530
21.1k
#endif
1531
21.1k
   }
1532
1533
21.1k
   ALLOC(ak, NB_ORDER, spx_coef_t);
1534
21.1k
   ALLOC(innov, NB_SUBFRAME_SIZE, spx_sig_t);
1535
21.1k
   ALLOC(exc32, NB_SUBFRAME_SIZE, spx_word32_t);
1536
1537
21.1k
   if (st->submodeID==1)
1538
3.71k
   {
1539
3.71k
      int extra;
1540
3.71k
      extra = speex_bits_unpack_unsigned(bits, 4);
1541
1542
3.71k
      if (extra==15)
1543
650
         st->dtx_enabled=1;
1544
3.06k
      else
1545
3.06k
         st->dtx_enabled=0;
1546
3.71k
   }
1547
21.1k
   if (st->submodeID>1)
1548
17.4k
      st->dtx_enabled=0;
1549
1550
   /*Loop on subframes */
1551
105k
   for (sub=0;sub<NB_NB_SUBFRAMES;sub++)
1552
84.6k
   {
1553
84.6k
      int offset;
1554
84.6k
      spx_word16_t *exc;
1555
84.6k
      spx_word16_t *innov_save = NULL;
1556
84.6k
      spx_word16_t tmp;
1557
1558
      /* Offset relative to start of frame */
1559
84.6k
      offset = NB_SUBFRAME_SIZE*sub;
1560
      /* Excitation */
1561
84.6k
      exc=st->exc+offset;
1562
      /* Original signal */
1563
84.6k
      if (st->innov_save)
1564
50.9k
         innov_save = st->innov_save+offset;
1565
1566
1567
      /* Reset excitation */
1568
84.6k
      SPEEX_MEMSET(exc, 0, NB_SUBFRAME_SIZE);
1569
1570
      /*Adaptive codebook contribution*/
1571
84.6k
      speex_assert (SUBMODE(ltp_unquant));
1572
84.6k
      {
1573
84.6k
         int pit_min, pit_max;
1574
         /* Handle pitch constraints if any */
1575
84.6k
         if (SUBMODE(lbr_pitch) != -1)
1576
32.6k
         {
1577
32.6k
            int margin;
1578
32.6k
            margin = SUBMODE(lbr_pitch);
1579
32.6k
            if (margin)
1580
0
            {
1581
/* GT - need optimization?
1582
               if (ol_pitch < NB_PITCH_START+margin-1)
1583
                  ol_pitch=NB_PITCH_START+margin-1;
1584
               if (ol_pitch > NB_PITCH_END-margin)
1585
                  ol_pitch=NB_PITCH_END-margin;
1586
               pit_min = ol_pitch-margin+1;
1587
               pit_max = ol_pitch+margin;
1588
*/
1589
0
               pit_min = ol_pitch-margin+1;
1590
0
               if (pit_min < NB_PITCH_START)
1591
0
      pit_min = NB_PITCH_START;
1592
0
               pit_max = ol_pitch+margin;
1593
0
               if (pit_max > NB_PITCH_END)
1594
0
      pit_max = NB_PITCH_END;
1595
32.6k
            } else {
1596
32.6k
               pit_min = pit_max = ol_pitch;
1597
32.6k
            }
1598
52.0k
         } else {
1599
52.0k
            pit_min = NB_PITCH_START;
1600
52.0k
            pit_max = NB_PITCH_END;
1601
52.0k
         }
1602
1603
1604
1605
84.6k
         SUBMODE(ltp_unquant)(exc, exc32, pit_min, pit_max, ol_pitch_coef, SUBMODE(ltp_params),
1606
84.6k
                 NB_SUBFRAME_SIZE, &pitch, &pitch_gain[0], bits, stack,
1607
84.6k
                 st->count_lost, offset, st->last_pitch_gain, 0);
1608
1609
         /* Ensuring that things aren't blowing up as would happen if e.g. an encoder is
1610
         crafting packets to make us produce NaNs and slow down the decoder (vague DoS threat).
1611
         We can probably be even more aggressive and limit to 15000 or so. */
1612
84.6k
         sanitize_values32(exc32, NEG32(QCONST32(32000,SIG_SHIFT-1)), QCONST32(32000,SIG_SHIFT-1), NB_SUBFRAME_SIZE);
1613
1614
84.6k
         tmp = gain_3tap_to_1tap(pitch_gain);
1615
1616
84.6k
         pitch_average += tmp;
1617
84.6k
         if ((tmp>best_pitch_gain&&ABS(2*best_pitch-pitch)>=3&&ABS(3*best_pitch-pitch)>=4&&ABS(4*best_pitch-pitch)>=5)
1618
84.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))
1619
84.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)) )
1620
12.0k
         {
1621
12.0k
            best_pitch = pitch;
1622
12.0k
            if (tmp > best_pitch_gain)
1623
10.9k
               best_pitch_gain = tmp;
1624
12.0k
         }
1625
84.6k
      }
1626
1627
      /* Unquantize the innovation */
1628
84.6k
      {
1629
84.6k
         int q_energy;
1630
84.6k
         spx_word32_t ener;
1631
1632
84.6k
         SPEEX_MEMSET(innov, 0, NB_SUBFRAME_SIZE);
1633
1634
         /* Decode sub-frame gain correction */
1635
84.6k
         if (SUBMODE(have_subframe_gain)==3)
1636
23.8k
         {
1637
23.8k
            q_energy = speex_bits_unpack_unsigned(bits, 3);
1638
23.8k
            ener = MULT16_32_Q14(exc_gain_quant_scal3[q_energy],ol_gain);
1639
60.8k
         } else if (SUBMODE(have_subframe_gain)==1)
1640
28.2k
         {
1641
28.2k
            q_energy = speex_bits_unpack_unsigned(bits, 1);
1642
28.2k
            ener = MULT16_32_Q14(exc_gain_quant_scal1[q_energy],ol_gain);
1643
32.6k
         } else {
1644
32.6k
            ener = ol_gain;
1645
32.6k
         }
1646
1647
84.6k
         speex_assert (SUBMODE(innovation_unquant));
1648
84.6k
         {
1649
            /*Fixed codebook contribution*/
1650
84.6k
            SUBMODE(innovation_unquant)(innov, SUBMODE(innovation_params), NB_SUBFRAME_SIZE, bits, stack, &st->seed);
1651
            /* De-normalize innovation and update excitation */
1652
1653
84.6k
            signal_mul(innov, innov, ener, NB_SUBFRAME_SIZE);
1654
1655
            /* Decode second codebook (only for some modes) */
1656
84.6k
            if (SUBMODE(double_codebook))
1657
13.9k
            {
1658
13.9k
               char *tmp_stack=stack;
1659
13.9k
               VARDECL(spx_sig_t *innov2);
1660
13.9k
               ALLOC(innov2, NB_SUBFRAME_SIZE, spx_sig_t);
1661
13.9k
               SPEEX_MEMSET(innov2, 0, NB_SUBFRAME_SIZE);
1662
13.9k
               SUBMODE(innovation_unquant)(innov2, SUBMODE(innovation_params), NB_SUBFRAME_SIZE, bits, stack, &st->seed);
1663
13.9k
               signal_mul(innov2, innov2, MULT16_32_Q15(QCONST16(0.454545f,15),ener), NB_SUBFRAME_SIZE);
1664
570k
               for (i=0;i<NB_SUBFRAME_SIZE;i++)
1665
556k
                  innov[i] = ADD32(innov[i], innov2[i]);
1666
13.9k
               stack = tmp_stack;
1667
13.9k
            }
1668
3.47M
            for (i=0;i<NB_SUBFRAME_SIZE;i++)
1669
3.38M
               exc[i]=EXTRACT16(SATURATE32(PSHR32(ADD32(SHL32(exc32[i],1),innov[i]),SIG_SHIFT),32767));
1670
            /*print_vec(exc, 40, "innov");*/
1671
84.6k
            if (innov_save)
1672
50.9k
            {
1673
2.08M
               for (i=0;i<NB_SUBFRAME_SIZE;i++)
1674
2.03M
                  innov_save[i] = EXTRACT16(PSHR32(innov[i], SIG_SHIFT));
1675
50.9k
            }
1676
84.6k
         }
1677
1678
         /*Vocoder mode*/
1679
84.6k
         if (st->submodeID==1)
1680
14.8k
         {
1681
14.8k
            spx_word16_t g=ol_pitch_coef;
1682
14.8k
            g=MULT16_16_P14(QCONST16(1.5f,14),(g-QCONST16(.2f,6)));
1683
14.8k
            if (g<0)
1684
9.64k
               g=0;
1685
14.8k
            if (g>GAIN_SCALING)
1686
2.37k
               g=GAIN_SCALING;
1687
1688
14.8k
            SPEEX_MEMSET(exc, 0, NB_SUBFRAME_SIZE);
1689
37.9k
            while (st->voc_offset<NB_SUBFRAME_SIZE)
1690
23.0k
            {
1691
               /* exc[st->voc_offset]= g*sqrt(2*ol_pitch)*ol_gain;
1692
                  Not quite sure why we need the factor of two in the sqrt */
1693
23.0k
               if (st->voc_offset>=0)
1694
23.0k
                  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)));
1695
23.0k
               st->voc_offset+=ol_pitch;
1696
23.0k
            }
1697
14.8k
            st->voc_offset -= NB_SUBFRAME_SIZE;
1698
1699
609k
            for (i=0;i<NB_SUBFRAME_SIZE;i++)
1700
594k
            {
1701
594k
               spx_word16_t exci=exc[i];
1702
594k
               exc[i]= ADD16(ADD16(MULT16_16_Q15(QCONST16(.7f,15),exc[i]) , MULT16_16_Q15(QCONST16(.3f,15),st->voc_m1)),
1703
594k
                             SUB16(MULT16_16_Q15(Q15_ONE-MULT16_16_16(QCONST16(.85f,9),g),EXTRACT16(PSHR32(innov[i],SIG_SHIFT))),
1704
594k
                                   MULT16_16_Q15(MULT16_16_16(QCONST16(.15f,9),g),EXTRACT16(PSHR32(st->voc_m2,SIG_SHIFT)))
1705
594k
                                  ));
1706
594k
               st->voc_m1 = exci;
1707
594k
               st->voc_m2=innov[i];
1708
594k
               st->voc_mean = EXTRACT16(PSHR32(ADD32(MULT16_16(QCONST16(.8f,15),st->voc_mean), MULT16_16(QCONST16(.2f,15),exc[i])), 15));
1709
594k
               exc[i]-=st->voc_mean;
1710
594k
            }
1711
14.8k
         }
1712
1713
84.6k
      }
1714
84.6k
   }
1715
1716
21.1k
   ALLOC(interp_qlsp, NB_ORDER, spx_lsp_t);
1717
1718
21.1k
   if (st->lpc_enh_enabled && SUBMODE(comb_gain)>0 && !st->count_lost)
1719
17.4k
   {
1720
17.4k
      multicomb(st->exc-NB_SUBFRAME_SIZE, out, st->interp_qlpc, NB_ORDER, 2*NB_SUBFRAME_SIZE, best_pitch, 40, SUBMODE(comb_gain), stack);
1721
17.4k
      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);
1722
17.4k
   } else {
1723
3.71k
      SPEEX_COPY(out, &st->exc[-NB_SUBFRAME_SIZE], NB_FRAME_SIZE);
1724
3.71k
   }
1725
1726
   /* If the last packet was lost, re-scale the excitation to obtain the same energy as encoded in ol_gain */
1727
21.1k
   if (st->count_lost)
1728
0
   {
1729
0
      spx_word16_t exc_ener;
1730
0
      spx_word32_t gain32;
1731
0
      spx_word16_t gain;
1732
0
      exc_ener = compute_rms16 (st->exc, NB_FRAME_SIZE);
1733
0
      gain32 = PDIV32(ol_gain, ADD16(exc_ener,1));
1734
#ifdef FIXED_POINT
1735
      if (gain32 > 32767)
1736
         gain32 = 32767;
1737
      gain = EXTRACT16(gain32);
1738
#else
1739
0
      if (gain32 > 2)
1740
0
         gain32=2;
1741
0
      gain = gain32;
1742
0
#endif
1743
0
      for (i=0;i<NB_FRAME_SIZE;i++)
1744
0
      {
1745
0
         st->exc[i] = MULT16_16_Q14(gain, st->exc[i]);
1746
0
         out[i]=st->exc[i-NB_SUBFRAME_SIZE];
1747
0
      }
1748
0
   }
1749
1750
   /*Loop on subframes */
1751
105k
   for (sub=0;sub<NB_NB_SUBFRAMES;sub++)
1752
84.6k
   {
1753
84.6k
      int offset;
1754
84.6k
      spx_word16_t *sp;
1755
1756
      /* Offset relative to start of frame */
1757
84.6k
      offset = NB_SUBFRAME_SIZE*sub;
1758
      /* Original signal */
1759
84.6k
      sp=out+offset;
1760
1761
      /* LSP interpolation (quantized and unquantized) */
1762
84.6k
      lsp_interpolate(st->old_qlsp, qlsp, interp_qlsp, NB_ORDER, sub, NB_NB_SUBFRAMES, LSP_MARGIN);
1763
1764
      /* Compute interpolated LPCs (unquantized) */
1765
84.6k
      lsp_to_lpc(interp_qlsp, ak, NB_ORDER, stack);
1766
1767
      /* Compute analysis filter at w=pi */
1768
84.6k
      {
1769
84.6k
         spx_word32_t pi_g=LPC_SCALING;
1770
508k
         for (i=0;i<NB_ORDER;i+=2)
1771
423k
         {
1772
            /*pi_g += -st->interp_qlpc[i] +  st->interp_qlpc[i+1];*/
1773
423k
            pi_g = ADD32(pi_g, SUB32(EXTEND32(ak[i+1]),EXTEND32(ak[i])));
1774
423k
         }
1775
84.6k
         st->pi_gain[sub] = pi_g;
1776
84.6k
      }
1777
1778
84.6k
      iir_mem16(sp, st->interp_qlpc, sp, NB_SUBFRAME_SIZE, NB_ORDER,
1779
84.6k
                st->mem_sp, stack);
1780
1781
931k
      for (i=0;i<NB_ORDER;i++)
1782
846k
         st->interp_qlpc[i] = ak[i];
1783
1784
84.6k
   }
1785
1786
21.1k
   if (st->highpass_enabled)
1787
21.1k
      highpass(out, out, NB_FRAME_SIZE, (st->isWideband?HIGHPASS_WIDEBAND:HIGHPASS_NARROWBAND)|HIGHPASS_OUTPUT, st->mem_hp);
1788
   /*for (i=0;i<NB_FRAME_SIZE;i++)
1789
     printf ("%d\n", (int)st->frame[i]);*/
1790
1791
   /* Tracking output level */
1792
21.1k
   st->level = 1+PSHR32(ol_gain,SIG_SHIFT);
1793
21.1k
   st->max_level = MAX16(MULT16_16_Q15(QCONST16(.99f,15), st->max_level), st->level);
1794
21.1k
   st->min_level = MIN16(ADD16(1,MULT16_16_Q14(QCONST16(1.01f,14), st->min_level)), st->level);
1795
21.1k
   if (st->max_level < st->min_level+1)
1796
7.36k
      st->max_level = st->min_level+1;
1797
   /*printf ("%f %f %f %d\n", og, st->min_level, st->max_level, update);*/
1798
1799
   /* Store the LSPs for interpolation in the next frame */
1800
232k
   for (i=0;i<NB_ORDER;i++)
1801
211k
      st->old_qlsp[i] = qlsp[i];
1802
1803
   /* The next frame will not be the first (Duh!) */
1804
21.1k
   st->first = 0;
1805
21.1k
   st->count_lost=0;
1806
21.1k
   st->last_pitch = best_pitch;
1807
#ifdef FIXED_POINT
1808
   st->last_pitch_gain = PSHR16(pitch_average,2);
1809
#else
1810
21.1k
   st->last_pitch_gain = .25*pitch_average;
1811
21.1k
#endif
1812
21.1k
   st->pitch_gain_buf[st->pitch_gain_buf_idx++] = st->last_pitch_gain;
1813
21.1k
   if (st->pitch_gain_buf_idx > 2) /* rollover */
1814
6.56k
      st->pitch_gain_buf_idx = 0;
1815
1816
21.1k
   st->last_ol_gain = ol_gain;
1817
1818
21.1k
   return 0;
1819
40.1k
}
1820
#endif /* DISABLE_DECODER */
1821