Let's also include aclocal.m4
[asterisk-bristuff.git] / codecs / codec_a_mu.c
blobb9c1c4938f35531033881ad6b75fa247124c3157
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 <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/translate.h"
41 #include "asterisk/channel.h"
42 #include "asterisk/alaw.h"
43 #include "asterisk/ulaw.h"
44 #include "asterisk/utils.h"
46 #define BUFFER_SAMPLES 8000 /* size for the translation buffers */
48 static unsigned char mu2a[256];
49 static unsigned char a2mu[256];
51 /* Sample frame data (Mu data is okay) */
53 #include "ulaw_slin_ex.h"
55 /*! \brief convert frame data and store into the buffer */
56 static int alawtoulaw_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
58 int x = f->samples;
59 unsigned char *src = f->data;
60 unsigned char *dst = (unsigned char *)pvt->outbuf + pvt->samples;
62 pvt->samples += x;
63 pvt->datalen += x;
65 while (x--)
66 *dst++ = a2mu[*src++];
68 return 0;
71 /*! \brief convert frame data and store into the buffer */
72 static int ulawtoalaw_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
74 int x = f->samples;
75 unsigned char *src = f->data;
76 unsigned char *dst = (unsigned char *)pvt->outbuf + pvt->samples;
78 pvt->samples += x;
79 pvt->datalen += x;
81 while (x--)
82 *dst++ = mu2a[*src++];
84 return 0;
88 * alawToLin_Sample. Just random data, somehow...
90 static struct ast_frame *alawtoulaw_sample(void)
92 static struct ast_frame f;
93 f.frametype = AST_FRAME_VOICE;
94 f.subclass = AST_FORMAT_ALAW;
95 f.datalen = sizeof(ulaw_slin_ex);
96 f.samples = sizeof(ulaw_slin_ex);
97 f.mallocd = 0;
98 f.offset = 0;
99 f.src = __PRETTY_FUNCTION__;
100 f.data = ulaw_slin_ex; /* XXX what ? */
101 return &f;
104 static struct ast_frame *ulawtoalaw_sample(void)
106 static struct ast_frame f;
107 f.frametype = AST_FRAME_VOICE;
108 f.subclass = AST_FORMAT_ULAW;
109 f.datalen = sizeof(ulaw_slin_ex);
110 f.samples = sizeof(ulaw_slin_ex);
111 f.mallocd = 0;
112 f.offset = 0;
113 f.src = __PRETTY_FUNCTION__;
114 f.data = ulaw_slin_ex;
115 return &f;
118 static struct ast_translator alawtoulaw = {
119 .name = "alawtoulaw",
120 .srcfmt = AST_FORMAT_ALAW,
121 .dstfmt = AST_FORMAT_ULAW,
122 .framein = alawtoulaw_framein,
123 .sample = alawtoulaw_sample,
124 .buffer_samples = BUFFER_SAMPLES,
125 .buf_size = BUFFER_SAMPLES,
128 static struct ast_translator ulawtoalaw = {
129 .name = "ulawtoalaw",
130 .srcfmt = AST_FORMAT_ULAW,
131 .dstfmt = AST_FORMAT_ALAW,
132 .framein = ulawtoalaw_framein,
133 .sample = ulawtoalaw_sample,
134 .buffer_samples = BUFFER_SAMPLES,
135 .buf_size = BUFFER_SAMPLES,
138 /*! \brief standard module glue */
140 static int unload_module(void)
142 int res;
144 res = ast_unregister_translator(&ulawtoalaw);
145 res |= ast_unregister_translator(&alawtoulaw);
147 return res;
150 static int load_module(void)
152 int res;
153 int x;
155 for (x=0;x<256;x++) {
156 mu2a[x] = AST_LIN2A(AST_MULAW(x));
157 a2mu[x] = AST_LIN2MU(AST_ALAW(x));
159 res = ast_register_translator(&alawtoulaw);
160 if (!res)
161 res = ast_register_translator(&ulawtoalaw);
162 else
163 ast_unregister_translator(&alawtoulaw);
165 return res;
168 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "A-law and Mulaw direct Coder/Decoder");