Coverage Report

Created: 2026-08-13 07:12

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/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
231k
#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
62.1k
#define LSP_MARGIN 410
77
6.26k
#define LSP_DELTA1 6553
78
156
#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
37.3k
#define LSP_MARGIN .05
92
3.86k
#define LSP_DELTA1 .2
93
20
#define LSP_DELTA2 .05
94
95
#endif
96
97
90.4k
#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
5.31k
{
146
5.31k
   int i;
147
5.31k
   spx_int32_t tmp;
148
5.31k
   SBEncState *st;
149
5.31k
   const SpeexSBMode *mode;
150
151
5.31k
   st = (SBEncState*)speex_alloc(sizeof(SBEncState));
152
5.31k
   if (!st)
153
0
      return NULL;
154
5.31k
   st->mode = m;
155
5.31k
   mode = (const SpeexSBMode*)m->mode;
156
157
158
5.31k
   st->st_low = speex_encoder_init(mode->nb_mode);
159
5.31k
#if defined(VAR_ARRAYS) || defined (USE_ALLOCA)
160
5.31k
   st->stack = NULL;
161
#else
162
   /*st->stack = (char*)speex_alloc_scratch(SB_ENC_STACK);*/
163
   speex_encoder_ctl(st->st_low, SPEEX_GET_STACK, &st->stack);
164
#endif
165
166
5.31k
   st->full_frame_size = 2*mode->frameSize;
167
5.31k
   st->frame_size = mode->frameSize;
168
5.31k
   st->subframeSize = mode->subframeSize;
169
5.31k
   st->nbSubframes = mode->frameSize/mode->subframeSize;
170
5.31k
   st->windowSize = st->frame_size+st->subframeSize;
171
5.31k
   st->lpcSize=mode->lpcSize;
172
173
5.31k
   st->encode_submode = 1;
174
5.31k
   st->submodes=mode->submodes;
175
5.31k
   st->submodeSelect = st->submodeID=mode->defaultSubmode;
176
177
5.31k
   tmp=9;
178
5.31k
   speex_encoder_ctl(st->st_low, SPEEX_SET_QUALITY, &tmp);
179
5.31k
   tmp=1;
180
5.31k
   speex_encoder_ctl(st->st_low, SPEEX_SET_WIDEBAND, &tmp);
181
182
5.31k
   st->lpc_floor = mode->lpc_floor;
183
5.31k
   st->gamma1=mode->gamma1;
184
5.31k
   st->gamma2=mode->gamma2;
185
5.31k
   st->first=1;
186
187
5.31k
   st->high=(spx_word16_t*)speex_alloc((st->windowSize-st->frame_size)*sizeof(spx_word16_t));
188
189
5.31k
   st->h0_mem=(spx_word16_t*)speex_alloc((QMF_ORDER)*sizeof(spx_word16_t));
190
191
5.31k
   st->window= lpc_window;
192
193
5.31k
   st->lagWindow = lag_window;
194
195
5.31k
   st->old_lsp = (spx_lsp_t*)speex_alloc(st->lpcSize*sizeof(spx_lsp_t));
196
5.31k
   st->old_qlsp = (spx_lsp_t*)speex_alloc(st->lpcSize*sizeof(spx_lsp_t));
197
5.31k
   st->interp_qlpc = (spx_coef_t*)speex_alloc(st->lpcSize*sizeof(spx_coef_t));
198
5.31k
   st->pi_gain = (spx_word32_t*)speex_alloc((st->nbSubframes)*sizeof(spx_word32_t));
199
5.31k
   st->exc_rms = (spx_word16_t*)speex_alloc((st->nbSubframes)*sizeof(spx_word16_t));
200
5.31k
   st->innov_rms_save = NULL;
201
202
5.31k
   st->mem_sp = (spx_mem_t*)speex_alloc((st->lpcSize)*sizeof(spx_mem_t));
203
5.31k
   st->mem_sp2 = (spx_mem_t*)speex_alloc((st->lpcSize)*sizeof(spx_mem_t));
204
5.31k
   st->mem_sw = (spx_mem_t*)speex_alloc((st->lpcSize)*sizeof(spx_mem_t));
205
206
47.8k
   for (i=0;i<st->lpcSize;i++)
207
42.5k
      st->old_lsp[i]= DIV32(MULT16_16(QCONST16(3.1415927f, LSP_SHIFT), i+1), st->lpcSize+1);
208
209
5.31k
#ifndef DISABLE_VBR
210
5.31k
   st->vbr_quality = 8;
211
5.31k
   st->vbr_enabled = 0;
212
5.31k
   st->vbr_max = 0;
213
5.31k
   st->vbr_max_high = 20000;  /* We just need a big value here */
214
5.31k
   st->vad_enabled = 0;
215
5.31k
   st->abr_enabled = 0;
216
5.31k
   st->relative_quality=0;
217
5.31k
#endif /* #ifndef DISABLE_VBR */
218
219
5.31k
   st->complexity=2;
220
5.31k
   speex_encoder_ctl(st->st_low, SPEEX_GET_SAMPLING_RATE, &st->sampling_rate);
221
5.31k
   st->sampling_rate*=2;
222
#ifdef ENABLE_VALGRIND
223
   VALGRIND_MAKE_MEM_DEFINED(st, (st->stack-(char*)st));
224
#endif
225
5.31k
   return st;
226
5.31k
}
sb_encoder_init
Line
Count
Source
145
2.65k
{
146
2.65k
   int i;
147
2.65k
   spx_int32_t tmp;
148
2.65k
   SBEncState *st;
149
2.65k
   const SpeexSBMode *mode;
150
151
2.65k
   st = (SBEncState*)speex_alloc(sizeof(SBEncState));
152
2.65k
   if (!st)
153
0
      return NULL;
154
2.65k
   st->mode = m;
155
2.65k
   mode = (const SpeexSBMode*)m->mode;
156
157
158
2.65k
   st->st_low = speex_encoder_init(mode->nb_mode);
159
2.65k
#if defined(VAR_ARRAYS) || defined (USE_ALLOCA)
160
2.65k
   st->stack = NULL;
161
#else
162
   /*st->stack = (char*)speex_alloc_scratch(SB_ENC_STACK);*/
163
   speex_encoder_ctl(st->st_low, SPEEX_GET_STACK, &st->stack);
164
#endif
165
166
2.65k
   st->full_frame_size = 2*mode->frameSize;
167
2.65k
   st->frame_size = mode->frameSize;
168
2.65k
   st->subframeSize = mode->subframeSize;
169
2.65k
   st->nbSubframes = mode->frameSize/mode->subframeSize;
170
2.65k
   st->windowSize = st->frame_size+st->subframeSize;
171
2.65k
   st->lpcSize=mode->lpcSize;
172
173
2.65k
   st->encode_submode = 1;
174
2.65k
   st->submodes=mode->submodes;
175
2.65k
   st->submodeSelect = st->submodeID=mode->defaultSubmode;
176
177
2.65k
   tmp=9;
178
2.65k
   speex_encoder_ctl(st->st_low, SPEEX_SET_QUALITY, &tmp);
179
2.65k
   tmp=1;
180
2.65k
   speex_encoder_ctl(st->st_low, SPEEX_SET_WIDEBAND, &tmp);
181
182
2.65k
   st->lpc_floor = mode->lpc_floor;
183
2.65k
   st->gamma1=mode->gamma1;
184
2.65k
   st->gamma2=mode->gamma2;
185
2.65k
   st->first=1;
186
187
2.65k
   st->high=(spx_word16_t*)speex_alloc((st->windowSize-st->frame_size)*sizeof(spx_word16_t));
188
189
2.65k
   st->h0_mem=(spx_word16_t*)speex_alloc((QMF_ORDER)*sizeof(spx_word16_t));
190
191
2.65k
   st->window= lpc_window;
192
193
2.65k
   st->lagWindow = lag_window;
194
195
2.65k
   st->old_lsp = (spx_lsp_t*)speex_alloc(st->lpcSize*sizeof(spx_lsp_t));
196
2.65k
   st->old_qlsp = (spx_lsp_t*)speex_alloc(st->lpcSize*sizeof(spx_lsp_t));
197
2.65k
   st->interp_qlpc = (spx_coef_t*)speex_alloc(st->lpcSize*sizeof(spx_coef_t));
198
2.65k
   st->pi_gain = (spx_word32_t*)speex_alloc((st->nbSubframes)*sizeof(spx_word32_t));
199
2.65k
   st->exc_rms = (spx_word16_t*)speex_alloc((st->nbSubframes)*sizeof(spx_word16_t));
200
2.65k
   st->innov_rms_save = NULL;
201
202
2.65k
   st->mem_sp = (spx_mem_t*)speex_alloc((st->lpcSize)*sizeof(spx_mem_t));
203
2.65k
   st->mem_sp2 = (spx_mem_t*)speex_alloc((st->lpcSize)*sizeof(spx_mem_t));
204
2.65k
   st->mem_sw = (spx_mem_t*)speex_alloc((st->lpcSize)*sizeof(spx_mem_t));
205
206
23.9k
   for (i=0;i<st->lpcSize;i++)
207
21.2k
      st->old_lsp[i]= DIV32(MULT16_16(QCONST16(3.1415927f, LSP_SHIFT), i+1), st->lpcSize+1);
208
209
2.65k
#ifndef DISABLE_VBR
210
2.65k
   st->vbr_quality = 8;
211
2.65k
   st->vbr_enabled = 0;
212
2.65k
   st->vbr_max = 0;
213
2.65k
   st->vbr_max_high = 20000;  /* We just need a big value here */
214
2.65k
   st->vad_enabled = 0;
215
2.65k
   st->abr_enabled = 0;
216
2.65k
   st->relative_quality=0;
217
2.65k
#endif /* #ifndef DISABLE_VBR */
218
219
2.65k
   st->complexity=2;
220
2.65k
   speex_encoder_ctl(st->st_low, SPEEX_GET_SAMPLING_RATE, &st->sampling_rate);
221
2.65k
   st->sampling_rate*=2;
222
#ifdef ENABLE_VALGRIND
223
   VALGRIND_MAKE_MEM_DEFINED(st, (st->stack-(char*)st));
224
#endif
225
2.65k
   return st;
226
2.65k
}
sb_encoder_init
Line
Count
Source
145
2.65k
{
146
2.65k
   int i;
147
2.65k
   spx_int32_t tmp;
148
2.65k
   SBEncState *st;
149
2.65k
   const SpeexSBMode *mode;
150
151
2.65k
   st = (SBEncState*)speex_alloc(sizeof(SBEncState));
152
2.65k
   if (!st)
153
0
      return NULL;
154
2.65k
   st->mode = m;
155
2.65k
   mode = (const SpeexSBMode*)m->mode;
156
157
158
2.65k
   st->st_low = speex_encoder_init(mode->nb_mode);
159
2.65k
#if defined(VAR_ARRAYS) || defined (USE_ALLOCA)
160
2.65k
   st->stack = NULL;
161
#else
162
   /*st->stack = (char*)speex_alloc_scratch(SB_ENC_STACK);*/
163
   speex_encoder_ctl(st->st_low, SPEEX_GET_STACK, &st->stack);
164
#endif
165
166
2.65k
   st->full_frame_size = 2*mode->frameSize;
167
2.65k
   st->frame_size = mode->frameSize;
168
2.65k
   st->subframeSize = mode->subframeSize;
169
2.65k
   st->nbSubframes = mode->frameSize/mode->subframeSize;
170
2.65k
   st->windowSize = st->frame_size+st->subframeSize;
171
2.65k
   st->lpcSize=mode->lpcSize;
172
173
2.65k
   st->encode_submode = 1;
174
2.65k
   st->submodes=mode->submodes;
175
2.65k
   st->submodeSelect = st->submodeID=mode->defaultSubmode;
176
177
2.65k
   tmp=9;
178
2.65k
   speex_encoder_ctl(st->st_low, SPEEX_SET_QUALITY, &tmp);
179
2.65k
   tmp=1;
180
2.65k
   speex_encoder_ctl(st->st_low, SPEEX_SET_WIDEBAND, &tmp);
181
182
2.65k
   st->lpc_floor = mode->lpc_floor;
183
2.65k
   st->gamma1=mode->gamma1;
184
2.65k
   st->gamma2=mode->gamma2;
185
2.65k
   st->first=1;
186
187
2.65k
   st->high=(spx_word16_t*)speex_alloc((st->windowSize-st->frame_size)*sizeof(spx_word16_t));
188
189
2.65k
   st->h0_mem=(spx_word16_t*)speex_alloc((QMF_ORDER)*sizeof(spx_word16_t));
190
191
2.65k
   st->window= lpc_window;
192
193
2.65k
   st->lagWindow = lag_window;
194
195
2.65k
   st->old_lsp = (spx_lsp_t*)speex_alloc(st->lpcSize*sizeof(spx_lsp_t));
196
2.65k
   st->old_qlsp = (spx_lsp_t*)speex_alloc(st->lpcSize*sizeof(spx_lsp_t));
197
2.65k
   st->interp_qlpc = (spx_coef_t*)speex_alloc(st->lpcSize*sizeof(spx_coef_t));
198
2.65k
   st->pi_gain = (spx_word32_t*)speex_alloc((st->nbSubframes)*sizeof(spx_word32_t));
199
2.65k
   st->exc_rms = (spx_word16_t*)speex_alloc((st->nbSubframes)*sizeof(spx_word16_t));
200
2.65k
   st->innov_rms_save = NULL;
201
202
2.65k
   st->mem_sp = (spx_mem_t*)speex_alloc((st->lpcSize)*sizeof(spx_mem_t));
203
2.65k
   st->mem_sp2 = (spx_mem_t*)speex_alloc((st->lpcSize)*sizeof(spx_mem_t));
204
2.65k
   st->mem_sw = (spx_mem_t*)speex_alloc((st->lpcSize)*sizeof(spx_mem_t));
205
206
23.9k
   for (i=0;i<st->lpcSize;i++)
207
21.2k
      st->old_lsp[i]= DIV32(MULT16_16(QCONST16(3.1415927f, LSP_SHIFT), i+1), st->lpcSize+1);
208
209
2.65k
#ifndef DISABLE_VBR
210
2.65k
   st->vbr_quality = 8;
211
2.65k
   st->vbr_enabled = 0;
212
2.65k
   st->vbr_max = 0;
213
2.65k
   st->vbr_max_high = 20000;  /* We just need a big value here */
214
2.65k
   st->vad_enabled = 0;
215
2.65k
   st->abr_enabled = 0;
216
2.65k
   st->relative_quality=0;
217
2.65k
#endif /* #ifndef DISABLE_VBR */
218
219
2.65k
   st->complexity=2;
220
2.65k
   speex_encoder_ctl(st->st_low, SPEEX_GET_SAMPLING_RATE, &st->sampling_rate);
221
2.65k
   st->sampling_rate*=2;
222
#ifdef ENABLE_VALGRIND
223
   VALGRIND_MAKE_MEM_DEFINED(st, (st->stack-(char*)st));
224
#endif
225
2.65k
   return st;
226
2.65k
}
227
228
void sb_encoder_destroy(void *state)
229
2.65k
{
230
2.65k
   SBEncState *st=(SBEncState*)state;
231
232
2.65k
   speex_encoder_destroy(st->st_low);
233
#if !(defined(VAR_ARRAYS) || defined (USE_ALLOCA))
234
   /*speex_free_scratch(st->stack);*/
235
#endif
236
237
2.65k
   speex_free(st->high);
238
239
2.65k
   speex_free(st->h0_mem);
240
241
2.65k
   speex_free(st->old_lsp);
242
2.65k
   speex_free(st->old_qlsp);
243
2.65k
   speex_free(st->interp_qlpc);
244
2.65k
   speex_free(st->pi_gain);
245
2.65k
   speex_free(st->exc_rms);
246
247
2.65k
   speex_free(st->mem_sp);
248
2.65k
   speex_free(st->mem_sp2);
249
2.65k
   speex_free(st->mem_sw);
250
251
252
2.65k
   speex_free(st);
253
2.65k
}
254
255
int sb_encoder_ctl(void *state, int request, void *ptr)
256
216k
{
257
216k
   SBEncState *st;
258
216k
   st=(SBEncState*)state;
259
216k
   switch(request)
260
216k
   {
261
9.47k
   case SPEEX_GET_FRAME_SIZE:
262
9.47k
      (*(spx_int32_t*)ptr) = st->full_frame_size;
263
9.47k
      break;
264
10.9k
   case SPEEX_SET_HIGH_MODE:
265
10.9k
      st->submodeSelect = st->submodeID = (*(spx_int32_t*)ptr);
266
10.9k
      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
2.03k
   case SPEEX_SET_DTX:
271
2.03k
      speex_encoder_ctl(st->st_low, SPEEX_SET_DTX, ptr);
272
2.03k
      break;
273
0
   case SPEEX_GET_DTX:
274
0
      speex_encoder_ctl(st->st_low, SPEEX_GET_DTX, ptr);
275
0
      break;
276
4.75k
   case SPEEX_GET_LOW_MODE:
277
4.75k
      speex_encoder_ctl(st->st_low, SPEEX_GET_LOW_MODE, ptr);
278
4.75k
      break;
279
14.4k
   case SPEEX_SET_MODE:
280
14.4k
      speex_encoder_ctl(st, SPEEX_SET_QUALITY, ptr);
281
14.4k
      break;
282
0
#ifndef DISABLE_VBR
283
2.56k
   case SPEEX_SET_VBR:
284
2.56k
      st->vbr_enabled = (*(spx_int32_t*)ptr);
285
2.56k
      speex_encoder_ctl(st->st_low, SPEEX_SET_VBR, ptr);
286
2.56k
      break;
287
0
   case SPEEX_GET_VBR:
288
0
      (*(spx_int32_t*)ptr) = st->vbr_enabled;
289
0
      break;
290
2.08k
   case SPEEX_SET_VAD:
291
2.08k
      st->vad_enabled = (*(spx_int32_t*)ptr);
292
2.08k
      speex_encoder_ctl(st->st_low, SPEEX_SET_VAD, ptr);
293
2.08k
      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
3.81k
   case SPEEX_SET_VBR_QUALITY:
300
3.81k
      {
301
3.81k
         spx_int32_t q;
302
3.81k
         float qual = (*(float*)ptr)+.6;
303
3.81k
         st->vbr_quality = (*(float*)ptr);
304
3.81k
         if (qual>10)
305
254
            qual=10;
306
3.81k
         q=(int)floor(.5+*(float*)ptr);
307
3.81k
         if (q>10)
308
0
            q=10;
309
3.81k
         speex_encoder_ctl(st->st_low, SPEEX_SET_VBR_QUALITY, &qual);
310
3.81k
         speex_encoder_ctl(state, SPEEX_SET_QUALITY, &q);
311
3.81k
         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
1.24k
   case SPEEX_SET_ABR:
319
1.24k
      st->abr_enabled = (*(spx_int32_t*)ptr);
320
1.24k
      st->vbr_enabled = st->abr_enabled!=0;
321
1.24k
      speex_encoder_ctl(st->st_low, SPEEX_SET_VBR, &st->vbr_enabled);
322
1.24k
      if (st->vbr_enabled)
323
1.24k
      {
324
1.24k
         spx_int32_t i=10, rate, target;
325
1.24k
         float vbr_qual;
326
1.24k
         target = (*(spx_int32_t*)ptr);
327
14.9k
         while (i>=0)
328
13.7k
         {
329
13.7k
            speex_encoder_ctl(st, SPEEX_SET_QUALITY, &i);
330
13.7k
            speex_encoder_ctl(st, SPEEX_GET_BITRATE, &rate);
331
13.7k
            if (rate <= target)
332
0
               break;
333
13.7k
            i--;
334
13.7k
         }
335
1.24k
         vbr_qual=i;
336
1.24k
         if (vbr_qual<0)
337
1.24k
            vbr_qual=0;
338
1.24k
         speex_encoder_ctl(st, SPEEX_SET_VBR_QUALITY, &vbr_qual);
339
1.24k
         st->abr_count=0;
340
1.24k
         st->abr_drift=0;
341
1.24k
         st->abr_drift2=0;
342
1.24k
      }
343
344
1.24k
      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
60.6k
   case SPEEX_SET_QUALITY:
351
60.6k
      {
352
60.6k
         spx_int32_t nb_qual;
353
60.6k
         int quality = (*(spx_int32_t*)ptr);
354
60.6k
         if (quality < 0)
355
0
            quality = 0;
356
60.6k
         if (quality > 10)
357
0
            quality = 10;
358
60.6k
         st->submodeSelect = st->submodeID = ((const SpeexSBMode*)(st->mode->mode))->quality_map[quality];
359
60.6k
         nb_qual = ((const SpeexSBMode*)(st->mode->mode))->low_quality_map[quality];
360
60.6k
         speex_encoder_ctl(st->st_low, SPEEX_SET_MODE, &nb_qual);
361
60.6k
      }
362
60.6k
      break;
363
5.31k
   case SPEEX_SET_COMPLEXITY:
364
5.31k
      speex_encoder_ctl(st->st_low, SPEEX_SET_COMPLEXITY, ptr);
365
5.31k
      st->complexity = (*(spx_int32_t*)ptr);
366
5.31k
      if (st->complexity<1)
367
912
         st->complexity=1;
368
5.31k
      break;
369
0
   case SPEEX_GET_COMPLEXITY:
370
0
      (*(spx_int32_t*)ptr) = st->complexity;
371
0
      break;
372
3.73k
   case SPEEX_SET_BITRATE:
373
3.73k
      {
374
3.73k
         spx_int32_t i=10;
375
3.73k
         spx_int32_t rate, target;
376
3.73k
         target = (*(spx_int32_t*)ptr);
377
25.7k
         while (i>=0)
378
24.8k
         {
379
24.8k
            speex_encoder_ctl(st, SPEEX_SET_QUALITY, &i);
380
24.8k
            speex_encoder_ctl(st, SPEEX_GET_BITRATE, &rate);
381
24.8k
            if (rate <= target)
382
2.83k
               break;
383
22.0k
            i--;
384
22.0k
         }
385
3.73k
      }
386
3.73k
      break;
387
57.0k
   case SPEEX_GET_BITRATE:
388
57.0k
      speex_encoder_ctl(st->st_low, request, ptr);
389
      /*fprintf (stderr, "before: %d\n", (*(int*)ptr));*/
390
57.0k
      if (st->submodes[st->submodeID])
391
56.0k
         (*(spx_int32_t*)ptr) += st->sampling_rate*SUBMODE(bits_per_frame)/st->full_frame_size;
392
1.00k
      else
393
1.00k
         (*(spx_int32_t*)ptr) += st->sampling_rate*(SB_SUBMODE_BITS+1)/st->full_frame_size;
394
      /*fprintf (stderr, "after: %d\n", (*(int*)ptr));*/
395
57.0k
      break;
396
5.31k
   case SPEEX_SET_SAMPLING_RATE:
397
5.31k
      {
398
5.31k
         spx_int32_t tmp=(*(spx_int32_t*)ptr);
399
5.31k
         st->sampling_rate = tmp;
400
5.31k
         tmp>>=1;
401
5.31k
         speex_encoder_ctl(st->st_low, SPEEX_SET_SAMPLING_RATE, &tmp);
402
5.31k
      }
403
5.31k
      break;
404
1.58k
   case SPEEX_GET_SAMPLING_RATE:
405
1.58k
      (*(spx_int32_t*)ptr)=st->sampling_rate;
406
1.58k
      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
5.31k
   case SPEEX_GET_LOOKAHEAD:
427
5.31k
      speex_encoder_ctl(st->st_low, SPEEX_GET_LOOKAHEAD, ptr);
428
5.31k
      (*(spx_int32_t*)ptr) = 2*(*(spx_int32_t*)ptr) + QMF_ORDER - 1;
429
5.31k
      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
2.21k
   case SPEEX_SET_VBR_MAX_BITRATE:
438
2.21k
      {
439
2.21k
         st->vbr_max = (*(spx_int32_t*)ptr);
440
2.21k
         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
2.21k
         } else {
445
2.21k
            spx_int32_t low_rate;
446
2.21k
            if (st->vbr_max >= 42200)
447
0
            {
448
0
               st->vbr_max_high = 17600;
449
2.21k
            } else if (st->vbr_max >= 27800)
450
0
            {
451
0
               st->vbr_max_high = 9600;
452
2.21k
            } else if (st->vbr_max > 20600)
453
0
            {
454
0
               st->vbr_max_high = 5600;
455
2.21k
            } else {
456
2.21k
               st->vbr_max_high = 1800;
457
2.21k
            }
458
2.21k
            if (st->subframeSize==80)
459
698
               st->vbr_max_high = 1800;
460
2.21k
            low_rate = st->vbr_max - st->vbr_max_high;
461
2.21k
            speex_encoder_ctl(st->st_low, SPEEX_SET_VBR_MAX_BITRATE, &low_rate);
462
2.21k
         }
463
2.21k
      }
464
2.21k
      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
5.31k
   case SPEEX_SET_HIGHPASS:
470
5.31k
      speex_encoder_ctl(st->st_low, SPEEX_SET_HIGHPASS, ptr);
471
5.31k
      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
4.75k
   case SPEEX_GET_PI_GAIN:
479
4.75k
      {
480
4.75k
         int i;
481
4.75k
         spx_word32_t *g = (spx_word32_t*)ptr;
482
23.7k
         for (i=0;i<st->nbSubframes;i++)
483
19.0k
            g[i]=st->pi_gain[i];
484
4.75k
      }
485
4.75k
      break;
486
4.75k
   case SPEEX_GET_EXC:
487
4.75k
      {
488
4.75k
         int i;
489
23.7k
         for (i=0;i<st->nbSubframes;i++)
490
19.0k
            ((spx_word16_t*)ptr)[i] = st->exc_rms[i];
491
4.75k
      }
492
4.75k
      break;
493
0
#ifndef DISABLE_VBR
494
3.06k
   case SPEEX_GET_RELATIVE_QUALITY:
495
3.06k
      (*(float*)ptr)=st->relative_quality;
496
3.06k
      break;
497
0
#endif /* #ifndef DISABLE_VBR */
498
4.75k
   case SPEEX_SET_INNOVATION_SAVE:
499
4.75k
      st->innov_rms_save = (spx_word16_t*)ptr;
500
4.75k
      break;
501
1.58k
   case SPEEX_SET_WIDEBAND:
502
1.58k
      speex_encoder_ctl(st->st_low, SPEEX_SET_WIDEBAND, ptr);
503
1.58k
      break;
504
0
   case SPEEX_GET_STACK:
505
0
      *((char**)ptr) = st->stack;
506
0
      break;
507
0
   default:
508
0
      speex_warning_int("Unknown nb_ctl request: ", request);
509
0
      return -1;
510
216k
   }
511
216k
   return 0;
512
216k
}
sb_encoder_ctl
Line
Count
Source
256
108k
{
257
108k
   SBEncState *st;
258
108k
   st=(SBEncState*)state;
259
108k
   switch(request)
260
108k
   {
261
4.73k
   case SPEEX_GET_FRAME_SIZE:
262
4.73k
      (*(spx_int32_t*)ptr) = st->full_frame_size;
263
4.73k
      break;
264
5.45k
   case SPEEX_SET_HIGH_MODE:
265
5.45k
      st->submodeSelect = st->submodeID = (*(spx_int32_t*)ptr);
266
5.45k
      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
1.01k
   case SPEEX_SET_DTX:
271
1.01k
      speex_encoder_ctl(st->st_low, SPEEX_SET_DTX, ptr);
272
1.01k
      break;
273
0
   case SPEEX_GET_DTX:
274
0
      speex_encoder_ctl(st->st_low, SPEEX_GET_DTX, ptr);
275
0
      break;
276
2.37k
   case SPEEX_GET_LOW_MODE:
277
2.37k
      speex_encoder_ctl(st->st_low, SPEEX_GET_LOW_MODE, ptr);
278
2.37k
      break;
279
7.24k
   case SPEEX_SET_MODE:
280
7.24k
      speex_encoder_ctl(st, SPEEX_SET_QUALITY, ptr);
281
7.24k
      break;
282
0
#ifndef DISABLE_VBR
283
1.28k
   case SPEEX_SET_VBR:
284
1.28k
      st->vbr_enabled = (*(spx_int32_t*)ptr);
285
1.28k
      speex_encoder_ctl(st->st_low, SPEEX_SET_VBR, ptr);
286
1.28k
      break;
287
0
   case SPEEX_GET_VBR:
288
0
      (*(spx_int32_t*)ptr) = st->vbr_enabled;
289
0
      break;
290
1.04k
   case SPEEX_SET_VAD:
291
1.04k
      st->vad_enabled = (*(spx_int32_t*)ptr);
292
1.04k
      speex_encoder_ctl(st->st_low, SPEEX_SET_VAD, ptr);
293
1.04k
      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
1.90k
   case SPEEX_SET_VBR_QUALITY:
300
1.90k
      {
301
1.90k
         spx_int32_t q;
302
1.90k
         float qual = (*(float*)ptr)+.6;
303
1.90k
         st->vbr_quality = (*(float*)ptr);
304
1.90k
         if (qual>10)
305
127
            qual=10;
306
1.90k
         q=(int)floor(.5+*(float*)ptr);
307
1.90k
         if (q>10)
308
0
            q=10;
309
1.90k
         speex_encoder_ctl(st->st_low, SPEEX_SET_VBR_QUALITY, &qual);
310
1.90k
         speex_encoder_ctl(state, SPEEX_SET_QUALITY, &q);
311
1.90k
         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
623
   case SPEEX_SET_ABR:
319
623
      st->abr_enabled = (*(spx_int32_t*)ptr);
320
623
      st->vbr_enabled = st->abr_enabled!=0;
321
623
      speex_encoder_ctl(st->st_low, SPEEX_SET_VBR, &st->vbr_enabled);
322
623
      if (st->vbr_enabled)
323
623
      {
324
623
         spx_int32_t i=10, rate, target;
325
623
         float vbr_qual;
326
623
         target = (*(spx_int32_t*)ptr);
327
7.47k
         while (i>=0)
328
6.85k
         {
329
6.85k
            speex_encoder_ctl(st, SPEEX_SET_QUALITY, &i);
330
6.85k
            speex_encoder_ctl(st, SPEEX_GET_BITRATE, &rate);
331
6.85k
            if (rate <= target)
332
0
               break;
333
6.85k
            i--;
334
6.85k
         }
335
623
         vbr_qual=i;
336
623
         if (vbr_qual<0)
337
623
            vbr_qual=0;
338
623
         speex_encoder_ctl(st, SPEEX_SET_VBR_QUALITY, &vbr_qual);
339
623
         st->abr_count=0;
340
623
         st->abr_drift=0;
341
623
         st->abr_drift2=0;
342
623
      }
343
344
623
      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
30.3k
   case SPEEX_SET_QUALITY:
351
30.3k
      {
352
30.3k
         spx_int32_t nb_qual;
353
30.3k
         int quality = (*(spx_int32_t*)ptr);
354
30.3k
         if (quality < 0)
355
0
            quality = 0;
356
30.3k
         if (quality > 10)
357
0
            quality = 10;
358
30.3k
         st->submodeSelect = st->submodeID = ((const SpeexSBMode*)(st->mode->mode))->quality_map[quality];
359
30.3k
         nb_qual = ((const SpeexSBMode*)(st->mode->mode))->low_quality_map[quality];
360
30.3k
         speex_encoder_ctl(st->st_low, SPEEX_SET_MODE, &nb_qual);
361
30.3k
      }
362
30.3k
      break;
363
2.65k
   case SPEEX_SET_COMPLEXITY:
364
2.65k
      speex_encoder_ctl(st->st_low, SPEEX_SET_COMPLEXITY, ptr);
365
2.65k
      st->complexity = (*(spx_int32_t*)ptr);
366
2.65k
      if (st->complexity<1)
367
456
         st->complexity=1;
368
2.65k
      break;
369
0
   case SPEEX_GET_COMPLEXITY:
370
0
      (*(spx_int32_t*)ptr) = st->complexity;
371
0
      break;
372
1.86k
   case SPEEX_SET_BITRATE:
373
1.86k
      {
374
1.86k
         spx_int32_t i=10;
375
1.86k
         spx_int32_t rate, target;
376
1.86k
         target = (*(spx_int32_t*)ptr);
377
12.8k
         while (i>=0)
378
12.4k
         {
379
12.4k
            speex_encoder_ctl(st, SPEEX_SET_QUALITY, &i);
380
12.4k
            speex_encoder_ctl(st, SPEEX_GET_BITRATE, &rate);
381
12.4k
            if (rate <= target)
382
1.41k
               break;
383
11.0k
            i--;
384
11.0k
         }
385
1.86k
      }
386
1.86k
      break;
387
28.5k
   case SPEEX_GET_BITRATE:
388
28.5k
      speex_encoder_ctl(st->st_low, request, ptr);
389
      /*fprintf (stderr, "before: %d\n", (*(int*)ptr));*/
390
28.5k
      if (st->submodes[st->submodeID])
391
28.0k
         (*(spx_int32_t*)ptr) += st->sampling_rate*SUBMODE(bits_per_frame)/st->full_frame_size;
392
502
      else
393
502
         (*(spx_int32_t*)ptr) += st->sampling_rate*(SB_SUBMODE_BITS+1)/st->full_frame_size;
394
      /*fprintf (stderr, "after: %d\n", (*(int*)ptr));*/
395
28.5k
      break;
396
2.65k
   case SPEEX_SET_SAMPLING_RATE:
397
2.65k
      {
398
2.65k
         spx_int32_t tmp=(*(spx_int32_t*)ptr);
399
2.65k
         st->sampling_rate = tmp;
400
2.65k
         tmp>>=1;
401
2.65k
         speex_encoder_ctl(st->st_low, SPEEX_SET_SAMPLING_RATE, &tmp);
402
2.65k
      }
403
2.65k
      break;
404
792
   case SPEEX_GET_SAMPLING_RATE:
405
792
      (*(spx_int32_t*)ptr)=st->sampling_rate;
406
792
      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
2.65k
   case SPEEX_GET_LOOKAHEAD:
427
2.65k
      speex_encoder_ctl(st->st_low, SPEEX_GET_LOOKAHEAD, ptr);
428
2.65k
      (*(spx_int32_t*)ptr) = 2*(*(spx_int32_t*)ptr) + QMF_ORDER - 1;
429
2.65k
      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
1.10k
   case SPEEX_SET_VBR_MAX_BITRATE:
438
1.10k
      {
439
1.10k
         st->vbr_max = (*(spx_int32_t*)ptr);
440
1.10k
         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
1.10k
         } else {
445
1.10k
            spx_int32_t low_rate;
446
1.10k
            if (st->vbr_max >= 42200)
447
0
            {
448
0
               st->vbr_max_high = 17600;
449
1.10k
            } else if (st->vbr_max >= 27800)
450
0
            {
451
0
               st->vbr_max_high = 9600;
452
1.10k
            } else if (st->vbr_max > 20600)
453
0
            {
454
0
               st->vbr_max_high = 5600;
455
1.10k
            } else {
456
1.10k
               st->vbr_max_high = 1800;
457
1.10k
            }
458
1.10k
            if (st->subframeSize==80)
459
349
               st->vbr_max_high = 1800;
460
1.10k
            low_rate = st->vbr_max - st->vbr_max_high;
461
1.10k
            speex_encoder_ctl(st->st_low, SPEEX_SET_VBR_MAX_BITRATE, &low_rate);
462
1.10k
         }
463
1.10k
      }
464
1.10k
      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
2.65k
   case SPEEX_SET_HIGHPASS:
470
2.65k
      speex_encoder_ctl(st->st_low, SPEEX_SET_HIGHPASS, ptr);
471
2.65k
      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
2.37k
   case SPEEX_GET_PI_GAIN:
479
2.37k
      {
480
2.37k
         int i;
481
2.37k
         spx_word32_t *g = (spx_word32_t*)ptr;
482
11.8k
         for (i=0;i<st->nbSubframes;i++)
483
9.50k
            g[i]=st->pi_gain[i];
484
2.37k
      }
485
2.37k
      break;
486
2.37k
   case SPEEX_GET_EXC:
487
2.37k
      {
488
2.37k
         int i;
489
11.8k
         for (i=0;i<st->nbSubframes;i++)
490
9.50k
            ((spx_word16_t*)ptr)[i] = st->exc_rms[i];
491
2.37k
      }
492
2.37k
      break;
493
0
#ifndef DISABLE_VBR
494
1.53k
   case SPEEX_GET_RELATIVE_QUALITY:
495
1.53k
      (*(float*)ptr)=st->relative_quality;
496
1.53k
      break;
497
0
#endif /* #ifndef DISABLE_VBR */
498
2.37k
   case SPEEX_SET_INNOVATION_SAVE:
499
2.37k
      st->innov_rms_save = (spx_word16_t*)ptr;
500
2.37k
      break;
501
792
   case SPEEX_SET_WIDEBAND:
502
792
      speex_encoder_ctl(st->st_low, SPEEX_SET_WIDEBAND, ptr);
503
792
      break;
504
0
   case SPEEX_GET_STACK:
505
0
      *((char**)ptr) = st->stack;
506
0
      break;
507
0
   default:
508
0
      speex_warning_int("Unknown nb_ctl request: ", request);
509
0
      return -1;
510
108k
   }
511
108k
   return 0;
512
108k
}
sb_encoder_ctl
Line
Count
Source
256
108k
{
257
108k
   SBEncState *st;
258
108k
   st=(SBEncState*)state;
259
108k
   switch(request)
260
108k
   {
261
4.73k
   case SPEEX_GET_FRAME_SIZE:
262
4.73k
      (*(spx_int32_t*)ptr) = st->full_frame_size;
263
4.73k
      break;
264
5.45k
   case SPEEX_SET_HIGH_MODE:
265
5.45k
      st->submodeSelect = st->submodeID = (*(spx_int32_t*)ptr);
266
5.45k
      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
1.01k
   case SPEEX_SET_DTX:
271
1.01k
      speex_encoder_ctl(st->st_low, SPEEX_SET_DTX, ptr);
272
1.01k
      break;
273
0
   case SPEEX_GET_DTX:
274
0
      speex_encoder_ctl(st->st_low, SPEEX_GET_DTX, ptr);
275
0
      break;
276
2.37k
   case SPEEX_GET_LOW_MODE:
277
2.37k
      speex_encoder_ctl(st->st_low, SPEEX_GET_LOW_MODE, ptr);
278
2.37k
      break;
279
7.24k
   case SPEEX_SET_MODE:
280
7.24k
      speex_encoder_ctl(st, SPEEX_SET_QUALITY, ptr);
281
7.24k
      break;
282
0
#ifndef DISABLE_VBR
283
1.28k
   case SPEEX_SET_VBR:
284
1.28k
      st->vbr_enabled = (*(spx_int32_t*)ptr);
285
1.28k
      speex_encoder_ctl(st->st_low, SPEEX_SET_VBR, ptr);
286
1.28k
      break;
287
0
   case SPEEX_GET_VBR:
288
0
      (*(spx_int32_t*)ptr) = st->vbr_enabled;
289
0
      break;
290
1.04k
   case SPEEX_SET_VAD:
291
1.04k
      st->vad_enabled = (*(spx_int32_t*)ptr);
292
1.04k
      speex_encoder_ctl(st->st_low, SPEEX_SET_VAD, ptr);
293
1.04k
      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
1.90k
   case SPEEX_SET_VBR_QUALITY:
300
1.90k
      {
301
1.90k
         spx_int32_t q;
302
1.90k
         float qual = (*(float*)ptr)+.6;
303
1.90k
         st->vbr_quality = (*(float*)ptr);
304
1.90k
         if (qual>10)
305
127
            qual=10;
306
1.90k
         q=(int)floor(.5+*(float*)ptr);
307
1.90k
         if (q>10)
308
0
            q=10;
309
1.90k
         speex_encoder_ctl(st->st_low, SPEEX_SET_VBR_QUALITY, &qual);
310
1.90k
         speex_encoder_ctl(state, SPEEX_SET_QUALITY, &q);
311
1.90k
         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
623
   case SPEEX_SET_ABR:
319
623
      st->abr_enabled = (*(spx_int32_t*)ptr);
320
623
      st->vbr_enabled = st->abr_enabled!=0;
321
623
      speex_encoder_ctl(st->st_low, SPEEX_SET_VBR, &st->vbr_enabled);
322
623
      if (st->vbr_enabled)
323
623
      {
324
623
         spx_int32_t i=10, rate, target;
325
623
         float vbr_qual;
326
623
         target = (*(spx_int32_t*)ptr);
327
7.47k
         while (i>=0)
328
6.85k
         {
329
6.85k
            speex_encoder_ctl(st, SPEEX_SET_QUALITY, &i);
330
6.85k
            speex_encoder_ctl(st, SPEEX_GET_BITRATE, &rate);
331
6.85k
            if (rate <= target)
332
0
               break;
333
6.85k
            i--;
334
6.85k
         }
335
623
         vbr_qual=i;
336
623
         if (vbr_qual<0)
337
623
            vbr_qual=0;
338
623
         speex_encoder_ctl(st, SPEEX_SET_VBR_QUALITY, &vbr_qual);
339
623
         st->abr_count=0;
340
623
         st->abr_drift=0;
341
623
         st->abr_drift2=0;
342
623
      }
343
344
623
      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
30.3k
   case SPEEX_SET_QUALITY:
351
30.3k
      {
352
30.3k
         spx_int32_t nb_qual;
353
30.3k
         int quality = (*(spx_int32_t*)ptr);
354
30.3k
         if (quality < 0)
355
0
            quality = 0;
356
30.3k
         if (quality > 10)
357
0
            quality = 10;
358
30.3k
         st->submodeSelect = st->submodeID = ((const SpeexSBMode*)(st->mode->mode))->quality_map[quality];
359
30.3k
         nb_qual = ((const SpeexSBMode*)(st->mode->mode))->low_quality_map[quality];
360
30.3k
         speex_encoder_ctl(st->st_low, SPEEX_SET_MODE, &nb_qual);
361
30.3k
      }
362
30.3k
      break;
363
2.65k
   case SPEEX_SET_COMPLEXITY:
364
2.65k
      speex_encoder_ctl(st->st_low, SPEEX_SET_COMPLEXITY, ptr);
365
2.65k
      st->complexity = (*(spx_int32_t*)ptr);
366
2.65k
      if (st->complexity<1)
367
456
         st->complexity=1;
368
2.65k
      break;
369
0
   case SPEEX_GET_COMPLEXITY:
370
0
      (*(spx_int32_t*)ptr) = st->complexity;
371
0
      break;
372
1.86k
   case SPEEX_SET_BITRATE:
373
1.86k
      {
374
1.86k
         spx_int32_t i=10;
375
1.86k
         spx_int32_t rate, target;
376
1.86k
         target = (*(spx_int32_t*)ptr);
377
12.8k
         while (i>=0)
378
12.4k
         {
379
12.4k
            speex_encoder_ctl(st, SPEEX_SET_QUALITY, &i);
380
12.4k
            speex_encoder_ctl(st, SPEEX_GET_BITRATE, &rate);
381
12.4k
            if (rate <= target)
382
1.41k
               break;
383
11.0k
            i--;
384
11.0k
         }
385
1.86k
      }
386
1.86k
      break;
387
28.5k
   case SPEEX_GET_BITRATE:
388
28.5k
      speex_encoder_ctl(st->st_low, request, ptr);
389
      /*fprintf (stderr, "before: %d\n", (*(int*)ptr));*/
390
28.5k
      if (st->submodes[st->submodeID])
391
28.0k
         (*(spx_int32_t*)ptr) += st->sampling_rate*SUBMODE(bits_per_frame)/st->full_frame_size;
392
502
      else
393
502
         (*(spx_int32_t*)ptr) += st->sampling_rate*(SB_SUBMODE_BITS+1)/st->full_frame_size;
394
      /*fprintf (stderr, "after: %d\n", (*(int*)ptr));*/
395
28.5k
      break;
396
2.65k
   case SPEEX_SET_SAMPLING_RATE:
397
2.65k
      {
398
2.65k
         spx_int32_t tmp=(*(spx_int32_t*)ptr);
399
2.65k
         st->sampling_rate = tmp;
400
2.65k
         tmp>>=1;
401
2.65k
         speex_encoder_ctl(st->st_low, SPEEX_SET_SAMPLING_RATE, &tmp);
402
2.65k
      }
403
2.65k
      break;
404
792
   case SPEEX_GET_SAMPLING_RATE:
405
792
      (*(spx_int32_t*)ptr)=st->sampling_rate;
406
792
      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
2.65k
   case SPEEX_GET_LOOKAHEAD:
427
2.65k
      speex_encoder_ctl(st->st_low, SPEEX_GET_LOOKAHEAD, ptr);
428
2.65k
      (*(spx_int32_t*)ptr) = 2*(*(spx_int32_t*)ptr) + QMF_ORDER - 1;
429
2.65k
      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
1.10k
   case SPEEX_SET_VBR_MAX_BITRATE:
438
1.10k
      {
439
1.10k
         st->vbr_max = (*(spx_int32_t*)ptr);
440
1.10k
         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
1.10k
         } else {
445
1.10k
            spx_int32_t low_rate;
446
1.10k
            if (st->vbr_max >= 42200)
447
0
            {
448
0
               st->vbr_max_high = 17600;
449
1.10k
            } else if (st->vbr_max >= 27800)
450
0
            {
451
0
               st->vbr_max_high = 9600;
452
1.10k
            } else if (st->vbr_max > 20600)
453
0
            {
454
0
               st->vbr_max_high = 5600;
455
1.10k
            } else {
456
1.10k
               st->vbr_max_high = 1800;
457
1.10k
            }
458
1.10k
            if (st->subframeSize==80)
459
349
               st->vbr_max_high = 1800;
460
1.10k
            low_rate = st->vbr_max - st->vbr_max_high;
461
1.10k
            speex_encoder_ctl(st->st_low, SPEEX_SET_VBR_MAX_BITRATE, &low_rate);
462
1.10k
         }
463
1.10k
      }
464
1.10k
      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
2.65k
   case SPEEX_SET_HIGHPASS:
470
2.65k
      speex_encoder_ctl(st->st_low, SPEEX_SET_HIGHPASS, ptr);
471
2.65k
      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
2.37k
   case SPEEX_GET_PI_GAIN:
479
2.37k
      {
480
2.37k
         int i;
481
2.37k
         spx_word32_t *g = (spx_word32_t*)ptr;
482
11.8k
         for (i=0;i<st->nbSubframes;i++)
483
9.50k
            g[i]=st->pi_gain[i];
484
2.37k
      }
485
2.37k
      break;
486
2.37k
   case SPEEX_GET_EXC:
487
2.37k
      {
488
2.37k
         int i;
489
11.8k
         for (i=0;i<st->nbSubframes;i++)
490
9.50k
            ((spx_word16_t*)ptr)[i] = st->exc_rms[i];
491
2.37k
      }
492
2.37k
      break;
493
0
#ifndef DISABLE_VBR
494
1.53k
   case SPEEX_GET_RELATIVE_QUALITY:
495
1.53k
      (*(float*)ptr)=st->relative_quality;
496
1.53k
      break;
497
0
#endif /* #ifndef DISABLE_VBR */
498
2.37k
   case SPEEX_SET_INNOVATION_SAVE:
499
2.37k
      st->innov_rms_save = (spx_word16_t*)ptr;
500
2.37k
      break;
501
792
   case SPEEX_SET_WIDEBAND:
502
792
      speex_encoder_ctl(st->st_low, SPEEX_SET_WIDEBAND, ptr);
503
792
      break;
504
0
   case SPEEX_GET_STACK:
505
0
      *((char**)ptr) = st->stack;
506
0
      break;
507
0
   default:
508
0
      speex_warning_int("Unknown nb_ctl request: ", request);
509
0
      return -1;
510
108k
   }
511
108k
   return 0;
512
108k
}
513
514
515
int sb_encode(void *state, void *vin, SpeexBits *bits)
516
10.1k
{
517
10.1k
   SBEncState *st;
518
10.1k
   int i, roots, sub;
519
10.1k
   char *stack;
520
10.1k
   VARDECL(spx_mem_t *mem);
521
10.1k
   VARDECL(spx_sig_t *innov);
522
10.1k
   VARDECL(spx_word16_t *target);
523
10.1k
   VARDECL(spx_word16_t *syn_resp);
524
10.1k
   VARDECL(spx_word32_t *low_pi_gain);
525
10.1k
   spx_word16_t *low;
526
10.1k
   spx_word16_t *high;
527
10.1k
   VARDECL(spx_word16_t *low_exc_rms);
528
10.1k
   VARDECL(spx_word16_t *low_innov_rms);
529
10.1k
   const SpeexSBMode *mode;
530
10.1k
   spx_int32_t dtx;
531
10.1k
   spx_word16_t *in = (spx_word16_t*)vin;
532
10.1k
   spx_word16_t e_low=0, e_high=0;
533
10.1k
   VARDECL(spx_coef_t *lpc);
534
10.1k
   VARDECL(spx_coef_t *interp_lpc);
535
10.1k
   VARDECL(spx_coef_t *bw_lpc1);
536
10.1k
   VARDECL(spx_coef_t *bw_lpc2);
537
10.1k
   VARDECL(spx_lsp_t *lsp);
538
10.1k
   VARDECL(spx_lsp_t *qlsp);
539
10.1k
   VARDECL(spx_lsp_t *interp_lsp);
540
10.1k
   VARDECL(spx_lsp_t *interp_qlsp);
541
542
10.1k
   st = (SBEncState*)state;
543
10.1k
   stack=st->stack;
544
10.1k
   mode = (const SpeexSBMode*)(st->mode->mode);
545
10.1k
   low = in;
546
10.1k
   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
10.1k
   qmf_decomp(in, h0, low, high, st->full_frame_size, QMF_ORDER, st->h0_mem, stack);
551
552
10.1k
#ifndef DISABLE_VBR
553
10.1k
   if (st->vbr_enabled || st->vad_enabled)
554
7.39k
   {
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
7.39k
      e_low = compute_rms16(low, st->frame_size);
558
7.39k
      e_high = compute_rms16(high, st->frame_size);
559
7.39k
   }
560
10.1k
#endif /* #ifndef DISABLE_VBR */
561
562
10.1k
   ALLOC(low_innov_rms, st->nbSubframes, spx_word16_t);
563
10.1k
   speex_encoder_ctl(st->st_low, SPEEX_SET_INNOVATION_SAVE, low_innov_rms);
564
   /* Encode the narrowband part*/
565
10.1k
   speex_encode_native(st->st_low, low, bits);
566
567
10.1k
   high = high - (st->windowSize-st->frame_size);
568
10.1k
   SPEEX_COPY(high, st->high, st->windowSize-st->frame_size);
569
10.1k
   SPEEX_COPY(st->high, &high[st->frame_size], st->windowSize-st->frame_size);
570
571
572
10.1k
   ALLOC(low_pi_gain, st->nbSubframes, spx_word32_t);
573
10.1k
   ALLOC(low_exc_rms, st->nbSubframes, spx_word16_t);
574
10.1k
   speex_encoder_ctl(st->st_low, SPEEX_GET_PI_GAIN, low_pi_gain);
575
10.1k
   speex_encoder_ctl(st->st_low, SPEEX_GET_EXC, low_exc_rms);
576
577
10.1k
   speex_encoder_ctl(st->st_low, SPEEX_GET_LOW_MODE, &dtx);
578
579
10.1k
   if (dtx==0)
580
534
      dtx=1;
581
9.59k
   else
582
9.59k
      dtx=0;
583
584
10.1k
   ALLOC(lpc, st->lpcSize, spx_coef_t);
585
10.1k
   ALLOC(interp_lpc, st->lpcSize, spx_coef_t);
586
10.1k
   ALLOC(bw_lpc1, st->lpcSize, spx_coef_t);
587
10.1k
   ALLOC(bw_lpc2, st->lpcSize, spx_coef_t);
588
589
10.1k
   ALLOC(lsp, st->lpcSize, spx_lsp_t);
590
10.1k
   ALLOC(qlsp, st->lpcSize, spx_lsp_t);
591
10.1k
   ALLOC(interp_lsp, st->lpcSize, spx_lsp_t);
592
10.1k
   ALLOC(interp_qlsp, st->lpcSize, spx_lsp_t);
593
594
10.1k
   {
595
10.1k
      VARDECL(spx_word16_t *autocorr);
596
10.1k
      VARDECL(spx_word16_t *w_sig);
597
10.1k
      ALLOC(autocorr, st->lpcSize+1, spx_word16_t);
598
10.1k
      ALLOC(w_sig, st->windowSize, spx_word16_t);
599
      /* Window for analysis */
600
      /* FIXME: This is a kludge */
601
10.1k
      if (st->subframeSize==80)
602
2.37k
      {
603
952k
         for (i=0;i<st->windowSize;i++)
604
950k
            w_sig[i] = EXTRACT16(SHR32(MULT16_16(high[i],st->window[i>>1]),SIG_SHIFT));
605
7.75k
      } else {
606
1.55M
         for (i=0;i<st->windowSize;i++)
607
1.55M
            w_sig[i] = EXTRACT16(SHR32(MULT16_16(high[i],st->window[i]),SIG_SHIFT));
608
7.75k
      }
609
      /* Compute auto-correlation */
610
10.1k
      _spx_autocorr(w_sig, autocorr, st->lpcSize+1, st->windowSize);
611
10.1k
      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
101k
      for (i=0;i<st->lpcSize+1;i++)
615
91.1k
         autocorr[i] = MULT16_16_Q14(autocorr[i],st->lagWindow[i]);
616
617
      /* Levinson-Durbin */
618
10.1k
      _spx_lpc(lpc, autocorr, st->lpcSize);
619
10.1k
   }
620
621
   /* LPC to LSPs (x-domain) transform */
622
10.1k
   roots=lpc_to_lsp (lpc, st->lpcSize, lsp, 10, LSP_DELTA1, stack);
623
10.1k
   if (roots!=st->lpcSize)
624
176
   {
625
176
      roots = lpc_to_lsp (lpc, st->lpcSize, lsp, 10, LSP_DELTA2, stack);
626
176
      if (roots!=st->lpcSize) {
627
         /*If we can't find all LSP's, do some damage control and use a flat filter*/
628
963
         for (i=0;i<st->lpcSize;i++)
629
856
         {
630
856
            lsp[i]=st->old_lsp[i];
631
856
         }
632
107
      }
633
176
   }
634
635
10.1k
#ifndef DISABLE_VBR
636
   /* VBR code */
637
10.1k
   if ((st->vbr_enabled || st->vad_enabled) && !dtx)
638
6.86k
   {
639
6.86k
      float ratio;
640
6.86k
      if (st->abr_enabled)
641
2.48k
      {
642
2.48k
         float qual_change=0;
643
2.48k
         if (st->abr_drift2 * st->abr_drift > 0)
644
1.85k
         {
645
            /* Only adapt if long-term and short-term drift are the same sign */
646
1.85k
            qual_change = -.00001*st->abr_drift/(1+st->abr_count);
647
1.85k
            if (qual_change>.1)
648
0
               qual_change=.1;
649
1.85k
            if (qual_change<-.1)
650
375
               qual_change=-.1;
651
1.85k
         }
652
2.48k
         st->vbr_quality += qual_change;
653
2.48k
         if (st->vbr_quality>10)
654
0
            st->vbr_quality=10;
655
2.48k
         if (st->vbr_quality<0)
656
1.85k
            st->vbr_quality=0;
657
2.48k
      }
658
659
660
6.86k
      ratio = 2*log((1.f+e_high)/(1.f+e_low));
661
662
6.86k
      speex_encoder_ctl(st->st_low, SPEEX_GET_RELATIVE_QUALITY, &st->relative_quality);
663
6.86k
      if (ratio<-4)
664
102
         ratio=-4;
665
6.86k
      if (ratio>2)
666
74
         ratio=2;
667
      /*if (ratio>-2)*/
668
6.86k
      if (st->vbr_enabled)
669
5.45k
      {
670
5.45k
         spx_int32_t modeid;
671
5.45k
         modeid = mode->nb_modes-1;
672
5.45k
         st->relative_quality+=1.0*(ratio+2);
673
5.45k
   if (st->relative_quality<-1)
674
18
            st->relative_quality=-1;
675
17.2k
         while (modeid)
676
16.8k
         {
677
16.8k
            int v1;
678
16.8k
            float thresh;
679
16.8k
            v1=(int)floor(st->vbr_quality);
680
16.8k
            if (v1==10)
681
844
               thresh = mode->vbr_thresh[modeid][v1];
682
15.9k
            else
683
15.9k
               thresh = (st->vbr_quality-v1)   * mode->vbr_thresh[modeid][v1+1] +
684
15.9k
                        (1+v1-st->vbr_quality) * mode->vbr_thresh[modeid][v1];
685
16.8k
            if (st->relative_quality >= thresh && st->sampling_rate*st->submodes[modeid]->bits_per_frame/st->full_frame_size <= st->vbr_max_high)
686
5.08k
               break;
687
11.7k
            modeid--;
688
11.7k
         }
689
5.45k
         speex_encoder_ctl(state, SPEEX_SET_HIGH_MODE, &modeid);
690
5.45k
         if (st->abr_enabled)
691
2.48k
         {
692
2.48k
            spx_int32_t bitrate;
693
2.48k
            speex_encoder_ctl(state, SPEEX_GET_BITRATE, &bitrate);
694
2.48k
            st->abr_drift+=(bitrate-st->abr_enabled);
695
2.48k
            st->abr_drift2 = .95*st->abr_drift2 + .05*(bitrate-st->abr_enabled);
696
2.48k
            st->abr_count += 1.0;
697
2.48k
         }
698
699
5.45k
      } else {
700
         /* VAD only */
701
1.41k
         int modeid;
702
1.41k
         if (st->relative_quality<2.0)
703
385
            modeid=1;
704
1.02k
         else
705
1.02k
            modeid=st->submodeSelect;
706
         /*speex_encoder_ctl(state, SPEEX_SET_MODE, &mode);*/
707
1.41k
         st->submodeID=modeid;
708
709
1.41k
      }
710
      /*fprintf (stderr, "%f %f\n", ratio, low_qual);*/
711
6.86k
   }
712
10.1k
#endif /* #ifndef DISABLE_VBR */
713
714
10.1k
   if (st->encode_submode)
715
10.1k
   {
716
10.1k
      speex_bits_pack(bits, 1, 1);
717
10.1k
      if (dtx)
718
534
         speex_bits_pack(bits, 0, SB_SUBMODE_BITS);
719
9.59k
      else
720
9.59k
         speex_bits_pack(bits, st->submodeID, SB_SUBMODE_BITS);
721
10.1k
   }
722
723
   /* If null mode (no transmission), just set a couple things to zero*/
724
10.1k
   if (dtx || st->submodes[st->submodeID] == NULL)
725
1.06k
   {
726
215k
      for (i=0;i<st->frame_size;i++)
727
214k
         high[i]=VERY_SMALL;
728
729
9.58k
      for (i=0;i<st->lpcSize;i++)
730
8.52k
         st->mem_sw[i]=0;
731
1.06k
      st->first=1;
732
733
      /* Final signal synthesis from excitation */
734
1.06k
      iir_mem16(high, st->interp_qlpc, high, st->frame_size, st->lpcSize, st->mem_sp, stack);
735
736
1.06k
      if (dtx)
737
534
         return 0;
738
531
      else
739
531
         return 1;
740
1.06k
   }
741
742
743
   /* LSP quantization */
744
9.06k
   SUBMODE(lsp_quant)(lsp, qlsp, st->lpcSize, bits);
745
746
9.06k
   if (st->first)
747
2.56k
   {
748
23.1k
      for (i=0;i<st->lpcSize;i++)
749
20.5k
         st->old_lsp[i] = lsp[i];
750
23.1k
      for (i=0;i<st->lpcSize;i++)
751
20.5k
         st->old_qlsp[i] = qlsp[i];
752
2.56k
   }
753
754
9.06k
   ALLOC(mem, st->lpcSize, spx_mem_t);
755
9.06k
   ALLOC(syn_resp, st->subframeSize, spx_word16_t);
756
9.06k
   ALLOC(innov, st->subframeSize, spx_sig_t);
757
9.06k
   ALLOC(target, st->subframeSize, spx_word16_t);
758
759
45.3k
   for (sub=0;sub<st->nbSubframes;sub++)
760
36.2k
   {
761
36.2k
      VARDECL(spx_word16_t *exc);
762
36.2k
      VARDECL(spx_word16_t *res);
763
36.2k
      VARDECL(spx_word16_t *sw);
764
36.2k
      spx_word16_t *sp;
765
36.2k
      spx_word16_t filter_ratio;     /*Q7*/
766
36.2k
      int offset;
767
36.2k
      spx_word32_t rl, rh;           /*Q13*/
768
36.2k
      spx_word16_t eh=0;
769
770
36.2k
      offset = st->subframeSize*sub;
771
36.2k
      sp=high+offset;
772
36.2k
      ALLOC(exc, st->subframeSize, spx_word16_t);
773
36.2k
      ALLOC(res, st->subframeSize, spx_word16_t);
774
36.2k
      ALLOC(sw, st->subframeSize, spx_word16_t);
775
776
      /* LSP interpolation (quantized and unquantized) */
777
36.2k
      lsp_interpolate(st->old_lsp, lsp, interp_lsp, st->lpcSize, sub, st->nbSubframes, LSP_MARGIN);
778
36.2k
      lsp_interpolate(st->old_qlsp, qlsp, interp_qlsp, st->lpcSize, sub, st->nbSubframes, LSP_MARGIN);
779
780
36.2k
      lsp_to_lpc(interp_lsp, interp_lpc, st->lpcSize,stack);
781
36.2k
      lsp_to_lpc(interp_qlsp, st->interp_qlpc, st->lpcSize, stack);
782
783
36.2k
      bw_lpc(st->gamma1, interp_lpc, bw_lpc1, st->lpcSize);
784
36.2k
      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
36.2k
      st->pi_gain[sub]=LPC_SCALING;
789
36.2k
      rh = LPC_SCALING;
790
181k
      for (i=0;i<st->lpcSize;i+=2)
791
144k
      {
792
144k
         rh += st->interp_qlpc[i+1] - st->interp_qlpc[i];
793
144k
         st->pi_gain[sub] += st->interp_qlpc[i] + st->interp_qlpc[i+1];
794
144k
      }
795
796
36.2k
      rl = low_pi_gain[sub];
797
#ifdef FIXED_POINT
798
22.9k
      filter_ratio=EXTRACT16(SATURATE(PDIV32(SHL32(ADD32(rl,82),7),ADD32(82,rh)),32767));
799
#else
800
      filter_ratio=(rl+.01)/(rh+.01);
801
#endif
802
803
      /* Compute "real excitation" */
804
36.2k
      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
36.2k
      eh = compute_rms16(exc, st->subframeSize);
808
809
36.2k
      if (!SUBMODE(innovation_quant)) {/* 1 for spectral folding excitation, 0 for stochastic */
810
27.6k
         spx_word32_t g;   /*Q7*/
811
27.6k
         spx_word16_t el;  /*Q0*/
812
27.6k
         el = low_innov_rms[sub];
813
814
         /* Gain to use if we want to use the low-band excitation for high-band */
815
27.6k
         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
27.6k
         {
836
27.6k
            int quant = scal_quant(g, fold_quant_bound, 32);
837
            /*speex_warning_int("tata", quant);*/
838
27.6k
            if (quant<0)
839
0
               quant=0;
840
27.6k
            if (quant>31)
841
0
               quant=31;
842
27.6k
            speex_bits_pack(bits, quant, 5);
843
27.6k
         }
844
27.6k
         if (st->innov_rms_save)
845
5.54k
         {
846
5.54k
            st->innov_rms_save[sub] = eh;
847
5.54k
         }
848
27.6k
         st->exc_rms[sub] = eh;
849
27.6k
      } else {
850
8.60k
         spx_word16_t gc;       /*Q7*/
851
8.60k
         spx_word32_t scale;    /*Q14*/
852
8.60k
         spx_word16_t el;       /*Q0*/
853
8.60k
         el = low_exc_rms[sub]; /*Q0*/
854
855
8.60k
         gc = PDIV32_16(MULT16_16(filter_ratio,1+eh),1+el);
856
857
         /* This is a kludge that cleans up a historical bug */
858
8.60k
         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
8.60k
         {
862
8.60k
            int qgc = scal_quant(gc, gc_quant_bound, 16);
863
8.60k
            speex_bits_pack(bits, qgc, 4);
864
8.60k
            gc = MULT16_16_Q15(QCONST16(0.87360,15),gc_quant_bound[qgc]);
865
8.60k
         }
866
8.60k
         if (st->subframeSize==80)
867
0
            gc = MULT16_16_P14(QCONST16(1.4142f,14), gc);
868
869
8.60k
         scale = SHL32(MULT16_16(PDIV32_16(SHL32(EXTEND32(gc),SIG_SHIFT-6),MAX16(EPSILON,filter_ratio)),(1+el)),6);
870
871
8.60k
         compute_impulse_response(st->interp_qlpc, bw_lpc1, bw_lpc2, syn_resp, st->subframeSize, st->lpcSize, stack);
872
873
874
         /* Reset excitation */
875
352k
         for (i=0;i<st->subframeSize;i++)
876
344k
            res[i]=VERY_SMALL;
877
878
         /* Compute zero response (ringing) of A(z/g1) / ( A(z/g2) * Aq(z) ) */
879
77.4k
         for (i=0;i<st->lpcSize;i++)
880
68.8k
            mem[i]=st->mem_sp[i];
881
8.60k
         iir_mem16(res, st->interp_qlpc, res, st->subframeSize, st->lpcSize, mem, stack);
882
883
77.4k
         for (i=0;i<st->lpcSize;i++)
884
68.8k
            mem[i]=st->mem_sw[i];
885
8.60k
         filter_mem16(res, bw_lpc1, bw_lpc2, res, st->subframeSize, st->lpcSize, mem, stack);
886
887
         /* Compute weighted signal */
888
77.4k
         for (i=0;i<st->lpcSize;i++)
889
68.8k
            mem[i]=st->mem_sw[i];
890
8.60k
         filter_mem16(sp, bw_lpc1, bw_lpc2, sw, st->subframeSize, st->lpcSize, mem, stack);
891
892
         /* Compute target signal */
893
352k
         for (i=0;i<st->subframeSize;i++)
894
344k
            target[i]=SUB16(sw[i],res[i]);
895
896
8.60k
         signal_div(target, target, scale, st->subframeSize);
897
898
         /* Reset excitation */
899
8.60k
         SPEEX_MEMSET(innov, 0, st->subframeSize);
900
901
         /*print_vec(target, st->subframeSize, "\ntarget");*/
902
8.60k
         SUBMODE(innovation_quant)(target, st->interp_qlpc, bw_lpc1, bw_lpc2,
903
8.60k
                                   SUBMODE(innovation_params), st->lpcSize, st->subframeSize,
904
8.60k
                                   innov, syn_resp, bits, stack, st->complexity, SUBMODE(double_codebook));
905
         /*print_vec(target, st->subframeSize, "after");*/
906
907
8.60k
         signal_mul(innov, innov, scale, st->subframeSize);
908
909
8.60k
         if (SUBMODE(double_codebook)) {
910
2.97k
            char *tmp_stack=stack;
911
2.97k
            VARDECL(spx_sig_t *innov2);
912
2.97k
            ALLOC(innov2, st->subframeSize, spx_sig_t);
913
2.97k
            SPEEX_MEMSET(innov2, 0, st->subframeSize);
914
122k
            for (i=0;i<st->subframeSize;i++)
915
119k
               target[i]=MULT16_16_P13(QCONST16(2.5f,13), target[i]);
916
917
2.97k
            SUBMODE(innovation_quant)(target, st->interp_qlpc, bw_lpc1, bw_lpc2,
918
2.97k
                                      SUBMODE(innovation_params), st->lpcSize, st->subframeSize,
919
2.97k
                                      innov2, syn_resp, bits, stack, st->complexity, 0);
920
2.97k
            signal_mul(innov2, innov2, MULT16_32_P15(QCONST16(0.4f,15),scale), st->subframeSize);
921
922
122k
            for (i=0;i<st->subframeSize;i++)
923
119k
               innov[i] = ADD32(innov[i],innov2[i]);
924
2.97k
            stack = tmp_stack;
925
2.97k
         }
926
352k
         for (i=0;i<st->subframeSize;i++)
927
344k
            exc[i] = PSHR32(innov[i],SIG_SHIFT);
928
929
8.60k
         if (st->innov_rms_save)
930
3.75k
         {
931
3.75k
            st->innov_rms_save[sub] = MULT16_16_Q15(QCONST16(.70711f, 15), compute_rms(innov, st->subframeSize));
932
3.75k
         }
933
8.60k
         st->exc_rms[sub] = compute_rms16(exc, st->subframeSize);
934
935
936
8.60k
      }
937
938
939
      /*Keep the previous memory*/
940
326k
      for (i=0;i<st->lpcSize;i++)
941
289k
         mem[i]=st->mem_sp[i];
942
      /* Final signal synthesis from excitation */
943
36.2k
      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
36.2k
      filter_mem16(sp, bw_lpc1, bw_lpc2, sw, st->subframeSize, st->lpcSize, st->mem_sw, stack);
947
36.2k
   }
948
949
81.5k
   for (i=0;i<st->lpcSize;i++)
950
72.4k
      st->old_lsp[i] = lsp[i];
951
81.5k
   for (i=0;i<st->lpcSize;i++)
952
72.4k
      st->old_qlsp[i] = qlsp[i];
953
954
9.06k
   st->first=0;
955
956
9.06k
   return 1;
957
10.1k
}
sb_encode
Line
Count
Source
516
3.86k
{
517
3.86k
   SBEncState *st;
518
3.86k
   int i, roots, sub;
519
3.86k
   char *stack;
520
3.86k
   VARDECL(spx_mem_t *mem);
521
3.86k
   VARDECL(spx_sig_t *innov);
522
3.86k
   VARDECL(spx_word16_t *target);
523
3.86k
   VARDECL(spx_word16_t *syn_resp);
524
3.86k
   VARDECL(spx_word32_t *low_pi_gain);
525
3.86k
   spx_word16_t *low;
526
3.86k
   spx_word16_t *high;
527
3.86k
   VARDECL(spx_word16_t *low_exc_rms);
528
3.86k
   VARDECL(spx_word16_t *low_innov_rms);
529
3.86k
   const SpeexSBMode *mode;
530
3.86k
   spx_int32_t dtx;
531
3.86k
   spx_word16_t *in = (spx_word16_t*)vin;
532
3.86k
   spx_word16_t e_low=0, e_high=0;
533
3.86k
   VARDECL(spx_coef_t *lpc);
534
3.86k
   VARDECL(spx_coef_t *interp_lpc);
535
3.86k
   VARDECL(spx_coef_t *bw_lpc1);
536
3.86k
   VARDECL(spx_coef_t *bw_lpc2);
537
3.86k
   VARDECL(spx_lsp_t *lsp);
538
3.86k
   VARDECL(spx_lsp_t *qlsp);
539
3.86k
   VARDECL(spx_lsp_t *interp_lsp);
540
3.86k
   VARDECL(spx_lsp_t *interp_qlsp);
541
542
3.86k
   st = (SBEncState*)state;
543
3.86k
   stack=st->stack;
544
3.86k
   mode = (const SpeexSBMode*)(st->mode->mode);
545
3.86k
   low = in;
546
3.86k
   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
3.86k
   qmf_decomp(in, h0, low, high, st->full_frame_size, QMF_ORDER, st->h0_mem, stack);
551
552
3.86k
#ifndef DISABLE_VBR
553
3.86k
   if (st->vbr_enabled || st->vad_enabled)
554
2.70k
   {
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
2.70k
      e_low = compute_rms16(low, st->frame_size);
558
2.70k
      e_high = compute_rms16(high, st->frame_size);
559
2.70k
   }
560
3.86k
#endif /* #ifndef DISABLE_VBR */
561
562
3.86k
   ALLOC(low_innov_rms, st->nbSubframes, spx_word16_t);
563
3.86k
   speex_encoder_ctl(st->st_low, SPEEX_SET_INNOVATION_SAVE, low_innov_rms);
564
   /* Encode the narrowband part*/
565
3.86k
   speex_encode_native(st->st_low, low, bits);
566
567
3.86k
   high = high - (st->windowSize-st->frame_size);
568
3.86k
   SPEEX_COPY(high, st->high, st->windowSize-st->frame_size);
569
3.86k
   SPEEX_COPY(st->high, &high[st->frame_size], st->windowSize-st->frame_size);
570
571
572
3.86k
   ALLOC(low_pi_gain, st->nbSubframes, spx_word32_t);
573
3.86k
   ALLOC(low_exc_rms, st->nbSubframes, spx_word16_t);
574
3.86k
   speex_encoder_ctl(st->st_low, SPEEX_GET_PI_GAIN, low_pi_gain);
575
3.86k
   speex_encoder_ctl(st->st_low, SPEEX_GET_EXC, low_exc_rms);
576
577
3.86k
   speex_encoder_ctl(st->st_low, SPEEX_GET_LOW_MODE, &dtx);
578
579
3.86k
   if (dtx==0)
580
274
      dtx=1;
581
3.58k
   else
582
3.58k
      dtx=0;
583
584
3.86k
   ALLOC(lpc, st->lpcSize, spx_coef_t);
585
3.86k
   ALLOC(interp_lpc, st->lpcSize, spx_coef_t);
586
3.86k
   ALLOC(bw_lpc1, st->lpcSize, spx_coef_t);
587
3.86k
   ALLOC(bw_lpc2, st->lpcSize, spx_coef_t);
588
589
3.86k
   ALLOC(lsp, st->lpcSize, spx_lsp_t);
590
3.86k
   ALLOC(qlsp, st->lpcSize, spx_lsp_t);
591
3.86k
   ALLOC(interp_lsp, st->lpcSize, spx_lsp_t);
592
3.86k
   ALLOC(interp_qlsp, st->lpcSize, spx_lsp_t);
593
594
3.86k
   {
595
3.86k
      VARDECL(spx_word16_t *autocorr);
596
3.86k
      VARDECL(spx_word16_t *w_sig);
597
3.86k
      ALLOC(autocorr, st->lpcSize+1, spx_word16_t);
598
3.86k
      ALLOC(w_sig, st->windowSize, spx_word16_t);
599
      /* Window for analysis */
600
      /* FIXME: This is a kludge */
601
3.86k
      if (st->subframeSize==80)
602
990
      {
603
396k
         for (i=0;i<st->windowSize;i++)
604
396k
            w_sig[i] = EXTRACT16(SHR32(MULT16_16(high[i],st->window[i>>1]),SIG_SHIFT));
605
2.87k
      } else {
606
576k
         for (i=0;i<st->windowSize;i++)
607
574k
            w_sig[i] = EXTRACT16(SHR32(MULT16_16(high[i],st->window[i]),SIG_SHIFT));
608
2.87k
      }
609
      /* Compute auto-correlation */
610
3.86k
      _spx_autocorr(w_sig, autocorr, st->lpcSize+1, st->windowSize);
611
3.86k
      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
38.6k
      for (i=0;i<st->lpcSize+1;i++)
615
34.7k
         autocorr[i] = MULT16_16_Q14(autocorr[i],st->lagWindow[i]);
616
617
      /* Levinson-Durbin */
618
3.86k
      _spx_lpc(lpc, autocorr, st->lpcSize);
619
3.86k
   }
620
621
   /* LPC to LSPs (x-domain) transform */
622
3.86k
   roots=lpc_to_lsp (lpc, st->lpcSize, lsp, 10, LSP_DELTA1, stack);
623
3.86k
   if (roots!=st->lpcSize)
624
20
   {
625
20
      roots = lpc_to_lsp (lpc, st->lpcSize, lsp, 10, LSP_DELTA2, stack);
626
20
      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
20
   }
634
635
3.86k
#ifndef DISABLE_VBR
636
   /* VBR code */
637
3.86k
   if ((st->vbr_enabled || st->vad_enabled) && !dtx)
638
2.43k
   {
639
2.43k
      float ratio;
640
2.43k
      if (st->abr_enabled)
641
633
      {
642
633
         float qual_change=0;
643
633
         if (st->abr_drift2 * st->abr_drift > 0)
644
457
         {
645
            /* Only adapt if long-term and short-term drift are the same sign */
646
457
            qual_change = -.00001*st->abr_drift/(1+st->abr_count);
647
457
            if (qual_change>.1)
648
0
               qual_change=.1;
649
457
            if (qual_change<-.1)
650
113
               qual_change=-.1;
651
457
         }
652
633
         st->vbr_quality += qual_change;
653
633
         if (st->vbr_quality>10)
654
0
            st->vbr_quality=10;
655
633
         if (st->vbr_quality<0)
656
457
            st->vbr_quality=0;
657
633
      }
658
659
660
2.43k
      ratio = 2*log((1.f+e_high)/(1.f+e_low));
661
662
2.43k
      speex_encoder_ctl(st->st_low, SPEEX_GET_RELATIVE_QUALITY, &st->relative_quality);
663
2.43k
      if (ratio<-4)
664
46
         ratio=-4;
665
2.43k
      if (ratio>2)
666
17
         ratio=2;
667
      /*if (ratio>-2)*/
668
2.43k
      if (st->vbr_enabled)
669
1.87k
      {
670
1.87k
         spx_int32_t modeid;
671
1.87k
         modeid = mode->nb_modes-1;
672
1.87k
         st->relative_quality+=1.0*(ratio+2);
673
1.87k
   if (st->relative_quality<-1)
674
10
            st->relative_quality=-1;
675
5.91k
         while (modeid)
676
5.73k
         {
677
5.73k
            int v1;
678
5.73k
            float thresh;
679
5.73k
            v1=(int)floor(st->vbr_quality);
680
5.73k
            if (v1==10)
681
527
               thresh = mode->vbr_thresh[modeid][v1];
682
5.20k
            else
683
5.20k
               thresh = (st->vbr_quality-v1)   * mode->vbr_thresh[modeid][v1+1] +
684
5.20k
                        (1+v1-st->vbr_quality) * mode->vbr_thresh[modeid][v1];
685
5.73k
            if (st->relative_quality >= thresh && st->sampling_rate*st->submodes[modeid]->bits_per_frame/st->full_frame_size <= st->vbr_max_high)
686
1.69k
               break;
687
4.03k
            modeid--;
688
4.03k
         }
689
1.87k
         speex_encoder_ctl(state, SPEEX_SET_HIGH_MODE, &modeid);
690
1.87k
         if (st->abr_enabled)
691
633
         {
692
633
            spx_int32_t bitrate;
693
633
            speex_encoder_ctl(state, SPEEX_GET_BITRATE, &bitrate);
694
633
            st->abr_drift+=(bitrate-st->abr_enabled);
695
633
            st->abr_drift2 = .95*st->abr_drift2 + .05*(bitrate-st->abr_enabled);
696
633
            st->abr_count += 1.0;
697
633
         }
698
699
1.87k
      } else {
700
         /* VAD only */
701
554
         int modeid;
702
554
         if (st->relative_quality<2.0)
703
222
            modeid=1;
704
332
         else
705
332
            modeid=st->submodeSelect;
706
         /*speex_encoder_ctl(state, SPEEX_SET_MODE, &mode);*/
707
554
         st->submodeID=modeid;
708
709
554
      }
710
      /*fprintf (stderr, "%f %f\n", ratio, low_qual);*/
711
2.43k
   }
712
3.86k
#endif /* #ifndef DISABLE_VBR */
713
714
3.86k
   if (st->encode_submode)
715
3.86k
   {
716
3.86k
      speex_bits_pack(bits, 1, 1);
717
3.86k
      if (dtx)
718
274
         speex_bits_pack(bits, 0, SB_SUBMODE_BITS);
719
3.58k
      else
720
3.58k
         speex_bits_pack(bits, st->submodeID, SB_SUBMODE_BITS);
721
3.86k
   }
722
723
   /* If null mode (no transmission), just set a couple things to zero*/
724
3.86k
   if (dtx || st->submodes[st->submodeID] == NULL)
725
546
   {
726
110k
      for (i=0;i<st->frame_size;i++)
727
110k
         high[i]=VERY_SMALL;
728
729
4.91k
      for (i=0;i<st->lpcSize;i++)
730
4.36k
         st->mem_sw[i]=0;
731
546
      st->first=1;
732
733
      /* Final signal synthesis from excitation */
734
546
      iir_mem16(high, st->interp_qlpc, high, st->frame_size, st->lpcSize, st->mem_sp, stack);
735
736
546
      if (dtx)
737
274
         return 0;
738
272
      else
739
272
         return 1;
740
546
   }
741
742
743
   /* LSP quantization */
744
3.31k
   SUBMODE(lsp_quant)(lsp, qlsp, st->lpcSize, bits);
745
746
3.31k
   if (st->first)
747
986
   {
748
8.87k
      for (i=0;i<st->lpcSize;i++)
749
7.88k
         st->old_lsp[i] = lsp[i];
750
8.87k
      for (i=0;i<st->lpcSize;i++)
751
7.88k
         st->old_qlsp[i] = qlsp[i];
752
986
   }
753
754
3.31k
   ALLOC(mem, st->lpcSize, spx_mem_t);
755
3.31k
   ALLOC(syn_resp, st->subframeSize, spx_word16_t);
756
3.31k
   ALLOC(innov, st->subframeSize, spx_sig_t);
757
3.31k
   ALLOC(target, st->subframeSize, spx_word16_t);
758
759
16.5k
   for (sub=0;sub<st->nbSubframes;sub++)
760
13.2k
   {
761
13.2k
      VARDECL(spx_word16_t *exc);
762
13.2k
      VARDECL(spx_word16_t *res);
763
13.2k
      VARDECL(spx_word16_t *sw);
764
13.2k
      spx_word16_t *sp;
765
13.2k
      spx_word16_t filter_ratio;     /*Q7*/
766
13.2k
      int offset;
767
13.2k
      spx_word32_t rl, rh;           /*Q13*/
768
13.2k
      spx_word16_t eh=0;
769
770
13.2k
      offset = st->subframeSize*sub;
771
13.2k
      sp=high+offset;
772
13.2k
      ALLOC(exc, st->subframeSize, spx_word16_t);
773
13.2k
      ALLOC(res, st->subframeSize, spx_word16_t);
774
13.2k
      ALLOC(sw, st->subframeSize, spx_word16_t);
775
776
      /* LSP interpolation (quantized and unquantized) */
777
13.2k
      lsp_interpolate(st->old_lsp, lsp, interp_lsp, st->lpcSize, sub, st->nbSubframes, LSP_MARGIN);
778
13.2k
      lsp_interpolate(st->old_qlsp, qlsp, interp_qlsp, st->lpcSize, sub, st->nbSubframes, LSP_MARGIN);
779
780
13.2k
      lsp_to_lpc(interp_lsp, interp_lpc, st->lpcSize,stack);
781
13.2k
      lsp_to_lpc(interp_qlsp, st->interp_qlpc, st->lpcSize, stack);
782
783
13.2k
      bw_lpc(st->gamma1, interp_lpc, bw_lpc1, st->lpcSize);
784
13.2k
      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
13.2k
      st->pi_gain[sub]=LPC_SCALING;
789
13.2k
      rh = LPC_SCALING;
790
66.2k
      for (i=0;i<st->lpcSize;i+=2)
791
53.0k
      {
792
53.0k
         rh += st->interp_qlpc[i+1] - st->interp_qlpc[i];
793
53.0k
         st->pi_gain[sub] += st->interp_qlpc[i] + st->interp_qlpc[i+1];
794
53.0k
      }
795
796
13.2k
      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
13.2k
      filter_ratio=(rl+.01)/(rh+.01);
801
13.2k
#endif
802
803
      /* Compute "real excitation" */
804
13.2k
      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
13.2k
      eh = compute_rms16(exc, st->subframeSize);
808
809
13.2k
      if (!SUBMODE(innovation_quant)) {/* 1 for spectral folding excitation, 0 for stochastic */
810
10.4k
         spx_word32_t g;   /*Q7*/
811
10.4k
         spx_word16_t el;  /*Q0*/
812
10.4k
         el = low_innov_rms[sub];
813
814
         /* Gain to use if we want to use the low-band excitation for high-band */
815
10.4k
         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
10.4k
         {
836
10.4k
            int quant = scal_quant(g, fold_quant_bound, 32);
837
            /*speex_warning_int("tata", quant);*/
838
10.4k
            if (quant<0)
839
0
               quant=0;
840
10.4k
            if (quant>31)
841
0
               quant=31;
842
10.4k
            speex_bits_pack(bits, quant, 5);
843
10.4k
         }
844
10.4k
         if (st->innov_rms_save)
845
2.53k
         {
846
2.53k
            st->innov_rms_save[sub] = eh;
847
2.53k
         }
848
10.4k
         st->exc_rms[sub] = eh;
849
10.4k
      } else {
850
2.76k
         spx_word16_t gc;       /*Q7*/
851
2.76k
         spx_word32_t scale;    /*Q14*/
852
2.76k
         spx_word16_t el;       /*Q0*/
853
2.76k
         el = low_exc_rms[sub]; /*Q0*/
854
855
2.76k
         gc = PDIV32_16(MULT16_16(filter_ratio,1+eh),1+el);
856
857
         /* This is a kludge that cleans up a historical bug */
858
2.76k
         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
2.76k
         {
862
2.76k
            int qgc = scal_quant(gc, gc_quant_bound, 16);
863
2.76k
            speex_bits_pack(bits, qgc, 4);
864
2.76k
            gc = MULT16_16_Q15(QCONST16(0.87360,15),gc_quant_bound[qgc]);
865
2.76k
         }
866
2.76k
         if (st->subframeSize==80)
867
0
            gc = MULT16_16_P14(QCONST16(1.4142f,14), gc);
868
869
2.76k
         scale = SHL32(MULT16_16(PDIV32_16(SHL32(EXTEND32(gc),SIG_SHIFT-6),MAX16(EPSILON,filter_ratio)),(1+el)),6);
870
871
2.76k
         compute_impulse_response(st->interp_qlpc, bw_lpc1, bw_lpc2, syn_resp, st->subframeSize, st->lpcSize, stack);
872
873
874
         /* Reset excitation */
875
113k
         for (i=0;i<st->subframeSize;i++)
876
110k
            res[i]=VERY_SMALL;
877
878
         /* Compute zero response (ringing) of A(z/g1) / ( A(z/g2) * Aq(z) ) */
879
24.9k
         for (i=0;i<st->lpcSize;i++)
880
22.1k
            mem[i]=st->mem_sp[i];
881
2.76k
         iir_mem16(res, st->interp_qlpc, res, st->subframeSize, st->lpcSize, mem, stack);
882
883
24.9k
         for (i=0;i<st->lpcSize;i++)
884
22.1k
            mem[i]=st->mem_sw[i];
885
2.76k
         filter_mem16(res, bw_lpc1, bw_lpc2, res, st->subframeSize, st->lpcSize, mem, stack);
886
887
         /* Compute weighted signal */
888
24.9k
         for (i=0;i<st->lpcSize;i++)
889
22.1k
            mem[i]=st->mem_sw[i];
890
2.76k
         filter_mem16(sp, bw_lpc1, bw_lpc2, sw, st->subframeSize, st->lpcSize, mem, stack);
891
892
         /* Compute target signal */
893
113k
         for (i=0;i<st->subframeSize;i++)
894
110k
            target[i]=SUB16(sw[i],res[i]);
895
896
2.76k
         signal_div(target, target, scale, st->subframeSize);
897
898
         /* Reset excitation */
899
2.76k
         SPEEX_MEMSET(innov, 0, st->subframeSize);
900
901
         /*print_vec(target, st->subframeSize, "\ntarget");*/
902
2.76k
         SUBMODE(innovation_quant)(target, st->interp_qlpc, bw_lpc1, bw_lpc2,
903
2.76k
                                   SUBMODE(innovation_params), st->lpcSize, st->subframeSize,
904
2.76k
                                   innov, syn_resp, bits, stack, st->complexity, SUBMODE(double_codebook));
905
         /*print_vec(target, st->subframeSize, "after");*/
906
907
2.76k
         signal_mul(innov, innov, scale, st->subframeSize);
908
909
2.76k
         if (SUBMODE(double_codebook)) {
910
900
            char *tmp_stack=stack;
911
900
            VARDECL(spx_sig_t *innov2);
912
900
            ALLOC(innov2, st->subframeSize, spx_sig_t);
913
900
            SPEEX_MEMSET(innov2, 0, st->subframeSize);
914
36.9k
            for (i=0;i<st->subframeSize;i++)
915
36.0k
               target[i]=MULT16_16_P13(QCONST16(2.5f,13), target[i]);
916
917
900
            SUBMODE(innovation_quant)(target, st->interp_qlpc, bw_lpc1, bw_lpc2,
918
900
                                      SUBMODE(innovation_params), st->lpcSize, st->subframeSize,
919
900
                                      innov2, syn_resp, bits, stack, st->complexity, 0);
920
900
            signal_mul(innov2, innov2, MULT16_32_P15(QCONST16(0.4f,15),scale), st->subframeSize);
921
922
36.9k
            for (i=0;i<st->subframeSize;i++)
923
36.0k
               innov[i] = ADD32(innov[i],innov2[i]);
924
900
            stack = tmp_stack;
925
900
         }
926
113k
         for (i=0;i<st->subframeSize;i++)
927
110k
            exc[i] = PSHR32(innov[i],SIG_SHIFT);
928
929
2.76k
         if (st->innov_rms_save)
930
1.34k
         {
931
1.34k
            st->innov_rms_save[sub] = MULT16_16_Q15(QCONST16(.70711f, 15), compute_rms(innov, st->subframeSize));
932
1.34k
         }
933
2.76k
         st->exc_rms[sub] = compute_rms16(exc, st->subframeSize);
934
935
936
2.76k
      }
937
938
939
      /*Keep the previous memory*/
940
119k
      for (i=0;i<st->lpcSize;i++)
941
106k
         mem[i]=st->mem_sp[i];
942
      /* Final signal synthesis from excitation */
943
13.2k
      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
13.2k
      filter_mem16(sp, bw_lpc1, bw_lpc2, sw, st->subframeSize, st->lpcSize, st->mem_sw, stack);
947
13.2k
   }
948
949
29.8k
   for (i=0;i<st->lpcSize;i++)
950
26.5k
      st->old_lsp[i] = lsp[i];
951
29.8k
   for (i=0;i<st->lpcSize;i++)
952
26.5k
      st->old_qlsp[i] = qlsp[i];
953
954
3.31k
   st->first=0;
955
956
3.31k
   return 1;
957
3.86k
}
sb_encode
Line
Count
Source
516
6.26k
{
517
6.26k
   SBEncState *st;
518
6.26k
   int i, roots, sub;
519
6.26k
   char *stack;
520
6.26k
   VARDECL(spx_mem_t *mem);
521
6.26k
   VARDECL(spx_sig_t *innov);
522
6.26k
   VARDECL(spx_word16_t *target);
523
6.26k
   VARDECL(spx_word16_t *syn_resp);
524
6.26k
   VARDECL(spx_word32_t *low_pi_gain);
525
6.26k
   spx_word16_t *low;
526
6.26k
   spx_word16_t *high;
527
6.26k
   VARDECL(spx_word16_t *low_exc_rms);
528
6.26k
   VARDECL(spx_word16_t *low_innov_rms);
529
6.26k
   const SpeexSBMode *mode;
530
6.26k
   spx_int32_t dtx;
531
6.26k
   spx_word16_t *in = (spx_word16_t*)vin;
532
6.26k
   spx_word16_t e_low=0, e_high=0;
533
6.26k
   VARDECL(spx_coef_t *lpc);
534
6.26k
   VARDECL(spx_coef_t *interp_lpc);
535
6.26k
   VARDECL(spx_coef_t *bw_lpc1);
536
6.26k
   VARDECL(spx_coef_t *bw_lpc2);
537
6.26k
   VARDECL(spx_lsp_t *lsp);
538
6.26k
   VARDECL(spx_lsp_t *qlsp);
539
6.26k
   VARDECL(spx_lsp_t *interp_lsp);
540
6.26k
   VARDECL(spx_lsp_t *interp_qlsp);
541
542
6.26k
   st = (SBEncState*)state;
543
6.26k
   stack=st->stack;
544
6.26k
   mode = (const SpeexSBMode*)(st->mode->mode);
545
6.26k
   low = in;
546
6.26k
   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
6.26k
   qmf_decomp(in, h0, low, high, st->full_frame_size, QMF_ORDER, st->h0_mem, stack);
551
552
6.26k
#ifndef DISABLE_VBR
553
6.26k
   if (st->vbr_enabled || st->vad_enabled)
554
4.69k
   {
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
4.69k
      e_low = compute_rms16(low, st->frame_size);
558
4.69k
      e_high = compute_rms16(high, st->frame_size);
559
4.69k
   }
560
6.26k
#endif /* #ifndef DISABLE_VBR */
561
562
6.26k
   ALLOC(low_innov_rms, st->nbSubframes, spx_word16_t);
563
6.26k
   speex_encoder_ctl(st->st_low, SPEEX_SET_INNOVATION_SAVE, low_innov_rms);
564
   /* Encode the narrowband part*/
565
6.26k
   speex_encode_native(st->st_low, low, bits);
566
567
6.26k
   high = high - (st->windowSize-st->frame_size);
568
6.26k
   SPEEX_COPY(high, st->high, st->windowSize-st->frame_size);
569
6.26k
   SPEEX_COPY(st->high, &high[st->frame_size], st->windowSize-st->frame_size);
570
571
572
6.26k
   ALLOC(low_pi_gain, st->nbSubframes, spx_word32_t);
573
6.26k
   ALLOC(low_exc_rms, st->nbSubframes, spx_word16_t);
574
6.26k
   speex_encoder_ctl(st->st_low, SPEEX_GET_PI_GAIN, low_pi_gain);
575
6.26k
   speex_encoder_ctl(st->st_low, SPEEX_GET_EXC, low_exc_rms);
576
577
6.26k
   speex_encoder_ctl(st->st_low, SPEEX_GET_LOW_MODE, &dtx);
578
579
6.26k
   if (dtx==0)
580
260
      dtx=1;
581
6.00k
   else
582
6.00k
      dtx=0;
583
584
6.26k
   ALLOC(lpc, st->lpcSize, spx_coef_t);
585
6.26k
   ALLOC(interp_lpc, st->lpcSize, spx_coef_t);
586
6.26k
   ALLOC(bw_lpc1, st->lpcSize, spx_coef_t);
587
6.26k
   ALLOC(bw_lpc2, st->lpcSize, spx_coef_t);
588
589
6.26k
   ALLOC(lsp, st->lpcSize, spx_lsp_t);
590
6.26k
   ALLOC(qlsp, st->lpcSize, spx_lsp_t);
591
6.26k
   ALLOC(interp_lsp, st->lpcSize, spx_lsp_t);
592
6.26k
   ALLOC(interp_qlsp, st->lpcSize, spx_lsp_t);
593
594
6.26k
   {
595
6.26k
      VARDECL(spx_word16_t *autocorr);
596
6.26k
      VARDECL(spx_word16_t *w_sig);
597
6.26k
      ALLOC(autocorr, st->lpcSize+1, spx_word16_t);
598
6.26k
      ALLOC(w_sig, st->windowSize, spx_word16_t);
599
      /* Window for analysis */
600
      /* FIXME: This is a kludge */
601
6.26k
      if (st->subframeSize==80)
602
1.38k
      {
603
555k
         for (i=0;i<st->windowSize;i++)
604
554k
            w_sig[i] = EXTRACT16(SHR32(MULT16_16(high[i],st->window[i>>1]),SIG_SHIFT));
605
4.88k
      } else {
606
981k
         for (i=0;i<st->windowSize;i++)
607
976k
            w_sig[i] = EXTRACT16(SHR32(MULT16_16(high[i],st->window[i]),SIG_SHIFT));
608
4.88k
      }
609
      /* Compute auto-correlation */
610
6.26k
      _spx_autocorr(w_sig, autocorr, st->lpcSize+1, st->windowSize);
611
6.26k
      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
62.6k
      for (i=0;i<st->lpcSize+1;i++)
615
56.4k
         autocorr[i] = MULT16_16_Q14(autocorr[i],st->lagWindow[i]);
616
617
      /* Levinson-Durbin */
618
6.26k
      _spx_lpc(lpc, autocorr, st->lpcSize);
619
6.26k
   }
620
621
   /* LPC to LSPs (x-domain) transform */
622
6.26k
   roots=lpc_to_lsp (lpc, st->lpcSize, lsp, 10, LSP_DELTA1, stack);
623
6.26k
   if (roots!=st->lpcSize)
624
156
   {
625
156
      roots = lpc_to_lsp (lpc, st->lpcSize, lsp, 10, LSP_DELTA2, stack);
626
156
      if (roots!=st->lpcSize) {
627
         /*If we can't find all LSP's, do some damage control and use a flat filter*/
628
963
         for (i=0;i<st->lpcSize;i++)
629
856
         {
630
856
            lsp[i]=st->old_lsp[i];
631
856
         }
632
107
      }
633
156
   }
634
635
6.26k
#ifndef DISABLE_VBR
636
   /* VBR code */
637
6.26k
   if ((st->vbr_enabled || st->vad_enabled) && !dtx)
638
4.43k
   {
639
4.43k
      float ratio;
640
4.43k
      if (st->abr_enabled)
641
1.84k
      {
642
1.84k
         float qual_change=0;
643
1.84k
         if (st->abr_drift2 * st->abr_drift > 0)
644
1.40k
         {
645
            /* Only adapt if long-term and short-term drift are the same sign */
646
1.40k
            qual_change = -.00001*st->abr_drift/(1+st->abr_count);
647
1.40k
            if (qual_change>.1)
648
0
               qual_change=.1;
649
1.40k
            if (qual_change<-.1)
650
262
               qual_change=-.1;
651
1.40k
         }
652
1.84k
         st->vbr_quality += qual_change;
653
1.84k
         if (st->vbr_quality>10)
654
0
            st->vbr_quality=10;
655
1.84k
         if (st->vbr_quality<0)
656
1.40k
            st->vbr_quality=0;
657
1.84k
      }
658
659
660
4.43k
      ratio = 2*log((1.f+e_high)/(1.f+e_low));
661
662
4.43k
      speex_encoder_ctl(st->st_low, SPEEX_GET_RELATIVE_QUALITY, &st->relative_quality);
663
4.43k
      if (ratio<-4)
664
56
         ratio=-4;
665
4.43k
      if (ratio>2)
666
57
         ratio=2;
667
      /*if (ratio>-2)*/
668
4.43k
      if (st->vbr_enabled)
669
3.57k
      {
670
3.57k
         spx_int32_t modeid;
671
3.57k
         modeid = mode->nb_modes-1;
672
3.57k
         st->relative_quality+=1.0*(ratio+2);
673
3.57k
   if (st->relative_quality<-1)
674
8
            st->relative_quality=-1;
675
11.2k
         while (modeid)
676
11.1k
         {
677
11.1k
            int v1;
678
11.1k
            float thresh;
679
11.1k
            v1=(int)floor(st->vbr_quality);
680
11.1k
            if (v1==10)
681
317
               thresh = mode->vbr_thresh[modeid][v1];
682
10.7k
            else
683
10.7k
               thresh = (st->vbr_quality-v1)   * mode->vbr_thresh[modeid][v1+1] +
684
10.7k
                        (1+v1-st->vbr_quality) * mode->vbr_thresh[modeid][v1];
685
11.1k
            if (st->relative_quality >= thresh && st->sampling_rate*st->submodes[modeid]->bits_per_frame/st->full_frame_size <= st->vbr_max_high)
686
3.38k
               break;
687
7.72k
            modeid--;
688
7.72k
         }
689
3.57k
         speex_encoder_ctl(state, SPEEX_SET_HIGH_MODE, &modeid);
690
3.57k
         if (st->abr_enabled)
691
1.84k
         {
692
1.84k
            spx_int32_t bitrate;
693
1.84k
            speex_encoder_ctl(state, SPEEX_GET_BITRATE, &bitrate);
694
1.84k
            st->abr_drift+=(bitrate-st->abr_enabled);
695
1.84k
            st->abr_drift2 = .95*st->abr_drift2 + .05*(bitrate-st->abr_enabled);
696
1.84k
            st->abr_count += 1.0;
697
1.84k
         }
698
699
3.57k
      } else {
700
         /* VAD only */
701
856
         int modeid;
702
856
         if (st->relative_quality<2.0)
703
163
            modeid=1;
704
693
         else
705
693
            modeid=st->submodeSelect;
706
         /*speex_encoder_ctl(state, SPEEX_SET_MODE, &mode);*/
707
856
         st->submodeID=modeid;
708
709
856
      }
710
      /*fprintf (stderr, "%f %f\n", ratio, low_qual);*/
711
4.43k
   }
712
6.26k
#endif /* #ifndef DISABLE_VBR */
713
714
6.26k
   if (st->encode_submode)
715
6.26k
   {
716
6.26k
      speex_bits_pack(bits, 1, 1);
717
6.26k
      if (dtx)
718
260
         speex_bits_pack(bits, 0, SB_SUBMODE_BITS);
719
6.00k
      else
720
6.00k
         speex_bits_pack(bits, st->submodeID, SB_SUBMODE_BITS);
721
6.26k
   }
722
723
   /* If null mode (no transmission), just set a couple things to zero*/
724
6.26k
   if (dtx || st->submodes[st->submodeID] == NULL)
725
519
   {
726
104k
      for (i=0;i<st->frame_size;i++)
727
104k
         high[i]=VERY_SMALL;
728
729
4.67k
      for (i=0;i<st->lpcSize;i++)
730
4.15k
         st->mem_sw[i]=0;
731
519
      st->first=1;
732
733
      /* Final signal synthesis from excitation */
734
519
      iir_mem16(high, st->interp_qlpc, high, st->frame_size, st->lpcSize, st->mem_sp, stack);
735
736
519
      if (dtx)
737
260
         return 0;
738
259
      else
739
259
         return 1;
740
519
   }
741
742
743
   /* LSP quantization */
744
5.74k
   SUBMODE(lsp_quant)(lsp, qlsp, st->lpcSize, bits);
745
746
5.74k
   if (st->first)
747
1.58k
   {
748
14.2k
      for (i=0;i<st->lpcSize;i++)
749
12.6k
         st->old_lsp[i] = lsp[i];
750
14.2k
      for (i=0;i<st->lpcSize;i++)
751
12.6k
         st->old_qlsp[i] = qlsp[i];
752
1.58k
   }
753
754
5.74k
   ALLOC(mem, st->lpcSize, spx_mem_t);
755
5.74k
   ALLOC(syn_resp, st->subframeSize, spx_word16_t);
756
5.74k
   ALLOC(innov, st->subframeSize, spx_sig_t);
757
5.74k
   ALLOC(target, st->subframeSize, spx_word16_t);
758
759
28.7k
   for (sub=0;sub<st->nbSubframes;sub++)
760
22.9k
   {
761
22.9k
      VARDECL(spx_word16_t *exc);
762
22.9k
      VARDECL(spx_word16_t *res);
763
22.9k
      VARDECL(spx_word16_t *sw);
764
22.9k
      spx_word16_t *sp;
765
22.9k
      spx_word16_t filter_ratio;     /*Q7*/
766
22.9k
      int offset;
767
22.9k
      spx_word32_t rl, rh;           /*Q13*/
768
22.9k
      spx_word16_t eh=0;
769
770
22.9k
      offset = st->subframeSize*sub;
771
22.9k
      sp=high+offset;
772
22.9k
      ALLOC(exc, st->subframeSize, spx_word16_t);
773
22.9k
      ALLOC(res, st->subframeSize, spx_word16_t);
774
22.9k
      ALLOC(sw, st->subframeSize, spx_word16_t);
775
776
      /* LSP interpolation (quantized and unquantized) */
777
22.9k
      lsp_interpolate(st->old_lsp, lsp, interp_lsp, st->lpcSize, sub, st->nbSubframes, LSP_MARGIN);
778
22.9k
      lsp_interpolate(st->old_qlsp, qlsp, interp_qlsp, st->lpcSize, sub, st->nbSubframes, LSP_MARGIN);
779
780
22.9k
      lsp_to_lpc(interp_lsp, interp_lpc, st->lpcSize,stack);
781
22.9k
      lsp_to_lpc(interp_qlsp, st->interp_qlpc, st->lpcSize, stack);
782
783
22.9k
      bw_lpc(st->gamma1, interp_lpc, bw_lpc1, st->lpcSize);
784
22.9k
      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
22.9k
      st->pi_gain[sub]=LPC_SCALING;
789
22.9k
      rh = LPC_SCALING;
790
114k
      for (i=0;i<st->lpcSize;i+=2)
791
91.9k
      {
792
91.9k
         rh += st->interp_qlpc[i+1] - st->interp_qlpc[i];
793
91.9k
         st->pi_gain[sub] += st->interp_qlpc[i] + st->interp_qlpc[i+1];
794
91.9k
      }
795
796
22.9k
      rl = low_pi_gain[sub];
797
22.9k
#ifdef FIXED_POINT
798
22.9k
      filter_ratio=EXTRACT16(SATURATE(PDIV32(SHL32(ADD32(rl,82),7),ADD32(82,rh)),32767));
799
#else
800
      filter_ratio=(rl+.01)/(rh+.01);
801
#endif
802
803
      /* Compute "real excitation" */
804
22.9k
      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
22.9k
      eh = compute_rms16(exc, st->subframeSize);
808
809
22.9k
      if (!SUBMODE(innovation_quant)) {/* 1 for spectral folding excitation, 0 for stochastic */
810
17.1k
         spx_word32_t g;   /*Q7*/
811
17.1k
         spx_word16_t el;  /*Q0*/
812
17.1k
         el = low_innov_rms[sub];
813
814
         /* Gain to use if we want to use the low-band excitation for high-band */
815
17.1k
         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
17.1k
         {
836
17.1k
            int quant = scal_quant(g, fold_quant_bound, 32);
837
            /*speex_warning_int("tata", quant);*/
838
17.1k
            if (quant<0)
839
0
               quant=0;
840
17.1k
            if (quant>31)
841
0
               quant=31;
842
17.1k
            speex_bits_pack(bits, quant, 5);
843
17.1k
         }
844
17.1k
         if (st->innov_rms_save)
845
3.00k
         {
846
3.00k
            st->innov_rms_save[sub] = eh;
847
3.00k
         }
848
17.1k
         st->exc_rms[sub] = eh;
849
17.1k
      } else {
850
5.83k
         spx_word16_t gc;       /*Q7*/
851
5.83k
         spx_word32_t scale;    /*Q14*/
852
5.83k
         spx_word16_t el;       /*Q0*/
853
5.83k
         el = low_exc_rms[sub]; /*Q0*/
854
855
5.83k
         gc = PDIV32_16(MULT16_16(filter_ratio,1+eh),1+el);
856
857
         /* This is a kludge that cleans up a historical bug */
858
5.83k
         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
5.83k
         {
862
5.83k
            int qgc = scal_quant(gc, gc_quant_bound, 16);
863
5.83k
            speex_bits_pack(bits, qgc, 4);
864
5.83k
            gc = MULT16_16_Q15(QCONST16(0.87360,15),gc_quant_bound[qgc]);
865
5.83k
         }
866
5.83k
         if (st->subframeSize==80)
867
0
            gc = MULT16_16_P14(QCONST16(1.4142f,14), gc);
868
869
5.83k
         scale = SHL32(MULT16_16(PDIV32_16(SHL32(EXTEND32(gc),SIG_SHIFT-6),MAX16(EPSILON,filter_ratio)),(1+el)),6);
870
871
5.83k
         compute_impulse_response(st->interp_qlpc, bw_lpc1, bw_lpc2, syn_resp, st->subframeSize, st->lpcSize, stack);
872
873
874
         /* Reset excitation */
875
239k
         for (i=0;i<st->subframeSize;i++)
876
233k
            res[i]=VERY_SMALL;
877
878
         /* Compute zero response (ringing) of A(z/g1) / ( A(z/g2) * Aq(z) ) */
879
52.4k
         for (i=0;i<st->lpcSize;i++)
880
46.6k
            mem[i]=st->mem_sp[i];
881
5.83k
         iir_mem16(res, st->interp_qlpc, res, st->subframeSize, st->lpcSize, mem, stack);
882
883
52.4k
         for (i=0;i<st->lpcSize;i++)
884
46.6k
            mem[i]=st->mem_sw[i];
885
5.83k
         filter_mem16(res, bw_lpc1, bw_lpc2, res, st->subframeSize, st->lpcSize, mem, stack);
886
887
         /* Compute weighted signal */
888
52.4k
         for (i=0;i<st->lpcSize;i++)
889
46.6k
            mem[i]=st->mem_sw[i];
890
5.83k
         filter_mem16(sp, bw_lpc1, bw_lpc2, sw, st->subframeSize, st->lpcSize, mem, stack);
891
892
         /* Compute target signal */
893
239k
         for (i=0;i<st->subframeSize;i++)
894
233k
            target[i]=SUB16(sw[i],res[i]);
895
896
5.83k
         signal_div(target, target, scale, st->subframeSize);
897
898
         /* Reset excitation */
899
5.83k
         SPEEX_MEMSET(innov, 0, st->subframeSize);
900
901
         /*print_vec(target, st->subframeSize, "\ntarget");*/
902
5.83k
         SUBMODE(innovation_quant)(target, st->interp_qlpc, bw_lpc1, bw_lpc2,
903
5.83k
                                   SUBMODE(innovation_params), st->lpcSize, st->subframeSize,
904
5.83k
                                   innov, syn_resp, bits, stack, st->complexity, SUBMODE(double_codebook));
905
         /*print_vec(target, st->subframeSize, "after");*/
906
907
5.83k
         signal_mul(innov, innov, scale, st->subframeSize);
908
909
5.83k
         if (SUBMODE(double_codebook)) {
910
2.07k
            char *tmp_stack=stack;
911
2.07k
            VARDECL(spx_sig_t *innov2);
912
2.07k
            ALLOC(innov2, st->subframeSize, spx_sig_t);
913
2.07k
            SPEEX_MEMSET(innov2, 0, st->subframeSize);
914
85.1k
            for (i=0;i<st->subframeSize;i++)
915
83.0k
               target[i]=MULT16_16_P13(QCONST16(2.5f,13), target[i]);
916
917
2.07k
            SUBMODE(innovation_quant)(target, st->interp_qlpc, bw_lpc1, bw_lpc2,
918
2.07k
                                      SUBMODE(innovation_params), st->lpcSize, st->subframeSize,
919
2.07k
                                      innov2, syn_resp, bits, stack, st->complexity, 0);
920
2.07k
            signal_mul(innov2, innov2, MULT16_32_P15(QCONST16(0.4f,15),scale), st->subframeSize);
921
922
85.1k
            for (i=0;i<st->subframeSize;i++)
923
83.0k
               innov[i] = ADD32(innov[i],innov2[i]);
924
2.07k
            stack = tmp_stack;
925
2.07k
         }
926
239k
         for (i=0;i<st->subframeSize;i++)
927
233k
            exc[i] = PSHR32(innov[i],SIG_SHIFT);
928
929
5.83k
         if (st->innov_rms_save)
930
2.40k
         {
931
2.40k
            st->innov_rms_save[sub] = MULT16_16_Q15(QCONST16(.70711f, 15), compute_rms(innov, st->subframeSize));
932
2.40k
         }
933
5.83k
         st->exc_rms[sub] = compute_rms16(exc, st->subframeSize);
934
935
936
5.83k
      }
937
938
939
      /*Keep the previous memory*/
940
206k
      for (i=0;i<st->lpcSize;i++)
941
183k
         mem[i]=st->mem_sp[i];
942
      /* Final signal synthesis from excitation */
943
22.9k
      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
22.9k
      filter_mem16(sp, bw_lpc1, bw_lpc2, sw, st->subframeSize, st->lpcSize, st->mem_sw, stack);
947
22.9k
   }
948
949
51.7k
   for (i=0;i<st->lpcSize;i++)
950
45.9k
      st->old_lsp[i] = lsp[i];
951
51.7k
   for (i=0;i<st->lpcSize;i++)
952
45.9k
      st->old_qlsp[i] = qlsp[i];
953
954
5.74k
   st->first=0;
955
956
5.74k
   return 1;
957
6.26k
}
958
959
#endif /* DISABLE_ENCODER */
960
961
962
#ifndef DISABLE_DECODER
963
void *sb_decoder_init(const SpeexMode *m)
964
4.83k
{
965
4.83k
   spx_int32_t tmp;
966
4.83k
   SBDecState *st;
967
4.83k
   const SpeexSBMode *mode;
968
4.83k
   st = (SBDecState*)speex_alloc(sizeof(SBDecState));
969
4.83k
   if (!st)
970
0
      return NULL;
971
4.83k
   st->mode = m;
972
4.83k
   mode=(const SpeexSBMode*)m->mode;
973
4.83k
   st->encode_submode = 1;
974
975
4.83k
   st->st_low = speex_decoder_init(mode->nb_mode);
976
4.83k
#if defined(VAR_ARRAYS) || defined (USE_ALLOCA)
977
4.83k
   st->stack = NULL;
978
#else
979
   /*st->stack = (char*)speex_alloc_scratch(SB_DEC_STACK);*/
980
   speex_decoder_ctl(st->st_low, SPEEX_GET_STACK, &st->stack);
981
#endif
982
983
4.83k
   st->full_frame_size = 2*mode->frameSize;
984
4.83k
   st->frame_size = mode->frameSize;
985
4.83k
   st->subframeSize = mode->subframeSize;
986
4.83k
   st->nbSubframes = mode->frameSize/mode->subframeSize;
987
4.83k
   st->lpcSize=mode->lpcSize;
988
4.83k
   speex_decoder_ctl(st->st_low, SPEEX_GET_SAMPLING_RATE, &st->sampling_rate);
989
4.83k
   st->sampling_rate*=2;
990
4.83k
   tmp=1;
991
4.83k
   speex_decoder_ctl(st->st_low, SPEEX_SET_WIDEBAND, &tmp);
992
993
4.83k
   st->submodes=mode->submodes;
994
4.83k
   st->submodeID=mode->defaultSubmode;
995
996
4.83k
   st->first=1;
997
998
4.83k
   st->g0_mem = (spx_word16_t*)speex_alloc((QMF_ORDER)*sizeof(spx_word16_t));
999
4.83k
   st->g1_mem = (spx_word16_t*)speex_alloc((QMF_ORDER)*sizeof(spx_word16_t));
1000
1001
4.83k
   st->excBuf = (spx_word16_t*)speex_alloc((st->subframeSize)*sizeof(spx_word16_t));
1002
1003
4.83k
   st->old_qlsp = (spx_lsp_t*)speex_alloc((st->lpcSize)*sizeof(spx_lsp_t));
1004
4.83k
   st->interp_qlpc = (spx_coef_t*)speex_alloc(st->lpcSize*sizeof(spx_coef_t));
1005
1006
4.83k
   st->pi_gain = (spx_word32_t*)speex_alloc((st->nbSubframes)*sizeof(spx_word32_t));
1007
4.83k
   st->exc_rms = (spx_word16_t*)speex_alloc((st->nbSubframes)*sizeof(spx_word16_t));
1008
4.83k
   st->mem_sp = (spx_mem_t*)speex_alloc((2*st->lpcSize)*sizeof(spx_mem_t));
1009
1010
4.83k
   st->innov_save = NULL;
1011
1012
1013
4.83k
   st->lpc_enh_enabled=0;
1014
4.83k
   st->seed = 1000;
1015
1016
#ifdef ENABLE_VALGRIND
1017
   VALGRIND_MAKE_MEM_DEFINED(st, (st->stack-(char*)st));
1018
#endif
1019
4.83k
   return st;
1020
4.83k
}
1021
1022
void sb_decoder_destroy(void *state)
1023
4.83k
{
1024
4.83k
   SBDecState *st;
1025
4.83k
   st = (SBDecState*)state;
1026
4.83k
   speex_decoder_destroy(st->st_low);
1027
#if !(defined(VAR_ARRAYS) || defined (USE_ALLOCA))
1028
   /*speex_free_scratch(st->stack);*/
1029
#endif
1030
1031
4.83k
   speex_free(st->g0_mem);
1032
4.83k
   speex_free(st->g1_mem);
1033
4.83k
   speex_free(st->excBuf);
1034
4.83k
   speex_free(st->old_qlsp);
1035
4.83k
   speex_free(st->interp_qlpc);
1036
4.83k
   speex_free(st->pi_gain);
1037
4.83k
   speex_free(st->exc_rms);
1038
4.83k
   speex_free(st->mem_sp);
1039
1040
4.83k
   speex_free(state);
1041
4.83k
}
1042
1043
1044
1045
int sb_decoder_ctl(void *state, int request, void *ptr)
1046
124k
{
1047
124k
   SBDecState *st;
1048
124k
   st=(SBDecState*)state;
1049
124k
   switch(request)
1050
124k
   {
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
29.1k
   case SPEEX_GET_FRAME_SIZE:
1061
29.1k
      (*(spx_int32_t*)ptr) = st->full_frame_size;
1062
29.1k
      break;
1063
4.83k
   case SPEEX_SET_ENH:
1064
4.83k
      speex_decoder_ctl(st->st_low, request, ptr);
1065
4.83k
      st->lpc_enh_enabled = *((spx_int32_t*)ptr);
1066
4.83k
      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
4.83k
   case SPEEX_SET_SAMPLING_RATE:
1092
4.83k
      {
1093
4.83k
         spx_int32_t tmp=(*(spx_int32_t*)ptr);
1094
4.83k
         st->sampling_rate = tmp;
1095
4.83k
         tmp>>=1;
1096
4.83k
         speex_decoder_ctl(st->st_low, SPEEX_SET_SAMPLING_RATE, &tmp);
1097
4.83k
      }
1098
4.83k
      break;
1099
1.96k
   case SPEEX_GET_SAMPLING_RATE:
1100
1.96k
      (*(spx_int32_t*)ptr)=st->sampling_rate;
1101
1.96k
      break;
1102
2.54k
   case SPEEX_SET_HANDLER:
1103
2.54k
      speex_decoder_ctl(st->st_low, SPEEX_SET_HANDLER, ptr);
1104
2.54k
      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
4.62k
   case SPEEX_GET_LOOKAHEAD:
1126
4.62k
      speex_decoder_ctl(st->st_low, SPEEX_GET_LOOKAHEAD, ptr);
1127
4.62k
      (*(spx_int32_t*)ptr) = 2*(*(spx_int32_t*)ptr);
1128
4.62k
      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
2.08k
   case SPEEX_GET_PI_GAIN:
1139
2.08k
      {
1140
2.08k
         int i;
1141
2.08k
         spx_word32_t *g = (spx_word32_t*)ptr;
1142
10.4k
         for (i=0;i<st->nbSubframes;i++)
1143
8.34k
            g[i]=st->pi_gain[i];
1144
2.08k
      }
1145
2.08k
      break;
1146
2.08k
   case SPEEX_GET_EXC:
1147
2.08k
      {
1148
2.08k
         int i;
1149
10.4k
         for (i=0;i<st->nbSubframes;i++)
1150
8.34k
            ((spx_word16_t*)ptr)[i] = st->exc_rms[i];
1151
2.08k
      }
1152
2.08k
      break;
1153
35.2k
   case SPEEX_GET_DTX_STATUS:
1154
35.2k
      speex_decoder_ctl(st->st_low, SPEEX_GET_DTX_STATUS, ptr);
1155
35.2k
      break;
1156
35.2k
   case SPEEX_SET_INNOVATION_SAVE:
1157
35.2k
      st->innov_save = (spx_word16_t*)ptr;
1158
35.2k
      break;
1159
1.96k
   case SPEEX_SET_WIDEBAND:
1160
1.96k
      speex_decoder_ctl(st->st_low, SPEEX_SET_WIDEBAND, ptr);
1161
1.96k
      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
124k
   }
1169
124k
   return 0;
1170
124k
}
1171
1172
1173
static void sb_decode_lost(SBDecState *st, spx_word16_t *out, int dtx, char *stack)
1174
3.19k
{
1175
3.19k
   int i;
1176
3.19k
   int saved_modeid=0;
1177
1178
3.19k
   if (dtx)
1179
3.19k
   {
1180
3.19k
      saved_modeid=st->submodeID;
1181
3.19k
      st->submodeID=1;
1182
3.19k
   } else {
1183
0
      bw_lpc(QCONST16(0.99f,15), st->interp_qlpc, st->interp_qlpc, st->lpcSize);
1184
0
   }
1185
1186
3.19k
   st->first=1;
1187
1188
1189
   /* Final signal synthesis from excitation */
1190
3.19k
   if (!dtx)
1191
0
   {
1192
0
      st->last_ener =  MULT16_16_Q15(QCONST16(.9f,15),st->last_ener);
1193
0
   }
1194
803k
   for (i=0;i<st->frame_size;i++)
1195
800k
      out[i+st->frame_size] = speex_rand(st->last_ener, &st->seed);
1196
1197
3.19k
   iir_mem16(out+st->frame_size, st->interp_qlpc, out+st->frame_size, st->frame_size, st->lpcSize,
1198
3.19k
            st->mem_sp, stack);
1199
1200
1201
   /* Reconstruct the original */
1202
3.19k
   qmf_synth(out, out+st->frame_size, h0, out, st->full_frame_size, QMF_ORDER, st->g0_mem, st->g1_mem, stack);
1203
3.19k
   if (dtx)
1204
3.19k
   {
1205
3.19k
      st->submodeID=saved_modeid;
1206
3.19k
   }
1207
1208
3.19k
   return;
1209
3.19k
}
sb_celp.c:sb_decode_lost
Line
Count
Source
1174
1.59k
{
1175
1.59k
   int i;
1176
1.59k
   int saved_modeid=0;
1177
1178
1.59k
   if (dtx)
1179
1.59k
   {
1180
1.59k
      saved_modeid=st->submodeID;
1181
1.59k
      st->submodeID=1;
1182
1.59k
   } else {
1183
0
      bw_lpc(QCONST16(0.99f,15), st->interp_qlpc, st->interp_qlpc, st->lpcSize);
1184
0
   }
1185
1186
1.59k
   st->first=1;
1187
1188
1189
   /* Final signal synthesis from excitation */
1190
1.59k
   if (!dtx)
1191
0
   {
1192
0
      st->last_ener =  MULT16_16_Q15(QCONST16(.9f,15),st->last_ener);
1193
0
   }
1194
401k
   for (i=0;i<st->frame_size;i++)
1195
400k
      out[i+st->frame_size] = speex_rand(st->last_ener, &st->seed);
1196
1197
1.59k
   iir_mem16(out+st->frame_size, st->interp_qlpc, out+st->frame_size, st->frame_size, st->lpcSize,
1198
1.59k
            st->mem_sp, stack);
1199
1200
1201
   /* Reconstruct the original */
1202
1.59k
   qmf_synth(out, out+st->frame_size, h0, out, st->full_frame_size, QMF_ORDER, st->g0_mem, st->g1_mem, stack);
1203
1.59k
   if (dtx)
1204
1.59k
   {
1205
1.59k
      st->submodeID=saved_modeid;
1206
1.59k
   }
1207
1208
1.59k
   return;
1209
1.59k
}
sb_celp.c:sb_decode_lost
Line
Count
Source
1174
1.59k
{
1175
1.59k
   int i;
1176
1.59k
   int saved_modeid=0;
1177
1178
1.59k
   if (dtx)
1179
1.59k
   {
1180
1.59k
      saved_modeid=st->submodeID;
1181
1.59k
      st->submodeID=1;
1182
1.59k
   } else {
1183
0
      bw_lpc(QCONST16(0.99f,15), st->interp_qlpc, st->interp_qlpc, st->lpcSize);
1184
0
   }
1185
1186
1.59k
   st->first=1;
1187
1188
1189
   /* Final signal synthesis from excitation */
1190
1.59k
   if (!dtx)
1191
0
   {
1192
0
      st->last_ener =  MULT16_16_Q15(QCONST16(.9f,15),st->last_ener);
1193
0
   }
1194
401k
   for (i=0;i<st->frame_size;i++)
1195
400k
      out[i+st->frame_size] = speex_rand(st->last_ener, &st->seed);
1196
1197
1.59k
   iir_mem16(out+st->frame_size, st->interp_qlpc, out+st->frame_size, st->frame_size, st->lpcSize,
1198
1.59k
            st->mem_sp, stack);
1199
1200
1201
   /* Reconstruct the original */
1202
1.59k
   qmf_synth(out, out+st->frame_size, h0, out, st->full_frame_size, QMF_ORDER, st->g0_mem, st->g1_mem, stack);
1203
1.59k
   if (dtx)
1204
1.59k
   {
1205
1.59k
      st->submodeID=saved_modeid;
1206
1.59k
   }
1207
1208
1.59k
   return;
1209
1.59k
}
1210
1211
int sb_decode(void *state, SpeexBits *bits, void *vout)
1212
85.3k
{
1213
85.3k
   int i, sub;
1214
85.3k
   SBDecState *st;
1215
85.3k
   int wideband;
1216
85.3k
   int ret;
1217
85.3k
   char *stack;
1218
85.3k
   VARDECL(spx_word32_t *low_pi_gain);
1219
85.3k
   VARDECL(spx_word16_t *low_exc_rms);
1220
85.3k
   VARDECL(spx_coef_t *ak);
1221
85.3k
   VARDECL(spx_lsp_t *qlsp);
1222
85.3k
   VARDECL(spx_lsp_t *interp_qlsp);
1223
85.3k
   spx_int32_t dtx;
1224
85.3k
   const SpeexSBMode *mode;
1225
85.3k
   spx_word16_t *out = (spx_word16_t*)vout;
1226
85.3k
   spx_word16_t *low_innov_alias;
1227
85.3k
   spx_word32_t exc_ener_sum = 0;
1228
1229
85.3k
   st = (SBDecState*)state;
1230
85.3k
   stack=st->stack;
1231
85.3k
   mode = (const SpeexSBMode*)(st->mode->mode);
1232
1233
85.3k
   low_innov_alias = out+st->frame_size;
1234
85.3k
   speex_decoder_ctl(st->st_low, SPEEX_SET_INNOVATION_SAVE, low_innov_alias);
1235
   /* Decode the low-band */
1236
85.3k
   ret = speex_decode_native(st->st_low, bits, out);
1237
1238
85.3k
   speex_decoder_ctl(st->st_low, SPEEX_GET_DTX_STATUS, &dtx);
1239
1240
   /* If error decoding the narrowband part, propagate error */
1241
85.3k
   if (ret!=0)
1242
26.1k
   {
1243
26.1k
      return ret;
1244
26.1k
   }
1245
1246
59.2k
   if (!bits)
1247
0
   {
1248
0
      sb_decode_lost(st, out, dtx, stack);
1249
0
      return 0;
1250
0
   }
1251
1252
59.2k
   if (st->encode_submode)
1253
59.2k
   {
1254
1255
      /*Check "wideband bit"*/
1256
59.2k
      if (speex_bits_remaining(bits)>0)
1257
31.7k
         wideband = speex_bits_peek(bits);
1258
27.4k
      else
1259
27.4k
         wideband = 0;
1260
59.2k
      if (wideband)
1261
10.2k
      {
1262
         /*Regular wideband frame, read the submode*/
1263
10.2k
         wideband = speex_bits_unpack_unsigned(bits, 1);
1264
10.2k
         st->submodeID = speex_bits_unpack_unsigned(bits, SB_SUBMODE_BITS);
1265
10.2k
      } else
1266
49.0k
      {
1267
         /*Was a narrowband frame, set "null submode"*/
1268
49.0k
         st->submodeID = 0;
1269
49.0k
      }
1270
59.2k
      if (st->submodeID != 0 && st->submodes[st->submodeID] == NULL)
1271
791
      {
1272
791
         speex_notify("Invalid mode encountered. The stream is corrupted.");
1273
791
         return -2;
1274
791
      }
1275
59.2k
   }
1276
1277
   /* If null mode (no transmission), just set a couple things to zero*/
1278
58.4k
   if (st->submodes[st->submodeID] == NULL)
1279
51.7k
   {
1280
51.7k
      if (st->innov_save)
1281
20.0k
        SPEEX_MEMSET(st->innov_save, 0, st->full_frame_size);
1282
1283
51.7k
      if (dtx)
1284
1.59k
      {
1285
1.59k
         sb_decode_lost(st, out, 1, stack);
1286
1.59k
         return 0;
1287
1.59k
      }
1288
1289
11.4M
      for (i=0;i<st->frame_size;i++)
1290
11.3M
         out[st->frame_size+i]=VERY_SMALL;
1291
1292
50.1k
      st->first=1;
1293
1294
      /* Final signal synthesis from excitation */
1295
50.1k
      iir_mem16(out+st->frame_size, st->interp_qlpc, out+st->frame_size, st->frame_size, st->lpcSize, st->mem_sp, stack);
1296
1297
50.1k
      qmf_synth(out, out+st->frame_size, h0, out, st->full_frame_size, QMF_ORDER, st->g0_mem, st->g1_mem, stack);
1298
1299
50.1k
      return 0;
1300
1301
51.7k
   }
1302
1303
6.74k
   ALLOC(low_pi_gain, st->nbSubframes, spx_word32_t);
1304
6.74k
   ALLOC(low_exc_rms, st->nbSubframes, spx_word16_t);
1305
6.74k
   speex_decoder_ctl(st->st_low, SPEEX_GET_PI_GAIN, low_pi_gain);
1306
6.74k
   speex_decoder_ctl(st->st_low, SPEEX_GET_EXC, low_exc_rms);
1307
1308
6.74k
   ALLOC(qlsp, st->lpcSize, spx_lsp_t);
1309
6.74k
   ALLOC(interp_qlsp, st->lpcSize, spx_lsp_t);
1310
6.74k
   SUBMODE(lsp_unquant)(qlsp, st->lpcSize, bits);
1311
1312
6.74k
   if (st->first)
1313
3.05k
   {
1314
27.5k
      for (i=0;i<st->lpcSize;i++)
1315
24.4k
         st->old_qlsp[i] = qlsp[i];
1316
3.05k
   }
1317
1318
6.74k
   ALLOC(ak, st->lpcSize, spx_coef_t);
1319
1320
33.7k
   for (sub=0;sub<st->nbSubframes;sub++)
1321
26.9k
   {
1322
26.9k
      VARDECL(spx_word32_t *exc);
1323
26.9k
      spx_word16_t *innov_save=NULL;
1324
26.9k
      spx_word16_t *sp;
1325
26.9k
      spx_word16_t filter_ratio;
1326
26.9k
      spx_word16_t el=0;
1327
26.9k
      int offset;
1328
26.9k
      spx_word32_t rl=0,rh=0;
1329
1330
26.9k
      offset = st->subframeSize*sub;
1331
26.9k
      sp=out+st->frame_size+offset;
1332
26.9k
      ALLOC(exc, st->subframeSize, spx_word32_t);
1333
      /* Pointer for saving innovation */
1334
26.9k
      if (st->innov_save)
1335
16.1k
      {
1336
16.1k
         innov_save = st->innov_save+2*offset;
1337
16.1k
         SPEEX_MEMSET(innov_save, 0, 2*st->subframeSize);
1338
16.1k
      }
1339
1340
      /* LSP interpolation */
1341
26.9k
      lsp_interpolate(st->old_qlsp, qlsp, interp_qlsp, st->lpcSize, sub, st->nbSubframes, LSP_MARGIN);
1342
1343
      /* LSP to LPC */
1344
26.9k
      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
26.9k
         st->pi_gain[sub]=LPC_SCALING;
1350
26.9k
         rh = LPC_SCALING;
1351
134k
         for (i=0;i<st->lpcSize;i+=2)
1352
107k
         {
1353
107k
            rh += ak[i+1] - ak[i];
1354
107k
            st->pi_gain[sub] += ak[i] + ak[i+1];
1355
107k
         }
1356
1357
26.9k
         rl = low_pi_gain[sub];
1358
#ifdef FIXED_POINT
1359
16.1k
         filter_ratio=EXTRACT16(SATURATE(PDIV32(SHL32(ADD32(rl,82),7),ADD32(82,rh)),32767));
1360
#else
1361
         filter_ratio=(rl+.01)/(rh+.01);
1362
#endif
1363
1364
26.9k
      SPEEX_MEMSET(exc, 0, st->subframeSize);
1365
26.9k
      if (!SUBMODE(innovation_unquant))
1366
11.8k
      {
1367
11.8k
         spx_word32_t g;
1368
11.8k
         int quant;
1369
1370
11.8k
         quant = speex_bits_unpack_unsigned(bits, 5);
1371
11.8k
         g= spx_exp(MULT16_16(QCONST16(.125f,11),(quant-10)));
1372
1373
         /* Clamp to a minimum of epsilon to avoid division by 0 */
1374
11.8k
         g = PDIV32(g, MAX16(EPSILON,filter_ratio));
1375
1376
415k
         for (i=0;i<st->subframeSize;i+=2)
1377
403k
         {
1378
403k
            exc[i]=SHL32(MULT16_32_P15(MULT16_16_Q15(mode->folding_gain,low_innov_alias[offset+i]),SHL32(g,6)),SIG_SHIFT);
1379
403k
            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
403k
         }
1381
1382
15.1k
      } else {
1383
15.1k
         spx_word16_t gc;
1384
15.1k
         spx_word32_t scale;
1385
15.1k
         int qgc = speex_bits_unpack_unsigned(bits, 4);
1386
1387
15.1k
         el = low_exc_rms[sub];
1388
15.1k
         gc = MULT16_16_Q15(QCONST16(0.87360,15),gc_quant_bound[qgc]);
1389
1390
15.1k
         if (st->subframeSize==80)
1391
0
            gc = MULT16_16_P14(QCONST16(1.4142f,14),gc);
1392
1393
15.1k
         scale = SHL32(PDIV32(SHL32(MULT16_16(gc, el),3), MAX16(EPSILON,filter_ratio)),SIG_SHIFT-3);
1394
15.1k
         SUBMODE(innovation_unquant)(exc, SUBMODE(innovation_params), st->subframeSize,
1395
15.1k
                                     bits, stack, &st->seed);
1396
1397
15.1k
         signal_mul(exc,exc,scale,st->subframeSize);
1398
1399
15.1k
         if (SUBMODE(double_codebook)) {
1400
5.42k
            char *tmp_stack=stack;
1401
5.42k
            VARDECL(spx_sig_t *innov2);
1402
5.42k
            ALLOC(innov2, st->subframeSize, spx_sig_t);
1403
5.42k
            SPEEX_MEMSET(innov2, 0, st->subframeSize);
1404
5.42k
            SUBMODE(innovation_unquant)(innov2, SUBMODE(innovation_params), st->subframeSize,
1405
5.42k
                                        bits, stack, &st->seed);
1406
5.42k
            signal_mul(innov2, innov2, MULT16_32_P15(QCONST16(0.4f,15),scale), st->subframeSize);
1407
222k
            for (i=0;i<st->subframeSize;i++)
1408
216k
               exc[i] = ADD32(exc[i],innov2[i]);
1409
5.42k
            stack = tmp_stack;
1410
5.42k
         }
1411
1412
15.1k
      }
1413
1414
26.9k
      if (st->innov_save)
1415
16.1k
      {
1416
660k
         for (i=0;i<st->subframeSize;i++)
1417
644k
            innov_save[2*i]=EXTRACT16(PSHR32(exc[i],SIG_SHIFT));
1418
16.1k
      }
1419
1420
26.9k
      iir_mem16(st->excBuf, st->interp_qlpc, sp, st->subframeSize, st->lpcSize,
1421
26.9k
               st->mem_sp, stack);
1422
1.44M
      for (i=0;i<st->subframeSize;i++)
1423
1.41M
         st->excBuf[i]=EXTRACT16(PSHR32(exc[i],SIG_SHIFT));
1424
242k
      for (i=0;i<st->lpcSize;i++)
1425
215k
         st->interp_qlpc[i] = ak[i];
1426
26.9k
      st->exc_rms[sub] = compute_rms16(st->excBuf, st->subframeSize);
1427
26.9k
      exc_ener_sum = ADD32(exc_ener_sum, DIV32(MULT16_16(st->exc_rms[sub],st->exc_rms[sub]), st->nbSubframes));
1428
26.9k
   }
1429
6.74k
   st->last_ener = spx_sqrt(exc_ener_sum);
1430
1431
6.74k
   qmf_synth(out, out+st->frame_size, h0, out, st->full_frame_size, QMF_ORDER, st->g0_mem, st->g1_mem, stack);
1432
60.7k
   for (i=0;i<st->lpcSize;i++)
1433
53.9k
      st->old_qlsp[i] = qlsp[i];
1434
1435
6.74k
   st->first=0;
1436
1437
6.74k
   return 0;
1438
58.4k
}
sb_decode
Line
Count
Source
1212
42.0k
{
1213
42.0k
   int i, sub;
1214
42.0k
   SBDecState *st;
1215
42.0k
   int wideband;
1216
42.0k
   int ret;
1217
42.0k
   char *stack;
1218
42.0k
   VARDECL(spx_word32_t *low_pi_gain);
1219
42.0k
   VARDECL(spx_word16_t *low_exc_rms);
1220
42.0k
   VARDECL(spx_coef_t *ak);
1221
42.0k
   VARDECL(spx_lsp_t *qlsp);
1222
42.0k
   VARDECL(spx_lsp_t *interp_qlsp);
1223
42.0k
   spx_int32_t dtx;
1224
42.0k
   const SpeexSBMode *mode;
1225
42.0k
   spx_word16_t *out = (spx_word16_t*)vout;
1226
42.0k
   spx_word16_t *low_innov_alias;
1227
42.0k
   spx_word32_t exc_ener_sum = 0;
1228
1229
42.0k
   st = (SBDecState*)state;
1230
42.0k
   stack=st->stack;
1231
42.0k
   mode = (const SpeexSBMode*)(st->mode->mode);
1232
1233
42.0k
   low_innov_alias = out+st->frame_size;
1234
42.0k
   speex_decoder_ctl(st->st_low, SPEEX_SET_INNOVATION_SAVE, low_innov_alias);
1235
   /* Decode the low-band */
1236
42.0k
   ret = speex_decode_native(st->st_low, bits, out);
1237
1238
42.0k
   speex_decoder_ctl(st->st_low, SPEEX_GET_DTX_STATUS, &dtx);
1239
1240
   /* If error decoding the narrowband part, propagate error */
1241
42.0k
   if (ret!=0)
1242
15.0k
   {
1243
15.0k
      return ret;
1244
15.0k
   }
1245
1246
27.0k
   if (!bits)
1247
0
   {
1248
0
      sb_decode_lost(st, out, dtx, stack);
1249
0
      return 0;
1250
0
   }
1251
1252
27.0k
   if (st->encode_submode)
1253
27.0k
   {
1254
1255
      /*Check "wideband bit"*/
1256
27.0k
      if (speex_bits_remaining(bits)>0)
1257
14.3k
         wideband = speex_bits_peek(bits);
1258
12.7k
      else
1259
12.7k
         wideband = 0;
1260
27.0k
      if (wideband)
1261
4.55k
      {
1262
         /*Regular wideband frame, read the submode*/
1263
4.55k
         wideband = speex_bits_unpack_unsigned(bits, 1);
1264
4.55k
         st->submodeID = speex_bits_unpack_unsigned(bits, SB_SUBMODE_BITS);
1265
4.55k
      } else
1266
22.5k
      {
1267
         /*Was a narrowband frame, set "null submode"*/
1268
22.5k
         st->submodeID = 0;
1269
22.5k
      }
1270
27.0k
      if (st->submodeID != 0 && st->submodes[st->submodeID] == NULL)
1271
341
      {
1272
341
         speex_notify("Invalid mode encountered. The stream is corrupted.");
1273
341
         return -2;
1274
341
      }
1275
27.0k
   }
1276
1277
   /* If null mode (no transmission), just set a couple things to zero*/
1278
26.7k
   if (st->submodes[st->submodeID] == NULL)
1279
24.0k
   {
1280
24.0k
      if (st->innov_save)
1281
10.1k
        SPEEX_MEMSET(st->innov_save, 0, st->full_frame_size);
1282
1283
24.0k
      if (dtx)
1284
948
      {
1285
948
         sb_decode_lost(st, out, 1, stack);
1286
948
         return 0;
1287
948
      }
1288
1289
5.28M
      for (i=0;i<st->frame_size;i++)
1290
5.26M
         out[st->frame_size+i]=VERY_SMALL;
1291
1292
23.0k
      st->first=1;
1293
1294
      /* Final signal synthesis from excitation */
1295
23.0k
      iir_mem16(out+st->frame_size, st->interp_qlpc, out+st->frame_size, st->frame_size, st->lpcSize, st->mem_sp, stack);
1296
1297
23.0k
      qmf_synth(out, out+st->frame_size, h0, out, st->full_frame_size, QMF_ORDER, st->g0_mem, st->g1_mem, stack);
1298
1299
23.0k
      return 0;
1300
1301
24.0k
   }
1302
1303
2.71k
   ALLOC(low_pi_gain, st->nbSubframes, spx_word32_t);
1304
2.71k
   ALLOC(low_exc_rms, st->nbSubframes, spx_word16_t);
1305
2.71k
   speex_decoder_ctl(st->st_low, SPEEX_GET_PI_GAIN, low_pi_gain);
1306
2.71k
   speex_decoder_ctl(st->st_low, SPEEX_GET_EXC, low_exc_rms);
1307
1308
2.71k
   ALLOC(qlsp, st->lpcSize, spx_lsp_t);
1309
2.71k
   ALLOC(interp_qlsp, st->lpcSize, spx_lsp_t);
1310
2.71k
   SUBMODE(lsp_unquant)(qlsp, st->lpcSize, bits);
1311
1312
2.71k
   if (st->first)
1313
1.23k
   {
1314
11.1k
      for (i=0;i<st->lpcSize;i++)
1315
9.89k
         st->old_qlsp[i] = qlsp[i];
1316
1.23k
   }
1317
1318
2.71k
   ALLOC(ak, st->lpcSize, spx_coef_t);
1319
1320
13.5k
   for (sub=0;sub<st->nbSubframes;sub++)
1321
10.8k
   {
1322
10.8k
      VARDECL(spx_word32_t *exc);
1323
10.8k
      spx_word16_t *innov_save=NULL;
1324
10.8k
      spx_word16_t *sp;
1325
10.8k
      spx_word16_t filter_ratio;
1326
10.8k
      spx_word16_t el=0;
1327
10.8k
      int offset;
1328
10.8k
      spx_word32_t rl=0,rh=0;
1329
1330
10.8k
      offset = st->subframeSize*sub;
1331
10.8k
      sp=out+st->frame_size+offset;
1332
10.8k
      ALLOC(exc, st->subframeSize, spx_word32_t);
1333
      /* Pointer for saving innovation */
1334
10.8k
      if (st->innov_save)
1335
5.72k
      {
1336
5.72k
         innov_save = st->innov_save+2*offset;
1337
5.72k
         SPEEX_MEMSET(innov_save, 0, 2*st->subframeSize);
1338
5.72k
      }
1339
1340
      /* LSP interpolation */
1341
10.8k
      lsp_interpolate(st->old_qlsp, qlsp, interp_qlsp, st->lpcSize, sub, st->nbSubframes, LSP_MARGIN);
1342
1343
      /* LSP to LPC */
1344
10.8k
      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
10.8k
         st->pi_gain[sub]=LPC_SCALING;
1350
10.8k
         rh = LPC_SCALING;
1351
54.3k
         for (i=0;i<st->lpcSize;i+=2)
1352
43.4k
         {
1353
43.4k
            rh += ak[i+1] - ak[i];
1354
43.4k
            st->pi_gain[sub] += ak[i] + ak[i+1];
1355
43.4k
         }
1356
1357
10.8k
         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
10.8k
         filter_ratio=(rl+.01)/(rh+.01);
1362
10.8k
#endif
1363
1364
10.8k
      SPEEX_MEMSET(exc, 0, st->subframeSize);
1365
10.8k
      if (!SUBMODE(innovation_unquant))
1366
6.16k
      {
1367
6.16k
         spx_word32_t g;
1368
6.16k
         int quant;
1369
1370
6.16k
         quant = speex_bits_unpack_unsigned(bits, 5);
1371
6.16k
         g= spx_exp(MULT16_16(QCONST16(.125f,11),(quant-10)));
1372
1373
         /* Clamp to a minimum of epsilon to avoid division by 0 */
1374
6.16k
         g = PDIV32(g, MAX16(EPSILON,filter_ratio));
1375
1376
223k
         for (i=0;i<st->subframeSize;i+=2)
1377
217k
         {
1378
217k
            exc[i]=SHL32(MULT16_32_P15(MULT16_16_Q15(mode->folding_gain,low_innov_alias[offset+i]),SHL32(g,6)),SIG_SHIFT);
1379
217k
            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
217k
         }
1381
1382
6.16k
      } else {
1383
4.70k
         spx_word16_t gc;
1384
4.70k
         spx_word32_t scale;
1385
4.70k
         int qgc = speex_bits_unpack_unsigned(bits, 4);
1386
1387
4.70k
         el = low_exc_rms[sub];
1388
4.70k
         gc = MULT16_16_Q15(QCONST16(0.87360,15),gc_quant_bound[qgc]);
1389
1390
4.70k
         if (st->subframeSize==80)
1391
0
            gc = MULT16_16_P14(QCONST16(1.4142f,14),gc);
1392
1393
4.70k
         scale = SHL32(PDIV32(SHL32(MULT16_16(gc, el),3), MAX16(EPSILON,filter_ratio)),SIG_SHIFT-3);
1394
4.70k
         SUBMODE(innovation_unquant)(exc, SUBMODE(innovation_params), st->subframeSize,
1395
4.70k
                                     bits, stack, &st->seed);
1396
1397
4.70k
         signal_mul(exc,exc,scale,st->subframeSize);
1398
1399
4.70k
         if (SUBMODE(double_codebook)) {
1400
1.73k
            char *tmp_stack=stack;
1401
1.73k
            VARDECL(spx_sig_t *innov2);
1402
1.73k
            ALLOC(innov2, st->subframeSize, spx_sig_t);
1403
1.73k
            SPEEX_MEMSET(innov2, 0, st->subframeSize);
1404
1.73k
            SUBMODE(innovation_unquant)(innov2, SUBMODE(innovation_params), st->subframeSize,
1405
1.73k
                                        bits, stack, &st->seed);
1406
1.73k
            signal_mul(innov2, innov2, MULT16_32_P15(QCONST16(0.4f,15),scale), st->subframeSize);
1407
71.0k
            for (i=0;i<st->subframeSize;i++)
1408
69.2k
               exc[i] = ADD32(exc[i],innov2[i]);
1409
1.73k
            stack = tmp_stack;
1410
1.73k
         }
1411
1412
4.70k
      }
1413
1414
10.8k
      if (st->innov_save)
1415
5.72k
      {
1416
234k
         for (i=0;i<st->subframeSize;i++)
1417
228k
            innov_save[2*i]=EXTRACT16(PSHR32(exc[i],SIG_SHIFT));
1418
5.72k
      }
1419
1420
10.8k
      iir_mem16(st->excBuf, st->interp_qlpc, sp, st->subframeSize, st->lpcSize,
1421
10.8k
               st->mem_sp, stack);
1422
634k
      for (i=0;i<st->subframeSize;i++)
1423
623k
         st->excBuf[i]=EXTRACT16(PSHR32(exc[i],SIG_SHIFT));
1424
97.8k
      for (i=0;i<st->lpcSize;i++)
1425
86.9k
         st->interp_qlpc[i] = ak[i];
1426
10.8k
      st->exc_rms[sub] = compute_rms16(st->excBuf, st->subframeSize);
1427
10.8k
      exc_ener_sum = ADD32(exc_ener_sum, DIV32(MULT16_16(st->exc_rms[sub],st->exc_rms[sub]), st->nbSubframes));
1428
10.8k
   }
1429
2.71k
   st->last_ener = spx_sqrt(exc_ener_sum);
1430
1431
2.71k
   qmf_synth(out, out+st->frame_size, h0, out, st->full_frame_size, QMF_ORDER, st->g0_mem, st->g1_mem, stack);
1432
24.4k
   for (i=0;i<st->lpcSize;i++)
1433
21.7k
      st->old_qlsp[i] = qlsp[i];
1434
1435
2.71k
   st->first=0;
1436
1437
2.71k
   return 0;
1438
26.7k
}
sb_decode
Line
Count
Source
1212
43.2k
{
1213
43.2k
   int i, sub;
1214
43.2k
   SBDecState *st;
1215
43.2k
   int wideband;
1216
43.2k
   int ret;
1217
43.2k
   char *stack;
1218
43.2k
   VARDECL(spx_word32_t *low_pi_gain);
1219
43.2k
   VARDECL(spx_word16_t *low_exc_rms);
1220
43.2k
   VARDECL(spx_coef_t *ak);
1221
43.2k
   VARDECL(spx_lsp_t *qlsp);
1222
43.2k
   VARDECL(spx_lsp_t *interp_qlsp);
1223
43.2k
   spx_int32_t dtx;
1224
43.2k
   const SpeexSBMode *mode;
1225
43.2k
   spx_word16_t *out = (spx_word16_t*)vout;
1226
43.2k
   spx_word16_t *low_innov_alias;
1227
43.2k
   spx_word32_t exc_ener_sum = 0;
1228
1229
43.2k
   st = (SBDecState*)state;
1230
43.2k
   stack=st->stack;
1231
43.2k
   mode = (const SpeexSBMode*)(st->mode->mode);
1232
1233
43.2k
   low_innov_alias = out+st->frame_size;
1234
43.2k
   speex_decoder_ctl(st->st_low, SPEEX_SET_INNOVATION_SAVE, low_innov_alias);
1235
   /* Decode the low-band */
1236
43.2k
   ret = speex_decode_native(st->st_low, bits, out);
1237
1238
43.2k
   speex_decoder_ctl(st->st_low, SPEEX_GET_DTX_STATUS, &dtx);
1239
1240
   /* If error decoding the narrowband part, propagate error */
1241
43.2k
   if (ret!=0)
1242
11.1k
   {
1243
11.1k
      return ret;
1244
11.1k
   }
1245
1246
32.1k
   if (!bits)
1247
0
   {
1248
0
      sb_decode_lost(st, out, dtx, stack);
1249
0
      return 0;
1250
0
   }
1251
1252
32.1k
   if (st->encode_submode)
1253
32.1k
   {
1254
1255
      /*Check "wideband bit"*/
1256
32.1k
      if (speex_bits_remaining(bits)>0)
1257
17.4k
         wideband = speex_bits_peek(bits);
1258
14.7k
      else
1259
14.7k
         wideband = 0;
1260
32.1k
      if (wideband)
1261
5.64k
      {
1262
         /*Regular wideband frame, read the submode*/
1263
5.64k
         wideband = speex_bits_unpack_unsigned(bits, 1);
1264
5.64k
         st->submodeID = speex_bits_unpack_unsigned(bits, SB_SUBMODE_BITS);
1265
5.64k
      } else
1266
26.5k
      {
1267
         /*Was a narrowband frame, set "null submode"*/
1268
26.5k
         st->submodeID = 0;
1269
26.5k
      }
1270
32.1k
      if (st->submodeID != 0 && st->submodes[st->submodeID] == NULL)
1271
450
      {
1272
450
         speex_notify("Invalid mode encountered. The stream is corrupted.");
1273
450
         return -2;
1274
450
      }
1275
32.1k
   }
1276
1277
   /* If null mode (no transmission), just set a couple things to zero*/
1278
31.7k
   if (st->submodes[st->submodeID] == NULL)
1279
27.6k
   {
1280
27.6k
      if (st->innov_save)
1281
9.93k
        SPEEX_MEMSET(st->innov_save, 0, st->full_frame_size);
1282
1283
27.6k
      if (dtx)
1284
647
      {
1285
647
         sb_decode_lost(st, out, 1, stack);
1286
647
         return 0;
1287
647
      }
1288
1289
6.14M
      for (i=0;i<st->frame_size;i++)
1290
6.11M
         out[st->frame_size+i]=VERY_SMALL;
1291
1292
27.0k
      st->first=1;
1293
1294
      /* Final signal synthesis from excitation */
1295
27.0k
      iir_mem16(out+st->frame_size, st->interp_qlpc, out+st->frame_size, st->frame_size, st->lpcSize, st->mem_sp, stack);
1296
1297
27.0k
      qmf_synth(out, out+st->frame_size, h0, out, st->full_frame_size, QMF_ORDER, st->g0_mem, st->g1_mem, stack);
1298
1299
27.0k
      return 0;
1300
1301
27.6k
   }
1302
1303
4.02k
   ALLOC(low_pi_gain, st->nbSubframes, spx_word32_t);
1304
4.02k
   ALLOC(low_exc_rms, st->nbSubframes, spx_word16_t);
1305
4.02k
   speex_decoder_ctl(st->st_low, SPEEX_GET_PI_GAIN, low_pi_gain);
1306
4.02k
   speex_decoder_ctl(st->st_low, SPEEX_GET_EXC, low_exc_rms);
1307
1308
4.02k
   ALLOC(qlsp, st->lpcSize, spx_lsp_t);
1309
4.02k
   ALLOC(interp_qlsp, st->lpcSize, spx_lsp_t);
1310
4.02k
   SUBMODE(lsp_unquant)(qlsp, st->lpcSize, bits);
1311
1312
4.02k
   if (st->first)
1313
1.81k
   {
1314
16.3k
      for (i=0;i<st->lpcSize;i++)
1315
14.5k
         st->old_qlsp[i] = qlsp[i];
1316
1.81k
   }
1317
1318
4.02k
   ALLOC(ak, st->lpcSize, spx_coef_t);
1319
1320
20.1k
   for (sub=0;sub<st->nbSubframes;sub++)
1321
16.1k
   {
1322
16.1k
      VARDECL(spx_word32_t *exc);
1323
16.1k
      spx_word16_t *innov_save=NULL;
1324
16.1k
      spx_word16_t *sp;
1325
16.1k
      spx_word16_t filter_ratio;
1326
16.1k
      spx_word16_t el=0;
1327
16.1k
      int offset;
1328
16.1k
      spx_word32_t rl=0,rh=0;
1329
1330
16.1k
      offset = st->subframeSize*sub;
1331
16.1k
      sp=out+st->frame_size+offset;
1332
16.1k
      ALLOC(exc, st->subframeSize, spx_word32_t);
1333
      /* Pointer for saving innovation */
1334
16.1k
      if (st->innov_save)
1335
10.3k
      {
1336
10.3k
         innov_save = st->innov_save+2*offset;
1337
10.3k
         SPEEX_MEMSET(innov_save, 0, 2*st->subframeSize);
1338
10.3k
      }
1339
1340
      /* LSP interpolation */
1341
16.1k
      lsp_interpolate(st->old_qlsp, qlsp, interp_qlsp, st->lpcSize, sub, st->nbSubframes, LSP_MARGIN);
1342
1343
      /* LSP to LPC */
1344
16.1k
      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
16.1k
         st->pi_gain[sub]=LPC_SCALING;
1350
16.1k
         rh = LPC_SCALING;
1351
80.5k
         for (i=0;i<st->lpcSize;i+=2)
1352
64.4k
         {
1353
64.4k
            rh += ak[i+1] - ak[i];
1354
64.4k
            st->pi_gain[sub] += ak[i] + ak[i+1];
1355
64.4k
         }
1356
1357
16.1k
         rl = low_pi_gain[sub];
1358
16.1k
#ifdef FIXED_POINT
1359
16.1k
         filter_ratio=EXTRACT16(SATURATE(PDIV32(SHL32(ADD32(rl,82),7),ADD32(82,rh)),32767));
1360
#else
1361
         filter_ratio=(rl+.01)/(rh+.01);
1362
#endif
1363
1364
16.1k
      SPEEX_MEMSET(exc, 0, st->subframeSize);
1365
16.1k
      if (!SUBMODE(innovation_unquant))
1366
5.65k
      {
1367
5.65k
         spx_word32_t g;
1368
5.65k
         int quant;
1369
1370
5.65k
         quant = speex_bits_unpack_unsigned(bits, 5);
1371
5.65k
         g= spx_exp(MULT16_16(QCONST16(.125f,11),(quant-10)));
1372
1373
         /* Clamp to a minimum of epsilon to avoid division by 0 */
1374
5.65k
         g = PDIV32(g, MAX16(EPSILON,filter_ratio));
1375
1376
191k
         for (i=0;i<st->subframeSize;i+=2)
1377
185k
         {
1378
185k
            exc[i]=SHL32(MULT16_32_P15(MULT16_16_Q15(mode->folding_gain,low_innov_alias[offset+i]),SHL32(g,6)),SIG_SHIFT);
1379
185k
            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
185k
         }
1381
1382
10.4k
      } else {
1383
10.4k
         spx_word16_t gc;
1384
10.4k
         spx_word32_t scale;
1385
10.4k
         int qgc = speex_bits_unpack_unsigned(bits, 4);
1386
1387
10.4k
         el = low_exc_rms[sub];
1388
10.4k
         gc = MULT16_16_Q15(QCONST16(0.87360,15),gc_quant_bound[qgc]);
1389
1390
10.4k
         if (st->subframeSize==80)
1391
0
            gc = MULT16_16_P14(QCONST16(1.4142f,14),gc);
1392
1393
10.4k
         scale = SHL32(PDIV32(SHL32(MULT16_16(gc, el),3), MAX16(EPSILON,filter_ratio)),SIG_SHIFT-3);
1394
10.4k
         SUBMODE(innovation_unquant)(exc, SUBMODE(innovation_params), st->subframeSize,
1395
10.4k
                                     bits, stack, &st->seed);
1396
1397
10.4k
         signal_mul(exc,exc,scale,st->subframeSize);
1398
1399
10.4k
         if (SUBMODE(double_codebook)) {
1400
3.69k
            char *tmp_stack=stack;
1401
3.69k
            VARDECL(spx_sig_t *innov2);
1402
3.69k
            ALLOC(innov2, st->subframeSize, spx_sig_t);
1403
3.69k
            SPEEX_MEMSET(innov2, 0, st->subframeSize);
1404
3.69k
            SUBMODE(innovation_unquant)(innov2, SUBMODE(innovation_params), st->subframeSize,
1405
3.69k
                                        bits, stack, &st->seed);
1406
3.69k
            signal_mul(innov2, innov2, MULT16_32_P15(QCONST16(0.4f,15),scale), st->subframeSize);
1407
151k
            for (i=0;i<st->subframeSize;i++)
1408
147k
               exc[i] = ADD32(exc[i],innov2[i]);
1409
3.69k
            stack = tmp_stack;
1410
3.69k
         }
1411
1412
10.4k
      }
1413
1414
16.1k
      if (st->innov_save)
1415
10.3k
      {
1416
426k
         for (i=0;i<st->subframeSize;i++)
1417
415k
            innov_save[2*i]=EXTRACT16(PSHR32(exc[i],SIG_SHIFT));
1418
10.3k
      }
1419
1420
16.1k
      iir_mem16(st->excBuf, st->interp_qlpc, sp, st->subframeSize, st->lpcSize,
1421
16.1k
               st->mem_sp, stack);
1422
806k
      for (i=0;i<st->subframeSize;i++)
1423
790k
         st->excBuf[i]=EXTRACT16(PSHR32(exc[i],SIG_SHIFT));
1424
145k
      for (i=0;i<st->lpcSize;i++)
1425
128k
         st->interp_qlpc[i] = ak[i];
1426
16.1k
      st->exc_rms[sub] = compute_rms16(st->excBuf, st->subframeSize);
1427
16.1k
      exc_ener_sum = ADD32(exc_ener_sum, DIV32(MULT16_16(st->exc_rms[sub],st->exc_rms[sub]), st->nbSubframes));
1428
16.1k
   }
1429
4.02k
   st->last_ener = spx_sqrt(exc_ener_sum);
1430
1431
4.02k
   qmf_synth(out, out+st->frame_size, h0, out, st->full_frame_size, QMF_ORDER, st->g0_mem, st->g1_mem, stack);
1432
36.2k
   for (i=0;i<st->lpcSize;i++)
1433
32.2k
      st->old_qlsp[i] = qlsp[i];
1434
1435
4.02k
   st->first=0;
1436
1437
4.02k
   return 0;
1438
31.7k
}
1439
#endif /* DISABLE_DECODER */
1440
1441
1442
1443
#endif
1444