2 * Asterisk -- An open source telephony toolkit.
4 * The iLBC code is from The IETF code base and is copyright The Internet Society (2004)
6 * Copyright (C) 1999 - 2005, Digium, Inc.
8 * Mark Spencer <markster@digium.com>
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.
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.
23 * \brief Translate between signed linear and Internet Low Bitrate Codec
30 <support_level>core</support_level>
35 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
37 #include "asterisk/translate.h"
38 #include "asterisk/module.h"
39 #include "asterisk/utils.h"
43 typedef WebRtc_UWord16 ilbc_bytes;
44 typedef WebRtc_Word16 ilbc_block;
47 #include "ilbc/iLBC_encode.h"
48 #include "ilbc/iLBC_decode.h"
49 typedef unsigned char ilbc_bytes;
50 typedef float ilbc_block;
54 #define USE_ILBC_ENHANCER 0
56 /* #define ILBC_MS 20 */
58 #define ILBC_FRAME_LEN 50 /* apparently... */
59 #define ILBC_SAMPLES 240 /* 30ms at 8000 hz */
60 #define BUFFER_SAMPLES 8000
62 /* Sample frame data */
63 #include "asterisk/slin.h"
66 struct ilbc_coder_pvt {
69 /* Enough to store a full second */
70 int16_t buf[BUFFER_SAMPLES];
73 static int lintoilbc_new(struct ast_trans_pvt *pvt)
75 struct ilbc_coder_pvt *tmp = pvt->pvt;
77 initEncode(&tmp->enc, ILBC_MS);
82 static int ilbctolin_new(struct ast_trans_pvt *pvt)
84 struct ilbc_coder_pvt *tmp = pvt->pvt;
86 initDecode(&tmp->dec, ILBC_MS, USE_ILBC_ENHANCER);
91 /*! \brief decode a frame and store in outbuf */
92 static int ilbctolin_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
94 struct ilbc_coder_pvt *tmp = pvt->pvt;
95 int plc_mode = 1; /* 1 = normal data, 0 = plc */
96 /* Assuming there's space left, decode into the current buffer at
97 the tail location. Read in as many frames as there are */
99 int16_t *dst = pvt->outbuf.i16;
100 ilbc_block tmpf[ILBC_SAMPLES];
102 if (!f->data.ptr && f->datalen) {
103 ast_debug(1, "issue 16070, ILIB ERROR. data = NULL datalen = %d src = %s\n", f->datalen, f->src ? f->src : "no src set");
107 if (f->datalen == 0) { /* native PLC, set fake f->datalen and clear plc_mode */
108 f->datalen = ILBC_FRAME_LEN;
109 f->samples = ILBC_SAMPLES;
110 plc_mode = 0; /* do native plc */
111 pvt->samples += ILBC_SAMPLES;
114 if (f->datalen % ILBC_FRAME_LEN) {
115 ast_log(LOG_WARNING, "Huh? An ilbc frame that isn't a multiple of 50 bytes long from %s (%d)?\n", f->src, f->datalen);
119 for (x=0; x < f->datalen ; x += ILBC_FRAME_LEN) {
120 if (pvt->samples + ILBC_SAMPLES > BUFFER_SAMPLES) {
121 ast_log(LOG_WARNING, "Out of buffer space\n");
124 iLBC_decode(tmpf, plc_mode ? f->data.ptr + x : NULL, &tmp->dec, plc_mode);
125 for ( i=0; i < ILBC_SAMPLES; i++)
126 dst[pvt->samples + i] = tmpf[i];
127 pvt->samples += ILBC_SAMPLES;
128 pvt->datalen += 2*ILBC_SAMPLES;
133 /*! \brief store a frame into a temporary buffer, for later decoding */
134 static int lintoilbc_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
136 struct ilbc_coder_pvt *tmp = pvt->pvt;
138 /* Just add the frames to our stream */
139 /* XXX We should look at how old the rest of our stream is, and if it
140 is too old, then we should overwrite it entirely, otherwise we can
141 get artifacts of earlier talk that do not belong */
142 memcpy(tmp->buf + pvt->samples, f->data.ptr, f->datalen);
143 pvt->samples += f->samples;
147 /*! \brief encode the temporary buffer and generate a frame */
148 static struct ast_frame *lintoilbc_frameout(struct ast_trans_pvt *pvt)
150 struct ilbc_coder_pvt *tmp = pvt->pvt;
154 /* We can't work on anything less than a frame in size */
155 if (pvt->samples < ILBC_SAMPLES)
157 while (pvt->samples >= ILBC_SAMPLES) {
158 ilbc_block tmpf[ILBC_SAMPLES];
161 /* Encode a frame of data */
162 for (i = 0 ; i < ILBC_SAMPLES ; i++)
163 tmpf[i] = tmp->buf[samples + i];
164 iLBC_encode( (ilbc_bytes*)pvt->outbuf.BUF_TYPE + datalen, tmpf, &tmp->enc);
166 datalen += ILBC_FRAME_LEN;
167 samples += ILBC_SAMPLES;
168 pvt->samples -= ILBC_SAMPLES;
171 /* Move the data at the end of the buffer to the front */
173 memmove(tmp->buf, tmp->buf + samples, pvt->samples * 2);
175 return ast_trans_frameout(pvt, datalen, samples);
178 static struct ast_translator ilbctolin = {
180 .newpvt = ilbctolin_new,
181 .framein = ilbctolin_framein,
182 .sample = ilbc_sample,
183 .desc_size = sizeof(struct ilbc_coder_pvt),
184 .buf_size = BUFFER_SAMPLES * 2,
188 static struct ast_translator lintoilbc = {
190 .newpvt = lintoilbc_new,
191 .framein = lintoilbc_framein,
192 .frameout = lintoilbc_frameout,
193 .sample = slin8_sample,
194 .desc_size = sizeof(struct ilbc_coder_pvt),
195 .buf_size = (BUFFER_SAMPLES * ILBC_FRAME_LEN + ILBC_SAMPLES - 1) / ILBC_SAMPLES,
198 static int unload_module(void)
202 res = ast_unregister_translator(&lintoilbc);
203 res |= ast_unregister_translator(&ilbctolin);
208 static int load_module(void)
212 ast_format_set(&ilbctolin.src_format, AST_FORMAT_ILBC, 0);
213 ast_format_set(&ilbctolin.dst_format, AST_FORMAT_SLINEAR, 0);
215 ast_format_set(&lintoilbc.src_format, AST_FORMAT_SLINEAR, 0);
216 ast_format_set(&lintoilbc.dst_format, AST_FORMAT_ILBC, 0);
219 res = ast_register_translator(&ilbctolin);
221 res=ast_register_translator(&lintoilbc);
223 ast_unregister_translator(&ilbctolin);
225 return AST_MODULE_LOAD_FAILURE;
226 return AST_MODULE_LOAD_SUCCESS;
229 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "iLBC Coder/Decoder");