2 This file is part of PulseAudio.
4 Copyright 2004-2006 Lennart Poettering
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.
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.
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
28 #ifdef HAVE_LIBSAMPLERATE
29 #include <samplerate.h>
33 #include <speex/speex_resampler.h>
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"
45 #include "resampler.h"
47 /* Number of samples of extra space we allow the resamplers to return */
48 #define EXTRA_FRAMES 128
51 pa_resample_method_t method;
52 pa_resample_flags_t flags;
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;
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;
69 pa_sample_format_t work_format;
70 uint8_t work_channels;
72 pa_convert_func_t to_work_format_func;
73 pa_convert_func_t from_work_format_func;
78 pa_resampler_impl impl;
81 struct trivial_data { /* data specific to the trivial resampler */
86 struct peaks_data { /* data specific to the peak finder pseudo resampler */
90 float max_f[PA_CHANNELS_MAX];
91 int16_t max_i[PA_CHANNELS_MAX];
94 struct ffmpeg_data { /* data specific to ffmpeg */
95 struct AVResampleContext *state;
96 pa_memchunk buf[PA_CHANNELS_MAX];
99 static int copy_init(pa_resampler *r);
100 static int trivial_init(pa_resampler*r);
102 static int speex_init(pa_resampler*r);
104 static int ffmpeg_init(pa_resampler*r);
105 static int peaks_init(pa_resampler*r);
106 #ifdef HAVE_LIBSAMPLERATE
107 static int libsamplerate_init(pa_resampler*r);
110 static void calc_map_table(pa_resampler *r);
112 static int (* const init_table[])(pa_resampler*r) = {
113 #ifdef HAVE_LIBSAMPLERATE
114 [PA_RESAMPLER_SRC_SINC_BEST_QUALITY] = libsamplerate_init,
115 [PA_RESAMPLER_SRC_SINC_MEDIUM_QUALITY] = libsamplerate_init,
116 [PA_RESAMPLER_SRC_SINC_FASTEST] = libsamplerate_init,
117 [PA_RESAMPLER_SRC_ZERO_ORDER_HOLD] = libsamplerate_init,
118 [PA_RESAMPLER_SRC_LINEAR] = libsamplerate_init,
120 [PA_RESAMPLER_SRC_SINC_BEST_QUALITY] = NULL,
121 [PA_RESAMPLER_SRC_SINC_MEDIUM_QUALITY] = NULL,
122 [PA_RESAMPLER_SRC_SINC_FASTEST] = NULL,
123 [PA_RESAMPLER_SRC_ZERO_ORDER_HOLD] = NULL,
124 [PA_RESAMPLER_SRC_LINEAR] = NULL,
126 [PA_RESAMPLER_TRIVIAL] = trivial_init,
128 [PA_RESAMPLER_SPEEX_FLOAT_BASE+0] = speex_init,
129 [PA_RESAMPLER_SPEEX_FLOAT_BASE+1] = speex_init,
130 [PA_RESAMPLER_SPEEX_FLOAT_BASE+2] = speex_init,
131 [PA_RESAMPLER_SPEEX_FLOAT_BASE+3] = speex_init,
132 [PA_RESAMPLER_SPEEX_FLOAT_BASE+4] = speex_init,
133 [PA_RESAMPLER_SPEEX_FLOAT_BASE+5] = speex_init,
134 [PA_RESAMPLER_SPEEX_FLOAT_BASE+6] = speex_init,
135 [PA_RESAMPLER_SPEEX_FLOAT_BASE+7] = speex_init,
136 [PA_RESAMPLER_SPEEX_FLOAT_BASE+8] = speex_init,
137 [PA_RESAMPLER_SPEEX_FLOAT_BASE+9] = speex_init,
138 [PA_RESAMPLER_SPEEX_FLOAT_BASE+10] = speex_init,
139 [PA_RESAMPLER_SPEEX_FIXED_BASE+0] = speex_init,
140 [PA_RESAMPLER_SPEEX_FIXED_BASE+1] = speex_init,
141 [PA_RESAMPLER_SPEEX_FIXED_BASE+2] = speex_init,
142 [PA_RESAMPLER_SPEEX_FIXED_BASE+3] = speex_init,
143 [PA_RESAMPLER_SPEEX_FIXED_BASE+4] = speex_init,
144 [PA_RESAMPLER_SPEEX_FIXED_BASE+5] = speex_init,
145 [PA_RESAMPLER_SPEEX_FIXED_BASE+6] = speex_init,
146 [PA_RESAMPLER_SPEEX_FIXED_BASE+7] = speex_init,
147 [PA_RESAMPLER_SPEEX_FIXED_BASE+8] = speex_init,
148 [PA_RESAMPLER_SPEEX_FIXED_BASE+9] = speex_init,
149 [PA_RESAMPLER_SPEEX_FIXED_BASE+10] = speex_init,
151 [PA_RESAMPLER_SPEEX_FLOAT_BASE+0] = NULL,
152 [PA_RESAMPLER_SPEEX_FLOAT_BASE+1] = NULL,
153 [PA_RESAMPLER_SPEEX_FLOAT_BASE+2] = NULL,
154 [PA_RESAMPLER_SPEEX_FLOAT_BASE+3] = NULL,
155 [PA_RESAMPLER_SPEEX_FLOAT_BASE+4] = NULL,
156 [PA_RESAMPLER_SPEEX_FLOAT_BASE+5] = NULL,
157 [PA_RESAMPLER_SPEEX_FLOAT_BASE+6] = NULL,
158 [PA_RESAMPLER_SPEEX_FLOAT_BASE+7] = NULL,
159 [PA_RESAMPLER_SPEEX_FLOAT_BASE+8] = NULL,
160 [PA_RESAMPLER_SPEEX_FLOAT_BASE+9] = NULL,
161 [PA_RESAMPLER_SPEEX_FLOAT_BASE+10] = NULL,
162 [PA_RESAMPLER_SPEEX_FIXED_BASE+0] = NULL,
163 [PA_RESAMPLER_SPEEX_FIXED_BASE+1] = NULL,
164 [PA_RESAMPLER_SPEEX_FIXED_BASE+2] = NULL,
165 [PA_RESAMPLER_SPEEX_FIXED_BASE+3] = NULL,
166 [PA_RESAMPLER_SPEEX_FIXED_BASE+4] = NULL,
167 [PA_RESAMPLER_SPEEX_FIXED_BASE+5] = NULL,
168 [PA_RESAMPLER_SPEEX_FIXED_BASE+6] = NULL,
169 [PA_RESAMPLER_SPEEX_FIXED_BASE+7] = NULL,
170 [PA_RESAMPLER_SPEEX_FIXED_BASE+8] = NULL,
171 [PA_RESAMPLER_SPEEX_FIXED_BASE+9] = NULL,
172 [PA_RESAMPLER_SPEEX_FIXED_BASE+10] = NULL,
174 [PA_RESAMPLER_FFMPEG] = ffmpeg_init,
175 [PA_RESAMPLER_AUTO] = NULL,
176 [PA_RESAMPLER_COPY] = copy_init,
177 [PA_RESAMPLER_PEAKS] = peaks_init,
180 static pa_resample_method_t choose_auto_resampler(pa_resample_flags_t flags) {
181 pa_resample_method_t method;
183 if (pa_resample_method_supported(PA_RESAMPLER_SPEEX_FLOAT_BASE + 1))
184 method = PA_RESAMPLER_SPEEX_FLOAT_BASE + 1;
185 else if (flags & PA_RESAMPLER_VARIABLE_RATE)
186 method = PA_RESAMPLER_TRIVIAL;
188 method = PA_RESAMPLER_FFMPEG;
193 static pa_resample_method_t pa_resampler_fix_method(
194 pa_resample_flags_t flags,
195 pa_resample_method_t method,
196 const uint32_t rate_a,
197 const uint32_t rate_b) {
199 pa_assert(rate_a > 0 && rate_a <= PA_RATE_MAX);
200 pa_assert(rate_b > 0 && rate_b <= PA_RATE_MAX);
201 pa_assert(method >= 0);
202 pa_assert(method < PA_RESAMPLER_MAX);
204 if (!(flags & PA_RESAMPLER_VARIABLE_RATE) && rate_a == rate_b) {
205 pa_log_info("Forcing resampler 'copy', because of fixed, identical sample rates.");
206 method = PA_RESAMPLER_COPY;
209 if (!pa_resample_method_supported(method)) {
210 pa_log_warn("Support for resampler '%s' not compiled in, reverting to 'auto'.", pa_resample_method_to_string(method));
211 method = PA_RESAMPLER_AUTO;
215 case PA_RESAMPLER_COPY:
216 if (rate_a != rate_b) {
217 pa_log_info("Resampler 'copy' cannot change sampling rate, reverting to resampler 'auto'.");
218 method = PA_RESAMPLER_AUTO;
221 /* Else fall through */
222 case PA_RESAMPLER_FFMPEG:
223 if (flags & PA_RESAMPLER_VARIABLE_RATE) {
224 pa_log_info("Resampler '%s' cannot do variable rate, reverting to resampler 'auto'.", pa_resample_method_to_string(method));
225 method = PA_RESAMPLER_AUTO;
229 /* The Peaks resampler only supports downsampling.
230 * Revert to auto if we are upsampling */
231 case PA_RESAMPLER_PEAKS:
232 if (rate_a < rate_b) {
233 pa_log_warn("The 'peaks' resampler only supports downsampling, reverting to resampler 'auto'.");
234 method = PA_RESAMPLER_AUTO;
242 if (method == PA_RESAMPLER_AUTO)
243 method = choose_auto_resampler(flags);
248 /* Return true if a is a more precise sample format than b, else return false */
249 static bool sample_format_more_precise(pa_sample_format_t a, pa_sample_format_t b) {
250 pa_assert(a >= 0 && a < PA_SAMPLE_MAX);
251 pa_assert(b >= 0 && b < PA_SAMPLE_MAX);
260 case PA_SAMPLE_S16LE:
261 case PA_SAMPLE_S16BE:
262 if (b == PA_SAMPLE_ULAW || b == PA_SAMPLE_ALAW || b == PA_SAMPLE_U8)
268 case PA_SAMPLE_S24LE:
269 case PA_SAMPLE_S24BE:
270 case PA_SAMPLE_S24_32LE:
271 case PA_SAMPLE_S24_32BE:
272 if (b == PA_SAMPLE_ULAW || b == PA_SAMPLE_ALAW || b == PA_SAMPLE_U8 ||
273 b == PA_SAMPLE_S16LE || b == PA_SAMPLE_S16BE)
279 case PA_SAMPLE_FLOAT32LE:
280 case PA_SAMPLE_FLOAT32BE:
281 case PA_SAMPLE_S32LE:
282 case PA_SAMPLE_S32BE:
283 if (b == PA_SAMPLE_FLOAT32LE || b == PA_SAMPLE_FLOAT32BE ||
284 b == PA_SAMPLE_S32LE || b == PA_SAMPLE_FLOAT32BE)
295 static pa_sample_format_t pa_resampler_choose_work_format(
296 pa_resample_method_t method,
297 pa_sample_format_t a,
298 pa_sample_format_t b,
300 pa_sample_format_t work_format;
302 pa_assert(a >= 0 && a < PA_SAMPLE_MAX);
303 pa_assert(b >= 0 && b < PA_SAMPLE_MAX);
304 pa_assert(method >= 0);
305 pa_assert(method < PA_RESAMPLER_MAX);
307 if (method >= PA_RESAMPLER_SPEEX_FIXED_BASE && method <= PA_RESAMPLER_SPEEX_FIXED_MAX)
308 method = PA_RESAMPLER_SPEEX_FIXED_BASE;
311 /* This block is for resampling functions that only
312 * support the S16 sample format. */
313 case PA_RESAMPLER_SPEEX_FIXED_BASE: /* fall through */
314 case PA_RESAMPLER_FFMPEG:
315 work_format = PA_SAMPLE_S16NE;
318 /* This block is for resampling functions that support
319 * any sample format. */
320 case PA_RESAMPLER_COPY: /* fall through */
321 case PA_RESAMPLER_TRIVIAL:
322 if (!map_required && a == b) {
326 /* Else fall trough */
327 case PA_RESAMPLER_PEAKS:
328 if (a == PA_SAMPLE_S16NE || b == PA_SAMPLE_S16NE)
329 work_format = PA_SAMPLE_S16NE;
330 else if (sample_format_more_precise(a, PA_SAMPLE_S16NE) ||
331 sample_format_more_precise(b, PA_SAMPLE_S16NE))
332 work_format = PA_SAMPLE_FLOAT32NE;
334 work_format = PA_SAMPLE_S16NE;
338 work_format = PA_SAMPLE_FLOAT32NE;
344 pa_resampler* pa_resampler_new(
346 const pa_sample_spec *a,
347 const pa_channel_map *am,
348 const pa_sample_spec *b,
349 const pa_channel_map *bm,
350 pa_resample_method_t method,
351 pa_resample_flags_t flags) {
353 pa_resampler *r = NULL;
358 pa_assert(pa_sample_spec_valid(a));
359 pa_assert(pa_sample_spec_valid(b));
360 pa_assert(method >= 0);
361 pa_assert(method < PA_RESAMPLER_MAX);
363 method = pa_resampler_fix_method(flags, method, a->rate, b->rate);
365 r = pa_xnew0(pa_resampler, 1);
370 /* Fill sample specs */
374 /* set up the remap structure */
375 r->remap.i_ss = &r->i_ss;
376 r->remap.o_ss = &r->o_ss;
377 r->remap.format = &r->work_format;
381 else if (!pa_channel_map_init_auto(&r->i_cm, r->i_ss.channels, PA_CHANNEL_MAP_DEFAULT))
386 else if (!pa_channel_map_init_auto(&r->o_cm, r->o_ss.channels, PA_CHANNEL_MAP_DEFAULT))
389 r->i_fz = pa_frame_size(a);
390 r->o_fz = pa_frame_size(b);
394 pa_log_info("Using resampler '%s'", pa_resample_method_to_string(method));
396 r->work_format = pa_resampler_choose_work_format(method, a->format, b->format, r->map_required);
398 pa_log_info("Using %s as working format.", pa_sample_format_to_string(r->work_format));
400 r->w_sz = pa_sample_size_of_format(r->work_format);
402 if (r->i_ss.format != r->work_format) {
403 if (r->work_format == PA_SAMPLE_FLOAT32NE) {
404 if (!(r->to_work_format_func = pa_get_convert_to_float32ne_function(r->i_ss.format)))
407 pa_assert(r->work_format == PA_SAMPLE_S16NE);
408 if (!(r->to_work_format_func = pa_get_convert_to_s16ne_function(r->i_ss.format)))
413 if (r->o_ss.format != r->work_format) {
414 if (r->work_format == PA_SAMPLE_FLOAT32NE) {
415 if (!(r->from_work_format_func = pa_get_convert_from_float32ne_function(r->o_ss.format)))
418 pa_assert(r->work_format == PA_SAMPLE_S16NE);
419 if (!(r->from_work_format_func = pa_get_convert_from_s16ne_function(r->o_ss.format)))
424 r->work_channels = r->o_ss.channels;
425 r->w_fz = pa_sample_size_of_format(r->work_format) * r->work_channels;
427 /* initialize implementation */
428 if (init_table[method](r) < 0)
439 void pa_resampler_free(pa_resampler *r) {
445 pa_xfree(r->impl.data);
447 if (r->to_work_format_buf.memblock)
448 pa_memblock_unref(r->to_work_format_buf.memblock);
449 if (r->remap_buf.memblock)
450 pa_memblock_unref(r->remap_buf.memblock);
451 if (r->resample_buf.memblock)
452 pa_memblock_unref(r->resample_buf.memblock);
453 if (r->from_work_format_buf.memblock)
454 pa_memblock_unref(r->from_work_format_buf.memblock);
459 void pa_resampler_set_input_rate(pa_resampler *r, uint32_t rate) {
462 pa_assert(r->impl.update_rates);
464 if (r->i_ss.rate == rate)
469 r->impl.update_rates(r);
472 void pa_resampler_set_output_rate(pa_resampler *r, uint32_t rate) {
475 pa_assert(r->impl.update_rates);
477 if (r->o_ss.rate == rate)
482 r->impl.update_rates(r);
485 size_t pa_resampler_request(pa_resampler *r, size_t out_length) {
488 /* Let's round up here to make it more likely that the caller will get at
489 * least out_length amount of data from pa_resampler_run().
491 * We don't take the leftover into account here. If we did, then it might
492 * be in theory possible that this function would return 0 and
493 * pa_resampler_run() would also return 0. That could lead to infinite
494 * loops. When the leftover is ignored here, such loops would eventually
495 * terminate, because the leftover would grow each round, finally
496 * surpassing the minimum input threshold of the resampler. */
497 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;
500 size_t pa_resampler_result(pa_resampler *r, size_t in_length) {
505 /* Let's round up here to ensure that the caller will always allocate big
506 * enough output buffer. */
508 frames = (in_length + r->i_fz - 1) / r->i_fz;
510 if (r->remap_buf_contains_leftover_data)
511 frames += r->remap_buf.length / r->w_fz;
513 return (((uint64_t) frames * r->o_ss.rate + r->i_ss.rate - 1) / r->i_ss.rate) * r->o_fz;
516 size_t pa_resampler_max_block_size(pa_resampler *r) {
517 size_t block_size_max;
518 pa_sample_spec max_ss;
524 block_size_max = pa_mempool_block_size_max(r->mempool);
526 /* We deduce the "largest" sample spec we're using during the
528 max_ss.channels = (uint8_t) (PA_MAX(r->i_ss.channels, r->o_ss.channels));
530 /* We silently assume that the format enum is ordered by size */
531 max_ss.format = PA_MAX(r->i_ss.format, r->o_ss.format);
532 max_ss.format = PA_MAX(max_ss.format, r->work_format);
534 max_ss.rate = PA_MAX(r->i_ss.rate, r->o_ss.rate);
536 max_fs = pa_frame_size(&max_ss);
537 frames = block_size_max / max_fs - EXTRA_FRAMES;
539 if (r->remap_buf_contains_leftover_data)
540 frames -= r->remap_buf.length / r->w_fz;
542 block_size_max = ((uint64_t) frames * r->i_ss.rate / max_ss.rate) * r->i_fz;
544 if (block_size_max > 0)
545 return block_size_max;
547 /* A single input frame may result in so much output that it doesn't
548 * fit in one standard memblock (e.g. converting 1 Hz to 44100 Hz). In
549 * this case the max block size will be set to one frame, and some
550 * memory will be probably be allocated with malloc() instead of using
553 * XXX: Should we support this case at all? We could also refuse to
554 * create resamplers whose max block size would exceed the memory pool
555 * block size. In this case also updating the resampler rate should
556 * fail if the new rate would cause an excessive max block size (in
557 * which case the stream would probably have to be killed). */
561 void pa_resampler_reset(pa_resampler *r) {
567 r->remap_buf_contains_leftover_data = false;
570 pa_resample_method_t pa_resampler_get_method(pa_resampler *r) {
576 const pa_channel_map* pa_resampler_input_channel_map(pa_resampler *r) {
582 const pa_sample_spec* pa_resampler_input_sample_spec(pa_resampler *r) {
588 const pa_channel_map* pa_resampler_output_channel_map(pa_resampler *r) {
594 const pa_sample_spec* pa_resampler_output_sample_spec(pa_resampler *r) {
600 static const char * const resample_methods[] = {
601 "src-sinc-best-quality",
602 "src-sinc-medium-quality",
604 "src-zero-order-hold",
635 const char *pa_resample_method_to_string(pa_resample_method_t m) {
637 if (m < 0 || m >= PA_RESAMPLER_MAX)
640 return resample_methods[m];
643 int pa_resample_method_supported(pa_resample_method_t m) {
645 if (m < 0 || m >= PA_RESAMPLER_MAX)
648 #ifndef HAVE_LIBSAMPLERATE
649 if (m <= PA_RESAMPLER_SRC_LINEAR)
654 if (m >= PA_RESAMPLER_SPEEX_FLOAT_BASE && m <= PA_RESAMPLER_SPEEX_FLOAT_MAX)
656 if (m >= PA_RESAMPLER_SPEEX_FIXED_BASE && m <= PA_RESAMPLER_SPEEX_FIXED_MAX)
663 pa_resample_method_t pa_parse_resample_method(const char *string) {
664 pa_resample_method_t m;
668 for (m = 0; m < PA_RESAMPLER_MAX; m++)
669 if (pa_streq(string, resample_methods[m]))
672 if (pa_streq(string, "speex-fixed"))
673 return PA_RESAMPLER_SPEEX_FIXED_BASE + 1;
675 if (pa_streq(string, "speex-float"))
676 return PA_RESAMPLER_SPEEX_FLOAT_BASE + 1;
678 return PA_RESAMPLER_INVALID;
681 static bool on_left(pa_channel_position_t p) {
684 p == PA_CHANNEL_POSITION_FRONT_LEFT ||
685 p == PA_CHANNEL_POSITION_REAR_LEFT ||
686 p == PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER ||
687 p == PA_CHANNEL_POSITION_SIDE_LEFT ||
688 p == PA_CHANNEL_POSITION_TOP_FRONT_LEFT ||
689 p == PA_CHANNEL_POSITION_TOP_REAR_LEFT;
692 static bool on_right(pa_channel_position_t p) {
695 p == PA_CHANNEL_POSITION_FRONT_RIGHT ||
696 p == PA_CHANNEL_POSITION_REAR_RIGHT ||
697 p == PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER ||
698 p == PA_CHANNEL_POSITION_SIDE_RIGHT ||
699 p == PA_CHANNEL_POSITION_TOP_FRONT_RIGHT ||
700 p == PA_CHANNEL_POSITION_TOP_REAR_RIGHT;
703 static bool on_center(pa_channel_position_t p) {
706 p == PA_CHANNEL_POSITION_FRONT_CENTER ||
707 p == PA_CHANNEL_POSITION_REAR_CENTER ||
708 p == PA_CHANNEL_POSITION_TOP_CENTER ||
709 p == PA_CHANNEL_POSITION_TOP_FRONT_CENTER ||
710 p == PA_CHANNEL_POSITION_TOP_REAR_CENTER;
713 static bool on_lfe(pa_channel_position_t p) {
715 p == PA_CHANNEL_POSITION_LFE;
718 static bool on_front(pa_channel_position_t p) {
720 p == PA_CHANNEL_POSITION_FRONT_LEFT ||
721 p == PA_CHANNEL_POSITION_FRONT_RIGHT ||
722 p == PA_CHANNEL_POSITION_FRONT_CENTER ||
723 p == PA_CHANNEL_POSITION_TOP_FRONT_LEFT ||
724 p == PA_CHANNEL_POSITION_TOP_FRONT_RIGHT ||
725 p == PA_CHANNEL_POSITION_TOP_FRONT_CENTER ||
726 p == PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER ||
727 p == PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER;
730 static bool on_rear(pa_channel_position_t p) {
732 p == PA_CHANNEL_POSITION_REAR_LEFT ||
733 p == PA_CHANNEL_POSITION_REAR_RIGHT ||
734 p == PA_CHANNEL_POSITION_REAR_CENTER ||
735 p == PA_CHANNEL_POSITION_TOP_REAR_LEFT ||
736 p == PA_CHANNEL_POSITION_TOP_REAR_RIGHT ||
737 p == PA_CHANNEL_POSITION_TOP_REAR_CENTER;
740 static bool on_side(pa_channel_position_t p) {
742 p == PA_CHANNEL_POSITION_SIDE_LEFT ||
743 p == PA_CHANNEL_POSITION_SIDE_RIGHT ||
744 p == PA_CHANNEL_POSITION_TOP_CENTER;
754 static int front_rear_side(pa_channel_position_t p) {
764 static void calc_map_table(pa_resampler *r) {
767 bool ic_connected[PA_CHANNELS_MAX];
775 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)))))
780 n_oc = r->o_ss.channels;
781 n_ic = r->i_ss.channels;
783 memset(m->map_table_f, 0, sizeof(m->map_table_f));
784 memset(m->map_table_i, 0, sizeof(m->map_table_i));
786 memset(ic_connected, 0, sizeof(ic_connected));
787 remix = (r->flags & (PA_RESAMPLER_NO_REMAP | PA_RESAMPLER_NO_REMIX)) == 0;
789 if (r->flags & PA_RESAMPLER_NO_REMAP) {
792 for (oc = 0; oc < PA_MIN(n_ic, n_oc); oc++)
793 m->map_table_f[oc][oc] = 1.0f;
795 } else if (r->flags & PA_RESAMPLER_NO_REMIX) {
797 for (oc = 0; oc < n_oc; oc++) {
798 pa_channel_position_t b = r->o_cm.map[oc];
800 for (ic = 0; ic < n_ic; ic++) {
801 pa_channel_position_t a = r->i_cm.map[ic];
803 /* We shall not do any remixing. Hence, just check by name */
805 m->map_table_f[oc][ic] = 1.0f;
810 /* OK, we shall do the full monty: upmixing and downmixing. Our
811 * algorithm is relatively simple, does not do spacialization, delay
812 * elements or apply lowpass filters for LFE. Patches are always
813 * welcome, though. Oh, and it doesn't do any matrix decoding. (Which
814 * probably wouldn't make any sense anyway.)
816 * This code is not idempotent: downmixing an upmixed stereo stream is
817 * not identical to the original. The volume will not match, and the
818 * two channels will be a linear combination of both.
820 * This is loosely based on random suggestions found on the Internet,
822 * http://www.halfgaar.net/surround-sound-in-linux and the alsa upmix
825 * The algorithm works basically like this:
827 * 1) Connect all channels with matching names.
830 * S:Mono: Copy into all D:channels
831 * D:Mono: Avg all S:channels
833 * 3) Mix D:Left, D:Right:
834 * D:Left: If not connected, avg all S:Left
835 * D:Right: If not connected, avg all S:Right
838 * If not connected, avg all S:Center
839 * If still not connected, avg all S:Left, S:Right
842 * If not connected, avg all S:*
844 * 6) Make sure S:Left/S:Right is used: S:Left/S:Right: If not
845 * connected, mix into all D:left and all D:right channels. Gain is
848 * 7) Make sure S:Center, S:LFE is used:
850 * S:Center, S:LFE: If not connected, mix into all D:left, all
851 * D:right, all D:center channels. Gain is 0.5 for center and 0.375
852 * for LFE. C-front is only mixed into L-front/R-front if available,
853 * otherwise into all L/R channels. Similarly for C-rear.
855 * 8) Normalize each row in the matrix such that the sum for each row is
856 * not larger than 1.0 in order to avoid clipping.
858 * S: and D: shall relate to the source resp. destination channels.
860 * Rationale: 1, 2 are probably obvious. For 3: this copies front to
861 * rear if needed. For 4: we try to find some suitable C source for C,
862 * if we don't find any, we avg L and R. For 5: LFE is mixed from all
863 * channels. For 6: the rear channels should not be dropped entirely,
864 * however have only minimal impact. For 7: movies usually encode
865 * speech on the center channel. Thus we have to make sure this channel
866 * is distributed to L and R if not available in the output. Also, LFE
867 * is used to achieve a greater dynamic range, and thus we should try
868 * to do our best to pass it to L+R.
875 ic_unconnected_left = 0,
876 ic_unconnected_right = 0,
877 ic_unconnected_center = 0,
878 ic_unconnected_lfe = 0;
879 bool ic_unconnected_center_mixed_in = 0;
883 for (ic = 0; ic < n_ic; ic++) {
884 if (on_left(r->i_cm.map[ic]))
886 if (on_right(r->i_cm.map[ic]))
888 if (on_center(r->i_cm.map[ic]))
892 for (oc = 0; oc < n_oc; oc++) {
893 bool oc_connected = false;
894 pa_channel_position_t b = r->o_cm.map[oc];
896 for (ic = 0; ic < n_ic; ic++) {
897 pa_channel_position_t a = r->i_cm.map[ic];
899 if (a == b || a == PA_CHANNEL_POSITION_MONO) {
900 m->map_table_f[oc][ic] = 1.0f;
903 ic_connected[ic] = true;
905 else if (b == PA_CHANNEL_POSITION_MONO) {
906 m->map_table_f[oc][ic] = 1.0f / (float) n_ic;
909 ic_connected[ic] = true;
914 /* Try to find matching input ports for this output port */
918 /* We are not connected and on the left side, let's
919 * average all left side input channels. */
922 for (ic = 0; ic < n_ic; ic++)
923 if (on_left(r->i_cm.map[ic])) {
924 m->map_table_f[oc][ic] = 1.0f / (float) ic_left;
925 ic_connected[ic] = true;
928 /* We ignore the case where there is no left input channel.
929 * Something is really wrong in this case anyway. */
931 } else if (on_right(b)) {
933 /* We are not connected and on the right side, let's
934 * average all right side input channels. */
937 for (ic = 0; ic < n_ic; ic++)
938 if (on_right(r->i_cm.map[ic])) {
939 m->map_table_f[oc][ic] = 1.0f / (float) ic_right;
940 ic_connected[ic] = true;
943 /* We ignore the case where there is no right input
944 * channel. Something is really wrong in this case anyway.
947 } else if (on_center(b)) {
951 /* We are not connected and at the center. Let's average
952 * all center input channels. */
954 for (ic = 0; ic < n_ic; ic++)
955 if (on_center(r->i_cm.map[ic])) {
956 m->map_table_f[oc][ic] = 1.0f / (float) ic_center;
957 ic_connected[ic] = true;
960 } else if (ic_left + ic_right > 0) {
962 /* Hmm, no center channel around, let's synthesize it
963 * by mixing L and R.*/
965 for (ic = 0; ic < n_ic; ic++)
966 if (on_left(r->i_cm.map[ic]) || on_right(r->i_cm.map[ic])) {
967 m->map_table_f[oc][ic] = 1.0f / (float) (ic_left + ic_right);
968 ic_connected[ic] = true;
972 /* We ignore the case where there is not even a left or
973 * right input channel. Something is really wrong in this
976 } else if (on_lfe(b) && !(r->flags & PA_RESAMPLER_NO_LFE)) {
978 /* We are not connected and an LFE. Let's average all
979 * channels for LFE. */
981 for (ic = 0; ic < n_ic; ic++)
982 m->map_table_f[oc][ic] = 1.0f / (float) n_ic;
984 /* Please note that a channel connected to LFE doesn't
985 * really count as connected. */
990 for (ic = 0; ic < n_ic; ic++) {
991 pa_channel_position_t a = r->i_cm.map[ic];
993 if (ic_connected[ic])
997 ic_unconnected_left++;
998 else if (on_right(a))
999 ic_unconnected_right++;
1000 else if (on_center(a))
1001 ic_unconnected_center++;
1003 ic_unconnected_lfe++;
1006 for (ic = 0; ic < n_ic; ic++) {
1007 pa_channel_position_t a = r->i_cm.map[ic];
1009 if (ic_connected[ic])
1012 for (oc = 0; oc < n_oc; oc++) {
1013 pa_channel_position_t b = r->o_cm.map[oc];
1015 if (on_left(a) && on_left(b))
1016 m->map_table_f[oc][ic] = (1.f/9.f) / (float) ic_unconnected_left;
1018 else if (on_right(a) && on_right(b))
1019 m->map_table_f[oc][ic] = (1.f/9.f) / (float) ic_unconnected_right;
1021 else if (on_center(a) && on_center(b)) {
1022 m->map_table_f[oc][ic] = (1.f/9.f) / (float) ic_unconnected_center;
1023 ic_unconnected_center_mixed_in = true;
1025 } else if (on_lfe(a) && !(r->flags & PA_RESAMPLER_NO_LFE))
1026 m->map_table_f[oc][ic] = .375f / (float) ic_unconnected_lfe;
1030 if (ic_unconnected_center > 0 && !ic_unconnected_center_mixed_in) {
1031 unsigned ncenter[PA_CHANNELS_MAX];
1032 bool found_frs[PA_CHANNELS_MAX];
1034 memset(ncenter, 0, sizeof(ncenter));
1035 memset(found_frs, 0, sizeof(found_frs));
1037 /* Hmm, as it appears there was no center channel we
1038 could mix our center channel in. In this case, mix it into
1039 left and right. Using .5 as the factor. */
1041 for (ic = 0; ic < n_ic; ic++) {
1043 if (ic_connected[ic])
1046 if (!on_center(r->i_cm.map[ic]))
1049 for (oc = 0; oc < n_oc; oc++) {
1051 if (!on_left(r->o_cm.map[oc]) && !on_right(r->o_cm.map[oc]))
1054 if (front_rear_side(r->i_cm.map[ic]) == front_rear_side(r->o_cm.map[oc])) {
1055 found_frs[ic] = true;
1060 for (oc = 0; oc < n_oc; oc++) {
1062 if (!on_left(r->o_cm.map[oc]) && !on_right(r->o_cm.map[oc]))
1065 if (!found_frs[ic] || front_rear_side(r->i_cm.map[ic]) == front_rear_side(r->o_cm.map[oc]))
1070 for (oc = 0; oc < n_oc; oc++) {
1072 if (!on_left(r->o_cm.map[oc]) && !on_right(r->o_cm.map[oc]))
1075 if (ncenter[oc] <= 0)
1078 for (ic = 0; ic < n_ic; ic++) {
1080 if (!on_center(r->i_cm.map[ic]))
1083 if (!found_frs[ic] || front_rear_side(r->i_cm.map[ic]) == front_rear_side(r->o_cm.map[oc]))
1084 m->map_table_f[oc][ic] = .5f / (float) ncenter[oc];
1090 for (oc = 0; oc < n_oc; oc++) {
1092 for (ic = 0; ic < n_ic; ic++)
1093 sum += m->map_table_f[oc][ic];
1096 for (ic = 0; ic < n_ic; ic++)
1097 m->map_table_f[oc][ic] /= sum;
1100 /* make an 16:16 int version of the matrix */
1101 for (oc = 0; oc < n_oc; oc++)
1102 for (ic = 0; ic < n_ic; ic++)
1103 m->map_table_i[oc][ic] = (int32_t) (m->map_table_f[oc][ic] * 0x10000);
1105 s = pa_strbuf_new();
1107 pa_strbuf_printf(s, " ");
1108 for (ic = 0; ic < n_ic; ic++)
1109 pa_strbuf_printf(s, " I%02u ", ic);
1110 pa_strbuf_puts(s, "\n +");
1112 for (ic = 0; ic < n_ic; ic++)
1113 pa_strbuf_printf(s, "------");
1114 pa_strbuf_puts(s, "\n");
1116 for (oc = 0; oc < n_oc; oc++) {
1117 pa_strbuf_printf(s, "O%02u |", oc);
1119 for (ic = 0; ic < n_ic; ic++)
1120 pa_strbuf_printf(s, " %1.3f", m->map_table_f[oc][ic]);
1122 pa_strbuf_puts(s, "\n");
1125 pa_log_debug("Channel matrix:\n%s", t = pa_strbuf_tostring_free(s));
1128 /* initialize the remapping function */
1132 static pa_memchunk* convert_to_work_format(pa_resampler *r, pa_memchunk *input) {
1138 pa_assert(input->memblock);
1140 /* Convert the incoming sample into the work sample format and place them
1141 * in to_work_format_buf. */
1143 if (!r->to_work_format_func || !input->length)
1146 n_samples = (unsigned) ((input->length / r->i_fz) * r->i_ss.channels);
1148 r->to_work_format_buf.index = 0;
1149 r->to_work_format_buf.length = r->w_sz * n_samples;
1151 if (!r->to_work_format_buf.memblock || r->to_work_format_buf_samples < n_samples) {
1152 if (r->to_work_format_buf.memblock)
1153 pa_memblock_unref(r->to_work_format_buf.memblock);
1155 r->to_work_format_buf_samples = n_samples;
1156 r->to_work_format_buf.memblock = pa_memblock_new(r->mempool, r->to_work_format_buf.length);
1159 src = pa_memblock_acquire_chunk(input);
1160 dst = pa_memblock_acquire(r->to_work_format_buf.memblock);
1162 r->to_work_format_func(n_samples, src, dst);
1164 pa_memblock_release(input->memblock);
1165 pa_memblock_release(r->to_work_format_buf.memblock);
1167 return &r->to_work_format_buf;
1170 static pa_memchunk *remap_channels(pa_resampler *r, pa_memchunk *input) {
1171 unsigned in_n_samples, out_n_samples, in_n_frames, out_n_frames;
1173 size_t leftover_length = 0;
1178 pa_assert(input->memblock);
1180 /* Remap channels and place the result in remap_buf. There may be leftover
1181 * data in the beginning of remap_buf. The leftover data is already
1182 * remapped, so it's not part of the input, it's part of the output. */
1184 have_leftover = r->remap_buf_contains_leftover_data;
1185 r->remap_buf_contains_leftover_data = false;
1187 if (!have_leftover && (!r->map_required || input->length <= 0))
1189 else if (input->length <= 0)
1190 return &r->remap_buf;
1192 in_n_samples = (unsigned) (input->length / r->w_sz);
1193 in_n_frames = out_n_frames = in_n_samples / r->i_ss.channels;
1195 if (have_leftover) {
1196 leftover_length = r->remap_buf.length;
1197 out_n_frames += leftover_length / r->w_fz;
1200 out_n_samples = out_n_frames * r->o_ss.channels;
1201 r->remap_buf.length = out_n_samples * r->w_sz;
1203 if (have_leftover) {
1204 if (r->remap_buf_size < r->remap_buf.length) {
1205 pa_memblock *new_block = pa_memblock_new(r->mempool, r->remap_buf.length);
1207 src = pa_memblock_acquire(r->remap_buf.memblock);
1208 dst = pa_memblock_acquire(new_block);
1209 memcpy(dst, src, leftover_length);
1210 pa_memblock_release(r->remap_buf.memblock);
1211 pa_memblock_release(new_block);
1213 pa_memblock_unref(r->remap_buf.memblock);
1214 r->remap_buf.memblock = new_block;
1215 r->remap_buf_size = r->remap_buf.length;
1219 if (!r->remap_buf.memblock || r->remap_buf_size < r->remap_buf.length) {
1220 if (r->remap_buf.memblock)
1221 pa_memblock_unref(r->remap_buf.memblock);
1223 r->remap_buf_size = r->remap_buf.length;
1224 r->remap_buf.memblock = pa_memblock_new(r->mempool, r->remap_buf.length);
1228 src = pa_memblock_acquire_chunk(input);
1229 dst = (uint8_t *) pa_memblock_acquire(r->remap_buf.memblock) + leftover_length;
1231 if (r->map_required) {
1232 pa_remap_t *remap = &r->remap;
1234 pa_assert(remap->do_remap);
1235 remap->do_remap(remap, dst, src, in_n_frames);
1238 memcpy(dst, src, input->length);
1240 pa_memblock_release(input->memblock);
1241 pa_memblock_release(r->remap_buf.memblock);
1243 return &r->remap_buf;
1246 static pa_memchunk *resample(pa_resampler *r, pa_memchunk *input) {
1247 unsigned in_n_frames, in_n_samples;
1248 unsigned out_n_frames, out_n_samples;
1253 /* Resample the data and place the result in resample_buf. */
1255 if (!r->impl.resample || !input->length)
1258 in_n_samples = (unsigned) (input->length / r->w_sz);
1259 in_n_frames = (unsigned) (in_n_samples / r->work_channels);
1261 out_n_frames = ((in_n_frames*r->o_ss.rate)/r->i_ss.rate)+EXTRA_FRAMES;
1262 out_n_samples = out_n_frames * r->work_channels;
1264 r->resample_buf.index = 0;
1265 r->resample_buf.length = r->w_sz * out_n_samples;
1267 if (!r->resample_buf.memblock || r->resample_buf_samples < out_n_samples) {
1268 if (r->resample_buf.memblock)
1269 pa_memblock_unref(r->resample_buf.memblock);
1271 r->resample_buf_samples = out_n_samples;
1272 r->resample_buf.memblock = pa_memblock_new(r->mempool, r->resample_buf.length);
1275 r->impl.resample(r, input, in_n_frames, &r->resample_buf, &out_n_frames);
1276 r->resample_buf.length = out_n_frames * r->w_fz;
1278 return &r->resample_buf;
1281 static pa_memchunk *convert_from_work_format(pa_resampler *r, pa_memchunk *input) {
1282 unsigned n_samples, n_frames;
1288 /* Convert the data into the correct sample type and place the result in
1289 * from_work_format_buf. */
1291 if (!r->from_work_format_func || !input->length)
1294 n_samples = (unsigned) (input->length / r->w_sz);
1295 n_frames = n_samples / r->o_ss.channels;
1297 r->from_work_format_buf.index = 0;
1298 r->from_work_format_buf.length = r->o_fz * n_frames;
1300 if (!r->from_work_format_buf.memblock || r->from_work_format_buf_samples < n_samples) {
1301 if (r->from_work_format_buf.memblock)
1302 pa_memblock_unref(r->from_work_format_buf.memblock);
1304 r->from_work_format_buf_samples = n_samples;
1305 r->from_work_format_buf.memblock = pa_memblock_new(r->mempool, r->from_work_format_buf.length);
1308 src = pa_memblock_acquire_chunk(input);
1309 dst = pa_memblock_acquire(r->from_work_format_buf.memblock);
1310 r->from_work_format_func(n_samples, src, dst);
1311 pa_memblock_release(input->memblock);
1312 pa_memblock_release(r->from_work_format_buf.memblock);
1314 return &r->from_work_format_buf;
1317 void pa_resampler_run(pa_resampler *r, const pa_memchunk *in, pa_memchunk *out) {
1323 pa_assert(in->length);
1324 pa_assert(in->memblock);
1325 pa_assert(in->length % r->i_fz == 0);
1327 buf = (pa_memchunk*) in;
1328 buf = convert_to_work_format(r, buf);
1329 buf = remap_channels(r, buf);
1330 buf = resample(r, buf);
1333 buf = convert_from_work_format(r, buf);
1337 pa_memblock_ref(buf->memblock);
1339 pa_memchunk_reset(buf);
1341 pa_memchunk_reset(out);
1344 static void save_leftover(pa_resampler *r, void *buf, size_t len) {
1351 /* Store the leftover to remap_buf. */
1353 r->remap_buf.length = len;
1355 if (!r->remap_buf.memblock || r->remap_buf_size < r->remap_buf.length) {
1356 if (r->remap_buf.memblock)
1357 pa_memblock_unref(r->remap_buf.memblock);
1359 r->remap_buf_size = r->remap_buf.length;
1360 r->remap_buf.memblock = pa_memblock_new(r->mempool, r->remap_buf.length);
1363 dst = pa_memblock_acquire(r->remap_buf.memblock);
1364 memcpy(dst, buf, r->remap_buf.length);
1365 pa_memblock_release(r->remap_buf.memblock);
1367 r->remap_buf_contains_leftover_data = true;
1370 /*** libsamplerate based implementation ***/
1372 #ifdef HAVE_LIBSAMPLERATE
1373 static void libsamplerate_resample(pa_resampler *r, const pa_memchunk *input, unsigned in_n_frames, pa_memchunk *output, unsigned *out_n_frames) {
1380 pa_assert(out_n_frames);
1382 state = r->impl.data;
1383 memset(&data, 0, sizeof(data));
1385 data.data_in = pa_memblock_acquire_chunk(input);
1386 data.input_frames = (long int) in_n_frames;
1388 data.data_out = pa_memblock_acquire_chunk(output);
1389 data.output_frames = (long int) *out_n_frames;
1391 data.src_ratio = (double) r->o_ss.rate / r->i_ss.rate;
1392 data.end_of_input = 0;
1394 pa_assert_se(src_process(state, &data) == 0);
1396 if (data.input_frames_used < in_n_frames) {
1397 void *leftover_data = data.data_in + data.input_frames_used * r->work_channels;
1398 size_t leftover_length = (in_n_frames - data.input_frames_used) * r->w_fz;
1400 save_leftover(r, leftover_data, leftover_length);
1403 pa_memblock_release(input->memblock);
1404 pa_memblock_release(output->memblock);
1406 *out_n_frames = (unsigned) data.output_frames_gen;
1409 static void libsamplerate_update_rates(pa_resampler *r) {
1413 state = r->impl.data;
1414 pa_assert_se(src_set_ratio(state, (double) r->o_ss.rate / r->i_ss.rate) == 0);
1417 static void libsamplerate_reset(pa_resampler *r) {
1421 state = r->impl.data;
1422 pa_assert_se(src_reset(state) == 0);
1425 static void libsamplerate_free(pa_resampler *r) {
1429 state = r->impl.data;
1434 static int libsamplerate_init(pa_resampler *r) {
1440 if (!(state = src_new(r->method, r->work_channels, &err)))
1443 r->impl.free = libsamplerate_free;
1444 r->impl.update_rates = libsamplerate_update_rates;
1445 r->impl.resample = libsamplerate_resample;
1446 r->impl.reset = libsamplerate_reset;
1447 r->impl.data = state;
1454 /*** speex based implementation ***/
1456 static void speex_resample_float(pa_resampler *r, const pa_memchunk *input, unsigned in_n_frames, pa_memchunk *output, unsigned *out_n_frames) {
1458 uint32_t inf = in_n_frames, outf = *out_n_frames;
1459 SpeexResamplerState *state;
1464 pa_assert(out_n_frames);
1466 state = r->impl.data;
1468 in = pa_memblock_acquire_chunk(input);
1469 out = pa_memblock_acquire_chunk(output);
1471 pa_assert_se(speex_resampler_process_interleaved_float(state, in, &inf, out, &outf) == 0);
1473 pa_memblock_release(input->memblock);
1474 pa_memblock_release(output->memblock);
1476 pa_assert(inf == in_n_frames);
1477 *out_n_frames = outf;
1480 static void speex_resample_int(pa_resampler *r, const pa_memchunk *input, unsigned in_n_frames, pa_memchunk *output, unsigned *out_n_frames) {
1482 uint32_t inf = in_n_frames, outf = *out_n_frames;
1483 SpeexResamplerState *state;
1488 pa_assert(out_n_frames);
1490 state = r->impl.data;
1492 in = pa_memblock_acquire_chunk(input);
1493 out = pa_memblock_acquire_chunk(output);
1495 pa_assert_se(speex_resampler_process_interleaved_int(state, in, &inf, out, &outf) == 0);
1497 pa_memblock_release(input->memblock);
1498 pa_memblock_release(output->memblock);
1500 pa_assert(inf == in_n_frames);
1501 *out_n_frames = outf;
1504 static void speex_update_rates(pa_resampler *r) {
1505 SpeexResamplerState *state;
1508 state = r->impl.data;
1510 pa_assert_se(speex_resampler_set_rate(state, r->i_ss.rate, r->o_ss.rate) == 0);
1513 static void speex_reset(pa_resampler *r) {
1514 SpeexResamplerState *state;
1517 state = r->impl.data;
1519 pa_assert_se(speex_resampler_reset_mem(state) == 0);
1522 static void speex_free(pa_resampler *r) {
1523 SpeexResamplerState *state;
1526 state = r->impl.data;
1530 speex_resampler_destroy(state);
1533 static int speex_init(pa_resampler *r) {
1535 SpeexResamplerState *state;
1539 r->impl.free = speex_free;
1540 r->impl.update_rates = speex_update_rates;
1541 r->impl.reset = speex_reset;
1543 if (r->method >= PA_RESAMPLER_SPEEX_FIXED_BASE && r->method <= PA_RESAMPLER_SPEEX_FIXED_MAX) {
1545 q = r->method - PA_RESAMPLER_SPEEX_FIXED_BASE;
1546 r->impl.resample = speex_resample_int;
1549 pa_assert(r->method >= PA_RESAMPLER_SPEEX_FLOAT_BASE && r->method <= PA_RESAMPLER_SPEEX_FLOAT_MAX);
1551 q = r->method - PA_RESAMPLER_SPEEX_FLOAT_BASE;
1552 r->impl.resample = speex_resample_float;
1555 pa_log_info("Choosing speex quality setting %i.", q);
1557 if (!(state = speex_resampler_init(r->work_channels, r->i_ss.rate, r->o_ss.rate, q, &err)))
1560 r->impl.data = state;
1566 /* Trivial implementation */
1568 static void trivial_resample(pa_resampler *r, const pa_memchunk *input, unsigned in_n_frames, pa_memchunk *output, unsigned *out_n_frames) {
1569 unsigned i_index, o_index;
1571 struct trivial_data *trivial_data;
1576 pa_assert(out_n_frames);
1578 trivial_data = r->impl.data;
1580 src = pa_memblock_acquire_chunk(input);
1581 dst = pa_memblock_acquire_chunk(output);
1583 for (o_index = 0;; o_index++, trivial_data->o_counter++) {
1584 i_index = ((uint64_t) trivial_data->o_counter * r->i_ss.rate) / r->o_ss.rate;
1585 i_index = i_index > trivial_data->i_counter ? i_index - trivial_data->i_counter : 0;
1587 if (i_index >= in_n_frames)
1590 pa_assert_fp(o_index * r->w_fz < pa_memblock_get_length(output->memblock));
1592 memcpy((uint8_t*) dst + r->w_fz * o_index, (uint8_t*) src + r->w_fz * i_index, (int) r->w_fz);
1595 pa_memblock_release(input->memblock);
1596 pa_memblock_release(output->memblock);
1598 *out_n_frames = o_index;
1600 trivial_data->i_counter += in_n_frames;
1602 /* Normalize counters */
1603 while (trivial_data->i_counter >= r->i_ss.rate) {
1604 pa_assert(trivial_data->o_counter >= r->o_ss.rate);
1606 trivial_data->i_counter -= r->i_ss.rate;
1607 trivial_data->o_counter -= r->o_ss.rate;
1611 static void trivial_update_rates_or_reset(pa_resampler *r) {
1612 struct trivial_data *trivial_data;
1615 trivial_data = r->impl.data;
1617 trivial_data->i_counter = 0;
1618 trivial_data->o_counter = 0;
1621 static int trivial_init(pa_resampler*r) {
1622 struct trivial_data *trivial_data;
1625 trivial_data = pa_xnew0(struct trivial_data, 1);
1627 r->impl.resample = trivial_resample;
1628 r->impl.update_rates = trivial_update_rates_or_reset;
1629 r->impl.reset = trivial_update_rates_or_reset;
1630 r->impl.data = trivial_data;
1635 /* Peak finder implementation */
1637 static void peaks_resample(pa_resampler *r, const pa_memchunk *input, unsigned in_n_frames, pa_memchunk *output, unsigned *out_n_frames) {
1638 unsigned c, o_index = 0;
1639 unsigned i, i_end = 0;
1641 struct peaks_data *peaks_data;
1646 pa_assert(out_n_frames);
1648 peaks_data = r->impl.data;
1649 src = pa_memblock_acquire_chunk(input);
1650 dst = pa_memblock_acquire_chunk(output);
1652 i = ((uint64_t) peaks_data->o_counter * r->i_ss.rate) / r->o_ss.rate;
1653 i = i > peaks_data->i_counter ? i - peaks_data->i_counter : 0;
1655 while (i_end < in_n_frames) {
1656 i_end = ((uint64_t) (peaks_data->o_counter + 1) * r->i_ss.rate) / r->o_ss.rate;
1657 i_end = i_end > peaks_data->i_counter ? i_end - peaks_data->i_counter : 0;
1659 pa_assert_fp(o_index * r->w_fz < pa_memblock_get_length(output->memblock));
1661 /* 1ch float is treated separately, because that is the common case */
1662 if (r->work_channels == 1 && r->work_format == PA_SAMPLE_FLOAT32NE) {
1663 float *s = (float*) src + i;
1664 float *d = (float*) dst + o_index;
1666 for (; i < i_end && i < in_n_frames; i++) {
1667 float n = fabsf(*s++);
1669 if (n > peaks_data->max_f[0])
1670 peaks_data->max_f[0] = n;
1674 *d = peaks_data->max_f[0];
1675 peaks_data->max_f[0] = 0;
1676 o_index++, peaks_data->o_counter++;
1678 } else if (r->work_format == PA_SAMPLE_S16NE) {
1679 int16_t *s = (int16_t*) src + r->work_channels * i;
1680 int16_t *d = (int16_t*) dst + r->work_channels * o_index;
1682 for (; i < i_end && i < in_n_frames; i++)
1683 for (c = 0; c < r->work_channels; c++) {
1684 int16_t n = abs(*s++);
1686 if (n > peaks_data->max_i[c])
1687 peaks_data->max_i[c] = n;
1691 for (c = 0; c < r->work_channels; c++, d++) {
1692 *d = peaks_data->max_i[c];
1693 peaks_data->max_i[c] = 0;
1695 o_index++, peaks_data->o_counter++;
1698 float *s = (float*) src + r->work_channels * i;
1699 float *d = (float*) dst + r->work_channels * o_index;
1701 for (; i < i_end && i < in_n_frames; i++)
1702 for (c = 0; c < r->work_channels; c++) {
1703 float n = fabsf(*s++);
1705 if (n > peaks_data->max_f[c])
1706 peaks_data->max_f[c] = n;
1710 for (c = 0; c < r->work_channels; c++, d++) {
1711 *d = peaks_data->max_f[c];
1712 peaks_data->max_f[c] = 0;
1714 o_index++, peaks_data->o_counter++;
1719 pa_memblock_release(input->memblock);
1720 pa_memblock_release(output->memblock);
1722 *out_n_frames = o_index;
1724 peaks_data->i_counter += in_n_frames;
1726 /* Normalize counters */
1727 while (peaks_data->i_counter >= r->i_ss.rate) {
1728 pa_assert(peaks_data->o_counter >= r->o_ss.rate);
1730 peaks_data->i_counter -= r->i_ss.rate;
1731 peaks_data->o_counter -= r->o_ss.rate;
1735 static void peaks_update_rates_or_reset(pa_resampler *r) {
1736 struct peaks_data *peaks_data;
1739 peaks_data = r->impl.data;
1741 peaks_data->i_counter = 0;
1742 peaks_data->o_counter = 0;
1745 static int peaks_init(pa_resampler*r) {
1746 struct peaks_data *peaks_data;
1748 pa_assert(r->i_ss.rate >= r->o_ss.rate);
1749 pa_assert(r->work_format == PA_SAMPLE_S16NE || r->work_format == PA_SAMPLE_FLOAT32NE);
1751 peaks_data = pa_xnew0(struct peaks_data, 1);
1753 r->impl.resample = peaks_resample;
1754 r->impl.update_rates = peaks_update_rates_or_reset;
1755 r->impl.reset = peaks_update_rates_or_reset;
1756 r->impl.data = peaks_data;
1761 /*** ffmpeg based implementation ***/
1763 static void ffmpeg_resample(pa_resampler *r, const pa_memchunk *input, unsigned in_n_frames, pa_memchunk *output, unsigned *out_n_frames) {
1764 unsigned used_frames = 0, c;
1765 int previous_consumed_frames = -1;
1766 struct ffmpeg_data *ffmpeg_data;
1771 pa_assert(out_n_frames);
1773 ffmpeg_data = r->impl.data;
1775 for (c = 0; c < r->work_channels; c++) {
1778 int16_t *p, *t, *k, *q, *s;
1779 int consumed_frames;
1781 /* Allocate a new block */
1782 b = pa_memblock_new(r->mempool, ffmpeg_data->buf[c].length + in_n_frames * sizeof(int16_t));
1783 p = pa_memblock_acquire(b);
1785 /* Now copy the input data, splitting up channels */
1786 t = (int16_t*) pa_memblock_acquire_chunk(input) + c;
1788 for (u = 0; u < in_n_frames; u++) {
1790 t += r->work_channels;
1793 pa_memblock_release(input->memblock);
1795 /* Allocate buffer for the result */
1796 w = pa_memblock_new(r->mempool, *out_n_frames * sizeof(int16_t));
1797 q = pa_memblock_acquire(w);
1800 used_frames = (unsigned) av_resample(ffmpeg_data->state,
1803 (int) in_n_frames, (int) *out_n_frames,
1804 c >= (unsigned) (r->work_channels-1));
1806 pa_memblock_release(b);
1807 pa_memblock_unref(b);
1809 pa_assert(consumed_frames <= (int) in_n_frames);
1810 pa_assert(previous_consumed_frames == -1 || consumed_frames == previous_consumed_frames);
1811 previous_consumed_frames = consumed_frames;
1813 /* And place the results in the output buffer */
1814 s = (int16_t *) pa_memblock_acquire_chunk(output) + c;
1815 for (u = 0; u < used_frames; u++) {
1818 s += r->work_channels;
1820 pa_memblock_release(output->memblock);
1821 pa_memblock_release(w);
1822 pa_memblock_unref(w);
1825 if (previous_consumed_frames < (int) in_n_frames) {
1826 void *leftover_data = (int16_t *) pa_memblock_acquire_chunk(input) + previous_consumed_frames * r->work_channels;
1827 size_t leftover_length = (in_n_frames - previous_consumed_frames) * r->w_fz;
1829 save_leftover(r, leftover_data, leftover_length);
1830 pa_memblock_release(input->memblock);
1833 *out_n_frames = used_frames;
1836 static void ffmpeg_free(pa_resampler *r) {
1838 struct ffmpeg_data *ffmpeg_data;
1842 ffmpeg_data = r->impl.data;
1843 if (ffmpeg_data->state)
1844 av_resample_close(ffmpeg_data->state);
1846 for (c = 0; c < PA_ELEMENTSOF(ffmpeg_data->buf); c++)
1847 if (ffmpeg_data->buf[c].memblock)
1848 pa_memblock_unref(ffmpeg_data->buf[c].memblock);
1851 static int ffmpeg_init(pa_resampler *r) {
1853 struct ffmpeg_data *ffmpeg_data;
1857 ffmpeg_data = pa_xnew(struct ffmpeg_data, 1);
1859 /* We could probably implement different quality levels by
1860 * adjusting the filter parameters here. However, ffmpeg
1861 * internally only uses these hardcoded values, so let's use them
1862 * here for now as well until ffmpeg makes this configurable. */
1864 if (!(ffmpeg_data->state = av_resample_init((int) r->o_ss.rate, (int) r->i_ss.rate, 16, 10, 0, 0.8)))
1867 r->impl.free = ffmpeg_free;
1868 r->impl.resample = ffmpeg_resample;
1869 r->impl.data = (void *) ffmpeg_data;
1871 for (c = 0; c < PA_ELEMENTSOF(ffmpeg_data->buf); c++)
1872 pa_memchunk_reset(&ffmpeg_data->buf[c]);
1877 /*** copy (noop) implementation ***/
1879 static int copy_init(pa_resampler *r) {
1882 pa_assert(r->o_ss.rate == r->i_ss.rate);