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