(closes issue #13493)
[asterisk-bristuff.git] / codecs / codec_gsm.c
blob8a37493190655e540ddf113ce035d3fdb54c9795
1 /*
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.
22 /*! \file
24 * \brief Translate between signed linear and Global System for Mobile Communications (GSM)
26 * \ingroup codecs
29 /*** MODULEINFO
30 <depend>gsm</depend>
31 ***/
33 #include "asterisk.h"
35 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
37 #include <fcntl.h>
38 #include <stdlib.h>
39 #include <unistd.h>
40 #include <netinet/in.h>
41 #include <string.h>
42 #include <stdio.h>
44 #include "asterisk/lock.h"
45 #include "asterisk/translate.h"
46 #include "asterisk/config.h"
47 #include "asterisk/options.h"
48 #include "asterisk/module.h"
49 #include "asterisk/logger.h"
50 #include "asterisk/channel.h"
51 #include "asterisk/utils.h"
53 #ifdef HAVE_GSM_HEADER
54 #include "gsm.h"
55 #elif defined(HAVE_GSM_GSM_HEADER)
56 #include <gsm/gsm.h>
57 #endif
59 #include "../formats/msgsm.h"
61 /* Sample frame data */
62 #include "slin_gsm_ex.h"
63 #include "gsm_slin_ex.h"
65 #define BUFFER_SAMPLES 8000
66 #define GSM_SAMPLES 160
67 #define GSM_FRAME_LEN 33
68 #define MSGSM_FRAME_LEN 65
70 struct gsm_translator_pvt { /* both gsm2lin and lin2gsm */
71 gsm gsm;
72 int16_t buf[BUFFER_SAMPLES]; /* lin2gsm, temporary storage */
75 static int gsm_new(struct ast_trans_pvt *pvt)
77 struct gsm_translator_pvt *tmp = pvt->pvt;
79 return (tmp->gsm = gsm_create()) ? 0 : -1;
82 static struct ast_frame *lintogsm_sample(void)
84 static struct ast_frame f;
85 f.frametype = AST_FRAME_VOICE;
86 f.subclass = AST_FORMAT_SLINEAR;
87 f.datalen = sizeof(slin_gsm_ex);
88 /* Assume 8000 Hz */
89 f.samples = sizeof(slin_gsm_ex)/2;
90 f.mallocd = 0;
91 f.offset = 0;
92 f.src = __PRETTY_FUNCTION__;
93 f.data = slin_gsm_ex;
94 return &f;
97 static struct ast_frame *gsmtolin_sample(void)
99 static struct ast_frame f;
100 f.frametype = AST_FRAME_VOICE;
101 f.subclass = AST_FORMAT_GSM;
102 f.datalen = sizeof(gsm_slin_ex);
103 /* All frames are 20 ms long */
104 f.samples = GSM_SAMPLES;
105 f.mallocd = 0;
106 f.offset = 0;
107 f.src = __PRETTY_FUNCTION__;
108 f.data = gsm_slin_ex;
109 return &f;
112 /*! \brief decode and store in outbuf. */
113 static int gsmtolin_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
115 struct gsm_translator_pvt *tmp = pvt->pvt;
116 int x;
117 int16_t *dst = (int16_t *)pvt->outbuf;
118 /* guess format from frame len. 65 for MSGSM, 33 for regular GSM */
119 int flen = (f->datalen % MSGSM_FRAME_LEN == 0) ?
120 MSGSM_FRAME_LEN : GSM_FRAME_LEN;
122 for (x=0; x < f->datalen; x += flen) {
123 unsigned char data[2 * GSM_FRAME_LEN];
124 unsigned char *src;
125 int len;
126 if (flen == MSGSM_FRAME_LEN) {
127 len = 2*GSM_SAMPLES;
128 src = data;
129 /* Translate MSGSM format to Real GSM format before feeding in */
130 /* XXX what's the point here! we should just work
131 * on the full format.
133 conv65(f->data + x, data);
134 } else {
135 len = GSM_SAMPLES;
136 src = f->data + x;
138 /* XXX maybe we don't need to check */
139 if (pvt->samples + len > BUFFER_SAMPLES) {
140 ast_log(LOG_WARNING, "Out of buffer space\n");
141 return -1;
143 if (gsm_decode(tmp->gsm, src, dst + pvt->samples)) {
144 ast_log(LOG_WARNING, "Invalid GSM data (1)\n");
145 return -1;
147 pvt->samples += GSM_SAMPLES;
148 pvt->datalen += 2 * GSM_SAMPLES;
149 if (flen == MSGSM_FRAME_LEN) {
150 if (gsm_decode(tmp->gsm, data + GSM_FRAME_LEN, dst + pvt->samples)) {
151 ast_log(LOG_WARNING, "Invalid GSM data (2)\n");
152 return -1;
154 pvt->samples += GSM_SAMPLES;
155 pvt->datalen += 2 * GSM_SAMPLES;
158 return 0;
161 /*! \brief store samples into working buffer for later decode */
162 static int lintogsm_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
164 struct gsm_translator_pvt *tmp = pvt->pvt;
166 /* XXX We should look at how old the rest of our stream is, and if it
167 is too old, then we should overwrite it entirely, otherwise we can
168 get artifacts of earlier talk that do not belong */
169 if (pvt->samples + f->samples > BUFFER_SAMPLES) {
170 ast_log(LOG_WARNING, "Out of buffer space\n");
171 return -1;
173 memcpy(tmp->buf + pvt->samples, f->data, f->datalen);
174 pvt->samples += f->samples;
175 return 0;
178 /*! \brief encode and produce a frame */
179 static struct ast_frame *lintogsm_frameout(struct ast_trans_pvt *pvt)
181 struct gsm_translator_pvt *tmp = pvt->pvt;
182 int datalen = 0;
183 int samples = 0;
185 /* We can't work on anything less than a frame in size */
186 if (pvt->samples < GSM_SAMPLES)
187 return NULL;
188 while (pvt->samples >= GSM_SAMPLES) {
189 /* Encode a frame of data */
190 gsm_encode(tmp->gsm, tmp->buf + samples, (gsm_byte *) pvt->outbuf + datalen);
191 datalen += GSM_FRAME_LEN;
192 samples += GSM_SAMPLES;
193 pvt->samples -= GSM_SAMPLES;
196 /* Move the data at the end of the buffer to the front */
197 if (pvt->samples)
198 memmove(tmp->buf, tmp->buf + samples, pvt->samples * 2);
200 return ast_trans_frameout(pvt, datalen, samples);
203 static void gsm_destroy_stuff(struct ast_trans_pvt *pvt)
205 struct gsm_translator_pvt *tmp = pvt->pvt;
206 if (tmp->gsm)
207 gsm_destroy(tmp->gsm);
210 static struct ast_translator gsmtolin = {
211 .name = "gsmtolin",
212 .srcfmt = AST_FORMAT_GSM,
213 .dstfmt = AST_FORMAT_SLINEAR,
214 .newpvt = gsm_new,
215 .framein = gsmtolin_framein,
216 .destroy = gsm_destroy_stuff,
217 .sample = gsmtolin_sample,
218 .buffer_samples = BUFFER_SAMPLES,
219 .buf_size = BUFFER_SAMPLES * 2,
220 .desc_size = sizeof (struct gsm_translator_pvt ),
221 .plc_samples = GSM_SAMPLES,
224 static struct ast_translator lintogsm = {
225 .name = "lintogsm",
226 .srcfmt = AST_FORMAT_SLINEAR,
227 .dstfmt = AST_FORMAT_GSM,
228 .newpvt = gsm_new,
229 .framein = lintogsm_framein,
230 .frameout = lintogsm_frameout,
231 .destroy = gsm_destroy_stuff,
232 .sample = lintogsm_sample,
233 .desc_size = sizeof (struct gsm_translator_pvt ),
234 .buf_size = (BUFFER_SAMPLES * GSM_FRAME_LEN + GSM_SAMPLES - 1)/GSM_SAMPLES,
238 static void parse_config(void)
240 struct ast_variable *var;
241 struct ast_config *cfg = ast_config_load("codecs.conf");
242 if (!cfg)
243 return;
244 for (var = ast_variable_browse(cfg, "plc"); var; var = var->next) {
245 if (!strcasecmp(var->name, "genericplc")) {
246 gsmtolin.useplc = ast_true(var->value) ? 1 : 0;
247 if (option_verbose > 2)
248 ast_verbose(VERBOSE_PREFIX_3 "codec_gsm: %susing generic PLC\n", gsmtolin.useplc ? "" : "not ");
251 ast_config_destroy(cfg);
254 /*! \brief standard module glue */
255 static int reload(void)
257 parse_config();
258 return 0;
261 static int unload_module(void)
263 int res;
265 res = ast_unregister_translator(&lintogsm);
266 if (!res)
267 res = ast_unregister_translator(&gsmtolin);
269 return res;
272 static int load_module(void)
274 int res;
276 parse_config();
277 res = ast_register_translator(&gsmtolin);
278 if (!res)
279 res=ast_register_translator(&lintogsm);
280 else
281 ast_unregister_translator(&gsmtolin);
283 return res;
286 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "GSM Coder/Decoder",
287 .load = load_module,
288 .unload = unload_module,
289 .reload = reload,