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>
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_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 pa_bool_t remap_buf_contains_leftover_data;
69 pa_sample_format_t work_format;
71 pa_convert_func_t to_work_format_func;
72 pa_convert_func_t from_work_format_func;
75 pa_bool_t map_required;
77 void (*impl_free)(pa_resampler *r);
78 void (*impl_update_rates)(pa_resampler *r);
79 void (*impl_resample)(pa_resampler *r, const pa_memchunk *in, unsigned in_samples, pa_memchunk *out, unsigned *out_samples);
80 void (*impl_reset)(pa_resampler *r);
82 struct { /* data specific to the trivial resampler */
87 struct { /* data specific to the peak finder pseudo resampler */
91 float max_f[PA_CHANNELS_MAX];
92 int16_t max_i[PA_CHANNELS_MAX];
96 #ifdef HAVE_LIBSAMPLERATE
97 struct { /* data specific to libsamplerate */
103 struct { /* data specific to speex */
104 SpeexResamplerState* state;
108 struct { /* data specific to ffmpeg */
109 struct AVResampleContext *state;
110 pa_memchunk buf[PA_CHANNELS_MAX];
114 static int copy_init(pa_resampler *r);
115 static int trivial_init(pa_resampler*r);
117 static int speex_init(pa_resampler*r);
119 static int ffmpeg_init(pa_resampler*r);
120 static int peaks_init(pa_resampler*r);
121 #ifdef HAVE_LIBSAMPLERATE
122 static int libsamplerate_init(pa_resampler*r);
125 static void calc_map_table(pa_resampler *r);
127 static int (* const init_table[])(pa_resampler*r) = {
128 #ifdef HAVE_LIBSAMPLERATE
129 [PA_RESAMPLER_SRC_SINC_BEST_QUALITY] = libsamplerate_init,
130 [PA_RESAMPLER_SRC_SINC_MEDIUM_QUALITY] = libsamplerate_init,
131 [PA_RESAMPLER_SRC_SINC_FASTEST] = libsamplerate_init,
132 [PA_RESAMPLER_SRC_ZERO_ORDER_HOLD] = libsamplerate_init,
133 [PA_RESAMPLER_SRC_LINEAR] = libsamplerate_init,
135 [PA_RESAMPLER_SRC_SINC_BEST_QUALITY] = NULL,
136 [PA_RESAMPLER_SRC_SINC_MEDIUM_QUALITY] = NULL,
137 [PA_RESAMPLER_SRC_SINC_FASTEST] = NULL,
138 [PA_RESAMPLER_SRC_ZERO_ORDER_HOLD] = NULL,
139 [PA_RESAMPLER_SRC_LINEAR] = NULL,
141 [PA_RESAMPLER_TRIVIAL] = trivial_init,
143 [PA_RESAMPLER_SPEEX_FLOAT_BASE+0] = speex_init,
144 [PA_RESAMPLER_SPEEX_FLOAT_BASE+1] = speex_init,
145 [PA_RESAMPLER_SPEEX_FLOAT_BASE+2] = speex_init,
146 [PA_RESAMPLER_SPEEX_FLOAT_BASE+3] = speex_init,
147 [PA_RESAMPLER_SPEEX_FLOAT_BASE+4] = speex_init,
148 [PA_RESAMPLER_SPEEX_FLOAT_BASE+5] = speex_init,
149 [PA_RESAMPLER_SPEEX_FLOAT_BASE+6] = speex_init,
150 [PA_RESAMPLER_SPEEX_FLOAT_BASE+7] = speex_init,
151 [PA_RESAMPLER_SPEEX_FLOAT_BASE+8] = speex_init,
152 [PA_RESAMPLER_SPEEX_FLOAT_BASE+9] = speex_init,
153 [PA_RESAMPLER_SPEEX_FLOAT_BASE+10] = speex_init,
154 [PA_RESAMPLER_SPEEX_FIXED_BASE+0] = speex_init,
155 [PA_RESAMPLER_SPEEX_FIXED_BASE+1] = speex_init,
156 [PA_RESAMPLER_SPEEX_FIXED_BASE+2] = speex_init,
157 [PA_RESAMPLER_SPEEX_FIXED_BASE+3] = speex_init,
158 [PA_RESAMPLER_SPEEX_FIXED_BASE+4] = speex_init,
159 [PA_RESAMPLER_SPEEX_FIXED_BASE+5] = speex_init,
160 [PA_RESAMPLER_SPEEX_FIXED_BASE+6] = speex_init,
161 [PA_RESAMPLER_SPEEX_FIXED_BASE+7] = speex_init,
162 [PA_RESAMPLER_SPEEX_FIXED_BASE+8] = speex_init,
163 [PA_RESAMPLER_SPEEX_FIXED_BASE+9] = speex_init,
164 [PA_RESAMPLER_SPEEX_FIXED_BASE+10] = speex_init,
166 [PA_RESAMPLER_SPEEX_FLOAT_BASE+0] = NULL,
167 [PA_RESAMPLER_SPEEX_FLOAT_BASE+1] = NULL,
168 [PA_RESAMPLER_SPEEX_FLOAT_BASE+2] = NULL,
169 [PA_RESAMPLER_SPEEX_FLOAT_BASE+3] = NULL,
170 [PA_RESAMPLER_SPEEX_FLOAT_BASE+4] = NULL,
171 [PA_RESAMPLER_SPEEX_FLOAT_BASE+5] = NULL,
172 [PA_RESAMPLER_SPEEX_FLOAT_BASE+6] = NULL,
173 [PA_RESAMPLER_SPEEX_FLOAT_BASE+7] = NULL,
174 [PA_RESAMPLER_SPEEX_FLOAT_BASE+8] = NULL,
175 [PA_RESAMPLER_SPEEX_FLOAT_BASE+9] = NULL,
176 [PA_RESAMPLER_SPEEX_FLOAT_BASE+10] = NULL,
177 [PA_RESAMPLER_SPEEX_FIXED_BASE+0] = NULL,
178 [PA_RESAMPLER_SPEEX_FIXED_BASE+1] = NULL,
179 [PA_RESAMPLER_SPEEX_FIXED_BASE+2] = NULL,
180 [PA_RESAMPLER_SPEEX_FIXED_BASE+3] = NULL,
181 [PA_RESAMPLER_SPEEX_FIXED_BASE+4] = NULL,
182 [PA_RESAMPLER_SPEEX_FIXED_BASE+5] = NULL,
183 [PA_RESAMPLER_SPEEX_FIXED_BASE+6] = NULL,
184 [PA_RESAMPLER_SPEEX_FIXED_BASE+7] = NULL,
185 [PA_RESAMPLER_SPEEX_FIXED_BASE+8] = NULL,
186 [PA_RESAMPLER_SPEEX_FIXED_BASE+9] = NULL,
187 [PA_RESAMPLER_SPEEX_FIXED_BASE+10] = NULL,
189 [PA_RESAMPLER_FFMPEG] = ffmpeg_init,
190 [PA_RESAMPLER_AUTO] = NULL,
191 [PA_RESAMPLER_COPY] = copy_init,
192 [PA_RESAMPLER_PEAKS] = peaks_init,
195 pa_resampler* pa_resampler_new(
197 const pa_sample_spec *a,
198 const pa_channel_map *am,
199 const pa_sample_spec *b,
200 const pa_channel_map *bm,
201 pa_resample_method_t method,
202 pa_resample_flags_t flags) {
204 pa_resampler *r = NULL;
209 pa_assert(pa_sample_spec_valid(a));
210 pa_assert(pa_sample_spec_valid(b));
211 pa_assert(method >= 0);
212 pa_assert(method < PA_RESAMPLER_MAX);
216 if (!(flags & PA_RESAMPLER_VARIABLE_RATE) && a->rate == b->rate) {
217 pa_log_info("Forcing resampler 'copy', because of fixed, identical sample rates.");
218 method = PA_RESAMPLER_COPY;
221 if (!pa_resample_method_supported(method)) {
222 pa_log_warn("Support for resampler '%s' not compiled in, reverting to 'auto'.", pa_resample_method_to_string(method));
223 method = PA_RESAMPLER_AUTO;
226 if (method == PA_RESAMPLER_FFMPEG && (flags & PA_RESAMPLER_VARIABLE_RATE)) {
227 pa_log_info("Resampler 'ffmpeg' cannot do variable rate, reverting to resampler 'auto'.");
228 method = PA_RESAMPLER_AUTO;
231 if (method == PA_RESAMPLER_COPY && ((flags & PA_RESAMPLER_VARIABLE_RATE) || a->rate != b->rate)) {
232 pa_log_info("Resampler 'copy' cannot change sampling rate, reverting to resampler 'auto'.");
233 method = PA_RESAMPLER_AUTO;
236 if (method == PA_RESAMPLER_AUTO) {
238 method = PA_RESAMPLER_SPEEX_FLOAT_BASE + 3;
240 method = PA_RESAMPLER_FFMPEG;
244 r = pa_xnew0(pa_resampler, 1);
249 /* Fill sample specs */
253 /* set up the remap structure */
254 r->remap.i_ss = &r->i_ss;
255 r->remap.o_ss = &r->o_ss;
256 r->remap.format = &r->work_format;
260 else if (!pa_channel_map_init_auto(&r->i_cm, r->i_ss.channels, PA_CHANNEL_MAP_DEFAULT))
265 else if (!pa_channel_map_init_auto(&r->o_cm, r->o_ss.channels, PA_CHANNEL_MAP_DEFAULT))
268 r->i_fz = pa_frame_size(a);
269 r->o_fz = pa_frame_size(b);
273 pa_log_info("Using resampler '%s'", pa_resample_method_to_string(method));
275 if ((method >= PA_RESAMPLER_SPEEX_FIXED_BASE && method <= PA_RESAMPLER_SPEEX_FIXED_MAX) ||
276 (method == PA_RESAMPLER_FFMPEG))
277 r->work_format = PA_SAMPLE_S16NE;
278 else if (method == PA_RESAMPLER_TRIVIAL || method == PA_RESAMPLER_COPY || method == PA_RESAMPLER_PEAKS) {
280 if (r->map_required || a->format != b->format || method == PA_RESAMPLER_PEAKS) {
282 if (a->format == PA_SAMPLE_S32NE || a->format == PA_SAMPLE_S32RE ||
283 a->format == PA_SAMPLE_FLOAT32NE || a->format == PA_SAMPLE_FLOAT32RE ||
284 a->format == PA_SAMPLE_S24NE || a->format == PA_SAMPLE_S24RE ||
285 a->format == PA_SAMPLE_S24_32NE || a->format == PA_SAMPLE_S24_32RE ||
286 b->format == PA_SAMPLE_S32NE || b->format == PA_SAMPLE_S32RE ||
287 b->format == PA_SAMPLE_FLOAT32NE || b->format == PA_SAMPLE_FLOAT32RE ||
288 b->format == PA_SAMPLE_S24NE || b->format == PA_SAMPLE_S24RE ||
289 b->format == PA_SAMPLE_S24_32NE || b->format == PA_SAMPLE_S24_32RE)
290 r->work_format = PA_SAMPLE_FLOAT32NE;
292 r->work_format = PA_SAMPLE_S16NE;
295 r->work_format = a->format;
298 r->work_format = PA_SAMPLE_FLOAT32NE;
300 pa_log_info("Using %s as working format.", pa_sample_format_to_string(r->work_format));
302 r->w_sz = pa_sample_size_of_format(r->work_format);
304 if (r->i_ss.format != r->work_format) {
305 if (r->work_format == PA_SAMPLE_FLOAT32NE) {
306 if (!(r->to_work_format_func = pa_get_convert_to_float32ne_function(r->i_ss.format)))
309 pa_assert(r->work_format == PA_SAMPLE_S16NE);
310 if (!(r->to_work_format_func = pa_get_convert_to_s16ne_function(r->i_ss.format)))
315 if (r->o_ss.format != r->work_format) {
316 if (r->work_format == PA_SAMPLE_FLOAT32NE) {
317 if (!(r->from_work_format_func = pa_get_convert_from_float32ne_function(r->o_ss.format)))
320 pa_assert(r->work_format == PA_SAMPLE_S16NE);
321 if (!(r->from_work_format_func = pa_get_convert_from_s16ne_function(r->o_ss.format)))
326 /* initialize implementation */
327 if (init_table[method](r) < 0)
338 void pa_resampler_free(pa_resampler *r) {
344 if (r->to_work_format_buf.memblock)
345 pa_memblock_unref(r->to_work_format_buf.memblock);
346 if (r->remap_buf.memblock)
347 pa_memblock_unref(r->remap_buf.memblock);
348 if (r->resample_buf.memblock)
349 pa_memblock_unref(r->resample_buf.memblock);
350 if (r->from_work_format_buf.memblock)
351 pa_memblock_unref(r->from_work_format_buf.memblock);
356 void pa_resampler_set_input_rate(pa_resampler *r, uint32_t rate) {
360 if (r->i_ss.rate == rate)
365 r->impl_update_rates(r);
368 void pa_resampler_set_output_rate(pa_resampler *r, uint32_t rate) {
372 if (r->o_ss.rate == rate)
377 r->impl_update_rates(r);
380 size_t pa_resampler_request(pa_resampler *r, size_t out_length) {
383 /* Let's round up here to make it more likely that the caller will get at
384 * least out_length amount of data from pa_resampler_run().
386 * We don't take the leftover into account here. If we did, then it might
387 * be in theory possible that this function would return 0 and
388 * pa_resampler_run() would also return 0. That could lead to infinite
389 * loops. When the leftover is ignored here, such loops would eventually
390 * terminate, because the leftover would grow each round, finally
391 * surpassing the minimum input threshold of the resampler. */
392 return (((((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;
395 size_t pa_resampler_result(pa_resampler *r, size_t in_length) {
400 /* Let's round up here to ensure that the caller will always allocate big
401 * enough output buffer. */
403 frames = (in_length + r->i_fz - 1) / r->i_fz;
405 if (r->remap_buf_contains_leftover_data)
406 frames += r->remap_buf.length / (r->w_sz * r->o_ss.channels);
408 return ((frames * r->o_ss.rate + r->i_ss.rate - 1) / r->i_ss.rate) * r->o_fz;
411 size_t pa_resampler_max_block_size(pa_resampler *r) {
412 size_t block_size_max;
413 pa_sample_spec max_ss;
419 block_size_max = pa_mempool_block_size_max(r->mempool);
421 /* We deduce the "largest" sample spec we're using during the
423 max_ss.channels = (uint8_t) (PA_MAX(r->i_ss.channels, r->o_ss.channels));
425 /* We silently assume that the format enum is ordered by size */
426 max_ss.format = PA_MAX(r->i_ss.format, r->o_ss.format);
427 max_ss.format = PA_MAX(max_ss.format, r->work_format);
429 max_ss.rate = PA_MAX(r->i_ss.rate, r->o_ss.rate);
431 max_fs = pa_frame_size(&max_ss);
432 frames = block_size_max / max_fs - EXTRA_FRAMES;
434 if (r->remap_buf_contains_leftover_data)
435 frames -= r->remap_buf.length / (r->w_sz * r->o_ss.channels);
437 return (frames * r->i_ss.rate / max_ss.rate) * r->i_fz;
440 void pa_resampler_reset(pa_resampler *r) {
446 r->remap_buf_contains_leftover_data = FALSE;
449 pa_resample_method_t pa_resampler_get_method(pa_resampler *r) {
455 const pa_channel_map* pa_resampler_input_channel_map(pa_resampler *r) {
461 const pa_sample_spec* pa_resampler_input_sample_spec(pa_resampler *r) {
467 const pa_channel_map* pa_resampler_output_channel_map(pa_resampler *r) {
473 const pa_sample_spec* pa_resampler_output_sample_spec(pa_resampler *r) {
479 static const char * const resample_methods[] = {
480 "src-sinc-best-quality",
481 "src-sinc-medium-quality",
483 "src-zero-order-hold",
514 const char *pa_resample_method_to_string(pa_resample_method_t m) {
516 if (m < 0 || m >= PA_RESAMPLER_MAX)
519 return resample_methods[m];
522 int pa_resample_method_supported(pa_resample_method_t m) {
524 if (m < 0 || m >= PA_RESAMPLER_MAX)
527 #ifndef HAVE_LIBSAMPLERATE
528 if (m <= PA_RESAMPLER_SRC_LINEAR)
533 if (m >= PA_RESAMPLER_SPEEX_FLOAT_BASE && m <= PA_RESAMPLER_SPEEX_FLOAT_MAX)
535 if (m >= PA_RESAMPLER_SPEEX_FIXED_BASE && m <= PA_RESAMPLER_SPEEX_FIXED_MAX)
542 pa_resample_method_t pa_parse_resample_method(const char *string) {
543 pa_resample_method_t m;
547 for (m = 0; m < PA_RESAMPLER_MAX; m++)
548 if (!strcmp(string, resample_methods[m]))
551 if (!strcmp(string, "speex-fixed"))
552 return PA_RESAMPLER_SPEEX_FIXED_BASE + 3;
554 if (!strcmp(string, "speex-float"))
555 return PA_RESAMPLER_SPEEX_FLOAT_BASE + 3;
557 return PA_RESAMPLER_INVALID;
560 static pa_bool_t on_left(pa_channel_position_t p) {
563 p == PA_CHANNEL_POSITION_FRONT_LEFT ||
564 p == PA_CHANNEL_POSITION_REAR_LEFT ||
565 p == PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER ||
566 p == PA_CHANNEL_POSITION_SIDE_LEFT ||
567 p == PA_CHANNEL_POSITION_TOP_FRONT_LEFT ||
568 p == PA_CHANNEL_POSITION_TOP_REAR_LEFT;
571 static pa_bool_t on_right(pa_channel_position_t p) {
574 p == PA_CHANNEL_POSITION_FRONT_RIGHT ||
575 p == PA_CHANNEL_POSITION_REAR_RIGHT ||
576 p == PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER ||
577 p == PA_CHANNEL_POSITION_SIDE_RIGHT ||
578 p == PA_CHANNEL_POSITION_TOP_FRONT_RIGHT ||
579 p == PA_CHANNEL_POSITION_TOP_REAR_RIGHT;
582 static pa_bool_t on_center(pa_channel_position_t p) {
585 p == PA_CHANNEL_POSITION_FRONT_CENTER ||
586 p == PA_CHANNEL_POSITION_REAR_CENTER ||
587 p == PA_CHANNEL_POSITION_TOP_CENTER ||
588 p == PA_CHANNEL_POSITION_TOP_FRONT_CENTER ||
589 p == PA_CHANNEL_POSITION_TOP_REAR_CENTER;
592 static pa_bool_t on_lfe(pa_channel_position_t p) {
594 p == PA_CHANNEL_POSITION_LFE;
597 static pa_bool_t on_front(pa_channel_position_t p) {
599 p == PA_CHANNEL_POSITION_FRONT_LEFT ||
600 p == PA_CHANNEL_POSITION_FRONT_RIGHT ||
601 p == PA_CHANNEL_POSITION_FRONT_CENTER ||
602 p == PA_CHANNEL_POSITION_TOP_FRONT_LEFT ||
603 p == PA_CHANNEL_POSITION_TOP_FRONT_RIGHT ||
604 p == PA_CHANNEL_POSITION_TOP_FRONT_CENTER ||
605 p == PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER ||
606 p == PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER;
609 static pa_bool_t on_rear(pa_channel_position_t p) {
611 p == PA_CHANNEL_POSITION_REAR_LEFT ||
612 p == PA_CHANNEL_POSITION_REAR_RIGHT ||
613 p == PA_CHANNEL_POSITION_REAR_CENTER ||
614 p == PA_CHANNEL_POSITION_TOP_REAR_LEFT ||
615 p == PA_CHANNEL_POSITION_TOP_REAR_RIGHT ||
616 p == PA_CHANNEL_POSITION_TOP_REAR_CENTER;
619 static pa_bool_t on_side(pa_channel_position_t p) {
621 p == PA_CHANNEL_POSITION_SIDE_LEFT ||
622 p == PA_CHANNEL_POSITION_SIDE_RIGHT ||
623 p == PA_CHANNEL_POSITION_TOP_CENTER;
633 static int front_rear_side(pa_channel_position_t p) {
643 static void calc_map_table(pa_resampler *r) {
646 pa_bool_t ic_connected[PA_CHANNELS_MAX];
654 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)))))
659 n_oc = r->o_ss.channels;
660 n_ic = r->i_ss.channels;
662 memset(m->map_table_f, 0, sizeof(m->map_table_f));
663 memset(m->map_table_i, 0, sizeof(m->map_table_i));
665 memset(ic_connected, 0, sizeof(ic_connected));
666 remix = (r->flags & (PA_RESAMPLER_NO_REMAP|PA_RESAMPLER_NO_REMIX)) == 0;
668 for (oc = 0; oc < n_oc; oc++) {
669 pa_bool_t oc_connected = FALSE;
670 pa_channel_position_t b = r->o_cm.map[oc];
672 for (ic = 0; ic < n_ic; ic++) {
673 pa_channel_position_t a = r->i_cm.map[ic];
675 if (r->flags & PA_RESAMPLER_NO_REMAP) {
676 /* We shall not do any remapping. Hence, just check by index */
679 m->map_table_f[oc][ic] = 1.0;
684 if (r->flags & PA_RESAMPLER_NO_REMIX) {
685 /* We shall not do any remixing. Hence, just check by name */
688 m->map_table_f[oc][ic] = 1.0;
695 /* OK, we shall do the full monty: upmixing and
696 * downmixing. Our algorithm is relatively simple, does
697 * not do spacialization, delay elements or apply lowpass
698 * filters for LFE. Patches are always welcome,
699 * though. Oh, and it doesn't do any matrix
700 * decoding. (Which probably wouldn't make any sense
703 * This code is not idempotent: downmixing an upmixed
704 * stereo stream is not identical to the original. The
705 * volume will not match, and the two channels will be a
706 * linear combination of both.
708 * This is loosely based on random suggestions found on the
709 * Internet, such as this:
710 * http://www.halfgaar.net/surround-sound-in-linux and the
713 * The algorithm works basically like this:
715 * 1) Connect all channels with matching names.
718 * S:Mono: Copy into all D:channels
719 * D:Mono: Copy in all S:channels
721 * 3) Mix D:Left, D:Right:
722 * D:Left: If not connected, avg all S:Left
723 * D:Right: If not connected, avg all S:Right
726 * If not connected, avg all S:Center
727 * If still not connected, avg all S:Left, S:Right
730 * If not connected, avg all S:*
732 * 6) Make sure S:Left/S:Right is used: S:Left/S:Right: If
733 * not connected, mix into all D:left and all D:right
734 * channels. Gain is 0.1, the current left and right
735 * should be multiplied by 0.9.
737 * 7) Make sure S:Center, S:LFE is used:
739 * S:Center, S:LFE: If not connected, mix into all
740 * D:left, all D:right, all D:center channels, gain is
741 * 0.375. The current (as result of 1..6) factors
742 * should be multiplied by 0.75. (Alt. suggestion: 0.25
743 * vs. 0.5) If C-front is only mixed into
744 * L-front/R-front if available, otherwise into all L/R
745 * channels. Similarly for C-rear.
747 * S: and D: shall relate to the source resp. destination channels.
749 * Rationale: 1, 2 are probably obvious. For 3: this
750 * copies front to rear if needed. For 4: we try to find
751 * some suitable C source for C, if we don't find any, we
752 * avg L and R. For 5: LFE is mixed from all channels. For
753 * 6: the rear channels should not be dropped entirely,
754 * however have only minimal impact. For 7: movies usually
755 * encode speech on the center channel. Thus we have to
756 * make sure this channel is distributed to L and R if not
757 * available in the output. Also, LFE is used to achieve a
758 * greater dynamic range, and thus we should try to do our
759 * best to pass it to L+R.
762 if (a == b || a == PA_CHANNEL_POSITION_MONO || b == PA_CHANNEL_POSITION_MONO) {
763 m->map_table_f[oc][ic] = 1.0;
766 ic_connected[ic] = TRUE;
770 if (!oc_connected && remix) {
771 /* OK, we shall remix */
773 /* Try to find matching input ports for this output port */
778 /* We are not connected and on the left side, let's
779 * average all left side input channels. */
781 for (ic = 0; ic < n_ic; ic++)
782 if (on_left(r->i_cm.map[ic]))
786 for (ic = 0; ic < n_ic; ic++)
787 if (on_left(r->i_cm.map[ic])) {
788 m->map_table_f[oc][ic] = 1.0f / (float) n;
789 ic_connected[ic] = TRUE;
792 /* We ignore the case where there is no left input
793 * channel. Something is really wrong in this case
796 } else if (on_right(b)) {
799 /* We are not connected and on the right side, let's
800 * average all right side input channels. */
802 for (ic = 0; ic < n_ic; ic++)
803 if (on_right(r->i_cm.map[ic]))
807 for (ic = 0; ic < n_ic; ic++)
808 if (on_right(r->i_cm.map[ic])) {
809 m->map_table_f[oc][ic] = 1.0f / (float) n;
810 ic_connected[ic] = TRUE;
813 /* We ignore the case where there is no right input
814 * channel. Something is really wrong in this case
817 } else if (on_center(b)) {
820 /* We are not connected and at the center. Let's
821 * average all center input channels. */
823 for (ic = 0; ic < n_ic; ic++)
824 if (on_center(r->i_cm.map[ic]))
828 for (ic = 0; ic < n_ic; ic++)
829 if (on_center(r->i_cm.map[ic])) {
830 m->map_table_f[oc][ic] = 1.0f / (float) n;
831 ic_connected[ic] = TRUE;
835 /* Hmm, no center channel around, let's synthesize
836 * it by mixing L and R.*/
840 for (ic = 0; ic < n_ic; ic++)
841 if (on_left(r->i_cm.map[ic]) || on_right(r->i_cm.map[ic]))
845 for (ic = 0; ic < n_ic; ic++)
846 if (on_left(r->i_cm.map[ic]) || on_right(r->i_cm.map[ic])) {
847 m->map_table_f[oc][ic] = 1.0f / (float) n;
848 ic_connected[ic] = TRUE;
851 /* We ignore the case where there is not even a
852 * left or right input channel. Something is
853 * really wrong in this case anyway. */
856 } else if (on_lfe(b)) {
858 /* We are not connected and an LFE. Let's average all
859 * channels for LFE. */
861 for (ic = 0; ic < n_ic; ic++) {
863 if (!(r->flags & PA_RESAMPLER_NO_LFE))
864 m->map_table_f[oc][ic] = 1.0f / (float) n_ic;
866 m->map_table_f[oc][ic] = 0;
868 /* Please note that a channel connected to LFE
869 * doesn't really count as connected. */
877 ic_unconnected_left = 0,
878 ic_unconnected_right = 0,
879 ic_unconnected_center = 0,
880 ic_unconnected_lfe = 0;
882 for (ic = 0; ic < n_ic; ic++) {
883 pa_channel_position_t a = r->i_cm.map[ic];
885 if (ic_connected[ic])
889 ic_unconnected_left++;
890 else if (on_right(a))
891 ic_unconnected_right++;
892 else if (on_center(a))
893 ic_unconnected_center++;
895 ic_unconnected_lfe++;
898 if (ic_unconnected_left > 0) {
900 /* OK, so there are unconnected input channels on the
901 * left. Let's multiply all already connected channels on
902 * the left side by .9 and add in our averaged unconnected
903 * channels multiplied by .1 */
905 for (oc = 0; oc < n_oc; oc++) {
907 if (!on_left(r->o_cm.map[oc]))
910 for (ic = 0; ic < n_ic; ic++) {
912 if (ic_connected[ic]) {
913 m->map_table_f[oc][ic] *= .9f;
917 if (on_left(r->i_cm.map[ic]))
918 m->map_table_f[oc][ic] = .1f / (float) ic_unconnected_left;
923 if (ic_unconnected_right > 0) {
925 /* OK, so there are unconnected input channels on the
926 * right. Let's multiply all already connected channels on
927 * the right side by .9 and add in our averaged unconnected
928 * channels multiplied by .1 */
930 for (oc = 0; oc < n_oc; oc++) {
932 if (!on_right(r->o_cm.map[oc]))
935 for (ic = 0; ic < n_ic; ic++) {
937 if (ic_connected[ic]) {
938 m->map_table_f[oc][ic] *= .9f;
942 if (on_right(r->i_cm.map[ic]))
943 m->map_table_f[oc][ic] = .1f / (float) ic_unconnected_right;
948 if (ic_unconnected_center > 0) {
949 pa_bool_t mixed_in = FALSE;
951 /* OK, so there are unconnected input channels on the
952 * center. Let's multiply all already connected channels on
953 * the center side by .9 and add in our averaged unconnected
954 * channels multiplied by .1 */
956 for (oc = 0; oc < n_oc; oc++) {
958 if (!on_center(r->o_cm.map[oc]))
961 for (ic = 0; ic < n_ic; ic++) {
963 if (ic_connected[ic]) {
964 m->map_table_f[oc][ic] *= .9f;
968 if (on_center(r->i_cm.map[ic])) {
969 m->map_table_f[oc][ic] = .1f / (float) ic_unconnected_center;
976 unsigned ncenter[PA_CHANNELS_MAX];
977 pa_bool_t found_frs[PA_CHANNELS_MAX];
979 memset(ncenter, 0, sizeof(ncenter));
980 memset(found_frs, 0, sizeof(found_frs));
982 /* Hmm, as it appears there was no center channel we
983 could mix our center channel in. In this case, mix
984 it into left and right. Using .375 and 0.75 as
987 for (ic = 0; ic < n_ic; ic++) {
989 if (ic_connected[ic])
992 if (!on_center(r->i_cm.map[ic]))
995 for (oc = 0; oc < n_oc; oc++) {
997 if (!on_left(r->o_cm.map[oc]) && !on_right(r->o_cm.map[oc]))
1000 if (front_rear_side(r->i_cm.map[ic]) == front_rear_side(r->o_cm.map[oc])) {
1001 found_frs[ic] = TRUE;
1006 for (oc = 0; oc < n_oc; oc++) {
1008 if (!on_left(r->o_cm.map[oc]) && !on_right(r->o_cm.map[oc]))
1011 if (!found_frs[ic] || front_rear_side(r->i_cm.map[ic]) == front_rear_side(r->o_cm.map[oc]))
1016 for (oc = 0; oc < n_oc; oc++) {
1018 if (!on_left(r->o_cm.map[oc]) && !on_right(r->o_cm.map[oc]))
1021 if (ncenter[oc] <= 0)
1024 for (ic = 0; ic < n_ic; ic++) {
1026 if (ic_connected[ic]) {
1027 m->map_table_f[oc][ic] *= .75f;
1031 if (!on_center(r->i_cm.map[ic]))
1034 if (!found_frs[ic] || front_rear_side(r->i_cm.map[ic]) == front_rear_side(r->o_cm.map[oc]))
1035 m->map_table_f[oc][ic] = .375f / (float) ncenter[oc];
1041 if (ic_unconnected_lfe > 0 && !(r->flags & PA_RESAMPLER_NO_LFE)) {
1043 /* OK, so there is an unconnected LFE channel. Let's mix
1044 * it into all channels, with factor 0.375 */
1046 for (ic = 0; ic < n_ic; ic++) {
1048 if (!on_lfe(r->i_cm.map[ic]))
1051 for (oc = 0; oc < n_oc; oc++)
1052 m->map_table_f[oc][ic] = 0.375f / (float) ic_unconnected_lfe;
1056 /* make an 16:16 int version of the matrix */
1057 for (oc = 0; oc < n_oc; oc++)
1058 for (ic = 0; ic < n_ic; ic++)
1059 m->map_table_i[oc][ic] = (int32_t) (m->map_table_f[oc][ic] * 0x10000);
1061 s = pa_strbuf_new();
1063 pa_strbuf_printf(s, " ");
1064 for (ic = 0; ic < n_ic; ic++)
1065 pa_strbuf_printf(s, " I%02u ", ic);
1066 pa_strbuf_puts(s, "\n +");
1068 for (ic = 0; ic < n_ic; ic++)
1069 pa_strbuf_printf(s, "------");
1070 pa_strbuf_puts(s, "\n");
1072 for (oc = 0; oc < n_oc; oc++) {
1073 pa_strbuf_printf(s, "O%02u |", oc);
1075 for (ic = 0; ic < n_ic; ic++)
1076 pa_strbuf_printf(s, " %1.3f", m->map_table_f[oc][ic]);
1078 pa_strbuf_puts(s, "\n");
1081 pa_log_debug("Channel matrix:\n%s", t = pa_strbuf_tostring_free(s));
1084 /* initialize the remapping function */
1088 static pa_memchunk* convert_to_work_format(pa_resampler *r, pa_memchunk *input) {
1094 pa_assert(input->memblock);
1096 /* Convert the incoming sample into the work sample format and place them
1097 * in to_work_format_buf. */
1099 if (!r->to_work_format_func || !input->length)
1102 n_samples = (unsigned) ((input->length / r->i_fz) * r->i_ss.channels);
1104 r->to_work_format_buf.index = 0;
1105 r->to_work_format_buf.length = r->w_sz * n_samples;
1107 if (!r->to_work_format_buf.memblock || r->to_work_format_buf_samples < n_samples) {
1108 if (r->to_work_format_buf.memblock)
1109 pa_memblock_unref(r->to_work_format_buf.memblock);
1111 r->to_work_format_buf_samples = n_samples;
1112 r->to_work_format_buf.memblock = pa_memblock_new(r->mempool, r->to_work_format_buf.length);
1115 src = (uint8_t*) pa_memblock_acquire(input->memblock) + input->index;
1116 dst = (uint8_t*) pa_memblock_acquire(r->to_work_format_buf.memblock);
1118 r->to_work_format_func(n_samples, src, dst);
1120 pa_memblock_release(input->memblock);
1121 pa_memblock_release(r->to_work_format_buf.memblock);
1123 return &r->to_work_format_buf;
1126 static pa_memchunk *remap_channels(pa_resampler *r, pa_memchunk *input) {
1127 unsigned in_n_samples, out_n_samples, in_n_frames, out_n_frames;
1129 size_t leftover_length = 0;
1130 pa_bool_t have_leftover;
1134 pa_assert(input->memblock);
1136 /* Remap channels and place the result in remap_buf. There may be leftover
1137 * data in the beginning of remap_buf. The leftover data is already
1138 * remapped, so it's not part of the input, it's part of the output. */
1140 have_leftover = r->remap_buf_contains_leftover_data;
1141 r->remap_buf_contains_leftover_data = FALSE;
1143 if (!have_leftover && (!r->map_required || input->length <= 0))
1145 else if (input->length <= 0)
1146 return &r->remap_buf;
1148 in_n_samples = (unsigned) (input->length / r->w_sz);
1149 in_n_frames = out_n_frames = in_n_samples / r->i_ss.channels;
1151 if (have_leftover) {
1152 leftover_length = r->remap_buf.length;
1153 out_n_frames += leftover_length / (r->w_sz * r->o_ss.channels);
1156 out_n_samples = out_n_frames * r->o_ss.channels;
1157 r->remap_buf.length = out_n_samples * r->w_sz;
1159 if (have_leftover) {
1160 if (r->remap_buf_size < r->remap_buf.length) {
1161 pa_memblock *new_block = pa_memblock_new(r->mempool, r->remap_buf.length);
1163 src = pa_memblock_acquire(r->remap_buf.memblock);
1164 dst = pa_memblock_acquire(new_block);
1165 memcpy(dst, src, leftover_length);
1166 pa_memblock_release(r->remap_buf.memblock);
1167 pa_memblock_release(new_block);
1169 pa_memblock_unref(r->remap_buf.memblock);
1170 r->remap_buf.memblock = new_block;
1171 r->remap_buf_size = r->remap_buf.length;
1175 if (!r->remap_buf.memblock || r->remap_buf_size < r->remap_buf.length) {
1176 if (r->remap_buf.memblock)
1177 pa_memblock_unref(r->remap_buf.memblock);
1179 r->remap_buf_size = r->remap_buf.length;
1180 r->remap_buf.memblock = pa_memblock_new(r->mempool, r->remap_buf.length);
1184 src = (uint8_t *) pa_memblock_acquire(input->memblock) + input->index;
1185 dst = (uint8_t *) pa_memblock_acquire(r->remap_buf.memblock) + leftover_length;
1187 if (r->map_required) {
1188 pa_remap_t *remap = &r->remap;
1190 pa_assert(remap->do_remap);
1191 remap->do_remap(remap, dst, src, in_n_frames);
1194 memcpy(dst, src, input->length);
1196 pa_memblock_release(input->memblock);
1197 pa_memblock_release(r->remap_buf.memblock);
1199 return &r->remap_buf;
1202 static pa_memchunk *resample(pa_resampler *r, pa_memchunk *input) {
1203 unsigned in_n_frames, in_n_samples;
1204 unsigned out_n_frames, out_n_samples;
1209 /* Resample the data and place the result in resample_buf. */
1211 if (!r->impl_resample || !input->length)
1214 in_n_samples = (unsigned) (input->length / r->w_sz);
1215 in_n_frames = (unsigned) (in_n_samples / r->o_ss.channels);
1217 out_n_frames = ((in_n_frames*r->o_ss.rate)/r->i_ss.rate)+EXTRA_FRAMES;
1218 out_n_samples = out_n_frames * r->o_ss.channels;
1220 r->resample_buf.index = 0;
1221 r->resample_buf.length = r->w_sz * out_n_samples;
1223 if (!r->resample_buf.memblock || r->resample_buf_samples < out_n_samples) {
1224 if (r->resample_buf.memblock)
1225 pa_memblock_unref(r->resample_buf.memblock);
1227 r->resample_buf_samples = out_n_samples;
1228 r->resample_buf.memblock = pa_memblock_new(r->mempool, r->resample_buf.length);
1231 r->impl_resample(r, input, in_n_frames, &r->resample_buf, &out_n_frames);
1232 r->resample_buf.length = out_n_frames * r->w_sz * r->o_ss.channels;
1234 return &r->resample_buf;
1237 static pa_memchunk *convert_from_work_format(pa_resampler *r, pa_memchunk *input) {
1238 unsigned n_samples, n_frames;
1244 /* Convert the data into the correct sample type and place the result in
1245 * from_work_format_buf. */
1247 if (!r->from_work_format_func || !input->length)
1250 n_samples = (unsigned) (input->length / r->w_sz);
1251 n_frames = n_samples / r->o_ss.channels;
1253 r->from_work_format_buf.index = 0;
1254 r->from_work_format_buf.length = r->o_fz * n_frames;
1256 if (!r->from_work_format_buf.memblock || r->from_work_format_buf_samples < n_samples) {
1257 if (r->from_work_format_buf.memblock)
1258 pa_memblock_unref(r->from_work_format_buf.memblock);
1260 r->from_work_format_buf_samples = n_samples;
1261 r->from_work_format_buf.memblock = pa_memblock_new(r->mempool, r->from_work_format_buf.length);
1264 src = (uint8_t*) pa_memblock_acquire(input->memblock) + input->index;
1265 dst = pa_memblock_acquire(r->from_work_format_buf.memblock);
1266 r->from_work_format_func(n_samples, src, dst);
1267 pa_memblock_release(input->memblock);
1268 pa_memblock_release(r->from_work_format_buf.memblock);
1270 r->from_work_format_buf.length = r->o_fz * n_frames;
1272 return &r->from_work_format_buf;
1275 void pa_resampler_run(pa_resampler *r, const pa_memchunk *in, pa_memchunk *out) {
1281 pa_assert(in->length);
1282 pa_assert(in->memblock);
1283 pa_assert(in->length % r->i_fz == 0);
1285 buf = (pa_memchunk*) in;
1286 buf = convert_to_work_format(r, buf);
1287 buf = remap_channels(r, buf);
1288 buf = resample(r, buf);
1291 buf = convert_from_work_format(r, buf);
1295 pa_memblock_ref(buf->memblock);
1297 pa_memchunk_reset(buf);
1299 pa_memchunk_reset(out);
1302 static void save_leftover(pa_resampler *r, void *buf, size_t len) {
1309 /* Store the leftover to remap_buf. */
1311 r->remap_buf.length = len;
1313 if (!r->remap_buf.memblock || r->remap_buf_size < r->remap_buf.length) {
1314 if (r->remap_buf.memblock)
1315 pa_memblock_unref(r->remap_buf.memblock);
1317 r->remap_buf_size = r->remap_buf.length;
1318 r->remap_buf.memblock = pa_memblock_new(r->mempool, r->remap_buf.length);
1321 dst = pa_memblock_acquire(r->remap_buf.memblock);
1322 memcpy(dst, buf, r->remap_buf.length);
1323 pa_memblock_release(r->remap_buf.memblock);
1325 r->remap_buf_contains_leftover_data = TRUE;
1328 /*** libsamplerate based implementation ***/
1330 #ifdef HAVE_LIBSAMPLERATE
1331 static void libsamplerate_resample(pa_resampler *r, const pa_memchunk *input, unsigned in_n_frames, pa_memchunk *output, unsigned *out_n_frames) {
1337 pa_assert(out_n_frames);
1339 memset(&data, 0, sizeof(data));
1341 data.data_in = (float*) ((uint8_t*) pa_memblock_acquire(input->memblock) + input->index);
1342 data.input_frames = (long int) in_n_frames;
1344 data.data_out = (float*) ((uint8_t*) pa_memblock_acquire(output->memblock) + output->index);
1345 data.output_frames = (long int) *out_n_frames;
1347 data.src_ratio = (double) r->o_ss.rate / r->i_ss.rate;
1348 data.end_of_input = 0;
1350 pa_assert_se(src_process(r->src.state, &data) == 0);
1352 if (data.input_frames_used < in_n_frames) {
1353 void *leftover_data = data.data_in + data.input_frames_used * r->o_ss.channels;
1354 size_t leftover_length = (in_n_frames - data.input_frames_used) * sizeof(float) * r->o_ss.channels;
1356 save_leftover(r, leftover_data, leftover_length);
1359 pa_memblock_release(input->memblock);
1360 pa_memblock_release(output->memblock);
1362 *out_n_frames = (unsigned) data.output_frames_gen;
1365 static void libsamplerate_update_rates(pa_resampler *r) {
1368 pa_assert_se(src_set_ratio(r->src.state, (double) r->o_ss.rate / r->i_ss.rate) == 0);
1371 static void libsamplerate_reset(pa_resampler *r) {
1374 pa_assert_se(src_reset(r->src.state) == 0);
1377 static void libsamplerate_free(pa_resampler *r) {
1381 src_delete(r->src.state);
1384 static int libsamplerate_init(pa_resampler *r) {
1389 if (!(r->src.state = src_new(r->method, r->o_ss.channels, &err)))
1392 r->impl_free = libsamplerate_free;
1393 r->impl_update_rates = libsamplerate_update_rates;
1394 r->impl_resample = libsamplerate_resample;
1395 r->impl_reset = libsamplerate_reset;
1402 /*** speex based implementation ***/
1404 static void speex_resample_float(pa_resampler *r, const pa_memchunk *input, unsigned in_n_frames, pa_memchunk *output, unsigned *out_n_frames) {
1406 uint32_t inf = in_n_frames, outf = *out_n_frames;
1411 pa_assert(out_n_frames);
1413 in = (float*) ((uint8_t*) pa_memblock_acquire(input->memblock) + input->index);
1414 out = (float*) ((uint8_t*) pa_memblock_acquire(output->memblock) + output->index);
1416 pa_assert_se(speex_resampler_process_interleaved_float(r->speex.state, in, &inf, out, &outf) == 0);
1418 pa_memblock_release(input->memblock);
1419 pa_memblock_release(output->memblock);
1421 pa_assert(inf == in_n_frames);
1422 *out_n_frames = outf;
1425 static void speex_resample_int(pa_resampler *r, const pa_memchunk *input, unsigned in_n_frames, pa_memchunk *output, unsigned *out_n_frames) {
1427 uint32_t inf = in_n_frames, outf = *out_n_frames;
1432 pa_assert(out_n_frames);
1434 in = (int16_t*) ((uint8_t*) pa_memblock_acquire(input->memblock) + input->index);
1435 out = (int16_t*) ((uint8_t*) pa_memblock_acquire(output->memblock) + output->index);
1437 pa_assert_se(speex_resampler_process_interleaved_int(r->speex.state, in, &inf, out, &outf) == 0);
1439 pa_memblock_release(input->memblock);
1440 pa_memblock_release(output->memblock);
1442 pa_assert(inf == in_n_frames);
1443 *out_n_frames = outf;
1446 static void speex_update_rates(pa_resampler *r) {
1449 pa_assert_se(speex_resampler_set_rate(r->speex.state, r->i_ss.rate, r->o_ss.rate) == 0);
1452 static void speex_reset(pa_resampler *r) {
1455 pa_assert_se(speex_resampler_reset_mem(r->speex.state) == 0);
1458 static void speex_free(pa_resampler *r) {
1461 if (!r->speex.state)
1464 speex_resampler_destroy(r->speex.state);
1467 static int speex_init(pa_resampler *r) {
1472 r->impl_free = speex_free;
1473 r->impl_update_rates = speex_update_rates;
1474 r->impl_reset = speex_reset;
1476 if (r->method >= PA_RESAMPLER_SPEEX_FIXED_BASE && r->method <= PA_RESAMPLER_SPEEX_FIXED_MAX) {
1478 q = r->method - PA_RESAMPLER_SPEEX_FIXED_BASE;
1479 r->impl_resample = speex_resample_int;
1482 pa_assert(r->method >= PA_RESAMPLER_SPEEX_FLOAT_BASE && r->method <= PA_RESAMPLER_SPEEX_FLOAT_MAX);
1484 q = r->method - PA_RESAMPLER_SPEEX_FLOAT_BASE;
1485 r->impl_resample = speex_resample_float;
1488 pa_log_info("Choosing speex quality setting %i.", q);
1490 if (!(r->speex.state = speex_resampler_init(r->o_ss.channels, r->i_ss.rate, r->o_ss.rate, q, &err)))
1497 /* Trivial implementation */
1499 static void trivial_resample(pa_resampler *r, const pa_memchunk *input, unsigned in_n_frames, pa_memchunk *output, unsigned *out_n_frames) {
1501 unsigned i_index, o_index;
1507 pa_assert(out_n_frames);
1509 fz = r->w_sz * r->o_ss.channels;
1511 src = (uint8_t*) pa_memblock_acquire(input->memblock) + input->index;
1512 dst = (uint8_t*) pa_memblock_acquire(output->memblock) + output->index;
1514 for (o_index = 0;; o_index++, r->trivial.o_counter++) {
1515 i_index = (r->trivial.o_counter * r->i_ss.rate) / r->o_ss.rate;
1516 i_index = i_index > r->trivial.i_counter ? i_index - r->trivial.i_counter : 0;
1518 if (i_index >= in_n_frames)
1521 pa_assert_fp(o_index * fz < pa_memblock_get_length(output->memblock));
1523 memcpy((uint8_t*) dst + fz * o_index, (uint8_t*) src + fz * i_index, (int) fz);
1526 pa_memblock_release(input->memblock);
1527 pa_memblock_release(output->memblock);
1529 *out_n_frames = o_index;
1531 r->trivial.i_counter += in_n_frames;
1533 /* Normalize counters */
1534 while (r->trivial.i_counter >= r->i_ss.rate) {
1535 pa_assert(r->trivial.o_counter >= r->o_ss.rate);
1537 r->trivial.i_counter -= r->i_ss.rate;
1538 r->trivial.o_counter -= r->o_ss.rate;
1542 static void trivial_update_rates_or_reset(pa_resampler *r) {
1545 r->trivial.i_counter = 0;
1546 r->trivial.o_counter = 0;
1549 static int trivial_init(pa_resampler*r) {
1552 r->trivial.o_counter = r->trivial.i_counter = 0;
1554 r->impl_resample = trivial_resample;
1555 r->impl_update_rates = trivial_update_rates_or_reset;
1556 r->impl_reset = trivial_update_rates_or_reset;
1561 /* Peak finder implementation */
1563 static void peaks_resample(pa_resampler *r, const pa_memchunk *input, unsigned in_n_frames, pa_memchunk *output, unsigned *out_n_frames) {
1564 unsigned c, o_index = 0;
1565 unsigned i, i_end = 0;
1571 pa_assert(out_n_frames);
1573 src = (uint8_t*) pa_memblock_acquire(input->memblock) + input->index;
1574 dst = (uint8_t*) pa_memblock_acquire(output->memblock) + output->index;
1576 i = (r->peaks.o_counter * r->i_ss.rate) / r->o_ss.rate;
1577 i = i > r->peaks.i_counter ? i - r->peaks.i_counter : 0;
1579 while (i_end < in_n_frames) {
1580 i_end = ((r->peaks.o_counter+1) * r->i_ss.rate) / r->o_ss.rate;
1581 i_end = i_end > r->peaks.i_counter ? i_end - r->peaks.i_counter : 0;
1583 pa_assert_fp(o_index * r->w_sz * r->o_ss.channels < pa_memblock_get_length(output->memblock));
1585 /* 1ch float is treated separately, because that is the common case */
1586 if (r->o_ss.channels == 1 && r->work_format == PA_SAMPLE_FLOAT32NE) {
1587 float *s = (float*) src + i;
1588 float *d = (float*) dst + o_index;
1590 for (; i < i_end && i < in_n_frames; i++) {
1591 float n = fabsf(*s++);
1593 if (n > r->peaks.max_f[0])
1594 r->peaks.max_f[0] = n;
1598 *d = r->peaks.max_f[0];
1599 r->peaks.max_f[0] = 0;
1600 o_index++, r->peaks.o_counter++;
1602 } else if (r->work_format == PA_SAMPLE_S16NE) {
1603 int16_t *s = (int16_t*) src + r->i_ss.channels * i;
1604 int16_t *d = (int16_t*) dst + r->o_ss.channels * o_index;
1606 for (; i < i_end && i < in_n_frames; i++)
1607 for (c = 0; c < r->o_ss.channels; c++) {
1608 int16_t n = abs(*s++);
1610 if (n > r->peaks.max_i[c])
1611 r->peaks.max_i[c] = n;
1615 for (c = 0; c < r->o_ss.channels; c++, d++) {
1616 *d = r->peaks.max_i[c];
1617 r->peaks.max_i[c] = 0;
1619 o_index++, r->peaks.o_counter++;
1622 float *s = (float*) src + r->i_ss.channels * i;
1623 float *d = (float*) dst + r->o_ss.channels * o_index;
1625 for (; i < i_end && i < in_n_frames; i++)
1626 for (c = 0; c < r->o_ss.channels; c++) {
1627 float n = fabsf(*s++);
1629 if (n > r->peaks.max_f[c])
1630 r->peaks.max_f[c] = n;
1634 for (c = 0; c < r->o_ss.channels; c++, d++) {
1635 *d = r->peaks.max_f[c];
1636 r->peaks.max_f[c] = 0;
1638 o_index++, r->peaks.o_counter++;
1643 pa_memblock_release(input->memblock);
1644 pa_memblock_release(output->memblock);
1646 *out_n_frames = o_index;
1648 r->peaks.i_counter += in_n_frames;
1650 /* Normalize counters */
1651 while (r->peaks.i_counter >= r->i_ss.rate) {
1652 pa_assert(r->peaks.o_counter >= r->o_ss.rate);
1654 r->peaks.i_counter -= r->i_ss.rate;
1655 r->peaks.o_counter -= r->o_ss.rate;
1659 static void peaks_update_rates_or_reset(pa_resampler *r) {
1662 r->peaks.i_counter = 0;
1663 r->peaks.o_counter = 0;
1666 static int peaks_init(pa_resampler*r) {
1668 pa_assert(r->i_ss.rate >= r->o_ss.rate);
1669 pa_assert(r->work_format == PA_SAMPLE_S16NE || r->work_format == PA_SAMPLE_FLOAT32NE);
1671 r->peaks.o_counter = r->peaks.i_counter = 0;
1672 memset(r->peaks.max_i, 0, sizeof(r->peaks.max_i));
1673 memset(r->peaks.max_f, 0, sizeof(r->peaks.max_f));
1675 r->impl_resample = peaks_resample;
1676 r->impl_update_rates = peaks_update_rates_or_reset;
1677 r->impl_reset = peaks_update_rates_or_reset;
1682 /*** ffmpeg based implementation ***/
1684 static void ffmpeg_resample(pa_resampler *r, const pa_memchunk *input, unsigned in_n_frames, pa_memchunk *output, unsigned *out_n_frames) {
1685 unsigned used_frames = 0, c;
1686 int previous_consumed_frames = -1;
1691 pa_assert(out_n_frames);
1693 for (c = 0; c < r->o_ss.channels; c++) {
1696 int16_t *p, *t, *k, *q, *s;
1697 int consumed_frames;
1699 /* Allocate a new block */
1700 b = pa_memblock_new(r->mempool, r->ffmpeg.buf[c].length + in_n_frames * sizeof(int16_t));
1701 p = pa_memblock_acquire(b);
1703 /* Now copy the input data, splitting up channels */
1704 t = ((int16_t*) ((uint8_t*) pa_memblock_acquire(input->memblock) + input->index)) + c;
1705 k = (int16_t*) ((uint8_t*) p);
1706 for (u = 0; u < in_n_frames; u++) {
1708 t += r->o_ss.channels;
1711 pa_memblock_release(input->memblock);
1713 /* Allocate buffer for the result */
1714 w = pa_memblock_new(r->mempool, *out_n_frames * sizeof(int16_t));
1715 q = pa_memblock_acquire(w);
1718 used_frames = (unsigned) av_resample(r->ffmpeg.state,
1721 (int) in_n_frames, (int) *out_n_frames,
1722 c >= (unsigned) (r->o_ss.channels-1));
1724 pa_memblock_release(b);
1725 pa_memblock_unref(b);
1727 pa_assert(consumed_frames <= (int) in_n_frames);
1728 pa_assert(previous_consumed_frames == -1 || consumed_frames == previous_consumed_frames);
1729 previous_consumed_frames = consumed_frames;
1731 /* And place the results in the output buffer */
1732 s = (short*) ((uint8_t*) pa_memblock_acquire(output->memblock) + output->index) + c;
1733 for (u = 0; u < used_frames; u++) {
1736 s += r->o_ss.channels;
1738 pa_memblock_release(output->memblock);
1739 pa_memblock_release(w);
1740 pa_memblock_unref(w);
1743 if (previous_consumed_frames < (int) in_n_frames) {
1744 void *leftover_data = (int16_t *) ((uint8_t *) pa_memblock_acquire(input->memblock) + output->index) + previous_consumed_frames * r->o_ss.channels;
1745 size_t leftover_length = (in_n_frames - previous_consumed_frames) * r->o_ss.channels * sizeof(int16_t);
1747 save_leftover(r, leftover_data, leftover_length);
1748 pa_memblock_release(input->memblock);
1751 *out_n_frames = used_frames;
1754 static void ffmpeg_free(pa_resampler *r) {
1759 if (r->ffmpeg.state)
1760 av_resample_close(r->ffmpeg.state);
1762 for (c = 0; c < PA_ELEMENTSOF(r->ffmpeg.buf); c++)
1763 if (r->ffmpeg.buf[c].memblock)
1764 pa_memblock_unref(r->ffmpeg.buf[c].memblock);
1767 static int ffmpeg_init(pa_resampler *r) {
1772 /* We could probably implement different quality levels by
1773 * adjusting the filter parameters here. However, ffmpeg
1774 * internally only uses these hardcoded values, so let's use them
1775 * here for now as well until ffmpeg makes this configurable. */
1777 if (!(r->ffmpeg.state = av_resample_init((int) r->o_ss.rate, (int) r->i_ss.rate, 16, 10, 0, 0.8)))
1780 r->impl_free = ffmpeg_free;
1781 r->impl_resample = ffmpeg_resample;
1783 for (c = 0; c < PA_ELEMENTSOF(r->ffmpeg.buf); c++)
1784 pa_memchunk_reset(&r->ffmpeg.buf[c]);
1789 /*** copy (noop) implementation ***/
1791 static int copy_init(pa_resampler *r) {
1794 pa_assert(r->o_ss.rate == r->i_ss.rate);