Actually use Zaptel CFLAGS if using Zaptel instead of DAHDI
[asterisk-bristuff.git] / codecs / codec_ilbc.c
blobb5fc9bf32c6e9d235ed14ac2afff193cb5ad3ad5
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 <fcntl.h>
37 #include <stdlib.h>
38 #include <unistd.h>
39 #include <netinet/in.h>
40 #include <string.h>
41 #include <stdio.h>
43 #include "asterisk/lock.h"
44 #include "asterisk/translate.h"
45 #include "asterisk/module.h"
46 #include "asterisk/logger.h"
47 #include "asterisk/channel.h"
48 #include "asterisk/utils.h"
50 #include "ilbc/iLBC_encode.h"
51 #include "ilbc/iLBC_decode.h"
53 /* Sample frame data */
54 #include "slin_ilbc_ex.h"
55 #include "ilbc_slin_ex.h"
57 #define USE_ILBC_ENHANCER 0
58 #define ILBC_MS 30
59 /* #define ILBC_MS 20 */
61 #define ILBC_FRAME_LEN 50 /* apparently... */
62 #define ILBC_SAMPLES 240 /* 30ms at 8000 hz */
63 #define BUFFER_SAMPLES 8000
65 struct ilbc_coder_pvt {
66 iLBC_Enc_Inst_t enc;
67 iLBC_Dec_Inst_t dec;
68 /* Enough to store a full second */
69 int16_t buf[BUFFER_SAMPLES];
72 static int lintoilbc_new(struct ast_trans_pvt *pvt)
74 struct ilbc_coder_pvt *tmp = pvt->pvt;
76 initEncode(&tmp->enc, ILBC_MS);
78 return 0;
81 static int ilbctolin_new(struct ast_trans_pvt *pvt)
83 struct ilbc_coder_pvt *tmp = pvt->pvt;
85 initDecode(&tmp->dec, ILBC_MS, USE_ILBC_ENHANCER);
87 return 0;
90 static struct ast_frame *lintoilbc_sample(void)
92 static struct ast_frame f;
93 f.frametype = AST_FRAME_VOICE;
94 f.subclass = AST_FORMAT_SLINEAR;
95 f.datalen = sizeof(slin_ilbc_ex);
96 f.samples = sizeof(slin_ilbc_ex)/2;
97 f.mallocd = 0;
98 f.offset = 0;
99 f.src = __PRETTY_FUNCTION__;
100 f.data = slin_ilbc_ex;
101 return &f;
104 static struct ast_frame *ilbctolin_sample(void)
106 static struct ast_frame f;
107 f.frametype = AST_FRAME_VOICE;
108 f.subclass = AST_FORMAT_ILBC;
109 f.datalen = sizeof(ilbc_slin_ex);
110 /* All frames are 30 ms long */
111 f.samples = ILBC_SAMPLES;
112 f.mallocd = 0;
113 f.offset = 0;
114 f.src = __PRETTY_FUNCTION__;
115 f.data = ilbc_slin_ex;
116 return &f;
119 /*! \brief decode a frame and store in outbuf */
120 static int ilbctolin_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
122 struct ilbc_coder_pvt *tmp = pvt->pvt;
123 int plc_mode = 1; /* 1 = normal data, 0 = plc */
124 /* Assuming there's space left, decode into the current buffer at
125 the tail location. Read in as many frames as there are */
126 int x,i;
127 int16_t *dst = (int16_t *)pvt->outbuf;
128 float tmpf[ILBC_SAMPLES];
130 if (f->datalen == 0) { /* native PLC, set fake f->datalen and clear plc_mode */
131 f->datalen = ILBC_FRAME_LEN;
132 f->samples = ILBC_SAMPLES;
133 plc_mode = 0; /* do native plc */
134 pvt->samples += ILBC_SAMPLES;
137 if (f->datalen % ILBC_FRAME_LEN) {
138 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);
139 return -1;
142 for (x=0; x < f->datalen ; x += ILBC_FRAME_LEN) {
143 if (pvt->samples + ILBC_SAMPLES > BUFFER_SAMPLES) {
144 ast_log(LOG_WARNING, "Out of buffer space\n");
145 return -1;
147 iLBC_decode(tmpf, plc_mode ? f->data + x : NULL, &tmp->dec, plc_mode);
148 for ( i=0; i < ILBC_SAMPLES; i++)
149 dst[pvt->samples + i] = tmpf[i];
150 pvt->samples += ILBC_SAMPLES;
151 pvt->datalen += 2*ILBC_SAMPLES;
153 return 0;
156 /*! \brief store a frame into a temporary buffer, for later decoding */
157 static int lintoilbc_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
159 struct ilbc_coder_pvt *tmp = pvt->pvt;
161 /* Just add the frames to our stream */
162 /* XXX We should look at how old the rest of our stream is, and if it
163 is too old, then we should overwrite it entirely, otherwise we can
164 get artifacts of earlier talk that do not belong */
165 memcpy(tmp->buf + pvt->samples, f->data, f->datalen);
166 pvt->samples += f->samples;
167 return 0;
170 /*! \brief encode the temporary buffer and generate a frame */
171 static struct ast_frame *lintoilbc_frameout(struct ast_trans_pvt *pvt)
173 struct ilbc_coder_pvt *tmp = pvt->pvt;
174 int datalen = 0;
175 int samples = 0;
177 /* We can't work on anything less than a frame in size */
178 if (pvt->samples < ILBC_SAMPLES)
179 return NULL;
180 while (pvt->samples >= ILBC_SAMPLES) {
181 float tmpf[ILBC_SAMPLES];
182 int i;
184 /* Encode a frame of data */
185 for (i = 0 ; i < ILBC_SAMPLES ; i++)
186 tmpf[i] = tmp->buf[samples + i];
187 iLBC_encode((unsigned char *) pvt->outbuf + datalen, tmpf, &tmp->enc);
189 datalen += ILBC_FRAME_LEN;
190 samples += ILBC_SAMPLES;
191 pvt->samples -= ILBC_SAMPLES;
194 /* Move the data at the end of the buffer to the front */
195 if (pvt->samples)
196 memmove(tmp->buf, tmp->buf + samples, pvt->samples * 2);
198 return ast_trans_frameout(pvt, datalen, samples);
201 static struct ast_translator ilbctolin = {
202 .name = "ilbctolin",
203 .srcfmt = AST_FORMAT_ILBC,
204 .dstfmt = AST_FORMAT_SLINEAR,
205 .newpvt = ilbctolin_new,
206 .framein = ilbctolin_framein,
207 .sample = ilbctolin_sample,
208 .desc_size = sizeof(struct ilbc_coder_pvt),
209 .buf_size = BUFFER_SAMPLES * 2,
210 .native_plc = 1,
213 static struct ast_translator lintoilbc = {
214 .name = "lintoilbc",
215 .srcfmt = AST_FORMAT_SLINEAR,
216 .dstfmt = AST_FORMAT_ILBC,
217 .newpvt = lintoilbc_new,
218 .framein = lintoilbc_framein,
219 .frameout = lintoilbc_frameout,
220 .sample = lintoilbc_sample,
221 .desc_size = sizeof(struct ilbc_coder_pvt),
222 .buf_size = (BUFFER_SAMPLES * ILBC_FRAME_LEN + ILBC_SAMPLES - 1) / ILBC_SAMPLES,
225 static int unload_module(void)
227 int res;
229 res = ast_unregister_translator(&lintoilbc);
230 res |= ast_unregister_translator(&ilbctolin);
232 return res;
235 static int load_module(void)
237 int res;
239 res = ast_register_translator(&ilbctolin);
240 if (!res)
241 res=ast_register_translator(&lintoilbc);
242 else
243 ast_unregister_translator(&ilbctolin);
245 return res;
248 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "iLBC Coder/Decoder");