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