Coverage Report

Created: 2025-07-12 06:18

/src/speex/libspeex/modes.c
Line
Count
Source (jump to first uncovered line)
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
#ifdef DISABLE_ENCODER
54
#define nb_encoder_init NULL
55
#define nb_encoder_destroy NULL
56
#define nb_encode NULL
57
#define nb_encoder_ctl NULL
58
59
#define split_cb_search_shape_sign NULL
60
#define noise_codebook_quant NULL
61
#define pitch_search_3tap NULL
62
#define forced_pitch_quant NULL
63
#define lsp_quant_nb NULL
64
#define lsp_quant_lbr NULL
65
#endif /* DISABLE_ENCODER */
66
67
#ifdef DISABLE_DECODER
68
#define nb_decoder_init NULL
69
#define nb_decoder_destroy NULL
70
#define nb_decode NULL
71
#define nb_decoder_ctl NULL
72
73
#define noise_codebook_unquant NULL
74
#define split_cb_shape_sign_unquant NULL
75
#define lsp_unquant_nb NULL
76
#define lsp_unquant_lbr NULL
77
#define pitch_unquant_3tap NULL
78
#define forced_pitch_unquant NULL
79
#endif /* DISABLE_DECODER */
80
81
/* Extern declarations for all codebooks we use here */
82
extern const signed char gain_cdbk_nb[];
83
extern const signed char gain_cdbk_lbr[];
84
extern const signed char exc_5_256_table[];
85
extern const signed char exc_5_64_table[];
86
extern const signed char exc_8_128_table[];
87
extern const signed char exc_10_32_table[];
88
extern const signed char exc_10_16_table[];
89
extern const signed char exc_20_32_table[];
90
91
92
/* Parameters for Long-Term Prediction (LTP)*/
93
static const ltp_params ltp_params_nb = {
94
   gain_cdbk_nb,
95
   7,
96
   7
97
};
98
99
/* Parameters for Long-Term Prediction (LTP)*/
100
static const ltp_params ltp_params_vlbr = {
101
   gain_cdbk_lbr,
102
   5,
103
   0
104
};
105
106
/* Parameters for Long-Term Prediction (LTP)*/
107
static const ltp_params ltp_params_lbr = {
108
   gain_cdbk_lbr,
109
   5,
110
   7
111
};
112
113
/* Parameters for Long-Term Prediction (LTP)*/
114
static const ltp_params ltp_params_med = {
115
   gain_cdbk_lbr,
116
   5,
117
   7
118
};
119
120
/* Split-VQ innovation parameters for very low bit-rate narrowband */
121
static const split_cb_params split_cb_nb_vlbr = {
122
   10,               /*subvect_size*/
123
   4,               /*nb_subvect*/
124
   exc_10_16_table, /*shape_cb*/
125
   4,               /*shape_bits*/
126
   0,
127
};
128
129
/* Split-VQ innovation parameters for very low bit-rate narrowband */
130
static const split_cb_params split_cb_nb_ulbr = {
131
   20,               /*subvect_size*/
132
   2,               /*nb_subvect*/
133
   exc_20_32_table, /*shape_cb*/
134
   5,               /*shape_bits*/
135
   0,
136
};
137
138
/* Split-VQ innovation parameters for low bit-rate narrowband */
139
static const split_cb_params split_cb_nb_lbr = {
140
   10,              /*subvect_size*/
141
   4,               /*nb_subvect*/
142
   exc_10_32_table, /*shape_cb*/
143
   5,               /*shape_bits*/
144
   0,
145
};
146
147
148
/* Split-VQ innovation parameters narrowband */
149
static const split_cb_params split_cb_nb = {
150
   5,               /*subvect_size*/
151
   8,               /*nb_subvect*/
152
   exc_5_64_table, /*shape_cb*/
153
   6,               /*shape_bits*/
154
   0,
155
};
156
157
/* Split-VQ innovation parameters narrowband */
158
static const split_cb_params split_cb_nb_med = {
159
   8,               /*subvect_size*/
160
   5,               /*nb_subvect*/
161
   exc_8_128_table, /*shape_cb*/
162
   7,               /*shape_bits*/
163
   0,
164
};
165
166
/* Split-VQ innovation for low-band wideband */
167
static const split_cb_params split_cb_sb = {
168
   5,               /*subvect_size*/
169
   8,              /*nb_subvect*/
170
   exc_5_256_table,    /*shape_cb*/
171
   8,               /*shape_bits*/
172
   0,
173
};
174
175
176
177
/* 2150 bps "vocoder-like" mode for comfort noise */
178
static const SpeexSubmode nb_submode1 = {
179
   0,
180
   1,
181
   0,
182
   0,
183
   /* LSP quantization */
184
   lsp_quant_lbr,
185
   lsp_unquant_lbr,
186
   /* No pitch quantization */
187
   forced_pitch_quant,
188
   forced_pitch_unquant,
189
   NULL,
190
   /* No innovation quantization (noise only) */
191
   noise_codebook_quant,
192
   noise_codebook_unquant,
193
   NULL,
194
   -1,
195
   43
196
};
197
198
/* 3.95 kbps very low bit-rate mode */
199
static const SpeexSubmode nb_submode8 = {
200
   0,
201
   1,
202
   0,
203
   0,
204
   /*LSP quantization*/
205
   lsp_quant_lbr,
206
   lsp_unquant_lbr,
207
   /*No pitch quantization*/
208
   forced_pitch_quant,
209
   forced_pitch_unquant,
210
   NULL,
211
   /*Innovation quantization*/
212
   split_cb_search_shape_sign,
213
   split_cb_shape_sign_unquant,
214
   &split_cb_nb_ulbr,
215
   QCONST16(.5,15),
216
   79
217
};
218
219
/* 5.95 kbps very low bit-rate mode */
220
static const SpeexSubmode nb_submode2 = {
221
   0,
222
   0,
223
   0,
224
   0,
225
   /*LSP quantization*/
226
   lsp_quant_lbr,
227
   lsp_unquant_lbr,
228
   /*No pitch quantization*/
229
   pitch_search_3tap,
230
   pitch_unquant_3tap,
231
   &ltp_params_vlbr,
232
   /*Innovation quantization*/
233
   split_cb_search_shape_sign,
234
   split_cb_shape_sign_unquant,
235
   &split_cb_nb_vlbr,
236
   QCONST16(.6,15),
237
   119
238
};
239
240
/* 8 kbps low bit-rate mode */
241
static const SpeexSubmode nb_submode3 = {
242
   -1,
243
   0,
244
   1,
245
   0,
246
   /*LSP quantization*/
247
   lsp_quant_lbr,
248
   lsp_unquant_lbr,
249
   /*Pitch quantization*/
250
   pitch_search_3tap,
251
   pitch_unquant_3tap,
252
   &ltp_params_lbr,
253
   /*Innovation quantization*/
254
   split_cb_search_shape_sign,
255
   split_cb_shape_sign_unquant,
256
   &split_cb_nb_lbr,
257
   QCONST16(.55,15),
258
   160
259
};
260
261
/* 11 kbps medium bit-rate mode */
262
static const SpeexSubmode nb_submode4 = {
263
   -1,
264
   0,
265
   1,
266
   0,
267
   /*LSP quantization*/
268
   lsp_quant_lbr,
269
   lsp_unquant_lbr,
270
   /*Pitch quantization*/
271
   pitch_search_3tap,
272
   pitch_unquant_3tap,
273
   &ltp_params_med,
274
   /*Innovation quantization*/
275
   split_cb_search_shape_sign,
276
   split_cb_shape_sign_unquant,
277
   &split_cb_nb_med,
278
   QCONST16(.45,15),
279
   220
280
};
281
282
/* 15 kbps high bit-rate mode */
283
static const SpeexSubmode nb_submode5 = {
284
   -1,
285
   0,
286
   3,
287
   0,
288
   /*LSP quantization*/
289
   lsp_quant_nb,
290
   lsp_unquant_nb,
291
   /*Pitch quantization*/
292
   pitch_search_3tap,
293
   pitch_unquant_3tap,
294
   &ltp_params_nb,
295
   /*Innovation quantization*/
296
   split_cb_search_shape_sign,
297
   split_cb_shape_sign_unquant,
298
   &split_cb_nb,
299
   QCONST16(.25,15),
300
   300
301
};
302
303
/* 18.2 high bit-rate mode */
304
static const SpeexSubmode nb_submode6 = {
305
   -1,
306
   0,
307
   3,
308
   0,
309
   /*LSP quantization*/
310
   lsp_quant_nb,
311
   lsp_unquant_nb,
312
   /*Pitch quantization*/
313
   pitch_search_3tap,
314
   pitch_unquant_3tap,
315
   &ltp_params_nb,
316
   /*Innovation quantization*/
317
   split_cb_search_shape_sign,
318
   split_cb_shape_sign_unquant,
319
   &split_cb_sb,
320
   QCONST16(.15,15),
321
   364
322
};
323
324
/* 24.6 kbps high bit-rate mode */
325
static const SpeexSubmode nb_submode7 = {
326
   -1,
327
   0,
328
   3,
329
   1,
330
   /*LSP quantization*/
331
   lsp_quant_nb,
332
   lsp_unquant_nb,
333
   /*Pitch quantization*/
334
   pitch_search_3tap,
335
   pitch_unquant_3tap,
336
   &ltp_params_nb,
337
   /*Innovation quantization*/
338
   split_cb_search_shape_sign,
339
   split_cb_shape_sign_unquant,
340
   &split_cb_nb,
341
   QCONST16(.05,15),
342
   492
343
};
344
345
346
/* Default mode for narrowband */
347
static const SpeexNBMode nb_mode = {
348
   NB_FRAME_SIZE,    /*frameSize*/
349
   NB_SUBFRAME_SIZE, /*subframeSize*/
350
   NB_ORDER,         /*lpcSize*/
351
   NB_PITCH_START,               /*pitchStart*/
352
   NB_PITCH_END,              /*pitchEnd*/
353
   QCONST16(0.92,15),  /* gamma1 */
354
   QCONST16(0.6,15),   /* gamma2 */
355
   QCONST16(.0002,15), /*lpc_floor*/
356
   {NULL, &nb_submode1, &nb_submode2, &nb_submode3, &nb_submode4, &nb_submode5, &nb_submode6, &nb_submode7,
357
   &nb_submode8, NULL, NULL, NULL, NULL, NULL, NULL, NULL},
358
   5,
359
   {1, 8, 2, 3, 3, 4, 4, 5, 5, 6, 7}
360
};
361
362
363
/* Default mode for narrowband */
364
EXPORT const SpeexMode speex_nb_mode = {
365
   &nb_mode,
366
   nb_mode_query,
367
   "narrowband",
368
   0,
369
   4,
370
   nb_encoder_init,
371
   nb_encoder_destroy,
372
   nb_encode,
373
   nb_decoder_init,
374
   nb_decoder_destroy,
375
   nb_decode,
376
   nb_encoder_ctl,
377
   nb_decoder_ctl,
378
};
379
380
381
382
EXPORT int speex_mode_query(const SpeexMode *mode, int request, void *ptr)
383
0
{
384
0
   return mode->query(mode->mode, request, ptr);
385
0
}
386
387
#ifdef FIXED_DEBUG
388
long long spx_mips=0;
389
#endif
390