Debian Codegraph @

3rdparty/iaxclient/lib/libspeex/sb_celp.c

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