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