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