2004-07-16 19:56:36 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
|
|
|
|
|
/***
|
2006-06-19 21:53:48 +00:00
|
|
|
This file is part of PulseAudio.
|
2004-07-16 19:56:36 +00:00
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
PulseAudio is free software; you can redistribute it and/or modify
|
2004-11-14 14:58:54 +00:00
|
|
|
it under the terms of the GNU Lesser General Public License as published
|
2004-07-16 19:56:36 +00:00
|
|
|
by the Free Software Foundation; either version 2 of the License,
|
|
|
|
|
or (at your option) any later version.
|
|
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
PulseAudio is distributed in the hope that it will be useful, but
|
2004-07-16 19:56:36 +00:00
|
|
|
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.
|
|
|
|
|
|
2004-11-14 14:58:54 +00:00
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
2006-06-19 21:53:48 +00:00
|
|
|
along with PulseAudio; if not, write to the Free Software
|
2004-07-16 19:56:36 +00:00
|
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
|
|
|
|
USA.
|
|
|
|
|
***/
|
|
|
|
|
|
2004-07-16 19:16:42 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
|
#include <config.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
2004-07-02 18:47:03 +00:00
|
|
|
#include <assert.h>
|
2004-11-20 16:23:53 +00:00
|
|
|
#include <string.h>
|
2004-07-02 18:47:03 +00:00
|
|
|
|
|
|
|
|
#include <samplerate.h>
|
2006-01-27 16:25:31 +00:00
|
|
|
#include <liboil/liboilfuncs.h>
|
|
|
|
|
#include <liboil/liboil.h>
|
2004-07-02 18:47:03 +00:00
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
#include <pulse/xmalloc.h>
|
2006-05-17 16:34:18 +00:00
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
#include <pulsecore/sconv.h>
|
|
|
|
|
#include <pulsecore/log.h>
|
2006-02-17 12:10:58 +00:00
|
|
|
|
2004-07-02 18:47:03 +00:00
|
|
|
#include "resampler.h"
|
|
|
|
|
|
2004-07-03 23:35:12 +00:00
|
|
|
struct pa_resampler {
|
2006-01-27 16:25:31 +00:00
|
|
|
pa_resample_method_t resample_method;
|
2006-01-11 01:17:39 +00:00
|
|
|
pa_sample_spec i_ss, o_ss;
|
2006-01-27 16:25:31 +00:00
|
|
|
pa_channel_map i_cm, o_cm;
|
2004-11-20 16:23:53 +00:00
|
|
|
size_t i_fz, o_fz;
|
2006-01-11 01:17:39 +00:00
|
|
|
pa_memblock_stat *memblock_stat;
|
2004-07-02 18:47:03 +00:00
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
void (*impl_free)(pa_resampler *r);
|
2006-01-27 16:25:31 +00:00
|
|
|
void (*impl_update_input_rate)(pa_resampler *r, uint32_t rate);
|
2006-01-11 01:17:39 +00:00
|
|
|
void (*impl_run)(pa_resampler *r, const pa_memchunk *in, pa_memchunk *out);
|
2006-01-27 16:25:31 +00:00
|
|
|
void *impl_data;
|
2004-11-20 16:23:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct impl_libsamplerate {
|
2006-01-27 16:25:31 +00:00
|
|
|
float* buf1, *buf2, *buf3, *buf4;
|
|
|
|
|
unsigned buf1_samples, buf2_samples, buf3_samples, buf4_samples;
|
|
|
|
|
|
2004-11-20 16:23:53 +00:00
|
|
|
pa_convert_to_float32ne_func_t to_float32ne_func;
|
|
|
|
|
pa_convert_from_float32ne_func_t from_float32ne_func;
|
2004-07-02 18:47:03 +00:00
|
|
|
SRC_STATE *src_state;
|
2006-01-27 16:25:31 +00:00
|
|
|
|
|
|
|
|
int map_table[PA_CHANNELS_MAX][PA_CHANNELS_MAX];
|
|
|
|
|
int map_required;
|
2004-11-20 16:23:53 +00:00
|
|
|
};
|
2004-08-17 19:37:29 +00:00
|
|
|
|
2004-11-20 16:23:53 +00:00
|
|
|
struct impl_trivial {
|
|
|
|
|
unsigned o_counter;
|
|
|
|
|
unsigned i_counter;
|
2004-07-02 18:47:03 +00:00
|
|
|
};
|
|
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
static int libsamplerate_init(pa_resampler*r);
|
|
|
|
|
static int trivial_init(pa_resampler*r);
|
2004-11-20 16:23:53 +00:00
|
|
|
|
2006-01-27 16:25:31 +00:00
|
|
|
pa_resampler* pa_resampler_new(
|
|
|
|
|
const pa_sample_spec *a,
|
|
|
|
|
const pa_channel_map *am,
|
|
|
|
|
const pa_sample_spec *b,
|
|
|
|
|
const pa_channel_map *bm,
|
|
|
|
|
pa_memblock_stat *s,
|
|
|
|
|
pa_resample_method_t resample_method) {
|
|
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
pa_resampler *r = NULL;
|
2004-07-02 18:47:03 +00:00
|
|
|
|
2006-01-27 16:25:31 +00:00
|
|
|
assert(a);
|
|
|
|
|
assert(b);
|
|
|
|
|
assert(pa_sample_spec_valid(a));
|
|
|
|
|
assert(pa_sample_spec_valid(b));
|
|
|
|
|
assert(resample_method != PA_RESAMPLER_INVALID);
|
2004-07-02 18:47:03 +00:00
|
|
|
|
2006-01-27 16:25:31 +00:00
|
|
|
r = pa_xnew(pa_resampler, 1);
|
2004-11-20 16:23:53 +00:00
|
|
|
r->impl_data = NULL;
|
|
|
|
|
r->memblock_stat = s;
|
|
|
|
|
r->resample_method = resample_method;
|
2004-07-03 00:19:17 +00:00
|
|
|
|
2004-11-20 16:23:53 +00:00
|
|
|
r->impl_free = NULL;
|
2006-01-27 16:25:31 +00:00
|
|
|
r->impl_update_input_rate = NULL;
|
2004-11-20 16:23:53 +00:00
|
|
|
r->impl_run = NULL;
|
2004-07-02 18:47:03 +00:00
|
|
|
|
2004-11-20 16:23:53 +00:00
|
|
|
/* Fill sample specs */
|
2004-07-02 18:47:03 +00:00
|
|
|
r->i_ss = *a;
|
|
|
|
|
r->o_ss = *b;
|
|
|
|
|
|
2006-01-27 16:25:31 +00:00
|
|
|
if (am)
|
|
|
|
|
r->i_cm = *am;
|
|
|
|
|
else
|
2006-05-16 23:47:38 +00:00
|
|
|
pa_channel_map_init_auto(&r->i_cm, r->i_ss.channels, PA_CHANNEL_MAP_DEFAULT);
|
2006-01-27 16:25:31 +00:00
|
|
|
|
|
|
|
|
if (bm)
|
|
|
|
|
r->o_cm = *bm;
|
|
|
|
|
else
|
2006-05-16 23:47:38 +00:00
|
|
|
pa_channel_map_init_auto(&r->o_cm, r->o_ss.channels, PA_CHANNEL_MAP_DEFAULT);
|
2006-01-27 16:25:31 +00:00
|
|
|
|
2004-11-20 16:23:53 +00:00
|
|
|
r->i_fz = pa_frame_size(a);
|
|
|
|
|
r->o_fz = pa_frame_size(b);
|
2004-08-17 19:37:29 +00:00
|
|
|
|
2004-11-20 16:23:53 +00:00
|
|
|
/* Choose implementation */
|
2006-01-27 16:25:31 +00:00
|
|
|
if (a->channels != b->channels ||
|
|
|
|
|
a->format != b->format ||
|
|
|
|
|
!pa_channel_map_equal(&r->i_cm, &r->o_cm) ||
|
|
|
|
|
resample_method != PA_RESAMPLER_TRIVIAL) {
|
|
|
|
|
|
2004-11-20 16:23:53 +00:00
|
|
|
/* Use the libsamplerate based resampler for the complicated cases */
|
|
|
|
|
if (resample_method == PA_RESAMPLER_TRIVIAL)
|
|
|
|
|
r->resample_method = PA_RESAMPLER_SRC_ZERO_ORDER_HOLD;
|
|
|
|
|
|
|
|
|
|
if (libsamplerate_init(r) < 0)
|
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
/* Use our own simple non-fp resampler for the trivial cases and when the user selects it */
|
|
|
|
|
if (trivial_init(r) < 0)
|
|
|
|
|
goto fail;
|
|
|
|
|
}
|
2004-07-02 18:47:03 +00:00
|
|
|
|
|
|
|
|
return r;
|
|
|
|
|
|
|
|
|
|
fail:
|
|
|
|
|
if (r)
|
2004-08-04 16:39:30 +00:00
|
|
|
pa_xfree(r);
|
2004-07-02 18:47:03 +00:00
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
void pa_resampler_free(pa_resampler *r) {
|
2004-07-02 18:47:03 +00:00
|
|
|
assert(r);
|
2004-11-20 16:23:53 +00:00
|
|
|
|
|
|
|
|
if (r->impl_free)
|
|
|
|
|
r->impl_free(r);
|
|
|
|
|
|
2004-08-04 16:39:30 +00:00
|
|
|
pa_xfree(r);
|
2004-07-02 18:47:03 +00:00
|
|
|
}
|
|
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
void pa_resampler_set_input_rate(pa_resampler *r, uint32_t rate) {
|
2006-01-27 16:25:31 +00:00
|
|
|
assert(r);
|
|
|
|
|
assert(rate > 0);
|
2004-11-20 16:23:53 +00:00
|
|
|
|
2006-01-27 16:25:31 +00:00
|
|
|
if (r->i_ss.rate == rate)
|
|
|
|
|
return;
|
|
|
|
|
|
2004-11-20 16:23:53 +00:00
|
|
|
r->i_ss.rate = rate;
|
2006-01-27 16:25:31 +00:00
|
|
|
|
|
|
|
|
if (r->impl_update_input_rate)
|
|
|
|
|
r->impl_update_input_rate(r, rate);
|
2004-11-20 16:23:53 +00:00
|
|
|
}
|
|
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
void pa_resampler_run(pa_resampler *r, const pa_memchunk *in, pa_memchunk *out) {
|
2004-11-20 16:23:53 +00:00
|
|
|
assert(r && in && out && r->impl_run);
|
|
|
|
|
|
|
|
|
|
r->impl_run(r, in, out);
|
|
|
|
|
}
|
|
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
size_t pa_resampler_request(pa_resampler *r, size_t out_length) {
|
2006-05-13 21:18:32 +00:00
|
|
|
assert(r);
|
|
|
|
|
|
2004-11-20 16:23:53 +00:00
|
|
|
return (((out_length / r->o_fz)*r->i_ss.rate)/r->o_ss.rate) * r->i_fz;
|
|
|
|
|
}
|
|
|
|
|
|
2006-01-27 16:25:31 +00:00
|
|
|
pa_resample_method_t pa_resampler_get_method(pa_resampler *r) {
|
2004-11-20 16:23:53 +00:00
|
|
|
assert(r);
|
|
|
|
|
return r->resample_method;
|
|
|
|
|
}
|
|
|
|
|
|
2006-01-27 16:25:31 +00:00
|
|
|
static const char * const resample_methods[] = {
|
|
|
|
|
"src-sinc-best-quality",
|
|
|
|
|
"src-sinc-medium-quality",
|
|
|
|
|
"src-sinc-fastest",
|
|
|
|
|
"src-zero-order-hold",
|
|
|
|
|
"src-linear",
|
|
|
|
|
"trivial"
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const char *pa_resample_method_to_string(pa_resample_method_t m) {
|
|
|
|
|
|
|
|
|
|
if (m < 0 || m >= PA_RESAMPLER_MAX)
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
return resample_methods[m];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pa_resample_method_t pa_parse_resample_method(const char *string) {
|
|
|
|
|
pa_resample_method_t m;
|
|
|
|
|
|
2004-11-20 16:23:53 +00:00
|
|
|
assert(string);
|
|
|
|
|
|
2006-01-27 16:25:31 +00:00
|
|
|
for (m = 0; m < PA_RESAMPLER_MAX; m++)
|
|
|
|
|
if (!strcmp(string, resample_methods[m]))
|
|
|
|
|
return m;
|
|
|
|
|
|
|
|
|
|
return PA_RESAMPLER_INVALID;
|
2004-07-02 18:47:03 +00:00
|
|
|
}
|
|
|
|
|
|
2006-01-27 16:25:31 +00:00
|
|
|
|
2004-11-20 16:23:53 +00:00
|
|
|
/*** libsamplerate based implementation ***/
|
2004-07-02 18:47:03 +00:00
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
static void libsamplerate_free(pa_resampler *r) {
|
2006-01-27 16:25:31 +00:00
|
|
|
struct impl_libsamplerate *u;
|
|
|
|
|
|
|
|
|
|
assert(r);
|
|
|
|
|
assert(r->impl_data);
|
2004-11-20 16:23:53 +00:00
|
|
|
|
2006-01-27 16:25:31 +00:00
|
|
|
u = r->impl_data;
|
|
|
|
|
|
|
|
|
|
if (u->src_state)
|
|
|
|
|
src_delete(u->src_state);
|
|
|
|
|
|
|
|
|
|
pa_xfree(u->buf1);
|
|
|
|
|
pa_xfree(u->buf2);
|
|
|
|
|
pa_xfree(u->buf3);
|
|
|
|
|
pa_xfree(u->buf4);
|
|
|
|
|
pa_xfree(u);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void calc_map_table(pa_resampler *r) {
|
|
|
|
|
struct impl_libsamplerate *u;
|
|
|
|
|
unsigned oc;
|
|
|
|
|
assert(r);
|
|
|
|
|
assert(r->impl_data);
|
2004-11-20 16:23:53 +00:00
|
|
|
|
2006-01-27 16:25:31 +00:00
|
|
|
u = r->impl_data;
|
|
|
|
|
|
|
|
|
|
if (!(u->map_required = (!pa_channel_map_equal(&r->i_cm, &r->o_cm) || r->i_ss.channels != r->o_ss.channels)))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
for (oc = 0; oc < r->o_ss.channels; oc++) {
|
|
|
|
|
unsigned ic, i = 0;
|
|
|
|
|
|
|
|
|
|
for (ic = 0; ic < r->i_ss.channels; ic++) {
|
|
|
|
|
pa_channel_position_t a, b;
|
|
|
|
|
|
|
|
|
|
a = r->i_cm.map[ic];
|
|
|
|
|
b = r->o_cm.map[oc];
|
|
|
|
|
|
|
|
|
|
if (a == b ||
|
|
|
|
|
(a == PA_CHANNEL_POSITION_MONO && b == PA_CHANNEL_POSITION_LEFT) ||
|
|
|
|
|
(a == PA_CHANNEL_POSITION_MONO && b == PA_CHANNEL_POSITION_RIGHT) ||
|
|
|
|
|
(a == PA_CHANNEL_POSITION_LEFT && b == PA_CHANNEL_POSITION_MONO) ||
|
|
|
|
|
(a == PA_CHANNEL_POSITION_RIGHT && b == PA_CHANNEL_POSITION_MONO))
|
|
|
|
|
|
|
|
|
|
u->map_table[oc][i++] = ic;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Add an end marker */
|
|
|
|
|
if (i < PA_CHANNELS_MAX)
|
|
|
|
|
u->map_table[oc][i] = -1;
|
|
|
|
|
}
|
2004-11-20 16:23:53 +00:00
|
|
|
}
|
|
|
|
|
|
2006-04-15 15:25:53 +00:00
|
|
|
static float * convert_to_float(pa_resampler *r, void *input, unsigned n_frames) {
|
2006-01-27 16:25:31 +00:00
|
|
|
struct impl_libsamplerate *u;
|
|
|
|
|
unsigned n_samples;
|
2004-07-02 18:47:03 +00:00
|
|
|
|
2006-01-27 16:25:31 +00:00
|
|
|
assert(r);
|
|
|
|
|
assert(input);
|
|
|
|
|
assert(r->impl_data);
|
|
|
|
|
u = r->impl_data;
|
|
|
|
|
|
|
|
|
|
/* Convert the incoming sample into floats and place them in buf1 */
|
2004-07-02 18:47:03 +00:00
|
|
|
|
2006-01-27 16:25:31 +00:00
|
|
|
if (!u->to_float32ne_func)
|
|
|
|
|
return input;
|
|
|
|
|
|
|
|
|
|
n_samples = n_frames * r->i_ss.channels;
|
2004-09-10 22:35:12 +00:00
|
|
|
|
2006-01-27 16:25:31 +00:00
|
|
|
if (u->buf1_samples < n_samples)
|
|
|
|
|
u->buf1 = pa_xrealloc(u->buf1, sizeof(float) * (u->buf1_samples = n_samples));
|
2004-07-02 18:47:03 +00:00
|
|
|
|
2006-01-27 16:25:31 +00:00
|
|
|
u->to_float32ne_func(n_samples, input, u->buf1);
|
2004-09-10 22:35:12 +00:00
|
|
|
|
2006-01-27 16:25:31 +00:00
|
|
|
return u->buf1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static float *remap_channels(pa_resampler *r, float *input, unsigned n_frames) {
|
|
|
|
|
struct impl_libsamplerate *u;
|
|
|
|
|
unsigned n_samples;
|
|
|
|
|
int i_skip, o_skip;
|
|
|
|
|
unsigned oc;
|
2004-09-10 22:35:12 +00:00
|
|
|
|
2006-01-27 16:25:31 +00:00
|
|
|
assert(r);
|
|
|
|
|
assert(input);
|
|
|
|
|
assert(r->impl_data);
|
|
|
|
|
u = r->impl_data;
|
|
|
|
|
|
|
|
|
|
/* Remap channels and place the result int buf2 */
|
2004-07-02 18:47:03 +00:00
|
|
|
|
2006-01-27 16:25:31 +00:00
|
|
|
if (!u->map_required)
|
|
|
|
|
return input;
|
2004-07-02 18:47:03 +00:00
|
|
|
|
2006-01-27 16:25:31 +00:00
|
|
|
n_samples = n_frames * r->o_ss.channels;
|
2004-09-10 22:35:12 +00:00
|
|
|
|
2006-01-27 16:25:31 +00:00
|
|
|
if (u->buf2_samples < n_samples)
|
|
|
|
|
u->buf2 = pa_xrealloc(u->buf2, sizeof(float) * (u->buf2_samples = n_samples));
|
2004-09-10 22:35:12 +00:00
|
|
|
|
2006-01-27 16:25:31 +00:00
|
|
|
memset(u->buf2, 0, n_samples * sizeof(float));
|
2004-07-02 18:47:03 +00:00
|
|
|
|
2006-01-27 16:25:31 +00:00
|
|
|
o_skip = sizeof(float) * r->o_ss.channels;
|
|
|
|
|
i_skip = sizeof(float) * r->i_ss.channels;
|
|
|
|
|
|
|
|
|
|
for (oc = 0; oc < r->o_ss.channels; oc++) {
|
|
|
|
|
unsigned i;
|
|
|
|
|
static const float one = 1.0;
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < PA_CHANNELS_MAX && u->map_table[oc][i] >= 0; i++)
|
|
|
|
|
oil_vectoradd_f32(
|
|
|
|
|
u->buf2 + oc, o_skip,
|
|
|
|
|
u->buf2 + oc, o_skip,
|
|
|
|
|
input + u->map_table[oc][i], i_skip,
|
|
|
|
|
n_frames,
|
|
|
|
|
&one, &one);
|
|
|
|
|
}
|
2004-07-02 18:47:03 +00:00
|
|
|
|
2006-01-27 16:25:31 +00:00
|
|
|
return u->buf2;
|
|
|
|
|
}
|
2004-07-02 18:47:03 +00:00
|
|
|
|
2006-01-27 16:25:31 +00:00
|
|
|
static float *resample(pa_resampler *r, float *input, unsigned *n_frames) {
|
|
|
|
|
struct impl_libsamplerate *u;
|
|
|
|
|
SRC_DATA data;
|
|
|
|
|
unsigned out_n_frames, out_n_samples;
|
|
|
|
|
int ret;
|
2004-07-02 18:47:03 +00:00
|
|
|
|
2006-01-27 16:25:31 +00:00
|
|
|
assert(r);
|
|
|
|
|
assert(input);
|
|
|
|
|
assert(n_frames);
|
|
|
|
|
assert(r->impl_data);
|
|
|
|
|
u = r->impl_data;
|
|
|
|
|
|
|
|
|
|
/* Resample the data and place the result in buf3 */
|
|
|
|
|
|
|
|
|
|
if (!u->src_state)
|
|
|
|
|
return input;
|
|
|
|
|
|
|
|
|
|
out_n_frames = (*n_frames*r->o_ss.rate/r->i_ss.rate)+1024;
|
|
|
|
|
out_n_samples = out_n_frames * r->o_ss.channels;
|
|
|
|
|
|
|
|
|
|
if (u->buf3_samples < out_n_samples)
|
|
|
|
|
u->buf3 = pa_xrealloc(u->buf3, sizeof(float) * (u->buf3_samples = out_n_samples));
|
|
|
|
|
|
|
|
|
|
data.data_in = input;
|
|
|
|
|
data.input_frames = *n_frames;
|
|
|
|
|
|
|
|
|
|
data.data_out = u->buf3;
|
|
|
|
|
data.output_frames = out_n_frames;
|
2004-07-02 18:47:03 +00:00
|
|
|
|
2006-01-27 16:25:31 +00:00
|
|
|
data.src_ratio = (double) r->o_ss.rate / r->i_ss.rate;
|
|
|
|
|
data.end_of_input = 0;
|
2004-07-03 00:19:17 +00:00
|
|
|
|
2006-01-27 16:25:31 +00:00
|
|
|
ret = src_process(u->src_state, &data);
|
|
|
|
|
assert(ret == 0);
|
|
|
|
|
assert((unsigned) data.input_frames_used == *n_frames);
|
|
|
|
|
|
|
|
|
|
*n_frames = data.output_frames_gen;
|
|
|
|
|
|
|
|
|
|
return u->buf3;
|
|
|
|
|
}
|
|
|
|
|
|
2006-04-15 15:25:53 +00:00
|
|
|
static void *convert_from_float(pa_resampler *r, float *input, unsigned n_frames) {
|
2006-01-27 16:25:31 +00:00
|
|
|
struct impl_libsamplerate *u;
|
|
|
|
|
unsigned n_samples;
|
|
|
|
|
|
|
|
|
|
assert(r);
|
|
|
|
|
assert(input);
|
|
|
|
|
assert(r->impl_data);
|
|
|
|
|
u = r->impl_data;
|
|
|
|
|
|
|
|
|
|
/* Convert the data into the correct sample type and place the result in buf4 */
|
|
|
|
|
|
|
|
|
|
if (!u->from_float32ne_func)
|
|
|
|
|
return input;
|
|
|
|
|
|
|
|
|
|
n_samples = n_frames * r->o_ss.channels;
|
|
|
|
|
|
|
|
|
|
if (u->buf4_samples < n_samples)
|
|
|
|
|
u->buf4 = pa_xrealloc(u->buf4, sizeof(float) * (u->buf4_samples = n_samples));
|
|
|
|
|
|
|
|
|
|
u->from_float32ne_func(n_samples, input, u->buf4);
|
|
|
|
|
|
|
|
|
|
return u->buf4;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void libsamplerate_run(pa_resampler *r, const pa_memchunk *in, pa_memchunk *out) {
|
|
|
|
|
struct impl_libsamplerate *u;
|
2006-04-15 15:25:53 +00:00
|
|
|
float *buf;
|
|
|
|
|
void *input, *output;
|
2006-01-27 16:25:31 +00:00
|
|
|
unsigned n_frames;
|
|
|
|
|
|
|
|
|
|
assert(r);
|
|
|
|
|
assert(in);
|
|
|
|
|
assert(out);
|
|
|
|
|
assert(in->length);
|
|
|
|
|
assert(in->memblock);
|
|
|
|
|
assert(in->length % r->i_fz == 0);
|
|
|
|
|
assert(r->impl_data);
|
|
|
|
|
|
|
|
|
|
u = r->impl_data;
|
|
|
|
|
|
2006-04-15 15:25:53 +00:00
|
|
|
input = ((uint8_t*) in->memblock->data + in->index);
|
2006-01-27 16:25:31 +00:00
|
|
|
n_frames = in->length / r->i_fz;
|
|
|
|
|
assert(n_frames > 0);
|
|
|
|
|
|
2006-04-15 15:25:53 +00:00
|
|
|
buf = convert_to_float(r, input, n_frames);
|
2006-01-27 16:25:31 +00:00
|
|
|
buf = remap_channels(r, buf, n_frames);
|
|
|
|
|
buf = resample(r, buf, &n_frames);
|
|
|
|
|
|
|
|
|
|
if (n_frames) {
|
2006-04-15 15:25:53 +00:00
|
|
|
output = convert_from_float(r, buf, n_frames);
|
2006-01-27 16:25:31 +00:00
|
|
|
|
2006-04-15 15:25:53 +00:00
|
|
|
if (output == input) {
|
2006-01-27 16:25:31 +00:00
|
|
|
/* Mm, no adjustment has been necessary, so let's return the original block */
|
|
|
|
|
out->memblock = pa_memblock_ref(in->memblock);
|
|
|
|
|
out->index = in->index;
|
|
|
|
|
out->length = in->length;
|
|
|
|
|
} else {
|
|
|
|
|
float **p = NULL;
|
|
|
|
|
|
|
|
|
|
out->length = n_frames * r->o_fz;
|
|
|
|
|
out->index = 0;
|
|
|
|
|
|
2006-04-15 15:25:53 +00:00
|
|
|
if (output == u->buf1) {
|
2006-01-27 16:25:31 +00:00
|
|
|
p = &u->buf1;
|
|
|
|
|
u->buf1_samples = 0;
|
2006-04-15 15:25:53 +00:00
|
|
|
} else if (output == u->buf2) {
|
2006-01-27 16:25:31 +00:00
|
|
|
p = &u->buf2;
|
|
|
|
|
u->buf2_samples = 0;
|
2006-04-15 15:25:53 +00:00
|
|
|
} else if (output == u->buf3) {
|
2006-01-27 16:25:31 +00:00
|
|
|
p = &u->buf3;
|
|
|
|
|
u->buf3_samples = 0;
|
2006-04-15 15:25:53 +00:00
|
|
|
} else if (output == u->buf4) {
|
2006-01-27 16:25:31 +00:00
|
|
|
p = &u->buf4;
|
|
|
|
|
u->buf4_samples = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assert(p);
|
|
|
|
|
|
|
|
|
|
/* Take the existing buffer and make it a memblock */
|
|
|
|
|
out->memblock = pa_memblock_new_dynamic(*p, out->length, r->memblock_stat);
|
|
|
|
|
*p = NULL;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2004-09-10 22:35:12 +00:00
|
|
|
out->memblock = NULL;
|
2006-01-27 16:25:31 +00:00
|
|
|
out->index = out->length = 0;
|
2004-09-10 22:35:12 +00:00
|
|
|
}
|
2004-07-02 18:47:03 +00:00
|
|
|
}
|
2004-09-14 17:52:11 +00:00
|
|
|
|
2006-01-27 16:25:31 +00:00
|
|
|
static void libsamplerate_update_input_rate(pa_resampler *r, uint32_t rate) {
|
|
|
|
|
struct impl_libsamplerate *u;
|
|
|
|
|
|
|
|
|
|
assert(r);
|
|
|
|
|
assert(rate > 0);
|
|
|
|
|
assert(r->impl_data);
|
|
|
|
|
u = r->impl_data;
|
|
|
|
|
|
|
|
|
|
if (!u->src_state) {
|
|
|
|
|
int err;
|
|
|
|
|
u->src_state = src_new(r->resample_method, r->o_ss.channels, &err);
|
|
|
|
|
assert(u->src_state);
|
|
|
|
|
} else {
|
|
|
|
|
int ret = src_set_ratio(u->src_state, (double) r->o_ss.rate / rate);
|
|
|
|
|
assert(ret == 0);
|
|
|
|
|
}
|
2004-09-14 17:52:11 +00:00
|
|
|
}
|
2004-11-20 16:23:53 +00:00
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
static int libsamplerate_init(pa_resampler *r) {
|
2006-01-27 16:25:31 +00:00
|
|
|
struct impl_libsamplerate *u = NULL;
|
2004-11-20 16:23:53 +00:00
|
|
|
int err;
|
|
|
|
|
|
2006-01-27 16:25:31 +00:00
|
|
|
r->impl_data = u = pa_xnew(struct impl_libsamplerate, 1);
|
|
|
|
|
|
|
|
|
|
u->buf1 = u->buf2 = u->buf3 = u->buf4 = NULL;
|
|
|
|
|
u->buf1_samples = u->buf2_samples = u->buf3_samples = u->buf4_samples = 0;
|
2004-11-20 16:23:53 +00:00
|
|
|
|
2006-01-27 16:25:31 +00:00
|
|
|
if (r->i_ss.format == PA_SAMPLE_FLOAT32NE)
|
|
|
|
|
u->to_float32ne_func = NULL;
|
|
|
|
|
else if (!(u->to_float32ne_func = pa_get_convert_to_float32ne_function(r->i_ss.format)))
|
2004-11-20 16:23:53 +00:00
|
|
|
goto fail;
|
2006-01-27 16:25:31 +00:00
|
|
|
|
|
|
|
|
if (r->o_ss.format == PA_SAMPLE_FLOAT32NE)
|
|
|
|
|
u->from_float32ne_func = NULL;
|
|
|
|
|
else if (!(u->from_float32ne_func = pa_get_convert_from_float32ne_function(r->o_ss.format)))
|
2004-11-20 16:23:53 +00:00
|
|
|
goto fail;
|
|
|
|
|
|
2006-01-27 16:25:31 +00:00
|
|
|
if (r->o_ss.rate == r->i_ss.rate)
|
|
|
|
|
u->src_state = NULL;
|
|
|
|
|
else if (!(u->src_state = src_new(r->resample_method, r->o_ss.channels, &err)))
|
|
|
|
|
goto fail;
|
2004-11-20 16:23:53 +00:00
|
|
|
|
|
|
|
|
r->impl_free = libsamplerate_free;
|
2006-01-27 16:25:31 +00:00
|
|
|
r->impl_update_input_rate = libsamplerate_update_input_rate;
|
2004-11-20 16:23:53 +00:00
|
|
|
r->impl_run = libsamplerate_run;
|
2006-01-27 16:25:31 +00:00
|
|
|
|
|
|
|
|
calc_map_table(r);
|
2004-11-20 16:23:53 +00:00
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
fail:
|
2006-01-27 16:25:31 +00:00
|
|
|
pa_xfree(u);
|
2004-11-20 16:23:53 +00:00
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Trivial implementation */
|
|
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
static void trivial_run(pa_resampler *r, const pa_memchunk *in, pa_memchunk *out) {
|
2004-11-20 16:23:53 +00:00
|
|
|
size_t fz;
|
2006-01-27 16:25:31 +00:00
|
|
|
unsigned n_frames;
|
|
|
|
|
struct impl_trivial *u;
|
|
|
|
|
|
|
|
|
|
assert(r);
|
|
|
|
|
assert(in);
|
|
|
|
|
assert(out);
|
|
|
|
|
assert(r->impl_data);
|
|
|
|
|
|
|
|
|
|
u = r->impl_data;
|
2004-11-20 16:23:53 +00:00
|
|
|
|
|
|
|
|
fz = r->i_fz;
|
|
|
|
|
assert(fz == r->o_fz);
|
|
|
|
|
|
2006-01-27 16:25:31 +00:00
|
|
|
n_frames = in->length/fz;
|
2004-11-20 16:23:53 +00:00
|
|
|
|
|
|
|
|
if (r->i_ss.rate == r->o_ss.rate) {
|
|
|
|
|
|
|
|
|
|
/* In case there's no diefference in sample types, do nothing */
|
|
|
|
|
*out = *in;
|
2004-11-21 15:22:59 +00:00
|
|
|
pa_memblock_ref(out->memblock);
|
2004-11-20 16:23:53 +00:00
|
|
|
|
2006-01-27 16:25:31 +00:00
|
|
|
u->o_counter += n_frames;
|
2004-11-20 16:23:53 +00:00
|
|
|
} else {
|
|
|
|
|
/* Do real resampling */
|
|
|
|
|
size_t l;
|
|
|
|
|
unsigned o_index;
|
|
|
|
|
|
|
|
|
|
/* The length of the new memory block rounded up */
|
2006-01-27 16:25:31 +00:00
|
|
|
l = ((((n_frames+1) * r->o_ss.rate) / r->i_ss.rate) + 1) * fz;
|
2004-11-20 16:23:53 +00:00
|
|
|
|
|
|
|
|
out->index = 0;
|
|
|
|
|
out->memblock = pa_memblock_new(l, r->memblock_stat);
|
|
|
|
|
|
2006-01-27 16:25:31 +00:00
|
|
|
for (o_index = 0;; o_index++, u->o_counter++) {
|
2004-11-20 16:23:53 +00:00
|
|
|
unsigned j;
|
|
|
|
|
|
2006-01-27 16:25:31 +00:00
|
|
|
j = (u->o_counter * r->i_ss.rate / r->o_ss.rate);
|
|
|
|
|
j = j > u->i_counter ? j - u->i_counter : 0;
|
2004-11-20 16:23:53 +00:00
|
|
|
|
2006-01-27 16:25:31 +00:00
|
|
|
if (j >= n_frames)
|
2004-11-20 16:23:53 +00:00
|
|
|
break;
|
2004-11-27 18:50:29 +00:00
|
|
|
|
2004-11-20 16:23:53 +00:00
|
|
|
assert(o_index*fz < out->memblock->length);
|
|
|
|
|
|
|
|
|
|
memcpy((uint8_t*) out->memblock->data + fz*o_index,
|
2004-11-21 15:22:59 +00:00
|
|
|
(uint8_t*) in->memblock->data + in->index + fz*j, fz);
|
2004-11-20 16:23:53 +00:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
out->length = o_index*fz;
|
|
|
|
|
}
|
|
|
|
|
|
2006-01-27 16:25:31 +00:00
|
|
|
u->i_counter += n_frames;
|
2004-11-20 16:23:53 +00:00
|
|
|
|
2004-11-27 18:50:29 +00:00
|
|
|
/* Normalize counters */
|
2006-01-27 16:25:31 +00:00
|
|
|
while (u->i_counter >= r->i_ss.rate) {
|
|
|
|
|
u->i_counter -= r->i_ss.rate;
|
|
|
|
|
assert(u->o_counter >= r->o_ss.rate);
|
|
|
|
|
u->o_counter -= r->o_ss.rate;
|
2004-11-27 18:50:29 +00:00
|
|
|
}
|
2004-11-20 16:23:53 +00:00
|
|
|
}
|
|
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
static void trivial_free(pa_resampler *r) {
|
2004-11-20 16:23:53 +00:00
|
|
|
assert(r);
|
2006-01-27 16:25:31 +00:00
|
|
|
|
2004-11-20 16:23:53 +00:00
|
|
|
pa_xfree(r->impl_data);
|
|
|
|
|
}
|
|
|
|
|
|
2006-01-27 16:25:31 +00:00
|
|
|
static void trivial_update_input_rate(pa_resampler *r, uint32_t rate) {
|
|
|
|
|
struct impl_trivial *u;
|
|
|
|
|
|
|
|
|
|
assert(r);
|
|
|
|
|
assert(rate > 0);
|
|
|
|
|
assert(r->impl_data);
|
2004-11-20 16:23:53 +00:00
|
|
|
|
2006-01-27 16:25:31 +00:00
|
|
|
u = r->impl_data;
|
|
|
|
|
u->i_counter = 0;
|
|
|
|
|
u->o_counter = 0;
|
2004-11-20 16:23:53 +00:00
|
|
|
}
|
|
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
static int trivial_init(pa_resampler*r) {
|
2006-01-27 16:25:31 +00:00
|
|
|
struct impl_trivial *u;
|
|
|
|
|
|
|
|
|
|
assert(r);
|
|
|
|
|
assert(r->i_ss.format == r->o_ss.format);
|
|
|
|
|
assert(r->i_ss.channels == r->o_ss.channels);
|
2004-11-20 16:23:53 +00:00
|
|
|
|
2006-01-27 16:25:31 +00:00
|
|
|
r->impl_data = u = pa_xnew(struct impl_trivial, 1);
|
|
|
|
|
u->o_counter = u->i_counter = 0;
|
2004-11-20 16:23:53 +00:00
|
|
|
|
|
|
|
|
r->impl_run = trivial_run;
|
|
|
|
|
r->impl_free = trivial_free;
|
2006-01-27 16:25:31 +00:00
|
|
|
r->impl_update_input_rate = trivial_update_input_rate;
|
2004-11-20 16:23:53 +00:00
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|