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