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