Coverage Report

Created: 2025-07-23 06:50

/src/speex/libspeex/sb_celp.c
Line
Count
Source (jump to first uncovered line)
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
239k
#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
64.7k
#define LSP_MARGIN 410
77
7.01k
#define LSP_DELTA1 6553
78
150
#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.7k
#define LSP_MARGIN .05
92
3.58k
#define LSP_DELTA1 .2
93
1
#define LSP_DELTA2 .05
94
95
#endif
96
97
93.0k
#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.52k
{
146
5.52k
   int i;
147
5.52k
   spx_int32_t tmp;
148
5.52k
   SBEncState *st;
149
5.52k
   const SpeexSBMode *mode;
150
151
5.52k
   st = (SBEncState*)speex_alloc(sizeof(SBEncState));
152
5.52k
   if (!st)
153
0
      return NULL;
154
5.52k
   st->mode = m;
155
5.52k
   mode = (const SpeexSBMode*)m->mode;
156
157
158
5.52k
   st->st_low = speex_encoder_init(mode->nb_mode);
159
5.52k
#if defined(VAR_ARRAYS) || defined (USE_ALLOCA)
160
5.52k
   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.52k
   st->full_frame_size = 2*mode->frameSize;
167
5.52k
   st->frame_size = mode->frameSize;
168
5.52k
   st->subframeSize = mode->subframeSize;
169
5.52k
   st->nbSubframes = mode->frameSize/mode->subframeSize;
170
5.52k
   st->windowSize = st->frame_size+st->subframeSize;
171
5.52k
   st->lpcSize=mode->lpcSize;
172
173
5.52k
   st->encode_submode = 1;
174
5.52k
   st->submodes=mode->submodes;
175
5.52k
   st->submodeSelect = st->submodeID=mode->defaultSubmode;
176
177
5.52k
   tmp=9;
178
5.52k
   speex_encoder_ctl(st->st_low, SPEEX_SET_QUALITY, &tmp);
179
5.52k
   tmp=1;
180
5.52k
   speex_encoder_ctl(st->st_low, SPEEX_SET_WIDEBAND, &tmp);
181
182
5.52k
   st->lpc_floor = mode->lpc_floor;
183
5.52k
   st->gamma1=mode->gamma1;
184
5.52k
   st->gamma2=mode->gamma2;
185
5.52k
   st->first=1;
186
187
5.52k
   st->high=(spx_word16_t*)speex_alloc((st->windowSize-st->frame_size)*sizeof(spx_word16_t));
188
189
5.52k
   st->h0_mem=(spx_word16_t*)speex_alloc((QMF_ORDER)*sizeof(spx_word16_t));
190
191
5.52k
   st->window= lpc_window;
192
193
5.52k
   st->lagWindow = lag_window;
194
195
5.52k
   st->old_lsp = (spx_lsp_t*)speex_alloc(st->lpcSize*sizeof(spx_lsp_t));
196
5.52k
   st->old_qlsp = (spx_lsp_t*)speex_alloc(st->lpcSize*sizeof(spx_lsp_t));
197
5.52k
   st->interp_qlpc = (spx_coef_t*)speex_alloc(st->lpcSize*sizeof(spx_coef_t));
198
5.52k
   st->pi_gain = (spx_word32_t*)speex_alloc((st->nbSubframes)*sizeof(spx_word32_t));
199
5.52k
   st->exc_rms = (spx_word16_t*)speex_alloc((st->nbSubframes)*sizeof(spx_word16_t));
200
5.52k
   st->innov_rms_save = NULL;
201
202
5.52k
   st->mem_sp = (spx_mem_t*)speex_alloc((st->lpcSize)*sizeof(spx_mem_t));
203
5.52k
   st->mem_sp2 = (spx_mem_t*)speex_alloc((st->lpcSize)*sizeof(spx_mem_t));
204
5.52k
   st->mem_sw = (spx_mem_t*)speex_alloc((st->lpcSize)*sizeof(spx_mem_t));
205
206
49.7k
   for (i=0;i<st->lpcSize;i++)
207
44.2k
      st->old_lsp[i]= DIV32(MULT16_16(QCONST16(3.1415927f, LSP_SHIFT), i+1), st->lpcSize+1);
208
209
5.52k
#ifndef DISABLE_VBR
210
5.52k
   st->vbr_quality = 8;
211
5.52k
   st->vbr_enabled = 0;
212
5.52k
   st->vbr_max = 0;
213
5.52k
   st->vbr_max_high = 20000;  /* We just need a big value here */
214
5.52k
   st->vad_enabled = 0;
215
5.52k
   st->abr_enabled = 0;
216
5.52k
   st->relative_quality=0;
217
5.52k
#endif /* #ifndef DISABLE_VBR */
218
219
5.52k
   st->complexity=2;
220
5.52k
   speex_encoder_ctl(st->st_low, SPEEX_GET_SAMPLING_RATE, &st->sampling_rate);
221
5.52k
   st->sampling_rate*=2;
222
#ifdef ENABLE_VALGRIND
223
   VALGRIND_MAKE_MEM_DEFINED(st, (st->stack-(char*)st));
224
#endif
225
5.52k
   return st;
226
5.52k
}
sb_encoder_init
Line
Count
Source
145
2.76k
{
146
2.76k
   int i;
147
2.76k
   spx_int32_t tmp;
148
2.76k
   SBEncState *st;
149
2.76k
   const SpeexSBMode *mode;
150
151
2.76k
   st = (SBEncState*)speex_alloc(sizeof(SBEncState));
152
2.76k
   if (!st)
153
0
      return NULL;
154
2.76k
   st->mode = m;
155
2.76k
   mode = (const SpeexSBMode*)m->mode;
156
157
158
2.76k
   st->st_low = speex_encoder_init(mode->nb_mode);
159
2.76k
#if defined(VAR_ARRAYS) || defined (USE_ALLOCA)
160
2.76k
   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.76k
   st->full_frame_size = 2*mode->frameSize;
167
2.76k
   st->frame_size = mode->frameSize;
168
2.76k
   st->subframeSize = mode->subframeSize;
169
2.76k
   st->nbSubframes = mode->frameSize/mode->subframeSize;
170
2.76k
   st->windowSize = st->frame_size+st->subframeSize;
171
2.76k
   st->lpcSize=mode->lpcSize;
172
173
2.76k
   st->encode_submode = 1;
174
2.76k
   st->submodes=mode->submodes;
175
2.76k
   st->submodeSelect = st->submodeID=mode->defaultSubmode;
176
177
2.76k
   tmp=9;
178
2.76k
   speex_encoder_ctl(st->st_low, SPEEX_SET_QUALITY, &tmp);
179
2.76k
   tmp=1;
180
2.76k
   speex_encoder_ctl(st->st_low, SPEEX_SET_WIDEBAND, &tmp);
181
182
2.76k
   st->lpc_floor = mode->lpc_floor;
183
2.76k
   st->gamma1=mode->gamma1;
184
2.76k
   st->gamma2=mode->gamma2;
185
2.76k
   st->first=1;
186
187
2.76k
   st->high=(spx_word16_t*)speex_alloc((st->windowSize-st->frame_size)*sizeof(spx_word16_t));
188
189
2.76k
   st->h0_mem=(spx_word16_t*)speex_alloc((QMF_ORDER)*sizeof(spx_word16_t));
190
191
2.76k
   st->window= lpc_window;
192
193
2.76k
   st->lagWindow = lag_window;
194
195
2.76k
   st->old_lsp = (spx_lsp_t*)speex_alloc(st->lpcSize*sizeof(spx_lsp_t));
196
2.76k
   st->old_qlsp = (spx_lsp_t*)speex_alloc(st->lpcSize*sizeof(spx_lsp_t));
197
2.76k
   st->interp_qlpc = (spx_coef_t*)speex_alloc(st->lpcSize*sizeof(spx_coef_t));
198
2.76k
   st->pi_gain = (spx_word32_t*)speex_alloc((st->nbSubframes)*sizeof(spx_word32_t));
199
2.76k
   st->exc_rms = (spx_word16_t*)speex_alloc((st->nbSubframes)*sizeof(spx_word16_t));
200
2.76k
   st->innov_rms_save = NULL;
201
202
2.76k
   st->mem_sp = (spx_mem_t*)speex_alloc((st->lpcSize)*sizeof(spx_mem_t));
203
2.76k
   st->mem_sp2 = (spx_mem_t*)speex_alloc((st->lpcSize)*sizeof(spx_mem_t));
204
2.76k
   st->mem_sw = (spx_mem_t*)speex_alloc((st->lpcSize)*sizeof(spx_mem_t));
205
206
24.8k
   for (i=0;i<st->lpcSize;i++)
207
22.1k
      st->old_lsp[i]= DIV32(MULT16_16(QCONST16(3.1415927f, LSP_SHIFT), i+1), st->lpcSize+1);
208
209
2.76k
#ifndef DISABLE_VBR
210
2.76k
   st->vbr_quality = 8;
211
2.76k
   st->vbr_enabled = 0;
212
2.76k
   st->vbr_max = 0;
213
2.76k
   st->vbr_max_high = 20000;  /* We just need a big value here */
214
2.76k
   st->vad_enabled = 0;
215
2.76k
   st->abr_enabled = 0;
216
2.76k
   st->relative_quality=0;
217
2.76k
#endif /* #ifndef DISABLE_VBR */
218
219
2.76k
   st->complexity=2;
220
2.76k
   speex_encoder_ctl(st->st_low, SPEEX_GET_SAMPLING_RATE, &st->sampling_rate);
221
2.76k
   st->sampling_rate*=2;
222
#ifdef ENABLE_VALGRIND
223
   VALGRIND_MAKE_MEM_DEFINED(st, (st->stack-(char*)st));
224
#endif
225
2.76k
   return st;
226
2.76k
}
sb_encoder_init
Line
Count
Source
145
2.76k
{
146
2.76k
   int i;
147
2.76k
   spx_int32_t tmp;
148
2.76k
   SBEncState *st;
149
2.76k
   const SpeexSBMode *mode;
150
151
2.76k
   st = (SBEncState*)speex_alloc(sizeof(SBEncState));
152
2.76k
   if (!st)
153
0
      return NULL;
154
2.76k
   st->mode = m;
155
2.76k
   mode = (const SpeexSBMode*)m->mode;
156
157
158
2.76k
   st->st_low = speex_encoder_init(mode->nb_mode);
159
2.76k
#if defined(VAR_ARRAYS) || defined (USE_ALLOCA)
160
2.76k
   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.76k
   st->full_frame_size = 2*mode->frameSize;
167
2.76k
   st->frame_size = mode->frameSize;
168
2.76k
   st->subframeSize = mode->subframeSize;
169
2.76k
   st->nbSubframes = mode->frameSize/mode->subframeSize;
170
2.76k
   st->windowSize = st->frame_size+st->subframeSize;
171
2.76k
   st->lpcSize=mode->lpcSize;
172
173
2.76k
   st->encode_submode = 1;
174
2.76k
   st->submodes=mode->submodes;
175
2.76k
   st->submodeSelect = st->submodeID=mode->defaultSubmode;
176
177
2.76k
   tmp=9;
178
2.76k
   speex_encoder_ctl(st->st_low, SPEEX_SET_QUALITY, &tmp);
179
2.76k
   tmp=1;
180
2.76k
   speex_encoder_ctl(st->st_low, SPEEX_SET_WIDEBAND, &tmp);
181
182
2.76k
   st->lpc_floor = mode->lpc_floor;
183
2.76k
   st->gamma1=mode->gamma1;
184
2.76k
   st->gamma2=mode->gamma2;
185
2.76k
   st->first=1;
186
187
2.76k
   st->high=(spx_word16_t*)speex_alloc((st->windowSize-st->frame_size)*sizeof(spx_word16_t));
188
189
2.76k
   st->h0_mem=(spx_word16_t*)speex_alloc((QMF_ORDER)*sizeof(spx_word16_t));
190
191
2.76k
   st->window= lpc_window;
192
193
2.76k
   st->lagWindow = lag_window;
194
195
2.76k
   st->old_lsp = (spx_lsp_t*)speex_alloc(st->lpcSize*sizeof(spx_lsp_t));
196
2.76k
   st->old_qlsp = (spx_lsp_t*)speex_alloc(st->lpcSize*sizeof(spx_lsp_t));
197
2.76k
   st->interp_qlpc = (spx_coef_t*)speex_alloc(st->lpcSize*sizeof(spx_coef_t));
198
2.76k
   st->pi_gain = (spx_word32_t*)speex_alloc((st->nbSubframes)*sizeof(spx_word32_t));
199
2.76k
   st->exc_rms = (spx_word16_t*)speex_alloc((st->nbSubframes)*sizeof(spx_word16_t));
200
2.76k
   st->innov_rms_save = NULL;
201
202
2.76k
   st->mem_sp = (spx_mem_t*)speex_alloc((st->lpcSize)*sizeof(spx_mem_t));
203
2.76k
   st->mem_sp2 = (spx_mem_t*)speex_alloc((st->lpcSize)*sizeof(spx_mem_t));
204
2.76k
   st->mem_sw = (spx_mem_t*)speex_alloc((st->lpcSize)*sizeof(spx_mem_t));
205
206
24.8k
   for (i=0;i<st->lpcSize;i++)
207
22.1k
      st->old_lsp[i]= DIV32(MULT16_16(QCONST16(3.1415927f, LSP_SHIFT), i+1), st->lpcSize+1);
208
209
2.76k
#ifndef DISABLE_VBR
210
2.76k
   st->vbr_quality = 8;
211
2.76k
   st->vbr_enabled = 0;
212
2.76k
   st->vbr_max = 0;
213
2.76k
   st->vbr_max_high = 20000;  /* We just need a big value here */
214
2.76k
   st->vad_enabled = 0;
215
2.76k
   st->abr_enabled = 0;
216
2.76k
   st->relative_quality=0;
217
2.76k
#endif /* #ifndef DISABLE_VBR */
218
219
2.76k
   st->complexity=2;
220
2.76k
   speex_encoder_ctl(st->st_low, SPEEX_GET_SAMPLING_RATE, &st->sampling_rate);
221
2.76k
   st->sampling_rate*=2;
222
#ifdef ENABLE_VALGRIND
223
   VALGRIND_MAKE_MEM_DEFINED(st, (st->stack-(char*)st));
224
#endif
225
2.76k
   return st;
226
2.76k
}
227
228
void sb_encoder_destroy(void *state)
229
2.76k
{
230
2.76k
   SBEncState *st=(SBEncState*)state;
231
232
2.76k
   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.76k
   speex_free(st->high);
238
239
2.76k
   speex_free(st->h0_mem);
240
241
2.76k
   speex_free(st->old_lsp);
242
2.76k
   speex_free(st->old_qlsp);
243
2.76k
   speex_free(st->interp_qlpc);
244
2.76k
   speex_free(st->pi_gain);
245
2.76k
   speex_free(st->exc_rms);
246
247
2.76k
   speex_free(st->mem_sp);
248
2.76k
   speex_free(st->mem_sp2);
249
2.76k
   speex_free(st->mem_sw);
250
251
252
2.76k
   speex_free(st);
253
2.76k
}
254
255
int sb_encoder_ctl(void *state, int request, void *ptr)
256
229k
{
257
229k
   SBEncState *st;
258
229k
   st=(SBEncState*)state;
259
229k
   switch(request)
260
229k
   {
261
9.34k
   case SPEEX_GET_FRAME_SIZE:
262
9.34k
      (*(spx_int32_t*)ptr) = st->full_frame_size;
263
9.34k
      break;
264
11.7k
   case SPEEX_SET_HIGH_MODE:
265
11.7k
      st->submodeSelect = st->submodeID = (*(spx_int32_t*)ptr);
266
11.7k
      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.16k
   case SPEEX_SET_DTX:
271
2.16k
      speex_encoder_ctl(st->st_low, SPEEX_SET_DTX, ptr);
272
2.16k
      break;
273
0
   case SPEEX_GET_DTX:
274
0
      speex_encoder_ctl(st->st_low, SPEEX_GET_DTX, ptr);
275
0
      break;
276
4.81k
   case SPEEX_GET_LOW_MODE:
277
4.81k
      speex_encoder_ctl(st->st_low, SPEEX_GET_LOW_MODE, ptr);
278
4.81k
      break;
279
15.2k
   case SPEEX_SET_MODE:
280
15.2k
      speex_encoder_ctl(st, SPEEX_SET_QUALITY, ptr);
281
15.2k
      break;
282
0
#ifndef DISABLE_VBR
283
2.50k
   case SPEEX_SET_VBR:
284
2.50k
      st->vbr_enabled = (*(spx_int32_t*)ptr);
285
2.50k
      speex_encoder_ctl(st->st_low, SPEEX_SET_VBR, ptr);
286
2.50k
      break;
287
0
   case SPEEX_GET_VBR:
288
0
      (*(spx_int32_t*)ptr) = st->vbr_enabled;
289
0
      break;
290
2.15k
   case SPEEX_SET_VAD:
291
2.15k
      st->vad_enabled = (*(spx_int32_t*)ptr);
292
2.15k
      speex_encoder_ctl(st->st_low, SPEEX_SET_VAD, ptr);
293
2.15k
      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.84k
   case SPEEX_SET_VBR_QUALITY:
300
3.84k
      {
301
3.84k
         spx_int32_t q;
302
3.84k
         float qual = (*(float*)ptr)+.6;
303
3.84k
         st->vbr_quality = (*(float*)ptr);
304
3.84k
         if (qual>10)
305
302
            qual=10;
306
3.84k
         q=(int)floor(.5+*(float*)ptr);
307
3.84k
         if (q>10)
308
0
            q=10;
309
3.84k
         speex_encoder_ctl(st->st_low, SPEEX_SET_VBR_QUALITY, &qual);
310
3.84k
         speex_encoder_ctl(state, SPEEX_SET_QUALITY, &q);
311
3.84k
         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.34k
   case SPEEX_SET_ABR:
319
1.34k
      st->abr_enabled = (*(spx_int32_t*)ptr);
320
1.34k
      st->vbr_enabled = st->abr_enabled!=0;
321
1.34k
      speex_encoder_ctl(st->st_low, SPEEX_SET_VBR, &st->vbr_enabled);
322
1.34k
      if (st->vbr_enabled)
323
1.34k
      {
324
1.34k
         spx_int32_t i=10, rate, target;
325
1.34k
         float vbr_qual;
326
1.34k
         target = (*(spx_int32_t*)ptr);
327
16.1k
         while (i>=0)
328
14.7k
         {
329
14.7k
            speex_encoder_ctl(st, SPEEX_SET_QUALITY, &i);
330
14.7k
            speex_encoder_ctl(st, SPEEX_GET_BITRATE, &rate);
331
14.7k
            if (rate <= target)
332
0
               break;
333
14.7k
            i--;
334
14.7k
         }
335
1.34k
         vbr_qual=i;
336
1.34k
         if (vbr_qual<0)
337
1.34k
            vbr_qual=0;
338
1.34k
         speex_encoder_ctl(st, SPEEX_SET_VBR_QUALITY, &vbr_qual);
339
1.34k
         st->abr_count=0;
340
1.34k
         st->abr_drift=0;
341
1.34k
         st->abr_drift2=0;
342
1.34k
      }
343
344
1.34k
      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
65.3k
   case SPEEX_SET_QUALITY:
351
65.3k
      {
352
65.3k
         spx_int32_t nb_qual;
353
65.3k
         int quality = (*(spx_int32_t*)ptr);
354
65.3k
         if (quality < 0)
355
0
            quality = 0;
356
65.3k
         if (quality > 10)
357
0
            quality = 10;
358
65.3k
         st->submodeSelect = st->submodeID = ((const SpeexSBMode*)(st->mode->mode))->quality_map[quality];
359
65.3k
         nb_qual = ((const SpeexSBMode*)(st->mode->mode))->low_quality_map[quality];
360
65.3k
         speex_encoder_ctl(st->st_low, SPEEX_SET_MODE, &nb_qual);
361
65.3k
      }
362
65.3k
      break;
363
5.52k
   case SPEEX_SET_COMPLEXITY:
364
5.52k
      speex_encoder_ctl(st->st_low, SPEEX_SET_COMPLEXITY, ptr);
365
5.52k
      st->complexity = (*(spx_int32_t*)ptr);
366
5.52k
      if (st->complexity<1)
367
1.23k
         st->complexity=1;
368
5.52k
      break;
369
0
   case SPEEX_GET_COMPLEXITY:
370
0
      (*(spx_int32_t*)ptr) = st->complexity;
371
0
      break;
372
3.92k
   case SPEEX_SET_BITRATE:
373
3.92k
      {
374
3.92k
         spx_int32_t i=10;
375
3.92k
         spx_int32_t rate, target;
376
3.92k
         target = (*(spx_int32_t*)ptr);
377
28.4k
         while (i>=0)
378
27.4k
         {
379
27.4k
            speex_encoder_ctl(st, SPEEX_SET_QUALITY, &i);
380
27.4k
            speex_encoder_ctl(st, SPEEX_GET_BITRATE, &rate);
381
27.4k
            if (rate <= target)
382
2.95k
               break;
383
24.4k
            i--;
384
24.4k
         }
385
3.92k
      }
386
3.92k
      break;
387
61.9k
   case SPEEX_GET_BITRATE:
388
61.9k
      speex_encoder_ctl(st->st_low, request, ptr);
389
      /*fprintf (stderr, "before: %d\n", (*(int*)ptr));*/
390
61.9k
      if (st->submodes[st->submodeID])
391
60.8k
         (*(spx_int32_t*)ptr) += st->sampling_rate*SUBMODE(bits_per_frame)/st->full_frame_size;
392
1.07k
      else
393
1.07k
         (*(spx_int32_t*)ptr) += st->sampling_rate*(SB_SUBMODE_BITS+1)/st->full_frame_size;
394
      /*fprintf (stderr, "after: %d\n", (*(int*)ptr));*/
395
61.9k
      break;
396
5.52k
   case SPEEX_SET_SAMPLING_RATE:
397
5.52k
      {
398
5.52k
         spx_int32_t tmp=(*(spx_int32_t*)ptr);
399
5.52k
         st->sampling_rate = tmp;
400
5.52k
         tmp>>=1;
401
5.52k
         speex_encoder_ctl(st->st_low, SPEEX_SET_SAMPLING_RATE, &tmp);
402
5.52k
      }
403
5.52k
      break;
404
1.60k
   case SPEEX_GET_SAMPLING_RATE:
405
1.60k
      (*(spx_int32_t*)ptr)=st->sampling_rate;
406
1.60k
      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.52k
   case SPEEX_GET_LOOKAHEAD:
427
5.52k
      speex_encoder_ctl(st->st_low, SPEEX_GET_LOOKAHEAD, ptr);
428
5.52k
      (*(spx_int32_t*)ptr) = 2*(*(spx_int32_t*)ptr) + QMF_ORDER - 1;
429
5.52k
      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.15k
   case SPEEX_SET_VBR_MAX_BITRATE:
438
2.15k
      {
439
2.15k
         st->vbr_max = (*(spx_int32_t*)ptr);
440
2.15k
         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.15k
         } else {
445
2.15k
            spx_int32_t low_rate;
446
2.15k
            if (st->vbr_max >= 42200)
447
0
            {
448
0
               st->vbr_max_high = 17600;
449
2.15k
            } else if (st->vbr_max >= 27800)
450
0
            {
451
0
               st->vbr_max_high = 9600;
452
2.15k
            } else if (st->vbr_max > 20600)
453
0
            {
454
0
               st->vbr_max_high = 5600;
455
2.15k
            } else {
456
2.15k
               st->vbr_max_high = 1800;
457
2.15k
            }
458
2.15k
            if (st->subframeSize==80)
459
678
               st->vbr_max_high = 1800;
460
2.15k
            low_rate = st->vbr_max - st->vbr_max_high;
461
2.15k
            speex_encoder_ctl(st->st_low, SPEEX_SET_VBR_MAX_BITRATE, &low_rate);
462
2.15k
         }
463
2.15k
      }
464
2.15k
      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.52k
   case SPEEX_SET_HIGHPASS:
470
5.52k
      speex_encoder_ctl(st->st_low, SPEEX_SET_HIGHPASS, ptr);
471
5.52k
      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.81k
   case SPEEX_GET_PI_GAIN:
479
4.81k
      {
480
4.81k
         int i;
481
4.81k
         spx_word32_t *g = (spx_word32_t*)ptr;
482
24.0k
         for (i=0;i<st->nbSubframes;i++)
483
19.2k
            g[i]=st->pi_gain[i];
484
4.81k
      }
485
4.81k
      break;
486
4.81k
   case SPEEX_GET_EXC:
487
4.81k
      {
488
4.81k
         int i;
489
24.0k
         for (i=0;i<st->nbSubframes;i++)
490
19.2k
            ((spx_word16_t*)ptr)[i] = st->exc_rms[i];
491
4.81k
      }
492
4.81k
      break;
493
0
#ifndef DISABLE_VBR
494
3.01k
   case SPEEX_GET_RELATIVE_QUALITY:
495
3.01k
      (*(float*)ptr)=st->relative_quality;
496
3.01k
      break;
497
0
#endif /* #ifndef DISABLE_VBR */
498
4.81k
   case SPEEX_SET_INNOVATION_SAVE:
499
4.81k
      st->innov_rms_save = (spx_word16_t*)ptr;
500
4.81k
      break;
501
1.60k
   case SPEEX_SET_WIDEBAND:
502
1.60k
      speex_encoder_ctl(st->st_low, SPEEX_SET_WIDEBAND, ptr);
503
1.60k
      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
229k
   }
511
229k
   return 0;
512
229k
}
sb_encoder_ctl
Line
Count
Source
256
114k
{
257
114k
   SBEncState *st;
258
114k
   st=(SBEncState*)state;
259
114k
   switch(request)
260
114k
   {
261
4.67k
   case SPEEX_GET_FRAME_SIZE:
262
4.67k
      (*(spx_int32_t*)ptr) = st->full_frame_size;
263
4.67k
      break;
264
5.88k
   case SPEEX_SET_HIGH_MODE:
265
5.88k
      st->submodeSelect = st->submodeID = (*(spx_int32_t*)ptr);
266
5.88k
      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.08k
   case SPEEX_SET_DTX:
271
1.08k
      speex_encoder_ctl(st->st_low, SPEEX_SET_DTX, ptr);
272
1.08k
      break;
273
0
   case SPEEX_GET_DTX:
274
0
      speex_encoder_ctl(st->st_low, SPEEX_GET_DTX, ptr);
275
0
      break;
276
2.40k
   case SPEEX_GET_LOW_MODE:
277
2.40k
      speex_encoder_ctl(st->st_low, SPEEX_GET_LOW_MODE, ptr);
278
2.40k
      break;
279
7.60k
   case SPEEX_SET_MODE:
280
7.60k
      speex_encoder_ctl(st, SPEEX_SET_QUALITY, ptr);
281
7.60k
      break;
282
0
#ifndef DISABLE_VBR
283
1.25k
   case SPEEX_SET_VBR:
284
1.25k
      st->vbr_enabled = (*(spx_int32_t*)ptr);
285
1.25k
      speex_encoder_ctl(st->st_low, SPEEX_SET_VBR, ptr);
286
1.25k
      break;
287
0
   case SPEEX_GET_VBR:
288
0
      (*(spx_int32_t*)ptr) = st->vbr_enabled;
289
0
      break;
290
1.07k
   case SPEEX_SET_VAD:
291
1.07k
      st->vad_enabled = (*(spx_int32_t*)ptr);
292
1.07k
      speex_encoder_ctl(st->st_low, SPEEX_SET_VAD, ptr);
293
1.07k
      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.92k
   case SPEEX_SET_VBR_QUALITY:
300
1.92k
      {
301
1.92k
         spx_int32_t q;
302
1.92k
         float qual = (*(float*)ptr)+.6;
303
1.92k
         st->vbr_quality = (*(float*)ptr);
304
1.92k
         if (qual>10)
305
151
            qual=10;
306
1.92k
         q=(int)floor(.5+*(float*)ptr);
307
1.92k
         if (q>10)
308
0
            q=10;
309
1.92k
         speex_encoder_ctl(st->st_low, SPEEX_SET_VBR_QUALITY, &qual);
310
1.92k
         speex_encoder_ctl(state, SPEEX_SET_QUALITY, &q);
311
1.92k
         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
672
   case SPEEX_SET_ABR:
319
672
      st->abr_enabled = (*(spx_int32_t*)ptr);
320
672
      st->vbr_enabled = st->abr_enabled!=0;
321
672
      speex_encoder_ctl(st->st_low, SPEEX_SET_VBR, &st->vbr_enabled);
322
672
      if (st->vbr_enabled)
323
672
      {
324
672
         spx_int32_t i=10, rate, target;
325
672
         float vbr_qual;
326
672
         target = (*(spx_int32_t*)ptr);
327
8.06k
         while (i>=0)
328
7.39k
         {
329
7.39k
            speex_encoder_ctl(st, SPEEX_SET_QUALITY, &i);
330
7.39k
            speex_encoder_ctl(st, SPEEX_GET_BITRATE, &rate);
331
7.39k
            if (rate <= target)
332
0
               break;
333
7.39k
            i--;
334
7.39k
         }
335
672
         vbr_qual=i;
336
672
         if (vbr_qual<0)
337
672
            vbr_qual=0;
338
672
         speex_encoder_ctl(st, SPEEX_SET_VBR_QUALITY, &vbr_qual);
339
672
         st->abr_count=0;
340
672
         st->abr_drift=0;
341
672
         st->abr_drift2=0;
342
672
      }
343
344
672
      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
32.6k
   case SPEEX_SET_QUALITY:
351
32.6k
      {
352
32.6k
         spx_int32_t nb_qual;
353
32.6k
         int quality = (*(spx_int32_t*)ptr);
354
32.6k
         if (quality < 0)
355
0
            quality = 0;
356
32.6k
         if (quality > 10)
357
0
            quality = 10;
358
32.6k
         st->submodeSelect = st->submodeID = ((const SpeexSBMode*)(st->mode->mode))->quality_map[quality];
359
32.6k
         nb_qual = ((const SpeexSBMode*)(st->mode->mode))->low_quality_map[quality];
360
32.6k
         speex_encoder_ctl(st->st_low, SPEEX_SET_MODE, &nb_qual);
361
32.6k
      }
362
32.6k
      break;
363
2.76k
   case SPEEX_SET_COMPLEXITY:
364
2.76k
      speex_encoder_ctl(st->st_low, SPEEX_SET_COMPLEXITY, ptr);
365
2.76k
      st->complexity = (*(spx_int32_t*)ptr);
366
2.76k
      if (st->complexity<1)
367
617
         st->complexity=1;
368
2.76k
      break;
369
0
   case SPEEX_GET_COMPLEXITY:
370
0
      (*(spx_int32_t*)ptr) = st->complexity;
371
0
      break;
372
1.96k
   case SPEEX_SET_BITRATE:
373
1.96k
      {
374
1.96k
         spx_int32_t i=10;
375
1.96k
         spx_int32_t rate, target;
376
1.96k
         target = (*(spx_int32_t*)ptr);
377
14.2k
         while (i>=0)
378
13.7k
         {
379
13.7k
            speex_encoder_ctl(st, SPEEX_SET_QUALITY, &i);
380
13.7k
            speex_encoder_ctl(st, SPEEX_GET_BITRATE, &rate);
381
13.7k
            if (rate <= target)
382
1.47k
               break;
383
12.2k
            i--;
384
12.2k
         }
385
1.96k
      }
386
1.96k
      break;
387
30.9k
   case SPEEX_GET_BITRATE:
388
30.9k
      speex_encoder_ctl(st->st_low, request, ptr);
389
      /*fprintf (stderr, "before: %d\n", (*(int*)ptr));*/
390
30.9k
      if (st->submodes[st->submodeID])
391
30.4k
         (*(spx_int32_t*)ptr) += st->sampling_rate*SUBMODE(bits_per_frame)/st->full_frame_size;
392
536
      else
393
536
         (*(spx_int32_t*)ptr) += st->sampling_rate*(SB_SUBMODE_BITS+1)/st->full_frame_size;
394
      /*fprintf (stderr, "after: %d\n", (*(int*)ptr));*/
395
30.9k
      break;
396
2.76k
   case SPEEX_SET_SAMPLING_RATE:
397
2.76k
      {
398
2.76k
         spx_int32_t tmp=(*(spx_int32_t*)ptr);
399
2.76k
         st->sampling_rate = tmp;
400
2.76k
         tmp>>=1;
401
2.76k
         speex_encoder_ctl(st->st_low, SPEEX_SET_SAMPLING_RATE, &tmp);
402
2.76k
      }
403
2.76k
      break;
404
802
   case SPEEX_GET_SAMPLING_RATE:
405
802
      (*(spx_int32_t*)ptr)=st->sampling_rate;
406
802
      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.76k
   case SPEEX_GET_LOOKAHEAD:
427
2.76k
      speex_encoder_ctl(st->st_low, SPEEX_GET_LOOKAHEAD, ptr);
428
2.76k
      (*(spx_int32_t*)ptr) = 2*(*(spx_int32_t*)ptr) + QMF_ORDER - 1;
429
2.76k
      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.07k
   case SPEEX_SET_VBR_MAX_BITRATE:
438
1.07k
      {
439
1.07k
         st->vbr_max = (*(spx_int32_t*)ptr);
440
1.07k
         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.07k
         } else {
445
1.07k
            spx_int32_t low_rate;
446
1.07k
            if (st->vbr_max >= 42200)
447
0
            {
448
0
               st->vbr_max_high = 17600;
449
1.07k
            } else if (st->vbr_max >= 27800)
450
0
            {
451
0
               st->vbr_max_high = 9600;
452
1.07k
            } else if (st->vbr_max > 20600)
453
0
            {
454
0
               st->vbr_max_high = 5600;
455
1.07k
            } else {
456
1.07k
               st->vbr_max_high = 1800;
457
1.07k
            }
458
1.07k
            if (st->subframeSize==80)
459
339
               st->vbr_max_high = 1800;
460
1.07k
            low_rate = st->vbr_max - st->vbr_max_high;
461
1.07k
            speex_encoder_ctl(st->st_low, SPEEX_SET_VBR_MAX_BITRATE, &low_rate);
462
1.07k
         }
463
1.07k
      }
464
1.07k
      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.76k
   case SPEEX_SET_HIGHPASS:
470
2.76k
      speex_encoder_ctl(st->st_low, SPEEX_SET_HIGHPASS, ptr);
471
2.76k
      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.40k
   case SPEEX_GET_PI_GAIN:
479
2.40k
      {
480
2.40k
         int i;
481
2.40k
         spx_word32_t *g = (spx_word32_t*)ptr;
482
12.0k
         for (i=0;i<st->nbSubframes;i++)
483
9.62k
            g[i]=st->pi_gain[i];
484
2.40k
      }
485
2.40k
      break;
486
2.40k
   case SPEEX_GET_EXC:
487
2.40k
      {
488
2.40k
         int i;
489
12.0k
         for (i=0;i<st->nbSubframes;i++)
490
9.62k
            ((spx_word16_t*)ptr)[i] = st->exc_rms[i];
491
2.40k
      }
492
2.40k
      break;
493
0
#ifndef DISABLE_VBR
494
1.50k
   case SPEEX_GET_RELATIVE_QUALITY:
495
1.50k
      (*(float*)ptr)=st->relative_quality;
496
1.50k
      break;
497
0
#endif /* #ifndef DISABLE_VBR */
498
2.40k
   case SPEEX_SET_INNOVATION_SAVE:
499
2.40k
      st->innov_rms_save = (spx_word16_t*)ptr;
500
2.40k
      break;
501
802
   case SPEEX_SET_WIDEBAND:
502
802
      speex_encoder_ctl(st->st_low, SPEEX_SET_WIDEBAND, ptr);
503
802
      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
114k
   }
511
114k
   return 0;
512
114k
}
sb_encoder_ctl
Line
Count
Source
256
114k
{
257
114k
   SBEncState *st;
258
114k
   st=(SBEncState*)state;
259
114k
   switch(request)
260
114k
   {
261
4.67k
   case SPEEX_GET_FRAME_SIZE:
262
4.67k
      (*(spx_int32_t*)ptr) = st->full_frame_size;
263
4.67k
      break;
264
5.88k
   case SPEEX_SET_HIGH_MODE:
265
5.88k
      st->submodeSelect = st->submodeID = (*(spx_int32_t*)ptr);
266
5.88k
      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.08k
   case SPEEX_SET_DTX:
271
1.08k
      speex_encoder_ctl(st->st_low, SPEEX_SET_DTX, ptr);
272
1.08k
      break;
273
0
   case SPEEX_GET_DTX:
274
0
      speex_encoder_ctl(st->st_low, SPEEX_GET_DTX, ptr);
275
0
      break;
276
2.40k
   case SPEEX_GET_LOW_MODE:
277
2.40k
      speex_encoder_ctl(st->st_low, SPEEX_GET_LOW_MODE, ptr);
278
2.40k
      break;
279
7.60k
   case SPEEX_SET_MODE:
280
7.60k
      speex_encoder_ctl(st, SPEEX_SET_QUALITY, ptr);
281
7.60k
      break;
282
0
#ifndef DISABLE_VBR
283
1.25k
   case SPEEX_SET_VBR:
284
1.25k
      st->vbr_enabled = (*(spx_int32_t*)ptr);
285
1.25k
      speex_encoder_ctl(st->st_low, SPEEX_SET_VBR, ptr);
286
1.25k
      break;
287
0
   case SPEEX_GET_VBR:
288
0
      (*(spx_int32_t*)ptr) = st->vbr_enabled;
289
0
      break;
290
1.07k
   case SPEEX_SET_VAD:
291
1.07k
      st->vad_enabled = (*(spx_int32_t*)ptr);
292
1.07k
      speex_encoder_ctl(st->st_low, SPEEX_SET_VAD, ptr);
293
1.07k
      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.92k
   case SPEEX_SET_VBR_QUALITY:
300
1.92k
      {
301
1.92k
         spx_int32_t q;
302
1.92k
         float qual = (*(float*)ptr)+.6;
303
1.92k
         st->vbr_quality = (*(float*)ptr);
304
1.92k
         if (qual>10)
305
151
            qual=10;
306
1.92k
         q=(int)floor(.5+*(float*)ptr);
307
1.92k
         if (q>10)
308
0
            q=10;
309
1.92k
         speex_encoder_ctl(st->st_low, SPEEX_SET_VBR_QUALITY, &qual);
310
1.92k
         speex_encoder_ctl(state, SPEEX_SET_QUALITY, &q);
311
1.92k
         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
672
   case SPEEX_SET_ABR:
319
672
      st->abr_enabled = (*(spx_int32_t*)ptr);
320
672
      st->vbr_enabled = st->abr_enabled!=0;
321
672
      speex_encoder_ctl(st->st_low, SPEEX_SET_VBR, &st->vbr_enabled);
322
672
      if (st->vbr_enabled)
323
672
      {
324
672
         spx_int32_t i=10, rate, target;
325
672
         float vbr_qual;
326
672
         target = (*(spx_int32_t*)ptr);
327
8.06k
         while (i>=0)
328
7.39k
         {
329
7.39k
            speex_encoder_ctl(st, SPEEX_SET_QUALITY, &i);
330
7.39k
            speex_encoder_ctl(st, SPEEX_GET_BITRATE, &rate);
331
7.39k
            if (rate <= target)
332
0
               break;
333
7.39k
            i--;
334
7.39k
         }
335
672
         vbr_qual=i;
336
672
         if (vbr_qual<0)
337
672
            vbr_qual=0;
338
672
         speex_encoder_ctl(st, SPEEX_SET_VBR_QUALITY, &vbr_qual);
339
672
         st->abr_count=0;
340
672
         st->abr_drift=0;
341
672
         st->abr_drift2=0;
342
672
      }
343
344
672
      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
32.6k
   case SPEEX_SET_QUALITY:
351
32.6k
      {
352
32.6k
         spx_int32_t nb_qual;
353
32.6k
         int quality = (*(spx_int32_t*)ptr);
354
32.6k
         if (quality < 0)
355
0
            quality = 0;
356
32.6k
         if (quality > 10)
357
0
            quality = 10;
358
32.6k
         st->submodeSelect = st->submodeID = ((const SpeexSBMode*)(st->mode->mode))->quality_map[quality];
359
32.6k
         nb_qual = ((const SpeexSBMode*)(st->mode->mode))->low_quality_map[quality];
360
32.6k
         speex_encoder_ctl(st->st_low, SPEEX_SET_MODE, &nb_qual);
361
32.6k
      }
362
32.6k
      break;
363
2.76k
   case SPEEX_SET_COMPLEXITY:
364
2.76k
      speex_encoder_ctl(st->st_low, SPEEX_SET_COMPLEXITY, ptr);
365
2.76k
      st->complexity = (*(spx_int32_t*)ptr);
366
2.76k
      if (st->complexity<1)
367
617
         st->complexity=1;
368
2.76k
      break;
369
0
   case SPEEX_GET_COMPLEXITY:
370
0
      (*(spx_int32_t*)ptr) = st->complexity;
371
0
      break;
372
1.96k
   case SPEEX_SET_BITRATE:
373
1.96k
      {
374
1.96k
         spx_int32_t i=10;
375
1.96k
         spx_int32_t rate, target;
376
1.96k
         target = (*(spx_int32_t*)ptr);
377
14.2k
         while (i>=0)
378
13.7k
         {
379
13.7k
            speex_encoder_ctl(st, SPEEX_SET_QUALITY, &i);
380
13.7k
            speex_encoder_ctl(st, SPEEX_GET_BITRATE, &rate);
381
13.7k
            if (rate <= target)
382
1.47k
               break;
383
12.2k
            i--;
384
12.2k
         }
385
1.96k
      }
386
1.96k
      break;
387
30.9k
   case SPEEX_GET_BITRATE:
388
30.9k
      speex_encoder_ctl(st->st_low, request, ptr);
389
      /*fprintf (stderr, "before: %d\n", (*(int*)ptr));*/
390
30.9k
      if (st->submodes[st->submodeID])
391
30.4k
         (*(spx_int32_t*)ptr) += st->sampling_rate*SUBMODE(bits_per_frame)/st->full_frame_size;
392
536
      else
393
536
         (*(spx_int32_t*)ptr) += st->sampling_rate*(SB_SUBMODE_BITS+1)/st->full_frame_size;
394
      /*fprintf (stderr, "after: %d\n", (*(int*)ptr));*/
395
30.9k
      break;
396
2.76k
   case SPEEX_SET_SAMPLING_RATE:
397
2.76k
      {
398
2.76k
         spx_int32_t tmp=(*(spx_int32_t*)ptr);
399
2.76k
         st->sampling_rate = tmp;
400
2.76k
         tmp>>=1;
401
2.76k
         speex_encoder_ctl(st->st_low, SPEEX_SET_SAMPLING_RATE, &tmp);
402
2.76k
      }
403
2.76k
      break;
404
802
   case SPEEX_GET_SAMPLING_RATE:
405
802
      (*(spx_int32_t*)ptr)=st->sampling_rate;
406
802
      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.76k
   case SPEEX_GET_LOOKAHEAD:
427
2.76k
      speex_encoder_ctl(st->st_low, SPEEX_GET_LOOKAHEAD, ptr);
428
2.76k
      (*(spx_int32_t*)ptr) = 2*(*(spx_int32_t*)ptr) + QMF_ORDER - 1;
429
2.76k
      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.07k
   case SPEEX_SET_VBR_MAX_BITRATE:
438
1.07k
      {
439
1.07k
         st->vbr_max = (*(spx_int32_t*)ptr);
440
1.07k
         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.07k
         } else {
445
1.07k
            spx_int32_t low_rate;
446
1.07k
            if (st->vbr_max >= 42200)
447
0
            {
448
0
               st->vbr_max_high = 17600;
449
1.07k
            } else if (st->vbr_max >= 27800)
450
0
            {
451
0
               st->vbr_max_high = 9600;
452
1.07k
            } else if (st->vbr_max > 20600)
453
0
            {
454
0
               st->vbr_max_high = 5600;
455
1.07k
            } else {
456
1.07k
               st->vbr_max_high = 1800;
457
1.07k
            }
458
1.07k
            if (st->subframeSize==80)
459
339
               st->vbr_max_high = 1800;
460
1.07k
            low_rate = st->vbr_max - st->vbr_max_high;
461
1.07k
            speex_encoder_ctl(st->st_low, SPEEX_SET_VBR_MAX_BITRATE, &low_rate);
462
1.07k
         }
463
1.07k
      }
464
1.07k
      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.76k
   case SPEEX_SET_HIGHPASS:
470
2.76k
      speex_encoder_ctl(st->st_low, SPEEX_SET_HIGHPASS, ptr);
471
2.76k
      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.40k
   case SPEEX_GET_PI_GAIN:
479
2.40k
      {
480
2.40k
         int i;
481
2.40k
         spx_word32_t *g = (spx_word32_t*)ptr;
482
12.0k
         for (i=0;i<st->nbSubframes;i++)
483
9.62k
            g[i]=st->pi_gain[i];
484
2.40k
      }
485
2.40k
      break;
486
2.40k
   case SPEEX_GET_EXC:
487
2.40k
      {
488
2.40k
         int i;
489
12.0k
         for (i=0;i<st->nbSubframes;i++)
490
9.62k
            ((spx_word16_t*)ptr)[i] = st->exc_rms[i];
491
2.40k
      }
492
2.40k
      break;
493
0
#ifndef DISABLE_VBR
494
1.50k
   case SPEEX_GET_RELATIVE_QUALITY:
495
1.50k
      (*(float*)ptr)=st->relative_quality;
496
1.50k
      break;
497
0
#endif /* #ifndef DISABLE_VBR */
498
2.40k
   case SPEEX_SET_INNOVATION_SAVE:
499
2.40k
      st->innov_rms_save = (spx_word16_t*)ptr;
500
2.40k
      break;
501
802
   case SPEEX_SET_WIDEBAND:
502
802
      speex_encoder_ctl(st->st_low, SPEEX_SET_WIDEBAND, ptr);
503
802
      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
114k
   }
511
114k
   return 0;
512
114k
}
513
514
515
int sb_encode(void *state, void *vin, SpeexBits *bits)
516
10.6k
{
517
10.6k
   SBEncState *st;
518
10.6k
   int i, roots, sub;
519
10.6k
   char *stack;
520
10.6k
   VARDECL(spx_mem_t *mem);
521
10.6k
   VARDECL(spx_sig_t *innov);
522
10.6k
   VARDECL(spx_word16_t *target);
523
10.6k
   VARDECL(spx_word16_t *syn_resp);
524
10.6k
   VARDECL(spx_word32_t *low_pi_gain);
525
10.6k
   spx_word16_t *low;
526
10.6k
   spx_word16_t *high;
527
10.6k
   VARDECL(spx_word16_t *low_exc_rms);
528
10.6k
   VARDECL(spx_word16_t *low_innov_rms);
529
10.6k
   const SpeexSBMode *mode;
530
10.6k
   spx_int32_t dtx;
531
10.6k
   spx_word16_t *in = (spx_word16_t*)vin;
532
10.6k
   spx_word16_t e_low=0, e_high=0;
533
10.6k
   VARDECL(spx_coef_t *lpc);
534
10.6k
   VARDECL(spx_coef_t *interp_lpc);
535
10.6k
   VARDECL(spx_coef_t *bw_lpc1);
536
10.6k
   VARDECL(spx_coef_t *bw_lpc2);
537
10.6k
   VARDECL(spx_lsp_t *lsp);
538
10.6k
   VARDECL(spx_lsp_t *qlsp);
539
10.6k
   VARDECL(spx_lsp_t *interp_lsp);
540
10.6k
   VARDECL(spx_lsp_t *interp_qlsp);
541
542
10.6k
   st = (SBEncState*)state;
543
10.6k
   stack=st->stack;
544
10.6k
   mode = (const SpeexSBMode*)(st->mode->mode);
545
10.6k
   low = in;
546
10.6k
   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.6k
   qmf_decomp(in, h0, low, high, st->full_frame_size, QMF_ORDER, st->h0_mem, stack);
551
552
10.6k
#ifndef DISABLE_VBR
553
10.6k
   if (st->vbr_enabled || st->vad_enabled)
554
7.72k
   {
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.72k
      e_low = compute_rms16(low, st->frame_size);
558
7.72k
      e_high = compute_rms16(high, st->frame_size);
559
7.72k
   }
560
10.6k
#endif /* #ifndef DISABLE_VBR */
561
562
10.6k
   ALLOC(low_innov_rms, st->nbSubframes, spx_word16_t);
563
10.6k
   speex_encoder_ctl(st->st_low, SPEEX_SET_INNOVATION_SAVE, low_innov_rms);
564
   /* Encode the narrowband part*/
565
10.6k
   speex_encode_native(st->st_low, low, bits);
566
567
10.6k
   high = high - (st->windowSize-st->frame_size);
568
10.6k
   SPEEX_COPY(high, st->high, st->windowSize-st->frame_size);
569
10.6k
   SPEEX_COPY(st->high, &high[st->frame_size], st->windowSize-st->frame_size);
570
571
572
10.6k
   ALLOC(low_pi_gain, st->nbSubframes, spx_word32_t);
573
10.6k
   ALLOC(low_exc_rms, st->nbSubframes, spx_word16_t);
574
10.6k
   speex_encoder_ctl(st->st_low, SPEEX_GET_PI_GAIN, low_pi_gain);
575
10.6k
   speex_encoder_ctl(st->st_low, SPEEX_GET_EXC, low_exc_rms);
576
577
10.6k
   speex_encoder_ctl(st->st_low, SPEEX_GET_LOW_MODE, &dtx);
578
579
10.6k
   if (dtx==0)
580
525
      dtx=1;
581
10.0k
   else
582
10.0k
      dtx=0;
583
584
10.6k
   ALLOC(lpc, st->lpcSize, spx_coef_t);
585
10.6k
   ALLOC(interp_lpc, st->lpcSize, spx_coef_t);
586
10.6k
   ALLOC(bw_lpc1, st->lpcSize, spx_coef_t);
587
10.6k
   ALLOC(bw_lpc2, st->lpcSize, spx_coef_t);
588
589
10.6k
   ALLOC(lsp, st->lpcSize, spx_lsp_t);
590
10.6k
   ALLOC(qlsp, st->lpcSize, spx_lsp_t);
591
10.6k
   ALLOC(interp_lsp, st->lpcSize, spx_lsp_t);
592
10.6k
   ALLOC(interp_qlsp, st->lpcSize, spx_lsp_t);
593
594
10.6k
   {
595
10.6k
      VARDECL(spx_word16_t *autocorr);
596
10.6k
      VARDECL(spx_word16_t *w_sig);
597
10.6k
      ALLOC(autocorr, st->lpcSize+1, spx_word16_t);
598
10.6k
      ALLOC(w_sig, st->windowSize, spx_word16_t);
599
      /* Window for analysis */
600
      /* FIXME: This is a kludge */
601
10.6k
      if (st->subframeSize==80)
602
2.40k
      {
603
964k
         for (i=0;i<st->windowSize;i++)
604
962k
            w_sig[i] = EXTRACT16(SHR32(MULT16_16(high[i],st->window[i>>1]),SIG_SHIFT));
605
8.20k
      } else {
606
1.64M
         for (i=0;i<st->windowSize;i++)
607
1.64M
            w_sig[i] = EXTRACT16(SHR32(MULT16_16(high[i],st->window[i]),SIG_SHIFT));
608
8.20k
      }
609
      /* Compute auto-correlation */
610
10.6k
      _spx_autocorr(w_sig, autocorr, st->lpcSize+1, st->windowSize);
611
10.6k
      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
106k
      for (i=0;i<st->lpcSize+1;i++)
615
95.4k
         autocorr[i] = MULT16_16_Q14(autocorr[i],st->lagWindow[i]);
616
617
      /* Levinson-Durbin */
618
10.6k
      _spx_lpc(lpc, autocorr, st->lpcSize);
619
10.6k
   }
620
621
   /* LPC to LSPs (x-domain) transform */
622
10.6k
   roots=lpc_to_lsp (lpc, st->lpcSize, lsp, 10, LSP_DELTA1, stack);
623
10.6k
   if (roots!=st->lpcSize)
624
151
   {
625
151
      roots = lpc_to_lsp (lpc, st->lpcSize, lsp, 10, LSP_DELTA2, stack);
626
151
      if (roots!=st->lpcSize) {
627
         /*If we can't find all LSP's, do some damage control and use a flat filter*/
628
1.00k
         for (i=0;i<st->lpcSize;i++)
629
896
         {
630
896
            lsp[i]=st->old_lsp[i];
631
896
         }
632
112
      }
633
151
   }
634
635
10.6k
#ifndef DISABLE_VBR
636
   /* VBR code */
637
10.6k
   if ((st->vbr_enabled || st->vad_enabled) && !dtx)
638
7.19k
   {
639
7.19k
      float ratio;
640
7.19k
      if (st->abr_enabled)
641
2.75k
      {
642
2.75k
         float qual_change=0;
643
2.75k
         if (st->abr_drift2 * st->abr_drift > 0)
644
2.08k
         {
645
            /* Only adapt if long-term and short-term drift are the same sign */
646
2.08k
            qual_change = -.00001*st->abr_drift/(1+st->abr_count);
647
2.08k
            if (qual_change>.1)
648
0
               qual_change=.1;
649
2.08k
            if (qual_change<-.1)
650
610
               qual_change=-.1;
651
2.08k
         }
652
2.75k
         st->vbr_quality += qual_change;
653
2.75k
         if (st->vbr_quality>10)
654
0
            st->vbr_quality=10;
655
2.75k
         if (st->vbr_quality<0)
656
2.08k
            st->vbr_quality=0;
657
2.75k
      }
658
659
660
7.19k
      ratio = 2*log((1.f+e_high)/(1.f+e_low));
661
662
7.19k
      speex_encoder_ctl(st->st_low, SPEEX_GET_RELATIVE_QUALITY, &st->relative_quality);
663
7.19k
      if (ratio<-4)
664
142
         ratio=-4;
665
7.19k
      if (ratio>2)
666
68
         ratio=2;
667
      /*if (ratio>-2)*/
668
7.19k
      if (st->vbr_enabled)
669
5.88k
      {
670
5.88k
         spx_int32_t modeid;
671
5.88k
         modeid = mode->nb_modes-1;
672
5.88k
         st->relative_quality+=1.0*(ratio+2);
673
5.88k
   if (st->relative_quality<-1)
674
19
            st->relative_quality=-1;
675
18.7k
         while (modeid)
676
18.3k
         {
677
18.3k
            int v1;
678
18.3k
            float thresh;
679
18.3k
            v1=(int)floor(st->vbr_quality);
680
18.3k
            if (v1==10)
681
1.17k
               thresh = mode->vbr_thresh[modeid][v1];
682
17.1k
            else
683
17.1k
               thresh = (st->vbr_quality-v1)   * mode->vbr_thresh[modeid][v1+1] +
684
17.1k
                        (1+v1-st->vbr_quality) * mode->vbr_thresh[modeid][v1];
685
18.3k
            if (st->relative_quality >= thresh && st->sampling_rate*st->submodes[modeid]->bits_per_frame/st->full_frame_size <= st->vbr_max_high)
686
5.43k
               break;
687
12.8k
            modeid--;
688
12.8k
         }
689
5.88k
         speex_encoder_ctl(state, SPEEX_SET_HIGH_MODE, &modeid);
690
5.88k
         if (st->abr_enabled)
691
2.75k
         {
692
2.75k
            spx_int32_t bitrate;
693
2.75k
            speex_encoder_ctl(state, SPEEX_GET_BITRATE, &bitrate);
694
2.75k
            st->abr_drift+=(bitrate-st->abr_enabled);
695
2.75k
            st->abr_drift2 = .95*st->abr_drift2 + .05*(bitrate-st->abr_enabled);
696
2.75k
            st->abr_count += 1.0;
697
2.75k
         }
698
699
5.88k
      } else {
700
         /* VAD only */
701
1.31k
         int modeid;
702
1.31k
         if (st->relative_quality<2.0)
703
317
            modeid=1;
704
996
         else
705
996
            modeid=st->submodeSelect;
706
         /*speex_encoder_ctl(state, SPEEX_SET_MODE, &mode);*/
707
1.31k
         st->submodeID=modeid;
708
709
1.31k
      }
710
      /*fprintf (stderr, "%f %f\n", ratio, low_qual);*/
711
7.19k
   }
712
10.6k
#endif /* #ifndef DISABLE_VBR */
713
714
10.6k
   if (st->encode_submode)
715
10.6k
   {
716
10.6k
      speex_bits_pack(bits, 1, 1);
717
10.6k
      if (dtx)
718
525
         speex_bits_pack(bits, 0, SB_SUBMODE_BITS);
719
10.0k
      else
720
10.0k
         speex_bits_pack(bits, st->submodeID, SB_SUBMODE_BITS);
721
10.6k
   }
722
723
   /* If null mode (no transmission), just set a couple things to zero*/
724
10.6k
   if (dtx || st->submodes[st->submodeID] == NULL)
725
1.21k
   {
726
256k
      for (i=0;i<st->frame_size;i++)
727
255k
         high[i]=VERY_SMALL;
728
729
10.9k
      for (i=0;i<st->lpcSize;i++)
730
9.72k
         st->mem_sw[i]=0;
731
1.21k
      st->first=1;
732
733
      /* Final signal synthesis from excitation */
734
1.21k
      iir_mem16(high, st->interp_qlpc, high, st->frame_size, st->lpcSize, st->mem_sp, stack);
735
736
1.21k
      if (dtx)
737
525
         return 0;
738
691
      else
739
691
         return 1;
740
1.21k
   }
741
742
743
   /* LSP quantization */
744
9.39k
   SUBMODE(lsp_quant)(lsp, qlsp, st->lpcSize, bits);
745
746
9.39k
   if (st->first)
747
2.62k
   {
748
23.6k
      for (i=0;i<st->lpcSize;i++)
749
21.0k
         st->old_lsp[i] = lsp[i];
750
23.6k
      for (i=0;i<st->lpcSize;i++)
751
21.0k
         st->old_qlsp[i] = qlsp[i];
752
2.62k
   }
753
754
9.39k
   ALLOC(mem, st->lpcSize, spx_mem_t);
755
9.39k
   ALLOC(syn_resp, st->subframeSize, spx_word16_t);
756
9.39k
   ALLOC(innov, st->subframeSize, spx_sig_t);
757
9.39k
   ALLOC(target, st->subframeSize, spx_word16_t);
758
759
46.9k
   for (sub=0;sub<st->nbSubframes;sub++)
760
37.5k
   {
761
37.5k
      VARDECL(spx_word16_t *exc);
762
37.5k
      VARDECL(spx_word16_t *res);
763
37.5k
      VARDECL(spx_word16_t *sw);
764
37.5k
      spx_word16_t *sp;
765
37.5k
      spx_word16_t filter_ratio;     /*Q7*/
766
37.5k
      int offset;
767
37.5k
      spx_word32_t rl, rh;           /*Q13*/
768
37.5k
      spx_word16_t eh=0;
769
770
37.5k
      offset = st->subframeSize*sub;
771
37.5k
      sp=high+offset;
772
37.5k
      ALLOC(exc, st->subframeSize, spx_word16_t);
773
37.5k
      ALLOC(res, st->subframeSize, spx_word16_t);
774
37.5k
      ALLOC(sw, st->subframeSize, spx_word16_t);
775
776
      /* LSP interpolation (quantized and unquantized) */
777
37.5k
      lsp_interpolate(st->old_lsp, lsp, interp_lsp, st->lpcSize, sub, st->nbSubframes, LSP_MARGIN);
778
37.5k
      lsp_interpolate(st->old_qlsp, qlsp, interp_qlsp, st->lpcSize, sub, st->nbSubframes, LSP_MARGIN);
779
780
37.5k
      lsp_to_lpc(interp_lsp, interp_lpc, st->lpcSize,stack);
781
37.5k
      lsp_to_lpc(interp_qlsp, st->interp_qlpc, st->lpcSize, stack);
782
783
37.5k
      bw_lpc(st->gamma1, interp_lpc, bw_lpc1, st->lpcSize);
784
37.5k
      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
37.5k
      st->pi_gain[sub]=LPC_SCALING;
789
37.5k
      rh = LPC_SCALING;
790
187k
      for (i=0;i<st->lpcSize;i+=2)
791
150k
      {
792
150k
         rh += st->interp_qlpc[i+1] - st->interp_qlpc[i];
793
150k
         st->pi_gain[sub] += st->interp_qlpc[i] + st->interp_qlpc[i+1];
794
150k
      }
795
796
37.5k
      rl = low_pi_gain[sub];
797
#ifdef FIXED_POINT
798
25.2k
      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
37.5k
      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
37.5k
      eh = compute_rms16(exc, st->subframeSize);
808
809
37.5k
      if (!SUBMODE(innovation_quant)) {/* 1 for spectral folding excitation, 0 for stochastic */
810
28.9k
         spx_word32_t g;   /*Q7*/
811
28.9k
         spx_word16_t el;  /*Q0*/
812
28.9k
         el = low_innov_rms[sub];
813
814
         /* Gain to use if we want to use the low-band excitation for high-band */
815
28.9k
         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
28.9k
         {
836
28.9k
            int quant = scal_quant(g, fold_quant_bound, 32);
837
            /*speex_warning_int("tata", quant);*/
838
28.9k
            if (quant<0)
839
0
               quant=0;
840
28.9k
            if (quant>31)
841
0
               quant=31;
842
28.9k
            speex_bits_pack(bits, quant, 5);
843
28.9k
         }
844
28.9k
         if (st->innov_rms_save)
845
5.81k
         {
846
5.81k
            st->innov_rms_save[sub] = eh;
847
5.81k
         }
848
28.9k
         st->exc_rms[sub] = eh;
849
28.9k
      } else {
850
8.65k
         spx_word16_t gc;       /*Q7*/
851
8.65k
         spx_word32_t scale;    /*Q14*/
852
8.65k
         spx_word16_t el;       /*Q0*/
853
8.65k
         el = low_exc_rms[sub]; /*Q0*/
854
855
8.65k
         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.65k
         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.65k
         {
862
8.65k
            int qgc = scal_quant(gc, gc_quant_bound, 16);
863
8.65k
            speex_bits_pack(bits, qgc, 4);
864
8.65k
            gc = MULT16_16_Q15(QCONST16(0.87360,15),gc_quant_bound[qgc]);
865
8.65k
         }
866
8.65k
         if (st->subframeSize==80)
867
0
            gc = MULT16_16_P14(QCONST16(1.4142f,14), gc);
868
869
8.65k
         scale = SHL32(MULT16_16(PDIV32_16(SHL32(EXTEND32(gc),SIG_SHIFT-6),MAX16(EPSILON,filter_ratio)),(1+el)),6);
870
871
8.65k
         compute_impulse_response(st->interp_qlpc, bw_lpc1, bw_lpc2, syn_resp, st->subframeSize, st->lpcSize, stack);
872
873
874
         /* Reset excitation */
875
354k
         for (i=0;i<st->subframeSize;i++)
876
346k
            res[i]=VERY_SMALL;
877
878
         /* Compute zero response (ringing) of A(z/g1) / ( A(z/g2) * Aq(z) ) */
879
77.8k
         for (i=0;i<st->lpcSize;i++)
880
69.2k
            mem[i]=st->mem_sp[i];
881
8.65k
         iir_mem16(res, st->interp_qlpc, res, st->subframeSize, st->lpcSize, mem, stack);
882
883
77.8k
         for (i=0;i<st->lpcSize;i++)
884
69.2k
            mem[i]=st->mem_sw[i];
885
8.65k
         filter_mem16(res, bw_lpc1, bw_lpc2, res, st->subframeSize, st->lpcSize, mem, stack);
886
887
         /* Compute weighted signal */
888
77.8k
         for (i=0;i<st->lpcSize;i++)
889
69.2k
            mem[i]=st->mem_sw[i];
890
8.65k
         filter_mem16(sp, bw_lpc1, bw_lpc2, sw, st->subframeSize, st->lpcSize, mem, stack);
891
892
         /* Compute target signal */
893
354k
         for (i=0;i<st->subframeSize;i++)
894
346k
            target[i]=SUB16(sw[i],res[i]);
895
896
8.65k
         signal_div(target, target, scale, st->subframeSize);
897
898
         /* Reset excitation */
899
8.65k
         SPEEX_MEMSET(innov, 0, st->subframeSize);
900
901
         /*print_vec(target, st->subframeSize, "\ntarget");*/
902
8.65k
         SUBMODE(innovation_quant)(target, st->interp_qlpc, bw_lpc1, bw_lpc2,
903
8.65k
                                   SUBMODE(innovation_params), st->lpcSize, st->subframeSize,
904
8.65k
                                   innov, syn_resp, bits, stack, st->complexity, SUBMODE(double_codebook));
905
         /*print_vec(target, st->subframeSize, "after");*/
906
907
8.65k
         signal_mul(innov, innov, scale, st->subframeSize);
908
909
8.65k
         if (SUBMODE(double_codebook)) {
910
3.11k
            char *tmp_stack=stack;
911
3.11k
            VARDECL(spx_sig_t *innov2);
912
3.11k
            ALLOC(innov2, st->subframeSize, spx_sig_t);
913
3.11k
            SPEEX_MEMSET(innov2, 0, st->subframeSize);
914
127k
            for (i=0;i<st->subframeSize;i++)
915
124k
               target[i]=MULT16_16_P13(QCONST16(2.5f,13), target[i]);
916
917
3.11k
            SUBMODE(innovation_quant)(target, st->interp_qlpc, bw_lpc1, bw_lpc2,
918
3.11k
                                      SUBMODE(innovation_params), st->lpcSize, st->subframeSize,
919
3.11k
                                      innov2, syn_resp, bits, stack, st->complexity, 0);
920
3.11k
            signal_mul(innov2, innov2, MULT16_32_P15(QCONST16(0.4f,15),scale), st->subframeSize);
921
922
127k
            for (i=0;i<st->subframeSize;i++)
923
124k
               innov[i] = ADD32(innov[i],innov2[i]);
924
3.11k
            stack = tmp_stack;
925
3.11k
         }
926
354k
         for (i=0;i<st->subframeSize;i++)
927
346k
            exc[i] = PSHR32(innov[i],SIG_SHIFT);
928
929
8.65k
         if (st->innov_rms_save)
930
3.54k
         {
931
3.54k
            st->innov_rms_save[sub] = MULT16_16_Q15(QCONST16(.70711f, 15), compute_rms(innov, st->subframeSize));
932
3.54k
         }
933
8.65k
         st->exc_rms[sub] = compute_rms16(exc, st->subframeSize);
934
935
936
8.65k
      }
937
938
939
      /*Keep the previous memory*/
940
338k
      for (i=0;i<st->lpcSize;i++)
941
300k
         mem[i]=st->mem_sp[i];
942
      /* Final signal synthesis from excitation */
943
37.5k
      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
37.5k
      filter_mem16(sp, bw_lpc1, bw_lpc2, sw, st->subframeSize, st->lpcSize, st->mem_sw, stack);
947
37.5k
   }
948
949
84.5k
   for (i=0;i<st->lpcSize;i++)
950
75.1k
      st->old_lsp[i] = lsp[i];
951
84.5k
   for (i=0;i<st->lpcSize;i++)
952
75.1k
      st->old_qlsp[i] = qlsp[i];
953
954
9.39k
   st->first=0;
955
956
9.39k
   return 1;
957
10.6k
}
sb_encode
Line
Count
Source
516
3.58k
{
517
3.58k
   SBEncState *st;
518
3.58k
   int i, roots, sub;
519
3.58k
   char *stack;
520
3.58k
   VARDECL(spx_mem_t *mem);
521
3.58k
   VARDECL(spx_sig_t *innov);
522
3.58k
   VARDECL(spx_word16_t *target);
523
3.58k
   VARDECL(spx_word16_t *syn_resp);
524
3.58k
   VARDECL(spx_word32_t *low_pi_gain);
525
3.58k
   spx_word16_t *low;
526
3.58k
   spx_word16_t *high;
527
3.58k
   VARDECL(spx_word16_t *low_exc_rms);
528
3.58k
   VARDECL(spx_word16_t *low_innov_rms);
529
3.58k
   const SpeexSBMode *mode;
530
3.58k
   spx_int32_t dtx;
531
3.58k
   spx_word16_t *in = (spx_word16_t*)vin;
532
3.58k
   spx_word16_t e_low=0, e_high=0;
533
3.58k
   VARDECL(spx_coef_t *lpc);
534
3.58k
   VARDECL(spx_coef_t *interp_lpc);
535
3.58k
   VARDECL(spx_coef_t *bw_lpc1);
536
3.58k
   VARDECL(spx_coef_t *bw_lpc2);
537
3.58k
   VARDECL(spx_lsp_t *lsp);
538
3.58k
   VARDECL(spx_lsp_t *qlsp);
539
3.58k
   VARDECL(spx_lsp_t *interp_lsp);
540
3.58k
   VARDECL(spx_lsp_t *interp_qlsp);
541
542
3.58k
   st = (SBEncState*)state;
543
3.58k
   stack=st->stack;
544
3.58k
   mode = (const SpeexSBMode*)(st->mode->mode);
545
3.58k
   low = in;
546
3.58k
   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.58k
   qmf_decomp(in, h0, low, high, st->full_frame_size, QMF_ORDER, st->h0_mem, stack);
551
552
3.58k
#ifndef DISABLE_VBR
553
3.58k
   if (st->vbr_enabled || st->vad_enabled)
554
2.50k
   {
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.50k
      e_low = compute_rms16(low, st->frame_size);
558
2.50k
      e_high = compute_rms16(high, st->frame_size);
559
2.50k
   }
560
3.58k
#endif /* #ifndef DISABLE_VBR */
561
562
3.58k
   ALLOC(low_innov_rms, st->nbSubframes, spx_word16_t);
563
3.58k
   speex_encoder_ctl(st->st_low, SPEEX_SET_INNOVATION_SAVE, low_innov_rms);
564
   /* Encode the narrowband part*/
565
3.58k
   speex_encode_native(st->st_low, low, bits);
566
567
3.58k
   high = high - (st->windowSize-st->frame_size);
568
3.58k
   SPEEX_COPY(high, st->high, st->windowSize-st->frame_size);
569
3.58k
   SPEEX_COPY(st->high, &high[st->frame_size], st->windowSize-st->frame_size);
570
571
572
3.58k
   ALLOC(low_pi_gain, st->nbSubframes, spx_word32_t);
573
3.58k
   ALLOC(low_exc_rms, st->nbSubframes, spx_word16_t);
574
3.58k
   speex_encoder_ctl(st->st_low, SPEEX_GET_PI_GAIN, low_pi_gain);
575
3.58k
   speex_encoder_ctl(st->st_low, SPEEX_GET_EXC, low_exc_rms);
576
577
3.58k
   speex_encoder_ctl(st->st_low, SPEEX_GET_LOW_MODE, &dtx);
578
579
3.58k
   if (dtx==0)
580
238
      dtx=1;
581
3.35k
   else
582
3.35k
      dtx=0;
583
584
3.58k
   ALLOC(lpc, st->lpcSize, spx_coef_t);
585
3.58k
   ALLOC(interp_lpc, st->lpcSize, spx_coef_t);
586
3.58k
   ALLOC(bw_lpc1, st->lpcSize, spx_coef_t);
587
3.58k
   ALLOC(bw_lpc2, st->lpcSize, spx_coef_t);
588
589
3.58k
   ALLOC(lsp, st->lpcSize, spx_lsp_t);
590
3.58k
   ALLOC(qlsp, st->lpcSize, spx_lsp_t);
591
3.58k
   ALLOC(interp_lsp, st->lpcSize, spx_lsp_t);
592
3.58k
   ALLOC(interp_qlsp, st->lpcSize, spx_lsp_t);
593
594
3.58k
   {
595
3.58k
      VARDECL(spx_word16_t *autocorr);
596
3.58k
      VARDECL(spx_word16_t *w_sig);
597
3.58k
      ALLOC(autocorr, st->lpcSize+1, spx_word16_t);
598
3.58k
      ALLOC(w_sig, st->windowSize, spx_word16_t);
599
      /* Window for analysis */
600
      /* FIXME: This is a kludge */
601
3.58k
      if (st->subframeSize==80)
602
879
      {
603
352k
         for (i=0;i<st->windowSize;i++)
604
351k
            w_sig[i] = EXTRACT16(SHR32(MULT16_16(high[i],st->window[i>>1]),SIG_SHIFT));
605
2.70k
      } else {
606
544k
         for (i=0;i<st->windowSize;i++)
607
541k
            w_sig[i] = EXTRACT16(SHR32(MULT16_16(high[i],st->window[i]),SIG_SHIFT));
608
2.70k
      }
609
      /* Compute auto-correlation */
610
3.58k
      _spx_autocorr(w_sig, autocorr, st->lpcSize+1, st->windowSize);
611
3.58k
      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
35.8k
      for (i=0;i<st->lpcSize+1;i++)
615
32.2k
         autocorr[i] = MULT16_16_Q14(autocorr[i],st->lagWindow[i]);
616
617
      /* Levinson-Durbin */
618
3.58k
      _spx_lpc(lpc, autocorr, st->lpcSize);
619
3.58k
   }
620
621
   /* LPC to LSPs (x-domain) transform */
622
3.58k
   roots=lpc_to_lsp (lpc, st->lpcSize, lsp, 10, LSP_DELTA1, stack);
623
3.58k
   if (roots!=st->lpcSize)
624
1
   {
625
1
      roots = lpc_to_lsp (lpc, st->lpcSize, lsp, 10, LSP_DELTA2, stack);
626
1
      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
1
   }
634
635
3.58k
#ifndef DISABLE_VBR
636
   /* VBR code */
637
3.58k
   if ((st->vbr_enabled || st->vad_enabled) && !dtx)
638
2.26k
   {
639
2.26k
      float ratio;
640
2.26k
      if (st->abr_enabled)
641
628
      {
642
628
         float qual_change=0;
643
628
         if (st->abr_drift2 * st->abr_drift > 0)
644
456
         {
645
            /* Only adapt if long-term and short-term drift are the same sign */
646
456
            qual_change = -.00001*st->abr_drift/(1+st->abr_count);
647
456
            if (qual_change>.1)
648
0
               qual_change=.1;
649
456
            if (qual_change<-.1)
650
124
               qual_change=-.1;
651
456
         }
652
628
         st->vbr_quality += qual_change;
653
628
         if (st->vbr_quality>10)
654
0
            st->vbr_quality=10;
655
628
         if (st->vbr_quality<0)
656
456
            st->vbr_quality=0;
657
628
      }
658
659
660
2.26k
      ratio = 2*log((1.f+e_high)/(1.f+e_low));
661
662
2.26k
      speex_encoder_ctl(st->st_low, SPEEX_GET_RELATIVE_QUALITY, &st->relative_quality);
663
2.26k
      if (ratio<-4)
664
72
         ratio=-4;
665
2.26k
      if (ratio>2)
666
15
         ratio=2;
667
      /*if (ratio>-2)*/
668
2.26k
      if (st->vbr_enabled)
669
1.73k
      {
670
1.73k
         spx_int32_t modeid;
671
1.73k
         modeid = mode->nb_modes-1;
672
1.73k
         st->relative_quality+=1.0*(ratio+2);
673
1.73k
   if (st->relative_quality<-1)
674
13
            st->relative_quality=-1;
675
5.57k
         while (modeid)
676
5.38k
         {
677
5.38k
            int v1;
678
5.38k
            float thresh;
679
5.38k
            v1=(int)floor(st->vbr_quality);
680
5.38k
            if (v1==10)
681
629
               thresh = mode->vbr_thresh[modeid][v1];
682
4.75k
            else
683
4.75k
               thresh = (st->vbr_quality-v1)   * mode->vbr_thresh[modeid][v1+1] +
684
4.75k
                        (1+v1-st->vbr_quality) * mode->vbr_thresh[modeid][v1];
685
5.38k
            if (st->relative_quality >= thresh && st->sampling_rate*st->submodes[modeid]->bits_per_frame/st->full_frame_size <= st->vbr_max_high)
686
1.53k
               break;
687
3.84k
            modeid--;
688
3.84k
         }
689
1.73k
         speex_encoder_ctl(state, SPEEX_SET_HIGH_MODE, &modeid);
690
1.73k
         if (st->abr_enabled)
691
628
         {
692
628
            spx_int32_t bitrate;
693
628
            speex_encoder_ctl(state, SPEEX_GET_BITRATE, &bitrate);
694
628
            st->abr_drift+=(bitrate-st->abr_enabled);
695
628
            st->abr_drift2 = .95*st->abr_drift2 + .05*(bitrate-st->abr_enabled);
696
628
            st->abr_count += 1.0;
697
628
         }
698
699
1.73k
      } else {
700
         /* VAD only */
701
531
         int modeid;
702
531
         if (st->relative_quality<2.0)
703
185
            modeid=1;
704
346
         else
705
346
            modeid=st->submodeSelect;
706
         /*speex_encoder_ctl(state, SPEEX_SET_MODE, &mode);*/
707
531
         st->submodeID=modeid;
708
709
531
      }
710
      /*fprintf (stderr, "%f %f\n", ratio, low_qual);*/
711
2.26k
   }
712
3.58k
#endif /* #ifndef DISABLE_VBR */
713
714
3.58k
   if (st->encode_submode)
715
3.58k
   {
716
3.58k
      speex_bits_pack(bits, 1, 1);
717
3.58k
      if (dtx)
718
238
         speex_bits_pack(bits, 0, SB_SUBMODE_BITS);
719
3.35k
      else
720
3.35k
         speex_bits_pack(bits, st->submodeID, SB_SUBMODE_BITS);
721
3.58k
   }
722
723
   /* If null mode (no transmission), just set a couple things to zero*/
724
3.58k
   if (dtx || st->submodes[st->submodeID] == NULL)
725
521
   {
726
108k
      for (i=0;i<st->frame_size;i++)
727
108k
         high[i]=VERY_SMALL;
728
729
4.68k
      for (i=0;i<st->lpcSize;i++)
730
4.16k
         st->mem_sw[i]=0;
731
521
      st->first=1;
732
733
      /* Final signal synthesis from excitation */
734
521
      iir_mem16(high, st->interp_qlpc, high, st->frame_size, st->lpcSize, st->mem_sp, stack);
735
736
521
      if (dtx)
737
238
         return 0;
738
283
      else
739
283
         return 1;
740
521
   }
741
742
743
   /* LSP quantization */
744
3.06k
   SUBMODE(lsp_quant)(lsp, qlsp, st->lpcSize, bits);
745
746
3.06k
   if (st->first)
747
897
   {
748
8.07k
      for (i=0;i<st->lpcSize;i++)
749
7.17k
         st->old_lsp[i] = lsp[i];
750
8.07k
      for (i=0;i<st->lpcSize;i++)
751
7.17k
         st->old_qlsp[i] = qlsp[i];
752
897
   }
753
754
3.06k
   ALLOC(mem, st->lpcSize, spx_mem_t);
755
3.06k
   ALLOC(syn_resp, st->subframeSize, spx_word16_t);
756
3.06k
   ALLOC(innov, st->subframeSize, spx_sig_t);
757
3.06k
   ALLOC(target, st->subframeSize, spx_word16_t);
758
759
15.3k
   for (sub=0;sub<st->nbSubframes;sub++)
760
12.2k
   {
761
12.2k
      VARDECL(spx_word16_t *exc);
762
12.2k
      VARDECL(spx_word16_t *res);
763
12.2k
      VARDECL(spx_word16_t *sw);
764
12.2k
      spx_word16_t *sp;
765
12.2k
      spx_word16_t filter_ratio;     /*Q7*/
766
12.2k
      int offset;
767
12.2k
      spx_word32_t rl, rh;           /*Q13*/
768
12.2k
      spx_word16_t eh=0;
769
770
12.2k
      offset = st->subframeSize*sub;
771
12.2k
      sp=high+offset;
772
12.2k
      ALLOC(exc, st->subframeSize, spx_word16_t);
773
12.2k
      ALLOC(res, st->subframeSize, spx_word16_t);
774
12.2k
      ALLOC(sw, st->subframeSize, spx_word16_t);
775
776
      /* LSP interpolation (quantized and unquantized) */
777
12.2k
      lsp_interpolate(st->old_lsp, lsp, interp_lsp, st->lpcSize, sub, st->nbSubframes, LSP_MARGIN);
778
12.2k
      lsp_interpolate(st->old_qlsp, qlsp, interp_qlsp, st->lpcSize, sub, st->nbSubframes, LSP_MARGIN);
779
780
12.2k
      lsp_to_lpc(interp_lsp, interp_lpc, st->lpcSize,stack);
781
12.2k
      lsp_to_lpc(interp_qlsp, st->interp_qlpc, st->lpcSize, stack);
782
783
12.2k
      bw_lpc(st->gamma1, interp_lpc, bw_lpc1, st->lpcSize);
784
12.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
12.2k
      st->pi_gain[sub]=LPC_SCALING;
789
12.2k
      rh = LPC_SCALING;
790
61.3k
      for (i=0;i<st->lpcSize;i+=2)
791
49.0k
      {
792
49.0k
         rh += st->interp_qlpc[i+1] - st->interp_qlpc[i];
793
49.0k
         st->pi_gain[sub] += st->interp_qlpc[i] + st->interp_qlpc[i+1];
794
49.0k
      }
795
796
12.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
12.2k
      filter_ratio=(rl+.01)/(rh+.01);
801
12.2k
#endif
802
803
      /* Compute "real excitation" */
804
12.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
12.2k
      eh = compute_rms16(exc, st->subframeSize);
808
809
12.2k
      if (!SUBMODE(innovation_quant)) {/* 1 for spectral folding excitation, 0 for stochastic */
810
9.52k
         spx_word32_t g;   /*Q7*/
811
9.52k
         spx_word16_t el;  /*Q0*/
812
9.52k
         el = low_innov_rms[sub];
813
814
         /* Gain to use if we want to use the low-band excitation for high-band */
815
9.52k
         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
9.52k
         {
836
9.52k
            int quant = scal_quant(g, fold_quant_bound, 32);
837
            /*speex_warning_int("tata", quant);*/
838
9.52k
            if (quant<0)
839
0
               quant=0;
840
9.52k
            if (quant>31)
841
0
               quant=31;
842
9.52k
            speex_bits_pack(bits, quant, 5);
843
9.52k
         }
844
9.52k
         if (st->innov_rms_save)
845
2.25k
         {
846
2.25k
            st->innov_rms_save[sub] = eh;
847
2.25k
         }
848
9.52k
         st->exc_rms[sub] = eh;
849
9.52k
      } else {
850
2.74k
         spx_word16_t gc;       /*Q7*/
851
2.74k
         spx_word32_t scale;    /*Q14*/
852
2.74k
         spx_word16_t el;       /*Q0*/
853
2.74k
         el = low_exc_rms[sub]; /*Q0*/
854
855
2.74k
         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.74k
         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.74k
         {
862
2.74k
            int qgc = scal_quant(gc, gc_quant_bound, 16);
863
2.74k
            speex_bits_pack(bits, qgc, 4);
864
2.74k
            gc = MULT16_16_Q15(QCONST16(0.87360,15),gc_quant_bound[qgc]);
865
2.74k
         }
866
2.74k
         if (st->subframeSize==80)
867
0
            gc = MULT16_16_P14(QCONST16(1.4142f,14), gc);
868
869
2.74k
         scale = SHL32(MULT16_16(PDIV32_16(SHL32(EXTEND32(gc),SIG_SHIFT-6),MAX16(EPSILON,filter_ratio)),(1+el)),6);
870
871
2.74k
         compute_impulse_response(st->interp_qlpc, bw_lpc1, bw_lpc2, syn_resp, st->subframeSize, st->lpcSize, stack);
872
873
874
         /* Reset excitation */
875
112k
         for (i=0;i<st->subframeSize;i++)
876
109k
            res[i]=VERY_SMALL;
877
878
         /* Compute zero response (ringing) of A(z/g1) / ( A(z/g2) * Aq(z) ) */
879
24.7k
         for (i=0;i<st->lpcSize;i++)
880
21.9k
            mem[i]=st->mem_sp[i];
881
2.74k
         iir_mem16(res, st->interp_qlpc, res, st->subframeSize, st->lpcSize, mem, stack);
882
883
24.7k
         for (i=0;i<st->lpcSize;i++)
884
21.9k
            mem[i]=st->mem_sw[i];
885
2.74k
         filter_mem16(res, bw_lpc1, bw_lpc2, res, st->subframeSize, st->lpcSize, mem, stack);
886
887
         /* Compute weighted signal */
888
24.7k
         for (i=0;i<st->lpcSize;i++)
889
21.9k
            mem[i]=st->mem_sw[i];
890
2.74k
         filter_mem16(sp, bw_lpc1, bw_lpc2, sw, st->subframeSize, st->lpcSize, mem, stack);
891
892
         /* Compute target signal */
893
112k
         for (i=0;i<st->subframeSize;i++)
894
109k
            target[i]=SUB16(sw[i],res[i]);
895
896
2.74k
         signal_div(target, target, scale, st->subframeSize);
897
898
         /* Reset excitation */
899
2.74k
         SPEEX_MEMSET(innov, 0, st->subframeSize);
900
901
         /*print_vec(target, st->subframeSize, "\ntarget");*/
902
2.74k
         SUBMODE(innovation_quant)(target, st->interp_qlpc, bw_lpc1, bw_lpc2,
903
2.74k
                                   SUBMODE(innovation_params), st->lpcSize, st->subframeSize,
904
2.74k
                                   innov, syn_resp, bits, stack, st->complexity, SUBMODE(double_codebook));
905
         /*print_vec(target, st->subframeSize, "after");*/
906
907
2.74k
         signal_mul(innov, innov, scale, st->subframeSize);
908
909
2.74k
         if (SUBMODE(double_codebook)) {
910
644
            char *tmp_stack=stack;
911
644
            VARDECL(spx_sig_t *innov2);
912
644
            ALLOC(innov2, st->subframeSize, spx_sig_t);
913
644
            SPEEX_MEMSET(innov2, 0, st->subframeSize);
914
26.4k
            for (i=0;i<st->subframeSize;i++)
915
25.7k
               target[i]=MULT16_16_P13(QCONST16(2.5f,13), target[i]);
916
917
644
            SUBMODE(innovation_quant)(target, st->interp_qlpc, bw_lpc1, bw_lpc2,
918
644
                                      SUBMODE(innovation_params), st->lpcSize, st->subframeSize,
919
644
                                      innov2, syn_resp, bits, stack, st->complexity, 0);
920
644
            signal_mul(innov2, innov2, MULT16_32_P15(QCONST16(0.4f,15),scale), st->subframeSize);
921
922
26.4k
            for (i=0;i<st->subframeSize;i++)
923
25.7k
               innov[i] = ADD32(innov[i],innov2[i]);
924
644
            stack = tmp_stack;
925
644
         }
926
112k
         for (i=0;i<st->subframeSize;i++)
927
109k
            exc[i] = PSHR32(innov[i],SIG_SHIFT);
928
929
2.74k
         if (st->innov_rms_save)
930
1.18k
         {
931
1.18k
            st->innov_rms_save[sub] = MULT16_16_Q15(QCONST16(.70711f, 15), compute_rms(innov, st->subframeSize));
932
1.18k
         }
933
2.74k
         st->exc_rms[sub] = compute_rms16(exc, st->subframeSize);
934
935
936
2.74k
      }
937
938
939
      /*Keep the previous memory*/
940
110k
      for (i=0;i<st->lpcSize;i++)
941
98.1k
         mem[i]=st->mem_sp[i];
942
      /* Final signal synthesis from excitation */
943
12.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
12.2k
      filter_mem16(sp, bw_lpc1, bw_lpc2, sw, st->subframeSize, st->lpcSize, st->mem_sw, stack);
947
12.2k
   }
948
949
27.6k
   for (i=0;i<st->lpcSize;i++)
950
24.5k
      st->old_lsp[i] = lsp[i];
951
27.6k
   for (i=0;i<st->lpcSize;i++)
952
24.5k
      st->old_qlsp[i] = qlsp[i];
953
954
3.06k
   st->first=0;
955
956
3.06k
   return 1;
957
3.58k
}
sb_encode
Line
Count
Source
516
7.01k
{
517
7.01k
   SBEncState *st;
518
7.01k
   int i, roots, sub;
519
7.01k
   char *stack;
520
7.01k
   VARDECL(spx_mem_t *mem);
521
7.01k
   VARDECL(spx_sig_t *innov);
522
7.01k
   VARDECL(spx_word16_t *target);
523
7.01k
   VARDECL(spx_word16_t *syn_resp);
524
7.01k
   VARDECL(spx_word32_t *low_pi_gain);
525
7.01k
   spx_word16_t *low;
526
7.01k
   spx_word16_t *high;
527
7.01k
   VARDECL(spx_word16_t *low_exc_rms);
528
7.01k
   VARDECL(spx_word16_t *low_innov_rms);
529
7.01k
   const SpeexSBMode *mode;
530
7.01k
   spx_int32_t dtx;
531
7.01k
   spx_word16_t *in = (spx_word16_t*)vin;
532
7.01k
   spx_word16_t e_low=0, e_high=0;
533
7.01k
   VARDECL(spx_coef_t *lpc);
534
7.01k
   VARDECL(spx_coef_t *interp_lpc);
535
7.01k
   VARDECL(spx_coef_t *bw_lpc1);
536
7.01k
   VARDECL(spx_coef_t *bw_lpc2);
537
7.01k
   VARDECL(spx_lsp_t *lsp);
538
7.01k
   VARDECL(spx_lsp_t *qlsp);
539
7.01k
   VARDECL(spx_lsp_t *interp_lsp);
540
7.01k
   VARDECL(spx_lsp_t *interp_qlsp);
541
542
7.01k
   st = (SBEncState*)state;
543
7.01k
   stack=st->stack;
544
7.01k
   mode = (const SpeexSBMode*)(st->mode->mode);
545
7.01k
   low = in;
546
7.01k
   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
7.01k
   qmf_decomp(in, h0, low, high, st->full_frame_size, QMF_ORDER, st->h0_mem, stack);
551
552
7.01k
#ifndef DISABLE_VBR
553
7.01k
   if (st->vbr_enabled || st->vad_enabled)
554
5.22k
   {
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
5.22k
      e_low = compute_rms16(low, st->frame_size);
558
5.22k
      e_high = compute_rms16(high, st->frame_size);
559
5.22k
   }
560
7.01k
#endif /* #ifndef DISABLE_VBR */
561
562
7.01k
   ALLOC(low_innov_rms, st->nbSubframes, spx_word16_t);
563
7.01k
   speex_encoder_ctl(st->st_low, SPEEX_SET_INNOVATION_SAVE, low_innov_rms);
564
   /* Encode the narrowband part*/
565
7.01k
   speex_encode_native(st->st_low, low, bits);
566
567
7.01k
   high = high - (st->windowSize-st->frame_size);
568
7.01k
   SPEEX_COPY(high, st->high, st->windowSize-st->frame_size);
569
7.01k
   SPEEX_COPY(st->high, &high[st->frame_size], st->windowSize-st->frame_size);
570
571
572
7.01k
   ALLOC(low_pi_gain, st->nbSubframes, spx_word32_t);
573
7.01k
   ALLOC(low_exc_rms, st->nbSubframes, spx_word16_t);
574
7.01k
   speex_encoder_ctl(st->st_low, SPEEX_GET_PI_GAIN, low_pi_gain);
575
7.01k
   speex_encoder_ctl(st->st_low, SPEEX_GET_EXC, low_exc_rms);
576
577
7.01k
   speex_encoder_ctl(st->st_low, SPEEX_GET_LOW_MODE, &dtx);
578
579
7.01k
   if (dtx==0)
580
287
      dtx=1;
581
6.73k
   else
582
6.73k
      dtx=0;
583
584
7.01k
   ALLOC(lpc, st->lpcSize, spx_coef_t);
585
7.01k
   ALLOC(interp_lpc, st->lpcSize, spx_coef_t);
586
7.01k
   ALLOC(bw_lpc1, st->lpcSize, spx_coef_t);
587
7.01k
   ALLOC(bw_lpc2, st->lpcSize, spx_coef_t);
588
589
7.01k
   ALLOC(lsp, st->lpcSize, spx_lsp_t);
590
7.01k
   ALLOC(qlsp, st->lpcSize, spx_lsp_t);
591
7.01k
   ALLOC(interp_lsp, st->lpcSize, spx_lsp_t);
592
7.01k
   ALLOC(interp_qlsp, st->lpcSize, spx_lsp_t);
593
594
7.01k
   {
595
7.01k
      VARDECL(spx_word16_t *autocorr);
596
7.01k
      VARDECL(spx_word16_t *w_sig);
597
7.01k
      ALLOC(autocorr, st->lpcSize+1, spx_word16_t);
598
7.01k
      ALLOC(w_sig, st->windowSize, spx_word16_t);
599
      /* Window for analysis */
600
      /* FIXME: This is a kludge */
601
7.01k
      if (st->subframeSize==80)
602
1.52k
      {
603
612k
         for (i=0;i<st->windowSize;i++)
604
610k
            w_sig[i] = EXTRACT16(SHR32(MULT16_16(high[i],st->window[i>>1]),SIG_SHIFT));
605
5.49k
      } else {
606
1.10M
         for (i=0;i<st->windowSize;i++)
607
1.09M
            w_sig[i] = EXTRACT16(SHR32(MULT16_16(high[i],st->window[i]),SIG_SHIFT));
608
5.49k
      }
609
      /* Compute auto-correlation */
610
7.01k
      _spx_autocorr(w_sig, autocorr, st->lpcSize+1, st->windowSize);
611
7.01k
      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
70.1k
      for (i=0;i<st->lpcSize+1;i++)
615
63.1k
         autocorr[i] = MULT16_16_Q14(autocorr[i],st->lagWindow[i]);
616
617
      /* Levinson-Durbin */
618
7.01k
      _spx_lpc(lpc, autocorr, st->lpcSize);
619
7.01k
   }
620
621
   /* LPC to LSPs (x-domain) transform */
622
7.01k
   roots=lpc_to_lsp (lpc, st->lpcSize, lsp, 10, LSP_DELTA1, stack);
623
7.01k
   if (roots!=st->lpcSize)
624
150
   {
625
150
      roots = lpc_to_lsp (lpc, st->lpcSize, lsp, 10, LSP_DELTA2, stack);
626
150
      if (roots!=st->lpcSize) {
627
         /*If we can't find all LSP's, do some damage control and use a flat filter*/
628
1.00k
         for (i=0;i<st->lpcSize;i++)
629
896
         {
630
896
            lsp[i]=st->old_lsp[i];
631
896
         }
632
112
      }
633
150
   }
634
635
7.01k
#ifndef DISABLE_VBR
636
   /* VBR code */
637
7.01k
   if ((st->vbr_enabled || st->vad_enabled) && !dtx)
638
4.93k
   {
639
4.93k
      float ratio;
640
4.93k
      if (st->abr_enabled)
641
2.12k
      {
642
2.12k
         float qual_change=0;
643
2.12k
         if (st->abr_drift2 * st->abr_drift > 0)
644
1.62k
         {
645
            /* Only adapt if long-term and short-term drift are the same sign */
646
1.62k
            qual_change = -.00001*st->abr_drift/(1+st->abr_count);
647
1.62k
            if (qual_change>.1)
648
0
               qual_change=.1;
649
1.62k
            if (qual_change<-.1)
650
486
               qual_change=-.1;
651
1.62k
         }
652
2.12k
         st->vbr_quality += qual_change;
653
2.12k
         if (st->vbr_quality>10)
654
0
            st->vbr_quality=10;
655
2.12k
         if (st->vbr_quality<0)
656
1.62k
            st->vbr_quality=0;
657
2.12k
      }
658
659
660
4.93k
      ratio = 2*log((1.f+e_high)/(1.f+e_low));
661
662
4.93k
      speex_encoder_ctl(st->st_low, SPEEX_GET_RELATIVE_QUALITY, &st->relative_quality);
663
4.93k
      if (ratio<-4)
664
70
         ratio=-4;
665
4.93k
      if (ratio>2)
666
53
         ratio=2;
667
      /*if (ratio>-2)*/
668
4.93k
      if (st->vbr_enabled)
669
4.15k
      {
670
4.15k
         spx_int32_t modeid;
671
4.15k
         modeid = mode->nb_modes-1;
672
4.15k
         st->relative_quality+=1.0*(ratio+2);
673
4.15k
   if (st->relative_quality<-1)
674
6
            st->relative_quality=-1;
675
13.1k
         while (modeid)
676
12.9k
         {
677
12.9k
            int v1;
678
12.9k
            float thresh;
679
12.9k
            v1=(int)floor(st->vbr_quality);
680
12.9k
            if (v1==10)
681
542
               thresh = mode->vbr_thresh[modeid][v1];
682
12.3k
            else
683
12.3k
               thresh = (st->vbr_quality-v1)   * mode->vbr_thresh[modeid][v1+1] +
684
12.3k
                        (1+v1-st->vbr_quality) * mode->vbr_thresh[modeid][v1];
685
12.9k
            if (st->relative_quality >= thresh && st->sampling_rate*st->submodes[modeid]->bits_per_frame/st->full_frame_size <= st->vbr_max_high)
686
3.89k
               break;
687
9.03k
            modeid--;
688
9.03k
         }
689
4.15k
         speex_encoder_ctl(state, SPEEX_SET_HIGH_MODE, &modeid);
690
4.15k
         if (st->abr_enabled)
691
2.12k
         {
692
2.12k
            spx_int32_t bitrate;
693
2.12k
            speex_encoder_ctl(state, SPEEX_GET_BITRATE, &bitrate);
694
2.12k
            st->abr_drift+=(bitrate-st->abr_enabled);
695
2.12k
            st->abr_drift2 = .95*st->abr_drift2 + .05*(bitrate-st->abr_enabled);
696
2.12k
            st->abr_count += 1.0;
697
2.12k
         }
698
699
4.15k
      } else {
700
         /* VAD only */
701
782
         int modeid;
702
782
         if (st->relative_quality<2.0)
703
132
            modeid=1;
704
650
         else
705
650
            modeid=st->submodeSelect;
706
         /*speex_encoder_ctl(state, SPEEX_SET_MODE, &mode);*/
707
782
         st->submodeID=modeid;
708
709
782
      }
710
      /*fprintf (stderr, "%f %f\n", ratio, low_qual);*/
711
4.93k
   }
712
7.01k
#endif /* #ifndef DISABLE_VBR */
713
714
7.01k
   if (st->encode_submode)
715
7.01k
   {
716
7.01k
      speex_bits_pack(bits, 1, 1);
717
7.01k
      if (dtx)
718
287
         speex_bits_pack(bits, 0, SB_SUBMODE_BITS);
719
6.73k
      else
720
6.73k
         speex_bits_pack(bits, st->submodeID, SB_SUBMODE_BITS);
721
7.01k
   }
722
723
   /* If null mode (no transmission), just set a couple things to zero*/
724
7.01k
   if (dtx || st->submodes[st->submodeID] == NULL)
725
695
   {
726
147k
      for (i=0;i<st->frame_size;i++)
727
147k
         high[i]=VERY_SMALL;
728
729
6.25k
      for (i=0;i<st->lpcSize;i++)
730
5.56k
         st->mem_sw[i]=0;
731
695
      st->first=1;
732
733
      /* Final signal synthesis from excitation */
734
695
      iir_mem16(high, st->interp_qlpc, high, st->frame_size, st->lpcSize, st->mem_sp, stack);
735
736
695
      if (dtx)
737
287
         return 0;
738
408
      else
739
408
         return 1;
740
695
   }
741
742
743
   /* LSP quantization */
744
6.32k
   SUBMODE(lsp_quant)(lsp, qlsp, st->lpcSize, bits);
745
746
6.32k
   if (st->first)
747
1.73k
   {
748
15.5k
      for (i=0;i<st->lpcSize;i++)
749
13.8k
         st->old_lsp[i] = lsp[i];
750
15.5k
      for (i=0;i<st->lpcSize;i++)
751
13.8k
         st->old_qlsp[i] = qlsp[i];
752
1.73k
   }
753
754
6.32k
   ALLOC(mem, st->lpcSize, spx_mem_t);
755
6.32k
   ALLOC(syn_resp, st->subframeSize, spx_word16_t);
756
6.32k
   ALLOC(innov, st->subframeSize, spx_sig_t);
757
6.32k
   ALLOC(target, st->subframeSize, spx_word16_t);
758
759
31.6k
   for (sub=0;sub<st->nbSubframes;sub++)
760
25.2k
   {
761
25.2k
      VARDECL(spx_word16_t *exc);
762
25.2k
      VARDECL(spx_word16_t *res);
763
25.2k
      VARDECL(spx_word16_t *sw);
764
25.2k
      spx_word16_t *sp;
765
25.2k
      spx_word16_t filter_ratio;     /*Q7*/
766
25.2k
      int offset;
767
25.2k
      spx_word32_t rl, rh;           /*Q13*/
768
25.2k
      spx_word16_t eh=0;
769
770
25.2k
      offset = st->subframeSize*sub;
771
25.2k
      sp=high+offset;
772
25.2k
      ALLOC(exc, st->subframeSize, spx_word16_t);
773
25.2k
      ALLOC(res, st->subframeSize, spx_word16_t);
774
25.2k
      ALLOC(sw, st->subframeSize, spx_word16_t);
775
776
      /* LSP interpolation (quantized and unquantized) */
777
25.2k
      lsp_interpolate(st->old_lsp, lsp, interp_lsp, st->lpcSize, sub, st->nbSubframes, LSP_MARGIN);
778
25.2k
      lsp_interpolate(st->old_qlsp, qlsp, interp_qlsp, st->lpcSize, sub, st->nbSubframes, LSP_MARGIN);
779
780
25.2k
      lsp_to_lpc(interp_lsp, interp_lpc, st->lpcSize,stack);
781
25.2k
      lsp_to_lpc(interp_qlsp, st->interp_qlpc, st->lpcSize, stack);
782
783
25.2k
      bw_lpc(st->gamma1, interp_lpc, bw_lpc1, st->lpcSize);
784
25.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
25.2k
      st->pi_gain[sub]=LPC_SCALING;
789
25.2k
      rh = LPC_SCALING;
790
126k
      for (i=0;i<st->lpcSize;i+=2)
791
101k
      {
792
101k
         rh += st->interp_qlpc[i+1] - st->interp_qlpc[i];
793
101k
         st->pi_gain[sub] += st->interp_qlpc[i] + st->interp_qlpc[i+1];
794
101k
      }
795
796
25.2k
      rl = low_pi_gain[sub];
797
25.2k
#ifdef FIXED_POINT
798
25.2k
      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
25.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
25.2k
      eh = compute_rms16(exc, st->subframeSize);
808
809
25.2k
      if (!SUBMODE(innovation_quant)) {/* 1 for spectral folding excitation, 0 for stochastic */
810
19.3k
         spx_word32_t g;   /*Q7*/
811
19.3k
         spx_word16_t el;  /*Q0*/
812
19.3k
         el = low_innov_rms[sub];
813
814
         /* Gain to use if we want to use the low-band excitation for high-band */
815
19.3k
         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
19.3k
         {
836
19.3k
            int quant = scal_quant(g, fold_quant_bound, 32);
837
            /*speex_warning_int("tata", quant);*/
838
19.3k
            if (quant<0)
839
0
               quant=0;
840
19.3k
            if (quant>31)
841
0
               quant=31;
842
19.3k
            speex_bits_pack(bits, quant, 5);
843
19.3k
         }
844
19.3k
         if (st->innov_rms_save)
845
3.56k
         {
846
3.56k
            st->innov_rms_save[sub] = eh;
847
3.56k
         }
848
19.3k
         st->exc_rms[sub] = eh;
849
19.3k
      } else {
850
5.90k
         spx_word16_t gc;       /*Q7*/
851
5.90k
         spx_word32_t scale;    /*Q14*/
852
5.90k
         spx_word16_t el;       /*Q0*/
853
5.90k
         el = low_exc_rms[sub]; /*Q0*/
854
855
5.90k
         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.90k
         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.90k
         {
862
5.90k
            int qgc = scal_quant(gc, gc_quant_bound, 16);
863
5.90k
            speex_bits_pack(bits, qgc, 4);
864
5.90k
            gc = MULT16_16_Q15(QCONST16(0.87360,15),gc_quant_bound[qgc]);
865
5.90k
         }
866
5.90k
         if (st->subframeSize==80)
867
0
            gc = MULT16_16_P14(QCONST16(1.4142f,14), gc);
868
869
5.90k
         scale = SHL32(MULT16_16(PDIV32_16(SHL32(EXTEND32(gc),SIG_SHIFT-6),MAX16(EPSILON,filter_ratio)),(1+el)),6);
870
871
5.90k
         compute_impulse_response(st->interp_qlpc, bw_lpc1, bw_lpc2, syn_resp, st->subframeSize, st->lpcSize, stack);
872
873
874
         /* Reset excitation */
875
242k
         for (i=0;i<st->subframeSize;i++)
876
236k
            res[i]=VERY_SMALL;
877
878
         /* Compute zero response (ringing) of A(z/g1) / ( A(z/g2) * Aq(z) ) */
879
53.1k
         for (i=0;i<st->lpcSize;i++)
880
47.2k
            mem[i]=st->mem_sp[i];
881
5.90k
         iir_mem16(res, st->interp_qlpc, res, st->subframeSize, st->lpcSize, mem, stack);
882
883
53.1k
         for (i=0;i<st->lpcSize;i++)
884
47.2k
            mem[i]=st->mem_sw[i];
885
5.90k
         filter_mem16(res, bw_lpc1, bw_lpc2, res, st->subframeSize, st->lpcSize, mem, stack);
886
887
         /* Compute weighted signal */
888
53.1k
         for (i=0;i<st->lpcSize;i++)
889
47.2k
            mem[i]=st->mem_sw[i];
890
5.90k
         filter_mem16(sp, bw_lpc1, bw_lpc2, sw, st->subframeSize, st->lpcSize, mem, stack);
891
892
         /* Compute target signal */
893
242k
         for (i=0;i<st->subframeSize;i++)
894
236k
            target[i]=SUB16(sw[i],res[i]);
895
896
5.90k
         signal_div(target, target, scale, st->subframeSize);
897
898
         /* Reset excitation */
899
5.90k
         SPEEX_MEMSET(innov, 0, st->subframeSize);
900
901
         /*print_vec(target, st->subframeSize, "\ntarget");*/
902
5.90k
         SUBMODE(innovation_quant)(target, st->interp_qlpc, bw_lpc1, bw_lpc2,
903
5.90k
                                   SUBMODE(innovation_params), st->lpcSize, st->subframeSize,
904
5.90k
                                   innov, syn_resp, bits, stack, st->complexity, SUBMODE(double_codebook));
905
         /*print_vec(target, st->subframeSize, "after");*/
906
907
5.90k
         signal_mul(innov, innov, scale, st->subframeSize);
908
909
5.90k
         if (SUBMODE(double_codebook)) {
910
2.46k
            char *tmp_stack=stack;
911
2.46k
            VARDECL(spx_sig_t *innov2);
912
2.46k
            ALLOC(innov2, st->subframeSize, spx_sig_t);
913
2.46k
            SPEEX_MEMSET(innov2, 0, st->subframeSize);
914
101k
            for (i=0;i<st->subframeSize;i++)
915
98.7k
               target[i]=MULT16_16_P13(QCONST16(2.5f,13), target[i]);
916
917
2.46k
            SUBMODE(innovation_quant)(target, st->interp_qlpc, bw_lpc1, bw_lpc2,
918
2.46k
                                      SUBMODE(innovation_params), st->lpcSize, st->subframeSize,
919
2.46k
                                      innov2, syn_resp, bits, stack, st->complexity, 0);
920
2.46k
            signal_mul(innov2, innov2, MULT16_32_P15(QCONST16(0.4f,15),scale), st->subframeSize);
921
922
101k
            for (i=0;i<st->subframeSize;i++)
923
98.7k
               innov[i] = ADD32(innov[i],innov2[i]);
924
2.46k
            stack = tmp_stack;
925
2.46k
         }
926
242k
         for (i=0;i<st->subframeSize;i++)
927
236k
            exc[i] = PSHR32(innov[i],SIG_SHIFT);
928
929
5.90k
         if (st->innov_rms_save)
930
2.36k
         {
931
2.36k
            st->innov_rms_save[sub] = MULT16_16_Q15(QCONST16(.70711f, 15), compute_rms(innov, st->subframeSize));
932
2.36k
         }
933
5.90k
         st->exc_rms[sub] = compute_rms16(exc, st->subframeSize);
934
935
936
5.90k
      }
937
938
939
      /*Keep the previous memory*/
940
227k
      for (i=0;i<st->lpcSize;i++)
941
202k
         mem[i]=st->mem_sp[i];
942
      /* Final signal synthesis from excitation */
943
25.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
25.2k
      filter_mem16(sp, bw_lpc1, bw_lpc2, sw, st->subframeSize, st->lpcSize, st->mem_sw, stack);
947
25.2k
   }
948
949
56.9k
   for (i=0;i<st->lpcSize;i++)
950
50.5k
      st->old_lsp[i] = lsp[i];
951
56.9k
   for (i=0;i<st->lpcSize;i++)
952
50.5k
      st->old_qlsp[i] = qlsp[i];
953
954
6.32k
   st->first=0;
955
956
6.32k
   return 1;
957
7.01k
}
958
959
#endif /* DISABLE_ENCODER */
960
961
962
#ifndef DISABLE_DECODER
963
void *sb_decoder_init(const SpeexMode *m)
964
4.53k
{
965
4.53k
   spx_int32_t tmp;
966
4.53k
   SBDecState *st;
967
4.53k
   const SpeexSBMode *mode;
968
4.53k
   st = (SBDecState*)speex_alloc(sizeof(SBDecState));
969
4.53k
   if (!st)
970
0
      return NULL;
971
4.53k
   st->mode = m;
972
4.53k
   mode=(const SpeexSBMode*)m->mode;
973
4.53k
   st->encode_submode = 1;
974
975
4.53k
   st->st_low = speex_decoder_init(mode->nb_mode);
976
4.53k
#if defined(VAR_ARRAYS) || defined (USE_ALLOCA)
977
4.53k
   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.53k
   st->full_frame_size = 2*mode->frameSize;
984
4.53k
   st->frame_size = mode->frameSize;
985
4.53k
   st->subframeSize = mode->subframeSize;
986
4.53k
   st->nbSubframes = mode->frameSize/mode->subframeSize;
987
4.53k
   st->lpcSize=mode->lpcSize;
988
4.53k
   speex_decoder_ctl(st->st_low, SPEEX_GET_SAMPLING_RATE, &st->sampling_rate);
989
4.53k
   st->sampling_rate*=2;
990
4.53k
   tmp=1;
991
4.53k
   speex_decoder_ctl(st->st_low, SPEEX_SET_WIDEBAND, &tmp);
992
993
4.53k
   st->submodes=mode->submodes;
994
4.53k
   st->submodeID=mode->defaultSubmode;
995
996
4.53k
   st->first=1;
997
998
4.53k
   st->g0_mem = (spx_word16_t*)speex_alloc((QMF_ORDER)*sizeof(spx_word16_t));
999
4.53k
   st->g1_mem = (spx_word16_t*)speex_alloc((QMF_ORDER)*sizeof(spx_word16_t));
1000
1001
4.53k
   st->excBuf = (spx_word16_t*)speex_alloc((st->subframeSize)*sizeof(spx_word16_t));
1002
1003
4.53k
   st->old_qlsp = (spx_lsp_t*)speex_alloc((st->lpcSize)*sizeof(spx_lsp_t));
1004
4.53k
   st->interp_qlpc = (spx_coef_t*)speex_alloc(st->lpcSize*sizeof(spx_coef_t));
1005
1006
4.53k
   st->pi_gain = (spx_word32_t*)speex_alloc((st->nbSubframes)*sizeof(spx_word32_t));
1007
4.53k
   st->exc_rms = (spx_word16_t*)speex_alloc((st->nbSubframes)*sizeof(spx_word16_t));
1008
4.53k
   st->mem_sp = (spx_mem_t*)speex_alloc((2*st->lpcSize)*sizeof(spx_mem_t));
1009
1010
4.53k
   st->innov_save = NULL;
1011
1012
1013
4.53k
   st->lpc_enh_enabled=0;
1014
4.53k
   st->seed = 1000;
1015
1016
#ifdef ENABLE_VALGRIND
1017
   VALGRIND_MAKE_MEM_DEFINED(st, (st->stack-(char*)st));
1018
#endif
1019
4.53k
   return st;
1020
4.53k
}
1021
1022
void sb_decoder_destroy(void *state)
1023
4.53k
{
1024
4.53k
   SBDecState *st;
1025
4.53k
   st = (SBDecState*)state;
1026
4.53k
   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.53k
   speex_free(st->g0_mem);
1032
4.53k
   speex_free(st->g1_mem);
1033
4.53k
   speex_free(st->excBuf);
1034
4.53k
   speex_free(st->old_qlsp);
1035
4.53k
   speex_free(st->interp_qlpc);
1036
4.53k
   speex_free(st->pi_gain);
1037
4.53k
   speex_free(st->exc_rms);
1038
4.53k
   speex_free(st->mem_sp);
1039
1040
4.53k
   speex_free(state);
1041
4.53k
}
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
28.1k
   case SPEEX_GET_FRAME_SIZE:
1061
28.1k
      (*(spx_int32_t*)ptr) = st->full_frame_size;
1062
28.1k
      break;
1063
4.53k
   case SPEEX_SET_ENH:
1064
4.53k
      speex_decoder_ctl(st->st_low, request, ptr);
1065
4.53k
      st->lpc_enh_enabled = *((spx_int32_t*)ptr);
1066
4.53k
      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.53k
   case SPEEX_SET_SAMPLING_RATE:
1092
4.53k
      {
1093
4.53k
         spx_int32_t tmp=(*(spx_int32_t*)ptr);
1094
4.53k
         st->sampling_rate = tmp;
1095
4.53k
         tmp>>=1;
1096
4.53k
         speex_decoder_ctl(st->st_low, SPEEX_SET_SAMPLING_RATE, &tmp);
1097
4.53k
      }
1098
4.53k
      break;
1099
1.71k
   case SPEEX_GET_SAMPLING_RATE:
1100
1.71k
      (*(spx_int32_t*)ptr)=st->sampling_rate;
1101
1.71k
      break;
1102
2.17k
   case SPEEX_SET_HANDLER:
1103
2.17k
      speex_decoder_ctl(st->st_low, SPEEX_SET_HANDLER, ptr);
1104
2.17k
      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.35k
   case SPEEX_GET_LOOKAHEAD:
1126
4.35k
      speex_decoder_ctl(st->st_low, SPEEX_GET_LOOKAHEAD, ptr);
1127
4.35k
      (*(spx_int32_t*)ptr) = 2*(*(spx_int32_t*)ptr);
1128
4.35k
      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.07k
   case SPEEX_GET_PI_GAIN:
1139
2.07k
      {
1140
2.07k
         int i;
1141
2.07k
         spx_word32_t *g = (spx_word32_t*)ptr;
1142
10.3k
         for (i=0;i<st->nbSubframes;i++)
1143
8.29k
            g[i]=st->pi_gain[i];
1144
2.07k
      }
1145
2.07k
      break;
1146
2.07k
   case SPEEX_GET_EXC:
1147
2.07k
      {
1148
2.07k
         int i;
1149
10.3k
         for (i=0;i<st->nbSubframes;i++)
1150
8.29k
            ((spx_word16_t*)ptr)[i] = st->exc_rms[i];
1151
2.07k
      }
1152
2.07k
      break;
1153
36.7k
   case SPEEX_GET_DTX_STATUS:
1154
36.7k
      speex_decoder_ctl(st->st_low, SPEEX_GET_DTX_STATUS, ptr);
1155
36.7k
      break;
1156
36.7k
   case SPEEX_SET_INNOVATION_SAVE:
1157
36.7k
      st->innov_save = (spx_word16_t*)ptr;
1158
36.7k
      break;
1159
1.71k
   case SPEEX_SET_WIDEBAND:
1160
1.71k
      speex_decoder_ctl(st->st_low, SPEEX_SET_WIDEBAND, ptr);
1161
1.71k
      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
4.72k
{
1175
4.72k
   int i;
1176
4.72k
   int saved_modeid=0;
1177
1178
4.72k
   if (dtx)
1179
4.72k
   {
1180
4.72k
      saved_modeid=st->submodeID;
1181
4.72k
      st->submodeID=1;
1182
4.72k
   } else {
1183
0
      bw_lpc(QCONST16(0.99f,15), st->interp_qlpc, st->interp_qlpc, st->lpcSize);
1184
0
   }
1185
1186
4.72k
   st->first=1;
1187
1188
1189
   /* Final signal synthesis from excitation */
1190
4.72k
   if (!dtx)
1191
0
   {
1192
0
      st->last_ener =  MULT16_16_Q15(QCONST16(.9f,15),st->last_ener);
1193
0
   }
1194
1.17M
   for (i=0;i<st->frame_size;i++)
1195
1.16M
      out[i+st->frame_size] = speex_rand(st->last_ener, &st->seed);
1196
1197
4.72k
   iir_mem16(out+st->frame_size, st->interp_qlpc, out+st->frame_size, st->frame_size, st->lpcSize,
1198
4.72k
            st->mem_sp, stack);
1199
1200
1201
   /* Reconstruct the original */
1202
4.72k
   qmf_synth(out, out+st->frame_size, h0, out, st->full_frame_size, QMF_ORDER, st->g0_mem, st->g1_mem, stack);
1203
4.72k
   if (dtx)
1204
4.72k
   {
1205
4.72k
      st->submodeID=saved_modeid;
1206
4.72k
   }
1207
1208
4.72k
   return;
1209
4.72k
}
sb_celp.c:sb_decode_lost
Line
Count
Source
1174
2.36k
{
1175
2.36k
   int i;
1176
2.36k
   int saved_modeid=0;
1177
1178
2.36k
   if (dtx)
1179
2.36k
   {
1180
2.36k
      saved_modeid=st->submodeID;
1181
2.36k
      st->submodeID=1;
1182
2.36k
   } else {
1183
0
      bw_lpc(QCONST16(0.99f,15), st->interp_qlpc, st->interp_qlpc, st->lpcSize);
1184
0
   }
1185
1186
2.36k
   st->first=1;
1187
1188
1189
   /* Final signal synthesis from excitation */
1190
2.36k
   if (!dtx)
1191
0
   {
1192
0
      st->last_ener =  MULT16_16_Q15(QCONST16(.9f,15),st->last_ener);
1193
0
   }
1194
587k
   for (i=0;i<st->frame_size;i++)
1195
584k
      out[i+st->frame_size] = speex_rand(st->last_ener, &st->seed);
1196
1197
2.36k
   iir_mem16(out+st->frame_size, st->interp_qlpc, out+st->frame_size, st->frame_size, st->lpcSize,
1198
2.36k
            st->mem_sp, stack);
1199
1200
1201
   /* Reconstruct the original */
1202
2.36k
   qmf_synth(out, out+st->frame_size, h0, out, st->full_frame_size, QMF_ORDER, st->g0_mem, st->g1_mem, stack);
1203
2.36k
   if (dtx)
1204
2.36k
   {
1205
2.36k
      st->submodeID=saved_modeid;
1206
2.36k
   }
1207
1208
2.36k
   return;
1209
2.36k
}
sb_celp.c:sb_decode_lost
Line
Count
Source
1174
2.36k
{
1175
2.36k
   int i;
1176
2.36k
   int saved_modeid=0;
1177
1178
2.36k
   if (dtx)
1179
2.36k
   {
1180
2.36k
      saved_modeid=st->submodeID;
1181
2.36k
      st->submodeID=1;
1182
2.36k
   } else {
1183
0
      bw_lpc(QCONST16(0.99f,15), st->interp_qlpc, st->interp_qlpc, st->lpcSize);
1184
0
   }
1185
1186
2.36k
   st->first=1;
1187
1188
1189
   /* Final signal synthesis from excitation */
1190
2.36k
   if (!dtx)
1191
0
   {
1192
0
      st->last_ener =  MULT16_16_Q15(QCONST16(.9f,15),st->last_ener);
1193
0
   }
1194
587k
   for (i=0;i<st->frame_size;i++)
1195
584k
      out[i+st->frame_size] = speex_rand(st->last_ener, &st->seed);
1196
1197
2.36k
   iir_mem16(out+st->frame_size, st->interp_qlpc, out+st->frame_size, st->frame_size, st->lpcSize,
1198
2.36k
            st->mem_sp, stack);
1199
1200
1201
   /* Reconstruct the original */
1202
2.36k
   qmf_synth(out, out+st->frame_size, h0, out, st->full_frame_size, QMF_ORDER, st->g0_mem, st->g1_mem, stack);
1203
2.36k
   if (dtx)
1204
2.36k
   {
1205
2.36k
      st->submodeID=saved_modeid;
1206
2.36k
   }
1207
1208
2.36k
   return;
1209
2.36k
}
1210
1211
int sb_decode(void *state, SpeexBits *bits, void *vout)
1212
87.9k
{
1213
87.9k
   int i, sub;
1214
87.9k
   SBDecState *st;
1215
87.9k
   int wideband;
1216
87.9k
   int ret;
1217
87.9k
   char *stack;
1218
87.9k
   VARDECL(spx_word32_t *low_pi_gain);
1219
87.9k
   VARDECL(spx_word16_t *low_exc_rms);
1220
87.9k
   VARDECL(spx_coef_t *ak);
1221
87.9k
   VARDECL(spx_lsp_t *qlsp);
1222
87.9k
   VARDECL(spx_lsp_t *interp_qlsp);
1223
87.9k
   spx_int32_t dtx;
1224
87.9k
   const SpeexSBMode *mode;
1225
87.9k
   spx_word16_t *out = (spx_word16_t*)vout;
1226
87.9k
   spx_word16_t *low_innov_alias;
1227
87.9k
   spx_word32_t exc_ener_sum = 0;
1228
1229
87.9k
   st = (SBDecState*)state;
1230
87.9k
   stack=st->stack;
1231
87.9k
   mode = (const SpeexSBMode*)(st->mode->mode);
1232
1233
87.9k
   low_innov_alias = out+st->frame_size;
1234
87.9k
   speex_decoder_ctl(st->st_low, SPEEX_SET_INNOVATION_SAVE, low_innov_alias);
1235
   /* Decode the low-band */
1236
87.9k
   ret = speex_decode_native(st->st_low, bits, out);
1237
1238
87.9k
   speex_decoder_ctl(st->st_low, SPEEX_GET_DTX_STATUS, &dtx);
1239
1240
   /* If error decoding the narrowband part, propagate error */
1241
87.9k
   if (ret!=0)
1242
27.1k
   {
1243
27.1k
      return ret;
1244
27.1k
   }
1245
1246
60.7k
   if (!bits)
1247
0
   {
1248
0
      sb_decode_lost(st, out, dtx, stack);
1249
0
      return 0;
1250
0
   }
1251
1252
60.7k
   if (st->encode_submode)
1253
60.7k
   {
1254
1255
      /*Check "wideband bit"*/
1256
60.7k
      if (speex_bits_remaining(bits)>0)
1257
33.6k
         wideband = speex_bits_peek(bits);
1258
27.0k
      else
1259
27.0k
         wideband = 0;
1260
60.7k
      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
50.5k
      {
1267
         /*Was a narrowband frame, set "null submode"*/
1268
50.5k
         st->submodeID = 0;
1269
50.5k
      }
1270
60.7k
      if (st->submodeID != 0 && st->submodes[st->submodeID] == NULL)
1271
859
      {
1272
859
         speex_notify("Invalid mode encountered. The stream is corrupted.");
1273
859
         return -2;
1274
859
      }
1275
60.7k
   }
1276
1277
   /* If null mode (no transmission), just set a couple things to zero*/
1278
59.9k
   if (st->submodes[st->submodeID] == NULL)
1279
53.0k
   {
1280
53.0k
      if (st->innov_save)
1281
21.2k
        SPEEX_MEMSET(st->innov_save, 0, st->full_frame_size);
1282
1283
53.0k
      if (dtx)
1284
2.36k
      {
1285
2.36k
         sb_decode_lost(st, out, 1, stack);
1286
2.36k
         return 0;
1287
2.36k
      }
1288
1289
11.6M
      for (i=0;i<st->frame_size;i++)
1290
11.5M
         out[st->frame_size+i]=VERY_SMALL;
1291
1292
50.7k
      st->first=1;
1293
1294
      /* Final signal synthesis from excitation */
1295
50.7k
      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.7k
      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.7k
      return 0;
1300
1301
53.0k
   }
1302
1303
6.84k
   ALLOC(low_pi_gain, st->nbSubframes, spx_word32_t);
1304
6.84k
   ALLOC(low_exc_rms, st->nbSubframes, spx_word16_t);
1305
6.84k
   speex_decoder_ctl(st->st_low, SPEEX_GET_PI_GAIN, low_pi_gain);
1306
6.84k
   speex_decoder_ctl(st->st_low, SPEEX_GET_EXC, low_exc_rms);
1307
1308
6.84k
   ALLOC(qlsp, st->lpcSize, spx_lsp_t);
1309
6.84k
   ALLOC(interp_qlsp, st->lpcSize, spx_lsp_t);
1310
6.84k
   SUBMODE(lsp_unquant)(qlsp, st->lpcSize, bits);
1311
1312
6.84k
   if (st->first)
1313
3.02k
   {
1314
27.2k
      for (i=0;i<st->lpcSize;i++)
1315
24.2k
         st->old_qlsp[i] = qlsp[i];
1316
3.02k
   }
1317
1318
6.84k
   ALLOC(ak, st->lpcSize, spx_coef_t);
1319
1320
34.2k
   for (sub=0;sub<st->nbSubframes;sub++)
1321
27.3k
   {
1322
27.3k
      VARDECL(spx_word32_t *exc);
1323
27.3k
      spx_word16_t *innov_save=NULL;
1324
27.3k
      spx_word16_t *sp;
1325
27.3k
      spx_word16_t filter_ratio;
1326
27.3k
      spx_word16_t el=0;
1327
27.3k
      int offset;
1328
27.3k
      spx_word32_t rl=0,rh=0;
1329
1330
27.3k
      offset = st->subframeSize*sub;
1331
27.3k
      sp=out+st->frame_size+offset;
1332
27.3k
      ALLOC(exc, st->subframeSize, spx_word32_t);
1333
      /* Pointer for saving innovation */
1334
27.3k
      if (st->innov_save)
1335
15.2k
      {
1336
15.2k
         innov_save = st->innov_save+2*offset;
1337
15.2k
         SPEEX_MEMSET(innov_save, 0, 2*st->subframeSize);
1338
15.2k
      }
1339
1340
      /* LSP interpolation */
1341
27.3k
      lsp_interpolate(st->old_qlsp, qlsp, interp_qlsp, st->lpcSize, sub, st->nbSubframes, LSP_MARGIN);
1342
1343
      /* LSP to LPC */
1344
27.3k
      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
27.3k
         st->pi_gain[sub]=LPC_SCALING;
1350
27.3k
         rh = LPC_SCALING;
1351
136k
         for (i=0;i<st->lpcSize;i+=2)
1352
109k
         {
1353
109k
            rh += ak[i+1] - ak[i];
1354
109k
            st->pi_gain[sub] += ak[i] + ak[i+1];
1355
109k
         }
1356
1357
27.3k
         rl = low_pi_gain[sub];
1358
#ifdef FIXED_POINT
1359
14.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
27.3k
      SPEEX_MEMSET(exc, 0, st->subframeSize);
1365
27.3k
      if (!SUBMODE(innovation_unquant))
1366
11.9k
      {
1367
11.9k
         spx_word32_t g;
1368
11.9k
         int quant;
1369
1370
11.9k
         quant = speex_bits_unpack_unsigned(bits, 5);
1371
11.9k
         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.9k
         g = PDIV32(g, MAX16(EPSILON,filter_ratio));
1375
1376
417k
         for (i=0;i<st->subframeSize;i+=2)
1377
405k
         {
1378
405k
            exc[i]=SHL32(MULT16_32_P15(MULT16_16_Q15(mode->folding_gain,low_innov_alias[offset+i]),SHL32(g,6)),SIG_SHIFT);
1379
405k
            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
405k
         }
1381
1382
15.3k
      } else {
1383
15.3k
         spx_word16_t gc;
1384
15.3k
         spx_word32_t scale;
1385
15.3k
         int qgc = speex_bits_unpack_unsigned(bits, 4);
1386
1387
15.3k
         el = low_exc_rms[sub];
1388
15.3k
         gc = MULT16_16_Q15(QCONST16(0.87360,15),gc_quant_bound[qgc]);
1389
1390
15.3k
         if (st->subframeSize==80)
1391
0
            gc = MULT16_16_P14(QCONST16(1.4142f,14),gc);
1392
1393
15.3k
         scale = SHL32(PDIV32(SHL32(MULT16_16(gc, el),3), MAX16(EPSILON,filter_ratio)),SIG_SHIFT-3);
1394
15.3k
         SUBMODE(innovation_unquant)(exc, SUBMODE(innovation_params), st->subframeSize,
1395
15.3k
                                     bits, stack, &st->seed);
1396
1397
15.3k
         signal_mul(exc,exc,scale,st->subframeSize);
1398
1399
15.3k
         if (SUBMODE(double_codebook)) {
1400
5.14k
            char *tmp_stack=stack;
1401
5.14k
            VARDECL(spx_sig_t *innov2);
1402
5.14k
            ALLOC(innov2, st->subframeSize, spx_sig_t);
1403
5.14k
            SPEEX_MEMSET(innov2, 0, st->subframeSize);
1404
5.14k
            SUBMODE(innovation_unquant)(innov2, SUBMODE(innovation_params), st->subframeSize,
1405
5.14k
                                        bits, stack, &st->seed);
1406
5.14k
            signal_mul(innov2, innov2, MULT16_32_P15(QCONST16(0.4f,15),scale), st->subframeSize);
1407
210k
            for (i=0;i<st->subframeSize;i++)
1408
205k
               exc[i] = ADD32(exc[i],innov2[i]);
1409
5.14k
            stack = tmp_stack;
1410
5.14k
         }
1411
1412
15.3k
      }
1413
1414
27.3k
      if (st->innov_save)
1415
15.2k
      {
1416
626k
         for (i=0;i<st->subframeSize;i++)
1417
611k
            innov_save[2*i]=EXTRACT16(PSHR32(exc[i],SIG_SHIFT));
1418
15.2k
      }
1419
1420
27.3k
      iir_mem16(st->excBuf, st->interp_qlpc, sp, st->subframeSize, st->lpcSize,
1421
27.3k
               st->mem_sp, stack);
1422
1.45M
      for (i=0;i<st->subframeSize;i++)
1423
1.42M
         st->excBuf[i]=EXTRACT16(PSHR32(exc[i],SIG_SHIFT));
1424
246k
      for (i=0;i<st->lpcSize;i++)
1425
218k
         st->interp_qlpc[i] = ak[i];
1426
27.3k
      st->exc_rms[sub] = compute_rms16(st->excBuf, st->subframeSize);
1427
27.3k
      exc_ener_sum = ADD32(exc_ener_sum, DIV32(MULT16_16(st->exc_rms[sub],st->exc_rms[sub]), st->nbSubframes));
1428
27.3k
   }
1429
6.84k
   st->last_ener = spx_sqrt(exc_ener_sum);
1430
1431
6.84k
   qmf_synth(out, out+st->frame_size, h0, out, st->full_frame_size, QMF_ORDER, st->g0_mem, st->g1_mem, stack);
1432
61.5k
   for (i=0;i<st->lpcSize;i++)
1433
54.7k
      st->old_qlsp[i] = qlsp[i];
1434
1435
6.84k
   st->first=0;
1436
1437
6.84k
   return 0;
1438
59.9k
}
sb_decode
Line
Count
Source
1212
46.4k
{
1213
46.4k
   int i, sub;
1214
46.4k
   SBDecState *st;
1215
46.4k
   int wideband;
1216
46.4k
   int ret;
1217
46.4k
   char *stack;
1218
46.4k
   VARDECL(spx_word32_t *low_pi_gain);
1219
46.4k
   VARDECL(spx_word16_t *low_exc_rms);
1220
46.4k
   VARDECL(spx_coef_t *ak);
1221
46.4k
   VARDECL(spx_lsp_t *qlsp);
1222
46.4k
   VARDECL(spx_lsp_t *interp_qlsp);
1223
46.4k
   spx_int32_t dtx;
1224
46.4k
   const SpeexSBMode *mode;
1225
46.4k
   spx_word16_t *out = (spx_word16_t*)vout;
1226
46.4k
   spx_word16_t *low_innov_alias;
1227
46.4k
   spx_word32_t exc_ener_sum = 0;
1228
1229
46.4k
   st = (SBDecState*)state;
1230
46.4k
   stack=st->stack;
1231
46.4k
   mode = (const SpeexSBMode*)(st->mode->mode);
1232
1233
46.4k
   low_innov_alias = out+st->frame_size;
1234
46.4k
   speex_decoder_ctl(st->st_low, SPEEX_SET_INNOVATION_SAVE, low_innov_alias);
1235
   /* Decode the low-band */
1236
46.4k
   ret = speex_decode_native(st->st_low, bits, out);
1237
1238
46.4k
   speex_decoder_ctl(st->st_low, SPEEX_GET_DTX_STATUS, &dtx);
1239
1240
   /* If error decoding the narrowband part, propagate error */
1241
46.4k
   if (ret!=0)
1242
16.2k
   {
1243
16.2k
      return ret;
1244
16.2k
   }
1245
1246
30.2k
   if (!bits)
1247
0
   {
1248
0
      sb_decode_lost(st, out, dtx, stack);
1249
0
      return 0;
1250
0
   }
1251
1252
30.2k
   if (st->encode_submode)
1253
30.2k
   {
1254
1255
      /*Check "wideband bit"*/
1256
30.2k
      if (speex_bits_remaining(bits)>0)
1257
17.3k
         wideband = speex_bits_peek(bits);
1258
12.9k
      else
1259
12.9k
         wideband = 0;
1260
30.2k
      if (wideband)
1261
5.21k
      {
1262
         /*Regular wideband frame, read the submode*/
1263
5.21k
         wideband = speex_bits_unpack_unsigned(bits, 1);
1264
5.21k
         st->submodeID = speex_bits_unpack_unsigned(bits, SB_SUBMODE_BITS);
1265
5.21k
      } else
1266
25.0k
      {
1267
         /*Was a narrowband frame, set "null submode"*/
1268
25.0k
         st->submodeID = 0;
1269
25.0k
      }
1270
30.2k
      if (st->submodeID != 0 && st->submodes[st->submodeID] == NULL)
1271
353
      {
1272
353
         speex_notify("Invalid mode encountered. The stream is corrupted.");
1273
353
         return -2;
1274
353
      }
1275
30.2k
   }
1276
1277
   /* If null mode (no transmission), just set a couple things to zero*/
1278
29.9k
   if (st->submodes[st->submodeID] == NULL)
1279
26.6k
   {
1280
26.6k
      if (st->innov_save)
1281
11.2k
        SPEEX_MEMSET(st->innov_save, 0, st->full_frame_size);
1282
1283
26.6k
      if (dtx)
1284
1.54k
      {
1285
1.54k
         sb_decode_lost(st, out, 1, stack);
1286
1.54k
         return 0;
1287
1.54k
      }
1288
1289
5.77M
      for (i=0;i<st->frame_size;i++)
1290
5.75M
         out[st->frame_size+i]=VERY_SMALL;
1291
1292
25.0k
      st->first=1;
1293
1294
      /* Final signal synthesis from excitation */
1295
25.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
25.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
25.0k
      return 0;
1300
1301
26.6k
   }
1302
1303
3.29k
   ALLOC(low_pi_gain, st->nbSubframes, spx_word32_t);
1304
3.29k
   ALLOC(low_exc_rms, st->nbSubframes, spx_word16_t);
1305
3.29k
   speex_decoder_ctl(st->st_low, SPEEX_GET_PI_GAIN, low_pi_gain);
1306
3.29k
   speex_decoder_ctl(st->st_low, SPEEX_GET_EXC, low_exc_rms);
1307
1308
3.29k
   ALLOC(qlsp, st->lpcSize, spx_lsp_t);
1309
3.29k
   ALLOC(interp_qlsp, st->lpcSize, spx_lsp_t);
1310
3.29k
   SUBMODE(lsp_unquant)(qlsp, st->lpcSize, bits);
1311
1312
3.29k
   if (st->first)
1313
1.35k
   {
1314
12.1k
      for (i=0;i<st->lpcSize;i++)
1315
10.8k
         st->old_qlsp[i] = qlsp[i];
1316
1.35k
   }
1317
1318
3.29k
   ALLOC(ak, st->lpcSize, spx_coef_t);
1319
1320
16.4k
   for (sub=0;sub<st->nbSubframes;sub++)
1321
13.1k
   {
1322
13.1k
      VARDECL(spx_word32_t *exc);
1323
13.1k
      spx_word16_t *innov_save=NULL;
1324
13.1k
      spx_word16_t *sp;
1325
13.1k
      spx_word16_t filter_ratio;
1326
13.1k
      spx_word16_t el=0;
1327
13.1k
      int offset;
1328
13.1k
      spx_word32_t rl=0,rh=0;
1329
1330
13.1k
      offset = st->subframeSize*sub;
1331
13.1k
      sp=out+st->frame_size+offset;
1332
13.1k
      ALLOC(exc, st->subframeSize, spx_word32_t);
1333
      /* Pointer for saving innovation */
1334
13.1k
      if (st->innov_save)
1335
7.32k
      {
1336
7.32k
         innov_save = st->innov_save+2*offset;
1337
7.32k
         SPEEX_MEMSET(innov_save, 0, 2*st->subframeSize);
1338
7.32k
      }
1339
1340
      /* LSP interpolation */
1341
13.1k
      lsp_interpolate(st->old_qlsp, qlsp, interp_qlsp, st->lpcSize, sub, st->nbSubframes, LSP_MARGIN);
1342
1343
      /* LSP to LPC */
1344
13.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
13.1k
         st->pi_gain[sub]=LPC_SCALING;
1350
13.1k
         rh = LPC_SCALING;
1351
65.9k
         for (i=0;i<st->lpcSize;i+=2)
1352
52.7k
         {
1353
52.7k
            rh += ak[i+1] - ak[i];
1354
52.7k
            st->pi_gain[sub] += ak[i] + ak[i+1];
1355
52.7k
         }
1356
1357
13.1k
         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
13.1k
         filter_ratio=(rl+.01)/(rh+.01);
1362
13.1k
#endif
1363
1364
13.1k
      SPEEX_MEMSET(exc, 0, st->subframeSize);
1365
13.1k
      if (!SUBMODE(innovation_unquant))
1366
7.24k
      {
1367
7.24k
         spx_word32_t g;
1368
7.24k
         int quant;
1369
1370
7.24k
         quant = speex_bits_unpack_unsigned(bits, 5);
1371
7.24k
         g= spx_exp(MULT16_16(QCONST16(.125f,11),(quant-10)));
1372
1373
         /* Clamp to a minimum of epsilon to avoid division by 0 */
1374
7.24k
         g = PDIV32(g, MAX16(EPSILON,filter_ratio));
1375
1376
261k
         for (i=0;i<st->subframeSize;i+=2)
1377
253k
         {
1378
253k
            exc[i]=SHL32(MULT16_32_P15(MULT16_16_Q15(mode->folding_gain,low_innov_alias[offset+i]),SHL32(g,6)),SIG_SHIFT);
1379
253k
            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
253k
         }
1381
1382
7.24k
      } else {
1383
5.95k
         spx_word16_t gc;
1384
5.95k
         spx_word32_t scale;
1385
5.95k
         int qgc = speex_bits_unpack_unsigned(bits, 4);
1386
1387
5.95k
         el = low_exc_rms[sub];
1388
5.95k
         gc = MULT16_16_Q15(QCONST16(0.87360,15),gc_quant_bound[qgc]);
1389
1390
5.95k
         if (st->subframeSize==80)
1391
0
            gc = MULT16_16_P14(QCONST16(1.4142f,14),gc);
1392
1393
5.95k
         scale = SHL32(PDIV32(SHL32(MULT16_16(gc, el),3), MAX16(EPSILON,filter_ratio)),SIG_SHIFT-3);
1394
5.95k
         SUBMODE(innovation_unquant)(exc, SUBMODE(innovation_params), st->subframeSize,
1395
5.95k
                                     bits, stack, &st->seed);
1396
1397
5.95k
         signal_mul(exc,exc,scale,st->subframeSize);
1398
1399
5.95k
         if (SUBMODE(double_codebook)) {
1400
2.52k
            char *tmp_stack=stack;
1401
2.52k
            VARDECL(spx_sig_t *innov2);
1402
2.52k
            ALLOC(innov2, st->subframeSize, spx_sig_t);
1403
2.52k
            SPEEX_MEMSET(innov2, 0, st->subframeSize);
1404
2.52k
            SUBMODE(innovation_unquant)(innov2, SUBMODE(innovation_params), st->subframeSize,
1405
2.52k
                                        bits, stack, &st->seed);
1406
2.52k
            signal_mul(innov2, innov2, MULT16_32_P15(QCONST16(0.4f,15),scale), st->subframeSize);
1407
103k
            for (i=0;i<st->subframeSize;i++)
1408
101k
               exc[i] = ADD32(exc[i],innov2[i]);
1409
2.52k
            stack = tmp_stack;
1410
2.52k
         }
1411
1412
5.95k
      }
1413
1414
13.1k
      if (st->innov_save)
1415
7.32k
      {
1416
300k
         for (i=0;i<st->subframeSize;i++)
1417
292k
            innov_save[2*i]=EXTRACT16(PSHR32(exc[i],SIG_SHIFT));
1418
7.32k
      }
1419
1420
13.1k
      iir_mem16(st->excBuf, st->interp_qlpc, sp, st->subframeSize, st->lpcSize,
1421
13.1k
               st->mem_sp, stack);
1422
758k
      for (i=0;i<st->subframeSize;i++)
1423
745k
         st->excBuf[i]=EXTRACT16(PSHR32(exc[i],SIG_SHIFT));
1424
118k
      for (i=0;i<st->lpcSize;i++)
1425
105k
         st->interp_qlpc[i] = ak[i];
1426
13.1k
      st->exc_rms[sub] = compute_rms16(st->excBuf, st->subframeSize);
1427
13.1k
      exc_ener_sum = ADD32(exc_ener_sum, DIV32(MULT16_16(st->exc_rms[sub],st->exc_rms[sub]), st->nbSubframes));
1428
13.1k
   }
1429
3.29k
   st->last_ener = spx_sqrt(exc_ener_sum);
1430
1431
3.29k
   qmf_synth(out, out+st->frame_size, h0, out, st->full_frame_size, QMF_ORDER, st->g0_mem, st->g1_mem, stack);
1432
29.6k
   for (i=0;i<st->lpcSize;i++)
1433
26.3k
      st->old_qlsp[i] = qlsp[i];
1434
1435
3.29k
   st->first=0;
1436
1437
3.29k
   return 0;
1438
29.9k
}
sb_decode
Line
Count
Source
1212
41.4k
{
1213
41.4k
   int i, sub;
1214
41.4k
   SBDecState *st;
1215
41.4k
   int wideband;
1216
41.4k
   int ret;
1217
41.4k
   char *stack;
1218
41.4k
   VARDECL(spx_word32_t *low_pi_gain);
1219
41.4k
   VARDECL(spx_word16_t *low_exc_rms);
1220
41.4k
   VARDECL(spx_coef_t *ak);
1221
41.4k
   VARDECL(spx_lsp_t *qlsp);
1222
41.4k
   VARDECL(spx_lsp_t *interp_qlsp);
1223
41.4k
   spx_int32_t dtx;
1224
41.4k
   const SpeexSBMode *mode;
1225
41.4k
   spx_word16_t *out = (spx_word16_t*)vout;
1226
41.4k
   spx_word16_t *low_innov_alias;
1227
41.4k
   spx_word32_t exc_ener_sum = 0;
1228
1229
41.4k
   st = (SBDecState*)state;
1230
41.4k
   stack=st->stack;
1231
41.4k
   mode = (const SpeexSBMode*)(st->mode->mode);
1232
1233
41.4k
   low_innov_alias = out+st->frame_size;
1234
41.4k
   speex_decoder_ctl(st->st_low, SPEEX_SET_INNOVATION_SAVE, low_innov_alias);
1235
   /* Decode the low-band */
1236
41.4k
   ret = speex_decode_native(st->st_low, bits, out);
1237
1238
41.4k
   speex_decoder_ctl(st->st_low, SPEEX_GET_DTX_STATUS, &dtx);
1239
1240
   /* If error decoding the narrowband part, propagate error */
1241
41.4k
   if (ret!=0)
1242
10.9k
   {
1243
10.9k
      return ret;
1244
10.9k
   }
1245
1246
30.5k
   if (!bits)
1247
0
   {
1248
0
      sb_decode_lost(st, out, dtx, stack);
1249
0
      return 0;
1250
0
   }
1251
1252
30.5k
   if (st->encode_submode)
1253
30.5k
   {
1254
1255
      /*Check "wideband bit"*/
1256
30.5k
      if (speex_bits_remaining(bits)>0)
1257
16.3k
         wideband = speex_bits_peek(bits);
1258
14.1k
      else
1259
14.1k
         wideband = 0;
1260
30.5k
      if (wideband)
1261
5.03k
      {
1262
         /*Regular wideband frame, read the submode*/
1263
5.03k
         wideband = speex_bits_unpack_unsigned(bits, 1);
1264
5.03k
         st->submodeID = speex_bits_unpack_unsigned(bits, SB_SUBMODE_BITS);
1265
5.03k
      } else
1266
25.4k
      {
1267
         /*Was a narrowband frame, set "null submode"*/
1268
25.4k
         st->submodeID = 0;
1269
25.4k
      }
1270
30.5k
      if (st->submodeID != 0 && st->submodes[st->submodeID] == NULL)
1271
506
      {
1272
506
         speex_notify("Invalid mode encountered. The stream is corrupted.");
1273
506
         return -2;
1274
506
      }
1275
30.5k
   }
1276
1277
   /* If null mode (no transmission), just set a couple things to zero*/
1278
30.0k
   if (st->submodes[st->submodeID] == NULL)
1279
26.4k
   {
1280
26.4k
      if (st->innov_save)
1281
9.98k
        SPEEX_MEMSET(st->innov_save, 0, st->full_frame_size);
1282
1283
26.4k
      if (dtx)
1284
820
      {
1285
820
         sb_decode_lost(st, out, 1, stack);
1286
820
         return 0;
1287
820
      }
1288
1289
5.85M
      for (i=0;i<st->frame_size;i++)
1290
5.82M
         out[st->frame_size+i]=VERY_SMALL;
1291
1292
25.6k
      st->first=1;
1293
1294
      /* Final signal synthesis from excitation */
1295
25.6k
      iir_mem16(out+st->frame_size, st->interp_qlpc, out+st->frame_size, st->frame_size, st->lpcSize, st->mem_sp, stack);
1296
1297
25.6k
      qmf_synth(out, out+st->frame_size, h0, out, st->full_frame_size, QMF_ORDER, st->g0_mem, st->g1_mem, stack);
1298
1299
25.6k
      return 0;
1300
1301
26.4k
   }
1302
1303
3.54k
   ALLOC(low_pi_gain, st->nbSubframes, spx_word32_t);
1304
3.54k
   ALLOC(low_exc_rms, st->nbSubframes, spx_word16_t);
1305
3.54k
   speex_decoder_ctl(st->st_low, SPEEX_GET_PI_GAIN, low_pi_gain);
1306
3.54k
   speex_decoder_ctl(st->st_low, SPEEX_GET_EXC, low_exc_rms);
1307
1308
3.54k
   ALLOC(qlsp, st->lpcSize, spx_lsp_t);
1309
3.54k
   ALLOC(interp_qlsp, st->lpcSize, spx_lsp_t);
1310
3.54k
   SUBMODE(lsp_unquant)(qlsp, st->lpcSize, bits);
1311
1312
3.54k
   if (st->first)
1313
1.67k
   {
1314
15.1k
      for (i=0;i<st->lpcSize;i++)
1315
13.4k
         st->old_qlsp[i] = qlsp[i];
1316
1.67k
   }
1317
1318
3.54k
   ALLOC(ak, st->lpcSize, spx_coef_t);
1319
1320
17.7k
   for (sub=0;sub<st->nbSubframes;sub++)
1321
14.1k
   {
1322
14.1k
      VARDECL(spx_word32_t *exc);
1323
14.1k
      spx_word16_t *innov_save=NULL;
1324
14.1k
      spx_word16_t *sp;
1325
14.1k
      spx_word16_t filter_ratio;
1326
14.1k
      spx_word16_t el=0;
1327
14.1k
      int offset;
1328
14.1k
      spx_word32_t rl=0,rh=0;
1329
1330
14.1k
      offset = st->subframeSize*sub;
1331
14.1k
      sp=out+st->frame_size+offset;
1332
14.1k
      ALLOC(exc, st->subframeSize, spx_word32_t);
1333
      /* Pointer for saving innovation */
1334
14.1k
      if (st->innov_save)
1335
7.97k
      {
1336
7.97k
         innov_save = st->innov_save+2*offset;
1337
7.97k
         SPEEX_MEMSET(innov_save, 0, 2*st->subframeSize);
1338
7.97k
      }
1339
1340
      /* LSP interpolation */
1341
14.1k
      lsp_interpolate(st->old_qlsp, qlsp, interp_qlsp, st->lpcSize, sub, st->nbSubframes, LSP_MARGIN);
1342
1343
      /* LSP to LPC */
1344
14.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
14.1k
         st->pi_gain[sub]=LPC_SCALING;
1350
14.1k
         rh = LPC_SCALING;
1351
70.8k
         for (i=0;i<st->lpcSize;i+=2)
1352
56.7k
         {
1353
56.7k
            rh += ak[i+1] - ak[i];
1354
56.7k
            st->pi_gain[sub] += ak[i] + ak[i+1];
1355
56.7k
         }
1356
1357
14.1k
         rl = low_pi_gain[sub];
1358
14.1k
#ifdef FIXED_POINT
1359
14.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
14.1k
      SPEEX_MEMSET(exc, 0, st->subframeSize);
1365
14.1k
      if (!SUBMODE(innovation_unquant))
1366
4.75k
      {
1367
4.75k
         spx_word32_t g;
1368
4.75k
         int quant;
1369
1370
4.75k
         quant = speex_bits_unpack_unsigned(bits, 5);
1371
4.75k
         g= spx_exp(MULT16_16(QCONST16(.125f,11),(quant-10)));
1372
1373
         /* Clamp to a minimum of epsilon to avoid division by 0 */
1374
4.75k
         g = PDIV32(g, MAX16(EPSILON,filter_ratio));
1375
1376
156k
         for (i=0;i<st->subframeSize;i+=2)
1377
151k
         {
1378
151k
            exc[i]=SHL32(MULT16_32_P15(MULT16_16_Q15(mode->folding_gain,low_innov_alias[offset+i]),SHL32(g,6)),SIG_SHIFT);
1379
151k
            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
151k
         }
1381
1382
9.42k
      } else {
1383
9.42k
         spx_word16_t gc;
1384
9.42k
         spx_word32_t scale;
1385
9.42k
         int qgc = speex_bits_unpack_unsigned(bits, 4);
1386
1387
9.42k
         el = low_exc_rms[sub];
1388
9.42k
         gc = MULT16_16_Q15(QCONST16(0.87360,15),gc_quant_bound[qgc]);
1389
1390
9.42k
         if (st->subframeSize==80)
1391
0
            gc = MULT16_16_P14(QCONST16(1.4142f,14),gc);
1392
1393
9.42k
         scale = SHL32(PDIV32(SHL32(MULT16_16(gc, el),3), MAX16(EPSILON,filter_ratio)),SIG_SHIFT-3);
1394
9.42k
         SUBMODE(innovation_unquant)(exc, SUBMODE(innovation_params), st->subframeSize,
1395
9.42k
                                     bits, stack, &st->seed);
1396
1397
9.42k
         signal_mul(exc,exc,scale,st->subframeSize);
1398
1399
9.42k
         if (SUBMODE(double_codebook)) {
1400
2.61k
            char *tmp_stack=stack;
1401
2.61k
            VARDECL(spx_sig_t *innov2);
1402
2.61k
            ALLOC(innov2, st->subframeSize, spx_sig_t);
1403
2.61k
            SPEEX_MEMSET(innov2, 0, st->subframeSize);
1404
2.61k
            SUBMODE(innovation_unquant)(innov2, SUBMODE(innovation_params), st->subframeSize,
1405
2.61k
                                        bits, stack, &st->seed);
1406
2.61k
            signal_mul(innov2, innov2, MULT16_32_P15(QCONST16(0.4f,15),scale), st->subframeSize);
1407
107k
            for (i=0;i<st->subframeSize;i++)
1408
104k
               exc[i] = ADD32(exc[i],innov2[i]);
1409
2.61k
            stack = tmp_stack;
1410
2.61k
         }
1411
1412
9.42k
      }
1413
1414
14.1k
      if (st->innov_save)
1415
7.97k
      {
1416
326k
         for (i=0;i<st->subframeSize;i++)
1417
318k
            innov_save[2*i]=EXTRACT16(PSHR32(exc[i],SIG_SHIFT));
1418
7.97k
      }
1419
1420
14.1k
      iir_mem16(st->excBuf, st->interp_qlpc, sp, st->subframeSize, st->lpcSize,
1421
14.1k
               st->mem_sp, stack);
1422
694k
      for (i=0;i<st->subframeSize;i++)
1423
680k
         st->excBuf[i]=EXTRACT16(PSHR32(exc[i],SIG_SHIFT));
1424
127k
      for (i=0;i<st->lpcSize;i++)
1425
113k
         st->interp_qlpc[i] = ak[i];
1426
14.1k
      st->exc_rms[sub] = compute_rms16(st->excBuf, st->subframeSize);
1427
14.1k
      exc_ener_sum = ADD32(exc_ener_sum, DIV32(MULT16_16(st->exc_rms[sub],st->exc_rms[sub]), st->nbSubframes));
1428
14.1k
   }
1429
3.54k
   st->last_ener = spx_sqrt(exc_ener_sum);
1430
1431
3.54k
   qmf_synth(out, out+st->frame_size, h0, out, st->full_frame_size, QMF_ORDER, st->g0_mem, st->g1_mem, stack);
1432
31.8k
   for (i=0;i<st->lpcSize;i++)
1433
28.3k
      st->old_qlsp[i] = qlsp[i];
1434
1435
3.54k
   st->first=0;
1436
1437
3.54k
   return 0;
1438
30.0k
}
1439
#endif /* DISABLE_DECODER */
1440
1441
1442
1443
#endif
1444