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