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$")
31 #include <netinet/in.h>
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
+ pvt
->samples
;
62 pvt
->datalen
+= i
* 2; /* 2 bytes/sample */
64 /* convert and copy in outbuf */
66 *dst
++ = AST_MULAW(*src
++);
71 /*! \brief convert and store samples in outbuf */
72 static int lintoulaw_framein(struct ast_trans_pvt
*pvt
, struct ast_frame
*f
)
75 char *dst
= pvt
->outbuf
+ pvt
->samples
;
76 int16_t *src
= f
->data
;
79 pvt
->datalen
+= i
; /* 1 byte/sample */
82 *dst
++ = AST_LIN2MU(*src
++);
87 /*! * \brief ulawToLin_Sample */
88 static struct ast_frame
*ulawtolin_sample(void)
90 static struct ast_frame f
;
91 f
.frametype
= AST_FRAME_VOICE
;
92 f
.subclass
= AST_FORMAT_ULAW
;
93 f
.datalen
= sizeof(ulaw_slin_ex
);
94 f
.samples
= sizeof(ulaw_slin_ex
);
97 f
.src
= __PRETTY_FUNCTION__
;
98 f
.data
= ulaw_slin_ex
;
103 * \brief LinToulaw_Sample
106 static struct ast_frame
*lintoulaw_sample(void)
108 static struct ast_frame f
;
109 f
.frametype
= AST_FRAME_VOICE
;
110 f
.subclass
= AST_FORMAT_SLINEAR
;
111 f
.datalen
= sizeof(slin_ulaw_ex
);
113 f
.samples
= sizeof(slin_ulaw_ex
) / 2;
116 f
.src
= __PRETTY_FUNCTION__
;
117 f
.data
= slin_ulaw_ex
;
122 * \brief The complete translator for ulawToLin.
125 static struct ast_translator ulawtolin
= {
127 .srcfmt
= AST_FORMAT_ULAW
,
128 .dstfmt
= AST_FORMAT_SLINEAR
,
129 .framein
= ulawtolin_framein
,
130 .sample
= ulawtolin_sample
,
131 .buffer_samples
= BUFFER_SAMPLES
,
132 .buf_size
= BUFFER_SAMPLES
* 2,
137 * \brief The complete translator for LinToulaw.
140 static struct ast_translator lintoulaw
= {
142 .srcfmt
= AST_FORMAT_SLINEAR
,
143 .dstfmt
= AST_FORMAT_ULAW
,
144 .framein
= lintoulaw_framein
,
145 .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
);
166 static int reload(void)
173 static int unload_module(void)
177 res
= ast_unregister_translator(&lintoulaw
);
178 res
|= ast_unregister_translator(&ulawtolin
);
183 static int load_module(void)
188 res
= ast_register_translator(&ulawtolin
);
190 res
= ast_register_translator(&lintoulaw
);
192 ast_unregister_translator(&ulawtolin
);
197 AST_MODULE_INFO(ASTERISK_GPL_KEY
, AST_MODFLAG_DEFAULT
, "mu-Law Coder/Decoder",
199 .unload
= unload_module
,