Minor formatting change to test a mantis change ...
[asterisk-bristuff.git] / codecs / codec_a_mu.c
blob23f04f0550ebc891fbfa1f23865ee0e2cb33fb2c
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_a_mu.c - translate between alaw and ulaw directly
23 * \ingroup codecs
26 #include "asterisk.h"
28 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
30 #include "asterisk/module.h"
31 #include "asterisk/translate.h"
32 #include "asterisk/alaw.h"
33 #include "asterisk/ulaw.h"
34 #include "asterisk/utils.h"
36 #define BUFFER_SAMPLES 8000 /* size for the translation buffers */
38 static unsigned char mu2a[256];
39 static unsigned char a2mu[256];
41 /* Sample frame data (Mu data is okay) */
43 #include "ulaw_slin_ex.h"
45 /*! \brief convert frame data and store into the buffer */
46 static int alawtoulaw_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
48 int x = f->samples;
49 unsigned char *src = f->data.ptr;
50 unsigned char *dst = (unsigned char *)pvt->outbuf + pvt->samples;
52 pvt->samples += x;
53 pvt->datalen += x;
55 while (x--)
56 *dst++ = a2mu[*src++];
58 return 0;
61 /*! \brief convert frame data and store into the buffer */
62 static int ulawtoalaw_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
64 int x = f->samples;
65 unsigned char *src = f->data.ptr;
66 unsigned char *dst = (unsigned char *)pvt->outbuf + pvt->samples;
68 pvt->samples += x;
69 pvt->datalen += x;
71 while (x--)
72 *dst++ = mu2a[*src++];
74 return 0;
78 * alawToLin_Sample. Just random data, somehow...
80 static struct ast_frame *alawtoulaw_sample(void)
82 static struct ast_frame f;
83 f.frametype = AST_FRAME_VOICE;
84 f.subclass = AST_FORMAT_ALAW;
85 f.datalen = sizeof(ulaw_slin_ex);
86 f.samples = sizeof(ulaw_slin_ex);
87 f.mallocd = 0;
88 f.offset = 0;
89 f.src = __PRETTY_FUNCTION__;
90 f.data.ptr = ulaw_slin_ex; /* XXX what ? */
91 return &f;
94 static struct ast_frame *ulawtoalaw_sample(void)
96 static struct ast_frame f;
97 f.frametype = AST_FRAME_VOICE;
98 f.subclass = AST_FORMAT_ULAW;
99 f.datalen = sizeof(ulaw_slin_ex);
100 f.samples = sizeof(ulaw_slin_ex);
101 f.mallocd = 0;
102 f.offset = 0;
103 f.src = __PRETTY_FUNCTION__;
104 f.data.ptr = ulaw_slin_ex;
105 return &f;
108 static struct ast_translator alawtoulaw = {
109 .name = "alawtoulaw",
110 .srcfmt = AST_FORMAT_ALAW,
111 .dstfmt = AST_FORMAT_ULAW,
112 .framein = alawtoulaw_framein,
113 .sample = alawtoulaw_sample,
114 .buffer_samples = BUFFER_SAMPLES,
115 .buf_size = BUFFER_SAMPLES,
118 static struct ast_translator ulawtoalaw = {
119 .name = "ulawtoalaw",
120 .srcfmt = AST_FORMAT_ULAW,
121 .dstfmt = AST_FORMAT_ALAW,
122 .framein = ulawtoalaw_framein,
123 .sample = ulawtoalaw_sample,
124 .buffer_samples = BUFFER_SAMPLES,
125 .buf_size = BUFFER_SAMPLES,
128 /*! \brief standard module glue */
130 static int unload_module(void)
132 int res;
134 res = ast_unregister_translator(&ulawtoalaw);
135 res |= ast_unregister_translator(&alawtoulaw);
137 return res;
140 static int load_module(void)
142 int res;
143 int x;
145 for (x=0;x<256;x++) {
146 mu2a[x] = AST_LIN2A(AST_MULAW(x));
147 a2mu[x] = AST_LIN2MU(AST_ALAW(x));
149 res = ast_register_translator(&alawtoulaw);
150 if (!res)
151 res = ast_register_translator(&ulawtoalaw);
152 else
153 ast_unregister_translator(&alawtoulaw);
154 if (res)
155 return AST_MODULE_LOAD_FAILURE;
156 return AST_MODULE_LOAD_SUCCESS;
159 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "A-law and Mulaw direct Coder/Decoder");