[pulseaudio-commits] 11 commits - src/pulsecore

Tanu Kaskinen tanuk at kemper.freedesktop.org
Tue Aug 27 05:14:47 PDT 2013


 src/pulsecore/resampler.c |  497 +++++++++++++++++++++++++++++-----------------
 src/pulsecore/resampler.h |    9 
 2 files changed, 329 insertions(+), 177 deletions(-)

New commits:
commit 9a590dd3f258bb7ea420a36196e4c4a7e549753a
Author: poljar (Damir Jelić) <poljarinho at gmail.com>
Date:   Wed Aug 7 19:55:44 2013 +0200

    resampler: Add a choose_auto_resampler function
    
    This function returns our preferred resampler if the user choose the
    auto (or if he has chosen an unsupported) resampler.

diff --git a/src/pulsecore/resampler.c b/src/pulsecore/resampler.c
index b2238ff..5599035 100644
--- a/src/pulsecore/resampler.c
+++ b/src/pulsecore/resampler.c
@@ -176,6 +176,19 @@ static int (* const init_table[])(pa_resampler*r) = {
     [PA_RESAMPLER_PEAKS]                   = peaks_init,
 };
 
+static pa_resample_method_t choose_auto_resampler(pa_resample_flags_t flags) {
+    pa_resample_method_t method;
+
+    if (pa_resample_method_supported(PA_RESAMPLER_SPEEX_FLOAT_BASE + 1))
+        method = PA_RESAMPLER_SPEEX_FLOAT_BASE + 1;
+    else if (flags & PA_RESAMPLER_VARIABLE_RATE)
+        method = PA_RESAMPLER_TRIVIAL;
+    else
+        method = PA_RESAMPLER_FFMPEG;
+
+    return method;
+}
+
 static pa_resample_method_t pa_resampler_fix_method(
                 pa_resample_flags_t flags,
                 pa_resample_method_t method,
@@ -224,16 +237,8 @@ static pa_resample_method_t pa_resampler_fix_method(
             break;
     }
 
-    if (method == PA_RESAMPLER_AUTO) {
-#ifdef HAVE_SPEEX
-        method = PA_RESAMPLER_SPEEX_FLOAT_BASE + 1;
-#else
-        if (flags & PA_RESAMPLER_VARIABLE_RATE)
-            method = PA_RESAMPLER_TRIVIAL;
-        else
-            method = PA_RESAMPLER_FFMPEG;
-#endif
-    }
+    if (method == PA_RESAMPLER_AUTO)
+        method = choose_auto_resampler(flags);
 
     return method;
 }

commit 8ab6c37e10bdcd1f3d069f2cd00a816a44280b67
Author: poljar (Damir Jelić) <poljarinho at gmail.com>
Date:   Wed Aug 7 19:37:55 2013 +0200

    resampler: Get rid of redundant implementation specific structs
    
    This patch removes implementation specific structs that contain a single
    member.

diff --git a/src/pulsecore/resampler.c b/src/pulsecore/resampler.c
index e5c1bb0..b2238ff 100644
--- a/src/pulsecore/resampler.c
+++ b/src/pulsecore/resampler.c
@@ -90,18 +90,6 @@ struct peaks_data { /* data specific to the peak finder pseudo resampler */
     int16_t max_i[PA_CHANNELS_MAX];
 };
 
-#ifdef HAVE_LIBSAMPLERATE
-struct src_data { /* data specific to libsamplerate */
-    SRC_STATE *state;
-};
-#endif
-
-#ifdef HAVE_SPEEX
-struct speex_data { /* data specific to speex */
-    SpeexResamplerState* state;
-};
-#endif
-
 struct ffmpeg_data { /* data specific to ffmpeg */
     struct AVResampleContext *state;
     pa_memchunk buf[PA_CHANNELS_MAX];
@@ -443,6 +431,8 @@ void pa_resampler_free(pa_resampler *r) {
 
     if (r->impl.free)
         r->impl.free(r);
+    else
+        pa_xfree(r->impl.data);
 
     if (r->to_work_format_buf.memblock)
         pa_memblock_unref(r->to_work_format_buf.memblock);
@@ -453,7 +443,6 @@ void pa_resampler_free(pa_resampler *r) {
     if (r->from_work_format_buf.memblock)
         pa_memblock_unref(r->from_work_format_buf.memblock);
 
-    pa_xfree(r->impl.data);
     pa_xfree(r);
 }
 
@@ -1357,14 +1346,14 @@ static void save_leftover(pa_resampler *r, void *buf, size_t len) {
 #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;
-    struct src_data *libsamplerate_data;
+    SRC_STATE *state;
 
     pa_assert(r);
     pa_assert(input);
     pa_assert(output);
     pa_assert(out_n_frames);
 
-    libsamplerate_data = r->impl.data;
+    state = r->impl.data;
     memset(&data, 0, sizeof(data));
 
     data.data_in = pa_memblock_acquire_chunk(input);
@@ -1376,7 +1365,7 @@ static void libsamplerate_resample(pa_resampler *r, const pa_memchunk *input, un
     data.src_ratio = (double) r->o_ss.rate / r->i_ss.rate;
     data.end_of_input = 0;
 
-    pa_assert_se(src_process(libsamplerate_data->state, &data) == 0);
+    pa_assert_se(src_process(state, &data) == 0);
 
     if (data.input_frames_used < in_n_frames) {
         void *leftover_data = data.data_in + data.input_frames_used * r->o_ss.channels;
@@ -1392,46 +1381,44 @@ static void libsamplerate_resample(pa_resampler *r, const pa_memchunk *input, un
 }
 
 static void libsamplerate_update_rates(pa_resampler *r) {
-    struct src_data *libsamplerate_data;
+    SRC_STATE *state;
     pa_assert(r);
 
-    libsamplerate_data = r->impl.data;
-    pa_assert_se(src_set_ratio(libsamplerate_data->state, (double) r->o_ss.rate / r->i_ss.rate) == 0);
+    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) {
-    struct src_data *libsamplerate_data;
+    SRC_STATE *state;
     pa_assert(r);
 
-    libsamplerate_data = r->impl.data;
-    pa_assert_se(src_reset(libsamplerate_data->state) == 0);
+    state = r->impl.data;
+    pa_assert_se(src_reset(state) == 0);
 }
 
 static void libsamplerate_free(pa_resampler *r) {
-    struct src_data *libsamplerate_data;
+    SRC_STATE *state;
     pa_assert(r);
 
-    libsamplerate_data = r->impl.data;
-    if (libsamplerate_data->state)
-        src_delete(libsamplerate_data->state);
+    state = r->impl.data;
+    if (state)
+        src_delete(state);
 }
 
 static int libsamplerate_init(pa_resampler *r) {
     int err;
-    struct src_data *libsamplerate_data;
+    SRC_STATE *state;
 
     pa_assert(r);
 
-    libsamplerate_data = pa_xnew(struct src_data, 1);
-
-    if (!(libsamplerate_data->state = src_new(r->method, r->o_ss.channels, &err)))
+    if (!(state = src_new(r->method, r->o_ss.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 = libsamplerate_data;
+    r->impl.data = state;
 
     return 0;
 }
@@ -1443,19 +1430,19 @@ static int libsamplerate_init(pa_resampler *r) {
 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;
-    struct speex_data *speex_data;
+    SpeexResamplerState *state;
 
     pa_assert(r);
     pa_assert(input);
     pa_assert(output);
     pa_assert(out_n_frames);
 
-    speex_data = r->impl.data;
+    state = r->impl.data;
 
     in = pa_memblock_acquire_chunk(input);
     out = pa_memblock_acquire_chunk(output);
 
-    pa_assert_se(speex_resampler_process_interleaved_float(speex_data->state, in, &inf, out, &outf) == 0);
+    pa_assert_se(speex_resampler_process_interleaved_float(state, in, &inf, out, &outf) == 0);
 
     pa_memblock_release(input->memblock);
     pa_memblock_release(output->memblock);
@@ -1467,19 +1454,19 @@ static void speex_resample_float(pa_resampler *r, const pa_memchunk *input, unsi
 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;
-    struct speex_data *speex_data;
+    SpeexResamplerState *state;
 
     pa_assert(r);
     pa_assert(input);
     pa_assert(output);
     pa_assert(out_n_frames);
 
-    speex_data = r->impl.data;
+    state = r->impl.data;
 
     in = pa_memblock_acquire_chunk(input);
     out = pa_memblock_acquire_chunk(output);
 
-    pa_assert_se(speex_resampler_process_interleaved_int(speex_data->state, in, &inf, out, &outf) == 0);
+    pa_assert_se(speex_resampler_process_interleaved_int(state, in, &inf, out, &outf) == 0);
 
     pa_memblock_release(input->memblock);
     pa_memblock_release(output->memblock);
@@ -1489,46 +1476,43 @@ static void speex_resample_int(pa_resampler *r, const pa_memchunk *input, unsign
 }
 
 static void speex_update_rates(pa_resampler *r) {
-    struct speex_data *speex_data;
+    SpeexResamplerState *state;
     pa_assert(r);
 
-    speex_data = r->impl.data;
+    state = r->impl.data;
 
-    pa_assert_se(speex_resampler_set_rate(speex_data->state, r->i_ss.rate, r->o_ss.rate) == 0);
+    pa_assert_se(speex_resampler_set_rate(state, r->i_ss.rate, r->o_ss.rate) == 0);
 }
 
 static void speex_reset(pa_resampler *r) {
-    struct speex_data *speex_data;
+    SpeexResamplerState *state;
     pa_assert(r);
 
-    speex_data = r->impl.data;
+    state = r->impl.data;
 
-    pa_assert_se(speex_resampler_reset_mem(speex_data->state) == 0);
+    pa_assert_se(speex_resampler_reset_mem(state) == 0);
 }
 
 static void speex_free(pa_resampler *r) {
-    struct speex_data *speex_data;
+    SpeexResamplerState *state;
     pa_assert(r);
 
-    speex_data = r->impl.data;
-    if (!speex_data->state)
+    state = r->impl.data;
+    if (!state)
         return;
 
-    speex_resampler_destroy(speex_data->state);
+    speex_resampler_destroy(state);
 }
 
 static int speex_init(pa_resampler *r) {
     int q, err;
-    struct speex_data *speex_data;
+    SpeexResamplerState *state;
 
     pa_assert(r);
 
-    speex_data = pa_xnew(struct speex_data, 1);
-
     r->impl.free = speex_free;
     r->impl.update_rates = speex_update_rates;
     r->impl.reset = speex_reset;
-    r->impl.data = speex_data;
 
     if (r->method >= PA_RESAMPLER_SPEEX_FIXED_BASE && r->method <= PA_RESAMPLER_SPEEX_FIXED_MAX) {
 
@@ -1544,9 +1528,11 @@ static int speex_init(pa_resampler *r) {
 
     pa_log_info("Choosing speex quality setting %i.", q);
 
-    if (!(speex_data->state = speex_resampler_init(r->o_ss.channels, r->i_ss.rate, r->o_ss.rate, q, &err)))
+    if (!(state = speex_resampler_init(r->o_ss.channels, r->i_ss.rate, r->o_ss.rate, q, &err)))
         return -1;
 
+    r->impl.data = state;
+
     return 0;
 }
 #endif

commit 0d525e9c85c1ebd2ed4ab809330c7605b5a8fbb4
Author: poljar (Damir Jelić) <poljarinho at gmail.com>
Date:   Wed Jul 10 13:10:04 2013 +0200

    resampler: Add assert to set_input/output_rate
    
    This adds asserts to check if the implementation has an update rate
    function defined for the unlikely event that some implementation forgets
    to assign a update rate function we can simply bail.
    
    It is expected from the resampling implementations to have such a
    function even if the state of the resampler is completely reset.

diff --git a/src/pulsecore/resampler.c b/src/pulsecore/resampler.c
index 5772684..e5c1bb0 100644
--- a/src/pulsecore/resampler.c
+++ b/src/pulsecore/resampler.c
@@ -460,6 +460,7 @@ void pa_resampler_free(pa_resampler *r) {
 void pa_resampler_set_input_rate(pa_resampler *r, uint32_t rate) {
     pa_assert(r);
     pa_assert(rate > 0);
+    pa_assert(r->impl.update_rates);
 
     if (r->i_ss.rate == rate)
         return;
@@ -472,6 +473,7 @@ void pa_resampler_set_input_rate(pa_resampler *r, uint32_t rate) {
 void pa_resampler_set_output_rate(pa_resampler *r, uint32_t rate) {
     pa_assert(r);
     pa_assert(rate > 0);
+    pa_assert(r->impl.update_rates);
 
     if (r->o_ss.rate == rate)
         return;

commit 4bdfebee5c580e3230a8b1b8311717ff7835c17a
Author: poljar (Damir Jelić) <poljarinho at gmail.com>
Date:   Mon Jun 24 13:36:49 2013 +0200

    resampler: Introduce a implementation specific struct
    
    This struct holds all the implementation specific data in one place.

diff --git a/src/pulsecore/resampler.c b/src/pulsecore/resampler.c
index eda6236..5772684 100644
--- a/src/pulsecore/resampler.c
+++ b/src/pulsecore/resampler.c
@@ -74,11 +74,7 @@ struct pa_resampler {
     pa_remap_t remap;
     bool map_required;
 
-    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);
-    void (*impl_reset)(pa_resampler *r);
-    void *impl_data;
+    pa_resampler_impl impl;
 };
 
 struct trivial_data { /* data specific to the trivial resampler */
@@ -445,8 +441,8 @@ fail:
 void pa_resampler_free(pa_resampler *r) {
     pa_assert(r);
 
-    if (r->impl_free)
-        r->impl_free(r);
+    if (r->impl.free)
+        r->impl.free(r);
 
     if (r->to_work_format_buf.memblock)
         pa_memblock_unref(r->to_work_format_buf.memblock);
@@ -457,7 +453,7 @@ void pa_resampler_free(pa_resampler *r) {
     if (r->from_work_format_buf.memblock)
         pa_memblock_unref(r->from_work_format_buf.memblock);
 
-    pa_xfree(r->impl_data);
+    pa_xfree(r->impl.data);
     pa_xfree(r);
 }
 
@@ -470,7 +466,7 @@ void pa_resampler_set_input_rate(pa_resampler *r, uint32_t rate) {
 
     r->i_ss.rate = rate;
 
-    r->impl_update_rates(r);
+    r->impl.update_rates(r);
 }
 
 void pa_resampler_set_output_rate(pa_resampler *r, uint32_t rate) {
@@ -482,7 +478,7 @@ void pa_resampler_set_output_rate(pa_resampler *r, uint32_t rate) {
 
     r->o_ss.rate = rate;
 
-    r->impl_update_rates(r);
+    r->impl.update_rates(r);
 }
 
 size_t pa_resampler_request(pa_resampler *r, size_t out_length) {
@@ -548,8 +544,8 @@ size_t pa_resampler_max_block_size(pa_resampler *r) {
 void pa_resampler_reset(pa_resampler *r) {
     pa_assert(r);
 
-    if (r->impl_reset)
-        r->impl_reset(r);
+    if (r->impl.reset)
+        r->impl.reset(r);
 
     r->remap_buf_contains_leftover_data = false;
 }
@@ -1239,7 +1235,7 @@ static pa_memchunk *resample(pa_resampler *r, pa_memchunk *input) {
 
     /* Resample the data and place the result in resample_buf. */
 
-    if (!r->impl_resample || !input->length)
+    if (!r->impl.resample || !input->length)
         return input;
 
     in_n_samples = (unsigned) (input->length / r->w_sz);
@@ -1259,7 +1255,7 @@ static pa_memchunk *resample(pa_resampler *r, pa_memchunk *input) {
         r->resample_buf.memblock = pa_memblock_new(r->mempool, r->resample_buf.length);
     }
 
-    r->impl_resample(r, input, in_n_frames, &r->resample_buf, &out_n_frames);
+    r->impl.resample(r, input, in_n_frames, &r->resample_buf, &out_n_frames);
     r->resample_buf.length = out_n_frames * r->w_sz * r->o_ss.channels;
 
     return &r->resample_buf;
@@ -1366,7 +1362,7 @@ static void libsamplerate_resample(pa_resampler *r, const pa_memchunk *input, un
     pa_assert(output);
     pa_assert(out_n_frames);
 
-    libsamplerate_data = r->impl_data;
+    libsamplerate_data = r->impl.data;
     memset(&data, 0, sizeof(data));
 
     data.data_in = pa_memblock_acquire_chunk(input);
@@ -1397,7 +1393,7 @@ static void libsamplerate_update_rates(pa_resampler *r) {
     struct src_data *libsamplerate_data;
     pa_assert(r);
 
-    libsamplerate_data = r->impl_data;
+    libsamplerate_data = r->impl.data;
     pa_assert_se(src_set_ratio(libsamplerate_data->state, (double) r->o_ss.rate / r->i_ss.rate) == 0);
 }
 
@@ -1405,7 +1401,7 @@ static void libsamplerate_reset(pa_resampler *r) {
     struct src_data *libsamplerate_data;
     pa_assert(r);
 
-    libsamplerate_data = r->impl_data;
+    libsamplerate_data = r->impl.data;
     pa_assert_se(src_reset(libsamplerate_data->state) == 0);
 }
 
@@ -1413,7 +1409,7 @@ static void libsamplerate_free(pa_resampler *r) {
     struct src_data *libsamplerate_data;
     pa_assert(r);
 
-    libsamplerate_data = r->impl_data;
+    libsamplerate_data = r->impl.data;
     if (libsamplerate_data->state)
         src_delete(libsamplerate_data->state);
 }
@@ -1429,11 +1425,11 @@ static int libsamplerate_init(pa_resampler *r) {
     if (!(libsamplerate_data->state = src_new(r->method, r->o_ss.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 = libsamplerate_data;
+    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 = libsamplerate_data;
 
     return 0;
 }
@@ -1452,7 +1448,7 @@ static void speex_resample_float(pa_resampler *r, const pa_memchunk *input, unsi
     pa_assert(output);
     pa_assert(out_n_frames);
 
-    speex_data = r->impl_data;
+    speex_data = r->impl.data;
 
     in = pa_memblock_acquire_chunk(input);
     out = pa_memblock_acquire_chunk(output);
@@ -1476,7 +1472,7 @@ static void speex_resample_int(pa_resampler *r, const pa_memchunk *input, unsign
     pa_assert(output);
     pa_assert(out_n_frames);
 
-    speex_data = r->impl_data;
+    speex_data = r->impl.data;
 
     in = pa_memblock_acquire_chunk(input);
     out = pa_memblock_acquire_chunk(output);
@@ -1494,7 +1490,7 @@ static void speex_update_rates(pa_resampler *r) {
     struct speex_data *speex_data;
     pa_assert(r);
 
-    speex_data = r->impl_data;
+    speex_data = r->impl.data;
 
     pa_assert_se(speex_resampler_set_rate(speex_data->state, r->i_ss.rate, r->o_ss.rate) == 0);
 }
@@ -1503,7 +1499,7 @@ static void speex_reset(pa_resampler *r) {
     struct speex_data *speex_data;
     pa_assert(r);
 
-    speex_data = r->impl_data;
+    speex_data = r->impl.data;
 
     pa_assert_se(speex_resampler_reset_mem(speex_data->state) == 0);
 }
@@ -1512,7 +1508,7 @@ static void speex_free(pa_resampler *r) {
     struct speex_data *speex_data;
     pa_assert(r);
 
-    speex_data = r->impl_data;
+    speex_data = r->impl.data;
     if (!speex_data->state)
         return;
 
@@ -1527,21 +1523,21 @@ static int speex_init(pa_resampler *r) {
 
     speex_data = pa_xnew(struct speex_data, 1);
 
-    r->impl_free = speex_free;
-    r->impl_update_rates = speex_update_rates;
-    r->impl_reset = speex_reset;
-    r->impl_data = speex_data;
+    r->impl.free = speex_free;
+    r->impl.update_rates = speex_update_rates;
+    r->impl.reset = speex_reset;
+    r->impl.data = speex_data;
 
     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;
+        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;
+        r->impl.resample = speex_resample_float;
     }
 
     pa_log_info("Choosing speex quality setting %i.", q);
@@ -1566,7 +1562,7 @@ static void trivial_resample(pa_resampler *r, const pa_memchunk *input, unsigned
     pa_assert(output);
     pa_assert(out_n_frames);
 
-    trivial_data = r->impl_data;
+    trivial_data = r->impl.data;
     fz = r->w_sz * r->o_ss.channels;
 
     src = pa_memblock_acquire_chunk(input);
@@ -1604,7 +1600,7 @@ 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 = r->impl.data;
 
     trivial_data->i_counter = 0;
     trivial_data->o_counter = 0;
@@ -1616,10 +1612,10 @@ static int trivial_init(pa_resampler*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;
+    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;
 }
@@ -1637,7 +1633,7 @@ static void peaks_resample(pa_resampler *r, const pa_memchunk *input, unsigned i
     pa_assert(output);
     pa_assert(out_n_frames);
 
-    peaks_data = r->impl_data;
+    peaks_data = r->impl.data;
     src = pa_memblock_acquire_chunk(input);
     dst = pa_memblock_acquire_chunk(output);
 
@@ -1728,7 +1724,7 @@ 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 = r->impl.data;
 
     peaks_data->i_counter = 0;
     peaks_data->o_counter = 0;
@@ -1742,10 +1738,10 @@ static int peaks_init(pa_resampler*r) {
 
     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;
+    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;
 }
@@ -1762,7 +1758,7 @@ static void ffmpeg_resample(pa_resampler *r, const pa_memchunk *input, unsigned
     pa_assert(output);
     pa_assert(out_n_frames);
 
-    ffmpeg_data = r->impl_data;
+    ffmpeg_data = r->impl.data;
 
     for (c = 0; c < r->o_ss.channels; c++) {
         unsigned u;
@@ -1831,7 +1827,7 @@ static void ffmpeg_free(pa_resampler *r) {
 
     pa_assert(r);
 
-    ffmpeg_data = r->impl_data;
+    ffmpeg_data = r->impl.data;
     if (ffmpeg_data->state)
         av_resample_close(ffmpeg_data->state);
 
@@ -1856,9 +1852,9 @@ static int ffmpeg_init(pa_resampler *r) {
     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;
+    r->impl.free = ffmpeg_free;
+    r->impl.resample = ffmpeg_resample;
+    r->impl.data = (void *) ffmpeg_data;
 
     for (c = 0; c < PA_ELEMENTSOF(ffmpeg_data->buf); c++)
         pa_memchunk_reset(&ffmpeg_data->buf[c]);
diff --git a/src/pulsecore/resampler.h b/src/pulsecore/resampler.h
index 742de6a..793b70b 100644
--- a/src/pulsecore/resampler.h
+++ b/src/pulsecore/resampler.h
@@ -28,6 +28,15 @@
 #include <pulsecore/memchunk.h>
 
 typedef struct pa_resampler pa_resampler;
+typedef struct pa_resampler_impl pa_resampler_impl;
+
+struct pa_resampler_impl {
+    void (*free)(pa_resampler *r);
+    void (*update_rates)(pa_resampler *r);
+    void (*resample)(pa_resampler *r, const pa_memchunk *in, unsigned in_samples, pa_memchunk *out, unsigned *out_samples);
+    void (*reset)(pa_resampler *r);
+    void *data;
+};
 
 typedef enum pa_resample_method {
     PA_RESAMPLER_INVALID                 = -1,

commit c5cd65d81e98f96c761803f12fd3ae159fced556
Author: poljar (Damir Jelić) <poljarinho at gmail.com>
Date:   Fri Jun 28 18:55:04 2013 +0200

    Resampler: Don't use the peaks resampler for upsampling
    
    This patch fixes this assertion:
        Assertion 'r->i_ss.rate >= r->o_ss.rate' failed at ../../src/pulsecore/resampler.c:1744, function peaks_init(). Aborting.

diff --git a/src/pulsecore/resampler.c b/src/pulsecore/resampler.c
index 446ddff..eda6236 100644
--- a/src/pulsecore/resampler.c
+++ b/src/pulsecore/resampler.c
@@ -226,6 +226,16 @@ static pa_resample_method_t pa_resampler_fix_method(
                 method = PA_RESAMPLER_AUTO;
             }
             break;
+
+        /* The Peaks resampler only supports downsampling.
+         * Revert to auto if we are upsampling */
+        case PA_RESAMPLER_PEAKS:
+            if (rate_a < rate_b) {
+                pa_log_warn("The 'peaks' resampler only supports downsampling, reverting to resampler 'auto'.");
+                method = PA_RESAMPLER_AUTO;
+            }
+            break;
+
         default:
             break;
     }

commit 2bed8cda25f5c5d06c2fa8aeba92a8ad3e80508e
Author: poljar (Damir Jelić) <poljarinho at gmail.com>
Date:   Fri Jun 28 17:58:22 2013 +0200

    resampler: Add a function for comparing sample format precision

diff --git a/src/pulsecore/resampler.c b/src/pulsecore/resampler.c
index 89ff5a1..446ddff 100644
--- a/src/pulsecore/resampler.c
+++ b/src/pulsecore/resampler.c
@@ -244,6 +244,53 @@ static pa_resample_method_t pa_resampler_fix_method(
     return method;
 }
 
+/* Return true if a is a more precise sample format than b, else return false */
+static bool sample_format_more_precise(pa_sample_format_t a, pa_sample_format_t b) {
+    pa_assert(a >= 0 && a < PA_SAMPLE_MAX);
+    pa_assert(b >= 0 && b < PA_SAMPLE_MAX);
+
+    switch (a) {
+        case PA_SAMPLE_U8:
+        case PA_SAMPLE_ALAW:
+        case PA_SAMPLE_ULAW:
+            return false;
+            break;
+
+        case PA_SAMPLE_S16LE:
+        case PA_SAMPLE_S16BE:
+            if (b == PA_SAMPLE_ULAW || b == PA_SAMPLE_ALAW || b == PA_SAMPLE_U8)
+                return true;
+            else
+                return false;
+            break;
+
+        case PA_SAMPLE_S24LE:
+        case PA_SAMPLE_S24BE:
+        case PA_SAMPLE_S24_32LE:
+        case PA_SAMPLE_S24_32BE:
+            if (b == PA_SAMPLE_ULAW || b == PA_SAMPLE_ALAW || b == PA_SAMPLE_U8 ||
+                b == PA_SAMPLE_S16LE || b == PA_SAMPLE_S16BE)
+                return true;
+            else
+                return false;
+            break;
+
+        case PA_SAMPLE_FLOAT32LE:
+        case PA_SAMPLE_FLOAT32BE:
+        case PA_SAMPLE_S32LE:
+        case PA_SAMPLE_S32BE:
+            if (b == PA_SAMPLE_FLOAT32LE || b == PA_SAMPLE_FLOAT32BE ||
+                b == PA_SAMPLE_S32LE || b == PA_SAMPLE_FLOAT32BE)
+                return false;
+            else
+                return true;
+            break;
+
+        default:
+            return false;
+    }
+}
+
 static pa_sample_format_t pa_resampler_choose_work_format(
                     pa_resample_method_t method,
                     pa_sample_format_t a,
@@ -279,14 +326,8 @@ static pa_sample_format_t pa_resampler_choose_work_format(
         case PA_RESAMPLER_PEAKS:
             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)
+            else if (sample_format_more_precise(a, PA_SAMPLE_S16NE) ||
+                     sample_format_more_precise(b, PA_SAMPLE_S16NE))
                 work_format = PA_SAMPLE_FLOAT32NE;
             else
                 work_format = PA_SAMPLE_S16NE;

commit 7665f60cc08c4b83e79acc96aa3bd7e9c4f1086d
Author: poljar (Damir Jelić) <poljarinho at gmail.com>
Date:   Thu Jul 11 19:28:29 2013 +0200

    resampler: choose_work_format use switch instead of if/else
    
    This way the function for choosing the work format should
    be more readable and easier to expand in the future.

diff --git a/src/pulsecore/resampler.c b/src/pulsecore/resampler.c
index c79a5df..89ff5a1 100644
--- a/src/pulsecore/resampler.c
+++ b/src/pulsecore/resampler.c
@@ -256,13 +256,27 @@ static pa_sample_format_t pa_resampler_choose_work_format(
     pa_assert(method >= 0);
     pa_assert(method < PA_RESAMPLER_MAX);
 
-    if ((method >= PA_RESAMPLER_SPEEX_FIXED_BASE && method <= PA_RESAMPLER_SPEEX_FIXED_MAX) ||
-        (method == PA_RESAMPLER_FFMPEG))
-        work_format = PA_SAMPLE_S16NE;
-    else if (method == PA_RESAMPLER_TRIVIAL || method == PA_RESAMPLER_COPY || method == PA_RESAMPLER_PEAKS) {
+    if (method >= PA_RESAMPLER_SPEEX_FIXED_BASE && method <= PA_RESAMPLER_SPEEX_FIXED_MAX)
+        method = PA_RESAMPLER_SPEEX_FIXED_BASE;
 
-        if (map_required || a != b || method == PA_RESAMPLER_PEAKS) {
+    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;
 
+        /* 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:
             if (a == PA_SAMPLE_S16NE || b == PA_SAMPLE_S16NE)
                 work_format = PA_SAMPLE_S16NE;
             else if (a == PA_SAMPLE_S32NE || a == PA_SAMPLE_S32RE ||
@@ -276,12 +290,11 @@ static pa_sample_format_t pa_resampler_choose_work_format(
                 work_format = PA_SAMPLE_FLOAT32NE;
             else
                 work_format = PA_SAMPLE_S16NE;
+            break;
 
-        } else
-            work_format = a;
-
-    } else
-        work_format = PA_SAMPLE_FLOAT32NE;
+        default:
+            work_format = PA_SAMPLE_FLOAT32NE;
+    }
 
     return work_format;
 }

commit 78c18e71e0ce329be84e40cd1a437c9c78db96b1
Author: poljar (Damir Jelić) <poljarinho at gmail.com>
Date:   Wed Jun 26 16:31:49 2013 +0200

    resampler: Move the work format finding logic into a separate function

diff --git a/src/pulsecore/resampler.c b/src/pulsecore/resampler.c
index fcc52e6..c79a5df 100644
--- a/src/pulsecore/resampler.c
+++ b/src/pulsecore/resampler.c
@@ -244,6 +244,48 @@ static pa_resample_method_t pa_resampler_fix_method(
     return method;
 }
 
+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);
+
+    if ((method >= PA_RESAMPLER_SPEEX_FIXED_BASE && method <= PA_RESAMPLER_SPEEX_FIXED_MAX) ||
+        (method == PA_RESAMPLER_FFMPEG))
+        work_format = PA_SAMPLE_S16NE;
+    else if (method == PA_RESAMPLER_TRIVIAL || method == PA_RESAMPLER_COPY || method == PA_RESAMPLER_PEAKS) {
+
+        if (map_required || a != b || method == PA_RESAMPLER_PEAKS) {
+
+            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;
+
+        } else
+            work_format = a;
+
+    } else
+        work_format = PA_SAMPLE_FLOAT32NE;
+
+    return work_format;
+}
+
 pa_resampler* pa_resampler_new(
         pa_mempool *pool,
         const pa_sample_spec *a,
@@ -296,32 +338,7 @@ pa_resampler* pa_resampler_new(
 
     pa_log_info("Using resampler '%s'", pa_resample_method_to_string(method));
 
-    if ((method >= PA_RESAMPLER_SPEEX_FIXED_BASE && method <= PA_RESAMPLER_SPEEX_FIXED_MAX) ||
-        (method == PA_RESAMPLER_FFMPEG))
-        r->work_format = PA_SAMPLE_S16NE;
-    else if (method == PA_RESAMPLER_TRIVIAL || method == PA_RESAMPLER_COPY || method == PA_RESAMPLER_PEAKS) {
-
-        if (r->map_required || a->format != b->format || method == PA_RESAMPLER_PEAKS) {
-
-            if (a->format == PA_SAMPLE_S16NE || b->format == PA_SAMPLE_S16NE)
-                r->work_format = PA_SAMPLE_S16NE;
-            else if (a->format == PA_SAMPLE_S32NE || a->format == PA_SAMPLE_S32RE ||
-                a->format == PA_SAMPLE_FLOAT32NE || a->format == PA_SAMPLE_FLOAT32RE ||
-                a->format == PA_SAMPLE_S24NE || a->format == PA_SAMPLE_S24RE ||
-                a->format == PA_SAMPLE_S24_32NE || a->format == PA_SAMPLE_S24_32RE ||
-                b->format == PA_SAMPLE_S32NE || b->format == PA_SAMPLE_S32RE ||
-                b->format == PA_SAMPLE_FLOAT32NE || b->format == PA_SAMPLE_FLOAT32RE ||
-                b->format == PA_SAMPLE_S24NE || b->format == PA_SAMPLE_S24RE ||
-                b->format == PA_SAMPLE_S24_32NE || b->format == PA_SAMPLE_S24_32RE)
-                r->work_format = PA_SAMPLE_FLOAT32NE;
-            else
-                r->work_format = PA_SAMPLE_S16NE;
-
-        } else
-            r->work_format = a->format;
-
-    } else
-        r->work_format = PA_SAMPLE_FLOAT32NE;
+    r->work_format = pa_resampler_choose_work_format(method, a->format, b->format, r->map_required);
 
     pa_log_info("Using %s as working format.", pa_sample_format_to_string(r->work_format));
 

commit 83cf44831f821962e9428e8faec9bd7b89ff39fb
Author: poljar (Damir Jelić) <poljarinho at gmail.com>
Date:   Wed Jun 26 16:09:08 2013 +0200

    resampler: fix_method use switch instead of if/else
    
    This way the fix method function should be more readable and easier to
    expand in the future.

diff --git a/src/pulsecore/resampler.c b/src/pulsecore/resampler.c
index 00682c7..fcc52e6 100644
--- a/src/pulsecore/resampler.c
+++ b/src/pulsecore/resampler.c
@@ -213,14 +213,21 @@ static pa_resample_method_t pa_resampler_fix_method(
         method = PA_RESAMPLER_AUTO;
     }
 
-    if (method == PA_RESAMPLER_FFMPEG && (flags & PA_RESAMPLER_VARIABLE_RATE)) {
-        pa_log_info("Resampler 'ffmpeg' cannot do variable rate, reverting to resampler 'auto'.");
-        method = PA_RESAMPLER_AUTO;
-    }
-
-    if (method == PA_RESAMPLER_COPY && ((flags & PA_RESAMPLER_VARIABLE_RATE) || a->rate != b->rate)) {
-        pa_log_info("Resampler 'copy' cannot change sampling rate, reverting to resampler 'auto'.");
-        method = PA_RESAMPLER_AUTO;
+    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;
     }
 
     if (method == PA_RESAMPLER_AUTO) {

commit 2d9aba0946eebeb2134df0a0d91ccb48bc276700
Author: poljar (Damir Jelić) <poljarinho at gmail.com>
Date:   Wed Jun 26 13:51:55 2013 +0200

    resampler: Move the fix method logic into a separate function

diff --git a/src/pulsecore/resampler.c b/src/pulsecore/resampler.c
index 235afdc..00682c7 100644
--- a/src/pulsecore/resampler.c
+++ b/src/pulsecore/resampler.c
@@ -192,28 +192,18 @@ static int (* const init_table[])(pa_resampler*r) = {
     [PA_RESAMPLER_PEAKS]                   = peaks_init,
 };
 
-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));
+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);
     pa_assert(method >= 0);
     pa_assert(method < PA_RESAMPLER_MAX);
 
-    /* Fix method */
-
-    if (!(flags & PA_RESAMPLER_VARIABLE_RATE) && a->rate == b->rate) {
+    if (!(flags & PA_RESAMPLER_VARIABLE_RATE) && rate_a == rate_b) {
         pa_log_info("Forcing resampler 'copy', because of fixed, identical sample rates.");
         method = PA_RESAMPLER_COPY;
     }
@@ -244,6 +234,30 @@ pa_resampler* pa_resampler_new(
 #endif
     }
 
+    return method;
+}
+
+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);
+
     r = pa_xnew0(pa_resampler, 1);
     r->mempool = pool;
     r->method = method;

commit 97feeab40c0a185748810be3e92ff19e99a194c4
Author: poljar (Damir Jelić) <poljarinho at gmail.com>
Date:   Tue Jun 18 14:22:26 2013 +0200

    resampler: Make resampler struct implementation agnostic
    
    The pa_resampler struct contains many implementation specific
    structures. These create overhead and don't belong there anyways.
    
    This patch moves the implementation specific structures out of the
    pa_resampler structure.

diff --git a/src/pulsecore/resampler.c b/src/pulsecore/resampler.c
index 341d85b..235afdc 100644
--- a/src/pulsecore/resampler.c
+++ b/src/pulsecore/resampler.c
@@ -78,37 +78,37 @@ struct pa_resampler {
     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);
     void (*impl_reset)(pa_resampler *r);
+    void *impl_data;
+};
 
-    struct { /* data specific to the trivial resampler */
-        unsigned o_counter;
-        unsigned i_counter;
-    } trivial;
-
-    struct { /* data specific to the peak finder pseudo resampler */
-        unsigned o_counter;
-        unsigned i_counter;
+struct trivial_data { /* data specific to the trivial resampler */
+    unsigned o_counter;
+    unsigned i_counter;
+};
 
-        float max_f[PA_CHANNELS_MAX];
-        int16_t max_i[PA_CHANNELS_MAX];
+struct peaks_data { /* data specific to the peak finder pseudo resampler */
+    unsigned o_counter;
+    unsigned i_counter;
 
-    } peaks;
+    float max_f[PA_CHANNELS_MAX];
+    int16_t max_i[PA_CHANNELS_MAX];
+};
 
 #ifdef HAVE_LIBSAMPLERATE
-    struct { /* data specific to libsamplerate */
-        SRC_STATE *state;
-    } src;
+struct src_data { /* data specific to libsamplerate */
+    SRC_STATE *state;
+};
 #endif
 
 #ifdef HAVE_SPEEX
-    struct { /* data specific to speex */
-        SpeexResamplerState* state;
-    } speex;
+struct speex_data { /* data specific to speex */
+    SpeexResamplerState* state;
+};
 #endif
 
-    struct { /* data specific to ffmpeg */
-        struct AVResampleContext *state;
-        pa_memchunk buf[PA_CHANNELS_MAX];
-    } ffmpeg;
+struct ffmpeg_data { /* data specific to ffmpeg */
+    struct AVResampleContext *state;
+    pa_memchunk buf[PA_CHANNELS_MAX];
 };
 
 static int copy_init(pa_resampler *r);
@@ -355,6 +355,7 @@ void pa_resampler_free(pa_resampler *r) {
     if (r->from_work_format_buf.memblock)
         pa_memblock_unref(r->from_work_format_buf.memblock);
 
+    pa_xfree(r->impl_data);
     pa_xfree(r);
 }
 
@@ -1256,12 +1257,14 @@ static void save_leftover(pa_resampler *r, void *buf, size_t len) {
 #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;
+    struct src_data *libsamplerate_data;
 
     pa_assert(r);
     pa_assert(input);
     pa_assert(output);
     pa_assert(out_n_frames);
 
+    libsamplerate_data = r->impl_data;
     memset(&data, 0, sizeof(data));
 
     data.data_in = pa_memblock_acquire_chunk(input);
@@ -1273,7 +1276,7 @@ static void libsamplerate_resample(pa_resampler *r, const pa_memchunk *input, un
     data.src_ratio = (double) r->o_ss.rate / r->i_ss.rate;
     data.end_of_input = 0;
 
-    pa_assert_se(src_process(r->src.state, &data) == 0);
+    pa_assert_se(src_process(libsamplerate_data->state, &data) == 0);
 
     if (data.input_frames_used < in_n_frames) {
         void *leftover_data = data.data_in + data.input_frames_used * r->o_ss.channels;
@@ -1289,36 +1292,46 @@ static void libsamplerate_resample(pa_resampler *r, const pa_memchunk *input, un
 }
 
 static void libsamplerate_update_rates(pa_resampler *r) {
+    struct src_data *libsamplerate_data;
     pa_assert(r);
 
-    pa_assert_se(src_set_ratio(r->src.state, (double) r->o_ss.rate / r->i_ss.rate) == 0);
+    libsamplerate_data = r->impl_data;
+    pa_assert_se(src_set_ratio(libsamplerate_data->state, (double) r->o_ss.rate / r->i_ss.rate) == 0);
 }
 
 static void libsamplerate_reset(pa_resampler *r) {
+    struct src_data *libsamplerate_data;
     pa_assert(r);
 
-    pa_assert_se(src_reset(r->src.state) == 0);
+    libsamplerate_data = r->impl_data;
+    pa_assert_se(src_reset(libsamplerate_data->state) == 0);
 }
 
 static void libsamplerate_free(pa_resampler *r) {
+    struct src_data *libsamplerate_data;
     pa_assert(r);
 
-    if (r->src.state)
-        src_delete(r->src.state);
+    libsamplerate_data = r->impl_data;
+    if (libsamplerate_data->state)
+        src_delete(libsamplerate_data->state);
 }
 
 static int libsamplerate_init(pa_resampler *r) {
     int err;
+    struct src_data *libsamplerate_data;
 
     pa_assert(r);
 
-    if (!(r->src.state = src_new(r->method, r->o_ss.channels, &err)))
+    libsamplerate_data = pa_xnew(struct src_data, 1);
+
+    if (!(libsamplerate_data->state = src_new(r->method, r->o_ss.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 = libsamplerate_data;
 
     return 0;
 }
@@ -1330,16 +1343,19 @@ static int libsamplerate_init(pa_resampler *r) {
 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;
+    struct speex_data *speex_data;
 
     pa_assert(r);
     pa_assert(input);
     pa_assert(output);
     pa_assert(out_n_frames);
 
+    speex_data = r->impl_data;
+
     in = pa_memblock_acquire_chunk(input);
     out = pa_memblock_acquire_chunk(output);
 
-    pa_assert_se(speex_resampler_process_interleaved_float(r->speex.state, in, &inf, out, &outf) == 0);
+    pa_assert_se(speex_resampler_process_interleaved_float(speex_data->state, in, &inf, out, &outf) == 0);
 
     pa_memblock_release(input->memblock);
     pa_memblock_release(output->memblock);
@@ -1351,16 +1367,19 @@ static void speex_resample_float(pa_resampler *r, const pa_memchunk *input, unsi
 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;
+    struct speex_data *speex_data;
 
     pa_assert(r);
     pa_assert(input);
     pa_assert(output);
     pa_assert(out_n_frames);
 
+    speex_data = r->impl_data;
+
     in = pa_memblock_acquire_chunk(input);
     out = pa_memblock_acquire_chunk(output);
 
-    pa_assert_se(speex_resampler_process_interleaved_int(r->speex.state, in, &inf, out, &outf) == 0);
+    pa_assert_se(speex_resampler_process_interleaved_int(speex_data->state, in, &inf, out, &outf) == 0);
 
     pa_memblock_release(input->memblock);
     pa_memblock_release(output->memblock);
@@ -1370,34 +1389,46 @@ static void speex_resample_int(pa_resampler *r, const pa_memchunk *input, unsign
 }
 
 static void speex_update_rates(pa_resampler *r) {
+    struct speex_data *speex_data;
     pa_assert(r);
 
-    pa_assert_se(speex_resampler_set_rate(r->speex.state, r->i_ss.rate, r->o_ss.rate) == 0);
+    speex_data = r->impl_data;
+
+    pa_assert_se(speex_resampler_set_rate(speex_data->state, r->i_ss.rate, r->o_ss.rate) == 0);
 }
 
 static void speex_reset(pa_resampler *r) {
+    struct speex_data *speex_data;
     pa_assert(r);
 
-    pa_assert_se(speex_resampler_reset_mem(r->speex.state) == 0);
+    speex_data = r->impl_data;
+
+    pa_assert_se(speex_resampler_reset_mem(speex_data->state) == 0);
 }
 
 static void speex_free(pa_resampler *r) {
+    struct speex_data *speex_data;
     pa_assert(r);
 
-    if (!r->speex.state)
+    speex_data = r->impl_data;
+    if (!speex_data->state)
         return;
 
-    speex_resampler_destroy(r->speex.state);
+    speex_resampler_destroy(speex_data->state);
 }
 
 static int speex_init(pa_resampler *r) {
     int q, err;
+    struct speex_data *speex_data;
 
     pa_assert(r);
 
+    speex_data = pa_xnew(struct speex_data, 1);
+
     r->impl_free = speex_free;
     r->impl_update_rates = speex_update_rates;
     r->impl_reset = speex_reset;
+    r->impl_data = speex_data;
 
     if (r->method >= PA_RESAMPLER_SPEEX_FIXED_BASE && r->method <= PA_RESAMPLER_SPEEX_FIXED_MAX) {
 
@@ -1413,7 +1444,7 @@ static int speex_init(pa_resampler *r) {
 
     pa_log_info("Choosing speex quality setting %i.", q);
 
-    if (!(r->speex.state = speex_resampler_init(r->o_ss.channels, r->i_ss.rate, r->o_ss.rate, q, &err)))
+    if (!(speex_data->state = speex_resampler_init(r->o_ss.channels, r->i_ss.rate, r->o_ss.rate, q, &err)))
         return -1;
 
     return 0;
@@ -1426,20 +1457,22 @@ static void trivial_resample(pa_resampler *r, const pa_memchunk *input, unsigned
     size_t fz;
     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;
     fz = r->w_sz * r->o_ss.channels;
 
     src = pa_memblock_acquire_chunk(input);
     dst = pa_memblock_acquire_chunk(output);
 
-    for (o_index = 0;; o_index++, r->trivial.o_counter++) {
-        i_index = ((uint64_t) r->trivial.o_counter * r->i_ss.rate) / r->o_ss.rate;
-        i_index = i_index > r->trivial.i_counter ? i_index - r->trivial.i_counter : 0;
+    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;
@@ -1454,32 +1487,37 @@ static void trivial_resample(pa_resampler *r, const pa_memchunk *input, unsigned
 
     *out_n_frames = o_index;
 
-    r->trivial.i_counter += in_n_frames;
+    trivial_data->i_counter += in_n_frames;
 
     /* Normalize counters */
-    while (r->trivial.i_counter >= r->i_ss.rate) {
-        pa_assert(r->trivial.o_counter >= r->o_ss.rate);
+    while (trivial_data->i_counter >= r->i_ss.rate) {
+        pa_assert(trivial_data->o_counter >= r->o_ss.rate);
 
-        r->trivial.i_counter -= r->i_ss.rate;
-        r->trivial.o_counter -= r->o_ss.rate;
+        trivial_data->i_counter -= r->i_ss.rate;
+        trivial_data->o_counter -= r->o_ss.rate;
     }
 }
 
 static void trivial_update_rates_or_reset(pa_resampler *r) {
+    struct trivial_data *trivial_data;
     pa_assert(r);
 
-    r->trivial.i_counter = 0;
-    r->trivial.o_counter = 0;
+    trivial_data = r->impl_data;
+
+    trivial_data->i_counter = 0;
+    trivial_data->o_counter = 0;
 }
 
 static int trivial_init(pa_resampler*r) {
+    struct trivial_data *trivial_data;
     pa_assert(r);
 
-    r->trivial.o_counter = r->trivial.i_counter = 0;
+    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;
 }
@@ -1490,21 +1528,23 @@ static void peaks_resample(pa_resampler *r, const pa_memchunk *input, unsigned i
     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) r->peaks.o_counter * r->i_ss.rate) / r->o_ss.rate;
-    i = i > r->peaks.i_counter ? i - r->peaks.i_counter : 0;
+    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) (r->peaks.o_counter + 1) * r->i_ss.rate) / r->o_ss.rate;
-        i_end = i_end > r->peaks.i_counter ? i_end - r->peaks.i_counter : 0;
+        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_sz * r->o_ss.channels < pa_memblock_get_length(output->memblock));
 
@@ -1516,14 +1556,14 @@ static void peaks_resample(pa_resampler *r, const pa_memchunk *input, unsigned i
             for (; i < i_end && i < in_n_frames; i++) {
                 float n = fabsf(*s++);
 
-                if (n > r->peaks.max_f[0])
-                    r->peaks.max_f[0] = n;
+                if (n > peaks_data->max_f[0])
+                    peaks_data->max_f[0] = n;
             }
 
             if (i == i_end) {
-                *d = r->peaks.max_f[0];
-                r->peaks.max_f[0] = 0;
-                o_index++, r->peaks.o_counter++;
+                *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->o_ss.channels * i;
@@ -1533,16 +1573,16 @@ static void peaks_resample(pa_resampler *r, const pa_memchunk *input, unsigned i
                 for (c = 0; c < r->o_ss.channels; c++) {
                     int16_t n = abs(*s++);
 
-                    if (n > r->peaks.max_i[c])
-                        r->peaks.max_i[c] = n;
+                    if (n > peaks_data->max_i[c])
+                        peaks_data->max_i[c] = n;
                 }
 
             if (i == i_end) {
                 for (c = 0; c < r->o_ss.channels; c++, d++) {
-                    *d = r->peaks.max_i[c];
-                    r->peaks.max_i[c] = 0;
+                    *d = peaks_data->max_i[c];
+                    peaks_data->max_i[c] = 0;
                 }
-                o_index++, r->peaks.o_counter++;
+                o_index++, peaks_data->o_counter++;
             }
         } else {
             float *s = (float*) src + r->o_ss.channels * i;
@@ -1552,16 +1592,16 @@ static void peaks_resample(pa_resampler *r, const pa_memchunk *input, unsigned i
                 for (c = 0; c < r->o_ss.channels; c++) {
                     float n = fabsf(*s++);
 
-                    if (n > r->peaks.max_f[c])
-                        r->peaks.max_f[c] = n;
+                    if (n > peaks_data->max_f[c])
+                        peaks_data->max_f[c] = n;
                 }
 
             if (i == i_end) {
                 for (c = 0; c < r->o_ss.channels; c++, d++) {
-                    *d = r->peaks.max_f[c];
-                    r->peaks.max_f[c] = 0;
+                    *d = peaks_data->max_f[c];
+                    peaks_data->max_f[c] = 0;
                 }
-                o_index++, r->peaks.o_counter++;
+                o_index++, peaks_data->o_counter++;
             }
         }
     }
@@ -1571,36 +1611,39 @@ static void peaks_resample(pa_resampler *r, const pa_memchunk *input, unsigned i
 
     *out_n_frames = o_index;
 
-    r->peaks.i_counter += in_n_frames;
+    peaks_data->i_counter += in_n_frames;
 
     /* Normalize counters */
-    while (r->peaks.i_counter >= r->i_ss.rate) {
-        pa_assert(r->peaks.o_counter >= r->o_ss.rate);
+    while (peaks_data->i_counter >= r->i_ss.rate) {
+        pa_assert(peaks_data->o_counter >= r->o_ss.rate);
 
-        r->peaks.i_counter -= r->i_ss.rate;
-        r->peaks.o_counter -= r->o_ss.rate;
+        peaks_data->i_counter -= r->i_ss.rate;
+        peaks_data->o_counter -= r->o_ss.rate;
     }
 }
 
 static void peaks_update_rates_or_reset(pa_resampler *r) {
+    struct peaks_data *peaks_data;
     pa_assert(r);
 
-    r->peaks.i_counter = 0;
-    r->peaks.o_counter = 0;
+    peaks_data = r->impl_data;
+
+    peaks_data->i_counter = 0;
+    peaks_data->o_counter = 0;
 }
 
 static int 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);
 
-    r->peaks.o_counter = r->peaks.i_counter = 0;
-    memset(r->peaks.max_i, 0, sizeof(r->peaks.max_i));
-    memset(r->peaks.max_f, 0, sizeof(r->peaks.max_f));
+    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;
 }
@@ -1610,12 +1653,15 @@ static int peaks_init(pa_resampler*r) {
 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;
     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->o_ss.channels; c++) {
         unsigned u;
         pa_memblock *b, *w;
@@ -1623,7 +1669,7 @@ static void ffmpeg_resample(pa_resampler *r, const pa_memchunk *input, unsigned
         int consumed_frames;
 
         /* Allocate a new block */
-        b = pa_memblock_new(r->mempool, r->ffmpeg.buf[c].length + in_n_frames * sizeof(int16_t));
+        b = pa_memblock_new(r->mempool, ffmpeg_data->buf[c].length + in_n_frames * sizeof(int16_t));
         p = pa_memblock_acquire(b);
 
         /* Now copy the input data, splitting up channels */
@@ -1641,7 +1687,7 @@ static void ffmpeg_resample(pa_resampler *r, const pa_memchunk *input, unsigned
         q = pa_memblock_acquire(w);
 
         /* Now, resample */
-        used_frames = (unsigned) av_resample(r->ffmpeg.state,
+        used_frames = (unsigned) av_resample(ffmpeg_data->state,
                                              q, p,
                                              &consumed_frames,
                                              (int) in_n_frames, (int) *out_n_frames,
@@ -1679,35 +1725,41 @@ static void ffmpeg_resample(pa_resampler *r, const pa_memchunk *input, unsigned
 
 static void ffmpeg_free(pa_resampler *r) {
     unsigned c;
+    struct ffmpeg_data *ffmpeg_data;
 
     pa_assert(r);
 
-    if (r->ffmpeg.state)
-        av_resample_close(r->ffmpeg.state);
+    ffmpeg_data = r->impl_data;
+    if (ffmpeg_data->state)
+        av_resample_close(ffmpeg_data->state);
 
-    for (c = 0; c < PA_ELEMENTSOF(r->ffmpeg.buf); c++)
-        if (r->ffmpeg.buf[c].memblock)
-            pa_memblock_unref(r->ffmpeg.buf[c].memblock);
+    for (c = 0; c < PA_ELEMENTSOF(ffmpeg_data->buf); c++)
+        if (ffmpeg_data->buf[c].memblock)
+            pa_memblock_unref(ffmpeg_data->buf[c].memblock);
 }
 
 static int ffmpeg_init(pa_resampler *r) {
     unsigned c;
+    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 (!(r->ffmpeg.state = av_resample_init((int) r->o_ss.rate, (int) r->i_ss.rate, 16, 10, 0, 0.8)))
+    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;
 
-    for (c = 0; c < PA_ELEMENTSOF(r->ffmpeg.buf); c++)
-        pa_memchunk_reset(&r->ffmpeg.buf[c]);
+    for (c = 0; c < PA_ELEMENTSOF(ffmpeg_data->buf); c++)
+        pa_memchunk_reset(&ffmpeg_data->buf[c]);
 
     return 0;
 }



More information about the pulseaudio-commits mailing list