2 * Asterisk -- An open source telephony toolkit.
4 * The GSM code is from TOAST. Copyright information for that package is available
5 * in the GSM directory.
7 * Copyright (C) 1999 - 2005, Digium, Inc.
9 * Mark Spencer <markster@digium.com>
11 * See http://www.asterisk.org for more information about
12 * the Asterisk project. Please do not directly contact
13 * any of the maintainers of this project for assistance;
14 * the project provides a web site, mailing lists and IRC
15 * channels for your use.
17 * This program is free software, distributed under the terms of
18 * the GNU General Public License Version 2. See the LICENSE file
19 * at the top of the source tree.
24 * \brief Translate between signed linear and Global System for Mobile Communications (GSM)
35 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
37 #include "asterisk/translate.h"
38 #include "asterisk/config.h"
39 #include "asterisk/module.h"
40 #include "asterisk/utils.h"
42 #ifdef HAVE_GSM_HEADER
44 #elif defined(HAVE_GSM_GSM_HEADER)
48 #include "../formats/msgsm.h"
50 /* Sample frame data */
51 #include "slin_gsm_ex.h"
52 #include "gsm_slin_ex.h"
54 #define BUFFER_SAMPLES 8000
55 #define GSM_SAMPLES 160
56 #define GSM_FRAME_LEN 33
57 #define MSGSM_FRAME_LEN 65
59 struct gsm_translator_pvt { /* both gsm2lin and lin2gsm */
61 int16_t buf[BUFFER_SAMPLES]; /* lin2gsm, temporary storage */
64 static int gsm_new(struct ast_trans_pvt *pvt)
66 struct gsm_translator_pvt *tmp = pvt->pvt;
68 return (tmp->gsm = gsm_create()) ? 0 : -1;
71 static struct ast_frame *lintogsm_sample(void)
73 static struct ast_frame f;
74 f.frametype = AST_FRAME_VOICE;
75 f.subclass = AST_FORMAT_SLINEAR;
76 f.datalen = sizeof(slin_gsm_ex);
78 f.samples = sizeof(slin_gsm_ex)/2;
81 f.src = __PRETTY_FUNCTION__;
82 f.data.ptr = slin_gsm_ex;
86 static struct ast_frame *gsmtolin_sample(void)
88 static struct ast_frame f;
89 f.frametype = AST_FRAME_VOICE;
90 f.subclass = AST_FORMAT_GSM;
91 f.datalen = sizeof(gsm_slin_ex);
92 /* All frames are 20 ms long */
93 f.samples = GSM_SAMPLES;
96 f.src = __PRETTY_FUNCTION__;
97 f.data.ptr = gsm_slin_ex;
101 /*! \brief decode and store in outbuf. */
102 static int gsmtolin_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
104 struct gsm_translator_pvt *tmp = pvt->pvt;
106 int16_t *dst = pvt->outbuf.i16;
107 /* guess format from frame len. 65 for MSGSM, 33 for regular GSM */
108 int flen = (f->datalen % MSGSM_FRAME_LEN == 0) ?
109 MSGSM_FRAME_LEN : GSM_FRAME_LEN;
111 for (x=0; x < f->datalen; x += flen) {
112 unsigned char data[2 * GSM_FRAME_LEN];
115 if (flen == MSGSM_FRAME_LEN) {
118 /* Translate MSGSM format to Real GSM format before feeding in */
119 /* XXX what's the point here! we should just work
120 * on the full format.
122 conv65(f->data.ptr + x, data);
125 src = f->data.ptr + x;
127 /* XXX maybe we don't need to check */
128 if (pvt->samples + len > BUFFER_SAMPLES) {
129 ast_log(LOG_WARNING, "Out of buffer space\n");
132 if (gsm_decode(tmp->gsm, src, dst + pvt->samples)) {
133 ast_log(LOG_WARNING, "Invalid GSM data (1)\n");
136 pvt->samples += GSM_SAMPLES;
137 pvt->datalen += 2 * GSM_SAMPLES;
138 if (flen == MSGSM_FRAME_LEN) {
139 if (gsm_decode(tmp->gsm, data + GSM_FRAME_LEN, dst + pvt->samples)) {
140 ast_log(LOG_WARNING, "Invalid GSM data (2)\n");
143 pvt->samples += GSM_SAMPLES;
144 pvt->datalen += 2 * GSM_SAMPLES;
150 /*! \brief store samples into working buffer for later decode */
151 static int lintogsm_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
153 struct gsm_translator_pvt *tmp = pvt->pvt;
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 if (pvt->samples + f->samples > BUFFER_SAMPLES) {
159 ast_log(LOG_WARNING, "Out of buffer space\n");
162 memcpy(tmp->buf + pvt->samples, f->data.ptr, f->datalen);
163 pvt->samples += f->samples;
167 /*! \brief encode and produce a frame */
168 static struct ast_frame *lintogsm_frameout(struct ast_trans_pvt *pvt)
170 struct gsm_translator_pvt *tmp = pvt->pvt;
174 /* We can't work on anything less than a frame in size */
175 if (pvt->samples < GSM_SAMPLES)
177 while (pvt->samples >= GSM_SAMPLES) {
178 /* Encode a frame of data */
179 gsm_encode(tmp->gsm, tmp->buf + samples, (gsm_byte *) pvt->outbuf.c + datalen);
180 datalen += GSM_FRAME_LEN;
181 samples += GSM_SAMPLES;
182 pvt->samples -= GSM_SAMPLES;
185 /* Move the data at the end of the buffer to the front */
187 memmove(tmp->buf, tmp->buf + samples, pvt->samples * 2);
189 return ast_trans_frameout(pvt, datalen, samples);
192 static void gsm_destroy_stuff(struct ast_trans_pvt *pvt)
194 struct gsm_translator_pvt *tmp = pvt->pvt;
196 gsm_destroy(tmp->gsm);
199 static struct ast_translator gsmtolin = {
201 .srcfmt = AST_FORMAT_GSM,
202 .dstfmt = AST_FORMAT_SLINEAR,
204 .framein = gsmtolin_framein,
205 .destroy = gsm_destroy_stuff,
206 .sample = gsmtolin_sample,
207 .buffer_samples = BUFFER_SAMPLES,
208 .buf_size = BUFFER_SAMPLES * 2,
209 .desc_size = sizeof (struct gsm_translator_pvt ),
210 .plc_samples = GSM_SAMPLES,
213 static struct ast_translator lintogsm = {
215 .srcfmt = AST_FORMAT_SLINEAR,
216 .dstfmt = AST_FORMAT_GSM,
218 .framein = lintogsm_framein,
219 .frameout = lintogsm_frameout,
220 .destroy = gsm_destroy_stuff,
221 .sample = lintogsm_sample,
222 .desc_size = sizeof (struct gsm_translator_pvt ),
223 .buf_size = (BUFFER_SAMPLES * GSM_FRAME_LEN + GSM_SAMPLES - 1)/GSM_SAMPLES,
227 static int parse_config(int reload)
229 struct ast_variable *var;
230 struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
231 struct ast_config *cfg = ast_config_load("codecs.conf", config_flags);
234 if (cfg == CONFIG_STATUS_FILEUNCHANGED)
236 for (var = ast_variable_browse(cfg, "plc"); var; var = var->next) {
237 if (!strcasecmp(var->name, "genericplc")) {
238 gsmtolin.useplc = ast_true(var->value) ? 1 : 0;
239 ast_verb(3, "codec_gsm: %susing generic PLC\n", gsmtolin.useplc ? "" : "not ");
242 ast_config_destroy(cfg);
246 /*! \brief standard module glue */
247 static int reload(void)
249 if (parse_config(1)) {
250 return AST_MODULE_LOAD_DECLINE;
252 return AST_MODULE_LOAD_SUCCESS;
255 static int unload_module(void)
259 res = ast_unregister_translator(&lintogsm);
261 res = ast_unregister_translator(&gsmtolin);
266 static int load_module(void)
271 return AST_MODULE_LOAD_DECLINE;
272 res = ast_register_translator(&gsmtolin);
274 res=ast_register_translator(&lintogsm);
276 ast_unregister_translator(&gsmtolin);
278 return AST_MODULE_LOAD_FAILURE;
279 return AST_MODULE_LOAD_SUCCESS;
282 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "GSM Coder/Decoder",
284 .unload = unload_module,