[pulseaudio-discuss] [PATCH 13/18] resampler: Get rid of redundant implementation specific structs

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


This patch removes implementation specific structs that contain a single
member.
---
 src/pulsecore/resampler.c | 84 +++++++++++++++++++++--------------------------
 1 file changed, 38 insertions(+), 46 deletions(-)

diff --git a/src/pulsecore/resampler.c b/src/pulsecore/resampler.c
index 2d83315..c4434ff 100644
--- a/src/pulsecore/resampler.c
+++ b/src/pulsecore/resampler.c
@@ -105,10 +105,6 @@ static void speex_free(pa_resampler *r);
 static void speex_update_rates(pa_resampler *r);
 static void speex_reset(pa_resampler *r);
 
-struct speex{ /* data specific to speex */
-    SpeexResamplerState* state;
-};
-
 static pa_resampler_implementation speex_impl = {
     .init = speex_init,
     .free = speex_free,
@@ -158,10 +154,6 @@ static void libsamplerate_update_rates(pa_resampler *r);
 static void libsamplerate_reset(pa_resampler *r);
 static void libsamplerate_free(pa_resampler *r);
 
-struct src{ /* data specific to libsamplerate */
-    SRC_STATE *state;
-};
-
 static pa_resampler_implementation libsamplerate_impl = {
     .init = libsamplerate_init,
     .free = libsamplerate_free,
@@ -456,6 +448,8 @@ void pa_resampler_free(pa_resampler *r) {
 
     if (r->implementation.free)
         r->implementation.free(r);
+    else
+        pa_xfree(r->implementation.data);
 
     if (r->to_work_format_buf.memblock)
         pa_memblock_unref(r->to_work_format_buf.memblock);
@@ -466,7 +460,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->implementation.data);
     pa_xfree(r);
 }
 
@@ -1358,14 +1351,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 *libsamplerate_data;
+    SRC_STATE *state;
 
     pa_assert(r);
     pa_assert(input);
     pa_assert(output);
     pa_assert(out_n_frames);
 
-    libsamplerate_data = r->implementation.data;
+    state = r->implementation.data;
     memset(&data, 0, sizeof(data));
 
     data.data_in = pa_memblock_acquire_chunk(input);
@@ -1377,7 +1370,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;
@@ -1393,42 +1386,41 @@ static void libsamplerate_resample(pa_resampler *r, const pa_memchunk *input, un
 }
 
 static void libsamplerate_update_rates(pa_resampler *r) {
-    struct src *libsamplerate_data;
+    SRC_STATE *state;
     pa_assert(r);
 
-    libsamplerate_data = r->implementation.data;
-    pa_assert_se(src_set_ratio(libsamplerate_data->state, (double) r->o_ss.rate / r->i_ss.rate) == 0);
+    state = r->implementation.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 *libsamplerate_data;
+    SRC_STATE *state;
     pa_assert(r);
 
-    libsamplerate_data = r->implementation.data;
-    pa_assert_se(src_reset(libsamplerate_data->state) == 0);
+    state = r->implementation.data;
+    pa_assert_se(src_reset(state) == 0);
 }
 
 static void libsamplerate_free(pa_resampler *r) {
-    struct src *libsamplerate_data;
+    SRC_STATE *state;
     pa_assert(r);
 
-    libsamplerate_data = r->implementation.data;
-    if (libsamplerate_data->state)
-        src_delete(libsamplerate_data->state);
+    state = r->implementation.data;
+    if (state)
+        src_delete(state);
 }
 
 static int libsamplerate_init(pa_resampler *r) {
     int err;
-    struct src *libsamplerate_data;
+    SRC_STATE *state = NULL;
 
     pa_assert(r);
 
-    libsamplerate_data = pa_xnew(struct src, 1);
-    r->implementation.data = libsamplerate_data;
 
-    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->implementation.data = state;
 
     return 0;
 }
@@ -1440,19 +1432,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 *speex_data;
+    SpeexResamplerState *state;
 
     pa_assert(r);
     pa_assert(input);
     pa_assert(output);
     pa_assert(out_n_frames);
 
-    speex_data = r->implementation.data;
+    state = r->implementation.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);
@@ -1464,19 +1456,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 *speex_data;
+    SpeexResamplerState *state;
 
     pa_assert(r);
     pa_assert(input);
     pa_assert(output);
     pa_assert(out_n_frames);
 
-    speex_data = r->implementation.data;
+    state = r->implementation.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);
@@ -1486,42 +1478,40 @@ static void speex_resample_int(pa_resampler *r, const pa_memchunk *input, unsign
 }
 
 static void speex_update_rates(pa_resampler *r) {
-    struct speex *speex_data;
+    SpeexResamplerState *state;
     pa_assert(r);
 
-    speex_data = r->implementation.data;
+    state = r->implementation.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 *speex_data;
+    SpeexResamplerState *state;
     pa_assert(r);
 
-    speex_data = r->implementation.data;
+    state = r->implementation.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 *speex_data;
+    SpeexResamplerState *state;
     pa_assert(r);
 
-    speex_data = r->implementation.data;
-    if (!speex_data->state)
+    state = r->implementation.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 *speex_data;
+    SpeexResamplerState *state;
 
     pa_assert(r);
 
-    speex_data = pa_xnew(struct speex, 1);
-    r->implementation.data = speex_data;
 
     if (r->method >= PA_RESAMPLER_SPEEX_FIXED_BASE && r->method <= PA_RESAMPLER_SPEEX_FIXED_MAX) {
 
@@ -1537,9 +1527,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->implementation.data = state;
+
     return 0;
 }
 #endif
-- 
1.8.3.2



More information about the pulseaudio-discuss mailing list