modes.c 9.06 KB
Newer Older
Tristan Matthews's avatar
Tristan Matthews committed
1
/* Copyright (C) 2002-2006 Jean-Marc Valin
jmvalin's avatar
jmvalin committed
2 3 4 5
   File: modes.c

   Describes the different modes of the codec

jm's avatar
jm committed
6 7 8
   Redistribution and use in source and binary forms, with or without
   modification, are permitted provided that the following conditions
   are met:
Tristan Matthews's avatar
Tristan Matthews committed
9

jm's avatar
jm committed
10 11
   - Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
Tristan Matthews's avatar
Tristan Matthews committed
12

jm's avatar
jm committed
13 14 15
   - Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.
Tristan Matthews's avatar
Tristan Matthews committed
16

jm's avatar
jm committed
17 18 19
   - Neither the name of the Xiph.org Foundation nor the names of its
   contributors may be used to endorse or promote products derived from
   this software without specific prior written permission.
Tristan Matthews's avatar
Tristan Matthews committed
20

jm's avatar
jm committed
21 22 23 24 25 26 27 28 29 30 31
   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
   A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
jmvalin's avatar
jmvalin committed
32 33 34

*/

35 36 37 38
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

jmvalin's avatar
jmvalin committed
39
#include "modes.h"
40 41
#include "ltp.h"
#include "quant_lsp.h"
42
#include "cb_search.h"
43
#include "sb_celp.h"
44
#include "nb_celp.h"
45
#include "vbr.h"
jm's avatar
jm committed
46
#include "arch.h"
47
#include <math.h>
48 49 50 51

#ifndef NULL
#define NULL 0
#endif
jmvalin's avatar
jmvalin committed
52

53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
#ifdef DISABLE_ENCODER
#define nb_encoder_init NULL
#define nb_encoder_destroy NULL
#define nb_encode NULL
#define nb_encoder_ctl NULL

#define split_cb_search_shape_sign NULL
#define noise_codebook_quant NULL
#define pitch_search_3tap NULL
#define forced_pitch_quant NULL
#define lsp_quant_nb NULL
#define lsp_quant_lbr NULL
#endif /* DISABLE_ENCODER */

#ifdef DISABLE_DECODER
#define nb_decoder_init NULL
#define nb_decoder_destroy NULL
#define nb_decode NULL
#define nb_decoder_ctl NULL

#define noise_codebook_unquant NULL
#define split_cb_shape_sign_unquant NULL
#define lsp_unquant_nb NULL
#define lsp_unquant_lbr NULL
#define pitch_unquant_3tap NULL
#define forced_pitch_unquant NULL
#endif /* DISABLE_DECODER */
jmvalin's avatar
jmvalin committed
80

jmvalin's avatar
jmvalin committed
81
/* Extern declarations for all codebooks we use here */
jm's avatar
jm committed
82 83 84 85 86 87 88 89 90
extern const signed char gain_cdbk_nb[];
extern const signed char gain_cdbk_lbr[];
extern const signed char exc_5_256_table[];
extern const signed char exc_5_64_table[];
extern const signed char exc_8_128_table[];
extern const signed char exc_10_32_table[];
extern const signed char exc_10_16_table[];
extern const signed char exc_20_32_table[];

jmvalin's avatar
jmvalin committed
91

jmvalin's avatar
jmvalin committed
92
/* Parameters for Long-Term Prediction (LTP)*/
jm's avatar
jm committed
93
static const ltp_params ltp_params_nb = {
94 95 96 97 98
   gain_cdbk_nb,
   7,
   7
};

99
/* Parameters for Long-Term Prediction (LTP)*/
jm's avatar
jm committed
100
static const ltp_params ltp_params_vlbr = {
101 102 103 104 105
   gain_cdbk_lbr,
   5,
   0
};

106
/* Parameters for Long-Term Prediction (LTP)*/
jm's avatar
jm committed
107
static const ltp_params ltp_params_lbr = {
jmvalin's avatar
jmvalin committed
108
   gain_cdbk_lbr,
109
   5,
jmvalin's avatar
jmvalin committed
110
   7
111 112
};

jmvalin's avatar
jmvalin committed
113
/* Parameters for Long-Term Prediction (LTP)*/
jm's avatar
jm committed
114
static const ltp_params ltp_params_med = {
jmvalin's avatar
jmvalin committed
115 116 117 118 119
   gain_cdbk_lbr,
   5,
   7
};

jmvalin's avatar
jmvalin committed
120
/* Split-VQ innovation parameters for very low bit-rate narrowband */
jm's avatar
jm committed
121
static const split_cb_params split_cb_nb_vlbr = {
122 123 124
   10,               /*subvect_size*/
   4,               /*nb_subvect*/
   exc_10_16_table, /*shape_cb*/
jmvalin's avatar
jmvalin committed
125
   4,               /*shape_bits*/
126
   0,
jmvalin's avatar
jmvalin committed
127
};
jmvalin's avatar
jmvalin committed
128

jm's avatar
jm committed
129
/* Split-VQ innovation parameters for very low bit-rate narrowband */
jm's avatar
jm committed
130
static const split_cb_params split_cb_nb_ulbr = {
jm's avatar
jm committed
131 132 133 134 135 136 137
   20,               /*subvect_size*/
   2,               /*nb_subvect*/
   exc_20_32_table, /*shape_cb*/
   5,               /*shape_bits*/
   0,
};

138
/* Split-VQ innovation parameters for low bit-rate narrowband */
jm's avatar
jm committed
139
static const split_cb_params split_cb_nb_lbr = {
140
   10,              /*subvect_size*/
141 142 143
   4,               /*nb_subvect*/
   exc_10_32_table, /*shape_cb*/
   5,               /*shape_bits*/
144
   0,
145 146 147 148
};


/* Split-VQ innovation parameters narrowband */
jm's avatar
jm committed
149
static const split_cb_params split_cb_nb = {
150 151 152 153
   5,               /*subvect_size*/
   8,               /*nb_subvect*/
   exc_5_64_table, /*shape_cb*/
   6,               /*shape_bits*/
154
   0,
155 156
};

jmvalin's avatar
jmvalin committed
157
/* Split-VQ innovation parameters narrowband */
jm's avatar
jm committed
158
static const split_cb_params split_cb_nb_med = {
jmvalin's avatar
jmvalin committed
159 160 161 162
   8,               /*subvect_size*/
   5,               /*nb_subvect*/
   exc_8_128_table, /*shape_cb*/
   7,               /*shape_bits*/
163
   0,
jmvalin's avatar
jmvalin committed
164 165
};

166
/* Split-VQ innovation for low-band wideband */
jm's avatar
jm committed
167
static const split_cb_params split_cb_sb = {
168 169
   5,               /*subvect_size*/
   8,              /*nb_subvect*/
170 171
   exc_5_256_table,    /*shape_cb*/
   8,               /*shape_bits*/
172
   0,
jmvalin's avatar
jmvalin committed
173 174
};

175

176

177
/* 2150 bps "vocoder-like" mode for comfort noise */
jm's avatar
jm committed
178
static const SpeexSubmode nb_submode1 = {
jmvalin's avatar
jmvalin committed
179 180
   0,
   1,
181
   0,
182
   0,
jmvalin's avatar
jmvalin committed
183 184 185 186
   /* LSP quantization */
   lsp_quant_lbr,
   lsp_unquant_lbr,
   /* No pitch quantization */
jmvalin's avatar
jmvalin committed
187 188
   forced_pitch_quant,
   forced_pitch_unquant,
jmvalin's avatar
jmvalin committed
189 190
   NULL,
   /* No innovation quantization (noise only) */
jmvalin's avatar
jmvalin committed
191 192
   noise_codebook_quant,
   noise_codebook_unquant,
jmvalin's avatar
jmvalin committed
193
   NULL,
194
   -1,
195
   43
jmvalin's avatar
jmvalin committed
196 197
};

jm's avatar
jm committed
198
/* 3.95 kbps very low bit-rate mode */
jm's avatar
jm committed
199
static const SpeexSubmode nb_submode8 = {
jm's avatar
jm committed
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
   0,
   1,
   0,
   0,
   /*LSP quantization*/
   lsp_quant_lbr,
   lsp_unquant_lbr,
   /*No pitch quantization*/
   forced_pitch_quant,
   forced_pitch_unquant,
   NULL,
   /*Innovation quantization*/
   split_cb_search_shape_sign,
   split_cb_shape_sign_unquant,
   &split_cb_nb_ulbr,
jm's avatar
jm committed
215
   QCONST16(.5,15),
jm's avatar
jm committed
216 217 218
   79
};

219
/* 5.95 kbps very low bit-rate mode */
jm's avatar
jm committed
220
static const SpeexSubmode nb_submode2 = {
jmvalin's avatar
jmvalin committed
221
   0,
jmvalin's avatar
jmvalin committed
222 223
   0,
   0,
224
   0,
jmvalin's avatar
jmvalin committed
225 226 227 228
   /*LSP quantization*/
   lsp_quant_lbr,
   lsp_unquant_lbr,
   /*No pitch quantization*/
229 230 231
   pitch_search_3tap,
   pitch_unquant_3tap,
   &ltp_params_vlbr,
jmvalin's avatar
jmvalin committed
232
   /*Innovation quantization*/
233 234
   split_cb_search_shape_sign,
   split_cb_shape_sign_unquant,
jmvalin's avatar
jmvalin committed
235
   &split_cb_nb_vlbr,
jm's avatar
jm committed
236
   QCONST16(.6,15),
237
   119
jmvalin's avatar
jmvalin committed
238 239
};

240
/* 8 kbps low bit-rate mode */
jm's avatar
jm committed
241
static const SpeexSubmode nb_submode3 = {
jmvalin's avatar
jmvalin committed
242
   -1,
jmvalin's avatar
jmvalin committed
243
   0,
244
   1,
245
   0,
246
   /*LSP quantization*/
247 248
   lsp_quant_lbr,
   lsp_unquant_lbr,
249 250 251
   /*Pitch quantization*/
   pitch_search_3tap,
   pitch_unquant_3tap,
252
   &ltp_params_lbr,
253
   /*Innovation quantization*/
254 255
   split_cb_search_shape_sign,
   split_cb_shape_sign_unquant,
256
   &split_cb_nb_lbr,
jm's avatar
jm committed
257
   QCONST16(.55,15),
258
   160
259 260
};

261
/* 11 kbps medium bit-rate mode */
jm's avatar
jm committed
262
static const SpeexSubmode nb_submode4 = {
jmvalin's avatar
jmvalin committed
263
   -1,
jmvalin's avatar
jmvalin committed
264
   0,
jmvalin's avatar
jmvalin committed
265
   1,
266
   0,
jmvalin's avatar
jmvalin committed
267
   /*LSP quantization*/
jmvalin's avatar
jmvalin committed
268 269
   lsp_quant_lbr,
   lsp_unquant_lbr,
jmvalin's avatar
jmvalin committed
270 271 272 273 274
   /*Pitch quantization*/
   pitch_search_3tap,
   pitch_unquant_3tap,
   &ltp_params_med,
   /*Innovation quantization*/
275 276
   split_cb_search_shape_sign,
   split_cb_shape_sign_unquant,
jmvalin's avatar
jmvalin committed
277
   &split_cb_nb_med,
jm's avatar
jm committed
278
   QCONST16(.45,15),
279
   220
jmvalin's avatar
jmvalin committed
280 281
};

282
/* 15 kbps high bit-rate mode */
jm's avatar
jm committed
283
static const SpeexSubmode nb_submode5 = {
jmvalin's avatar
jmvalin committed
284
   -1,
jmvalin's avatar
jmvalin committed
285
   0,
286
   3,
287
   0,
288
   /*LSP quantization*/
289 290
   lsp_quant_nb,
   lsp_unquant_nb,
291 292 293
   /*Pitch quantization*/
   pitch_search_3tap,
   pitch_unquant_3tap,
294
   &ltp_params_nb,
295
   /*Innovation quantization*/
296 297
   split_cb_search_shape_sign,
   split_cb_shape_sign_unquant,
298
   &split_cb_nb,
299
   QCONST16(.25,15),
300
   300
301 302
};

303
/* 18.2 high bit-rate mode */
jm's avatar
jm committed
304
static const SpeexSubmode nb_submode6 = {
jmvalin's avatar
jmvalin committed
305
   -1,
jmvalin's avatar
jmvalin committed
306
   0,
307
   3,
308
   0,
309 310 311 312 313 314 315 316
   /*LSP quantization*/
   lsp_quant_nb,
   lsp_unquant_nb,
   /*Pitch quantization*/
   pitch_search_3tap,
   pitch_unquant_3tap,
   &ltp_params_nb,
   /*Innovation quantization*/
317 318
   split_cb_search_shape_sign,
   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 */
jm's avatar
jm committed
325
static const SpeexSubmode nb_submode7 = {
326 327 328 329 330 331 332 333 334 335 336 337
   -1,
   0,
   3,
   1,
   /*LSP quantization*/
   lsp_quant_nb,
   lsp_unquant_nb,
   /*Pitch quantization*/
   pitch_search_3tap,
   pitch_unquant_3tap,
   &ltp_params_nb,
   /*Innovation quantization*/
338 339
   split_cb_search_shape_sign,
   split_cb_shape_sign_unquant,
340
   &split_cb_nb,
341
   QCONST16(.05,15),
342 343 344
   492
};

345 346

/* Default mode for narrowband */
jm's avatar
jm committed
347
static const SpeexNBMode nb_mode = {
348 349 350 351 352
   NB_FRAME_SIZE,    /*frameSize*/
   NB_SUBFRAME_SIZE, /*subframeSize*/
   NB_ORDER,         /*lpcSize*/
   NB_PITCH_START,               /*pitchStart*/
   NB_PITCH_END,              /*pitchEnd*/
353 354
   QCONST16(0.92,15),  /* gamma1 */
   QCONST16(0.6,15),   /* gamma2 */
jm's avatar
jm committed
355
   QCONST16(.0002,15), /*lpc_floor*/
356
   {NULL, &nb_submode1, &nb_submode2, &nb_submode3, &nb_submode4, &nb_submode5, &nb_submode6, &nb_submode7,
jm's avatar
jm committed
357
   &nb_submode8, NULL, NULL, NULL, NULL, NULL, NULL, NULL},
jm's avatar
jm committed
358
   5,
jm's avatar
jm committed
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,
jmvalin's avatar
jmvalin committed
366
   nb_mode_query,
367
   "narrowband",
368
   0,
369
   4,
370 371 372 373 374 375 376 377
   nb_encoder_init,
   nb_encoder_destroy,
   nb_encode,
   nb_decoder_init,
   nb_decoder_destroy,
   nb_decode,
   nb_encoder_ctl,
   nb_decoder_ctl,
jmvalin's avatar
jmvalin committed
378 379
};

380

381

382
EXPORT int speex_mode_query(const SpeexMode *mode, int request, void *ptr)
jm's avatar
jm committed
383 384 385
{
   return mode->query(mode->mode, request, ptr);
}
386

387 388 389 390
#ifdef FIXED_DEBUG
long long spx_mips=0;
#endif