Convert casts to unions, to fix alignment issues on Solaris
[asterisk/asterisk.git] / codecs / codec_resample.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2007, Digium, Inc.
5  *
6  * Russell Bryant <russell@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 /*! 
20  * \file
21  *
22  * \brief Resample slinear audio
23  *
24  * \ingroup codecs
25  */
26
27 #include "asterisk.h"
28
29 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
30
31 /* These are for SHRT_MAX and FLT_MAX -- { */
32 #if defined(__Darwin__) || defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__CYGWIN__)
33 #include <float.h>
34 #else
35 #include <values.h>
36 #endif
37 #include <limits.h>
38 /* } */
39
40 #include "asterisk/module.h"
41 #include "asterisk/translate.h"
42 #include "asterisk/libresample.h"
43
44 #include "slin_resample_ex.h"
45
46 #define RESAMPLER_QUALITY 0
47
48 #define OUTBUF_SIZE   8096
49
50 struct slin16_to_slin8_pvt {
51         void *resampler;
52         float resample_factor;
53 };
54
55 struct slin8_to_slin16_pvt {
56         void *resampler;
57         float resample_factor;
58 };
59
60 static int slin16_to_slin8_new(struct ast_trans_pvt *pvt)
61 {
62         struct slin16_to_slin8_pvt *resamp_pvt = pvt->pvt;
63
64         resamp_pvt->resample_factor = 0.5;
65
66         if (!(resamp_pvt->resampler = resample_open(RESAMPLER_QUALITY, 0.5, 0.5)))
67                 return -1;
68
69         return 0;
70 }
71
72 static int slin8_to_slin16_new(struct ast_trans_pvt *pvt)
73 {
74         struct slin8_to_slin16_pvt *resamp_pvt = pvt->pvt;
75
76         resamp_pvt->resample_factor = 2.0;
77
78         if (!(resamp_pvt->resampler = resample_open(RESAMPLER_QUALITY, 2.0, 2.0)))
79                 return -1;
80
81         return 0;
82 }
83
84 static void slin16_to_slin8_destroy(struct ast_trans_pvt *pvt)
85 {
86         struct slin16_to_slin8_pvt *resamp_pvt = pvt->pvt;
87
88         if (resamp_pvt->resampler)
89                 resample_close(resamp_pvt->resampler);
90 }
91
92 static void slin8_to_slin16_destroy(struct ast_trans_pvt *pvt)
93 {
94         struct slin8_to_slin16_pvt *resamp_pvt = pvt->pvt;
95
96         if (resamp_pvt->resampler)
97                 resample_close(resamp_pvt->resampler);
98 }
99
100 static int resample_frame(struct ast_trans_pvt *pvt,
101         void *resampler, float resample_factor, struct ast_frame *f)
102 {
103         int total_in_buf_used = 0;
104         int total_out_buf_used = 0;
105         int16_t *in_buf = (int16_t *) f->data.ptr;
106         int16_t *out_buf = pvt->outbuf.i16 + pvt->samples;
107         float in_buf_f[f->samples];
108         float out_buf_f[2048];
109         int res = 0;
110         int i;
111
112         for (i = 0; i < f->samples; i++)
113                 in_buf_f[i] = in_buf[i] * (FLT_MAX / SHRT_MAX);
114
115         while (total_in_buf_used < f->samples) {
116                 int in_buf_used, out_buf_used;
117
118                 out_buf_used = resample_process(resampler, resample_factor,
119                         &in_buf_f[total_in_buf_used], f->samples - total_in_buf_used,
120                         0, &in_buf_used,
121                         &out_buf_f[total_out_buf_used], ARRAY_LEN(out_buf_f) - total_out_buf_used);
122
123                 if (out_buf_used < 0)
124                         break;
125
126                 total_out_buf_used += out_buf_used;
127                 total_in_buf_used += in_buf_used;
128
129                 if (total_out_buf_used == ARRAY_LEN(out_buf_f)) {
130                         ast_log(LOG_ERROR, "Output buffer filled ... need to increase its size\n");
131                         res = -1;
132                         break;
133                 }
134         }
135
136         for (i = 0; i < total_out_buf_used; i++)
137                 out_buf[i] = out_buf_f[i] * (SHRT_MAX / FLT_MAX);       
138
139         pvt->samples += total_out_buf_used;
140         pvt->datalen += (total_out_buf_used * sizeof(int16_t));
141
142         return res;
143 }
144
145 static int slin16_to_slin8_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
146 {
147         struct slin16_to_slin8_pvt *resamp_pvt = pvt->pvt;
148         void *resampler = resamp_pvt->resampler;
149         float resample_factor = resamp_pvt->resample_factor;
150
151         return resample_frame(pvt, resampler, resample_factor, f);
152 }
153
154 static int slin8_to_slin16_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
155 {
156         struct slin8_to_slin16_pvt *resamp_pvt = pvt->pvt;
157         void *resampler = resamp_pvt->resampler;
158         float resample_factor = resamp_pvt->resample_factor;
159
160         return resample_frame(pvt, resampler, resample_factor, f);
161 }
162
163 static struct ast_frame *slin16_to_slin8_sample(void)
164 {
165         static struct ast_frame f = {
166                 .frametype = AST_FRAME_VOICE,
167                 .subclass = AST_FORMAT_SLINEAR16,
168                 .datalen = sizeof(slin16_slin8_ex),
169                 .samples = sizeof(slin16_slin8_ex) / sizeof(slin16_slin8_ex[0]),
170                 .src = __PRETTY_FUNCTION__,
171                 .data.ptr = slin16_slin8_ex,
172         };
173
174         return &f;
175 }
176
177 static struct ast_frame *slin8_to_slin16_sample(void)
178 {
179         static struct ast_frame f = {
180                 .frametype = AST_FRAME_VOICE,
181                 .subclass = AST_FORMAT_SLINEAR,
182                 .datalen = sizeof(slin8_slin16_ex),
183                 .samples = sizeof(slin8_slin16_ex) / sizeof(slin8_slin16_ex[0]),
184                 .src = __PRETTY_FUNCTION__,
185                 .data.ptr = slin8_slin16_ex,
186         };
187
188         return &f;
189 }
190
191 static struct ast_translator slin16_to_slin8 = {
192         .name = "slin16_to_slin8",
193         .srcfmt = AST_FORMAT_SLINEAR16,
194         .dstfmt = AST_FORMAT_SLINEAR,
195         .newpvt = slin16_to_slin8_new,
196         .destroy = slin16_to_slin8_destroy,
197         .framein = slin16_to_slin8_framein,
198         .sample = slin16_to_slin8_sample,
199         .desc_size = sizeof(struct slin16_to_slin8_pvt),
200         .buffer_samples = (OUTBUF_SIZE / sizeof(int16_t)),
201         .buf_size = OUTBUF_SIZE,
202 };
203
204 static struct ast_translator slin8_to_slin16 = {
205         .name = "slin8_to_slin16",
206         .srcfmt = AST_FORMAT_SLINEAR,
207         .dstfmt = AST_FORMAT_SLINEAR16,
208         .newpvt = slin8_to_slin16_new,
209         .destroy = slin8_to_slin16_destroy,
210         .framein = slin8_to_slin16_framein,
211         .sample = slin8_to_slin16_sample,
212         .desc_size = sizeof(struct slin8_to_slin16_pvt),
213         .buffer_samples = (OUTBUF_SIZE / sizeof(int16_t)),
214         .buf_size = OUTBUF_SIZE,
215 };
216
217 static int unload_module(void)
218 {
219         int res = 0;
220
221         res |= ast_unregister_translator(&slin16_to_slin8);
222         res |= ast_unregister_translator(&slin8_to_slin16);
223
224         return res;
225 }
226
227 static int load_module(void)
228 {
229         int res = 0;
230
231         res |= ast_register_translator(&slin16_to_slin8);
232         res |= ast_register_translator(&slin8_to_slin16);
233
234         return AST_MODULE_LOAD_SUCCESS;
235 }
236
237 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "SLIN Resampling Codec");