resampler: Split the resampler implementations into separate files

Rebased by Peter Meerwald.

Signed-off-by: Peter Meerwald <pmeerw@pmeerw.net>
Signed-off-by: poljar (Damir Jelić) <poljarinho@gmail.com>
This commit is contained in:
poljar (Damir Jelić) 2014-08-04 14:40:12 +02:00 committed by Tanu Kaskinen
parent 64d17a6b0a
commit 72103e1e33
8 changed files with 748 additions and 604 deletions

View file

@ -0,0 +1,132 @@
/***
This file is part of PulseAudio.
Copyright 2004-2006 Lennart Poettering
PulseAudio is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation; either version 2.1 of the License,
or (at your option) any later version.
PulseAudio 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 Lesser General Public License
along with PulseAudio; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
USA.
***/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <pulse/xmalloc.h>
#include "pulsecore/ffmpeg/avcodec.h"
#include "pulsecore/resampler.h"
struct ffmpeg_data { /* data specific to ffmpeg */
struct AVResampleContext *state;
};
static unsigned ffmpeg_resample(pa_resampler *r, const pa_memchunk *input, unsigned in_n_frames, pa_memchunk *output, unsigned *out_n_frames) {
unsigned used_frames = 0, c;
int previous_consumed_frames = -1;
struct ffmpeg_data *ffmpeg_data;
pa_assert(r);
pa_assert(input);
pa_assert(output);
pa_assert(out_n_frames);
ffmpeg_data = r->impl.data;
for (c = 0; c < r->work_channels; c++) {
unsigned u;
pa_memblock *b, *w;
int16_t *p, *t, *k, *q, *s;
int consumed_frames;
/* Allocate a new block */
b = pa_memblock_new(r->mempool, in_n_frames * sizeof(int16_t));
p = pa_memblock_acquire(b);
/* Now copy the input data, splitting up channels */
t = (int16_t*) pa_memblock_acquire_chunk(input) + c;
k = p;
for (u = 0; u < in_n_frames; u++) {
*k = *t;
t += r->work_channels;
k ++;
}
pa_memblock_release(input->memblock);
/* Allocate buffer for the result */
w = pa_memblock_new(r->mempool, *out_n_frames * sizeof(int16_t));
q = pa_memblock_acquire(w);
/* Now, resample */
used_frames = (unsigned) av_resample(ffmpeg_data->state,
q, p,
&consumed_frames,
(int) in_n_frames, (int) *out_n_frames,
c >= (unsigned) (r->work_channels-1));
pa_memblock_release(b);
pa_memblock_unref(b);
pa_assert(consumed_frames <= (int) in_n_frames);
pa_assert(previous_consumed_frames == -1 || consumed_frames == previous_consumed_frames);
previous_consumed_frames = consumed_frames;
/* And place the results in the output buffer */
s = (int16_t *) pa_memblock_acquire_chunk(output) + c;
for (u = 0; u < used_frames; u++) {
*s = *q;
q++;
s += r->work_channels;
}
pa_memblock_release(output->memblock);
pa_memblock_release(w);
pa_memblock_unref(w);
}
*out_n_frames = used_frames;
return in_n_frames - previous_consumed_frames;
}
static void ffmpeg_free(pa_resampler *r) {
struct ffmpeg_data *ffmpeg_data;
pa_assert(r);
ffmpeg_data = r->impl.data;
if (ffmpeg_data->state)
av_resample_close(ffmpeg_data->state);
}
int pa_resampler_ffmpeg_init(pa_resampler *r) {
struct ffmpeg_data *ffmpeg_data;
pa_assert(r);
ffmpeg_data = pa_xnew(struct ffmpeg_data, 1);
/* We could probably implement different quality levels by
* adjusting the filter parameters here. However, ffmpeg
* internally only uses these hardcoded values, so let's use them
* here for now as well until ffmpeg makes this configurable. */
if (!(ffmpeg_data->state = av_resample_init((int) r->o_ss.rate, (int) r->i_ss.rate, 16, 10, 0, 0.8)))
return -1;
r->impl.free = ffmpeg_free;
r->impl.resample = ffmpeg_resample;
r->impl.data = (void *) ffmpeg_data;
return 0;
}

View file

@ -0,0 +1,102 @@
/***
This file is part of PulseAudio.
Copyright 2004-2006 Lennart Poettering
PulseAudio is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation; either version 2.1 of the License,
or (at your option) any later version.
PulseAudio 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 Lesser General Public License
along with PulseAudio; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
USA.
***/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <samplerate.h>
#include "pulsecore/resampler.h"
static unsigned libsamplerate_resample(pa_resampler *r, const pa_memchunk *input, unsigned in_n_frames, pa_memchunk *output, unsigned *out_n_frames) {
SRC_DATA data;
SRC_STATE *state;
pa_assert(r);
pa_assert(input);
pa_assert(output);
pa_assert(out_n_frames);
state = r->impl.data;
memset(&data, 0, sizeof(data));
data.data_in = pa_memblock_acquire_chunk(input);
data.input_frames = (long int) in_n_frames;
data.data_out = pa_memblock_acquire_chunk(output);
data.output_frames = (long int) *out_n_frames;
data.src_ratio = (double) r->o_ss.rate / r->i_ss.rate;
data.end_of_input = 0;
pa_assert_se(src_process(state, &data) == 0);
pa_memblock_release(input->memblock);
pa_memblock_release(output->memblock);
*out_n_frames = (unsigned) data.output_frames_gen;
return in_n_frames - data.input_frames_used;
}
static void libsamplerate_update_rates(pa_resampler *r) {
SRC_STATE *state;
pa_assert(r);
state = r->impl.data;
pa_assert_se(src_set_ratio(state, (double) r->o_ss.rate / r->i_ss.rate) == 0);
}
static void libsamplerate_reset(pa_resampler *r) {
SRC_STATE *state;
pa_assert(r);
state = r->impl.data;
pa_assert_se(src_reset(state) == 0);
}
static void libsamplerate_free(pa_resampler *r) {
SRC_STATE *state;
pa_assert(r);
state = r->impl.data;
if (state)
src_delete(state);
}
int pa_resampler_libsamplerate_init(pa_resampler *r) {
int err;
SRC_STATE *state;
pa_assert(r);
if (!(state = src_new(r->method, r->work_channels, &err)))
return -1;
r->impl.free = libsamplerate_free;
r->impl.update_rates = libsamplerate_update_rates;
r->impl.resample = libsamplerate_resample;
r->impl.reset = libsamplerate_reset;
r->impl.data = state;
return 0;
}

View file

@ -0,0 +1,163 @@
/***
This file is part of PulseAudio.
Copyright 2004-2006 Lennart Poettering
PulseAudio is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation; either version 2.1 of the License,
or (at your option) any later version.
PulseAudio 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 Lesser General Public License
along with PulseAudio; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
USA.
***/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <pulse/xmalloc.h>
#include <math.h>
#include "pulsecore/resampler.h"
struct peaks_data { /* data specific to the peak finder pseudo resampler */
unsigned o_counter;
unsigned i_counter;
float max_f[PA_CHANNELS_MAX];
int16_t max_i[PA_CHANNELS_MAX];
};
static unsigned peaks_resample(pa_resampler *r, const pa_memchunk *input, unsigned in_n_frames, pa_memchunk *output, unsigned *out_n_frames) {
unsigned c, o_index = 0;
unsigned i, i_end = 0;
void *src, *dst;
struct peaks_data *peaks_data;
pa_assert(r);
pa_assert(input);
pa_assert(output);
pa_assert(out_n_frames);
peaks_data = r->impl.data;
src = pa_memblock_acquire_chunk(input);
dst = pa_memblock_acquire_chunk(output);
i = ((uint64_t) peaks_data->o_counter * r->i_ss.rate) / r->o_ss.rate;
i = i > peaks_data->i_counter ? i - peaks_data->i_counter : 0;
while (i_end < in_n_frames) {
i_end = ((uint64_t) (peaks_data->o_counter + 1) * r->i_ss.rate) / r->o_ss.rate;
i_end = i_end > peaks_data->i_counter ? i_end - peaks_data->i_counter : 0;
pa_assert_fp(o_index * r->w_fz < pa_memblock_get_length(output->memblock));
/* 1ch float is treated separately, because that is the common case */
if (r->work_channels == 1 && r->work_format == PA_SAMPLE_FLOAT32NE) {
float *s = (float*) src + i;
float *d = (float*) dst + o_index;
for (; i < i_end && i < in_n_frames; i++) {
float n = fabsf(*s++);
if (n > peaks_data->max_f[0])
peaks_data->max_f[0] = n;
}
if (i == i_end) {
*d = peaks_data->max_f[0];
peaks_data->max_f[0] = 0;
o_index++, peaks_data->o_counter++;
}
} else if (r->work_format == PA_SAMPLE_S16NE) {
int16_t *s = (int16_t*) src + r->work_channels * i;
int16_t *d = (int16_t*) dst + r->work_channels * o_index;
for (; i < i_end && i < in_n_frames; i++)
for (c = 0; c < r->work_channels; c++) {
int16_t n = abs(*s++);
if (n > peaks_data->max_i[c])
peaks_data->max_i[c] = n;
}
if (i == i_end) {
for (c = 0; c < r->work_channels; c++, d++) {
*d = peaks_data->max_i[c];
peaks_data->max_i[c] = 0;
}
o_index++, peaks_data->o_counter++;
}
} else {
float *s = (float*) src + r->work_channels * i;
float *d = (float*) dst + r->work_channels * o_index;
for (; i < i_end && i < in_n_frames; i++)
for (c = 0; c < r->work_channels; c++) {
float n = fabsf(*s++);
if (n > peaks_data->max_f[c])
peaks_data->max_f[c] = n;
}
if (i == i_end) {
for (c = 0; c < r->work_channels; c++, d++) {
*d = peaks_data->max_f[c];
peaks_data->max_f[c] = 0;
}
o_index++, peaks_data->o_counter++;
}
}
}
pa_memblock_release(input->memblock);
pa_memblock_release(output->memblock);
*out_n_frames = o_index;
peaks_data->i_counter += in_n_frames;
/* Normalize counters */
while (peaks_data->i_counter >= r->i_ss.rate) {
pa_assert(peaks_data->o_counter >= r->o_ss.rate);
peaks_data->i_counter -= r->i_ss.rate;
peaks_data->o_counter -= r->o_ss.rate;
}
return 0;
}
static void peaks_update_rates_or_reset(pa_resampler *r) {
struct peaks_data *peaks_data;
pa_assert(r);
peaks_data = r->impl.data;
peaks_data->i_counter = 0;
peaks_data->o_counter = 0;
}
int pa_resampler_peaks_init(pa_resampler*r) {
struct peaks_data *peaks_data;
pa_assert(r);
pa_assert(r->i_ss.rate >= r->o_ss.rate);
pa_assert(r->work_format == PA_SAMPLE_S16NE || r->work_format == PA_SAMPLE_FLOAT32NE);
peaks_data = pa_xnew0(struct peaks_data, 1);
r->impl.resample = peaks_resample;
r->impl.update_rates = peaks_update_rates_or_reset;
r->impl.reset = peaks_update_rates_or_reset;
r->impl.data = peaks_data;
return 0;
}

View file

@ -0,0 +1,151 @@
/***
This file is part of PulseAudio.
Copyright 2004-2006 Lennart Poettering
PulseAudio is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation; either version 2.1 of the License,
or (at your option) any later version.
PulseAudio 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 Lesser General Public License
along with PulseAudio; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
USA.
***/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <speex/speex_resampler.h>
#include "pulsecore/resampler.h"
static unsigned speex_resample_float(pa_resampler *r, const pa_memchunk *input, unsigned in_n_frames, pa_memchunk *output, unsigned *out_n_frames) {
float *in, *out;
uint32_t inf = in_n_frames, outf = *out_n_frames;
SpeexResamplerState *state;
pa_assert(r);
pa_assert(input);
pa_assert(output);
pa_assert(out_n_frames);
state = r->impl.data;
in = pa_memblock_acquire_chunk(input);
out = pa_memblock_acquire_chunk(output);
/* Strictly speaking, speex resampler expects its input
* to be normalized to the [-32768.0 .. 32767.0] range.
* This matters if speex has been compiled with --enable-fixed-point,
* because such speex will round the samples to the nearest
* integer. speex with --enable-fixed-point is therefore incompatible
* with PulseAudio's floating-point sample range [-1 .. 1]. speex
* without --enable-fixed-point works fine with this range.
* Care has been taken to call speex_resample_float() only
* for speex compiled without --enable-fixed-point.
*/
pa_assert_se(speex_resampler_process_interleaved_float(state, in, &inf, out, &outf) == 0);
pa_memblock_release(input->memblock);
pa_memblock_release(output->memblock);
pa_assert(inf == in_n_frames);
*out_n_frames = outf;
return 0;
}
static unsigned speex_resample_int(pa_resampler *r, const pa_memchunk *input, unsigned in_n_frames, pa_memchunk *output, unsigned *out_n_frames) {
int16_t *in, *out;
uint32_t inf = in_n_frames, outf = *out_n_frames;
SpeexResamplerState *state;
pa_assert(r);
pa_assert(input);
pa_assert(output);
pa_assert(out_n_frames);
state = r->impl.data;
in = pa_memblock_acquire_chunk(input);
out = pa_memblock_acquire_chunk(output);
pa_assert_se(speex_resampler_process_interleaved_int(state, in, &inf, out, &outf) == 0);
pa_memblock_release(input->memblock);
pa_memblock_release(output->memblock);
pa_assert(inf == in_n_frames);
*out_n_frames = outf;
return 0;
}
static void speex_update_rates(pa_resampler *r) {
SpeexResamplerState *state;
pa_assert(r);
state = r->impl.data;
pa_assert_se(speex_resampler_set_rate(state, r->i_ss.rate, r->o_ss.rate) == 0);
}
static void speex_reset(pa_resampler *r) {
SpeexResamplerState *state;
pa_assert(r);
state = r->impl.data;
pa_assert_se(speex_resampler_reset_mem(state) == 0);
}
static void speex_free(pa_resampler *r) {
SpeexResamplerState *state;
pa_assert(r);
state = r->impl.data;
if (!state)
return;
speex_resampler_destroy(state);
}
int pa_resampler_speex_init(pa_resampler *r) {
int q, err;
SpeexResamplerState *state;
pa_assert(r);
r->impl.free = speex_free;
r->impl.update_rates = speex_update_rates;
r->impl.reset = speex_reset;
if (r->method >= PA_RESAMPLER_SPEEX_FIXED_BASE && r->method <= PA_RESAMPLER_SPEEX_FIXED_MAX) {
q = r->method - PA_RESAMPLER_SPEEX_FIXED_BASE;
r->impl.resample = speex_resample_int;
} else {
pa_assert(r->method >= PA_RESAMPLER_SPEEX_FLOAT_BASE && r->method <= PA_RESAMPLER_SPEEX_FLOAT_MAX);
q = r->method - PA_RESAMPLER_SPEEX_FLOAT_BASE;
r->impl.resample = speex_resample_float;
}
pa_log_info("Choosing speex quality setting %i.", q);
if (!(state = speex_resampler_init(r->work_channels, r->i_ss.rate, r->o_ss.rate, q, &err)))
return -1;
r->impl.data = state;
return 0;
}

View file

@ -0,0 +1,102 @@
/***
This file is part of PulseAudio.
Copyright 2004-2006 Lennart Poettering
PulseAudio is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation; either version 2.1 of the License,
or (at your option) any later version.
PulseAudio 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 Lesser General Public License
along with PulseAudio; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
USA.
***/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <pulse/xmalloc.h>
#include "pulsecore/resampler.h"
struct trivial_data { /* data specific to the trivial resampler */
unsigned o_counter;
unsigned i_counter;
};
static unsigned trivial_resample(pa_resampler *r, const pa_memchunk *input, unsigned in_n_frames, pa_memchunk *output, unsigned *out_n_frames) {
unsigned i_index, o_index;
void *src, *dst;
struct trivial_data *trivial_data;
pa_assert(r);
pa_assert(input);
pa_assert(output);
pa_assert(out_n_frames);
trivial_data = r->impl.data;
src = pa_memblock_acquire_chunk(input);
dst = pa_memblock_acquire_chunk(output);
for (o_index = 0;; o_index++, trivial_data->o_counter++) {
i_index = ((uint64_t) trivial_data->o_counter * r->i_ss.rate) / r->o_ss.rate;
i_index = i_index > trivial_data->i_counter ? i_index - trivial_data->i_counter : 0;
if (i_index >= in_n_frames)
break;
pa_assert_fp(o_index * r->w_fz < pa_memblock_get_length(output->memblock));
memcpy((uint8_t*) dst + r->w_fz * o_index, (uint8_t*) src + r->w_fz * i_index, (int) r->w_fz);
}
pa_memblock_release(input->memblock);
pa_memblock_release(output->memblock);
*out_n_frames = o_index;
trivial_data->i_counter += in_n_frames;
/* Normalize counters */
while (trivial_data->i_counter >= r->i_ss.rate) {
pa_assert(trivial_data->o_counter >= r->o_ss.rate);
trivial_data->i_counter -= r->i_ss.rate;
trivial_data->o_counter -= r->o_ss.rate;
}
return 0;
}
static void trivial_update_rates_or_reset(pa_resampler *r) {
struct trivial_data *trivial_data;
pa_assert(r);
trivial_data = r->impl.data;
trivial_data->i_counter = 0;
trivial_data->o_counter = 0;
}
int pa_resampler_trivial_init(pa_resampler *r) {
struct trivial_data *trivial_data;
pa_assert(r);
trivial_data = pa_xnew0(struct trivial_data, 1);
r->impl.resample = trivial_resample;
r->impl.update_rates = trivial_update_rates_or_reset;
r->impl.reset = trivial_update_rates_or_reset;
r->impl.data = trivial_data;
return 0;
}