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 #define BUFFER_SAMPLES 8000
51 #define GSM_SAMPLES 160
52 #define GSM_FRAME_LEN 33
53 #define MSGSM_FRAME_LEN 65
55 /* Sample frame data */
56 #include "asterisk/slin.h"
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 /*! \brief decode and store in outbuf. */
72 static int gsmtolin_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
74 struct gsm_translator_pvt *tmp = pvt->pvt;
76 int16_t *dst = pvt->outbuf.i16;
77 /* guess format from frame len. 65 for MSGSM, 33 for regular GSM */
78 int flen = (f->datalen % MSGSM_FRAME_LEN == 0) ?
79 MSGSM_FRAME_LEN : GSM_FRAME_LEN;
81 for (x=0; x < f->datalen; x += flen) {
82 unsigned char data[2 * GSM_FRAME_LEN];
85 if (flen == MSGSM_FRAME_LEN) {
88 /* Translate MSGSM format to Real GSM format before feeding in */
89 /* XXX what's the point here! we should just work
92 conv65(f->data.ptr + x, data);
95 src = f->data.ptr + x;
97 /* XXX maybe we don't need to check */
98 if (pvt->samples + len > BUFFER_SAMPLES) {
99 ast_log(LOG_WARNING, "Out of buffer space\n");
102 if (gsm_decode(tmp->gsm, src, dst + pvt->samples)) {
103 ast_log(LOG_WARNING, "Invalid GSM data (1)\n");
106 pvt->samples += GSM_SAMPLES;
107 pvt->datalen += 2 * GSM_SAMPLES;
108 if (flen == MSGSM_FRAME_LEN) {
109 if (gsm_decode(tmp->gsm, data + GSM_FRAME_LEN, dst + pvt->samples)) {
110 ast_log(LOG_WARNING, "Invalid GSM data (2)\n");
113 pvt->samples += GSM_SAMPLES;
114 pvt->datalen += 2 * GSM_SAMPLES;
120 /*! \brief store samples into working buffer for later decode */
121 static int lintogsm_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
123 struct gsm_translator_pvt *tmp = pvt->pvt;
125 /* XXX We should look at how old the rest of our stream is, and if it
126 is too old, then we should overwrite it entirely, otherwise we can
127 get artifacts of earlier talk that do not belong */
128 if (pvt->samples + f->samples > BUFFER_SAMPLES) {
129 ast_log(LOG_WARNING, "Out of buffer space\n");
132 memcpy(tmp->buf + pvt->samples, f->data.ptr, f->datalen);
133 pvt->samples += f->samples;
137 /*! \brief encode and produce a frame */
138 static struct ast_frame *lintogsm_frameout(struct ast_trans_pvt *pvt)
140 struct gsm_translator_pvt *tmp = pvt->pvt;
144 /* We can't work on anything less than a frame in size */
145 if (pvt->samples < GSM_SAMPLES)
147 while (pvt->samples >= GSM_SAMPLES) {
148 /* Encode a frame of data */
149 gsm_encode(tmp->gsm, tmp->buf + samples, (gsm_byte *) pvt->outbuf.c + datalen);
150 datalen += GSM_FRAME_LEN;
151 samples += GSM_SAMPLES;
152 pvt->samples -= GSM_SAMPLES;
155 /* Move the data at the end of the buffer to the front */
157 memmove(tmp->buf, tmp->buf + samples, pvt->samples * 2);
159 return ast_trans_frameout(pvt, datalen, samples);
162 static void gsm_destroy_stuff(struct ast_trans_pvt *pvt)
164 struct gsm_translator_pvt *tmp = pvt->pvt;
166 gsm_destroy(tmp->gsm);
169 static struct ast_translator gsmtolin = {
171 .srcfmt = AST_FORMAT_GSM,
172 .dstfmt = AST_FORMAT_SLINEAR,
174 .framein = gsmtolin_framein,
175 .destroy = gsm_destroy_stuff,
176 .sample = gsm_sample,
177 .buffer_samples = BUFFER_SAMPLES,
178 .buf_size = BUFFER_SAMPLES * 2,
179 .desc_size = sizeof (struct gsm_translator_pvt ),
180 .plc_samples = GSM_SAMPLES,
183 static struct ast_translator lintogsm = {
185 .srcfmt = AST_FORMAT_SLINEAR,
186 .dstfmt = AST_FORMAT_GSM,
188 .framein = lintogsm_framein,
189 .frameout = lintogsm_frameout,
190 .destroy = gsm_destroy_stuff,
191 .sample = slin8_sample,
192 .desc_size = sizeof (struct gsm_translator_pvt ),
193 .buf_size = (BUFFER_SAMPLES * GSM_FRAME_LEN + GSM_SAMPLES - 1)/GSM_SAMPLES,
197 static int parse_config(int reload)
199 struct ast_variable *var;
200 struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
201 struct ast_config *cfg = ast_config_load("codecs.conf", config_flags);
202 if (cfg == CONFIG_STATUS_FILEMISSING || cfg == CONFIG_STATUS_FILEUNCHANGED || cfg == CONFIG_STATUS_FILEINVALID)
204 for (var = ast_variable_browse(cfg, "plc"); var; var = var->next) {
205 if (!strcasecmp(var->name, "genericplc")) {
206 gsmtolin.useplc = ast_true(var->value) ? 1 : 0;
207 ast_verb(3, "codec_gsm: %susing generic PLC\n", gsmtolin.useplc ? "" : "not ");
210 ast_config_destroy(cfg);
214 /*! \brief standard module glue */
215 static int reload(void)
217 if (parse_config(1)) {
218 return AST_MODULE_LOAD_DECLINE;
220 return AST_MODULE_LOAD_SUCCESS;
223 static int unload_module(void)
227 res = ast_unregister_translator(&lintogsm);
229 res = ast_unregister_translator(&gsmtolin);
234 static int load_module(void)
239 return AST_MODULE_LOAD_DECLINE;
240 res = ast_register_translator(&gsmtolin);
242 res=ast_register_translator(&lintogsm);
244 ast_unregister_translator(&gsmtolin);
246 return AST_MODULE_LOAD_FAILURE;
247 return AST_MODULE_LOAD_SUCCESS;
250 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "GSM Coder/Decoder",
252 .unload = unload_module,