Merged revisions 330940 via svnmerge from
[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 /*** MODULEINFO
29         <support_level>core</support_level>
30  ***/
31
32 #include "asterisk.h"
33 #include "speex/speex_resampler.h"
34
35 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
36
37 #include "asterisk/module.h"
38 #include "asterisk/translate.h"
39 #include "asterisk/slin.h"
40
41 #define OUTBUF_SIZE   8096
42
43 static struct ast_translator *translators;
44 static int trans_size;
45 static int id_list[] = {
46         AST_FORMAT_SLINEAR,
47         AST_FORMAT_SLINEAR12,
48         AST_FORMAT_SLINEAR16,
49         AST_FORMAT_SLINEAR24,
50         AST_FORMAT_SLINEAR32,
51         AST_FORMAT_SLINEAR44,
52         AST_FORMAT_SLINEAR48,
53         AST_FORMAT_SLINEAR96,
54         AST_FORMAT_SLINEAR192,
55 };
56
57 static int resamp_new(struct ast_trans_pvt *pvt)
58 {
59         int err;
60
61         if (!(pvt->pvt = speex_resampler_init(1, ast_format_rate(&pvt->t->src_format), ast_format_rate(&pvt->t->dst_format), 5, &err))) {
62                 return -1;
63         }
64
65         return 0;
66 }
67
68 static void resamp_destroy(struct ast_trans_pvt *pvt)
69 {
70         SpeexResamplerState *resamp_pvt = pvt->pvt;
71         speex_resampler_destroy(resamp_pvt);
72 }
73
74 static int resamp_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
75 {
76         SpeexResamplerState *resamp_pvt = pvt->pvt;
77         unsigned int out_samples = (OUTBUF_SIZE / sizeof(int16_t)) - pvt->samples;
78         unsigned int in_samples;
79
80         if (!f->datalen) {
81                 return -1;
82         }
83         in_samples = f->datalen / 2;
84
85         speex_resampler_process_int(resamp_pvt,
86                 0,
87                 f->data.ptr,
88                 &in_samples,
89                 pvt->outbuf.i16 + pvt->samples,
90                 &out_samples);
91
92         pvt->samples += out_samples;
93         pvt->datalen += out_samples * 2;
94
95         return 0;
96 }
97
98 static int unload_module(void)
99 {
100         int res = 0;
101         int idx;
102
103         for (idx = 0; idx < trans_size; idx++) {
104                 res |= ast_unregister_translator(&translators[idx]);
105         }
106         ast_free(translators);
107
108         return res;
109 }
110
111 static int load_module(void)
112 {
113         int res = 0;
114         int x, y, idx = 0;
115
116         trans_size = ARRAY_LEN(id_list) * ARRAY_LEN(id_list);
117         if (!(translators = ast_calloc(1, sizeof(struct ast_translator) * trans_size))) {
118                 return AST_MODULE_LOAD_FAILURE;
119         }
120
121         for (x = 0; x < ARRAY_LEN(id_list); x++) {
122                 for (y = 0; y < ARRAY_LEN(id_list); y++) {
123                         if (x == y) {
124                                 continue;
125                         }
126                         translators[idx].newpvt = resamp_new;
127                         translators[idx].destroy = resamp_destroy;
128                         translators[idx].framein = resamp_framein;
129                         translators[idx].desc_size = 0;
130                         translators[idx].buffer_samples = (OUTBUF_SIZE / sizeof(int16_t));
131                         translators[idx].buf_size = OUTBUF_SIZE;
132                         ast_format_set(&translators[idx].src_format, id_list[x], 0);
133                         ast_format_set(&translators[idx].dst_format, id_list[y], 0);
134                         snprintf(translators[idx].name, sizeof(translators[idx].name), "slin %dkhz -> %dkhz",
135                                 ast_format_rate(&translators[idx].src_format), ast_format_rate(&translators[idx].dst_format));
136                         res |= ast_register_translator(&translators[idx]);
137                         idx++;
138                 }
139
140         }
141
142         return AST_MODULE_LOAD_SUCCESS;
143 }
144
145 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "SLIN Resampling Codec");