Let's also include aclocal.m4
[asterisk-bristuff.git] / codecs / codec_ulaw.c
blob334f8d9ad7638cfe7489003135cb5656466a7c93
1 /*
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.
19 /*! \file
21 * \brief codec_ulaw.c - translate between signed linear and ulaw
23 * \ingroup codecs
26 #include "asterisk.h"
28 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
30 #include <fcntl.h>
31 #include <netinet/in.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <unistd.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)
57 int i = f->samples;
58 unsigned char *src = f->data;
59 int16_t *dst = (int16_t *)pvt->outbuf + pvt->samples;
61 pvt->samples += i;
62 pvt->datalen += i * 2; /* 2 bytes/sample */
64 /* convert and copy in outbuf */
65 while (i--)
66 *dst++ = AST_MULAW(*src++);
68 return 0;
71 /*! \brief convert and store samples in outbuf */
72 static int lintoulaw_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
74 int i = f->samples;
75 char *dst = pvt->outbuf + pvt->samples;
76 int16_t *src = f->data;
78 pvt->samples += i;
79 pvt->datalen += i; /* 1 byte/sample */
81 while (i--)
82 *dst++ = AST_LIN2MU(*src++);
84 return 0;
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);
95 f.mallocd = 0;
96 f.offset = 0;
97 f.src = __PRETTY_FUNCTION__;
98 f.data = ulaw_slin_ex;
99 return &f;
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);
112 /* Assume 8000 Hz */
113 f.samples = sizeof(slin_ulaw_ex) / 2;
114 f.mallocd = 0;
115 f.offset = 0;
116 f.src = __PRETTY_FUNCTION__;
117 f.data = slin_ulaw_ex;
118 return &f;
122 * \brief The complete translator for ulawToLin.
125 static struct ast_translator ulawtolin = {
126 .name = "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,
133 .plc_samples = 160,
137 * \brief The complete translator for LinToulaw.
140 static struct ast_translator lintoulaw = {
141 .name = "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");
154 if (!cfg)
155 return;
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)
168 parse_config();
170 return 0;
173 static int unload_module(void)
175 int res;
177 res = ast_unregister_translator(&lintoulaw);
178 res |= ast_unregister_translator(&ulawtolin);
180 return res;
183 static int load_module(void)
185 int res;
187 parse_config();
188 res = ast_register_translator(&ulawtolin);
189 if (!res)
190 res = ast_register_translator(&lintoulaw);
191 else
192 ast_unregister_translator(&ulawtolin);
194 return res;
197 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "mu-Law Coder/Decoder",
198 .load = load_module,
199 .unload = unload_module,
200 .reload = reload,