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