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