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/once.h>
43 #include <pulsecore/core-util.h>
44 #include "ffmpeg/avcodec.h"
46 #include "resampler.h"
48 /* Number of samples of extra space we allow the resamplers to return */
49 #define EXTRA_FRAMES 128
52 pa_resample_method_t method
;
53 pa_resample_flags_t flags
;
55 pa_sample_spec i_ss
, o_ss
;
56 pa_channel_map i_cm
, o_cm
;
57 size_t i_fz
, o_fz
, w_fz
, w_sz
;
60 pa_memchunk to_work_format_buf
;
61 pa_memchunk remap_buf
;
62 pa_memchunk resample_buf
;
63 pa_memchunk from_work_format_buf
;
64 size_t to_work_format_buf_size
;
65 size_t remap_buf_size
;
66 size_t resample_buf_size
;
67 size_t from_work_format_buf_size
;
69 /* points to buffer before resampling stage, remap or to_work */
70 pa_memchunk
*leftover_buf
;
71 size_t *leftover_buf_size
;
73 /* have_leftover points to leftover_in_remap or leftover_in_to_work */
75 bool leftover_in_remap
;
76 bool leftover_in_to_work
;
78 pa_sample_format_t work_format
;
79 uint8_t work_channels
;
81 pa_convert_func_t to_work_format_func
;
82 pa_convert_func_t from_work_format_func
;
87 pa_resampler_impl impl
;
90 struct trivial_data
{ /* data specific to the trivial resampler */
95 struct peaks_data
{ /* data specific to the peak finder pseudo resampler */
99 float max_f
[PA_CHANNELS_MAX
];
100 int16_t max_i
[PA_CHANNELS_MAX
];
103 struct ffmpeg_data
{ /* data specific to ffmpeg */
104 struct AVResampleContext
*state
;
107 static int copy_init(pa_resampler
*r
);
108 static int trivial_init(pa_resampler
*r
);
110 static int speex_init(pa_resampler
*r
);
112 static int ffmpeg_init(pa_resampler
*r
);
113 static int peaks_init(pa_resampler
*r
);
114 #ifdef HAVE_LIBSAMPLERATE
115 static int libsamplerate_init(pa_resampler
*r
);
118 static void setup_remap(const pa_resampler
*r
, pa_remap_t
*m
);
119 static void free_remap(pa_remap_t
*m
);
121 static int (* const init_table
[])(pa_resampler
*r
) = {
122 #ifdef HAVE_LIBSAMPLERATE
123 [PA_RESAMPLER_SRC_SINC_BEST_QUALITY
] = libsamplerate_init
,
124 [PA_RESAMPLER_SRC_SINC_MEDIUM_QUALITY
] = libsamplerate_init
,
125 [PA_RESAMPLER_SRC_SINC_FASTEST
] = libsamplerate_init
,
126 [PA_RESAMPLER_SRC_ZERO_ORDER_HOLD
] = libsamplerate_init
,
127 [PA_RESAMPLER_SRC_LINEAR
] = libsamplerate_init
,
129 [PA_RESAMPLER_SRC_SINC_BEST_QUALITY
] = NULL
,
130 [PA_RESAMPLER_SRC_SINC_MEDIUM_QUALITY
] = NULL
,
131 [PA_RESAMPLER_SRC_SINC_FASTEST
] = NULL
,
132 [PA_RESAMPLER_SRC_ZERO_ORDER_HOLD
] = NULL
,
133 [PA_RESAMPLER_SRC_LINEAR
] = NULL
,
135 [PA_RESAMPLER_TRIVIAL
] = trivial_init
,
137 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+0] = speex_init
,
138 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+1] = speex_init
,
139 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+2] = speex_init
,
140 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+3] = speex_init
,
141 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+4] = speex_init
,
142 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+5] = speex_init
,
143 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+6] = speex_init
,
144 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+7] = speex_init
,
145 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+8] = speex_init
,
146 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+9] = speex_init
,
147 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+10] = speex_init
,
148 [PA_RESAMPLER_SPEEX_FIXED_BASE
+0] = speex_init
,
149 [PA_RESAMPLER_SPEEX_FIXED_BASE
+1] = speex_init
,
150 [PA_RESAMPLER_SPEEX_FIXED_BASE
+2] = speex_init
,
151 [PA_RESAMPLER_SPEEX_FIXED_BASE
+3] = speex_init
,
152 [PA_RESAMPLER_SPEEX_FIXED_BASE
+4] = speex_init
,
153 [PA_RESAMPLER_SPEEX_FIXED_BASE
+5] = speex_init
,
154 [PA_RESAMPLER_SPEEX_FIXED_BASE
+6] = speex_init
,
155 [PA_RESAMPLER_SPEEX_FIXED_BASE
+7] = speex_init
,
156 [PA_RESAMPLER_SPEEX_FIXED_BASE
+8] = speex_init
,
157 [PA_RESAMPLER_SPEEX_FIXED_BASE
+9] = speex_init
,
158 [PA_RESAMPLER_SPEEX_FIXED_BASE
+10] = speex_init
,
160 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+0] = NULL
,
161 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+1] = NULL
,
162 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+2] = NULL
,
163 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+3] = NULL
,
164 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+4] = NULL
,
165 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+5] = NULL
,
166 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+6] = NULL
,
167 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+7] = NULL
,
168 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+8] = NULL
,
169 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+9] = NULL
,
170 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+10] = NULL
,
171 [PA_RESAMPLER_SPEEX_FIXED_BASE
+0] = NULL
,
172 [PA_RESAMPLER_SPEEX_FIXED_BASE
+1] = NULL
,
173 [PA_RESAMPLER_SPEEX_FIXED_BASE
+2] = NULL
,
174 [PA_RESAMPLER_SPEEX_FIXED_BASE
+3] = NULL
,
175 [PA_RESAMPLER_SPEEX_FIXED_BASE
+4] = NULL
,
176 [PA_RESAMPLER_SPEEX_FIXED_BASE
+5] = NULL
,
177 [PA_RESAMPLER_SPEEX_FIXED_BASE
+6] = NULL
,
178 [PA_RESAMPLER_SPEEX_FIXED_BASE
+7] = NULL
,
179 [PA_RESAMPLER_SPEEX_FIXED_BASE
+8] = NULL
,
180 [PA_RESAMPLER_SPEEX_FIXED_BASE
+9] = NULL
,
181 [PA_RESAMPLER_SPEEX_FIXED_BASE
+10] = NULL
,
183 [PA_RESAMPLER_FFMPEG
] = ffmpeg_init
,
184 [PA_RESAMPLER_AUTO
] = NULL
,
185 [PA_RESAMPLER_COPY
] = copy_init
,
186 [PA_RESAMPLER_PEAKS
] = peaks_init
,
189 static bool speex_is_fixed_point(void);
191 static pa_resample_method_t
choose_auto_resampler(pa_resample_flags_t flags
) {
192 pa_resample_method_t method
;
194 if (pa_resample_method_supported(PA_RESAMPLER_SPEEX_FLOAT_BASE
+ 1))
195 method
= PA_RESAMPLER_SPEEX_FLOAT_BASE
+ 1;
196 else if (flags
& PA_RESAMPLER_VARIABLE_RATE
)
197 method
= PA_RESAMPLER_TRIVIAL
;
199 method
= PA_RESAMPLER_FFMPEG
;
204 static pa_resample_method_t
fix_method(
205 pa_resample_flags_t flags
,
206 pa_resample_method_t method
,
207 const uint32_t rate_a
,
208 const uint32_t rate_b
) {
210 pa_assert(pa_sample_rate_valid(rate_a
));
211 pa_assert(pa_sample_rate_valid(rate_b
));
212 pa_assert(method
>= 0);
213 pa_assert(method
< PA_RESAMPLER_MAX
);
215 if (!(flags
& PA_RESAMPLER_VARIABLE_RATE
) && rate_a
== rate_b
) {
216 pa_log_info("Forcing resampler 'copy', because of fixed, identical sample rates.");
217 method
= PA_RESAMPLER_COPY
;
220 if (!pa_resample_method_supported(method
)) {
221 pa_log_warn("Support for resampler '%s' not compiled in, reverting to 'auto'.", pa_resample_method_to_string(method
));
222 method
= PA_RESAMPLER_AUTO
;
226 case PA_RESAMPLER_COPY
:
227 if (rate_a
!= rate_b
) {
228 pa_log_info("Resampler 'copy' cannot change sampling rate, reverting to resampler 'auto'.");
229 method
= PA_RESAMPLER_AUTO
;
232 /* Else fall through */
233 case PA_RESAMPLER_FFMPEG
:
234 if (flags
& PA_RESAMPLER_VARIABLE_RATE
) {
235 pa_log_info("Resampler '%s' cannot do variable rate, reverting to resampler 'auto'.", pa_resample_method_to_string(method
));
236 method
= PA_RESAMPLER_AUTO
;
240 /* The Peaks resampler only supports downsampling.
241 * Revert to auto if we are upsampling */
242 case PA_RESAMPLER_PEAKS
:
243 if (rate_a
< rate_b
) {
244 pa_log_warn("The 'peaks' resampler only supports downsampling, reverting to resampler 'auto'.");
245 method
= PA_RESAMPLER_AUTO
;
253 if (method
== PA_RESAMPLER_AUTO
)
254 method
= choose_auto_resampler(flags
);
256 /* At this point, method is supported in the sense that it
257 * has an init function and supports the required flags. However,
258 * speex-float implementation in PulseAudio relies on the
259 * assumption that is invalid if speex has been compiled with
260 * --enable-fixed-point. Besides, speex-fixed is more efficient
261 * in this configuration. So use it instead.
263 if (method
>= PA_RESAMPLER_SPEEX_FLOAT_BASE
&& method
<= PA_RESAMPLER_SPEEX_FLOAT_MAX
) {
264 if (speex_is_fixed_point()) {
265 pa_log_info("Speex appears to be compiled with --enable-fixed-point. "
266 "Switching to a fixed-point resampler because it should be faster.");
267 method
= method
- PA_RESAMPLER_SPEEX_FLOAT_BASE
+ PA_RESAMPLER_SPEEX_FIXED_BASE
;
273 /* Return true if a is a more precise sample format than b, else return false */
274 static bool sample_format_more_precise(pa_sample_format_t a
, pa_sample_format_t b
) {
275 pa_assert(pa_sample_format_valid(a
));
276 pa_assert(pa_sample_format_valid(b
));
285 case PA_SAMPLE_S16LE
:
286 case PA_SAMPLE_S16BE
:
287 if (b
== PA_SAMPLE_ULAW
|| b
== PA_SAMPLE_ALAW
|| b
== PA_SAMPLE_U8
)
293 case PA_SAMPLE_S24LE
:
294 case PA_SAMPLE_S24BE
:
295 case PA_SAMPLE_S24_32LE
:
296 case PA_SAMPLE_S24_32BE
:
297 if (b
== PA_SAMPLE_ULAW
|| b
== PA_SAMPLE_ALAW
|| b
== PA_SAMPLE_U8
||
298 b
== PA_SAMPLE_S16LE
|| b
== PA_SAMPLE_S16BE
)
304 case PA_SAMPLE_FLOAT32LE
:
305 case PA_SAMPLE_FLOAT32BE
:
306 case PA_SAMPLE_S32LE
:
307 case PA_SAMPLE_S32BE
:
308 if (b
== PA_SAMPLE_FLOAT32LE
|| b
== PA_SAMPLE_FLOAT32BE
||
309 b
== PA_SAMPLE_S32LE
|| b
== PA_SAMPLE_FLOAT32BE
)
320 static pa_sample_format_t
choose_work_format(
321 pa_resample_method_t method
,
322 pa_sample_format_t a
,
323 pa_sample_format_t b
,
325 pa_sample_format_t work_format
;
327 pa_assert(pa_sample_format_valid(a
));
328 pa_assert(pa_sample_format_valid(b
));
329 pa_assert(method
>= 0);
330 pa_assert(method
< PA_RESAMPLER_MAX
);
332 if (method
>= PA_RESAMPLER_SPEEX_FIXED_BASE
&& method
<= PA_RESAMPLER_SPEEX_FIXED_MAX
)
333 method
= PA_RESAMPLER_SPEEX_FIXED_BASE
;
336 /* This block is for resampling functions that only
337 * support the S16 sample format. */
338 case PA_RESAMPLER_SPEEX_FIXED_BASE
: /* fall through */
339 case PA_RESAMPLER_FFMPEG
:
340 work_format
= PA_SAMPLE_S16NE
;
343 /* This block is for resampling functions that support
344 * any sample format. */
345 case PA_RESAMPLER_COPY
: /* fall through */
346 case PA_RESAMPLER_TRIVIAL
:
347 if (!map_required
&& a
== b
) {
351 /* Else fall trough */
352 case PA_RESAMPLER_PEAKS
:
353 if (a
== PA_SAMPLE_S16NE
|| b
== PA_SAMPLE_S16NE
)
354 work_format
= PA_SAMPLE_S16NE
;
355 else if (sample_format_more_precise(a
, PA_SAMPLE_S16NE
) ||
356 sample_format_more_precise(b
, PA_SAMPLE_S16NE
))
357 work_format
= PA_SAMPLE_FLOAT32NE
;
359 work_format
= PA_SAMPLE_S16NE
;
363 work_format
= PA_SAMPLE_FLOAT32NE
;
369 pa_resampler
* pa_resampler_new(
371 const pa_sample_spec
*a
,
372 const pa_channel_map
*am
,
373 const pa_sample_spec
*b
,
374 const pa_channel_map
*bm
,
375 pa_resample_method_t method
,
376 pa_resample_flags_t flags
) {
378 pa_resampler
*r
= NULL
;
383 pa_assert(pa_sample_spec_valid(a
));
384 pa_assert(pa_sample_spec_valid(b
));
385 pa_assert(method
>= 0);
386 pa_assert(method
< PA_RESAMPLER_MAX
);
388 method
= fix_method(flags
, method
, a
->rate
, b
->rate
);
390 r
= pa_xnew0(pa_resampler
, 1);
395 /* Fill sample specs */
401 else if (!pa_channel_map_init_auto(&r
->i_cm
, r
->i_ss
.channels
, PA_CHANNEL_MAP_DEFAULT
))
406 else if (!pa_channel_map_init_auto(&r
->o_cm
, r
->o_ss
.channels
, PA_CHANNEL_MAP_DEFAULT
))
409 r
->i_fz
= pa_frame_size(a
);
410 r
->o_fz
= pa_frame_size(b
);
412 r
->map_required
= (r
->i_ss
.channels
!= r
->o_ss
.channels
|| (!(r
->flags
& PA_RESAMPLER_NO_REMAP
) &&
413 !pa_channel_map_equal(&r
->i_cm
, &r
->o_cm
)));
415 r
->work_format
= choose_work_format(method
, a
->format
, b
->format
, r
->map_required
);
416 r
->w_sz
= pa_sample_size_of_format(r
->work_format
);
418 if (r
->i_ss
.format
!= r
->work_format
) {
419 if (r
->work_format
== PA_SAMPLE_FLOAT32NE
) {
420 if (!(r
->to_work_format_func
= pa_get_convert_to_float32ne_function(r
->i_ss
.format
)))
423 pa_assert(r
->work_format
== PA_SAMPLE_S16NE
);
424 if (!(r
->to_work_format_func
= pa_get_convert_to_s16ne_function(r
->i_ss
.format
)))
429 if (r
->o_ss
.format
!= r
->work_format
) {
430 if (r
->work_format
== PA_SAMPLE_FLOAT32NE
) {
431 if (!(r
->from_work_format_func
= pa_get_convert_from_float32ne_function(r
->o_ss
.format
)))
434 pa_assert(r
->work_format
== PA_SAMPLE_S16NE
);
435 if (!(r
->from_work_format_func
= pa_get_convert_from_s16ne_function(r
->o_ss
.format
)))
440 if (r
->o_ss
.channels
<= r
->i_ss
.channels
) {
441 /* pipeline is: format conv. -> remap -> resample -> format conv. */
442 r
->work_channels
= r
->o_ss
.channels
;
444 /* leftover buffer is remap output buffer (before resampling) */
445 r
->leftover_buf
= &r
->remap_buf
;
446 r
->leftover_buf_size
= &r
->remap_buf_size
;
447 r
->have_leftover
= &r
->leftover_in_remap
;
449 /* pipeline is: format conv. -> resample -> remap -> format conv. */
450 r
->work_channels
= r
->i_ss
.channels
;
452 /* leftover buffer is to_work output buffer (before resampling) */
453 r
->leftover_buf
= &r
->to_work_format_buf
;
454 r
->leftover_buf_size
= &r
->to_work_format_buf_size
;
455 r
->have_leftover
= &r
->leftover_in_to_work
;
457 r
->w_fz
= pa_sample_size_of_format(r
->work_format
) * r
->work_channels
;
459 pa_log_debug("Resampler:");
460 pa_log_debug(" rate %d -> %d (method %s)", a
->rate
, b
->rate
, pa_resample_method_to_string(r
->method
));
461 pa_log_debug(" format %s -> %s (intermediate %s)", pa_sample_format_to_string(a
->format
),
462 pa_sample_format_to_string(b
->format
), pa_sample_format_to_string(r
->work_format
));
463 pa_log_debug(" channels %d -> %d (resampling %d)", a
->channels
, b
->channels
, r
->work_channels
);
465 /* set up the remap structure */
467 setup_remap(r
, &r
->remap
);
469 /* initialize implementation */
470 if (init_table
[method
](r
) < 0)
481 void pa_resampler_free(pa_resampler
*r
) {
487 pa_xfree(r
->impl
.data
);
489 if (r
->to_work_format_buf
.memblock
)
490 pa_memblock_unref(r
->to_work_format_buf
.memblock
);
491 if (r
->remap_buf
.memblock
)
492 pa_memblock_unref(r
->remap_buf
.memblock
);
493 if (r
->resample_buf
.memblock
)
494 pa_memblock_unref(r
->resample_buf
.memblock
);
495 if (r
->from_work_format_buf
.memblock
)
496 pa_memblock_unref(r
->from_work_format_buf
.memblock
);
498 free_remap(&r
->remap
);
503 void pa_resampler_set_input_rate(pa_resampler
*r
, uint32_t rate
) {
506 pa_assert(r
->impl
.update_rates
);
508 if (r
->i_ss
.rate
== rate
)
513 r
->impl
.update_rates(r
);
516 void pa_resampler_set_output_rate(pa_resampler
*r
, uint32_t rate
) {
519 pa_assert(r
->impl
.update_rates
);
521 if (r
->o_ss
.rate
== rate
)
526 r
->impl
.update_rates(r
);
529 size_t pa_resampler_request(pa_resampler
*r
, size_t out_length
) {
532 /* Let's round up here to make it more likely that the caller will get at
533 * least out_length amount of data from pa_resampler_run().
535 * We don't take the leftover into account here. If we did, then it might
536 * be in theory possible that this function would return 0 and
537 * pa_resampler_run() would also return 0. That could lead to infinite
538 * loops. When the leftover is ignored here, such loops would eventually
539 * terminate, because the leftover would grow each round, finally
540 * surpassing the minimum input threshold of the resampler. */
541 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
;
544 size_t pa_resampler_result(pa_resampler
*r
, size_t in_length
) {
549 /* Let's round up here to ensure that the caller will always allocate big
550 * enough output buffer. */
552 frames
= (in_length
+ r
->i_fz
- 1) / r
->i_fz
;
553 if (*r
->have_leftover
)
554 frames
+= r
->leftover_buf
->length
/ r
->w_fz
;
556 return (((uint64_t) frames
* r
->o_ss
.rate
+ r
->i_ss
.rate
- 1) / r
->i_ss
.rate
) * r
->o_fz
;
559 size_t pa_resampler_max_block_size(pa_resampler
*r
) {
560 size_t block_size_max
;
561 pa_sample_spec max_ss
;
567 block_size_max
= pa_mempool_block_size_max(r
->mempool
);
569 /* We deduce the "largest" sample spec we're using during the
571 max_ss
.channels
= (uint8_t) (PA_MAX(r
->i_ss
.channels
, r
->o_ss
.channels
));
573 /* We silently assume that the format enum is ordered by size */
574 max_ss
.format
= PA_MAX(r
->i_ss
.format
, r
->o_ss
.format
);
575 max_ss
.format
= PA_MAX(max_ss
.format
, r
->work_format
);
577 max_ss
.rate
= PA_MAX(r
->i_ss
.rate
, r
->o_ss
.rate
);
579 max_fs
= pa_frame_size(&max_ss
);
580 frames
= block_size_max
/ max_fs
- EXTRA_FRAMES
;
582 pa_assert(frames
>= (r
->leftover_buf
->length
/ r
->w_fz
));
583 if (*r
->have_leftover
)
584 frames
-= r
->leftover_buf
->length
/ r
->w_fz
;
586 block_size_max
= ((uint64_t) frames
* r
->i_ss
.rate
/ max_ss
.rate
) * r
->i_fz
;
588 if (block_size_max
> 0)
589 return block_size_max
;
591 /* A single input frame may result in so much output that it doesn't
592 * fit in one standard memblock (e.g. converting 1 Hz to 44100 Hz). In
593 * this case the max block size will be set to one frame, and some
594 * memory will be probably be allocated with malloc() instead of using
597 * XXX: Should we support this case at all? We could also refuse to
598 * create resamplers whose max block size would exceed the memory pool
599 * block size. In this case also updating the resampler rate should
600 * fail if the new rate would cause an excessive max block size (in
601 * which case the stream would probably have to be killed). */
605 void pa_resampler_reset(pa_resampler
*r
) {
611 *r
->have_leftover
= false;
614 pa_resample_method_t
pa_resampler_get_method(pa_resampler
*r
) {
620 const pa_channel_map
* pa_resampler_input_channel_map(pa_resampler
*r
) {
626 const pa_sample_spec
* pa_resampler_input_sample_spec(pa_resampler
*r
) {
632 const pa_channel_map
* pa_resampler_output_channel_map(pa_resampler
*r
) {
638 const pa_sample_spec
* pa_resampler_output_sample_spec(pa_resampler
*r
) {
644 static const char * const resample_methods
[] = {
645 "src-sinc-best-quality",
646 "src-sinc-medium-quality",
648 "src-zero-order-hold",
679 const char *pa_resample_method_to_string(pa_resample_method_t m
) {
681 if (m
< 0 || m
>= PA_RESAMPLER_MAX
)
684 return resample_methods
[m
];
687 int pa_resample_method_supported(pa_resample_method_t m
) {
689 if (m
< 0 || m
>= PA_RESAMPLER_MAX
)
692 #ifndef HAVE_LIBSAMPLERATE
693 if (m
<= PA_RESAMPLER_SRC_LINEAR
)
698 if (m
>= PA_RESAMPLER_SPEEX_FLOAT_BASE
&& m
<= PA_RESAMPLER_SPEEX_FLOAT_MAX
)
700 if (m
>= PA_RESAMPLER_SPEEX_FIXED_BASE
&& m
<= PA_RESAMPLER_SPEEX_FIXED_MAX
)
707 pa_resample_method_t
pa_parse_resample_method(const char *string
) {
708 pa_resample_method_t m
;
712 for (m
= 0; m
< PA_RESAMPLER_MAX
; m
++)
713 if (pa_streq(string
, resample_methods
[m
]))
716 if (pa_streq(string
, "speex-fixed"))
717 return PA_RESAMPLER_SPEEX_FIXED_BASE
+ 1;
719 if (pa_streq(string
, "speex-float"))
720 return PA_RESAMPLER_SPEEX_FLOAT_BASE
+ 1;
722 return PA_RESAMPLER_INVALID
;
725 static bool on_left(pa_channel_position_t p
) {
728 p
== PA_CHANNEL_POSITION_FRONT_LEFT
||
729 p
== PA_CHANNEL_POSITION_REAR_LEFT
||
730 p
== PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER
||
731 p
== PA_CHANNEL_POSITION_SIDE_LEFT
||
732 p
== PA_CHANNEL_POSITION_TOP_FRONT_LEFT
||
733 p
== PA_CHANNEL_POSITION_TOP_REAR_LEFT
;
736 static bool on_right(pa_channel_position_t p
) {
739 p
== PA_CHANNEL_POSITION_FRONT_RIGHT
||
740 p
== PA_CHANNEL_POSITION_REAR_RIGHT
||
741 p
== PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER
||
742 p
== PA_CHANNEL_POSITION_SIDE_RIGHT
||
743 p
== PA_CHANNEL_POSITION_TOP_FRONT_RIGHT
||
744 p
== PA_CHANNEL_POSITION_TOP_REAR_RIGHT
;
747 static bool on_center(pa_channel_position_t p
) {
750 p
== PA_CHANNEL_POSITION_FRONT_CENTER
||
751 p
== PA_CHANNEL_POSITION_REAR_CENTER
||
752 p
== PA_CHANNEL_POSITION_TOP_CENTER
||
753 p
== PA_CHANNEL_POSITION_TOP_FRONT_CENTER
||
754 p
== PA_CHANNEL_POSITION_TOP_REAR_CENTER
;
757 static bool on_lfe(pa_channel_position_t p
) {
759 p
== PA_CHANNEL_POSITION_LFE
;
762 static bool on_front(pa_channel_position_t p
) {
764 p
== PA_CHANNEL_POSITION_FRONT_LEFT
||
765 p
== PA_CHANNEL_POSITION_FRONT_RIGHT
||
766 p
== PA_CHANNEL_POSITION_FRONT_CENTER
||
767 p
== PA_CHANNEL_POSITION_TOP_FRONT_LEFT
||
768 p
== PA_CHANNEL_POSITION_TOP_FRONT_RIGHT
||
769 p
== PA_CHANNEL_POSITION_TOP_FRONT_CENTER
||
770 p
== PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER
||
771 p
== PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER
;
774 static bool on_rear(pa_channel_position_t p
) {
776 p
== PA_CHANNEL_POSITION_REAR_LEFT
||
777 p
== PA_CHANNEL_POSITION_REAR_RIGHT
||
778 p
== PA_CHANNEL_POSITION_REAR_CENTER
||
779 p
== PA_CHANNEL_POSITION_TOP_REAR_LEFT
||
780 p
== PA_CHANNEL_POSITION_TOP_REAR_RIGHT
||
781 p
== PA_CHANNEL_POSITION_TOP_REAR_CENTER
;
784 static bool on_side(pa_channel_position_t p
) {
786 p
== PA_CHANNEL_POSITION_SIDE_LEFT
||
787 p
== PA_CHANNEL_POSITION_SIDE_RIGHT
||
788 p
== PA_CHANNEL_POSITION_TOP_CENTER
;
798 static int front_rear_side(pa_channel_position_t p
) {
808 static void setup_remap(const pa_resampler
*r
, pa_remap_t
*m
) {
811 bool ic_connected
[PA_CHANNELS_MAX
];
819 n_oc
= r
->o_ss
.channels
;
820 n_ic
= r
->i_ss
.channels
;
822 m
->format
= r
->work_format
;
826 memset(m
->map_table_f
, 0, sizeof(m
->map_table_f
));
827 memset(m
->map_table_i
, 0, sizeof(m
->map_table_i
));
829 memset(ic_connected
, 0, sizeof(ic_connected
));
830 remix
= (r
->flags
& (PA_RESAMPLER_NO_REMAP
| PA_RESAMPLER_NO_REMIX
)) == 0;
832 if (r
->flags
& PA_RESAMPLER_NO_REMAP
) {
835 for (oc
= 0; oc
< PA_MIN(n_ic
, n_oc
); oc
++)
836 m
->map_table_f
[oc
][oc
] = 1.0f
;
838 } else if (r
->flags
& PA_RESAMPLER_NO_REMIX
) {
840 for (oc
= 0; oc
< n_oc
; oc
++) {
841 pa_channel_position_t b
= r
->o_cm
.map
[oc
];
843 for (ic
= 0; ic
< n_ic
; ic
++) {
844 pa_channel_position_t a
= r
->i_cm
.map
[ic
];
846 /* We shall not do any remixing. Hence, just check by name */
848 m
->map_table_f
[oc
][ic
] = 1.0f
;
853 /* OK, we shall do the full monty: upmixing and downmixing. Our
854 * algorithm is relatively simple, does not do spacialization, delay
855 * elements or apply lowpass filters for LFE. Patches are always
856 * welcome, though. Oh, and it doesn't do any matrix decoding. (Which
857 * probably wouldn't make any sense anyway.)
859 * This code is not idempotent: downmixing an upmixed stereo stream is
860 * not identical to the original. The volume will not match, and the
861 * two channels will be a linear combination of both.
863 * This is loosely based on random suggestions found on the Internet,
865 * http://www.halfgaar.net/surround-sound-in-linux and the alsa upmix
868 * The algorithm works basically like this:
870 * 1) Connect all channels with matching names.
873 * S:Mono: Copy into all D:channels
874 * D:Mono: Avg all S:channels
876 * 3) Mix D:Left, D:Right:
877 * D:Left: If not connected, avg all S:Left
878 * D:Right: If not connected, avg all S:Right
881 * If not connected, avg all S:Center
882 * If still not connected, avg all S:Left, S:Right
885 * If not connected, avg all S:*
887 * 6) Make sure S:Left/S:Right is used: S:Left/S:Right: If not
888 * connected, mix into all D:left and all D:right channels. Gain is
891 * 7) Make sure S:Center, S:LFE is used:
893 * S:Center, S:LFE: If not connected, mix into all D:left, all
894 * D:right, all D:center channels. Gain is 0.5 for center and 0.375
895 * for LFE. C-front is only mixed into L-front/R-front if available,
896 * otherwise into all L/R channels. Similarly for C-rear.
898 * 8) Normalize each row in the matrix such that the sum for each row is
899 * not larger than 1.0 in order to avoid clipping.
901 * S: and D: shall relate to the source resp. destination channels.
903 * Rationale: 1, 2 are probably obvious. For 3: this copies front to
904 * rear if needed. For 4: we try to find some suitable C source for C,
905 * if we don't find any, we avg L and R. For 5: LFE is mixed from all
906 * channels. For 6: the rear channels should not be dropped entirely,
907 * however have only minimal impact. For 7: movies usually encode
908 * speech on the center channel. Thus we have to make sure this channel
909 * is distributed to L and R if not available in the output. Also, LFE
910 * is used to achieve a greater dynamic range, and thus we should try
911 * to do our best to pass it to L+R.
918 ic_unconnected_left
= 0,
919 ic_unconnected_right
= 0,
920 ic_unconnected_center
= 0,
921 ic_unconnected_lfe
= 0;
922 bool ic_unconnected_center_mixed_in
= 0;
926 for (ic
= 0; ic
< n_ic
; ic
++) {
927 if (on_left(r
->i_cm
.map
[ic
]))
929 if (on_right(r
->i_cm
.map
[ic
]))
931 if (on_center(r
->i_cm
.map
[ic
]))
935 for (oc
= 0; oc
< n_oc
; oc
++) {
936 bool oc_connected
= false;
937 pa_channel_position_t b
= r
->o_cm
.map
[oc
];
939 for (ic
= 0; ic
< n_ic
; ic
++) {
940 pa_channel_position_t a
= r
->i_cm
.map
[ic
];
942 if (a
== b
|| a
== PA_CHANNEL_POSITION_MONO
) {
943 m
->map_table_f
[oc
][ic
] = 1.0f
;
946 ic_connected
[ic
] = true;
948 else if (b
== PA_CHANNEL_POSITION_MONO
) {
949 m
->map_table_f
[oc
][ic
] = 1.0f
/ (float) n_ic
;
952 ic_connected
[ic
] = true;
957 /* Try to find matching input ports for this output port */
961 /* We are not connected and on the left side, let's
962 * average all left side input channels. */
965 for (ic
= 0; ic
< n_ic
; ic
++)
966 if (on_left(r
->i_cm
.map
[ic
])) {
967 m
->map_table_f
[oc
][ic
] = 1.0f
/ (float) ic_left
;
968 ic_connected
[ic
] = true;
971 /* We ignore the case where there is no left input channel.
972 * Something is really wrong in this case anyway. */
974 } else if (on_right(b
)) {
976 /* We are not connected and on the right side, let's
977 * average all right side input channels. */
980 for (ic
= 0; ic
< n_ic
; ic
++)
981 if (on_right(r
->i_cm
.map
[ic
])) {
982 m
->map_table_f
[oc
][ic
] = 1.0f
/ (float) ic_right
;
983 ic_connected
[ic
] = true;
986 /* We ignore the case where there is no right input
987 * channel. Something is really wrong in this case anyway.
990 } else if (on_center(b
)) {
994 /* We are not connected and at the center. Let's average
995 * all center input channels. */
997 for (ic
= 0; ic
< n_ic
; ic
++)
998 if (on_center(r
->i_cm
.map
[ic
])) {
999 m
->map_table_f
[oc
][ic
] = 1.0f
/ (float) ic_center
;
1000 ic_connected
[ic
] = true;
1003 } else if (ic_left
+ ic_right
> 0) {
1005 /* Hmm, no center channel around, let's synthesize it
1006 * by mixing L and R.*/
1008 for (ic
= 0; ic
< n_ic
; ic
++)
1009 if (on_left(r
->i_cm
.map
[ic
]) || on_right(r
->i_cm
.map
[ic
])) {
1010 m
->map_table_f
[oc
][ic
] = 1.0f
/ (float) (ic_left
+ ic_right
);
1011 ic_connected
[ic
] = true;
1015 /* We ignore the case where there is not even a left or
1016 * right input channel. Something is really wrong in this
1019 } else if (on_lfe(b
) && !(r
->flags
& PA_RESAMPLER_NO_LFE
)) {
1021 /* We are not connected and an LFE. Let's average all
1022 * channels for LFE. */
1024 for (ic
= 0; ic
< n_ic
; ic
++)
1025 m
->map_table_f
[oc
][ic
] = 1.0f
/ (float) n_ic
;
1027 /* Please note that a channel connected to LFE doesn't
1028 * really count as connected. */
1033 for (ic
= 0; ic
< n_ic
; ic
++) {
1034 pa_channel_position_t a
= r
->i_cm
.map
[ic
];
1036 if (ic_connected
[ic
])
1040 ic_unconnected_left
++;
1041 else if (on_right(a
))
1042 ic_unconnected_right
++;
1043 else if (on_center(a
))
1044 ic_unconnected_center
++;
1046 ic_unconnected_lfe
++;
1049 for (ic
= 0; ic
< n_ic
; ic
++) {
1050 pa_channel_position_t a
= r
->i_cm
.map
[ic
];
1052 if (ic_connected
[ic
])
1055 for (oc
= 0; oc
< n_oc
; oc
++) {
1056 pa_channel_position_t b
= r
->o_cm
.map
[oc
];
1058 if (on_left(a
) && on_left(b
))
1059 m
->map_table_f
[oc
][ic
] = (1.f
/9.f
) / (float) ic_unconnected_left
;
1061 else if (on_right(a
) && on_right(b
))
1062 m
->map_table_f
[oc
][ic
] = (1.f
/9.f
) / (float) ic_unconnected_right
;
1064 else if (on_center(a
) && on_center(b
)) {
1065 m
->map_table_f
[oc
][ic
] = (1.f
/9.f
) / (float) ic_unconnected_center
;
1066 ic_unconnected_center_mixed_in
= true;
1068 } else if (on_lfe(a
) && !(r
->flags
& PA_RESAMPLER_NO_LFE
))
1069 m
->map_table_f
[oc
][ic
] = .375f
/ (float) ic_unconnected_lfe
;
1073 if (ic_unconnected_center
> 0 && !ic_unconnected_center_mixed_in
) {
1074 unsigned ncenter
[PA_CHANNELS_MAX
];
1075 bool found_frs
[PA_CHANNELS_MAX
];
1077 memset(ncenter
, 0, sizeof(ncenter
));
1078 memset(found_frs
, 0, sizeof(found_frs
));
1080 /* Hmm, as it appears there was no center channel we
1081 could mix our center channel in. In this case, mix it into
1082 left and right. Using .5 as the factor. */
1084 for (ic
= 0; ic
< n_ic
; ic
++) {
1086 if (ic_connected
[ic
])
1089 if (!on_center(r
->i_cm
.map
[ic
]))
1092 for (oc
= 0; oc
< n_oc
; oc
++) {
1094 if (!on_left(r
->o_cm
.map
[oc
]) && !on_right(r
->o_cm
.map
[oc
]))
1097 if (front_rear_side(r
->i_cm
.map
[ic
]) == front_rear_side(r
->o_cm
.map
[oc
])) {
1098 found_frs
[ic
] = true;
1103 for (oc
= 0; oc
< n_oc
; oc
++) {
1105 if (!on_left(r
->o_cm
.map
[oc
]) && !on_right(r
->o_cm
.map
[oc
]))
1108 if (!found_frs
[ic
] || front_rear_side(r
->i_cm
.map
[ic
]) == front_rear_side(r
->o_cm
.map
[oc
]))
1113 for (oc
= 0; oc
< n_oc
; oc
++) {
1115 if (!on_left(r
->o_cm
.map
[oc
]) && !on_right(r
->o_cm
.map
[oc
]))
1118 if (ncenter
[oc
] <= 0)
1121 for (ic
= 0; ic
< n_ic
; ic
++) {
1123 if (!on_center(r
->i_cm
.map
[ic
]))
1126 if (!found_frs
[ic
] || front_rear_side(r
->i_cm
.map
[ic
]) == front_rear_side(r
->o_cm
.map
[oc
]))
1127 m
->map_table_f
[oc
][ic
] = .5f
/ (float) ncenter
[oc
];
1133 for (oc
= 0; oc
< n_oc
; oc
++) {
1135 for (ic
= 0; ic
< n_ic
; ic
++)
1136 sum
+= m
->map_table_f
[oc
][ic
];
1139 for (ic
= 0; ic
< n_ic
; ic
++)
1140 m
->map_table_f
[oc
][ic
] /= sum
;
1143 /* make an 16:16 int version of the matrix */
1144 for (oc
= 0; oc
< n_oc
; oc
++)
1145 for (ic
= 0; ic
< n_ic
; ic
++)
1146 m
->map_table_i
[oc
][ic
] = (int32_t) (m
->map_table_f
[oc
][ic
] * 0x10000);
1148 s
= pa_strbuf_new();
1150 pa_strbuf_printf(s
, " ");
1151 for (ic
= 0; ic
< n_ic
; ic
++)
1152 pa_strbuf_printf(s
, " I%02u ", ic
);
1153 pa_strbuf_puts(s
, "\n +");
1155 for (ic
= 0; ic
< n_ic
; ic
++)
1156 pa_strbuf_printf(s
, "------");
1157 pa_strbuf_puts(s
, "\n");
1159 for (oc
= 0; oc
< n_oc
; oc
++) {
1160 pa_strbuf_printf(s
, "O%02u |", oc
);
1162 for (ic
= 0; ic
< n_ic
; ic
++)
1163 pa_strbuf_printf(s
, " %1.3f", m
->map_table_f
[oc
][ic
]);
1165 pa_strbuf_puts(s
, "\n");
1168 pa_log_debug("Channel matrix:\n%s", t
= pa_strbuf_tostring_free(s
));
1171 /* initialize the remapping function */
1172 pa_init_remap_func(m
);
1175 static void free_remap(pa_remap_t
*m
) {
1181 /* check if buf's memblock is large enough to hold 'len' bytes; create a
1182 * new memblock if necessary and optionally preserve 'copy' data bytes */
1183 static void fit_buf(pa_resampler
*r
, pa_memchunk
*buf
, size_t len
, size_t *size
, size_t copy
) {
1186 if (!buf
->memblock
|| len
> *size
) {
1187 pa_memblock
*new_block
= pa_memblock_new(r
->mempool
, len
);
1189 if (buf
->memblock
) {
1191 void *src
= pa_memblock_acquire(buf
->memblock
);
1192 void *dst
= pa_memblock_acquire(new_block
);
1193 pa_assert(copy
<= len
);
1194 memcpy(dst
, src
, copy
);
1195 pa_memblock_release(new_block
);
1196 pa_memblock_release(buf
->memblock
);
1199 pa_memblock_unref(buf
->memblock
);
1202 buf
->memblock
= new_block
;
1209 static pa_memchunk
* convert_to_work_format(pa_resampler
*r
, pa_memchunk
*input
) {
1210 unsigned in_n_samples
, out_n_samples
;
1213 size_t leftover_length
= 0;
1217 pa_assert(input
->memblock
);
1219 /* Convert the incoming sample into the work sample format and place them
1220 * in to_work_format_buf. The leftover data is already converted, so it's
1221 * part of the output buffer. */
1223 have_leftover
= r
->leftover_in_to_work
;
1224 r
->leftover_in_to_work
= false;
1226 if (!have_leftover
&& (!r
->to_work_format_func
|| !input
->length
))
1228 else if (input
->length
<= 0)
1229 return &r
->to_work_format_buf
;
1231 in_n_samples
= out_n_samples
= (unsigned) ((input
->length
/ r
->i_fz
) * r
->i_ss
.channels
);
1233 if (have_leftover
) {
1234 leftover_length
= r
->to_work_format_buf
.length
;
1235 out_n_samples
+= (unsigned) (leftover_length
/ r
->w_sz
);
1238 fit_buf(r
, &r
->to_work_format_buf
, r
->w_sz
* out_n_samples
, &r
->to_work_format_buf_size
, leftover_length
);
1240 src
= pa_memblock_acquire_chunk(input
);
1241 dst
= (uint8_t *) pa_memblock_acquire(r
->to_work_format_buf
.memblock
) + leftover_length
;
1243 if (r
->to_work_format_func
)
1244 r
->to_work_format_func(in_n_samples
, src
, dst
);
1246 memcpy(dst
, src
, input
->length
);
1248 pa_memblock_release(input
->memblock
);
1249 pa_memblock_release(r
->to_work_format_buf
.memblock
);
1251 return &r
->to_work_format_buf
;
1254 static pa_memchunk
*remap_channels(pa_resampler
*r
, pa_memchunk
*input
) {
1255 unsigned in_n_samples
, out_n_samples
, in_n_frames
, out_n_frames
;
1257 size_t leftover_length
= 0;
1262 pa_assert(input
->memblock
);
1264 /* Remap channels and place the result in remap_buf. There may be leftover
1265 * data in the beginning of remap_buf. The leftover data is already
1266 * remapped, so it's not part of the input, it's part of the output. */
1268 have_leftover
= r
->leftover_in_remap
;
1269 r
->leftover_in_remap
= false;
1271 if (!have_leftover
&& (!r
->map_required
|| input
->length
<= 0))
1273 else if (input
->length
<= 0)
1274 return &r
->remap_buf
;
1276 in_n_samples
= (unsigned) (input
->length
/ r
->w_sz
);
1277 in_n_frames
= out_n_frames
= in_n_samples
/ r
->i_ss
.channels
;
1279 if (have_leftover
) {
1280 leftover_length
= r
->remap_buf
.length
;
1281 out_n_frames
+= leftover_length
/ r
->w_fz
;
1284 out_n_samples
= out_n_frames
* r
->o_ss
.channels
;
1285 fit_buf(r
, &r
->remap_buf
, out_n_samples
* r
->w_sz
, &r
->remap_buf_size
, leftover_length
);
1287 src
= pa_memblock_acquire_chunk(input
);
1288 dst
= (uint8_t *) pa_memblock_acquire(r
->remap_buf
.memblock
) + leftover_length
;
1290 if (r
->map_required
) {
1291 pa_remap_t
*remap
= &r
->remap
;
1293 pa_assert(remap
->do_remap
);
1294 remap
->do_remap(remap
, dst
, src
, in_n_frames
);
1297 memcpy(dst
, src
, input
->length
);
1299 pa_memblock_release(input
->memblock
);
1300 pa_memblock_release(r
->remap_buf
.memblock
);
1302 return &r
->remap_buf
;
1305 static void save_leftover(pa_resampler
*r
, void *buf
, size_t len
) {
1312 /* Store the leftover data. */
1313 fit_buf(r
, r
->leftover_buf
, len
, r
->leftover_buf_size
, 0);
1314 *r
->have_leftover
= true;
1316 dst
= pa_memblock_acquire(r
->leftover_buf
->memblock
);
1317 memmove(dst
, buf
, len
);
1318 pa_memblock_release(r
->leftover_buf
->memblock
);
1321 static pa_memchunk
*resample(pa_resampler
*r
, pa_memchunk
*input
) {
1322 unsigned in_n_frames
, out_n_frames
, leftover_n_frames
;
1327 /* Resample the data and place the result in resample_buf. */
1329 if (!r
->impl
.resample
|| !input
->length
)
1332 in_n_frames
= (unsigned) (input
->length
/ r
->w_fz
);
1334 out_n_frames
= ((in_n_frames
*r
->o_ss
.rate
)/r
->i_ss
.rate
)+EXTRA_FRAMES
;
1335 fit_buf(r
, &r
->resample_buf
, r
->w_fz
* out_n_frames
, &r
->resample_buf_size
, 0);
1337 leftover_n_frames
= r
->impl
.resample(r
, input
, in_n_frames
, &r
->resample_buf
, &out_n_frames
);
1339 if (leftover_n_frames
> 0) {
1340 void *leftover_data
= (uint8_t *) pa_memblock_acquire_chunk(input
) + (in_n_frames
- leftover_n_frames
) * r
->w_fz
;
1341 save_leftover(r
, leftover_data
, leftover_n_frames
* r
->w_fz
);
1342 pa_memblock_release(input
->memblock
);
1345 r
->resample_buf
.length
= out_n_frames
* r
->w_fz
;
1347 return &r
->resample_buf
;
1350 static pa_memchunk
*convert_from_work_format(pa_resampler
*r
, pa_memchunk
*input
) {
1351 unsigned n_samples
, n_frames
;
1357 /* Convert the data into the correct sample type and place the result in
1358 * from_work_format_buf. */
1360 if (!r
->from_work_format_func
|| !input
->length
)
1363 n_samples
= (unsigned) (input
->length
/ r
->w_sz
);
1364 n_frames
= n_samples
/ r
->o_ss
.channels
;
1365 fit_buf(r
, &r
->from_work_format_buf
, r
->o_fz
* n_frames
, &r
->from_work_format_buf_size
, 0);
1367 src
= pa_memblock_acquire_chunk(input
);
1368 dst
= pa_memblock_acquire(r
->from_work_format_buf
.memblock
);
1369 r
->from_work_format_func(n_samples
, src
, dst
);
1370 pa_memblock_release(input
->memblock
);
1371 pa_memblock_release(r
->from_work_format_buf
.memblock
);
1373 return &r
->from_work_format_buf
;
1376 void pa_resampler_run(pa_resampler
*r
, const pa_memchunk
*in
, pa_memchunk
*out
) {
1382 pa_assert(in
->length
);
1383 pa_assert(in
->memblock
);
1384 pa_assert(in
->length
% r
->i_fz
== 0);
1386 buf
= (pa_memchunk
*) in
;
1387 buf
= convert_to_work_format(r
, buf
);
1389 /* Try to save resampling effort: if we have more output channels than
1390 * input channels, do resampling first, then remapping. */
1391 if (r
->o_ss
.channels
<= r
->i_ss
.channels
) {
1392 buf
= remap_channels(r
, buf
);
1393 buf
= resample(r
, buf
);
1395 buf
= resample(r
, buf
);
1396 buf
= remap_channels(r
, buf
);
1400 buf
= convert_from_work_format(r
, buf
);
1404 pa_memblock_ref(buf
->memblock
);
1406 pa_memchunk_reset(buf
);
1408 pa_memchunk_reset(out
);
1411 /*** libsamplerate based implementation ***/
1413 #ifdef HAVE_LIBSAMPLERATE
1414 static unsigned libsamplerate_resample(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
1421 pa_assert(out_n_frames
);
1423 state
= r
->impl
.data
;
1424 memset(&data
, 0, sizeof(data
));
1426 data
.data_in
= pa_memblock_acquire_chunk(input
);
1427 data
.input_frames
= (long int) in_n_frames
;
1429 data
.data_out
= pa_memblock_acquire_chunk(output
);
1430 data
.output_frames
= (long int) *out_n_frames
;
1432 data
.src_ratio
= (double) r
->o_ss
.rate
/ r
->i_ss
.rate
;
1433 data
.end_of_input
= 0;
1435 pa_assert_se(src_process(state
, &data
) == 0);
1437 pa_memblock_release(input
->memblock
);
1438 pa_memblock_release(output
->memblock
);
1440 *out_n_frames
= (unsigned) data
.output_frames_gen
;
1442 return in_n_frames
- data
.input_frames_used
;
1445 static void libsamplerate_update_rates(pa_resampler
*r
) {
1449 state
= r
->impl
.data
;
1450 pa_assert_se(src_set_ratio(state
, (double) r
->o_ss
.rate
/ r
->i_ss
.rate
) == 0);
1453 static void libsamplerate_reset(pa_resampler
*r
) {
1457 state
= r
->impl
.data
;
1458 pa_assert_se(src_reset(state
) == 0);
1461 static void libsamplerate_free(pa_resampler
*r
) {
1465 state
= r
->impl
.data
;
1470 static int libsamplerate_init(pa_resampler
*r
) {
1476 if (!(state
= src_new(r
->method
, r
->work_channels
, &err
)))
1479 r
->impl
.free
= libsamplerate_free
;
1480 r
->impl
.update_rates
= libsamplerate_update_rates
;
1481 r
->impl
.resample
= libsamplerate_resample
;
1482 r
->impl
.reset
= libsamplerate_reset
;
1483 r
->impl
.data
= state
;
1489 /*** speex based implementation ***/
1491 static bool speex_is_fixed_point(void) {
1492 static bool result
= false;
1495 float f_out
= -1.0f
, f_in
= 1.0f
;
1496 spx_uint32_t in_len
= 1, out_len
= 1;
1497 SpeexResamplerState
*s
;
1499 pa_assert_se(s
= speex_resampler_init(1, 1, 1,
1500 SPEEX_RESAMPLER_QUALITY_MIN
, NULL
));
1502 /* feed one sample that is too soft for fixed-point speex */
1503 pa_assert_se(speex_resampler_process_float(s
, 0, &f_in
, &in_len
,
1504 &f_out
, &out_len
) == RESAMPLER_ERR_SUCCESS
);
1506 /* expecting sample has been processed, one sample output */
1507 pa_assert_se(in_len
== 1 && out_len
== 1);
1509 /* speex compiled with --enable-fixed-point will output 0.0 due to insufficient precision */
1510 if (fabsf(f_out
) < 0.00001f
)
1513 speex_resampler_destroy(s
);
1520 static unsigned speex_resample_float(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
1522 uint32_t inf
= in_n_frames
, outf
= *out_n_frames
;
1523 SpeexResamplerState
*state
;
1528 pa_assert(out_n_frames
);
1530 state
= r
->impl
.data
;
1532 in
= pa_memblock_acquire_chunk(input
);
1533 out
= pa_memblock_acquire_chunk(output
);
1535 /* Strictly speaking, speex resampler expects its input
1536 * to be normalized to the [-32768.0 .. 32767.0] range.
1537 * This matters if speex has been compiled with --enable-fixed-point,
1538 * because such speex will round the samples to the nearest
1539 * integer. speex with --enable-fixed-point is therefore incompatible
1540 * with PulseAudio's floating-point sample range [-1 .. 1]. speex
1541 * without --enable-fixed-point works fine with this range.
1542 * Care has been taken to call speex_resample_float() only
1543 * for speex compiled without --enable-fixed-point.
1545 pa_assert_se(speex_resampler_process_interleaved_float(state
, in
, &inf
, out
, &outf
) == 0);
1547 pa_memblock_release(input
->memblock
);
1548 pa_memblock_release(output
->memblock
);
1550 pa_assert(inf
== in_n_frames
);
1551 *out_n_frames
= outf
;
1556 static unsigned speex_resample_int(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
1558 uint32_t inf
= in_n_frames
, outf
= *out_n_frames
;
1559 SpeexResamplerState
*state
;
1564 pa_assert(out_n_frames
);
1566 state
= r
->impl
.data
;
1568 in
= pa_memblock_acquire_chunk(input
);
1569 out
= pa_memblock_acquire_chunk(output
);
1571 pa_assert_se(speex_resampler_process_interleaved_int(state
, in
, &inf
, out
, &outf
) == 0);
1573 pa_memblock_release(input
->memblock
);
1574 pa_memblock_release(output
->memblock
);
1576 pa_assert(inf
== in_n_frames
);
1577 *out_n_frames
= outf
;
1582 static void speex_update_rates(pa_resampler
*r
) {
1583 SpeexResamplerState
*state
;
1586 state
= r
->impl
.data
;
1588 pa_assert_se(speex_resampler_set_rate(state
, r
->i_ss
.rate
, r
->o_ss
.rate
) == 0);
1591 static void speex_reset(pa_resampler
*r
) {
1592 SpeexResamplerState
*state
;
1595 state
= r
->impl
.data
;
1597 pa_assert_se(speex_resampler_reset_mem(state
) == 0);
1600 static void speex_free(pa_resampler
*r
) {
1601 SpeexResamplerState
*state
;
1604 state
= r
->impl
.data
;
1608 speex_resampler_destroy(state
);
1611 static int speex_init(pa_resampler
*r
) {
1613 SpeexResamplerState
*state
;
1617 r
->impl
.free
= speex_free
;
1618 r
->impl
.update_rates
= speex_update_rates
;
1619 r
->impl
.reset
= speex_reset
;
1621 if (r
->method
>= PA_RESAMPLER_SPEEX_FIXED_BASE
&& r
->method
<= PA_RESAMPLER_SPEEX_FIXED_MAX
) {
1623 q
= r
->method
- PA_RESAMPLER_SPEEX_FIXED_BASE
;
1624 r
->impl
.resample
= speex_resample_int
;
1627 pa_assert(r
->method
>= PA_RESAMPLER_SPEEX_FLOAT_BASE
&& r
->method
<= PA_RESAMPLER_SPEEX_FLOAT_MAX
);
1629 q
= r
->method
- PA_RESAMPLER_SPEEX_FLOAT_BASE
;
1630 r
->impl
.resample
= speex_resample_float
;
1633 pa_log_info("Choosing speex quality setting %i.", q
);
1635 if (!(state
= speex_resampler_init(r
->work_channels
, r
->i_ss
.rate
, r
->o_ss
.rate
, q
, &err
)))
1638 r
->impl
.data
= state
;
1644 /* Trivial implementation */
1646 static unsigned trivial_resample(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
1647 unsigned i_index
, o_index
;
1649 struct trivial_data
*trivial_data
;
1654 pa_assert(out_n_frames
);
1656 trivial_data
= r
->impl
.data
;
1658 src
= pa_memblock_acquire_chunk(input
);
1659 dst
= pa_memblock_acquire_chunk(output
);
1661 for (o_index
= 0;; o_index
++, trivial_data
->o_counter
++) {
1662 i_index
= ((uint64_t) trivial_data
->o_counter
* r
->i_ss
.rate
) / r
->o_ss
.rate
;
1663 i_index
= i_index
> trivial_data
->i_counter
? i_index
- trivial_data
->i_counter
: 0;
1665 if (i_index
>= in_n_frames
)
1668 pa_assert_fp(o_index
* r
->w_fz
< pa_memblock_get_length(output
->memblock
));
1670 memcpy((uint8_t*) dst
+ r
->w_fz
* o_index
, (uint8_t*) src
+ r
->w_fz
* i_index
, (int) r
->w_fz
);
1673 pa_memblock_release(input
->memblock
);
1674 pa_memblock_release(output
->memblock
);
1676 *out_n_frames
= o_index
;
1678 trivial_data
->i_counter
+= in_n_frames
;
1680 /* Normalize counters */
1681 while (trivial_data
->i_counter
>= r
->i_ss
.rate
) {
1682 pa_assert(trivial_data
->o_counter
>= r
->o_ss
.rate
);
1684 trivial_data
->i_counter
-= r
->i_ss
.rate
;
1685 trivial_data
->o_counter
-= r
->o_ss
.rate
;
1691 static void trivial_update_rates_or_reset(pa_resampler
*r
) {
1692 struct trivial_data
*trivial_data
;
1695 trivial_data
= r
->impl
.data
;
1697 trivial_data
->i_counter
= 0;
1698 trivial_data
->o_counter
= 0;
1701 static int trivial_init(pa_resampler
*r
) {
1702 struct trivial_data
*trivial_data
;
1705 trivial_data
= pa_xnew0(struct trivial_data
, 1);
1707 r
->impl
.resample
= trivial_resample
;
1708 r
->impl
.update_rates
= trivial_update_rates_or_reset
;
1709 r
->impl
.reset
= trivial_update_rates_or_reset
;
1710 r
->impl
.data
= trivial_data
;
1715 /* Peak finder implementation */
1717 static unsigned peaks_resample(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
1718 unsigned c
, o_index
= 0;
1719 unsigned i
, i_end
= 0;
1721 struct peaks_data
*peaks_data
;
1726 pa_assert(out_n_frames
);
1728 peaks_data
= r
->impl
.data
;
1729 src
= pa_memblock_acquire_chunk(input
);
1730 dst
= pa_memblock_acquire_chunk(output
);
1732 i
= ((uint64_t) peaks_data
->o_counter
* r
->i_ss
.rate
) / r
->o_ss
.rate
;
1733 i
= i
> peaks_data
->i_counter
? i
- peaks_data
->i_counter
: 0;
1735 while (i_end
< in_n_frames
) {
1736 i_end
= ((uint64_t) (peaks_data
->o_counter
+ 1) * r
->i_ss
.rate
) / r
->o_ss
.rate
;
1737 i_end
= i_end
> peaks_data
->i_counter
? i_end
- peaks_data
->i_counter
: 0;
1739 pa_assert_fp(o_index
* r
->w_fz
< pa_memblock_get_length(output
->memblock
));
1741 /* 1ch float is treated separately, because that is the common case */
1742 if (r
->work_channels
== 1 && r
->work_format
== PA_SAMPLE_FLOAT32NE
) {
1743 float *s
= (float*) src
+ i
;
1744 float *d
= (float*) dst
+ o_index
;
1746 for (; i
< i_end
&& i
< in_n_frames
; i
++) {
1747 float n
= fabsf(*s
++);
1749 if (n
> peaks_data
->max_f
[0])
1750 peaks_data
->max_f
[0] = n
;
1754 *d
= peaks_data
->max_f
[0];
1755 peaks_data
->max_f
[0] = 0;
1756 o_index
++, peaks_data
->o_counter
++;
1758 } else if (r
->work_format
== PA_SAMPLE_S16NE
) {
1759 int16_t *s
= (int16_t*) src
+ r
->work_channels
* i
;
1760 int16_t *d
= (int16_t*) dst
+ r
->work_channels
* o_index
;
1762 for (; i
< i_end
&& i
< in_n_frames
; i
++)
1763 for (c
= 0; c
< r
->work_channels
; c
++) {
1764 int16_t n
= abs(*s
++);
1766 if (n
> peaks_data
->max_i
[c
])
1767 peaks_data
->max_i
[c
] = n
;
1771 for (c
= 0; c
< r
->work_channels
; c
++, d
++) {
1772 *d
= peaks_data
->max_i
[c
];
1773 peaks_data
->max_i
[c
] = 0;
1775 o_index
++, peaks_data
->o_counter
++;
1778 float *s
= (float*) src
+ r
->work_channels
* i
;
1779 float *d
= (float*) dst
+ r
->work_channels
* o_index
;
1781 for (; i
< i_end
&& i
< in_n_frames
; i
++)
1782 for (c
= 0; c
< r
->work_channels
; c
++) {
1783 float n
= fabsf(*s
++);
1785 if (n
> peaks_data
->max_f
[c
])
1786 peaks_data
->max_f
[c
] = n
;
1790 for (c
= 0; c
< r
->work_channels
; c
++, d
++) {
1791 *d
= peaks_data
->max_f
[c
];
1792 peaks_data
->max_f
[c
] = 0;
1794 o_index
++, peaks_data
->o_counter
++;
1799 pa_memblock_release(input
->memblock
);
1800 pa_memblock_release(output
->memblock
);
1802 *out_n_frames
= o_index
;
1804 peaks_data
->i_counter
+= in_n_frames
;
1806 /* Normalize counters */
1807 while (peaks_data
->i_counter
>= r
->i_ss
.rate
) {
1808 pa_assert(peaks_data
->o_counter
>= r
->o_ss
.rate
);
1810 peaks_data
->i_counter
-= r
->i_ss
.rate
;
1811 peaks_data
->o_counter
-= r
->o_ss
.rate
;
1817 static void peaks_update_rates_or_reset(pa_resampler
*r
) {
1818 struct peaks_data
*peaks_data
;
1821 peaks_data
= r
->impl
.data
;
1823 peaks_data
->i_counter
= 0;
1824 peaks_data
->o_counter
= 0;
1827 static int peaks_init(pa_resampler
*r
) {
1828 struct peaks_data
*peaks_data
;
1830 pa_assert(r
->i_ss
.rate
>= r
->o_ss
.rate
);
1831 pa_assert(r
->work_format
== PA_SAMPLE_S16NE
|| r
->work_format
== PA_SAMPLE_FLOAT32NE
);
1833 peaks_data
= pa_xnew0(struct peaks_data
, 1);
1835 r
->impl
.resample
= peaks_resample
;
1836 r
->impl
.update_rates
= peaks_update_rates_or_reset
;
1837 r
->impl
.reset
= peaks_update_rates_or_reset
;
1838 r
->impl
.data
= peaks_data
;
1843 /*** ffmpeg based implementation ***/
1845 static unsigned ffmpeg_resample(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
1846 unsigned used_frames
= 0, c
;
1847 int previous_consumed_frames
= -1;
1848 struct ffmpeg_data
*ffmpeg_data
;
1853 pa_assert(out_n_frames
);
1855 ffmpeg_data
= r
->impl
.data
;
1857 for (c
= 0; c
< r
->work_channels
; c
++) {
1860 int16_t *p
, *t
, *k
, *q
, *s
;
1861 int consumed_frames
;
1863 /* Allocate a new block */
1864 b
= pa_memblock_new(r
->mempool
, in_n_frames
* sizeof(int16_t));
1865 p
= pa_memblock_acquire(b
);
1867 /* Now copy the input data, splitting up channels */
1868 t
= (int16_t*) pa_memblock_acquire_chunk(input
) + c
;
1870 for (u
= 0; u
< in_n_frames
; u
++) {
1872 t
+= r
->work_channels
;
1875 pa_memblock_release(input
->memblock
);
1877 /* Allocate buffer for the result */
1878 w
= pa_memblock_new(r
->mempool
, *out_n_frames
* sizeof(int16_t));
1879 q
= pa_memblock_acquire(w
);
1882 used_frames
= (unsigned) av_resample(ffmpeg_data
->state
,
1885 (int) in_n_frames
, (int) *out_n_frames
,
1886 c
>= (unsigned) (r
->work_channels
-1));
1888 pa_memblock_release(b
);
1889 pa_memblock_unref(b
);
1891 pa_assert(consumed_frames
<= (int) in_n_frames
);
1892 pa_assert(previous_consumed_frames
== -1 || consumed_frames
== previous_consumed_frames
);
1893 previous_consumed_frames
= consumed_frames
;
1895 /* And place the results in the output buffer */
1896 s
= (int16_t *) pa_memblock_acquire_chunk(output
) + c
;
1897 for (u
= 0; u
< used_frames
; u
++) {
1900 s
+= r
->work_channels
;
1902 pa_memblock_release(output
->memblock
);
1903 pa_memblock_release(w
);
1904 pa_memblock_unref(w
);
1907 *out_n_frames
= used_frames
;
1909 return in_n_frames
- previous_consumed_frames
;
1912 static void ffmpeg_free(pa_resampler
*r
) {
1913 struct ffmpeg_data
*ffmpeg_data
;
1917 ffmpeg_data
= r
->impl
.data
;
1918 if (ffmpeg_data
->state
)
1919 av_resample_close(ffmpeg_data
->state
);
1922 static int ffmpeg_init(pa_resampler
*r
) {
1923 struct ffmpeg_data
*ffmpeg_data
;
1927 ffmpeg_data
= pa_xnew(struct ffmpeg_data
, 1);
1929 /* We could probably implement different quality levels by
1930 * adjusting the filter parameters here. However, ffmpeg
1931 * internally only uses these hardcoded values, so let's use them
1932 * here for now as well until ffmpeg makes this configurable. */
1934 if (!(ffmpeg_data
->state
= av_resample_init((int) r
->o_ss
.rate
, (int) r
->i_ss
.rate
, 16, 10, 0, 0.8)))
1937 r
->impl
.free
= ffmpeg_free
;
1938 r
->impl
.resample
= ffmpeg_resample
;
1939 r
->impl
.data
= (void *) ffmpeg_data
;
1944 /*** copy (noop) implementation ***/
1946 static int copy_init(pa_resampler
*r
) {
1949 pa_assert(r
->o_ss
.rate
== r
->i_ss
.rate
);