Remove libresample dependency from codec_resample.c
[asterisk/asterisk.git] / codecs / codec_resample.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2011, Digium, Inc.
5  *
6  * Russell Bryant <russell@digium.com>
7  * David Vossel <dvossel@digium.com>
8  *
9  * See http://www.asterisk.org for more information about
10  * the Asterisk project. Please do not directly contact
11  * any of the maintainers of this project for assistance;
12  * the project provides a web site, mailing lists and IRC
13  * channels for your use.
14  *
15  * This program is free software, distributed under the terms of
16  * the GNU General Public License Version 2. See the LICENSE file
17  * at the top of the source tree.
18  */
19
20 /*! 
21  * \file
22  *
23  * \brief Resample slinear audio
24  * 
25  * \ingroup codecs
26  */
27
28 #include "asterisk.h"
29 #include "speex/speex_resampler.h"
30
31 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
32
33 #include "asterisk/module.h"
34 #include "asterisk/translate.h"
35 #include "asterisk/slin.h"
36
37 #define OUTBUF_SIZE   8096
38
39 static struct ast_translator *translators;
40 static int trans_size;
41 static int id_list[] = {
42         AST_FORMAT_SLINEAR,
43         AST_FORMAT_SLINEAR12,
44         AST_FORMAT_SLINEAR16,
45         AST_FORMAT_SLINEAR24,
46         AST_FORMAT_SLINEAR32,
47         AST_FORMAT_SLINEAR44,
48         AST_FORMAT_SLINEAR48,
49         AST_FORMAT_SLINEAR96,
50         AST_FORMAT_SLINEAR192,
51 };
52
53 static int resamp_new(struct ast_trans_pvt *pvt)
54 {
55         int err;
56
57         if (!(pvt->pvt = speex_resampler_init(1, ast_format_rate(&pvt->t->src_format), ast_format_rate(&pvt->t->dst_format), 5, &err))) {
58                 return -1;
59         }
60
61         return 0;
62 }
63
64 static void resamp_destroy(struct ast_trans_pvt *pvt)
65 {
66         SpeexResamplerState *resamp_pvt = pvt->pvt;
67         speex_resampler_destroy(resamp_pvt);
68 }
69
70 static int resamp_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
71 {
72         SpeexResamplerState *resamp_pvt = pvt->pvt;
73         unsigned int out_samples = (OUTBUF_SIZE / sizeof(int16_t)) - pvt->samples;
74         unsigned int in_samples = f->samples;
75
76         speex_resampler_process_int(resamp_pvt,
77                 0,
78                 f->data.ptr,
79                 &in_samples,
80                 pvt->outbuf.i16 + pvt->samples,
81                 &out_samples);
82
83         pvt->samples += out_samples;
84         pvt->datalen += out_samples * 2;
85
86         return 0;
87 }
88
89 static int unload_module(void)
90 {
91         int res = 0;
92         int idx;
93
94         for (idx = 0; idx < trans_size; idx++) {
95                 res |= ast_unregister_translator(&translators[idx]);
96         }
97         ast_free(translators);
98
99         return res;
100 }
101
102 static int load_module(void)
103 {
104         int res = 0;
105         int x, y, idx = 0;
106
107         trans_size = ARRAY_LEN(id_list) * ARRAY_LEN(id_list);
108         if (!(translators = ast_calloc(1, sizeof(struct ast_translator) * trans_size))) {
109                 return AST_MODULE_LOAD_FAILURE;
110         }
111
112         for (x = 0; x < ARRAY_LEN(id_list); x++) {
113                 for (y = 0; y < ARRAY_LEN(id_list); y++) {
114                         if (x == y) {
115                                 continue;
116                         }
117                         translators[idx].newpvt = resamp_new;
118                         translators[idx].destroy = resamp_destroy;
119                         translators[idx].framein = resamp_framein;
120                         translators[idx].desc_size = 0;
121                         translators[idx].buffer_samples = (OUTBUF_SIZE / sizeof(int16_t));
122                         translators[idx].buf_size = OUTBUF_SIZE;
123                         ast_format_set(&translators[idx].src_format, id_list[x], 0);
124                         ast_format_set(&translators[idx].dst_format, id_list[y], 0);
125                         snprintf(translators[idx].name, sizeof(translators[idx].name), "slin %dkhz -> %dkhz",
126                                 ast_format_rate(&translators[idx].src_format), ast_format_rate(&translators[idx].dst_format));
127                         res |= ast_register_translator(&translators[idx]);
128                         idx++;
129                 }
130
131         }
132
133         return AST_MODULE_LOAD_SUCCESS;
134 }
135
136 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "SLIN Resampling Codec");