Coverage Report

Created: 2026-05-30 06:04

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/speex/libspeex/sb_celp.c
Line
Count
Source
1
/* Copyright (C) 2002-2006 Jean-Marc Valin
2
   File: sb_celp.c
3
4
   Redistribution and use in source and binary forms, with or without
5
   modification, are permitted provided that the following conditions
6
   are met:
7
8
   - Redistributions of source code must retain the above copyright
9
   notice, this list of conditions and the following disclaimer.
10
11
   - Redistributions in binary form must reproduce the above copyright
12
   notice, this list of conditions and the following disclaimer in the
13
   documentation and/or other materials provided with the distribution.
14
15
   - Neither the name of the Xiph.org Foundation nor the names of its
16
   contributors may be used to endorse or promote products derived from
17
   this software without specific prior written permission.
18
19
   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20
   ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21
   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22
   A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
23
   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24
   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25
   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26
   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27
   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28
   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29
   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
*/
31
32
#ifdef HAVE_CONFIG_H
33
#include "config.h"
34
#endif
35
36
#include <math.h>
37
#include "sb_celp.h"
38
#include "filters.h"
39
#include "lpc.h"
40
#include "lsp.h"
41
#include "stack_alloc.h"
42
#include "cb_search.h"
43
#include "quant_lsp.h"
44
#include "vq.h"
45
#include "ltp.h"
46
#include "arch.h"
47
#include "math_approx.h"
48
#include "os_support.h"
49
50
#ifndef NULL
51
#define NULL 0
52
#endif
53
54
/* Default size for the encoder and decoder stack (can be changed at compile time).
55
   This does not apply when using variable-size arrays or alloca. */
56
#ifndef SB_ENC_STACK
57
#define SB_ENC_STACK (10000*sizeof(spx_sig_t))
58
#endif
59
60
#ifndef SB_DEC_STACK
61
#define SB_DEC_STACK (6000*sizeof(spx_sig_t))
62
#endif
63
64
65
#ifndef DISABLE_WIDEBAND
66
67
237k
#define SUBMODE(x) st->submodes[st->submodeID]->x
68
69
#ifdef FIXED_POINT
70
static const spx_word16_t gc_quant_bound[16] = {125, 164, 215, 282, 370, 484, 635, 832, 1090, 1428, 1871, 2452, 3213, 4210, 5516, 7228};
71
static const spx_word16_t fold_quant_bound[32] = {
72
   39, 44, 50, 57, 64, 73, 83, 94,
73
   106, 120, 136, 154, 175, 198, 225, 255,
74
   288, 327, 370, 420, 476, 539, 611, 692,
75
   784, 889, 1007, 1141, 1293, 1465, 1660, 1881};
76
62.0k
#define LSP_MARGIN 410
77
6.68k
#define LSP_DELTA1 6553
78
141
#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.8k
#define LSP_MARGIN .05
92
3.66k
#define LSP_DELTA1 .2
93
20
#define LSP_DELTA2 .05
94
95
#endif
96
97
88.4k
#define QMF_ORDER 64
98
99
#ifdef FIXED_POINT
100
static const spx_word16_t h0[64] = {2, -7, -7, 18, 15, -39, -25, 75, 35, -130, -41, 212, 38, -327, -17, 483, -32, -689, 124, 956, -283, -1307, 543, 1780, -973, -2467, 1733, 3633, -3339, -6409, 9059, 30153, 30153, 9059, -6409, -3339, 3633, 1733, -2467, -973, 1780, 543, -1307, -283, 956, 124, -689, -32, 483, -17, -327, 38, 212, -41, -130, 35, 75, -25, -39, 15, 18, -7, -7, 2};
101
102
#else
103
static const float h0[64] = {
104
   3.596189e-05f, -0.0001123515f,
105
   -0.0001104587f, 0.0002790277f,
106
   0.0002298438f, -0.0005953563f,
107
   -0.0003823631f, 0.00113826f,
108
   0.0005308539f, -0.001986177f,
109
   -0.0006243724f, 0.003235877f,
110
   0.0005743159f, -0.004989147f,
111
   -0.0002584767f, 0.007367171f,
112
   -0.0004857935f, -0.01050689f,
113
   0.001894714f, 0.01459396f,
114
   -0.004313674f, -0.01994365f,
115
   0.00828756f, 0.02716055f,
116
   -0.01485397f, -0.03764973f,
117
   0.026447f, 0.05543245f,
118
   -0.05095487f, -0.09779096f,
119
   0.1382363f, 0.4600981f,
120
   0.4600981f, 0.1382363f,
121
   -0.09779096f, -0.05095487f,
122
   0.05543245f, 0.026447f,
123
   -0.03764973f, -0.01485397f,
124
   0.02716055f, 0.00828756f,
125
   -0.01994365f, -0.004313674f,
126
   0.01459396f, 0.001894714f,
127
   -0.01050689f, -0.0004857935f,
128
   0.007367171f, -0.0002584767f,
129
   -0.004989147f, 0.0005743159f,
130
   0.003235877f, -0.0006243724f,
131
   -0.001986177f, 0.0005308539f,
132
   0.00113826f, -0.0003823631f,
133
   -0.0005953563f, 0.0002298438f,
134
   0.0002790277f, -0.0001104587f,
135
   -0.0001123515f, 3.596189e-05f
136
};
137
138
#endif
139
140
extern const spx_word16_t lag_window[];
141
extern const spx_word16_t lpc_window[];
142
143
#ifndef DISABLE_ENCODER
144
void *sb_encoder_init(const SpeexMode *m)
145
5.48k
{
146
5.48k
   int i;
147
5.48k
   spx_int32_t tmp;
148
5.48k
   SBEncState *st;
149
5.48k
   const SpeexSBMode *mode;
150
151
5.48k
   st = (SBEncState*)speex_alloc(sizeof(SBEncState));
152
5.48k
   if (!st)
153
0
      return NULL;
154
5.48k
   st->mode = m;
155
5.48k
   mode = (const SpeexSBMode*)m->mode;
156
157
158
5.48k
   st->st_low = speex_encoder_init(mode->nb_mode);
159
5.48k
#if defined(VAR_ARRAYS) || defined (USE_ALLOCA)
160
5.48k
   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.48k
   st->full_frame_size = 2*mode->frameSize;
167
5.48k
   st->frame_size = mode->frameSize;
168
5.48k
   st->subframeSize = mode->subframeSize;
169
5.48k
   st->nbSubframes = mode->frameSize/mode->subframeSize;
170
5.48k
   st->windowSize = st->frame_size+st->subframeSize;
171
5.48k
   st->lpcSize=mode->lpcSize;
172
173
5.48k
   st->encode_submode = 1;
174
5.48k
   st->submodes=mode->submodes;
175
5.48k
   st->submodeSelect = st->submodeID=mode->defaultSubmode;
176
177
5.48k
   tmp=9;
178
5.48k
   speex_encoder_ctl(st->st_low, SPEEX_SET_QUALITY, &tmp);
179
5.48k
   tmp=1;
180
5.48k
   speex_encoder_ctl(st->st_low, SPEEX_SET_WIDEBAND, &tmp);
181
182
5.48k
   st->lpc_floor = mode->lpc_floor;
183
5.48k
   st->gamma1=mode->gamma1;
184
5.48k
   st->gamma2=mode->gamma2;
185
5.48k
   st->first=1;
186
187
5.48k
   st->high=(spx_word16_t*)speex_alloc((st->windowSize-st->frame_size)*sizeof(spx_word16_t));
188
189
5.48k
   st->h0_mem=(spx_word16_t*)speex_alloc((QMF_ORDER)*sizeof(spx_word16_t));
190
191
5.48k
   st->window= lpc_window;
192
193
5.48k
   st->lagWindow = lag_window;
194
195
5.48k
   st->old_lsp = (spx_lsp_t*)speex_alloc(st->lpcSize*sizeof(spx_lsp_t));
196
5.48k
   st->old_qlsp = (spx_lsp_t*)speex_alloc(st->lpcSize*sizeof(spx_lsp_t));
197
5.48k
   st->interp_qlpc = (spx_coef_t*)speex_alloc(st->lpcSize*sizeof(spx_coef_t));
198
5.48k
   st->pi_gain = (spx_word32_t*)speex_alloc((st->nbSubframes)*sizeof(spx_word32_t));
199
5.48k
   st->exc_rms = (spx_word16_t*)speex_alloc((st->nbSubframes)*sizeof(spx_word16_t));
200
5.48k
   st->innov_rms_save = NULL;
201
202
5.48k
   st->mem_sp = (spx_mem_t*)speex_alloc((st->lpcSize)*sizeof(spx_mem_t));
203
5.48k
   st->mem_sp2 = (spx_mem_t*)speex_alloc((st->lpcSize)*sizeof(spx_mem_t));
204
5.48k
   st->mem_sw = (spx_mem_t*)speex_alloc((st->lpcSize)*sizeof(spx_mem_t));
205
206
49.3k
   for (i=0;i<st->lpcSize;i++)
207
43.8k
      st->old_lsp[i]= DIV32(MULT16_16(QCONST16(3.1415927f, LSP_SHIFT), i+1), st->lpcSize+1);
208
209
5.48k
#ifndef DISABLE_VBR
210
5.48k
   st->vbr_quality = 8;
211
5.48k
   st->vbr_enabled = 0;
212
5.48k
   st->vbr_max = 0;
213
5.48k
   st->vbr_max_high = 20000;  /* We just need a big value here */
214
5.48k
   st->vad_enabled = 0;
215
5.48k
   st->abr_enabled = 0;
216
5.48k
   st->relative_quality=0;
217
5.48k
#endif /* #ifndef DISABLE_VBR */
218
219
5.48k
   st->complexity=2;
220
5.48k
   speex_encoder_ctl(st->st_low, SPEEX_GET_SAMPLING_RATE, &st->sampling_rate);
221
5.48k
   st->sampling_rate*=2;
222
#ifdef ENABLE_VALGRIND
223
   VALGRIND_MAKE_MEM_DEFINED(st, (st->stack-(char*)st));
224
#endif
225
5.48k
   return st;
226
5.48k
}
sb_encoder_init
Line
Count
Source
145
2.74k
{
146
2.74k
   int i;
147
2.74k
   spx_int32_t tmp;
148
2.74k
   SBEncState *st;
149
2.74k
   const SpeexSBMode *mode;
150
151
2.74k
   st = (SBEncState*)speex_alloc(sizeof(SBEncState));
152
2.74k
   if (!st)
153
0
      return NULL;
154
2.74k
   st->mode = m;
155
2.74k
   mode = (const SpeexSBMode*)m->mode;
156
157
158
2.74k
   st->st_low = speex_encoder_init(mode->nb_mode);
159
2.74k
#if defined(VAR_ARRAYS) || defined (USE_ALLOCA)
160
2.74k
   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.74k
   st->full_frame_size = 2*mode->frameSize;
167
2.74k
   st->frame_size = mode->frameSize;
168
2.74k
   st->subframeSize = mode->subframeSize;
169
2.74k
   st->nbSubframes = mode->frameSize/mode->subframeSize;
170
2.74k
   st->windowSize = st->frame_size+st->subframeSize;
171
2.74k
   st->lpcSize=mode->lpcSize;
172
173
2.74k
   st->encode_submode = 1;
174
2.74k
   st->submodes=mode->submodes;
175
2.74k
   st->submodeSelect = st->submodeID=mode->defaultSubmode;
176
177
2.74k
   tmp=9;
178
2.74k
   speex_encoder_ctl(st->st_low, SPEEX_SET_QUALITY, &tmp);
179
2.74k
   tmp=1;
180
2.74k
   speex_encoder_ctl(st->st_low, SPEEX_SET_WIDEBAND, &tmp);
181
182
2.74k
   st->lpc_floor = mode->lpc_floor;
183
2.74k
   st->gamma1=mode->gamma1;
184
2.74k
   st->gamma2=mode->gamma2;
185
2.74k
   st->first=1;
186
187
2.74k
   st->high=(spx_word16_t*)speex_alloc((st->windowSize-st->frame_size)*sizeof(spx_word16_t));
188
189
2.74k
   st->h0_mem=(spx_word16_t*)speex_alloc((QMF_ORDER)*sizeof(spx_word16_t));
190
191
2.74k
   st->window= lpc_window;
192
193
2.74k
   st->lagWindow = lag_window;
194
195
2.74k
   st->old_lsp = (spx_lsp_t*)speex_alloc(st->lpcSize*sizeof(spx_lsp_t));
196
2.74k
   st->old_qlsp = (spx_lsp_t*)speex_alloc(st->lpcSize*sizeof(spx_lsp_t));
197
2.74k
   st->interp_qlpc = (spx_coef_t*)speex_alloc(st->lpcSize*sizeof(spx_coef_t));
198
2.74k
   st->pi_gain = (spx_word32_t*)speex_alloc((st->nbSubframes)*sizeof(spx_word32_t));
199
2.74k
   st->exc_rms = (spx_word16_t*)speex_alloc((st->nbSubframes)*sizeof(spx_word16_t));
200
2.74k
   st->innov_rms_save = NULL;
201
202
2.74k
   st->mem_sp = (spx_mem_t*)speex_alloc((st->lpcSize)*sizeof(spx_mem_t));
203
2.74k
   st->mem_sp2 = (spx_mem_t*)speex_alloc((st->lpcSize)*sizeof(spx_mem_t));
204
2.74k
   st->mem_sw = (spx_mem_t*)speex_alloc((st->lpcSize)*sizeof(spx_mem_t));
205
206
24.6k
   for (i=0;i<st->lpcSize;i++)
207
21.9k
      st->old_lsp[i]= DIV32(MULT16_16(QCONST16(3.1415927f, LSP_SHIFT), i+1), st->lpcSize+1);
208
209
2.74k
#ifndef DISABLE_VBR
210
2.74k
   st->vbr_quality = 8;
211
2.74k
   st->vbr_enabled = 0;
212
2.74k
   st->vbr_max = 0;
213
2.74k
   st->vbr_max_high = 20000;  /* We just need a big value here */
214
2.74k
   st->vad_enabled = 0;
215
2.74k
   st->abr_enabled = 0;
216
2.74k
   st->relative_quality=0;
217
2.74k
#endif /* #ifndef DISABLE_VBR */
218
219
2.74k
   st->complexity=2;
220
2.74k
   speex_encoder_ctl(st->st_low, SPEEX_GET_SAMPLING_RATE, &st->sampling_rate);
221
2.74k
   st->sampling_rate*=2;
222
#ifdef ENABLE_VALGRIND
223
   VALGRIND_MAKE_MEM_DEFINED(st, (st->stack-(char*)st));
224
#endif
225
2.74k
   return st;
226
2.74k
}
sb_encoder_init
Line
Count
Source
145
2.74k
{
146
2.74k
   int i;
147
2.74k
   spx_int32_t tmp;
148
2.74k
   SBEncState *st;
149
2.74k
   const SpeexSBMode *mode;
150
151
2.74k
   st = (SBEncState*)speex_alloc(sizeof(SBEncState));
152
2.74k
   if (!st)
153
0
      return NULL;
154
2.74k
   st->mode = m;
155
2.74k
   mode = (const SpeexSBMode*)m->mode;
156
157
158
2.74k
   st->st_low = speex_encoder_init(mode->nb_mode);
159
2.74k
#if defined(VAR_ARRAYS) || defined (USE_ALLOCA)
160
2.74k
   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.74k
   st->full_frame_size = 2*mode->frameSize;
167
2.74k
   st->frame_size = mode->frameSize;
168
2.74k
   st->subframeSize = mode->subframeSize;
169
2.74k
   st->nbSubframes = mode->frameSize/mode->subframeSize;
170
2.74k
   st->windowSize = st->frame_size+st->subframeSize;
171
2.74k
   st->lpcSize=mode->lpcSize;
172
173
2.74k
   st->encode_submode = 1;
174
2.74k
   st->submodes=mode->submodes;
175
2.74k
   st->submodeSelect = st->submodeID=mode->defaultSubmode;
176
177
2.74k
   tmp=9;
178
2.74k
   speex_encoder_ctl(st->st_low, SPEEX_SET_QUALITY, &tmp);
179
2.74k
   tmp=1;
180
2.74k
   speex_encoder_ctl(st->st_low, SPEEX_SET_WIDEBAND, &tmp);
181
182
2.74k
   st->lpc_floor = mode->lpc_floor;
183
2.74k
   st->gamma1=mode->gamma1;
184
2.74k
   st->gamma2=mode->gamma2;
185
2.74k
   st->first=1;
186
187
2.74k
   st->high=(spx_word16_t*)speex_alloc((st->windowSize-st->frame_size)*sizeof(spx_word16_t));
188
189
2.74k
   st->h0_mem=(spx_word16_t*)speex_alloc((QMF_ORDER)*sizeof(spx_word16_t));
190
191
2.74k
   st->window= lpc_window;
192
193
2.74k
   st->lagWindow = lag_window;
194
195
2.74k
   st->old_lsp = (spx_lsp_t*)speex_alloc(st->lpcSize*sizeof(spx_lsp_t));
196
2.74k
   st->old_qlsp = (spx_lsp_t*)speex_alloc(st->lpcSize*sizeof(spx_lsp_t));
197
2.74k
   st->interp_qlpc = (spx_coef_t*)speex_alloc(st->lpcSize*sizeof(spx_coef_t));
198
2.74k
   st->pi_gain = (spx_word32_t*)speex_alloc((st->nbSubframes)*sizeof(spx_word32_t));
199
2.74k
   st->exc_rms = (spx_word16_t*)speex_alloc((st->nbSubframes)*sizeof(spx_word16_t));
200
2.74k
   st->innov_rms_save = NULL;
201
202
2.74k
   st->mem_sp = (spx_mem_t*)speex_alloc((st->lpcSize)*sizeof(spx_mem_t));
203
2.74k
   st->mem_sp2 = (spx_mem_t*)speex_alloc((st->lpcSize)*sizeof(spx_mem_t));
204
2.74k
   st->mem_sw = (spx_mem_t*)speex_alloc((st->lpcSize)*sizeof(spx_mem_t));
205
206
24.6k
   for (i=0;i<st->lpcSize;i++)
207
21.9k
      st->old_lsp[i]= DIV32(MULT16_16(QCONST16(3.1415927f, LSP_SHIFT), i+1), st->lpcSize+1);
208
209
2.74k
#ifndef DISABLE_VBR
210
2.74k
   st->vbr_quality = 8;
211
2.74k
   st->vbr_enabled = 0;
212
2.74k
   st->vbr_max = 0;
213
2.74k
   st->vbr_max_high = 20000;  /* We just need a big value here */
214
2.74k
   st->vad_enabled = 0;
215
2.74k
   st->abr_enabled = 0;
216
2.74k
   st->relative_quality=0;
217
2.74k
#endif /* #ifndef DISABLE_VBR */
218
219
2.74k
   st->complexity=2;
220
2.74k
   speex_encoder_ctl(st->st_low, SPEEX_GET_SAMPLING_RATE, &st->sampling_rate);
221
2.74k
   st->sampling_rate*=2;
222
#ifdef ENABLE_VALGRIND
223
   VALGRIND_MAKE_MEM_DEFINED(st, (st->stack-(char*)st));
224
#endif
225
2.74k
   return st;
226
2.74k
}
227
228
void sb_encoder_destroy(void *state)
229
2.74k
{
230
2.74k
   SBEncState *st=(SBEncState*)state;
231
232
2.74k
   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.74k
   speex_free(st->high);
238
239
2.74k
   speex_free(st->h0_mem);
240
241
2.74k
   speex_free(st->old_lsp);
242
2.74k
   speex_free(st->old_qlsp);
243
2.74k
   speex_free(st->interp_qlpc);
244
2.74k
   speex_free(st->pi_gain);
245
2.74k
   speex_free(st->exc_rms);
246
247
2.74k
   speex_free(st->mem_sp);
248
2.74k
   speex_free(st->mem_sp2);
249
2.74k
   speex_free(st->mem_sw);
250
251
252
2.74k
   speex_free(st);
253
2.74k
}
254
255
int sb_encoder_ctl(void *state, int request, void *ptr)
256
221k
{
257
221k
   SBEncState *st;
258
221k
   st=(SBEncState*)state;
259
221k
   switch(request)
260
221k
   {
261
9.20k
   case SPEEX_GET_FRAME_SIZE:
262
9.20k
      (*(spx_int32_t*)ptr) = st->full_frame_size;
263
9.20k
      break;
264
10.8k
   case SPEEX_SET_HIGH_MODE:
265
10.8k
      st->submodeSelect = st->submodeID = (*(spx_int32_t*)ptr);
266
10.8k
      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.97k
   case SPEEX_SET_DTX:
271
1.97k
      speex_encoder_ctl(st->st_low, SPEEX_SET_DTX, ptr);
272
1.97k
      break;
273
0
   case SPEEX_GET_DTX:
274
0
      speex_encoder_ctl(st->st_low, SPEEX_GET_DTX, ptr);
275
0
      break;
276
5.02k
   case SPEEX_GET_LOW_MODE:
277
5.02k
      speex_encoder_ctl(st->st_low, SPEEX_GET_LOW_MODE, ptr);
278
5.02k
      break;
279
15.0k
   case SPEEX_SET_MODE:
280
15.0k
      speex_encoder_ctl(st, SPEEX_SET_QUALITY, ptr);
281
15.0k
      break;
282
0
#ifndef DISABLE_VBR
283
2.47k
   case SPEEX_SET_VBR:
284
2.47k
      st->vbr_enabled = (*(spx_int32_t*)ptr);
285
2.47k
      speex_encoder_ctl(st->st_low, SPEEX_SET_VBR, ptr);
286
2.47k
      break;
287
0
   case SPEEX_GET_VBR:
288
0
      (*(spx_int32_t*)ptr) = st->vbr_enabled;
289
0
      break;
290
2.17k
   case SPEEX_SET_VAD:
291
2.17k
      st->vad_enabled = (*(spx_int32_t*)ptr);
292
2.17k
      speex_encoder_ctl(st->st_low, SPEEX_SET_VAD, ptr);
293
2.17k
      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.72k
   case SPEEX_SET_VBR_QUALITY:
300
3.72k
      {
301
3.72k
         spx_int32_t q;
302
3.72k
         float qual = (*(float*)ptr)+.6;
303
3.72k
         st->vbr_quality = (*(float*)ptr);
304
3.72k
         if (qual>10)
305
286
            qual=10;
306
3.72k
         q=(int)floor(.5+*(float*)ptr);
307
3.72k
         if (q>10)
308
0
            q=10;
309
3.72k
         speex_encoder_ctl(st->st_low, SPEEX_SET_VBR_QUALITY, &qual);
310
3.72k
         speex_encoder_ctl(state, SPEEX_SET_QUALITY, &q);
311
3.72k
         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.25k
   case SPEEX_SET_ABR:
319
1.25k
      st->abr_enabled = (*(spx_int32_t*)ptr);
320
1.25k
      st->vbr_enabled = st->abr_enabled!=0;
321
1.25k
      speex_encoder_ctl(st->st_low, SPEEX_SET_VBR, &st->vbr_enabled);
322
1.25k
      if (st->vbr_enabled)
323
1.25k
      {
324
1.25k
         spx_int32_t i=10, rate, target;
325
1.25k
         float vbr_qual;
326
1.25k
         target = (*(spx_int32_t*)ptr);
327
15.0k
         while (i>=0)
328
13.7k
         {
329
13.7k
            speex_encoder_ctl(st, SPEEX_SET_QUALITY, &i);
330
13.7k
            speex_encoder_ctl(st, SPEEX_GET_BITRATE, &rate);
331
13.7k
            if (rate <= target)
332
0
               break;
333
13.7k
            i--;
334
13.7k
         }
335
1.25k
         vbr_qual=i;
336
1.25k
         if (vbr_qual<0)
337
1.25k
            vbr_qual=0;
338
1.25k
         speex_encoder_ctl(st, SPEEX_SET_VBR_QUALITY, &vbr_qual);
339
1.25k
         st->abr_count=0;
340
1.25k
         st->abr_drift=0;
341
1.25k
         st->abr_drift2=0;
342
1.25k
      }
343
344
1.25k
      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
61.9k
   case SPEEX_SET_QUALITY:
351
61.9k
      {
352
61.9k
         spx_int32_t nb_qual;
353
61.9k
         int quality = (*(spx_int32_t*)ptr);
354
61.9k
         if (quality < 0)
355
0
            quality = 0;
356
61.9k
         if (quality > 10)
357
0
            quality = 10;
358
61.9k
         st->submodeSelect = st->submodeID = ((const SpeexSBMode*)(st->mode->mode))->quality_map[quality];
359
61.9k
         nb_qual = ((const SpeexSBMode*)(st->mode->mode))->low_quality_map[quality];
360
61.9k
         speex_encoder_ctl(st->st_low, SPEEX_SET_MODE, &nb_qual);
361
61.9k
      }
362
61.9k
      break;
363
5.48k
   case SPEEX_SET_COMPLEXITY:
364
5.48k
      speex_encoder_ctl(st->st_low, SPEEX_SET_COMPLEXITY, ptr);
365
5.48k
      st->complexity = (*(spx_int32_t*)ptr);
366
5.48k
      if (st->complexity<1)
367
980
         st->complexity=1;
368
5.48k
      break;
369
0
   case SPEEX_GET_COMPLEXITY:
370
0
      (*(spx_int32_t*)ptr) = st->complexity;
371
0
      break;
372
3.80k
   case SPEEX_SET_BITRATE:
373
3.80k
      {
374
3.80k
         spx_int32_t i=10;
375
3.80k
         spx_int32_t rate, target;
376
3.80k
         target = (*(spx_int32_t*)ptr);
377
26.1k
         while (i>=0)
378
25.2k
         {
379
25.2k
            speex_encoder_ctl(st, SPEEX_SET_QUALITY, &i);
380
25.2k
            speex_encoder_ctl(st, SPEEX_GET_BITRATE, &rate);
381
25.2k
            if (rate <= target)
382
2.89k
               break;
383
22.3k
            i--;
384
22.3k
         }
385
3.80k
      }
386
3.80k
      break;
387
58.0k
   case SPEEX_GET_BITRATE:
388
58.0k
      speex_encoder_ctl(st->st_low, request, ptr);
389
      /*fprintf (stderr, "before: %d\n", (*(int*)ptr));*/
390
58.0k
      if (st->submodes[st->submodeID])
391
57.0k
         (*(spx_int32_t*)ptr) += st->sampling_rate*SUBMODE(bits_per_frame)/st->full_frame_size;
392
1.02k
      else
393
1.02k
         (*(spx_int32_t*)ptr) += st->sampling_rate*(SB_SUBMODE_BITS+1)/st->full_frame_size;
394
      /*fprintf (stderr, "after: %d\n", (*(int*)ptr));*/
395
58.0k
      break;
396
5.48k
   case SPEEX_SET_SAMPLING_RATE:
397
5.48k
      {
398
5.48k
         spx_int32_t tmp=(*(spx_int32_t*)ptr);
399
5.48k
         st->sampling_rate = tmp;
400
5.48k
         tmp>>=1;
401
5.48k
         speex_encoder_ctl(st->st_low, SPEEX_SET_SAMPLING_RATE, &tmp);
402
5.48k
      }
403
5.48k
      break;
404
1.67k
   case SPEEX_GET_SAMPLING_RATE:
405
1.67k
      (*(spx_int32_t*)ptr)=st->sampling_rate;
406
1.67k
      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.48k
   case SPEEX_GET_LOOKAHEAD:
427
5.48k
      speex_encoder_ctl(st->st_low, SPEEX_GET_LOOKAHEAD, ptr);
428
5.48k
      (*(spx_int32_t*)ptr) = 2*(*(spx_int32_t*)ptr) + QMF_ORDER - 1;
429
5.48k
      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.12k
   case SPEEX_SET_VBR_MAX_BITRATE:
438
2.12k
      {
439
2.12k
         st->vbr_max = (*(spx_int32_t*)ptr);
440
2.12k
         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.12k
         } else {
445
2.12k
            spx_int32_t low_rate;
446
2.12k
            if (st->vbr_max >= 42200)
447
0
            {
448
0
               st->vbr_max_high = 17600;
449
2.12k
            } else if (st->vbr_max >= 27800)
450
0
            {
451
0
               st->vbr_max_high = 9600;
452
2.12k
            } else if (st->vbr_max > 20600)
453
0
            {
454
0
               st->vbr_max_high = 5600;
455
2.12k
            } else {
456
2.12k
               st->vbr_max_high = 1800;
457
2.12k
            }
458
2.12k
            if (st->subframeSize==80)
459
706
               st->vbr_max_high = 1800;
460
2.12k
            low_rate = st->vbr_max - st->vbr_max_high;
461
2.12k
            speex_encoder_ctl(st->st_low, SPEEX_SET_VBR_MAX_BITRATE, &low_rate);
462
2.12k
         }
463
2.12k
      }
464
2.12k
      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.48k
   case SPEEX_SET_HIGHPASS:
470
5.48k
      speex_encoder_ctl(st->st_low, SPEEX_SET_HIGHPASS, ptr);
471
5.48k
      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
5.02k
   case SPEEX_GET_PI_GAIN:
479
5.02k
      {
480
5.02k
         int i;
481
5.02k
         spx_word32_t *g = (spx_word32_t*)ptr;
482
25.1k
         for (i=0;i<st->nbSubframes;i++)
483
20.0k
            g[i]=st->pi_gain[i];
484
5.02k
      }
485
5.02k
      break;
486
5.02k
   case SPEEX_GET_EXC:
487
5.02k
      {
488
5.02k
         int i;
489
25.1k
         for (i=0;i<st->nbSubframes;i++)
490
20.0k
            ((spx_word16_t*)ptr)[i] = st->exc_rms[i];
491
5.02k
      }
492
5.02k
      break;
493
0
#ifndef DISABLE_VBR
494
3.18k
   case SPEEX_GET_RELATIVE_QUALITY:
495
3.18k
      (*(float*)ptr)=st->relative_quality;
496
3.18k
      break;
497
0
#endif /* #ifndef DISABLE_VBR */
498
5.02k
   case SPEEX_SET_INNOVATION_SAVE:
499
5.02k
      st->innov_rms_save = (spx_word16_t*)ptr;
500
5.02k
      break;
501
1.67k
   case SPEEX_SET_WIDEBAND:
502
1.67k
      speex_encoder_ctl(st->st_low, SPEEX_SET_WIDEBAND, ptr);
503
1.67k
      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
221k
   }
511
221k
   return 0;
512
221k
}
sb_encoder_ctl
Line
Count
Source
256
110k
{
257
110k
   SBEncState *st;
258
110k
   st=(SBEncState*)state;
259
110k
   switch(request)
260
110k
   {
261
4.60k
   case SPEEX_GET_FRAME_SIZE:
262
4.60k
      (*(spx_int32_t*)ptr) = st->full_frame_size;
263
4.60k
      break;
264
5.44k
   case SPEEX_SET_HIGH_MODE:
265
5.44k
      st->submodeSelect = st->submodeID = (*(spx_int32_t*)ptr);
266
5.44k
      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
986
   case SPEEX_SET_DTX:
271
986
      speex_encoder_ctl(st->st_low, SPEEX_SET_DTX, ptr);
272
986
      break;
273
0
   case SPEEX_GET_DTX:
274
0
      speex_encoder_ctl(st->st_low, SPEEX_GET_DTX, ptr);
275
0
      break;
276
2.51k
   case SPEEX_GET_LOW_MODE:
277
2.51k
      speex_encoder_ctl(st->st_low, SPEEX_GET_LOW_MODE, ptr);
278
2.51k
      break;
279
7.54k
   case SPEEX_SET_MODE:
280
7.54k
      speex_encoder_ctl(st, SPEEX_SET_QUALITY, ptr);
281
7.54k
      break;
282
0
#ifndef DISABLE_VBR
283
1.23k
   case SPEEX_SET_VBR:
284
1.23k
      st->vbr_enabled = (*(spx_int32_t*)ptr);
285
1.23k
      speex_encoder_ctl(st->st_low, SPEEX_SET_VBR, ptr);
286
1.23k
      break;
287
0
   case SPEEX_GET_VBR:
288
0
      (*(spx_int32_t*)ptr) = st->vbr_enabled;
289
0
      break;
290
1.08k
   case SPEEX_SET_VAD:
291
1.08k
      st->vad_enabled = (*(spx_int32_t*)ptr);
292
1.08k
      speex_encoder_ctl(st->st_low, SPEEX_SET_VAD, ptr);
293
1.08k
      break;
294
0
   case SPEEX_GET_VAD:
295
0
      (*(spx_int32_t*)ptr) = st->vad_enabled;
296
0
      break;
297
0
#endif /* #ifndef DISABLE_VBR */
298
0
#if !defined(DISABLE_VBR) && !defined(DISABLE_FLOAT_API)
299
1.86k
   case SPEEX_SET_VBR_QUALITY:
300
1.86k
      {
301
1.86k
         spx_int32_t q;
302
1.86k
         float qual = (*(float*)ptr)+.6;
303
1.86k
         st->vbr_quality = (*(float*)ptr);
304
1.86k
         if (qual>10)
305
143
            qual=10;
306
1.86k
         q=(int)floor(.5+*(float*)ptr);
307
1.86k
         if (q>10)
308
0
            q=10;
309
1.86k
         speex_encoder_ctl(st->st_low, SPEEX_SET_VBR_QUALITY, &qual);
310
1.86k
         speex_encoder_ctl(state, SPEEX_SET_QUALITY, &q);
311
1.86k
         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
627
   case SPEEX_SET_ABR:
319
627
      st->abr_enabled = (*(spx_int32_t*)ptr);
320
627
      st->vbr_enabled = st->abr_enabled!=0;
321
627
      speex_encoder_ctl(st->st_low, SPEEX_SET_VBR, &st->vbr_enabled);
322
627
      if (st->vbr_enabled)
323
627
      {
324
627
         spx_int32_t i=10, rate, target;
325
627
         float vbr_qual;
326
627
         target = (*(spx_int32_t*)ptr);
327
7.52k
         while (i>=0)
328
6.89k
         {
329
6.89k
            speex_encoder_ctl(st, SPEEX_SET_QUALITY, &i);
330
6.89k
            speex_encoder_ctl(st, SPEEX_GET_BITRATE, &rate);
331
6.89k
            if (rate <= target)
332
0
               break;
333
6.89k
            i--;
334
6.89k
         }
335
627
         vbr_qual=i;
336
627
         if (vbr_qual<0)
337
627
            vbr_qual=0;
338
627
         speex_encoder_ctl(st, SPEEX_SET_VBR_QUALITY, &vbr_qual);
339
627
         st->abr_count=0;
340
627
         st->abr_drift=0;
341
627
         st->abr_drift2=0;
342
627
      }
343
344
627
      break;
345
0
   case SPEEX_GET_ABR:
346
0
      (*(spx_int32_t*)ptr) = st->abr_enabled;
347
0
      break;
348
0
#endif /* #ifndef DISABLE_VBR */
349
350
30.9k
   case SPEEX_SET_QUALITY:
351
30.9k
      {
352
30.9k
         spx_int32_t nb_qual;
353
30.9k
         int quality = (*(spx_int32_t*)ptr);
354
30.9k
         if (quality < 0)
355
0
            quality = 0;
356
30.9k
         if (quality > 10)
357
0
            quality = 10;
358
30.9k
         st->submodeSelect = st->submodeID = ((const SpeexSBMode*)(st->mode->mode))->quality_map[quality];
359
30.9k
         nb_qual = ((const SpeexSBMode*)(st->mode->mode))->low_quality_map[quality];
360
30.9k
         speex_encoder_ctl(st->st_low, SPEEX_SET_MODE, &nb_qual);
361
30.9k
      }
362
30.9k
      break;
363
2.74k
   case SPEEX_SET_COMPLEXITY:
364
2.74k
      speex_encoder_ctl(st->st_low, SPEEX_SET_COMPLEXITY, ptr);
365
2.74k
      st->complexity = (*(spx_int32_t*)ptr);
366
2.74k
      if (st->complexity<1)
367
490
         st->complexity=1;
368
2.74k
      break;
369
0
   case SPEEX_GET_COMPLEXITY:
370
0
      (*(spx_int32_t*)ptr) = st->complexity;
371
0
      break;
372
1.90k
   case SPEEX_SET_BITRATE:
373
1.90k
      {
374
1.90k
         spx_int32_t i=10;
375
1.90k
         spx_int32_t rate, target;
376
1.90k
         target = (*(spx_int32_t*)ptr);
377
13.0k
         while (i>=0)
378
12.6k
         {
379
12.6k
            speex_encoder_ctl(st, SPEEX_SET_QUALITY, &i);
380
12.6k
            speex_encoder_ctl(st, SPEEX_GET_BITRATE, &rate);
381
12.6k
            if (rate <= target)
382
1.44k
               break;
383
11.1k
            i--;
384
11.1k
         }
385
1.90k
      }
386
1.90k
      break;
387
29.0k
   case SPEEX_GET_BITRATE:
388
29.0k
      speex_encoder_ctl(st->st_low, request, ptr);
389
      /*fprintf (stderr, "before: %d\n", (*(int*)ptr));*/
390
29.0k
      if (st->submodes[st->submodeID])
391
28.5k
         (*(spx_int32_t*)ptr) += st->sampling_rate*SUBMODE(bits_per_frame)/st->full_frame_size;
392
510
      else
393
510
         (*(spx_int32_t*)ptr) += st->sampling_rate*(SB_SUBMODE_BITS+1)/st->full_frame_size;
394
      /*fprintf (stderr, "after: %d\n", (*(int*)ptr));*/
395
29.0k
      break;
396
2.74k
   case SPEEX_SET_SAMPLING_RATE:
397
2.74k
      {
398
2.74k
         spx_int32_t tmp=(*(spx_int32_t*)ptr);
399
2.74k
         st->sampling_rate = tmp;
400
2.74k
         tmp>>=1;
401
2.74k
         speex_encoder_ctl(st->st_low, SPEEX_SET_SAMPLING_RATE, &tmp);
402
2.74k
      }
403
2.74k
      break;
404
837
   case SPEEX_GET_SAMPLING_RATE:
405
837
      (*(spx_int32_t*)ptr)=st->sampling_rate;
406
837
      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.74k
   case SPEEX_GET_LOOKAHEAD:
427
2.74k
      speex_encoder_ctl(st->st_low, SPEEX_GET_LOOKAHEAD, ptr);
428
2.74k
      (*(spx_int32_t*)ptr) = 2*(*(spx_int32_t*)ptr) + QMF_ORDER - 1;
429
2.74k
      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.06k
   case SPEEX_SET_VBR_MAX_BITRATE:
438
1.06k
      {
439
1.06k
         st->vbr_max = (*(spx_int32_t*)ptr);
440
1.06k
         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.06k
         } else {
445
1.06k
            spx_int32_t low_rate;
446
1.06k
            if (st->vbr_max >= 42200)
447
0
            {
448
0
               st->vbr_max_high = 17600;
449
1.06k
            } else if (st->vbr_max >= 27800)
450
0
            {
451
0
               st->vbr_max_high = 9600;
452
1.06k
            } else if (st->vbr_max > 20600)
453
0
            {
454
0
               st->vbr_max_high = 5600;
455
1.06k
            } else {
456
1.06k
               st->vbr_max_high = 1800;
457
1.06k
            }
458
1.06k
            if (st->subframeSize==80)
459
353
               st->vbr_max_high = 1800;
460
1.06k
            low_rate = st->vbr_max - st->vbr_max_high;
461
1.06k
            speex_encoder_ctl(st->st_low, SPEEX_SET_VBR_MAX_BITRATE, &low_rate);
462
1.06k
         }
463
1.06k
      }
464
1.06k
      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.74k
   case SPEEX_SET_HIGHPASS:
470
2.74k
      speex_encoder_ctl(st->st_low, SPEEX_SET_HIGHPASS, ptr);
471
2.74k
      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.51k
   case SPEEX_GET_PI_GAIN:
479
2.51k
      {
480
2.51k
         int i;
481
2.51k
         spx_word32_t *g = (spx_word32_t*)ptr;
482
12.5k
         for (i=0;i<st->nbSubframes;i++)
483
10.0k
            g[i]=st->pi_gain[i];
484
2.51k
      }
485
2.51k
      break;
486
2.51k
   case SPEEX_GET_EXC:
487
2.51k
      {
488
2.51k
         int i;
489
12.5k
         for (i=0;i<st->nbSubframes;i++)
490
10.0k
            ((spx_word16_t*)ptr)[i] = st->exc_rms[i];
491
2.51k
      }
492
2.51k
      break;
493
0
#ifndef DISABLE_VBR
494
1.59k
   case SPEEX_GET_RELATIVE_QUALITY:
495
1.59k
      (*(float*)ptr)=st->relative_quality;
496
1.59k
      break;
497
0
#endif /* #ifndef DISABLE_VBR */
498
2.51k
   case SPEEX_SET_INNOVATION_SAVE:
499
2.51k
      st->innov_rms_save = (spx_word16_t*)ptr;
500
2.51k
      break;
501
837
   case SPEEX_SET_WIDEBAND:
502
837
      speex_encoder_ctl(st->st_low, SPEEX_SET_WIDEBAND, ptr);
503
837
      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
110k
   }
511
110k
   return 0;
512
110k
}
sb_encoder_ctl
Line
Count
Source
256
110k
{
257
110k
   SBEncState *st;
258
110k
   st=(SBEncState*)state;
259
110k
   switch(request)
260
110k
   {
261
4.60k
   case SPEEX_GET_FRAME_SIZE:
262
4.60k
      (*(spx_int32_t*)ptr) = st->full_frame_size;
263
4.60k
      break;
264
5.44k
   case SPEEX_SET_HIGH_MODE:
265
5.44k
      st->submodeSelect = st->submodeID = (*(spx_int32_t*)ptr);
266
5.44k
      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
986
   case SPEEX_SET_DTX:
271
986
      speex_encoder_ctl(st->st_low, SPEEX_SET_DTX, ptr);
272
986
      break;
273
0
   case SPEEX_GET_DTX:
274
0
      speex_encoder_ctl(st->st_low, SPEEX_GET_DTX, ptr);
275
0
      break;
276
2.51k
   case SPEEX_GET_LOW_MODE:
277
2.51k
      speex_encoder_ctl(st->st_low, SPEEX_GET_LOW_MODE, ptr);
278
2.51k
      break;
279
7.54k
   case SPEEX_SET_MODE:
280
7.54k
      speex_encoder_ctl(st, SPEEX_SET_QUALITY, ptr);
281
7.54k
      break;
282
0
#ifndef DISABLE_VBR
283
1.23k
   case SPEEX_SET_VBR:
284
1.23k
      st->vbr_enabled = (*(spx_int32_t*)ptr);
285
1.23k
      speex_encoder_ctl(st->st_low, SPEEX_SET_VBR, ptr);
286
1.23k
      break;
287
0
   case SPEEX_GET_VBR:
288
0
      (*(spx_int32_t*)ptr) = st->vbr_enabled;
289
0
      break;
290
1.08k
   case SPEEX_SET_VAD:
291
1.08k
      st->vad_enabled = (*(spx_int32_t*)ptr);
292
1.08k
      speex_encoder_ctl(st->st_low, SPEEX_SET_VAD, ptr);
293
1.08k
      break;
294
0
   case SPEEX_GET_VAD:
295
0
      (*(spx_int32_t*)ptr) = st->vad_enabled;
296
0
      break;
297
0
#endif /* #ifndef DISABLE_VBR */
298
0
#if !defined(DISABLE_VBR) && !defined(DISABLE_FLOAT_API)
299
1.86k
   case SPEEX_SET_VBR_QUALITY:
300
1.86k
      {
301
1.86k
         spx_int32_t q;
302
1.86k
         float qual = (*(float*)ptr)+.6;
303
1.86k
         st->vbr_quality = (*(float*)ptr);
304
1.86k
         if (qual>10)
305
143
            qual=10;
306
1.86k
         q=(int)floor(.5+*(float*)ptr);
307
1.86k
         if (q>10)
308
0
            q=10;
309
1.86k
         speex_encoder_ctl(st->st_low, SPEEX_SET_VBR_QUALITY, &qual);
310
1.86k
         speex_encoder_ctl(state, SPEEX_SET_QUALITY, &q);
311
1.86k
         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
627
   case SPEEX_SET_ABR:
319
627
      st->abr_enabled = (*(spx_int32_t*)ptr);
320
627
      st->vbr_enabled = st->abr_enabled!=0;
321
627
      speex_encoder_ctl(st->st_low, SPEEX_SET_VBR, &st->vbr_enabled);
322
627
      if (st->vbr_enabled)
323
627
      {
324
627
         spx_int32_t i=10, rate, target;
325
627
         float vbr_qual;
326
627
         target = (*(spx_int32_t*)ptr);
327
7.52k
         while (i>=0)
328
6.89k
         {
329
6.89k
            speex_encoder_ctl(st, SPEEX_SET_QUALITY, &i);
330
6.89k
            speex_encoder_ctl(st, SPEEX_GET_BITRATE, &rate);
331
6.89k
            if (rate <= target)
332
0
               break;
333
6.89k
            i--;
334
6.89k
         }
335
627
         vbr_qual=i;
336
627
         if (vbr_qual<0)
337
627
            vbr_qual=0;
338
627
         speex_encoder_ctl(st, SPEEX_SET_VBR_QUALITY, &vbr_qual);
339
627
         st->abr_count=0;
340
627
         st->abr_drift=0;
341
627
         st->abr_drift2=0;
342
627
      }
343
344
627
      break;
345
0
   case SPEEX_GET_ABR:
346
0
      (*(spx_int32_t*)ptr) = st->abr_enabled;
347
0
      break;
348
0
#endif /* #ifndef DISABLE_VBR */
349
350
30.9k
   case SPEEX_SET_QUALITY:
351
30.9k
      {
352
30.9k
         spx_int32_t nb_qual;
353
30.9k
         int quality = (*(spx_int32_t*)ptr);
354
30.9k
         if (quality < 0)
355
0
            quality = 0;
356
30.9k
         if (quality > 10)
357
0
            quality = 10;
358
30.9k
         st->submodeSelect = st->submodeID = ((const SpeexSBMode*)(st->mode->mode))->quality_map[quality];
359
30.9k
         nb_qual = ((const SpeexSBMode*)(st->mode->mode))->low_quality_map[quality];
360
30.9k
         speex_encoder_ctl(st->st_low, SPEEX_SET_MODE, &nb_qual);
361
30.9k
      }
362
30.9k
      break;
363
2.74k
   case SPEEX_SET_COMPLEXITY:
364
2.74k
      speex_encoder_ctl(st->st_low, SPEEX_SET_COMPLEXITY, ptr);
365
2.74k
      st->complexity = (*(spx_int32_t*)ptr);
366
2.74k
      if (st->complexity<1)
367
490
         st->complexity=1;
368
2.74k
      break;
369
0
   case SPEEX_GET_COMPLEXITY:
370
0
      (*(spx_int32_t*)ptr) = st->complexity;
371
0
      break;
372
1.90k
   case SPEEX_SET_BITRATE:
373
1.90k
      {
374
1.90k
         spx_int32_t i=10;
375
1.90k
         spx_int32_t rate, target;
376
1.90k
         target = (*(spx_int32_t*)ptr);
377
13.0k
         while (i>=0)
378
12.6k
         {
379
12.6k
            speex_encoder_ctl(st, SPEEX_SET_QUALITY, &i);
380
12.6k
            speex_encoder_ctl(st, SPEEX_GET_BITRATE, &rate);
381
12.6k
            if (rate <= target)
382
1.44k
               break;
383
11.1k
            i--;
384
11.1k
         }
385
1.90k
      }
386
1.90k
      break;
387
29.0k
   case SPEEX_GET_BITRATE:
388
29.0k
      speex_encoder_ctl(st->st_low, request, ptr);
389
      /*fprintf (stderr, "before: %d\n", (*(int*)ptr));*/
390
29.0k
      if (st->submodes[st->submodeID])
391
28.5k
         (*(spx_int32_t*)ptr) += st->sampling_rate*SUBMODE(bits_per_frame)/st->full_frame_size;
392
510
      else
393
510
         (*(spx_int32_t*)ptr) += st->sampling_rate*(SB_SUBMODE_BITS+1)/st->full_frame_size;
394
      /*fprintf (stderr, "after: %d\n", (*(int*)ptr));*/
395
29.0k
      break;
396
2.74k
   case SPEEX_SET_SAMPLING_RATE:
397
2.74k
      {
398
2.74k
         spx_int32_t tmp=(*(spx_int32_t*)ptr);
399
2.74k
         st->sampling_rate = tmp;
400
2.74k
         tmp>>=1;
401
2.74k
         speex_encoder_ctl(st->st_low, SPEEX_SET_SAMPLING_RATE, &tmp);
402
2.74k
      }
403
2.74k
      break;
404
837
   case SPEEX_GET_SAMPLING_RATE:
405
837
      (*(spx_int32_t*)ptr)=st->sampling_rate;
406
837
      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.74k
   case SPEEX_GET_LOOKAHEAD:
427
2.74k
      speex_encoder_ctl(st->st_low, SPEEX_GET_LOOKAHEAD, ptr);
428
2.74k
      (*(spx_int32_t*)ptr) = 2*(*(spx_int32_t*)ptr) + QMF_ORDER - 1;
429
2.74k
      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.06k
   case SPEEX_SET_VBR_MAX_BITRATE:
438
1.06k
      {
439
1.06k
         st->vbr_max = (*(spx_int32_t*)ptr);
440
1.06k
         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.06k
         } else {
445
1.06k
            spx_int32_t low_rate;
446
1.06k
            if (st->vbr_max >= 42200)
447
0
            {
448
0
               st->vbr_max_high = 17600;
449
1.06k
            } else if (st->vbr_max >= 27800)
450
0
            {
451
0
               st->vbr_max_high = 9600;
452
1.06k
            } else if (st->vbr_max > 20600)
453
0
            {
454
0
               st->vbr_max_high = 5600;
455
1.06k
            } else {
456
1.06k
               st->vbr_max_high = 1800;
457
1.06k
            }
458
1.06k
            if (st->subframeSize==80)
459
353
               st->vbr_max_high = 1800;
460
1.06k
            low_rate = st->vbr_max - st->vbr_max_high;
461
1.06k
            speex_encoder_ctl(st->st_low, SPEEX_SET_VBR_MAX_BITRATE, &low_rate);
462
1.06k
         }
463
1.06k
      }
464
1.06k
      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.74k
   case SPEEX_SET_HIGHPASS:
470
2.74k
      speex_encoder_ctl(st->st_low, SPEEX_SET_HIGHPASS, ptr);
471
2.74k
      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.51k
   case SPEEX_GET_PI_GAIN:
479
2.51k
      {
480
2.51k
         int i;
481
2.51k
         spx_word32_t *g = (spx_word32_t*)ptr;
482
12.5k
         for (i=0;i<st->nbSubframes;i++)
483
10.0k
            g[i]=st->pi_gain[i];
484
2.51k
      }
485
2.51k
      break;
486
2.51k
   case SPEEX_GET_EXC:
487
2.51k
      {
488
2.51k
         int i;
489
12.5k
         for (i=0;i<st->nbSubframes;i++)
490
10.0k
            ((spx_word16_t*)ptr)[i] = st->exc_rms[i];
491
2.51k
      }
492
2.51k
      break;
493
0
#ifndef DISABLE_VBR
494
1.59k
   case SPEEX_GET_RELATIVE_QUALITY:
495
1.59k
      (*(float*)ptr)=st->relative_quality;
496
1.59k
      break;
497
0
#endif /* #ifndef DISABLE_VBR */
498
2.51k
   case SPEEX_SET_INNOVATION_SAVE:
499
2.51k
      st->innov_rms_save = (spx_word16_t*)ptr;
500
2.51k
      break;
501
837
   case SPEEX_SET_WIDEBAND:
502
837
      speex_encoder_ctl(st->st_low, SPEEX_SET_WIDEBAND, ptr);
503
837
      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
110k
   }
511
110k
   return 0;
512
110k
}
513
514
515
int sb_encode(void *state, void *vin, SpeexBits *bits)
516
10.3k
{
517
10.3k
   SBEncState *st;
518
10.3k
   int i, roots, sub;
519
10.3k
   char *stack;
520
10.3k
   VARDECL(spx_mem_t *mem);
521
10.3k
   VARDECL(spx_sig_t *innov);
522
10.3k
   VARDECL(spx_word16_t *target);
523
10.3k
   VARDECL(spx_word16_t *syn_resp);
524
10.3k
   VARDECL(spx_word32_t *low_pi_gain);
525
10.3k
   spx_word16_t *low;
526
10.3k
   spx_word16_t *high;
527
10.3k
   VARDECL(spx_word16_t *low_exc_rms);
528
10.3k
   VARDECL(spx_word16_t *low_innov_rms);
529
10.3k
   const SpeexSBMode *mode;
530
10.3k
   spx_int32_t dtx;
531
10.3k
   spx_word16_t *in = (spx_word16_t*)vin;
532
10.3k
   spx_word16_t e_low=0, e_high=0;
533
10.3k
   VARDECL(spx_coef_t *lpc);
534
10.3k
   VARDECL(spx_coef_t *interp_lpc);
535
10.3k
   VARDECL(spx_coef_t *bw_lpc1);
536
10.3k
   VARDECL(spx_coef_t *bw_lpc2);
537
10.3k
   VARDECL(spx_lsp_t *lsp);
538
10.3k
   VARDECL(spx_lsp_t *qlsp);
539
10.3k
   VARDECL(spx_lsp_t *interp_lsp);
540
10.3k
   VARDECL(spx_lsp_t *interp_qlsp);
541
542
10.3k
   st = (SBEncState*)state;
543
10.3k
   stack=st->stack;
544
10.3k
   mode = (const SpeexSBMode*)(st->mode->mode);
545
10.3k
   low = in;
546
10.3k
   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.3k
   qmf_decomp(in, h0, low, high, st->full_frame_size, QMF_ORDER, st->h0_mem, stack);
551
552
10.3k
#ifndef DISABLE_VBR
553
10.3k
   if (st->vbr_enabled || st->vad_enabled)
554
7.48k
   {
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.48k
      e_low = compute_rms16(low, st->frame_size);
558
7.48k
      e_high = compute_rms16(high, st->frame_size);
559
7.48k
   }
560
10.3k
#endif /* #ifndef DISABLE_VBR */
561
562
10.3k
   ALLOC(low_innov_rms, st->nbSubframes, spx_word16_t);
563
10.3k
   speex_encoder_ctl(st->st_low, SPEEX_SET_INNOVATION_SAVE, low_innov_rms);
564
   /* Encode the narrowband part*/
565
10.3k
   speex_encode_native(st->st_low, low, bits);
566
567
10.3k
   high = high - (st->windowSize-st->frame_size);
568
10.3k
   SPEEX_COPY(high, st->high, st->windowSize-st->frame_size);
569
10.3k
   SPEEX_COPY(st->high, &high[st->frame_size], st->windowSize-st->frame_size);
570
571
572
10.3k
   ALLOC(low_pi_gain, st->nbSubframes, spx_word32_t);
573
10.3k
   ALLOC(low_exc_rms, st->nbSubframes, spx_word16_t);
574
10.3k
   speex_encoder_ctl(st->st_low, SPEEX_GET_PI_GAIN, low_pi_gain);
575
10.3k
   speex_encoder_ctl(st->st_low, SPEEX_GET_EXC, low_exc_rms);
576
577
10.3k
   speex_encoder_ctl(st->st_low, SPEEX_GET_LOW_MODE, &dtx);
578
579
10.3k
   if (dtx==0)
580
550
      dtx=1;
581
9.80k
   else
582
9.80k
      dtx=0;
583
584
10.3k
   ALLOC(lpc, st->lpcSize, spx_coef_t);
585
10.3k
   ALLOC(interp_lpc, st->lpcSize, spx_coef_t);
586
10.3k
   ALLOC(bw_lpc1, st->lpcSize, spx_coef_t);
587
10.3k
   ALLOC(bw_lpc2, st->lpcSize, spx_coef_t);
588
589
10.3k
   ALLOC(lsp, st->lpcSize, spx_lsp_t);
590
10.3k
   ALLOC(qlsp, st->lpcSize, spx_lsp_t);
591
10.3k
   ALLOC(interp_lsp, st->lpcSize, spx_lsp_t);
592
10.3k
   ALLOC(interp_qlsp, st->lpcSize, spx_lsp_t);
593
594
10.3k
   {
595
10.3k
      VARDECL(spx_word16_t *autocorr);
596
10.3k
      VARDECL(spx_word16_t *w_sig);
597
10.3k
      ALLOC(autocorr, st->lpcSize+1, spx_word16_t);
598
10.3k
      ALLOC(w_sig, st->windowSize, spx_word16_t);
599
      /* Window for analysis */
600
      /* FIXME: This is a kludge */
601
10.3k
      if (st->subframeSize==80)
602
2.51k
      {
603
1.00M
         for (i=0;i<st->windowSize;i++)
604
1.00M
            w_sig[i] = EXTRACT16(SHR32(MULT16_16(high[i],st->window[i>>1]),SIG_SHIFT));
605
7.84k
      } else {
606
1.57M
         for (i=0;i<st->windowSize;i++)
607
1.56M
            w_sig[i] = EXTRACT16(SHR32(MULT16_16(high[i],st->window[i]),SIG_SHIFT));
608
7.84k
      }
609
      /* Compute auto-correlation */
610
10.3k
      _spx_autocorr(w_sig, autocorr, st->lpcSize+1, st->windowSize);
611
10.3k
      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
103k
      for (i=0;i<st->lpcSize+1;i++)
615
93.1k
         autocorr[i] = MULT16_16_Q14(autocorr[i],st->lagWindow[i]);
616
617
      /* Levinson-Durbin */
618
10.3k
      _spx_lpc(lpc, autocorr, st->lpcSize);
619
10.3k
   }
620
621
   /* LPC to LSPs (x-domain) transform */
622
10.3k
   roots=lpc_to_lsp (lpc, st->lpcSize, lsp, 10, LSP_DELTA1, stack);
623
10.3k
   if (roots!=st->lpcSize)
624
161
   {
625
161
      roots = lpc_to_lsp (lpc, st->lpcSize, lsp, 10, LSP_DELTA2, stack);
626
161
      if (roots!=st->lpcSize) {
627
         /*If we can't find all LSP's, do some damage control and use a flat filter*/
628
945
         for (i=0;i<st->lpcSize;i++)
629
840
         {
630
840
            lsp[i]=st->old_lsp[i];
631
840
         }
632
105
      }
633
161
   }
634
635
10.3k
#ifndef DISABLE_VBR
636
   /* VBR code */
637
10.3k
   if ((st->vbr_enabled || st->vad_enabled) && !dtx)
638
6.93k
   {
639
6.93k
      float ratio;
640
6.93k
      if (st->abr_enabled)
641
2.49k
      {
642
2.49k
         float qual_change=0;
643
2.49k
         if (st->abr_drift2 * st->abr_drift > 0)
644
1.86k
         {
645
            /* Only adapt if long-term and short-term drift are the same sign */
646
1.86k
            qual_change = -.00001*st->abr_drift/(1+st->abr_count);
647
1.86k
            if (qual_change>.1)
648
0
               qual_change=.1;
649
1.86k
            if (qual_change<-.1)
650
395
               qual_change=-.1;
651
1.86k
         }
652
2.49k
         st->vbr_quality += qual_change;
653
2.49k
         if (st->vbr_quality>10)
654
0
            st->vbr_quality=10;
655
2.49k
         if (st->vbr_quality<0)
656
1.86k
            st->vbr_quality=0;
657
2.49k
      }
658
659
660
6.93k
      ratio = 2*log((1.f+e_high)/(1.f+e_low));
661
662
6.93k
      speex_encoder_ctl(st->st_low, SPEEX_GET_RELATIVE_QUALITY, &st->relative_quality);
663
6.93k
      if (ratio<-4)
664
96
         ratio=-4;
665
6.93k
      if (ratio>2)
666
78
         ratio=2;
667
      /*if (ratio>-2)*/
668
6.93k
      if (st->vbr_enabled)
669
5.44k
      {
670
5.44k
         spx_int32_t modeid;
671
5.44k
         modeid = mode->nb_modes-1;
672
5.44k
         st->relative_quality+=1.0*(ratio+2);
673
5.44k
   if (st->relative_quality<-1)
674
14
            st->relative_quality=-1;
675
17.0k
         while (modeid)
676
16.6k
         {
677
16.6k
            int v1;
678
16.6k
            float thresh;
679
16.6k
            v1=(int)floor(st->vbr_quality);
680
16.6k
            if (v1==10)
681
855
               thresh = mode->vbr_thresh[modeid][v1];
682
15.7k
            else
683
15.7k
               thresh = (st->vbr_quality-v1)   * mode->vbr_thresh[modeid][v1+1] +
684
15.7k
                        (1+v1-st->vbr_quality) * mode->vbr_thresh[modeid][v1];
685
16.6k
            if (st->relative_quality >= thresh && st->sampling_rate*st->submodes[modeid]->bits_per_frame/st->full_frame_size <= st->vbr_max_high)
686
5.05k
               break;
687
11.5k
            modeid--;
688
11.5k
         }
689
5.44k
         speex_encoder_ctl(state, SPEEX_SET_HIGH_MODE, &modeid);
690
5.44k
         if (st->abr_enabled)
691
2.49k
         {
692
2.49k
            spx_int32_t bitrate;
693
2.49k
            speex_encoder_ctl(state, SPEEX_GET_BITRATE, &bitrate);
694
2.49k
            st->abr_drift+=(bitrate-st->abr_enabled);
695
2.49k
            st->abr_drift2 = .95*st->abr_drift2 + .05*(bitrate-st->abr_enabled);
696
2.49k
            st->abr_count += 1.0;
697
2.49k
         }
698
699
5.44k
      } else {
700
         /* VAD only */
701
1.48k
         int modeid;
702
1.48k
         if (st->relative_quality<2.0)
703
400
            modeid=1;
704
1.08k
         else
705
1.08k
            modeid=st->submodeSelect;
706
         /*speex_encoder_ctl(state, SPEEX_SET_MODE, &mode);*/
707
1.48k
         st->submodeID=modeid;
708
709
1.48k
      }
710
      /*fprintf (stderr, "%f %f\n", ratio, low_qual);*/
711
6.93k
   }
712
10.3k
#endif /* #ifndef DISABLE_VBR */
713
714
10.3k
   if (st->encode_submode)
715
10.3k
   {
716
10.3k
      speex_bits_pack(bits, 1, 1);
717
10.3k
      if (dtx)
718
550
         speex_bits_pack(bits, 0, SB_SUBMODE_BITS);
719
9.80k
      else
720
9.80k
         speex_bits_pack(bits, st->submodeID, SB_SUBMODE_BITS);
721
10.3k
   }
722
723
   /* If null mode (no transmission), just set a couple things to zero*/
724
10.3k
   if (dtx || st->submodes[st->submodeID] == NULL)
725
1.08k
   {
726
220k
      for (i=0;i<st->frame_size;i++)
727
219k
         high[i]=VERY_SMALL;
728
729
9.75k
      for (i=0;i<st->lpcSize;i++)
730
8.67k
         st->mem_sw[i]=0;
731
1.08k
      st->first=1;
732
733
      /* Final signal synthesis from excitation */
734
1.08k
      iir_mem16(high, st->interp_qlpc, high, st->frame_size, st->lpcSize, st->mem_sp, stack);
735
736
1.08k
      if (dtx)
737
550
         return 0;
738
534
      else
739
534
         return 1;
740
1.08k
   }
741
742
743
   /* LSP quantization */
744
9.26k
   SUBMODE(lsp_quant)(lsp, qlsp, st->lpcSize, bits);
745
746
9.26k
   if (st->first)
747
2.65k
   {
748
23.8k
      for (i=0;i<st->lpcSize;i++)
749
21.2k
         st->old_lsp[i] = lsp[i];
750
23.8k
      for (i=0;i<st->lpcSize;i++)
751
21.2k
         st->old_qlsp[i] = qlsp[i];
752
2.65k
   }
753
754
9.26k
   ALLOC(mem, st->lpcSize, spx_mem_t);
755
9.26k
   ALLOC(syn_resp, st->subframeSize, spx_word16_t);
756
9.26k
   ALLOC(innov, st->subframeSize, spx_sig_t);
757
9.26k
   ALLOC(target, st->subframeSize, spx_word16_t);
758
759
46.3k
   for (sub=0;sub<st->nbSubframes;sub++)
760
37.0k
   {
761
37.0k
      VARDECL(spx_word16_t *exc);
762
37.0k
      VARDECL(spx_word16_t *res);
763
37.0k
      VARDECL(spx_word16_t *sw);
764
37.0k
      spx_word16_t *sp;
765
37.0k
      spx_word16_t filter_ratio;     /*Q7*/
766
37.0k
      int offset;
767
37.0k
      spx_word32_t rl, rh;           /*Q13*/
768
37.0k
      spx_word16_t eh=0;
769
770
37.0k
      offset = st->subframeSize*sub;
771
37.0k
      sp=high+offset;
772
37.0k
      ALLOC(exc, st->subframeSize, spx_word16_t);
773
37.0k
      ALLOC(res, st->subframeSize, spx_word16_t);
774
37.0k
      ALLOC(sw, st->subframeSize, spx_word16_t);
775
776
      /* LSP interpolation (quantized and unquantized) */
777
37.0k
      lsp_interpolate(st->old_lsp, lsp, interp_lsp, st->lpcSize, sub, st->nbSubframes, LSP_MARGIN);
778
37.0k
      lsp_interpolate(st->old_qlsp, qlsp, interp_qlsp, st->lpcSize, sub, st->nbSubframes, LSP_MARGIN);
779
780
37.0k
      lsp_to_lpc(interp_lsp, interp_lpc, st->lpcSize,stack);
781
37.0k
      lsp_to_lpc(interp_qlsp, st->interp_qlpc, st->lpcSize, stack);
782
783
37.0k
      bw_lpc(st->gamma1, interp_lpc, bw_lpc1, st->lpcSize);
784
37.0k
      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.0k
      st->pi_gain[sub]=LPC_SCALING;
789
37.0k
      rh = LPC_SCALING;
790
185k
      for (i=0;i<st->lpcSize;i+=2)
791
148k
      {
792
148k
         rh += st->interp_qlpc[i+1] - st->interp_qlpc[i];
793
148k
         st->pi_gain[sub] += st->interp_qlpc[i] + st->interp_qlpc[i+1];
794
148k
      }
795
796
37.0k
      rl = low_pi_gain[sub];
797
#ifdef FIXED_POINT
798
24.3k
      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.0k
      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.0k
      eh = compute_rms16(exc, st->subframeSize);
808
809
37.0k
      if (!SUBMODE(innovation_quant)) {/* 1 for spectral folding excitation, 0 for stochastic */
810
28.1k
         spx_word32_t g;   /*Q7*/
811
28.1k
         spx_word16_t el;  /*Q0*/
812
28.1k
         el = low_innov_rms[sub];
813
814
         /* Gain to use if we want to use the low-band excitation for high-band */
815
28.1k
         g=PDIV32(MULT16_16(filter_ratio,eh),EXTEND32(ADD16(1,el)));
816
817
#if 0
818
         {
819
            char *tmp_stack=stack;
820
            float *tmp_sig;
821
            float g2;
822
            ALLOC(tmp_sig, st->subframeSize, spx_sig_t);
823
            for (i=0;i<st->lpcSize;i++)
824
               mem[i]=st->mem_sp[i];
825
            iir_mem2(st->low_innov+offset, st->interp_qlpc, tmp_sig, st->subframeSize, st->lpcSize, mem);
826
            g2 = compute_rms(sp, st->subframeSize)/(.01+compute_rms(tmp_sig, st->subframeSize));
827
            /*fprintf (stderr, "gains: %f %f\n", g, g2);*/
828
            g = g2;
829
            stack = tmp_stack;
830
         }
831
#endif
832
833
         /*print_vec(&g, 1, "gain factor");*/
834
         /* Gain quantization */
835
28.1k
         {
836
28.1k
            int quant = scal_quant(g, fold_quant_bound, 32);
837
            /*speex_warning_int("tata", quant);*/
838
28.1k
            if (quant<0)
839
0
               quant=0;
840
28.1k
            if (quant>31)
841
0
               quant=31;
842
28.1k
            speex_bits_pack(bits, quant, 5);
843
28.1k
         }
844
28.1k
         if (st->innov_rms_save)
845
5.81k
         {
846
5.81k
            st->innov_rms_save[sub] = eh;
847
5.81k
         }
848
28.1k
         st->exc_rms[sub] = eh;
849
28.1k
      } else {
850
8.90k
         spx_word16_t gc;       /*Q7*/
851
8.90k
         spx_word32_t scale;    /*Q14*/
852
8.90k
         spx_word16_t el;       /*Q0*/
853
8.90k
         el = low_exc_rms[sub]; /*Q0*/
854
855
8.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
8.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
8.90k
         {
862
8.90k
            int qgc = scal_quant(gc, gc_quant_bound, 16);
863
8.90k
            speex_bits_pack(bits, qgc, 4);
864
8.90k
            gc = MULT16_16_Q15(QCONST16(0.87360,15),gc_quant_bound[qgc]);
865
8.90k
         }
866
8.90k
         if (st->subframeSize==80)
867
0
            gc = MULT16_16_P14(QCONST16(1.4142f,14), gc);
868
869
8.90k
         scale = SHL32(MULT16_16(PDIV32_16(SHL32(EXTEND32(gc),SIG_SHIFT-6),MAX16(EPSILON,filter_ratio)),(1+el)),6);
870
871
8.90k
         compute_impulse_response(st->interp_qlpc, bw_lpc1, bw_lpc2, syn_resp, st->subframeSize, st->lpcSize, stack);
872
873
874
         /* Reset excitation */
875
365k
         for (i=0;i<st->subframeSize;i++)
876
356k
            res[i]=VERY_SMALL;
877
878
         /* Compute zero response (ringing) of A(z/g1) / ( A(z/g2) * Aq(z) ) */
879
80.1k
         for (i=0;i<st->lpcSize;i++)
880
71.2k
            mem[i]=st->mem_sp[i];
881
8.90k
         iir_mem16(res, st->interp_qlpc, res, st->subframeSize, st->lpcSize, mem, stack);
882
883
80.1k
         for (i=0;i<st->lpcSize;i++)
884
71.2k
            mem[i]=st->mem_sw[i];
885
8.90k
         filter_mem16(res, bw_lpc1, bw_lpc2, res, st->subframeSize, st->lpcSize, mem, stack);
886
887
         /* Compute weighted signal */
888
80.1k
         for (i=0;i<st->lpcSize;i++)
889
71.2k
            mem[i]=st->mem_sw[i];
890
8.90k
         filter_mem16(sp, bw_lpc1, bw_lpc2, sw, st->subframeSize, st->lpcSize, mem, stack);
891
892
         /* Compute target signal */
893
365k
         for (i=0;i<st->subframeSize;i++)
894
356k
            target[i]=SUB16(sw[i],res[i]);
895
896
8.90k
         signal_div(target, target, scale, st->subframeSize);
897
898
         /* Reset excitation */
899
8.90k
         SPEEX_MEMSET(innov, 0, st->subframeSize);
900
901
         /*print_vec(target, st->subframeSize, "\ntarget");*/
902
8.90k
         SUBMODE(innovation_quant)(target, st->interp_qlpc, bw_lpc1, bw_lpc2,
903
8.90k
                                   SUBMODE(innovation_params), st->lpcSize, st->subframeSize,
904
8.90k
                                   innov, syn_resp, bits, stack, st->complexity, SUBMODE(double_codebook));
905
         /*print_vec(target, st->subframeSize, "after");*/
906
907
8.90k
         signal_mul(innov, innov, scale, st->subframeSize);
908
909
8.90k
         if (SUBMODE(double_codebook)) {
910
2.88k
            char *tmp_stack=stack;
911
2.88k
            VARDECL(spx_sig_t *innov2);
912
2.88k
            ALLOC(innov2, st->subframeSize, spx_sig_t);
913
2.88k
            SPEEX_MEMSET(innov2, 0, st->subframeSize);
914
118k
            for (i=0;i<st->subframeSize;i++)
915
115k
               target[i]=MULT16_16_P13(QCONST16(2.5f,13), target[i]);
916
917
2.88k
            SUBMODE(innovation_quant)(target, st->interp_qlpc, bw_lpc1, bw_lpc2,
918
2.88k
                                      SUBMODE(innovation_params), st->lpcSize, st->subframeSize,
919
2.88k
                                      innov2, syn_resp, bits, stack, st->complexity, 0);
920
2.88k
            signal_mul(innov2, innov2, MULT16_32_P15(QCONST16(0.4f,15),scale), st->subframeSize);
921
922
118k
            for (i=0;i<st->subframeSize;i++)
923
115k
               innov[i] = ADD32(innov[i],innov2[i]);
924
2.88k
            stack = tmp_stack;
925
2.88k
         }
926
365k
         for (i=0;i<st->subframeSize;i++)
927
356k
            exc[i] = PSHR32(innov[i],SIG_SHIFT);
928
929
8.90k
         if (st->innov_rms_save)
930
3.93k
         {
931
3.93k
            st->innov_rms_save[sub] = MULT16_16_Q15(QCONST16(.70711f, 15), compute_rms(innov, st->subframeSize));
932
3.93k
         }
933
8.90k
         st->exc_rms[sub] = compute_rms16(exc, st->subframeSize);
934
935
936
8.90k
      }
937
938
939
      /*Keep the previous memory*/
940
333k
      for (i=0;i<st->lpcSize;i++)
941
296k
         mem[i]=st->mem_sp[i];
942
      /* Final signal synthesis from excitation */
943
37.0k
      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.0k
      filter_mem16(sp, bw_lpc1, bw_lpc2, sw, st->subframeSize, st->lpcSize, st->mem_sw, stack);
947
37.0k
   }
948
949
83.4k
   for (i=0;i<st->lpcSize;i++)
950
74.1k
      st->old_lsp[i] = lsp[i];
951
83.4k
   for (i=0;i<st->lpcSize;i++)
952
74.1k
      st->old_qlsp[i] = qlsp[i];
953
954
9.26k
   st->first=0;
955
956
9.26k
   return 1;
957
10.3k
}
sb_encode
Line
Count
Source
516
3.66k
{
517
3.66k
   SBEncState *st;
518
3.66k
   int i, roots, sub;
519
3.66k
   char *stack;
520
3.66k
   VARDECL(spx_mem_t *mem);
521
3.66k
   VARDECL(spx_sig_t *innov);
522
3.66k
   VARDECL(spx_word16_t *target);
523
3.66k
   VARDECL(spx_word16_t *syn_resp);
524
3.66k
   VARDECL(spx_word32_t *low_pi_gain);
525
3.66k
   spx_word16_t *low;
526
3.66k
   spx_word16_t *high;
527
3.66k
   VARDECL(spx_word16_t *low_exc_rms);
528
3.66k
   VARDECL(spx_word16_t *low_innov_rms);
529
3.66k
   const SpeexSBMode *mode;
530
3.66k
   spx_int32_t dtx;
531
3.66k
   spx_word16_t *in = (spx_word16_t*)vin;
532
3.66k
   spx_word16_t e_low=0, e_high=0;
533
3.66k
   VARDECL(spx_coef_t *lpc);
534
3.66k
   VARDECL(spx_coef_t *interp_lpc);
535
3.66k
   VARDECL(spx_coef_t *bw_lpc1);
536
3.66k
   VARDECL(spx_coef_t *bw_lpc2);
537
3.66k
   VARDECL(spx_lsp_t *lsp);
538
3.66k
   VARDECL(spx_lsp_t *qlsp);
539
3.66k
   VARDECL(spx_lsp_t *interp_lsp);
540
3.66k
   VARDECL(spx_lsp_t *interp_qlsp);
541
542
3.66k
   st = (SBEncState*)state;
543
3.66k
   stack=st->stack;
544
3.66k
   mode = (const SpeexSBMode*)(st->mode->mode);
545
3.66k
   low = in;
546
3.66k
   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.66k
   qmf_decomp(in, h0, low, high, st->full_frame_size, QMF_ORDER, st->h0_mem, stack);
551
552
3.66k
#ifndef DISABLE_VBR
553
3.66k
   if (st->vbr_enabled || st->vad_enabled)
554
2.54k
   {
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.54k
      e_low = compute_rms16(low, st->frame_size);
558
2.54k
      e_high = compute_rms16(high, st->frame_size);
559
2.54k
   }
560
3.66k
#endif /* #ifndef DISABLE_VBR */
561
562
3.66k
   ALLOC(low_innov_rms, st->nbSubframes, spx_word16_t);
563
3.66k
   speex_encoder_ctl(st->st_low, SPEEX_SET_INNOVATION_SAVE, low_innov_rms);
564
   /* Encode the narrowband part*/
565
3.66k
   speex_encode_native(st->st_low, low, bits);
566
567
3.66k
   high = high - (st->windowSize-st->frame_size);
568
3.66k
   SPEEX_COPY(high, st->high, st->windowSize-st->frame_size);
569
3.66k
   SPEEX_COPY(st->high, &high[st->frame_size], st->windowSize-st->frame_size);
570
571
572
3.66k
   ALLOC(low_pi_gain, st->nbSubframes, spx_word32_t);
573
3.66k
   ALLOC(low_exc_rms, st->nbSubframes, spx_word16_t);
574
3.66k
   speex_encoder_ctl(st->st_low, SPEEX_GET_PI_GAIN, low_pi_gain);
575
3.66k
   speex_encoder_ctl(st->st_low, SPEEX_GET_EXC, low_exc_rms);
576
577
3.66k
   speex_encoder_ctl(st->st_low, SPEEX_GET_LOW_MODE, &dtx);
578
579
3.66k
   if (dtx==0)
580
237
      dtx=1;
581
3.43k
   else
582
3.43k
      dtx=0;
583
584
3.66k
   ALLOC(lpc, st->lpcSize, spx_coef_t);
585
3.66k
   ALLOC(interp_lpc, st->lpcSize, spx_coef_t);
586
3.66k
   ALLOC(bw_lpc1, st->lpcSize, spx_coef_t);
587
3.66k
   ALLOC(bw_lpc2, st->lpcSize, spx_coef_t);
588
589
3.66k
   ALLOC(lsp, st->lpcSize, spx_lsp_t);
590
3.66k
   ALLOC(qlsp, st->lpcSize, spx_lsp_t);
591
3.66k
   ALLOC(interp_lsp, st->lpcSize, spx_lsp_t);
592
3.66k
   ALLOC(interp_qlsp, st->lpcSize, spx_lsp_t);
593
594
3.66k
   {
595
3.66k
      VARDECL(spx_word16_t *autocorr);
596
3.66k
      VARDECL(spx_word16_t *w_sig);
597
3.66k
      ALLOC(autocorr, st->lpcSize+1, spx_word16_t);
598
3.66k
      ALLOC(w_sig, st->windowSize, spx_word16_t);
599
      /* Window for analysis */
600
      /* FIXME: This is a kludge */
601
3.66k
      if (st->subframeSize==80)
602
972
      {
603
389k
         for (i=0;i<st->windowSize;i++)
604
388k
            w_sig[i] = EXTRACT16(SHR32(MULT16_16(high[i],st->window[i>>1]),SIG_SHIFT));
605
2.69k
      } else {
606
542k
         for (i=0;i<st->windowSize;i++)
607
539k
            w_sig[i] = EXTRACT16(SHR32(MULT16_16(high[i],st->window[i]),SIG_SHIFT));
608
2.69k
      }
609
      /* Compute auto-correlation */
610
3.66k
      _spx_autocorr(w_sig, autocorr, st->lpcSize+1, st->windowSize);
611
3.66k
      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
36.6k
      for (i=0;i<st->lpcSize+1;i++)
615
33.0k
         autocorr[i] = MULT16_16_Q14(autocorr[i],st->lagWindow[i]);
616
617
      /* Levinson-Durbin */
618
3.66k
      _spx_lpc(lpc, autocorr, st->lpcSize);
619
3.66k
   }
620
621
   /* LPC to LSPs (x-domain) transform */
622
3.66k
   roots=lpc_to_lsp (lpc, st->lpcSize, lsp, 10, LSP_DELTA1, stack);
623
3.66k
   if (roots!=st->lpcSize)
624
20
   {
625
20
      roots = lpc_to_lsp (lpc, st->lpcSize, lsp, 10, LSP_DELTA2, stack);
626
20
      if (roots!=st->lpcSize) {
627
         /*If we can't find all LSP's, do some damage control and use a flat filter*/
628
0
         for (i=0;i<st->lpcSize;i++)
629
0
         {
630
0
            lsp[i]=st->old_lsp[i];
631
0
         }
632
0
      }
633
20
   }
634
635
3.66k
#ifndef DISABLE_VBR
636
   /* VBR code */
637
3.66k
   if ((st->vbr_enabled || st->vad_enabled) && !dtx)
638
2.30k
   {
639
2.30k
      float ratio;
640
2.30k
      if (st->abr_enabled)
641
621
      {
642
621
         float qual_change=0;
643
621
         if (st->abr_drift2 * st->abr_drift > 0)
644
451
         {
645
            /* Only adapt if long-term and short-term drift are the same sign */
646
451
            qual_change = -.00001*st->abr_drift/(1+st->abr_count);
647
451
            if (qual_change>.1)
648
0
               qual_change=.1;
649
451
            if (qual_change<-.1)
650
141
               qual_change=-.1;
651
451
         }
652
621
         st->vbr_quality += qual_change;
653
621
         if (st->vbr_quality>10)
654
0
            st->vbr_quality=10;
655
621
         if (st->vbr_quality<0)
656
451
            st->vbr_quality=0;
657
621
      }
658
659
660
2.30k
      ratio = 2*log((1.f+e_high)/(1.f+e_low));
661
662
2.30k
      speex_encoder_ctl(st->st_low, SPEEX_GET_RELATIVE_QUALITY, &st->relative_quality);
663
2.30k
      if (ratio<-4)
664
39
         ratio=-4;
665
2.30k
      if (ratio>2)
666
18
         ratio=2;
667
      /*if (ratio>-2)*/
668
2.30k
      if (st->vbr_enabled)
669
1.74k
      {
670
1.74k
         spx_int32_t modeid;
671
1.74k
         modeid = mode->nb_modes-1;
672
1.74k
         st->relative_quality+=1.0*(ratio+2);
673
1.74k
   if (st->relative_quality<-1)
674
11
            st->relative_quality=-1;
675
5.39k
         while (modeid)
676
5.21k
         {
677
5.21k
            int v1;
678
5.21k
            float thresh;
679
5.21k
            v1=(int)floor(st->vbr_quality);
680
5.21k
            if (v1==10)
681
407
               thresh = mode->vbr_thresh[modeid][v1];
682
4.80k
            else
683
4.80k
               thresh = (st->vbr_quality-v1)   * mode->vbr_thresh[modeid][v1+1] +
684
4.80k
                        (1+v1-st->vbr_quality) * mode->vbr_thresh[modeid][v1];
685
5.21k
            if (st->relative_quality >= thresh && st->sampling_rate*st->submodes[modeid]->bits_per_frame/st->full_frame_size <= st->vbr_max_high)
686
1.57k
               break;
687
3.64k
            modeid--;
688
3.64k
         }
689
1.74k
         speex_encoder_ctl(state, SPEEX_SET_HIGH_MODE, &modeid);
690
1.74k
         if (st->abr_enabled)
691
621
         {
692
621
            spx_int32_t bitrate;
693
621
            speex_encoder_ctl(state, SPEEX_GET_BITRATE, &bitrate);
694
621
            st->abr_drift+=(bitrate-st->abr_enabled);
695
621
            st->abr_drift2 = .95*st->abr_drift2 + .05*(bitrate-st->abr_enabled);
696
621
            st->abr_count += 1.0;
697
621
         }
698
699
1.74k
      } else {
700
         /* VAD only */
701
559
         int modeid;
702
559
         if (st->relative_quality<2.0)
703
224
            modeid=1;
704
335
         else
705
335
            modeid=st->submodeSelect;
706
         /*speex_encoder_ctl(state, SPEEX_SET_MODE, &mode);*/
707
559
         st->submodeID=modeid;
708
709
559
      }
710
      /*fprintf (stderr, "%f %f\n", ratio, low_qual);*/
711
2.30k
   }
712
3.66k
#endif /* #ifndef DISABLE_VBR */
713
714
3.66k
   if (st->encode_submode)
715
3.66k
   {
716
3.66k
      speex_bits_pack(bits, 1, 1);
717
3.66k
      if (dtx)
718
237
         speex_bits_pack(bits, 0, SB_SUBMODE_BITS);
719
3.43k
      else
720
3.43k
         speex_bits_pack(bits, st->submodeID, SB_SUBMODE_BITS);
721
3.66k
   }
722
723
   /* If null mode (no transmission), just set a couple things to zero*/
724
3.66k
   if (dtx || st->submodes[st->submodeID] == NULL)
725
489
   {
726
99.6k
      for (i=0;i<st->frame_size;i++)
727
99.2k
         high[i]=VERY_SMALL;
728
729
4.40k
      for (i=0;i<st->lpcSize;i++)
730
3.91k
         st->mem_sw[i]=0;
731
489
      st->first=1;
732
733
      /* Final signal synthesis from excitation */
734
489
      iir_mem16(high, st->interp_qlpc, high, st->frame_size, st->lpcSize, st->mem_sp, stack);
735
736
489
      if (dtx)
737
237
         return 0;
738
252
      else
739
252
         return 1;
740
489
   }
741
742
743
   /* LSP quantization */
744
3.18k
   SUBMODE(lsp_quant)(lsp, qlsp, st->lpcSize, bits);
745
746
3.18k
   if (st->first)
747
956
   {
748
8.60k
      for (i=0;i<st->lpcSize;i++)
749
7.64k
         st->old_lsp[i] = lsp[i];
750
8.60k
      for (i=0;i<st->lpcSize;i++)
751
7.64k
         st->old_qlsp[i] = qlsp[i];
752
956
   }
753
754
3.18k
   ALLOC(mem, st->lpcSize, spx_mem_t);
755
3.18k
   ALLOC(syn_resp, st->subframeSize, spx_word16_t);
756
3.18k
   ALLOC(innov, st->subframeSize, spx_sig_t);
757
3.18k
   ALLOC(target, st->subframeSize, spx_word16_t);
758
759
15.9k
   for (sub=0;sub<st->nbSubframes;sub++)
760
12.7k
   {
761
12.7k
      VARDECL(spx_word16_t *exc);
762
12.7k
      VARDECL(spx_word16_t *res);
763
12.7k
      VARDECL(spx_word16_t *sw);
764
12.7k
      spx_word16_t *sp;
765
12.7k
      spx_word16_t filter_ratio;     /*Q7*/
766
12.7k
      int offset;
767
12.7k
      spx_word32_t rl, rh;           /*Q13*/
768
12.7k
      spx_word16_t eh=0;
769
770
12.7k
      offset = st->subframeSize*sub;
771
12.7k
      sp=high+offset;
772
12.7k
      ALLOC(exc, st->subframeSize, spx_word16_t);
773
12.7k
      ALLOC(res, st->subframeSize, spx_word16_t);
774
12.7k
      ALLOC(sw, st->subframeSize, spx_word16_t);
775
776
      /* LSP interpolation (quantized and unquantized) */
777
12.7k
      lsp_interpolate(st->old_lsp, lsp, interp_lsp, st->lpcSize, sub, st->nbSubframes, LSP_MARGIN);
778
12.7k
      lsp_interpolate(st->old_qlsp, qlsp, interp_qlsp, st->lpcSize, sub, st->nbSubframes, LSP_MARGIN);
779
780
12.7k
      lsp_to_lpc(interp_lsp, interp_lpc, st->lpcSize,stack);
781
12.7k
      lsp_to_lpc(interp_qlsp, st->interp_qlpc, st->lpcSize, stack);
782
783
12.7k
      bw_lpc(st->gamma1, interp_lpc, bw_lpc1, st->lpcSize);
784
12.7k
      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.7k
      st->pi_gain[sub]=LPC_SCALING;
789
12.7k
      rh = LPC_SCALING;
790
63.6k
      for (i=0;i<st->lpcSize;i+=2)
791
50.8k
      {
792
50.8k
         rh += st->interp_qlpc[i+1] - st->interp_qlpc[i];
793
50.8k
         st->pi_gain[sub] += st->interp_qlpc[i] + st->interp_qlpc[i+1];
794
50.8k
      }
795
796
12.7k
      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.7k
      filter_ratio=(rl+.01)/(rh+.01);
801
12.7k
#endif
802
803
      /* Compute "real excitation" */
804
12.7k
      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.7k
      eh = compute_rms16(exc, st->subframeSize);
808
809
12.7k
      if (!SUBMODE(innovation_quant)) {/* 1 for spectral folding excitation, 0 for stochastic */
810
9.76k
         spx_word32_t g;   /*Q7*/
811
9.76k
         spx_word16_t el;  /*Q0*/
812
9.76k
         el = low_innov_rms[sub];
813
814
         /* Gain to use if we want to use the low-band excitation for high-band */
815
9.76k
         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.76k
         {
836
9.76k
            int quant = scal_quant(g, fold_quant_bound, 32);
837
            /*speex_warning_int("tata", quant);*/
838
9.76k
            if (quant<0)
839
0
               quant=0;
840
9.76k
            if (quant>31)
841
0
               quant=31;
842
9.76k
            speex_bits_pack(bits, quant, 5);
843
9.76k
         }
844
9.76k
         if (st->innov_rms_save)
845
2.38k
         {
846
2.38k
            st->innov_rms_save[sub] = eh;
847
2.38k
         }
848
9.76k
         st->exc_rms[sub] = eh;
849
9.76k
      } else {
850
2.96k
         spx_word16_t gc;       /*Q7*/
851
2.96k
         spx_word32_t scale;    /*Q14*/
852
2.96k
         spx_word16_t el;       /*Q0*/
853
2.96k
         el = low_exc_rms[sub]; /*Q0*/
854
855
2.96k
         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.96k
         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.96k
         {
862
2.96k
            int qgc = scal_quant(gc, gc_quant_bound, 16);
863
2.96k
            speex_bits_pack(bits, qgc, 4);
864
2.96k
            gc = MULT16_16_Q15(QCONST16(0.87360,15),gc_quant_bound[qgc]);
865
2.96k
         }
866
2.96k
         if (st->subframeSize==80)
867
0
            gc = MULT16_16_P14(QCONST16(1.4142f,14), gc);
868
869
2.96k
         scale = SHL32(MULT16_16(PDIV32_16(SHL32(EXTEND32(gc),SIG_SHIFT-6),MAX16(EPSILON,filter_ratio)),(1+el)),6);
870
871
2.96k
         compute_impulse_response(st->interp_qlpc, bw_lpc1, bw_lpc2, syn_resp, st->subframeSize, st->lpcSize, stack);
872
873
874
         /* Reset excitation */
875
121k
         for (i=0;i<st->subframeSize;i++)
876
118k
            res[i]=VERY_SMALL;
877
878
         /* Compute zero response (ringing) of A(z/g1) / ( A(z/g2) * Aq(z) ) */
879
26.6k
         for (i=0;i<st->lpcSize;i++)
880
23.6k
            mem[i]=st->mem_sp[i];
881
2.96k
         iir_mem16(res, st->interp_qlpc, res, st->subframeSize, st->lpcSize, mem, stack);
882
883
26.6k
         for (i=0;i<st->lpcSize;i++)
884
23.6k
            mem[i]=st->mem_sw[i];
885
2.96k
         filter_mem16(res, bw_lpc1, bw_lpc2, res, st->subframeSize, st->lpcSize, mem, stack);
886
887
         /* Compute weighted signal */
888
26.6k
         for (i=0;i<st->lpcSize;i++)
889
23.6k
            mem[i]=st->mem_sw[i];
890
2.96k
         filter_mem16(sp, bw_lpc1, bw_lpc2, sw, st->subframeSize, st->lpcSize, mem, stack);
891
892
         /* Compute target signal */
893
121k
         for (i=0;i<st->subframeSize;i++)
894
118k
            target[i]=SUB16(sw[i],res[i]);
895
896
2.96k
         signal_div(target, target, scale, st->subframeSize);
897
898
         /* Reset excitation */
899
2.96k
         SPEEX_MEMSET(innov, 0, st->subframeSize);
900
901
         /*print_vec(target, st->subframeSize, "\ntarget");*/
902
2.96k
         SUBMODE(innovation_quant)(target, st->interp_qlpc, bw_lpc1, bw_lpc2,
903
2.96k
                                   SUBMODE(innovation_params), st->lpcSize, st->subframeSize,
904
2.96k
                                   innov, syn_resp, bits, stack, st->complexity, SUBMODE(double_codebook));
905
         /*print_vec(target, st->subframeSize, "after");*/
906
907
2.96k
         signal_mul(innov, innov, scale, st->subframeSize);
908
909
2.96k
         if (SUBMODE(double_codebook)) {
910
884
            char *tmp_stack=stack;
911
884
            VARDECL(spx_sig_t *innov2);
912
884
            ALLOC(innov2, st->subframeSize, spx_sig_t);
913
884
            SPEEX_MEMSET(innov2, 0, st->subframeSize);
914
36.2k
            for (i=0;i<st->subframeSize;i++)
915
35.3k
               target[i]=MULT16_16_P13(QCONST16(2.5f,13), target[i]);
916
917
884
            SUBMODE(innovation_quant)(target, st->interp_qlpc, bw_lpc1, bw_lpc2,
918
884
                                      SUBMODE(innovation_params), st->lpcSize, st->subframeSize,
919
884
                                      innov2, syn_resp, bits, stack, st->complexity, 0);
920
884
            signal_mul(innov2, innov2, MULT16_32_P15(QCONST16(0.4f,15),scale), st->subframeSize);
921
922
36.2k
            for (i=0;i<st->subframeSize;i++)
923
35.3k
               innov[i] = ADD32(innov[i],innov2[i]);
924
884
            stack = tmp_stack;
925
884
         }
926
121k
         for (i=0;i<st->subframeSize;i++)
927
118k
            exc[i] = PSHR32(innov[i],SIG_SHIFT);
928
929
2.96k
         if (st->innov_rms_save)
930
1.42k
         {
931
1.42k
            st->innov_rms_save[sub] = MULT16_16_Q15(QCONST16(.70711f, 15), compute_rms(innov, st->subframeSize));
932
1.42k
         }
933
2.96k
         st->exc_rms[sub] = compute_rms16(exc, st->subframeSize);
934
935
936
2.96k
      }
937
938
939
      /*Keep the previous memory*/
940
114k
      for (i=0;i<st->lpcSize;i++)
941
101k
         mem[i]=st->mem_sp[i];
942
      /* Final signal synthesis from excitation */
943
12.7k
      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.7k
      filter_mem16(sp, bw_lpc1, bw_lpc2, sw, st->subframeSize, st->lpcSize, st->mem_sw, stack);
947
12.7k
   }
948
949
28.6k
   for (i=0;i<st->lpcSize;i++)
950
25.4k
      st->old_lsp[i] = lsp[i];
951
28.6k
   for (i=0;i<st->lpcSize;i++)
952
25.4k
      st->old_qlsp[i] = qlsp[i];
953
954
3.18k
   st->first=0;
955
956
3.18k
   return 1;
957
3.66k
}
sb_encode
Line
Count
Source
516
6.68k
{
517
6.68k
   SBEncState *st;
518
6.68k
   int i, roots, sub;
519
6.68k
   char *stack;
520
6.68k
   VARDECL(spx_mem_t *mem);
521
6.68k
   VARDECL(spx_sig_t *innov);
522
6.68k
   VARDECL(spx_word16_t *target);
523
6.68k
   VARDECL(spx_word16_t *syn_resp);
524
6.68k
   VARDECL(spx_word32_t *low_pi_gain);
525
6.68k
   spx_word16_t *low;
526
6.68k
   spx_word16_t *high;
527
6.68k
   VARDECL(spx_word16_t *low_exc_rms);
528
6.68k
   VARDECL(spx_word16_t *low_innov_rms);
529
6.68k
   const SpeexSBMode *mode;
530
6.68k
   spx_int32_t dtx;
531
6.68k
   spx_word16_t *in = (spx_word16_t*)vin;
532
6.68k
   spx_word16_t e_low=0, e_high=0;
533
6.68k
   VARDECL(spx_coef_t *lpc);
534
6.68k
   VARDECL(spx_coef_t *interp_lpc);
535
6.68k
   VARDECL(spx_coef_t *bw_lpc1);
536
6.68k
   VARDECL(spx_coef_t *bw_lpc2);
537
6.68k
   VARDECL(spx_lsp_t *lsp);
538
6.68k
   VARDECL(spx_lsp_t *qlsp);
539
6.68k
   VARDECL(spx_lsp_t *interp_lsp);
540
6.68k
   VARDECL(spx_lsp_t *interp_qlsp);
541
542
6.68k
   st = (SBEncState*)state;
543
6.68k
   stack=st->stack;
544
6.68k
   mode = (const SpeexSBMode*)(st->mode->mode);
545
6.68k
   low = in;
546
6.68k
   high = in+st->frame_size;
547
548
   /* High-band buffering / sync with low band */
549
   /* Compute the two sub-bands by filtering with QMF h0*/
550
6.68k
   qmf_decomp(in, h0, low, high, st->full_frame_size, QMF_ORDER, st->h0_mem, stack);
551
552
6.68k
#ifndef DISABLE_VBR
553
6.68k
   if (st->vbr_enabled || st->vad_enabled)
554
4.93k
   {
555
      /* Need to compute things here before the signal is trashed by the encoder */
556
      /*FIXME: Are the two signals (low, high) in sync? */
557
4.93k
      e_low = compute_rms16(low, st->frame_size);
558
4.93k
      e_high = compute_rms16(high, st->frame_size);
559
4.93k
   }
560
6.68k
#endif /* #ifndef DISABLE_VBR */
561
562
6.68k
   ALLOC(low_innov_rms, st->nbSubframes, spx_word16_t);
563
6.68k
   speex_encoder_ctl(st->st_low, SPEEX_SET_INNOVATION_SAVE, low_innov_rms);
564
   /* Encode the narrowband part*/
565
6.68k
   speex_encode_native(st->st_low, low, bits);
566
567
6.68k
   high = high - (st->windowSize-st->frame_size);
568
6.68k
   SPEEX_COPY(high, st->high, st->windowSize-st->frame_size);
569
6.68k
   SPEEX_COPY(st->high, &high[st->frame_size], st->windowSize-st->frame_size);
570
571
572
6.68k
   ALLOC(low_pi_gain, st->nbSubframes, spx_word32_t);
573
6.68k
   ALLOC(low_exc_rms, st->nbSubframes, spx_word16_t);
574
6.68k
   speex_encoder_ctl(st->st_low, SPEEX_GET_PI_GAIN, low_pi_gain);
575
6.68k
   speex_encoder_ctl(st->st_low, SPEEX_GET_EXC, low_exc_rms);
576
577
6.68k
   speex_encoder_ctl(st->st_low, SPEEX_GET_LOW_MODE, &dtx);
578
579
6.68k
   if (dtx==0)
580
313
      dtx=1;
581
6.37k
   else
582
6.37k
      dtx=0;
583
584
6.68k
   ALLOC(lpc, st->lpcSize, spx_coef_t);
585
6.68k
   ALLOC(interp_lpc, st->lpcSize, spx_coef_t);
586
6.68k
   ALLOC(bw_lpc1, st->lpcSize, spx_coef_t);
587
6.68k
   ALLOC(bw_lpc2, st->lpcSize, spx_coef_t);
588
589
6.68k
   ALLOC(lsp, st->lpcSize, spx_lsp_t);
590
6.68k
   ALLOC(qlsp, st->lpcSize, spx_lsp_t);
591
6.68k
   ALLOC(interp_lsp, st->lpcSize, spx_lsp_t);
592
6.68k
   ALLOC(interp_qlsp, st->lpcSize, spx_lsp_t);
593
594
6.68k
   {
595
6.68k
      VARDECL(spx_word16_t *autocorr);
596
6.68k
      VARDECL(spx_word16_t *w_sig);
597
6.68k
      ALLOC(autocorr, st->lpcSize+1, spx_word16_t);
598
6.68k
      ALLOC(w_sig, st->windowSize, spx_word16_t);
599
      /* Window for analysis */
600
      /* FIXME: This is a kludge */
601
6.68k
      if (st->subframeSize==80)
602
1.53k
      {
603
617k
         for (i=0;i<st->windowSize;i++)
604
615k
            w_sig[i] = EXTRACT16(SHR32(MULT16_16(high[i],st->window[i>>1]),SIG_SHIFT));
605
5.14k
      } else {
606
1.03M
         for (i=0;i<st->windowSize;i++)
607
1.02M
            w_sig[i] = EXTRACT16(SHR32(MULT16_16(high[i],st->window[i]),SIG_SHIFT));
608
5.14k
      }
609
      /* Compute auto-correlation */
610
6.68k
      _spx_autocorr(w_sig, autocorr, st->lpcSize+1, st->windowSize);
611
6.68k
      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
66.8k
      for (i=0;i<st->lpcSize+1;i++)
615
60.1k
         autocorr[i] = MULT16_16_Q14(autocorr[i],st->lagWindow[i]);
616
617
      /* Levinson-Durbin */
618
6.68k
      _spx_lpc(lpc, autocorr, st->lpcSize);
619
6.68k
   }
620
621
   /* LPC to LSPs (x-domain) transform */
622
6.68k
   roots=lpc_to_lsp (lpc, st->lpcSize, lsp, 10, LSP_DELTA1, stack);
623
6.68k
   if (roots!=st->lpcSize)
624
141
   {
625
141
      roots = lpc_to_lsp (lpc, st->lpcSize, lsp, 10, LSP_DELTA2, stack);
626
141
      if (roots!=st->lpcSize) {
627
         /*If we can't find all LSP's, do some damage control and use a flat filter*/
628
945
         for (i=0;i<st->lpcSize;i++)
629
840
         {
630
840
            lsp[i]=st->old_lsp[i];
631
840
         }
632
105
      }
633
141
   }
634
635
6.68k
#ifndef DISABLE_VBR
636
   /* VBR code */
637
6.68k
   if ((st->vbr_enabled || st->vad_enabled) && !dtx)
638
4.62k
   {
639
4.62k
      float ratio;
640
4.62k
      if (st->abr_enabled)
641
1.87k
      {
642
1.87k
         float qual_change=0;
643
1.87k
         if (st->abr_drift2 * st->abr_drift > 0)
644
1.41k
         {
645
            /* Only adapt if long-term and short-term drift are the same sign */
646
1.41k
            qual_change = -.00001*st->abr_drift/(1+st->abr_count);
647
1.41k
            if (qual_change>.1)
648
0
               qual_change=.1;
649
1.41k
            if (qual_change<-.1)
650
254
               qual_change=-.1;
651
1.41k
         }
652
1.87k
         st->vbr_quality += qual_change;
653
1.87k
         if (st->vbr_quality>10)
654
0
            st->vbr_quality=10;
655
1.87k
         if (st->vbr_quality<0)
656
1.41k
            st->vbr_quality=0;
657
1.87k
      }
658
659
660
4.62k
      ratio = 2*log((1.f+e_high)/(1.f+e_low));
661
662
4.62k
      speex_encoder_ctl(st->st_low, SPEEX_GET_RELATIVE_QUALITY, &st->relative_quality);
663
4.62k
      if (ratio<-4)
664
57
         ratio=-4;
665
4.62k
      if (ratio>2)
666
60
         ratio=2;
667
      /*if (ratio>-2)*/
668
4.62k
      if (st->vbr_enabled)
669
3.69k
      {
670
3.69k
         spx_int32_t modeid;
671
3.69k
         modeid = mode->nb_modes-1;
672
3.69k
         st->relative_quality+=1.0*(ratio+2);
673
3.69k
   if (st->relative_quality<-1)
674
3
            st->relative_quality=-1;
675
11.6k
         while (modeid)
676
11.4k
         {
677
11.4k
            int v1;
678
11.4k
            float thresh;
679
11.4k
            v1=(int)floor(st->vbr_quality);
680
11.4k
            if (v1==10)
681
448
               thresh = mode->vbr_thresh[modeid][v1];
682
10.9k
            else
683
10.9k
               thresh = (st->vbr_quality-v1)   * mode->vbr_thresh[modeid][v1+1] +
684
10.9k
                        (1+v1-st->vbr_quality) * mode->vbr_thresh[modeid][v1];
685
11.4k
            if (st->relative_quality >= thresh && st->sampling_rate*st->submodes[modeid]->bits_per_frame/st->full_frame_size <= st->vbr_max_high)
686
3.48k
               break;
687
7.93k
            modeid--;
688
7.93k
         }
689
3.69k
         speex_encoder_ctl(state, SPEEX_SET_HIGH_MODE, &modeid);
690
3.69k
         if (st->abr_enabled)
691
1.87k
         {
692
1.87k
            spx_int32_t bitrate;
693
1.87k
            speex_encoder_ctl(state, SPEEX_GET_BITRATE, &bitrate);
694
1.87k
            st->abr_drift+=(bitrate-st->abr_enabled);
695
1.87k
            st->abr_drift2 = .95*st->abr_drift2 + .05*(bitrate-st->abr_enabled);
696
1.87k
            st->abr_count += 1.0;
697
1.87k
         }
698
699
3.69k
      } else {
700
         /* VAD only */
701
924
         int modeid;
702
924
         if (st->relative_quality<2.0)
703
176
            modeid=1;
704
748
         else
705
748
            modeid=st->submodeSelect;
706
         /*speex_encoder_ctl(state, SPEEX_SET_MODE, &mode);*/
707
924
         st->submodeID=modeid;
708
709
924
      }
710
      /*fprintf (stderr, "%f %f\n", ratio, low_qual);*/
711
4.62k
   }
712
6.68k
#endif /* #ifndef DISABLE_VBR */
713
714
6.68k
   if (st->encode_submode)
715
6.68k
   {
716
6.68k
      speex_bits_pack(bits, 1, 1);
717
6.68k
      if (dtx)
718
313
         speex_bits_pack(bits, 0, SB_SUBMODE_BITS);
719
6.37k
      else
720
6.37k
         speex_bits_pack(bits, st->submodeID, SB_SUBMODE_BITS);
721
6.68k
   }
722
723
   /* If null mode (no transmission), just set a couple things to zero*/
724
6.68k
   if (dtx || st->submodes[st->submodeID] == NULL)
725
595
   {
726
120k
      for (i=0;i<st->frame_size;i++)
727
120k
         high[i]=VERY_SMALL;
728
729
5.35k
      for (i=0;i<st->lpcSize;i++)
730
4.76k
         st->mem_sw[i]=0;
731
595
      st->first=1;
732
733
      /* Final signal synthesis from excitation */
734
595
      iir_mem16(high, st->interp_qlpc, high, st->frame_size, st->lpcSize, st->mem_sp, stack);
735
736
595
      if (dtx)
737
313
         return 0;
738
282
      else
739
282
         return 1;
740
595
   }
741
742
743
   /* LSP quantization */
744
6.08k
   SUBMODE(lsp_quant)(lsp, qlsp, st->lpcSize, bits);
745
746
6.08k
   if (st->first)
747
1.69k
   {
748
15.2k
      for (i=0;i<st->lpcSize;i++)
749
13.5k
         st->old_lsp[i] = lsp[i];
750
15.2k
      for (i=0;i<st->lpcSize;i++)
751
13.5k
         st->old_qlsp[i] = qlsp[i];
752
1.69k
   }
753
754
6.08k
   ALLOC(mem, st->lpcSize, spx_mem_t);
755
6.08k
   ALLOC(syn_resp, st->subframeSize, spx_word16_t);
756
6.08k
   ALLOC(innov, st->subframeSize, spx_sig_t);
757
6.08k
   ALLOC(target, st->subframeSize, spx_word16_t);
758
759
30.4k
   for (sub=0;sub<st->nbSubframes;sub++)
760
24.3k
   {
761
24.3k
      VARDECL(spx_word16_t *exc);
762
24.3k
      VARDECL(spx_word16_t *res);
763
24.3k
      VARDECL(spx_word16_t *sw);
764
24.3k
      spx_word16_t *sp;
765
24.3k
      spx_word16_t filter_ratio;     /*Q7*/
766
24.3k
      int offset;
767
24.3k
      spx_word32_t rl, rh;           /*Q13*/
768
24.3k
      spx_word16_t eh=0;
769
770
24.3k
      offset = st->subframeSize*sub;
771
24.3k
      sp=high+offset;
772
24.3k
      ALLOC(exc, st->subframeSize, spx_word16_t);
773
24.3k
      ALLOC(res, st->subframeSize, spx_word16_t);
774
24.3k
      ALLOC(sw, st->subframeSize, spx_word16_t);
775
776
      /* LSP interpolation (quantized and unquantized) */
777
24.3k
      lsp_interpolate(st->old_lsp, lsp, interp_lsp, st->lpcSize, sub, st->nbSubframes, LSP_MARGIN);
778
24.3k
      lsp_interpolate(st->old_qlsp, qlsp, interp_qlsp, st->lpcSize, sub, st->nbSubframes, LSP_MARGIN);
779
780
24.3k
      lsp_to_lpc(interp_lsp, interp_lpc, st->lpcSize,stack);
781
24.3k
      lsp_to_lpc(interp_qlsp, st->interp_qlpc, st->lpcSize, stack);
782
783
24.3k
      bw_lpc(st->gamma1, interp_lpc, bw_lpc1, st->lpcSize);
784
24.3k
      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
24.3k
      st->pi_gain[sub]=LPC_SCALING;
789
24.3k
      rh = LPC_SCALING;
790
121k
      for (i=0;i<st->lpcSize;i+=2)
791
97.4k
      {
792
97.4k
         rh += st->interp_qlpc[i+1] - st->interp_qlpc[i];
793
97.4k
         st->pi_gain[sub] += st->interp_qlpc[i] + st->interp_qlpc[i+1];
794
97.4k
      }
795
796
24.3k
      rl = low_pi_gain[sub];
797
24.3k
#ifdef FIXED_POINT
798
24.3k
      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
24.3k
      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
24.3k
      eh = compute_rms16(exc, st->subframeSize);
808
809
24.3k
      if (!SUBMODE(innovation_quant)) {/* 1 for spectral folding excitation, 0 for stochastic */
810
18.4k
         spx_word32_t g;   /*Q7*/
811
18.4k
         spx_word16_t el;  /*Q0*/
812
18.4k
         el = low_innov_rms[sub];
813
814
         /* Gain to use if we want to use the low-band excitation for high-band */
815
18.4k
         g=PDIV32(MULT16_16(filter_ratio,eh),EXTEND32(ADD16(1,el)));
816
817
#if 0
818
         {
819
            char *tmp_stack=stack;
820
            float *tmp_sig;
821
            float g2;
822
            ALLOC(tmp_sig, st->subframeSize, spx_sig_t);
823
            for (i=0;i<st->lpcSize;i++)
824
               mem[i]=st->mem_sp[i];
825
            iir_mem2(st->low_innov+offset, st->interp_qlpc, tmp_sig, st->subframeSize, st->lpcSize, mem);
826
            g2 = compute_rms(sp, st->subframeSize)/(.01+compute_rms(tmp_sig, st->subframeSize));
827
            /*fprintf (stderr, "gains: %f %f\n", g, g2);*/
828
            g = g2;
829
            stack = tmp_stack;
830
         }
831
#endif
832
833
         /*print_vec(&g, 1, "gain factor");*/
834
         /* Gain quantization */
835
18.4k
         {
836
18.4k
            int quant = scal_quant(g, fold_quant_bound, 32);
837
            /*speex_warning_int("tata", quant);*/
838
18.4k
            if (quant<0)
839
0
               quant=0;
840
18.4k
            if (quant>31)
841
0
               quant=31;
842
18.4k
            speex_bits_pack(bits, quant, 5);
843
18.4k
         }
844
18.4k
         if (st->innov_rms_save)
845
3.42k
         {
846
3.42k
            st->innov_rms_save[sub] = eh;
847
3.42k
         }
848
18.4k
         st->exc_rms[sub] = eh;
849
18.4k
      } else {
850
5.94k
         spx_word16_t gc;       /*Q7*/
851
5.94k
         spx_word32_t scale;    /*Q14*/
852
5.94k
         spx_word16_t el;       /*Q0*/
853
5.94k
         el = low_exc_rms[sub]; /*Q0*/
854
855
5.94k
         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.94k
         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.94k
         {
862
5.94k
            int qgc = scal_quant(gc, gc_quant_bound, 16);
863
5.94k
            speex_bits_pack(bits, qgc, 4);
864
5.94k
            gc = MULT16_16_Q15(QCONST16(0.87360,15),gc_quant_bound[qgc]);
865
5.94k
         }
866
5.94k
         if (st->subframeSize==80)
867
0
            gc = MULT16_16_P14(QCONST16(1.4142f,14), gc);
868
869
5.94k
         scale = SHL32(MULT16_16(PDIV32_16(SHL32(EXTEND32(gc),SIG_SHIFT-6),MAX16(EPSILON,filter_ratio)),(1+el)),6);
870
871
5.94k
         compute_impulse_response(st->interp_qlpc, bw_lpc1, bw_lpc2, syn_resp, st->subframeSize, st->lpcSize, stack);
872
873
874
         /* Reset excitation */
875
243k
         for (i=0;i<st->subframeSize;i++)
876
237k
            res[i]=VERY_SMALL;
877
878
         /* Compute zero response (ringing) of A(z/g1) / ( A(z/g2) * Aq(z) ) */
879
53.5k
         for (i=0;i<st->lpcSize;i++)
880
47.5k
            mem[i]=st->mem_sp[i];
881
5.94k
         iir_mem16(res, st->interp_qlpc, res, st->subframeSize, st->lpcSize, mem, stack);
882
883
53.5k
         for (i=0;i<st->lpcSize;i++)
884
47.5k
            mem[i]=st->mem_sw[i];
885
5.94k
         filter_mem16(res, bw_lpc1, bw_lpc2, res, st->subframeSize, st->lpcSize, mem, stack);
886
887
         /* Compute weighted signal */
888
53.5k
         for (i=0;i<st->lpcSize;i++)
889
47.5k
            mem[i]=st->mem_sw[i];
890
5.94k
         filter_mem16(sp, bw_lpc1, bw_lpc2, sw, st->subframeSize, st->lpcSize, mem, stack);
891
892
         /* Compute target signal */
893
243k
         for (i=0;i<st->subframeSize;i++)
894
237k
            target[i]=SUB16(sw[i],res[i]);
895
896
5.94k
         signal_div(target, target, scale, st->subframeSize);
897
898
         /* Reset excitation */
899
5.94k
         SPEEX_MEMSET(innov, 0, st->subframeSize);
900
901
         /*print_vec(target, st->subframeSize, "\ntarget");*/
902
5.94k
         SUBMODE(innovation_quant)(target, st->interp_qlpc, bw_lpc1, bw_lpc2,
903
5.94k
                                   SUBMODE(innovation_params), st->lpcSize, st->subframeSize,
904
5.94k
                                   innov, syn_resp, bits, stack, st->complexity, SUBMODE(double_codebook));
905
         /*print_vec(target, st->subframeSize, "after");*/
906
907
5.94k
         signal_mul(innov, innov, scale, st->subframeSize);
908
909
5.94k
         if (SUBMODE(double_codebook)) {
910
1.99k
            char *tmp_stack=stack;
911
1.99k
            VARDECL(spx_sig_t *innov2);
912
1.99k
            ALLOC(innov2, st->subframeSize, spx_sig_t);
913
1.99k
            SPEEX_MEMSET(innov2, 0, st->subframeSize);
914
81.8k
            for (i=0;i<st->subframeSize;i++)
915
79.8k
               target[i]=MULT16_16_P13(QCONST16(2.5f,13), target[i]);
916
917
1.99k
            SUBMODE(innovation_quant)(target, st->interp_qlpc, bw_lpc1, bw_lpc2,
918
1.99k
                                      SUBMODE(innovation_params), st->lpcSize, st->subframeSize,
919
1.99k
                                      innov2, syn_resp, bits, stack, st->complexity, 0);
920
1.99k
            signal_mul(innov2, innov2, MULT16_32_P15(QCONST16(0.4f,15),scale), st->subframeSize);
921
922
81.8k
            for (i=0;i<st->subframeSize;i++)
923
79.8k
               innov[i] = ADD32(innov[i],innov2[i]);
924
1.99k
            stack = tmp_stack;
925
1.99k
         }
926
243k
         for (i=0;i<st->subframeSize;i++)
927
237k
            exc[i] = PSHR32(innov[i],SIG_SHIFT);
928
929
5.94k
         if (st->innov_rms_save)
930
2.51k
         {
931
2.51k
            st->innov_rms_save[sub] = MULT16_16_Q15(QCONST16(.70711f, 15), compute_rms(innov, st->subframeSize));
932
2.51k
         }
933
5.94k
         st->exc_rms[sub] = compute_rms16(exc, st->subframeSize);
934
935
936
5.94k
      }
937
938
939
      /*Keep the previous memory*/
940
219k
      for (i=0;i<st->lpcSize;i++)
941
194k
         mem[i]=st->mem_sp[i];
942
      /* Final signal synthesis from excitation */
943
24.3k
      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
24.3k
      filter_mem16(sp, bw_lpc1, bw_lpc2, sw, st->subframeSize, st->lpcSize, st->mem_sw, stack);
947
24.3k
   }
948
949
54.7k
   for (i=0;i<st->lpcSize;i++)
950
48.7k
      st->old_lsp[i] = lsp[i];
951
54.7k
   for (i=0;i<st->lpcSize;i++)
952
48.7k
      st->old_qlsp[i] = qlsp[i];
953
954
6.08k
   st->first=0;
955
956
6.08k
   return 1;
957
6.68k
}
958
959
#endif /* DISABLE_ENCODER */
960
961
962
#ifndef DISABLE_DECODER
963
void *sb_decoder_init(const SpeexMode *m)
964
4.91k
{
965
4.91k
   spx_int32_t tmp;
966
4.91k
   SBDecState *st;
967
4.91k
   const SpeexSBMode *mode;
968
4.91k
   st = (SBDecState*)speex_alloc(sizeof(SBDecState));
969
4.91k
   if (!st)
970
0
      return NULL;
971
4.91k
   st->mode = m;
972
4.91k
   mode=(const SpeexSBMode*)m->mode;
973
4.91k
   st->encode_submode = 1;
974
975
4.91k
   st->st_low = speex_decoder_init(mode->nb_mode);
976
4.91k
#if defined(VAR_ARRAYS) || defined (USE_ALLOCA)
977
4.91k
   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.91k
   st->full_frame_size = 2*mode->frameSize;
984
4.91k
   st->frame_size = mode->frameSize;
985
4.91k
   st->subframeSize = mode->subframeSize;
986
4.91k
   st->nbSubframes = mode->frameSize/mode->subframeSize;
987
4.91k
   st->lpcSize=mode->lpcSize;
988
4.91k
   speex_decoder_ctl(st->st_low, SPEEX_GET_SAMPLING_RATE, &st->sampling_rate);
989
4.91k
   st->sampling_rate*=2;
990
4.91k
   tmp=1;
991
4.91k
   speex_decoder_ctl(st->st_low, SPEEX_SET_WIDEBAND, &tmp);
992
993
4.91k
   st->submodes=mode->submodes;
994
4.91k
   st->submodeID=mode->defaultSubmode;
995
996
4.91k
   st->first=1;
997
998
4.91k
   st->g0_mem = (spx_word16_t*)speex_alloc((QMF_ORDER)*sizeof(spx_word16_t));
999
4.91k
   st->g1_mem = (spx_word16_t*)speex_alloc((QMF_ORDER)*sizeof(spx_word16_t));
1000
1001
4.91k
   st->excBuf = (spx_word16_t*)speex_alloc((st->subframeSize)*sizeof(spx_word16_t));
1002
1003
4.91k
   st->old_qlsp = (spx_lsp_t*)speex_alloc((st->lpcSize)*sizeof(spx_lsp_t));
1004
4.91k
   st->interp_qlpc = (spx_coef_t*)speex_alloc(st->lpcSize*sizeof(spx_coef_t));
1005
1006
4.91k
   st->pi_gain = (spx_word32_t*)speex_alloc((st->nbSubframes)*sizeof(spx_word32_t));
1007
4.91k
   st->exc_rms = (spx_word16_t*)speex_alloc((st->nbSubframes)*sizeof(spx_word16_t));
1008
4.91k
   st->mem_sp = (spx_mem_t*)speex_alloc((2*st->lpcSize)*sizeof(spx_mem_t));
1009
1010
4.91k
   st->innov_save = NULL;
1011
1012
1013
4.91k
   st->lpc_enh_enabled=0;
1014
4.91k
   st->seed = 1000;
1015
1016
#ifdef ENABLE_VALGRIND
1017
   VALGRIND_MAKE_MEM_DEFINED(st, (st->stack-(char*)st));
1018
#endif
1019
4.91k
   return st;
1020
4.91k
}
1021
1022
void sb_decoder_destroy(void *state)
1023
4.91k
{
1024
4.91k
   SBDecState *st;
1025
4.91k
   st = (SBDecState*)state;
1026
4.91k
   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.91k
   speex_free(st->g0_mem);
1032
4.91k
   speex_free(st->g1_mem);
1033
4.91k
   speex_free(st->excBuf);
1034
4.91k
   speex_free(st->old_qlsp);
1035
4.91k
   speex_free(st->interp_qlpc);
1036
4.91k
   speex_free(st->pi_gain);
1037
4.91k
   speex_free(st->exc_rms);
1038
4.91k
   speex_free(st->mem_sp);
1039
1040
4.91k
   speex_free(state);
1041
4.91k
}
1042
1043
1044
1045
int sb_decoder_ctl(void *state, int request, void *ptr)
1046
121k
{
1047
121k
   SBDecState *st;
1048
121k
   st=(SBDecState*)state;
1049
121k
   switch(request)
1050
121k
   {
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
26.9k
   case SPEEX_GET_FRAME_SIZE:
1061
26.9k
      (*(spx_int32_t*)ptr) = st->full_frame_size;
1062
26.9k
      break;
1063
4.91k
   case SPEEX_SET_ENH:
1064
4.91k
      speex_decoder_ctl(st->st_low, request, ptr);
1065
4.91k
      st->lpc_enh_enabled = *((spx_int32_t*)ptr);
1066
4.91k
      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.91k
   case SPEEX_SET_SAMPLING_RATE:
1092
4.91k
      {
1093
4.91k
         spx_int32_t tmp=(*(spx_int32_t*)ptr);
1094
4.91k
         st->sampling_rate = tmp;
1095
4.91k
         tmp>>=1;
1096
4.91k
         speex_decoder_ctl(st->st_low, SPEEX_SET_SAMPLING_RATE, &tmp);
1097
4.91k
      }
1098
4.91k
      break;
1099
1.98k
   case SPEEX_GET_SAMPLING_RATE:
1100
1.98k
      (*(spx_int32_t*)ptr)=st->sampling_rate;
1101
1.98k
      break;
1102
2.53k
   case SPEEX_SET_HANDLER:
1103
2.53k
      speex_decoder_ctl(st->st_low, SPEEX_SET_HANDLER, ptr);
1104
2.53k
      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.73k
   case SPEEX_GET_LOOKAHEAD:
1126
4.73k
      speex_decoder_ctl(st->st_low, SPEEX_GET_LOOKAHEAD, ptr);
1127
4.73k
      (*(spx_int32_t*)ptr) = 2*(*(spx_int32_t*)ptr);
1128
4.73k
      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
1.77k
   case SPEEX_GET_PI_GAIN:
1139
1.77k
      {
1140
1.77k
         int i;
1141
1.77k
         spx_word32_t *g = (spx_word32_t*)ptr;
1142
8.89k
         for (i=0;i<st->nbSubframes;i++)
1143
7.11k
            g[i]=st->pi_gain[i];
1144
1.77k
      }
1145
1.77k
      break;
1146
1.77k
   case SPEEX_GET_EXC:
1147
1.77k
      {
1148
1.77k
         int i;
1149
8.89k
         for (i=0;i<st->nbSubframes;i++)
1150
7.11k
            ((spx_word16_t*)ptr)[i] = st->exc_rms[i];
1151
1.77k
      }
1152
1.77k
      break;
1153
34.8k
   case SPEEX_GET_DTX_STATUS:
1154
34.8k
      speex_decoder_ctl(st->st_low, SPEEX_GET_DTX_STATUS, ptr);
1155
34.8k
      break;
1156
34.8k
   case SPEEX_SET_INNOVATION_SAVE:
1157
34.8k
      st->innov_save = (spx_word16_t*)ptr;
1158
34.8k
      break;
1159
1.98k
   case SPEEX_SET_WIDEBAND:
1160
1.98k
      speex_decoder_ctl(st->st_low, SPEEX_SET_WIDEBAND, ptr);
1161
1.98k
      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
121k
   }
1169
121k
   return 0;
1170
121k
}
1171
1172
1173
static void sb_decode_lost(SBDecState *st, spx_word16_t *out, int dtx, char *stack)
1174
3.06k
{
1175
3.06k
   int i;
1176
3.06k
   int saved_modeid=0;
1177
1178
3.06k
   if (dtx)
1179
3.06k
   {
1180
3.06k
      saved_modeid=st->submodeID;
1181
3.06k
      st->submodeID=1;
1182
3.06k
   } else {
1183
0
      bw_lpc(QCONST16(0.99f,15), st->interp_qlpc, st->interp_qlpc, st->lpcSize);
1184
0
   }
1185
1186
3.06k
   st->first=1;
1187
1188
1189
   /* Final signal synthesis from excitation */
1190
3.06k
   if (!dtx)
1191
0
   {
1192
0
      st->last_ener =  MULT16_16_Q15(QCONST16(.9f,15),st->last_ener);
1193
0
   }
1194
774k
   for (i=0;i<st->frame_size;i++)
1195
771k
      out[i+st->frame_size] = speex_rand(st->last_ener, &st->seed);
1196
1197
3.06k
   iir_mem16(out+st->frame_size, st->interp_qlpc, out+st->frame_size, st->frame_size, st->lpcSize,
1198
3.06k
            st->mem_sp, stack);
1199
1200
1201
   /* Reconstruct the original */
1202
3.06k
   qmf_synth(out, out+st->frame_size, h0, out, st->full_frame_size, QMF_ORDER, st->g0_mem, st->g1_mem, stack);
1203
3.06k
   if (dtx)
1204
3.06k
   {
1205
3.06k
      st->submodeID=saved_modeid;
1206
3.06k
   }
1207
1208
3.06k
   return;
1209
3.06k
}
sb_celp.c:sb_decode_lost
Line
Count
Source
1174
1.53k
{
1175
1.53k
   int i;
1176
1.53k
   int saved_modeid=0;
1177
1178
1.53k
   if (dtx)
1179
1.53k
   {
1180
1.53k
      saved_modeid=st->submodeID;
1181
1.53k
      st->submodeID=1;
1182
1.53k
   } else {
1183
0
      bw_lpc(QCONST16(0.99f,15), st->interp_qlpc, st->interp_qlpc, st->lpcSize);
1184
0
   }
1185
1186
1.53k
   st->first=1;
1187
1188
1189
   /* Final signal synthesis from excitation */
1190
1.53k
   if (!dtx)
1191
0
   {
1192
0
      st->last_ener =  MULT16_16_Q15(QCONST16(.9f,15),st->last_ener);
1193
0
   }
1194
387k
   for (i=0;i<st->frame_size;i++)
1195
385k
      out[i+st->frame_size] = speex_rand(st->last_ener, &st->seed);
1196
1197
1.53k
   iir_mem16(out+st->frame_size, st->interp_qlpc, out+st->frame_size, st->frame_size, st->lpcSize,
1198
1.53k
            st->mem_sp, stack);
1199
1200
1201
   /* Reconstruct the original */
1202
1.53k
   qmf_synth(out, out+st->frame_size, h0, out, st->full_frame_size, QMF_ORDER, st->g0_mem, st->g1_mem, stack);
1203
1.53k
   if (dtx)
1204
1.53k
   {
1205
1.53k
      st->submodeID=saved_modeid;
1206
1.53k
   }
1207
1208
1.53k
   return;
1209
1.53k
}
sb_celp.c:sb_decode_lost
Line
Count
Source
1174
1.53k
{
1175
1.53k
   int i;
1176
1.53k
   int saved_modeid=0;
1177
1178
1.53k
   if (dtx)
1179
1.53k
   {
1180
1.53k
      saved_modeid=st->submodeID;
1181
1.53k
      st->submodeID=1;
1182
1.53k
   } else {
1183
0
      bw_lpc(QCONST16(0.99f,15), st->interp_qlpc, st->interp_qlpc, st->lpcSize);
1184
0
   }
1185
1186
1.53k
   st->first=1;
1187
1188
1189
   /* Final signal synthesis from excitation */
1190
1.53k
   if (!dtx)
1191
0
   {
1192
0
      st->last_ener =  MULT16_16_Q15(QCONST16(.9f,15),st->last_ener);
1193
0
   }
1194
387k
   for (i=0;i<st->frame_size;i++)
1195
385k
      out[i+st->frame_size] = speex_rand(st->last_ener, &st->seed);
1196
1197
1.53k
   iir_mem16(out+st->frame_size, st->interp_qlpc, out+st->frame_size, st->frame_size, st->lpcSize,
1198
1.53k
            st->mem_sp, stack);
1199
1200
1201
   /* Reconstruct the original */
1202
1.53k
   qmf_synth(out, out+st->frame_size, h0, out, st->full_frame_size, QMF_ORDER, st->g0_mem, st->g1_mem, stack);
1203
1.53k
   if (dtx)
1204
1.53k
   {
1205
1.53k
      st->submodeID=saved_modeid;
1206
1.53k
   }
1207
1208
1.53k
   return;
1209
1.53k
}
1210
1211
int sb_decode(void *state, SpeexBits *bits, void *vout)
1212
84.5k
{
1213
84.5k
   int i, sub;
1214
84.5k
   SBDecState *st;
1215
84.5k
   int wideband;
1216
84.5k
   int ret;
1217
84.5k
   char *stack;
1218
84.5k
   VARDECL(spx_word32_t *low_pi_gain);
1219
84.5k
   VARDECL(spx_word16_t *low_exc_rms);
1220
84.5k
   VARDECL(spx_coef_t *ak);
1221
84.5k
   VARDECL(spx_lsp_t *qlsp);
1222
84.5k
   VARDECL(spx_lsp_t *interp_qlsp);
1223
84.5k
   spx_int32_t dtx;
1224
84.5k
   const SpeexSBMode *mode;
1225
84.5k
   spx_word16_t *out = (spx_word16_t*)vout;
1226
84.5k
   spx_word16_t *low_innov_alias;
1227
84.5k
   spx_word32_t exc_ener_sum = 0;
1228
1229
84.5k
   st = (SBDecState*)state;
1230
84.5k
   stack=st->stack;
1231
84.5k
   mode = (const SpeexSBMode*)(st->mode->mode);
1232
1233
84.5k
   low_innov_alias = out+st->frame_size;
1234
84.5k
   speex_decoder_ctl(st->st_low, SPEEX_SET_INNOVATION_SAVE, low_innov_alias);
1235
   /* Decode the low-band */
1236
84.5k
   ret = speex_decode_native(st->st_low, bits, out);
1237
1238
84.5k
   speex_decoder_ctl(st->st_low, SPEEX_GET_DTX_STATUS, &dtx);
1239
1240
   /* If error decoding the narrowband part, propagate error */
1241
84.5k
   if (ret!=0)
1242
27.9k
   {
1243
27.9k
      return ret;
1244
27.9k
   }
1245
1246
56.6k
   if (!bits)
1247
0
   {
1248
0
      sb_decode_lost(st, out, dtx, stack);
1249
0
      return 0;
1250
0
   }
1251
1252
56.6k
   if (st->encode_submode)
1253
56.6k
   {
1254
1255
      /*Check "wideband bit"*/
1256
56.6k
      if (speex_bits_remaining(bits)>0)
1257
30.0k
         wideband = speex_bits_peek(bits);
1258
26.5k
      else
1259
26.5k
         wideband = 0;
1260
56.6k
      if (wideband)
1261
9.47k
      {
1262
         /*Regular wideband frame, read the submode*/
1263
9.47k
         wideband = speex_bits_unpack_unsigned(bits, 1);
1264
9.47k
         st->submodeID = speex_bits_unpack_unsigned(bits, SB_SUBMODE_BITS);
1265
9.47k
      } else
1266
47.1k
      {
1267
         /*Was a narrowband frame, set "null submode"*/
1268
47.1k
         st->submodeID = 0;
1269
47.1k
      }
1270
56.6k
      if (st->submodeID != 0 && st->submodes[st->submodeID] == NULL)
1271
838
      {
1272
838
         speex_notify("Invalid mode encountered. The stream is corrupted.");
1273
838
         return -2;
1274
838
      }
1275
56.6k
   }
1276
1277
   /* If null mode (no transmission), just set a couple things to zero*/
1278
55.7k
   if (st->submodes[st->submodeID] == NULL)
1279
49.3k
   {
1280
49.3k
      if (st->innov_save)
1281
18.9k
        SPEEX_MEMSET(st->innov_save, 0, st->full_frame_size);
1282
1283
49.3k
      if (dtx)
1284
1.53k
      {
1285
1.53k
         sb_decode_lost(st, out, 1, stack);
1286
1.53k
         return 0;
1287
1.53k
      }
1288
1289
10.9M
      for (i=0;i<st->frame_size;i++)
1290
10.8M
         out[st->frame_size+i]=VERY_SMALL;
1291
1292
47.8k
      st->first=1;
1293
1294
      /* Final signal synthesis from excitation */
1295
47.8k
      iir_mem16(out+st->frame_size, st->interp_qlpc, out+st->frame_size, st->frame_size, st->lpcSize, st->mem_sp, stack);
1296
1297
47.8k
      qmf_synth(out, out+st->frame_size, h0, out, st->full_frame_size, QMF_ORDER, st->g0_mem, st->g1_mem, stack);
1298
1299
47.8k
      return 0;
1300
1301
49.3k
   }
1302
1303
6.42k
   ALLOC(low_pi_gain, st->nbSubframes, spx_word32_t);
1304
6.42k
   ALLOC(low_exc_rms, st->nbSubframes, spx_word16_t);
1305
6.42k
   speex_decoder_ctl(st->st_low, SPEEX_GET_PI_GAIN, low_pi_gain);
1306
6.42k
   speex_decoder_ctl(st->st_low, SPEEX_GET_EXC, low_exc_rms);
1307
1308
6.42k
   ALLOC(qlsp, st->lpcSize, spx_lsp_t);
1309
6.42k
   ALLOC(interp_qlsp, st->lpcSize, spx_lsp_t);
1310
6.42k
   SUBMODE(lsp_unquant)(qlsp, st->lpcSize, bits);
1311
1312
6.42k
   if (st->first)
1313
2.68k
   {
1314
24.1k
      for (i=0;i<st->lpcSize;i++)
1315
21.4k
         st->old_qlsp[i] = qlsp[i];
1316
2.68k
   }
1317
1318
6.42k
   ALLOC(ak, st->lpcSize, spx_coef_t);
1319
1320
32.1k
   for (sub=0;sub<st->nbSubframes;sub++)
1321
25.7k
   {
1322
25.7k
      VARDECL(spx_word32_t *exc);
1323
25.7k
      spx_word16_t *innov_save=NULL;
1324
25.7k
      spx_word16_t *sp;
1325
25.7k
      spx_word16_t filter_ratio;
1326
25.7k
      spx_word16_t el=0;
1327
25.7k
      int offset;
1328
25.7k
      spx_word32_t rl=0,rh=0;
1329
1330
25.7k
      offset = st->subframeSize*sub;
1331
25.7k
      sp=out+st->frame_size+offset;
1332
25.7k
      ALLOC(exc, st->subframeSize, spx_word32_t);
1333
      /* Pointer for saving innovation */
1334
25.7k
      if (st->innov_save)
1335
15.1k
      {
1336
15.1k
         innov_save = st->innov_save+2*offset;
1337
15.1k
         SPEEX_MEMSET(innov_save, 0, 2*st->subframeSize);
1338
15.1k
      }
1339
1340
      /* LSP interpolation */
1341
25.7k
      lsp_interpolate(st->old_qlsp, qlsp, interp_qlsp, st->lpcSize, sub, st->nbSubframes, LSP_MARGIN);
1342
1343
      /* LSP to LPC */
1344
25.7k
      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
25.7k
         st->pi_gain[sub]=LPC_SCALING;
1350
25.7k
         rh = LPC_SCALING;
1351
128k
         for (i=0;i<st->lpcSize;i+=2)
1352
102k
         {
1353
102k
            rh += ak[i+1] - ak[i];
1354
102k
            st->pi_gain[sub] += ak[i] + ak[i+1];
1355
102k
         }
1356
1357
25.7k
         rl = low_pi_gain[sub];
1358
#ifdef FIXED_POINT
1359
13.3k
         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
25.7k
      SPEEX_MEMSET(exc, 0, st->subframeSize);
1365
25.7k
      if (!SUBMODE(innovation_unquant))
1366
10.0k
      {
1367
10.0k
         spx_word32_t g;
1368
10.0k
         int quant;
1369
1370
10.0k
         quant = speex_bits_unpack_unsigned(bits, 5);
1371
10.0k
         g= spx_exp(MULT16_16(QCONST16(.125f,11),(quant-10)));
1372
1373
         /* Clamp to a minimum of epsilon to avoid division by 0 */
1374
10.0k
         g = PDIV32(g, MAX16(EPSILON,filter_ratio));
1375
1376
353k
         for (i=0;i<st->subframeSize;i+=2)
1377
343k
         {
1378
343k
            exc[i]=SHL32(MULT16_32_P15(MULT16_16_Q15(mode->folding_gain,low_innov_alias[offset+i]),SHL32(g,6)),SIG_SHIFT);
1379
343k
            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
343k
         }
1381
1382
15.6k
      } else {
1383
15.6k
         spx_word16_t gc;
1384
15.6k
         spx_word32_t scale;
1385
15.6k
         int qgc = speex_bits_unpack_unsigned(bits, 4);
1386
1387
15.6k
         el = low_exc_rms[sub];
1388
15.6k
         gc = MULT16_16_Q15(QCONST16(0.87360,15),gc_quant_bound[qgc]);
1389
1390
15.6k
         if (st->subframeSize==80)
1391
0
            gc = MULT16_16_P14(QCONST16(1.4142f,14),gc);
1392
1393
15.6k
         scale = SHL32(PDIV32(SHL32(MULT16_16(gc, el),3), MAX16(EPSILON,filter_ratio)),SIG_SHIFT-3);
1394
15.6k
         SUBMODE(innovation_unquant)(exc, SUBMODE(innovation_params), st->subframeSize,
1395
15.6k
                                     bits, stack, &st->seed);
1396
1397
15.6k
         signal_mul(exc,exc,scale,st->subframeSize);
1398
1399
15.6k
         if (SUBMODE(double_codebook)) {
1400
6.67k
            char *tmp_stack=stack;
1401
6.67k
            VARDECL(spx_sig_t *innov2);
1402
6.67k
            ALLOC(innov2, st->subframeSize, spx_sig_t);
1403
6.67k
            SPEEX_MEMSET(innov2, 0, st->subframeSize);
1404
6.67k
            SUBMODE(innovation_unquant)(innov2, SUBMODE(innovation_params), st->subframeSize,
1405
6.67k
                                        bits, stack, &st->seed);
1406
6.67k
            signal_mul(innov2, innov2, MULT16_32_P15(QCONST16(0.4f,15),scale), st->subframeSize);
1407
273k
            for (i=0;i<st->subframeSize;i++)
1408
267k
               exc[i] = ADD32(exc[i],innov2[i]);
1409
6.67k
            stack = tmp_stack;
1410
6.67k
         }
1411
1412
15.6k
      }
1413
1414
25.7k
      if (st->innov_save)
1415
15.1k
      {
1416
621k
         for (i=0;i<st->subframeSize;i++)
1417
605k
            innov_save[2*i]=EXTRACT16(PSHR32(exc[i],SIG_SHIFT));
1418
15.1k
      }
1419
1420
25.7k
      iir_mem16(st->excBuf, st->interp_qlpc, sp, st->subframeSize, st->lpcSize,
1421
25.7k
               st->mem_sp, stack);
1422
1.33M
      for (i=0;i<st->subframeSize;i++)
1423
1.31M
         st->excBuf[i]=EXTRACT16(PSHR32(exc[i],SIG_SHIFT));
1424
231k
      for (i=0;i<st->lpcSize;i++)
1425
205k
         st->interp_qlpc[i] = ak[i];
1426
25.7k
      st->exc_rms[sub] = compute_rms16(st->excBuf, st->subframeSize);
1427
25.7k
      exc_ener_sum = ADD32(exc_ener_sum, DIV32(MULT16_16(st->exc_rms[sub],st->exc_rms[sub]), st->nbSubframes));
1428
25.7k
   }
1429
6.42k
   st->last_ener = spx_sqrt(exc_ener_sum);
1430
1431
6.42k
   qmf_synth(out, out+st->frame_size, h0, out, st->full_frame_size, QMF_ORDER, st->g0_mem, st->g1_mem, stack);
1432
57.8k
   for (i=0;i<st->lpcSize;i++)
1433
51.4k
      st->old_qlsp[i] = qlsp[i];
1434
1435
6.42k
   st->first=0;
1436
1437
6.42k
   return 0;
1438
55.7k
}
sb_decode
Line
Count
Source
1212
45.9k
{
1213
45.9k
   int i, sub;
1214
45.9k
   SBDecState *st;
1215
45.9k
   int wideband;
1216
45.9k
   int ret;
1217
45.9k
   char *stack;
1218
45.9k
   VARDECL(spx_word32_t *low_pi_gain);
1219
45.9k
   VARDECL(spx_word16_t *low_exc_rms);
1220
45.9k
   VARDECL(spx_coef_t *ak);
1221
45.9k
   VARDECL(spx_lsp_t *qlsp);
1222
45.9k
   VARDECL(spx_lsp_t *interp_qlsp);
1223
45.9k
   spx_int32_t dtx;
1224
45.9k
   const SpeexSBMode *mode;
1225
45.9k
   spx_word16_t *out = (spx_word16_t*)vout;
1226
45.9k
   spx_word16_t *low_innov_alias;
1227
45.9k
   spx_word32_t exc_ener_sum = 0;
1228
1229
45.9k
   st = (SBDecState*)state;
1230
45.9k
   stack=st->stack;
1231
45.9k
   mode = (const SpeexSBMode*)(st->mode->mode);
1232
1233
45.9k
   low_innov_alias = out+st->frame_size;
1234
45.9k
   speex_decoder_ctl(st->st_low, SPEEX_SET_INNOVATION_SAVE, low_innov_alias);
1235
   /* Decode the low-band */
1236
45.9k
   ret = speex_decode_native(st->st_low, bits, out);
1237
1238
45.9k
   speex_decoder_ctl(st->st_low, SPEEX_GET_DTX_STATUS, &dtx);
1239
1240
   /* If error decoding the narrowband part, propagate error */
1241
45.9k
   if (ret!=0)
1242
17.8k
   {
1243
17.8k
      return ret;
1244
17.8k
   }
1245
1246
28.0k
   if (!bits)
1247
0
   {
1248
0
      sb_decode_lost(st, out, dtx, stack);
1249
0
      return 0;
1250
0
   }
1251
1252
28.0k
   if (st->encode_submode)
1253
28.0k
   {
1254
1255
      /*Check "wideband bit"*/
1256
28.0k
      if (speex_bits_remaining(bits)>0)
1257
15.3k
         wideband = speex_bits_peek(bits);
1258
12.7k
      else
1259
12.7k
         wideband = 0;
1260
28.0k
      if (wideband)
1261
4.79k
      {
1262
         /*Regular wideband frame, read the submode*/
1263
4.79k
         wideband = speex_bits_unpack_unsigned(bits, 1);
1264
4.79k
         st->submodeID = speex_bits_unpack_unsigned(bits, SB_SUBMODE_BITS);
1265
4.79k
      } else
1266
23.2k
      {
1267
         /*Was a narrowband frame, set "null submode"*/
1268
23.2k
         st->submodeID = 0;
1269
23.2k
      }
1270
28.0k
      if (st->submodeID != 0 && st->submodes[st->submodeID] == NULL)
1271
324
      {
1272
324
         speex_notify("Invalid mode encountered. The stream is corrupted.");
1273
324
         return -2;
1274
324
      }
1275
28.0k
   }
1276
1277
   /* If null mode (no transmission), just set a couple things to zero*/
1278
27.7k
   if (st->submodes[st->submodeID] == NULL)
1279
24.6k
   {
1280
24.6k
      if (st->innov_save)
1281
10.2k
        SPEEX_MEMSET(st->innov_save, 0, st->full_frame_size);
1282
1283
24.6k
      if (dtx)
1284
831
      {
1285
831
         sb_decode_lost(st, out, 1, stack);
1286
831
         return 0;
1287
831
      }
1288
1289
5.49M
      for (i=0;i<st->frame_size;i++)
1290
5.47M
         out[st->frame_size+i]=VERY_SMALL;
1291
1292
23.7k
      st->first=1;
1293
1294
      /* Final signal synthesis from excitation */
1295
23.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
23.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
23.7k
      return 0;
1300
1301
24.6k
   }
1302
1303
3.09k
   ALLOC(low_pi_gain, st->nbSubframes, spx_word32_t);
1304
3.09k
   ALLOC(low_exc_rms, st->nbSubframes, spx_word16_t);
1305
3.09k
   speex_decoder_ctl(st->st_low, SPEEX_GET_PI_GAIN, low_pi_gain);
1306
3.09k
   speex_decoder_ctl(st->st_low, SPEEX_GET_EXC, low_exc_rms);
1307
1308
3.09k
   ALLOC(qlsp, st->lpcSize, spx_lsp_t);
1309
3.09k
   ALLOC(interp_qlsp, st->lpcSize, spx_lsp_t);
1310
3.09k
   SUBMODE(lsp_unquant)(qlsp, st->lpcSize, bits);
1311
1312
3.09k
   if (st->first)
1313
1.12k
   {
1314
10.1k
      for (i=0;i<st->lpcSize;i++)
1315
9.00k
         st->old_qlsp[i] = qlsp[i];
1316
1.12k
   }
1317
1318
3.09k
   ALLOC(ak, st->lpcSize, spx_coef_t);
1319
1320
15.4k
   for (sub=0;sub<st->nbSubframes;sub++)
1321
12.3k
   {
1322
12.3k
      VARDECL(spx_word32_t *exc);
1323
12.3k
      spx_word16_t *innov_save=NULL;
1324
12.3k
      spx_word16_t *sp;
1325
12.3k
      spx_word16_t filter_ratio;
1326
12.3k
      spx_word16_t el=0;
1327
12.3k
      int offset;
1328
12.3k
      spx_word32_t rl=0,rh=0;
1329
1330
12.3k
      offset = st->subframeSize*sub;
1331
12.3k
      sp=out+st->frame_size+offset;
1332
12.3k
      ALLOC(exc, st->subframeSize, spx_word32_t);
1333
      /* Pointer for saving innovation */
1334
12.3k
      if (st->innov_save)
1335
7.08k
      {
1336
7.08k
         innov_save = st->innov_save+2*offset;
1337
7.08k
         SPEEX_MEMSET(innov_save, 0, 2*st->subframeSize);
1338
7.08k
      }
1339
1340
      /* LSP interpolation */
1341
12.3k
      lsp_interpolate(st->old_qlsp, qlsp, interp_qlsp, st->lpcSize, sub, st->nbSubframes, LSP_MARGIN);
1342
1343
      /* LSP to LPC */
1344
12.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
12.3k
         st->pi_gain[sub]=LPC_SCALING;
1350
12.3k
         rh = LPC_SCALING;
1351
61.9k
         for (i=0;i<st->lpcSize;i+=2)
1352
49.5k
         {
1353
49.5k
            rh += ak[i+1] - ak[i];
1354
49.5k
            st->pi_gain[sub] += ak[i] + ak[i+1];
1355
49.5k
         }
1356
1357
12.3k
         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
12.3k
         filter_ratio=(rl+.01)/(rh+.01);
1362
12.3k
#endif
1363
1364
12.3k
      SPEEX_MEMSET(exc, 0, st->subframeSize);
1365
12.3k
      if (!SUBMODE(innovation_unquant))
1366
5.94k
      {
1367
5.94k
         spx_word32_t g;
1368
5.94k
         int quant;
1369
1370
5.94k
         quant = speex_bits_unpack_unsigned(bits, 5);
1371
5.94k
         g= spx_exp(MULT16_16(QCONST16(.125f,11),(quant-10)));
1372
1373
         /* Clamp to a minimum of epsilon to avoid division by 0 */
1374
5.94k
         g = PDIV32(g, MAX16(EPSILON,filter_ratio));
1375
1376
217k
         for (i=0;i<st->subframeSize;i+=2)
1377
211k
         {
1378
211k
            exc[i]=SHL32(MULT16_32_P15(MULT16_16_Q15(mode->folding_gain,low_innov_alias[offset+i]),SHL32(g,6)),SIG_SHIFT);
1379
211k
            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
211k
         }
1381
1382
6.44k
      } else {
1383
6.44k
         spx_word16_t gc;
1384
6.44k
         spx_word32_t scale;
1385
6.44k
         int qgc = speex_bits_unpack_unsigned(bits, 4);
1386
1387
6.44k
         el = low_exc_rms[sub];
1388
6.44k
         gc = MULT16_16_Q15(QCONST16(0.87360,15),gc_quant_bound[qgc]);
1389
1390
6.44k
         if (st->subframeSize==80)
1391
0
            gc = MULT16_16_P14(QCONST16(1.4142f,14),gc);
1392
1393
6.44k
         scale = SHL32(PDIV32(SHL32(MULT16_16(gc, el),3), MAX16(EPSILON,filter_ratio)),SIG_SHIFT-3);
1394
6.44k
         SUBMODE(innovation_unquant)(exc, SUBMODE(innovation_params), st->subframeSize,
1395
6.44k
                                     bits, stack, &st->seed);
1396
1397
6.44k
         signal_mul(exc,exc,scale,st->subframeSize);
1398
1399
6.44k
         if (SUBMODE(double_codebook)) {
1400
3.42k
            char *tmp_stack=stack;
1401
3.42k
            VARDECL(spx_sig_t *innov2);
1402
3.42k
            ALLOC(innov2, st->subframeSize, spx_sig_t);
1403
3.42k
            SPEEX_MEMSET(innov2, 0, st->subframeSize);
1404
3.42k
            SUBMODE(innovation_unquant)(innov2, SUBMODE(innovation_params), st->subframeSize,
1405
3.42k
                                        bits, stack, &st->seed);
1406
3.42k
            signal_mul(innov2, innov2, MULT16_32_P15(QCONST16(0.4f,15),scale), st->subframeSize);
1407
140k
            for (i=0;i<st->subframeSize;i++)
1408
137k
               exc[i] = ADD32(exc[i],innov2[i]);
1409
3.42k
            stack = tmp_stack;
1410
3.42k
         }
1411
1412
6.44k
      }
1413
1414
12.3k
      if (st->innov_save)
1415
7.08k
      {
1416
290k
         for (i=0;i<st->subframeSize;i++)
1417
283k
            innov_save[2*i]=EXTRACT16(PSHR32(exc[i],SIG_SHIFT));
1418
7.08k
      }
1419
1420
12.3k
      iir_mem16(st->excBuf, st->interp_qlpc, sp, st->subframeSize, st->lpcSize,
1421
12.3k
               st->mem_sp, stack);
1422
692k
      for (i=0;i<st->subframeSize;i++)
1423
680k
         st->excBuf[i]=EXTRACT16(PSHR32(exc[i],SIG_SHIFT));
1424
111k
      for (i=0;i<st->lpcSize;i++)
1425
99.1k
         st->interp_qlpc[i] = ak[i];
1426
12.3k
      st->exc_rms[sub] = compute_rms16(st->excBuf, st->subframeSize);
1427
12.3k
      exc_ener_sum = ADD32(exc_ener_sum, DIV32(MULT16_16(st->exc_rms[sub],st->exc_rms[sub]), st->nbSubframes));
1428
12.3k
   }
1429
3.09k
   st->last_ener = spx_sqrt(exc_ener_sum);
1430
1431
3.09k
   qmf_synth(out, out+st->frame_size, h0, out, st->full_frame_size, QMF_ORDER, st->g0_mem, st->g1_mem, stack);
1432
27.8k
   for (i=0;i<st->lpcSize;i++)
1433
24.7k
      st->old_qlsp[i] = qlsp[i];
1434
1435
3.09k
   st->first=0;
1436
1437
3.09k
   return 0;
1438
27.7k
}
sb_decode
Line
Count
Source
1212
38.6k
{
1213
38.6k
   int i, sub;
1214
38.6k
   SBDecState *st;
1215
38.6k
   int wideband;
1216
38.6k
   int ret;
1217
38.6k
   char *stack;
1218
38.6k
   VARDECL(spx_word32_t *low_pi_gain);
1219
38.6k
   VARDECL(spx_word16_t *low_exc_rms);
1220
38.6k
   VARDECL(spx_coef_t *ak);
1221
38.6k
   VARDECL(spx_lsp_t *qlsp);
1222
38.6k
   VARDECL(spx_lsp_t *interp_qlsp);
1223
38.6k
   spx_int32_t dtx;
1224
38.6k
   const SpeexSBMode *mode;
1225
38.6k
   spx_word16_t *out = (spx_word16_t*)vout;
1226
38.6k
   spx_word16_t *low_innov_alias;
1227
38.6k
   spx_word32_t exc_ener_sum = 0;
1228
1229
38.6k
   st = (SBDecState*)state;
1230
38.6k
   stack=st->stack;
1231
38.6k
   mode = (const SpeexSBMode*)(st->mode->mode);
1232
1233
38.6k
   low_innov_alias = out+st->frame_size;
1234
38.6k
   speex_decoder_ctl(st->st_low, SPEEX_SET_INNOVATION_SAVE, low_innov_alias);
1235
   /* Decode the low-band */
1236
38.6k
   ret = speex_decode_native(st->st_low, bits, out);
1237
1238
38.6k
   speex_decoder_ctl(st->st_low, SPEEX_GET_DTX_STATUS, &dtx);
1239
1240
   /* If error decoding the narrowband part, propagate error */
1241
38.6k
   if (ret!=0)
1242
10.1k
   {
1243
10.1k
      return ret;
1244
10.1k
   }
1245
1246
28.5k
   if (!bits)
1247
0
   {
1248
0
      sb_decode_lost(st, out, dtx, stack);
1249
0
      return 0;
1250
0
   }
1251
1252
28.5k
   if (st->encode_submode)
1253
28.5k
   {
1254
1255
      /*Check "wideband bit"*/
1256
28.5k
      if (speex_bits_remaining(bits)>0)
1257
14.7k
         wideband = speex_bits_peek(bits);
1258
13.8k
      else
1259
13.8k
         wideband = 0;
1260
28.5k
      if (wideband)
1261
4.68k
      {
1262
         /*Regular wideband frame, read the submode*/
1263
4.68k
         wideband = speex_bits_unpack_unsigned(bits, 1);
1264
4.68k
         st->submodeID = speex_bits_unpack_unsigned(bits, SB_SUBMODE_BITS);
1265
4.68k
      } else
1266
23.8k
      {
1267
         /*Was a narrowband frame, set "null submode"*/
1268
23.8k
         st->submodeID = 0;
1269
23.8k
      }
1270
28.5k
      if (st->submodeID != 0 && st->submodes[st->submodeID] == NULL)
1271
514
      {
1272
514
         speex_notify("Invalid mode encountered. The stream is corrupted.");
1273
514
         return -2;
1274
514
      }
1275
28.5k
   }
1276
1277
   /* If null mode (no transmission), just set a couple things to zero*/
1278
28.0k
   if (st->submodes[st->submodeID] == NULL)
1279
24.7k
   {
1280
24.7k
      if (st->innov_save)
1281
8.69k
        SPEEX_MEMSET(st->innov_save, 0, st->full_frame_size);
1282
1283
24.7k
      if (dtx)
1284
700
      {
1285
700
         sb_decode_lost(st, out, 1, stack);
1286
700
         return 0;
1287
700
      }
1288
1289
5.40M
      for (i=0;i<st->frame_size;i++)
1290
5.38M
         out[st->frame_size+i]=VERY_SMALL;
1291
1292
24.0k
      st->first=1;
1293
1294
      /* Final signal synthesis from excitation */
1295
24.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
24.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
24.0k
      return 0;
1300
1301
24.7k
   }
1302
1303
3.32k
   ALLOC(low_pi_gain, st->nbSubframes, spx_word32_t);
1304
3.32k
   ALLOC(low_exc_rms, st->nbSubframes, spx_word16_t);
1305
3.32k
   speex_decoder_ctl(st->st_low, SPEEX_GET_PI_GAIN, low_pi_gain);
1306
3.32k
   speex_decoder_ctl(st->st_low, SPEEX_GET_EXC, low_exc_rms);
1307
1308
3.32k
   ALLOC(qlsp, st->lpcSize, spx_lsp_t);
1309
3.32k
   ALLOC(interp_qlsp, st->lpcSize, spx_lsp_t);
1310
3.32k
   SUBMODE(lsp_unquant)(qlsp, st->lpcSize, bits);
1311
1312
3.32k
   if (st->first)
1313
1.56k
   {
1314
14.0k
      for (i=0;i<st->lpcSize;i++)
1315
12.4k
         st->old_qlsp[i] = qlsp[i];
1316
1.56k
   }
1317
1318
3.32k
   ALLOC(ak, st->lpcSize, spx_coef_t);
1319
1320
16.6k
   for (sub=0;sub<st->nbSubframes;sub++)
1321
13.3k
   {
1322
13.3k
      VARDECL(spx_word32_t *exc);
1323
13.3k
      spx_word16_t *innov_save=NULL;
1324
13.3k
      spx_word16_t *sp;
1325
13.3k
      spx_word16_t filter_ratio;
1326
13.3k
      spx_word16_t el=0;
1327
13.3k
      int offset;
1328
13.3k
      spx_word32_t rl=0,rh=0;
1329
1330
13.3k
      offset = st->subframeSize*sub;
1331
13.3k
      sp=out+st->frame_size+offset;
1332
13.3k
      ALLOC(exc, st->subframeSize, spx_word32_t);
1333
      /* Pointer for saving innovation */
1334
13.3k
      if (st->innov_save)
1335
8.06k
      {
1336
8.06k
         innov_save = st->innov_save+2*offset;
1337
8.06k
         SPEEX_MEMSET(innov_save, 0, 2*st->subframeSize);
1338
8.06k
      }
1339
1340
      /* LSP interpolation */
1341
13.3k
      lsp_interpolate(st->old_qlsp, qlsp, interp_qlsp, st->lpcSize, sub, st->nbSubframes, LSP_MARGIN);
1342
1343
      /* LSP to LPC */
1344
13.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
13.3k
         st->pi_gain[sub]=LPC_SCALING;
1350
13.3k
         rh = LPC_SCALING;
1351
66.5k
         for (i=0;i<st->lpcSize;i+=2)
1352
53.2k
         {
1353
53.2k
            rh += ak[i+1] - ak[i];
1354
53.2k
            st->pi_gain[sub] += ak[i] + ak[i+1];
1355
53.2k
         }
1356
1357
13.3k
         rl = low_pi_gain[sub];
1358
13.3k
#ifdef FIXED_POINT
1359
13.3k
         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
13.3k
      SPEEX_MEMSET(exc, 0, st->subframeSize);
1365
13.3k
      if (!SUBMODE(innovation_unquant))
1366
4.10k
      {
1367
4.10k
         spx_word32_t g;
1368
4.10k
         int quant;
1369
1370
4.10k
         quant = speex_bits_unpack_unsigned(bits, 5);
1371
4.10k
         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.10k
         g = PDIV32(g, MAX16(EPSILON,filter_ratio));
1375
1376
135k
         for (i=0;i<st->subframeSize;i+=2)
1377
131k
         {
1378
131k
            exc[i]=SHL32(MULT16_32_P15(MULT16_16_Q15(mode->folding_gain,low_innov_alias[offset+i]),SHL32(g,6)),SIG_SHIFT);
1379
131k
            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
131k
         }
1381
1382
9.21k
      } else {
1383
9.21k
         spx_word16_t gc;
1384
9.21k
         spx_word32_t scale;
1385
9.21k
         int qgc = speex_bits_unpack_unsigned(bits, 4);
1386
1387
9.21k
         el = low_exc_rms[sub];
1388
9.21k
         gc = MULT16_16_Q15(QCONST16(0.87360,15),gc_quant_bound[qgc]);
1389
1390
9.21k
         if (st->subframeSize==80)
1391
0
            gc = MULT16_16_P14(QCONST16(1.4142f,14),gc);
1392
1393
9.21k
         scale = SHL32(PDIV32(SHL32(MULT16_16(gc, el),3), MAX16(EPSILON,filter_ratio)),SIG_SHIFT-3);
1394
9.21k
         SUBMODE(innovation_unquant)(exc, SUBMODE(innovation_params), st->subframeSize,
1395
9.21k
                                     bits, stack, &st->seed);
1396
1397
9.21k
         signal_mul(exc,exc,scale,st->subframeSize);
1398
1399
9.21k
         if (SUBMODE(double_codebook)) {
1400
3.24k
            char *tmp_stack=stack;
1401
3.24k
            VARDECL(spx_sig_t *innov2);
1402
3.24k
            ALLOC(innov2, st->subframeSize, spx_sig_t);
1403
3.24k
            SPEEX_MEMSET(innov2, 0, st->subframeSize);
1404
3.24k
            SUBMODE(innovation_unquant)(innov2, SUBMODE(innovation_params), st->subframeSize,
1405
3.24k
                                        bits, stack, &st->seed);
1406
3.24k
            signal_mul(innov2, innov2, MULT16_32_P15(QCONST16(0.4f,15),scale), st->subframeSize);
1407
133k
            for (i=0;i<st->subframeSize;i++)
1408
129k
               exc[i] = ADD32(exc[i],innov2[i]);
1409
3.24k
            stack = tmp_stack;
1410
3.24k
         }
1411
1412
9.21k
      }
1413
1414
13.3k
      if (st->innov_save)
1415
8.06k
      {
1416
330k
         for (i=0;i<st->subframeSize;i++)
1417
322k
            innov_save[2*i]=EXTRACT16(PSHR32(exc[i],SIG_SHIFT));
1418
8.06k
      }
1419
1420
13.3k
      iir_mem16(st->excBuf, st->interp_qlpc, sp, st->subframeSize, st->lpcSize,
1421
13.3k
               st->mem_sp, stack);
1422
645k
      for (i=0;i<st->subframeSize;i++)
1423
632k
         st->excBuf[i]=EXTRACT16(PSHR32(exc[i],SIG_SHIFT));
1424
119k
      for (i=0;i<st->lpcSize;i++)
1425
106k
         st->interp_qlpc[i] = ak[i];
1426
13.3k
      st->exc_rms[sub] = compute_rms16(st->excBuf, st->subframeSize);
1427
13.3k
      exc_ener_sum = ADD32(exc_ener_sum, DIV32(MULT16_16(st->exc_rms[sub],st->exc_rms[sub]), st->nbSubframes));
1428
13.3k
   }
1429
3.32k
   st->last_ener = spx_sqrt(exc_ener_sum);
1430
1431
3.32k
   qmf_synth(out, out+st->frame_size, h0, out, st->full_frame_size, QMF_ORDER, st->g0_mem, st->g1_mem, stack);
1432
29.9k
   for (i=0;i<st->lpcSize;i++)
1433
26.6k
      st->old_qlsp[i] = qlsp[i];
1434
1435
3.32k
   st->first=0;
1436
1437
3.32k
   return 0;
1438
28.0k
}
1439
#endif /* DISABLE_DECODER */
1440
1441
1442
1443
#endif
1444