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