1/***
2  This file is part of PulseAudio.
3
4  Copyright 2004-2006 Lennart Poettering
5
6  PulseAudio is free software; you can redistribute it and/or modify
7  it under the terms of the GNU Lesser General Public License as published
8  by the Free Software Foundation; either version 2.1 of the License,
9  or (at your option) any later version.
10
11  PulseAudio is distributed in the hope that it will be useful, but
12  WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  General Public License for more details.
15
16  You should have received a copy of the GNU Lesser General Public License
17  along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
18***/
19
20#ifdef HAVE_CONFIG_H
21#include <config.h>
22#endif
23
24#include <string.h>
25#include <math.h>
26
27#include <pulse/xmalloc.h>
28#include <pulse/timeval.h>
29#include <pulsecore/log.h>
30#include <pulsecore/macro.h>
31#include <pulsecore/strbuf.h>
32#include <pulsecore/core-util.h>
33
34#include "resampler.h"
35#include "downmix.h"
36
37/* Number of samples of extra space we allow the resamplers to return */
38#define EXTRA_FRAMES 128
39#define RESAMPLER_CACHE_SIZE_RATIO 20
40
41struct ffmpeg_data { /* data specific to ffmpeg */
42    struct AVResampleContext *state;
43};
44
45static int copy_init(pa_resampler *r);
46
47static void setup_remap(const pa_resampler *r, pa_remap_t *m, bool *lfe_remixed);
48static void free_remap(pa_remap_t *m);
49
50static int (* const init_table[])(pa_resampler *r) = {
51#ifdef HAVE_LIBSAMPLERATE
52    [PA_RESAMPLER_SRC_SINC_BEST_QUALITY]   = pa_resampler_libsamplerate_init,
53    [PA_RESAMPLER_SRC_SINC_MEDIUM_QUALITY] = pa_resampler_libsamplerate_init,
54    [PA_RESAMPLER_SRC_SINC_FASTEST]        = pa_resampler_libsamplerate_init,
55    [PA_RESAMPLER_SRC_ZERO_ORDER_HOLD]     = pa_resampler_libsamplerate_init,
56    [PA_RESAMPLER_SRC_LINEAR]              = pa_resampler_libsamplerate_init,
57#else
58    [PA_RESAMPLER_SRC_SINC_BEST_QUALITY]   = NULL,
59    [PA_RESAMPLER_SRC_SINC_MEDIUM_QUALITY] = NULL,
60    [PA_RESAMPLER_SRC_SINC_FASTEST]        = NULL,
61    [PA_RESAMPLER_SRC_ZERO_ORDER_HOLD]     = NULL,
62    [PA_RESAMPLER_SRC_LINEAR]              = NULL,
63#endif
64    [PA_RESAMPLER_TRIVIAL]                 = pa_resampler_trivial_init,
65#ifdef HAVE_SPEEX
66    [PA_RESAMPLER_SPEEX_FLOAT_BASE+0]      = pa_resampler_speex_init,
67    [PA_RESAMPLER_SPEEX_FLOAT_BASE+1]      = pa_resampler_speex_init,
68    [PA_RESAMPLER_SPEEX_FLOAT_BASE+2]      = pa_resampler_speex_init,
69    [PA_RESAMPLER_SPEEX_FLOAT_BASE+3]      = pa_resampler_speex_init,
70    [PA_RESAMPLER_SPEEX_FLOAT_BASE+4]      = pa_resampler_speex_init,
71    [PA_RESAMPLER_SPEEX_FLOAT_BASE+5]      = pa_resampler_speex_init,
72    [PA_RESAMPLER_SPEEX_FLOAT_BASE+6]      = pa_resampler_speex_init,
73    [PA_RESAMPLER_SPEEX_FLOAT_BASE+7]      = pa_resampler_speex_init,
74    [PA_RESAMPLER_SPEEX_FLOAT_BASE+8]      = pa_resampler_speex_init,
75    [PA_RESAMPLER_SPEEX_FLOAT_BASE+9]      = pa_resampler_speex_init,
76    [PA_RESAMPLER_SPEEX_FLOAT_BASE+10]     = pa_resampler_speex_init,
77    [PA_RESAMPLER_SPEEX_FIXED_BASE+0]      = pa_resampler_speex_init,
78    [PA_RESAMPLER_SPEEX_FIXED_BASE+1]      = pa_resampler_speex_init,
79    [PA_RESAMPLER_SPEEX_FIXED_BASE+2]      = pa_resampler_speex_init,
80    [PA_RESAMPLER_SPEEX_FIXED_BASE+3]      = pa_resampler_speex_init,
81    [PA_RESAMPLER_SPEEX_FIXED_BASE+4]      = pa_resampler_speex_init,
82    [PA_RESAMPLER_SPEEX_FIXED_BASE+5]      = pa_resampler_speex_init,
83    [PA_RESAMPLER_SPEEX_FIXED_BASE+6]      = pa_resampler_speex_init,
84    [PA_RESAMPLER_SPEEX_FIXED_BASE+7]      = pa_resampler_speex_init,
85    [PA_RESAMPLER_SPEEX_FIXED_BASE+8]      = pa_resampler_speex_init,
86    [PA_RESAMPLER_SPEEX_FIXED_BASE+9]      = pa_resampler_speex_init,
87    [PA_RESAMPLER_SPEEX_FIXED_BASE+10]     = pa_resampler_speex_init,
88#else
89    [PA_RESAMPLER_SPEEX_FLOAT_BASE+0]      = NULL,
90    [PA_RESAMPLER_SPEEX_FLOAT_BASE+1]      = NULL,
91    [PA_RESAMPLER_SPEEX_FLOAT_BASE+2]      = NULL,
92    [PA_RESAMPLER_SPEEX_FLOAT_BASE+3]      = NULL,
93    [PA_RESAMPLER_SPEEX_FLOAT_BASE+4]      = NULL,
94    [PA_RESAMPLER_SPEEX_FLOAT_BASE+5]      = NULL,
95    [PA_RESAMPLER_SPEEX_FLOAT_BASE+6]      = NULL,
96    [PA_RESAMPLER_SPEEX_FLOAT_BASE+7]      = NULL,
97    [PA_RESAMPLER_SPEEX_FLOAT_BASE+8]      = NULL,
98    [PA_RESAMPLER_SPEEX_FLOAT_BASE+9]      = NULL,
99    [PA_RESAMPLER_SPEEX_FLOAT_BASE+10]     = NULL,
100    [PA_RESAMPLER_SPEEX_FIXED_BASE+0]      = NULL,
101    [PA_RESAMPLER_SPEEX_FIXED_BASE+1]      = NULL,
102    [PA_RESAMPLER_SPEEX_FIXED_BASE+2]      = NULL,
103    [PA_RESAMPLER_SPEEX_FIXED_BASE+3]      = NULL,
104    [PA_RESAMPLER_SPEEX_FIXED_BASE+4]      = NULL,
105    [PA_RESAMPLER_SPEEX_FIXED_BASE+5]      = NULL,
106    [PA_RESAMPLER_SPEEX_FIXED_BASE+6]      = NULL,
107    [PA_RESAMPLER_SPEEX_FIXED_BASE+7]      = NULL,
108    [PA_RESAMPLER_SPEEX_FIXED_BASE+8]      = NULL,
109    [PA_RESAMPLER_SPEEX_FIXED_BASE+9]      = NULL,
110    [PA_RESAMPLER_SPEEX_FIXED_BASE+10]     = NULL,
111#endif
112    [PA_RESAMPLER_FFMPEG]                  = pa_resampler_ffmpeg_init,
113    [PA_RESAMPLER_AUTO]                    = NULL,
114    [PA_RESAMPLER_COPY]                    = copy_init,
115    [PA_RESAMPLER_PEAKS]                   = pa_resampler_peaks_init,
116#ifdef HAVE_SOXR
117    [PA_RESAMPLER_SOXR_MQ]                 = pa_resampler_soxr_init,
118    [PA_RESAMPLER_SOXR_HQ]                 = pa_resampler_soxr_init,
119    [PA_RESAMPLER_SOXR_VHQ]                = pa_resampler_soxr_init,
120#else
121    [PA_RESAMPLER_SOXR_MQ]                 = NULL,
122    [PA_RESAMPLER_SOXR_HQ]                 = NULL,
123    [PA_RESAMPLER_SOXR_VHQ]                = NULL,
124#endif
125};
126
127static void calculate_gcd(pa_resampler *r) {
128    unsigned gcd, n;
129
130    pa_assert(r);
131
132    gcd = r->i_ss.rate;
133    n = r->o_ss.rate;
134
135    while (n != 0) {
136        unsigned tmp = gcd;
137
138        gcd = n;
139        n = tmp % n;
140    }
141
142    r->gcd = gcd;
143}
144
145static pa_resample_method_t choose_auto_resampler(pa_resample_flags_t flags,
146    const uint32_t rate_a, const uint32_t rate_b) {
147    pa_resample_method_t method;
148
149    if (pa_resample_method_supported(PA_RESAMPLER_SPEEX_FLOAT_BASE + 1) && (rate_a != rate_b)) {
150        method = PA_RESAMPLER_SPEEX_FLOAT_BASE + 1;
151    } else {
152        method = PA_RESAMPLER_TRIVIAL;
153    }
154
155    return method;
156}
157
158static pa_resample_method_t fix_method(
159                pa_resample_flags_t flags,
160                pa_resample_method_t method,
161                const uint32_t rate_a,
162                const uint32_t rate_b) {
163
164    pa_assert(pa_sample_rate_valid(rate_a));
165    pa_assert(pa_sample_rate_valid(rate_b));
166    pa_assert(method >= 0);
167    pa_assert(method < PA_RESAMPLER_MAX);
168
169    if (!(flags & PA_RESAMPLER_VARIABLE_RATE) && rate_a == rate_b) {
170        pa_log_info("Forcing resampler 'copy', because of fixed, identical sample rates.");
171        method = PA_RESAMPLER_COPY;
172    }
173
174    if (!pa_resample_method_supported(method)) {
175        pa_log_warn("Support for resampler '%s' not compiled in, reverting to 'auto'.", pa_resample_method_to_string(method));
176        method = PA_RESAMPLER_AUTO;
177    }
178
179    switch (method) {
180        case PA_RESAMPLER_COPY:
181            if (rate_a != rate_b) {
182                pa_log_info("Resampler 'copy' cannot change sampling rate, reverting to resampler 'auto'.");
183                method = PA_RESAMPLER_AUTO;
184                break;
185            }
186            /* Else fall through */
187        case PA_RESAMPLER_FFMPEG:
188            if (flags & PA_RESAMPLER_VARIABLE_RATE) {
189                pa_log_info("Resampler '%s' cannot do variable rate, reverting to resampler 'auto'.", pa_resample_method_to_string(method));
190                method = PA_RESAMPLER_AUTO;
191            }
192            break;
193
194        /* The Peaks resampler only supports downsampling.
195         * Revert to auto if we are upsampling */
196        case PA_RESAMPLER_PEAKS:
197            if (rate_a < rate_b) {
198                pa_log_warn("The 'peaks' resampler only supports downsampling, reverting to resampler 'auto'.");
199                method = PA_RESAMPLER_AUTO;
200            }
201            break;
202
203        default:
204            break;
205    }
206
207    if (method == PA_RESAMPLER_AUTO)
208        method = choose_auto_resampler(flags, rate_a, rate_b);
209
210#ifdef HAVE_SPEEX
211    /* At this point, method is supported in the sense that it
212     * has an init function and supports the required flags. However,
213     * speex-float implementation in PulseAudio relies on the
214     * assumption that is invalid if speex has been compiled with
215     * --enable-fixed-point. Besides, speex-fixed is more efficient
216     * in this configuration. So use it instead.
217     */
218    if (method >= PA_RESAMPLER_SPEEX_FLOAT_BASE && method <= PA_RESAMPLER_SPEEX_FLOAT_MAX) {
219        if (pa_speex_is_fixed_point()) {
220            pa_log_info("Speex appears to be compiled with --enable-fixed-point. "
221                        "Switching to a fixed-point resampler because it should be faster.");
222            method = method - PA_RESAMPLER_SPEEX_FLOAT_BASE + PA_RESAMPLER_SPEEX_FIXED_BASE;
223        }
224    }
225#endif
226
227    return method;
228}
229
230/* Return true if a is a more precise sample format than b, else return false */
231static bool sample_format_more_precise(pa_sample_format_t a, pa_sample_format_t b) {
232    pa_assert(pa_sample_format_valid(a));
233    pa_assert(pa_sample_format_valid(b));
234
235    switch (a) {
236        case PA_SAMPLE_U8:
237        case PA_SAMPLE_ALAW:
238        case PA_SAMPLE_ULAW:
239            return false;
240            break;
241
242        case PA_SAMPLE_S16LE:
243        case PA_SAMPLE_S16BE:
244            if (b == PA_SAMPLE_ULAW || b == PA_SAMPLE_ALAW || b == PA_SAMPLE_U8)
245                return true;
246            else
247                return false;
248            break;
249
250        case PA_SAMPLE_S24LE:
251        case PA_SAMPLE_S24BE:
252        case PA_SAMPLE_S24_32LE:
253        case PA_SAMPLE_S24_32BE:
254            if (b == PA_SAMPLE_ULAW || b == PA_SAMPLE_ALAW || b == PA_SAMPLE_U8 ||
255                b == PA_SAMPLE_S16LE || b == PA_SAMPLE_S16BE)
256                return true;
257            else
258                return false;
259            break;
260
261        case PA_SAMPLE_FLOAT32LE:
262        case PA_SAMPLE_FLOAT32BE:
263        case PA_SAMPLE_S32LE:
264        case PA_SAMPLE_S32BE:
265            if (b == PA_SAMPLE_FLOAT32LE || b == PA_SAMPLE_FLOAT32BE ||
266                b == PA_SAMPLE_S32LE || b == PA_SAMPLE_S32BE)
267                return false;
268            else
269                return true;
270            break;
271
272        default:
273            return false;
274    }
275}
276
277static pa_sample_format_t choose_work_format(
278                    pa_resample_method_t method,
279                    pa_sample_format_t a,
280                    pa_sample_format_t b,
281                    bool map_required) {
282    pa_sample_format_t work_format;
283
284    pa_assert(pa_sample_format_valid(a));
285    pa_assert(pa_sample_format_valid(b));
286    pa_assert(method >= 0);
287    pa_assert(method < PA_RESAMPLER_MAX);
288
289    if (method >= PA_RESAMPLER_SPEEX_FIXED_BASE && method <= PA_RESAMPLER_SPEEX_FIXED_MAX)
290        method = PA_RESAMPLER_SPEEX_FIXED_BASE;
291
292    switch (method) {
293        /* This block is for resampling functions that only
294         * support the S16 sample format. */
295        case PA_RESAMPLER_SPEEX_FIXED_BASE:
296        case PA_RESAMPLER_FFMPEG:
297            work_format = PA_SAMPLE_S16NE;
298            break;
299
300        /* This block is for resampling functions that support
301         * any sample format. */
302        case PA_RESAMPLER_COPY:
303        case PA_RESAMPLER_TRIVIAL:
304            if (!map_required && a == b) {
305                work_format = a;
306                break;
307            }
308            /* If both input and output are using S32NE and we don't
309             * need any resampling we can use S32NE directly, avoiding
310             * converting back and forth between S32NE and
311             * FLOAT32NE. */
312            if ((a == PA_SAMPLE_S32NE) && (b == PA_SAMPLE_S32NE)) {
313                work_format = PA_SAMPLE_S32NE;
314                break;
315            }
316            /* Else fall through */
317        case PA_RESAMPLER_PEAKS:
318            /* PEAKS, COPY and TRIVIAL do not benefit from increased
319             * working precision, so for better performance use s16ne
320             * if either input or output fits in it. */
321            if (a == PA_SAMPLE_S16NE || b == PA_SAMPLE_S16NE) {
322                work_format = PA_SAMPLE_S16NE;
323                break;
324            }
325            /* Else fall through */
326        case PA_RESAMPLER_SOXR_MQ:
327        case PA_RESAMPLER_SOXR_HQ:
328        case PA_RESAMPLER_SOXR_VHQ:
329            /* Do processing with max precision of input and output. */
330            if (sample_format_more_precise(a, PA_SAMPLE_S16NE) ||
331                sample_format_more_precise(b, PA_SAMPLE_S16NE))
332                work_format = PA_SAMPLE_FLOAT32NE;
333            else
334                work_format = PA_SAMPLE_S16NE;
335            break;
336
337        default:
338            work_format = PA_SAMPLE_FLOAT32NE;
339    }
340
341    return work_format;
342}
343
344pa_resampler* pa_resampler_new(
345        pa_mempool *pool,
346        const pa_sample_spec *a,
347        const pa_channel_map *am,
348        const pa_sample_spec *b,
349        const pa_channel_map *bm,
350        unsigned crossover_freq,
351        pa_resample_method_t method,
352        pa_resample_flags_t flags) {
353
354    pa_resampler *r = NULL;
355    bool lfe_remixed = false;
356
357    pa_assert(pool);
358    pa_assert(a);
359    pa_assert(b);
360    pa_assert(pa_sample_spec_valid(a));
361    pa_assert(pa_sample_spec_valid(b));
362    pa_assert(method >= 0);
363    pa_assert(method < PA_RESAMPLER_MAX);
364
365    method = fix_method(flags, method, a->rate, b->rate);
366
367    r = pa_xnew0(pa_resampler, 1);
368    r->mempool = pool;
369    r->method = method;
370    r->flags = flags;
371    r->in_frames = 0;
372    r->out_frames = 0;
373
374    /* Fill sample specs */
375    r->i_ss = *a;
376    r->o_ss = *b;
377    calculate_gcd(r);
378
379    if (am)
380        r->i_cm = *am;
381    else if (!pa_channel_map_init_auto(&r->i_cm, r->i_ss.channels, PA_CHANNEL_MAP_DEFAULT))
382        goto fail;
383
384    if (bm)
385        r->o_cm = *bm;
386    else if (!pa_channel_map_init_auto(&r->o_cm, r->o_ss.channels, PA_CHANNEL_MAP_DEFAULT))
387        goto fail;
388
389    r->i_fz = pa_frame_size(a);
390    r->o_fz = pa_frame_size(b);
391
392    r->map_required = (r->i_ss.channels != r->o_ss.channels || (!(r->flags & PA_RESAMPLER_NO_REMAP) &&
393        !pa_channel_map_equal(&r->i_cm, &r->o_cm)));
394
395    r->work_format = choose_work_format(method, a->format, b->format, r->map_required);
396    r->w_sz = pa_sample_size_of_format(r->work_format);
397
398    if (r->i_ss.format != r->work_format) {
399        if (r->work_format == PA_SAMPLE_FLOAT32NE) {
400            if (!(r->to_work_format_func = pa_get_convert_to_float32ne_function(r->i_ss.format)))
401                goto fail;
402        } else {
403            pa_assert(r->work_format == PA_SAMPLE_S16NE);
404            if (!(r->to_work_format_func = pa_get_convert_to_s16ne_function(r->i_ss.format)))
405                goto fail;
406        }
407    }
408
409    if (r->o_ss.format != r->work_format) {
410        if (r->work_format == PA_SAMPLE_FLOAT32NE) {
411            if (!(r->from_work_format_func = pa_get_convert_from_float32ne_function(r->o_ss.format)))
412                goto fail;
413        } else {
414            pa_assert(r->work_format == PA_SAMPLE_S16NE);
415            if (!(r->from_work_format_func = pa_get_convert_from_s16ne_function(r->o_ss.format)))
416                goto fail;
417        }
418    }
419
420    if (r->o_ss.channels <= r->i_ss.channels) {
421        /* pipeline is: format conv. -> remap -> resample -> format conv. */
422        r->work_channels = r->o_ss.channels;
423
424        /* leftover buffer is remap output buffer (before resampling) */
425        r->leftover_buf = &r->remap_buf;
426        r->leftover_buf_size = &r->remap_buf_size;
427        r->have_leftover = &r->leftover_in_remap;
428    } else {
429        /* pipeline is: format conv. -> resample -> remap -> format conv. */
430        r->work_channels = r->i_ss.channels;
431
432        /* leftover buffer is to_work output buffer (before resampling) */
433        r->leftover_buf = &r->to_work_format_buf;
434        r->leftover_buf_size = &r->to_work_format_buf_size;
435        r->have_leftover = &r->leftover_in_to_work;
436    }
437    r->w_fz = pa_sample_size_of_format(r->work_format) * r->work_channels;
438
439    pa_log_debug("Resampler:");
440    pa_log_debug("  rate %d -> %d (method %s)",
441        a->rate, b->rate, pa_resample_method_to_string(r->method));
442    pa_log_debug("  format %s -> %s (intermediate %s)",
443        pa_sample_format_to_string(a->format), pa_sample_format_to_string(b->format),
444        pa_sample_format_to_string(r->work_format));
445    pa_log_debug("  channels %d -> %d (resampling %d)",
446        a->channels, b->channels, r->work_channels);
447
448    /* set up the remap structure */
449    if (r->map_required)
450        setup_remap(r, &r->remap, &lfe_remixed);
451
452    if (lfe_remixed && crossover_freq > 0) {
453        pa_sample_spec wss = r->o_ss;
454        wss.format = r->work_format;
455        /* FIXME: For now just hardcode maxrewind to 3 seconds */
456        r->lfe_filter = pa_lfe_filter_new(&wss, &r->o_cm, (float)crossover_freq, b->rate * 3);
457        pa_log_debug("  lfe filter activated (LR4 type), the crossover_freq = %uHz", crossover_freq);
458    }
459
460    /* initialize implementation */
461    if (init_table[method](r) < 0)
462        goto fail;
463
464    return r;
465
466fail:
467    if (r->lfe_filter)
468      pa_lfe_filter_free(r->lfe_filter);
469    pa_xfree(r);
470
471    return NULL;
472}
473
474void pa_resampler_free(pa_resampler *r) {
475    pa_assert(r);
476
477    if (r->impl.free)
478        r->impl.free(r);
479    else
480        pa_xfree(r->impl.data);
481
482    if (r->lfe_filter)
483        pa_lfe_filter_free(r->lfe_filter);
484
485    if (r->to_work_format_buf.memblock)
486        pa_memblock_unref(r->to_work_format_buf.memblock);
487    if (r->remap_buf.memblock)
488        pa_memblock_unref(r->remap_buf.memblock);
489    if (r->resample_buf.memblock)
490        pa_memblock_unref(r->resample_buf.memblock);
491    if (r->from_work_format_buf.memblock)
492        pa_memblock_unref(r->from_work_format_buf.memblock);
493
494    free_remap(&r->remap);
495
496    pa_xfree(r);
497}
498
499void pa_resampler_set_input_rate(pa_resampler *r, uint32_t rate) {
500    pa_assert(r);
501    pa_assert(rate > 0);
502    pa_assert(r->impl.update_rates);
503
504    if (r->i_ss.rate == rate)
505        return;
506
507    /* Recalculate delay counters */
508    r->in_frames = pa_resampler_get_delay(r, false);
509    r->out_frames = 0;
510
511    r->i_ss.rate = rate;
512    calculate_gcd(r);
513
514    r->impl.update_rates(r);
515}
516
517void pa_resampler_set_output_rate(pa_resampler *r, uint32_t rate) {
518    pa_assert(r);
519    pa_assert(rate > 0);
520    pa_assert(r->impl.update_rates);
521
522    if (r->o_ss.rate == rate)
523        return;
524
525    /* Recalculate delay counters */
526    r->in_frames = pa_resampler_get_delay(r, false);
527    r->out_frames = 0;
528
529    r->o_ss.rate = rate;
530    calculate_gcd(r);
531
532    r->impl.update_rates(r);
533
534    if (r->lfe_filter)
535        pa_lfe_filter_update_rate(r->lfe_filter, rate);
536}
537
538/* pa_resampler_request() and pa_resampler_result() should be as exact as
539 * possible to ensure that no samples are lost or duplicated during rewinds.
540 * Ignore the leftover buffer, the value appears to be wrong for ffmpeg
541 * and 0 in all other cases. If the resampler is NULL it means that no
542 * resampling is necessary and the input length equals the output length.
543 * FIXME: These functions are not exact for the soxr resamplers because
544 * soxr uses a different algorithm. */
545size_t pa_resampler_request(pa_resampler *r, size_t out_length) {
546    size_t in_length;
547
548    if (!r || out_length == 0)
549        return out_length;
550
551    /* Convert to output frames */
552    out_length = out_length / r->o_fz;
553
554    /* Convert to input frames. The equation matches exactly the
555     * behavior of the used resamplers and will calculate the
556     * minimum number of input frames that are needed to produce
557     * the given number of output frames. */
558    in_length = (out_length - 1) * r->i_ss.rate / r->o_ss.rate + 1;
559
560    /* Convert to input length */
561    return in_length * r->i_fz;
562}
563
564size_t pa_resampler_result(pa_resampler *r, size_t in_length) {
565    size_t out_length;
566
567    if (!r)
568        return in_length;
569
570    /* Convert to intput frames */
571    in_length = in_length / r->i_fz;
572
573     /* soxr processes samples in blocks, depending on the ratio.
574      * Therefore samples  that do not fit into a block must be
575      * ignored. */
576    if (r->method == PA_RESAMPLER_SOXR_MQ || r->method == PA_RESAMPLER_SOXR_HQ || r->method == PA_RESAMPLER_SOXR_VHQ) {
577        double ratio;
578        size_t block_size;
579        int k;
580
581        ratio = (double)r->i_ss.rate / (double)r->o_ss.rate;
582
583        for (k = 0; k < 7; k++) {
584            if (ratio < pow(2, k + 1))
585                break;
586        }
587        block_size = pow(2, k);
588        in_length = in_length - in_length % block_size;
589    }
590
591    /* Convert to output frames. This matches exactly the algorithm
592     * used by the resamplers except for the soxr resamplers. */
593
594     out_length = in_length * r->o_ss.rate / r->i_ss.rate;
595     if ((double)in_length * (double)r->o_ss.rate / (double)r->i_ss.rate - out_length > 0)
596         out_length++;
597     /* The libsamplerate resamplers return one sample more if the result is integral and the ratio is not integral. */
598     else if (r->method >= PA_RESAMPLER_SRC_SINC_BEST_QUALITY && r->method <= PA_RESAMPLER_SRC_SINC_FASTEST && r->i_ss.rate > r->o_ss.rate && r->i_ss.rate % r->o_ss.rate > 0 && (double)in_length * (double)r->o_ss.rate / (double)r->i_ss.rate - out_length <= 0)
599         out_length++;
600     else if (r->method == PA_RESAMPLER_SRC_ZERO_ORDER_HOLD && r->i_ss.rate > r->o_ss.rate && (double)in_length * (double)r->o_ss.rate / (double)r->i_ss.rate - out_length <= 0)
601         out_length++;
602
603    /* Convert to output length */
604    return out_length * r->o_fz;
605}
606
607size_t pa_resampler_max_block_size(pa_resampler *r) {
608    size_t block_size_max;
609    pa_sample_spec max_ss;
610    size_t max_fs;
611    size_t frames;
612
613    pa_assert(r);
614
615    block_size_max = pa_mempool_block_size_max(r->mempool);
616
617    /* We deduce the "largest" sample spec we're using during the
618     * conversion */
619    max_ss.channels = (uint8_t) (PA_MAX(r->i_ss.channels, r->o_ss.channels));
620
621    /* We silently assume that the format enum is ordered by size */
622    max_ss.format = PA_MAX(r->i_ss.format, r->o_ss.format);
623    max_ss.format = PA_MAX(max_ss.format, r->work_format);
624
625    max_ss.rate = PA_MAX(r->i_ss.rate, r->o_ss.rate);
626
627    max_fs = pa_frame_size(&max_ss);
628    frames = block_size_max / max_fs - EXTRA_FRAMES;
629
630    pa_assert(frames >= (r->leftover_buf->length / r->w_fz));
631    if (*r->have_leftover)
632        frames -= r->leftover_buf->length / r->w_fz;
633
634    block_size_max = ((uint64_t) frames * r->i_ss.rate / max_ss.rate) * r->i_fz;
635
636    if (block_size_max > 0)
637        return block_size_max;
638    else
639        /* A single input frame may result in so much output that it doesn't
640         * fit in one standard memblock (e.g. converting 1 Hz to 44100 Hz). In
641         * this case the max block size will be set to one frame, and some
642         * memory will be probably be allocated with malloc() instead of using
643         * the memory pool.
644         *
645         * XXX: Should we support this case at all? We could also refuse to
646         * create resamplers whose max block size would exceed the memory pool
647         * block size. In this case also updating the resampler rate should
648         * fail if the new rate would cause an excessive max block size (in
649         * which case the stream would probably have to be killed). */
650        return r->i_fz;
651}
652
653void pa_resampler_reset(pa_resampler *r) {
654    pa_assert(r);
655
656    if (r->impl.reset)
657        r->impl.reset(r);
658
659    if (r->lfe_filter)
660        pa_lfe_filter_reset(r->lfe_filter);
661
662    *r->have_leftover = false;
663
664    r->in_frames = 0;
665    r->out_frames = 0;
666}
667
668/* This function runs amount bytes of data from the history queue through the
669 * resampler and discards the result. The history queue is unchanged after the
670 * call. This is used to preload a resampler after a reset. Returns the number
671 * of frames produced by the resampler. */
672size_t pa_resampler_prepare(pa_resampler *r, pa_memblockq *history_queue, size_t amount) {
673    size_t history_bytes, max_block_size, out_size;
674    int64_t to_run;
675
676    pa_assert(r);
677
678    if (!history_queue || amount == 0)
679        return 0;
680
681    /* Rewind the LFE filter by the amount of history data. */
682    history_bytes = pa_resampler_result(r, amount);
683    if (r->lfe_filter)
684        pa_lfe_filter_rewind(r->lfe_filter, history_bytes);
685
686    pa_memblockq_rewind(history_queue, amount);
687    max_block_size = pa_resampler_max_block_size(r);
688    to_run = amount;
689    out_size = 0;
690
691    while (to_run > 0) {
692        pa_memchunk in_chunk, out_chunk;
693        size_t current;
694
695        current = PA_MIN(to_run, (int64_t) max_block_size);
696
697        /* Get data from memblockq */
698        if (pa_memblockq_peek_fixed_size(history_queue, current, &in_chunk) < 0) {
699            pa_log_warn("Could not read history data for resampler.");
700
701            /* Restore queue to original state and reset resampler */
702            pa_memblockq_drop(history_queue, to_run);
703            pa_resampler_reset(r);
704            return out_size;
705        }
706
707        /* Run the resampler */
708        pa_resampler_run(r, &in_chunk, &out_chunk);
709
710        /* Discard result */
711        if (out_chunk.length != 0) {
712            out_size += out_chunk.length;
713            pa_memblock_unref(out_chunk.memblock);
714        }
715
716        pa_memblock_unref(in_chunk.memblock);
717        pa_memblockq_drop(history_queue, current);
718        to_run -= current;
719    }
720
721    return out_size;
722}
723
724size_t pa_resampler_rewind(pa_resampler *r, size_t out_bytes, pa_memblockq *history_queue, size_t amount) {
725    pa_assert(r);
726
727    /* For now, we don't have any rewindable resamplers, so we just reset
728     * the resampler if we cannot rewind using pa_resampler_prepare(). */
729    if (r->impl.reset && !history_queue)
730        r->impl.reset(r);
731
732    if (r->lfe_filter)
733        pa_lfe_filter_rewind(r->lfe_filter, out_bytes);
734
735    if (!history_queue) {
736        *r->have_leftover = false;
737
738        r->in_frames = 0;
739        r->out_frames = 0;
740    }
741
742    if (history_queue && amount > 0)
743        return pa_resampler_prepare(r, history_queue, amount);
744
745    return 0;
746}
747
748pa_resample_method_t pa_resampler_get_method(pa_resampler *r) {
749    pa_assert(r);
750
751    return r->method;
752}
753
754const pa_channel_map* pa_resampler_input_channel_map(pa_resampler *r) {
755    pa_assert(r);
756
757    return &r->i_cm;
758}
759
760const pa_sample_spec* pa_resampler_input_sample_spec(pa_resampler *r) {
761    pa_assert(r);
762
763    return &r->i_ss;
764}
765
766const pa_channel_map* pa_resampler_output_channel_map(pa_resampler *r) {
767    pa_assert(r);
768
769    return &r->o_cm;
770}
771
772const pa_sample_spec* pa_resampler_output_sample_spec(pa_resampler *r) {
773    pa_assert(r);
774
775    return &r->o_ss;
776}
777
778static const char * const resample_methods[] = {
779    "src-sinc-best-quality",
780    "src-sinc-medium-quality",
781    "src-sinc-fastest",
782    "src-zero-order-hold",
783    "src-linear",
784    "trivial",
785    "speex-float-0",
786    "speex-float-1",
787    "speex-float-2",
788    "speex-float-3",
789    "speex-float-4",
790    "speex-float-5",
791    "speex-float-6",
792    "speex-float-7",
793    "speex-float-8",
794    "speex-float-9",
795    "speex-float-10",
796    "speex-fixed-0",
797    "speex-fixed-1",
798    "speex-fixed-2",
799    "speex-fixed-3",
800    "speex-fixed-4",
801    "speex-fixed-5",
802    "speex-fixed-6",
803    "speex-fixed-7",
804    "speex-fixed-8",
805    "speex-fixed-9",
806    "speex-fixed-10",
807    "ffmpeg",
808    "auto",
809    "copy",
810    "peaks",
811    "soxr-mq",
812    "soxr-hq",
813    "soxr-vhq"
814};
815
816const char *pa_resample_method_to_string(pa_resample_method_t m) {
817
818    if (m < 0 || m >= PA_RESAMPLER_MAX)
819        return NULL;
820
821    return resample_methods[m];
822}
823
824int pa_resample_method_supported(pa_resample_method_t m) {
825
826    if (m < 0 || m >= PA_RESAMPLER_MAX)
827        return 0;
828
829#ifndef HAVE_LIBSAMPLERATE
830    if (m <= PA_RESAMPLER_SRC_LINEAR)
831        return 0;
832#endif
833
834#ifndef HAVE_SPEEX
835    if (m >= PA_RESAMPLER_SPEEX_FLOAT_BASE && m <= PA_RESAMPLER_SPEEX_FLOAT_MAX)
836        return 0;
837    if (m >= PA_RESAMPLER_SPEEX_FIXED_BASE && m <= PA_RESAMPLER_SPEEX_FIXED_MAX)
838        return 0;
839#endif
840
841#ifndef HAVE_SOXR
842    if (m >= PA_RESAMPLER_SOXR_MQ && m <= PA_RESAMPLER_SOXR_VHQ)
843        return 0;
844#endif
845
846    return 1;
847}
848
849pa_resample_method_t pa_parse_resample_method(const char *string) {
850    pa_resample_method_t m;
851
852    pa_assert(string);
853
854    for (m = 0; m < PA_RESAMPLER_MAX; m++)
855        if (pa_streq(string, resample_methods[m]))
856            return m;
857
858    if (pa_streq(string, "speex-fixed"))
859        return PA_RESAMPLER_SPEEX_FIXED_BASE + 1;
860
861    if (pa_streq(string, "speex-float"))
862        return PA_RESAMPLER_SPEEX_FLOAT_BASE + 1;
863
864    return PA_RESAMPLER_INVALID;
865}
866
867static bool on_left(pa_channel_position_t p) {
868
869    return
870        p == PA_CHANNEL_POSITION_FRONT_LEFT ||
871        p == PA_CHANNEL_POSITION_REAR_LEFT ||
872        p == PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER ||
873        p == PA_CHANNEL_POSITION_SIDE_LEFT ||
874        p == PA_CHANNEL_POSITION_TOP_FRONT_LEFT ||
875        p == PA_CHANNEL_POSITION_TOP_REAR_LEFT;
876}
877
878static bool on_right(pa_channel_position_t p) {
879
880    return
881        p == PA_CHANNEL_POSITION_FRONT_RIGHT ||
882        p == PA_CHANNEL_POSITION_REAR_RIGHT ||
883        p == PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER ||
884        p == PA_CHANNEL_POSITION_SIDE_RIGHT ||
885        p == PA_CHANNEL_POSITION_TOP_FRONT_RIGHT ||
886        p == PA_CHANNEL_POSITION_TOP_REAR_RIGHT;
887}
888
889static bool on_center(pa_channel_position_t p) {
890
891    return
892        p == PA_CHANNEL_POSITION_FRONT_CENTER ||
893        p == PA_CHANNEL_POSITION_REAR_CENTER ||
894        p == PA_CHANNEL_POSITION_TOP_CENTER ||
895        p == PA_CHANNEL_POSITION_TOP_FRONT_CENTER ||
896        p == PA_CHANNEL_POSITION_TOP_REAR_CENTER;
897}
898
899static bool on_lfe(pa_channel_position_t p) {
900    return
901        p == PA_CHANNEL_POSITION_LFE;
902}
903
904static bool on_front(pa_channel_position_t p) {
905    return
906        p == PA_CHANNEL_POSITION_FRONT_LEFT ||
907        p == PA_CHANNEL_POSITION_FRONT_RIGHT ||
908        p == PA_CHANNEL_POSITION_FRONT_CENTER ||
909        p == PA_CHANNEL_POSITION_TOP_FRONT_LEFT ||
910        p == PA_CHANNEL_POSITION_TOP_FRONT_RIGHT ||
911        p == PA_CHANNEL_POSITION_TOP_FRONT_CENTER ||
912        p == PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER ||
913        p == PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER;
914}
915
916static bool on_rear(pa_channel_position_t p) {
917    return
918        p == PA_CHANNEL_POSITION_REAR_LEFT ||
919        p == PA_CHANNEL_POSITION_REAR_RIGHT ||
920        p == PA_CHANNEL_POSITION_REAR_CENTER ||
921        p == PA_CHANNEL_POSITION_TOP_REAR_LEFT ||
922        p == PA_CHANNEL_POSITION_TOP_REAR_RIGHT ||
923        p == PA_CHANNEL_POSITION_TOP_REAR_CENTER;
924}
925
926static bool on_side(pa_channel_position_t p) {
927    return
928        p == PA_CHANNEL_POSITION_SIDE_LEFT ||
929        p == PA_CHANNEL_POSITION_SIDE_RIGHT ||
930        p == PA_CHANNEL_POSITION_TOP_CENTER;
931}
932
933typedef enum pa_channel_direction {
934    ON_FRONT,
935    ON_REAR,
936    ON_SIDE,
937    ON_OTHER
938} pa_channel_direction_t;
939
940static int front_rear_side(pa_channel_position_t p) {
941    if (on_front(p))
942        return ON_FRONT;
943    if (on_rear(p))
944        return ON_REAR;
945    if (on_side(p))
946        return ON_SIDE;
947    return ON_OTHER;
948}
949
950/* For downmixing other output format, usage [direction_of_input_channel][direction_of_output_channel] */
951/*           layout            */
952/*        front rear side      */
953/* front |     |    |    |     */
954/* rear  |     |    |    |     */
955/* side  |     |    |    |     */
956static const float directionDownMixMatrix[ON_OTHER][ON_OTHER] = {
957    {1.0f, 0.5f, 0.7071f},
958    {0.5f, 1.0f, 0.7071f},
959    {0.7071f, 0.7071f, 1.0f},
960};
961
962/* Fill a map of which output channels should get mono from input, not including
963 * LFE output channels. (The LFE output channels are mapped separately.)
964 */
965static void setup_oc_mono_map(const pa_resampler *r, float *oc_mono_map) {
966    unsigned oc;
967    unsigned n_oc;
968    bool found_oc_for_mono = false;
969
970    pa_assert(r);
971    pa_assert(oc_mono_map);
972
973    n_oc = r->o_ss.channels;
974
975    if (!(r->flags & PA_RESAMPLER_NO_FILL_SINK)) {
976        /* Mono goes to all non-LFE output channels and we're done. */
977        for (oc = 0; oc < n_oc; oc++)
978            oc_mono_map[oc] = on_lfe(r->o_cm.map[oc]) ? 0.0f : 1.0f;
979        return;
980    } else {
981        /* Initialize to all zero so we can select individual channels below. */
982        for (oc = 0; oc < n_oc; oc++)
983            oc_mono_map[oc] = 0.0f;
984    }
985
986    for (oc = 0; oc < n_oc; oc++) {
987        if (r->o_cm.map[oc] == PA_CHANNEL_POSITION_MONO) {
988            oc_mono_map[oc] = 1.0f;
989            found_oc_for_mono = true;
990        }
991    }
992    if (found_oc_for_mono)
993        return;
994
995    for (oc = 0; oc < n_oc; oc++) {
996        if (r->o_cm.map[oc] == PA_CHANNEL_POSITION_FRONT_CENTER) {
997            oc_mono_map[oc] = 1.0f;
998            found_oc_for_mono = true;
999        }
1000    }
1001    if (found_oc_for_mono)
1002        return;
1003
1004    for (oc = 0; oc < n_oc; oc++) {
1005        if (r->o_cm.map[oc] == PA_CHANNEL_POSITION_FRONT_LEFT || r->o_cm.map[oc] == PA_CHANNEL_POSITION_FRONT_RIGHT) {
1006            oc_mono_map[oc] = 1.0f;
1007            found_oc_for_mono = true;
1008        }
1009    }
1010    if (found_oc_for_mono)
1011        return;
1012
1013    /* Give up on finding a suitable map for mono, and just send it to all
1014     * non-LFE output channels.
1015     */
1016    for (oc = 0; oc < n_oc; oc++)
1017        oc_mono_map[oc] = on_lfe(r->o_cm.map[oc]) ? 0.0f : 1.0f;
1018}
1019
1020static void setup_remap(const pa_resampler *r, pa_remap_t *m, bool *lfe_remixed) {
1021    unsigned oc, ic;
1022    unsigned n_oc, n_ic;
1023    bool ic_connected[PA_CHANNELS_MAX];
1024    pa_strbuf *s;
1025    char *t;
1026
1027    pa_assert(r);
1028    pa_assert(m);
1029    pa_assert(lfe_remixed);
1030
1031    n_oc = r->o_ss.channels;
1032    n_ic = r->i_ss.channels;
1033
1034    m->format = r->work_format;
1035    m->i_ss = r->i_ss;
1036    m->o_ss = r->o_ss;
1037
1038    memset(m->map_table_f, 0, sizeof(m->map_table_f));
1039    memset(m->map_table_i, 0, sizeof(m->map_table_i));
1040
1041    memset(ic_connected, 0, sizeof(ic_connected));
1042    *lfe_remixed = false;
1043
1044    if (r->flags & PA_RESAMPLER_NO_REMAP) {
1045        for (oc = 0; oc < PA_MIN(n_ic, n_oc); oc++)
1046            m->map_table_f[oc][oc] = 1.0f;
1047
1048    } else if (r->flags & PA_RESAMPLER_NO_REMIX) {
1049        for (oc = 0; oc < n_oc; oc++) {
1050            pa_channel_position_t b = r->o_cm.map[oc];
1051
1052            for (ic = 0; ic < n_ic; ic++) {
1053                pa_channel_position_t a = r->i_cm.map[ic];
1054
1055                /* We shall not do any remixing. Hence, just check by name */
1056                if (a == b)
1057                    m->map_table_f[oc][ic] = 1.0f;
1058            }
1059        }
1060    } else {
1061
1062        /* OK, we shall do the full monty: upmixing and downmixing. Our
1063         * algorithm is relatively simple, does not do spacialization, or delay
1064         * elements. LFE filters are done after the remap step. Patches are always
1065         * welcome, though. Oh, and it doesn't do any matrix decoding. (Which
1066         * probably wouldn't make any sense anyway.)
1067         *
1068         * This code is not idempotent: downmixing an upmixed stereo stream is
1069         * not identical to the original. The volume will not match, and the
1070         * two channels will be a linear combination of both.
1071         *
1072         * This is loosely based on random suggestions found on the Internet,
1073         * such as this:
1074         * http://www.halfgaar.net/surround-sound-in-linux and the alsa upmix
1075         * plugin.
1076         *
1077         * The algorithm works basically like this:
1078         *
1079         * 1) Connect all channels with matching names.
1080         *    This also includes fixing confusion between "5.1" and
1081         *    "5.1 (Side)" layouts, done by mpv.
1082         *
1083         * 2) Mono Handling:
1084         *    S:Mono: See setup_oc_mono_map().
1085         *    D:Mono: Avg all S:channels
1086         *
1087         * 3) Mix D:Left, D:Right (if PA_RESAMPLER_NO_FILL_SINK is clear):
1088         *    D:Left: If not connected, avg all S:Left
1089         *    D:Right: If not connected, avg all S:Right
1090         *
1091         * 4) Mix D:Center (if PA_RESAMPLER_NO_FILL_SINK is clear):
1092         *    If not connected, avg all S:Center
1093         *    If still not connected, avg all S:Left, S:Right
1094         *
1095         * 5) Mix D:LFE
1096         *    If not connected, avg all S:*
1097         *
1098         * 6) Make sure S:Left/S:Right is used: S:Left/S:Right: If not
1099         *    connected, mix into all D:left and all D:right channels. Gain is
1100         *    1/9.
1101         *
1102         * 7) Make sure S:Center, S:LFE is used:
1103         *
1104         *    S:Center, S:LFE: If not connected, mix into all D:left, all
1105         *    D:right, all D:center channels. Gain is 0.5 for center and 0.375
1106         *    for LFE. C-front is only mixed into L-front/R-front if available,
1107         *    otherwise into all L/R channels. Similarly for C-rear.
1108         *
1109         * 8) Normalize each row in the matrix such that the sum for each row is
1110         *    not larger than 1.0 in order to avoid clipping.
1111         *
1112         * S: and D: shall relate to the source resp. destination channels.
1113         *
1114         * Rationale: 1, 2 are probably obvious. For 3: this copies front to
1115         * rear if needed. For 4: we try to find some suitable C source for C,
1116         * if we don't find any, we avg L and R. For 5: LFE is mixed from all
1117         * channels. For 6: the rear channels should not be dropped entirely,
1118         * however have only minimal impact. For 7: movies usually encode
1119         * speech on the center channel. Thus we have to make sure this channel
1120         * is distributed to L and R if not available in the output. Also, LFE
1121         * is used to achieve a greater dynamic range, and thus we should try
1122         * to do our best to pass it to L+R.
1123         */
1124
1125        unsigned
1126            ic_left = 0,
1127            ic_right = 0,
1128            ic_center = 0,
1129            ic_unconnected_center = 0,
1130            ic_unconnected_lfe = 0;
1131        bool ic_unconnected_center_mixed_in = 0;
1132        float oc_mono_map[PA_CHANNELS_MAX];
1133
1134        for (ic = 0; ic < n_ic; ic++) {
1135            if (on_left(r->i_cm.map[ic]))
1136                ic_left++;
1137            if (on_right(r->i_cm.map[ic]))
1138                ic_right++;
1139            if (on_center(r->i_cm.map[ic]))
1140                ic_center++;
1141        }
1142
1143        setup_oc_mono_map(r, oc_mono_map);
1144
1145        // mono ouput or input channel = output channel
1146        for (oc = 0; oc < n_oc; oc++) {
1147            bool oc_connected = false;
1148            pa_channel_position_t b = r->o_cm.map[oc];
1149
1150            for (ic = 0; ic < n_ic; ic++) {
1151                pa_channel_position_t a = r->i_cm.map[ic];
1152
1153                if (a == b) {
1154                    m->map_table_f[oc][ic] = 1.0f;
1155
1156                    oc_connected = true;
1157                    ic_connected[ic] = true;
1158                }
1159                else if (a == PA_CHANNEL_POSITION_MONO && oc_mono_map[oc] > 0.0f) {
1160                    m->map_table_f[oc][ic] = oc_mono_map[oc];
1161
1162                    oc_connected = true;
1163                    ic_connected[ic] = true;
1164                }
1165                else if (b == PA_CHANNEL_POSITION_MONO) {
1166                    m->map_table_f[oc][ic] = 1.0f / (float) n_ic;
1167
1168                    oc_connected = true;
1169                    ic_connected[ic] = true;
1170                }
1171            }
1172            // output channel has no relating input channel, upmix here
1173            if (!oc_connected) {
1174                /* Try to find matching input ports for this output port */
1175
1176                if (on_left(b) && !(r->flags & PA_RESAMPLER_NO_FILL_SINK)) {
1177
1178                    /* We are not connected and on the left side, let's
1179                     * average all left side input channels. */
1180
1181                    if (ic_left > 0)
1182                        for (ic = 0; ic < n_ic; ic++)
1183                            if (on_left(r->i_cm.map[ic])) {
1184                                m->map_table_f[oc][ic] = 1.0f / (float) ic_left;
1185                                ic_connected[ic] = true;
1186                            }
1187
1188                    /* We ignore the case where there is no left input channel.
1189                     * Something is really wrong in this case anyway. */
1190
1191                } else if (on_right(b) && !(r->flags & PA_RESAMPLER_NO_FILL_SINK)) {
1192
1193                    /* We are not connected and on the right side, let's
1194                     * average all right side input channels. */
1195
1196                    if (ic_right > 0)
1197                        for (ic = 0; ic < n_ic; ic++)
1198                            if (on_right(r->i_cm.map[ic])) {
1199                                m->map_table_f[oc][ic] = 1.0f / (float) ic_right;
1200                                ic_connected[ic] = true;
1201                            }
1202
1203                    /* We ignore the case where there is no right input
1204                     * channel. Something is really wrong in this case anyway.
1205                     * */
1206
1207                } else if (on_center(b) && !(r->flags & PA_RESAMPLER_NO_FILL_SINK)) {
1208
1209                    if (ic_center > 0) {
1210
1211                        /* We are not connected and at the center. Let's average
1212                         * all center input channels. */
1213
1214                        for (ic = 0; ic < n_ic; ic++)
1215                            if (on_center(r->i_cm.map[ic])) {
1216                                m->map_table_f[oc][ic] = 1.0f / (float) ic_center;
1217                                ic_connected[ic] = true;
1218                            }
1219
1220                    } else if (ic_left + ic_right > 0) {
1221
1222                        /* Hmm, no center channel around, let's synthesize it
1223                         * by mixing L and R.*/
1224
1225                        for (ic = 0; ic < n_ic; ic++)
1226                            if (on_left(r->i_cm.map[ic]) || on_right(r->i_cm.map[ic])) {
1227                                m->map_table_f[oc][ic] = 1.0f / (float) (ic_left + ic_right);
1228                                ic_connected[ic] = true;
1229                            }
1230                    }
1231
1232                    /* We ignore the case where there is not even a left or
1233                     * right input channel. Something is really wrong in this
1234                     * case anyway. */
1235
1236                } else if (on_lfe(b) && (r->flags & PA_RESAMPLER_PRODUCE_LFE)) {
1237
1238                    /* We are not connected and an LFE. Let's average all
1239                     * channels for LFE. */
1240
1241                    for (ic = 0; ic < n_ic; ic++)
1242                        m->map_table_f[oc][ic] = 1.0f / (float) n_ic;
1243
1244                    /* Please note that a channel connected to LFE doesn't
1245                     * really count as connected. */
1246
1247                    *lfe_remixed = true;
1248                }
1249            }
1250        } /* upmix done, so far every output should be connected*/
1251        /* downmix here, connect unconnected input here */
1252        /* check if output format is supported with downmix table */
1253        pa_channel_layout_index_t output_layout_index = pa_channel_map_to_index(&r->o_cm);
1254
1255        for (ic = 0; ic < n_ic; ic++) {
1256            pa_channel_position_t a = r->i_cm.map[ic];
1257            if (ic_connected[ic]) {
1258                continue;
1259            } else if (on_center(a)) {
1260                ic_unconnected_center++;
1261            } else if (on_lfe(a)) {
1262                ic_unconnected_lfe++;
1263            }
1264        }
1265
1266        if (output_layout_index != PA_CHANNEL_LAYOUT_OTHER) {
1267            for (ic = 0; ic < n_ic; ic++) {
1268                if (ic_connected[ic]) { continue; }
1269                pa_channel_position_t a = r->i_cm.map[ic];
1270                int a_downmix = pa_to_downmix_position(a);
1271                for (oc = 0; oc < n_oc; oc++) {
1272                    pa_channel_position_t b = r->o_cm.map[oc];
1273                    int b_downmix = pa_to_downmix_position(b);
1274                    m->map_table_f[oc][ic] =
1275                    (float)channelDownmixMatrix[output_layout_index][a_downmix][b_downmix]/(float)RESCALE_COEF;
1276                    /* force lfe downmix*/
1277                    if (on_lfe(a))
1278                        m->map_table_f[oc][ic] = .375f/(float)ic_unconnected_lfe;
1279                }
1280            }
1281        } else { /* channels that are not supported by downmix table */
1282            for (ic = 0; ic < n_ic; ic++) {
1283                pa_channel_position_t a = r->i_cm.map[ic];
1284                pa_channel_direction_t ic_direction = front_rear_side(r->i_cm.map[ic]);
1285                if (ic_connected[ic]) {
1286                    continue;
1287                }
1288                for (oc = 0; oc < n_oc; oc++) {
1289                    pa_channel_position_t b = r->o_cm.map[oc];
1290                    pa_channel_direction_t oc_direction = front_rear_side(r->o_cm.map[oc]);
1291                    if (on_left(a) && on_left(b)) {
1292                        m->map_table_f[oc][ic] = directionDownMixMatrix[ic_direction][oc_direction];
1293                    } else if (on_right(a) && on_right(b)) {
1294                        m->map_table_f[oc][ic] = directionDownMixMatrix[ic_direction][oc_direction];
1295                    } else if (on_center(a) && on_center(b)) {
1296                        m->map_table_f[oc][ic] = directionDownMixMatrix[ic_direction][oc_direction];
1297                        ic_unconnected_center_mixed_in = true;
1298                    } else if (on_lfe(a)) {
1299                        /* force lfe downmix */
1300                        m->map_table_f[oc][ic] = .375f / (float) ic_unconnected_lfe;
1301                    }
1302                }
1303            }
1304            if (ic_unconnected_center > 0 && !ic_unconnected_center_mixed_in) {
1305                unsigned ncenter[PA_CHANNELS_MAX];
1306                bool found_frs[PA_CHANNELS_MAX];
1307
1308                memset(ncenter, 0, sizeof(ncenter));
1309                memset(found_frs, 0, sizeof(found_frs));
1310
1311                /* Hmm, as it appears there was no center channel we
1312                could mix our center channel in. In this case, mix it into
1313                left and right. Using .5 as the factor. */
1314
1315                for (ic = 0; ic < n_ic; ic++) {
1316
1317                    if (ic_connected[ic])
1318                        continue;
1319
1320                    if (!on_center(r->i_cm.map[ic]))
1321                        continue;
1322
1323                    for (oc = 0; oc < n_oc; oc++) {
1324
1325                        if (!on_left(r->o_cm.map[oc]) && !on_right(r->o_cm.map[oc]))
1326                            continue;
1327
1328                        if (front_rear_side(r->i_cm.map[ic]) == front_rear_side(r->o_cm.map[oc])) {
1329                            found_frs[ic] = true;
1330                            break;
1331                        }
1332                    }
1333
1334                    for (oc = 0; oc < n_oc; oc++) {
1335
1336                        if (!on_left(r->o_cm.map[oc]) && !on_right(r->o_cm.map[oc]))
1337                            continue;
1338
1339                        if (!found_frs[ic] || front_rear_side(r->i_cm.map[ic]) == front_rear_side(r->o_cm.map[oc]))
1340                            ncenter[oc]++;
1341                    }
1342                }
1343
1344                for (oc = 0; oc < n_oc; oc++) {
1345
1346                    if (!on_left(r->o_cm.map[oc]) && !on_right(r->o_cm.map[oc]))
1347                        continue;
1348
1349                    if (ncenter[oc] <= 0)
1350                        continue;
1351
1352                    for (ic = 0; ic < n_ic; ic++) {
1353
1354                        if (!on_center(r->i_cm.map[ic]))
1355                            continue;
1356
1357                        if (!found_frs[ic] || front_rear_side(r->i_cm.map[ic]) == front_rear_side(r->o_cm.map[oc]))
1358                            m->map_table_f[oc][ic] = .5f / (float) ncenter[oc];
1359                    }
1360                }
1361            }
1362        }
1363    }
1364
1365    // uniform with maximum sum
1366    float max_sum = 0.0f;
1367
1368    for (oc = 0; oc < n_oc; oc++) {
1369        float sum = 0.0f;
1370        for (ic = 0; ic < n_ic; ic++) {
1371            sum += m->map_table_f[oc][ic];
1372        }
1373        if (sum > max_sum) { max_sum = sum; }
1374    }
1375    for (oc = 0; oc < n_oc; oc++) {
1376        for (ic = 0; ic < n_ic; ic++) {
1377            m->map_table_f[oc][ic] /= max_sum;
1378        }
1379    }
1380    /* make an 16:16 int version of the matrix */
1381    for (oc = 0; oc < n_oc; oc++)
1382        for (ic = 0; ic < n_ic; ic++)
1383            m->map_table_i[oc][ic] = (int32_t) (m->map_table_f[oc][ic] * 0x10000);
1384
1385    s = pa_strbuf_new();
1386
1387    pa_strbuf_printf(s, "     ");
1388    for (ic = 0; ic < n_ic; ic++)
1389        pa_strbuf_printf(s, "  I%02u ", ic);
1390    pa_strbuf_puts(s, "\n    +");
1391
1392    for (ic = 0; ic < n_ic; ic++)
1393        pa_strbuf_printf(s, "------");
1394    pa_strbuf_puts(s, "\n");
1395
1396    for (oc = 0; oc < n_oc; oc++) {
1397        pa_strbuf_printf(s, "O%02u |", oc);
1398
1399        for (ic = 0; ic < n_ic; ic++)
1400            pa_strbuf_printf(s, " %1.3f", m->map_table_f[oc][ic]);
1401
1402        pa_strbuf_puts(s, "\n");
1403    }
1404
1405    pa_log_debug("Channel matrix:\n%s", t = pa_strbuf_to_string_free(s));
1406    pa_xfree(t);
1407
1408    /* initialize the remapping function */
1409    pa_init_remap_func(m);
1410}
1411
1412static void free_remap(pa_remap_t *m) {
1413    pa_assert(m);
1414
1415    pa_xfree(m->state);
1416}
1417
1418/* check if buf's memblock is large enough to hold 'len' bytes; create a
1419 * new memblock if necessary and optionally preserve 'copy' data bytes */
1420static void fit_buf(pa_resampler *r, pa_memchunk *buf, size_t len, size_t *size, size_t copy) {
1421    pa_assert(size);
1422
1423    if (!buf->memblock || len > *size) {
1424        pa_memblock *new_block = pa_memblock_new(r->mempool, len);
1425
1426        if (buf->memblock) {
1427            if (copy > 0) {
1428                void *src = pa_memblock_acquire(buf->memblock);
1429                void *dst = pa_memblock_acquire(new_block);
1430                pa_assert(copy <= len);
1431                memcpy(dst, src, copy);
1432                pa_memblock_release(new_block);
1433                pa_memblock_release(buf->memblock);
1434            }
1435
1436            pa_memblock_unref(buf->memblock);
1437        }
1438
1439        buf->memblock = new_block;
1440        *size = len;
1441    }
1442
1443    buf->length = len;
1444}
1445
1446static pa_memchunk* convert_to_work_format(pa_resampler *r, pa_memchunk *input) {
1447    unsigned in_n_samples, out_n_samples;
1448    void *src, *dst;
1449    bool have_leftover;
1450    size_t leftover_length = 0;
1451
1452    pa_assert(r);
1453    pa_assert(input);
1454    pa_assert(input->memblock);
1455
1456    /* Convert the incoming sample into the work sample format and place them
1457     * in to_work_format_buf. The leftover data is already converted, so it's
1458     * part of the output buffer. */
1459
1460    have_leftover = r->leftover_in_to_work;
1461    r->leftover_in_to_work = false;
1462
1463    if (!have_leftover && (!r->to_work_format_func || !input->length))
1464        return input;
1465    else if (input->length <= 0)
1466        return &r->to_work_format_buf;
1467
1468    in_n_samples = out_n_samples = (unsigned) ((input->length / r->i_fz) * r->i_ss.channels);
1469
1470    if (have_leftover) {
1471        leftover_length = r->to_work_format_buf.length;
1472        out_n_samples += (unsigned) (leftover_length / r->w_sz);
1473    }
1474
1475    fit_buf(r, &r->to_work_format_buf, r->w_sz * out_n_samples, &r->to_work_format_buf_size, leftover_length);
1476
1477    src = pa_memblock_acquire_chunk(input);
1478    dst = (uint8_t *) pa_memblock_acquire(r->to_work_format_buf.memblock) + leftover_length;
1479
1480    if (r->to_work_format_func)
1481        r->to_work_format_func(in_n_samples, src, dst);
1482    else
1483        memcpy(dst, src, input->length);
1484
1485    pa_memblock_release(input->memblock);
1486    pa_memblock_release(r->to_work_format_buf.memblock);
1487
1488    return &r->to_work_format_buf;
1489}
1490
1491static pa_memchunk *remap_channels(pa_resampler *r, pa_memchunk *input) {
1492    unsigned in_n_samples, out_n_samples, in_n_frames, out_n_frames;
1493    void *src, *dst;
1494    size_t leftover_length = 0;
1495    bool have_leftover;
1496
1497    pa_assert(r);
1498    pa_assert(input);
1499    pa_assert(input->memblock);
1500
1501    /* Remap channels and place the result in remap_buf. There may be leftover
1502     * data in the beginning of remap_buf. The leftover data is already
1503     * remapped, so it's not part of the input, it's part of the output. */
1504
1505    have_leftover = r->leftover_in_remap;
1506    r->leftover_in_remap = false;
1507
1508    if (!have_leftover && (!r->map_required || input->length <= 0))
1509        return input;
1510    else if (input->length <= 0)
1511        return &r->remap_buf;
1512
1513    in_n_samples = (unsigned) (input->length / r->w_sz);
1514    in_n_frames = out_n_frames = in_n_samples / r->i_ss.channels;
1515
1516    if (have_leftover) {
1517        leftover_length = r->remap_buf.length;
1518        out_n_frames += leftover_length / r->w_fz;
1519    }
1520
1521    out_n_samples = out_n_frames * r->o_ss.channels;
1522    fit_buf(r, &r->remap_buf, out_n_samples * r->w_sz, &r->remap_buf_size, leftover_length);
1523
1524    src = pa_memblock_acquire_chunk(input);
1525    dst = (uint8_t *) pa_memblock_acquire(r->remap_buf.memblock) + leftover_length;
1526
1527    if (r->map_required) {
1528        pa_remap_t *remap = &r->remap;
1529
1530        pa_assert(remap->do_remap);
1531        remap->do_remap(remap, dst, src, in_n_frames);
1532
1533    } else
1534        memcpy(dst, src, input->length);
1535
1536    pa_memblock_release(input->memblock);
1537    pa_memblock_release(r->remap_buf.memblock);
1538
1539    return &r->remap_buf;
1540}
1541
1542static void save_leftover(pa_resampler *r, void *buf, size_t len) {
1543    void *dst;
1544
1545    pa_assert(r);
1546    pa_assert(buf);
1547    pa_assert(len > 0);
1548
1549    /* Store the leftover data. */
1550    fit_buf(r, r->leftover_buf, len, r->leftover_buf_size, 0);
1551    *r->have_leftover = true;
1552
1553    dst = pa_memblock_acquire(r->leftover_buf->memblock);
1554    memmove(dst, buf, len);
1555    pa_memblock_release(r->leftover_buf->memblock);
1556}
1557
1558static pa_memchunk *resample(pa_resampler *r, pa_memchunk *input) {
1559    unsigned in_n_frames, out_n_frames, leftover_n_frames;
1560
1561    pa_assert(r);
1562    pa_assert(input);
1563
1564    /* Resample the data and place the result in resample_buf. */
1565
1566    if (!r->impl.resample || !input->length)
1567        return input;
1568
1569    in_n_frames = (unsigned) (input->length / r->w_fz);
1570
1571    out_n_frames = ((in_n_frames*r->o_ss.rate)/r->i_ss.rate)+EXTRA_FRAMES;
1572    fit_buf(r, &r->resample_buf, r->w_fz * out_n_frames, &r->resample_buf_size, 0);
1573
1574    leftover_n_frames = r->impl.resample(r, input, in_n_frames, &r->resample_buf, &out_n_frames);
1575
1576    if (leftover_n_frames > 0) {
1577        void *leftover_data = (uint8_t *) pa_memblock_acquire_chunk(input) + (in_n_frames - leftover_n_frames) * r->w_fz;
1578        save_leftover(r, leftover_data, leftover_n_frames * r->w_fz);
1579        pa_memblock_release(input->memblock);
1580    }
1581
1582    r->resample_buf.length = out_n_frames * r->w_fz;
1583
1584    return &r->resample_buf;
1585}
1586
1587static pa_memchunk *convert_from_work_format(pa_resampler *r, pa_memchunk *input) {
1588    unsigned n_samples, n_frames;
1589    void *src, *dst;
1590
1591    pa_assert(r);
1592    pa_assert(input);
1593
1594    /* Convert the data into the correct sample type and place the result in
1595     * from_work_format_buf. */
1596
1597    if (!r->from_work_format_func || !input->length)
1598        return input;
1599
1600    n_samples = (unsigned) (input->length / r->w_sz);
1601    n_frames = n_samples / r->o_ss.channels;
1602    fit_buf(r, &r->from_work_format_buf, r->o_fz * n_frames, &r->from_work_format_buf_size, 0);
1603
1604    src = pa_memblock_acquire_chunk(input);
1605    dst = pa_memblock_acquire(r->from_work_format_buf.memblock);
1606    r->from_work_format_func(n_samples, src, dst);
1607    pa_memblock_release(input->memblock);
1608    pa_memblock_release(r->from_work_format_buf.memblock);
1609
1610    return &r->from_work_format_buf;
1611}
1612
1613void pa_resampler_run(pa_resampler *r, const pa_memchunk *in, pa_memchunk *out) {
1614    pa_memchunk *buf;
1615
1616    pa_assert(r);
1617    pa_assert(in);
1618    pa_assert(out);
1619    pa_assert(in->length);
1620    pa_assert(in->memblock);
1621    pa_assert(in->length % r->i_fz == 0);
1622
1623    /* If first frame and resampler doesn't init, push one frame to init.
1624     * Otherwise, resamplers demand 2 or 3 times to output*/
1625    if ((r->in_frames == 0) && (r->i_ss.rate != r->o_ss.rate)) {
1626        pa_memchunk dumpBuf;
1627        buf = &dumpBuf;
1628        size_t tempLength = r->i_fz * RESAMPLER_CACHE_SIZE_RATIO * r->o_fz;
1629        buf->length = tempLength;
1630        buf->memblock = pa_memblock_new(r->mempool, tempLength);
1631        buf->index = 0;
1632
1633        // silence memblock
1634        void *data = pa_memblock_acquire(buf->memblock);
1635        memset(data, 0, buf->length);
1636        pa_memblock_release(buf->memblock);
1637
1638        resample(r, buf);
1639        pa_memblock_release(buf->memblock);
1640    }
1641
1642    buf = (pa_memchunk*) in;
1643    r->in_frames += buf->length / r->i_fz;
1644    buf = convert_to_work_format(r, buf);
1645
1646    /* Try to save resampling effort: if we have more output channels than
1647     * input channels, do resampling first, then remapping. */
1648    if (r->o_ss.channels <= r->i_ss.channels) {
1649        buf = remap_channels(r, buf);
1650        buf = resample(r, buf);
1651    } else {
1652        buf = resample(r, buf);
1653        buf = remap_channels(r, buf);
1654    }
1655
1656    if (r->lfe_filter)
1657        buf = pa_lfe_filter_process(r->lfe_filter, buf);
1658
1659    if (buf->length) {
1660        buf = convert_from_work_format(r, buf);
1661        *out = *buf;
1662        r->out_frames += buf->length / r->o_fz;
1663
1664        if (buf == in)
1665            pa_memblock_ref(buf->memblock);
1666        else
1667            pa_memchunk_reset(buf);
1668    } else
1669        pa_memchunk_reset(out);
1670}
1671
1672/* Get delay in input frames. Some resamplers may have negative delay. */
1673double pa_resampler_get_delay(pa_resampler *r, bool allow_negative) {
1674    double frames;
1675
1676    frames = r->out_frames * r->i_ss.rate / r->o_ss.rate;
1677    if (frames >= r->in_frames && !allow_negative)
1678        return 0;
1679    return r->in_frames - frames;
1680}
1681
1682/* Get delay in usec */
1683pa_usec_t pa_resampler_get_delay_usec(pa_resampler *r) {
1684
1685    if (!r)
1686        return 0;
1687
1688    return (pa_usec_t) (pa_resampler_get_delay(r, false) * PA_USEC_PER_SEC / r->i_ss.rate);
1689}
1690
1691/* Get GCD of input and output rate. */
1692unsigned pa_resampler_get_gcd(pa_resampler *r) {
1693    pa_assert(r);
1694
1695    return r->gcd;
1696}
1697
1698/* Get maximum resampler history. The resamplers have finite impulse response, so really old
1699 * data (more than 2x the resampler latency) cannot affect the output. This means, that in an
1700 * ideal case, we should re-run 2 - 3 times the resampler delay through the resampler when it
1701 * is rewound. On the other hand this would mean for high sample rates that more than 25000
1702 * samples would need to be used (384k * 33ms). Therefore limit the history to 1.5 times the
1703 * maximum resampler delay, which should be fully sufficient in most cases and allows to run
1704 * at least more than one delay through the resampler in case of high rates. */
1705size_t pa_resampler_get_max_history(pa_resampler *r) {
1706
1707    if (!r)
1708        return 0;
1709
1710    return (uint64_t) PA_RESAMPLER_MAX_DELAY_USEC * r->i_ss.rate * 3 / PA_USEC_PER_SEC / 2;
1711}
1712
1713/*** copy (noop) implementation ***/
1714
1715static int copy_init(pa_resampler *r) {
1716    pa_assert(r);
1717
1718    pa_assert(r->o_ss.rate == r->i_ss.rate);
1719
1720    return 0;
1721}
1722