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