Debian Codegraph @

libspeex/sb_celp.c

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