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