Asterisk - The Open Source Telephony Project GIT-master-57e2743
Loading...
Searching...
No Matches
codec_ilbc.c
Go to the documentation of this file.
1/*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * The iLBC code is from The IETF code base and is copyright The Internet Society (2004)
5 *
6 * Copyright (C) 1999 - 2005, Digium, Inc.
7 *
8 * Mark Spencer <markster@digium.com>
9 *
10 * See http://www.asterisk.org for more information about
11 * the Asterisk project. Please do not directly contact
12 * any of the maintainers of this project for assistance;
13 * the project provides a web site, mailing lists and IRC
14 * channels for your use.
15 *
16 * This program is free software, distributed under the terms of
17 * the GNU General Public License Version 2. See the LICENSE file
18 * at the top of the source tree.
19 */
20
21/*! \file
22 *
23 * \brief Translate between signed linear and Internet Low Bitrate Codec
24 *
25 * \ingroup codecs
26 */
27
28/*** MODULEINFO
29 <use>ilbc</use>
30 <support_level>core</support_level>
31 ***/
32
33#include "asterisk.h"
34
35#include "asterisk/codec.h" /* for AST_MEDIA_TYPE_AUDIO */
36#include "asterisk/format.h" /* for ast_format_get_attribute_data */
37#include "asterisk/frame.h" /* for ast_frame, etc */
38#include "asterisk/linkedlists.h" /* for AST_LIST_NEXT, etc */
39#include "asterisk/logger.h" /* for ast_log, ast_debug, etc */
40#include "asterisk/module.h"
41#include "asterisk/translate.h" /* for ast_trans_pvt, etc */
42
43#ifdef ILBC_WEBRTC
44#include <ilbc.h>
45typedef uint16_t ilbc_bytes;
46typedef int16_t ilbc_block;
47#define BUF_TYPE i16
48/* libilbc 3.x removed the RFC 3951 compatibility macros that 2.x's <ilbc.h>
49 * provided. Re-add them only when the header did not, so this is a no-op
50 * against libilbc 2.x and only takes effect on 3.x. */
51#ifndef initDecode
52#define initDecode WebRtcIlbcfix_InitDecode
53#endif
54#ifndef initEncode
55#define initEncode WebRtcIlbcfix_InitEncode
56#endif
57#ifndef iLBC_decode
58#define iLBC_decode WebRtcIlbcfix_DecodeImpl
59#endif
60#ifndef iLBC_encode
61#define iLBC_encode WebRtcIlbcfix_EncodeImpl
62#endif
63/* libilbc 3.x also deprecates the iLBC_{Enc,Dec}_Inst_t type names in favour of
64 * IlbcEncoder/IlbcDecoder; use the current names there so the build stays clean
65 * under -Werror=deprecated-declarations. libilbc 2.x has only the iLBC_* names. */
66#if defined(LIBILBC_VERSION_MAJOR) && LIBILBC_VERSION_MAJOR >= 3
67#define iLBC_Enc_Inst_t IlbcEncoder
68#define iLBC_Dec_Inst_t IlbcDecoder
69#endif
70#else
71#include "ilbc/iLBC_encode.h"
72#include "ilbc/iLBC_decode.h"
73typedef unsigned char ilbc_bytes;
74typedef float ilbc_block;
75#define BUF_TYPE uc
76#endif
77
78#include "asterisk/ilbc.h"
79
80#define USE_ILBC_ENHANCER 0
81#define BUFFER_SAMPLES 8000
82
83/* Sample frame data */
84#include "asterisk/slin.h"
85#include "ex_ilbc.h"
86
90 /* Enough to store a full second */
92 int16_t inited;
93};
94
95static int lintoilbc_new(struct ast_trans_pvt *pvt)
96{
97 struct ilbc_coder_pvt *tmp = pvt->pvt;
99 const unsigned int mode = attr ? attr->mode : 30;
100
101 initEncode(&tmp->enc, mode);
102
103 return 0;
104}
105
106static int ilbctolin_new(struct ast_trans_pvt *pvt)
107{
108 struct ilbc_coder_pvt *tmp = pvt->pvt;
109
110 tmp->inited = 0; /* we do not know the iLBC mode, yet */
111
112 return 0;
113}
114
115/*! \brief decode a frame and store in outbuf */
116static int ilbctolin_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
117{
118 struct ilbc_coder_pvt *tmp = pvt->pvt;
120 const unsigned int mode = attr ? attr->mode : 30;
121 const unsigned int sample_rate = pvt->t->dst_codec.sample_rate;
122 const unsigned int samples_per_frame = mode * sample_rate / 1000;
123 const unsigned int octets_per_frame = (mode == 20) ? 38 : 50;
124
125 int plc_mode = 1; /* 1 = normal data, 0 = plc */
126 /* Assuming there's space left, decode into the current buffer at
127 the tail location. Read in as many frames as there are */
128 int x,i;
129 int datalen = f->datalen;
130 int16_t *dst = pvt->outbuf.i16;
131 ilbc_block tmpf[samples_per_frame];
132
133 if (!f->data.ptr && datalen) {
134 ast_debug(1, "issue 16070, ILIB ERROR. data = NULL datalen = %d src = %s\n", datalen, f->src ? f->src : "no src set");
135 f->datalen = 0;
136 datalen = 0;
137 }
138
139 if (datalen == 0) { /* native PLC, set fake datalen and clear plc_mode */
140 datalen = octets_per_frame;
141 f->samples = samples_per_frame;
142 plc_mode = 0; /* do native plc */
143 pvt->samples += samples_per_frame;
144 }
145
146 if (datalen % octets_per_frame) {
147 ast_log(LOG_WARNING, "Huh? An ilbc frame that isn't a multiple of %u bytes long from %s (%d)?\n", octets_per_frame, f->src, datalen);
148 return -1;
149 }
150
151 if (!tmp->inited) {
153 tmp->inited = 1;
154 }
155
156 for (x = 0; x < datalen; x += octets_per_frame) {
157 if (pvt->samples + samples_per_frame > BUFFER_SAMPLES) {
158 ast_log(LOG_WARNING, "Out of buffer space\n");
159 return -1;
160 }
161 iLBC_decode(tmpf, plc_mode ? f->data.ptr + x : NULL, &tmp->dec, plc_mode);
162 for (i = 0; i < samples_per_frame; i++)
163 dst[pvt->samples + i] = tmpf[i];
164 pvt->samples += samples_per_frame;
165 pvt->datalen += samples_per_frame * 2;
166 }
167 return 0;
168}
169
170/*! \brief store a frame into a temporary buffer, for later decoding */
171static int lintoilbc_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
172{
173 struct ilbc_coder_pvt *tmp = pvt->pvt;
174
175 /* Just add the frames to our stream */
176 /* XXX We should look at how old the rest of our stream is, and if it
177 is too old, then we should overwrite it entirely, otherwise we can
178 get artifacts of earlier talk that do not belong */
179 memcpy(tmp->buf + pvt->samples, f->data.ptr, f->datalen);
180 pvt->samples += f->samples;
181 return 0;
182}
183
184/*! \brief encode the temporary buffer and generate a frame */
185static struct ast_frame *lintoilbc_frameout(struct ast_trans_pvt *pvt)
186{
187 struct ilbc_coder_pvt *tmp = pvt->pvt;
188 struct ast_frame *result = NULL;
189 struct ast_frame *last = NULL;
190 int samples = 0; /* output samples */
191
193 const unsigned int mode = attr ? attr->mode : 30;
194 const unsigned int sample_rate = pvt->t->dst_codec.sample_rate;
195 const unsigned int samples_per_frame = mode * sample_rate / 1000;
196 const unsigned int octets_per_frame = (mode == 20) ? 38 : 50;
197
198 while (pvt->samples >= samples_per_frame) {
199 struct ast_frame *current;
200 ilbc_block tmpf[samples_per_frame];
201 int i;
202
203 /* Encode a frame of data */
204 for (i = 0; i < samples_per_frame; i++)
205 tmpf[i] = tmp->buf[samples + i];
206 iLBC_encode((ilbc_bytes *) pvt->outbuf.BUF_TYPE, tmpf, &tmp->enc);
207
208 samples += samples_per_frame;
209 pvt->samples -= samples_per_frame;
210
211 current = ast_trans_frameout(pvt, octets_per_frame, samples_per_frame);
212 if (!current) {
213 continue;
214 } else if (last) {
216 } else {
217 result = current;
218 }
219 last = current;
220 }
221
222 /* Move the data at the end of the buffer to the front */
223 if (samples) {
224 memmove(tmp->buf, tmp->buf + samples, pvt->samples * 2);
225 }
226
227 return result;
228}
229
230static struct ast_translator ilbctolin = {
231 .name = "ilbctolin",
232 .src_codec = {
233 .name = "ilbc",
234 .type = AST_MEDIA_TYPE_AUDIO,
235 .sample_rate = 8000,
236 },
237 .dst_codec = {
238 .name = "slin",
239 .type = AST_MEDIA_TYPE_AUDIO,
240 .sample_rate = 8000,
241 },
242 .format = "slin",
243 .newpvt = ilbctolin_new,
244 .framein = ilbctolin_framein,
245 .sample = ilbc_sample,
246 .desc_size = sizeof(struct ilbc_coder_pvt),
247 .buf_size = BUFFER_SAMPLES * 2,
248 .native_plc = 1,
249};
250
251static struct ast_translator lintoilbc = {
252 .name = "lintoilbc",
253 .src_codec = {
254 .name = "slin",
255 .type = AST_MEDIA_TYPE_AUDIO,
256 .sample_rate = 8000,
257 },
258 .dst_codec = {
259 .name = "ilbc",
260 .type = AST_MEDIA_TYPE_AUDIO,
261 .sample_rate = 8000,
262 },
263 .format = "ilbc",
264 .newpvt = lintoilbc_new,
265 .framein = lintoilbc_framein,
266 .frameout = lintoilbc_frameout,
267 .sample = slin8_sample,
268 .desc_size = sizeof(struct ilbc_coder_pvt),
269 /* frame len (38 bytes), frame size (160 samples), ceil (+ 160 - 1) */
270 .buf_size = (BUFFER_SAMPLES * 38 + 160 - 1) / 160,
271};
272
273static int unload_module(void)
274{
275 int res;
276
279
280 return res;
281}
282
283static int load_module(void)
284{
285 int res;
286
289
290 if (res) {
293 }
294
296}
297
struct sla_ringing_trunk * last
Definition app_sla.c:338
Asterisk main include file. File version handling, generic pbx functions.
#define ast_log
Definition astobj2.c:42
static PGresult * result
Definition cel_pgsql.c:84
size_t current
Codec API.
@ AST_MEDIA_TYPE_AUDIO
Definition codec.h:32
#define USE_ILBC_ENHANCER
Definition codec_ilbc.c:80
unsigned char ilbc_bytes
Definition codec_ilbc.c:73
static struct ast_translator ilbctolin
Definition codec_ilbc.c:230
#define BUFFER_SAMPLES
Definition codec_ilbc.c:81
float ilbc_block
Definition codec_ilbc.c:74
static int ilbctolin_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
decode a frame and store in outbuf
Definition codec_ilbc.c:116
static struct ast_translator lintoilbc
Definition codec_ilbc.c:251
static int lintoilbc_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
store a frame into a temporary buffer, for later decoding
Definition codec_ilbc.c:171
static int load_module(void)
Definition codec_ilbc.c:283
static struct ast_frame * lintoilbc_frameout(struct ast_trans_pvt *pvt)
encode the temporary buffer and generate a frame
Definition codec_ilbc.c:185
static int lintoilbc_new(struct ast_trans_pvt *pvt)
Definition codec_ilbc.c:95
static int unload_module(void)
Definition codec_ilbc.c:273
static int ilbctolin_new(struct ast_trans_pvt *pvt)
Definition codec_ilbc.c:106
Raw 8-bit data.
static struct ast_frame * ilbc_sample(void)
Definition ex_ilbc.h:21
Media Format API.
void * ast_format_get_attribute_data(const struct ast_format *format)
Get the attribute data on a format.
Definition format.c:125
short initDecode(iLBC_Dec_Inst_t *iLBCdec_inst, int mode, int use_enhancer)
Definition iLBC_decode.c:33
void iLBC_decode(float *decblock, unsigned char *bytes, iLBC_Dec_Inst_t *iLBCdec_inst, int mode)
void iLBC_encode(unsigned char *bytes, float *block, iLBC_Enc_Inst_t *iLBCenc_inst)
Definition iLBC_encode.c:89
short initEncode(iLBC_Enc_Inst_t *iLBCenc_inst, int mode)
Definition iLBC_encode.c:35
Asterisk internal frame definitions.
Support for logging to various files, console and syslog Configuration in file logger....
#define ast_debug(level,...)
Log a DEBUG message.
#define LOG_WARNING
A set of macros to manage forward-linked lists.
#define AST_LIST_NEXT(elm, field)
Returns the next entry in the list after the given entry.
Asterisk module definitions.
#define AST_MODULE_INFO_STANDARD(keystr, desc)
Definition module.h:581
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition module.h:46
@ AST_MODULE_LOAD_SUCCESS
Definition module.h:70
@ AST_MODULE_LOAD_DECLINE
Module has failed to load, may be in an inconsistent state.
Definition module.h:78
#define NULL
Definition resample.c:96
static struct ast_frame * slin8_sample(void)
Definition slin.h:64
unsigned int sample_rate
Sample rate (number of samples carried in a second)
Definition codec.h:52
struct ast_format * format
Data structure associated with a single frame of data.
struct ast_frame_subclass subclass
union ast_frame::@237 data
Default structure for translators, with the basic fields and buffers, all allocated as part of the sa...
Definition translate.h:213
union ast_trans_pvt::@306 outbuf
struct ast_frame f
Definition translate.h:215
struct ast_translator * t
Definition translate.h:214
struct ast_format * explicit_dst
Definition translate.h:237
int datalen
actual space used in outbuf
Definition translate.h:218
int16_t * i16
Definition translate.h:223
Descriptor of a translator.
Definition translate.h:137
char name[80]
Definition translate.h:138
struct ast_codec dst_codec
Definition translate.h:140
unsigned int mode
Definition ilbc.h:5
int16_t buf[BUFFER_SAMPLES]
Definition codec_ilbc.c:91
iLBC_Enc_Inst_t enc
Definition codec_ilbc.c:88
iLBC_Dec_Inst_t dec
Definition codec_ilbc.c:89
int16_t inited
Definition codec_ilbc.c:92
Support for translation of data formats. translate.c.
#define ast_register_translator(t)
See __ast_register_translator()
Definition translate.h:258
int ast_unregister_translator(struct ast_translator *t)
Unregister a translator Unregisters the given translator.
Definition translate.c:1624
struct ast_frame * ast_trans_frameout(struct ast_trans_pvt *pvt, int datalen, int samples)
generic frameout function
Definition translate.c:496