Merge another change from team/russell/events
[asterisk-bristuff.git] / codecs / codec_alaw.c
blobb2f4f94357ca5de7e52d416739a358916024c1cd
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 "asterisk/module.h"
31 #include "asterisk/config.h"
32 #include "asterisk/translate.h"
33 #include "asterisk/alaw.h"
34 #include "asterisk/utils.h"
36 #define BUFFER_SAMPLES 8096 /* size for the translation buffers */
38 /* Sample frame data (Mu data is okay) */
40 #include "slin_ulaw_ex.h"
41 #include "ulaw_slin_ex.h"
43 /*! \brief decode frame into lin and fill output buffer. */
44 static int alawtolin_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
46 int i = f->samples;
47 unsigned char *src = f->data.ptr;
48 int16_t *dst = (int16_t *)pvt->outbuf + pvt->samples;
50 pvt->samples += i;
51 pvt->datalen += i * 2; /* 2 bytes/sample */
53 while (i--)
54 *dst++ = AST_ALAW(*src++);
56 return 0;
59 /*! \brief convert and store input samples in output buffer */
60 static int lintoalaw_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
62 int i = f->samples;
63 char *dst = pvt->outbuf + pvt->samples;
64 int16_t *src = f->data.ptr;
66 pvt->samples += i;
67 pvt->datalen += i; /* 1 byte/sample */
69 while (i--)
70 *dst++ = AST_LIN2A(*src++);
72 return 0;
75 /*! \brief alawToLin_Sample */
76 static struct ast_frame *alawtolin_sample(void)
78 static struct ast_frame f;
79 f.frametype = AST_FRAME_VOICE;
80 f.subclass = AST_FORMAT_ALAW;
81 f.datalen = sizeof(ulaw_slin_ex);
82 f.samples = sizeof(ulaw_slin_ex);
83 f.mallocd = 0;
84 f.offset = 0;
85 f.src = __PRETTY_FUNCTION__;
86 f.data.ptr = ulaw_slin_ex;
87 return &f;
90 /*! \brief LinToalaw_Sample */
91 static struct ast_frame *lintoalaw_sample(void)
93 static struct ast_frame f;
94 f.frametype = AST_FRAME_VOICE;
95 f.subclass = AST_FORMAT_SLINEAR;
96 f.datalen = sizeof(slin_ulaw_ex);
97 f.samples = sizeof(slin_ulaw_ex) / 2;
98 f.mallocd = 0;
99 f.offset = 0;
100 f.src = __PRETTY_FUNCTION__;
101 f.data.ptr = slin_ulaw_ex;
102 return &f;
105 static struct ast_translator alawtolin = {
106 .name = "alawtolin",
107 .srcfmt = AST_FORMAT_ALAW,
108 .dstfmt = AST_FORMAT_SLINEAR,
109 .framein = alawtolin_framein,
110 .sample = alawtolin_sample,
111 .buffer_samples = BUFFER_SAMPLES,
112 .buf_size = BUFFER_SAMPLES * 2,
113 .plc_samples = 160,
116 static struct ast_translator lintoalaw = {
117 "lintoalaw",
118 .srcfmt = AST_FORMAT_SLINEAR,
119 .dstfmt = AST_FORMAT_ALAW,
120 .framein = lintoalaw_framein,
121 .sample = lintoalaw_sample,
122 .buffer_samples = BUFFER_SAMPLES,
123 .buf_size = BUFFER_SAMPLES,
126 static int parse_config(int reload)
128 struct ast_variable *var;
129 struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
130 struct ast_config *cfg = ast_config_load("codecs.conf", config_flags);
131 if (cfg == NULL)
132 return 0;
133 if (cfg == CONFIG_STATUS_FILEUNCHANGED)
134 return 0;
135 for (var = ast_variable_browse(cfg, "plc"); var; var = var->next) {
136 if (!strcasecmp(var->name, "genericplc")) {
137 alawtolin.useplc = ast_true(var->value) ? 1 : 0;
138 ast_verb(3, "codec_alaw: %susing generic PLC\n", alawtolin.useplc ? "" : "not ");
141 ast_config_destroy(cfg);
142 return 0;
145 /*! \brief standard module stuff */
147 static int reload(void)
149 if (parse_config(1))
150 return AST_MODULE_LOAD_DECLINE;
151 return AST_MODULE_LOAD_SUCCESS;
154 static int unload_module(void)
156 int res;
158 res = ast_unregister_translator(&lintoalaw);
159 res |= ast_unregister_translator(&alawtolin);
161 return res;
164 static int load_module(void)
166 int res;
168 if (parse_config(0))
169 return AST_MODULE_LOAD_DECLINE;
170 res = ast_register_translator(&alawtolin);
171 if (!res)
172 res = ast_register_translator(&lintoalaw);
173 else
174 ast_unregister_translator(&alawtolin);
175 if (res)
176 return AST_MODULE_LOAD_FAILURE;
177 return AST_MODULE_LOAD_SUCCESS;
180 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "A-law Coder/Decoder",
181 .load = load_module,
182 .unload = unload_module,
183 .reload = reload,