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