Coverage Report

Created: 2026-10-28 06:39

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/pjsip/third_party/speex/libspeex/sb_celp.c
Line
Count
Source
1
/* Copyright (C) 2002-2006 Jean-Marc Valin
2
   File: sb_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 "sb_celp.h"
38
#include "filters.h"
39
#include "lpc.h"
40
#include "lsp.h"
41
#include "stack_alloc.h"
42
#include "cb_search.h"
43
#include "quant_lsp.h"
44
#include "vq.h"
45
#include "ltp.h"
46
#include "arch.h"
47
#include "math_approx.h"
48
#include "os_support.h"
49
50
#ifndef NULL
51
#define NULL 0
52
#endif
53
54
/* Default size for the encoder and decoder stack (can be changed at compile time).
55
   This does not apply when using variable-size arrays or alloca. */
56
#ifndef SB_ENC_STACK
57
#define SB_ENC_STACK (10000*sizeof(spx_sig_t))
58
#endif
59
60
#ifndef SB_DEC_STACK
61
#define SB_DEC_STACK (6000*sizeof(spx_sig_t))
62
#endif
63
64
65
#ifndef DISABLE_WIDEBAND
66
67
6
#define SUBMODE(x) st->submodes[st->submodeID]->x
68
69
#ifdef FIXED_POINT
70
static const spx_word16_t gc_quant_bound[16] = {125, 164, 215, 282, 370, 484, 635, 832, 1090, 1428, 1871, 2452, 3213, 4210, 5516, 7228};
71
static const spx_word16_t fold_quant_bound[32] = {
72
   39, 44, 50, 57, 64, 73, 83, 94,
73
   106, 120, 136, 154, 175, 198, 225, 255,
74
   288, 327, 370, 420, 476, 539, 611, 692,
75
   784, 889, 1007, 1141, 1293, 1465, 1660, 1881};
76
#define LSP_MARGIN 410
77
#define LSP_DELTA1 6553
78
#define LSP_DELTA2 1638
79
80
#else
81
82
static const spx_word16_t gc_quant_bound[16] = {
83
      0.97979, 1.28384, 1.68223, 2.20426, 2.88829, 3.78458, 4.95900, 6.49787,
84
      8.51428, 11.15642, 14.61846, 19.15484, 25.09895, 32.88761, 43.09325, 56.46588};
85
static const spx_word16_t fold_quant_bound[32] = {
86
   0.30498, 0.34559, 0.39161, 0.44375, 0.50283, 0.56979, 0.64565, 0.73162,
87
   0.82903, 0.93942, 1.06450, 1.20624, 1.36685, 1.54884, 1.75506, 1.98875,
88
   2.25355, 2.55360, 2.89361, 3.27889, 3.71547, 4.21018, 4.77076, 5.40598,
89
   6.12577, 6.94141, 7.86565, 8.91295, 10.09969, 11.44445, 12.96826, 14.69497};
90
91
0
#define LSP_MARGIN .05
92
0
#define LSP_DELTA1 .2
93
0
#define LSP_DELTA2 .05
94
95
#endif
96
97
3
#define QMF_ORDER 64
98
99
#ifdef FIXED_POINT
100
static const spx_word16_t h0[64] = {2, -7, -7, 18, 15, -39, -25, 75, 35, -130, -41, 212, 38, -327, -17, 483, -32, -689, 124, 956, -283, -1307, 543, 1780, -973, -2467, 1733, 3633, -3339, -6409, 9059, 30153, 30153, 9059, -6409, -3339, 3633, 1733, -2467, -973, 1780, 543, -1307, -283, 956, 124, -689, -32, 483, -17, -327, 38, 212, -41, -130, 35, 75, -25, -39, 15, 18, -7, -7, 2};
101
102
#else
103
static const float h0[64] = {
104
   3.596189e-05f, -0.0001123515f,
105
   -0.0001104587f, 0.0002790277f,
106
   0.0002298438f, -0.0005953563f,
107
   -0.0003823631f, 0.00113826f,
108
   0.0005308539f, -0.001986177f,
109
   -0.0006243724f, 0.003235877f,
110
   0.0005743159f, -0.004989147f,
111
   -0.0002584767f, 0.007367171f,
112
   -0.0004857935f, -0.01050689f,
113
   0.001894714f, 0.01459396f,
114
   -0.004313674f, -0.01994365f,
115
   0.00828756f, 0.02716055f,
116
   -0.01485397f, -0.03764973f,
117
   0.026447f, 0.05543245f,
118
   -0.05095487f, -0.09779096f,
119
   0.1382363f, 0.4600981f,
120
   0.4600981f, 0.1382363f,
121
   -0.09779096f, -0.05095487f,
122
   0.05543245f, 0.026447f,
123
   -0.03764973f, -0.01485397f,
124
   0.02716055f, 0.00828756f,
125
   -0.01994365f, -0.004313674f,
126
   0.01459396f, 0.001894714f,
127
   -0.01050689f, -0.0004857935f,
128
   0.007367171f, -0.0002584767f,
129
   -0.004989147f, 0.0005743159f,
130
   0.003235877f, -0.0006243724f,
131
   -0.001986177f, 0.0005308539f,
132
   0.00113826f, -0.0003823631f,
133
   -0.0005953563f, 0.0002298438f,
134
   0.0002790277f, -0.0001104587f,
135
   -0.0001123515f, 3.596189e-05f
136
};
137
138
#endif
139
140
extern const spx_word16_t lag_window[];
141
extern const spx_word16_t lpc_window[];
142
143
#ifndef DISABLE_ENCODER
144
void *sb_encoder_init(const SpeexMode *m)
145
3
{
146
3
   int i;
147
3
   spx_int32_t tmp;
148
3
   SBEncState *st;
149
3
   const SpeexSBMode *mode;
150
151
3
   st = (SBEncState*)speex_alloc(sizeof(SBEncState));
152
3
   if (!st)
153
0
      return NULL;
154
3
   st->mode = m;
155
3
   mode = (const SpeexSBMode*)m->mode;
156
157
158
3
   st->st_low = speex_encoder_init(mode->nb_mode);
159
#if defined(VAR_ARRAYS) || defined (USE_ALLOCA)
160
   st->stack = NULL;
161
#else
162
   /*st->stack = (char*)speex_alloc_scratch(SB_ENC_STACK);*/
163
3
   speex_encoder_ctl(st->st_low, SPEEX_GET_STACK, &st->stack);
164
3
#endif
165
166
3
   st->full_frame_size = 2*mode->frameSize;
167
3
   st->frame_size = mode->frameSize;
168
3
   st->subframeSize = mode->subframeSize;
169
3
   st->nbSubframes = mode->frameSize/mode->subframeSize;
170
3
   st->windowSize = st->frame_size+st->subframeSize;
171
3
   st->lpcSize=mode->lpcSize;
172
173
3
   st->encode_submode = 1;
174
3
   st->submodes=mode->submodes;
175
3
   st->submodeSelect = st->submodeID=mode->defaultSubmode;
176
177
3
   tmp=9;
178
3
   speex_encoder_ctl(st->st_low, SPEEX_SET_QUALITY, &tmp);
179
3
   tmp=1;
180
3
   speex_encoder_ctl(st->st_low, SPEEX_SET_WIDEBAND, &tmp);
181
182
3
   st->lpc_floor = mode->lpc_floor;
183
3
   st->gamma1=mode->gamma1;
184
3
   st->gamma2=mode->gamma2;
185
3
   st->first=1;
186
187
3
   st->high=(spx_word16_t*)speex_alloc((st->windowSize-st->frame_size)*sizeof(spx_word16_t));
188
189
3
   st->h0_mem=(spx_word16_t*)speex_alloc((QMF_ORDER)*sizeof(spx_word16_t));
190
191
3
   st->window= lpc_window;
192
193
3
   st->lagWindow = lag_window;
194
195
3
   st->old_lsp = (spx_lsp_t*)speex_alloc(st->lpcSize*sizeof(spx_lsp_t));
196
3
   st->old_qlsp = (spx_lsp_t*)speex_alloc(st->lpcSize*sizeof(spx_lsp_t));
197
3
   st->interp_qlpc = (spx_coef_t*)speex_alloc(st->lpcSize*sizeof(spx_coef_t));
198
3
   st->pi_gain = (spx_word32_t*)speex_alloc((st->nbSubframes)*sizeof(spx_word32_t));
199
3
   st->exc_rms = (spx_word16_t*)speex_alloc((st->nbSubframes)*sizeof(spx_word16_t));
200
3
   st->innov_rms_save = NULL;
201
202
3
   st->mem_sp = (spx_mem_t*)speex_alloc((st->lpcSize)*sizeof(spx_mem_t));
203
3
   st->mem_sp2 = (spx_mem_t*)speex_alloc((st->lpcSize)*sizeof(spx_mem_t));
204
3
   st->mem_sw = (spx_mem_t*)speex_alloc((st->lpcSize)*sizeof(spx_mem_t));
205
206
27
   for (i=0;i<st->lpcSize;i++)
207
24
      st->old_lsp[i]= DIV32(MULT16_16(QCONST16(3.1415927f, LSP_SHIFT), i+1), st->lpcSize+1);
208
209
3
#ifndef DISABLE_VBR
210
3
   st->vbr_quality = 8;
211
3
   st->vbr_enabled = 0;
212
3
   st->vbr_max = 0;
213
3
   st->vbr_max_high = 20000;  /* We just need a big value here */
214
3
   st->vad_enabled = 0;
215
3
   st->abr_enabled = 0;
216
3
   st->relative_quality=0;
217
3
#endif /* #ifndef DISABLE_VBR */
218
219
3
   st->complexity=2;
220
3
   speex_encoder_ctl(st->st_low, SPEEX_GET_SAMPLING_RATE, &st->sampling_rate);
221
3
   st->sampling_rate*=2;
222
#ifdef ENABLE_VALGRIND
223
   VALGRIND_MAKE_MEM_DEFINED(st, (st->stack-(char*)st));
224
#endif
225
3
   return st;
226
3
}
227
228
void sb_encoder_destroy(void *state)
229
3
{
230
3
   SBEncState *st=(SBEncState*)state;
231
232
3
   speex_encoder_destroy(st->st_low);
233
3
#if !(defined(VAR_ARRAYS) || defined (USE_ALLOCA))
234
   /*speex_free_scratch(st->stack);*/
235
3
#endif
236
237
3
   speex_free(st->high);
238
239
3
   speex_free(st->h0_mem);
240
241
3
   speex_free(st->old_lsp);
242
3
   speex_free(st->old_qlsp);
243
3
   speex_free(st->interp_qlpc);
244
3
   speex_free(st->pi_gain);
245
3
   speex_free(st->exc_rms);
246
247
3
   speex_free(st->mem_sp);
248
3
   speex_free(st->mem_sp2);
249
3
   speex_free(st->mem_sw);
250
251
252
3
   speex_free(st);
253
3
}
254
255
int sb_encoder_ctl(void *state, int request, void *ptr)
256
29
{
257
29
   SBEncState *st;
258
29
   st=(SBEncState*)state;
259
29
   switch(request)
260
29
   {
261
2
   case SPEEX_GET_FRAME_SIZE:
262
2
      (*(spx_int32_t*)ptr) = st->full_frame_size;
263
2
      break;
264
0
   case SPEEX_SET_HIGH_MODE:
265
0
      st->submodeSelect = st->submodeID = (*(spx_int32_t*)ptr);
266
0
      break;
267
0
   case SPEEX_SET_LOW_MODE:
268
0
      speex_encoder_ctl(st->st_low, SPEEX_SET_LOW_MODE, ptr);
269
0
      break;
270
0
   case SPEEX_SET_DTX:
271
0
      speex_encoder_ctl(st->st_low, SPEEX_SET_DTX, ptr);
272
0
      break;
273
0
   case SPEEX_GET_DTX:
274
0
      speex_encoder_ctl(st->st_low, SPEEX_GET_DTX, ptr);
275
0
      break;
276
0
   case SPEEX_GET_LOW_MODE:
277
0
      speex_encoder_ctl(st->st_low, SPEEX_GET_LOW_MODE, ptr);
278
0
      break;
279
2
   case SPEEX_SET_MODE:
280
2
      speex_encoder_ctl(st, SPEEX_SET_QUALITY, ptr);
281
2
      break;
282
0
#ifndef DISABLE_VBR
283
0
   case SPEEX_SET_VBR:
284
0
      st->vbr_enabled = (*(spx_int32_t*)ptr);
285
0
      speex_encoder_ctl(st->st_low, SPEEX_SET_VBR, ptr);
286
0
      break;
287
0
   case SPEEX_GET_VBR:
288
0
      (*(spx_int32_t*)ptr) = st->vbr_enabled;
289
0
      break;
290
3
   case SPEEX_SET_VAD:
291
3
      st->vad_enabled = (*(spx_int32_t*)ptr);
292
3
      speex_encoder_ctl(st->st_low, SPEEX_SET_VAD, ptr);
293
3
      break;
294
0
   case SPEEX_GET_VAD:
295
0
      (*(spx_int32_t*)ptr) = st->vad_enabled;
296
0
      break;
297
0
#endif /* #ifndef DISABLE_VBR */
298
0
#if !defined(DISABLE_VBR) && !defined(DISABLE_FLOAT_API)
299
0
   case SPEEX_SET_VBR_QUALITY:
300
0
      {
301
0
         spx_int32_t q;
302
0
         float qual = (*(float*)ptr)+.6;
303
0
         st->vbr_quality = (*(float*)ptr);
304
0
         if (qual>10)
305
0
            qual=10;
306
0
         q=(int)floor(.5+*(float*)ptr);
307
0
         if (q>10)
308
0
            q=10;
309
0
         speex_encoder_ctl(st->st_low, SPEEX_SET_VBR_QUALITY, &qual);
310
0
         speex_encoder_ctl(state, SPEEX_SET_QUALITY, &q);
311
0
         break;
312
0
      }
313
0
   case SPEEX_GET_VBR_QUALITY:
314
0
      (*(float*)ptr) = st->vbr_quality;
315
0
      break;
316
0
#endif /* #if !defined(DISABLE_VBR) && !defined(DISABLE_FLOAT_API) */
317
0
#ifndef DISABLE_VBR
318
0
   case SPEEX_SET_ABR:
319
0
      st->abr_enabled = (*(spx_int32_t*)ptr);
320
0
      st->vbr_enabled = st->abr_enabled!=0;
321
0
      speex_encoder_ctl(st->st_low, SPEEX_SET_VBR, &st->vbr_enabled);
322
0
      if (st->vbr_enabled)
323
0
      {
324
0
         spx_int32_t i=10, rate, target;
325
0
         float vbr_qual;
326
0
         target = (*(spx_int32_t*)ptr);
327
0
         while (i>=0)
328
0
         {
329
0
            speex_encoder_ctl(st, SPEEX_SET_QUALITY, &i);
330
0
            speex_encoder_ctl(st, SPEEX_GET_BITRATE, &rate);
331
0
            if (rate <= target)
332
0
               break;
333
0
            i--;
334
0
         }
335
0
         vbr_qual=i;
336
0
         if (vbr_qual<0)
337
0
            vbr_qual=0;
338
0
         speex_encoder_ctl(st, SPEEX_SET_VBR_QUALITY, &vbr_qual);
339
0
         st->abr_count=0;
340
0
         st->abr_drift=0;
341
0
         st->abr_drift2=0;
342
0
      }
343
344
0
      break;
345
0
   case SPEEX_GET_ABR:
346
0
      (*(spx_int32_t*)ptr) = st->abr_enabled;
347
0
      break;
348
0
#endif /* #ifndef DISABLE_VBR */
349
350
7
   case SPEEX_SET_QUALITY:
351
7
      {
352
7
         spx_int32_t nb_qual;
353
7
         int quality = (*(spx_int32_t*)ptr);
354
7
         if (quality < 0)
355
0
            quality = 0;
356
7
         if (quality > 10)
357
0
            quality = 10;
358
7
         st->submodeSelect = st->submodeID = ((const SpeexSBMode*)(st->mode->mode))->quality_map[quality];
359
7
         nb_qual = ((const SpeexSBMode*)(st->mode->mode))->low_quality_map[quality];
360
7
         speex_encoder_ctl(st->st_low, SPEEX_SET_MODE, &nb_qual);
361
7
      }
362
7
      break;
363
3
   case SPEEX_SET_COMPLEXITY:
364
3
      speex_encoder_ctl(st->st_low, SPEEX_SET_COMPLEXITY, ptr);
365
3
      st->complexity = (*(spx_int32_t*)ptr);
366
3
      if (st->complexity<1)
367
0
         st->complexity=1;
368
3
      break;
369
0
   case SPEEX_GET_COMPLEXITY:
370
0
      (*(spx_int32_t*)ptr) = st->complexity;
371
0
      break;
372
0
   case SPEEX_SET_BITRATE:
373
0
      {
374
0
         spx_int32_t i=10;
375
0
         spx_int32_t rate, target;
376
0
         target = (*(spx_int32_t*)ptr);
377
0
         while (i>=0)
378
0
         {
379
0
            speex_encoder_ctl(st, SPEEX_SET_QUALITY, &i);
380
0
            speex_encoder_ctl(st, SPEEX_GET_BITRATE, &rate);
381
0
            if (rate <= target)
382
0
               break;
383
0
            i--;
384
0
         }
385
0
      }
386
0
      break;
387
6
   case SPEEX_GET_BITRATE:
388
6
      speex_encoder_ctl(st->st_low, request, ptr);
389
      /*fprintf (stderr, "before: %d\n", (*(int*)ptr));*/
390
6
      if (st->submodes[st->submodeID])
391
6
         (*(spx_int32_t*)ptr) += st->sampling_rate*SUBMODE(bits_per_frame)/st->full_frame_size;
392
0
      else
393
0
         (*(spx_int32_t*)ptr) += st->sampling_rate*(SB_SUBMODE_BITS+1)/st->full_frame_size;
394
      /*fprintf (stderr, "after: %d\n", (*(int*)ptr));*/
395
6
      break;
396
3
   case SPEEX_SET_SAMPLING_RATE:
397
3
      {
398
3
         spx_int32_t tmp=(*(spx_int32_t*)ptr);
399
3
         st->sampling_rate = tmp;
400
3
         tmp>>=1;
401
3
         speex_encoder_ctl(st->st_low, SPEEX_SET_SAMPLING_RATE, &tmp);
402
3
      }
403
3
      break;
404
1
   case SPEEX_GET_SAMPLING_RATE:
405
1
      (*(spx_int32_t*)ptr)=st->sampling_rate;
406
1
      break;
407
0
   case SPEEX_RESET_STATE:
408
0
      {
409
0
         int i;
410
0
         st->first = 1;
411
0
         for (i=0;i<st->lpcSize;i++)
412
0
            st->old_lsp[i]= DIV32(MULT16_16(QCONST16(3.1415927f, LSP_SHIFT), i+1), st->lpcSize+1);
413
0
         for (i=0;i<st->lpcSize;i++)
414
0
            st->mem_sw[i]=st->mem_sp[i]=st->mem_sp2[i]=0;
415
0
         for (i=0;i<QMF_ORDER;i++)
416
0
            st->h0_mem[i]=0;
417
0
      }
418
0
      break;
419
0
   case SPEEX_SET_SUBMODE_ENCODING:
420
0
      st->encode_submode = (*(spx_int32_t*)ptr);
421
0
      speex_encoder_ctl(st->st_low, SPEEX_SET_SUBMODE_ENCODING, ptr);
422
0
      break;
423
0
   case SPEEX_GET_SUBMODE_ENCODING:
424
0
      (*(spx_int32_t*)ptr) = st->encode_submode;
425
0
      break;
426
0
   case SPEEX_GET_LOOKAHEAD:
427
0
      speex_encoder_ctl(st->st_low, SPEEX_GET_LOOKAHEAD, ptr);
428
0
      (*(spx_int32_t*)ptr) = 2*(*(spx_int32_t*)ptr) + QMF_ORDER - 1;
429
0
      break;
430
0
   case SPEEX_SET_PLC_TUNING:
431
0
      speex_encoder_ctl(st->st_low, SPEEX_SET_PLC_TUNING, ptr);
432
0
      break;
433
0
   case SPEEX_GET_PLC_TUNING:
434
0
      speex_encoder_ctl(st->st_low, SPEEX_GET_PLC_TUNING, ptr);
435
0
      break;
436
0
#ifndef DISABLE_VBR
437
0
   case SPEEX_SET_VBR_MAX_BITRATE:
438
0
      {
439
0
         st->vbr_max = (*(spx_int32_t*)ptr);
440
0
         if (SPEEX_SET_VBR_MAX_BITRATE<1)
441
0
         {
442
0
            speex_encoder_ctl(st->st_low, SPEEX_SET_VBR_MAX_BITRATE, &st->vbr_max);
443
0
            st->vbr_max_high = 17600;
444
0
         } else {
445
0
            spx_int32_t low_rate;
446
0
            if (st->vbr_max >= 42200)
447
0
            {
448
0
               st->vbr_max_high = 17600;
449
0
            } else if (st->vbr_max >= 27800)
450
0
            {
451
0
               st->vbr_max_high = 9600;
452
0
            } else if (st->vbr_max > 20600)
453
0
            {
454
0
               st->vbr_max_high = 5600;
455
0
            } else {
456
0
               st->vbr_max_high = 1800;
457
0
            }
458
0
            if (st->subframeSize==80)
459
0
               st->vbr_max_high = 1800;
460
0
            low_rate = st->vbr_max - st->vbr_max_high;
461
0
            speex_encoder_ctl(st->st_low, SPEEX_SET_VBR_MAX_BITRATE, &low_rate);
462
0
         }
463
0
      }
464
0
      break;
465
0
   case SPEEX_GET_VBR_MAX_BITRATE:
466
0
      (*(spx_int32_t*)ptr) = st->vbr_max;
467
0
      break;
468
0
#endif /* #ifndef DISABLE_VBR */
469
0
   case SPEEX_SET_HIGHPASS:
470
0
      speex_encoder_ctl(st->st_low, SPEEX_SET_HIGHPASS, ptr);
471
0
      break;
472
0
   case SPEEX_GET_HIGHPASS:
473
0
      speex_encoder_ctl(st->st_low, SPEEX_GET_HIGHPASS, ptr);
474
0
      break;
475
476
477
   /* This is all internal stuff past this point */
478
0
   case SPEEX_GET_PI_GAIN:
479
0
      {
480
0
         int i;
481
0
         spx_word32_t *g = (spx_word32_t*)ptr;
482
0
         for (i=0;i<st->nbSubframes;i++)
483
0
            g[i]=st->pi_gain[i];
484
0
      }
485
0
      break;
486
0
   case SPEEX_GET_EXC:
487
0
      {
488
0
         int i;
489
0
         for (i=0;i<st->nbSubframes;i++)
490
0
            ((spx_word16_t*)ptr)[i] = st->exc_rms[i];
491
0
      }
492
0
      break;
493
0
#ifndef DISABLE_VBR
494
0
   case SPEEX_GET_RELATIVE_QUALITY:
495
0
      (*(float*)ptr)=st->relative_quality;
496
0
      break;
497
0
#endif /* #ifndef DISABLE_VBR */
498
0
   case SPEEX_SET_INNOVATION_SAVE:
499
0
      st->innov_rms_save = (spx_word16_t*)ptr;
500
0
      break;
501
1
   case SPEEX_SET_WIDEBAND:
502
1
      speex_encoder_ctl(st->st_low, SPEEX_SET_WIDEBAND, ptr);
503
1
      break;
504
1
   case SPEEX_GET_STACK:
505
1
      *((char**)ptr) = st->stack;
506
1
      break;
507
0
   default:
508
0
      speex_warning_int("Unknown nb_ctl request: ", request);
509
0
      return -1;
510
29
   }
511
29
   return 0;
512
29
}
513
514
515
int sb_encode(void *state, void *vin, SpeexBits *bits)
516
0
{
517
0
   SBEncState *st;
518
0
   int i, roots, sub;
519
0
   char *stack;
520
0
   VARDECL(spx_mem_t *mem);
521
0
   VARDECL(spx_sig_t *innov);
522
0
   VARDECL(spx_word16_t *target);
523
0
   VARDECL(spx_word16_t *syn_resp);
524
0
   VARDECL(spx_word32_t *low_pi_gain);
525
0
   spx_word16_t *low;
526
0
   spx_word16_t *high;
527
0
   VARDECL(spx_word16_t *low_exc_rms);
528
0
   VARDECL(spx_word16_t *low_innov_rms);
529
0
   const SpeexSBMode *mode;
530
0
   spx_int32_t dtx;
531
0
   spx_word16_t *in = (spx_word16_t*)vin;
532
0
   spx_word16_t e_low=0, e_high=0;
533
0
   VARDECL(spx_coef_t *lpc);
534
0
   VARDECL(spx_coef_t *interp_lpc);
535
0
   VARDECL(spx_coef_t *bw_lpc1);
536
0
   VARDECL(spx_coef_t *bw_lpc2);
537
0
   VARDECL(spx_lsp_t *lsp);
538
0
   VARDECL(spx_lsp_t *qlsp);
539
0
   VARDECL(spx_lsp_t *interp_lsp);
540
0
   VARDECL(spx_lsp_t *interp_qlsp);
541
542
0
   st = (SBEncState*)state;
543
0
   stack=st->stack;
544
0
   mode = (const SpeexSBMode*)(st->mode->mode);
545
0
   low = in;
546
0
   high = in+st->frame_size;
547
548
   /* High-band buffering / sync with low band */
549
   /* Compute the two sub-bands by filtering with QMF h0*/
550
0
   qmf_decomp(in, h0, low, high, st->full_frame_size, QMF_ORDER, st->h0_mem, stack);
551
552
0
#ifndef DISABLE_VBR
553
0
   if (st->vbr_enabled || st->vad_enabled)
554
0
   {
555
      /* Need to compute things here before the signal is trashed by the encoder */
556
      /*FIXME: Are the two signals (low, high) in sync? */
557
0
      e_low = compute_rms16(low, st->frame_size);
558
0
      e_high = compute_rms16(high, st->frame_size);
559
0
   }
560
0
#endif /* #ifndef DISABLE_VBR */
561
562
0
   ALLOC(low_innov_rms, st->nbSubframes, spx_word16_t);
563
0
   speex_encoder_ctl(st->st_low, SPEEX_SET_INNOVATION_SAVE, low_innov_rms);
564
   /* Encode the narrowband part*/
565
0
   speex_encode_native(st->st_low, low, bits);
566
567
0
   high = high - (st->windowSize-st->frame_size);
568
0
   SPEEX_COPY(high, st->high, st->windowSize-st->frame_size);
569
0
   SPEEX_COPY(st->high, &high[st->frame_size], st->windowSize-st->frame_size);
570
571
572
0
   ALLOC(low_pi_gain, st->nbSubframes, spx_word32_t);
573
0
   ALLOC(low_exc_rms, st->nbSubframes, spx_word16_t);
574
0
   speex_encoder_ctl(st->st_low, SPEEX_GET_PI_GAIN, low_pi_gain);
575
0
   speex_encoder_ctl(st->st_low, SPEEX_GET_EXC, low_exc_rms);
576
577
0
   speex_encoder_ctl(st->st_low, SPEEX_GET_LOW_MODE, &dtx);
578
579
0
   if (dtx==0)
580
0
      dtx=1;
581
0
   else
582
0
      dtx=0;
583
584
0
   ALLOC(lpc, st->lpcSize, spx_coef_t);
585
0
   ALLOC(interp_lpc, st->lpcSize, spx_coef_t);
586
0
   ALLOC(bw_lpc1, st->lpcSize, spx_coef_t);
587
0
   ALLOC(bw_lpc2, st->lpcSize, spx_coef_t);
588
589
0
   ALLOC(lsp, st->lpcSize, spx_lsp_t);
590
0
   ALLOC(qlsp, st->lpcSize, spx_lsp_t);
591
0
   ALLOC(interp_lsp, st->lpcSize, spx_lsp_t);
592
0
   ALLOC(interp_qlsp, st->lpcSize, spx_lsp_t);
593
594
0
   {
595
0
      VARDECL(spx_word16_t *autocorr);
596
0
      VARDECL(spx_word16_t *w_sig);
597
0
      ALLOC(autocorr, st->lpcSize+1, spx_word16_t);
598
0
      ALLOC(w_sig, st->windowSize, spx_word16_t);
599
      /* Window for analysis */
600
      /* FIXME: This is a kludge */
601
0
      if (st->subframeSize==80)
602
0
      {
603
0
         for (i=0;i<st->windowSize;i++)
604
0
            w_sig[i] = EXTRACT16(SHR32(MULT16_16(high[i],st->window[i>>1]),SIG_SHIFT));
605
0
      } else {
606
0
         for (i=0;i<st->windowSize;i++)
607
0
            w_sig[i] = EXTRACT16(SHR32(MULT16_16(high[i],st->window[i]),SIG_SHIFT));
608
0
      }
609
      /* Compute auto-correlation */
610
0
      _spx_autocorr(w_sig, autocorr, st->lpcSize+1, st->windowSize);
611
0
      autocorr[0] = ADD16(autocorr[0],MULT16_16_Q15(autocorr[0],st->lpc_floor)); /* Noise floor in auto-correlation domain */
612
613
      /* Lag windowing: equivalent to filtering in the power-spectrum domain */
614
0
      for (i=0;i<st->lpcSize+1;i++)
615
0
         autocorr[i] = MULT16_16_Q14(autocorr[i],st->lagWindow[i]);
616
617
      /* Levinson-Durbin */
618
0
      _spx_lpc(lpc, autocorr, st->lpcSize);
619
0
   }
620
621
   /* LPC to LSPs (x-domain) transform */
622
0
   roots=lpc_to_lsp (lpc, st->lpcSize, lsp, 10, LSP_DELTA1, stack);
623
0
   if (roots!=st->lpcSize)
624
0
   {
625
0
      roots = lpc_to_lsp (lpc, st->lpcSize, lsp, 10, LSP_DELTA2, stack);
626
0
      if (roots!=st->lpcSize) {
627
         /*If we can't find all LSP's, do some damage control and use a flat filter*/
628
0
         for (i=0;i<st->lpcSize;i++)
629
0
         {
630
0
            lsp[i]=st->old_lsp[i];
631
0
         }
632
0
      }
633
0
   }
634
635
0
#ifndef DISABLE_VBR
636
   /* VBR code */
637
0
   if ((st->vbr_enabled || st->vad_enabled) && !dtx)
638
0
   {
639
0
      float ratio;
640
0
      if (st->abr_enabled)
641
0
      {
642
0
         float qual_change=0;
643
0
         if (st->abr_drift2 * st->abr_drift > 0)
644
0
         {
645
            /* Only adapt if long-term and short-term drift are the same sign */
646
0
            qual_change = -.00001*st->abr_drift/(1+st->abr_count);
647
0
            if (qual_change>.1)
648
0
               qual_change=.1;
649
0
            if (qual_change<-.1)
650
0
               qual_change=-.1;
651
0
         }
652
0
         st->vbr_quality += qual_change;
653
0
         if (st->vbr_quality>10)
654
0
            st->vbr_quality=10;
655
0
         if (st->vbr_quality<0)
656
0
            st->vbr_quality=0;
657
0
      }
658
659
660
0
      ratio = 2*log((1.f+e_high)/(1.f+e_low));
661
662
0
      speex_encoder_ctl(st->st_low, SPEEX_GET_RELATIVE_QUALITY, &st->relative_quality);
663
0
      if (ratio<-4)
664
0
         ratio=-4;
665
0
      if (ratio>2)
666
0
         ratio=2;
667
      /*if (ratio>-2)*/
668
0
      if (st->vbr_enabled)
669
0
      {
670
0
         spx_int32_t modeid;
671
0
         modeid = mode->nb_modes-1;
672
0
         st->relative_quality+=1.0*(ratio+2);
673
0
   if (st->relative_quality<-1)
674
0
            st->relative_quality=-1;
675
0
         while (modeid)
676
0
         {
677
0
            int v1;
678
0
            float thresh;
679
0
            v1=(int)floor(st->vbr_quality);
680
0
            if (v1==10)
681
0
               thresh = mode->vbr_thresh[modeid][v1];
682
0
            else
683
0
               thresh = (st->vbr_quality-v1)   * mode->vbr_thresh[modeid][v1+1] +
684
0
                        (1+v1-st->vbr_quality) * mode->vbr_thresh[modeid][v1];
685
0
            if (st->relative_quality >= thresh && st->sampling_rate*st->submodes[modeid]->bits_per_frame/st->full_frame_size <= st->vbr_max_high)
686
0
               break;
687
0
            modeid--;
688
0
         }
689
0
         speex_encoder_ctl(state, SPEEX_SET_HIGH_MODE, &modeid);
690
0
         if (st->abr_enabled)
691
0
         {
692
0
            spx_int32_t bitrate;
693
0
            speex_encoder_ctl(state, SPEEX_GET_BITRATE, &bitrate);
694
0
            st->abr_drift+=(bitrate-st->abr_enabled);
695
0
            st->abr_drift2 = .95*st->abr_drift2 + .05*(bitrate-st->abr_enabled);
696
0
            st->abr_count += 1.0;
697
0
         }
698
699
0
      } else {
700
         /* VAD only */
701
0
         int modeid;
702
0
         if (st->relative_quality<2.0)
703
0
            modeid=1;
704
0
         else
705
0
            modeid=st->submodeSelect;
706
         /*speex_encoder_ctl(state, SPEEX_SET_MODE, &mode);*/
707
0
         st->submodeID=modeid;
708
709
0
      }
710
      /*fprintf (stderr, "%f %f\n", ratio, low_qual);*/
711
0
   }
712
0
#endif /* #ifndef DISABLE_VBR */
713
714
0
   if (st->encode_submode)
715
0
   {
716
0
      speex_bits_pack(bits, 1, 1);
717
0
      if (dtx)
718
0
         speex_bits_pack(bits, 0, SB_SUBMODE_BITS);
719
0
      else
720
0
         speex_bits_pack(bits, st->submodeID, SB_SUBMODE_BITS);
721
0
   }
722
723
   /* If null mode (no transmission), just set a couple things to zero*/
724
0
   if (dtx || st->submodes[st->submodeID] == NULL)
725
0
   {
726
0
      for (i=0;i<st->frame_size;i++)
727
0
         high[i]=VERY_SMALL;
728
729
0
      for (i=0;i<st->lpcSize;i++)
730
0
         st->mem_sw[i]=0;
731
0
      st->first=1;
732
733
      /* Final signal synthesis from excitation */
734
0
      iir_mem16(high, st->interp_qlpc, high, st->frame_size, st->lpcSize, st->mem_sp, stack);
735
736
0
      if (dtx)
737
0
         return 0;
738
0
      else
739
0
         return 1;
740
0
   }
741
742
743
   /* LSP quantization */
744
0
   SUBMODE(lsp_quant)(lsp, qlsp, st->lpcSize, bits);
745
746
0
   if (st->first)
747
0
   {
748
0
      for (i=0;i<st->lpcSize;i++)
749
0
         st->old_lsp[i] = lsp[i];
750
0
      for (i=0;i<st->lpcSize;i++)
751
0
         st->old_qlsp[i] = qlsp[i];
752
0
   }
753
754
0
   ALLOC(mem, st->lpcSize, spx_mem_t);
755
0
   ALLOC(syn_resp, st->subframeSize, spx_word16_t);
756
0
   ALLOC(innov, st->subframeSize, spx_sig_t);
757
0
   ALLOC(target, st->subframeSize, spx_word16_t);
758
759
0
   for (sub=0;sub<st->nbSubframes;sub++)
760
0
   {
761
0
      VARDECL(spx_word16_t *exc);
762
0
      VARDECL(spx_word16_t *res);
763
0
      VARDECL(spx_word16_t *sw);
764
0
      spx_word16_t *sp;
765
0
      spx_word16_t filter_ratio;     /*Q7*/
766
0
      int offset;
767
0
      spx_word32_t rl, rh;           /*Q13*/
768
0
      spx_word16_t eh=0;
769
770
0
      offset = st->subframeSize*sub;
771
0
      sp=high+offset;
772
0
      ALLOC(exc, st->subframeSize, spx_word16_t);
773
0
      ALLOC(res, st->subframeSize, spx_word16_t);
774
0
      ALLOC(sw, st->subframeSize, spx_word16_t);
775
776
      /* LSP interpolation (quantized and unquantized) */
777
0
      lsp_interpolate(st->old_lsp, lsp, interp_lsp, st->lpcSize, sub, st->nbSubframes, LSP_MARGIN);
778
0
      lsp_interpolate(st->old_qlsp, qlsp, interp_qlsp, st->lpcSize, sub, st->nbSubframes, LSP_MARGIN);
779
780
0
      lsp_to_lpc(interp_lsp, interp_lpc, st->lpcSize,stack);
781
0
      lsp_to_lpc(interp_qlsp, st->interp_qlpc, st->lpcSize, stack);
782
783
0
      bw_lpc(st->gamma1, interp_lpc, bw_lpc1, st->lpcSize);
784
0
      bw_lpc(st->gamma2, interp_lpc, bw_lpc2, st->lpcSize);
785
786
      /* Compute mid-band (4000 Hz for wideband) response of low-band and high-band
787
         filters */
788
0
      st->pi_gain[sub]=LPC_SCALING;
789
0
      rh = LPC_SCALING;
790
0
      for (i=0;i<st->lpcSize;i+=2)
791
0
      {
792
0
         rh += st->interp_qlpc[i+1] - st->interp_qlpc[i];
793
0
         st->pi_gain[sub] += st->interp_qlpc[i] + st->interp_qlpc[i+1];
794
0
      }
795
796
0
      rl = low_pi_gain[sub];
797
#ifdef FIXED_POINT
798
      filter_ratio=EXTRACT16(SATURATE(PDIV32(SHL32(ADD32(rl,82),7),ADD32(82,rh)),32767));
799
#else
800
0
      filter_ratio=(rl+.01)/(rh+.01);
801
0
#endif
802
803
      /* Compute "real excitation" */
804
0
      fir_mem16(sp, st->interp_qlpc, exc, st->subframeSize, st->lpcSize, st->mem_sp2, stack);
805
      /* Compute energy of low-band and high-band excitation */
806
807
0
      eh = compute_rms16(exc, st->subframeSize);
808
809
0
      if (!SUBMODE(innovation_quant)) {/* 1 for spectral folding excitation, 0 for stochastic */
810
0
         spx_word32_t g;   /*Q7*/
811
0
         spx_word16_t el;  /*Q0*/
812
0
         el = low_innov_rms[sub];
813
814
         /* Gain to use if we want to use the low-band excitation for high-band */
815
0
         g=PDIV32(MULT16_16(filter_ratio,eh),EXTEND32(ADD16(1,el)));
816
817
#if 0
818
         {
819
            char *tmp_stack=stack;
820
            float *tmp_sig;
821
            float g2;
822
            ALLOC(tmp_sig, st->subframeSize, spx_sig_t);
823
            for (i=0;i<st->lpcSize;i++)
824
               mem[i]=st->mem_sp[i];
825
            iir_mem2(st->low_innov+offset, st->interp_qlpc, tmp_sig, st->subframeSize, st->lpcSize, mem);
826
            g2 = compute_rms(sp, st->subframeSize)/(.01+compute_rms(tmp_sig, st->subframeSize));
827
            /*fprintf (stderr, "gains: %f %f\n", g, g2);*/
828
            g = g2;
829
            stack = tmp_stack;
830
         }
831
#endif
832
833
         /*print_vec(&g, 1, "gain factor");*/
834
         /* Gain quantization */
835
0
         {
836
0
            int quant = scal_quant(g, fold_quant_bound, 32);
837
            /*speex_warning_int("tata", quant);*/
838
0
            if (quant<0)
839
0
               quant=0;
840
0
            if (quant>31)
841
0
               quant=31;
842
0
            speex_bits_pack(bits, quant, 5);
843
0
         }
844
0
         if (st->innov_rms_save)
845
0
         {
846
0
            st->innov_rms_save[sub] = eh;
847
0
         }
848
0
         st->exc_rms[sub] = eh;
849
0
      } else {
850
0
         spx_word16_t gc;       /*Q7*/
851
0
         spx_word32_t scale;    /*Q14*/
852
0
         spx_word16_t el;       /*Q0*/
853
0
         el = low_exc_rms[sub]; /*Q0*/
854
855
0
         gc = PDIV32_16(MULT16_16(filter_ratio,1+eh),1+el);
856
857
         /* This is a kludge that cleans up a historical bug */
858
0
         if (st->subframeSize==80)
859
0
            gc = MULT16_16_P15(QCONST16(0.70711f,15),gc);
860
         /*printf ("%f %f %f %f\n", el, eh, filter_ratio, gc);*/
861
0
         {
862
0
            int qgc = scal_quant(gc, gc_quant_bound, 16);
863
0
            speex_bits_pack(bits, qgc, 4);
864
0
            gc = MULT16_16_Q15(QCONST16(0.87360,15),gc_quant_bound[qgc]);
865
0
         }
866
0
         if (st->subframeSize==80)
867
0
            gc = MULT16_16_P14(QCONST16(1.4142f,14), gc);
868
869
0
         scale = SHL32(MULT16_16(PDIV32_16(SHL32(EXTEND32(gc),SIG_SHIFT-6),MAX16(EPSILON,filter_ratio)),(1+el)),6);
870
871
0
         compute_impulse_response(st->interp_qlpc, bw_lpc1, bw_lpc2, syn_resp, st->subframeSize, st->lpcSize, stack);
872
873
874
         /* Reset excitation */
875
0
         for (i=0;i<st->subframeSize;i++)
876
0
            res[i]=VERY_SMALL;
877
878
         /* Compute zero response (ringing) of A(z/g1) / ( A(z/g2) * Aq(z) ) */
879
0
         for (i=0;i<st->lpcSize;i++)
880
0
            mem[i]=st->mem_sp[i];
881
0
         iir_mem16(res, st->interp_qlpc, res, st->subframeSize, st->lpcSize, mem, stack);
882
883
0
         for (i=0;i<st->lpcSize;i++)
884
0
            mem[i]=st->mem_sw[i];
885
0
         filter_mem16(res, bw_lpc1, bw_lpc2, res, st->subframeSize, st->lpcSize, mem, stack);
886
887
         /* Compute weighted signal */
888
0
         for (i=0;i<st->lpcSize;i++)
889
0
            mem[i]=st->mem_sw[i];
890
0
         filter_mem16(sp, bw_lpc1, bw_lpc2, sw, st->subframeSize, st->lpcSize, mem, stack);
891
892
         /* Compute target signal */
893
0
         for (i=0;i<st->subframeSize;i++)
894
0
            target[i]=SUB16(sw[i],res[i]);
895
896
0
         signal_div(target, target, scale, st->subframeSize);
897
898
         /* Reset excitation */
899
0
         SPEEX_MEMSET(innov, 0, st->subframeSize);
900
901
         /*print_vec(target, st->subframeSize, "\ntarget");*/
902
0
         SUBMODE(innovation_quant)(target, st->interp_qlpc, bw_lpc1, bw_lpc2,
903
0
                                   SUBMODE(innovation_params), st->lpcSize, st->subframeSize,
904
0
                                   innov, syn_resp, bits, stack, st->complexity, SUBMODE(double_codebook));
905
         /*print_vec(target, st->subframeSize, "after");*/
906
907
0
         signal_mul(innov, innov, scale, st->subframeSize);
908
909
0
         if (SUBMODE(double_codebook)) {
910
0
            char *tmp_stack=stack;
911
0
            VARDECL(spx_sig_t *innov2);
912
0
            ALLOC(innov2, st->subframeSize, spx_sig_t);
913
0
            SPEEX_MEMSET(innov2, 0, st->subframeSize);
914
0
            for (i=0;i<st->subframeSize;i++)
915
0
               target[i]=MULT16_16_P13(QCONST16(2.5f,13), target[i]);
916
917
0
            SUBMODE(innovation_quant)(target, st->interp_qlpc, bw_lpc1, bw_lpc2,
918
0
                                      SUBMODE(innovation_params), st->lpcSize, st->subframeSize,
919
0
                                      innov2, syn_resp, bits, stack, st->complexity, 0);
920
0
            signal_mul(innov2, innov2, MULT16_32_P15(QCONST16(0.4f,15),scale), st->subframeSize);
921
922
0
            for (i=0;i<st->subframeSize;i++)
923
0
               innov[i] = ADD32(innov[i],innov2[i]);
924
0
            stack = tmp_stack;
925
0
         }
926
0
         for (i=0;i<st->subframeSize;i++)
927
0
            exc[i] = PSHR32(innov[i],SIG_SHIFT);
928
929
0
         if (st->innov_rms_save)
930
0
         {
931
0
            st->innov_rms_save[sub] = MULT16_16_Q15(QCONST16(.70711f, 15), compute_rms(innov, st->subframeSize));
932
0
         }
933
0
         st->exc_rms[sub] = compute_rms16(exc, st->subframeSize);
934
935
936
0
      }
937
938
939
      /*Keep the previous memory*/
940
0
      for (i=0;i<st->lpcSize;i++)
941
0
         mem[i]=st->mem_sp[i];
942
      /* Final signal synthesis from excitation */
943
0
      iir_mem16(exc, st->interp_qlpc, sp, st->subframeSize, st->lpcSize, st->mem_sp, stack);
944
945
      /* Compute weighted signal again, from synthesized speech (not sure it's the right thing) */
946
0
      filter_mem16(sp, bw_lpc1, bw_lpc2, sw, st->subframeSize, st->lpcSize, st->mem_sw, stack);
947
0
   }
948
949
0
   for (i=0;i<st->lpcSize;i++)
950
0
      st->old_lsp[i] = lsp[i];
951
0
   for (i=0;i<st->lpcSize;i++)
952
0
      st->old_qlsp[i] = qlsp[i];
953
954
0
   st->first=0;
955
956
0
   return 1;
957
0
}
958
959
#endif /* DISABLE_ENCODER */
960
961
962
#ifndef DISABLE_DECODER
963
void *sb_decoder_init(const SpeexMode *m)
964
0
{
965
0
   spx_int32_t tmp;
966
0
   SBDecState *st;
967
0
   const SpeexSBMode *mode;
968
0
   st = (SBDecState*)speex_alloc(sizeof(SBDecState));
969
0
   if (!st)
970
0
      return NULL;
971
0
   st->mode = m;
972
0
   mode=(const SpeexSBMode*)m->mode;
973
0
   st->encode_submode = 1;
974
975
0
   st->st_low = speex_decoder_init(mode->nb_mode);
976
#if defined(VAR_ARRAYS) || defined (USE_ALLOCA)
977
   st->stack = NULL;
978
#else
979
   /*st->stack = (char*)speex_alloc_scratch(SB_DEC_STACK);*/
980
0
   speex_decoder_ctl(st->st_low, SPEEX_GET_STACK, &st->stack);
981
0
#endif
982
983
0
   st->full_frame_size = 2*mode->frameSize;
984
0
   st->frame_size = mode->frameSize;
985
0
   st->subframeSize = mode->subframeSize;
986
0
   st->nbSubframes = mode->frameSize/mode->subframeSize;
987
0
   st->lpcSize=mode->lpcSize;
988
0
   speex_decoder_ctl(st->st_low, SPEEX_GET_SAMPLING_RATE, &st->sampling_rate);
989
0
   st->sampling_rate*=2;
990
0
   tmp=1;
991
0
   speex_decoder_ctl(st->st_low, SPEEX_SET_WIDEBAND, &tmp);
992
993
0
   st->submodes=mode->submodes;
994
0
   st->submodeID=mode->defaultSubmode;
995
996
0
   st->first=1;
997
998
0
   st->g0_mem = (spx_word16_t*)speex_alloc((QMF_ORDER)*sizeof(spx_word16_t));
999
0
   st->g1_mem = (spx_word16_t*)speex_alloc((QMF_ORDER)*sizeof(spx_word16_t));
1000
1001
0
   st->excBuf = (spx_word16_t*)speex_alloc((st->subframeSize)*sizeof(spx_word16_t));
1002
1003
0
   st->old_qlsp = (spx_lsp_t*)speex_alloc((st->lpcSize)*sizeof(spx_lsp_t));
1004
0
   st->interp_qlpc = (spx_coef_t*)speex_alloc(st->lpcSize*sizeof(spx_coef_t));
1005
1006
0
   st->pi_gain = (spx_word32_t*)speex_alloc((st->nbSubframes)*sizeof(spx_word32_t));
1007
0
   st->exc_rms = (spx_word16_t*)speex_alloc((st->nbSubframes)*sizeof(spx_word16_t));
1008
0
   st->mem_sp = (spx_mem_t*)speex_alloc((2*st->lpcSize)*sizeof(spx_mem_t));
1009
1010
0
   st->innov_save = NULL;
1011
1012
1013
0
   st->lpc_enh_enabled=0;
1014
0
   st->seed = 1000;
1015
1016
#ifdef ENABLE_VALGRIND
1017
   VALGRIND_MAKE_MEM_DEFINED(st, (st->stack-(char*)st));
1018
#endif
1019
0
   return st;
1020
0
}
1021
1022
void sb_decoder_destroy(void *state)
1023
0
{
1024
0
   SBDecState *st;
1025
0
   st = (SBDecState*)state;
1026
0
   speex_decoder_destroy(st->st_low);
1027
0
#if !(defined(VAR_ARRAYS) || defined (USE_ALLOCA))
1028
   /*speex_free_scratch(st->stack);*/
1029
0
#endif
1030
1031
0
   speex_free(st->g0_mem);
1032
0
   speex_free(st->g1_mem);
1033
0
   speex_free(st->excBuf);
1034
0
   speex_free(st->old_qlsp);
1035
0
   speex_free(st->interp_qlpc);
1036
0
   speex_free(st->pi_gain);
1037
0
   speex_free(st->exc_rms);
1038
0
   speex_free(st->mem_sp);
1039
1040
0
   speex_free(state);
1041
0
}
1042
1043
1044
1045
int sb_decoder_ctl(void *state, int request, void *ptr)
1046
0
{
1047
0
   SBDecState *st;
1048
0
   st=(SBDecState*)state;
1049
0
   switch(request)
1050
0
   {
1051
0
   case SPEEX_SET_HIGH_MODE:
1052
0
      st->submodeID = (*(spx_int32_t*)ptr);
1053
0
      break;
1054
0
   case SPEEX_SET_LOW_MODE:
1055
0
      speex_decoder_ctl(st->st_low, SPEEX_SET_LOW_MODE, ptr);
1056
0
      break;
1057
0
   case SPEEX_GET_LOW_MODE:
1058
0
      speex_decoder_ctl(st->st_low, SPEEX_GET_LOW_MODE, ptr);
1059
0
      break;
1060
0
   case SPEEX_GET_FRAME_SIZE:
1061
0
      (*(spx_int32_t*)ptr) = st->full_frame_size;
1062
0
      break;
1063
0
   case SPEEX_SET_ENH:
1064
0
      speex_decoder_ctl(st->st_low, request, ptr);
1065
0
      st->lpc_enh_enabled = *((spx_int32_t*)ptr);
1066
0
      break;
1067
0
   case SPEEX_GET_ENH:
1068
0
      *((spx_int32_t*)ptr) = st->lpc_enh_enabled;
1069
0
      break;
1070
0
   case SPEEX_SET_MODE:
1071
0
   case SPEEX_SET_QUALITY:
1072
0
      {
1073
0
         spx_int32_t nb_qual;
1074
0
         int quality = (*(spx_int32_t*)ptr);
1075
0
         if (quality < 0)
1076
0
            quality = 0;
1077
0
         if (quality > 10)
1078
0
            quality = 10;
1079
0
         st->submodeID = ((const SpeexSBMode*)(st->mode->mode))->quality_map[quality];
1080
0
         nb_qual = ((const SpeexSBMode*)(st->mode->mode))->low_quality_map[quality];
1081
0
         speex_decoder_ctl(st->st_low, SPEEX_SET_MODE, &nb_qual);
1082
0
      }
1083
0
      break;
1084
0
   case SPEEX_GET_BITRATE:
1085
0
      speex_decoder_ctl(st->st_low, request, ptr);
1086
0
      if (st->submodes[st->submodeID])
1087
0
         (*(spx_int32_t*)ptr) += st->sampling_rate*SUBMODE(bits_per_frame)/st->full_frame_size;
1088
0
      else
1089
0
         (*(spx_int32_t*)ptr) += st->sampling_rate*(SB_SUBMODE_BITS+1)/st->full_frame_size;
1090
0
      break;
1091
0
   case SPEEX_SET_SAMPLING_RATE:
1092
0
      {
1093
0
         spx_int32_t tmp=(*(spx_int32_t*)ptr);
1094
0
         st->sampling_rate = tmp;
1095
0
         tmp>>=1;
1096
0
         speex_decoder_ctl(st->st_low, SPEEX_SET_SAMPLING_RATE, &tmp);
1097
0
      }
1098
0
      break;
1099
0
   case SPEEX_GET_SAMPLING_RATE:
1100
0
      (*(spx_int32_t*)ptr)=st->sampling_rate;
1101
0
      break;
1102
0
   case SPEEX_SET_HANDLER:
1103
0
      speex_decoder_ctl(st->st_low, SPEEX_SET_HANDLER, ptr);
1104
0
      break;
1105
0
   case SPEEX_SET_USER_HANDLER:
1106
0
      speex_decoder_ctl(st->st_low, SPEEX_SET_USER_HANDLER, ptr);
1107
0
      break;
1108
0
   case SPEEX_RESET_STATE:
1109
0
      {
1110
0
         int i;
1111
0
         for (i=0;i<2*st->lpcSize;i++)
1112
0
            st->mem_sp[i]=0;
1113
0
         for (i=0;i<QMF_ORDER;i++)
1114
0
            st->g0_mem[i]=st->g1_mem[i]=0;
1115
0
         st->last_ener=0;
1116
0
      }
1117
0
      break;
1118
0
   case SPEEX_SET_SUBMODE_ENCODING:
1119
0
      st->encode_submode = (*(spx_int32_t*)ptr);
1120
0
      speex_decoder_ctl(st->st_low, SPEEX_SET_SUBMODE_ENCODING, ptr);
1121
0
      break;
1122
0
   case SPEEX_GET_SUBMODE_ENCODING:
1123
0
      (*(spx_int32_t*)ptr) = st->encode_submode;
1124
0
      break;
1125
0
   case SPEEX_GET_LOOKAHEAD:
1126
0
      speex_decoder_ctl(st->st_low, SPEEX_GET_LOOKAHEAD, ptr);
1127
0
      (*(spx_int32_t*)ptr) = 2*(*(spx_int32_t*)ptr);
1128
0
      break;
1129
0
   case SPEEX_SET_HIGHPASS:
1130
0
      speex_decoder_ctl(st->st_low, SPEEX_SET_HIGHPASS, ptr);
1131
0
      break;
1132
0
   case SPEEX_GET_HIGHPASS:
1133
0
      speex_decoder_ctl(st->st_low, SPEEX_GET_HIGHPASS, ptr);
1134
0
      break;
1135
0
   case SPEEX_GET_ACTIVITY:
1136
0
      speex_decoder_ctl(st->st_low, SPEEX_GET_ACTIVITY, ptr);
1137
0
      break;
1138
0
   case SPEEX_GET_PI_GAIN:
1139
0
      {
1140
0
         int i;
1141
0
         spx_word32_t *g = (spx_word32_t*)ptr;
1142
0
         for (i=0;i<st->nbSubframes;i++)
1143
0
            g[i]=st->pi_gain[i];
1144
0
      }
1145
0
      break;
1146
0
   case SPEEX_GET_EXC:
1147
0
      {
1148
0
         int i;
1149
0
         for (i=0;i<st->nbSubframes;i++)
1150
0
            ((spx_word16_t*)ptr)[i] = st->exc_rms[i];
1151
0
      }
1152
0
      break;
1153
0
   case SPEEX_GET_DTX_STATUS:
1154
0
      speex_decoder_ctl(st->st_low, SPEEX_GET_DTX_STATUS, ptr);
1155
0
      break;
1156
0
   case SPEEX_SET_INNOVATION_SAVE:
1157
0
      st->innov_save = (spx_word16_t*)ptr;
1158
0
      break;
1159
0
   case SPEEX_SET_WIDEBAND:
1160
0
      speex_decoder_ctl(st->st_low, SPEEX_SET_WIDEBAND, ptr);
1161
0
      break;
1162
0
   case SPEEX_GET_STACK:
1163
0
      *((char**)ptr) = st->stack;
1164
0
      break;
1165
0
   default:
1166
0
      speex_warning_int("Unknown nb_ctl request: ", request);
1167
0
      return -1;
1168
0
   }
1169
0
   return 0;
1170
0
}
1171
1172
1173
static void sb_decode_lost(SBDecState *st, spx_word16_t *out, int dtx, char *stack)
1174
0
{
1175
0
   int i;
1176
0
   int saved_modeid=0;
1177
1178
0
   if (dtx)
1179
0
   {
1180
0
      saved_modeid=st->submodeID;
1181
0
      st->submodeID=1;
1182
0
   } else {
1183
0
      bw_lpc(QCONST16(0.99f,15), st->interp_qlpc, st->interp_qlpc, st->lpcSize);
1184
0
   }
1185
1186
0
   st->first=1;
1187
1188
1189
   /* Final signal synthesis from excitation */
1190
0
   if (!dtx)
1191
0
   {
1192
0
      st->last_ener =  MULT16_16_Q15(QCONST16(.9f,15),st->last_ener);
1193
0
   }
1194
0
   for (i=0;i<st->frame_size;i++)
1195
0
      out[i+st->frame_size] = speex_rand(st->last_ener, &st->seed);
1196
1197
0
   iir_mem16(out+st->frame_size, st->interp_qlpc, out+st->frame_size, st->frame_size, st->lpcSize,
1198
0
            st->mem_sp, stack);
1199
1200
1201
   /* Reconstruct the original */
1202
0
   qmf_synth(out, out+st->frame_size, h0, out, st->full_frame_size, QMF_ORDER, st->g0_mem, st->g1_mem, stack);
1203
0
   if (dtx)
1204
0
   {
1205
0
      st->submodeID=saved_modeid;
1206
0
   }
1207
1208
0
   return;
1209
0
}
1210
1211
int sb_decode(void *state, SpeexBits *bits, void *vout)
1212
0
{
1213
0
   int i, sub;
1214
0
   SBDecState *st;
1215
0
   int wideband;
1216
0
   int ret;
1217
0
   char *stack;
1218
0
   VARDECL(spx_word32_t *low_pi_gain);
1219
0
   VARDECL(spx_word16_t *low_exc_rms);
1220
0
   VARDECL(spx_coef_t *ak);
1221
0
   VARDECL(spx_lsp_t *qlsp);
1222
0
   VARDECL(spx_lsp_t *interp_qlsp);
1223
0
   spx_int32_t dtx;
1224
0
   const SpeexSBMode *mode;
1225
0
   spx_word16_t *out = (spx_word16_t*)vout;
1226
0
   spx_word16_t *low_innov_alias;
1227
0
   spx_word32_t exc_ener_sum = 0;
1228
1229
0
   st = (SBDecState*)state;
1230
0
   stack=st->stack;
1231
0
   mode = (const SpeexSBMode*)(st->mode->mode);
1232
1233
0
   low_innov_alias = out+st->frame_size;
1234
0
   speex_decoder_ctl(st->st_low, SPEEX_SET_INNOVATION_SAVE, low_innov_alias);
1235
   /* Decode the low-band */
1236
0
   ret = speex_decode_native(st->st_low, bits, out);
1237
1238
0
   speex_decoder_ctl(st->st_low, SPEEX_GET_DTX_STATUS, &dtx);
1239
1240
   /* If error decoding the narrowband part, propagate error */
1241
0
   if (ret!=0)
1242
0
   {
1243
0
      return ret;
1244
0
   }
1245
1246
0
   if (!bits)
1247
0
   {
1248
0
      sb_decode_lost(st, out, dtx, stack);
1249
0
      return 0;
1250
0
   }
1251
1252
0
   if (st->encode_submode)
1253
0
   {
1254
1255
      /*Check "wideband bit"*/
1256
0
      if (speex_bits_remaining(bits)>0)
1257
0
         wideband = speex_bits_peek(bits);
1258
0
      else
1259
0
         wideband = 0;
1260
0
      if (wideband)
1261
0
      {
1262
         /*Regular wideband frame, read the submode*/
1263
0
         wideband = speex_bits_unpack_unsigned(bits, 1);
1264
0
         st->submodeID = speex_bits_unpack_unsigned(bits, SB_SUBMODE_BITS);
1265
0
      } else
1266
0
      {
1267
         /*Was a narrowband frame, set "null submode"*/
1268
0
         st->submodeID = 0;
1269
0
      }
1270
0
      if (st->submodeID != 0 && st->submodes[st->submodeID] == NULL)
1271
0
      {
1272
0
         speex_notify("Invalid mode encountered. The stream is corrupted.");
1273
0
         return -2;
1274
0
      }
1275
0
   }
1276
1277
   /* If null mode (no transmission), just set a couple things to zero*/
1278
0
   if (st->submodes[st->submodeID] == NULL)
1279
0
   {
1280
0
      if (st->innov_save)
1281
0
        SPEEX_MEMSET(st->innov_save, 0, st->full_frame_size);
1282
1283
0
      if (dtx)
1284
0
      {
1285
0
         sb_decode_lost(st, out, 1, stack);
1286
0
         return 0;
1287
0
      }
1288
1289
0
      for (i=0;i<st->frame_size;i++)
1290
0
         out[st->frame_size+i]=VERY_SMALL;
1291
1292
0
      st->first=1;
1293
1294
      /* Final signal synthesis from excitation */
1295
0
      iir_mem16(out+st->frame_size, st->interp_qlpc, out+st->frame_size, st->frame_size, st->lpcSize, st->mem_sp, stack);
1296
1297
0
      qmf_synth(out, out+st->frame_size, h0, out, st->full_frame_size, QMF_ORDER, st->g0_mem, st->g1_mem, stack);
1298
1299
0
      return 0;
1300
1301
0
   }
1302
1303
0
   ALLOC(low_pi_gain, st->nbSubframes, spx_word32_t);
1304
0
   ALLOC(low_exc_rms, st->nbSubframes, spx_word16_t);
1305
0
   speex_decoder_ctl(st->st_low, SPEEX_GET_PI_GAIN, low_pi_gain);
1306
0
   speex_decoder_ctl(st->st_low, SPEEX_GET_EXC, low_exc_rms);
1307
1308
0
   ALLOC(qlsp, st->lpcSize, spx_lsp_t);
1309
0
   ALLOC(interp_qlsp, st->lpcSize, spx_lsp_t);
1310
0
   SUBMODE(lsp_unquant)(qlsp, st->lpcSize, bits);
1311
1312
0
   if (st->first)
1313
0
   {
1314
0
      for (i=0;i<st->lpcSize;i++)
1315
0
         st->old_qlsp[i] = qlsp[i];
1316
0
   }
1317
1318
0
   ALLOC(ak, st->lpcSize, spx_coef_t);
1319
1320
0
   for (sub=0;sub<st->nbSubframes;sub++)
1321
0
   {
1322
0
      VARDECL(spx_word32_t *exc);
1323
0
      spx_word16_t *innov_save=NULL;
1324
0
      spx_word16_t *sp;
1325
0
      spx_word16_t filter_ratio;
1326
0
      spx_word16_t el=0;
1327
0
      int offset;
1328
0
      spx_word32_t rl=0,rh=0;
1329
1330
0
      offset = st->subframeSize*sub;
1331
0
      sp=out+st->frame_size+offset;
1332
0
      ALLOC(exc, st->subframeSize, spx_word32_t);
1333
      /* Pointer for saving innovation */
1334
0
      if (st->innov_save)
1335
0
      {
1336
0
         innov_save = st->innov_save+2*offset;
1337
0
         SPEEX_MEMSET(innov_save, 0, 2*st->subframeSize);
1338
0
      }
1339
1340
      /* LSP interpolation */
1341
0
      lsp_interpolate(st->old_qlsp, qlsp, interp_qlsp, st->lpcSize, sub, st->nbSubframes, LSP_MARGIN);
1342
1343
      /* LSP to LPC */
1344
0
      lsp_to_lpc(interp_qlsp, ak, st->lpcSize, stack);
1345
1346
      /* Calculate response ratio between the low and high filter in the middle
1347
         of the band (4000 Hz) */
1348
1349
0
         st->pi_gain[sub]=LPC_SCALING;
1350
0
         rh = LPC_SCALING;
1351
0
         for (i=0;i<st->lpcSize;i+=2)
1352
0
         {
1353
0
            rh += ak[i+1] - ak[i];
1354
0
            st->pi_gain[sub] += ak[i] + ak[i+1];
1355
0
         }
1356
1357
0
         rl = low_pi_gain[sub];
1358
#ifdef FIXED_POINT
1359
         filter_ratio=EXTRACT16(SATURATE(PDIV32(SHL32(ADD32(rl,82),7),ADD32(82,rh)),32767));
1360
#else
1361
0
         filter_ratio=(rl+.01)/(rh+.01);
1362
0
#endif
1363
1364
0
      SPEEX_MEMSET(exc, 0, st->subframeSize);
1365
0
      if (!SUBMODE(innovation_unquant))
1366
0
      {
1367
0
         spx_word32_t g;
1368
0
         int quant;
1369
1370
0
         quant = speex_bits_unpack_unsigned(bits, 5);
1371
0
         g= spx_exp(MULT16_16(QCONST16(.125f,11),(quant-10)));
1372
1373
         /* Clamp to a minimum of epsilon to avoid division by 0 */
1374
0
         g = PDIV32(g, MAX16(EPSILON,filter_ratio));
1375
1376
0
         for (i=0;i<st->subframeSize;i+=2)
1377
0
         {
1378
0
            exc[i]=SHL32(MULT16_32_P15(MULT16_16_Q15(mode->folding_gain,low_innov_alias[offset+i]),SHL32(g,6)),SIG_SHIFT);
1379
0
            exc[i+1]=NEG32(SHL32(MULT16_32_P15(MULT16_16_Q15(mode->folding_gain,low_innov_alias[offset+i+1]),SHL32(g,6)),SIG_SHIFT));
1380
0
         }
1381
1382
0
      } else {
1383
0
         spx_word16_t gc;
1384
0
         spx_word32_t scale;
1385
0
         int qgc = speex_bits_unpack_unsigned(bits, 4);
1386
1387
0
         el = low_exc_rms[sub];
1388
0
         gc = MULT16_16_Q15(QCONST16(0.87360,15),gc_quant_bound[qgc]);
1389
1390
0
         if (st->subframeSize==80)
1391
0
            gc = MULT16_16_P14(QCONST16(1.4142f,14),gc);
1392
1393
0
         scale = SHL32(PDIV32(SHL32(MULT16_16(gc, el),3), MAX16(EPSILON,filter_ratio)),SIG_SHIFT-3);
1394
0
         SUBMODE(innovation_unquant)(exc, SUBMODE(innovation_params), st->subframeSize,
1395
0
                                     bits, stack, &st->seed);
1396
1397
0
         signal_mul(exc,exc,scale,st->subframeSize);
1398
1399
0
         if (SUBMODE(double_codebook)) {
1400
0
            char *tmp_stack=stack;
1401
0
            VARDECL(spx_sig_t *innov2);
1402
0
            ALLOC(innov2, st->subframeSize, spx_sig_t);
1403
0
            SPEEX_MEMSET(innov2, 0, st->subframeSize);
1404
0
            SUBMODE(innovation_unquant)(innov2, SUBMODE(innovation_params), st->subframeSize,
1405
0
                                        bits, stack, &st->seed);
1406
0
            signal_mul(innov2, innov2, MULT16_32_P15(QCONST16(0.4f,15),scale), st->subframeSize);
1407
0
            for (i=0;i<st->subframeSize;i++)
1408
0
               exc[i] = ADD32(exc[i],innov2[i]);
1409
0
            stack = tmp_stack;
1410
0
         }
1411
1412
0
      }
1413
1414
0
      if (st->innov_save)
1415
0
      {
1416
0
         for (i=0;i<st->subframeSize;i++)
1417
0
            innov_save[2*i]=EXTRACT16(PSHR32(exc[i],SIG_SHIFT));
1418
0
      }
1419
1420
0
      iir_mem16(st->excBuf, st->interp_qlpc, sp, st->subframeSize, st->lpcSize,
1421
0
               st->mem_sp, stack);
1422
0
      for (i=0;i<st->subframeSize;i++)
1423
0
         st->excBuf[i]=EXTRACT16(PSHR32(exc[i],SIG_SHIFT));
1424
0
      for (i=0;i<st->lpcSize;i++)
1425
0
         st->interp_qlpc[i] = ak[i];
1426
0
      st->exc_rms[sub] = compute_rms16(st->excBuf, st->subframeSize);
1427
0
      exc_ener_sum = ADD32(exc_ener_sum, DIV32(MULT16_16(st->exc_rms[sub],st->exc_rms[sub]), st->nbSubframes));
1428
0
   }
1429
0
   st->last_ener = spx_sqrt(exc_ener_sum);
1430
1431
0
   qmf_synth(out, out+st->frame_size, h0, out, st->full_frame_size, QMF_ORDER, st->g0_mem, st->g1_mem, stack);
1432
0
   for (i=0;i<st->lpcSize;i++)
1433
0
      st->old_qlsp[i] = qlsp[i];
1434
1435
0
   st->first=0;
1436
1437
0
   return 0;
1438
0
}
1439
#endif /* DISABLE_DECODER */
1440
1441
1442
1443
#endif
1444