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
29 <defaultenabled>no</defaultenabled>
34 ASTERISK_FILE_VERSION(__FILE__
, "$Revision$")
39 #include <netinet/in.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
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
{
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
);
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
);
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;
99 f
.src
= __PRETTY_FUNCTION__
;
100 f
.data
= slin_ilbc_ex
;
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
;
114 f
.src
= __PRETTY_FUNCTION__
;
115 f
.data
= ilbc_slin_ex
;
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 */
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
);
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");
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
;
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
;
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
;
177 /* We can't work on anything less than a frame in size */
178 if (pvt
->samples
< ILBC_SAMPLES
)
180 while (pvt
->samples
>= ILBC_SAMPLES
) {
181 float tmpf
[ILBC_SAMPLES
];
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 */
196 memmove(tmp
->buf
, tmp
->buf
+ samples
, pvt
->samples
* 2);
198 return ast_trans_frameout(pvt
, datalen
, samples
);
201 static struct ast_translator 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,
213 static struct ast_translator 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)
229 res
= ast_unregister_translator(&lintoilbc
);
230 res
|= ast_unregister_translator(&ilbctolin
);
235 static int load_module(void)
239 res
= ast_register_translator(&ilbctolin
);
241 res
=ast_register_translator(&lintoilbc
);
243 ast_unregister_translator(&ilbctolin
);
248 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY
, "iLBC Coder/Decoder");