aboutsummaryrefslogtreecommitdiffstats
path: root/codecs/codec_resample.c
blob: 834fb46ba7c63c43eb1c17ff31353fc7ff83da66 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
 * Asterisk -- An open source telephony toolkit.
 *
 * Copyright (C) 2007, Digium, Inc.
 *
 * Russell Bryant <russell@digium.com>
 *
 * See http://www.asterisk.org for more information about
 * the Asterisk project. Please do not directly contact
 * any of the maintainers of this project for assistance;
 * the project provides a web site, mailing lists and IRC
 * channels for your use.
 *
 * This program is free software, distributed under the terms of
 * the GNU General Public License Version 2. See the LICENSE file
 * at the top of the source tree.
 */

/*! 
 * \file
 *
 * \brief Resample slinear audio
 * 
 * \note To install libresample, check it out of the following repository:
 * <code>$ svn co http://svn.digium.com/svn/thirdparty/libresample/trunk</code>
 *
 * \ingroup codecs
 */

/*** MODULEINFO
	<depend>resample</depend>
 ***/

#include "asterisk.h"

ASTERISK_FILE_VERSION(__FILE__, "$Revision$")

/* These are for SHRT_MAX and FLT_MAX -- { */
#if defined(__Darwin__) || defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__CYGWIN__)
#include <float.h>
#else
#include <values.h>
#endif
#include <limits.h>
/* } */

#include <libresample.h>

#include "asterisk/module.h"
#include "asterisk/translate.h"

#include "asterisk/slin.h"

#define RESAMPLER_QUALITY 1

#define OUTBUF_SIZE   8096

struct slin16_to_slin8_pvt {
	void *resampler;
	float resample_factor;
};

struct slin8_to_slin16_pvt {
	void *resampler;
	float resample_factor;
};

static int slin16_to_slin8_new(struct ast_trans_pvt *pvt)
{
	struct slin16_to_slin8_pvt *resamp_pvt = pvt->pvt;

	resamp_pvt->resample_factor = 8000.0 / 16000.0;

	if (!(resamp_pvt->resampler = resample_open(RESAMPLER_QUALITY, resamp_pvt->resample_factor, resamp_pvt->resample_factor)))
		return -1;

	return 0;
}

static int slin8_to_slin16_new(struct ast_trans_pvt *pvt)
{
	struct slin8_to_slin16_pvt *resamp_pvt = pvt->pvt;

	resamp_pvt->resample_factor = 16000.0 / 8000.0;

	if (!(resamp_pvt->resampler = resample_open(RESAMPLER_QUALITY, resamp_pvt->resample_factor, resamp_pvt->resample_factor)))
		return -1;

	return 0;
}

static void slin16_to_slin8_destroy(struct ast_trans_pvt *pvt)
{
	struct slin16_to_slin8_pvt *resamp_pvt = pvt->pvt;

	if (resamp_pvt->resampler)
		resample_close(resamp_pvt->resampler);
}

static void slin8_to_slin16_destroy(struct ast_trans_pvt *pvt)
{
	struct slin8_to_slin16_pvt *resamp_pvt = pvt->pvt;

	if (resamp_pvt->resampler)
		resample_close(resamp_pvt->resampler);
}

static int resample_frame(struct ast_trans_pvt *pvt,
	void *resampler, float resample_factor, struct ast_frame *f)
{
	int total_in_buf_used = 0;
	int total_out_buf_used = 0;
	int16_t *in_buf = (int16_t *) f->data.ptr;
	int16_t *out_buf = pvt->outbuf.i16 + pvt->samples;
	float in_buf_f[f->samples];
	float out_buf_f[2048];
	int res = 0;
	int i;

	for (i = 0; i < f->samples; i++)
		in_buf_f[i] = in_buf[i] * (FLT_MAX / SHRT_MAX);

	while (total_in_buf_used < f->samples) {
		int in_buf_used, out_buf_used;

		out_buf_used = resample_process(resampler, resample_factor,
			&in_buf_f[total_in_buf_used], f->samples - total_in_buf_used,
			0, &in_buf_used,
			&out_buf_f[total_out_buf_used], ARRAY_LEN(out_buf_f) - total_out_buf_used);

		if (out_buf_used < 0)
			break;

		total_out_buf_used += out_buf_used;
		total_in_buf_used += in_buf_used;

		if (total_out_buf_used == ARRAY_LEN(out_buf_f)) {
			ast_log(LOG_ERROR, "Output buffer filled ... need to increase its size\n");
			res = -1;
			break;
		}
	}

	for (i = 0; i < total_out_buf_used; i++)
		out_buf[i] = out_buf_f[i] * (SHRT_MAX / FLT_MAX);	

	pvt->samples += total_out_buf_used;
	pvt->datalen += (total_out_buf_used * sizeof(int16_t));

	return res;
}

static int slin16_to_slin8_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
{
	struct slin16_to_slin8_pvt *resamp_pvt = pvt->pvt;
	void *resampler = resamp_pvt->resampler;
	float resample_factor = resamp_pvt->resample_factor;

	return resample_frame(pvt, resampler, resample_factor, f);
}

static int slin8_to_slin16_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
{
	struct slin8_to_slin16_pvt *resamp_pvt = pvt->pvt;
	void *resampler = resamp_pvt->resampler;
	float resample_factor = resamp_pvt->resample_factor;

	return resample_frame(pvt, resampler, resample_factor, f);
}

static struct ast_translator slin16_to_slin8 = {
	.name = "slin16_to_slin8",
	.srcfmt = AST_FORMAT_SLINEAR16,
	.dstfmt = AST_FORMAT_SLINEAR,
	.newpvt = slin16_to_slin8_new,
	.destroy = slin16_to_slin8_destroy,
	.framein = slin16_to_slin8_framein,
	.sample = slin16_sample,
	.desc_size = sizeof(struct slin16_to_slin8_pvt),
	.buffer_samples = (OUTBUF_SIZE / sizeof(int16_t)),
	.buf_size = OUTBUF_SIZE,
};

static struct ast_translator slin8_to_slin16 = {
	.name = "slin8_to_slin16",
	.srcfmt = AST_FORMAT_SLINEAR,
	.dstfmt = AST_FORMAT_SLINEAR16,
	.newpvt = slin8_to_slin16_new,
	.destroy = slin8_to_slin16_destroy,
	.framein = slin8_to_slin16_framein,
	.sample = slin8_sample,
	.desc_size = sizeof(struct slin8_to_slin16_pvt),
	.buffer_samples = (OUTBUF_SIZE / sizeof(int16_t)),
	.buf_size = OUTBUF_SIZE,
};

static int unload_module(void)
{
	int res = 0;

	res |= ast_unregister_translator(&slin16_to_slin8);
	res |= ast_unregister_translator(&slin8_to_slin16);

	return res;
}

static int load_module(void)
{
	int res = 0;

	res |= ast_register_translator(&slin16_to_slin8);
	res |= ast_register_translator(&slin8_to_slin16);

	return AST_MODULE_LOAD_SUCCESS;
}

AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "SLIN Resampling Codec");