1 /* Copyright (C) 2002-2006 Jean-Marc Valin
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions
8 - Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
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.
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.
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.
33 #include "config-speex.h"
41 #include "stack_alloc.h"
42 #include "cb_search.h"
43 #include "quant_lsp.h"
47 #include "math_approx.h"
48 #include "os_support.h"
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. */
57 #define SB_ENC_STACK (10000*sizeof(spx_sig_t))
61 #define SB_DEC_STACK (6000*sizeof(spx_sig_t))
65 #ifdef DISABLE_WIDEBAND
66 void *sb_encoder_init(const SpeexMode
*m
)
68 speex_fatal("Wideband and Ultra-wideband are disabled");
71 void sb_encoder_destroy(void *state
)
73 speex_fatal("Wideband and Ultra-wideband are disabled");
75 int sb_encode(void *state
, void *vin
, SpeexBits
*bits
)
77 speex_fatal("Wideband and Ultra-wideband are disabled");
80 void *sb_decoder_init(const SpeexMode
*m
)
82 speex_fatal("Wideband and Ultra-wideband are disabled");
85 void sb_decoder_destroy(void *state
)
87 speex_fatal("Wideband and Ultra-wideband are disabled");
89 int sb_decode(void *state
, SpeexBits
*bits
, void *vout
)
91 speex_fatal("Wideband and Ultra-wideband are disabled");
94 int sb_encoder_ctl(void *state
, int request
, void *ptr
)
96 speex_fatal("Wideband and Ultra-wideband are disabled");
99 int sb_decoder_ctl(void *state
, int request
, void *ptr
)
101 speex_fatal("Wideband and Ultra-wideband are disabled");
108 #define M_PI 3.14159265358979323846 /* pi */
111 #define sqr(x) ((x)*(x))
113 #define SUBMODE(x) st->submodes[st->submodeID]->x
116 static const spx_word16_t gc_quant_bound
[16] ICONST_ATTR
= {125, 164, 215, 282, 370, 484, 635, 832, 1090, 1428, 1871, 2452, 3213, 4210, 5516, 7228};
117 static const spx_word16_t fold_quant_bound
[32] ICONST_ATTR
= {
118 39, 44, 50, 57, 64, 73, 83, 94,
119 106, 120, 136, 154, 175, 198, 225, 255,
120 288, 327, 370, 420, 476, 539, 611, 692,
121 784, 889, 1007, 1141, 1293, 1465, 1660, 1881};
122 #define LSP_MARGIN 410
123 #define LSP_DELTA1 6553
124 #define LSP_DELTA2 1638
128 static const spx_word16_t gc_quant_bound
[16] = {
129 0.97979, 1.28384, 1.68223, 2.20426, 2.88829, 3.78458, 4.95900, 6.49787,
130 8.51428, 11.15642, 14.61846, 19.15484, 25.09895, 32.88761, 43.09325, 56.46588};
131 static const spx_word16_t fold_quant_bound
[32] = {
132 0.30498, 0.34559, 0.39161, 0.44375, 0.50283, 0.56979, 0.64565, 0.73162,
133 0.82903, 0.93942, 1.06450, 1.20624, 1.36685, 1.54884, 1.75506, 1.98875,
134 2.25355, 2.55360, 2.89361, 3.27889, 3.71547, 4.21018, 4.77076, 5.40598,
135 6.12577, 6.94141, 7.86565, 8.91295, 10.09969, 11.44445, 12.96826, 14.69497};
137 #define LSP_MARGIN .05
138 #define LSP_DELTA1 .2
139 #define LSP_DELTA2 .05
146 static const spx_word16_t h0
[64] ICONST_ATTR
= {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};
149 static const float h0
[64] = {
150 3.596189e-05f
, -0.0001123515f
,
151 -0.0001104587f
, 0.0002790277f
,
152 0.0002298438f
, -0.0005953563f
,
153 -0.0003823631f
, 0.00113826f
,
154 0.0005308539f
, -0.001986177f
,
155 -0.0006243724f
, 0.003235877f
,
156 0.0005743159f
, -0.004989147f
,
157 -0.0002584767f
, 0.007367171f
,
158 -0.0004857935f
, -0.01050689f
,
159 0.001894714f
, 0.01459396f
,
160 -0.004313674f
, -0.01994365f
,
161 0.00828756f
, 0.02716055f
,
162 -0.01485397f
, -0.03764973f
,
163 0.026447f
, 0.05543245f
,
164 -0.05095487f
, -0.09779096f
,
165 0.1382363f
, 0.4600981f
,
166 0.4600981f
, 0.1382363f
,
167 -0.09779096f
, -0.05095487f
,
168 0.05543245f
, 0.026447f
,
169 -0.03764973f
, -0.01485397f
,
170 0.02716055f
, 0.00828756f
,
171 -0.01994365f
, -0.004313674f
,
172 0.01459396f
, 0.001894714f
,
173 -0.01050689f
, -0.0004857935f
,
174 0.007367171f
, -0.0002584767f
,
175 -0.004989147f
, 0.0005743159f
,
176 0.003235877f
, -0.0006243724f
,
177 -0.001986177f
, 0.0005308539f
,
178 0.00113826f
, -0.0003823631f
,
179 -0.0005953563f
, 0.0002298438f
,
180 0.0002790277f
, -0.0001104587f
,
181 -0.0001123515f
, 3.596189e-05f
186 extern const spx_word16_t lag_window
[];
187 extern const spx_word16_t lpc_window
[];
189 #ifndef SPEEX_DISABLE_ENCODER
190 void *sb_encoder_init(const SpeexMode
*m
)
195 const SpeexSBMode
*mode
;
197 st
= (SBEncState
*)speex_alloc(sizeof(SBEncState
));
201 mode
= (const SpeexSBMode
*)m
->mode
;
204 st
->st_low
= speex_encoder_init(mode
->nb_mode
);
205 #if defined(VAR_ARRAYS) || defined (USE_ALLOCA)
208 /*st->stack = (char*)speex_alloc_scratch(SB_ENC_STACK);*/
209 speex_encoder_ctl(st
->st_low
, SPEEX_GET_STACK
, &st
->stack
);
212 st
->full_frame_size
= 2*mode
->frameSize
;
213 st
->frame_size
= mode
->frameSize
;
214 st
->subframeSize
= mode
->subframeSize
;
215 st
->nbSubframes
= mode
->frameSize
/mode
->subframeSize
;
216 st
->windowSize
= st
->frame_size
+st
->subframeSize
;
217 st
->lpcSize
=mode
->lpcSize
;
219 st
->encode_submode
= 1;
220 st
->submodes
=mode
->submodes
;
221 st
->submodeSelect
= st
->submodeID
=mode
->defaultSubmode
;
224 speex_encoder_ctl(st
->st_low
, SPEEX_SET_QUALITY
, &tmp
);
226 speex_encoder_ctl(st
->st_low
, SPEEX_SET_WIDEBAND
, &tmp
);
228 st
->lpc_floor
= mode
->lpc_floor
;
229 st
->gamma1
=mode
->gamma1
;
230 st
->gamma2
=mode
->gamma2
;
233 st
->high
=(spx_word16_t
*)speex_alloc((st
->windowSize
-st
->frame_size
)*sizeof(spx_word16_t
));
235 st
->h0_mem
=(spx_word16_t
*)speex_alloc((QMF_ORDER
)*sizeof(spx_word16_t
));
236 st
->h1_mem
=(spx_word16_t
*)speex_alloc((QMF_ORDER
)*sizeof(spx_word16_t
));
238 st
->window
= lpc_window
;
240 st
->lagWindow
= lag_window
;
242 st
->old_lsp
= (spx_lsp_t
*)speex_alloc(st
->lpcSize
*sizeof(spx_lsp_t
));
243 st
->old_qlsp
= (spx_lsp_t
*)speex_alloc(st
->lpcSize
*sizeof(spx_lsp_t
));
244 st
->interp_qlpc
= (spx_coef_t
*)speex_alloc(st
->lpcSize
*sizeof(spx_coef_t
));
245 st
->pi_gain
= (spx_word32_t
*)speex_alloc((st
->nbSubframes
)*sizeof(spx_word32_t
));
246 st
->exc_rms
= (spx_word16_t
*)speex_alloc((st
->nbSubframes
)*sizeof(spx_word16_t
));
247 st
->innov_rms_save
= NULL
;
249 st
->mem_sp
= (spx_mem_t
*)speex_alloc((st
->lpcSize
)*sizeof(spx_mem_t
));
250 st
->mem_sp2
= (spx_mem_t
*)speex_alloc((st
->lpcSize
)*sizeof(spx_mem_t
));
251 st
->mem_sw
= (spx_mem_t
*)speex_alloc((st
->lpcSize
)*sizeof(spx_mem_t
));
253 for (i
=0;i
<st
->lpcSize
;i
++)
254 st
->old_lsp
[i
]= DIV32(MULT16_16(QCONST16(3.1415927f
, LSP_SHIFT
), i
+1), st
->lpcSize
+1);
260 st
->vbr_max_high
= 20000; /* We just need a big value here */
263 st
->relative_quality
=0;
264 #endif /* #ifndef DISABLE_VBR */
267 speex_encoder_ctl(st
->st_low
, SPEEX_GET_SAMPLING_RATE
, &st
->sampling_rate
);
268 st
->sampling_rate
*=2;
269 #ifdef ENABLE_VALGRIND
270 VALGRIND_MAKE_READABLE(st
, (st
->stack
-(char*)st
));
275 void sb_encoder_destroy(void *state
)
277 SBEncState
*st
=(SBEncState
*)state
;
279 speex_encoder_destroy(st
->st_low
);
280 #if !(defined(VAR_ARRAYS) || defined (USE_ALLOCA))
281 /*speex_free_scratch(st->stack);*/
284 speex_free(st
->high
);
286 speex_free(st
->h0_mem
);
287 speex_free(st
->h1_mem
);
289 speex_free(st
->old_lsp
);
290 speex_free(st
->old_qlsp
);
291 speex_free(st
->interp_qlpc
);
292 speex_free(st
->pi_gain
);
293 speex_free(st
->exc_rms
);
295 speex_free(st
->mem_sp
);
296 speex_free(st
->mem_sp2
);
297 speex_free(st
->mem_sw
);
304 int sb_encode(void *state
, void *vin
, SpeexBits
*bits
)
309 VARDECL(spx_mem_t
*mem
);
310 VARDECL(spx_sig_t
*innov
);
311 VARDECL(spx_word16_t
*target
);
312 VARDECL(spx_word16_t
*syn_resp
);
313 VARDECL(spx_word32_t
*low_pi_gain
);
316 VARDECL(spx_word16_t
*low_exc_rms
);
317 VARDECL(spx_word16_t
*low_innov_rms
);
318 const SpeexSBMode
*mode
;
320 spx_word16_t
*in
= (spx_word16_t
*)vin
;
321 spx_word16_t e_low
=0, e_high
=0;
322 VARDECL(spx_coef_t
*lpc
);
323 VARDECL(spx_coef_t
*interp_lpc
);
324 VARDECL(spx_coef_t
*bw_lpc1
);
325 VARDECL(spx_coef_t
*bw_lpc2
);
326 VARDECL(spx_lsp_t
*lsp
);
327 VARDECL(spx_lsp_t
*qlsp
);
328 VARDECL(spx_lsp_t
*interp_lsp
);
329 VARDECL(spx_lsp_t
*interp_qlsp
);
331 st
= (SBEncState
*)state
;
333 mode
= (const SpeexSBMode
*)(st
->mode
->mode
);
335 high
= in
+st
->frame_size
;
337 /* High-band buffering / sync with low band */
338 /* Compute the two sub-bands by filtering with QMF h0*/
339 qmf_decomp(in
, h0
, low
, high
, st
->full_frame_size
, QMF_ORDER
, st
->h0_mem
, stack
);
342 if (st
->vbr_enabled
|| st
->vad_enabled
)
344 /* Need to compute things here before the signal is trashed by the encoder */
345 /*FIXME: Are the two signals (low, high) in sync? */
346 e_low
= compute_rms16(low
, st
->frame_size
);
347 e_high
= compute_rms16(high
, st
->frame_size
);
349 #endif /* #ifndef DISABLE_VBR */
351 ALLOC(low_innov_rms
, st
->nbSubframes
, spx_word16_t
);
352 speex_encoder_ctl(st
->st_low
, SPEEX_SET_INNOVATION_SAVE
, low_innov_rms
);
353 /* Encode the narrowband part*/
354 speex_encode_native(st
->st_low
, low
, bits
);
356 high
= high
- (st
->windowSize
-st
->frame_size
);
357 SPEEX_COPY(high
, st
->high
, st
->windowSize
-st
->frame_size
);
358 SPEEX_COPY(st
->high
, &high
[st
->frame_size
], st
->windowSize
-st
->frame_size
);
361 ALLOC(low_pi_gain
, st
->nbSubframes
, spx_word32_t
);
362 ALLOC(low_exc_rms
, st
->nbSubframes
, spx_word16_t
);
363 speex_encoder_ctl(st
->st_low
, SPEEX_GET_PI_GAIN
, low_pi_gain
);
364 speex_encoder_ctl(st
->st_low
, SPEEX_GET_EXC
, low_exc_rms
);
366 speex_encoder_ctl(st
->st_low
, SPEEX_GET_LOW_MODE
, &dtx
);
373 ALLOC(lpc
, st
->lpcSize
, spx_coef_t
);
374 ALLOC(interp_lpc
, st
->lpcSize
, spx_coef_t
);
375 ALLOC(bw_lpc1
, st
->lpcSize
, spx_coef_t
);
376 ALLOC(bw_lpc2
, st
->lpcSize
, spx_coef_t
);
378 ALLOC(lsp
, st
->lpcSize
, spx_lsp_t
);
379 ALLOC(qlsp
, st
->lpcSize
, spx_lsp_t
);
380 ALLOC(interp_lsp
, st
->lpcSize
, spx_lsp_t
);
381 ALLOC(interp_qlsp
, st
->lpcSize
, spx_lsp_t
);
384 VARDECL(spx_word16_t
*autocorr
);
385 VARDECL(spx_word16_t
*w_sig
);
386 ALLOC(autocorr
, st
->lpcSize
+1, spx_word16_t
);
387 ALLOC(w_sig
, st
->windowSize
, spx_word16_t
);
388 /* Window for analysis */
389 /* FIXME: This is a kludge */
390 if (st
->subframeSize
==80)
392 for (i
=0;i
<st
->windowSize
;i
++)
393 w_sig
[i
] = EXTRACT16(SHR32(MULT16_16(high
[i
],st
->window
[i
>>1]),SIG_SHIFT
));
395 for (i
=0;i
<st
->windowSize
;i
++)
396 w_sig
[i
] = EXTRACT16(SHR32(MULT16_16(high
[i
],st
->window
[i
]),SIG_SHIFT
));
398 /* Compute auto-correlation */
399 _spx_autocorr(w_sig
, autocorr
, st
->lpcSize
+1, st
->windowSize
);
400 autocorr
[0] = ADD16(autocorr
[0],MULT16_16_Q15(autocorr
[0],st
->lpc_floor
)); /* Noise floor in auto-correlation domain */
402 /* Lag windowing: equivalent to filtering in the power-spectrum domain */
403 for (i
=0;i
<st
->lpcSize
+1;i
++)
404 autocorr
[i
] = MULT16_16_Q14(autocorr
[i
],st
->lagWindow
[i
]);
406 /* Levinson-Durbin */
407 _spx_lpc(lpc
, autocorr
, st
->lpcSize
);
410 /* LPC to LSPs (x-domain) transform */
411 roots
=lpc_to_lsp (lpc
, st
->lpcSize
, lsp
, 10, LSP_DELTA1
, stack
);
412 if (roots
!=st
->lpcSize
)
414 roots
= lpc_to_lsp (lpc
, st
->lpcSize
, lsp
, 10, LSP_DELTA2
, stack
);
415 if (roots
!=st
->lpcSize
) {
416 /*If we can't find all LSP's, do some damage control and use a flat filter*/
417 for (i
=0;i
<st
->lpcSize
;i
++)
419 lsp
[i
]=st
->old_lsp
[i
];
426 if ((st
->vbr_enabled
|| st
->vad_enabled
) && !dtx
)
432 if (st
->abr_drift2
* st
->abr_drift
> 0)
434 /* Only adapt if long-term and short-term drift are the same sign */
435 qual_change
= -.00001*st
->abr_drift
/(1+st
->abr_count
);
441 st
->vbr_quality
+= qual_change
;
442 if (st
->vbr_quality
>10)
444 if (st
->vbr_quality
<0)
449 ratio
= 2*log((1.f
+e_high
)/(1.f
+e_low
));
451 speex_encoder_ctl(st
->st_low
, SPEEX_GET_RELATIVE_QUALITY
, &st
->relative_quality
);
460 modeid
= mode
->nb_modes
-1;
461 st
->relative_quality
+=1.0*(ratio
+2);
462 if (st
->relative_quality
<-1)
463 st
->relative_quality
=-1;
468 v1
=(int)floor(st
->vbr_quality
);
470 thresh
= mode
->vbr_thresh
[modeid
][v1
];
472 thresh
= (st
->vbr_quality
-v1
) * mode
->vbr_thresh
[modeid
][v1
+1] +
473 (1+v1
-st
->vbr_quality
) * mode
->vbr_thresh
[modeid
][v1
];
474 if (st
->relative_quality
>= thresh
&& st
->sampling_rate
*st
->submodes
[modeid
]->bits_per_frame
/st
->full_frame_size
<= st
->vbr_max_high
)
478 speex_encoder_ctl(state
, SPEEX_SET_HIGH_MODE
, &modeid
);
482 speex_encoder_ctl(state
, SPEEX_GET_BITRATE
, &bitrate
);
483 st
->abr_drift
+=(bitrate
-st
->abr_enabled
);
484 st
->abr_drift2
= .95*st
->abr_drift2
+ .05*(bitrate
-st
->abr_enabled
);
485 st
->abr_count
+= 1.0;
491 if (st
->relative_quality
<2.0)
494 modeid
=st
->submodeSelect
;
495 /*speex_encoder_ctl(state, SPEEX_SET_MODE, &mode);*/
496 st
->submodeID
=modeid
;
499 /*fprintf (stderr, "%f %f\n", ratio, low_qual);*/
501 #endif /* #ifndef DISABLE_VBR */
503 if (st
->encode_submode
)
505 speex_bits_pack(bits
, 1, 1);
507 speex_bits_pack(bits
, 0, SB_SUBMODE_BITS
);
509 speex_bits_pack(bits
, st
->submodeID
, SB_SUBMODE_BITS
);
512 /* If null mode (no transmission), just set a couple things to zero*/
513 if (dtx
|| st
->submodes
[st
->submodeID
] == NULL
)
515 for (i
=0;i
<st
->frame_size
;i
++)
518 for (i
=0;i
<st
->lpcSize
;i
++)
522 /* Final signal synthesis from excitation */
523 iir_mem16(high
, st
->interp_qlpc
, high
, st
->frame_size
, st
->lpcSize
, st
->mem_sp
, stack
);
532 /* LSP quantization */
533 SUBMODE(lsp_quant
)(lsp
, qlsp
, st
->lpcSize
, bits
);
537 for (i
=0;i
<st
->lpcSize
;i
++)
538 st
->old_lsp
[i
] = lsp
[i
];
539 for (i
=0;i
<st
->lpcSize
;i
++)
540 st
->old_qlsp
[i
] = qlsp
[i
];
543 ALLOC(mem
, st
->lpcSize
, spx_mem_t
);
544 ALLOC(syn_resp
, st
->subframeSize
, spx_word16_t
);
545 ALLOC(innov
, st
->subframeSize
, spx_sig_t
);
546 ALLOC(target
, st
->subframeSize
, spx_word16_t
);
548 for (sub
=0;sub
<st
->nbSubframes
;sub
++)
550 VARDECL(spx_word16_t
*exc
);
551 VARDECL(spx_word16_t
*res
);
552 VARDECL(spx_word16_t
*sw
);
554 spx_word16_t filter_ratio
; /*Q7*/
556 spx_word32_t rl
, rh
; /*Q13*/
559 offset
= st
->subframeSize
*sub
;
561 ALLOC(exc
, st
->subframeSize
, spx_word16_t
);
562 ALLOC(res
, st
->subframeSize
, spx_word16_t
);
563 ALLOC(sw
, st
->subframeSize
, spx_word16_t
);
565 /* LSP interpolation (quantized and unquantized) */
566 lsp_interpolate(st
->old_lsp
, lsp
, interp_lsp
, st
->lpcSize
, sub
, st
->nbSubframes
);
567 lsp_interpolate(st
->old_qlsp
, qlsp
, interp_qlsp
, st
->lpcSize
, sub
, st
->nbSubframes
);
569 lsp_enforce_margin(interp_lsp
, st
->lpcSize
, LSP_MARGIN
);
570 lsp_enforce_margin(interp_qlsp
, st
->lpcSize
, LSP_MARGIN
);
572 lsp_to_lpc(interp_lsp
, interp_lpc
, st
->lpcSize
,stack
);
573 lsp_to_lpc(interp_qlsp
, st
->interp_qlpc
, st
->lpcSize
, stack
);
575 bw_lpc(st
->gamma1
, interp_lpc
, bw_lpc1
, st
->lpcSize
);
576 bw_lpc(st
->gamma2
, interp_lpc
, bw_lpc2
, st
->lpcSize
);
578 /* Compute mid-band (4000 Hz for wideband) response of low-band and high-band
580 st
->pi_gain
[sub
]=LPC_SCALING
;
582 for (i
=0;i
<st
->lpcSize
;i
+=2)
584 rh
+= st
->interp_qlpc
[i
+1] - st
->interp_qlpc
[i
];
585 st
->pi_gain
[sub
] += st
->interp_qlpc
[i
] + st
->interp_qlpc
[i
+1];
588 rl
= low_pi_gain
[sub
];
590 filter_ratio
=EXTRACT16(SATURATE(PDIV32(SHL32(ADD32(rl
,82),7),ADD32(82,rh
)),32767));
592 filter_ratio
=(rl
+.01)/(rh
+.01);
595 /* Compute "real excitation" */
596 fir_mem16(sp
, st
->interp_qlpc
, exc
, st
->subframeSize
, st
->lpcSize
, st
->mem_sp2
, stack
);
597 /* Compute energy of low-band and high-band excitation */
599 eh
= compute_rms16(exc
, st
->subframeSize
);
601 if (!SUBMODE(innovation_quant
)) {/* 1 for spectral folding excitation, 0 for stochastic */
602 spx_word32_t g
; /*Q7*/
603 spx_word16_t el
; /*Q0*/
604 el
= low_innov_rms
[sub
];
606 /* Gain to use if we want to use the low-band excitation for high-band */
607 g
=PDIV32(MULT16_16(filter_ratio
,eh
),EXTEND32(ADD16(1,el
)));
611 char *tmp_stack
=stack
;
614 ALLOC(tmp_sig
, st
->subframeSize
, spx_sig_t
);
615 for (i
=0;i
<st
->lpcSize
;i
++)
616 mem
[i
]=st
->mem_sp
[i
];
617 iir_mem2(st
->low_innov
+offset
, st
->interp_qlpc
, tmp_sig
, st
->subframeSize
, st
->lpcSize
, mem
);
618 g2
= compute_rms(sp
, st
->subframeSize
)/(.01+compute_rms(tmp_sig
, st
->subframeSize
));
619 /*fprintf (stderr, "gains: %f %f\n", g, g2);*/
625 /*print_vec(&g, 1, "gain factor");*/
626 /* Gain quantization */
628 int quant
= scal_quant(g
, fold_quant_bound
, 32);
629 /*speex_warning_int("tata", quant);*/
634 speex_bits_pack(bits
, quant
, 5);
636 if (st
->innov_rms_save
)
638 st
->innov_rms_save
[sub
] = eh
;
640 st
->exc_rms
[sub
] = eh
;
642 spx_word16_t gc
; /*Q7*/
643 spx_word32_t scale
; /*Q14*/
644 spx_word16_t el
; /*Q0*/
645 el
= low_exc_rms
[sub
]; /*Q0*/
647 gc
= PDIV32_16(MULT16_16(filter_ratio
,1+eh
),1+el
);
649 /* This is a kludge that cleans up a historical bug */
650 if (st
->subframeSize
==80)
651 gc
= MULT16_16_P15(QCONST16(0.70711f
,15),gc
);
652 /*printf ("%f %f %f %f\n", el, eh, filter_ratio, gc);*/
654 int qgc
= scal_quant(gc
, gc_quant_bound
, 16);
655 speex_bits_pack(bits
, qgc
, 4);
656 gc
= MULT16_16_Q15(QCONST16(0.87360,15),gc_quant_bound
[qgc
]);
658 if (st
->subframeSize
==80)
659 gc
= MULT16_16_P14(QCONST16(1.4142f
,14), gc
);
661 scale
= SHL32(MULT16_16(PDIV32_16(SHL32(EXTEND32(gc
),SIG_SHIFT
-6),filter_ratio
),(1+el
)),6);
663 compute_impulse_response(st
->interp_qlpc
, bw_lpc1
, bw_lpc2
, syn_resp
, st
->subframeSize
, st
->lpcSize
, stack
);
666 /* Reset excitation */
667 for (i
=0;i
<st
->subframeSize
;i
++)
670 /* Compute zero response (ringing) of A(z/g1) / ( A(z/g2) * Aq(z) ) */
671 for (i
=0;i
<st
->lpcSize
;i
++)
672 mem
[i
]=st
->mem_sp
[i
];
673 iir_mem16(res
, st
->interp_qlpc
, res
, st
->subframeSize
, st
->lpcSize
, mem
, stack
);
675 for (i
=0;i
<st
->lpcSize
;i
++)
676 mem
[i
]=st
->mem_sw
[i
];
677 filter_mem16(res
, bw_lpc1
, bw_lpc2
, res
, st
->subframeSize
, st
->lpcSize
, mem
, stack
);
679 /* Compute weighted signal */
680 for (i
=0;i
<st
->lpcSize
;i
++)
681 mem
[i
]=st
->mem_sw
[i
];
682 filter_mem16(sp
, bw_lpc1
, bw_lpc2
, sw
, st
->subframeSize
, st
->lpcSize
, mem
, stack
);
684 /* Compute target signal */
685 for (i
=0;i
<st
->subframeSize
;i
++)
686 target
[i
]=SUB16(sw
[i
],res
[i
]);
688 signal_div(target
, target
, scale
, st
->subframeSize
);
690 /* Reset excitation */
691 SPEEX_MEMSET(innov
, 0, st
->subframeSize
);
693 /*print_vec(target, st->subframeSize, "\ntarget");*/
694 SUBMODE(innovation_quant
)(target
, st
->interp_qlpc
, bw_lpc1
, bw_lpc2
,
695 SUBMODE(innovation_params
), st
->lpcSize
, st
->subframeSize
,
696 innov
, syn_resp
, bits
, stack
, st
->complexity
, SUBMODE(double_codebook
));
697 /*print_vec(target, st->subframeSize, "after");*/
699 signal_mul(innov
, innov
, scale
, st
->subframeSize
);
701 if (SUBMODE(double_codebook
)) {
702 char *tmp_stack
=stack
;
703 VARDECL(spx_sig_t
*innov2
);
704 ALLOC(innov2
, st
->subframeSize
, spx_sig_t
);
705 SPEEX_MEMSET(innov2
, 0, st
->subframeSize
);
706 for (i
=0;i
<st
->subframeSize
;i
++)
707 target
[i
]=MULT16_16_P13(QCONST16(2.5f
,13), target
[i
]);
709 SUBMODE(innovation_quant
)(target
, st
->interp_qlpc
, bw_lpc1
, bw_lpc2
,
710 SUBMODE(innovation_params
), st
->lpcSize
, st
->subframeSize
,
711 innov2
, syn_resp
, bits
, stack
, st
->complexity
, 0);
712 signal_mul(innov2
, innov2
, MULT16_32_P15(QCONST16(0.4f
,15),scale
), st
->subframeSize
);
714 for (i
=0;i
<st
->subframeSize
;i
++)
715 innov
[i
] = ADD32(innov
[i
],innov2
[i
]);
718 for (i
=0;i
<st
->subframeSize
;i
++)
719 exc
[i
] = PSHR32(innov
[i
],SIG_SHIFT
);
721 if (st
->innov_rms_save
)
723 st
->innov_rms_save
[sub
] = MULT16_16_Q15(QCONST16(.70711f
, 15), compute_rms(innov
, st
->subframeSize
));
725 st
->exc_rms
[sub
] = compute_rms16(exc
, st
->subframeSize
);
731 /*Keep the previous memory*/
732 for (i
=0;i
<st
->lpcSize
;i
++)
733 mem
[i
]=st
->mem_sp
[i
];
734 /* Final signal synthesis from excitation */
735 iir_mem16(exc
, st
->interp_qlpc
, sp
, st
->subframeSize
, st
->lpcSize
, st
->mem_sp
, stack
);
737 /* Compute weighted signal again, from synthesized speech (not sure it's the right thing) */
738 filter_mem16(sp
, bw_lpc1
, bw_lpc2
, sw
, st
->subframeSize
, st
->lpcSize
, st
->mem_sw
, stack
);
741 for (i
=0;i
<st
->lpcSize
;i
++)
742 st
->old_lsp
[i
] = lsp
[i
];
743 for (i
=0;i
<st
->lpcSize
;i
++)
744 st
->old_qlsp
[i
] = qlsp
[i
];
750 #endif /* SPEEX_DISABLE_ENCODER */
754 static SBDecState global_decstate_wb IBSS_ATTR
;
755 /* Do not include this for voice codec, files will never be UWB */
756 #ifndef ROCKBOX_VOICE_CODEC
757 static SBDecState global_decstate_uwb IBSS_ATTR
;
760 void *sb_decoder_init(const SpeexMode
*m
)
764 const SpeexSBMode
*mode
;
766 st = (SBDecState*)speex_alloc(sizeof(SBDecState));
770 #ifndef ROCKBOX_VOICE_CODEC
771 if (m
->modeID
== SPEEX_MODEID_UWB
)
772 st
= &global_decstate_uwb
;
775 st
= &global_decstate_wb
;
776 memset(st
, 0, sizeof(*st
));
778 mode
=(const SpeexSBMode
*)m
->mode
;
779 st
->encode_submode
= 1;
781 st
->st_low
= speex_decoder_init(mode
->nb_mode
);
782 #if defined(VAR_ARRAYS) || defined (USE_ALLOCA)
785 /*st->stack = (char*)speex_alloc_scratch(SB_DEC_STACK);*/
786 speex_decoder_ctl(st
->st_low
, SPEEX_GET_STACK
, &st
->stack
);
789 st
->full_frame_size
= 2*mode
->frameSize
;
790 st
->frame_size
= mode
->frameSize
;
791 st
->subframeSize
= mode
->subframeSize
;
792 st
->nbSubframes
= mode
->frameSize
/mode
->subframeSize
;
793 st
->lpcSize
=mode
->lpcSize
;
794 speex_decoder_ctl(st
->st_low
, SPEEX_GET_SAMPLING_RATE
, &st
->sampling_rate
);
795 st
->sampling_rate
*=2;
797 speex_decoder_ctl(st
->st_low
, SPEEX_SET_WIDEBAND
, &tmp
);
799 st
->submodes
=mode
->submodes
;
800 st
->submodeID
=mode
->defaultSubmode
;
804 /* st->g0_mem = (spx_word32_t*)speex_alloc((QMF_ORDER)*sizeof(spx_word32_t)); */
805 /* st->g1_mem = (spx_word32_t*)speex_alloc((QMF_ORDER)*sizeof(spx_word32_t)); */
807 /* st->excBuf = (spx_word16_t*)speex_alloc((st->subframeSize)*sizeof(spx_word16_t)); */
809 /* st->old_qlsp = (spx_lsp_t*)speex_alloc((st->lpcSize)*sizeof(spx_lsp_t)); */
810 /* st->interp_qlpc = (spx_coef_t*)speex_alloc(st->lpcSize*sizeof(spx_coef_t)); */
812 /* st->pi_gain = (spx_word32_t*)speex_alloc((st->nbSubframes)*sizeof(spx_word32_t)); */
813 /*st->exc_rms = (spx_word16_t*)speex_alloc((st->nbSubframes)*sizeof(spx_word16_t)); */
814 /* st->mem_sp = (spx_mem_t*)speex_alloc((2*st->lpcSize)*sizeof(spx_mem_t)); */
816 st
->innov_save
= NULL
;
819 st
->lpc_enh_enabled
=0;
822 #ifdef ENABLE_VALGRIND
823 VALGRIND_MAKE_READABLE(st
, (st
->stack
-(char*)st
));
828 void sb_decoder_destroy(void *state
)
831 st
= (SBDecState
*)state
;
832 speex_decoder_destroy(st
->st_low
);
833 #if !(defined(VAR_ARRAYS) || defined (USE_ALLOCA))
834 /*speex_free_scratch(st->stack);*/
837 speex_free(st->g0_mem);
838 speex_free(st->g1_mem);
839 speex_free(st->excBuf);
840 speex_free(st->old_qlsp);
841 speex_free(st->interp_qlpc);
842 speex_free(st->pi_gain);
843 speex_free(st->exc_rms);
844 speex_free(st->mem_sp);
850 #ifndef ROCKBOX_VOICE_CODEC
851 static void sb_decode_lost(SBDecState
*st
, spx_word16_t
*out
, int dtx
, char *stack
)
858 saved_modeid
=st
->submodeID
;
861 bw_lpc(QCONST16(0.99f
,15), st
->interp_qlpc
, st
->interp_qlpc
, st
->lpcSize
);
867 /* Final signal synthesis from excitation */
870 st
->last_ener
= MULT16_16_Q15(QCONST16(.9f
,15),st
->last_ener
);
872 for (i
=0;i
<st
->frame_size
;i
++)
873 out
[i
+st
->frame_size
] = speex_rand(st
->last_ener
, &st
->seed
);
875 iir_mem16(out
+st
->frame_size
, st
->interp_qlpc
, out
+st
->frame_size
, st
->frame_size
, st
->lpcSize
,
879 /* Reconstruct the original */
880 qmf_synth(out
, out
+st
->frame_size
, h0
, out
, st
->full_frame_size
, QMF_ORDER
, st
->g0_mem
, st
->g1_mem
, stack
);
883 st
->submodeID
=saved_modeid
;
890 int sb_decode(void *state
, SpeexBits
*bits
, void *vout
)
897 VARDECL(spx_word32_t
*low_pi_gain
);
898 VARDECL(spx_word16_t
*low_exc_rms
);
899 VARDECL(spx_coef_t
*ak
);
900 VARDECL(spx_lsp_t
*qlsp
);
901 VARDECL(spx_lsp_t
*interp_qlsp
);
903 const SpeexSBMode
*mode
;
904 spx_word16_t
*out
= (spx_word16_t
*)vout
;
905 spx_word16_t
*low_innov_alias
;
906 spx_word32_t exc_ener_sum
= 0;
908 st
= (SBDecState
*)state
;
910 mode
= (const SpeexSBMode
*)(st
->mode
->mode
);
912 low_innov_alias
= out
+st
->frame_size
;
913 speex_decoder_ctl(st
->st_low
, SPEEX_SET_INNOVATION_SAVE
, low_innov_alias
);
914 /* Decode the low-band */
915 ret
= speex_decode_native(st
->st_low
, bits
, out
);
917 speex_decoder_ctl(st
->st_low
, SPEEX_GET_DTX_STATUS
, &dtx
);
919 /* If error decoding the narrowband part, propagate error */
925 #ifndef ROCKBOX_VOICE_CODEC
928 sb_decode_lost(st
, out
, dtx
, stack
);
933 if (st
->encode_submode
)
936 /*Check "wideband bit"*/
937 if (speex_bits_remaining(bits
)>0)
938 wideband
= speex_bits_peek(bits
);
943 /*Regular wideband frame, read the submode*/
944 wideband
= speex_bits_unpack_unsigned(bits
, 1);
945 st
->submodeID
= speex_bits_unpack_unsigned(bits
, SB_SUBMODE_BITS
);
948 /*Was a narrowband frame, set "null submode"*/
951 if (st
->submodeID
!= 0 && st
->submodes
[st
->submodeID
] == NULL
)
953 speex_notify("Invalid mode encountered. The stream is corrupted.");
958 /* If null mode (no transmission), just set a couple things to zero*/
959 if (st
->submodes
[st
->submodeID
] == NULL
)
961 #ifndef ROCKBOX_VOICE_CODEC
964 sb_decode_lost(st
, out
, 1, stack
);
969 for (i
=0;i
<st
->frame_size
;i
++)
970 out
[st
->frame_size
+i
]=VERY_SMALL
;
974 /* Final signal synthesis from excitation */
975 iir_mem16(out
+st
->frame_size
, st
->interp_qlpc
, out
+st
->frame_size
, st
->frame_size
, st
->lpcSize
, st
->mem_sp
, stack
);
977 qmf_synth(out
, out
+st
->frame_size
, h0
, out
, st
->full_frame_size
, QMF_ORDER
, st
->g0_mem
, st
->g1_mem
, stack
);
983 ALLOC(low_pi_gain
, st
->nbSubframes
, spx_word32_t
);
984 ALLOC(low_exc_rms
, st
->nbSubframes
, spx_word16_t
);
985 speex_decoder_ctl(st
->st_low
, SPEEX_GET_PI_GAIN
, low_pi_gain
);
986 speex_decoder_ctl(st
->st_low
, SPEEX_GET_EXC
, low_exc_rms
);
988 ALLOC(qlsp
, st
->lpcSize
, spx_lsp_t
);
989 ALLOC(interp_qlsp
, st
->lpcSize
, spx_lsp_t
);
990 SUBMODE(lsp_unquant
)(qlsp
, st
->lpcSize
, bits
);
994 for (i
=0;i
<st
->lpcSize
;i
++)
995 st
->old_qlsp
[i
] = qlsp
[i
];
998 ALLOC(ak
, st
->lpcSize
, spx_coef_t
);
1000 for (sub
=0;sub
<st
->nbSubframes
;sub
++)
1002 VARDECL(spx_word32_t
*exc
);
1003 spx_word16_t
*innov_save
=NULL
;
1005 spx_word16_t filter_ratio
;
1008 spx_word32_t rl
=0,rh
=0;
1010 offset
= st
->subframeSize
*sub
;
1011 sp
=out
+st
->frame_size
+offset
;
1012 ALLOC(exc
, st
->subframeSize
, spx_word32_t
);
1013 /* Pointer for saving innovation */
1016 innov_save
= st
->innov_save
+2*offset
;
1017 SPEEX_MEMSET(innov_save
, 0, 2*st
->subframeSize
);
1020 /* LSP interpolation */
1021 lsp_interpolate(st
->old_qlsp
, qlsp
, interp_qlsp
, st
->lpcSize
, sub
, st
->nbSubframes
);
1023 lsp_enforce_margin(interp_qlsp
, st
->lpcSize
, LSP_MARGIN
);
1026 lsp_to_lpc(interp_qlsp
, ak
, st
->lpcSize
, stack
);
1028 /* Calculate reponse ratio between the low and high filter in the middle
1029 of the band (4000 Hz) */
1031 st
->pi_gain
[sub
]=LPC_SCALING
;
1033 for (i
=0;i
<st
->lpcSize
;i
+=2)
1035 rh
+= ak
[i
+1] - ak
[i
];
1036 st
->pi_gain
[sub
] += ak
[i
] + ak
[i
+1];
1039 rl
= low_pi_gain
[sub
];
1041 filter_ratio
=EXTRACT16(SATURATE(PDIV32(SHL32(ADD32(rl
,82),7),ADD32(82,rh
)),32767));
1043 filter_ratio
=(rl
+.01)/(rh
+.01);
1046 SPEEX_MEMSET(exc
, 0, st
->subframeSize
);
1047 if (!SUBMODE(innovation_unquant
))
1052 quant
= speex_bits_unpack_unsigned(bits
, 5);
1053 g
= spx_exp(MULT16_16(QCONST16(.125f
,11),(quant
-10)));
1055 g
= PDIV32(g
, filter_ratio
);
1057 for (i
=0;i
<st
->subframeSize
;i
+=2)
1059 exc
[i
]=SHL32(MULT16_32_P15(MULT16_16_Q15(mode
->folding_gain
,low_innov_alias
[offset
+i
]),SHL32(g
,6)),SIG_SHIFT
);
1060 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
));
1066 int qgc
= speex_bits_unpack_unsigned(bits
, 4);
1068 el
= low_exc_rms
[sub
];
1069 gc
= MULT16_16_Q15(QCONST16(0.87360,15),gc_quant_bound
[qgc
]);
1071 if (st
->subframeSize
==80)
1072 gc
= MULT16_16_P14(QCONST16(1.4142f
,14),gc
);
1074 scale
= SHL32(PDIV32(SHL32(MULT16_16(gc
, el
),3), filter_ratio
),SIG_SHIFT
-3);
1075 SUBMODE(innovation_unquant
)(exc
, SUBMODE(innovation_params
), st
->subframeSize
,
1076 bits
, stack
, &st
->seed
);
1078 signal_mul(exc
,exc
,scale
,st
->subframeSize
);
1080 if (SUBMODE(double_codebook
)) {
1081 char *tmp_stack
=stack
;
1082 VARDECL(spx_sig_t
*innov2
);
1083 ALLOC(innov2
, st
->subframeSize
, spx_sig_t
);
1084 SPEEX_MEMSET(innov2
, 0, st
->subframeSize
);
1085 SUBMODE(innovation_unquant
)(innov2
, SUBMODE(innovation_params
), st
->subframeSize
,
1086 bits
, stack
, &st
->seed
);
1087 signal_mul(innov2
, innov2
, MULT16_32_P15(QCONST16(0.4f
,15),scale
), st
->subframeSize
);
1088 for (i
=0;i
<st
->subframeSize
;i
++)
1089 exc
[i
] = ADD32(exc
[i
],innov2
[i
]);
1097 for (i
=0;i
<st
->subframeSize
;i
++)
1098 innov_save
[2*i
]=EXTRACT16(PSHR32(exc
[i
],SIG_SHIFT
));
1101 iir_mem16(st
->excBuf
, st
->interp_qlpc
, sp
, st
->subframeSize
, st
->lpcSize
,
1103 for (i
=0;i
<st
->subframeSize
;i
++)
1104 st
->excBuf
[i
]=EXTRACT16(PSHR32(exc
[i
],SIG_SHIFT
));
1105 for (i
=0;i
<st
->lpcSize
;i
++)
1106 st
->interp_qlpc
[i
] = ak
[i
];
1107 st
->exc_rms
[sub
] = compute_rms16(st
->excBuf
, st
->subframeSize
);
1108 exc_ener_sum
= ADD32(exc_ener_sum
, DIV32(MULT16_16(st
->exc_rms
[sub
],st
->exc_rms
[sub
]), st
->nbSubframes
));
1110 st
->last_ener
= spx_sqrt(exc_ener_sum
);
1112 qmf_synth(out
, out
+st
->frame_size
, h0
, out
, st
->full_frame_size
, QMF_ORDER
, st
->g0_mem
, st
->g1_mem
, stack
);
1113 for (i
=0;i
<st
->lpcSize
;i
++)
1114 st
->old_qlsp
[i
] = qlsp
[i
];
1121 #ifndef SPEEX_DISABLE_ENCODER
1122 int sb_encoder_ctl(void *state
, int request
, void *ptr
)
1125 st
=(SBEncState
*)state
;
1128 case SPEEX_GET_FRAME_SIZE
:
1129 (*(spx_int32_t
*)ptr
) = st
->full_frame_size
;
1131 case SPEEX_SET_HIGH_MODE
:
1132 st
->submodeSelect
= st
->submodeID
= (*(spx_int32_t
*)ptr
);
1134 case SPEEX_SET_LOW_MODE
:
1135 speex_encoder_ctl(st
->st_low
, SPEEX_SET_LOW_MODE
, ptr
);
1138 speex_encoder_ctl(st
->st_low
, SPEEX_SET_DTX
, ptr
);
1141 speex_encoder_ctl(st
->st_low
, SPEEX_GET_DTX
, ptr
);
1143 case SPEEX_GET_LOW_MODE
:
1144 speex_encoder_ctl(st
->st_low
, SPEEX_GET_LOW_MODE
, ptr
);
1146 case SPEEX_SET_MODE
:
1147 speex_encoder_ctl(st
, SPEEX_SET_QUALITY
, ptr
);
1151 st
->vbr_enabled
= (*(spx_int32_t
*)ptr
);
1152 speex_encoder_ctl(st
->st_low
, SPEEX_SET_VBR
, ptr
);
1155 (*(spx_int32_t
*)ptr
) = st
->vbr_enabled
;
1158 st
->vad_enabled
= (*(spx_int32_t
*)ptr
);
1159 speex_encoder_ctl(st
->st_low
, SPEEX_SET_VAD
, ptr
);
1162 (*(spx_int32_t
*)ptr
) = st
->vad_enabled
;
1164 #endif /* #ifndef DISABLE_VBR */
1165 #if !defined(DISABLE_VBR) && !defined(DISABLE_FLOAT_API)
1166 case SPEEX_SET_VBR_QUALITY
:
1169 float qual
= (*(float*)ptr
)+.6;
1170 st
->vbr_quality
= (*(float*)ptr
);
1173 q
=(int)floor(.5+*(float*)ptr
);
1176 speex_encoder_ctl(st
->st_low
, SPEEX_SET_VBR_QUALITY
, &qual
);
1177 speex_encoder_ctl(state
, SPEEX_SET_QUALITY
, &q
);
1180 case SPEEX_GET_VBR_QUALITY
:
1181 (*(float*)ptr
) = st
->vbr_quality
;
1183 #endif /* #if !defined(DISABLE_VBR) && !defined(DISABLE_FLOAT_API) */
1186 st
->abr_enabled
= (*(spx_int32_t
*)ptr
);
1187 st
->vbr_enabled
= st
->abr_enabled
!=0;
1188 speex_encoder_ctl(st
->st_low
, SPEEX_SET_VBR
, &st
->vbr_enabled
);
1189 if (st
->vbr_enabled
)
1191 spx_int32_t i
=10, rate
, target
;
1193 target
= (*(spx_int32_t
*)ptr
);
1196 speex_encoder_ctl(st
, SPEEX_SET_QUALITY
, &i
);
1197 speex_encoder_ctl(st
, SPEEX_GET_BITRATE
, &rate
);
1205 speex_encoder_ctl(st
, SPEEX_SET_VBR_QUALITY
, &vbr_qual
);
1213 (*(spx_int32_t
*)ptr
) = st
->abr_enabled
;
1215 #endif /* #ifndef DISABLE_VBR */
1217 case SPEEX_SET_QUALITY
:
1219 spx_int32_t nb_qual
;
1220 int quality
= (*(spx_int32_t
*)ptr
);
1225 st
->submodeSelect
= st
->submodeID
= ((const SpeexSBMode
*)(st
->mode
->mode
))->quality_map
[quality
];
1226 nb_qual
= ((const SpeexSBMode
*)(st
->mode
->mode
))->low_quality_map
[quality
];
1227 speex_encoder_ctl(st
->st_low
, SPEEX_SET_MODE
, &nb_qual
);
1230 case SPEEX_SET_COMPLEXITY
:
1231 speex_encoder_ctl(st
->st_low
, SPEEX_SET_COMPLEXITY
, ptr
);
1232 st
->complexity
= (*(spx_int32_t
*)ptr
);
1233 if (st
->complexity
<1)
1236 case SPEEX_GET_COMPLEXITY
:
1237 (*(spx_int32_t
*)ptr
) = st
->complexity
;
1239 case SPEEX_SET_BITRATE
:
1242 spx_int32_t rate
, target
;
1243 target
= (*(spx_int32_t
*)ptr
);
1246 speex_encoder_ctl(st
, SPEEX_SET_QUALITY
, &i
);
1247 speex_encoder_ctl(st
, SPEEX_GET_BITRATE
, &rate
);
1254 case SPEEX_GET_BITRATE
:
1255 speex_encoder_ctl(st
->st_low
, request
, ptr
);
1256 /*fprintf (stderr, "before: %d\n", (*(int*)ptr));*/
1257 if (st
->submodes
[st
->submodeID
])
1258 (*(spx_int32_t
*)ptr
) += st
->sampling_rate
*SUBMODE(bits_per_frame
)/st
->full_frame_size
;
1260 (*(spx_int32_t
*)ptr
) += st
->sampling_rate
*(SB_SUBMODE_BITS
+1)/st
->full_frame_size
;
1261 /*fprintf (stderr, "after: %d\n", (*(int*)ptr));*/
1263 case SPEEX_SET_SAMPLING_RATE
:
1265 spx_int32_t tmp
=(*(spx_int32_t
*)ptr
);
1266 st
->sampling_rate
= tmp
;
1268 speex_encoder_ctl(st
->st_low
, SPEEX_SET_SAMPLING_RATE
, &tmp
);
1271 case SPEEX_GET_SAMPLING_RATE
:
1272 (*(spx_int32_t
*)ptr
)=st
->sampling_rate
;
1274 case SPEEX_RESET_STATE
:
1278 for (i
=0;i
<st
->lpcSize
;i
++)
1279 st
->old_lsp
[i
]= DIV32(MULT16_16(QCONST16(3.1415927f
, LSP_SHIFT
), i
+1), st
->lpcSize
+1);
1280 for (i
=0;i
<st
->lpcSize
;i
++)
1281 st
->mem_sw
[i
]=st
->mem_sp
[i
]=st
->mem_sp2
[i
]=0;
1282 for (i
=0;i
<QMF_ORDER
;i
++)
1283 st
->h0_mem
[i
]=st
->h1_mem
[i
]=0;
1286 case SPEEX_SET_SUBMODE_ENCODING
:
1287 st
->encode_submode
= (*(spx_int32_t
*)ptr
);
1288 speex_encoder_ctl(st
->st_low
, SPEEX_SET_SUBMODE_ENCODING
, ptr
);
1290 case SPEEX_GET_SUBMODE_ENCODING
:
1291 (*(spx_int32_t
*)ptr
) = st
->encode_submode
;
1293 case SPEEX_GET_LOOKAHEAD
:
1294 speex_encoder_ctl(st
->st_low
, SPEEX_GET_LOOKAHEAD
, ptr
);
1295 (*(spx_int32_t
*)ptr
) = 2*(*(spx_int32_t
*)ptr
) + QMF_ORDER
- 1;
1297 case SPEEX_SET_PLC_TUNING
:
1298 speex_encoder_ctl(st
->st_low
, SPEEX_SET_PLC_TUNING
, ptr
);
1300 case SPEEX_GET_PLC_TUNING
:
1301 speex_encoder_ctl(st
->st_low
, SPEEX_GET_PLC_TUNING
, ptr
);
1304 case SPEEX_SET_VBR_MAX_BITRATE
:
1306 st
->vbr_max
= (*(spx_int32_t
*)ptr
);
1307 if (SPEEX_SET_VBR_MAX_BITRATE
<1)
1309 speex_encoder_ctl(st
->st_low
, SPEEX_SET_VBR_MAX_BITRATE
, &st
->vbr_max
);
1310 st
->vbr_max_high
= 17600;
1312 spx_int32_t low_rate
;
1313 if (st
->vbr_max
>= 42200)
1315 st
->vbr_max_high
= 17600;
1316 } else if (st
->vbr_max
>= 27800)
1318 st
->vbr_max_high
= 9600;
1319 } else if (st
->vbr_max
> 20600)
1321 st
->vbr_max_high
= 5600;
1323 st
->vbr_max_high
= 1800;
1325 if (st
->subframeSize
==80)
1326 st
->vbr_max_high
= 1800;
1327 low_rate
= st
->vbr_max
- st
->vbr_max_high
;
1328 speex_encoder_ctl(st
->st_low
, SPEEX_SET_VBR_MAX_BITRATE
, &low_rate
);
1332 case SPEEX_GET_VBR_MAX_BITRATE
:
1333 (*(spx_int32_t
*)ptr
) = st
->vbr_max
;
1335 #endif /* #ifndef DISABLE_VBR */
1336 case SPEEX_SET_HIGHPASS
:
1337 speex_encoder_ctl(st
->st_low
, SPEEX_SET_HIGHPASS
, ptr
);
1339 case SPEEX_GET_HIGHPASS
:
1340 speex_encoder_ctl(st
->st_low
, SPEEX_GET_HIGHPASS
, ptr
);
1344 /* This is all internal stuff past this point */
1345 case SPEEX_GET_PI_GAIN
:
1348 spx_word32_t
*g
= (spx_word32_t
*)ptr
;
1349 for (i
=0;i
<st
->nbSubframes
;i
++)
1350 g
[i
]=st
->pi_gain
[i
];
1356 for (i
=0;i
<st
->nbSubframes
;i
++)
1357 ((spx_word16_t
*)ptr
)[i
] = st
->exc_rms
[i
];
1361 case SPEEX_GET_RELATIVE_QUALITY
:
1362 (*(float*)ptr
)=st
->relative_quality
;
1364 #endif /* #ifndef DISABLE_VBR */
1365 case SPEEX_SET_INNOVATION_SAVE
:
1366 st
->innov_rms_save
= (spx_word16_t
*)ptr
;
1368 case SPEEX_SET_WIDEBAND
:
1369 speex_encoder_ctl(st
->st_low
, SPEEX_SET_WIDEBAND
, ptr
);
1371 case SPEEX_GET_STACK
:
1372 *((char**)ptr
) = st
->stack
;
1375 speex_warning_int("Unknown nb_ctl request: ", request
);
1380 #endif /* SPEEX_DISABLE_ENCODER */
1382 int sb_decoder_ctl(void *state
, int request
, void *ptr
)
1385 st
=(SBDecState
*)state
;
1388 case SPEEX_SET_HIGH_MODE
:
1389 st
->submodeID
= (*(spx_int32_t
*)ptr
);
1391 case SPEEX_SET_LOW_MODE
:
1392 speex_decoder_ctl(st
->st_low
, SPEEX_SET_LOW_MODE
, ptr
);
1394 case SPEEX_GET_LOW_MODE
:
1395 speex_decoder_ctl(st
->st_low
, SPEEX_GET_LOW_MODE
, ptr
);
1397 case SPEEX_GET_FRAME_SIZE
:
1398 (*(spx_int32_t
*)ptr
) = st
->full_frame_size
;
1401 speex_decoder_ctl(st
->st_low
, request
, ptr
);
1402 st
->lpc_enh_enabled
= *((spx_int32_t
*)ptr
);
1405 *((spx_int32_t
*)ptr
) = st
->lpc_enh_enabled
;
1407 case SPEEX_SET_MODE
:
1408 case SPEEX_SET_QUALITY
:
1410 spx_int32_t nb_qual
;
1411 int quality
= (*(spx_int32_t
*)ptr
);
1416 st
->submodeID
= ((const SpeexSBMode
*)(st
->mode
->mode
))->quality_map
[quality
];
1417 nb_qual
= ((const SpeexSBMode
*)(st
->mode
->mode
))->low_quality_map
[quality
];
1418 speex_decoder_ctl(st
->st_low
, SPEEX_SET_MODE
, &nb_qual
);
1421 case SPEEX_GET_BITRATE
:
1422 speex_decoder_ctl(st
->st_low
, request
, ptr
);
1423 if (st
->submodes
[st
->submodeID
])
1424 (*(spx_int32_t
*)ptr
) += st
->sampling_rate
*SUBMODE(bits_per_frame
)/st
->full_frame_size
;
1426 (*(spx_int32_t
*)ptr
) += st
->sampling_rate
*(SB_SUBMODE_BITS
+1)/st
->full_frame_size
;
1428 case SPEEX_SET_SAMPLING_RATE
:
1430 spx_int32_t tmp
=(*(spx_int32_t
*)ptr
);
1431 st
->sampling_rate
= tmp
;
1433 speex_decoder_ctl(st
->st_low
, SPEEX_SET_SAMPLING_RATE
, &tmp
);
1436 case SPEEX_GET_SAMPLING_RATE
:
1437 (*(spx_int32_t
*)ptr
)=st
->sampling_rate
;
1439 case SPEEX_SET_HANDLER
:
1440 speex_decoder_ctl(st
->st_low
, SPEEX_SET_HANDLER
, ptr
);
1442 case SPEEX_SET_USER_HANDLER
:
1443 speex_decoder_ctl(st
->st_low
, SPEEX_SET_USER_HANDLER
, ptr
);
1445 case SPEEX_RESET_STATE
:
1448 for (i
=0;i
<2*st
->lpcSize
;i
++)
1450 for (i
=0;i
<QMF_ORDER
;i
++)
1451 st
->g0_mem
[i
]=st
->g1_mem
[i
]=0;
1455 case SPEEX_SET_SUBMODE_ENCODING
:
1456 st
->encode_submode
= (*(spx_int32_t
*)ptr
);
1457 speex_decoder_ctl(st
->st_low
, SPEEX_SET_SUBMODE_ENCODING
, ptr
);
1459 case SPEEX_GET_SUBMODE_ENCODING
:
1460 (*(spx_int32_t
*)ptr
) = st
->encode_submode
;
1462 case SPEEX_GET_LOOKAHEAD
:
1463 speex_decoder_ctl(st
->st_low
, SPEEX_GET_LOOKAHEAD
, ptr
);
1464 (*(spx_int32_t
*)ptr
) = 2*(*(spx_int32_t
*)ptr
);
1466 case SPEEX_SET_HIGHPASS
:
1467 speex_decoder_ctl(st
->st_low
, SPEEX_SET_HIGHPASS
, ptr
);
1469 case SPEEX_GET_HIGHPASS
:
1470 speex_decoder_ctl(st
->st_low
, SPEEX_GET_HIGHPASS
, ptr
);
1472 case SPEEX_GET_ACTIVITY
:
1473 speex_decoder_ctl(st
->st_low
, SPEEX_GET_ACTIVITY
, ptr
);
1475 case SPEEX_GET_PI_GAIN
:
1478 spx_word32_t
*g
= (spx_word32_t
*)ptr
;
1479 for (i
=0;i
<st
->nbSubframes
;i
++)
1480 g
[i
]=st
->pi_gain
[i
];
1486 for (i
=0;i
<st
->nbSubframes
;i
++)
1487 ((spx_word16_t
*)ptr
)[i
] = st
->exc_rms
[i
];
1490 case SPEEX_GET_DTX_STATUS
:
1491 speex_decoder_ctl(st
->st_low
, SPEEX_GET_DTX_STATUS
, ptr
);
1493 case SPEEX_SET_INNOVATION_SAVE
:
1494 st
->innov_save
= (spx_word16_t
*)ptr
;
1496 case SPEEX_SET_WIDEBAND
:
1497 speex_decoder_ctl(st
->st_low
, SPEEX_SET_WIDEBAND
, ptr
);
1499 case SPEEX_GET_STACK
:
1500 *((char**)ptr
) = st
->stack
;
1503 speex_warning_int("Unknown nb_ctl request: ", request
);