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