2 This file is part of PulseAudio.
4 Copyright 2004-2006 Lennart Poettering
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2.1 of the License,
9 or (at your option) any later version.
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
28 #ifdef HAVE_LIBSAMPLERATE
29 #include <samplerate.h>
33 #include <speex/speex_resampler.h>
36 #include <pulse/xmalloc.h>
37 #include <pulsecore/sconv.h>
38 #include <pulsecore/log.h>
39 #include <pulsecore/macro.h>
40 #include <pulsecore/strbuf.h>
41 #include <pulsecore/remap.h>
42 #include <pulsecore/core-util.h>
43 #include "ffmpeg/avcodec.h"
45 #include "resampler.h"
47 /* Number of samples of extra space we allow the resamplers to return */
48 #define EXTRA_FRAMES 128
51 pa_resample_method_t method
;
52 pa_resample_flags_t flags
;
54 pa_sample_spec i_ss
, o_ss
;
55 pa_channel_map i_cm
, o_cm
;
56 size_t i_fz
, o_fz
, w_sz
;
59 pa_memchunk to_work_format_buf
;
60 pa_memchunk remap_buf
;
61 pa_memchunk resample_buf
;
62 pa_memchunk from_work_format_buf
;
63 unsigned to_work_format_buf_samples
;
64 size_t remap_buf_size
;
65 unsigned resample_buf_samples
;
66 unsigned from_work_format_buf_samples
;
67 bool remap_buf_contains_leftover_data
;
69 pa_sample_format_t work_format
;
71 pa_convert_func_t to_work_format_func
;
72 pa_convert_func_t from_work_format_func
;
77 pa_resampler_impl impl
;
80 struct trivial_data
{ /* data specific to the trivial resampler */
85 struct peaks_data
{ /* data specific to the peak finder pseudo resampler */
89 float max_f
[PA_CHANNELS_MAX
];
90 int16_t max_i
[PA_CHANNELS_MAX
];
93 #ifdef HAVE_LIBSAMPLERATE
94 struct src_data
{ /* data specific to libsamplerate */
100 struct speex_data
{ /* data specific to speex */
101 SpeexResamplerState
* state
;
105 struct ffmpeg_data
{ /* data specific to ffmpeg */
106 struct AVResampleContext
*state
;
107 pa_memchunk buf
[PA_CHANNELS_MAX
];
110 static int copy_init(pa_resampler
*r
);
111 static int trivial_init(pa_resampler
*r
);
113 static int speex_init(pa_resampler
*r
);
115 static int ffmpeg_init(pa_resampler
*r
);
116 static int peaks_init(pa_resampler
*r
);
117 #ifdef HAVE_LIBSAMPLERATE
118 static int libsamplerate_init(pa_resampler
*r
);
121 static void calc_map_table(pa_resampler
*r
);
123 static int (* const init_table
[])(pa_resampler
*r
) = {
124 #ifdef HAVE_LIBSAMPLERATE
125 [PA_RESAMPLER_SRC_SINC_BEST_QUALITY
] = libsamplerate_init
,
126 [PA_RESAMPLER_SRC_SINC_MEDIUM_QUALITY
] = libsamplerate_init
,
127 [PA_RESAMPLER_SRC_SINC_FASTEST
] = libsamplerate_init
,
128 [PA_RESAMPLER_SRC_ZERO_ORDER_HOLD
] = libsamplerate_init
,
129 [PA_RESAMPLER_SRC_LINEAR
] = libsamplerate_init
,
131 [PA_RESAMPLER_SRC_SINC_BEST_QUALITY
] = NULL
,
132 [PA_RESAMPLER_SRC_SINC_MEDIUM_QUALITY
] = NULL
,
133 [PA_RESAMPLER_SRC_SINC_FASTEST
] = NULL
,
134 [PA_RESAMPLER_SRC_ZERO_ORDER_HOLD
] = NULL
,
135 [PA_RESAMPLER_SRC_LINEAR
] = NULL
,
137 [PA_RESAMPLER_TRIVIAL
] = trivial_init
,
139 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+0] = speex_init
,
140 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+1] = speex_init
,
141 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+2] = speex_init
,
142 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+3] = speex_init
,
143 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+4] = speex_init
,
144 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+5] = speex_init
,
145 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+6] = speex_init
,
146 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+7] = speex_init
,
147 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+8] = speex_init
,
148 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+9] = speex_init
,
149 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+10] = speex_init
,
150 [PA_RESAMPLER_SPEEX_FIXED_BASE
+0] = speex_init
,
151 [PA_RESAMPLER_SPEEX_FIXED_BASE
+1] = speex_init
,
152 [PA_RESAMPLER_SPEEX_FIXED_BASE
+2] = speex_init
,
153 [PA_RESAMPLER_SPEEX_FIXED_BASE
+3] = speex_init
,
154 [PA_RESAMPLER_SPEEX_FIXED_BASE
+4] = speex_init
,
155 [PA_RESAMPLER_SPEEX_FIXED_BASE
+5] = speex_init
,
156 [PA_RESAMPLER_SPEEX_FIXED_BASE
+6] = speex_init
,
157 [PA_RESAMPLER_SPEEX_FIXED_BASE
+7] = speex_init
,
158 [PA_RESAMPLER_SPEEX_FIXED_BASE
+8] = speex_init
,
159 [PA_RESAMPLER_SPEEX_FIXED_BASE
+9] = speex_init
,
160 [PA_RESAMPLER_SPEEX_FIXED_BASE
+10] = speex_init
,
162 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+0] = NULL
,
163 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+1] = NULL
,
164 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+2] = NULL
,
165 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+3] = NULL
,
166 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+4] = NULL
,
167 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+5] = NULL
,
168 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+6] = NULL
,
169 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+7] = NULL
,
170 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+8] = NULL
,
171 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+9] = NULL
,
172 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+10] = NULL
,
173 [PA_RESAMPLER_SPEEX_FIXED_BASE
+0] = NULL
,
174 [PA_RESAMPLER_SPEEX_FIXED_BASE
+1] = NULL
,
175 [PA_RESAMPLER_SPEEX_FIXED_BASE
+2] = NULL
,
176 [PA_RESAMPLER_SPEEX_FIXED_BASE
+3] = NULL
,
177 [PA_RESAMPLER_SPEEX_FIXED_BASE
+4] = NULL
,
178 [PA_RESAMPLER_SPEEX_FIXED_BASE
+5] = NULL
,
179 [PA_RESAMPLER_SPEEX_FIXED_BASE
+6] = NULL
,
180 [PA_RESAMPLER_SPEEX_FIXED_BASE
+7] = NULL
,
181 [PA_RESAMPLER_SPEEX_FIXED_BASE
+8] = NULL
,
182 [PA_RESAMPLER_SPEEX_FIXED_BASE
+9] = NULL
,
183 [PA_RESAMPLER_SPEEX_FIXED_BASE
+10] = NULL
,
185 [PA_RESAMPLER_FFMPEG
] = ffmpeg_init
,
186 [PA_RESAMPLER_AUTO
] = NULL
,
187 [PA_RESAMPLER_COPY
] = copy_init
,
188 [PA_RESAMPLER_PEAKS
] = peaks_init
,
191 static pa_resample_method_t
pa_resampler_fix_method(
192 pa_resample_flags_t flags
,
193 pa_resample_method_t method
,
194 const uint32_t rate_a
,
195 const uint32_t rate_b
) {
197 pa_assert(rate_a
> 0 && rate_a
<= PA_RATE_MAX
);
198 pa_assert(rate_b
> 0 && rate_b
<= PA_RATE_MAX
);
199 pa_assert(method
>= 0);
200 pa_assert(method
< PA_RESAMPLER_MAX
);
202 if (!(flags
& PA_RESAMPLER_VARIABLE_RATE
) && rate_a
== rate_b
) {
203 pa_log_info("Forcing resampler 'copy', because of fixed, identical sample rates.");
204 method
= PA_RESAMPLER_COPY
;
207 if (!pa_resample_method_supported(method
)) {
208 pa_log_warn("Support for resampler '%s' not compiled in, reverting to 'auto'.", pa_resample_method_to_string(method
));
209 method
= PA_RESAMPLER_AUTO
;
213 case PA_RESAMPLER_COPY
:
214 if (rate_a
!= rate_b
) {
215 pa_log_info("Resampler 'copy' cannot change sampling rate, reverting to resampler 'auto'.");
218 /* Else fall through */
219 case PA_RESAMPLER_FFMPEG
:
220 if (flags
& PA_RESAMPLER_VARIABLE_RATE
) {
221 pa_log_info("Resampler '%s' cannot do variable rate, reverting to resampler 'auto'.", pa_resample_method_to_string(method
));
222 method
= PA_RESAMPLER_AUTO
;
226 /* The Peaks resampler only supports downsampling.
227 * Revert to auto if we are upsampling */
228 case PA_RESAMPLER_PEAKS
:
229 if (rate_a
< rate_b
) {
230 pa_log_warn("The 'peaks' resampler only supports downsampling, reverting to resampler 'auto'.");
231 method
= PA_RESAMPLER_AUTO
;
239 if (method
== PA_RESAMPLER_AUTO
) {
241 method
= PA_RESAMPLER_SPEEX_FLOAT_BASE
+ 1;
243 if (flags
& PA_RESAMPLER_VARIABLE_RATE
)
244 method
= PA_RESAMPLER_TRIVIAL
;
246 method
= PA_RESAMPLER_FFMPEG
;
253 /* Return true if a is a more precise sample format than b, else return false */
254 static bool sample_format_more_precise(pa_sample_format_t a
, pa_sample_format_t b
) {
255 pa_assert(a
>= 0 && a
< PA_SAMPLE_MAX
);
256 pa_assert(b
>= 0 && b
< PA_SAMPLE_MAX
);
265 case PA_SAMPLE_S16LE
:
266 case PA_SAMPLE_S16BE
:
267 if (b
== PA_SAMPLE_ULAW
|| b
== PA_SAMPLE_ALAW
|| b
== PA_SAMPLE_U8
)
273 case PA_SAMPLE_S24LE
:
274 case PA_SAMPLE_S24BE
:
275 case PA_SAMPLE_S24_32LE
:
276 case PA_SAMPLE_S24_32BE
:
277 if (b
== PA_SAMPLE_ULAW
|| b
== PA_SAMPLE_ALAW
|| b
== PA_SAMPLE_U8
||
278 b
== PA_SAMPLE_S16LE
|| b
== PA_SAMPLE_S16BE
)
284 case PA_SAMPLE_FLOAT32LE
:
285 case PA_SAMPLE_FLOAT32BE
:
286 case PA_SAMPLE_S32LE
:
287 case PA_SAMPLE_S32BE
:
288 if (b
== PA_SAMPLE_FLOAT32LE
|| b
== PA_SAMPLE_FLOAT32BE
||
289 b
== PA_SAMPLE_S32LE
|| b
== PA_SAMPLE_FLOAT32BE
)
300 static pa_sample_format_t
pa_resampler_choose_work_format(
301 pa_resample_method_t method
,
302 pa_sample_format_t a
,
303 pa_sample_format_t b
,
305 pa_sample_format_t work_format
;
307 pa_assert(a
>= 0 && a
< PA_SAMPLE_MAX
);
308 pa_assert(b
>= 0 && b
< PA_SAMPLE_MAX
);
309 pa_assert(method
>= 0);
310 pa_assert(method
< PA_RESAMPLER_MAX
);
312 if (method
>= PA_RESAMPLER_SPEEX_FIXED_BASE
&& method
<= PA_RESAMPLER_SPEEX_FIXED_MAX
)
313 method
= PA_RESAMPLER_SPEEX_FIXED_BASE
;
316 /* This block is for resampling functions that only
317 * support the S16 sample format. */
318 case PA_RESAMPLER_SPEEX_FIXED_BASE
: /* fall through */
319 case PA_RESAMPLER_FFMPEG
:
320 work_format
= PA_SAMPLE_S16NE
;
323 /* This block is for resampling functions that support
324 * any sample format. */
325 case PA_RESAMPLER_COPY
: /* fall through */
326 case PA_RESAMPLER_TRIVIAL
:
327 if (!map_required
&& a
== b
) {
331 /* Else fall trough */
332 case PA_RESAMPLER_PEAKS
:
333 if (a
== PA_SAMPLE_S16NE
|| b
== PA_SAMPLE_S16NE
)
334 work_format
= PA_SAMPLE_S16NE
;
335 else if (sample_format_more_precise(a
, PA_SAMPLE_S16NE
) ||
336 sample_format_more_precise(b
, PA_SAMPLE_S16NE
))
337 work_format
= PA_SAMPLE_FLOAT32NE
;
339 work_format
= PA_SAMPLE_S16NE
;
343 work_format
= PA_SAMPLE_FLOAT32NE
;
349 pa_resampler
* pa_resampler_new(
351 const pa_sample_spec
*a
,
352 const pa_channel_map
*am
,
353 const pa_sample_spec
*b
,
354 const pa_channel_map
*bm
,
355 pa_resample_method_t method
,
356 pa_resample_flags_t flags
) {
358 pa_resampler
*r
= NULL
;
363 pa_assert(pa_sample_spec_valid(a
));
364 pa_assert(pa_sample_spec_valid(b
));
365 pa_assert(method
>= 0);
366 pa_assert(method
< PA_RESAMPLER_MAX
);
368 method
= pa_resampler_fix_method(flags
, method
, a
->rate
, b
->rate
);
370 r
= pa_xnew0(pa_resampler
, 1);
375 /* Fill sample specs */
379 /* set up the remap structure */
380 r
->remap
.i_ss
= &r
->i_ss
;
381 r
->remap
.o_ss
= &r
->o_ss
;
382 r
->remap
.format
= &r
->work_format
;
386 else if (!pa_channel_map_init_auto(&r
->i_cm
, r
->i_ss
.channels
, PA_CHANNEL_MAP_DEFAULT
))
391 else if (!pa_channel_map_init_auto(&r
->o_cm
, r
->o_ss
.channels
, PA_CHANNEL_MAP_DEFAULT
))
394 r
->i_fz
= pa_frame_size(a
);
395 r
->o_fz
= pa_frame_size(b
);
399 pa_log_info("Using resampler '%s'", pa_resample_method_to_string(method
));
401 r
->work_format
= pa_resampler_choose_work_format(method
, a
->format
, b
->format
, r
->map_required
);
403 pa_log_info("Using %s as working format.", pa_sample_format_to_string(r
->work_format
));
405 r
->w_sz
= pa_sample_size_of_format(r
->work_format
);
407 if (r
->i_ss
.format
!= r
->work_format
) {
408 if (r
->work_format
== PA_SAMPLE_FLOAT32NE
) {
409 if (!(r
->to_work_format_func
= pa_get_convert_to_float32ne_function(r
->i_ss
.format
)))
412 pa_assert(r
->work_format
== PA_SAMPLE_S16NE
);
413 if (!(r
->to_work_format_func
= pa_get_convert_to_s16ne_function(r
->i_ss
.format
)))
418 if (r
->o_ss
.format
!= r
->work_format
) {
419 if (r
->work_format
== PA_SAMPLE_FLOAT32NE
) {
420 if (!(r
->from_work_format_func
= pa_get_convert_from_float32ne_function(r
->o_ss
.format
)))
423 pa_assert(r
->work_format
== PA_SAMPLE_S16NE
);
424 if (!(r
->from_work_format_func
= pa_get_convert_from_s16ne_function(r
->o_ss
.format
)))
429 /* initialize implementation */
430 if (init_table
[method
](r
) < 0)
441 void pa_resampler_free(pa_resampler
*r
) {
447 if (r
->to_work_format_buf
.memblock
)
448 pa_memblock_unref(r
->to_work_format_buf
.memblock
);
449 if (r
->remap_buf
.memblock
)
450 pa_memblock_unref(r
->remap_buf
.memblock
);
451 if (r
->resample_buf
.memblock
)
452 pa_memblock_unref(r
->resample_buf
.memblock
);
453 if (r
->from_work_format_buf
.memblock
)
454 pa_memblock_unref(r
->from_work_format_buf
.memblock
);
456 pa_xfree(r
->impl
.data
);
460 void pa_resampler_set_input_rate(pa_resampler
*r
, uint32_t rate
) {
463 pa_assert(r
->impl
.update_rates
);
465 if (r
->i_ss
.rate
== rate
)
470 r
->impl
.update_rates(r
);
473 void pa_resampler_set_output_rate(pa_resampler
*r
, uint32_t rate
) {
476 pa_assert(r
->impl
.update_rates
);
478 if (r
->o_ss
.rate
== rate
)
483 r
->impl
.update_rates(r
);
486 size_t pa_resampler_request(pa_resampler
*r
, size_t out_length
) {
489 /* Let's round up here to make it more likely that the caller will get at
490 * least out_length amount of data from pa_resampler_run().
492 * We don't take the leftover into account here. If we did, then it might
493 * be in theory possible that this function would return 0 and
494 * pa_resampler_run() would also return 0. That could lead to infinite
495 * loops. When the leftover is ignored here, such loops would eventually
496 * terminate, because the leftover would grow each round, finally
497 * surpassing the minimum input threshold of the resampler. */
498 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
;
501 size_t pa_resampler_result(pa_resampler
*r
, size_t in_length
) {
506 /* Let's round up here to ensure that the caller will always allocate big
507 * enough output buffer. */
509 frames
= (in_length
+ r
->i_fz
- 1) / r
->i_fz
;
511 if (r
->remap_buf_contains_leftover_data
)
512 frames
+= r
->remap_buf
.length
/ (r
->w_sz
* r
->o_ss
.channels
);
514 return (((uint64_t) frames
* r
->o_ss
.rate
+ r
->i_ss
.rate
- 1) / r
->i_ss
.rate
) * r
->o_fz
;
517 size_t pa_resampler_max_block_size(pa_resampler
*r
) {
518 size_t block_size_max
;
519 pa_sample_spec max_ss
;
525 block_size_max
= pa_mempool_block_size_max(r
->mempool
);
527 /* We deduce the "largest" sample spec we're using during the
529 max_ss
.channels
= (uint8_t) (PA_MAX(r
->i_ss
.channels
, r
->o_ss
.channels
));
531 /* We silently assume that the format enum is ordered by size */
532 max_ss
.format
= PA_MAX(r
->i_ss
.format
, r
->o_ss
.format
);
533 max_ss
.format
= PA_MAX(max_ss
.format
, r
->work_format
);
535 max_ss
.rate
= PA_MAX(r
->i_ss
.rate
, r
->o_ss
.rate
);
537 max_fs
= pa_frame_size(&max_ss
);
538 frames
= block_size_max
/ max_fs
- EXTRA_FRAMES
;
540 if (r
->remap_buf_contains_leftover_data
)
541 frames
-= r
->remap_buf
.length
/ (r
->w_sz
* r
->o_ss
.channels
);
543 return ((uint64_t) frames
* r
->i_ss
.rate
/ max_ss
.rate
) * r
->i_fz
;
546 void pa_resampler_reset(pa_resampler
*r
) {
552 r
->remap_buf_contains_leftover_data
= false;
555 pa_resample_method_t
pa_resampler_get_method(pa_resampler
*r
) {
561 const pa_channel_map
* pa_resampler_input_channel_map(pa_resampler
*r
) {
567 const pa_sample_spec
* pa_resampler_input_sample_spec(pa_resampler
*r
) {
573 const pa_channel_map
* pa_resampler_output_channel_map(pa_resampler
*r
) {
579 const pa_sample_spec
* pa_resampler_output_sample_spec(pa_resampler
*r
) {
585 static const char * const resample_methods
[] = {
586 "src-sinc-best-quality",
587 "src-sinc-medium-quality",
589 "src-zero-order-hold",
620 const char *pa_resample_method_to_string(pa_resample_method_t m
) {
622 if (m
< 0 || m
>= PA_RESAMPLER_MAX
)
625 return resample_methods
[m
];
628 int pa_resample_method_supported(pa_resample_method_t m
) {
630 if (m
< 0 || m
>= PA_RESAMPLER_MAX
)
633 #ifndef HAVE_LIBSAMPLERATE
634 if (m
<= PA_RESAMPLER_SRC_LINEAR
)
639 if (m
>= PA_RESAMPLER_SPEEX_FLOAT_BASE
&& m
<= PA_RESAMPLER_SPEEX_FLOAT_MAX
)
641 if (m
>= PA_RESAMPLER_SPEEX_FIXED_BASE
&& m
<= PA_RESAMPLER_SPEEX_FIXED_MAX
)
648 pa_resample_method_t
pa_parse_resample_method(const char *string
) {
649 pa_resample_method_t m
;
653 for (m
= 0; m
< PA_RESAMPLER_MAX
; m
++)
654 if (pa_streq(string
, resample_methods
[m
]))
657 if (pa_streq(string
, "speex-fixed"))
658 return PA_RESAMPLER_SPEEX_FIXED_BASE
+ 1;
660 if (pa_streq(string
, "speex-float"))
661 return PA_RESAMPLER_SPEEX_FLOAT_BASE
+ 1;
663 return PA_RESAMPLER_INVALID
;
666 static bool on_left(pa_channel_position_t p
) {
669 p
== PA_CHANNEL_POSITION_FRONT_LEFT
||
670 p
== PA_CHANNEL_POSITION_REAR_LEFT
||
671 p
== PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER
||
672 p
== PA_CHANNEL_POSITION_SIDE_LEFT
||
673 p
== PA_CHANNEL_POSITION_TOP_FRONT_LEFT
||
674 p
== PA_CHANNEL_POSITION_TOP_REAR_LEFT
;
677 static bool on_right(pa_channel_position_t p
) {
680 p
== PA_CHANNEL_POSITION_FRONT_RIGHT
||
681 p
== PA_CHANNEL_POSITION_REAR_RIGHT
||
682 p
== PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER
||
683 p
== PA_CHANNEL_POSITION_SIDE_RIGHT
||
684 p
== PA_CHANNEL_POSITION_TOP_FRONT_RIGHT
||
685 p
== PA_CHANNEL_POSITION_TOP_REAR_RIGHT
;
688 static bool on_center(pa_channel_position_t p
) {
691 p
== PA_CHANNEL_POSITION_FRONT_CENTER
||
692 p
== PA_CHANNEL_POSITION_REAR_CENTER
||
693 p
== PA_CHANNEL_POSITION_TOP_CENTER
||
694 p
== PA_CHANNEL_POSITION_TOP_FRONT_CENTER
||
695 p
== PA_CHANNEL_POSITION_TOP_REAR_CENTER
;
698 static bool on_lfe(pa_channel_position_t p
) {
700 p
== PA_CHANNEL_POSITION_LFE
;
703 static bool on_front(pa_channel_position_t p
) {
705 p
== PA_CHANNEL_POSITION_FRONT_LEFT
||
706 p
== PA_CHANNEL_POSITION_FRONT_RIGHT
||
707 p
== PA_CHANNEL_POSITION_FRONT_CENTER
||
708 p
== PA_CHANNEL_POSITION_TOP_FRONT_LEFT
||
709 p
== PA_CHANNEL_POSITION_TOP_FRONT_RIGHT
||
710 p
== PA_CHANNEL_POSITION_TOP_FRONT_CENTER
||
711 p
== PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER
||
712 p
== PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER
;
715 static bool on_rear(pa_channel_position_t p
) {
717 p
== PA_CHANNEL_POSITION_REAR_LEFT
||
718 p
== PA_CHANNEL_POSITION_REAR_RIGHT
||
719 p
== PA_CHANNEL_POSITION_REAR_CENTER
||
720 p
== PA_CHANNEL_POSITION_TOP_REAR_LEFT
||
721 p
== PA_CHANNEL_POSITION_TOP_REAR_RIGHT
||
722 p
== PA_CHANNEL_POSITION_TOP_REAR_CENTER
;
725 static bool on_side(pa_channel_position_t p
) {
727 p
== PA_CHANNEL_POSITION_SIDE_LEFT
||
728 p
== PA_CHANNEL_POSITION_SIDE_RIGHT
||
729 p
== PA_CHANNEL_POSITION_TOP_CENTER
;
739 static int front_rear_side(pa_channel_position_t p
) {
749 static void calc_map_table(pa_resampler
*r
) {
752 bool ic_connected
[PA_CHANNELS_MAX
];
760 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
)))))
765 n_oc
= r
->o_ss
.channels
;
766 n_ic
= r
->i_ss
.channels
;
768 memset(m
->map_table_f
, 0, sizeof(m
->map_table_f
));
769 memset(m
->map_table_i
, 0, sizeof(m
->map_table_i
));
771 memset(ic_connected
, 0, sizeof(ic_connected
));
772 remix
= (r
->flags
& (PA_RESAMPLER_NO_REMAP
| PA_RESAMPLER_NO_REMIX
)) == 0;
774 if (r
->flags
& PA_RESAMPLER_NO_REMAP
) {
777 for (oc
= 0; oc
< PA_MIN(n_ic
, n_oc
); oc
++)
778 m
->map_table_f
[oc
][oc
] = 1.0f
;
780 } else if (r
->flags
& PA_RESAMPLER_NO_REMIX
) {
782 for (oc
= 0; oc
< n_oc
; oc
++) {
783 pa_channel_position_t b
= r
->o_cm
.map
[oc
];
785 for (ic
= 0; ic
< n_ic
; ic
++) {
786 pa_channel_position_t a
= r
->i_cm
.map
[ic
];
788 /* We shall not do any remixing. Hence, just check by name */
790 m
->map_table_f
[oc
][ic
] = 1.0f
;
795 /* OK, we shall do the full monty: upmixing and downmixing. Our
796 * algorithm is relatively simple, does not do spacialization, delay
797 * elements or apply lowpass filters for LFE. Patches are always
798 * welcome, though. Oh, and it doesn't do any matrix decoding. (Which
799 * probably wouldn't make any sense anyway.)
801 * This code is not idempotent: downmixing an upmixed stereo stream is
802 * not identical to the original. The volume will not match, and the
803 * two channels will be a linear combination of both.
805 * This is loosely based on random suggestions found on the Internet,
807 * http://www.halfgaar.net/surround-sound-in-linux and the alsa upmix
810 * The algorithm works basically like this:
812 * 1) Connect all channels with matching names.
815 * S:Mono: Copy into all D:channels
816 * D:Mono: Avg all S:channels
818 * 3) Mix D:Left, D:Right:
819 * D:Left: If not connected, avg all S:Left
820 * D:Right: If not connected, avg all S:Right
823 * If not connected, avg all S:Center
824 * If still not connected, avg all S:Left, S:Right
827 * If not connected, avg all S:*
829 * 6) Make sure S:Left/S:Right is used: S:Left/S:Right: If not
830 * connected, mix into all D:left and all D:right channels. Gain is
833 * 7) Make sure S:Center, S:LFE is used:
835 * S:Center, S:LFE: If not connected, mix into all D:left, all
836 * D:right, all D:center channels. Gain is 0.5 for center and 0.375
837 * for LFE. C-front is only mixed into L-front/R-front if available,
838 * otherwise into all L/R channels. Similarly for C-rear.
840 * 8) Normalize each row in the matrix such that the sum for each row is
841 * not larger than 1.0 in order to avoid clipping.
843 * S: and D: shall relate to the source resp. destination channels.
845 * Rationale: 1, 2 are probably obvious. For 3: this copies front to
846 * rear if needed. For 4: we try to find some suitable C source for C,
847 * if we don't find any, we avg L and R. For 5: LFE is mixed from all
848 * channels. For 6: the rear channels should not be dropped entirely,
849 * however have only minimal impact. For 7: movies usually encode
850 * speech on the center channel. Thus we have to make sure this channel
851 * is distributed to L and R if not available in the output. Also, LFE
852 * is used to achieve a greater dynamic range, and thus we should try
853 * to do our best to pass it to L+R.
860 ic_unconnected_left
= 0,
861 ic_unconnected_right
= 0,
862 ic_unconnected_center
= 0,
863 ic_unconnected_lfe
= 0;
864 bool ic_unconnected_center_mixed_in
= 0;
868 for (ic
= 0; ic
< n_ic
; ic
++) {
869 if (on_left(r
->i_cm
.map
[ic
]))
871 if (on_right(r
->i_cm
.map
[ic
]))
873 if (on_center(r
->i_cm
.map
[ic
]))
877 for (oc
= 0; oc
< n_oc
; oc
++) {
878 bool oc_connected
= false;
879 pa_channel_position_t b
= r
->o_cm
.map
[oc
];
881 for (ic
= 0; ic
< n_ic
; ic
++) {
882 pa_channel_position_t a
= r
->i_cm
.map
[ic
];
884 if (a
== b
|| a
== PA_CHANNEL_POSITION_MONO
) {
885 m
->map_table_f
[oc
][ic
] = 1.0f
;
888 ic_connected
[ic
] = true;
890 else if (b
== PA_CHANNEL_POSITION_MONO
) {
891 m
->map_table_f
[oc
][ic
] = 1.0f
/ (float) n_ic
;
894 ic_connected
[ic
] = true;
899 /* Try to find matching input ports for this output port */
903 /* We are not connected and on the left side, let's
904 * average all left side input channels. */
907 for (ic
= 0; ic
< n_ic
; ic
++)
908 if (on_left(r
->i_cm
.map
[ic
])) {
909 m
->map_table_f
[oc
][ic
] = 1.0f
/ (float) ic_left
;
910 ic_connected
[ic
] = true;
913 /* We ignore the case where there is no left input channel.
914 * Something is really wrong in this case anyway. */
916 } else if (on_right(b
)) {
918 /* We are not connected and on the right side, let's
919 * average all right side input channels. */
922 for (ic
= 0; ic
< n_ic
; ic
++)
923 if (on_right(r
->i_cm
.map
[ic
])) {
924 m
->map_table_f
[oc
][ic
] = 1.0f
/ (float) ic_right
;
925 ic_connected
[ic
] = true;
928 /* We ignore the case where there is no right input
929 * channel. Something is really wrong in this case anyway.
932 } else if (on_center(b
)) {
936 /* We are not connected and at the center. Let's average
937 * all center input channels. */
939 for (ic
= 0; ic
< n_ic
; ic
++)
940 if (on_center(r
->i_cm
.map
[ic
])) {
941 m
->map_table_f
[oc
][ic
] = 1.0f
/ (float) ic_center
;
942 ic_connected
[ic
] = true;
945 } else if (ic_left
+ ic_right
> 0) {
947 /* Hmm, no center channel around, let's synthesize it
948 * by mixing L and R.*/
950 for (ic
= 0; ic
< n_ic
; ic
++)
951 if (on_left(r
->i_cm
.map
[ic
]) || on_right(r
->i_cm
.map
[ic
])) {
952 m
->map_table_f
[oc
][ic
] = 1.0f
/ (float) (ic_left
+ ic_right
);
953 ic_connected
[ic
] = true;
957 /* We ignore the case where there is not even a left or
958 * right input channel. Something is really wrong in this
961 } else if (on_lfe(b
) && !(r
->flags
& PA_RESAMPLER_NO_LFE
)) {
963 /* We are not connected and an LFE. Let's average all
964 * channels for LFE. */
966 for (ic
= 0; ic
< n_ic
; ic
++)
967 m
->map_table_f
[oc
][ic
] = 1.0f
/ (float) n_ic
;
969 /* Please note that a channel connected to LFE doesn't
970 * really count as connected. */
975 for (ic
= 0; ic
< n_ic
; ic
++) {
976 pa_channel_position_t a
= r
->i_cm
.map
[ic
];
978 if (ic_connected
[ic
])
982 ic_unconnected_left
++;
983 else if (on_right(a
))
984 ic_unconnected_right
++;
985 else if (on_center(a
))
986 ic_unconnected_center
++;
988 ic_unconnected_lfe
++;
991 for (ic
= 0; ic
< n_ic
; ic
++) {
992 pa_channel_position_t a
= r
->i_cm
.map
[ic
];
994 if (ic_connected
[ic
])
997 for (oc
= 0; oc
< n_oc
; oc
++) {
998 pa_channel_position_t b
= r
->o_cm
.map
[oc
];
1000 if (on_left(a
) && on_left(b
))
1001 m
->map_table_f
[oc
][ic
] = (1.f
/9.f
) / (float) ic_unconnected_left
;
1003 else if (on_right(a
) && on_right(b
))
1004 m
->map_table_f
[oc
][ic
] = (1.f
/9.f
) / (float) ic_unconnected_right
;
1006 else if (on_center(a
) && on_center(b
)) {
1007 m
->map_table_f
[oc
][ic
] = (1.f
/9.f
) / (float) ic_unconnected_center
;
1008 ic_unconnected_center_mixed_in
= true;
1010 } else if (on_lfe(a
) && !(r
->flags
& PA_RESAMPLER_NO_LFE
))
1011 m
->map_table_f
[oc
][ic
] = .375f
/ (float) ic_unconnected_lfe
;
1015 if (ic_unconnected_center
> 0 && !ic_unconnected_center_mixed_in
) {
1016 unsigned ncenter
[PA_CHANNELS_MAX
];
1017 bool found_frs
[PA_CHANNELS_MAX
];
1019 memset(ncenter
, 0, sizeof(ncenter
));
1020 memset(found_frs
, 0, sizeof(found_frs
));
1022 /* Hmm, as it appears there was no center channel we
1023 could mix our center channel in. In this case, mix it into
1024 left and right. Using .5 as the factor. */
1026 for (ic
= 0; ic
< n_ic
; ic
++) {
1028 if (ic_connected
[ic
])
1031 if (!on_center(r
->i_cm
.map
[ic
]))
1034 for (oc
= 0; oc
< n_oc
; oc
++) {
1036 if (!on_left(r
->o_cm
.map
[oc
]) && !on_right(r
->o_cm
.map
[oc
]))
1039 if (front_rear_side(r
->i_cm
.map
[ic
]) == front_rear_side(r
->o_cm
.map
[oc
])) {
1040 found_frs
[ic
] = true;
1045 for (oc
= 0; oc
< n_oc
; oc
++) {
1047 if (!on_left(r
->o_cm
.map
[oc
]) && !on_right(r
->o_cm
.map
[oc
]))
1050 if (!found_frs
[ic
] || front_rear_side(r
->i_cm
.map
[ic
]) == front_rear_side(r
->o_cm
.map
[oc
]))
1055 for (oc
= 0; oc
< n_oc
; oc
++) {
1057 if (!on_left(r
->o_cm
.map
[oc
]) && !on_right(r
->o_cm
.map
[oc
]))
1060 if (ncenter
[oc
] <= 0)
1063 for (ic
= 0; ic
< n_ic
; ic
++) {
1065 if (!on_center(r
->i_cm
.map
[ic
]))
1068 if (!found_frs
[ic
] || front_rear_side(r
->i_cm
.map
[ic
]) == front_rear_side(r
->o_cm
.map
[oc
]))
1069 m
->map_table_f
[oc
][ic
] = .5f
/ (float) ncenter
[oc
];
1075 for (oc
= 0; oc
< n_oc
; oc
++) {
1077 for (ic
= 0; ic
< n_ic
; ic
++)
1078 sum
+= m
->map_table_f
[oc
][ic
];
1081 for (ic
= 0; ic
< n_ic
; ic
++)
1082 m
->map_table_f
[oc
][ic
] /= sum
;
1085 /* make an 16:16 int version of the matrix */
1086 for (oc
= 0; oc
< n_oc
; oc
++)
1087 for (ic
= 0; ic
< n_ic
; ic
++)
1088 m
->map_table_i
[oc
][ic
] = (int32_t) (m
->map_table_f
[oc
][ic
] * 0x10000);
1090 s
= pa_strbuf_new();
1092 pa_strbuf_printf(s
, " ");
1093 for (ic
= 0; ic
< n_ic
; ic
++)
1094 pa_strbuf_printf(s
, " I%02u ", ic
);
1095 pa_strbuf_puts(s
, "\n +");
1097 for (ic
= 0; ic
< n_ic
; ic
++)
1098 pa_strbuf_printf(s
, "------");
1099 pa_strbuf_puts(s
, "\n");
1101 for (oc
= 0; oc
< n_oc
; oc
++) {
1102 pa_strbuf_printf(s
, "O%02u |", oc
);
1104 for (ic
= 0; ic
< n_ic
; ic
++)
1105 pa_strbuf_printf(s
, " %1.3f", m
->map_table_f
[oc
][ic
]);
1107 pa_strbuf_puts(s
, "\n");
1110 pa_log_debug("Channel matrix:\n%s", t
= pa_strbuf_tostring_free(s
));
1113 /* initialize the remapping function */
1117 static pa_memchunk
* convert_to_work_format(pa_resampler
*r
, pa_memchunk
*input
) {
1123 pa_assert(input
->memblock
);
1125 /* Convert the incoming sample into the work sample format and place them
1126 * in to_work_format_buf. */
1128 if (!r
->to_work_format_func
|| !input
->length
)
1131 n_samples
= (unsigned) ((input
->length
/ r
->i_fz
) * r
->i_ss
.channels
);
1133 r
->to_work_format_buf
.index
= 0;
1134 r
->to_work_format_buf
.length
= r
->w_sz
* n_samples
;
1136 if (!r
->to_work_format_buf
.memblock
|| r
->to_work_format_buf_samples
< n_samples
) {
1137 if (r
->to_work_format_buf
.memblock
)
1138 pa_memblock_unref(r
->to_work_format_buf
.memblock
);
1140 r
->to_work_format_buf_samples
= n_samples
;
1141 r
->to_work_format_buf
.memblock
= pa_memblock_new(r
->mempool
, r
->to_work_format_buf
.length
);
1144 src
= pa_memblock_acquire_chunk(input
);
1145 dst
= pa_memblock_acquire(r
->to_work_format_buf
.memblock
);
1147 r
->to_work_format_func(n_samples
, src
, dst
);
1149 pa_memblock_release(input
->memblock
);
1150 pa_memblock_release(r
->to_work_format_buf
.memblock
);
1152 return &r
->to_work_format_buf
;
1155 static pa_memchunk
*remap_channels(pa_resampler
*r
, pa_memchunk
*input
) {
1156 unsigned in_n_samples
, out_n_samples
, in_n_frames
, out_n_frames
;
1158 size_t leftover_length
= 0;
1163 pa_assert(input
->memblock
);
1165 /* Remap channels and place the result in remap_buf. There may be leftover
1166 * data in the beginning of remap_buf. The leftover data is already
1167 * remapped, so it's not part of the input, it's part of the output. */
1169 have_leftover
= r
->remap_buf_contains_leftover_data
;
1170 r
->remap_buf_contains_leftover_data
= false;
1172 if (!have_leftover
&& (!r
->map_required
|| input
->length
<= 0))
1174 else if (input
->length
<= 0)
1175 return &r
->remap_buf
;
1177 in_n_samples
= (unsigned) (input
->length
/ r
->w_sz
);
1178 in_n_frames
= out_n_frames
= in_n_samples
/ r
->i_ss
.channels
;
1180 if (have_leftover
) {
1181 leftover_length
= r
->remap_buf
.length
;
1182 out_n_frames
+= leftover_length
/ (r
->w_sz
* r
->o_ss
.channels
);
1185 out_n_samples
= out_n_frames
* r
->o_ss
.channels
;
1186 r
->remap_buf
.length
= out_n_samples
* r
->w_sz
;
1188 if (have_leftover
) {
1189 if (r
->remap_buf_size
< r
->remap_buf
.length
) {
1190 pa_memblock
*new_block
= pa_memblock_new(r
->mempool
, r
->remap_buf
.length
);
1192 src
= pa_memblock_acquire(r
->remap_buf
.memblock
);
1193 dst
= pa_memblock_acquire(new_block
);
1194 memcpy(dst
, src
, leftover_length
);
1195 pa_memblock_release(r
->remap_buf
.memblock
);
1196 pa_memblock_release(new_block
);
1198 pa_memblock_unref(r
->remap_buf
.memblock
);
1199 r
->remap_buf
.memblock
= new_block
;
1200 r
->remap_buf_size
= r
->remap_buf
.length
;
1204 if (!r
->remap_buf
.memblock
|| r
->remap_buf_size
< r
->remap_buf
.length
) {
1205 if (r
->remap_buf
.memblock
)
1206 pa_memblock_unref(r
->remap_buf
.memblock
);
1208 r
->remap_buf_size
= r
->remap_buf
.length
;
1209 r
->remap_buf
.memblock
= pa_memblock_new(r
->mempool
, r
->remap_buf
.length
);
1213 src
= pa_memblock_acquire_chunk(input
);
1214 dst
= (uint8_t *) pa_memblock_acquire(r
->remap_buf
.memblock
) + leftover_length
;
1216 if (r
->map_required
) {
1217 pa_remap_t
*remap
= &r
->remap
;
1219 pa_assert(remap
->do_remap
);
1220 remap
->do_remap(remap
, dst
, src
, in_n_frames
);
1223 memcpy(dst
, src
, input
->length
);
1225 pa_memblock_release(input
->memblock
);
1226 pa_memblock_release(r
->remap_buf
.memblock
);
1228 return &r
->remap_buf
;
1231 static pa_memchunk
*resample(pa_resampler
*r
, pa_memchunk
*input
) {
1232 unsigned in_n_frames
, in_n_samples
;
1233 unsigned out_n_frames
, out_n_samples
;
1238 /* Resample the data and place the result in resample_buf. */
1240 if (!r
->impl
.resample
|| !input
->length
)
1243 in_n_samples
= (unsigned) (input
->length
/ r
->w_sz
);
1244 in_n_frames
= (unsigned) (in_n_samples
/ r
->o_ss
.channels
);
1246 out_n_frames
= ((in_n_frames
*r
->o_ss
.rate
)/r
->i_ss
.rate
)+EXTRA_FRAMES
;
1247 out_n_samples
= out_n_frames
* r
->o_ss
.channels
;
1249 r
->resample_buf
.index
= 0;
1250 r
->resample_buf
.length
= r
->w_sz
* out_n_samples
;
1252 if (!r
->resample_buf
.memblock
|| r
->resample_buf_samples
< out_n_samples
) {
1253 if (r
->resample_buf
.memblock
)
1254 pa_memblock_unref(r
->resample_buf
.memblock
);
1256 r
->resample_buf_samples
= out_n_samples
;
1257 r
->resample_buf
.memblock
= pa_memblock_new(r
->mempool
, r
->resample_buf
.length
);
1260 r
->impl
.resample(r
, input
, in_n_frames
, &r
->resample_buf
, &out_n_frames
);
1261 r
->resample_buf
.length
= out_n_frames
* r
->w_sz
* r
->o_ss
.channels
;
1263 return &r
->resample_buf
;
1266 static pa_memchunk
*convert_from_work_format(pa_resampler
*r
, pa_memchunk
*input
) {
1267 unsigned n_samples
, n_frames
;
1273 /* Convert the data into the correct sample type and place the result in
1274 * from_work_format_buf. */
1276 if (!r
->from_work_format_func
|| !input
->length
)
1279 n_samples
= (unsigned) (input
->length
/ r
->w_sz
);
1280 n_frames
= n_samples
/ r
->o_ss
.channels
;
1282 r
->from_work_format_buf
.index
= 0;
1283 r
->from_work_format_buf
.length
= r
->o_fz
* n_frames
;
1285 if (!r
->from_work_format_buf
.memblock
|| r
->from_work_format_buf_samples
< n_samples
) {
1286 if (r
->from_work_format_buf
.memblock
)
1287 pa_memblock_unref(r
->from_work_format_buf
.memblock
);
1289 r
->from_work_format_buf_samples
= n_samples
;
1290 r
->from_work_format_buf
.memblock
= pa_memblock_new(r
->mempool
, r
->from_work_format_buf
.length
);
1293 src
= pa_memblock_acquire_chunk(input
);
1294 dst
= pa_memblock_acquire(r
->from_work_format_buf
.memblock
);
1295 r
->from_work_format_func(n_samples
, src
, dst
);
1296 pa_memblock_release(input
->memblock
);
1297 pa_memblock_release(r
->from_work_format_buf
.memblock
);
1299 return &r
->from_work_format_buf
;
1302 void pa_resampler_run(pa_resampler
*r
, const pa_memchunk
*in
, pa_memchunk
*out
) {
1308 pa_assert(in
->length
);
1309 pa_assert(in
->memblock
);
1310 pa_assert(in
->length
% r
->i_fz
== 0);
1312 buf
= (pa_memchunk
*) in
;
1313 buf
= convert_to_work_format(r
, buf
);
1314 buf
= remap_channels(r
, buf
);
1315 buf
= resample(r
, buf
);
1318 buf
= convert_from_work_format(r
, buf
);
1322 pa_memblock_ref(buf
->memblock
);
1324 pa_memchunk_reset(buf
);
1326 pa_memchunk_reset(out
);
1329 static void save_leftover(pa_resampler
*r
, void *buf
, size_t len
) {
1336 /* Store the leftover to remap_buf. */
1338 r
->remap_buf
.length
= len
;
1340 if (!r
->remap_buf
.memblock
|| r
->remap_buf_size
< r
->remap_buf
.length
) {
1341 if (r
->remap_buf
.memblock
)
1342 pa_memblock_unref(r
->remap_buf
.memblock
);
1344 r
->remap_buf_size
= r
->remap_buf
.length
;
1345 r
->remap_buf
.memblock
= pa_memblock_new(r
->mempool
, r
->remap_buf
.length
);
1348 dst
= pa_memblock_acquire(r
->remap_buf
.memblock
);
1349 memcpy(dst
, buf
, r
->remap_buf
.length
);
1350 pa_memblock_release(r
->remap_buf
.memblock
);
1352 r
->remap_buf_contains_leftover_data
= true;
1355 /*** libsamplerate based implementation ***/
1357 #ifdef HAVE_LIBSAMPLERATE
1358 static void libsamplerate_resample(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
1360 struct src_data
*libsamplerate_data
;
1365 pa_assert(out_n_frames
);
1367 libsamplerate_data
= r
->impl
.data
;
1368 memset(&data
, 0, sizeof(data
));
1370 data
.data_in
= pa_memblock_acquire_chunk(input
);
1371 data
.input_frames
= (long int) in_n_frames
;
1373 data
.data_out
= pa_memblock_acquire_chunk(output
);
1374 data
.output_frames
= (long int) *out_n_frames
;
1376 data
.src_ratio
= (double) r
->o_ss
.rate
/ r
->i_ss
.rate
;
1377 data
.end_of_input
= 0;
1379 pa_assert_se(src_process(libsamplerate_data
->state
, &data
) == 0);
1381 if (data
.input_frames_used
< in_n_frames
) {
1382 void *leftover_data
= data
.data_in
+ data
.input_frames_used
* r
->o_ss
.channels
;
1383 size_t leftover_length
= (in_n_frames
- data
.input_frames_used
) * sizeof(float) * r
->o_ss
.channels
;
1385 save_leftover(r
, leftover_data
, leftover_length
);
1388 pa_memblock_release(input
->memblock
);
1389 pa_memblock_release(output
->memblock
);
1391 *out_n_frames
= (unsigned) data
.output_frames_gen
;
1394 static void libsamplerate_update_rates(pa_resampler
*r
) {
1395 struct src_data
*libsamplerate_data
;
1398 libsamplerate_data
= r
->impl
.data
;
1399 pa_assert_se(src_set_ratio(libsamplerate_data
->state
, (double) r
->o_ss
.rate
/ r
->i_ss
.rate
) == 0);
1402 static void libsamplerate_reset(pa_resampler
*r
) {
1403 struct src_data
*libsamplerate_data
;
1406 libsamplerate_data
= r
->impl
.data
;
1407 pa_assert_se(src_reset(libsamplerate_data
->state
) == 0);
1410 static void libsamplerate_free(pa_resampler
*r
) {
1411 struct src_data
*libsamplerate_data
;
1414 libsamplerate_data
= r
->impl
.data
;
1415 if (libsamplerate_data
->state
)
1416 src_delete(libsamplerate_data
->state
);
1419 static int libsamplerate_init(pa_resampler
*r
) {
1421 struct src_data
*libsamplerate_data
;
1425 libsamplerate_data
= pa_xnew(struct src_data
, 1);
1427 if (!(libsamplerate_data
->state
= src_new(r
->method
, r
->o_ss
.channels
, &err
)))
1430 r
->impl
.free
= libsamplerate_free
;
1431 r
->impl
.update_rates
= libsamplerate_update_rates
;
1432 r
->impl
.resample
= libsamplerate_resample
;
1433 r
->impl
.reset
= libsamplerate_reset
;
1434 r
->impl
.data
= libsamplerate_data
;
1441 /*** speex based implementation ***/
1443 static void speex_resample_float(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
1445 uint32_t inf
= in_n_frames
, outf
= *out_n_frames
;
1446 struct speex_data
*speex_data
;
1451 pa_assert(out_n_frames
);
1453 speex_data
= r
->impl
.data
;
1455 in
= pa_memblock_acquire_chunk(input
);
1456 out
= pa_memblock_acquire_chunk(output
);
1458 pa_assert_se(speex_resampler_process_interleaved_float(speex_data
->state
, in
, &inf
, out
, &outf
) == 0);
1460 pa_memblock_release(input
->memblock
);
1461 pa_memblock_release(output
->memblock
);
1463 pa_assert(inf
== in_n_frames
);
1464 *out_n_frames
= outf
;
1467 static void speex_resample_int(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
1469 uint32_t inf
= in_n_frames
, outf
= *out_n_frames
;
1470 struct speex_data
*speex_data
;
1475 pa_assert(out_n_frames
);
1477 speex_data
= r
->impl
.data
;
1479 in
= pa_memblock_acquire_chunk(input
);
1480 out
= pa_memblock_acquire_chunk(output
);
1482 pa_assert_se(speex_resampler_process_interleaved_int(speex_data
->state
, in
, &inf
, out
, &outf
) == 0);
1484 pa_memblock_release(input
->memblock
);
1485 pa_memblock_release(output
->memblock
);
1487 pa_assert(inf
== in_n_frames
);
1488 *out_n_frames
= outf
;
1491 static void speex_update_rates(pa_resampler
*r
) {
1492 struct speex_data
*speex_data
;
1495 speex_data
= r
->impl
.data
;
1497 pa_assert_se(speex_resampler_set_rate(speex_data
->state
, r
->i_ss
.rate
, r
->o_ss
.rate
) == 0);
1500 static void speex_reset(pa_resampler
*r
) {
1501 struct speex_data
*speex_data
;
1504 speex_data
= r
->impl
.data
;
1506 pa_assert_se(speex_resampler_reset_mem(speex_data
->state
) == 0);
1509 static void speex_free(pa_resampler
*r
) {
1510 struct speex_data
*speex_data
;
1513 speex_data
= r
->impl
.data
;
1514 if (!speex_data
->state
)
1517 speex_resampler_destroy(speex_data
->state
);
1520 static int speex_init(pa_resampler
*r
) {
1522 struct speex_data
*speex_data
;
1526 speex_data
= pa_xnew(struct speex_data
, 1);
1528 r
->impl
.free
= speex_free
;
1529 r
->impl
.update_rates
= speex_update_rates
;
1530 r
->impl
.reset
= speex_reset
;
1531 r
->impl
.data
= speex_data
;
1533 if (r
->method
>= PA_RESAMPLER_SPEEX_FIXED_BASE
&& r
->method
<= PA_RESAMPLER_SPEEX_FIXED_MAX
) {
1535 q
= r
->method
- PA_RESAMPLER_SPEEX_FIXED_BASE
;
1536 r
->impl
.resample
= speex_resample_int
;
1539 pa_assert(r
->method
>= PA_RESAMPLER_SPEEX_FLOAT_BASE
&& r
->method
<= PA_RESAMPLER_SPEEX_FLOAT_MAX
);
1541 q
= r
->method
- PA_RESAMPLER_SPEEX_FLOAT_BASE
;
1542 r
->impl
.resample
= speex_resample_float
;
1545 pa_log_info("Choosing speex quality setting %i.", q
);
1547 if (!(speex_data
->state
= speex_resampler_init(r
->o_ss
.channels
, r
->i_ss
.rate
, r
->o_ss
.rate
, q
, &err
)))
1554 /* Trivial implementation */
1556 static void trivial_resample(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
1558 unsigned i_index
, o_index
;
1560 struct trivial_data
*trivial_data
;
1565 pa_assert(out_n_frames
);
1567 trivial_data
= r
->impl
.data
;
1568 fz
= r
->w_sz
* r
->o_ss
.channels
;
1570 src
= pa_memblock_acquire_chunk(input
);
1571 dst
= pa_memblock_acquire_chunk(output
);
1573 for (o_index
= 0;; o_index
++, trivial_data
->o_counter
++) {
1574 i_index
= ((uint64_t) trivial_data
->o_counter
* r
->i_ss
.rate
) / r
->o_ss
.rate
;
1575 i_index
= i_index
> trivial_data
->i_counter
? i_index
- trivial_data
->i_counter
: 0;
1577 if (i_index
>= in_n_frames
)
1580 pa_assert_fp(o_index
* fz
< pa_memblock_get_length(output
->memblock
));
1582 memcpy((uint8_t*) dst
+ fz
* o_index
, (uint8_t*) src
+ fz
* i_index
, (int) fz
);
1585 pa_memblock_release(input
->memblock
);
1586 pa_memblock_release(output
->memblock
);
1588 *out_n_frames
= o_index
;
1590 trivial_data
->i_counter
+= in_n_frames
;
1592 /* Normalize counters */
1593 while (trivial_data
->i_counter
>= r
->i_ss
.rate
) {
1594 pa_assert(trivial_data
->o_counter
>= r
->o_ss
.rate
);
1596 trivial_data
->i_counter
-= r
->i_ss
.rate
;
1597 trivial_data
->o_counter
-= r
->o_ss
.rate
;
1601 static void trivial_update_rates_or_reset(pa_resampler
*r
) {
1602 struct trivial_data
*trivial_data
;
1605 trivial_data
= r
->impl
.data
;
1607 trivial_data
->i_counter
= 0;
1608 trivial_data
->o_counter
= 0;
1611 static int trivial_init(pa_resampler
*r
) {
1612 struct trivial_data
*trivial_data
;
1615 trivial_data
= pa_xnew0(struct trivial_data
, 1);
1617 r
->impl
.resample
= trivial_resample
;
1618 r
->impl
.update_rates
= trivial_update_rates_or_reset
;
1619 r
->impl
.reset
= trivial_update_rates_or_reset
;
1620 r
->impl
.data
= trivial_data
;
1625 /* Peak finder implementation */
1627 static void peaks_resample(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
1628 unsigned c
, o_index
= 0;
1629 unsigned i
, i_end
= 0;
1631 struct peaks_data
*peaks_data
;
1636 pa_assert(out_n_frames
);
1638 peaks_data
= r
->impl
.data
;
1639 src
= pa_memblock_acquire_chunk(input
);
1640 dst
= pa_memblock_acquire_chunk(output
);
1642 i
= ((uint64_t) peaks_data
->o_counter
* r
->i_ss
.rate
) / r
->o_ss
.rate
;
1643 i
= i
> peaks_data
->i_counter
? i
- peaks_data
->i_counter
: 0;
1645 while (i_end
< in_n_frames
) {
1646 i_end
= ((uint64_t) (peaks_data
->o_counter
+ 1) * r
->i_ss
.rate
) / r
->o_ss
.rate
;
1647 i_end
= i_end
> peaks_data
->i_counter
? i_end
- peaks_data
->i_counter
: 0;
1649 pa_assert_fp(o_index
* r
->w_sz
* r
->o_ss
.channels
< pa_memblock_get_length(output
->memblock
));
1651 /* 1ch float is treated separately, because that is the common case */
1652 if (r
->o_ss
.channels
== 1 && r
->work_format
== PA_SAMPLE_FLOAT32NE
) {
1653 float *s
= (float*) src
+ i
;
1654 float *d
= (float*) dst
+ o_index
;
1656 for (; i
< i_end
&& i
< in_n_frames
; i
++) {
1657 float n
= fabsf(*s
++);
1659 if (n
> peaks_data
->max_f
[0])
1660 peaks_data
->max_f
[0] = n
;
1664 *d
= peaks_data
->max_f
[0];
1665 peaks_data
->max_f
[0] = 0;
1666 o_index
++, peaks_data
->o_counter
++;
1668 } else if (r
->work_format
== PA_SAMPLE_S16NE
) {
1669 int16_t *s
= (int16_t*) src
+ r
->o_ss
.channels
* i
;
1670 int16_t *d
= (int16_t*) dst
+ r
->o_ss
.channels
* o_index
;
1672 for (; i
< i_end
&& i
< in_n_frames
; i
++)
1673 for (c
= 0; c
< r
->o_ss
.channels
; c
++) {
1674 int16_t n
= abs(*s
++);
1676 if (n
> peaks_data
->max_i
[c
])
1677 peaks_data
->max_i
[c
] = n
;
1681 for (c
= 0; c
< r
->o_ss
.channels
; c
++, d
++) {
1682 *d
= peaks_data
->max_i
[c
];
1683 peaks_data
->max_i
[c
] = 0;
1685 o_index
++, peaks_data
->o_counter
++;
1688 float *s
= (float*) src
+ r
->o_ss
.channels
* i
;
1689 float *d
= (float*) dst
+ r
->o_ss
.channels
* o_index
;
1691 for (; i
< i_end
&& i
< in_n_frames
; i
++)
1692 for (c
= 0; c
< r
->o_ss
.channels
; c
++) {
1693 float n
= fabsf(*s
++);
1695 if (n
> peaks_data
->max_f
[c
])
1696 peaks_data
->max_f
[c
] = n
;
1700 for (c
= 0; c
< r
->o_ss
.channels
; c
++, d
++) {
1701 *d
= peaks_data
->max_f
[c
];
1702 peaks_data
->max_f
[c
] = 0;
1704 o_index
++, peaks_data
->o_counter
++;
1709 pa_memblock_release(input
->memblock
);
1710 pa_memblock_release(output
->memblock
);
1712 *out_n_frames
= o_index
;
1714 peaks_data
->i_counter
+= in_n_frames
;
1716 /* Normalize counters */
1717 while (peaks_data
->i_counter
>= r
->i_ss
.rate
) {
1718 pa_assert(peaks_data
->o_counter
>= r
->o_ss
.rate
);
1720 peaks_data
->i_counter
-= r
->i_ss
.rate
;
1721 peaks_data
->o_counter
-= r
->o_ss
.rate
;
1725 static void peaks_update_rates_or_reset(pa_resampler
*r
) {
1726 struct peaks_data
*peaks_data
;
1729 peaks_data
= r
->impl
.data
;
1731 peaks_data
->i_counter
= 0;
1732 peaks_data
->o_counter
= 0;
1735 static int peaks_init(pa_resampler
*r
) {
1736 struct peaks_data
*peaks_data
;
1738 pa_assert(r
->i_ss
.rate
>= r
->o_ss
.rate
);
1739 pa_assert(r
->work_format
== PA_SAMPLE_S16NE
|| r
->work_format
== PA_SAMPLE_FLOAT32NE
);
1741 peaks_data
= pa_xnew0(struct peaks_data
, 1);
1743 r
->impl
.resample
= peaks_resample
;
1744 r
->impl
.update_rates
= peaks_update_rates_or_reset
;
1745 r
->impl
.reset
= peaks_update_rates_or_reset
;
1746 r
->impl
.data
= peaks_data
;
1751 /*** ffmpeg based implementation ***/
1753 static void ffmpeg_resample(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
1754 unsigned used_frames
= 0, c
;
1755 int previous_consumed_frames
= -1;
1756 struct ffmpeg_data
*ffmpeg_data
;
1761 pa_assert(out_n_frames
);
1763 ffmpeg_data
= r
->impl
.data
;
1765 for (c
= 0; c
< r
->o_ss
.channels
; c
++) {
1768 int16_t *p
, *t
, *k
, *q
, *s
;
1769 int consumed_frames
;
1771 /* Allocate a new block */
1772 b
= pa_memblock_new(r
->mempool
, ffmpeg_data
->buf
[c
].length
+ in_n_frames
* sizeof(int16_t));
1773 p
= pa_memblock_acquire(b
);
1775 /* Now copy the input data, splitting up channels */
1776 t
= (int16_t*) pa_memblock_acquire_chunk(input
) + c
;
1778 for (u
= 0; u
< in_n_frames
; u
++) {
1780 t
+= r
->o_ss
.channels
;
1783 pa_memblock_release(input
->memblock
);
1785 /* Allocate buffer for the result */
1786 w
= pa_memblock_new(r
->mempool
, *out_n_frames
* sizeof(int16_t));
1787 q
= pa_memblock_acquire(w
);
1790 used_frames
= (unsigned) av_resample(ffmpeg_data
->state
,
1793 (int) in_n_frames
, (int) *out_n_frames
,
1794 c
>= (unsigned) (r
->o_ss
.channels
-1));
1796 pa_memblock_release(b
);
1797 pa_memblock_unref(b
);
1799 pa_assert(consumed_frames
<= (int) in_n_frames
);
1800 pa_assert(previous_consumed_frames
== -1 || consumed_frames
== previous_consumed_frames
);
1801 previous_consumed_frames
= consumed_frames
;
1803 /* And place the results in the output buffer */
1804 s
= (int16_t *) pa_memblock_acquire_chunk(output
) + c
;
1805 for (u
= 0; u
< used_frames
; u
++) {
1808 s
+= r
->o_ss
.channels
;
1810 pa_memblock_release(output
->memblock
);
1811 pa_memblock_release(w
);
1812 pa_memblock_unref(w
);
1815 if (previous_consumed_frames
< (int) in_n_frames
) {
1816 void *leftover_data
= (int16_t *) pa_memblock_acquire_chunk(input
) + previous_consumed_frames
* r
->o_ss
.channels
;
1817 size_t leftover_length
= (in_n_frames
- previous_consumed_frames
) * r
->o_ss
.channels
* sizeof(int16_t);
1819 save_leftover(r
, leftover_data
, leftover_length
);
1820 pa_memblock_release(input
->memblock
);
1823 *out_n_frames
= used_frames
;
1826 static void ffmpeg_free(pa_resampler
*r
) {
1828 struct ffmpeg_data
*ffmpeg_data
;
1832 ffmpeg_data
= r
->impl
.data
;
1833 if (ffmpeg_data
->state
)
1834 av_resample_close(ffmpeg_data
->state
);
1836 for (c
= 0; c
< PA_ELEMENTSOF(ffmpeg_data
->buf
); c
++)
1837 if (ffmpeg_data
->buf
[c
].memblock
)
1838 pa_memblock_unref(ffmpeg_data
->buf
[c
].memblock
);
1841 static int ffmpeg_init(pa_resampler
*r
) {
1843 struct ffmpeg_data
*ffmpeg_data
;
1847 ffmpeg_data
= pa_xnew(struct ffmpeg_data
, 1);
1849 /* We could probably implement different quality levels by
1850 * adjusting the filter parameters here. However, ffmpeg
1851 * internally only uses these hardcoded values, so let's use them
1852 * here for now as well until ffmpeg makes this configurable. */
1854 if (!(ffmpeg_data
->state
= av_resample_init((int) r
->o_ss
.rate
, (int) r
->i_ss
.rate
, 16, 10, 0, 0.8)))
1857 r
->impl
.free
= ffmpeg_free
;
1858 r
->impl
.resample
= ffmpeg_resample
;
1859 r
->impl
.data
= (void *) ffmpeg_data
;
1861 for (c
= 0; c
< PA_ELEMENTSOF(ffmpeg_data
->buf
); c
++)
1862 pa_memchunk_reset(&ffmpeg_data
->buf
[c
]);
1867 /*** copy (noop) implementation ***/
1869 static int copy_init(pa_resampler
*r
) {
1872 pa_assert(r
->o_ss
.rate
== r
->i_ss
.rate
);