summaryrefslogtreecommitdiffstats
path: root/tinyDAV/src/audio/tdav_speex_resampler.c
blob: f71ddd21d59f3723b7b960d7fbd6b0ed9d5ebc37 (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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/*
* Copyright (C) 2011-2015 Mamadou DIOP.
*
* This file is part of Open Source Doubango Framework.
*
* DOUBANGO is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* DOUBANGO is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with DOUBANGO.
*
*/
#include "tinydav/audio/tdav_speex_resampler.h"

#if HAVE_SPEEX_DSP && (!defined(HAVE_SPEEX_RESAMPLER) || HAVE_SPEEX_RESAMPLER)

#include <speex/speex_resampler.h>

#include "tsk_memory.h"
#include "tsk_debug.h"

#define TDAV_SPEEX_RESAMPLER_MAX_QUALITY 10

/** Speex resampler*/
typedef struct tdav_speex_resampler_s
{
	TMEDIA_DECLARE_RESAMPLER;

	tsk_size_t in_size;
	tsk_size_t out_size;
	uint32_t in_channels;
	uint32_t out_channels;
	uint32_t bytes_per_sample;

	struct{
		void* ptr;
		tsk_size_t size_in_samples;
	} tmp_buffer;

	SpeexResamplerState *state;
}
tdav_speex_resampler_t;

static int tdav_speex_resampler_open(tmedia_resampler_t* self, uint32_t in_freq, uint32_t out_freq, uint32_t frame_duration, uint32_t in_channels, uint32_t out_channels, uint32_t quality, uint32_t bits_per_sample)
{
	tdav_speex_resampler_t *resampler = (tdav_speex_resampler_t *)self;
	int ret = 0;
	uint32_t bytes_per_sample = (bits_per_sample >> 3);

	if (in_channels != 1 && in_channels != 2) {
		TSK_DEBUG_ERROR("%d not valid as input channel", in_channels);
		return -1;
	}
	if (out_channels != 1 && out_channels != 2) {
		TSK_DEBUG_ERROR("%d not valid as output channel", out_channels);
		return -1;
	}
	if (bytes_per_sample != sizeof(spx_int16_t) && bytes_per_sample != sizeof(float)) {
		TSK_DEBUG_ERROR("%d not valid as bits_per_sample", bits_per_sample);
		return -1;
	}

	if (!(resampler->state = speex_resampler_init(in_channels, in_freq, out_freq, TSK_CLAMP(0, quality, TDAV_SPEEX_RESAMPLER_MAX_QUALITY), &ret))) {
		TSK_DEBUG_ERROR("speex_resampler_init() returned %d", ret);
		return -2;
	}

	resampler->bytes_per_sample = bytes_per_sample;
	resampler->in_size = ((in_freq * frame_duration) / 1000) << (in_channels == 2 ? 1 : 0);
	resampler->out_size = ((out_freq * frame_duration) / 1000) << (out_channels == 2 ? 1 : 0);
	resampler->in_channels = in_channels;
	resampler->out_channels = out_channels;

	if (in_channels != out_channels) {
		resampler->tmp_buffer.size_in_samples = ((TSK_MAX(in_freq, out_freq) * frame_duration) / 1000) << (TSK_MAX(in_channels, out_channels) == 2 ? 1 : 0);
		if (!(resampler->tmp_buffer.ptr = tsk_realloc(resampler->tmp_buffer.ptr, resampler->tmp_buffer.size_in_samples * resampler->bytes_per_sample))) {
			resampler->tmp_buffer.size_in_samples = 0;
			return -2;
		}
	}

	return 0;
}


static tsk_size_t tdav_speex_resampler_process(tmedia_resampler_t* self, const void* in_data, tsk_size_t in_size_in_sample, void* out_data, tsk_size_t out_size_in_sample)
{
	tdav_speex_resampler_t *resampler = (tdav_speex_resampler_t *)self;
	int err = RESAMPLER_ERR_SUCCESS;
	spx_uint32_t _out_size_in_sample = (spx_uint32_t)out_size_in_sample;
	if (!resampler->state || !out_data) {
		TSK_DEBUG_ERROR("Invalid parameter");
		return 0;
	}

	if (in_size_in_sample != resampler->in_size) {
		TSK_DEBUG_ERROR("Input data has wrong size");
		return 0;
	}

	if (out_size_in_sample < resampler->out_size) {
		TSK_DEBUG_ERROR("Output data is too short");
		return 0;
	}

	if (resampler->in_channels == resampler->out_channels) {
		if (resampler->bytes_per_sample == sizeof(spx_int16_t)) {
			err = speex_resampler_process_int(resampler->state, 0,
				(const spx_int16_t *)in_data, (spx_uint32_t *)&in_size_in_sample,
				(spx_int16_t *)out_data, &_out_size_in_sample);
		}
		else {
			err = speex_resampler_process_float(resampler->state, 0,
				(const float *)in_data, (spx_uint32_t *)&in_size_in_sample,
				(float *)out_data, &_out_size_in_sample);
		}
	}
	else {
		spx_uint32_t i, j;
		// in_channels = 1, out_channels = 2
		if (resampler->in_channels == 1) {
			if (resampler->bytes_per_sample == sizeof(spx_int16_t)) {
				err = speex_resampler_process_int(resampler->state, 0, (const spx_int16_t *)in_data, (spx_uint32_t *)&in_size_in_sample, resampler->tmp_buffer.ptr, &_out_size_in_sample);
				if (err == RESAMPLER_ERR_SUCCESS) {
					spx_int16_t* pout_data = (spx_int16_t*)(out_data);
					for (i = 0, j = 0; i < _out_size_in_sample; ++i, j += 2) {
						pout_data[j] = pout_data[j + 1] = *(((const spx_int16_t*)resampler->tmp_buffer.ptr) + i);
					}
				}
			}
			else {
				err = speex_resampler_process_float(resampler->state, 0, (const float *)in_data, (spx_uint32_t *)&in_size_in_sample, resampler->tmp_buffer.ptr, &_out_size_in_sample);
				if (err == RESAMPLER_ERR_SUCCESS) {
					float* pout_data = (float*)(out_data);
					for (i = 0, j = 0; i < _out_size_in_sample; ++i, j += 2) {
						pout_data[j] = pout_data[j + 1] = *(((const float*)resampler->tmp_buffer.ptr) + i);
					}
				}
			}

		}
		else {
			// in_channels = 2, out_channels = 1
			spx_uint32_t _out_size2_in_sample = (_out_size_in_sample << 1);
			if (resampler->bytes_per_sample == sizeof(spx_int16_t)) {
				err = speex_resampler_process_int(resampler->state, 0,
					(const spx_int16_t *)in_data, (spx_uint32_t *)&in_size_in_sample,
					(spx_int16_t *)resampler->tmp_buffer.ptr, &_out_size2_in_sample);
				if (err == RESAMPLER_ERR_SUCCESS) {
					spx_int16_t* pout_data = (spx_int16_t*)(out_data);
					_out_size_in_sample = (spx_uint32_t)resampler->out_size;
					for (i = 0, j = 0; j < _out_size2_in_sample; ++i, j += 2) {
						pout_data[i] = *(((const spx_int16_t*)resampler->tmp_buffer.ptr) + j);
					}
				}
			}
			else {
				err = speex_resampler_process_float(resampler->state, 0,
					(const float *)in_data, (spx_uint32_t *)&in_size_in_sample,
					(float *)resampler->tmp_buffer.ptr, &_out_size2_in_sample);
				if (err == RESAMPLER_ERR_SUCCESS) {
					float* pout_data = (float*)(out_data);
					for (i = 0, j = 0; j < _out_size2_in_sample; ++i, j += 2) {
						pout_data[i] = *(((const float*)resampler->tmp_buffer.ptr) + j);
					}
				}
			}
		}
	}

	if (err != RESAMPLER_ERR_SUCCESS) {
		TSK_DEBUG_ERROR("speex_resampler_process_int() failed with error code %d", err);
		return 0;
	}
	return (tsk_size_t)_out_size_in_sample;
}

static int tdav_speex_resampler_close(tmedia_resampler_t* self)
{
	tdav_speex_resampler_t *resampler = (tdav_speex_resampler_t *)self;

	if (resampler->state) {
		speex_resampler_destroy(resampler->state);
		resampler->state = tsk_null;
	}
	return 0;
}



//
//	Speex resamplerr Plugin definition
//

/* constructor */
static tsk_object_t* tdav_speex_resampler_ctor(tsk_object_t * self, va_list * app)
{
	tdav_speex_resampler_t *resampler = (tdav_speex_resampler_t *)self;
	if (resampler){
		/* init base */
		tmedia_resampler_init(TMEDIA_RESAMPLER(resampler));
		/* init self */
	}
	return self;
}
/* destructor */
static tsk_object_t* tdav_speex_resampler_dtor(tsk_object_t * self)
{
	tdav_speex_resampler_t *resampler = (tdav_speex_resampler_t *)self;
	if (resampler){
		/* deinit base */
		tmedia_resampler_deinit(TMEDIA_RESAMPLER(resampler));
		/* deinit self */
		if (resampler->state) {
			speex_resampler_destroy(resampler->state);
			resampler->state = tsk_null;
		}
		TSK_FREE(resampler->tmp_buffer.ptr);

		TSK_DEBUG_INFO("*** SpeexDSP resampler (plugin) destroyed ***");
	}

	return self;
}
/* object definition */
static const tsk_object_def_t tdav_speex_resampler_def_s =
{
	sizeof(tdav_speex_resampler_t),
	tdav_speex_resampler_ctor,
	tdav_speex_resampler_dtor,
	tsk_null,
};
/* plugin definition*/
static const tmedia_resampler_plugin_def_t tdav_speex_resampler_plugin_def_s =
{
	&tdav_speex_resampler_def_s,

	"Audio Resampler based on Speex",

	tdav_speex_resampler_open,
	tdav_speex_resampler_process,
	tdav_speex_resampler_close,
};
const tmedia_resampler_plugin_def_t *tdav_speex_resampler_plugin_def_t = &tdav_speex_resampler_plugin_def_s;


#endif /* HAVE_SPEEX_DSP */
OpenPOWER on IntegriCloud