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