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
28 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
30 #include "asterisk/module.h"
31 #include "asterisk/config.h"
32 #include "asterisk/translate.h"
33 #include "asterisk/ulaw.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 convert and store samples in outbuf */
43 static int ulawtolin_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 */
52 /* convert and copy in outbuf */
54 *dst++ = AST_MULAW(*src++);
59 /*! \brief convert and store samples in outbuf */
60 static int lintoulaw_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
63 char *dst = pvt->outbuf.c + pvt->samples;
64 int16_t *src = f->data.ptr;
67 pvt->datalen += i; /* 1 byte/sample */
70 *dst++ = AST_LIN2MU(*src++);
76 * \brief The complete translator for ulawToLin.
79 static struct ast_translator ulawtolin = {
81 .srcfmt = AST_FORMAT_ULAW,
82 .dstfmt = AST_FORMAT_SLINEAR,
83 .framein = ulawtolin_framein,
84 .sample = ulaw_sample,
85 .buffer_samples = BUFFER_SAMPLES,
86 .buf_size = BUFFER_SAMPLES * 2,
89 static struct ast_translator testlawtolin = {
90 .name = "testlawtolin",
91 .srcfmt = AST_FORMAT_TESTLAW,
92 .dstfmt = AST_FORMAT_SLINEAR,
93 .framein = ulawtolin_framein,
94 .sample = ulaw_sample,
95 .buffer_samples = BUFFER_SAMPLES,
96 .buf_size = BUFFER_SAMPLES * 2,
100 * \brief The complete translator for LinToulaw.
103 static struct ast_translator lintoulaw = {
105 .srcfmt = AST_FORMAT_SLINEAR,
106 .dstfmt = AST_FORMAT_ULAW,
107 .framein = lintoulaw_framein,
108 .sample = slin8_sample,
109 .buf_size = BUFFER_SAMPLES,
110 .buffer_samples = BUFFER_SAMPLES,
113 static struct ast_translator lintotestlaw = {
114 .name = "lintotestlaw",
115 .srcfmt = AST_FORMAT_SLINEAR,
116 .dstfmt = AST_FORMAT_TESTLAW,
117 .framein = lintoulaw_framein,
118 .sample = slin8_sample,
119 .buf_size = BUFFER_SAMPLES,
120 .buffer_samples = BUFFER_SAMPLES,
123 static int reload(void)
125 return AST_MODULE_LOAD_SUCCESS;
128 static int unload_module(void)
132 res = ast_unregister_translator(&lintoulaw);
133 res |= ast_unregister_translator(&ulawtolin);
134 res |= ast_unregister_translator(&testlawtolin);
135 res |= ast_unregister_translator(&lintotestlaw);
140 static int load_module(void)
144 res = ast_register_translator(&ulawtolin);
146 res = ast_register_translator(&lintoulaw);
147 res |= ast_register_translator(&lintotestlaw);
148 res |= ast_register_translator(&testlawtolin);
150 ast_unregister_translator(&ulawtolin);
152 return AST_MODULE_LOAD_FAILURE;
153 return AST_MODULE_LOAD_SUCCESS;
156 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "mu-Law Coder/Decoder",
158 .unload = unload_module,