Minor formatting change to test a mantis change ...
[asterisk-bristuff.git] / codecs / codec_ilbc.c
blobdcef62dffa6928fcccd51029a2a0444f7ffbbeae
1 /*
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.
21 /*! \file
23 * \brief Translate between signed linear and Internet Low Bitrate Codec
25 * \ingroup codecs
28 /*** MODULEINFO
29 <defaultenabled>no</defaultenabled>
30 ***/
32 #include "asterisk.h"
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 /* Sample frame data */
44 #include "slin_ilbc_ex.h"
45 #include "ilbc_slin_ex.h"
47 #define USE_ILBC_ENHANCER 0
48 #define ILBC_MS 30
49 /* #define ILBC_MS 20 */
51 #define ILBC_FRAME_LEN 50 /* apparently... */
52 #define ILBC_SAMPLES 240 /* 30ms at 8000 hz */
53 #define BUFFER_SAMPLES 8000
55 struct ilbc_coder_pvt {
56 iLBC_Enc_Inst_t enc;
57 iLBC_Dec_Inst_t dec;
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);
68 return 0;
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);
77 return 0;
80 static struct ast_frame *lintoilbc_sample(void)
82 static struct ast_frame f;
83 f.frametype = AST_FRAME_VOICE;
84 f.subclass = AST_FORMAT_SLINEAR;
85 f.datalen = sizeof(slin_ilbc_ex);
86 f.samples = sizeof(slin_ilbc_ex)/2;
87 f.mallocd = 0;
88 f.offset = 0;
89 f.src = __PRETTY_FUNCTION__;
90 f.data = slin_ilbc_ex;
91 return &f;
94 static struct ast_frame *ilbctolin_sample(void)
96 static struct ast_frame f;
97 f.frametype = AST_FRAME_VOICE;
98 f.subclass = AST_FORMAT_ILBC;
99 f.datalen = sizeof(ilbc_slin_ex);
100 /* All frames are 30 ms long */
101 f.samples = ILBC_SAMPLES;
102 f.mallocd = 0;
103 f.offset = 0;
104 f.src = __PRETTY_FUNCTION__;
105 f.data = ilbc_slin_ex;
106 return &f;
109 /*! \brief decode a frame and store in outbuf */
110 static int ilbctolin_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
112 struct ilbc_coder_pvt *tmp = pvt->pvt;
113 int plc_mode = 1; /* 1 = normal data, 0 = plc */
114 /* Assuming there's space left, decode into the current buffer at
115 the tail location. Read in as many frames as there are */
116 int x,i;
117 int16_t *dst = (int16_t *)pvt->outbuf;
118 float tmpf[ILBC_SAMPLES];
120 if (f->datalen == 0) { /* native PLC, set fake f->datalen and clear plc_mode */
121 f->datalen = ILBC_FRAME_LEN;
122 f->samples = ILBC_SAMPLES;
123 plc_mode = 0; /* do native plc */
124 pvt->samples += ILBC_SAMPLES;
127 if (f->datalen % ILBC_FRAME_LEN) {
128 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);
129 return -1;
132 for (x=0; x < f->datalen ; x += ILBC_FRAME_LEN) {
133 if (pvt->samples + ILBC_SAMPLES > BUFFER_SAMPLES) {
134 ast_log(LOG_WARNING, "Out of buffer space\n");
135 return -1;
137 iLBC_decode(tmpf, plc_mode ? f->data + x : NULL, &tmp->dec, plc_mode);
138 for ( i=0; i < ILBC_SAMPLES; i++)
139 dst[pvt->samples + i] = tmpf[i];
140 pvt->samples += ILBC_SAMPLES;
141 pvt->datalen += 2*ILBC_SAMPLES;
143 return 0;
146 /*! \brief store a frame into a temporary buffer, for later decoding */
147 static int lintoilbc_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
149 struct ilbc_coder_pvt *tmp = pvt->pvt;
151 /* Just add the frames to our stream */
152 /* XXX We should look at how old the rest of our stream is, and if it
153 is too old, then we should overwrite it entirely, otherwise we can
154 get artifacts of earlier talk that do not belong */
155 memcpy(tmp->buf + pvt->samples, f->data, f->datalen);
156 pvt->samples += f->samples;
157 return 0;
160 /*! \brief encode the temporary buffer and generate a frame */
161 static struct ast_frame *lintoilbc_frameout(struct ast_trans_pvt *pvt)
163 struct ilbc_coder_pvt *tmp = pvt->pvt;
164 int datalen = 0;
165 int samples = 0;
167 /* We can't work on anything less than a frame in size */
168 if (pvt->samples < ILBC_SAMPLES)
169 return NULL;
170 while (pvt->samples >= ILBC_SAMPLES) {
171 float tmpf[ILBC_SAMPLES];
172 int i;
174 /* Encode a frame of data */
175 for (i = 0 ; i < ILBC_SAMPLES ; i++)
176 tmpf[i] = tmp->buf[samples + i];
177 iLBC_encode((unsigned char *) pvt->outbuf + datalen, tmpf, &tmp->enc);
179 datalen += ILBC_FRAME_LEN;
180 samples += ILBC_SAMPLES;
181 pvt->samples -= ILBC_SAMPLES;
184 /* Move the data at the end of the buffer to the front */
185 if (pvt->samples)
186 memmove(tmp->buf, tmp->buf + samples, pvt->samples * 2);
188 return ast_trans_frameout(pvt, datalen, samples);
191 static struct ast_translator ilbctolin = {
192 .name = "ilbctolin",
193 .srcfmt = AST_FORMAT_ILBC,
194 .dstfmt = AST_FORMAT_SLINEAR,
195 .newpvt = ilbctolin_new,
196 .framein = ilbctolin_framein,
197 .sample = ilbctolin_sample,
198 .desc_size = sizeof(struct ilbc_coder_pvt),
199 .buf_size = BUFFER_SAMPLES * 2,
200 .native_plc = 1,
203 static struct ast_translator lintoilbc = {
204 .name = "lintoilbc",
205 .srcfmt = AST_FORMAT_SLINEAR,
206 .dstfmt = AST_FORMAT_ILBC,
207 .newpvt = lintoilbc_new,
208 .framein = lintoilbc_framein,
209 .frameout = lintoilbc_frameout,
210 .sample = lintoilbc_sample,
211 .desc_size = sizeof(struct ilbc_coder_pvt),
212 .buf_size = (BUFFER_SAMPLES * ILBC_FRAME_LEN + ILBC_SAMPLES - 1) / ILBC_SAMPLES,
215 static int unload_module(void)
217 int res;
219 res = ast_unregister_translator(&lintoilbc);
220 res |= ast_unregister_translator(&ilbctolin);
222 return res;
225 static int load_module(void)
227 int res;
229 res = ast_register_translator(&ilbctolin);
230 if (!res)
231 res=ast_register_translator(&lintoilbc);
232 else
233 ast_unregister_translator(&ilbctolin);
234 if (res)
235 return AST_MODULE_LOAD_FAILURE;
236 return AST_MODULE_LOAD_SUCCESS;
239 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "iLBC Coder/Decoder");