]> code.delx.au - pulseaudio/blobdiff - src/pulsecore/resampler.c
resample: Switch to speex-float-1 by default
[pulseaudio] / src / pulsecore / resampler.c
index bec337a0cc5adfe1a064035ac534a1ade0dc1c66..bc45f065e86e4c34833242bbee41a182fb4f55dd 100644 (file)
@@ -67,6 +67,7 @@ struct pa_resampler {
     bool remap_buf_contains_leftover_data;
 
     pa_sample_format_t work_format;
+    uint8_t work_channels;
 
     pa_convert_func_t to_work_format_func;
     pa_convert_func_t from_work_format_func;
@@ -235,7 +236,7 @@ pa_resampler* pa_resampler_new(
 
     if (method == PA_RESAMPLER_AUTO) {
 #ifdef HAVE_SPEEX
-        method = PA_RESAMPLER_SPEEX_FLOAT_BASE + 3;
+        method = PA_RESAMPLER_SPEEX_FLOAT_BASE + 1;
 #else
         if (flags & PA_RESAMPLER_VARIABLE_RATE)
             method = PA_RESAMPLER_TRIVIAL;
@@ -328,6 +329,16 @@ pa_resampler* pa_resampler_new(
         }
     }
 
+    if (r->o_ss.channels <= r->i_ss.channels)
+        r->work_channels = r->o_ss.channels;
+    else
+        r->work_channels = r->i_ss.channels;
+
+    pa_log_debug("Resampler:\n  rate %d -> %d (method %s),\n  format %s -> %s (intermediate %s),\n  channels %d -> %d (resampling %d)",
+        a->rate, b->rate, pa_resample_method_to_string(r->method),
+        pa_sample_format_to_string(a->format), pa_sample_format_to_string(b->format), pa_sample_format_to_string(r->work_format),
+        a->channels, b->channels, r->work_channels);
+
     /* initialize implementation */
     if (init_table[method](r) < 0)
         goto fail;
@@ -554,10 +565,10 @@ pa_resample_method_t pa_parse_resample_method(const char *string) {
             return m;
 
     if (pa_streq(string, "speex-fixed"))
-        return PA_RESAMPLER_SPEEX_FIXED_BASE + 3;
+        return PA_RESAMPLER_SPEEX_FIXED_BASE + 1;
 
     if (pa_streq(string, "speex-float"))
-        return PA_RESAMPLER_SPEEX_FLOAT_BASE + 3;
+        return PA_RESAMPLER_SPEEX_FLOAT_BASE + 1;
 
     return PA_RESAMPLER_INVALID;
 }
@@ -727,16 +738,17 @@ static void calc_map_table(pa_resampler *r) {
          *
          * 6) Make sure S:Left/S:Right is used: S:Left/S:Right: If not
          *    connected, mix into all D:left and all D:right channels. Gain is
-         *    0.1, the current left and right should be multiplied by 0.9.
+         *    1/9.
          *
          * 7) Make sure S:Center, S:LFE is used:
          *
          *    S:Center, S:LFE: If not connected, mix into all D:left, all
-         *    D:right, all D:center channels, gain is 0.375. The current (as
-         *    result of 1..6) factors should be multiplied by 0.75. (Alt.
-         *    suggestion: 0.25 vs. 0.5) If C-front is only mixed into
-         *    L-front/R-front if available, otherwise into all L/R channels.
-         *    Similarly for C-rear.
+         *    D:right, all D:center channels. Gain is 0.5 for center and 0.375
+         *    for LFE. C-front is only mixed into L-front/R-front if available,
+         *    otherwise into all L/R channels. Similarly for C-rear.
+         *
+         * 8) Normalize each row in the matrix such that the sum for each row is
+         *    not larger than 1.0 in order to avoid clipping.
          *
          * S: and D: shall relate to the source resp. destination channels.
          *
@@ -759,6 +771,7 @@ static void calc_map_table(pa_resampler *r) {
             ic_unconnected_right = 0,
             ic_unconnected_center = 0,
             ic_unconnected_lfe = 0;
+        bool ic_unconnected_center_mixed_in = 0;
 
         pa_assert(remix);
 
@@ -885,159 +898,98 @@ static void calc_map_table(pa_resampler *r) {
                 ic_unconnected_lfe++;
         }
 
-        if (ic_unconnected_left > 0) {
+        for (ic = 0; ic < n_ic; ic++) {
+            pa_channel_position_t a = r->i_cm.map[ic];
 
-            /* 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 multiplied by .1 */
+            if (ic_connected[ic])
+                continue;
 
             for (oc = 0; oc < n_oc; oc++) {
+                pa_channel_position_t b = r->o_cm.map[oc];
 
-                if (!on_left(r->o_cm.map[oc]))
-                    continue;
+                if (on_left(a) && on_left(b))
+                    m->map_table_f[oc][ic] = (1.f/9.f) / (float) ic_unconnected_left;
 
-                for (ic = 0; ic < n_ic; ic++) {
+                else if (on_right(a) && on_right(b))
+                    m->map_table_f[oc][ic] = (1.f/9.f) / (float) ic_unconnected_right;
 
-                    if (ic_connected[ic]) {
-                        m->map_table_f[oc][ic] *= .9f;
-                        continue;
-                    }
+                else if (on_center(a) && on_center(b)) {
+                    m->map_table_f[oc][ic] = (1.f/9.f) / (float) ic_unconnected_center;
+                    ic_unconnected_center_mixed_in = true;
 
-                    if (on_left(r->i_cm.map[ic]))
-                        m->map_table_f[oc][ic] = .1f / (float) ic_unconnected_left;
-                }
+                } else if (on_lfe(a) && !(r->flags & PA_RESAMPLER_NO_LFE))
+                    m->map_table_f[oc][ic] = .375f / (float) ic_unconnected_lfe;
             }
         }
 
-        if (ic_unconnected_right > 0) {
+        if (ic_unconnected_center > 0 && !ic_unconnected_center_mixed_in) {
+            unsigned ncenter[PA_CHANNELS_MAX];
+            bool found_frs[PA_CHANNELS_MAX];
 
-            /* 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 multiplied by .1 */
+            memset(ncenter, 0, sizeof(ncenter));
+            memset(found_frs, 0, sizeof(found_frs));
 
-            for (oc = 0; oc < n_oc; oc++) {
+            /* Hmm, as it appears there was no center channel we
+               could mix our center channel in. In this case, mix it into
+               left and right. Using .5 as the factor. */
+
+            for (ic = 0; ic < n_ic; ic++) {
 
-                if (!on_right(r->o_cm.map[oc]))
+                if (ic_connected[ic])
                     continue;
 
-                for (ic = 0; ic < n_ic; ic++) {
+                if (!on_center(r->i_cm.map[ic]))
+                    continue;
+
+                for (oc = 0; oc < n_oc; oc++) {
 
-                    if (ic_connected[ic]) {
-                        m->map_table_f[oc][ic] *= .9f;
+                    if (!on_left(r->o_cm.map[oc]) && !on_right(r->o_cm.map[oc]))
                         continue;
-                    }
 
-                    if (on_right(r->i_cm.map[ic]))
-                        m->map_table_f[oc][ic] = .1f / (float) ic_unconnected_right;
+                    if (front_rear_side(r->i_cm.map[ic]) == front_rear_side(r->o_cm.map[oc])) {
+                        found_frs[ic] = true;
+                        break;
+                    }
                 }
-            }
-        }
-
-        if (ic_unconnected_center > 0) {
-            bool mixed_in = false;
-
-            /* 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 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 (oc = 0; oc < n_oc; oc++) {
 
-                    if (ic_connected[ic]) {
-                        m->map_table_f[oc][ic] *= .9f;
+                    if (!on_left(r->o_cm.map[oc]) && !on_right(r->o_cm.map[oc]))
                         continue;
-                    }
 
-                    if (on_center(r->i_cm.map[ic])) {
-                        m->map_table_f[oc][ic] = .1f / (float) ic_unconnected_center;
-                        mixed_in = true;
-                    }
+                    if (!found_frs[ic] || front_rear_side(r->i_cm.map[ic]) == front_rear_side(r->o_cm.map[oc]))
+                        ncenter[oc]++;
                 }
             }
 
-            if (!mixed_in) {
-                unsigned ncenter[PA_CHANNELS_MAX];
-                bool found_frs[PA_CHANNELS_MAX];
+            for (oc = 0; oc < n_oc; oc++) {
 
-                memset(ncenter, 0, sizeof(ncenter));
-                memset(found_frs, 0, sizeof(found_frs));
+                if (!on_left(r->o_cm.map[oc]) && !on_right(r->o_cm.map[oc]))
+                    continue;
 
-                /* Hmm, as it appears there was no center channel we
-                   could mix our center channel in. In this case, mix it into
-                   left and right. Using .375 and 0.75 as factors. */
+                if (ncenter[oc] <= 0)
+                    continue;
 
                 for (ic = 0; ic < n_ic; ic++) {
 
-                    if (ic_connected[ic])
-                        continue;
-
                     if (!on_center(r->i_cm.map[ic]))
                         continue;
 
-                    for (oc = 0; oc < n_oc; oc++) {
-
-                        if (!on_left(r->o_cm.map[oc]) && !on_right(r->o_cm.map[oc]))
-                            continue;
-
-                        if (front_rear_side(r->i_cm.map[ic]) == front_rear_side(r->o_cm.map[oc])) {
-                            found_frs[ic] = true;
-                            break;
-                        }
-                    }
-
-                    for (oc = 0; oc < n_oc; oc++) {
-
-                        if (!on_left(r->o_cm.map[oc]) && !on_right(r->o_cm.map[oc]))
-                            continue;
-
-                        if (!found_frs[ic] || front_rear_side(r->i_cm.map[ic]) == front_rear_side(r->o_cm.map[oc]))
-                            ncenter[oc]++;
-                    }
-                }
-
-                for (oc = 0; oc < n_oc; oc++) {
-
-                    if (!on_left(r->o_cm.map[oc]) && !on_right(r->o_cm.map[oc]))
-                        continue;
-
-                    if (ncenter[oc] <= 0)
-                        continue;
-
-                    for (ic = 0; ic < n_ic; ic++) {
-
-                        if (ic_connected[ic]) {
-                            m->map_table_f[oc][ic] *= .75f;
-                            continue;
-                        }
-
-                        if (!on_center(r->i_cm.map[ic]))
-                            continue;
-
-                        if (!found_frs[ic] || front_rear_side(r->i_cm.map[ic]) == front_rear_side(r->o_cm.map[oc]))
-                            m->map_table_f[oc][ic] = .375f / (float) ncenter[oc];
-                    }
+                    if (!found_frs[ic] || front_rear_side(r->i_cm.map[ic]) == front_rear_side(r->o_cm.map[oc]))
+                        m->map_table_f[oc][ic] = .5f / (float) ncenter[oc];
                 }
             }
         }
+    }
 
-        if (ic_unconnected_lfe > 0 && !(r->flags & PA_RESAMPLER_NO_LFE)) {
-
-            /* 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++) {
-
-                if (!on_lfe(r->i_cm.map[ic]))
-                    continue;
+    for (oc = 0; oc < n_oc; oc++) {
+        float sum = 0.0f;
+        for (ic = 0; ic < n_ic; ic++)
+            sum += m->map_table_f[oc][ic];
 
-                for (oc = 0; oc < n_oc; oc++)
-                    m->map_table_f[oc][ic] = 0.375f / (float) ic_unconnected_lfe;
-            }
-        }
+        if (sum > 1.0f)
+            for (ic = 0; ic < n_ic; ic++)
+                m->map_table_f[oc][ic] /= sum;
     }
 
     /* make an 16:16 int version of the matrix */
@@ -1199,10 +1151,10 @@ static pa_memchunk *resample(pa_resampler *r, pa_memchunk *input) {
         return input;
 
     in_n_samples = (unsigned) (input->length / r->w_sz);
-    in_n_frames = (unsigned) (in_n_samples / r->o_ss.channels);
+    in_n_frames = (unsigned) (in_n_samples / r->work_channels);
 
     out_n_frames = ((in_n_frames*r->o_ss.rate)/r->i_ss.rate)+EXTRA_FRAMES;
-    out_n_samples = out_n_frames * r->o_ss.channels;
+    out_n_samples = out_n_frames * r->work_channels;
 
     r->resample_buf.index = 0;
     r->resample_buf.length = r->w_sz * out_n_samples;
@@ -1216,7 +1168,7 @@ static pa_memchunk *resample(pa_resampler *r, pa_memchunk *input) {
     }
 
     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;
+    r->resample_buf.length = out_n_frames * r->w_sz * r->work_channels;
 
     return &r->resample_buf;
 }
@@ -1269,8 +1221,15 @@ void pa_resampler_run(pa_resampler *r, const pa_memchunk *in, pa_memchunk *out)
 
     buf = (pa_memchunk*) in;
     buf = convert_to_work_format(r, buf);
-    buf = remap_channels(r, buf);
-    buf = resample(r, buf);
+    /* Try to save resampling effort: if we have more output channels than
+     * input channels, do resampling first, then remapping. */
+    if (r->o_ss.channels <= r->i_ss.channels) {
+        buf = remap_channels(r, buf);
+        buf = resample(r, buf);
+    } else {
+        buf = resample(r, buf);
+        buf = remap_channels(r, buf);
+    }
 
     if (buf->length) {
         buf = convert_from_work_format(r, buf);
@@ -1335,8 +1294,8 @@ static void libsamplerate_resample(pa_resampler *r, const pa_memchunk *input, un
     pa_assert_se(src_process(r->src.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;
-        size_t leftover_length = (in_n_frames - data.input_frames_used) * sizeof(float) * r->o_ss.channels;
+        void *leftover_data = data.data_in + data.input_frames_used * r->work_channels;
+        size_t leftover_length = (in_n_frames - data.input_frames_used) * sizeof(float) * r->work_channels;
 
         save_leftover(r, leftover_data, leftover_length);
     }
@@ -1472,7 +1431,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 (!(r->speex.state = speex_resampler_init(r->work_channels, r->i_ss.rate, r->o_ss.rate, q, &err)))
         return -1;
 
     return 0;
@@ -1491,7 +1450,7 @@ static void trivial_resample(pa_resampler *r, const pa_memchunk *input, unsigned
     pa_assert(output);
     pa_assert(out_n_frames);
 
-    fz = r->w_sz * r->o_ss.channels;
+    fz = r->w_sz * r->work_channels;
 
     src = pa_memblock_acquire_chunk(input);
     dst = pa_memblock_acquire_chunk(output);
@@ -1675,7 +1634,7 @@ static void ffmpeg_resample(pa_resampler *r, const pa_memchunk *input, unsigned
     pa_assert(output);
     pa_assert(out_n_frames);
 
-    for (c = 0; c < r->o_ss.channels; c++) {
+    for (c = 0; c < r->work_channels; c++) {
         unsigned u;
         pa_memblock *b, *w;
         int16_t *p, *t, *k, *q, *s;
@@ -1690,7 +1649,7 @@ static void ffmpeg_resample(pa_resampler *r, const pa_memchunk *input, unsigned
         k = p;
         for (u = 0; u < in_n_frames; u++) {
             *k = *t;
-            t += r->o_ss.channels;
+            t += r->work_channels;
             k ++;
         }
         pa_memblock_release(input->memblock);
@@ -1704,7 +1663,7 @@ static void ffmpeg_resample(pa_resampler *r, const pa_memchunk *input, unsigned
                                              q, p,
                                              &consumed_frames,
                                              (int) in_n_frames, (int) *out_n_frames,
-                                             c >= (unsigned) (r->o_ss.channels-1));
+                                             c >= (unsigned) (r->work_channels-1));
 
         pa_memblock_release(b);
         pa_memblock_unref(b);
@@ -1718,7 +1677,7 @@ static void ffmpeg_resample(pa_resampler *r, const pa_memchunk *input, unsigned
         for (u = 0; u < used_frames; u++) {
             *s = *q;
             q++;
-            s += r->o_ss.channels;
+            s += r->work_channels;
         }
         pa_memblock_release(output->memblock);
         pa_memblock_release(w);