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 void (*impl_free
)(pa_resampler
*r
);
78 void (*impl_update_rates
)(pa_resampler
*r
);
79 void (*impl_resample
)(pa_resampler
*r
, const pa_memchunk
*in
, unsigned in_samples
, pa_memchunk
*out
, unsigned *out_samples
);
80 void (*impl_reset
)(pa_resampler
*r
);
84 struct trivial_data
{ /* data specific to the trivial resampler */
89 struct peaks_data
{ /* data specific to the peak finder pseudo resampler */
93 float max_f
[PA_CHANNELS_MAX
];
94 int16_t max_i
[PA_CHANNELS_MAX
];
97 #ifdef HAVE_LIBSAMPLERATE
98 struct src_data
{ /* data specific to libsamplerate */
104 struct speex_data
{ /* data specific to speex */
105 SpeexResamplerState
* state
;
109 struct ffmpeg_data
{ /* data specific to ffmpeg */
110 struct AVResampleContext
*state
;
111 pa_memchunk buf
[PA_CHANNELS_MAX
];
114 static int copy_init(pa_resampler
*r
);
115 static int trivial_init(pa_resampler
*r
);
117 static int speex_init(pa_resampler
*r
);
119 static int ffmpeg_init(pa_resampler
*r
);
120 static int peaks_init(pa_resampler
*r
);
121 #ifdef HAVE_LIBSAMPLERATE
122 static int libsamplerate_init(pa_resampler
*r
);
125 static void calc_map_table(pa_resampler
*r
);
127 static int (* const init_table
[])(pa_resampler
*r
) = {
128 #ifdef HAVE_LIBSAMPLERATE
129 [PA_RESAMPLER_SRC_SINC_BEST_QUALITY
] = libsamplerate_init
,
130 [PA_RESAMPLER_SRC_SINC_MEDIUM_QUALITY
] = libsamplerate_init
,
131 [PA_RESAMPLER_SRC_SINC_FASTEST
] = libsamplerate_init
,
132 [PA_RESAMPLER_SRC_ZERO_ORDER_HOLD
] = libsamplerate_init
,
133 [PA_RESAMPLER_SRC_LINEAR
] = libsamplerate_init
,
135 [PA_RESAMPLER_SRC_SINC_BEST_QUALITY
] = NULL
,
136 [PA_RESAMPLER_SRC_SINC_MEDIUM_QUALITY
] = NULL
,
137 [PA_RESAMPLER_SRC_SINC_FASTEST
] = NULL
,
138 [PA_RESAMPLER_SRC_ZERO_ORDER_HOLD
] = NULL
,
139 [PA_RESAMPLER_SRC_LINEAR
] = NULL
,
141 [PA_RESAMPLER_TRIVIAL
] = trivial_init
,
143 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+0] = speex_init
,
144 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+1] = speex_init
,
145 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+2] = speex_init
,
146 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+3] = speex_init
,
147 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+4] = speex_init
,
148 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+5] = speex_init
,
149 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+6] = speex_init
,
150 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+7] = speex_init
,
151 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+8] = speex_init
,
152 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+9] = speex_init
,
153 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+10] = speex_init
,
154 [PA_RESAMPLER_SPEEX_FIXED_BASE
+0] = speex_init
,
155 [PA_RESAMPLER_SPEEX_FIXED_BASE
+1] = speex_init
,
156 [PA_RESAMPLER_SPEEX_FIXED_BASE
+2] = speex_init
,
157 [PA_RESAMPLER_SPEEX_FIXED_BASE
+3] = speex_init
,
158 [PA_RESAMPLER_SPEEX_FIXED_BASE
+4] = speex_init
,
159 [PA_RESAMPLER_SPEEX_FIXED_BASE
+5] = speex_init
,
160 [PA_RESAMPLER_SPEEX_FIXED_BASE
+6] = speex_init
,
161 [PA_RESAMPLER_SPEEX_FIXED_BASE
+7] = speex_init
,
162 [PA_RESAMPLER_SPEEX_FIXED_BASE
+8] = speex_init
,
163 [PA_RESAMPLER_SPEEX_FIXED_BASE
+9] = speex_init
,
164 [PA_RESAMPLER_SPEEX_FIXED_BASE
+10] = speex_init
,
166 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+0] = NULL
,
167 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+1] = NULL
,
168 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+2] = NULL
,
169 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+3] = NULL
,
170 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+4] = NULL
,
171 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+5] = NULL
,
172 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+6] = NULL
,
173 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+7] = NULL
,
174 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+8] = NULL
,
175 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+9] = NULL
,
176 [PA_RESAMPLER_SPEEX_FLOAT_BASE
+10] = NULL
,
177 [PA_RESAMPLER_SPEEX_FIXED_BASE
+0] = NULL
,
178 [PA_RESAMPLER_SPEEX_FIXED_BASE
+1] = NULL
,
179 [PA_RESAMPLER_SPEEX_FIXED_BASE
+2] = NULL
,
180 [PA_RESAMPLER_SPEEX_FIXED_BASE
+3] = NULL
,
181 [PA_RESAMPLER_SPEEX_FIXED_BASE
+4] = NULL
,
182 [PA_RESAMPLER_SPEEX_FIXED_BASE
+5] = NULL
,
183 [PA_RESAMPLER_SPEEX_FIXED_BASE
+6] = NULL
,
184 [PA_RESAMPLER_SPEEX_FIXED_BASE
+7] = NULL
,
185 [PA_RESAMPLER_SPEEX_FIXED_BASE
+8] = NULL
,
186 [PA_RESAMPLER_SPEEX_FIXED_BASE
+9] = NULL
,
187 [PA_RESAMPLER_SPEEX_FIXED_BASE
+10] = NULL
,
189 [PA_RESAMPLER_FFMPEG
] = ffmpeg_init
,
190 [PA_RESAMPLER_AUTO
] = NULL
,
191 [PA_RESAMPLER_COPY
] = copy_init
,
192 [PA_RESAMPLER_PEAKS
] = peaks_init
,
195 static pa_resample_method_t
pa_resampler_fix_method(
196 pa_resample_flags_t flags
,
197 pa_resample_method_t method
,
198 const uint32_t rate_a
,
199 const uint32_t rate_b
) {
201 pa_assert(rate_a
> 0 && rate_a
<= PA_RATE_MAX
);
202 pa_assert(rate_b
> 0 && rate_b
<= PA_RATE_MAX
);
203 pa_assert(method
>= 0);
204 pa_assert(method
< PA_RESAMPLER_MAX
);
206 if (!(flags
& PA_RESAMPLER_VARIABLE_RATE
) && rate_a
== rate_b
) {
207 pa_log_info("Forcing resampler 'copy', because of fixed, identical sample rates.");
208 method
= PA_RESAMPLER_COPY
;
211 if (!pa_resample_method_supported(method
)) {
212 pa_log_warn("Support for resampler '%s' not compiled in, reverting to 'auto'.", pa_resample_method_to_string(method
));
213 method
= PA_RESAMPLER_AUTO
;
217 case PA_RESAMPLER_COPY
:
218 if (rate_a
!= rate_b
) {
219 pa_log_info("Resampler 'copy' cannot change sampling rate, reverting to resampler 'auto'.");
222 /* Else fall through */
223 case PA_RESAMPLER_FFMPEG
:
224 if (flags
& PA_RESAMPLER_VARIABLE_RATE
) {
225 pa_log_info("Resampler '%s' cannot do variable rate, reverting to resampler 'auto'.", pa_resample_method_to_string(method
));
226 method
= PA_RESAMPLER_AUTO
;
230 /* The Peaks resampler only supports downsampling.
231 * Revert to auto if we are upsampling */
232 case PA_RESAMPLER_PEAKS
:
233 if (rate_a
< rate_b
) {
234 pa_log_warn("The 'peaks' resampler only supports downsampling, reverting to resampler 'auto'.");
235 method
= PA_RESAMPLER_AUTO
;
243 if (method
== PA_RESAMPLER_AUTO
) {
245 method
= PA_RESAMPLER_SPEEX_FLOAT_BASE
+ 1;
247 if (flags
& PA_RESAMPLER_VARIABLE_RATE
)
248 method
= PA_RESAMPLER_TRIVIAL
;
250 method
= PA_RESAMPLER_FFMPEG
;
257 /* Return true if a is a more precise sample format than b, else return false */
258 static bool sample_format_more_precise(pa_sample_format_t a
, pa_sample_format_t b
) {
259 pa_assert(a
>= 0 && a
< PA_SAMPLE_MAX
);
260 pa_assert(b
>= 0 && b
< PA_SAMPLE_MAX
);
269 case PA_SAMPLE_S16LE
:
270 case PA_SAMPLE_S16BE
:
271 if (b
== PA_SAMPLE_ULAW
|| b
== PA_SAMPLE_ALAW
|| b
== PA_SAMPLE_U8
)
277 case PA_SAMPLE_S24LE
:
278 case PA_SAMPLE_S24BE
:
279 case PA_SAMPLE_S24_32LE
:
280 case PA_SAMPLE_S24_32BE
:
281 if (b
== PA_SAMPLE_ULAW
|| b
== PA_SAMPLE_ALAW
|| b
== PA_SAMPLE_U8
||
282 b
== PA_SAMPLE_S16LE
|| b
== PA_SAMPLE_S16BE
)
288 case PA_SAMPLE_FLOAT32LE
:
289 case PA_SAMPLE_FLOAT32BE
:
290 case PA_SAMPLE_S32LE
:
291 case PA_SAMPLE_S32BE
:
292 if (b
== PA_SAMPLE_FLOAT32LE
|| b
== PA_SAMPLE_FLOAT32BE
||
293 b
== PA_SAMPLE_S32LE
|| b
== PA_SAMPLE_FLOAT32BE
)
304 static pa_sample_format_t
pa_resampler_choose_work_format(
305 pa_resample_method_t method
,
306 pa_sample_format_t a
,
307 pa_sample_format_t b
,
309 pa_sample_format_t work_format
;
311 pa_assert(a
>= 0 && a
< PA_SAMPLE_MAX
);
312 pa_assert(b
>= 0 && b
< PA_SAMPLE_MAX
);
313 pa_assert(method
>= 0);
314 pa_assert(method
< PA_RESAMPLER_MAX
);
316 if (method
>= PA_RESAMPLER_SPEEX_FIXED_BASE
&& method
<= PA_RESAMPLER_SPEEX_FIXED_MAX
)
317 method
= PA_RESAMPLER_SPEEX_FIXED_BASE
;
320 /* This block is for resampling functions that only
321 * support the S16 sample format. */
322 case PA_RESAMPLER_SPEEX_FIXED_BASE
: /* fall through */
323 case PA_RESAMPLER_FFMPEG
:
324 work_format
= PA_SAMPLE_S16NE
;
327 /* This block is for resampling functions that support
328 * any sample format. */
329 case PA_RESAMPLER_COPY
: /* fall through */
330 case PA_RESAMPLER_TRIVIAL
:
331 if (!map_required
&& a
== b
) {
335 /* Else fall trough */
336 case PA_RESAMPLER_PEAKS
:
337 if (a
== PA_SAMPLE_S16NE
|| b
== PA_SAMPLE_S16NE
)
338 work_format
= PA_SAMPLE_S16NE
;
339 else if (sample_format_more_precise(a
, PA_SAMPLE_S16NE
) ||
340 sample_format_more_precise(b
, PA_SAMPLE_S16NE
))
341 work_format
= PA_SAMPLE_FLOAT32NE
;
343 work_format
= PA_SAMPLE_S16NE
;
347 work_format
= PA_SAMPLE_FLOAT32NE
;
353 pa_resampler
* pa_resampler_new(
355 const pa_sample_spec
*a
,
356 const pa_channel_map
*am
,
357 const pa_sample_spec
*b
,
358 const pa_channel_map
*bm
,
359 pa_resample_method_t method
,
360 pa_resample_flags_t flags
) {
362 pa_resampler
*r
= NULL
;
367 pa_assert(pa_sample_spec_valid(a
));
368 pa_assert(pa_sample_spec_valid(b
));
369 pa_assert(method
>= 0);
370 pa_assert(method
< PA_RESAMPLER_MAX
);
372 method
= pa_resampler_fix_method(flags
, method
, a
->rate
, b
->rate
);
374 r
= pa_xnew0(pa_resampler
, 1);
379 /* Fill sample specs */
383 /* set up the remap structure */
384 r
->remap
.i_ss
= &r
->i_ss
;
385 r
->remap
.o_ss
= &r
->o_ss
;
386 r
->remap
.format
= &r
->work_format
;
390 else if (!pa_channel_map_init_auto(&r
->i_cm
, r
->i_ss
.channels
, PA_CHANNEL_MAP_DEFAULT
))
395 else if (!pa_channel_map_init_auto(&r
->o_cm
, r
->o_ss
.channels
, PA_CHANNEL_MAP_DEFAULT
))
398 r
->i_fz
= pa_frame_size(a
);
399 r
->o_fz
= pa_frame_size(b
);
403 pa_log_info("Using resampler '%s'", pa_resample_method_to_string(method
));
405 r
->work_format
= pa_resampler_choose_work_format(method
, a
->format
, b
->format
, r
->map_required
);
407 pa_log_info("Using %s as working format.", pa_sample_format_to_string(r
->work_format
));
409 r
->w_sz
= pa_sample_size_of_format(r
->work_format
);
411 if (r
->i_ss
.format
!= r
->work_format
) {
412 if (r
->work_format
== PA_SAMPLE_FLOAT32NE
) {
413 if (!(r
->to_work_format_func
= pa_get_convert_to_float32ne_function(r
->i_ss
.format
)))
416 pa_assert(r
->work_format
== PA_SAMPLE_S16NE
);
417 if (!(r
->to_work_format_func
= pa_get_convert_to_s16ne_function(r
->i_ss
.format
)))
422 if (r
->o_ss
.format
!= r
->work_format
) {
423 if (r
->work_format
== PA_SAMPLE_FLOAT32NE
) {
424 if (!(r
->from_work_format_func
= pa_get_convert_from_float32ne_function(r
->o_ss
.format
)))
427 pa_assert(r
->work_format
== PA_SAMPLE_S16NE
);
428 if (!(r
->from_work_format_func
= pa_get_convert_from_s16ne_function(r
->o_ss
.format
)))
433 /* initialize implementation */
434 if (init_table
[method
](r
) < 0)
445 void pa_resampler_free(pa_resampler
*r
) {
451 if (r
->to_work_format_buf
.memblock
)
452 pa_memblock_unref(r
->to_work_format_buf
.memblock
);
453 if (r
->remap_buf
.memblock
)
454 pa_memblock_unref(r
->remap_buf
.memblock
);
455 if (r
->resample_buf
.memblock
)
456 pa_memblock_unref(r
->resample_buf
.memblock
);
457 if (r
->from_work_format_buf
.memblock
)
458 pa_memblock_unref(r
->from_work_format_buf
.memblock
);
460 pa_xfree(r
->impl_data
);
464 void pa_resampler_set_input_rate(pa_resampler
*r
, uint32_t rate
) {
468 if (r
->i_ss
.rate
== rate
)
473 r
->impl_update_rates(r
);
476 void pa_resampler_set_output_rate(pa_resampler
*r
, uint32_t rate
) {
480 if (r
->o_ss
.rate
== rate
)
485 r
->impl_update_rates(r
);
488 size_t pa_resampler_request(pa_resampler
*r
, size_t out_length
) {
491 /* Let's round up here to make it more likely that the caller will get at
492 * least out_length amount of data from pa_resampler_run().
494 * We don't take the leftover into account here. If we did, then it might
495 * be in theory possible that this function would return 0 and
496 * pa_resampler_run() would also return 0. That could lead to infinite
497 * loops. When the leftover is ignored here, such loops would eventually
498 * terminate, because the leftover would grow each round, finally
499 * surpassing the minimum input threshold of the resampler. */
500 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
;
503 size_t pa_resampler_result(pa_resampler
*r
, size_t in_length
) {
508 /* Let's round up here to ensure that the caller will always allocate big
509 * enough output buffer. */
511 frames
= (in_length
+ r
->i_fz
- 1) / r
->i_fz
;
513 if (r
->remap_buf_contains_leftover_data
)
514 frames
+= r
->remap_buf
.length
/ (r
->w_sz
* r
->o_ss
.channels
);
516 return (((uint64_t) frames
* r
->o_ss
.rate
+ r
->i_ss
.rate
- 1) / r
->i_ss
.rate
) * r
->o_fz
;
519 size_t pa_resampler_max_block_size(pa_resampler
*r
) {
520 size_t block_size_max
;
521 pa_sample_spec max_ss
;
527 block_size_max
= pa_mempool_block_size_max(r
->mempool
);
529 /* We deduce the "largest" sample spec we're using during the
531 max_ss
.channels
= (uint8_t) (PA_MAX(r
->i_ss
.channels
, r
->o_ss
.channels
));
533 /* We silently assume that the format enum is ordered by size */
534 max_ss
.format
= PA_MAX(r
->i_ss
.format
, r
->o_ss
.format
);
535 max_ss
.format
= PA_MAX(max_ss
.format
, r
->work_format
);
537 max_ss
.rate
= PA_MAX(r
->i_ss
.rate
, r
->o_ss
.rate
);
539 max_fs
= pa_frame_size(&max_ss
);
540 frames
= block_size_max
/ max_fs
- EXTRA_FRAMES
;
542 if (r
->remap_buf_contains_leftover_data
)
543 frames
-= r
->remap_buf
.length
/ (r
->w_sz
* r
->o_ss
.channels
);
545 return ((uint64_t) frames
* r
->i_ss
.rate
/ max_ss
.rate
) * r
->i_fz
;
548 void pa_resampler_reset(pa_resampler
*r
) {
554 r
->remap_buf_contains_leftover_data
= false;
557 pa_resample_method_t
pa_resampler_get_method(pa_resampler
*r
) {
563 const pa_channel_map
* pa_resampler_input_channel_map(pa_resampler
*r
) {
569 const pa_sample_spec
* pa_resampler_input_sample_spec(pa_resampler
*r
) {
575 const pa_channel_map
* pa_resampler_output_channel_map(pa_resampler
*r
) {
581 const pa_sample_spec
* pa_resampler_output_sample_spec(pa_resampler
*r
) {
587 static const char * const resample_methods
[] = {
588 "src-sinc-best-quality",
589 "src-sinc-medium-quality",
591 "src-zero-order-hold",
622 const char *pa_resample_method_to_string(pa_resample_method_t m
) {
624 if (m
< 0 || m
>= PA_RESAMPLER_MAX
)
627 return resample_methods
[m
];
630 int pa_resample_method_supported(pa_resample_method_t m
) {
632 if (m
< 0 || m
>= PA_RESAMPLER_MAX
)
635 #ifndef HAVE_LIBSAMPLERATE
636 if (m
<= PA_RESAMPLER_SRC_LINEAR
)
641 if (m
>= PA_RESAMPLER_SPEEX_FLOAT_BASE
&& m
<= PA_RESAMPLER_SPEEX_FLOAT_MAX
)
643 if (m
>= PA_RESAMPLER_SPEEX_FIXED_BASE
&& m
<= PA_RESAMPLER_SPEEX_FIXED_MAX
)
650 pa_resample_method_t
pa_parse_resample_method(const char *string
) {
651 pa_resample_method_t m
;
655 for (m
= 0; m
< PA_RESAMPLER_MAX
; m
++)
656 if (pa_streq(string
, resample_methods
[m
]))
659 if (pa_streq(string
, "speex-fixed"))
660 return PA_RESAMPLER_SPEEX_FIXED_BASE
+ 1;
662 if (pa_streq(string
, "speex-float"))
663 return PA_RESAMPLER_SPEEX_FLOAT_BASE
+ 1;
665 return PA_RESAMPLER_INVALID
;
668 static bool on_left(pa_channel_position_t p
) {
671 p
== PA_CHANNEL_POSITION_FRONT_LEFT
||
672 p
== PA_CHANNEL_POSITION_REAR_LEFT
||
673 p
== PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER
||
674 p
== PA_CHANNEL_POSITION_SIDE_LEFT
||
675 p
== PA_CHANNEL_POSITION_TOP_FRONT_LEFT
||
676 p
== PA_CHANNEL_POSITION_TOP_REAR_LEFT
;
679 static bool on_right(pa_channel_position_t p
) {
682 p
== PA_CHANNEL_POSITION_FRONT_RIGHT
||
683 p
== PA_CHANNEL_POSITION_REAR_RIGHT
||
684 p
== PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER
||
685 p
== PA_CHANNEL_POSITION_SIDE_RIGHT
||
686 p
== PA_CHANNEL_POSITION_TOP_FRONT_RIGHT
||
687 p
== PA_CHANNEL_POSITION_TOP_REAR_RIGHT
;
690 static bool on_center(pa_channel_position_t p
) {
693 p
== PA_CHANNEL_POSITION_FRONT_CENTER
||
694 p
== PA_CHANNEL_POSITION_REAR_CENTER
||
695 p
== PA_CHANNEL_POSITION_TOP_CENTER
||
696 p
== PA_CHANNEL_POSITION_TOP_FRONT_CENTER
||
697 p
== PA_CHANNEL_POSITION_TOP_REAR_CENTER
;
700 static bool on_lfe(pa_channel_position_t p
) {
702 p
== PA_CHANNEL_POSITION_LFE
;
705 static bool on_front(pa_channel_position_t p
) {
707 p
== PA_CHANNEL_POSITION_FRONT_LEFT
||
708 p
== PA_CHANNEL_POSITION_FRONT_RIGHT
||
709 p
== PA_CHANNEL_POSITION_FRONT_CENTER
||
710 p
== PA_CHANNEL_POSITION_TOP_FRONT_LEFT
||
711 p
== PA_CHANNEL_POSITION_TOP_FRONT_RIGHT
||
712 p
== PA_CHANNEL_POSITION_TOP_FRONT_CENTER
||
713 p
== PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER
||
714 p
== PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER
;
717 static bool on_rear(pa_channel_position_t p
) {
719 p
== PA_CHANNEL_POSITION_REAR_LEFT
||
720 p
== PA_CHANNEL_POSITION_REAR_RIGHT
||
721 p
== PA_CHANNEL_POSITION_REAR_CENTER
||
722 p
== PA_CHANNEL_POSITION_TOP_REAR_LEFT
||
723 p
== PA_CHANNEL_POSITION_TOP_REAR_RIGHT
||
724 p
== PA_CHANNEL_POSITION_TOP_REAR_CENTER
;
727 static bool on_side(pa_channel_position_t p
) {
729 p
== PA_CHANNEL_POSITION_SIDE_LEFT
||
730 p
== PA_CHANNEL_POSITION_SIDE_RIGHT
||
731 p
== PA_CHANNEL_POSITION_TOP_CENTER
;
741 static int front_rear_side(pa_channel_position_t p
) {
751 static void calc_map_table(pa_resampler
*r
) {
754 bool ic_connected
[PA_CHANNELS_MAX
];
762 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
)))))
767 n_oc
= r
->o_ss
.channels
;
768 n_ic
= r
->i_ss
.channels
;
770 memset(m
->map_table_f
, 0, sizeof(m
->map_table_f
));
771 memset(m
->map_table_i
, 0, sizeof(m
->map_table_i
));
773 memset(ic_connected
, 0, sizeof(ic_connected
));
774 remix
= (r
->flags
& (PA_RESAMPLER_NO_REMAP
| PA_RESAMPLER_NO_REMIX
)) == 0;
776 if (r
->flags
& PA_RESAMPLER_NO_REMAP
) {
779 for (oc
= 0; oc
< PA_MIN(n_ic
, n_oc
); oc
++)
780 m
->map_table_f
[oc
][oc
] = 1.0f
;
782 } else if (r
->flags
& PA_RESAMPLER_NO_REMIX
) {
784 for (oc
= 0; oc
< n_oc
; oc
++) {
785 pa_channel_position_t b
= r
->o_cm
.map
[oc
];
787 for (ic
= 0; ic
< n_ic
; ic
++) {
788 pa_channel_position_t a
= r
->i_cm
.map
[ic
];
790 /* We shall not do any remixing. Hence, just check by name */
792 m
->map_table_f
[oc
][ic
] = 1.0f
;
797 /* OK, we shall do the full monty: upmixing and downmixing. Our
798 * algorithm is relatively simple, does not do spacialization, delay
799 * elements or apply lowpass filters for LFE. Patches are always
800 * welcome, though. Oh, and it doesn't do any matrix decoding. (Which
801 * probably wouldn't make any sense anyway.)
803 * This code is not idempotent: downmixing an upmixed stereo stream is
804 * not identical to the original. The volume will not match, and the
805 * two channels will be a linear combination of both.
807 * This is loosely based on random suggestions found on the Internet,
809 * http://www.halfgaar.net/surround-sound-in-linux and the alsa upmix
812 * The algorithm works basically like this:
814 * 1) Connect all channels with matching names.
817 * S:Mono: Copy into all D:channels
818 * D:Mono: Avg all S:channels
820 * 3) Mix D:Left, D:Right:
821 * D:Left: If not connected, avg all S:Left
822 * D:Right: If not connected, avg all S:Right
825 * If not connected, avg all S:Center
826 * If still not connected, avg all S:Left, S:Right
829 * If not connected, avg all S:*
831 * 6) Make sure S:Left/S:Right is used: S:Left/S:Right: If not
832 * connected, mix into all D:left and all D:right channels. Gain is
835 * 7) Make sure S:Center, S:LFE is used:
837 * S:Center, S:LFE: If not connected, mix into all D:left, all
838 * D:right, all D:center channels. Gain is 0.5 for center and 0.375
839 * for LFE. C-front is only mixed into L-front/R-front if available,
840 * otherwise into all L/R channels. Similarly for C-rear.
842 * 8) Normalize each row in the matrix such that the sum for each row is
843 * not larger than 1.0 in order to avoid clipping.
845 * S: and D: shall relate to the source resp. destination channels.
847 * Rationale: 1, 2 are probably obvious. For 3: this copies front to
848 * rear if needed. For 4: we try to find some suitable C source for C,
849 * if we don't find any, we avg L and R. For 5: LFE is mixed from all
850 * channels. For 6: the rear channels should not be dropped entirely,
851 * however have only minimal impact. For 7: movies usually encode
852 * speech on the center channel. Thus we have to make sure this channel
853 * is distributed to L and R if not available in the output. Also, LFE
854 * is used to achieve a greater dynamic range, and thus we should try
855 * to do our best to pass it to L+R.
862 ic_unconnected_left
= 0,
863 ic_unconnected_right
= 0,
864 ic_unconnected_center
= 0,
865 ic_unconnected_lfe
= 0;
866 bool ic_unconnected_center_mixed_in
= 0;
870 for (ic
= 0; ic
< n_ic
; ic
++) {
871 if (on_left(r
->i_cm
.map
[ic
]))
873 if (on_right(r
->i_cm
.map
[ic
]))
875 if (on_center(r
->i_cm
.map
[ic
]))
879 for (oc
= 0; oc
< n_oc
; oc
++) {
880 bool oc_connected
= false;
881 pa_channel_position_t b
= r
->o_cm
.map
[oc
];
883 for (ic
= 0; ic
< n_ic
; ic
++) {
884 pa_channel_position_t a
= r
->i_cm
.map
[ic
];
886 if (a
== b
|| a
== PA_CHANNEL_POSITION_MONO
) {
887 m
->map_table_f
[oc
][ic
] = 1.0f
;
890 ic_connected
[ic
] = true;
892 else if (b
== PA_CHANNEL_POSITION_MONO
) {
893 m
->map_table_f
[oc
][ic
] = 1.0f
/ (float) n_ic
;
896 ic_connected
[ic
] = true;
901 /* Try to find matching input ports for this output port */
905 /* We are not connected and on the left side, let's
906 * average all left side input channels. */
909 for (ic
= 0; ic
< n_ic
; ic
++)
910 if (on_left(r
->i_cm
.map
[ic
])) {
911 m
->map_table_f
[oc
][ic
] = 1.0f
/ (float) ic_left
;
912 ic_connected
[ic
] = true;
915 /* We ignore the case where there is no left input channel.
916 * Something is really wrong in this case anyway. */
918 } else if (on_right(b
)) {
920 /* We are not connected and on the right side, let's
921 * average all right side input channels. */
924 for (ic
= 0; ic
< n_ic
; ic
++)
925 if (on_right(r
->i_cm
.map
[ic
])) {
926 m
->map_table_f
[oc
][ic
] = 1.0f
/ (float) ic_right
;
927 ic_connected
[ic
] = true;
930 /* We ignore the case where there is no right input
931 * channel. Something is really wrong in this case anyway.
934 } else if (on_center(b
)) {
938 /* We are not connected and at the center. Let's average
939 * all center input channels. */
941 for (ic
= 0; ic
< n_ic
; ic
++)
942 if (on_center(r
->i_cm
.map
[ic
])) {
943 m
->map_table_f
[oc
][ic
] = 1.0f
/ (float) ic_center
;
944 ic_connected
[ic
] = true;
947 } else if (ic_left
+ ic_right
> 0) {
949 /* Hmm, no center channel around, let's synthesize it
950 * by mixing L and R.*/
952 for (ic
= 0; ic
< n_ic
; ic
++)
953 if (on_left(r
->i_cm
.map
[ic
]) || on_right(r
->i_cm
.map
[ic
])) {
954 m
->map_table_f
[oc
][ic
] = 1.0f
/ (float) (ic_left
+ ic_right
);
955 ic_connected
[ic
] = true;
959 /* We ignore the case where there is not even a left or
960 * right input channel. Something is really wrong in this
963 } else if (on_lfe(b
) && !(r
->flags
& PA_RESAMPLER_NO_LFE
)) {
965 /* We are not connected and an LFE. Let's average all
966 * channels for LFE. */
968 for (ic
= 0; ic
< n_ic
; ic
++)
969 m
->map_table_f
[oc
][ic
] = 1.0f
/ (float) n_ic
;
971 /* Please note that a channel connected to LFE doesn't
972 * really count as connected. */
977 for (ic
= 0; ic
< n_ic
; ic
++) {
978 pa_channel_position_t a
= r
->i_cm
.map
[ic
];
980 if (ic_connected
[ic
])
984 ic_unconnected_left
++;
985 else if (on_right(a
))
986 ic_unconnected_right
++;
987 else if (on_center(a
))
988 ic_unconnected_center
++;
990 ic_unconnected_lfe
++;
993 for (ic
= 0; ic
< n_ic
; ic
++) {
994 pa_channel_position_t a
= r
->i_cm
.map
[ic
];
996 if (ic_connected
[ic
])
999 for (oc
= 0; oc
< n_oc
; oc
++) {
1000 pa_channel_position_t b
= r
->o_cm
.map
[oc
];
1002 if (on_left(a
) && on_left(b
))
1003 m
->map_table_f
[oc
][ic
] = (1.f
/9.f
) / (float) ic_unconnected_left
;
1005 else if (on_right(a
) && on_right(b
))
1006 m
->map_table_f
[oc
][ic
] = (1.f
/9.f
) / (float) ic_unconnected_right
;
1008 else if (on_center(a
) && on_center(b
)) {
1009 m
->map_table_f
[oc
][ic
] = (1.f
/9.f
) / (float) ic_unconnected_center
;
1010 ic_unconnected_center_mixed_in
= true;
1012 } else if (on_lfe(a
) && !(r
->flags
& PA_RESAMPLER_NO_LFE
))
1013 m
->map_table_f
[oc
][ic
] = .375f
/ (float) ic_unconnected_lfe
;
1017 if (ic_unconnected_center
> 0 && !ic_unconnected_center_mixed_in
) {
1018 unsigned ncenter
[PA_CHANNELS_MAX
];
1019 bool found_frs
[PA_CHANNELS_MAX
];
1021 memset(ncenter
, 0, sizeof(ncenter
));
1022 memset(found_frs
, 0, sizeof(found_frs
));
1024 /* Hmm, as it appears there was no center channel we
1025 could mix our center channel in. In this case, mix it into
1026 left and right. Using .5 as the factor. */
1028 for (ic
= 0; ic
< n_ic
; ic
++) {
1030 if (ic_connected
[ic
])
1033 if (!on_center(r
->i_cm
.map
[ic
]))
1036 for (oc
= 0; oc
< n_oc
; oc
++) {
1038 if (!on_left(r
->o_cm
.map
[oc
]) && !on_right(r
->o_cm
.map
[oc
]))
1041 if (front_rear_side(r
->i_cm
.map
[ic
]) == front_rear_side(r
->o_cm
.map
[oc
])) {
1042 found_frs
[ic
] = true;
1047 for (oc
= 0; oc
< n_oc
; oc
++) {
1049 if (!on_left(r
->o_cm
.map
[oc
]) && !on_right(r
->o_cm
.map
[oc
]))
1052 if (!found_frs
[ic
] || front_rear_side(r
->i_cm
.map
[ic
]) == front_rear_side(r
->o_cm
.map
[oc
]))
1057 for (oc
= 0; oc
< n_oc
; oc
++) {
1059 if (!on_left(r
->o_cm
.map
[oc
]) && !on_right(r
->o_cm
.map
[oc
]))
1062 if (ncenter
[oc
] <= 0)
1065 for (ic
= 0; ic
< n_ic
; ic
++) {
1067 if (!on_center(r
->i_cm
.map
[ic
]))
1070 if (!found_frs
[ic
] || front_rear_side(r
->i_cm
.map
[ic
]) == front_rear_side(r
->o_cm
.map
[oc
]))
1071 m
->map_table_f
[oc
][ic
] = .5f
/ (float) ncenter
[oc
];
1077 for (oc
= 0; oc
< n_oc
; oc
++) {
1079 for (ic
= 0; ic
< n_ic
; ic
++)
1080 sum
+= m
->map_table_f
[oc
][ic
];
1083 for (ic
= 0; ic
< n_ic
; ic
++)
1084 m
->map_table_f
[oc
][ic
] /= sum
;
1087 /* make an 16:16 int version of the matrix */
1088 for (oc
= 0; oc
< n_oc
; oc
++)
1089 for (ic
= 0; ic
< n_ic
; ic
++)
1090 m
->map_table_i
[oc
][ic
] = (int32_t) (m
->map_table_f
[oc
][ic
] * 0x10000);
1092 s
= pa_strbuf_new();
1094 pa_strbuf_printf(s
, " ");
1095 for (ic
= 0; ic
< n_ic
; ic
++)
1096 pa_strbuf_printf(s
, " I%02u ", ic
);
1097 pa_strbuf_puts(s
, "\n +");
1099 for (ic
= 0; ic
< n_ic
; ic
++)
1100 pa_strbuf_printf(s
, "------");
1101 pa_strbuf_puts(s
, "\n");
1103 for (oc
= 0; oc
< n_oc
; oc
++) {
1104 pa_strbuf_printf(s
, "O%02u |", oc
);
1106 for (ic
= 0; ic
< n_ic
; ic
++)
1107 pa_strbuf_printf(s
, " %1.3f", m
->map_table_f
[oc
][ic
]);
1109 pa_strbuf_puts(s
, "\n");
1112 pa_log_debug("Channel matrix:\n%s", t
= pa_strbuf_tostring_free(s
));
1115 /* initialize the remapping function */
1119 static pa_memchunk
* convert_to_work_format(pa_resampler
*r
, pa_memchunk
*input
) {
1125 pa_assert(input
->memblock
);
1127 /* Convert the incoming sample into the work sample format and place them
1128 * in to_work_format_buf. */
1130 if (!r
->to_work_format_func
|| !input
->length
)
1133 n_samples
= (unsigned) ((input
->length
/ r
->i_fz
) * r
->i_ss
.channels
);
1135 r
->to_work_format_buf
.index
= 0;
1136 r
->to_work_format_buf
.length
= r
->w_sz
* n_samples
;
1138 if (!r
->to_work_format_buf
.memblock
|| r
->to_work_format_buf_samples
< n_samples
) {
1139 if (r
->to_work_format_buf
.memblock
)
1140 pa_memblock_unref(r
->to_work_format_buf
.memblock
);
1142 r
->to_work_format_buf_samples
= n_samples
;
1143 r
->to_work_format_buf
.memblock
= pa_memblock_new(r
->mempool
, r
->to_work_format_buf
.length
);
1146 src
= pa_memblock_acquire_chunk(input
);
1147 dst
= pa_memblock_acquire(r
->to_work_format_buf
.memblock
);
1149 r
->to_work_format_func(n_samples
, src
, dst
);
1151 pa_memblock_release(input
->memblock
);
1152 pa_memblock_release(r
->to_work_format_buf
.memblock
);
1154 return &r
->to_work_format_buf
;
1157 static pa_memchunk
*remap_channels(pa_resampler
*r
, pa_memchunk
*input
) {
1158 unsigned in_n_samples
, out_n_samples
, in_n_frames
, out_n_frames
;
1160 size_t leftover_length
= 0;
1165 pa_assert(input
->memblock
);
1167 /* Remap channels and place the result in remap_buf. There may be leftover
1168 * data in the beginning of remap_buf. The leftover data is already
1169 * remapped, so it's not part of the input, it's part of the output. */
1171 have_leftover
= r
->remap_buf_contains_leftover_data
;
1172 r
->remap_buf_contains_leftover_data
= false;
1174 if (!have_leftover
&& (!r
->map_required
|| input
->length
<= 0))
1176 else if (input
->length
<= 0)
1177 return &r
->remap_buf
;
1179 in_n_samples
= (unsigned) (input
->length
/ r
->w_sz
);
1180 in_n_frames
= out_n_frames
= in_n_samples
/ r
->i_ss
.channels
;
1182 if (have_leftover
) {
1183 leftover_length
= r
->remap_buf
.length
;
1184 out_n_frames
+= leftover_length
/ (r
->w_sz
* r
->o_ss
.channels
);
1187 out_n_samples
= out_n_frames
* r
->o_ss
.channels
;
1188 r
->remap_buf
.length
= out_n_samples
* r
->w_sz
;
1190 if (have_leftover
) {
1191 if (r
->remap_buf_size
< r
->remap_buf
.length
) {
1192 pa_memblock
*new_block
= pa_memblock_new(r
->mempool
, r
->remap_buf
.length
);
1194 src
= pa_memblock_acquire(r
->remap_buf
.memblock
);
1195 dst
= pa_memblock_acquire(new_block
);
1196 memcpy(dst
, src
, leftover_length
);
1197 pa_memblock_release(r
->remap_buf
.memblock
);
1198 pa_memblock_release(new_block
);
1200 pa_memblock_unref(r
->remap_buf
.memblock
);
1201 r
->remap_buf
.memblock
= new_block
;
1202 r
->remap_buf_size
= r
->remap_buf
.length
;
1206 if (!r
->remap_buf
.memblock
|| r
->remap_buf_size
< r
->remap_buf
.length
) {
1207 if (r
->remap_buf
.memblock
)
1208 pa_memblock_unref(r
->remap_buf
.memblock
);
1210 r
->remap_buf_size
= r
->remap_buf
.length
;
1211 r
->remap_buf
.memblock
= pa_memblock_new(r
->mempool
, r
->remap_buf
.length
);
1215 src
= pa_memblock_acquire_chunk(input
);
1216 dst
= (uint8_t *) pa_memblock_acquire(r
->remap_buf
.memblock
) + leftover_length
;
1218 if (r
->map_required
) {
1219 pa_remap_t
*remap
= &r
->remap
;
1221 pa_assert(remap
->do_remap
);
1222 remap
->do_remap(remap
, dst
, src
, in_n_frames
);
1225 memcpy(dst
, src
, input
->length
);
1227 pa_memblock_release(input
->memblock
);
1228 pa_memblock_release(r
->remap_buf
.memblock
);
1230 return &r
->remap_buf
;
1233 static pa_memchunk
*resample(pa_resampler
*r
, pa_memchunk
*input
) {
1234 unsigned in_n_frames
, in_n_samples
;
1235 unsigned out_n_frames
, out_n_samples
;
1240 /* Resample the data and place the result in resample_buf. */
1242 if (!r
->impl_resample
|| !input
->length
)
1245 in_n_samples
= (unsigned) (input
->length
/ r
->w_sz
);
1246 in_n_frames
= (unsigned) (in_n_samples
/ r
->o_ss
.channels
);
1248 out_n_frames
= ((in_n_frames
*r
->o_ss
.rate
)/r
->i_ss
.rate
)+EXTRA_FRAMES
;
1249 out_n_samples
= out_n_frames
* r
->o_ss
.channels
;
1251 r
->resample_buf
.index
= 0;
1252 r
->resample_buf
.length
= r
->w_sz
* out_n_samples
;
1254 if (!r
->resample_buf
.memblock
|| r
->resample_buf_samples
< out_n_samples
) {
1255 if (r
->resample_buf
.memblock
)
1256 pa_memblock_unref(r
->resample_buf
.memblock
);
1258 r
->resample_buf_samples
= out_n_samples
;
1259 r
->resample_buf
.memblock
= pa_memblock_new(r
->mempool
, r
->resample_buf
.length
);
1262 r
->impl_resample(r
, input
, in_n_frames
, &r
->resample_buf
, &out_n_frames
);
1263 r
->resample_buf
.length
= out_n_frames
* r
->w_sz
* r
->o_ss
.channels
;
1265 return &r
->resample_buf
;
1268 static pa_memchunk
*convert_from_work_format(pa_resampler
*r
, pa_memchunk
*input
) {
1269 unsigned n_samples
, n_frames
;
1275 /* Convert the data into the correct sample type and place the result in
1276 * from_work_format_buf. */
1278 if (!r
->from_work_format_func
|| !input
->length
)
1281 n_samples
= (unsigned) (input
->length
/ r
->w_sz
);
1282 n_frames
= n_samples
/ r
->o_ss
.channels
;
1284 r
->from_work_format_buf
.index
= 0;
1285 r
->from_work_format_buf
.length
= r
->o_fz
* n_frames
;
1287 if (!r
->from_work_format_buf
.memblock
|| r
->from_work_format_buf_samples
< n_samples
) {
1288 if (r
->from_work_format_buf
.memblock
)
1289 pa_memblock_unref(r
->from_work_format_buf
.memblock
);
1291 r
->from_work_format_buf_samples
= n_samples
;
1292 r
->from_work_format_buf
.memblock
= pa_memblock_new(r
->mempool
, r
->from_work_format_buf
.length
);
1295 src
= pa_memblock_acquire_chunk(input
);
1296 dst
= pa_memblock_acquire(r
->from_work_format_buf
.memblock
);
1297 r
->from_work_format_func(n_samples
, src
, dst
);
1298 pa_memblock_release(input
->memblock
);
1299 pa_memblock_release(r
->from_work_format_buf
.memblock
);
1301 return &r
->from_work_format_buf
;
1304 void pa_resampler_run(pa_resampler
*r
, const pa_memchunk
*in
, pa_memchunk
*out
) {
1310 pa_assert(in
->length
);
1311 pa_assert(in
->memblock
);
1312 pa_assert(in
->length
% r
->i_fz
== 0);
1314 buf
= (pa_memchunk
*) in
;
1315 buf
= convert_to_work_format(r
, buf
);
1316 buf
= remap_channels(r
, buf
);
1317 buf
= resample(r
, buf
);
1320 buf
= convert_from_work_format(r
, buf
);
1324 pa_memblock_ref(buf
->memblock
);
1326 pa_memchunk_reset(buf
);
1328 pa_memchunk_reset(out
);
1331 static void save_leftover(pa_resampler
*r
, void *buf
, size_t len
) {
1338 /* Store the leftover to remap_buf. */
1340 r
->remap_buf
.length
= len
;
1342 if (!r
->remap_buf
.memblock
|| r
->remap_buf_size
< r
->remap_buf
.length
) {
1343 if (r
->remap_buf
.memblock
)
1344 pa_memblock_unref(r
->remap_buf
.memblock
);
1346 r
->remap_buf_size
= r
->remap_buf
.length
;
1347 r
->remap_buf
.memblock
= pa_memblock_new(r
->mempool
, r
->remap_buf
.length
);
1350 dst
= pa_memblock_acquire(r
->remap_buf
.memblock
);
1351 memcpy(dst
, buf
, r
->remap_buf
.length
);
1352 pa_memblock_release(r
->remap_buf
.memblock
);
1354 r
->remap_buf_contains_leftover_data
= true;
1357 /*** libsamplerate based implementation ***/
1359 #ifdef HAVE_LIBSAMPLERATE
1360 static void libsamplerate_resample(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
1362 struct src_data
*libsamplerate_data
;
1367 pa_assert(out_n_frames
);
1369 libsamplerate_data
= r
->impl_data
;
1370 memset(&data
, 0, sizeof(data
));
1372 data
.data_in
= pa_memblock_acquire_chunk(input
);
1373 data
.input_frames
= (long int) in_n_frames
;
1375 data
.data_out
= pa_memblock_acquire_chunk(output
);
1376 data
.output_frames
= (long int) *out_n_frames
;
1378 data
.src_ratio
= (double) r
->o_ss
.rate
/ r
->i_ss
.rate
;
1379 data
.end_of_input
= 0;
1381 pa_assert_se(src_process(libsamplerate_data
->state
, &data
) == 0);
1383 if (data
.input_frames_used
< in_n_frames
) {
1384 void *leftover_data
= data
.data_in
+ data
.input_frames_used
* r
->o_ss
.channels
;
1385 size_t leftover_length
= (in_n_frames
- data
.input_frames_used
) * sizeof(float) * r
->o_ss
.channels
;
1387 save_leftover(r
, leftover_data
, leftover_length
);
1390 pa_memblock_release(input
->memblock
);
1391 pa_memblock_release(output
->memblock
);
1393 *out_n_frames
= (unsigned) data
.output_frames_gen
;
1396 static void libsamplerate_update_rates(pa_resampler
*r
) {
1397 struct src_data
*libsamplerate_data
;
1400 libsamplerate_data
= r
->impl_data
;
1401 pa_assert_se(src_set_ratio(libsamplerate_data
->state
, (double) r
->o_ss
.rate
/ r
->i_ss
.rate
) == 0);
1404 static void libsamplerate_reset(pa_resampler
*r
) {
1405 struct src_data
*libsamplerate_data
;
1408 libsamplerate_data
= r
->impl_data
;
1409 pa_assert_se(src_reset(libsamplerate_data
->state
) == 0);
1412 static void libsamplerate_free(pa_resampler
*r
) {
1413 struct src_data
*libsamplerate_data
;
1416 libsamplerate_data
= r
->impl_data
;
1417 if (libsamplerate_data
->state
)
1418 src_delete(libsamplerate_data
->state
);
1421 static int libsamplerate_init(pa_resampler
*r
) {
1423 struct src_data
*libsamplerate_data
;
1427 libsamplerate_data
= pa_xnew(struct src_data
, 1);
1429 if (!(libsamplerate_data
->state
= src_new(r
->method
, r
->o_ss
.channels
, &err
)))
1432 r
->impl_free
= libsamplerate_free
;
1433 r
->impl_update_rates
= libsamplerate_update_rates
;
1434 r
->impl_resample
= libsamplerate_resample
;
1435 r
->impl_reset
= libsamplerate_reset
;
1436 r
->impl_data
= libsamplerate_data
;
1443 /*** speex based implementation ***/
1445 static void speex_resample_float(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
1447 uint32_t inf
= in_n_frames
, outf
= *out_n_frames
;
1448 struct speex_data
*speex_data
;
1453 pa_assert(out_n_frames
);
1455 speex_data
= r
->impl_data
;
1457 in
= pa_memblock_acquire_chunk(input
);
1458 out
= pa_memblock_acquire_chunk(output
);
1460 pa_assert_se(speex_resampler_process_interleaved_float(speex_data
->state
, in
, &inf
, out
, &outf
) == 0);
1462 pa_memblock_release(input
->memblock
);
1463 pa_memblock_release(output
->memblock
);
1465 pa_assert(inf
== in_n_frames
);
1466 *out_n_frames
= outf
;
1469 static void speex_resample_int(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
1471 uint32_t inf
= in_n_frames
, outf
= *out_n_frames
;
1472 struct speex_data
*speex_data
;
1477 pa_assert(out_n_frames
);
1479 speex_data
= r
->impl_data
;
1481 in
= pa_memblock_acquire_chunk(input
);
1482 out
= pa_memblock_acquire_chunk(output
);
1484 pa_assert_se(speex_resampler_process_interleaved_int(speex_data
->state
, in
, &inf
, out
, &outf
) == 0);
1486 pa_memblock_release(input
->memblock
);
1487 pa_memblock_release(output
->memblock
);
1489 pa_assert(inf
== in_n_frames
);
1490 *out_n_frames
= outf
;
1493 static void speex_update_rates(pa_resampler
*r
) {
1494 struct speex_data
*speex_data
;
1497 speex_data
= r
->impl_data
;
1499 pa_assert_se(speex_resampler_set_rate(speex_data
->state
, r
->i_ss
.rate
, r
->o_ss
.rate
) == 0);
1502 static void speex_reset(pa_resampler
*r
) {
1503 struct speex_data
*speex_data
;
1506 speex_data
= r
->impl_data
;
1508 pa_assert_se(speex_resampler_reset_mem(speex_data
->state
) == 0);
1511 static void speex_free(pa_resampler
*r
) {
1512 struct speex_data
*speex_data
;
1515 speex_data
= r
->impl_data
;
1516 if (!speex_data
->state
)
1519 speex_resampler_destroy(speex_data
->state
);
1522 static int speex_init(pa_resampler
*r
) {
1524 struct speex_data
*speex_data
;
1528 speex_data
= pa_xnew(struct speex_data
, 1);
1530 r
->impl_free
= speex_free
;
1531 r
->impl_update_rates
= speex_update_rates
;
1532 r
->impl_reset
= speex_reset
;
1533 r
->impl_data
= speex_data
;
1535 if (r
->method
>= PA_RESAMPLER_SPEEX_FIXED_BASE
&& r
->method
<= PA_RESAMPLER_SPEEX_FIXED_MAX
) {
1537 q
= r
->method
- PA_RESAMPLER_SPEEX_FIXED_BASE
;
1538 r
->impl_resample
= speex_resample_int
;
1541 pa_assert(r
->method
>= PA_RESAMPLER_SPEEX_FLOAT_BASE
&& r
->method
<= PA_RESAMPLER_SPEEX_FLOAT_MAX
);
1543 q
= r
->method
- PA_RESAMPLER_SPEEX_FLOAT_BASE
;
1544 r
->impl_resample
= speex_resample_float
;
1547 pa_log_info("Choosing speex quality setting %i.", q
);
1549 if (!(speex_data
->state
= speex_resampler_init(r
->o_ss
.channels
, r
->i_ss
.rate
, r
->o_ss
.rate
, q
, &err
)))
1556 /* Trivial implementation */
1558 static void trivial_resample(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
1560 unsigned i_index
, o_index
;
1562 struct trivial_data
*trivial_data
;
1567 pa_assert(out_n_frames
);
1569 trivial_data
= r
->impl_data
;
1570 fz
= r
->w_sz
* r
->o_ss
.channels
;
1572 src
= pa_memblock_acquire_chunk(input
);
1573 dst
= pa_memblock_acquire_chunk(output
);
1575 for (o_index
= 0;; o_index
++, trivial_data
->o_counter
++) {
1576 i_index
= ((uint64_t) trivial_data
->o_counter
* r
->i_ss
.rate
) / r
->o_ss
.rate
;
1577 i_index
= i_index
> trivial_data
->i_counter
? i_index
- trivial_data
->i_counter
: 0;
1579 if (i_index
>= in_n_frames
)
1582 pa_assert_fp(o_index
* fz
< pa_memblock_get_length(output
->memblock
));
1584 memcpy((uint8_t*) dst
+ fz
* o_index
, (uint8_t*) src
+ fz
* i_index
, (int) fz
);
1587 pa_memblock_release(input
->memblock
);
1588 pa_memblock_release(output
->memblock
);
1590 *out_n_frames
= o_index
;
1592 trivial_data
->i_counter
+= in_n_frames
;
1594 /* Normalize counters */
1595 while (trivial_data
->i_counter
>= r
->i_ss
.rate
) {
1596 pa_assert(trivial_data
->o_counter
>= r
->o_ss
.rate
);
1598 trivial_data
->i_counter
-= r
->i_ss
.rate
;
1599 trivial_data
->o_counter
-= r
->o_ss
.rate
;
1603 static void trivial_update_rates_or_reset(pa_resampler
*r
) {
1604 struct trivial_data
*trivial_data
;
1607 trivial_data
= r
->impl_data
;
1609 trivial_data
->i_counter
= 0;
1610 trivial_data
->o_counter
= 0;
1613 static int trivial_init(pa_resampler
*r
) {
1614 struct trivial_data
*trivial_data
;
1617 trivial_data
= pa_xnew0(struct trivial_data
, 1);
1619 r
->impl_resample
= trivial_resample
;
1620 r
->impl_update_rates
= trivial_update_rates_or_reset
;
1621 r
->impl_reset
= trivial_update_rates_or_reset
;
1622 r
->impl_data
= trivial_data
;
1627 /* Peak finder implementation */
1629 static void peaks_resample(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
1630 unsigned c
, o_index
= 0;
1631 unsigned i
, i_end
= 0;
1633 struct peaks_data
*peaks_data
;
1638 pa_assert(out_n_frames
);
1640 peaks_data
= r
->impl_data
;
1641 src
= pa_memblock_acquire_chunk(input
);
1642 dst
= pa_memblock_acquire_chunk(output
);
1644 i
= ((uint64_t) peaks_data
->o_counter
* r
->i_ss
.rate
) / r
->o_ss
.rate
;
1645 i
= i
> peaks_data
->i_counter
? i
- peaks_data
->i_counter
: 0;
1647 while (i_end
< in_n_frames
) {
1648 i_end
= ((uint64_t) (peaks_data
->o_counter
+ 1) * r
->i_ss
.rate
) / r
->o_ss
.rate
;
1649 i_end
= i_end
> peaks_data
->i_counter
? i_end
- peaks_data
->i_counter
: 0;
1651 pa_assert_fp(o_index
* r
->w_sz
* r
->o_ss
.channels
< pa_memblock_get_length(output
->memblock
));
1653 /* 1ch float is treated separately, because that is the common case */
1654 if (r
->o_ss
.channels
== 1 && r
->work_format
== PA_SAMPLE_FLOAT32NE
) {
1655 float *s
= (float*) src
+ i
;
1656 float *d
= (float*) dst
+ o_index
;
1658 for (; i
< i_end
&& i
< in_n_frames
; i
++) {
1659 float n
= fabsf(*s
++);
1661 if (n
> peaks_data
->max_f
[0])
1662 peaks_data
->max_f
[0] = n
;
1666 *d
= peaks_data
->max_f
[0];
1667 peaks_data
->max_f
[0] = 0;
1668 o_index
++, peaks_data
->o_counter
++;
1670 } else if (r
->work_format
== PA_SAMPLE_S16NE
) {
1671 int16_t *s
= (int16_t*) src
+ r
->o_ss
.channels
* i
;
1672 int16_t *d
= (int16_t*) dst
+ r
->o_ss
.channels
* o_index
;
1674 for (; i
< i_end
&& i
< in_n_frames
; i
++)
1675 for (c
= 0; c
< r
->o_ss
.channels
; c
++) {
1676 int16_t n
= abs(*s
++);
1678 if (n
> peaks_data
->max_i
[c
])
1679 peaks_data
->max_i
[c
] = n
;
1683 for (c
= 0; c
< r
->o_ss
.channels
; c
++, d
++) {
1684 *d
= peaks_data
->max_i
[c
];
1685 peaks_data
->max_i
[c
] = 0;
1687 o_index
++, peaks_data
->o_counter
++;
1690 float *s
= (float*) src
+ r
->o_ss
.channels
* i
;
1691 float *d
= (float*) dst
+ r
->o_ss
.channels
* o_index
;
1693 for (; i
< i_end
&& i
< in_n_frames
; i
++)
1694 for (c
= 0; c
< r
->o_ss
.channels
; c
++) {
1695 float n
= fabsf(*s
++);
1697 if (n
> peaks_data
->max_f
[c
])
1698 peaks_data
->max_f
[c
] = n
;
1702 for (c
= 0; c
< r
->o_ss
.channels
; c
++, d
++) {
1703 *d
= peaks_data
->max_f
[c
];
1704 peaks_data
->max_f
[c
] = 0;
1706 o_index
++, peaks_data
->o_counter
++;
1711 pa_memblock_release(input
->memblock
);
1712 pa_memblock_release(output
->memblock
);
1714 *out_n_frames
= o_index
;
1716 peaks_data
->i_counter
+= in_n_frames
;
1718 /* Normalize counters */
1719 while (peaks_data
->i_counter
>= r
->i_ss
.rate
) {
1720 pa_assert(peaks_data
->o_counter
>= r
->o_ss
.rate
);
1722 peaks_data
->i_counter
-= r
->i_ss
.rate
;
1723 peaks_data
->o_counter
-= r
->o_ss
.rate
;
1727 static void peaks_update_rates_or_reset(pa_resampler
*r
) {
1728 struct peaks_data
*peaks_data
;
1731 peaks_data
= r
->impl_data
;
1733 peaks_data
->i_counter
= 0;
1734 peaks_data
->o_counter
= 0;
1737 static int peaks_init(pa_resampler
*r
) {
1738 struct peaks_data
*peaks_data
;
1740 pa_assert(r
->i_ss
.rate
>= r
->o_ss
.rate
);
1741 pa_assert(r
->work_format
== PA_SAMPLE_S16NE
|| r
->work_format
== PA_SAMPLE_FLOAT32NE
);
1743 peaks_data
= pa_xnew0(struct peaks_data
, 1);
1745 r
->impl_resample
= peaks_resample
;
1746 r
->impl_update_rates
= peaks_update_rates_or_reset
;
1747 r
->impl_reset
= peaks_update_rates_or_reset
;
1748 r
->impl_data
= peaks_data
;
1753 /*** ffmpeg based implementation ***/
1755 static void ffmpeg_resample(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
1756 unsigned used_frames
= 0, c
;
1757 int previous_consumed_frames
= -1;
1758 struct ffmpeg_data
*ffmpeg_data
;
1763 pa_assert(out_n_frames
);
1765 ffmpeg_data
= r
->impl_data
;
1767 for (c
= 0; c
< r
->o_ss
.channels
; c
++) {
1770 int16_t *p
, *t
, *k
, *q
, *s
;
1771 int consumed_frames
;
1773 /* Allocate a new block */
1774 b
= pa_memblock_new(r
->mempool
, ffmpeg_data
->buf
[c
].length
+ in_n_frames
* sizeof(int16_t));
1775 p
= pa_memblock_acquire(b
);
1777 /* Now copy the input data, splitting up channels */
1778 t
= (int16_t*) pa_memblock_acquire_chunk(input
) + c
;
1780 for (u
= 0; u
< in_n_frames
; u
++) {
1782 t
+= r
->o_ss
.channels
;
1785 pa_memblock_release(input
->memblock
);
1787 /* Allocate buffer for the result */
1788 w
= pa_memblock_new(r
->mempool
, *out_n_frames
* sizeof(int16_t));
1789 q
= pa_memblock_acquire(w
);
1792 used_frames
= (unsigned) av_resample(ffmpeg_data
->state
,
1795 (int) in_n_frames
, (int) *out_n_frames
,
1796 c
>= (unsigned) (r
->o_ss
.channels
-1));
1798 pa_memblock_release(b
);
1799 pa_memblock_unref(b
);
1801 pa_assert(consumed_frames
<= (int) in_n_frames
);
1802 pa_assert(previous_consumed_frames
== -1 || consumed_frames
== previous_consumed_frames
);
1803 previous_consumed_frames
= consumed_frames
;
1805 /* And place the results in the output buffer */
1806 s
= (int16_t *) pa_memblock_acquire_chunk(output
) + c
;
1807 for (u
= 0; u
< used_frames
; u
++) {
1810 s
+= r
->o_ss
.channels
;
1812 pa_memblock_release(output
->memblock
);
1813 pa_memblock_release(w
);
1814 pa_memblock_unref(w
);
1817 if (previous_consumed_frames
< (int) in_n_frames
) {
1818 void *leftover_data
= (int16_t *) pa_memblock_acquire_chunk(input
) + previous_consumed_frames
* r
->o_ss
.channels
;
1819 size_t leftover_length
= (in_n_frames
- previous_consumed_frames
) * r
->o_ss
.channels
* sizeof(int16_t);
1821 save_leftover(r
, leftover_data
, leftover_length
);
1822 pa_memblock_release(input
->memblock
);
1825 *out_n_frames
= used_frames
;
1828 static void ffmpeg_free(pa_resampler
*r
) {
1830 struct ffmpeg_data
*ffmpeg_data
;
1834 ffmpeg_data
= r
->impl_data
;
1835 if (ffmpeg_data
->state
)
1836 av_resample_close(ffmpeg_data
->state
);
1838 for (c
= 0; c
< PA_ELEMENTSOF(ffmpeg_data
->buf
); c
++)
1839 if (ffmpeg_data
->buf
[c
].memblock
)
1840 pa_memblock_unref(ffmpeg_data
->buf
[c
].memblock
);
1843 static int ffmpeg_init(pa_resampler
*r
) {
1845 struct ffmpeg_data
*ffmpeg_data
;
1849 ffmpeg_data
= pa_xnew(struct ffmpeg_data
, 1);
1851 /* We could probably implement different quality levels by
1852 * adjusting the filter parameters here. However, ffmpeg
1853 * internally only uses these hardcoded values, so let's use them
1854 * here for now as well until ffmpeg makes this configurable. */
1856 if (!(ffmpeg_data
->state
= av_resample_init((int) r
->o_ss
.rate
, (int) r
->i_ss
.rate
, 16, 10, 0, 0.8)))
1859 r
->impl_free
= ffmpeg_free
;
1860 r
->impl_resample
= ffmpeg_resample
;
1861 r
->impl_data
= (void *) ffmpeg_data
;
1863 for (c
= 0; c
< PA_ELEMENTSOF(ffmpeg_data
->buf
); c
++)
1864 pa_memchunk_reset(&ffmpeg_data
->buf
[c
]);
1869 /*** copy (noop) implementation ***/
1871 static int copy_init(pa_resampler
*r
) {
1874 pa_assert(r
->o_ss
.rate
== r
->i_ss
.rate
);