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