Let's also include aclocal.m4
[asterisk-bristuff.git] / codecs / codec_alaw.c
blobde106168733e6fdd0aa39557c6d8e64dba02d9fb
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_alaw.c - translate between signed linear and alaw
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/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)
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 while (i--)
65 *dst++ = AST_ALAW(*src++);
67 return 0;
70 /*! \brief convert and store input samples in output buffer */
71 static int lintoalaw_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
73 int i = f->samples;
74 char *dst = pvt->outbuf + pvt->samples;
75 int16_t *src = f->data;
77 pvt->samples += i;
78 pvt->datalen += i; /* 1 byte/sample */
80 while (i--)
81 *dst++ = AST_LIN2A(*src++);
83 return 0;
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);
94 f.mallocd = 0;
95 f.offset = 0;
96 f.src = __PRETTY_FUNCTION__;
97 f.data = ulaw_slin_ex;
98 return &f;
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;
109 f.mallocd = 0;
110 f.offset = 0;
111 f.src = __PRETTY_FUNCTION__;
112 f.data = slin_ulaw_ex;
113 return &f;
116 static struct ast_translator alawtolin = {
117 .name = "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,
124 .plc_samples = 160,
127 static struct ast_translator lintoalaw = {
128 "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");
141 if (!cfg)
142 return;
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)
157 parse_config();
158 return 0;
161 static int unload_module(void)
163 int res;
165 res = ast_unregister_translator(&lintoalaw);
166 res |= ast_unregister_translator(&alawtolin);
168 return res;
171 static int load_module(void)
173 int res;
175 parse_config();
176 res = ast_register_translator(&alawtolin);
177 if (!res)
178 res = ast_register_translator(&lintoalaw);
179 else
180 ast_unregister_translator(&alawtolin);
182 return res;
185 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "A-law Coder/Decoder",
186 .load = load_module,
187 .unload = unload_module,
188 .reload = reload,