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