2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2005, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
21 * \brief codec_alaw.c - translate between signed linear and alaw
28 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
30 #include "asterisk/module.h"
31 #include "asterisk/config.h"
32 #include "asterisk/translate.h"
33 #include "asterisk/alaw.h"
34 #include "asterisk/utils.h"
36 #define BUFFER_SAMPLES 8096 /* size for the translation buffers */
38 /* Sample frame data (Mu data is okay) */
40 #include "slin_ulaw_ex.h"
41 #include "ulaw_slin_ex.h"
43 /*! \brief decode frame into lin and fill output buffer. */
44 static int alawtolin_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
47 unsigned char *src = f->data.ptr;
48 int16_t *dst = (int16_t *)pvt->outbuf + pvt->samples;
51 pvt->datalen += i * 2; /* 2 bytes/sample */
54 *dst++ = AST_ALAW(*src++);
59 /*! \brief convert and store input samples in output buffer */
60 static int lintoalaw_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
63 char *dst = pvt->outbuf + pvt->samples;
64 int16_t *src = f->data.ptr;
67 pvt->datalen += i; /* 1 byte/sample */
70 *dst++ = AST_LIN2A(*src++);
75 /*! \brief alawToLin_Sample */
76 static struct ast_frame *alawtolin_sample(void)
78 static struct ast_frame f;
79 f.frametype = AST_FRAME_VOICE;
80 f.subclass = AST_FORMAT_ALAW;
81 f.datalen = sizeof(ulaw_slin_ex);
82 f.samples = sizeof(ulaw_slin_ex);
85 f.src = __PRETTY_FUNCTION__;
86 f.data.ptr = ulaw_slin_ex;
90 /*! \brief LinToalaw_Sample */
91 static struct ast_frame *lintoalaw_sample(void)
93 static struct ast_frame f;
94 f.frametype = AST_FRAME_VOICE;
95 f.subclass = AST_FORMAT_SLINEAR;
96 f.datalen = sizeof(slin_ulaw_ex);
97 f.samples = sizeof(slin_ulaw_ex) / 2;
100 f.src = __PRETTY_FUNCTION__;
101 f.data.ptr = slin_ulaw_ex;
105 static struct ast_translator alawtolin = {
107 .srcfmt = AST_FORMAT_ALAW,
108 .dstfmt = AST_FORMAT_SLINEAR,
109 .framein = alawtolin_framein,
110 .sample = alawtolin_sample,
111 .buffer_samples = BUFFER_SAMPLES,
112 .buf_size = BUFFER_SAMPLES * 2,
116 static struct ast_translator lintoalaw = {
118 .srcfmt = AST_FORMAT_SLINEAR,
119 .dstfmt = AST_FORMAT_ALAW,
120 .framein = lintoalaw_framein,
121 .sample = lintoalaw_sample,
122 .buffer_samples = BUFFER_SAMPLES,
123 .buf_size = BUFFER_SAMPLES,
126 static int parse_config(int reload)
128 struct ast_variable *var;
129 struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
130 struct ast_config *cfg = ast_config_load("codecs.conf", config_flags);
133 if (cfg == CONFIG_STATUS_FILEUNCHANGED)
135 for (var = ast_variable_browse(cfg, "plc"); var; var = var->next) {
136 if (!strcasecmp(var->name, "genericplc")) {
137 alawtolin.useplc = ast_true(var->value) ? 1 : 0;
138 ast_verb(3, "codec_alaw: %susing generic PLC\n", alawtolin.useplc ? "" : "not ");
141 ast_config_destroy(cfg);
145 /*! \brief standard module stuff */
147 static int reload(void)
150 return AST_MODULE_LOAD_DECLINE;
151 return AST_MODULE_LOAD_SUCCESS;
154 static int unload_module(void)
158 res = ast_unregister_translator(&lintoalaw);
159 res |= ast_unregister_translator(&alawtolin);
164 static int load_module(void)
169 return AST_MODULE_LOAD_DECLINE;
170 res = ast_register_translator(&alawtolin);
172 res = ast_register_translator(&lintoalaw);
174 ast_unregister_translator(&alawtolin);
176 return AST_MODULE_LOAD_FAILURE;
177 return AST_MODULE_LOAD_SUCCESS;
180 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "A-law Coder/Decoder",
182 .unload = unload_module,