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
29 <defaultenabled>no</defaultenabled>
34 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
36 #include "asterisk/translate.h"
37 #include "asterisk/module.h"
38 #include "asterisk/utils.h"
40 #include "ilbc/iLBC_encode.h"
41 #include "ilbc/iLBC_decode.h"
43 #define USE_ILBC_ENHANCER 0
45 /* #define ILBC_MS 20 */
47 #define ILBC_FRAME_LEN 50 /* apparently... */
48 #define ILBC_SAMPLES 240 /* 30ms at 8000 hz */
49 #define BUFFER_SAMPLES 8000
51 /* Sample frame data */
52 #include "asterisk/slin.h"
55 struct ilbc_coder_pvt {
58 /* Enough to store a full second */
59 int16_t buf[BUFFER_SAMPLES];
62 static int lintoilbc_new(struct ast_trans_pvt *pvt)
64 struct ilbc_coder_pvt *tmp = pvt->pvt;
66 initEncode(&tmp->enc, ILBC_MS);
71 static int ilbctolin_new(struct ast_trans_pvt *pvt)
73 struct ilbc_coder_pvt *tmp = pvt->pvt;
75 initDecode(&tmp->dec, ILBC_MS, USE_ILBC_ENHANCER);
80 /*! \brief decode a frame and store in outbuf */
81 static int ilbctolin_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
83 struct ilbc_coder_pvt *tmp = pvt->pvt;
84 int plc_mode = 1; /* 1 = normal data, 0 = plc */
85 /* Assuming there's space left, decode into the current buffer at
86 the tail location. Read in as many frames as there are */
88 int16_t *dst = pvt->outbuf.i16;
89 float tmpf[ILBC_SAMPLES];
91 if (!f->data.ptr && f->datalen) {
92 ast_log(LOG_DEBUG, "issue 16070, ILIB ERROR. data = NULL datalen = %d src = %s\n", f->datalen, f->src ? f->src : "no src set");
96 if (f->datalen == 0) { /* native PLC, set fake f->datalen and clear plc_mode */
97 f->datalen = ILBC_FRAME_LEN;
98 f->samples = ILBC_SAMPLES;
99 plc_mode = 0; /* do native plc */
100 pvt->samples += ILBC_SAMPLES;
103 if (f->datalen % ILBC_FRAME_LEN) {
104 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);
108 for (x=0; x < f->datalen ; x += ILBC_FRAME_LEN) {
109 if (pvt->samples + ILBC_SAMPLES > BUFFER_SAMPLES) {
110 ast_log(LOG_WARNING, "Out of buffer space\n");
113 iLBC_decode(tmpf, plc_mode ? f->data.ptr + x : NULL, &tmp->dec, plc_mode);
114 for ( i=0; i < ILBC_SAMPLES; i++)
115 dst[pvt->samples + i] = tmpf[i];
116 pvt->samples += ILBC_SAMPLES;
117 pvt->datalen += 2*ILBC_SAMPLES;
122 /*! \brief store a frame into a temporary buffer, for later decoding */
123 static int lintoilbc_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
125 struct ilbc_coder_pvt *tmp = pvt->pvt;
127 /* Just add the frames to our stream */
128 /* XXX We should look at how old the rest of our stream is, and if it
129 is too old, then we should overwrite it entirely, otherwise we can
130 get artifacts of earlier talk that do not belong */
131 memcpy(tmp->buf + pvt->samples, f->data.ptr, f->datalen);
132 pvt->samples += f->samples;
136 /*! \brief encode the temporary buffer and generate a frame */
137 static struct ast_frame *lintoilbc_frameout(struct ast_trans_pvt *pvt)
139 struct ilbc_coder_pvt *tmp = pvt->pvt;
143 /* We can't work on anything less than a frame in size */
144 if (pvt->samples < ILBC_SAMPLES)
146 while (pvt->samples >= ILBC_SAMPLES) {
147 float tmpf[ILBC_SAMPLES];
150 /* Encode a frame of data */
151 for (i = 0 ; i < ILBC_SAMPLES ; i++)
152 tmpf[i] = tmp->buf[samples + i];
153 iLBC_encode( pvt->outbuf.uc + datalen, tmpf, &tmp->enc);
155 datalen += ILBC_FRAME_LEN;
156 samples += ILBC_SAMPLES;
157 pvt->samples -= ILBC_SAMPLES;
160 /* Move the data at the end of the buffer to the front */
162 memmove(tmp->buf, tmp->buf + samples, pvt->samples * 2);
164 return ast_trans_frameout(pvt, datalen, samples);
167 static struct ast_translator ilbctolin = {
169 .srcfmt = AST_FORMAT_ILBC,
170 .dstfmt = AST_FORMAT_SLINEAR,
171 .newpvt = ilbctolin_new,
172 .framein = ilbctolin_framein,
173 .sample = ilbc_sample,
174 .desc_size = sizeof(struct ilbc_coder_pvt),
175 .buf_size = BUFFER_SAMPLES * 2,
179 static struct ast_translator lintoilbc = {
181 .srcfmt = AST_FORMAT_SLINEAR,
182 .dstfmt = AST_FORMAT_ILBC,
183 .newpvt = lintoilbc_new,
184 .framein = lintoilbc_framein,
185 .frameout = lintoilbc_frameout,
186 .sample = slin8_sample,
187 .desc_size = sizeof(struct ilbc_coder_pvt),
188 .buf_size = (BUFFER_SAMPLES * ILBC_FRAME_LEN + ILBC_SAMPLES - 1) / ILBC_SAMPLES,
191 static int unload_module(void)
195 res = ast_unregister_translator(&lintoilbc);
196 res |= ast_unregister_translator(&ilbctolin);
201 static int load_module(void)
205 res = ast_register_translator(&ilbctolin);
207 res=ast_register_translator(&lintoilbc);
209 ast_unregister_translator(&ilbctolin);
211 return AST_MODULE_LOAD_FAILURE;
212 return AST_MODULE_LOAD_SUCCESS;
215 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "iLBC Coder/Decoder");