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 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
35 #include <netinet/in.h>
39 #include "asterisk/lock.h"
40 #include "asterisk/translate.h"
41 #include "asterisk/module.h"
42 #include "asterisk/logger.h"
43 #include "asterisk/channel.h"
44 #include "asterisk/utils.h"
46 #include "ilbc/iLBC_encode.h"
47 #include "ilbc/iLBC_decode.h"
49 /* Sample frame data */
50 #include "slin_ilbc_ex.h"
51 #include "ilbc_slin_ex.h"
53 #define USE_ILBC_ENHANCER 0
55 /* #define ILBC_MS 20 */
57 #define ILBC_FRAME_LEN 50 /* apparently... */
58 #define ILBC_SAMPLES 240 /* 30ms at 8000 hz */
59 #define BUFFER_SAMPLES 8000
61 static char *tdesc = "iLBC/PCM16 (signed linear) Codec Translator";
63 struct ilbc_coder_pvt {
66 /* Enough to store a full second */
67 int16_t buf[BUFFER_SAMPLES];
70 static int lintoilbc_new(struct ast_trans_pvt *pvt)
72 struct ilbc_coder_pvt *tmp = pvt->pvt;
74 initEncode(&tmp->enc, ILBC_MS);
79 static int ilbctolin_new(struct ast_trans_pvt *pvt)
81 struct ilbc_coder_pvt *tmp = pvt->pvt;
83 initDecode(&tmp->dec, ILBC_MS, USE_ILBC_ENHANCER);
88 static struct ast_frame *lintoilbc_sample(void)
90 static struct ast_frame f;
91 f.frametype = AST_FRAME_VOICE;
92 f.subclass = AST_FORMAT_SLINEAR;
93 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 */
109 f.samples = ILBC_SAMPLES;
112 f.src = __PRETTY_FUNCTION__;
113 f.data = ilbc_slin_ex;
117 /*! \brief decode a frame and store in outbuf */
118 static int ilbctolin_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
120 struct ilbc_coder_pvt *tmp = pvt->pvt;
121 int plc_mode = 1; /* 1 = normal data, 0 = plc */
122 /* Assuming there's space left, decode into the current buffer at
123 the tail location. Read in as many frames as there are */
125 int16_t *dst = (int16_t *)pvt->outbuf;
126 float tmpf[ILBC_SAMPLES];
128 if (f->datalen == 0) { /* native PLC, set fake f->datalen and clear plc_mode */
129 f->datalen = ILBC_FRAME_LEN;
130 f->samples = ILBC_SAMPLES;
131 plc_mode = 0; /* do native plc */
132 pvt->samples += ILBC_SAMPLES;
135 if (f->datalen % ILBC_FRAME_LEN) {
136 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);
140 for (x=0; x < f->datalen ; x += ILBC_FRAME_LEN) {
141 if (pvt->samples + ILBC_SAMPLES > BUFFER_SAMPLES) {
142 ast_log(LOG_WARNING, "Out of buffer space\n");
145 iLBC_decode(tmpf, plc_mode ? f->data + x : NULL, &tmp->dec, plc_mode);
146 for ( i=0; i < ILBC_SAMPLES; i++)
147 dst[pvt->samples + i] = tmpf[i];
148 pvt->samples += ILBC_SAMPLES;
149 pvt->datalen += 2*ILBC_SAMPLES;
154 /*! \brief store a frame into a temporary buffer, for later decoding */
155 static int lintoilbc_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
157 struct ilbc_coder_pvt *tmp = pvt->pvt;
159 /* Just add the frames to our stream */
160 /* XXX We should look at how old the rest of our stream is, and if it
161 is too old, then we should overwrite it entirely, otherwise we can
162 get artifacts of earlier talk that do not belong */
163 memcpy(tmp->buf + pvt->samples, f->data, f->datalen);
164 pvt->samples += f->samples;
168 /*! \brief encode the temporary buffer and generate a frame */
169 static struct ast_frame *lintoilbc_frameout(struct ast_trans_pvt *pvt)
171 struct ilbc_coder_pvt *tmp = pvt->pvt;
175 /* We can't work on anything less than a frame in size */
176 if (pvt->samples < ILBC_SAMPLES)
178 while (pvt->samples >= ILBC_SAMPLES) {
179 float tmpf[ILBC_SAMPLES];
181 /* Encode a frame of data */
182 for ( i = 0 ; i < ILBC_SAMPLES ; i++ )
183 tmpf[i] = tmp->buf[i];
184 iLBC_encode((unsigned char *) pvt->outbuf + datalen, tmpf, &tmp->enc);
185 datalen += ILBC_FRAME_LEN;
186 samples += ILBC_SAMPLES;
187 pvt->samples -= ILBC_SAMPLES;
188 /* Move the data at the end of the buffer to the front */
190 memmove(tmp->buf, tmp->buf + ILBC_SAMPLES, pvt->samples * 2);
192 return ast_trans_frameout(pvt, datalen, samples);
195 static struct ast_translator ilbctolin = {
197 .srcfmt = AST_FORMAT_ILBC,
198 .dstfmt = AST_FORMAT_SLINEAR,
199 .newpvt = ilbctolin_new,
200 .framein = ilbctolin_framein,
201 .sample = ilbctolin_sample,
202 .desc_size = sizeof(struct ilbc_coder_pvt),
203 .buf_size = BUFFER_SAMPLES * 2,
206 static struct ast_translator lintoilbc = {
208 .srcfmt = AST_FORMAT_SLINEAR,
209 .dstfmt = AST_FORMAT_ILBC,
210 .newpvt = lintoilbc_new,
211 .framein = lintoilbc_framein,
212 .frameout = lintoilbc_frameout,
213 .sample = lintoilbc_sample,
214 .desc_size = sizeof(struct ilbc_coder_pvt),
215 .buf_size = (BUFFER_SAMPLES * ILBC_FRAME_LEN + ILBC_SAMPLES - 1) / ILBC_SAMPLES,
218 static int unload_module(void *mod)
221 res = ast_unregister_translator(&lintoilbc);
222 res |= ast_unregister_translator(&ilbctolin);
226 static int load_module(void *mod)
229 res = ast_register_translator(&ilbctolin, mod);
231 res=ast_register_translator(&lintoilbc, mod);
233 ast_unregister_translator(&ilbctolin);
237 static const char *description(void)
242 static const char *key(void)
244 return ASTERISK_GPL_KEY;
247 STD_MOD(MOD_1, NULL, NULL, NULL);