AST_MODULE_INFO: Format corrections to the usages of AST_MODULE_INFO macro.
[asterisk/asterisk.git] / codecs / codec_ulaw.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2005, Digium, Inc.
5  *
6  * Mark Spencer <markster@digium.com>
7  *
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.
13  *
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.
17  */
18
19 /*! \file
20  *
21  * \brief codec_ulaw.c - translate between signed linear and ulaw
22  * 
23  * \ingroup codecs
24  */
25
26 /*** MODULEINFO
27         <support_level>core</support_level>
28  ***/
29
30 #include "asterisk.h"
31
32 ASTERISK_REGISTER_FILE()
33
34 #include "asterisk/module.h"
35 #include "asterisk/config.h"
36 #include "asterisk/translate.h"
37 #include "asterisk/ulaw.h"
38 #include "asterisk/utils.h"
39
40 #define BUFFER_SAMPLES   8096   /* size for the translation buffers */
41
42 /* Sample frame data */
43 #include "asterisk/slin.h"
44 #include "ex_ulaw.h"
45
46 /*! \brief convert and store samples in outbuf */
47 static int ulawtolin_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
48 {
49         int i = f->samples;
50         unsigned char *src = f->data.ptr;
51         int16_t *dst = pvt->outbuf.i16 + pvt->samples;
52
53         pvt->samples += i;
54         pvt->datalen += i * 2;  /* 2 bytes/sample */
55
56         /* convert and copy in outbuf */
57         while (i--)
58                 *dst++ = AST_MULAW(*src++);
59
60         return 0;
61 }
62
63 /*! \brief convert and store samples in outbuf */
64 static int lintoulaw_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
65 {
66         int i = f->samples;
67         char *dst = pvt->outbuf.c + pvt->samples;
68         int16_t *src = f->data.ptr;
69
70         pvt->samples += i;
71         pvt->datalen += i;      /* 1 byte/sample */
72
73         while (i--)
74                 *dst++ = AST_LIN2MU(*src++);
75
76         return 0;
77 }
78
79 /*!
80  * \brief The complete translator for ulawToLin.
81  */
82
83 static struct ast_translator ulawtolin = {
84         .name = "ulawtolin",
85         .src_codec = {
86                 .name = "ulaw",
87                 .type = AST_MEDIA_TYPE_AUDIO,
88                 .sample_rate = 8000,
89         },
90         .dst_codec = {
91                 .name = "slin",
92                 .type = AST_MEDIA_TYPE_AUDIO,
93                 .sample_rate = 8000,
94         },
95         .format = "slin",
96         .framein = ulawtolin_framein,
97         .sample = ulaw_sample,
98         .buffer_samples = BUFFER_SAMPLES,
99         .buf_size = BUFFER_SAMPLES * 2,
100 };
101
102 static struct ast_translator testlawtolin = {
103         .name = "testlawtolin",
104         .src_codec = {
105                 .name = "testlaw",
106                 .type = AST_MEDIA_TYPE_AUDIO,
107                 .sample_rate = 8000,
108         },
109         .dst_codec = {
110                 .name = "slin",
111                 .type = AST_MEDIA_TYPE_AUDIO,
112                 .sample_rate = 8000,
113         },
114         .format = "slin",
115         .framein = ulawtolin_framein,
116         .sample = ulaw_sample,
117         .buffer_samples = BUFFER_SAMPLES,
118         .buf_size = BUFFER_SAMPLES * 2,
119 };
120
121 /*!
122  * \brief The complete translator for LinToulaw.
123  */
124
125 static struct ast_translator lintoulaw = {
126         .name = "lintoulaw",
127         .src_codec = {
128                 .name = "slin",
129                 .type = AST_MEDIA_TYPE_AUDIO,
130                 .sample_rate = 8000,
131         },
132         .dst_codec = {
133                 .name = "ulaw",
134                 .type = AST_MEDIA_TYPE_AUDIO,
135                 .sample_rate = 8000,
136         },
137         .format = "ulaw",
138         .framein = lintoulaw_framein,
139         .sample = slin8_sample,
140         .buf_size = BUFFER_SAMPLES,
141         .buffer_samples = BUFFER_SAMPLES,
142 };
143
144 static struct ast_translator lintotestlaw = {
145         .name = "lintotestlaw",
146         .src_codec = {
147                 .name = "slin",
148                 .type = AST_MEDIA_TYPE_AUDIO,
149                 .sample_rate = 8000,
150         },
151         .dst_codec = {
152                 .name = "testlaw",
153                 .type = AST_MEDIA_TYPE_AUDIO,
154                 .sample_rate = 8000,
155         },
156         .format = "testlaw",
157         .framein = lintoulaw_framein,
158         .sample = slin8_sample,
159         .buf_size = BUFFER_SAMPLES,
160         .buffer_samples = BUFFER_SAMPLES,
161 };
162
163 static int unload_module(void)
164 {
165         int res;
166
167         res = ast_unregister_translator(&lintoulaw);
168         res |= ast_unregister_translator(&ulawtolin);
169         res |= ast_unregister_translator(&testlawtolin);
170         res |= ast_unregister_translator(&lintotestlaw);
171
172         return res;
173 }
174
175 static int load_module(void)
176 {
177         int res;
178
179         res = ast_register_translator(&ulawtolin);
180         res |= ast_register_translator(&lintoulaw);
181         res |= ast_register_translator(&lintotestlaw);
182         res |= ast_register_translator(&testlawtolin);
183
184         if (res) {
185                 unload_module();
186                 return AST_MODULE_LOAD_FAILURE;
187         }
188
189         return AST_MODULE_LOAD_SUCCESS;
190 }
191
192 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "mu-Law Coder/Decoder",
193         .support_level = AST_MODULE_SUPPORT_CORE,
194         .load = load_module,
195         .unload = unload_module,
196 );