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$")
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/alaw.h"
45 #include "asterisk/utils.h"
47 #define BUFFER_SAMPLES 8096 /* size for the translation buffers */
49 /* Sample frame data (Mu data is okay) */
51 #include "slin_ulaw_ex.h"
52 #include "ulaw_slin_ex.h"
54 /*! \brief decode frame into lin and fill output buffer. */
55 static int alawtolin_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 */
65 *dst
++ = AST_ALAW(*src
++);
70 /*! \brief convert and store input samples in output buffer */
71 static int lintoalaw_framein(struct ast_trans_pvt
*pvt
, struct ast_frame
*f
)
74 char *dst
= pvt
->outbuf
+ pvt
->samples
;
75 int16_t *src
= f
->data
;
78 pvt
->datalen
+= i
; /* 1 byte/sample */
81 *dst
++ = AST_LIN2A(*src
++);
86 /*! \brief alawToLin_Sample */
87 static struct ast_frame
*alawtolin_sample(void)
89 static struct ast_frame f
;
90 f
.frametype
= AST_FRAME_VOICE
;
91 f
.subclass
= AST_FORMAT_ALAW
;
92 f
.datalen
= sizeof(ulaw_slin_ex
);
93 f
.samples
= sizeof(ulaw_slin_ex
);
96 f
.src
= __PRETTY_FUNCTION__
;
97 f
.data
= ulaw_slin_ex
;
101 /*! \brief LinToalaw_Sample */
102 static struct ast_frame
*lintoalaw_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
);
108 f
.samples
= sizeof(slin_ulaw_ex
) / 2;
111 f
.src
= __PRETTY_FUNCTION__
;
112 f
.data
= slin_ulaw_ex
;
116 static struct ast_translator alawtolin
= {
118 .srcfmt
= AST_FORMAT_ALAW
,
119 .dstfmt
= AST_FORMAT_SLINEAR
,
120 .framein
= alawtolin_framein
,
121 .sample
= alawtolin_sample
,
122 .buffer_samples
= BUFFER_SAMPLES
,
123 .buf_size
= BUFFER_SAMPLES
* 2,
127 static struct ast_translator lintoalaw
= {
129 .srcfmt
= AST_FORMAT_SLINEAR
,
130 .dstfmt
= AST_FORMAT_ALAW
,
131 .framein
= lintoalaw_framein
,
132 .sample
= lintoalaw_sample
,
133 .buffer_samples
= BUFFER_SAMPLES
,
134 .buf_size
= BUFFER_SAMPLES
,
137 static void parse_config(void)
139 struct ast_variable
*var
;
140 struct ast_config
*cfg
= ast_config_load("codecs.conf");
143 for (var
= ast_variable_browse(cfg
, "plc"); var
; var
= var
->next
) {
144 if (!strcasecmp(var
->name
, "genericplc")) {
145 alawtolin
.useplc
= ast_true(var
->value
) ? 1 : 0;
146 if (option_verbose
> 2)
147 ast_verbose(VERBOSE_PREFIX_3
"codec_alaw: %susing generic PLC\n", alawtolin
.useplc
? "" : "not ");
150 ast_config_destroy(cfg
);
153 /*! \brief standard module stuff */
155 static int reload(void)
161 static int unload_module(void)
165 res
= ast_unregister_translator(&lintoalaw
);
166 res
|= ast_unregister_translator(&alawtolin
);
171 static int load_module(void)
176 res
= ast_register_translator(&alawtolin
);
178 res
= ast_register_translator(&lintoalaw
);
180 ast_unregister_translator(&alawtolin
);
185 AST_MODULE_INFO(ASTERISK_GPL_KEY
, AST_MODFLAG_DEFAULT
, "A-law Coder/Decoder",
187 .unload
= unload_module
,