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_ulaw.c - translate between signed linear and ulaw
27 #include <netinet/in.h>
35 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
37 #include "asterisk/lock.h"
38 #include "asterisk/logger.h"
39 #include "asterisk/module.h"
40 #include "asterisk/config.h"
41 #include "asterisk/options.h"
42 #include "asterisk/translate.h"
43 #include "asterisk/channel.h"
44 #include "asterisk/ulaw.h"
45 #include "asterisk/utils.h"
47 #define BUFFER_SAMPLES 8096 /* size for the translation buffers */
49 /* Sample frame data */
51 #include "slin_ulaw_ex.h"
52 #include "ulaw_slin_ex.h"
54 /*! \brief convert and store samples in outbuf */
55 static int ulawtolin_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
58 unsigned char *src = f->data;
59 int16_t *dst = (int16_t *)pvt->outbuf;
61 /* convert and copy in outbuf */
62 for (i=0; i<f->samples; i++)
63 dst[pvt->samples + i] = AST_MULAW(src[i]);
65 pvt->samples += f->samples;
66 pvt->datalen += 2 * f->samples;
70 /*! \brief convert and store samples in outbuf */
71 static int lintoulaw_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
74 int16_t *src = f->data;
76 for (i=0 ; i < f->samples; i++)
77 pvt->outbuf[pvt->samples + i] = AST_LIN2MU(src[i]);
78 pvt->samples += f->samples;
79 pvt->datalen += f->samples; /* 1 byte/sample */
83 /*! * \brief ulawToLin_Sample */
84 static struct ast_frame *ulawtolin_sample(void)
86 static struct ast_frame f;
87 f.frametype = AST_FRAME_VOICE;
88 f.subclass = AST_FORMAT_ULAW;
89 f.datalen = sizeof(ulaw_slin_ex);
90 f.samples = sizeof(ulaw_slin_ex);
93 f.src = __PRETTY_FUNCTION__;
94 f.data = ulaw_slin_ex;
99 * \brief LinToulaw_Sample
102 static struct ast_frame *lintoulaw_sample(void)
104 static struct ast_frame f;
105 f.frametype = AST_FRAME_VOICE;
106 f.subclass = AST_FORMAT_SLINEAR;
107 f.datalen = sizeof(slin_ulaw_ex);
109 f.samples = sizeof(slin_ulaw_ex) / 2;
112 f.src = __PRETTY_FUNCTION__;
113 f.data = slin_ulaw_ex;
118 * \brief The complete translator for ulawToLin.
121 static struct ast_module_lock me = { .usecnt = -1 };
123 static struct ast_translator ulawtolin = {
125 .srcfmt = AST_FORMAT_ULAW,
126 .dstfmt = AST_FORMAT_SLINEAR,
127 .framein = ulawtolin_framein,
128 .sample = ulawtolin_sample,
130 .buffer_samples = BUFFER_SAMPLES,
131 .buf_size = BUFFER_SAMPLES * 2,
136 * \brief The complete translator for LinToulaw.
139 static struct ast_translator lintoulaw = {
141 .srcfmt = AST_FORMAT_SLINEAR,
142 .dstfmt = AST_FORMAT_ULAW,
143 .framein = lintoulaw_framein,
144 .sample = lintoulaw_sample,
146 .buf_size = BUFFER_SAMPLES,
147 .buffer_samples = BUFFER_SAMPLES,
150 static void parse_config(void)
152 struct ast_variable *var;
153 struct ast_config *cfg = ast_config_load("codecs.conf");
156 for (var = ast_variable_browse(cfg, "plc"); var; var = var->next) {
157 if (!strcasecmp(var->name, "genericplc")) {
158 ulawtolin.useplc = ast_true(var->value) ? 1 : 0;
159 if (option_verbose > 2)
160 ast_verbose(VERBOSE_PREFIX_3 "codec_ulaw: %susing generic PLC\n", ulawtolin.useplc ? "" : "not ");
163 ast_config_destroy(cfg);
172 int unload_module(void)
175 ast_mutex_lock(&me.lock);
176 res = ast_unregister_translator(&lintoulaw);
178 res = ast_unregister_translator(&ulawtolin);
181 ast_mutex_unlock(&me.lock);
185 int load_module(void)
189 res = ast_register_translator(&ulawtolin);
191 res = ast_register_translator(&lintoulaw);
193 ast_unregister_translator(&ulawtolin);
198 * Return a description of this module.
201 char *description(void)
203 return "Mu-law Coder/Decoder";
213 return ASTERISK_GPL_KEY;