forked from TimothyGu/libilbc
-
Notifications
You must be signed in to change notification settings - Fork 13
/
ilbc.h
485 lines (415 loc) · 14.8 KB
/
ilbc.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
/*
* Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
/*
* ilbc.h
*
* This header file contains all of the API's for iLBC.
*
*/
#ifndef MODULES_AUDIO_CODING_CODECS_ILBC_ILBC_H_
#define MODULES_AUDIO_CODING_CODECS_ILBC_ILBC_H_
#include <stddef.h>
#include <stdint.h>
#include "ilbc_export.h"
/*
* Solution to support multiple instances
* Customer has to cast instance to proper type
*/
typedef struct iLBC_encinst_t_ IlbcEncoderInstance;
typedef struct iLBC_decinst_t_ IlbcDecoderInstance;
/*
* Comfort noise constants
*/
#define ILBC_SPEECH 1
#define ILBC_CNG 2
#ifdef __cplusplus
extern "C" {
#endif
/****************************************************************************
* WebRtcIlbcfix_XxxAssign(...)
*
* These functions assigns the encoder/decoder instance to the specified
* memory location
*
* Input:
* - XXX_xxxinst : Pointer to created instance that should be
* assigned
* - ILBCXXX_inst_Addr : Pointer to the desired memory space
* - size : The size that this structure occupies (in Word16)
*
* Return value : 0 - Ok
* -1 - Error
*/
ILBC_EXPORT
int16_t WebRtcIlbcfix_EncoderAssign(IlbcEncoderInstance** iLBC_encinst,
int16_t* ILBCENC_inst_Addr,
int16_t* size);
ILBC_EXPORT
int16_t WebRtcIlbcfix_DecoderAssign(IlbcDecoderInstance** iLBC_decinst,
int16_t* ILBCDEC_inst_Addr,
int16_t* size);
/****************************************************************************
* WebRtcIlbcfix_XxxAssign(...)
*
* These functions create a instance to the specified structure
*
* Input:
* - XXX_inst : Pointer to created instance that should be created
*
* Return value : 0 - Ok
* -1 - Error
*/
ILBC_EXPORT
int16_t WebRtcIlbcfix_EncoderCreate(IlbcEncoderInstance** iLBC_encinst);
ILBC_EXPORT
int16_t WebRtcIlbcfix_DecoderCreate(IlbcDecoderInstance** iLBC_decinst);
/****************************************************************************
* WebRtcIlbcfix_XxxFree(...)
*
* These functions frees the dynamic memory of a specified instance
*
* Input:
* - XXX_inst : Pointer to created instance that should be freed
*
* Return value : 0 - Ok
* -1 - Error
*/
ILBC_EXPORT
int16_t WebRtcIlbcfix_EncoderFree(IlbcEncoderInstance* iLBC_encinst);
ILBC_EXPORT
int16_t WebRtcIlbcfix_DecoderFree(IlbcDecoderInstance* iLBC_decinst);
/****************************************************************************
* WebRtcIlbcfix_EncoderInit(...)
*
* This function initializes a iLBC instance
*
* Input:
* - iLBCenc_inst : iLBC instance, i.e. the user that should receive
* be initialized
* - frameLen : The frame length of the codec 20/30 (ms)
*
* Return value : 0 - Ok
* -1 - Error
*/
ILBC_EXPORT
int16_t WebRtcIlbcfix_EncoderInit(IlbcEncoderInstance* iLBCenc_inst,
int16_t frameLen);
/****************************************************************************
* WebRtcIlbcfix_Encode(...)
*
* This function encodes one iLBC frame. Input speech length has be a
* multiple of the frame length.
*
* Input:
* - iLBCenc_inst : iLBC instance, i.e. the user that should encode
* a package
* - speechIn : Input speech vector
* - len : Samples in speechIn (160, 240, 320 or 480)
*
* Output:
* - encoded : The encoded data vector
*
* Return value : >0 - Length (in bytes) of coded data
* -1 - Error
*/
ILBC_EXPORT
int WebRtcIlbcfix_Encode(IlbcEncoderInstance* iLBCenc_inst,
const int16_t* speechIn,
size_t len,
uint8_t* encoded);
/****************************************************************************
* WebRtcIlbcfix_DecoderInit(...)
*
* This function initializes a iLBC instance with either 20 or 30 ms frames
* Alternatively the WebRtcIlbcfix_DecoderInit_XXms can be used. Then it's
* not needed to specify the frame length with a variable.
*
* Input:
* - IlbcDecoderInstance : iLBC decoder instance
* - frameLen : The frame length of the codec 20/30 (ms)
*
* Return value : 0 - Ok
* -1 - Error
*/
ILBC_EXPORT
int16_t WebRtcIlbcfix_DecoderInit(IlbcDecoderInstance* iLBCdec_inst,
int16_t frameLen);
ILBC_EXPORT
void WebRtcIlbcfix_DecoderInit20Ms(IlbcDecoderInstance* iLBCdec_inst);
ILBC_EXPORT
void WebRtcIlbcfix_Decoderinit30Ms(IlbcDecoderInstance* iLBCdec_inst);
/****************************************************************************
* WebRtcIlbcfix_Decode(...)
*
* This function decodes a packet with iLBC frame(s). Output speech length
* will be a multiple of 160 or 240 samples ((160 or 240)*frames/packet).
*
* Input:
* - iLBCdec_inst : iLBC instance, i.e. the user that should decode
* a packet
* - encoded : Encoded iLBC frame(s)
* - len : Bytes in encoded vector
*
* Output:
* - decoded : The decoded vector
* - speechType : 1 normal, 2 CNG
*
* Return value : >0 - Samples in decoded vector
* -1 - Error
*/
ILBC_EXPORT
int WebRtcIlbcfix_Decode(IlbcDecoderInstance* iLBCdec_inst,
const uint8_t* encoded,
size_t len,
int16_t* decoded,
int16_t* speechType);
ILBC_EXPORT
int WebRtcIlbcfix_Decode20Ms(IlbcDecoderInstance* iLBCdec_inst,
const uint8_t* encoded,
size_t len,
int16_t* decoded,
int16_t* speechType);
ILBC_EXPORT
int WebRtcIlbcfix_Decode30Ms(IlbcDecoderInstance* iLBCdec_inst,
const uint8_t* encoded,
size_t len,
int16_t* decoded,
int16_t* speechType);
/****************************************************************************
* WebRtcIlbcfix_DecodePlc(...)
*
* This function conducts PLC for iLBC frame(s). Output speech length
* will be a multiple of 160 or 240 samples.
*
* Input:
* - iLBCdec_inst : iLBC instance, i.e. the user that should perform
* a PLC
* - noOfLostFrames : Number of PLC frames to produce
*
* Output:
* - decoded : The "decoded" vector
*
* Return value : Samples in decoded PLC vector
*/
ILBC_EXPORT
size_t WebRtcIlbcfix_DecodePlc(IlbcDecoderInstance* iLBCdec_inst,
int16_t* decoded,
size_t noOfLostFrames);
/****************************************************************************
* WebRtcIlbcfix_NetEqPlc(...)
*
* This function updates the decoder when a packet loss has occured, but it
* does not produce any PLC data. Function can be used if another PLC method
* is used (i.e NetEq).
*
* Input:
* - iLBCdec_inst : iLBC instance that should be updated
* - noOfLostFrames : Number of lost frames
*
* Output:
* - decoded : The "decoded" vector (nothing in this case)
*
* Return value : Samples in decoded PLC vector
*/
ILBC_EXPORT
size_t WebRtcIlbcfix_NetEqPlc(IlbcDecoderInstance* iLBCdec_inst,
int16_t* decoded,
size_t noOfLostFrames);
/****************************************************************************
* WebRtcIlbcfix_version(...)
*
* This function returns the version number of iLBC
*
* Output:
* - version : Version number of iLBC (maximum 20 char)
*/
ILBC_EXPORT
void WebRtcIlbcfix_version(char* version);
/****************************************************************************
* Compatibility with the library code from RFC3951.
*
*/
/* general codec settings */
#define FS 8000
#define BLOCKL_20MS 160
#define BLOCKL_30MS 240
#define BLOCKL_MAX 240
#define NSUB_20MS 4
#define NSUB_30MS 6
#define NSUB_MAX 6
#define NASUB_20MS 2
#define NASUB_30MS 4
#define NASUB_MAX 4
#define SUBL 40
#define STATE_LEN 80
#define STATE_SHORT_LEN_30MS 58
#define STATE_SHORT_LEN_20MS 57
/* LPC settings */
#define LPC_FILTERORDER 10
#define LPC_LOOKBACK 60
#define LPC_N_20MS 1
#define LPC_N_30MS 2
#define LPC_N_MAX 2
#define LPC_ASYMDIFF 20
#define LSF_NSPLIT 3
#define LSF_NUMBER_OF_STEPS 4
#define LPC_HALFORDER 5
#define COS_GRID_POINTS 60
/* enhancer */
#define ENH_BLOCKL 80 /* block length */
#define ENH_BLOCKL_HALF (ENH_BLOCKL / 2)
#define ENH_HL \
3 /* 2*ENH_HL+1 is number blocks \
in said second \
sequence */
#define ENH_SLOP \
2 /* max difference estimated and \
correct pitch period */
#define ENH_PLOCSL \
8 /* pitch-estimates and \
pitch-locations buffer \
length */
#define ENH_OVERHANG 2
#define ENH_UPS0 4 /* upsampling rate */
#define ENH_FL0 3 /* 2*FLO+1 is the length of each filter */
#define ENH_FLO_MULT2_PLUS1 7
#define ENH_VECTL (ENH_BLOCKL + 2 * ENH_FL0)
#define ENH_CORRDIM (2 * ENH_SLOP + 1)
#define ENH_NBLOCKS (BLOCKL / ENH_BLOCKL)
#define ENH_NBLOCKS_EXTRA 5
#define ENH_NBLOCKS_TOT 8 /* ENH_NBLOCKS+ENH_NBLOCKS_EXTRA */
#define ENH_BUFL (ENH_NBLOCKS_TOT) * ENH_BLOCKL
#define ENH_BUFL_FILTEROVERHEAD 3
#define ENH_A0 819 /* Q14 */
#define ENH_A0_MINUS_A0A0DIV4 848256041 /* Q34 */
#define ENH_A0DIV2 26843546 /* Q30 */
/* type definition encoder instance */
typedef struct IlbcEncoder_ {
/* flag for frame size mode */
int16_t mode;
/* basic parameters for different frame sizes */
size_t blockl;
size_t nsub;
int16_t nasub;
size_t no_of_bytes, no_of_words;
int16_t lpc_n;
size_t state_short_len;
/* analysis filter state */
int16_t anaMem[LPC_FILTERORDER];
/* Fix-point old lsf parameters for interpolation */
int16_t lsfold[LPC_FILTERORDER];
int16_t lsfdeqold[LPC_FILTERORDER];
/* signal buffer for LP analysis */
int16_t lpc_buffer[LPC_LOOKBACK + BLOCKL_MAX];
/* state of input HP filter */
int16_t hpimemx[2];
int16_t hpimemy[4];
#ifdef SPLIT_10MS
int16_t weightdenumbuf[66];
int16_t past_samples[160];
uint16_t bytes[25];
int16_t section;
int16_t Nfor_flag;
int16_t Nback_flag;
int16_t start_pos;
size_t diff;
#endif
} IlbcEncoder;
/* type definition decoder instance */
typedef struct IlbcDecoder_ {
/* flag for frame size mode */
int16_t mode;
/* basic parameters for different frame sizes */
size_t blockl;
size_t nsub;
int16_t nasub;
size_t no_of_bytes, no_of_words;
int16_t lpc_n;
size_t state_short_len;
/* synthesis filter state */
int16_t syntMem[LPC_FILTERORDER];
/* old LSF for interpolation */
int16_t lsfdeqold[LPC_FILTERORDER];
/* pitch lag estimated in enhancer and used in PLC */
size_t last_lag;
/* PLC state information */
int consPLICount, prev_enh_pl;
int16_t perSquare;
int16_t prevScale, prevPLI;
size_t prevLag;
int16_t prevLpc[LPC_FILTERORDER + 1];
int16_t prevResidual[NSUB_MAX * SUBL];
int16_t seed;
/* previous synthesis filter parameters */
int16_t old_syntdenum[(LPC_FILTERORDER + 1) * NSUB_MAX];
/* state of output HP filter */
int16_t hpimemx[2];
int16_t hpimemy[4];
/* enhancer state information */
int use_enhancer;
int16_t enh_buf[ENH_BUFL + ENH_BUFL_FILTEROVERHEAD];
size_t enh_period[ENH_NBLOCKS_TOT];
} IlbcDecoder;
/*----------------------------------------------------------------*
* Initiation of decoder instance.
*---------------------------------------------------------------*/
ILBC_EXPORT
int WebRtcIlbcfix_InitDecode(/* (o) Number of decoded samples */
IlbcDecoder*
iLBCdec_inst, /* (i/o) Decoder instance */
int16_t mode, /* (i) frame size mode */
int use_enhancer /* (i) 1 to use enhancer
0 to run without enhancer */
);
/*----------------------------------------------------------------*
* Initiation of encoder instance.
*---------------------------------------------------------------*/
ILBC_EXPORT
int WebRtcIlbcfix_InitEncode(/* (o) Number of bytes encoded */
IlbcEncoder*
iLBCenc_inst, /* (i/o) Encoder instance */
int16_t mode /* (i) frame size mode */
);
/*----------------------------------------------------------------*
* main decoder function
*---------------------------------------------------------------*/
// Returns 0 on success, -1 on error.
ILBC_EXPORT
int WebRtcIlbcfix_DecodeImpl(
int16_t* decblock, /* (o) decoded signal block */
const uint16_t* bytes, /* (i) encoded signal bits */
IlbcDecoder* iLBCdec_inst, /* (i/o) the decoder state
structure */
int16_t mode /* (i) 0: bad packet, PLC,
1: normal */
);
/*----------------------------------------------------------------*
* main encoder function
*---------------------------------------------------------------*/
ILBC_EXPORT
void WebRtcIlbcfix_EncodeImpl(
uint16_t* bytes, /* (o) encoded data bits iLBC */
const int16_t* block, /* (i) speech vector to encode */
IlbcEncoder* iLBCenc_inst /* (i/o) the general encoder
state */
);
// Version macros
#define LIBILBC_VERSION_MAJOR 3
#define LIBILBC_VERSION_MINOR 0
#define LIBILBC_VERSION_PATCH 3
// Old type names:
typedef ILBC_DEPRECATED IlbcEncoderInstance iLBC_encinst_t;
typedef ILBC_DEPRECATED IlbcDecoderInstance iLBC_decinst_t;
typedef ILBC_DEPRECATED IlbcEncoder iLBC_Enc_Inst_t;
typedef ILBC_DEPRECATED IlbcDecoder iLBC_Dec_Inst_t;
#ifdef __cplusplus
}
#endif
#endif // MODULES_AUDIO_CODING_CODECS_ILBC_ILBC_H_