Debian Codegraph @

codecs/codec_ilbc.c

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * The iLBC code is from The IETF code base and is copyright The Internet Society (2004)
5 *
6 * Copyright (C) 1999 - 2005, Digium, Inc.
7 *
8 * Mark Spencer <[email protected]>
9 *
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.
15 *
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.
19 */
20
21 /*! \file
22 *
23 * \brief Translate between signed linear and Internet Low Bitrate Codec
24 *
25 * \ingroup codecs
26 */
27
28 /*** MODULEINFO
29 <use>ilbc</use>
•30 <defaultenabled>no</defaultenabled>
31 <support_level>core</support_level>
32 ***/
33
34 #include "asterisk.h"
35
36 #include "asterisk/codec.h" /* for AST_MEDIA_TYPE_AUDIO */
37 #include "asterisk/format.h" /* for ast_format_get_attribute_data */
38 #include "asterisk/frame.h" /* for ast_frame, etc */
39 #include "asterisk/linkedlists.h" /* for AST_LIST_NEXT, etc */
40 #include "asterisk/logger.h" /* for ast_log, ast_debug, etc */
41 #include "asterisk/module.h"
42 #include "asterisk/translate.h" /* for ast_trans_pvt, etc */
43
44 #ifdef ILBC_WEBRTC
45 #include <ilbc.h>
46 typedef uint16_t ilbc_bytes;
47 typedef int16_t ilbc_block;
48 #define BUF_TYPE i16
49 #else
50 #include "ilbc/iLBC_encode.h"
51 #include "ilbc/iLBC_decode.h"
52 typedef unsigned char ilbc_bytes;
53 typedef float ilbc_block;
54 #define BUF_TYPE uc
55 #endif
56
57 #include "asterisk/ilbc.h"
58
59 #define USE_ILBC_ENHANCER 0
60 #define BUFFER_SAMPLES 8000
61
62 /* Sample frame data */
63 #include "asterisk/slin.h"
64 #include "ex_ilbc.h"
65
66 struct ilbc_coder_pvt {
67 iLBC_Enc_Inst_t enc;
68 iLBC_Dec_Inst_t dec;
69 /* Enough to store a full second */
70 int16_t buf[BUFFER_SAMPLES];
71 int16_t inited;
72 };
73
74 static int lintoilbc_new(struct ast_trans_pvt *pvt)
75 {
76 struct ilbc_coder_pvt *tmp = pvt->pvt;
77 struct ilbc_attr *attr = pvt->explicit_dst ? ast_format_get_attribute_data(pvt->explicit_dst) : NULL;
78 const unsigned int mode = attr ? attr->mode : 30;
79
80 initEncode(&tmp->enc, mode);
81
82 return 0;
83 }
84
85 static int ilbctolin_new(struct ast_trans_pvt *pvt)
86 {
87 struct ilbc_coder_pvt *tmp = pvt->pvt;
88
89 tmp->inited = 0; /* we do not know the iLBC mode, yet */
90
91 return 0;
92 }
93
94 /*! \brief decode a frame and store in outbuf */
95 static int ilbctolin_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
96 {
97 struct ilbc_coder_pvt *tmp = pvt->pvt;
98 struct ilbc_attr *attr = ast_format_get_attribute_data(f->subclass.format);
99 const unsigned int mode = attr ? attr->mode : 30;
100 const unsigned int sample_rate = pvt->t->dst_codec.sample_rate;
101 const unsigned int samples_per_frame = mode * sample_rate / 1000;
102 const unsigned int octets_per_frame = (mode == 20) ? 38 : 50;
103
104 int plc_mode = 1; /* 1 = normal data, 0 = plc */
105 /* Assuming there's space left, decode into the current buffer at
106 the tail location. Read in as many frames as there are */
107 int x,i;
108 int datalen = f->datalen;
109 int16_t *dst = pvt->outbuf.i16;
110 ilbc_block tmpf[samples_per_frame];
111
112 if (!f->data.ptr && datalen) {
113 ast_debug(1, "issue 16070, ILIB ERROR. data = NULL datalen = %d src = %s\n", datalen, f->src ? f->src : "no src set");
114 f->datalen = 0;
115 datalen = 0;
116 }
117
118 if (datalen == 0) { /* native PLC, set fake datalen and clear plc_mode */
119 datalen = octets_per_frame;
120 f->samples = samples_per_frame;
121 plc_mode = 0; /* do native plc */
122 pvt->samples += samples_per_frame;
123 }
124
125 if (datalen % octets_per_frame) {
126 ast_log(LOG_WARNING, "Huh? An ilbc frame that isn't a multiple of %u bytes long from %s (%d)?\n", octets_per_frame, f->src, datalen);
127 return -1;
128 }
129
130 if (!tmp->inited) {
131 initDecode(&tmp->dec, mode, USE_ILBC_ENHANCER);
132 tmp->inited = 1;
133 }
134
135 for (x = 0; x < datalen; x += octets_per_frame) {
136 if (pvt->samples + samples_per_frame > BUFFER_SAMPLES) {
137 ast_log(LOG_WARNING, "Out of buffer space\n");
138 return -1;
139 }
140 iLBC_decode(tmpf, plc_mode ? f->data.ptr + x : NULL, &tmp->dec, plc_mode);
141 for (i = 0; i < samples_per_frame; i++)
142 dst[pvt->samples + i] = tmpf[i];
143 pvt->samples += samples_per_frame;
144 pvt->datalen += samples_per_frame * 2;
145 }
146 return 0;
147 }
148
149 /*! \brief store a frame into a temporary buffer, for later decoding */
150 static int lintoilbc_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
151 {
152 struct ilbc_coder_pvt *tmp = pvt->pvt;
153
154 /* Just add the frames to our stream */
155 /* XXX We should look at how old the rest of our stream is, and if it
156 is too old, then we should overwrite it entirely, otherwise we can
157 get artifacts of earlier talk that do not belong */
158 memcpy(tmp->buf + pvt->samples, f->data.ptr, f->datalen);
159 pvt->samples += f->samples;
160 return 0;
161 }
162
163 /*! \brief encode the temporary buffer and generate a frame */
164 static struct ast_frame *lintoilbc_frameout(struct ast_trans_pvt *pvt)
165 {
166 struct ilbc_coder_pvt *tmp = pvt->pvt;
167 struct ast_frame *result = NULL;
168 struct ast_frame *last = NULL;
169 int samples = 0; /* output samples */
170
171 struct ilbc_attr *attr = ast_format_get_attribute_data(pvt->f.subclass.format);
172 const unsigned int mode = attr ? attr->mode : 30;
173 const unsigned int sample_rate = pvt->t->dst_codec.sample_rate;
174 const unsigned int samples_per_frame = mode * sample_rate / 1000;
175 const unsigned int octets_per_frame = (mode == 20) ? 38 : 50;
176
177 while (pvt->samples >= samples_per_frame) {
178 struct ast_frame *current;
179 ilbc_block tmpf[samples_per_frame];
180 int i;
181
182 /* Encode a frame of data */
183 for (i = 0; i < samples_per_frame; i++)
184 tmpf[i] = tmp->buf[samples + i];
185 iLBC_encode((ilbc_bytes *) pvt->outbuf.BUF_TYPE, tmpf, &tmp->enc);
186
187 samples += samples_per_frame;
188 pvt->samples -= samples_per_frame;
189
190 current = ast_trans_frameout(pvt, octets_per_frame, samples_per_frame);
191 if (!current) {
192 continue;
193 } else if (last) {
194 AST_LIST_NEXT(last, frame_list) = current;
195 } else {
196 result = current;
197 }
198 last = current;
199 }
200
201 /* Move the data at the end of the buffer to the front */
202 if (samples) {
203 memmove(tmp->buf, tmp->buf + samples, pvt->samples * 2);
204 }
205
206 return result;
207 }
208
209 static struct ast_translator ilbctolin = {
210 .name = "ilbctolin",
211 .src_codec = {
212 .name = "ilbc",
213 .type = AST_MEDIA_TYPE_AUDIO,
214 .sample_rate = 8000,
215 },
216 .dst_codec = {
217 .name = "slin",
218 .type = AST_MEDIA_TYPE_AUDIO,
219 .sample_rate = 8000,
220 },
221 .format = "slin",
222 .newpvt = ilbctolin_new,
223 .framein = ilbctolin_framein,
224 .sample = ilbc_sample,
225 .desc_size = sizeof(struct ilbc_coder_pvt),
226 .buf_size = BUFFER_SAMPLES * 2,
227 .native_plc = 1,
228 };
229
230 static struct ast_translator lintoilbc = {
231 .name = "lintoilbc",
232 .src_codec = {
233 .name = "slin",
234 .type = AST_MEDIA_TYPE_AUDIO,
235 .sample_rate = 8000,
236 },
237 .dst_codec = {
238 .name = "ilbc",
239 .type = AST_MEDIA_TYPE_AUDIO,
240 .sample_rate = 8000,
241 },
242 .format = "ilbc",
243 .newpvt = lintoilbc_new,
244 .framein = lintoilbc_framein,
245 .frameout = lintoilbc_frameout,
246 .sample = slin8_sample,
247 .desc_size = sizeof(struct ilbc_coder_pvt),
248 /* frame len (38 bytes), frame size (160 samples), ceil (+ 160 - 1) */
249 .buf_size = (BUFFER_SAMPLES * 38 + 160 - 1) / 160,
250 };
251
252 static int unload_module(void)
253 {
254 int res;
255
256 res = ast_unregister_translator(&lintoilbc);
257 res |= ast_unregister_translator(&ilbctolin);
258
259 return res;
260 }
261
262 static int load_module(void)
263 {
264 int res;
265
266 res = ast_register_translator(&ilbctolin);
267 res |= ast_register_translator(&lintoilbc);
268
269 if (res) {
270 unload_module();
271 return AST_MODULE_LOAD_DECLINE;
272 }
273
274 return AST_MODULE_LOAD_SUCCESS;
275 }
276
277 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "iLBC Coder/Decoder");