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 */
39 #include "asterisk/slin.h"
42 /*! \brief decode frame into lin and fill output buffer. */
43 static int alawtolin_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
46 unsigned char *src = f->data.ptr;
47 int16_t *dst = pvt->outbuf.i16 + pvt->samples;
50 pvt->datalen += i * 2; /* 2 bytes/sample */
53 *dst++ = AST_ALAW(*src++);
58 /*! \brief convert and store input samples in output buffer */
59 static int lintoalaw_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
62 char *dst = pvt->outbuf.c + pvt->samples;
63 int16_t *src = f->data.ptr;
66 pvt->datalen += i; /* 1 byte/sample */
69 *dst++ = AST_LIN2A(*src++);
74 static struct ast_translator alawtolin = {
76 .srcfmt = AST_FORMAT_ALAW,
77 .dstfmt = AST_FORMAT_SLINEAR,
78 .framein = alawtolin_framein,
79 .sample = alaw_sample,
80 .buffer_samples = BUFFER_SAMPLES,
81 .buf_size = BUFFER_SAMPLES * 2,
85 static struct ast_translator lintoalaw = {
87 .srcfmt = AST_FORMAT_SLINEAR,
88 .dstfmt = AST_FORMAT_ALAW,
89 .framein = lintoalaw_framein,
90 .sample = slin8_sample,
91 .buffer_samples = BUFFER_SAMPLES,
92 .buf_size = BUFFER_SAMPLES,
95 static int parse_config(int reload)
97 struct ast_variable *var;
98 struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
99 struct ast_config *cfg = ast_config_load("codecs.conf", config_flags);
100 if (cfg == CONFIG_STATUS_FILEMISSING || cfg == CONFIG_STATUS_FILEUNCHANGED || cfg == CONFIG_STATUS_FILEINVALID)
102 for (var = ast_variable_browse(cfg, "plc"); var; var = var->next) {
103 if (!strcasecmp(var->name, "genericplc")) {
104 alawtolin.useplc = ast_true(var->value) ? 1 : 0;
105 ast_verb(3, "codec_alaw: %susing generic PLC\n", alawtolin.useplc ? "" : "not ");
108 ast_config_destroy(cfg);
112 /*! \brief standard module stuff */
114 static int reload(void)
117 return AST_MODULE_LOAD_DECLINE;
118 return AST_MODULE_LOAD_SUCCESS;
121 static int unload_module(void)
125 res = ast_unregister_translator(&lintoalaw);
126 res |= ast_unregister_translator(&alawtolin);
131 static int load_module(void)
136 return AST_MODULE_LOAD_DECLINE;
137 res = ast_register_translator(&alawtolin);
139 res = ast_register_translator(&lintoalaw);
141 ast_unregister_translator(&alawtolin);
143 return AST_MODULE_LOAD_FAILURE;
144 return AST_MODULE_LOAD_SUCCESS;
147 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "A-law Coder/Decoder",
149 .unload = unload_module,