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
;
233 if (method
== PA_RESAMPLER_AUTO
) {
235 method
= PA_RESAMPLER_SPEEX_FLOAT_BASE
+ 1;
237 if (flags
& PA_RESAMPLER_VARIABLE_RATE
)
238 method
= PA_RESAMPLER_TRIVIAL
;
240 method
= PA_RESAMPLER_FFMPEG
;
247 static pa_sample_format_t
pa_resampler_choose_work_format(
248 pa_resample_method_t method
,
249 pa_sample_format_t a
,
250 pa_sample_format_t b
,
252 pa_sample_format_t work_format
;
254 pa_assert(a
>= 0 && a
< PA_SAMPLE_MAX
);
255 pa_assert(b
>= 0 && b
< PA_SAMPLE_MAX
);
256 pa_assert(method
>= 0);
257 pa_assert(method
< PA_RESAMPLER_MAX
);
259 if ((method
>= PA_RESAMPLER_SPEEX_FIXED_BASE
&& method
<= PA_RESAMPLER_SPEEX_FIXED_MAX
) ||
260 (method
== PA_RESAMPLER_FFMPEG
))
261 work_format
= PA_SAMPLE_S16NE
;
262 else if (method
== PA_RESAMPLER_TRIVIAL
|| method
== PA_RESAMPLER_COPY
|| method
== PA_RESAMPLER_PEAKS
) {
264 if (map_required
|| a
!= b
|| method
== PA_RESAMPLER_PEAKS
) {
266 if (a
== PA_SAMPLE_S16NE
|| b
== PA_SAMPLE_S16NE
)
267 work_format
= PA_SAMPLE_S16NE
;
268 else if (a
== PA_SAMPLE_S32NE
|| a
== PA_SAMPLE_S32RE
||
269 a
== PA_SAMPLE_FLOAT32NE
|| a
== PA_SAMPLE_FLOAT32RE
||
270 a
== PA_SAMPLE_S24NE
|| a
== PA_SAMPLE_S24RE
||
271 a
== PA_SAMPLE_S24_32NE
|| a
== PA_SAMPLE_S24_32RE
||
272 b
== PA_SAMPLE_S32NE
|| b
== PA_SAMPLE_S32RE
||
273 b
== PA_SAMPLE_FLOAT32NE
|| b
== PA_SAMPLE_FLOAT32RE
||
274 b
== PA_SAMPLE_S24NE
|| b
== PA_SAMPLE_S24RE
||
275 b
== PA_SAMPLE_S24_32NE
|| b
== PA_SAMPLE_S24_32RE
)
276 work_format
= PA_SAMPLE_FLOAT32NE
;
278 work_format
= PA_SAMPLE_S16NE
;
284 work_format
= PA_SAMPLE_FLOAT32NE
;
289 pa_resampler
* pa_resampler_new(
291 const pa_sample_spec
*a
,
292 const pa_channel_map
*am
,
293 const pa_sample_spec
*b
,
294 const pa_channel_map
*bm
,
295 pa_resample_method_t method
,
296 pa_resample_flags_t flags
) {
298 pa_resampler
*r
= NULL
;
303 pa_assert(pa_sample_spec_valid(a
));
304 pa_assert(pa_sample_spec_valid(b
));
305 pa_assert(method
>= 0);
306 pa_assert(method
< PA_RESAMPLER_MAX
);
308 method
= pa_resampler_fix_method(flags
, method
, a
->rate
, b
->rate
);
310 r
= pa_xnew0(pa_resampler
, 1);
315 /* Fill sample specs */
319 /* set up the remap structure */
320 r
->remap
.i_ss
= &r
->i_ss
;
321 r
->remap
.o_ss
= &r
->o_ss
;
322 r
->remap
.format
= &r
->work_format
;
326 else if (!pa_channel_map_init_auto(&r
->i_cm
, r
->i_ss
.channels
, PA_CHANNEL_MAP_DEFAULT
))
331 else if (!pa_channel_map_init_auto(&r
->o_cm
, r
->o_ss
.channels
, PA_CHANNEL_MAP_DEFAULT
))
334 r
->i_fz
= pa_frame_size(a
);
335 r
->o_fz
= pa_frame_size(b
);
339 pa_log_info("Using resampler '%s'", pa_resample_method_to_string(method
));
341 r
->work_format
= pa_resampler_choose_work_format(method
, a
->format
, b
->format
, r
->map_required
);
343 pa_log_info("Using %s as working format.", pa_sample_format_to_string(r
->work_format
));
345 r
->w_sz
= pa_sample_size_of_format(r
->work_format
);
347 if (r
->i_ss
.format
!= r
->work_format
) {
348 if (r
->work_format
== PA_SAMPLE_FLOAT32NE
) {
349 if (!(r
->to_work_format_func
= pa_get_convert_to_float32ne_function(r
->i_ss
.format
)))
352 pa_assert(r
->work_format
== PA_SAMPLE_S16NE
);
353 if (!(r
->to_work_format_func
= pa_get_convert_to_s16ne_function(r
->i_ss
.format
)))
358 if (r
->o_ss
.format
!= r
->work_format
) {
359 if (r
->work_format
== PA_SAMPLE_FLOAT32NE
) {
360 if (!(r
->from_work_format_func
= pa_get_convert_from_float32ne_function(r
->o_ss
.format
)))
363 pa_assert(r
->work_format
== PA_SAMPLE_S16NE
);
364 if (!(r
->from_work_format_func
= pa_get_convert_from_s16ne_function(r
->o_ss
.format
)))
369 /* initialize implementation */
370 if (init_table
[method
](r
) < 0)
381 void pa_resampler_free(pa_resampler
*r
) {
387 if (r
->to_work_format_buf
.memblock
)
388 pa_memblock_unref(r
->to_work_format_buf
.memblock
);
389 if (r
->remap_buf
.memblock
)
390 pa_memblock_unref(r
->remap_buf
.memblock
);
391 if (r
->resample_buf
.memblock
)
392 pa_memblock_unref(r
->resample_buf
.memblock
);
393 if (r
->from_work_format_buf
.memblock
)
394 pa_memblock_unref(r
->from_work_format_buf
.memblock
);
396 pa_xfree(r
->impl_data
);
400 void pa_resampler_set_input_rate(pa_resampler
*r
, uint32_t rate
) {
404 if (r
->i_ss
.rate
== rate
)
409 r
->impl_update_rates(r
);
412 void pa_resampler_set_output_rate(pa_resampler
*r
, uint32_t rate
) {
416 if (r
->o_ss
.rate
== rate
)
421 r
->impl_update_rates(r
);
424 size_t pa_resampler_request(pa_resampler
*r
, size_t out_length
) {
427 /* Let's round up here to make it more likely that the caller will get at
428 * least out_length amount of data from pa_resampler_run().
430 * We don't take the leftover into account here. If we did, then it might
431 * be in theory possible that this function would return 0 and
432 * pa_resampler_run() would also return 0. That could lead to infinite
433 * loops. When the leftover is ignored here, such loops would eventually
434 * terminate, because the leftover would grow each round, finally
435 * surpassing the minimum input threshold of the resampler. */
436 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
;
439 size_t pa_resampler_result(pa_resampler
*r
, size_t in_length
) {
444 /* Let's round up here to ensure that the caller will always allocate big
445 * enough output buffer. */
447 frames
= (in_length
+ r
->i_fz
- 1) / r
->i_fz
;
449 if (r
->remap_buf_contains_leftover_data
)
450 frames
+= r
->remap_buf
.length
/ (r
->w_sz
* r
->o_ss
.channels
);
452 return (((uint64_t) frames
* r
->o_ss
.rate
+ r
->i_ss
.rate
- 1) / r
->i_ss
.rate
) * r
->o_fz
;
455 size_t pa_resampler_max_block_size(pa_resampler
*r
) {
456 size_t block_size_max
;
457 pa_sample_spec max_ss
;
463 block_size_max
= pa_mempool_block_size_max(r
->mempool
);
465 /* We deduce the "largest" sample spec we're using during the
467 max_ss
.channels
= (uint8_t) (PA_MAX(r
->i_ss
.channels
, r
->o_ss
.channels
));
469 /* We silently assume that the format enum is ordered by size */
470 max_ss
.format
= PA_MAX(r
->i_ss
.format
, r
->o_ss
.format
);
471 max_ss
.format
= PA_MAX(max_ss
.format
, r
->work_format
);
473 max_ss
.rate
= PA_MAX(r
->i_ss
.rate
, r
->o_ss
.rate
);
475 max_fs
= pa_frame_size(&max_ss
);
476 frames
= block_size_max
/ max_fs
- EXTRA_FRAMES
;
478 if (r
->remap_buf_contains_leftover_data
)
479 frames
-= r
->remap_buf
.length
/ (r
->w_sz
* r
->o_ss
.channels
);
481 return ((uint64_t) frames
* r
->i_ss
.rate
/ max_ss
.rate
) * r
->i_fz
;
484 void pa_resampler_reset(pa_resampler
*r
) {
490 r
->remap_buf_contains_leftover_data
= false;
493 pa_resample_method_t
pa_resampler_get_method(pa_resampler
*r
) {
499 const pa_channel_map
* pa_resampler_input_channel_map(pa_resampler
*r
) {
505 const pa_sample_spec
* pa_resampler_input_sample_spec(pa_resampler
*r
) {
511 const pa_channel_map
* pa_resampler_output_channel_map(pa_resampler
*r
) {
517 const pa_sample_spec
* pa_resampler_output_sample_spec(pa_resampler
*r
) {
523 static const char * const resample_methods
[] = {
524 "src-sinc-best-quality",
525 "src-sinc-medium-quality",
527 "src-zero-order-hold",
558 const char *pa_resample_method_to_string(pa_resample_method_t m
) {
560 if (m
< 0 || m
>= PA_RESAMPLER_MAX
)
563 return resample_methods
[m
];
566 int pa_resample_method_supported(pa_resample_method_t m
) {
568 if (m
< 0 || m
>= PA_RESAMPLER_MAX
)
571 #ifndef HAVE_LIBSAMPLERATE
572 if (m
<= PA_RESAMPLER_SRC_LINEAR
)
577 if (m
>= PA_RESAMPLER_SPEEX_FLOAT_BASE
&& m
<= PA_RESAMPLER_SPEEX_FLOAT_MAX
)
579 if (m
>= PA_RESAMPLER_SPEEX_FIXED_BASE
&& m
<= PA_RESAMPLER_SPEEX_FIXED_MAX
)
586 pa_resample_method_t
pa_parse_resample_method(const char *string
) {
587 pa_resample_method_t m
;
591 for (m
= 0; m
< PA_RESAMPLER_MAX
; m
++)
592 if (pa_streq(string
, resample_methods
[m
]))
595 if (pa_streq(string
, "speex-fixed"))
596 return PA_RESAMPLER_SPEEX_FIXED_BASE
+ 1;
598 if (pa_streq(string
, "speex-float"))
599 return PA_RESAMPLER_SPEEX_FLOAT_BASE
+ 1;
601 return PA_RESAMPLER_INVALID
;
604 static bool on_left(pa_channel_position_t p
) {
607 p
== PA_CHANNEL_POSITION_FRONT_LEFT
||
608 p
== PA_CHANNEL_POSITION_REAR_LEFT
||
609 p
== PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER
||
610 p
== PA_CHANNEL_POSITION_SIDE_LEFT
||
611 p
== PA_CHANNEL_POSITION_TOP_FRONT_LEFT
||
612 p
== PA_CHANNEL_POSITION_TOP_REAR_LEFT
;
615 static bool on_right(pa_channel_position_t p
) {
618 p
== PA_CHANNEL_POSITION_FRONT_RIGHT
||
619 p
== PA_CHANNEL_POSITION_REAR_RIGHT
||
620 p
== PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER
||
621 p
== PA_CHANNEL_POSITION_SIDE_RIGHT
||
622 p
== PA_CHANNEL_POSITION_TOP_FRONT_RIGHT
||
623 p
== PA_CHANNEL_POSITION_TOP_REAR_RIGHT
;
626 static bool on_center(pa_channel_position_t p
) {
629 p
== PA_CHANNEL_POSITION_FRONT_CENTER
||
630 p
== PA_CHANNEL_POSITION_REAR_CENTER
||
631 p
== PA_CHANNEL_POSITION_TOP_CENTER
||
632 p
== PA_CHANNEL_POSITION_TOP_FRONT_CENTER
||
633 p
== PA_CHANNEL_POSITION_TOP_REAR_CENTER
;
636 static bool on_lfe(pa_channel_position_t p
) {
638 p
== PA_CHANNEL_POSITION_LFE
;
641 static bool on_front(pa_channel_position_t p
) {
643 p
== PA_CHANNEL_POSITION_FRONT_LEFT
||
644 p
== PA_CHANNEL_POSITION_FRONT_RIGHT
||
645 p
== PA_CHANNEL_POSITION_FRONT_CENTER
||
646 p
== PA_CHANNEL_POSITION_TOP_FRONT_LEFT
||
647 p
== PA_CHANNEL_POSITION_TOP_FRONT_RIGHT
||
648 p
== PA_CHANNEL_POSITION_TOP_FRONT_CENTER
||
649 p
== PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER
||
650 p
== PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER
;
653 static bool on_rear(pa_channel_position_t p
) {
655 p
== PA_CHANNEL_POSITION_REAR_LEFT
||
656 p
== PA_CHANNEL_POSITION_REAR_RIGHT
||
657 p
== PA_CHANNEL_POSITION_REAR_CENTER
||
658 p
== PA_CHANNEL_POSITION_TOP_REAR_LEFT
||
659 p
== PA_CHANNEL_POSITION_TOP_REAR_RIGHT
||
660 p
== PA_CHANNEL_POSITION_TOP_REAR_CENTER
;
663 static bool on_side(pa_channel_position_t p
) {
665 p
== PA_CHANNEL_POSITION_SIDE_LEFT
||
666 p
== PA_CHANNEL_POSITION_SIDE_RIGHT
||
667 p
== PA_CHANNEL_POSITION_TOP_CENTER
;
677 static int front_rear_side(pa_channel_position_t p
) {
687 static void calc_map_table(pa_resampler
*r
) {
690 bool ic_connected
[PA_CHANNELS_MAX
];
698 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
)))))
703 n_oc
= r
->o_ss
.channels
;
704 n_ic
= r
->i_ss
.channels
;
706 memset(m
->map_table_f
, 0, sizeof(m
->map_table_f
));
707 memset(m
->map_table_i
, 0, sizeof(m
->map_table_i
));
709 memset(ic_connected
, 0, sizeof(ic_connected
));
710 remix
= (r
->flags
& (PA_RESAMPLER_NO_REMAP
| PA_RESAMPLER_NO_REMIX
)) == 0;
712 if (r
->flags
& PA_RESAMPLER_NO_REMAP
) {
715 for (oc
= 0; oc
< PA_MIN(n_ic
, n_oc
); oc
++)
716 m
->map_table_f
[oc
][oc
] = 1.0f
;
718 } else if (r
->flags
& PA_RESAMPLER_NO_REMIX
) {
720 for (oc
= 0; oc
< n_oc
; oc
++) {
721 pa_channel_position_t b
= r
->o_cm
.map
[oc
];
723 for (ic
= 0; ic
< n_ic
; ic
++) {
724 pa_channel_position_t a
= r
->i_cm
.map
[ic
];
726 /* We shall not do any remixing. Hence, just check by name */
728 m
->map_table_f
[oc
][ic
] = 1.0f
;
733 /* OK, we shall do the full monty: upmixing and downmixing. Our
734 * algorithm is relatively simple, does not do spacialization, delay
735 * elements or apply lowpass filters for LFE. Patches are always
736 * welcome, though. Oh, and it doesn't do any matrix decoding. (Which
737 * probably wouldn't make any sense anyway.)
739 * This code is not idempotent: downmixing an upmixed stereo stream is
740 * not identical to the original. The volume will not match, and the
741 * two channels will be a linear combination of both.
743 * This is loosely based on random suggestions found on the Internet,
745 * http://www.halfgaar.net/surround-sound-in-linux and the alsa upmix
748 * The algorithm works basically like this:
750 * 1) Connect all channels with matching names.
753 * S:Mono: Copy into all D:channels
754 * D:Mono: Avg all S:channels
756 * 3) Mix D:Left, D:Right:
757 * D:Left: If not connected, avg all S:Left
758 * D:Right: If not connected, avg all S:Right
761 * If not connected, avg all S:Center
762 * If still not connected, avg all S:Left, S:Right
765 * If not connected, avg all S:*
767 * 6) Make sure S:Left/S:Right is used: S:Left/S:Right: If not
768 * connected, mix into all D:left and all D:right channels. Gain is
771 * 7) Make sure S:Center, S:LFE is used:
773 * S:Center, S:LFE: If not connected, mix into all D:left, all
774 * D:right, all D:center channels. Gain is 0.5 for center and 0.375
775 * for LFE. C-front is only mixed into L-front/R-front if available,
776 * otherwise into all L/R channels. Similarly for C-rear.
778 * 8) Normalize each row in the matrix such that the sum for each row is
779 * not larger than 1.0 in order to avoid clipping.
781 * S: and D: shall relate to the source resp. destination channels.
783 * Rationale: 1, 2 are probably obvious. For 3: this copies front to
784 * rear if needed. For 4: we try to find some suitable C source for C,
785 * if we don't find any, we avg L and R. For 5: LFE is mixed from all
786 * channels. For 6: the rear channels should not be dropped entirely,
787 * however have only minimal impact. For 7: movies usually encode
788 * speech on the center channel. Thus we have to make sure this channel
789 * is distributed to L and R if not available in the output. Also, LFE
790 * is used to achieve a greater dynamic range, and thus we should try
791 * to do our best to pass it to L+R.
798 ic_unconnected_left
= 0,
799 ic_unconnected_right
= 0,
800 ic_unconnected_center
= 0,
801 ic_unconnected_lfe
= 0;
802 bool ic_unconnected_center_mixed_in
= 0;
806 for (ic
= 0; ic
< n_ic
; ic
++) {
807 if (on_left(r
->i_cm
.map
[ic
]))
809 if (on_right(r
->i_cm
.map
[ic
]))
811 if (on_center(r
->i_cm
.map
[ic
]))
815 for (oc
= 0; oc
< n_oc
; oc
++) {
816 bool oc_connected
= false;
817 pa_channel_position_t b
= r
->o_cm
.map
[oc
];
819 for (ic
= 0; ic
< n_ic
; ic
++) {
820 pa_channel_position_t a
= r
->i_cm
.map
[ic
];
822 if (a
== b
|| a
== PA_CHANNEL_POSITION_MONO
) {
823 m
->map_table_f
[oc
][ic
] = 1.0f
;
826 ic_connected
[ic
] = true;
828 else if (b
== PA_CHANNEL_POSITION_MONO
) {
829 m
->map_table_f
[oc
][ic
] = 1.0f
/ (float) n_ic
;
832 ic_connected
[ic
] = true;
837 /* Try to find matching input ports for this output port */
841 /* We are not connected and on the left side, let's
842 * average all left side input channels. */
845 for (ic
= 0; ic
< n_ic
; ic
++)
846 if (on_left(r
->i_cm
.map
[ic
])) {
847 m
->map_table_f
[oc
][ic
] = 1.0f
/ (float) ic_left
;
848 ic_connected
[ic
] = true;
851 /* We ignore the case where there is no left input channel.
852 * Something is really wrong in this case anyway. */
854 } else if (on_right(b
)) {
856 /* We are not connected and on the right side, let's
857 * average all right side input channels. */
860 for (ic
= 0; ic
< n_ic
; ic
++)
861 if (on_right(r
->i_cm
.map
[ic
])) {
862 m
->map_table_f
[oc
][ic
] = 1.0f
/ (float) ic_right
;
863 ic_connected
[ic
] = true;
866 /* We ignore the case where there is no right input
867 * channel. Something is really wrong in this case anyway.
870 } else if (on_center(b
)) {
874 /* We are not connected and at the center. Let's average
875 * all center input channels. */
877 for (ic
= 0; ic
< n_ic
; ic
++)
878 if (on_center(r
->i_cm
.map
[ic
])) {
879 m
->map_table_f
[oc
][ic
] = 1.0f
/ (float) ic_center
;
880 ic_connected
[ic
] = true;
883 } else if (ic_left
+ ic_right
> 0) {
885 /* Hmm, no center channel around, let's synthesize it
886 * by mixing L and R.*/
888 for (ic
= 0; ic
< n_ic
; ic
++)
889 if (on_left(r
->i_cm
.map
[ic
]) || on_right(r
->i_cm
.map
[ic
])) {
890 m
->map_table_f
[oc
][ic
] = 1.0f
/ (float) (ic_left
+ ic_right
);
891 ic_connected
[ic
] = true;
895 /* We ignore the case where there is not even a left or
896 * right input channel. Something is really wrong in this
899 } else if (on_lfe(b
) && !(r
->flags
& PA_RESAMPLER_NO_LFE
)) {
901 /* We are not connected and an LFE. Let's average all
902 * channels for LFE. */
904 for (ic
= 0; ic
< n_ic
; ic
++)
905 m
->map_table_f
[oc
][ic
] = 1.0f
/ (float) n_ic
;
907 /* Please note that a channel connected to LFE doesn't
908 * really count as connected. */
913 for (ic
= 0; ic
< n_ic
; ic
++) {
914 pa_channel_position_t a
= r
->i_cm
.map
[ic
];
916 if (ic_connected
[ic
])
920 ic_unconnected_left
++;
921 else if (on_right(a
))
922 ic_unconnected_right
++;
923 else if (on_center(a
))
924 ic_unconnected_center
++;
926 ic_unconnected_lfe
++;
929 for (ic
= 0; ic
< n_ic
; ic
++) {
930 pa_channel_position_t a
= r
->i_cm
.map
[ic
];
932 if (ic_connected
[ic
])
935 for (oc
= 0; oc
< n_oc
; oc
++) {
936 pa_channel_position_t b
= r
->o_cm
.map
[oc
];
938 if (on_left(a
) && on_left(b
))
939 m
->map_table_f
[oc
][ic
] = (1.f
/9.f
) / (float) ic_unconnected_left
;
941 else if (on_right(a
) && on_right(b
))
942 m
->map_table_f
[oc
][ic
] = (1.f
/9.f
) / (float) ic_unconnected_right
;
944 else if (on_center(a
) && on_center(b
)) {
945 m
->map_table_f
[oc
][ic
] = (1.f
/9.f
) / (float) ic_unconnected_center
;
946 ic_unconnected_center_mixed_in
= true;
948 } else if (on_lfe(a
) && !(r
->flags
& PA_RESAMPLER_NO_LFE
))
949 m
->map_table_f
[oc
][ic
] = .375f
/ (float) ic_unconnected_lfe
;
953 if (ic_unconnected_center
> 0 && !ic_unconnected_center_mixed_in
) {
954 unsigned ncenter
[PA_CHANNELS_MAX
];
955 bool found_frs
[PA_CHANNELS_MAX
];
957 memset(ncenter
, 0, sizeof(ncenter
));
958 memset(found_frs
, 0, sizeof(found_frs
));
960 /* Hmm, as it appears there was no center channel we
961 could mix our center channel in. In this case, mix it into
962 left and right. Using .5 as the factor. */
964 for (ic
= 0; ic
< n_ic
; ic
++) {
966 if (ic_connected
[ic
])
969 if (!on_center(r
->i_cm
.map
[ic
]))
972 for (oc
= 0; oc
< n_oc
; oc
++) {
974 if (!on_left(r
->o_cm
.map
[oc
]) && !on_right(r
->o_cm
.map
[oc
]))
977 if (front_rear_side(r
->i_cm
.map
[ic
]) == front_rear_side(r
->o_cm
.map
[oc
])) {
978 found_frs
[ic
] = true;
983 for (oc
= 0; oc
< n_oc
; oc
++) {
985 if (!on_left(r
->o_cm
.map
[oc
]) && !on_right(r
->o_cm
.map
[oc
]))
988 if (!found_frs
[ic
] || front_rear_side(r
->i_cm
.map
[ic
]) == front_rear_side(r
->o_cm
.map
[oc
]))
993 for (oc
= 0; oc
< n_oc
; oc
++) {
995 if (!on_left(r
->o_cm
.map
[oc
]) && !on_right(r
->o_cm
.map
[oc
]))
998 if (ncenter
[oc
] <= 0)
1001 for (ic
= 0; ic
< n_ic
; ic
++) {
1003 if (!on_center(r
->i_cm
.map
[ic
]))
1006 if (!found_frs
[ic
] || front_rear_side(r
->i_cm
.map
[ic
]) == front_rear_side(r
->o_cm
.map
[oc
]))
1007 m
->map_table_f
[oc
][ic
] = .5f
/ (float) ncenter
[oc
];
1013 for (oc
= 0; oc
< n_oc
; oc
++) {
1015 for (ic
= 0; ic
< n_ic
; ic
++)
1016 sum
+= m
->map_table_f
[oc
][ic
];
1019 for (ic
= 0; ic
< n_ic
; ic
++)
1020 m
->map_table_f
[oc
][ic
] /= sum
;
1023 /* make an 16:16 int version of the matrix */
1024 for (oc
= 0; oc
< n_oc
; oc
++)
1025 for (ic
= 0; ic
< n_ic
; ic
++)
1026 m
->map_table_i
[oc
][ic
] = (int32_t) (m
->map_table_f
[oc
][ic
] * 0x10000);
1028 s
= pa_strbuf_new();
1030 pa_strbuf_printf(s
, " ");
1031 for (ic
= 0; ic
< n_ic
; ic
++)
1032 pa_strbuf_printf(s
, " I%02u ", ic
);
1033 pa_strbuf_puts(s
, "\n +");
1035 for (ic
= 0; ic
< n_ic
; ic
++)
1036 pa_strbuf_printf(s
, "------");
1037 pa_strbuf_puts(s
, "\n");
1039 for (oc
= 0; oc
< n_oc
; oc
++) {
1040 pa_strbuf_printf(s
, "O%02u |", oc
);
1042 for (ic
= 0; ic
< n_ic
; ic
++)
1043 pa_strbuf_printf(s
, " %1.3f", m
->map_table_f
[oc
][ic
]);
1045 pa_strbuf_puts(s
, "\n");
1048 pa_log_debug("Channel matrix:\n%s", t
= pa_strbuf_tostring_free(s
));
1051 /* initialize the remapping function */
1055 static pa_memchunk
* convert_to_work_format(pa_resampler
*r
, pa_memchunk
*input
) {
1061 pa_assert(input
->memblock
);
1063 /* Convert the incoming sample into the work sample format and place them
1064 * in to_work_format_buf. */
1066 if (!r
->to_work_format_func
|| !input
->length
)
1069 n_samples
= (unsigned) ((input
->length
/ r
->i_fz
) * r
->i_ss
.channels
);
1071 r
->to_work_format_buf
.index
= 0;
1072 r
->to_work_format_buf
.length
= r
->w_sz
* n_samples
;
1074 if (!r
->to_work_format_buf
.memblock
|| r
->to_work_format_buf_samples
< n_samples
) {
1075 if (r
->to_work_format_buf
.memblock
)
1076 pa_memblock_unref(r
->to_work_format_buf
.memblock
);
1078 r
->to_work_format_buf_samples
= n_samples
;
1079 r
->to_work_format_buf
.memblock
= pa_memblock_new(r
->mempool
, r
->to_work_format_buf
.length
);
1082 src
= pa_memblock_acquire_chunk(input
);
1083 dst
= pa_memblock_acquire(r
->to_work_format_buf
.memblock
);
1085 r
->to_work_format_func(n_samples
, src
, dst
);
1087 pa_memblock_release(input
->memblock
);
1088 pa_memblock_release(r
->to_work_format_buf
.memblock
);
1090 return &r
->to_work_format_buf
;
1093 static pa_memchunk
*remap_channels(pa_resampler
*r
, pa_memchunk
*input
) {
1094 unsigned in_n_samples
, out_n_samples
, in_n_frames
, out_n_frames
;
1096 size_t leftover_length
= 0;
1101 pa_assert(input
->memblock
);
1103 /* Remap channels and place the result in remap_buf. There may be leftover
1104 * data in the beginning of remap_buf. The leftover data is already
1105 * remapped, so it's not part of the input, it's part of the output. */
1107 have_leftover
= r
->remap_buf_contains_leftover_data
;
1108 r
->remap_buf_contains_leftover_data
= false;
1110 if (!have_leftover
&& (!r
->map_required
|| input
->length
<= 0))
1112 else if (input
->length
<= 0)
1113 return &r
->remap_buf
;
1115 in_n_samples
= (unsigned) (input
->length
/ r
->w_sz
);
1116 in_n_frames
= out_n_frames
= in_n_samples
/ r
->i_ss
.channels
;
1118 if (have_leftover
) {
1119 leftover_length
= r
->remap_buf
.length
;
1120 out_n_frames
+= leftover_length
/ (r
->w_sz
* r
->o_ss
.channels
);
1123 out_n_samples
= out_n_frames
* r
->o_ss
.channels
;
1124 r
->remap_buf
.length
= out_n_samples
* r
->w_sz
;
1126 if (have_leftover
) {
1127 if (r
->remap_buf_size
< r
->remap_buf
.length
) {
1128 pa_memblock
*new_block
= pa_memblock_new(r
->mempool
, r
->remap_buf
.length
);
1130 src
= pa_memblock_acquire(r
->remap_buf
.memblock
);
1131 dst
= pa_memblock_acquire(new_block
);
1132 memcpy(dst
, src
, leftover_length
);
1133 pa_memblock_release(r
->remap_buf
.memblock
);
1134 pa_memblock_release(new_block
);
1136 pa_memblock_unref(r
->remap_buf
.memblock
);
1137 r
->remap_buf
.memblock
= new_block
;
1138 r
->remap_buf_size
= r
->remap_buf
.length
;
1142 if (!r
->remap_buf
.memblock
|| r
->remap_buf_size
< r
->remap_buf
.length
) {
1143 if (r
->remap_buf
.memblock
)
1144 pa_memblock_unref(r
->remap_buf
.memblock
);
1146 r
->remap_buf_size
= r
->remap_buf
.length
;
1147 r
->remap_buf
.memblock
= pa_memblock_new(r
->mempool
, r
->remap_buf
.length
);
1151 src
= pa_memblock_acquire_chunk(input
);
1152 dst
= (uint8_t *) pa_memblock_acquire(r
->remap_buf
.memblock
) + leftover_length
;
1154 if (r
->map_required
) {
1155 pa_remap_t
*remap
= &r
->remap
;
1157 pa_assert(remap
->do_remap
);
1158 remap
->do_remap(remap
, dst
, src
, in_n_frames
);
1161 memcpy(dst
, src
, input
->length
);
1163 pa_memblock_release(input
->memblock
);
1164 pa_memblock_release(r
->remap_buf
.memblock
);
1166 return &r
->remap_buf
;
1169 static pa_memchunk
*resample(pa_resampler
*r
, pa_memchunk
*input
) {
1170 unsigned in_n_frames
, in_n_samples
;
1171 unsigned out_n_frames
, out_n_samples
;
1176 /* Resample the data and place the result in resample_buf. */
1178 if (!r
->impl_resample
|| !input
->length
)
1181 in_n_samples
= (unsigned) (input
->length
/ r
->w_sz
);
1182 in_n_frames
= (unsigned) (in_n_samples
/ r
->o_ss
.channels
);
1184 out_n_frames
= ((in_n_frames
*r
->o_ss
.rate
)/r
->i_ss
.rate
)+EXTRA_FRAMES
;
1185 out_n_samples
= out_n_frames
* r
->o_ss
.channels
;
1187 r
->resample_buf
.index
= 0;
1188 r
->resample_buf
.length
= r
->w_sz
* out_n_samples
;
1190 if (!r
->resample_buf
.memblock
|| r
->resample_buf_samples
< out_n_samples
) {
1191 if (r
->resample_buf
.memblock
)
1192 pa_memblock_unref(r
->resample_buf
.memblock
);
1194 r
->resample_buf_samples
= out_n_samples
;
1195 r
->resample_buf
.memblock
= pa_memblock_new(r
->mempool
, r
->resample_buf
.length
);
1198 r
->impl_resample(r
, input
, in_n_frames
, &r
->resample_buf
, &out_n_frames
);
1199 r
->resample_buf
.length
= out_n_frames
* r
->w_sz
* r
->o_ss
.channels
;
1201 return &r
->resample_buf
;
1204 static pa_memchunk
*convert_from_work_format(pa_resampler
*r
, pa_memchunk
*input
) {
1205 unsigned n_samples
, n_frames
;
1211 /* Convert the data into the correct sample type and place the result in
1212 * from_work_format_buf. */
1214 if (!r
->from_work_format_func
|| !input
->length
)
1217 n_samples
= (unsigned) (input
->length
/ r
->w_sz
);
1218 n_frames
= n_samples
/ r
->o_ss
.channels
;
1220 r
->from_work_format_buf
.index
= 0;
1221 r
->from_work_format_buf
.length
= r
->o_fz
* n_frames
;
1223 if (!r
->from_work_format_buf
.memblock
|| r
->from_work_format_buf_samples
< n_samples
) {
1224 if (r
->from_work_format_buf
.memblock
)
1225 pa_memblock_unref(r
->from_work_format_buf
.memblock
);
1227 r
->from_work_format_buf_samples
= n_samples
;
1228 r
->from_work_format_buf
.memblock
= pa_memblock_new(r
->mempool
, r
->from_work_format_buf
.length
);
1231 src
= pa_memblock_acquire_chunk(input
);
1232 dst
= pa_memblock_acquire(r
->from_work_format_buf
.memblock
);
1233 r
->from_work_format_func(n_samples
, src
, dst
);
1234 pa_memblock_release(input
->memblock
);
1235 pa_memblock_release(r
->from_work_format_buf
.memblock
);
1237 return &r
->from_work_format_buf
;
1240 void pa_resampler_run(pa_resampler
*r
, const pa_memchunk
*in
, pa_memchunk
*out
) {
1246 pa_assert(in
->length
);
1247 pa_assert(in
->memblock
);
1248 pa_assert(in
->length
% r
->i_fz
== 0);
1250 buf
= (pa_memchunk
*) in
;
1251 buf
= convert_to_work_format(r
, buf
);
1252 buf
= remap_channels(r
, buf
);
1253 buf
= resample(r
, buf
);
1256 buf
= convert_from_work_format(r
, buf
);
1260 pa_memblock_ref(buf
->memblock
);
1262 pa_memchunk_reset(buf
);
1264 pa_memchunk_reset(out
);
1267 static void save_leftover(pa_resampler
*r
, void *buf
, size_t len
) {
1274 /* Store the leftover to remap_buf. */
1276 r
->remap_buf
.length
= len
;
1278 if (!r
->remap_buf
.memblock
|| r
->remap_buf_size
< r
->remap_buf
.length
) {
1279 if (r
->remap_buf
.memblock
)
1280 pa_memblock_unref(r
->remap_buf
.memblock
);
1282 r
->remap_buf_size
= r
->remap_buf
.length
;
1283 r
->remap_buf
.memblock
= pa_memblock_new(r
->mempool
, r
->remap_buf
.length
);
1286 dst
= pa_memblock_acquire(r
->remap_buf
.memblock
);
1287 memcpy(dst
, buf
, r
->remap_buf
.length
);
1288 pa_memblock_release(r
->remap_buf
.memblock
);
1290 r
->remap_buf_contains_leftover_data
= true;
1293 /*** libsamplerate based implementation ***/
1295 #ifdef HAVE_LIBSAMPLERATE
1296 static void libsamplerate_resample(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
1298 struct src_data
*libsamplerate_data
;
1303 pa_assert(out_n_frames
);
1305 libsamplerate_data
= r
->impl_data
;
1306 memset(&data
, 0, sizeof(data
));
1308 data
.data_in
= pa_memblock_acquire_chunk(input
);
1309 data
.input_frames
= (long int) in_n_frames
;
1311 data
.data_out
= pa_memblock_acquire_chunk(output
);
1312 data
.output_frames
= (long int) *out_n_frames
;
1314 data
.src_ratio
= (double) r
->o_ss
.rate
/ r
->i_ss
.rate
;
1315 data
.end_of_input
= 0;
1317 pa_assert_se(src_process(libsamplerate_data
->state
, &data
) == 0);
1319 if (data
.input_frames_used
< in_n_frames
) {
1320 void *leftover_data
= data
.data_in
+ data
.input_frames_used
* r
->o_ss
.channels
;
1321 size_t leftover_length
= (in_n_frames
- data
.input_frames_used
) * sizeof(float) * r
->o_ss
.channels
;
1323 save_leftover(r
, leftover_data
, leftover_length
);
1326 pa_memblock_release(input
->memblock
);
1327 pa_memblock_release(output
->memblock
);
1329 *out_n_frames
= (unsigned) data
.output_frames_gen
;
1332 static void libsamplerate_update_rates(pa_resampler
*r
) {
1333 struct src_data
*libsamplerate_data
;
1336 libsamplerate_data
= r
->impl_data
;
1337 pa_assert_se(src_set_ratio(libsamplerate_data
->state
, (double) r
->o_ss
.rate
/ r
->i_ss
.rate
) == 0);
1340 static void libsamplerate_reset(pa_resampler
*r
) {
1341 struct src_data
*libsamplerate_data
;
1344 libsamplerate_data
= r
->impl_data
;
1345 pa_assert_se(src_reset(libsamplerate_data
->state
) == 0);
1348 static void libsamplerate_free(pa_resampler
*r
) {
1349 struct src_data
*libsamplerate_data
;
1352 libsamplerate_data
= r
->impl_data
;
1353 if (libsamplerate_data
->state
)
1354 src_delete(libsamplerate_data
->state
);
1357 static int libsamplerate_init(pa_resampler
*r
) {
1359 struct src_data
*libsamplerate_data
;
1363 libsamplerate_data
= pa_xnew(struct src_data
, 1);
1365 if (!(libsamplerate_data
->state
= src_new(r
->method
, r
->o_ss
.channels
, &err
)))
1368 r
->impl_free
= libsamplerate_free
;
1369 r
->impl_update_rates
= libsamplerate_update_rates
;
1370 r
->impl_resample
= libsamplerate_resample
;
1371 r
->impl_reset
= libsamplerate_reset
;
1372 r
->impl_data
= libsamplerate_data
;
1379 /*** speex based implementation ***/
1381 static void speex_resample_float(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
1383 uint32_t inf
= in_n_frames
, outf
= *out_n_frames
;
1384 struct speex_data
*speex_data
;
1389 pa_assert(out_n_frames
);
1391 speex_data
= r
->impl_data
;
1393 in
= pa_memblock_acquire_chunk(input
);
1394 out
= pa_memblock_acquire_chunk(output
);
1396 pa_assert_se(speex_resampler_process_interleaved_float(speex_data
->state
, in
, &inf
, out
, &outf
) == 0);
1398 pa_memblock_release(input
->memblock
);
1399 pa_memblock_release(output
->memblock
);
1401 pa_assert(inf
== in_n_frames
);
1402 *out_n_frames
= outf
;
1405 static void speex_resample_int(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
1407 uint32_t inf
= in_n_frames
, outf
= *out_n_frames
;
1408 struct speex_data
*speex_data
;
1413 pa_assert(out_n_frames
);
1415 speex_data
= r
->impl_data
;
1417 in
= pa_memblock_acquire_chunk(input
);
1418 out
= pa_memblock_acquire_chunk(output
);
1420 pa_assert_se(speex_resampler_process_interleaved_int(speex_data
->state
, in
, &inf
, out
, &outf
) == 0);
1422 pa_memblock_release(input
->memblock
);
1423 pa_memblock_release(output
->memblock
);
1425 pa_assert(inf
== in_n_frames
);
1426 *out_n_frames
= outf
;
1429 static void speex_update_rates(pa_resampler
*r
) {
1430 struct speex_data
*speex_data
;
1433 speex_data
= r
->impl_data
;
1435 pa_assert_se(speex_resampler_set_rate(speex_data
->state
, r
->i_ss
.rate
, r
->o_ss
.rate
) == 0);
1438 static void speex_reset(pa_resampler
*r
) {
1439 struct speex_data
*speex_data
;
1442 speex_data
= r
->impl_data
;
1444 pa_assert_se(speex_resampler_reset_mem(speex_data
->state
) == 0);
1447 static void speex_free(pa_resampler
*r
) {
1448 struct speex_data
*speex_data
;
1451 speex_data
= r
->impl_data
;
1452 if (!speex_data
->state
)
1455 speex_resampler_destroy(speex_data
->state
);
1458 static int speex_init(pa_resampler
*r
) {
1460 struct speex_data
*speex_data
;
1464 speex_data
= pa_xnew(struct speex_data
, 1);
1466 r
->impl_free
= speex_free
;
1467 r
->impl_update_rates
= speex_update_rates
;
1468 r
->impl_reset
= speex_reset
;
1469 r
->impl_data
= speex_data
;
1471 if (r
->method
>= PA_RESAMPLER_SPEEX_FIXED_BASE
&& r
->method
<= PA_RESAMPLER_SPEEX_FIXED_MAX
) {
1473 q
= r
->method
- PA_RESAMPLER_SPEEX_FIXED_BASE
;
1474 r
->impl_resample
= speex_resample_int
;
1477 pa_assert(r
->method
>= PA_RESAMPLER_SPEEX_FLOAT_BASE
&& r
->method
<= PA_RESAMPLER_SPEEX_FLOAT_MAX
);
1479 q
= r
->method
- PA_RESAMPLER_SPEEX_FLOAT_BASE
;
1480 r
->impl_resample
= speex_resample_float
;
1483 pa_log_info("Choosing speex quality setting %i.", q
);
1485 if (!(speex_data
->state
= speex_resampler_init(r
->o_ss
.channels
, r
->i_ss
.rate
, r
->o_ss
.rate
, q
, &err
)))
1492 /* Trivial implementation */
1494 static void trivial_resample(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
1496 unsigned i_index
, o_index
;
1498 struct trivial_data
*trivial_data
;
1503 pa_assert(out_n_frames
);
1505 trivial_data
= r
->impl_data
;
1506 fz
= r
->w_sz
* r
->o_ss
.channels
;
1508 src
= pa_memblock_acquire_chunk(input
);
1509 dst
= pa_memblock_acquire_chunk(output
);
1511 for (o_index
= 0;; o_index
++, trivial_data
->o_counter
++) {
1512 i_index
= ((uint64_t) trivial_data
->o_counter
* r
->i_ss
.rate
) / r
->o_ss
.rate
;
1513 i_index
= i_index
> trivial_data
->i_counter
? i_index
- trivial_data
->i_counter
: 0;
1515 if (i_index
>= in_n_frames
)
1518 pa_assert_fp(o_index
* fz
< pa_memblock_get_length(output
->memblock
));
1520 memcpy((uint8_t*) dst
+ fz
* o_index
, (uint8_t*) src
+ fz
* i_index
, (int) fz
);
1523 pa_memblock_release(input
->memblock
);
1524 pa_memblock_release(output
->memblock
);
1526 *out_n_frames
= o_index
;
1528 trivial_data
->i_counter
+= in_n_frames
;
1530 /* Normalize counters */
1531 while (trivial_data
->i_counter
>= r
->i_ss
.rate
) {
1532 pa_assert(trivial_data
->o_counter
>= r
->o_ss
.rate
);
1534 trivial_data
->i_counter
-= r
->i_ss
.rate
;
1535 trivial_data
->o_counter
-= r
->o_ss
.rate
;
1539 static void trivial_update_rates_or_reset(pa_resampler
*r
) {
1540 struct trivial_data
*trivial_data
;
1543 trivial_data
= r
->impl_data
;
1545 trivial_data
->i_counter
= 0;
1546 trivial_data
->o_counter
= 0;
1549 static int trivial_init(pa_resampler
*r
) {
1550 struct trivial_data
*trivial_data
;
1553 trivial_data
= pa_xnew0(struct trivial_data
, 1);
1555 r
->impl_resample
= trivial_resample
;
1556 r
->impl_update_rates
= trivial_update_rates_or_reset
;
1557 r
->impl_reset
= trivial_update_rates_or_reset
;
1558 r
->impl_data
= trivial_data
;
1563 /* Peak finder implementation */
1565 static void peaks_resample(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
1566 unsigned c
, o_index
= 0;
1567 unsigned i
, i_end
= 0;
1569 struct peaks_data
*peaks_data
;
1574 pa_assert(out_n_frames
);
1576 peaks_data
= r
->impl_data
;
1577 src
= pa_memblock_acquire_chunk(input
);
1578 dst
= pa_memblock_acquire_chunk(output
);
1580 i
= ((uint64_t) peaks_data
->o_counter
* r
->i_ss
.rate
) / r
->o_ss
.rate
;
1581 i
= i
> peaks_data
->i_counter
? i
- peaks_data
->i_counter
: 0;
1583 while (i_end
< in_n_frames
) {
1584 i_end
= ((uint64_t) (peaks_data
->o_counter
+ 1) * r
->i_ss
.rate
) / r
->o_ss
.rate
;
1585 i_end
= i_end
> peaks_data
->i_counter
? i_end
- peaks_data
->i_counter
: 0;
1587 pa_assert_fp(o_index
* r
->w_sz
* r
->o_ss
.channels
< pa_memblock_get_length(output
->memblock
));
1589 /* 1ch float is treated separately, because that is the common case */
1590 if (r
->o_ss
.channels
== 1 && r
->work_format
== PA_SAMPLE_FLOAT32NE
) {
1591 float *s
= (float*) src
+ i
;
1592 float *d
= (float*) dst
+ o_index
;
1594 for (; i
< i_end
&& i
< in_n_frames
; i
++) {
1595 float n
= fabsf(*s
++);
1597 if (n
> peaks_data
->max_f
[0])
1598 peaks_data
->max_f
[0] = n
;
1602 *d
= peaks_data
->max_f
[0];
1603 peaks_data
->max_f
[0] = 0;
1604 o_index
++, peaks_data
->o_counter
++;
1606 } else if (r
->work_format
== PA_SAMPLE_S16NE
) {
1607 int16_t *s
= (int16_t*) src
+ r
->o_ss
.channels
* i
;
1608 int16_t *d
= (int16_t*) dst
+ r
->o_ss
.channels
* o_index
;
1610 for (; i
< i_end
&& i
< in_n_frames
; i
++)
1611 for (c
= 0; c
< r
->o_ss
.channels
; c
++) {
1612 int16_t n
= abs(*s
++);
1614 if (n
> peaks_data
->max_i
[c
])
1615 peaks_data
->max_i
[c
] = n
;
1619 for (c
= 0; c
< r
->o_ss
.channels
; c
++, d
++) {
1620 *d
= peaks_data
->max_i
[c
];
1621 peaks_data
->max_i
[c
] = 0;
1623 o_index
++, peaks_data
->o_counter
++;
1626 float *s
= (float*) src
+ r
->o_ss
.channels
* i
;
1627 float *d
= (float*) dst
+ r
->o_ss
.channels
* o_index
;
1629 for (; i
< i_end
&& i
< in_n_frames
; i
++)
1630 for (c
= 0; c
< r
->o_ss
.channels
; c
++) {
1631 float n
= fabsf(*s
++);
1633 if (n
> peaks_data
->max_f
[c
])
1634 peaks_data
->max_f
[c
] = n
;
1638 for (c
= 0; c
< r
->o_ss
.channels
; c
++, d
++) {
1639 *d
= peaks_data
->max_f
[c
];
1640 peaks_data
->max_f
[c
] = 0;
1642 o_index
++, peaks_data
->o_counter
++;
1647 pa_memblock_release(input
->memblock
);
1648 pa_memblock_release(output
->memblock
);
1650 *out_n_frames
= o_index
;
1652 peaks_data
->i_counter
+= in_n_frames
;
1654 /* Normalize counters */
1655 while (peaks_data
->i_counter
>= r
->i_ss
.rate
) {
1656 pa_assert(peaks_data
->o_counter
>= r
->o_ss
.rate
);
1658 peaks_data
->i_counter
-= r
->i_ss
.rate
;
1659 peaks_data
->o_counter
-= r
->o_ss
.rate
;
1663 static void peaks_update_rates_or_reset(pa_resampler
*r
) {
1664 struct peaks_data
*peaks_data
;
1667 peaks_data
= r
->impl_data
;
1669 peaks_data
->i_counter
= 0;
1670 peaks_data
->o_counter
= 0;
1673 static int peaks_init(pa_resampler
*r
) {
1674 struct peaks_data
*peaks_data
;
1676 pa_assert(r
->i_ss
.rate
>= r
->o_ss
.rate
);
1677 pa_assert(r
->work_format
== PA_SAMPLE_S16NE
|| r
->work_format
== PA_SAMPLE_FLOAT32NE
);
1679 peaks_data
= pa_xnew0(struct peaks_data
, 1);
1681 r
->impl_resample
= peaks_resample
;
1682 r
->impl_update_rates
= peaks_update_rates_or_reset
;
1683 r
->impl_reset
= peaks_update_rates_or_reset
;
1684 r
->impl_data
= peaks_data
;
1689 /*** ffmpeg based implementation ***/
1691 static void ffmpeg_resample(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
1692 unsigned used_frames
= 0, c
;
1693 int previous_consumed_frames
= -1;
1694 struct ffmpeg_data
*ffmpeg_data
;
1699 pa_assert(out_n_frames
);
1701 ffmpeg_data
= r
->impl_data
;
1703 for (c
= 0; c
< r
->o_ss
.channels
; c
++) {
1706 int16_t *p
, *t
, *k
, *q
, *s
;
1707 int consumed_frames
;
1709 /* Allocate a new block */
1710 b
= pa_memblock_new(r
->mempool
, ffmpeg_data
->buf
[c
].length
+ in_n_frames
* sizeof(int16_t));
1711 p
= pa_memblock_acquire(b
);
1713 /* Now copy the input data, splitting up channels */
1714 t
= (int16_t*) pa_memblock_acquire_chunk(input
) + c
;
1716 for (u
= 0; u
< in_n_frames
; u
++) {
1718 t
+= r
->o_ss
.channels
;
1721 pa_memblock_release(input
->memblock
);
1723 /* Allocate buffer for the result */
1724 w
= pa_memblock_new(r
->mempool
, *out_n_frames
* sizeof(int16_t));
1725 q
= pa_memblock_acquire(w
);
1728 used_frames
= (unsigned) av_resample(ffmpeg_data
->state
,
1731 (int) in_n_frames
, (int) *out_n_frames
,
1732 c
>= (unsigned) (r
->o_ss
.channels
-1));
1734 pa_memblock_release(b
);
1735 pa_memblock_unref(b
);
1737 pa_assert(consumed_frames
<= (int) in_n_frames
);
1738 pa_assert(previous_consumed_frames
== -1 || consumed_frames
== previous_consumed_frames
);
1739 previous_consumed_frames
= consumed_frames
;
1741 /* And place the results in the output buffer */
1742 s
= (int16_t *) pa_memblock_acquire_chunk(output
) + c
;
1743 for (u
= 0; u
< used_frames
; u
++) {
1746 s
+= r
->o_ss
.channels
;
1748 pa_memblock_release(output
->memblock
);
1749 pa_memblock_release(w
);
1750 pa_memblock_unref(w
);
1753 if (previous_consumed_frames
< (int) in_n_frames
) {
1754 void *leftover_data
= (int16_t *) pa_memblock_acquire_chunk(input
) + previous_consumed_frames
* r
->o_ss
.channels
;
1755 size_t leftover_length
= (in_n_frames
- previous_consumed_frames
) * r
->o_ss
.channels
* sizeof(int16_t);
1757 save_leftover(r
, leftover_data
, leftover_length
);
1758 pa_memblock_release(input
->memblock
);
1761 *out_n_frames
= used_frames
;
1764 static void ffmpeg_free(pa_resampler
*r
) {
1766 struct ffmpeg_data
*ffmpeg_data
;
1770 ffmpeg_data
= r
->impl_data
;
1771 if (ffmpeg_data
->state
)
1772 av_resample_close(ffmpeg_data
->state
);
1774 for (c
= 0; c
< PA_ELEMENTSOF(ffmpeg_data
->buf
); c
++)
1775 if (ffmpeg_data
->buf
[c
].memblock
)
1776 pa_memblock_unref(ffmpeg_data
->buf
[c
].memblock
);
1779 static int ffmpeg_init(pa_resampler
*r
) {
1781 struct ffmpeg_data
*ffmpeg_data
;
1785 ffmpeg_data
= pa_xnew(struct ffmpeg_data
, 1);
1787 /* We could probably implement different quality levels by
1788 * adjusting the filter parameters here. However, ffmpeg
1789 * internally only uses these hardcoded values, so let's use them
1790 * here for now as well until ffmpeg makes this configurable. */
1792 if (!(ffmpeg_data
->state
= av_resample_init((int) r
->o_ss
.rate
, (int) r
->i_ss
.rate
, 16, 10, 0, 0.8)))
1795 r
->impl_free
= ffmpeg_free
;
1796 r
->impl_resample
= ffmpeg_resample
;
1797 r
->impl_data
= (void *) ffmpeg_data
;
1799 for (c
= 0; c
< PA_ELEMENTSOF(ffmpeg_data
->buf
); c
++)
1800 pa_memchunk_reset(&ffmpeg_data
->buf
[c
]);
1805 /*** copy (noop) implementation ***/
1807 static int copy_init(pa_resampler
*r
) {
1810 pa_assert(r
->o_ss
.rate
== r
->i_ss
.rate
);