2004-07-16 19:56:36 +00:00
|
|
|
/***
|
2006-06-19 21:53:48 +00:00
|
|
|
This file is part of PulseAudio.
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-02-13 15:35:19 +00:00
|
|
|
Copyright 2004-2006 Lennart Poettering
|
|
|
|
|
|
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
|
2009-03-03 20:23:02 +00:00
|
|
|
by the Free Software Foundation; either version 2.1 of the License,
|
2004-07-16 19:56:36 +00:00
|
|
|
or (at your option) any later version.
|
2007-01-04 13:43:45 +00:00
|
|
|
|
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.
|
2007-01-04 13:43:45 +00:00
|
|
|
|
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-11-20 16:23:53 +00:00
|
|
|
#include <string.h>
|
2004-07-02 18:47:03 +00:00
|
|
|
|
2009-01-22 23:38:07 +01:00
|
|
|
#ifdef HAVE_LIBSAMPLERATE
|
2004-07-02 18:47:03 +00:00
|
|
|
#include <samplerate.h>
|
2007-10-28 19:13:50 +00:00
|
|
|
#endif
|
|
|
|
|
|
2011-12-11 16:07:41 +01:00
|
|
|
#ifdef HAVE_SPEEX
|
2008-06-28 02:20:14 +02:00
|
|
|
#include <speex/speex_resampler.h>
|
2011-12-11 16:07:41 +01:00
|
|
|
#endif
|
2008-06-28 02:20:14 +02:00
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
#include <pulse/xmalloc.h>
|
|
|
|
|
#include <pulsecore/sconv.h>
|
|
|
|
|
#include <pulsecore/log.h>
|
2007-10-28 19:13:50 +00:00
|
|
|
#include <pulsecore/macro.h>
|
2007-11-11 02:30:59 +00:00
|
|
|
#include <pulsecore/strbuf.h>
|
2011-03-09 10:00:20 +01:00
|
|
|
#include <pulsecore/remap.h>
|
2012-06-06 01:28:14 +05:30
|
|
|
#include <pulsecore/core-util.h>
|
2007-10-28 19:13:50 +00:00
|
|
|
#include "ffmpeg/avcodec.h"
|
2006-02-17 12:10:58 +00:00
|
|
|
|
2004-07-02 18:47:03 +00:00
|
|
|
#include "resampler.h"
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
/* Number of samples of extra space we allow the resamplers to return */
|
2008-05-15 23:34:41 +00:00
|
|
|
#define EXTRA_FRAMES 128
|
2007-10-28 19:13:50 +00:00
|
|
|
|
2004-07-03 23:35:12 +00:00
|
|
|
struct pa_resampler {
|
2007-11-11 02:30:59 +00:00
|
|
|
pa_resample_method_t method;
|
|
|
|
|
pa_resample_flags_t flags;
|
|
|
|
|
|
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;
|
2007-10-28 19:13:50 +00:00
|
|
|
size_t i_fz, o_fz, w_sz;
|
2006-08-18 19:55:18 +00:00
|
|
|
pa_mempool *mempool;
|
2004-07-02 18:47:03 +00:00
|
|
|
|
2012-05-10 09:19:22 +03:00
|
|
|
pa_memchunk to_work_format_buf;
|
|
|
|
|
pa_memchunk remap_buf;
|
|
|
|
|
pa_memchunk resample_buf;
|
|
|
|
|
pa_memchunk from_work_format_buf;
|
|
|
|
|
unsigned to_work_format_buf_samples;
|
2012-05-10 09:19:23 +03:00
|
|
|
size_t remap_buf_size;
|
2012-05-10 09:19:22 +03:00
|
|
|
unsigned resample_buf_samples;
|
|
|
|
|
unsigned from_work_format_buf_samples;
|
2013-02-07 14:03:15 +01:00
|
|
|
bool remap_buf_contains_leftover_data;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_sample_format_t work_format;
|
|
|
|
|
|
|
|
|
|
pa_convert_func_t to_work_format_func;
|
|
|
|
|
pa_convert_func_t from_work_format_func;
|
2006-01-27 16:25:31 +00:00
|
|
|
|
2009-08-20 17:54:45 +02:00
|
|
|
pa_remap_t remap;
|
2013-02-07 14:03:15 +01:00
|
|
|
bool map_required;
|
2004-08-17 19:37:29 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
void (*impl_free)(pa_resampler *r);
|
|
|
|
|
void (*impl_update_rates)(pa_resampler *r);
|
|
|
|
|
void (*impl_resample)(pa_resampler *r, const pa_memchunk *in, unsigned in_samples, pa_memchunk *out, unsigned *out_samples);
|
2007-12-23 20:15:03 +00:00
|
|
|
void (*impl_reset)(pa_resampler *r);
|
2013-06-18 14:22:26 +02:00
|
|
|
void *impl_data;
|
|
|
|
|
};
|
2007-10-28 19:13:50 +00:00
|
|
|
|
2013-06-18 14:22:26 +02:00
|
|
|
struct trivial_data { /* data specific to the trivial resampler */
|
|
|
|
|
unsigned o_counter;
|
|
|
|
|
unsigned i_counter;
|
|
|
|
|
};
|
2008-05-15 23:34:41 +00:00
|
|
|
|
2013-06-18 14:22:26 +02:00
|
|
|
struct peaks_data { /* data specific to the peak finder pseudo resampler */
|
|
|
|
|
unsigned o_counter;
|
|
|
|
|
unsigned i_counter;
|
2008-05-15 23:34:41 +00:00
|
|
|
|
2013-06-18 14:22:26 +02:00
|
|
|
float max_f[PA_CHANNELS_MAX];
|
|
|
|
|
int16_t max_i[PA_CHANNELS_MAX];
|
|
|
|
|
};
|
2008-05-15 23:34:41 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
#ifdef HAVE_LIBSAMPLERATE
|
2013-06-18 14:22:26 +02:00
|
|
|
struct src_data { /* data specific to libsamplerate */
|
|
|
|
|
SRC_STATE *state;
|
|
|
|
|
};
|
2007-10-28 19:13:50 +00:00
|
|
|
#endif
|
|
|
|
|
|
2011-12-11 16:07:41 +01:00
|
|
|
#ifdef HAVE_SPEEX
|
2013-06-18 14:22:26 +02:00
|
|
|
struct speex_data { /* data specific to speex */
|
|
|
|
|
SpeexResamplerState* state;
|
|
|
|
|
};
|
2011-12-11 16:07:41 +01:00
|
|
|
#endif
|
2007-10-28 19:13:50 +00:00
|
|
|
|
2013-06-18 14:22:26 +02:00
|
|
|
struct ffmpeg_data { /* data specific to ffmpeg */
|
|
|
|
|
struct AVResampleContext *state;
|
|
|
|
|
pa_memchunk buf[PA_CHANNELS_MAX];
|
2004-07-02 18:47:03 +00:00
|
|
|
};
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
static int copy_init(pa_resampler *r);
|
2006-01-11 01:17:39 +00:00
|
|
|
static int trivial_init(pa_resampler*r);
|
2011-12-11 16:07:41 +01:00
|
|
|
#ifdef HAVE_SPEEX
|
2007-10-28 19:13:50 +00:00
|
|
|
static int speex_init(pa_resampler*r);
|
2011-12-11 16:07:41 +01:00
|
|
|
#endif
|
2007-10-28 19:13:50 +00:00
|
|
|
static int ffmpeg_init(pa_resampler*r);
|
2008-05-15 23:34:41 +00:00
|
|
|
static int peaks_init(pa_resampler*r);
|
2007-10-28 19:13:50 +00:00
|
|
|
#ifdef HAVE_LIBSAMPLERATE
|
|
|
|
|
static int libsamplerate_init(pa_resampler*r);
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
static void calc_map_table(pa_resampler *r);
|
|
|
|
|
|
|
|
|
|
static int (* const init_table[])(pa_resampler*r) = {
|
|
|
|
|
#ifdef HAVE_LIBSAMPLERATE
|
|
|
|
|
[PA_RESAMPLER_SRC_SINC_BEST_QUALITY] = libsamplerate_init,
|
|
|
|
|
[PA_RESAMPLER_SRC_SINC_MEDIUM_QUALITY] = libsamplerate_init,
|
|
|
|
|
[PA_RESAMPLER_SRC_SINC_FASTEST] = libsamplerate_init,
|
|
|
|
|
[PA_RESAMPLER_SRC_ZERO_ORDER_HOLD] = libsamplerate_init,
|
|
|
|
|
[PA_RESAMPLER_SRC_LINEAR] = libsamplerate_init,
|
|
|
|
|
#else
|
|
|
|
|
[PA_RESAMPLER_SRC_SINC_BEST_QUALITY] = NULL,
|
|
|
|
|
[PA_RESAMPLER_SRC_SINC_MEDIUM_QUALITY] = NULL,
|
|
|
|
|
[PA_RESAMPLER_SRC_SINC_FASTEST] = NULL,
|
|
|
|
|
[PA_RESAMPLER_SRC_ZERO_ORDER_HOLD] = NULL,
|
|
|
|
|
[PA_RESAMPLER_SRC_LINEAR] = NULL,
|
|
|
|
|
#endif
|
|
|
|
|
[PA_RESAMPLER_TRIVIAL] = trivial_init,
|
2011-12-11 16:07:41 +01:00
|
|
|
#ifdef HAVE_SPEEX
|
2007-10-28 19:13:50 +00:00
|
|
|
[PA_RESAMPLER_SPEEX_FLOAT_BASE+0] = speex_init,
|
|
|
|
|
[PA_RESAMPLER_SPEEX_FLOAT_BASE+1] = speex_init,
|
|
|
|
|
[PA_RESAMPLER_SPEEX_FLOAT_BASE+2] = speex_init,
|
|
|
|
|
[PA_RESAMPLER_SPEEX_FLOAT_BASE+3] = speex_init,
|
|
|
|
|
[PA_RESAMPLER_SPEEX_FLOAT_BASE+4] = speex_init,
|
|
|
|
|
[PA_RESAMPLER_SPEEX_FLOAT_BASE+5] = speex_init,
|
|
|
|
|
[PA_RESAMPLER_SPEEX_FLOAT_BASE+6] = speex_init,
|
|
|
|
|
[PA_RESAMPLER_SPEEX_FLOAT_BASE+7] = speex_init,
|
|
|
|
|
[PA_RESAMPLER_SPEEX_FLOAT_BASE+8] = speex_init,
|
|
|
|
|
[PA_RESAMPLER_SPEEX_FLOAT_BASE+9] = speex_init,
|
|
|
|
|
[PA_RESAMPLER_SPEEX_FLOAT_BASE+10] = speex_init,
|
|
|
|
|
[PA_RESAMPLER_SPEEX_FIXED_BASE+0] = speex_init,
|
|
|
|
|
[PA_RESAMPLER_SPEEX_FIXED_BASE+1] = speex_init,
|
|
|
|
|
[PA_RESAMPLER_SPEEX_FIXED_BASE+2] = speex_init,
|
|
|
|
|
[PA_RESAMPLER_SPEEX_FIXED_BASE+3] = speex_init,
|
|
|
|
|
[PA_RESAMPLER_SPEEX_FIXED_BASE+4] = speex_init,
|
|
|
|
|
[PA_RESAMPLER_SPEEX_FIXED_BASE+5] = speex_init,
|
|
|
|
|
[PA_RESAMPLER_SPEEX_FIXED_BASE+6] = speex_init,
|
|
|
|
|
[PA_RESAMPLER_SPEEX_FIXED_BASE+7] = speex_init,
|
|
|
|
|
[PA_RESAMPLER_SPEEX_FIXED_BASE+8] = speex_init,
|
|
|
|
|
[PA_RESAMPLER_SPEEX_FIXED_BASE+9] = speex_init,
|
|
|
|
|
[PA_RESAMPLER_SPEEX_FIXED_BASE+10] = speex_init,
|
2011-12-11 16:07:41 +01:00
|
|
|
#else
|
|
|
|
|
[PA_RESAMPLER_SPEEX_FLOAT_BASE+0] = NULL,
|
|
|
|
|
[PA_RESAMPLER_SPEEX_FLOAT_BASE+1] = NULL,
|
|
|
|
|
[PA_RESAMPLER_SPEEX_FLOAT_BASE+2] = NULL,
|
|
|
|
|
[PA_RESAMPLER_SPEEX_FLOAT_BASE+3] = NULL,
|
|
|
|
|
[PA_RESAMPLER_SPEEX_FLOAT_BASE+4] = NULL,
|
|
|
|
|
[PA_RESAMPLER_SPEEX_FLOAT_BASE+5] = NULL,
|
|
|
|
|
[PA_RESAMPLER_SPEEX_FLOAT_BASE+6] = NULL,
|
|
|
|
|
[PA_RESAMPLER_SPEEX_FLOAT_BASE+7] = NULL,
|
|
|
|
|
[PA_RESAMPLER_SPEEX_FLOAT_BASE+8] = NULL,
|
|
|
|
|
[PA_RESAMPLER_SPEEX_FLOAT_BASE+9] = NULL,
|
|
|
|
|
[PA_RESAMPLER_SPEEX_FLOAT_BASE+10] = NULL,
|
|
|
|
|
[PA_RESAMPLER_SPEEX_FIXED_BASE+0] = NULL,
|
|
|
|
|
[PA_RESAMPLER_SPEEX_FIXED_BASE+1] = NULL,
|
|
|
|
|
[PA_RESAMPLER_SPEEX_FIXED_BASE+2] = NULL,
|
|
|
|
|
[PA_RESAMPLER_SPEEX_FIXED_BASE+3] = NULL,
|
|
|
|
|
[PA_RESAMPLER_SPEEX_FIXED_BASE+4] = NULL,
|
|
|
|
|
[PA_RESAMPLER_SPEEX_FIXED_BASE+5] = NULL,
|
|
|
|
|
[PA_RESAMPLER_SPEEX_FIXED_BASE+6] = NULL,
|
|
|
|
|
[PA_RESAMPLER_SPEEX_FIXED_BASE+7] = NULL,
|
|
|
|
|
[PA_RESAMPLER_SPEEX_FIXED_BASE+8] = NULL,
|
|
|
|
|
[PA_RESAMPLER_SPEEX_FIXED_BASE+9] = NULL,
|
|
|
|
|
[PA_RESAMPLER_SPEEX_FIXED_BASE+10] = NULL,
|
|
|
|
|
#endif
|
2007-10-28 19:13:50 +00:00
|
|
|
[PA_RESAMPLER_FFMPEG] = ffmpeg_init,
|
|
|
|
|
[PA_RESAMPLER_AUTO] = NULL,
|
2008-05-15 23:34:41 +00:00
|
|
|
[PA_RESAMPLER_COPY] = copy_init,
|
|
|
|
|
[PA_RESAMPLER_PEAKS] = peaks_init,
|
2007-10-28 19:13:50 +00:00
|
|
|
};
|
|
|
|
|
|
2013-06-26 13:51:55 +02:00
|
|
|
static pa_resample_method_t pa_resampler_fix_method(
|
|
|
|
|
pa_resample_flags_t flags,
|
|
|
|
|
pa_resample_method_t method,
|
|
|
|
|
const uint32_t rate_a,
|
|
|
|
|
const uint32_t rate_b) {
|
|
|
|
|
|
|
|
|
|
pa_assert(rate_a > 0 && rate_a <= PA_RATE_MAX);
|
|
|
|
|
pa_assert(rate_b > 0 && rate_b <= PA_RATE_MAX);
|
2007-11-11 02:30:59 +00:00
|
|
|
pa_assert(method >= 0);
|
|
|
|
|
pa_assert(method < PA_RESAMPLER_MAX);
|
2007-10-28 19:13:50 +00:00
|
|
|
|
2013-06-26 13:51:55 +02:00
|
|
|
if (!(flags & PA_RESAMPLER_VARIABLE_RATE) && rate_a == rate_b) {
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_log_info("Forcing resampler 'copy', because of fixed, identical sample rates.");
|
2007-11-11 02:30:59 +00:00
|
|
|
method = PA_RESAMPLER_COPY;
|
2007-10-28 19:13:50 +00:00
|
|
|
}
|
|
|
|
|
|
2007-11-11 02:30:59 +00:00
|
|
|
if (!pa_resample_method_supported(method)) {
|
|
|
|
|
pa_log_warn("Support for resampler '%s' not compiled in, reverting to 'auto'.", pa_resample_method_to_string(method));
|
|
|
|
|
method = PA_RESAMPLER_AUTO;
|
2007-10-28 19:13:50 +00:00
|
|
|
}
|
|
|
|
|
|
2013-06-26 16:09:08 +02:00
|
|
|
switch (method) {
|
|
|
|
|
case PA_RESAMPLER_COPY:
|
|
|
|
|
if (rate_a != rate_b) {
|
|
|
|
|
pa_log_info("Resampler 'copy' cannot change sampling rate, reverting to resampler 'auto'.");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
/* Else fall through */
|
|
|
|
|
case PA_RESAMPLER_FFMPEG:
|
|
|
|
|
if (flags & PA_RESAMPLER_VARIABLE_RATE) {
|
|
|
|
|
pa_log_info("Resampler '%s' cannot do variable rate, reverting to resampler 'auto'.", pa_resample_method_to_string(method));
|
|
|
|
|
method = PA_RESAMPLER_AUTO;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
2007-10-28 19:13:50 +00:00
|
|
|
}
|
|
|
|
|
|
2011-12-11 16:07:41 +01:00
|
|
|
if (method == PA_RESAMPLER_AUTO) {
|
|
|
|
|
#ifdef HAVE_SPEEX
|
2012-11-19 15:08:34 +05:30
|
|
|
method = PA_RESAMPLER_SPEEX_FLOAT_BASE + 1;
|
2011-12-11 16:07:41 +01:00
|
|
|
#else
|
resampler: Fix crash if 'auto' resampler chooses ffmpeg with variable rate
To reproduce, add resampler-method = ffmpeg in daemon.conf
then start PA, and load module-loopback
Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0xb2f1db40 (LWP 23047)]
0x00000000 in ?? ()
(gdb) bt
0 0x00000000 in ?? ()
1 0xb7c463cb in pa_resampler_set_input_rate (r=0x80e9438, rate=44011) at pulsecore/resampler.c:365
2 0xb7c6321d in pa_sink_input_process_msg (o=0x80e87a0, code=3, userdata=0xabeb, offset=0, chunk=0x0)
at pulsecore/sink-input.c:1833
3 0xb7e9840b in sink_input_process_msg_cb (obj=0x80e87a0, code=3, data=0xabeb, offset=0, chunk=0x0)
at modules/module-loopback.c:538
4 0xb7c2709b in pa_asyncmsgq_dispatch (object=0x80e87a0, code=3, userdata=0xabeb, offset=0, memchunk=0xb2f1d17c)
at pulsecore/asyncmsgq.c:322
5 0xb7c4c6e3 in asyncmsgq_read_work (i=0x80dd580) at pulsecore/rtpoll.c:564
6 0xb7c4b34a in pa_rtpoll_run (p=0x80fb7e0, wait_op=true) at pulsecore/rtpoll.c:238
7 0xb7dd90af in thread_func (userdata=0x80afe88) at modules/alsa/alsa-sink.c:1785
8 0xb7bf3291 in internal_thread_func (userdata=0x8095d08) at pulsecore/thread-posix.c:83
9 0xb7ab9d4c in start_thread (arg=0xb2f1db40) at pthread_create.c:308
10 0xb79f3ace in clone () at ../sysdeps/unix/sysv/linux/i386/clone.S:130
2012-08-22 15:42:16 +02:00
|
|
|
if (flags & PA_RESAMPLER_VARIABLE_RATE)
|
|
|
|
|
method = PA_RESAMPLER_TRIVIAL;
|
|
|
|
|
else
|
|
|
|
|
method = PA_RESAMPLER_FFMPEG;
|
2011-12-11 16:07:41 +01:00
|
|
|
#endif
|
|
|
|
|
}
|
2004-07-02 18:47:03 +00:00
|
|
|
|
2013-06-26 13:51:55 +02:00
|
|
|
return method;
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-26 16:31:49 +02:00
|
|
|
static pa_sample_format_t pa_resampler_choose_work_format(
|
|
|
|
|
pa_resample_method_t method,
|
|
|
|
|
pa_sample_format_t a,
|
|
|
|
|
pa_sample_format_t b,
|
|
|
|
|
bool map_required) {
|
|
|
|
|
pa_sample_format_t work_format;
|
|
|
|
|
|
|
|
|
|
pa_assert(a >= 0 && a < PA_SAMPLE_MAX);
|
|
|
|
|
pa_assert(b >= 0 && b < PA_SAMPLE_MAX);
|
|
|
|
|
pa_assert(method >= 0);
|
|
|
|
|
pa_assert(method < PA_RESAMPLER_MAX);
|
|
|
|
|
|
2013-07-11 19:28:29 +02:00
|
|
|
if (method >= PA_RESAMPLER_SPEEX_FIXED_BASE && method <= PA_RESAMPLER_SPEEX_FIXED_MAX)
|
|
|
|
|
method = PA_RESAMPLER_SPEEX_FIXED_BASE;
|
2013-06-26 16:31:49 +02:00
|
|
|
|
2013-07-11 19:28:29 +02:00
|
|
|
switch (method) {
|
|
|
|
|
/* This block is for resampling functions that only
|
|
|
|
|
* support the S16 sample format. */
|
|
|
|
|
case PA_RESAMPLER_SPEEX_FIXED_BASE: /* fall through */
|
|
|
|
|
case PA_RESAMPLER_FFMPEG:
|
|
|
|
|
work_format = PA_SAMPLE_S16NE;
|
|
|
|
|
break;
|
2013-06-26 16:31:49 +02:00
|
|
|
|
2013-07-11 19:28:29 +02:00
|
|
|
/* This block is for resampling functions that support
|
|
|
|
|
* any sample format. */
|
|
|
|
|
case PA_RESAMPLER_COPY: /* fall through */
|
|
|
|
|
case PA_RESAMPLER_TRIVIAL:
|
|
|
|
|
if (!map_required && a == b) {
|
|
|
|
|
work_format = a;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
/* Else fall trough */
|
|
|
|
|
case PA_RESAMPLER_PEAKS:
|
2013-06-26 16:31:49 +02:00
|
|
|
if (a == PA_SAMPLE_S16NE || b == PA_SAMPLE_S16NE)
|
|
|
|
|
work_format = PA_SAMPLE_S16NE;
|
|
|
|
|
else if (a == PA_SAMPLE_S32NE || a == PA_SAMPLE_S32RE ||
|
|
|
|
|
a == PA_SAMPLE_FLOAT32NE || a == PA_SAMPLE_FLOAT32RE ||
|
|
|
|
|
a == PA_SAMPLE_S24NE || a == PA_SAMPLE_S24RE ||
|
|
|
|
|
a == PA_SAMPLE_S24_32NE || a == PA_SAMPLE_S24_32RE ||
|
|
|
|
|
b == PA_SAMPLE_S32NE || b == PA_SAMPLE_S32RE ||
|
|
|
|
|
b == PA_SAMPLE_FLOAT32NE || b == PA_SAMPLE_FLOAT32RE ||
|
|
|
|
|
b == PA_SAMPLE_S24NE || b == PA_SAMPLE_S24RE ||
|
|
|
|
|
b == PA_SAMPLE_S24_32NE || b == PA_SAMPLE_S24_32RE)
|
|
|
|
|
work_format = PA_SAMPLE_FLOAT32NE;
|
|
|
|
|
else
|
|
|
|
|
work_format = PA_SAMPLE_S16NE;
|
2013-07-11 19:28:29 +02:00
|
|
|
break;
|
2013-06-26 16:31:49 +02:00
|
|
|
|
2013-07-11 19:28:29 +02:00
|
|
|
default:
|
|
|
|
|
work_format = PA_SAMPLE_FLOAT32NE;
|
|
|
|
|
}
|
2013-06-26 16:31:49 +02:00
|
|
|
|
|
|
|
|
return work_format;
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-26 13:51:55 +02:00
|
|
|
pa_resampler* pa_resampler_new(
|
|
|
|
|
pa_mempool *pool,
|
|
|
|
|
const pa_sample_spec *a,
|
|
|
|
|
const pa_channel_map *am,
|
|
|
|
|
const pa_sample_spec *b,
|
|
|
|
|
const pa_channel_map *bm,
|
|
|
|
|
pa_resample_method_t method,
|
|
|
|
|
pa_resample_flags_t flags) {
|
|
|
|
|
|
|
|
|
|
pa_resampler *r = NULL;
|
|
|
|
|
|
|
|
|
|
pa_assert(pool);
|
|
|
|
|
pa_assert(a);
|
|
|
|
|
pa_assert(b);
|
|
|
|
|
pa_assert(pa_sample_spec_valid(a));
|
|
|
|
|
pa_assert(pa_sample_spec_valid(b));
|
|
|
|
|
pa_assert(method >= 0);
|
|
|
|
|
pa_assert(method < PA_RESAMPLER_MAX);
|
|
|
|
|
|
|
|
|
|
method = pa_resampler_fix_method(flags, method, a->rate, b->rate);
|
|
|
|
|
|
2012-05-10 09:19:21 +03:00
|
|
|
r = pa_xnew0(pa_resampler, 1);
|
2006-08-18 19:55:18 +00:00
|
|
|
r->mempool = pool;
|
2007-11-11 02:30:59 +00:00
|
|
|
r->method = method;
|
|
|
|
|
r->flags = flags;
|
2004-07-03 00:19:17 +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;
|
|
|
|
|
|
2009-08-20 17:54:45 +02:00
|
|
|
/* set up the remap structure */
|
|
|
|
|
r->remap.i_ss = &r->i_ss;
|
|
|
|
|
r->remap.o_ss = &r->o_ss;
|
|
|
|
|
r->remap.format = &r->work_format;
|
|
|
|
|
|
2006-01-27 16:25:31 +00:00
|
|
|
if (am)
|
|
|
|
|
r->i_cm = *am;
|
2008-02-13 22:13:44 +00:00
|
|
|
else if (!pa_channel_map_init_auto(&r->i_cm, r->i_ss.channels, PA_CHANNEL_MAP_DEFAULT))
|
|
|
|
|
goto fail;
|
2006-01-27 16:25:31 +00:00
|
|
|
|
|
|
|
|
if (bm)
|
|
|
|
|
r->o_cm = *bm;
|
2008-02-13 22:13:44 +00:00
|
|
|
else if (!pa_channel_map_init_auto(&r->o_cm, r->o_ss.channels, PA_CHANNEL_MAP_DEFAULT))
|
|
|
|
|
goto fail;
|
2007-01-04 13:43:45 +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
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
calc_map_table(r);
|
|
|
|
|
|
2007-11-11 02:30:59 +00:00
|
|
|
pa_log_info("Using resampler '%s'", pa_resample_method_to_string(method));
|
2007-10-28 19:13:50 +00:00
|
|
|
|
2013-06-26 16:31:49 +02:00
|
|
|
r->work_format = pa_resampler_choose_work_format(method, a->format, b->format, r->map_required);
|
2007-10-28 19:13:50 +00:00
|
|
|
|
|
|
|
|
pa_log_info("Using %s as working format.", pa_sample_format_to_string(r->work_format));
|
|
|
|
|
|
2009-02-04 17:16:53 +01:00
|
|
|
r->w_sz = pa_sample_size_of_format(r->work_format);
|
2007-10-28 19:13:50 +00:00
|
|
|
|
2012-05-10 09:19:21 +03:00
|
|
|
if (r->i_ss.format != r->work_format) {
|
|
|
|
|
if (r->work_format == PA_SAMPLE_FLOAT32NE) {
|
|
|
|
|
if (!(r->to_work_format_func = pa_get_convert_to_float32ne_function(r->i_ss.format)))
|
|
|
|
|
goto fail;
|
|
|
|
|
} else {
|
|
|
|
|
pa_assert(r->work_format == PA_SAMPLE_S16NE);
|
|
|
|
|
if (!(r->to_work_format_func = pa_get_convert_to_s16ne_function(r->i_ss.format)))
|
|
|
|
|
goto fail;
|
|
|
|
|
}
|
2007-10-28 19:13:50 +00:00
|
|
|
}
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2012-05-10 09:19:21 +03:00
|
|
|
if (r->o_ss.format != r->work_format) {
|
|
|
|
|
if (r->work_format == PA_SAMPLE_FLOAT32NE) {
|
|
|
|
|
if (!(r->from_work_format_func = pa_get_convert_from_float32ne_function(r->o_ss.format)))
|
|
|
|
|
goto fail;
|
|
|
|
|
} else {
|
|
|
|
|
pa_assert(r->work_format == PA_SAMPLE_S16NE);
|
|
|
|
|
if (!(r->from_work_format_func = pa_get_convert_from_s16ne_function(r->o_ss.format)))
|
|
|
|
|
goto fail;
|
|
|
|
|
}
|
2004-11-20 16:23:53 +00:00
|
|
|
}
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
/* initialize implementation */
|
2007-11-11 02:30:59 +00:00
|
|
|
if (init_table[method](r) < 0)
|
2007-10-28 19:13:50 +00:00
|
|
|
goto fail;
|
|
|
|
|
|
2004-07-02 18:47:03 +00:00
|
|
|
return r;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2004-07-02 18:47:03 +00:00
|
|
|
fail:
|
2009-08-28 15:16:54 +03:00
|
|
|
pa_xfree(r);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
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) {
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(r);
|
2004-11-20 16:23:53 +00:00
|
|
|
|
|
|
|
|
if (r->impl_free)
|
|
|
|
|
r->impl_free(r);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2012-05-10 09:19:22 +03:00
|
|
|
if (r->to_work_format_buf.memblock)
|
|
|
|
|
pa_memblock_unref(r->to_work_format_buf.memblock);
|
|
|
|
|
if (r->remap_buf.memblock)
|
|
|
|
|
pa_memblock_unref(r->remap_buf.memblock);
|
|
|
|
|
if (r->resample_buf.memblock)
|
|
|
|
|
pa_memblock_unref(r->resample_buf.memblock);
|
|
|
|
|
if (r->from_work_format_buf.memblock)
|
|
|
|
|
pa_memblock_unref(r->from_work_format_buf.memblock);
|
2007-10-28 19:13:50 +00:00
|
|
|
|
2013-06-18 14:22:26 +02:00
|
|
|
pa_xfree(r->impl_data);
|
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) {
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(r);
|
|
|
|
|
pa_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;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2004-11-20 16:23:53 +00:00
|
|
|
r->i_ss.rate = rate;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
r->impl_update_rates(r);
|
2004-11-20 16:23:53 +00:00
|
|
|
}
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
void pa_resampler_set_output_rate(pa_resampler *r, uint32_t rate) {
|
|
|
|
|
pa_assert(r);
|
|
|
|
|
pa_assert(rate > 0);
|
2004-11-20 16:23:53 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
if (r->o_ss.rate == rate)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
r->o_ss.rate = rate;
|
|
|
|
|
|
|
|
|
|
r->impl_update_rates(r);
|
2004-11-20 16:23:53 +00:00
|
|
|
}
|
|
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
size_t pa_resampler_request(pa_resampler *r, size_t out_length) {
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(r);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2012-05-10 09:19:23 +03:00
|
|
|
/* Let's round up here to make it more likely that the caller will get at
|
|
|
|
|
* least out_length amount of data from pa_resampler_run().
|
|
|
|
|
*
|
|
|
|
|
* We don't take the leftover into account here. If we did, then it might
|
|
|
|
|
* be in theory possible that this function would return 0 and
|
|
|
|
|
* pa_resampler_run() would also return 0. That could lead to infinite
|
|
|
|
|
* loops. When the leftover is ignored here, such loops would eventually
|
|
|
|
|
* terminate, because the leftover would grow each round, finally
|
|
|
|
|
* surpassing the minimum input threshold of the resampler. */
|
2012-07-09 10:07:05 +03:00
|
|
|
return ((((uint64_t) ((out_length + r->o_fz-1) / r->o_fz) * r->i_ss.rate) + r->o_ss.rate-1) / r->o_ss.rate) * r->i_fz;
|
2004-11-20 16:23:53 +00:00
|
|
|
}
|
|
|
|
|
|
2008-05-15 23:34:41 +00:00
|
|
|
size_t pa_resampler_result(pa_resampler *r, size_t in_length) {
|
2012-05-10 09:19:23 +03:00
|
|
|
size_t frames;
|
|
|
|
|
|
2008-05-15 23:34:41 +00:00
|
|
|
pa_assert(r);
|
|
|
|
|
|
2012-05-10 09:19:23 +03:00
|
|
|
/* Let's round up here to ensure that the caller will always allocate big
|
|
|
|
|
* enough output buffer. */
|
|
|
|
|
|
|
|
|
|
frames = (in_length + r->i_fz - 1) / r->i_fz;
|
2009-08-14 04:21:09 +02:00
|
|
|
|
2012-05-10 09:19:23 +03:00
|
|
|
if (r->remap_buf_contains_leftover_data)
|
|
|
|
|
frames += r->remap_buf.length / (r->w_sz * r->o_ss.channels);
|
|
|
|
|
|
2012-07-09 10:07:05 +03:00
|
|
|
return (((uint64_t) frames * r->o_ss.rate + r->i_ss.rate - 1) / r->i_ss.rate) * r->o_fz;
|
2008-05-15 23:34:41 +00:00
|
|
|
}
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
size_t pa_resampler_max_block_size(pa_resampler *r) {
|
|
|
|
|
size_t block_size_max;
|
2012-05-10 09:19:23 +03:00
|
|
|
pa_sample_spec max_ss;
|
|
|
|
|
size_t max_fs;
|
|
|
|
|
size_t frames;
|
2007-10-28 19:13:50 +00:00
|
|
|
|
|
|
|
|
pa_assert(r);
|
|
|
|
|
|
|
|
|
|
block_size_max = pa_mempool_block_size_max(r->mempool);
|
|
|
|
|
|
|
|
|
|
/* We deduce the "largest" sample spec we're using during the
|
|
|
|
|
* conversion */
|
2012-05-10 09:19:23 +03:00
|
|
|
max_ss.channels = (uint8_t) (PA_MAX(r->i_ss.channels, r->o_ss.channels));
|
2007-10-28 19:13:50 +00:00
|
|
|
|
|
|
|
|
/* We silently assume that the format enum is ordered by size */
|
2012-05-10 09:19:23 +03:00
|
|
|
max_ss.format = PA_MAX(r->i_ss.format, r->o_ss.format);
|
|
|
|
|
max_ss.format = PA_MAX(max_ss.format, r->work_format);
|
|
|
|
|
|
|
|
|
|
max_ss.rate = PA_MAX(r->i_ss.rate, r->o_ss.rate);
|
2007-10-28 19:13:50 +00:00
|
|
|
|
2012-05-10 09:19:23 +03:00
|
|
|
max_fs = pa_frame_size(&max_ss);
|
|
|
|
|
frames = block_size_max / max_fs - EXTRA_FRAMES;
|
2007-10-28 19:13:50 +00:00
|
|
|
|
2012-05-10 09:19:23 +03:00
|
|
|
if (r->remap_buf_contains_leftover_data)
|
|
|
|
|
frames -= r->remap_buf.length / (r->w_sz * r->o_ss.channels);
|
2007-10-28 19:13:50 +00:00
|
|
|
|
2012-07-09 10:07:05 +03:00
|
|
|
return ((uint64_t) frames * r->i_ss.rate / max_ss.rate) * r->i_fz;
|
2007-10-28 19:13:50 +00:00
|
|
|
}
|
|
|
|
|
|
2007-12-23 20:15:03 +00:00
|
|
|
void pa_resampler_reset(pa_resampler *r) {
|
|
|
|
|
pa_assert(r);
|
|
|
|
|
|
|
|
|
|
if (r->impl_reset)
|
|
|
|
|
r->impl_reset(r);
|
2012-05-10 09:19:23 +03:00
|
|
|
|
2013-02-07 14:03:15 +01:00
|
|
|
r->remap_buf_contains_leftover_data = false;
|
2007-12-23 20:15:03 +00:00
|
|
|
}
|
|
|
|
|
|
2006-01-27 16:25:31 +00:00
|
|
|
pa_resample_method_t pa_resampler_get_method(pa_resampler *r) {
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(r);
|
|
|
|
|
|
2007-11-11 02:30:59 +00:00
|
|
|
return r->method;
|
2004-11-20 16:23:53 +00:00
|
|
|
}
|
|
|
|
|
|
2009-01-23 22:28:11 +01:00
|
|
|
const pa_channel_map* pa_resampler_input_channel_map(pa_resampler *r) {
|
|
|
|
|
pa_assert(r);
|
|
|
|
|
|
|
|
|
|
return &r->i_cm;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const pa_sample_spec* pa_resampler_input_sample_spec(pa_resampler *r) {
|
|
|
|
|
pa_assert(r);
|
|
|
|
|
|
|
|
|
|
return &r->i_ss;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const pa_channel_map* pa_resampler_output_channel_map(pa_resampler *r) {
|
|
|
|
|
pa_assert(r);
|
|
|
|
|
|
|
|
|
|
return &r->o_cm;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const pa_sample_spec* pa_resampler_output_sample_spec(pa_resampler *r) {
|
|
|
|
|
pa_assert(r);
|
|
|
|
|
|
|
|
|
|
return &r->o_ss;
|
|
|
|
|
}
|
|
|
|
|
|
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",
|
2007-10-28 19:13:50 +00:00
|
|
|
"trivial",
|
|
|
|
|
"speex-float-0",
|
|
|
|
|
"speex-float-1",
|
|
|
|
|
"speex-float-2",
|
|
|
|
|
"speex-float-3",
|
|
|
|
|
"speex-float-4",
|
|
|
|
|
"speex-float-5",
|
|
|
|
|
"speex-float-6",
|
|
|
|
|
"speex-float-7",
|
|
|
|
|
"speex-float-8",
|
|
|
|
|
"speex-float-9",
|
|
|
|
|
"speex-float-10",
|
|
|
|
|
"speex-fixed-0",
|
|
|
|
|
"speex-fixed-1",
|
|
|
|
|
"speex-fixed-2",
|
|
|
|
|
"speex-fixed-3",
|
|
|
|
|
"speex-fixed-4",
|
|
|
|
|
"speex-fixed-5",
|
|
|
|
|
"speex-fixed-6",
|
|
|
|
|
"speex-fixed-7",
|
|
|
|
|
"speex-fixed-8",
|
|
|
|
|
"speex-fixed-9",
|
|
|
|
|
"speex-fixed-10",
|
|
|
|
|
"ffmpeg",
|
|
|
|
|
"auto",
|
2008-05-15 23:34:41 +00:00
|
|
|
"copy",
|
|
|
|
|
"peaks"
|
2006-01-27 16:25:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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];
|
|
|
|
|
}
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
int pa_resample_method_supported(pa_resample_method_t m) {
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
if (m < 0 || m >= PA_RESAMPLER_MAX)
|
|
|
|
|
return 0;
|
2004-11-20 16:23:53 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
#ifndef HAVE_LIBSAMPLERATE
|
|
|
|
|
if (m <= PA_RESAMPLER_SRC_LINEAR)
|
|
|
|
|
return 0;
|
|
|
|
|
#endif
|
2006-01-27 16:25:31 +00:00
|
|
|
|
2011-12-11 16:07:41 +01:00
|
|
|
#ifndef HAVE_SPEEX
|
|
|
|
|
if (m >= PA_RESAMPLER_SPEEX_FLOAT_BASE && m <= PA_RESAMPLER_SPEEX_FLOAT_MAX)
|
|
|
|
|
return 0;
|
|
|
|
|
if (m >= PA_RESAMPLER_SPEEX_FIXED_BASE && m <= PA_RESAMPLER_SPEEX_FIXED_MAX)
|
|
|
|
|
return 0;
|
|
|
|
|
#endif
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
return 1;
|
2004-07-02 18:47:03 +00:00
|
|
|
}
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_resample_method_t pa_parse_resample_method(const char *string) {
|
|
|
|
|
pa_resample_method_t m;
|
2006-01-27 16:25:31 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(string);
|
2006-01-27 16:25:31 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
for (m = 0; m < PA_RESAMPLER_MAX; m++)
|
2012-06-06 01:28:14 +05:30
|
|
|
if (pa_streq(string, resample_methods[m]))
|
2007-10-28 19:13:50 +00:00
|
|
|
return m;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2012-06-06 01:28:14 +05:30
|
|
|
if (pa_streq(string, "speex-fixed"))
|
2012-11-19 15:08:34 +05:30
|
|
|
return PA_RESAMPLER_SPEEX_FIXED_BASE + 1;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2012-06-06 01:28:14 +05:30
|
|
|
if (pa_streq(string, "speex-float"))
|
2012-11-19 15:08:34 +05:30
|
|
|
return PA_RESAMPLER_SPEEX_FLOAT_BASE + 1;
|
2006-01-27 16:25:31 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
return PA_RESAMPLER_INVALID;
|
2006-01-27 16:25:31 +00:00
|
|
|
}
|
|
|
|
|
|
2013-02-07 14:03:15 +01:00
|
|
|
static bool on_left(pa_channel_position_t p) {
|
2007-11-11 02:30:59 +00:00
|
|
|
|
|
|
|
|
return
|
|
|
|
|
p == PA_CHANNEL_POSITION_FRONT_LEFT ||
|
|
|
|
|
p == PA_CHANNEL_POSITION_REAR_LEFT ||
|
|
|
|
|
p == PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER ||
|
|
|
|
|
p == PA_CHANNEL_POSITION_SIDE_LEFT ||
|
|
|
|
|
p == PA_CHANNEL_POSITION_TOP_FRONT_LEFT ||
|
|
|
|
|
p == PA_CHANNEL_POSITION_TOP_REAR_LEFT;
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-07 14:03:15 +01:00
|
|
|
static bool on_right(pa_channel_position_t p) {
|
2007-11-11 02:30:59 +00:00
|
|
|
|
|
|
|
|
return
|
|
|
|
|
p == PA_CHANNEL_POSITION_FRONT_RIGHT ||
|
|
|
|
|
p == PA_CHANNEL_POSITION_REAR_RIGHT ||
|
|
|
|
|
p == PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER ||
|
|
|
|
|
p == PA_CHANNEL_POSITION_SIDE_RIGHT ||
|
|
|
|
|
p == PA_CHANNEL_POSITION_TOP_FRONT_RIGHT ||
|
|
|
|
|
p == PA_CHANNEL_POSITION_TOP_REAR_RIGHT;
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-07 14:03:15 +01:00
|
|
|
static bool on_center(pa_channel_position_t p) {
|
2007-11-11 02:30:59 +00:00
|
|
|
|
|
|
|
|
return
|
|
|
|
|
p == PA_CHANNEL_POSITION_FRONT_CENTER ||
|
|
|
|
|
p == PA_CHANNEL_POSITION_REAR_CENTER ||
|
|
|
|
|
p == PA_CHANNEL_POSITION_TOP_CENTER ||
|
|
|
|
|
p == PA_CHANNEL_POSITION_TOP_FRONT_CENTER ||
|
|
|
|
|
p == PA_CHANNEL_POSITION_TOP_REAR_CENTER;
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-07 14:03:15 +01:00
|
|
|
static bool on_lfe(pa_channel_position_t p) {
|
2007-11-11 02:30:59 +00:00
|
|
|
return
|
|
|
|
|
p == PA_CHANNEL_POSITION_LFE;
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-07 14:03:15 +01:00
|
|
|
static bool on_front(pa_channel_position_t p) {
|
2008-12-17 19:53:58 +01:00
|
|
|
return
|
|
|
|
|
p == PA_CHANNEL_POSITION_FRONT_LEFT ||
|
|
|
|
|
p == PA_CHANNEL_POSITION_FRONT_RIGHT ||
|
|
|
|
|
p == PA_CHANNEL_POSITION_FRONT_CENTER ||
|
|
|
|
|
p == PA_CHANNEL_POSITION_TOP_FRONT_LEFT ||
|
|
|
|
|
p == PA_CHANNEL_POSITION_TOP_FRONT_RIGHT ||
|
|
|
|
|
p == PA_CHANNEL_POSITION_TOP_FRONT_CENTER ||
|
|
|
|
|
p == PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER ||
|
|
|
|
|
p == PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER;
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-07 14:03:15 +01:00
|
|
|
static bool on_rear(pa_channel_position_t p) {
|
2008-12-17 19:53:58 +01:00
|
|
|
return
|
|
|
|
|
p == PA_CHANNEL_POSITION_REAR_LEFT ||
|
|
|
|
|
p == PA_CHANNEL_POSITION_REAR_RIGHT ||
|
|
|
|
|
p == PA_CHANNEL_POSITION_REAR_CENTER ||
|
|
|
|
|
p == PA_CHANNEL_POSITION_TOP_REAR_LEFT ||
|
|
|
|
|
p == PA_CHANNEL_POSITION_TOP_REAR_RIGHT ||
|
|
|
|
|
p == PA_CHANNEL_POSITION_TOP_REAR_CENTER;
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-07 14:03:15 +01:00
|
|
|
static bool on_side(pa_channel_position_t p) {
|
2008-12-17 19:53:58 +01:00
|
|
|
return
|
|
|
|
|
p == PA_CHANNEL_POSITION_SIDE_LEFT ||
|
|
|
|
|
p == PA_CHANNEL_POSITION_SIDE_RIGHT ||
|
|
|
|
|
p == PA_CHANNEL_POSITION_TOP_CENTER;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
enum {
|
|
|
|
|
ON_FRONT,
|
|
|
|
|
ON_REAR,
|
|
|
|
|
ON_SIDE,
|
|
|
|
|
ON_OTHER
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static int front_rear_side(pa_channel_position_t p) {
|
|
|
|
|
if (on_front(p))
|
|
|
|
|
return ON_FRONT;
|
|
|
|
|
if (on_rear(p))
|
|
|
|
|
return ON_REAR;
|
|
|
|
|
if (on_side(p))
|
|
|
|
|
return ON_SIDE;
|
|
|
|
|
return ON_OTHER;
|
|
|
|
|
}
|
|
|
|
|
|
2006-01-27 16:25:31 +00:00
|
|
|
static void calc_map_table(pa_resampler *r) {
|
2007-11-11 02:30:59 +00:00
|
|
|
unsigned oc, ic;
|
2009-08-20 17:54:45 +02:00
|
|
|
unsigned n_oc, n_ic;
|
2013-02-07 14:03:15 +01:00
|
|
|
bool ic_connected[PA_CHANNELS_MAX];
|
|
|
|
|
bool remix;
|
2007-11-11 02:30:59 +00:00
|
|
|
pa_strbuf *s;
|
|
|
|
|
char *t;
|
2009-08-20 17:54:45 +02:00
|
|
|
pa_remap_t *m;
|
2004-11-20 16:23:53 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(r);
|
2006-01-27 16:25:31 +00:00
|
|
|
|
2007-11-11 02:30:59 +00:00
|
|
|
if (!(r->map_required = (r->i_ss.channels != r->o_ss.channels || (!(r->flags & PA_RESAMPLER_NO_REMAP) && !pa_channel_map_equal(&r->i_cm, &r->o_cm)))))
|
2006-01-27 16:25:31 +00:00
|
|
|
return;
|
|
|
|
|
|
2009-08-20 17:54:45 +02:00
|
|
|
m = &r->remap;
|
|
|
|
|
|
|
|
|
|
n_oc = r->o_ss.channels;
|
|
|
|
|
n_ic = r->i_ss.channels;
|
|
|
|
|
|
|
|
|
|
memset(m->map_table_f, 0, sizeof(m->map_table_f));
|
|
|
|
|
memset(m->map_table_i, 0, sizeof(m->map_table_i));
|
|
|
|
|
|
2007-11-11 02:30:59 +00:00
|
|
|
memset(ic_connected, 0, sizeof(ic_connected));
|
2013-02-07 14:03:16 +01:00
|
|
|
remix = (r->flags & (PA_RESAMPLER_NO_REMAP | PA_RESAMPLER_NO_REMIX)) == 0;
|
2007-11-11 02:30:59 +00:00
|
|
|
|
2013-02-07 14:03:16 +01:00
|
|
|
if (r->flags & PA_RESAMPLER_NO_REMAP) {
|
|
|
|
|
pa_assert(!remix);
|
2007-11-11 02:30:59 +00:00
|
|
|
|
2013-02-07 14:03:16 +01:00
|
|
|
for (oc = 0; oc < PA_MIN(n_ic, n_oc); oc++)
|
|
|
|
|
m->map_table_f[oc][oc] = 1.0f;
|
2007-11-11 02:30:59 +00:00
|
|
|
|
2013-02-07 14:03:16 +01:00
|
|
|
} else if (r->flags & PA_RESAMPLER_NO_REMIX) {
|
|
|
|
|
pa_assert(!remix);
|
|
|
|
|
for (oc = 0; oc < n_oc; oc++) {
|
|
|
|
|
pa_channel_position_t b = r->o_cm.map[oc];
|
2007-11-11 02:30:59 +00:00
|
|
|
|
2013-02-07 14:03:16 +01:00
|
|
|
for (ic = 0; ic < n_ic; ic++) {
|
|
|
|
|
pa_channel_position_t a = r->i_cm.map[ic];
|
2007-11-11 02:30:59 +00:00
|
|
|
|
|
|
|
|
/* We shall not do any remixing. Hence, just check by name */
|
|
|
|
|
if (a == b)
|
2013-02-07 14:03:16 +01:00
|
|
|
m->map_table_f[oc][ic] = 1.0f;
|
2007-11-11 02:30:59 +00:00
|
|
|
}
|
2013-02-07 14:03:16 +01:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
|
|
/* OK, we shall do the full monty: upmixing and downmixing. Our
|
|
|
|
|
* algorithm is relatively simple, does not do spacialization, delay
|
|
|
|
|
* elements or apply lowpass filters for LFE. Patches are always
|
|
|
|
|
* welcome, though. Oh, and it doesn't do any matrix decoding. (Which
|
|
|
|
|
* probably wouldn't make any sense anyway.)
|
|
|
|
|
*
|
|
|
|
|
* This code is not idempotent: downmixing an upmixed stereo stream is
|
|
|
|
|
* not identical to the original. The volume will not match, and the
|
|
|
|
|
* two channels will be a linear combination of both.
|
|
|
|
|
*
|
|
|
|
|
* This is loosely based on random suggestions found on the Internet,
|
|
|
|
|
* such as this:
|
|
|
|
|
* http://www.halfgaar.net/surround-sound-in-linux and the alsa upmix
|
|
|
|
|
* plugin.
|
|
|
|
|
*
|
|
|
|
|
* The algorithm works basically like this:
|
|
|
|
|
*
|
|
|
|
|
* 1) Connect all channels with matching names.
|
|
|
|
|
*
|
|
|
|
|
* 2) Mono Handling:
|
|
|
|
|
* S:Mono: Copy into all D:channels
|
|
|
|
|
* D:Mono: Avg all S:channels
|
|
|
|
|
*
|
|
|
|
|
* 3) Mix D:Left, D:Right:
|
|
|
|
|
* D:Left: If not connected, avg all S:Left
|
|
|
|
|
* D:Right: If not connected, avg all S:Right
|
|
|
|
|
*
|
|
|
|
|
* 4) Mix D:Center
|
|
|
|
|
* If not connected, avg all S:Center
|
|
|
|
|
* If still not connected, avg all S:Left, S:Right
|
|
|
|
|
*
|
|
|
|
|
* 5) Mix D:LFE
|
|
|
|
|
* If not connected, avg all S:*
|
|
|
|
|
*
|
|
|
|
|
* 6) Make sure S:Left/S:Right is used: S:Left/S:Right: If not
|
|
|
|
|
* connected, mix into all D:left and all D:right channels. Gain is
|
2013-02-07 14:03:17 +01:00
|
|
|
* 1/9.
|
2013-02-07 14:03:16 +01:00
|
|
|
*
|
|
|
|
|
* 7) Make sure S:Center, S:LFE is used:
|
|
|
|
|
*
|
|
|
|
|
* S:Center, S:LFE: If not connected, mix into all D:left, all
|
2013-02-07 14:03:17 +01:00
|
|
|
* D:right, all D:center channels. Gain is 0.5 for center and 0.375
|
|
|
|
|
* for LFE. C-front is only mixed into L-front/R-front if available,
|
|
|
|
|
* otherwise into all L/R channels. Similarly for C-rear.
|
|
|
|
|
*
|
|
|
|
|
* 8) Normalize each row in the matrix such that the sum for each row is
|
|
|
|
|
* not larger than 1.0 in order to avoid clipping.
|
2013-02-07 14:03:16 +01:00
|
|
|
*
|
|
|
|
|
* S: and D: shall relate to the source resp. destination channels.
|
|
|
|
|
*
|
|
|
|
|
* Rationale: 1, 2 are probably obvious. For 3: this copies front to
|
|
|
|
|
* rear if needed. For 4: we try to find some suitable C source for C,
|
|
|
|
|
* if we don't find any, we avg L and R. For 5: LFE is mixed from all
|
|
|
|
|
* channels. For 6: the rear channels should not be dropped entirely,
|
|
|
|
|
* however have only minimal impact. For 7: movies usually encode
|
|
|
|
|
* speech on the center channel. Thus we have to make sure this channel
|
|
|
|
|
* is distributed to L and R if not available in the output. Also, LFE
|
|
|
|
|
* is used to achieve a greater dynamic range, and thus we should try
|
|
|
|
|
* to do our best to pass it to L+R.
|
|
|
|
|
*/
|
2007-11-11 02:30:59 +00:00
|
|
|
|
2013-02-07 14:03:16 +01:00
|
|
|
unsigned
|
|
|
|
|
ic_left = 0,
|
|
|
|
|
ic_right = 0,
|
|
|
|
|
ic_center = 0,
|
|
|
|
|
ic_unconnected_left = 0,
|
|
|
|
|
ic_unconnected_right = 0,
|
|
|
|
|
ic_unconnected_center = 0,
|
|
|
|
|
ic_unconnected_lfe = 0;
|
2013-02-07 14:03:17 +01:00
|
|
|
bool ic_unconnected_center_mixed_in = 0;
|
2012-08-08 16:01:38 +02:00
|
|
|
|
2013-02-07 14:03:16 +01:00
|
|
|
pa_assert(remix);
|
|
|
|
|
|
|
|
|
|
for (ic = 0; ic < n_ic; ic++) {
|
|
|
|
|
if (on_left(r->i_cm.map[ic]))
|
|
|
|
|
ic_left++;
|
|
|
|
|
if (on_right(r->i_cm.map[ic]))
|
|
|
|
|
ic_right++;
|
|
|
|
|
if (on_center(r->i_cm.map[ic]))
|
|
|
|
|
ic_center++;
|
2007-11-11 02:30:59 +00:00
|
|
|
}
|
|
|
|
|
|
2013-02-07 14:03:16 +01:00
|
|
|
for (oc = 0; oc < n_oc; oc++) {
|
|
|
|
|
bool oc_connected = false;
|
|
|
|
|
pa_channel_position_t b = r->o_cm.map[oc];
|
2007-11-11 02:30:59 +00:00
|
|
|
|
2013-02-07 14:03:16 +01:00
|
|
|
for (ic = 0; ic < n_ic; ic++) {
|
|
|
|
|
pa_channel_position_t a = r->i_cm.map[ic];
|
2008-12-17 19:53:58 +01:00
|
|
|
|
2013-02-07 14:03:16 +01:00
|
|
|
if (a == b || a == PA_CHANNEL_POSITION_MONO) {
|
|
|
|
|
m->map_table_f[oc][ic] = 1.0f;
|
2007-11-11 02:30:59 +00:00
|
|
|
|
2013-02-07 14:03:16 +01:00
|
|
|
oc_connected = true;
|
|
|
|
|
ic_connected[ic] = true;
|
|
|
|
|
}
|
|
|
|
|
else if (b == PA_CHANNEL_POSITION_MONO) {
|
|
|
|
|
m->map_table_f[oc][ic] = 1.0f / (float) n_ic;
|
2007-11-11 02:30:59 +00:00
|
|
|
|
2013-02-07 14:03:16 +01:00
|
|
|
oc_connected = true;
|
|
|
|
|
ic_connected[ic] = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2007-11-11 02:30:59 +00:00
|
|
|
|
2013-02-07 14:03:16 +01:00
|
|
|
if (!oc_connected) {
|
|
|
|
|
/* Try to find matching input ports for this output port */
|
2007-11-11 02:30:59 +00:00
|
|
|
|
2013-02-07 14:03:16 +01:00
|
|
|
if (on_left(b)) {
|
2007-11-11 02:30:59 +00:00
|
|
|
|
2013-02-07 14:03:16 +01:00
|
|
|
/* We are not connected and on the left side, let's
|
|
|
|
|
* average all left side input channels. */
|
2007-11-11 02:30:59 +00:00
|
|
|
|
2013-02-07 14:03:16 +01:00
|
|
|
if (ic_left > 0)
|
|
|
|
|
for (ic = 0; ic < n_ic; ic++)
|
|
|
|
|
if (on_left(r->i_cm.map[ic])) {
|
|
|
|
|
m->map_table_f[oc][ic] = 1.0f / (float) ic_left;
|
|
|
|
|
ic_connected[ic] = true;
|
|
|
|
|
}
|
2007-11-11 02:30:59 +00:00
|
|
|
|
2013-02-07 14:03:16 +01:00
|
|
|
/* We ignore the case where there is no left input channel.
|
|
|
|
|
* Something is really wrong in this case anyway. */
|
2007-11-11 02:30:59 +00:00
|
|
|
|
2013-02-07 14:03:16 +01:00
|
|
|
} else if (on_right(b)) {
|
2007-11-11 02:30:59 +00:00
|
|
|
|
2013-02-07 14:03:16 +01:00
|
|
|
/* We are not connected and on the right side, let's
|
|
|
|
|
* average all right side input channels. */
|
2007-11-11 02:30:59 +00:00
|
|
|
|
2013-02-07 14:03:16 +01:00
|
|
|
if (ic_right > 0)
|
|
|
|
|
for (ic = 0; ic < n_ic; ic++)
|
|
|
|
|
if (on_right(r->i_cm.map[ic])) {
|
|
|
|
|
m->map_table_f[oc][ic] = 1.0f / (float) ic_right;
|
|
|
|
|
ic_connected[ic] = true;
|
|
|
|
|
}
|
2007-11-11 02:30:59 +00:00
|
|
|
|
2013-02-07 14:03:16 +01:00
|
|
|
/* We ignore the case where there is no right input
|
|
|
|
|
* channel. Something is really wrong in this case anyway.
|
|
|
|
|
* */
|
2007-11-11 02:30:59 +00:00
|
|
|
|
2013-02-07 14:03:16 +01:00
|
|
|
} else if (on_center(b)) {
|
2007-11-11 02:30:59 +00:00
|
|
|
|
2013-02-07 14:03:16 +01:00
|
|
|
if (ic_center > 0) {
|
2007-11-11 02:30:59 +00:00
|
|
|
|
2013-02-07 14:03:16 +01:00
|
|
|
/* We are not connected and at the center. Let's average
|
|
|
|
|
* all center input channels. */
|
2007-11-11 02:30:59 +00:00
|
|
|
|
2013-02-07 14:03:16 +01:00
|
|
|
for (ic = 0; ic < n_ic; ic++)
|
|
|
|
|
if (on_center(r->i_cm.map[ic])) {
|
|
|
|
|
m->map_table_f[oc][ic] = 1.0f / (float) ic_center;
|
|
|
|
|
ic_connected[ic] = true;
|
|
|
|
|
}
|
2007-11-11 02:30:59 +00:00
|
|
|
|
2013-02-07 14:03:16 +01:00
|
|
|
} else if (ic_left + ic_right > 0) {
|
|
|
|
|
|
|
|
|
|
/* Hmm, no center channel around, let's synthesize it
|
|
|
|
|
* by mixing L and R.*/
|
2007-11-11 02:30:59 +00:00
|
|
|
|
2009-08-20 17:54:45 +02:00
|
|
|
for (ic = 0; ic < n_ic; ic++)
|
2007-11-11 02:30:59 +00:00
|
|
|
if (on_left(r->i_cm.map[ic]) || on_right(r->i_cm.map[ic])) {
|
2013-02-07 14:03:16 +01:00
|
|
|
m->map_table_f[oc][ic] = 1.0f / (float) (ic_left + ic_right);
|
2013-02-07 14:03:15 +01:00
|
|
|
ic_connected[ic] = true;
|
2007-11-11 02:30:59 +00:00
|
|
|
}
|
2013-02-07 14:03:16 +01:00
|
|
|
}
|
2007-11-11 02:30:59 +00:00
|
|
|
|
2013-02-07 14:03:16 +01:00
|
|
|
/* We ignore the case where there is not even a left or
|
|
|
|
|
* right input channel. Something is really wrong in this
|
|
|
|
|
* case anyway. */
|
2007-11-11 02:30:59 +00:00
|
|
|
|
2013-02-07 14:03:16 +01:00
|
|
|
} else if (on_lfe(b) && !(r->flags & PA_RESAMPLER_NO_LFE)) {
|
2007-11-11 02:30:59 +00:00
|
|
|
|
2013-02-07 14:03:16 +01:00
|
|
|
/* We are not connected and an LFE. Let's average all
|
|
|
|
|
* channels for LFE. */
|
2007-11-11 02:30:59 +00:00
|
|
|
|
2013-02-07 14:03:16 +01:00
|
|
|
for (ic = 0; ic < n_ic; ic++)
|
2009-08-20 17:54:45 +02:00
|
|
|
m->map_table_f[oc][ic] = 1.0f / (float) n_ic;
|
2007-11-11 02:30:59 +00:00
|
|
|
|
2013-02-07 14:03:16 +01:00
|
|
|
/* Please note that a channel connected to LFE doesn't
|
|
|
|
|
* really count as connected. */
|
2007-11-11 02:30:59 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-08-20 17:54:45 +02:00
|
|
|
for (ic = 0; ic < n_ic; ic++) {
|
2007-11-11 02:30:59 +00:00
|
|
|
pa_channel_position_t a = r->i_cm.map[ic];
|
|
|
|
|
|
|
|
|
|
if (ic_connected[ic])
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if (on_left(a))
|
|
|
|
|
ic_unconnected_left++;
|
|
|
|
|
else if (on_right(a))
|
|
|
|
|
ic_unconnected_right++;
|
|
|
|
|
else if (on_center(a))
|
|
|
|
|
ic_unconnected_center++;
|
|
|
|
|
else if (on_lfe(a))
|
|
|
|
|
ic_unconnected_lfe++;
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-07 14:03:17 +01:00
|
|
|
for (ic = 0; ic < n_ic; ic++) {
|
|
|
|
|
pa_channel_position_t a = r->i_cm.map[ic];
|
2007-11-11 02:30:59 +00:00
|
|
|
|
2013-02-07 14:03:17 +01:00
|
|
|
if (ic_connected[ic])
|
|
|
|
|
continue;
|
2007-11-11 02:30:59 +00:00
|
|
|
|
2009-08-20 17:54:45 +02:00
|
|
|
for (oc = 0; oc < n_oc; oc++) {
|
2013-02-07 14:03:17 +01:00
|
|
|
pa_channel_position_t b = r->o_cm.map[oc];
|
2007-11-11 02:30:59 +00:00
|
|
|
|
2013-02-07 14:03:17 +01:00
|
|
|
if (on_left(a) && on_left(b))
|
|
|
|
|
m->map_table_f[oc][ic] = (1.f/9.f) / (float) ic_unconnected_left;
|
2007-11-11 02:30:59 +00:00
|
|
|
|
2013-02-07 14:03:17 +01:00
|
|
|
else if (on_right(a) && on_right(b))
|
|
|
|
|
m->map_table_f[oc][ic] = (1.f/9.f) / (float) ic_unconnected_right;
|
2007-11-11 02:30:59 +00:00
|
|
|
|
2013-02-07 14:03:17 +01:00
|
|
|
else if (on_center(a) && on_center(b)) {
|
|
|
|
|
m->map_table_f[oc][ic] = (1.f/9.f) / (float) ic_unconnected_center;
|
|
|
|
|
ic_unconnected_center_mixed_in = true;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2013-02-07 14:03:17 +01:00
|
|
|
} else if (on_lfe(a) && !(r->flags & PA_RESAMPLER_NO_LFE))
|
|
|
|
|
m->map_table_f[oc][ic] = .375f / (float) ic_unconnected_lfe;
|
2007-11-11 02:30:59 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-07 14:03:17 +01:00
|
|
|
if (ic_unconnected_center > 0 && !ic_unconnected_center_mixed_in) {
|
|
|
|
|
unsigned ncenter[PA_CHANNELS_MAX];
|
|
|
|
|
bool found_frs[PA_CHANNELS_MAX];
|
2007-11-11 02:30:59 +00:00
|
|
|
|
2013-02-07 14:03:17 +01:00
|
|
|
memset(ncenter, 0, sizeof(ncenter));
|
|
|
|
|
memset(found_frs, 0, sizeof(found_frs));
|
2007-11-11 02:30:59 +00:00
|
|
|
|
2013-02-07 14:03:17 +01:00
|
|
|
/* Hmm, as it appears there was no center channel we
|
|
|
|
|
could mix our center channel in. In this case, mix it into
|
|
|
|
|
left and right. Using .5 as the factor. */
|
|
|
|
|
|
|
|
|
|
for (ic = 0; ic < n_ic; ic++) {
|
2007-11-11 02:30:59 +00:00
|
|
|
|
2013-02-07 14:03:17 +01:00
|
|
|
if (ic_connected[ic])
|
2007-11-11 02:30:59 +00:00
|
|
|
continue;
|
|
|
|
|
|
2013-02-07 14:03:17 +01:00
|
|
|
if (!on_center(r->i_cm.map[ic]))
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
for (oc = 0; oc < n_oc; oc++) {
|
2007-11-11 02:30:59 +00:00
|
|
|
|
2013-02-07 14:03:17 +01:00
|
|
|
if (!on_left(r->o_cm.map[oc]) && !on_right(r->o_cm.map[oc]))
|
2007-11-11 02:30:59 +00:00
|
|
|
continue;
|
|
|
|
|
|
2013-02-07 14:03:17 +01:00
|
|
|
if (front_rear_side(r->i_cm.map[ic]) == front_rear_side(r->o_cm.map[oc])) {
|
|
|
|
|
found_frs[ic] = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2007-11-11 02:30:59 +00:00
|
|
|
}
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2013-02-07 14:03:17 +01:00
|
|
|
for (oc = 0; oc < n_oc; oc++) {
|
2007-11-11 02:30:59 +00:00
|
|
|
|
2013-02-07 14:03:17 +01:00
|
|
|
if (!on_left(r->o_cm.map[oc]) && !on_right(r->o_cm.map[oc]))
|
2007-11-11 02:30:59 +00:00
|
|
|
continue;
|
|
|
|
|
|
2013-02-07 14:03:17 +01:00
|
|
|
if (!found_frs[ic] || front_rear_side(r->i_cm.map[ic]) == front_rear_side(r->o_cm.map[oc]))
|
|
|
|
|
ncenter[oc]++;
|
2007-11-11 02:30:59 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-07 14:03:17 +01:00
|
|
|
for (oc = 0; oc < n_oc; oc++) {
|
2008-12-17 19:53:58 +01:00
|
|
|
|
2013-02-07 14:03:17 +01:00
|
|
|
if (!on_left(r->o_cm.map[oc]) && !on_right(r->o_cm.map[oc]))
|
|
|
|
|
continue;
|
2007-11-11 02:30:59 +00:00
|
|
|
|
2013-02-07 14:03:17 +01:00
|
|
|
if (ncenter[oc] <= 0)
|
|
|
|
|
continue;
|
2007-11-11 02:30:59 +00:00
|
|
|
|
2009-08-20 17:54:45 +02:00
|
|
|
for (ic = 0; ic < n_ic; ic++) {
|
2008-12-17 19:53:58 +01:00
|
|
|
|
|
|
|
|
if (!on_center(r->i_cm.map[ic]))
|
|
|
|
|
continue;
|
|
|
|
|
|
2013-02-07 14:03:17 +01:00
|
|
|
if (!found_frs[ic] || front_rear_side(r->i_cm.map[ic]) == front_rear_side(r->o_cm.map[oc]))
|
|
|
|
|
m->map_table_f[oc][ic] = .5f / (float) ncenter[oc];
|
2007-11-11 02:30:59 +00:00
|
|
|
}
|
|
|
|
|
}
|
2006-01-27 16:25:31 +00:00
|
|
|
}
|
2013-02-07 14:03:17 +01:00
|
|
|
}
|
2006-01-27 16:25:31 +00:00
|
|
|
|
2013-02-07 14:03:17 +01:00
|
|
|
for (oc = 0; oc < n_oc; oc++) {
|
|
|
|
|
float sum = 0.0f;
|
|
|
|
|
for (ic = 0; ic < n_ic; ic++)
|
|
|
|
|
sum += m->map_table_f[oc][ic];
|
2007-11-11 02:30:59 +00:00
|
|
|
|
2013-02-07 14:03:17 +01:00
|
|
|
if (sum > 1.0f)
|
|
|
|
|
for (ic = 0; ic < n_ic; ic++)
|
|
|
|
|
m->map_table_f[oc][ic] /= sum;
|
2007-11-11 02:30:59 +00:00
|
|
|
}
|
2013-02-07 14:03:16 +01:00
|
|
|
|
2009-08-17 11:35:47 +02:00
|
|
|
/* make an 16:16 int version of the matrix */
|
2009-08-20 17:54:45 +02:00
|
|
|
for (oc = 0; oc < n_oc; oc++)
|
|
|
|
|
for (ic = 0; ic < n_ic; ic++)
|
|
|
|
|
m->map_table_i[oc][ic] = (int32_t) (m->map_table_f[oc][ic] * 0x10000);
|
2007-11-11 02:30:59 +00:00
|
|
|
|
|
|
|
|
s = pa_strbuf_new();
|
|
|
|
|
|
|
|
|
|
pa_strbuf_printf(s, " ");
|
2009-08-20 17:54:45 +02:00
|
|
|
for (ic = 0; ic < n_ic; ic++)
|
2007-11-11 02:30:59 +00:00
|
|
|
pa_strbuf_printf(s, " I%02u ", ic);
|
|
|
|
|
pa_strbuf_puts(s, "\n +");
|
|
|
|
|
|
2009-08-20 17:54:45 +02:00
|
|
|
for (ic = 0; ic < n_ic; ic++)
|
2007-11-11 02:30:59 +00:00
|
|
|
pa_strbuf_printf(s, "------");
|
|
|
|
|
pa_strbuf_puts(s, "\n");
|
|
|
|
|
|
2009-08-20 17:54:45 +02:00
|
|
|
for (oc = 0; oc < n_oc; oc++) {
|
2007-11-11 02:30:59 +00:00
|
|
|
pa_strbuf_printf(s, "O%02u |", oc);
|
|
|
|
|
|
2009-08-20 17:54:45 +02:00
|
|
|
for (ic = 0; ic < n_ic; ic++)
|
|
|
|
|
pa_strbuf_printf(s, " %1.3f", m->map_table_f[oc][ic]);
|
2007-11-11 02:30:59 +00:00
|
|
|
|
|
|
|
|
pa_strbuf_puts(s, "\n");
|
2006-01-27 16:25:31 +00:00
|
|
|
}
|
2007-11-11 02:30:59 +00:00
|
|
|
|
|
|
|
|
pa_log_debug("Channel matrix:\n%s", t = pa_strbuf_tostring_free(s));
|
|
|
|
|
pa_xfree(t);
|
2009-08-19 16:15:18 +02:00
|
|
|
|
2009-08-20 18:23:42 +02:00
|
|
|
/* initialize the remapping function */
|
2011-03-02 12:41:26 +01:00
|
|
|
pa_init_remap(m);
|
2004-11-20 16:23:53 +00:00
|
|
|
}
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
static pa_memchunk* convert_to_work_format(pa_resampler *r, pa_memchunk *input) {
|
2006-01-27 16:25:31 +00:00
|
|
|
unsigned n_samples;
|
2007-10-28 19:13:50 +00:00
|
|
|
void *src, *dst;
|
2004-07-02 18:47:03 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(r);
|
|
|
|
|
pa_assert(input);
|
|
|
|
|
pa_assert(input->memblock);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2012-05-10 09:19:22 +03:00
|
|
|
/* Convert the incoming sample into the work sample format and place them
|
|
|
|
|
* in to_work_format_buf. */
|
2004-07-02 18:47:03 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
if (!r->to_work_format_func || !input->length)
|
2006-01-27 16:25:31 +00:00
|
|
|
return input;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2008-08-19 22:39:54 +02:00
|
|
|
n_samples = (unsigned) ((input->length / r->i_fz) * r->i_ss.channels);
|
2004-09-10 22:35:12 +00:00
|
|
|
|
2012-05-10 09:19:22 +03:00
|
|
|
r->to_work_format_buf.index = 0;
|
|
|
|
|
r->to_work_format_buf.length = r->w_sz * n_samples;
|
2006-08-19 02:23:52 +00:00
|
|
|
|
2012-05-10 09:19:22 +03:00
|
|
|
if (!r->to_work_format_buf.memblock || r->to_work_format_buf_samples < n_samples) {
|
|
|
|
|
if (r->to_work_format_buf.memblock)
|
|
|
|
|
pa_memblock_unref(r->to_work_format_buf.memblock);
|
2007-10-28 19:13:50 +00:00
|
|
|
|
2012-05-10 09:19:22 +03:00
|
|
|
r->to_work_format_buf_samples = n_samples;
|
|
|
|
|
r->to_work_format_buf.memblock = pa_memblock_new(r->mempool, r->to_work_format_buf.length);
|
2006-08-19 02:23:52 +00:00
|
|
|
}
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2012-08-17 18:09:34 +03:00
|
|
|
src = pa_memblock_acquire_chunk(input);
|
|
|
|
|
dst = pa_memblock_acquire(r->to_work_format_buf.memblock);
|
2007-10-28 19:13:50 +00:00
|
|
|
|
|
|
|
|
r->to_work_format_func(n_samples, src, dst);
|
|
|
|
|
|
|
|
|
|
pa_memblock_release(input->memblock);
|
2012-05-10 09:19:22 +03:00
|
|
|
pa_memblock_release(r->to_work_format_buf.memblock);
|
2004-09-10 22:35:12 +00:00
|
|
|
|
2012-05-10 09:19:22 +03:00
|
|
|
return &r->to_work_format_buf;
|
2006-01-27 16:25:31 +00:00
|
|
|
}
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
static pa_memchunk *remap_channels(pa_resampler *r, pa_memchunk *input) {
|
2012-05-10 09:19:23 +03:00
|
|
|
unsigned in_n_samples, out_n_samples, in_n_frames, out_n_frames;
|
2007-10-28 19:13:50 +00:00
|
|
|
void *src, *dst;
|
2012-05-10 09:19:23 +03:00
|
|
|
size_t leftover_length = 0;
|
2013-02-07 14:03:15 +01:00
|
|
|
bool have_leftover;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(r);
|
|
|
|
|
pa_assert(input);
|
|
|
|
|
pa_assert(input->memblock);
|
2006-01-27 16:25:31 +00:00
|
|
|
|
2012-05-10 09:19:23 +03:00
|
|
|
/* Remap channels and place the result in remap_buf. There may be leftover
|
|
|
|
|
* data in the beginning of remap_buf. The leftover data is already
|
|
|
|
|
* remapped, so it's not part of the input, it's part of the output. */
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2012-05-10 09:19:23 +03:00
|
|
|
have_leftover = r->remap_buf_contains_leftover_data;
|
2013-02-07 14:03:15 +01:00
|
|
|
r->remap_buf_contains_leftover_data = false;
|
2012-05-10 09:19:23 +03:00
|
|
|
|
|
|
|
|
if (!have_leftover && (!r->map_required || input->length <= 0))
|
2006-01-27 16:25:31 +00:00
|
|
|
return input;
|
2012-05-10 09:19:23 +03:00
|
|
|
else if (input->length <= 0)
|
|
|
|
|
return &r->remap_buf;
|
2004-07-02 18:47:03 +00:00
|
|
|
|
2008-08-19 22:39:54 +02:00
|
|
|
in_n_samples = (unsigned) (input->length / r->w_sz);
|
2012-05-10 09:19:23 +03:00
|
|
|
in_n_frames = out_n_frames = in_n_samples / r->i_ss.channels;
|
2007-10-28 19:13:50 +00:00
|
|
|
|
2012-05-10 09:19:23 +03:00
|
|
|
if (have_leftover) {
|
|
|
|
|
leftover_length = r->remap_buf.length;
|
|
|
|
|
out_n_frames += leftover_length / (r->w_sz * r->o_ss.channels);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
out_n_samples = out_n_frames * r->o_ss.channels;
|
|
|
|
|
r->remap_buf.length = out_n_samples * r->w_sz;
|
|
|
|
|
|
|
|
|
|
if (have_leftover) {
|
|
|
|
|
if (r->remap_buf_size < r->remap_buf.length) {
|
|
|
|
|
pa_memblock *new_block = pa_memblock_new(r->mempool, r->remap_buf.length);
|
|
|
|
|
|
|
|
|
|
src = pa_memblock_acquire(r->remap_buf.memblock);
|
|
|
|
|
dst = pa_memblock_acquire(new_block);
|
|
|
|
|
memcpy(dst, src, leftover_length);
|
|
|
|
|
pa_memblock_release(r->remap_buf.memblock);
|
|
|
|
|
pa_memblock_release(new_block);
|
2004-09-10 22:35:12 +00:00
|
|
|
|
2012-05-10 09:19:22 +03:00
|
|
|
pa_memblock_unref(r->remap_buf.memblock);
|
2012-05-10 09:19:23 +03:00
|
|
|
r->remap_buf.memblock = new_block;
|
|
|
|
|
r->remap_buf_size = r->remap_buf.length;
|
|
|
|
|
}
|
2006-08-19 02:23:52 +00:00
|
|
|
|
2012-05-10 09:19:23 +03:00
|
|
|
} else {
|
|
|
|
|
if (!r->remap_buf.memblock || r->remap_buf_size < r->remap_buf.length) {
|
|
|
|
|
if (r->remap_buf.memblock)
|
|
|
|
|
pa_memblock_unref(r->remap_buf.memblock);
|
|
|
|
|
|
|
|
|
|
r->remap_buf_size = r->remap_buf.length;
|
|
|
|
|
r->remap_buf.memblock = pa_memblock_new(r->mempool, r->remap_buf.length);
|
|
|
|
|
}
|
2006-08-19 02:23:52 +00:00
|
|
|
}
|
2004-09-10 22:35:12 +00:00
|
|
|
|
2012-08-17 18:09:34 +03:00
|
|
|
src = pa_memblock_acquire_chunk(input);
|
2012-05-10 09:19:23 +03:00
|
|
|
dst = (uint8_t *) pa_memblock_acquire(r->remap_buf.memblock) + leftover_length;
|
|
|
|
|
|
|
|
|
|
if (r->map_required) {
|
|
|
|
|
pa_remap_t *remap = &r->remap;
|
2004-07-02 18:47:03 +00:00
|
|
|
|
2012-05-10 09:19:23 +03:00
|
|
|
pa_assert(remap->do_remap);
|
|
|
|
|
remap->do_remap(remap, dst, src, in_n_frames);
|
2009-08-20 17:54:45 +02:00
|
|
|
|
2012-05-10 09:19:23 +03:00
|
|
|
} else
|
|
|
|
|
memcpy(dst, src, input->length);
|
2004-07-02 18:47:03 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_memblock_release(input->memblock);
|
2012-05-10 09:19:22 +03:00
|
|
|
pa_memblock_release(r->remap_buf.memblock);
|
2007-10-28 19:13:50 +00:00
|
|
|
|
2012-05-10 09:19:22 +03:00
|
|
|
return &r->remap_buf;
|
2006-01-27 16:25:31 +00:00
|
|
|
}
|
2004-07-02 18:47:03 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
static pa_memchunk *resample(pa_resampler *r, pa_memchunk *input) {
|
|
|
|
|
unsigned in_n_frames, in_n_samples;
|
2006-01-27 16:25:31 +00:00
|
|
|
unsigned out_n_frames, out_n_samples;
|
2004-07-02 18:47:03 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(r);
|
|
|
|
|
pa_assert(input);
|
2006-01-27 16:25:31 +00:00
|
|
|
|
2012-05-10 09:19:22 +03:00
|
|
|
/* Resample the data and place the result in resample_buf. */
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
if (!r->impl_resample || !input->length)
|
2006-01-27 16:25:31 +00:00
|
|
|
return input;
|
|
|
|
|
|
2008-08-19 22:39:54 +02:00
|
|
|
in_n_samples = (unsigned) (input->length / r->w_sz);
|
2013-07-10 21:24:04 +03:00
|
|
|
in_n_frames = (unsigned) (in_n_samples / r->o_ss.channels);
|
2007-10-28 19:13:50 +00:00
|
|
|
|
2008-05-15 23:34:41 +00:00
|
|
|
out_n_frames = ((in_n_frames*r->o_ss.rate)/r->i_ss.rate)+EXTRA_FRAMES;
|
2013-07-10 21:24:04 +03:00
|
|
|
out_n_samples = out_n_frames * r->o_ss.channels;
|
2006-01-27 16:25:31 +00:00
|
|
|
|
2012-05-10 09:19:22 +03:00
|
|
|
r->resample_buf.index = 0;
|
|
|
|
|
r->resample_buf.length = r->w_sz * out_n_samples;
|
2007-10-28 19:13:50 +00:00
|
|
|
|
2012-05-10 09:19:22 +03:00
|
|
|
if (!r->resample_buf.memblock || r->resample_buf_samples < out_n_samples) {
|
|
|
|
|
if (r->resample_buf.memblock)
|
|
|
|
|
pa_memblock_unref(r->resample_buf.memblock);
|
2006-08-19 02:23:52 +00:00
|
|
|
|
2012-05-10 09:19:22 +03:00
|
|
|
r->resample_buf_samples = out_n_samples;
|
|
|
|
|
r->resample_buf.memblock = pa_memblock_new(r->mempool, r->resample_buf.length);
|
2006-08-19 02:23:52 +00:00
|
|
|
}
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2012-05-10 09:19:22 +03:00
|
|
|
r->impl_resample(r, input, in_n_frames, &r->resample_buf, &out_n_frames);
|
2013-07-10 21:24:04 +03:00
|
|
|
r->resample_buf.length = out_n_frames * r->w_sz * r->o_ss.channels;
|
2006-01-27 16:25:31 +00:00
|
|
|
|
2012-05-10 09:19:22 +03:00
|
|
|
return &r->resample_buf;
|
2007-10-28 19:13:50 +00:00
|
|
|
}
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
static pa_memchunk *convert_from_work_format(pa_resampler *r, pa_memchunk *input) {
|
|
|
|
|
unsigned n_samples, n_frames;
|
|
|
|
|
void *src, *dst;
|
|
|
|
|
|
|
|
|
|
pa_assert(r);
|
|
|
|
|
pa_assert(input);
|
|
|
|
|
|
2012-05-10 09:19:22 +03:00
|
|
|
/* Convert the data into the correct sample type and place the result in
|
|
|
|
|
* from_work_format_buf. */
|
2007-10-28 19:13:50 +00:00
|
|
|
|
|
|
|
|
if (!r->from_work_format_func || !input->length)
|
|
|
|
|
return input;
|
|
|
|
|
|
2008-08-19 22:39:54 +02:00
|
|
|
n_samples = (unsigned) (input->length / r->w_sz);
|
|
|
|
|
n_frames = n_samples / r->o_ss.channels;
|
2007-10-28 19:13:50 +00:00
|
|
|
|
2012-05-10 09:19:22 +03:00
|
|
|
r->from_work_format_buf.index = 0;
|
|
|
|
|
r->from_work_format_buf.length = r->o_fz * n_frames;
|
2007-10-28 19:13:50 +00:00
|
|
|
|
2012-05-10 09:19:22 +03:00
|
|
|
if (!r->from_work_format_buf.memblock || r->from_work_format_buf_samples < n_samples) {
|
|
|
|
|
if (r->from_work_format_buf.memblock)
|
|
|
|
|
pa_memblock_unref(r->from_work_format_buf.memblock);
|
2007-10-28 19:13:50 +00:00
|
|
|
|
2012-05-10 09:19:22 +03:00
|
|
|
r->from_work_format_buf_samples = n_samples;
|
|
|
|
|
r->from_work_format_buf.memblock = pa_memblock_new(r->mempool, r->from_work_format_buf.length);
|
2007-10-28 19:13:50 +00:00
|
|
|
}
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2012-08-17 18:09:34 +03:00
|
|
|
src = pa_memblock_acquire_chunk(input);
|
2012-05-10 09:19:22 +03:00
|
|
|
dst = pa_memblock_acquire(r->from_work_format_buf.memblock);
|
2007-10-28 19:13:50 +00:00
|
|
|
r->from_work_format_func(n_samples, src, dst);
|
|
|
|
|
pa_memblock_release(input->memblock);
|
2012-05-10 09:19:22 +03:00
|
|
|
pa_memblock_release(r->from_work_format_buf.memblock);
|
2006-01-27 16:25:31 +00:00
|
|
|
|
2012-05-10 09:19:22 +03:00
|
|
|
return &r->from_work_format_buf;
|
2006-01-27 16:25:31 +00:00
|
|
|
}
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
void pa_resampler_run(pa_resampler *r, const pa_memchunk *in, pa_memchunk *out) {
|
|
|
|
|
pa_memchunk *buf;
|
|
|
|
|
|
|
|
|
|
pa_assert(r);
|
|
|
|
|
pa_assert(in);
|
|
|
|
|
pa_assert(out);
|
|
|
|
|
pa_assert(in->length);
|
|
|
|
|
pa_assert(in->memblock);
|
|
|
|
|
pa_assert(in->length % r->i_fz == 0);
|
|
|
|
|
|
|
|
|
|
buf = (pa_memchunk*) in;
|
|
|
|
|
buf = convert_to_work_format(r, buf);
|
2013-07-10 21:24:04 +03:00
|
|
|
buf = remap_channels(r, buf);
|
|
|
|
|
buf = resample(r, buf);
|
2007-10-28 19:13:50 +00:00
|
|
|
|
|
|
|
|
if (buf->length) {
|
|
|
|
|
buf = convert_from_work_format(r, buf);
|
|
|
|
|
*out = *buf;
|
|
|
|
|
|
|
|
|
|
if (buf == in)
|
|
|
|
|
pa_memblock_ref(buf->memblock);
|
|
|
|
|
else
|
|
|
|
|
pa_memchunk_reset(buf);
|
|
|
|
|
} else
|
|
|
|
|
pa_memchunk_reset(out);
|
|
|
|
|
}
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2012-05-10 09:19:23 +03:00
|
|
|
static void save_leftover(pa_resampler *r, void *buf, size_t len) {
|
|
|
|
|
void *dst;
|
|
|
|
|
|
|
|
|
|
pa_assert(r);
|
|
|
|
|
pa_assert(buf);
|
|
|
|
|
pa_assert(len > 0);
|
|
|
|
|
|
|
|
|
|
/* Store the leftover to remap_buf. */
|
|
|
|
|
|
|
|
|
|
r->remap_buf.length = len;
|
|
|
|
|
|
|
|
|
|
if (!r->remap_buf.memblock || r->remap_buf_size < r->remap_buf.length) {
|
|
|
|
|
if (r->remap_buf.memblock)
|
|
|
|
|
pa_memblock_unref(r->remap_buf.memblock);
|
|
|
|
|
|
|
|
|
|
r->remap_buf_size = r->remap_buf.length;
|
|
|
|
|
r->remap_buf.memblock = pa_memblock_new(r->mempool, r->remap_buf.length);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dst = pa_memblock_acquire(r->remap_buf.memblock);
|
|
|
|
|
memcpy(dst, buf, r->remap_buf.length);
|
|
|
|
|
pa_memblock_release(r->remap_buf.memblock);
|
|
|
|
|
|
2013-02-07 14:03:15 +01:00
|
|
|
r->remap_buf_contains_leftover_data = true;
|
2012-05-10 09:19:23 +03:00
|
|
|
}
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
/*** libsamplerate based implementation ***/
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
#ifdef HAVE_LIBSAMPLERATE
|
|
|
|
|
static void libsamplerate_resample(pa_resampler *r, const pa_memchunk *input, unsigned in_n_frames, pa_memchunk *output, unsigned *out_n_frames) {
|
|
|
|
|
SRC_DATA data;
|
2013-06-18 14:22:26 +02:00
|
|
|
struct src_data *libsamplerate_data;
|
2006-01-27 16:25:31 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(r);
|
|
|
|
|
pa_assert(input);
|
|
|
|
|
pa_assert(output);
|
|
|
|
|
pa_assert(out_n_frames);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2013-06-18 14:22:26 +02:00
|
|
|
libsamplerate_data = r->impl_data;
|
2007-10-28 19:13:50 +00:00
|
|
|
memset(&data, 0, sizeof(data));
|
2006-01-27 16:25:31 +00:00
|
|
|
|
2012-08-17 18:09:34 +03:00
|
|
|
data.data_in = pa_memblock_acquire_chunk(input);
|
2008-08-20 03:33:06 +03:00
|
|
|
data.input_frames = (long int) in_n_frames;
|
2006-08-19 02:23:52 +00:00
|
|
|
|
2012-08-17 18:09:34 +03:00
|
|
|
data.data_out = pa_memblock_acquire_chunk(output);
|
2008-08-20 03:33:06 +03:00
|
|
|
data.output_frames = (long int) *out_n_frames;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
data.src_ratio = (double) r->o_ss.rate / r->i_ss.rate;
|
|
|
|
|
data.end_of_input = 0;
|
2006-11-06 13:06:01 +00:00
|
|
|
|
2013-06-18 14:22:26 +02:00
|
|
|
pa_assert_se(src_process(libsamplerate_data->state, &data) == 0);
|
2012-05-10 09:19:23 +03:00
|
|
|
|
|
|
|
|
if (data.input_frames_used < in_n_frames) {
|
2013-07-10 21:24:04 +03:00
|
|
|
void *leftover_data = data.data_in + data.input_frames_used * r->o_ss.channels;
|
|
|
|
|
size_t leftover_length = (in_n_frames - data.input_frames_used) * sizeof(float) * r->o_ss.channels;
|
2012-05-10 09:19:23 +03:00
|
|
|
|
|
|
|
|
save_leftover(r, leftover_data, leftover_length);
|
|
|
|
|
}
|
2007-10-28 19:13:50 +00:00
|
|
|
|
|
|
|
|
pa_memblock_release(input->memblock);
|
|
|
|
|
pa_memblock_release(output->memblock);
|
|
|
|
|
|
2008-08-19 22:39:54 +02:00
|
|
|
*out_n_frames = (unsigned) data.output_frames_gen;
|
2004-07-02 18:47:03 +00:00
|
|
|
}
|
2004-09-14 17:52:11 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
static void libsamplerate_update_rates(pa_resampler *r) {
|
2013-06-18 14:22:26 +02:00
|
|
|
struct src_data *libsamplerate_data;
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(r);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2013-06-18 14:22:26 +02:00
|
|
|
libsamplerate_data = r->impl_data;
|
|
|
|
|
pa_assert_se(src_set_ratio(libsamplerate_data->state, (double) r->o_ss.rate / r->i_ss.rate) == 0);
|
2007-10-28 19:13:50 +00:00
|
|
|
}
|
2006-01-27 16:25:31 +00:00
|
|
|
|
2007-12-23 20:15:03 +00:00
|
|
|
static void libsamplerate_reset(pa_resampler *r) {
|
2013-06-18 14:22:26 +02:00
|
|
|
struct src_data *libsamplerate_data;
|
2007-12-23 20:15:03 +00:00
|
|
|
pa_assert(r);
|
|
|
|
|
|
2013-06-18 14:22:26 +02:00
|
|
|
libsamplerate_data = r->impl_data;
|
|
|
|
|
pa_assert_se(src_reset(libsamplerate_data->state) == 0);
|
2007-12-23 20:15:03 +00:00
|
|
|
}
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
static void libsamplerate_free(pa_resampler *r) {
|
2013-06-18 14:22:26 +02:00
|
|
|
struct src_data *libsamplerate_data;
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(r);
|
|
|
|
|
|
2013-06-18 14:22:26 +02:00
|
|
|
libsamplerate_data = r->impl_data;
|
|
|
|
|
if (libsamplerate_data->state)
|
|
|
|
|
src_delete(libsamplerate_data->state);
|
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) {
|
2004-11-20 16:23:53 +00:00
|
|
|
int err;
|
2013-06-18 14:22:26 +02:00
|
|
|
struct src_data *libsamplerate_data;
|
2004-11-20 16:23:53 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(r);
|
2006-01-27 16:25:31 +00:00
|
|
|
|
2013-06-18 14:22:26 +02:00
|
|
|
libsamplerate_data = pa_xnew(struct src_data, 1);
|
|
|
|
|
|
|
|
|
|
if (!(libsamplerate_data->state = src_new(r->method, r->o_ss.channels, &err)))
|
2007-10-28 19:13:50 +00:00
|
|
|
return -1;
|
2004-11-20 16:23:53 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
r->impl_free = libsamplerate_free;
|
|
|
|
|
r->impl_update_rates = libsamplerate_update_rates;
|
|
|
|
|
r->impl_resample = libsamplerate_resample;
|
2007-12-23 20:15:03 +00:00
|
|
|
r->impl_reset = libsamplerate_reset;
|
2013-06-18 14:22:26 +02:00
|
|
|
r->impl_data = libsamplerate_data;
|
2006-01-27 16:25:31 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2004-11-20 16:23:53 +00:00
|
|
|
|
2011-12-11 16:07:41 +01:00
|
|
|
#ifdef HAVE_SPEEX
|
2007-10-28 19:13:50 +00:00
|
|
|
/*** speex based implementation ***/
|
2004-11-20 16:23:53 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
static void 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;
|
2013-06-18 14:22:26 +02:00
|
|
|
struct speex_data *speex_data;
|
2006-01-27 16:25:31 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(r);
|
|
|
|
|
pa_assert(input);
|
|
|
|
|
pa_assert(output);
|
|
|
|
|
pa_assert(out_n_frames);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2013-06-18 14:22:26 +02:00
|
|
|
speex_data = r->impl_data;
|
|
|
|
|
|
2012-08-17 18:09:34 +03:00
|
|
|
in = pa_memblock_acquire_chunk(input);
|
|
|
|
|
out = pa_memblock_acquire_chunk(output);
|
2004-11-20 16:23:53 +00:00
|
|
|
|
2013-06-18 14:22:26 +02:00
|
|
|
pa_assert_se(speex_resampler_process_interleaved_float(speex_data->state, in, &inf, out, &outf) == 0);
|
2007-10-28 19:13:50 +00:00
|
|
|
|
|
|
|
|
pa_memblock_release(input->memblock);
|
|
|
|
|
pa_memblock_release(output->memblock);
|
|
|
|
|
|
|
|
|
|
pa_assert(inf == in_n_frames);
|
|
|
|
|
*out_n_frames = outf;
|
2004-11-20 16:23:53 +00:00
|
|
|
}
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
static void 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;
|
2013-06-18 14:22:26 +02:00
|
|
|
struct speex_data *speex_data;
|
2004-11-20 16:23:53 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(r);
|
|
|
|
|
pa_assert(input);
|
|
|
|
|
pa_assert(output);
|
|
|
|
|
pa_assert(out_n_frames);
|
|
|
|
|
|
2013-06-18 14:22:26 +02:00
|
|
|
speex_data = r->impl_data;
|
|
|
|
|
|
2012-08-17 18:09:34 +03:00
|
|
|
in = pa_memblock_acquire_chunk(input);
|
|
|
|
|
out = pa_memblock_acquire_chunk(output);
|
2007-10-28 19:13:50 +00:00
|
|
|
|
2013-06-18 14:22:26 +02:00
|
|
|
pa_assert_se(speex_resampler_process_interleaved_int(speex_data->state, in, &inf, out, &outf) == 0);
|
2007-10-28 19:13:50 +00:00
|
|
|
|
|
|
|
|
pa_memblock_release(input->memblock);
|
|
|
|
|
pa_memblock_release(output->memblock);
|
2006-01-27 16:25:31 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(inf == in_n_frames);
|
|
|
|
|
*out_n_frames = outf;
|
|
|
|
|
}
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
static void speex_update_rates(pa_resampler *r) {
|
2013-06-18 14:22:26 +02:00
|
|
|
struct speex_data *speex_data;
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(r);
|
2004-11-20 16:23:53 +00:00
|
|
|
|
2013-06-18 14:22:26 +02:00
|
|
|
speex_data = r->impl_data;
|
|
|
|
|
|
|
|
|
|
pa_assert_se(speex_resampler_set_rate(speex_data->state, r->i_ss.rate, r->o_ss.rate) == 0);
|
2007-10-28 19:13:50 +00:00
|
|
|
}
|
2004-11-20 16:23:53 +00:00
|
|
|
|
2007-12-23 20:15:03 +00:00
|
|
|
static void speex_reset(pa_resampler *r) {
|
2013-06-18 14:22:26 +02:00
|
|
|
struct speex_data *speex_data;
|
2007-12-23 20:15:03 +00:00
|
|
|
pa_assert(r);
|
|
|
|
|
|
2013-06-18 14:22:26 +02:00
|
|
|
speex_data = r->impl_data;
|
|
|
|
|
|
|
|
|
|
pa_assert_se(speex_resampler_reset_mem(speex_data->state) == 0);
|
2007-12-23 20:15:03 +00:00
|
|
|
}
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
static void speex_free(pa_resampler *r) {
|
2013-06-18 14:22:26 +02:00
|
|
|
struct speex_data *speex_data;
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(r);
|
2004-11-20 16:23:53 +00:00
|
|
|
|
2013-06-18 14:22:26 +02:00
|
|
|
speex_data = r->impl_data;
|
|
|
|
|
if (!speex_data->state)
|
2007-10-28 19:13:50 +00:00
|
|
|
return;
|
|
|
|
|
|
2013-06-18 14:22:26 +02:00
|
|
|
speex_resampler_destroy(speex_data->state);
|
2007-10-28 19:13:50 +00:00
|
|
|
}
|
2004-11-20 16:23:53 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
static int speex_init(pa_resampler *r) {
|
|
|
|
|
int q, err;
|
2013-06-18 14:22:26 +02:00
|
|
|
struct speex_data *speex_data;
|
2004-11-20 16:23:53 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(r);
|
|
|
|
|
|
2013-06-18 14:22:26 +02:00
|
|
|
speex_data = pa_xnew(struct speex_data, 1);
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
r->impl_free = speex_free;
|
|
|
|
|
r->impl_update_rates = speex_update_rates;
|
2007-12-23 20:15:03 +00:00
|
|
|
r->impl_reset = speex_reset;
|
2013-06-18 14:22:26 +02:00
|
|
|
r->impl_data = speex_data;
|
2007-10-28 19:13:50 +00:00
|
|
|
|
2007-11-11 02:30:59 +00:00
|
|
|
if (r->method >= PA_RESAMPLER_SPEEX_FIXED_BASE && r->method <= PA_RESAMPLER_SPEEX_FIXED_MAX) {
|
2007-10-28 19:13:50 +00:00
|
|
|
|
2008-06-28 02:20:14 +02:00
|
|
|
q = r->method - PA_RESAMPLER_SPEEX_FIXED_BASE;
|
2007-10-28 19:13:50 +00:00
|
|
|
r->impl_resample = speex_resample_int;
|
2008-06-28 02:20:14 +02:00
|
|
|
|
2004-11-20 16:23:53 +00:00
|
|
|
} else {
|
2007-11-11 02:30:59 +00:00
|
|
|
pa_assert(r->method >= PA_RESAMPLER_SPEEX_FLOAT_BASE && r->method <= PA_RESAMPLER_SPEEX_FLOAT_MAX);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2008-06-28 02:20:14 +02:00
|
|
|
q = r->method - PA_RESAMPLER_SPEEX_FLOAT_BASE;
|
2007-10-28 19:13:50 +00:00
|
|
|
r->impl_resample = speex_resample_float;
|
|
|
|
|
}
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2008-06-28 02:20:14 +02:00
|
|
|
pa_log_info("Choosing speex quality setting %i.", q);
|
|
|
|
|
|
2013-06-18 14:22:26 +02:00
|
|
|
if (!(speex_data->state = speex_resampler_init(r->o_ss.channels, r->i_ss.rate, r->o_ss.rate, q, &err)))
|
2008-06-28 02:20:14 +02:00
|
|
|
return -1;
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|
2011-12-11 16:07:41 +01:00
|
|
|
#endif
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
/* Trivial implementation */
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
static void trivial_resample(pa_resampler *r, const pa_memchunk *input, unsigned in_n_frames, pa_memchunk *output, unsigned *out_n_frames) {
|
|
|
|
|
size_t fz;
|
2011-11-23 11:40:07 +01:00
|
|
|
unsigned i_index, o_index;
|
2007-10-28 19:13:50 +00:00
|
|
|
void *src, *dst;
|
2013-06-18 14:22:26 +02:00
|
|
|
struct trivial_data *trivial_data;
|
2004-11-27 18:50:29 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(r);
|
|
|
|
|
pa_assert(input);
|
|
|
|
|
pa_assert(output);
|
|
|
|
|
pa_assert(out_n_frames);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2013-06-18 14:22:26 +02:00
|
|
|
trivial_data = r->impl_data;
|
2013-07-10 21:24:04 +03:00
|
|
|
fz = r->w_sz * r->o_ss.channels;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2012-08-17 18:09:34 +03:00
|
|
|
src = pa_memblock_acquire_chunk(input);
|
|
|
|
|
dst = pa_memblock_acquire_chunk(output);
|
2007-10-28 19:13:50 +00:00
|
|
|
|
2013-06-18 14:22:26 +02:00
|
|
|
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;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2011-11-23 11:40:07 +01:00
|
|
|
if (i_index >= in_n_frames)
|
2007-10-28 19:13:50 +00:00
|
|
|
break;
|
|
|
|
|
|
2011-11-23 11:40:05 +01:00
|
|
|
pa_assert_fp(o_index * fz < pa_memblock_get_length(output->memblock));
|
2007-10-28 19:13:50 +00:00
|
|
|
|
2012-03-23 09:24:48 +00:00
|
|
|
memcpy((uint8_t*) dst + fz * o_index, (uint8_t*) src + fz * i_index, (int) fz);
|
2004-11-20 16:23:53 +00:00
|
|
|
}
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_memblock_release(input->memblock);
|
|
|
|
|
pa_memblock_release(output->memblock);
|
|
|
|
|
|
|
|
|
|
*out_n_frames = o_index;
|
|
|
|
|
|
2013-06-18 14:22:26 +02:00
|
|
|
trivial_data->i_counter += in_n_frames;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2004-11-27 18:50:29 +00:00
|
|
|
/* Normalize counters */
|
2013-06-18 14:22:26 +02:00
|
|
|
while (trivial_data->i_counter >= r->i_ss.rate) {
|
|
|
|
|
pa_assert(trivial_data->o_counter >= r->o_ss.rate);
|
2007-10-28 19:13:50 +00:00
|
|
|
|
2013-06-18 14:22:26 +02:00
|
|
|
trivial_data->i_counter -= r->i_ss.rate;
|
|
|
|
|
trivial_data->o_counter -= r->o_ss.rate;
|
2004-11-27 18:50:29 +00:00
|
|
|
}
|
2004-11-20 16:23:53 +00:00
|
|
|
}
|
|
|
|
|
|
2007-12-23 20:15:03 +00:00
|
|
|
static void trivial_update_rates_or_reset(pa_resampler *r) {
|
2013-06-18 14:22:26 +02:00
|
|
|
struct trivial_data *trivial_data;
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(r);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2013-06-18 14:22:26 +02:00
|
|
|
trivial_data = r->impl_data;
|
|
|
|
|
|
|
|
|
|
trivial_data->i_counter = 0;
|
|
|
|
|
trivial_data->o_counter = 0;
|
2004-11-20 16:23:53 +00:00
|
|
|
}
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
static int trivial_init(pa_resampler*r) {
|
2013-06-18 14:22:26 +02:00
|
|
|
struct trivial_data *trivial_data;
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(r);
|
|
|
|
|
|
2013-06-18 14:22:26 +02:00
|
|
|
trivial_data = pa_xnew0(struct trivial_data, 1);
|
2006-01-27 16:25:31 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
r->impl_resample = trivial_resample;
|
2007-12-23 20:15:03 +00:00
|
|
|
r->impl_update_rates = trivial_update_rates_or_reset;
|
|
|
|
|
r->impl_reset = trivial_update_rates_or_reset;
|
2013-06-18 14:22:26 +02:00
|
|
|
r->impl_data = trivial_data;
|
2004-11-20 16:23:53 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
return 0;
|
2004-11-20 16:23:53 +00:00
|
|
|
}
|
|
|
|
|
|
2008-05-15 23:34:41 +00:00
|
|
|
/* Peak finder implementation */
|
|
|
|
|
|
|
|
|
|
static void peaks_resample(pa_resampler *r, const pa_memchunk *input, unsigned in_n_frames, pa_memchunk *output, unsigned *out_n_frames) {
|
2011-11-02 21:54:16 +01:00
|
|
|
unsigned c, o_index = 0;
|
|
|
|
|
unsigned i, i_end = 0;
|
2008-05-15 23:34:41 +00:00
|
|
|
void *src, *dst;
|
2013-06-18 14:22:26 +02:00
|
|
|
struct peaks_data *peaks_data;
|
2008-05-15 23:34:41 +00:00
|
|
|
|
|
|
|
|
pa_assert(r);
|
|
|
|
|
pa_assert(input);
|
|
|
|
|
pa_assert(output);
|
|
|
|
|
pa_assert(out_n_frames);
|
|
|
|
|
|
2013-06-18 14:22:26 +02:00
|
|
|
peaks_data = r->impl_data;
|
2012-08-17 18:09:34 +03:00
|
|
|
src = pa_memblock_acquire_chunk(input);
|
|
|
|
|
dst = pa_memblock_acquire_chunk(output);
|
2008-05-15 23:34:41 +00:00
|
|
|
|
2013-06-18 14:22:26 +02:00
|
|
|
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;
|
2008-05-15 23:34:41 +00:00
|
|
|
|
2011-11-02 21:54:16 +01:00
|
|
|
while (i_end < in_n_frames) {
|
2013-06-18 14:22:26 +02:00
|
|
|
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;
|
2008-05-15 23:34:41 +00:00
|
|
|
|
2011-11-28 10:36:34 +01:00
|
|
|
pa_assert_fp(o_index * r->w_sz * r->o_ss.channels < pa_memblock_get_length(output->memblock));
|
2011-11-02 21:54:17 +01:00
|
|
|
|
|
|
|
|
/* 1ch float is treated separately, because that is the common case */
|
|
|
|
|
if (r->o_ss.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++);
|
|
|
|
|
|
2013-06-18 14:22:26 +02:00
|
|
|
if (n > peaks_data->max_f[0])
|
|
|
|
|
peaks_data->max_f[0] = n;
|
2011-11-02 21:54:17 +01:00
|
|
|
}
|
2008-05-15 23:34:41 +00:00
|
|
|
|
2011-11-02 21:54:17 +01:00
|
|
|
if (i == i_end) {
|
2013-06-18 14:22:26 +02:00
|
|
|
*d = peaks_data->max_f[0];
|
|
|
|
|
peaks_data->max_f[0] = 0;
|
|
|
|
|
o_index++, peaks_data->o_counter++;
|
2011-11-02 21:54:17 +01:00
|
|
|
}
|
|
|
|
|
} else if (r->work_format == PA_SAMPLE_S16NE) {
|
2013-07-10 21:48:40 +03:00
|
|
|
int16_t *s = (int16_t*) src + r->o_ss.channels * i;
|
2011-11-28 10:36:34 +01:00
|
|
|
int16_t *d = (int16_t*) dst + r->o_ss.channels * o_index;
|
2008-05-15 23:34:41 +00:00
|
|
|
|
2011-11-02 21:54:16 +01:00
|
|
|
for (; i < i_end && i < in_n_frames; i++)
|
2011-11-02 21:54:17 +01:00
|
|
|
for (c = 0; c < r->o_ss.channels; c++) {
|
|
|
|
|
int16_t n = abs(*s++);
|
2008-05-15 23:34:41 +00:00
|
|
|
|
2013-06-18 14:22:26 +02:00
|
|
|
if (n > peaks_data->max_i[c])
|
|
|
|
|
peaks_data->max_i[c] = n;
|
2008-05-15 23:34:41 +00:00
|
|
|
}
|
|
|
|
|
|
2011-11-02 21:54:16 +01:00
|
|
|
if (i == i_end) {
|
|
|
|
|
for (c = 0; c < r->o_ss.channels; c++, d++) {
|
2013-06-18 14:22:26 +02:00
|
|
|
*d = peaks_data->max_i[c];
|
|
|
|
|
peaks_data->max_i[c] = 0;
|
2011-11-02 21:54:16 +01:00
|
|
|
}
|
2013-06-18 14:22:26 +02:00
|
|
|
o_index++, peaks_data->o_counter++;
|
2008-05-17 09:10:16 +00:00
|
|
|
}
|
2008-05-15 23:34:41 +00:00
|
|
|
} else {
|
2013-07-10 21:48:40 +03:00
|
|
|
float *s = (float*) src + r->o_ss.channels * i;
|
2011-11-28 10:36:34 +01:00
|
|
|
float *d = (float*) dst + r->o_ss.channels * o_index;
|
2008-05-15 23:34:41 +00:00
|
|
|
|
2011-11-02 21:54:16 +01:00
|
|
|
for (; i < i_end && i < in_n_frames; i++)
|
2011-11-02 21:54:17 +01:00
|
|
|
for (c = 0; c < r->o_ss.channels; c++) {
|
|
|
|
|
float n = fabsf(*s++);
|
2008-05-15 23:34:41 +00:00
|
|
|
|
2013-06-18 14:22:26 +02:00
|
|
|
if (n > peaks_data->max_f[c])
|
|
|
|
|
peaks_data->max_f[c] = n;
|
2008-05-15 23:34:41 +00:00
|
|
|
}
|
|
|
|
|
|
2011-11-02 21:54:16 +01:00
|
|
|
if (i == i_end) {
|
|
|
|
|
for (c = 0; c < r->o_ss.channels; c++, d++) {
|
2013-06-18 14:22:26 +02:00
|
|
|
*d = peaks_data->max_f[c];
|
|
|
|
|
peaks_data->max_f[c] = 0;
|
2011-11-02 21:54:16 +01:00
|
|
|
}
|
2013-06-18 14:22:26 +02:00
|
|
|
o_index++, peaks_data->o_counter++;
|
2008-05-17 09:10:16 +00:00
|
|
|
}
|
2008-05-15 23:34:41 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pa_memblock_release(input->memblock);
|
|
|
|
|
pa_memblock_release(output->memblock);
|
|
|
|
|
|
|
|
|
|
*out_n_frames = o_index;
|
|
|
|
|
|
2013-06-18 14:22:26 +02:00
|
|
|
peaks_data->i_counter += in_n_frames;
|
2008-05-15 23:34:41 +00:00
|
|
|
|
|
|
|
|
/* Normalize counters */
|
2013-06-18 14:22:26 +02:00
|
|
|
while (peaks_data->i_counter >= r->i_ss.rate) {
|
|
|
|
|
pa_assert(peaks_data->o_counter >= r->o_ss.rate);
|
2008-05-15 23:34:41 +00:00
|
|
|
|
2013-06-18 14:22:26 +02:00
|
|
|
peaks_data->i_counter -= r->i_ss.rate;
|
|
|
|
|
peaks_data->o_counter -= r->o_ss.rate;
|
2008-05-15 23:34:41 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void peaks_update_rates_or_reset(pa_resampler *r) {
|
2013-06-18 14:22:26 +02:00
|
|
|
struct peaks_data *peaks_data;
|
2008-05-15 23:34:41 +00:00
|
|
|
pa_assert(r);
|
|
|
|
|
|
2013-06-18 14:22:26 +02:00
|
|
|
peaks_data = r->impl_data;
|
|
|
|
|
|
|
|
|
|
peaks_data->i_counter = 0;
|
|
|
|
|
peaks_data->o_counter = 0;
|
2008-05-15 23:34:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int peaks_init(pa_resampler*r) {
|
2013-06-18 14:22:26 +02:00
|
|
|
struct peaks_data *peaks_data;
|
2008-05-15 23:34:41 +00:00
|
|
|
pa_assert(r);
|
2011-12-13 09:26:44 +05:30
|
|
|
pa_assert(r->i_ss.rate >= r->o_ss.rate);
|
|
|
|
|
pa_assert(r->work_format == PA_SAMPLE_S16NE || r->work_format == PA_SAMPLE_FLOAT32NE);
|
2008-05-15 23:34:41 +00:00
|
|
|
|
2013-06-18 14:22:26 +02:00
|
|
|
peaks_data = pa_xnew0(struct peaks_data, 1);
|
2008-05-15 23:34:41 +00:00
|
|
|
|
|
|
|
|
r->impl_resample = peaks_resample;
|
|
|
|
|
r->impl_update_rates = peaks_update_rates_or_reset;
|
|
|
|
|
r->impl_reset = peaks_update_rates_or_reset;
|
2013-06-18 14:22:26 +02:00
|
|
|
r->impl_data = peaks_data;
|
2008-05-15 23:34:41 +00:00
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
/*** ffmpeg based implementation ***/
|
|
|
|
|
|
|
|
|
|
static void 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;
|
2012-05-10 09:19:23 +03:00
|
|
|
int previous_consumed_frames = -1;
|
2013-06-18 14:22:26 +02:00
|
|
|
struct ffmpeg_data *ffmpeg_data;
|
2007-10-28 19:13:50 +00:00
|
|
|
|
|
|
|
|
pa_assert(r);
|
|
|
|
|
pa_assert(input);
|
|
|
|
|
pa_assert(output);
|
|
|
|
|
pa_assert(out_n_frames);
|
|
|
|
|
|
2013-06-18 14:22:26 +02:00
|
|
|
ffmpeg_data = r->impl_data;
|
|
|
|
|
|
2013-07-10 21:24:04 +03:00
|
|
|
for (c = 0; c < r->o_ss.channels; c++) {
|
2007-10-28 19:13:50 +00:00
|
|
|
unsigned u;
|
|
|
|
|
pa_memblock *b, *w;
|
|
|
|
|
int16_t *p, *t, *k, *q, *s;
|
|
|
|
|
int consumed_frames;
|
|
|
|
|
|
|
|
|
|
/* Allocate a new block */
|
2013-06-18 14:22:26 +02:00
|
|
|
b = pa_memblock_new(r->mempool, ffmpeg_data->buf[c].length + in_n_frames * sizeof(int16_t));
|
2007-10-28 19:13:50 +00:00
|
|
|
p = pa_memblock_acquire(b);
|
|
|
|
|
|
2012-05-10 09:19:23 +03:00
|
|
|
/* Now copy the input data, splitting up channels */
|
2012-08-17 18:09:34 +03:00
|
|
|
t = (int16_t*) pa_memblock_acquire_chunk(input) + c;
|
|
|
|
|
k = p;
|
2007-10-28 19:13:50 +00:00
|
|
|
for (u = 0; u < in_n_frames; u++) {
|
|
|
|
|
*k = *t;
|
2013-07-10 21:24:04 +03:00
|
|
|
t += r->o_ss.channels;
|
2007-10-28 19:13:50 +00:00
|
|
|
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 */
|
2013-06-18 14:22:26 +02:00
|
|
|
used_frames = (unsigned) av_resample(ffmpeg_data->state,
|
2008-08-19 22:39:54 +02:00
|
|
|
q, p,
|
|
|
|
|
&consumed_frames,
|
2012-05-10 09:19:23 +03:00
|
|
|
(int) in_n_frames, (int) *out_n_frames,
|
2013-07-10 21:24:04 +03:00
|
|
|
c >= (unsigned) (r->o_ss.channels-1));
|
2007-10-28 19:13:50 +00:00
|
|
|
|
|
|
|
|
pa_memblock_release(b);
|
2012-05-10 09:19:23 +03:00
|
|
|
pa_memblock_unref(b);
|
2007-10-28 19:13:50 +00:00
|
|
|
|
2012-05-10 09:19:23 +03:00
|
|
|
pa_assert(consumed_frames <= (int) in_n_frames);
|
|
|
|
|
pa_assert(previous_consumed_frames == -1 || consumed_frames == previous_consumed_frames);
|
|
|
|
|
previous_consumed_frames = consumed_frames;
|
2007-10-28 19:13:50 +00:00
|
|
|
|
|
|
|
|
/* And place the results in the output buffer */
|
2012-08-17 18:09:34 +03:00
|
|
|
s = (int16_t *) pa_memblock_acquire_chunk(output) + c;
|
2007-10-28 19:13:50 +00:00
|
|
|
for (u = 0; u < used_frames; u++) {
|
|
|
|
|
*s = *q;
|
|
|
|
|
q++;
|
2013-07-10 21:24:04 +03:00
|
|
|
s += r->o_ss.channels;
|
2007-10-28 19:13:50 +00:00
|
|
|
}
|
|
|
|
|
pa_memblock_release(output->memblock);
|
|
|
|
|
pa_memblock_release(w);
|
|
|
|
|
pa_memblock_unref(w);
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-10 09:19:23 +03:00
|
|
|
if (previous_consumed_frames < (int) in_n_frames) {
|
2012-08-17 18:09:34 +03:00
|
|
|
void *leftover_data = (int16_t *) pa_memblock_acquire_chunk(input) + previous_consumed_frames * r->o_ss.channels;
|
2012-05-10 09:19:23 +03:00
|
|
|
size_t leftover_length = (in_n_frames - previous_consumed_frames) * r->o_ss.channels * sizeof(int16_t);
|
|
|
|
|
|
|
|
|
|
save_leftover(r, leftover_data, leftover_length);
|
|
|
|
|
pa_memblock_release(input->memblock);
|
|
|
|
|
}
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
*out_n_frames = used_frames;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void ffmpeg_free(pa_resampler *r) {
|
|
|
|
|
unsigned c;
|
2013-06-18 14:22:26 +02:00
|
|
|
struct ffmpeg_data *ffmpeg_data;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(r);
|
2004-11-20 16:23:53 +00:00
|
|
|
|
2013-06-18 14:22:26 +02:00
|
|
|
ffmpeg_data = r->impl_data;
|
|
|
|
|
if (ffmpeg_data->state)
|
|
|
|
|
av_resample_close(ffmpeg_data->state);
|
2004-11-20 16:23:53 +00:00
|
|
|
|
2013-06-18 14:22:26 +02:00
|
|
|
for (c = 0; c < PA_ELEMENTSOF(ffmpeg_data->buf); c++)
|
|
|
|
|
if (ffmpeg_data->buf[c].memblock)
|
|
|
|
|
pa_memblock_unref(ffmpeg_data->buf[c].memblock);
|
2007-10-28 19:13:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int ffmpeg_init(pa_resampler *r) {
|
|
|
|
|
unsigned c;
|
2013-06-18 14:22:26 +02:00
|
|
|
struct ffmpeg_data *ffmpeg_data;
|
2007-10-28 19:13:50 +00:00
|
|
|
|
|
|
|
|
pa_assert(r);
|
|
|
|
|
|
2013-06-18 14:22:26 +02:00
|
|
|
ffmpeg_data = pa_xnew(struct ffmpeg_data, 1);
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
/* 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. */
|
|
|
|
|
|
2013-06-18 14:22:26 +02:00
|
|
|
if (!(ffmpeg_data->state = av_resample_init((int) r->o_ss.rate, (int) r->i_ss.rate, 16, 10, 0, 0.8)))
|
2007-10-28 19:13:50 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
|
|
r->impl_free = ffmpeg_free;
|
|
|
|
|
r->impl_resample = ffmpeg_resample;
|
2013-06-18 14:22:26 +02:00
|
|
|
r->impl_data = (void *) ffmpeg_data;
|
2007-10-28 19:13:50 +00:00
|
|
|
|
2013-06-18 14:22:26 +02:00
|
|
|
for (c = 0; c < PA_ELEMENTSOF(ffmpeg_data->buf); c++)
|
|
|
|
|
pa_memchunk_reset(&ffmpeg_data->buf[c]);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2004-11-20 16:23:53 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
/*** copy (noop) implementation ***/
|
2004-11-20 16:23:53 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
static int copy_init(pa_resampler *r) {
|
|
|
|
|
pa_assert(r);
|
|
|
|
|
|
|
|
|
|
pa_assert(r->o_ss.rate == r->i_ss.rate);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|