[pulseaudio-discuss] [PATCH 08/18] resampler: Introduce a implementation specific struct

poljar (Damir Jelić) poljarinho at gmail.com
Mon Jul 15 06:48:30 PDT 2013


This struct holds all the implementation specific data in one place.
---
 src/pulsecore/resampler.c | 98 +++++++++++++++++++++++------------------------
 src/pulsecore/resampler.h |  9 +++++
 2 files changed, 56 insertions(+), 51 deletions(-)

diff --git a/src/pulsecore/resampler.c b/src/pulsecore/resampler.c
index 4d542bc..ea5f1aa 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_implementation implementation;
 };
 
 struct trivial { /* data specific to the trivial resampler */
@@ -441,8 +437,8 @@ fail:
 void pa_resampler_free(pa_resampler *r) {
     pa_assert(r);
 
-    if (r->impl_free)
-        r->impl_free(r);
+    if (r->implementation.free)
+        r->implementation.free(r);
 
     if (r->to_work_format_buf.memblock)
         pa_memblock_unref(r->to_work_format_buf.memblock);
@@ -453,7 +449,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->implementation.data);
     pa_xfree(r);
 }
 
@@ -466,7 +462,7 @@ void pa_resampler_set_input_rate(pa_resampler *r, uint32_t rate) {
 
     r->i_ss.rate = rate;
 
-    r->impl_update_rates(r);
+    r->implementation.update_rates(r);
 }
 
 void pa_resampler_set_output_rate(pa_resampler *r, uint32_t rate) {
@@ -478,7 +474,7 @@ void pa_resampler_set_output_rate(pa_resampler *r, uint32_t rate) {
 
     r->o_ss.rate = rate;
 
-    r->impl_update_rates(r);
+    r->implementation.update_rates(r);
 }
 
 size_t pa_resampler_request(pa_resampler *r, size_t out_length) {
@@ -544,8 +540,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->implementation.reset)
+        r->implementation.reset(r);
 
     r->remap_buf_contains_leftover_data = false;
 }
@@ -1235,7 +1231,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->implementation.resample || !input->length)
         return input;
 
     in_n_samples = (unsigned) (input->length / r->w_sz);
@@ -1255,7 +1251,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->implementation.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;
@@ -1362,7 +1358,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->implementation.data;
     memset(&data, 0, sizeof(data));
 
     data.data_in = pa_memblock_acquire_chunk(input);
@@ -1393,7 +1389,7 @@ static void libsamplerate_update_rates(pa_resampler *r) {
     struct src *libsamplerate_data;
     pa_assert(r);
 
-    libsamplerate_data = r->impl_data;
+    libsamplerate_data = r->implementation.data;
     pa_assert_se(src_set_ratio(libsamplerate_data->state, (double) r->o_ss.rate / r->i_ss.rate) == 0);
 }
 
@@ -1401,7 +1397,7 @@ static void libsamplerate_reset(pa_resampler *r) {
     struct src *libsamplerate_data;
     pa_assert(r);
 
-    libsamplerate_data = r->impl_data;
+    libsamplerate_data = r->implementation.data;
     pa_assert_se(src_reset(libsamplerate_data->state) == 0);
 }
 
@@ -1409,7 +1405,7 @@ static void libsamplerate_free(pa_resampler *r) {
     struct src *libsamplerate_data;
     pa_assert(r);
 
-    libsamplerate_data = r->impl_data;
+    libsamplerate_data = r->implementation.data;
     if (libsamplerate_data->state)
         src_delete(libsamplerate_data->state);
 }
@@ -1425,11 +1421,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->implementation.free = libsamplerate_free;
+    r->implementation.update_rates = libsamplerate_update_rates;
+    r->implementation.resample = libsamplerate_resample;
+    r->implementation.reset = libsamplerate_reset;
+    r->implementation.data = libsamplerate_data;
 
     return 0;
 }
@@ -1448,7 +1444,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->implementation.data;
 
     in = pa_memblock_acquire_chunk(input);
     out = pa_memblock_acquire_chunk(output);
@@ -1472,7 +1468,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->implementation.data;
 
     in = pa_memblock_acquire_chunk(input);
     out = pa_memblock_acquire_chunk(output);
@@ -1490,7 +1486,7 @@ static void speex_update_rates(pa_resampler *r) {
     struct speex *speex_data;
     pa_assert(r);
 
-    speex_data = r->impl_data;
+    speex_data = r->implementation.data;
 
     pa_assert_se(speex_resampler_set_rate(speex_data->state, r->i_ss.rate, r->o_ss.rate) == 0);
 }
@@ -1499,7 +1495,7 @@ static void speex_reset(pa_resampler *r) {
     struct speex *speex_data;
     pa_assert(r);
 
-    speex_data = r->impl_data;
+    speex_data = r->implementation.data;
 
     pa_assert_se(speex_resampler_reset_mem(speex_data->state) == 0);
 }
@@ -1508,7 +1504,7 @@ static void speex_free(pa_resampler *r) {
     struct speex *speex_data;
     pa_assert(r);
 
-    speex_data = r->impl_data;
+    speex_data = r->implementation.data;
     if (!speex_data->state)
         return;
 
@@ -1523,21 +1519,21 @@ static int speex_init(pa_resampler *r) {
 
     speex_data = pa_xnew(struct speex, 1);
 
-    r->impl_free = speex_free;
-    r->impl_update_rates = speex_update_rates;
-    r->impl_reset = speex_reset;
-    r->impl_data = speex_data;
+    r->implementation.free = speex_free;
+    r->implementation.update_rates = speex_update_rates;
+    r->implementation.reset = speex_reset;
+    r->implementation.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->implementation.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->implementation.resample = speex_resample_float;
     }
 
     pa_log_info("Choosing speex quality setting %i.", q);
@@ -1562,7 +1558,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->implementation.data;
     fz = r->w_sz * r->o_ss.channels;
 
     src = pa_memblock_acquire_chunk(input);
@@ -1600,7 +1596,7 @@ static void trivial_update_rates_or_reset(pa_resampler *r) {
     struct trivial *trivial_data;
     pa_assert(r);
 
-    trivial_data = r->impl_data;
+    trivial_data = r->implementation.data;
 
     trivial_data->i_counter = 0;
     trivial_data->o_counter = 0;
@@ -1612,10 +1608,10 @@ static int trivial_init(pa_resampler*r) {
 
     trivial_data = pa_xnew0(struct trivial, 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->implementation.resample = trivial_resample;
+    r->implementation.update_rates = trivial_update_rates_or_reset;
+    r->implementation.reset = trivial_update_rates_or_reset;
+    r->implementation.data = trivial_data;
 
     return 0;
 }
@@ -1633,7 +1629,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->implementation.data;
     src = pa_memblock_acquire_chunk(input);
     dst = pa_memblock_acquire_chunk(output);
 
@@ -1724,7 +1720,7 @@ static void peaks_update_rates_or_reset(pa_resampler *r) {
     struct peaks *peaks_data;
     pa_assert(r);
 
-    peaks_data = r->impl_data;
+    peaks_data = r->implementation.data;
 
     peaks_data->i_counter = 0;
     peaks_data->o_counter = 0;
@@ -1741,10 +1737,10 @@ static int peaks_init(pa_resampler*r) {
     memset(peaks_data->max_i, 0, sizeof(peaks_data->max_i));
     memset(peaks_data->max_f, 0, sizeof(peaks_data->max_f));
 
-    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->implementation.resample = peaks_resample;
+    r->implementation.update_rates = peaks_update_rates_or_reset;
+    r->implementation.reset = peaks_update_rates_or_reset;
+    r->implementation.data = peaks_data;
 
     return 0;
 }
@@ -1761,7 +1757,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->implementation.data;
 
     for (c = 0; c < r->o_ss.channels; c++) {
         unsigned u;
@@ -1830,7 +1826,7 @@ static void ffmpeg_free(pa_resampler *r) {
 
     pa_assert(r);
 
-    ffmpeg_data = r->impl_data;
+    ffmpeg_data = r->implementation.data;
     if (ffmpeg_data->state)
         av_resample_close(ffmpeg_data->state);
 
@@ -1855,9 +1851,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->implementation.free = ffmpeg_free;
+    r->implementation.resample = ffmpeg_resample;
+    r->implementation.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..7dbafa8 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_implementation pa_resampler_implementation;
+
+struct pa_resampler_implementation {
+    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,
-- 
1.8.3.2



More information about the pulseaudio-discuss mailing list