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