1 /* Copyright (C) 2002-2006 Jean-Marc Valin
2    File: modes.c
3 
4    Describes the different modes of the codec
5 
6    Redistribution and use in source and binary forms, with or without
7    modification, are permitted provided that the following conditions
8    are met:
9 
10    - Redistributions of source code must retain the above copyright
11    notice, this list of conditions and the following disclaimer.
12 
13    - Redistributions in binary form must reproduce the above copyright
14    notice, this list of conditions and the following disclaimer in the
15    documentation and/or other materials provided with the distribution.
16 
17    - Neither the name of the Xiph.org Foundation nor the names of its
18    contributors may be used to endorse or promote products derived from
19    this software without specific prior written permission.
20 
21    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22    ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24    A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
25    CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26    EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27    PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28    PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29    LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 
33 */
34 
35 #ifdef HAVE_CONFIG_H
36 #include "config.h"
37 #endif
38 
39 #include "modes.h"
40 #include "ltp.h"
41 #include "quant_lsp.h"
42 #include "cb_search.h"
43 #include "sb_celp.h"
44 #include "nb_celp.h"
45 #include "vbr.h"
46 #include "arch.h"
47 #include <math.h>
48 
49 #ifndef NULL
50 #define NULL 0
51 #endif
52 
53 
54 /* Extern declarations for all codebooks we use here */
55 extern const signed char gain_cdbk_nb[];
56 extern const signed char gain_cdbk_lbr[];
57 extern const signed char exc_5_256_table[];
58 extern const signed char exc_5_64_table[];
59 extern const signed char exc_8_128_table[];
60 extern const signed char exc_10_32_table[];
61 extern const signed char exc_10_16_table[];
62 extern const signed char exc_20_32_table[];
63 
64 
65 /* Parameters for Long-Term Prediction (LTP)*/
66 static const ltp_params ltp_params_nb = {
67    gain_cdbk_nb,
68    7,
69    7
70 };
71 
72 /* Parameters for Long-Term Prediction (LTP)*/
73 static const ltp_params ltp_params_vlbr = {
74    gain_cdbk_lbr,
75    5,
76    0
77 };
78 
79 /* Parameters for Long-Term Prediction (LTP)*/
80 static const ltp_params ltp_params_lbr = {
81    gain_cdbk_lbr,
82    5,
83    7
84 };
85 
86 /* Parameters for Long-Term Prediction (LTP)*/
87 static const ltp_params ltp_params_med = {
88    gain_cdbk_lbr,
89    5,
90    7
91 };
92 
93 /* Split-VQ innovation parameters for very low bit-rate narrowband */
94 static const split_cb_params split_cb_nb_vlbr = {
95    10,               /*subvect_size*/
96    4,               /*nb_subvect*/
97    exc_10_16_table, /*shape_cb*/
98    4,               /*shape_bits*/
99    0,
100 };
101 
102 /* Split-VQ innovation parameters for very low bit-rate narrowband */
103 static const split_cb_params split_cb_nb_ulbr = {
104    20,               /*subvect_size*/
105    2,               /*nb_subvect*/
106    exc_20_32_table, /*shape_cb*/
107    5,               /*shape_bits*/
108    0,
109 };
110 
111 /* Split-VQ innovation parameters for low bit-rate narrowband */
112 static const split_cb_params split_cb_nb_lbr = {
113    10,              /*subvect_size*/
114    4,               /*nb_subvect*/
115    exc_10_32_table, /*shape_cb*/
116    5,               /*shape_bits*/
117    0,
118 };
119 
120 
121 /* Split-VQ innovation parameters narrowband */
122 static const split_cb_params split_cb_nb = {
123    5,               /*subvect_size*/
124    8,               /*nb_subvect*/
125    exc_5_64_table, /*shape_cb*/
126    6,               /*shape_bits*/
127    0,
128 };
129 
130 /* Split-VQ innovation parameters narrowband */
131 static const split_cb_params split_cb_nb_med = {
132    8,               /*subvect_size*/
133    5,               /*nb_subvect*/
134    exc_8_128_table, /*shape_cb*/
135    7,               /*shape_bits*/
136    0,
137 };
138 
139 /* Split-VQ innovation for low-band wideband */
140 static const split_cb_params split_cb_sb = {
141    5,               /*subvect_size*/
142    8,              /*nb_subvect*/
143    exc_5_256_table,    /*shape_cb*/
144    8,               /*shape_bits*/
145    0,
146 };
147 
148 
149 
150 /* 2150 bps "vocoder-like" mode for comfort noise */
151 static const SpeexSubmode nb_submode1 = {
152    0,
153    1,
154    0,
155    0,
156    /* LSP quantization */
157    lsp_quant_lbr,
158    lsp_unquant_lbr,
159    /* No pitch quantization */
160    forced_pitch_quant,
161    forced_pitch_unquant,
162    NULL,
163    /* No innovation quantization (noise only) */
164    noise_codebook_quant,
165    noise_codebook_unquant,
166    NULL,
167    -1,
168    43
169 };
170 
171 /* 3.95 kbps very low bit-rate mode */
172 static const SpeexSubmode nb_submode8 = {
173    0,
174    1,
175    0,
176    0,
177    /*LSP quantization*/
178    lsp_quant_lbr,
179    lsp_unquant_lbr,
180    /*No pitch quantization*/
181    forced_pitch_quant,
182    forced_pitch_unquant,
183    NULL,
184    /*Innovation quantization*/
185    split_cb_search_shape_sign,
186    split_cb_shape_sign_unquant,
187    &split_cb_nb_ulbr,
188    QCONST16(.5,15),
189    79
190 };
191 
192 /* 5.95 kbps very low bit-rate mode */
193 static const SpeexSubmode nb_submode2 = {
194    0,
195    0,
196    0,
197    0,
198    /*LSP quantization*/
199    lsp_quant_lbr,
200    lsp_unquant_lbr,
201    /*No pitch quantization*/
202    pitch_search_3tap,
203    pitch_unquant_3tap,
204    &ltp_params_vlbr,
205    /*Innovation quantization*/
206    split_cb_search_shape_sign,
207    split_cb_shape_sign_unquant,
208    &split_cb_nb_vlbr,
209    QCONST16(.6,15),
210    119
211 };
212 
213 /* 8 kbps low bit-rate mode */
214 static const SpeexSubmode nb_submode3 = {
215    -1,
216    0,
217    1,
218    0,
219    /*LSP quantization*/
220    lsp_quant_lbr,
221    lsp_unquant_lbr,
222    /*Pitch quantization*/
223    pitch_search_3tap,
224    pitch_unquant_3tap,
225    &ltp_params_lbr,
226    /*Innovation quantization*/
227    split_cb_search_shape_sign,
228    split_cb_shape_sign_unquant,
229    &split_cb_nb_lbr,
230    QCONST16(.55,15),
231    160
232 };
233 
234 /* 11 kbps medium bit-rate mode */
235 static const SpeexSubmode nb_submode4 = {
236    -1,
237    0,
238    1,
239    0,
240    /*LSP quantization*/
241    lsp_quant_lbr,
242    lsp_unquant_lbr,
243    /*Pitch quantization*/
244    pitch_search_3tap,
245    pitch_unquant_3tap,
246    &ltp_params_med,
247    /*Innovation quantization*/
248    split_cb_search_shape_sign,
249    split_cb_shape_sign_unquant,
250    &split_cb_nb_med,
251    QCONST16(.45,15),
252    220
253 };
254 
255 /* 15 kbps high bit-rate mode */
256 static const SpeexSubmode nb_submode5 = {
257    -1,
258    0,
259    3,
260    0,
261    /*LSP quantization*/
262    lsp_quant_nb,
263    lsp_unquant_nb,
264    /*Pitch quantization*/
265    pitch_search_3tap,
266    pitch_unquant_3tap,
267    &ltp_params_nb,
268    /*Innovation quantization*/
269    split_cb_search_shape_sign,
270    split_cb_shape_sign_unquant,
271    &split_cb_nb,
272    QCONST16(.3,15),
273    300
274 };
275 
276 /* 18.2 high bit-rate mode */
277 static const SpeexSubmode nb_submode6 = {
278    -1,
279    0,
280    3,
281    0,
282    /*LSP quantization*/
283    lsp_quant_nb,
284    lsp_unquant_nb,
285    /*Pitch quantization*/
286    pitch_search_3tap,
287    pitch_unquant_3tap,
288    &ltp_params_nb,
289    /*Innovation quantization*/
290    split_cb_search_shape_sign,
291    split_cb_shape_sign_unquant,
292    &split_cb_sb,
293    QCONST16(.2,15),
294    364
295 };
296 
297 /* 24.6 kbps high bit-rate mode */
298 static const SpeexSubmode nb_submode7 = {
299    -1,
300    0,
301    3,
302    1,
303    /*LSP quantization*/
304    lsp_quant_nb,
305    lsp_unquant_nb,
306    /*Pitch quantization*/
307    pitch_search_3tap,
308    pitch_unquant_3tap,
309    &ltp_params_nb,
310    /*Innovation quantization*/
311    split_cb_search_shape_sign,
312    split_cb_shape_sign_unquant,
313    &split_cb_nb,
314    QCONST16(.1,15),
315    492
316 };
317 
318 
319 /* Default mode for narrowband */
320 static const SpeexNBMode nb_mode = {
321    160,    /*frameSize*/
322    40,     /*subframeSize*/
323    10,     /*lpcSize*/
324    17,     /*pitchStart*/
325    144,    /*pitchEnd*/
326 #ifdef FIXED_POINT
327    29491, 19661, /* gamma1, gamma2 */
328 #else
329    0.9, 0.6, /* gamma1, gamma2 */
330 #endif
331    QCONST16(.0002,15), /*lpc_floor*/
332    {NULL, &nb_submode1, &nb_submode2, &nb_submode3, &nb_submode4, &nb_submode5, &nb_submode6, &nb_submode7,
333    &nb_submode8, NULL, NULL, NULL, NULL, NULL, NULL, NULL},
334    5,
335    {1, 8, 2, 3, 3, 4, 4, 5, 5, 6, 7}
336 };
337 
338 
339 /* Default mode for narrowband */
340 EXPORT const SpeexMode speex_nb_mode = {
341    &nb_mode,
342    nb_mode_query,
343    "narrowband",
344    0,
345    4,
346    &nb_encoder_init,
347    &nb_encoder_destroy,
348    &nb_encode,
349    &nb_decoder_init,
350    &nb_decoder_destroy,
351    &nb_decode,
352    &nb_encoder_ctl,
353    &nb_decoder_ctl,
354 };
355 
356 
357 
speex_mode_query(const SpeexMode * mode,int request,void * ptr)358 EXPORT int speex_mode_query(const SpeexMode *mode, int request, void *ptr)
359 {
360    return mode->query(mode->mode, request, ptr);
361 }
362 
363 #ifdef FIXED_DEBUG
364 long long spx_mips=0;
365 #endif
366 
367