Fix the buffer_samples value. For signed linear, the number of samples needed
[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         double resample_factor;
53 };
54
55 struct slin8_to_slin16_pvt {
56         void *resampler;
57         double 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, double 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;
106         int16_t *out_buf = (int16_t *) pvt->outbuf + 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
149         return resample_frame(pvt, resamp_pvt->resampler, resamp_pvt->resample_factor, f);
150 }
151
152 static int slin8_to_slin16_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
153 {
154         struct slin8_to_slin16_pvt *resamp_pvt = pvt->pvt;
155
156         return resample_frame(pvt, resamp_pvt->resampler, resamp_pvt->resample_factor, f);
157 }
158
159 static struct ast_frame *slin16_to_slin8_sample(void)
160 {
161         static struct ast_frame f = {
162                 .frametype = AST_FRAME_VOICE,
163                 .subclass = AST_FORMAT_SLINEAR16,
164                 .datalen = sizeof(slin16_slin8_ex),
165                 .samples = sizeof(slin16_slin8_ex) / sizeof(slin16_slin8_ex[0]),
166                 .src = __PRETTY_FUNCTION__,
167                 .data = slin16_slin8_ex,
168         };
169
170         return &f;
171 }
172
173 static struct ast_frame *slin8_to_slin16_sample(void)
174 {
175         static struct ast_frame f = {
176                 .frametype = AST_FRAME_VOICE,
177                 .subclass = AST_FORMAT_SLINEAR,
178                 .datalen = sizeof(slin8_slin16_ex),
179                 .samples = sizeof(slin8_slin16_ex) / sizeof(slin8_slin16_ex[0]),
180                 .src = __PRETTY_FUNCTION__,
181                 .data = slin8_slin16_ex,
182         };
183
184         return &f;
185 }
186
187 static struct ast_translator slin16_to_slin8 = {
188         .name = "slin16_to_slin8",
189         .srcfmt = AST_FORMAT_SLINEAR16,
190         .dstfmt = AST_FORMAT_SLINEAR,
191         .newpvt = slin16_to_slin8_new,
192         .destroy = slin16_to_slin8_destroy,
193         .framein = slin16_to_slin8_framein,
194         .sample = slin16_to_slin8_sample,
195         .desc_size = sizeof(struct slin16_to_slin8_pvt),
196         .buffer_samples = (OUTBUF_SIZE / sizeof(int16_t)),
197         .buf_size = OUTBUF_SIZE,
198 };
199
200 static struct ast_translator slin8_to_slin16 = {
201         .name = "slin8_to_slin16",
202         .srcfmt = AST_FORMAT_SLINEAR,
203         .dstfmt = AST_FORMAT_SLINEAR16,
204         .newpvt = slin8_to_slin16_new,
205         .destroy = slin8_to_slin16_destroy,
206         .framein = slin8_to_slin16_framein,
207         .sample = slin8_to_slin16_sample,
208         .desc_size = sizeof(struct slin8_to_slin16_pvt),
209         .buffer_samples = (OUTBUF_SIZE / sizeof(int16_t)),
210         .buf_size = OUTBUF_SIZE,
211 };
212
213 static int unload_module(void)
214 {
215         int res = 0;
216
217         res |= ast_unregister_translator(&slin16_to_slin8);
218         res |= ast_unregister_translator(&slin8_to_slin16);
219
220         return res;
221 }
222
223 static int load_module(void)
224 {
225         int res = 0;
226
227         res |= ast_register_translator(&slin16_to_slin8);
228         res |= ast_register_translator(&slin8_to_slin16);
229
230         return AST_MODULE_LOAD_SUCCESS;
231 }
232
233 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "SLIN Resampling Codec");