]> code.delx.au - pulseaudio/blob - src/pulsecore/resampler.c
resampler: Add assert to set_input/output_rate
[pulseaudio] / src / pulsecore / resampler.c
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, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <string.h>
27
28 #ifdef HAVE_LIBSAMPLERATE
29 #include <samplerate.h>
30 #endif
31
32 #ifdef HAVE_SPEEX
33 #include <speex/speex_resampler.h>
34 #endif
35
36 #include <pulse/xmalloc.h>
37 #include <pulsecore/sconv.h>
38 #include <pulsecore/log.h>
39 #include <pulsecore/macro.h>
40 #include <pulsecore/strbuf.h>
41 #include <pulsecore/remap.h>
42 #include <pulsecore/core-util.h>
43 #include "ffmpeg/avcodec.h"
44
45 #include "resampler.h"
46
47 /* Number of samples of extra space we allow the resamplers to return */
48 #define EXTRA_FRAMES 128
49
50 struct pa_resampler {
51 pa_resample_method_t method;
52 pa_resample_flags_t flags;
53
54 pa_sample_spec i_ss, o_ss;
55 pa_channel_map i_cm, o_cm;
56 size_t i_fz, o_fz, w_sz;
57 pa_mempool *mempool;
58
59 pa_memchunk to_work_format_buf;
60 pa_memchunk remap_buf;
61 pa_memchunk resample_buf;
62 pa_memchunk from_work_format_buf;
63 unsigned to_work_format_buf_samples;
64 size_t remap_buf_size;
65 unsigned resample_buf_samples;
66 unsigned from_work_format_buf_samples;
67 bool remap_buf_contains_leftover_data;
68
69 pa_sample_format_t work_format;
70
71 pa_convert_func_t to_work_format_func;
72 pa_convert_func_t from_work_format_func;
73
74 pa_remap_t remap;
75 bool map_required;
76
77 pa_resampler_impl impl;
78 };
79
80 struct trivial_data { /* data specific to the trivial resampler */
81 unsigned o_counter;
82 unsigned i_counter;
83 };
84
85 struct peaks_data { /* data specific to the peak finder pseudo resampler */
86 unsigned o_counter;
87 unsigned i_counter;
88
89 float max_f[PA_CHANNELS_MAX];
90 int16_t max_i[PA_CHANNELS_MAX];
91 };
92
93 #ifdef HAVE_LIBSAMPLERATE
94 struct src_data { /* data specific to libsamplerate */
95 SRC_STATE *state;
96 };
97 #endif
98
99 #ifdef HAVE_SPEEX
100 struct speex_data { /* data specific to speex */
101 SpeexResamplerState* state;
102 };
103 #endif
104
105 struct ffmpeg_data { /* data specific to ffmpeg */
106 struct AVResampleContext *state;
107 pa_memchunk buf[PA_CHANNELS_MAX];
108 };
109
110 static int copy_init(pa_resampler *r);
111 static int trivial_init(pa_resampler*r);
112 #ifdef HAVE_SPEEX
113 static int speex_init(pa_resampler*r);
114 #endif
115 static int ffmpeg_init(pa_resampler*r);
116 static int peaks_init(pa_resampler*r);
117 #ifdef HAVE_LIBSAMPLERATE
118 static int libsamplerate_init(pa_resampler*r);
119 #endif
120
121 static void calc_map_table(pa_resampler *r);
122
123 static int (* const init_table[])(pa_resampler*r) = {
124 #ifdef HAVE_LIBSAMPLERATE
125 [PA_RESAMPLER_SRC_SINC_BEST_QUALITY] = libsamplerate_init,
126 [PA_RESAMPLER_SRC_SINC_MEDIUM_QUALITY] = libsamplerate_init,
127 [PA_RESAMPLER_SRC_SINC_FASTEST] = libsamplerate_init,
128 [PA_RESAMPLER_SRC_ZERO_ORDER_HOLD] = libsamplerate_init,
129 [PA_RESAMPLER_SRC_LINEAR] = libsamplerate_init,
130 #else
131 [PA_RESAMPLER_SRC_SINC_BEST_QUALITY] = NULL,
132 [PA_RESAMPLER_SRC_SINC_MEDIUM_QUALITY] = NULL,
133 [PA_RESAMPLER_SRC_SINC_FASTEST] = NULL,
134 [PA_RESAMPLER_SRC_ZERO_ORDER_HOLD] = NULL,
135 [PA_RESAMPLER_SRC_LINEAR] = NULL,
136 #endif
137 [PA_RESAMPLER_TRIVIAL] = trivial_init,
138 #ifdef HAVE_SPEEX
139 [PA_RESAMPLER_SPEEX_FLOAT_BASE+0] = speex_init,
140 [PA_RESAMPLER_SPEEX_FLOAT_BASE+1] = speex_init,
141 [PA_RESAMPLER_SPEEX_FLOAT_BASE+2] = speex_init,
142 [PA_RESAMPLER_SPEEX_FLOAT_BASE+3] = speex_init,
143 [PA_RESAMPLER_SPEEX_FLOAT_BASE+4] = speex_init,
144 [PA_RESAMPLER_SPEEX_FLOAT_BASE+5] = speex_init,
145 [PA_RESAMPLER_SPEEX_FLOAT_BASE+6] = speex_init,
146 [PA_RESAMPLER_SPEEX_FLOAT_BASE+7] = speex_init,
147 [PA_RESAMPLER_SPEEX_FLOAT_BASE+8] = speex_init,
148 [PA_RESAMPLER_SPEEX_FLOAT_BASE+9] = speex_init,
149 [PA_RESAMPLER_SPEEX_FLOAT_BASE+10] = speex_init,
150 [PA_RESAMPLER_SPEEX_FIXED_BASE+0] = speex_init,
151 [PA_RESAMPLER_SPEEX_FIXED_BASE+1] = speex_init,
152 [PA_RESAMPLER_SPEEX_FIXED_BASE+2] = speex_init,
153 [PA_RESAMPLER_SPEEX_FIXED_BASE+3] = speex_init,
154 [PA_RESAMPLER_SPEEX_FIXED_BASE+4] = speex_init,
155 [PA_RESAMPLER_SPEEX_FIXED_BASE+5] = speex_init,
156 [PA_RESAMPLER_SPEEX_FIXED_BASE+6] = speex_init,
157 [PA_RESAMPLER_SPEEX_FIXED_BASE+7] = speex_init,
158 [PA_RESAMPLER_SPEEX_FIXED_BASE+8] = speex_init,
159 [PA_RESAMPLER_SPEEX_FIXED_BASE+9] = speex_init,
160 [PA_RESAMPLER_SPEEX_FIXED_BASE+10] = speex_init,
161 #else
162 [PA_RESAMPLER_SPEEX_FLOAT_BASE+0] = NULL,
163 [PA_RESAMPLER_SPEEX_FLOAT_BASE+1] = NULL,
164 [PA_RESAMPLER_SPEEX_FLOAT_BASE+2] = NULL,
165 [PA_RESAMPLER_SPEEX_FLOAT_BASE+3] = NULL,
166 [PA_RESAMPLER_SPEEX_FLOAT_BASE+4] = NULL,
167 [PA_RESAMPLER_SPEEX_FLOAT_BASE+5] = NULL,
168 [PA_RESAMPLER_SPEEX_FLOAT_BASE+6] = NULL,
169 [PA_RESAMPLER_SPEEX_FLOAT_BASE+7] = NULL,
170 [PA_RESAMPLER_SPEEX_FLOAT_BASE+8] = NULL,
171 [PA_RESAMPLER_SPEEX_FLOAT_BASE+9] = NULL,
172 [PA_RESAMPLER_SPEEX_FLOAT_BASE+10] = NULL,
173 [PA_RESAMPLER_SPEEX_FIXED_BASE+0] = NULL,
174 [PA_RESAMPLER_SPEEX_FIXED_BASE+1] = NULL,
175 [PA_RESAMPLER_SPEEX_FIXED_BASE+2] = NULL,
176 [PA_RESAMPLER_SPEEX_FIXED_BASE+3] = NULL,
177 [PA_RESAMPLER_SPEEX_FIXED_BASE+4] = NULL,
178 [PA_RESAMPLER_SPEEX_FIXED_BASE+5] = NULL,
179 [PA_RESAMPLER_SPEEX_FIXED_BASE+6] = NULL,
180 [PA_RESAMPLER_SPEEX_FIXED_BASE+7] = NULL,
181 [PA_RESAMPLER_SPEEX_FIXED_BASE+8] = NULL,
182 [PA_RESAMPLER_SPEEX_FIXED_BASE+9] = NULL,
183 [PA_RESAMPLER_SPEEX_FIXED_BASE+10] = NULL,
184 #endif
185 [PA_RESAMPLER_FFMPEG] = ffmpeg_init,
186 [PA_RESAMPLER_AUTO] = NULL,
187 [PA_RESAMPLER_COPY] = copy_init,
188 [PA_RESAMPLER_PEAKS] = peaks_init,
189 };
190
191 static pa_resample_method_t pa_resampler_fix_method(
192 pa_resample_flags_t flags,
193 pa_resample_method_t method,
194 const uint32_t rate_a,
195 const uint32_t rate_b) {
196
197 pa_assert(rate_a > 0 && rate_a <= PA_RATE_MAX);
198 pa_assert(rate_b > 0 && rate_b <= PA_RATE_MAX);
199 pa_assert(method >= 0);
200 pa_assert(method < PA_RESAMPLER_MAX);
201
202 if (!(flags & PA_RESAMPLER_VARIABLE_RATE) && rate_a == rate_b) {
203 pa_log_info("Forcing resampler 'copy', because of fixed, identical sample rates.");
204 method = PA_RESAMPLER_COPY;
205 }
206
207 if (!pa_resample_method_supported(method)) {
208 pa_log_warn("Support for resampler '%s' not compiled in, reverting to 'auto'.", pa_resample_method_to_string(method));
209 method = PA_RESAMPLER_AUTO;
210 }
211
212 switch (method) {
213 case PA_RESAMPLER_COPY:
214 if (rate_a != rate_b) {
215 pa_log_info("Resampler 'copy' cannot change sampling rate, reverting to resampler 'auto'.");
216 break;
217 }
218 /* Else fall through */
219 case PA_RESAMPLER_FFMPEG:
220 if (flags & PA_RESAMPLER_VARIABLE_RATE) {
221 pa_log_info("Resampler '%s' cannot do variable rate, reverting to resampler 'auto'.", pa_resample_method_to_string(method));
222 method = PA_RESAMPLER_AUTO;
223 }
224 break;
225
226 /* The Peaks resampler only supports downsampling.
227 * Revert to auto if we are upsampling */
228 case PA_RESAMPLER_PEAKS:
229 if (rate_a < rate_b) {
230 pa_log_warn("The 'peaks' resampler only supports downsampling, reverting to resampler 'auto'.");
231 method = PA_RESAMPLER_AUTO;
232 }
233 break;
234
235 default:
236 break;
237 }
238
239 if (method == PA_RESAMPLER_AUTO) {
240 #ifdef HAVE_SPEEX
241 method = PA_RESAMPLER_SPEEX_FLOAT_BASE + 1;
242 #else
243 if (flags & PA_RESAMPLER_VARIABLE_RATE)
244 method = PA_RESAMPLER_TRIVIAL;
245 else
246 method = PA_RESAMPLER_FFMPEG;
247 #endif
248 }
249
250 return method;
251 }
252
253 /* Return true if a is a more precise sample format than b, else return false */
254 static bool sample_format_more_precise(pa_sample_format_t a, pa_sample_format_t b) {
255 pa_assert(a >= 0 && a < PA_SAMPLE_MAX);
256 pa_assert(b >= 0 && b < PA_SAMPLE_MAX);
257
258 switch (a) {
259 case PA_SAMPLE_U8:
260 case PA_SAMPLE_ALAW:
261 case PA_SAMPLE_ULAW:
262 return false;
263 break;
264
265 case PA_SAMPLE_S16LE:
266 case PA_SAMPLE_S16BE:
267 if (b == PA_SAMPLE_ULAW || b == PA_SAMPLE_ALAW || b == PA_SAMPLE_U8)
268 return true;
269 else
270 return false;
271 break;
272
273 case PA_SAMPLE_S24LE:
274 case PA_SAMPLE_S24BE:
275 case PA_SAMPLE_S24_32LE:
276 case PA_SAMPLE_S24_32BE:
277 if (b == PA_SAMPLE_ULAW || b == PA_SAMPLE_ALAW || b == PA_SAMPLE_U8 ||
278 b == PA_SAMPLE_S16LE || b == PA_SAMPLE_S16BE)
279 return true;
280 else
281 return false;
282 break;
283
284 case PA_SAMPLE_FLOAT32LE:
285 case PA_SAMPLE_FLOAT32BE:
286 case PA_SAMPLE_S32LE:
287 case PA_SAMPLE_S32BE:
288 if (b == PA_SAMPLE_FLOAT32LE || b == PA_SAMPLE_FLOAT32BE ||
289 b == PA_SAMPLE_S32LE || b == PA_SAMPLE_FLOAT32BE)
290 return false;
291 else
292 return true;
293 break;
294
295 default:
296 return false;
297 }
298 }
299
300 static pa_sample_format_t pa_resampler_choose_work_format(
301 pa_resample_method_t method,
302 pa_sample_format_t a,
303 pa_sample_format_t b,
304 bool map_required) {
305 pa_sample_format_t work_format;
306
307 pa_assert(a >= 0 && a < PA_SAMPLE_MAX);
308 pa_assert(b >= 0 && b < PA_SAMPLE_MAX);
309 pa_assert(method >= 0);
310 pa_assert(method < PA_RESAMPLER_MAX);
311
312 if (method >= PA_RESAMPLER_SPEEX_FIXED_BASE && method <= PA_RESAMPLER_SPEEX_FIXED_MAX)
313 method = PA_RESAMPLER_SPEEX_FIXED_BASE;
314
315 switch (method) {
316 /* This block is for resampling functions that only
317 * support the S16 sample format. */
318 case PA_RESAMPLER_SPEEX_FIXED_BASE: /* fall through */
319 case PA_RESAMPLER_FFMPEG:
320 work_format = PA_SAMPLE_S16NE;
321 break;
322
323 /* This block is for resampling functions that support
324 * any sample format. */
325 case PA_RESAMPLER_COPY: /* fall through */
326 case PA_RESAMPLER_TRIVIAL:
327 if (!map_required && a == b) {
328 work_format = a;
329 break;
330 }
331 /* Else fall trough */
332 case PA_RESAMPLER_PEAKS:
333 if (a == PA_SAMPLE_S16NE || b == PA_SAMPLE_S16NE)
334 work_format = PA_SAMPLE_S16NE;
335 else if (sample_format_more_precise(a, PA_SAMPLE_S16NE) ||
336 sample_format_more_precise(b, PA_SAMPLE_S16NE))
337 work_format = PA_SAMPLE_FLOAT32NE;
338 else
339 work_format = PA_SAMPLE_S16NE;
340 break;
341
342 default:
343 work_format = PA_SAMPLE_FLOAT32NE;
344 }
345
346 return work_format;
347 }
348
349 pa_resampler* pa_resampler_new(
350 pa_mempool *pool,
351 const pa_sample_spec *a,
352 const pa_channel_map *am,
353 const pa_sample_spec *b,
354 const pa_channel_map *bm,
355 pa_resample_method_t method,
356 pa_resample_flags_t flags) {
357
358 pa_resampler *r = NULL;
359
360 pa_assert(pool);
361 pa_assert(a);
362 pa_assert(b);
363 pa_assert(pa_sample_spec_valid(a));
364 pa_assert(pa_sample_spec_valid(b));
365 pa_assert(method >= 0);
366 pa_assert(method < PA_RESAMPLER_MAX);
367
368 method = pa_resampler_fix_method(flags, method, a->rate, b->rate);
369
370 r = pa_xnew0(pa_resampler, 1);
371 r->mempool = pool;
372 r->method = method;
373 r->flags = flags;
374
375 /* Fill sample specs */
376 r->i_ss = *a;
377 r->o_ss = *b;
378
379 /* set up the remap structure */
380 r->remap.i_ss = &r->i_ss;
381 r->remap.o_ss = &r->o_ss;
382 r->remap.format = &r->work_format;
383
384 if (am)
385 r->i_cm = *am;
386 else if (!pa_channel_map_init_auto(&r->i_cm, r->i_ss.channels, PA_CHANNEL_MAP_DEFAULT))
387 goto fail;
388
389 if (bm)
390 r->o_cm = *bm;
391 else if (!pa_channel_map_init_auto(&r->o_cm, r->o_ss.channels, PA_CHANNEL_MAP_DEFAULT))
392 goto fail;
393
394 r->i_fz = pa_frame_size(a);
395 r->o_fz = pa_frame_size(b);
396
397 calc_map_table(r);
398
399 pa_log_info("Using resampler '%s'", pa_resample_method_to_string(method));
400
401 r->work_format = pa_resampler_choose_work_format(method, a->format, b->format, r->map_required);
402
403 pa_log_info("Using %s as working format.", pa_sample_format_to_string(r->work_format));
404
405 r->w_sz = pa_sample_size_of_format(r->work_format);
406
407 if (r->i_ss.format != r->work_format) {
408 if (r->work_format == PA_SAMPLE_FLOAT32NE) {
409 if (!(r->to_work_format_func = pa_get_convert_to_float32ne_function(r->i_ss.format)))
410 goto fail;
411 } else {
412 pa_assert(r->work_format == PA_SAMPLE_S16NE);
413 if (!(r->to_work_format_func = pa_get_convert_to_s16ne_function(r->i_ss.format)))
414 goto fail;
415 }
416 }
417
418 if (r->o_ss.format != r->work_format) {
419 if (r->work_format == PA_SAMPLE_FLOAT32NE) {
420 if (!(r->from_work_format_func = pa_get_convert_from_float32ne_function(r->o_ss.format)))
421 goto fail;
422 } else {
423 pa_assert(r->work_format == PA_SAMPLE_S16NE);
424 if (!(r->from_work_format_func = pa_get_convert_from_s16ne_function(r->o_ss.format)))
425 goto fail;
426 }
427 }
428
429 /* initialize implementation */
430 if (init_table[method](r) < 0)
431 goto fail;
432
433 return r;
434
435 fail:
436 pa_xfree(r);
437
438 return NULL;
439 }
440
441 void pa_resampler_free(pa_resampler *r) {
442 pa_assert(r);
443
444 if (r->impl.free)
445 r->impl.free(r);
446
447 if (r->to_work_format_buf.memblock)
448 pa_memblock_unref(r->to_work_format_buf.memblock);
449 if (r->remap_buf.memblock)
450 pa_memblock_unref(r->remap_buf.memblock);
451 if (r->resample_buf.memblock)
452 pa_memblock_unref(r->resample_buf.memblock);
453 if (r->from_work_format_buf.memblock)
454 pa_memblock_unref(r->from_work_format_buf.memblock);
455
456 pa_xfree(r->impl.data);
457 pa_xfree(r);
458 }
459
460 void pa_resampler_set_input_rate(pa_resampler *r, uint32_t rate) {
461 pa_assert(r);
462 pa_assert(rate > 0);
463 pa_assert(r->impl.update_rates);
464
465 if (r->i_ss.rate == rate)
466 return;
467
468 r->i_ss.rate = rate;
469
470 r->impl.update_rates(r);
471 }
472
473 void pa_resampler_set_output_rate(pa_resampler *r, uint32_t rate) {
474 pa_assert(r);
475 pa_assert(rate > 0);
476 pa_assert(r->impl.update_rates);
477
478 if (r->o_ss.rate == rate)
479 return;
480
481 r->o_ss.rate = rate;
482
483 r->impl.update_rates(r);
484 }
485
486 size_t pa_resampler_request(pa_resampler *r, size_t out_length) {
487 pa_assert(r);
488
489 /* Let's round up here to make it more likely that the caller will get at
490 * least out_length amount of data from pa_resampler_run().
491 *
492 * We don't take the leftover into account here. If we did, then it might
493 * be in theory possible that this function would return 0 and
494 * pa_resampler_run() would also return 0. That could lead to infinite
495 * loops. When the leftover is ignored here, such loops would eventually
496 * terminate, because the leftover would grow each round, finally
497 * surpassing the minimum input threshold of the resampler. */
498 return ((((uint64_t) ((out_length + r->o_fz-1) / r->o_fz) * r->i_ss.rate) + r->o_ss.rate-1) / r->o_ss.rate) * r->i_fz;
499 }
500
501 size_t pa_resampler_result(pa_resampler *r, size_t in_length) {
502 size_t frames;
503
504 pa_assert(r);
505
506 /* Let's round up here to ensure that the caller will always allocate big
507 * enough output buffer. */
508
509 frames = (in_length + r->i_fz - 1) / r->i_fz;
510
511 if (r->remap_buf_contains_leftover_data)
512 frames += r->remap_buf.length / (r->w_sz * r->o_ss.channels);
513
514 return (((uint64_t) frames * r->o_ss.rate + r->i_ss.rate - 1) / r->i_ss.rate) * r->o_fz;
515 }
516
517 size_t pa_resampler_max_block_size(pa_resampler *r) {
518 size_t block_size_max;
519 pa_sample_spec max_ss;
520 size_t max_fs;
521 size_t frames;
522
523 pa_assert(r);
524
525 block_size_max = pa_mempool_block_size_max(r->mempool);
526
527 /* We deduce the "largest" sample spec we're using during the
528 * conversion */
529 max_ss.channels = (uint8_t) (PA_MAX(r->i_ss.channels, r->o_ss.channels));
530
531 /* We silently assume that the format enum is ordered by size */
532 max_ss.format = PA_MAX(r->i_ss.format, r->o_ss.format);
533 max_ss.format = PA_MAX(max_ss.format, r->work_format);
534
535 max_ss.rate = PA_MAX(r->i_ss.rate, r->o_ss.rate);
536
537 max_fs = pa_frame_size(&max_ss);
538 frames = block_size_max / max_fs - EXTRA_FRAMES;
539
540 if (r->remap_buf_contains_leftover_data)
541 frames -= r->remap_buf.length / (r->w_sz * r->o_ss.channels);
542
543 return ((uint64_t) frames * r->i_ss.rate / max_ss.rate) * r->i_fz;
544 }
545
546 void pa_resampler_reset(pa_resampler *r) {
547 pa_assert(r);
548
549 if (r->impl.reset)
550 r->impl.reset(r);
551
552 r->remap_buf_contains_leftover_data = false;
553 }
554
555 pa_resample_method_t pa_resampler_get_method(pa_resampler *r) {
556 pa_assert(r);
557
558 return r->method;
559 }
560
561 const pa_channel_map* pa_resampler_input_channel_map(pa_resampler *r) {
562 pa_assert(r);
563
564 return &r->i_cm;
565 }
566
567 const pa_sample_spec* pa_resampler_input_sample_spec(pa_resampler *r) {
568 pa_assert(r);
569
570 return &r->i_ss;
571 }
572
573 const pa_channel_map* pa_resampler_output_channel_map(pa_resampler *r) {
574 pa_assert(r);
575
576 return &r->o_cm;
577 }
578
579 const pa_sample_spec* pa_resampler_output_sample_spec(pa_resampler *r) {
580 pa_assert(r);
581
582 return &r->o_ss;
583 }
584
585 static const char * const resample_methods[] = {
586 "src-sinc-best-quality",
587 "src-sinc-medium-quality",
588 "src-sinc-fastest",
589 "src-zero-order-hold",
590 "src-linear",
591 "trivial",
592 "speex-float-0",
593 "speex-float-1",
594 "speex-float-2",
595 "speex-float-3",
596 "speex-float-4",
597 "speex-float-5",
598 "speex-float-6",
599 "speex-float-7",
600 "speex-float-8",
601 "speex-float-9",
602 "speex-float-10",
603 "speex-fixed-0",
604 "speex-fixed-1",
605 "speex-fixed-2",
606 "speex-fixed-3",
607 "speex-fixed-4",
608 "speex-fixed-5",
609 "speex-fixed-6",
610 "speex-fixed-7",
611 "speex-fixed-8",
612 "speex-fixed-9",
613 "speex-fixed-10",
614 "ffmpeg",
615 "auto",
616 "copy",
617 "peaks"
618 };
619
620 const char *pa_resample_method_to_string(pa_resample_method_t m) {
621
622 if (m < 0 || m >= PA_RESAMPLER_MAX)
623 return NULL;
624
625 return resample_methods[m];
626 }
627
628 int pa_resample_method_supported(pa_resample_method_t m) {
629
630 if (m < 0 || m >= PA_RESAMPLER_MAX)
631 return 0;
632
633 #ifndef HAVE_LIBSAMPLERATE
634 if (m <= PA_RESAMPLER_SRC_LINEAR)
635 return 0;
636 #endif
637
638 #ifndef HAVE_SPEEX
639 if (m >= PA_RESAMPLER_SPEEX_FLOAT_BASE && m <= PA_RESAMPLER_SPEEX_FLOAT_MAX)
640 return 0;
641 if (m >= PA_RESAMPLER_SPEEX_FIXED_BASE && m <= PA_RESAMPLER_SPEEX_FIXED_MAX)
642 return 0;
643 #endif
644
645 return 1;
646 }
647
648 pa_resample_method_t pa_parse_resample_method(const char *string) {
649 pa_resample_method_t m;
650
651 pa_assert(string);
652
653 for (m = 0; m < PA_RESAMPLER_MAX; m++)
654 if (pa_streq(string, resample_methods[m]))
655 return m;
656
657 if (pa_streq(string, "speex-fixed"))
658 return PA_RESAMPLER_SPEEX_FIXED_BASE + 1;
659
660 if (pa_streq(string, "speex-float"))
661 return PA_RESAMPLER_SPEEX_FLOAT_BASE + 1;
662
663 return PA_RESAMPLER_INVALID;
664 }
665
666 static bool on_left(pa_channel_position_t p) {
667
668 return
669 p == PA_CHANNEL_POSITION_FRONT_LEFT ||
670 p == PA_CHANNEL_POSITION_REAR_LEFT ||
671 p == PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER ||
672 p == PA_CHANNEL_POSITION_SIDE_LEFT ||
673 p == PA_CHANNEL_POSITION_TOP_FRONT_LEFT ||
674 p == PA_CHANNEL_POSITION_TOP_REAR_LEFT;
675 }
676
677 static bool on_right(pa_channel_position_t p) {
678
679 return
680 p == PA_CHANNEL_POSITION_FRONT_RIGHT ||
681 p == PA_CHANNEL_POSITION_REAR_RIGHT ||
682 p == PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER ||
683 p == PA_CHANNEL_POSITION_SIDE_RIGHT ||
684 p == PA_CHANNEL_POSITION_TOP_FRONT_RIGHT ||
685 p == PA_CHANNEL_POSITION_TOP_REAR_RIGHT;
686 }
687
688 static bool on_center(pa_channel_position_t p) {
689
690 return
691 p == PA_CHANNEL_POSITION_FRONT_CENTER ||
692 p == PA_CHANNEL_POSITION_REAR_CENTER ||
693 p == PA_CHANNEL_POSITION_TOP_CENTER ||
694 p == PA_CHANNEL_POSITION_TOP_FRONT_CENTER ||
695 p == PA_CHANNEL_POSITION_TOP_REAR_CENTER;
696 }
697
698 static bool on_lfe(pa_channel_position_t p) {
699 return
700 p == PA_CHANNEL_POSITION_LFE;
701 }
702
703 static bool on_front(pa_channel_position_t p) {
704 return
705 p == PA_CHANNEL_POSITION_FRONT_LEFT ||
706 p == PA_CHANNEL_POSITION_FRONT_RIGHT ||
707 p == PA_CHANNEL_POSITION_FRONT_CENTER ||
708 p == PA_CHANNEL_POSITION_TOP_FRONT_LEFT ||
709 p == PA_CHANNEL_POSITION_TOP_FRONT_RIGHT ||
710 p == PA_CHANNEL_POSITION_TOP_FRONT_CENTER ||
711 p == PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER ||
712 p == PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER;
713 }
714
715 static bool on_rear(pa_channel_position_t p) {
716 return
717 p == PA_CHANNEL_POSITION_REAR_LEFT ||
718 p == PA_CHANNEL_POSITION_REAR_RIGHT ||
719 p == PA_CHANNEL_POSITION_REAR_CENTER ||
720 p == PA_CHANNEL_POSITION_TOP_REAR_LEFT ||
721 p == PA_CHANNEL_POSITION_TOP_REAR_RIGHT ||
722 p == PA_CHANNEL_POSITION_TOP_REAR_CENTER;
723 }
724
725 static bool on_side(pa_channel_position_t p) {
726 return
727 p == PA_CHANNEL_POSITION_SIDE_LEFT ||
728 p == PA_CHANNEL_POSITION_SIDE_RIGHT ||
729 p == PA_CHANNEL_POSITION_TOP_CENTER;
730 }
731
732 enum {
733 ON_FRONT,
734 ON_REAR,
735 ON_SIDE,
736 ON_OTHER
737 };
738
739 static int front_rear_side(pa_channel_position_t p) {
740 if (on_front(p))
741 return ON_FRONT;
742 if (on_rear(p))
743 return ON_REAR;
744 if (on_side(p))
745 return ON_SIDE;
746 return ON_OTHER;
747 }
748
749 static void calc_map_table(pa_resampler *r) {
750 unsigned oc, ic;
751 unsigned n_oc, n_ic;
752 bool ic_connected[PA_CHANNELS_MAX];
753 bool remix;
754 pa_strbuf *s;
755 char *t;
756 pa_remap_t *m;
757
758 pa_assert(r);
759
760 if (!(r->map_required = (r->i_ss.channels != r->o_ss.channels || (!(r->flags & PA_RESAMPLER_NO_REMAP) && !pa_channel_map_equal(&r->i_cm, &r->o_cm)))))
761 return;
762
763 m = &r->remap;
764
765 n_oc = r->o_ss.channels;
766 n_ic = r->i_ss.channels;
767
768 memset(m->map_table_f, 0, sizeof(m->map_table_f));
769 memset(m->map_table_i, 0, sizeof(m->map_table_i));
770
771 memset(ic_connected, 0, sizeof(ic_connected));
772 remix = (r->flags & (PA_RESAMPLER_NO_REMAP | PA_RESAMPLER_NO_REMIX)) == 0;
773
774 if (r->flags & PA_RESAMPLER_NO_REMAP) {
775 pa_assert(!remix);
776
777 for (oc = 0; oc < PA_MIN(n_ic, n_oc); oc++)
778 m->map_table_f[oc][oc] = 1.0f;
779
780 } else if (r->flags & PA_RESAMPLER_NO_REMIX) {
781 pa_assert(!remix);
782 for (oc = 0; oc < n_oc; oc++) {
783 pa_channel_position_t b = r->o_cm.map[oc];
784
785 for (ic = 0; ic < n_ic; ic++) {
786 pa_channel_position_t a = r->i_cm.map[ic];
787
788 /* We shall not do any remixing. Hence, just check by name */
789 if (a == b)
790 m->map_table_f[oc][ic] = 1.0f;
791 }
792 }
793 } else {
794
795 /* OK, we shall do the full monty: upmixing and downmixing. Our
796 * algorithm is relatively simple, does not do spacialization, delay
797 * elements or apply lowpass filters for LFE. Patches are always
798 * welcome, though. Oh, and it doesn't do any matrix decoding. (Which
799 * probably wouldn't make any sense anyway.)
800 *
801 * This code is not idempotent: downmixing an upmixed stereo stream is
802 * not identical to the original. The volume will not match, and the
803 * two channels will be a linear combination of both.
804 *
805 * This is loosely based on random suggestions found on the Internet,
806 * such as this:
807 * http://www.halfgaar.net/surround-sound-in-linux and the alsa upmix
808 * plugin.
809 *
810 * The algorithm works basically like this:
811 *
812 * 1) Connect all channels with matching names.
813 *
814 * 2) Mono Handling:
815 * S:Mono: Copy into all D:channels
816 * D:Mono: Avg all S:channels
817 *
818 * 3) Mix D:Left, D:Right:
819 * D:Left: If not connected, avg all S:Left
820 * D:Right: If not connected, avg all S:Right
821 *
822 * 4) Mix D:Center
823 * If not connected, avg all S:Center
824 * If still not connected, avg all S:Left, S:Right
825 *
826 * 5) Mix D:LFE
827 * If not connected, avg all S:*
828 *
829 * 6) Make sure S:Left/S:Right is used: S:Left/S:Right: If not
830 * connected, mix into all D:left and all D:right channels. Gain is
831 * 1/9.
832 *
833 * 7) Make sure S:Center, S:LFE is used:
834 *
835 * S:Center, S:LFE: If not connected, mix into all D:left, all
836 * D:right, all D:center channels. Gain is 0.5 for center and 0.375
837 * for LFE. C-front is only mixed into L-front/R-front if available,
838 * otherwise into all L/R channels. Similarly for C-rear.
839 *
840 * 8) Normalize each row in the matrix such that the sum for each row is
841 * not larger than 1.0 in order to avoid clipping.
842 *
843 * S: and D: shall relate to the source resp. destination channels.
844 *
845 * Rationale: 1, 2 are probably obvious. For 3: this copies front to
846 * rear if needed. For 4: we try to find some suitable C source for C,
847 * if we don't find any, we avg L and R. For 5: LFE is mixed from all
848 * channels. For 6: the rear channels should not be dropped entirely,
849 * however have only minimal impact. For 7: movies usually encode
850 * speech on the center channel. Thus we have to make sure this channel
851 * is distributed to L and R if not available in the output. Also, LFE
852 * is used to achieve a greater dynamic range, and thus we should try
853 * to do our best to pass it to L+R.
854 */
855
856 unsigned
857 ic_left = 0,
858 ic_right = 0,
859 ic_center = 0,
860 ic_unconnected_left = 0,
861 ic_unconnected_right = 0,
862 ic_unconnected_center = 0,
863 ic_unconnected_lfe = 0;
864 bool ic_unconnected_center_mixed_in = 0;
865
866 pa_assert(remix);
867
868 for (ic = 0; ic < n_ic; ic++) {
869 if (on_left(r->i_cm.map[ic]))
870 ic_left++;
871 if (on_right(r->i_cm.map[ic]))
872 ic_right++;
873 if (on_center(r->i_cm.map[ic]))
874 ic_center++;
875 }
876
877 for (oc = 0; oc < n_oc; oc++) {
878 bool oc_connected = false;
879 pa_channel_position_t b = r->o_cm.map[oc];
880
881 for (ic = 0; ic < n_ic; ic++) {
882 pa_channel_position_t a = r->i_cm.map[ic];
883
884 if (a == b || a == PA_CHANNEL_POSITION_MONO) {
885 m->map_table_f[oc][ic] = 1.0f;
886
887 oc_connected = true;
888 ic_connected[ic] = true;
889 }
890 else if (b == PA_CHANNEL_POSITION_MONO) {
891 m->map_table_f[oc][ic] = 1.0f / (float) n_ic;
892
893 oc_connected = true;
894 ic_connected[ic] = true;
895 }
896 }
897
898 if (!oc_connected) {
899 /* Try to find matching input ports for this output port */
900
901 if (on_left(b)) {
902
903 /* We are not connected and on the left side, let's
904 * average all left side input channels. */
905
906 if (ic_left > 0)
907 for (ic = 0; ic < n_ic; ic++)
908 if (on_left(r->i_cm.map[ic])) {
909 m->map_table_f[oc][ic] = 1.0f / (float) ic_left;
910 ic_connected[ic] = true;
911 }
912
913 /* We ignore the case where there is no left input channel.
914 * Something is really wrong in this case anyway. */
915
916 } else if (on_right(b)) {
917
918 /* We are not connected and on the right side, let's
919 * average all right side input channels. */
920
921 if (ic_right > 0)
922 for (ic = 0; ic < n_ic; ic++)
923 if (on_right(r->i_cm.map[ic])) {
924 m->map_table_f[oc][ic] = 1.0f / (float) ic_right;
925 ic_connected[ic] = true;
926 }
927
928 /* We ignore the case where there is no right input
929 * channel. Something is really wrong in this case anyway.
930 * */
931
932 } else if (on_center(b)) {
933
934 if (ic_center > 0) {
935
936 /* We are not connected and at the center. Let's average
937 * all center input channels. */
938
939 for (ic = 0; ic < n_ic; ic++)
940 if (on_center(r->i_cm.map[ic])) {
941 m->map_table_f[oc][ic] = 1.0f / (float) ic_center;
942 ic_connected[ic] = true;
943 }
944
945 } else if (ic_left + ic_right > 0) {
946
947 /* Hmm, no center channel around, let's synthesize it
948 * by mixing L and R.*/
949
950 for (ic = 0; ic < n_ic; ic++)
951 if (on_left(r->i_cm.map[ic]) || on_right(r->i_cm.map[ic])) {
952 m->map_table_f[oc][ic] = 1.0f / (float) (ic_left + ic_right);
953 ic_connected[ic] = true;
954 }
955 }
956
957 /* We ignore the case where there is not even a left or
958 * right input channel. Something is really wrong in this
959 * case anyway. */
960
961 } else if (on_lfe(b) && !(r->flags & PA_RESAMPLER_NO_LFE)) {
962
963 /* We are not connected and an LFE. Let's average all
964 * channels for LFE. */
965
966 for (ic = 0; ic < n_ic; ic++)
967 m->map_table_f[oc][ic] = 1.0f / (float) n_ic;
968
969 /* Please note that a channel connected to LFE doesn't
970 * really count as connected. */
971 }
972 }
973 }
974
975 for (ic = 0; ic < n_ic; ic++) {
976 pa_channel_position_t a = r->i_cm.map[ic];
977
978 if (ic_connected[ic])
979 continue;
980
981 if (on_left(a))
982 ic_unconnected_left++;
983 else if (on_right(a))
984 ic_unconnected_right++;
985 else if (on_center(a))
986 ic_unconnected_center++;
987 else if (on_lfe(a))
988 ic_unconnected_lfe++;
989 }
990
991 for (ic = 0; ic < n_ic; ic++) {
992 pa_channel_position_t a = r->i_cm.map[ic];
993
994 if (ic_connected[ic])
995 continue;
996
997 for (oc = 0; oc < n_oc; oc++) {
998 pa_channel_position_t b = r->o_cm.map[oc];
999
1000 if (on_left(a) && on_left(b))
1001 m->map_table_f[oc][ic] = (1.f/9.f) / (float) ic_unconnected_left;
1002
1003 else if (on_right(a) && on_right(b))
1004 m->map_table_f[oc][ic] = (1.f/9.f) / (float) ic_unconnected_right;
1005
1006 else if (on_center(a) && on_center(b)) {
1007 m->map_table_f[oc][ic] = (1.f/9.f) / (float) ic_unconnected_center;
1008 ic_unconnected_center_mixed_in = true;
1009
1010 } else if (on_lfe(a) && !(r->flags & PA_RESAMPLER_NO_LFE))
1011 m->map_table_f[oc][ic] = .375f / (float) ic_unconnected_lfe;
1012 }
1013 }
1014
1015 if (ic_unconnected_center > 0 && !ic_unconnected_center_mixed_in) {
1016 unsigned ncenter[PA_CHANNELS_MAX];
1017 bool found_frs[PA_CHANNELS_MAX];
1018
1019 memset(ncenter, 0, sizeof(ncenter));
1020 memset(found_frs, 0, sizeof(found_frs));
1021
1022 /* Hmm, as it appears there was no center channel we
1023 could mix our center channel in. In this case, mix it into
1024 left and right. Using .5 as the factor. */
1025
1026 for (ic = 0; ic < n_ic; ic++) {
1027
1028 if (ic_connected[ic])
1029 continue;
1030
1031 if (!on_center(r->i_cm.map[ic]))
1032 continue;
1033
1034 for (oc = 0; oc < n_oc; oc++) {
1035
1036 if (!on_left(r->o_cm.map[oc]) && !on_right(r->o_cm.map[oc]))
1037 continue;
1038
1039 if (front_rear_side(r->i_cm.map[ic]) == front_rear_side(r->o_cm.map[oc])) {
1040 found_frs[ic] = true;
1041 break;
1042 }
1043 }
1044
1045 for (oc = 0; oc < n_oc; oc++) {
1046
1047 if (!on_left(r->o_cm.map[oc]) && !on_right(r->o_cm.map[oc]))
1048 continue;
1049
1050 if (!found_frs[ic] || front_rear_side(r->i_cm.map[ic]) == front_rear_side(r->o_cm.map[oc]))
1051 ncenter[oc]++;
1052 }
1053 }
1054
1055 for (oc = 0; oc < n_oc; oc++) {
1056
1057 if (!on_left(r->o_cm.map[oc]) && !on_right(r->o_cm.map[oc]))
1058 continue;
1059
1060 if (ncenter[oc] <= 0)
1061 continue;
1062
1063 for (ic = 0; ic < n_ic; ic++) {
1064
1065 if (!on_center(r->i_cm.map[ic]))
1066 continue;
1067
1068 if (!found_frs[ic] || front_rear_side(r->i_cm.map[ic]) == front_rear_side(r->o_cm.map[oc]))
1069 m->map_table_f[oc][ic] = .5f / (float) ncenter[oc];
1070 }
1071 }
1072 }
1073 }
1074
1075 for (oc = 0; oc < n_oc; oc++) {
1076 float sum = 0.0f;
1077 for (ic = 0; ic < n_ic; ic++)
1078 sum += m->map_table_f[oc][ic];
1079
1080 if (sum > 1.0f)
1081 for (ic = 0; ic < n_ic; ic++)
1082 m->map_table_f[oc][ic] /= sum;
1083 }
1084
1085 /* make an 16:16 int version of the matrix */
1086 for (oc = 0; oc < n_oc; oc++)
1087 for (ic = 0; ic < n_ic; ic++)
1088 m->map_table_i[oc][ic] = (int32_t) (m->map_table_f[oc][ic] * 0x10000);
1089
1090 s = pa_strbuf_new();
1091
1092 pa_strbuf_printf(s, " ");
1093 for (ic = 0; ic < n_ic; ic++)
1094 pa_strbuf_printf(s, " I%02u ", ic);
1095 pa_strbuf_puts(s, "\n +");
1096
1097 for (ic = 0; ic < n_ic; ic++)
1098 pa_strbuf_printf(s, "------");
1099 pa_strbuf_puts(s, "\n");
1100
1101 for (oc = 0; oc < n_oc; oc++) {
1102 pa_strbuf_printf(s, "O%02u |", oc);
1103
1104 for (ic = 0; ic < n_ic; ic++)
1105 pa_strbuf_printf(s, " %1.3f", m->map_table_f[oc][ic]);
1106
1107 pa_strbuf_puts(s, "\n");
1108 }
1109
1110 pa_log_debug("Channel matrix:\n%s", t = pa_strbuf_tostring_free(s));
1111 pa_xfree(t);
1112
1113 /* initialize the remapping function */
1114 pa_init_remap(m);
1115 }
1116
1117 static pa_memchunk* convert_to_work_format(pa_resampler *r, pa_memchunk *input) {
1118 unsigned n_samples;
1119 void *src, *dst;
1120
1121 pa_assert(r);
1122 pa_assert(input);
1123 pa_assert(input->memblock);
1124
1125 /* Convert the incoming sample into the work sample format and place them
1126 * in to_work_format_buf. */
1127
1128 if (!r->to_work_format_func || !input->length)
1129 return input;
1130
1131 n_samples = (unsigned) ((input->length / r->i_fz) * r->i_ss.channels);
1132
1133 r->to_work_format_buf.index = 0;
1134 r->to_work_format_buf.length = r->w_sz * n_samples;
1135
1136 if (!r->to_work_format_buf.memblock || r->to_work_format_buf_samples < n_samples) {
1137 if (r->to_work_format_buf.memblock)
1138 pa_memblock_unref(r->to_work_format_buf.memblock);
1139
1140 r->to_work_format_buf_samples = n_samples;
1141 r->to_work_format_buf.memblock = pa_memblock_new(r->mempool, r->to_work_format_buf.length);
1142 }
1143
1144 src = pa_memblock_acquire_chunk(input);
1145 dst = pa_memblock_acquire(r->to_work_format_buf.memblock);
1146
1147 r->to_work_format_func(n_samples, src, dst);
1148
1149 pa_memblock_release(input->memblock);
1150 pa_memblock_release(r->to_work_format_buf.memblock);
1151
1152 return &r->to_work_format_buf;
1153 }
1154
1155 static pa_memchunk *remap_channels(pa_resampler *r, pa_memchunk *input) {
1156 unsigned in_n_samples, out_n_samples, in_n_frames, out_n_frames;
1157 void *src, *dst;
1158 size_t leftover_length = 0;
1159 bool have_leftover;
1160
1161 pa_assert(r);
1162 pa_assert(input);
1163 pa_assert(input->memblock);
1164
1165 /* Remap channels and place the result in remap_buf. There may be leftover
1166 * data in the beginning of remap_buf. The leftover data is already
1167 * remapped, so it's not part of the input, it's part of the output. */
1168
1169 have_leftover = r->remap_buf_contains_leftover_data;
1170 r->remap_buf_contains_leftover_data = false;
1171
1172 if (!have_leftover && (!r->map_required || input->length <= 0))
1173 return input;
1174 else if (input->length <= 0)
1175 return &r->remap_buf;
1176
1177 in_n_samples = (unsigned) (input->length / r->w_sz);
1178 in_n_frames = out_n_frames = in_n_samples / r->i_ss.channels;
1179
1180 if (have_leftover) {
1181 leftover_length = r->remap_buf.length;
1182 out_n_frames += leftover_length / (r->w_sz * r->o_ss.channels);
1183 }
1184
1185 out_n_samples = out_n_frames * r->o_ss.channels;
1186 r->remap_buf.length = out_n_samples * r->w_sz;
1187
1188 if (have_leftover) {
1189 if (r->remap_buf_size < r->remap_buf.length) {
1190 pa_memblock *new_block = pa_memblock_new(r->mempool, r->remap_buf.length);
1191
1192 src = pa_memblock_acquire(r->remap_buf.memblock);
1193 dst = pa_memblock_acquire(new_block);
1194 memcpy(dst, src, leftover_length);
1195 pa_memblock_release(r->remap_buf.memblock);
1196 pa_memblock_release(new_block);
1197
1198 pa_memblock_unref(r->remap_buf.memblock);
1199 r->remap_buf.memblock = new_block;
1200 r->remap_buf_size = r->remap_buf.length;
1201 }
1202
1203 } else {
1204 if (!r->remap_buf.memblock || r->remap_buf_size < r->remap_buf.length) {
1205 if (r->remap_buf.memblock)
1206 pa_memblock_unref(r->remap_buf.memblock);
1207
1208 r->remap_buf_size = r->remap_buf.length;
1209 r->remap_buf.memblock = pa_memblock_new(r->mempool, r->remap_buf.length);
1210 }
1211 }
1212
1213 src = pa_memblock_acquire_chunk(input);
1214 dst = (uint8_t *) pa_memblock_acquire(r->remap_buf.memblock) + leftover_length;
1215
1216 if (r->map_required) {
1217 pa_remap_t *remap = &r->remap;
1218
1219 pa_assert(remap->do_remap);
1220 remap->do_remap(remap, dst, src, in_n_frames);
1221
1222 } else
1223 memcpy(dst, src, input->length);
1224
1225 pa_memblock_release(input->memblock);
1226 pa_memblock_release(r->remap_buf.memblock);
1227
1228 return &r->remap_buf;
1229 }
1230
1231 static pa_memchunk *resample(pa_resampler *r, pa_memchunk *input) {
1232 unsigned in_n_frames, in_n_samples;
1233 unsigned out_n_frames, out_n_samples;
1234
1235 pa_assert(r);
1236 pa_assert(input);
1237
1238 /* Resample the data and place the result in resample_buf. */
1239
1240 if (!r->impl.resample || !input->length)
1241 return input;
1242
1243 in_n_samples = (unsigned) (input->length / r->w_sz);
1244 in_n_frames = (unsigned) (in_n_samples / r->o_ss.channels);
1245
1246 out_n_frames = ((in_n_frames*r->o_ss.rate)/r->i_ss.rate)+EXTRA_FRAMES;
1247 out_n_samples = out_n_frames * r->o_ss.channels;
1248
1249 r->resample_buf.index = 0;
1250 r->resample_buf.length = r->w_sz * out_n_samples;
1251
1252 if (!r->resample_buf.memblock || r->resample_buf_samples < out_n_samples) {
1253 if (r->resample_buf.memblock)
1254 pa_memblock_unref(r->resample_buf.memblock);
1255
1256 r->resample_buf_samples = out_n_samples;
1257 r->resample_buf.memblock = pa_memblock_new(r->mempool, r->resample_buf.length);
1258 }
1259
1260 r->impl.resample(r, input, in_n_frames, &r->resample_buf, &out_n_frames);
1261 r->resample_buf.length = out_n_frames * r->w_sz * r->o_ss.channels;
1262
1263 return &r->resample_buf;
1264 }
1265
1266 static pa_memchunk *convert_from_work_format(pa_resampler *r, pa_memchunk *input) {
1267 unsigned n_samples, n_frames;
1268 void *src, *dst;
1269
1270 pa_assert(r);
1271 pa_assert(input);
1272
1273 /* Convert the data into the correct sample type and place the result in
1274 * from_work_format_buf. */
1275
1276 if (!r->from_work_format_func || !input->length)
1277 return input;
1278
1279 n_samples = (unsigned) (input->length / r->w_sz);
1280 n_frames = n_samples / r->o_ss.channels;
1281
1282 r->from_work_format_buf.index = 0;
1283 r->from_work_format_buf.length = r->o_fz * n_frames;
1284
1285 if (!r->from_work_format_buf.memblock || r->from_work_format_buf_samples < n_samples) {
1286 if (r->from_work_format_buf.memblock)
1287 pa_memblock_unref(r->from_work_format_buf.memblock);
1288
1289 r->from_work_format_buf_samples = n_samples;
1290 r->from_work_format_buf.memblock = pa_memblock_new(r->mempool, r->from_work_format_buf.length);
1291 }
1292
1293 src = pa_memblock_acquire_chunk(input);
1294 dst = pa_memblock_acquire(r->from_work_format_buf.memblock);
1295 r->from_work_format_func(n_samples, src, dst);
1296 pa_memblock_release(input->memblock);
1297 pa_memblock_release(r->from_work_format_buf.memblock);
1298
1299 return &r->from_work_format_buf;
1300 }
1301
1302 void pa_resampler_run(pa_resampler *r, const pa_memchunk *in, pa_memchunk *out) {
1303 pa_memchunk *buf;
1304
1305 pa_assert(r);
1306 pa_assert(in);
1307 pa_assert(out);
1308 pa_assert(in->length);
1309 pa_assert(in->memblock);
1310 pa_assert(in->length % r->i_fz == 0);
1311
1312 buf = (pa_memchunk*) in;
1313 buf = convert_to_work_format(r, buf);
1314 buf = remap_channels(r, buf);
1315 buf = resample(r, buf);
1316
1317 if (buf->length) {
1318 buf = convert_from_work_format(r, buf);
1319 *out = *buf;
1320
1321 if (buf == in)
1322 pa_memblock_ref(buf->memblock);
1323 else
1324 pa_memchunk_reset(buf);
1325 } else
1326 pa_memchunk_reset(out);
1327 }
1328
1329 static void save_leftover(pa_resampler *r, void *buf, size_t len) {
1330 void *dst;
1331
1332 pa_assert(r);
1333 pa_assert(buf);
1334 pa_assert(len > 0);
1335
1336 /* Store the leftover to remap_buf. */
1337
1338 r->remap_buf.length = len;
1339
1340 if (!r->remap_buf.memblock || r->remap_buf_size < r->remap_buf.length) {
1341 if (r->remap_buf.memblock)
1342 pa_memblock_unref(r->remap_buf.memblock);
1343
1344 r->remap_buf_size = r->remap_buf.length;
1345 r->remap_buf.memblock = pa_memblock_new(r->mempool, r->remap_buf.length);
1346 }
1347
1348 dst = pa_memblock_acquire(r->remap_buf.memblock);
1349 memcpy(dst, buf, r->remap_buf.length);
1350 pa_memblock_release(r->remap_buf.memblock);
1351
1352 r->remap_buf_contains_leftover_data = true;
1353 }
1354
1355 /*** libsamplerate based implementation ***/
1356
1357 #ifdef HAVE_LIBSAMPLERATE
1358 static void libsamplerate_resample(pa_resampler *r, const pa_memchunk *input, unsigned in_n_frames, pa_memchunk *output, unsigned *out_n_frames) {
1359 SRC_DATA data;
1360 struct src_data *libsamplerate_data;
1361
1362 pa_assert(r);
1363 pa_assert(input);
1364 pa_assert(output);
1365 pa_assert(out_n_frames);
1366
1367 libsamplerate_data = r->impl.data;
1368 memset(&data, 0, sizeof(data));
1369
1370 data.data_in = pa_memblock_acquire_chunk(input);
1371 data.input_frames = (long int) in_n_frames;
1372
1373 data.data_out = pa_memblock_acquire_chunk(output);
1374 data.output_frames = (long int) *out_n_frames;
1375
1376 data.src_ratio = (double) r->o_ss.rate / r->i_ss.rate;
1377 data.end_of_input = 0;
1378
1379 pa_assert_se(src_process(libsamplerate_data->state, &data) == 0);
1380
1381 if (data.input_frames_used < in_n_frames) {
1382 void *leftover_data = data.data_in + data.input_frames_used * r->o_ss.channels;
1383 size_t leftover_length = (in_n_frames - data.input_frames_used) * sizeof(float) * r->o_ss.channels;
1384
1385 save_leftover(r, leftover_data, leftover_length);
1386 }
1387
1388 pa_memblock_release(input->memblock);
1389 pa_memblock_release(output->memblock);
1390
1391 *out_n_frames = (unsigned) data.output_frames_gen;
1392 }
1393
1394 static void libsamplerate_update_rates(pa_resampler *r) {
1395 struct src_data *libsamplerate_data;
1396 pa_assert(r);
1397
1398 libsamplerate_data = r->impl.data;
1399 pa_assert_se(src_set_ratio(libsamplerate_data->state, (double) r->o_ss.rate / r->i_ss.rate) == 0);
1400 }
1401
1402 static void libsamplerate_reset(pa_resampler *r) {
1403 struct src_data *libsamplerate_data;
1404 pa_assert(r);
1405
1406 libsamplerate_data = r->impl.data;
1407 pa_assert_se(src_reset(libsamplerate_data->state) == 0);
1408 }
1409
1410 static void libsamplerate_free(pa_resampler *r) {
1411 struct src_data *libsamplerate_data;
1412 pa_assert(r);
1413
1414 libsamplerate_data = r->impl.data;
1415 if (libsamplerate_data->state)
1416 src_delete(libsamplerate_data->state);
1417 }
1418
1419 static int libsamplerate_init(pa_resampler *r) {
1420 int err;
1421 struct src_data *libsamplerate_data;
1422
1423 pa_assert(r);
1424
1425 libsamplerate_data = pa_xnew(struct src_data, 1);
1426
1427 if (!(libsamplerate_data->state = src_new(r->method, r->o_ss.channels, &err)))
1428 return -1;
1429
1430 r->impl.free = libsamplerate_free;
1431 r->impl.update_rates = libsamplerate_update_rates;
1432 r->impl.resample = libsamplerate_resample;
1433 r->impl.reset = libsamplerate_reset;
1434 r->impl.data = libsamplerate_data;
1435
1436 return 0;
1437 }
1438 #endif
1439
1440 #ifdef HAVE_SPEEX
1441 /*** speex based implementation ***/
1442
1443 static void speex_resample_float(pa_resampler *r, const pa_memchunk *input, unsigned in_n_frames, pa_memchunk *output, unsigned *out_n_frames) {
1444 float *in, *out;
1445 uint32_t inf = in_n_frames, outf = *out_n_frames;
1446 struct speex_data *speex_data;
1447
1448 pa_assert(r);
1449 pa_assert(input);
1450 pa_assert(output);
1451 pa_assert(out_n_frames);
1452
1453 speex_data = r->impl.data;
1454
1455 in = pa_memblock_acquire_chunk(input);
1456 out = pa_memblock_acquire_chunk(output);
1457
1458 pa_assert_se(speex_resampler_process_interleaved_float(speex_data->state, in, &inf, out, &outf) == 0);
1459
1460 pa_memblock_release(input->memblock);
1461 pa_memblock_release(output->memblock);
1462
1463 pa_assert(inf == in_n_frames);
1464 *out_n_frames = outf;
1465 }
1466
1467 static void speex_resample_int(pa_resampler *r, const pa_memchunk *input, unsigned in_n_frames, pa_memchunk *output, unsigned *out_n_frames) {
1468 int16_t *in, *out;
1469 uint32_t inf = in_n_frames, outf = *out_n_frames;
1470 struct speex_data *speex_data;
1471
1472 pa_assert(r);
1473 pa_assert(input);
1474 pa_assert(output);
1475 pa_assert(out_n_frames);
1476
1477 speex_data = r->impl.data;
1478
1479 in = pa_memblock_acquire_chunk(input);
1480 out = pa_memblock_acquire_chunk(output);
1481
1482 pa_assert_se(speex_resampler_process_interleaved_int(speex_data->state, in, &inf, out, &outf) == 0);
1483
1484 pa_memblock_release(input->memblock);
1485 pa_memblock_release(output->memblock);
1486
1487 pa_assert(inf == in_n_frames);
1488 *out_n_frames = outf;
1489 }
1490
1491 static void speex_update_rates(pa_resampler *r) {
1492 struct speex_data *speex_data;
1493 pa_assert(r);
1494
1495 speex_data = r->impl.data;
1496
1497 pa_assert_se(speex_resampler_set_rate(speex_data->state, r->i_ss.rate, r->o_ss.rate) == 0);
1498 }
1499
1500 static void speex_reset(pa_resampler *r) {
1501 struct speex_data *speex_data;
1502 pa_assert(r);
1503
1504 speex_data = r->impl.data;
1505
1506 pa_assert_se(speex_resampler_reset_mem(speex_data->state) == 0);
1507 }
1508
1509 static void speex_free(pa_resampler *r) {
1510 struct speex_data *speex_data;
1511 pa_assert(r);
1512
1513 speex_data = r->impl.data;
1514 if (!speex_data->state)
1515 return;
1516
1517 speex_resampler_destroy(speex_data->state);
1518 }
1519
1520 static int speex_init(pa_resampler *r) {
1521 int q, err;
1522 struct speex_data *speex_data;
1523
1524 pa_assert(r);
1525
1526 speex_data = pa_xnew(struct speex_data, 1);
1527
1528 r->impl.free = speex_free;
1529 r->impl.update_rates = speex_update_rates;
1530 r->impl.reset = speex_reset;
1531 r->impl.data = speex_data;
1532
1533 if (r->method >= PA_RESAMPLER_SPEEX_FIXED_BASE && r->method <= PA_RESAMPLER_SPEEX_FIXED_MAX) {
1534
1535 q = r->method - PA_RESAMPLER_SPEEX_FIXED_BASE;
1536 r->impl.resample = speex_resample_int;
1537
1538 } else {
1539 pa_assert(r->method >= PA_RESAMPLER_SPEEX_FLOAT_BASE && r->method <= PA_RESAMPLER_SPEEX_FLOAT_MAX);
1540
1541 q = r->method - PA_RESAMPLER_SPEEX_FLOAT_BASE;
1542 r->impl.resample = speex_resample_float;
1543 }
1544
1545 pa_log_info("Choosing speex quality setting %i.", q);
1546
1547 if (!(speex_data->state = speex_resampler_init(r->o_ss.channels, r->i_ss.rate, r->o_ss.rate, q, &err)))
1548 return -1;
1549
1550 return 0;
1551 }
1552 #endif
1553
1554 /* Trivial implementation */
1555
1556 static void trivial_resample(pa_resampler *r, const pa_memchunk *input, unsigned in_n_frames, pa_memchunk *output, unsigned *out_n_frames) {
1557 size_t fz;
1558 unsigned i_index, o_index;
1559 void *src, *dst;
1560 struct trivial_data *trivial_data;
1561
1562 pa_assert(r);
1563 pa_assert(input);
1564 pa_assert(output);
1565 pa_assert(out_n_frames);
1566
1567 trivial_data = r->impl.data;
1568 fz = r->w_sz * r->o_ss.channels;
1569
1570 src = pa_memblock_acquire_chunk(input);
1571 dst = pa_memblock_acquire_chunk(output);
1572
1573 for (o_index = 0;; o_index++, trivial_data->o_counter++) {
1574 i_index = ((uint64_t) trivial_data->o_counter * r->i_ss.rate) / r->o_ss.rate;
1575 i_index = i_index > trivial_data->i_counter ? i_index - trivial_data->i_counter : 0;
1576
1577 if (i_index >= in_n_frames)
1578 break;
1579
1580 pa_assert_fp(o_index * fz < pa_memblock_get_length(output->memblock));
1581
1582 memcpy((uint8_t*) dst + fz * o_index, (uint8_t*) src + fz * i_index, (int) fz);
1583 }
1584
1585 pa_memblock_release(input->memblock);
1586 pa_memblock_release(output->memblock);
1587
1588 *out_n_frames = o_index;
1589
1590 trivial_data->i_counter += in_n_frames;
1591
1592 /* Normalize counters */
1593 while (trivial_data->i_counter >= r->i_ss.rate) {
1594 pa_assert(trivial_data->o_counter >= r->o_ss.rate);
1595
1596 trivial_data->i_counter -= r->i_ss.rate;
1597 trivial_data->o_counter -= r->o_ss.rate;
1598 }
1599 }
1600
1601 static void trivial_update_rates_or_reset(pa_resampler *r) {
1602 struct trivial_data *trivial_data;
1603 pa_assert(r);
1604
1605 trivial_data = r->impl.data;
1606
1607 trivial_data->i_counter = 0;
1608 trivial_data->o_counter = 0;
1609 }
1610
1611 static int trivial_init(pa_resampler*r) {
1612 struct trivial_data *trivial_data;
1613 pa_assert(r);
1614
1615 trivial_data = pa_xnew0(struct trivial_data, 1);
1616
1617 r->impl.resample = trivial_resample;
1618 r->impl.update_rates = trivial_update_rates_or_reset;
1619 r->impl.reset = trivial_update_rates_or_reset;
1620 r->impl.data = trivial_data;
1621
1622 return 0;
1623 }
1624
1625 /* Peak finder implementation */
1626
1627 static void peaks_resample(pa_resampler *r, const pa_memchunk *input, unsigned in_n_frames, pa_memchunk *output, unsigned *out_n_frames) {
1628 unsigned c, o_index = 0;
1629 unsigned i, i_end = 0;
1630 void *src, *dst;
1631 struct peaks_data *peaks_data;
1632
1633 pa_assert(r);
1634 pa_assert(input);
1635 pa_assert(output);
1636 pa_assert(out_n_frames);
1637
1638 peaks_data = r->impl.data;
1639 src = pa_memblock_acquire_chunk(input);
1640 dst = pa_memblock_acquire_chunk(output);
1641
1642 i = ((uint64_t) peaks_data->o_counter * r->i_ss.rate) / r->o_ss.rate;
1643 i = i > peaks_data->i_counter ? i - peaks_data->i_counter : 0;
1644
1645 while (i_end < in_n_frames) {
1646 i_end = ((uint64_t) (peaks_data->o_counter + 1) * r->i_ss.rate) / r->o_ss.rate;
1647 i_end = i_end > peaks_data->i_counter ? i_end - peaks_data->i_counter : 0;
1648
1649 pa_assert_fp(o_index * r->w_sz * r->o_ss.channels < pa_memblock_get_length(output->memblock));
1650
1651 /* 1ch float is treated separately, because that is the common case */
1652 if (r->o_ss.channels == 1 && r->work_format == PA_SAMPLE_FLOAT32NE) {
1653 float *s = (float*) src + i;
1654 float *d = (float*) dst + o_index;
1655
1656 for (; i < i_end && i < in_n_frames; i++) {
1657 float n = fabsf(*s++);
1658
1659 if (n > peaks_data->max_f[0])
1660 peaks_data->max_f[0] = n;
1661 }
1662
1663 if (i == i_end) {
1664 *d = peaks_data->max_f[0];
1665 peaks_data->max_f[0] = 0;
1666 o_index++, peaks_data->o_counter++;
1667 }
1668 } else if (r->work_format == PA_SAMPLE_S16NE) {
1669 int16_t *s = (int16_t*) src + r->o_ss.channels * i;
1670 int16_t *d = (int16_t*) dst + r->o_ss.channels * o_index;
1671
1672 for (; i < i_end && i < in_n_frames; i++)
1673 for (c = 0; c < r->o_ss.channels; c++) {
1674 int16_t n = abs(*s++);
1675
1676 if (n > peaks_data->max_i[c])
1677 peaks_data->max_i[c] = n;
1678 }
1679
1680 if (i == i_end) {
1681 for (c = 0; c < r->o_ss.channels; c++, d++) {
1682 *d = peaks_data->max_i[c];
1683 peaks_data->max_i[c] = 0;
1684 }
1685 o_index++, peaks_data->o_counter++;
1686 }
1687 } else {
1688 float *s = (float*) src + r->o_ss.channels * i;
1689 float *d = (float*) dst + r->o_ss.channels * o_index;
1690
1691 for (; i < i_end && i < in_n_frames; i++)
1692 for (c = 0; c < r->o_ss.channels; c++) {
1693 float n = fabsf(*s++);
1694
1695 if (n > peaks_data->max_f[c])
1696 peaks_data->max_f[c] = n;
1697 }
1698
1699 if (i == i_end) {
1700 for (c = 0; c < r->o_ss.channels; c++, d++) {
1701 *d = peaks_data->max_f[c];
1702 peaks_data->max_f[c] = 0;
1703 }
1704 o_index++, peaks_data->o_counter++;
1705 }
1706 }
1707 }
1708
1709 pa_memblock_release(input->memblock);
1710 pa_memblock_release(output->memblock);
1711
1712 *out_n_frames = o_index;
1713
1714 peaks_data->i_counter += in_n_frames;
1715
1716 /* Normalize counters */
1717 while (peaks_data->i_counter >= r->i_ss.rate) {
1718 pa_assert(peaks_data->o_counter >= r->o_ss.rate);
1719
1720 peaks_data->i_counter -= r->i_ss.rate;
1721 peaks_data->o_counter -= r->o_ss.rate;
1722 }
1723 }
1724
1725 static void peaks_update_rates_or_reset(pa_resampler *r) {
1726 struct peaks_data *peaks_data;
1727 pa_assert(r);
1728
1729 peaks_data = r->impl.data;
1730
1731 peaks_data->i_counter = 0;
1732 peaks_data->o_counter = 0;
1733 }
1734
1735 static int peaks_init(pa_resampler*r) {
1736 struct peaks_data *peaks_data;
1737 pa_assert(r);
1738 pa_assert(r->i_ss.rate >= r->o_ss.rate);
1739 pa_assert(r->work_format == PA_SAMPLE_S16NE || r->work_format == PA_SAMPLE_FLOAT32NE);
1740
1741 peaks_data = pa_xnew0(struct peaks_data, 1);
1742
1743 r->impl.resample = peaks_resample;
1744 r->impl.update_rates = peaks_update_rates_or_reset;
1745 r->impl.reset = peaks_update_rates_or_reset;
1746 r->impl.data = peaks_data;
1747
1748 return 0;
1749 }
1750
1751 /*** ffmpeg based implementation ***/
1752
1753 static void ffmpeg_resample(pa_resampler *r, const pa_memchunk *input, unsigned in_n_frames, pa_memchunk *output, unsigned *out_n_frames) {
1754 unsigned used_frames = 0, c;
1755 int previous_consumed_frames = -1;
1756 struct ffmpeg_data *ffmpeg_data;
1757
1758 pa_assert(r);
1759 pa_assert(input);
1760 pa_assert(output);
1761 pa_assert(out_n_frames);
1762
1763 ffmpeg_data = r->impl.data;
1764
1765 for (c = 0; c < r->o_ss.channels; c++) {
1766 unsigned u;
1767 pa_memblock *b, *w;
1768 int16_t *p, *t, *k, *q, *s;
1769 int consumed_frames;
1770
1771 /* Allocate a new block */
1772 b = pa_memblock_new(r->mempool, ffmpeg_data->buf[c].length + in_n_frames * sizeof(int16_t));
1773 p = pa_memblock_acquire(b);
1774
1775 /* Now copy the input data, splitting up channels */
1776 t = (int16_t*) pa_memblock_acquire_chunk(input) + c;
1777 k = p;
1778 for (u = 0; u < in_n_frames; u++) {
1779 *k = *t;
1780 t += r->o_ss.channels;
1781 k ++;
1782 }
1783 pa_memblock_release(input->memblock);
1784
1785 /* Allocate buffer for the result */
1786 w = pa_memblock_new(r->mempool, *out_n_frames * sizeof(int16_t));
1787 q = pa_memblock_acquire(w);
1788
1789 /* Now, resample */
1790 used_frames = (unsigned) av_resample(ffmpeg_data->state,
1791 q, p,
1792 &consumed_frames,
1793 (int) in_n_frames, (int) *out_n_frames,
1794 c >= (unsigned) (r->o_ss.channels-1));
1795
1796 pa_memblock_release(b);
1797 pa_memblock_unref(b);
1798
1799 pa_assert(consumed_frames <= (int) in_n_frames);
1800 pa_assert(previous_consumed_frames == -1 || consumed_frames == previous_consumed_frames);
1801 previous_consumed_frames = consumed_frames;
1802
1803 /* And place the results in the output buffer */
1804 s = (int16_t *) pa_memblock_acquire_chunk(output) + c;
1805 for (u = 0; u < used_frames; u++) {
1806 *s = *q;
1807 q++;
1808 s += r->o_ss.channels;
1809 }
1810 pa_memblock_release(output->memblock);
1811 pa_memblock_release(w);
1812 pa_memblock_unref(w);
1813 }
1814
1815 if (previous_consumed_frames < (int) in_n_frames) {
1816 void *leftover_data = (int16_t *) pa_memblock_acquire_chunk(input) + previous_consumed_frames * r->o_ss.channels;
1817 size_t leftover_length = (in_n_frames - previous_consumed_frames) * r->o_ss.channels * sizeof(int16_t);
1818
1819 save_leftover(r, leftover_data, leftover_length);
1820 pa_memblock_release(input->memblock);
1821 }
1822
1823 *out_n_frames = used_frames;
1824 }
1825
1826 static void ffmpeg_free(pa_resampler *r) {
1827 unsigned c;
1828 struct ffmpeg_data *ffmpeg_data;
1829
1830 pa_assert(r);
1831
1832 ffmpeg_data = r->impl.data;
1833 if (ffmpeg_data->state)
1834 av_resample_close(ffmpeg_data->state);
1835
1836 for (c = 0; c < PA_ELEMENTSOF(ffmpeg_data->buf); c++)
1837 if (ffmpeg_data->buf[c].memblock)
1838 pa_memblock_unref(ffmpeg_data->buf[c].memblock);
1839 }
1840
1841 static int ffmpeg_init(pa_resampler *r) {
1842 unsigned c;
1843 struct ffmpeg_data *ffmpeg_data;
1844
1845 pa_assert(r);
1846
1847 ffmpeg_data = pa_xnew(struct ffmpeg_data, 1);
1848
1849 /* We could probably implement different quality levels by
1850 * adjusting the filter parameters here. However, ffmpeg
1851 * internally only uses these hardcoded values, so let's use them
1852 * here for now as well until ffmpeg makes this configurable. */
1853
1854 if (!(ffmpeg_data->state = av_resample_init((int) r->o_ss.rate, (int) r->i_ss.rate, 16, 10, 0, 0.8)))
1855 return -1;
1856
1857 r->impl.free = ffmpeg_free;
1858 r->impl.resample = ffmpeg_resample;
1859 r->impl.data = (void *) ffmpeg_data;
1860
1861 for (c = 0; c < PA_ELEMENTSOF(ffmpeg_data->buf); c++)
1862 pa_memchunk_reset(&ffmpeg_data->buf[c]);
1863
1864 return 0;
1865 }
1866
1867 /*** copy (noop) implementation ***/
1868
1869 static int copy_init(pa_resampler *r) {
1870 pa_assert(r);
1871
1872 pa_assert(r->o_ss.rate == r->i_ss.rate);
1873
1874 return 0;
1875 }