[pulseaudio-discuss] [PATCH 01/18] resampler: Make resampler struct implementation agnostic

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


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.
---
 src/pulsecore/resampler.c | 223 +++++++++++++++++++++++++++++-----------------
 1 file changed, 139 insertions(+), 84 deletions(-)

diff --git a/src/pulsecore/resampler.c b/src/pulsecore/resampler.c
index cdfaf8f..f70f186 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 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 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 specific to libsamplerate */
+    SRC_STATE *state;
+};
 #endif
 
 #ifdef HAVE_SPEEX
-    struct { /* data specific to speex */
-        SpeexResamplerState* state;
-    } speex;
+struct speex{ /* 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 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 *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 *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 *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 *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 *libsamplerate_data;
 
     pa_assert(r);
 
-    if (!(r->src.state = src_new(r->method, r->o_ss.channels, &err)))
+    libsamplerate_data = pa_xnew(struct src, 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 *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 *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 *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 *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 *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 *speex_data;
 
     pa_assert(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;
 
     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 *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 *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 *trivial_data;
     pa_assert(r);
 
-    r->trivial.o_counter = r->trivial.i_counter = 0;
+    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;
 
     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 *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->i_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->i_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,42 @@ 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 *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 *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_xnew(struct peaks, 1);
+    peaks_data->o_counter = peaks_data->i_counter = 0;
+    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;
 
     return 0;
 }
@@ -1610,12 +1656,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 *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 +1672,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 +1690,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 +1728,41 @@ static void ffmpeg_resample(pa_resampler *r, const pa_memchunk *input, unsigned
 
 static void ffmpeg_free(pa_resampler *r) {
     unsigned c;
+    struct ffmpeg *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 *ffmpeg_data;
 
     pa_assert(r);
 
+    ffmpeg_data = pa_xnew(struct ffmpeg, 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;
 }
-- 
1.8.3.2



More information about the pulseaudio-discuss mailing list