Merge changes from team/russell/codec_resample
[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 #ifdef __Darwin__
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
43 #include "libresample.h"
44
45 #include "slin_resample_ex.h"
46
47 #define RESAMPLER_QUALITY 0
48
49 #define OUTBUF_SIZE   8096
50
51 struct slin16_to_slin8_pvt {
52         void *resampler;
53         double resample_factor;
54 };
55
56 struct slin8_to_slin16_pvt {
57         void *resampler;
58         double resample_factor;
59 };
60
61 static int slin16_to_slin8_new(struct ast_trans_pvt *pvt)
62 {
63         struct slin16_to_slin8_pvt *resamp_pvt = pvt->pvt;
64
65         resamp_pvt->resample_factor = 0.5;
66
67         if (!(resamp_pvt->resampler = resample_open(RESAMPLER_QUALITY, 0.5, 0.5)))
68                 return -1;
69
70         return 0;
71 }
72
73 static int slin8_to_slin16_new(struct ast_trans_pvt *pvt)
74 {
75         struct slin8_to_slin16_pvt *resamp_pvt = pvt->pvt;
76
77         resamp_pvt->resample_factor = 2.0;
78
79         if (!(resamp_pvt->resampler = resample_open(RESAMPLER_QUALITY, 2.0, 2.0)))
80                 return -1;
81
82         return 0;
83 }
84
85 static void slin16_to_slin8_destroy(struct ast_trans_pvt *pvt)
86 {
87         struct slin16_to_slin8_pvt *resamp_pvt = pvt->pvt;
88
89         if (resamp_pvt->resampler)
90                 resample_close(resamp_pvt->resampler);
91 }
92
93 static void slin8_to_slin16_destroy(struct ast_trans_pvt *pvt)
94 {
95         struct slin8_to_slin16_pvt *resamp_pvt = pvt->pvt;
96
97         if (resamp_pvt->resampler)
98                 resample_close(resamp_pvt->resampler);
99 }
100
101 static int resample_frame(struct ast_trans_pvt *pvt,
102         void *resampler, double resample_factor, struct ast_frame *f)
103 {
104         int total_in_buf_used = 0;
105         int total_out_buf_used = 0;
106         int16_t *in_buf = (int16_t *) f->data;
107         int16_t *out_buf = (int16_t *) pvt->outbuf + pvt->samples;
108         float in_buf_f[f->samples];
109         float out_buf_f[2048];
110         int res = 0;
111         int i;
112
113         for (i = 0; i < f->samples; i++)
114                 in_buf_f[i] = in_buf[i] * (FLT_MAX / SHRT_MAX);
115
116         while (total_in_buf_used < f->samples) {
117                 int in_buf_used, out_buf_used;
118
119                 out_buf_used = resample_process(resampler, resample_factor,
120                         &in_buf_f[total_in_buf_used], f->samples - total_in_buf_used,
121                         0, &in_buf_used,
122                         &out_buf_f[total_out_buf_used], ARRAY_LEN(out_buf_f) - total_out_buf_used);
123
124                 if (out_buf_used < 0)
125                         break;
126
127                 total_out_buf_used += out_buf_used;
128                 total_in_buf_used += in_buf_used;
129
130                 if (total_out_buf_used == ARRAY_LEN(out_buf_f)) {
131                         ast_log(LOG_ERROR, "Output buffer filled ... need to increase its size\n");
132                         res = -1;
133                         break;
134                 }
135         }
136
137         for (i = 0; i < total_out_buf_used; i++)
138                 out_buf[i] = out_buf_f[i] * (SHRT_MAX / FLT_MAX);       
139
140         pvt->samples += total_out_buf_used;
141         pvt->datalen += (total_out_buf_used * sizeof(int16_t));
142
143         return res;
144 }
145
146 static int slin16_to_slin8_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
147 {
148         struct slin16_to_slin8_pvt *resamp_pvt = pvt->pvt;
149
150         return resample_frame(pvt, resamp_pvt->resampler, resamp_pvt->resample_factor, f);
151 }
152
153 static int slin8_to_slin16_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
154 {
155         struct slin8_to_slin16_pvt *resamp_pvt = pvt->pvt;
156
157         return resample_frame(pvt, resamp_pvt->resampler, resamp_pvt->resample_factor, f);
158 }
159
160 static struct ast_frame *slin16_to_slin8_sample(void)
161 {
162         static struct ast_frame f = {
163                 .frametype = AST_FRAME_VOICE,
164                 .subclass = AST_FORMAT_SLINEAR16,
165                 .datalen = sizeof(slin16_slin8_ex),
166                 .samples = sizeof(slin16_slin8_ex) / sizeof(slin16_slin8_ex[0]),
167                 .src = __PRETTY_FUNCTION__,
168                 .data = slin16_slin8_ex,
169         };
170
171         return &f;
172 }
173
174 static struct ast_frame *slin8_to_slin16_sample(void)
175 {
176         static struct ast_frame f = {
177                 .frametype = AST_FRAME_VOICE,
178                 .subclass = AST_FORMAT_SLINEAR,
179                 .datalen = sizeof(slin8_slin16_ex),
180                 .samples = sizeof(slin8_slin16_ex) / sizeof(slin8_slin16_ex[0]),
181                 .src = __PRETTY_FUNCTION__,
182                 .data = slin8_slin16_ex,
183         };
184
185         return &f;
186 }
187
188 static struct ast_translator slin16_to_slin8 = {
189         .name = "slin16_to_slin8",
190         .srcfmt = AST_FORMAT_SLINEAR16,
191         .dstfmt = AST_FORMAT_SLINEAR,
192         .newpvt = slin16_to_slin8_new,
193         .destroy = slin16_to_slin8_destroy,
194         .framein = slin16_to_slin8_framein,
195         .sample = slin16_to_slin8_sample,
196         .desc_size = sizeof(struct slin16_to_slin8_pvt),
197         .buffer_samples = (OUTBUF_SIZE / sizeof(int16_t)),
198         .buf_size = OUTBUF_SIZE,
199 };
200
201 static struct ast_translator slin8_to_slin16 = {
202         .name = "slin8_to_slin16",
203         .srcfmt = AST_FORMAT_SLINEAR,
204         .dstfmt = AST_FORMAT_SLINEAR16,
205         .newpvt = slin8_to_slin16_new,
206         .destroy = slin8_to_slin16_destroy,
207         .framein = slin8_to_slin16_framein,
208         .sample = slin8_to_slin16_sample,
209         .desc_size = sizeof(struct slin8_to_slin16_pvt),
210         .buffer_samples = OUTBUF_SIZE,
211         .buf_size = OUTBUF_SIZE,
212 };
213
214 static int unload_module(void)
215 {
216         int res = 0;
217
218         res |= ast_unregister_translator(&slin16_to_slin8);
219         res |= ast_unregister_translator(&slin8_to_slin16);
220
221         return res;
222 }
223
224 static int load_module(void)
225 {
226         int res = 0;
227
228         res |= ast_register_translator(&slin16_to_slin8);
229         res |= ast_register_translator(&slin8_to_slin16);
230
231         return AST_MODULE_LOAD_SUCCESS;
232 }
233
234 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "SLIN Resampling Codec");