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