Media Project Phase2: SILK 8khz-24khz, SLINEAR 8khz-192khz, SPEEX 32khz, hd audio...
[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         <depend>resample</depend>
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 = f->samples;
79
80         speex_resampler_process_int(resamp_pvt,
81                 0,
82                 f->data.ptr,
83                 &in_samples,
84                 pvt->outbuf.i16 + pvt->samples,
85                 &out_samples);
86
87         pvt->samples += out_samples;
88         pvt->datalen += out_samples * 2;
89
90         return 0;
91 }
92
93 static int unload_module(void)
94 {
95         int res = 0;
96         int idx;
97
98         for (idx = 0; idx < trans_size; idx++) {
99                 res |= ast_unregister_translator(&translators[idx]);
100         }
101         ast_free(translators);
102
103         return res;
104 }
105
106 static int load_module(void)
107 {
108         int res = 0;
109         int x, y, idx = 0;
110
111         trans_size = ARRAY_LEN(id_list) * ARRAY_LEN(id_list);
112         if (!(translators = ast_calloc(1, sizeof(struct ast_translator) * trans_size))) {
113                 return AST_MODULE_LOAD_FAILURE;
114         }
115
116         for (x = 0; x < ARRAY_LEN(id_list); x++) {
117                 for (y = 0; y < ARRAY_LEN(id_list); y++) {
118                         if (x == y) {
119                                 continue;
120                         }
121                         translators[idx].newpvt = resamp_new;
122                         translators[idx].destroy = resamp_destroy;
123                         translators[idx].framein = resamp_framein;
124                         translators[idx].desc_size = 0;
125                         translators[idx].buffer_samples = (OUTBUF_SIZE / sizeof(int16_t));
126                         translators[idx].buf_size = OUTBUF_SIZE;
127                         ast_format_set(&translators[idx].src_format, id_list[x], 0);
128                         ast_format_set(&translators[idx].dst_format, id_list[y], 0);
129                         snprintf(translators[idx].name, sizeof(translators[idx].name), "slin %dkhz -> %dkhz",
130                                 ast_format_rate(&translators[idx].src_format), ast_format_rate(&translators[idx].dst_format));
131                         res |= ast_register_translator(&translators[idx]);
132                         idx++;
133                 }
134
135         }
136
137         return AST_MODULE_LOAD_SUCCESS;
138 }
139
140 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "SLIN Resampling Codec");