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