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