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