1
/* $Id: resample_resample.c 3553 2011-05-05 06:14:19Z nanang $ */
3
* Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4
* Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
#include <pjmedia/resample.h>
23
#include <pjmedia/errno.h>
24
#include <pj/assert.h>
29
#if PJMEDIA_RESAMPLE_IMP==PJMEDIA_RESAMPLE_LIBRESAMPLE
31
#include <third_party/resample/include/resamplesubs.h>
33
#define THIS_FILE "resample.c"
37
struct pjmedia_resample
39
double factor; /* Conversion factor = rate_out / rate_in. */
40
pj_bool_t large_filter; /* Large filter? */
41
pj_bool_t high_quality; /* Not fast? */
42
unsigned xoff; /* History and lookahead size, in samples */
43
unsigned frame_size; /* Samples per frame. */
44
unsigned channel_cnt; /* Channel count. */
46
/* Buffer for monochannel */
47
pj_int16_t *buffer; /* Input buffer. */
49
/* Buffer for multichannel */
50
pj_int16_t **in_buffer; /* Array of input buffer for each channel. */
51
pj_int16_t *tmp_buffer; /* Temporary output buffer for processing. */
55
PJ_DEF(pj_status_t) pjmedia_resample_create( pj_pool_t *pool,
56
pj_bool_t high_quality,
57
pj_bool_t large_filter,
58
unsigned channel_count,
61
unsigned samples_per_frame,
62
pjmedia_resample **p_resample)
64
pjmedia_resample *resample;
66
PJ_ASSERT_RETURN(pool && p_resample && rate_in &&
67
rate_out && samples_per_frame, PJ_EINVAL);
69
resample = PJ_POOL_ZALLOC_T(pool, pjmedia_resample);
70
PJ_ASSERT_RETURN(resample, PJ_ENOMEM);
73
* If we're downsampling, always use the fast algorithm since it seems
74
* to yield the same quality.
76
if (rate_out < rate_in) {
77
//no this is not a good idea. It sounds pretty good with speech,
78
//but very poor with background noise etc.
82
resample->factor = rate_out * 1.0 / rate_in;
83
resample->large_filter = large_filter;
84
resample->high_quality = high_quality;
85
resample->channel_cnt = channel_count;
86
resample->frame_size = samples_per_frame;
89
/* This is a bug in xoff calculation, thanks Stephane Lussier
90
* of Macadamian dot com.
91
* resample->xoff = large_filter ? 32 : 6;
93
resample->xoff = res_GetXOFF(resample->factor, (char)large_filter);
98
if (channel_count == 1) {
101
/* Allocate input buffer */
102
size = (samples_per_frame + 2*resample->xoff) * sizeof(pj_int16_t);
103
resample->buffer = (pj_int16_t*) pj_pool_alloc(pool, size);
104
PJ_ASSERT_RETURN(resample->buffer, PJ_ENOMEM);
106
pjmedia_zero_samples(resample->buffer, resample->xoff*2);
108
} else if (channel_count > 1) {
111
/* Allocate input buffer table */
112
size = channel_count * sizeof(pj_int16_t*);
113
resample->in_buffer = (pj_int16_t**)pj_pool_alloc(pool, size);
115
/* Allocate input buffer */
116
size = (samples_per_frame/channel_count + 2*resample->xoff) *
118
for (i = 0; i < channel_count; ++i) {
119
resample->in_buffer[i] = (pj_int16_t*)pj_pool_alloc(pool, size);
120
PJ_ASSERT_RETURN(resample->in_buffer, PJ_ENOMEM);
121
pjmedia_zero_samples(resample->in_buffer[i], resample->xoff*2);
124
/* Allocate temporary output buffer */
125
size = (unsigned) (resample->frame_size * sizeof(pj_int16_t) *
126
resample->factor / channel_count + 0.5);
127
resample->tmp_buffer = (pj_int16_t*) pj_pool_alloc(pool, size);
128
PJ_ASSERT_RETURN(resample->tmp_buffer, PJ_ENOMEM);
131
*p_resample = resample;
133
PJ_LOG(5,(THIS_FILE, "resample created: %s qualiy, %s filter, in/out "
135
(high_quality?"high":"low"),
136
(large_filter?"large":"small"),
143
PJ_DEF(void) pjmedia_resample_run( pjmedia_resample *resample,
144
const pj_int16_t *input,
147
PJ_ASSERT_ON_FAIL(resample, return);
149
/* Okay chaps, here's how we do resampling.
151
* The original resample algorithm requires xoff samples *before* the
152
* input buffer as history, and another xoff samples *after* the
153
* end of the input buffer as lookahead. Since application can only
154
* supply framesize buffer on each run, PJMEDIA needs to arrange the
155
* buffer to meet these requirements.
157
* So here comes the trick.
159
* First of all, because of the history and lookahead requirement,
160
* resample->buffer need to accomodate framesize+2*xoff samples in its
161
* buffer. This is done when the buffer is created.
163
* On the first run, the input frame (supplied by application) is
164
* copied to resample->buffer at 2*xoff position. The first 2*xoff
165
* samples are initially zeroed (in the initialization). The resample
166
* algorithm then invoked at resample->buffer+xoff ONLY, thus giving
167
* it one xoff at the beginning as zero, and one xoff at the end
168
* as the end of the original input. The resample algorithm will see
169
* that the first xoff samples in the input as zero.
171
* So here's the layout of resample->buffer on the first run.
174
* +------+------+--------------+
175
* | 0000 | 0000 | frame0... |
176
* +------+------+--------------+
178
* 0 xoff 2*xoff size+2*xoff
180
* (Note again: resample algorithm is called at resample->buffer+xoff)
182
* At the end of the run, 2*xoff samples from the end of
183
* resample->buffer are copied to the beginning of resample->buffer.
184
* The first xoff part of this will be used as history for the next
185
* run, and the second xoff part of this is actually the start of
186
* resampling for the next run.
188
* And the first run completes, the function returns.
191
* On the next run, the input frame supplied by application is again
192
* copied at 2*xoff position in the resample->buffer, and the
193
* resample algorithm is again invoked at resample->buffer+xoff
194
* position. So effectively, the resample algorithm will start its
195
* operation on the last xoff from the previous frame, and gets the
196
* history from the last 2*xoff of the previous frame, and the look-
197
* ahead from the last xoff of current frame.
199
* So on this run, the buffer layout is:
202
* +------+------+--------------+
203
* | frm0 | frm0 | frame1... |
204
* +------+------+--------------+
206
* 0 xoff 2*xoff size+2*xoff
208
* As you can see from above diagram, the resampling algorithm is
209
* actually called from the last xoff part of previous frame (frm0).
211
* And so on the process continues for the next frame, and the next,
215
if (resample->channel_cnt == 1) {
217
const pj_int16_t *src_buf;
219
/* Prepare input frame */
220
dst_buf = resample->buffer + resample->xoff*2;
221
pjmedia_copy_samples(dst_buf, input, resample->frame_size);
224
if (resample->high_quality) {
225
res_Resample(resample->buffer + resample->xoff, output,
226
resample->factor, (pj_uint16_t)resample->frame_size,
227
(char)resample->large_filter, (char)PJ_TRUE);
229
res_SrcLinear(resample->buffer + resample->xoff, output,
230
resample->factor, (pj_uint16_t)resample->frame_size);
234
dst_buf = resample->buffer;
235
src_buf = input + resample->frame_size - resample->xoff*2;
236
pjmedia_copy_samples(dst_buf, src_buf, resample->xoff * 2);
238
} else { /* Multichannel */
241
for (i = 0; i < resample->channel_cnt; ++i) {
243
const pj_int16_t *src_buf;
244
unsigned mono_frm_sz_in;
245
unsigned mono_frm_sz_out;
247
mono_frm_sz_in = resample->frame_size / resample->channel_cnt;
248
mono_frm_sz_out = (unsigned)(mono_frm_sz_in * resample->factor + 0.5);
250
/* Deinterleave input */
251
dst_buf = resample->in_buffer[i] + resample->xoff*2;
253
for (j = 0; j < mono_frm_sz_in; ++j) {
254
*dst_buf++ = *src_buf;
255
src_buf += resample->channel_cnt;
258
/* Resample this channel */
259
if (resample->high_quality) {
260
res_Resample(resample->in_buffer[i] + resample->xoff,
261
resample->tmp_buffer, resample->factor,
262
(pj_uint16_t)mono_frm_sz_in,
263
(char)resample->large_filter, (char)PJ_TRUE);
265
res_SrcLinear( resample->in_buffer[i],
266
resample->tmp_buffer,
268
(pj_uint16_t)mono_frm_sz_in);
272
dst_buf = resample->in_buffer[i];
273
src_buf = resample->in_buffer[i] + mono_frm_sz_in;
274
pjmedia_copy_samples(dst_buf, src_buf, resample->xoff * 2);
276
/* Reinterleave output */
277
dst_buf = output + i;
278
src_buf = resample->tmp_buffer;
279
for (j = 0; j < mono_frm_sz_out; ++j) {
280
*dst_buf = *src_buf++;
281
dst_buf += resample->channel_cnt;
287
PJ_DEF(unsigned) pjmedia_resample_get_input_size(pjmedia_resample *resample)
289
PJ_ASSERT_RETURN(resample != NULL, 0);
290
return resample->frame_size;
293
PJ_DEF(void) pjmedia_resample_destroy(pjmedia_resample *resample)
295
PJ_UNUSED_ARG(resample);
299
#elif PJMEDIA_RESAMPLE_IMP==PJMEDIA_RESAMPLE_NONE
302
* This is the configuration when sample rate conversion is disabled.
304
PJ_DEF(pj_status_t) pjmedia_resample_create( pj_pool_t *pool,
305
pj_bool_t high_quality,
306
pj_bool_t large_filter,
307
unsigned channel_count,
310
unsigned samples_per_frame,
311
pjmedia_resample **p_resample)
314
PJ_UNUSED_ARG(high_quality);
315
PJ_UNUSED_ARG(large_filter);
316
PJ_UNUSED_ARG(channel_count);
317
PJ_UNUSED_ARG(rate_in);
318
PJ_UNUSED_ARG(rate_out);
319
PJ_UNUSED_ARG(samples_per_frame);
320
PJ_UNUSED_ARG(p_resample);
322
return PJ_EINVALIDOP;
325
PJ_DEF(void) pjmedia_resample_run( pjmedia_resample *resample,
326
const pj_int16_t *input,
329
PJ_UNUSED_ARG(resample);
330
PJ_UNUSED_ARG(input);
331
PJ_UNUSED_ARG(output);
334
PJ_DEF(unsigned) pjmedia_resample_get_input_size(pjmedia_resample *resample)
336
PJ_UNUSED_ARG(resample);
340
PJ_DEF(void) pjmedia_resample_destroy(pjmedia_resample *resample)
342
PJ_UNUSED_ARG(resample);
345
#endif /* PJMEDIA_RESAMPLE_IMP */