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