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
;
216 if (method
== PA_RESAMPLER_FFMPEG
&& (flags
& PA_RESAMPLER_VARIABLE_RATE
)) {
217 pa_log_info("Resampler 'ffmpeg' cannot do variable rate, reverting to resampler 'auto'.");
218 method
= PA_RESAMPLER_AUTO
;
221 if (method
== PA_RESAMPLER_COPY
&& ((flags
& PA_RESAMPLER_VARIABLE_RATE
) || a
->rate
!= b
->rate
)) {
222 pa_log_info("Resampler 'copy' cannot change sampling rate, reverting to resampler 'auto'.");
223 method
= PA_RESAMPLER_AUTO
;
226 if (method
== PA_RESAMPLER_AUTO
) {
228 method
= PA_RESAMPLER_SPEEX_FLOAT_BASE
+ 1;
230 if (flags
& PA_RESAMPLER_VARIABLE_RATE
)
231 method
= PA_RESAMPLER_TRIVIAL
;
233 method
= PA_RESAMPLER_FFMPEG
;
240 pa_resampler
* pa_resampler_new(
242 const pa_sample_spec
*a
,
243 const pa_channel_map
*am
,
244 const pa_sample_spec
*b
,
245 const pa_channel_map
*bm
,
246 pa_resample_method_t method
,
247 pa_resample_flags_t flags
) {
249 pa_resampler
*r
= NULL
;
254 pa_assert(pa_sample_spec_valid(a
));
255 pa_assert(pa_sample_spec_valid(b
));
256 pa_assert(method
>= 0);
257 pa_assert(method
< PA_RESAMPLER_MAX
);
259 method
= pa_resampler_fix_method(flags
, method
, a
->rate
, b
->rate
);
261 r
= pa_xnew0(pa_resampler
, 1);
266 /* Fill sample specs */
270 /* set up the remap structure */
271 r
->remap
.i_ss
= &r
->i_ss
;
272 r
->remap
.o_ss
= &r
->o_ss
;
273 r
->remap
.format
= &r
->work_format
;
277 else if (!pa_channel_map_init_auto(&r
->i_cm
, r
->i_ss
.channels
, PA_CHANNEL_MAP_DEFAULT
))
282 else if (!pa_channel_map_init_auto(&r
->o_cm
, r
->o_ss
.channels
, PA_CHANNEL_MAP_DEFAULT
))
285 r
->i_fz
= pa_frame_size(a
);
286 r
->o_fz
= pa_frame_size(b
);
290 pa_log_info("Using resampler '%s'", pa_resample_method_to_string(method
));
292 if ((method
>= PA_RESAMPLER_SPEEX_FIXED_BASE
&& method
<= PA_RESAMPLER_SPEEX_FIXED_MAX
) ||
293 (method
== PA_RESAMPLER_FFMPEG
))
294 r
->work_format
= PA_SAMPLE_S16NE
;
295 else if (method
== PA_RESAMPLER_TRIVIAL
|| method
== PA_RESAMPLER_COPY
|| method
== PA_RESAMPLER_PEAKS
) {
297 if (r
->map_required
|| a
->format
!= b
->format
|| method
== PA_RESAMPLER_PEAKS
) {
299 if (a
->format
== PA_SAMPLE_S16NE
|| b
->format
== PA_SAMPLE_S16NE
)
300 r
->work_format
= PA_SAMPLE_S16NE
;
301 else if (a
->format
== PA_SAMPLE_S32NE
|| a
->format
== PA_SAMPLE_S32RE
||
302 a
->format
== PA_SAMPLE_FLOAT32NE
|| a
->format
== PA_SAMPLE_FLOAT32RE
||
303 a
->format
== PA_SAMPLE_S24NE
|| a
->format
== PA_SAMPLE_S24RE
||
304 a
->format
== PA_SAMPLE_S24_32NE
|| a
->format
== PA_SAMPLE_S24_32RE
||
305 b
->format
== PA_SAMPLE_S32NE
|| b
->format
== PA_SAMPLE_S32RE
||
306 b
->format
== PA_SAMPLE_FLOAT32NE
|| b
->format
== PA_SAMPLE_FLOAT32RE
||
307 b
->format
== PA_SAMPLE_S24NE
|| b
->format
== PA_SAMPLE_S24RE
||
308 b
->format
== PA_SAMPLE_S24_32NE
|| b
->format
== PA_SAMPLE_S24_32RE
)
309 r
->work_format
= PA_SAMPLE_FLOAT32NE
;
311 r
->work_format
= PA_SAMPLE_S16NE
;
314 r
->work_format
= a
->format
;
317 r
->work_format
= PA_SAMPLE_FLOAT32NE
;
319 pa_log_info("Using %s as working format.", pa_sample_format_to_string(r
->work_format
));
321 r
->w_sz
= pa_sample_size_of_format(r
->work_format
);
323 if (r
->i_ss
.format
!= r
->work_format
) {
324 if (r
->work_format
== PA_SAMPLE_FLOAT32NE
) {
325 if (!(r
->to_work_format_func
= pa_get_convert_to_float32ne_function(r
->i_ss
.format
)))
328 pa_assert(r
->work_format
== PA_SAMPLE_S16NE
);
329 if (!(r
->to_work_format_func
= pa_get_convert_to_s16ne_function(r
->i_ss
.format
)))
334 if (r
->o_ss
.format
!= r
->work_format
) {
335 if (r
->work_format
== PA_SAMPLE_FLOAT32NE
) {
336 if (!(r
->from_work_format_func
= pa_get_convert_from_float32ne_function(r
->o_ss
.format
)))
339 pa_assert(r
->work_format
== PA_SAMPLE_S16NE
);
340 if (!(r
->from_work_format_func
= pa_get_convert_from_s16ne_function(r
->o_ss
.format
)))
345 /* initialize implementation */
346 if (init_table
[method
](r
) < 0)
357 void pa_resampler_free(pa_resampler
*r
) {
363 if (r
->to_work_format_buf
.memblock
)
364 pa_memblock_unref(r
->to_work_format_buf
.memblock
);
365 if (r
->remap_buf
.memblock
)
366 pa_memblock_unref(r
->remap_buf
.memblock
);
367 if (r
->resample_buf
.memblock
)
368 pa_memblock_unref(r
->resample_buf
.memblock
);
369 if (r
->from_work_format_buf
.memblock
)
370 pa_memblock_unref(r
->from_work_format_buf
.memblock
);
372 pa_xfree(r
->impl_data
);
376 void pa_resampler_set_input_rate(pa_resampler
*r
, uint32_t rate
) {
380 if (r
->i_ss
.rate
== rate
)
385 r
->impl_update_rates(r
);
388 void pa_resampler_set_output_rate(pa_resampler
*r
, uint32_t rate
) {
392 if (r
->o_ss
.rate
== rate
)
397 r
->impl_update_rates(r
);
400 size_t pa_resampler_request(pa_resampler
*r
, size_t out_length
) {
403 /* Let's round up here to make it more likely that the caller will get at
404 * least out_length amount of data from pa_resampler_run().
406 * We don't take the leftover into account here. If we did, then it might
407 * be in theory possible that this function would return 0 and
408 * pa_resampler_run() would also return 0. That could lead to infinite
409 * loops. When the leftover is ignored here, such loops would eventually
410 * terminate, because the leftover would grow each round, finally
411 * surpassing the minimum input threshold of the resampler. */
412 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
;
415 size_t pa_resampler_result(pa_resampler
*r
, size_t in_length
) {
420 /* Let's round up here to ensure that the caller will always allocate big
421 * enough output buffer. */
423 frames
= (in_length
+ r
->i_fz
- 1) / r
->i_fz
;
425 if (r
->remap_buf_contains_leftover_data
)
426 frames
+= r
->remap_buf
.length
/ (r
->w_sz
* r
->o_ss
.channels
);
428 return (((uint64_t) frames
* r
->o_ss
.rate
+ r
->i_ss
.rate
- 1) / r
->i_ss
.rate
) * r
->o_fz
;
431 size_t pa_resampler_max_block_size(pa_resampler
*r
) {
432 size_t block_size_max
;
433 pa_sample_spec max_ss
;
439 block_size_max
= pa_mempool_block_size_max(r
->mempool
);
441 /* We deduce the "largest" sample spec we're using during the
443 max_ss
.channels
= (uint8_t) (PA_MAX(r
->i_ss
.channels
, r
->o_ss
.channels
));
445 /* We silently assume that the format enum is ordered by size */
446 max_ss
.format
= PA_MAX(r
->i_ss
.format
, r
->o_ss
.format
);
447 max_ss
.format
= PA_MAX(max_ss
.format
, r
->work_format
);
449 max_ss
.rate
= PA_MAX(r
->i_ss
.rate
, r
->o_ss
.rate
);
451 max_fs
= pa_frame_size(&max_ss
);
452 frames
= block_size_max
/ max_fs
- EXTRA_FRAMES
;
454 if (r
->remap_buf_contains_leftover_data
)
455 frames
-= r
->remap_buf
.length
/ (r
->w_sz
* r
->o_ss
.channels
);
457 return ((uint64_t) frames
* r
->i_ss
.rate
/ max_ss
.rate
) * r
->i_fz
;
460 void pa_resampler_reset(pa_resampler
*r
) {
466 r
->remap_buf_contains_leftover_data
= false;
469 pa_resample_method_t
pa_resampler_get_method(pa_resampler
*r
) {
475 const pa_channel_map
* pa_resampler_input_channel_map(pa_resampler
*r
) {
481 const pa_sample_spec
* pa_resampler_input_sample_spec(pa_resampler
*r
) {
487 const pa_channel_map
* pa_resampler_output_channel_map(pa_resampler
*r
) {
493 const pa_sample_spec
* pa_resampler_output_sample_spec(pa_resampler
*r
) {
499 static const char * const resample_methods
[] = {
500 "src-sinc-best-quality",
501 "src-sinc-medium-quality",
503 "src-zero-order-hold",
534 const char *pa_resample_method_to_string(pa_resample_method_t m
) {
536 if (m
< 0 || m
>= PA_RESAMPLER_MAX
)
539 return resample_methods
[m
];
542 int pa_resample_method_supported(pa_resample_method_t m
) {
544 if (m
< 0 || m
>= PA_RESAMPLER_MAX
)
547 #ifndef HAVE_LIBSAMPLERATE
548 if (m
<= PA_RESAMPLER_SRC_LINEAR
)
553 if (m
>= PA_RESAMPLER_SPEEX_FLOAT_BASE
&& m
<= PA_RESAMPLER_SPEEX_FLOAT_MAX
)
555 if (m
>= PA_RESAMPLER_SPEEX_FIXED_BASE
&& m
<= PA_RESAMPLER_SPEEX_FIXED_MAX
)
562 pa_resample_method_t
pa_parse_resample_method(const char *string
) {
563 pa_resample_method_t m
;
567 for (m
= 0; m
< PA_RESAMPLER_MAX
; m
++)
568 if (pa_streq(string
, resample_methods
[m
]))
571 if (pa_streq(string
, "speex-fixed"))
572 return PA_RESAMPLER_SPEEX_FIXED_BASE
+ 1;
574 if (pa_streq(string
, "speex-float"))
575 return PA_RESAMPLER_SPEEX_FLOAT_BASE
+ 1;
577 return PA_RESAMPLER_INVALID
;
580 static bool on_left(pa_channel_position_t p
) {
583 p
== PA_CHANNEL_POSITION_FRONT_LEFT
||
584 p
== PA_CHANNEL_POSITION_REAR_LEFT
||
585 p
== PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER
||
586 p
== PA_CHANNEL_POSITION_SIDE_LEFT
||
587 p
== PA_CHANNEL_POSITION_TOP_FRONT_LEFT
||
588 p
== PA_CHANNEL_POSITION_TOP_REAR_LEFT
;
591 static bool on_right(pa_channel_position_t p
) {
594 p
== PA_CHANNEL_POSITION_FRONT_RIGHT
||
595 p
== PA_CHANNEL_POSITION_REAR_RIGHT
||
596 p
== PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER
||
597 p
== PA_CHANNEL_POSITION_SIDE_RIGHT
||
598 p
== PA_CHANNEL_POSITION_TOP_FRONT_RIGHT
||
599 p
== PA_CHANNEL_POSITION_TOP_REAR_RIGHT
;
602 static bool on_center(pa_channel_position_t p
) {
605 p
== PA_CHANNEL_POSITION_FRONT_CENTER
||
606 p
== PA_CHANNEL_POSITION_REAR_CENTER
||
607 p
== PA_CHANNEL_POSITION_TOP_CENTER
||
608 p
== PA_CHANNEL_POSITION_TOP_FRONT_CENTER
||
609 p
== PA_CHANNEL_POSITION_TOP_REAR_CENTER
;
612 static bool on_lfe(pa_channel_position_t p
) {
614 p
== PA_CHANNEL_POSITION_LFE
;
617 static bool on_front(pa_channel_position_t p
) {
619 p
== PA_CHANNEL_POSITION_FRONT_LEFT
||
620 p
== PA_CHANNEL_POSITION_FRONT_RIGHT
||
621 p
== PA_CHANNEL_POSITION_FRONT_CENTER
||
622 p
== PA_CHANNEL_POSITION_TOP_FRONT_LEFT
||
623 p
== PA_CHANNEL_POSITION_TOP_FRONT_RIGHT
||
624 p
== PA_CHANNEL_POSITION_TOP_FRONT_CENTER
||
625 p
== PA_CHANNEL_POSITION_FRONT_LEFT_OF_CENTER
||
626 p
== PA_CHANNEL_POSITION_FRONT_RIGHT_OF_CENTER
;
629 static bool on_rear(pa_channel_position_t p
) {
631 p
== PA_CHANNEL_POSITION_REAR_LEFT
||
632 p
== PA_CHANNEL_POSITION_REAR_RIGHT
||
633 p
== PA_CHANNEL_POSITION_REAR_CENTER
||
634 p
== PA_CHANNEL_POSITION_TOP_REAR_LEFT
||
635 p
== PA_CHANNEL_POSITION_TOP_REAR_RIGHT
||
636 p
== PA_CHANNEL_POSITION_TOP_REAR_CENTER
;
639 static bool on_side(pa_channel_position_t p
) {
641 p
== PA_CHANNEL_POSITION_SIDE_LEFT
||
642 p
== PA_CHANNEL_POSITION_SIDE_RIGHT
||
643 p
== PA_CHANNEL_POSITION_TOP_CENTER
;
653 static int front_rear_side(pa_channel_position_t p
) {
663 static void calc_map_table(pa_resampler
*r
) {
666 bool ic_connected
[PA_CHANNELS_MAX
];
674 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
)))))
679 n_oc
= r
->o_ss
.channels
;
680 n_ic
= r
->i_ss
.channels
;
682 memset(m
->map_table_f
, 0, sizeof(m
->map_table_f
));
683 memset(m
->map_table_i
, 0, sizeof(m
->map_table_i
));
685 memset(ic_connected
, 0, sizeof(ic_connected
));
686 remix
= (r
->flags
& (PA_RESAMPLER_NO_REMAP
| PA_RESAMPLER_NO_REMIX
)) == 0;
688 if (r
->flags
& PA_RESAMPLER_NO_REMAP
) {
691 for (oc
= 0; oc
< PA_MIN(n_ic
, n_oc
); oc
++)
692 m
->map_table_f
[oc
][oc
] = 1.0f
;
694 } else if (r
->flags
& PA_RESAMPLER_NO_REMIX
) {
696 for (oc
= 0; oc
< n_oc
; oc
++) {
697 pa_channel_position_t b
= r
->o_cm
.map
[oc
];
699 for (ic
= 0; ic
< n_ic
; ic
++) {
700 pa_channel_position_t a
= r
->i_cm
.map
[ic
];
702 /* We shall not do any remixing. Hence, just check by name */
704 m
->map_table_f
[oc
][ic
] = 1.0f
;
709 /* OK, we shall do the full monty: upmixing and downmixing. Our
710 * algorithm is relatively simple, does not do spacialization, delay
711 * elements or apply lowpass filters for LFE. Patches are always
712 * welcome, though. Oh, and it doesn't do any matrix decoding. (Which
713 * probably wouldn't make any sense anyway.)
715 * This code is not idempotent: downmixing an upmixed stereo stream is
716 * not identical to the original. The volume will not match, and the
717 * two channels will be a linear combination of both.
719 * This is loosely based on random suggestions found on the Internet,
721 * http://www.halfgaar.net/surround-sound-in-linux and the alsa upmix
724 * The algorithm works basically like this:
726 * 1) Connect all channels with matching names.
729 * S:Mono: Copy into all D:channels
730 * D:Mono: Avg all S:channels
732 * 3) Mix D:Left, D:Right:
733 * D:Left: If not connected, avg all S:Left
734 * D:Right: If not connected, avg all S:Right
737 * If not connected, avg all S:Center
738 * If still not connected, avg all S:Left, S:Right
741 * If not connected, avg all S:*
743 * 6) Make sure S:Left/S:Right is used: S:Left/S:Right: If not
744 * connected, mix into all D:left and all D:right channels. Gain is
747 * 7) Make sure S:Center, S:LFE is used:
749 * S:Center, S:LFE: If not connected, mix into all D:left, all
750 * D:right, all D:center channels. Gain is 0.5 for center and 0.375
751 * for LFE. C-front is only mixed into L-front/R-front if available,
752 * otherwise into all L/R channels. Similarly for C-rear.
754 * 8) Normalize each row in the matrix such that the sum for each row is
755 * not larger than 1.0 in order to avoid clipping.
757 * S: and D: shall relate to the source resp. destination channels.
759 * Rationale: 1, 2 are probably obvious. For 3: this copies front to
760 * rear if needed. For 4: we try to find some suitable C source for C,
761 * if we don't find any, we avg L and R. For 5: LFE is mixed from all
762 * channels. For 6: the rear channels should not be dropped entirely,
763 * however have only minimal impact. For 7: movies usually encode
764 * speech on the center channel. Thus we have to make sure this channel
765 * is distributed to L and R if not available in the output. Also, LFE
766 * is used to achieve a greater dynamic range, and thus we should try
767 * to do our best to pass it to L+R.
774 ic_unconnected_left
= 0,
775 ic_unconnected_right
= 0,
776 ic_unconnected_center
= 0,
777 ic_unconnected_lfe
= 0;
778 bool ic_unconnected_center_mixed_in
= 0;
782 for (ic
= 0; ic
< n_ic
; ic
++) {
783 if (on_left(r
->i_cm
.map
[ic
]))
785 if (on_right(r
->i_cm
.map
[ic
]))
787 if (on_center(r
->i_cm
.map
[ic
]))
791 for (oc
= 0; oc
< n_oc
; oc
++) {
792 bool oc_connected
= false;
793 pa_channel_position_t b
= r
->o_cm
.map
[oc
];
795 for (ic
= 0; ic
< n_ic
; ic
++) {
796 pa_channel_position_t a
= r
->i_cm
.map
[ic
];
798 if (a
== b
|| a
== PA_CHANNEL_POSITION_MONO
) {
799 m
->map_table_f
[oc
][ic
] = 1.0f
;
802 ic_connected
[ic
] = true;
804 else if (b
== PA_CHANNEL_POSITION_MONO
) {
805 m
->map_table_f
[oc
][ic
] = 1.0f
/ (float) n_ic
;
808 ic_connected
[ic
] = true;
813 /* Try to find matching input ports for this output port */
817 /* We are not connected and on the left side, let's
818 * average all left side input channels. */
821 for (ic
= 0; ic
< n_ic
; ic
++)
822 if (on_left(r
->i_cm
.map
[ic
])) {
823 m
->map_table_f
[oc
][ic
] = 1.0f
/ (float) ic_left
;
824 ic_connected
[ic
] = true;
827 /* We ignore the case where there is no left input channel.
828 * Something is really wrong in this case anyway. */
830 } else if (on_right(b
)) {
832 /* We are not connected and on the right side, let's
833 * average all right side input channels. */
836 for (ic
= 0; ic
< n_ic
; ic
++)
837 if (on_right(r
->i_cm
.map
[ic
])) {
838 m
->map_table_f
[oc
][ic
] = 1.0f
/ (float) ic_right
;
839 ic_connected
[ic
] = true;
842 /* We ignore the case where there is no right input
843 * channel. Something is really wrong in this case anyway.
846 } else if (on_center(b
)) {
850 /* We are not connected and at the center. Let's average
851 * all center input channels. */
853 for (ic
= 0; ic
< n_ic
; ic
++)
854 if (on_center(r
->i_cm
.map
[ic
])) {
855 m
->map_table_f
[oc
][ic
] = 1.0f
/ (float) ic_center
;
856 ic_connected
[ic
] = true;
859 } else if (ic_left
+ ic_right
> 0) {
861 /* Hmm, no center channel around, let's synthesize it
862 * by mixing L and R.*/
864 for (ic
= 0; ic
< n_ic
; ic
++)
865 if (on_left(r
->i_cm
.map
[ic
]) || on_right(r
->i_cm
.map
[ic
])) {
866 m
->map_table_f
[oc
][ic
] = 1.0f
/ (float) (ic_left
+ ic_right
);
867 ic_connected
[ic
] = true;
871 /* We ignore the case where there is not even a left or
872 * right input channel. Something is really wrong in this
875 } else if (on_lfe(b
) && !(r
->flags
& PA_RESAMPLER_NO_LFE
)) {
877 /* We are not connected and an LFE. Let's average all
878 * channels for LFE. */
880 for (ic
= 0; ic
< n_ic
; ic
++)
881 m
->map_table_f
[oc
][ic
] = 1.0f
/ (float) n_ic
;
883 /* Please note that a channel connected to LFE doesn't
884 * really count as connected. */
889 for (ic
= 0; ic
< n_ic
; ic
++) {
890 pa_channel_position_t a
= r
->i_cm
.map
[ic
];
892 if (ic_connected
[ic
])
896 ic_unconnected_left
++;
897 else if (on_right(a
))
898 ic_unconnected_right
++;
899 else if (on_center(a
))
900 ic_unconnected_center
++;
902 ic_unconnected_lfe
++;
905 for (ic
= 0; ic
< n_ic
; ic
++) {
906 pa_channel_position_t a
= r
->i_cm
.map
[ic
];
908 if (ic_connected
[ic
])
911 for (oc
= 0; oc
< n_oc
; oc
++) {
912 pa_channel_position_t b
= r
->o_cm
.map
[oc
];
914 if (on_left(a
) && on_left(b
))
915 m
->map_table_f
[oc
][ic
] = (1.f
/9.f
) / (float) ic_unconnected_left
;
917 else if (on_right(a
) && on_right(b
))
918 m
->map_table_f
[oc
][ic
] = (1.f
/9.f
) / (float) ic_unconnected_right
;
920 else if (on_center(a
) && on_center(b
)) {
921 m
->map_table_f
[oc
][ic
] = (1.f
/9.f
) / (float) ic_unconnected_center
;
922 ic_unconnected_center_mixed_in
= true;
924 } else if (on_lfe(a
) && !(r
->flags
& PA_RESAMPLER_NO_LFE
))
925 m
->map_table_f
[oc
][ic
] = .375f
/ (float) ic_unconnected_lfe
;
929 if (ic_unconnected_center
> 0 && !ic_unconnected_center_mixed_in
) {
930 unsigned ncenter
[PA_CHANNELS_MAX
];
931 bool found_frs
[PA_CHANNELS_MAX
];
933 memset(ncenter
, 0, sizeof(ncenter
));
934 memset(found_frs
, 0, sizeof(found_frs
));
936 /* Hmm, as it appears there was no center channel we
937 could mix our center channel in. In this case, mix it into
938 left and right. Using .5 as the factor. */
940 for (ic
= 0; ic
< n_ic
; ic
++) {
942 if (ic_connected
[ic
])
945 if (!on_center(r
->i_cm
.map
[ic
]))
948 for (oc
= 0; oc
< n_oc
; oc
++) {
950 if (!on_left(r
->o_cm
.map
[oc
]) && !on_right(r
->o_cm
.map
[oc
]))
953 if (front_rear_side(r
->i_cm
.map
[ic
]) == front_rear_side(r
->o_cm
.map
[oc
])) {
954 found_frs
[ic
] = true;
959 for (oc
= 0; oc
< n_oc
; oc
++) {
961 if (!on_left(r
->o_cm
.map
[oc
]) && !on_right(r
->o_cm
.map
[oc
]))
964 if (!found_frs
[ic
] || front_rear_side(r
->i_cm
.map
[ic
]) == front_rear_side(r
->o_cm
.map
[oc
]))
969 for (oc
= 0; oc
< n_oc
; oc
++) {
971 if (!on_left(r
->o_cm
.map
[oc
]) && !on_right(r
->o_cm
.map
[oc
]))
974 if (ncenter
[oc
] <= 0)
977 for (ic
= 0; ic
< n_ic
; ic
++) {
979 if (!on_center(r
->i_cm
.map
[ic
]))
982 if (!found_frs
[ic
] || front_rear_side(r
->i_cm
.map
[ic
]) == front_rear_side(r
->o_cm
.map
[oc
]))
983 m
->map_table_f
[oc
][ic
] = .5f
/ (float) ncenter
[oc
];
989 for (oc
= 0; oc
< n_oc
; oc
++) {
991 for (ic
= 0; ic
< n_ic
; ic
++)
992 sum
+= m
->map_table_f
[oc
][ic
];
995 for (ic
= 0; ic
< n_ic
; ic
++)
996 m
->map_table_f
[oc
][ic
] /= sum
;
999 /* make an 16:16 int version of the matrix */
1000 for (oc
= 0; oc
< n_oc
; oc
++)
1001 for (ic
= 0; ic
< n_ic
; ic
++)
1002 m
->map_table_i
[oc
][ic
] = (int32_t) (m
->map_table_f
[oc
][ic
] * 0x10000);
1004 s
= pa_strbuf_new();
1006 pa_strbuf_printf(s
, " ");
1007 for (ic
= 0; ic
< n_ic
; ic
++)
1008 pa_strbuf_printf(s
, " I%02u ", ic
);
1009 pa_strbuf_puts(s
, "\n +");
1011 for (ic
= 0; ic
< n_ic
; ic
++)
1012 pa_strbuf_printf(s
, "------");
1013 pa_strbuf_puts(s
, "\n");
1015 for (oc
= 0; oc
< n_oc
; oc
++) {
1016 pa_strbuf_printf(s
, "O%02u |", oc
);
1018 for (ic
= 0; ic
< n_ic
; ic
++)
1019 pa_strbuf_printf(s
, " %1.3f", m
->map_table_f
[oc
][ic
]);
1021 pa_strbuf_puts(s
, "\n");
1024 pa_log_debug("Channel matrix:\n%s", t
= pa_strbuf_tostring_free(s
));
1027 /* initialize the remapping function */
1031 static pa_memchunk
* convert_to_work_format(pa_resampler
*r
, pa_memchunk
*input
) {
1037 pa_assert(input
->memblock
);
1039 /* Convert the incoming sample into the work sample format and place them
1040 * in to_work_format_buf. */
1042 if (!r
->to_work_format_func
|| !input
->length
)
1045 n_samples
= (unsigned) ((input
->length
/ r
->i_fz
) * r
->i_ss
.channels
);
1047 r
->to_work_format_buf
.index
= 0;
1048 r
->to_work_format_buf
.length
= r
->w_sz
* n_samples
;
1050 if (!r
->to_work_format_buf
.memblock
|| r
->to_work_format_buf_samples
< n_samples
) {
1051 if (r
->to_work_format_buf
.memblock
)
1052 pa_memblock_unref(r
->to_work_format_buf
.memblock
);
1054 r
->to_work_format_buf_samples
= n_samples
;
1055 r
->to_work_format_buf
.memblock
= pa_memblock_new(r
->mempool
, r
->to_work_format_buf
.length
);
1058 src
= pa_memblock_acquire_chunk(input
);
1059 dst
= pa_memblock_acquire(r
->to_work_format_buf
.memblock
);
1061 r
->to_work_format_func(n_samples
, src
, dst
);
1063 pa_memblock_release(input
->memblock
);
1064 pa_memblock_release(r
->to_work_format_buf
.memblock
);
1066 return &r
->to_work_format_buf
;
1069 static pa_memchunk
*remap_channels(pa_resampler
*r
, pa_memchunk
*input
) {
1070 unsigned in_n_samples
, out_n_samples
, in_n_frames
, out_n_frames
;
1072 size_t leftover_length
= 0;
1077 pa_assert(input
->memblock
);
1079 /* Remap channels and place the result in remap_buf. There may be leftover
1080 * data in the beginning of remap_buf. The leftover data is already
1081 * remapped, so it's not part of the input, it's part of the output. */
1083 have_leftover
= r
->remap_buf_contains_leftover_data
;
1084 r
->remap_buf_contains_leftover_data
= false;
1086 if (!have_leftover
&& (!r
->map_required
|| input
->length
<= 0))
1088 else if (input
->length
<= 0)
1089 return &r
->remap_buf
;
1091 in_n_samples
= (unsigned) (input
->length
/ r
->w_sz
);
1092 in_n_frames
= out_n_frames
= in_n_samples
/ r
->i_ss
.channels
;
1094 if (have_leftover
) {
1095 leftover_length
= r
->remap_buf
.length
;
1096 out_n_frames
+= leftover_length
/ (r
->w_sz
* r
->o_ss
.channels
);
1099 out_n_samples
= out_n_frames
* r
->o_ss
.channels
;
1100 r
->remap_buf
.length
= out_n_samples
* r
->w_sz
;
1102 if (have_leftover
) {
1103 if (r
->remap_buf_size
< r
->remap_buf
.length
) {
1104 pa_memblock
*new_block
= pa_memblock_new(r
->mempool
, r
->remap_buf
.length
);
1106 src
= pa_memblock_acquire(r
->remap_buf
.memblock
);
1107 dst
= pa_memblock_acquire(new_block
);
1108 memcpy(dst
, src
, leftover_length
);
1109 pa_memblock_release(r
->remap_buf
.memblock
);
1110 pa_memblock_release(new_block
);
1112 pa_memblock_unref(r
->remap_buf
.memblock
);
1113 r
->remap_buf
.memblock
= new_block
;
1114 r
->remap_buf_size
= r
->remap_buf
.length
;
1118 if (!r
->remap_buf
.memblock
|| r
->remap_buf_size
< r
->remap_buf
.length
) {
1119 if (r
->remap_buf
.memblock
)
1120 pa_memblock_unref(r
->remap_buf
.memblock
);
1122 r
->remap_buf_size
= r
->remap_buf
.length
;
1123 r
->remap_buf
.memblock
= pa_memblock_new(r
->mempool
, r
->remap_buf
.length
);
1127 src
= pa_memblock_acquire_chunk(input
);
1128 dst
= (uint8_t *) pa_memblock_acquire(r
->remap_buf
.memblock
) + leftover_length
;
1130 if (r
->map_required
) {
1131 pa_remap_t
*remap
= &r
->remap
;
1133 pa_assert(remap
->do_remap
);
1134 remap
->do_remap(remap
, dst
, src
, in_n_frames
);
1137 memcpy(dst
, src
, input
->length
);
1139 pa_memblock_release(input
->memblock
);
1140 pa_memblock_release(r
->remap_buf
.memblock
);
1142 return &r
->remap_buf
;
1145 static pa_memchunk
*resample(pa_resampler
*r
, pa_memchunk
*input
) {
1146 unsigned in_n_frames
, in_n_samples
;
1147 unsigned out_n_frames
, out_n_samples
;
1152 /* Resample the data and place the result in resample_buf. */
1154 if (!r
->impl_resample
|| !input
->length
)
1157 in_n_samples
= (unsigned) (input
->length
/ r
->w_sz
);
1158 in_n_frames
= (unsigned) (in_n_samples
/ r
->o_ss
.channels
);
1160 out_n_frames
= ((in_n_frames
*r
->o_ss
.rate
)/r
->i_ss
.rate
)+EXTRA_FRAMES
;
1161 out_n_samples
= out_n_frames
* r
->o_ss
.channels
;
1163 r
->resample_buf
.index
= 0;
1164 r
->resample_buf
.length
= r
->w_sz
* out_n_samples
;
1166 if (!r
->resample_buf
.memblock
|| r
->resample_buf_samples
< out_n_samples
) {
1167 if (r
->resample_buf
.memblock
)
1168 pa_memblock_unref(r
->resample_buf
.memblock
);
1170 r
->resample_buf_samples
= out_n_samples
;
1171 r
->resample_buf
.memblock
= pa_memblock_new(r
->mempool
, r
->resample_buf
.length
);
1174 r
->impl_resample(r
, input
, in_n_frames
, &r
->resample_buf
, &out_n_frames
);
1175 r
->resample_buf
.length
= out_n_frames
* r
->w_sz
* r
->o_ss
.channels
;
1177 return &r
->resample_buf
;
1180 static pa_memchunk
*convert_from_work_format(pa_resampler
*r
, pa_memchunk
*input
) {
1181 unsigned n_samples
, n_frames
;
1187 /* Convert the data into the correct sample type and place the result in
1188 * from_work_format_buf. */
1190 if (!r
->from_work_format_func
|| !input
->length
)
1193 n_samples
= (unsigned) (input
->length
/ r
->w_sz
);
1194 n_frames
= n_samples
/ r
->o_ss
.channels
;
1196 r
->from_work_format_buf
.index
= 0;
1197 r
->from_work_format_buf
.length
= r
->o_fz
* n_frames
;
1199 if (!r
->from_work_format_buf
.memblock
|| r
->from_work_format_buf_samples
< n_samples
) {
1200 if (r
->from_work_format_buf
.memblock
)
1201 pa_memblock_unref(r
->from_work_format_buf
.memblock
);
1203 r
->from_work_format_buf_samples
= n_samples
;
1204 r
->from_work_format_buf
.memblock
= pa_memblock_new(r
->mempool
, r
->from_work_format_buf
.length
);
1207 src
= pa_memblock_acquire_chunk(input
);
1208 dst
= pa_memblock_acquire(r
->from_work_format_buf
.memblock
);
1209 r
->from_work_format_func(n_samples
, src
, dst
);
1210 pa_memblock_release(input
->memblock
);
1211 pa_memblock_release(r
->from_work_format_buf
.memblock
);
1213 return &r
->from_work_format_buf
;
1216 void pa_resampler_run(pa_resampler
*r
, const pa_memchunk
*in
, pa_memchunk
*out
) {
1222 pa_assert(in
->length
);
1223 pa_assert(in
->memblock
);
1224 pa_assert(in
->length
% r
->i_fz
== 0);
1226 buf
= (pa_memchunk
*) in
;
1227 buf
= convert_to_work_format(r
, buf
);
1228 buf
= remap_channels(r
, buf
);
1229 buf
= resample(r
, buf
);
1232 buf
= convert_from_work_format(r
, buf
);
1236 pa_memblock_ref(buf
->memblock
);
1238 pa_memchunk_reset(buf
);
1240 pa_memchunk_reset(out
);
1243 static void save_leftover(pa_resampler
*r
, void *buf
, size_t len
) {
1250 /* Store the leftover to remap_buf. */
1252 r
->remap_buf
.length
= len
;
1254 if (!r
->remap_buf
.memblock
|| r
->remap_buf_size
< r
->remap_buf
.length
) {
1255 if (r
->remap_buf
.memblock
)
1256 pa_memblock_unref(r
->remap_buf
.memblock
);
1258 r
->remap_buf_size
= r
->remap_buf
.length
;
1259 r
->remap_buf
.memblock
= pa_memblock_new(r
->mempool
, r
->remap_buf
.length
);
1262 dst
= pa_memblock_acquire(r
->remap_buf
.memblock
);
1263 memcpy(dst
, buf
, r
->remap_buf
.length
);
1264 pa_memblock_release(r
->remap_buf
.memblock
);
1266 r
->remap_buf_contains_leftover_data
= true;
1269 /*** libsamplerate based implementation ***/
1271 #ifdef HAVE_LIBSAMPLERATE
1272 static void libsamplerate_resample(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
1274 struct src_data
*libsamplerate_data
;
1279 pa_assert(out_n_frames
);
1281 libsamplerate_data
= r
->impl_data
;
1282 memset(&data
, 0, sizeof(data
));
1284 data
.data_in
= pa_memblock_acquire_chunk(input
);
1285 data
.input_frames
= (long int) in_n_frames
;
1287 data
.data_out
= pa_memblock_acquire_chunk(output
);
1288 data
.output_frames
= (long int) *out_n_frames
;
1290 data
.src_ratio
= (double) r
->o_ss
.rate
/ r
->i_ss
.rate
;
1291 data
.end_of_input
= 0;
1293 pa_assert_se(src_process(libsamplerate_data
->state
, &data
) == 0);
1295 if (data
.input_frames_used
< in_n_frames
) {
1296 void *leftover_data
= data
.data_in
+ data
.input_frames_used
* r
->o_ss
.channels
;
1297 size_t leftover_length
= (in_n_frames
- data
.input_frames_used
) * sizeof(float) * r
->o_ss
.channels
;
1299 save_leftover(r
, leftover_data
, leftover_length
);
1302 pa_memblock_release(input
->memblock
);
1303 pa_memblock_release(output
->memblock
);
1305 *out_n_frames
= (unsigned) data
.output_frames_gen
;
1308 static void libsamplerate_update_rates(pa_resampler
*r
) {
1309 struct src_data
*libsamplerate_data
;
1312 libsamplerate_data
= r
->impl_data
;
1313 pa_assert_se(src_set_ratio(libsamplerate_data
->state
, (double) r
->o_ss
.rate
/ r
->i_ss
.rate
) == 0);
1316 static void libsamplerate_reset(pa_resampler
*r
) {
1317 struct src_data
*libsamplerate_data
;
1320 libsamplerate_data
= r
->impl_data
;
1321 pa_assert_se(src_reset(libsamplerate_data
->state
) == 0);
1324 static void libsamplerate_free(pa_resampler
*r
) {
1325 struct src_data
*libsamplerate_data
;
1328 libsamplerate_data
= r
->impl_data
;
1329 if (libsamplerate_data
->state
)
1330 src_delete(libsamplerate_data
->state
);
1333 static int libsamplerate_init(pa_resampler
*r
) {
1335 struct src_data
*libsamplerate_data
;
1339 libsamplerate_data
= pa_xnew(struct src_data
, 1);
1341 if (!(libsamplerate_data
->state
= src_new(r
->method
, r
->o_ss
.channels
, &err
)))
1344 r
->impl_free
= libsamplerate_free
;
1345 r
->impl_update_rates
= libsamplerate_update_rates
;
1346 r
->impl_resample
= libsamplerate_resample
;
1347 r
->impl_reset
= libsamplerate_reset
;
1348 r
->impl_data
= libsamplerate_data
;
1355 /*** speex based implementation ***/
1357 static void speex_resample_float(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
1359 uint32_t inf
= in_n_frames
, outf
= *out_n_frames
;
1360 struct speex_data
*speex_data
;
1365 pa_assert(out_n_frames
);
1367 speex_data
= r
->impl_data
;
1369 in
= pa_memblock_acquire_chunk(input
);
1370 out
= pa_memblock_acquire_chunk(output
);
1372 pa_assert_se(speex_resampler_process_interleaved_float(speex_data
->state
, in
, &inf
, out
, &outf
) == 0);
1374 pa_memblock_release(input
->memblock
);
1375 pa_memblock_release(output
->memblock
);
1377 pa_assert(inf
== in_n_frames
);
1378 *out_n_frames
= outf
;
1381 static void speex_resample_int(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_int(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_update_rates(pa_resampler
*r
) {
1406 struct speex_data
*speex_data
;
1409 speex_data
= r
->impl_data
;
1411 pa_assert_se(speex_resampler_set_rate(speex_data
->state
, r
->i_ss
.rate
, r
->o_ss
.rate
) == 0);
1414 static void speex_reset(pa_resampler
*r
) {
1415 struct speex_data
*speex_data
;
1418 speex_data
= r
->impl_data
;
1420 pa_assert_se(speex_resampler_reset_mem(speex_data
->state
) == 0);
1423 static void speex_free(pa_resampler
*r
) {
1424 struct speex_data
*speex_data
;
1427 speex_data
= r
->impl_data
;
1428 if (!speex_data
->state
)
1431 speex_resampler_destroy(speex_data
->state
);
1434 static int speex_init(pa_resampler
*r
) {
1436 struct speex_data
*speex_data
;
1440 speex_data
= pa_xnew(struct speex_data
, 1);
1442 r
->impl_free
= speex_free
;
1443 r
->impl_update_rates
= speex_update_rates
;
1444 r
->impl_reset
= speex_reset
;
1445 r
->impl_data
= speex_data
;
1447 if (r
->method
>= PA_RESAMPLER_SPEEX_FIXED_BASE
&& r
->method
<= PA_RESAMPLER_SPEEX_FIXED_MAX
) {
1449 q
= r
->method
- PA_RESAMPLER_SPEEX_FIXED_BASE
;
1450 r
->impl_resample
= speex_resample_int
;
1453 pa_assert(r
->method
>= PA_RESAMPLER_SPEEX_FLOAT_BASE
&& r
->method
<= PA_RESAMPLER_SPEEX_FLOAT_MAX
);
1455 q
= r
->method
- PA_RESAMPLER_SPEEX_FLOAT_BASE
;
1456 r
->impl_resample
= speex_resample_float
;
1459 pa_log_info("Choosing speex quality setting %i.", q
);
1461 if (!(speex_data
->state
= speex_resampler_init(r
->o_ss
.channels
, r
->i_ss
.rate
, r
->o_ss
.rate
, q
, &err
)))
1468 /* Trivial implementation */
1470 static void trivial_resample(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
1472 unsigned i_index
, o_index
;
1474 struct trivial_data
*trivial_data
;
1479 pa_assert(out_n_frames
);
1481 trivial_data
= r
->impl_data
;
1482 fz
= r
->w_sz
* r
->o_ss
.channels
;
1484 src
= pa_memblock_acquire_chunk(input
);
1485 dst
= pa_memblock_acquire_chunk(output
);
1487 for (o_index
= 0;; o_index
++, trivial_data
->o_counter
++) {
1488 i_index
= ((uint64_t) trivial_data
->o_counter
* r
->i_ss
.rate
) / r
->o_ss
.rate
;
1489 i_index
= i_index
> trivial_data
->i_counter
? i_index
- trivial_data
->i_counter
: 0;
1491 if (i_index
>= in_n_frames
)
1494 pa_assert_fp(o_index
* fz
< pa_memblock_get_length(output
->memblock
));
1496 memcpy((uint8_t*) dst
+ fz
* o_index
, (uint8_t*) src
+ fz
* i_index
, (int) fz
);
1499 pa_memblock_release(input
->memblock
);
1500 pa_memblock_release(output
->memblock
);
1502 *out_n_frames
= o_index
;
1504 trivial_data
->i_counter
+= in_n_frames
;
1506 /* Normalize counters */
1507 while (trivial_data
->i_counter
>= r
->i_ss
.rate
) {
1508 pa_assert(trivial_data
->o_counter
>= r
->o_ss
.rate
);
1510 trivial_data
->i_counter
-= r
->i_ss
.rate
;
1511 trivial_data
->o_counter
-= r
->o_ss
.rate
;
1515 static void trivial_update_rates_or_reset(pa_resampler
*r
) {
1516 struct trivial_data
*trivial_data
;
1519 trivial_data
= r
->impl_data
;
1521 trivial_data
->i_counter
= 0;
1522 trivial_data
->o_counter
= 0;
1525 static int trivial_init(pa_resampler
*r
) {
1526 struct trivial_data
*trivial_data
;
1529 trivial_data
= pa_xnew0(struct trivial_data
, 1);
1531 r
->impl_resample
= trivial_resample
;
1532 r
->impl_update_rates
= trivial_update_rates_or_reset
;
1533 r
->impl_reset
= trivial_update_rates_or_reset
;
1534 r
->impl_data
= trivial_data
;
1539 /* Peak finder implementation */
1541 static void peaks_resample(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
1542 unsigned c
, o_index
= 0;
1543 unsigned i
, i_end
= 0;
1545 struct peaks_data
*peaks_data
;
1550 pa_assert(out_n_frames
);
1552 peaks_data
= r
->impl_data
;
1553 src
= pa_memblock_acquire_chunk(input
);
1554 dst
= pa_memblock_acquire_chunk(output
);
1556 i
= ((uint64_t) peaks_data
->o_counter
* r
->i_ss
.rate
) / r
->o_ss
.rate
;
1557 i
= i
> peaks_data
->i_counter
? i
- peaks_data
->i_counter
: 0;
1559 while (i_end
< in_n_frames
) {
1560 i_end
= ((uint64_t) (peaks_data
->o_counter
+ 1) * r
->i_ss
.rate
) / r
->o_ss
.rate
;
1561 i_end
= i_end
> peaks_data
->i_counter
? i_end
- peaks_data
->i_counter
: 0;
1563 pa_assert_fp(o_index
* r
->w_sz
* r
->o_ss
.channels
< pa_memblock_get_length(output
->memblock
));
1565 /* 1ch float is treated separately, because that is the common case */
1566 if (r
->o_ss
.channels
== 1 && r
->work_format
== PA_SAMPLE_FLOAT32NE
) {
1567 float *s
= (float*) src
+ i
;
1568 float *d
= (float*) dst
+ o_index
;
1570 for (; i
< i_end
&& i
< in_n_frames
; i
++) {
1571 float n
= fabsf(*s
++);
1573 if (n
> peaks_data
->max_f
[0])
1574 peaks_data
->max_f
[0] = n
;
1578 *d
= peaks_data
->max_f
[0];
1579 peaks_data
->max_f
[0] = 0;
1580 o_index
++, peaks_data
->o_counter
++;
1582 } else if (r
->work_format
== PA_SAMPLE_S16NE
) {
1583 int16_t *s
= (int16_t*) src
+ r
->o_ss
.channels
* i
;
1584 int16_t *d
= (int16_t*) dst
+ r
->o_ss
.channels
* o_index
;
1586 for (; i
< i_end
&& i
< in_n_frames
; i
++)
1587 for (c
= 0; c
< r
->o_ss
.channels
; c
++) {
1588 int16_t n
= abs(*s
++);
1590 if (n
> peaks_data
->max_i
[c
])
1591 peaks_data
->max_i
[c
] = n
;
1595 for (c
= 0; c
< r
->o_ss
.channels
; c
++, d
++) {
1596 *d
= peaks_data
->max_i
[c
];
1597 peaks_data
->max_i
[c
] = 0;
1599 o_index
++, peaks_data
->o_counter
++;
1602 float *s
= (float*) src
+ r
->o_ss
.channels
* i
;
1603 float *d
= (float*) dst
+ r
->o_ss
.channels
* o_index
;
1605 for (; i
< i_end
&& i
< in_n_frames
; i
++)
1606 for (c
= 0; c
< r
->o_ss
.channels
; c
++) {
1607 float n
= fabsf(*s
++);
1609 if (n
> peaks_data
->max_f
[c
])
1610 peaks_data
->max_f
[c
] = n
;
1614 for (c
= 0; c
< r
->o_ss
.channels
; c
++, d
++) {
1615 *d
= peaks_data
->max_f
[c
];
1616 peaks_data
->max_f
[c
] = 0;
1618 o_index
++, peaks_data
->o_counter
++;
1623 pa_memblock_release(input
->memblock
);
1624 pa_memblock_release(output
->memblock
);
1626 *out_n_frames
= o_index
;
1628 peaks_data
->i_counter
+= in_n_frames
;
1630 /* Normalize counters */
1631 while (peaks_data
->i_counter
>= r
->i_ss
.rate
) {
1632 pa_assert(peaks_data
->o_counter
>= r
->o_ss
.rate
);
1634 peaks_data
->i_counter
-= r
->i_ss
.rate
;
1635 peaks_data
->o_counter
-= r
->o_ss
.rate
;
1639 static void peaks_update_rates_or_reset(pa_resampler
*r
) {
1640 struct peaks_data
*peaks_data
;
1643 peaks_data
= r
->impl_data
;
1645 peaks_data
->i_counter
= 0;
1646 peaks_data
->o_counter
= 0;
1649 static int peaks_init(pa_resampler
*r
) {
1650 struct peaks_data
*peaks_data
;
1652 pa_assert(r
->i_ss
.rate
>= r
->o_ss
.rate
);
1653 pa_assert(r
->work_format
== PA_SAMPLE_S16NE
|| r
->work_format
== PA_SAMPLE_FLOAT32NE
);
1655 peaks_data
= pa_xnew0(struct peaks_data
, 1);
1657 r
->impl_resample
= peaks_resample
;
1658 r
->impl_update_rates
= peaks_update_rates_or_reset
;
1659 r
->impl_reset
= peaks_update_rates_or_reset
;
1660 r
->impl_data
= peaks_data
;
1665 /*** ffmpeg based implementation ***/
1667 static void ffmpeg_resample(pa_resampler
*r
, const pa_memchunk
*input
, unsigned in_n_frames
, pa_memchunk
*output
, unsigned *out_n_frames
) {
1668 unsigned used_frames
= 0, c
;
1669 int previous_consumed_frames
= -1;
1670 struct ffmpeg_data
*ffmpeg_data
;
1675 pa_assert(out_n_frames
);
1677 ffmpeg_data
= r
->impl_data
;
1679 for (c
= 0; c
< r
->o_ss
.channels
; c
++) {
1682 int16_t *p
, *t
, *k
, *q
, *s
;
1683 int consumed_frames
;
1685 /* Allocate a new block */
1686 b
= pa_memblock_new(r
->mempool
, ffmpeg_data
->buf
[c
].length
+ in_n_frames
* sizeof(int16_t));
1687 p
= pa_memblock_acquire(b
);
1689 /* Now copy the input data, splitting up channels */
1690 t
= (int16_t*) pa_memblock_acquire_chunk(input
) + c
;
1692 for (u
= 0; u
< in_n_frames
; u
++) {
1694 t
+= r
->o_ss
.channels
;
1697 pa_memblock_release(input
->memblock
);
1699 /* Allocate buffer for the result */
1700 w
= pa_memblock_new(r
->mempool
, *out_n_frames
* sizeof(int16_t));
1701 q
= pa_memblock_acquire(w
);
1704 used_frames
= (unsigned) av_resample(ffmpeg_data
->state
,
1707 (int) in_n_frames
, (int) *out_n_frames
,
1708 c
>= (unsigned) (r
->o_ss
.channels
-1));
1710 pa_memblock_release(b
);
1711 pa_memblock_unref(b
);
1713 pa_assert(consumed_frames
<= (int) in_n_frames
);
1714 pa_assert(previous_consumed_frames
== -1 || consumed_frames
== previous_consumed_frames
);
1715 previous_consumed_frames
= consumed_frames
;
1717 /* And place the results in the output buffer */
1718 s
= (int16_t *) pa_memblock_acquire_chunk(output
) + c
;
1719 for (u
= 0; u
< used_frames
; u
++) {
1722 s
+= r
->o_ss
.channels
;
1724 pa_memblock_release(output
->memblock
);
1725 pa_memblock_release(w
);
1726 pa_memblock_unref(w
);
1729 if (previous_consumed_frames
< (int) in_n_frames
) {
1730 void *leftover_data
= (int16_t *) pa_memblock_acquire_chunk(input
) + previous_consumed_frames
* r
->o_ss
.channels
;
1731 size_t leftover_length
= (in_n_frames
- previous_consumed_frames
) * r
->o_ss
.channels
* sizeof(int16_t);
1733 save_leftover(r
, leftover_data
, leftover_length
);
1734 pa_memblock_release(input
->memblock
);
1737 *out_n_frames
= used_frames
;
1740 static void ffmpeg_free(pa_resampler
*r
) {
1742 struct ffmpeg_data
*ffmpeg_data
;
1746 ffmpeg_data
= r
->impl_data
;
1747 if (ffmpeg_data
->state
)
1748 av_resample_close(ffmpeg_data
->state
);
1750 for (c
= 0; c
< PA_ELEMENTSOF(ffmpeg_data
->buf
); c
++)
1751 if (ffmpeg_data
->buf
[c
].memblock
)
1752 pa_memblock_unref(ffmpeg_data
->buf
[c
].memblock
);
1755 static int ffmpeg_init(pa_resampler
*r
) {
1757 struct ffmpeg_data
*ffmpeg_data
;
1761 ffmpeg_data
= pa_xnew(struct ffmpeg_data
, 1);
1763 /* We could probably implement different quality levels by
1764 * adjusting the filter parameters here. However, ffmpeg
1765 * internally only uses these hardcoded values, so let's use them
1766 * here for now as well until ffmpeg makes this configurable. */
1768 if (!(ffmpeg_data
->state
= av_resample_init((int) r
->o_ss
.rate
, (int) r
->i_ss
.rate
, 16, 10, 0, 0.8)))
1771 r
->impl_free
= ffmpeg_free
;
1772 r
->impl_resample
= ffmpeg_resample
;
1773 r
->impl_data
= (void *) ffmpeg_data
;
1775 for (c
= 0; c
< PA_ELEMENTSOF(ffmpeg_data
->buf
); c
++)
1776 pa_memchunk_reset(&ffmpeg_data
->buf
[c
]);
1781 /*** copy (noop) implementation ***/
1783 static int copy_init(pa_resampler
*r
) {
1786 pa_assert(r
->o_ss
.rate
== r
->i_ss
.rate
);