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