]> code.delx.au - pulseaudio/blobdiff - src/pulsecore/resampler.c
build-sys: Make speex library optional
[pulseaudio] / src / pulsecore / resampler.c
index f1bfa156b54499a6f4f5918d391ed838d4864b9b..b56c1f5985bb49400829dfcdec03aea586c420ea 100644 (file)
 #include <samplerate.h>
 #endif
 
+#ifdef HAVE_SPEEX
 #include <speex/speex_resampler.h>
+#endif
 
 #include <pulse/xmalloc.h>
 #include <pulsecore/sconv.h>
 #include <pulsecore/log.h>
 #include <pulsecore/macro.h>
 #include <pulsecore/strbuf.h>
+#include <pulsecore/remap.h>
 
 #include "ffmpeg/avcodec.h"
 
 #include "resampler.h"
-#include "remap.h"
 
 /* Number of samples of extra space we allow the resamplers to return */
 #define EXTRA_FRAMES 128
@@ -90,9 +92,11 @@ struct pa_resampler {
     } src;
 #endif
 
+#ifdef HAVE_SPEEX
     struct { /* data specific to speex */
         SpeexResamplerState* state;
     } speex;
+#endif
 
     struct { /* data specific to ffmpeg */
         struct AVResampleContext *state;
@@ -102,7 +106,9 @@ struct pa_resampler {
 
 static int copy_init(pa_resampler *r);
 static int trivial_init(pa_resampler*r);
+#ifdef HAVE_SPEEX
 static int speex_init(pa_resampler*r);
+#endif
 static int ffmpeg_init(pa_resampler*r);
 static int peaks_init(pa_resampler*r);
 #ifdef HAVE_LIBSAMPLERATE
@@ -126,6 +132,7 @@ static int (* const init_table[])(pa_resampler*r) = {
     [PA_RESAMPLER_SRC_LINEAR]              = NULL,
 #endif
     [PA_RESAMPLER_TRIVIAL]                 = trivial_init,
+#ifdef HAVE_SPEEX
     [PA_RESAMPLER_SPEEX_FLOAT_BASE+0]      = speex_init,
     [PA_RESAMPLER_SPEEX_FLOAT_BASE+1]      = speex_init,
     [PA_RESAMPLER_SPEEX_FLOAT_BASE+2]      = speex_init,
@@ -148,6 +155,30 @@ static int (* const init_table[])(pa_resampler*r) = {
     [PA_RESAMPLER_SPEEX_FIXED_BASE+8]      = speex_init,
     [PA_RESAMPLER_SPEEX_FIXED_BASE+9]      = speex_init,
     [PA_RESAMPLER_SPEEX_FIXED_BASE+10]     = speex_init,
+#else
+    [PA_RESAMPLER_SPEEX_FLOAT_BASE+0]      = NULL,
+    [PA_RESAMPLER_SPEEX_FLOAT_BASE+1]      = NULL,
+    [PA_RESAMPLER_SPEEX_FLOAT_BASE+2]      = NULL,
+    [PA_RESAMPLER_SPEEX_FLOAT_BASE+3]      = NULL,
+    [PA_RESAMPLER_SPEEX_FLOAT_BASE+4]      = NULL,
+    [PA_RESAMPLER_SPEEX_FLOAT_BASE+5]      = NULL,
+    [PA_RESAMPLER_SPEEX_FLOAT_BASE+6]      = NULL,
+    [PA_RESAMPLER_SPEEX_FLOAT_BASE+7]      = NULL,
+    [PA_RESAMPLER_SPEEX_FLOAT_BASE+8]      = NULL,
+    [PA_RESAMPLER_SPEEX_FLOAT_BASE+9]      = NULL,
+    [PA_RESAMPLER_SPEEX_FLOAT_BASE+10]     = NULL,
+    [PA_RESAMPLER_SPEEX_FIXED_BASE+0]      = NULL,
+    [PA_RESAMPLER_SPEEX_FIXED_BASE+1]      = NULL,
+    [PA_RESAMPLER_SPEEX_FIXED_BASE+2]      = NULL,
+    [PA_RESAMPLER_SPEEX_FIXED_BASE+3]      = NULL,
+    [PA_RESAMPLER_SPEEX_FIXED_BASE+4]      = NULL,
+    [PA_RESAMPLER_SPEEX_FIXED_BASE+5]      = NULL,
+    [PA_RESAMPLER_SPEEX_FIXED_BASE+6]      = NULL,
+    [PA_RESAMPLER_SPEEX_FIXED_BASE+7]      = NULL,
+    [PA_RESAMPLER_SPEEX_FIXED_BASE+8]      = NULL,
+    [PA_RESAMPLER_SPEEX_FIXED_BASE+9]      = NULL,
+    [PA_RESAMPLER_SPEEX_FIXED_BASE+10]     = NULL,
+#endif
     [PA_RESAMPLER_FFMPEG]                  = ffmpeg_init,
     [PA_RESAMPLER_AUTO]                    = NULL,
     [PA_RESAMPLER_COPY]                    = copy_init,
@@ -195,8 +226,13 @@ pa_resampler* pa_resampler_new(
         method = PA_RESAMPLER_AUTO;
     }
 
-    if (method == PA_RESAMPLER_AUTO)
+    if (method == PA_RESAMPLER_AUTO) {
+#ifdef HAVE_SPEEX
         method = PA_RESAMPLER_SPEEX_FLOAT_BASE + 3;
+#else
+        method = PA_RESAMPLER_FFMPEG;
+#endif
+    }
 
     r = pa_xnew(pa_resampler, 1);
     r->mempool = pool;
@@ -299,8 +335,7 @@ pa_resampler* pa_resampler_new(
     return r;
 
 fail:
-    if (r)
-        pa_xfree(r);
+    pa_xfree(r);
 
     return NULL;
 }
@@ -477,6 +512,13 @@ int pa_resample_method_supported(pa_resample_method_t m) {
         return 0;
 #endif
 
+#ifndef HAVE_SPEEX
+    if (m >= PA_RESAMPLER_SPEEX_FLOAT_BASE && m <= PA_RESAMPLER_SPEEX_FLOAT_MAX)
+        return 0;
+    if (m >= PA_RESAMPLER_SPEEX_FIXED_BASE && m <= PA_RESAMPLER_SPEEX_FIXED_MAX)
+        return 0;
+#endif
+
     return 1;
 }
 
@@ -646,7 +688,7 @@ static void calc_map_table(pa_resampler *r) {
              * volume will not match, and the two channels will be a
              * linear combination of both.
              *
-             * This is losely based on random suggestions found on the
+             * This is loosely based on random suggestions found on the
              * Internet, such as this:
              * http://www.halfgaar.net/surround-sound-in-linux and the
              * alsa upmix plugin.
@@ -841,7 +883,7 @@ static void calc_map_table(pa_resampler *r) {
             /* OK, so there are unconnected input channels on the
              * left. Let's multiply all already connected channels on
              * the left side by .9 and add in our averaged unconnected
-             * channels multplied by .1 */
+             * channels multiplied by .1 */
 
             for (oc = 0; oc < n_oc; oc++) {
 
@@ -866,7 +908,7 @@ static void calc_map_table(pa_resampler *r) {
             /* OK, so there are unconnected input channels on the
              * right. Let's multiply all already connected channels on
              * the right side by .9 and add in our averaged unconnected
-             * channels multplied by .1 */
+             * channels multiplied by .1 */
 
             for (oc = 0; oc < n_oc; oc++) {
 
@@ -892,14 +934,14 @@ static void calc_map_table(pa_resampler *r) {
             /* OK, so there are unconnected input channels on the
              * center. Let's multiply all already connected channels on
              * the center side by .9 and add in our averaged unconnected
-             * channels multplied by .1 */
+             * channels multiplied by .1 */
 
             for (oc = 0; oc < n_oc; oc++) {
 
                 if (!on_center(r->o_cm.map[oc]))
                     continue;
 
-                for (ic = 0; ic < n_ic; ic++)  {
+                for (ic = 0; ic < n_ic; ic++) {
 
                     if (ic_connected[ic]) {
                         m->map_table_f[oc][ic] *= .9f;
@@ -962,7 +1004,7 @@ static void calc_map_table(pa_resampler *r) {
                     if (ncenter[oc] <= 0)
                         continue;
 
-                    for (ic = 0; ic < n_ic; ic++)  {
+                    for (ic = 0; ic < n_ic; ic++) {
 
                         if (ic_connected[ic]) {
                             m->map_table_f[oc][ic] *= .75f;
@@ -984,7 +1026,7 @@ static void calc_map_table(pa_resampler *r) {
             /* OK, so there is an unconnected LFE channel. Let's mix
              * it into all channels, with factor 0.375 */
 
-            for (ic = 0; ic < n_ic; ic++)  {
+            for (ic = 0; ic < n_ic; ic++) {
 
                 if (!on_lfe(r->i_cm.map[ic]))
                     continue;
@@ -1023,7 +1065,7 @@ static void calc_map_table(pa_resampler *r) {
     pa_xfree(t);
 
     /* initialize the remapping function */
-    pa_init_remap (m);
+    pa_init_remap(m);
 }
 
 static pa_memchunk* convert_to_work_format(pa_resampler *r, pa_memchunk *input) {
@@ -1097,8 +1139,8 @@ static pa_memchunk *remap_channels(pa_resampler *r, pa_memchunk *input) {
 
     remap = &r->remap;
 
-    pa_assert (remap->do_remap);
-    remap->do_remap (remap, dst, src, n_frames);
+    pa_assert(remap->do_remap);
+    remap->do_remap(remap, dst, src, n_frames);
 
     pa_memblock_release(input->memblock);
     pa_memblock_release(r->buf2.memblock);
@@ -1272,6 +1314,7 @@ static int libsamplerate_init(pa_resampler *r) {
 }
 #endif
 
+#ifdef HAVE_SPEEX
 /*** speex based implementation ***/
 
 static void speex_resample_float(pa_resampler *r, const pa_memchunk *input, unsigned in_n_frames, pa_memchunk *output, unsigned *out_n_frames) {
@@ -1365,18 +1408,20 @@ static int speex_init(pa_resampler *r) {
 
     return 0;
 }
+#endif
 
 /* Trivial implementation */
 
 static void trivial_resample(pa_resampler *r, const pa_memchunk *input, unsigned in_n_frames, pa_memchunk *output, unsigned *out_n_frames) {
     size_t fz;
-    unsigned o_index;
+    unsigned i_index, o_index;
     void *src, *dst;
 
     pa_assert(r);
     pa_assert(input);
     pa_assert(output);
     pa_assert(out_n_frames);
+    pa_assert(r->i_ss.channels == r->o_ss.channels);
 
     fz = r->w_sz * r->o_ss.channels;
 
@@ -1384,18 +1429,24 @@ static void trivial_resample(pa_resampler *r, const pa_memchunk *input, unsigned
     dst = (uint8_t*) pa_memblock_acquire(output->memblock) + output->index;
 
     for (o_index = 0;; o_index++, r->trivial.o_counter++) {
-        unsigned j;
-
-        j = ((r->trivial.o_counter * r->i_ss.rate) / r->o_ss.rate);
-        j = j > r->trivial.i_counter ? j - r->trivial.i_counter : 0;
+        i_index = (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;
 
-        if (j >= in_n_frames)
+        if (i_index >= in_n_frames)
             break;
 
-        pa_assert(o_index * fz < pa_memblock_get_length(output->memblock));
+        pa_assert_fp(o_index * fz < pa_memblock_get_length(output->memblock));
 
-        memcpy((uint8_t*) dst + fz * o_index,
-                   (uint8_t*) src + fz * j, (int) fz);
+        /* Directly assign some common sample sizes, use memcpy as fallback */
+        if (r->w_sz == 2) {
+            for (unsigned c = 0; c < r->o_ss.channels; c++)
+                ((uint16_t *) dst)[o_index+c] = ((uint16_t *) src)[i_index+c];
+        } else if (r->w_sz == 4) {
+            for (unsigned c = 0; c < r->o_ss.channels; c++)
+                ((uint32_t *) dst)[o_index+c] = ((uint32_t *) src)[i_index+c];
+        } else {
+            memcpy((uint8_t *) dst + fz * o_index, (uint8_t *) src + fz * i_index, (int) fz);
+        }
     }
 
     pa_memblock_release(input->memblock);
@@ -1436,82 +1487,86 @@ static int trivial_init(pa_resampler*r) {
 /* Peak finder implementation */
 
 static void peaks_resample(pa_resampler *r, const pa_memchunk *input, unsigned in_n_frames, pa_memchunk *output, unsigned *out_n_frames) {
-    size_t fz;
-    unsigned o_index;
+    unsigned c, o_index = 0;
+    unsigned i, i_end = 0;
     void *src, *dst;
-    unsigned start = 0;
 
     pa_assert(r);
     pa_assert(input);
     pa_assert(output);
     pa_assert(out_n_frames);
-
-    fz = r->w_sz * r->o_ss.channels;
+    pa_assert(r->i_ss.rate >= r->o_ss.rate);
+    pa_assert(r->i_ss.channels == r->o_ss.channels);
+    pa_assert(r->work_format == PA_SAMPLE_S16NE || r->work_format == PA_SAMPLE_FLOAT32NE);
 
     src = (uint8_t*) pa_memblock_acquire(input->memblock) + input->index;
     dst = (uint8_t*) pa_memblock_acquire(output->memblock) + output->index;
 
-    for (o_index = 0;; o_index++, r->peaks.o_counter++) {
-        unsigned j;
+    i = (r->peaks.o_counter * r->i_ss.rate) / r->o_ss.rate;
+    i = i > r->peaks.i_counter ? i - r->peaks.i_counter : 0;
 
-        j = ((r->peaks.o_counter * r->i_ss.rate) / r->o_ss.rate);
+    while (i_end < in_n_frames) {
+        i_end = ((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;
 
-        if (j > r->peaks.i_counter)
-            j -= r->peaks.i_counter;
-        else
-            j = 0;
+        pa_assert_fp(o_index * r->w_sz * r->o_ss.channels < pa_memblock_get_length(output->memblock));
 
-        pa_assert(o_index * fz < pa_memblock_get_length(output->memblock));
+        /* 1ch float is treated separately, because that is the common case */
+        if (r->o_ss.channels == 1 && r->work_format == PA_SAMPLE_FLOAT32NE) {
+            float *s = (float*) src + i;
+            float *d = (float*) dst + o_index;
 
-        if (r->work_format == PA_SAMPLE_S16NE) {
-            unsigned i, c;
-            int16_t *s = (int16_t*) ((uint8_t*) src + fz * start);
-            int16_t *d = (int16_t*) ((uint8_t*) dst + fz * o_index);
+            for (; i < i_end && i < in_n_frames; i++) {
+                float n = fabsf(*s++);
 
-            for (i = start; i <= j && i < in_n_frames; i++)
+                if (n > r->peaks.max_f[0])
+                    r->peaks.max_f[0] = n;
+            }
 
-                for (c = 0; c < r->o_ss.channels; c++, s++) {
-                    int16_t n;
+            if (i == i_end) {
+                *d = r->peaks.max_f[0];
+                r->peaks.max_f[0] = 0;
+                o_index++, r->peaks.o_counter++;
+            }
+        } else if (r->work_format == PA_SAMPLE_S16NE) {
+            int16_t *s = (int16_t*) src + r->i_ss.channels * i;
+            int16_t *d = (int16_t*) dst + r->o_ss.channels * o_index;
 
-                    n = (int16_t) (*s < 0 ? -*s : *s);
+            for (; i < i_end && i < in_n_frames; i++)
+                for (c = 0; c < r->o_ss.channels; c++) {
+                    int16_t n = abs(*s++);
 
-                    if (PA_UNLIKELY(n > r->peaks.max_i[c]))
+                    if (n > r->peaks.max_i[c])
                         r->peaks.max_i[c] = n;
                 }
 
-            if (i >= in_n_frames)
-                break;
-
-            for (c = 0; c < r->o_ss.channels; c++, d++) {
-                *d = r->peaks.max_i[c];
-                r->peaks.max_i[c] = 0;
+            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;
+                }
+                o_index++, r->peaks.o_counter++;
             }
-
         } else {
-            unsigned i, c;
-            float *s = (float*) ((uint8_t*) src + fz * start);
-            float *d = (float*) ((uint8_t*) dst + fz * o_index);
-
-            pa_assert(r->work_format == PA_SAMPLE_FLOAT32NE);
+            float *s = (float*) src + r->i_ss.channels * i;
+            float *d = (float*) dst + r->o_ss.channels * o_index;
 
-            for (i = start; i <= j && i < in_n_frames; i++)
-                for (c = 0; c < r->o_ss.channels; c++, s++) {
-                    float n = fabsf(*s);
+            for (; i < i_end && i < in_n_frames; 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 (i >= in_n_frames)
-                break;
-
-            for (c = 0; c < r->o_ss.channels; c++, d++) {
-                *d = r->peaks.max_f[c];
-                r->peaks.max_f[c] = 0;
+            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;
+                }
+                o_index++, r->peaks.o_counter++;
             }
         }
-
-        start = j;
     }
 
     pa_memblock_release(input->memblock);