2 * Asterisk -- A telephony toolkit for Linux.
4 * Translate between signed linear and Internet Low Bitrate Codec
6 * The iLBC code is from The IETF code base and is copyright The Internet Society (2004)
8 * Copyright (C) 1999 - 2005, Digium, Inc.
10 * Mark Spencer <markster@digium.com>
12 * This program is free software, distributed under the terms of
13 * the GNU General Public License
16 #include "asterisk/lock.h"
17 #include "asterisk/translate.h"
18 #include "asterisk/module.h"
19 #include "asterisk/logger.h"
20 #include "asterisk/channel.h"
24 #include <netinet/in.h>
28 #include "ilbc/iLBC_encode.h"
29 #include "ilbc/iLBC_decode.h"
31 /* Sample frame data */
32 #include "slin_ilbc_ex.h"
33 #include "ilbc_slin_ex.h"
35 #define USE_ILBC_ENHANCER 0
37 /* #define ILBC_MS 20 */
39 AST_MUTEX_DEFINE_STATIC(localuser_lock);
40 static int localusecnt=0;
42 static char *tdesc = "iLBC/PCM16 (signed linear) Codec Translator";
44 struct ast_translator_pvt {
48 /* Space to build offset */
49 char offset[AST_FRIENDLY_OFFSET];
50 /* Buffer for our outgoing frame */
52 /* Enough to store a full second */
57 #define ilbc_coder_pvt ast_translator_pvt
59 static struct ast_translator_pvt *lintoilbc_new(void)
61 struct ilbc_coder_pvt *tmp;
62 tmp = malloc(sizeof(struct ilbc_coder_pvt));
64 /* Shut valgrind up */
65 memset(&tmp->enc, 0, sizeof(tmp->enc));
66 initEncode(&tmp->enc, ILBC_MS);
73 static struct ast_translator_pvt *ilbctolin_new(void)
75 struct ilbc_coder_pvt *tmp;
76 tmp = malloc(sizeof(struct ilbc_coder_pvt));
78 /* Shut valgrind up */
79 memset(&tmp->dec, 0, sizeof(tmp->dec));
80 initDecode(&tmp->dec, ILBC_MS, USE_ILBC_ENHANCER);
87 static struct ast_frame *lintoilbc_sample(void)
89 static struct ast_frame f;
90 f.frametype = AST_FRAME_VOICE;
91 f.subclass = AST_FORMAT_SLINEAR;
92 f.datalen = sizeof(slin_ilbc_ex);
94 f.samples = sizeof(slin_ilbc_ex)/2;
97 f.src = __PRETTY_FUNCTION__;
98 f.data = slin_ilbc_ex;
102 static struct ast_frame *ilbctolin_sample(void)
104 static struct ast_frame f;
105 f.frametype = AST_FRAME_VOICE;
106 f.subclass = AST_FORMAT_ILBC;
107 f.datalen = sizeof(ilbc_slin_ex);
108 /* All frames are 30 ms long */
112 f.src = __PRETTY_FUNCTION__;
113 f.data = ilbc_slin_ex;
117 static struct ast_frame *ilbctolin_frameout(struct ast_translator_pvt *tmp)
121 /* Signed linear is no particular frame size, so just send whatever
122 we have in the buffer in one lump sum */
123 tmp->f.frametype = AST_FRAME_VOICE;
124 tmp->f.subclass = AST_FORMAT_SLINEAR;
125 tmp->f.datalen = tmp->tail * 2;
127 tmp->f.samples = tmp->tail;
129 tmp->f.offset = AST_FRIENDLY_OFFSET;
130 tmp->f.src = __PRETTY_FUNCTION__;
131 tmp->f.data = tmp->buf;
132 /* Reset tail pointer */
138 static int ilbctolin_framein(struct ast_translator_pvt *tmp, struct ast_frame *f)
140 /* Assuming there's space left, decode into the current buffer at
141 the tail location. Read in as many frames as there are */
145 if (f->datalen == 0) { /* native PLC */
146 if (tmp->tail + 240 < sizeof(tmp->buf)/2) {
147 iLBC_decode(tmpf, NULL, &tmp->dec, 0);
149 tmp->buf[tmp->tail + i] = tmpf[i];
152 ast_log(LOG_WARNING, "Out of buffer space\n");
157 if (f->datalen % 50) {
158 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);
162 for (x=0;x<f->datalen;x+=50) {
163 if (tmp->tail + 240 < sizeof(tmp->buf)/2) {
164 iLBC_decode(tmpf, f->data + x, &tmp->dec, 1);
166 tmp->buf[tmp->tail + i] = tmpf[i];
169 ast_log(LOG_WARNING, "Out of buffer space\n");
176 static int lintoilbc_framein(struct ast_translator_pvt *tmp, struct ast_frame *f)
178 /* Just add the frames to our stream */
179 /* XXX We should look at how old the rest of our stream is, and if it
180 is too old, then we should overwrite it entirely, otherwise we can
181 get artifacts of earlier talk that do not belong */
182 if (tmp->tail + f->datalen/2 < sizeof(tmp->buf) / 2) {
183 memcpy((tmp->buf + tmp->tail), f->data, f->datalen);
184 tmp->tail += f->datalen/2;
186 ast_log(LOG_WARNING, "Out of buffer space\n");
192 static struct ast_frame *lintoilbc_frameout(struct ast_translator_pvt *tmp)
196 /* We can't work on anything less than a frame in size */
199 tmp->f.frametype = AST_FRAME_VOICE;
200 tmp->f.subclass = AST_FORMAT_ILBC;
202 tmp->f.offset = AST_FRIENDLY_OFFSET;
203 tmp->f.src = __PRETTY_FUNCTION__;
204 tmp->f.data = tmp->outbuf;
205 while(tmp->tail >= 240) {
206 if ((x+1) * 50 >= sizeof(tmp->outbuf)) {
207 ast_log(LOG_WARNING, "Out of buffer space\n");
211 tmpf[i] = tmp->buf[i];
212 /* Encode a frame of data */
213 iLBC_encode(((unsigned char *)(tmp->outbuf)) + (x * 50), tmpf, &tmp->enc);
214 /* Assume 8000 Hz -- 20 ms */
216 /* Move the data at the end of the buffer to the front */
218 memmove(tmp->buf, tmp->buf + 240, tmp->tail * 2);
221 tmp->f.datalen = x * 50;
222 tmp->f.samples = x * 240;
227 fd = open("ilbc.out", O_CREAT|O_TRUNC|O_WRONLY, 0666);
228 write(fd, tmp->f.data, tmp->f.datalen);
236 static void ilbc_destroy_stuff(struct ast_translator_pvt *pvt)
242 static struct ast_translator ilbctolin =
244 AST_FORMAT_ILBC, AST_FORMAT_SLINEAR,
252 static struct ast_translator lintoilbc =
254 AST_FORMAT_SLINEAR, AST_FORMAT_ILBC,
262 int unload_module(void)
265 ast_mutex_lock(&localuser_lock);
266 res = ast_unregister_translator(&lintoilbc);
268 res = ast_unregister_translator(&ilbctolin);
271 ast_mutex_unlock(&localuser_lock);
275 int load_module(void)
278 res=ast_register_translator(&ilbctolin);
280 res=ast_register_translator(&lintoilbc);
282 ast_unregister_translator(&ilbctolin);
286 char *description(void)
294 STANDARD_USECOUNT(res);
300 return ASTERISK_GPL_KEY;