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-2004, 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 % 50) {
146 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);
150 for (x=0;x<f->datalen;x+=50) {
151 if (tmp->tail + 240 < sizeof(tmp->buf)/2) {
152 iLBC_decode(tmpf, f->data + x, &tmp->dec, 1);
154 tmp->buf[tmp->tail + i] = tmpf[i];
157 ast_log(LOG_WARNING, "Out of buffer space\n");
164 static int lintoilbc_framein(struct ast_translator_pvt *tmp, struct ast_frame *f)
166 /* Just add the frames to our stream */
167 /* XXX We should look at how old the rest of our stream is, and if it
168 is too old, then we should overwrite it entirely, otherwise we can
169 get artifacts of earlier talk that do not belong */
170 if (tmp->tail + f->datalen/2 < sizeof(tmp->buf) / 2) {
171 memcpy((tmp->buf + tmp->tail), f->data, f->datalen);
172 tmp->tail += f->datalen/2;
174 ast_log(LOG_WARNING, "Out of buffer space\n");
180 static struct ast_frame *lintoilbc_frameout(struct ast_translator_pvt *tmp)
184 /* We can't work on anything less than a frame in size */
187 tmp->f.frametype = AST_FRAME_VOICE;
188 tmp->f.subclass = AST_FORMAT_ILBC;
190 tmp->f.offset = AST_FRIENDLY_OFFSET;
191 tmp->f.src = __PRETTY_FUNCTION__;
192 tmp->f.data = tmp->outbuf;
193 while(tmp->tail >= 240) {
194 if ((x+1) * 50 >= sizeof(tmp->outbuf)) {
195 ast_log(LOG_WARNING, "Out of buffer space\n");
199 tmpf[i] = tmp->buf[i];
200 /* Encode a frame of data */
201 iLBC_encode(((unsigned char *)(tmp->outbuf)) + (x * 50), tmpf, &tmp->enc);
202 /* Assume 8000 Hz -- 20 ms */
204 /* Move the data at the end of the buffer to the front */
206 memmove(tmp->buf, tmp->buf + 240, tmp->tail * 2);
209 tmp->f.datalen = x * 50;
210 tmp->f.samples = x * 240;
215 fd = open("ilbc.out", O_CREAT|O_TRUNC|O_WRONLY, 0666);
216 write(fd, tmp->f.data, tmp->f.datalen);
224 static void ilbc_destroy_stuff(struct ast_translator_pvt *pvt)
230 static struct ast_translator ilbctolin =
232 AST_FORMAT_ILBC, AST_FORMAT_SLINEAR,
240 static struct ast_translator lintoilbc =
242 AST_FORMAT_SLINEAR, AST_FORMAT_ILBC,
250 int unload_module(void)
253 ast_mutex_lock(&localuser_lock);
254 res = ast_unregister_translator(&lintoilbc);
256 res = ast_unregister_translator(&ilbctolin);
259 ast_mutex_unlock(&localuser_lock);
263 int load_module(void)
266 res=ast_register_translator(&ilbctolin);
268 res=ast_register_translator(&lintoilbc);
270 ast_unregister_translator(&ilbctolin);
274 char *description(void)
282 STANDARD_USECOUNT(res);
288 return ASTERISK_GPL_KEY;